Fix formatting of most recent entry.
[binutils.git] / gold / output.h
blobfbfdb256264c5cbf0858b6633c99405fa93eb6e4
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, an
771 // output section, or the undefined symbol at index 0. We represent
772 // 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 // A reloc against a global symbol.
791 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
792 Address address, bool is_relative);
794 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
795 unsigned int shndx, Address address, bool is_relative);
797 // A reloc against a local symbol.
799 Output_reloc(Sized_relobj<size, big_endian>* relobj,
800 unsigned int local_sym_index, unsigned int type,
801 Output_data* od, Address address, bool is_relative);
803 Output_reloc(Sized_relobj<size, big_endian>* relobj,
804 unsigned int local_sym_index, unsigned int type,
805 unsigned int shndx, Address address, bool is_relative);
807 // A reloc against the STT_SECTION symbol of an output section.
809 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
810 Address address);
812 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
813 unsigned int shndx, Address address);
815 // Return TRUE if this is a RELATIVE relocation.
816 bool
817 is_relative() const
818 { return this->is_relative_; }
820 // Get the value of the symbol referred to by a Rel relocation.
822 Address
823 symbol_value() const;
825 // Write the reloc entry to an output view.
826 void
827 write(unsigned char* pov) const;
829 // Write the offset and info fields to Write_rel.
830 template<typename Write_rel>
831 void write_rel(Write_rel*) const;
833 private:
834 // Return the symbol index. We can't do a double template
835 // specialization, so we do a secondary template here.
836 unsigned int
837 get_symbol_index() const;
839 // Codes for local_sym_index_.
840 enum
842 // Global symbol.
843 GSYM_CODE = -1U,
844 // Output section.
845 SECTION_CODE = -2U,
846 // Invalid uninitialized entry.
847 INVALID_CODE = -3U
850 union
852 // For a local symbol, the object. We will never generate a
853 // relocation against a local symbol in a dynamic object; that
854 // doesn't make sense. And our callers will always be
855 // templatized, so we use Sized_relobj here.
856 Sized_relobj<size, big_endian>* relobj;
857 // For a global symbol, the symbol. If this is NULL, it indicates
858 // a relocation against the undefined 0 symbol.
859 Symbol* gsym;
860 // For a relocation against an output section, the output section.
861 Output_section* os;
862 } u1_;
863 union
865 // If shndx_ is not INVALID CODE, the object which holds the input
866 // section being used to specify the reloc address.
867 Relobj* relobj;
868 // If shndx_ is INVALID_CODE, the output data being used to
869 // specify the reloc address. This may be NULL if the reloc
870 // address is absolute.
871 Output_data* od;
872 } u2_;
873 // The address offset within the input section or the Output_data.
874 Address address_;
875 // For a local symbol, the local symbol index. This is GSYM_CODE
876 // for a global symbol, or INVALID_CODE for an uninitialized value.
877 unsigned int local_sym_index_;
878 // The reloc type--a processor specific code.
879 unsigned int type_ : 31;
880 // True if the relocation is a RELATIVE relocation.
881 bool is_relative_ : 1;
882 // If the reloc address is an input section in an object, the
883 // section index. This is INVALID_CODE if the reloc address is
884 // specified in some other way.
885 unsigned int shndx_;
888 // The SHT_RELA version of Output_reloc<>. This is just derived from
889 // the SHT_REL version of Output_reloc, but it adds an addend.
891 template<bool dynamic, int size, bool big_endian>
892 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
894 public:
895 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
896 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
898 // An uninitialized entry.
899 Output_reloc()
900 : rel_()
903 // A reloc against a global symbol.
905 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
906 Address address, Addend addend, bool is_relative)
907 : rel_(gsym, type, od, address, is_relative), addend_(addend)
910 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
911 unsigned int shndx, Address address, Addend addend,
912 bool is_relative)
913 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
916 // A reloc against a local symbol.
918 Output_reloc(Sized_relobj<size, big_endian>* relobj,
919 unsigned int local_sym_index, unsigned int type,
920 Output_data* od, Address address,
921 Addend addend, bool is_relative)
922 : rel_(relobj, local_sym_index, type, od, address, is_relative),
923 addend_(addend)
926 Output_reloc(Sized_relobj<size, big_endian>* relobj,
927 unsigned int local_sym_index, unsigned int type,
928 unsigned int shndx, Address address,
929 Addend addend, bool is_relative)
930 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
931 addend_(addend)
934 // A reloc against the STT_SECTION symbol of an output section.
936 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
937 Address address, Addend addend)
938 : rel_(os, type, od, address), addend_(addend)
941 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
942 unsigned int shndx, Address address, Addend addend)
943 : rel_(os, type, relobj, shndx, address), addend_(addend)
946 // Write the reloc entry to an output view.
947 void
948 write(unsigned char* pov) const;
950 private:
951 // The basic reloc.
952 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
953 // The addend.
954 Addend addend_;
957 // Output_data_reloc is used to manage a section containing relocs.
958 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
959 // indicates whether this is a dynamic relocation or a normal
960 // relocation. Output_data_reloc_base is a base class.
961 // Output_data_reloc is the real class, which we specialize based on
962 // the reloc type.
964 template<int sh_type, bool dynamic, int size, bool big_endian>
965 class Output_data_reloc_base : public Output_section_data_build
967 public:
968 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
969 typedef typename Output_reloc_type::Address Address;
970 static const int reloc_size =
971 Reloc_types<sh_type, size, big_endian>::reloc_size;
973 // Construct the section.
974 Output_data_reloc_base()
975 : Output_section_data_build(Output_data::default_alignment_for_size(size))
978 protected:
979 // Write out the data.
980 void
981 do_write(Output_file*);
983 // Set the entry size and the link.
984 void
985 do_adjust_output_section(Output_section *os);
987 // Add a relocation entry.
988 void
989 add(Output_data *od, const Output_reloc_type& reloc)
991 this->relocs_.push_back(reloc);
992 this->set_current_data_size(this->relocs_.size() * reloc_size);
993 od->add_dynamic_reloc();
996 private:
997 typedef std::vector<Output_reloc_type> Relocs;
999 Relocs relocs_;
1002 // The class which callers actually create.
1004 template<int sh_type, bool dynamic, int size, bool big_endian>
1005 class Output_data_reloc;
1007 // The SHT_REL version of Output_data_reloc.
1009 template<bool dynamic, int size, bool big_endian>
1010 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1011 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1013 private:
1014 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1015 big_endian> Base;
1017 public:
1018 typedef typename Base::Output_reloc_type Output_reloc_type;
1019 typedef typename Output_reloc_type::Address Address;
1021 Output_data_reloc()
1022 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1025 // Add a reloc against a global symbol.
1027 void
1028 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1029 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1031 void
1032 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1033 unsigned int shndx, Address address)
1034 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1035 false)); }
1037 // Add a RELATIVE reloc against a global symbol. The final relocation
1038 // will not reference the symbol.
1040 void
1041 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1042 Address address)
1043 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1045 void
1046 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1047 Relobj* relobj, unsigned int shndx, Address address)
1048 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1049 true)); }
1051 // Add a reloc against a local symbol.
1053 void
1054 add_local(Sized_relobj<size, big_endian>* relobj,
1055 unsigned int local_sym_index, unsigned int type,
1056 Output_data* od, Address address)
1057 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1058 address, false)); }
1060 void
1061 add_local(Sized_relobj<size, big_endian>* relobj,
1062 unsigned int local_sym_index, unsigned int type,
1063 Output_data* od, unsigned int shndx, Address address)
1064 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1065 address, false)); }
1067 // Add a RELATIVE reloc against a local symbol.
1069 void
1070 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1071 unsigned int local_sym_index, unsigned int type,
1072 Output_data* od, Address address)
1073 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1074 address, true)); }
1076 void
1077 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1078 unsigned int local_sym_index, unsigned int type,
1079 Output_data* od, unsigned int shndx, Address address)
1080 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1081 address, true)); }
1083 // A reloc against the STT_SECTION symbol of an output section.
1084 // OS is the Output_section that the relocation refers to; OD is
1085 // the Output_data object being relocated.
1087 void
1088 add_output_section(Output_section* os, unsigned int type,
1089 Output_data* od, Address address)
1090 { this->add(od, Output_reloc_type(os, type, od, address)); }
1092 void
1093 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1094 Relobj* relobj, unsigned int shndx, Address address)
1095 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1098 // The SHT_RELA version of Output_data_reloc.
1100 template<bool dynamic, int size, bool big_endian>
1101 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1102 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1104 private:
1105 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1106 big_endian> Base;
1108 public:
1109 typedef typename Base::Output_reloc_type Output_reloc_type;
1110 typedef typename Output_reloc_type::Address Address;
1111 typedef typename Output_reloc_type::Addend Addend;
1113 Output_data_reloc()
1114 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1117 // Add a reloc against a global symbol.
1119 void
1120 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1121 Address address, Addend addend)
1122 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1123 false)); }
1125 void
1126 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1127 unsigned int shndx, Address address,
1128 Addend addend)
1129 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1130 addend, false)); }
1132 // Add a RELATIVE reloc against a global symbol. The final output
1133 // relocation will not reference the symbol, but we must keep the symbol
1134 // information long enough to set the addend of the relocation correctly
1135 // when it is written.
1137 void
1138 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1139 Address address, Addend addend)
1140 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1142 void
1143 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1144 Relobj* relobj, unsigned int shndx, Address address,
1145 Addend addend)
1146 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1147 addend, true)); }
1149 // Add a reloc against a local symbol.
1151 void
1152 add_local(Sized_relobj<size, big_endian>* relobj,
1153 unsigned int local_sym_index, unsigned int type,
1154 Output_data* od, Address address, Addend addend)
1156 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1157 addend, false));
1160 void
1161 add_local(Sized_relobj<size, big_endian>* relobj,
1162 unsigned int local_sym_index, unsigned int type,
1163 Output_data* od, unsigned int shndx, Address address,
1164 Addend addend)
1166 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1167 address, addend, false));
1170 // Add a RELATIVE reloc against a local symbol.
1172 void
1173 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1174 unsigned int local_sym_index, unsigned int type,
1175 Output_data* od, Address address, Addend addend)
1177 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1178 addend, true));
1181 void
1182 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1183 unsigned int local_sym_index, unsigned int type,
1184 Output_data* od, unsigned int shndx, Address address,
1185 Addend addend)
1187 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1188 address, addend, true));
1191 // A reloc against the STT_SECTION symbol of an output section.
1193 void
1194 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1195 Address address, Addend addend)
1196 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1198 void
1199 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1200 unsigned int shndx, Address address, Addend addend)
1201 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1202 addend)); }
1205 // Output_relocatable_relocs represents a relocation section in a
1206 // relocatable link. The actual data is written out in the target
1207 // hook relocate_for_relocatable. This just saves space for it.
1209 template<int sh_type, int size, bool big_endian>
1210 class Output_relocatable_relocs : public Output_section_data
1212 public:
1213 Output_relocatable_relocs(Relocatable_relocs* rr)
1214 : Output_section_data(Output_data::default_alignment_for_size(size)),
1215 rr_(rr)
1218 void
1219 set_final_data_size();
1221 // Write out the data. There is nothing to do here.
1222 void
1223 do_write(Output_file*)
1226 private:
1227 // The relocs associated with this input section.
1228 Relocatable_relocs* rr_;
1231 // Handle a GROUP section.
1233 template<int size, bool big_endian>
1234 class Output_data_group : public Output_section_data
1236 public:
1237 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1238 section_size_type entry_count,
1239 const elfcpp::Elf_Word* contents);
1241 void
1242 do_write(Output_file*);
1244 private:
1245 // The input object.
1246 Sized_relobj<size, big_endian>* relobj_;
1247 // The group flag word.
1248 elfcpp::Elf_Word flags_;
1249 // The section indexes of the input sections in this group.
1250 std::vector<unsigned int> input_sections_;
1253 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1254 // for one symbol--either a global symbol or a local symbol in an
1255 // object. The target specific code adds entries to the GOT as
1256 // needed.
1258 template<int size, bool big_endian>
1259 class Output_data_got : public Output_section_data_build
1261 public:
1262 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1263 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1264 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1266 Output_data_got()
1267 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1268 entries_()
1271 // Add an entry for a global symbol to the GOT. Return true if this
1272 // is a new GOT entry, false if the symbol was already in the GOT.
1273 bool
1274 add_global(Symbol* gsym);
1276 // Add an entry for a global symbol to the GOT, and add a dynamic
1277 // relocation of type R_TYPE for the GOT entry.
1278 void
1279 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1281 void
1282 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1284 // Add an entry for a local symbol to the GOT. This returns true if
1285 // this is a new GOT entry, false if the symbol already has a GOT
1286 // entry.
1287 bool
1288 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1290 // Add an entry for a global symbol to the GOT, and add a dynamic
1291 // relocation of type R_TYPE for the GOT entry.
1292 void
1293 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1294 unsigned int sym_index, Rel_dyn* rel_dyn,
1295 unsigned int r_type);
1297 void
1298 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1299 unsigned int sym_index, Rela_dyn* rela_dyn,
1300 unsigned int r_type);
1302 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1303 // Return true if this is a new GOT entry, false if the symbol was
1304 // already in the GOT.
1305 bool
1306 add_global_tls(Symbol* gsym, bool need_pair);
1308 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1309 // relocation of type R_TYPE.
1310 void
1311 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1312 unsigned int r_type);
1314 void
1315 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1316 unsigned int r_type);
1318 // Add a pair of entries for a global TLS symbol to the GOT, and add
1319 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1320 void
1321 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1322 unsigned int mod_r_type,
1323 unsigned int dtv_r_type);
1325 void
1326 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1327 unsigned int mod_r_type,
1328 unsigned int dtv_r_type);
1330 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1331 // This returns true if this is a new GOT entry, false if the symbol
1332 // already has a GOT entry.
1333 bool
1334 add_local_tls(Sized_relobj<size, big_endian>* object,
1335 unsigned int sym_index, bool need_pair);
1337 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1338 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1339 // Because this is a local symbol, the first GOT entry can be relocated
1340 // relative to a section symbol, and the second GOT entry will have an
1341 // dtv-relative value that can be computed at link time.
1342 void
1343 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1344 unsigned int sym_index, unsigned int shndx,
1345 bool need_pair, Rel_dyn* rel_dyn,
1346 unsigned int r_type);
1348 void
1349 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1350 unsigned int sym_index, unsigned int shndx,
1351 bool need_pair, Rela_dyn* rela_dyn,
1352 unsigned int r_type);
1354 // Add a constant to the GOT. This returns the offset of the new
1355 // entry from the start of the GOT.
1356 unsigned int
1357 add_constant(Valtype constant)
1359 this->entries_.push_back(Got_entry(constant));
1360 this->set_got_size();
1361 return this->last_got_offset();
1364 protected:
1365 // Write out the GOT table.
1366 void
1367 do_write(Output_file*);
1369 private:
1370 // This POD class holds a single GOT entry.
1371 class Got_entry
1373 public:
1374 // Create a zero entry.
1375 Got_entry()
1376 : local_sym_index_(CONSTANT_CODE)
1377 { this->u_.constant = 0; }
1379 // Create a global symbol entry.
1380 explicit Got_entry(Symbol* gsym)
1381 : local_sym_index_(GSYM_CODE)
1382 { this->u_.gsym = gsym; }
1384 // Create a local symbol entry.
1385 Got_entry(Sized_relobj<size, big_endian>* object,
1386 unsigned int local_sym_index)
1387 : local_sym_index_(local_sym_index)
1389 gold_assert(local_sym_index != GSYM_CODE
1390 && local_sym_index != CONSTANT_CODE);
1391 this->u_.object = object;
1394 // Create a constant entry. The constant is a host value--it will
1395 // be swapped, if necessary, when it is written out.
1396 explicit Got_entry(Valtype constant)
1397 : local_sym_index_(CONSTANT_CODE)
1398 { this->u_.constant = constant; }
1400 // Write the GOT entry to an output view.
1401 void
1402 write(unsigned char* pov) const;
1404 private:
1405 enum
1407 GSYM_CODE = -1U,
1408 CONSTANT_CODE = -2U
1411 union
1413 // For a local symbol, the object.
1414 Sized_relobj<size, big_endian>* object;
1415 // For a global symbol, the symbol.
1416 Symbol* gsym;
1417 // For a constant, the constant.
1418 Valtype constant;
1419 } u_;
1420 // For a local symbol, the local symbol index. This is GSYM_CODE
1421 // for a global symbol, or CONSTANT_CODE for a constant.
1422 unsigned int local_sym_index_;
1425 typedef std::vector<Got_entry> Got_entries;
1427 // Return the offset into the GOT of GOT entry I.
1428 unsigned int
1429 got_offset(unsigned int i) const
1430 { return i * (size / 8); }
1432 // Return the offset into the GOT of the last entry added.
1433 unsigned int
1434 last_got_offset() const
1435 { return this->got_offset(this->entries_.size() - 1); }
1437 // Set the size of the section.
1438 void
1439 set_got_size()
1440 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1442 // The list of GOT entries.
1443 Got_entries entries_;
1446 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1447 // section.
1449 class Output_data_dynamic : public Output_section_data
1451 public:
1452 Output_data_dynamic(Stringpool* pool)
1453 : Output_section_data(Output_data::default_alignment()),
1454 entries_(), pool_(pool)
1457 // Add a new dynamic entry with a fixed numeric value.
1458 void
1459 add_constant(elfcpp::DT tag, unsigned int val)
1460 { this->add_entry(Dynamic_entry(tag, val)); }
1462 // Add a new dynamic entry with the address of output data.
1463 void
1464 add_section_address(elfcpp::DT tag, const Output_data* od)
1465 { this->add_entry(Dynamic_entry(tag, od, false)); }
1467 // Add a new dynamic entry with the size of output data.
1468 void
1469 add_section_size(elfcpp::DT tag, const Output_data* od)
1470 { this->add_entry(Dynamic_entry(tag, od, true)); }
1472 // Add a new dynamic entry with the address of a symbol.
1473 void
1474 add_symbol(elfcpp::DT tag, const Symbol* sym)
1475 { this->add_entry(Dynamic_entry(tag, sym)); }
1477 // Add a new dynamic entry with a string.
1478 void
1479 add_string(elfcpp::DT tag, const char* str)
1480 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1482 void
1483 add_string(elfcpp::DT tag, const std::string& str)
1484 { this->add_string(tag, str.c_str()); }
1486 protected:
1487 // Adjust the output section to set the entry size.
1488 void
1489 do_adjust_output_section(Output_section*);
1491 // Set the final data size.
1492 void
1493 set_final_data_size();
1495 // Write out the dynamic entries.
1496 void
1497 do_write(Output_file*);
1499 private:
1500 // This POD class holds a single dynamic entry.
1501 class Dynamic_entry
1503 public:
1504 // Create an entry with a fixed numeric value.
1505 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1506 : tag_(tag), classification_(DYNAMIC_NUMBER)
1507 { this->u_.val = val; }
1509 // Create an entry with the size or address of a section.
1510 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1511 : tag_(tag),
1512 classification_(section_size
1513 ? DYNAMIC_SECTION_SIZE
1514 : DYNAMIC_SECTION_ADDRESS)
1515 { this->u_.od = od; }
1517 // Create an entry with the address of a symbol.
1518 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1519 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1520 { this->u_.sym = sym; }
1522 // Create an entry with a string.
1523 Dynamic_entry(elfcpp::DT tag, const char* str)
1524 : tag_(tag), classification_(DYNAMIC_STRING)
1525 { this->u_.str = str; }
1527 // Write the dynamic entry to an output view.
1528 template<int size, bool big_endian>
1529 void
1530 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1532 private:
1533 enum Classification
1535 // Number.
1536 DYNAMIC_NUMBER,
1537 // Section address.
1538 DYNAMIC_SECTION_ADDRESS,
1539 // Section size.
1540 DYNAMIC_SECTION_SIZE,
1541 // Symbol adress.
1542 DYNAMIC_SYMBOL,
1543 // String.
1544 DYNAMIC_STRING
1547 union
1549 // For DYNAMIC_NUMBER.
1550 unsigned int val;
1551 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1552 const Output_data* od;
1553 // For DYNAMIC_SYMBOL.
1554 const Symbol* sym;
1555 // For DYNAMIC_STRING.
1556 const char* str;
1557 } u_;
1558 // The dynamic tag.
1559 elfcpp::DT tag_;
1560 // The type of entry.
1561 Classification classification_;
1564 // Add an entry to the list.
1565 void
1566 add_entry(const Dynamic_entry& entry)
1567 { this->entries_.push_back(entry); }
1569 // Sized version of write function.
1570 template<int size, bool big_endian>
1571 void
1572 sized_write(Output_file* of);
1574 // The type of the list of entries.
1575 typedef std::vector<Dynamic_entry> Dynamic_entries;
1577 // The entries.
1578 Dynamic_entries entries_;
1579 // The pool used for strings.
1580 Stringpool* pool_;
1583 // An output section. We don't expect to have too many output
1584 // sections, so we don't bother to do a template on the size.
1586 class Output_section : public Output_data
1588 public:
1589 // Create an output section, giving the name, type, and flags.
1590 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1591 virtual ~Output_section();
1593 // Add a new input section SHNDX, named NAME, with header SHDR, from
1594 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1595 // which applies to this section, or 0 if none, or -1U if more than
1596 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1597 // in a linker script; in that case we need to keep track of input
1598 // sections associated with an output section. Return the offset
1599 // within the output section.
1600 template<int size, bool big_endian>
1601 off_t
1602 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1603 const char *name,
1604 const elfcpp::Shdr<size, big_endian>& shdr,
1605 unsigned int reloc_shndx, bool have_sections_script);
1607 // Add generated data POSD to this output section.
1608 void
1609 add_output_section_data(Output_section_data* posd);
1611 // Return the section name.
1612 const char*
1613 name() const
1614 { return this->name_; }
1616 // Return the section type.
1617 elfcpp::Elf_Word
1618 type() const
1619 { return this->type_; }
1621 // Return the section flags.
1622 elfcpp::Elf_Xword
1623 flags() const
1624 { return this->flags_; }
1626 // Return the entsize field.
1627 uint64_t
1628 entsize() const
1629 { return this->entsize_; }
1631 // Set the entsize field.
1632 void
1633 set_entsize(uint64_t v);
1635 // Set the load address.
1636 void
1637 set_load_address(uint64_t load_address)
1639 this->load_address_ = load_address;
1640 this->has_load_address_ = true;
1643 // Set the link field to the output section index of a section.
1644 void
1645 set_link_section(const Output_data* od)
1647 gold_assert(this->link_ == 0
1648 && !this->should_link_to_symtab_
1649 && !this->should_link_to_dynsym_);
1650 this->link_section_ = od;
1653 // Set the link field to a constant.
1654 void
1655 set_link(unsigned int v)
1657 gold_assert(this->link_section_ == NULL
1658 && !this->should_link_to_symtab_
1659 && !this->should_link_to_dynsym_);
1660 this->link_ = v;
1663 // Record that this section should link to the normal symbol table.
1664 void
1665 set_should_link_to_symtab()
1667 gold_assert(this->link_section_ == NULL
1668 && this->link_ == 0
1669 && !this->should_link_to_dynsym_);
1670 this->should_link_to_symtab_ = true;
1673 // Record that this section should link to the dynamic symbol table.
1674 void
1675 set_should_link_to_dynsym()
1677 gold_assert(this->link_section_ == NULL
1678 && this->link_ == 0
1679 && !this->should_link_to_symtab_);
1680 this->should_link_to_dynsym_ = true;
1683 // Return the info field.
1684 unsigned int
1685 info() const
1687 gold_assert(this->info_section_ == NULL
1688 && this->info_symndx_ == NULL);
1689 return this->info_;
1692 // Set the info field to the output section index of a section.
1693 void
1694 set_info_section(const Output_section* os)
1696 gold_assert((this->info_section_ == NULL
1697 || (this->info_section_ == os
1698 && this->info_uses_section_index_))
1699 && this->info_symndx_ == NULL
1700 && this->info_ == 0);
1701 this->info_section_ = os;
1702 this->info_uses_section_index_= true;
1705 // Set the info field to the symbol table index of a symbol.
1706 void
1707 set_info_symndx(const Symbol* sym)
1709 gold_assert(this->info_section_ == NULL
1710 && (this->info_symndx_ == NULL
1711 || this->info_symndx_ == sym)
1712 && this->info_ == 0);
1713 this->info_symndx_ = sym;
1716 // Set the info field to the symbol table index of a section symbol.
1717 void
1718 set_info_section_symndx(const Output_section* os)
1720 gold_assert((this->info_section_ == NULL
1721 || (this->info_section_ == os
1722 && !this->info_uses_section_index_))
1723 && this->info_symndx_ == NULL
1724 && this->info_ == 0);
1725 this->info_section_ = os;
1726 this->info_uses_section_index_ = false;
1729 // Set the info field to a constant.
1730 void
1731 set_info(unsigned int v)
1733 gold_assert(this->info_section_ == NULL
1734 && this->info_symndx_ == NULL
1735 && (this->info_ == 0
1736 || this->info_ == v));
1737 this->info_ = v;
1740 // Set the addralign field.
1741 void
1742 set_addralign(uint64_t v)
1743 { this->addralign_ = v; }
1745 // Indicate that we need a symtab index.
1746 void
1747 set_needs_symtab_index()
1748 { this->needs_symtab_index_ = true; }
1750 // Return whether we need a symtab index.
1751 bool
1752 needs_symtab_index() const
1753 { return this->needs_symtab_index_; }
1755 // Get the symtab index.
1756 unsigned int
1757 symtab_index() const
1759 gold_assert(this->symtab_index_ != 0);
1760 return this->symtab_index_;
1763 // Set the symtab index.
1764 void
1765 set_symtab_index(unsigned int index)
1767 gold_assert(index != 0);
1768 this->symtab_index_ = index;
1771 // Indicate that we need a dynsym index.
1772 void
1773 set_needs_dynsym_index()
1774 { this->needs_dynsym_index_ = true; }
1776 // Return whether we need a dynsym index.
1777 bool
1778 needs_dynsym_index() const
1779 { return this->needs_dynsym_index_; }
1781 // Get the dynsym index.
1782 unsigned int
1783 dynsym_index() const
1785 gold_assert(this->dynsym_index_ != 0);
1786 return this->dynsym_index_;
1789 // Set the dynsym index.
1790 void
1791 set_dynsym_index(unsigned int index)
1793 gold_assert(index != 0);
1794 this->dynsym_index_ = index;
1797 // Return whether this section should be written after all the input
1798 // sections are complete.
1799 bool
1800 after_input_sections() const
1801 { return this->after_input_sections_; }
1803 // Record that this section should be written after all the input
1804 // sections are complete.
1805 void
1806 set_after_input_sections()
1807 { this->after_input_sections_ = true; }
1809 // Return whether this section requires postprocessing after all
1810 // relocations have been applied.
1811 bool
1812 requires_postprocessing() const
1813 { return this->requires_postprocessing_; }
1815 // If a section requires postprocessing, return the buffer to use.
1816 unsigned char*
1817 postprocessing_buffer() const
1819 gold_assert(this->postprocessing_buffer_ != NULL);
1820 return this->postprocessing_buffer_;
1823 // If a section requires postprocessing, create the buffer to use.
1824 void
1825 create_postprocessing_buffer();
1827 // If a section requires postprocessing, this is the size of the
1828 // buffer to which relocations should be applied.
1829 off_t
1830 postprocessing_buffer_size() const
1831 { return this->current_data_size_for_child(); }
1833 // Modify the section name. This is only permitted for an
1834 // unallocated section, and only before the size has been finalized.
1835 // Otherwise the name will not get into Layout::namepool_.
1836 void
1837 set_name(const char* newname)
1839 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1840 gold_assert(!this->is_data_size_valid());
1841 this->name_ = newname;
1844 // Return whether the offset OFFSET in the input section SHNDX in
1845 // object OBJECT is being included in the link.
1846 bool
1847 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1848 off_t offset) const;
1850 // Return the offset within the output section of OFFSET relative to
1851 // the start of input section SHNDX in object OBJECT.
1852 section_offset_type
1853 output_offset(const Relobj* object, unsigned int shndx,
1854 section_offset_type offset) const;
1856 // Return the output virtual address of OFFSET relative to the start
1857 // of input section SHNDX in object OBJECT.
1858 uint64_t
1859 output_address(const Relobj* object, unsigned int shndx,
1860 off_t offset) const;
1862 // Return the output address of the start of the merged section for
1863 // input section SHNDX in object OBJECT. This is not necessarily
1864 // the offset corresponding to input offset 0 in the section, since
1865 // the section may be mapped arbitrarily.
1866 uint64_t
1867 starting_output_address(const Relobj* object, unsigned int shndx) const;
1869 // Record that this output section was found in the SECTIONS clause
1870 // of a linker script.
1871 void
1872 set_found_in_sections_clause()
1873 { this->found_in_sections_clause_ = true; }
1875 // Return whether this output section was found in the SECTIONS
1876 // clause of a linker script.
1877 bool
1878 found_in_sections_clause() const
1879 { return this->found_in_sections_clause_; }
1881 // Write the section header into *OPHDR.
1882 template<int size, bool big_endian>
1883 void
1884 write_header(const Layout*, const Stringpool*,
1885 elfcpp::Shdr_write<size, big_endian>*) const;
1887 // The next few calls are for linker script support.
1889 // Store the list of input sections for this Output_section into the
1890 // list passed in. This removes the input sections, leaving only
1891 // any Output_section_data elements. This returns the size of those
1892 // Output_section_data elements. ADDRESS is the address of this
1893 // output section. FILL is the fill value to use, in case there are
1894 // any spaces between the remaining Output_section_data elements.
1895 uint64_t
1896 get_input_sections(uint64_t address, const std::string& fill,
1897 std::list<std::pair<Relobj*, unsigned int > >*);
1899 // Add an input section from a script.
1900 void
1901 add_input_section_for_script(Relobj* object, unsigned int shndx,
1902 off_t data_size, uint64_t addralign);
1904 // Set the current size of the output section.
1905 void
1906 set_current_data_size(off_t size)
1907 { this->set_current_data_size_for_child(size); }
1909 // Get the current size of the output section.
1910 off_t
1911 current_data_size() const
1912 { return this->current_data_size_for_child(); }
1914 // End of linker script support.
1916 // Print merge statistics to stderr.
1917 void
1918 print_merge_stats();
1920 protected:
1921 // Return the output section--i.e., the object itself.
1922 Output_section*
1923 do_output_section()
1924 { return this; }
1926 // Return the section index in the output file.
1927 unsigned int
1928 do_out_shndx() const
1930 gold_assert(this->out_shndx_ != -1U);
1931 return this->out_shndx_;
1934 // Set the output section index.
1935 void
1936 do_set_out_shndx(unsigned int shndx)
1938 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
1939 this->out_shndx_ = shndx;
1942 // Set the final data size of the Output_section. For a typical
1943 // Output_section, there is nothing to do, but if there are any
1944 // Output_section_data objects we need to set their final addresses
1945 // here.
1946 virtual void
1947 set_final_data_size();
1949 // Reset the address and file offset.
1950 void
1951 do_reset_address_and_file_offset();
1953 // Write the data to the file. For a typical Output_section, this
1954 // does nothing: the data is written out by calling Object::Relocate
1955 // on each input object. But if there are any Output_section_data
1956 // objects we do need to write them out here.
1957 virtual void
1958 do_write(Output_file*);
1960 // Return the address alignment--function required by parent class.
1961 uint64_t
1962 do_addralign() const
1963 { return this->addralign_; }
1965 // Return whether there is a load address.
1966 bool
1967 do_has_load_address() const
1968 { return this->has_load_address_; }
1970 // Return the load address.
1971 uint64_t
1972 do_load_address() const
1974 gold_assert(this->has_load_address_);
1975 return this->load_address_;
1978 // Return whether this is an Output_section.
1979 bool
1980 do_is_section() const
1981 { return true; }
1983 // Return whether this is a section of the specified type.
1984 bool
1985 do_is_section_type(elfcpp::Elf_Word type) const
1986 { return this->type_ == type; }
1988 // Return whether the specified section flag is set.
1989 bool
1990 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1991 { return (this->flags_ & flag) != 0; }
1993 // Set the TLS offset. Called only for SHT_TLS sections.
1994 void
1995 do_set_tls_offset(uint64_t tls_base);
1997 // Return the TLS offset, relative to the base of the TLS segment.
1998 // Valid only for SHT_TLS sections.
1999 uint64_t
2000 do_tls_offset() const
2001 { return this->tls_offset_; }
2003 // This may be implemented by a child class.
2004 virtual void
2005 do_finalize_name(Layout*)
2008 // Record that this section requires postprocessing after all
2009 // relocations have been applied. This is called by a child class.
2010 void
2011 set_requires_postprocessing()
2013 this->requires_postprocessing_ = true;
2014 this->after_input_sections_ = true;
2017 // Write all the data of an Output_section into the postprocessing
2018 // buffer.
2019 void
2020 write_to_postprocessing_buffer();
2022 private:
2023 // In some cases we need to keep a list of the input sections
2024 // associated with this output section. We only need the list if we
2025 // might have to change the offsets of the input section within the
2026 // output section after we add the input section. The ordinary
2027 // input sections will be written out when we process the object
2028 // file, and as such we don't need to track them here. We do need
2029 // to track Output_section_data objects here. We store instances of
2030 // this structure in a std::vector, so it must be a POD. There can
2031 // be many instances of this structure, so we use a union to save
2032 // some space.
2033 class Input_section
2035 public:
2036 Input_section()
2037 : shndx_(0), p2align_(0)
2039 this->u1_.data_size = 0;
2040 this->u2_.object = NULL;
2043 // For an ordinary input section.
2044 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2045 uint64_t addralign)
2046 : shndx_(shndx),
2047 p2align_(ffsll(static_cast<long long>(addralign)))
2049 gold_assert(shndx != OUTPUT_SECTION_CODE
2050 && shndx != MERGE_DATA_SECTION_CODE
2051 && shndx != MERGE_STRING_SECTION_CODE);
2052 this->u1_.data_size = data_size;
2053 this->u2_.object = object;
2056 // For a non-merge output section.
2057 Input_section(Output_section_data* posd)
2058 : shndx_(OUTPUT_SECTION_CODE),
2059 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2061 this->u1_.data_size = 0;
2062 this->u2_.posd = posd;
2065 // For a merge section.
2066 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2067 : shndx_(is_string
2068 ? MERGE_STRING_SECTION_CODE
2069 : MERGE_DATA_SECTION_CODE),
2070 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2072 this->u1_.entsize = entsize;
2073 this->u2_.posd = posd;
2076 // The required alignment.
2077 uint64_t
2078 addralign() const
2080 return (this->p2align_ == 0
2082 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2085 // Return the required size.
2086 off_t
2087 data_size() const;
2089 // Whether this is an input section.
2090 bool
2091 is_input_section() const
2093 return (this->shndx_ != OUTPUT_SECTION_CODE
2094 && this->shndx_ != MERGE_DATA_SECTION_CODE
2095 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2098 // Return whether this is a merge section which matches the
2099 // parameters.
2100 bool
2101 is_merge_section(bool is_string, uint64_t entsize,
2102 uint64_t addralign) const
2104 return (this->shndx_ == (is_string
2105 ? MERGE_STRING_SECTION_CODE
2106 : MERGE_DATA_SECTION_CODE)
2107 && this->u1_.entsize == entsize
2108 && this->addralign() == addralign);
2111 // Return the object for an input section.
2112 Relobj*
2113 relobj() const
2115 gold_assert(this->is_input_section());
2116 return this->u2_.object;
2119 // Return the input section index for an input section.
2120 unsigned int
2121 shndx() const
2123 gold_assert(this->is_input_section());
2124 return this->shndx_;
2127 // Set the output section.
2128 void
2129 set_output_section(Output_section* os)
2131 gold_assert(!this->is_input_section());
2132 this->u2_.posd->set_output_section(os);
2135 // Set the address and file offset. This is called during
2136 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2137 // the enclosing section.
2138 void
2139 set_address_and_file_offset(uint64_t address, off_t file_offset,
2140 off_t section_file_offset);
2142 // Reset the address and file offset.
2143 void
2144 reset_address_and_file_offset();
2146 // Finalize the data size.
2147 void
2148 finalize_data_size();
2150 // Add an input section, for SHF_MERGE sections.
2151 bool
2152 add_input_section(Relobj* object, unsigned int shndx)
2154 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2155 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2156 return this->u2_.posd->add_input_section(object, shndx);
2159 // Given an input OBJECT, an input section index SHNDX within that
2160 // object, and an OFFSET relative to the start of that input
2161 // section, return whether or not the output offset is known. If
2162 // this function returns true, it sets *POUTPUT to the offset in
2163 // the output section, relative to the start of the input section
2164 // in the output section. *POUTPUT may be different from OFFSET
2165 // for a merged section.
2166 bool
2167 output_offset(const Relobj* object, unsigned int shndx,
2168 section_offset_type offset,
2169 section_offset_type *poutput) const;
2171 // Return whether this is the merge section for the input section
2172 // SHNDX in OBJECT.
2173 bool
2174 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2176 // Write out the data. This does nothing for an input section.
2177 void
2178 write(Output_file*);
2180 // Write the data to a buffer. This does nothing for an input
2181 // section.
2182 void
2183 write_to_buffer(unsigned char*);
2185 // Print statistics about merge sections to stderr.
2186 void
2187 print_merge_stats(const char* section_name)
2189 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2190 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2191 this->u2_.posd->print_merge_stats(section_name);
2194 private:
2195 // Code values which appear in shndx_. If the value is not one of
2196 // these codes, it is the input section index in the object file.
2197 enum
2199 // An Output_section_data.
2200 OUTPUT_SECTION_CODE = -1U,
2201 // An Output_section_data for an SHF_MERGE section with
2202 // SHF_STRINGS not set.
2203 MERGE_DATA_SECTION_CODE = -2U,
2204 // An Output_section_data for an SHF_MERGE section with
2205 // SHF_STRINGS set.
2206 MERGE_STRING_SECTION_CODE = -3U
2209 // For an ordinary input section, this is the section index in the
2210 // input file. For an Output_section_data, this is
2211 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2212 // MERGE_STRING_SECTION_CODE.
2213 unsigned int shndx_;
2214 // The required alignment, stored as a power of 2.
2215 unsigned int p2align_;
2216 union
2218 // For an ordinary input section, the section size.
2219 off_t data_size;
2220 // For OUTPUT_SECTION_CODE, this is not used. For
2221 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2222 // entity size.
2223 uint64_t entsize;
2224 } u1_;
2225 union
2227 // For an ordinary input section, the object which holds the
2228 // input section.
2229 Relobj* object;
2230 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2231 // MERGE_STRING_SECTION_CODE, the data.
2232 Output_section_data* posd;
2233 } u2_;
2236 typedef std::vector<Input_section> Input_section_list;
2238 // Fill data. This is used to fill in data between input sections.
2239 // It is also used for data statements (BYTE, WORD, etc.) in linker
2240 // scripts. When we have to keep track of the input sections, we
2241 // can use an Output_data_const, but we don't want to have to keep
2242 // track of input sections just to implement fills.
2243 class Fill
2245 public:
2246 Fill(off_t section_offset, off_t length)
2247 : section_offset_(section_offset),
2248 length_(convert_to_section_size_type(length))
2251 // Return section offset.
2252 off_t
2253 section_offset() const
2254 { return this->section_offset_; }
2256 // Return fill length.
2257 section_size_type
2258 length() const
2259 { return this->length_; }
2261 private:
2262 // The offset within the output section.
2263 off_t section_offset_;
2264 // The length of the space to fill.
2265 section_size_type length_;
2268 typedef std::vector<Fill> Fill_list;
2270 // Add a new output section by Input_section.
2271 void
2272 add_output_section_data(Input_section*);
2274 // Add an SHF_MERGE input section. Returns true if the section was
2275 // handled.
2276 bool
2277 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2278 uint64_t entsize, uint64_t addralign);
2280 // Add an output SHF_MERGE section POSD to this output section.
2281 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2282 // ENTSIZE is the entity size. This returns the entry added to
2283 // input_sections_.
2284 void
2285 add_output_merge_section(Output_section_data* posd, bool is_string,
2286 uint64_t entsize);
2288 // Most of these fields are only valid after layout.
2290 // The name of the section. This will point into a Stringpool.
2291 const char* name_;
2292 // The section address is in the parent class.
2293 // The section alignment.
2294 uint64_t addralign_;
2295 // The section entry size.
2296 uint64_t entsize_;
2297 // The load address. This is only used when using a linker script
2298 // with a SECTIONS clause. The has_load_address_ field indicates
2299 // whether this field is valid.
2300 uint64_t load_address_;
2301 // The file offset is in the parent class.
2302 // Set the section link field to the index of this section.
2303 const Output_data* link_section_;
2304 // If link_section_ is NULL, this is the link field.
2305 unsigned int link_;
2306 // Set the section info field to the index of this section.
2307 const Output_section* info_section_;
2308 // If info_section_ is NULL, set the info field to the symbol table
2309 // index of this symbol.
2310 const Symbol* info_symndx_;
2311 // If info_section_ and info_symndx_ are NULL, this is the section
2312 // info field.
2313 unsigned int info_;
2314 // The section type.
2315 const elfcpp::Elf_Word type_;
2316 // The section flags.
2317 elfcpp::Elf_Xword flags_;
2318 // The section index.
2319 unsigned int out_shndx_;
2320 // If there is a STT_SECTION for this output section in the normal
2321 // symbol table, this is the symbol index. This starts out as zero.
2322 // It is initialized in Layout::finalize() to be the index, or -1U
2323 // if there isn't one.
2324 unsigned int symtab_index_;
2325 // If there is a STT_SECTION for this output section in the dynamic
2326 // symbol table, this is the symbol index. This starts out as zero.
2327 // It is initialized in Layout::finalize() to be the index, or -1U
2328 // if there isn't one.
2329 unsigned int dynsym_index_;
2330 // The input sections. This will be empty in cases where we don't
2331 // need to keep track of them.
2332 Input_section_list input_sections_;
2333 // The offset of the first entry in input_sections_.
2334 off_t first_input_offset_;
2335 // The fill data. This is separate from input_sections_ because we
2336 // often will need fill sections without needing to keep track of
2337 // input sections.
2338 Fill_list fills_;
2339 // If the section requires postprocessing, this buffer holds the
2340 // section contents during relocation.
2341 unsigned char* postprocessing_buffer_;
2342 // Whether this output section needs a STT_SECTION symbol in the
2343 // normal symbol table. This will be true if there is a relocation
2344 // which needs it.
2345 bool needs_symtab_index_ : 1;
2346 // Whether this output section needs a STT_SECTION symbol in the
2347 // dynamic symbol table. This will be true if there is a dynamic
2348 // relocation which needs it.
2349 bool needs_dynsym_index_ : 1;
2350 // Whether the link field of this output section should point to the
2351 // normal symbol table.
2352 bool should_link_to_symtab_ : 1;
2353 // Whether the link field of this output section should point to the
2354 // dynamic symbol table.
2355 bool should_link_to_dynsym_ : 1;
2356 // Whether this section should be written after all the input
2357 // sections are complete.
2358 bool after_input_sections_ : 1;
2359 // Whether this section requires post processing after all
2360 // relocations have been applied.
2361 bool requires_postprocessing_ : 1;
2362 // Whether an input section was mapped to this output section
2363 // because of a SECTIONS clause in a linker script.
2364 bool found_in_sections_clause_ : 1;
2365 // Whether this section has an explicitly specified load address.
2366 bool has_load_address_ : 1;
2367 // True if the info_section_ field means the section index of the
2368 // section, false if it means the symbol index of the corresponding
2369 // section symbol.
2370 bool info_uses_section_index_ : 1;
2371 // For SHT_TLS sections, the offset of this section relative to the base
2372 // of the TLS segment.
2373 uint64_t tls_offset_;
2376 // An output segment. PT_LOAD segments are built from collections of
2377 // output sections. Other segments typically point within PT_LOAD
2378 // segments, and are built directly as needed.
2380 class Output_segment
2382 public:
2383 // Create an output segment, specifying the type and flags.
2384 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2386 // Return the virtual address.
2387 uint64_t
2388 vaddr() const
2389 { return this->vaddr_; }
2391 // Return the physical address.
2392 uint64_t
2393 paddr() const
2394 { return this->paddr_; }
2396 // Return the segment type.
2397 elfcpp::Elf_Word
2398 type() const
2399 { return this->type_; }
2401 // Return the segment flags.
2402 elfcpp::Elf_Word
2403 flags() const
2404 { return this->flags_; }
2406 // Return the memory size.
2407 uint64_t
2408 memsz() const
2409 { return this->memsz_; }
2411 // Return the file size.
2412 off_t
2413 filesz() const
2414 { return this->filesz_; }
2416 // Return the file offset.
2417 off_t
2418 offset() const
2419 { return this->offset_; }
2421 // Return the maximum alignment of the Output_data.
2422 uint64_t
2423 maximum_alignment();
2425 // Add an Output_section to this segment.
2426 void
2427 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2428 { this->add_output_section(os, seg_flags, false); }
2430 // Add an Output_section to the start of this segment.
2431 void
2432 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2433 { this->add_output_section(os, seg_flags, true); }
2435 // Add an Output_data (which is not an Output_section) to the start
2436 // of this segment.
2437 void
2438 add_initial_output_data(Output_data*);
2440 // Return the number of dynamic relocations applied to this segment.
2441 unsigned int
2442 dynamic_reloc_count() const;
2444 // Return the address of the first section.
2445 uint64_t
2446 first_section_load_address() const;
2448 // Return whether the addresses have been set already.
2449 bool
2450 are_addresses_set() const
2451 { return this->are_addresses_set_; }
2453 // Set the addresses.
2454 void
2455 set_addresses(uint64_t vaddr, uint64_t paddr)
2457 this->vaddr_ = vaddr;
2458 this->paddr_ = paddr;
2459 this->are_addresses_set_ = true;
2462 // Set the segment flags. This is only used if we have a PHDRS
2463 // clause which explicitly specifies the flags.
2464 void
2465 set_flags(elfcpp::Elf_Word flags)
2466 { this->flags_ = flags; }
2468 // Set the address of the segment to ADDR and the offset to *POFF
2469 // and set the addresses and offsets of all contained output
2470 // sections accordingly. Set the section indexes of all contained
2471 // output sections starting with *PSHNDX. If RESET is true, first
2472 // reset the addresses of the contained sections. Return the
2473 // address of the immediately following segment. Update *POFF and
2474 // *PSHNDX. This should only be called for a PT_LOAD segment.
2475 uint64_t
2476 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2477 unsigned int* pshndx);
2479 // Set the minimum alignment of this segment. This may be adjusted
2480 // upward based on the section alignments.
2481 void
2482 set_minimum_p_align(uint64_t align)
2483 { this->min_p_align_ = align; }
2485 // Set the offset of this segment based on the section. This should
2486 // only be called for a non-PT_LOAD segment.
2487 void
2488 set_offset();
2490 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2491 void
2492 set_tls_offsets();
2494 // Return the number of output sections.
2495 unsigned int
2496 output_section_count() const;
2498 // Return the section attached to the list segment with the lowest
2499 // load address. This is used when handling a PHDRS clause in a
2500 // linker script.
2501 Output_section*
2502 section_with_lowest_load_address() const;
2504 // Write the segment header into *OPHDR.
2505 template<int size, bool big_endian>
2506 void
2507 write_header(elfcpp::Phdr_write<size, big_endian>*);
2509 // Write the section headers of associated sections into V.
2510 template<int size, bool big_endian>
2511 unsigned char*
2512 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2513 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2515 private:
2516 Output_segment(const Output_segment&);
2517 Output_segment& operator=(const Output_segment&);
2519 typedef std::list<Output_data*> Output_data_list;
2521 // Add an Output_section to this segment, specifying front or back.
2522 void
2523 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2524 bool front);
2526 // Find the maximum alignment in an Output_data_list.
2527 static uint64_t
2528 maximum_alignment_list(const Output_data_list*);
2530 // Set the section addresses in an Output_data_list.
2531 uint64_t
2532 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2533 off_t* poff, unsigned int* pshndx);
2535 // Return the number of Output_sections in an Output_data_list.
2536 unsigned int
2537 output_section_count_list(const Output_data_list*) const;
2539 // Return the number of dynamic relocs in an Output_data_list.
2540 unsigned int
2541 dynamic_reloc_count_list(const Output_data_list*) const;
2543 // Find the section with the lowest load address in an
2544 // Output_data_list.
2545 void
2546 lowest_load_address_in_list(const Output_data_list* pdl,
2547 Output_section** found,
2548 uint64_t* found_lma) const;
2550 // Write the section headers in the list into V.
2551 template<int size, bool big_endian>
2552 unsigned char*
2553 write_section_headers_list(const Layout*, const Stringpool*,
2554 const Output_data_list*, unsigned char* v,
2555 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2557 // The list of output data with contents attached to this segment.
2558 Output_data_list output_data_;
2559 // The list of output data without contents attached to this segment.
2560 Output_data_list output_bss_;
2561 // The segment virtual address.
2562 uint64_t vaddr_;
2563 // The segment physical address.
2564 uint64_t paddr_;
2565 // The size of the segment in memory.
2566 uint64_t memsz_;
2567 // The maximum section alignment. The is_max_align_known_ field
2568 // indicates whether this has been finalized.
2569 uint64_t max_align_;
2570 // The required minimum value for the p_align field. This is used
2571 // for PT_LOAD segments. Note that this does not mean that
2572 // addresses should be aligned to this value; it means the p_paddr
2573 // and p_vaddr fields must be congruent modulo this value. For
2574 // non-PT_LOAD segments, the dynamic linker works more efficiently
2575 // if the p_align field has the more conventional value, although it
2576 // can align as needed.
2577 uint64_t min_p_align_;
2578 // The offset of the segment data within the file.
2579 off_t offset_;
2580 // The size of the segment data in the file.
2581 off_t filesz_;
2582 // The segment type;
2583 elfcpp::Elf_Word type_;
2584 // The segment flags.
2585 elfcpp::Elf_Word flags_;
2586 // Whether we have finalized max_align_.
2587 bool is_max_align_known_ : 1;
2588 // Whether vaddr and paddr were set by a linker script.
2589 bool are_addresses_set_ : 1;
2592 // This class represents the output file.
2594 class Output_file
2596 public:
2597 Output_file(const char* name);
2599 // Indicate that this is a temporary file which should not be
2600 // output.
2601 void
2602 set_is_temporary()
2603 { this->is_temporary_ = true; }
2605 // Open the output file. FILE_SIZE is the final size of the file.
2606 void
2607 open(off_t file_size);
2609 // Resize the output file.
2610 void
2611 resize(off_t file_size);
2613 // Close the output file (flushing all buffered data) and make sure
2614 // there are no errors.
2615 void
2616 close();
2618 // We currently always use mmap which makes the view handling quite
2619 // simple. In the future we may support other approaches.
2621 // Write data to the output file.
2622 void
2623 write(off_t offset, const void* data, size_t len)
2624 { memcpy(this->base_ + offset, data, len); }
2626 // Get a buffer to use to write to the file, given the offset into
2627 // the file and the size.
2628 unsigned char*
2629 get_output_view(off_t start, size_t size)
2631 gold_assert(start >= 0
2632 && start + static_cast<off_t>(size) <= this->file_size_);
2633 return this->base_ + start;
2636 // VIEW must have been returned by get_output_view. Write the
2637 // buffer to the file, passing in the offset and the size.
2638 void
2639 write_output_view(off_t, size_t, unsigned char*)
2642 // Get a read/write buffer. This is used when we want to write part
2643 // of the file, read it in, and write it again.
2644 unsigned char*
2645 get_input_output_view(off_t start, size_t size)
2646 { return this->get_output_view(start, size); }
2648 // Write a read/write buffer back to the file.
2649 void
2650 write_input_output_view(off_t, size_t, unsigned char*)
2653 // Get a read buffer. This is used when we just want to read part
2654 // of the file back it in.
2655 const unsigned char*
2656 get_input_view(off_t start, size_t size)
2657 { return this->get_output_view(start, size); }
2659 // Release a read bfufer.
2660 void
2661 free_input_view(off_t, size_t, const unsigned char*)
2664 private:
2665 // Map the file into memory and return a pointer to the map.
2666 void
2667 map();
2669 // Unmap the file from memory (and flush to disk buffers).
2670 void
2671 unmap();
2673 // File name.
2674 const char* name_;
2675 // File descriptor.
2676 int o_;
2677 // File size.
2678 off_t file_size_;
2679 // Base of file mapped into memory.
2680 unsigned char* base_;
2681 // True iff base_ points to a memory buffer rather than an output file.
2682 bool map_is_anonymous_;
2683 // True if this is a temporary file which should not be output.
2684 bool is_temporary_;
2687 } // End namespace gold.
2689 #endif // !defined(GOLD_OUTPUT_H)