2007-10-03 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / object.cc
blob2dbd0f25e1f7af896a1ae98112ce976ca03632fc
1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
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"
36 namespace gold
39 // Class Object.
41 // Set the target based on fields in the ELF file header.
43 void
44 Object::set_target(int machine, int size, bool big_endian, int osabi,
45 int abiversion)
47 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
48 if (target == NULL)
50 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
51 program_name, this->name().c_str(), machine);
52 gold_exit(false);
54 this->target_ = target;
57 // Report an error for the elfcpp::Elf_file interface.
59 void
60 Object::error(const char* format, ...)
62 va_list args;
64 fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
65 va_start(args, format);
66 vfprintf(stderr, format, args);
67 va_end(args);
68 putc('\n', stderr);
70 gold_exit(false);
73 // Return a view of the contents of a section.
75 const unsigned char*
76 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
78 Location loc(this->do_section_contents(shndx));
79 *plen = loc.data_size;
80 return this->get_view(loc.file_offset, loc.data_size, cache);
83 // Read the section data into SD. This is code common to Sized_relobj
84 // and Sized_dynobj, so we put it into Object.
86 template<int size, bool big_endian>
87 void
88 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
89 Read_symbols_data* sd)
91 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
93 // Read the section headers.
94 const off_t shoff = elf_file->shoff();
95 const unsigned int shnum = this->shnum();
96 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
98 // Read the section names.
99 const unsigned char* pshdrs = sd->section_headers->data();
100 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
101 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
103 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
105 fprintf(stderr,
106 _("%s: %s: section name section has wrong type: %u\n"),
107 program_name, this->name().c_str(),
108 static_cast<unsigned int>(shdrnames.get_sh_type()));
109 gold_exit(false);
112 sd->section_names_size = shdrnames.get_sh_size();
113 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
114 sd->section_names_size, false);
117 // If NAME is the name of a special .gnu.warning section, arrange for
118 // the warning to be issued. SHNDX is the section index. Return
119 // whether it is a warning section.
121 bool
122 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
123 Symbol_table* symtab)
125 const char warn_prefix[] = ".gnu.warning.";
126 const int warn_prefix_len = sizeof warn_prefix - 1;
127 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
129 symtab->add_warning(name + warn_prefix_len, this, shndx);
130 return true;
132 return false;
135 // Class Sized_relobj.
137 template<int size, bool big_endian>
138 Sized_relobj<size, big_endian>::Sized_relobj(
139 const std::string& name,
140 Input_file* input_file,
141 off_t offset,
142 const elfcpp::Ehdr<size, big_endian>& ehdr)
143 : Relobj(name, input_file, offset),
144 elf_file_(this, ehdr),
145 symtab_shndx_(-1U),
146 local_symbol_count_(0),
147 output_local_symbol_count_(0),
148 symbols_(NULL),
149 local_symbol_offset_(0),
150 local_values_()
154 template<int size, bool big_endian>
155 Sized_relobj<size, big_endian>::~Sized_relobj()
159 // Set up an object file based on the file header. This sets up the
160 // target and reads the section information.
162 template<int size, bool big_endian>
163 void
164 Sized_relobj<size, big_endian>::setup(
165 const elfcpp::Ehdr<size, big_endian>& ehdr)
167 this->set_target(ehdr.get_e_machine(), size, big_endian,
168 ehdr.get_e_ident()[elfcpp::EI_OSABI],
169 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
171 const unsigned int shnum = this->elf_file_.shnum();
172 this->set_shnum(shnum);
175 // Find the SHT_SYMTAB section, given the section headers. The ELF
176 // standard says that maybe in the future there can be more than one
177 // SHT_SYMTAB section. Until somebody figures out how that could
178 // work, we assume there is only one.
180 template<int size, bool big_endian>
181 void
182 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
184 const unsigned int shnum = this->shnum();
185 this->symtab_shndx_ = 0;
186 if (shnum > 0)
188 // Look through the sections in reverse order, since gas tends
189 // to put the symbol table at the end.
190 const unsigned char* p = pshdrs + shnum * This::shdr_size;
191 unsigned int i = shnum;
192 while (i > 0)
194 --i;
195 p -= This::shdr_size;
196 typename This::Shdr shdr(p);
197 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
199 this->symtab_shndx_ = i;
200 break;
206 // Read the sections and symbols from an object file.
208 template<int size, bool big_endian>
209 void
210 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
212 this->read_section_data(&this->elf_file_, sd);
214 const unsigned char* const pshdrs = sd->section_headers->data();
216 this->find_symtab(pshdrs);
218 if (this->symtab_shndx_ == 0)
220 // No symbol table. Weird but legal.
221 sd->symbols = NULL;
222 sd->symbols_size = 0;
223 sd->symbol_names = NULL;
224 sd->symbol_names_size = 0;
225 return;
228 // Get the symbol table section header.
229 typename This::Shdr symtabshdr(pshdrs
230 + this->symtab_shndx_ * This::shdr_size);
231 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
233 // We only need the external symbols.
234 const int sym_size = This::sym_size;
235 const unsigned int loccount = symtabshdr.get_sh_info();
236 this->local_symbol_count_ = loccount;
237 off_t locsize = loccount * sym_size;
238 off_t extoff = symtabshdr.get_sh_offset() + locsize;
239 off_t extsize = symtabshdr.get_sh_size() - locsize;
241 // Read the symbol table.
242 File_view* fvsymtab = this->get_lasting_view(extoff, extsize, false);
244 // Read the section header for the symbol names.
245 unsigned int strtab_shndx = symtabshdr.get_sh_link();
246 if (strtab_shndx >= this->shnum())
248 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
249 program_name, this->name().c_str(), strtab_shndx);
250 gold_exit(false);
252 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
253 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
255 fprintf(stderr,
256 _("%s: %s: symbol table name section has wrong type: %u\n"),
257 program_name, this->name().c_str(),
258 static_cast<unsigned int>(strtabshdr.get_sh_type()));
259 gold_exit(false);
262 // Read the symbol names.
263 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
264 strtabshdr.get_sh_size(), true);
266 sd->symbols = fvsymtab;
267 sd->symbols_size = extsize;
268 sd->symbol_names = fvstrtab;
269 sd->symbol_names_size = strtabshdr.get_sh_size();
272 // Return whether to include a section group in the link. LAYOUT is
273 // used to keep track of which section groups we have already seen.
274 // INDEX is the index of the section group and SHDR is the section
275 // header. If we do not want to include this group, we set bits in
276 // OMIT for each section which should be discarded.
278 template<int size, bool big_endian>
279 bool
280 Sized_relobj<size, big_endian>::include_section_group(
281 Layout* layout,
282 unsigned int index,
283 const elfcpp::Shdr<size, big_endian>& shdr,
284 std::vector<bool>* omit)
286 // Read the section contents.
287 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
288 shdr.get_sh_size(), false);
289 const elfcpp::Elf_Word* pword =
290 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
292 // The first word contains flags. We only care about COMDAT section
293 // groups. Other section groups are always included in the link
294 // just like ordinary sections.
295 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
296 if ((flags & elfcpp::GRP_COMDAT) == 0)
297 return true;
299 // Look up the group signature, which is the name of a symbol. This
300 // is a lot of effort to go to to read a string. Why didn't they
301 // just use the name of the SHT_GROUP section as the group
302 // signature?
304 // Get the appropriate symbol table header (this will normally be
305 // the single SHT_SYMTAB section, but in principle it need not be).
306 const unsigned int link = shdr.get_sh_link();
307 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
309 // Read the symbol table entry.
310 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
312 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
313 program_name, this->name().c_str(), index, shdr.get_sh_info());
314 gold_exit(false);
316 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
317 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
318 elfcpp::Sym<size, big_endian> sym(psym);
320 // Read the symbol table names.
321 off_t symnamelen;
322 const unsigned char* psymnamesu;
323 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
324 true);
325 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
327 // Get the section group signature.
328 if (sym.get_st_name() >= symnamelen)
330 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
331 program_name, this->name().c_str(), shdr.get_sh_info(),
332 sym.get_st_name());
333 gold_exit(false);
336 const char* signature = psymnames + sym.get_st_name();
338 // It seems that some versions of gas will create a section group
339 // associated with a section symbol, and then fail to give a name to
340 // the section symbol. In such a case, use the name of the section.
341 // FIXME.
342 std::string secname;
343 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
345 secname = this->section_name(sym.get_st_shndx());
346 signature = secname.c_str();
349 // Record this section group, and see whether we've already seen one
350 // with the same signature.
351 if (layout->add_comdat(signature, true))
352 return true;
354 // This is a duplicate. We want to discard the sections in this
355 // group.
356 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
357 for (size_t i = 1; i < count; ++i)
359 elfcpp::Elf_Word secnum =
360 elfcpp::Swap<32, big_endian>::readval(pword + i);
361 if (secnum >= this->shnum())
363 fprintf(stderr,
364 _("%s: %s: section %u in section group %u out of range"),
365 program_name, this->name().c_str(), secnum,
366 index);
367 gold_exit(false);
369 (*omit)[secnum] = true;
372 return false;
375 // Whether to include a linkonce section in the link. NAME is the
376 // name of the section and SHDR is the section header.
378 // Linkonce sections are a GNU extension implemented in the original
379 // GNU linker before section groups were defined. The semantics are
380 // that we only include one linkonce section with a given name. The
381 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
382 // where T is the type of section and SYMNAME is the name of a symbol.
383 // In an attempt to make linkonce sections interact well with section
384 // groups, we try to identify SYMNAME and use it like a section group
385 // signature. We want to block section groups with that signature,
386 // but not other linkonce sections with that signature. We also use
387 // the full name of the linkonce section as a normal section group
388 // signature.
390 template<int size, bool big_endian>
391 bool
392 Sized_relobj<size, big_endian>::include_linkonce_section(
393 Layout* layout,
394 const char* name,
395 const elfcpp::Shdr<size, big_endian>&)
397 const char* symname = strrchr(name, '.') + 1;
398 bool include1 = layout->add_comdat(symname, false);
399 bool include2 = layout->add_comdat(name, true);
400 return include1 && include2;
403 // Lay out the input sections. We walk through the sections and check
404 // whether they should be included in the link. If they should, we
405 // pass them to the Layout object, which will return an output section
406 // and an offset.
408 template<int size, bool big_endian>
409 void
410 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
411 Layout* layout,
412 Read_symbols_data* sd)
414 const unsigned int shnum = this->shnum();
415 if (shnum == 0)
416 return;
418 // Get the section headers.
419 const unsigned char* pshdrs = sd->section_headers->data();
421 // Get the section names.
422 const unsigned char* pnamesu = sd->section_names->data();
423 const char* pnames = reinterpret_cast<const char*>(pnamesu);
425 std::vector<Map_to_output>& map_sections(this->map_to_output());
426 map_sections.resize(shnum);
428 // Keep track of which sections to omit.
429 std::vector<bool> omit(shnum, false);
431 // Skip the first, dummy, section.
432 pshdrs += This::shdr_size;
433 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
435 typename This::Shdr shdr(pshdrs);
437 if (shdr.get_sh_name() >= sd->section_names_size)
439 fprintf(stderr,
440 _("%s: %s: bad section name offset for section %u: %lu\n"),
441 program_name, this->name().c_str(), i,
442 static_cast<unsigned long>(shdr.get_sh_name()));
443 gold_exit(false);
446 const char* name = pnames + shdr.get_sh_name();
448 if (this->handle_gnu_warning_section(name, i, symtab))
450 if (!parameters->output_is_object())
451 omit[i] = true;
454 bool discard = omit[i];
455 if (!discard)
457 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
459 if (!this->include_section_group(layout, i, shdr, &omit))
460 discard = true;
462 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
463 && Layout::is_linkonce(name))
465 if (!this->include_linkonce_section(layout, name, shdr))
466 discard = true;
470 if (discard)
472 // Do not include this section in the link.
473 map_sections[i].output_section = NULL;
474 continue;
477 off_t offset;
478 Output_section* os = layout->layout(this, i, name, shdr, &offset);
480 map_sections[i].output_section = os;
481 map_sections[i].offset = offset;
484 delete sd->section_headers;
485 sd->section_headers = NULL;
486 delete sd->section_names;
487 sd->section_names = NULL;
490 // Add the symbols to the symbol table.
492 template<int size, bool big_endian>
493 void
494 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
495 Read_symbols_data* sd)
497 if (sd->symbols == NULL)
499 gold_assert(sd->symbol_names == NULL);
500 return;
503 const int sym_size = This::sym_size;
504 size_t symcount = sd->symbols_size / sym_size;
505 if (static_cast<off_t>(symcount * sym_size) != sd->symbols_size)
507 fprintf(stderr,
508 _("%s: %s: size of symbols is not multiple of symbol size\n"),
509 program_name, this->name().c_str());
510 gold_exit(false);
513 this->symbols_ = new Symbol*[symcount];
515 const char* sym_names =
516 reinterpret_cast<const char*>(sd->symbol_names->data());
517 symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
518 sd->symbol_names_size, this->symbols_);
520 delete sd->symbols;
521 sd->symbols = NULL;
522 delete sd->symbol_names;
523 sd->symbol_names = NULL;
526 // Finalize the local symbols. Here we record the file offset at
527 // which they should be output, we add their names to *POOL, and we
528 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
529 // This function is always called from the main thread. The actual
530 // output of the local symbols will occur in a separate task.
532 template<int size, bool big_endian>
533 unsigned int
534 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
535 off_t off,
536 Stringpool* pool)
538 gold_assert(this->symtab_shndx_ != -1U);
539 if (this->symtab_shndx_ == 0)
541 // This object has no symbols. Weird but legal.
542 return index;
545 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
547 this->local_symbol_offset_ = off;
549 // Read the symbol table section header.
550 const unsigned int symtab_shndx = this->symtab_shndx_;
551 typename This::Shdr symtabshdr(this,
552 this->elf_file_.section_header(symtab_shndx));
553 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
555 // Read the local symbols.
556 const int sym_size = This::sym_size;
557 const unsigned int loccount = this->local_symbol_count_;
558 gold_assert(loccount == symtabshdr.get_sh_info());
559 off_t locsize = loccount * sym_size;
560 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
561 locsize, true);
563 this->local_values_.resize(loccount);
565 // Read the symbol names.
566 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
567 off_t strtab_size;
568 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
569 &strtab_size,
570 true);
571 const char* pnames = reinterpret_cast<const char*>(pnamesu);
573 // Loop over the local symbols.
575 const std::vector<Map_to_output>& mo(this->map_to_output());
576 unsigned int shnum = this->shnum();
577 unsigned int count = 0;
578 // Skip the first, dummy, symbol.
579 psyms += sym_size;
580 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
582 elfcpp::Sym<size, big_endian> sym(psyms);
584 Symbol_value<size>& lv(this->local_values_[i]);
586 unsigned int shndx = sym.get_st_shndx();
587 lv.set_input_shndx(shndx);
589 if (shndx >= elfcpp::SHN_LORESERVE)
591 if (shndx == elfcpp::SHN_ABS)
592 lv.set_output_value(sym.get_st_value());
593 else
595 // FIXME: Handle SHN_XINDEX.
596 fprintf(stderr,
597 _("%s: %s: unknown section index %u "
598 "for local symbol %u\n"),
599 program_name, this->name().c_str(), shndx, i);
600 gold_exit(false);
603 else
605 if (shndx >= shnum)
607 fprintf(stderr,
608 _("%s: %s: local symbol %u section index %u "
609 "out of range\n"),
610 program_name, this->name().c_str(), i, shndx);
611 gold_exit(false);
614 Output_section* os = mo[shndx].output_section;
616 if (os == NULL)
618 lv.set_output_value(0);
619 lv.set_no_output_symtab_entry();
620 continue;
623 if (mo[shndx].offset == -1)
624 lv.set_input_value(sym.get_st_value());
625 else
626 lv.set_output_value(mo[shndx].output_section->address()
627 + mo[shndx].offset
628 + sym.get_st_value());
631 // Decide whether this symbol should go into the output file.
633 if (sym.get_st_type() == elfcpp::STT_SECTION)
635 lv.set_no_output_symtab_entry();
636 continue;
639 if (sym.get_st_name() >= strtab_size)
641 fprintf(stderr,
642 _("%s: %s: local symbol %u section name "
643 "out of range: %u >= %u\n"),
644 program_name, this->name().c_str(),
645 i, sym.get_st_name(),
646 static_cast<unsigned int>(strtab_size));
647 gold_exit(false);
650 const char* name = pnames + sym.get_st_name();
651 pool->add(name, NULL);
652 lv.set_output_symtab_index(index);
653 ++index;
654 ++count;
657 this->output_local_symbol_count_ = count;
659 return index;
662 // Return the value of a local symbol defined in input section SHNDX,
663 // with value VALUE, adding addend ADDEND. This handles SHF_MERGE
664 // sections.
665 template<int size, bool big_endian>
666 typename elfcpp::Elf_types<size>::Elf_Addr
667 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
668 Address value,
669 Address addend) const
671 const std::vector<Map_to_output>& mo(this->map_to_output());
672 Output_section* os = mo[shndx].output_section;
673 if (os == NULL)
674 return addend;
675 gold_assert(mo[shndx].offset == -1);
676 return os->output_address(this, shndx, value + addend);
679 // Write out the local symbols.
681 template<int size, bool big_endian>
682 void
683 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
684 const Stringpool* sympool)
686 gold_assert(this->symtab_shndx_ != -1U);
687 if (this->symtab_shndx_ == 0)
689 // This object has no symbols. Weird but legal.
690 return;
693 // Read the symbol table section header.
694 const unsigned int symtab_shndx = this->symtab_shndx_;
695 typename This::Shdr symtabshdr(this,
696 this->elf_file_.section_header(symtab_shndx));
697 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
698 const unsigned int loccount = this->local_symbol_count_;
699 gold_assert(loccount == symtabshdr.get_sh_info());
701 // Read the local symbols.
702 const int sym_size = This::sym_size;
703 off_t locsize = loccount * sym_size;
704 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
705 locsize, false);
707 // Read the symbol names.
708 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
709 off_t strtab_size;
710 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
711 &strtab_size,
712 true);
713 const char* pnames = reinterpret_cast<const char*>(pnamesu);
715 // Get a view into the output file.
716 off_t output_size = this->output_local_symbol_count_ * sym_size;
717 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
718 output_size);
720 const std::vector<Map_to_output>& mo(this->map_to_output());
722 gold_assert(this->local_values_.size() == loccount);
724 unsigned char* ov = oview;
725 psyms += sym_size;
726 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
728 elfcpp::Sym<size, big_endian> isym(psyms);
730 if (!this->local_values_[i].needs_output_symtab_entry())
731 continue;
733 unsigned int st_shndx = isym.get_st_shndx();
734 if (st_shndx < elfcpp::SHN_LORESERVE)
736 gold_assert(st_shndx < mo.size());
737 if (mo[st_shndx].output_section == NULL)
738 continue;
739 st_shndx = mo[st_shndx].output_section->out_shndx();
742 elfcpp::Sym_write<size, big_endian> osym(ov);
744 gold_assert(isym.get_st_name() < strtab_size);
745 const char* name = pnames + isym.get_st_name();
746 osym.put_st_name(sympool->get_offset(name));
747 osym.put_st_value(this->local_values_[i].value(this, 0));
748 osym.put_st_size(isym.get_st_size());
749 osym.put_st_info(isym.get_st_info());
750 osym.put_st_other(isym.get_st_other());
751 osym.put_st_shndx(st_shndx);
753 ov += sym_size;
756 gold_assert(ov - oview == output_size);
758 of->write_output_view(this->local_symbol_offset_, output_size, oview);
761 // Input_objects methods.
763 // Add a regular relocatable object to the list. Return false if this
764 // object should be ignored.
766 bool
767 Input_objects::add_object(Object* obj)
769 if (!obj->is_dynamic())
770 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
771 else
773 // See if this is a duplicate SONAME.
774 Dynobj* dynobj = static_cast<Dynobj*>(obj);
776 std::pair<Unordered_set<std::string>::iterator, bool> ins =
777 this->sonames_.insert(dynobj->soname());
778 if (!ins.second)
780 // We have already seen a dynamic object with this soname.
781 return false;
784 this->dynobj_list_.push_back(dynobj);
787 Target* target = obj->target();
788 if (this->target_ == NULL)
789 this->target_ = target;
790 else if (this->target_ != target)
792 fprintf(stderr, "%s: %s: incompatible target\n",
793 program_name, obj->name().c_str());
794 gold_exit(false);
797 set_parameters_size_and_endianness(target->get_size(),
798 target->is_big_endian());
800 return true;
803 // Relocate_info methods.
805 // Return a string describing the location of a relocation. This is
806 // only used in error messages.
808 template<int size, bool big_endian>
809 std::string
810 Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
812 std::string ret(this->object->name());
813 ret += ": reloc ";
814 char buf[100];
815 snprintf(buf, sizeof buf, "%zu", relnum);
816 ret += buf;
817 ret += " in reloc section ";
818 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
819 ret += buf;
820 ret += " (" + this->object->section_name(this->reloc_shndx);
821 ret += ") for section ";
822 snprintf(buf, sizeof buf, "%u", this->data_shndx);
823 ret += buf;
824 ret += " (" + this->object->section_name(this->data_shndx) + ")";
825 return ret;
828 } // End namespace gold.
830 namespace
833 using namespace gold;
835 // Read an ELF file with the header and return the appropriate
836 // instance of Object.
838 template<int size, bool big_endian>
839 Object*
840 make_elf_sized_object(const std::string& name, Input_file* input_file,
841 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
843 int et = ehdr.get_e_type();
844 if (et == elfcpp::ET_REL)
846 Sized_relobj<size, big_endian>* obj =
847 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
848 obj->setup(ehdr);
849 return obj;
851 else if (et == elfcpp::ET_DYN)
853 Sized_dynobj<size, big_endian>* obj =
854 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
855 obj->setup(ehdr);
856 return obj;
858 else
860 fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
861 program_name, name.c_str(), et);
862 gold_exit(false);
866 } // End anonymous namespace.
868 namespace gold
871 // Read an ELF file and return the appropriate instance of Object.
873 Object*
874 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
875 const unsigned char* p, off_t bytes)
877 if (bytes < elfcpp::EI_NIDENT)
879 fprintf(stderr, _("%s: %s: ELF file too short\n"),
880 program_name, name.c_str());
881 gold_exit(false);
884 int v = p[elfcpp::EI_VERSION];
885 if (v != elfcpp::EV_CURRENT)
887 if (v == elfcpp::EV_NONE)
888 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
889 program_name, name.c_str());
890 else
891 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
892 program_name, name.c_str(), v);
893 gold_exit(false);
896 int c = p[elfcpp::EI_CLASS];
897 if (c == elfcpp::ELFCLASSNONE)
899 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
900 program_name, name.c_str());
901 gold_exit(false);
903 else if (c != elfcpp::ELFCLASS32
904 && c != elfcpp::ELFCLASS64)
906 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
907 program_name, name.c_str(), c);
908 gold_exit(false);
911 int d = p[elfcpp::EI_DATA];
912 if (d == elfcpp::ELFDATANONE)
914 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
915 program_name, name.c_str());
916 gold_exit(false);
918 else if (d != elfcpp::ELFDATA2LSB
919 && d != elfcpp::ELFDATA2MSB)
921 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
922 program_name, name.c_str(), d);
923 gold_exit(false);
926 bool big_endian = d == elfcpp::ELFDATA2MSB;
928 if (c == elfcpp::ELFCLASS32)
930 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
932 fprintf(stderr, _("%s: %s: ELF file too short\n"),
933 program_name, name.c_str());
934 gold_exit(false);
936 if (big_endian)
938 #ifdef HAVE_TARGET_32_BIG
939 elfcpp::Ehdr<32, true> ehdr(p);
940 return make_elf_sized_object<32, true>(name, input_file,
941 offset, ehdr);
942 #else
943 fprintf(stderr,
944 _("%s: %s: not configured to support 32-bit big-endian object\n"),
945 program_name, name.c_str());
946 gold_exit(false);
947 #endif
949 else
951 #ifdef HAVE_TARGET_32_LITTLE
952 elfcpp::Ehdr<32, false> ehdr(p);
953 return make_elf_sized_object<32, false>(name, input_file,
954 offset, ehdr);
955 #else
956 fprintf(stderr,
957 _("%s: %s: not configured to support 32-bit little-endian object\n"),
958 program_name, name.c_str());
959 gold_exit(false);
960 #endif
963 else
965 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
967 fprintf(stderr, _("%s: %s: ELF file too short\n"),
968 program_name, name.c_str());
969 gold_exit(false);
971 if (big_endian)
973 #ifdef HAVE_TARGET_64_BIG
974 elfcpp::Ehdr<64, true> ehdr(p);
975 return make_elf_sized_object<64, true>(name, input_file,
976 offset, ehdr);
977 #else
978 fprintf(stderr,
979 _("%s: %s: not configured to support 64-bit big-endian object\n"),
980 program_name, name.c_str());
981 gold_exit(false);
982 #endif
984 else
986 #ifdef HAVE_TARGET_64_LITTLE
987 elfcpp::Ehdr<64, false> ehdr(p);
988 return make_elf_sized_object<64, false>(name, input_file,
989 offset, ehdr);
990 #else
991 fprintf(stderr,
992 _("%s: %s: not configured to support 64-bit little-endian object\n"),
993 program_name, name.c_str());
994 gold_exit(false);
995 #endif
1000 // Instantiate the templates we need. We could use the configure
1001 // script to restrict this to only the ones for implemented targets.
1003 #ifdef HAVE_TARGET_32_LITTLE
1004 template
1005 class Sized_relobj<32, false>;
1006 #endif
1008 #ifdef HAVE_TARGET_32_BIG
1009 template
1010 class Sized_relobj<32, true>;
1011 #endif
1013 #ifdef HAVE_TARGET_64_LITTLE
1014 template
1015 class Sized_relobj<64, false>;
1016 #endif
1018 #ifdef HAVE_TARGET_64_BIG
1019 template
1020 class Sized_relobj<64, true>;
1021 #endif
1023 #ifdef HAVE_TARGET_32_LITTLE
1024 template
1025 struct Relocate_info<32, false>;
1026 #endif
1028 #ifdef HAVE_TARGET_32_BIG
1029 template
1030 struct Relocate_info<32, true>;
1031 #endif
1033 #ifdef HAVE_TARGET_64_LITTLE
1034 template
1035 struct Relocate_info<64, false>;
1036 #endif
1038 #ifdef HAVE_TARGET_64_BIG
1039 template
1040 struct Relocate_info<64, true>;
1041 #endif
1043 } // End namespace gold.