bfd/
[binutils.git] / gold / object.cc
blobb49bee17502b0387aadddd8fe56abe8b99897984
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 "reloc.h"
37 #include "object.h"
38 #include "dynobj.h"
40 namespace gold
43 // Class Xindex.
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>
50 void
51 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
53 if (!this->symtab_xindex_.empty())
54 return;
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();
61 while (i > 0)
63 --i;
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);
68 return;
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
77 // section headers.
79 template<int size, bool big_endian>
80 void
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;
86 if (pshdrs == NULL)
87 contents = object->section_contents(xindex_shndx, &bytecount, false);
88 else
90 const unsigned char* p = (pshdrs
91 + (xindex_shndx
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
109 // index.
111 unsigned int
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"),
117 symndx);
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"),
124 symndx, shndx);
125 return elfcpp::SHN_UNDEF;
127 return shndx;
130 // Class Object.
132 // Set the target based on fields in the ELF file header.
134 void
135 Object::set_target(int machine, int size, bool big_endian, int osabi,
136 int abiversion)
138 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
139 if (target == NULL)
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
147 // itself.
149 void
150 Object::error(const char* format, ...) const
152 va_list args;
153 va_start(args, format);
154 char* buf = NULL;
155 if (vasprintf(&buf, format, args) < 0)
156 gold_nomem();
157 va_end(args);
158 gold_error(_("%s: %s"), this->name().c_str(), buf);
159 free(buf);
162 // Return a view of the contents of a section.
164 const unsigned char*
165 Object::section_contents(unsigned int shndx, section_size_type* plen,
166 bool cache)
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>
177 void
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,
187 true, true);
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,
202 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.
209 bool
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
222 // simultaneously.
223 section_size_type len;
224 const unsigned char* contents = this->section_contents(shndx, &len,
225 false);
226 std::string warning(reinterpret_cast<const char*>(contents), len);
227 symtab->add_warning(name + warn_prefix_len, this, warning);
228 return true;
230 return false;
233 // Class Relobj.
235 // Return the output address of the input section SHNDX.
236 uint64_t
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,
251 off_t offset,
252 const elfcpp::Ehdr<size, big_endian>& ehdr)
253 : Relobj(name, input_file, offset),
254 elf_file_(this, ehdr),
255 symtab_shndx_(-1U),
256 local_symbol_count_(0),
257 output_local_symbol_count_(0),
258 output_local_dynsym_count_(0),
259 symbols_(),
260 local_symbol_offset_(0),
261 local_dynsym_offset_(0),
262 local_values_(),
263 local_got_offsets_(),
264 has_eh_frame_(false)
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>
277 void
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>
295 void
296 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
298 const unsigned int shnum = this->shnum();
299 this->symtab_shndx_ = 0;
300 if (shnum > 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;
308 while (i > 0)
310 --i;
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)
318 Xindex* xindex =
319 new Xindex(this->elf_file_.large_shndx_offset());
320 xindex->read_symtab_xindex<size, big_endian>(this,
321 xindex_shndx,
322 pshdrs);
323 this->set_xindex(xindex);
325 break;
328 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
329 // one. This will work if it follows the SHT_SYMTAB
330 // section.
331 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
333 xindex_shndx = i;
334 xindex_link = this->adjust_shndx(shdr.get_sh_link());
340 // Return the Xindex structure to use for object with lots of
341 // sections.
343 template<int size, bool big_endian>
344 Xindex*
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_);
350 return xindex;
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>
357 bool
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>
369 bool
370 Sized_relobj<size, big_endian>::find_eh_frame(
371 const unsigned char* pshdrs,
372 const char* names,
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()));
386 continue;
389 const char* name = names + shdr.get_sh_name();
390 if (strcmp(name, ".eh_frame") == 0)
391 return true;
394 return false;
397 // Read the sections and symbols from an object file.
399 template<int size, bool big_endian>
400 void
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;
417 sd->symbols = NULL;
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.
426 return;
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
439 // now.
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;
455 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
457 // Read the section header for the symbol names.
458 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
459 if (strtab_shndx >= this->shnum())
461 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
462 return;
464 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
465 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
467 this->error(_("symbol table name section has wrong type: %u"),
468 static_cast<unsigned int>(strtabshdr.get_sh_type()));
469 return;
472 // Read the symbol names.
473 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
474 strtabshdr.get_sh_size(),
475 false, true);
477 sd->symbols = fvsymtab;
478 sd->symbols_size = readsize;
479 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
480 sd->symbol_names = fvstrtab;
481 sd->symbol_names_size =
482 convert_to_section_size_type(strtabshdr.get_sh_size());
485 // Return the section index of symbol SYM. Set *VALUE to its value in
486 // the object file. Set *IS_ORDINARY if this is an ordinary section
487 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
488 // Note that for a symbol which is not defined in this object file,
489 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
490 // the final value of the symbol in the link.
492 template<int size, bool big_endian>
493 unsigned int
494 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
495 Address* value,
496 bool* is_ordinary)
498 section_size_type symbols_size;
499 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
500 &symbols_size,
501 false);
503 const size_t count = symbols_size / This::sym_size;
504 gold_assert(sym < count);
506 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
507 *value = elfsym.get_st_value();
509 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
512 // Return whether to include a section group in the link. LAYOUT is
513 // used to keep track of which section groups we have already seen.
514 // INDEX is the index of the section group and SHDR is the section
515 // header. If we do not want to include this group, we set bits in
516 // OMIT for each section which should be discarded.
518 template<int size, bool big_endian>
519 bool
520 Sized_relobj<size, big_endian>::include_section_group(
521 Symbol_table* symtab,
522 Layout* layout,
523 unsigned int index,
524 const char* name,
525 const unsigned char* shdrs,
526 const char* section_names,
527 section_size_type section_names_size,
528 std::vector<bool>* omit)
530 // Read the section contents.
531 typename This::Shdr shdr(shdrs + index * This::shdr_size);
532 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
533 shdr.get_sh_size(), true, false);
534 const elfcpp::Elf_Word* pword =
535 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
537 // The first word contains flags. We only care about COMDAT section
538 // groups. Other section groups are always included in the link
539 // just like ordinary sections.
540 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
542 // Look up the group signature, which is the name of a symbol. This
543 // is a lot of effort to go to to read a string. Why didn't they
544 // just have the group signature point into the string table, rather
545 // than indirect through a symbol?
547 // Get the appropriate symbol table header (this will normally be
548 // the single SHT_SYMTAB section, but in principle it need not be).
549 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
550 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
552 // Read the symbol table entry.
553 unsigned int symndx = shdr.get_sh_info();
554 if (symndx >= symshdr.get_sh_size() / This::sym_size)
556 this->error(_("section group %u info %u out of range"),
557 index, symndx);
558 return false;
560 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
561 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
562 false);
563 elfcpp::Sym<size, big_endian> sym(psym);
565 // Read the symbol table names.
566 section_size_type symnamelen;
567 const unsigned char* psymnamesu;
568 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
569 &symnamelen, true);
570 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
572 // Get the section group signature.
573 if (sym.get_st_name() >= symnamelen)
575 this->error(_("symbol %u name offset %u out of range"),
576 symndx, sym.get_st_name());
577 return false;
580 std::string signature(psymnames + sym.get_st_name());
582 // It seems that some versions of gas will create a section group
583 // associated with a section symbol, and then fail to give a name to
584 // the section symbol. In such a case, use the name of the section.
585 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
587 bool is_ordinary;
588 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
589 sym.get_st_shndx(),
590 &is_ordinary);
591 if (!is_ordinary || sym_shndx >= this->shnum())
593 this->error(_("symbol %u invalid section index %u"),
594 symndx, sym_shndx);
595 return false;
597 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
598 if (member_shdr.get_sh_name() < section_names_size)
599 signature = section_names + member_shdr.get_sh_name();
602 // Record this section group in the layout, and see whether we've already
603 // seen one with the same signature.
604 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
605 || layout->add_comdat(this, index, signature, true));
607 Relobj* kept_object = NULL;
608 Comdat_group* kept_group = NULL;
610 if (!include_group)
612 // This group is being discarded. Find the object and group
613 // that was kept in its place.
614 unsigned int kept_group_index = 0;
615 kept_object = layout->find_kept_object(signature, &kept_group_index);
616 if (kept_object != NULL)
617 kept_group = kept_object->find_comdat_group(kept_group_index);
619 else if (flags & elfcpp::GRP_COMDAT)
621 // This group is being kept. Create the table to map section names
622 // to section indexes and add it to the table of groups.
623 kept_group = new Comdat_group();
624 this->add_comdat_group(index, kept_group);
627 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
629 std::vector<unsigned int> shndxes;
630 bool relocate_group = include_group && parameters->options().relocatable();
631 if (relocate_group)
632 shndxes.reserve(count - 1);
634 for (size_t i = 1; i < count; ++i)
636 elfcpp::Elf_Word secnum =
637 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
639 if (relocate_group)
640 shndxes.push_back(secnum);
642 if (secnum >= this->shnum())
644 this->error(_("section %u in section group %u out of range"),
645 secnum, index);
646 continue;
649 // Check for an earlier section number, since we're going to get
650 // it wrong--we may have already decided to include the section.
651 if (secnum < index)
652 this->error(_("invalid section group %u refers to earlier section %u"),
653 index, secnum);
655 // Get the name of the member section.
656 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
657 if (member_shdr.get_sh_name() >= section_names_size)
659 // This is an error, but it will be diagnosed eventually
660 // in do_layout, so we don't need to do anything here but
661 // ignore it.
662 continue;
664 std::string mname(section_names + member_shdr.get_sh_name());
666 if (!include_group)
668 (*omit)[secnum] = true;
669 if (kept_group != NULL)
671 // Find the corresponding kept section, and store that info
672 // in the discarded section table.
673 Comdat_group::const_iterator p = kept_group->find(mname);
674 if (p != kept_group->end())
676 Kept_comdat_section* kept =
677 new Kept_comdat_section(kept_object, p->second);
678 this->set_kept_comdat_section(secnum, kept);
682 else if (flags & elfcpp::GRP_COMDAT)
684 // Add the section to the kept group table.
685 gold_assert(kept_group != NULL);
686 kept_group->insert(std::make_pair(mname, secnum));
690 if (relocate_group)
691 layout->layout_group(symtab, this, index, name, signature.c_str(),
692 shdr, flags, &shndxes);
694 return include_group;
697 // Whether to include a linkonce section in the link. NAME is the
698 // name of the section and SHDR is the section header.
700 // Linkonce sections are a GNU extension implemented in the original
701 // GNU linker before section groups were defined. The semantics are
702 // that we only include one linkonce section with a given name. The
703 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
704 // where T is the type of section and SYMNAME is the name of a symbol.
705 // In an attempt to make linkonce sections interact well with section
706 // groups, we try to identify SYMNAME and use it like a section group
707 // signature. We want to block section groups with that signature,
708 // but not other linkonce sections with that signature. We also use
709 // the full name of the linkonce section as a normal section group
710 // signature.
712 template<int size, bool big_endian>
713 bool
714 Sized_relobj<size, big_endian>::include_linkonce_section(
715 Layout* layout,
716 unsigned int index,
717 const char* name,
718 const elfcpp::Shdr<size, big_endian>&)
720 // In general the symbol name we want will be the string following
721 // the last '.'. However, we have to handle the case of
722 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
723 // some versions of gcc. So we use a heuristic: if the name starts
724 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
725 // we look for the last '.'. We can't always simply skip
726 // ".gnu.linkonce.X", because we have to deal with cases like
727 // ".gnu.linkonce.d.rel.ro.local".
728 const char* const linkonce_t = ".gnu.linkonce.t.";
729 const char* symname;
730 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
731 symname = name + strlen(linkonce_t);
732 else
733 symname = strrchr(name, '.') + 1;
734 std::string sig1(symname);
735 std::string sig2(name);
736 bool include1 = layout->add_comdat(this, index, sig1, false);
737 bool include2 = layout->add_comdat(this, index, sig2, true);
739 if (!include2)
741 // The section is being discarded on the basis of its section
742 // name (i.e., the kept section was also a linkonce section).
743 // In this case, the section index stored with the layout object
744 // is the linkonce section that was kept.
745 unsigned int kept_group_index = 0;
746 Relobj* kept_object = layout->find_kept_object(sig2, &kept_group_index);
747 if (kept_object != NULL)
749 Kept_comdat_section* kept =
750 new Kept_comdat_section(kept_object, kept_group_index);
751 this->set_kept_comdat_section(index, kept);
754 else if (!include1)
756 // The section is being discarded on the basis of its symbol
757 // name. This means that the corresponding kept section was
758 // part of a comdat group, and it will be difficult to identify
759 // the specific section within that group that corresponds to
760 // this linkonce section. We'll handle the simple case where
761 // the group has only one member section. Otherwise, it's not
762 // worth the effort.
763 unsigned int kept_group_index = 0;
764 Relobj* kept_object = layout->find_kept_object(sig1, &kept_group_index);
765 if (kept_object != NULL)
767 Comdat_group* kept_group =
768 kept_object->find_comdat_group(kept_group_index);
769 if (kept_group != NULL && kept_group->size() == 1)
771 Comdat_group::const_iterator p = kept_group->begin();
772 gold_assert(p != kept_group->end());
773 Kept_comdat_section* kept =
774 new Kept_comdat_section(kept_object, p->second);
775 this->set_kept_comdat_section(index, kept);
780 return include1 && include2;
783 // Lay out the input sections. We walk through the sections and check
784 // whether they should be included in the link. If they should, we
785 // pass them to the Layout object, which will return an output section
786 // and an offset.
788 template<int size, bool big_endian>
789 void
790 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
791 Layout* layout,
792 Read_symbols_data* sd)
794 const unsigned int shnum = this->shnum();
795 if (shnum == 0)
796 return;
798 // Get the section headers.
799 const unsigned char* shdrs = sd->section_headers->data();
800 const unsigned char* pshdrs;
802 // Get the section names.
803 const unsigned char* pnamesu = sd->section_names->data();
804 const char* pnames = reinterpret_cast<const char*>(pnamesu);
806 // For each section, record the index of the reloc section if any.
807 // Use 0 to mean that there is no reloc section, -1U to mean that
808 // there is more than one.
809 std::vector<unsigned int> reloc_shndx(shnum, 0);
810 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
811 // Skip the first, dummy, section.
812 pshdrs = shdrs + This::shdr_size;
813 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
815 typename This::Shdr shdr(pshdrs);
817 unsigned int sh_type = shdr.get_sh_type();
818 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
820 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
821 if (target_shndx == 0 || target_shndx >= shnum)
823 this->error(_("relocation section %u has bad info %u"),
824 i, target_shndx);
825 continue;
828 if (reloc_shndx[target_shndx] != 0)
829 reloc_shndx[target_shndx] = -1U;
830 else
832 reloc_shndx[target_shndx] = i;
833 reloc_type[target_shndx] = sh_type;
838 std::vector<Map_to_output>& map_sections(this->map_to_output());
839 map_sections.resize(shnum);
841 // If we are only linking for symbols, then there is nothing else to
842 // do here.
843 if (this->input_file()->just_symbols())
845 delete sd->section_headers;
846 sd->section_headers = NULL;
847 delete sd->section_names;
848 sd->section_names = NULL;
849 return;
852 // Whether we've seen a .note.GNU-stack section.
853 bool seen_gnu_stack = false;
854 // The flags of a .note.GNU-stack section.
855 uint64_t gnu_stack_flags = 0;
857 // Keep track of which sections to omit.
858 std::vector<bool> omit(shnum, false);
860 // Keep track of reloc sections when emitting relocations.
861 const bool relocatable = parameters->options().relocatable();
862 const bool emit_relocs = (relocatable
863 || parameters->options().emit_relocs());
864 std::vector<unsigned int> reloc_sections;
866 // Keep track of .eh_frame sections.
867 std::vector<unsigned int> eh_frame_sections;
869 // Skip the first, dummy, section.
870 pshdrs = shdrs + This::shdr_size;
871 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
873 typename This::Shdr shdr(pshdrs);
875 if (shdr.get_sh_name() >= sd->section_names_size)
877 this->error(_("bad section name offset for section %u: %lu"),
878 i, static_cast<unsigned long>(shdr.get_sh_name()));
879 return;
882 const char* name = pnames + shdr.get_sh_name();
884 if (this->handle_gnu_warning_section(name, i, symtab))
886 if (!relocatable)
887 omit[i] = true;
890 // The .note.GNU-stack section is special. It gives the
891 // protection flags that this object file requires for the stack
892 // in memory.
893 if (strcmp(name, ".note.GNU-stack") == 0)
895 seen_gnu_stack = true;
896 gnu_stack_flags |= shdr.get_sh_flags();
897 omit[i] = true;
900 bool discard = omit[i];
901 if (!discard)
903 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
905 if (!this->include_section_group(symtab, layout, i, name, shdrs,
906 pnames, sd->section_names_size,
907 &omit))
908 discard = true;
910 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
911 && Layout::is_linkonce(name))
913 if (!this->include_linkonce_section(layout, i, name, shdr))
914 discard = true;
918 if (discard)
920 // Do not include this section in the link.
921 map_sections[i].output_section = NULL;
922 continue;
925 // When doing a relocatable link we are going to copy input
926 // reloc sections into the output. We only want to copy the
927 // ones associated with sections which are not being discarded.
928 // However, we don't know that yet for all sections. So save
929 // reloc sections and process them later.
930 if (emit_relocs
931 && (shdr.get_sh_type() == elfcpp::SHT_REL
932 || shdr.get_sh_type() == elfcpp::SHT_RELA))
934 reloc_sections.push_back(i);
935 continue;
938 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
939 continue;
941 // The .eh_frame section is special. It holds exception frame
942 // information that we need to read in order to generate the
943 // exception frame header. We process these after all the other
944 // sections so that the exception frame reader can reliably
945 // determine which sections are being discarded, and discard the
946 // corresponding information.
947 if (!relocatable
948 && strcmp(name, ".eh_frame") == 0
949 && this->check_eh_frame_flags(&shdr))
951 eh_frame_sections.push_back(i);
952 continue;
955 off_t offset;
956 Output_section* os = layout->layout(this, i, name, shdr,
957 reloc_shndx[i], reloc_type[i],
958 &offset);
960 map_sections[i].output_section = os;
961 map_sections[i].offset = offset;
963 // If this section requires special handling, and if there are
964 // relocs that apply to it, then we must do the special handling
965 // before we apply the relocs.
966 if (offset == -1 && reloc_shndx[i] != 0)
967 this->set_relocs_must_follow_section_writes();
970 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
972 // When doing a relocatable link handle the reloc sections at the
973 // end.
974 if (emit_relocs)
975 this->size_relocatable_relocs();
976 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
977 p != reloc_sections.end();
978 ++p)
980 unsigned int i = *p;
981 const unsigned char* pshdr;
982 pshdr = sd->section_headers->data() + i * This::shdr_size;
983 typename This::Shdr shdr(pshdr);
985 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
986 if (data_shndx >= shnum)
988 // We already warned about this above.
989 continue;
992 Output_section* data_section = map_sections[data_shndx].output_section;
993 if (data_section == NULL)
995 map_sections[i].output_section = NULL;
996 continue;
999 Relocatable_relocs* rr = new Relocatable_relocs();
1000 this->set_relocatable_relocs(i, rr);
1002 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1003 rr);
1004 map_sections[i].output_section = os;
1005 map_sections[i].offset = -1;
1008 // Handle the .eh_frame sections at the end.
1009 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1010 p != eh_frame_sections.end();
1011 ++p)
1013 gold_assert(this->has_eh_frame_);
1014 gold_assert(sd->external_symbols_offset != 0);
1016 unsigned int i = *p;
1017 const unsigned char *pshdr;
1018 pshdr = sd->section_headers->data() + i * This::shdr_size;
1019 typename This::Shdr shdr(pshdr);
1021 off_t offset;
1022 Output_section* os = layout->layout_eh_frame(this,
1023 sd->symbols->data(),
1024 sd->symbols_size,
1025 sd->symbol_names->data(),
1026 sd->symbol_names_size,
1027 i, shdr,
1028 reloc_shndx[i],
1029 reloc_type[i],
1030 &offset);
1031 map_sections[i].output_section = os;
1032 map_sections[i].offset = offset;
1034 // If this section requires special handling, and if there are
1035 // relocs that apply to it, then we must do the special handling
1036 // before we apply the relocs.
1037 if (offset == -1 && reloc_shndx[i] != 0)
1038 this->set_relocs_must_follow_section_writes();
1041 delete sd->section_headers;
1042 sd->section_headers = NULL;
1043 delete sd->section_names;
1044 sd->section_names = NULL;
1047 // Add the symbols to the symbol table.
1049 template<int size, bool big_endian>
1050 void
1051 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1052 Read_symbols_data* sd)
1054 if (sd->symbols == NULL)
1056 gold_assert(sd->symbol_names == NULL);
1057 return;
1060 const int sym_size = This::sym_size;
1061 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1062 / sym_size);
1063 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1065 this->error(_("size of symbols is not multiple of symbol size"));
1066 return;
1069 this->symbols_.resize(symcount);
1071 const char* sym_names =
1072 reinterpret_cast<const char*>(sd->symbol_names->data());
1073 symtab->add_from_relobj(this,
1074 sd->symbols->data() + sd->external_symbols_offset,
1075 symcount, this->local_symbol_count_,
1076 sym_names, sd->symbol_names_size,
1077 &this->symbols_);
1079 delete sd->symbols;
1080 sd->symbols = NULL;
1081 delete sd->symbol_names;
1082 sd->symbol_names = NULL;
1085 // First pass over the local symbols. Here we add their names to
1086 // *POOL and *DYNPOOL, and we store the symbol value in
1087 // THIS->LOCAL_VALUES_. This function is always called from a
1088 // singleton thread. This is followed by a call to
1089 // finalize_local_symbols.
1091 template<int size, bool big_endian>
1092 void
1093 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1094 Stringpool* dynpool)
1096 gold_assert(this->symtab_shndx_ != -1U);
1097 if (this->symtab_shndx_ == 0)
1099 // This object has no symbols. Weird but legal.
1100 return;
1103 // Read the symbol table section header.
1104 const unsigned int symtab_shndx = this->symtab_shndx_;
1105 typename This::Shdr symtabshdr(this,
1106 this->elf_file_.section_header(symtab_shndx));
1107 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1109 // Read the local symbols.
1110 const int sym_size = This::sym_size;
1111 const unsigned int loccount = this->local_symbol_count_;
1112 gold_assert(loccount == symtabshdr.get_sh_info());
1113 off_t locsize = loccount * sym_size;
1114 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1115 locsize, true, true);
1117 // Read the symbol names.
1118 const unsigned int strtab_shndx =
1119 this->adjust_shndx(symtabshdr.get_sh_link());
1120 section_size_type strtab_size;
1121 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1122 &strtab_size,
1123 true);
1124 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1126 // Loop over the local symbols.
1128 const std::vector<Map_to_output>& mo(this->map_to_output());
1129 unsigned int shnum = this->shnum();
1130 unsigned int count = 0;
1131 unsigned int dyncount = 0;
1132 // Skip the first, dummy, symbol.
1133 psyms += sym_size;
1134 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1136 elfcpp::Sym<size, big_endian> sym(psyms);
1138 Symbol_value<size>& lv(this->local_values_[i]);
1140 bool is_ordinary;
1141 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1142 &is_ordinary);
1143 lv.set_input_shndx(shndx, is_ordinary);
1145 if (sym.get_st_type() == elfcpp::STT_SECTION)
1146 lv.set_is_section_symbol();
1147 else if (sym.get_st_type() == elfcpp::STT_TLS)
1148 lv.set_is_tls_symbol();
1150 // Save the input symbol value for use in do_finalize_local_symbols().
1151 lv.set_input_value(sym.get_st_value());
1153 // Decide whether this symbol should go into the output file.
1155 if (shndx < shnum && mo[shndx].output_section == NULL)
1157 lv.set_no_output_symtab_entry();
1158 gold_assert(!lv.needs_output_dynsym_entry());
1159 continue;
1162 if (sym.get_st_type() == elfcpp::STT_SECTION)
1164 lv.set_no_output_symtab_entry();
1165 gold_assert(!lv.needs_output_dynsym_entry());
1166 continue;
1169 if (sym.get_st_name() >= strtab_size)
1171 this->error(_("local symbol %u section name out of range: %u >= %u"),
1172 i, sym.get_st_name(),
1173 static_cast<unsigned int>(strtab_size));
1174 lv.set_no_output_symtab_entry();
1175 continue;
1178 // Add the symbol to the symbol table string pool.
1179 const char* name = pnames + sym.get_st_name();
1180 pool->add(name, true, NULL);
1181 ++count;
1183 // If needed, add the symbol to the dynamic symbol table string pool.
1184 if (lv.needs_output_dynsym_entry())
1186 dynpool->add(name, true, NULL);
1187 ++dyncount;
1191 this->output_local_symbol_count_ = count;
1192 this->output_local_dynsym_count_ = dyncount;
1195 // Finalize the local symbols. Here we set the final value in
1196 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1197 // This function is always called from a singleton thread. The actual
1198 // output of the local symbols will occur in a separate task.
1200 template<int size, bool big_endian>
1201 unsigned int
1202 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1203 off_t off)
1205 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1207 const unsigned int loccount = this->local_symbol_count_;
1208 this->local_symbol_offset_ = off;
1210 const std::vector<Map_to_output>& mo(this->map_to_output());
1211 unsigned int shnum = this->shnum();
1213 for (unsigned int i = 1; i < loccount; ++i)
1215 Symbol_value<size>& lv(this->local_values_[i]);
1217 bool is_ordinary;
1218 unsigned int shndx = lv.input_shndx(&is_ordinary);
1220 // Set the output symbol value.
1222 if (!is_ordinary)
1224 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1225 lv.set_output_value(lv.input_value());
1226 else
1228 this->error(_("unknown section index %u for local symbol %u"),
1229 shndx, i);
1230 lv.set_output_value(0);
1233 else
1235 if (shndx >= shnum)
1237 this->error(_("local symbol %u section index %u out of range"),
1238 i, shndx);
1239 shndx = 0;
1242 Output_section* os = mo[shndx].output_section;
1244 if (os == NULL)
1246 // This local symbol belongs to a section we are discarding.
1247 // In some cases when applying relocations later, we will
1248 // attempt to match it to the corresponding kept section,
1249 // so we leave the input value unchanged here.
1250 continue;
1252 else if (mo[shndx].offset == -1)
1254 // This is a SHF_MERGE section or one which otherwise
1255 // requires special handling. We get the output address
1256 // of the start of the merged section. If this is not a
1257 // section symbol, we can then determine the final
1258 // value. If it is a section symbol, we can not, as in
1259 // that case we have to consider the addend to determine
1260 // the value to use in a relocation.
1261 if (!lv.is_section_symbol())
1262 lv.set_output_value(os->output_address(this, shndx,
1263 lv.input_value()));
1264 else
1266 section_offset_type start =
1267 os->starting_output_address(this, shndx);
1268 Merged_symbol_value<size>* msv =
1269 new Merged_symbol_value<size>(lv.input_value(), start);
1270 lv.set_merged_symbol_value(msv);
1273 else if (lv.is_tls_symbol())
1274 lv.set_output_value(os->tls_offset()
1275 + mo[shndx].offset
1276 + lv.input_value());
1277 else
1278 lv.set_output_value(os->address()
1279 + mo[shndx].offset
1280 + lv.input_value());
1283 if (lv.needs_output_symtab_entry())
1285 lv.set_output_symtab_index(index);
1286 ++index;
1289 return index;
1292 // Set the output dynamic symbol table indexes for the local variables.
1294 template<int size, bool big_endian>
1295 unsigned int
1296 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1298 const unsigned int loccount = this->local_symbol_count_;
1299 for (unsigned int i = 1; i < loccount; ++i)
1301 Symbol_value<size>& lv(this->local_values_[i]);
1302 if (lv.needs_output_dynsym_entry())
1304 lv.set_output_dynsym_index(index);
1305 ++index;
1308 return index;
1311 // Set the offset where local dynamic symbol information will be stored.
1312 // Returns the count of local symbols contributed to the symbol table by
1313 // this object.
1315 template<int size, bool big_endian>
1316 unsigned int
1317 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1319 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1320 this->local_dynsym_offset_ = off;
1321 return this->output_local_dynsym_count_;
1324 // Write out the local symbols.
1326 template<int size, bool big_endian>
1327 void
1328 Sized_relobj<size, big_endian>::write_local_symbols(
1329 Output_file* of,
1330 const Stringpool* sympool,
1331 const Stringpool* dynpool,
1332 Output_symtab_xindex* symtab_xindex,
1333 Output_symtab_xindex* dynsym_xindex)
1335 if (parameters->options().strip_all()
1336 && this->output_local_dynsym_count_ == 0)
1337 return;
1339 gold_assert(this->symtab_shndx_ != -1U);
1340 if (this->symtab_shndx_ == 0)
1342 // This object has no symbols. Weird but legal.
1343 return;
1346 // Read the symbol table section header.
1347 const unsigned int symtab_shndx = this->symtab_shndx_;
1348 typename This::Shdr symtabshdr(this,
1349 this->elf_file_.section_header(symtab_shndx));
1350 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1351 const unsigned int loccount = this->local_symbol_count_;
1352 gold_assert(loccount == symtabshdr.get_sh_info());
1354 // Read the local symbols.
1355 const int sym_size = This::sym_size;
1356 off_t locsize = loccount * sym_size;
1357 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1358 locsize, true, false);
1360 // Read the symbol names.
1361 const unsigned int strtab_shndx =
1362 this->adjust_shndx(symtabshdr.get_sh_link());
1363 section_size_type strtab_size;
1364 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1365 &strtab_size,
1366 false);
1367 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1369 // Get views into the output file for the portions of the symbol table
1370 // and the dynamic symbol table that we will be writing.
1371 off_t output_size = this->output_local_symbol_count_ * sym_size;
1372 unsigned char* oview = NULL;
1373 if (output_size > 0)
1374 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1376 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1377 unsigned char* dyn_oview = NULL;
1378 if (dyn_output_size > 0)
1379 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1380 dyn_output_size);
1382 const std::vector<Map_to_output>& mo(this->map_to_output());
1384 gold_assert(this->local_values_.size() == loccount);
1386 unsigned char* ov = oview;
1387 unsigned char* dyn_ov = dyn_oview;
1388 psyms += sym_size;
1389 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1391 elfcpp::Sym<size, big_endian> isym(psyms);
1393 Symbol_value<size>& lv(this->local_values_[i]);
1395 bool is_ordinary;
1396 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1397 &is_ordinary);
1398 if (is_ordinary)
1400 gold_assert(st_shndx < mo.size());
1401 if (mo[st_shndx].output_section == NULL)
1402 continue;
1403 st_shndx = mo[st_shndx].output_section->out_shndx();
1404 if (st_shndx >= elfcpp::SHN_LORESERVE)
1406 if (lv.needs_output_symtab_entry())
1407 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1408 if (lv.needs_output_dynsym_entry())
1409 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1410 st_shndx = elfcpp::SHN_XINDEX;
1414 // Write the symbol to the output symbol table.
1415 if (!parameters->options().strip_all()
1416 && lv.needs_output_symtab_entry())
1418 elfcpp::Sym_write<size, big_endian> osym(ov);
1420 gold_assert(isym.get_st_name() < strtab_size);
1421 const char* name = pnames + isym.get_st_name();
1422 osym.put_st_name(sympool->get_offset(name));
1423 osym.put_st_value(this->local_values_[i].value(this, 0));
1424 osym.put_st_size(isym.get_st_size());
1425 osym.put_st_info(isym.get_st_info());
1426 osym.put_st_other(isym.get_st_other());
1427 osym.put_st_shndx(st_shndx);
1429 ov += sym_size;
1432 // Write the symbol to the output dynamic symbol table.
1433 if (lv.needs_output_dynsym_entry())
1435 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1436 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1438 gold_assert(isym.get_st_name() < strtab_size);
1439 const char* name = pnames + isym.get_st_name();
1440 osym.put_st_name(dynpool->get_offset(name));
1441 osym.put_st_value(this->local_values_[i].value(this, 0));
1442 osym.put_st_size(isym.get_st_size());
1443 osym.put_st_info(isym.get_st_info());
1444 osym.put_st_other(isym.get_st_other());
1445 osym.put_st_shndx(st_shndx);
1447 dyn_ov += sym_size;
1452 if (output_size > 0)
1454 gold_assert(ov - oview == output_size);
1455 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1458 if (dyn_output_size > 0)
1460 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1461 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1462 dyn_oview);
1466 // Set *INFO to symbolic information about the offset OFFSET in the
1467 // section SHNDX. Return true if we found something, false if we
1468 // found nothing.
1470 template<int size, bool big_endian>
1471 bool
1472 Sized_relobj<size, big_endian>::get_symbol_location_info(
1473 unsigned int shndx,
1474 off_t offset,
1475 Symbol_location_info* info)
1477 if (this->symtab_shndx_ == 0)
1478 return false;
1480 section_size_type symbols_size;
1481 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1482 &symbols_size,
1483 false);
1485 unsigned int symbol_names_shndx =
1486 this->adjust_shndx(this->section_link(this->symtab_shndx_));
1487 section_size_type names_size;
1488 const unsigned char* symbol_names_u =
1489 this->section_contents(symbol_names_shndx, &names_size, false);
1490 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1492 const int sym_size = This::sym_size;
1493 const size_t count = symbols_size / sym_size;
1495 const unsigned char* p = symbols;
1496 for (size_t i = 0; i < count; ++i, p += sym_size)
1498 elfcpp::Sym<size, big_endian> sym(p);
1500 if (sym.get_st_type() == elfcpp::STT_FILE)
1502 if (sym.get_st_name() >= names_size)
1503 info->source_file = "(invalid)";
1504 else
1505 info->source_file = symbol_names + sym.get_st_name();
1506 continue;
1509 bool is_ordinary;
1510 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1511 &is_ordinary);
1512 if (is_ordinary
1513 && st_shndx == shndx
1514 && static_cast<off_t>(sym.get_st_value()) <= offset
1515 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1516 > offset))
1518 if (sym.get_st_name() > names_size)
1519 info->enclosing_symbol_name = "(invalid)";
1520 else
1522 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1523 if (parameters->options().do_demangle())
1525 char* demangled_name = cplus_demangle(
1526 info->enclosing_symbol_name.c_str(),
1527 DMGL_ANSI | DMGL_PARAMS);
1528 if (demangled_name != NULL)
1530 info->enclosing_symbol_name.assign(demangled_name);
1531 free(demangled_name);
1535 return true;
1539 return false;
1542 // Look for a kept section corresponding to the given discarded section,
1543 // and return its output address. This is used only for relocations in
1544 // debugging sections. If we can't find the kept section, return 0.
1546 template<int size, bool big_endian>
1547 typename Sized_relobj<size, big_endian>::Address
1548 Sized_relobj<size, big_endian>::map_to_kept_section(
1549 unsigned int shndx,
1550 bool* found) const
1552 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1553 if (kept != NULL)
1555 gold_assert(kept->object_ != NULL);
1556 *found = true;
1557 return (static_cast<Address>
1558 (kept->object_->output_section_address(kept->shndx_)));
1560 *found = false;
1561 return 0;
1564 // Input_objects methods.
1566 // Add a regular relocatable object to the list. Return false if this
1567 // object should be ignored.
1569 bool
1570 Input_objects::add_object(Object* obj)
1572 // Set the global target from the first object file we recognize.
1573 Target* target = obj->target();
1574 if (!parameters->target_valid())
1575 set_parameters_target(target);
1576 else if (target != &parameters->target())
1578 obj->error(_("incompatible target"));
1579 return false;
1582 // Print the filename if the -t/--trace option is selected.
1583 if (parameters->options().trace())
1584 gold_info("%s", obj->name().c_str());
1586 if (!obj->is_dynamic())
1587 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1588 else
1590 // See if this is a duplicate SONAME.
1591 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1592 const char* soname = dynobj->soname();
1594 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1595 this->sonames_.insert(soname);
1596 if (!ins.second)
1598 // We have already seen a dynamic object with this soname.
1599 return false;
1602 this->dynobj_list_.push_back(dynobj);
1604 // If this is -lc, remember the directory in which we found it.
1605 // We use this when issuing warnings about undefined symbols: as
1606 // a heuristic, we don't warn about system libraries found in
1607 // the same directory as -lc.
1608 if (strncmp(soname, "libc.so", 7) == 0)
1610 const char* object_name = dynobj->name().c_str();
1611 const char* base = lbasename(object_name);
1612 if (base != object_name)
1613 this->system_library_directory_.assign(object_name,
1614 base - 1 - object_name);
1618 return true;
1621 // Return whether an object was found in the system library directory.
1623 bool
1624 Input_objects::found_in_system_library_directory(const Object* object) const
1626 return (!this->system_library_directory_.empty()
1627 && object->name().compare(0,
1628 this->system_library_directory_.size(),
1629 this->system_library_directory_) == 0);
1632 // For each dynamic object, record whether we've seen all of its
1633 // explicit dependencies.
1635 void
1636 Input_objects::check_dynamic_dependencies() const
1638 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1639 p != this->dynobj_list_.end();
1640 ++p)
1642 const Dynobj::Needed& needed((*p)->needed());
1643 bool found_all = true;
1644 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1645 pneeded != needed.end();
1646 ++pneeded)
1648 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1650 found_all = false;
1651 break;
1654 (*p)->set_has_unknown_needed_entries(!found_all);
1658 // Relocate_info methods.
1660 // Return a string describing the location of a relocation. This is
1661 // only used in error messages.
1663 template<int size, bool big_endian>
1664 std::string
1665 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1667 // See if we can get line-number information from debugging sections.
1668 std::string filename;
1669 std::string file_and_lineno; // Better than filename-only, if available.
1671 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1672 // This will be "" if we failed to parse the debug info for any reason.
1673 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1675 std::string ret(this->object->name());
1676 ret += ':';
1677 Symbol_location_info info;
1678 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1680 ret += " in function ";
1681 ret += info.enclosing_symbol_name;
1682 ret += ":";
1683 filename = info.source_file;
1686 if (!file_and_lineno.empty())
1687 ret += file_and_lineno;
1688 else
1690 if (!filename.empty())
1691 ret += filename;
1692 ret += "(";
1693 ret += this->object->section_name(this->data_shndx);
1694 char buf[100];
1695 // Offsets into sections have to be positive.
1696 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1697 ret += buf;
1698 ret += ")";
1700 return ret;
1703 } // End namespace gold.
1705 namespace
1708 using namespace gold;
1710 // Read an ELF file with the header and return the appropriate
1711 // instance of Object.
1713 template<int size, bool big_endian>
1714 Object*
1715 make_elf_sized_object(const std::string& name, Input_file* input_file,
1716 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1718 int et = ehdr.get_e_type();
1719 if (et == elfcpp::ET_REL)
1721 Sized_relobj<size, big_endian>* obj =
1722 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1723 obj->setup(ehdr);
1724 return obj;
1726 else if (et == elfcpp::ET_DYN)
1728 Sized_dynobj<size, big_endian>* obj =
1729 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1730 obj->setup(ehdr);
1731 return obj;
1733 else
1735 gold_error(_("%s: unsupported ELF file type %d"),
1736 name.c_str(), et);
1737 return NULL;
1741 } // End anonymous namespace.
1743 namespace gold
1746 // Read an ELF file and return the appropriate instance of Object.
1748 Object*
1749 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1750 const unsigned char* p, section_offset_type bytes)
1752 if (bytes < elfcpp::EI_NIDENT)
1754 gold_error(_("%s: ELF file too short"), name.c_str());
1755 return NULL;
1758 int v = p[elfcpp::EI_VERSION];
1759 if (v != elfcpp::EV_CURRENT)
1761 if (v == elfcpp::EV_NONE)
1762 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1763 else
1764 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1765 return NULL;
1768 int c = p[elfcpp::EI_CLASS];
1769 if (c == elfcpp::ELFCLASSNONE)
1771 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1772 return NULL;
1774 else if (c != elfcpp::ELFCLASS32
1775 && c != elfcpp::ELFCLASS64)
1777 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1778 return NULL;
1781 int d = p[elfcpp::EI_DATA];
1782 if (d == elfcpp::ELFDATANONE)
1784 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1785 return NULL;
1787 else if (d != elfcpp::ELFDATA2LSB
1788 && d != elfcpp::ELFDATA2MSB)
1790 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1791 return NULL;
1794 bool big_endian = d == elfcpp::ELFDATA2MSB;
1796 if (c == elfcpp::ELFCLASS32)
1798 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1800 gold_error(_("%s: ELF file too short"), name.c_str());
1801 return NULL;
1803 if (big_endian)
1805 #ifdef HAVE_TARGET_32_BIG
1806 elfcpp::Ehdr<32, true> ehdr(p);
1807 return make_elf_sized_object<32, true>(name, input_file,
1808 offset, ehdr);
1809 #else
1810 gold_error(_("%s: not configured to support "
1811 "32-bit big-endian object"),
1812 name.c_str());
1813 return NULL;
1814 #endif
1816 else
1818 #ifdef HAVE_TARGET_32_LITTLE
1819 elfcpp::Ehdr<32, false> ehdr(p);
1820 return make_elf_sized_object<32, false>(name, input_file,
1821 offset, ehdr);
1822 #else
1823 gold_error(_("%s: not configured to support "
1824 "32-bit little-endian object"),
1825 name.c_str());
1826 return NULL;
1827 #endif
1830 else
1832 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
1834 gold_error(_("%s: ELF file too short"), name.c_str());
1835 return NULL;
1837 if (big_endian)
1839 #ifdef HAVE_TARGET_64_BIG
1840 elfcpp::Ehdr<64, true> ehdr(p);
1841 return make_elf_sized_object<64, true>(name, input_file,
1842 offset, ehdr);
1843 #else
1844 gold_error(_("%s: not configured to support "
1845 "64-bit big-endian object"),
1846 name.c_str());
1847 return NULL;
1848 #endif
1850 else
1852 #ifdef HAVE_TARGET_64_LITTLE
1853 elfcpp::Ehdr<64, false> ehdr(p);
1854 return make_elf_sized_object<64, false>(name, input_file,
1855 offset, ehdr);
1856 #else
1857 gold_error(_("%s: not configured to support "
1858 "64-bit little-endian object"),
1859 name.c_str());
1860 return NULL;
1861 #endif
1866 // Instantiate the templates we need.
1868 #ifdef HAVE_TARGET_32_LITTLE
1869 template
1870 void
1871 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1872 Read_symbols_data*);
1873 #endif
1875 #ifdef HAVE_TARGET_32_BIG
1876 template
1877 void
1878 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1879 Read_symbols_data*);
1880 #endif
1882 #ifdef HAVE_TARGET_64_LITTLE
1883 template
1884 void
1885 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1886 Read_symbols_data*);
1887 #endif
1889 #ifdef HAVE_TARGET_64_BIG
1890 template
1891 void
1892 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1893 Read_symbols_data*);
1894 #endif
1896 #ifdef HAVE_TARGET_32_LITTLE
1897 template
1898 class Sized_relobj<32, false>;
1899 #endif
1901 #ifdef HAVE_TARGET_32_BIG
1902 template
1903 class Sized_relobj<32, true>;
1904 #endif
1906 #ifdef HAVE_TARGET_64_LITTLE
1907 template
1908 class Sized_relobj<64, false>;
1909 #endif
1911 #ifdef HAVE_TARGET_64_BIG
1912 template
1913 class Sized_relobj<64, true>;
1914 #endif
1916 #ifdef HAVE_TARGET_32_LITTLE
1917 template
1918 struct Relocate_info<32, false>;
1919 #endif
1921 #ifdef HAVE_TARGET_32_BIG
1922 template
1923 struct Relocate_info<32, true>;
1924 #endif
1926 #ifdef HAVE_TARGET_64_LITTLE
1927 template
1928 struct Relocate_info<64, false>;
1929 #endif
1931 #ifdef HAVE_TARGET_64_BIG
1932 template
1933 struct Relocate_info<64, true>;
1934 #endif
1936 } // End namespace gold.