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