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