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