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