PR ld/11843
[binutils.git] / gold / output.h
blob78c51ce7b089d28fc102e0526372f4f484fb9217
1 // output.h -- manage the output file for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
26 #include <list>
27 #include <vector>
29 #include "elfcpp.h"
30 #include "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
34 namespace gold
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_merge_base;
42 class Output_section;
43 class Relocatable_relocs;
44 class Target;
45 template<int size, bool big_endian>
46 class Sized_target;
47 template<int size, bool big_endian>
48 class Sized_relobj;
50 // An abtract class for data which has to go into the output file.
52 class Output_data
54 public:
55 explicit Output_data()
56 : address_(0), data_size_(0), offset_(-1),
57 is_address_valid_(false), is_data_size_valid_(false),
58 is_offset_valid_(false), is_data_size_fixed_(false),
59 has_dynamic_reloc_(false)
60 { }
62 virtual
63 ~Output_data();
65 // Return the address. For allocated sections, this is only valid
66 // after Layout::finalize is finished.
67 uint64_t
68 address() const
70 gold_assert(this->is_address_valid_);
71 return this->address_;
74 // Return the size of the data. For allocated sections, this must
75 // be valid after Layout::finalize calls set_address, but need not
76 // be valid before then.
77 off_t
78 data_size() const
80 gold_assert(this->is_data_size_valid_);
81 return this->data_size_;
84 // Return true if data size is fixed.
85 bool
86 is_data_size_fixed() const
87 { return this->is_data_size_fixed_; }
89 // Return the file offset. This is only valid after
90 // Layout::finalize is finished. For some non-allocated sections,
91 // it may not be valid until near the end of the link.
92 off_t
93 offset() const
95 gold_assert(this->is_offset_valid_);
96 return this->offset_;
99 // Reset the address and file offset. This essentially disables the
100 // sanity testing about duplicate and unknown settings.
101 void
102 reset_address_and_file_offset()
104 this->is_address_valid_ = false;
105 this->is_offset_valid_ = false;
106 if (!this->is_data_size_fixed_)
107 this->is_data_size_valid_ = false;
108 this->do_reset_address_and_file_offset();
111 // Return true if address and file offset already have reset values. In
112 // other words, calling reset_address_and_file_offset will not change them.
113 bool
114 address_and_file_offset_have_reset_values() const
115 { return this->do_address_and_file_offset_have_reset_values(); }
117 // Return the required alignment.
118 uint64_t
119 addralign() const
120 { return this->do_addralign(); }
122 // Return whether this has a load address.
123 bool
124 has_load_address() const
125 { return this->do_has_load_address(); }
127 // Return the load address.
128 uint64_t
129 load_address() const
130 { return this->do_load_address(); }
132 // Return whether this is an Output_section.
133 bool
134 is_section() const
135 { return this->do_is_section(); }
137 // Return whether this is an Output_section of the specified type.
138 bool
139 is_section_type(elfcpp::Elf_Word stt) const
140 { return this->do_is_section_type(stt); }
142 // Return whether this is an Output_section with the specified flag
143 // set.
144 bool
145 is_section_flag_set(elfcpp::Elf_Xword shf) const
146 { return this->do_is_section_flag_set(shf); }
148 // Return the output section that this goes in, if there is one.
149 Output_section*
150 output_section()
151 { return this->do_output_section(); }
153 const Output_section*
154 output_section() const
155 { return this->do_output_section(); }
157 // Return the output section index, if there is an output section.
158 unsigned int
159 out_shndx() const
160 { return this->do_out_shndx(); }
162 // Set the output section index, if this is an output section.
163 void
164 set_out_shndx(unsigned int shndx)
165 { this->do_set_out_shndx(shndx); }
167 // Set the address and file offset of this data, and finalize the
168 // size of the data. This is called during Layout::finalize for
169 // allocated sections.
170 void
171 set_address_and_file_offset(uint64_t addr, off_t off)
173 this->set_address(addr);
174 this->set_file_offset(off);
175 this->finalize_data_size();
178 // Set the address.
179 void
180 set_address(uint64_t addr)
182 gold_assert(!this->is_address_valid_);
183 this->address_ = addr;
184 this->is_address_valid_ = true;
187 // Set the file offset.
188 void
189 set_file_offset(off_t off)
191 gold_assert(!this->is_offset_valid_);
192 this->offset_ = off;
193 this->is_offset_valid_ = true;
196 // Finalize the data size.
197 void
198 finalize_data_size()
200 if (!this->is_data_size_valid_)
202 // Tell the child class to set the data size.
203 this->set_final_data_size();
204 gold_assert(this->is_data_size_valid_);
208 // Set the TLS offset. Called only for SHT_TLS sections.
209 void
210 set_tls_offset(uint64_t tls_base)
211 { this->do_set_tls_offset(tls_base); }
213 // Return the TLS offset, relative to the base of the TLS segment.
214 // Valid only for SHT_TLS sections.
215 uint64_t
216 tls_offset() const
217 { return this->do_tls_offset(); }
219 // Write the data to the output file. This is called after
220 // Layout::finalize is complete.
221 void
222 write(Output_file* file)
223 { this->do_write(file); }
225 // This is called by Layout::finalize to note that the sizes of
226 // allocated sections must now be fixed.
227 static void
228 layout_complete()
229 { Output_data::allocated_sizes_are_fixed = true; }
231 // Used to check that layout has been done.
232 static bool
233 is_layout_complete()
234 { return Output_data::allocated_sizes_are_fixed; }
236 // Note that a dynamic reloc has been applied to this data.
237 void
238 add_dynamic_reloc()
239 { this->has_dynamic_reloc_ = true; }
241 // Return whether a dynamic reloc has been applied.
242 bool
243 has_dynamic_reloc() const
244 { return this->has_dynamic_reloc_; }
246 // Whether the address is valid.
247 bool
248 is_address_valid() const
249 { return this->is_address_valid_; }
251 // Whether the file offset is valid.
252 bool
253 is_offset_valid() const
254 { return this->is_offset_valid_; }
256 // Whether the data size is valid.
257 bool
258 is_data_size_valid() const
259 { return this->is_data_size_valid_; }
261 // Print information to the map file.
262 void
263 print_to_mapfile(Mapfile* mapfile) const
264 { return this->do_print_to_mapfile(mapfile); }
266 protected:
267 // Functions that child classes may or in some cases must implement.
269 // Write the data to the output file.
270 virtual void
271 do_write(Output_file*) = 0;
273 // Return the required alignment.
274 virtual uint64_t
275 do_addralign() const = 0;
277 // Return whether this has a load address.
278 virtual bool
279 do_has_load_address() const
280 { return false; }
282 // Return the load address.
283 virtual uint64_t
284 do_load_address() const
285 { gold_unreachable(); }
287 // Return whether this is an Output_section.
288 virtual bool
289 do_is_section() const
290 { return false; }
292 // Return whether this is an Output_section of the specified type.
293 // This only needs to be implement by Output_section.
294 virtual bool
295 do_is_section_type(elfcpp::Elf_Word) const
296 { return false; }
298 // Return whether this is an Output_section with the specific flag
299 // set. This only needs to be implemented by Output_section.
300 virtual bool
301 do_is_section_flag_set(elfcpp::Elf_Xword) const
302 { return false; }
304 // Return the output section, if there is one.
305 virtual Output_section*
306 do_output_section()
307 { return NULL; }
309 virtual const Output_section*
310 do_output_section() const
311 { return NULL; }
313 // Return the output section index, if there is an output section.
314 virtual unsigned int
315 do_out_shndx() const
316 { gold_unreachable(); }
318 // Set the output section index, if this is an output section.
319 virtual void
320 do_set_out_shndx(unsigned int)
321 { gold_unreachable(); }
323 // This is a hook for derived classes to set the data size. This is
324 // called by finalize_data_size, normally called during
325 // Layout::finalize, when the section address is set.
326 virtual void
327 set_final_data_size()
328 { gold_unreachable(); }
330 // A hook for resetting the address and file offset.
331 virtual void
332 do_reset_address_and_file_offset()
335 // Return true if address and file offset already have reset values. In
336 // other words, calling reset_address_and_file_offset will not change them.
337 // A child class overriding do_reset_address_and_file_offset may need to
338 // also override this.
339 virtual bool
340 do_address_and_file_offset_have_reset_values() const
341 { return !this->is_address_valid_ && !this->is_offset_valid_; }
343 // Set the TLS offset. Called only for SHT_TLS sections.
344 virtual void
345 do_set_tls_offset(uint64_t)
346 { gold_unreachable(); }
348 // Return the TLS offset, relative to the base of the TLS segment.
349 // Valid only for SHT_TLS sections.
350 virtual uint64_t
351 do_tls_offset() const
352 { gold_unreachable(); }
354 // Print to the map file. This only needs to be implemented by
355 // classes which may appear in a PT_LOAD segment.
356 virtual void
357 do_print_to_mapfile(Mapfile*) const
358 { gold_unreachable(); }
360 // Functions that child classes may call.
362 // Reset the address. The Output_section class needs this when an
363 // SHF_ALLOC input section is added to an output section which was
364 // formerly not SHF_ALLOC.
365 void
366 mark_address_invalid()
367 { this->is_address_valid_ = false; }
369 // Set the size of the data.
370 void
371 set_data_size(off_t data_size)
373 gold_assert(!this->is_data_size_valid_
374 && !this->is_data_size_fixed_);
375 this->data_size_ = data_size;
376 this->is_data_size_valid_ = true;
379 // Fix the data size. Once it is fixed, it cannot be changed
380 // and the data size remains always valid.
381 void
382 fix_data_size()
384 gold_assert(this->is_data_size_valid_);
385 this->is_data_size_fixed_ = true;
388 // Get the current data size--this is for the convenience of
389 // sections which build up their size over time.
390 off_t
391 current_data_size_for_child() const
392 { return this->data_size_; }
394 // Set the current data size--this is for the convenience of
395 // sections which build up their size over time.
396 void
397 set_current_data_size_for_child(off_t data_size)
399 gold_assert(!this->is_data_size_valid_);
400 this->data_size_ = data_size;
403 // Return default alignment for the target size.
404 static uint64_t
405 default_alignment();
407 // Return default alignment for a specified size--32 or 64.
408 static uint64_t
409 default_alignment_for_size(int size);
411 private:
412 Output_data(const Output_data&);
413 Output_data& operator=(const Output_data&);
415 // This is used for verification, to make sure that we don't try to
416 // change any sizes of allocated sections after we set the section
417 // addresses.
418 static bool allocated_sizes_are_fixed;
420 // Memory address in output file.
421 uint64_t address_;
422 // Size of data in output file.
423 off_t data_size_;
424 // File offset of contents in output file.
425 off_t offset_;
426 // Whether address_ is valid.
427 bool is_address_valid_ : 1;
428 // Whether data_size_ is valid.
429 bool is_data_size_valid_ : 1;
430 // Whether offset_ is valid.
431 bool is_offset_valid_ : 1;
432 // Whether data size is fixed.
433 bool is_data_size_fixed_ : 1;
434 // Whether any dynamic relocs have been applied to this section.
435 bool has_dynamic_reloc_ : 1;
438 // Output the section headers.
440 class Output_section_headers : public Output_data
442 public:
443 Output_section_headers(const Layout*,
444 const Layout::Segment_list*,
445 const Layout::Section_list*,
446 const Layout::Section_list*,
447 const Stringpool*,
448 const Output_section*);
450 protected:
451 // Write the data to the file.
452 void
453 do_write(Output_file*);
455 // Return the required alignment.
456 uint64_t
457 do_addralign() const
458 { return Output_data::default_alignment(); }
460 // Write to a map file.
461 void
462 do_print_to_mapfile(Mapfile* mapfile) const
463 { mapfile->print_output_data(this, _("** section headers")); }
465 // Set final data size.
466 void
467 set_final_data_size()
468 { this->set_data_size(this->do_size()); }
470 private:
471 // Write the data to the file with the right size and endianness.
472 template<int size, bool big_endian>
473 void
474 do_sized_write(Output_file*);
476 // Compute data size.
477 off_t
478 do_size() const;
480 const Layout* layout_;
481 const Layout::Segment_list* segment_list_;
482 const Layout::Section_list* section_list_;
483 const Layout::Section_list* unattached_section_list_;
484 const Stringpool* secnamepool_;
485 const Output_section* shstrtab_section_;
488 // Output the segment headers.
490 class Output_segment_headers : public Output_data
492 public:
493 Output_segment_headers(const Layout::Segment_list& segment_list);
495 protected:
496 // Write the data to the file.
497 void
498 do_write(Output_file*);
500 // Return the required alignment.
501 uint64_t
502 do_addralign() const
503 { return Output_data::default_alignment(); }
505 // Write to a map file.
506 void
507 do_print_to_mapfile(Mapfile* mapfile) const
508 { mapfile->print_output_data(this, _("** segment headers")); }
510 // Set final data size.
511 void
512 set_final_data_size()
513 { this->set_data_size(this->do_size()); }
515 private:
516 // Write the data to the file with the right size and endianness.
517 template<int size, bool big_endian>
518 void
519 do_sized_write(Output_file*);
521 // Compute the current size.
522 off_t
523 do_size() const;
525 const Layout::Segment_list& segment_list_;
528 // Output the ELF file header.
530 class Output_file_header : public Output_data
532 public:
533 Output_file_header(const Target*,
534 const Symbol_table*,
535 const Output_segment_headers*,
536 const char* entry);
538 // Add information about the section headers. We lay out the ELF
539 // file header before we create the section headers.
540 void set_section_info(const Output_section_headers*,
541 const Output_section* shstrtab);
543 protected:
544 // Write the data to the file.
545 void
546 do_write(Output_file*);
548 // Return the required alignment.
549 uint64_t
550 do_addralign() const
551 { return Output_data::default_alignment(); }
553 // Write to a map file.
554 void
555 do_print_to_mapfile(Mapfile* mapfile) const
556 { mapfile->print_output_data(this, _("** file header")); }
558 // Set final data size.
559 void
560 set_final_data_size(void)
561 { this->set_data_size(this->do_size()); }
563 private:
564 // Write the data to the file with the right size and endianness.
565 template<int size, bool big_endian>
566 void
567 do_sized_write(Output_file*);
569 // Return the value to use for the entry address.
570 template<int size>
571 typename elfcpp::Elf_types<size>::Elf_Addr
572 entry();
574 // Compute the current data size.
575 off_t
576 do_size() const;
578 const Target* target_;
579 const Symbol_table* symtab_;
580 const Output_segment_headers* segment_header_;
581 const Output_section_headers* section_header_;
582 const Output_section* shstrtab_;
583 const char* entry_;
586 // Output sections are mainly comprised of input sections. However,
587 // there are cases where we have data to write out which is not in an
588 // input section. Output_section_data is used in such cases. This is
589 // an abstract base class.
591 class Output_section_data : public Output_data
593 public:
594 Output_section_data(off_t data_size, uint64_t addralign,
595 bool is_data_size_fixed)
596 : Output_data(), output_section_(NULL), addralign_(addralign)
598 this->set_data_size(data_size);
599 if (is_data_size_fixed)
600 this->fix_data_size();
603 Output_section_data(uint64_t addralign)
604 : Output_data(), output_section_(NULL), addralign_(addralign)
607 // Return the output section.
608 const Output_section*
609 output_section() const
610 { return this->output_section_; }
612 // Record the output section.
613 void
614 set_output_section(Output_section* os);
616 // Add an input section, for SHF_MERGE sections. This returns true
617 // if the section was handled.
618 bool
619 add_input_section(Relobj* object, unsigned int shndx)
620 { return this->do_add_input_section(object, shndx); }
622 // Given an input OBJECT, an input section index SHNDX within that
623 // object, and an OFFSET relative to the start of that input
624 // section, return whether or not the corresponding offset within
625 // the output section is known. If this function returns true, it
626 // sets *POUTPUT to the output offset. The value -1 indicates that
627 // this input offset is being discarded.
628 bool
629 output_offset(const Relobj* object, unsigned int shndx,
630 section_offset_type offset,
631 section_offset_type *poutput) const
632 { return this->do_output_offset(object, shndx, offset, poutput); }
634 // Return whether this is the merge section for the input section
635 // SHNDX in OBJECT. This should return true when output_offset
636 // would return true for some values of OFFSET.
637 bool
638 is_merge_section_for(const Relobj* object, unsigned int shndx) const
639 { return this->do_is_merge_section_for(object, shndx); }
641 // Write the contents to a buffer. This is used for sections which
642 // require postprocessing, such as compression.
643 void
644 write_to_buffer(unsigned char* buffer)
645 { this->do_write_to_buffer(buffer); }
647 // Print merge stats to stderr. This should only be called for
648 // SHF_MERGE sections.
649 void
650 print_merge_stats(const char* section_name)
651 { this->do_print_merge_stats(section_name); }
653 protected:
654 // The child class must implement do_write.
656 // The child class may implement specific adjustments to the output
657 // section.
658 virtual void
659 do_adjust_output_section(Output_section*)
662 // May be implemented by child class. Return true if the section
663 // was handled.
664 virtual bool
665 do_add_input_section(Relobj*, unsigned int)
666 { gold_unreachable(); }
668 // The child class may implement output_offset.
669 virtual bool
670 do_output_offset(const Relobj*, unsigned int, section_offset_type,
671 section_offset_type*) const
672 { return false; }
674 // The child class may implement is_merge_section_for.
675 virtual bool
676 do_is_merge_section_for(const Relobj*, unsigned int) const
677 { return false; }
679 // The child class may implement write_to_buffer. Most child
680 // classes can not appear in a compressed section, and they do not
681 // implement this.
682 virtual void
683 do_write_to_buffer(unsigned char*)
684 { gold_unreachable(); }
686 // Print merge statistics.
687 virtual void
688 do_print_merge_stats(const char*)
689 { gold_unreachable(); }
691 // Return the required alignment.
692 uint64_t
693 do_addralign() const
694 { return this->addralign_; }
696 // Return the output section.
697 Output_section*
698 do_output_section()
699 { return this->output_section_; }
701 const Output_section*
702 do_output_section() const
703 { return this->output_section_; }
705 // Return the section index of the output section.
706 unsigned int
707 do_out_shndx() const;
709 // Set the alignment.
710 void
711 set_addralign(uint64_t addralign);
713 private:
714 // The output section for this section.
715 Output_section* output_section_;
716 // The required alignment.
717 uint64_t addralign_;
720 // Some Output_section_data classes build up their data step by step,
721 // rather than all at once. This class provides an interface for
722 // them.
724 class Output_section_data_build : public Output_section_data
726 public:
727 Output_section_data_build(uint64_t addralign)
728 : Output_section_data(addralign)
731 // Get the current data size.
732 off_t
733 current_data_size() const
734 { return this->current_data_size_for_child(); }
736 // Set the current data size.
737 void
738 set_current_data_size(off_t data_size)
739 { this->set_current_data_size_for_child(data_size); }
741 protected:
742 // Set the final data size.
743 virtual void
744 set_final_data_size()
745 { this->set_data_size(this->current_data_size_for_child()); }
748 // A simple case of Output_data in which we have constant data to
749 // output.
751 class Output_data_const : public Output_section_data
753 public:
754 Output_data_const(const std::string& data, uint64_t addralign)
755 : Output_section_data(data.size(), addralign, true), data_(data)
758 Output_data_const(const char* p, off_t len, uint64_t addralign)
759 : Output_section_data(len, addralign, true), data_(p, len)
762 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
763 : Output_section_data(len, addralign, true),
764 data_(reinterpret_cast<const char*>(p), len)
767 protected:
768 // Write the data to the output file.
769 void
770 do_write(Output_file*);
772 // Write the data to a buffer.
773 void
774 do_write_to_buffer(unsigned char* buffer)
775 { memcpy(buffer, this->data_.data(), this->data_.size()); }
777 // Write to a map file.
778 void
779 do_print_to_mapfile(Mapfile* mapfile) const
780 { mapfile->print_output_data(this, _("** fill")); }
782 private:
783 std::string data_;
786 // Another version of Output_data with constant data, in which the
787 // buffer is allocated by the caller.
789 class Output_data_const_buffer : public Output_section_data
791 public:
792 Output_data_const_buffer(const unsigned char* p, off_t len,
793 uint64_t addralign, const char* map_name)
794 : Output_section_data(len, addralign, true),
795 p_(p), map_name_(map_name)
798 protected:
799 // Write the data the output file.
800 void
801 do_write(Output_file*);
803 // Write the data to a buffer.
804 void
805 do_write_to_buffer(unsigned char* buffer)
806 { memcpy(buffer, this->p_, this->data_size()); }
808 // Write to a map file.
809 void
810 do_print_to_mapfile(Mapfile* mapfile) const
811 { mapfile->print_output_data(this, _(this->map_name_)); }
813 private:
814 // The data to output.
815 const unsigned char* p_;
816 // Name to use in a map file. Maps are a rarely used feature, but
817 // the space usage is minor as aren't very many of these objects.
818 const char* map_name_;
821 // A place holder for a fixed amount of data written out via some
822 // other mechanism.
824 class Output_data_fixed_space : public Output_section_data
826 public:
827 Output_data_fixed_space(off_t data_size, uint64_t addralign,
828 const char* map_name)
829 : Output_section_data(data_size, addralign, true),
830 map_name_(map_name)
833 protected:
834 // Write out the data--the actual data must be written out
835 // elsewhere.
836 void
837 do_write(Output_file*)
840 // Write to a map file.
841 void
842 do_print_to_mapfile(Mapfile* mapfile) const
843 { mapfile->print_output_data(this, _(this->map_name_)); }
845 private:
846 // Name to use in a map file. Maps are a rarely used feature, but
847 // the space usage is minor as aren't very many of these objects.
848 const char* map_name_;
851 // A place holder for variable sized data written out via some other
852 // mechanism.
854 class Output_data_space : public Output_section_data_build
856 public:
857 explicit Output_data_space(uint64_t addralign, const char* map_name)
858 : Output_section_data_build(addralign),
859 map_name_(map_name)
862 // Set the alignment.
863 void
864 set_space_alignment(uint64_t align)
865 { this->set_addralign(align); }
867 protected:
868 // Write out the data--the actual data must be written out
869 // elsewhere.
870 void
871 do_write(Output_file*)
874 // Write to a map file.
875 void
876 do_print_to_mapfile(Mapfile* mapfile) const
877 { mapfile->print_output_data(this, _(this->map_name_)); }
879 private:
880 // Name to use in a map file. Maps are a rarely used feature, but
881 // the space usage is minor as aren't very many of these objects.
882 const char* map_name_;
885 // Fill fixed space with zeroes. This is just like
886 // Output_data_fixed_space, except that the map name is known.
888 class Output_data_zero_fill : public Output_section_data
890 public:
891 Output_data_zero_fill(off_t data_size, uint64_t addralign)
892 : Output_section_data(data_size, addralign, true)
895 protected:
896 // There is no data to write out.
897 void
898 do_write(Output_file*)
901 // Write to a map file.
902 void
903 do_print_to_mapfile(Mapfile* mapfile) const
904 { mapfile->print_output_data(this, "** zero fill"); }
907 // A string table which goes into an output section.
909 class Output_data_strtab : public Output_section_data
911 public:
912 Output_data_strtab(Stringpool* strtab)
913 : Output_section_data(1), strtab_(strtab)
916 protected:
917 // This is called to set the address and file offset. Here we make
918 // sure that the Stringpool is finalized.
919 void
920 set_final_data_size();
922 // Write out the data.
923 void
924 do_write(Output_file*);
926 // Write the data to a buffer.
927 void
928 do_write_to_buffer(unsigned char* buffer)
929 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
931 // Write to a map file.
932 void
933 do_print_to_mapfile(Mapfile* mapfile) const
934 { mapfile->print_output_data(this, _("** string table")); }
936 private:
937 Stringpool* strtab_;
940 // This POD class is used to represent a single reloc in the output
941 // file. This could be a private class within Output_data_reloc, but
942 // the templatization is complex enough that I broke it out into a
943 // separate class. The class is templatized on either elfcpp::SHT_REL
944 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
945 // relocation or an ordinary relocation.
947 // A relocation can be against a global symbol, a local symbol, a
948 // local section symbol, an output section, or the undefined symbol at
949 // index 0. We represent the latter by using a NULL global symbol.
951 template<int sh_type, bool dynamic, int size, bool big_endian>
952 class Output_reloc;
954 template<bool dynamic, int size, bool big_endian>
955 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
957 public:
958 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
959 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
961 static const Address invalid_address = static_cast<Address>(0) - 1;
963 // An uninitialized entry. We need this because we want to put
964 // instances of this class into an STL container.
965 Output_reloc()
966 : local_sym_index_(INVALID_CODE)
969 // We have a bunch of different constructors. They come in pairs
970 // depending on how the address of the relocation is specified. It
971 // can either be an offset in an Output_data or an offset in an
972 // input section.
974 // A reloc against a global symbol.
976 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
977 Address address, bool is_relative, bool is_symbolless);
979 Output_reloc(Symbol* gsym, unsigned int type,
980 Sized_relobj<size, big_endian>* relobj,
981 unsigned int shndx, Address address, bool is_relative,
982 bool is_symbolless);
984 // A reloc against a local symbol or local section symbol.
986 Output_reloc(Sized_relobj<size, big_endian>* relobj,
987 unsigned int local_sym_index, unsigned int type,
988 Output_data* od, Address address, bool is_relative,
989 bool is_symbolless, bool is_section_symbol);
991 Output_reloc(Sized_relobj<size, big_endian>* relobj,
992 unsigned int local_sym_index, unsigned int type,
993 unsigned int shndx, Address address, bool is_relative,
994 bool is_symbolless, bool is_section_symbol);
996 // A reloc against the STT_SECTION symbol of an output section.
998 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
999 Address address);
1001 Output_reloc(Output_section* os, unsigned int type,
1002 Sized_relobj<size, big_endian>* relobj,
1003 unsigned int shndx, Address address);
1005 // An absolute relocation with no symbol.
1007 Output_reloc(unsigned int type, Output_data* od, Address address);
1009 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1010 unsigned int shndx, Address address);
1012 // A target specific relocation. The target will be called to get
1013 // the symbol index, passing ARG. The type and offset will be set
1014 // as for other relocation types.
1016 Output_reloc(unsigned int type, void* arg, Output_data* od,
1017 Address address);
1019 Output_reloc(unsigned int type, void* arg,
1020 Sized_relobj<size, big_endian>* relobj,
1021 unsigned int shndx, Address address);
1023 // Return the reloc type.
1024 unsigned int
1025 type() const
1026 { return this->type_; }
1028 // Return whether this is a RELATIVE relocation.
1029 bool
1030 is_relative() const
1031 { return this->is_relative_; }
1033 // Return whether this is a relocation which should not use
1034 // a symbol, but which obtains its addend from a symbol.
1035 bool
1036 is_symbolless() const
1037 { return this->is_symbolless_; }
1039 // Return whether this is against a local section symbol.
1040 bool
1041 is_local_section_symbol() const
1043 return (this->local_sym_index_ != GSYM_CODE
1044 && this->local_sym_index_ != SECTION_CODE
1045 && this->local_sym_index_ != INVALID_CODE
1046 && this->local_sym_index_ != TARGET_CODE
1047 && this->is_section_symbol_);
1050 // Return whether this is a target specific relocation.
1051 bool
1052 is_target_specific() const
1053 { return this->local_sym_index_ == TARGET_CODE; }
1055 // Return the argument to pass to the target for a target specific
1056 // relocation.
1057 void*
1058 target_arg() const
1060 gold_assert(this->local_sym_index_ == TARGET_CODE);
1061 return this->u1_.arg;
1064 // For a local section symbol, return the offset of the input
1065 // section within the output section. ADDEND is the addend being
1066 // applied to the input section.
1067 Address
1068 local_section_offset(Addend addend) const;
1070 // Get the value of the symbol referred to by a Rel relocation when
1071 // we are adding the given ADDEND.
1072 Address
1073 symbol_value(Addend addend) const;
1075 // Write the reloc entry to an output view.
1076 void
1077 write(unsigned char* pov) const;
1079 // Write the offset and info fields to Write_rel.
1080 template<typename Write_rel>
1081 void write_rel(Write_rel*) const;
1083 // This is used when sorting dynamic relocs. Return -1 to sort this
1084 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1086 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1087 const;
1089 // Return whether this reloc should be sorted before the argument
1090 // when sorting dynamic relocs.
1091 bool
1092 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1093 r2) const
1094 { return this->compare(r2) < 0; }
1096 private:
1097 // Record that we need a dynamic symbol index.
1098 void
1099 set_needs_dynsym_index();
1101 // Return the symbol index.
1102 unsigned int
1103 get_symbol_index() const;
1105 // Return the output address.
1106 Address
1107 get_address() const;
1109 // Codes for local_sym_index_.
1110 enum
1112 // Global symbol.
1113 GSYM_CODE = -1U,
1114 // Output section.
1115 SECTION_CODE = -2U,
1116 // Target specific.
1117 TARGET_CODE = -3U,
1118 // Invalid uninitialized entry.
1119 INVALID_CODE = -4U
1122 union
1124 // For a local symbol or local section symbol
1125 // (this->local_sym_index_ >= 0), the object. We will never
1126 // generate a relocation against a local symbol in a dynamic
1127 // object; that doesn't make sense. And our callers will always
1128 // be templatized, so we use Sized_relobj here.
1129 Sized_relobj<size, big_endian>* relobj;
1130 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1131 // symbol. If this is NULL, it indicates a relocation against the
1132 // undefined 0 symbol.
1133 Symbol* gsym;
1134 // For a relocation against an output section
1135 // (this->local_sym_index_ == SECTION_CODE), the output section.
1136 Output_section* os;
1137 // For a target specific relocation, an argument to pass to the
1138 // target.
1139 void* arg;
1140 } u1_;
1141 union
1143 // If this->shndx_ is not INVALID CODE, the object which holds the
1144 // input section being used to specify the reloc address.
1145 Sized_relobj<size, big_endian>* relobj;
1146 // If this->shndx_ is INVALID_CODE, the output data being used to
1147 // specify the reloc address. This may be NULL if the reloc
1148 // address is absolute.
1149 Output_data* od;
1150 } u2_;
1151 // The address offset within the input section or the Output_data.
1152 Address address_;
1153 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1154 // relocation against an output section, or TARGET_CODE for a target
1155 // specific relocation, or INVALID_CODE for an uninitialized value.
1156 // Otherwise, for a local symbol (this->is_section_symbol_ is
1157 // false), the local symbol index. For a local section symbol
1158 // (this->is_section_symbol_ is true), the section index in the
1159 // input file.
1160 unsigned int local_sym_index_;
1161 // The reloc type--a processor specific code.
1162 unsigned int type_ : 29;
1163 // True if the relocation is a RELATIVE relocation.
1164 bool is_relative_ : 1;
1165 // True if the relocation is one which should not use
1166 // a symbol, but which obtains its addend from a symbol.
1167 bool is_symbolless_ : 1;
1168 // True if the relocation is against a section symbol.
1169 bool is_section_symbol_ : 1;
1170 // If the reloc address is an input section in an object, the
1171 // section index. This is INVALID_CODE if the reloc address is
1172 // specified in some other way.
1173 unsigned int shndx_;
1176 // The SHT_RELA version of Output_reloc<>. This is just derived from
1177 // the SHT_REL version of Output_reloc, but it adds an addend.
1179 template<bool dynamic, int size, bool big_endian>
1180 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1182 public:
1183 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1184 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1186 // An uninitialized entry.
1187 Output_reloc()
1188 : rel_()
1191 // A reloc against a global symbol.
1193 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1194 Address address, Addend addend, bool is_relative,
1195 bool is_symbolless)
1196 : rel_(gsym, type, od, address, is_relative, is_symbolless),
1197 addend_(addend)
1200 Output_reloc(Symbol* gsym, unsigned int type,
1201 Sized_relobj<size, big_endian>* relobj,
1202 unsigned int shndx, Address address, Addend addend,
1203 bool is_relative, bool is_symbolless)
1204 : rel_(gsym, type, relobj, shndx, address, is_relative,
1205 is_symbolless), addend_(addend)
1208 // A reloc against a local symbol.
1210 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1211 unsigned int local_sym_index, unsigned int type,
1212 Output_data* od, Address address,
1213 Addend addend, bool is_relative,
1214 bool is_symbolless, bool is_section_symbol)
1215 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1216 is_symbolless, is_section_symbol),
1217 addend_(addend)
1220 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1221 unsigned int local_sym_index, unsigned int type,
1222 unsigned int shndx, Address address,
1223 Addend addend, bool is_relative,
1224 bool is_symbolless, bool is_section_symbol)
1225 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1226 is_symbolless, is_section_symbol),
1227 addend_(addend)
1230 // A reloc against the STT_SECTION symbol of an output section.
1232 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1233 Address address, Addend addend)
1234 : rel_(os, type, od, address), addend_(addend)
1237 Output_reloc(Output_section* os, unsigned int type,
1238 Sized_relobj<size, big_endian>* relobj,
1239 unsigned int shndx, Address address, Addend addend)
1240 : rel_(os, type, relobj, shndx, address), addend_(addend)
1243 // An absolute relocation with no symbol.
1245 Output_reloc(unsigned int type, Output_data* od, Address address,
1246 Addend addend)
1247 : rel_(type, od, address), addend_(addend)
1250 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1251 unsigned int shndx, Address address, Addend addend)
1252 : rel_(type, relobj, shndx, address), addend_(addend)
1255 // A target specific relocation. The target will be called to get
1256 // the symbol index and the addend, passing ARG. The type and
1257 // offset will be set as for other relocation types.
1259 Output_reloc(unsigned int type, void* arg, Output_data* od,
1260 Address address, Addend addend)
1261 : rel_(type, arg, od, address), addend_(addend)
1264 Output_reloc(unsigned int type, void* arg,
1265 Sized_relobj<size, big_endian>* relobj,
1266 unsigned int shndx, Address address, Addend addend)
1267 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1270 // Return whether this is a RELATIVE relocation.
1271 bool
1272 is_relative() const
1273 { return this->rel_.is_relative(); }
1275 // Return whether this is a relocation which should not use
1276 // a symbol, but which obtains its addend from a symbol.
1277 bool
1278 is_symbolless() const
1279 { return this->rel_.is_symbolless(); }
1281 // Write the reloc entry to an output view.
1282 void
1283 write(unsigned char* pov) const;
1285 // Return whether this reloc should be sorted before the argument
1286 // when sorting dynamic relocs.
1287 bool
1288 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1289 r2) const
1291 int i = this->rel_.compare(r2.rel_);
1292 if (i < 0)
1293 return true;
1294 else if (i > 0)
1295 return false;
1296 else
1297 return this->addend_ < r2.addend_;
1300 private:
1301 // The basic reloc.
1302 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1303 // The addend.
1304 Addend addend_;
1307 // Output_data_reloc_generic is a non-template base class for
1308 // Output_data_reloc_base. This gives the generic code a way to hold
1309 // a pointer to a reloc section.
1311 class Output_data_reloc_generic : public Output_section_data_build
1313 public:
1314 Output_data_reloc_generic(int size, bool sort_relocs)
1315 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1316 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1319 // Return the number of relative relocs in this section.
1320 size_t
1321 relative_reloc_count() const
1322 { return this->relative_reloc_count_; }
1324 // Whether we should sort the relocs.
1325 bool
1326 sort_relocs() const
1327 { return this->sort_relocs_; }
1329 protected:
1330 // Note that we've added another relative reloc.
1331 void
1332 bump_relative_reloc_count()
1333 { ++this->relative_reloc_count_; }
1335 private:
1336 // The number of relative relocs added to this section. This is to
1337 // support DT_RELCOUNT.
1338 size_t relative_reloc_count_;
1339 // Whether to sort the relocations when writing them out, to make
1340 // the dynamic linker more efficient.
1341 bool sort_relocs_;
1344 // Output_data_reloc is used to manage a section containing relocs.
1345 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1346 // indicates whether this is a dynamic relocation or a normal
1347 // relocation. Output_data_reloc_base is a base class.
1348 // Output_data_reloc is the real class, which we specialize based on
1349 // the reloc type.
1351 template<int sh_type, bool dynamic, int size, bool big_endian>
1352 class Output_data_reloc_base : public Output_data_reloc_generic
1354 public:
1355 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1356 typedef typename Output_reloc_type::Address Address;
1357 static const int reloc_size =
1358 Reloc_types<sh_type, size, big_endian>::reloc_size;
1360 // Construct the section.
1361 Output_data_reloc_base(bool sort_relocs)
1362 : Output_data_reloc_generic(size, sort_relocs)
1365 protected:
1366 // Write out the data.
1367 void
1368 do_write(Output_file*);
1370 // Set the entry size and the link.
1371 void
1372 do_adjust_output_section(Output_section *os);
1374 // Write to a map file.
1375 void
1376 do_print_to_mapfile(Mapfile* mapfile) const
1378 mapfile->print_output_data(this,
1379 (dynamic
1380 ? _("** dynamic relocs")
1381 : _("** relocs")));
1384 // Add a relocation entry.
1385 void
1386 add(Output_data *od, const Output_reloc_type& reloc)
1388 this->relocs_.push_back(reloc);
1389 this->set_current_data_size(this->relocs_.size() * reloc_size);
1390 od->add_dynamic_reloc();
1391 if (reloc.is_relative())
1392 this->bump_relative_reloc_count();
1395 private:
1396 typedef std::vector<Output_reloc_type> Relocs;
1398 // The class used to sort the relocations.
1399 struct Sort_relocs_comparison
1401 bool
1402 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1403 { return r1.sort_before(r2); }
1406 // The relocations in this section.
1407 Relocs relocs_;
1410 // The class which callers actually create.
1412 template<int sh_type, bool dynamic, int size, bool big_endian>
1413 class Output_data_reloc;
1415 // The SHT_REL version of Output_data_reloc.
1417 template<bool dynamic, int size, bool big_endian>
1418 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1419 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1421 private:
1422 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1423 big_endian> Base;
1425 public:
1426 typedef typename Base::Output_reloc_type Output_reloc_type;
1427 typedef typename Output_reloc_type::Address Address;
1429 Output_data_reloc(bool sr)
1430 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1433 // Add a reloc against a global symbol.
1435 void
1436 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1437 { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
1439 void
1440 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1441 Sized_relobj<size, big_endian>* relobj,
1442 unsigned int shndx, Address address)
1443 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1444 false, false)); }
1446 // These are to simplify the Copy_relocs class.
1448 void
1449 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1450 Address addend)
1452 gold_assert(addend == 0);
1453 this->add_global(gsym, type, od, address);
1456 void
1457 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1458 Sized_relobj<size, big_endian>* relobj,
1459 unsigned int shndx, Address address, Address addend)
1461 gold_assert(addend == 0);
1462 this->add_global(gsym, type, od, relobj, shndx, address);
1465 // Add a RELATIVE reloc against a global symbol. The final relocation
1466 // will not reference the symbol.
1468 void
1469 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1470 Address address)
1471 { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
1473 void
1474 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1475 Sized_relobj<size, big_endian>* relobj,
1476 unsigned int shndx, Address address)
1478 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1479 true, true));
1482 // Add a global relocation which does not use a symbol for the relocation,
1483 // but which gets its addend from a symbol.
1485 void
1486 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1487 Output_data* od, Address address)
1488 { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1490 void
1491 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1492 Output_data* od,
1493 Sized_relobj<size, big_endian>* relobj,
1494 unsigned int shndx, Address address)
1496 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1497 false, true));
1500 // Add a reloc against a local symbol.
1502 void
1503 add_local(Sized_relobj<size, big_endian>* relobj,
1504 unsigned int local_sym_index, unsigned int type,
1505 Output_data* od, Address address)
1507 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1508 address, false, false, false));
1511 void
1512 add_local(Sized_relobj<size, big_endian>* relobj,
1513 unsigned int local_sym_index, unsigned int type,
1514 Output_data* od, unsigned int shndx, Address address)
1516 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1517 address, false, false, false));
1520 // Add a RELATIVE reloc against a local symbol.
1522 void
1523 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1524 unsigned int local_sym_index, unsigned int type,
1525 Output_data* od, Address address)
1527 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1528 address, true, true, false));
1531 void
1532 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1533 unsigned int local_sym_index, unsigned int type,
1534 Output_data* od, unsigned int shndx, Address address)
1536 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1537 address, true, true, false));
1540 // Add a local relocation which does not use a symbol for the relocation,
1541 // but which gets its addend from a symbol.
1543 void
1544 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1545 unsigned int local_sym_index, unsigned int type,
1546 Output_data* od, Address address)
1548 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1549 address, false, true, false));
1552 void
1553 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1554 unsigned int local_sym_index, unsigned int type,
1555 Output_data* od, unsigned int shndx,
1556 Address address)
1558 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1559 address, false, true, false));
1562 // Add a reloc against a local section symbol. This will be
1563 // converted into a reloc against the STT_SECTION symbol of the
1564 // output section.
1566 void
1567 add_local_section(Sized_relobj<size, big_endian>* relobj,
1568 unsigned int input_shndx, unsigned int type,
1569 Output_data* od, Address address)
1571 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1572 address, false, false, true));
1575 void
1576 add_local_section(Sized_relobj<size, big_endian>* relobj,
1577 unsigned int input_shndx, unsigned int type,
1578 Output_data* od, unsigned int shndx, Address address)
1580 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1581 address, false, false, true));
1584 // A reloc against the STT_SECTION symbol of an output section.
1585 // OS is the Output_section that the relocation refers to; OD is
1586 // the Output_data object being relocated.
1588 void
1589 add_output_section(Output_section* os, unsigned int type,
1590 Output_data* od, Address address)
1591 { this->add(od, Output_reloc_type(os, type, od, address)); }
1593 void
1594 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1595 Sized_relobj<size, big_endian>* relobj,
1596 unsigned int shndx, Address address)
1597 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1599 // Add an absolute relocation.
1601 void
1602 add_absolute(unsigned int type, Output_data* od, Address address)
1603 { this->add(od, Output_reloc_type(type, od, address)); }
1605 void
1606 add_absolute(unsigned int type, Output_data* od,
1607 Sized_relobj<size, big_endian>* relobj,
1608 unsigned int shndx, Address address)
1609 { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1611 // Add a target specific relocation. A target which calls this must
1612 // define the reloc_symbol_index and reloc_addend virtual functions.
1614 void
1615 add_target_specific(unsigned int type, void* arg, Output_data* od,
1616 Address address)
1617 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1619 void
1620 add_target_specific(unsigned int type, void* arg, Output_data* od,
1621 Sized_relobj<size, big_endian>* relobj,
1622 unsigned int shndx, Address address)
1623 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1626 // The SHT_RELA version of Output_data_reloc.
1628 template<bool dynamic, int size, bool big_endian>
1629 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1630 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1632 private:
1633 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1634 big_endian> Base;
1636 public:
1637 typedef typename Base::Output_reloc_type Output_reloc_type;
1638 typedef typename Output_reloc_type::Address Address;
1639 typedef typename Output_reloc_type::Addend Addend;
1641 Output_data_reloc(bool sr)
1642 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1645 // Add a reloc against a global symbol.
1647 void
1648 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1649 Address address, Addend addend)
1650 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1651 false, false)); }
1653 void
1654 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1655 Sized_relobj<size, big_endian>* relobj,
1656 unsigned int shndx, Address address,
1657 Addend addend)
1658 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1659 addend, false, false)); }
1661 // Add a RELATIVE reloc against a global symbol. The final output
1662 // relocation will not reference the symbol, but we must keep the symbol
1663 // information long enough to set the addend of the relocation correctly
1664 // when it is written.
1666 void
1667 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1668 Address address, Addend addend)
1669 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1670 true)); }
1672 void
1673 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1674 Sized_relobj<size, big_endian>* relobj,
1675 unsigned int shndx, Address address, Addend addend)
1676 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1677 addend, true, true)); }
1679 // Add a global relocation which does not use a symbol for the relocation,
1680 // but which gets its addend from a symbol.
1682 void
1683 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1684 Address address, Addend addend)
1685 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1686 false, true)); }
1688 void
1689 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1690 Output_data* od,
1691 Sized_relobj<size, big_endian>* relobj,
1692 unsigned int shndx, Address address, Addend addend)
1693 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1694 addend, false, true)); }
1696 // Add a reloc against a local symbol.
1698 void
1699 add_local(Sized_relobj<size, big_endian>* relobj,
1700 unsigned int local_sym_index, unsigned int type,
1701 Output_data* od, Address address, Addend addend)
1703 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1704 addend, false, false, false));
1707 void
1708 add_local(Sized_relobj<size, big_endian>* relobj,
1709 unsigned int local_sym_index, unsigned int type,
1710 Output_data* od, unsigned int shndx, Address address,
1711 Addend addend)
1713 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1714 address, addend, false, false, false));
1717 // Add a RELATIVE reloc against a local symbol.
1719 void
1720 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1721 unsigned int local_sym_index, unsigned int type,
1722 Output_data* od, Address address, Addend addend)
1724 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1725 addend, true, true, false));
1728 void
1729 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1730 unsigned int local_sym_index, unsigned int type,
1731 Output_data* od, unsigned int shndx, Address address,
1732 Addend addend)
1734 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1735 address, addend, true, true, false));
1738 // Add a local relocation which does not use a symbol for the relocation,
1739 // but which gets it's addend from a symbol.
1741 void
1742 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1743 unsigned int local_sym_index, unsigned int type,
1744 Output_data* od, Address address, Addend addend)
1746 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1747 addend, false, true, false));
1750 void
1751 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1752 unsigned int local_sym_index, unsigned int type,
1753 Output_data* od, unsigned int shndx,
1754 Address address, Addend addend)
1756 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1757 address, addend, false, true, false));
1760 // Add a reloc against a local section symbol. This will be
1761 // converted into a reloc against the STT_SECTION symbol of the
1762 // output section.
1764 void
1765 add_local_section(Sized_relobj<size, big_endian>* relobj,
1766 unsigned int input_shndx, unsigned int type,
1767 Output_data* od, Address address, Addend addend)
1769 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1770 addend, false, false, true));
1773 void
1774 add_local_section(Sized_relobj<size, big_endian>* relobj,
1775 unsigned int input_shndx, unsigned int type,
1776 Output_data* od, unsigned int shndx, Address address,
1777 Addend addend)
1779 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1780 address, addend, false, false, true));
1783 // A reloc against the STT_SECTION symbol of an output section.
1785 void
1786 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1787 Address address, Addend addend)
1788 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1790 void
1791 add_output_section(Output_section* os, unsigned int type,
1792 Sized_relobj<size, big_endian>* relobj,
1793 unsigned int shndx, Address address, Addend addend)
1794 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1795 addend)); }
1797 // Add an absolute relocation.
1799 void
1800 add_absolute(unsigned int type, Output_data* od, Address address,
1801 Addend addend)
1802 { this->add(od, Output_reloc_type(type, od, address, addend)); }
1804 void
1805 add_absolute(unsigned int type, Output_data* od,
1806 Sized_relobj<size, big_endian>* relobj,
1807 unsigned int shndx, Address address, Addend addend)
1808 { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1810 // Add a target specific relocation. A target which calls this must
1811 // define the reloc_symbol_index and reloc_addend virtual functions.
1813 void
1814 add_target_specific(unsigned int type, void* arg, Output_data* od,
1815 Address address, Addend addend)
1816 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1818 void
1819 add_target_specific(unsigned int type, void* arg, Output_data* od,
1820 Sized_relobj<size, big_endian>* relobj,
1821 unsigned int shndx, Address address, Addend addend)
1823 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1824 addend));
1828 // Output_relocatable_relocs represents a relocation section in a
1829 // relocatable link. The actual data is written out in the target
1830 // hook relocate_for_relocatable. This just saves space for it.
1832 template<int sh_type, int size, bool big_endian>
1833 class Output_relocatable_relocs : public Output_section_data
1835 public:
1836 Output_relocatable_relocs(Relocatable_relocs* rr)
1837 : Output_section_data(Output_data::default_alignment_for_size(size)),
1838 rr_(rr)
1841 void
1842 set_final_data_size();
1844 // Write out the data. There is nothing to do here.
1845 void
1846 do_write(Output_file*)
1849 // Write to a map file.
1850 void
1851 do_print_to_mapfile(Mapfile* mapfile) const
1852 { mapfile->print_output_data(this, _("** relocs")); }
1854 private:
1855 // The relocs associated with this input section.
1856 Relocatable_relocs* rr_;
1859 // Handle a GROUP section.
1861 template<int size, bool big_endian>
1862 class Output_data_group : public Output_section_data
1864 public:
1865 // The constructor clears *INPUT_SHNDXES.
1866 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1867 section_size_type entry_count,
1868 elfcpp::Elf_Word flags,
1869 std::vector<unsigned int>* input_shndxes);
1871 void
1872 do_write(Output_file*);
1874 // Write to a map file.
1875 void
1876 do_print_to_mapfile(Mapfile* mapfile) const
1877 { mapfile->print_output_data(this, _("** group")); }
1879 // Set final data size.
1880 void
1881 set_final_data_size()
1882 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1884 private:
1885 // The input object.
1886 Sized_relobj<size, big_endian>* relobj_;
1887 // The group flag word.
1888 elfcpp::Elf_Word flags_;
1889 // The section indexes of the input sections in this group.
1890 std::vector<unsigned int> input_shndxes_;
1893 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1894 // for one symbol--either a global symbol or a local symbol in an
1895 // object. The target specific code adds entries to the GOT as
1896 // needed.
1898 template<int size, bool big_endian>
1899 class Output_data_got : public Output_section_data_build
1901 public:
1902 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1903 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1904 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1906 Output_data_got()
1907 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1908 entries_()
1911 // Add an entry for a global symbol to the GOT. Return true if this
1912 // is a new GOT entry, false if the symbol was already in the GOT.
1913 bool
1914 add_global(Symbol* gsym, unsigned int got_type);
1916 // Add an entry for a global symbol to the GOT, and add a dynamic
1917 // relocation of type R_TYPE for the GOT entry.
1918 void
1919 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1920 Rel_dyn* rel_dyn, unsigned int r_type);
1922 void
1923 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1924 Rela_dyn* rela_dyn, unsigned int r_type);
1926 // Add a pair of entries for a global symbol to the GOT, and add
1927 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1928 void
1929 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1930 Rel_dyn* rel_dyn, unsigned int r_type_1,
1931 unsigned int r_type_2);
1933 void
1934 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1935 Rela_dyn* rela_dyn, unsigned int r_type_1,
1936 unsigned int r_type_2);
1938 // Add an entry for a local symbol to the GOT. This returns true if
1939 // this is a new GOT entry, false if the symbol already has a GOT
1940 // entry.
1941 bool
1942 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1943 unsigned int got_type);
1945 // Add an entry for a local symbol to the GOT, and add a dynamic
1946 // relocation of type R_TYPE for the GOT entry.
1947 void
1948 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1949 unsigned int sym_index, unsigned int got_type,
1950 Rel_dyn* rel_dyn, unsigned int r_type);
1952 void
1953 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1954 unsigned int sym_index, unsigned int got_type,
1955 Rela_dyn* rela_dyn, unsigned int r_type);
1957 // Add a pair of entries for a local symbol to the GOT, and add
1958 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1959 void
1960 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1961 unsigned int sym_index, unsigned int shndx,
1962 unsigned int got_type, Rel_dyn* rel_dyn,
1963 unsigned int r_type_1, unsigned int r_type_2);
1965 void
1966 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1967 unsigned int sym_index, unsigned int shndx,
1968 unsigned int got_type, Rela_dyn* rela_dyn,
1969 unsigned int r_type_1, unsigned int r_type_2);
1971 // Add a constant to the GOT. This returns the offset of the new
1972 // entry from the start of the GOT.
1973 unsigned int
1974 add_constant(Valtype constant)
1976 this->entries_.push_back(Got_entry(constant));
1977 this->set_got_size();
1978 return this->last_got_offset();
1981 protected:
1982 // Write out the GOT table.
1983 void
1984 do_write(Output_file*);
1986 // Write to a map file.
1987 void
1988 do_print_to_mapfile(Mapfile* mapfile) const
1989 { mapfile->print_output_data(this, _("** GOT")); }
1991 private:
1992 // This POD class holds a single GOT entry.
1993 class Got_entry
1995 public:
1996 // Create a zero entry.
1997 Got_entry()
1998 : local_sym_index_(CONSTANT_CODE)
1999 { this->u_.constant = 0; }
2001 // Create a global symbol entry.
2002 explicit Got_entry(Symbol* gsym)
2003 : local_sym_index_(GSYM_CODE)
2004 { this->u_.gsym = gsym; }
2006 // Create a local symbol entry.
2007 Got_entry(Sized_relobj<size, big_endian>* object,
2008 unsigned int local_sym_index)
2009 : local_sym_index_(local_sym_index)
2011 gold_assert(local_sym_index != GSYM_CODE
2012 && local_sym_index != CONSTANT_CODE);
2013 this->u_.object = object;
2016 // Create a constant entry. The constant is a host value--it will
2017 // be swapped, if necessary, when it is written out.
2018 explicit Got_entry(Valtype constant)
2019 : local_sym_index_(CONSTANT_CODE)
2020 { this->u_.constant = constant; }
2022 // Write the GOT entry to an output view.
2023 void
2024 write(unsigned char* pov) const;
2026 private:
2027 enum
2029 GSYM_CODE = -1U,
2030 CONSTANT_CODE = -2U
2033 union
2035 // For a local symbol, the object.
2036 Sized_relobj<size, big_endian>* object;
2037 // For a global symbol, the symbol.
2038 Symbol* gsym;
2039 // For a constant, the constant.
2040 Valtype constant;
2041 } u_;
2042 // For a local symbol, the local symbol index. This is GSYM_CODE
2043 // for a global symbol, or CONSTANT_CODE for a constant.
2044 unsigned int local_sym_index_;
2047 typedef std::vector<Got_entry> Got_entries;
2049 // Return the offset into the GOT of GOT entry I.
2050 unsigned int
2051 got_offset(unsigned int i) const
2052 { return i * (size / 8); }
2054 // Return the offset into the GOT of the last entry added.
2055 unsigned int
2056 last_got_offset() const
2057 { return this->got_offset(this->entries_.size() - 1); }
2059 // Set the size of the section.
2060 void
2061 set_got_size()
2062 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2064 // The list of GOT entries.
2065 Got_entries entries_;
2068 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2069 // section.
2071 class Output_data_dynamic : public Output_section_data
2073 public:
2074 Output_data_dynamic(Stringpool* pool)
2075 : Output_section_data(Output_data::default_alignment()),
2076 entries_(), pool_(pool)
2079 // Add a new dynamic entry with a fixed numeric value.
2080 void
2081 add_constant(elfcpp::DT tag, unsigned int val)
2082 { this->add_entry(Dynamic_entry(tag, val)); }
2084 // Add a new dynamic entry with the address of output data.
2085 void
2086 add_section_address(elfcpp::DT tag, const Output_data* od)
2087 { this->add_entry(Dynamic_entry(tag, od, false)); }
2089 // Add a new dynamic entry with the address of output data
2090 // plus a constant offset.
2091 void
2092 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2093 unsigned int offset)
2094 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2096 // Add a new dynamic entry with the size of output data.
2097 void
2098 add_section_size(elfcpp::DT tag, const Output_data* od)
2099 { this->add_entry(Dynamic_entry(tag, od, true)); }
2101 // Add a new dynamic entry with the total size of two output datas.
2102 void
2103 add_section_size(elfcpp::DT tag, const Output_data* od,
2104 const Output_data* od2)
2105 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2107 // Add a new dynamic entry with the address of a symbol.
2108 void
2109 add_symbol(elfcpp::DT tag, const Symbol* sym)
2110 { this->add_entry(Dynamic_entry(tag, sym)); }
2112 // Add a new dynamic entry with a string.
2113 void
2114 add_string(elfcpp::DT tag, const char* str)
2115 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2117 void
2118 add_string(elfcpp::DT tag, const std::string& str)
2119 { this->add_string(tag, str.c_str()); }
2121 protected:
2122 // Adjust the output section to set the entry size.
2123 void
2124 do_adjust_output_section(Output_section*);
2126 // Set the final data size.
2127 void
2128 set_final_data_size();
2130 // Write out the dynamic entries.
2131 void
2132 do_write(Output_file*);
2134 // Write to a map file.
2135 void
2136 do_print_to_mapfile(Mapfile* mapfile) const
2137 { mapfile->print_output_data(this, _("** dynamic")); }
2139 private:
2140 // This POD class holds a single dynamic entry.
2141 class Dynamic_entry
2143 public:
2144 // Create an entry with a fixed numeric value.
2145 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2146 : tag_(tag), offset_(DYNAMIC_NUMBER)
2147 { this->u_.val = val; }
2149 // Create an entry with the size or address of a section.
2150 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2151 : tag_(tag),
2152 offset_(section_size
2153 ? DYNAMIC_SECTION_SIZE
2154 : DYNAMIC_SECTION_ADDRESS)
2156 this->u_.od = od;
2157 this->od2 = NULL;
2160 // Create an entry with the size of two sections.
2161 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2162 : tag_(tag),
2163 offset_(DYNAMIC_SECTION_SIZE)
2165 this->u_.od = od;
2166 this->od2 = od2;
2169 // Create an entry with the address of a section plus a constant offset.
2170 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2171 : tag_(tag),
2172 offset_(offset)
2173 { this->u_.od = od; }
2175 // Create an entry with the address of a symbol.
2176 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2177 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2178 { this->u_.sym = sym; }
2180 // Create an entry with a string.
2181 Dynamic_entry(elfcpp::DT tag, const char* str)
2182 : tag_(tag), offset_(DYNAMIC_STRING)
2183 { this->u_.str = str; }
2185 // Return the tag of this entry.
2186 elfcpp::DT
2187 tag() const
2188 { return this->tag_; }
2190 // Write the dynamic entry to an output view.
2191 template<int size, bool big_endian>
2192 void
2193 write(unsigned char* pov, const Stringpool*) const;
2195 private:
2196 // Classification is encoded in the OFFSET field.
2197 enum Classification
2199 // Section address.
2200 DYNAMIC_SECTION_ADDRESS = 0,
2201 // Number.
2202 DYNAMIC_NUMBER = -1U,
2203 // Section size.
2204 DYNAMIC_SECTION_SIZE = -2U,
2205 // Symbol adress.
2206 DYNAMIC_SYMBOL = -3U,
2207 // String.
2208 DYNAMIC_STRING = -4U
2209 // Any other value indicates a section address plus OFFSET.
2212 union
2214 // For DYNAMIC_NUMBER.
2215 unsigned int val;
2216 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2217 const Output_data* od;
2218 // For DYNAMIC_SYMBOL.
2219 const Symbol* sym;
2220 // For DYNAMIC_STRING.
2221 const char* str;
2222 } u_;
2223 // For DYNAMIC_SYMBOL with two sections.
2224 const Output_data* od2;
2225 // The dynamic tag.
2226 elfcpp::DT tag_;
2227 // The type of entry (Classification) or offset within a section.
2228 unsigned int offset_;
2231 // Add an entry to the list.
2232 void
2233 add_entry(const Dynamic_entry& entry)
2234 { this->entries_.push_back(entry); }
2236 // Sized version of write function.
2237 template<int size, bool big_endian>
2238 void
2239 sized_write(Output_file* of);
2241 // The type of the list of entries.
2242 typedef std::vector<Dynamic_entry> Dynamic_entries;
2244 // The entries.
2245 Dynamic_entries entries_;
2246 // The pool used for strings.
2247 Stringpool* pool_;
2250 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2251 // which may be required if the object file has more than
2252 // SHN_LORESERVE sections.
2254 class Output_symtab_xindex : public Output_section_data
2256 public:
2257 Output_symtab_xindex(size_t symcount)
2258 : Output_section_data(symcount * 4, 4, true),
2259 entries_()
2262 // Add an entry: symbol number SYMNDX has section SHNDX.
2263 void
2264 add(unsigned int symndx, unsigned int shndx)
2265 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2267 protected:
2268 void
2269 do_write(Output_file*);
2271 // Write to a map file.
2272 void
2273 do_print_to_mapfile(Mapfile* mapfile) const
2274 { mapfile->print_output_data(this, _("** symtab xindex")); }
2276 private:
2277 template<bool big_endian>
2278 void
2279 endian_do_write(unsigned char*);
2281 // It is likely that most symbols will not require entries. Rather
2282 // than keep a vector for all symbols, we keep pairs of symbol index
2283 // and section index.
2284 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2286 // The entries we need.
2287 Xindex_entries entries_;
2290 // A relaxed input section.
2291 class Output_relaxed_input_section : public Output_section_data_build
2293 public:
2294 // We would like to call relobj->section_addralign(shndx) to get the
2295 // alignment but we do not want the constructor to fail. So callers
2296 // are repsonsible for ensuring that.
2297 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2298 uint64_t addralign)
2299 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2302 // Return the Relobj of this relaxed input section.
2303 Relobj*
2304 relobj() const
2305 { return this->relobj_; }
2307 // Return the section index of this relaxed input section.
2308 unsigned int
2309 shndx() const
2310 { return this->shndx_; }
2312 private:
2313 Relobj* relobj_;
2314 unsigned int shndx_;
2317 // This class describes properties of merge data sections. It is used
2318 // as a key type for maps.
2319 class Merge_section_properties
2321 public:
2322 Merge_section_properties(bool is_string, uint64_t entsize,
2323 uint64_t addralign)
2324 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2327 // Whether this equals to another Merge_section_properties MSP.
2328 bool
2329 eq(const Merge_section_properties& msp) const
2331 return ((this->is_string_ == msp.is_string_)
2332 && (this->entsize_ == msp.entsize_)
2333 && (this->addralign_ == msp.addralign_));
2336 // Compute a hash value for this using 64-bit FNV-1a hash.
2337 size_t
2338 hash_value() const
2340 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2341 uint64_t prime = 1099511628211ULL;
2342 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2343 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2344 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2345 return h;
2348 // Functors for associative containers.
2349 struct equal_to
2351 bool
2352 operator()(const Merge_section_properties& msp1,
2353 const Merge_section_properties& msp2) const
2354 { return msp1.eq(msp2); }
2357 struct hash
2359 size_t
2360 operator()(const Merge_section_properties& msp) const
2361 { return msp.hash_value(); }
2364 private:
2365 // Whether this merge data section is for strings.
2366 bool is_string_;
2367 // Entsize of this merge data section.
2368 uint64_t entsize_;
2369 // Address alignment.
2370 uint64_t addralign_;
2373 // This class is used to speed up look up of special input sections in an
2374 // Output_section.
2376 class Output_section_lookup_maps
2378 public:
2379 Output_section_lookup_maps()
2380 : is_valid_(true), merge_sections_by_properties_(),
2381 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2384 // Whether the maps are valid.
2385 bool
2386 is_valid() const
2387 { return this->is_valid_; }
2389 // Invalidate the maps.
2390 void
2391 invalidate()
2392 { this->is_valid_ = false; }
2394 // Clear the maps.
2395 void
2396 clear()
2398 this->merge_sections_by_properties_.clear();
2399 this->merge_sections_by_id_.clear();
2400 this->relaxed_input_sections_by_id_.clear();
2401 // A cleared map is valid.
2402 this->is_valid_ = true;
2405 // Find a merge section by merge section properties. Return NULL if none
2406 // is found.
2407 Output_merge_base*
2408 find_merge_section(const Merge_section_properties& msp) const
2410 gold_assert(this->is_valid_);
2411 Merge_sections_by_properties::const_iterator p =
2412 this->merge_sections_by_properties_.find(msp);
2413 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2416 // Find a merge section by section ID of a merge input section. Return NULL
2417 // if none is found.
2418 Output_merge_base*
2419 find_merge_section(const Object* object, unsigned int shndx) const
2421 gold_assert(this->is_valid_);
2422 Merge_sections_by_id::const_iterator p =
2423 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2424 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2427 // Add a merge section pointed by POMB with properties MSP.
2428 void
2429 add_merge_section(const Merge_section_properties& msp,
2430 Output_merge_base* pomb)
2432 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2433 std::pair<Merge_sections_by_properties::iterator, bool> result =
2434 this->merge_sections_by_properties_.insert(value);
2435 gold_assert(result.second);
2438 // Add a mapping from a merged input section in OBJECT with index SHNDX
2439 // to a merge output section pointed by POMB.
2440 void
2441 add_merge_input_section(const Object* object, unsigned int shndx,
2442 Output_merge_base* pomb)
2444 Const_section_id csid(object, shndx);
2445 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2446 std::pair<Merge_sections_by_id::iterator, bool> result =
2447 this->merge_sections_by_id_.insert(value);
2448 gold_assert(result.second);
2451 // Find a relaxed input section of OBJECT with index SHNDX.
2452 Output_relaxed_input_section*
2453 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2455 gold_assert(this->is_valid_);
2456 Relaxed_input_sections_by_id::const_iterator p =
2457 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2458 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2461 // Add a relaxed input section pointed by POMB and whose original input
2462 // section is in OBJECT with index SHNDX.
2463 void
2464 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2465 Output_relaxed_input_section* poris)
2467 Const_section_id csid(relobj, shndx);
2468 std::pair<Const_section_id, Output_relaxed_input_section*>
2469 value(csid, poris);
2470 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2471 this->relaxed_input_sections_by_id_.insert(value);
2472 gold_assert(result.second);
2475 private:
2476 typedef Unordered_map<Const_section_id, Output_merge_base*,
2477 Const_section_id_hash>
2478 Merge_sections_by_id;
2480 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2481 Merge_section_properties::hash,
2482 Merge_section_properties::equal_to>
2483 Merge_sections_by_properties;
2485 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2486 Const_section_id_hash>
2487 Relaxed_input_sections_by_id;
2489 // Whether this is valid
2490 bool is_valid_;
2491 // Merge sections by merge section properties.
2492 Merge_sections_by_properties merge_sections_by_properties_;
2493 // Merge sections by section IDs.
2494 Merge_sections_by_id merge_sections_by_id_;
2495 // Relaxed sections by section IDs.
2496 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2499 // An output section. We don't expect to have too many output
2500 // sections, so we don't bother to do a template on the size.
2502 class Output_section : public Output_data
2504 public:
2505 // Create an output section, giving the name, type, and flags.
2506 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2507 virtual ~Output_section();
2509 // Add a new input section SHNDX, named NAME, with header SHDR, from
2510 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2511 // which applies to this section, or 0 if none, or -1 if more than
2512 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2513 // in a linker script; in that case we need to keep track of input
2514 // sections associated with an output section. Return the offset
2515 // within the output section.
2516 template<int size, bool big_endian>
2517 off_t
2518 add_input_section(Layout* layout, Sized_relobj<size, big_endian>* object,
2519 unsigned int shndx, const char *name,
2520 const elfcpp::Shdr<size, big_endian>& shdr,
2521 unsigned int reloc_shndx, bool have_sections_script);
2523 // Add generated data POSD to this output section.
2524 void
2525 add_output_section_data(Output_section_data* posd);
2527 // Add a relaxed input section PORIS to this output section.
2528 void
2529 add_relaxed_input_section(Output_relaxed_input_section* poris);
2531 // Return the section name.
2532 const char*
2533 name() const
2534 { return this->name_; }
2536 // Return the section type.
2537 elfcpp::Elf_Word
2538 type() const
2539 { return this->type_; }
2541 // Return the section flags.
2542 elfcpp::Elf_Xword
2543 flags() const
2544 { return this->flags_; }
2546 // Update the output section flags based on input section flags.
2547 void
2548 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2550 // Return the entsize field.
2551 uint64_t
2552 entsize() const
2553 { return this->entsize_; }
2555 // Set the entsize field.
2556 void
2557 set_entsize(uint64_t v);
2559 // Set the load address.
2560 void
2561 set_load_address(uint64_t load_address)
2563 this->load_address_ = load_address;
2564 this->has_load_address_ = true;
2567 // Set the link field to the output section index of a section.
2568 void
2569 set_link_section(const Output_data* od)
2571 gold_assert(this->link_ == 0
2572 && !this->should_link_to_symtab_
2573 && !this->should_link_to_dynsym_);
2574 this->link_section_ = od;
2577 // Set the link field to a constant.
2578 void
2579 set_link(unsigned int v)
2581 gold_assert(this->link_section_ == NULL
2582 && !this->should_link_to_symtab_
2583 && !this->should_link_to_dynsym_);
2584 this->link_ = v;
2587 // Record that this section should link to the normal symbol table.
2588 void
2589 set_should_link_to_symtab()
2591 gold_assert(this->link_section_ == NULL
2592 && this->link_ == 0
2593 && !this->should_link_to_dynsym_);
2594 this->should_link_to_symtab_ = true;
2597 // Record that this section should link to the dynamic symbol table.
2598 void
2599 set_should_link_to_dynsym()
2601 gold_assert(this->link_section_ == NULL
2602 && this->link_ == 0
2603 && !this->should_link_to_symtab_);
2604 this->should_link_to_dynsym_ = true;
2607 // Return the info field.
2608 unsigned int
2609 info() const
2611 gold_assert(this->info_section_ == NULL
2612 && this->info_symndx_ == NULL);
2613 return this->info_;
2616 // Set the info field to the output section index of a section.
2617 void
2618 set_info_section(const Output_section* os)
2620 gold_assert((this->info_section_ == NULL
2621 || (this->info_section_ == os
2622 && this->info_uses_section_index_))
2623 && this->info_symndx_ == NULL
2624 && this->info_ == 0);
2625 this->info_section_ = os;
2626 this->info_uses_section_index_= true;
2629 // Set the info field to the symbol table index of a symbol.
2630 void
2631 set_info_symndx(const Symbol* sym)
2633 gold_assert(this->info_section_ == NULL
2634 && (this->info_symndx_ == NULL
2635 || this->info_symndx_ == sym)
2636 && this->info_ == 0);
2637 this->info_symndx_ = sym;
2640 // Set the info field to the symbol table index of a section symbol.
2641 void
2642 set_info_section_symndx(const Output_section* os)
2644 gold_assert((this->info_section_ == NULL
2645 || (this->info_section_ == os
2646 && !this->info_uses_section_index_))
2647 && this->info_symndx_ == NULL
2648 && this->info_ == 0);
2649 this->info_section_ = os;
2650 this->info_uses_section_index_ = false;
2653 // Set the info field to a constant.
2654 void
2655 set_info(unsigned int v)
2657 gold_assert(this->info_section_ == NULL
2658 && this->info_symndx_ == NULL
2659 && (this->info_ == 0
2660 || this->info_ == v));
2661 this->info_ = v;
2664 // Set the addralign field.
2665 void
2666 set_addralign(uint64_t v)
2667 { this->addralign_ = v; }
2669 // Whether the output section index has been set.
2670 bool
2671 has_out_shndx() const
2672 { return this->out_shndx_ != -1U; }
2674 // Indicate that we need a symtab index.
2675 void
2676 set_needs_symtab_index()
2677 { this->needs_symtab_index_ = true; }
2679 // Return whether we need a symtab index.
2680 bool
2681 needs_symtab_index() const
2682 { return this->needs_symtab_index_; }
2684 // Get the symtab index.
2685 unsigned int
2686 symtab_index() const
2688 gold_assert(this->symtab_index_ != 0);
2689 return this->symtab_index_;
2692 // Set the symtab index.
2693 void
2694 set_symtab_index(unsigned int index)
2696 gold_assert(index != 0);
2697 this->symtab_index_ = index;
2700 // Indicate that we need a dynsym index.
2701 void
2702 set_needs_dynsym_index()
2703 { this->needs_dynsym_index_ = true; }
2705 // Return whether we need a dynsym index.
2706 bool
2707 needs_dynsym_index() const
2708 { return this->needs_dynsym_index_; }
2710 // Get the dynsym index.
2711 unsigned int
2712 dynsym_index() const
2714 gold_assert(this->dynsym_index_ != 0);
2715 return this->dynsym_index_;
2718 // Set the dynsym index.
2719 void
2720 set_dynsym_index(unsigned int index)
2722 gold_assert(index != 0);
2723 this->dynsym_index_ = index;
2726 // Return whether the input sections sections attachd to this output
2727 // section may require sorting. This is used to handle constructor
2728 // priorities compatibly with GNU ld.
2729 bool
2730 may_sort_attached_input_sections() const
2731 { return this->may_sort_attached_input_sections_; }
2733 // Record that the input sections attached to this output section
2734 // may require sorting.
2735 void
2736 set_may_sort_attached_input_sections()
2737 { this->may_sort_attached_input_sections_ = true; }
2739 // Returns true if input sections must be sorted according to the
2740 // order in which their name appear in the --section-ordering-file.
2741 bool
2742 input_section_order_specified()
2743 { return this->input_section_order_specified_; }
2745 // Record that input sections must be sorted as some of their names
2746 // match the patterns specified through --section-ordering-file.
2747 void
2748 set_input_section_order_specified()
2749 { this->input_section_order_specified_ = true; }
2751 // Return whether the input sections attached to this output section
2752 // require sorting. This is used to handle constructor priorities
2753 // compatibly with GNU ld.
2754 bool
2755 must_sort_attached_input_sections() const
2756 { return this->must_sort_attached_input_sections_; }
2758 // Record that the input sections attached to this output section
2759 // require sorting.
2760 void
2761 set_must_sort_attached_input_sections()
2762 { this->must_sort_attached_input_sections_ = true; }
2764 // Get the order in which this section appears in the PT_LOAD output
2765 // segment.
2766 Output_section_order
2767 order() const
2768 { return this->order_; }
2770 // Set the order for this section.
2771 void
2772 set_order(Output_section_order order)
2773 { this->order_ = order; }
2775 // Return whether this section holds relro data--data which has
2776 // dynamic relocations but which may be marked read-only after the
2777 // dynamic relocations have been completed.
2778 bool
2779 is_relro() const
2780 { return this->is_relro_; }
2782 // Record that this section holds relro data.
2783 void
2784 set_is_relro()
2785 { this->is_relro_ = true; }
2787 // Record that this section does not hold relro data.
2788 void
2789 clear_is_relro()
2790 { this->is_relro_ = false; }
2792 // True if this is a small section: a section which holds small
2793 // variables.
2794 bool
2795 is_small_section() const
2796 { return this->is_small_section_; }
2798 // Record that this is a small section.
2799 void
2800 set_is_small_section()
2801 { this->is_small_section_ = true; }
2803 // True if this is a large section: a section which holds large
2804 // variables.
2805 bool
2806 is_large_section() const
2807 { return this->is_large_section_; }
2809 // Record that this is a large section.
2810 void
2811 set_is_large_section()
2812 { this->is_large_section_ = true; }
2814 // True if this is a large data (not BSS) section.
2815 bool
2816 is_large_data_section()
2817 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2819 // Return whether this section should be written after all the input
2820 // sections are complete.
2821 bool
2822 after_input_sections() const
2823 { return this->after_input_sections_; }
2825 // Record that this section should be written after all the input
2826 // sections are complete.
2827 void
2828 set_after_input_sections()
2829 { this->after_input_sections_ = true; }
2831 // Return whether this section requires postprocessing after all
2832 // relocations have been applied.
2833 bool
2834 requires_postprocessing() const
2835 { return this->requires_postprocessing_; }
2837 // If a section requires postprocessing, return the buffer to use.
2838 unsigned char*
2839 postprocessing_buffer() const
2841 gold_assert(this->postprocessing_buffer_ != NULL);
2842 return this->postprocessing_buffer_;
2845 // If a section requires postprocessing, create the buffer to use.
2846 void
2847 create_postprocessing_buffer();
2849 // If a section requires postprocessing, this is the size of the
2850 // buffer to which relocations should be applied.
2851 off_t
2852 postprocessing_buffer_size() const
2853 { return this->current_data_size_for_child(); }
2855 // Modify the section name. This is only permitted for an
2856 // unallocated section, and only before the size has been finalized.
2857 // Otherwise the name will not get into Layout::namepool_.
2858 void
2859 set_name(const char* newname)
2861 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2862 gold_assert(!this->is_data_size_valid());
2863 this->name_ = newname;
2866 // Return whether the offset OFFSET in the input section SHNDX in
2867 // object OBJECT is being included in the link.
2868 bool
2869 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2870 off_t offset) const;
2872 // Return the offset within the output section of OFFSET relative to
2873 // the start of input section SHNDX in object OBJECT.
2874 section_offset_type
2875 output_offset(const Relobj* object, unsigned int shndx,
2876 section_offset_type offset) const;
2878 // Return the output virtual address of OFFSET relative to the start
2879 // of input section SHNDX in object OBJECT.
2880 uint64_t
2881 output_address(const Relobj* object, unsigned int shndx,
2882 off_t offset) const;
2884 // Look for the merged section for input section SHNDX in object
2885 // OBJECT. If found, return true, and set *ADDR to the address of
2886 // the start of the merged section. This is not necessary the
2887 // output offset corresponding to input offset 0 in the section,
2888 // since the section may be mapped arbitrarily.
2889 bool
2890 find_starting_output_address(const Relobj* object, unsigned int shndx,
2891 uint64_t* addr) const;
2893 // Record that this output section was found in the SECTIONS clause
2894 // of a linker script.
2895 void
2896 set_found_in_sections_clause()
2897 { this->found_in_sections_clause_ = true; }
2899 // Return whether this output section was found in the SECTIONS
2900 // clause of a linker script.
2901 bool
2902 found_in_sections_clause() const
2903 { return this->found_in_sections_clause_; }
2905 // Write the section header into *OPHDR.
2906 template<int size, bool big_endian>
2907 void
2908 write_header(const Layout*, const Stringpool*,
2909 elfcpp::Shdr_write<size, big_endian>*) const;
2911 // The next few calls are for linker script support.
2913 // In some cases we need to keep a list of the input sections
2914 // associated with this output section. We only need the list if we
2915 // might have to change the offsets of the input section within the
2916 // output section after we add the input section. The ordinary
2917 // input sections will be written out when we process the object
2918 // file, and as such we don't need to track them here. We do need
2919 // to track Output_section_data objects here. We store instances of
2920 // this structure in a std::vector, so it must be a POD. There can
2921 // be many instances of this structure, so we use a union to save
2922 // some space.
2923 class Input_section
2925 public:
2926 Input_section()
2927 : shndx_(0), p2align_(0)
2929 this->u1_.data_size = 0;
2930 this->u2_.object = NULL;
2933 // For an ordinary input section.
2934 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2935 uint64_t addralign)
2936 : shndx_(shndx),
2937 p2align_(ffsll(static_cast<long long>(addralign))),
2938 section_order_index_(0)
2940 gold_assert(shndx != OUTPUT_SECTION_CODE
2941 && shndx != MERGE_DATA_SECTION_CODE
2942 && shndx != MERGE_STRING_SECTION_CODE
2943 && shndx != RELAXED_INPUT_SECTION_CODE);
2944 this->u1_.data_size = data_size;
2945 this->u2_.object = object;
2948 // For a non-merge output section.
2949 Input_section(Output_section_data* posd)
2950 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
2951 section_order_index_(0)
2953 this->u1_.data_size = 0;
2954 this->u2_.posd = posd;
2957 // For a merge section.
2958 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2959 : shndx_(is_string
2960 ? MERGE_STRING_SECTION_CODE
2961 : MERGE_DATA_SECTION_CODE),
2962 p2align_(0),
2963 section_order_index_(0)
2965 this->u1_.entsize = entsize;
2966 this->u2_.posd = posd;
2969 // For a relaxed input section.
2970 Input_section(Output_relaxed_input_section *psection)
2971 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
2972 section_order_index_(0)
2974 this->u1_.data_size = 0;
2975 this->u2_.poris = psection;
2978 unsigned int
2979 section_order_index() const
2981 return this->section_order_index_;
2984 void
2985 set_section_order_index(unsigned int number)
2987 this->section_order_index_ = number;
2990 // The required alignment.
2991 uint64_t
2992 addralign() const
2994 if (this->p2align_ != 0)
2995 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
2996 else if (!this->is_input_section())
2997 return this->u2_.posd->addralign();
2998 else
2999 return 0;
3002 // Set the required alignment, which must be either 0 or a power of 2.
3003 // For input sections that are sub-classes of Output_section_data, a
3004 // alignment of zero means asking the underlying object for alignment.
3005 void
3006 set_addralign(uint64_t addralign)
3008 if (addralign == 0)
3009 this->p2align_ = 0;
3010 else
3012 gold_assert((addralign & (addralign - 1)) == 0);
3013 this->p2align_ = ffsll(static_cast<long long>(addralign));
3017 // Return the required size.
3018 off_t
3019 data_size() const;
3021 // Whether this is an input section.
3022 bool
3023 is_input_section() const
3025 return (this->shndx_ != OUTPUT_SECTION_CODE
3026 && this->shndx_ != MERGE_DATA_SECTION_CODE
3027 && this->shndx_ != MERGE_STRING_SECTION_CODE
3028 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3031 // Return whether this is a merge section which matches the
3032 // parameters.
3033 bool
3034 is_merge_section(bool is_string, uint64_t entsize,
3035 uint64_t addralign) const
3037 return (this->shndx_ == (is_string
3038 ? MERGE_STRING_SECTION_CODE
3039 : MERGE_DATA_SECTION_CODE)
3040 && this->u1_.entsize == entsize
3041 && this->addralign() == addralign);
3044 // Return whether this is a merge section for some input section.
3045 bool
3046 is_merge_section() const
3048 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3049 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3052 // Return whether this is a relaxed input section.
3053 bool
3054 is_relaxed_input_section() const
3055 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3057 // Return whether this is a generic Output_section_data.
3058 bool
3059 is_output_section_data() const
3061 return this->shndx_ == OUTPUT_SECTION_CODE;
3064 // Return the object for an input section.
3065 Relobj*
3066 relobj() const;
3068 // Return the input section index for an input section.
3069 unsigned int
3070 shndx() const;
3072 // For non-input-sections, return the associated Output_section_data
3073 // object.
3074 Output_section_data*
3075 output_section_data() const
3077 gold_assert(!this->is_input_section());
3078 return this->u2_.posd;
3081 // For a merge section, return the Output_merge_base pointer.
3082 Output_merge_base*
3083 output_merge_base() const
3085 gold_assert(this->is_merge_section());
3086 return this->u2_.pomb;
3089 // Return the Output_relaxed_input_section object.
3090 Output_relaxed_input_section*
3091 relaxed_input_section() const
3093 gold_assert(this->is_relaxed_input_section());
3094 return this->u2_.poris;
3097 // Set the output section.
3098 void
3099 set_output_section(Output_section* os)
3101 gold_assert(!this->is_input_section());
3102 Output_section_data *posd =
3103 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3104 posd->set_output_section(os);
3107 // Set the address and file offset. This is called during
3108 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3109 // the enclosing section.
3110 void
3111 set_address_and_file_offset(uint64_t address, off_t file_offset,
3112 off_t section_file_offset);
3114 // Reset the address and file offset.
3115 void
3116 reset_address_and_file_offset();
3118 // Finalize the data size.
3119 void
3120 finalize_data_size();
3122 // Add an input section, for SHF_MERGE sections.
3123 bool
3124 add_input_section(Relobj* object, unsigned int shndx)
3126 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3127 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3128 return this->u2_.posd->add_input_section(object, shndx);
3131 // Given an input OBJECT, an input section index SHNDX within that
3132 // object, and an OFFSET relative to the start of that input
3133 // section, return whether or not the output offset is known. If
3134 // this function returns true, it sets *POUTPUT to the offset in
3135 // the output section, relative to the start of the input section
3136 // in the output section. *POUTPUT may be different from OFFSET
3137 // for a merged section.
3138 bool
3139 output_offset(const Relobj* object, unsigned int shndx,
3140 section_offset_type offset,
3141 section_offset_type *poutput) const;
3143 // Return whether this is the merge section for the input section
3144 // SHNDX in OBJECT.
3145 bool
3146 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3148 // Write out the data. This does nothing for an input section.
3149 void
3150 write(Output_file*);
3152 // Write the data to a buffer. This does nothing for an input
3153 // section.
3154 void
3155 write_to_buffer(unsigned char*);
3157 // Print to a map file.
3158 void
3159 print_to_mapfile(Mapfile*) const;
3161 // Print statistics about merge sections to stderr.
3162 void
3163 print_merge_stats(const char* section_name)
3165 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3166 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3167 this->u2_.posd->print_merge_stats(section_name);
3170 private:
3171 // Code values which appear in shndx_. If the value is not one of
3172 // these codes, it is the input section index in the object file.
3173 enum
3175 // An Output_section_data.
3176 OUTPUT_SECTION_CODE = -1U,
3177 // An Output_section_data for an SHF_MERGE section with
3178 // SHF_STRINGS not set.
3179 MERGE_DATA_SECTION_CODE = -2U,
3180 // An Output_section_data for an SHF_MERGE section with
3181 // SHF_STRINGS set.
3182 MERGE_STRING_SECTION_CODE = -3U,
3183 // An Output_section_data for a relaxed input section.
3184 RELAXED_INPUT_SECTION_CODE = -4U
3187 // For an ordinary input section, this is the section index in the
3188 // input file. For an Output_section_data, this is
3189 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3190 // MERGE_STRING_SECTION_CODE.
3191 unsigned int shndx_;
3192 // The required alignment, stored as a power of 2.
3193 unsigned int p2align_;
3194 union
3196 // For an ordinary input section, the section size.
3197 off_t data_size;
3198 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3199 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3200 // entity size.
3201 uint64_t entsize;
3202 } u1_;
3203 union
3205 // For an ordinary input section, the object which holds the
3206 // input section.
3207 Relobj* object;
3208 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3209 // MERGE_STRING_SECTION_CODE, the data.
3210 Output_section_data* posd;
3211 Output_merge_base* pomb;
3212 // For RELAXED_INPUT_SECTION_CODE, the data.
3213 Output_relaxed_input_section* poris;
3214 } u2_;
3215 // The line number of the pattern it matches in the --section-ordering-file
3216 // file. It is 0 if does not match any pattern.
3217 unsigned int section_order_index_;
3220 // Store the list of input sections for this Output_section into the
3221 // list passed in. This removes the input sections, leaving only
3222 // any Output_section_data elements. This returns the size of those
3223 // Output_section_data elements. ADDRESS is the address of this
3224 // output section. FILL is the fill value to use, in case there are
3225 // any spaces between the remaining Output_section_data elements.
3226 uint64_t
3227 get_input_sections(uint64_t address, const std::string& fill,
3228 std::list<Input_section>*);
3230 // Add a script input section. A script input section can either be
3231 // a plain input section or a sub-class of Output_section_data.
3232 void
3233 add_script_input_section(const Input_section& input_section);
3235 // Set the current size of the output section.
3236 void
3237 set_current_data_size(off_t size)
3238 { this->set_current_data_size_for_child(size); }
3240 // Get the current size of the output section.
3241 off_t
3242 current_data_size() const
3243 { return this->current_data_size_for_child(); }
3245 // End of linker script support.
3247 // Save states before doing section layout.
3248 // This is used for relaxation.
3249 void
3250 save_states();
3252 // Restore states prior to section layout.
3253 void
3254 restore_states();
3256 // Discard states.
3257 void
3258 discard_states();
3260 // Convert existing input sections to relaxed input sections.
3261 void
3262 convert_input_sections_to_relaxed_sections(
3263 const std::vector<Output_relaxed_input_section*>& sections);
3265 // Find a relaxed input section to an input section in OBJECT
3266 // with index SHNDX. Return NULL if none is found.
3267 const Output_relaxed_input_section*
3268 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3270 // Whether section offsets need adjustment due to relaxation.
3271 bool
3272 section_offsets_need_adjustment() const
3273 { return this->section_offsets_need_adjustment_; }
3275 // Set section_offsets_need_adjustment to be true.
3276 void
3277 set_section_offsets_need_adjustment()
3278 { this->section_offsets_need_adjustment_ = true; }
3280 // Adjust section offsets of input sections in this. This is
3281 // requires if relaxation caused some input sections to change sizes.
3282 void
3283 adjust_section_offsets();
3285 // Whether this is a NOLOAD section.
3286 bool
3287 is_noload() const
3288 { return this->is_noload_; }
3290 // Set NOLOAD flag.
3291 void
3292 set_is_noload()
3293 { this->is_noload_ = true; }
3295 // Print merge statistics to stderr.
3296 void
3297 print_merge_stats();
3299 protected:
3300 // Return the output section--i.e., the object itself.
3301 Output_section*
3302 do_output_section()
3303 { return this; }
3305 const Output_section*
3306 do_output_section() const
3307 { return this; }
3309 // Return the section index in the output file.
3310 unsigned int
3311 do_out_shndx() const
3313 gold_assert(this->out_shndx_ != -1U);
3314 return this->out_shndx_;
3317 // Set the output section index.
3318 void
3319 do_set_out_shndx(unsigned int shndx)
3321 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3322 this->out_shndx_ = shndx;
3325 // Set the final data size of the Output_section. For a typical
3326 // Output_section, there is nothing to do, but if there are any
3327 // Output_section_data objects we need to set their final addresses
3328 // here.
3329 virtual void
3330 set_final_data_size();
3332 // Reset the address and file offset.
3333 void
3334 do_reset_address_and_file_offset();
3336 // Return true if address and file offset already have reset values. In
3337 // other words, calling reset_address_and_file_offset will not change them.
3338 bool
3339 do_address_and_file_offset_have_reset_values() const;
3341 // Write the data to the file. For a typical Output_section, this
3342 // does nothing: the data is written out by calling Object::Relocate
3343 // on each input object. But if there are any Output_section_data
3344 // objects we do need to write them out here.
3345 virtual void
3346 do_write(Output_file*);
3348 // Return the address alignment--function required by parent class.
3349 uint64_t
3350 do_addralign() const
3351 { return this->addralign_; }
3353 // Return whether there is a load address.
3354 bool
3355 do_has_load_address() const
3356 { return this->has_load_address_; }
3358 // Return the load address.
3359 uint64_t
3360 do_load_address() const
3362 gold_assert(this->has_load_address_);
3363 return this->load_address_;
3366 // Return whether this is an Output_section.
3367 bool
3368 do_is_section() const
3369 { return true; }
3371 // Return whether this is a section of the specified type.
3372 bool
3373 do_is_section_type(elfcpp::Elf_Word type) const
3374 { return this->type_ == type; }
3376 // Return whether the specified section flag is set.
3377 bool
3378 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3379 { return (this->flags_ & flag) != 0; }
3381 // Set the TLS offset. Called only for SHT_TLS sections.
3382 void
3383 do_set_tls_offset(uint64_t tls_base);
3385 // Return the TLS offset, relative to the base of the TLS segment.
3386 // Valid only for SHT_TLS sections.
3387 uint64_t
3388 do_tls_offset() const
3389 { return this->tls_offset_; }
3391 // This may be implemented by a child class.
3392 virtual void
3393 do_finalize_name(Layout*)
3396 // Print to the map file.
3397 virtual void
3398 do_print_to_mapfile(Mapfile*) const;
3400 // Record that this section requires postprocessing after all
3401 // relocations have been applied. This is called by a child class.
3402 void
3403 set_requires_postprocessing()
3405 this->requires_postprocessing_ = true;
3406 this->after_input_sections_ = true;
3409 // Write all the data of an Output_section into the postprocessing
3410 // buffer.
3411 void
3412 write_to_postprocessing_buffer();
3414 typedef std::vector<Input_section> Input_section_list;
3416 // Allow a child class to access the input sections.
3417 const Input_section_list&
3418 input_sections() const
3419 { return this->input_sections_; }
3421 // Whether this always keeps an input section list
3422 bool
3423 always_keeps_input_sections() const
3424 { return this->always_keeps_input_sections_; }
3426 // Always keep an input section list.
3427 void
3428 set_always_keeps_input_sections()
3430 gold_assert(this->current_data_size_for_child() == 0);
3431 this->always_keeps_input_sections_ = true;
3434 private:
3435 // We only save enough information to undo the effects of section layout.
3436 class Checkpoint_output_section
3438 public:
3439 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3440 const Input_section_list& input_sections,
3441 off_t first_input_offset,
3442 bool attached_input_sections_are_sorted)
3443 : addralign_(addralign), flags_(flags),
3444 input_sections_(input_sections),
3445 input_sections_size_(input_sections_.size()),
3446 input_sections_copy_(), first_input_offset_(first_input_offset),
3447 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3450 virtual
3451 ~Checkpoint_output_section()
3454 // Return the address alignment.
3455 uint64_t
3456 addralign() const
3457 { return this->addralign_; }
3459 // Return the section flags.
3460 elfcpp::Elf_Xword
3461 flags() const
3462 { return this->flags_; }
3464 // Return a reference to the input section list copy.
3465 Input_section_list*
3466 input_sections()
3467 { return &this->input_sections_copy_; }
3469 // Return the size of input_sections at the time when checkpoint is
3470 // taken.
3471 size_t
3472 input_sections_size() const
3473 { return this->input_sections_size_; }
3475 // Whether input sections are copied.
3476 bool
3477 input_sections_saved() const
3478 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3480 off_t
3481 first_input_offset() const
3482 { return this->first_input_offset_; }
3484 bool
3485 attached_input_sections_are_sorted() const
3486 { return this->attached_input_sections_are_sorted_; }
3488 // Save input sections.
3489 void
3490 save_input_sections()
3492 this->input_sections_copy_.reserve(this->input_sections_size_);
3493 this->input_sections_copy_.clear();
3494 Input_section_list::const_iterator p = this->input_sections_.begin();
3495 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3496 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3497 this->input_sections_copy_.push_back(*p);
3500 private:
3501 // The section alignment.
3502 uint64_t addralign_;
3503 // The section flags.
3504 elfcpp::Elf_Xword flags_;
3505 // Reference to the input sections to be checkpointed.
3506 const Input_section_list& input_sections_;
3507 // Size of the checkpointed portion of input_sections_;
3508 size_t input_sections_size_;
3509 // Copy of input sections.
3510 Input_section_list input_sections_copy_;
3511 // The offset of the first entry in input_sections_.
3512 off_t first_input_offset_;
3513 // True if the input sections attached to this output section have
3514 // already been sorted.
3515 bool attached_input_sections_are_sorted_;
3518 // This class is used to sort the input sections.
3519 class Input_section_sort_entry;
3521 // This is the sort comparison function for ctors and dtors.
3522 struct Input_section_sort_compare
3524 bool
3525 operator()(const Input_section_sort_entry&,
3526 const Input_section_sort_entry&) const;
3529 // This is the sort comparison function for .init_array and .fini_array.
3530 struct Input_section_sort_init_fini_compare
3532 bool
3533 operator()(const Input_section_sort_entry&,
3534 const Input_section_sort_entry&) const;
3537 // This is the sort comparison function when a section order is specified
3538 // from an input file.
3539 struct Input_section_sort_section_order_index_compare
3541 bool
3542 operator()(const Input_section_sort_entry&,
3543 const Input_section_sort_entry&) const;
3546 // Fill data. This is used to fill in data between input sections.
3547 // It is also used for data statements (BYTE, WORD, etc.) in linker
3548 // scripts. When we have to keep track of the input sections, we
3549 // can use an Output_data_const, but we don't want to have to keep
3550 // track of input sections just to implement fills.
3551 class Fill
3553 public:
3554 Fill(off_t section_offset, off_t length)
3555 : section_offset_(section_offset),
3556 length_(convert_to_section_size_type(length))
3559 // Return section offset.
3560 off_t
3561 section_offset() const
3562 { return this->section_offset_; }
3564 // Return fill length.
3565 section_size_type
3566 length() const
3567 { return this->length_; }
3569 private:
3570 // The offset within the output section.
3571 off_t section_offset_;
3572 // The length of the space to fill.
3573 section_size_type length_;
3576 typedef std::vector<Fill> Fill_list;
3578 // Map used during relaxation of existing sections. This map
3579 // a section id an input section list index. We assume that
3580 // Input_section_list is a vector.
3581 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
3583 // Add a new output section by Input_section.
3584 void
3585 add_output_section_data(Input_section*);
3587 // Add an SHF_MERGE input section. Returns true if the section was
3588 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3589 // stores information about the merged input sections.
3590 bool
3591 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3592 uint64_t entsize, uint64_t addralign,
3593 bool keeps_input_sections);
3595 // Add an output SHF_MERGE section POSD to this output section.
3596 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3597 // ENTSIZE is the entity size. This returns the entry added to
3598 // input_sections_.
3599 void
3600 add_output_merge_section(Output_section_data* posd, bool is_string,
3601 uint64_t entsize);
3603 // Sort the attached input sections.
3604 void
3605 sort_attached_input_sections();
3607 // Find the merge section into which an input section with index SHNDX in
3608 // OBJECT has been added. Return NULL if none found.
3609 Output_section_data*
3610 find_merge_section(const Relobj* object, unsigned int shndx) const;
3612 // Build a relaxation map.
3613 void
3614 build_relaxation_map(
3615 const Input_section_list& input_sections,
3616 size_t limit,
3617 Relaxation_map* map) const;
3619 // Convert input sections in an input section list into relaxed sections.
3620 void
3621 convert_input_sections_in_list_to_relaxed_sections(
3622 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3623 const Relaxation_map& map,
3624 Input_section_list* input_sections);
3626 // Build the lookup maps for merge and relaxed input sections.
3627 void
3628 build_lookup_maps() const;
3630 // Most of these fields are only valid after layout.
3632 // The name of the section. This will point into a Stringpool.
3633 const char* name_;
3634 // The section address is in the parent class.
3635 // The section alignment.
3636 uint64_t addralign_;
3637 // The section entry size.
3638 uint64_t entsize_;
3639 // The load address. This is only used when using a linker script
3640 // with a SECTIONS clause. The has_load_address_ field indicates
3641 // whether this field is valid.
3642 uint64_t load_address_;
3643 // The file offset is in the parent class.
3644 // Set the section link field to the index of this section.
3645 const Output_data* link_section_;
3646 // If link_section_ is NULL, this is the link field.
3647 unsigned int link_;
3648 // Set the section info field to the index of this section.
3649 const Output_section* info_section_;
3650 // If info_section_ is NULL, set the info field to the symbol table
3651 // index of this symbol.
3652 const Symbol* info_symndx_;
3653 // If info_section_ and info_symndx_ are NULL, this is the section
3654 // info field.
3655 unsigned int info_;
3656 // The section type.
3657 const elfcpp::Elf_Word type_;
3658 // The section flags.
3659 elfcpp::Elf_Xword flags_;
3660 // The order of this section in the output segment.
3661 Output_section_order order_;
3662 // The section index.
3663 unsigned int out_shndx_;
3664 // If there is a STT_SECTION for this output section in the normal
3665 // symbol table, this is the symbol index. This starts out as zero.
3666 // It is initialized in Layout::finalize() to be the index, or -1U
3667 // if there isn't one.
3668 unsigned int symtab_index_;
3669 // If there is a STT_SECTION for this output section in the dynamic
3670 // symbol table, this is the symbol index. This starts out as zero.
3671 // It is initialized in Layout::finalize() to be the index, or -1U
3672 // if there isn't one.
3673 unsigned int dynsym_index_;
3674 // The input sections. This will be empty in cases where we don't
3675 // need to keep track of them.
3676 Input_section_list input_sections_;
3677 // The offset of the first entry in input_sections_.
3678 off_t first_input_offset_;
3679 // The fill data. This is separate from input_sections_ because we
3680 // often will need fill sections without needing to keep track of
3681 // input sections.
3682 Fill_list fills_;
3683 // If the section requires postprocessing, this buffer holds the
3684 // section contents during relocation.
3685 unsigned char* postprocessing_buffer_;
3686 // Whether this output section needs a STT_SECTION symbol in the
3687 // normal symbol table. This will be true if there is a relocation
3688 // which needs it.
3689 bool needs_symtab_index_ : 1;
3690 // Whether this output section needs a STT_SECTION symbol in the
3691 // dynamic symbol table. This will be true if there is a dynamic
3692 // relocation which needs it.
3693 bool needs_dynsym_index_ : 1;
3694 // Whether the link field of this output section should point to the
3695 // normal symbol table.
3696 bool should_link_to_symtab_ : 1;
3697 // Whether the link field of this output section should point to the
3698 // dynamic symbol table.
3699 bool should_link_to_dynsym_ : 1;
3700 // Whether this section should be written after all the input
3701 // sections are complete.
3702 bool after_input_sections_ : 1;
3703 // Whether this section requires post processing after all
3704 // relocations have been applied.
3705 bool requires_postprocessing_ : 1;
3706 // Whether an input section was mapped to this output section
3707 // because of a SECTIONS clause in a linker script.
3708 bool found_in_sections_clause_ : 1;
3709 // Whether this section has an explicitly specified load address.
3710 bool has_load_address_ : 1;
3711 // True if the info_section_ field means the section index of the
3712 // section, false if it means the symbol index of the corresponding
3713 // section symbol.
3714 bool info_uses_section_index_ : 1;
3715 // True if input sections attached to this output section have to be
3716 // sorted according to a specified order.
3717 bool input_section_order_specified_ : 1;
3718 // True if the input sections attached to this output section may
3719 // need sorting.
3720 bool may_sort_attached_input_sections_ : 1;
3721 // True if the input sections attached to this output section must
3722 // be sorted.
3723 bool must_sort_attached_input_sections_ : 1;
3724 // True if the input sections attached to this output section have
3725 // already been sorted.
3726 bool attached_input_sections_are_sorted_ : 1;
3727 // True if this section holds relro data.
3728 bool is_relro_ : 1;
3729 // True if this is a small section.
3730 bool is_small_section_ : 1;
3731 // True if this is a large section.
3732 bool is_large_section_ : 1;
3733 // Whether code-fills are generated at write.
3734 bool generate_code_fills_at_write_ : 1;
3735 // Whether the entry size field should be zero.
3736 bool is_entsize_zero_ : 1;
3737 // Whether section offsets need adjustment due to relaxation.
3738 bool section_offsets_need_adjustment_ : 1;
3739 // Whether this is a NOLOAD section.
3740 bool is_noload_ : 1;
3741 // Whether this always keeps input section.
3742 bool always_keeps_input_sections_ : 1;
3743 // For SHT_TLS sections, the offset of this section relative to the base
3744 // of the TLS segment.
3745 uint64_t tls_offset_;
3746 // Saved checkpoint.
3747 Checkpoint_output_section* checkpoint_;
3748 // Fast lookup maps for merged and relaxed input sections.
3749 Output_section_lookup_maps* lookup_maps_;
3752 // An output segment. PT_LOAD segments are built from collections of
3753 // output sections. Other segments typically point within PT_LOAD
3754 // segments, and are built directly as needed.
3756 // NOTE: We want to use the copy constructor for this class. During
3757 // relaxation, we may try built the segments multiple times. We do
3758 // that by copying the original segment list before lay-out, doing
3759 // a trial lay-out and roll-back to the saved copied if we need to
3760 // to the lay-out again.
3762 class Output_segment
3764 public:
3765 // Create an output segment, specifying the type and flags.
3766 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3768 // Return the virtual address.
3769 uint64_t
3770 vaddr() const
3771 { return this->vaddr_; }
3773 // Return the physical address.
3774 uint64_t
3775 paddr() const
3776 { return this->paddr_; }
3778 // Return the segment type.
3779 elfcpp::Elf_Word
3780 type() const
3781 { return this->type_; }
3783 // Return the segment flags.
3784 elfcpp::Elf_Word
3785 flags() const
3786 { return this->flags_; }
3788 // Return the memory size.
3789 uint64_t
3790 memsz() const
3791 { return this->memsz_; }
3793 // Return the file size.
3794 off_t
3795 filesz() const
3796 { return this->filesz_; }
3798 // Return the file offset.
3799 off_t
3800 offset() const
3801 { return this->offset_; }
3803 // Whether this is a segment created to hold large data sections.
3804 bool
3805 is_large_data_segment() const
3806 { return this->is_large_data_segment_; }
3808 // Record that this is a segment created to hold large data
3809 // sections.
3810 void
3811 set_is_large_data_segment()
3812 { this->is_large_data_segment_ = true; }
3814 // Return the maximum alignment of the Output_data.
3815 uint64_t
3816 maximum_alignment();
3818 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
3819 // the segment flags to use.
3820 void
3821 add_output_section_to_load(Layout* layout, Output_section* os,
3822 elfcpp::Elf_Word seg_flags);
3824 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
3825 // is the segment flags to use.
3826 void
3827 add_output_section_to_nonload(Output_section* os,
3828 elfcpp::Elf_Word seg_flags);
3830 // Remove an Output_section from this segment. It is an error if it
3831 // is not present.
3832 void
3833 remove_output_section(Output_section* os);
3835 // Add an Output_data (which need not be an Output_section) to the
3836 // start of this segment.
3837 void
3838 add_initial_output_data(Output_data*);
3840 // Return true if this segment has any sections which hold actual
3841 // data, rather than being a BSS section.
3842 bool
3843 has_any_data_sections() const;
3845 // Whether this segment has a dynamic relocs.
3846 bool
3847 has_dynamic_reloc() const;
3849 // Return the address of the first section.
3850 uint64_t
3851 first_section_load_address() const;
3853 // Return whether the addresses have been set already.
3854 bool
3855 are_addresses_set() const
3856 { return this->are_addresses_set_; }
3858 // Set the addresses.
3859 void
3860 set_addresses(uint64_t vaddr, uint64_t paddr)
3862 this->vaddr_ = vaddr;
3863 this->paddr_ = paddr;
3864 this->are_addresses_set_ = true;
3867 // Update the flags for the flags of an output section added to this
3868 // segment.
3869 void
3870 update_flags_for_output_section(elfcpp::Elf_Xword flags)
3872 // The ELF ABI specifies that a PT_TLS segment should always have
3873 // PF_R as the flags.
3874 if (this->type() != elfcpp::PT_TLS)
3875 this->flags_ |= flags;
3878 // Set the segment flags. This is only used if we have a PHDRS
3879 // clause which explicitly specifies the flags.
3880 void
3881 set_flags(elfcpp::Elf_Word flags)
3882 { this->flags_ = flags; }
3884 // Set the address of the segment to ADDR and the offset to *POFF
3885 // and set the addresses and offsets of all contained output
3886 // sections accordingly. Set the section indexes of all contained
3887 // output sections starting with *PSHNDX. If RESET is true, first
3888 // reset the addresses of the contained sections. Return the
3889 // address of the immediately following segment. Update *POFF and
3890 // *PSHNDX. This should only be called for a PT_LOAD segment.
3891 uint64_t
3892 set_section_addresses(const Layout*, bool reset, uint64_t addr,
3893 unsigned int increase_relro, off_t* poff,
3894 unsigned int* pshndx);
3896 // Set the minimum alignment of this segment. This may be adjusted
3897 // upward based on the section alignments.
3898 void
3899 set_minimum_p_align(uint64_t align)
3901 if (align > this->min_p_align_)
3902 this->min_p_align_ = align;
3905 // Set the offset of this segment based on the section. This should
3906 // only be called for a non-PT_LOAD segment.
3907 void
3908 set_offset(unsigned int increase);
3910 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3911 void
3912 set_tls_offsets();
3914 // Return the number of output sections.
3915 unsigned int
3916 output_section_count() const;
3918 // Return the section attached to the list segment with the lowest
3919 // load address. This is used when handling a PHDRS clause in a
3920 // linker script.
3921 Output_section*
3922 section_with_lowest_load_address() const;
3924 // Write the segment header into *OPHDR.
3925 template<int size, bool big_endian>
3926 void
3927 write_header(elfcpp::Phdr_write<size, big_endian>*);
3929 // Write the section headers of associated sections into V.
3930 template<int size, bool big_endian>
3931 unsigned char*
3932 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3933 unsigned int* pshndx) const;
3935 // Print the output sections in the map file.
3936 void
3937 print_sections_to_mapfile(Mapfile*) const;
3939 private:
3940 typedef std::vector<Output_data*> Output_data_list;
3942 // Find the maximum alignment in an Output_data_list.
3943 static uint64_t
3944 maximum_alignment_list(const Output_data_list*);
3946 // Return whether the first data section is a relro section.
3947 bool
3948 is_first_section_relro() const;
3950 // Set the section addresses in an Output_data_list.
3951 uint64_t
3952 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3953 uint64_t addr, off_t* poff, unsigned int* pshndx,
3954 bool* in_tls);
3956 // Return the number of Output_sections in an Output_data_list.
3957 unsigned int
3958 output_section_count_list(const Output_data_list*) const;
3960 // Return whether an Output_data_list has a dynamic reloc.
3961 bool
3962 has_dynamic_reloc_list(const Output_data_list*) const;
3964 // Find the section with the lowest load address in an
3965 // Output_data_list.
3966 void
3967 lowest_load_address_in_list(const Output_data_list* pdl,
3968 Output_section** found,
3969 uint64_t* found_lma) const;
3971 // Find the first and last entries by address.
3972 void
3973 find_first_and_last_list(const Output_data_list* pdl,
3974 const Output_data** pfirst,
3975 const Output_data** plast) const;
3977 // Write the section headers in the list into V.
3978 template<int size, bool big_endian>
3979 unsigned char*
3980 write_section_headers_list(const Layout*, const Stringpool*,
3981 const Output_data_list*, unsigned char* v,
3982 unsigned int* pshdx) const;
3984 // Print a section list to the mapfile.
3985 void
3986 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3988 // NOTE: We want to use the copy constructor. Currently, shallow copy
3989 // works for us so we do not need to write our own copy constructor.
3991 // The list of output data attached to this segment.
3992 Output_data_list output_lists_[ORDER_MAX];
3993 // The segment virtual address.
3994 uint64_t vaddr_;
3995 // The segment physical address.
3996 uint64_t paddr_;
3997 // The size of the segment in memory.
3998 uint64_t memsz_;
3999 // The maximum section alignment. The is_max_align_known_ field
4000 // indicates whether this has been finalized.
4001 uint64_t max_align_;
4002 // The required minimum value for the p_align field. This is used
4003 // for PT_LOAD segments. Note that this does not mean that
4004 // addresses should be aligned to this value; it means the p_paddr
4005 // and p_vaddr fields must be congruent modulo this value. For
4006 // non-PT_LOAD segments, the dynamic linker works more efficiently
4007 // if the p_align field has the more conventional value, although it
4008 // can align as needed.
4009 uint64_t min_p_align_;
4010 // The offset of the segment data within the file.
4011 off_t offset_;
4012 // The size of the segment data in the file.
4013 off_t filesz_;
4014 // The segment type;
4015 elfcpp::Elf_Word type_;
4016 // The segment flags.
4017 elfcpp::Elf_Word flags_;
4018 // Whether we have finalized max_align_.
4019 bool is_max_align_known_ : 1;
4020 // Whether vaddr and paddr were set by a linker script.
4021 bool are_addresses_set_ : 1;
4022 // Whether this segment holds large data sections.
4023 bool is_large_data_segment_ : 1;
4026 // This class represents the output file.
4028 class Output_file
4030 public:
4031 Output_file(const char* name);
4033 // Indicate that this is a temporary file which should not be
4034 // output.
4035 void
4036 set_is_temporary()
4037 { this->is_temporary_ = true; }
4039 // Try to open an existing file. Returns false if the file doesn't
4040 // exist, has a size of 0 or can't be mmaped. This method is
4041 // thread-unsafe.
4042 bool
4043 open_for_modification();
4045 // Open the output file. FILE_SIZE is the final size of the file.
4046 // If the file already exists, it is deleted/truncated. This method
4047 // is thread-unsafe.
4048 void
4049 open(off_t file_size);
4051 // Resize the output file. This method is thread-unsafe.
4052 void
4053 resize(off_t file_size);
4055 // Close the output file (flushing all buffered data) and make sure
4056 // there are no errors. This method is thread-unsafe.
4057 void
4058 close();
4060 // Return the size of this file.
4061 off_t
4062 filesize()
4063 { return this->file_size_; }
4065 // Return the name of this file.
4066 const char*
4067 filename()
4068 { return this->name_; }
4070 // We currently always use mmap which makes the view handling quite
4071 // simple. In the future we may support other approaches.
4073 // Write data to the output file.
4074 void
4075 write(off_t offset, const void* data, size_t len)
4076 { memcpy(this->base_ + offset, data, len); }
4078 // Get a buffer to use to write to the file, given the offset into
4079 // the file and the size.
4080 unsigned char*
4081 get_output_view(off_t start, size_t size)
4083 gold_assert(start >= 0
4084 && start + static_cast<off_t>(size) <= this->file_size_);
4085 return this->base_ + start;
4088 // VIEW must have been returned by get_output_view. Write the
4089 // buffer to the file, passing in the offset and the size.
4090 void
4091 write_output_view(off_t, size_t, unsigned char*)
4094 // Get a read/write buffer. This is used when we want to write part
4095 // of the file, read it in, and write it again.
4096 unsigned char*
4097 get_input_output_view(off_t start, size_t size)
4098 { return this->get_output_view(start, size); }
4100 // Write a read/write buffer back to the file.
4101 void
4102 write_input_output_view(off_t, size_t, unsigned char*)
4105 // Get a read buffer. This is used when we just want to read part
4106 // of the file back it in.
4107 const unsigned char*
4108 get_input_view(off_t start, size_t size)
4109 { return this->get_output_view(start, size); }
4111 // Release a read bfufer.
4112 void
4113 free_input_view(off_t, size_t, const unsigned char*)
4116 private:
4117 // Map the file into memory or, if that fails, allocate anonymous
4118 // memory.
4119 void
4120 map();
4122 // Allocate anonymous memory for the file.
4123 bool
4124 map_anonymous();
4126 // Map the file into memory.
4127 bool
4128 map_no_anonymous();
4130 // Unmap the file from memory (and flush to disk buffers).
4131 void
4132 unmap();
4134 // File name.
4135 const char* name_;
4136 // File descriptor.
4137 int o_;
4138 // File size.
4139 off_t file_size_;
4140 // Base of file mapped into memory.
4141 unsigned char* base_;
4142 // True iff base_ points to a memory buffer rather than an output file.
4143 bool map_is_anonymous_;
4144 // True if this is a temporary file which should not be output.
4145 bool is_temporary_;
4148 } // End namespace gold.
4150 #endif // !defined(GOLD_OUTPUT_H)