*** empty log message ***
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
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>
bae7f79e 28
14bfc3f5 29#include "target-select.h"
5c2c6c95 30#include "dwarf_reader.h"
a2fb1b05 31#include "layout.h"
61ba1cf9 32#include "output.h"
f6ce93d6 33#include "symtab.h"
4c50553d 34#include "reloc.h"
f6ce93d6
ILT
35#include "object.h"
36#include "dynobj.h"
bae7f79e
ILT
37
38namespace gold
39{
40
645f8123
ILT
41// Class Object.
42
dbe717ef
ILT
43// Set the target based on fields in the ELF file header.
44
45void
46Object::set_target(int machine, int size, bool big_endian, int osabi,
47 int abiversion)
48{
49 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
50 if (target == NULL)
75f2446e
ILT
51 gold_fatal(_("%s: unsupported ELF machine number %d"),
52 this->name().c_str(), machine);
dbe717ef
ILT
53 this->target_ = target;
54}
55
75f2446e
ILT
56// Report an error for this object file. This is used by the
57// elfcpp::Elf_file interface, and also called by the Object code
58// itself.
645f8123
ILT
59
60void
75f2446e 61Object::error(const char* format, ...) const
645f8123
ILT
62{
63 va_list args;
645f8123 64 va_start(args, format);
75f2446e
ILT
65 char* buf = NULL;
66 if (vasprintf(&buf, format, args) < 0)
67 gold_nomem();
645f8123 68 va_end(args);
75f2446e
ILT
69 gold_error(_("%s: %s"), this->name().c_str(), buf);
70 free(buf);
645f8123
ILT
71}
72
73// Return a view of the contents of a section.
74
75const unsigned char*
9eb9fa57 76Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
645f8123
ILT
77{
78 Location loc(this->do_section_contents(shndx));
79 *plen = loc.data_size;
9eb9fa57 80 return this->get_view(loc.file_offset, loc.data_size, cache);
645f8123
ILT
81}
82
dbe717ef
ILT
83// Read the section data into SD. This is code common to Sized_relobj
84// and Sized_dynobj, so we put it into Object.
85
86template<int size, bool big_endian>
87void
88Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
89 Read_symbols_data* sd)
90{
91 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
92
93 // Read the section headers.
94 const off_t shoff = elf_file->shoff();
95 const unsigned int shnum = this->shnum();
9eb9fa57 96 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
dbe717ef
ILT
97
98 // Read the section names.
99 const unsigned char* pshdrs = sd->section_headers->data();
100 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
101 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
102
103 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
75f2446e
ILT
104 this->error(_("section name section has wrong type: %u"),
105 static_cast<unsigned int>(shdrnames.get_sh_type()));
dbe717ef
ILT
106
107 sd->section_names_size = shdrnames.get_sh_size();
108 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
9eb9fa57 109 sd->section_names_size, false);
dbe717ef
ILT
110}
111
112// If NAME is the name of a special .gnu.warning section, arrange for
113// the warning to be issued. SHNDX is the section index. Return
114// whether it is a warning section.
115
116bool
117Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
118 Symbol_table* symtab)
119{
120 const char warn_prefix[] = ".gnu.warning.";
121 const int warn_prefix_len = sizeof warn_prefix - 1;
122 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
123 {
124 symtab->add_warning(name + warn_prefix_len, this, shndx);
125 return true;
126 }
127 return false;
128}
129
f6ce93d6 130// Class Sized_relobj.
bae7f79e
ILT
131
132template<int size, bool big_endian>
f6ce93d6 133Sized_relobj<size, big_endian>::Sized_relobj(
bae7f79e
ILT
134 const std::string& name,
135 Input_file* input_file,
136 off_t offset,
137 const elfcpp::Ehdr<size, big_endian>& ehdr)
f6ce93d6 138 : Relobj(name, input_file, offset),
645f8123 139 elf_file_(this, ehdr),
dbe717ef 140 symtab_shndx_(-1U),
61ba1cf9
ILT
141 local_symbol_count_(0),
142 output_local_symbol_count_(0),
730cdc88 143 symbols_(),
61ba1cf9 144 local_symbol_offset_(0),
e727fa71 145 local_values_(),
730cdc88
ILT
146 local_got_offsets_(),
147 has_eh_frame_(false)
bae7f79e 148{
bae7f79e
ILT
149}
150
151template<int size, bool big_endian>
f6ce93d6 152Sized_relobj<size, big_endian>::~Sized_relobj()
bae7f79e
ILT
153{
154}
155
645f8123 156// Set up an object file based on the file header. This sets up the
bae7f79e
ILT
157// target and reads the section information.
158
159template<int size, bool big_endian>
160void
f6ce93d6 161Sized_relobj<size, big_endian>::setup(
bae7f79e
ILT
162 const elfcpp::Ehdr<size, big_endian>& ehdr)
163{
dbe717ef
ILT
164 this->set_target(ehdr.get_e_machine(), size, big_endian,
165 ehdr.get_e_ident()[elfcpp::EI_OSABI],
166 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
12e14209 167
dbe717ef 168 const unsigned int shnum = this->elf_file_.shnum();
a2fb1b05 169 this->set_shnum(shnum);
dbe717ef 170}
12e14209 171
dbe717ef
ILT
172// Find the SHT_SYMTAB section, given the section headers. The ELF
173// standard says that maybe in the future there can be more than one
174// SHT_SYMTAB section. Until somebody figures out how that could
175// work, we assume there is only one.
12e14209 176
dbe717ef
ILT
177template<int size, bool big_endian>
178void
179Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
180{
181 const unsigned int shnum = this->shnum();
182 this->symtab_shndx_ = 0;
183 if (shnum > 0)
bae7f79e 184 {
dbe717ef
ILT
185 // Look through the sections in reverse order, since gas tends
186 // to put the symbol table at the end.
187 const unsigned char* p = pshdrs + shnum * This::shdr_size;
188 unsigned int i = shnum;
189 while (i > 0)
bae7f79e 190 {
dbe717ef
ILT
191 --i;
192 p -= This::shdr_size;
193 typename This::Shdr shdr(p);
194 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
195 {
196 this->symtab_shndx_ = i;
197 break;
198 }
bae7f79e 199 }
bae7f79e
ILT
200 }
201}
202
730cdc88
ILT
203// Return whether SHDR has the right type and flags to be a GNU
204// .eh_frame section.
205
206template<int size, bool big_endian>
207bool
208Sized_relobj<size, big_endian>::check_eh_frame_flags(
209 const elfcpp::Shdr<size, big_endian>* shdr) const
210{
211 return (shdr->get_sh_size() > 0
212 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
213 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
214}
215
216// Return whether there is a GNU .eh_frame section, given the section
217// headers and the section names.
218
219template<int size, bool big_endian>
220bool
221Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
222 const char* names,
223 off_t names_size) const
224{
225 const unsigned int shnum = this->shnum();
226 const unsigned char* p = pshdrs + This::shdr_size;
227 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
228 {
229 typename This::Shdr shdr(p);
230 if (this->check_eh_frame_flags(&shdr))
231 {
232 if (shdr.get_sh_name() >= names_size)
233 {
234 this->error(_("bad section name offset for section %u: %lu"),
235 i, static_cast<unsigned long>(shdr.get_sh_name()));
236 continue;
237 }
238
239 const char* name = names + shdr.get_sh_name();
240 if (strcmp(name, ".eh_frame") == 0)
241 return true;
242 }
243 }
244 return false;
245}
246
12e14209 247// Read the sections and symbols from an object file.
bae7f79e
ILT
248
249template<int size, bool big_endian>
12e14209 250void
f6ce93d6 251Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
bae7f79e 252{
dbe717ef 253 this->read_section_data(&this->elf_file_, sd);
12e14209 254
dbe717ef
ILT
255 const unsigned char* const pshdrs = sd->section_headers->data();
256
257 this->find_symtab(pshdrs);
12e14209 258
730cdc88
ILT
259 const unsigned char* namesu = sd->section_names->data();
260 const char* names = reinterpret_cast<const char*>(namesu);
261 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
262 this->has_eh_frame_ = true;
263
75f2446e
ILT
264 sd->symbols = NULL;
265 sd->symbols_size = 0;
730cdc88 266 sd->external_symbols_offset = 0;
75f2446e
ILT
267 sd->symbol_names = NULL;
268 sd->symbol_names_size = 0;
269
645f8123 270 if (this->symtab_shndx_ == 0)
bae7f79e
ILT
271 {
272 // No symbol table. Weird but legal.
12e14209 273 return;
bae7f79e
ILT
274 }
275
12e14209
ILT
276 // Get the symbol table section header.
277 typename This::Shdr symtabshdr(pshdrs
645f8123 278 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 279 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
bae7f79e 280
730cdc88
ILT
281 // If this object has a .eh_frame section, we need all the symbols.
282 // Otherwise we only need the external symbols. While it would be
283 // simpler to just always read all the symbols, I've seen object
284 // files with well over 2000 local symbols, which for a 64-bit
285 // object file format is over 5 pages that we don't need to read
286 // now.
287
75f65a3e 288 const int sym_size = This::sym_size;
92e059d8
ILT
289 const unsigned int loccount = symtabshdr.get_sh_info();
290 this->local_symbol_count_ = loccount;
291 off_t locsize = loccount * sym_size;
730cdc88
ILT
292 off_t dataoff = symtabshdr.get_sh_offset();
293 off_t datasize = symtabshdr.get_sh_size();
294 off_t extoff = dataoff + locsize;
295 off_t extsize = datasize - locsize;
75f65a3e 296
730cdc88
ILT
297 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
298 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
299
300 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
bae7f79e
ILT
301
302 // Read the section header for the symbol names.
dbe717ef
ILT
303 unsigned int strtab_shndx = symtabshdr.get_sh_link();
304 if (strtab_shndx >= this->shnum())
bae7f79e 305 {
75f2446e
ILT
306 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
307 return;
bae7f79e 308 }
dbe717ef 309 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
bae7f79e
ILT
310 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
311 {
75f2446e
ILT
312 this->error(_("symbol table name section has wrong type: %u"),
313 static_cast<unsigned int>(strtabshdr.get_sh_type()));
314 return;
bae7f79e
ILT
315 }
316
317 // Read the symbol names.
318 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
9eb9fa57 319 strtabshdr.get_sh_size(), true);
bae7f79e 320
12e14209 321 sd->symbols = fvsymtab;
730cdc88
ILT
322 sd->symbols_size = readsize;
323 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
12e14209
ILT
324 sd->symbol_names = fvstrtab;
325 sd->symbol_names_size = strtabshdr.get_sh_size();
a2fb1b05
ILT
326}
327
730cdc88
ILT
328// Return the section index of symbol SYM. Set *VALUE to its value in
329// the object file. Note that for a symbol which is not defined in
330// this object file, this will set *VALUE to 0 and return SHN_UNDEF;
331// it will not return the final value of the symbol in the link.
332
333template<int size, bool big_endian>
334unsigned int
335Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
336 Address* value)
337{
338 off_t symbols_size;
339 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
340 &symbols_size,
341 false);
342
343 const size_t count = symbols_size / This::sym_size;
344 gold_assert(sym < count);
345
346 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
347 *value = elfsym.get_st_value();
348 // FIXME: Handle SHN_XINDEX.
349 return elfsym.get_st_shndx();
350}
351
a2fb1b05
ILT
352// Return whether to include a section group in the link. LAYOUT is
353// used to keep track of which section groups we have already seen.
354// INDEX is the index of the section group and SHDR is the section
355// header. If we do not want to include this group, we set bits in
356// OMIT for each section which should be discarded.
357
358template<int size, bool big_endian>
359bool
f6ce93d6 360Sized_relobj<size, big_endian>::include_section_group(
a2fb1b05
ILT
361 Layout* layout,
362 unsigned int index,
363 const elfcpp::Shdr<size, big_endian>& shdr,
364 std::vector<bool>* omit)
365{
366 // Read the section contents.
367 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
9eb9fa57 368 shdr.get_sh_size(), false);
a2fb1b05
ILT
369 const elfcpp::Elf_Word* pword =
370 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
371
372 // The first word contains flags. We only care about COMDAT section
373 // groups. Other section groups are always included in the link
374 // just like ordinary sections.
f6ce93d6 375 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
a2fb1b05
ILT
376 if ((flags & elfcpp::GRP_COMDAT) == 0)
377 return true;
378
379 // Look up the group signature, which is the name of a symbol. This
380 // is a lot of effort to go to to read a string. Why didn't they
381 // just use the name of the SHT_GROUP section as the group
382 // signature?
383
384 // Get the appropriate symbol table header (this will normally be
385 // the single SHT_SYMTAB section, but in principle it need not be).
645f8123
ILT
386 const unsigned int link = shdr.get_sh_link();
387 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
a2fb1b05
ILT
388
389 // Read the symbol table entry.
390 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
391 {
75f2446e
ILT
392 this->error(_("section group %u info %u out of range"),
393 index, shdr.get_sh_info());
394 return false;
a2fb1b05
ILT
395 }
396 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
9eb9fa57 397 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
a2fb1b05
ILT
398 elfcpp::Sym<size, big_endian> sym(psym);
399
a2fb1b05 400 // Read the symbol table names.
645f8123
ILT
401 off_t symnamelen;
402 const unsigned char* psymnamesu;
9eb9fa57
ILT
403 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
404 true);
a2fb1b05
ILT
405 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
406
407 // Get the section group signature.
645f8123 408 if (sym.get_st_name() >= symnamelen)
a2fb1b05 409 {
75f2446e
ILT
410 this->error(_("symbol %u name offset %u out of range"),
411 shdr.get_sh_info(), sym.get_st_name());
412 return false;
a2fb1b05
ILT
413 }
414
415 const char* signature = psymnames + sym.get_st_name();
416
ead1e424
ILT
417 // It seems that some versions of gas will create a section group
418 // associated with a section symbol, and then fail to give a name to
419 // the section symbol. In such a case, use the name of the section.
420 // FIXME.
645f8123
ILT
421 std::string secname;
422 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
ead1e424 423 {
645f8123
ILT
424 secname = this->section_name(sym.get_st_shndx());
425 signature = secname.c_str();
ead1e424
ILT
426 }
427
a2fb1b05
ILT
428 // Record this section group, and see whether we've already seen one
429 // with the same signature.
430 if (layout->add_comdat(signature, true))
431 return true;
432
433 // This is a duplicate. We want to discard the sections in this
434 // group.
435 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
436 for (size_t i = 1; i < count; ++i)
437 {
f6ce93d6
ILT
438 elfcpp::Elf_Word secnum =
439 elfcpp::Swap<32, big_endian>::readval(pword + i);
a2fb1b05
ILT
440 if (secnum >= this->shnum())
441 {
75f2446e
ILT
442 this->error(_("section %u in section group %u out of range"),
443 secnum, index);
444 continue;
a2fb1b05
ILT
445 }
446 (*omit)[secnum] = true;
447 }
448
449 return false;
450}
451
452// Whether to include a linkonce section in the link. NAME is the
453// name of the section and SHDR is the section header.
454
455// Linkonce sections are a GNU extension implemented in the original
456// GNU linker before section groups were defined. The semantics are
457// that we only include one linkonce section with a given name. The
458// name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
459// where T is the type of section and SYMNAME is the name of a symbol.
460// In an attempt to make linkonce sections interact well with section
461// groups, we try to identify SYMNAME and use it like a section group
462// signature. We want to block section groups with that signature,
463// but not other linkonce sections with that signature. We also use
464// the full name of the linkonce section as a normal section group
465// signature.
466
467template<int size, bool big_endian>
468bool
f6ce93d6 469Sized_relobj<size, big_endian>::include_linkonce_section(
a2fb1b05
ILT
470 Layout* layout,
471 const char* name,
472 const elfcpp::Shdr<size, big_endian>&)
473{
ad435a24
ILT
474 // In general the symbol name we want will be the string following
475 // the last '.'. However, we have to handle the case of
476 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
477 // some versions of gcc. So we use a heuristic: if the name starts
478 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
479 // we look for the last '.'. We can't always simply skip
480 // ".gnu.linkonce.X", because we have to deal with cases like
481 // ".gnu.linkonce.d.rel.ro.local".
482 const char* const linkonce_t = ".gnu.linkonce.t.";
483 const char* symname;
484 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
485 symname = name + strlen(linkonce_t);
486 else
487 symname = strrchr(name, '.') + 1;
a783673b
ILT
488 bool include1 = layout->add_comdat(symname, false);
489 bool include2 = layout->add_comdat(name, true);
490 return include1 && include2;
a2fb1b05
ILT
491}
492
493// Lay out the input sections. We walk through the sections and check
494// whether they should be included in the link. If they should, we
495// pass them to the Layout object, which will return an output section
496// and an offset.
497
498template<int size, bool big_endian>
499void
7e1edb90 500Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
f6ce93d6 501 Layout* layout,
12e14209 502 Read_symbols_data* sd)
a2fb1b05 503{
dbe717ef 504 const unsigned int shnum = this->shnum();
12e14209
ILT
505 if (shnum == 0)
506 return;
a2fb1b05
ILT
507
508 // Get the section headers.
12e14209 509 const unsigned char* pshdrs = sd->section_headers->data();
a2fb1b05
ILT
510
511 // Get the section names.
12e14209 512 const unsigned char* pnamesu = sd->section_names->data();
a2fb1b05
ILT
513 const char* pnames = reinterpret_cast<const char*>(pnamesu);
514
730cdc88
ILT
515 // For each section, record the index of the reloc section if any.
516 // Use 0 to mean that there is no reloc section, -1U to mean that
517 // there is more than one.
518 std::vector<unsigned int> reloc_shndx(shnum, 0);
519 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
520 // Skip the first, dummy, section.
521 pshdrs += This::shdr_size;
522 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
523 {
524 typename This::Shdr shdr(pshdrs);
525
526 unsigned int sh_type = shdr.get_sh_type();
527 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
528 {
529 unsigned int target_shndx = shdr.get_sh_info();
530 if (target_shndx == 0 || target_shndx >= shnum)
531 {
532 this->error(_("relocation section %u has bad info %u"),
533 i, target_shndx);
534 continue;
535 }
536
537 if (reloc_shndx[target_shndx] != 0)
538 reloc_shndx[target_shndx] = -1U;
539 else
540 {
541 reloc_shndx[target_shndx] = i;
542 reloc_type[target_shndx] = sh_type;
543 }
544 }
545 }
546
a2fb1b05 547 std::vector<Map_to_output>& map_sections(this->map_to_output());
61ba1cf9 548 map_sections.resize(shnum);
a2fb1b05 549
35cdfc9a
ILT
550 // Whether we've seen a .note.GNU-stack section.
551 bool seen_gnu_stack = false;
552 // The flags of a .note.GNU-stack section.
553 uint64_t gnu_stack_flags = 0;
554
a2fb1b05
ILT
555 // Keep track of which sections to omit.
556 std::vector<bool> omit(shnum, false);
557
730cdc88
ILT
558 // Keep track of .eh_frame sections.
559 std::vector<unsigned int> eh_frame_sections;
560
f6ce93d6 561 // Skip the first, dummy, section.
730cdc88 562 pshdrs = sd->section_headers->data() + This::shdr_size;
f6ce93d6 563 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
a2fb1b05 564 {
75f65a3e 565 typename This::Shdr shdr(pshdrs);
a2fb1b05 566
12e14209 567 if (shdr.get_sh_name() >= sd->section_names_size)
a2fb1b05 568 {
75f2446e
ILT
569 this->error(_("bad section name offset for section %u: %lu"),
570 i, static_cast<unsigned long>(shdr.get_sh_name()));
571 return;
a2fb1b05
ILT
572 }
573
574 const char* name = pnames + shdr.get_sh_name();
575
dbe717ef 576 if (this->handle_gnu_warning_section(name, i, symtab))
f6ce93d6 577 {
7e1edb90 578 if (!parameters->output_is_object())
f6ce93d6
ILT
579 omit[i] = true;
580 }
581
35cdfc9a
ILT
582 // The .note.GNU-stack section is special. It gives the
583 // protection flags that this object file requires for the stack
584 // in memory.
585 if (strcmp(name, ".note.GNU-stack") == 0)
586 {
587 seen_gnu_stack = true;
588 gnu_stack_flags |= shdr.get_sh_flags();
589 omit[i] = true;
590 }
591
a2fb1b05
ILT
592 bool discard = omit[i];
593 if (!discard)
594 {
595 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
596 {
597 if (!this->include_section_group(layout, i, shdr, &omit))
598 discard = true;
599 }
cba134d6
ILT
600 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
601 && Layout::is_linkonce(name))
a2fb1b05
ILT
602 {
603 if (!this->include_linkonce_section(layout, name, shdr))
604 discard = true;
605 }
606 }
607
608 if (discard)
609 {
610 // Do not include this section in the link.
611 map_sections[i].output_section = NULL;
612 continue;
613 }
614
730cdc88
ILT
615 // The .eh_frame section is special. It holds exception frame
616 // information that we need to read in order to generate the
617 // exception frame header. We process these after all the other
618 // sections so that the exception frame reader can reliably
619 // determine which sections are being discarded, and discard the
620 // corresponding information.
621 if (!parameters->output_is_object()
622 && strcmp(name, ".eh_frame") == 0
623 && this->check_eh_frame_flags(&shdr))
624 {
625 eh_frame_sections.push_back(i);
626 continue;
627 }
628
a2fb1b05 629 off_t offset;
730cdc88
ILT
630 Output_section* os = layout->layout(this, i, name, shdr,
631 reloc_shndx[i], reloc_type[i],
632 &offset);
a2fb1b05
ILT
633
634 map_sections[i].output_section = os;
635 map_sections[i].offset = offset;
730cdc88
ILT
636
637 // If this section requires special handling, and if there are
638 // relocs that apply to it, then we must do the special handling
639 // before we apply the relocs.
640 if (offset == -1 && reloc_shndx[i] != 0)
641 this->set_relocs_must_follow_section_writes();
12e14209
ILT
642 }
643
35cdfc9a
ILT
644 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
645
730cdc88
ILT
646 // Handle the .eh_frame sections at the end.
647 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
648 p != eh_frame_sections.end();
649 ++p)
650 {
651 gold_assert(this->has_eh_frame_);
652 gold_assert(sd->external_symbols_offset != 0);
653
654 unsigned int i = *p;
655 const unsigned char *pshdr;
656 pshdr = sd->section_headers->data() + i * This::shdr_size;
657 typename This::Shdr shdr(pshdr);
658
659 off_t offset;
660 Output_section* os = layout->layout_eh_frame(this,
661 sd->symbols->data(),
662 sd->symbols_size,
663 sd->symbol_names->data(),
664 sd->symbol_names_size,
665 i, shdr,
666 reloc_shndx[i],
667 reloc_type[i],
668 &offset);
669 map_sections[i].output_section = os;
670 map_sections[i].offset = offset;
671
672 // If this section requires special handling, and if there are
673 // relocs that apply to it, then we must do the special handling
674 // before we apply the relocs.
675 if (offset == -1 && reloc_shndx[i] != 0)
676 this->set_relocs_must_follow_section_writes();
677 }
678
12e14209
ILT
679 delete sd->section_headers;
680 sd->section_headers = NULL;
681 delete sd->section_names;
682 sd->section_names = NULL;
683}
684
685// Add the symbols to the symbol table.
686
687template<int size, bool big_endian>
688void
f6ce93d6 689Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
12e14209
ILT
690 Read_symbols_data* sd)
691{
692 if (sd->symbols == NULL)
693 {
a3ad94ed 694 gold_assert(sd->symbol_names == NULL);
12e14209
ILT
695 return;
696 }
a2fb1b05 697
12e14209 698 const int sym_size = This::sym_size;
730cdc88
ILT
699 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
700 / sym_size);
701 if (static_cast<off_t>(symcount * sym_size)
702 != sd->symbols_size - sd->external_symbols_offset)
12e14209 703 {
75f2446e
ILT
704 this->error(_("size of symbols is not multiple of symbol size"));
705 return;
a2fb1b05 706 }
12e14209 707
730cdc88 708 this->symbols_.resize(symcount);
12e14209 709
12e14209
ILT
710 const char* sym_names =
711 reinterpret_cast<const char*>(sd->symbol_names->data());
730cdc88
ILT
712 symtab->add_from_relobj(this,
713 sd->symbols->data() + sd->external_symbols_offset,
714 symcount, sym_names, sd->symbol_names_size,
715 &this->symbols_);
12e14209
ILT
716
717 delete sd->symbols;
718 sd->symbols = NULL;
719 delete sd->symbol_names;
720 sd->symbol_names = NULL;
bae7f79e
ILT
721}
722
75f65a3e 723// Finalize the local symbols. Here we record the file offset at
61ba1cf9 724// which they should be output, we add their names to *POOL, and we
b8e6aad9
ILT
725// add their values to THIS->LOCAL_VALUES_. Return the symbol index.
726// This function is always called from the main thread. The actual
727// output of the local symbols will occur in a separate task.
75f65a3e
ILT
728
729template<int size, bool big_endian>
c06b7b0b
ILT
730unsigned int
731Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
732 off_t off,
75f65a3e
ILT
733 Stringpool* pool)
734{
a3ad94ed 735 gold_assert(this->symtab_shndx_ != -1U);
645f8123 736 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
737 {
738 // This object has no symbols. Weird but legal.
c06b7b0b 739 return index;
61ba1cf9
ILT
740 }
741
a3ad94ed 742 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
61ba1cf9 743
75f65a3e
ILT
744 this->local_symbol_offset_ = off;
745
746 // Read the symbol table section header.
645f8123
ILT
747 const unsigned int symtab_shndx = this->symtab_shndx_;
748 typename This::Shdr symtabshdr(this,
749 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 750 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
75f65a3e
ILT
751
752 // Read the local symbols.
75f65a3e 753 const int sym_size = This::sym_size;
92e059d8 754 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 755 gold_assert(loccount == symtabshdr.get_sh_info());
75f65a3e
ILT
756 off_t locsize = loccount * sym_size;
757 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
9eb9fa57 758 locsize, true);
75f65a3e 759
c06b7b0b 760 this->local_values_.resize(loccount);
61ba1cf9 761
75f65a3e 762 // Read the symbol names.
645f8123
ILT
763 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
764 off_t strtab_size;
765 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57
ILT
766 &strtab_size,
767 true);
75f65a3e
ILT
768 const char* pnames = reinterpret_cast<const char*>(pnamesu);
769
770 // Loop over the local symbols.
771
c06b7b0b 772 const std::vector<Map_to_output>& mo(this->map_to_output());
75f65a3e 773 unsigned int shnum = this->shnum();
61ba1cf9 774 unsigned int count = 0;
75f65a3e
ILT
775 // Skip the first, dummy, symbol.
776 psyms += sym_size;
61ba1cf9 777 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
75f65a3e
ILT
778 {
779 elfcpp::Sym<size, big_endian> sym(psyms);
780
b8e6aad9
ILT
781 Symbol_value<size>& lv(this->local_values_[i]);
782
75f65a3e 783 unsigned int shndx = sym.get_st_shndx();
b8e6aad9 784 lv.set_input_shndx(shndx);
75f65a3e 785
063f12a8
ILT
786 if (sym.get_st_type() == elfcpp::STT_SECTION)
787 lv.set_is_section_symbol();
788
75f65a3e
ILT
789 if (shndx >= elfcpp::SHN_LORESERVE)
790 {
61ba1cf9 791 if (shndx == elfcpp::SHN_ABS)
b8e6aad9 792 lv.set_output_value(sym.get_st_value());
61ba1cf9 793 else
75f65a3e 794 {
61ba1cf9 795 // FIXME: Handle SHN_XINDEX.
75f2446e
ILT
796 this->error(_("unknown section index %u for local symbol %u"),
797 shndx, i);
798 lv.set_output_value(0);
75f65a3e 799 }
75f65a3e
ILT
800 }
801 else
802 {
803 if (shndx >= shnum)
804 {
75f2446e
ILT
805 this->error(_("local symbol %u section index %u out of range"),
806 i, shndx);
807 shndx = 0;
75f65a3e
ILT
808 }
809
b8e6aad9
ILT
810 Output_section* os = mo[shndx].output_section;
811
812 if (os == NULL)
61ba1cf9 813 {
b8e6aad9
ILT
814 lv.set_output_value(0);
815 lv.set_no_output_symtab_entry();
61ba1cf9
ILT
816 continue;
817 }
818
b8e6aad9
ILT
819 if (mo[shndx].offset == -1)
820 lv.set_input_value(sym.get_st_value());
821 else
822 lv.set_output_value(mo[shndx].output_section->address()
823 + mo[shndx].offset
824 + sym.get_st_value());
75f65a3e
ILT
825 }
826
c06b7b0b
ILT
827 // Decide whether this symbol should go into the output file.
828
829 if (sym.get_st_type() == elfcpp::STT_SECTION)
f6ce93d6 830 {
b8e6aad9 831 lv.set_no_output_symtab_entry();
c06b7b0b
ILT
832 continue;
833 }
645f8123 834
c06b7b0b
ILT
835 if (sym.get_st_name() >= strtab_size)
836 {
75f2446e
ILT
837 this->error(_("local symbol %u section name out of range: %u >= %u"),
838 i, sym.get_st_name(),
839 static_cast<unsigned int>(strtab_size));
840 lv.set_no_output_symtab_entry();
841 continue;
f6ce93d6 842 }
c06b7b0b
ILT
843
844 const char* name = pnames + sym.get_st_name();
cfd73a4e 845 pool->add(name, true, NULL);
b8e6aad9 846 lv.set_output_symtab_index(index);
c06b7b0b 847 ++index;
c06b7b0b 848 ++count;
75f65a3e
ILT
849 }
850
61ba1cf9
ILT
851 this->output_local_symbol_count_ = count;
852
c06b7b0b 853 return index;
75f65a3e
ILT
854}
855
e727fa71
ILT
856// Return the value of the local symbol symndx.
857template<int size, bool big_endian>
858typename elfcpp::Elf_types<size>::Elf_Addr
859Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
860{
861 gold_assert(symndx < this->local_symbol_count_);
862 gold_assert(symndx < this->local_values_.size());
863 const Symbol_value<size>& lv(this->local_values_[symndx]);
864 return lv.value(this, 0);
865}
866
b8e6aad9 867// Return the value of a local symbol defined in input section SHNDX,
063f12a8
ILT
868// with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
869// indicates whether the symbol is a section symbol. This handles
870// SHF_MERGE sections.
b8e6aad9
ILT
871template<int size, bool big_endian>
872typename elfcpp::Elf_types<size>::Elf_Addr
873Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
874 Address value,
063f12a8 875 bool is_section_symbol,
b8e6aad9
ILT
876 Address addend) const
877{
878 const std::vector<Map_to_output>& mo(this->map_to_output());
879 Output_section* os = mo[shndx].output_section;
880 if (os == NULL)
881 return addend;
882 gold_assert(mo[shndx].offset == -1);
063f12a8
ILT
883
884 // Do the mapping required by the output section. If this is not a
885 // section symbol, then we want to map the symbol value, and then
886 // include the addend. If this is a section symbol, then we need to
887 // include the addend to figure out where in the section we are,
888 // before we do the mapping. This will do the right thing provided
889 // the assembler is careful to only convert a relocation in a merged
890 // section to a section symbol if there is a zero addend. If the
891 // assembler does not do this, then in general we can't know what to
892 // do, because we can't distinguish the addend for the instruction
893 // format from the addend for the section offset.
894
895 if (is_section_symbol)
896 return os->output_address(this, shndx, value + addend);
897 else
898 return addend + os->output_address(this, shndx, value);
b8e6aad9
ILT
899}
900
61ba1cf9
ILT
901// Write out the local symbols.
902
903template<int size, bool big_endian>
904void
f6ce93d6 905Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
61ba1cf9
ILT
906 const Stringpool* sympool)
907{
9e2dcb77
ILT
908 if (parameters->strip_all())
909 return;
910
a3ad94ed 911 gold_assert(this->symtab_shndx_ != -1U);
645f8123 912 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
913 {
914 // This object has no symbols. Weird but legal.
915 return;
916 }
917
918 // Read the symbol table section header.
645f8123
ILT
919 const unsigned int symtab_shndx = this->symtab_shndx_;
920 typename This::Shdr symtabshdr(this,
921 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 922 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8 923 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 924 gold_assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
925
926 // Read the local symbols.
927 const int sym_size = This::sym_size;
92e059d8 928 off_t locsize = loccount * sym_size;
61ba1cf9 929 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
9eb9fa57 930 locsize, false);
61ba1cf9 931
61ba1cf9 932 // Read the symbol names.
645f8123
ILT
933 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
934 off_t strtab_size;
935 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57
ILT
936 &strtab_size,
937 true);
61ba1cf9
ILT
938 const char* pnames = reinterpret_cast<const char*>(pnamesu);
939
940 // Get a view into the output file.
941 off_t output_size = this->output_local_symbol_count_ * sym_size;
942 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
943 output_size);
944
c06b7b0b
ILT
945 const std::vector<Map_to_output>& mo(this->map_to_output());
946
a3ad94ed 947 gold_assert(this->local_values_.size() == loccount);
61ba1cf9 948
61ba1cf9 949 unsigned char* ov = oview;
c06b7b0b 950 psyms += sym_size;
92e059d8 951 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
952 {
953 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6 954
b8e6aad9 955 if (!this->local_values_[i].needs_output_symtab_entry())
f6ce93d6 956 continue;
61ba1cf9
ILT
957
958 unsigned int st_shndx = isym.get_st_shndx();
959 if (st_shndx < elfcpp::SHN_LORESERVE)
960 {
a3ad94ed 961 gold_assert(st_shndx < mo.size());
61ba1cf9
ILT
962 if (mo[st_shndx].output_section == NULL)
963 continue;
ead1e424 964 st_shndx = mo[st_shndx].output_section->out_shndx();
61ba1cf9
ILT
965 }
966
f6ce93d6
ILT
967 elfcpp::Sym_write<size, big_endian> osym(ov);
968
a3ad94ed 969 gold_assert(isym.get_st_name() < strtab_size);
c06b7b0b
ILT
970 const char* name = pnames + isym.get_st_name();
971 osym.put_st_name(sympool->get_offset(name));
b8e6aad9 972 osym.put_st_value(this->local_values_[i].value(this, 0));
61ba1cf9
ILT
973 osym.put_st_size(isym.get_st_size());
974 osym.put_st_info(isym.get_st_info());
975 osym.put_st_other(isym.get_st_other());
976 osym.put_st_shndx(st_shndx);
977
978 ov += sym_size;
979 }
980
a3ad94ed 981 gold_assert(ov - oview == output_size);
61ba1cf9
ILT
982
983 of->write_output_view(this->local_symbol_offset_, output_size, oview);
984}
985
f7e2ee48
ILT
986// Set *INFO to symbolic information about the offset OFFSET in the
987// section SHNDX. Return true if we found something, false if we
988// found nothing.
989
990template<int size, bool big_endian>
991bool
992Sized_relobj<size, big_endian>::get_symbol_location_info(
993 unsigned int shndx,
994 off_t offset,
995 Symbol_location_info* info)
996{
997 if (this->symtab_shndx_ == 0)
998 return false;
999
1000 off_t symbols_size;
1001 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1002 &symbols_size,
1003 false);
1004
1005 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1006 off_t names_size;
1007 const unsigned char* symbol_names_u =
1008 this->section_contents(symbol_names_shndx, &names_size, false);
1009 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1010
1011 const int sym_size = This::sym_size;
1012 const size_t count = symbols_size / sym_size;
1013
1014 const unsigned char* p = symbols;
1015 for (size_t i = 0; i < count; ++i, p += sym_size)
1016 {
1017 elfcpp::Sym<size, big_endian> sym(p);
1018
1019 if (sym.get_st_type() == elfcpp::STT_FILE)
1020 {
1021 if (sym.get_st_name() >= names_size)
1022 info->source_file = "(invalid)";
1023 else
1024 info->source_file = symbol_names + sym.get_st_name();
1025 }
1026 else if (sym.get_st_shndx() == shndx
1027 && static_cast<off_t>(sym.get_st_value()) <= offset
1028 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
5c2c6c95 1029 > offset))
f7e2ee48
ILT
1030 {
1031 if (sym.get_st_name() > names_size)
1032 info->enclosing_symbol_name = "(invalid)";
1033 else
1034 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1035 return true;
1036 }
1037 }
1038
1039 return false;
1040}
1041
54dc6425
ILT
1042// Input_objects methods.
1043
008db82e
ILT
1044// Add a regular relocatable object to the list. Return false if this
1045// object should be ignored.
f6ce93d6 1046
008db82e 1047bool
54dc6425
ILT
1048Input_objects::add_object(Object* obj)
1049{
019cdb1a
ILT
1050 Target* target = obj->target();
1051 if (this->target_ == NULL)
1052 this->target_ = target;
1053 else if (this->target_ != target)
1054 {
1055 gold_error(_("%s: incompatible target"), obj->name().c_str());
1056 return false;
1057 }
1058
008db82e 1059 if (!obj->is_dynamic())
f6ce93d6 1060 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
008db82e
ILT
1061 else
1062 {
1063 // See if this is a duplicate SONAME.
1064 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1065
1066 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1067 this->sonames_.insert(dynobj->soname());
1068 if (!ins.second)
1069 {
1070 // We have already seen a dynamic object with this soname.
1071 return false;
1072 }
1073
1074 this->dynobj_list_.push_back(dynobj);
1075 }
75f65a3e 1076
9025d29d
ILT
1077 set_parameters_size_and_endianness(target->get_size(),
1078 target->is_big_endian());
1079
008db82e 1080 return true;
54dc6425
ILT
1081}
1082
92e059d8
ILT
1083// Relocate_info methods.
1084
1085// Return a string describing the location of a relocation. This is
1086// only used in error messages.
1087
1088template<int size, bool big_endian>
1089std::string
f7e2ee48 1090Relocate_info<size, big_endian>::location(size_t, off_t offset) const
92e059d8 1091{
5c2c6c95
ILT
1092 // See if we can get line-number information from debugging sections.
1093 std::string filename;
1094 std::string file_and_lineno; // Better than filename-only, if available.
4c50553d 1095
24badc65
ILT
1096 Dwarf_line_info<size, big_endian> line_info(this->object);
1097 // This will be "" if we failed to parse the debug info for any reason.
1098 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
4c50553d 1099
92e059d8 1100 std::string ret(this->object->name());
f7e2ee48
ILT
1101 ret += ':';
1102 Symbol_location_info info;
1103 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1104 {
1105 ret += " in function ";
5c2c6c95
ILT
1106 // We could demangle this name before printing, but we don't
1107 // bother because gcc runs linker output through a demangle
1108 // filter itself. The only advantage to demangling here is if
1109 // someone might call ld directly, rather than via gcc. If we
1110 // did want to demangle, cplus_demangle() is in libiberty.
f7e2ee48
ILT
1111 ret += info.enclosing_symbol_name;
1112 ret += ":";
5c2c6c95
ILT
1113 filename = info.source_file;
1114 }
1115
1116 if (!file_and_lineno.empty())
1117 ret += file_and_lineno;
1118 else
1119 {
1120 if (!filename.empty())
1121 ret += filename;
1122 ret += "(";
1123 ret += this->object->section_name(this->data_shndx);
1124 char buf[100];
1125 // Offsets into sections have to be positive.
1126 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1127 ret += buf;
1128 ret += ")";
f7e2ee48 1129 }
92e059d8
ILT
1130 return ret;
1131}
1132
bae7f79e
ILT
1133} // End namespace gold.
1134
1135namespace
1136{
1137
1138using namespace gold;
1139
1140// Read an ELF file with the header and return the appropriate
1141// instance of Object.
1142
1143template<int size, bool big_endian>
1144Object*
1145make_elf_sized_object(const std::string& name, Input_file* input_file,
1146 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1147{
1148 int et = ehdr.get_e_type();
bae7f79e
ILT
1149 if (et == elfcpp::ET_REL)
1150 {
f6ce93d6
ILT
1151 Sized_relobj<size, big_endian>* obj =
1152 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
1153 obj->setup(ehdr);
1154 return obj;
1155 }
dbe717ef
ILT
1156 else if (et == elfcpp::ET_DYN)
1157 {
1158 Sized_dynobj<size, big_endian>* obj =
1159 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1160 obj->setup(ehdr);
1161 return obj;
1162 }
bae7f79e
ILT
1163 else
1164 {
75f2446e
ILT
1165 gold_error(_("%s: unsupported ELF file type %d"),
1166 name.c_str(), et);
1167 return NULL;
bae7f79e
ILT
1168 }
1169}
1170
1171} // End anonymous namespace.
1172
1173namespace gold
1174{
1175
1176// Read an ELF file and return the appropriate instance of Object.
1177
1178Object*
1179make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1180 const unsigned char* p, off_t bytes)
1181{
1182 if (bytes < elfcpp::EI_NIDENT)
1183 {
75f2446e
ILT
1184 gold_error(_("%s: ELF file too short"), name.c_str());
1185 return NULL;
bae7f79e
ILT
1186 }
1187
1188 int v = p[elfcpp::EI_VERSION];
1189 if (v != elfcpp::EV_CURRENT)
1190 {
1191 if (v == elfcpp::EV_NONE)
75f2446e 1192 gold_error(_("%s: invalid ELF version 0"), name.c_str());
bae7f79e 1193 else
75f2446e
ILT
1194 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1195 return NULL;
bae7f79e
ILT
1196 }
1197
1198 int c = p[elfcpp::EI_CLASS];
1199 if (c == elfcpp::ELFCLASSNONE)
1200 {
75f2446e
ILT
1201 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1202 return NULL;
bae7f79e
ILT
1203 }
1204 else if (c != elfcpp::ELFCLASS32
1205 && c != elfcpp::ELFCLASS64)
1206 {
75f2446e
ILT
1207 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1208 return NULL;
bae7f79e
ILT
1209 }
1210
1211 int d = p[elfcpp::EI_DATA];
1212 if (d == elfcpp::ELFDATANONE)
1213 {
75f2446e
ILT
1214 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1215 return NULL;
bae7f79e
ILT
1216 }
1217 else if (d != elfcpp::ELFDATA2LSB
1218 && d != elfcpp::ELFDATA2MSB)
1219 {
75f2446e
ILT
1220 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1221 return NULL;
bae7f79e
ILT
1222 }
1223
1224 bool big_endian = d == elfcpp::ELFDATA2MSB;
1225
1226 if (c == elfcpp::ELFCLASS32)
1227 {
1228 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1229 {
75f2446e
ILT
1230 gold_error(_("%s: ELF file too short"), name.c_str());
1231 return NULL;
bae7f79e
ILT
1232 }
1233 if (big_endian)
1234 {
193a53d9 1235#ifdef HAVE_TARGET_32_BIG
bae7f79e
ILT
1236 elfcpp::Ehdr<32, true> ehdr(p);
1237 return make_elf_sized_object<32, true>(name, input_file,
1238 offset, ehdr);
193a53d9 1239#else
75f2446e
ILT
1240 gold_error(_("%s: not configured to support "
1241 "32-bit big-endian object"),
1242 name.c_str());
1243 return NULL;
193a53d9 1244#endif
bae7f79e
ILT
1245 }
1246 else
1247 {
193a53d9 1248#ifdef HAVE_TARGET_32_LITTLE
bae7f79e
ILT
1249 elfcpp::Ehdr<32, false> ehdr(p);
1250 return make_elf_sized_object<32, false>(name, input_file,
1251 offset, ehdr);
193a53d9 1252#else
75f2446e
ILT
1253 gold_error(_("%s: not configured to support "
1254 "32-bit little-endian object"),
1255 name.c_str());
1256 return NULL;
193a53d9 1257#endif
bae7f79e
ILT
1258 }
1259 }
1260 else
1261 {
1262 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1263 {
75f2446e
ILT
1264 gold_error(_("%s: ELF file too short"), name.c_str());
1265 return NULL;
bae7f79e
ILT
1266 }
1267 if (big_endian)
1268 {
193a53d9 1269#ifdef HAVE_TARGET_64_BIG
bae7f79e
ILT
1270 elfcpp::Ehdr<64, true> ehdr(p);
1271 return make_elf_sized_object<64, true>(name, input_file,
1272 offset, ehdr);
193a53d9 1273#else
75f2446e
ILT
1274 gold_error(_("%s: not configured to support "
1275 "64-bit big-endian object"),
1276 name.c_str());
1277 return NULL;
193a53d9 1278#endif
bae7f79e
ILT
1279 }
1280 else
1281 {
193a53d9 1282#ifdef HAVE_TARGET_64_LITTLE
bae7f79e
ILT
1283 elfcpp::Ehdr<64, false> ehdr(p);
1284 return make_elf_sized_object<64, false>(name, input_file,
1285 offset, ehdr);
193a53d9 1286#else
75f2446e
ILT
1287 gold_error(_("%s: not configured to support "
1288 "64-bit little-endian object"),
1289 name.c_str());
1290 return NULL;
193a53d9 1291#endif
bae7f79e
ILT
1292 }
1293 }
1294}
1295
1296// Instantiate the templates we need. We could use the configure
1297// script to restrict this to only the ones for implemented targets.
1298
193a53d9 1299#ifdef HAVE_TARGET_32_LITTLE
bae7f79e 1300template
f6ce93d6 1301class Sized_relobj<32, false>;
193a53d9 1302#endif
bae7f79e 1303
193a53d9 1304#ifdef HAVE_TARGET_32_BIG
bae7f79e 1305template
f6ce93d6 1306class Sized_relobj<32, true>;
193a53d9 1307#endif
bae7f79e 1308
193a53d9 1309#ifdef HAVE_TARGET_64_LITTLE
bae7f79e 1310template
f6ce93d6 1311class Sized_relobj<64, false>;
193a53d9 1312#endif
bae7f79e 1313
193a53d9 1314#ifdef HAVE_TARGET_64_BIG
bae7f79e 1315template
f6ce93d6 1316class Sized_relobj<64, true>;
193a53d9 1317#endif
bae7f79e 1318
193a53d9 1319#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1320template
1321struct Relocate_info<32, false>;
193a53d9 1322#endif
92e059d8 1323
193a53d9 1324#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1325template
1326struct Relocate_info<32, true>;
193a53d9 1327#endif
92e059d8 1328
193a53d9 1329#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1330template
1331struct Relocate_info<64, false>;
193a53d9 1332#endif
92e059d8 1333
193a53d9 1334#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1335template
1336struct Relocate_info<64, true>;
193a53d9 1337#endif
92e059d8 1338
bae7f79e 1339} // End namespace gold.
This page took 0.130084 seconds and 4 git commands to generate.