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