Home | History | Annotate | Download | only in gold
      1 // object.cc -- support for an object file for linking in gold
      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 #include "gold.h"
     24 
     25 #include <cerrno>
     26 #include <cstring>
     27 #include <cstdarg>
     28 #include "demangle.h"
     29 #include "libiberty.h"
     30 
     31 #include "gc.h"
     32 #include "target-select.h"
     33 #include "dwarf_reader.h"
     34 #include "layout.h"
     35 #include "output.h"
     36 #include "symtab.h"
     37 #include "cref.h"
     38 #include "reloc.h"
     39 #include "object.h"
     40 #include "dynobj.h"
     41 #include "plugin.h"
     42 #include "compressed_output.h"
     43 #include "incremental.h"
     44 
     45 namespace gold
     46 {
     47 
     48 // Struct Read_symbols_data.
     49 
     50 // Destroy any remaining File_view objects and buffers of decompressed
     51 // sections.
     52 
     53 Read_symbols_data::~Read_symbols_data()
     54 {
     55   if (this->section_headers != NULL)
     56     delete this->section_headers;
     57   if (this->section_names != NULL)
     58     delete this->section_names;
     59   if (this->symbols != NULL)
     60     delete this->symbols;
     61   if (this->symbol_names != NULL)
     62     delete this->symbol_names;
     63   if (this->versym != NULL)
     64     delete this->versym;
     65   if (this->verdef != NULL)
     66     delete this->verdef;
     67   if (this->verneed != NULL)
     68     delete this->verneed;
     69 }
     70 
     71 // Class Xindex.
     72 
     73 // Initialize the symtab_xindex_ array.  Find the SHT_SYMTAB_SHNDX
     74 // section and read it in.  SYMTAB_SHNDX is the index of the symbol
     75 // table we care about.
     76 
     77 template<int size, bool big_endian>
     78 void
     79 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
     80 {
     81   if (!this->symtab_xindex_.empty())
     82     return;
     83 
     84   gold_assert(symtab_shndx != 0);
     85 
     86   // Look through the sections in reverse order, on the theory that it
     87   // is more likely to be near the end than the beginning.
     88   unsigned int i = object->shnum();
     89   while (i > 0)
     90     {
     91       --i;
     92       if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
     93 	  && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
     94 	{
     95 	  this->read_symtab_xindex<size, big_endian>(object, i, NULL);
     96 	  return;
     97 	}
     98     }
     99 
    100   object->error(_("missing SHT_SYMTAB_SHNDX section"));
    101 }
    102 
    103 // Read in the symtab_xindex_ array, given the section index of the
    104 // SHT_SYMTAB_SHNDX section.  If PSHDRS is not NULL, it points at the
    105 // section headers.
    106 
    107 template<int size, bool big_endian>
    108 void
    109 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
    110 			   const unsigned char* pshdrs)
    111 {
    112   section_size_type bytecount;
    113   const unsigned char* contents;
    114   if (pshdrs == NULL)
    115     contents = object->section_contents(xindex_shndx, &bytecount, false);
    116   else
    117     {
    118       const unsigned char* p = (pshdrs
    119 				+ (xindex_shndx
    120 				   * elfcpp::Elf_sizes<size>::shdr_size));
    121       typename elfcpp::Shdr<size, big_endian> shdr(p);
    122       bytecount = convert_to_section_size_type(shdr.get_sh_size());
    123       contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
    124     }
    125 
    126   gold_assert(this->symtab_xindex_.empty());
    127   this->symtab_xindex_.reserve(bytecount / 4);
    128   for (section_size_type i = 0; i < bytecount; i += 4)
    129     {
    130       unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
    131       // We preadjust the section indexes we save.
    132       this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
    133     }
    134 }
    135 
    136 // Symbol symndx has a section of SHN_XINDEX; return the real section
    137 // index.
    138 
    139 unsigned int
    140 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
    141 {
    142   if (symndx >= this->symtab_xindex_.size())
    143     {
    144       object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
    145 		    symndx);
    146       return elfcpp::SHN_UNDEF;
    147     }
    148   unsigned int shndx = this->symtab_xindex_[symndx];
    149   if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
    150     {
    151       object->error(_("extended index for symbol %u out of range: %u"),
    152 		    symndx, shndx);
    153       return elfcpp::SHN_UNDEF;
    154     }
    155   return shndx;
    156 }
    157 
    158 // Class Object.
    159 
    160 // Report an error for this object file.  This is used by the
    161 // elfcpp::Elf_file interface, and also called by the Object code
    162 // itself.
    163 
    164 void
    165 Object::error(const char* format, ...) const
    166 {
    167   va_list args;
    168   va_start(args, format);
    169   char* buf = NULL;
    170   if (vasprintf(&buf, format, args) < 0)
    171     gold_nomem();
    172   va_end(args);
    173   gold_error(_("%s: %s"), this->name().c_str(), buf);
    174   free(buf);
    175 }
    176 
    177 // Return a view of the contents of a section.
    178 
    179 const unsigned char*
    180 Object::section_contents(unsigned int shndx, section_size_type* plen,
    181 			 bool cache)
    182 { return this->do_section_contents(shndx, plen, cache); }
    183 
    184 // Read the section data into SD.  This is code common to Sized_relobj_file
    185 // and Sized_dynobj, so we put it into Object.
    186 
    187 template<int size, bool big_endian>
    188 void
    189 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
    190 			  Read_symbols_data* sd)
    191 {
    192   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
    193 
    194   // Read the section headers.
    195   const off_t shoff = elf_file->shoff();
    196   const unsigned int shnum = this->shnum();
    197   sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
    198 					       true, true);
    199 
    200   // Read the section names.
    201   const unsigned char* pshdrs = sd->section_headers->data();
    202   const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
    203   typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
    204 
    205   if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
    206     this->error(_("section name section has wrong type: %u"),
    207 		static_cast<unsigned int>(shdrnames.get_sh_type()));
    208 
    209   sd->section_names_size =
    210     convert_to_section_size_type(shdrnames.get_sh_size());
    211   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
    212 					     sd->section_names_size, false,
    213 					     false);
    214 }
    215 
    216 // If NAME is the name of a special .gnu.warning section, arrange for
    217 // the warning to be issued.  SHNDX is the section index.  Return
    218 // whether it is a warning section.
    219 
    220 bool
    221 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
    222 				   Symbol_table* symtab)
    223 {
    224   const char warn_prefix[] = ".gnu.warning.";
    225   const int warn_prefix_len = sizeof warn_prefix - 1;
    226   if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
    227     {
    228       // Read the section contents to get the warning text.  It would
    229       // be nicer if we only did this if we have to actually issue a
    230       // warning.  Unfortunately, warnings are issued as we relocate
    231       // sections.  That means that we can not lock the object then,
    232       // as we might try to issue the same warning multiple times
    233       // simultaneously.
    234       section_size_type len;
    235       const unsigned char* contents = this->section_contents(shndx, &len,
    236 							     false);
    237       if (len == 0)
    238 	{
    239 	  const char* warning = name + warn_prefix_len;
    240 	  contents = reinterpret_cast<const unsigned char*>(warning);
    241 	  len = strlen(warning);
    242 	}
    243       std::string warning(reinterpret_cast<const char*>(contents), len);
    244       symtab->add_warning(name + warn_prefix_len, this, warning);
    245       return true;
    246     }
    247   return false;
    248 }
    249 
    250 // If NAME is the name of the special section which indicates that
    251 // this object was compiled with -fsplit-stack, mark it accordingly.
    252 
    253 bool
    254 Object::handle_split_stack_section(const char* name)
    255 {
    256   if (strcmp(name, ".note.GNU-split-stack") == 0)
    257     {
    258       this->uses_split_stack_ = true;
    259       return true;
    260     }
    261   if (strcmp(name, ".note.GNU-no-split-stack") == 0)
    262     {
    263       this->has_no_split_stack_ = true;
    264       return true;
    265     }
    266   return false;
    267 }
    268 
    269 // Class Relobj
    270 
    271 // To copy the symbols data read from the file to a local data structure.
    272 // This function is called from do_layout only while doing garbage
    273 // collection.
    274 
    275 void
    276 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
    277 			  unsigned int section_header_size)
    278 {
    279   gc_sd->section_headers_data =
    280 	 new unsigned char[(section_header_size)];
    281   memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
    282 	 section_header_size);
    283   gc_sd->section_names_data =
    284 	 new unsigned char[sd->section_names_size];
    285   memcpy(gc_sd->section_names_data, sd->section_names->data(),
    286 	 sd->section_names_size);
    287   gc_sd->section_names_size = sd->section_names_size;
    288   if (sd->symbols != NULL)
    289     {
    290       gc_sd->symbols_data =
    291 	     new unsigned char[sd->symbols_size];
    292       memcpy(gc_sd->symbols_data, sd->symbols->data(),
    293 	    sd->symbols_size);
    294     }
    295   else
    296     {
    297       gc_sd->symbols_data = NULL;
    298     }
    299   gc_sd->symbols_size = sd->symbols_size;
    300   gc_sd->external_symbols_offset = sd->external_symbols_offset;
    301   if (sd->symbol_names != NULL)
    302     {
    303       gc_sd->symbol_names_data =
    304 	     new unsigned char[sd->symbol_names_size];
    305       memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
    306 	    sd->symbol_names_size);
    307     }
    308   else
    309     {
    310       gc_sd->symbol_names_data = NULL;
    311     }
    312   gc_sd->symbol_names_size = sd->symbol_names_size;
    313 }
    314 
    315 // This function determines if a particular section name must be included
    316 // in the link.  This is used during garbage collection to determine the
    317 // roots of the worklist.
    318 
    319 bool
    320 Relobj::is_section_name_included(const char* name)
    321 {
    322   if (is_prefix_of(".ctors", name)
    323       || is_prefix_of(".dtors", name)
    324       || is_prefix_of(".note", name)
    325       || is_prefix_of(".init", name)
    326       || is_prefix_of(".fini", name)
    327       || is_prefix_of(".gcc_except_table", name)
    328       || is_prefix_of(".jcr", name)
    329       || is_prefix_of(".preinit_array", name)
    330       || (is_prefix_of(".text", name)
    331 	  && strstr(name, "personality"))
    332       || (is_prefix_of(".data", name)
    333 	  && strstr(name, "personality"))
    334       || (is_prefix_of(".sdata", name)
    335 	  && strstr(name, "personality"))
    336       || (is_prefix_of(".gnu.linkonce.d", name)
    337 	  && strstr(name, "personality"))
    338       || (is_prefix_of(".rodata", name)
    339 	  && strstr(name, "nptl_version")))
    340     {
    341       return true;
    342     }
    343   return false;
    344 }
    345 
    346 // Finalize the incremental relocation information.  Allocates a block
    347 // of relocation entries for each symbol, and sets the reloc_bases_
    348 // array to point to the first entry in each block.  If CLEAR_COUNTS
    349 // is TRUE, also clear the per-symbol relocation counters.
    350 
    351 void
    352 Relobj::finalize_incremental_relocs(Layout* layout, bool clear_counts)
    353 {
    354   unsigned int nsyms = this->get_global_symbols()->size();
    355   this->reloc_bases_ = new unsigned int[nsyms];
    356 
    357   gold_assert(this->reloc_bases_ != NULL);
    358   gold_assert(layout->incremental_inputs() != NULL);
    359 
    360   unsigned int rindex = layout->incremental_inputs()->get_reloc_count();
    361   for (unsigned int i = 0; i < nsyms; ++i)
    362     {
    363       this->reloc_bases_[i] = rindex;
    364       rindex += this->reloc_counts_[i];
    365       if (clear_counts)
    366 	this->reloc_counts_[i] = 0;
    367     }
    368   layout->incremental_inputs()->set_reloc_count(rindex);
    369 }
    370 
    371 // Class Sized_relobj.
    372 
    373 // Iterate over local symbols, calling a visitor class V for each GOT offset
    374 // associated with a local symbol.
    375 
    376 template<int size, bool big_endian>
    377 void
    378 Sized_relobj<size, big_endian>::do_for_all_local_got_entries(
    379     Got_offset_list::Visitor* v) const
    380 {
    381   unsigned int nsyms = this->local_symbol_count();
    382   for (unsigned int i = 0; i < nsyms; i++)
    383     {
    384       Local_got_offsets::const_iterator p = this->local_got_offsets_.find(i);
    385       if (p != this->local_got_offsets_.end())
    386 	{
    387 	  const Got_offset_list* got_offsets = p->second;
    388 	  got_offsets->for_all_got_offsets(v);
    389 	}
    390     }
    391 }
    392 
    393 // Get the address of an output section.
    394 
    395 template<int size, bool big_endian>
    396 uint64_t
    397 Sized_relobj<size, big_endian>::do_output_section_address(
    398     unsigned int shndx)
    399 {
    400   // If the input file is linked as --just-symbols, the output
    401   // section address is the input section address.
    402   if (this->just_symbols())
    403     return this->section_address(shndx);
    404 
    405   const Output_section* os = this->do_output_section(shndx);
    406   gold_assert(os != NULL);
    407   return os->address();
    408 }
    409 
    410 // Class Sized_relobj_file.
    411 
    412 template<int size, bool big_endian>
    413 Sized_relobj_file<size, big_endian>::Sized_relobj_file(
    414     const std::string& name,
    415     Input_file* input_file,
    416     off_t offset,
    417     const elfcpp::Ehdr<size, big_endian>& ehdr)
    418   : Sized_relobj<size, big_endian>(name, input_file, offset),
    419     elf_file_(this, ehdr),
    420     symtab_shndx_(-1U),
    421     local_symbol_count_(0),
    422     output_local_symbol_count_(0),
    423     output_local_dynsym_count_(0),
    424     symbols_(),
    425     defined_count_(0),
    426     local_symbol_offset_(0),
    427     local_dynsym_offset_(0),
    428     local_values_(),
    429     local_plt_offsets_(),
    430     kept_comdat_sections_(),
    431     has_eh_frame_(false),
    432     discarded_eh_frame_shndx_(-1U),
    433     is_deferred_layout_(false),
    434     deferred_layout_(),
    435     deferred_layout_relocs_()
    436 {
    437   this->e_type_ = ehdr.get_e_type();
    438 }
    439 
    440 template<int size, bool big_endian>
    441 Sized_relobj_file<size, big_endian>::~Sized_relobj_file()
    442 {
    443 }
    444 
    445 // Set up an object file based on the file header.  This sets up the
    446 // section information.
    447 
    448 template<int size, bool big_endian>
    449 void
    450 Sized_relobj_file<size, big_endian>::do_setup()
    451 {
    452   const unsigned int shnum = this->elf_file_.shnum();
    453   this->set_shnum(shnum);
    454 }
    455 
    456 // Find the SHT_SYMTAB section, given the section headers.  The ELF
    457 // standard says that maybe in the future there can be more than one
    458 // SHT_SYMTAB section.  Until somebody figures out how that could
    459 // work, we assume there is only one.
    460 
    461 template<int size, bool big_endian>
    462 void
    463 Sized_relobj_file<size, big_endian>::find_symtab(const unsigned char* pshdrs)
    464 {
    465   const unsigned int shnum = this->shnum();
    466   this->symtab_shndx_ = 0;
    467   if (shnum > 0)
    468     {
    469       // Look through the sections in reverse order, since gas tends
    470       // to put the symbol table at the end.
    471       const unsigned char* p = pshdrs + shnum * This::shdr_size;
    472       unsigned int i = shnum;
    473       unsigned int xindex_shndx = 0;
    474       unsigned int xindex_link = 0;
    475       while (i > 0)
    476 	{
    477 	  --i;
    478 	  p -= This::shdr_size;
    479 	  typename This::Shdr shdr(p);
    480 	  if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
    481 	    {
    482 	      this->symtab_shndx_ = i;
    483 	      if (xindex_shndx > 0 && xindex_link == i)
    484 		{
    485 		  Xindex* xindex =
    486 		    new Xindex(this->elf_file_.large_shndx_offset());
    487 		  xindex->read_symtab_xindex<size, big_endian>(this,
    488 							       xindex_shndx,
    489 							       pshdrs);
    490 		  this->set_xindex(xindex);
    491 		}
    492 	      break;
    493 	    }
    494 
    495 	  // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
    496 	  // one.  This will work if it follows the SHT_SYMTAB
    497 	  // section.
    498 	  if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
    499 	    {
    500 	      xindex_shndx = i;
    501 	      xindex_link = this->adjust_shndx(shdr.get_sh_link());
    502 	    }
    503 	}
    504     }
    505 }
    506 
    507 // Return the Xindex structure to use for object with lots of
    508 // sections.
    509 
    510 template<int size, bool big_endian>
    511 Xindex*
    512 Sized_relobj_file<size, big_endian>::do_initialize_xindex()
    513 {
    514   gold_assert(this->symtab_shndx_ != -1U);
    515   Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
    516   xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
    517   return xindex;
    518 }
    519 
    520 // Return whether SHDR has the right type and flags to be a GNU
    521 // .eh_frame section.
    522 
    523 template<int size, bool big_endian>
    524 bool
    525 Sized_relobj_file<size, big_endian>::check_eh_frame_flags(
    526     const elfcpp::Shdr<size, big_endian>* shdr) const
    527 {
    528   elfcpp::Elf_Word sh_type = shdr->get_sh_type();
    529   return ((sh_type == elfcpp::SHT_PROGBITS
    530 	   || sh_type == elfcpp::SHT_X86_64_UNWIND)
    531 	  && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
    532 }
    533 
    534 // Find the section header with the given name.
    535 
    536 template<int size, bool big_endian>
    537 const unsigned char*
    538 Object::find_shdr(
    539     const unsigned char* pshdrs,
    540     const char* name,
    541     const char* names,
    542     section_size_type names_size,
    543     const unsigned char* hdr) const
    544 {
    545   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
    546   const unsigned int shnum = this->shnum();
    547   const unsigned char* hdr_end = pshdrs + shdr_size * shnum;
    548   size_t sh_name = 0;
    549 
    550   while (1)
    551     {
    552       if (hdr)
    553 	{
    554 	  // We found HDR last time we were called, continue looking.
    555 	  typename elfcpp::Shdr<size, big_endian> shdr(hdr);
    556 	  sh_name = shdr.get_sh_name();
    557 	}
    558       else
    559 	{
    560 	  // Look for the next occurrence of NAME in NAMES.
    561 	  // The fact that .shstrtab produced by current GNU tools is
    562 	  // string merged means we shouldn't have both .not.foo and
    563 	  // .foo in .shstrtab, and multiple .foo sections should all
    564 	  // have the same sh_name.  However, this is not guaranteed
    565 	  // by the ELF spec and not all ELF object file producers may
    566 	  // be so clever.
    567 	  size_t len = strlen(name) + 1;
    568 	  const char *p = sh_name ? names + sh_name + len : names;
    569 	  p = reinterpret_cast<const char*>(memmem(p, names_size - (p - names),
    570 						   name, len));
    571 	  if (p == NULL)
    572 	    return NULL;
    573 	  sh_name = p - names;
    574 	  hdr = pshdrs;
    575 	  if (sh_name == 0)
    576 	    return hdr;
    577 	}
    578 
    579       hdr += shdr_size;
    580       while (hdr < hdr_end)
    581 	{
    582 	  typename elfcpp::Shdr<size, big_endian> shdr(hdr);
    583 	  if (shdr.get_sh_name() == sh_name)
    584 	    return hdr;
    585 	  hdr += shdr_size;
    586 	}
    587       hdr = NULL;
    588       if (sh_name == 0)
    589 	return hdr;
    590     }
    591 }
    592 
    593 // Return whether there is a GNU .eh_frame section, given the section
    594 // headers and the section names.
    595 
    596 template<int size, bool big_endian>
    597 bool
    598 Sized_relobj_file<size, big_endian>::find_eh_frame(
    599     const unsigned char* pshdrs,
    600     const char* names,
    601     section_size_type names_size) const
    602 {
    603   const unsigned char* s = NULL;
    604 
    605   while (1)
    606     {
    607       s = this->template find_shdr<size, big_endian>(pshdrs, ".eh_frame",
    608 						     names, names_size, s);
    609       if (s == NULL)
    610 	return false;
    611 
    612       typename This::Shdr shdr(s);
    613       if (this->check_eh_frame_flags(&shdr))
    614 	return true;
    615     }
    616 }
    617 
    618 // Return TRUE if this is a section whose contents will be needed in the
    619 // Add_symbols task.  This function is only called for sections that have
    620 // already passed the test in is_compressed_debug_section(), so we know
    621 // that the section name begins with ".zdebug".
    622 
    623 static bool
    624 need_decompressed_section(const char* name)
    625 {
    626   // Skip over the ".zdebug" and a quick check for the "_".
    627   name += 7;
    628   if (*name++ != '_')
    629     return false;
    630 
    631 #ifdef ENABLE_THREADS
    632   // Decompressing these sections now will help only if we're
    633   // multithreaded.
    634   if (parameters->options().threads())
    635     {
    636       // We will need .zdebug_str if this is not an incremental link
    637       // (i.e., we are processing string merge sections) or if we need
    638       // to build a gdb index.
    639       if ((!parameters->incremental() || parameters->options().gdb_index())
    640 	  && strcmp(name, "str") == 0)
    641 	return true;
    642 
    643       // We will need these other sections when building a gdb index.
    644       if (parameters->options().gdb_index()
    645 	  && (strcmp(name, "info") == 0
    646 	      || strcmp(name, "types") == 0
    647 	      || strcmp(name, "pubnames") == 0
    648 	      || strcmp(name, "pubtypes") == 0
    649 	      || strcmp(name, "ranges") == 0
    650 	      || strcmp(name, "abbrev") == 0))
    651 	return true;
    652     }
    653 #endif
    654 
    655   // Even when single-threaded, we will need .zdebug_str if this is
    656   // not an incremental link and we are building a gdb index.
    657   // Otherwise, we would decompress the section twice: once for
    658   // string merge processing, and once for building the gdb index.
    659   if (!parameters->incremental()
    660       && parameters->options().gdb_index()
    661       && strcmp(name, "str") == 0)
    662     return true;
    663 
    664   return false;
    665 }
    666 
    667 // Build a table for any compressed debug sections, mapping each section index
    668 // to the uncompressed size and (if needed) the decompressed contents.
    669 
    670 template<int size, bool big_endian>
    671 Compressed_section_map*
    672 build_compressed_section_map(
    673     const unsigned char* pshdrs,
    674     unsigned int shnum,
    675     const char* names,
    676     section_size_type names_size,
    677     Object* obj,
    678     bool decompress_if_needed)
    679 {
    680   Compressed_section_map* uncompressed_map = new Compressed_section_map();
    681   const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
    682   const unsigned char* p = pshdrs + shdr_size;
    683 
    684   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
    685     {
    686       typename elfcpp::Shdr<size, big_endian> shdr(p);
    687       if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
    688 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
    689 	{
    690 	  if (shdr.get_sh_name() >= names_size)
    691 	    {
    692 	      obj->error(_("bad section name offset for section %u: %lu"),
    693 			 i, static_cast<unsigned long>(shdr.get_sh_name()));
    694 	      continue;
    695 	    }
    696 
    697 	  const char* name = names + shdr.get_sh_name();
    698 	  if (is_compressed_debug_section(name))
    699 	    {
    700 	      section_size_type len;
    701 	      const unsigned char* contents =
    702 		  obj->section_contents(i, &len, false);
    703 	      uint64_t uncompressed_size = get_uncompressed_size(contents, len);
    704 	      Compressed_section_info info;
    705 	      info.size = convert_to_section_size_type(uncompressed_size);
    706 	      info.contents = NULL;
    707 	      if (uncompressed_size != -1ULL)
    708 		{
    709 		  unsigned char* uncompressed_data = NULL;
    710 		  if (decompress_if_needed && need_decompressed_section(name))
    711 		    {
    712 		      uncompressed_data = new unsigned char[uncompressed_size];
    713 		      if (decompress_input_section(contents, len,
    714 						   uncompressed_data,
    715 						   uncompressed_size))
    716 			info.contents = uncompressed_data;
    717 		      else
    718 			delete[] uncompressed_data;
    719 		    }
    720 		  (*uncompressed_map)[i] = info;
    721 		}
    722 	    }
    723 	}
    724     }
    725   return uncompressed_map;
    726 }
    727 
    728 // Stash away info for a number of special sections.
    729 // Return true if any of the sections found require local symbols to be read.
    730 
    731 template<int size, bool big_endian>
    732 bool
    733 Sized_relobj_file<size, big_endian>::do_find_special_sections(
    734     Read_symbols_data* sd)
    735 {
    736   const unsigned char* const pshdrs = sd->section_headers->data();
    737   const unsigned char* namesu = sd->section_names->data();
    738   const char* names = reinterpret_cast<const char*>(namesu);
    739 
    740   if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
    741     this->has_eh_frame_ = true;
    742 
    743   if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL)
    744     {
    745       Compressed_section_map* compressed_sections =
    746 	  build_compressed_section_map<size, big_endian>(
    747 	      pshdrs, this->shnum(), names, sd->section_names_size, this, true);
    748       if (compressed_sections != NULL)
    749         this->set_compressed_sections(compressed_sections);
    750     }
    751 
    752   return (this->has_eh_frame_
    753 	  || (!parameters->options().relocatable()
    754 	      && parameters->options().gdb_index()
    755 	      && (memmem(names, sd->section_names_size, "debug_info", 12) == 0
    756 		  || memmem(names, sd->section_names_size, "debug_types",
    757 			    13) == 0)));
    758 }
    759 
    760 // Read the sections and symbols from an object file.
    761 
    762 template<int size, bool big_endian>
    763 void
    764 Sized_relobj_file<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
    765 {
    766   this->base_read_symbols(sd);
    767 }
    768 
    769 // Read the sections and symbols from an object file.  This is common
    770 // code for all target-specific overrides of do_read_symbols().
    771 
    772 template<int size, bool big_endian>
    773 void
    774 Sized_relobj_file<size, big_endian>::base_read_symbols(Read_symbols_data* sd)
    775 {
    776   this->read_section_data(&this->elf_file_, sd);
    777 
    778   const unsigned char* const pshdrs = sd->section_headers->data();
    779 
    780   this->find_symtab(pshdrs);
    781 
    782   bool need_local_symbols = this->do_find_special_sections(sd);
    783 
    784   sd->symbols = NULL;
    785   sd->symbols_size = 0;
    786   sd->external_symbols_offset = 0;
    787   sd->symbol_names = NULL;
    788   sd->symbol_names_size = 0;
    789 
    790   if (this->symtab_shndx_ == 0)
    791     {
    792       // No symbol table.  Weird but legal.
    793       return;
    794     }
    795 
    796   // Get the symbol table section header.
    797   typename This::Shdr symtabshdr(pshdrs
    798 				 + this->symtab_shndx_ * This::shdr_size);
    799   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
    800 
    801   // If this object has a .eh_frame section, or if building a .gdb_index
    802   // section and there is debug info, we need all the symbols.
    803   // Otherwise we only need the external symbols.  While it would be
    804   // simpler to just always read all the symbols, I've seen object
    805   // files with well over 2000 local symbols, which for a 64-bit
    806   // object file format is over 5 pages that we don't need to read
    807   // now.
    808 
    809   const int sym_size = This::sym_size;
    810   const unsigned int loccount = symtabshdr.get_sh_info();
    811   this->local_symbol_count_ = loccount;
    812   this->local_values_.resize(loccount);
    813   section_offset_type locsize = loccount * sym_size;
    814   off_t dataoff = symtabshdr.get_sh_offset();
    815   section_size_type datasize =
    816     convert_to_section_size_type(symtabshdr.get_sh_size());
    817   off_t extoff = dataoff + locsize;
    818   section_size_type extsize = datasize - locsize;
    819 
    820   off_t readoff = need_local_symbols ? dataoff : extoff;
    821   section_size_type readsize = need_local_symbols ? datasize : extsize;
    822 
    823   if (readsize == 0)
    824     {
    825       // No external symbols.  Also weird but also legal.
    826       return;
    827     }
    828 
    829   File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
    830 
    831   // Read the section header for the symbol names.
    832   unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
    833   if (strtab_shndx >= this->shnum())
    834     {
    835       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
    836       return;
    837     }
    838   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
    839   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
    840     {
    841       this->error(_("symbol table name section has wrong type: %u"),
    842 		  static_cast<unsigned int>(strtabshdr.get_sh_type()));
    843       return;
    844     }
    845 
    846   // Read the symbol names.
    847   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
    848 					       strtabshdr.get_sh_size(),
    849 					       false, true);
    850 
    851   sd->symbols = fvsymtab;
    852   sd->symbols_size = readsize;
    853   sd->external_symbols_offset = need_local_symbols ? locsize : 0;
    854   sd->symbol_names = fvstrtab;
    855   sd->symbol_names_size =
    856     convert_to_section_size_type(strtabshdr.get_sh_size());
    857 }
    858 
    859 // Return the section index of symbol SYM.  Set *VALUE to its value in
    860 // the object file.  Set *IS_ORDINARY if this is an ordinary section
    861 // index, not a special code between SHN_LORESERVE and SHN_HIRESERVE.
    862 // Note that for a symbol which is not defined in this object file,
    863 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
    864 // the final value of the symbol in the link.
    865 
    866 template<int size, bool big_endian>
    867 unsigned int
    868 Sized_relobj_file<size, big_endian>::symbol_section_and_value(unsigned int sym,
    869 							      Address* value,
    870 							      bool* is_ordinary)
    871 {
    872   section_size_type symbols_size;
    873   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
    874 							&symbols_size,
    875 							false);
    876 
    877   const size_t count = symbols_size / This::sym_size;
    878   gold_assert(sym < count);
    879 
    880   elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
    881   *value = elfsym.get_st_value();
    882 
    883   return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
    884 }
    885 
    886 // Return whether to include a section group in the link.  LAYOUT is
    887 // used to keep track of which section groups we have already seen.
    888 // INDEX is the index of the section group and SHDR is the section
    889 // header.  If we do not want to include this group, we set bits in
    890 // OMIT for each section which should be discarded.
    891 
    892 template<int size, bool big_endian>
    893 bool
    894 Sized_relobj_file<size, big_endian>::include_section_group(
    895     Symbol_table* symtab,
    896     Layout* layout,
    897     unsigned int index,
    898     const char* name,
    899     const unsigned char* shdrs,
    900     const char* section_names,
    901     section_size_type section_names_size,
    902     std::vector<bool>* omit)
    903 {
    904   // Read the section contents.
    905   typename This::Shdr shdr(shdrs + index * This::shdr_size);
    906   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
    907 					     shdr.get_sh_size(), true, false);
    908   const elfcpp::Elf_Word* pword =
    909     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
    910 
    911   // The first word contains flags.  We only care about COMDAT section
    912   // groups.  Other section groups are always included in the link
    913   // just like ordinary sections.
    914   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
    915 
    916   // Look up the group signature, which is the name of a symbol.  ELF
    917   // uses a symbol name because some group signatures are long, and
    918   // the name is generally already in the symbol table, so it makes
    919   // sense to put the long string just once in .strtab rather than in
    920   // both .strtab and .shstrtab.
    921 
    922   // Get the appropriate symbol table header (this will normally be
    923   // the single SHT_SYMTAB section, but in principle it need not be).
    924   const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
    925   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
    926 
    927   // Read the symbol table entry.
    928   unsigned int symndx = shdr.get_sh_info();
    929   if (symndx >= symshdr.get_sh_size() / This::sym_size)
    930     {
    931       this->error(_("section group %u info %u out of range"),
    932 		  index, symndx);
    933       return false;
    934     }
    935   off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
    936   const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
    937 					     false);
    938   elfcpp::Sym<size, big_endian> sym(psym);
    939 
    940   // Read the symbol table names.
    941   section_size_type symnamelen;
    942   const unsigned char* psymnamesu;
    943   psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
    944 				      &symnamelen, true);
    945   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
    946 
    947   // Get the section group signature.
    948   if (sym.get_st_name() >= symnamelen)
    949     {
    950       this->error(_("symbol %u name offset %u out of range"),
    951 		  symndx, sym.get_st_name());
    952       return false;
    953     }
    954 
    955   std::string signature(psymnames + sym.get_st_name());
    956 
    957   // It seems that some versions of gas will create a section group
    958   // associated with a section symbol, and then fail to give a name to
    959   // the section symbol.  In such a case, use the name of the section.
    960   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
    961     {
    962       bool is_ordinary;
    963       unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
    964 						      sym.get_st_shndx(),
    965 						      &is_ordinary);
    966       if (!is_ordinary || sym_shndx >= this->shnum())
    967 	{
    968 	  this->error(_("symbol %u invalid section index %u"),
    969 		      symndx, sym_shndx);
    970 	  return false;
    971 	}
    972       typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
    973       if (member_shdr.get_sh_name() < section_names_size)
    974 	signature = section_names + member_shdr.get_sh_name();
    975     }
    976 
    977   // Record this section group in the layout, and see whether we've already
    978   // seen one with the same signature.
    979   bool include_group;
    980   bool is_comdat;
    981   Kept_section* kept_section = NULL;
    982 
    983   if ((flags & elfcpp::GRP_COMDAT) == 0)
    984     {
    985       include_group = true;
    986       is_comdat = false;
    987     }
    988   else
    989     {
    990       include_group = layout->find_or_add_kept_section(signature,
    991 						       this, index, true,
    992 						       true, &kept_section);
    993       is_comdat = true;
    994     }
    995 
    996   if (is_comdat && include_group)
    997     {
    998       Incremental_inputs* incremental_inputs = layout->incremental_inputs();
    999       if (incremental_inputs != NULL)
   1000 	incremental_inputs->report_comdat_group(this, signature.c_str());
   1001     }
   1002 
   1003   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
   1004 
   1005   std::vector<unsigned int> shndxes;
   1006   bool relocate_group = include_group && parameters->options().relocatable();
   1007   if (relocate_group)
   1008     shndxes.reserve(count - 1);
   1009 
   1010   for (size_t i = 1; i < count; ++i)
   1011     {
   1012       elfcpp::Elf_Word shndx =
   1013 	this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
   1014 
   1015       if (relocate_group)
   1016 	shndxes.push_back(shndx);
   1017 
   1018       if (shndx >= this->shnum())
   1019 	{
   1020 	  this->error(_("section %u in section group %u out of range"),
   1021 		      shndx, index);
   1022 	  continue;
   1023 	}
   1024 
   1025       // Check for an earlier section number, since we're going to get
   1026       // it wrong--we may have already decided to include the section.
   1027       if (shndx < index)
   1028 	this->error(_("invalid section group %u refers to earlier section %u"),
   1029 		    index, shndx);
   1030 
   1031       // Get the name of the member section.
   1032       typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
   1033       if (member_shdr.get_sh_name() >= section_names_size)
   1034 	{
   1035 	  // This is an error, but it will be diagnosed eventually
   1036 	  // in do_layout, so we don't need to do anything here but
   1037 	  // ignore it.
   1038 	  continue;
   1039 	}
   1040       std::string mname(section_names + member_shdr.get_sh_name());
   1041 
   1042       if (include_group)
   1043 	{
   1044 	  if (is_comdat)
   1045 	    kept_section->add_comdat_section(mname, shndx,
   1046 					     member_shdr.get_sh_size());
   1047 	}
   1048       else
   1049 	{
   1050 	  (*omit)[shndx] = true;
   1051 
   1052 	  if (is_comdat)
   1053 	    {
   1054 	      Relobj* kept_object = kept_section->object();
   1055 	      if (kept_section->is_comdat())
   1056 		{
   1057 		  // Find the corresponding kept section, and store
   1058 		  // that info in the discarded section table.
   1059 		  unsigned int kept_shndx;
   1060 		  uint64_t kept_size;
   1061 		  if (kept_section->find_comdat_section(mname, &kept_shndx,
   1062 							&kept_size))
   1063 		    {
   1064 		      // We don't keep a mapping for this section if
   1065 		      // it has a different size.  The mapping is only
   1066 		      // used for relocation processing, and we don't
   1067 		      // want to treat the sections as similar if the
   1068 		      // sizes are different.  Checking the section
   1069 		      // size is the approach used by the GNU linker.
   1070 		      if (kept_size == member_shdr.get_sh_size())
   1071 			this->set_kept_comdat_section(shndx, kept_object,
   1072 						      kept_shndx);
   1073 		    }
   1074 		}
   1075 	      else
   1076 		{
   1077 		  // The existing section is a linkonce section.  Add
   1078 		  // a mapping if there is exactly one section in the
   1079 		  // group (which is true when COUNT == 2) and if it
   1080 		  // is the same size.
   1081 		  if (count == 2
   1082 		      && (kept_section->linkonce_size()
   1083 			  == member_shdr.get_sh_size()))
   1084 		    this->set_kept_comdat_section(shndx, kept_object,
   1085 						  kept_section->shndx());
   1086 		}
   1087 	    }
   1088 	}
   1089     }
   1090 
   1091   if (relocate_group)
   1092     layout->layout_group(symtab, this, index, name, signature.c_str(),
   1093 			 shdr, flags, &shndxes);
   1094 
   1095   return include_group;
   1096 }
   1097 
   1098 // Whether to include a linkonce section in the link.  NAME is the
   1099 // name of the section and SHDR is the section header.
   1100 
   1101 // Linkonce sections are a GNU extension implemented in the original
   1102 // GNU linker before section groups were defined.  The semantics are
   1103 // that we only include one linkonce section with a given name.  The
   1104 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
   1105 // where T is the type of section and SYMNAME is the name of a symbol.
   1106 // In an attempt to make linkonce sections interact well with section
   1107 // groups, we try to identify SYMNAME and use it like a section group
   1108 // signature.  We want to block section groups with that signature,
   1109 // but not other linkonce sections with that signature.  We also use
   1110 // the full name of the linkonce section as a normal section group
   1111 // signature.
   1112 
   1113 template<int size, bool big_endian>
   1114 bool
   1115 Sized_relobj_file<size, big_endian>::include_linkonce_section(
   1116     Layout* layout,
   1117     unsigned int index,
   1118     const char* name,
   1119     const elfcpp::Shdr<size, big_endian>& shdr)
   1120 {
   1121   typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
   1122   // In general the symbol name we want will be the string following
   1123   // the last '.'.  However, we have to handle the case of
   1124   // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
   1125   // some versions of gcc.  So we use a heuristic: if the name starts
   1126   // with ".gnu.linkonce.t.", we use everything after that.  Otherwise
   1127   // we look for the last '.'.  We can't always simply skip
   1128   // ".gnu.linkonce.X", because we have to deal with cases like
   1129   // ".gnu.linkonce.d.rel.ro.local".
   1130   const char* const linkonce_t = ".gnu.linkonce.t.";
   1131   const char* symname;
   1132   if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
   1133     symname = name + strlen(linkonce_t);
   1134   else
   1135     symname = strrchr(name, '.') + 1;
   1136   std::string sig1(symname);
   1137   std::string sig2(name);
   1138   Kept_section* kept1;
   1139   Kept_section* kept2;
   1140   bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
   1141 						   false, &kept1);
   1142   bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
   1143 						   true, &kept2);
   1144 
   1145   if (!include2)
   1146     {
   1147       // We are not including this section because we already saw the
   1148       // name of the section as a signature.  This normally implies
   1149       // that the kept section is another linkonce section.  If it is
   1150       // the same size, record it as the section which corresponds to
   1151       // this one.
   1152       if (kept2->object() != NULL
   1153 	  && !kept2->is_comdat()
   1154 	  && kept2->linkonce_size() == sh_size)
   1155 	this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
   1156     }
   1157   else if (!include1)
   1158     {
   1159       // The section is being discarded on the basis of its symbol
   1160       // name.  This means that the corresponding kept section was
   1161       // part of a comdat group, and it will be difficult to identify
   1162       // the specific section within that group that corresponds to
   1163       // this linkonce section.  We'll handle the simple case where
   1164       // the group has only one member section.  Otherwise, it's not
   1165       // worth the effort.
   1166       unsigned int kept_shndx;
   1167       uint64_t kept_size;
   1168       if (kept1->object() != NULL
   1169 	  && kept1->is_comdat()
   1170 	  && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
   1171 	  && kept_size == sh_size)
   1172 	this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
   1173     }
   1174   else
   1175     {
   1176       kept1->set_linkonce_size(sh_size);
   1177       kept2->set_linkonce_size(sh_size);
   1178     }
   1179 
   1180   return include1 && include2;
   1181 }
   1182 
   1183 // Layout an input section.
   1184 
   1185 template<int size, bool big_endian>
   1186 inline void
   1187 Sized_relobj_file<size, big_endian>::layout_section(
   1188     Layout* layout,
   1189     unsigned int shndx,
   1190     const char* name,
   1191     const typename This::Shdr& shdr,
   1192     unsigned int reloc_shndx,
   1193     unsigned int reloc_type)
   1194 {
   1195   off_t offset;
   1196   Output_section* os = layout->layout(this, shndx, name, shdr,
   1197 					  reloc_shndx, reloc_type, &offset);
   1198 
   1199   this->output_sections()[shndx] = os;
   1200   if (offset == -1)
   1201     this->section_offsets()[shndx] = invalid_address;
   1202   else
   1203     this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
   1204 
   1205   // If this section requires special handling, and if there are
   1206   // relocs that apply to it, then we must do the special handling
   1207   // before we apply the relocs.
   1208   if (offset == -1 && reloc_shndx != 0)
   1209     this->set_relocs_must_follow_section_writes();
   1210 }
   1211 
   1212 // Layout an input .eh_frame section.
   1213 
   1214 template<int size, bool big_endian>
   1215 void
   1216 Sized_relobj_file<size, big_endian>::layout_eh_frame_section(
   1217     Layout* layout,
   1218     const unsigned char* symbols_data,
   1219     section_size_type symbols_size,
   1220     const unsigned char* symbol_names_data,
   1221     section_size_type symbol_names_size,
   1222     unsigned int shndx,
   1223     const typename This::Shdr& shdr,
   1224     unsigned int reloc_shndx,
   1225     unsigned int reloc_type)
   1226 {
   1227   gold_assert(this->has_eh_frame_);
   1228 
   1229   off_t offset;
   1230   Output_section* os = layout->layout_eh_frame(this,
   1231 					       symbols_data,
   1232 					       symbols_size,
   1233 					       symbol_names_data,
   1234 					       symbol_names_size,
   1235 					       shndx,
   1236 					       shdr,
   1237 					       reloc_shndx,
   1238 					       reloc_type,
   1239 					       &offset);
   1240   this->output_sections()[shndx] = os;
   1241   if (os == NULL || offset == -1)
   1242     {
   1243       // An object can contain at most one section holding exception
   1244       // frame information.
   1245       gold_assert(this->discarded_eh_frame_shndx_ == -1U);
   1246       this->discarded_eh_frame_shndx_ = shndx;
   1247       this->section_offsets()[shndx] = invalid_address;
   1248     }
   1249   else
   1250     this->section_offsets()[shndx] = convert_types<Address, off_t>(offset);
   1251 
   1252   // If this section requires special handling, and if there are
   1253   // relocs that aply to it, then we must do the special handling
   1254   // before we apply the relocs.
   1255   if (os != NULL && offset == -1 && reloc_shndx != 0)
   1256     this->set_relocs_must_follow_section_writes();
   1257 }
   1258 
   1259 // Lay out the input sections.  We walk through the sections and check
   1260 // whether they should be included in the link.  If they should, we
   1261 // pass them to the Layout object, which will return an output section
   1262 // and an offset.
   1263 // This function is called twice sometimes, two passes, when mapping
   1264 // of input sections to output sections must be delayed.
   1265 // This is true for the following :
   1266 // * Garbage collection (--gc-sections): Some input sections will be
   1267 // discarded and hence the assignment must wait until the second pass.
   1268 // In the first pass,  it is for setting up some sections as roots to
   1269 // a work-list for --gc-sections and to do comdat processing.
   1270 // * Identical Code Folding (--icf=<safe,all>): Some input sections
   1271 // will be folded and hence the assignment must wait.
   1272 // * Using plugins to map some sections to unique segments: Mapping
   1273 // some sections to unique segments requires mapping them to unique
   1274 // output sections too.  This can be done via plugins now and this
   1275 // information is not available in the first pass.
   1276 
   1277 template<int size, bool big_endian>
   1278 void
   1279 Sized_relobj_file<size, big_endian>::do_layout(Symbol_table* symtab,
   1280 					       Layout* layout,
   1281 					       Read_symbols_data* sd)
   1282 {
   1283   const unsigned int shnum = this->shnum();
   1284 
   1285   /* Should this function be called twice?  */
   1286   bool is_two_pass = (parameters->options().gc_sections()
   1287 		      || parameters->options().icf_enabled()
   1288 		      || layout->is_unique_segment_for_sections_specified());
   1289 
   1290   /* Only one of is_pass_one and is_pass_two is true.  Both are false when
   1291      a two-pass approach is not needed.  */
   1292   bool is_pass_one = false;
   1293   bool is_pass_two = false;
   1294 
   1295   Symbols_data* gc_sd = NULL;
   1296 
   1297   /* Check if do_layout needs to be two-pass.  If so, find out which pass
   1298      should happen.  In the first pass, the data in sd is saved to be used
   1299      later in the second pass.  */
   1300   if (is_two_pass)
   1301     {
   1302       gc_sd = this->get_symbols_data();
   1303       if (gc_sd == NULL)
   1304 	{
   1305 	  gold_assert(sd != NULL);
   1306 	  is_pass_one = true;
   1307 	}
   1308       else
   1309 	{
   1310 	  if (parameters->options().gc_sections())
   1311 	    gold_assert(symtab->gc()->is_worklist_ready());
   1312 	  if (parameters->options().icf_enabled())
   1313 	    gold_assert(symtab->icf()->is_icf_ready());
   1314 	  is_pass_two = true;
   1315 	}
   1316     }
   1317 
   1318   if (shnum == 0)
   1319     return;
   1320 
   1321   if (is_pass_one)
   1322     {
   1323       // During garbage collection save the symbols data to use it when
   1324       // re-entering this function.
   1325       gc_sd = new Symbols_data;
   1326       this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
   1327       this->set_symbols_data(gc_sd);
   1328     }
   1329 
   1330   const unsigned char* section_headers_data = NULL;
   1331   section_size_type section_names_size;
   1332   const unsigned char* symbols_data = NULL;
   1333   section_size_type symbols_size;
   1334   const unsigned char* symbol_names_data = NULL;
   1335   section_size_type symbol_names_size;
   1336 
   1337   if (is_two_pass)
   1338     {
   1339       section_headers_data = gc_sd->section_headers_data;
   1340       section_names_size = gc_sd->section_names_size;
   1341       symbols_data = gc_sd->symbols_data;
   1342       symbols_size = gc_sd->symbols_size;
   1343       symbol_names_data = gc_sd->symbol_names_data;
   1344       symbol_names_size = gc_sd->symbol_names_size;
   1345     }
   1346   else
   1347     {
   1348       section_headers_data = sd->section_headers->data();
   1349       section_names_size = sd->section_names_size;
   1350       if (sd->symbols != NULL)
   1351 	symbols_data = sd->symbols->data();
   1352       symbols_size = sd->symbols_size;
   1353       if (sd->symbol_names != NULL)
   1354 	symbol_names_data = sd->symbol_names->data();
   1355       symbol_names_size = sd->symbol_names_size;
   1356     }
   1357 
   1358   // Get the section headers.
   1359   const unsigned char* shdrs = section_headers_data;
   1360   const unsigned char* pshdrs;
   1361 
   1362   // Get the section names.
   1363   const unsigned char* pnamesu = (is_two_pass
   1364 				  ? gc_sd->section_names_data
   1365 				  : sd->section_names->data());
   1366 
   1367   const char* pnames = reinterpret_cast<const char*>(pnamesu);
   1368 
   1369   // If any input files have been claimed by plugins, we need to defer
   1370   // actual layout until the replacement files have arrived.
   1371   const bool should_defer_layout =
   1372       (parameters->options().has_plugins()
   1373        && parameters->options().plugins()->should_defer_layout());
   1374   unsigned int num_sections_to_defer = 0;
   1375 
   1376   // For each section, record the index of the reloc section if any.
   1377   // Use 0 to mean that there is no reloc section, -1U to mean that
   1378   // there is more than one.
   1379   std::vector<unsigned int> reloc_shndx(shnum, 0);
   1380   std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
   1381   // Skip the first, dummy, section.
   1382   pshdrs = shdrs + This::shdr_size;
   1383   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
   1384     {
   1385       typename This::Shdr shdr(pshdrs);
   1386 
   1387       // Count the number of sections whose layout will be deferred.
   1388       if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
   1389 	++num_sections_to_defer;
   1390 
   1391       unsigned int sh_type = shdr.get_sh_type();
   1392       if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
   1393 	{
   1394 	  unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
   1395 	  if (target_shndx == 0 || target_shndx >= shnum)
   1396 	    {
   1397 	      this->error(_("relocation section %u has bad info %u"),
   1398 			  i, target_shndx);
   1399 	      continue;
   1400 	    }
   1401 
   1402 	  if (reloc_shndx[target_shndx] != 0)
   1403 	    reloc_shndx[target_shndx] = -1U;
   1404 	  else
   1405 	    {
   1406 	      reloc_shndx[target_shndx] = i;
   1407 	      reloc_type[target_shndx] = sh_type;
   1408 	    }
   1409 	}
   1410     }
   1411 
   1412   Output_sections& out_sections(this->output_sections());
   1413   std::vector<Address>& out_section_offsets(this->section_offsets());
   1414 
   1415   if (!is_pass_two)
   1416     {
   1417       out_sections.resize(shnum);
   1418       out_section_offsets.resize(shnum);
   1419     }
   1420 
   1421   // If we are only linking for symbols, then there is nothing else to
   1422   // do here.
   1423   if (this->input_file()->just_symbols())
   1424     {
   1425       if (!is_pass_two)
   1426 	{
   1427 	  delete sd->section_headers;
   1428 	  sd->section_headers = NULL;
   1429 	  delete sd->section_names;
   1430 	  sd->section_names = NULL;
   1431 	}
   1432       return;
   1433     }
   1434 
   1435   if (num_sections_to_defer > 0)
   1436     {
   1437       parameters->options().plugins()->add_deferred_layout_object(this);
   1438       this->deferred_layout_.reserve(num_sections_to_defer);
   1439       this->is_deferred_layout_ = true;
   1440     }
   1441 
   1442   // Whether we've seen a .note.GNU-stack section.
   1443   bool seen_gnu_stack = false;
   1444   // The flags of a .note.GNU-stack section.
   1445   uint64_t gnu_stack_flags = 0;
   1446 
   1447   // Keep track of which sections to omit.
   1448   std::vector<bool> omit(shnum, false);
   1449 
   1450   // Keep track of reloc sections when emitting relocations.
   1451   const bool relocatable = parameters->options().relocatable();
   1452   const bool emit_relocs = (relocatable
   1453 			    || parameters->options().emit_relocs());
   1454   std::vector<unsigned int> reloc_sections;
   1455 
   1456   // Keep track of .eh_frame sections.
   1457   std::vector<unsigned int> eh_frame_sections;
   1458 
   1459   // Keep track of .debug_info and .debug_types sections.
   1460   std::vector<unsigned int> debug_info_sections;
   1461   std::vector<unsigned int> debug_types_sections;
   1462 
   1463   // Skip the first, dummy, section.
   1464   pshdrs = shdrs + This::shdr_size;
   1465   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
   1466     {
   1467       typename This::Shdr shdr(pshdrs);
   1468 
   1469       if (shdr.get_sh_name() >= section_names_size)
   1470 	{
   1471 	  this->error(_("bad section name offset for section %u: %lu"),
   1472 		      i, static_cast<unsigned long>(shdr.get_sh_name()));
   1473 	  return;
   1474 	}
   1475 
   1476       const char* name = pnames + shdr.get_sh_name();
   1477 
   1478       if (!is_pass_two)
   1479 	{
   1480 	  if (this->handle_gnu_warning_section(name, i, symtab))
   1481 	    {
   1482 	      if (!relocatable && !parameters->options().shared())
   1483 		omit[i] = true;
   1484 	    }
   1485 
   1486 	  // The .note.GNU-stack section is special.  It gives the
   1487 	  // protection flags that this object file requires for the stack
   1488 	  // in memory.
   1489 	  if (strcmp(name, ".note.GNU-stack") == 0)
   1490 	    {
   1491 	      seen_gnu_stack = true;
   1492 	      gnu_stack_flags |= shdr.get_sh_flags();
   1493 	      omit[i] = true;
   1494 	    }
   1495 
   1496 	  // The .note.GNU-split-stack section is also special.  It
   1497 	  // indicates that the object was compiled with
   1498 	  // -fsplit-stack.
   1499 	  if (this->handle_split_stack_section(name))
   1500 	    {
   1501 	      if (!relocatable && !parameters->options().shared())
   1502 		omit[i] = true;
   1503 	    }
   1504 
   1505 	  // Skip attributes section.
   1506 	  if (parameters->target().is_attributes_section(name))
   1507 	    {
   1508 	      omit[i] = true;
   1509 	    }
   1510 
   1511 	  bool discard = omit[i];
   1512 	  if (!discard)
   1513 	    {
   1514 	      if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
   1515 		{
   1516 		  if (!this->include_section_group(symtab, layout, i, name,
   1517 						   shdrs, pnames,
   1518 						   section_names_size,
   1519 						   &omit))
   1520 		    discard = true;
   1521 		}
   1522 	      else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
   1523 		       && Layout::is_linkonce(name))
   1524 		{
   1525 		  if (!this->include_linkonce_section(layout, i, name, shdr))
   1526 		    discard = true;
   1527 		}
   1528 	    }
   1529 
   1530 	  // Add the section to the incremental inputs layout.
   1531 	  Incremental_inputs* incremental_inputs = layout->incremental_inputs();
   1532 	  if (incremental_inputs != NULL
   1533 	      && !discard
   1534 	      && can_incremental_update(shdr.get_sh_type()))
   1535 	    {
   1536 	      off_t sh_size = shdr.get_sh_size();
   1537 	      section_size_type uncompressed_size;
   1538 	      if (this->section_is_compressed(i, &uncompressed_size))
   1539 		sh_size = uncompressed_size;
   1540 	      incremental_inputs->report_input_section(this, i, name, sh_size);
   1541 	    }
   1542 
   1543 	  if (discard)
   1544 	    {
   1545 	      // Do not include this section in the link.
   1546 	      out_sections[i] = NULL;
   1547 	      out_section_offsets[i] = invalid_address;
   1548 	      continue;
   1549 	    }
   1550 	}
   1551 
   1552       if (is_pass_one && parameters->options().gc_sections())
   1553 	{
   1554 	  if (this->is_section_name_included(name)
   1555 	      || layout->keep_input_section (this, name)
   1556 	      || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
   1557 	      || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
   1558 	    {
   1559 	      symtab->gc()->worklist().push(Section_id(this, i));
   1560 	    }
   1561 	  // If the section name XXX can be represented as a C identifier
   1562 	  // it cannot be discarded if there are references to
   1563 	  // __start_XXX and __stop_XXX symbols.  These need to be
   1564 	  // specially handled.
   1565 	  if (is_cident(name))
   1566 	    {
   1567 	      symtab->gc()->add_cident_section(name, Section_id(this, i));
   1568 	    }
   1569 	}
   1570 
   1571       // When doing a relocatable link we are going to copy input
   1572       // reloc sections into the output.  We only want to copy the
   1573       // ones associated with sections which are not being discarded.
   1574       // However, we don't know that yet for all sections.  So save
   1575       // reloc sections and process them later. Garbage collection is
   1576       // not triggered when relocatable code is desired.
   1577       if (emit_relocs
   1578 	  && (shdr.get_sh_type() == elfcpp::SHT_REL
   1579 	      || shdr.get_sh_type() == elfcpp::SHT_RELA))
   1580 	{
   1581 	  reloc_sections.push_back(i);
   1582 	  continue;
   1583 	}
   1584 
   1585       if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
   1586 	continue;
   1587 
   1588       // The .eh_frame section is special.  It holds exception frame
   1589       // information that we need to read in order to generate the
   1590       // exception frame header.  We process these after all the other
   1591       // sections so that the exception frame reader can reliably
   1592       // determine which sections are being discarded, and discard the
   1593       // corresponding information.
   1594       if (!relocatable
   1595 	  && strcmp(name, ".eh_frame") == 0
   1596 	  && this->check_eh_frame_flags(&shdr))
   1597 	{
   1598 	  if (is_pass_one)
   1599 	    {
   1600 	      if (this->is_deferred_layout())
   1601 		out_sections[i] = reinterpret_cast<Output_section*>(2);
   1602 	      else
   1603 		out_sections[i] = reinterpret_cast<Output_section*>(1);
   1604 	      out_section_offsets[i] = invalid_address;
   1605 	    }
   1606 	  else if (this->is_deferred_layout())
   1607 	    this->deferred_layout_.push_back(Deferred_layout(i, name,
   1608 							     pshdrs,
   1609 							     reloc_shndx[i],
   1610 							     reloc_type[i]));
   1611 	  else
   1612 	    eh_frame_sections.push_back(i);
   1613 	  continue;
   1614 	}
   1615 
   1616       if (is_pass_two && parameters->options().gc_sections())
   1617 	{
   1618 	  // This is executed during the second pass of garbage
   1619 	  // collection. do_layout has been called before and some
   1620 	  // sections have been already discarded. Simply ignore
   1621 	  // such sections this time around.
   1622 	  if (out_sections[i] == NULL)
   1623 	    {
   1624 	      gold_assert(out_section_offsets[i] == invalid_address);
   1625 	      continue;
   1626 	    }
   1627 	  if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
   1628 	      && symtab->gc()->is_section_garbage(this, i))
   1629 	      {
   1630 		if (parameters->options().print_gc_sections())
   1631 		  gold_info(_("%s: removing unused section from '%s'"
   1632 			      " in file '%s'"),
   1633 			    program_name, this->section_name(i).c_str(),
   1634 			    this->name().c_str());
   1635 		out_sections[i] = NULL;
   1636 		out_section_offsets[i] = invalid_address;
   1637 		continue;
   1638 	      }
   1639 	}
   1640 
   1641       if (is_pass_two && parameters->options().icf_enabled())
   1642 	{
   1643 	  if (out_sections[i] == NULL)
   1644 	    {
   1645 	      gold_assert(out_section_offsets[i] == invalid_address);
   1646 	      continue;
   1647 	    }
   1648 	  if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
   1649 	      && symtab->icf()->is_section_folded(this, i))
   1650 	      {
   1651 		if (parameters->options().print_icf_sections())
   1652 		  {
   1653 		    Section_id folded =
   1654 				symtab->icf()->get_folded_section(this, i);
   1655 		    Relobj* folded_obj =
   1656 				reinterpret_cast<Relobj*>(folded.first);
   1657 		    gold_info(_("%s: ICF folding section '%s' in file '%s' "
   1658 				"into '%s' in file '%s'"),
   1659 			      program_name, this->section_name(i).c_str(),
   1660 			      this->name().c_str(),
   1661 			      folded_obj->section_name(folded.second).c_str(),
   1662 			      folded_obj->name().c_str());
   1663 		  }
   1664 		out_sections[i] = NULL;
   1665 		out_section_offsets[i] = invalid_address;
   1666 		continue;
   1667 	      }
   1668 	}
   1669 
   1670       // Defer layout here if input files are claimed by plugins.  When gc
   1671       // is turned on this function is called twice; we only want to do this
   1672       // on the first pass.
   1673       if (!is_pass_two
   1674 	  && this->is_deferred_layout()
   1675 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
   1676 	{
   1677 	  this->deferred_layout_.push_back(Deferred_layout(i, name,
   1678 							   pshdrs,
   1679 							   reloc_shndx[i],
   1680 							   reloc_type[i]));
   1681 	  // Put dummy values here; real values will be supplied by
   1682 	  // do_layout_deferred_sections.
   1683 	  out_sections[i] = reinterpret_cast<Output_section*>(2);
   1684 	  out_section_offsets[i] = invalid_address;
   1685 	  continue;
   1686 	}
   1687 
   1688       // During gc_pass_two if a section that was previously deferred is
   1689       // found, do not layout the section as layout_deferred_sections will
   1690       // do it later from gold.cc.
   1691       if (is_pass_two
   1692 	  && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
   1693 	continue;
   1694 
   1695       if (is_pass_one)
   1696 	{
   1697 	  // This is during garbage collection. The out_sections are
   1698 	  // assigned in the second call to this function.
   1699 	  out_sections[i] = reinterpret_cast<Output_section*>(1);
   1700 	  out_section_offsets[i] = invalid_address;
   1701 	}
   1702       else
   1703 	{
   1704 	  // When garbage collection is switched on the actual layout
   1705 	  // only happens in the second call.
   1706 	  this->layout_section(layout, i, name, shdr, reloc_shndx[i],
   1707 			       reloc_type[i]);
   1708 
   1709 	  // When generating a .gdb_index section, we do additional
   1710 	  // processing of .debug_info and .debug_types sections after all
   1711 	  // the other sections for the same reason as above.
   1712 	  if (!relocatable
   1713 	      && parameters->options().gdb_index()
   1714 	      && !(shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
   1715 	    {
   1716 	      if (strcmp(name, ".debug_info") == 0
   1717 		  || strcmp(name, ".zdebug_info") == 0)
   1718 		debug_info_sections.push_back(i);
   1719 	      else if (strcmp(name, ".debug_types") == 0
   1720 		       || strcmp(name, ".zdebug_types") == 0)
   1721 		debug_types_sections.push_back(i);
   1722 	    }
   1723 	}
   1724     }
   1725 
   1726   if (!is_pass_two)
   1727     layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags, this);
   1728 
   1729   // Handle the .eh_frame sections after the other sections.
   1730   gold_assert(!is_pass_one || eh_frame_sections.empty());
   1731   for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
   1732        p != eh_frame_sections.end();
   1733        ++p)
   1734     {
   1735       unsigned int i = *p;
   1736       const unsigned char* pshdr;
   1737       pshdr = section_headers_data + i * This::shdr_size;
   1738       typename This::Shdr shdr(pshdr);
   1739 
   1740       this->layout_eh_frame_section(layout,
   1741 				    symbols_data,
   1742 				    symbols_size,
   1743 				    symbol_names_data,
   1744 				    symbol_names_size,
   1745 				    i,
   1746 				    shdr,
   1747 				    reloc_shndx[i],
   1748 				    reloc_type[i]);
   1749     }
   1750 
   1751   // When doing a relocatable link handle the reloc sections at the
   1752   // end.  Garbage collection  and Identical Code Folding is not
   1753   // turned on for relocatable code.
   1754   if (emit_relocs)
   1755     this->size_relocatable_relocs();
   1756 
   1757   gold_assert(!is_two_pass || reloc_sections.empty());
   1758 
   1759   for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
   1760        p != reloc_sections.end();
   1761        ++p)
   1762     {
   1763       unsigned int i = *p;
   1764       const unsigned char* pshdr;
   1765       pshdr = section_headers_data + i * This::shdr_size;
   1766       typename This::Shdr shdr(pshdr);
   1767 
   1768       unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
   1769       if (data_shndx >= shnum)
   1770 	{
   1771 	  // We already warned about this above.
   1772 	  continue;
   1773 	}
   1774 
   1775       Output_section* data_section = out_sections[data_shndx];
   1776       if (data_section == reinterpret_cast<Output_section*>(2))
   1777 	{
   1778 	  if (is_pass_two)
   1779 	    continue;
   1780 	  // The layout for the data section was deferred, so we need
   1781 	  // to defer the relocation section, too.
   1782 	  const char* name = pnames + shdr.get_sh_name();
   1783 	  this->deferred_layout_relocs_.push_back(
   1784 	      Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
   1785 	  out_sections[i] = reinterpret_cast<Output_section*>(2);
   1786 	  out_section_offsets[i] = invalid_address;
   1787 	  continue;
   1788 	}
   1789       if (data_section == NULL)
   1790 	{
   1791 	  out_sections[i] = NULL;
   1792 	  out_section_offsets[i] = invalid_address;
   1793 	  continue;
   1794 	}
   1795 
   1796       Relocatable_relocs* rr = new Relocatable_relocs();
   1797       this->set_relocatable_relocs(i, rr);
   1798 
   1799       Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
   1800 						rr);
   1801       out_sections[i] = os;
   1802       out_section_offsets[i] = invalid_address;
   1803     }
   1804 
   1805   // When building a .gdb_index section, scan the .debug_info and
   1806   // .debug_types sections.
   1807   gold_assert(!is_pass_one
   1808 	      || (debug_info_sections.empty() && debug_types_sections.empty()));
   1809   for (std::vector<unsigned int>::const_iterator p
   1810 	   = debug_info_sections.begin();
   1811        p != debug_info_sections.end();
   1812        ++p)
   1813     {
   1814       unsigned int i = *p;
   1815       layout->add_to_gdb_index(false, this, symbols_data, symbols_size,
   1816 			       i, reloc_shndx[i], reloc_type[i]);
   1817     }
   1818   for (std::vector<unsigned int>::const_iterator p
   1819 	   = debug_types_sections.begin();
   1820        p != debug_types_sections.end();
   1821        ++p)
   1822     {
   1823       unsigned int i = *p;
   1824       layout->add_to_gdb_index(true, this, symbols_data, symbols_size,
   1825 			       i, reloc_shndx[i], reloc_type[i]);
   1826     }
   1827 
   1828   if (is_pass_two)
   1829     {
   1830       delete[] gc_sd->section_headers_data;
   1831       delete[] gc_sd->section_names_data;
   1832       delete[] gc_sd->symbols_data;
   1833       delete[] gc_sd->symbol_names_data;
   1834       this->set_symbols_data(NULL);
   1835     }
   1836   else
   1837     {
   1838       delete sd->section_headers;
   1839       sd->section_headers = NULL;
   1840       delete sd->section_names;
   1841       sd->section_names = NULL;
   1842     }
   1843 }
   1844 
   1845 // Layout sections whose layout was deferred while waiting for
   1846 // input files from a plugin.
   1847 
   1848 template<int size, bool big_endian>
   1849 void
   1850 Sized_relobj_file<size, big_endian>::do_layout_deferred_sections(Layout* layout)
   1851 {
   1852   typename std::vector<Deferred_layout>::iterator deferred;
   1853 
   1854   for (deferred = this->deferred_layout_.begin();
   1855        deferred != this->deferred_layout_.end();
   1856        ++deferred)
   1857     {
   1858       typename This::Shdr shdr(deferred->shdr_data_);
   1859 
   1860       if (!parameters->options().relocatable()
   1861 	  && deferred->name_ == ".eh_frame"
   1862 	  && this->check_eh_frame_flags(&shdr))
   1863 	{
   1864 	  // Checking is_section_included is not reliable for
   1865 	  // .eh_frame sections, because they do not have an output
   1866 	  // section.  This is not a problem normally because we call
   1867 	  // layout_eh_frame_section unconditionally, but when
   1868 	  // deferring sections that is not true.  We don't want to
   1869 	  // keep all .eh_frame sections because that will cause us to
   1870 	  // keep all sections that they refer to, which is the wrong
   1871 	  // way around.  Instead, the eh_frame code will discard
   1872 	  // .eh_frame sections that refer to discarded sections.
   1873 
   1874 	  // Reading the symbols again here may be slow.
   1875 	  Read_symbols_data sd;
   1876 	  this->base_read_symbols(&sd);
   1877 	  this->layout_eh_frame_section(layout,
   1878 					sd.symbols->data(),
   1879 					sd.symbols_size,
   1880 					sd.symbol_names->data(),
   1881 					sd.symbol_names_size,
   1882 					deferred->shndx_,
   1883 					shdr,
   1884 					deferred->reloc_shndx_,
   1885 					deferred->reloc_type_);
   1886 	  continue;
   1887 	}
   1888 
   1889       // If the section is not included, it is because the garbage collector
   1890       // decided it is not needed.  Avoid reverting that decision.
   1891       if (!this->is_section_included(deferred->shndx_))
   1892 	continue;
   1893 
   1894       this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
   1895 			   shdr, deferred->reloc_shndx_,
   1896 			   deferred->reloc_type_);
   1897     }
   1898 
   1899   this->deferred_layout_.clear();
   1900 
   1901   // Now handle the deferred relocation sections.
   1902 
   1903   Output_sections& out_sections(this->output_sections());
   1904   std::vector<Address>& out_section_offsets(this->section_offsets());
   1905 
   1906   for (deferred = this->deferred_layout_relocs_.begin();
   1907        deferred != this->deferred_layout_relocs_.end();
   1908        ++deferred)
   1909     {
   1910       unsigned int shndx = deferred->shndx_;
   1911       typename This::Shdr shdr(deferred->shdr_data_);
   1912       unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
   1913 
   1914       Output_section* data_section = out_sections[data_shndx];
   1915       if (data_section == NULL)
   1916 	{
   1917 	  out_sections[shndx] = NULL;
   1918 	  out_section_offsets[shndx] = invalid_address;
   1919 	  continue;
   1920 	}
   1921 
   1922       Relocatable_relocs* rr = new Relocatable_relocs();
   1923       this->set_relocatable_relocs(shndx, rr);
   1924 
   1925       Output_section* os = layout->layout_reloc(this, shndx, shdr,
   1926 						data_section, rr);
   1927       out_sections[shndx] = os;
   1928       out_section_offsets[shndx] = invalid_address;
   1929     }
   1930 }
   1931 
   1932 // Add the symbols to the symbol table.
   1933 
   1934 template<int size, bool big_endian>
   1935 void
   1936 Sized_relobj_file<size, big_endian>::do_add_symbols(Symbol_table* symtab,
   1937 						    Read_symbols_data* sd,
   1938 						    Layout*)
   1939 {
   1940   if (sd->symbols == NULL)
   1941     {
   1942       gold_assert(sd->symbol_names == NULL);
   1943       return;
   1944     }
   1945 
   1946   const int sym_size = This::sym_size;
   1947   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
   1948 		     / sym_size);
   1949   if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
   1950     {
   1951       this->error(_("size of symbols is not multiple of symbol size"));
   1952       return;
   1953     }
   1954 
   1955   this->symbols_.resize(symcount);
   1956 
   1957   const char* sym_names =
   1958     reinterpret_cast<const char*>(sd->symbol_names->data());
   1959   symtab->add_from_relobj(this,
   1960 			  sd->symbols->data() + sd->external_symbols_offset,
   1961 			  symcount, this->local_symbol_count_,
   1962 			  sym_names, sd->symbol_names_size,
   1963 			  &this->symbols_,
   1964 			  &this->defined_count_);
   1965 
   1966   delete sd->symbols;
   1967   sd->symbols = NULL;
   1968   delete sd->symbol_names;
   1969   sd->symbol_names = NULL;
   1970 }
   1971 
   1972 // Find out if this object, that is a member of a lib group, should be included
   1973 // in the link. We check every symbol defined by this object. If the symbol
   1974 // table has a strong undefined reference to that symbol, we have to include
   1975 // the object.
   1976 
   1977 template<int size, bool big_endian>
   1978 Archive::Should_include
   1979 Sized_relobj_file<size, big_endian>::do_should_include_member(
   1980     Symbol_table* symtab,
   1981     Layout* layout,
   1982     Read_symbols_data* sd,
   1983     std::string* why)
   1984 {
   1985   char* tmpbuf = NULL;
   1986   size_t tmpbuflen = 0;
   1987   const char* sym_names =
   1988       reinterpret_cast<const char*>(sd->symbol_names->data());
   1989   const unsigned char* syms =
   1990       sd->symbols->data() + sd->external_symbols_offset;
   1991   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
   1992   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
   1993 			 / sym_size);
   1994 
   1995   const unsigned char* p = syms;
   1996 
   1997   for (size_t i = 0; i < symcount; ++i, p += sym_size)
   1998     {
   1999       elfcpp::Sym<size, big_endian> sym(p);
   2000       unsigned int st_shndx = sym.get_st_shndx();
   2001       if (st_shndx == elfcpp::SHN_UNDEF)
   2002 	continue;
   2003 
   2004       unsigned int st_name = sym.get_st_name();
   2005       const char* name = sym_names + st_name;
   2006       Symbol* symbol;
   2007       Archive::Should_include t = Archive::should_include_member(symtab,
   2008 								 layout,
   2009 								 name,
   2010 								 &symbol, why,
   2011 								 &tmpbuf,
   2012 								 &tmpbuflen);
   2013       if (t == Archive::SHOULD_INCLUDE_YES)
   2014 	{
   2015 	  if (tmpbuf != NULL)
   2016 	    free(tmpbuf);
   2017 	  return t;
   2018 	}
   2019     }
   2020   if (tmpbuf != NULL)
   2021     free(tmpbuf);
   2022   return Archive::SHOULD_INCLUDE_UNKNOWN;
   2023 }
   2024 
   2025 // Iterate over global defined symbols, calling a visitor class V for each.
   2026 
   2027 template<int size, bool big_endian>
   2028 void
   2029 Sized_relobj_file<size, big_endian>::do_for_all_global_symbols(
   2030     Read_symbols_data* sd,
   2031     Library_base::Symbol_visitor_base* v)
   2032 {
   2033   const char* sym_names =
   2034       reinterpret_cast<const char*>(sd->symbol_names->data());
   2035   const unsigned char* syms =
   2036       sd->symbols->data() + sd->external_symbols_offset;
   2037   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
   2038   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
   2039 		     / sym_size);
   2040   const unsigned char* p = syms;
   2041 
   2042   for (size_t i = 0; i < symcount; ++i, p += sym_size)
   2043     {
   2044       elfcpp::Sym<size, big_endian> sym(p);
   2045       if (sym.get_st_shndx() != elfcpp::SHN_UNDEF)
   2046 	v->visit(sym_names + sym.get_st_name());
   2047     }
   2048 }
   2049 
   2050 // Return whether the local symbol SYMNDX has a PLT offset.
   2051 
   2052 template<int size, bool big_endian>
   2053 bool
   2054 Sized_relobj_file<size, big_endian>::local_has_plt_offset(
   2055     unsigned int symndx) const
   2056 {
   2057   typename Local_plt_offsets::const_iterator p =
   2058     this->local_plt_offsets_.find(symndx);
   2059   return p != this->local_plt_offsets_.end();
   2060 }
   2061 
   2062 // Get the PLT offset of a local symbol.
   2063 
   2064 template<int size, bool big_endian>
   2065 unsigned int
   2066 Sized_relobj_file<size, big_endian>::do_local_plt_offset(
   2067     unsigned int symndx) const
   2068 {
   2069   typename Local_plt_offsets::const_iterator p =
   2070     this->local_plt_offsets_.find(symndx);
   2071   gold_assert(p != this->local_plt_offsets_.end());
   2072   return p->second;
   2073 }
   2074 
   2075 // Set the PLT offset of a local symbol.
   2076 
   2077 template<int size, bool big_endian>
   2078 void
   2079 Sized_relobj_file<size, big_endian>::set_local_plt_offset(
   2080     unsigned int symndx, unsigned int plt_offset)
   2081 {
   2082   std::pair<typename Local_plt_offsets::iterator, bool> ins =
   2083     this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset));
   2084   gold_assert(ins.second);
   2085 }
   2086 
   2087 // First pass over the local symbols.  Here we add their names to
   2088 // *POOL and *DYNPOOL, and we store the symbol value in
   2089 // THIS->LOCAL_VALUES_.  This function is always called from a
   2090 // singleton thread.  This is followed by a call to
   2091 // finalize_local_symbols.
   2092 
   2093 template<int size, bool big_endian>
   2094 void
   2095 Sized_relobj_file<size, big_endian>::do_count_local_symbols(Stringpool* pool,
   2096 							    Stringpool* dynpool)
   2097 {
   2098   gold_assert(this->symtab_shndx_ != -1U);
   2099   if (this->symtab_shndx_ == 0)
   2100     {
   2101       // This object has no symbols.  Weird but legal.
   2102       return;
   2103     }
   2104 
   2105   // Read the symbol table section header.
   2106   const unsigned int symtab_shndx = this->symtab_shndx_;
   2107   typename This::Shdr symtabshdr(this,
   2108 				 this->elf_file_.section_header(symtab_shndx));
   2109   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
   2110 
   2111   // Read the local symbols.
   2112   const int sym_size = This::sym_size;
   2113   const unsigned int loccount = this->local_symbol_count_;
   2114   gold_assert(loccount == symtabshdr.get_sh_info());
   2115   off_t locsize = loccount * sym_size;
   2116   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
   2117 					      locsize, true, true);
   2118 
   2119   // Read the symbol names.
   2120   const unsigned int strtab_shndx =
   2121     this->adjust_shndx(symtabshdr.get_sh_link());
   2122   section_size_type strtab_size;
   2123   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
   2124 							&strtab_size,
   2125 							true);
   2126   const char* pnames = reinterpret_cast<const char*>(pnamesu);
   2127 
   2128   // Loop over the local symbols.
   2129 
   2130   const Output_sections& out_sections(this->output_sections());
   2131   unsigned int shnum = this->shnum();
   2132   unsigned int count = 0;
   2133   unsigned int dyncount = 0;
   2134   // Skip the first, dummy, symbol.
   2135   psyms += sym_size;
   2136   bool strip_all = parameters->options().strip_all();
   2137   bool discard_all = parameters->options().discard_all();
   2138   bool discard_locals = parameters->options().discard_locals();
   2139   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
   2140     {
   2141       elfcpp::Sym<size, big_endian> sym(psyms);
   2142 
   2143       Symbol_value<size>& lv(this->local_values_[i]);
   2144 
   2145       bool is_ordinary;
   2146       unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
   2147 						  &is_ordinary);
   2148       lv.set_input_shndx(shndx, is_ordinary);
   2149 
   2150       if (sym.get_st_type() == elfcpp::STT_SECTION)
   2151 	lv.set_is_section_symbol();
   2152       else if (sym.get_st_type() == elfcpp::STT_TLS)
   2153 	lv.set_is_tls_symbol();
   2154       else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
   2155 	lv.set_is_ifunc_symbol();
   2156 
   2157       // Save the input symbol value for use in do_finalize_local_symbols().
   2158       lv.set_input_value(sym.get_st_value());
   2159 
   2160       // Decide whether this symbol should go into the output file.
   2161 
   2162       if ((shndx < shnum && out_sections[shndx] == NULL)
   2163 	  || shndx == this->discarded_eh_frame_shndx_)
   2164 	{
   2165 	  lv.set_no_output_symtab_entry();
   2166 	  gold_assert(!lv.needs_output_dynsym_entry());
   2167 	  continue;
   2168 	}
   2169 
   2170       if (sym.get_st_type() == elfcpp::STT_SECTION
   2171 	  || !this->adjust_local_symbol(&lv))
   2172 	{
   2173 	  lv.set_no_output_symtab_entry();
   2174 	  gold_assert(!lv.needs_output_dynsym_entry());
   2175 	  continue;
   2176 	}
   2177 
   2178       if (sym.get_st_name() >= strtab_size)
   2179 	{
   2180 	  this->error(_("local symbol %u section name out of range: %u >= %u"),
   2181 		      i, sym.get_st_name(),
   2182 		      static_cast<unsigned int>(strtab_size));
   2183 	  lv.set_no_output_symtab_entry();
   2184 	  continue;
   2185 	}
   2186 
   2187       const char* name = pnames + sym.get_st_name();
   2188 
   2189       // If needed, add the symbol to the dynamic symbol table string pool.
   2190       if (lv.needs_output_dynsym_entry())
   2191 	{
   2192 	  dynpool->add(name, true, NULL);
   2193 	  ++dyncount;
   2194 	}
   2195 
   2196       if (strip_all
   2197 	  || (discard_all && lv.may_be_discarded_from_output_symtab()))
   2198 	{
   2199 	  lv.set_no_output_symtab_entry();
   2200 	  continue;
   2201 	}
   2202 
   2203       // If --discard-locals option is used, discard all temporary local
   2204       // symbols.  These symbols start with system-specific local label
   2205       // prefixes, typically .L for ELF system.  We want to be compatible
   2206       // with GNU ld so here we essentially use the same check in
   2207       // bfd_is_local_label().  The code is different because we already
   2208       // know that:
   2209       //
   2210       //   - the symbol is local and thus cannot have global or weak binding.
   2211       //   - the symbol is not a section symbol.
   2212       //   - the symbol has a name.
   2213       //
   2214       // We do not discard a symbol if it needs a dynamic symbol entry.
   2215       if (discard_locals
   2216 	  && sym.get_st_type() != elfcpp::STT_FILE
   2217 	  && !lv.needs_output_dynsym_entry()
   2218 	  && lv.may_be_discarded_from_output_symtab()
   2219 	  && parameters->target().is_local_label_name(name))
   2220 	{
   2221 	  lv.set_no_output_symtab_entry();
   2222 	  continue;
   2223 	}
   2224 
   2225       // Discard the local symbol if -retain_symbols_file is specified
   2226       // and the local symbol is not in that file.
   2227       if (!parameters->options().should_retain_symbol(name))
   2228 	{
   2229 	  lv.set_no_output_symtab_entry();
   2230 	  continue;
   2231 	}
   2232 
   2233       // Add the symbol to the symbol table string pool.
   2234       pool->add(name, true, NULL);
   2235       ++count;
   2236     }
   2237 
   2238   this->output_local_symbol_count_ = count;
   2239   this->output_local_dynsym_count_ = dyncount;
   2240 }
   2241 
   2242 // Compute the final value of a local symbol.
   2243 
   2244 template<int size, bool big_endian>
   2245 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
   2246 Sized_relobj_file<size, big_endian>::compute_final_local_value_internal(
   2247     unsigned int r_sym,
   2248     const Symbol_value<size>* lv_in,
   2249     Symbol_value<size>* lv_out,
   2250     bool relocatable,
   2251     const Output_sections& out_sections,
   2252     const std::vector<Address>& out_offsets,
   2253     const Symbol_table* symtab)
   2254 {
   2255   // We are going to overwrite *LV_OUT, if it has a merged symbol value,
   2256   // we may have a memory leak.
   2257   gold_assert(lv_out->has_output_value());
   2258 
   2259   bool is_ordinary;
   2260   unsigned int shndx = lv_in->input_shndx(&is_ordinary);
   2261 
   2262   // Set the output symbol value.
   2263 
   2264   if (!is_ordinary)
   2265     {
   2266       if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
   2267 	lv_out->set_output_value(lv_in->input_value());
   2268       else
   2269 	{
   2270 	  this->error(_("unknown section index %u for local symbol %u"),
   2271 		      shndx, r_sym);
   2272 	  lv_out->set_output_value(0);
   2273 	  return This::CFLV_ERROR;
   2274 	}
   2275     }
   2276   else
   2277     {
   2278       if (shndx >= this->shnum())
   2279 	{
   2280 	  this->error(_("local symbol %u section index %u out of range"),
   2281 		      r_sym, shndx);
   2282 	  lv_out->set_output_value(0);
   2283 	  return This::CFLV_ERROR;
   2284 	}
   2285 
   2286       Output_section* os = out_sections[shndx];
   2287       Address secoffset = out_offsets[shndx];
   2288       if (symtab->is_section_folded(this, shndx))
   2289 	{
   2290 	  gold_assert(os == NULL && secoffset == invalid_address);
   2291 	  // Get the os of the section it is folded onto.
   2292 	  Section_id folded = symtab->icf()->get_folded_section(this,
   2293 								shndx);
   2294 	  gold_assert(folded.first != NULL);
   2295 	  Sized_relobj_file<size, big_endian>* folded_obj = reinterpret_cast
   2296 	    <Sized_relobj_file<size, big_endian>*>(folded.first);
   2297 	  os = folded_obj->output_section(folded.second);
   2298 	  gold_assert(os != NULL);
   2299 	  secoffset = folded_obj->get_output_section_offset(folded.second);
   2300 
   2301 	  // This could be a relaxed input section.
   2302 	  if (secoffset == invalid_address)
   2303 	    {
   2304 	      const Output_relaxed_input_section* relaxed_section =
   2305 		os->find_relaxed_input_section(folded_obj, folded.second);
   2306 	      gold_assert(relaxed_section != NULL);
   2307 	      secoffset = relaxed_section->address() - os->address();
   2308 	    }
   2309 	}
   2310 
   2311       if (os == NULL)
   2312 	{
   2313 	  // This local symbol belongs to a section we are discarding.
   2314 	  // In some cases when applying relocations later, we will
   2315 	  // attempt to match it to the corresponding kept section,
   2316 	  // so we leave the input value unchanged here.
   2317 	  return This::CFLV_DISCARDED;
   2318 	}
   2319       else if (secoffset == invalid_address)
   2320 	{
   2321 	  uint64_t start;
   2322 
   2323 	  // This is a SHF_MERGE section or one which otherwise
   2324 	  // requires special handling.
   2325 	  if (shndx == this->discarded_eh_frame_shndx_)
   2326 	    {
   2327 	      // This local symbol belongs to a discarded .eh_frame
   2328 	      // section.  Just treat it like the case in which
   2329 	      // os == NULL above.
   2330 	      gold_assert(this->has_eh_frame_);
   2331 	      return This::CFLV_DISCARDED;
   2332 	    }
   2333 	  else if (!lv_in->is_section_symbol())
   2334 	    {
   2335 	      // This is not a section symbol.  We can determine
   2336 	      // the final value now.
   2337 	      lv_out->set_output_value(
   2338 		  os->output_address(this, shndx, lv_in->input_value()));
   2339 	    }
   2340 	  else if (!os->find_starting_output_address(this, shndx, &start))
   2341 	    {
   2342 	      // This is a section symbol, but apparently not one in a
   2343 	      // merged section.  First check to see if this is a relaxed
   2344 	      // input section.  If so, use its address.  Otherwise just
   2345 	      // use the start of the output section.  This happens with
   2346 	      // relocatable links when the input object has section
   2347 	      // symbols for arbitrary non-merge sections.
   2348 	      const Output_section_data* posd =
   2349 		os->find_relaxed_input_section(this, shndx);
   2350 	      if (posd != NULL)
   2351 		{
   2352 		  Address relocatable_link_adjustment =
   2353 		    relocatable ? os->address() : 0;
   2354 		  lv_out->set_output_value(posd->address()
   2355 					   - relocatable_link_adjustment);
   2356 		}
   2357 	      else
   2358 		lv_out->set_output_value(os->address());
   2359 	    }
   2360 	  else
   2361 	    {
   2362 	      // We have to consider the addend to determine the
   2363 	      // value to use in a relocation.  START is the start
   2364 	      // of this input section.  If we are doing a relocatable
   2365 	      // link, use offset from start output section instead of
   2366 	      // address.
   2367 	      Address adjusted_start =
   2368 		relocatable ? start - os->address() : start;
   2369 	      Merged_symbol_value<size>* msv =
   2370 		new Merged_symbol_value<size>(lv_in->input_value(),
   2371 					      adjusted_start);
   2372 	      lv_out->set_merged_symbol_value(msv);
   2373 	    }
   2374 	}
   2375       else if (lv_in->is_tls_symbol()
   2376                || (lv_in->is_section_symbol()
   2377                    && (os->flags() & elfcpp::SHF_TLS)))
   2378 	lv_out->set_output_value(os->tls_offset()
   2379 				 + secoffset
   2380 				 + lv_in->input_value());
   2381       else
   2382 	lv_out->set_output_value((relocatable ? 0 : os->address())
   2383 				 + secoffset
   2384 				 + lv_in->input_value());
   2385     }
   2386   return This::CFLV_OK;
   2387 }
   2388 
   2389 // Compute final local symbol value.  R_SYM is the index of a local
   2390 // symbol in symbol table.  LV points to a symbol value, which is
   2391 // expected to hold the input value and to be over-written by the
   2392 // final value.  SYMTAB points to a symbol table.  Some targets may want
   2393 // to know would-be-finalized local symbol values in relaxation.
   2394 // Hence we provide this method.  Since this method updates *LV, a
   2395 // callee should make a copy of the original local symbol value and
   2396 // use the copy instead of modifying an object's local symbols before
   2397 // everything is finalized.  The caller should also free up any allocated
   2398 // memory in the return value in *LV.
   2399 template<int size, bool big_endian>
   2400 typename Sized_relobj_file<size, big_endian>::Compute_final_local_value_status
   2401 Sized_relobj_file<size, big_endian>::compute_final_local_value(
   2402     unsigned int r_sym,
   2403     const Symbol_value<size>* lv_in,
   2404     Symbol_value<size>* lv_out,
   2405     const Symbol_table* symtab)
   2406 {
   2407   // This is just a wrapper of compute_final_local_value_internal.
   2408   const bool relocatable = parameters->options().relocatable();
   2409   const Output_sections& out_sections(this->output_sections());
   2410   const std::vector<Address>& out_offsets(this->section_offsets());
   2411   return this->compute_final_local_value_internal(r_sym, lv_in, lv_out,
   2412 						  relocatable, out_sections,
   2413 						  out_offsets, symtab);
   2414 }
   2415 
   2416 // Finalize the local symbols.  Here we set the final value in
   2417 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
   2418 // This function is always called from a singleton thread.  The actual
   2419 // output of the local symbols will occur in a separate task.
   2420 
   2421 template<int size, bool big_endian>
   2422 unsigned int
   2423 Sized_relobj_file<size, big_endian>::do_finalize_local_symbols(
   2424     unsigned int index,
   2425     off_t off,
   2426     Symbol_table* symtab)
   2427 {
   2428   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
   2429 
   2430   const unsigned int loccount = this->local_symbol_count_;
   2431   this->local_symbol_offset_ = off;
   2432 
   2433   const bool relocatable = parameters->options().relocatable();
   2434   const Output_sections& out_sections(this->output_sections());
   2435   const std::vector<Address>& out_offsets(this->section_offsets());
   2436 
   2437   for (unsigned int i = 1; i < loccount; ++i)
   2438     {
   2439       Symbol_value<size>* lv = &this->local_values_[i];
   2440 
   2441       Compute_final_local_value_status cflv_status =
   2442 	this->compute_final_local_value_internal(i, lv, lv, relocatable,
   2443 						 out_sections, out_offsets,
   2444 						 symtab);
   2445       switch (cflv_status)
   2446 	{
   2447 	case CFLV_OK:
   2448 	  if (!lv->is_output_symtab_index_set())
   2449 	    {
   2450 	      lv->set_output_symtab_index(index);
   2451 	      ++index;
   2452 	    }
   2453 	  break;
   2454 	case CFLV_DISCARDED:
   2455 	case CFLV_ERROR:
   2456 	  // Do nothing.
   2457 	  break;
   2458 	default:
   2459 	  gold_unreachable();
   2460 	}
   2461     }
   2462   return index;
   2463 }
   2464 
   2465 // Set the output dynamic symbol table indexes for the local variables.
   2466 
   2467 template<int size, bool big_endian>
   2468 unsigned int
   2469 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_indexes(
   2470     unsigned int index)
   2471 {
   2472   const unsigned int loccount = this->local_symbol_count_;
   2473   for (unsigned int i = 1; i < loccount; ++i)
   2474     {
   2475       Symbol_value<size>& lv(this->local_values_[i]);
   2476       if (lv.needs_output_dynsym_entry())
   2477 	{
   2478 	  lv.set_output_dynsym_index(index);
   2479 	  ++index;
   2480 	}
   2481     }
   2482   return index;
   2483 }
   2484 
   2485 // Set the offset where local dynamic symbol information will be stored.
   2486 // Returns the count of local symbols contributed to the symbol table by
   2487 // this object.
   2488 
   2489 template<int size, bool big_endian>
   2490 unsigned int
   2491 Sized_relobj_file<size, big_endian>::do_set_local_dynsym_offset(off_t off)
   2492 {
   2493   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
   2494   this->local_dynsym_offset_ = off;
   2495   return this->output_local_dynsym_count_;
   2496 }
   2497 
   2498 // If Symbols_data is not NULL get the section flags from here otherwise
   2499 // get it from the file.
   2500 
   2501 template<int size, bool big_endian>
   2502 uint64_t
   2503 Sized_relobj_file<size, big_endian>::do_section_flags(unsigned int shndx)
   2504 {
   2505   Symbols_data* sd = this->get_symbols_data();
   2506   if (sd != NULL)
   2507     {
   2508       const unsigned char* pshdrs = sd->section_headers_data
   2509 				    + This::shdr_size * shndx;
   2510       typename This::Shdr shdr(pshdrs);
   2511       return shdr.get_sh_flags();
   2512     }
   2513   // If sd is NULL, read the section header from the file.
   2514   return this->elf_file_.section_flags(shndx);
   2515 }
   2516 
   2517 // Get the section's ent size from Symbols_data.  Called by get_section_contents
   2518 // in icf.cc
   2519 
   2520 template<int size, bool big_endian>
   2521 uint64_t
   2522 Sized_relobj_file<size, big_endian>::do_section_entsize(unsigned int shndx)
   2523 {
   2524   Symbols_data* sd = this->get_symbols_data();
   2525   gold_assert(sd != NULL);
   2526 
   2527   const unsigned char* pshdrs = sd->section_headers_data
   2528 				+ This::shdr_size * shndx;
   2529   typename This::Shdr shdr(pshdrs);
   2530   return shdr.get_sh_entsize();
   2531 }
   2532 
   2533 // Write out the local symbols.
   2534 
   2535 template<int size, bool big_endian>
   2536 void
   2537 Sized_relobj_file<size, big_endian>::write_local_symbols(
   2538     Output_file* of,
   2539     const Stringpool* sympool,
   2540     const Stringpool* dynpool,
   2541     Output_symtab_xindex* symtab_xindex,
   2542     Output_symtab_xindex* dynsym_xindex,
   2543     off_t symtab_off)
   2544 {
   2545   const bool strip_all = parameters->options().strip_all();
   2546   if (strip_all)
   2547     {
   2548       if (this->output_local_dynsym_count_ == 0)
   2549 	return;
   2550       this->output_local_symbol_count_ = 0;
   2551     }
   2552 
   2553   gold_assert(this->symtab_shndx_ != -1U);
   2554   if (this->symtab_shndx_ == 0)
   2555     {
   2556       // This object has no symbols.  Weird but legal.
   2557       return;
   2558     }
   2559 
   2560   // Read the symbol table section header.
   2561   const unsigned int symtab_shndx = this->symtab_shndx_;
   2562   typename This::Shdr symtabshdr(this,
   2563 				 this->elf_file_.section_header(symtab_shndx));
   2564   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
   2565   const unsigned int loccount = this->local_symbol_count_;
   2566   gold_assert(loccount == symtabshdr.get_sh_info());
   2567 
   2568   // Read the local symbols.
   2569   const int sym_size = This::sym_size;
   2570   off_t locsize = loccount * sym_size;
   2571   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
   2572 					      locsize, true, false);
   2573 
   2574   // Read the symbol names.
   2575   const unsigned int strtab_shndx =
   2576     this->adjust_shndx(symtabshdr.get_sh_link());
   2577   section_size_type strtab_size;
   2578   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
   2579 							&strtab_size,
   2580 							false);
   2581   const char* pnames = reinterpret_cast<const char*>(pnamesu);
   2582 
   2583   // Get views into the output file for the portions of the symbol table
   2584   // and the dynamic symbol table that we will be writing.
   2585   off_t output_size = this->output_local_symbol_count_ * sym_size;
   2586   unsigned char* oview = NULL;
   2587   if (output_size > 0)
   2588     oview = of->get_output_view(symtab_off + this->local_symbol_offset_,
   2589 				output_size);
   2590 
   2591   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
   2592   unsigned char* dyn_oview = NULL;
   2593   if (dyn_output_size > 0)
   2594     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
   2595 				    dyn_output_size);
   2596 
   2597   const Output_sections& out_sections(this->output_sections());
   2598 
   2599   gold_assert(this->local_values_.size() == loccount);
   2600 
   2601   unsigned char* ov = oview;
   2602   unsigned char* dyn_ov = dyn_oview;
   2603   psyms += sym_size;
   2604   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
   2605     {
   2606       elfcpp::Sym<size, big_endian> isym(psyms);
   2607 
   2608       Symbol_value<size>& lv(this->local_values_[i]);
   2609 
   2610       bool is_ordinary;
   2611       unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
   2612 						     &is_ordinary);
   2613       if (is_ordinary)
   2614 	{
   2615 	  gold_assert(st_shndx < out_sections.size());
   2616 	  if (out_sections[st_shndx] == NULL)
   2617 	    continue;
   2618 	  st_shndx = out_sections[st_shndx]->out_shndx();
   2619 	  if (st_shndx >= elfcpp::SHN_LORESERVE)
   2620 	    {
   2621 	      if (lv.has_output_symtab_entry())
   2622 		symtab_xindex->add(lv.output_symtab_index(), st_shndx);
   2623 	      if (lv.has_output_dynsym_entry())
   2624 		dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
   2625 	      st_shndx = elfcpp::SHN_XINDEX;
   2626 	    }
   2627 	}
   2628 
   2629       // Write the symbol to the output symbol table.
   2630       if (lv.has_output_symtab_entry())
   2631 	{
   2632 	  elfcpp::Sym_write<size, big_endian> osym(ov);
   2633 
   2634 	  gold_assert(isym.get_st_name() < strtab_size);
   2635 	  const char* name = pnames + isym.get_st_name();
   2636 	  osym.put_st_name(sympool->get_offset(name));
   2637 	  osym.put_st_value(this->local_values_[i].value(this, 0));
   2638 	  osym.put_st_size(isym.get_st_size());
   2639 	  osym.put_st_info(isym.get_st_info());
   2640 	  osym.put_st_other(isym.get_st_other());
   2641 	  osym.put_st_shndx(st_shndx);
   2642 
   2643 	  ov += sym_size;
   2644 	}
   2645 
   2646       // Write the symbol to the output dynamic symbol table.
   2647       if (lv.has_output_dynsym_entry())
   2648 	{
   2649 	  gold_assert(dyn_ov < dyn_oview + dyn_output_size);
   2650 	  elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
   2651 
   2652 	  gold_assert(isym.get_st_name() < strtab_size);
   2653 	  const char* name = pnames + isym.get_st_name();
   2654 	  osym.put_st_name(dynpool->get_offset(name));
   2655 	  osym.put_st_value(this->local_values_[i].value(this, 0));
   2656 	  osym.put_st_size(isym.get_st_size());
   2657 	  osym.put_st_info(isym.get_st_info());
   2658 	  osym.put_st_other(isym.get_st_other());
   2659 	  osym.put_st_shndx(st_shndx);
   2660 
   2661 	  dyn_ov += sym_size;
   2662 	}
   2663     }
   2664 
   2665 
   2666   if (output_size > 0)
   2667     {
   2668       gold_assert(ov - oview == output_size);
   2669       of->write_output_view(symtab_off + this->local_symbol_offset_,
   2670 			    output_size, oview);
   2671     }
   2672 
   2673   if (dyn_output_size > 0)
   2674     {
   2675       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
   2676       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
   2677 			    dyn_oview);
   2678     }
   2679 }
   2680 
   2681 // Set *INFO to symbolic information about the offset OFFSET in the
   2682 // section SHNDX.  Return true if we found something, false if we
   2683 // found nothing.
   2684 
   2685 template<int size, bool big_endian>
   2686 bool
   2687 Sized_relobj_file<size, big_endian>::get_symbol_location_info(
   2688     unsigned int shndx,
   2689     off_t offset,
   2690     Symbol_location_info* info)
   2691 {
   2692   if (this->symtab_shndx_ == 0)
   2693     return false;
   2694 
   2695   section_size_type symbols_size;
   2696   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
   2697 							&symbols_size,
   2698 							false);
   2699 
   2700   unsigned int symbol_names_shndx =
   2701     this->adjust_shndx(this->section_link(this->symtab_shndx_));
   2702   section_size_type names_size;
   2703   const unsigned char* symbol_names_u =
   2704     this->section_contents(symbol_names_shndx, &names_size, false);
   2705   const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
   2706 
   2707   const int sym_size = This::sym_size;
   2708   const size_t count = symbols_size / sym_size;
   2709 
   2710   const unsigned char* p = symbols;
   2711   for (size_t i = 0; i < count; ++i, p += sym_size)
   2712     {
   2713       elfcpp::Sym<size, big_endian> sym(p);
   2714 
   2715       if (sym.get_st_type() == elfcpp::STT_FILE)
   2716 	{
   2717 	  if (sym.get_st_name() >= names_size)
   2718 	    info->source_file = "(invalid)";
   2719 	  else
   2720 	    info->source_file = symbol_names + sym.get_st_name();
   2721 	  continue;
   2722 	}
   2723 
   2724       bool is_ordinary;
   2725       unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
   2726 						     &is_ordinary);
   2727       if (is_ordinary
   2728 	  && st_shndx == shndx
   2729 	  && static_cast<off_t>(sym.get_st_value()) <= offset
   2730 	  && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
   2731 	      > offset))
   2732 	{
   2733 	  info->enclosing_symbol_type = sym.get_st_type();
   2734 	  if (sym.get_st_name() > names_size)
   2735 	    info->enclosing_symbol_name = "(invalid)";
   2736 	  else
   2737 	    {
   2738 	      info->enclosing_symbol_name = symbol_names + sym.get_st_name();
   2739 	      if (parameters->options().do_demangle())
   2740 		{
   2741 		  char* demangled_name = cplus_demangle(
   2742 		      info->enclosing_symbol_name.c_str(),
   2743 		      DMGL_ANSI | DMGL_PARAMS);
   2744 		  if (demangled_name != NULL)
   2745 		    {
   2746 		      info->enclosing_symbol_name.assign(demangled_name);
   2747 		      free(demangled_name);
   2748 		    }
   2749 		}
   2750 	    }
   2751 	  return true;
   2752 	}
   2753     }
   2754 
   2755   return false;
   2756 }
   2757 
   2758 // Look for a kept section corresponding to the given discarded section,
   2759 // and return its output address.  This is used only for relocations in
   2760 // debugging sections.  If we can't find the kept section, return 0.
   2761 
   2762 template<int size, bool big_endian>
   2763 typename Sized_relobj_file<size, big_endian>::Address
   2764 Sized_relobj_file<size, big_endian>::map_to_kept_section(
   2765     unsigned int shndx,
   2766     bool* found) const
   2767 {
   2768   Relobj* kept_object;
   2769   unsigned int kept_shndx;
   2770   if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
   2771     {
   2772       Sized_relobj_file<size, big_endian>* kept_relobj =
   2773 	static_cast<Sized_relobj_file<size, big_endian>*>(kept_object);
   2774       Output_section* os = kept_relobj->output_section(kept_shndx);
   2775       Address offset = kept_relobj->get_output_section_offset(kept_shndx);
   2776       if (os != NULL && offset != invalid_address)
   2777 	{
   2778 	  *found = true;
   2779 	  return os->address() + offset;
   2780 	}
   2781     }
   2782   *found = false;
   2783   return 0;
   2784 }
   2785 
   2786 // Get symbol counts.
   2787 
   2788 template<int size, bool big_endian>
   2789 void
   2790 Sized_relobj_file<size, big_endian>::do_get_global_symbol_counts(
   2791     const Symbol_table*,
   2792     size_t* defined,
   2793     size_t* used) const
   2794 {
   2795   *defined = this->defined_count_;
   2796   size_t count = 0;
   2797   for (typename Symbols::const_iterator p = this->symbols_.begin();
   2798        p != this->symbols_.end();
   2799        ++p)
   2800     if (*p != NULL
   2801 	&& (*p)->source() == Symbol::FROM_OBJECT
   2802 	&& (*p)->object() == this
   2803 	&& (*p)->is_defined())
   2804       ++count;
   2805   *used = count;
   2806 }
   2807 
   2808 // Return a view of the decompressed contents of a section.  Set *PLEN
   2809 // to the size.  Set *IS_NEW to true if the contents need to be freed
   2810 // by the caller.
   2811 
   2812 const unsigned char*
   2813 Object::decompressed_section_contents(
   2814     unsigned int shndx,
   2815     section_size_type* plen,
   2816     bool* is_new)
   2817 {
   2818   section_size_type buffer_size;
   2819   const unsigned char* buffer = this->do_section_contents(shndx, &buffer_size,
   2820 							  false);
   2821 
   2822   if (this->compressed_sections_ == NULL)
   2823     {
   2824       *plen = buffer_size;
   2825       *is_new = false;
   2826       return buffer;
   2827     }
   2828 
   2829   Compressed_section_map::const_iterator p =
   2830       this->compressed_sections_->find(shndx);
   2831   if (p == this->compressed_sections_->end())
   2832     {
   2833       *plen = buffer_size;
   2834       *is_new = false;
   2835       return buffer;
   2836     }
   2837 
   2838   section_size_type uncompressed_size = p->second.size;
   2839   if (p->second.contents != NULL)
   2840     {
   2841       *plen = uncompressed_size;
   2842       *is_new = false;
   2843       return p->second.contents;
   2844     }
   2845 
   2846   unsigned char* uncompressed_data = new unsigned char[uncompressed_size];
   2847   if (!decompress_input_section(buffer,
   2848 				buffer_size,
   2849 				uncompressed_data,
   2850 				uncompressed_size))
   2851     this->error(_("could not decompress section %s"),
   2852 		this->do_section_name(shndx).c_str());
   2853 
   2854   // We could cache the results in p->second.contents and store
   2855   // false in *IS_NEW, but build_compressed_section_map() would
   2856   // have done so if it had expected it to be profitable.  If
   2857   // we reach this point, we expect to need the contents only
   2858   // once in this pass.
   2859   *plen = uncompressed_size;
   2860   *is_new = true;
   2861   return uncompressed_data;
   2862 }
   2863 
   2864 // Discard any buffers of uncompressed sections.  This is done
   2865 // at the end of the Add_symbols task.
   2866 
   2867 void
   2868 Object::discard_decompressed_sections()
   2869 {
   2870   if (this->compressed_sections_ == NULL)
   2871     return;
   2872 
   2873   for (Compressed_section_map::iterator p = this->compressed_sections_->begin();
   2874        p != this->compressed_sections_->end();
   2875        ++p)
   2876     {
   2877       if (p->second.contents != NULL)
   2878 	{
   2879 	  delete[] p->second.contents;
   2880 	  p->second.contents = NULL;
   2881 	}
   2882     }
   2883 }
   2884 
   2885 // Input_objects methods.
   2886 
   2887 // Add a regular relocatable object to the list.  Return false if this
   2888 // object should be ignored.
   2889 
   2890 bool
   2891 Input_objects::add_object(Object* obj)
   2892 {
   2893   // Print the filename if the -t/--trace option is selected.
   2894   if (parameters->options().trace())
   2895     gold_info("%s", obj->name().c_str());
   2896 
   2897   if (!obj->is_dynamic())
   2898     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
   2899   else
   2900     {
   2901       // See if this is a duplicate SONAME.
   2902       Dynobj* dynobj = static_cast<Dynobj*>(obj);
   2903       const char* soname = dynobj->soname();
   2904 
   2905       std::pair<Unordered_set<std::string>::iterator, bool> ins =
   2906 	this->sonames_.insert(soname);
   2907       if (!ins.second)
   2908 	{
   2909 	  // We have already seen a dynamic object with this soname.
   2910 	  return false;
   2911 	}
   2912 
   2913       this->dynobj_list_.push_back(dynobj);
   2914     }
   2915 
   2916   // Add this object to the cross-referencer if requested.
   2917   if (parameters->options().user_set_print_symbol_counts()
   2918       || parameters->options().cref())
   2919     {
   2920       if (this->cref_ == NULL)
   2921 	this->cref_ = new Cref();
   2922       this->cref_->add_object(obj);
   2923     }
   2924 
   2925   return true;
   2926 }
   2927 
   2928 // For each dynamic object, record whether we've seen all of its
   2929 // explicit dependencies.
   2930 
   2931 void
   2932 Input_objects::check_dynamic_dependencies() const
   2933 {
   2934   bool issued_copy_dt_needed_error = false;
   2935   for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
   2936        p != this->dynobj_list_.end();
   2937        ++p)
   2938     {
   2939       const Dynobj::Needed& needed((*p)->needed());
   2940       bool found_all = true;
   2941       Dynobj::Needed::const_iterator pneeded;
   2942       for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
   2943 	{
   2944 	  if (this->sonames_.find(*pneeded) == this->sonames_.end())
   2945 	    {
   2946 	      found_all = false;
   2947 	      break;
   2948 	    }
   2949 	}
   2950       (*p)->set_has_unknown_needed_entries(!found_all);
   2951 
   2952       // --copy-dt-needed-entries aka --add-needed is a GNU ld option
   2953       // that gold does not support.  However, they cause no trouble
   2954       // unless there is a DT_NEEDED entry that we don't know about;
   2955       // warn only in that case.
   2956       if (!found_all
   2957 	  && !issued_copy_dt_needed_error
   2958 	  && (parameters->options().copy_dt_needed_entries()
   2959 	      || parameters->options().add_needed()))
   2960 	{
   2961 	  const char* optname;
   2962 	  if (parameters->options().copy_dt_needed_entries())
   2963 	    optname = "--copy-dt-needed-entries";
   2964 	  else
   2965 	    optname = "--add-needed";
   2966 	  gold_error(_("%s is not supported but is required for %s in %s"),
   2967 		     optname, (*pneeded).c_str(), (*p)->name().c_str());
   2968 	  issued_copy_dt_needed_error = true;
   2969 	}
   2970     }
   2971 }
   2972 
   2973 // Start processing an archive.
   2974 
   2975 void
   2976 Input_objects::archive_start(Archive* archive)
   2977 {
   2978   if (parameters->options().user_set_print_symbol_counts()
   2979       || parameters->options().cref())
   2980     {
   2981       if (this->cref_ == NULL)
   2982 	this->cref_ = new Cref();
   2983       this->cref_->add_archive_start(archive);
   2984     }
   2985 }
   2986 
   2987 // Stop processing an archive.
   2988 
   2989 void
   2990 Input_objects::archive_stop(Archive* archive)
   2991 {
   2992   if (parameters->options().user_set_print_symbol_counts()
   2993       || parameters->options().cref())
   2994     this->cref_->add_archive_stop(archive);
   2995 }
   2996 
   2997 // Print symbol counts
   2998 
   2999 void
   3000 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
   3001 {
   3002   if (parameters->options().user_set_print_symbol_counts()
   3003       && this->cref_ != NULL)
   3004     this->cref_->print_symbol_counts(symtab);
   3005 }
   3006 
   3007 // Print a cross reference table.
   3008 
   3009 void
   3010 Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
   3011 {
   3012   if (parameters->options().cref() && this->cref_ != NULL)
   3013     this->cref_->print_cref(symtab, f);
   3014 }
   3015 
   3016 // Relocate_info methods.
   3017 
   3018 // Return a string describing the location of a relocation when file
   3019 // and lineno information is not available.  This is only used in
   3020 // error messages.
   3021 
   3022 template<int size, bool big_endian>
   3023 std::string
   3024 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
   3025 {
   3026   Sized_dwarf_line_info<size, big_endian> line_info(this->object);
   3027   std::string ret = line_info.addr2line(this->data_shndx, offset, NULL);
   3028   if (!ret.empty())
   3029     return ret;
   3030 
   3031   ret = this->object->name();
   3032 
   3033   Symbol_location_info info;
   3034   if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
   3035     {
   3036       if (!info.source_file.empty())
   3037 	{
   3038 	  ret += ":";
   3039 	  ret += info.source_file;
   3040 	}
   3041       ret += ":";
   3042       if (info.enclosing_symbol_type == elfcpp::STT_FUNC)
   3043 	ret += _("function ");
   3044       ret += info.enclosing_symbol_name;
   3045       return ret;
   3046     }
   3047 
   3048   ret += "(";
   3049   ret += this->object->section_name(this->data_shndx);
   3050   char buf[100];
   3051   snprintf(buf, sizeof buf, "+0x%lx)", static_cast<long>(offset));
   3052   ret += buf;
   3053   return ret;
   3054 }
   3055 
   3056 } // End namespace gold.
   3057 
   3058 namespace
   3059 {
   3060 
   3061 using namespace gold;
   3062 
   3063 // Read an ELF file with the header and return the appropriate
   3064 // instance of Object.
   3065 
   3066 template<int size, bool big_endian>
   3067 Object*
   3068 make_elf_sized_object(const std::string& name, Input_file* input_file,
   3069 		      off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
   3070 		      bool* punconfigured)
   3071 {
   3072   Target* target = select_target(input_file, offset,
   3073 				 ehdr.get_e_machine(), size, big_endian,
   3074 				 ehdr.get_e_ident()[elfcpp::EI_OSABI],
   3075 				 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
   3076   if (target == NULL)
   3077     gold_fatal(_("%s: unsupported ELF machine number %d"),
   3078 	       name.c_str(), ehdr.get_e_machine());
   3079 
   3080   if (!parameters->target_valid())
   3081     set_parameters_target(target);
   3082   else if (target != &parameters->target())
   3083     {
   3084       if (punconfigured != NULL)
   3085 	*punconfigured = true;
   3086       else
   3087 	gold_error(_("%s: incompatible target"), name.c_str());
   3088       return NULL;
   3089     }
   3090 
   3091   return target->make_elf_object<size, big_endian>(name, input_file, offset,
   3092 						   ehdr);
   3093 }
   3094 
   3095 } // End anonymous namespace.
   3096 
   3097 namespace gold
   3098 {
   3099 
   3100 // Return whether INPUT_FILE is an ELF object.
   3101 
   3102 bool
   3103 is_elf_object(Input_file* input_file, off_t offset,
   3104 	      const unsigned char** start, int* read_size)
   3105 {
   3106   off_t filesize = input_file->file().filesize();
   3107   int want = elfcpp::Elf_recognizer::max_header_size;
   3108   if (filesize - offset < want)
   3109     want = filesize - offset;
   3110 
   3111   const unsigned char* p = input_file->file().get_view(offset, 0, want,
   3112 						       true, false);
   3113   *start = p;
   3114   *read_size = want;
   3115 
   3116   return elfcpp::Elf_recognizer::is_elf_file(p, want);
   3117 }
   3118 
   3119 // Read an ELF file and return the appropriate instance of Object.
   3120 
   3121 Object*
   3122 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
   3123 		const unsigned char* p, section_offset_type bytes,
   3124 		bool* punconfigured)
   3125 {
   3126   if (punconfigured != NULL)
   3127     *punconfigured = false;
   3128 
   3129   std::string error;
   3130   bool big_endian = false;
   3131   int size = 0;
   3132   if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
   3133 					       &big_endian, &error))
   3134     {
   3135       gold_error(_("%s: %s"), name.c_str(), error.c_str());
   3136       return NULL;
   3137     }
   3138 
   3139   if (size == 32)
   3140     {
   3141       if (big_endian)
   3142 	{
   3143 #ifdef HAVE_TARGET_32_BIG
   3144 	  elfcpp::Ehdr<32, true> ehdr(p);
   3145 	  return make_elf_sized_object<32, true>(name, input_file,
   3146 						 offset, ehdr, punconfigured);
   3147 #else
   3148 	  if (punconfigured != NULL)
   3149 	    *punconfigured = true;
   3150 	  else
   3151 	    gold_error(_("%s: not configured to support "
   3152 			 "32-bit big-endian object"),
   3153 		       name.c_str());
   3154 	  return NULL;
   3155 #endif
   3156 	}
   3157       else
   3158 	{
   3159 #ifdef HAVE_TARGET_32_LITTLE
   3160 	  elfcpp::Ehdr<32, false> ehdr(p);
   3161 	  return make_elf_sized_object<32, false>(name, input_file,
   3162 						  offset, ehdr, punconfigured);
   3163 #else
   3164 	  if (punconfigured != NULL)
   3165 	    *punconfigured = true;
   3166 	  else
   3167 	    gold_error(_("%s: not configured to support "
   3168 			 "32-bit little-endian object"),
   3169 		       name.c_str());
   3170 	  return NULL;
   3171 #endif
   3172 	}
   3173     }
   3174   else if (size == 64)
   3175     {
   3176       if (big_endian)
   3177 	{
   3178 #ifdef HAVE_TARGET_64_BIG
   3179 	  elfcpp::Ehdr<64, true> ehdr(p);
   3180 	  return make_elf_sized_object<64, true>(name, input_file,
   3181 						 offset, ehdr, punconfigured);
   3182 #else
   3183 	  if (punconfigured != NULL)
   3184 	    *punconfigured = true;
   3185 	  else
   3186 	    gold_error(_("%s: not configured to support "
   3187 			 "64-bit big-endian object"),
   3188 		       name.c_str());
   3189 	  return NULL;
   3190 #endif
   3191 	}
   3192       else
   3193 	{
   3194 #ifdef HAVE_TARGET_64_LITTLE
   3195 	  elfcpp::Ehdr<64, false> ehdr(p);
   3196 	  return make_elf_sized_object<64, false>(name, input_file,
   3197 						  offset, ehdr, punconfigured);
   3198 #else
   3199 	  if (punconfigured != NULL)
   3200 	    *punconfigured = true;
   3201 	  else
   3202 	    gold_error(_("%s: not configured to support "
   3203 			 "64-bit little-endian object"),
   3204 		       name.c_str());
   3205 	  return NULL;
   3206 #endif
   3207 	}
   3208     }
   3209   else
   3210     gold_unreachable();
   3211 }
   3212 
   3213 // Instantiate the templates we need.
   3214 
   3215 #ifdef HAVE_TARGET_32_LITTLE
   3216 template
   3217 void
   3218 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
   3219 				     Read_symbols_data*);
   3220 template
   3221 const unsigned char*
   3222 Object::find_shdr<32,false>(const unsigned char*, const char*, const char*,
   3223 			    section_size_type, const unsigned char*) const;
   3224 #endif
   3225 
   3226 #ifdef HAVE_TARGET_32_BIG
   3227 template
   3228 void
   3229 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
   3230 				    Read_symbols_data*);
   3231 template
   3232 const unsigned char*
   3233 Object::find_shdr<32,true>(const unsigned char*, const char*, const char*,
   3234 			   section_size_type, const unsigned char*) const;
   3235 #endif
   3236 
   3237 #ifdef HAVE_TARGET_64_LITTLE
   3238 template
   3239 void
   3240 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
   3241 				     Read_symbols_data*);
   3242 template
   3243 const unsigned char*
   3244 Object::find_shdr<64,false>(const unsigned char*, const char*, const char*,
   3245 			    section_size_type, const unsigned char*) const;
   3246 #endif
   3247 
   3248 #ifdef HAVE_TARGET_64_BIG
   3249 template
   3250 void
   3251 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
   3252 				    Read_symbols_data*);
   3253 template
   3254 const unsigned char*
   3255 Object::find_shdr<64,true>(const unsigned char*, const char*, const char*,
   3256 			   section_size_type, const unsigned char*) const;
   3257 #endif
   3258 
   3259 #ifdef HAVE_TARGET_32_LITTLE
   3260 template
   3261 class Sized_relobj<32, false>;
   3262 
   3263 template
   3264 class Sized_relobj_file<32, false>;
   3265 #endif
   3266 
   3267 #ifdef HAVE_TARGET_32_BIG
   3268 template
   3269 class Sized_relobj<32, true>;
   3270 
   3271 template
   3272 class Sized_relobj_file<32, true>;
   3273 #endif
   3274 
   3275 #ifdef HAVE_TARGET_64_LITTLE
   3276 template
   3277 class Sized_relobj<64, false>;
   3278 
   3279 template
   3280 class Sized_relobj_file<64, false>;
   3281 #endif
   3282 
   3283 #ifdef HAVE_TARGET_64_BIG
   3284 template
   3285 class Sized_relobj<64, true>;
   3286 
   3287 template
   3288 class Sized_relobj_file<64, true>;
   3289 #endif
   3290 
   3291 #ifdef HAVE_TARGET_32_LITTLE
   3292 template
   3293 struct Relocate_info<32, false>;
   3294 #endif
   3295 
   3296 #ifdef HAVE_TARGET_32_BIG
   3297 template
   3298 struct Relocate_info<32, true>;
   3299 #endif
   3300 
   3301 #ifdef HAVE_TARGET_64_LITTLE
   3302 template
   3303 struct Relocate_info<64, false>;
   3304 #endif
   3305 
   3306 #ifdef HAVE_TARGET_64_BIG
   3307 template
   3308 struct Relocate_info<64, true>;
   3309 #endif
   3310 
   3311 #ifdef HAVE_TARGET_32_LITTLE
   3312 template
   3313 void
   3314 Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
   3315 
   3316 template
   3317 void
   3318 Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
   3319 				      const unsigned char*);
   3320 #endif
   3321 
   3322 #ifdef HAVE_TARGET_32_BIG
   3323 template
   3324 void
   3325 Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
   3326 
   3327 template
   3328 void
   3329 Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
   3330 				     const unsigned char*);
   3331 #endif
   3332 
   3333 #ifdef HAVE_TARGET_64_LITTLE
   3334 template
   3335 void
   3336 Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
   3337 
   3338 template
   3339 void
   3340 Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
   3341 				      const unsigned char*);
   3342 #endif
   3343 
   3344 #ifdef HAVE_TARGET_64_BIG
   3345 template
   3346 void
   3347 Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
   3348 
   3349 template
   3350 void
   3351 Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
   3352 				     const unsigned char*);
   3353 #endif
   3354 
   3355 } // End namespace gold.
   3356