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