bfd/
[binutils.git] / gold / output.h
bloba004515dc9a0b63e468b2c4d74c81ae6a2504281
1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007 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*);
389 protected:
390 // Write the data to the file.
391 void
392 do_write(Output_file*);
394 // Return the required alignment.
395 uint64_t
396 do_addralign() const
397 { return Output_data::default_alignment(); }
399 private:
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
402 void
403 do_sized_write(Output_file*);
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
407 const Layout::Section_list* section_list_;
408 const Layout::Section_list* unattached_section_list_;
409 const Stringpool* secnamepool_;
412 // Output the segment headers.
414 class Output_segment_headers : public Output_data
416 public:
417 Output_segment_headers(const Layout::Segment_list& segment_list);
419 protected:
420 // Write the data to the file.
421 void
422 do_write(Output_file*);
424 // Return the required alignment.
425 uint64_t
426 do_addralign() const
427 { return Output_data::default_alignment(); }
429 private:
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
432 void
433 do_sized_write(Output_file*);
435 const Layout::Segment_list& segment_list_;
438 // Output the ELF file header.
440 class Output_file_header : public Output_data
442 public:
443 Output_file_header(const Target*,
444 const Symbol_table*,
445 const Output_segment_headers*,
446 const char* entry);
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
453 protected:
454 // Write the data to the file.
455 void
456 do_write(Output_file*);
458 // Return the required alignment.
459 uint64_t
460 do_addralign() const
461 { return Output_data::default_alignment(); }
463 private:
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
466 void
467 do_sized_write(Output_file*);
469 // Return the value to use for the entry address.
470 template<int size>
471 typename elfcpp::Elf_types<size>::Elf_Addr
472 entry();
474 const Target* target_;
475 const Symbol_table* symtab_;
476 const Output_segment_headers* segment_header_;
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
479 const char* entry_;
482 // Output sections are mainly comprised of input sections. However,
483 // there are cases where we have data to write out which is not in an
484 // input section. Output_section_data is used in such cases. This is
485 // an abstract base class.
487 class Output_section_data : public Output_data
489 public:
490 Output_section_data(off_t data_size, uint64_t addralign)
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
494 Output_section_data(uint64_t addralign)
495 : Output_data(), output_section_(NULL), addralign_(addralign)
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
503 // Record the output section.
504 void
505 set_output_section(Output_section* os);
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
509 bool
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
519 bool
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
523 { return this->do_output_offset(object, shndx, offset, poutput); }
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
528 bool
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
534 void
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
540 void
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
544 protected:
545 // The child class must implement do_write.
547 // The child class may implement specific adjustments to the output
548 // section.
549 virtual void
550 do_adjust_output_section(Output_section*)
553 // May be implemented by child class. Return true if the section
554 // was handled.
555 virtual bool
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
559 // The child class may implement output_offset.
560 virtual bool
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
563 { return false; }
565 // The child class may implement is_merge_section_for.
566 virtual bool
567 do_is_merge_section_for(const Relobj*, unsigned int) const
568 { return false; }
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
572 // implement this.
573 virtual void
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
577 // Print merge statistics.
578 virtual void
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
582 // Return the required alignment.
583 uint64_t
584 do_addralign() const
585 { return this->addralign_; }
587 // Return the output section.
588 Output_section*
589 do_output_section()
590 { return this->output_section_; }
592 // Return the section index of the output section.
593 unsigned int
594 do_out_shndx() const;
596 // Set the alignment.
597 void
598 set_addralign(uint64_t addralign)
599 { this->addralign_ = addralign; }
601 private:
602 // The output section for this section.
603 Output_section* output_section_;
604 // The required alignment.
605 uint64_t addralign_;
608 // Some Output_section_data classes build up their data step by step,
609 // rather than all at once. This class provides an interface for
610 // them.
612 class Output_section_data_build : public Output_section_data
614 public:
615 Output_section_data_build(uint64_t addralign)
616 : Output_section_data(addralign)
619 // Get the current data size.
620 off_t
621 current_data_size() const
622 { return this->current_data_size_for_child(); }
624 // Set the current data size.
625 void
626 set_current_data_size(off_t data_size)
627 { this->set_current_data_size_for_child(data_size); }
629 protected:
630 // Set the final data size.
631 virtual void
632 set_final_data_size()
633 { this->set_data_size(this->current_data_size_for_child()); }
636 // A simple case of Output_data in which we have constant data to
637 // output.
639 class Output_data_const : public Output_section_data
641 public:
642 Output_data_const(const std::string& data, uint64_t addralign)
643 : Output_section_data(data.size(), addralign), data_(data)
646 Output_data_const(const char* p, off_t len, uint64_t addralign)
647 : Output_section_data(len, addralign), data_(p, len)
650 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
651 : Output_section_data(len, addralign),
652 data_(reinterpret_cast<const char*>(p), len)
655 protected:
656 // Write the data to the output file.
657 void
658 do_write(Output_file*);
660 // Write the data to a buffer.
661 void
662 do_write_to_buffer(unsigned char* buffer)
663 { memcpy(buffer, this->data_.data(), this->data_.size()); }
665 private:
666 std::string data_;
669 // Another version of Output_data with constant data, in which the
670 // buffer is allocated by the caller.
672 class Output_data_const_buffer : public Output_section_data
674 public:
675 Output_data_const_buffer(const unsigned char* p, off_t len,
676 uint64_t addralign)
677 : Output_section_data(len, addralign), p_(p)
680 protected:
681 // Write the data the output file.
682 void
683 do_write(Output_file*);
685 // Write the data to a buffer.
686 void
687 do_write_to_buffer(unsigned char* buffer)
688 { memcpy(buffer, this->p_, this->data_size()); }
690 private:
691 const unsigned char* p_;
694 // A place holder for a fixed amount of data written out via some
695 // other mechanism.
697 class Output_data_fixed_space : public Output_section_data
699 public:
700 Output_data_fixed_space(off_t data_size, uint64_t addralign)
701 : Output_section_data(data_size, addralign)
704 protected:
705 // Write out the data--the actual data must be written out
706 // elsewhere.
707 void
708 do_write(Output_file*)
712 // A place holder for variable sized data written out via some other
713 // mechanism.
715 class Output_data_space : public Output_section_data_build
717 public:
718 explicit Output_data_space(uint64_t addralign)
719 : Output_section_data_build(addralign)
722 // Set the alignment.
723 void
724 set_space_alignment(uint64_t align)
725 { this->set_addralign(align); }
727 protected:
728 // Write out the data--the actual data must be written out
729 // elsewhere.
730 void
731 do_write(Output_file*)
735 // A string table which goes into an output section.
737 class Output_data_strtab : public Output_section_data
739 public:
740 Output_data_strtab(Stringpool* strtab)
741 : Output_section_data(1), strtab_(strtab)
744 protected:
745 // This is called to set the address and file offset. Here we make
746 // sure that the Stringpool is finalized.
747 void
748 set_final_data_size();
750 // Write out the data.
751 void
752 do_write(Output_file*);
754 // Write the data to a buffer.
755 void
756 do_write_to_buffer(unsigned char* buffer)
757 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
759 private:
760 Stringpool* strtab_;
763 // This POD class is used to represent a single reloc in the output
764 // file. This could be a private class within Output_data_reloc, but
765 // the templatization is complex enough that I broke it out into a
766 // separate class. The class is templatized on either elfcpp::SHT_REL
767 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
768 // relocation or an ordinary relocation.
770 // A relocation can be against a global symbol, a local symbol, a
771 // local section symbol, an output section, or the undefined symbol at
772 // index 0. We represent the latter by using a NULL global symbol.
774 template<int sh_type, bool dynamic, int size, bool big_endian>
775 class Output_reloc;
777 template<bool dynamic, int size, bool big_endian>
778 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
780 public:
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
783 // An uninitialized entry. We need this because we want to put
784 // instances of this class into an STL container.
785 Output_reloc()
786 : local_sym_index_(INVALID_CODE)
789 // We have a bunch of different constructors. They come in pairs
790 // depending on how the address of the relocation is specified. It
791 // can either be an offset in an Output_data or an offset in an
792 // input section.
794 // A reloc against a global symbol.
796 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
797 Address address, bool is_relative);
799 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
800 unsigned int shndx, Address address, bool is_relative);
802 // A reloc against a local symbol or local section symbol.
804 Output_reloc(Sized_relobj<size, big_endian>* relobj,
805 unsigned int local_sym_index, unsigned int type,
806 Output_data* od, Address address, bool is_relative,
807 bool is_section_symbol);
809 Output_reloc(Sized_relobj<size, big_endian>* relobj,
810 unsigned int local_sym_index, unsigned int type,
811 unsigned int shndx, Address address, bool is_relative,
812 bool is_section_symbol);
814 // A reloc against the STT_SECTION symbol of an output section.
816 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
817 Address address);
819 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
820 unsigned int shndx, Address address);
822 // Return TRUE if this is a RELATIVE relocation.
823 bool
824 is_relative() const
825 { return this->is_relative_; }
827 // Return whether this is against a local section symbol.
828 bool
829 is_local_section_symbol() const
831 return (this->local_sym_index_ != GSYM_CODE
832 && this->local_sym_index_ != SECTION_CODE
833 && this->local_sym_index_ != INVALID_CODE
834 && this->is_section_symbol_);
837 // For a local section symbol, return the offset of the input
838 // section within the output section.
839 section_offset_type
840 local_section_offset() const;
842 // Get the value of the symbol referred to by a Rel relocation.
844 Address
845 symbol_value() const;
847 // Write the reloc entry to an output view.
848 void
849 write(unsigned char* pov) const;
851 // Write the offset and info fields to Write_rel.
852 template<typename Write_rel>
853 void write_rel(Write_rel*) const;
855 private:
856 // Record that we need a dynamic symbol index.
857 void
858 set_needs_dynsym_index();
860 // Return the symbol index.
861 unsigned int
862 get_symbol_index() const;
864 // Codes for local_sym_index_.
865 enum
867 // Global symbol.
868 GSYM_CODE = -1U,
869 // Output section.
870 SECTION_CODE = -2U,
871 // Invalid uninitialized entry.
872 INVALID_CODE = -3U
875 union
877 // For a local symbol or local section symbol
878 // (this->local_sym_index_ >= 0), the object. We will never
879 // generate a relocation against a local symbol in a dynamic
880 // object; that doesn't make sense. And our callers will always
881 // be templatized, so we use Sized_relobj here.
882 Sized_relobj<size, big_endian>* relobj;
883 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
884 // symbol. If this is NULL, it indicates a relocation against the
885 // undefined 0 symbol.
886 Symbol* gsym;
887 // For a relocation against an output section
888 // (this->local_sym_index_ == SECTION_CODE), the output section.
889 Output_section* os;
890 } u1_;
891 union
893 // If this->shndx_ is not INVALID CODE, the object which holds the
894 // input section being used to specify the reloc address.
895 Relobj* relobj;
896 // If this->shndx_ is INVALID_CODE, the output data being used to
897 // specify the reloc address. This may be NULL if the reloc
898 // address is absolute.
899 Output_data* od;
900 } u2_;
901 // The address offset within the input section or the Output_data.
902 Address address_;
903 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
904 // relocation against an output section, or INVALID_CODE for an
905 // uninitialized value. Otherwise, for a local symbol
906 // (this->is_section_symbol_ is false), the local symbol index. For
907 // a local section symbol (this->is_section_symbol_ is true), the
908 // section index in the input file.
909 unsigned int local_sym_index_;
910 // The reloc type--a processor specific code.
911 unsigned int type_ : 30;
912 // True if the relocation is a RELATIVE relocation.
913 bool is_relative_ : 1;
914 // True if the relocation is against a section symbol.
915 bool is_section_symbol_ : 1;
916 // If the reloc address is an input section in an object, the
917 // section index. This is INVALID_CODE if the reloc address is
918 // specified in some other way.
919 unsigned int shndx_;
922 // The SHT_RELA version of Output_reloc<>. This is just derived from
923 // the SHT_REL version of Output_reloc, but it adds an addend.
925 template<bool dynamic, int size, bool big_endian>
926 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
928 public:
929 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
930 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
932 // An uninitialized entry.
933 Output_reloc()
934 : rel_()
937 // A reloc against a global symbol.
939 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
940 Address address, Addend addend, bool is_relative)
941 : rel_(gsym, type, od, address, is_relative), addend_(addend)
944 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
945 unsigned int shndx, Address address, Addend addend,
946 bool is_relative)
947 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
950 // A reloc against a local symbol.
952 Output_reloc(Sized_relobj<size, big_endian>* relobj,
953 unsigned int local_sym_index, unsigned int type,
954 Output_data* od, Address address,
955 Addend addend, bool is_relative, bool is_section_symbol)
956 : rel_(relobj, local_sym_index, type, od, address, is_relative,
957 is_section_symbol),
958 addend_(addend)
961 Output_reloc(Sized_relobj<size, big_endian>* relobj,
962 unsigned int local_sym_index, unsigned int type,
963 unsigned int shndx, Address address,
964 Addend addend, bool is_relative, bool is_section_symbol)
965 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
966 is_section_symbol),
967 addend_(addend)
970 // A reloc against the STT_SECTION symbol of an output section.
972 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
973 Address address, Addend addend)
974 : rel_(os, type, od, address), addend_(addend)
977 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
978 unsigned int shndx, Address address, Addend addend)
979 : rel_(os, type, relobj, shndx, address), addend_(addend)
982 // Write the reloc entry to an output view.
983 void
984 write(unsigned char* pov) const;
986 private:
987 // The basic reloc.
988 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
989 // The addend.
990 Addend addend_;
993 // Output_data_reloc is used to manage a section containing relocs.
994 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
995 // indicates whether this is a dynamic relocation or a normal
996 // relocation. Output_data_reloc_base is a base class.
997 // Output_data_reloc is the real class, which we specialize based on
998 // the reloc type.
1000 template<int sh_type, bool dynamic, int size, bool big_endian>
1001 class Output_data_reloc_base : public Output_section_data_build
1003 public:
1004 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1005 typedef typename Output_reloc_type::Address Address;
1006 static const int reloc_size =
1007 Reloc_types<sh_type, size, big_endian>::reloc_size;
1009 // Construct the section.
1010 Output_data_reloc_base()
1011 : Output_section_data_build(Output_data::default_alignment_for_size(size))
1014 protected:
1015 // Write out the data.
1016 void
1017 do_write(Output_file*);
1019 // Set the entry size and the link.
1020 void
1021 do_adjust_output_section(Output_section *os);
1023 // Add a relocation entry.
1024 void
1025 add(Output_data *od, const Output_reloc_type& reloc)
1027 this->relocs_.push_back(reloc);
1028 this->set_current_data_size(this->relocs_.size() * reloc_size);
1029 od->add_dynamic_reloc();
1032 private:
1033 typedef std::vector<Output_reloc_type> Relocs;
1035 Relocs relocs_;
1038 // The class which callers actually create.
1040 template<int sh_type, bool dynamic, int size, bool big_endian>
1041 class Output_data_reloc;
1043 // The SHT_REL version of Output_data_reloc.
1045 template<bool dynamic, int size, bool big_endian>
1046 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1047 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1049 private:
1050 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1051 big_endian> Base;
1053 public:
1054 typedef typename Base::Output_reloc_type Output_reloc_type;
1055 typedef typename Output_reloc_type::Address Address;
1057 Output_data_reloc()
1058 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1061 // Add a reloc against a global symbol.
1063 void
1064 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1065 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1067 void
1068 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1069 unsigned int shndx, Address address)
1070 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1071 false)); }
1073 // Add a RELATIVE reloc against a global symbol. The final relocation
1074 // will not reference the symbol.
1076 void
1077 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1078 Address address)
1079 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1081 void
1082 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1083 Relobj* relobj, unsigned int shndx, Address address)
1085 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1086 true));
1089 // Add a reloc against a local symbol.
1091 void
1092 add_local(Sized_relobj<size, big_endian>* relobj,
1093 unsigned int local_sym_index, unsigned int type,
1094 Output_data* od, Address address)
1096 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1097 address, false, false));
1100 void
1101 add_local(Sized_relobj<size, big_endian>* relobj,
1102 unsigned int local_sym_index, unsigned int type,
1103 Output_data* od, unsigned int shndx, Address address)
1105 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1106 address, false, false));
1109 // Add a RELATIVE reloc against a local symbol.
1111 void
1112 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1113 unsigned int local_sym_index, unsigned int type,
1114 Output_data* od, Address address)
1116 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1117 address, true, false));
1120 void
1121 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1122 unsigned int local_sym_index, unsigned int type,
1123 Output_data* od, unsigned int shndx, Address address)
1125 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1126 address, true, false));
1129 // Add a reloc against a local section symbol. This will be
1130 // converted into a reloc against the STT_SECTION symbol of the
1131 // output section.
1133 void
1134 add_local_section(Sized_relobj<size, big_endian>* relobj,
1135 unsigned int input_shndx, unsigned int type,
1136 Output_data* od, Address address)
1138 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1139 address, false, true));
1142 void
1143 add_local_section(Sized_relobj<size, big_endian>* relobj,
1144 unsigned int input_shndx, unsigned int type,
1145 Output_data* od, unsigned int shndx, Address address)
1147 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1148 address, false, true));
1151 // A reloc against the STT_SECTION symbol of an output section.
1152 // OS is the Output_section that the relocation refers to; OD is
1153 // the Output_data object being relocated.
1155 void
1156 add_output_section(Output_section* os, unsigned int type,
1157 Output_data* od, Address address)
1158 { this->add(od, Output_reloc_type(os, type, od, address)); }
1160 void
1161 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1162 Relobj* relobj, unsigned int shndx, Address address)
1163 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1166 // The SHT_RELA version of Output_data_reloc.
1168 template<bool dynamic, int size, bool big_endian>
1169 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1170 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1172 private:
1173 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1174 big_endian> Base;
1176 public:
1177 typedef typename Base::Output_reloc_type Output_reloc_type;
1178 typedef typename Output_reloc_type::Address Address;
1179 typedef typename Output_reloc_type::Addend Addend;
1181 Output_data_reloc()
1182 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1185 // Add a reloc against a global symbol.
1187 void
1188 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1189 Address address, Addend addend)
1190 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1191 false)); }
1193 void
1194 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1195 unsigned int shndx, Address address,
1196 Addend addend)
1197 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1198 addend, false)); }
1200 // Add a RELATIVE reloc against a global symbol. The final output
1201 // relocation will not reference the symbol, but we must keep the symbol
1202 // information long enough to set the addend of the relocation correctly
1203 // when it is written.
1205 void
1206 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1207 Address address, Addend addend)
1208 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1210 void
1211 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1212 Relobj* relobj, unsigned int shndx, Address address,
1213 Addend addend)
1214 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1215 addend, true)); }
1217 // Add a reloc against a local symbol.
1219 void
1220 add_local(Sized_relobj<size, big_endian>* relobj,
1221 unsigned int local_sym_index, unsigned int type,
1222 Output_data* od, Address address, Addend addend)
1224 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1225 addend, false, false));
1228 void
1229 add_local(Sized_relobj<size, big_endian>* relobj,
1230 unsigned int local_sym_index, unsigned int type,
1231 Output_data* od, unsigned int shndx, Address address,
1232 Addend addend)
1234 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1235 address, addend, false, false));
1238 // Add a RELATIVE reloc against a local symbol.
1240 void
1241 add_local_relative(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, true, false));
1249 void
1250 add_local_relative(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, true, false));
1259 // Add a reloc against a local section symbol. This will be
1260 // converted into a reloc against the STT_SECTION symbol of the
1261 // output section.
1263 void
1264 add_local_section(Sized_relobj<size, big_endian>* relobj,
1265 unsigned int input_shndx, unsigned int type,
1266 Output_data* od, Address address, Addend addend)
1268 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1269 addend, false, true));
1272 void
1273 add_local_section(Sized_relobj<size, big_endian>* relobj,
1274 unsigned int input_shndx, unsigned int type,
1275 Output_data* od, unsigned int shndx, Address address,
1276 Addend addend)
1278 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1279 address, addend, false, true));
1282 // A reloc against the STT_SECTION symbol of an output section.
1284 void
1285 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1286 Address address, Addend addend)
1287 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1289 void
1290 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1291 unsigned int shndx, Address address, Addend addend)
1292 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1293 addend)); }
1296 // Output_relocatable_relocs represents a relocation section in a
1297 // relocatable link. The actual data is written out in the target
1298 // hook relocate_for_relocatable. This just saves space for it.
1300 template<int sh_type, int size, bool big_endian>
1301 class Output_relocatable_relocs : public Output_section_data
1303 public:
1304 Output_relocatable_relocs(Relocatable_relocs* rr)
1305 : Output_section_data(Output_data::default_alignment_for_size(size)),
1306 rr_(rr)
1309 void
1310 set_final_data_size();
1312 // Write out the data. There is nothing to do here.
1313 void
1314 do_write(Output_file*)
1317 private:
1318 // The relocs associated with this input section.
1319 Relocatable_relocs* rr_;
1322 // Handle a GROUP section.
1324 template<int size, bool big_endian>
1325 class Output_data_group : public Output_section_data
1327 public:
1328 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1329 section_size_type entry_count,
1330 const elfcpp::Elf_Word* contents);
1332 void
1333 do_write(Output_file*);
1335 private:
1336 // The input object.
1337 Sized_relobj<size, big_endian>* relobj_;
1338 // The group flag word.
1339 elfcpp::Elf_Word flags_;
1340 // The section indexes of the input sections in this group.
1341 std::vector<unsigned int> input_sections_;
1344 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1345 // for one symbol--either a global symbol or a local symbol in an
1346 // object. The target specific code adds entries to the GOT as
1347 // needed.
1349 template<int size, bool big_endian>
1350 class Output_data_got : public Output_section_data_build
1352 public:
1353 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1354 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1355 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1357 Output_data_got()
1358 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1359 entries_()
1362 // Add an entry for a global symbol to the GOT. Return true if this
1363 // is a new GOT entry, false if the symbol was already in the GOT.
1364 bool
1365 add_global(Symbol* gsym);
1367 // Add an entry for a global symbol to the GOT, and add a dynamic
1368 // relocation of type R_TYPE for the GOT entry.
1369 void
1370 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1372 void
1373 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1375 // Add an entry for a local symbol to the GOT. This returns true if
1376 // this is a new GOT entry, false if the symbol already has a GOT
1377 // entry.
1378 bool
1379 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1381 // Add an entry for a global symbol to the GOT, and add a dynamic
1382 // relocation of type R_TYPE for the GOT entry.
1383 void
1384 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1385 unsigned int sym_index, Rel_dyn* rel_dyn,
1386 unsigned int r_type);
1388 void
1389 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1390 unsigned int sym_index, Rela_dyn* rela_dyn,
1391 unsigned int r_type);
1393 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1394 // Return true if this is a new GOT entry, false if the symbol was
1395 // already in the GOT.
1396 bool
1397 add_global_tls(Symbol* gsym, bool need_pair);
1399 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1400 // relocation of type R_TYPE.
1401 void
1402 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1403 unsigned int r_type);
1405 void
1406 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1407 unsigned int r_type);
1409 // Add a pair of entries for a global TLS symbol to the GOT, and add
1410 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1411 void
1412 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1413 unsigned int mod_r_type,
1414 unsigned int dtv_r_type);
1416 void
1417 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1418 unsigned int mod_r_type,
1419 unsigned int dtv_r_type);
1421 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1422 // This returns true if this is a new GOT entry, false if the symbol
1423 // already has a GOT entry.
1424 bool
1425 add_local_tls(Sized_relobj<size, big_endian>* object,
1426 unsigned int sym_index, bool need_pair);
1428 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1429 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1430 // Because this is a local symbol, the first GOT entry can be relocated
1431 // relative to a section symbol, and the second GOT entry will have an
1432 // dtv-relative value that can be computed at link time.
1433 void
1434 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1435 unsigned int sym_index, unsigned int shndx,
1436 bool need_pair, Rel_dyn* rel_dyn,
1437 unsigned int r_type);
1439 void
1440 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1441 unsigned int sym_index, unsigned int shndx,
1442 bool need_pair, Rela_dyn* rela_dyn,
1443 unsigned int r_type);
1445 // Add a constant to the GOT. This returns the offset of the new
1446 // entry from the start of the GOT.
1447 unsigned int
1448 add_constant(Valtype constant)
1450 this->entries_.push_back(Got_entry(constant));
1451 this->set_got_size();
1452 return this->last_got_offset();
1455 protected:
1456 // Write out the GOT table.
1457 void
1458 do_write(Output_file*);
1460 private:
1461 // This POD class holds a single GOT entry.
1462 class Got_entry
1464 public:
1465 // Create a zero entry.
1466 Got_entry()
1467 : local_sym_index_(CONSTANT_CODE)
1468 { this->u_.constant = 0; }
1470 // Create a global symbol entry.
1471 explicit Got_entry(Symbol* gsym)
1472 : local_sym_index_(GSYM_CODE)
1473 { this->u_.gsym = gsym; }
1475 // Create a local symbol entry.
1476 Got_entry(Sized_relobj<size, big_endian>* object,
1477 unsigned int local_sym_index)
1478 : local_sym_index_(local_sym_index)
1480 gold_assert(local_sym_index != GSYM_CODE
1481 && local_sym_index != CONSTANT_CODE);
1482 this->u_.object = object;
1485 // Create a constant entry. The constant is a host value--it will
1486 // be swapped, if necessary, when it is written out.
1487 explicit Got_entry(Valtype constant)
1488 : local_sym_index_(CONSTANT_CODE)
1489 { this->u_.constant = constant; }
1491 // Write the GOT entry to an output view.
1492 void
1493 write(unsigned char* pov) const;
1495 private:
1496 enum
1498 GSYM_CODE = -1U,
1499 CONSTANT_CODE = -2U
1502 union
1504 // For a local symbol, the object.
1505 Sized_relobj<size, big_endian>* object;
1506 // For a global symbol, the symbol.
1507 Symbol* gsym;
1508 // For a constant, the constant.
1509 Valtype constant;
1510 } u_;
1511 // For a local symbol, the local symbol index. This is GSYM_CODE
1512 // for a global symbol, or CONSTANT_CODE for a constant.
1513 unsigned int local_sym_index_;
1516 typedef std::vector<Got_entry> Got_entries;
1518 // Return the offset into the GOT of GOT entry I.
1519 unsigned int
1520 got_offset(unsigned int i) const
1521 { return i * (size / 8); }
1523 // Return the offset into the GOT of the last entry added.
1524 unsigned int
1525 last_got_offset() const
1526 { return this->got_offset(this->entries_.size() - 1); }
1528 // Set the size of the section.
1529 void
1530 set_got_size()
1531 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1533 // The list of GOT entries.
1534 Got_entries entries_;
1537 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1538 // section.
1540 class Output_data_dynamic : public Output_section_data
1542 public:
1543 Output_data_dynamic(Stringpool* pool)
1544 : Output_section_data(Output_data::default_alignment()),
1545 entries_(), pool_(pool)
1548 // Add a new dynamic entry with a fixed numeric value.
1549 void
1550 add_constant(elfcpp::DT tag, unsigned int val)
1551 { this->add_entry(Dynamic_entry(tag, val)); }
1553 // Add a new dynamic entry with the address of output data.
1554 void
1555 add_section_address(elfcpp::DT tag, const Output_data* od)
1556 { this->add_entry(Dynamic_entry(tag, od, false)); }
1558 // Add a new dynamic entry with the size of output data.
1559 void
1560 add_section_size(elfcpp::DT tag, const Output_data* od)
1561 { this->add_entry(Dynamic_entry(tag, od, true)); }
1563 // Add a new dynamic entry with the address of a symbol.
1564 void
1565 add_symbol(elfcpp::DT tag, const Symbol* sym)
1566 { this->add_entry(Dynamic_entry(tag, sym)); }
1568 // Add a new dynamic entry with a string.
1569 void
1570 add_string(elfcpp::DT tag, const char* str)
1571 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1573 void
1574 add_string(elfcpp::DT tag, const std::string& str)
1575 { this->add_string(tag, str.c_str()); }
1577 protected:
1578 // Adjust the output section to set the entry size.
1579 void
1580 do_adjust_output_section(Output_section*);
1582 // Set the final data size.
1583 void
1584 set_final_data_size();
1586 // Write out the dynamic entries.
1587 void
1588 do_write(Output_file*);
1590 private:
1591 // This POD class holds a single dynamic entry.
1592 class Dynamic_entry
1594 public:
1595 // Create an entry with a fixed numeric value.
1596 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1597 : tag_(tag), classification_(DYNAMIC_NUMBER)
1598 { this->u_.val = val; }
1600 // Create an entry with the size or address of a section.
1601 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1602 : tag_(tag),
1603 classification_(section_size
1604 ? DYNAMIC_SECTION_SIZE
1605 : DYNAMIC_SECTION_ADDRESS)
1606 { this->u_.od = od; }
1608 // Create an entry with the address of a symbol.
1609 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1610 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1611 { this->u_.sym = sym; }
1613 // Create an entry with a string.
1614 Dynamic_entry(elfcpp::DT tag, const char* str)
1615 : tag_(tag), classification_(DYNAMIC_STRING)
1616 { this->u_.str = str; }
1618 // Write the dynamic entry to an output view.
1619 template<int size, bool big_endian>
1620 void
1621 write(unsigned char* pov, const Stringpool*) const;
1623 private:
1624 enum Classification
1626 // Number.
1627 DYNAMIC_NUMBER,
1628 // Section address.
1629 DYNAMIC_SECTION_ADDRESS,
1630 // Section size.
1631 DYNAMIC_SECTION_SIZE,
1632 // Symbol adress.
1633 DYNAMIC_SYMBOL,
1634 // String.
1635 DYNAMIC_STRING
1638 union
1640 // For DYNAMIC_NUMBER.
1641 unsigned int val;
1642 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1643 const Output_data* od;
1644 // For DYNAMIC_SYMBOL.
1645 const Symbol* sym;
1646 // For DYNAMIC_STRING.
1647 const char* str;
1648 } u_;
1649 // The dynamic tag.
1650 elfcpp::DT tag_;
1651 // The type of entry.
1652 Classification classification_;
1655 // Add an entry to the list.
1656 void
1657 add_entry(const Dynamic_entry& entry)
1658 { this->entries_.push_back(entry); }
1660 // Sized version of write function.
1661 template<int size, bool big_endian>
1662 void
1663 sized_write(Output_file* of);
1665 // The type of the list of entries.
1666 typedef std::vector<Dynamic_entry> Dynamic_entries;
1668 // The entries.
1669 Dynamic_entries entries_;
1670 // The pool used for strings.
1671 Stringpool* pool_;
1674 // An output section. We don't expect to have too many output
1675 // sections, so we don't bother to do a template on the size.
1677 class Output_section : public Output_data
1679 public:
1680 // Create an output section, giving the name, type, and flags.
1681 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1682 virtual ~Output_section();
1684 // Add a new input section SHNDX, named NAME, with header SHDR, from
1685 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1686 // which applies to this section, or 0 if none, or -1U if more than
1687 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1688 // in a linker script; in that case we need to keep track of input
1689 // sections associated with an output section. Return the offset
1690 // within the output section.
1691 template<int size, bool big_endian>
1692 off_t
1693 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1694 const char *name,
1695 const elfcpp::Shdr<size, big_endian>& shdr,
1696 unsigned int reloc_shndx, bool have_sections_script);
1698 // Add generated data POSD to this output section.
1699 void
1700 add_output_section_data(Output_section_data* posd);
1702 // Return the section name.
1703 const char*
1704 name() const
1705 { return this->name_; }
1707 // Return the section type.
1708 elfcpp::Elf_Word
1709 type() const
1710 { return this->type_; }
1712 // Return the section flags.
1713 elfcpp::Elf_Xword
1714 flags() const
1715 { return this->flags_; }
1717 // Return the entsize field.
1718 uint64_t
1719 entsize() const
1720 { return this->entsize_; }
1722 // Set the entsize field.
1723 void
1724 set_entsize(uint64_t v);
1726 // Set the load address.
1727 void
1728 set_load_address(uint64_t load_address)
1730 this->load_address_ = load_address;
1731 this->has_load_address_ = true;
1734 // Set the link field to the output section index of a section.
1735 void
1736 set_link_section(const Output_data* od)
1738 gold_assert(this->link_ == 0
1739 && !this->should_link_to_symtab_
1740 && !this->should_link_to_dynsym_);
1741 this->link_section_ = od;
1744 // Set the link field to a constant.
1745 void
1746 set_link(unsigned int v)
1748 gold_assert(this->link_section_ == NULL
1749 && !this->should_link_to_symtab_
1750 && !this->should_link_to_dynsym_);
1751 this->link_ = v;
1754 // Record that this section should link to the normal symbol table.
1755 void
1756 set_should_link_to_symtab()
1758 gold_assert(this->link_section_ == NULL
1759 && this->link_ == 0
1760 && !this->should_link_to_dynsym_);
1761 this->should_link_to_symtab_ = true;
1764 // Record that this section should link to the dynamic symbol table.
1765 void
1766 set_should_link_to_dynsym()
1768 gold_assert(this->link_section_ == NULL
1769 && this->link_ == 0
1770 && !this->should_link_to_symtab_);
1771 this->should_link_to_dynsym_ = true;
1774 // Return the info field.
1775 unsigned int
1776 info() const
1778 gold_assert(this->info_section_ == NULL
1779 && this->info_symndx_ == NULL);
1780 return this->info_;
1783 // Set the info field to the output section index of a section.
1784 void
1785 set_info_section(const Output_section* os)
1787 gold_assert((this->info_section_ == NULL
1788 || (this->info_section_ == os
1789 && this->info_uses_section_index_))
1790 && this->info_symndx_ == NULL
1791 && this->info_ == 0);
1792 this->info_section_ = os;
1793 this->info_uses_section_index_= true;
1796 // Set the info field to the symbol table index of a symbol.
1797 void
1798 set_info_symndx(const Symbol* sym)
1800 gold_assert(this->info_section_ == NULL
1801 && (this->info_symndx_ == NULL
1802 || this->info_symndx_ == sym)
1803 && this->info_ == 0);
1804 this->info_symndx_ = sym;
1807 // Set the info field to the symbol table index of a section symbol.
1808 void
1809 set_info_section_symndx(const Output_section* os)
1811 gold_assert((this->info_section_ == NULL
1812 || (this->info_section_ == os
1813 && !this->info_uses_section_index_))
1814 && this->info_symndx_ == NULL
1815 && this->info_ == 0);
1816 this->info_section_ = os;
1817 this->info_uses_section_index_ = false;
1820 // Set the info field to a constant.
1821 void
1822 set_info(unsigned int v)
1824 gold_assert(this->info_section_ == NULL
1825 && this->info_symndx_ == NULL
1826 && (this->info_ == 0
1827 || this->info_ == v));
1828 this->info_ = v;
1831 // Set the addralign field.
1832 void
1833 set_addralign(uint64_t v)
1834 { this->addralign_ = v; }
1836 // Indicate that we need a symtab index.
1837 void
1838 set_needs_symtab_index()
1839 { this->needs_symtab_index_ = true; }
1841 // Return whether we need a symtab index.
1842 bool
1843 needs_symtab_index() const
1844 { return this->needs_symtab_index_; }
1846 // Get the symtab index.
1847 unsigned int
1848 symtab_index() const
1850 gold_assert(this->symtab_index_ != 0);
1851 return this->symtab_index_;
1854 // Set the symtab index.
1855 void
1856 set_symtab_index(unsigned int index)
1858 gold_assert(index != 0);
1859 this->symtab_index_ = index;
1862 // Indicate that we need a dynsym index.
1863 void
1864 set_needs_dynsym_index()
1865 { this->needs_dynsym_index_ = true; }
1867 // Return whether we need a dynsym index.
1868 bool
1869 needs_dynsym_index() const
1870 { return this->needs_dynsym_index_; }
1872 // Get the dynsym index.
1873 unsigned int
1874 dynsym_index() const
1876 gold_assert(this->dynsym_index_ != 0);
1877 return this->dynsym_index_;
1880 // Set the dynsym index.
1881 void
1882 set_dynsym_index(unsigned int index)
1884 gold_assert(index != 0);
1885 this->dynsym_index_ = index;
1888 // Return whether this section should be written after all the input
1889 // sections are complete.
1890 bool
1891 after_input_sections() const
1892 { return this->after_input_sections_; }
1894 // Record that this section should be written after all the input
1895 // sections are complete.
1896 void
1897 set_after_input_sections()
1898 { this->after_input_sections_ = true; }
1900 // Return whether this section requires postprocessing after all
1901 // relocations have been applied.
1902 bool
1903 requires_postprocessing() const
1904 { return this->requires_postprocessing_; }
1906 // If a section requires postprocessing, return the buffer to use.
1907 unsigned char*
1908 postprocessing_buffer() const
1910 gold_assert(this->postprocessing_buffer_ != NULL);
1911 return this->postprocessing_buffer_;
1914 // If a section requires postprocessing, create the buffer to use.
1915 void
1916 create_postprocessing_buffer();
1918 // If a section requires postprocessing, this is the size of the
1919 // buffer to which relocations should be applied.
1920 off_t
1921 postprocessing_buffer_size() const
1922 { return this->current_data_size_for_child(); }
1924 // Modify the section name. This is only permitted for an
1925 // unallocated section, and only before the size has been finalized.
1926 // Otherwise the name will not get into Layout::namepool_.
1927 void
1928 set_name(const char* newname)
1930 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1931 gold_assert(!this->is_data_size_valid());
1932 this->name_ = newname;
1935 // Return whether the offset OFFSET in the input section SHNDX in
1936 // object OBJECT is being included in the link.
1937 bool
1938 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1939 off_t offset) const;
1941 // Return the offset within the output section of OFFSET relative to
1942 // the start of input section SHNDX in object OBJECT.
1943 section_offset_type
1944 output_offset(const Relobj* object, unsigned int shndx,
1945 section_offset_type offset) const;
1947 // Return the output virtual address of OFFSET relative to the start
1948 // of input section SHNDX in object OBJECT.
1949 uint64_t
1950 output_address(const Relobj* object, unsigned int shndx,
1951 off_t offset) const;
1953 // Return the output address of the start of the merged section for
1954 // input section SHNDX in object OBJECT. This is not necessarily
1955 // the offset corresponding to input offset 0 in the section, since
1956 // the section may be mapped arbitrarily.
1957 uint64_t
1958 starting_output_address(const Relobj* object, unsigned int shndx) const;
1960 // Record that this output section was found in the SECTIONS clause
1961 // of a linker script.
1962 void
1963 set_found_in_sections_clause()
1964 { this->found_in_sections_clause_ = true; }
1966 // Return whether this output section was found in the SECTIONS
1967 // clause of a linker script.
1968 bool
1969 found_in_sections_clause() const
1970 { return this->found_in_sections_clause_; }
1972 // Write the section header into *OPHDR.
1973 template<int size, bool big_endian>
1974 void
1975 write_header(const Layout*, const Stringpool*,
1976 elfcpp::Shdr_write<size, big_endian>*) const;
1978 // The next few calls are for linker script support.
1980 // Store the list of input sections for this Output_section into the
1981 // list passed in. This removes the input sections, leaving only
1982 // any Output_section_data elements. This returns the size of those
1983 // Output_section_data elements. ADDRESS is the address of this
1984 // output section. FILL is the fill value to use, in case there are
1985 // any spaces between the remaining Output_section_data elements.
1986 uint64_t
1987 get_input_sections(uint64_t address, const std::string& fill,
1988 std::list<std::pair<Relobj*, unsigned int > >*);
1990 // Add an input section from a script.
1991 void
1992 add_input_section_for_script(Relobj* object, unsigned int shndx,
1993 off_t data_size, uint64_t addralign);
1995 // Set the current size of the output section.
1996 void
1997 set_current_data_size(off_t size)
1998 { this->set_current_data_size_for_child(size); }
2000 // Get the current size of the output section.
2001 off_t
2002 current_data_size() const
2003 { return this->current_data_size_for_child(); }
2005 // End of linker script support.
2007 // Print merge statistics to stderr.
2008 void
2009 print_merge_stats();
2011 protected:
2012 // Return the output section--i.e., the object itself.
2013 Output_section*
2014 do_output_section()
2015 { return this; }
2017 // Return the section index in the output file.
2018 unsigned int
2019 do_out_shndx() const
2021 gold_assert(this->out_shndx_ != -1U);
2022 return this->out_shndx_;
2025 // Set the output section index.
2026 void
2027 do_set_out_shndx(unsigned int shndx)
2029 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2030 this->out_shndx_ = shndx;
2033 // Set the final data size of the Output_section. For a typical
2034 // Output_section, there is nothing to do, but if there are any
2035 // Output_section_data objects we need to set their final addresses
2036 // here.
2037 virtual void
2038 set_final_data_size();
2040 // Reset the address and file offset.
2041 void
2042 do_reset_address_and_file_offset();
2044 // Write the data to the file. For a typical Output_section, this
2045 // does nothing: the data is written out by calling Object::Relocate
2046 // on each input object. But if there are any Output_section_data
2047 // objects we do need to write them out here.
2048 virtual void
2049 do_write(Output_file*);
2051 // Return the address alignment--function required by parent class.
2052 uint64_t
2053 do_addralign() const
2054 { return this->addralign_; }
2056 // Return whether there is a load address.
2057 bool
2058 do_has_load_address() const
2059 { return this->has_load_address_; }
2061 // Return the load address.
2062 uint64_t
2063 do_load_address() const
2065 gold_assert(this->has_load_address_);
2066 return this->load_address_;
2069 // Return whether this is an Output_section.
2070 bool
2071 do_is_section() const
2072 { return true; }
2074 // Return whether this is a section of the specified type.
2075 bool
2076 do_is_section_type(elfcpp::Elf_Word type) const
2077 { return this->type_ == type; }
2079 // Return whether the specified section flag is set.
2080 bool
2081 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2082 { return (this->flags_ & flag) != 0; }
2084 // Set the TLS offset. Called only for SHT_TLS sections.
2085 void
2086 do_set_tls_offset(uint64_t tls_base);
2088 // Return the TLS offset, relative to the base of the TLS segment.
2089 // Valid only for SHT_TLS sections.
2090 uint64_t
2091 do_tls_offset() const
2092 { return this->tls_offset_; }
2094 // This may be implemented by a child class.
2095 virtual void
2096 do_finalize_name(Layout*)
2099 // Record that this section requires postprocessing after all
2100 // relocations have been applied. This is called by a child class.
2101 void
2102 set_requires_postprocessing()
2104 this->requires_postprocessing_ = true;
2105 this->after_input_sections_ = true;
2108 // Write all the data of an Output_section into the postprocessing
2109 // buffer.
2110 void
2111 write_to_postprocessing_buffer();
2113 private:
2114 // In some cases we need to keep a list of the input sections
2115 // associated with this output section. We only need the list if we
2116 // might have to change the offsets of the input section within the
2117 // output section after we add the input section. The ordinary
2118 // input sections will be written out when we process the object
2119 // file, and as such we don't need to track them here. We do need
2120 // to track Output_section_data objects here. We store instances of
2121 // this structure in a std::vector, so it must be a POD. There can
2122 // be many instances of this structure, so we use a union to save
2123 // some space.
2124 class Input_section
2126 public:
2127 Input_section()
2128 : shndx_(0), p2align_(0)
2130 this->u1_.data_size = 0;
2131 this->u2_.object = NULL;
2134 // For an ordinary input section.
2135 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2136 uint64_t addralign)
2137 : shndx_(shndx),
2138 p2align_(ffsll(static_cast<long long>(addralign)))
2140 gold_assert(shndx != OUTPUT_SECTION_CODE
2141 && shndx != MERGE_DATA_SECTION_CODE
2142 && shndx != MERGE_STRING_SECTION_CODE);
2143 this->u1_.data_size = data_size;
2144 this->u2_.object = object;
2147 // For a non-merge output section.
2148 Input_section(Output_section_data* posd)
2149 : shndx_(OUTPUT_SECTION_CODE),
2150 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2152 this->u1_.data_size = 0;
2153 this->u2_.posd = posd;
2156 // For a merge section.
2157 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2158 : shndx_(is_string
2159 ? MERGE_STRING_SECTION_CODE
2160 : MERGE_DATA_SECTION_CODE),
2161 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2163 this->u1_.entsize = entsize;
2164 this->u2_.posd = posd;
2167 // The required alignment.
2168 uint64_t
2169 addralign() const
2171 return (this->p2align_ == 0
2173 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2176 // Return the required size.
2177 off_t
2178 data_size() const;
2180 // Whether this is an input section.
2181 bool
2182 is_input_section() const
2184 return (this->shndx_ != OUTPUT_SECTION_CODE
2185 && this->shndx_ != MERGE_DATA_SECTION_CODE
2186 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2189 // Return whether this is a merge section which matches the
2190 // parameters.
2191 bool
2192 is_merge_section(bool is_string, uint64_t entsize,
2193 uint64_t addralign) const
2195 return (this->shndx_ == (is_string
2196 ? MERGE_STRING_SECTION_CODE
2197 : MERGE_DATA_SECTION_CODE)
2198 && this->u1_.entsize == entsize
2199 && this->addralign() == addralign);
2202 // Return the object for an input section.
2203 Relobj*
2204 relobj() const
2206 gold_assert(this->is_input_section());
2207 return this->u2_.object;
2210 // Return the input section index for an input section.
2211 unsigned int
2212 shndx() const
2214 gold_assert(this->is_input_section());
2215 return this->shndx_;
2218 // Set the output section.
2219 void
2220 set_output_section(Output_section* os)
2222 gold_assert(!this->is_input_section());
2223 this->u2_.posd->set_output_section(os);
2226 // Set the address and file offset. This is called during
2227 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2228 // the enclosing section.
2229 void
2230 set_address_and_file_offset(uint64_t address, off_t file_offset,
2231 off_t section_file_offset);
2233 // Reset the address and file offset.
2234 void
2235 reset_address_and_file_offset();
2237 // Finalize the data size.
2238 void
2239 finalize_data_size();
2241 // Add an input section, for SHF_MERGE sections.
2242 bool
2243 add_input_section(Relobj* object, unsigned int shndx)
2245 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2246 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2247 return this->u2_.posd->add_input_section(object, shndx);
2250 // Given an input OBJECT, an input section index SHNDX within that
2251 // object, and an OFFSET relative to the start of that input
2252 // section, return whether or not the output offset is known. If
2253 // this function returns true, it sets *POUTPUT to the offset in
2254 // the output section, relative to the start of the input section
2255 // in the output section. *POUTPUT may be different from OFFSET
2256 // for a merged section.
2257 bool
2258 output_offset(const Relobj* object, unsigned int shndx,
2259 section_offset_type offset,
2260 section_offset_type *poutput) const;
2262 // Return whether this is the merge section for the input section
2263 // SHNDX in OBJECT.
2264 bool
2265 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2267 // Write out the data. This does nothing for an input section.
2268 void
2269 write(Output_file*);
2271 // Write the data to a buffer. This does nothing for an input
2272 // section.
2273 void
2274 write_to_buffer(unsigned char*);
2276 // Print statistics about merge sections to stderr.
2277 void
2278 print_merge_stats(const char* section_name)
2280 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2281 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2282 this->u2_.posd->print_merge_stats(section_name);
2285 private:
2286 // Code values which appear in shndx_. If the value is not one of
2287 // these codes, it is the input section index in the object file.
2288 enum
2290 // An Output_section_data.
2291 OUTPUT_SECTION_CODE = -1U,
2292 // An Output_section_data for an SHF_MERGE section with
2293 // SHF_STRINGS not set.
2294 MERGE_DATA_SECTION_CODE = -2U,
2295 // An Output_section_data for an SHF_MERGE section with
2296 // SHF_STRINGS set.
2297 MERGE_STRING_SECTION_CODE = -3U
2300 // For an ordinary input section, this is the section index in the
2301 // input file. For an Output_section_data, this is
2302 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2303 // MERGE_STRING_SECTION_CODE.
2304 unsigned int shndx_;
2305 // The required alignment, stored as a power of 2.
2306 unsigned int p2align_;
2307 union
2309 // For an ordinary input section, the section size.
2310 off_t data_size;
2311 // For OUTPUT_SECTION_CODE, this is not used. For
2312 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2313 // entity size.
2314 uint64_t entsize;
2315 } u1_;
2316 union
2318 // For an ordinary input section, the object which holds the
2319 // input section.
2320 Relobj* object;
2321 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2322 // MERGE_STRING_SECTION_CODE, the data.
2323 Output_section_data* posd;
2324 } u2_;
2327 typedef std::vector<Input_section> Input_section_list;
2329 // Fill data. This is used to fill in data between input sections.
2330 // It is also used for data statements (BYTE, WORD, etc.) in linker
2331 // scripts. When we have to keep track of the input sections, we
2332 // can use an Output_data_const, but we don't want to have to keep
2333 // track of input sections just to implement fills.
2334 class Fill
2336 public:
2337 Fill(off_t section_offset, off_t length)
2338 : section_offset_(section_offset),
2339 length_(convert_to_section_size_type(length))
2342 // Return section offset.
2343 off_t
2344 section_offset() const
2345 { return this->section_offset_; }
2347 // Return fill length.
2348 section_size_type
2349 length() const
2350 { return this->length_; }
2352 private:
2353 // The offset within the output section.
2354 off_t section_offset_;
2355 // The length of the space to fill.
2356 section_size_type length_;
2359 typedef std::vector<Fill> Fill_list;
2361 // Add a new output section by Input_section.
2362 void
2363 add_output_section_data(Input_section*);
2365 // Add an SHF_MERGE input section. Returns true if the section was
2366 // handled.
2367 bool
2368 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2369 uint64_t entsize, uint64_t addralign);
2371 // Add an output SHF_MERGE section POSD to this output section.
2372 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2373 // ENTSIZE is the entity size. This returns the entry added to
2374 // input_sections_.
2375 void
2376 add_output_merge_section(Output_section_data* posd, bool is_string,
2377 uint64_t entsize);
2379 // Most of these fields are only valid after layout.
2381 // The name of the section. This will point into a Stringpool.
2382 const char* name_;
2383 // The section address is in the parent class.
2384 // The section alignment.
2385 uint64_t addralign_;
2386 // The section entry size.
2387 uint64_t entsize_;
2388 // The load address. This is only used when using a linker script
2389 // with a SECTIONS clause. The has_load_address_ field indicates
2390 // whether this field is valid.
2391 uint64_t load_address_;
2392 // The file offset is in the parent class.
2393 // Set the section link field to the index of this section.
2394 const Output_data* link_section_;
2395 // If link_section_ is NULL, this is the link field.
2396 unsigned int link_;
2397 // Set the section info field to the index of this section.
2398 const Output_section* info_section_;
2399 // If info_section_ is NULL, set the info field to the symbol table
2400 // index of this symbol.
2401 const Symbol* info_symndx_;
2402 // If info_section_ and info_symndx_ are NULL, this is the section
2403 // info field.
2404 unsigned int info_;
2405 // The section type.
2406 const elfcpp::Elf_Word type_;
2407 // The section flags.
2408 elfcpp::Elf_Xword flags_;
2409 // The section index.
2410 unsigned int out_shndx_;
2411 // If there is a STT_SECTION for this output section in the normal
2412 // symbol table, this is the symbol index. This starts out as zero.
2413 // It is initialized in Layout::finalize() to be the index, or -1U
2414 // if there isn't one.
2415 unsigned int symtab_index_;
2416 // If there is a STT_SECTION for this output section in the dynamic
2417 // symbol table, this is the symbol index. This starts out as zero.
2418 // It is initialized in Layout::finalize() to be the index, or -1U
2419 // if there isn't one.
2420 unsigned int dynsym_index_;
2421 // The input sections. This will be empty in cases where we don't
2422 // need to keep track of them.
2423 Input_section_list input_sections_;
2424 // The offset of the first entry in input_sections_.
2425 off_t first_input_offset_;
2426 // The fill data. This is separate from input_sections_ because we
2427 // often will need fill sections without needing to keep track of
2428 // input sections.
2429 Fill_list fills_;
2430 // If the section requires postprocessing, this buffer holds the
2431 // section contents during relocation.
2432 unsigned char* postprocessing_buffer_;
2433 // Whether this output section needs a STT_SECTION symbol in the
2434 // normal symbol table. This will be true if there is a relocation
2435 // which needs it.
2436 bool needs_symtab_index_ : 1;
2437 // Whether this output section needs a STT_SECTION symbol in the
2438 // dynamic symbol table. This will be true if there is a dynamic
2439 // relocation which needs it.
2440 bool needs_dynsym_index_ : 1;
2441 // Whether the link field of this output section should point to the
2442 // normal symbol table.
2443 bool should_link_to_symtab_ : 1;
2444 // Whether the link field of this output section should point to the
2445 // dynamic symbol table.
2446 bool should_link_to_dynsym_ : 1;
2447 // Whether this section should be written after all the input
2448 // sections are complete.
2449 bool after_input_sections_ : 1;
2450 // Whether this section requires post processing after all
2451 // relocations have been applied.
2452 bool requires_postprocessing_ : 1;
2453 // Whether an input section was mapped to this output section
2454 // because of a SECTIONS clause in a linker script.
2455 bool found_in_sections_clause_ : 1;
2456 // Whether this section has an explicitly specified load address.
2457 bool has_load_address_ : 1;
2458 // True if the info_section_ field means the section index of the
2459 // section, false if it means the symbol index of the corresponding
2460 // section symbol.
2461 bool info_uses_section_index_ : 1;
2462 // For SHT_TLS sections, the offset of this section relative to the base
2463 // of the TLS segment.
2464 uint64_t tls_offset_;
2467 // An output segment. PT_LOAD segments are built from collections of
2468 // output sections. Other segments typically point within PT_LOAD
2469 // segments, and are built directly as needed.
2471 class Output_segment
2473 public:
2474 // Create an output segment, specifying the type and flags.
2475 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2477 // Return the virtual address.
2478 uint64_t
2479 vaddr() const
2480 { return this->vaddr_; }
2482 // Return the physical address.
2483 uint64_t
2484 paddr() const
2485 { return this->paddr_; }
2487 // Return the segment type.
2488 elfcpp::Elf_Word
2489 type() const
2490 { return this->type_; }
2492 // Return the segment flags.
2493 elfcpp::Elf_Word
2494 flags() const
2495 { return this->flags_; }
2497 // Return the memory size.
2498 uint64_t
2499 memsz() const
2500 { return this->memsz_; }
2502 // Return the file size.
2503 off_t
2504 filesz() const
2505 { return this->filesz_; }
2507 // Return the file offset.
2508 off_t
2509 offset() const
2510 { return this->offset_; }
2512 // Return the maximum alignment of the Output_data.
2513 uint64_t
2514 maximum_alignment();
2516 // Add an Output_section to this segment.
2517 void
2518 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2519 { this->add_output_section(os, seg_flags, false); }
2521 // Add an Output_section to the start of this segment.
2522 void
2523 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2524 { this->add_output_section(os, seg_flags, true); }
2526 // Add an Output_data (which is not an Output_section) to the start
2527 // of this segment.
2528 void
2529 add_initial_output_data(Output_data*);
2531 // Return true if this segment has any sections which hold actual
2532 // data, rather than being a BSS section.
2533 bool
2534 has_any_data_sections() const
2535 { return !this->output_data_.empty(); }
2537 // Return the number of dynamic relocations applied to this segment.
2538 unsigned int
2539 dynamic_reloc_count() const;
2541 // Return the address of the first section.
2542 uint64_t
2543 first_section_load_address() const;
2545 // Return whether the addresses have been set already.
2546 bool
2547 are_addresses_set() const
2548 { return this->are_addresses_set_; }
2550 // Set the addresses.
2551 void
2552 set_addresses(uint64_t vaddr, uint64_t paddr)
2554 this->vaddr_ = vaddr;
2555 this->paddr_ = paddr;
2556 this->are_addresses_set_ = true;
2559 // Set the segment flags. This is only used if we have a PHDRS
2560 // clause which explicitly specifies the flags.
2561 void
2562 set_flags(elfcpp::Elf_Word flags)
2563 { this->flags_ = flags; }
2565 // Set the address of the segment to ADDR and the offset to *POFF
2566 // and set the addresses and offsets of all contained output
2567 // sections accordingly. Set the section indexes of all contained
2568 // output sections starting with *PSHNDX. If RESET is true, first
2569 // reset the addresses of the contained sections. Return the
2570 // address of the immediately following segment. Update *POFF and
2571 // *PSHNDX. This should only be called for a PT_LOAD segment.
2572 uint64_t
2573 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2574 unsigned int* pshndx);
2576 // Set the minimum alignment of this segment. This may be adjusted
2577 // upward based on the section alignments.
2578 void
2579 set_minimum_p_align(uint64_t align)
2580 { this->min_p_align_ = align; }
2582 // Set the offset of this segment based on the section. This should
2583 // only be called for a non-PT_LOAD segment.
2584 void
2585 set_offset();
2587 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2588 void
2589 set_tls_offsets();
2591 // Return the number of output sections.
2592 unsigned int
2593 output_section_count() const;
2595 // Return the section attached to the list segment with the lowest
2596 // load address. This is used when handling a PHDRS clause in a
2597 // linker script.
2598 Output_section*
2599 section_with_lowest_load_address() const;
2601 // Write the segment header into *OPHDR.
2602 template<int size, bool big_endian>
2603 void
2604 write_header(elfcpp::Phdr_write<size, big_endian>*);
2606 // Write the section headers of associated sections into V.
2607 template<int size, bool big_endian>
2608 unsigned char*
2609 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2610 unsigned int* pshndx) const;
2612 private:
2613 Output_segment(const Output_segment&);
2614 Output_segment& operator=(const Output_segment&);
2616 typedef std::list<Output_data*> Output_data_list;
2618 // Add an Output_section to this segment, specifying front or back.
2619 void
2620 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2621 bool front);
2623 // Find the maximum alignment in an Output_data_list.
2624 static uint64_t
2625 maximum_alignment_list(const Output_data_list*);
2627 // Set the section addresses in an Output_data_list.
2628 uint64_t
2629 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2630 off_t* poff, unsigned int* pshndx);
2632 // Return the number of Output_sections in an Output_data_list.
2633 unsigned int
2634 output_section_count_list(const Output_data_list*) const;
2636 // Return the number of dynamic relocs in an Output_data_list.
2637 unsigned int
2638 dynamic_reloc_count_list(const Output_data_list*) const;
2640 // Find the section with the lowest load address in an
2641 // Output_data_list.
2642 void
2643 lowest_load_address_in_list(const Output_data_list* pdl,
2644 Output_section** found,
2645 uint64_t* found_lma) const;
2647 // Write the section headers in the list into V.
2648 template<int size, bool big_endian>
2649 unsigned char*
2650 write_section_headers_list(const Layout*, const Stringpool*,
2651 const Output_data_list*, unsigned char* v,
2652 unsigned int* pshdx) const;
2654 // The list of output data with contents attached to this segment.
2655 Output_data_list output_data_;
2656 // The list of output data without contents attached to this segment.
2657 Output_data_list output_bss_;
2658 // The segment virtual address.
2659 uint64_t vaddr_;
2660 // The segment physical address.
2661 uint64_t paddr_;
2662 // The size of the segment in memory.
2663 uint64_t memsz_;
2664 // The maximum section alignment. The is_max_align_known_ field
2665 // indicates whether this has been finalized.
2666 uint64_t max_align_;
2667 // The required minimum value for the p_align field. This is used
2668 // for PT_LOAD segments. Note that this does not mean that
2669 // addresses should be aligned to this value; it means the p_paddr
2670 // and p_vaddr fields must be congruent modulo this value. For
2671 // non-PT_LOAD segments, the dynamic linker works more efficiently
2672 // if the p_align field has the more conventional value, although it
2673 // can align as needed.
2674 uint64_t min_p_align_;
2675 // The offset of the segment data within the file.
2676 off_t offset_;
2677 // The size of the segment data in the file.
2678 off_t filesz_;
2679 // The segment type;
2680 elfcpp::Elf_Word type_;
2681 // The segment flags.
2682 elfcpp::Elf_Word flags_;
2683 // Whether we have finalized max_align_.
2684 bool is_max_align_known_ : 1;
2685 // Whether vaddr and paddr were set by a linker script.
2686 bool are_addresses_set_ : 1;
2689 // This class represents the output file.
2691 class Output_file
2693 public:
2694 Output_file(const char* name);
2696 // Indicate that this is a temporary file which should not be
2697 // output.
2698 void
2699 set_is_temporary()
2700 { this->is_temporary_ = true; }
2702 // Open the output file. FILE_SIZE is the final size of the file.
2703 void
2704 open(off_t file_size);
2706 // Resize the output file.
2707 void
2708 resize(off_t file_size);
2710 // Close the output file (flushing all buffered data) and make sure
2711 // there are no errors.
2712 void
2713 close();
2715 // We currently always use mmap which makes the view handling quite
2716 // simple. In the future we may support other approaches.
2718 // Write data to the output file.
2719 void
2720 write(off_t offset, const void* data, size_t len)
2721 { memcpy(this->base_ + offset, data, len); }
2723 // Get a buffer to use to write to the file, given the offset into
2724 // the file and the size.
2725 unsigned char*
2726 get_output_view(off_t start, size_t size)
2728 gold_assert(start >= 0
2729 && start + static_cast<off_t>(size) <= this->file_size_);
2730 return this->base_ + start;
2733 // VIEW must have been returned by get_output_view. Write the
2734 // buffer to the file, passing in the offset and the size.
2735 void
2736 write_output_view(off_t, size_t, unsigned char*)
2739 // Get a read/write buffer. This is used when we want to write part
2740 // of the file, read it in, and write it again.
2741 unsigned char*
2742 get_input_output_view(off_t start, size_t size)
2743 { return this->get_output_view(start, size); }
2745 // Write a read/write buffer back to the file.
2746 void
2747 write_input_output_view(off_t, size_t, unsigned char*)
2750 // Get a read buffer. This is used when we just want to read part
2751 // of the file back it in.
2752 const unsigned char*
2753 get_input_view(off_t start, size_t size)
2754 { return this->get_output_view(start, size); }
2756 // Release a read bfufer.
2757 void
2758 free_input_view(off_t, size_t, const unsigned char*)
2761 private:
2762 // Map the file into memory and return a pointer to the map.
2763 void
2764 map();
2766 // Unmap the file from memory (and flush to disk buffers).
2767 void
2768 unmap();
2770 // File name.
2771 const char* name_;
2772 // File descriptor.
2773 int o_;
2774 // File size.
2775 off_t file_size_;
2776 // Base of file mapped into memory.
2777 unsigned char* base_;
2778 // True iff base_ points to a memory buffer rather than an output file.
2779 bool map_is_anonymous_;
2780 // True if this is a temporary file which should not be output.
2781 bool is_temporary_;
2784 } // End namespace gold.
2786 #endif // !defined(GOLD_OUTPUT_H)