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