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