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