bfd/
[binutils.git] / gold / output.h
blobf9cbfa6eadd69a30939b08c4632812c3e6a51131
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_section;
42 class Relocatable_relocs;
43 class Target;
44 template<int size, bool big_endian>
45 class Sized_target;
46 template<int size, bool big_endian>
47 class Sized_relobj;
49 // An abtract class for data which has to go into the output file.
51 class Output_data
53 public:
54 explicit Output_data()
55 : address_(0), data_size_(0), offset_(-1),
56 is_address_valid_(false), is_data_size_valid_(false),
57 is_offset_valid_(false),
58 dynamic_reloc_count_(0)
59 { }
61 virtual
62 ~Output_data();
64 // Return the address. For allocated sections, this is only valid
65 // after Layout::finalize is finished.
66 uint64_t
67 address() const
69 gold_assert(this->is_address_valid_);
70 return this->address_;
73 // Return the size of the data. For allocated sections, this must
74 // be valid after Layout::finalize calls set_address, but need not
75 // be valid before then.
76 off_t
77 data_size() const
79 gold_assert(this->is_data_size_valid_);
80 return this->data_size_;
83 // Return the file offset. This is only valid after
84 // Layout::finalize is finished. For some non-allocated sections,
85 // it may not be valid until near the end of the link.
86 off_t
87 offset() const
89 gold_assert(this->is_offset_valid_);
90 return this->offset_;
93 // Reset the address and file offset. This essentially disables the
94 // sanity testing about duplicate and unknown settings.
95 void
96 reset_address_and_file_offset()
98 this->is_address_valid_ = false;
99 this->is_offset_valid_ = false;
100 this->is_data_size_valid_ = false;
101 this->do_reset_address_and_file_offset();
104 // Return the required alignment.
105 uint64_t
106 addralign() const
107 { return this->do_addralign(); }
109 // Return whether this has a load address.
110 bool
111 has_load_address() const
112 { return this->do_has_load_address(); }
114 // Return the load address.
115 uint64_t
116 load_address() const
117 { return this->do_load_address(); }
119 // Return whether this is an Output_section.
120 bool
121 is_section() const
122 { return this->do_is_section(); }
124 // Return whether this is an Output_section of the specified type.
125 bool
126 is_section_type(elfcpp::Elf_Word stt) const
127 { return this->do_is_section_type(stt); }
129 // Return whether this is an Output_section with the specified flag
130 // set.
131 bool
132 is_section_flag_set(elfcpp::Elf_Xword shf) const
133 { return this->do_is_section_flag_set(shf); }
135 // Return the output section that this goes in, if there is one.
136 Output_section*
137 output_section()
138 { return this->do_output_section(); }
140 // Return the output section index, if there is an output section.
141 unsigned int
142 out_shndx() const
143 { return this->do_out_shndx(); }
145 // Set the output section index, if this is an output section.
146 void
147 set_out_shndx(unsigned int shndx)
148 { this->do_set_out_shndx(shndx); }
150 // Set the address and file offset of this data, and finalize the
151 // size of the data. This is called during Layout::finalize for
152 // allocated sections.
153 void
154 set_address_and_file_offset(uint64_t addr, off_t off)
156 this->set_address(addr);
157 this->set_file_offset(off);
158 this->finalize_data_size();
161 // Set the address.
162 void
163 set_address(uint64_t addr)
165 gold_assert(!this->is_address_valid_);
166 this->address_ = addr;
167 this->is_address_valid_ = true;
170 // Set the file offset.
171 void
172 set_file_offset(off_t off)
174 gold_assert(!this->is_offset_valid_);
175 this->offset_ = off;
176 this->is_offset_valid_ = true;
179 // Finalize the data size.
180 void
181 finalize_data_size()
183 if (!this->is_data_size_valid_)
185 // Tell the child class to set the data size.
186 this->set_final_data_size();
187 gold_assert(this->is_data_size_valid_);
191 // Set the TLS offset. Called only for SHT_TLS sections.
192 void
193 set_tls_offset(uint64_t tls_base)
194 { this->do_set_tls_offset(tls_base); }
196 // Return the TLS offset, relative to the base of the TLS segment.
197 // Valid only for SHT_TLS sections.
198 uint64_t
199 tls_offset() const
200 { return this->do_tls_offset(); }
202 // Write the data to the output file. This is called after
203 // Layout::finalize is complete.
204 void
205 write(Output_file* file)
206 { this->do_write(file); }
208 // This is called by Layout::finalize to note that the sizes of
209 // allocated sections must now be fixed.
210 static void
211 layout_complete()
212 { Output_data::allocated_sizes_are_fixed = true; }
214 // Used to check that layout has been done.
215 static bool
216 is_layout_complete()
217 { return Output_data::allocated_sizes_are_fixed; }
219 // Count the number of dynamic relocations applied to this section.
220 void
221 add_dynamic_reloc()
222 { ++this->dynamic_reloc_count_; }
224 // Return the number of dynamic relocations applied to this section.
225 unsigned int
226 dynamic_reloc_count() const
227 { return this->dynamic_reloc_count_; }
229 // Whether the address is valid.
230 bool
231 is_address_valid() const
232 { return this->is_address_valid_; }
234 // Whether the file offset is valid.
235 bool
236 is_offset_valid() const
237 { return this->is_offset_valid_; }
239 // Whether the data size is valid.
240 bool
241 is_data_size_valid() const
242 { return this->is_data_size_valid_; }
244 // Print information to the map file.
245 void
246 print_to_mapfile(Mapfile* mapfile) const
247 { return this->do_print_to_mapfile(mapfile); }
249 protected:
250 // Functions that child classes may or in some cases must implement.
252 // Write the data to the output file.
253 virtual void
254 do_write(Output_file*) = 0;
256 // Return the required alignment.
257 virtual uint64_t
258 do_addralign() const = 0;
260 // Return whether this has a load address.
261 virtual bool
262 do_has_load_address() const
263 { return false; }
265 // Return the load address.
266 virtual uint64_t
267 do_load_address() const
268 { gold_unreachable(); }
270 // Return whether this is an Output_section.
271 virtual bool
272 do_is_section() const
273 { return false; }
275 // Return whether this is an Output_section of the specified type.
276 // This only needs to be implement by Output_section.
277 virtual bool
278 do_is_section_type(elfcpp::Elf_Word) const
279 { return false; }
281 // Return whether this is an Output_section with the specific flag
282 // set. This only needs to be implemented by Output_section.
283 virtual bool
284 do_is_section_flag_set(elfcpp::Elf_Xword) const
285 { return false; }
287 // Return the output section, if there is one.
288 virtual Output_section*
289 do_output_section()
290 { return NULL; }
292 // Return the output section index, if there is an output section.
293 virtual unsigned int
294 do_out_shndx() const
295 { gold_unreachable(); }
297 // Set the output section index, if this is an output section.
298 virtual void
299 do_set_out_shndx(unsigned int)
300 { gold_unreachable(); }
302 // This is a hook for derived classes to set the data size. This is
303 // called by finalize_data_size, normally called during
304 // Layout::finalize, when the section address is set.
305 virtual void
306 set_final_data_size()
307 { gold_unreachable(); }
309 // A hook for resetting the address and file offset.
310 virtual void
311 do_reset_address_and_file_offset()
314 // Set the TLS offset. Called only for SHT_TLS sections.
315 virtual void
316 do_set_tls_offset(uint64_t)
317 { gold_unreachable(); }
319 // Return the TLS offset, relative to the base of the TLS segment.
320 // Valid only for SHT_TLS sections.
321 virtual uint64_t
322 do_tls_offset() const
323 { gold_unreachable(); }
325 // Print to the map file. This only needs to be implemented by
326 // classes which may appear in a PT_LOAD segment.
327 virtual void
328 do_print_to_mapfile(Mapfile*) const
329 { gold_unreachable(); }
331 // Functions that child classes may call.
333 // Reset the address. The Output_section class needs this when an
334 // SHF_ALLOC input section is added to an output section which was
335 // formerly not SHF_ALLOC.
336 void
337 mark_address_invalid()
338 { this->is_address_valid_ = false; }
340 // Set the size of the data.
341 void
342 set_data_size(off_t data_size)
344 gold_assert(!this->is_data_size_valid_);
345 this->data_size_ = data_size;
346 this->is_data_size_valid_ = true;
349 // Get the current data size--this is for the convenience of
350 // sections which build up their size over time.
351 off_t
352 current_data_size_for_child() const
353 { return this->data_size_; }
355 // Set the current data size--this is for the convenience of
356 // sections which build up their size over time.
357 void
358 set_current_data_size_for_child(off_t data_size)
360 gold_assert(!this->is_data_size_valid_);
361 this->data_size_ = data_size;
364 // Return default alignment for the target size.
365 static uint64_t
366 default_alignment();
368 // Return default alignment for a specified size--32 or 64.
369 static uint64_t
370 default_alignment_for_size(int size);
372 private:
373 Output_data(const Output_data&);
374 Output_data& operator=(const Output_data&);
376 // This is used for verification, to make sure that we don't try to
377 // change any sizes of allocated sections after we set the section
378 // addresses.
379 static bool allocated_sizes_are_fixed;
381 // Memory address in output file.
382 uint64_t address_;
383 // Size of data in output file.
384 off_t data_size_;
385 // File offset of contents in output file.
386 off_t offset_;
387 // Whether address_ is valid.
388 bool is_address_valid_;
389 // Whether data_size_ is valid.
390 bool is_data_size_valid_;
391 // Whether offset_ is valid.
392 bool is_offset_valid_;
393 // Count of dynamic relocations applied to this section.
394 unsigned int dynamic_reloc_count_;
397 // Output the section headers.
399 class Output_section_headers : public Output_data
401 public:
402 Output_section_headers(const Layout*,
403 const Layout::Segment_list*,
404 const Layout::Section_list*,
405 const Layout::Section_list*,
406 const Stringpool*,
407 const Output_section*);
409 protected:
410 // Write the data to the file.
411 void
412 do_write(Output_file*);
414 // Return the required alignment.
415 uint64_t
416 do_addralign() const
417 { return Output_data::default_alignment(); }
419 // Write to a map file.
420 void
421 do_print_to_mapfile(Mapfile* mapfile) const
422 { mapfile->print_output_data(this, _("** section headers")); }
424 private:
425 // Write the data to the file with the right size and endianness.
426 template<int size, bool big_endian>
427 void
428 do_sized_write(Output_file*);
430 const Layout* layout_;
431 const Layout::Segment_list* segment_list_;
432 const Layout::Section_list* section_list_;
433 const Layout::Section_list* unattached_section_list_;
434 const Stringpool* secnamepool_;
435 const Output_section* shstrtab_section_;
438 // Output the segment headers.
440 class Output_segment_headers : public Output_data
442 public:
443 Output_segment_headers(const Layout::Segment_list& segment_list);
445 protected:
446 // Write the data to the file.
447 void
448 do_write(Output_file*);
450 // Return the required alignment.
451 uint64_t
452 do_addralign() const
453 { return Output_data::default_alignment(); }
455 // Write to a map file.
456 void
457 do_print_to_mapfile(Mapfile* mapfile) const
458 { mapfile->print_output_data(this, _("** segment headers")); }
460 private:
461 // Write the data to the file with the right size and endianness.
462 template<int size, bool big_endian>
463 void
464 do_sized_write(Output_file*);
466 const Layout::Segment_list& segment_list_;
469 // Output the ELF file header.
471 class Output_file_header : public Output_data
473 public:
474 Output_file_header(const Target*,
475 const Symbol_table*,
476 const Output_segment_headers*,
477 const char* entry);
479 // Add information about the section headers. We lay out the ELF
480 // file header before we create the section headers.
481 void set_section_info(const Output_section_headers*,
482 const Output_section* shstrtab);
484 protected:
485 // Write the data to the file.
486 void
487 do_write(Output_file*);
489 // Return the required alignment.
490 uint64_t
491 do_addralign() const
492 { return Output_data::default_alignment(); }
494 // Write to a map file.
495 void
496 do_print_to_mapfile(Mapfile* mapfile) const
497 { mapfile->print_output_data(this, _("** file header")); }
499 private:
500 // Write the data to the file with the right size and endianness.
501 template<int size, bool big_endian>
502 void
503 do_sized_write(Output_file*);
505 // Return the value to use for the entry address.
506 template<int size>
507 typename elfcpp::Elf_types<size>::Elf_Addr
508 entry();
510 const Target* target_;
511 const Symbol_table* symtab_;
512 const Output_segment_headers* segment_header_;
513 const Output_section_headers* section_header_;
514 const Output_section* shstrtab_;
515 const char* entry_;
518 // Output sections are mainly comprised of input sections. However,
519 // there are cases where we have data to write out which is not in an
520 // input section. Output_section_data is used in such cases. This is
521 // an abstract base class.
523 class Output_section_data : public Output_data
525 public:
526 Output_section_data(off_t data_size, uint64_t addralign)
527 : Output_data(), output_section_(NULL), addralign_(addralign)
528 { this->set_data_size(data_size); }
530 Output_section_data(uint64_t addralign)
531 : Output_data(), output_section_(NULL), addralign_(addralign)
534 // Return the output section.
535 const Output_section*
536 output_section() const
537 { return this->output_section_; }
539 // Record the output section.
540 void
541 set_output_section(Output_section* os);
543 // Add an input section, for SHF_MERGE sections. This returns true
544 // if the section was handled.
545 bool
546 add_input_section(Relobj* object, unsigned int shndx)
547 { return this->do_add_input_section(object, shndx); }
549 // Given an input OBJECT, an input section index SHNDX within that
550 // object, and an OFFSET relative to the start of that input
551 // section, return whether or not the corresponding offset within
552 // the output section is known. If this function returns true, it
553 // sets *POUTPUT to the output offset. The value -1 indicates that
554 // this input offset is being discarded.
555 bool
556 output_offset(const Relobj* object, unsigned int shndx,
557 section_offset_type offset,
558 section_offset_type *poutput) const
559 { return this->do_output_offset(object, shndx, offset, poutput); }
561 // Return whether this is the merge section for the input section
562 // SHNDX in OBJECT. This should return true when output_offset
563 // would return true for some values of OFFSET.
564 bool
565 is_merge_section_for(const Relobj* object, unsigned int shndx) const
566 { return this->do_is_merge_section_for(object, shndx); }
568 // Write the contents to a buffer. This is used for sections which
569 // require postprocessing, such as compression.
570 void
571 write_to_buffer(unsigned char* buffer)
572 { this->do_write_to_buffer(buffer); }
574 // Print merge stats to stderr. This should only be called for
575 // SHF_MERGE sections.
576 void
577 print_merge_stats(const char* section_name)
578 { this->do_print_merge_stats(section_name); }
580 protected:
581 // The child class must implement do_write.
583 // The child class may implement specific adjustments to the output
584 // section.
585 virtual void
586 do_adjust_output_section(Output_section*)
589 // May be implemented by child class. Return true if the section
590 // was handled.
591 virtual bool
592 do_add_input_section(Relobj*, unsigned int)
593 { gold_unreachable(); }
595 // The child class may implement output_offset.
596 virtual bool
597 do_output_offset(const Relobj*, unsigned int, section_offset_type,
598 section_offset_type*) const
599 { return false; }
601 // The child class may implement is_merge_section_for.
602 virtual bool
603 do_is_merge_section_for(const Relobj*, unsigned int) const
604 { return false; }
606 // The child class may implement write_to_buffer. Most child
607 // classes can not appear in a compressed section, and they do not
608 // implement this.
609 virtual void
610 do_write_to_buffer(unsigned char*)
611 { gold_unreachable(); }
613 // Print merge statistics.
614 virtual void
615 do_print_merge_stats(const char*)
616 { gold_unreachable(); }
618 // Return the required alignment.
619 uint64_t
620 do_addralign() const
621 { return this->addralign_; }
623 // Return the output section.
624 Output_section*
625 do_output_section()
626 { return this->output_section_; }
628 // Return the section index of the output section.
629 unsigned int
630 do_out_shndx() const;
632 // Set the alignment.
633 void
634 set_addralign(uint64_t addralign);
636 private:
637 // The output section for this section.
638 Output_section* output_section_;
639 // The required alignment.
640 uint64_t addralign_;
643 // Some Output_section_data classes build up their data step by step,
644 // rather than all at once. This class provides an interface for
645 // them.
647 class Output_section_data_build : public Output_section_data
649 public:
650 Output_section_data_build(uint64_t addralign)
651 : Output_section_data(addralign)
654 // Get the current data size.
655 off_t
656 current_data_size() const
657 { return this->current_data_size_for_child(); }
659 // Set the current data size.
660 void
661 set_current_data_size(off_t data_size)
662 { this->set_current_data_size_for_child(data_size); }
664 protected:
665 // Set the final data size.
666 virtual void
667 set_final_data_size()
668 { this->set_data_size(this->current_data_size_for_child()); }
671 // A simple case of Output_data in which we have constant data to
672 // output.
674 class Output_data_const : public Output_section_data
676 public:
677 Output_data_const(const std::string& data, uint64_t addralign)
678 : Output_section_data(data.size(), addralign), data_(data)
681 Output_data_const(const char* p, off_t len, uint64_t addralign)
682 : Output_section_data(len, addralign), data_(p, len)
685 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
686 : Output_section_data(len, addralign),
687 data_(reinterpret_cast<const char*>(p), len)
690 protected:
691 // Write the data to the output file.
692 void
693 do_write(Output_file*);
695 // Write the data to a buffer.
696 void
697 do_write_to_buffer(unsigned char* buffer)
698 { memcpy(buffer, this->data_.data(), this->data_.size()); }
700 // Write to a map file.
701 void
702 do_print_to_mapfile(Mapfile* mapfile) const
703 { mapfile->print_output_data(this, _("** fill")); }
705 private:
706 std::string data_;
709 // Another version of Output_data with constant data, in which the
710 // buffer is allocated by the caller.
712 class Output_data_const_buffer : public Output_section_data
714 public:
715 Output_data_const_buffer(const unsigned char* p, off_t len,
716 uint64_t addralign, const char* map_name)
717 : Output_section_data(len, addralign),
718 p_(p), map_name_(map_name)
721 protected:
722 // Write the data the output file.
723 void
724 do_write(Output_file*);
726 // Write the data to a buffer.
727 void
728 do_write_to_buffer(unsigned char* buffer)
729 { memcpy(buffer, this->p_, this->data_size()); }
731 // Write to a map file.
732 void
733 do_print_to_mapfile(Mapfile* mapfile) const
734 { mapfile->print_output_data(this, _(this->map_name_)); }
736 private:
737 // The data to output.
738 const unsigned char* p_;
739 // Name to use in a map file. Maps are a rarely used feature, but
740 // the space usage is minor as aren't very many of these objects.
741 const char* map_name_;
744 // A place holder for a fixed amount of data written out via some
745 // other mechanism.
747 class Output_data_fixed_space : public Output_section_data
749 public:
750 Output_data_fixed_space(off_t data_size, uint64_t addralign,
751 const char* map_name)
752 : Output_section_data(data_size, addralign),
753 map_name_(map_name)
756 protected:
757 // Write out the data--the actual data must be written out
758 // elsewhere.
759 void
760 do_write(Output_file*)
763 // Write to a map file.
764 void
765 do_print_to_mapfile(Mapfile* mapfile) const
766 { mapfile->print_output_data(this, _(this->map_name_)); }
768 private:
769 // Name to use in a map file. Maps are a rarely used feature, but
770 // the space usage is minor as aren't very many of these objects.
771 const char* map_name_;
774 // A place holder for variable sized data written out via some other
775 // mechanism.
777 class Output_data_space : public Output_section_data_build
779 public:
780 explicit Output_data_space(uint64_t addralign, const char* map_name)
781 : Output_section_data_build(addralign),
782 map_name_(map_name)
785 // Set the alignment.
786 void
787 set_space_alignment(uint64_t align)
788 { this->set_addralign(align); }
790 protected:
791 // Write out the data--the actual data must be written out
792 // elsewhere.
793 void
794 do_write(Output_file*)
797 // Write to a map file.
798 void
799 do_print_to_mapfile(Mapfile* mapfile) const
800 { mapfile->print_output_data(this, _(this->map_name_)); }
802 private:
803 // Name to use in a map file. Maps are a rarely used feature, but
804 // the space usage is minor as aren't very many of these objects.
805 const char* map_name_;
808 // Fill fixed space with zeroes. This is just like
809 // Output_data_fixed_space, except that the map name is known.
811 class Output_data_zero_fill : public Output_section_data
813 public:
814 Output_data_zero_fill(off_t data_size, uint64_t addralign)
815 : Output_section_data(data_size, addralign)
818 protected:
819 // There is no data to write out.
820 void
821 do_write(Output_file*)
824 // Write to a map file.
825 void
826 do_print_to_mapfile(Mapfile* mapfile) const
827 { mapfile->print_output_data(this, "** zero fill"); }
830 // A string table which goes into an output section.
832 class Output_data_strtab : public Output_section_data
834 public:
835 Output_data_strtab(Stringpool* strtab)
836 : Output_section_data(1), strtab_(strtab)
839 protected:
840 // This is called to set the address and file offset. Here we make
841 // sure that the Stringpool is finalized.
842 void
843 set_final_data_size();
845 // Write out the data.
846 void
847 do_write(Output_file*);
849 // Write the data to a buffer.
850 void
851 do_write_to_buffer(unsigned char* buffer)
852 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
854 // Write to a map file.
855 void
856 do_print_to_mapfile(Mapfile* mapfile) const
857 { mapfile->print_output_data(this, _("** string table")); }
859 private:
860 Stringpool* strtab_;
863 // This POD class is used to represent a single reloc in the output
864 // file. This could be a private class within Output_data_reloc, but
865 // the templatization is complex enough that I broke it out into a
866 // separate class. The class is templatized on either elfcpp::SHT_REL
867 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
868 // relocation or an ordinary relocation.
870 // A relocation can be against a global symbol, a local symbol, a
871 // local section symbol, an output section, or the undefined symbol at
872 // index 0. We represent the latter by using a NULL global symbol.
874 template<int sh_type, bool dynamic, int size, bool big_endian>
875 class Output_reloc;
877 template<bool dynamic, int size, bool big_endian>
878 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
880 public:
881 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
882 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
884 static const Address invalid_address = static_cast<Address>(0) - 1;
886 // An uninitialized entry. We need this because we want to put
887 // instances of this class into an STL container.
888 Output_reloc()
889 : local_sym_index_(INVALID_CODE)
892 // We have a bunch of different constructors. They come in pairs
893 // depending on how the address of the relocation is specified. It
894 // can either be an offset in an Output_data or an offset in an
895 // input section.
897 // A reloc against a global symbol.
899 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
900 Address address, bool is_relative);
902 Output_reloc(Symbol* gsym, unsigned int type,
903 Sized_relobj<size, big_endian>* relobj,
904 unsigned int shndx, Address address, bool is_relative);
906 // A reloc against a local symbol or local section symbol.
908 Output_reloc(Sized_relobj<size, big_endian>* relobj,
909 unsigned int local_sym_index, unsigned int type,
910 Output_data* od, Address address, bool is_relative,
911 bool is_section_symbol);
913 Output_reloc(Sized_relobj<size, big_endian>* relobj,
914 unsigned int local_sym_index, unsigned int type,
915 unsigned int shndx, Address address, bool is_relative,
916 bool is_section_symbol);
918 // A reloc against the STT_SECTION symbol of an output section.
920 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
921 Address address);
923 Output_reloc(Output_section* os, unsigned int type,
924 Sized_relobj<size, big_endian>* relobj,
925 unsigned int shndx, Address address);
927 // Return TRUE if this is a RELATIVE relocation.
928 bool
929 is_relative() const
930 { return this->is_relative_; }
932 // Return whether this is against a local section symbol.
933 bool
934 is_local_section_symbol() const
936 return (this->local_sym_index_ != GSYM_CODE
937 && this->local_sym_index_ != SECTION_CODE
938 && this->local_sym_index_ != INVALID_CODE
939 && this->is_section_symbol_);
942 // For a local section symbol, return the offset of the input
943 // section within the output section. ADDEND is the addend being
944 // applied to the input section.
945 Address
946 local_section_offset(Addend addend) const;
948 // Get the value of the symbol referred to by a Rel relocation when
949 // we are adding the given ADDEND.
950 Address
951 symbol_value(Addend addend) const;
953 // Write the reloc entry to an output view.
954 void
955 write(unsigned char* pov) const;
957 // Write the offset and info fields to Write_rel.
958 template<typename Write_rel>
959 void write_rel(Write_rel*) const;
961 // This is used when sorting dynamic relocs. Return -1 to sort this
962 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
964 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
965 const;
967 // Return whether this reloc should be sorted before the argument
968 // when sorting dynamic relocs.
969 bool
970 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
971 r2) const
972 { return this->compare(r2) < 0; }
974 private:
975 // Record that we need a dynamic symbol index.
976 void
977 set_needs_dynsym_index();
979 // Return the symbol index.
980 unsigned int
981 get_symbol_index() const;
983 // Return the output address.
984 Address
985 get_address() const;
987 // Codes for local_sym_index_.
988 enum
990 // Global symbol.
991 GSYM_CODE = -1U,
992 // Output section.
993 SECTION_CODE = -2U,
994 // Invalid uninitialized entry.
995 INVALID_CODE = -3U
998 union
1000 // For a local symbol or local section symbol
1001 // (this->local_sym_index_ >= 0), the object. We will never
1002 // generate a relocation against a local symbol in a dynamic
1003 // object; that doesn't make sense. And our callers will always
1004 // be templatized, so we use Sized_relobj here.
1005 Sized_relobj<size, big_endian>* relobj;
1006 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1007 // symbol. If this is NULL, it indicates a relocation against the
1008 // undefined 0 symbol.
1009 Symbol* gsym;
1010 // For a relocation against an output section
1011 // (this->local_sym_index_ == SECTION_CODE), the output section.
1012 Output_section* os;
1013 } u1_;
1014 union
1016 // If this->shndx_ is not INVALID CODE, the object which holds the
1017 // input section being used to specify the reloc address.
1018 Sized_relobj<size, big_endian>* relobj;
1019 // If this->shndx_ is INVALID_CODE, the output data being used to
1020 // specify the reloc address. This may be NULL if the reloc
1021 // address is absolute.
1022 Output_data* od;
1023 } u2_;
1024 // The address offset within the input section or the Output_data.
1025 Address address_;
1026 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1027 // relocation against an output section, or INVALID_CODE for an
1028 // uninitialized value. Otherwise, for a local symbol
1029 // (this->is_section_symbol_ is false), the local symbol index. For
1030 // a local section symbol (this->is_section_symbol_ is true), the
1031 // section index in the input file.
1032 unsigned int local_sym_index_;
1033 // The reloc type--a processor specific code.
1034 unsigned int type_ : 30;
1035 // True if the relocation is a RELATIVE relocation.
1036 bool is_relative_ : 1;
1037 // True if the relocation is against a section symbol.
1038 bool is_section_symbol_ : 1;
1039 // If the reloc address is an input section in an object, the
1040 // section index. This is INVALID_CODE if the reloc address is
1041 // specified in some other way.
1042 unsigned int shndx_;
1045 // The SHT_RELA version of Output_reloc<>. This is just derived from
1046 // the SHT_REL version of Output_reloc, but it adds an addend.
1048 template<bool dynamic, int size, bool big_endian>
1049 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1051 public:
1052 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1053 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1055 // An uninitialized entry.
1056 Output_reloc()
1057 : rel_()
1060 // A reloc against a global symbol.
1062 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1063 Address address, Addend addend, bool is_relative)
1064 : rel_(gsym, type, od, address, is_relative), addend_(addend)
1067 Output_reloc(Symbol* gsym, unsigned int type,
1068 Sized_relobj<size, big_endian>* relobj,
1069 unsigned int shndx, Address address, Addend addend,
1070 bool is_relative)
1071 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1074 // A reloc against a local symbol.
1076 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1077 unsigned int local_sym_index, unsigned int type,
1078 Output_data* od, Address address,
1079 Addend addend, bool is_relative, bool is_section_symbol)
1080 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1081 is_section_symbol),
1082 addend_(addend)
1085 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1086 unsigned int local_sym_index, unsigned int type,
1087 unsigned int shndx, Address address,
1088 Addend addend, bool is_relative, bool is_section_symbol)
1089 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1090 is_section_symbol),
1091 addend_(addend)
1094 // A reloc against the STT_SECTION symbol of an output section.
1096 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1097 Address address, Addend addend)
1098 : rel_(os, type, od, address), addend_(addend)
1101 Output_reloc(Output_section* os, unsigned int type,
1102 Sized_relobj<size, big_endian>* relobj,
1103 unsigned int shndx, Address address, Addend addend)
1104 : rel_(os, type, relobj, shndx, address), addend_(addend)
1107 // Write the reloc entry to an output view.
1108 void
1109 write(unsigned char* pov) const;
1111 // Return whether this reloc should be sorted before the argument
1112 // when sorting dynamic relocs.
1113 bool
1114 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1115 r2) const
1117 int i = this->rel_.compare(r2.rel_);
1118 if (i < 0)
1119 return true;
1120 else if (i > 0)
1121 return false;
1122 else
1123 return this->addend_ < r2.addend_;
1126 private:
1127 // The basic reloc.
1128 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1129 // The addend.
1130 Addend addend_;
1133 // Output_data_reloc is used to manage a section containing relocs.
1134 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1135 // indicates whether this is a dynamic relocation or a normal
1136 // relocation. Output_data_reloc_base is a base class.
1137 // Output_data_reloc is the real class, which we specialize based on
1138 // the reloc type.
1140 template<int sh_type, bool dynamic, int size, bool big_endian>
1141 class Output_data_reloc_base : public Output_section_data_build
1143 public:
1144 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1145 typedef typename Output_reloc_type::Address Address;
1146 static const int reloc_size =
1147 Reloc_types<sh_type, size, big_endian>::reloc_size;
1149 // Construct the section.
1150 Output_data_reloc_base(bool sort_relocs)
1151 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1152 sort_relocs_(sort_relocs)
1155 protected:
1156 // Write out the data.
1157 void
1158 do_write(Output_file*);
1160 // Set the entry size and the link.
1161 void
1162 do_adjust_output_section(Output_section *os);
1164 // Write to a map file.
1165 void
1166 do_print_to_mapfile(Mapfile* mapfile) const
1168 mapfile->print_output_data(this,
1169 (dynamic
1170 ? _("** dynamic relocs")
1171 : _("** relocs")));
1174 // Add a relocation entry.
1175 void
1176 add(Output_data *od, const Output_reloc_type& reloc)
1178 this->relocs_.push_back(reloc);
1179 this->set_current_data_size(this->relocs_.size() * reloc_size);
1180 od->add_dynamic_reloc();
1183 private:
1184 typedef std::vector<Output_reloc_type> Relocs;
1186 // The class used to sort the relocations.
1187 struct Sort_relocs_comparison
1189 bool
1190 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1191 { return r1.sort_before(r2); }
1194 // The relocations in this section.
1195 Relocs relocs_;
1196 // Whether to sort the relocations when writing them out, to make
1197 // the dynamic linker more efficient.
1198 bool sort_relocs_;
1201 // The class which callers actually create.
1203 template<int sh_type, bool dynamic, int size, bool big_endian>
1204 class Output_data_reloc;
1206 // The SHT_REL version of Output_data_reloc.
1208 template<bool dynamic, int size, bool big_endian>
1209 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1210 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1212 private:
1213 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1214 big_endian> Base;
1216 public:
1217 typedef typename Base::Output_reloc_type Output_reloc_type;
1218 typedef typename Output_reloc_type::Address Address;
1220 Output_data_reloc(bool sr)
1221 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1224 // Add a reloc against a global symbol.
1226 void
1227 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1228 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1230 void
1231 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1232 Sized_relobj<size, big_endian>* relobj,
1233 unsigned int shndx, Address address)
1234 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1235 false)); }
1237 // These are to simplify the Copy_relocs class.
1239 void
1240 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1241 Address addend)
1243 gold_assert(addend == 0);
1244 this->add_global(gsym, type, od, address);
1247 void
1248 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1249 Sized_relobj<size, big_endian>* relobj,
1250 unsigned int shndx, Address address, Address addend)
1252 gold_assert(addend == 0);
1253 this->add_global(gsym, type, od, relobj, shndx, address);
1256 // Add a RELATIVE reloc against a global symbol. The final relocation
1257 // will not reference the symbol.
1259 void
1260 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1261 Address address)
1262 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1264 void
1265 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1266 Sized_relobj<size, big_endian>* relobj,
1267 unsigned int shndx, Address address)
1269 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1270 true));
1273 // Add a reloc against a local symbol.
1275 void
1276 add_local(Sized_relobj<size, big_endian>* relobj,
1277 unsigned int local_sym_index, unsigned int type,
1278 Output_data* od, Address address)
1280 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1281 address, false, false));
1284 void
1285 add_local(Sized_relobj<size, big_endian>* relobj,
1286 unsigned int local_sym_index, unsigned int type,
1287 Output_data* od, unsigned int shndx, Address address)
1289 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1290 address, false, false));
1293 // Add a RELATIVE reloc against a local symbol.
1295 void
1296 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1297 unsigned int local_sym_index, unsigned int type,
1298 Output_data* od, Address address)
1300 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1301 address, true, false));
1304 void
1305 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1306 unsigned int local_sym_index, unsigned int type,
1307 Output_data* od, unsigned int shndx, Address address)
1309 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1310 address, true, false));
1313 // Add a reloc against a local section symbol. This will be
1314 // converted into a reloc against the STT_SECTION symbol of the
1315 // output section.
1317 void
1318 add_local_section(Sized_relobj<size, big_endian>* relobj,
1319 unsigned int input_shndx, unsigned int type,
1320 Output_data* od, Address address)
1322 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1323 address, false, true));
1326 void
1327 add_local_section(Sized_relobj<size, big_endian>* relobj,
1328 unsigned int input_shndx, unsigned int type,
1329 Output_data* od, unsigned int shndx, Address address)
1331 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1332 address, false, true));
1335 // A reloc against the STT_SECTION symbol of an output section.
1336 // OS is the Output_section that the relocation refers to; OD is
1337 // the Output_data object being relocated.
1339 void
1340 add_output_section(Output_section* os, unsigned int type,
1341 Output_data* od, Address address)
1342 { this->add(od, Output_reloc_type(os, type, od, address)); }
1344 void
1345 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1346 Sized_relobj<size, big_endian>* relobj,
1347 unsigned int shndx, Address address)
1348 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1351 // The SHT_RELA version of Output_data_reloc.
1353 template<bool dynamic, int size, bool big_endian>
1354 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1355 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1357 private:
1358 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1359 big_endian> Base;
1361 public:
1362 typedef typename Base::Output_reloc_type Output_reloc_type;
1363 typedef typename Output_reloc_type::Address Address;
1364 typedef typename Output_reloc_type::Addend Addend;
1366 Output_data_reloc(bool sr)
1367 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1370 // Add a reloc against a global symbol.
1372 void
1373 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1374 Address address, Addend addend)
1375 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1376 false)); }
1378 void
1379 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1380 Sized_relobj<size, big_endian>* relobj,
1381 unsigned int shndx, Address address,
1382 Addend addend)
1383 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1384 addend, false)); }
1386 // Add a RELATIVE reloc against a global symbol. The final output
1387 // relocation will not reference the symbol, but we must keep the symbol
1388 // information long enough to set the addend of the relocation correctly
1389 // when it is written.
1391 void
1392 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1393 Address address, Addend addend)
1394 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1396 void
1397 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1398 Sized_relobj<size, big_endian>* relobj,
1399 unsigned int shndx, Address address, Addend addend)
1400 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1401 addend, true)); }
1403 // Add a reloc against a local symbol.
1405 void
1406 add_local(Sized_relobj<size, big_endian>* relobj,
1407 unsigned int local_sym_index, unsigned int type,
1408 Output_data* od, Address address, Addend addend)
1410 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1411 addend, false, false));
1414 void
1415 add_local(Sized_relobj<size, big_endian>* relobj,
1416 unsigned int local_sym_index, unsigned int type,
1417 Output_data* od, unsigned int shndx, Address address,
1418 Addend addend)
1420 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1421 address, addend, false, false));
1424 // Add a RELATIVE reloc against a local symbol.
1426 void
1427 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1428 unsigned int local_sym_index, unsigned int type,
1429 Output_data* od, Address address, Addend addend)
1431 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1432 addend, true, false));
1435 void
1436 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1437 unsigned int local_sym_index, unsigned int type,
1438 Output_data* od, unsigned int shndx, Address address,
1439 Addend addend)
1441 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1442 address, addend, true, false));
1445 // Add a reloc against a local section symbol. This will be
1446 // converted into a reloc against the STT_SECTION symbol of the
1447 // output section.
1449 void
1450 add_local_section(Sized_relobj<size, big_endian>* relobj,
1451 unsigned int input_shndx, unsigned int type,
1452 Output_data* od, Address address, Addend addend)
1454 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1455 addend, false, true));
1458 void
1459 add_local_section(Sized_relobj<size, big_endian>* relobj,
1460 unsigned int input_shndx, unsigned int type,
1461 Output_data* od, unsigned int shndx, Address address,
1462 Addend addend)
1464 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1465 address, addend, false, true));
1468 // A reloc against the STT_SECTION symbol of an output section.
1470 void
1471 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1472 Address address, Addend addend)
1473 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1475 void
1476 add_output_section(Output_section* os, unsigned int type,
1477 Sized_relobj<size, big_endian>* relobj,
1478 unsigned int shndx, Address address, Addend addend)
1479 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1480 addend)); }
1483 // Output_relocatable_relocs represents a relocation section in a
1484 // relocatable link. The actual data is written out in the target
1485 // hook relocate_for_relocatable. This just saves space for it.
1487 template<int sh_type, int size, bool big_endian>
1488 class Output_relocatable_relocs : public Output_section_data
1490 public:
1491 Output_relocatable_relocs(Relocatable_relocs* rr)
1492 : Output_section_data(Output_data::default_alignment_for_size(size)),
1493 rr_(rr)
1496 void
1497 set_final_data_size();
1499 // Write out the data. There is nothing to do here.
1500 void
1501 do_write(Output_file*)
1504 // Write to a map file.
1505 void
1506 do_print_to_mapfile(Mapfile* mapfile) const
1507 { mapfile->print_output_data(this, _("** relocs")); }
1509 private:
1510 // The relocs associated with this input section.
1511 Relocatable_relocs* rr_;
1514 // Handle a GROUP section.
1516 template<int size, bool big_endian>
1517 class Output_data_group : public Output_section_data
1519 public:
1520 // The constructor clears *INPUT_SHNDXES.
1521 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1522 section_size_type entry_count,
1523 elfcpp::Elf_Word flags,
1524 std::vector<unsigned int>* input_shndxes);
1526 void
1527 do_write(Output_file*);
1529 // Write to a map file.
1530 void
1531 do_print_to_mapfile(Mapfile* mapfile) const
1532 { mapfile->print_output_data(this, _("** group")); }
1534 private:
1535 // The input object.
1536 Sized_relobj<size, big_endian>* relobj_;
1537 // The group flag word.
1538 elfcpp::Elf_Word flags_;
1539 // The section indexes of the input sections in this group.
1540 std::vector<unsigned int> input_shndxes_;
1543 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1544 // for one symbol--either a global symbol or a local symbol in an
1545 // object. The target specific code adds entries to the GOT as
1546 // needed.
1548 template<int size, bool big_endian>
1549 class Output_data_got : public Output_section_data_build
1551 public:
1552 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1553 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1554 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1556 Output_data_got()
1557 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1558 entries_()
1561 // Add an entry for a global symbol to the GOT. Return true if this
1562 // is a new GOT entry, false if the symbol was already in the GOT.
1563 bool
1564 add_global(Symbol* gsym, unsigned int got_type);
1566 // Add an entry for a global symbol to the GOT, and add a dynamic
1567 // relocation of type R_TYPE for the GOT entry.
1568 void
1569 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1570 Rel_dyn* rel_dyn, unsigned int r_type);
1572 void
1573 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1574 Rela_dyn* rela_dyn, unsigned int r_type);
1576 // Add a pair of entries for a global symbol to the GOT, and add
1577 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1578 void
1579 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1580 Rel_dyn* rel_dyn, unsigned int r_type_1,
1581 unsigned int r_type_2);
1583 void
1584 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1585 Rela_dyn* rela_dyn, unsigned int r_type_1,
1586 unsigned int r_type_2);
1588 // Add an entry for a local symbol to the GOT. This returns true if
1589 // this is a new GOT entry, false if the symbol already has a GOT
1590 // entry.
1591 bool
1592 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1593 unsigned int got_type);
1595 // Add an entry for a local symbol to the GOT, and add a dynamic
1596 // relocation of type R_TYPE for the GOT entry.
1597 void
1598 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1599 unsigned int sym_index, unsigned int got_type,
1600 Rel_dyn* rel_dyn, unsigned int r_type);
1602 void
1603 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1604 unsigned int sym_index, unsigned int got_type,
1605 Rela_dyn* rela_dyn, unsigned int r_type);
1607 // Add a pair of entries for a local symbol to the GOT, and add
1608 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1609 void
1610 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1611 unsigned int sym_index, unsigned int shndx,
1612 unsigned int got_type, Rel_dyn* rel_dyn,
1613 unsigned int r_type_1, unsigned int r_type_2);
1615 void
1616 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1617 unsigned int sym_index, unsigned int shndx,
1618 unsigned int got_type, Rela_dyn* rela_dyn,
1619 unsigned int r_type_1, unsigned int r_type_2);
1621 // Add a constant to the GOT. This returns the offset of the new
1622 // entry from the start of the GOT.
1623 unsigned int
1624 add_constant(Valtype constant)
1626 this->entries_.push_back(Got_entry(constant));
1627 this->set_got_size();
1628 return this->last_got_offset();
1631 protected:
1632 // Write out the GOT table.
1633 void
1634 do_write(Output_file*);
1636 // Write to a map file.
1637 void
1638 do_print_to_mapfile(Mapfile* mapfile) const
1639 { mapfile->print_output_data(this, _("** GOT")); }
1641 private:
1642 // This POD class holds a single GOT entry.
1643 class Got_entry
1645 public:
1646 // Create a zero entry.
1647 Got_entry()
1648 : local_sym_index_(CONSTANT_CODE)
1649 { this->u_.constant = 0; }
1651 // Create a global symbol entry.
1652 explicit Got_entry(Symbol* gsym)
1653 : local_sym_index_(GSYM_CODE)
1654 { this->u_.gsym = gsym; }
1656 // Create a local symbol entry.
1657 Got_entry(Sized_relobj<size, big_endian>* object,
1658 unsigned int local_sym_index)
1659 : local_sym_index_(local_sym_index)
1661 gold_assert(local_sym_index != GSYM_CODE
1662 && local_sym_index != CONSTANT_CODE);
1663 this->u_.object = object;
1666 // Create a constant entry. The constant is a host value--it will
1667 // be swapped, if necessary, when it is written out.
1668 explicit Got_entry(Valtype constant)
1669 : local_sym_index_(CONSTANT_CODE)
1670 { this->u_.constant = constant; }
1672 // Write the GOT entry to an output view.
1673 void
1674 write(unsigned char* pov) const;
1676 private:
1677 enum
1679 GSYM_CODE = -1U,
1680 CONSTANT_CODE = -2U
1683 union
1685 // For a local symbol, the object.
1686 Sized_relobj<size, big_endian>* object;
1687 // For a global symbol, the symbol.
1688 Symbol* gsym;
1689 // For a constant, the constant.
1690 Valtype constant;
1691 } u_;
1692 // For a local symbol, the local symbol index. This is GSYM_CODE
1693 // for a global symbol, or CONSTANT_CODE for a constant.
1694 unsigned int local_sym_index_;
1697 typedef std::vector<Got_entry> Got_entries;
1699 // Return the offset into the GOT of GOT entry I.
1700 unsigned int
1701 got_offset(unsigned int i) const
1702 { return i * (size / 8); }
1704 // Return the offset into the GOT of the last entry added.
1705 unsigned int
1706 last_got_offset() const
1707 { return this->got_offset(this->entries_.size() - 1); }
1709 // Set the size of the section.
1710 void
1711 set_got_size()
1712 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1714 // The list of GOT entries.
1715 Got_entries entries_;
1718 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1719 // section.
1721 class Output_data_dynamic : public Output_section_data
1723 public:
1724 Output_data_dynamic(Stringpool* pool)
1725 : Output_section_data(Output_data::default_alignment()),
1726 entries_(), pool_(pool)
1729 // Add a new dynamic entry with a fixed numeric value.
1730 void
1731 add_constant(elfcpp::DT tag, unsigned int val)
1732 { this->add_entry(Dynamic_entry(tag, val)); }
1734 // Add a new dynamic entry with the address of output data.
1735 void
1736 add_section_address(elfcpp::DT tag, const Output_data* od)
1737 { this->add_entry(Dynamic_entry(tag, od, false)); }
1739 // Add a new dynamic entry with the address of output data
1740 // plus a constant offset.
1741 void
1742 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1743 unsigned int offset)
1744 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1746 // Add a new dynamic entry with the size of output data.
1747 void
1748 add_section_size(elfcpp::DT tag, const Output_data* od)
1749 { this->add_entry(Dynamic_entry(tag, od, true)); }
1751 // Add a new dynamic entry with the address of a symbol.
1752 void
1753 add_symbol(elfcpp::DT tag, const Symbol* sym)
1754 { this->add_entry(Dynamic_entry(tag, sym)); }
1756 // Add a new dynamic entry with a string.
1757 void
1758 add_string(elfcpp::DT tag, const char* str)
1759 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1761 void
1762 add_string(elfcpp::DT tag, const std::string& str)
1763 { this->add_string(tag, str.c_str()); }
1765 protected:
1766 // Adjust the output section to set the entry size.
1767 void
1768 do_adjust_output_section(Output_section*);
1770 // Set the final data size.
1771 void
1772 set_final_data_size();
1774 // Write out the dynamic entries.
1775 void
1776 do_write(Output_file*);
1778 // Write to a map file.
1779 void
1780 do_print_to_mapfile(Mapfile* mapfile) const
1781 { mapfile->print_output_data(this, _("** dynamic")); }
1783 private:
1784 // This POD class holds a single dynamic entry.
1785 class Dynamic_entry
1787 public:
1788 // Create an entry with a fixed numeric value.
1789 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1790 : tag_(tag), offset_(DYNAMIC_NUMBER)
1791 { this->u_.val = val; }
1793 // Create an entry with the size or address of a section.
1794 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1795 : tag_(tag),
1796 offset_(section_size
1797 ? DYNAMIC_SECTION_SIZE
1798 : DYNAMIC_SECTION_ADDRESS)
1799 { this->u_.od = od; }
1801 // Create an entry with the address of a section plus a constant offset.
1802 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1803 : tag_(tag),
1804 offset_(offset)
1805 { this->u_.od = od; }
1807 // Create an entry with the address of a symbol.
1808 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1809 : tag_(tag), offset_(DYNAMIC_SYMBOL)
1810 { this->u_.sym = sym; }
1812 // Create an entry with a string.
1813 Dynamic_entry(elfcpp::DT tag, const char* str)
1814 : tag_(tag), offset_(DYNAMIC_STRING)
1815 { this->u_.str = str; }
1817 // Write the dynamic entry to an output view.
1818 template<int size, bool big_endian>
1819 void
1820 write(unsigned char* pov, const Stringpool*) const;
1822 private:
1823 // Classification is encoded in the OFFSET field.
1824 enum Classification
1826 // Section address.
1827 DYNAMIC_SECTION_ADDRESS = 0,
1828 // Number.
1829 DYNAMIC_NUMBER = -1U,
1830 // Section size.
1831 DYNAMIC_SECTION_SIZE = -2U,
1832 // Symbol adress.
1833 DYNAMIC_SYMBOL = -3U,
1834 // String.
1835 DYNAMIC_STRING = -4U
1836 // Any other value indicates a section address plus OFFSET.
1839 union
1841 // For DYNAMIC_NUMBER.
1842 unsigned int val;
1843 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1844 const Output_data* od;
1845 // For DYNAMIC_SYMBOL.
1846 const Symbol* sym;
1847 // For DYNAMIC_STRING.
1848 const char* str;
1849 } u_;
1850 // The dynamic tag.
1851 elfcpp::DT tag_;
1852 // The type of entry (Classification) or offset within a section.
1853 unsigned int offset_;
1856 // Add an entry to the list.
1857 void
1858 add_entry(const Dynamic_entry& entry)
1859 { this->entries_.push_back(entry); }
1861 // Sized version of write function.
1862 template<int size, bool big_endian>
1863 void
1864 sized_write(Output_file* of);
1866 // The type of the list of entries.
1867 typedef std::vector<Dynamic_entry> Dynamic_entries;
1869 // The entries.
1870 Dynamic_entries entries_;
1871 // The pool used for strings.
1872 Stringpool* pool_;
1875 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
1876 // which may be required if the object file has more than
1877 // SHN_LORESERVE sections.
1879 class Output_symtab_xindex : public Output_section_data
1881 public:
1882 Output_symtab_xindex(size_t symcount)
1883 : Output_section_data(symcount * 4, 4),
1884 entries_()
1887 // Add an entry: symbol number SYMNDX has section SHNDX.
1888 void
1889 add(unsigned int symndx, unsigned int shndx)
1890 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
1892 protected:
1893 void
1894 do_write(Output_file*);
1896 // Write to a map file.
1897 void
1898 do_print_to_mapfile(Mapfile* mapfile) const
1899 { mapfile->print_output_data(this, _("** symtab xindex")); }
1901 private:
1902 template<bool big_endian>
1903 void
1904 endian_do_write(unsigned char*);
1906 // It is likely that most symbols will not require entries. Rather
1907 // than keep a vector for all symbols, we keep pairs of symbol index
1908 // and section index.
1909 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
1911 // The entries we need.
1912 Xindex_entries entries_;
1915 // An output section. We don't expect to have too many output
1916 // sections, so we don't bother to do a template on the size.
1918 class Output_section : public Output_data
1920 public:
1921 // Create an output section, giving the name, type, and flags.
1922 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1923 virtual ~Output_section();
1925 // Add a new input section SHNDX, named NAME, with header SHDR, from
1926 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1927 // which applies to this section, or 0 if none, or -1 if more than
1928 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1929 // in a linker script; in that case we need to keep track of input
1930 // sections associated with an output section. Return the offset
1931 // within the output section.
1932 template<int size, bool big_endian>
1933 off_t
1934 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1935 const char *name,
1936 const elfcpp::Shdr<size, big_endian>& shdr,
1937 unsigned int reloc_shndx, bool have_sections_script);
1939 // Add generated data POSD to this output section.
1940 void
1941 add_output_section_data(Output_section_data* posd);
1943 // Return the section name.
1944 const char*
1945 name() const
1946 { return this->name_; }
1948 // Return the section type.
1949 elfcpp::Elf_Word
1950 type() const
1951 { return this->type_; }
1953 // Return the section flags.
1954 elfcpp::Elf_Xword
1955 flags() const
1956 { return this->flags_; }
1958 // Update the output section flags based on input section flags.
1959 void
1960 update_flags_for_input_section(elfcpp::Elf_Xword flags);
1962 // Return the entsize field.
1963 uint64_t
1964 entsize() const
1965 { return this->entsize_; }
1967 // Set the entsize field.
1968 void
1969 set_entsize(uint64_t v);
1971 // Set the load address.
1972 void
1973 set_load_address(uint64_t load_address)
1975 this->load_address_ = load_address;
1976 this->has_load_address_ = true;
1979 // Set the link field to the output section index of a section.
1980 void
1981 set_link_section(const Output_data* od)
1983 gold_assert(this->link_ == 0
1984 && !this->should_link_to_symtab_
1985 && !this->should_link_to_dynsym_);
1986 this->link_section_ = od;
1989 // Set the link field to a constant.
1990 void
1991 set_link(unsigned int v)
1993 gold_assert(this->link_section_ == NULL
1994 && !this->should_link_to_symtab_
1995 && !this->should_link_to_dynsym_);
1996 this->link_ = v;
1999 // Record that this section should link to the normal symbol table.
2000 void
2001 set_should_link_to_symtab()
2003 gold_assert(this->link_section_ == NULL
2004 && this->link_ == 0
2005 && !this->should_link_to_dynsym_);
2006 this->should_link_to_symtab_ = true;
2009 // Record that this section should link to the dynamic symbol table.
2010 void
2011 set_should_link_to_dynsym()
2013 gold_assert(this->link_section_ == NULL
2014 && this->link_ == 0
2015 && !this->should_link_to_symtab_);
2016 this->should_link_to_dynsym_ = true;
2019 // Return the info field.
2020 unsigned int
2021 info() const
2023 gold_assert(this->info_section_ == NULL
2024 && this->info_symndx_ == NULL);
2025 return this->info_;
2028 // Set the info field to the output section index of a section.
2029 void
2030 set_info_section(const Output_section* os)
2032 gold_assert((this->info_section_ == NULL
2033 || (this->info_section_ == os
2034 && this->info_uses_section_index_))
2035 && this->info_symndx_ == NULL
2036 && this->info_ == 0);
2037 this->info_section_ = os;
2038 this->info_uses_section_index_= true;
2041 // Set the info field to the symbol table index of a symbol.
2042 void
2043 set_info_symndx(const Symbol* sym)
2045 gold_assert(this->info_section_ == NULL
2046 && (this->info_symndx_ == NULL
2047 || this->info_symndx_ == sym)
2048 && this->info_ == 0);
2049 this->info_symndx_ = sym;
2052 // Set the info field to the symbol table index of a section symbol.
2053 void
2054 set_info_section_symndx(const Output_section* os)
2056 gold_assert((this->info_section_ == NULL
2057 || (this->info_section_ == os
2058 && !this->info_uses_section_index_))
2059 && this->info_symndx_ == NULL
2060 && this->info_ == 0);
2061 this->info_section_ = os;
2062 this->info_uses_section_index_ = false;
2065 // Set the info field to a constant.
2066 void
2067 set_info(unsigned int v)
2069 gold_assert(this->info_section_ == NULL
2070 && this->info_symndx_ == NULL
2071 && (this->info_ == 0
2072 || this->info_ == v));
2073 this->info_ = v;
2076 // Set the addralign field.
2077 void
2078 set_addralign(uint64_t v)
2079 { this->addralign_ = v; }
2081 // Whether the output section index has been set.
2082 bool
2083 has_out_shndx() const
2084 { return this->out_shndx_ != -1U; }
2086 // Indicate that we need a symtab index.
2087 void
2088 set_needs_symtab_index()
2089 { this->needs_symtab_index_ = true; }
2091 // Return whether we need a symtab index.
2092 bool
2093 needs_symtab_index() const
2094 { return this->needs_symtab_index_; }
2096 // Get the symtab index.
2097 unsigned int
2098 symtab_index() const
2100 gold_assert(this->symtab_index_ != 0);
2101 return this->symtab_index_;
2104 // Set the symtab index.
2105 void
2106 set_symtab_index(unsigned int index)
2108 gold_assert(index != 0);
2109 this->symtab_index_ = index;
2112 // Indicate that we need a dynsym index.
2113 void
2114 set_needs_dynsym_index()
2115 { this->needs_dynsym_index_ = true; }
2117 // Return whether we need a dynsym index.
2118 bool
2119 needs_dynsym_index() const
2120 { return this->needs_dynsym_index_; }
2122 // Get the dynsym index.
2123 unsigned int
2124 dynsym_index() const
2126 gold_assert(this->dynsym_index_ != 0);
2127 return this->dynsym_index_;
2130 // Set the dynsym index.
2131 void
2132 set_dynsym_index(unsigned int index)
2134 gold_assert(index != 0);
2135 this->dynsym_index_ = index;
2138 // Return whether the input sections sections attachd to this output
2139 // section may require sorting. This is used to handle constructor
2140 // priorities compatibly with GNU ld.
2141 bool
2142 may_sort_attached_input_sections() const
2143 { return this->may_sort_attached_input_sections_; }
2145 // Record that the input sections attached to this output section
2146 // may require sorting.
2147 void
2148 set_may_sort_attached_input_sections()
2149 { this->may_sort_attached_input_sections_ = true; }
2151 // Return whether the input sections attached to this output section
2152 // require sorting. This is used to handle constructor priorities
2153 // compatibly with GNU ld.
2154 bool
2155 must_sort_attached_input_sections() const
2156 { return this->must_sort_attached_input_sections_; }
2158 // Record that the input sections attached to this output section
2159 // require sorting.
2160 void
2161 set_must_sort_attached_input_sections()
2162 { this->must_sort_attached_input_sections_ = true; }
2164 // Return whether this section holds relro data--data which has
2165 // dynamic relocations but which may be marked read-only after the
2166 // dynamic relocations have been completed.
2167 bool
2168 is_relro() const
2169 { return this->is_relro_; }
2171 // Record that this section holds relro data.
2172 void
2173 set_is_relro()
2174 { this->is_relro_ = true; }
2176 // Record that this section does not hold relro data.
2177 void
2178 clear_is_relro()
2179 { this->is_relro_ = false; }
2181 // True if this section holds relro local data--relro data for which
2182 // the dynamic relocations are all RELATIVE relocations.
2183 bool
2184 is_relro_local() const
2185 { return this->is_relro_local_; }
2187 // Record that this section holds relro local data.
2188 void
2189 set_is_relro_local()
2190 { this->is_relro_local_ = true; }
2192 // True if this is a small section: a section which holds small
2193 // variables.
2194 bool
2195 is_small_section() const
2196 { return this->is_small_section_; }
2198 // Record that this is a small section.
2199 void
2200 set_is_small_section()
2201 { this->is_small_section_ = true; }
2203 // True if this is a large section: a section which holds large
2204 // variables.
2205 bool
2206 is_large_section() const
2207 { return this->is_large_section_; }
2209 // Record that this is a large section.
2210 void
2211 set_is_large_section()
2212 { this->is_large_section_ = true; }
2214 // True if this is a large data (not BSS) section.
2215 bool
2216 is_large_data_section()
2217 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2219 // Return whether this section should be written after all the input
2220 // sections are complete.
2221 bool
2222 after_input_sections() const
2223 { return this->after_input_sections_; }
2225 // Record that this section should be written after all the input
2226 // sections are complete.
2227 void
2228 set_after_input_sections()
2229 { this->after_input_sections_ = true; }
2231 // Return whether this section requires postprocessing after all
2232 // relocations have been applied.
2233 bool
2234 requires_postprocessing() const
2235 { return this->requires_postprocessing_; }
2237 // If a section requires postprocessing, return the buffer to use.
2238 unsigned char*
2239 postprocessing_buffer() const
2241 gold_assert(this->postprocessing_buffer_ != NULL);
2242 return this->postprocessing_buffer_;
2245 // If a section requires postprocessing, create the buffer to use.
2246 void
2247 create_postprocessing_buffer();
2249 // If a section requires postprocessing, this is the size of the
2250 // buffer to which relocations should be applied.
2251 off_t
2252 postprocessing_buffer_size() const
2253 { return this->current_data_size_for_child(); }
2255 // Modify the section name. This is only permitted for an
2256 // unallocated section, and only before the size has been finalized.
2257 // Otherwise the name will not get into Layout::namepool_.
2258 void
2259 set_name(const char* newname)
2261 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2262 gold_assert(!this->is_data_size_valid());
2263 this->name_ = newname;
2266 // Return whether the offset OFFSET in the input section SHNDX in
2267 // object OBJECT is being included in the link.
2268 bool
2269 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2270 off_t offset) const;
2272 // Return the offset within the output section of OFFSET relative to
2273 // the start of input section SHNDX in object OBJECT.
2274 section_offset_type
2275 output_offset(const Relobj* object, unsigned int shndx,
2276 section_offset_type offset) const;
2278 // Return the output virtual address of OFFSET relative to the start
2279 // of input section SHNDX in object OBJECT.
2280 uint64_t
2281 output_address(const Relobj* object, unsigned int shndx,
2282 off_t offset) const;
2284 // Look for the merged section for input section SHNDX in object
2285 // OBJECT. If found, return true, and set *ADDR to the address of
2286 // the start of the merged section. This is not necessary the
2287 // output offset corresponding to input offset 0 in the section,
2288 // since the section may be mapped arbitrarily.
2289 bool
2290 find_starting_output_address(const Relobj* object, unsigned int shndx,
2291 uint64_t* addr) const;
2293 // Record that this output section was found in the SECTIONS clause
2294 // of a linker script.
2295 void
2296 set_found_in_sections_clause()
2297 { this->found_in_sections_clause_ = true; }
2299 // Return whether this output section was found in the SECTIONS
2300 // clause of a linker script.
2301 bool
2302 found_in_sections_clause() const
2303 { return this->found_in_sections_clause_; }
2305 // Write the section header into *OPHDR.
2306 template<int size, bool big_endian>
2307 void
2308 write_header(const Layout*, const Stringpool*,
2309 elfcpp::Shdr_write<size, big_endian>*) const;
2311 // The next few calls are for linker script support.
2313 // Store the list of input sections for this Output_section into the
2314 // list passed in. This removes the input sections, leaving only
2315 // any Output_section_data elements. This returns the size of those
2316 // Output_section_data elements. ADDRESS is the address of this
2317 // output section. FILL is the fill value to use, in case there are
2318 // any spaces between the remaining Output_section_data elements.
2319 uint64_t
2320 get_input_sections(uint64_t address, const std::string& fill,
2321 std::list<std::pair<Relobj*, unsigned int > >*);
2323 // Add an input section from a script.
2324 void
2325 add_input_section_for_script(Relobj* object, unsigned int shndx,
2326 off_t data_size, uint64_t addralign);
2328 // Set the current size of the output section.
2329 void
2330 set_current_data_size(off_t size)
2331 { this->set_current_data_size_for_child(size); }
2333 // Get the current size of the output section.
2334 off_t
2335 current_data_size() const
2336 { return this->current_data_size_for_child(); }
2338 // End of linker script support.
2340 // Print merge statistics to stderr.
2341 void
2342 print_merge_stats();
2344 protected:
2345 // Return the output section--i.e., the object itself.
2346 Output_section*
2347 do_output_section()
2348 { return this; }
2350 // Return the section index in the output file.
2351 unsigned int
2352 do_out_shndx() const
2354 gold_assert(this->out_shndx_ != -1U);
2355 return this->out_shndx_;
2358 // Set the output section index.
2359 void
2360 do_set_out_shndx(unsigned int shndx)
2362 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2363 this->out_shndx_ = shndx;
2366 // Set the final data size of the Output_section. For a typical
2367 // Output_section, there is nothing to do, but if there are any
2368 // Output_section_data objects we need to set their final addresses
2369 // here.
2370 virtual void
2371 set_final_data_size();
2373 // Reset the address and file offset.
2374 void
2375 do_reset_address_and_file_offset();
2377 // Write the data to the file. For a typical Output_section, this
2378 // does nothing: the data is written out by calling Object::Relocate
2379 // on each input object. But if there are any Output_section_data
2380 // objects we do need to write them out here.
2381 virtual void
2382 do_write(Output_file*);
2384 // Return the address alignment--function required by parent class.
2385 uint64_t
2386 do_addralign() const
2387 { return this->addralign_; }
2389 // Return whether there is a load address.
2390 bool
2391 do_has_load_address() const
2392 { return this->has_load_address_; }
2394 // Return the load address.
2395 uint64_t
2396 do_load_address() const
2398 gold_assert(this->has_load_address_);
2399 return this->load_address_;
2402 // Return whether this is an Output_section.
2403 bool
2404 do_is_section() const
2405 { return true; }
2407 // Return whether this is a section of the specified type.
2408 bool
2409 do_is_section_type(elfcpp::Elf_Word type) const
2410 { return this->type_ == type; }
2412 // Return whether the specified section flag is set.
2413 bool
2414 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2415 { return (this->flags_ & flag) != 0; }
2417 // Set the TLS offset. Called only for SHT_TLS sections.
2418 void
2419 do_set_tls_offset(uint64_t tls_base);
2421 // Return the TLS offset, relative to the base of the TLS segment.
2422 // Valid only for SHT_TLS sections.
2423 uint64_t
2424 do_tls_offset() const
2425 { return this->tls_offset_; }
2427 // This may be implemented by a child class.
2428 virtual void
2429 do_finalize_name(Layout*)
2432 // Print to the map file.
2433 virtual void
2434 do_print_to_mapfile(Mapfile*) const;
2436 // Record that this section requires postprocessing after all
2437 // relocations have been applied. This is called by a child class.
2438 void
2439 set_requires_postprocessing()
2441 this->requires_postprocessing_ = true;
2442 this->after_input_sections_ = true;
2445 // Write all the data of an Output_section into the postprocessing
2446 // buffer.
2447 void
2448 write_to_postprocessing_buffer();
2450 private:
2451 // In some cases we need to keep a list of the input sections
2452 // associated with this output section. We only need the list if we
2453 // might have to change the offsets of the input section within the
2454 // output section after we add the input section. The ordinary
2455 // input sections will be written out when we process the object
2456 // file, and as such we don't need to track them here. We do need
2457 // to track Output_section_data objects here. We store instances of
2458 // this structure in a std::vector, so it must be a POD. There can
2459 // be many instances of this structure, so we use a union to save
2460 // some space.
2461 class Input_section
2463 public:
2464 Input_section()
2465 : shndx_(0), p2align_(0)
2467 this->u1_.data_size = 0;
2468 this->u2_.object = NULL;
2471 // For an ordinary input section.
2472 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2473 uint64_t addralign)
2474 : shndx_(shndx),
2475 p2align_(ffsll(static_cast<long long>(addralign)))
2477 gold_assert(shndx != OUTPUT_SECTION_CODE
2478 && shndx != MERGE_DATA_SECTION_CODE
2479 && shndx != MERGE_STRING_SECTION_CODE);
2480 this->u1_.data_size = data_size;
2481 this->u2_.object = object;
2484 // For a non-merge output section.
2485 Input_section(Output_section_data* posd)
2486 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2488 this->u1_.data_size = 0;
2489 this->u2_.posd = posd;
2492 // For a merge section.
2493 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2494 : shndx_(is_string
2495 ? MERGE_STRING_SECTION_CODE
2496 : MERGE_DATA_SECTION_CODE),
2497 p2align_(0)
2499 this->u1_.entsize = entsize;
2500 this->u2_.posd = posd;
2503 // The required alignment.
2504 uint64_t
2505 addralign() const
2507 if (!this->is_input_section())
2508 return this->u2_.posd->addralign();
2509 return (this->p2align_ == 0
2511 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2514 // Return the required size.
2515 off_t
2516 data_size() const;
2518 // Whether this is an input section.
2519 bool
2520 is_input_section() const
2522 return (this->shndx_ != OUTPUT_SECTION_CODE
2523 && this->shndx_ != MERGE_DATA_SECTION_CODE
2524 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2527 // Return whether this is a merge section which matches the
2528 // parameters.
2529 bool
2530 is_merge_section(bool is_string, uint64_t entsize,
2531 uint64_t addralign) const
2533 return (this->shndx_ == (is_string
2534 ? MERGE_STRING_SECTION_CODE
2535 : MERGE_DATA_SECTION_CODE)
2536 && this->u1_.entsize == entsize
2537 && this->addralign() == addralign);
2540 // Return the object for an input section.
2541 Relobj*
2542 relobj() const
2544 gold_assert(this->is_input_section());
2545 return this->u2_.object;
2548 // Return the input section index for an input section.
2549 unsigned int
2550 shndx() const
2552 gold_assert(this->is_input_section());
2553 return this->shndx_;
2556 // Set the output section.
2557 void
2558 set_output_section(Output_section* os)
2560 gold_assert(!this->is_input_section());
2561 this->u2_.posd->set_output_section(os);
2564 // Set the address and file offset. This is called during
2565 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2566 // the enclosing section.
2567 void
2568 set_address_and_file_offset(uint64_t address, off_t file_offset,
2569 off_t section_file_offset);
2571 // Reset the address and file offset.
2572 void
2573 reset_address_and_file_offset();
2575 // Finalize the data size.
2576 void
2577 finalize_data_size();
2579 // Add an input section, for SHF_MERGE sections.
2580 bool
2581 add_input_section(Relobj* object, unsigned int shndx)
2583 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2584 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2585 return this->u2_.posd->add_input_section(object, shndx);
2588 // Given an input OBJECT, an input section index SHNDX within that
2589 // object, and an OFFSET relative to the start of that input
2590 // section, return whether or not the output offset is known. If
2591 // this function returns true, it sets *POUTPUT to the offset in
2592 // the output section, relative to the start of the input section
2593 // in the output section. *POUTPUT may be different from OFFSET
2594 // for a merged section.
2595 bool
2596 output_offset(const Relobj* object, unsigned int shndx,
2597 section_offset_type offset,
2598 section_offset_type *poutput) const;
2600 // Return whether this is the merge section for the input section
2601 // SHNDX in OBJECT.
2602 bool
2603 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2605 // Write out the data. This does nothing for an input section.
2606 void
2607 write(Output_file*);
2609 // Write the data to a buffer. This does nothing for an input
2610 // section.
2611 void
2612 write_to_buffer(unsigned char*);
2614 // Print to a map file.
2615 void
2616 print_to_mapfile(Mapfile*) const;
2618 // Print statistics about merge sections to stderr.
2619 void
2620 print_merge_stats(const char* section_name)
2622 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2623 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2624 this->u2_.posd->print_merge_stats(section_name);
2627 private:
2628 // Code values which appear in shndx_. If the value is not one of
2629 // these codes, it is the input section index in the object file.
2630 enum
2632 // An Output_section_data.
2633 OUTPUT_SECTION_CODE = -1U,
2634 // An Output_section_data for an SHF_MERGE section with
2635 // SHF_STRINGS not set.
2636 MERGE_DATA_SECTION_CODE = -2U,
2637 // An Output_section_data for an SHF_MERGE section with
2638 // SHF_STRINGS set.
2639 MERGE_STRING_SECTION_CODE = -3U
2642 // For an ordinary input section, this is the section index in the
2643 // input file. For an Output_section_data, this is
2644 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2645 // MERGE_STRING_SECTION_CODE.
2646 unsigned int shndx_;
2647 // The required alignment, stored as a power of 2.
2648 unsigned int p2align_;
2649 union
2651 // For an ordinary input section, the section size.
2652 off_t data_size;
2653 // For OUTPUT_SECTION_CODE, this is not used. For
2654 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2655 // entity size.
2656 uint64_t entsize;
2657 } u1_;
2658 union
2660 // For an ordinary input section, the object which holds the
2661 // input section.
2662 Relobj* object;
2663 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2664 // MERGE_STRING_SECTION_CODE, the data.
2665 Output_section_data* posd;
2666 } u2_;
2669 typedef std::vector<Input_section> Input_section_list;
2671 // This class is used to sort the input sections.
2672 class Input_section_sort_entry;
2674 // This is the sort comparison function.
2675 struct Input_section_sort_compare
2677 bool
2678 operator()(const Input_section_sort_entry&,
2679 const Input_section_sort_entry&) const;
2682 // Fill data. This is used to fill in data between input sections.
2683 // It is also used for data statements (BYTE, WORD, etc.) in linker
2684 // scripts. When we have to keep track of the input sections, we
2685 // can use an Output_data_const, but we don't want to have to keep
2686 // track of input sections just to implement fills.
2687 class Fill
2689 public:
2690 Fill(off_t section_offset, off_t length)
2691 : section_offset_(section_offset),
2692 length_(convert_to_section_size_type(length))
2695 // Return section offset.
2696 off_t
2697 section_offset() const
2698 { return this->section_offset_; }
2700 // Return fill length.
2701 section_size_type
2702 length() const
2703 { return this->length_; }
2705 private:
2706 // The offset within the output section.
2707 off_t section_offset_;
2708 // The length of the space to fill.
2709 section_size_type length_;
2712 typedef std::vector<Fill> Fill_list;
2714 // Add a new output section by Input_section.
2715 void
2716 add_output_section_data(Input_section*);
2718 // Add an SHF_MERGE input section. Returns true if the section was
2719 // handled.
2720 bool
2721 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2722 uint64_t entsize, uint64_t addralign);
2724 // Add an output SHF_MERGE section POSD to this output section.
2725 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2726 // ENTSIZE is the entity size. This returns the entry added to
2727 // input_sections_.
2728 void
2729 add_output_merge_section(Output_section_data* posd, bool is_string,
2730 uint64_t entsize);
2732 // Sort the attached input sections.
2733 void
2734 sort_attached_input_sections();
2736 // Most of these fields are only valid after layout.
2738 // The name of the section. This will point into a Stringpool.
2739 const char* name_;
2740 // The section address is in the parent class.
2741 // The section alignment.
2742 uint64_t addralign_;
2743 // The section entry size.
2744 uint64_t entsize_;
2745 // The load address. This is only used when using a linker script
2746 // with a SECTIONS clause. The has_load_address_ field indicates
2747 // whether this field is valid.
2748 uint64_t load_address_;
2749 // The file offset is in the parent class.
2750 // Set the section link field to the index of this section.
2751 const Output_data* link_section_;
2752 // If link_section_ is NULL, this is the link field.
2753 unsigned int link_;
2754 // Set the section info field to the index of this section.
2755 const Output_section* info_section_;
2756 // If info_section_ is NULL, set the info field to the symbol table
2757 // index of this symbol.
2758 const Symbol* info_symndx_;
2759 // If info_section_ and info_symndx_ are NULL, this is the section
2760 // info field.
2761 unsigned int info_;
2762 // The section type.
2763 const elfcpp::Elf_Word type_;
2764 // The section flags.
2765 elfcpp::Elf_Xword flags_;
2766 // The section index.
2767 unsigned int out_shndx_;
2768 // If there is a STT_SECTION for this output section in the normal
2769 // symbol table, this is the symbol index. This starts out as zero.
2770 // It is initialized in Layout::finalize() to be the index, or -1U
2771 // if there isn't one.
2772 unsigned int symtab_index_;
2773 // If there is a STT_SECTION for this output section in the dynamic
2774 // symbol table, this is the symbol index. This starts out as zero.
2775 // It is initialized in Layout::finalize() to be the index, or -1U
2776 // if there isn't one.
2777 unsigned int dynsym_index_;
2778 // The input sections. This will be empty in cases where we don't
2779 // need to keep track of them.
2780 Input_section_list input_sections_;
2781 // The offset of the first entry in input_sections_.
2782 off_t first_input_offset_;
2783 // The fill data. This is separate from input_sections_ because we
2784 // often will need fill sections without needing to keep track of
2785 // input sections.
2786 Fill_list fills_;
2787 // If the section requires postprocessing, this buffer holds the
2788 // section contents during relocation.
2789 unsigned char* postprocessing_buffer_;
2790 // Whether this output section needs a STT_SECTION symbol in the
2791 // normal symbol table. This will be true if there is a relocation
2792 // which needs it.
2793 bool needs_symtab_index_ : 1;
2794 // Whether this output section needs a STT_SECTION symbol in the
2795 // dynamic symbol table. This will be true if there is a dynamic
2796 // relocation which needs it.
2797 bool needs_dynsym_index_ : 1;
2798 // Whether the link field of this output section should point to the
2799 // normal symbol table.
2800 bool should_link_to_symtab_ : 1;
2801 // Whether the link field of this output section should point to the
2802 // dynamic symbol table.
2803 bool should_link_to_dynsym_ : 1;
2804 // Whether this section should be written after all the input
2805 // sections are complete.
2806 bool after_input_sections_ : 1;
2807 // Whether this section requires post processing after all
2808 // relocations have been applied.
2809 bool requires_postprocessing_ : 1;
2810 // Whether an input section was mapped to this output section
2811 // because of a SECTIONS clause in a linker script.
2812 bool found_in_sections_clause_ : 1;
2813 // Whether this section has an explicitly specified load address.
2814 bool has_load_address_ : 1;
2815 // True if the info_section_ field means the section index of the
2816 // section, false if it means the symbol index of the corresponding
2817 // section symbol.
2818 bool info_uses_section_index_ : 1;
2819 // True if the input sections attached to this output section may
2820 // need sorting.
2821 bool may_sort_attached_input_sections_ : 1;
2822 // True if the input sections attached to this output section must
2823 // be sorted.
2824 bool must_sort_attached_input_sections_ : 1;
2825 // True if the input sections attached to this output section have
2826 // already been sorted.
2827 bool attached_input_sections_are_sorted_ : 1;
2828 // True if this section holds relro data.
2829 bool is_relro_ : 1;
2830 // True if this section holds relro local data.
2831 bool is_relro_local_ : 1;
2832 // True if this is a small section.
2833 bool is_small_section_ : 1;
2834 // True if this is a large section.
2835 bool is_large_section_ : 1;
2836 // For SHT_TLS sections, the offset of this section relative to the base
2837 // of the TLS segment.
2838 uint64_t tls_offset_;
2841 // An output segment. PT_LOAD segments are built from collections of
2842 // output sections. Other segments typically point within PT_LOAD
2843 // segments, and are built directly as needed.
2845 class Output_segment
2847 public:
2848 // Create an output segment, specifying the type and flags.
2849 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2851 // Return the virtual address.
2852 uint64_t
2853 vaddr() const
2854 { return this->vaddr_; }
2856 // Return the physical address.
2857 uint64_t
2858 paddr() const
2859 { return this->paddr_; }
2861 // Return the segment type.
2862 elfcpp::Elf_Word
2863 type() const
2864 { return this->type_; }
2866 // Return the segment flags.
2867 elfcpp::Elf_Word
2868 flags() const
2869 { return this->flags_; }
2871 // Return the memory size.
2872 uint64_t
2873 memsz() const
2874 { return this->memsz_; }
2876 // Return the file size.
2877 off_t
2878 filesz() const
2879 { return this->filesz_; }
2881 // Return the file offset.
2882 off_t
2883 offset() const
2884 { return this->offset_; }
2886 // Whether this is a segment created to hold large data sections.
2887 bool
2888 is_large_data_segment() const
2889 { return this->is_large_data_segment_; }
2891 // Record that this is a segment created to hold large data
2892 // sections.
2893 void
2894 set_is_large_data_segment()
2895 { this->is_large_data_segment_ = true; }
2897 // Return the maximum alignment of the Output_data.
2898 uint64_t
2899 maximum_alignment();
2901 // Add an Output_section to this segment.
2902 void
2903 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags);
2905 // Remove an Output_section from this segment. It is an error if it
2906 // is not present.
2907 void
2908 remove_output_section(Output_section* os);
2910 // Add an Output_data (which is not an Output_section) to the start
2911 // of this segment.
2912 void
2913 add_initial_output_data(Output_data*);
2915 // Return true if this segment has any sections which hold actual
2916 // data, rather than being a BSS section.
2917 bool
2918 has_any_data_sections() const
2919 { return !this->output_data_.empty(); }
2921 // Return the number of dynamic relocations applied to this segment.
2922 unsigned int
2923 dynamic_reloc_count() const;
2925 // Return the address of the first section.
2926 uint64_t
2927 first_section_load_address() const;
2929 // Return whether the addresses have been set already.
2930 bool
2931 are_addresses_set() const
2932 { return this->are_addresses_set_; }
2934 // Set the addresses.
2935 void
2936 set_addresses(uint64_t vaddr, uint64_t paddr)
2938 this->vaddr_ = vaddr;
2939 this->paddr_ = paddr;
2940 this->are_addresses_set_ = true;
2943 // Set the segment flags. This is only used if we have a PHDRS
2944 // clause which explicitly specifies the flags.
2945 void
2946 set_flags(elfcpp::Elf_Word flags)
2947 { this->flags_ = flags; }
2949 // Set the address of the segment to ADDR and the offset to *POFF
2950 // and set the addresses and offsets of all contained output
2951 // sections accordingly. Set the section indexes of all contained
2952 // output sections starting with *PSHNDX. If RESET is true, first
2953 // reset the addresses of the contained sections. Return the
2954 // address of the immediately following segment. Update *POFF and
2955 // *PSHNDX. This should only be called for a PT_LOAD segment.
2956 uint64_t
2957 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
2958 unsigned int* pshndx);
2960 // Set the minimum alignment of this segment. This may be adjusted
2961 // upward based on the section alignments.
2962 void
2963 set_minimum_p_align(uint64_t align)
2964 { this->min_p_align_ = align; }
2966 // Set the offset of this segment based on the section. This should
2967 // only be called for a non-PT_LOAD segment.
2968 void
2969 set_offset();
2971 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2972 void
2973 set_tls_offsets();
2975 // Return the number of output sections.
2976 unsigned int
2977 output_section_count() const;
2979 // Return the section attached to the list segment with the lowest
2980 // load address. This is used when handling a PHDRS clause in a
2981 // linker script.
2982 Output_section*
2983 section_with_lowest_load_address() const;
2985 // Write the segment header into *OPHDR.
2986 template<int size, bool big_endian>
2987 void
2988 write_header(elfcpp::Phdr_write<size, big_endian>*);
2990 // Write the section headers of associated sections into V.
2991 template<int size, bool big_endian>
2992 unsigned char*
2993 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2994 unsigned int* pshndx) const;
2996 // Print the output sections in the map file.
2997 void
2998 print_sections_to_mapfile(Mapfile*) const;
3000 private:
3001 Output_segment(const Output_segment&);
3002 Output_segment& operator=(const Output_segment&);
3004 typedef std::list<Output_data*> Output_data_list;
3006 // Find the maximum alignment in an Output_data_list.
3007 static uint64_t
3008 maximum_alignment_list(const Output_data_list*);
3010 // Return whether the first data section is a relro section.
3011 bool
3012 is_first_section_relro() const;
3014 // Set the section addresses in an Output_data_list.
3015 uint64_t
3016 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3017 uint64_t addr, off_t* poff, unsigned int* pshndx,
3018 bool* in_tls, bool* in_relro);
3020 // Return the number of Output_sections in an Output_data_list.
3021 unsigned int
3022 output_section_count_list(const Output_data_list*) const;
3024 // Return the number of dynamic relocs in an Output_data_list.
3025 unsigned int
3026 dynamic_reloc_count_list(const Output_data_list*) const;
3028 // Find the section with the lowest load address in an
3029 // Output_data_list.
3030 void
3031 lowest_load_address_in_list(const Output_data_list* pdl,
3032 Output_section** found,
3033 uint64_t* found_lma) const;
3035 // Write the section headers in the list into V.
3036 template<int size, bool big_endian>
3037 unsigned char*
3038 write_section_headers_list(const Layout*, const Stringpool*,
3039 const Output_data_list*, unsigned char* v,
3040 unsigned int* pshdx) const;
3042 // Print a section list to the mapfile.
3043 void
3044 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3046 // The list of output data with contents attached to this segment.
3047 Output_data_list output_data_;
3048 // The list of output data without contents attached to this segment.
3049 Output_data_list output_bss_;
3050 // The segment virtual address.
3051 uint64_t vaddr_;
3052 // The segment physical address.
3053 uint64_t paddr_;
3054 // The size of the segment in memory.
3055 uint64_t memsz_;
3056 // The maximum section alignment. The is_max_align_known_ field
3057 // indicates whether this has been finalized.
3058 uint64_t max_align_;
3059 // The required minimum value for the p_align field. This is used
3060 // for PT_LOAD segments. Note that this does not mean that
3061 // addresses should be aligned to this value; it means the p_paddr
3062 // and p_vaddr fields must be congruent modulo this value. For
3063 // non-PT_LOAD segments, the dynamic linker works more efficiently
3064 // if the p_align field has the more conventional value, although it
3065 // can align as needed.
3066 uint64_t min_p_align_;
3067 // The offset of the segment data within the file.
3068 off_t offset_;
3069 // The size of the segment data in the file.
3070 off_t filesz_;
3071 // The segment type;
3072 elfcpp::Elf_Word type_;
3073 // The segment flags.
3074 elfcpp::Elf_Word flags_;
3075 // Whether we have finalized max_align_.
3076 bool is_max_align_known_ : 1;
3077 // Whether vaddr and paddr were set by a linker script.
3078 bool are_addresses_set_ : 1;
3079 // Whether this segment holds large data sections.
3080 bool is_large_data_segment_ : 1;
3083 // This class represents the output file.
3085 class Output_file
3087 public:
3088 Output_file(const char* name);
3090 // Indicate that this is a temporary file which should not be
3091 // output.
3092 void
3093 set_is_temporary()
3094 { this->is_temporary_ = true; }
3096 // Open the output file. FILE_SIZE is the final size of the file.
3097 void
3098 open(off_t file_size);
3100 // Resize the output file.
3101 void
3102 resize(off_t file_size);
3104 // Close the output file (flushing all buffered data) and make sure
3105 // there are no errors.
3106 void
3107 close();
3109 // We currently always use mmap which makes the view handling quite
3110 // simple. In the future we may support other approaches.
3112 // Write data to the output file.
3113 void
3114 write(off_t offset, const void* data, size_t len)
3115 { memcpy(this->base_ + offset, data, len); }
3117 // Get a buffer to use to write to the file, given the offset into
3118 // the file and the size.
3119 unsigned char*
3120 get_output_view(off_t start, size_t size)
3122 gold_assert(start >= 0
3123 && start + static_cast<off_t>(size) <= this->file_size_);
3124 return this->base_ + start;
3127 // VIEW must have been returned by get_output_view. Write the
3128 // buffer to the file, passing in the offset and the size.
3129 void
3130 write_output_view(off_t, size_t, unsigned char*)
3133 // Get a read/write buffer. This is used when we want to write part
3134 // of the file, read it in, and write it again.
3135 unsigned char*
3136 get_input_output_view(off_t start, size_t size)
3137 { return this->get_output_view(start, size); }
3139 // Write a read/write buffer back to the file.
3140 void
3141 write_input_output_view(off_t, size_t, unsigned char*)
3144 // Get a read buffer. This is used when we just want to read part
3145 // of the file back it in.
3146 const unsigned char*
3147 get_input_view(off_t start, size_t size)
3148 { return this->get_output_view(start, size); }
3150 // Release a read bfufer.
3151 void
3152 free_input_view(off_t, size_t, const unsigned char*)
3155 private:
3156 // Map the file into memory.
3157 void
3158 map();
3160 // Allocate anonymous memory for the file.
3161 void*
3162 map_anonymous();
3164 // Unmap the file from memory (and flush to disk buffers).
3165 void
3166 unmap();
3168 // File name.
3169 const char* name_;
3170 // File descriptor.
3171 int o_;
3172 // File size.
3173 off_t file_size_;
3174 // Base of file mapped into memory.
3175 unsigned char* base_;
3176 // True iff base_ points to a memory buffer rather than an output file.
3177 bool map_is_anonymous_;
3178 // True if this is a temporary file which should not be output.
3179 bool is_temporary_;
3182 } // End namespace gold.
3184 #endif // !defined(GOLD_OUTPUT_H)