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