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