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