Support --oformat binary.
[binutils.git] / gold / output.h
blob5e68e7c294ae238a749b7afedb593eb604f0e031
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 index, if there is an output section.
135 unsigned int
136 out_shndx() const
137 { return this->do_out_shndx(); }
139 // Set the output section index, if this is an output section.
140 void
141 set_out_shndx(unsigned int shndx)
142 { this->do_set_out_shndx(shndx); }
144 // Set the address and file offset of this data, and finalize the
145 // size of the data. This is called during Layout::finalize for
146 // allocated sections.
147 void
148 set_address_and_file_offset(uint64_t addr, off_t off)
150 this->set_address(addr);
151 this->set_file_offset(off);
152 this->finalize_data_size();
155 // Set the address.
156 void
157 set_address(uint64_t addr)
159 gold_assert(!this->is_address_valid_);
160 this->address_ = addr;
161 this->is_address_valid_ = true;
164 // Set the file offset.
165 void
166 set_file_offset(off_t off)
168 gold_assert(!this->is_offset_valid_);
169 this->offset_ = off;
170 this->is_offset_valid_ = true;
173 // Finalize the data size.
174 void
175 finalize_data_size()
177 if (!this->is_data_size_valid_)
179 // Tell the child class to set the data size.
180 this->set_final_data_size();
181 gold_assert(this->is_data_size_valid_);
185 // Set the TLS offset. Called only for SHT_TLS sections.
186 void
187 set_tls_offset(uint64_t tls_base)
188 { this->do_set_tls_offset(tls_base); }
190 // Return the TLS offset, relative to the base of the TLS segment.
191 // Valid only for SHT_TLS sections.
192 uint64_t
193 tls_offset() const
194 { return this->do_tls_offset(); }
196 // Write the data to the output file. This is called after
197 // Layout::finalize is complete.
198 void
199 write(Output_file* file)
200 { this->do_write(file); }
202 // This is called by Layout::finalize to note that the sizes of
203 // allocated sections must now be fixed.
204 static void
205 layout_complete()
206 { Output_data::allocated_sizes_are_fixed = true; }
208 // Used to check that layout has been done.
209 static bool
210 is_layout_complete()
211 { return Output_data::allocated_sizes_are_fixed; }
213 // Count the number of dynamic relocations applied to this section.
214 void
215 add_dynamic_reloc()
216 { ++this->dynamic_reloc_count_; }
218 // Return the number of dynamic relocations applied to this section.
219 unsigned int
220 dynamic_reloc_count() const
221 { return this->dynamic_reloc_count_; }
223 // Whether the address is valid.
224 bool
225 is_address_valid() const
226 { return this->is_address_valid_; }
228 // Whether the file offset is valid.
229 bool
230 is_offset_valid() const
231 { return this->is_offset_valid_; }
233 // Whether the data size is valid.
234 bool
235 is_data_size_valid() const
236 { return this->is_data_size_valid_; }
238 protected:
239 // Functions that child classes may or in some cases must implement.
241 // Write the data to the output file.
242 virtual void
243 do_write(Output_file*) = 0;
245 // Return the required alignment.
246 virtual uint64_t
247 do_addralign() const = 0;
249 // Return whether this has a load address.
250 virtual bool
251 do_has_load_address() const
252 { return false; }
254 // Return the load address.
255 virtual uint64_t
256 do_load_address() const
257 { gold_unreachable(); }
259 // Return whether this is an Output_section.
260 virtual bool
261 do_is_section() const
262 { return false; }
264 // Return whether this is an Output_section of the specified type.
265 // This only needs to be implement by Output_section.
266 virtual bool
267 do_is_section_type(elfcpp::Elf_Word) const
268 { return false; }
270 // Return whether this is an Output_section with the specific flag
271 // set. This only needs to be implemented by Output_section.
272 virtual bool
273 do_is_section_flag_set(elfcpp::Elf_Xword) const
274 { return false; }
276 // Return the output section index, if there is an output section.
277 virtual unsigned int
278 do_out_shndx() const
279 { gold_unreachable(); }
281 // Set the output section index, if this is an output section.
282 virtual void
283 do_set_out_shndx(unsigned int)
284 { gold_unreachable(); }
286 // This is a hook for derived classes to set the data size. This is
287 // called by finalize_data_size, normally called during
288 // Layout::finalize, when the section address is set.
289 virtual void
290 set_final_data_size()
291 { gold_unreachable(); }
293 // A hook for resetting the address and file offset.
294 virtual void
295 do_reset_address_and_file_offset()
298 // Set the TLS offset. Called only for SHT_TLS sections.
299 virtual void
300 do_set_tls_offset(uint64_t)
301 { gold_unreachable(); }
303 // Return the TLS offset, relative to the base of the TLS segment.
304 // Valid only for SHT_TLS sections.
305 virtual uint64_t
306 do_tls_offset() const
307 { gold_unreachable(); }
309 // Functions that child classes may call.
311 // Set the size of the data.
312 void
313 set_data_size(off_t data_size)
315 gold_assert(!this->is_data_size_valid_);
316 this->data_size_ = data_size;
317 this->is_data_size_valid_ = true;
320 // Get the current data size--this is for the convenience of
321 // sections which build up their size over time.
322 off_t
323 current_data_size_for_child() const
324 { return this->data_size_; }
326 // Set the current data size--this is for the convenience of
327 // sections which build up their size over time.
328 void
329 set_current_data_size_for_child(off_t data_size)
331 gold_assert(!this->is_data_size_valid_);
332 this->data_size_ = data_size;
335 // Return default alignment for the target size.
336 static uint64_t
337 default_alignment();
339 // Return default alignment for a specified size--32 or 64.
340 static uint64_t
341 default_alignment_for_size(int size);
343 private:
344 Output_data(const Output_data&);
345 Output_data& operator=(const Output_data&);
347 // This is used for verification, to make sure that we don't try to
348 // change any sizes of allocated sections after we set the section
349 // addresses.
350 static bool allocated_sizes_are_fixed;
352 // Memory address in output file.
353 uint64_t address_;
354 // Size of data in output file.
355 off_t data_size_;
356 // File offset of contents in output file.
357 off_t offset_;
358 // Whether address_ is valid.
359 bool is_address_valid_;
360 // Whether data_size_ is valid.
361 bool is_data_size_valid_;
362 // Whether offset_ is valid.
363 bool is_offset_valid_;
364 // Count of dynamic relocations applied to this section.
365 unsigned int dynamic_reloc_count_;
368 // Output the section headers.
370 class Output_section_headers : public Output_data
372 public:
373 Output_section_headers(const Layout*,
374 const Layout::Segment_list*,
375 const Layout::Section_list*,
376 const Layout::Section_list*,
377 const Stringpool*);
379 protected:
380 // Write the data to the file.
381 void
382 do_write(Output_file*);
384 // Return the required alignment.
385 uint64_t
386 do_addralign() const
387 { return Output_data::default_alignment(); }
389 private:
390 // Write the data to the file with the right size and endianness.
391 template<int size, bool big_endian>
392 void
393 do_sized_write(Output_file*);
395 const Layout* layout_;
396 const Layout::Segment_list* segment_list_;
397 const Layout::Section_list* section_list_;
398 const Layout::Section_list* unattached_section_list_;
399 const Stringpool* secnamepool_;
402 // Output the segment headers.
404 class Output_segment_headers : public Output_data
406 public:
407 Output_segment_headers(const Layout::Segment_list& segment_list);
409 protected:
410 // Write the data to the file.
411 void
412 do_write(Output_file*);
414 // Return the required alignment.
415 uint64_t
416 do_addralign() const
417 { return Output_data::default_alignment(); }
419 private:
420 // Write the data to the file with the right size and endianness.
421 template<int size, bool big_endian>
422 void
423 do_sized_write(Output_file*);
425 const Layout::Segment_list& segment_list_;
428 // Output the ELF file header.
430 class Output_file_header : public Output_data
432 public:
433 Output_file_header(const Target*,
434 const Symbol_table*,
435 const Output_segment_headers*,
436 const char* entry);
438 // Add information about the section headers. We lay out the ELF
439 // file header before we create the section headers.
440 void set_section_info(const Output_section_headers*,
441 const Output_section* shstrtab);
443 protected:
444 // Write the data to the file.
445 void
446 do_write(Output_file*);
448 // Return the required alignment.
449 uint64_t
450 do_addralign() const
451 { return Output_data::default_alignment(); }
453 private:
454 // Write the data to the file with the right size and endianness.
455 template<int size, bool big_endian>
456 void
457 do_sized_write(Output_file*);
459 // Return the value to use for the entry address.
460 template<int size>
461 typename elfcpp::Elf_types<size>::Elf_Addr
462 entry();
464 const Target* target_;
465 const Symbol_table* symtab_;
466 const Output_segment_headers* segment_header_;
467 const Output_section_headers* section_header_;
468 const Output_section* shstrtab_;
469 const char* entry_;
472 // Output sections are mainly comprised of input sections. However,
473 // there are cases where we have data to write out which is not in an
474 // input section. Output_section_data is used in such cases. This is
475 // an abstract base class.
477 class Output_section_data : public Output_data
479 public:
480 Output_section_data(off_t data_size, uint64_t addralign)
481 : Output_data(), output_section_(NULL), addralign_(addralign)
482 { this->set_data_size(data_size); }
484 Output_section_data(uint64_t addralign)
485 : Output_data(), output_section_(NULL), addralign_(addralign)
488 // Return the output section.
489 const Output_section*
490 output_section() const
491 { return this->output_section_; }
493 // Record the output section.
494 void
495 set_output_section(Output_section* os);
497 // Add an input section, for SHF_MERGE sections. This returns true
498 // if the section was handled.
499 bool
500 add_input_section(Relobj* object, unsigned int shndx)
501 { return this->do_add_input_section(object, shndx); }
503 // Given an input OBJECT, an input section index SHNDX within that
504 // object, and an OFFSET relative to the start of that input
505 // section, return whether or not the corresponding offset within
506 // the output section is known. If this function returns true, it
507 // sets *POUTPUT to the output offset. The value -1 indicates that
508 // this input offset is being discarded.
509 bool
510 output_offset(const Relobj* object, unsigned int shndx,
511 section_offset_type offset,
512 section_offset_type *poutput) const
513 { return this->do_output_offset(object, shndx, offset, poutput); }
515 // Return whether this is the merge section for the input section
516 // SHNDX in OBJECT. This should return true when output_offset
517 // would return true for some values of OFFSET.
518 bool
519 is_merge_section_for(const Relobj* object, unsigned int shndx) const
520 { return this->do_is_merge_section_for(object, shndx); }
522 // Write the contents to a buffer. This is used for sections which
523 // require postprocessing, such as compression.
524 void
525 write_to_buffer(unsigned char* buffer)
526 { this->do_write_to_buffer(buffer); }
528 // Print merge stats to stderr. This should only be called for
529 // SHF_MERGE sections.
530 void
531 print_merge_stats(const char* section_name)
532 { this->do_print_merge_stats(section_name); }
534 protected:
535 // The child class must implement do_write.
537 // The child class may implement specific adjustments to the output
538 // section.
539 virtual void
540 do_adjust_output_section(Output_section*)
543 // May be implemented by child class. Return true if the section
544 // was handled.
545 virtual bool
546 do_add_input_section(Relobj*, unsigned int)
547 { gold_unreachable(); }
549 // The child class may implement output_offset.
550 virtual bool
551 do_output_offset(const Relobj*, unsigned int, section_offset_type,
552 section_offset_type*) const
553 { return false; }
555 // The child class may implement is_merge_section_for.
556 virtual bool
557 do_is_merge_section_for(const Relobj*, unsigned int) const
558 { return false; }
560 // The child class may implement write_to_buffer. Most child
561 // classes can not appear in a compressed section, and they do not
562 // implement this.
563 virtual void
564 do_write_to_buffer(unsigned char*)
565 { gold_unreachable(); }
567 // Print merge statistics.
568 virtual void
569 do_print_merge_stats(const char*)
570 { gold_unreachable(); }
572 // Return the required alignment.
573 uint64_t
574 do_addralign() const
575 { return this->addralign_; }
577 // Return the section index of the output section.
578 unsigned int
579 do_out_shndx() const;
581 // Set the alignment.
582 void
583 set_addralign(uint64_t addralign)
584 { this->addralign_ = addralign; }
586 private:
587 // The output section for this section.
588 const Output_section* output_section_;
589 // The required alignment.
590 uint64_t addralign_;
593 // Some Output_section_data classes build up their data step by step,
594 // rather than all at once. This class provides an interface for
595 // them.
597 class Output_section_data_build : public Output_section_data
599 public:
600 Output_section_data_build(uint64_t addralign)
601 : Output_section_data(addralign)
604 // Get the current data size.
605 off_t
606 current_data_size() const
607 { return this->current_data_size_for_child(); }
609 // Set the current data size.
610 void
611 set_current_data_size(off_t data_size)
612 { this->set_current_data_size_for_child(data_size); }
614 protected:
615 // Set the final data size.
616 virtual void
617 set_final_data_size()
618 { this->set_data_size(this->current_data_size_for_child()); }
621 // A simple case of Output_data in which we have constant data to
622 // output.
624 class Output_data_const : public Output_section_data
626 public:
627 Output_data_const(const std::string& data, uint64_t addralign)
628 : Output_section_data(data.size(), addralign), data_(data)
631 Output_data_const(const char* p, off_t len, uint64_t addralign)
632 : Output_section_data(len, addralign), data_(p, len)
635 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
636 : Output_section_data(len, addralign),
637 data_(reinterpret_cast<const char*>(p), len)
640 protected:
641 // Write the data to the output file.
642 void
643 do_write(Output_file*);
645 // Write the data to a buffer.
646 void
647 do_write_to_buffer(unsigned char* buffer)
648 { memcpy(buffer, this->data_.data(), this->data_.size()); }
650 private:
651 std::string data_;
654 // Another version of Output_data with constant data, in which the
655 // buffer is allocated by the caller.
657 class Output_data_const_buffer : public Output_section_data
659 public:
660 Output_data_const_buffer(const unsigned char* p, off_t len,
661 uint64_t addralign)
662 : Output_section_data(len, addralign), p_(p)
665 protected:
666 // Write the data the output file.
667 void
668 do_write(Output_file*);
670 // Write the data to a buffer.
671 void
672 do_write_to_buffer(unsigned char* buffer)
673 { memcpy(buffer, this->p_, this->data_size()); }
675 private:
676 const unsigned char* p_;
679 // A place holder for a fixed amount of data written out via some
680 // other mechanism.
682 class Output_data_fixed_space : public Output_section_data
684 public:
685 Output_data_fixed_space(off_t data_size, uint64_t addralign)
686 : Output_section_data(data_size, addralign)
689 protected:
690 // Write out the data--the actual data must be written out
691 // elsewhere.
692 void
693 do_write(Output_file*)
697 // A place holder for variable sized data written out via some other
698 // mechanism.
700 class Output_data_space : public Output_section_data_build
702 public:
703 explicit Output_data_space(uint64_t addralign)
704 : Output_section_data_build(addralign)
707 // Set the alignment.
708 void
709 set_space_alignment(uint64_t align)
710 { this->set_addralign(align); }
712 protected:
713 // Write out the data--the actual data must be written out
714 // elsewhere.
715 void
716 do_write(Output_file*)
720 // A string table which goes into an output section.
722 class Output_data_strtab : public Output_section_data
724 public:
725 Output_data_strtab(Stringpool* strtab)
726 : Output_section_data(1), strtab_(strtab)
729 protected:
730 // This is called to set the address and file offset. Here we make
731 // sure that the Stringpool is finalized.
732 void
733 set_final_data_size();
735 // Write out the data.
736 void
737 do_write(Output_file*);
739 // Write the data to a buffer.
740 void
741 do_write_to_buffer(unsigned char* buffer)
742 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
744 private:
745 Stringpool* strtab_;
748 // This POD class is used to represent a single reloc in the output
749 // file. This could be a private class within Output_data_reloc, but
750 // the templatization is complex enough that I broke it out into a
751 // separate class. The class is templatized on either elfcpp::SHT_REL
752 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
753 // relocation or an ordinary relocation.
755 // A relocation can be against a global symbol, a local symbol, an
756 // output section, or the undefined symbol at index 0. We represent
757 // the latter by using a NULL global symbol.
759 template<int sh_type, bool dynamic, int size, bool big_endian>
760 class Output_reloc;
762 template<bool dynamic, int size, bool big_endian>
763 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
765 public:
766 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
768 // An uninitialized entry. We need this because we want to put
769 // instances of this class into an STL container.
770 Output_reloc()
771 : local_sym_index_(INVALID_CODE)
774 // A reloc against a global symbol.
776 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
777 Address address, bool is_relative);
779 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
780 unsigned int shndx, Address address, bool is_relative);
782 // A reloc against a local symbol.
784 Output_reloc(Sized_relobj<size, big_endian>* relobj,
785 unsigned int local_sym_index, unsigned int type,
786 Output_data* od, Address address, bool is_relative);
788 Output_reloc(Sized_relobj<size, big_endian>* relobj,
789 unsigned int local_sym_index, unsigned int type,
790 unsigned int shndx, Address address, bool is_relative);
792 // A reloc against the STT_SECTION symbol of an output section.
794 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
795 Address address);
797 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
798 unsigned int shndx, Address address);
800 // Return TRUE if this is a RELATIVE relocation.
801 bool
802 is_relative() const
803 { return this->is_relative_; }
805 // Get the value of the symbol referred to by a Rel relocation.
807 Address
808 symbol_value() const;
810 // Write the reloc entry to an output view.
811 void
812 write(unsigned char* pov) const;
814 // Write the offset and info fields to Write_rel.
815 template<typename Write_rel>
816 void write_rel(Write_rel*) const;
818 private:
819 // Return the symbol index. We can't do a double template
820 // specialization, so we do a secondary template here.
821 unsigned int
822 get_symbol_index() const;
824 // Codes for local_sym_index_.
825 enum
827 // Global symbol.
828 GSYM_CODE = -1U,
829 // Output section.
830 SECTION_CODE = -2U,
831 // Invalid uninitialized entry.
832 INVALID_CODE = -3U
835 union
837 // For a local symbol, the object. We will never generate a
838 // relocation against a local symbol in a dynamic object; that
839 // doesn't make sense. And our callers will always be
840 // templatized, so we use Sized_relobj here.
841 Sized_relobj<size, big_endian>* relobj;
842 // For a global symbol, the symbol. If this is NULL, it indicates
843 // a relocation against the undefined 0 symbol.
844 Symbol* gsym;
845 // For a relocation against an output section, the output section.
846 Output_section* os;
847 } u1_;
848 union
850 // If shndx_ is not INVALID CODE, the object which holds the input
851 // section being used to specify the reloc address.
852 Relobj* relobj;
853 // If shndx_ is INVALID_CODE, the output data being used to
854 // specify the reloc address. This may be NULL if the reloc
855 // address is absolute.
856 Output_data* od;
857 } u2_;
858 // The address offset within the input section or the Output_data.
859 Address address_;
860 // For a local symbol, the local symbol index. This is GSYM_CODE
861 // for a global symbol, or INVALID_CODE for an uninitialized value.
862 unsigned int local_sym_index_;
863 // The reloc type--a processor specific code.
864 unsigned int type_ : 31;
865 // True if the relocation is a RELATIVE relocation.
866 bool is_relative_ : 1;
867 // If the reloc address is an input section in an object, the
868 // section index. This is INVALID_CODE if the reloc address is
869 // specified in some other way.
870 unsigned int shndx_;
873 // The SHT_RELA version of Output_reloc<>. This is just derived from
874 // the SHT_REL version of Output_reloc, but it adds an addend.
876 template<bool dynamic, int size, bool big_endian>
877 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
879 public:
880 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
881 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
883 // An uninitialized entry.
884 Output_reloc()
885 : rel_()
888 // A reloc against a global symbol.
890 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
891 Address address, Addend addend, bool is_relative)
892 : rel_(gsym, type, od, address, is_relative), addend_(addend)
895 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
896 unsigned int shndx, Address address, Addend addend,
897 bool is_relative)
898 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
901 // A reloc against a local symbol.
903 Output_reloc(Sized_relobj<size, big_endian>* relobj,
904 unsigned int local_sym_index, unsigned int type,
905 Output_data* od, Address address,
906 Addend addend, bool is_relative)
907 : rel_(relobj, local_sym_index, type, od, address, is_relative),
908 addend_(addend)
911 Output_reloc(Sized_relobj<size, big_endian>* relobj,
912 unsigned int local_sym_index, unsigned int type,
913 unsigned int shndx, Address address,
914 Addend addend, bool is_relative)
915 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
916 addend_(addend)
919 // A reloc against the STT_SECTION symbol of an output section.
921 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
922 Address address, Addend addend)
923 : rel_(os, type, od, address), addend_(addend)
926 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
927 unsigned int shndx, Address address, Addend addend)
928 : rel_(os, type, relobj, shndx, address), addend_(addend)
931 // Write the reloc entry to an output view.
932 void
933 write(unsigned char* pov) const;
935 private:
936 // The basic reloc.
937 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
938 // The addend.
939 Addend addend_;
942 // Output_data_reloc is used to manage a section containing relocs.
943 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
944 // indicates whether this is a dynamic relocation or a normal
945 // relocation. Output_data_reloc_base is a base class.
946 // Output_data_reloc is the real class, which we specialize based on
947 // the reloc type.
949 template<int sh_type, bool dynamic, int size, bool big_endian>
950 class Output_data_reloc_base : public Output_section_data_build
952 public:
953 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
954 typedef typename Output_reloc_type::Address Address;
955 static const int reloc_size =
956 Reloc_types<sh_type, size, big_endian>::reloc_size;
958 // Construct the section.
959 Output_data_reloc_base()
960 : Output_section_data_build(Output_data::default_alignment_for_size(size))
963 protected:
964 // Write out the data.
965 void
966 do_write(Output_file*);
968 // Set the entry size and the link.
969 void
970 do_adjust_output_section(Output_section *os);
972 // Add a relocation entry.
973 void
974 add(Output_data *od, const Output_reloc_type& reloc)
976 this->relocs_.push_back(reloc);
977 this->set_current_data_size(this->relocs_.size() * reloc_size);
978 od->add_dynamic_reloc();
981 private:
982 typedef std::vector<Output_reloc_type> Relocs;
984 Relocs relocs_;
987 // The class which callers actually create.
989 template<int sh_type, bool dynamic, int size, bool big_endian>
990 class Output_data_reloc;
992 // The SHT_REL version of Output_data_reloc.
994 template<bool dynamic, int size, bool big_endian>
995 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
996 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
998 private:
999 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1000 big_endian> Base;
1002 public:
1003 typedef typename Base::Output_reloc_type Output_reloc_type;
1004 typedef typename Output_reloc_type::Address Address;
1006 Output_data_reloc()
1007 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1010 // Add a reloc against a global symbol.
1012 void
1013 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1014 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1016 void
1017 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1018 unsigned int shndx, Address address)
1019 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1020 false)); }
1022 // Add a RELATIVE reloc against a global symbol. The final relocation
1023 // will not reference the symbol.
1025 void
1026 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1027 Address address)
1028 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1030 void
1031 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1032 Relobj* relobj, unsigned int shndx, Address address)
1033 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1034 true)); }
1036 // Add a reloc against a local symbol.
1038 void
1039 add_local(Sized_relobj<size, big_endian>* relobj,
1040 unsigned int local_sym_index, unsigned int type,
1041 Output_data* od, Address address)
1042 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1043 address, false)); }
1045 void
1046 add_local(Sized_relobj<size, big_endian>* relobj,
1047 unsigned int local_sym_index, unsigned int type,
1048 Output_data* od, unsigned int shndx, Address address)
1049 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1050 address, false)); }
1052 // Add a RELATIVE reloc against a local symbol.
1054 void
1055 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1056 unsigned int local_sym_index, unsigned int type,
1057 Output_data* od, Address address)
1058 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1059 address, true)); }
1061 void
1062 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1063 unsigned int local_sym_index, unsigned int type,
1064 Output_data* od, unsigned int shndx, Address address)
1065 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1066 address, true)); }
1068 // A reloc against the STT_SECTION symbol of an output section.
1069 // OS is the Output_section that the relocation refers to; OD is
1070 // the Output_data object being relocated.
1072 void
1073 add_output_section(Output_section* os, unsigned int type,
1074 Output_data* od, Address address)
1075 { this->add(od, Output_reloc_type(os, type, od, address)); }
1077 void
1078 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1079 Relobj* relobj, unsigned int shndx, Address address)
1080 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1083 // The SHT_RELA version of Output_data_reloc.
1085 template<bool dynamic, int size, bool big_endian>
1086 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1087 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1089 private:
1090 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1091 big_endian> Base;
1093 public:
1094 typedef typename Base::Output_reloc_type Output_reloc_type;
1095 typedef typename Output_reloc_type::Address Address;
1096 typedef typename Output_reloc_type::Addend Addend;
1098 Output_data_reloc()
1099 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1102 // Add a reloc against a global symbol.
1104 void
1105 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1106 Address address, Addend addend)
1107 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1108 false)); }
1110 void
1111 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1112 unsigned int shndx, Address address,
1113 Addend addend)
1114 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1115 addend, false)); }
1117 // Add a RELATIVE reloc against a global symbol. The final output
1118 // relocation will not reference the symbol, but we must keep the symbol
1119 // information long enough to set the addend of the relocation correctly
1120 // when it is written.
1122 void
1123 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1124 Address address, Addend addend)
1125 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1127 void
1128 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1129 Relobj* relobj, unsigned int shndx, Address address,
1130 Addend addend)
1131 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1132 addend, true)); }
1134 // Add a reloc against a local symbol.
1136 void
1137 add_local(Sized_relobj<size, big_endian>* relobj,
1138 unsigned int local_sym_index, unsigned int type,
1139 Output_data* od, Address address, Addend addend)
1141 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1142 addend, false));
1145 void
1146 add_local(Sized_relobj<size, big_endian>* relobj,
1147 unsigned int local_sym_index, unsigned int type,
1148 Output_data* od, unsigned int shndx, Address address,
1149 Addend addend)
1151 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1152 address, addend, false));
1155 // Add a RELATIVE reloc against a local symbol.
1157 void
1158 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1159 unsigned int local_sym_index, unsigned int type,
1160 Output_data* od, Address address, Addend addend)
1162 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1163 addend, true));
1166 void
1167 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1168 unsigned int local_sym_index, unsigned int type,
1169 Output_data* od, unsigned int shndx, Address address,
1170 Addend addend)
1172 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1173 address, addend, true));
1176 // A reloc against the STT_SECTION symbol of an output section.
1178 void
1179 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1180 Address address, Addend addend)
1181 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1183 void
1184 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1185 unsigned int shndx, Address address, Addend addend)
1186 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1187 addend)); }
1190 // Output_relocatable_relocs represents a relocation section in a
1191 // relocatable link. The actual data is written out in the target
1192 // hook relocate_for_relocatable. This just saves space for it.
1194 template<int sh_type, int size, bool big_endian>
1195 class Output_relocatable_relocs : public Output_section_data
1197 public:
1198 Output_relocatable_relocs(Relocatable_relocs* rr)
1199 : Output_section_data(Output_data::default_alignment_for_size(size)),
1200 rr_(rr)
1203 void
1204 set_final_data_size();
1206 // Write out the data. There is nothing to do here.
1207 void
1208 do_write(Output_file*)
1211 private:
1212 // The relocs associated with this input section.
1213 Relocatable_relocs* rr_;
1216 // Handle a GROUP section.
1218 template<int size, bool big_endian>
1219 class Output_data_group : public Output_section_data
1221 public:
1222 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1223 section_size_type entry_count,
1224 const elfcpp::Elf_Word* contents);
1226 void
1227 do_write(Output_file*);
1229 private:
1230 // The input object.
1231 Sized_relobj<size, big_endian>* relobj_;
1232 // The group flag word.
1233 elfcpp::Elf_Word flags_;
1234 // The section indexes of the input sections in this group.
1235 std::vector<unsigned int> input_sections_;
1238 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1239 // for one symbol--either a global symbol or a local symbol in an
1240 // object. The target specific code adds entries to the GOT as
1241 // needed.
1243 template<int size, bool big_endian>
1244 class Output_data_got : public Output_section_data_build
1246 public:
1247 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1248 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1249 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1251 Output_data_got()
1252 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1253 entries_()
1256 // Add an entry for a global symbol to the GOT. Return true if this
1257 // is a new GOT entry, false if the symbol was already in the GOT.
1258 bool
1259 add_global(Symbol* gsym);
1261 // Add an entry for a global symbol to the GOT, and add a dynamic
1262 // relocation of type R_TYPE for the GOT entry.
1263 void
1264 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1266 void
1267 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1269 // Add an entry for a local symbol to the GOT. This returns true if
1270 // this is a new GOT entry, false if the symbol already has a GOT
1271 // entry.
1272 bool
1273 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1275 // Add an entry for a global symbol to the GOT, and add a dynamic
1276 // relocation of type R_TYPE for the GOT entry.
1277 void
1278 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1279 unsigned int sym_index, Rel_dyn* rel_dyn,
1280 unsigned int r_type);
1282 void
1283 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1284 unsigned int sym_index, Rela_dyn* rela_dyn,
1285 unsigned int r_type);
1287 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1288 // Return true if this is a new GOT entry, false if the symbol was
1289 // already in the GOT.
1290 bool
1291 add_global_tls(Symbol* gsym, bool need_pair);
1293 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1294 // relocation of type R_TYPE.
1295 void
1296 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1297 unsigned int r_type);
1299 void
1300 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1301 unsigned int r_type);
1303 // Add a pair of entries for a global TLS symbol to the GOT, and add
1304 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1305 void
1306 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1307 unsigned int mod_r_type,
1308 unsigned int dtv_r_type);
1310 void
1311 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1312 unsigned int mod_r_type,
1313 unsigned int dtv_r_type);
1315 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1316 // This returns true if this is a new GOT entry, false if the symbol
1317 // already has a GOT entry.
1318 bool
1319 add_local_tls(Sized_relobj<size, big_endian>* object,
1320 unsigned int sym_index, bool need_pair);
1322 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1323 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1324 // Because this is a local symbol, the first GOT entry can be relocated
1325 // relative to a section symbol, and the second GOT entry will have an
1326 // dtv-relative value that can be computed at link time.
1327 void
1328 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1329 unsigned int sym_index, unsigned int shndx,
1330 bool need_pair, Rel_dyn* rel_dyn,
1331 unsigned int r_type);
1333 void
1334 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1335 unsigned int sym_index, unsigned int shndx,
1336 bool need_pair, Rela_dyn* rela_dyn,
1337 unsigned int r_type);
1339 // Add a constant to the GOT. This returns the offset of the new
1340 // entry from the start of the GOT.
1341 unsigned int
1342 add_constant(Valtype constant)
1344 this->entries_.push_back(Got_entry(constant));
1345 this->set_got_size();
1346 return this->last_got_offset();
1349 protected:
1350 // Write out the GOT table.
1351 void
1352 do_write(Output_file*);
1354 private:
1355 // This POD class holds a single GOT entry.
1356 class Got_entry
1358 public:
1359 // Create a zero entry.
1360 Got_entry()
1361 : local_sym_index_(CONSTANT_CODE)
1362 { this->u_.constant = 0; }
1364 // Create a global symbol entry.
1365 explicit Got_entry(Symbol* gsym)
1366 : local_sym_index_(GSYM_CODE)
1367 { this->u_.gsym = gsym; }
1369 // Create a local symbol entry.
1370 Got_entry(Sized_relobj<size, big_endian>* object,
1371 unsigned int local_sym_index)
1372 : local_sym_index_(local_sym_index)
1374 gold_assert(local_sym_index != GSYM_CODE
1375 && local_sym_index != CONSTANT_CODE);
1376 this->u_.object = object;
1379 // Create a constant entry. The constant is a host value--it will
1380 // be swapped, if necessary, when it is written out.
1381 explicit Got_entry(Valtype constant)
1382 : local_sym_index_(CONSTANT_CODE)
1383 { this->u_.constant = constant; }
1385 // Write the GOT entry to an output view.
1386 void
1387 write(unsigned char* pov) const;
1389 private:
1390 enum
1392 GSYM_CODE = -1U,
1393 CONSTANT_CODE = -2U
1396 union
1398 // For a local symbol, the object.
1399 Sized_relobj<size, big_endian>* object;
1400 // For a global symbol, the symbol.
1401 Symbol* gsym;
1402 // For a constant, the constant.
1403 Valtype constant;
1404 } u_;
1405 // For a local symbol, the local symbol index. This is GSYM_CODE
1406 // for a global symbol, or CONSTANT_CODE for a constant.
1407 unsigned int local_sym_index_;
1410 typedef std::vector<Got_entry> Got_entries;
1412 // Return the offset into the GOT of GOT entry I.
1413 unsigned int
1414 got_offset(unsigned int i) const
1415 { return i * (size / 8); }
1417 // Return the offset into the GOT of the last entry added.
1418 unsigned int
1419 last_got_offset() const
1420 { return this->got_offset(this->entries_.size() - 1); }
1422 // Set the size of the section.
1423 void
1424 set_got_size()
1425 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1427 // The list of GOT entries.
1428 Got_entries entries_;
1431 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1432 // section.
1434 class Output_data_dynamic : public Output_section_data
1436 public:
1437 Output_data_dynamic(Stringpool* pool)
1438 : Output_section_data(Output_data::default_alignment()),
1439 entries_(), pool_(pool)
1442 // Add a new dynamic entry with a fixed numeric value.
1443 void
1444 add_constant(elfcpp::DT tag, unsigned int val)
1445 { this->add_entry(Dynamic_entry(tag, val)); }
1447 // Add a new dynamic entry with the address of output data.
1448 void
1449 add_section_address(elfcpp::DT tag, const Output_data* od)
1450 { this->add_entry(Dynamic_entry(tag, od, false)); }
1452 // Add a new dynamic entry with the size of output data.
1453 void
1454 add_section_size(elfcpp::DT tag, const Output_data* od)
1455 { this->add_entry(Dynamic_entry(tag, od, true)); }
1457 // Add a new dynamic entry with the address of a symbol.
1458 void
1459 add_symbol(elfcpp::DT tag, const Symbol* sym)
1460 { this->add_entry(Dynamic_entry(tag, sym)); }
1462 // Add a new dynamic entry with a string.
1463 void
1464 add_string(elfcpp::DT tag, const char* str)
1465 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1467 void
1468 add_string(elfcpp::DT tag, const std::string& str)
1469 { this->add_string(tag, str.c_str()); }
1471 protected:
1472 // Adjust the output section to set the entry size.
1473 void
1474 do_adjust_output_section(Output_section*);
1476 // Set the final data size.
1477 void
1478 set_final_data_size();
1480 // Write out the dynamic entries.
1481 void
1482 do_write(Output_file*);
1484 private:
1485 // This POD class holds a single dynamic entry.
1486 class Dynamic_entry
1488 public:
1489 // Create an entry with a fixed numeric value.
1490 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1491 : tag_(tag), classification_(DYNAMIC_NUMBER)
1492 { this->u_.val = val; }
1494 // Create an entry with the size or address of a section.
1495 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1496 : tag_(tag),
1497 classification_(section_size
1498 ? DYNAMIC_SECTION_SIZE
1499 : DYNAMIC_SECTION_ADDRESS)
1500 { this->u_.od = od; }
1502 // Create an entry with the address of a symbol.
1503 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1504 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1505 { this->u_.sym = sym; }
1507 // Create an entry with a string.
1508 Dynamic_entry(elfcpp::DT tag, const char* str)
1509 : tag_(tag), classification_(DYNAMIC_STRING)
1510 { this->u_.str = str; }
1512 // Write the dynamic entry to an output view.
1513 template<int size, bool big_endian>
1514 void
1515 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1517 private:
1518 enum Classification
1520 // Number.
1521 DYNAMIC_NUMBER,
1522 // Section address.
1523 DYNAMIC_SECTION_ADDRESS,
1524 // Section size.
1525 DYNAMIC_SECTION_SIZE,
1526 // Symbol adress.
1527 DYNAMIC_SYMBOL,
1528 // String.
1529 DYNAMIC_STRING
1532 union
1534 // For DYNAMIC_NUMBER.
1535 unsigned int val;
1536 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1537 const Output_data* od;
1538 // For DYNAMIC_SYMBOL.
1539 const Symbol* sym;
1540 // For DYNAMIC_STRING.
1541 const char* str;
1542 } u_;
1543 // The dynamic tag.
1544 elfcpp::DT tag_;
1545 // The type of entry.
1546 Classification classification_;
1549 // Add an entry to the list.
1550 void
1551 add_entry(const Dynamic_entry& entry)
1552 { this->entries_.push_back(entry); }
1554 // Sized version of write function.
1555 template<int size, bool big_endian>
1556 void
1557 sized_write(Output_file* of);
1559 // The type of the list of entries.
1560 typedef std::vector<Dynamic_entry> Dynamic_entries;
1562 // The entries.
1563 Dynamic_entries entries_;
1564 // The pool used for strings.
1565 Stringpool* pool_;
1568 // An output section. We don't expect to have too many output
1569 // sections, so we don't bother to do a template on the size.
1571 class Output_section : public Output_data
1573 public:
1574 // Create an output section, giving the name, type, and flags.
1575 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1576 virtual ~Output_section();
1578 // Add a new input section SHNDX, named NAME, with header SHDR, from
1579 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1580 // which applies to this section, or 0 if none, or -1U if more than
1581 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1582 // in a linker script; in that case we need to keep track of input
1583 // sections associated with an output section. Return the offset
1584 // within the output section.
1585 template<int size, bool big_endian>
1586 off_t
1587 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1588 const char *name,
1589 const elfcpp::Shdr<size, big_endian>& shdr,
1590 unsigned int reloc_shndx, bool have_sections_script);
1592 // Add generated data POSD to this output section.
1593 void
1594 add_output_section_data(Output_section_data* posd);
1596 // Return the section name.
1597 const char*
1598 name() const
1599 { return this->name_; }
1601 // Return the section type.
1602 elfcpp::Elf_Word
1603 type() const
1604 { return this->type_; }
1606 // Return the section flags.
1607 elfcpp::Elf_Xword
1608 flags() const
1609 { return this->flags_; }
1611 // Return the entsize field.
1612 uint64_t
1613 entsize() const
1614 { return this->entsize_; }
1616 // Set the entsize field.
1617 void
1618 set_entsize(uint64_t v);
1620 // Set the load address.
1621 void
1622 set_load_address(uint64_t load_address)
1624 this->load_address_ = load_address;
1625 this->has_load_address_ = true;
1628 // Set the link field to the output section index of a section.
1629 void
1630 set_link_section(const Output_data* od)
1632 gold_assert(this->link_ == 0
1633 && !this->should_link_to_symtab_
1634 && !this->should_link_to_dynsym_);
1635 this->link_section_ = od;
1638 // Set the link field to a constant.
1639 void
1640 set_link(unsigned int v)
1642 gold_assert(this->link_section_ == NULL
1643 && !this->should_link_to_symtab_
1644 && !this->should_link_to_dynsym_);
1645 this->link_ = v;
1648 // Record that this section should link to the normal symbol table.
1649 void
1650 set_should_link_to_symtab()
1652 gold_assert(this->link_section_ == NULL
1653 && this->link_ == 0
1654 && !this->should_link_to_dynsym_);
1655 this->should_link_to_symtab_ = true;
1658 // Record that this section should link to the dynamic symbol table.
1659 void
1660 set_should_link_to_dynsym()
1662 gold_assert(this->link_section_ == NULL
1663 && this->link_ == 0
1664 && !this->should_link_to_symtab_);
1665 this->should_link_to_dynsym_ = true;
1668 // Return the info field.
1669 unsigned int
1670 info() const
1672 gold_assert(this->info_section_ == NULL
1673 && this->info_symndx_ == NULL);
1674 return this->info_;
1677 // Set the info field to the output section index of a section.
1678 void
1679 set_info_section(const Output_section* os)
1681 gold_assert((this->info_section_ == NULL
1682 || (this->info_section_ == os
1683 && this->info_uses_section_index_))
1684 && this->info_symndx_ == NULL
1685 && this->info_ == 0);
1686 this->info_section_ = os;
1687 this->info_uses_section_index_= true;
1690 // Set the info field to the symbol table index of a symbol.
1691 void
1692 set_info_symndx(const Symbol* sym)
1694 gold_assert(this->info_section_ == NULL
1695 && (this->info_symndx_ == NULL
1696 || this->info_symndx_ == sym)
1697 && this->info_ == 0);
1698 this->info_symndx_ = sym;
1701 // Set the info field to the symbol table index of a section symbol.
1702 void
1703 set_info_section_symndx(const Output_section* os)
1705 gold_assert((this->info_section_ == NULL
1706 || (this->info_section_ == os
1707 && !this->info_uses_section_index_))
1708 && this->info_symndx_ == NULL
1709 && this->info_ == 0);
1710 this->info_section_ = os;
1711 this->info_uses_section_index_ = false;
1714 // Set the info field to a constant.
1715 void
1716 set_info(unsigned int v)
1718 gold_assert(this->info_section_ == NULL
1719 && this->info_symndx_ == NULL
1720 && (this->info_ == 0
1721 || this->info_ == v));
1722 this->info_ = v;
1725 // Set the addralign field.
1726 void
1727 set_addralign(uint64_t v)
1728 { this->addralign_ = v; }
1730 // Indicate that we need a symtab index.
1731 void
1732 set_needs_symtab_index()
1733 { this->needs_symtab_index_ = true; }
1735 // Return whether we need a symtab index.
1736 bool
1737 needs_symtab_index() const
1738 { return this->needs_symtab_index_; }
1740 // Get the symtab index.
1741 unsigned int
1742 symtab_index() const
1744 gold_assert(this->symtab_index_ != 0);
1745 return this->symtab_index_;
1748 // Set the symtab index.
1749 void
1750 set_symtab_index(unsigned int index)
1752 gold_assert(index != 0);
1753 this->symtab_index_ = index;
1756 // Indicate that we need a dynsym index.
1757 void
1758 set_needs_dynsym_index()
1759 { this->needs_dynsym_index_ = true; }
1761 // Return whether we need a dynsym index.
1762 bool
1763 needs_dynsym_index() const
1764 { return this->needs_dynsym_index_; }
1766 // Get the dynsym index.
1767 unsigned int
1768 dynsym_index() const
1770 gold_assert(this->dynsym_index_ != 0);
1771 return this->dynsym_index_;
1774 // Set the dynsym index.
1775 void
1776 set_dynsym_index(unsigned int index)
1778 gold_assert(index != 0);
1779 this->dynsym_index_ = index;
1782 // Return whether this section should be written after all the input
1783 // sections are complete.
1784 bool
1785 after_input_sections() const
1786 { return this->after_input_sections_; }
1788 // Record that this section should be written after all the input
1789 // sections are complete.
1790 void
1791 set_after_input_sections()
1792 { this->after_input_sections_ = true; }
1794 // Return whether this section requires postprocessing after all
1795 // relocations have been applied.
1796 bool
1797 requires_postprocessing() const
1798 { return this->requires_postprocessing_; }
1800 // If a section requires postprocessing, return the buffer to use.
1801 unsigned char*
1802 postprocessing_buffer() const
1804 gold_assert(this->postprocessing_buffer_ != NULL);
1805 return this->postprocessing_buffer_;
1808 // If a section requires postprocessing, create the buffer to use.
1809 void
1810 create_postprocessing_buffer();
1812 // If a section requires postprocessing, this is the size of the
1813 // buffer to which relocations should be applied.
1814 off_t
1815 postprocessing_buffer_size() const
1816 { return this->current_data_size_for_child(); }
1818 // Modify the section name. This is only permitted for an
1819 // unallocated section, and only before the size has been finalized.
1820 // Otherwise the name will not get into Layout::namepool_.
1821 void
1822 set_name(const char* newname)
1824 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1825 gold_assert(!this->is_data_size_valid());
1826 this->name_ = newname;
1829 // Return whether the offset OFFSET in the input section SHNDX in
1830 // object OBJECT is being included in the link.
1831 bool
1832 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1833 off_t offset) const;
1835 // Return the offset within the output section of OFFSET relative to
1836 // the start of input section SHNDX in object OBJECT.
1837 section_offset_type
1838 output_offset(const Relobj* object, unsigned int shndx,
1839 section_offset_type offset) const;
1841 // Return the output virtual address of OFFSET relative to the start
1842 // of input section SHNDX in object OBJECT.
1843 uint64_t
1844 output_address(const Relobj* object, unsigned int shndx,
1845 off_t offset) const;
1847 // Return the output address of the start of the merged section for
1848 // input section SHNDX in object OBJECT. This is not necessarily
1849 // the offset corresponding to input offset 0 in the section, since
1850 // the section may be mapped arbitrarily.
1851 uint64_t
1852 starting_output_address(const Relobj* object, unsigned int shndx) const;
1854 // Record that this output section was found in the SECTIONS clause
1855 // of a linker script.
1856 void
1857 set_found_in_sections_clause()
1858 { this->found_in_sections_clause_ = true; }
1860 // Return whether this output section was found in the SECTIONS
1861 // clause of a linker script.
1862 bool
1863 found_in_sections_clause() const
1864 { return this->found_in_sections_clause_; }
1866 // Write the section header into *OPHDR.
1867 template<int size, bool big_endian>
1868 void
1869 write_header(const Layout*, const Stringpool*,
1870 elfcpp::Shdr_write<size, big_endian>*) const;
1872 // The next few calls are for linker script support.
1874 // Store the list of input sections for this Output_section into the
1875 // list passed in. This removes the input sections, leaving only
1876 // any Output_section_data elements. This returns the size of those
1877 // Output_section_data elements. ADDRESS is the address of this
1878 // output section. FILL is the fill value to use, in case there are
1879 // any spaces between the remaining Output_section_data elements.
1880 uint64_t
1881 get_input_sections(uint64_t address, const std::string& fill,
1882 std::list<std::pair<Relobj*, unsigned int > >*);
1884 // Add an input section from a script.
1885 void
1886 add_input_section_for_script(Relobj* object, unsigned int shndx,
1887 off_t data_size, uint64_t addralign);
1889 // Set the current size of the output section.
1890 void
1891 set_current_data_size(off_t size)
1892 { this->set_current_data_size_for_child(size); }
1894 // Get the current size of the output section.
1895 off_t
1896 current_data_size() const
1897 { return this->current_data_size_for_child(); }
1899 // End of linker script support.
1901 // Print merge statistics to stderr.
1902 void
1903 print_merge_stats();
1905 protected:
1906 // Return the section index in the output file.
1907 unsigned int
1908 do_out_shndx() const
1910 gold_assert(this->out_shndx_ != -1U);
1911 return this->out_shndx_;
1914 // Set the output section index.
1915 void
1916 do_set_out_shndx(unsigned int shndx)
1918 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
1919 this->out_shndx_ = shndx;
1922 // Set the final data size of the Output_section. For a typical
1923 // Output_section, there is nothing to do, but if there are any
1924 // Output_section_data objects we need to set their final addresses
1925 // here.
1926 virtual void
1927 set_final_data_size();
1929 // Reset the address and file offset.
1930 void
1931 do_reset_address_and_file_offset();
1933 // Write the data to the file. For a typical Output_section, this
1934 // does nothing: the data is written out by calling Object::Relocate
1935 // on each input object. But if there are any Output_section_data
1936 // objects we do need to write them out here.
1937 virtual void
1938 do_write(Output_file*);
1940 // Return the address alignment--function required by parent class.
1941 uint64_t
1942 do_addralign() const
1943 { return this->addralign_; }
1945 // Return whether there is a load address.
1946 bool
1947 do_has_load_address() const
1948 { return this->has_load_address_; }
1950 // Return the load address.
1951 uint64_t
1952 do_load_address() const
1954 gold_assert(this->has_load_address_);
1955 return this->load_address_;
1958 // Return whether this is an Output_section.
1959 bool
1960 do_is_section() const
1961 { return true; }
1963 // Return whether this is a section of the specified type.
1964 bool
1965 do_is_section_type(elfcpp::Elf_Word type) const
1966 { return this->type_ == type; }
1968 // Return whether the specified section flag is set.
1969 bool
1970 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1971 { return (this->flags_ & flag) != 0; }
1973 // Set the TLS offset. Called only for SHT_TLS sections.
1974 void
1975 do_set_tls_offset(uint64_t tls_base);
1977 // Return the TLS offset, relative to the base of the TLS segment.
1978 // Valid only for SHT_TLS sections.
1979 uint64_t
1980 do_tls_offset() const
1981 { return this->tls_offset_; }
1983 // This may be implemented by a child class.
1984 virtual void
1985 do_finalize_name(Layout*)
1988 // Record that this section requires postprocessing after all
1989 // relocations have been applied. This is called by a child class.
1990 void
1991 set_requires_postprocessing()
1993 this->requires_postprocessing_ = true;
1994 this->after_input_sections_ = true;
1997 // Write all the data of an Output_section into the postprocessing
1998 // buffer.
1999 void
2000 write_to_postprocessing_buffer();
2002 private:
2003 // In some cases we need to keep a list of the input sections
2004 // associated with this output section. We only need the list if we
2005 // might have to change the offsets of the input section within the
2006 // output section after we add the input section. The ordinary
2007 // input sections will be written out when we process the object
2008 // file, and as such we don't need to track them here. We do need
2009 // to track Output_section_data objects here. We store instances of
2010 // this structure in a std::vector, so it must be a POD. There can
2011 // be many instances of this structure, so we use a union to save
2012 // some space.
2013 class Input_section
2015 public:
2016 Input_section()
2017 : shndx_(0), p2align_(0)
2019 this->u1_.data_size = 0;
2020 this->u2_.object = NULL;
2023 // For an ordinary input section.
2024 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2025 uint64_t addralign)
2026 : shndx_(shndx),
2027 p2align_(ffsll(static_cast<long long>(addralign)))
2029 gold_assert(shndx != OUTPUT_SECTION_CODE
2030 && shndx != MERGE_DATA_SECTION_CODE
2031 && shndx != MERGE_STRING_SECTION_CODE);
2032 this->u1_.data_size = data_size;
2033 this->u2_.object = object;
2036 // For a non-merge output section.
2037 Input_section(Output_section_data* posd)
2038 : shndx_(OUTPUT_SECTION_CODE),
2039 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2041 this->u1_.data_size = 0;
2042 this->u2_.posd = posd;
2045 // For a merge section.
2046 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2047 : shndx_(is_string
2048 ? MERGE_STRING_SECTION_CODE
2049 : MERGE_DATA_SECTION_CODE),
2050 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2052 this->u1_.entsize = entsize;
2053 this->u2_.posd = posd;
2056 // The required alignment.
2057 uint64_t
2058 addralign() const
2060 return (this->p2align_ == 0
2062 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2065 // Return the required size.
2066 off_t
2067 data_size() const;
2069 // Whether this is an input section.
2070 bool
2071 is_input_section() const
2073 return (this->shndx_ != OUTPUT_SECTION_CODE
2074 && this->shndx_ != MERGE_DATA_SECTION_CODE
2075 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2078 // Return whether this is a merge section which matches the
2079 // parameters.
2080 bool
2081 is_merge_section(bool is_string, uint64_t entsize,
2082 uint64_t addralign) const
2084 return (this->shndx_ == (is_string
2085 ? MERGE_STRING_SECTION_CODE
2086 : MERGE_DATA_SECTION_CODE)
2087 && this->u1_.entsize == entsize
2088 && this->addralign() == addralign);
2091 // Return the object for an input section.
2092 Relobj*
2093 relobj() const
2095 gold_assert(this->is_input_section());
2096 return this->u2_.object;
2099 // Return the input section index for an input section.
2100 unsigned int
2101 shndx() const
2103 gold_assert(this->is_input_section());
2104 return this->shndx_;
2107 // Set the output section.
2108 void
2109 set_output_section(Output_section* os)
2111 gold_assert(!this->is_input_section());
2112 this->u2_.posd->set_output_section(os);
2115 // Set the address and file offset. This is called during
2116 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2117 // the enclosing section.
2118 void
2119 set_address_and_file_offset(uint64_t address, off_t file_offset,
2120 off_t section_file_offset);
2122 // Reset the address and file offset.
2123 void
2124 reset_address_and_file_offset();
2126 // Finalize the data size.
2127 void
2128 finalize_data_size();
2130 // Add an input section, for SHF_MERGE sections.
2131 bool
2132 add_input_section(Relobj* object, unsigned int shndx)
2134 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2135 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2136 return this->u2_.posd->add_input_section(object, shndx);
2139 // Given an input OBJECT, an input section index SHNDX within that
2140 // object, and an OFFSET relative to the start of that input
2141 // section, return whether or not the output offset is known. If
2142 // this function returns true, it sets *POUTPUT to the offset in
2143 // the output section, relative to the start of the input section
2144 // in the output section. *POUTPUT may be different from OFFSET
2145 // for a merged section.
2146 bool
2147 output_offset(const Relobj* object, unsigned int shndx,
2148 section_offset_type offset,
2149 section_offset_type *poutput) const;
2151 // Return whether this is the merge section for the input section
2152 // SHNDX in OBJECT.
2153 bool
2154 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2156 // Write out the data. This does nothing for an input section.
2157 void
2158 write(Output_file*);
2160 // Write the data to a buffer. This does nothing for an input
2161 // section.
2162 void
2163 write_to_buffer(unsigned char*);
2165 // Print statistics about merge sections to stderr.
2166 void
2167 print_merge_stats(const char* section_name)
2169 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2170 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2171 this->u2_.posd->print_merge_stats(section_name);
2174 private:
2175 // Code values which appear in shndx_. If the value is not one of
2176 // these codes, it is the input section index in the object file.
2177 enum
2179 // An Output_section_data.
2180 OUTPUT_SECTION_CODE = -1U,
2181 // An Output_section_data for an SHF_MERGE section with
2182 // SHF_STRINGS not set.
2183 MERGE_DATA_SECTION_CODE = -2U,
2184 // An Output_section_data for an SHF_MERGE section with
2185 // SHF_STRINGS set.
2186 MERGE_STRING_SECTION_CODE = -3U
2189 // For an ordinary input section, this is the section index in the
2190 // input file. For an Output_section_data, this is
2191 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2192 // MERGE_STRING_SECTION_CODE.
2193 unsigned int shndx_;
2194 // The required alignment, stored as a power of 2.
2195 unsigned int p2align_;
2196 union
2198 // For an ordinary input section, the section size.
2199 off_t data_size;
2200 // For OUTPUT_SECTION_CODE, this is not used. For
2201 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2202 // entity size.
2203 uint64_t entsize;
2204 } u1_;
2205 union
2207 // For an ordinary input section, the object which holds the
2208 // input section.
2209 Relobj* object;
2210 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2211 // MERGE_STRING_SECTION_CODE, the data.
2212 Output_section_data* posd;
2213 } u2_;
2216 typedef std::vector<Input_section> Input_section_list;
2218 // Fill data. This is used to fill in data between input sections.
2219 // It is also used for data statements (BYTE, WORD, etc.) in linker
2220 // scripts. When we have to keep track of the input sections, we
2221 // can use an Output_data_const, but we don't want to have to keep
2222 // track of input sections just to implement fills.
2223 class Fill
2225 public:
2226 Fill(off_t section_offset, off_t length)
2227 : section_offset_(section_offset),
2228 length_(convert_to_section_size_type(length))
2231 // Return section offset.
2232 off_t
2233 section_offset() const
2234 { return this->section_offset_; }
2236 // Return fill length.
2237 section_size_type
2238 length() const
2239 { return this->length_; }
2241 private:
2242 // The offset within the output section.
2243 off_t section_offset_;
2244 // The length of the space to fill.
2245 section_size_type length_;
2248 typedef std::vector<Fill> Fill_list;
2250 // Add a new output section by Input_section.
2251 void
2252 add_output_section_data(Input_section*);
2254 // Add an SHF_MERGE input section. Returns true if the section was
2255 // handled.
2256 bool
2257 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2258 uint64_t entsize, uint64_t addralign);
2260 // Add an output SHF_MERGE section POSD to this output section.
2261 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2262 // ENTSIZE is the entity size. This returns the entry added to
2263 // input_sections_.
2264 void
2265 add_output_merge_section(Output_section_data* posd, bool is_string,
2266 uint64_t entsize);
2268 // Most of these fields are only valid after layout.
2270 // The name of the section. This will point into a Stringpool.
2271 const char* name_;
2272 // The section address is in the parent class.
2273 // The section alignment.
2274 uint64_t addralign_;
2275 // The section entry size.
2276 uint64_t entsize_;
2277 // The load address. This is only used when using a linker script
2278 // with a SECTIONS clause. The has_load_address_ field indicates
2279 // whether this field is valid.
2280 uint64_t load_address_;
2281 // The file offset is in the parent class.
2282 // Set the section link field to the index of this section.
2283 const Output_data* link_section_;
2284 // If link_section_ is NULL, this is the link field.
2285 unsigned int link_;
2286 // Set the section info field to the index of this section.
2287 const Output_section* info_section_;
2288 // If info_section_ is NULL, set the info field to the symbol table
2289 // index of this symbol.
2290 const Symbol* info_symndx_;
2291 // If info_section_ and info_symndx_ are NULL, this is the section
2292 // info field.
2293 unsigned int info_;
2294 // The section type.
2295 const elfcpp::Elf_Word type_;
2296 // The section flags.
2297 elfcpp::Elf_Xword flags_;
2298 // The section index.
2299 unsigned int out_shndx_;
2300 // If there is a STT_SECTION for this output section in the normal
2301 // symbol table, this is the symbol index. This starts out as zero.
2302 // It is initialized in Layout::finalize() to be the index, or -1U
2303 // if there isn't one.
2304 unsigned int symtab_index_;
2305 // If there is a STT_SECTION for this output section in the dynamic
2306 // symbol table, this is the symbol index. This starts out as zero.
2307 // It is initialized in Layout::finalize() to be the index, or -1U
2308 // if there isn't one.
2309 unsigned int dynsym_index_;
2310 // The input sections. This will be empty in cases where we don't
2311 // need to keep track of them.
2312 Input_section_list input_sections_;
2313 // The offset of the first entry in input_sections_.
2314 off_t first_input_offset_;
2315 // The fill data. This is separate from input_sections_ because we
2316 // often will need fill sections without needing to keep track of
2317 // input sections.
2318 Fill_list fills_;
2319 // If the section requires postprocessing, this buffer holds the
2320 // section contents during relocation.
2321 unsigned char* postprocessing_buffer_;
2322 // Whether this output section needs a STT_SECTION symbol in the
2323 // normal symbol table. This will be true if there is a relocation
2324 // which needs it.
2325 bool needs_symtab_index_ : 1;
2326 // Whether this output section needs a STT_SECTION symbol in the
2327 // dynamic symbol table. This will be true if there is a dynamic
2328 // relocation which needs it.
2329 bool needs_dynsym_index_ : 1;
2330 // Whether the link field of this output section should point to the
2331 // normal symbol table.
2332 bool should_link_to_symtab_ : 1;
2333 // Whether the link field of this output section should point to the
2334 // dynamic symbol table.
2335 bool should_link_to_dynsym_ : 1;
2336 // Whether this section should be written after all the input
2337 // sections are complete.
2338 bool after_input_sections_ : 1;
2339 // Whether this section requires post processing after all
2340 // relocations have been applied.
2341 bool requires_postprocessing_ : 1;
2342 // Whether an input section was mapped to this output section
2343 // because of a SECTIONS clause in a linker script.
2344 bool found_in_sections_clause_ : 1;
2345 // Whether this section has an explicitly specified load address.
2346 bool has_load_address_ : 1;
2347 // True if the info_section_ field means the section index of the
2348 // section, false if it means the symbol index of the corresponding
2349 // section symbol.
2350 bool info_uses_section_index_ : 1;
2351 // For SHT_TLS sections, the offset of this section relative to the base
2352 // of the TLS segment.
2353 uint64_t tls_offset_;
2356 // An output segment. PT_LOAD segments are built from collections of
2357 // output sections. Other segments typically point within PT_LOAD
2358 // segments, and are built directly as needed.
2360 class Output_segment
2362 public:
2363 // Create an output segment, specifying the type and flags.
2364 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2366 // Return the virtual address.
2367 uint64_t
2368 vaddr() const
2369 { return this->vaddr_; }
2371 // Return the physical address.
2372 uint64_t
2373 paddr() const
2374 { return this->paddr_; }
2376 // Return the segment type.
2377 elfcpp::Elf_Word
2378 type() const
2379 { return this->type_; }
2381 // Return the segment flags.
2382 elfcpp::Elf_Word
2383 flags() const
2384 { return this->flags_; }
2386 // Return the memory size.
2387 uint64_t
2388 memsz() const
2389 { return this->memsz_; }
2391 // Return the file size.
2392 off_t
2393 filesz() const
2394 { return this->filesz_; }
2396 // Return the file offset.
2397 off_t
2398 offset() const
2399 { return this->offset_; }
2401 // Return the maximum alignment of the Output_data.
2402 uint64_t
2403 maximum_alignment();
2405 // Add an Output_section to this segment.
2406 void
2407 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2408 { this->add_output_section(os, seg_flags, false); }
2410 // Add an Output_section to the start of this segment.
2411 void
2412 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2413 { this->add_output_section(os, seg_flags, true); }
2415 // Add an Output_data (which is not an Output_section) to the start
2416 // of this segment.
2417 void
2418 add_initial_output_data(Output_data*);
2420 // Return the number of dynamic relocations applied to this segment.
2421 unsigned int
2422 dynamic_reloc_count() const;
2424 // Return the address of the first section.
2425 uint64_t
2426 first_section_load_address() const;
2428 // Return whether the addresses have been set already.
2429 bool
2430 are_addresses_set() const
2431 { return this->are_addresses_set_; }
2433 // Set the addresses.
2434 void
2435 set_addresses(uint64_t vaddr, uint64_t paddr)
2437 this->vaddr_ = vaddr;
2438 this->paddr_ = paddr;
2439 this->are_addresses_set_ = true;
2442 // Set the segment flags. This is only used if we have a PHDRS
2443 // clause which explicitly specifies the flags.
2444 void
2445 set_flags(elfcpp::Elf_Word flags)
2446 { this->flags_ = flags; }
2448 // Set the address of the segment to ADDR and the offset to *POFF
2449 // and set the addresses and offsets of all contained output
2450 // sections accordingly. Set the section indexes of all contained
2451 // output sections starting with *PSHNDX. If RESET is true, first
2452 // reset the addresses of the contained sections. Return the
2453 // address of the immediately following segment. Update *POFF and
2454 // *PSHNDX. This should only be called for a PT_LOAD segment.
2455 uint64_t
2456 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2457 unsigned int* pshndx);
2459 // Set the minimum alignment of this segment. This may be adjusted
2460 // upward based on the section alignments.
2461 void
2462 set_minimum_p_align(uint64_t align)
2463 { this->min_p_align_ = align; }
2465 // Set the offset of this segment based on the section. This should
2466 // only be called for a non-PT_LOAD segment.
2467 void
2468 set_offset();
2470 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2471 void
2472 set_tls_offsets();
2474 // Return the number of output sections.
2475 unsigned int
2476 output_section_count() const;
2478 // Return the section attached to the list segment with the lowest
2479 // load address. This is used when handling a PHDRS clause in a
2480 // linker script.
2481 Output_section*
2482 section_with_lowest_load_address() const;
2484 // Write the segment header into *OPHDR.
2485 template<int size, bool big_endian>
2486 void
2487 write_header(elfcpp::Phdr_write<size, big_endian>*);
2489 // Write the section headers of associated sections into V.
2490 template<int size, bool big_endian>
2491 unsigned char*
2492 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2493 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2495 private:
2496 Output_segment(const Output_segment&);
2497 Output_segment& operator=(const Output_segment&);
2499 typedef std::list<Output_data*> Output_data_list;
2501 // Add an Output_section to this segment, specifying front or back.
2502 void
2503 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2504 bool front);
2506 // Find the maximum alignment in an Output_data_list.
2507 static uint64_t
2508 maximum_alignment_list(const Output_data_list*);
2510 // Set the section addresses in an Output_data_list.
2511 uint64_t
2512 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2513 off_t* poff, unsigned int* pshndx);
2515 // Return the number of Output_sections in an Output_data_list.
2516 unsigned int
2517 output_section_count_list(const Output_data_list*) const;
2519 // Return the number of dynamic relocs in an Output_data_list.
2520 unsigned int
2521 dynamic_reloc_count_list(const Output_data_list*) const;
2523 // Find the section with the lowest load address in an
2524 // Output_data_list.
2525 void
2526 lowest_load_address_in_list(const Output_data_list* pdl,
2527 Output_section** found,
2528 uint64_t* found_lma) const;
2530 // Write the section headers in the list into V.
2531 template<int size, bool big_endian>
2532 unsigned char*
2533 write_section_headers_list(const Layout*, const Stringpool*,
2534 const Output_data_list*, unsigned char* v,
2535 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2537 // The list of output data with contents attached to this segment.
2538 Output_data_list output_data_;
2539 // The list of output data without contents attached to this segment.
2540 Output_data_list output_bss_;
2541 // The segment virtual address.
2542 uint64_t vaddr_;
2543 // The segment physical address.
2544 uint64_t paddr_;
2545 // The size of the segment in memory.
2546 uint64_t memsz_;
2547 // The maximum section alignment. The is_max_align_known_ field
2548 // indicates whether this has been finalized.
2549 uint64_t max_align_;
2550 // The required minimum value for the p_align field. This is used
2551 // for PT_LOAD segments. Note that this does not mean that
2552 // addresses should be aligned to this value; it means the p_paddr
2553 // and p_vaddr fields must be congruent modulo this value. For
2554 // non-PT_LOAD segments, the dynamic linker works more efficiently
2555 // if the p_align field has the more conventional value, although it
2556 // can align as needed.
2557 uint64_t min_p_align_;
2558 // The offset of the segment data within the file.
2559 off_t offset_;
2560 // The size of the segment data in the file.
2561 off_t filesz_;
2562 // The segment type;
2563 elfcpp::Elf_Word type_;
2564 // The segment flags.
2565 elfcpp::Elf_Word flags_;
2566 // Whether we have finalized max_align_.
2567 bool is_max_align_known_ : 1;
2568 // Whether vaddr and paddr were set by a linker script.
2569 bool are_addresses_set_ : 1;
2572 // This class represents the output file.
2574 class Output_file
2576 public:
2577 Output_file(const char* name);
2579 // Indicate that this is a temporary file which should not be
2580 // output.
2581 void
2582 set_is_temporary()
2583 { this->is_temporary_ = true; }
2585 // Open the output file. FILE_SIZE is the final size of the file.
2586 void
2587 open(off_t file_size);
2589 // Resize the output file.
2590 void
2591 resize(off_t file_size);
2593 // Close the output file (flushing all buffered data) and make sure
2594 // there are no errors.
2595 void
2596 close();
2598 // We currently always use mmap which makes the view handling quite
2599 // simple. In the future we may support other approaches.
2601 // Write data to the output file.
2602 void
2603 write(off_t offset, const void* data, size_t len)
2604 { memcpy(this->base_ + offset, data, len); }
2606 // Get a buffer to use to write to the file, given the offset into
2607 // the file and the size.
2608 unsigned char*
2609 get_output_view(off_t start, size_t size)
2611 gold_assert(start >= 0
2612 && start + static_cast<off_t>(size) <= this->file_size_);
2613 return this->base_ + start;
2616 // VIEW must have been returned by get_output_view. Write the
2617 // buffer to the file, passing in the offset and the size.
2618 void
2619 write_output_view(off_t, size_t, unsigned char*)
2622 // Get a read/write buffer. This is used when we want to write part
2623 // of the file, read it in, and write it again.
2624 unsigned char*
2625 get_input_output_view(off_t start, size_t size)
2626 { return this->get_output_view(start, size); }
2628 // Write a read/write buffer back to the file.
2629 void
2630 write_input_output_view(off_t, size_t, unsigned char*)
2633 // Get a read buffer. This is used when we just want to read part
2634 // of the file back it in.
2635 const unsigned char*
2636 get_input_view(off_t start, size_t size)
2637 { return this->get_output_view(start, size); }
2639 // Release a read bfufer.
2640 void
2641 free_input_view(off_t, size_t, const unsigned char*)
2644 private:
2645 // Map the file into memory and return a pointer to the map.
2646 void
2647 map();
2649 // Unmap the file from memory (and flush to disk buffers).
2650 void
2651 unmap();
2653 // File name.
2654 const char* name_;
2655 // File descriptor.
2656 int o_;
2657 // File size.
2658 off_t file_size_;
2659 // Base of file mapped into memory.
2660 unsigned char* base_;
2661 // True iff base_ points to a memory buffer rather than an output file.
2662 bool map_is_anonymous_;
2663 // True if this is a temporary file which should not be output.
2664 bool is_temporary_;
2667 } // End namespace gold.
2669 #endif // !defined(GOLD_OUTPUT_H)