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