*** empty log message ***
[deliverable/binutils-gdb.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 #include "gold.h"
4
5 #include <cerrno>
6 #include <cstring>
7 #include <cassert>
8 #include <cstdarg>
9
10 #include "target-select.h"
11 #include "layout.h"
12 #include "output.h"
13 #include "symtab.h"
14 #include "object.h"
15 #include "dynobj.h"
16
17 namespace gold
18 {
19
20 // Class Object.
21
22 // Set the target based on fields in the ELF file header.
23
24 void
25 Object::set_target(int machine, int size, bool big_endian, int osabi,
26 int abiversion)
27 {
28 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
29 if (target == NULL)
30 {
31 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
32 program_name, this->name().c_str(), machine);
33 gold_exit(false);
34 }
35 this->target_ = target;
36 }
37
38 // Report an error for the elfcpp::Elf_file interface.
39
40 void
41 Object::error(const char* format, ...)
42 {
43 va_list args;
44
45 fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
46 va_start(args, format);
47 vfprintf(stderr, format, args);
48 va_end(args);
49 putc('\n', stderr);
50
51 gold_exit(false);
52 }
53
54 // Return a view of the contents of a section.
55
56 const unsigned char*
57 Object::section_contents(unsigned int shndx, off_t* plen)
58 {
59 Location loc(this->do_section_contents(shndx));
60 *plen = loc.data_size;
61 return this->get_view(loc.file_offset, loc.data_size);
62 }
63
64 // Read the section data into SD. This is code common to Sized_relobj
65 // and Sized_dynobj, so we put it into Object.
66
67 template<int size, bool big_endian>
68 void
69 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
70 Read_symbols_data* sd)
71 {
72 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
73
74 // Read the section headers.
75 const off_t shoff = elf_file->shoff();
76 const unsigned int shnum = this->shnum();
77 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size);
78
79 // Read the section names.
80 const unsigned char* pshdrs = sd->section_headers->data();
81 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
82 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
83
84 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
85 {
86 fprintf(stderr,
87 _("%s: %s: section name section has wrong type: %u\n"),
88 program_name, this->name().c_str(),
89 static_cast<unsigned int>(shdrnames.get_sh_type()));
90 gold_exit(false);
91 }
92
93 sd->section_names_size = shdrnames.get_sh_size();
94 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
95 sd->section_names_size);
96 }
97
98 // If NAME is the name of a special .gnu.warning section, arrange for
99 // the warning to be issued. SHNDX is the section index. Return
100 // whether it is a warning section.
101
102 bool
103 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
104 Symbol_table* symtab)
105 {
106 const char warn_prefix[] = ".gnu.warning.";
107 const int warn_prefix_len = sizeof warn_prefix - 1;
108 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
109 {
110 symtab->add_warning(name + warn_prefix_len, this, shndx);
111 return true;
112 }
113 return false;
114 }
115
116 // Class Sized_relobj.
117
118 template<int size, bool big_endian>
119 Sized_relobj<size, big_endian>::Sized_relobj(
120 const std::string& name,
121 Input_file* input_file,
122 off_t offset,
123 const elfcpp::Ehdr<size, big_endian>& ehdr)
124 : Relobj(name, input_file, offset),
125 elf_file_(this, ehdr),
126 symtab_shndx_(-1U),
127 local_symbol_count_(0),
128 output_local_symbol_count_(0),
129 symbols_(NULL),
130 local_symbol_offset_(0),
131 values_(NULL)
132 {
133 }
134
135 template<int size, bool big_endian>
136 Sized_relobj<size, big_endian>::~Sized_relobj()
137 {
138 }
139
140 // Set up an object file based on the file header. This sets up the
141 // target and reads the section information.
142
143 template<int size, bool big_endian>
144 void
145 Sized_relobj<size, big_endian>::setup(
146 const elfcpp::Ehdr<size, big_endian>& ehdr)
147 {
148 this->set_target(ehdr.get_e_machine(), size, big_endian,
149 ehdr.get_e_ident()[elfcpp::EI_OSABI],
150 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
151
152 const unsigned int shnum = this->elf_file_.shnum();
153 this->set_shnum(shnum);
154 }
155
156 // Find the SHT_SYMTAB section, given the section headers. The ELF
157 // standard says that maybe in the future there can be more than one
158 // SHT_SYMTAB section. Until somebody figures out how that could
159 // work, we assume there is only one.
160
161 template<int size, bool big_endian>
162 void
163 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
164 {
165 const unsigned int shnum = this->shnum();
166 this->symtab_shndx_ = 0;
167 if (shnum > 0)
168 {
169 // Look through the sections in reverse order, since gas tends
170 // to put the symbol table at the end.
171 const unsigned char* p = pshdrs + shnum * This::shdr_size;
172 unsigned int i = shnum;
173 while (i > 0)
174 {
175 --i;
176 p -= This::shdr_size;
177 typename This::Shdr shdr(p);
178 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
179 {
180 this->symtab_shndx_ = i;
181 break;
182 }
183 }
184 }
185 }
186
187 // Read the sections and symbols from an object file.
188
189 template<int size, bool big_endian>
190 void
191 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
192 {
193 this->read_section_data(&this->elf_file_, sd);
194
195 const unsigned char* const pshdrs = sd->section_headers->data();
196
197 this->find_symtab(pshdrs);
198
199 if (this->symtab_shndx_ == 0)
200 {
201 // No symbol table. Weird but legal.
202 sd->symbols = NULL;
203 sd->symbols_size = 0;
204 sd->symbol_names = NULL;
205 sd->symbol_names_size = 0;
206 return;
207 }
208
209 // Get the symbol table section header.
210 typename This::Shdr symtabshdr(pshdrs
211 + this->symtab_shndx_ * This::shdr_size);
212 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
213
214 // We only need the external symbols.
215 const int sym_size = This::sym_size;
216 const unsigned int loccount = symtabshdr.get_sh_info();
217 this->local_symbol_count_ = loccount;
218 off_t locsize = loccount * sym_size;
219 off_t extoff = symtabshdr.get_sh_offset() + locsize;
220 off_t extsize = symtabshdr.get_sh_size() - locsize;
221
222 // Read the symbol table.
223 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
224
225 // Read the section header for the symbol names.
226 unsigned int strtab_shndx = symtabshdr.get_sh_link();
227 if (strtab_shndx >= this->shnum())
228 {
229 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
230 program_name, this->name().c_str(), strtab_shndx);
231 gold_exit(false);
232 }
233 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
234 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
235 {
236 fprintf(stderr,
237 _("%s: %s: symbol table name section has wrong type: %u\n"),
238 program_name, this->name().c_str(),
239 static_cast<unsigned int>(strtabshdr.get_sh_type()));
240 gold_exit(false);
241 }
242
243 // Read the symbol names.
244 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
245 strtabshdr.get_sh_size());
246
247 sd->symbols = fvsymtab;
248 sd->symbols_size = extsize;
249 sd->symbol_names = fvstrtab;
250 sd->symbol_names_size = strtabshdr.get_sh_size();
251 }
252
253 // Return whether to include a section group in the link. LAYOUT is
254 // used to keep track of which section groups we have already seen.
255 // INDEX is the index of the section group and SHDR is the section
256 // header. If we do not want to include this group, we set bits in
257 // OMIT for each section which should be discarded.
258
259 template<int size, bool big_endian>
260 bool
261 Sized_relobj<size, big_endian>::include_section_group(
262 Layout* layout,
263 unsigned int index,
264 const elfcpp::Shdr<size, big_endian>& shdr,
265 std::vector<bool>* omit)
266 {
267 // Read the section contents.
268 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
269 shdr.get_sh_size());
270 const elfcpp::Elf_Word* pword =
271 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
272
273 // The first word contains flags. We only care about COMDAT section
274 // groups. Other section groups are always included in the link
275 // just like ordinary sections.
276 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
277 if ((flags & elfcpp::GRP_COMDAT) == 0)
278 return true;
279
280 // Look up the group signature, which is the name of a symbol. This
281 // is a lot of effort to go to to read a string. Why didn't they
282 // just use the name of the SHT_GROUP section as the group
283 // signature?
284
285 // Get the appropriate symbol table header (this will normally be
286 // the single SHT_SYMTAB section, but in principle it need not be).
287 const unsigned int link = shdr.get_sh_link();
288 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
289
290 // Read the symbol table entry.
291 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
292 {
293 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
294 program_name, this->name().c_str(), index, shdr.get_sh_info());
295 gold_exit(false);
296 }
297 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
298 const unsigned char* psym = this->get_view(symoff, This::sym_size);
299 elfcpp::Sym<size, big_endian> sym(psym);
300
301 // Read the symbol table names.
302 off_t symnamelen;
303 const unsigned char* psymnamesu;
304 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen);
305 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
306
307 // Get the section group signature.
308 if (sym.get_st_name() >= symnamelen)
309 {
310 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
311 program_name, this->name().c_str(), shdr.get_sh_info(),
312 sym.get_st_name());
313 gold_exit(false);
314 }
315
316 const char* signature = psymnames + sym.get_st_name();
317
318 // It seems that some versions of gas will create a section group
319 // associated with a section symbol, and then fail to give a name to
320 // the section symbol. In such a case, use the name of the section.
321 // FIXME.
322 std::string secname;
323 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
324 {
325 secname = this->section_name(sym.get_st_shndx());
326 signature = secname.c_str();
327 }
328
329 // Record this section group, and see whether we've already seen one
330 // with the same signature.
331 if (layout->add_comdat(signature, true))
332 return true;
333
334 // This is a duplicate. We want to discard the sections in this
335 // group.
336 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
337 for (size_t i = 1; i < count; ++i)
338 {
339 elfcpp::Elf_Word secnum =
340 elfcpp::Swap<32, big_endian>::readval(pword + i);
341 if (secnum >= this->shnum())
342 {
343 fprintf(stderr,
344 _("%s: %s: section %u in section group %u out of range"),
345 program_name, this->name().c_str(), secnum,
346 index);
347 gold_exit(false);
348 }
349 (*omit)[secnum] = true;
350 }
351
352 return false;
353 }
354
355 // Whether to include a linkonce section in the link. NAME is the
356 // name of the section and SHDR is the section header.
357
358 // Linkonce sections are a GNU extension implemented in the original
359 // GNU linker before section groups were defined. The semantics are
360 // that we only include one linkonce section with a given name. The
361 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
362 // where T is the type of section and SYMNAME is the name of a symbol.
363 // In an attempt to make linkonce sections interact well with section
364 // groups, we try to identify SYMNAME and use it like a section group
365 // signature. We want to block section groups with that signature,
366 // but not other linkonce sections with that signature. We also use
367 // the full name of the linkonce section as a normal section group
368 // signature.
369
370 template<int size, bool big_endian>
371 bool
372 Sized_relobj<size, big_endian>::include_linkonce_section(
373 Layout* layout,
374 const char* name,
375 const elfcpp::Shdr<size, big_endian>&)
376 {
377 const char* symname = strrchr(name, '.') + 1;
378 bool include1 = layout->add_comdat(symname, false);
379 bool include2 = layout->add_comdat(name, true);
380 return include1 && include2;
381 }
382
383 // Lay out the input sections. We walk through the sections and check
384 // whether they should be included in the link. If they should, we
385 // pass them to the Layout object, which will return an output section
386 // and an offset.
387
388 template<int size, bool big_endian>
389 void
390 Sized_relobj<size, big_endian>::do_layout(const General_options& options,
391 Symbol_table* symtab,
392 Layout* layout,
393 Read_symbols_data* sd)
394 {
395 const unsigned int shnum = this->shnum();
396 if (shnum == 0)
397 return;
398
399 // Get the section headers.
400 const unsigned char* pshdrs = sd->section_headers->data();
401
402 // Get the section names.
403 const unsigned char* pnamesu = sd->section_names->data();
404 const char* pnames = reinterpret_cast<const char*>(pnamesu);
405
406 std::vector<Map_to_output>& map_sections(this->map_to_output());
407 map_sections.resize(shnum);
408
409 // Keep track of which sections to omit.
410 std::vector<bool> omit(shnum, false);
411
412 // Skip the first, dummy, section.
413 pshdrs += This::shdr_size;
414 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
415 {
416 typename This::Shdr shdr(pshdrs);
417
418 if (shdr.get_sh_name() >= sd->section_names_size)
419 {
420 fprintf(stderr,
421 _("%s: %s: bad section name offset for section %u: %lu\n"),
422 program_name, this->name().c_str(), i,
423 static_cast<unsigned long>(shdr.get_sh_name()));
424 gold_exit(false);
425 }
426
427 const char* name = pnames + shdr.get_sh_name();
428
429 if (this->handle_gnu_warning_section(name, i, symtab))
430 {
431 if (!options.is_relocatable())
432 omit[i] = true;
433 }
434
435 bool discard = omit[i];
436 if (!discard)
437 {
438 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
439 {
440 if (!this->include_section_group(layout, i, shdr, &omit))
441 discard = true;
442 }
443 else if (Layout::is_linkonce(name))
444 {
445 if (!this->include_linkonce_section(layout, name, shdr))
446 discard = true;
447 }
448 }
449
450 if (discard)
451 {
452 // Do not include this section in the link.
453 map_sections[i].output_section = NULL;
454 continue;
455 }
456
457 off_t offset;
458 Output_section* os = layout->layout(this, i, name, shdr, &offset);
459
460 map_sections[i].output_section = os;
461 map_sections[i].offset = offset;
462 }
463
464 delete sd->section_headers;
465 sd->section_headers = NULL;
466 delete sd->section_names;
467 sd->section_names = NULL;
468 }
469
470 // Add the symbols to the symbol table.
471
472 template<int size, bool big_endian>
473 void
474 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
475 Read_symbols_data* sd)
476 {
477 if (sd->symbols == NULL)
478 {
479 assert(sd->symbol_names == NULL);
480 return;
481 }
482
483 const int sym_size = This::sym_size;
484 size_t symcount = sd->symbols_size / sym_size;
485 if (symcount * sym_size != sd->symbols_size)
486 {
487 fprintf(stderr,
488 _("%s: %s: size of symbols is not multiple of symbol size\n"),
489 program_name, this->name().c_str());
490 gold_exit(false);
491 }
492
493 this->symbols_ = new Symbol*[symcount];
494
495 const char* sym_names =
496 reinterpret_cast<const char*>(sd->symbol_names->data());
497 symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
498 sd->symbol_names_size, this->symbols_);
499
500 delete sd->symbols;
501 sd->symbols = NULL;
502 delete sd->symbol_names;
503 sd->symbol_names = NULL;
504 }
505
506 // Finalize the local symbols. Here we record the file offset at
507 // which they should be output, we add their names to *POOL, and we
508 // add their values to THIS->VALUES_. Return the new file offset.
509 // This function is always called from the main thread. The actual
510 // output of the local symbols will occur in a separate task.
511
512 template<int size, bool big_endian>
513 off_t
514 Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
515 Stringpool* pool)
516 {
517 assert(this->symtab_shndx_ != -1U);
518 if (this->symtab_shndx_ == 0)
519 {
520 // This object has no symbols. Weird but legal.
521 return off;
522 }
523
524 off = align_address(off, size >> 3);
525
526 this->local_symbol_offset_ = off;
527
528 // Read the symbol table section header.
529 const unsigned int symtab_shndx = this->symtab_shndx_;
530 typename This::Shdr symtabshdr(this,
531 this->elf_file_.section_header(symtab_shndx));
532 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
533
534 // Read the local symbols.
535 const int sym_size = This::sym_size;
536 const unsigned int loccount = this->local_symbol_count_;
537 assert(loccount == symtabshdr.get_sh_info());
538 off_t locsize = loccount * sym_size;
539 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
540 locsize);
541
542 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
543
544 // Read the symbol names.
545 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
546 off_t strtab_size;
547 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
548 &strtab_size);
549 const char* pnames = reinterpret_cast<const char*>(pnamesu);
550
551 // Loop over the local symbols.
552
553 std::vector<Map_to_output>& mo(this->map_to_output());
554 unsigned int shnum = this->shnum();
555 unsigned int count = 0;
556 // Skip the first, dummy, symbol.
557 psyms += sym_size;
558 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
559 {
560 elfcpp::Sym<size, big_endian> sym(psyms);
561
562 unsigned int shndx = sym.get_st_shndx();
563
564 if (shndx >= elfcpp::SHN_LORESERVE)
565 {
566 if (shndx == elfcpp::SHN_ABS)
567 this->values_[i] = sym.get_st_value();
568 else
569 {
570 // FIXME: Handle SHN_XINDEX.
571 fprintf(stderr,
572 _("%s: %s: unknown section index %u "
573 "for local symbol %u\n"),
574 program_name, this->name().c_str(), shndx, i);
575 gold_exit(false);
576 }
577 }
578 else
579 {
580 if (shndx >= shnum)
581 {
582 fprintf(stderr,
583 _("%s: %s: local symbol %u section index %u "
584 "out of range\n"),
585 program_name, this->name().c_str(), i, shndx);
586 gold_exit(false);
587 }
588
589 if (mo[shndx].output_section == NULL)
590 {
591 this->values_[i] = 0;
592 continue;
593 }
594
595 this->values_[i] = (mo[shndx].output_section->address()
596 + mo[shndx].offset
597 + sym.get_st_value());
598 }
599
600 if (sym.get_st_type() != elfcpp::STT_SECTION)
601 {
602 if (sym.get_st_name() >= strtab_size)
603 {
604 fprintf(stderr,
605 _("%s: %s: local symbol %u section name "
606 "out of range: %u >= %u\n"),
607 program_name, this->name().c_str(),
608 i, sym.get_st_name(),
609 static_cast<unsigned int>(strtab_size));
610 gold_exit(false);
611 }
612
613 pool->add(pnames + sym.get_st_name(), NULL);
614 off += sym_size;
615 ++count;
616 }
617 }
618
619 this->output_local_symbol_count_ = count;
620
621 return off;
622 }
623
624 // Write out the local symbols.
625
626 template<int size, bool big_endian>
627 void
628 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
629 const Stringpool* sympool)
630 {
631 assert(this->symtab_shndx_ != -1U);
632 if (this->symtab_shndx_ == 0)
633 {
634 // This object has no symbols. Weird but legal.
635 return;
636 }
637
638 // Read the symbol table section header.
639 const unsigned int symtab_shndx = this->symtab_shndx_;
640 typename This::Shdr symtabshdr(this,
641 this->elf_file_.section_header(symtab_shndx));
642 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
643 const unsigned int loccount = this->local_symbol_count_;
644 assert(loccount == symtabshdr.get_sh_info());
645
646 // Read the local symbols.
647 const int sym_size = This::sym_size;
648 off_t locsize = loccount * sym_size;
649 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
650 locsize);
651
652 // Read the symbol names.
653 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
654 off_t strtab_size;
655 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
656 &strtab_size);
657 const char* pnames = reinterpret_cast<const char*>(pnamesu);
658
659 // Get a view into the output file.
660 off_t output_size = this->output_local_symbol_count_ * sym_size;
661 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
662 output_size);
663
664 std::vector<Map_to_output>& mo(this->map_to_output());
665
666 psyms += sym_size;
667 unsigned char* ov = oview;
668 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
669 {
670 elfcpp::Sym<size, big_endian> isym(psyms);
671
672 if (isym.get_st_type() == elfcpp::STT_SECTION)
673 continue;
674
675 unsigned int st_shndx = isym.get_st_shndx();
676 if (st_shndx < elfcpp::SHN_LORESERVE)
677 {
678 assert(st_shndx < mo.size());
679 if (mo[st_shndx].output_section == NULL)
680 continue;
681 st_shndx = mo[st_shndx].output_section->out_shndx();
682 }
683
684 elfcpp::Sym_write<size, big_endian> osym(ov);
685
686 assert(isym.get_st_name() < strtab_size);
687 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
688 osym.put_st_value(this->values_[i]);
689 osym.put_st_size(isym.get_st_size());
690 osym.put_st_info(isym.get_st_info());
691 osym.put_st_other(isym.get_st_other());
692 osym.put_st_shndx(st_shndx);
693
694 ov += sym_size;
695 }
696
697 assert(ov - oview == output_size);
698
699 of->write_output_view(this->local_symbol_offset_, output_size, oview);
700 }
701
702 // Input_objects methods.
703
704 // Add a regular relocatable object to the list.
705
706 void
707 Input_objects::add_object(Object* obj)
708 {
709 if (obj->is_dynamic())
710 this->dynobj_list_.push_back(static_cast<Dynobj*>(obj));
711 else
712 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
713
714 Target* target = obj->target();
715 if (this->target_ == NULL)
716 this->target_ = target;
717 else if (this->target_ != target)
718 {
719 fprintf(stderr, "%s: %s: incompatible target\n",
720 program_name, obj->name().c_str());
721 gold_exit(false);
722 }
723 }
724
725 // Relocate_info methods.
726
727 // Return a string describing the location of a relocation. This is
728 // only used in error messages.
729
730 template<int size, bool big_endian>
731 std::string
732 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
733 {
734 std::string ret(this->object->name());
735 ret += ": reloc ";
736 char buf[100];
737 snprintf(buf, sizeof buf, "%zu", relnum);
738 ret += buf;
739 ret += " in reloc section ";
740 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
741 ret += buf;
742 ret += " (" + this->object->section_name(this->reloc_shndx);
743 ret += ") for section ";
744 snprintf(buf, sizeof buf, "%u", this->data_shndx);
745 ret += buf;
746 ret += " (" + this->object->section_name(this->data_shndx) + ")";
747 return ret;
748 }
749
750 } // End namespace gold.
751
752 namespace
753 {
754
755 using namespace gold;
756
757 // Read an ELF file with the header and return the appropriate
758 // instance of Object.
759
760 template<int size, bool big_endian>
761 Object*
762 make_elf_sized_object(const std::string& name, Input_file* input_file,
763 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
764 {
765 int et = ehdr.get_e_type();
766 if (et == elfcpp::ET_REL)
767 {
768 Sized_relobj<size, big_endian>* obj =
769 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
770 obj->setup(ehdr);
771 return obj;
772 }
773 else if (et == elfcpp::ET_DYN)
774 {
775 Sized_dynobj<size, big_endian>* obj =
776 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
777 obj->setup(ehdr);
778 return obj;
779 }
780 else
781 {
782 fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
783 program_name, name.c_str(), et);
784 gold_exit(false);
785 }
786 }
787
788 } // End anonymous namespace.
789
790 namespace gold
791 {
792
793 // Read an ELF file and return the appropriate instance of Object.
794
795 Object*
796 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
797 const unsigned char* p, off_t bytes)
798 {
799 if (bytes < elfcpp::EI_NIDENT)
800 {
801 fprintf(stderr, _("%s: %s: ELF file too short\n"),
802 program_name, name.c_str());
803 gold_exit(false);
804 }
805
806 int v = p[elfcpp::EI_VERSION];
807 if (v != elfcpp::EV_CURRENT)
808 {
809 if (v == elfcpp::EV_NONE)
810 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
811 program_name, name.c_str());
812 else
813 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
814 program_name, name.c_str(), v);
815 gold_exit(false);
816 }
817
818 int c = p[elfcpp::EI_CLASS];
819 if (c == elfcpp::ELFCLASSNONE)
820 {
821 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
822 program_name, name.c_str());
823 gold_exit(false);
824 }
825 else if (c != elfcpp::ELFCLASS32
826 && c != elfcpp::ELFCLASS64)
827 {
828 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
829 program_name, name.c_str(), c);
830 gold_exit(false);
831 }
832
833 int d = p[elfcpp::EI_DATA];
834 if (d == elfcpp::ELFDATANONE)
835 {
836 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
837 program_name, name.c_str());
838 gold_exit(false);
839 }
840 else if (d != elfcpp::ELFDATA2LSB
841 && d != elfcpp::ELFDATA2MSB)
842 {
843 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
844 program_name, name.c_str(), d);
845 gold_exit(false);
846 }
847
848 bool big_endian = d == elfcpp::ELFDATA2MSB;
849
850 if (c == elfcpp::ELFCLASS32)
851 {
852 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
853 {
854 fprintf(stderr, _("%s: %s: ELF file too short\n"),
855 program_name, name.c_str());
856 gold_exit(false);
857 }
858 if (big_endian)
859 {
860 elfcpp::Ehdr<32, true> ehdr(p);
861 return make_elf_sized_object<32, true>(name, input_file,
862 offset, ehdr);
863 }
864 else
865 {
866 elfcpp::Ehdr<32, false> ehdr(p);
867 return make_elf_sized_object<32, false>(name, input_file,
868 offset, ehdr);
869 }
870 }
871 else
872 {
873 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
874 {
875 fprintf(stderr, _("%s: %s: ELF file too short\n"),
876 program_name, name.c_str());
877 gold_exit(false);
878 }
879 if (big_endian)
880 {
881 elfcpp::Ehdr<64, true> ehdr(p);
882 return make_elf_sized_object<64, true>(name, input_file,
883 offset, ehdr);
884 }
885 else
886 {
887 elfcpp::Ehdr<64, false> ehdr(p);
888 return make_elf_sized_object<64, false>(name, input_file,
889 offset, ehdr);
890 }
891 }
892 }
893
894 // Instantiate the templates we need. We could use the configure
895 // script to restrict this to only the ones for implemented targets.
896
897 template
898 class Sized_relobj<32, false>;
899
900 template
901 class Sized_relobj<32, true>;
902
903 template
904 class Sized_relobj<64, false>;
905
906 template
907 class Sized_relobj<64, true>;
908
909 template
910 struct Relocate_info<32, false>;
911
912 template
913 struct Relocate_info<32, true>;
914
915 template
916 struct Relocate_info<64, false>;
917
918 template
919 struct Relocate_info<64, true>;
920
921 } // End namespace gold.
This page took 0.062273 seconds and 4 git commands to generate.