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