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