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