* infcmd.c (step_1): Put thread id on the stack to avoid possible
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 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
14bfc3f5 31#include "target-select.h"
5c2c6c95 32#include "dwarf_reader.h"
a2fb1b05 33#include "layout.h"
61ba1cf9 34#include "output.h"
f6ce93d6 35#include "symtab.h"
4c50553d 36#include "reloc.h"
f6ce93d6
ILT
37#include "object.h"
38#include "dynobj.h"
bae7f79e
ILT
39
40namespace gold
41{
42
d491d34e
ILT
43// Class Xindex.
44
45// Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
46// section and read it in. SYMTAB_SHNDX is the index of the symbol
47// table we care about.
48
49template<int size, bool big_endian>
50void
51Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
52{
53 if (!this->symtab_xindex_.empty())
54 return;
55
56 gold_assert(symtab_shndx != 0);
57
58 // Look through the sections in reverse order, on the theory that it
59 // is more likely to be near the end than the beginning.
60 unsigned int i = object->shnum();
61 while (i > 0)
62 {
63 --i;
64 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
65 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
66 {
67 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
68 return;
69 }
70 }
71
72 object->error(_("missing SHT_SYMTAB_SHNDX section"));
73}
74
75// Read in the symtab_xindex_ array, given the section index of the
76// SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
77// section headers.
78
79template<int size, bool big_endian>
80void
81Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
82 const unsigned char* pshdrs)
83{
84 section_size_type bytecount;
85 const unsigned char* contents;
86 if (pshdrs == NULL)
87 contents = object->section_contents(xindex_shndx, &bytecount, false);
88 else
89 {
90 const unsigned char* p = (pshdrs
91 + (xindex_shndx
92 * elfcpp::Elf_sizes<size>::shdr_size));
93 typename elfcpp::Shdr<size, big_endian> shdr(p);
94 bytecount = convert_to_section_size_type(shdr.get_sh_size());
95 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
96 }
97
98 gold_assert(this->symtab_xindex_.empty());
99 this->symtab_xindex_.reserve(bytecount / 4);
100 for (section_size_type i = 0; i < bytecount; i += 4)
101 {
102 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
103 // We preadjust the section indexes we save.
104 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
105 }
106}
107
108// Symbol symndx has a section of SHN_XINDEX; return the real section
109// index.
110
111unsigned int
112Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
113{
114 if (symndx >= this->symtab_xindex_.size())
115 {
116 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
117 symndx);
118 return elfcpp::SHN_UNDEF;
119 }
120 unsigned int shndx = this->symtab_xindex_[symndx];
121 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
122 {
123 object->error(_("extended index for symbol %u out of range: %u"),
124 symndx, shndx);
125 return elfcpp::SHN_UNDEF;
126 }
127 return shndx;
128}
129
645f8123
ILT
130// Class Object.
131
dbe717ef
ILT
132// Set the target based on fields in the ELF file header.
133
134void
135Object::set_target(int machine, int size, bool big_endian, int osabi,
136 int abiversion)
137{
138 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
139 if (target == NULL)
75f2446e
ILT
140 gold_fatal(_("%s: unsupported ELF machine number %d"),
141 this->name().c_str(), machine);
dbe717ef
ILT
142 this->target_ = target;
143}
144
75f2446e
ILT
145// Report an error for this object file. This is used by the
146// elfcpp::Elf_file interface, and also called by the Object code
147// itself.
645f8123
ILT
148
149void
75f2446e 150Object::error(const char* format, ...) const
645f8123
ILT
151{
152 va_list args;
645f8123 153 va_start(args, format);
75f2446e
ILT
154 char* buf = NULL;
155 if (vasprintf(&buf, format, args) < 0)
156 gold_nomem();
645f8123 157 va_end(args);
75f2446e
ILT
158 gold_error(_("%s: %s"), this->name().c_str(), buf);
159 free(buf);
645f8123
ILT
160}
161
162// Return a view of the contents of a section.
163
164const unsigned char*
8383303e
ILT
165Object::section_contents(unsigned int shndx, section_size_type* plen,
166 bool cache)
645f8123
ILT
167{
168 Location loc(this->do_section_contents(shndx));
8383303e 169 *plen = convert_to_section_size_type(loc.data_size);
39d0cb0e 170 return this->get_view(loc.file_offset, *plen, true, cache);
645f8123
ILT
171}
172
dbe717ef
ILT
173// Read the section data into SD. This is code common to Sized_relobj
174// and Sized_dynobj, so we put it into Object.
175
176template<int size, bool big_endian>
177void
178Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
179 Read_symbols_data* sd)
180{
181 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
182
183 // Read the section headers.
184 const off_t shoff = elf_file->shoff();
185 const unsigned int shnum = this->shnum();
39d0cb0e
ILT
186 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
187 true, true);
dbe717ef
ILT
188
189 // Read the section names.
190 const unsigned char* pshdrs = sd->section_headers->data();
191 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
192 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
193
194 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
75f2446e
ILT
195 this->error(_("section name section has wrong type: %u"),
196 static_cast<unsigned int>(shdrnames.get_sh_type()));
dbe717ef 197
8383303e
ILT
198 sd->section_names_size =
199 convert_to_section_size_type(shdrnames.get_sh_size());
dbe717ef 200 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
39d0cb0e
ILT
201 sd->section_names_size, false,
202 false);
dbe717ef
ILT
203}
204
205// If NAME is the name of a special .gnu.warning section, arrange for
206// the warning to be issued. SHNDX is the section index. Return
207// whether it is a warning section.
208
209bool
210Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
211 Symbol_table* symtab)
212{
213 const char warn_prefix[] = ".gnu.warning.";
214 const int warn_prefix_len = sizeof warn_prefix - 1;
215 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
216 {
cb295612
ILT
217 // Read the section contents to get the warning text. It would
218 // be nicer if we only did this if we have to actually issue a
219 // warning. Unfortunately, warnings are issued as we relocate
220 // sections. That means that we can not lock the object then,
221 // as we might try to issue the same warning multiple times
222 // simultaneously.
223 section_size_type len;
224 const unsigned char* contents = this->section_contents(shndx, &len,
225 false);
226 std::string warning(reinterpret_cast<const char*>(contents), len);
227 symtab->add_warning(name + warn_prefix_len, this, warning);
dbe717ef
ILT
228 return true;
229 }
230 return false;
231}
232
e94cf127
CC
233// Class Relobj.
234
235// Return the output address of the input section SHNDX.
236uint64_t
237Relobj::output_section_address(unsigned int shndx) const
238{
239 section_offset_type offset;
240 Output_section* os = this->output_section(shndx, &offset);
241 gold_assert(os != NULL && offset != -1);
242 return os->address() + offset;
243}
244
f6ce93d6 245// Class Sized_relobj.
bae7f79e
ILT
246
247template<int size, bool big_endian>
f6ce93d6 248Sized_relobj<size, big_endian>::Sized_relobj(
bae7f79e
ILT
249 const std::string& name,
250 Input_file* input_file,
251 off_t offset,
252 const elfcpp::Ehdr<size, big_endian>& ehdr)
f6ce93d6 253 : Relobj(name, input_file, offset),
645f8123 254 elf_file_(this, ehdr),
dbe717ef 255 symtab_shndx_(-1U),
61ba1cf9
ILT
256 local_symbol_count_(0),
257 output_local_symbol_count_(0),
7bf1f802 258 output_local_dynsym_count_(0),
730cdc88 259 symbols_(),
61ba1cf9 260 local_symbol_offset_(0),
7bf1f802 261 local_dynsym_offset_(0),
e727fa71 262 local_values_(),
730cdc88
ILT
263 local_got_offsets_(),
264 has_eh_frame_(false)
bae7f79e 265{
bae7f79e
ILT
266}
267
268template<int size, bool big_endian>
f6ce93d6 269Sized_relobj<size, big_endian>::~Sized_relobj()
bae7f79e
ILT
270{
271}
272
645f8123 273// Set up an object file based on the file header. This sets up the
bae7f79e
ILT
274// target and reads the section information.
275
276template<int size, bool big_endian>
277void
f6ce93d6 278Sized_relobj<size, big_endian>::setup(
bae7f79e
ILT
279 const elfcpp::Ehdr<size, big_endian>& ehdr)
280{
dbe717ef
ILT
281 this->set_target(ehdr.get_e_machine(), size, big_endian,
282 ehdr.get_e_ident()[elfcpp::EI_OSABI],
283 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
12e14209 284
dbe717ef 285 const unsigned int shnum = this->elf_file_.shnum();
a2fb1b05 286 this->set_shnum(shnum);
dbe717ef 287}
12e14209 288
dbe717ef
ILT
289// Find the SHT_SYMTAB section, given the section headers. The ELF
290// standard says that maybe in the future there can be more than one
291// SHT_SYMTAB section. Until somebody figures out how that could
292// work, we assume there is only one.
12e14209 293
dbe717ef
ILT
294template<int size, bool big_endian>
295void
296Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
297{
298 const unsigned int shnum = this->shnum();
299 this->symtab_shndx_ = 0;
300 if (shnum > 0)
bae7f79e 301 {
dbe717ef
ILT
302 // Look through the sections in reverse order, since gas tends
303 // to put the symbol table at the end.
304 const unsigned char* p = pshdrs + shnum * This::shdr_size;
305 unsigned int i = shnum;
d491d34e
ILT
306 unsigned int xindex_shndx = 0;
307 unsigned int xindex_link = 0;
dbe717ef 308 while (i > 0)
bae7f79e 309 {
dbe717ef
ILT
310 --i;
311 p -= This::shdr_size;
312 typename This::Shdr shdr(p);
313 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
314 {
315 this->symtab_shndx_ = i;
d491d34e
ILT
316 if (xindex_shndx > 0 && xindex_link == i)
317 {
318 Xindex* xindex =
319 new Xindex(this->elf_file_.large_shndx_offset());
320 xindex->read_symtab_xindex<size, big_endian>(this,
321 xindex_shndx,
322 pshdrs);
323 this->set_xindex(xindex);
324 }
dbe717ef
ILT
325 break;
326 }
d491d34e
ILT
327
328 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
329 // one. This will work if it follows the SHT_SYMTAB
330 // section.
331 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
332 {
333 xindex_shndx = i;
334 xindex_link = this->adjust_shndx(shdr.get_sh_link());
335 }
bae7f79e 336 }
bae7f79e
ILT
337 }
338}
339
d491d34e
ILT
340// Return the Xindex structure to use for object with lots of
341// sections.
342
343template<int size, bool big_endian>
344Xindex*
345Sized_relobj<size, big_endian>::do_initialize_xindex()
346{
347 gold_assert(this->symtab_shndx_ != -1U);
348 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
349 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
350 return xindex;
351}
352
730cdc88
ILT
353// Return whether SHDR has the right type and flags to be a GNU
354// .eh_frame section.
355
356template<int size, bool big_endian>
357bool
358Sized_relobj<size, big_endian>::check_eh_frame_flags(
359 const elfcpp::Shdr<size, big_endian>* shdr) const
360{
2c38906f 361 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
1650c4ff 362 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
730cdc88
ILT
363}
364
365// Return whether there is a GNU .eh_frame section, given the section
366// headers and the section names.
367
368template<int size, bool big_endian>
369bool
8383303e
ILT
370Sized_relobj<size, big_endian>::find_eh_frame(
371 const unsigned char* pshdrs,
372 const char* names,
373 section_size_type names_size) const
730cdc88
ILT
374{
375 const unsigned int shnum = this->shnum();
376 const unsigned char* p = pshdrs + This::shdr_size;
377 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
378 {
379 typename This::Shdr shdr(p);
380 if (this->check_eh_frame_flags(&shdr))
381 {
382 if (shdr.get_sh_name() >= names_size)
383 {
384 this->error(_("bad section name offset for section %u: %lu"),
385 i, static_cast<unsigned long>(shdr.get_sh_name()));
386 continue;
387 }
388
389 const char* name = names + shdr.get_sh_name();
390 if (strcmp(name, ".eh_frame") == 0)
391 return true;
392 }
393 }
394 return false;
395}
396
12e14209 397// Read the sections and symbols from an object file.
bae7f79e
ILT
398
399template<int size, bool big_endian>
12e14209 400void
f6ce93d6 401Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
bae7f79e 402{
dbe717ef 403 this->read_section_data(&this->elf_file_, sd);
12e14209 404
dbe717ef
ILT
405 const unsigned char* const pshdrs = sd->section_headers->data();
406
407 this->find_symtab(pshdrs);
12e14209 408
730cdc88
ILT
409 const unsigned char* namesu = sd->section_names->data();
410 const char* names = reinterpret_cast<const char*>(namesu);
1650c4ff
ILT
411 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
412 {
413 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
414 this->has_eh_frame_ = true;
415 }
730cdc88 416
75f2446e
ILT
417 sd->symbols = NULL;
418 sd->symbols_size = 0;
730cdc88 419 sd->external_symbols_offset = 0;
75f2446e
ILT
420 sd->symbol_names = NULL;
421 sd->symbol_names_size = 0;
422
645f8123 423 if (this->symtab_shndx_ == 0)
bae7f79e
ILT
424 {
425 // No symbol table. Weird but legal.
12e14209 426 return;
bae7f79e
ILT
427 }
428
12e14209
ILT
429 // Get the symbol table section header.
430 typename This::Shdr symtabshdr(pshdrs
645f8123 431 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 432 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
bae7f79e 433
730cdc88
ILT
434 // If this object has a .eh_frame section, we need all the symbols.
435 // Otherwise we only need the external symbols. While it would be
436 // simpler to just always read all the symbols, I've seen object
437 // files with well over 2000 local symbols, which for a 64-bit
438 // object file format is over 5 pages that we don't need to read
439 // now.
440
75f65a3e 441 const int sym_size = This::sym_size;
92e059d8
ILT
442 const unsigned int loccount = symtabshdr.get_sh_info();
443 this->local_symbol_count_ = loccount;
7bf1f802 444 this->local_values_.resize(loccount);
8383303e 445 section_offset_type locsize = loccount * sym_size;
730cdc88 446 off_t dataoff = symtabshdr.get_sh_offset();
8383303e
ILT
447 section_size_type datasize =
448 convert_to_section_size_type(symtabshdr.get_sh_size());
730cdc88 449 off_t extoff = dataoff + locsize;
8383303e 450 section_size_type extsize = datasize - locsize;
75f65a3e 451
730cdc88 452 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
8383303e 453 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
730cdc88 454
39d0cb0e 455 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
bae7f79e
ILT
456
457 // Read the section header for the symbol names.
d491d34e 458 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
dbe717ef 459 if (strtab_shndx >= this->shnum())
bae7f79e 460 {
75f2446e
ILT
461 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
462 return;
bae7f79e 463 }
dbe717ef 464 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
bae7f79e
ILT
465 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
466 {
75f2446e
ILT
467 this->error(_("symbol table name section has wrong type: %u"),
468 static_cast<unsigned int>(strtabshdr.get_sh_type()));
469 return;
bae7f79e
ILT
470 }
471
472 // Read the symbol names.
473 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
39d0cb0e
ILT
474 strtabshdr.get_sh_size(),
475 false, true);
bae7f79e 476
12e14209 477 sd->symbols = fvsymtab;
730cdc88
ILT
478 sd->symbols_size = readsize;
479 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
12e14209 480 sd->symbol_names = fvstrtab;
8383303e
ILT
481 sd->symbol_names_size =
482 convert_to_section_size_type(strtabshdr.get_sh_size());
a2fb1b05
ILT
483}
484
730cdc88 485// Return the section index of symbol SYM. Set *VALUE to its value in
d491d34e
ILT
486// the object file. Set *IS_ORDINARY if this is an ordinary section
487// index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
488// Note that for a symbol which is not defined in this object file,
489// this will set *VALUE to 0 and return SHN_UNDEF; it will not return
490// the final value of the symbol in the link.
730cdc88
ILT
491
492template<int size, bool big_endian>
493unsigned int
494Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
d491d34e
ILT
495 Address* value,
496 bool* is_ordinary)
730cdc88 497{
8383303e 498 section_size_type symbols_size;
730cdc88
ILT
499 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
500 &symbols_size,
501 false);
502
503 const size_t count = symbols_size / This::sym_size;
504 gold_assert(sym < count);
505
506 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
507 *value = elfsym.get_st_value();
d491d34e
ILT
508
509 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
730cdc88
ILT
510}
511
a2fb1b05
ILT
512// Return whether to include a section group in the link. LAYOUT is
513// used to keep track of which section groups we have already seen.
514// INDEX is the index of the section group and SHDR is the section
515// header. If we do not want to include this group, we set bits in
516// OMIT for each section which should be discarded.
517
518template<int size, bool big_endian>
519bool
f6ce93d6 520Sized_relobj<size, big_endian>::include_section_group(
6a74a719 521 Symbol_table* symtab,
a2fb1b05
ILT
522 Layout* layout,
523 unsigned int index,
6a74a719 524 const char* name,
e94cf127
CC
525 const unsigned char* shdrs,
526 const char* section_names,
527 section_size_type section_names_size,
a2fb1b05
ILT
528 std::vector<bool>* omit)
529{
530 // Read the section contents.
e94cf127 531 typename This::Shdr shdr(shdrs + index * This::shdr_size);
a2fb1b05 532 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
39d0cb0e 533 shdr.get_sh_size(), true, false);
a2fb1b05
ILT
534 const elfcpp::Elf_Word* pword =
535 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
536
537 // The first word contains flags. We only care about COMDAT section
538 // groups. Other section groups are always included in the link
539 // just like ordinary sections.
f6ce93d6 540 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
a2fb1b05
ILT
541
542 // Look up the group signature, which is the name of a symbol. This
543 // is a lot of effort to go to to read a string. Why didn't they
6a74a719
ILT
544 // just have the group signature point into the string table, rather
545 // than indirect through a symbol?
a2fb1b05
ILT
546
547 // Get the appropriate symbol table header (this will normally be
548 // the single SHT_SYMTAB section, but in principle it need not be).
d491d34e 549 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
645f8123 550 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
a2fb1b05
ILT
551
552 // Read the symbol table entry.
d491d34e
ILT
553 unsigned int symndx = shdr.get_sh_info();
554 if (symndx >= symshdr.get_sh_size() / This::sym_size)
a2fb1b05 555 {
75f2446e 556 this->error(_("section group %u info %u out of range"),
d491d34e 557 index, symndx);
75f2446e 558 return false;
a2fb1b05 559 }
d491d34e 560 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
39d0cb0e
ILT
561 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
562 false);
a2fb1b05
ILT
563 elfcpp::Sym<size, big_endian> sym(psym);
564
a2fb1b05 565 // Read the symbol table names.
8383303e 566 section_size_type symnamelen;
645f8123 567 const unsigned char* psymnamesu;
d491d34e
ILT
568 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
569 &symnamelen, true);
a2fb1b05
ILT
570 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
571
572 // Get the section group signature.
645f8123 573 if (sym.get_st_name() >= symnamelen)
a2fb1b05 574 {
75f2446e 575 this->error(_("symbol %u name offset %u out of range"),
d491d34e 576 symndx, sym.get_st_name());
75f2446e 577 return false;
a2fb1b05
ILT
578 }
579
e94cf127 580 std::string signature(psymnames + sym.get_st_name());
a2fb1b05 581
ead1e424
ILT
582 // It seems that some versions of gas will create a section group
583 // associated with a section symbol, and then fail to give a name to
584 // the section symbol. In such a case, use the name of the section.
645f8123 585 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
ead1e424 586 {
d491d34e
ILT
587 bool is_ordinary;
588 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
589 sym.get_st_shndx(),
590 &is_ordinary);
591 if (!is_ordinary || sym_shndx >= this->shnum())
592 {
593 this->error(_("symbol %u invalid section index %u"),
594 symndx, sym_shndx);
595 return false;
596 }
e94cf127
CC
597 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
598 if (member_shdr.get_sh_name() < section_names_size)
599 signature = section_names + member_shdr.get_sh_name();
ead1e424
ILT
600 }
601
e94cf127
CC
602 // Record this section group in the layout, and see whether we've already
603 // seen one with the same signature.
604 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
605 || layout->add_comdat(this, index, signature, true));
606
607 if (include_group && parameters->options().relocatable())
608 layout->layout_group(symtab, this, index, name, signature.c_str(),
609 shdr, pword);
610
611 Relobj* kept_object = NULL;
612 Comdat_group* kept_group = NULL;
6a74a719 613
e94cf127 614 if (!include_group)
6a74a719 615 {
e94cf127
CC
616 // This group is being discarded. Find the object and group
617 // that was kept in its place.
618 unsigned int kept_group_index = 0;
619 kept_object = layout->find_kept_object(signature, &kept_group_index);
620 if (kept_object != NULL)
621 kept_group = kept_object->find_comdat_group(kept_group_index);
622 }
623 else if (flags & elfcpp::GRP_COMDAT)
624 {
625 // This group is being kept. Create the table to map section names
626 // to section indexes and add it to the table of groups.
627 kept_group = new Comdat_group();
628 this->add_comdat_group(index, kept_group);
6a74a719 629 }
a2fb1b05 630
a2fb1b05
ILT
631 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
632 for (size_t i = 1; i < count; ++i)
633 {
f6ce93d6
ILT
634 elfcpp::Elf_Word secnum =
635 elfcpp::Swap<32, big_endian>::readval(pword + i);
a2fb1b05
ILT
636 if (secnum >= this->shnum())
637 {
75f2446e
ILT
638 this->error(_("section %u in section group %u out of range"),
639 secnum, index);
640 continue;
a2fb1b05 641 }
55438702
ILT
642
643 // Check for an earlier section number, since we're going to get
644 // it wrong--we may have already decided to include the section.
645 if (secnum < index)
646 this->error(_("invalid section group %u refers to earlier section %u"),
647 index, secnum);
648
e94cf127
CC
649 // Get the name of the member section.
650 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
651 if (member_shdr.get_sh_name() >= section_names_size)
652 {
653 // This is an error, but it will be diagnosed eventually
654 // in do_layout, so we don't need to do anything here but
655 // ignore it.
656 continue;
657 }
658 std::string mname(section_names + member_shdr.get_sh_name());
659
660 if (!include_group)
661 {
662 (*omit)[secnum] = true;
663 if (kept_group != NULL)
664 {
665 // Find the corresponding kept section, and store that info
666 // in the discarded section table.
667 Comdat_group::const_iterator p = kept_group->find(mname);
668 if (p != kept_group->end())
669 {
670 Kept_comdat_section* kept =
671 new Kept_comdat_section(kept_object, p->second);
672 this->set_kept_comdat_section(secnum, kept);
673 }
674 }
675 }
676 else if (flags & elfcpp::GRP_COMDAT)
677 {
678 // Add the section to the kept group table.
679 gold_assert(kept_group != NULL);
680 kept_group->insert(std::make_pair(mname, secnum));
681 }
a2fb1b05
ILT
682 }
683
e94cf127 684 return include_group;
a2fb1b05
ILT
685}
686
687// Whether to include a linkonce section in the link. NAME is the
688// name of the section and SHDR is the section header.
689
690// Linkonce sections are a GNU extension implemented in the original
691// GNU linker before section groups were defined. The semantics are
692// that we only include one linkonce section with a given name. The
693// name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
694// where T is the type of section and SYMNAME is the name of a symbol.
695// In an attempt to make linkonce sections interact well with section
696// groups, we try to identify SYMNAME and use it like a section group
697// signature. We want to block section groups with that signature,
698// but not other linkonce sections with that signature. We also use
699// the full name of the linkonce section as a normal section group
700// signature.
701
702template<int size, bool big_endian>
703bool
f6ce93d6 704Sized_relobj<size, big_endian>::include_linkonce_section(
a2fb1b05 705 Layout* layout,
e94cf127 706 unsigned int index,
a2fb1b05
ILT
707 const char* name,
708 const elfcpp::Shdr<size, big_endian>&)
709{
ad435a24
ILT
710 // In general the symbol name we want will be the string following
711 // the last '.'. However, we have to handle the case of
712 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
713 // some versions of gcc. So we use a heuristic: if the name starts
714 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
715 // we look for the last '.'. We can't always simply skip
716 // ".gnu.linkonce.X", because we have to deal with cases like
717 // ".gnu.linkonce.d.rel.ro.local".
718 const char* const linkonce_t = ".gnu.linkonce.t.";
719 const char* symname;
720 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
721 symname = name + strlen(linkonce_t);
722 else
723 symname = strrchr(name, '.') + 1;
e94cf127
CC
724 std::string sig1(symname);
725 std::string sig2(name);
726 bool include1 = layout->add_comdat(this, index, sig1, false);
727 bool include2 = layout->add_comdat(this, index, sig2, true);
728
729 if (!include2)
730 {
731 // The section is being discarded on the basis of its section
732 // name (i.e., the kept section was also a linkonce section).
733 // In this case, the section index stored with the layout object
734 // is the linkonce section that was kept.
735 unsigned int kept_group_index = 0;
736 Relobj* kept_object = layout->find_kept_object(sig2, &kept_group_index);
737 if (kept_object != NULL)
738 {
739 Kept_comdat_section* kept =
740 new Kept_comdat_section(kept_object, kept_group_index);
741 this->set_kept_comdat_section(index, kept);
742 }
743 }
744 else if (!include1)
745 {
746 // The section is being discarded on the basis of its symbol
747 // name. This means that the corresponding kept section was
748 // part of a comdat group, and it will be difficult to identify
749 // the specific section within that group that corresponds to
750 // this linkonce section. We'll handle the simple case where
751 // the group has only one member section. Otherwise, it's not
752 // worth the effort.
753 unsigned int kept_group_index = 0;
754 Relobj* kept_object = layout->find_kept_object(sig1, &kept_group_index);
755 if (kept_object != NULL)
756 {
757 Comdat_group* kept_group =
758 kept_object->find_comdat_group(kept_group_index);
759 if (kept_group != NULL && kept_group->size() == 1)
760 {
761 Comdat_group::const_iterator p = kept_group->begin();
762 gold_assert(p != kept_group->end());
763 Kept_comdat_section* kept =
764 new Kept_comdat_section(kept_object, p->second);
765 this->set_kept_comdat_section(index, kept);
766 }
767 }
768 }
769
a783673b 770 return include1 && include2;
a2fb1b05
ILT
771}
772
773// Lay out the input sections. We walk through the sections and check
774// whether they should be included in the link. If they should, we
775// pass them to the Layout object, which will return an output section
776// and an offset.
777
778template<int size, bool big_endian>
779void
7e1edb90 780Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
f6ce93d6 781 Layout* layout,
12e14209 782 Read_symbols_data* sd)
a2fb1b05 783{
dbe717ef 784 const unsigned int shnum = this->shnum();
12e14209
ILT
785 if (shnum == 0)
786 return;
a2fb1b05
ILT
787
788 // Get the section headers.
e94cf127
CC
789 const unsigned char* shdrs = sd->section_headers->data();
790 const unsigned char* pshdrs;
a2fb1b05
ILT
791
792 // Get the section names.
12e14209 793 const unsigned char* pnamesu = sd->section_names->data();
a2fb1b05
ILT
794 const char* pnames = reinterpret_cast<const char*>(pnamesu);
795
730cdc88
ILT
796 // For each section, record the index of the reloc section if any.
797 // Use 0 to mean that there is no reloc section, -1U to mean that
798 // there is more than one.
799 std::vector<unsigned int> reloc_shndx(shnum, 0);
800 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
801 // Skip the first, dummy, section.
e94cf127 802 pshdrs = shdrs + This::shdr_size;
730cdc88
ILT
803 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
804 {
805 typename This::Shdr shdr(pshdrs);
806
807 unsigned int sh_type = shdr.get_sh_type();
808 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
809 {
d491d34e 810 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
730cdc88
ILT
811 if (target_shndx == 0 || target_shndx >= shnum)
812 {
813 this->error(_("relocation section %u has bad info %u"),
814 i, target_shndx);
815 continue;
816 }
817
818 if (reloc_shndx[target_shndx] != 0)
819 reloc_shndx[target_shndx] = -1U;
820 else
821 {
822 reloc_shndx[target_shndx] = i;
823 reloc_type[target_shndx] = sh_type;
824 }
825 }
826 }
827
a2fb1b05 828 std::vector<Map_to_output>& map_sections(this->map_to_output());
61ba1cf9 829 map_sections.resize(shnum);
a2fb1b05 830
88dd47ac
ILT
831 // If we are only linking for symbols, then there is nothing else to
832 // do here.
833 if (this->input_file()->just_symbols())
834 {
835 delete sd->section_headers;
836 sd->section_headers = NULL;
837 delete sd->section_names;
838 sd->section_names = NULL;
839 return;
840 }
841
35cdfc9a
ILT
842 // Whether we've seen a .note.GNU-stack section.
843 bool seen_gnu_stack = false;
844 // The flags of a .note.GNU-stack section.
845 uint64_t gnu_stack_flags = 0;
846
a2fb1b05
ILT
847 // Keep track of which sections to omit.
848 std::vector<bool> omit(shnum, false);
849
7019cd25 850 // Keep track of reloc sections when emitting relocations.
8851ecca
ILT
851 const bool relocatable = parameters->options().relocatable();
852 const bool emit_relocs = (relocatable
853 || parameters->options().emit_relocs());
6a74a719
ILT
854 std::vector<unsigned int> reloc_sections;
855
730cdc88
ILT
856 // Keep track of .eh_frame sections.
857 std::vector<unsigned int> eh_frame_sections;
858
f6ce93d6 859 // Skip the first, dummy, section.
e94cf127 860 pshdrs = shdrs + This::shdr_size;
f6ce93d6 861 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
a2fb1b05 862 {
75f65a3e 863 typename This::Shdr shdr(pshdrs);
a2fb1b05 864
12e14209 865 if (shdr.get_sh_name() >= sd->section_names_size)
a2fb1b05 866 {
75f2446e
ILT
867 this->error(_("bad section name offset for section %u: %lu"),
868 i, static_cast<unsigned long>(shdr.get_sh_name()));
869 return;
a2fb1b05
ILT
870 }
871
872 const char* name = pnames + shdr.get_sh_name();
873
dbe717ef 874 if (this->handle_gnu_warning_section(name, i, symtab))
f6ce93d6 875 {
8851ecca 876 if (!relocatable)
f6ce93d6
ILT
877 omit[i] = true;
878 }
879
35cdfc9a
ILT
880 // The .note.GNU-stack section is special. It gives the
881 // protection flags that this object file requires for the stack
882 // in memory.
883 if (strcmp(name, ".note.GNU-stack") == 0)
884 {
885 seen_gnu_stack = true;
886 gnu_stack_flags |= shdr.get_sh_flags();
887 omit[i] = true;
888 }
889
a2fb1b05
ILT
890 bool discard = omit[i];
891 if (!discard)
892 {
893 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
894 {
e94cf127
CC
895 if (!this->include_section_group(symtab, layout, i, name, shdrs,
896 pnames, sd->section_names_size,
6a74a719 897 &omit))
a2fb1b05
ILT
898 discard = true;
899 }
cba134d6
ILT
900 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
901 && Layout::is_linkonce(name))
a2fb1b05 902 {
e94cf127 903 if (!this->include_linkonce_section(layout, i, name, shdr))
a2fb1b05
ILT
904 discard = true;
905 }
906 }
907
908 if (discard)
909 {
910 // Do not include this section in the link.
911 map_sections[i].output_section = NULL;
912 continue;
913 }
914
6a74a719
ILT
915 // When doing a relocatable link we are going to copy input
916 // reloc sections into the output. We only want to copy the
917 // ones associated with sections which are not being discarded.
918 // However, we don't know that yet for all sections. So save
919 // reloc sections and process them later.
7019cd25 920 if (emit_relocs
6a74a719
ILT
921 && (shdr.get_sh_type() == elfcpp::SHT_REL
922 || shdr.get_sh_type() == elfcpp::SHT_RELA))
923 {
924 reloc_sections.push_back(i);
925 continue;
926 }
927
8851ecca 928 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
6a74a719
ILT
929 continue;
930
730cdc88
ILT
931 // The .eh_frame section is special. It holds exception frame
932 // information that we need to read in order to generate the
933 // exception frame header. We process these after all the other
934 // sections so that the exception frame reader can reliably
935 // determine which sections are being discarded, and discard the
936 // corresponding information.
8851ecca 937 if (!relocatable
730cdc88
ILT
938 && strcmp(name, ".eh_frame") == 0
939 && this->check_eh_frame_flags(&shdr))
940 {
941 eh_frame_sections.push_back(i);
942 continue;
943 }
944
a2fb1b05 945 off_t offset;
730cdc88
ILT
946 Output_section* os = layout->layout(this, i, name, shdr,
947 reloc_shndx[i], reloc_type[i],
948 &offset);
a2fb1b05
ILT
949
950 map_sections[i].output_section = os;
951 map_sections[i].offset = offset;
730cdc88
ILT
952
953 // If this section requires special handling, and if there are
954 // relocs that apply to it, then we must do the special handling
955 // before we apply the relocs.
956 if (offset == -1 && reloc_shndx[i] != 0)
957 this->set_relocs_must_follow_section_writes();
12e14209
ILT
958 }
959
35cdfc9a
ILT
960 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
961
6a74a719
ILT
962 // When doing a relocatable link handle the reloc sections at the
963 // end.
7019cd25 964 if (emit_relocs)
6a74a719
ILT
965 this->size_relocatable_relocs();
966 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
967 p != reloc_sections.end();
968 ++p)
969 {
970 unsigned int i = *p;
971 const unsigned char* pshdr;
972 pshdr = sd->section_headers->data() + i * This::shdr_size;
973 typename This::Shdr shdr(pshdr);
974
d491d34e 975 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
6a74a719
ILT
976 if (data_shndx >= shnum)
977 {
978 // We already warned about this above.
979 continue;
980 }
981
982 Output_section* data_section = map_sections[data_shndx].output_section;
983 if (data_section == NULL)
984 {
985 map_sections[i].output_section = NULL;
986 continue;
987 }
988
989 Relocatable_relocs* rr = new Relocatable_relocs();
990 this->set_relocatable_relocs(i, rr);
991
992 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
993 rr);
994 map_sections[i].output_section = os;
995 map_sections[i].offset = -1;
996 }
997
730cdc88
ILT
998 // Handle the .eh_frame sections at the end.
999 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1000 p != eh_frame_sections.end();
1001 ++p)
1002 {
1003 gold_assert(this->has_eh_frame_);
1004 gold_assert(sd->external_symbols_offset != 0);
1005
1006 unsigned int i = *p;
1007 const unsigned char *pshdr;
1008 pshdr = sd->section_headers->data() + i * This::shdr_size;
1009 typename This::Shdr shdr(pshdr);
1010
1011 off_t offset;
1012 Output_section* os = layout->layout_eh_frame(this,
1013 sd->symbols->data(),
1014 sd->symbols_size,
1015 sd->symbol_names->data(),
1016 sd->symbol_names_size,
1017 i, shdr,
1018 reloc_shndx[i],
1019 reloc_type[i],
1020 &offset);
1021 map_sections[i].output_section = os;
1022 map_sections[i].offset = offset;
1023
1024 // If this section requires special handling, and if there are
1025 // relocs that apply to it, then we must do the special handling
1026 // before we apply the relocs.
1027 if (offset == -1 && reloc_shndx[i] != 0)
1028 this->set_relocs_must_follow_section_writes();
1029 }
1030
12e14209
ILT
1031 delete sd->section_headers;
1032 sd->section_headers = NULL;
1033 delete sd->section_names;
1034 sd->section_names = NULL;
1035}
1036
1037// Add the symbols to the symbol table.
1038
1039template<int size, bool big_endian>
1040void
f6ce93d6 1041Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
12e14209
ILT
1042 Read_symbols_data* sd)
1043{
1044 if (sd->symbols == NULL)
1045 {
a3ad94ed 1046 gold_assert(sd->symbol_names == NULL);
12e14209
ILT
1047 return;
1048 }
a2fb1b05 1049
12e14209 1050 const int sym_size = This::sym_size;
730cdc88
ILT
1051 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1052 / sym_size);
8383303e 1053 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
12e14209 1054 {
75f2446e
ILT
1055 this->error(_("size of symbols is not multiple of symbol size"));
1056 return;
a2fb1b05 1057 }
12e14209 1058
730cdc88 1059 this->symbols_.resize(symcount);
12e14209 1060
12e14209
ILT
1061 const char* sym_names =
1062 reinterpret_cast<const char*>(sd->symbol_names->data());
730cdc88
ILT
1063 symtab->add_from_relobj(this,
1064 sd->symbols->data() + sd->external_symbols_offset,
7fcd3aa9 1065 symcount, this->local_symbol_count_,
d491d34e 1066 sym_names, sd->symbol_names_size,
730cdc88 1067 &this->symbols_);
12e14209
ILT
1068
1069 delete sd->symbols;
1070 sd->symbols = NULL;
1071 delete sd->symbol_names;
1072 sd->symbol_names = NULL;
bae7f79e
ILT
1073}
1074
cb295612
ILT
1075// First pass over the local symbols. Here we add their names to
1076// *POOL and *DYNPOOL, and we store the symbol value in
1077// THIS->LOCAL_VALUES_. This function is always called from a
1078// singleton thread. This is followed by a call to
1079// finalize_local_symbols.
75f65a3e
ILT
1080
1081template<int size, bool big_endian>
7bf1f802
ILT
1082void
1083Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1084 Stringpool* dynpool)
75f65a3e 1085{
a3ad94ed 1086 gold_assert(this->symtab_shndx_ != -1U);
645f8123 1087 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
1088 {
1089 // This object has no symbols. Weird but legal.
7bf1f802 1090 return;
61ba1cf9
ILT
1091 }
1092
75f65a3e 1093 // Read the symbol table section header.
645f8123
ILT
1094 const unsigned int symtab_shndx = this->symtab_shndx_;
1095 typename This::Shdr symtabshdr(this,
1096 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 1097 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
75f65a3e
ILT
1098
1099 // Read the local symbols.
75f65a3e 1100 const int sym_size = This::sym_size;
92e059d8 1101 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 1102 gold_assert(loccount == symtabshdr.get_sh_info());
75f65a3e
ILT
1103 off_t locsize = loccount * sym_size;
1104 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
39d0cb0e 1105 locsize, true, true);
75f65a3e 1106
75f65a3e 1107 // Read the symbol names.
d491d34e
ILT
1108 const unsigned int strtab_shndx =
1109 this->adjust_shndx(symtabshdr.get_sh_link());
8383303e 1110 section_size_type strtab_size;
645f8123 1111 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57
ILT
1112 &strtab_size,
1113 true);
75f65a3e
ILT
1114 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1115
1116 // Loop over the local symbols.
1117
c06b7b0b 1118 const std::vector<Map_to_output>& mo(this->map_to_output());
75f65a3e 1119 unsigned int shnum = this->shnum();
61ba1cf9 1120 unsigned int count = 0;
7bf1f802 1121 unsigned int dyncount = 0;
75f65a3e
ILT
1122 // Skip the first, dummy, symbol.
1123 psyms += sym_size;
61ba1cf9 1124 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
75f65a3e
ILT
1125 {
1126 elfcpp::Sym<size, big_endian> sym(psyms);
1127
b8e6aad9
ILT
1128 Symbol_value<size>& lv(this->local_values_[i]);
1129
d491d34e
ILT
1130 bool is_ordinary;
1131 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1132 &is_ordinary);
1133 lv.set_input_shndx(shndx, is_ordinary);
75f65a3e 1134
063f12a8
ILT
1135 if (sym.get_st_type() == elfcpp::STT_SECTION)
1136 lv.set_is_section_symbol();
7bf1f802
ILT
1137 else if (sym.get_st_type() == elfcpp::STT_TLS)
1138 lv.set_is_tls_symbol();
1139
1140 // Save the input symbol value for use in do_finalize_local_symbols().
1141 lv.set_input_value(sym.get_st_value());
1142
1143 // Decide whether this symbol should go into the output file.
063f12a8 1144
7bf1f802
ILT
1145 if (shndx < shnum && mo[shndx].output_section == NULL)
1146 {
1147 lv.set_no_output_symtab_entry();
dceae3c1 1148 gold_assert(!lv.needs_output_dynsym_entry());
7bf1f802
ILT
1149 continue;
1150 }
1151
1152 if (sym.get_st_type() == elfcpp::STT_SECTION)
1153 {
1154 lv.set_no_output_symtab_entry();
dceae3c1 1155 gold_assert(!lv.needs_output_dynsym_entry());
7bf1f802
ILT
1156 continue;
1157 }
1158
1159 if (sym.get_st_name() >= strtab_size)
1160 {
1161 this->error(_("local symbol %u section name out of range: %u >= %u"),
1162 i, sym.get_st_name(),
1163 static_cast<unsigned int>(strtab_size));
1164 lv.set_no_output_symtab_entry();
1165 continue;
1166 }
1167
1168 // Add the symbol to the symbol table string pool.
1169 const char* name = pnames + sym.get_st_name();
1170 pool->add(name, true, NULL);
1171 ++count;
1172
1173 // If needed, add the symbol to the dynamic symbol table string pool.
1174 if (lv.needs_output_dynsym_entry())
1175 {
1176 dynpool->add(name, true, NULL);
1177 ++dyncount;
1178 }
1179 }
1180
1181 this->output_local_symbol_count_ = count;
1182 this->output_local_dynsym_count_ = dyncount;
1183}
1184
cb295612 1185// Finalize the local symbols. Here we set the final value in
7bf1f802 1186// THIS->LOCAL_VALUES_ and set their output symbol table indexes.
17a1d0a9 1187// This function is always called from a singleton thread. The actual
7bf1f802
ILT
1188// output of the local symbols will occur in a separate task.
1189
1190template<int size, bool big_endian>
1191unsigned int
1192Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
d491d34e 1193 off_t off)
7bf1f802
ILT
1194{
1195 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1196
1197 const unsigned int loccount = this->local_symbol_count_;
1198 this->local_symbol_offset_ = off;
1199
1200 const std::vector<Map_to_output>& mo(this->map_to_output());
1201 unsigned int shnum = this->shnum();
1202
1203 for (unsigned int i = 1; i < loccount; ++i)
1204 {
1205 Symbol_value<size>& lv(this->local_values_[i]);
1206
d491d34e
ILT
1207 bool is_ordinary;
1208 unsigned int shndx = lv.input_shndx(&is_ordinary);
7bf1f802
ILT
1209
1210 // Set the output symbol value.
1211
d491d34e 1212 if (!is_ordinary)
75f65a3e 1213 {
0dfbdef4 1214 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
7bf1f802 1215 lv.set_output_value(lv.input_value());
61ba1cf9 1216 else
75f65a3e 1217 {
75f2446e
ILT
1218 this->error(_("unknown section index %u for local symbol %u"),
1219 shndx, i);
1220 lv.set_output_value(0);
75f65a3e 1221 }
75f65a3e
ILT
1222 }
1223 else
1224 {
1225 if (shndx >= shnum)
1226 {
75f2446e
ILT
1227 this->error(_("local symbol %u section index %u out of range"),
1228 i, shndx);
1229 shndx = 0;
75f65a3e
ILT
1230 }
1231
b8e6aad9
ILT
1232 Output_section* os = mo[shndx].output_section;
1233
1234 if (os == NULL)
61ba1cf9 1235 {
e94cf127
CC
1236 // This local symbol belongs to a section we are discarding.
1237 // In some cases when applying relocations later, we will
1238 // attempt to match it to the corresponding kept section,
1239 // so we leave the input value unchanged here.
61ba1cf9
ILT
1240 continue;
1241 }
7bf1f802
ILT
1242 else if (mo[shndx].offset == -1)
1243 {
a9a60db6
ILT
1244 // This is a SHF_MERGE section or one which otherwise
1245 // requires special handling. We get the output address
1246 // of the start of the merged section. If this is not a
1247 // section symbol, we can then determine the final
1248 // value. If it is a section symbol, we can not, as in
1249 // that case we have to consider the addend to determine
1250 // the value to use in a relocation.
a9a60db6 1251 if (!lv.is_section_symbol())
8d32f935
ILT
1252 lv.set_output_value(os->output_address(this, shndx,
1253 lv.input_value()));
a9a60db6
ILT
1254 else
1255 {
8d32f935
ILT
1256 section_offset_type start =
1257 os->starting_output_address(this, shndx);
a9a60db6
ILT
1258 Merged_symbol_value<size>* msv =
1259 new Merged_symbol_value<size>(lv.input_value(), start);
1260 lv.set_merged_symbol_value(msv);
1261 }
7bf1f802
ILT
1262 }
1263 else if (lv.is_tls_symbol())
a9a60db6 1264 lv.set_output_value(os->tls_offset()
7bf1f802
ILT
1265 + mo[shndx].offset
1266 + lv.input_value());
b8e6aad9 1267 else
a9a60db6 1268 lv.set_output_value(os->address()
b8e6aad9 1269 + mo[shndx].offset
7bf1f802 1270 + lv.input_value());
75f65a3e
ILT
1271 }
1272
7bf1f802
ILT
1273 if (lv.needs_output_symtab_entry())
1274 {
1275 lv.set_output_symtab_index(index);
1276 ++index;
1277 }
1278 }
1279 return index;
1280}
645f8123 1281
7bf1f802 1282// Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 1283
7bf1f802
ILT
1284template<int size, bool big_endian>
1285unsigned int
1286Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1287{
1288 const unsigned int loccount = this->local_symbol_count_;
1289 for (unsigned int i = 1; i < loccount; ++i)
1290 {
1291 Symbol_value<size>& lv(this->local_values_[i]);
1292 if (lv.needs_output_dynsym_entry())
1293 {
1294 lv.set_output_dynsym_index(index);
1295 ++index;
1296 }
75f65a3e 1297 }
7bf1f802
ILT
1298 return index;
1299}
75f65a3e 1300
7bf1f802
ILT
1301// Set the offset where local dynamic symbol information will be stored.
1302// Returns the count of local symbols contributed to the symbol table by
1303// this object.
61ba1cf9 1304
7bf1f802
ILT
1305template<int size, bool big_endian>
1306unsigned int
1307Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1308{
1309 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1310 this->local_dynsym_offset_ = off;
1311 return this->output_local_dynsym_count_;
75f65a3e
ILT
1312}
1313
61ba1cf9
ILT
1314// Write out the local symbols.
1315
1316template<int size, bool big_endian>
1317void
17a1d0a9
ILT
1318Sized_relobj<size, big_endian>::write_local_symbols(
1319 Output_file* of,
1320 const Stringpool* sympool,
d491d34e
ILT
1321 const Stringpool* dynpool,
1322 Output_symtab_xindex* symtab_xindex,
1323 Output_symtab_xindex* dynsym_xindex)
61ba1cf9 1324{
8851ecca
ILT
1325 if (parameters->options().strip_all()
1326 && this->output_local_dynsym_count_ == 0)
9e2dcb77
ILT
1327 return;
1328
a3ad94ed 1329 gold_assert(this->symtab_shndx_ != -1U);
645f8123 1330 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
1331 {
1332 // This object has no symbols. Weird but legal.
1333 return;
1334 }
1335
1336 // Read the symbol table section header.
645f8123
ILT
1337 const unsigned int symtab_shndx = this->symtab_shndx_;
1338 typename This::Shdr symtabshdr(this,
1339 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 1340 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8 1341 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 1342 gold_assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
1343
1344 // Read the local symbols.
1345 const int sym_size = This::sym_size;
92e059d8 1346 off_t locsize = loccount * sym_size;
61ba1cf9 1347 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
39d0cb0e 1348 locsize, true, false);
61ba1cf9 1349
61ba1cf9 1350 // Read the symbol names.
d491d34e
ILT
1351 const unsigned int strtab_shndx =
1352 this->adjust_shndx(symtabshdr.get_sh_link());
8383303e 1353 section_size_type strtab_size;
645f8123 1354 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57 1355 &strtab_size,
cb295612 1356 false);
61ba1cf9
ILT
1357 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1358
7bf1f802
ILT
1359 // Get views into the output file for the portions of the symbol table
1360 // and the dynamic symbol table that we will be writing.
61ba1cf9 1361 off_t output_size = this->output_local_symbol_count_ * sym_size;
f2619d6c 1362 unsigned char* oview = NULL;
7bf1f802
ILT
1363 if (output_size > 0)
1364 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1365
1366 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1367 unsigned char* dyn_oview = NULL;
1368 if (dyn_output_size > 0)
1369 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1370 dyn_output_size);
61ba1cf9 1371
c06b7b0b
ILT
1372 const std::vector<Map_to_output>& mo(this->map_to_output());
1373
a3ad94ed 1374 gold_assert(this->local_values_.size() == loccount);
61ba1cf9 1375
61ba1cf9 1376 unsigned char* ov = oview;
7bf1f802 1377 unsigned char* dyn_ov = dyn_oview;
c06b7b0b 1378 psyms += sym_size;
92e059d8 1379 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
1380 {
1381 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6 1382
d491d34e
ILT
1383 Symbol_value<size>& lv(this->local_values_[i]);
1384
1385 bool is_ordinary;
1386 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1387 &is_ordinary);
1388 if (is_ordinary)
61ba1cf9 1389 {
a3ad94ed 1390 gold_assert(st_shndx < mo.size());
61ba1cf9
ILT
1391 if (mo[st_shndx].output_section == NULL)
1392 continue;
ead1e424 1393 st_shndx = mo[st_shndx].output_section->out_shndx();
d491d34e
ILT
1394 if (st_shndx >= elfcpp::SHN_LORESERVE)
1395 {
1396 if (lv.needs_output_symtab_entry())
1397 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1398 if (lv.needs_output_dynsym_entry())
1399 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1400 st_shndx = elfcpp::SHN_XINDEX;
1401 }
61ba1cf9
ILT
1402 }
1403
7bf1f802 1404 // Write the symbol to the output symbol table.
8851ecca 1405 if (!parameters->options().strip_all()
d491d34e 1406 && lv.needs_output_symtab_entry())
7bf1f802
ILT
1407 {
1408 elfcpp::Sym_write<size, big_endian> osym(ov);
1409
1410 gold_assert(isym.get_st_name() < strtab_size);
1411 const char* name = pnames + isym.get_st_name();
1412 osym.put_st_name(sympool->get_offset(name));
1413 osym.put_st_value(this->local_values_[i].value(this, 0));
1414 osym.put_st_size(isym.get_st_size());
1415 osym.put_st_info(isym.get_st_info());
1416 osym.put_st_other(isym.get_st_other());
1417 osym.put_st_shndx(st_shndx);
1418
1419 ov += sym_size;
1420 }
1421
1422 // Write the symbol to the output dynamic symbol table.
d491d34e 1423 if (lv.needs_output_dynsym_entry())
7bf1f802
ILT
1424 {
1425 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1426 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1427
1428 gold_assert(isym.get_st_name() < strtab_size);
1429 const char* name = pnames + isym.get_st_name();
1430 osym.put_st_name(dynpool->get_offset(name));
1431 osym.put_st_value(this->local_values_[i].value(this, 0));
1432 osym.put_st_size(isym.get_st_size());
1433 osym.put_st_info(isym.get_st_info());
1434 osym.put_st_other(isym.get_st_other());
1435 osym.put_st_shndx(st_shndx);
1436
1437 dyn_ov += sym_size;
1438 }
1439 }
f6ce93d6 1440
61ba1cf9 1441
7bf1f802
ILT
1442 if (output_size > 0)
1443 {
1444 gold_assert(ov - oview == output_size);
1445 of->write_output_view(this->local_symbol_offset_, output_size, oview);
61ba1cf9
ILT
1446 }
1447
7bf1f802
ILT
1448 if (dyn_output_size > 0)
1449 {
1450 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1451 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1452 dyn_oview);
1453 }
61ba1cf9
ILT
1454}
1455
f7e2ee48
ILT
1456// Set *INFO to symbolic information about the offset OFFSET in the
1457// section SHNDX. Return true if we found something, false if we
1458// found nothing.
1459
1460template<int size, bool big_endian>
1461bool
1462Sized_relobj<size, big_endian>::get_symbol_location_info(
1463 unsigned int shndx,
1464 off_t offset,
1465 Symbol_location_info* info)
1466{
1467 if (this->symtab_shndx_ == 0)
1468 return false;
1469
8383303e 1470 section_size_type symbols_size;
f7e2ee48
ILT
1471 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1472 &symbols_size,
1473 false);
1474
d491d34e
ILT
1475 unsigned int symbol_names_shndx =
1476 this->adjust_shndx(this->section_link(this->symtab_shndx_));
8383303e 1477 section_size_type names_size;
f7e2ee48
ILT
1478 const unsigned char* symbol_names_u =
1479 this->section_contents(symbol_names_shndx, &names_size, false);
1480 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1481
1482 const int sym_size = This::sym_size;
1483 const size_t count = symbols_size / sym_size;
1484
1485 const unsigned char* p = symbols;
1486 for (size_t i = 0; i < count; ++i, p += sym_size)
1487 {
1488 elfcpp::Sym<size, big_endian> sym(p);
1489
1490 if (sym.get_st_type() == elfcpp::STT_FILE)
1491 {
1492 if (sym.get_st_name() >= names_size)
1493 info->source_file = "(invalid)";
1494 else
1495 info->source_file = symbol_names + sym.get_st_name();
d491d34e 1496 continue;
f7e2ee48 1497 }
d491d34e
ILT
1498
1499 bool is_ordinary;
1500 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1501 &is_ordinary);
1502 if (is_ordinary
1503 && st_shndx == shndx
1504 && static_cast<off_t>(sym.get_st_value()) <= offset
1505 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1506 > offset))
f7e2ee48
ILT
1507 {
1508 if (sym.get_st_name() > names_size)
1509 info->enclosing_symbol_name = "(invalid)";
1510 else
a2b1aa12
ILT
1511 {
1512 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
086a1841 1513 if (parameters->options().do_demangle())
a2b1aa12
ILT
1514 {
1515 char* demangled_name = cplus_demangle(
1516 info->enclosing_symbol_name.c_str(),
1517 DMGL_ANSI | DMGL_PARAMS);
1518 if (demangled_name != NULL)
1519 {
1520 info->enclosing_symbol_name.assign(demangled_name);
1521 free(demangled_name);
1522 }
1523 }
1524 }
f7e2ee48
ILT
1525 return true;
1526 }
1527 }
1528
1529 return false;
1530}
1531
e94cf127
CC
1532// Look for a kept section corresponding to the given discarded section,
1533// and return its output address. This is used only for relocations in
1534// debugging sections. If we can't find the kept section, return 0.
1535
1536template<int size, bool big_endian>
1537typename Sized_relobj<size, big_endian>::Address
1538Sized_relobj<size, big_endian>::map_to_kept_section(
1539 unsigned int shndx,
1540 bool* found) const
1541{
1542 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1543 if (kept != NULL)
1544 {
1545 gold_assert(kept->object_ != NULL);
1546 *found = true;
1547 return (static_cast<Address>
1548 (kept->object_->output_section_address(kept->shndx_)));
1549 }
1550 *found = false;
1551 return 0;
1552}
1553
54dc6425
ILT
1554// Input_objects methods.
1555
008db82e
ILT
1556// Add a regular relocatable object to the list. Return false if this
1557// object should be ignored.
f6ce93d6 1558
008db82e 1559bool
54dc6425
ILT
1560Input_objects::add_object(Object* obj)
1561{
fbfba508 1562 // Set the global target from the first object file we recognize.
019cdb1a 1563 Target* target = obj->target();
8851ecca 1564 if (!parameters->target_valid())
fbfba508 1565 set_parameters_target(target);
8851ecca 1566 else if (target != &parameters->target())
019cdb1a 1567 {
fbfba508 1568 obj->error(_("incompatible target"));
019cdb1a
ILT
1569 return false;
1570 }
1571
c5818ff1
CC
1572 // Print the filename if the -t/--trace option is selected.
1573 if (parameters->options().trace())
1574 gold_info("%s", obj->name().c_str());
1575
008db82e 1576 if (!obj->is_dynamic())
f6ce93d6 1577 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
008db82e
ILT
1578 else
1579 {
1580 // See if this is a duplicate SONAME.
1581 Dynobj* dynobj = static_cast<Dynobj*>(obj);
9a2d6984 1582 const char* soname = dynobj->soname();
008db82e
ILT
1583
1584 std::pair<Unordered_set<std::string>::iterator, bool> ins =
9a2d6984 1585 this->sonames_.insert(soname);
008db82e
ILT
1586 if (!ins.second)
1587 {
1588 // We have already seen a dynamic object with this soname.
1589 return false;
1590 }
1591
1592 this->dynobj_list_.push_back(dynobj);
9a2d6984
ILT
1593
1594 // If this is -lc, remember the directory in which we found it.
1595 // We use this when issuing warnings about undefined symbols: as
1596 // a heuristic, we don't warn about system libraries found in
1597 // the same directory as -lc.
1598 if (strncmp(soname, "libc.so", 7) == 0)
1599 {
1600 const char* object_name = dynobj->name().c_str();
1601 const char* base = lbasename(object_name);
1602 if (base != object_name)
1603 this->system_library_directory_.assign(object_name,
1604 base - 1 - object_name);
1605 }
008db82e 1606 }
75f65a3e 1607
008db82e 1608 return true;
54dc6425
ILT
1609}
1610
9a2d6984
ILT
1611// Return whether an object was found in the system library directory.
1612
1613bool
1614Input_objects::found_in_system_library_directory(const Object* object) const
1615{
1616 return (!this->system_library_directory_.empty()
1617 && object->name().compare(0,
1618 this->system_library_directory_.size(),
1619 this->system_library_directory_) == 0);
1620}
1621
e2827e5f
ILT
1622// For each dynamic object, record whether we've seen all of its
1623// explicit dependencies.
1624
1625void
1626Input_objects::check_dynamic_dependencies() const
1627{
1628 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1629 p != this->dynobj_list_.end();
1630 ++p)
1631 {
1632 const Dynobj::Needed& needed((*p)->needed());
1633 bool found_all = true;
1634 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1635 pneeded != needed.end();
1636 ++pneeded)
1637 {
1638 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1639 {
1640 found_all = false;
1641 break;
1642 }
1643 }
1644 (*p)->set_has_unknown_needed_entries(!found_all);
1645 }
1646}
1647
92e059d8
ILT
1648// Relocate_info methods.
1649
1650// Return a string describing the location of a relocation. This is
1651// only used in error messages.
1652
1653template<int size, bool big_endian>
1654std::string
f7e2ee48 1655Relocate_info<size, big_endian>::location(size_t, off_t offset) const
92e059d8 1656{
5c2c6c95
ILT
1657 // See if we can get line-number information from debugging sections.
1658 std::string filename;
1659 std::string file_and_lineno; // Better than filename-only, if available.
4c50553d 1660
a55ce7fe 1661 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
24badc65
ILT
1662 // This will be "" if we failed to parse the debug info for any reason.
1663 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
4c50553d 1664
92e059d8 1665 std::string ret(this->object->name());
f7e2ee48
ILT
1666 ret += ':';
1667 Symbol_location_info info;
1668 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1669 {
1670 ret += " in function ";
1671 ret += info.enclosing_symbol_name;
1672 ret += ":";
5c2c6c95
ILT
1673 filename = info.source_file;
1674 }
1675
1676 if (!file_and_lineno.empty())
1677 ret += file_and_lineno;
1678 else
1679 {
1680 if (!filename.empty())
1681 ret += filename;
1682 ret += "(";
1683 ret += this->object->section_name(this->data_shndx);
1684 char buf[100];
1685 // Offsets into sections have to be positive.
1686 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1687 ret += buf;
1688 ret += ")";
f7e2ee48 1689 }
92e059d8
ILT
1690 return ret;
1691}
1692
bae7f79e
ILT
1693} // End namespace gold.
1694
1695namespace
1696{
1697
1698using namespace gold;
1699
1700// Read an ELF file with the header and return the appropriate
1701// instance of Object.
1702
1703template<int size, bool big_endian>
1704Object*
1705make_elf_sized_object(const std::string& name, Input_file* input_file,
1706 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1707{
1708 int et = ehdr.get_e_type();
bae7f79e
ILT
1709 if (et == elfcpp::ET_REL)
1710 {
f6ce93d6
ILT
1711 Sized_relobj<size, big_endian>* obj =
1712 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
1713 obj->setup(ehdr);
1714 return obj;
1715 }
dbe717ef
ILT
1716 else if (et == elfcpp::ET_DYN)
1717 {
1718 Sized_dynobj<size, big_endian>* obj =
1719 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1720 obj->setup(ehdr);
1721 return obj;
1722 }
bae7f79e
ILT
1723 else
1724 {
75f2446e
ILT
1725 gold_error(_("%s: unsupported ELF file type %d"),
1726 name.c_str(), et);
1727 return NULL;
bae7f79e
ILT
1728 }
1729}
1730
1731} // End anonymous namespace.
1732
1733namespace gold
1734{
1735
1736// Read an ELF file and return the appropriate instance of Object.
1737
1738Object*
1739make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
8383303e 1740 const unsigned char* p, section_offset_type bytes)
bae7f79e
ILT
1741{
1742 if (bytes < elfcpp::EI_NIDENT)
1743 {
75f2446e
ILT
1744 gold_error(_("%s: ELF file too short"), name.c_str());
1745 return NULL;
bae7f79e
ILT
1746 }
1747
1748 int v = p[elfcpp::EI_VERSION];
1749 if (v != elfcpp::EV_CURRENT)
1750 {
1751 if (v == elfcpp::EV_NONE)
75f2446e 1752 gold_error(_("%s: invalid ELF version 0"), name.c_str());
bae7f79e 1753 else
75f2446e
ILT
1754 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1755 return NULL;
bae7f79e
ILT
1756 }
1757
1758 int c = p[elfcpp::EI_CLASS];
1759 if (c == elfcpp::ELFCLASSNONE)
1760 {
75f2446e
ILT
1761 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1762 return NULL;
bae7f79e
ILT
1763 }
1764 else if (c != elfcpp::ELFCLASS32
1765 && c != elfcpp::ELFCLASS64)
1766 {
75f2446e
ILT
1767 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1768 return NULL;
bae7f79e
ILT
1769 }
1770
1771 int d = p[elfcpp::EI_DATA];
1772 if (d == elfcpp::ELFDATANONE)
1773 {
75f2446e
ILT
1774 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1775 return NULL;
bae7f79e
ILT
1776 }
1777 else if (d != elfcpp::ELFDATA2LSB
1778 && d != elfcpp::ELFDATA2MSB)
1779 {
75f2446e
ILT
1780 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1781 return NULL;
bae7f79e
ILT
1782 }
1783
1784 bool big_endian = d == elfcpp::ELFDATA2MSB;
1785
1786 if (c == elfcpp::ELFCLASS32)
1787 {
1788 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1789 {
75f2446e
ILT
1790 gold_error(_("%s: ELF file too short"), name.c_str());
1791 return NULL;
bae7f79e
ILT
1792 }
1793 if (big_endian)
1794 {
193a53d9 1795#ifdef HAVE_TARGET_32_BIG
bae7f79e
ILT
1796 elfcpp::Ehdr<32, true> ehdr(p);
1797 return make_elf_sized_object<32, true>(name, input_file,
1798 offset, ehdr);
193a53d9 1799#else
75f2446e
ILT
1800 gold_error(_("%s: not configured to support "
1801 "32-bit big-endian object"),
1802 name.c_str());
1803 return NULL;
193a53d9 1804#endif
bae7f79e
ILT
1805 }
1806 else
1807 {
193a53d9 1808#ifdef HAVE_TARGET_32_LITTLE
bae7f79e
ILT
1809 elfcpp::Ehdr<32, false> ehdr(p);
1810 return make_elf_sized_object<32, false>(name, input_file,
1811 offset, ehdr);
193a53d9 1812#else
75f2446e
ILT
1813 gold_error(_("%s: not configured to support "
1814 "32-bit little-endian object"),
1815 name.c_str());
1816 return NULL;
193a53d9 1817#endif
bae7f79e
ILT
1818 }
1819 }
1820 else
1821 {
c165fb93 1822 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
bae7f79e 1823 {
75f2446e
ILT
1824 gold_error(_("%s: ELF file too short"), name.c_str());
1825 return NULL;
bae7f79e
ILT
1826 }
1827 if (big_endian)
1828 {
193a53d9 1829#ifdef HAVE_TARGET_64_BIG
bae7f79e
ILT
1830 elfcpp::Ehdr<64, true> ehdr(p);
1831 return make_elf_sized_object<64, true>(name, input_file,
1832 offset, ehdr);
193a53d9 1833#else
75f2446e
ILT
1834 gold_error(_("%s: not configured to support "
1835 "64-bit big-endian object"),
1836 name.c_str());
1837 return NULL;
193a53d9 1838#endif
bae7f79e
ILT
1839 }
1840 else
1841 {
193a53d9 1842#ifdef HAVE_TARGET_64_LITTLE
bae7f79e
ILT
1843 elfcpp::Ehdr<64, false> ehdr(p);
1844 return make_elf_sized_object<64, false>(name, input_file,
1845 offset, ehdr);
193a53d9 1846#else
75f2446e
ILT
1847 gold_error(_("%s: not configured to support "
1848 "64-bit little-endian object"),
1849 name.c_str());
1850 return NULL;
193a53d9 1851#endif
bae7f79e
ILT
1852 }
1853 }
1854}
1855
04bf7072
ILT
1856// Instantiate the templates we need.
1857
1858#ifdef HAVE_TARGET_32_LITTLE
1859template
1860void
1861Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1862 Read_symbols_data*);
1863#endif
1864
1865#ifdef HAVE_TARGET_32_BIG
1866template
1867void
1868Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1869 Read_symbols_data*);
1870#endif
1871
1872#ifdef HAVE_TARGET_64_LITTLE
1873template
1874void
1875Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1876 Read_symbols_data*);
1877#endif
1878
1879#ifdef HAVE_TARGET_64_BIG
1880template
1881void
1882Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1883 Read_symbols_data*);
1884#endif
bae7f79e 1885
193a53d9 1886#ifdef HAVE_TARGET_32_LITTLE
bae7f79e 1887template
f6ce93d6 1888class Sized_relobj<32, false>;
193a53d9 1889#endif
bae7f79e 1890
193a53d9 1891#ifdef HAVE_TARGET_32_BIG
bae7f79e 1892template
f6ce93d6 1893class Sized_relobj<32, true>;
193a53d9 1894#endif
bae7f79e 1895
193a53d9 1896#ifdef HAVE_TARGET_64_LITTLE
bae7f79e 1897template
f6ce93d6 1898class Sized_relobj<64, false>;
193a53d9 1899#endif
bae7f79e 1900
193a53d9 1901#ifdef HAVE_TARGET_64_BIG
bae7f79e 1902template
f6ce93d6 1903class Sized_relobj<64, true>;
193a53d9 1904#endif
bae7f79e 1905
193a53d9 1906#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1907template
1908struct Relocate_info<32, false>;
193a53d9 1909#endif
92e059d8 1910
193a53d9 1911#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1912template
1913struct Relocate_info<32, true>;
193a53d9 1914#endif
92e059d8 1915
193a53d9 1916#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1917template
1918struct Relocate_info<64, false>;
193a53d9 1919#endif
92e059d8 1920
193a53d9 1921#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1922template
1923struct Relocate_info<64, true>;
193a53d9 1924#endif
92e059d8 1925
bae7f79e 1926} // End namespace gold.
This page took 0.212505 seconds and 4 git commands to generate.