2009-01-14 Mike Frysinger <vapier@gentoo.org>
[binutils.git] / gold / object.cc
blobb1f83e7aef8eb7f8462b65ca50e81ca289db84e8
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "demangle.h"
29 #include "libiberty.h"
31 #include "target-select.h"
32 #include "dwarf_reader.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "symtab.h"
36 #include "cref.h"
37 #include "reloc.h"
38 #include "object.h"
39 #include "dynobj.h"
40 #include "plugin.h"
42 namespace gold
45 // Class Xindex.
47 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
48 // section and read it in. SYMTAB_SHNDX is the index of the symbol
49 // table we care about.
51 template<int size, bool big_endian>
52 void
53 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
55 if (!this->symtab_xindex_.empty())
56 return;
58 gold_assert(symtab_shndx != 0);
60 // Look through the sections in reverse order, on the theory that it
61 // is more likely to be near the end than the beginning.
62 unsigned int i = object->shnum();
63 while (i > 0)
65 --i;
66 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
67 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
69 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
70 return;
74 object->error(_("missing SHT_SYMTAB_SHNDX section"));
77 // Read in the symtab_xindex_ array, given the section index of the
78 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
79 // section headers.
81 template<int size, bool big_endian>
82 void
83 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
84 const unsigned char* pshdrs)
86 section_size_type bytecount;
87 const unsigned char* contents;
88 if (pshdrs == NULL)
89 contents = object->section_contents(xindex_shndx, &bytecount, false);
90 else
92 const unsigned char* p = (pshdrs
93 + (xindex_shndx
94 * elfcpp::Elf_sizes<size>::shdr_size));
95 typename elfcpp::Shdr<size, big_endian> shdr(p);
96 bytecount = convert_to_section_size_type(shdr.get_sh_size());
97 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
100 gold_assert(this->symtab_xindex_.empty());
101 this->symtab_xindex_.reserve(bytecount / 4);
102 for (section_size_type i = 0; i < bytecount; i += 4)
104 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
105 // We preadjust the section indexes we save.
106 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
110 // Symbol symndx has a section of SHN_XINDEX; return the real section
111 // index.
113 unsigned int
114 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
116 if (symndx >= this->symtab_xindex_.size())
118 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
119 symndx);
120 return elfcpp::SHN_UNDEF;
122 unsigned int shndx = this->symtab_xindex_[symndx];
123 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
125 object->error(_("extended index for symbol %u out of range: %u"),
126 symndx, shndx);
127 return elfcpp::SHN_UNDEF;
129 return shndx;
132 // Class Object.
134 // Set the target based on fields in the ELF file header.
136 void
137 Object::set_target(int machine, int size, bool big_endian, int osabi,
138 int abiversion)
140 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
141 if (target == NULL)
142 gold_fatal(_("%s: unsupported ELF machine number %d"),
143 this->name().c_str(), machine);
144 this->target_ = target;
147 // Report an error for this object file. This is used by the
148 // elfcpp::Elf_file interface, and also called by the Object code
149 // itself.
151 void
152 Object::error(const char* format, ...) const
154 va_list args;
155 va_start(args, format);
156 char* buf = NULL;
157 if (vasprintf(&buf, format, args) < 0)
158 gold_nomem();
159 va_end(args);
160 gold_error(_("%s: %s"), this->name().c_str(), buf);
161 free(buf);
164 // Return a view of the contents of a section.
166 const unsigned char*
167 Object::section_contents(unsigned int shndx, section_size_type* plen,
168 bool cache)
170 Location loc(this->do_section_contents(shndx));
171 *plen = convert_to_section_size_type(loc.data_size);
172 return this->get_view(loc.file_offset, *plen, true, cache);
175 // Read the section data into SD. This is code common to Sized_relobj
176 // and Sized_dynobj, so we put it into Object.
178 template<int size, bool big_endian>
179 void
180 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
181 Read_symbols_data* sd)
183 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
185 // Read the section headers.
186 const off_t shoff = elf_file->shoff();
187 const unsigned int shnum = this->shnum();
188 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
189 true, true);
191 // Read the section names.
192 const unsigned char* pshdrs = sd->section_headers->data();
193 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
194 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
196 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
197 this->error(_("section name section has wrong type: %u"),
198 static_cast<unsigned int>(shdrnames.get_sh_type()));
200 sd->section_names_size =
201 convert_to_section_size_type(shdrnames.get_sh_size());
202 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
203 sd->section_names_size, false,
204 false);
207 // If NAME is the name of a special .gnu.warning section, arrange for
208 // the warning to be issued. SHNDX is the section index. Return
209 // whether it is a warning section.
211 bool
212 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
213 Symbol_table* symtab)
215 const char warn_prefix[] = ".gnu.warning.";
216 const int warn_prefix_len = sizeof warn_prefix - 1;
217 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
219 // Read the section contents to get the warning text. It would
220 // be nicer if we only did this if we have to actually issue a
221 // warning. Unfortunately, warnings are issued as we relocate
222 // sections. That means that we can not lock the object then,
223 // as we might try to issue the same warning multiple times
224 // simultaneously.
225 section_size_type len;
226 const unsigned char* contents = this->section_contents(shndx, &len,
227 false);
228 std::string warning(reinterpret_cast<const char*>(contents), len);
229 symtab->add_warning(name + warn_prefix_len, this, warning);
230 return true;
232 return false;
235 // Class Sized_relobj.
237 template<int size, bool big_endian>
238 Sized_relobj<size, big_endian>::Sized_relobj(
239 const std::string& name,
240 Input_file* input_file,
241 off_t offset,
242 const elfcpp::Ehdr<size, big_endian>& ehdr)
243 : Relobj(name, input_file, offset),
244 elf_file_(this, ehdr),
245 symtab_shndx_(-1U),
246 local_symbol_count_(0),
247 output_local_symbol_count_(0),
248 output_local_dynsym_count_(0),
249 symbols_(),
250 defined_count_(0),
251 local_symbol_offset_(0),
252 local_dynsym_offset_(0),
253 local_values_(),
254 local_got_offsets_(),
255 kept_comdat_sections_(),
256 comdat_groups_(),
257 has_eh_frame_(false)
261 template<int size, bool big_endian>
262 Sized_relobj<size, big_endian>::~Sized_relobj()
266 // Set up an object file based on the file header. This sets up the
267 // target and reads the section information.
269 template<int size, bool big_endian>
270 void
271 Sized_relobj<size, big_endian>::setup(
272 const elfcpp::Ehdr<size, big_endian>& ehdr)
274 this->set_target(ehdr.get_e_machine(), size, big_endian,
275 ehdr.get_e_ident()[elfcpp::EI_OSABI],
276 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
278 const unsigned int shnum = this->elf_file_.shnum();
279 this->set_shnum(shnum);
282 // Find the SHT_SYMTAB section, given the section headers. The ELF
283 // standard says that maybe in the future there can be more than one
284 // SHT_SYMTAB section. Until somebody figures out how that could
285 // work, we assume there is only one.
287 template<int size, bool big_endian>
288 void
289 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
291 const unsigned int shnum = this->shnum();
292 this->symtab_shndx_ = 0;
293 if (shnum > 0)
295 // Look through the sections in reverse order, since gas tends
296 // to put the symbol table at the end.
297 const unsigned char* p = pshdrs + shnum * This::shdr_size;
298 unsigned int i = shnum;
299 unsigned int xindex_shndx = 0;
300 unsigned int xindex_link = 0;
301 while (i > 0)
303 --i;
304 p -= This::shdr_size;
305 typename This::Shdr shdr(p);
306 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
308 this->symtab_shndx_ = i;
309 if (xindex_shndx > 0 && xindex_link == i)
311 Xindex* xindex =
312 new Xindex(this->elf_file_.large_shndx_offset());
313 xindex->read_symtab_xindex<size, big_endian>(this,
314 xindex_shndx,
315 pshdrs);
316 this->set_xindex(xindex);
318 break;
321 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
322 // one. This will work if it follows the SHT_SYMTAB
323 // section.
324 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
326 xindex_shndx = i;
327 xindex_link = this->adjust_shndx(shdr.get_sh_link());
333 // Return the Xindex structure to use for object with lots of
334 // sections.
336 template<int size, bool big_endian>
337 Xindex*
338 Sized_relobj<size, big_endian>::do_initialize_xindex()
340 gold_assert(this->symtab_shndx_ != -1U);
341 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
342 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
343 return xindex;
346 // Return whether SHDR has the right type and flags to be a GNU
347 // .eh_frame section.
349 template<int size, bool big_endian>
350 bool
351 Sized_relobj<size, big_endian>::check_eh_frame_flags(
352 const elfcpp::Shdr<size, big_endian>* shdr) const
354 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
355 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
358 // Return whether there is a GNU .eh_frame section, given the section
359 // headers and the section names.
361 template<int size, bool big_endian>
362 bool
363 Sized_relobj<size, big_endian>::find_eh_frame(
364 const unsigned char* pshdrs,
365 const char* names,
366 section_size_type names_size) const
368 const unsigned int shnum = this->shnum();
369 const unsigned char* p = pshdrs + This::shdr_size;
370 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
372 typename This::Shdr shdr(p);
373 if (this->check_eh_frame_flags(&shdr))
375 if (shdr.get_sh_name() >= names_size)
377 this->error(_("bad section name offset for section %u: %lu"),
378 i, static_cast<unsigned long>(shdr.get_sh_name()));
379 continue;
382 const char* name = names + shdr.get_sh_name();
383 if (strcmp(name, ".eh_frame") == 0)
384 return true;
387 return false;
390 // Read the sections and symbols from an object file.
392 template<int size, bool big_endian>
393 void
394 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
396 this->read_section_data(&this->elf_file_, sd);
398 const unsigned char* const pshdrs = sd->section_headers->data();
400 this->find_symtab(pshdrs);
402 const unsigned char* namesu = sd->section_names->data();
403 const char* names = reinterpret_cast<const char*>(namesu);
404 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
406 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
407 this->has_eh_frame_ = true;
410 sd->symbols = NULL;
411 sd->symbols_size = 0;
412 sd->external_symbols_offset = 0;
413 sd->symbol_names = NULL;
414 sd->symbol_names_size = 0;
416 if (this->symtab_shndx_ == 0)
418 // No symbol table. Weird but legal.
419 return;
422 // Get the symbol table section header.
423 typename This::Shdr symtabshdr(pshdrs
424 + this->symtab_shndx_ * This::shdr_size);
425 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
427 // If this object has a .eh_frame section, we need all the symbols.
428 // Otherwise we only need the external symbols. While it would be
429 // simpler to just always read all the symbols, I've seen object
430 // files with well over 2000 local symbols, which for a 64-bit
431 // object file format is over 5 pages that we don't need to read
432 // now.
434 const int sym_size = This::sym_size;
435 const unsigned int loccount = symtabshdr.get_sh_info();
436 this->local_symbol_count_ = loccount;
437 this->local_values_.resize(loccount);
438 section_offset_type locsize = loccount * sym_size;
439 off_t dataoff = symtabshdr.get_sh_offset();
440 section_size_type datasize =
441 convert_to_section_size_type(symtabshdr.get_sh_size());
442 off_t extoff = dataoff + locsize;
443 section_size_type extsize = datasize - locsize;
445 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
446 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
448 if (readsize == 0)
450 // No external symbols. Also weird but also legal.
451 return;
454 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
456 // Read the section header for the symbol names.
457 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
458 if (strtab_shndx >= this->shnum())
460 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
461 return;
463 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
464 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
466 this->error(_("symbol table name section has wrong type: %u"),
467 static_cast<unsigned int>(strtabshdr.get_sh_type()));
468 return;
471 // Read the symbol names.
472 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
473 strtabshdr.get_sh_size(),
474 false, true);
476 sd->symbols = fvsymtab;
477 sd->symbols_size = readsize;
478 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
479 sd->symbol_names = fvstrtab;
480 sd->symbol_names_size =
481 convert_to_section_size_type(strtabshdr.get_sh_size());
484 // Return the section index of symbol SYM. Set *VALUE to its value in
485 // the object file. Set *IS_ORDINARY if this is an ordinary section
486 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
487 // Note that for a symbol which is not defined in this object file,
488 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
489 // the final value of the symbol in the link.
491 template<int size, bool big_endian>
492 unsigned int
493 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
494 Address* value,
495 bool* is_ordinary)
497 section_size_type symbols_size;
498 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
499 &symbols_size,
500 false);
502 const size_t count = symbols_size / This::sym_size;
503 gold_assert(sym < count);
505 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
506 *value = elfsym.get_st_value();
508 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
511 // Return whether to include a section group in the link. LAYOUT is
512 // used to keep track of which section groups we have already seen.
513 // INDEX is the index of the section group and SHDR is the section
514 // header. If we do not want to include this group, we set bits in
515 // OMIT for each section which should be discarded.
517 template<int size, bool big_endian>
518 bool
519 Sized_relobj<size, big_endian>::include_section_group(
520 Symbol_table* symtab,
521 Layout* layout,
522 unsigned int index,
523 const char* name,
524 const unsigned char* shdrs,
525 const char* section_names,
526 section_size_type section_names_size,
527 std::vector<bool>* omit)
529 // Read the section contents.
530 typename This::Shdr shdr(shdrs + index * This::shdr_size);
531 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
532 shdr.get_sh_size(), true, false);
533 const elfcpp::Elf_Word* pword =
534 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
536 // The first word contains flags. We only care about COMDAT section
537 // groups. Other section groups are always included in the link
538 // just like ordinary sections.
539 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
541 // Look up the group signature, which is the name of a symbol. This
542 // is a lot of effort to go to to read a string. Why didn't they
543 // just have the group signature point into the string table, rather
544 // than indirect through a symbol?
546 // Get the appropriate symbol table header (this will normally be
547 // the single SHT_SYMTAB section, but in principle it need not be).
548 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
549 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
551 // Read the symbol table entry.
552 unsigned int symndx = shdr.get_sh_info();
553 if (symndx >= symshdr.get_sh_size() / This::sym_size)
555 this->error(_("section group %u info %u out of range"),
556 index, symndx);
557 return false;
559 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
560 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
561 false);
562 elfcpp::Sym<size, big_endian> sym(psym);
564 // Read the symbol table names.
565 section_size_type symnamelen;
566 const unsigned char* psymnamesu;
567 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
568 &symnamelen, true);
569 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
571 // Get the section group signature.
572 if (sym.get_st_name() >= symnamelen)
574 this->error(_("symbol %u name offset %u out of range"),
575 symndx, sym.get_st_name());
576 return false;
579 std::string signature(psymnames + sym.get_st_name());
581 // It seems that some versions of gas will create a section group
582 // associated with a section symbol, and then fail to give a name to
583 // the section symbol. In such a case, use the name of the section.
584 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
586 bool is_ordinary;
587 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
588 sym.get_st_shndx(),
589 &is_ordinary);
590 if (!is_ordinary || sym_shndx >= this->shnum())
592 this->error(_("symbol %u invalid section index %u"),
593 symndx, sym_shndx);
594 return false;
596 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
597 if (member_shdr.get_sh_name() < section_names_size)
598 signature = section_names + member_shdr.get_sh_name();
601 // Record this section group in the layout, and see whether we've already
602 // seen one with the same signature.
603 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
604 || layout->add_comdat(this, index, signature, true));
606 Sized_relobj<size, big_endian>* kept_object = NULL;
607 Comdat_group* kept_group = NULL;
609 if (!include_group)
611 // This group is being discarded. Find the object and group
612 // that was kept in its place.
613 unsigned int kept_group_index = 0;
614 Relobj* kept_relobj = layout->find_kept_object(signature,
615 &kept_group_index);
616 kept_object = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
617 if (kept_object != NULL)
618 kept_group = kept_object->find_comdat_group(kept_group_index);
620 else if (flags & elfcpp::GRP_COMDAT)
622 // This group is being kept. Create the table to map section names
623 // to section indexes and add it to the table of groups.
624 kept_group = new Comdat_group();
625 this->add_comdat_group(index, kept_group);
628 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
630 std::vector<unsigned int> shndxes;
631 bool relocate_group = include_group && parameters->options().relocatable();
632 if (relocate_group)
633 shndxes.reserve(count - 1);
635 for (size_t i = 1; i < count; ++i)
637 elfcpp::Elf_Word secnum =
638 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
640 if (relocate_group)
641 shndxes.push_back(secnum);
643 if (secnum >= this->shnum())
645 this->error(_("section %u in section group %u out of range"),
646 secnum, index);
647 continue;
650 // Check for an earlier section number, since we're going to get
651 // it wrong--we may have already decided to include the section.
652 if (secnum < index)
653 this->error(_("invalid section group %u refers to earlier section %u"),
654 index, secnum);
656 // Get the name of the member section.
657 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
658 if (member_shdr.get_sh_name() >= section_names_size)
660 // This is an error, but it will be diagnosed eventually
661 // in do_layout, so we don't need to do anything here but
662 // ignore it.
663 continue;
665 std::string mname(section_names + member_shdr.get_sh_name());
667 if (!include_group)
669 (*omit)[secnum] = true;
670 if (kept_group != NULL)
672 // Find the corresponding kept section, and store that info
673 // in the discarded section table.
674 Comdat_group::const_iterator p = kept_group->find(mname);
675 if (p != kept_group->end())
677 Kept_comdat_section* kept =
678 new Kept_comdat_section(kept_object, p->second);
679 this->set_kept_comdat_section(secnum, kept);
683 else if (flags & elfcpp::GRP_COMDAT)
685 // Add the section to the kept group table.
686 gold_assert(kept_group != NULL);
687 kept_group->insert(std::make_pair(mname, secnum));
691 if (relocate_group)
692 layout->layout_group(symtab, this, index, name, signature.c_str(),
693 shdr, flags, &shndxes);
695 return include_group;
698 // Whether to include a linkonce section in the link. NAME is the
699 // name of the section and SHDR is the section header.
701 // Linkonce sections are a GNU extension implemented in the original
702 // GNU linker before section groups were defined. The semantics are
703 // that we only include one linkonce section with a given name. The
704 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
705 // where T is the type of section and SYMNAME is the name of a symbol.
706 // In an attempt to make linkonce sections interact well with section
707 // groups, we try to identify SYMNAME and use it like a section group
708 // signature. We want to block section groups with that signature,
709 // but not other linkonce sections with that signature. We also use
710 // the full name of the linkonce section as a normal section group
711 // signature.
713 template<int size, bool big_endian>
714 bool
715 Sized_relobj<size, big_endian>::include_linkonce_section(
716 Layout* layout,
717 unsigned int index,
718 const char* name,
719 const elfcpp::Shdr<size, big_endian>&)
721 // In general the symbol name we want will be the string following
722 // the last '.'. However, we have to handle the case of
723 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
724 // some versions of gcc. So we use a heuristic: if the name starts
725 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
726 // we look for the last '.'. We can't always simply skip
727 // ".gnu.linkonce.X", because we have to deal with cases like
728 // ".gnu.linkonce.d.rel.ro.local".
729 const char* const linkonce_t = ".gnu.linkonce.t.";
730 const char* symname;
731 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
732 symname = name + strlen(linkonce_t);
733 else
734 symname = strrchr(name, '.') + 1;
735 std::string sig1(symname);
736 std::string sig2(name);
737 bool include1 = layout->add_comdat(this, index, sig1, false);
738 bool include2 = layout->add_comdat(this, index, sig2, true);
740 if (!include2)
742 // The section is being discarded on the basis of its section
743 // name (i.e., the kept section was also a linkonce section).
744 // In this case, the section index stored with the layout object
745 // is the linkonce section that was kept.
746 unsigned int kept_group_index = 0;
747 Relobj* kept_relobj = layout->find_kept_object(sig2, &kept_group_index);
748 if (kept_relobj != NULL)
750 Sized_relobj<size, big_endian>* kept_object
751 = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
752 Kept_comdat_section* kept =
753 new Kept_comdat_section(kept_object, kept_group_index);
754 this->set_kept_comdat_section(index, kept);
757 else if (!include1)
759 // The section is being discarded on the basis of its symbol
760 // name. This means that the corresponding kept section was
761 // part of a comdat group, and it will be difficult to identify
762 // the specific section within that group that corresponds to
763 // this linkonce section. We'll handle the simple case where
764 // the group has only one member section. Otherwise, it's not
765 // worth the effort.
766 unsigned int kept_group_index = 0;
767 Relobj* kept_relobj = layout->find_kept_object(sig1, &kept_group_index);
768 if (kept_relobj != NULL)
770 Sized_relobj<size, big_endian>* kept_object =
771 static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
772 Comdat_group* kept_group =
773 kept_object->find_comdat_group(kept_group_index);
774 if (kept_group != NULL && kept_group->size() == 1)
776 Comdat_group::const_iterator p = kept_group->begin();
777 gold_assert(p != kept_group->end());
778 Kept_comdat_section* kept =
779 new Kept_comdat_section(kept_object, p->second);
780 this->set_kept_comdat_section(index, kept);
785 return include1 && include2;
788 // Layout an input section.
790 template<int size, bool big_endian>
791 inline void
792 Sized_relobj<size, big_endian>::layout_section(Layout* layout,
793 unsigned int shndx,
794 const char* name,
795 typename This::Shdr& shdr,
796 unsigned int reloc_shndx,
797 unsigned int reloc_type)
799 off_t offset;
800 Output_section* os = layout->layout(this, shndx, name, shdr,
801 reloc_shndx, reloc_type, &offset);
803 this->output_sections()[shndx] = os;
804 if (offset == -1)
805 this->section_offsets_[shndx] = invalid_address;
806 else
807 this->section_offsets_[shndx] = convert_types<Address, off_t>(offset);
809 // If this section requires special handling, and if there are
810 // relocs that apply to it, then we must do the special handling
811 // before we apply the relocs.
812 if (offset == -1 && reloc_shndx != 0)
813 this->set_relocs_must_follow_section_writes();
816 // Lay out the input sections. We walk through the sections and check
817 // whether they should be included in the link. If they should, we
818 // pass them to the Layout object, which will return an output section
819 // and an offset.
821 template<int size, bool big_endian>
822 void
823 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
824 Layout* layout,
825 Read_symbols_data* sd)
827 const unsigned int shnum = this->shnum();
828 if (shnum == 0)
829 return;
831 // Get the section headers.
832 const unsigned char* shdrs = sd->section_headers->data();
833 const unsigned char* pshdrs;
835 // Get the section names.
836 const unsigned char* pnamesu = sd->section_names->data();
837 const char* pnames = reinterpret_cast<const char*>(pnamesu);
839 // If any input files have been claimed by plugins, we need to defer
840 // actual layout until the replacement files have arrived.
841 const bool should_defer_layout =
842 (parameters->options().has_plugins()
843 && parameters->options().plugins()->should_defer_layout());
844 unsigned int num_sections_to_defer = 0;
846 // For each section, record the index of the reloc section if any.
847 // Use 0 to mean that there is no reloc section, -1U to mean that
848 // there is more than one.
849 std::vector<unsigned int> reloc_shndx(shnum, 0);
850 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
851 // Skip the first, dummy, section.
852 pshdrs = shdrs + This::shdr_size;
853 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
855 typename This::Shdr shdr(pshdrs);
857 // Count the number of sections whose layout will be deferred.
858 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
859 ++num_sections_to_defer;
861 unsigned int sh_type = shdr.get_sh_type();
862 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
864 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
865 if (target_shndx == 0 || target_shndx >= shnum)
867 this->error(_("relocation section %u has bad info %u"),
868 i, target_shndx);
869 continue;
872 if (reloc_shndx[target_shndx] != 0)
873 reloc_shndx[target_shndx] = -1U;
874 else
876 reloc_shndx[target_shndx] = i;
877 reloc_type[target_shndx] = sh_type;
882 Output_sections& out_sections(this->output_sections());
883 std::vector<Address>& out_section_offsets(this->section_offsets_);
885 out_sections.resize(shnum);
886 out_section_offsets.resize(shnum);
888 // If we are only linking for symbols, then there is nothing else to
889 // do here.
890 if (this->input_file()->just_symbols())
892 delete sd->section_headers;
893 sd->section_headers = NULL;
894 delete sd->section_names;
895 sd->section_names = NULL;
896 return;
899 if (num_sections_to_defer > 0)
901 parameters->options().plugins()->add_deferred_layout_object(this);
902 this->deferred_layout_.reserve(num_sections_to_defer);
905 // Whether we've seen a .note.GNU-stack section.
906 bool seen_gnu_stack = false;
907 // The flags of a .note.GNU-stack section.
908 uint64_t gnu_stack_flags = 0;
910 // Keep track of which sections to omit.
911 std::vector<bool> omit(shnum, false);
913 // Keep track of reloc sections when emitting relocations.
914 const bool relocatable = parameters->options().relocatable();
915 const bool emit_relocs = (relocatable
916 || parameters->options().emit_relocs());
917 std::vector<unsigned int> reloc_sections;
919 // Keep track of .eh_frame sections.
920 std::vector<unsigned int> eh_frame_sections;
922 // Skip the first, dummy, section.
923 pshdrs = shdrs + This::shdr_size;
924 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
926 typename This::Shdr shdr(pshdrs);
928 if (shdr.get_sh_name() >= sd->section_names_size)
930 this->error(_("bad section name offset for section %u: %lu"),
931 i, static_cast<unsigned long>(shdr.get_sh_name()));
932 return;
935 const char* name = pnames + shdr.get_sh_name();
937 if (this->handle_gnu_warning_section(name, i, symtab))
939 if (!relocatable)
940 omit[i] = true;
943 // The .note.GNU-stack section is special. It gives the
944 // protection flags that this object file requires for the stack
945 // in memory.
946 if (strcmp(name, ".note.GNU-stack") == 0)
948 seen_gnu_stack = true;
949 gnu_stack_flags |= shdr.get_sh_flags();
950 omit[i] = true;
953 bool discard = omit[i];
954 if (!discard)
956 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
958 if (!this->include_section_group(symtab, layout, i, name, shdrs,
959 pnames, sd->section_names_size,
960 &omit))
961 discard = true;
963 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
964 && Layout::is_linkonce(name))
966 if (!this->include_linkonce_section(layout, i, name, shdr))
967 discard = true;
971 if (discard)
973 // Do not include this section in the link.
974 out_sections[i] = NULL;
975 out_section_offsets[i] = invalid_address;
976 continue;
979 // When doing a relocatable link we are going to copy input
980 // reloc sections into the output. We only want to copy the
981 // ones associated with sections which are not being discarded.
982 // However, we don't know that yet for all sections. So save
983 // reloc sections and process them later.
984 if (emit_relocs
985 && (shdr.get_sh_type() == elfcpp::SHT_REL
986 || shdr.get_sh_type() == elfcpp::SHT_RELA))
988 reloc_sections.push_back(i);
989 continue;
992 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
993 continue;
995 // The .eh_frame section is special. It holds exception frame
996 // information that we need to read in order to generate the
997 // exception frame header. We process these after all the other
998 // sections so that the exception frame reader can reliably
999 // determine which sections are being discarded, and discard the
1000 // corresponding information.
1001 if (!relocatable
1002 && strcmp(name, ".eh_frame") == 0
1003 && this->check_eh_frame_flags(&shdr))
1005 eh_frame_sections.push_back(i);
1006 continue;
1009 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1011 this->deferred_layout_.push_back(Deferred_layout(i, name, pshdrs,
1012 reloc_shndx[i],
1013 reloc_type[i]));
1015 // Put dummy values here; real values will be supplied by
1016 // do_layout_deferred_sections.
1017 out_sections[i] = reinterpret_cast<Output_section*>(1);
1018 out_section_offsets[i] = invalid_address;
1020 else
1022 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1023 reloc_type[i]);
1027 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1029 // When doing a relocatable link handle the reloc sections at the
1030 // end.
1031 if (emit_relocs)
1032 this->size_relocatable_relocs();
1033 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1034 p != reloc_sections.end();
1035 ++p)
1037 unsigned int i = *p;
1038 const unsigned char* pshdr;
1039 pshdr = sd->section_headers->data() + i * This::shdr_size;
1040 typename This::Shdr shdr(pshdr);
1042 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1043 if (data_shndx >= shnum)
1045 // We already warned about this above.
1046 continue;
1049 Output_section* data_section = out_sections[data_shndx];
1050 if (data_section == NULL)
1052 out_sections[i] = NULL;
1053 out_section_offsets[i] = invalid_address;
1054 continue;
1057 Relocatable_relocs* rr = new Relocatable_relocs();
1058 this->set_relocatable_relocs(i, rr);
1060 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1061 rr);
1062 out_sections[i] = os;
1063 out_section_offsets[i] = invalid_address;
1066 // Handle the .eh_frame sections at the end.
1067 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1068 p != eh_frame_sections.end();
1069 ++p)
1071 gold_assert(this->has_eh_frame_);
1072 gold_assert(sd->external_symbols_offset != 0);
1074 unsigned int i = *p;
1075 const unsigned char *pshdr;
1076 pshdr = sd->section_headers->data() + i * This::shdr_size;
1077 typename This::Shdr shdr(pshdr);
1079 off_t offset;
1080 Output_section* os = layout->layout_eh_frame(this,
1081 sd->symbols->data(),
1082 sd->symbols_size,
1083 sd->symbol_names->data(),
1084 sd->symbol_names_size,
1085 i, shdr,
1086 reloc_shndx[i],
1087 reloc_type[i],
1088 &offset);
1089 out_sections[i] = os;
1090 if (offset == -1)
1091 out_section_offsets[i] = invalid_address;
1092 else
1093 out_section_offsets[i] = convert_types<Address, off_t>(offset);
1095 // If this section requires special handling, and if there are
1096 // relocs that apply to it, then we must do the special handling
1097 // before we apply the relocs.
1098 if (offset == -1 && reloc_shndx[i] != 0)
1099 this->set_relocs_must_follow_section_writes();
1102 delete sd->section_headers;
1103 sd->section_headers = NULL;
1104 delete sd->section_names;
1105 sd->section_names = NULL;
1108 // Layout sections whose layout was deferred while waiting for
1109 // input files from a plugin.
1111 template<int size, bool big_endian>
1112 void
1113 Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1115 typename std::vector<Deferred_layout>::iterator deferred;
1117 for (deferred = this->deferred_layout_.begin();
1118 deferred != this->deferred_layout_.end();
1119 ++deferred)
1121 typename This::Shdr shdr(deferred->shdr_data_);
1122 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1123 shdr, deferred->reloc_shndx_, deferred->reloc_type_);
1126 this->deferred_layout_.clear();
1129 // Add the symbols to the symbol table.
1131 template<int size, bool big_endian>
1132 void
1133 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1134 Read_symbols_data* sd)
1136 if (sd->symbols == NULL)
1138 gold_assert(sd->symbol_names == NULL);
1139 return;
1142 const int sym_size = This::sym_size;
1143 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1144 / sym_size);
1145 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1147 this->error(_("size of symbols is not multiple of symbol size"));
1148 return;
1151 this->symbols_.resize(symcount);
1153 const char* sym_names =
1154 reinterpret_cast<const char*>(sd->symbol_names->data());
1155 symtab->add_from_relobj(this,
1156 sd->symbols->data() + sd->external_symbols_offset,
1157 symcount, this->local_symbol_count_,
1158 sym_names, sd->symbol_names_size,
1159 &this->symbols_,
1160 &this->defined_count_);
1162 delete sd->symbols;
1163 sd->symbols = NULL;
1164 delete sd->symbol_names;
1165 sd->symbol_names = NULL;
1168 // First pass over the local symbols. Here we add their names to
1169 // *POOL and *DYNPOOL, and we store the symbol value in
1170 // THIS->LOCAL_VALUES_. This function is always called from a
1171 // singleton thread. This is followed by a call to
1172 // finalize_local_symbols.
1174 template<int size, bool big_endian>
1175 void
1176 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1177 Stringpool* dynpool)
1179 gold_assert(this->symtab_shndx_ != -1U);
1180 if (this->symtab_shndx_ == 0)
1182 // This object has no symbols. Weird but legal.
1183 return;
1186 // Read the symbol table section header.
1187 const unsigned int symtab_shndx = this->symtab_shndx_;
1188 typename This::Shdr symtabshdr(this,
1189 this->elf_file_.section_header(symtab_shndx));
1190 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1192 // Read the local symbols.
1193 const int sym_size = This::sym_size;
1194 const unsigned int loccount = this->local_symbol_count_;
1195 gold_assert(loccount == symtabshdr.get_sh_info());
1196 off_t locsize = loccount * sym_size;
1197 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1198 locsize, true, true);
1200 // Read the symbol names.
1201 const unsigned int strtab_shndx =
1202 this->adjust_shndx(symtabshdr.get_sh_link());
1203 section_size_type strtab_size;
1204 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1205 &strtab_size,
1206 true);
1207 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1209 // Loop over the local symbols.
1211 const Output_sections& out_sections(this->output_sections());
1212 unsigned int shnum = this->shnum();
1213 unsigned int count = 0;
1214 unsigned int dyncount = 0;
1215 // Skip the first, dummy, symbol.
1216 psyms += sym_size;
1217 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1219 elfcpp::Sym<size, big_endian> sym(psyms);
1221 Symbol_value<size>& lv(this->local_values_[i]);
1223 bool is_ordinary;
1224 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1225 &is_ordinary);
1226 lv.set_input_shndx(shndx, is_ordinary);
1228 if (sym.get_st_type() == elfcpp::STT_SECTION)
1229 lv.set_is_section_symbol();
1230 else if (sym.get_st_type() == elfcpp::STT_TLS)
1231 lv.set_is_tls_symbol();
1233 // Save the input symbol value for use in do_finalize_local_symbols().
1234 lv.set_input_value(sym.get_st_value());
1236 // Decide whether this symbol should go into the output file.
1238 if (shndx < shnum && out_sections[shndx] == NULL)
1240 lv.set_no_output_symtab_entry();
1241 gold_assert(!lv.needs_output_dynsym_entry());
1242 continue;
1245 if (sym.get_st_type() == elfcpp::STT_SECTION)
1247 lv.set_no_output_symtab_entry();
1248 gold_assert(!lv.needs_output_dynsym_entry());
1249 continue;
1252 if (sym.get_st_name() >= strtab_size)
1254 this->error(_("local symbol %u section name out of range: %u >= %u"),
1255 i, sym.get_st_name(),
1256 static_cast<unsigned int>(strtab_size));
1257 lv.set_no_output_symtab_entry();
1258 continue;
1261 // Add the symbol to the symbol table string pool.
1262 const char* name = pnames + sym.get_st_name();
1263 pool->add(name, true, NULL);
1264 ++count;
1266 // If needed, add the symbol to the dynamic symbol table string pool.
1267 if (lv.needs_output_dynsym_entry())
1269 dynpool->add(name, true, NULL);
1270 ++dyncount;
1274 this->output_local_symbol_count_ = count;
1275 this->output_local_dynsym_count_ = dyncount;
1278 // Finalize the local symbols. Here we set the final value in
1279 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1280 // This function is always called from a singleton thread. The actual
1281 // output of the local symbols will occur in a separate task.
1283 template<int size, bool big_endian>
1284 unsigned int
1285 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1286 off_t off)
1288 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1290 const unsigned int loccount = this->local_symbol_count_;
1291 this->local_symbol_offset_ = off;
1293 const Output_sections& out_sections(this->output_sections());
1294 const std::vector<Address>& out_offsets(this->section_offsets_);
1295 unsigned int shnum = this->shnum();
1297 for (unsigned int i = 1; i < loccount; ++i)
1299 Symbol_value<size>& lv(this->local_values_[i]);
1301 bool is_ordinary;
1302 unsigned int shndx = lv.input_shndx(&is_ordinary);
1304 // Set the output symbol value.
1306 if (!is_ordinary)
1308 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1309 lv.set_output_value(lv.input_value());
1310 else
1312 this->error(_("unknown section index %u for local symbol %u"),
1313 shndx, i);
1314 lv.set_output_value(0);
1317 else
1319 if (shndx >= shnum)
1321 this->error(_("local symbol %u section index %u out of range"),
1322 i, shndx);
1323 shndx = 0;
1326 Output_section* os = out_sections[shndx];
1328 if (os == NULL)
1330 // This local symbol belongs to a section we are discarding.
1331 // In some cases when applying relocations later, we will
1332 // attempt to match it to the corresponding kept section,
1333 // so we leave the input value unchanged here.
1334 continue;
1336 else if (out_offsets[shndx] == invalid_address)
1338 // This is a SHF_MERGE section or one which otherwise
1339 // requires special handling. We get the output address
1340 // of the start of the merged section. If this is not a
1341 // section symbol, we can then determine the final
1342 // value. If it is a section symbol, we can not, as in
1343 // that case we have to consider the addend to determine
1344 // the value to use in a relocation.
1345 if (!lv.is_section_symbol())
1346 lv.set_output_value(os->output_address(this, shndx,
1347 lv.input_value()));
1348 else
1350 section_offset_type start =
1351 os->starting_output_address(this, shndx);
1352 Merged_symbol_value<size>* msv =
1353 new Merged_symbol_value<size>(lv.input_value(), start);
1354 lv.set_merged_symbol_value(msv);
1357 else if (lv.is_tls_symbol())
1358 lv.set_output_value(os->tls_offset()
1359 + out_offsets[shndx]
1360 + lv.input_value());
1361 else
1362 lv.set_output_value(os->address()
1363 + out_offsets[shndx]
1364 + lv.input_value());
1367 if (lv.needs_output_symtab_entry())
1369 lv.set_output_symtab_index(index);
1370 ++index;
1373 return index;
1376 // Set the output dynamic symbol table indexes for the local variables.
1378 template<int size, bool big_endian>
1379 unsigned int
1380 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1382 const unsigned int loccount = this->local_symbol_count_;
1383 for (unsigned int i = 1; i < loccount; ++i)
1385 Symbol_value<size>& lv(this->local_values_[i]);
1386 if (lv.needs_output_dynsym_entry())
1388 lv.set_output_dynsym_index(index);
1389 ++index;
1392 return index;
1395 // Set the offset where local dynamic symbol information will be stored.
1396 // Returns the count of local symbols contributed to the symbol table by
1397 // this object.
1399 template<int size, bool big_endian>
1400 unsigned int
1401 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1403 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1404 this->local_dynsym_offset_ = off;
1405 return this->output_local_dynsym_count_;
1408 // Write out the local symbols.
1410 template<int size, bool big_endian>
1411 void
1412 Sized_relobj<size, big_endian>::write_local_symbols(
1413 Output_file* of,
1414 const Stringpool* sympool,
1415 const Stringpool* dynpool,
1416 Output_symtab_xindex* symtab_xindex,
1417 Output_symtab_xindex* dynsym_xindex)
1419 if (parameters->options().strip_all()
1420 && this->output_local_dynsym_count_ == 0)
1421 return;
1423 gold_assert(this->symtab_shndx_ != -1U);
1424 if (this->symtab_shndx_ == 0)
1426 // This object has no symbols. Weird but legal.
1427 return;
1430 // Read the symbol table section header.
1431 const unsigned int symtab_shndx = this->symtab_shndx_;
1432 typename This::Shdr symtabshdr(this,
1433 this->elf_file_.section_header(symtab_shndx));
1434 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1435 const unsigned int loccount = this->local_symbol_count_;
1436 gold_assert(loccount == symtabshdr.get_sh_info());
1438 // Read the local symbols.
1439 const int sym_size = This::sym_size;
1440 off_t locsize = loccount * sym_size;
1441 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1442 locsize, true, false);
1444 // Read the symbol names.
1445 const unsigned int strtab_shndx =
1446 this->adjust_shndx(symtabshdr.get_sh_link());
1447 section_size_type strtab_size;
1448 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1449 &strtab_size,
1450 false);
1451 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1453 // Get views into the output file for the portions of the symbol table
1454 // and the dynamic symbol table that we will be writing.
1455 off_t output_size = this->output_local_symbol_count_ * sym_size;
1456 unsigned char* oview = NULL;
1457 if (output_size > 0)
1458 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1460 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1461 unsigned char* dyn_oview = NULL;
1462 if (dyn_output_size > 0)
1463 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1464 dyn_output_size);
1466 const Output_sections out_sections(this->output_sections());
1468 gold_assert(this->local_values_.size() == loccount);
1470 unsigned char* ov = oview;
1471 unsigned char* dyn_ov = dyn_oview;
1472 psyms += sym_size;
1473 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1475 elfcpp::Sym<size, big_endian> isym(psyms);
1477 Symbol_value<size>& lv(this->local_values_[i]);
1479 bool is_ordinary;
1480 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1481 &is_ordinary);
1482 if (is_ordinary)
1484 gold_assert(st_shndx < out_sections.size());
1485 if (out_sections[st_shndx] == NULL)
1486 continue;
1487 st_shndx = out_sections[st_shndx]->out_shndx();
1488 if (st_shndx >= elfcpp::SHN_LORESERVE)
1490 if (lv.needs_output_symtab_entry())
1491 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1492 if (lv.needs_output_dynsym_entry())
1493 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1494 st_shndx = elfcpp::SHN_XINDEX;
1498 // Write the symbol to the output symbol table.
1499 if (!parameters->options().strip_all()
1500 && lv.needs_output_symtab_entry())
1502 elfcpp::Sym_write<size, big_endian> osym(ov);
1504 gold_assert(isym.get_st_name() < strtab_size);
1505 const char* name = pnames + isym.get_st_name();
1506 osym.put_st_name(sympool->get_offset(name));
1507 osym.put_st_value(this->local_values_[i].value(this, 0));
1508 osym.put_st_size(isym.get_st_size());
1509 osym.put_st_info(isym.get_st_info());
1510 osym.put_st_other(isym.get_st_other());
1511 osym.put_st_shndx(st_shndx);
1513 ov += sym_size;
1516 // Write the symbol to the output dynamic symbol table.
1517 if (lv.needs_output_dynsym_entry())
1519 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1520 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1522 gold_assert(isym.get_st_name() < strtab_size);
1523 const char* name = pnames + isym.get_st_name();
1524 osym.put_st_name(dynpool->get_offset(name));
1525 osym.put_st_value(this->local_values_[i].value(this, 0));
1526 osym.put_st_size(isym.get_st_size());
1527 osym.put_st_info(isym.get_st_info());
1528 osym.put_st_other(isym.get_st_other());
1529 osym.put_st_shndx(st_shndx);
1531 dyn_ov += sym_size;
1536 if (output_size > 0)
1538 gold_assert(ov - oview == output_size);
1539 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1542 if (dyn_output_size > 0)
1544 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1545 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1546 dyn_oview);
1550 // Set *INFO to symbolic information about the offset OFFSET in the
1551 // section SHNDX. Return true if we found something, false if we
1552 // found nothing.
1554 template<int size, bool big_endian>
1555 bool
1556 Sized_relobj<size, big_endian>::get_symbol_location_info(
1557 unsigned int shndx,
1558 off_t offset,
1559 Symbol_location_info* info)
1561 if (this->symtab_shndx_ == 0)
1562 return false;
1564 section_size_type symbols_size;
1565 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1566 &symbols_size,
1567 false);
1569 unsigned int symbol_names_shndx =
1570 this->adjust_shndx(this->section_link(this->symtab_shndx_));
1571 section_size_type names_size;
1572 const unsigned char* symbol_names_u =
1573 this->section_contents(symbol_names_shndx, &names_size, false);
1574 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1576 const int sym_size = This::sym_size;
1577 const size_t count = symbols_size / sym_size;
1579 const unsigned char* p = symbols;
1580 for (size_t i = 0; i < count; ++i, p += sym_size)
1582 elfcpp::Sym<size, big_endian> sym(p);
1584 if (sym.get_st_type() == elfcpp::STT_FILE)
1586 if (sym.get_st_name() >= names_size)
1587 info->source_file = "(invalid)";
1588 else
1589 info->source_file = symbol_names + sym.get_st_name();
1590 continue;
1593 bool is_ordinary;
1594 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1595 &is_ordinary);
1596 if (is_ordinary
1597 && st_shndx == shndx
1598 && static_cast<off_t>(sym.get_st_value()) <= offset
1599 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1600 > offset))
1602 if (sym.get_st_name() > names_size)
1603 info->enclosing_symbol_name = "(invalid)";
1604 else
1606 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1607 if (parameters->options().do_demangle())
1609 char* demangled_name = cplus_demangle(
1610 info->enclosing_symbol_name.c_str(),
1611 DMGL_ANSI | DMGL_PARAMS);
1612 if (demangled_name != NULL)
1614 info->enclosing_symbol_name.assign(demangled_name);
1615 free(demangled_name);
1619 return true;
1623 return false;
1626 // Look for a kept section corresponding to the given discarded section,
1627 // and return its output address. This is used only for relocations in
1628 // debugging sections. If we can't find the kept section, return 0.
1630 template<int size, bool big_endian>
1631 typename Sized_relobj<size, big_endian>::Address
1632 Sized_relobj<size, big_endian>::map_to_kept_section(
1633 unsigned int shndx,
1634 bool* found) const
1636 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1637 if (kept != NULL)
1639 gold_assert(kept->object_ != NULL);
1640 *found = true;
1641 Output_section* os = kept->object_->output_section(kept->shndx_);
1642 Address offset = kept->object_->get_output_section_offset(kept->shndx_);
1643 gold_assert(os != NULL && offset != invalid_address);
1644 return os->address() + offset;
1646 *found = false;
1647 return 0;
1650 // Get symbol counts.
1652 template<int size, bool big_endian>
1653 void
1654 Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
1655 const Symbol_table*,
1656 size_t* defined,
1657 size_t* used) const
1659 *defined = this->defined_count_;
1660 size_t count = 0;
1661 for (Symbols::const_iterator p = this->symbols_.begin();
1662 p != this->symbols_.end();
1663 ++p)
1664 if (*p != NULL
1665 && (*p)->source() == Symbol::FROM_OBJECT
1666 && (*p)->object() == this
1667 && (*p)->is_defined())
1668 ++count;
1669 *used = count;
1672 // Input_objects methods.
1674 // Add a regular relocatable object to the list. Return false if this
1675 // object should be ignored.
1677 bool
1678 Input_objects::add_object(Object* obj)
1680 // Set the global target from the first object file we recognize.
1681 Target* target = obj->target();
1682 if (!parameters->target_valid())
1683 set_parameters_target(target);
1684 else if (target != &parameters->target())
1686 obj->error(_("incompatible target"));
1687 return false;
1690 // Print the filename if the -t/--trace option is selected.
1691 if (parameters->options().trace())
1692 gold_info("%s", obj->name().c_str());
1694 if (!obj->is_dynamic())
1695 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1696 else
1698 // See if this is a duplicate SONAME.
1699 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1700 const char* soname = dynobj->soname();
1702 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1703 this->sonames_.insert(soname);
1704 if (!ins.second)
1706 // We have already seen a dynamic object with this soname.
1707 return false;
1710 this->dynobj_list_.push_back(dynobj);
1712 // If this is -lc, remember the directory in which we found it.
1713 // We use this when issuing warnings about undefined symbols: as
1714 // a heuristic, we don't warn about system libraries found in
1715 // the same directory as -lc.
1716 if (strncmp(soname, "libc.so", 7) == 0)
1718 const char* object_name = dynobj->name().c_str();
1719 const char* base = lbasename(object_name);
1720 if (base != object_name)
1721 this->system_library_directory_.assign(object_name,
1722 base - 1 - object_name);
1726 // Add this object to the cross-referencer if requested.
1727 if (parameters->options().user_set_print_symbol_counts())
1729 if (this->cref_ == NULL)
1730 this->cref_ = new Cref();
1731 this->cref_->add_object(obj);
1734 return true;
1737 // Return whether an object was found in the system library directory.
1739 bool
1740 Input_objects::found_in_system_library_directory(const Object* object) const
1742 return (!this->system_library_directory_.empty()
1743 && object->name().compare(0,
1744 this->system_library_directory_.size(),
1745 this->system_library_directory_) == 0);
1748 // For each dynamic object, record whether we've seen all of its
1749 // explicit dependencies.
1751 void
1752 Input_objects::check_dynamic_dependencies() const
1754 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1755 p != this->dynobj_list_.end();
1756 ++p)
1758 const Dynobj::Needed& needed((*p)->needed());
1759 bool found_all = true;
1760 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1761 pneeded != needed.end();
1762 ++pneeded)
1764 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1766 found_all = false;
1767 break;
1770 (*p)->set_has_unknown_needed_entries(!found_all);
1774 // Start processing an archive.
1776 void
1777 Input_objects::archive_start(Archive* archive)
1779 if (parameters->options().user_set_print_symbol_counts())
1781 if (this->cref_ == NULL)
1782 this->cref_ = new Cref();
1783 this->cref_->add_archive_start(archive);
1787 // Stop processing an archive.
1789 void
1790 Input_objects::archive_stop(Archive* archive)
1792 if (parameters->options().user_set_print_symbol_counts())
1793 this->cref_->add_archive_stop(archive);
1796 // Print symbol counts
1798 void
1799 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
1801 if (parameters->options().user_set_print_symbol_counts()
1802 && this->cref_ != NULL)
1803 this->cref_->print_symbol_counts(symtab);
1806 // Relocate_info methods.
1808 // Return a string describing the location of a relocation. This is
1809 // only used in error messages.
1811 template<int size, bool big_endian>
1812 std::string
1813 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1815 // See if we can get line-number information from debugging sections.
1816 std::string filename;
1817 std::string file_and_lineno; // Better than filename-only, if available.
1819 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1820 // This will be "" if we failed to parse the debug info for any reason.
1821 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1823 std::string ret(this->object->name());
1824 ret += ':';
1825 Symbol_location_info info;
1826 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1828 ret += " in function ";
1829 ret += info.enclosing_symbol_name;
1830 ret += ":";
1831 filename = info.source_file;
1834 if (!file_and_lineno.empty())
1835 ret += file_and_lineno;
1836 else
1838 if (!filename.empty())
1839 ret += filename;
1840 ret += "(";
1841 ret += this->object->section_name(this->data_shndx);
1842 char buf[100];
1843 // Offsets into sections have to be positive.
1844 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1845 ret += buf;
1846 ret += ")";
1848 return ret;
1851 } // End namespace gold.
1853 namespace
1856 using namespace gold;
1858 // Read an ELF file with the header and return the appropriate
1859 // instance of Object.
1861 template<int size, bool big_endian>
1862 Object*
1863 make_elf_sized_object(const std::string& name, Input_file* input_file,
1864 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1866 int et = ehdr.get_e_type();
1867 if (et == elfcpp::ET_REL)
1869 Sized_relobj<size, big_endian>* obj =
1870 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1871 obj->setup(ehdr);
1872 return obj;
1874 else if (et == elfcpp::ET_DYN)
1876 Sized_dynobj<size, big_endian>* obj =
1877 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1878 obj->setup(ehdr);
1879 return obj;
1881 else
1883 gold_error(_("%s: unsupported ELF file type %d"),
1884 name.c_str(), et);
1885 return NULL;
1889 } // End anonymous namespace.
1891 namespace gold
1894 // Read an ELF file and return the appropriate instance of Object.
1896 Object*
1897 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1898 const unsigned char* p, section_offset_type bytes)
1900 if (bytes < elfcpp::EI_NIDENT)
1902 gold_error(_("%s: ELF file too short"), name.c_str());
1903 return NULL;
1906 int v = p[elfcpp::EI_VERSION];
1907 if (v != elfcpp::EV_CURRENT)
1909 if (v == elfcpp::EV_NONE)
1910 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1911 else
1912 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1913 return NULL;
1916 int c = p[elfcpp::EI_CLASS];
1917 if (c == elfcpp::ELFCLASSNONE)
1919 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1920 return NULL;
1922 else if (c != elfcpp::ELFCLASS32
1923 && c != elfcpp::ELFCLASS64)
1925 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1926 return NULL;
1929 int d = p[elfcpp::EI_DATA];
1930 if (d == elfcpp::ELFDATANONE)
1932 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1933 return NULL;
1935 else if (d != elfcpp::ELFDATA2LSB
1936 && d != elfcpp::ELFDATA2MSB)
1938 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1939 return NULL;
1942 bool big_endian = d == elfcpp::ELFDATA2MSB;
1944 if (c == elfcpp::ELFCLASS32)
1946 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1948 gold_error(_("%s: ELF file too short"), name.c_str());
1949 return NULL;
1951 if (big_endian)
1953 #ifdef HAVE_TARGET_32_BIG
1954 elfcpp::Ehdr<32, true> ehdr(p);
1955 return make_elf_sized_object<32, true>(name, input_file,
1956 offset, ehdr);
1957 #else
1958 gold_error(_("%s: not configured to support "
1959 "32-bit big-endian object"),
1960 name.c_str());
1961 return NULL;
1962 #endif
1964 else
1966 #ifdef HAVE_TARGET_32_LITTLE
1967 elfcpp::Ehdr<32, false> ehdr(p);
1968 return make_elf_sized_object<32, false>(name, input_file,
1969 offset, ehdr);
1970 #else
1971 gold_error(_("%s: not configured to support "
1972 "32-bit little-endian object"),
1973 name.c_str());
1974 return NULL;
1975 #endif
1978 else
1980 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
1982 gold_error(_("%s: ELF file too short"), name.c_str());
1983 return NULL;
1985 if (big_endian)
1987 #ifdef HAVE_TARGET_64_BIG
1988 elfcpp::Ehdr<64, true> ehdr(p);
1989 return make_elf_sized_object<64, true>(name, input_file,
1990 offset, ehdr);
1991 #else
1992 gold_error(_("%s: not configured to support "
1993 "64-bit big-endian object"),
1994 name.c_str());
1995 return NULL;
1996 #endif
1998 else
2000 #ifdef HAVE_TARGET_64_LITTLE
2001 elfcpp::Ehdr<64, false> ehdr(p);
2002 return make_elf_sized_object<64, false>(name, input_file,
2003 offset, ehdr);
2004 #else
2005 gold_error(_("%s: not configured to support "
2006 "64-bit little-endian object"),
2007 name.c_str());
2008 return NULL;
2009 #endif
2014 // Instantiate the templates we need.
2016 #ifdef HAVE_TARGET_32_LITTLE
2017 template
2018 void
2019 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2020 Read_symbols_data*);
2021 #endif
2023 #ifdef HAVE_TARGET_32_BIG
2024 template
2025 void
2026 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2027 Read_symbols_data*);
2028 #endif
2030 #ifdef HAVE_TARGET_64_LITTLE
2031 template
2032 void
2033 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2034 Read_symbols_data*);
2035 #endif
2037 #ifdef HAVE_TARGET_64_BIG
2038 template
2039 void
2040 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2041 Read_symbols_data*);
2042 #endif
2044 #ifdef HAVE_TARGET_32_LITTLE
2045 template
2046 class Sized_relobj<32, false>;
2047 #endif
2049 #ifdef HAVE_TARGET_32_BIG
2050 template
2051 class Sized_relobj<32, true>;
2052 #endif
2054 #ifdef HAVE_TARGET_64_LITTLE
2055 template
2056 class Sized_relobj<64, false>;
2057 #endif
2059 #ifdef HAVE_TARGET_64_BIG
2060 template
2061 class Sized_relobj<64, true>;
2062 #endif
2064 #ifdef HAVE_TARGET_32_LITTLE
2065 template
2066 struct Relocate_info<32, false>;
2067 #endif
2069 #ifdef HAVE_TARGET_32_BIG
2070 template
2071 struct Relocate_info<32, true>;
2072 #endif
2074 #ifdef HAVE_TARGET_64_LITTLE
2075 template
2076 struct Relocate_info<64, false>;
2077 #endif
2079 #ifdef HAVE_TARGET_64_BIG
2080 template
2081 struct Relocate_info<64, true>;
2082 #endif
2084 } // End namespace gold.