Replace VEX.DNS with VEX.NDS in comments.
[binutils.git] / gold / output.h
blob7a356526cb61e9232e15a893632eee631b730867
1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
26 #include <list>
27 #include <vector>
29 #include "elfcpp.h"
30 #include "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
34 namespace gold
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_merge_base;
42 class Output_section;
43 class Relocatable_relocs;
44 class Target;
45 template<int size, bool big_endian>
46 class Sized_target;
47 template<int size, bool big_endian>
48 class Sized_relobj;
50 // This class specifies an input section. It is used as a key type
51 // for maps.
53 class Input_section_specifier
55 public:
56 Input_section_specifier(const Relobj* relobj, unsigned int shndx)
57 : relobj_(relobj), shndx_(shndx)
58 { }
60 // Return Relobj of this.
61 const Relobj*
62 relobj() const
63 { return this->relobj_; }
65 // Return section index of this.
66 unsigned int
67 shndx() const
68 { return this->shndx_; }
70 // Whether this equals to another specifier ISS.
71 bool
72 eq(const Input_section_specifier& iss) const
73 { return this->relobj_ == iss.relobj_ && this->shndx_ == iss.shndx_; }
75 // Compute a hash value of this.
76 size_t
77 hash_value() const
79 return (gold::string_hash<char>(this->relobj_->name().c_str())
80 ^ this->shndx_);
83 // Functors for containers.
84 struct equal_to
86 bool
87 operator()(const Input_section_specifier& iss1,
88 const Input_section_specifier& iss2) const
89 { return iss1.eq(iss2); }
92 struct hash
94 size_t
95 operator()(const Input_section_specifier& iss) const
96 { return iss.hash_value(); }
99 private:
100 // An object.
101 const Relobj* relobj_;
102 // A section index.
103 unsigned int shndx_;
106 // An abtract class for data which has to go into the output file.
108 class Output_data
110 public:
111 explicit Output_data()
112 : address_(0), data_size_(0), offset_(-1),
113 is_address_valid_(false), is_data_size_valid_(false),
114 is_offset_valid_(false), is_data_size_fixed_(false),
115 dynamic_reloc_count_(0)
118 virtual
119 ~Output_data();
121 // Return the address. For allocated sections, this is only valid
122 // after Layout::finalize is finished.
123 uint64_t
124 address() const
126 gold_assert(this->is_address_valid_);
127 return this->address_;
130 // Return the size of the data. For allocated sections, this must
131 // be valid after Layout::finalize calls set_address, but need not
132 // be valid before then.
133 off_t
134 data_size() const
136 gold_assert(this->is_data_size_valid_);
137 return this->data_size_;
140 // Return true if data size is fixed.
141 bool
142 is_data_size_fixed() const
143 { return this->is_data_size_fixed_; }
145 // Return the file offset. This is only valid after
146 // Layout::finalize is finished. For some non-allocated sections,
147 // it may not be valid until near the end of the link.
148 off_t
149 offset() const
151 gold_assert(this->is_offset_valid_);
152 return this->offset_;
155 // Reset the address and file offset. This essentially disables the
156 // sanity testing about duplicate and unknown settings.
157 void
158 reset_address_and_file_offset()
160 this->is_address_valid_ = false;
161 this->is_offset_valid_ = false;
162 if (!this->is_data_size_fixed_)
163 this->is_data_size_valid_ = false;
164 this->do_reset_address_and_file_offset();
167 // Return true if address and file offset already have reset values. In
168 // other words, calling reset_address_and_file_offset will not change them.
169 bool
170 address_and_file_offset_have_reset_values() const
171 { return this->do_address_and_file_offset_have_reset_values(); }
173 // Return the required alignment.
174 uint64_t
175 addralign() const
176 { return this->do_addralign(); }
178 // Return whether this has a load address.
179 bool
180 has_load_address() const
181 { return this->do_has_load_address(); }
183 // Return the load address.
184 uint64_t
185 load_address() const
186 { return this->do_load_address(); }
188 // Return whether this is an Output_section.
189 bool
190 is_section() const
191 { return this->do_is_section(); }
193 // Return whether this is an Output_section of the specified type.
194 bool
195 is_section_type(elfcpp::Elf_Word stt) const
196 { return this->do_is_section_type(stt); }
198 // Return whether this is an Output_section with the specified flag
199 // set.
200 bool
201 is_section_flag_set(elfcpp::Elf_Xword shf) const
202 { return this->do_is_section_flag_set(shf); }
204 // Return the output section that this goes in, if there is one.
205 Output_section*
206 output_section()
207 { return this->do_output_section(); }
209 const Output_section*
210 output_section() const
211 { return this->do_output_section(); }
213 // Return the output section index, if there is an output section.
214 unsigned int
215 out_shndx() const
216 { return this->do_out_shndx(); }
218 // Set the output section index, if this is an output section.
219 void
220 set_out_shndx(unsigned int shndx)
221 { this->do_set_out_shndx(shndx); }
223 // Set the address and file offset of this data, and finalize the
224 // size of the data. This is called during Layout::finalize for
225 // allocated sections.
226 void
227 set_address_and_file_offset(uint64_t addr, off_t off)
229 this->set_address(addr);
230 this->set_file_offset(off);
231 this->finalize_data_size();
234 // Set the address.
235 void
236 set_address(uint64_t addr)
238 gold_assert(!this->is_address_valid_);
239 this->address_ = addr;
240 this->is_address_valid_ = true;
243 // Set the file offset.
244 void
245 set_file_offset(off_t off)
247 gold_assert(!this->is_offset_valid_);
248 this->offset_ = off;
249 this->is_offset_valid_ = true;
252 // Finalize the data size.
253 void
254 finalize_data_size()
256 if (!this->is_data_size_valid_)
258 // Tell the child class to set the data size.
259 this->set_final_data_size();
260 gold_assert(this->is_data_size_valid_);
264 // Set the TLS offset. Called only for SHT_TLS sections.
265 void
266 set_tls_offset(uint64_t tls_base)
267 { this->do_set_tls_offset(tls_base); }
269 // Return the TLS offset, relative to the base of the TLS segment.
270 // Valid only for SHT_TLS sections.
271 uint64_t
272 tls_offset() const
273 { return this->do_tls_offset(); }
275 // Write the data to the output file. This is called after
276 // Layout::finalize is complete.
277 void
278 write(Output_file* file)
279 { this->do_write(file); }
281 // This is called by Layout::finalize to note that the sizes of
282 // allocated sections must now be fixed.
283 static void
284 layout_complete()
285 { Output_data::allocated_sizes_are_fixed = true; }
287 // Used to check that layout has been done.
288 static bool
289 is_layout_complete()
290 { return Output_data::allocated_sizes_are_fixed; }
292 // Count the number of dynamic relocations applied to this section.
293 void
294 add_dynamic_reloc()
295 { ++this->dynamic_reloc_count_; }
297 // Return the number of dynamic relocations applied to this section.
298 unsigned int
299 dynamic_reloc_count() const
300 { return this->dynamic_reloc_count_; }
302 // Whether the address is valid.
303 bool
304 is_address_valid() const
305 { return this->is_address_valid_; }
307 // Whether the file offset is valid.
308 bool
309 is_offset_valid() const
310 { return this->is_offset_valid_; }
312 // Whether the data size is valid.
313 bool
314 is_data_size_valid() const
315 { return this->is_data_size_valid_; }
317 // Print information to the map file.
318 void
319 print_to_mapfile(Mapfile* mapfile) const
320 { return this->do_print_to_mapfile(mapfile); }
322 protected:
323 // Functions that child classes may or in some cases must implement.
325 // Write the data to the output file.
326 virtual void
327 do_write(Output_file*) = 0;
329 // Return the required alignment.
330 virtual uint64_t
331 do_addralign() const = 0;
333 // Return whether this has a load address.
334 virtual bool
335 do_has_load_address() const
336 { return false; }
338 // Return the load address.
339 virtual uint64_t
340 do_load_address() const
341 { gold_unreachable(); }
343 // Return whether this is an Output_section.
344 virtual bool
345 do_is_section() const
346 { return false; }
348 // Return whether this is an Output_section of the specified type.
349 // This only needs to be implement by Output_section.
350 virtual bool
351 do_is_section_type(elfcpp::Elf_Word) const
352 { return false; }
354 // Return whether this is an Output_section with the specific flag
355 // set. This only needs to be implemented by Output_section.
356 virtual bool
357 do_is_section_flag_set(elfcpp::Elf_Xword) const
358 { return false; }
360 // Return the output section, if there is one.
361 virtual Output_section*
362 do_output_section()
363 { return NULL; }
365 virtual const Output_section*
366 do_output_section() const
367 { return NULL; }
369 // Return the output section index, if there is an output section.
370 virtual unsigned int
371 do_out_shndx() const
372 { gold_unreachable(); }
374 // Set the output section index, if this is an output section.
375 virtual void
376 do_set_out_shndx(unsigned int)
377 { gold_unreachable(); }
379 // This is a hook for derived classes to set the data size. This is
380 // called by finalize_data_size, normally called during
381 // Layout::finalize, when the section address is set.
382 virtual void
383 set_final_data_size()
384 { gold_unreachable(); }
386 // A hook for resetting the address and file offset.
387 virtual void
388 do_reset_address_and_file_offset()
391 // Return true if address and file offset already have reset values. In
392 // other words, calling reset_address_and_file_offset will not change them.
393 // A child class overriding do_reset_address_and_file_offset may need to
394 // also override this.
395 virtual bool
396 do_address_and_file_offset_have_reset_values() const
397 { return !this->is_address_valid_ && !this->is_offset_valid_; }
399 // Set the TLS offset. Called only for SHT_TLS sections.
400 virtual void
401 do_set_tls_offset(uint64_t)
402 { gold_unreachable(); }
404 // Return the TLS offset, relative to the base of the TLS segment.
405 // Valid only for SHT_TLS sections.
406 virtual uint64_t
407 do_tls_offset() const
408 { gold_unreachable(); }
410 // Print to the map file. This only needs to be implemented by
411 // classes which may appear in a PT_LOAD segment.
412 virtual void
413 do_print_to_mapfile(Mapfile*) const
414 { gold_unreachable(); }
416 // Functions that child classes may call.
418 // Reset the address. The Output_section class needs this when an
419 // SHF_ALLOC input section is added to an output section which was
420 // formerly not SHF_ALLOC.
421 void
422 mark_address_invalid()
423 { this->is_address_valid_ = false; }
425 // Set the size of the data.
426 void
427 set_data_size(off_t data_size)
429 gold_assert(!this->is_data_size_valid_
430 && !this->is_data_size_fixed_);
431 this->data_size_ = data_size;
432 this->is_data_size_valid_ = true;
435 // Fix the data size. Once it is fixed, it cannot be changed
436 // and the data size remains always valid.
437 void
438 fix_data_size()
440 gold_assert(this->is_data_size_valid_);
441 this->is_data_size_fixed_ = true;
444 // Get the current data size--this is for the convenience of
445 // sections which build up their size over time.
446 off_t
447 current_data_size_for_child() const
448 { return this->data_size_; }
450 // Set the current data size--this is for the convenience of
451 // sections which build up their size over time.
452 void
453 set_current_data_size_for_child(off_t data_size)
455 gold_assert(!this->is_data_size_valid_);
456 this->data_size_ = data_size;
459 // Return default alignment for the target size.
460 static uint64_t
461 default_alignment();
463 // Return default alignment for a specified size--32 or 64.
464 static uint64_t
465 default_alignment_for_size(int size);
467 private:
468 Output_data(const Output_data&);
469 Output_data& operator=(const Output_data&);
471 // This is used for verification, to make sure that we don't try to
472 // change any sizes of allocated sections after we set the section
473 // addresses.
474 static bool allocated_sizes_are_fixed;
476 // Memory address in output file.
477 uint64_t address_;
478 // Size of data in output file.
479 off_t data_size_;
480 // File offset of contents in output file.
481 off_t offset_;
482 // Whether address_ is valid.
483 bool is_address_valid_;
484 // Whether data_size_ is valid.
485 bool is_data_size_valid_;
486 // Whether offset_ is valid.
487 bool is_offset_valid_;
488 // Whether data size is fixed.
489 bool is_data_size_fixed_;
490 // Count of dynamic relocations applied to this section.
491 unsigned int dynamic_reloc_count_;
494 // Output the section headers.
496 class Output_section_headers : public Output_data
498 public:
499 Output_section_headers(const Layout*,
500 const Layout::Segment_list*,
501 const Layout::Section_list*,
502 const Layout::Section_list*,
503 const Stringpool*,
504 const Output_section*);
506 protected:
507 // Write the data to the file.
508 void
509 do_write(Output_file*);
511 // Return the required alignment.
512 uint64_t
513 do_addralign() const
514 { return Output_data::default_alignment(); }
516 // Write to a map file.
517 void
518 do_print_to_mapfile(Mapfile* mapfile) const
519 { mapfile->print_output_data(this, _("** section headers")); }
521 // Set final data size.
522 void
523 set_final_data_size()
524 { this->set_data_size(this->do_size()); }
526 private:
527 // Write the data to the file with the right size and endianness.
528 template<int size, bool big_endian>
529 void
530 do_sized_write(Output_file*);
532 // Compute data size.
533 off_t
534 do_size() const;
536 const Layout* layout_;
537 const Layout::Segment_list* segment_list_;
538 const Layout::Section_list* section_list_;
539 const Layout::Section_list* unattached_section_list_;
540 const Stringpool* secnamepool_;
541 const Output_section* shstrtab_section_;
544 // Output the segment headers.
546 class Output_segment_headers : public Output_data
548 public:
549 Output_segment_headers(const Layout::Segment_list& segment_list);
551 protected:
552 // Write the data to the file.
553 void
554 do_write(Output_file*);
556 // Return the required alignment.
557 uint64_t
558 do_addralign() const
559 { return Output_data::default_alignment(); }
561 // Write to a map file.
562 void
563 do_print_to_mapfile(Mapfile* mapfile) const
564 { mapfile->print_output_data(this, _("** segment headers")); }
566 // Set final data size.
567 void
568 set_final_data_size()
569 { this->set_data_size(this->do_size()); }
571 private:
572 // Write the data to the file with the right size and endianness.
573 template<int size, bool big_endian>
574 void
575 do_sized_write(Output_file*);
577 // Compute the current size.
578 off_t
579 do_size() const;
581 const Layout::Segment_list& segment_list_;
584 // Output the ELF file header.
586 class Output_file_header : public Output_data
588 public:
589 Output_file_header(const Target*,
590 const Symbol_table*,
591 const Output_segment_headers*,
592 const char* entry);
594 // Add information about the section headers. We lay out the ELF
595 // file header before we create the section headers.
596 void set_section_info(const Output_section_headers*,
597 const Output_section* shstrtab);
599 protected:
600 // Write the data to the file.
601 void
602 do_write(Output_file*);
604 // Return the required alignment.
605 uint64_t
606 do_addralign() const
607 { return Output_data::default_alignment(); }
609 // Write to a map file.
610 void
611 do_print_to_mapfile(Mapfile* mapfile) const
612 { mapfile->print_output_data(this, _("** file header")); }
614 // Set final data size.
615 void
616 set_final_data_size(void)
617 { this->set_data_size(this->do_size()); }
619 private:
620 // Write the data to the file with the right size and endianness.
621 template<int size, bool big_endian>
622 void
623 do_sized_write(Output_file*);
625 // Return the value to use for the entry address.
626 template<int size>
627 typename elfcpp::Elf_types<size>::Elf_Addr
628 entry();
630 // Compute the current data size.
631 off_t
632 do_size() const;
634 const Target* target_;
635 const Symbol_table* symtab_;
636 const Output_segment_headers* segment_header_;
637 const Output_section_headers* section_header_;
638 const Output_section* shstrtab_;
639 const char* entry_;
642 // Output sections are mainly comprised of input sections. However,
643 // there are cases where we have data to write out which is not in an
644 // input section. Output_section_data is used in such cases. This is
645 // an abstract base class.
647 class Output_section_data : public Output_data
649 public:
650 Output_section_data(off_t data_size, uint64_t addralign,
651 bool is_data_size_fixed)
652 : Output_data(), output_section_(NULL), addralign_(addralign)
654 this->set_data_size(data_size);
655 if (is_data_size_fixed)
656 this->fix_data_size();
659 Output_section_data(uint64_t addralign)
660 : Output_data(), output_section_(NULL), addralign_(addralign)
663 // Return the output section.
664 const Output_section*
665 output_section() const
666 { return this->output_section_; }
668 // Record the output section.
669 void
670 set_output_section(Output_section* os);
672 // Add an input section, for SHF_MERGE sections. This returns true
673 // if the section was handled.
674 bool
675 add_input_section(Relobj* object, unsigned int shndx)
676 { return this->do_add_input_section(object, shndx); }
678 // Given an input OBJECT, an input section index SHNDX within that
679 // object, and an OFFSET relative to the start of that input
680 // section, return whether or not the corresponding offset within
681 // the output section is known. If this function returns true, it
682 // sets *POUTPUT to the output offset. The value -1 indicates that
683 // this input offset is being discarded.
684 bool
685 output_offset(const Relobj* object, unsigned int shndx,
686 section_offset_type offset,
687 section_offset_type *poutput) const
688 { return this->do_output_offset(object, shndx, offset, poutput); }
690 // Return whether this is the merge section for the input section
691 // SHNDX in OBJECT. This should return true when output_offset
692 // would return true for some values of OFFSET.
693 bool
694 is_merge_section_for(const Relobj* object, unsigned int shndx) const
695 { return this->do_is_merge_section_for(object, shndx); }
697 // Write the contents to a buffer. This is used for sections which
698 // require postprocessing, such as compression.
699 void
700 write_to_buffer(unsigned char* buffer)
701 { this->do_write_to_buffer(buffer); }
703 // Print merge stats to stderr. This should only be called for
704 // SHF_MERGE sections.
705 void
706 print_merge_stats(const char* section_name)
707 { this->do_print_merge_stats(section_name); }
709 protected:
710 // The child class must implement do_write.
712 // The child class may implement specific adjustments to the output
713 // section.
714 virtual void
715 do_adjust_output_section(Output_section*)
718 // May be implemented by child class. Return true if the section
719 // was handled.
720 virtual bool
721 do_add_input_section(Relobj*, unsigned int)
722 { gold_unreachable(); }
724 // The child class may implement output_offset.
725 virtual bool
726 do_output_offset(const Relobj*, unsigned int, section_offset_type,
727 section_offset_type*) const
728 { return false; }
730 // The child class may implement is_merge_section_for.
731 virtual bool
732 do_is_merge_section_for(const Relobj*, unsigned int) const
733 { return false; }
735 // The child class may implement write_to_buffer. Most child
736 // classes can not appear in a compressed section, and they do not
737 // implement this.
738 virtual void
739 do_write_to_buffer(unsigned char*)
740 { gold_unreachable(); }
742 // Print merge statistics.
743 virtual void
744 do_print_merge_stats(const char*)
745 { gold_unreachable(); }
747 // Return the required alignment.
748 uint64_t
749 do_addralign() const
750 { return this->addralign_; }
752 // Return the output section.
753 Output_section*
754 do_output_section()
755 { return this->output_section_; }
757 const Output_section*
758 do_output_section() const
759 { return this->output_section_; }
761 // Return the section index of the output section.
762 unsigned int
763 do_out_shndx() const;
765 // Set the alignment.
766 void
767 set_addralign(uint64_t addralign);
769 private:
770 // The output section for this section.
771 Output_section* output_section_;
772 // The required alignment.
773 uint64_t addralign_;
776 // Some Output_section_data classes build up their data step by step,
777 // rather than all at once. This class provides an interface for
778 // them.
780 class Output_section_data_build : public Output_section_data
782 public:
783 Output_section_data_build(uint64_t addralign)
784 : Output_section_data(addralign)
787 // Get the current data size.
788 off_t
789 current_data_size() const
790 { return this->current_data_size_for_child(); }
792 // Set the current data size.
793 void
794 set_current_data_size(off_t data_size)
795 { this->set_current_data_size_for_child(data_size); }
797 protected:
798 // Set the final data size.
799 virtual void
800 set_final_data_size()
801 { this->set_data_size(this->current_data_size_for_child()); }
804 // A simple case of Output_data in which we have constant data to
805 // output.
807 class Output_data_const : public Output_section_data
809 public:
810 Output_data_const(const std::string& data, uint64_t addralign)
811 : Output_section_data(data.size(), addralign, true), data_(data)
814 Output_data_const(const char* p, off_t len, uint64_t addralign)
815 : Output_section_data(len, addralign, true), data_(p, len)
818 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
819 : Output_section_data(len, addralign, true),
820 data_(reinterpret_cast<const char*>(p), len)
823 protected:
824 // Write the data to the output file.
825 void
826 do_write(Output_file*);
828 // Write the data to a buffer.
829 void
830 do_write_to_buffer(unsigned char* buffer)
831 { memcpy(buffer, this->data_.data(), this->data_.size()); }
833 // Write to a map file.
834 void
835 do_print_to_mapfile(Mapfile* mapfile) const
836 { mapfile->print_output_data(this, _("** fill")); }
838 private:
839 std::string data_;
842 // Another version of Output_data with constant data, in which the
843 // buffer is allocated by the caller.
845 class Output_data_const_buffer : public Output_section_data
847 public:
848 Output_data_const_buffer(const unsigned char* p, off_t len,
849 uint64_t addralign, const char* map_name)
850 : Output_section_data(len, addralign, true),
851 p_(p), map_name_(map_name)
854 protected:
855 // Write the data the output file.
856 void
857 do_write(Output_file*);
859 // Write the data to a buffer.
860 void
861 do_write_to_buffer(unsigned char* buffer)
862 { memcpy(buffer, this->p_, this->data_size()); }
864 // Write to a map file.
865 void
866 do_print_to_mapfile(Mapfile* mapfile) const
867 { mapfile->print_output_data(this, _(this->map_name_)); }
869 private:
870 // The data to output.
871 const unsigned char* p_;
872 // Name to use in a map file. Maps are a rarely used feature, but
873 // the space usage is minor as aren't very many of these objects.
874 const char* map_name_;
877 // A place holder for a fixed amount of data written out via some
878 // other mechanism.
880 class Output_data_fixed_space : public Output_section_data
882 public:
883 Output_data_fixed_space(off_t data_size, uint64_t addralign,
884 const char* map_name)
885 : Output_section_data(data_size, addralign, true),
886 map_name_(map_name)
889 protected:
890 // Write out the data--the actual data must be written out
891 // elsewhere.
892 void
893 do_write(Output_file*)
896 // Write to a map file.
897 void
898 do_print_to_mapfile(Mapfile* mapfile) const
899 { mapfile->print_output_data(this, _(this->map_name_)); }
901 private:
902 // Name to use in a map file. Maps are a rarely used feature, but
903 // the space usage is minor as aren't very many of these objects.
904 const char* map_name_;
907 // A place holder for variable sized data written out via some other
908 // mechanism.
910 class Output_data_space : public Output_section_data_build
912 public:
913 explicit Output_data_space(uint64_t addralign, const char* map_name)
914 : Output_section_data_build(addralign),
915 map_name_(map_name)
918 // Set the alignment.
919 void
920 set_space_alignment(uint64_t align)
921 { this->set_addralign(align); }
923 protected:
924 // Write out the data--the actual data must be written out
925 // elsewhere.
926 void
927 do_write(Output_file*)
930 // Write to a map file.
931 void
932 do_print_to_mapfile(Mapfile* mapfile) const
933 { mapfile->print_output_data(this, _(this->map_name_)); }
935 private:
936 // Name to use in a map file. Maps are a rarely used feature, but
937 // the space usage is minor as aren't very many of these objects.
938 const char* map_name_;
941 // Fill fixed space with zeroes. This is just like
942 // Output_data_fixed_space, except that the map name is known.
944 class Output_data_zero_fill : public Output_section_data
946 public:
947 Output_data_zero_fill(off_t data_size, uint64_t addralign)
948 : Output_section_data(data_size, addralign, true)
951 protected:
952 // There is no data to write out.
953 void
954 do_write(Output_file*)
957 // Write to a map file.
958 void
959 do_print_to_mapfile(Mapfile* mapfile) const
960 { mapfile->print_output_data(this, "** zero fill"); }
963 // A string table which goes into an output section.
965 class Output_data_strtab : public Output_section_data
967 public:
968 Output_data_strtab(Stringpool* strtab)
969 : Output_section_data(1), strtab_(strtab)
972 protected:
973 // This is called to set the address and file offset. Here we make
974 // sure that the Stringpool is finalized.
975 void
976 set_final_data_size();
978 // Write out the data.
979 void
980 do_write(Output_file*);
982 // Write the data to a buffer.
983 void
984 do_write_to_buffer(unsigned char* buffer)
985 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
987 // Write to a map file.
988 void
989 do_print_to_mapfile(Mapfile* mapfile) const
990 { mapfile->print_output_data(this, _("** string table")); }
992 private:
993 Stringpool* strtab_;
996 // This POD class is used to represent a single reloc in the output
997 // file. This could be a private class within Output_data_reloc, but
998 // the templatization is complex enough that I broke it out into a
999 // separate class. The class is templatized on either elfcpp::SHT_REL
1000 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1001 // relocation or an ordinary relocation.
1003 // A relocation can be against a global symbol, a local symbol, a
1004 // local section symbol, an output section, or the undefined symbol at
1005 // index 0. We represent the latter by using a NULL global symbol.
1007 template<int sh_type, bool dynamic, int size, bool big_endian>
1008 class Output_reloc;
1010 template<bool dynamic, int size, bool big_endian>
1011 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1013 public:
1014 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1015 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1017 static const Address invalid_address = static_cast<Address>(0) - 1;
1019 // An uninitialized entry. We need this because we want to put
1020 // instances of this class into an STL container.
1021 Output_reloc()
1022 : local_sym_index_(INVALID_CODE)
1025 // We have a bunch of different constructors. They come in pairs
1026 // depending on how the address of the relocation is specified. It
1027 // can either be an offset in an Output_data or an offset in an
1028 // input section.
1030 // A reloc against a global symbol.
1032 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1033 Address address, bool is_relative);
1035 Output_reloc(Symbol* gsym, unsigned int type,
1036 Sized_relobj<size, big_endian>* relobj,
1037 unsigned int shndx, Address address, bool is_relative);
1039 // A reloc against a local symbol or local section symbol.
1041 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1042 unsigned int local_sym_index, unsigned int type,
1043 Output_data* od, Address address, bool is_relative,
1044 bool is_section_symbol);
1046 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1047 unsigned int local_sym_index, unsigned int type,
1048 unsigned int shndx, Address address, bool is_relative,
1049 bool is_section_symbol);
1051 // A reloc against the STT_SECTION symbol of an output section.
1053 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1054 Address address);
1056 Output_reloc(Output_section* os, unsigned int type,
1057 Sized_relobj<size, big_endian>* relobj,
1058 unsigned int shndx, Address address);
1060 // An absolute relocation with no symbol.
1062 Output_reloc(unsigned int type, Output_data* od, Address address);
1064 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1065 unsigned int shndx, Address address);
1067 // A target specific relocation. The target will be called to get
1068 // the symbol index, passing ARG. The type and offset will be set
1069 // as for other relocation types.
1071 Output_reloc(unsigned int type, void* arg, Output_data* od,
1072 Address address);
1074 Output_reloc(unsigned int type, void* arg,
1075 Sized_relobj<size, big_endian>* relobj,
1076 unsigned int shndx, Address address);
1078 // Return the reloc type.
1079 unsigned int
1080 type() const
1081 { return this->type_; }
1083 // Return whether this is a RELATIVE relocation.
1084 bool
1085 is_relative() const
1086 { return this->is_relative_; }
1088 // Return whether this is against a local section symbol.
1089 bool
1090 is_local_section_symbol() const
1092 return (this->local_sym_index_ != GSYM_CODE
1093 && this->local_sym_index_ != SECTION_CODE
1094 && this->local_sym_index_ != INVALID_CODE
1095 && this->local_sym_index_ != TARGET_CODE
1096 && this->is_section_symbol_);
1099 // Return whether this is a target specific relocation.
1100 bool
1101 is_target_specific() const
1102 { return this->local_sym_index_ == TARGET_CODE; }
1104 // Return the argument to pass to the target for a target specific
1105 // relocation.
1106 void*
1107 target_arg() const
1109 gold_assert(this->local_sym_index_ == TARGET_CODE);
1110 return this->u1_.arg;
1113 // For a local section symbol, return the offset of the input
1114 // section within the output section. ADDEND is the addend being
1115 // applied to the input section.
1116 Address
1117 local_section_offset(Addend addend) const;
1119 // Get the value of the symbol referred to by a Rel relocation when
1120 // we are adding the given ADDEND.
1121 Address
1122 symbol_value(Addend addend) const;
1124 // Write the reloc entry to an output view.
1125 void
1126 write(unsigned char* pov) const;
1128 // Write the offset and info fields to Write_rel.
1129 template<typename Write_rel>
1130 void write_rel(Write_rel*) const;
1132 // This is used when sorting dynamic relocs. Return -1 to sort this
1133 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1135 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1136 const;
1138 // Return whether this reloc should be sorted before the argument
1139 // when sorting dynamic relocs.
1140 bool
1141 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1142 r2) const
1143 { return this->compare(r2) < 0; }
1145 private:
1146 // Record that we need a dynamic symbol index.
1147 void
1148 set_needs_dynsym_index();
1150 // Return the symbol index.
1151 unsigned int
1152 get_symbol_index() const;
1154 // Return the output address.
1155 Address
1156 get_address() const;
1158 // Codes for local_sym_index_.
1159 enum
1161 // Global symbol.
1162 GSYM_CODE = -1U,
1163 // Output section.
1164 SECTION_CODE = -2U,
1165 // Target specific.
1166 TARGET_CODE = -3U,
1167 // Invalid uninitialized entry.
1168 INVALID_CODE = -4U
1171 union
1173 // For a local symbol or local section symbol
1174 // (this->local_sym_index_ >= 0), the object. We will never
1175 // generate a relocation against a local symbol in a dynamic
1176 // object; that doesn't make sense. And our callers will always
1177 // be templatized, so we use Sized_relobj here.
1178 Sized_relobj<size, big_endian>* relobj;
1179 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1180 // symbol. If this is NULL, it indicates a relocation against the
1181 // undefined 0 symbol.
1182 Symbol* gsym;
1183 // For a relocation against an output section
1184 // (this->local_sym_index_ == SECTION_CODE), the output section.
1185 Output_section* os;
1186 // For a target specific relocation, an argument to pass to the
1187 // target.
1188 void* arg;
1189 } u1_;
1190 union
1192 // If this->shndx_ is not INVALID CODE, the object which holds the
1193 // input section being used to specify the reloc address.
1194 Sized_relobj<size, big_endian>* relobj;
1195 // If this->shndx_ is INVALID_CODE, the output data being used to
1196 // specify the reloc address. This may be NULL if the reloc
1197 // address is absolute.
1198 Output_data* od;
1199 } u2_;
1200 // The address offset within the input section or the Output_data.
1201 Address address_;
1202 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1203 // relocation against an output section, or TARGET_CODE for a target
1204 // specific relocation, or INVALID_CODE for an uninitialized value.
1205 // Otherwise, for a local symbol (this->is_section_symbol_ is
1206 // false), the local symbol index. For a local section symbol
1207 // (this->is_section_symbol_ is true), the section index in the
1208 // input file.
1209 unsigned int local_sym_index_;
1210 // The reloc type--a processor specific code.
1211 unsigned int type_ : 30;
1212 // True if the relocation is a RELATIVE relocation.
1213 bool is_relative_ : 1;
1214 // True if the relocation is against a section symbol.
1215 bool is_section_symbol_ : 1;
1216 // If the reloc address is an input section in an object, the
1217 // section index. This is INVALID_CODE if the reloc address is
1218 // specified in some other way.
1219 unsigned int shndx_;
1222 // The SHT_RELA version of Output_reloc<>. This is just derived from
1223 // the SHT_REL version of Output_reloc, but it adds an addend.
1225 template<bool dynamic, int size, bool big_endian>
1226 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1228 public:
1229 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1230 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1232 // An uninitialized entry.
1233 Output_reloc()
1234 : rel_()
1237 // A reloc against a global symbol.
1239 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1240 Address address, Addend addend, bool is_relative)
1241 : rel_(gsym, type, od, address, is_relative), addend_(addend)
1244 Output_reloc(Symbol* gsym, unsigned int type,
1245 Sized_relobj<size, big_endian>* relobj,
1246 unsigned int shndx, Address address, Addend addend,
1247 bool is_relative)
1248 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1251 // A reloc against a local symbol.
1253 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1254 unsigned int local_sym_index, unsigned int type,
1255 Output_data* od, Address address,
1256 Addend addend, bool is_relative, bool is_section_symbol)
1257 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1258 is_section_symbol),
1259 addend_(addend)
1262 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1263 unsigned int local_sym_index, unsigned int type,
1264 unsigned int shndx, Address address,
1265 Addend addend, bool is_relative, bool is_section_symbol)
1266 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1267 is_section_symbol),
1268 addend_(addend)
1271 // A reloc against the STT_SECTION symbol of an output section.
1273 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1274 Address address, Addend addend)
1275 : rel_(os, type, od, address), addend_(addend)
1278 Output_reloc(Output_section* os, unsigned int type,
1279 Sized_relobj<size, big_endian>* relobj,
1280 unsigned int shndx, Address address, Addend addend)
1281 : rel_(os, type, relobj, shndx, address), addend_(addend)
1284 // An absolute relocation with no symbol.
1286 Output_reloc(unsigned int type, Output_data* od, Address address,
1287 Addend addend)
1288 : rel_(type, od, address), addend_(addend)
1291 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1292 unsigned int shndx, Address address, Addend addend)
1293 : rel_(type, relobj, shndx, address), addend_(addend)
1296 // A target specific relocation. The target will be called to get
1297 // the symbol index and the addend, passing ARG. The type and
1298 // offset will be set as for other relocation types.
1300 Output_reloc(unsigned int type, void* arg, Output_data* od,
1301 Address address, Addend addend)
1302 : rel_(type, arg, od, address), addend_(addend)
1305 Output_reloc(unsigned int type, void* arg,
1306 Sized_relobj<size, big_endian>* relobj,
1307 unsigned int shndx, Address address, Addend addend)
1308 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1311 // Return whether this is a RELATIVE relocation.
1312 bool
1313 is_relative() const
1314 { return this->rel_.is_relative(); }
1316 // Write the reloc entry to an output view.
1317 void
1318 write(unsigned char* pov) const;
1320 // Return whether this reloc should be sorted before the argument
1321 // when sorting dynamic relocs.
1322 bool
1323 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1324 r2) const
1326 int i = this->rel_.compare(r2.rel_);
1327 if (i < 0)
1328 return true;
1329 else if (i > 0)
1330 return false;
1331 else
1332 return this->addend_ < r2.addend_;
1335 private:
1336 // The basic reloc.
1337 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1338 // The addend.
1339 Addend addend_;
1342 // Output_data_reloc_generic is a non-template base class for
1343 // Output_data_reloc_base. This gives the generic code a way to hold
1344 // a pointer to a reloc section.
1346 class Output_data_reloc_generic : public Output_section_data_build
1348 public:
1349 Output_data_reloc_generic(int size, bool sort_relocs)
1350 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1351 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1354 // Return the number of relative relocs in this section.
1355 size_t
1356 relative_reloc_count() const
1357 { return this->relative_reloc_count_; }
1359 // Whether we should sort the relocs.
1360 bool
1361 sort_relocs() const
1362 { return this->sort_relocs_; }
1364 protected:
1365 // Note that we've added another relative reloc.
1366 void
1367 bump_relative_reloc_count()
1368 { ++this->relative_reloc_count_; }
1370 private:
1371 // The number of relative relocs added to this section. This is to
1372 // support DT_RELCOUNT.
1373 size_t relative_reloc_count_;
1374 // Whether to sort the relocations when writing them out, to make
1375 // the dynamic linker more efficient.
1376 bool sort_relocs_;
1379 // Output_data_reloc is used to manage a section containing relocs.
1380 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1381 // indicates whether this is a dynamic relocation or a normal
1382 // relocation. Output_data_reloc_base is a base class.
1383 // Output_data_reloc is the real class, which we specialize based on
1384 // the reloc type.
1386 template<int sh_type, bool dynamic, int size, bool big_endian>
1387 class Output_data_reloc_base : public Output_data_reloc_generic
1389 public:
1390 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1391 typedef typename Output_reloc_type::Address Address;
1392 static const int reloc_size =
1393 Reloc_types<sh_type, size, big_endian>::reloc_size;
1395 // Construct the section.
1396 Output_data_reloc_base(bool sort_relocs)
1397 : Output_data_reloc_generic(size, sort_relocs)
1400 protected:
1401 // Write out the data.
1402 void
1403 do_write(Output_file*);
1405 // Set the entry size and the link.
1406 void
1407 do_adjust_output_section(Output_section *os);
1409 // Write to a map file.
1410 void
1411 do_print_to_mapfile(Mapfile* mapfile) const
1413 mapfile->print_output_data(this,
1414 (dynamic
1415 ? _("** dynamic relocs")
1416 : _("** relocs")));
1419 // Add a relocation entry.
1420 void
1421 add(Output_data *od, const Output_reloc_type& reloc)
1423 this->relocs_.push_back(reloc);
1424 this->set_current_data_size(this->relocs_.size() * reloc_size);
1425 od->add_dynamic_reloc();
1426 if (reloc.is_relative())
1427 this->bump_relative_reloc_count();
1430 private:
1431 typedef std::vector<Output_reloc_type> Relocs;
1433 // The class used to sort the relocations.
1434 struct Sort_relocs_comparison
1436 bool
1437 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1438 { return r1.sort_before(r2); }
1441 // The relocations in this section.
1442 Relocs relocs_;
1445 // The class which callers actually create.
1447 template<int sh_type, bool dynamic, int size, bool big_endian>
1448 class Output_data_reloc;
1450 // The SHT_REL version of Output_data_reloc.
1452 template<bool dynamic, int size, bool big_endian>
1453 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1454 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1456 private:
1457 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1458 big_endian> Base;
1460 public:
1461 typedef typename Base::Output_reloc_type Output_reloc_type;
1462 typedef typename Output_reloc_type::Address Address;
1464 Output_data_reloc(bool sr)
1465 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1468 // Add a reloc against a global symbol.
1470 void
1471 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1472 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1474 void
1475 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1476 Sized_relobj<size, big_endian>* relobj,
1477 unsigned int shndx, Address address)
1478 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1479 false)); }
1481 // These are to simplify the Copy_relocs class.
1483 void
1484 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1485 Address addend)
1487 gold_assert(addend == 0);
1488 this->add_global(gsym, type, od, address);
1491 void
1492 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1493 Sized_relobj<size, big_endian>* relobj,
1494 unsigned int shndx, Address address, Address addend)
1496 gold_assert(addend == 0);
1497 this->add_global(gsym, type, od, relobj, shndx, address);
1500 // Add a RELATIVE reloc against a global symbol. The final relocation
1501 // will not reference the symbol.
1503 void
1504 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1505 Address address)
1506 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1508 void
1509 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1510 Sized_relobj<size, big_endian>* relobj,
1511 unsigned int shndx, Address address)
1513 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1514 true));
1517 // Add a reloc against a local symbol.
1519 void
1520 add_local(Sized_relobj<size, big_endian>* relobj,
1521 unsigned int local_sym_index, unsigned int type,
1522 Output_data* od, Address address)
1524 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1525 address, false, false));
1528 void
1529 add_local(Sized_relobj<size, big_endian>* relobj,
1530 unsigned int local_sym_index, unsigned int type,
1531 Output_data* od, unsigned int shndx, Address address)
1533 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1534 address, false, false));
1537 // Add a RELATIVE reloc against a local symbol.
1539 void
1540 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1541 unsigned int local_sym_index, unsigned int type,
1542 Output_data* od, Address address)
1544 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1545 address, true, false));
1548 void
1549 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1550 unsigned int local_sym_index, unsigned int type,
1551 Output_data* od, unsigned int shndx, Address address)
1553 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1554 address, true, false));
1557 // Add a reloc against a local section symbol. This will be
1558 // converted into a reloc against the STT_SECTION symbol of the
1559 // output section.
1561 void
1562 add_local_section(Sized_relobj<size, big_endian>* relobj,
1563 unsigned int input_shndx, unsigned int type,
1564 Output_data* od, Address address)
1566 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1567 address, false, true));
1570 void
1571 add_local_section(Sized_relobj<size, big_endian>* relobj,
1572 unsigned int input_shndx, unsigned int type,
1573 Output_data* od, unsigned int shndx, Address address)
1575 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1576 address, false, true));
1579 // A reloc against the STT_SECTION symbol of an output section.
1580 // OS is the Output_section that the relocation refers to; OD is
1581 // the Output_data object being relocated.
1583 void
1584 add_output_section(Output_section* os, unsigned int type,
1585 Output_data* od, Address address)
1586 { this->add(od, Output_reloc_type(os, type, od, address)); }
1588 void
1589 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1590 Sized_relobj<size, big_endian>* relobj,
1591 unsigned int shndx, Address address)
1592 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1594 // Add an absolute relocation.
1596 void
1597 add_absolute(unsigned int type, Output_data* od, Address address)
1598 { this->add(od, Output_reloc_type(type, od, address)); }
1600 void
1601 add_absolute(unsigned int type, Output_data* od,
1602 Sized_relobj<size, big_endian>* relobj,
1603 unsigned int shndx, Address address)
1604 { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1606 // Add a target specific relocation. A target which calls this must
1607 // define the reloc_symbol_index and reloc_addend virtual functions.
1609 void
1610 add_target_specific(unsigned int type, void* arg, Output_data* od,
1611 Address address)
1612 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1614 void
1615 add_target_specific(unsigned int type, void* arg, Output_data* od,
1616 Sized_relobj<size, big_endian>* relobj,
1617 unsigned int shndx, Address address)
1618 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1621 // The SHT_RELA version of Output_data_reloc.
1623 template<bool dynamic, int size, bool big_endian>
1624 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1625 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1627 private:
1628 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1629 big_endian> Base;
1631 public:
1632 typedef typename Base::Output_reloc_type Output_reloc_type;
1633 typedef typename Output_reloc_type::Address Address;
1634 typedef typename Output_reloc_type::Addend Addend;
1636 Output_data_reloc(bool sr)
1637 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1640 // Add a reloc against a global symbol.
1642 void
1643 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1644 Address address, Addend addend)
1645 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1646 false)); }
1648 void
1649 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1650 Sized_relobj<size, big_endian>* relobj,
1651 unsigned int shndx, Address address,
1652 Addend addend)
1653 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1654 addend, false)); }
1656 // Add a RELATIVE reloc against a global symbol. The final output
1657 // relocation will not reference the symbol, but we must keep the symbol
1658 // information long enough to set the addend of the relocation correctly
1659 // when it is written.
1661 void
1662 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1663 Address address, Addend addend)
1664 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1666 void
1667 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1668 Sized_relobj<size, big_endian>* relobj,
1669 unsigned int shndx, Address address, Addend addend)
1670 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1671 addend, true)); }
1673 // Add a reloc against a local symbol.
1675 void
1676 add_local(Sized_relobj<size, big_endian>* relobj,
1677 unsigned int local_sym_index, unsigned int type,
1678 Output_data* od, Address address, Addend addend)
1680 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1681 addend, false, false));
1684 void
1685 add_local(Sized_relobj<size, big_endian>* relobj,
1686 unsigned int local_sym_index, unsigned int type,
1687 Output_data* od, unsigned int shndx, Address address,
1688 Addend addend)
1690 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1691 address, addend, false, false));
1694 // Add a RELATIVE reloc against a local symbol.
1696 void
1697 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1698 unsigned int local_sym_index, unsigned int type,
1699 Output_data* od, Address address, Addend addend)
1701 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1702 addend, true, false));
1705 void
1706 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1707 unsigned int local_sym_index, unsigned int type,
1708 Output_data* od, unsigned int shndx, Address address,
1709 Addend addend)
1711 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1712 address, addend, true, false));
1715 // Add a reloc against a local section symbol. This will be
1716 // converted into a reloc against the STT_SECTION symbol of the
1717 // output section.
1719 void
1720 add_local_section(Sized_relobj<size, big_endian>* relobj,
1721 unsigned int input_shndx, unsigned int type,
1722 Output_data* od, Address address, Addend addend)
1724 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1725 addend, false, true));
1728 void
1729 add_local_section(Sized_relobj<size, big_endian>* relobj,
1730 unsigned int input_shndx, unsigned int type,
1731 Output_data* od, unsigned int shndx, Address address,
1732 Addend addend)
1734 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1735 address, addend, false, true));
1738 // A reloc against the STT_SECTION symbol of an output section.
1740 void
1741 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1742 Address address, Addend addend)
1743 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1745 void
1746 add_output_section(Output_section* os, unsigned int type,
1747 Sized_relobj<size, big_endian>* relobj,
1748 unsigned int shndx, Address address, Addend addend)
1749 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1750 addend)); }
1752 // Add an absolute relocation.
1754 void
1755 add_absolute(unsigned int type, Output_data* od, Address address,
1756 Addend addend)
1757 { this->add(od, Output_reloc_type(type, od, address, addend)); }
1759 void
1760 add_absolute(unsigned int type, Output_data* od,
1761 Sized_relobj<size, big_endian>* relobj,
1762 unsigned int shndx, Address address, Addend addend)
1763 { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1765 // Add a target specific relocation. A target which calls this must
1766 // define the reloc_symbol_index and reloc_addend virtual functions.
1768 void
1769 add_target_specific(unsigned int type, void* arg, Output_data* od,
1770 Address address, Addend addend)
1771 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1773 void
1774 add_target_specific(unsigned int type, void* arg, Output_data* od,
1775 Sized_relobj<size, big_endian>* relobj,
1776 unsigned int shndx, Address address, Addend addend)
1778 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1779 addend));
1783 // Output_relocatable_relocs represents a relocation section in a
1784 // relocatable link. The actual data is written out in the target
1785 // hook relocate_for_relocatable. This just saves space for it.
1787 template<int sh_type, int size, bool big_endian>
1788 class Output_relocatable_relocs : public Output_section_data
1790 public:
1791 Output_relocatable_relocs(Relocatable_relocs* rr)
1792 : Output_section_data(Output_data::default_alignment_for_size(size)),
1793 rr_(rr)
1796 void
1797 set_final_data_size();
1799 // Write out the data. There is nothing to do here.
1800 void
1801 do_write(Output_file*)
1804 // Write to a map file.
1805 void
1806 do_print_to_mapfile(Mapfile* mapfile) const
1807 { mapfile->print_output_data(this, _("** relocs")); }
1809 private:
1810 // The relocs associated with this input section.
1811 Relocatable_relocs* rr_;
1814 // Handle a GROUP section.
1816 template<int size, bool big_endian>
1817 class Output_data_group : public Output_section_data
1819 public:
1820 // The constructor clears *INPUT_SHNDXES.
1821 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1822 section_size_type entry_count,
1823 elfcpp::Elf_Word flags,
1824 std::vector<unsigned int>* input_shndxes);
1826 void
1827 do_write(Output_file*);
1829 // Write to a map file.
1830 void
1831 do_print_to_mapfile(Mapfile* mapfile) const
1832 { mapfile->print_output_data(this, _("** group")); }
1834 // Set final data size.
1835 void
1836 set_final_data_size()
1837 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1839 private:
1840 // The input object.
1841 Sized_relobj<size, big_endian>* relobj_;
1842 // The group flag word.
1843 elfcpp::Elf_Word flags_;
1844 // The section indexes of the input sections in this group.
1845 std::vector<unsigned int> input_shndxes_;
1848 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1849 // for one symbol--either a global symbol or a local symbol in an
1850 // object. The target specific code adds entries to the GOT as
1851 // needed.
1853 template<int size, bool big_endian>
1854 class Output_data_got : public Output_section_data_build
1856 public:
1857 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1858 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1859 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1861 Output_data_got()
1862 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1863 entries_()
1866 // Add an entry for a global symbol to the GOT. Return true if this
1867 // is a new GOT entry, false if the symbol was already in the GOT.
1868 bool
1869 add_global(Symbol* gsym, unsigned int got_type);
1871 // Add an entry for a global symbol to the GOT, and add a dynamic
1872 // relocation of type R_TYPE for the GOT entry.
1873 void
1874 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1875 Rel_dyn* rel_dyn, unsigned int r_type);
1877 void
1878 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1879 Rela_dyn* rela_dyn, unsigned int r_type);
1881 // Add a pair of entries for a global symbol to the GOT, and add
1882 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1883 void
1884 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1885 Rel_dyn* rel_dyn, unsigned int r_type_1,
1886 unsigned int r_type_2);
1888 void
1889 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1890 Rela_dyn* rela_dyn, unsigned int r_type_1,
1891 unsigned int r_type_2);
1893 // Add an entry for a local symbol to the GOT. This returns true if
1894 // this is a new GOT entry, false if the symbol already has a GOT
1895 // entry.
1896 bool
1897 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1898 unsigned int got_type);
1900 // Add an entry for a local symbol to the GOT, and add a dynamic
1901 // relocation of type R_TYPE for the GOT entry.
1902 void
1903 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1904 unsigned int sym_index, unsigned int got_type,
1905 Rel_dyn* rel_dyn, unsigned int r_type);
1907 void
1908 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1909 unsigned int sym_index, unsigned int got_type,
1910 Rela_dyn* rela_dyn, unsigned int r_type);
1912 // Add a pair of entries for a local symbol to the GOT, and add
1913 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1914 void
1915 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1916 unsigned int sym_index, unsigned int shndx,
1917 unsigned int got_type, Rel_dyn* rel_dyn,
1918 unsigned int r_type_1, unsigned int r_type_2);
1920 void
1921 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1922 unsigned int sym_index, unsigned int shndx,
1923 unsigned int got_type, Rela_dyn* rela_dyn,
1924 unsigned int r_type_1, unsigned int r_type_2);
1926 // Add a constant to the GOT. This returns the offset of the new
1927 // entry from the start of the GOT.
1928 unsigned int
1929 add_constant(Valtype constant)
1931 this->entries_.push_back(Got_entry(constant));
1932 this->set_got_size();
1933 return this->last_got_offset();
1936 protected:
1937 // Write out the GOT table.
1938 void
1939 do_write(Output_file*);
1941 // Write to a map file.
1942 void
1943 do_print_to_mapfile(Mapfile* mapfile) const
1944 { mapfile->print_output_data(this, _("** GOT")); }
1946 private:
1947 // This POD class holds a single GOT entry.
1948 class Got_entry
1950 public:
1951 // Create a zero entry.
1952 Got_entry()
1953 : local_sym_index_(CONSTANT_CODE)
1954 { this->u_.constant = 0; }
1956 // Create a global symbol entry.
1957 explicit Got_entry(Symbol* gsym)
1958 : local_sym_index_(GSYM_CODE)
1959 { this->u_.gsym = gsym; }
1961 // Create a local symbol entry.
1962 Got_entry(Sized_relobj<size, big_endian>* object,
1963 unsigned int local_sym_index)
1964 : local_sym_index_(local_sym_index)
1966 gold_assert(local_sym_index != GSYM_CODE
1967 && local_sym_index != CONSTANT_CODE);
1968 this->u_.object = object;
1971 // Create a constant entry. The constant is a host value--it will
1972 // be swapped, if necessary, when it is written out.
1973 explicit Got_entry(Valtype constant)
1974 : local_sym_index_(CONSTANT_CODE)
1975 { this->u_.constant = constant; }
1977 // Write the GOT entry to an output view.
1978 void
1979 write(unsigned char* pov) const;
1981 private:
1982 enum
1984 GSYM_CODE = -1U,
1985 CONSTANT_CODE = -2U
1988 union
1990 // For a local symbol, the object.
1991 Sized_relobj<size, big_endian>* object;
1992 // For a global symbol, the symbol.
1993 Symbol* gsym;
1994 // For a constant, the constant.
1995 Valtype constant;
1996 } u_;
1997 // For a local symbol, the local symbol index. This is GSYM_CODE
1998 // for a global symbol, or CONSTANT_CODE for a constant.
1999 unsigned int local_sym_index_;
2002 typedef std::vector<Got_entry> Got_entries;
2004 // Return the offset into the GOT of GOT entry I.
2005 unsigned int
2006 got_offset(unsigned int i) const
2007 { return i * (size / 8); }
2009 // Return the offset into the GOT of the last entry added.
2010 unsigned int
2011 last_got_offset() const
2012 { return this->got_offset(this->entries_.size() - 1); }
2014 // Set the size of the section.
2015 void
2016 set_got_size()
2017 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2019 // The list of GOT entries.
2020 Got_entries entries_;
2023 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2024 // section.
2026 class Output_data_dynamic : public Output_section_data
2028 public:
2029 Output_data_dynamic(Stringpool* pool)
2030 : Output_section_data(Output_data::default_alignment()),
2031 entries_(), pool_(pool)
2034 // Add a new dynamic entry with a fixed numeric value.
2035 void
2036 add_constant(elfcpp::DT tag, unsigned int val)
2037 { this->add_entry(Dynamic_entry(tag, val)); }
2039 // Add a new dynamic entry with the address of output data.
2040 void
2041 add_section_address(elfcpp::DT tag, const Output_data* od)
2042 { this->add_entry(Dynamic_entry(tag, od, false)); }
2044 // Add a new dynamic entry with the address of output data
2045 // plus a constant offset.
2046 void
2047 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2048 unsigned int offset)
2049 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2051 // Add a new dynamic entry with the size of output data.
2052 void
2053 add_section_size(elfcpp::DT tag, const Output_data* od)
2054 { this->add_entry(Dynamic_entry(tag, od, true)); }
2056 // Add a new dynamic entry with the address of a symbol.
2057 void
2058 add_symbol(elfcpp::DT tag, const Symbol* sym)
2059 { this->add_entry(Dynamic_entry(tag, sym)); }
2061 // Add a new dynamic entry with a string.
2062 void
2063 add_string(elfcpp::DT tag, const char* str)
2064 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2066 void
2067 add_string(elfcpp::DT tag, const std::string& str)
2068 { this->add_string(tag, str.c_str()); }
2070 protected:
2071 // Adjust the output section to set the entry size.
2072 void
2073 do_adjust_output_section(Output_section*);
2075 // Set the final data size.
2076 void
2077 set_final_data_size();
2079 // Write out the dynamic entries.
2080 void
2081 do_write(Output_file*);
2083 // Write to a map file.
2084 void
2085 do_print_to_mapfile(Mapfile* mapfile) const
2086 { mapfile->print_output_data(this, _("** dynamic")); }
2088 private:
2089 // This POD class holds a single dynamic entry.
2090 class Dynamic_entry
2092 public:
2093 // Create an entry with a fixed numeric value.
2094 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2095 : tag_(tag), offset_(DYNAMIC_NUMBER)
2096 { this->u_.val = val; }
2098 // Create an entry with the size or address of a section.
2099 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2100 : tag_(tag),
2101 offset_(section_size
2102 ? DYNAMIC_SECTION_SIZE
2103 : DYNAMIC_SECTION_ADDRESS)
2104 { this->u_.od = od; }
2106 // Create an entry with the address of a section plus a constant offset.
2107 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2108 : tag_(tag),
2109 offset_(offset)
2110 { this->u_.od = od; }
2112 // Create an entry with the address of a symbol.
2113 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2114 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2115 { this->u_.sym = sym; }
2117 // Create an entry with a string.
2118 Dynamic_entry(elfcpp::DT tag, const char* str)
2119 : tag_(tag), offset_(DYNAMIC_STRING)
2120 { this->u_.str = str; }
2122 // Return the tag of this entry.
2123 elfcpp::DT
2124 tag() const
2125 { return this->tag_; }
2127 // Write the dynamic entry to an output view.
2128 template<int size, bool big_endian>
2129 void
2130 write(unsigned char* pov, const Stringpool*) const;
2132 private:
2133 // Classification is encoded in the OFFSET field.
2134 enum Classification
2136 // Section address.
2137 DYNAMIC_SECTION_ADDRESS = 0,
2138 // Number.
2139 DYNAMIC_NUMBER = -1U,
2140 // Section size.
2141 DYNAMIC_SECTION_SIZE = -2U,
2142 // Symbol adress.
2143 DYNAMIC_SYMBOL = -3U,
2144 // String.
2145 DYNAMIC_STRING = -4U
2146 // Any other value indicates a section address plus OFFSET.
2149 union
2151 // For DYNAMIC_NUMBER.
2152 unsigned int val;
2153 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2154 const Output_data* od;
2155 // For DYNAMIC_SYMBOL.
2156 const Symbol* sym;
2157 // For DYNAMIC_STRING.
2158 const char* str;
2159 } u_;
2160 // The dynamic tag.
2161 elfcpp::DT tag_;
2162 // The type of entry (Classification) or offset within a section.
2163 unsigned int offset_;
2166 // Add an entry to the list.
2167 void
2168 add_entry(const Dynamic_entry& entry)
2169 { this->entries_.push_back(entry); }
2171 // Sized version of write function.
2172 template<int size, bool big_endian>
2173 void
2174 sized_write(Output_file* of);
2176 // The type of the list of entries.
2177 typedef std::vector<Dynamic_entry> Dynamic_entries;
2179 // The entries.
2180 Dynamic_entries entries_;
2181 // The pool used for strings.
2182 Stringpool* pool_;
2185 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2186 // which may be required if the object file has more than
2187 // SHN_LORESERVE sections.
2189 class Output_symtab_xindex : public Output_section_data
2191 public:
2192 Output_symtab_xindex(size_t symcount)
2193 : Output_section_data(symcount * 4, 4, true),
2194 entries_()
2197 // Add an entry: symbol number SYMNDX has section SHNDX.
2198 void
2199 add(unsigned int symndx, unsigned int shndx)
2200 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2202 protected:
2203 void
2204 do_write(Output_file*);
2206 // Write to a map file.
2207 void
2208 do_print_to_mapfile(Mapfile* mapfile) const
2209 { mapfile->print_output_data(this, _("** symtab xindex")); }
2211 private:
2212 template<bool big_endian>
2213 void
2214 endian_do_write(unsigned char*);
2216 // It is likely that most symbols will not require entries. Rather
2217 // than keep a vector for all symbols, we keep pairs of symbol index
2218 // and section index.
2219 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2221 // The entries we need.
2222 Xindex_entries entries_;
2225 // A relaxed input section.
2226 class Output_relaxed_input_section : public Output_section_data_build
2228 public:
2229 // We would like to call relobj->section_addralign(shndx) to get the
2230 // alignment but we do not want the constructor to fail. So callers
2231 // are repsonsible for ensuring that.
2232 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2233 uint64_t addralign)
2234 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2237 // Return the Relobj of this relaxed input section.
2238 Relobj*
2239 relobj() const
2240 { return this->relobj_; }
2242 // Return the section index of this relaxed input section.
2243 unsigned int
2244 shndx() const
2245 { return this->shndx_; }
2247 private:
2248 Relobj* relobj_;
2249 unsigned int shndx_;
2252 // An output section. We don't expect to have too many output
2253 // sections, so we don't bother to do a template on the size.
2255 class Output_section : public Output_data
2257 public:
2258 // Create an output section, giving the name, type, and flags.
2259 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2260 virtual ~Output_section();
2262 // Add a new input section SHNDX, named NAME, with header SHDR, from
2263 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2264 // which applies to this section, or 0 if none, or -1 if more than
2265 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2266 // in a linker script; in that case we need to keep track of input
2267 // sections associated with an output section. Return the offset
2268 // within the output section.
2269 template<int size, bool big_endian>
2270 off_t
2271 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2272 const char *name,
2273 const elfcpp::Shdr<size, big_endian>& shdr,
2274 unsigned int reloc_shndx, bool have_sections_script);
2276 // Add generated data POSD to this output section.
2277 void
2278 add_output_section_data(Output_section_data* posd);
2280 // Add a relaxed input section PORIS to this output section.
2281 void
2282 add_relaxed_input_section(Output_relaxed_input_section* poris);
2284 // Return the section name.
2285 const char*
2286 name() const
2287 { return this->name_; }
2289 // Return the section type.
2290 elfcpp::Elf_Word
2291 type() const
2292 { return this->type_; }
2294 // Return the section flags.
2295 elfcpp::Elf_Xword
2296 flags() const
2297 { return this->flags_; }
2299 // Update the output section flags based on input section flags.
2300 void
2301 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2303 // Return the entsize field.
2304 uint64_t
2305 entsize() const
2306 { return this->entsize_; }
2308 // Set the entsize field.
2309 void
2310 set_entsize(uint64_t v);
2312 // Set the load address.
2313 void
2314 set_load_address(uint64_t load_address)
2316 this->load_address_ = load_address;
2317 this->has_load_address_ = true;
2320 // Set the link field to the output section index of a section.
2321 void
2322 set_link_section(const Output_data* od)
2324 gold_assert(this->link_ == 0
2325 && !this->should_link_to_symtab_
2326 && !this->should_link_to_dynsym_);
2327 this->link_section_ = od;
2330 // Set the link field to a constant.
2331 void
2332 set_link(unsigned int v)
2334 gold_assert(this->link_section_ == NULL
2335 && !this->should_link_to_symtab_
2336 && !this->should_link_to_dynsym_);
2337 this->link_ = v;
2340 // Record that this section should link to the normal symbol table.
2341 void
2342 set_should_link_to_symtab()
2344 gold_assert(this->link_section_ == NULL
2345 && this->link_ == 0
2346 && !this->should_link_to_dynsym_);
2347 this->should_link_to_symtab_ = true;
2350 // Record that this section should link to the dynamic symbol table.
2351 void
2352 set_should_link_to_dynsym()
2354 gold_assert(this->link_section_ == NULL
2355 && this->link_ == 0
2356 && !this->should_link_to_symtab_);
2357 this->should_link_to_dynsym_ = true;
2360 // Return the info field.
2361 unsigned int
2362 info() const
2364 gold_assert(this->info_section_ == NULL
2365 && this->info_symndx_ == NULL);
2366 return this->info_;
2369 // Set the info field to the output section index of a section.
2370 void
2371 set_info_section(const Output_section* os)
2373 gold_assert((this->info_section_ == NULL
2374 || (this->info_section_ == os
2375 && this->info_uses_section_index_))
2376 && this->info_symndx_ == NULL
2377 && this->info_ == 0);
2378 this->info_section_ = os;
2379 this->info_uses_section_index_= true;
2382 // Set the info field to the symbol table index of a symbol.
2383 void
2384 set_info_symndx(const Symbol* sym)
2386 gold_assert(this->info_section_ == NULL
2387 && (this->info_symndx_ == NULL
2388 || this->info_symndx_ == sym)
2389 && this->info_ == 0);
2390 this->info_symndx_ = sym;
2393 // Set the info field to the symbol table index of a section symbol.
2394 void
2395 set_info_section_symndx(const Output_section* os)
2397 gold_assert((this->info_section_ == NULL
2398 || (this->info_section_ == os
2399 && !this->info_uses_section_index_))
2400 && this->info_symndx_ == NULL
2401 && this->info_ == 0);
2402 this->info_section_ = os;
2403 this->info_uses_section_index_ = false;
2406 // Set the info field to a constant.
2407 void
2408 set_info(unsigned int v)
2410 gold_assert(this->info_section_ == NULL
2411 && this->info_symndx_ == NULL
2412 && (this->info_ == 0
2413 || this->info_ == v));
2414 this->info_ = v;
2417 // Set the addralign field.
2418 void
2419 set_addralign(uint64_t v)
2420 { this->addralign_ = v; }
2422 // Whether the output section index has been set.
2423 bool
2424 has_out_shndx() const
2425 { return this->out_shndx_ != -1U; }
2427 // Indicate that we need a symtab index.
2428 void
2429 set_needs_symtab_index()
2430 { this->needs_symtab_index_ = true; }
2432 // Return whether we need a symtab index.
2433 bool
2434 needs_symtab_index() const
2435 { return this->needs_symtab_index_; }
2437 // Get the symtab index.
2438 unsigned int
2439 symtab_index() const
2441 gold_assert(this->symtab_index_ != 0);
2442 return this->symtab_index_;
2445 // Set the symtab index.
2446 void
2447 set_symtab_index(unsigned int index)
2449 gold_assert(index != 0);
2450 this->symtab_index_ = index;
2453 // Indicate that we need a dynsym index.
2454 void
2455 set_needs_dynsym_index()
2456 { this->needs_dynsym_index_ = true; }
2458 // Return whether we need a dynsym index.
2459 bool
2460 needs_dynsym_index() const
2461 { return this->needs_dynsym_index_; }
2463 // Get the dynsym index.
2464 unsigned int
2465 dynsym_index() const
2467 gold_assert(this->dynsym_index_ != 0);
2468 return this->dynsym_index_;
2471 // Set the dynsym index.
2472 void
2473 set_dynsym_index(unsigned int index)
2475 gold_assert(index != 0);
2476 this->dynsym_index_ = index;
2479 // Return whether the input sections sections attachd to this output
2480 // section may require sorting. This is used to handle constructor
2481 // priorities compatibly with GNU ld.
2482 bool
2483 may_sort_attached_input_sections() const
2484 { return this->may_sort_attached_input_sections_; }
2486 // Record that the input sections attached to this output section
2487 // may require sorting.
2488 void
2489 set_may_sort_attached_input_sections()
2490 { this->may_sort_attached_input_sections_ = true; }
2492 // Return whether the input sections attached to this output section
2493 // require sorting. This is used to handle constructor priorities
2494 // compatibly with GNU ld.
2495 bool
2496 must_sort_attached_input_sections() const
2497 { return this->must_sort_attached_input_sections_; }
2499 // Record that the input sections attached to this output section
2500 // require sorting.
2501 void
2502 set_must_sort_attached_input_sections()
2503 { this->must_sort_attached_input_sections_ = true; }
2505 // Return whether this section holds relro data--data which has
2506 // dynamic relocations but which may be marked read-only after the
2507 // dynamic relocations have been completed.
2508 bool
2509 is_relro() const
2510 { return this->is_relro_; }
2512 // Record that this section holds relro data.
2513 void
2514 set_is_relro()
2515 { this->is_relro_ = true; }
2517 // Record that this section does not hold relro data.
2518 void
2519 clear_is_relro()
2520 { this->is_relro_ = false; }
2522 // True if this section holds relro local data--relro data for which
2523 // the dynamic relocations are all RELATIVE relocations.
2524 bool
2525 is_relro_local() const
2526 { return this->is_relro_local_; }
2528 // Record that this section holds relro local data.
2529 void
2530 set_is_relro_local()
2531 { this->is_relro_local_ = true; }
2533 // True if this must be the last relro section.
2534 bool
2535 is_last_relro() const
2536 { return this->is_last_relro_; }
2538 // Record that this must be the last relro section.
2539 void
2540 set_is_last_relro()
2542 gold_assert(this->is_relro_);
2543 this->is_last_relro_ = true;
2546 // True if this must be the first section following the relro sections.
2547 bool
2548 is_first_non_relro() const
2550 gold_assert(!this->is_relro_);
2551 return this->is_first_non_relro_;
2554 // Record that this must be the first non-relro section.
2555 void
2556 set_is_first_non_relro()
2558 gold_assert(!this->is_relro_);
2559 this->is_first_non_relro_ = true;
2562 // True if this is a small section: a section which holds small
2563 // variables.
2564 bool
2565 is_small_section() const
2566 { return this->is_small_section_; }
2568 // Record that this is a small section.
2569 void
2570 set_is_small_section()
2571 { this->is_small_section_ = true; }
2573 // True if this is a large section: a section which holds large
2574 // variables.
2575 bool
2576 is_large_section() const
2577 { return this->is_large_section_; }
2579 // Record that this is a large section.
2580 void
2581 set_is_large_section()
2582 { this->is_large_section_ = true; }
2584 // True if this is a large data (not BSS) section.
2585 bool
2586 is_large_data_section()
2587 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2589 // True if this is the .interp section which goes into the PT_INTERP
2590 // segment.
2591 bool
2592 is_interp() const
2593 { return this->is_interp_; }
2595 // Record that this is the interp section.
2596 void
2597 set_is_interp()
2598 { this->is_interp_ = true; }
2600 // True if this is a section used by the dynamic linker.
2601 bool
2602 is_dynamic_linker_section() const
2603 { return this->is_dynamic_linker_section_; }
2605 // Record that this is a section used by the dynamic linker.
2606 void
2607 set_is_dynamic_linker_section()
2608 { this->is_dynamic_linker_section_ = true; }
2610 // Return whether this section should be written after all the input
2611 // sections are complete.
2612 bool
2613 after_input_sections() const
2614 { return this->after_input_sections_; }
2616 // Record that this section should be written after all the input
2617 // sections are complete.
2618 void
2619 set_after_input_sections()
2620 { this->after_input_sections_ = true; }
2622 // Return whether this section requires postprocessing after all
2623 // relocations have been applied.
2624 bool
2625 requires_postprocessing() const
2626 { return this->requires_postprocessing_; }
2628 // If a section requires postprocessing, return the buffer to use.
2629 unsigned char*
2630 postprocessing_buffer() const
2632 gold_assert(this->postprocessing_buffer_ != NULL);
2633 return this->postprocessing_buffer_;
2636 // If a section requires postprocessing, create the buffer to use.
2637 void
2638 create_postprocessing_buffer();
2640 // If a section requires postprocessing, this is the size of the
2641 // buffer to which relocations should be applied.
2642 off_t
2643 postprocessing_buffer_size() const
2644 { return this->current_data_size_for_child(); }
2646 // Modify the section name. This is only permitted for an
2647 // unallocated section, and only before the size has been finalized.
2648 // Otherwise the name will not get into Layout::namepool_.
2649 void
2650 set_name(const char* newname)
2652 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2653 gold_assert(!this->is_data_size_valid());
2654 this->name_ = newname;
2657 // Return whether the offset OFFSET in the input section SHNDX in
2658 // object OBJECT is being included in the link.
2659 bool
2660 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2661 off_t offset) const;
2663 // Return the offset within the output section of OFFSET relative to
2664 // the start of input section SHNDX in object OBJECT.
2665 section_offset_type
2666 output_offset(const Relobj* object, unsigned int shndx,
2667 section_offset_type offset) const;
2669 // Return the output virtual address of OFFSET relative to the start
2670 // of input section SHNDX in object OBJECT.
2671 uint64_t
2672 output_address(const Relobj* object, unsigned int shndx,
2673 off_t offset) const;
2675 // Look for the merged section for input section SHNDX in object
2676 // OBJECT. If found, return true, and set *ADDR to the address of
2677 // the start of the merged section. This is not necessary the
2678 // output offset corresponding to input offset 0 in the section,
2679 // since the section may be mapped arbitrarily.
2680 bool
2681 find_starting_output_address(const Relobj* object, unsigned int shndx,
2682 uint64_t* addr) const;
2684 // Record that this output section was found in the SECTIONS clause
2685 // of a linker script.
2686 void
2687 set_found_in_sections_clause()
2688 { this->found_in_sections_clause_ = true; }
2690 // Return whether this output section was found in the SECTIONS
2691 // clause of a linker script.
2692 bool
2693 found_in_sections_clause() const
2694 { return this->found_in_sections_clause_; }
2696 // Write the section header into *OPHDR.
2697 template<int size, bool big_endian>
2698 void
2699 write_header(const Layout*, const Stringpool*,
2700 elfcpp::Shdr_write<size, big_endian>*) const;
2702 // The next few calls are for linker script support.
2704 // We need to export the input sections to linker scripts. Previously
2705 // we export a pair of Relobj pointer and section index. We now need to
2706 // handle relaxed input sections as well. So we use this class.
2707 class Simple_input_section
2709 private:
2710 static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2712 public:
2713 Simple_input_section(Relobj *relobj, unsigned int shndx)
2714 : shndx_(shndx)
2716 gold_assert(shndx != invalid_shndx);
2717 this->u_.relobj = relobj;
2720 Simple_input_section(Output_relaxed_input_section* section)
2721 : shndx_(invalid_shndx)
2722 { this->u_.relaxed_input_section = section; }
2724 // Whether this is a relaxed section.
2725 bool
2726 is_relaxed_input_section() const
2727 { return this->shndx_ == invalid_shndx; }
2729 // Return object of an input section.
2730 Relobj*
2731 relobj() const
2733 return ((this->shndx_ != invalid_shndx)
2734 ? this->u_.relobj
2735 : this->u_.relaxed_input_section->relobj());
2738 // Return index of an input section.
2739 unsigned int
2740 shndx() const
2742 return ((this->shndx_ != invalid_shndx)
2743 ? this->shndx_
2744 : this->u_.relaxed_input_section->shndx());
2747 // Return the Output_relaxed_input_section object of a relaxed section.
2748 Output_relaxed_input_section*
2749 relaxed_input_section() const
2751 gold_assert(this->shndx_ == invalid_shndx);
2752 return this->u_.relaxed_input_section;
2755 private:
2756 // Pointer to either an Relobj or an Output_relaxed_input_section.
2757 union
2759 Relobj* relobj;
2760 Output_relaxed_input_section* relaxed_input_section;
2761 } u_;
2762 // Section index for an non-relaxed section or invalid_shndx for
2763 // a relaxed section.
2764 unsigned int shndx_;
2767 // Store the list of input sections for this Output_section into the
2768 // list passed in. This removes the input sections, leaving only
2769 // any Output_section_data elements. This returns the size of those
2770 // Output_section_data elements. ADDRESS is the address of this
2771 // output section. FILL is the fill value to use, in case there are
2772 // any spaces between the remaining Output_section_data elements.
2773 uint64_t
2774 get_input_sections(uint64_t address, const std::string& fill,
2775 std::list<Simple_input_section>*);
2777 // Add an input section from a script.
2778 void
2779 add_input_section_for_script(const Simple_input_section& input_section,
2780 off_t data_size, uint64_t addralign);
2782 // Set the current size of the output section.
2783 void
2784 set_current_data_size(off_t size)
2785 { this->set_current_data_size_for_child(size); }
2787 // Get the current size of the output section.
2788 off_t
2789 current_data_size() const
2790 { return this->current_data_size_for_child(); }
2792 // End of linker script support.
2794 // Save states before doing section layout.
2795 // This is used for relaxation.
2796 void
2797 save_states();
2799 // Restore states prior to section layout.
2800 void
2801 restore_states();
2803 // Convert existing input sections to relaxed input sections.
2804 void
2805 convert_input_sections_to_relaxed_sections(
2806 const std::vector<Output_relaxed_input_section*>& sections);
2808 // Find a relaxed input section to an input section in OBJECT
2809 // with index SHNDX. Return NULL if none is found.
2810 const Output_relaxed_input_section*
2811 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
2813 // Print merge statistics to stderr.
2814 void
2815 print_merge_stats();
2817 protected:
2818 // Return the output section--i.e., the object itself.
2819 Output_section*
2820 do_output_section()
2821 { return this; }
2823 const Output_section*
2824 do_output_section() const
2825 { return this; }
2827 // Return the section index in the output file.
2828 unsigned int
2829 do_out_shndx() const
2831 gold_assert(this->out_shndx_ != -1U);
2832 return this->out_shndx_;
2835 // Set the output section index.
2836 void
2837 do_set_out_shndx(unsigned int shndx)
2839 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2840 this->out_shndx_ = shndx;
2843 // Set the final data size of the Output_section. For a typical
2844 // Output_section, there is nothing to do, but if there are any
2845 // Output_section_data objects we need to set their final addresses
2846 // here.
2847 virtual void
2848 set_final_data_size();
2850 // Reset the address and file offset.
2851 void
2852 do_reset_address_and_file_offset();
2854 // Return true if address and file offset already have reset values. In
2855 // other words, calling reset_address_and_file_offset will not change them.
2856 bool
2857 do_address_and_file_offset_have_reset_values() const;
2859 // Write the data to the file. For a typical Output_section, this
2860 // does nothing: the data is written out by calling Object::Relocate
2861 // on each input object. But if there are any Output_section_data
2862 // objects we do need to write them out here.
2863 virtual void
2864 do_write(Output_file*);
2866 // Return the address alignment--function required by parent class.
2867 uint64_t
2868 do_addralign() const
2869 { return this->addralign_; }
2871 // Return whether there is a load address.
2872 bool
2873 do_has_load_address() const
2874 { return this->has_load_address_; }
2876 // Return the load address.
2877 uint64_t
2878 do_load_address() const
2880 gold_assert(this->has_load_address_);
2881 return this->load_address_;
2884 // Return whether this is an Output_section.
2885 bool
2886 do_is_section() const
2887 { return true; }
2889 // Return whether this is a section of the specified type.
2890 bool
2891 do_is_section_type(elfcpp::Elf_Word type) const
2892 { return this->type_ == type; }
2894 // Return whether the specified section flag is set.
2895 bool
2896 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2897 { return (this->flags_ & flag) != 0; }
2899 // Set the TLS offset. Called only for SHT_TLS sections.
2900 void
2901 do_set_tls_offset(uint64_t tls_base);
2903 // Return the TLS offset, relative to the base of the TLS segment.
2904 // Valid only for SHT_TLS sections.
2905 uint64_t
2906 do_tls_offset() const
2907 { return this->tls_offset_; }
2909 // This may be implemented by a child class.
2910 virtual void
2911 do_finalize_name(Layout*)
2914 // Print to the map file.
2915 virtual void
2916 do_print_to_mapfile(Mapfile*) const;
2918 // Record that this section requires postprocessing after all
2919 // relocations have been applied. This is called by a child class.
2920 void
2921 set_requires_postprocessing()
2923 this->requires_postprocessing_ = true;
2924 this->after_input_sections_ = true;
2927 // Write all the data of an Output_section into the postprocessing
2928 // buffer.
2929 void
2930 write_to_postprocessing_buffer();
2932 // In some cases we need to keep a list of the input sections
2933 // associated with this output section. We only need the list if we
2934 // might have to change the offsets of the input section within the
2935 // output section after we add the input section. The ordinary
2936 // input sections will be written out when we process the object
2937 // file, and as such we don't need to track them here. We do need
2938 // to track Output_section_data objects here. We store instances of
2939 // this structure in a std::vector, so it must be a POD. There can
2940 // be many instances of this structure, so we use a union to save
2941 // some space.
2942 class Input_section
2944 public:
2945 Input_section()
2946 : shndx_(0), p2align_(0)
2948 this->u1_.data_size = 0;
2949 this->u2_.object = NULL;
2952 // For an ordinary input section.
2953 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2954 uint64_t addralign)
2955 : shndx_(shndx),
2956 p2align_(ffsll(static_cast<long long>(addralign)))
2958 gold_assert(shndx != OUTPUT_SECTION_CODE
2959 && shndx != MERGE_DATA_SECTION_CODE
2960 && shndx != MERGE_STRING_SECTION_CODE
2961 && shndx != RELAXED_INPUT_SECTION_CODE);
2962 this->u1_.data_size = data_size;
2963 this->u2_.object = object;
2966 // For a non-merge output section.
2967 Input_section(Output_section_data* posd)
2968 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2970 this->u1_.data_size = 0;
2971 this->u2_.posd = posd;
2974 // For a merge section.
2975 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2976 : shndx_(is_string
2977 ? MERGE_STRING_SECTION_CODE
2978 : MERGE_DATA_SECTION_CODE),
2979 p2align_(0)
2981 this->u1_.entsize = entsize;
2982 this->u2_.posd = posd;
2985 // For a relaxed input section.
2986 Input_section(Output_relaxed_input_section *psection)
2987 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2989 this->u1_.data_size = 0;
2990 this->u2_.poris = psection;
2993 // The required alignment.
2994 uint64_t
2995 addralign() const
2997 if (!this->is_input_section())
2998 return this->u2_.posd->addralign();
2999 return (this->p2align_ == 0
3001 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
3004 // Return the required size.
3005 off_t
3006 data_size() const;
3008 // Whether this is an input section.
3009 bool
3010 is_input_section() const
3012 return (this->shndx_ != OUTPUT_SECTION_CODE
3013 && this->shndx_ != MERGE_DATA_SECTION_CODE
3014 && this->shndx_ != MERGE_STRING_SECTION_CODE
3015 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3018 // Return whether this is a merge section which matches the
3019 // parameters.
3020 bool
3021 is_merge_section(bool is_string, uint64_t entsize,
3022 uint64_t addralign) const
3024 return (this->shndx_ == (is_string
3025 ? MERGE_STRING_SECTION_CODE
3026 : MERGE_DATA_SECTION_CODE)
3027 && this->u1_.entsize == entsize
3028 && this->addralign() == addralign);
3031 // Return whether this is a relaxed input section.
3032 bool
3033 is_relaxed_input_section() const
3034 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3036 // Return whether this is a generic Output_section_data.
3037 bool
3038 is_output_section_data() const
3040 return this->shndx_ == OUTPUT_SECTION_CODE;
3043 // Return the object for an input section.
3044 Relobj*
3045 relobj() const
3047 if (this->is_input_section())
3048 return this->u2_.object;
3049 else if (this->is_relaxed_input_section())
3050 return this->u2_.poris->relobj();
3051 else
3052 gold_unreachable();
3055 // Return the input section index for an input section.
3056 unsigned int
3057 shndx() const
3059 if (this->is_input_section())
3060 return this->shndx_;
3061 else if (this->is_relaxed_input_section())
3062 return this->u2_.poris->shndx();
3063 else
3064 gold_unreachable();
3067 // For non-input-sections, return the associated Output_section_data
3068 // object.
3069 Output_section_data*
3070 output_section_data() const
3072 gold_assert(!this->is_input_section());
3073 return this->u2_.posd;
3076 // Return the Output_relaxed_input_section object.
3077 Output_relaxed_input_section*
3078 relaxed_input_section() const
3080 gold_assert(this->is_relaxed_input_section());
3081 return this->u2_.poris;
3084 // Set the output section.
3085 void
3086 set_output_section(Output_section* os)
3088 gold_assert(!this->is_input_section());
3089 Output_section_data *posd =
3090 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3091 posd->set_output_section(os);
3094 // Set the address and file offset. This is called during
3095 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3096 // the enclosing section.
3097 void
3098 set_address_and_file_offset(uint64_t address, off_t file_offset,
3099 off_t section_file_offset);
3101 // Reset the address and file offset.
3102 void
3103 reset_address_and_file_offset();
3105 // Finalize the data size.
3106 void
3107 finalize_data_size();
3109 // Add an input section, for SHF_MERGE sections.
3110 bool
3111 add_input_section(Relobj* object, unsigned int shndx)
3113 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3114 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3115 return this->u2_.posd->add_input_section(object, shndx);
3118 // Given an input OBJECT, an input section index SHNDX within that
3119 // object, and an OFFSET relative to the start of that input
3120 // section, return whether or not the output offset is known. If
3121 // this function returns true, it sets *POUTPUT to the offset in
3122 // the output section, relative to the start of the input section
3123 // in the output section. *POUTPUT may be different from OFFSET
3124 // for a merged section.
3125 bool
3126 output_offset(const Relobj* object, unsigned int shndx,
3127 section_offset_type offset,
3128 section_offset_type *poutput) const;
3130 // Return whether this is the merge section for the input section
3131 // SHNDX in OBJECT.
3132 bool
3133 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3135 // Write out the data. This does nothing for an input section.
3136 void
3137 write(Output_file*);
3139 // Write the data to a buffer. This does nothing for an input
3140 // section.
3141 void
3142 write_to_buffer(unsigned char*);
3144 // Print to a map file.
3145 void
3146 print_to_mapfile(Mapfile*) const;
3148 // Print statistics about merge sections to stderr.
3149 void
3150 print_merge_stats(const char* section_name)
3152 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3153 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3154 this->u2_.posd->print_merge_stats(section_name);
3157 private:
3158 // Code values which appear in shndx_. If the value is not one of
3159 // these codes, it is the input section index in the object file.
3160 enum
3162 // An Output_section_data.
3163 OUTPUT_SECTION_CODE = -1U,
3164 // An Output_section_data for an SHF_MERGE section with
3165 // SHF_STRINGS not set.
3166 MERGE_DATA_SECTION_CODE = -2U,
3167 // An Output_section_data for an SHF_MERGE section with
3168 // SHF_STRINGS set.
3169 MERGE_STRING_SECTION_CODE = -3U,
3170 // An Output_section_data for a relaxed input section.
3171 RELAXED_INPUT_SECTION_CODE = -4U
3174 // For an ordinary input section, this is the section index in the
3175 // input file. For an Output_section_data, this is
3176 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3177 // MERGE_STRING_SECTION_CODE.
3178 unsigned int shndx_;
3179 // The required alignment, stored as a power of 2.
3180 unsigned int p2align_;
3181 union
3183 // For an ordinary input section, the section size.
3184 off_t data_size;
3185 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3186 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3187 // entity size.
3188 uint64_t entsize;
3189 } u1_;
3190 union
3192 // For an ordinary input section, the object which holds the
3193 // input section.
3194 Relobj* object;
3195 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3196 // MERGE_STRING_SECTION_CODE, the data.
3197 Output_section_data* posd;
3198 // For RELAXED_INPUT_SECTION_CODE, the data.
3199 Output_relaxed_input_section* poris;
3200 } u2_;
3203 typedef std::vector<Input_section> Input_section_list;
3205 // Allow a child class to access the input sections.
3206 const Input_section_list&
3207 input_sections() const
3208 { return this->input_sections_; }
3210 private:
3211 // We only save enough information to undo the effects of section layout.
3212 class Checkpoint_output_section
3214 public:
3215 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3216 const Input_section_list& input_sections,
3217 off_t first_input_offset,
3218 bool attached_input_sections_are_sorted)
3219 : addralign_(addralign), flags_(flags),
3220 input_sections_(input_sections),
3221 input_sections_size_(input_sections_.size()),
3222 input_sections_copy_(), first_input_offset_(first_input_offset),
3223 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3226 virtual
3227 ~Checkpoint_output_section()
3230 // Return the address alignment.
3231 uint64_t
3232 addralign() const
3233 { return this->addralign_; }
3235 // Return the section flags.
3236 elfcpp::Elf_Xword
3237 flags() const
3238 { return this->flags_; }
3240 // Return a reference to the input section list copy.
3241 Input_section_list*
3242 input_sections()
3243 { return &this->input_sections_copy_; }
3245 // Return the size of input_sections at the time when checkpoint is
3246 // taken.
3247 size_t
3248 input_sections_size() const
3249 { return this->input_sections_size_; }
3251 // Whether input sections are copied.
3252 bool
3253 input_sections_saved() const
3254 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3256 off_t
3257 first_input_offset() const
3258 { return this->first_input_offset_; }
3260 bool
3261 attached_input_sections_are_sorted() const
3262 { return this->attached_input_sections_are_sorted_; }
3264 // Save input sections.
3265 void
3266 save_input_sections()
3268 this->input_sections_copy_.reserve(this->input_sections_size_);
3269 this->input_sections_copy_.clear();
3270 Input_section_list::const_iterator p = this->input_sections_.begin();
3271 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3272 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3273 this->input_sections_copy_.push_back(*p);
3276 private:
3277 // The section alignment.
3278 uint64_t addralign_;
3279 // The section flags.
3280 elfcpp::Elf_Xword flags_;
3281 // Reference to the input sections to be checkpointed.
3282 const Input_section_list& input_sections_;
3283 // Size of the checkpointed portion of input_sections_;
3284 size_t input_sections_size_;
3285 // Copy of input sections.
3286 Input_section_list input_sections_copy_;
3287 // The offset of the first entry in input_sections_.
3288 off_t first_input_offset_;
3289 // True if the input sections attached to this output section have
3290 // already been sorted.
3291 bool attached_input_sections_are_sorted_;
3294 // This class is used to sort the input sections.
3295 class Input_section_sort_entry;
3297 // This is the sort comparison function.
3298 struct Input_section_sort_compare
3300 bool
3301 operator()(const Input_section_sort_entry&,
3302 const Input_section_sort_entry&) const;
3305 // Fill data. This is used to fill in data between input sections.
3306 // It is also used for data statements (BYTE, WORD, etc.) in linker
3307 // scripts. When we have to keep track of the input sections, we
3308 // can use an Output_data_const, but we don't want to have to keep
3309 // track of input sections just to implement fills.
3310 class Fill
3312 public:
3313 Fill(off_t section_offset, off_t length)
3314 : section_offset_(section_offset),
3315 length_(convert_to_section_size_type(length))
3318 // Return section offset.
3319 off_t
3320 section_offset() const
3321 { return this->section_offset_; }
3323 // Return fill length.
3324 section_size_type
3325 length() const
3326 { return this->length_; }
3328 private:
3329 // The offset within the output section.
3330 off_t section_offset_;
3331 // The length of the space to fill.
3332 section_size_type length_;
3335 typedef std::vector<Fill> Fill_list;
3337 // This class describes properties of merge data sections. It is used
3338 // as a key type for maps.
3339 class Merge_section_properties
3341 public:
3342 Merge_section_properties(bool is_string, uint64_t entsize,
3343 uint64_t addralign)
3344 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
3347 // Whether this equals to another Merge_section_properties MSP.
3348 bool
3349 eq(const Merge_section_properties& msp) const
3351 return ((this->is_string_ == msp.is_string_)
3352 && (this->entsize_ == msp.entsize_)
3353 && (this->addralign_ == msp.addralign_));
3356 // Compute a hash value for this using 64-bit FNV-1a hash.
3357 size_t
3358 hash_value() const
3360 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
3361 uint64_t prime = 1099511628211ULL;
3362 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
3363 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
3364 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3365 return h;
3368 // Functors for associative containers.
3369 struct equal_to
3371 bool
3372 operator()(const Merge_section_properties& msp1,
3373 const Merge_section_properties& msp2) const
3374 { return msp1.eq(msp2); }
3377 struct hash
3379 size_t
3380 operator()(const Merge_section_properties& msp) const
3381 { return msp.hash_value(); }
3384 private:
3385 // Whether this merge data section is for strings.
3386 bool is_string_;
3387 // Entsize of this merge data section.
3388 uint64_t entsize_;
3389 // Address alignment.
3390 uint64_t addralign_;
3393 // Map that link Merge_section_properties to Output_merge_base.
3394 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3395 Merge_section_properties::hash,
3396 Merge_section_properties::equal_to>
3397 Merge_section_by_properties_map;
3399 // Map that link Input_section_specifier to Output_section_data.
3400 typedef Unordered_map<Input_section_specifier, Output_section_data*,
3401 Input_section_specifier::hash,
3402 Input_section_specifier::equal_to>
3403 Output_section_data_by_input_section_map;
3405 // Map that link Input_section_specifier to Output_relaxed_input_section.
3406 typedef Unordered_map<Input_section_specifier, Output_relaxed_input_section*,
3407 Input_section_specifier::hash,
3408 Input_section_specifier::equal_to>
3409 Output_relaxed_input_section_by_input_section_map;
3411 // Map used during relaxation of existing sections. This map
3412 // an input section specifier to an input section list index.
3413 // We assume that Input_section_list is a vector.
3414 typedef Unordered_map<Input_section_specifier, size_t,
3415 Input_section_specifier::hash,
3416 Input_section_specifier::equal_to>
3417 Relaxation_map;
3419 // Add a new output section by Input_section.
3420 void
3421 add_output_section_data(Input_section*);
3423 // Add an SHF_MERGE input section. Returns true if the section was
3424 // handled.
3425 bool
3426 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3427 uint64_t entsize, uint64_t addralign);
3429 // Add an output SHF_MERGE section POSD to this output section.
3430 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3431 // ENTSIZE is the entity size. This returns the entry added to
3432 // input_sections_.
3433 void
3434 add_output_merge_section(Output_section_data* posd, bool is_string,
3435 uint64_t entsize);
3437 // Sort the attached input sections.
3438 void
3439 sort_attached_input_sections();
3441 // Find the merge section into which an input section with index SHNDX in
3442 // OBJECT has been added. Return NULL if none found.
3443 Output_section_data*
3444 find_merge_section(const Relobj* object, unsigned int shndx) const;
3446 // Build a relaxation map.
3447 void
3448 build_relaxation_map(
3449 const Input_section_list& input_sections,
3450 size_t limit,
3451 Relaxation_map* map) const;
3453 // Convert input sections in an input section list into relaxed sections.
3454 void
3455 convert_input_sections_in_list_to_relaxed_sections(
3456 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3457 const Relaxation_map& map,
3458 Input_section_list* input_sections);
3460 // Most of these fields are only valid after layout.
3462 // The name of the section. This will point into a Stringpool.
3463 const char* name_;
3464 // The section address is in the parent class.
3465 // The section alignment.
3466 uint64_t addralign_;
3467 // The section entry size.
3468 uint64_t entsize_;
3469 // The load address. This is only used when using a linker script
3470 // with a SECTIONS clause. The has_load_address_ field indicates
3471 // whether this field is valid.
3472 uint64_t load_address_;
3473 // The file offset is in the parent class.
3474 // Set the section link field to the index of this section.
3475 const Output_data* link_section_;
3476 // If link_section_ is NULL, this is the link field.
3477 unsigned int link_;
3478 // Set the section info field to the index of this section.
3479 const Output_section* info_section_;
3480 // If info_section_ is NULL, set the info field to the symbol table
3481 // index of this symbol.
3482 const Symbol* info_symndx_;
3483 // If info_section_ and info_symndx_ are NULL, this is the section
3484 // info field.
3485 unsigned int info_;
3486 // The section type.
3487 const elfcpp::Elf_Word type_;
3488 // The section flags.
3489 elfcpp::Elf_Xword flags_;
3490 // The section index.
3491 unsigned int out_shndx_;
3492 // If there is a STT_SECTION for this output section in the normal
3493 // symbol table, this is the symbol index. This starts out as zero.
3494 // It is initialized in Layout::finalize() to be the index, or -1U
3495 // if there isn't one.
3496 unsigned int symtab_index_;
3497 // If there is a STT_SECTION for this output section in the dynamic
3498 // symbol table, this is the symbol index. This starts out as zero.
3499 // It is initialized in Layout::finalize() to be the index, or -1U
3500 // if there isn't one.
3501 unsigned int dynsym_index_;
3502 // The input sections. This will be empty in cases where we don't
3503 // need to keep track of them.
3504 Input_section_list input_sections_;
3505 // The offset of the first entry in input_sections_.
3506 off_t first_input_offset_;
3507 // The fill data. This is separate from input_sections_ because we
3508 // often will need fill sections without needing to keep track of
3509 // input sections.
3510 Fill_list fills_;
3511 // If the section requires postprocessing, this buffer holds the
3512 // section contents during relocation.
3513 unsigned char* postprocessing_buffer_;
3514 // Whether this output section needs a STT_SECTION symbol in the
3515 // normal symbol table. This will be true if there is a relocation
3516 // which needs it.
3517 bool needs_symtab_index_ : 1;
3518 // Whether this output section needs a STT_SECTION symbol in the
3519 // dynamic symbol table. This will be true if there is a dynamic
3520 // relocation which needs it.
3521 bool needs_dynsym_index_ : 1;
3522 // Whether the link field of this output section should point to the
3523 // normal symbol table.
3524 bool should_link_to_symtab_ : 1;
3525 // Whether the link field of this output section should point to the
3526 // dynamic symbol table.
3527 bool should_link_to_dynsym_ : 1;
3528 // Whether this section should be written after all the input
3529 // sections are complete.
3530 bool after_input_sections_ : 1;
3531 // Whether this section requires post processing after all
3532 // relocations have been applied.
3533 bool requires_postprocessing_ : 1;
3534 // Whether an input section was mapped to this output section
3535 // because of a SECTIONS clause in a linker script.
3536 bool found_in_sections_clause_ : 1;
3537 // Whether this section has an explicitly specified load address.
3538 bool has_load_address_ : 1;
3539 // True if the info_section_ field means the section index of the
3540 // section, false if it means the symbol index of the corresponding
3541 // section symbol.
3542 bool info_uses_section_index_ : 1;
3543 // True if the input sections attached to this output section may
3544 // need sorting.
3545 bool may_sort_attached_input_sections_ : 1;
3546 // True if the input sections attached to this output section must
3547 // be sorted.
3548 bool must_sort_attached_input_sections_ : 1;
3549 // True if the input sections attached to this output section have
3550 // already been sorted.
3551 bool attached_input_sections_are_sorted_ : 1;
3552 // True if this section holds relro data.
3553 bool is_relro_ : 1;
3554 // True if this section holds relro local data.
3555 bool is_relro_local_ : 1;
3556 // True if this must be the last relro section.
3557 bool is_last_relro_ : 1;
3558 // True if this must be the first section after the relro sections.
3559 bool is_first_non_relro_ : 1;
3560 // True if this is a small section.
3561 bool is_small_section_ : 1;
3562 // True if this is a large section.
3563 bool is_large_section_ : 1;
3564 // True if this is the .interp section going into the PT_INTERP
3565 // segment.
3566 bool is_interp_ : 1;
3567 // True if this is section is read by the dynamic linker.
3568 bool is_dynamic_linker_section_ : 1;
3569 // Whether code-fills are generated at write.
3570 bool generate_code_fills_at_write_ : 1;
3571 // Whether the entry size field should be zero.
3572 bool is_entsize_zero_ : 1;
3573 // For SHT_TLS sections, the offset of this section relative to the base
3574 // of the TLS segment.
3575 uint64_t tls_offset_;
3576 // Saved checkpoint.
3577 Checkpoint_output_section* checkpoint_;
3578 // Map from input sections to merge sections.
3579 Output_section_data_by_input_section_map merge_section_map_;
3580 // Map from merge section properties to merge_sections;
3581 Merge_section_by_properties_map merge_section_by_properties_map_;
3582 // Map from input sections to relaxed input sections. This is mutable
3583 // because it is updated lazily. We may need to update it in a
3584 // const qualified method.
3585 mutable Output_relaxed_input_section_by_input_section_map
3586 relaxed_input_section_map_;
3587 // Whether relaxed_input_section_map_ is valid.
3588 mutable bool is_relaxed_input_section_map_valid_;
3591 // An output segment. PT_LOAD segments are built from collections of
3592 // output sections. Other segments typically point within PT_LOAD
3593 // segments, and are built directly as needed.
3595 // NOTE: We want to use the copy constructor for this class. During
3596 // relaxation, we may try built the segments multiple times. We do
3597 // that by copying the original segment list before lay-out, doing
3598 // a trial lay-out and roll-back to the saved copied if we need to
3599 // to the lay-out again.
3601 class Output_segment
3603 public:
3604 // Create an output segment, specifying the type and flags.
3605 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3607 // Return the virtual address.
3608 uint64_t
3609 vaddr() const
3610 { return this->vaddr_; }
3612 // Return the physical address.
3613 uint64_t
3614 paddr() const
3615 { return this->paddr_; }
3617 // Return the segment type.
3618 elfcpp::Elf_Word
3619 type() const
3620 { return this->type_; }
3622 // Return the segment flags.
3623 elfcpp::Elf_Word
3624 flags() const
3625 { return this->flags_; }
3627 // Return the memory size.
3628 uint64_t
3629 memsz() const
3630 { return this->memsz_; }
3632 // Return the file size.
3633 off_t
3634 filesz() const
3635 { return this->filesz_; }
3637 // Return the file offset.
3638 off_t
3639 offset() const
3640 { return this->offset_; }
3642 // Whether this is a segment created to hold large data sections.
3643 bool
3644 is_large_data_segment() const
3645 { return this->is_large_data_segment_; }
3647 // Record that this is a segment created to hold large data
3648 // sections.
3649 void
3650 set_is_large_data_segment()
3651 { this->is_large_data_segment_ = true; }
3653 // Return the maximum alignment of the Output_data.
3654 uint64_t
3655 maximum_alignment();
3657 // Add the Output_section OS to this segment. SEG_FLAGS is the
3658 // segment flags to use. DO_SORT is true if we should sort the
3659 // placement of the input section for more efficient generated code.
3660 void
3661 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags,
3662 bool do_sort);
3664 // Remove an Output_section from this segment. It is an error if it
3665 // is not present.
3666 void
3667 remove_output_section(Output_section* os);
3669 // Add an Output_data (which need not be an Output_section) to the
3670 // start of this segment.
3671 void
3672 add_initial_output_data(Output_data*);
3674 // Return true if this segment has any sections which hold actual
3675 // data, rather than being a BSS section.
3676 bool
3677 has_any_data_sections() const
3678 { return !this->output_data_.empty(); }
3680 // Return the number of dynamic relocations applied to this segment.
3681 unsigned int
3682 dynamic_reloc_count() const;
3684 // Return the address of the first section.
3685 uint64_t
3686 first_section_load_address() const;
3688 // Return whether the addresses have been set already.
3689 bool
3690 are_addresses_set() const
3691 { return this->are_addresses_set_; }
3693 // Set the addresses.
3694 void
3695 set_addresses(uint64_t vaddr, uint64_t paddr)
3697 this->vaddr_ = vaddr;
3698 this->paddr_ = paddr;
3699 this->are_addresses_set_ = true;
3702 // Update the flags for the flags of an output section added to this
3703 // segment.
3704 void
3705 update_flags_for_output_section(elfcpp::Elf_Xword flags)
3707 // The ELF ABI specifies that a PT_TLS segment should always have
3708 // PF_R as the flags.
3709 if (this->type() != elfcpp::PT_TLS)
3710 this->flags_ |= flags;
3713 // Set the segment flags. This is only used if we have a PHDRS
3714 // clause which explicitly specifies the flags.
3715 void
3716 set_flags(elfcpp::Elf_Word flags)
3717 { this->flags_ = flags; }
3719 // Set the address of the segment to ADDR and the offset to *POFF
3720 // and set the addresses and offsets of all contained output
3721 // sections accordingly. Set the section indexes of all contained
3722 // output sections starting with *PSHNDX. If RESET is true, first
3723 // reset the addresses of the contained sections. Return the
3724 // address of the immediately following segment. Update *POFF and
3725 // *PSHNDX. This should only be called for a PT_LOAD segment.
3726 uint64_t
3727 set_section_addresses(const Layout*, bool reset, uint64_t addr,
3728 unsigned int increase_relro, off_t* poff,
3729 unsigned int* pshndx);
3731 // Set the minimum alignment of this segment. This may be adjusted
3732 // upward based on the section alignments.
3733 void
3734 set_minimum_p_align(uint64_t align)
3735 { this->min_p_align_ = align; }
3737 // Set the offset of this segment based on the section. This should
3738 // only be called for a non-PT_LOAD segment.
3739 void
3740 set_offset(unsigned int increase);
3742 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3743 void
3744 set_tls_offsets();
3746 // Return the number of output sections.
3747 unsigned int
3748 output_section_count() const;
3750 // Return the section attached to the list segment with the lowest
3751 // load address. This is used when handling a PHDRS clause in a
3752 // linker script.
3753 Output_section*
3754 section_with_lowest_load_address() const;
3756 // Write the segment header into *OPHDR.
3757 template<int size, bool big_endian>
3758 void
3759 write_header(elfcpp::Phdr_write<size, big_endian>*);
3761 // Write the section headers of associated sections into V.
3762 template<int size, bool big_endian>
3763 unsigned char*
3764 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3765 unsigned int* pshndx) const;
3767 // Print the output sections in the map file.
3768 void
3769 print_sections_to_mapfile(Mapfile*) const;
3771 private:
3772 typedef std::list<Output_data*> Output_data_list;
3774 // Find the maximum alignment in an Output_data_list.
3775 static uint64_t
3776 maximum_alignment_list(const Output_data_list*);
3778 // Return whether the first data section is a relro section.
3779 bool
3780 is_first_section_relro() const;
3782 // Set the section addresses in an Output_data_list.
3783 uint64_t
3784 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3785 uint64_t addr, off_t* poff, unsigned int* pshndx,
3786 bool* in_tls);
3788 // Return the number of Output_sections in an Output_data_list.
3789 unsigned int
3790 output_section_count_list(const Output_data_list*) const;
3792 // Return the number of dynamic relocs in an Output_data_list.
3793 unsigned int
3794 dynamic_reloc_count_list(const Output_data_list*) const;
3796 // Find the section with the lowest load address in an
3797 // Output_data_list.
3798 void
3799 lowest_load_address_in_list(const Output_data_list* pdl,
3800 Output_section** found,
3801 uint64_t* found_lma) const;
3803 // Write the section headers in the list into V.
3804 template<int size, bool big_endian>
3805 unsigned char*
3806 write_section_headers_list(const Layout*, const Stringpool*,
3807 const Output_data_list*, unsigned char* v,
3808 unsigned int* pshdx) const;
3810 // Print a section list to the mapfile.
3811 void
3812 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3814 // NOTE: We want to use the copy constructor. Currently, shallow copy
3815 // works for us so we do not need to write our own copy constructor.
3817 // The list of output data with contents attached to this segment.
3818 Output_data_list output_data_;
3819 // The list of output data without contents attached to this segment.
3820 Output_data_list output_bss_;
3821 // The segment virtual address.
3822 uint64_t vaddr_;
3823 // The segment physical address.
3824 uint64_t paddr_;
3825 // The size of the segment in memory.
3826 uint64_t memsz_;
3827 // The maximum section alignment. The is_max_align_known_ field
3828 // indicates whether this has been finalized.
3829 uint64_t max_align_;
3830 // The required minimum value for the p_align field. This is used
3831 // for PT_LOAD segments. Note that this does not mean that
3832 // addresses should be aligned to this value; it means the p_paddr
3833 // and p_vaddr fields must be congruent modulo this value. For
3834 // non-PT_LOAD segments, the dynamic linker works more efficiently
3835 // if the p_align field has the more conventional value, although it
3836 // can align as needed.
3837 uint64_t min_p_align_;
3838 // The offset of the segment data within the file.
3839 off_t offset_;
3840 // The size of the segment data in the file.
3841 off_t filesz_;
3842 // The segment type;
3843 elfcpp::Elf_Word type_;
3844 // The segment flags.
3845 elfcpp::Elf_Word flags_;
3846 // Whether we have finalized max_align_.
3847 bool is_max_align_known_ : 1;
3848 // Whether vaddr and paddr were set by a linker script.
3849 bool are_addresses_set_ : 1;
3850 // Whether this segment holds large data sections.
3851 bool is_large_data_segment_ : 1;
3854 // This class represents the output file.
3856 class Output_file
3858 public:
3859 Output_file(const char* name);
3861 // Indicate that this is a temporary file which should not be
3862 // output.
3863 void
3864 set_is_temporary()
3865 { this->is_temporary_ = true; }
3867 // Try to open an existing file. Returns false if the file doesn't
3868 // exist, has a size of 0 or can't be mmaped. This method is
3869 // thread-unsafe.
3870 bool
3871 open_for_modification();
3873 // Open the output file. FILE_SIZE is the final size of the file.
3874 // If the file already exists, it is deleted/truncated. This method
3875 // is thread-unsafe.
3876 void
3877 open(off_t file_size);
3879 // Resize the output file. This method is thread-unsafe.
3880 void
3881 resize(off_t file_size);
3883 // Close the output file (flushing all buffered data) and make sure
3884 // there are no errors. This method is thread-unsafe.
3885 void
3886 close();
3888 // Return the size of this file.
3889 off_t
3890 filesize()
3891 { return this->file_size_; }
3893 // Return the name of this file.
3894 const char*
3895 filename()
3896 { return this->name_; }
3898 // We currently always use mmap which makes the view handling quite
3899 // simple. In the future we may support other approaches.
3901 // Write data to the output file.
3902 void
3903 write(off_t offset, const void* data, size_t len)
3904 { memcpy(this->base_ + offset, data, len); }
3906 // Get a buffer to use to write to the file, given the offset into
3907 // the file and the size.
3908 unsigned char*
3909 get_output_view(off_t start, size_t size)
3911 gold_assert(start >= 0
3912 && start + static_cast<off_t>(size) <= this->file_size_);
3913 return this->base_ + start;
3916 // VIEW must have been returned by get_output_view. Write the
3917 // buffer to the file, passing in the offset and the size.
3918 void
3919 write_output_view(off_t, size_t, unsigned char*)
3922 // Get a read/write buffer. This is used when we want to write part
3923 // of the file, read it in, and write it again.
3924 unsigned char*
3925 get_input_output_view(off_t start, size_t size)
3926 { return this->get_output_view(start, size); }
3928 // Write a read/write buffer back to the file.
3929 void
3930 write_input_output_view(off_t, size_t, unsigned char*)
3933 // Get a read buffer. This is used when we just want to read part
3934 // of the file back it in.
3935 const unsigned char*
3936 get_input_view(off_t start, size_t size)
3937 { return this->get_output_view(start, size); }
3939 // Release a read bfufer.
3940 void
3941 free_input_view(off_t, size_t, const unsigned char*)
3944 private:
3945 // Map the file into memory or, if that fails, allocate anonymous
3946 // memory.
3947 void
3948 map();
3950 // Allocate anonymous memory for the file.
3951 bool
3952 map_anonymous();
3954 // Map the file into memory.
3955 bool
3956 map_no_anonymous();
3958 // Unmap the file from memory (and flush to disk buffers).
3959 void
3960 unmap();
3962 // File name.
3963 const char* name_;
3964 // File descriptor.
3965 int o_;
3966 // File size.
3967 off_t file_size_;
3968 // Base of file mapped into memory.
3969 unsigned char* base_;
3970 // True iff base_ points to a memory buffer rather than an output file.
3971 bool map_is_anonymous_;
3972 // True if this is a temporary file which should not be output.
3973 bool is_temporary_;
3976 } // End namespace gold.
3978 #endif // !defined(GOLD_OUTPUT_H)