1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008 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.
29 #include "libiberty.h"
31 #include "target-select.h"
32 #include "dwarf_reader.h"
45 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
46 // section and read it in. SYMTAB_SHNDX is the index of the symbol
47 // table we care about.
49 template<int size, bool big_endian>
51 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
53 if (!this->symtab_xindex_.empty())
56 gold_assert(symtab_shndx != 0);
58 // Look through the sections in reverse order, on the theory that it
59 // is more likely to be near the end than the beginning.
60 unsigned int i = object->shnum();
64 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
65 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
67 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
72 object->error(_("missing SHT_SYMTAB_SHNDX section"));
75 // Read in the symtab_xindex_ array, given the section index of the
76 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
79 template<int size, bool big_endian>
81 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
82 const unsigned char* pshdrs)
84 section_size_type bytecount;
85 const unsigned char* contents;
87 contents = object->section_contents(xindex_shndx, &bytecount, false);
90 const unsigned char* p = (pshdrs
92 * elfcpp::Elf_sizes<size>::shdr_size));
93 typename elfcpp::Shdr<size, big_endian> shdr(p);
94 bytecount = convert_to_section_size_type(shdr.get_sh_size());
95 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
98 gold_assert(this->symtab_xindex_.empty());
99 this->symtab_xindex_.reserve(bytecount / 4);
100 for (section_size_type i = 0; i < bytecount; i += 4)
102 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
103 // We preadjust the section indexes we save.
104 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
108 // Symbol symndx has a section of SHN_XINDEX; return the real section
112 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
114 if (symndx >= this->symtab_xindex_.size())
116 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
118 return elfcpp::SHN_UNDEF;
120 unsigned int shndx = this->symtab_xindex_[symndx];
121 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
123 object->error(_("extended index for symbol %u out of range: %u"),
125 return elfcpp::SHN_UNDEF;
132 // Set the target based on fields in the ELF file header.
135 Object::set_target(int machine, int size, bool big_endian, int osabi,
138 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
140 gold_fatal(_("%s: unsupported ELF machine number %d"),
141 this->name().c_str(), machine);
142 this->target_ = target;
145 // Report an error for this object file. This is used by the
146 // elfcpp::Elf_file interface, and also called by the Object code
150 Object::error(const char* format, ...) const
153 va_start(args, format);
155 if (vasprintf(&buf, format, args) < 0)
158 gold_error(_("%s: %s"), this->name().c_str(), buf);
162 // Return a view of the contents of a section.
165 Object::section_contents(unsigned int shndx, section_size_type* plen,
168 Location loc(this->do_section_contents(shndx));
169 *plen = convert_to_section_size_type(loc.data_size);
170 return this->get_view(loc.file_offset, *plen, true, cache);
173 // Read the section data into SD. This is code common to Sized_relobj
174 // and Sized_dynobj, so we put it into Object.
176 template<int size, bool big_endian>
178 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
179 Read_symbols_data* sd)
181 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
183 // Read the section headers.
184 const off_t shoff = elf_file->shoff();
185 const unsigned int shnum = this->shnum();
186 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
189 // Read the section names.
190 const unsigned char* pshdrs = sd->section_headers->data();
191 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
192 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
194 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
195 this->error(_("section name section has wrong type: %u"),
196 static_cast<unsigned int>(shdrnames.get_sh_type()));
198 sd->section_names_size =
199 convert_to_section_size_type(shdrnames.get_sh_size());
200 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
201 sd->section_names_size, false,
205 // If NAME is the name of a special .gnu.warning section, arrange for
206 // the warning to be issued. SHNDX is the section index. Return
207 // whether it is a warning section.
210 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
211 Symbol_table* symtab)
213 const char warn_prefix[] = ".gnu.warning.";
214 const int warn_prefix_len = sizeof warn_prefix - 1;
215 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
217 // Read the section contents to get the warning text. It would
218 // be nicer if we only did this if we have to actually issue a
219 // warning. Unfortunately, warnings are issued as we relocate
220 // sections. That means that we can not lock the object then,
221 // as we might try to issue the same warning multiple times
223 section_size_type len;
224 const unsigned char* contents = this->section_contents(shndx, &len,
226 std::string warning(reinterpret_cast<const char*>(contents), len);
227 symtab->add_warning(name + warn_prefix_len, this, warning);
235 // Return the output address of the input section SHNDX.
237 Relobj::output_section_address(unsigned int shndx) const
239 section_offset_type offset;
240 Output_section* os = this->output_section(shndx, &offset);
241 gold_assert(os != NULL && offset != -1);
242 return os->address() + offset;
245 // Class Sized_relobj.
247 template<int size, bool big_endian>
248 Sized_relobj<size, big_endian>::Sized_relobj(
249 const std::string& name,
250 Input_file* input_file,
252 const elfcpp::Ehdr<size, big_endian>& ehdr)
253 : Relobj(name, input_file, offset),
254 elf_file_(this, ehdr),
256 local_symbol_count_(0),
257 output_local_symbol_count_(0),
258 output_local_dynsym_count_(0),
260 local_symbol_offset_(0),
261 local_dynsym_offset_(0),
263 local_got_offsets_(),
268 template<int size, bool big_endian>
269 Sized_relobj<size, big_endian>::~Sized_relobj()
273 // Set up an object file based on the file header. This sets up the
274 // target and reads the section information.
276 template<int size, bool big_endian>
278 Sized_relobj<size, big_endian>::setup(
279 const elfcpp::Ehdr<size, big_endian>& ehdr)
281 this->set_target(ehdr.get_e_machine(), size, big_endian,
282 ehdr.get_e_ident()[elfcpp::EI_OSABI],
283 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
285 const unsigned int shnum = this->elf_file_.shnum();
286 this->set_shnum(shnum);
289 // Find the SHT_SYMTAB section, given the section headers. The ELF
290 // standard says that maybe in the future there can be more than one
291 // SHT_SYMTAB section. Until somebody figures out how that could
292 // work, we assume there is only one.
294 template<int size, bool big_endian>
296 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
298 const unsigned int shnum = this->shnum();
299 this->symtab_shndx_ = 0;
302 // Look through the sections in reverse order, since gas tends
303 // to put the symbol table at the end.
304 const unsigned char* p = pshdrs + shnum * This::shdr_size;
305 unsigned int i = shnum;
306 unsigned int xindex_shndx = 0;
307 unsigned int xindex_link = 0;
311 p -= This::shdr_size;
312 typename This::Shdr shdr(p);
313 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
315 this->symtab_shndx_ = i;
316 if (xindex_shndx > 0 && xindex_link == i)
319 new Xindex(this->elf_file_.large_shndx_offset());
320 xindex->read_symtab_xindex<size, big_endian>(this,
323 this->set_xindex(xindex);
328 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
329 // one. This will work if it follows the SHT_SYMTAB
331 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
334 xindex_link = this->adjust_shndx(shdr.get_sh_link());
340 // Return the Xindex structure to use for object with lots of
343 template<int size, bool big_endian>
345 Sized_relobj<size, big_endian>::do_initialize_xindex()
347 gold_assert(this->symtab_shndx_ != -1U);
348 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
349 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
353 // Return whether SHDR has the right type and flags to be a GNU
354 // .eh_frame section.
356 template<int size, bool big_endian>
358 Sized_relobj<size, big_endian>::check_eh_frame_flags(
359 const elfcpp::Shdr<size, big_endian>* shdr) const
361 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
362 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
365 // Return whether there is a GNU .eh_frame section, given the section
366 // headers and the section names.
368 template<int size, bool big_endian>
370 Sized_relobj<size, big_endian>::find_eh_frame(
371 const unsigned char* pshdrs,
373 section_size_type names_size) const
375 const unsigned int shnum = this->shnum();
376 const unsigned char* p = pshdrs + This::shdr_size;
377 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
379 typename This::Shdr shdr(p);
380 if (this->check_eh_frame_flags(&shdr))
382 if (shdr.get_sh_name() >= names_size)
384 this->error(_("bad section name offset for section %u: %lu"),
385 i, static_cast<unsigned long>(shdr.get_sh_name()));
389 const char* name = names + shdr.get_sh_name();
390 if (strcmp(name, ".eh_frame") == 0)
397 // Read the sections and symbols from an object file.
399 template<int size, bool big_endian>
401 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
403 this->read_section_data(&this->elf_file_, sd);
405 const unsigned char* const pshdrs = sd->section_headers->data();
407 this->find_symtab(pshdrs);
409 const unsigned char* namesu = sd->section_names->data();
410 const char* names = reinterpret_cast<const char*>(namesu);
411 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
413 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
414 this->has_eh_frame_ = true;
418 sd->symbols_size = 0;
419 sd->external_symbols_offset = 0;
420 sd->symbol_names = NULL;
421 sd->symbol_names_size = 0;
423 if (this->symtab_shndx_ == 0)
425 // No symbol table. Weird but legal.
429 // Get the symbol table section header.
430 typename This::Shdr symtabshdr(pshdrs
431 + this->symtab_shndx_ * This::shdr_size);
432 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
434 // If this object has a .eh_frame section, we need all the symbols.
435 // Otherwise we only need the external symbols. While it would be
436 // simpler to just always read all the symbols, I've seen object
437 // files with well over 2000 local symbols, which for a 64-bit
438 // object file format is over 5 pages that we don't need to read
441 const int sym_size = This::sym_size;
442 const unsigned int loccount = symtabshdr.get_sh_info();
443 this->local_symbol_count_ = loccount;
444 this->local_values_.resize(loccount);
445 section_offset_type locsize = loccount * sym_size;
446 off_t dataoff = symtabshdr.get_sh_offset();
447 section_size_type datasize =
448 convert_to_section_size_type(symtabshdr.get_sh_size());
449 off_t extoff = dataoff + locsize;
450 section_size_type extsize = datasize - locsize;
452 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
453 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
457 // No external symbols. Also weird but also legal.
461 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
463 // Read the section header for the symbol names.
464 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
465 if (strtab_shndx >= this->shnum())
467 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
470 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
471 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
473 this->error(_("symbol table name section has wrong type: %u"),
474 static_cast<unsigned int>(strtabshdr.get_sh_type()));
478 // Read the symbol names.
479 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
480 strtabshdr.get_sh_size(),
483 sd->symbols = fvsymtab;
484 sd->symbols_size = readsize;
485 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
486 sd->symbol_names = fvstrtab;
487 sd->symbol_names_size =
488 convert_to_section_size_type(strtabshdr.get_sh_size());
491 // Return the section index of symbol SYM. Set *VALUE to its value in
492 // the object file. Set *IS_ORDINARY if this is an ordinary section
493 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
494 // Note that for a symbol which is not defined in this object file,
495 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
496 // the final value of the symbol in the link.
498 template<int size, bool big_endian>
500 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
504 section_size_type symbols_size;
505 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
509 const size_t count = symbols_size / This::sym_size;
510 gold_assert(sym < count);
512 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
513 *value = elfsym.get_st_value();
515 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
518 // Return whether to include a section group in the link. LAYOUT is
519 // used to keep track of which section groups we have already seen.
520 // INDEX is the index of the section group and SHDR is the section
521 // header. If we do not want to include this group, we set bits in
522 // OMIT for each section which should be discarded.
524 template<int size, bool big_endian>
526 Sized_relobj<size, big_endian>::include_section_group(
527 Symbol_table* symtab,
531 const unsigned char* shdrs,
532 const char* section_names,
533 section_size_type section_names_size,
534 std::vector<bool>* omit)
536 // Read the section contents.
537 typename This::Shdr shdr(shdrs + index * This::shdr_size);
538 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
539 shdr.get_sh_size(), true, false);
540 const elfcpp::Elf_Word* pword =
541 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
543 // The first word contains flags. We only care about COMDAT section
544 // groups. Other section groups are always included in the link
545 // just like ordinary sections.
546 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
548 // Look up the group signature, which is the name of a symbol. This
549 // is a lot of effort to go to to read a string. Why didn't they
550 // just have the group signature point into the string table, rather
551 // than indirect through a symbol?
553 // Get the appropriate symbol table header (this will normally be
554 // the single SHT_SYMTAB section, but in principle it need not be).
555 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
556 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
558 // Read the symbol table entry.
559 unsigned int symndx = shdr.get_sh_info();
560 if (symndx >= symshdr.get_sh_size() / This::sym_size)
562 this->error(_("section group %u info %u out of range"),
566 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
567 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
569 elfcpp::Sym<size, big_endian> sym(psym);
571 // Read the symbol table names.
572 section_size_type symnamelen;
573 const unsigned char* psymnamesu;
574 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
576 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
578 // Get the section group signature.
579 if (sym.get_st_name() >= symnamelen)
581 this->error(_("symbol %u name offset %u out of range"),
582 symndx, sym.get_st_name());
586 std::string signature(psymnames + sym.get_st_name());
588 // It seems that some versions of gas will create a section group
589 // associated with a section symbol, and then fail to give a name to
590 // the section symbol. In such a case, use the name of the section.
591 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
594 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
597 if (!is_ordinary || sym_shndx >= this->shnum())
599 this->error(_("symbol %u invalid section index %u"),
603 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
604 if (member_shdr.get_sh_name() < section_names_size)
605 signature = section_names + member_shdr.get_sh_name();
608 // Record this section group in the layout, and see whether we've already
609 // seen one with the same signature.
610 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
611 || layout->add_comdat(this, index, signature, true));
613 Relobj* kept_object = NULL;
614 Comdat_group* kept_group = NULL;
618 // This group is being discarded. Find the object and group
619 // that was kept in its place.
620 unsigned int kept_group_index = 0;
621 kept_object = layout->find_kept_object(signature, &kept_group_index);
622 if (kept_object != NULL)
623 kept_group = kept_object->find_comdat_group(kept_group_index);
625 else if (flags & elfcpp::GRP_COMDAT)
627 // This group is being kept. Create the table to map section names
628 // to section indexes and add it to the table of groups.
629 kept_group = new Comdat_group();
630 this->add_comdat_group(index, kept_group);
633 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
635 std::vector<unsigned int> shndxes;
636 bool relocate_group = include_group && parameters->options().relocatable();
638 shndxes.reserve(count - 1);
640 for (size_t i = 1; i < count; ++i)
642 elfcpp::Elf_Word secnum =
643 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
646 shndxes.push_back(secnum);
648 if (secnum >= this->shnum())
650 this->error(_("section %u in section group %u out of range"),
655 // Check for an earlier section number, since we're going to get
656 // it wrong--we may have already decided to include the section.
658 this->error(_("invalid section group %u refers to earlier section %u"),
661 // Get the name of the member section.
662 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
663 if (member_shdr.get_sh_name() >= section_names_size)
665 // This is an error, but it will be diagnosed eventually
666 // in do_layout, so we don't need to do anything here but
670 std::string mname(section_names + member_shdr.get_sh_name());
674 (*omit)[secnum] = true;
675 if (kept_group != NULL)
677 // Find the corresponding kept section, and store that info
678 // in the discarded section table.
679 Comdat_group::const_iterator p = kept_group->find(mname);
680 if (p != kept_group->end())
682 Kept_comdat_section* kept =
683 new Kept_comdat_section(kept_object, p->second);
684 this->set_kept_comdat_section(secnum, kept);
688 else if (flags & elfcpp::GRP_COMDAT)
690 // Add the section to the kept group table.
691 gold_assert(kept_group != NULL);
692 kept_group->insert(std::make_pair(mname, secnum));
697 layout->layout_group(symtab, this, index, name, signature.c_str(),
698 shdr, flags, &shndxes);
700 return include_group;
703 // Whether to include a linkonce section in the link. NAME is the
704 // name of the section and SHDR is the section header.
706 // Linkonce sections are a GNU extension implemented in the original
707 // GNU linker before section groups were defined. The semantics are
708 // that we only include one linkonce section with a given name. The
709 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
710 // where T is the type of section and SYMNAME is the name of a symbol.
711 // In an attempt to make linkonce sections interact well with section
712 // groups, we try to identify SYMNAME and use it like a section group
713 // signature. We want to block section groups with that signature,
714 // but not other linkonce sections with that signature. We also use
715 // the full name of the linkonce section as a normal section group
718 template<int size, bool big_endian>
720 Sized_relobj<size, big_endian>::include_linkonce_section(
724 const elfcpp::Shdr<size, big_endian>&)
726 // In general the symbol name we want will be the string following
727 // the last '.'. However, we have to handle the case of
728 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
729 // some versions of gcc. So we use a heuristic: if the name starts
730 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
731 // we look for the last '.'. We can't always simply skip
732 // ".gnu.linkonce.X", because we have to deal with cases like
733 // ".gnu.linkonce.d.rel.ro.local".
734 const char* const linkonce_t = ".gnu.linkonce.t.";
736 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
737 symname = name + strlen(linkonce_t);
739 symname = strrchr(name, '.') + 1;
740 std::string sig1(symname);
741 std::string sig2(name);
742 bool include1 = layout->add_comdat(this, index, sig1, false);
743 bool include2 = layout->add_comdat(this, index, sig2, true);
747 // The section is being discarded on the basis of its section
748 // name (i.e., the kept section was also a linkonce section).
749 // In this case, the section index stored with the layout object
750 // is the linkonce section that was kept.
751 unsigned int kept_group_index = 0;
752 Relobj* kept_object = layout->find_kept_object(sig2, &kept_group_index);
753 if (kept_object != NULL)
755 Kept_comdat_section* kept =
756 new Kept_comdat_section(kept_object, kept_group_index);
757 this->set_kept_comdat_section(index, kept);
762 // The section is being discarded on the basis of its symbol
763 // name. This means that the corresponding kept section was
764 // part of a comdat group, and it will be difficult to identify
765 // the specific section within that group that corresponds to
766 // this linkonce section. We'll handle the simple case where
767 // the group has only one member section. Otherwise, it's not
769 unsigned int kept_group_index = 0;
770 Relobj* kept_object = layout->find_kept_object(sig1, &kept_group_index);
771 if (kept_object != NULL)
773 Comdat_group* kept_group =
774 kept_object->find_comdat_group(kept_group_index);
775 if (kept_group != NULL && kept_group->size() == 1)
777 Comdat_group::const_iterator p = kept_group->begin();
778 gold_assert(p != kept_group->end());
779 Kept_comdat_section* kept =
780 new Kept_comdat_section(kept_object, p->second);
781 this->set_kept_comdat_section(index, kept);
786 return include1 && include2;
789 // Lay out the input sections. We walk through the sections and check
790 // whether they should be included in the link. If they should, we
791 // pass them to the Layout object, which will return an output section
794 template<int size, bool big_endian>
796 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
798 Read_symbols_data* sd)
800 const unsigned int shnum = this->shnum();
804 // Get the section headers.
805 const unsigned char* shdrs = sd->section_headers->data();
806 const unsigned char* pshdrs;
808 // Get the section names.
809 const unsigned char* pnamesu = sd->section_names->data();
810 const char* pnames = reinterpret_cast<const char*>(pnamesu);
812 // For each section, record the index of the reloc section if any.
813 // Use 0 to mean that there is no reloc section, -1U to mean that
814 // there is more than one.
815 std::vector<unsigned int> reloc_shndx(shnum, 0);
816 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
817 // Skip the first, dummy, section.
818 pshdrs = shdrs + This::shdr_size;
819 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
821 typename This::Shdr shdr(pshdrs);
823 unsigned int sh_type = shdr.get_sh_type();
824 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
826 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
827 if (target_shndx == 0 || target_shndx >= shnum)
829 this->error(_("relocation section %u has bad info %u"),
834 if (reloc_shndx[target_shndx] != 0)
835 reloc_shndx[target_shndx] = -1U;
838 reloc_shndx[target_shndx] = i;
839 reloc_type[target_shndx] = sh_type;
844 std::vector<Map_to_output>& map_sections(this->map_to_output());
845 map_sections.resize(shnum);
847 // If we are only linking for symbols, then there is nothing else to
849 if (this->input_file()->just_symbols())
851 delete sd->section_headers;
852 sd->section_headers = NULL;
853 delete sd->section_names;
854 sd->section_names = NULL;
858 // Whether we've seen a .note.GNU-stack section.
859 bool seen_gnu_stack = false;
860 // The flags of a .note.GNU-stack section.
861 uint64_t gnu_stack_flags = 0;
863 // Keep track of which sections to omit.
864 std::vector<bool> omit(shnum, false);
866 // Keep track of reloc sections when emitting relocations.
867 const bool relocatable = parameters->options().relocatable();
868 const bool emit_relocs = (relocatable
869 || parameters->options().emit_relocs());
870 std::vector<unsigned int> reloc_sections;
872 // Keep track of .eh_frame sections.
873 std::vector<unsigned int> eh_frame_sections;
875 // Skip the first, dummy, section.
876 pshdrs = shdrs + This::shdr_size;
877 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
879 typename This::Shdr shdr(pshdrs);
881 if (shdr.get_sh_name() >= sd->section_names_size)
883 this->error(_("bad section name offset for section %u: %lu"),
884 i, static_cast<unsigned long>(shdr.get_sh_name()));
888 const char* name = pnames + shdr.get_sh_name();
890 if (this->handle_gnu_warning_section(name, i, symtab))
896 // The .note.GNU-stack section is special. It gives the
897 // protection flags that this object file requires for the stack
899 if (strcmp(name, ".note.GNU-stack") == 0)
901 seen_gnu_stack = true;
902 gnu_stack_flags |= shdr.get_sh_flags();
906 bool discard = omit[i];
909 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
911 if (!this->include_section_group(symtab, layout, i, name, shdrs,
912 pnames, sd->section_names_size,
916 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
917 && Layout::is_linkonce(name))
919 if (!this->include_linkonce_section(layout, i, name, shdr))
926 // Do not include this section in the link.
927 map_sections[i].output_section = NULL;
931 // When doing a relocatable link we are going to copy input
932 // reloc sections into the output. We only want to copy the
933 // ones associated with sections which are not being discarded.
934 // However, we don't know that yet for all sections. So save
935 // reloc sections and process them later.
937 && (shdr.get_sh_type() == elfcpp::SHT_REL
938 || shdr.get_sh_type() == elfcpp::SHT_RELA))
940 reloc_sections.push_back(i);
944 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
947 // The .eh_frame section is special. It holds exception frame
948 // information that we need to read in order to generate the
949 // exception frame header. We process these after all the other
950 // sections so that the exception frame reader can reliably
951 // determine which sections are being discarded, and discard the
952 // corresponding information.
954 && strcmp(name, ".eh_frame") == 0
955 && this->check_eh_frame_flags(&shdr))
957 eh_frame_sections.push_back(i);
962 Output_section* os = layout->layout(this, i, name, shdr,
963 reloc_shndx[i], reloc_type[i],
966 map_sections[i].output_section = os;
967 map_sections[i].offset = offset;
969 // If this section requires special handling, and if there are
970 // relocs that apply to it, then we must do the special handling
971 // before we apply the relocs.
972 if (offset == -1 && reloc_shndx[i] != 0)
973 this->set_relocs_must_follow_section_writes();
976 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
978 // When doing a relocatable link handle the reloc sections at the
981 this->size_relocatable_relocs();
982 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
983 p != reloc_sections.end();
987 const unsigned char* pshdr;
988 pshdr = sd->section_headers->data() + i * This::shdr_size;
989 typename This::Shdr shdr(pshdr);
991 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
992 if (data_shndx >= shnum)
994 // We already warned about this above.
998 Output_section* data_section = map_sections[data_shndx].output_section;
999 if (data_section == NULL)
1001 map_sections[i].output_section = NULL;
1005 Relocatable_relocs* rr = new Relocatable_relocs();
1006 this->set_relocatable_relocs(i, rr);
1008 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1010 map_sections[i].output_section = os;
1011 map_sections[i].offset = -1;
1014 // Handle the .eh_frame sections at the end.
1015 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1016 p != eh_frame_sections.end();
1019 gold_assert(this->has_eh_frame_);
1020 gold_assert(sd->external_symbols_offset != 0);
1022 unsigned int i = *p;
1023 const unsigned char *pshdr;
1024 pshdr = sd->section_headers->data() + i * This::shdr_size;
1025 typename This::Shdr shdr(pshdr);
1028 Output_section* os = layout->layout_eh_frame(this,
1029 sd->symbols->data(),
1031 sd->symbol_names->data(),
1032 sd->symbol_names_size,
1037 map_sections[i].output_section = os;
1038 map_sections[i].offset = offset;
1040 // If this section requires special handling, and if there are
1041 // relocs that apply to it, then we must do the special handling
1042 // before we apply the relocs.
1043 if (offset == -1 && reloc_shndx[i] != 0)
1044 this->set_relocs_must_follow_section_writes();
1047 delete sd->section_headers;
1048 sd->section_headers = NULL;
1049 delete sd->section_names;
1050 sd->section_names = NULL;
1053 // Add the symbols to the symbol table.
1055 template<int size, bool big_endian>
1057 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1058 Read_symbols_data* sd)
1060 if (sd->symbols == NULL)
1062 gold_assert(sd->symbol_names == NULL);
1066 const int sym_size = This::sym_size;
1067 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1069 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1071 this->error(_("size of symbols is not multiple of symbol size"));
1075 this->symbols_.resize(symcount);
1077 const char* sym_names =
1078 reinterpret_cast<const char*>(sd->symbol_names->data());
1079 symtab->add_from_relobj(this,
1080 sd->symbols->data() + sd->external_symbols_offset,
1081 symcount, this->local_symbol_count_,
1082 sym_names, sd->symbol_names_size,
1087 delete sd->symbol_names;
1088 sd->symbol_names = NULL;
1091 // First pass over the local symbols. Here we add their names to
1092 // *POOL and *DYNPOOL, and we store the symbol value in
1093 // THIS->LOCAL_VALUES_. This function is always called from a
1094 // singleton thread. This is followed by a call to
1095 // finalize_local_symbols.
1097 template<int size, bool big_endian>
1099 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1100 Stringpool* dynpool)
1102 gold_assert(this->symtab_shndx_ != -1U);
1103 if (this->symtab_shndx_ == 0)
1105 // This object has no symbols. Weird but legal.
1109 // Read the symbol table section header.
1110 const unsigned int symtab_shndx = this->symtab_shndx_;
1111 typename This::Shdr symtabshdr(this,
1112 this->elf_file_.section_header(symtab_shndx));
1113 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1115 // Read the local symbols.
1116 const int sym_size = This::sym_size;
1117 const unsigned int loccount = this->local_symbol_count_;
1118 gold_assert(loccount == symtabshdr.get_sh_info());
1119 off_t locsize = loccount * sym_size;
1120 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1121 locsize, true, true);
1123 // Read the symbol names.
1124 const unsigned int strtab_shndx =
1125 this->adjust_shndx(symtabshdr.get_sh_link());
1126 section_size_type strtab_size;
1127 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1130 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1132 // Loop over the local symbols.
1134 const std::vector<Map_to_output>& mo(this->map_to_output());
1135 unsigned int shnum = this->shnum();
1136 unsigned int count = 0;
1137 unsigned int dyncount = 0;
1138 // Skip the first, dummy, symbol.
1140 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1142 elfcpp::Sym<size, big_endian> sym(psyms);
1144 Symbol_value<size>& lv(this->local_values_[i]);
1147 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1149 lv.set_input_shndx(shndx, is_ordinary);
1151 if (sym.get_st_type() == elfcpp::STT_SECTION)
1152 lv.set_is_section_symbol();
1153 else if (sym.get_st_type() == elfcpp::STT_TLS)
1154 lv.set_is_tls_symbol();
1156 // Save the input symbol value for use in do_finalize_local_symbols().
1157 lv.set_input_value(sym.get_st_value());
1159 // Decide whether this symbol should go into the output file.
1161 if (shndx < shnum && mo[shndx].output_section == NULL)
1163 lv.set_no_output_symtab_entry();
1164 gold_assert(!lv.needs_output_dynsym_entry());
1168 if (sym.get_st_type() == elfcpp::STT_SECTION)
1170 lv.set_no_output_symtab_entry();
1171 gold_assert(!lv.needs_output_dynsym_entry());
1175 if (sym.get_st_name() >= strtab_size)
1177 this->error(_("local symbol %u section name out of range: %u >= %u"),
1178 i, sym.get_st_name(),
1179 static_cast<unsigned int>(strtab_size));
1180 lv.set_no_output_symtab_entry();
1184 // Add the symbol to the symbol table string pool.
1185 const char* name = pnames + sym.get_st_name();
1186 pool->add(name, true, NULL);
1189 // If needed, add the symbol to the dynamic symbol table string pool.
1190 if (lv.needs_output_dynsym_entry())
1192 dynpool->add(name, true, NULL);
1197 this->output_local_symbol_count_ = count;
1198 this->output_local_dynsym_count_ = dyncount;
1201 // Finalize the local symbols. Here we set the final value in
1202 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1203 // This function is always called from a singleton thread. The actual
1204 // output of the local symbols will occur in a separate task.
1206 template<int size, bool big_endian>
1208 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1211 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1213 const unsigned int loccount = this->local_symbol_count_;
1214 this->local_symbol_offset_ = off;
1216 const std::vector<Map_to_output>& mo(this->map_to_output());
1217 unsigned int shnum = this->shnum();
1219 for (unsigned int i = 1; i < loccount; ++i)
1221 Symbol_value<size>& lv(this->local_values_[i]);
1224 unsigned int shndx = lv.input_shndx(&is_ordinary);
1226 // Set the output symbol value.
1230 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1231 lv.set_output_value(lv.input_value());
1234 this->error(_("unknown section index %u for local symbol %u"),
1236 lv.set_output_value(0);
1243 this->error(_("local symbol %u section index %u out of range"),
1248 Output_section* os = mo[shndx].output_section;
1252 // This local symbol belongs to a section we are discarding.
1253 // In some cases when applying relocations later, we will
1254 // attempt to match it to the corresponding kept section,
1255 // so we leave the input value unchanged here.
1258 else if (mo[shndx].offset == -1)
1260 // This is a SHF_MERGE section or one which otherwise
1261 // requires special handling. We get the output address
1262 // of the start of the merged section. If this is not a
1263 // section symbol, we can then determine the final
1264 // value. If it is a section symbol, we can not, as in
1265 // that case we have to consider the addend to determine
1266 // the value to use in a relocation.
1267 if (!lv.is_section_symbol())
1268 lv.set_output_value(os->output_address(this, shndx,
1272 section_offset_type start =
1273 os->starting_output_address(this, shndx);
1274 Merged_symbol_value<size>* msv =
1275 new Merged_symbol_value<size>(lv.input_value(), start);
1276 lv.set_merged_symbol_value(msv);
1279 else if (lv.is_tls_symbol())
1280 lv.set_output_value(os->tls_offset()
1282 + lv.input_value());
1284 lv.set_output_value(os->address()
1286 + lv.input_value());
1289 if (lv.needs_output_symtab_entry())
1291 lv.set_output_symtab_index(index);
1298 // Set the output dynamic symbol table indexes for the local variables.
1300 template<int size, bool big_endian>
1302 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1304 const unsigned int loccount = this->local_symbol_count_;
1305 for (unsigned int i = 1; i < loccount; ++i)
1307 Symbol_value<size>& lv(this->local_values_[i]);
1308 if (lv.needs_output_dynsym_entry())
1310 lv.set_output_dynsym_index(index);
1317 // Set the offset where local dynamic symbol information will be stored.
1318 // Returns the count of local symbols contributed to the symbol table by
1321 template<int size, bool big_endian>
1323 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1325 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1326 this->local_dynsym_offset_ = off;
1327 return this->output_local_dynsym_count_;
1330 // Write out the local symbols.
1332 template<int size, bool big_endian>
1334 Sized_relobj<size, big_endian>::write_local_symbols(
1336 const Stringpool* sympool,
1337 const Stringpool* dynpool,
1338 Output_symtab_xindex* symtab_xindex,
1339 Output_symtab_xindex* dynsym_xindex)
1341 if (parameters->options().strip_all()
1342 && this->output_local_dynsym_count_ == 0)
1345 gold_assert(this->symtab_shndx_ != -1U);
1346 if (this->symtab_shndx_ == 0)
1348 // This object has no symbols. Weird but legal.
1352 // Read the symbol table section header.
1353 const unsigned int symtab_shndx = this->symtab_shndx_;
1354 typename This::Shdr symtabshdr(this,
1355 this->elf_file_.section_header(symtab_shndx));
1356 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1357 const unsigned int loccount = this->local_symbol_count_;
1358 gold_assert(loccount == symtabshdr.get_sh_info());
1360 // Read the local symbols.
1361 const int sym_size = This::sym_size;
1362 off_t locsize = loccount * sym_size;
1363 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1364 locsize, true, false);
1366 // Read the symbol names.
1367 const unsigned int strtab_shndx =
1368 this->adjust_shndx(symtabshdr.get_sh_link());
1369 section_size_type strtab_size;
1370 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1373 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1375 // Get views into the output file for the portions of the symbol table
1376 // and the dynamic symbol table that we will be writing.
1377 off_t output_size = this->output_local_symbol_count_ * sym_size;
1378 unsigned char* oview = NULL;
1379 if (output_size > 0)
1380 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1382 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1383 unsigned char* dyn_oview = NULL;
1384 if (dyn_output_size > 0)
1385 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1388 const std::vector<Map_to_output>& mo(this->map_to_output());
1390 gold_assert(this->local_values_.size() == loccount);
1392 unsigned char* ov = oview;
1393 unsigned char* dyn_ov = dyn_oview;
1395 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1397 elfcpp::Sym<size, big_endian> isym(psyms);
1399 Symbol_value<size>& lv(this->local_values_[i]);
1402 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1406 gold_assert(st_shndx < mo.size());
1407 if (mo[st_shndx].output_section == NULL)
1409 st_shndx = mo[st_shndx].output_section->out_shndx();
1410 if (st_shndx >= elfcpp::SHN_LORESERVE)
1412 if (lv.needs_output_symtab_entry())
1413 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1414 if (lv.needs_output_dynsym_entry())
1415 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1416 st_shndx = elfcpp::SHN_XINDEX;
1420 // Write the symbol to the output symbol table.
1421 if (!parameters->options().strip_all()
1422 && lv.needs_output_symtab_entry())
1424 elfcpp::Sym_write<size, big_endian> osym(ov);
1426 gold_assert(isym.get_st_name() < strtab_size);
1427 const char* name = pnames + isym.get_st_name();
1428 osym.put_st_name(sympool->get_offset(name));
1429 osym.put_st_value(this->local_values_[i].value(this, 0));
1430 osym.put_st_size(isym.get_st_size());
1431 osym.put_st_info(isym.get_st_info());
1432 osym.put_st_other(isym.get_st_other());
1433 osym.put_st_shndx(st_shndx);
1438 // Write the symbol to the output dynamic symbol table.
1439 if (lv.needs_output_dynsym_entry())
1441 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1442 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1444 gold_assert(isym.get_st_name() < strtab_size);
1445 const char* name = pnames + isym.get_st_name();
1446 osym.put_st_name(dynpool->get_offset(name));
1447 osym.put_st_value(this->local_values_[i].value(this, 0));
1448 osym.put_st_size(isym.get_st_size());
1449 osym.put_st_info(isym.get_st_info());
1450 osym.put_st_other(isym.get_st_other());
1451 osym.put_st_shndx(st_shndx);
1458 if (output_size > 0)
1460 gold_assert(ov - oview == output_size);
1461 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1464 if (dyn_output_size > 0)
1466 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1467 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1472 // Set *INFO to symbolic information about the offset OFFSET in the
1473 // section SHNDX. Return true if we found something, false if we
1476 template<int size, bool big_endian>
1478 Sized_relobj<size, big_endian>::get_symbol_location_info(
1481 Symbol_location_info* info)
1483 if (this->symtab_shndx_ == 0)
1486 section_size_type symbols_size;
1487 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1491 unsigned int symbol_names_shndx =
1492 this->adjust_shndx(this->section_link(this->symtab_shndx_));
1493 section_size_type names_size;
1494 const unsigned char* symbol_names_u =
1495 this->section_contents(symbol_names_shndx, &names_size, false);
1496 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1498 const int sym_size = This::sym_size;
1499 const size_t count = symbols_size / sym_size;
1501 const unsigned char* p = symbols;
1502 for (size_t i = 0; i < count; ++i, p += sym_size)
1504 elfcpp::Sym<size, big_endian> sym(p);
1506 if (sym.get_st_type() == elfcpp::STT_FILE)
1508 if (sym.get_st_name() >= names_size)
1509 info->source_file = "(invalid)";
1511 info->source_file = symbol_names + sym.get_st_name();
1516 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1519 && st_shndx == shndx
1520 && static_cast<off_t>(sym.get_st_value()) <= offset
1521 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1524 if (sym.get_st_name() > names_size)
1525 info->enclosing_symbol_name = "(invalid)";
1528 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1529 if (parameters->options().do_demangle())
1531 char* demangled_name = cplus_demangle(
1532 info->enclosing_symbol_name.c_str(),
1533 DMGL_ANSI | DMGL_PARAMS);
1534 if (demangled_name != NULL)
1536 info->enclosing_symbol_name.assign(demangled_name);
1537 free(demangled_name);
1548 // Look for a kept section corresponding to the given discarded section,
1549 // and return its output address. This is used only for relocations in
1550 // debugging sections. If we can't find the kept section, return 0.
1552 template<int size, bool big_endian>
1553 typename Sized_relobj<size, big_endian>::Address
1554 Sized_relobj<size, big_endian>::map_to_kept_section(
1558 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1561 gold_assert(kept->object_ != NULL);
1563 return (static_cast<Address>
1564 (kept->object_->output_section_address(kept->shndx_)));
1570 // Input_objects methods.
1572 // Add a regular relocatable object to the list. Return false if this
1573 // object should be ignored.
1576 Input_objects::add_object(Object* obj)
1578 // Set the global target from the first object file we recognize.
1579 Target* target = obj->target();
1580 if (!parameters->target_valid())
1581 set_parameters_target(target);
1582 else if (target != ¶meters->target())
1584 obj->error(_("incompatible target"));
1588 // Print the filename if the -t/--trace option is selected.
1589 if (parameters->options().trace())
1590 gold_info("%s", obj->name().c_str());
1592 if (!obj->is_dynamic())
1593 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1596 // See if this is a duplicate SONAME.
1597 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1598 const char* soname = dynobj->soname();
1600 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1601 this->sonames_.insert(soname);
1604 // We have already seen a dynamic object with this soname.
1608 this->dynobj_list_.push_back(dynobj);
1610 // If this is -lc, remember the directory in which we found it.
1611 // We use this when issuing warnings about undefined symbols: as
1612 // a heuristic, we don't warn about system libraries found in
1613 // the same directory as -lc.
1614 if (strncmp(soname, "libc.so", 7) == 0)
1616 const char* object_name = dynobj->name().c_str();
1617 const char* base = lbasename(object_name);
1618 if (base != object_name)
1619 this->system_library_directory_.assign(object_name,
1620 base - 1 - object_name);
1627 // Return whether an object was found in the system library directory.
1630 Input_objects::found_in_system_library_directory(const Object* object) const
1632 return (!this->system_library_directory_.empty()
1633 && object->name().compare(0,
1634 this->system_library_directory_.size(),
1635 this->system_library_directory_) == 0);
1638 // For each dynamic object, record whether we've seen all of its
1639 // explicit dependencies.
1642 Input_objects::check_dynamic_dependencies() const
1644 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1645 p != this->dynobj_list_.end();
1648 const Dynobj::Needed& needed((*p)->needed());
1649 bool found_all = true;
1650 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1651 pneeded != needed.end();
1654 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1660 (*p)->set_has_unknown_needed_entries(!found_all);
1664 // Relocate_info methods.
1666 // Return a string describing the location of a relocation. This is
1667 // only used in error messages.
1669 template<int size, bool big_endian>
1671 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1673 // See if we can get line-number information from debugging sections.
1674 std::string filename;
1675 std::string file_and_lineno; // Better than filename-only, if available.
1677 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1678 // This will be "" if we failed to parse the debug info for any reason.
1679 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1681 std::string ret(this->object->name());
1683 Symbol_location_info info;
1684 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1686 ret += " in function ";
1687 ret += info.enclosing_symbol_name;
1689 filename = info.source_file;
1692 if (!file_and_lineno.empty())
1693 ret += file_and_lineno;
1696 if (!filename.empty())
1699 ret += this->object->section_name(this->data_shndx);
1701 // Offsets into sections have to be positive.
1702 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1709 } // End namespace gold.
1714 using namespace gold;
1716 // Read an ELF file with the header and return the appropriate
1717 // instance of Object.
1719 template<int size, bool big_endian>
1721 make_elf_sized_object(const std::string& name, Input_file* input_file,
1722 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1724 int et = ehdr.get_e_type();
1725 if (et == elfcpp::ET_REL)
1727 Sized_relobj<size, big_endian>* obj =
1728 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1732 else if (et == elfcpp::ET_DYN)
1734 Sized_dynobj<size, big_endian>* obj =
1735 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1741 gold_error(_("%s: unsupported ELF file type %d"),
1747 } // End anonymous namespace.
1752 // Read an ELF file and return the appropriate instance of Object.
1755 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1756 const unsigned char* p, section_offset_type bytes)
1758 if (bytes < elfcpp::EI_NIDENT)
1760 gold_error(_("%s: ELF file too short"), name.c_str());
1764 int v = p[elfcpp::EI_VERSION];
1765 if (v != elfcpp::EV_CURRENT)
1767 if (v == elfcpp::EV_NONE)
1768 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1770 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1774 int c = p[elfcpp::EI_CLASS];
1775 if (c == elfcpp::ELFCLASSNONE)
1777 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1780 else if (c != elfcpp::ELFCLASS32
1781 && c != elfcpp::ELFCLASS64)
1783 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1787 int d = p[elfcpp::EI_DATA];
1788 if (d == elfcpp::ELFDATANONE)
1790 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1793 else if (d != elfcpp::ELFDATA2LSB
1794 && d != elfcpp::ELFDATA2MSB)
1796 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1800 bool big_endian = d == elfcpp::ELFDATA2MSB;
1802 if (c == elfcpp::ELFCLASS32)
1804 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1806 gold_error(_("%s: ELF file too short"), name.c_str());
1811 #ifdef HAVE_TARGET_32_BIG
1812 elfcpp::Ehdr<32, true> ehdr(p);
1813 return make_elf_sized_object<32, true>(name, input_file,
1816 gold_error(_("%s: not configured to support "
1817 "32-bit big-endian object"),
1824 #ifdef HAVE_TARGET_32_LITTLE
1825 elfcpp::Ehdr<32, false> ehdr(p);
1826 return make_elf_sized_object<32, false>(name, input_file,
1829 gold_error(_("%s: not configured to support "
1830 "32-bit little-endian object"),
1838 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
1840 gold_error(_("%s: ELF file too short"), name.c_str());
1845 #ifdef HAVE_TARGET_64_BIG
1846 elfcpp::Ehdr<64, true> ehdr(p);
1847 return make_elf_sized_object<64, true>(name, input_file,
1850 gold_error(_("%s: not configured to support "
1851 "64-bit big-endian object"),
1858 #ifdef HAVE_TARGET_64_LITTLE
1859 elfcpp::Ehdr<64, false> ehdr(p);
1860 return make_elf_sized_object<64, false>(name, input_file,
1863 gold_error(_("%s: not configured to support "
1864 "64-bit little-endian object"),
1872 // Instantiate the templates we need.
1874 #ifdef HAVE_TARGET_32_LITTLE
1877 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1878 Read_symbols_data*);
1881 #ifdef HAVE_TARGET_32_BIG
1884 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1885 Read_symbols_data*);
1888 #ifdef HAVE_TARGET_64_LITTLE
1891 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1892 Read_symbols_data*);
1895 #ifdef HAVE_TARGET_64_BIG
1898 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1899 Read_symbols_data*);
1902 #ifdef HAVE_TARGET_32_LITTLE
1904 class Sized_relobj<32, false>;
1907 #ifdef HAVE_TARGET_32_BIG
1909 class Sized_relobj<32, true>;
1912 #ifdef HAVE_TARGET_64_LITTLE
1914 class Sized_relobj<64, false>;
1917 #ifdef HAVE_TARGET_64_BIG
1919 class Sized_relobj<64, true>;
1922 #ifdef HAVE_TARGET_32_LITTLE
1924 struct Relocate_info<32, false>;
1927 #ifdef HAVE_TARGET_32_BIG
1929 struct Relocate_info<32, true>;
1932 #ifdef HAVE_TARGET_64_LITTLE
1934 struct Relocate_info<64, false>;
1937 #ifdef HAVE_TARGET_64_BIG
1939 struct Relocate_info<64, true>;
1942 } // End namespace gold.