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