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