xref: /netbsd-src/external/gpl3/binutils/dist/gold/output.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 // output.h -- manage the output file for gold   -*- C++ -*-
2 
3 // Copyright (C) 2006-2024 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5 
6 // This file is part of gold.
7 
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.
12 
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.
17 
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.
22 
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
25 
26 #include <algorithm>
27 #include <list>
28 #include <vector>
29 
30 #include "elfcpp.h"
31 #include "mapfile.h"
32 #include "layout.h"
33 #include "reloc-types.h"
34 
35 namespace gold
36 {
37 
38 class General_options;
39 class Object;
40 class Symbol;
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;
49 template<int size, bool big_endian>
50 class Sized_relobj_file;
51 
52 // This class represents the output file.
53 
54 class Output_file
55 {
56  public:
57   Output_file(const char* name);
58 
59   // Indicate that this is a temporary file which should not be
60   // output.
61   void
set_is_temporary()62   set_is_temporary()
63   { this->is_temporary_ = true; }
64 
65   // Try to open an existing file. Returns false if the file doesn't
66   // exist, has a size of 0 or can't be mmaped.  This method is
67   // thread-unsafe.  If BASE_NAME is not NULL, use the contents of
68   // that file as the base for incremental linking.
69   bool
70   open_base_file(const char* base_name, bool writable);
71 
72   // Open the output file.  FILE_SIZE is the final size of the file.
73   // If the file already exists, it is deleted/truncated.  This method
74   // is thread-unsafe.
75   void
76   open(off_t file_size);
77 
78   // Resize the output file.  This method is thread-unsafe.
79   void
80   resize(off_t file_size);
81 
82   // Close the output file (flushing all buffered data) and make sure
83   // there are no errors.  This method is thread-unsafe.
84   void
85   close();
86 
87   // Return the size of this file.
88   off_t
filesize()89   filesize()
90   { return this->file_size_; }
91 
92   // Return the name of this file.
93   const char*
filename()94   filename()
95   { return this->name_; }
96 
97   // We currently always use mmap which makes the view handling quite
98   // simple.  In the future we may support other approaches.
99 
100   // Write data to the output file.
101   void
write(off_t offset,const void * data,size_t len)102   write(off_t offset, const void* data, size_t len)
103   { memcpy(this->base_ + offset, data, len); }
104 
105   // Get a buffer to use to write to the file, given the offset into
106   // the file and the size.
107   unsigned char*
get_output_view(off_t start,size_t size)108   get_output_view(off_t start, size_t size)
109   {
110     gold_assert(start >= 0
111 		&& start + static_cast<off_t>(size) <= this->file_size_);
112     return this->base_ + start;
113   }
114 
115   // VIEW must have been returned by get_output_view.  Write the
116   // buffer to the file, passing in the offset and the size.
117   void
write_output_view(off_t,size_t,unsigned char *)118   write_output_view(off_t, size_t, unsigned char*)
119   { }
120 
121   // Get a read/write buffer.  This is used when we want to write part
122   // of the file, read it in, and write it again.
123   unsigned char*
get_input_output_view(off_t start,size_t size)124   get_input_output_view(off_t start, size_t size)
125   { return this->get_output_view(start, size); }
126 
127   // Write a read/write buffer back to the file.
128   void
write_input_output_view(off_t,size_t,unsigned char *)129   write_input_output_view(off_t, size_t, unsigned char*)
130   { }
131 
132   // Get a read buffer.  This is used when we just want to read part
133   // of the file back it in.
134   const unsigned char*
get_input_view(off_t start,size_t size)135   get_input_view(off_t start, size_t size)
136   { return this->get_output_view(start, size); }
137 
138   // Release a read bfufer.
139   void
free_input_view(off_t,size_t,const unsigned char *)140   free_input_view(off_t, size_t, const unsigned char*)
141   { }
142 
143  private:
144   // Map the file into memory or, if that fails, allocate anonymous
145   // memory.
146   void
147   map();
148 
149   // Allocate anonymous memory for the file.
150   bool
151   map_anonymous();
152 
153   // Map the file into memory.
154   bool
155   map_no_anonymous(bool);
156 
157   // Unmap the file from memory (and flush to disk buffers).
158   void
159   unmap();
160 
161   // File name.
162   const char* name_;
163   // File descriptor.
164   int o_;
165   // File size.
166   off_t file_size_;
167   // Base of file mapped into memory.
168   unsigned char* base_;
169   // True iff base_ points to a memory buffer rather than an output file.
170   bool map_is_anonymous_;
171   // True if base_ was allocated using new rather than mmap.
172   bool map_is_allocated_;
173   // True if this is a temporary file which should not be output.
174   bool is_temporary_;
175 };
176 
177 // An abtract class for data which has to go into the output file.
178 
179 class Output_data
180 {
181  public:
Output_data()182   explicit Output_data()
183     : address_(0), data_size_(0), offset_(-1),
184       is_address_valid_(false), is_data_size_valid_(false),
185       is_offset_valid_(false), is_data_size_fixed_(false),
186       has_dynamic_reloc_(false)
187   { }
188 
189   virtual
190   ~Output_data();
191 
192   // Return the address.  For allocated sections, this is only valid
193   // after Layout::finalize is finished.
194   uint64_t
address()195   address() const
196   {
197     gold_assert(this->is_address_valid_);
198     return this->address_;
199   }
200 
201   // Return the size of the data.  For allocated sections, this must
202   // be valid after Layout::finalize calls set_address, but need not
203   // be valid before then.
204   off_t
data_size()205   data_size() const
206   {
207     gold_assert(this->is_data_size_valid_);
208     return this->data_size_;
209   }
210 
211   // Get the current data size.
212   off_t
current_data_size()213   current_data_size() const
214   { return this->current_data_size_for_child(); }
215 
216   // Return true if data size is fixed.
217   bool
is_data_size_fixed()218   is_data_size_fixed() const
219   { return this->is_data_size_fixed_; }
220 
221   // Return the file offset.  This is only valid after
222   // Layout::finalize is finished.  For some non-allocated sections,
223   // it may not be valid until near the end of the link.
224   off_t
offset()225   offset() const
226   {
227     gold_assert(this->is_offset_valid_);
228     return this->offset_;
229   }
230 
231   // Reset the address, file offset and data size.  This essentially
232   // disables the sanity testing about duplicate and unknown settings.
233   void
reset_address_and_file_offset()234   reset_address_and_file_offset()
235   {
236     this->is_address_valid_ = false;
237     this->is_offset_valid_ = false;
238     if (!this->is_data_size_fixed_)
239       this->is_data_size_valid_ = false;
240     this->do_reset_address_and_file_offset();
241   }
242 
243   // As above, but just for data size.
244   void
reset_data_size()245   reset_data_size()
246   {
247     if (!this->is_data_size_fixed_)
248       this->is_data_size_valid_ = false;
249   }
250 
251   // Return true if address and file offset already have reset values. In
252   // other words, calling reset_address_and_file_offset will not change them.
253   bool
address_and_file_offset_have_reset_values()254   address_and_file_offset_have_reset_values() const
255   { return this->do_address_and_file_offset_have_reset_values(); }
256 
257   // Return the required alignment.
258   uint64_t
addralign()259   addralign() const
260   { return this->do_addralign(); }
261 
262   // Return whether this has a load address.
263   bool
has_load_address()264   has_load_address() const
265   { return this->do_has_load_address(); }
266 
267   // Return the load address.
268   uint64_t
load_address()269   load_address() const
270   { return this->do_load_address(); }
271 
272   // Return whether this is an Output_section.
273   bool
is_section()274   is_section() const
275   { return this->do_is_section(); }
276 
277   // Return whether this is an Output_section of the specified type.
278   bool
is_section_type(elfcpp::Elf_Word stt)279   is_section_type(elfcpp::Elf_Word stt) const
280   { return this->do_is_section_type(stt); }
281 
282   // Return whether this is an Output_section with the specified flag
283   // set.
284   bool
is_section_flag_set(elfcpp::Elf_Xword shf)285   is_section_flag_set(elfcpp::Elf_Xword shf) const
286   { return this->do_is_section_flag_set(shf); }
287 
288   // Return the output section that this goes in, if there is one.
289   Output_section*
output_section()290   output_section()
291   { return this->do_output_section(); }
292 
293   const Output_section*
output_section()294   output_section() const
295   { return this->do_output_section(); }
296 
297   // Return the output section index, if there is an output section.
298   unsigned int
out_shndx()299   out_shndx() const
300   { return this->do_out_shndx(); }
301 
302   // Set the output section index, if this is an output section.
303   void
set_out_shndx(unsigned int shndx)304   set_out_shndx(unsigned int shndx)
305   { this->do_set_out_shndx(shndx); }
306 
307   // Set the address and file offset of this data, and finalize the
308   // size of the data.  This is called during Layout::finalize for
309   // allocated sections.
310   void
set_address_and_file_offset(uint64_t addr,off_t off)311   set_address_and_file_offset(uint64_t addr, off_t off)
312   {
313     this->set_address(addr);
314     this->set_file_offset(off);
315     this->finalize_data_size();
316   }
317 
318   // Set the address.
319   void
set_address(uint64_t addr)320   set_address(uint64_t addr)
321   {
322     gold_assert(!this->is_address_valid_);
323     this->address_ = addr;
324     this->is_address_valid_ = true;
325   }
326 
327   // Set the file offset.
328   void
set_file_offset(off_t off)329   set_file_offset(off_t off)
330   {
331     gold_assert(!this->is_offset_valid_);
332     this->offset_ = off;
333     this->is_offset_valid_ = true;
334   }
335 
336   // Update the data size without finalizing it.
337   void
pre_finalize_data_size()338   pre_finalize_data_size()
339   {
340     if (!this->is_data_size_valid_)
341       {
342 	// Tell the child class to update the data size.
343 	this->update_data_size();
344       }
345   }
346 
347   // Finalize the data size.
348   void
finalize_data_size()349   finalize_data_size()
350   {
351     if (!this->is_data_size_valid_)
352       {
353 	// Tell the child class to set the data size.
354 	this->set_final_data_size();
355 	gold_assert(this->is_data_size_valid_);
356       }
357   }
358 
359   // Set the TLS offset.  Called only for SHT_TLS sections.
360   void
set_tls_offset(uint64_t tls_base)361   set_tls_offset(uint64_t tls_base)
362   { this->do_set_tls_offset(tls_base); }
363 
364   // Return the TLS offset, relative to the base of the TLS segment.
365   // Valid only for SHT_TLS sections.
366   uint64_t
tls_offset()367   tls_offset() const
368   { return this->do_tls_offset(); }
369 
370   // Write the data to the output file.  This is called after
371   // Layout::finalize is complete.
372   void
write(Output_file * file)373   write(Output_file* file)
374   { this->do_write(file); }
375 
376   // This is called by Layout::finalize to note that the sizes of
377   // allocated sections must now be fixed.
378   static void
layout_complete()379   layout_complete()
380   { Output_data::allocated_sizes_are_fixed = true; }
381 
382   // Used to check that layout has been done.
383   static bool
is_layout_complete()384   is_layout_complete()
385   { return Output_data::allocated_sizes_are_fixed; }
386 
387   // Note that a dynamic reloc has been applied to this data.
388   void
add_dynamic_reloc()389   add_dynamic_reloc()
390   { this->has_dynamic_reloc_ = true; }
391 
392   // Return whether a dynamic reloc has been applied.
393   bool
has_dynamic_reloc()394   has_dynamic_reloc() const
395   { return this->has_dynamic_reloc_; }
396 
397   // Whether the address is valid.
398   bool
is_address_valid()399   is_address_valid() const
400   { return this->is_address_valid_; }
401 
402   // Whether the file offset is valid.
403   bool
is_offset_valid()404   is_offset_valid() const
405   { return this->is_offset_valid_; }
406 
407   // Whether the data size is valid.
408   bool
is_data_size_valid()409   is_data_size_valid() const
410   { return this->is_data_size_valid_; }
411 
412   // Print information to the map file.
413   void
print_to_mapfile(Mapfile * mapfile)414   print_to_mapfile(Mapfile* mapfile) const
415   { return this->do_print_to_mapfile(mapfile); }
416 
417  protected:
418   // Functions that child classes may or in some cases must implement.
419 
420   // Write the data to the output file.
421   virtual void
422   do_write(Output_file*) = 0;
423 
424   // Return the required alignment.
425   virtual uint64_t
426   do_addralign() const = 0;
427 
428   // Return whether this has a load address.
429   virtual bool
do_has_load_address()430   do_has_load_address() const
431   { return false; }
432 
433   // Return the load address.
434   virtual uint64_t
do_load_address()435   do_load_address() const
436   { gold_unreachable(); }
437 
438   // Return whether this is an Output_section.
439   virtual bool
do_is_section()440   do_is_section() const
441   { return false; }
442 
443   // Return whether this is an Output_section of the specified type.
444   // This only needs to be implement by Output_section.
445   virtual bool
do_is_section_type(elfcpp::Elf_Word)446   do_is_section_type(elfcpp::Elf_Word) const
447   { return false; }
448 
449   // Return whether this is an Output_section with the specific flag
450   // set.  This only needs to be implemented by Output_section.
451   virtual bool
do_is_section_flag_set(elfcpp::Elf_Xword)452   do_is_section_flag_set(elfcpp::Elf_Xword) const
453   { return false; }
454 
455   // Return the output section, if there is one.
456   virtual Output_section*
do_output_section()457   do_output_section()
458   { return NULL; }
459 
460   virtual const Output_section*
do_output_section()461   do_output_section() const
462   { return NULL; }
463 
464   // Return the output section index, if there is an output section.
465   virtual unsigned int
do_out_shndx()466   do_out_shndx() const
467   { gold_unreachable(); }
468 
469   // Set the output section index, if this is an output section.
470   virtual void
do_set_out_shndx(unsigned int)471   do_set_out_shndx(unsigned int)
472   { gold_unreachable(); }
473 
474   // This is a hook for derived classes to set the preliminary data size.
475   // This is called by pre_finalize_data_size, normally called during
476   // Layout::finalize, before the section address is set, and is used
477   // during an incremental update, when we need to know the size of a
478   // section before allocating space in the output file.  For classes
479   // where the current data size is up to date, this default version of
480   // the method can be inherited.
481   virtual void
update_data_size()482   update_data_size()
483   { }
484 
485   // This is a hook for derived classes to set the data size.  This is
486   // called by finalize_data_size, normally called during
487   // Layout::finalize, when the section address is set.
488   virtual void
set_final_data_size()489   set_final_data_size()
490   { gold_unreachable(); }
491 
492   // A hook for resetting the address and file offset.
493   virtual void
do_reset_address_and_file_offset()494   do_reset_address_and_file_offset()
495   { }
496 
497   // Return true if address and file offset already have reset values. In
498   // other words, calling reset_address_and_file_offset will not change them.
499   // A child class overriding do_reset_address_and_file_offset may need to
500   // also override this.
501   virtual bool
do_address_and_file_offset_have_reset_values()502   do_address_and_file_offset_have_reset_values() const
503   { return !this->is_address_valid_ && !this->is_offset_valid_; }
504 
505   // Set the TLS offset.  Called only for SHT_TLS sections.
506   virtual void
do_set_tls_offset(uint64_t)507   do_set_tls_offset(uint64_t)
508   { gold_unreachable(); }
509 
510   // Return the TLS offset, relative to the base of the TLS segment.
511   // Valid only for SHT_TLS sections.
512   virtual uint64_t
do_tls_offset()513   do_tls_offset() const
514   { gold_unreachable(); }
515 
516   // Print to the map file.  This only needs to be implemented by
517   // classes which may appear in a PT_LOAD segment.
518   virtual void
do_print_to_mapfile(Mapfile *)519   do_print_to_mapfile(Mapfile*) const
520   { gold_unreachable(); }
521 
522   // Functions that child classes may call.
523 
524   // Reset the address.  The Output_section class needs this when an
525   // SHF_ALLOC input section is added to an output section which was
526   // formerly not SHF_ALLOC.
527   void
mark_address_invalid()528   mark_address_invalid()
529   { this->is_address_valid_ = false; }
530 
531   // Set the size of the data.
532   void
set_data_size(off_t data_size)533   set_data_size(off_t data_size)
534   {
535     gold_assert(!this->is_data_size_valid_
536 		&& !this->is_data_size_fixed_);
537     this->data_size_ = data_size;
538     this->is_data_size_valid_ = true;
539   }
540 
541   // Fix the data size.  Once it is fixed, it cannot be changed
542   // and the data size remains always valid.
543   void
fix_data_size()544   fix_data_size()
545   {
546     gold_assert(this->is_data_size_valid_);
547     this->is_data_size_fixed_ = true;
548   }
549 
550   // Get the current data size--this is for the convenience of
551   // sections which build up their size over time.
552   off_t
current_data_size_for_child()553   current_data_size_for_child() const
554   { return this->data_size_; }
555 
556   // Set the current data size--this is for the convenience of
557   // sections which build up their size over time.
558   void
set_current_data_size_for_child(off_t data_size)559   set_current_data_size_for_child(off_t data_size)
560   {
561     gold_assert(!this->is_data_size_valid_);
562     this->data_size_ = data_size;
563   }
564 
565   // Return default alignment for the target size.
566   static uint64_t
567   default_alignment();
568 
569   // Return default alignment for a specified size--32 or 64.
570   static uint64_t
571   default_alignment_for_size(int size);
572 
573  private:
574   Output_data(const Output_data&);
575   Output_data& operator=(const Output_data&);
576 
577   // This is used for verification, to make sure that we don't try to
578   // change any sizes of allocated sections after we set the section
579   // addresses.
580   static bool allocated_sizes_are_fixed;
581 
582   // Memory address in output file.
583   uint64_t address_;
584   // Size of data in output file.
585   off_t data_size_;
586   // File offset of contents in output file.
587   off_t offset_;
588   // Whether address_ is valid.
589   bool is_address_valid_ : 1;
590   // Whether data_size_ is valid.
591   bool is_data_size_valid_ : 1;
592   // Whether offset_ is valid.
593   bool is_offset_valid_ : 1;
594   // Whether data size is fixed.
595   bool is_data_size_fixed_ : 1;
596   // Whether any dynamic relocs have been applied to this section.
597   bool has_dynamic_reloc_ : 1;
598 };
599 
600 // Output the section headers.
601 
602 class Output_section_headers : public Output_data
603 {
604  public:
605   Output_section_headers(const Layout*,
606 			 const Layout::Segment_list*,
607 			 const Layout::Section_list*,
608 			 const Layout::Section_list*,
609 			 const Stringpool*,
610 			 const Output_section*);
611 
612  protected:
613   // Write the data to the file.
614   void
615   do_write(Output_file*);
616 
617   // Return the required alignment.
618   uint64_t
do_addralign()619   do_addralign() const
620   { return Output_data::default_alignment(); }
621 
622   // Write to a map file.
623   void
do_print_to_mapfile(Mapfile * mapfile)624   do_print_to_mapfile(Mapfile* mapfile) const
625   { mapfile->print_output_data(this, _("** section headers")); }
626 
627   // Update the data size.
628   void
update_data_size()629   update_data_size()
630   { this->set_data_size(this->do_size()); }
631 
632   // Set final data size.
633   void
set_final_data_size()634   set_final_data_size()
635   { this->set_data_size(this->do_size()); }
636 
637  private:
638   // Write the data to the file with the right size and endianness.
639   template<int size, bool big_endian>
640   void
641   do_sized_write(Output_file*);
642 
643   // Compute data size.
644   off_t
645   do_size() const;
646 
647   const Layout* layout_;
648   const Layout::Segment_list* segment_list_;
649   const Layout::Section_list* section_list_;
650   const Layout::Section_list* unattached_section_list_;
651   const Stringpool* secnamepool_;
652   const Output_section* shstrtab_section_;
653 };
654 
655 // Output the segment headers.
656 
657 class Output_segment_headers : public Output_data
658 {
659  public:
660   Output_segment_headers(const Layout::Segment_list& segment_list);
661 
662  protected:
663   // Write the data to the file.
664   void
665   do_write(Output_file*);
666 
667   // Return the required alignment.
668   uint64_t
do_addralign()669   do_addralign() const
670   { return Output_data::default_alignment(); }
671 
672   // Write to a map file.
673   void
do_print_to_mapfile(Mapfile * mapfile)674   do_print_to_mapfile(Mapfile* mapfile) const
675   { mapfile->print_output_data(this, _("** segment headers")); }
676 
677   // Set final data size.
678   void
set_final_data_size()679   set_final_data_size()
680   { this->set_data_size(this->do_size()); }
681 
682  private:
683   // Write the data to the file with the right size and endianness.
684   template<int size, bool big_endian>
685   void
686   do_sized_write(Output_file*);
687 
688   // Compute the current size.
689   off_t
690   do_size() const;
691 
692   const Layout::Segment_list& segment_list_;
693 };
694 
695 // Output the ELF file header.
696 
697 class Output_file_header : public Output_data
698 {
699  public:
700   Output_file_header(Target*,
701 		     const Symbol_table*,
702 		     const Output_segment_headers*);
703 
704   // Add information about the section headers.  We lay out the ELF
705   // file header before we create the section headers.
706   void set_section_info(const Output_section_headers*,
707 			const Output_section* shstrtab);
708 
709  protected:
710   // Write the data to the file.
711   void
712   do_write(Output_file*);
713 
714   // Return the required alignment.
715   uint64_t
do_addralign()716   do_addralign() const
717   { return Output_data::default_alignment(); }
718 
719   // Write to a map file.
720   void
do_print_to_mapfile(Mapfile * mapfile)721   do_print_to_mapfile(Mapfile* mapfile) const
722   { mapfile->print_output_data(this, _("** file header")); }
723 
724   // Set final data size.
725   void
set_final_data_size(void)726   set_final_data_size(void)
727   { this->set_data_size(this->do_size()); }
728 
729  private:
730   // Write the data to the file with the right size and endianness.
731   template<int size, bool big_endian>
732   void
733   do_sized_write(Output_file*);
734 
735   // Return the value to use for the entry address.
736   template<int size>
737   typename elfcpp::Elf_types<size>::Elf_Addr
738   entry();
739 
740   // Compute the current data size.
741   off_t
742   do_size() const;
743 
744   Target* target_;
745   const Symbol_table* symtab_;
746   const Output_segment_headers* segment_header_;
747   const Output_section_headers* section_header_;
748   const Output_section* shstrtab_;
749 };
750 
751 // Output sections are mainly comprised of input sections.  However,
752 // there are cases where we have data to write out which is not in an
753 // input section.  Output_section_data is used in such cases.  This is
754 // an abstract base class.
755 
756 class Output_section_data : public Output_data
757 {
758  public:
Output_section_data(off_t data_size,uint64_t addralign,bool is_data_size_fixed)759   Output_section_data(off_t data_size, uint64_t addralign,
760 		      bool is_data_size_fixed)
761     : Output_data(), output_section_(NULL), addralign_(addralign)
762   {
763     this->set_data_size(data_size);
764     if (is_data_size_fixed)
765       this->fix_data_size();
766   }
767 
Output_section_data(uint64_t addralign)768   Output_section_data(uint64_t addralign)
769     : Output_data(), output_section_(NULL), addralign_(addralign)
770   { }
771 
772   // Return the output section.
773   Output_section*
output_section()774   output_section()
775   { return this->output_section_; }
776 
777   const Output_section*
output_section()778   output_section() const
779   { return this->output_section_; }
780 
781   // Record the output section.
782   void
783   set_output_section(Output_section* os);
784 
785   // Add an input section, for SHF_MERGE sections.  This returns true
786   // if the section was handled.
787   bool
add_input_section(Relobj * object,unsigned int shndx)788   add_input_section(Relobj* object, unsigned int shndx)
789   { return this->do_add_input_section(object, shndx); }
790 
791   // Given an input OBJECT, an input section index SHNDX within that
792   // object, and an OFFSET relative to the start of that input
793   // section, return whether or not the corresponding offset within
794   // the output section is known.  If this function returns true, it
795   // sets *POUTPUT to the output offset.  The value -1 indicates that
796   // this input offset is being discarded.
797   bool
output_offset(const Relobj * object,unsigned int shndx,section_offset_type offset,section_offset_type * poutput)798   output_offset(const Relobj* object, unsigned int shndx,
799 		section_offset_type offset,
800 		section_offset_type* poutput) const
801   { return this->do_output_offset(object, shndx, offset, poutput); }
802 
803   // Write the contents to a buffer.  This is used for sections which
804   // require postprocessing, such as compression.
805   void
write_to_buffer(unsigned char * buffer)806   write_to_buffer(unsigned char* buffer)
807   { this->do_write_to_buffer(buffer); }
808 
809   // Print merge stats to stderr.  This should only be called for
810   // SHF_MERGE sections.
811   void
print_merge_stats(const char * section_name)812   print_merge_stats(const char* section_name)
813   { this->do_print_merge_stats(section_name); }
814 
815  protected:
816   // The child class must implement do_write.
817 
818   // The child class may implement specific adjustments to the output
819   // section.
820   virtual void
do_adjust_output_section(Output_section *)821   do_adjust_output_section(Output_section*)
822   { }
823 
824   // May be implemented by child class.  Return true if the section
825   // was handled.
826   virtual bool
do_add_input_section(Relobj *,unsigned int)827   do_add_input_section(Relobj*, unsigned int)
828   { gold_unreachable(); }
829 
830   // The child class may implement output_offset.
831   virtual bool
do_output_offset(const Relobj *,unsigned int,section_offset_type,section_offset_type *)832   do_output_offset(const Relobj*, unsigned int, section_offset_type,
833 		   section_offset_type*) const
834   { return false; }
835 
836   // The child class may implement write_to_buffer.  Most child
837   // classes can not appear in a compressed section, and they do not
838   // implement this.
839   virtual void
do_write_to_buffer(unsigned char *)840   do_write_to_buffer(unsigned char*)
841   { gold_unreachable(); }
842 
843   // Print merge statistics.
844   virtual void
do_print_merge_stats(const char *)845   do_print_merge_stats(const char*)
846   { gold_unreachable(); }
847 
848   // Return the required alignment.
849   uint64_t
do_addralign()850   do_addralign() const
851   { return this->addralign_; }
852 
853   // Return the output section.
854   Output_section*
do_output_section()855   do_output_section()
856   { return this->output_section_; }
857 
858   const Output_section*
do_output_section()859   do_output_section() const
860   { return this->output_section_; }
861 
862   // Return the section index of the output section.
863   unsigned int
864   do_out_shndx() const;
865 
866   // Set the alignment.
867   void
868   set_addralign(uint64_t addralign);
869 
870  private:
871   // The output section for this section.
872   Output_section* output_section_;
873   // The required alignment.
874   uint64_t addralign_;
875 };
876 
877 // Some Output_section_data classes build up their data step by step,
878 // rather than all at once.  This class provides an interface for
879 // them.
880 
881 class Output_section_data_build : public Output_section_data
882 {
883  public:
Output_section_data_build(uint64_t addralign)884   Output_section_data_build(uint64_t addralign)
885     : Output_section_data(addralign)
886   { }
887 
Output_section_data_build(off_t data_size,uint64_t addralign)888   Output_section_data_build(off_t data_size, uint64_t addralign)
889     : Output_section_data(data_size, addralign, false)
890   { }
891 
892   // Set the current data size.
893   void
set_current_data_size(off_t data_size)894   set_current_data_size(off_t data_size)
895   { this->set_current_data_size_for_child(data_size); }
896 
897  protected:
898   // Set the final data size.
899   virtual void
set_final_data_size()900   set_final_data_size()
901   { this->set_data_size(this->current_data_size_for_child()); }
902 };
903 
904 // A simple case of Output_data in which we have constant data to
905 // output.
906 
907 class Output_data_const : public Output_section_data
908 {
909  public:
Output_data_const(const std::string & data,uint64_t addralign)910   Output_data_const(const std::string& data, uint64_t addralign)
911     : Output_section_data(data.size(), addralign, true), data_(data)
912   { }
913 
Output_data_const(const char * p,off_t len,uint64_t addralign)914   Output_data_const(const char* p, off_t len, uint64_t addralign)
915     : Output_section_data(len, addralign, true), data_(p, len)
916   { }
917 
Output_data_const(const unsigned char * p,off_t len,uint64_t addralign)918   Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
919     : Output_section_data(len, addralign, true),
920       data_(reinterpret_cast<const char*>(p), len)
921   { }
922 
923  protected:
924   // Write the data to the output file.
925   void
926   do_write(Output_file*);
927 
928   // Write the data to a buffer.
929   void
do_write_to_buffer(unsigned char * buffer)930   do_write_to_buffer(unsigned char* buffer)
931   { memcpy(buffer, this->data_.data(), this->data_.size()); }
932 
933   // Write to a map file.
934   void
do_print_to_mapfile(Mapfile * mapfile)935   do_print_to_mapfile(Mapfile* mapfile) const
936   { mapfile->print_output_data(this, _("** fill")); }
937 
938  private:
939   std::string data_;
940 };
941 
942 // Another version of Output_data with constant data, in which the
943 // buffer is allocated by the caller.
944 
945 class Output_data_const_buffer : public Output_section_data
946 {
947  public:
Output_data_const_buffer(const unsigned char * p,off_t len,uint64_t addralign,const char * map_name)948   Output_data_const_buffer(const unsigned char* p, off_t len,
949 			   uint64_t addralign, const char* map_name)
950     : Output_section_data(len, addralign, true),
951       p_(p), map_name_(map_name)
952   { }
953 
954  protected:
955   // Write the data the output file.
956   void
957   do_write(Output_file*);
958 
959   // Write the data to a buffer.
960   void
do_write_to_buffer(unsigned char * buffer)961   do_write_to_buffer(unsigned char* buffer)
962   { memcpy(buffer, this->p_, this->data_size()); }
963 
964   // Write to a map file.
965   void
do_print_to_mapfile(Mapfile * mapfile)966   do_print_to_mapfile(Mapfile* mapfile) const
967   { mapfile->print_output_data(this, _(this->map_name_)); }
968 
969  private:
970   // The data to output.
971   const unsigned char* p_;
972   // Name to use in a map file.  Maps are a rarely used feature, but
973   // the space usage is minor as aren't very many of these objects.
974   const char* map_name_;
975 };
976 
977 // A place holder for a fixed amount of data written out via some
978 // other mechanism.
979 
980 class Output_data_fixed_space : public Output_section_data
981 {
982  public:
Output_data_fixed_space(off_t data_size,uint64_t addralign,const char * map_name)983   Output_data_fixed_space(off_t data_size, uint64_t addralign,
984 			  const char* map_name)
985     : Output_section_data(data_size, addralign, true),
986       map_name_(map_name)
987   { }
988 
989  protected:
990   // Write out the data--the actual data must be written out
991   // elsewhere.
992   void
do_write(Output_file *)993   do_write(Output_file*)
994   { }
995 
996   // Write to a map file.
997   void
do_print_to_mapfile(Mapfile * mapfile)998   do_print_to_mapfile(Mapfile* mapfile) const
999   { mapfile->print_output_data(this, _(this->map_name_)); }
1000 
1001  private:
1002   // Name to use in a map file.  Maps are a rarely used feature, but
1003   // the space usage is minor as aren't very many of these objects.
1004   const char* map_name_;
1005 };
1006 
1007 // A place holder for variable sized data written out via some other
1008 // mechanism.
1009 
1010 class Output_data_space : public Output_section_data_build
1011 {
1012  public:
Output_data_space(uint64_t addralign,const char * map_name)1013   explicit Output_data_space(uint64_t addralign, const char* map_name)
1014     : Output_section_data_build(addralign),
1015       map_name_(map_name)
1016   { }
1017 
Output_data_space(off_t data_size,uint64_t addralign,const char * map_name)1018   explicit Output_data_space(off_t data_size, uint64_t addralign,
1019 			     const char* map_name)
1020     : Output_section_data_build(data_size, addralign),
1021       map_name_(map_name)
1022   { }
1023 
1024   // Set the alignment.
1025   void
set_space_alignment(uint64_t align)1026   set_space_alignment(uint64_t align)
1027   { this->set_addralign(align); }
1028 
1029  protected:
1030   // Write out the data--the actual data must be written out
1031   // elsewhere.
1032   void
do_write(Output_file *)1033   do_write(Output_file*)
1034   { }
1035 
1036   // Write to a map file.
1037   void
do_print_to_mapfile(Mapfile * mapfile)1038   do_print_to_mapfile(Mapfile* mapfile) const
1039   { mapfile->print_output_data(this, _(this->map_name_)); }
1040 
1041  private:
1042   // Name to use in a map file.  Maps are a rarely used feature, but
1043   // the space usage is minor as aren't very many of these objects.
1044   const char* map_name_;
1045 };
1046 
1047 // Fill fixed space with zeroes.  This is just like
1048 // Output_data_fixed_space, except that the map name is known.
1049 
1050 class Output_data_zero_fill : public Output_section_data
1051 {
1052  public:
Output_data_zero_fill(off_t data_size,uint64_t addralign)1053   Output_data_zero_fill(off_t data_size, uint64_t addralign)
1054     : Output_section_data(data_size, addralign, true)
1055   { }
1056 
1057  protected:
1058   // There is no data to write out.
1059   void
do_write(Output_file *)1060   do_write(Output_file*)
1061   { }
1062 
1063   // Write to a map file.
1064   void
do_print_to_mapfile(Mapfile * mapfile)1065   do_print_to_mapfile(Mapfile* mapfile) const
1066   { mapfile->print_output_data(this, "** zero fill"); }
1067 };
1068 
1069 // A string table which goes into an output section.
1070 
1071 class Output_data_strtab : public Output_section_data
1072 {
1073  public:
Output_data_strtab(Stringpool * strtab)1074   Output_data_strtab(Stringpool* strtab)
1075     : Output_section_data(1), strtab_(strtab)
1076   { }
1077 
1078  protected:
1079   // This is called to update the section size prior to assigning
1080   // the address and file offset.
1081   void
update_data_size()1082   update_data_size()
1083   { this->set_final_data_size(); }
1084 
1085   // This is called to set the address and file offset.  Here we make
1086   // sure that the Stringpool is finalized.
1087   void
1088   set_final_data_size();
1089 
1090   // Write out the data.
1091   void
1092   do_write(Output_file*);
1093 
1094   // Write the data to a buffer.
1095   void
do_write_to_buffer(unsigned char * buffer)1096   do_write_to_buffer(unsigned char* buffer)
1097   { this->strtab_->write_to_buffer(buffer, this->data_size()); }
1098 
1099   // Write to a map file.
1100   void
do_print_to_mapfile(Mapfile * mapfile)1101   do_print_to_mapfile(Mapfile* mapfile) const
1102   { mapfile->print_output_data(this, _("** string table")); }
1103 
1104  private:
1105   Stringpool* strtab_;
1106 };
1107 
1108 // This POD class is used to represent a single reloc in the output
1109 // file.  This could be a private class within Output_data_reloc, but
1110 // the templatization is complex enough that I broke it out into a
1111 // separate class.  The class is templatized on either elfcpp::SHT_REL
1112 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
1113 // relocation or an ordinary relocation.
1114 
1115 // A relocation can be against a global symbol, a local symbol, a
1116 // local section symbol, an output section, or the undefined symbol at
1117 // index 0.  We represent the latter by using a NULL global symbol.
1118 
1119 template<int sh_type, bool dynamic, int size, bool big_endian>
1120 class Output_reloc;
1121 
1122 template<bool dynamic, int size, bool big_endian>
1123 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1124 {
1125  public:
1126   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1127   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1128 
1129   static const Address invalid_address = static_cast<Address>(0) - 1;
1130 
1131   // An uninitialized entry.  We need this because we want to put
1132   // instances of this class into an STL container.
Output_reloc()1133   Output_reloc()
1134     : local_sym_index_(INVALID_CODE)
1135   { }
1136 
1137   // We have a bunch of different constructors.  They come in pairs
1138   // depending on how the address of the relocation is specified.  It
1139   // can either be an offset in an Output_data or an offset in an
1140   // input section.
1141 
1142   // A reloc against a global symbol.
1143 
1144   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1145 	       Address address, bool is_relative, bool is_symbolless,
1146 	       bool use_plt_offset);
1147 
1148   Output_reloc(Symbol* gsym, unsigned int type,
1149 	       Sized_relobj<size, big_endian>* relobj,
1150 	       unsigned int shndx, Address address, bool is_relative,
1151 	       bool is_symbolless, bool use_plt_offset);
1152 
1153   // A reloc against a local symbol or local section symbol.
1154 
1155   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1156 	       unsigned int local_sym_index, unsigned int type,
1157 	       Output_data* od, Address address, bool is_relative,
1158 	       bool is_symbolless, bool is_section_symbol,
1159 	       bool use_plt_offset);
1160 
1161   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1162 	       unsigned int local_sym_index, unsigned int type,
1163 	       unsigned int shndx, Address address, bool is_relative,
1164 	       bool is_symbolless, bool is_section_symbol,
1165 	       bool use_plt_offset);
1166 
1167   // A reloc against the STT_SECTION symbol of an output section.
1168 
1169   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1170 	       Address address, bool is_relative);
1171 
1172   Output_reloc(Output_section* os, unsigned int type,
1173 	       Sized_relobj<size, big_endian>* relobj, unsigned int shndx,
1174 	       Address address, bool is_relative);
1175 
1176   // An absolute or relative relocation with no symbol.
1177 
1178   Output_reloc(unsigned int type, Output_data* od, Address address,
1179 	       bool is_relative);
1180 
1181   Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1182 	       unsigned int shndx, Address address, bool is_relative);
1183 
1184   // A target specific relocation.  The target will be called to get
1185   // the symbol index, passing ARG.  The type and offset will be set
1186   // as for other relocation types.
1187 
1188   Output_reloc(unsigned int type, void* arg, Output_data* od,
1189 	       Address address);
1190 
1191   Output_reloc(unsigned int type, void* arg,
1192 	       Sized_relobj<size, big_endian>* relobj,
1193 	       unsigned int shndx, Address address);
1194 
1195   // Return the reloc type.
1196   unsigned int
type()1197   type() const
1198   { return this->type_; }
1199 
1200   // Return whether this is a RELATIVE relocation.
1201   bool
is_relative()1202   is_relative() const
1203   { return this->is_relative_; }
1204 
1205   // Return whether this is a relocation which should not use
1206   // a symbol, but which obtains its addend from a symbol.
1207   bool
is_symbolless()1208   is_symbolless() const
1209   { return this->is_symbolless_; }
1210 
1211   // Return whether this is against a local section symbol.
1212   bool
is_local_section_symbol()1213   is_local_section_symbol() const
1214   {
1215     return (this->local_sym_index_ != GSYM_CODE
1216 	    && this->local_sym_index_ != SECTION_CODE
1217 	    && this->local_sym_index_ != INVALID_CODE
1218 	    && this->local_sym_index_ != TARGET_CODE
1219 	    && this->is_section_symbol_);
1220   }
1221 
1222   // Return whether this is a target specific relocation.
1223   bool
is_target_specific()1224   is_target_specific() const
1225   { return this->local_sym_index_ == TARGET_CODE; }
1226 
1227   // Return the argument to pass to the target for a target specific
1228   // relocation.
1229   void*
target_arg()1230   target_arg() const
1231   {
1232     gold_assert(this->local_sym_index_ == TARGET_CODE);
1233     return this->u1_.arg;
1234   }
1235 
1236   // For a local section symbol, return the offset of the input
1237   // section within the output section.  ADDEND is the addend being
1238   // applied to the input section.
1239   Address
1240   local_section_offset(Addend addend) const;
1241 
1242   // Get the value of the symbol referred to by a Rel relocation when
1243   // we are adding the given ADDEND.
1244   Address
1245   symbol_value(Addend addend) const;
1246 
1247   // If this relocation is against an input section, return the
1248   // relocatable object containing the input section.
1249   Sized_relobj<size, big_endian>*
get_relobj()1250   get_relobj() const
1251   {
1252     if (this->shndx_ == INVALID_CODE)
1253       return NULL;
1254     return this->u2_.relobj;
1255   }
1256 
1257   // Write the reloc entry to an output view.
1258   void
1259   write(unsigned char* pov) const;
1260 
1261   // Write the offset and info fields to Write_rel.
1262   template<typename Write_rel>
1263   void write_rel(Write_rel*) const;
1264 
1265   // This is used when sorting dynamic relocs.  Return -1 to sort this
1266   // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1267   int
1268   compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1269     const;
1270 
1271   // Return whether this reloc should be sorted before the argument
1272   // when sorting dynamic relocs.
1273   bool
sort_before(const Output_reloc<elfcpp::SHT_REL,dynamic,size,big_endian> & r2)1274   sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1275 	      r2) const
1276   { return this->compare(r2) < 0; }
1277 
1278   // Return the symbol index.
1279   unsigned int
1280   get_symbol_index() const;
1281 
1282   // Return the output address.
1283   Address
1284   get_address() const;
1285 
1286  private:
1287   // Record that we need a dynamic symbol index.
1288   void
1289   set_needs_dynsym_index();
1290 
1291   // Codes for local_sym_index_.
1292   enum
1293   {
1294     // Global symbol.
1295     GSYM_CODE = -1U,
1296     // Output section.
1297     SECTION_CODE = -2U,
1298     // Target specific.
1299     TARGET_CODE = -3U,
1300     // Invalid uninitialized entry.
1301     INVALID_CODE = -4U
1302   };
1303 
1304   union
1305   {
1306     // For a local symbol or local section symbol
1307     // (this->local_sym_index_ >= 0), the object.  We will never
1308     // generate a relocation against a local symbol in a dynamic
1309     // object; that doesn't make sense.  And our callers will always
1310     // be templatized, so we use Sized_relobj here.
1311     Sized_relobj<size, big_endian>* relobj;
1312     // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1313     // symbol.  If this is NULL, it indicates a relocation against the
1314     // undefined 0 symbol.
1315     Symbol* gsym;
1316     // For a relocation against an output section
1317     // (this->local_sym_index_ == SECTION_CODE), the output section.
1318     Output_section* os;
1319     // For a target specific relocation, an argument to pass to the
1320     // target.
1321     void* arg;
1322   } u1_;
1323   union
1324   {
1325     // If this->shndx_ is not INVALID CODE, the object which holds the
1326     // input section being used to specify the reloc address.
1327     Sized_relobj<size, big_endian>* relobj;
1328     // If this->shndx_ is INVALID_CODE, the output data being used to
1329     // specify the reloc address.  This may be NULL if the reloc
1330     // address is absolute.
1331     Output_data* od;
1332   } u2_;
1333   // The address offset within the input section or the Output_data.
1334   Address address_;
1335   // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1336   // relocation against an output section, or TARGET_CODE for a target
1337   // specific relocation, or INVALID_CODE for an uninitialized value.
1338   // Otherwise, for a local symbol (this->is_section_symbol_ is
1339   // false), the local symbol index.  For a local section symbol
1340   // (this->is_section_symbol_ is true), the section index in the
1341   // input file.
1342   unsigned int local_sym_index_;
1343   // The reloc type--a processor specific code.
1344   unsigned int type_ : 28;
1345   // True if the relocation is a RELATIVE relocation.
1346   bool is_relative_ : 1;
1347   // True if the relocation is one which should not use
1348   // a symbol, but which obtains its addend from a symbol.
1349   bool is_symbolless_ : 1;
1350   // True if the relocation is against a section symbol.
1351   bool is_section_symbol_ : 1;
1352   // True if the addend should be the PLT offset.
1353   // (Used only for RELA, but stored here for space.)
1354   bool use_plt_offset_ : 1;
1355   // If the reloc address is an input section in an object, the
1356   // section index.  This is INVALID_CODE if the reloc address is
1357   // specified in some other way.
1358   unsigned int shndx_;
1359 };
1360 
1361 // The SHT_RELA version of Output_reloc<>.  This is just derived from
1362 // the SHT_REL version of Output_reloc, but it adds an addend.
1363 
1364 template<bool dynamic, int size, bool big_endian>
1365 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1366 {
1367  public:
1368   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1369   typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1370 
1371   // An uninitialized entry.
Output_reloc()1372   Output_reloc()
1373     : rel_()
1374   { }
1375 
1376   // A reloc against a global symbol.
1377 
Output_reloc(Symbol * gsym,unsigned int type,Output_data * od,Address address,Addend addend,bool is_relative,bool is_symbolless,bool use_plt_offset)1378   Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1379 	       Address address, Addend addend, bool is_relative,
1380 	       bool is_symbolless, bool use_plt_offset)
1381     : rel_(gsym, type, od, address, is_relative, is_symbolless,
1382 	   use_plt_offset),
1383       addend_(addend)
1384   { }
1385 
Output_reloc(Symbol * gsym,unsigned int type,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend,bool is_relative,bool is_symbolless,bool use_plt_offset)1386   Output_reloc(Symbol* gsym, unsigned int type,
1387 	       Sized_relobj<size, big_endian>* relobj,
1388 	       unsigned int shndx, Address address, Addend addend,
1389 	       bool is_relative, bool is_symbolless, bool use_plt_offset)
1390     : rel_(gsym, type, relobj, shndx, address, is_relative,
1391 	   is_symbolless, use_plt_offset), addend_(addend)
1392   { }
1393 
1394   // A reloc against a local symbol.
1395 
Output_reloc(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address,Addend addend,bool is_relative,bool is_symbolless,bool is_section_symbol,bool use_plt_offset)1396   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1397 	       unsigned int local_sym_index, unsigned int type,
1398 	       Output_data* od, Address address,
1399 	       Addend addend, bool is_relative,
1400 	       bool is_symbolless, bool is_section_symbol,
1401 	       bool use_plt_offset)
1402     : rel_(relobj, local_sym_index, type, od, address, is_relative,
1403 	   is_symbolless, is_section_symbol, use_plt_offset),
1404       addend_(addend)
1405   { }
1406 
Output_reloc(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,unsigned int shndx,Address address,Addend addend,bool is_relative,bool is_symbolless,bool is_section_symbol,bool use_plt_offset)1407   Output_reloc(Sized_relobj<size, big_endian>* relobj,
1408 	       unsigned int local_sym_index, unsigned int type,
1409 	       unsigned int shndx, Address address,
1410 	       Addend addend, bool is_relative,
1411 	       bool is_symbolless, bool is_section_symbol,
1412 	       bool use_plt_offset)
1413     : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1414 	   is_symbolless, is_section_symbol, use_plt_offset),
1415       addend_(addend)
1416   { }
1417 
1418   // A reloc against the STT_SECTION symbol of an output section.
1419 
Output_reloc(Output_section * os,unsigned int type,Output_data * od,Address address,Addend addend,bool is_relative)1420   Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1421 	       Address address, Addend addend, bool is_relative)
1422     : rel_(os, type, od, address, is_relative), addend_(addend)
1423   { }
1424 
Output_reloc(Output_section * os,unsigned int type,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend,bool is_relative)1425   Output_reloc(Output_section* os, unsigned int type,
1426 	       Sized_relobj<size, big_endian>* relobj,
1427 	       unsigned int shndx, Address address, Addend addend,
1428 	       bool is_relative)
1429     : rel_(os, type, relobj, shndx, address, is_relative), addend_(addend)
1430   { }
1431 
1432   // An absolute or relative relocation with no symbol.
1433 
Output_reloc(unsigned int type,Output_data * od,Address address,Addend addend,bool is_relative)1434   Output_reloc(unsigned int type, Output_data* od, Address address,
1435 	       Addend addend, bool is_relative)
1436     : rel_(type, od, address, is_relative), addend_(addend)
1437   { }
1438 
Output_reloc(unsigned int type,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend,bool is_relative)1439   Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1440 	       unsigned int shndx, Address address, Addend addend,
1441 	       bool is_relative)
1442     : rel_(type, relobj, shndx, address, is_relative), addend_(addend)
1443   { }
1444 
1445   // A target specific relocation.  The target will be called to get
1446   // the symbol index and the addend, passing ARG.  The type and
1447   // offset will be set as for other relocation types.
1448 
Output_reloc(unsigned int type,void * arg,Output_data * od,Address address,Addend addend)1449   Output_reloc(unsigned int type, void* arg, Output_data* od,
1450 	       Address address, Addend addend)
1451     : rel_(type, arg, od, address), addend_(addend)
1452   { }
1453 
Output_reloc(unsigned int type,void * arg,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)1454   Output_reloc(unsigned int type, void* arg,
1455 	       Sized_relobj<size, big_endian>* relobj,
1456 	       unsigned int shndx, Address address, Addend addend)
1457     : rel_(type, arg, relobj, shndx, address), addend_(addend)
1458   { }
1459 
1460   // Return whether this is a RELATIVE relocation.
1461   bool
is_relative()1462   is_relative() const
1463   { return this->rel_.is_relative(); }
1464 
1465   // Return whether this is a relocation which should not use
1466   // a symbol, but which obtains its addend from a symbol.
1467   bool
is_symbolless()1468   is_symbolless() const
1469   { return this->rel_.is_symbolless(); }
1470 
1471   // If this relocation is against an input section, return the
1472   // relocatable object containing the input section.
1473   Sized_relobj<size, big_endian>*
get_relobj()1474   get_relobj() const
1475   { return this->rel_.get_relobj(); }
1476 
1477   // Write the reloc entry to an output view.
1478   void
1479   write(unsigned char* pov) const;
1480 
1481   // Return whether this reloc should be sorted before the argument
1482   // when sorting dynamic relocs.
1483   bool
sort_before(const Output_reloc<elfcpp::SHT_RELA,dynamic,size,big_endian> & r2)1484   sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1485 	      r2) const
1486   {
1487     int i = this->rel_.compare(r2.rel_);
1488     if (i < 0)
1489       return true;
1490     else if (i > 0)
1491       return false;
1492     else
1493       return this->addend_ < r2.addend_;
1494   }
1495 
1496  private:
1497   // The basic reloc.
1498   Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1499   // The addend.
1500   Addend addend_;
1501 };
1502 
1503 // Output_data_reloc_generic is a non-template base class for
1504 // Output_data_reloc_base.  This gives the generic code a way to hold
1505 // a pointer to a reloc section.
1506 
1507 class Output_data_reloc_generic : public Output_section_data_build
1508 {
1509  public:
Output_data_reloc_generic(int size,bool sort_relocs)1510   Output_data_reloc_generic(int size, bool sort_relocs)
1511     : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1512       relative_reloc_count_(0), sort_relocs_(sort_relocs)
1513   { }
1514 
1515   // Return the number of relative relocs in this section.
1516   size_t
relative_reloc_count()1517   relative_reloc_count() const
1518   { return this->relative_reloc_count_; }
1519 
1520   // Whether we should sort the relocs.
1521   bool
sort_relocs()1522   sort_relocs() const
1523   { return this->sort_relocs_; }
1524 
1525   // Add a reloc of type TYPE against the global symbol GSYM.  The
1526   // relocation applies to the data at offset ADDRESS within OD.
1527   virtual void
1528   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1529 		     uint64_t address, uint64_t addend) = 0;
1530 
1531   // Add a reloc of type TYPE against the global symbol GSYM.  The
1532   // relocation applies to data at offset ADDRESS within section SHNDX
1533   // of object file RELOBJ.  OD is the associated output section.
1534   virtual void
1535   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1536 		     Relobj* relobj, unsigned int shndx, uint64_t address,
1537 		     uint64_t addend) = 0;
1538 
1539   // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1540   // in RELOBJ.  The relocation applies to the data at offset ADDRESS
1541   // within OD.
1542   virtual void
1543   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1544 		    unsigned int type, Output_data* od, uint64_t address,
1545 		    uint64_t addend) = 0;
1546 
1547   // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1548   // in RELOBJ.  The relocation applies to the data at offset ADDRESS
1549   // within section SHNDX of RELOBJ.  OD is the associated output
1550   // section.
1551   virtual void
1552   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1553 		    unsigned int type, Output_data* od, unsigned int shndx,
1554 		    uint64_t address, uint64_t addend) = 0;
1555 
1556   // Add a reloc of type TYPE against the STT_SECTION symbol of the
1557   // output section OS.  The relocation applies to the data at offset
1558   // ADDRESS within OD.
1559   virtual void
1560   add_output_section_generic(Output_section *os, unsigned int type,
1561 			     Output_data* od, uint64_t address,
1562 			     uint64_t addend) = 0;
1563 
1564   // Add a reloc of type TYPE against the STT_SECTION symbol of the
1565   // output section OS.  The relocation applies to the data at offset
1566   // ADDRESS within section SHNDX of RELOBJ.  OD is the associated
1567   // output section.
1568   virtual void
1569   add_output_section_generic(Output_section* os, unsigned int type,
1570 			     Output_data* od, Relobj* relobj,
1571 			     unsigned int shndx, uint64_t address,
1572 			     uint64_t addend) = 0;
1573 
1574  protected:
1575   // Note that we've added another relative reloc.
1576   void
bump_relative_reloc_count()1577   bump_relative_reloc_count()
1578   { ++this->relative_reloc_count_; }
1579 
1580  private:
1581   // The number of relative relocs added to this section.  This is to
1582   // support DT_RELCOUNT.
1583   size_t relative_reloc_count_;
1584   // Whether to sort the relocations when writing them out, to make
1585   // the dynamic linker more efficient.
1586   bool sort_relocs_;
1587 };
1588 
1589 // Output_data_reloc is used to manage a section containing relocs.
1590 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1591 // indicates whether this is a dynamic relocation or a normal
1592 // relocation.  Output_data_reloc_base is a base class.
1593 // Output_data_reloc is the real class, which we specialize based on
1594 // the reloc type.
1595 
1596 template<int sh_type, bool dynamic, int size, bool big_endian>
1597 class Output_data_reloc_base : public Output_data_reloc_generic
1598 {
1599  public:
1600   typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1601   typedef typename Output_reloc_type::Address Address;
1602   static const int reloc_size =
1603     Reloc_types<sh_type, size, big_endian>::reloc_size;
1604 
1605   // Construct the section.
Output_data_reloc_base(bool sort_relocs)1606   Output_data_reloc_base(bool sort_relocs)
1607     : Output_data_reloc_generic(size, sort_relocs)
1608   { }
1609 
1610  protected:
1611   // Write out the data.
1612   void
1613   do_write(Output_file*);
1614 
1615   // Generic implementation of do_write, allowing a customized
1616   // class for writing the output relocation (e.g., for MIPS-64).
1617   template<class Output_reloc_writer>
1618   void
do_write_generic(Output_file * of)1619   do_write_generic(Output_file* of)
1620   {
1621     const off_t off = this->offset();
1622     const off_t oview_size = this->data_size();
1623     unsigned char* const oview = of->get_output_view(off, oview_size);
1624 
1625     if (this->sort_relocs())
1626       {
1627 	gold_assert(dynamic);
1628 	std::sort(this->relocs_.begin(), this->relocs_.end(),
1629 		  Sort_relocs_comparison());
1630       }
1631 
1632     unsigned char* pov = oview;
1633     for (typename Relocs::const_iterator p = this->relocs_.begin();
1634 	 p != this->relocs_.end();
1635 	 ++p)
1636       {
1637 	Output_reloc_writer::write(p, pov);
1638 	pov += reloc_size;
1639       }
1640 
1641     gold_assert(pov - oview == oview_size);
1642 
1643     of->write_output_view(off, oview_size, oview);
1644 
1645     // We no longer need the relocation entries.
1646     this->relocs_.clear();
1647   }
1648 
1649   // Set the entry size and the link.
1650   void
1651   do_adjust_output_section(Output_section* os);
1652 
1653   // Write to a map file.
1654   void
do_print_to_mapfile(Mapfile * mapfile)1655   do_print_to_mapfile(Mapfile* mapfile) const
1656   {
1657     mapfile->print_output_data(this,
1658 			       (dynamic
1659 				? _("** dynamic relocs")
1660 				: _("** relocs")));
1661   }
1662 
1663   // Add a relocation entry.
1664   void
add(Output_data * od,const Output_reloc_type & reloc)1665   add(Output_data* od, const Output_reloc_type& reloc)
1666   {
1667     this->relocs_.push_back(reloc);
1668     this->set_current_data_size(this->relocs_.size() * reloc_size);
1669     if (dynamic)
1670       od->add_dynamic_reloc();
1671     if (reloc.is_relative())
1672       this->bump_relative_reloc_count();
1673     Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1674     if (relobj != NULL)
1675       relobj->add_dyn_reloc(this->relocs_.size() - 1);
1676   }
1677 
1678  private:
1679   typedef std::vector<Output_reloc_type> Relocs;
1680 
1681   // The class used to sort the relocations.
1682   struct Sort_relocs_comparison
1683   {
1684     bool
operatorSort_relocs_comparison1685     operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1686     { return r1.sort_before(r2); }
1687   };
1688 
1689   // The relocations in this section.
1690   Relocs relocs_;
1691 };
1692 
1693 // The class which callers actually create.
1694 
1695 template<int sh_type, bool dynamic, int size, bool big_endian>
1696 class Output_data_reloc;
1697 
1698 // The SHT_REL version of Output_data_reloc.
1699 
1700 template<bool dynamic, int size, bool big_endian>
1701 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1702   : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1703 {
1704  private:
1705   typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1706 				 big_endian> Base;
1707 
1708  public:
1709   typedef typename Base::Output_reloc_type Output_reloc_type;
1710   typedef typename Output_reloc_type::Address Address;
1711 
Output_data_reloc(bool sr)1712   Output_data_reloc(bool sr)
1713     : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1714   { }
1715 
1716   // Add a reloc against a global symbol.
1717 
1718   void
add_global(Symbol * gsym,unsigned int type,Output_data * od,Address address)1719   add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1720   {
1721     this->add(od, Output_reloc_type(gsym, type, od, address,
1722 				    false, false, false));
1723   }
1724 
1725   void
add_global(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1726   add_global(Symbol* gsym, unsigned int type, Output_data* od,
1727 	     Sized_relobj<size, big_endian>* relobj,
1728 	     unsigned int shndx, Address address)
1729   {
1730     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1731 				    false, false, false));
1732   }
1733 
1734   void
add_global_generic(Symbol * gsym,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)1735   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1736 		     uint64_t address, uint64_t addend)
1737   {
1738     gold_assert(addend == 0);
1739     this->add(od, Output_reloc_type(gsym, type, od,
1740 				    convert_types<Address, uint64_t>(address),
1741 				    false, false, false));
1742   }
1743 
1744   void
add_global_generic(Symbol * gsym,unsigned int type,Output_data * od,Relobj * relobj,unsigned int shndx,uint64_t address,uint64_t addend)1745   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1746 		     Relobj* relobj, unsigned int shndx, uint64_t address,
1747 		     uint64_t addend)
1748   {
1749     gold_assert(addend == 0);
1750     Sized_relobj<size, big_endian>* sized_relobj =
1751       static_cast<Sized_relobj<size, big_endian>*>(relobj);
1752     this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1753 				    convert_types<Address, uint64_t>(address),
1754 				    false, false, false));
1755   }
1756 
1757   // Add a RELATIVE reloc against a global symbol.  The final relocation
1758   // will not reference the symbol.
1759 
1760   void
add_global_relative(Symbol * gsym,unsigned int type,Output_data * od,Address address)1761   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1762 		      Address address)
1763   {
1764     this->add(od, Output_reloc_type(gsym, type, od, address, true, true,
1765 				    false));
1766   }
1767 
1768   void
add_global_relative(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1769   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1770 		      Sized_relobj<size, big_endian>* relobj,
1771 		      unsigned int shndx, Address address)
1772   {
1773     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1774 				    true, true, false));
1775   }
1776 
1777   // Add a global relocation which does not use a symbol for the relocation,
1778   // but which gets its addend from a symbol.
1779 
1780   void
add_symbolless_global_addend(Symbol * gsym,unsigned int type,Output_data * od,Address address)1781   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1782 			       Output_data* od, Address address)
1783   {
1784     this->add(od, Output_reloc_type(gsym, type, od, address, false, true,
1785 				    false));
1786   }
1787 
1788   void
add_symbolless_global_addend(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1789   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1790 			       Output_data* od,
1791 			       Sized_relobj<size, big_endian>* relobj,
1792 			       unsigned int shndx, Address address)
1793   {
1794     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1795 				    false, true, false));
1796   }
1797 
1798   // Add a reloc against a local symbol.
1799 
1800   void
add_local(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address)1801   add_local(Sized_relobj<size, big_endian>* relobj,
1802 	    unsigned int local_sym_index, unsigned int type,
1803 	    Output_data* od, Address address)
1804   {
1805     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1806 				    address, false, false, false, false));
1807   }
1808 
1809   void
add_local(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address)1810   add_local(Sized_relobj<size, big_endian>* relobj,
1811 	    unsigned int local_sym_index, unsigned int type,
1812 	    Output_data* od, unsigned int shndx, Address address)
1813   {
1814     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1815 				    address, false, false, false, false));
1816   }
1817 
1818   void
add_local_generic(Relobj * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)1819   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1820 		    unsigned int type, Output_data* od, uint64_t address,
1821 		    uint64_t addend)
1822   {
1823     gold_assert(addend == 0);
1824     Sized_relobj<size, big_endian>* sized_relobj =
1825       static_cast<Sized_relobj<size, big_endian> *>(relobj);
1826     this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1827 				    convert_types<Address, uint64_t>(address),
1828 				    false, false, false, false));
1829   }
1830 
1831   void
add_local_generic(Relobj * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,uint64_t address,uint64_t addend)1832   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1833 		    unsigned int type, Output_data* od, unsigned int shndx,
1834 		    uint64_t address, uint64_t addend)
1835   {
1836     gold_assert(addend == 0);
1837     Sized_relobj<size, big_endian>* sized_relobj =
1838       static_cast<Sized_relobj<size, big_endian>*>(relobj);
1839     this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1840 				    convert_types<Address, uint64_t>(address),
1841 				    false, false, false, false));
1842   }
1843 
1844   // Add a RELATIVE reloc against a local symbol.
1845 
1846   void
add_local_relative(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address)1847   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1848 		     unsigned int local_sym_index, unsigned int type,
1849 		     Output_data* od, Address address)
1850   {
1851     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1852 				    address, true, true, false, false));
1853   }
1854 
1855   void
add_local_relative(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address)1856   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1857 		     unsigned int local_sym_index, unsigned int type,
1858 		     Output_data* od, unsigned int shndx, Address address)
1859   {
1860     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1861 				    address, true, true, false, false));
1862   }
1863 
1864   void
add_local_relative(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address,bool use_plt_offset)1865   add_local_relative(Sized_relobj<size, big_endian>* relobj,
1866 		     unsigned int local_sym_index, unsigned int type,
1867 		     Output_data* od, unsigned int shndx, Address address,
1868 		     bool use_plt_offset)
1869   {
1870     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1871 				    address, true, true, false,
1872 				    use_plt_offset));
1873   }
1874 
1875   // Add a local relocation which does not use a symbol for the relocation,
1876   // but which gets its addend from a symbol.
1877 
1878   void
add_symbolless_local_addend(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address)1879   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1880 			      unsigned int local_sym_index, unsigned int type,
1881 			      Output_data* od, Address address)
1882   {
1883     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1884 				    address, false, true, false, false));
1885   }
1886 
1887   void
add_symbolless_local_addend(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address)1888   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1889 			      unsigned int local_sym_index, unsigned int type,
1890 			      Output_data* od, unsigned int shndx,
1891 			      Address address)
1892   {
1893     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1894 				    address, false, true, false, false));
1895   }
1896 
1897   // Add a reloc against a local section symbol.  This will be
1898   // converted into a reloc against the STT_SECTION symbol of the
1899   // output section.
1900 
1901   void
add_local_section(Sized_relobj<size,big_endian> * relobj,unsigned int input_shndx,unsigned int type,Output_data * od,Address address)1902   add_local_section(Sized_relobj<size, big_endian>* relobj,
1903 		    unsigned int input_shndx, unsigned int type,
1904 		    Output_data* od, Address address)
1905   {
1906     this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1907 				    address, false, false, true, false));
1908   }
1909 
1910   void
add_local_section(Sized_relobj<size,big_endian> * relobj,unsigned int input_shndx,unsigned int type,Output_data * od,unsigned int shndx,Address address)1911   add_local_section(Sized_relobj<size, big_endian>* relobj,
1912 		    unsigned int input_shndx, unsigned int type,
1913 		    Output_data* od, unsigned int shndx, Address address)
1914   {
1915     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1916 				    address, false, false, true, false));
1917   }
1918 
1919   // A reloc against the STT_SECTION symbol of an output section.
1920   // OS is the Output_section that the relocation refers to; OD is
1921   // the Output_data object being relocated.
1922 
1923   void
add_output_section(Output_section * os,unsigned int type,Output_data * od,Address address)1924   add_output_section(Output_section* os, unsigned int type,
1925 		     Output_data* od, Address address)
1926   { this->add(od, Output_reloc_type(os, type, od, address, false)); }
1927 
1928   void
add_output_section(Output_section * os,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1929   add_output_section(Output_section* os, unsigned int type, Output_data* od,
1930 		     Sized_relobj<size, big_endian>* relobj,
1931 		     unsigned int shndx, Address address)
1932   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, false)); }
1933 
1934   void
add_output_section_generic(Output_section * os,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)1935   add_output_section_generic(Output_section* os, unsigned int type,
1936 			     Output_data* od, uint64_t address,
1937 			     uint64_t addend)
1938   {
1939     gold_assert(addend == 0);
1940     this->add(od, Output_reloc_type(os, type, od,
1941 				    convert_types<Address, uint64_t>(address),
1942 				    false));
1943   }
1944 
1945   void
add_output_section_generic(Output_section * os,unsigned int type,Output_data * od,Relobj * relobj,unsigned int shndx,uint64_t address,uint64_t addend)1946   add_output_section_generic(Output_section* os, unsigned int type,
1947 			     Output_data* od, Relobj* relobj,
1948 			     unsigned int shndx, uint64_t address,
1949 			     uint64_t addend)
1950   {
1951     gold_assert(addend == 0);
1952     Sized_relobj<size, big_endian>* sized_relobj =
1953       static_cast<Sized_relobj<size, big_endian>*>(relobj);
1954     this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
1955 				    convert_types<Address, uint64_t>(address),
1956 				    false));
1957   }
1958 
1959   // As above, but the reloc TYPE is relative
1960 
1961   void
add_output_section_relative(Output_section * os,unsigned int type,Output_data * od,Address address)1962   add_output_section_relative(Output_section* os, unsigned int type,
1963 			      Output_data* od, Address address)
1964   { this->add(od, Output_reloc_type(os, type, od, address, true)); }
1965 
1966   void
add_output_section_relative(Output_section * os,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1967   add_output_section_relative(Output_section* os, unsigned int type,
1968 			      Output_data* od,
1969 			      Sized_relobj<size, big_endian>* relobj,
1970 			      unsigned int shndx, Address address)
1971   { this->add(od, Output_reloc_type(os, type, relobj, shndx, address, true)); }
1972 
1973   // Add an absolute relocation.
1974 
1975   void
add_absolute(unsigned int type,Output_data * od,Address address)1976   add_absolute(unsigned int type, Output_data* od, Address address)
1977   { this->add(od, Output_reloc_type(type, od, address, false)); }
1978 
1979   void
add_absolute(unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1980   add_absolute(unsigned int type, Output_data* od,
1981 	       Sized_relobj<size, big_endian>* relobj,
1982 	       unsigned int shndx, Address address)
1983   { this->add(od, Output_reloc_type(type, relobj, shndx, address, false)); }
1984 
1985   // Add a relative relocation
1986 
1987   void
add_relative(unsigned int type,Output_data * od,Address address)1988   add_relative(unsigned int type, Output_data* od, Address address)
1989   { this->add(od, Output_reloc_type(type, od, address, true)); }
1990 
1991   void
add_relative(unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)1992   add_relative(unsigned int type, Output_data* od,
1993 	       Sized_relobj<size, big_endian>* relobj,
1994 	       unsigned int shndx, Address address)
1995   { this->add(od, Output_reloc_type(type, relobj, shndx, address, true)); }
1996 
1997   // Add a target specific relocation.  A target which calls this must
1998   // define the reloc_symbol_index and reloc_addend virtual functions.
1999 
2000   void
add_target_specific(unsigned int type,void * arg,Output_data * od,Address address)2001   add_target_specific(unsigned int type, void* arg, Output_data* od,
2002 		      Address address)
2003   { this->add(od, Output_reloc_type(type, arg, od, address)); }
2004 
2005   void
add_target_specific(unsigned int type,void * arg,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address)2006   add_target_specific(unsigned int type, void* arg, Output_data* od,
2007 		      Sized_relobj<size, big_endian>* relobj,
2008 		      unsigned int shndx, Address address)
2009   { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
2010 };
2011 
2012 // The SHT_RELA version of Output_data_reloc.
2013 
2014 template<bool dynamic, int size, bool big_endian>
2015 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
2016   : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
2017 {
2018  private:
2019   typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
2020 				 big_endian> Base;
2021 
2022  public:
2023   typedef typename Base::Output_reloc_type Output_reloc_type;
2024   typedef typename Output_reloc_type::Address Address;
2025   typedef typename Output_reloc_type::Addend Addend;
2026 
Output_data_reloc(bool sr)2027   Output_data_reloc(bool sr)
2028     : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
2029   { }
2030 
2031   // Add a reloc against a global symbol.
2032 
2033   void
add_global(Symbol * gsym,unsigned int type,Output_data * od,Address address,Addend addend)2034   add_global(Symbol* gsym, unsigned int type, Output_data* od,
2035 	     Address address, Addend addend)
2036   {
2037     this->add(od, Output_reloc_type(gsym, type, od, address, addend,
2038 				    false, false, false));
2039   }
2040 
2041   void
add_global(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2042   add_global(Symbol* gsym, unsigned int type, Output_data* od,
2043 	     Sized_relobj<size, big_endian>* relobj,
2044 	     unsigned int shndx, Address address,
2045 	     Addend addend)
2046   {
2047     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
2048 				    addend, false, false, false));
2049   }
2050 
2051   void
add_global_generic(Symbol * gsym,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)2052   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
2053 		     uint64_t address, uint64_t addend)
2054   {
2055     this->add(od, Output_reloc_type(gsym, type, od,
2056 				    convert_types<Address, uint64_t>(address),
2057 				    convert_types<Addend, uint64_t>(addend),
2058 				    false, false, false));
2059   }
2060 
2061   void
add_global_generic(Symbol * gsym,unsigned int type,Output_data * od,Relobj * relobj,unsigned int shndx,uint64_t address,uint64_t addend)2062   add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
2063 		     Relobj* relobj, unsigned int shndx, uint64_t address,
2064 		     uint64_t addend)
2065   {
2066     Sized_relobj<size, big_endian>* sized_relobj =
2067       static_cast<Sized_relobj<size, big_endian>*>(relobj);
2068     this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
2069 				    convert_types<Address, uint64_t>(address),
2070 				    convert_types<Addend, uint64_t>(addend),
2071 				    false, false, false));
2072   }
2073 
2074   // Add a RELATIVE reloc against a global symbol.  The final output
2075   // relocation will not reference the symbol, but we must keep the symbol
2076   // information long enough to set the addend of the relocation correctly
2077   // when it is written.
2078 
2079   void
add_global_relative(Symbol * gsym,unsigned int type,Output_data * od,Address address,Addend addend,bool use_plt_offset)2080   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
2081 		      Address address, Addend addend, bool use_plt_offset)
2082   {
2083     this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
2084 				    true, use_plt_offset));
2085   }
2086 
2087   void
add_global_relative(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend,bool use_plt_offset)2088   add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
2089 		      Sized_relobj<size, big_endian>* relobj,
2090 		      unsigned int shndx, Address address, Addend addend,
2091 		      bool use_plt_offset)
2092   {
2093     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
2094 				    addend, true, true, use_plt_offset));
2095   }
2096 
2097   // Add a global relocation which does not use a symbol for the relocation,
2098   // but which gets its addend from a symbol.
2099 
2100   void
add_symbolless_global_addend(Symbol * gsym,unsigned int type,Output_data * od,Address address,Addend addend)2101   add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
2102 			       Address address, Addend addend)
2103   {
2104     this->add(od, Output_reloc_type(gsym, type, od, address, addend,
2105 				    false, true, false));
2106   }
2107 
2108   void
add_symbolless_global_addend(Symbol * gsym,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2109   add_symbolless_global_addend(Symbol* gsym, unsigned int type,
2110 			       Output_data* od,
2111 			       Sized_relobj<size, big_endian>* relobj,
2112 			       unsigned int shndx, Address address,
2113 			       Addend addend)
2114   {
2115     this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
2116 				    addend, false, true, false));
2117   }
2118 
2119   // Add a reloc against a local symbol.
2120 
2121   void
add_local(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address,Addend addend)2122   add_local(Sized_relobj<size, big_endian>* relobj,
2123 	    unsigned int local_sym_index, unsigned int type,
2124 	    Output_data* od, Address address, Addend addend)
2125   {
2126     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2127 				    addend, false, false, false, false));
2128   }
2129 
2130   void
add_local(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address,Addend addend)2131   add_local(Sized_relobj<size, big_endian>* relobj,
2132 	    unsigned int local_sym_index, unsigned int type,
2133 	    Output_data* od, unsigned int shndx, Address address,
2134 	    Addend addend)
2135   {
2136     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2137 				    address, addend, false, false, false,
2138 				    false));
2139   }
2140 
2141   void
add_local_generic(Relobj * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)2142   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
2143 		    unsigned int type, Output_data* od, uint64_t address,
2144 		    uint64_t addend)
2145   {
2146     Sized_relobj<size, big_endian>* sized_relobj =
2147       static_cast<Sized_relobj<size, big_endian> *>(relobj);
2148     this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
2149 				    convert_types<Address, uint64_t>(address),
2150 				    convert_types<Addend, uint64_t>(addend),
2151 				    false, false, false, false));
2152   }
2153 
2154   void
add_local_generic(Relobj * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,uint64_t address,uint64_t addend)2155   add_local_generic(Relobj* relobj, unsigned int local_sym_index,
2156 		    unsigned int type, Output_data* od, unsigned int shndx,
2157 		    uint64_t address, uint64_t addend)
2158   {
2159     Sized_relobj<size, big_endian>* sized_relobj =
2160       static_cast<Sized_relobj<size, big_endian>*>(relobj);
2161     this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
2162 				    convert_types<Address, uint64_t>(address),
2163 				    convert_types<Addend, uint64_t>(addend),
2164 				    false, false, false, false));
2165   }
2166 
2167   // Add a RELATIVE reloc against a local symbol.
2168 
2169   void
add_local_relative(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address,Addend addend,bool use_plt_offset)2170   add_local_relative(Sized_relobj<size, big_endian>* relobj,
2171 		     unsigned int local_sym_index, unsigned int type,
2172 		     Output_data* od, Address address, Addend addend,
2173 		     bool use_plt_offset)
2174   {
2175     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2176 				    addend, true, true, false,
2177 				    use_plt_offset));
2178   }
2179 
2180   void
add_local_relative(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address,Addend addend,bool use_plt_offset)2181   add_local_relative(Sized_relobj<size, big_endian>* relobj,
2182 		     unsigned int local_sym_index, unsigned int type,
2183 		     Output_data* od, unsigned int shndx, Address address,
2184 		     Addend addend, bool use_plt_offset)
2185   {
2186     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2187 				    address, addend, true, true, false,
2188 				    use_plt_offset));
2189   }
2190 
2191   // Add a local relocation which does not use a symbol for the relocation,
2192   // but which gets it's addend from a symbol.
2193 
2194   void
add_symbolless_local_addend(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,Address address,Addend addend)2195   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2196 			      unsigned int local_sym_index, unsigned int type,
2197 			      Output_data* od, Address address, Addend addend)
2198   {
2199     this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
2200 				    addend, false, true, false, false));
2201   }
2202 
2203   void
add_symbolless_local_addend(Sized_relobj<size,big_endian> * relobj,unsigned int local_sym_index,unsigned int type,Output_data * od,unsigned int shndx,Address address,Addend addend)2204   add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
2205 			      unsigned int local_sym_index, unsigned int type,
2206 			      Output_data* od, unsigned int shndx,
2207 			      Address address, Addend addend)
2208   {
2209     this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
2210 				    address, addend, false, true, false,
2211 				    false));
2212   }
2213 
2214   // Add a reloc against a local section symbol.  This will be
2215   // converted into a reloc against the STT_SECTION symbol of the
2216   // output section.
2217 
2218   void
add_local_section(Sized_relobj<size,big_endian> * relobj,unsigned int input_shndx,unsigned int type,Output_data * od,Address address,Addend addend)2219   add_local_section(Sized_relobj<size, big_endian>* relobj,
2220 		    unsigned int input_shndx, unsigned int type,
2221 		    Output_data* od, Address address, Addend addend)
2222   {
2223     this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
2224 				    addend, false, false, true, false));
2225   }
2226 
2227   void
add_local_section(Sized_relobj<size,big_endian> * relobj,unsigned int input_shndx,unsigned int type,Output_data * od,unsigned int shndx,Address address,Addend addend)2228   add_local_section(Sized_relobj<size, big_endian>* relobj,
2229 		    unsigned int input_shndx, unsigned int type,
2230 		    Output_data* od, unsigned int shndx, Address address,
2231 		    Addend addend)
2232   {
2233     this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
2234 				    address, addend, false, false, true,
2235 				    false));
2236   }
2237 
2238   // A reloc against the STT_SECTION symbol of an output section.
2239 
2240   void
add_output_section(Output_section * os,unsigned int type,Output_data * od,Address address,Addend addend)2241   add_output_section(Output_section* os, unsigned int type, Output_data* od,
2242 		     Address address, Addend addend)
2243   { this->add(od, Output_reloc_type(os, type, od, address, addend, false)); }
2244 
2245   void
add_output_section(Output_section * os,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2246   add_output_section(Output_section* os, unsigned int type, Output_data* od,
2247 		     Sized_relobj<size, big_endian>* relobj,
2248 		     unsigned int shndx, Address address, Addend addend)
2249   {
2250     this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
2251 				    addend, false));
2252   }
2253 
2254   void
add_output_section_generic(Output_section * os,unsigned int type,Output_data * od,uint64_t address,uint64_t addend)2255   add_output_section_generic(Output_section* os, unsigned int type,
2256 			     Output_data* od, uint64_t address,
2257 			     uint64_t addend)
2258   {
2259     this->add(od, Output_reloc_type(os, type, od,
2260 				    convert_types<Address, uint64_t>(address),
2261 				    convert_types<Addend, uint64_t>(addend),
2262 				    false));
2263   }
2264 
2265   void
add_output_section_generic(Output_section * os,unsigned int type,Output_data * od,Relobj * relobj,unsigned int shndx,uint64_t address,uint64_t addend)2266   add_output_section_generic(Output_section* os, unsigned int type,
2267 			     Output_data* od, Relobj* relobj,
2268 			     unsigned int shndx, uint64_t address,
2269 			     uint64_t addend)
2270   {
2271     Sized_relobj<size, big_endian>* sized_relobj =
2272       static_cast<Sized_relobj<size, big_endian>*>(relobj);
2273     this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
2274 				    convert_types<Address, uint64_t>(address),
2275 				    convert_types<Addend, uint64_t>(addend),
2276 				    false));
2277   }
2278 
2279   // As above, but the reloc TYPE is relative
2280 
2281   void
add_output_section_relative(Output_section * os,unsigned int type,Output_data * od,Address address,Addend addend)2282   add_output_section_relative(Output_section* os, unsigned int type,
2283 			      Output_data* od, Address address, Addend addend)
2284   { this->add(od, Output_reloc_type(os, type, od, address, addend, true)); }
2285 
2286   void
add_output_section_relative(Output_section * os,unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2287   add_output_section_relative(Output_section* os, unsigned int type,
2288 			      Output_data* od,
2289 			      Sized_relobj<size, big_endian>* relobj,
2290 			      unsigned int shndx, Address address,
2291 			      Addend addend)
2292   {
2293     this->add(od, Output_reloc_type(os, type, relobj, shndx,
2294 				    address, addend, true));
2295   }
2296 
2297   // Add an absolute relocation.
2298 
2299   void
add_absolute(unsigned int type,Output_data * od,Address address,Addend addend)2300   add_absolute(unsigned int type, Output_data* od, Address address,
2301 	       Addend addend)
2302   { this->add(od, Output_reloc_type(type, od, address, addend, false)); }
2303 
2304   void
add_absolute(unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2305   add_absolute(unsigned int type, Output_data* od,
2306 	       Sized_relobj<size, big_endian>* relobj,
2307 	       unsigned int shndx, Address address, Addend addend)
2308   {
2309     this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2310 				    false));
2311   }
2312 
2313   // Add a relative relocation
2314 
2315   void
add_relative(unsigned int type,Output_data * od,Address address,Addend addend)2316   add_relative(unsigned int type, Output_data* od, Address address,
2317 	       Addend addend)
2318   { this->add(od, Output_reloc_type(type, od, address, addend, true)); }
2319 
2320   void
add_relative(unsigned int type,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2321   add_relative(unsigned int type, Output_data* od,
2322 	       Sized_relobj<size, big_endian>* relobj,
2323 	       unsigned int shndx, Address address, Addend addend)
2324   {
2325     this->add(od, Output_reloc_type(type, relobj, shndx, address, addend,
2326 				    true));
2327   }
2328 
2329   // Add a target specific relocation.  A target which calls this must
2330   // define the reloc_symbol_index and reloc_addend virtual functions.
2331 
2332   void
add_target_specific(unsigned int type,void * arg,Output_data * od,Address address,Addend addend)2333   add_target_specific(unsigned int type, void* arg, Output_data* od,
2334 		      Address address, Addend addend)
2335   { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
2336 
2337   void
add_target_specific(unsigned int type,void * arg,Output_data * od,Sized_relobj<size,big_endian> * relobj,unsigned int shndx,Address address,Addend addend)2338   add_target_specific(unsigned int type, void* arg, Output_data* od,
2339 		      Sized_relobj<size, big_endian>* relobj,
2340 		      unsigned int shndx, Address address, Addend addend)
2341   {
2342     this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
2343 				    addend));
2344   }
2345 };
2346 
2347 // Output_relocatable_relocs represents a relocation section in a
2348 // relocatable link.  The actual data is written out in the target
2349 // hook relocate_relocs.  This just saves space for it.
2350 
2351 template<int sh_type, int size, bool big_endian>
2352 class Output_relocatable_relocs : public Output_section_data
2353 {
2354  public:
Output_relocatable_relocs(Relocatable_relocs * rr)2355   Output_relocatable_relocs(Relocatable_relocs* rr)
2356     : Output_section_data(Output_data::default_alignment_for_size(size)),
2357       rr_(rr)
2358   { }
2359 
2360   void
2361   set_final_data_size();
2362 
2363   // Write out the data.  There is nothing to do here.
2364   void
do_write(Output_file *)2365   do_write(Output_file*)
2366   { }
2367 
2368   // Write to a map file.
2369   void
do_print_to_mapfile(Mapfile * mapfile)2370   do_print_to_mapfile(Mapfile* mapfile) const
2371   { mapfile->print_output_data(this, _("** relocs")); }
2372 
2373  private:
2374   // The relocs associated with this input section.
2375   Relocatable_relocs* rr_;
2376 };
2377 
2378 // Handle a GROUP section.
2379 
2380 template<int size, bool big_endian>
2381 class Output_data_group : public Output_section_data
2382 {
2383  public:
2384   // The constructor clears *INPUT_SHNDXES.
2385   Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
2386 		    section_size_type entry_count,
2387 		    elfcpp::Elf_Word flags,
2388 		    std::vector<unsigned int>* input_shndxes);
2389 
2390   void
2391   do_write(Output_file*);
2392 
2393   // Write to a map file.
2394   void
do_print_to_mapfile(Mapfile * mapfile)2395   do_print_to_mapfile(Mapfile* mapfile) const
2396   { mapfile->print_output_data(this, _("** group")); }
2397 
2398   // Set final data size.
2399   void
set_final_data_size()2400   set_final_data_size()
2401   { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
2402 
2403  private:
2404   // The input object.
2405   Sized_relobj_file<size, big_endian>* relobj_;
2406   // The group flag word.
2407   elfcpp::Elf_Word flags_;
2408   // The section indexes of the input sections in this group.
2409   std::vector<unsigned int> input_shndxes_;
2410 };
2411 
2412 // Output_data_got is used to manage a GOT.  Each entry in the GOT is
2413 // for one symbol--either a global symbol or a local symbol in an
2414 // object.  The target specific code adds entries to the GOT as
2415 // needed.  The GOT_SIZE template parameter is the size in bits of a
2416 // GOT entry, typically 32 or 64.
2417 
2418 class Output_data_got_base : public Output_section_data_build
2419 {
2420  public:
Output_data_got_base(uint64_t align)2421   Output_data_got_base(uint64_t align)
2422     : Output_section_data_build(align)
2423   { }
2424 
Output_data_got_base(off_t data_size,uint64_t align)2425   Output_data_got_base(off_t data_size, uint64_t align)
2426     : Output_section_data_build(data_size, align)
2427   { }
2428 
2429   // Reserve the slot at index I in the GOT.
2430   void
reserve_slot(unsigned int i)2431   reserve_slot(unsigned int i)
2432   { this->do_reserve_slot(i); }
2433 
2434  protected:
2435   // Reserve the slot at index I in the GOT.
2436   virtual void
2437   do_reserve_slot(unsigned int i) = 0;
2438 };
2439 
2440 template<int got_size, bool big_endian>
2441 class Output_data_got : public Output_data_got_base
2442 {
2443  public:
2444   typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
2445 
Output_data_got()2446   Output_data_got()
2447     : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
2448       entries_(), free_list_()
2449   { }
2450 
Output_data_got(off_t data_size)2451   Output_data_got(off_t data_size)
2452     : Output_data_got_base(data_size,
2453 			   Output_data::default_alignment_for_size(got_size)),
2454       entries_(), free_list_()
2455   {
2456     // For an incremental update, we have an existing GOT section.
2457     // Initialize the list of entries and the free list.
2458     this->entries_.resize(data_size / (got_size / 8));
2459     this->free_list_.init(data_size, false);
2460   }
2461 
2462   // Add an entry for a global symbol GSYM plus ADDEND to the GOT.
2463   // Return true if this is a new GOT entry, false if the symbol plus
2464   // addend was already in the GOT.
2465   bool
2466   add_global(Symbol* gsym, unsigned int got_type, uint64_t addend = 0);
2467 
2468   // Like add_global, but use the PLT offset of the global symbol if
2469   // it has one.
2470   bool
2471   add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend = 0);
2472 
2473   // Like add_global, but for a TLS symbol where the value will be
2474   // offset using Target::tls_offset_for_global.
2475   bool
2476   add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend = 0)
2477   { return this->add_global_plt(gsym, got_type, addend); }
2478 
2479   // Add an entry for a global symbol GSYM plus ADDEND to the GOT, and
2480   // add a dynamic relocation of type R_TYPE for the GOT entry.
2481   void
2482   add_global_with_rel(Symbol* gsym, unsigned int got_type,
2483 		      Output_data_reloc_generic* rel_dyn, unsigned int r_type,
2484 		      uint64_t addend = 0);
2485 
2486   // Add a pair of entries for a global symbol GSYM plus ADDEND to the
2487   // GOT, and add dynamic relocations of type R_TYPE_1 and R_TYPE_2,
2488   // respectively.
2489   void
2490   add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2491 			   Output_data_reloc_generic* rel_dyn,
2492 			   unsigned int r_type_1, unsigned int r_type_2,
2493 			   uint64_t addend = 0);
2494 
2495   // Add an entry for a local symbol plus ADDEND to the GOT.  This returns
2496   // true if this is a new GOT entry, false if the symbol already has a GOT
2497   // entry.
2498   bool
2499   add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
2500 	    uint64_t addend = 0);
2501 
2502   // Like add_local, but use the PLT offset of the local symbol if it
2503   // has one.
2504   bool
2505   add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type,
2506 		uint64_t addend = 0);
2507 
2508   // Like add_local, but for a TLS symbol where the value will be
2509   // offset using Target::tls_offset_for_local.
2510   bool
2511   add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type,
2512 		uint64_t addend = 0)
2513   { return this->add_local_plt(object, sym_index, got_type, addend); }
2514 
2515   // Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
2516   // relocation of type R_TYPE for the GOT entry.
2517   void
2518   add_local_with_rel(Relobj* object, unsigned int sym_index,
2519 		     unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2520 		     unsigned int r_type, uint64_t addend = 0);
2521 
2522   // Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
2523   // a dynamic relocation of type R_TYPE using the section symbol of
2524   // the output section to which input section SHNDX maps, on the first.
2525   // The first got entry will have a value of zero, the second the
2526   // value of the local symbol.
2527   void
2528   add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2529 			  unsigned int shndx, unsigned int got_type,
2530 			  Output_data_reloc_generic* rel_dyn,
2531 			  unsigned int r_type, uint64_t addend = 0);
2532 
2533   // Add a pair of entries for a local symbol plus ADDEND to the GOT,
2534   // and add a dynamic relocation of type R_TYPE using STN_UNDEF on
2535   // the first.  The first got entry will have a value of zero, the
2536   // second the value of the local symbol plus ADDEND offset by
2537   // Target::tls_offset_for_local.
2538   void
2539   add_local_tls_pair(Relobj* object, unsigned int sym_index,
2540 		     unsigned int got_type,
2541 		     Output_data_reloc_generic* rel_dyn,
2542 		     unsigned int r_type, uint64_t addend = 0);
2543 
2544   // Add a constant to the GOT.  This returns the offset of the new
2545   // entry from the start of the GOT.
2546   unsigned int
add_constant(Valtype constant)2547   add_constant(Valtype constant)
2548   { return this->add_got_entry(Got_entry(constant)); }
2549 
2550   // Add a pair of constants to the GOT.  This returns the offset of
2551   // the new entry from the start of the GOT.
2552   unsigned int
add_constant_pair(Valtype c1,Valtype c2)2553   add_constant_pair(Valtype c1, Valtype c2)
2554   { return this->add_got_entry_pair(Got_entry(c1), Got_entry(c2)); }
2555 
2556   // Replace GOT entry I with a new constant.
2557   void
replace_constant(unsigned int i,Valtype constant)2558   replace_constant(unsigned int i, Valtype constant)
2559   {
2560     this->replace_got_entry(i, Got_entry(constant));
2561   }
2562 
2563   // Reserve a slot in the GOT for a local symbol plus ADDEND.
2564   void
2565   reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2566 		unsigned int got_type, uint64_t addend = 0);
2567 
2568   // Reserve a slot in the GOT for a global symbol plus ADDEND.
2569   void
2570   reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type,
2571 		 uint64_t addend = 0);
2572 
2573  protected:
2574   // Write out the GOT table.
2575   void
2576   do_write(Output_file*);
2577 
2578   // Write to a map file.
2579   void
do_print_to_mapfile(Mapfile * mapfile)2580   do_print_to_mapfile(Mapfile* mapfile) const
2581   { mapfile->print_output_data(this, _("** GOT")); }
2582 
2583   // Reserve the slot at index I in the GOT.
2584   virtual void
do_reserve_slot(unsigned int i)2585   do_reserve_slot(unsigned int i)
2586   { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2587 
2588   // Return the number of words in the GOT.
2589   unsigned int
num_entries()2590   num_entries () const
2591   { return this->entries_.size(); }
2592 
2593   // Return the offset into the GOT of GOT entry I.
2594   unsigned int
got_offset(unsigned int i)2595   got_offset(unsigned int i) const
2596   { return i * (got_size / 8); }
2597 
2598  private:
2599   // This POD class holds a single GOT entry.
2600   class Got_entry
2601   {
2602    public:
2603     // Create a zero entry.
Got_entry()2604     Got_entry()
2605       : local_sym_index_(RESERVED_CODE), use_plt_or_tls_offset_(false),
2606 	addend_(0)
2607     { this->u_.constant = 0; }
2608 
2609     // Create a global symbol entry.
Got_entry(Symbol * gsym,bool use_plt_or_tls_offset,uint64_t addend)2610     Got_entry(Symbol* gsym, bool use_plt_or_tls_offset, uint64_t addend)
2611       : local_sym_index_(GSYM_CODE),
2612 	use_plt_or_tls_offset_(use_plt_or_tls_offset), addend_(addend)
2613     { this->u_.gsym = gsym; }
2614 
2615     // Create a local symbol entry.
Got_entry(Relobj * object,unsigned int local_sym_index,bool use_plt_or_tls_offset,uint64_t addend)2616     Got_entry(Relobj* object, unsigned int local_sym_index,
2617 	      bool use_plt_or_tls_offset, uint64_t addend)
2618       : local_sym_index_(local_sym_index),
2619 	use_plt_or_tls_offset_(use_plt_or_tls_offset), addend_(addend)
2620     {
2621       gold_assert(local_sym_index != GSYM_CODE
2622 		  && local_sym_index != CONSTANT_CODE
2623 		  && local_sym_index != RESERVED_CODE
2624 		  && local_sym_index == this->local_sym_index_);
2625       this->u_.object = object;
2626     }
2627 
2628     // Create a constant entry.  The constant is a host value--it will
2629     // be swapped, if necessary, when it is written out.
Got_entry(Valtype constant)2630     explicit Got_entry(Valtype constant)
2631       : local_sym_index_(CONSTANT_CODE), use_plt_or_tls_offset_(false)
2632     { this->u_.constant = constant; }
2633 
2634     // Write the GOT entry to an output view.
2635     void
2636     write(Output_data_got_base* got, unsigned int got_indx,
2637 	  unsigned char* pov) const;
2638 
2639    private:
2640     enum
2641     {
2642       GSYM_CODE = 0x7fffffff,
2643       CONSTANT_CODE = 0x7ffffffe,
2644       RESERVED_CODE = 0x7ffffffd
2645     };
2646 
2647     union
2648     {
2649       // For a local symbol, the object.
2650       Relobj* object;
2651       // For a global symbol, the symbol.
2652       Symbol* gsym;
2653       // For a constant, the constant.
2654       Valtype constant;
2655     } u_;
2656     // For a local symbol, the local symbol index.  This is GSYM_CODE
2657     // for a global symbol, or CONSTANT_CODE for a constant.
2658     unsigned int local_sym_index_ : 31;
2659     // Whether to use the PLT offset of the symbol if it has one.
2660     // For TLS symbols, whether to offset the symbol value.
2661     bool use_plt_or_tls_offset_ : 1;
2662     // The addend.
2663     uint64_t addend_;
2664   };
2665 
2666   typedef std::vector<Got_entry> Got_entries;
2667 
2668   // Create a new GOT entry and return its offset.
2669   unsigned int
2670   add_got_entry(Got_entry got_entry);
2671 
2672   // Create a pair of new GOT entries and return the offset of the first.
2673   unsigned int
2674   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2675 
2676   // Replace GOT entry I with a new value.
2677   void
2678   replace_got_entry(unsigned int i, Got_entry got_entry);
2679 
2680   // Return the offset into the GOT of the last entry added.
2681   unsigned int
last_got_offset()2682   last_got_offset() const
2683   { return this->got_offset(this->num_entries() - 1); }
2684 
2685   // Set the size of the section.
2686   void
set_got_size()2687   set_got_size()
2688   { this->set_current_data_size(this->got_offset(this->num_entries())); }
2689 
2690   // The list of GOT entries.
2691   Got_entries entries_;
2692 
2693   // List of available regions within the section, for incremental
2694   // update links.
2695   Free_list free_list_;
2696 };
2697 
2698 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2699 // section.
2700 
2701 class Output_data_dynamic : public Output_section_data
2702 {
2703  public:
Output_data_dynamic(Stringpool * pool)2704   Output_data_dynamic(Stringpool* pool)
2705     : Output_section_data(Output_data::default_alignment()),
2706       entries_(), pool_(pool)
2707   { }
2708 
2709   // Add a new dynamic entry with a fixed numeric value.
2710   void
add_constant(elfcpp::DT tag,unsigned int val)2711   add_constant(elfcpp::DT tag, unsigned int val)
2712   { this->add_entry(Dynamic_entry(tag, val)); }
2713 
2714   // Add a new dynamic entry with the address of output data.
2715   void
add_section_address(elfcpp::DT tag,const Output_data * od)2716   add_section_address(elfcpp::DT tag, const Output_data* od)
2717   { this->add_entry(Dynamic_entry(tag, od, false)); }
2718 
2719   // Add a new dynamic entry with the address of output data
2720   // plus a constant offset.
2721   void
add_section_plus_offset(elfcpp::DT tag,const Output_data * od,unsigned int offset)2722   add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2723 			  unsigned int offset)
2724   { this->add_entry(Dynamic_entry(tag, od, offset)); }
2725 
2726   // Add a new dynamic entry with the size of output data.
2727   void
add_section_size(elfcpp::DT tag,const Output_data * od)2728   add_section_size(elfcpp::DT tag, const Output_data* od)
2729   { this->add_entry(Dynamic_entry(tag, od, true)); }
2730 
2731   // Add a new dynamic entry with the total size of two output datas.
2732   void
add_section_size(elfcpp::DT tag,const Output_data * od,const Output_data * od2)2733   add_section_size(elfcpp::DT tag, const Output_data* od,
2734 		   const Output_data* od2)
2735   { this->add_entry(Dynamic_entry(tag, od, od2)); }
2736 
2737   // Add a new dynamic entry with the address of a symbol.
2738   void
add_symbol(elfcpp::DT tag,const Symbol * sym)2739   add_symbol(elfcpp::DT tag, const Symbol* sym)
2740   { this->add_entry(Dynamic_entry(tag, sym)); }
2741 
2742   // Add a new dynamic entry with a string.
2743   void
add_string(elfcpp::DT tag,const char * str)2744   add_string(elfcpp::DT tag, const char* str)
2745   { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2746 
2747   void
add_string(elfcpp::DT tag,const std::string & str)2748   add_string(elfcpp::DT tag, const std::string& str)
2749   { this->add_string(tag, str.c_str()); }
2750 
2751   // Add a new dynamic entry with custom value.
2752   void
add_custom(elfcpp::DT tag)2753   add_custom(elfcpp::DT tag)
2754   { this->add_entry(Dynamic_entry(tag)); }
2755 
2756   // Get a dynamic entry offset.
2757   unsigned int
2758   get_entry_offset(elfcpp::DT tag) const;
2759 
2760  protected:
2761   // Adjust the output section to set the entry size.
2762   void
2763   do_adjust_output_section(Output_section*);
2764 
2765   // Set the final data size.
2766   void
2767   set_final_data_size();
2768 
2769   // Write out the dynamic entries.
2770   void
2771   do_write(Output_file*);
2772 
2773   // Write to a map file.
2774   void
do_print_to_mapfile(Mapfile * mapfile)2775   do_print_to_mapfile(Mapfile* mapfile) const
2776   { mapfile->print_output_data(this, _("** dynamic")); }
2777 
2778  private:
2779   // This POD class holds a single dynamic entry.
2780   class Dynamic_entry
2781   {
2782    public:
2783     // Create an entry with a fixed numeric value.
Dynamic_entry(elfcpp::DT tag,unsigned int val)2784     Dynamic_entry(elfcpp::DT tag, unsigned int val)
2785       : tag_(tag), offset_(DYNAMIC_NUMBER)
2786     { this->u_.val = val; }
2787 
2788     // Create an entry with the size or address of a section.
Dynamic_entry(elfcpp::DT tag,const Output_data * od,bool section_size)2789     Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2790       : tag_(tag),
2791 	offset_(section_size
2792 		? DYNAMIC_SECTION_SIZE
2793 		: DYNAMIC_SECTION_ADDRESS)
2794     {
2795       this->u_.od = od;
2796       this->od2 = NULL;
2797     }
2798 
2799     // Create an entry with the size of two sections.
Dynamic_entry(elfcpp::DT tag,const Output_data * od,const Output_data * od2)2800     Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2801       : tag_(tag),
2802 	offset_(DYNAMIC_SECTION_SIZE)
2803     {
2804       this->u_.od = od;
2805       this->od2 = od2;
2806     }
2807 
2808     // Create an entry with the address of a section plus a constant offset.
Dynamic_entry(elfcpp::DT tag,const Output_data * od,unsigned int offset)2809     Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2810       : tag_(tag),
2811 	offset_(offset)
2812     { this->u_.od = od; }
2813 
2814     // Create an entry with the address of a symbol.
Dynamic_entry(elfcpp::DT tag,const Symbol * sym)2815     Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2816       : tag_(tag), offset_(DYNAMIC_SYMBOL)
2817     { this->u_.sym = sym; }
2818 
2819     // Create an entry with a string.
Dynamic_entry(elfcpp::DT tag,const char * str)2820     Dynamic_entry(elfcpp::DT tag, const char* str)
2821       : tag_(tag), offset_(DYNAMIC_STRING)
2822     { this->u_.str = str; }
2823 
2824     // Create an entry with a custom value.
Dynamic_entry(elfcpp::DT tag)2825     Dynamic_entry(elfcpp::DT tag)
2826       : tag_(tag), offset_(DYNAMIC_CUSTOM)
2827     { }
2828 
2829     // Return the tag of this entry.
2830     elfcpp::DT
tag()2831     tag() const
2832     { return this->tag_; }
2833 
2834     // Write the dynamic entry to an output view.
2835     template<int size, bool big_endian>
2836     void
2837     write(unsigned char* pov, const Stringpool*) const;
2838 
2839    private:
2840     // Classification is encoded in the OFFSET field.
2841     enum Classification
2842     {
2843       // Section address.
2844       DYNAMIC_SECTION_ADDRESS = 0,
2845       // Number.
2846       DYNAMIC_NUMBER = -1U,
2847       // Section size.
2848       DYNAMIC_SECTION_SIZE = -2U,
2849       // Symbol address.
2850       DYNAMIC_SYMBOL = -3U,
2851       // String.
2852       DYNAMIC_STRING = -4U,
2853       // Custom value.
2854       DYNAMIC_CUSTOM = -5U
2855       // Any other value indicates a section address plus OFFSET.
2856     };
2857 
2858     union
2859     {
2860       // For DYNAMIC_NUMBER.
2861       unsigned int val;
2862       // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2863       const Output_data* od;
2864       // For DYNAMIC_SYMBOL.
2865       const Symbol* sym;
2866       // For DYNAMIC_STRING.
2867       const char* str;
2868     } u_;
2869     // For DYNAMIC_SYMBOL with two sections.
2870     const Output_data* od2;
2871     // The dynamic tag.
2872     elfcpp::DT tag_;
2873     // The type of entry (Classification) or offset within a section.
2874     unsigned int offset_;
2875   };
2876 
2877   // Add an entry to the list.
2878   void
add_entry(const Dynamic_entry & entry)2879   add_entry(const Dynamic_entry& entry)
2880   { this->entries_.push_back(entry); }
2881 
2882   // Sized version of write function.
2883   template<int size, bool big_endian>
2884   void
2885   sized_write(Output_file* of);
2886 
2887   // The type of the list of entries.
2888   typedef std::vector<Dynamic_entry> Dynamic_entries;
2889 
2890   // The entries.
2891   Dynamic_entries entries_;
2892   // The pool used for strings.
2893   Stringpool* pool_;
2894 };
2895 
2896 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2897 // which may be required if the object file has more than
2898 // SHN_LORESERVE sections.
2899 
2900 class Output_symtab_xindex : public Output_section_data
2901 {
2902  public:
Output_symtab_xindex(size_t symcount)2903   Output_symtab_xindex(size_t symcount)
2904     : Output_section_data(symcount * 4, 4, true),
2905       entries_()
2906   { }
2907 
2908   // Add an entry: symbol number SYMNDX has section SHNDX.
2909   void
add(unsigned int symndx,unsigned int shndx)2910   add(unsigned int symndx, unsigned int shndx)
2911   { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2912 
2913  protected:
2914   void
2915   do_write(Output_file*);
2916 
2917   // Write to a map file.
2918   void
do_print_to_mapfile(Mapfile * mapfile)2919   do_print_to_mapfile(Mapfile* mapfile) const
2920   { mapfile->print_output_data(this, _("** symtab xindex")); }
2921 
2922  private:
2923   template<bool big_endian>
2924   void
2925   endian_do_write(unsigned char*);
2926 
2927   // It is likely that most symbols will not require entries.  Rather
2928   // than keep a vector for all symbols, we keep pairs of symbol index
2929   // and section index.
2930   typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2931 
2932   // The entries we need.
2933   Xindex_entries entries_;
2934 };
2935 
2936 // A relaxed input section.
2937 class Output_relaxed_input_section : public Output_section_data_build
2938 {
2939  public:
2940   // We would like to call relobj->section_addralign(shndx) to get the
2941   // alignment but we do not want the constructor to fail.  So callers
2942   // are repsonsible for ensuring that.
Output_relaxed_input_section(Relobj * relobj,unsigned int shndx,uint64_t addralign)2943   Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2944 			       uint64_t addralign)
2945     : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2946   { }
2947 
2948   // Return the Relobj of this relaxed input section.
2949   Relobj*
relobj()2950   relobj() const
2951   { return this->relobj_; }
2952 
2953   // Return the section index of this relaxed input section.
2954   unsigned int
shndx()2955   shndx() const
2956   { return this->shndx_; }
2957 
2958  protected:
2959   void
set_relobj(Relobj * relobj)2960   set_relobj(Relobj* relobj)
2961   { this->relobj_ = relobj; }
2962 
2963   void
set_shndx(unsigned int shndx)2964   set_shndx(unsigned int shndx)
2965   { this->shndx_ = shndx; }
2966 
2967  private:
2968   Relobj* relobj_;
2969   unsigned int shndx_;
2970 };
2971 
2972 // This class describes properties of merge data sections.  It is used
2973 // as a key type for maps.
2974 class Merge_section_properties
2975 {
2976  public:
Merge_section_properties(bool is_string,uint64_t entsize,uint64_t addralign)2977   Merge_section_properties(bool is_string, uint64_t entsize,
2978 			     uint64_t addralign)
2979     : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2980   { }
2981 
2982   // Whether this equals to another Merge_section_properties MSP.
2983   bool
eq(const Merge_section_properties & msp)2984   eq(const Merge_section_properties& msp) const
2985   {
2986     return ((this->is_string_ == msp.is_string_)
2987 	    && (this->entsize_ == msp.entsize_)
2988 	    && (this->addralign_ == msp.addralign_));
2989   }
2990 
2991   // Compute a hash value for this using 64-bit FNV-1a hash.
2992   size_t
hash_value()2993   hash_value() const
2994   {
2995     uint64_t h = 14695981039346656037ULL;	// FNV offset basis.
2996     uint64_t prime = 1099511628211ULL;
2997     h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2998     h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2999     h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3000     return h;
3001   }
3002 
3003   // Functors for associative containers.
3004   struct equal_to
3005   {
3006     bool
operatorequal_to3007     operator()(const Merge_section_properties& msp1,
3008 	       const Merge_section_properties& msp2) const
3009     { return msp1.eq(msp2); }
3010   };
3011 
3012   struct hash
3013   {
3014     size_t
operatorhash3015     operator()(const Merge_section_properties& msp) const
3016     { return msp.hash_value(); }
3017   };
3018 
3019  private:
3020   // Whether this merge data section is for strings.
3021   bool is_string_;
3022   // Entsize of this merge data section.
3023   uint64_t entsize_;
3024   // Address alignment.
3025   uint64_t addralign_;
3026 };
3027 
3028 // This class is used to speed up look up of special input sections in an
3029 // Output_section.
3030 
3031 class Output_section_lookup_maps
3032 {
3033  public:
Output_section_lookup_maps()3034   Output_section_lookup_maps()
3035     : is_valid_(true), merge_sections_by_properties_(),
3036       relaxed_input_sections_by_id_()
3037   { }
3038 
3039   // Whether the maps are valid.
3040   bool
is_valid()3041   is_valid() const
3042   { return this->is_valid_; }
3043 
3044   // Invalidate the maps.
3045   void
invalidate()3046   invalidate()
3047   { this->is_valid_ = false; }
3048 
3049   // Clear the maps.
3050   void
clear()3051   clear()
3052   {
3053     this->merge_sections_by_properties_.clear();
3054     this->relaxed_input_sections_by_id_.clear();
3055     // A cleared map is valid.
3056     this->is_valid_ = true;
3057   }
3058 
3059   // Find a merge section by merge section properties.  Return NULL if none
3060   // is found.
3061   Output_merge_base*
find_merge_section(const Merge_section_properties & msp)3062   find_merge_section(const Merge_section_properties& msp) const
3063   {
3064     gold_assert(this->is_valid_);
3065     Merge_sections_by_properties::const_iterator p =
3066       this->merge_sections_by_properties_.find(msp);
3067     return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
3068   }
3069 
3070   // Add a merge section pointed by POMB with properties MSP.
3071   void
add_merge_section(const Merge_section_properties & msp,Output_merge_base * pomb)3072   add_merge_section(const Merge_section_properties& msp,
3073 		    Output_merge_base* pomb)
3074   {
3075     std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
3076     std::pair<Merge_sections_by_properties::iterator, bool> result =
3077       this->merge_sections_by_properties_.insert(value);
3078     gold_assert(result.second);
3079   }
3080 
3081   // Find a relaxed input section of OBJECT with index SHNDX.
3082   Output_relaxed_input_section*
find_relaxed_input_section(const Relobj * object,unsigned int shndx)3083   find_relaxed_input_section(const Relobj* object, unsigned int shndx) const
3084   {
3085     gold_assert(this->is_valid_);
3086     Relaxed_input_sections_by_id::const_iterator p =
3087       this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
3088     return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
3089   }
3090 
3091   // Add a relaxed input section pointed by POMB and whose original input
3092   // section is in OBJECT with index SHNDX.
3093   void
add_relaxed_input_section(const Relobj * relobj,unsigned int shndx,Output_relaxed_input_section * poris)3094   add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
3095 			    Output_relaxed_input_section* poris)
3096   {
3097     Const_section_id csid(relobj, shndx);
3098     std::pair<Const_section_id, Output_relaxed_input_section*>
3099       value(csid, poris);
3100     std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
3101       this->relaxed_input_sections_by_id_.insert(value);
3102     gold_assert(result.second);
3103   }
3104 
3105  private:
3106   typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3107 			Merge_section_properties::hash,
3108 			Merge_section_properties::equal_to>
3109     Merge_sections_by_properties;
3110 
3111   typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
3112 			Const_section_id_hash>
3113     Relaxed_input_sections_by_id;
3114 
3115   // Whether this is valid
3116   bool is_valid_;
3117   // Merge sections by merge section properties.
3118   Merge_sections_by_properties merge_sections_by_properties_;
3119   // Relaxed sections by section IDs.
3120   Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
3121 };
3122 
3123 // This abstract base class defines the interface for the
3124 // types of methods used to fill free space left in an output
3125 // section during an incremental link.  These methods are used
3126 // to insert dummy compilation units into debug info so that
3127 // debug info consumers can scan the debug info serially.
3128 
3129 class Output_fill
3130 {
3131  public:
Output_fill()3132   Output_fill()
3133     : is_big_endian_(parameters->target().is_big_endian())
3134   { }
3135 
3136   virtual
~Output_fill()3137   ~Output_fill()
3138   { }
3139 
3140   // Return the smallest size chunk of free space that can be
3141   // filled with a dummy compilation unit.
3142   size_t
minimum_hole_size()3143   minimum_hole_size() const
3144   { return this->do_minimum_hole_size(); }
3145 
3146   // Write a fill pattern of length LEN at offset OFF in the file.
3147   void
write(Output_file * of,off_t off,size_t len)3148   write(Output_file* of, off_t off, size_t len) const
3149   { this->do_write(of, off, len); }
3150 
3151  protected:
3152   virtual size_t
3153   do_minimum_hole_size() const = 0;
3154 
3155   virtual void
3156   do_write(Output_file* of, off_t off, size_t len) const = 0;
3157 
3158   bool
is_big_endian()3159   is_big_endian() const
3160   { return this->is_big_endian_; }
3161 
3162  private:
3163   bool is_big_endian_;
3164 };
3165 
3166 // Fill method that introduces a dummy compilation unit in
3167 // a .debug_info or .debug_types section.
3168 
3169 class Output_fill_debug_info : public Output_fill
3170 {
3171  public:
Output_fill_debug_info(bool is_debug_types)3172   Output_fill_debug_info(bool is_debug_types)
3173     : is_debug_types_(is_debug_types)
3174   { }
3175 
3176  protected:
3177   virtual size_t
3178   do_minimum_hole_size() const;
3179 
3180   virtual void
3181   do_write(Output_file* of, off_t off, size_t len) const;
3182 
3183  private:
3184   // Version of the header.
3185   static const int version = 4;
3186   // True if this is a .debug_types section.
3187   bool is_debug_types_;
3188 };
3189 
3190 // Fill method that introduces a dummy compilation unit in
3191 // a .debug_line section.
3192 
3193 class Output_fill_debug_line : public Output_fill
3194 {
3195  public:
Output_fill_debug_line()3196   Output_fill_debug_line()
3197   { }
3198 
3199  protected:
3200   virtual size_t
3201   do_minimum_hole_size() const;
3202 
3203   virtual void
3204   do_write(Output_file* of, off_t off, size_t len) const;
3205 
3206  private:
3207   // Version of the header.  We write a DWARF-3 header because it's smaller
3208   // and many tools have not yet been updated to understand the DWARF-4 header.
3209   static const int version = 3;
3210   // Length of the portion of the header that follows the header_length
3211   // field.  This includes the following fields:
3212   // minimum_instruction_length, default_is_stmt, line_base, line_range,
3213   // opcode_base, standard_opcode_lengths[], include_directories, filenames.
3214   // The standard_opcode_lengths array is 12 bytes long, and the
3215   // include_directories and filenames fields each contain only a single
3216   // null byte.
3217   static const size_t header_length = 19;
3218 };
3219 
3220 // An output section.  We don't expect to have too many output
3221 // sections, so we don't bother to do a template on the size.
3222 
3223 class Output_section : public Output_data
3224 {
3225  public:
3226   // Create an output section, giving the name, type, and flags.
3227   Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
3228   virtual ~Output_section();
3229 
3230   // Add a new input section SHNDX, named NAME, with header SHDR, from
3231   // object OBJECT.  RELOC_SHNDX is the index of a relocation section
3232   // which applies to this section, or 0 if none, or -1 if more than
3233   // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
3234   // in a linker script; in that case we need to keep track of input
3235   // sections associated with an output section.  Return the offset
3236   // within the output section.
3237   template<int size, bool big_endian>
3238   off_t
3239   add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
3240 		    unsigned int shndx, const char* name,
3241 		    const elfcpp::Shdr<size, big_endian>& shdr,
3242 		    unsigned int reloc_shndx, bool have_sections_script);
3243 
3244   // Add generated data POSD to this output section.
3245   void
3246   add_output_section_data(Output_section_data* posd);
3247 
3248   // Add a relaxed input section PORIS called NAME to this output section
3249   // with LAYOUT.
3250   void
3251   add_relaxed_input_section(Layout* layout,
3252 			    Output_relaxed_input_section* poris,
3253 			    const std::string& name);
3254 
3255   // Return the section name.
3256   const char*
name()3257   name() const
3258   { return this->name_; }
3259 
3260   // Return the section type.
3261   elfcpp::Elf_Word
type()3262   type() const
3263   { return this->type_; }
3264 
3265   // Return the section flags.
3266   elfcpp::Elf_Xword
flags()3267   flags() const
3268   { return this->flags_; }
3269 
3270   typedef std::map<Section_id, unsigned int> Section_layout_order;
3271 
3272   void
3273   update_section_layout(const Section_layout_order* order_map);
3274 
3275   // Update the output section flags based on input section flags.
3276   void
3277   update_flags_for_input_section(elfcpp::Elf_Xword flags);
3278 
3279   // Set the output section flags.
3280   void
set_flags(elfcpp::Elf_Xword flags)3281   set_flags(elfcpp::Elf_Xword flags)
3282   { this->flags_ = flags; }
3283 
3284   // Return the entsize field.
3285   uint64_t
entsize()3286   entsize() const
3287   { return this->entsize_; }
3288 
3289   // Set the entsize field.
3290   void
3291   set_entsize(uint64_t v);
3292 
3293   // Set the load address.
3294   void
set_load_address(uint64_t load_address)3295   set_load_address(uint64_t load_address)
3296   {
3297     this->load_address_ = load_address;
3298     this->has_load_address_ = true;
3299   }
3300 
3301   // Set the link field to the output section index of a section.
3302   void
set_link_section(const Output_data * od)3303   set_link_section(const Output_data* od)
3304   {
3305     gold_assert(this->link_ == 0
3306 		&& !this->should_link_to_symtab_
3307 		&& !this->should_link_to_dynsym_);
3308     this->link_section_ = od;
3309   }
3310 
3311   // Set the link field to a constant.
3312   void
set_link(unsigned int v)3313   set_link(unsigned int v)
3314   {
3315     gold_assert(this->link_section_ == NULL
3316 		&& !this->should_link_to_symtab_
3317 		&& !this->should_link_to_dynsym_);
3318     this->link_ = v;
3319   }
3320 
3321   // Record that this section should link to the normal symbol table.
3322   void
set_should_link_to_symtab()3323   set_should_link_to_symtab()
3324   {
3325     gold_assert(this->link_section_ == NULL
3326 		&& this->link_ == 0
3327 		&& !this->should_link_to_dynsym_);
3328     this->should_link_to_symtab_ = true;
3329   }
3330 
3331   // Record that this section should link to the dynamic symbol table.
3332   void
set_should_link_to_dynsym()3333   set_should_link_to_dynsym()
3334   {
3335     gold_assert(this->link_section_ == NULL
3336 		&& this->link_ == 0
3337 		&& !this->should_link_to_symtab_);
3338     this->should_link_to_dynsym_ = true;
3339   }
3340 
3341   // Return the info field.
3342   unsigned int
info()3343   info() const
3344   {
3345     gold_assert(this->info_section_ == NULL
3346 		&& this->info_symndx_ == NULL);
3347     return this->info_;
3348   }
3349 
3350   // Set the info field to the output section index of a section.
3351   void
set_info_section(const Output_section * os)3352   set_info_section(const Output_section* os)
3353   {
3354     gold_assert((this->info_section_ == NULL
3355 		 || (this->info_section_ == os
3356 		     && this->info_uses_section_index_))
3357 		&& this->info_symndx_ == NULL
3358 		&& this->info_ == 0);
3359     this->info_section_ = os;
3360     this->info_uses_section_index_= true;
3361   }
3362 
3363   // Set the info field to the symbol table index of a symbol.
3364   void
set_info_symndx(const Symbol * sym)3365   set_info_symndx(const Symbol* sym)
3366   {
3367     gold_assert(this->info_section_ == NULL
3368 		&& (this->info_symndx_ == NULL
3369 		    || this->info_symndx_ == sym)
3370 		&& this->info_ == 0);
3371     this->info_symndx_ = sym;
3372   }
3373 
3374   // Set the info field to the symbol table index of a section symbol.
3375   void
set_info_section_symndx(const Output_section * os)3376   set_info_section_symndx(const Output_section* os)
3377   {
3378     gold_assert((this->info_section_ == NULL
3379 		 || (this->info_section_ == os
3380 		     && !this->info_uses_section_index_))
3381 		&& this->info_symndx_ == NULL
3382 		&& this->info_ == 0);
3383     this->info_section_ = os;
3384     this->info_uses_section_index_ = false;
3385   }
3386 
3387   // Set the info field to a constant.
3388   void
set_info(unsigned int v)3389   set_info(unsigned int v)
3390   {
3391     gold_assert(this->info_section_ == NULL
3392 		&& this->info_symndx_ == NULL
3393 		&& (this->info_ == 0
3394 		    || this->info_ == v));
3395     this->info_ = v;
3396   }
3397 
3398   // Set the addralign field.
3399   void
set_addralign(uint64_t v)3400   set_addralign(uint64_t v)
3401   { this->addralign_ = v; }
3402 
3403   void
checkpoint_set_addralign(uint64_t val)3404   checkpoint_set_addralign(uint64_t val)
3405   {
3406     if (this->checkpoint_ != NULL)
3407       this->checkpoint_->set_addralign(val);
3408   }
3409 
3410   // Whether the output section index has been set.
3411   bool
has_out_shndx()3412   has_out_shndx() const
3413   { return this->out_shndx_ != -1U; }
3414 
3415   // Indicate that we need a symtab index.
3416   void
set_needs_symtab_index()3417   set_needs_symtab_index()
3418   { this->needs_symtab_index_ = true; }
3419 
3420   // Return whether we need a symtab index.
3421   bool
needs_symtab_index()3422   needs_symtab_index() const
3423   { return this->needs_symtab_index_; }
3424 
3425   // Get the symtab index.
3426   unsigned int
symtab_index()3427   symtab_index() const
3428   {
3429     gold_assert(this->symtab_index_ != 0);
3430     return this->symtab_index_;
3431   }
3432 
3433   // Set the symtab index.
3434   void
set_symtab_index(unsigned int index)3435   set_symtab_index(unsigned int index)
3436   {
3437     gold_assert(index != 0);
3438     this->symtab_index_ = index;
3439   }
3440 
3441   // Indicate that we need a dynsym index.
3442   void
set_needs_dynsym_index()3443   set_needs_dynsym_index()
3444   { this->needs_dynsym_index_ = true; }
3445 
3446   // Return whether we need a dynsym index.
3447   bool
needs_dynsym_index()3448   needs_dynsym_index() const
3449   { return this->needs_dynsym_index_; }
3450 
3451   // Get the dynsym index.
3452   unsigned int
dynsym_index()3453   dynsym_index() const
3454   {
3455     gold_assert(this->dynsym_index_ != 0);
3456     return this->dynsym_index_;
3457   }
3458 
3459   // Set the dynsym index.
3460   void
set_dynsym_index(unsigned int index)3461   set_dynsym_index(unsigned int index)
3462   {
3463     gold_assert(index != 0);
3464     this->dynsym_index_ = index;
3465   }
3466 
3467   // Sort the attached input sections.
3468   void
3469   sort_attached_input_sections();
3470 
3471   // Return whether the input sections sections attachd to this output
3472   // section may require sorting.  This is used to handle constructor
3473   // priorities compatibly with GNU ld.
3474   bool
may_sort_attached_input_sections()3475   may_sort_attached_input_sections() const
3476   { return this->may_sort_attached_input_sections_; }
3477 
3478   // Record that the input sections attached to this output section
3479   // may require sorting.
3480   void
set_may_sort_attached_input_sections()3481   set_may_sort_attached_input_sections()
3482   { this->may_sort_attached_input_sections_ = true; }
3483 
3484    // Returns true if input sections must be sorted according to the
3485   // order in which their name appear in the --section-ordering-file.
3486   bool
input_section_order_specified()3487   input_section_order_specified()
3488   { return this->input_section_order_specified_; }
3489 
3490   // Record that input sections must be sorted as some of their names
3491   // match the patterns specified through --section-ordering-file.
3492   void
set_input_section_order_specified()3493   set_input_section_order_specified()
3494   { this->input_section_order_specified_ = true; }
3495 
3496   // Return whether the input sections attached to this output section
3497   // require sorting.  This is used to handle constructor priorities
3498   // compatibly with GNU ld.
3499   bool
must_sort_attached_input_sections()3500   must_sort_attached_input_sections() const
3501   { return this->must_sort_attached_input_sections_; }
3502 
3503   // Record that the input sections attached to this output section
3504   // require sorting.
3505   void
set_must_sort_attached_input_sections()3506   set_must_sort_attached_input_sections()
3507   { this->must_sort_attached_input_sections_ = true; }
3508 
3509   // Get the order in which this section appears in the PT_LOAD output
3510   // segment.
3511   Output_section_order
order()3512   order() const
3513   { return this->order_; }
3514 
3515   // Set the order for this section.
3516   void
set_order(Output_section_order order)3517   set_order(Output_section_order order)
3518   { this->order_ = order; }
3519 
3520   // Return whether this section holds relro data--data which has
3521   // dynamic relocations but which may be marked read-only after the
3522   // dynamic relocations have been completed.
3523   bool
is_relro()3524   is_relro() const
3525   { return this->is_relro_; }
3526 
3527   // Record that this section holds relro data.
3528   void
set_is_relro()3529   set_is_relro()
3530   { this->is_relro_ = true; }
3531 
3532   // Record that this section does not hold relro data.
3533   void
clear_is_relro()3534   clear_is_relro()
3535   { this->is_relro_ = false; }
3536 
3537   // True if this is a small section: a section which holds small
3538   // variables.
3539   bool
is_small_section()3540   is_small_section() const
3541   { return this->is_small_section_; }
3542 
3543   // Record that this is a small section.
3544   void
set_is_small_section()3545   set_is_small_section()
3546   { this->is_small_section_ = true; }
3547 
3548   // True if this is a large section: a section which holds large
3549   // variables.
3550   bool
is_large_section()3551   is_large_section() const
3552   { return this->is_large_section_; }
3553 
3554   // Record that this is a large section.
3555   void
set_is_large_section()3556   set_is_large_section()
3557   { this->is_large_section_ = true; }
3558 
3559   // True if this is a large data (not BSS) section.
3560   bool
is_large_data_section()3561   is_large_data_section()
3562   { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3563 
3564   // Return whether this section should be written after all the input
3565   // sections are complete.
3566   bool
after_input_sections()3567   after_input_sections() const
3568   { return this->after_input_sections_; }
3569 
3570   // Record that this section should be written after all the input
3571   // sections are complete.
3572   void
set_after_input_sections()3573   set_after_input_sections()
3574   { this->after_input_sections_ = true; }
3575 
3576   // Return whether this section requires postprocessing after all
3577   // relocations have been applied.
3578   bool
requires_postprocessing()3579   requires_postprocessing() const
3580   { return this->requires_postprocessing_; }
3581 
3582   bool
is_unique_segment()3583   is_unique_segment() const
3584   { return this->is_unique_segment_; }
3585 
3586   void
set_is_unique_segment()3587   set_is_unique_segment()
3588   { this->is_unique_segment_ = true; }
3589 
extra_segment_flags()3590   uint64_t extra_segment_flags() const
3591   { return this->extra_segment_flags_; }
3592 
3593   void
set_extra_segment_flags(uint64_t flags)3594   set_extra_segment_flags(uint64_t flags)
3595   { this->extra_segment_flags_ = flags; }
3596 
segment_alignment()3597   uint64_t segment_alignment() const
3598   { return this->segment_alignment_; }
3599 
3600   void
set_segment_alignment(uint64_t align)3601   set_segment_alignment(uint64_t align)
3602   { this->segment_alignment_ = align; }
3603 
3604   // If a section requires postprocessing, return the buffer to use.
3605   unsigned char*
postprocessing_buffer()3606   postprocessing_buffer() const
3607   {
3608     gold_assert(this->postprocessing_buffer_ != NULL);
3609     return this->postprocessing_buffer_;
3610   }
3611 
3612   // If a section requires postprocessing, create the buffer to use.
3613   void
3614   create_postprocessing_buffer();
3615 
3616   // If a section requires postprocessing, this is the size of the
3617   // buffer to which relocations should be applied.
3618   off_t
postprocessing_buffer_size()3619   postprocessing_buffer_size() const
3620   { return this->current_data_size_for_child(); }
3621 
3622   // Modify the section name.  This is only permitted for an
3623   // unallocated section, and only before the size has been finalized.
3624   // Otherwise the name will not get into Layout::namepool_.
3625   void
set_name(const char * newname)3626   set_name(const char* newname)
3627   {
3628     gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3629     gold_assert(!this->is_data_size_valid());
3630     this->name_ = newname;
3631   }
3632 
3633   // Return whether the offset OFFSET in the input section SHNDX in
3634   // object OBJECT is being included in the link.
3635   bool
3636   is_input_address_mapped(const Relobj* object, unsigned int shndx,
3637 			  off_t offset) const;
3638 
3639   // Return the offset within the output section of OFFSET relative to
3640   // the start of input section SHNDX in object OBJECT.
3641   section_offset_type
3642   output_offset(const Relobj* object, unsigned int shndx,
3643 		section_offset_type offset) const;
3644 
3645   // Return the output virtual address of OFFSET relative to the start
3646   // of input section SHNDX in object OBJECT.
3647   uint64_t
3648   output_address(const Relobj* object, unsigned int shndx,
3649 		 off_t offset) const;
3650 
3651   // Look for the merged section for input section SHNDX in object
3652   // OBJECT.  If found, return true, and set *ADDR to the address of
3653   // the start of the merged section.  This is not necessary the
3654   // output offset corresponding to input offset 0 in the section,
3655   // since the section may be mapped arbitrarily.
3656   bool
3657   find_starting_output_address(const Relobj* object, unsigned int shndx,
3658 			       uint64_t* addr) const;
3659 
3660   // Record that this output section was found in the SECTIONS clause
3661   // of a linker script.
3662   void
set_found_in_sections_clause()3663   set_found_in_sections_clause()
3664   { this->found_in_sections_clause_ = true; }
3665 
3666   // Return whether this output section was found in the SECTIONS
3667   // clause of a linker script.
3668   bool
found_in_sections_clause()3669   found_in_sections_clause() const
3670   { return this->found_in_sections_clause_; }
3671 
3672   // Write the section header into *OPHDR.
3673   template<int size, bool big_endian>
3674   void
3675   write_header(const Layout*, const Stringpool*,
3676 	       elfcpp::Shdr_write<size, big_endian>*) const;
3677 
3678   // The next few calls are for linker script support.
3679 
3680   // In some cases we need to keep a list of the input sections
3681   // associated with this output section.  We only need the list if we
3682   // might have to change the offsets of the input section within the
3683   // output section after we add the input section.  The ordinary
3684   // input sections will be written out when we process the object
3685   // file, and as such we don't need to track them here.  We do need
3686   // to track Output_section_data objects here.  We store instances of
3687   // this structure in a std::vector, so it must be a POD.  There can
3688   // be many instances of this structure, so we use a union to save
3689   // some space.
3690   class Input_section
3691   {
3692    public:
Input_section()3693     Input_section()
3694       : shndx_(0), p2align_(0)
3695     {
3696       this->u1_.data_size = 0;
3697       this->u2_.object = NULL;
3698     }
3699 
3700     // For an ordinary input section.
Input_section(Relobj * object,unsigned int shndx,off_t data_size,uint64_t addralign)3701     Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3702 		  uint64_t addralign)
3703       : shndx_(shndx),
3704 	p2align_(ffsll(static_cast<long long>(addralign))),
3705 	section_order_index_(0)
3706     {
3707       gold_assert(shndx != OUTPUT_SECTION_CODE
3708 		  && shndx != MERGE_DATA_SECTION_CODE
3709 		  && shndx != MERGE_STRING_SECTION_CODE
3710 		  && shndx != RELAXED_INPUT_SECTION_CODE);
3711       this->u1_.data_size = data_size;
3712       this->u2_.object = object;
3713     }
3714 
3715     // For a non-merge output section.
Input_section(Output_section_data * posd)3716     Input_section(Output_section_data* posd)
3717       : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3718 	section_order_index_(0)
3719     {
3720       this->u1_.data_size = 0;
3721       this->u2_.posd = posd;
3722     }
3723 
3724     // For a merge section.
Input_section(Output_section_data * posd,bool is_string,uint64_t entsize)3725     Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3726       : shndx_(is_string
3727 	       ? MERGE_STRING_SECTION_CODE
3728 	       : MERGE_DATA_SECTION_CODE),
3729 	p2align_(0),
3730 	section_order_index_(0)
3731     {
3732       this->u1_.entsize = entsize;
3733       this->u2_.posd = posd;
3734     }
3735 
3736     // For a relaxed input section.
Input_section(Output_relaxed_input_section * psection)3737     Input_section(Output_relaxed_input_section* psection)
3738       : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3739 	section_order_index_(0)
3740     {
3741       this->u1_.data_size = 0;
3742       this->u2_.poris = psection;
3743     }
3744 
3745     unsigned int
section_order_index()3746     section_order_index() const
3747     {
3748       return this->section_order_index_;
3749     }
3750 
3751     void
set_section_order_index(unsigned int number)3752     set_section_order_index(unsigned int number)
3753     {
3754       this->section_order_index_ = number;
3755     }
3756 
3757     // The required alignment.
3758     uint64_t
addralign()3759     addralign() const
3760     {
3761       if (this->p2align_ != 0)
3762 	return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3763       else if (!this->is_input_section())
3764 	return this->u2_.posd->addralign();
3765       else
3766 	return 0;
3767     }
3768 
3769     // Set the required alignment, which must be either 0 or a power of 2.
3770     // For input sections that are sub-classes of Output_section_data, a
3771     // alignment of zero means asking the underlying object for alignment.
3772     void
set_addralign(uint64_t addralign)3773     set_addralign(uint64_t addralign)
3774     {
3775       if (addralign == 0)
3776 	this->p2align_ = 0;
3777       else
3778 	{
3779 	  gold_assert((addralign & (addralign - 1)) == 0);
3780 	  this->p2align_ = ffsll(static_cast<long long>(addralign));
3781 	}
3782     }
3783 
3784     // Return the current required size, without finalization.
3785     off_t
3786     current_data_size() const;
3787 
3788     // Return the required size.
3789     off_t
3790     data_size() const;
3791 
3792     // Whether this is an input section.
3793     bool
is_input_section()3794     is_input_section() const
3795     {
3796       return (this->shndx_ != OUTPUT_SECTION_CODE
3797 	      && this->shndx_ != MERGE_DATA_SECTION_CODE
3798 	      && this->shndx_ != MERGE_STRING_SECTION_CODE
3799 	      && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3800     }
3801 
3802     // Return whether this is a merge section which matches the
3803     // parameters.
3804     bool
is_merge_section(bool is_string,uint64_t entsize,uint64_t addralign)3805     is_merge_section(bool is_string, uint64_t entsize,
3806 		     uint64_t addralign) const
3807     {
3808       return (this->shndx_ == (is_string
3809 			       ? MERGE_STRING_SECTION_CODE
3810 			       : MERGE_DATA_SECTION_CODE)
3811 	      && this->u1_.entsize == entsize
3812 	      && this->addralign() == addralign);
3813     }
3814 
3815     // Return whether this is a merge section for some input section.
3816     bool
is_merge_section()3817     is_merge_section() const
3818     {
3819       return (this->shndx_ == MERGE_DATA_SECTION_CODE
3820 	      || this->shndx_ == MERGE_STRING_SECTION_CODE);
3821     }
3822 
3823     // Return whether this is a relaxed input section.
3824     bool
is_relaxed_input_section()3825     is_relaxed_input_section() const
3826     { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3827 
3828     // Return whether this is a generic Output_section_data.
3829     bool
is_output_section_data()3830     is_output_section_data() const
3831     {
3832       return this->shndx_ == OUTPUT_SECTION_CODE;
3833     }
3834 
3835     // Return the object for an input section.
3836     Relobj*
3837     relobj() const;
3838 
3839     // Return the input section index for an input section.
3840     unsigned int
3841     shndx() const;
3842 
3843     // For non-input-sections, return the associated Output_section_data
3844     // object.
3845     Output_section_data*
output_section_data()3846     output_section_data() const
3847     {
3848       gold_assert(!this->is_input_section());
3849       return this->u2_.posd;
3850     }
3851 
3852     // For a merge section, return the Output_merge_base pointer.
3853     Output_merge_base*
output_merge_base()3854     output_merge_base() const
3855     {
3856       gold_assert(this->is_merge_section());
3857       return this->u2_.pomb;
3858     }
3859 
3860     // Return the Output_relaxed_input_section object.
3861     Output_relaxed_input_section*
relaxed_input_section()3862     relaxed_input_section() const
3863     {
3864       gold_assert(this->is_relaxed_input_section());
3865       return this->u2_.poris;
3866     }
3867 
3868     // Set the output section.
3869     void
set_output_section(Output_section * os)3870     set_output_section(Output_section* os)
3871     {
3872       gold_assert(!this->is_input_section());
3873       Output_section_data* posd =
3874 	this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3875       posd->set_output_section(os);
3876     }
3877 
3878     // Set the address and file offset.  This is called during
3879     // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
3880     // the enclosing section.
3881     void
3882     set_address_and_file_offset(uint64_t address, off_t file_offset,
3883 				off_t section_file_offset);
3884 
3885     // Reset the address and file offset.
3886     void
3887     reset_address_and_file_offset();
3888 
3889     // Finalize the data size.
3890     void
3891     finalize_data_size();
3892 
3893     // Add an input section, for SHF_MERGE sections.
3894     bool
add_input_section(Relobj * object,unsigned int shndx)3895     add_input_section(Relobj* object, unsigned int shndx)
3896     {
3897       gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3898 		  || this->shndx_ == MERGE_STRING_SECTION_CODE);
3899       return this->u2_.posd->add_input_section(object, shndx);
3900     }
3901 
3902     // Given an input OBJECT, an input section index SHNDX within that
3903     // object, and an OFFSET relative to the start of that input
3904     // section, return whether or not the output offset is known.  If
3905     // this function returns true, it sets *POUTPUT to the offset in
3906     // the output section, relative to the start of the input section
3907     // in the output section.  *POUTPUT may be different from OFFSET
3908     // for a merged section.
3909     bool
3910     output_offset(const Relobj* object, unsigned int shndx,
3911 		  section_offset_type offset,
3912 		  section_offset_type* poutput) const;
3913 
3914     // Write out the data.  This does nothing for an input section.
3915     void
3916     write(Output_file*);
3917 
3918     // Write the data to a buffer.  This does nothing for an input
3919     // section.
3920     void
3921     write_to_buffer(unsigned char*);
3922 
3923     // Print to a map file.
3924     void
3925     print_to_mapfile(Mapfile*) const;
3926 
3927     // Print statistics about merge sections to stderr.
3928     void
print_merge_stats(const char * section_name)3929     print_merge_stats(const char* section_name)
3930     {
3931       if (this->shndx_ == MERGE_DATA_SECTION_CODE
3932 	  || this->shndx_ == MERGE_STRING_SECTION_CODE)
3933 	this->u2_.posd->print_merge_stats(section_name);
3934     }
3935 
3936    private:
3937     // Code values which appear in shndx_.  If the value is not one of
3938     // these codes, it is the input section index in the object file.
3939     enum
3940     {
3941       // An Output_section_data.
3942       OUTPUT_SECTION_CODE = -1U,
3943       // An Output_section_data for an SHF_MERGE section with
3944       // SHF_STRINGS not set.
3945       MERGE_DATA_SECTION_CODE = -2U,
3946       // An Output_section_data for an SHF_MERGE section with
3947       // SHF_STRINGS set.
3948       MERGE_STRING_SECTION_CODE = -3U,
3949       // An Output_section_data for a relaxed input section.
3950       RELAXED_INPUT_SECTION_CODE = -4U
3951     };
3952 
3953     // For an ordinary input section, this is the section index in the
3954     // input file.  For an Output_section_data, this is
3955     // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3956     // MERGE_STRING_SECTION_CODE.
3957     unsigned int shndx_;
3958     // The required alignment, stored as a power of 2.
3959     unsigned int p2align_;
3960     union
3961     {
3962       // For an ordinary input section, the section size.
3963       off_t data_size;
3964       // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3965       // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3966       // entity size.
3967       uint64_t entsize;
3968     } u1_;
3969     union
3970     {
3971       // For an ordinary input section, the object which holds the
3972       // input section.
3973       Relobj* object;
3974       // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3975       // MERGE_STRING_SECTION_CODE, the data.
3976       Output_section_data* posd;
3977       Output_merge_base* pomb;
3978       // For RELAXED_INPUT_SECTION_CODE, the data.
3979       Output_relaxed_input_section* poris;
3980     } u2_;
3981     // The line number of the pattern it matches in the --section-ordering-file
3982     // file.  It is 0 if does not match any pattern.
3983     unsigned int section_order_index_;
3984   };
3985 
3986   // Store the list of input sections for this Output_section into the
3987   // list passed in.  This removes the input sections, leaving only
3988   // any Output_section_data elements.  This returns the size of those
3989   // Output_section_data elements.  ADDRESS is the address of this
3990   // output section.  FILL is the fill value to use, in case there are
3991   // any spaces between the remaining Output_section_data elements.
3992   uint64_t
3993   get_input_sections(uint64_t address, const std::string& fill,
3994 		     std::list<Input_section>*);
3995 
3996   // Add a script input section.  A script input section can either be
3997   // a plain input section or a sub-class of Output_section_data.
3998   void
3999   add_script_input_section(const Input_section& input_section);
4000 
4001   // Set the current size of the output section.
4002   void
set_current_data_size(off_t size)4003   set_current_data_size(off_t size)
4004   { this->set_current_data_size_for_child(size); }
4005 
4006   // End of linker script support.
4007 
4008   // Save states before doing section layout.
4009   // This is used for relaxation.
4010   void
4011   save_states();
4012 
4013   // Restore states prior to section layout.
4014   void
4015   restore_states();
4016 
4017   // Discard states.
4018   void
4019   discard_states();
4020 
4021   // Convert existing input sections to relaxed input sections.
4022   void
4023   convert_input_sections_to_relaxed_sections(
4024       const std::vector<Output_relaxed_input_section*>& sections);
4025 
4026   // Find a relaxed input section to an input section in OBJECT
4027   // with index SHNDX.  Return NULL if none is found.
4028   const Output_relaxed_input_section*
4029   find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
4030 
4031   // Whether section offsets need adjustment due to relaxation.
4032   bool
section_offsets_need_adjustment()4033   section_offsets_need_adjustment() const
4034   { return this->section_offsets_need_adjustment_; }
4035 
4036   // Set section_offsets_need_adjustment to be true.
4037   void
set_section_offsets_need_adjustment()4038   set_section_offsets_need_adjustment()
4039   { this->section_offsets_need_adjustment_ = true; }
4040 
4041   // Set section_offsets_need_adjustment to be false.
4042   void
clear_section_offsets_need_adjustment()4043   clear_section_offsets_need_adjustment()
4044   { this->section_offsets_need_adjustment_ = false; }
4045 
4046   // Adjust section offsets of input sections in this.  This is
4047   // requires if relaxation caused some input sections to change sizes.
4048   void
4049   adjust_section_offsets();
4050 
4051   // Whether this is a NOLOAD section.
4052   bool
is_noload()4053   is_noload() const
4054   { return this->is_noload_; }
4055 
4056   // Set NOLOAD flag.
4057   void
set_is_noload()4058   set_is_noload()
4059   { this->is_noload_ = true; }
4060 
4061   // Print merge statistics to stderr.
4062   void
4063   print_merge_stats();
4064 
4065   // Set a fixed layout for the section.  Used for incremental update links.
4066   void
4067   set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
4068 		   uint64_t sh_addralign);
4069 
4070   // Return TRUE if the section has a fixed layout.
4071   bool
has_fixed_layout()4072   has_fixed_layout() const
4073   { return this->has_fixed_layout_; }
4074 
4075   // Set flag to allow patch space for this section.  Used for full
4076   // incremental links.
4077   void
set_is_patch_space_allowed()4078   set_is_patch_space_allowed()
4079   { this->is_patch_space_allowed_ = true; }
4080 
4081   // Set a fill method to use for free space left in the output section
4082   // during incremental links.
4083   void
set_free_space_fill(Output_fill * free_space_fill)4084   set_free_space_fill(Output_fill* free_space_fill)
4085   {
4086     this->free_space_fill_ = free_space_fill;
4087     this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
4088   }
4089 
4090   // Reserve space within the fixed layout for the section.  Used for
4091   // incremental update links.
4092   void
4093   reserve(uint64_t sh_offset, uint64_t sh_size);
4094 
4095   // Allocate space from the free list for the section.  Used for
4096   // incremental update links.
4097   off_t
4098   allocate(off_t len, uint64_t addralign);
4099 
4100   typedef std::vector<Input_section> Input_section_list;
4101 
4102   // Allow access to the input sections.
4103   const Input_section_list&
input_sections()4104   input_sections() const
4105   { return this->input_sections_; }
4106 
4107   Input_section_list&
input_sections()4108   input_sections()
4109   { return this->input_sections_; }
4110 
4111   // For -r and --emit-relocs, we need to keep track of the associated
4112   // relocation section.
4113   Output_section*
reloc_section()4114   reloc_section() const
4115   { return this->reloc_section_; }
4116 
4117   void
set_reloc_section(Output_section * os)4118   set_reloc_section(Output_section* os)
4119   { this->reloc_section_ = os; }
4120 
4121  protected:
4122   // Return the output section--i.e., the object itself.
4123   Output_section*
do_output_section()4124   do_output_section()
4125   { return this; }
4126 
4127   const Output_section*
do_output_section()4128   do_output_section() const
4129   { return this; }
4130 
4131   // Return the section index in the output file.
4132   unsigned int
do_out_shndx()4133   do_out_shndx() const
4134   {
4135     gold_assert(this->out_shndx_ != -1U);
4136     return this->out_shndx_;
4137   }
4138 
4139   // Set the output section index.
4140   void
do_set_out_shndx(unsigned int shndx)4141   do_set_out_shndx(unsigned int shndx)
4142   {
4143     gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
4144     this->out_shndx_ = shndx;
4145   }
4146 
4147   // Update the data size of the Output_section.  For a typical
4148   // Output_section, there is nothing to do, but if there are any
4149   // Output_section_data objects we need to do a trial layout
4150   // here.
4151   virtual void
4152   update_data_size();
4153 
4154   // Set the final data size of the Output_section.  For a typical
4155   // Output_section, there is nothing to do, but if there are any
4156   // Output_section_data objects we need to set their final addresses
4157   // here.
4158   virtual void
4159   set_final_data_size();
4160 
4161   // Reset the address and file offset.
4162   void
4163   do_reset_address_and_file_offset();
4164 
4165   // Return true if address and file offset already have reset values. In
4166   // other words, calling reset_address_and_file_offset will not change them.
4167   bool
4168   do_address_and_file_offset_have_reset_values() const;
4169 
4170   // Write the data to the file.  For a typical Output_section, this
4171   // does nothing: the data is written out by calling Object::Relocate
4172   // on each input object.  But if there are any Output_section_data
4173   // objects we do need to write them out here.
4174   virtual void
4175   do_write(Output_file*);
4176 
4177   // Return the address alignment--function required by parent class.
4178   uint64_t
do_addralign()4179   do_addralign() const
4180   { return this->addralign_; }
4181 
4182   // Return whether there is a load address.
4183   bool
do_has_load_address()4184   do_has_load_address() const
4185   { return this->has_load_address_; }
4186 
4187   // Return the load address.
4188   uint64_t
do_load_address()4189   do_load_address() const
4190   {
4191     gold_assert(this->has_load_address_);
4192     return this->load_address_;
4193   }
4194 
4195   // Return whether this is an Output_section.
4196   bool
do_is_section()4197   do_is_section() const
4198   { return true; }
4199 
4200   // Return whether this is a section of the specified type.
4201   bool
do_is_section_type(elfcpp::Elf_Word type)4202   do_is_section_type(elfcpp::Elf_Word type) const
4203   { return this->type_ == type; }
4204 
4205   // Return whether the specified section flag is set.
4206   bool
do_is_section_flag_set(elfcpp::Elf_Xword flag)4207   do_is_section_flag_set(elfcpp::Elf_Xword flag) const
4208   { return (this->flags_ & flag) != 0; }
4209 
4210   // Set the TLS offset.  Called only for SHT_TLS sections.
4211   void
4212   do_set_tls_offset(uint64_t tls_base);
4213 
4214   // Return the TLS offset, relative to the base of the TLS segment.
4215   // Valid only for SHT_TLS sections.
4216   uint64_t
do_tls_offset()4217   do_tls_offset() const
4218   { return this->tls_offset_; }
4219 
4220   // This may be implemented by a child class.
4221   virtual void
do_finalize_name(Layout *)4222   do_finalize_name(Layout*)
4223   { }
4224 
4225   // Print to the map file.
4226   virtual void
4227   do_print_to_mapfile(Mapfile*) const;
4228 
4229   // Record that this section requires postprocessing after all
4230   // relocations have been applied.  This is called by a child class.
4231   void
set_requires_postprocessing()4232   set_requires_postprocessing()
4233   {
4234     this->requires_postprocessing_ = true;
4235     this->after_input_sections_ = true;
4236   }
4237 
4238   // Write all the data of an Output_section into the postprocessing
4239   // buffer.
4240   void
4241   write_to_postprocessing_buffer();
4242 
4243   // Whether this always keeps an input section list
4244   bool
always_keeps_input_sections()4245   always_keeps_input_sections() const
4246   { return this->always_keeps_input_sections_; }
4247 
4248   // Always keep an input section list.
4249   void
set_always_keeps_input_sections()4250   set_always_keeps_input_sections()
4251   {
4252     gold_assert(this->current_data_size_for_child() == 0);
4253     this->always_keeps_input_sections_ = true;
4254   }
4255 
4256  private:
4257   // We only save enough information to undo the effects of section layout.
4258   class Checkpoint_output_section
4259   {
4260    public:
Checkpoint_output_section(uint64_t addralign,elfcpp::Elf_Xword flags,const Input_section_list & input_sections,off_t first_input_offset,bool attached_input_sections_are_sorted)4261     Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
4262 			      const Input_section_list& input_sections,
4263 			      off_t first_input_offset,
4264 			      bool attached_input_sections_are_sorted)
4265       : addralign_(addralign), flags_(flags),
4266 	input_sections_(input_sections),
4267 	input_sections_size_(input_sections_.size()),
4268 	input_sections_copy_(), first_input_offset_(first_input_offset),
4269 	attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
4270     { }
4271 
4272     virtual
~Checkpoint_output_section()4273     ~Checkpoint_output_section()
4274     { }
4275 
4276     // Return the address alignment.
4277     uint64_t
addralign()4278     addralign() const
4279     { return this->addralign_; }
4280 
4281     void
set_addralign(uint64_t val)4282     set_addralign(uint64_t val)
4283     { this->addralign_ = val; }
4284 
4285     // Return the section flags.
4286     elfcpp::Elf_Xword
flags()4287     flags() const
4288     { return this->flags_; }
4289 
4290     // Return a reference to the input section list copy.
4291     Input_section_list*
input_sections()4292     input_sections()
4293     { return &this->input_sections_copy_; }
4294 
4295     // Return the size of input_sections at the time when checkpoint is
4296     // taken.
4297     size_t
input_sections_size()4298     input_sections_size() const
4299     { return this->input_sections_size_; }
4300 
4301     // Whether input sections are copied.
4302     bool
input_sections_saved()4303     input_sections_saved() const
4304     { return this->input_sections_copy_.size() == this->input_sections_size_; }
4305 
4306     off_t
first_input_offset()4307     first_input_offset() const
4308     { return this->first_input_offset_; }
4309 
4310     bool
attached_input_sections_are_sorted()4311     attached_input_sections_are_sorted() const
4312     { return this->attached_input_sections_are_sorted_; }
4313 
4314     // Save input sections.
4315     void
save_input_sections()4316     save_input_sections()
4317     {
4318       this->input_sections_copy_.reserve(this->input_sections_size_);
4319       this->input_sections_copy_.clear();
4320       Input_section_list::const_iterator p = this->input_sections_.begin();
4321       gold_assert(this->input_sections_size_ >= this->input_sections_.size());
4322       for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
4323 	this->input_sections_copy_.push_back(*p);
4324     }
4325 
4326    private:
4327     // The section alignment.
4328     uint64_t addralign_;
4329     // The section flags.
4330     elfcpp::Elf_Xword flags_;
4331     // Reference to the input sections to be checkpointed.
4332     const Input_section_list& input_sections_;
4333     // Size of the checkpointed portion of input_sections_;
4334     size_t input_sections_size_;
4335     // Copy of input sections.
4336     Input_section_list input_sections_copy_;
4337     // The offset of the first entry in input_sections_.
4338     off_t first_input_offset_;
4339     // True if the input sections attached to this output section have
4340     // already been sorted.
4341     bool attached_input_sections_are_sorted_;
4342   };
4343 
4344   // This class is used to sort the input sections.
4345   class Input_section_sort_entry;
4346 
4347   // This is the sort comparison function for ctors and dtors.
4348   struct Input_section_sort_compare
4349   {
4350     bool
4351     operator()(const Input_section_sort_entry&,
4352 	       const Input_section_sort_entry&) const;
4353   };
4354 
4355   // This is the sort comparison function for .init_array and .fini_array.
4356   struct Input_section_sort_init_fini_compare
4357   {
4358     bool
4359     operator()(const Input_section_sort_entry&,
4360 	       const Input_section_sort_entry&) const;
4361   };
4362 
4363   // This is the sort comparison function when a section order is specified
4364   // from an input file.
4365   struct Input_section_sort_section_order_index_compare
4366   {
4367     bool
4368     operator()(const Input_section_sort_entry&,
4369 	       const Input_section_sort_entry&) const;
4370   };
4371 
4372   // This is the sort comparison function for .text to sort sections with
4373   // prefixes .text.{unlikely,exit,startup,hot} before other sections.
4374   struct Input_section_sort_section_prefix_special_ordering_compare
4375   {
4376     bool
4377     operator()(const Input_section_sort_entry&,
4378 	       const Input_section_sort_entry&) const;
4379   };
4380 
4381   // This is the sort comparison function for sorting sections by name.
4382   struct Input_section_sort_section_name_compare
4383   {
4384     bool
4385     operator()(const Input_section_sort_entry&,
4386 	       const Input_section_sort_entry&) const;
4387   };
4388 
4389   // Fill data.  This is used to fill in data between input sections.
4390   // It is also used for data statements (BYTE, WORD, etc.) in linker
4391   // scripts.  When we have to keep track of the input sections, we
4392   // can use an Output_data_const, but we don't want to have to keep
4393   // track of input sections just to implement fills.
4394   class Fill
4395   {
4396    public:
Fill(off_t section_offset,off_t length)4397     Fill(off_t section_offset, off_t length)
4398       : section_offset_(section_offset),
4399 	length_(convert_to_section_size_type(length))
4400     { }
4401 
4402     // Return section offset.
4403     off_t
section_offset()4404     section_offset() const
4405     { return this->section_offset_; }
4406 
4407     // Return fill length.
4408     section_size_type
length()4409     length() const
4410     { return this->length_; }
4411 
4412    private:
4413     // The offset within the output section.
4414     off_t section_offset_;
4415     // The length of the space to fill.
4416     section_size_type length_;
4417   };
4418 
4419   typedef std::vector<Fill> Fill_list;
4420 
4421   // Map used during relaxation of existing sections.  This map
4422   // a section id an input section list index.  We assume that
4423   // Input_section_list is a vector.
4424   typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4425 
4426   // Add a new output section by Input_section.
4427   void
4428   add_output_section_data(Input_section*);
4429 
4430   // Add an SHF_MERGE input section.  Returns true if the section was
4431   // handled.  If KEEPS_INPUT_SECTIONS is true, the output merge section
4432   // stores information about the merged input sections.
4433   bool
4434   add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4435 			  uint64_t entsize, uint64_t addralign,
4436 			  bool keeps_input_sections);
4437 
4438   // Add an output SHF_MERGE section POSD to this output section.
4439   // IS_STRING indicates whether it is a SHF_STRINGS section, and
4440   // ENTSIZE is the entity size.  This returns the entry added to
4441   // input_sections_.
4442   void
4443   add_output_merge_section(Output_section_data* posd, bool is_string,
4444 			   uint64_t entsize);
4445 
4446   // Find the merge section into which an input section with index SHNDX in
4447   // OBJECT has been added.  Return NULL if none found.
4448   const Output_section_data*
4449   find_merge_section(const Relobj* object, unsigned int shndx) const;
4450 
4451   // Build a relaxation map.
4452   void
4453   build_relaxation_map(
4454       const Input_section_list& input_sections,
4455       size_t limit,
4456       Relaxation_map* map) const;
4457 
4458   // Convert input sections in an input section list into relaxed sections.
4459   void
4460   convert_input_sections_in_list_to_relaxed_sections(
4461       const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4462       const Relaxation_map& map,
4463       Input_section_list* input_sections);
4464 
4465   // Build the lookup maps for merge and relaxed input sections.
4466   void
4467   build_lookup_maps() const;
4468 
4469   // Most of these fields are only valid after layout.
4470 
4471   // The name of the section.  This will point into a Stringpool.
4472   const char* name_;
4473   // The section address is in the parent class.
4474   // The section alignment.
4475   uint64_t addralign_;
4476   // The section entry size.
4477   uint64_t entsize_;
4478   // The load address.  This is only used when using a linker script
4479   // with a SECTIONS clause.  The has_load_address_ field indicates
4480   // whether this field is valid.
4481   uint64_t load_address_;
4482   // The file offset is in the parent class.
4483   // Set the section link field to the index of this section.
4484   const Output_data* link_section_;
4485   // If link_section_ is NULL, this is the link field.
4486   unsigned int link_;
4487   // Set the section info field to the index of this section.
4488   const Output_section* info_section_;
4489   // If info_section_ is NULL, set the info field to the symbol table
4490   // index of this symbol.
4491   const Symbol* info_symndx_;
4492   // If info_section_ and info_symndx_ are NULL, this is the section
4493   // info field.
4494   unsigned int info_;
4495   // The section type.
4496   const elfcpp::Elf_Word type_;
4497   // The section flags.
4498   elfcpp::Elf_Xword flags_;
4499   // The order of this section in the output segment.
4500   Output_section_order order_;
4501   // The section index.
4502   unsigned int out_shndx_;
4503   // If there is a STT_SECTION for this output section in the normal
4504   // symbol table, this is the symbol index.  This starts out as zero.
4505   // It is initialized in Layout::finalize() to be the index, or -1U
4506   // if there isn't one.
4507   unsigned int symtab_index_;
4508   // If there is a STT_SECTION for this output section in the dynamic
4509   // symbol table, this is the symbol index.  This starts out as zero.
4510   // It is initialized in Layout::finalize() to be the index, or -1U
4511   // if there isn't one.
4512   unsigned int dynsym_index_;
4513   // The input sections.  This will be empty in cases where we don't
4514   // need to keep track of them.
4515   Input_section_list input_sections_;
4516   // The offset of the first entry in input_sections_.
4517   off_t first_input_offset_;
4518   // The fill data.  This is separate from input_sections_ because we
4519   // often will need fill sections without needing to keep track of
4520   // input sections.
4521   Fill_list fills_;
4522   // If the section requires postprocessing, this buffer holds the
4523   // section contents during relocation.
4524   unsigned char* postprocessing_buffer_;
4525   // Whether this output section needs a STT_SECTION symbol in the
4526   // normal symbol table.  This will be true if there is a relocation
4527   // which needs it.
4528   bool needs_symtab_index_ : 1;
4529   // Whether this output section needs a STT_SECTION symbol in the
4530   // dynamic symbol table.  This will be true if there is a dynamic
4531   // relocation which needs it.
4532   bool needs_dynsym_index_ : 1;
4533   // Whether the link field of this output section should point to the
4534   // normal symbol table.
4535   bool should_link_to_symtab_ : 1;
4536   // Whether the link field of this output section should point to the
4537   // dynamic symbol table.
4538   bool should_link_to_dynsym_ : 1;
4539   // Whether this section should be written after all the input
4540   // sections are complete.
4541   bool after_input_sections_ : 1;
4542   // Whether this section requires post processing after all
4543   // relocations have been applied.
4544   bool requires_postprocessing_ : 1;
4545   // Whether an input section was mapped to this output section
4546   // because of a SECTIONS clause in a linker script.
4547   bool found_in_sections_clause_ : 1;
4548   // Whether this section has an explicitly specified load address.
4549   bool has_load_address_ : 1;
4550   // True if the info_section_ field means the section index of the
4551   // section, false if it means the symbol index of the corresponding
4552   // section symbol.
4553   bool info_uses_section_index_ : 1;
4554   // True if input sections attached to this output section have to be
4555   // sorted according to a specified order.
4556   bool input_section_order_specified_ : 1;
4557   // True if the input sections attached to this output section may
4558   // need sorting.
4559   bool may_sort_attached_input_sections_ : 1;
4560   // True if the input sections attached to this output section must
4561   // be sorted.
4562   bool must_sort_attached_input_sections_ : 1;
4563   // True if the input sections attached to this output section have
4564   // already been sorted.
4565   bool attached_input_sections_are_sorted_ : 1;
4566   // True if this section holds relro data.
4567   bool is_relro_ : 1;
4568   // True if this is a small section.
4569   bool is_small_section_ : 1;
4570   // True if this is a large section.
4571   bool is_large_section_ : 1;
4572   // Whether code-fills are generated at write.
4573   bool generate_code_fills_at_write_ : 1;
4574   // Whether the entry size field should be zero.
4575   bool is_entsize_zero_ : 1;
4576   // Whether section offsets need adjustment due to relaxation.
4577   bool section_offsets_need_adjustment_ : 1;
4578   // Whether this is a NOLOAD section.
4579   bool is_noload_ : 1;
4580   // Whether this always keeps input section.
4581   bool always_keeps_input_sections_ : 1;
4582   // Whether this section has a fixed layout, for incremental update links.
4583   bool has_fixed_layout_ : 1;
4584   // True if we can add patch space to this section.
4585   bool is_patch_space_allowed_ : 1;
4586   // True if this output section goes into a unique segment.
4587   bool is_unique_segment_ : 1;
4588   // For SHT_TLS sections, the offset of this section relative to the base
4589   // of the TLS segment.
4590   uint64_t tls_offset_;
4591   // Additional segment flags, specified via linker plugin, when mapping some
4592   // input sections to unique segments.
4593   uint64_t extra_segment_flags_;
4594   // Segment alignment specified via linker plugin, when mapping some
4595   // input sections to unique segments.
4596   uint64_t segment_alignment_;
4597   // Saved checkpoint.
4598   Checkpoint_output_section* checkpoint_;
4599   // Fast lookup maps for merged and relaxed input sections.
4600   Output_section_lookup_maps* lookup_maps_;
4601   // List of available regions within the section, for incremental
4602   // update links.
4603   Free_list free_list_;
4604   // Method for filling chunks of free space.
4605   Output_fill* free_space_fill_;
4606   // Amount added as patch space for incremental linking.
4607   off_t patch_space_;
4608   // Associated relocation section, when emitting relocations.
4609   Output_section* reloc_section_;
4610 };
4611 
4612 // An output segment.  PT_LOAD segments are built from collections of
4613 // output sections.  Other segments typically point within PT_LOAD
4614 // segments, and are built directly as needed.
4615 //
4616 // NOTE: We want to use the copy constructor for this class.  During
4617 // relaxation, we may try built the segments multiple times.  We do
4618 // that by copying the original segment list before lay-out, doing
4619 // a trial lay-out and roll-back to the saved copied if we need to
4620 // to the lay-out again.
4621 
4622 class Output_segment
4623 {
4624  public:
4625   // Create an output segment, specifying the type and flags.
4626   Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4627 
4628   // Return the virtual address.
4629   uint64_t
vaddr()4630   vaddr() const
4631   { return this->vaddr_; }
4632 
4633   // Return the physical address.
4634   uint64_t
paddr()4635   paddr() const
4636   { return this->paddr_; }
4637 
4638   // Return the segment type.
4639   elfcpp::Elf_Word
type()4640   type() const
4641   { return this->type_; }
4642 
4643   // Return the segment flags.
4644   elfcpp::Elf_Word
flags()4645   flags() const
4646   { return this->flags_; }
4647 
4648   // Return the memory size.
4649   uint64_t
memsz()4650   memsz() const
4651   { return this->memsz_; }
4652 
4653   // Return the file size.
4654   off_t
filesz()4655   filesz() const
4656   { return this->filesz_; }
4657 
4658   // Return the file offset.
4659   off_t
offset()4660   offset() const
4661   { return this->offset_; }
4662 
4663   // Return the segment alignment.
4664   uint64_t
align()4665   align() const
4666   { return this->align_; }
4667 
4668   // Set the segment alignment.
4669   void
set_align(uint64_t align)4670   set_align(uint64_t align)
4671   { this->align_ = align; }
4672 
4673   // Whether this is a segment created to hold large data sections.
4674   bool
is_large_data_segment()4675   is_large_data_segment() const
4676   { return this->is_large_data_segment_; }
4677 
4678   // Record that this is a segment created to hold large data
4679   // sections.
4680   void
set_is_large_data_segment()4681   set_is_large_data_segment()
4682   { this->is_large_data_segment_ = true; }
4683 
4684   bool
is_unique_segment()4685   is_unique_segment() const
4686   { return this->is_unique_segment_; }
4687 
4688   // Mark segment as unique, happens when linker plugins request that
4689   // certain input sections be mapped to unique segments.
4690   void
set_is_unique_segment()4691   set_is_unique_segment()
4692   { this->is_unique_segment_ = true; }
4693 
4694   // Return the maximum alignment of the Output_data.
4695   uint64_t
4696   maximum_alignment();
4697 
4698   // Add the Output_section OS to this PT_LOAD segment.  SEG_FLAGS is
4699   // the segment flags to use.
4700   void
4701   add_output_section_to_load(Layout* layout, Output_section* os,
4702 			     elfcpp::Elf_Word seg_flags);
4703 
4704   // Add the Output_section OS to this non-PT_LOAD segment.  SEG_FLAGS
4705   // is the segment flags to use.
4706   void
4707   add_output_section_to_nonload(Output_section* os,
4708 				elfcpp::Elf_Word seg_flags);
4709 
4710   // Remove an Output_section from this segment.  It is an error if it
4711   // is not present.
4712   void
4713   remove_output_section(Output_section* os);
4714 
4715   // Add an Output_data (which need not be an Output_section) to the
4716   // start of this segment.
4717   void
4718   add_initial_output_data(Output_data*);
4719 
4720   // Return true if this segment has any sections which hold actual
4721   // data, rather than being a BSS section.
4722   bool
4723   has_any_data_sections() const;
4724 
4725   // Whether this segment has a dynamic relocs.
4726   bool
4727   has_dynamic_reloc() const;
4728 
4729   // Return the first section.
4730   Output_section*
4731   first_section() const;
4732 
4733   // Return the address of the first section.
4734   uint64_t
first_section_load_address()4735   first_section_load_address() const
4736   {
4737     const Output_section* os = this->first_section();
4738     gold_assert(os != NULL);
4739     return os->has_load_address() ? os->load_address() : os->address();
4740   }
4741 
4742   // Return whether the addresses have been set already.
4743   bool
are_addresses_set()4744   are_addresses_set() const
4745   { return this->are_addresses_set_; }
4746 
4747   // Set the addresses.
4748   void
set_addresses(uint64_t vaddr,uint64_t paddr)4749   set_addresses(uint64_t vaddr, uint64_t paddr)
4750   {
4751     this->vaddr_ = vaddr;
4752     this->paddr_ = paddr;
4753     this->are_addresses_set_ = true;
4754   }
4755 
4756   // Update the flags for the flags of an output section added to this
4757   // segment.
4758   void
update_flags_for_output_section(elfcpp::Elf_Xword flags)4759   update_flags_for_output_section(elfcpp::Elf_Xword flags)
4760   {
4761     // The ELF ABI specifies that a PT_TLS segment should always have
4762     // PF_R as the flags.
4763     if (this->type() != elfcpp::PT_TLS)
4764       this->flags_ |= flags;
4765   }
4766 
4767   // Set the segment flags.  This is only used if we have a PHDRS
4768   // clause which explicitly specifies the flags.
4769   void
set_flags(elfcpp::Elf_Word flags)4770   set_flags(elfcpp::Elf_Word flags)
4771   { this->flags_ = flags; }
4772 
4773   // Set the address of the segment to ADDR and the offset to *POFF
4774   // and set the addresses and offsets of all contained output
4775   // sections accordingly.  Set the section indexes of all contained
4776   // output sections starting with *PSHNDX.  If RESET is true, first
4777   // reset the addresses of the contained sections.  Return the
4778   // address of the immediately following segment.  Update *POFF and
4779   // *PSHNDX.  This should only be called for a PT_LOAD segment.
4780   uint64_t
4781   set_section_addresses(const Target*, Layout*, bool reset, uint64_t addr,
4782 			unsigned int* increase_relro, bool* has_relro,
4783 			off_t* poff, unsigned int* pshndx);
4784 
4785   // Set the minimum alignment of this segment.  This may be adjusted
4786   // upward based on the section alignments.
4787   void
set_minimum_p_align(uint64_t align)4788   set_minimum_p_align(uint64_t align)
4789   {
4790     if (align > this->min_p_align_)
4791       this->min_p_align_ = align;
4792   }
4793 
4794   // Set the memory size of this segment.
4795   void
set_size(uint64_t size)4796   set_size(uint64_t size)
4797   {
4798     this->memsz_ = size;
4799   }
4800 
4801   // Set the offset of this segment based on the section.  This should
4802   // only be called for a non-PT_LOAD segment.
4803   void
4804   set_offset(unsigned int increase);
4805 
4806   // Set the TLS offsets of the sections contained in the PT_TLS segment.
4807   void
4808   set_tls_offsets();
4809 
4810   // Return the number of output sections.
4811   unsigned int
4812   output_section_count() const;
4813 
4814   // Return the section attached to the list segment with the lowest
4815   // load address.  This is used when handling a PHDRS clause in a
4816   // linker script.
4817   Output_section*
4818   section_with_lowest_load_address() const;
4819 
4820   // Write the segment header into *OPHDR.
4821   template<int size, bool big_endian>
4822   void
4823   write_header(elfcpp::Phdr_write<size, big_endian>*);
4824 
4825   // Write the section headers of associated sections into V.
4826   template<int size, bool big_endian>
4827   unsigned char*
4828   write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4829 			unsigned int* pshndx) const;
4830 
4831   // Print the output sections in the map file.
4832   void
4833   print_sections_to_mapfile(Mapfile*) const;
4834 
4835  private:
4836   typedef std::vector<Output_data*> Output_data_list;
4837 
4838   // Find the maximum alignment in an Output_data_list.
4839   static uint64_t
4840   maximum_alignment_list(const Output_data_list*);
4841 
4842   // Return whether the first data section is a relro section.
4843   bool
4844   is_first_section_relro() const;
4845 
4846   // Set the section addresses in an Output_data_list.
4847   uint64_t
4848   set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4849 			     uint64_t addr, off_t* poff, off_t* fpoff,
4850 			     unsigned int* pshndx, bool* in_tls);
4851 
4852   // Return the number of Output_sections in an Output_data_list.
4853   unsigned int
4854   output_section_count_list(const Output_data_list*) const;
4855 
4856   // Return whether an Output_data_list has a dynamic reloc.
4857   bool
4858   has_dynamic_reloc_list(const Output_data_list*) const;
4859 
4860   // Find the section with the lowest load address in an
4861   // Output_data_list.
4862   void
4863   lowest_load_address_in_list(const Output_data_list* pdl,
4864 			      Output_section** found,
4865 			      uint64_t* found_lma) const;
4866 
4867   // Find the first and last entries by address.
4868   void
4869   find_first_and_last_list(const Output_data_list* pdl,
4870 			   const Output_data** pfirst,
4871 			   const Output_data** plast) const;
4872 
4873   // Write the section headers in the list into V.
4874   template<int size, bool big_endian>
4875   unsigned char*
4876   write_section_headers_list(const Layout*, const Stringpool*,
4877 			     const Output_data_list*, unsigned char* v,
4878 			     unsigned int* pshdx) const;
4879 
4880   // Print a section list to the mapfile.
4881   void
4882   print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4883 
4884   // NOTE: We want to use the copy constructor.  Currently, shallow copy
4885   // works for us so we do not need to write our own copy constructor.
4886 
4887   // The list of output data attached to this segment.
4888   Output_data_list output_lists_[ORDER_MAX];
4889   // The segment virtual address.
4890   uint64_t vaddr_;
4891   // The segment physical address.
4892   uint64_t paddr_;
4893   // The size of the segment in memory.
4894   uint64_t memsz_;
4895   // The segment alignment.
4896   uint64_t align_;
4897   // The maximum section alignment.  The is_max_align_known_ field
4898   // indicates whether this has been finalized.
4899   uint64_t max_align_;
4900   // The required minimum value for the p_align field.  This is used
4901   // for PT_LOAD segments.  Note that this does not mean that
4902   // addresses should be aligned to this value; it means the p_paddr
4903   // and p_vaddr fields must be congruent modulo this value.  For
4904   // non-PT_LOAD segments, the dynamic linker works more efficiently
4905   // if the p_align field has the more conventional value, although it
4906   // can align as needed.
4907   uint64_t min_p_align_;
4908   // The offset of the segment data within the file.
4909   off_t offset_;
4910   // The size of the segment data in the file.
4911   off_t filesz_;
4912   // The segment type;
4913   elfcpp::Elf_Word type_;
4914   // The segment flags.
4915   elfcpp::Elf_Word flags_;
4916   // Whether we have finalized max_align_.
4917   bool is_max_align_known_ : 1;
4918   // Whether vaddr and paddr were set by a linker script.
4919   bool are_addresses_set_ : 1;
4920   // Whether this segment holds large data sections.
4921   bool is_large_data_segment_ : 1;
4922   // Whether this was marked as a unique segment via a linker plugin.
4923   bool is_unique_segment_ : 1;
4924 };
4925 
4926 } // End namespace gold.
4927 
4928 #endif // !defined(GOLD_OUTPUT_H)
4929