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