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