* elf64-ppc.c (ha_reloc_match): Allow matches to other than r2.
[binutils.git] / gold / object.cc
blob1bf73677f62e93e4f7dad294ed13f311784ebf54
1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 "gc.h"
32 #include "target-select.h"
33 #include "dwarf_reader.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "symtab.h"
37 #include "cref.h"
38 #include "reloc.h"
39 #include "object.h"
40 #include "dynobj.h"
41 #include "plugin.h"
42 #include "compressed_output.h"
44 namespace gold
47 // Struct Read_symbols_data.
49 // Destroy any remaining File_view objects.
51 Read_symbols_data::~Read_symbols_data()
53 if (this->section_headers != NULL)
54 delete this->section_headers;
55 if (this->section_names != NULL)
56 delete this->section_names;
57 if (this->symbols != NULL)
58 delete this->symbols;
59 if (this->symbol_names != NULL)
60 delete this->symbol_names;
61 if (this->versym != NULL)
62 delete this->versym;
63 if (this->verdef != NULL)
64 delete this->verdef;
65 if (this->verneed != NULL)
66 delete this->verneed;
69 // Class Xindex.
71 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
72 // section and read it in. SYMTAB_SHNDX is the index of the symbol
73 // table we care about.
75 template<int size, bool big_endian>
76 void
77 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
79 if (!this->symtab_xindex_.empty())
80 return;
82 gold_assert(symtab_shndx != 0);
84 // Look through the sections in reverse order, on the theory that it
85 // is more likely to be near the end than the beginning.
86 unsigned int i = object->shnum();
87 while (i > 0)
89 --i;
90 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
91 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
93 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
94 return;
98 object->error(_("missing SHT_SYMTAB_SHNDX section"));
101 // Read in the symtab_xindex_ array, given the section index of the
102 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
103 // section headers.
105 template<int size, bool big_endian>
106 void
107 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
108 const unsigned char* pshdrs)
110 section_size_type bytecount;
111 const unsigned char* contents;
112 if (pshdrs == NULL)
113 contents = object->section_contents(xindex_shndx, &bytecount, false);
114 else
116 const unsigned char* p = (pshdrs
117 + (xindex_shndx
118 * elfcpp::Elf_sizes<size>::shdr_size));
119 typename elfcpp::Shdr<size, big_endian> shdr(p);
120 bytecount = convert_to_section_size_type(shdr.get_sh_size());
121 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
124 gold_assert(this->symtab_xindex_.empty());
125 this->symtab_xindex_.reserve(bytecount / 4);
126 for (section_size_type i = 0; i < bytecount; i += 4)
128 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
129 // We preadjust the section indexes we save.
130 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
134 // Symbol symndx has a section of SHN_XINDEX; return the real section
135 // index.
137 unsigned int
138 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
140 if (symndx >= this->symtab_xindex_.size())
142 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
143 symndx);
144 return elfcpp::SHN_UNDEF;
146 unsigned int shndx = this->symtab_xindex_[symndx];
147 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
149 object->error(_("extended index for symbol %u out of range: %u"),
150 symndx, shndx);
151 return elfcpp::SHN_UNDEF;
153 return shndx;
156 // Class Object.
158 // Report an error for this object file. This is used by the
159 // elfcpp::Elf_file interface, and also called by the Object code
160 // itself.
162 void
163 Object::error(const char* format, ...) const
165 va_list args;
166 va_start(args, format);
167 char* buf = NULL;
168 if (vasprintf(&buf, format, args) < 0)
169 gold_nomem();
170 va_end(args);
171 gold_error(_("%s: %s"), this->name().c_str(), buf);
172 free(buf);
175 // Return a view of the contents of a section.
177 const unsigned char*
178 Object::section_contents(unsigned int shndx, section_size_type* plen,
179 bool cache)
181 Location loc(this->do_section_contents(shndx));
182 *plen = convert_to_section_size_type(loc.data_size);
183 if (*plen == 0)
185 static const unsigned char empty[1] = { '\0' };
186 return empty;
188 return this->get_view(loc.file_offset, *plen, true, cache);
191 // Read the section data into SD. This is code common to Sized_relobj
192 // and Sized_dynobj, so we put it into Object.
194 template<int size, bool big_endian>
195 void
196 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
197 Read_symbols_data* sd)
199 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
201 // Read the section headers.
202 const off_t shoff = elf_file->shoff();
203 const unsigned int shnum = this->shnum();
204 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
205 true, true);
207 // Read the section names.
208 const unsigned char* pshdrs = sd->section_headers->data();
209 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
210 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
212 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
213 this->error(_("section name section has wrong type: %u"),
214 static_cast<unsigned int>(shdrnames.get_sh_type()));
216 sd->section_names_size =
217 convert_to_section_size_type(shdrnames.get_sh_size());
218 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
219 sd->section_names_size, false,
220 false);
223 // If NAME is the name of a special .gnu.warning section, arrange for
224 // the warning to be issued. SHNDX is the section index. Return
225 // whether it is a warning section.
227 bool
228 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
229 Symbol_table* symtab)
231 const char warn_prefix[] = ".gnu.warning.";
232 const int warn_prefix_len = sizeof warn_prefix - 1;
233 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
235 // Read the section contents to get the warning text. It would
236 // be nicer if we only did this if we have to actually issue a
237 // warning. Unfortunately, warnings are issued as we relocate
238 // sections. That means that we can not lock the object then,
239 // as we might try to issue the same warning multiple times
240 // simultaneously.
241 section_size_type len;
242 const unsigned char* contents = this->section_contents(shndx, &len,
243 false);
244 if (len == 0)
246 const char* warning = name + warn_prefix_len;
247 contents = reinterpret_cast<const unsigned char*>(warning);
248 len = strlen(warning);
250 std::string warning(reinterpret_cast<const char*>(contents), len);
251 symtab->add_warning(name + warn_prefix_len, this, warning);
252 return true;
254 return false;
257 // If NAME is the name of the special section which indicates that
258 // this object was compiled with -fstack-split, mark it accordingly.
260 bool
261 Object::handle_split_stack_section(const char* name)
263 if (strcmp(name, ".note.GNU-split-stack") == 0)
265 this->uses_split_stack_ = true;
266 return true;
268 if (strcmp(name, ".note.GNU-no-split-stack") == 0)
270 this->has_no_split_stack_ = true;
271 return true;
273 return false;
276 // Class Relobj
278 // To copy the symbols data read from the file to a local data structure.
279 // This function is called from do_layout only while doing garbage
280 // collection.
282 void
283 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
284 unsigned int section_header_size)
286 gc_sd->section_headers_data =
287 new unsigned char[(section_header_size)];
288 memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
289 section_header_size);
290 gc_sd->section_names_data =
291 new unsigned char[sd->section_names_size];
292 memcpy(gc_sd->section_names_data, sd->section_names->data(),
293 sd->section_names_size);
294 gc_sd->section_names_size = sd->section_names_size;
295 if (sd->symbols != NULL)
297 gc_sd->symbols_data =
298 new unsigned char[sd->symbols_size];
299 memcpy(gc_sd->symbols_data, sd->symbols->data(),
300 sd->symbols_size);
302 else
304 gc_sd->symbols_data = NULL;
306 gc_sd->symbols_size = sd->symbols_size;
307 gc_sd->external_symbols_offset = sd->external_symbols_offset;
308 if (sd->symbol_names != NULL)
310 gc_sd->symbol_names_data =
311 new unsigned char[sd->symbol_names_size];
312 memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
313 sd->symbol_names_size);
315 else
317 gc_sd->symbol_names_data = NULL;
319 gc_sd->symbol_names_size = sd->symbol_names_size;
322 // This function determines if a particular section name must be included
323 // in the link. This is used during garbage collection to determine the
324 // roots of the worklist.
326 bool
327 Relobj::is_section_name_included(const char* name)
329 if (is_prefix_of(".ctors", name)
330 || is_prefix_of(".dtors", name)
331 || is_prefix_of(".note", name)
332 || is_prefix_of(".init", name)
333 || is_prefix_of(".fini", name)
334 || is_prefix_of(".gcc_except_table", name)
335 || is_prefix_of(".jcr", name)
336 || is_prefix_of(".preinit_array", name)
337 || (is_prefix_of(".text", name)
338 && strstr(name, "personality"))
339 || (is_prefix_of(".data", name)
340 && strstr(name, "personality"))
341 || (is_prefix_of(".gnu.linkonce.d", name)
342 && strstr(name, "personality")))
344 return true;
346 return false;
349 // Class Sized_relobj.
351 template<int size, bool big_endian>
352 Sized_relobj<size, big_endian>::Sized_relobj(
353 const std::string& name,
354 Input_file* input_file,
355 off_t offset,
356 const elfcpp::Ehdr<size, big_endian>& ehdr)
357 : Relobj(name, input_file, offset),
358 elf_file_(this, ehdr),
359 symtab_shndx_(-1U),
360 local_symbol_count_(0),
361 output_local_symbol_count_(0),
362 output_local_dynsym_count_(0),
363 symbols_(),
364 defined_count_(0),
365 local_symbol_offset_(0),
366 local_dynsym_offset_(0),
367 local_values_(),
368 local_got_offsets_(),
369 kept_comdat_sections_(),
370 has_eh_frame_(false),
371 discarded_eh_frame_shndx_(-1U),
372 deferred_layout_(),
373 deferred_layout_relocs_(),
374 compressed_sections_()
378 template<int size, bool big_endian>
379 Sized_relobj<size, big_endian>::~Sized_relobj()
383 // Set up an object file based on the file header. This sets up the
384 // section information.
386 template<int size, bool big_endian>
387 void
388 Sized_relobj<size, big_endian>::do_setup()
390 const unsigned int shnum = this->elf_file_.shnum();
391 this->set_shnum(shnum);
394 // Find the SHT_SYMTAB section, given the section headers. The ELF
395 // standard says that maybe in the future there can be more than one
396 // SHT_SYMTAB section. Until somebody figures out how that could
397 // work, we assume there is only one.
399 template<int size, bool big_endian>
400 void
401 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
403 const unsigned int shnum = this->shnum();
404 this->symtab_shndx_ = 0;
405 if (shnum > 0)
407 // Look through the sections in reverse order, since gas tends
408 // to put the symbol table at the end.
409 const unsigned char* p = pshdrs + shnum * This::shdr_size;
410 unsigned int i = shnum;
411 unsigned int xindex_shndx = 0;
412 unsigned int xindex_link = 0;
413 while (i > 0)
415 --i;
416 p -= This::shdr_size;
417 typename This::Shdr shdr(p);
418 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
420 this->symtab_shndx_ = i;
421 if (xindex_shndx > 0 && xindex_link == i)
423 Xindex* xindex =
424 new Xindex(this->elf_file_.large_shndx_offset());
425 xindex->read_symtab_xindex<size, big_endian>(this,
426 xindex_shndx,
427 pshdrs);
428 this->set_xindex(xindex);
430 break;
433 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
434 // one. This will work if it follows the SHT_SYMTAB
435 // section.
436 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
438 xindex_shndx = i;
439 xindex_link = this->adjust_shndx(shdr.get_sh_link());
445 // Return the Xindex structure to use for object with lots of
446 // sections.
448 template<int size, bool big_endian>
449 Xindex*
450 Sized_relobj<size, big_endian>::do_initialize_xindex()
452 gold_assert(this->symtab_shndx_ != -1U);
453 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
454 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
455 return xindex;
458 // Return whether SHDR has the right type and flags to be a GNU
459 // .eh_frame section.
461 template<int size, bool big_endian>
462 bool
463 Sized_relobj<size, big_endian>::check_eh_frame_flags(
464 const elfcpp::Shdr<size, big_endian>* shdr) const
466 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
467 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
470 // Return whether there is a GNU .eh_frame section, given the section
471 // headers and the section names.
473 template<int size, bool big_endian>
474 bool
475 Sized_relobj<size, big_endian>::find_eh_frame(
476 const unsigned char* pshdrs,
477 const char* names,
478 section_size_type names_size) const
480 const unsigned int shnum = this->shnum();
481 const unsigned char* p = pshdrs + This::shdr_size;
482 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
484 typename This::Shdr shdr(p);
485 if (this->check_eh_frame_flags(&shdr))
487 if (shdr.get_sh_name() >= names_size)
489 this->error(_("bad section name offset for section %u: %lu"),
490 i, static_cast<unsigned long>(shdr.get_sh_name()));
491 continue;
494 const char* name = names + shdr.get_sh_name();
495 if (strcmp(name, ".eh_frame") == 0)
496 return true;
499 return false;
502 // Build a table for any compressed debug sections, mapping each section index
503 // to the uncompressed size.
505 template<int size, bool big_endian>
506 Compressed_section_map*
507 build_compressed_section_map(
508 const unsigned char* pshdrs,
509 unsigned int shnum,
510 const char* names,
511 section_size_type names_size,
512 Sized_relobj<size, big_endian>* obj)
514 Compressed_section_map* uncompressed_sizes = new Compressed_section_map();
515 const unsigned int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
516 const unsigned char* p = pshdrs + shdr_size;
517 for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
519 typename elfcpp::Shdr<size, big_endian> shdr(p);
520 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
521 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
523 if (shdr.get_sh_name() >= names_size)
525 obj->error(_("bad section name offset for section %u: %lu"),
526 i, static_cast<unsigned long>(shdr.get_sh_name()));
527 continue;
530 const char* name = names + shdr.get_sh_name();
531 if (is_compressed_debug_section(name))
533 section_size_type len;
534 const unsigned char* contents =
535 obj->section_contents(i, &len, false);
536 uint64_t uncompressed_size = get_uncompressed_size(contents, len);
537 if (uncompressed_size != -1ULL)
538 (*uncompressed_sizes)[i] =
539 convert_to_section_size_type(uncompressed_size);
543 return uncompressed_sizes;
546 // Read the sections and symbols from an object file.
548 template<int size, bool big_endian>
549 void
550 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
552 this->read_section_data(&this->elf_file_, sd);
554 const unsigned char* const pshdrs = sd->section_headers->data();
556 this->find_symtab(pshdrs);
558 const unsigned char* namesu = sd->section_names->data();
559 const char* names = reinterpret_cast<const char*>(namesu);
560 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
562 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
563 this->has_eh_frame_ = true;
565 if (memmem(names, sd->section_names_size, ".zdebug_", 8) != NULL)
566 this->compressed_sections_ =
567 build_compressed_section_map(pshdrs, this->shnum(), names,
568 sd->section_names_size, this);
570 sd->symbols = NULL;
571 sd->symbols_size = 0;
572 sd->external_symbols_offset = 0;
573 sd->symbol_names = NULL;
574 sd->symbol_names_size = 0;
576 if (this->symtab_shndx_ == 0)
578 // No symbol table. Weird but legal.
579 return;
582 // Get the symbol table section header.
583 typename This::Shdr symtabshdr(pshdrs
584 + this->symtab_shndx_ * This::shdr_size);
585 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
587 // If this object has a .eh_frame section, we need all the symbols.
588 // Otherwise we only need the external symbols. While it would be
589 // simpler to just always read all the symbols, I've seen object
590 // files with well over 2000 local symbols, which for a 64-bit
591 // object file format is over 5 pages that we don't need to read
592 // now.
594 const int sym_size = This::sym_size;
595 const unsigned int loccount = symtabshdr.get_sh_info();
596 this->local_symbol_count_ = loccount;
597 this->local_values_.resize(loccount);
598 section_offset_type locsize = loccount * sym_size;
599 off_t dataoff = symtabshdr.get_sh_offset();
600 section_size_type datasize =
601 convert_to_section_size_type(symtabshdr.get_sh_size());
602 off_t extoff = dataoff + locsize;
603 section_size_type extsize = datasize - locsize;
605 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
606 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
608 if (readsize == 0)
610 // No external symbols. Also weird but also legal.
611 return;
614 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
616 // Read the section header for the symbol names.
617 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
618 if (strtab_shndx >= this->shnum())
620 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
621 return;
623 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
624 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
626 this->error(_("symbol table name section has wrong type: %u"),
627 static_cast<unsigned int>(strtabshdr.get_sh_type()));
628 return;
631 // Read the symbol names.
632 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
633 strtabshdr.get_sh_size(),
634 false, true);
636 sd->symbols = fvsymtab;
637 sd->symbols_size = readsize;
638 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
639 sd->symbol_names = fvstrtab;
640 sd->symbol_names_size =
641 convert_to_section_size_type(strtabshdr.get_sh_size());
644 // Return the section index of symbol SYM. Set *VALUE to its value in
645 // the object file. Set *IS_ORDINARY if this is an ordinary section
646 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
647 // Note that for a symbol which is not defined in this object file,
648 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
649 // the final value of the symbol in the link.
651 template<int size, bool big_endian>
652 unsigned int
653 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
654 Address* value,
655 bool* is_ordinary)
657 section_size_type symbols_size;
658 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
659 &symbols_size,
660 false);
662 const size_t count = symbols_size / This::sym_size;
663 gold_assert(sym < count);
665 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
666 *value = elfsym.get_st_value();
668 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
671 // Return whether to include a section group in the link. LAYOUT is
672 // used to keep track of which section groups we have already seen.
673 // INDEX is the index of the section group and SHDR is the section
674 // header. If we do not want to include this group, we set bits in
675 // OMIT for each section which should be discarded.
677 template<int size, bool big_endian>
678 bool
679 Sized_relobj<size, big_endian>::include_section_group(
680 Symbol_table* symtab,
681 Layout* layout,
682 unsigned int index,
683 const char* name,
684 const unsigned char* shdrs,
685 const char* section_names,
686 section_size_type section_names_size,
687 std::vector<bool>* omit)
689 // Read the section contents.
690 typename This::Shdr shdr(shdrs + index * This::shdr_size);
691 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
692 shdr.get_sh_size(), true, false);
693 const elfcpp::Elf_Word* pword =
694 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
696 // The first word contains flags. We only care about COMDAT section
697 // groups. Other section groups are always included in the link
698 // just like ordinary sections.
699 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
701 // Look up the group signature, which is the name of a symbol. This
702 // is a lot of effort to go to to read a string. Why didn't they
703 // just have the group signature point into the string table, rather
704 // than indirect through a symbol?
706 // Get the appropriate symbol table header (this will normally be
707 // the single SHT_SYMTAB section, but in principle it need not be).
708 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
709 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
711 // Read the symbol table entry.
712 unsigned int symndx = shdr.get_sh_info();
713 if (symndx >= symshdr.get_sh_size() / This::sym_size)
715 this->error(_("section group %u info %u out of range"),
716 index, symndx);
717 return false;
719 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
720 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
721 false);
722 elfcpp::Sym<size, big_endian> sym(psym);
724 // Read the symbol table names.
725 section_size_type symnamelen;
726 const unsigned char* psymnamesu;
727 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
728 &symnamelen, true);
729 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
731 // Get the section group signature.
732 if (sym.get_st_name() >= symnamelen)
734 this->error(_("symbol %u name offset %u out of range"),
735 symndx, sym.get_st_name());
736 return false;
739 std::string signature(psymnames + sym.get_st_name());
741 // It seems that some versions of gas will create a section group
742 // associated with a section symbol, and then fail to give a name to
743 // the section symbol. In such a case, use the name of the section.
744 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
746 bool is_ordinary;
747 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
748 sym.get_st_shndx(),
749 &is_ordinary);
750 if (!is_ordinary || sym_shndx >= this->shnum())
752 this->error(_("symbol %u invalid section index %u"),
753 symndx, sym_shndx);
754 return false;
756 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
757 if (member_shdr.get_sh_name() < section_names_size)
758 signature = section_names + member_shdr.get_sh_name();
761 // Record this section group in the layout, and see whether we've already
762 // seen one with the same signature.
763 bool include_group;
764 bool is_comdat;
765 Kept_section* kept_section = NULL;
767 if ((flags & elfcpp::GRP_COMDAT) == 0)
769 include_group = true;
770 is_comdat = false;
772 else
774 include_group = layout->find_or_add_kept_section(signature,
775 this, index, true,
776 true, &kept_section);
777 is_comdat = true;
780 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
782 std::vector<unsigned int> shndxes;
783 bool relocate_group = include_group && parameters->options().relocatable();
784 if (relocate_group)
785 shndxes.reserve(count - 1);
787 for (size_t i = 1; i < count; ++i)
789 elfcpp::Elf_Word shndx =
790 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
792 if (relocate_group)
793 shndxes.push_back(shndx);
795 if (shndx >= this->shnum())
797 this->error(_("section %u in section group %u out of range"),
798 shndx, index);
799 continue;
802 // Check for an earlier section number, since we're going to get
803 // it wrong--we may have already decided to include the section.
804 if (shndx < index)
805 this->error(_("invalid section group %u refers to earlier section %u"),
806 index, shndx);
808 // Get the name of the member section.
809 typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
810 if (member_shdr.get_sh_name() >= section_names_size)
812 // This is an error, but it will be diagnosed eventually
813 // in do_layout, so we don't need to do anything here but
814 // ignore it.
815 continue;
817 std::string mname(section_names + member_shdr.get_sh_name());
819 if (include_group)
821 if (is_comdat)
822 kept_section->add_comdat_section(mname, shndx,
823 member_shdr.get_sh_size());
825 else
827 (*omit)[shndx] = true;
829 if (is_comdat)
831 Relobj* kept_object = kept_section->object();
832 if (kept_section->is_comdat())
834 // Find the corresponding kept section, and store
835 // that info in the discarded section table.
836 unsigned int kept_shndx;
837 uint64_t kept_size;
838 if (kept_section->find_comdat_section(mname, &kept_shndx,
839 &kept_size))
841 // We don't keep a mapping for this section if
842 // it has a different size. The mapping is only
843 // used for relocation processing, and we don't
844 // want to treat the sections as similar if the
845 // sizes are different. Checking the section
846 // size is the approach used by the GNU linker.
847 if (kept_size == member_shdr.get_sh_size())
848 this->set_kept_comdat_section(shndx, kept_object,
849 kept_shndx);
852 else
854 // The existing section is a linkonce section. Add
855 // a mapping if there is exactly one section in the
856 // group (which is true when COUNT == 2) and if it
857 // is the same size.
858 if (count == 2
859 && (kept_section->linkonce_size()
860 == member_shdr.get_sh_size()))
861 this->set_kept_comdat_section(shndx, kept_object,
862 kept_section->shndx());
868 if (relocate_group)
869 layout->layout_group(symtab, this, index, name, signature.c_str(),
870 shdr, flags, &shndxes);
872 return include_group;
875 // Whether to include a linkonce section in the link. NAME is the
876 // name of the section and SHDR is the section header.
878 // Linkonce sections are a GNU extension implemented in the original
879 // GNU linker before section groups were defined. The semantics are
880 // that we only include one linkonce section with a given name. The
881 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
882 // where T is the type of section and SYMNAME is the name of a symbol.
883 // In an attempt to make linkonce sections interact well with section
884 // groups, we try to identify SYMNAME and use it like a section group
885 // signature. We want to block section groups with that signature,
886 // but not other linkonce sections with that signature. We also use
887 // the full name of the linkonce section as a normal section group
888 // signature.
890 template<int size, bool big_endian>
891 bool
892 Sized_relobj<size, big_endian>::include_linkonce_section(
893 Layout* layout,
894 unsigned int index,
895 const char* name,
896 const elfcpp::Shdr<size, big_endian>& shdr)
898 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
899 // In general the symbol name we want will be the string following
900 // the last '.'. However, we have to handle the case of
901 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
902 // some versions of gcc. So we use a heuristic: if the name starts
903 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
904 // we look for the last '.'. We can't always simply skip
905 // ".gnu.linkonce.X", because we have to deal with cases like
906 // ".gnu.linkonce.d.rel.ro.local".
907 const char* const linkonce_t = ".gnu.linkonce.t.";
908 const char* symname;
909 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
910 symname = name + strlen(linkonce_t);
911 else
912 symname = strrchr(name, '.') + 1;
913 std::string sig1(symname);
914 std::string sig2(name);
915 Kept_section* kept1;
916 Kept_section* kept2;
917 bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
918 false, &kept1);
919 bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
920 true, &kept2);
922 if (!include2)
924 // We are not including this section because we already saw the
925 // name of the section as a signature. This normally implies
926 // that the kept section is another linkonce section. If it is
927 // the same size, record it as the section which corresponds to
928 // this one.
929 if (kept2->object() != NULL
930 && !kept2->is_comdat()
931 && kept2->linkonce_size() == sh_size)
932 this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
934 else if (!include1)
936 // The section is being discarded on the basis of its symbol
937 // name. This means that the corresponding kept section was
938 // part of a comdat group, and it will be difficult to identify
939 // the specific section within that group that corresponds to
940 // this linkonce section. We'll handle the simple case where
941 // the group has only one member section. Otherwise, it's not
942 // worth the effort.
943 unsigned int kept_shndx;
944 uint64_t kept_size;
945 if (kept1->object() != NULL
946 && kept1->is_comdat()
947 && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
948 && kept_size == sh_size)
949 this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
951 else
953 kept1->set_linkonce_size(sh_size);
954 kept2->set_linkonce_size(sh_size);
957 return include1 && include2;
960 // Layout an input section.
962 template<int size, bool big_endian>
963 inline void
964 Sized_relobj<size, big_endian>::layout_section(Layout* layout,
965 unsigned int shndx,
966 const char* name,
967 typename This::Shdr& shdr,
968 unsigned int reloc_shndx,
969 unsigned int reloc_type)
971 off_t offset;
972 Output_section* os = layout->layout(this, shndx, name, shdr,
973 reloc_shndx, reloc_type, &offset);
975 this->output_sections()[shndx] = os;
976 if (offset == -1)
977 this->section_offsets_[shndx] = invalid_address;
978 else
979 this->section_offsets_[shndx] = convert_types<Address, off_t>(offset);
981 // If this section requires special handling, and if there are
982 // relocs that apply to it, then we must do the special handling
983 // before we apply the relocs.
984 if (offset == -1 && reloc_shndx != 0)
985 this->set_relocs_must_follow_section_writes();
988 // Lay out the input sections. We walk through the sections and check
989 // whether they should be included in the link. If they should, we
990 // pass them to the Layout object, which will return an output section
991 // and an offset.
992 // During garbage collection (--gc-sections) and identical code folding
993 // (--icf), this function is called twice. When it is called the first
994 // time, it is for setting up some sections as roots to a work-list for
995 // --gc-sections and to do comdat processing. Actual layout happens the
996 // second time around after all the relevant sections have been determined.
997 // The first time, is_worklist_ready or is_icf_ready is false. It is then
998 // set to true after the garbage collection worklist or identical code
999 // folding is processed and the relevant sections to be kept are
1000 // determined. Then, this function is called again to layout the sections.
1002 template<int size, bool big_endian>
1003 void
1004 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
1005 Layout* layout,
1006 Read_symbols_data* sd)
1008 const unsigned int shnum = this->shnum();
1009 bool is_gc_pass_one = ((parameters->options().gc_sections()
1010 && !symtab->gc()->is_worklist_ready())
1011 || (parameters->options().icf_enabled()
1012 && !symtab->icf()->is_icf_ready()));
1014 bool is_gc_pass_two = ((parameters->options().gc_sections()
1015 && symtab->gc()->is_worklist_ready())
1016 || (parameters->options().icf_enabled()
1017 && symtab->icf()->is_icf_ready()));
1019 bool is_gc_or_icf = (parameters->options().gc_sections()
1020 || parameters->options().icf_enabled());
1022 // Both is_gc_pass_one and is_gc_pass_two should not be true.
1023 gold_assert(!(is_gc_pass_one && is_gc_pass_two));
1025 if (shnum == 0)
1026 return;
1027 Symbols_data* gc_sd = NULL;
1028 if (is_gc_pass_one)
1030 // During garbage collection save the symbols data to use it when
1031 // re-entering this function.
1032 gc_sd = new Symbols_data;
1033 this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
1034 this->set_symbols_data(gc_sd);
1036 else if (is_gc_pass_two)
1038 gc_sd = this->get_symbols_data();
1041 const unsigned char* section_headers_data = NULL;
1042 section_size_type section_names_size;
1043 const unsigned char* symbols_data = NULL;
1044 section_size_type symbols_size;
1045 section_offset_type external_symbols_offset;
1046 const unsigned char* symbol_names_data = NULL;
1047 section_size_type symbol_names_size;
1049 if (is_gc_or_icf)
1051 section_headers_data = gc_sd->section_headers_data;
1052 section_names_size = gc_sd->section_names_size;
1053 symbols_data = gc_sd->symbols_data;
1054 symbols_size = gc_sd->symbols_size;
1055 external_symbols_offset = gc_sd->external_symbols_offset;
1056 symbol_names_data = gc_sd->symbol_names_data;
1057 symbol_names_size = gc_sd->symbol_names_size;
1059 else
1061 section_headers_data = sd->section_headers->data();
1062 section_names_size = sd->section_names_size;
1063 if (sd->symbols != NULL)
1064 symbols_data = sd->symbols->data();
1065 symbols_size = sd->symbols_size;
1066 external_symbols_offset = sd->external_symbols_offset;
1067 if (sd->symbol_names != NULL)
1068 symbol_names_data = sd->symbol_names->data();
1069 symbol_names_size = sd->symbol_names_size;
1072 // Get the section headers.
1073 const unsigned char* shdrs = section_headers_data;
1074 const unsigned char* pshdrs;
1076 // Get the section names.
1077 const unsigned char* pnamesu = (is_gc_or_icf)
1078 ? gc_sd->section_names_data
1079 : sd->section_names->data();
1081 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1083 // If any input files have been claimed by plugins, we need to defer
1084 // actual layout until the replacement files have arrived.
1085 const bool should_defer_layout =
1086 (parameters->options().has_plugins()
1087 && parameters->options().plugins()->should_defer_layout());
1088 unsigned int num_sections_to_defer = 0;
1090 // For each section, record the index of the reloc section if any.
1091 // Use 0 to mean that there is no reloc section, -1U to mean that
1092 // there is more than one.
1093 std::vector<unsigned int> reloc_shndx(shnum, 0);
1094 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
1095 // Skip the first, dummy, section.
1096 pshdrs = shdrs + This::shdr_size;
1097 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1099 typename This::Shdr shdr(pshdrs);
1101 // Count the number of sections whose layout will be deferred.
1102 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1103 ++num_sections_to_defer;
1105 unsigned int sh_type = shdr.get_sh_type();
1106 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
1108 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
1109 if (target_shndx == 0 || target_shndx >= shnum)
1111 this->error(_("relocation section %u has bad info %u"),
1112 i, target_shndx);
1113 continue;
1116 if (reloc_shndx[target_shndx] != 0)
1117 reloc_shndx[target_shndx] = -1U;
1118 else
1120 reloc_shndx[target_shndx] = i;
1121 reloc_type[target_shndx] = sh_type;
1126 Output_sections& out_sections(this->output_sections());
1127 std::vector<Address>& out_section_offsets(this->section_offsets_);
1129 if (!is_gc_pass_two)
1131 out_sections.resize(shnum);
1132 out_section_offsets.resize(shnum);
1135 // If we are only linking for symbols, then there is nothing else to
1136 // do here.
1137 if (this->input_file()->just_symbols())
1139 if (!is_gc_pass_two)
1141 delete sd->section_headers;
1142 sd->section_headers = NULL;
1143 delete sd->section_names;
1144 sd->section_names = NULL;
1146 return;
1149 if (num_sections_to_defer > 0)
1151 parameters->options().plugins()->add_deferred_layout_object(this);
1152 this->deferred_layout_.reserve(num_sections_to_defer);
1155 // Whether we've seen a .note.GNU-stack section.
1156 bool seen_gnu_stack = false;
1157 // The flags of a .note.GNU-stack section.
1158 uint64_t gnu_stack_flags = 0;
1160 // Keep track of which sections to omit.
1161 std::vector<bool> omit(shnum, false);
1163 // Keep track of reloc sections when emitting relocations.
1164 const bool relocatable = parameters->options().relocatable();
1165 const bool emit_relocs = (relocatable
1166 || parameters->options().emit_relocs());
1167 std::vector<unsigned int> reloc_sections;
1169 // Keep track of .eh_frame sections.
1170 std::vector<unsigned int> eh_frame_sections;
1172 // Skip the first, dummy, section.
1173 pshdrs = shdrs + This::shdr_size;
1174 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1176 typename This::Shdr shdr(pshdrs);
1178 if (shdr.get_sh_name() >= section_names_size)
1180 this->error(_("bad section name offset for section %u: %lu"),
1181 i, static_cast<unsigned long>(shdr.get_sh_name()));
1182 return;
1185 const char* name = pnames + shdr.get_sh_name();
1187 if (!is_gc_pass_two)
1189 if (this->handle_gnu_warning_section(name, i, symtab))
1191 if (!relocatable)
1192 omit[i] = true;
1195 // The .note.GNU-stack section is special. It gives the
1196 // protection flags that this object file requires for the stack
1197 // in memory.
1198 if (strcmp(name, ".note.GNU-stack") == 0)
1200 seen_gnu_stack = true;
1201 gnu_stack_flags |= shdr.get_sh_flags();
1202 omit[i] = true;
1205 // The .note.GNU-split-stack section is also special. It
1206 // indicates that the object was compiled with
1207 // -fsplit-stack.
1208 if (this->handle_split_stack_section(name))
1210 if (!parameters->options().relocatable()
1211 && !parameters->options().shared())
1212 omit[i] = true;
1215 // Skip attributes section.
1216 if (parameters->target().is_attributes_section(name))
1218 omit[i] = true;
1221 bool discard = omit[i];
1222 if (!discard)
1224 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
1226 if (!this->include_section_group(symtab, layout, i, name,
1227 shdrs, pnames,
1228 section_names_size,
1229 &omit))
1230 discard = true;
1232 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
1233 && Layout::is_linkonce(name))
1235 if (!this->include_linkonce_section(layout, i, name, shdr))
1236 discard = true;
1240 if (discard)
1242 // Do not include this section in the link.
1243 out_sections[i] = NULL;
1244 out_section_offsets[i] = invalid_address;
1245 continue;
1249 if (is_gc_pass_one && parameters->options().gc_sections())
1251 if (is_section_name_included(name)
1252 || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1253 || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
1255 symtab->gc()->worklist().push(Section_id(this, i));
1257 // If the section name XXX can be represented as a C identifier
1258 // it cannot be discarded if there are references to
1259 // __start_XXX and __stop_XXX symbols. These need to be
1260 // specially handled.
1261 if (is_cident(name))
1263 symtab->gc()->add_cident_section(name, Section_id(this, i));
1267 // When doing a relocatable link we are going to copy input
1268 // reloc sections into the output. We only want to copy the
1269 // ones associated with sections which are not being discarded.
1270 // However, we don't know that yet for all sections. So save
1271 // reloc sections and process them later. Garbage collection is
1272 // not triggered when relocatable code is desired.
1273 if (emit_relocs
1274 && (shdr.get_sh_type() == elfcpp::SHT_REL
1275 || shdr.get_sh_type() == elfcpp::SHT_RELA))
1277 reloc_sections.push_back(i);
1278 continue;
1281 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
1282 continue;
1284 // The .eh_frame section is special. It holds exception frame
1285 // information that we need to read in order to generate the
1286 // exception frame header. We process these after all the other
1287 // sections so that the exception frame reader can reliably
1288 // determine which sections are being discarded, and discard the
1289 // corresponding information.
1290 if (!relocatable
1291 && strcmp(name, ".eh_frame") == 0
1292 && this->check_eh_frame_flags(&shdr))
1294 if (is_gc_pass_one)
1296 out_sections[i] = reinterpret_cast<Output_section*>(1);
1297 out_section_offsets[i] = invalid_address;
1299 else
1300 eh_frame_sections.push_back(i);
1301 continue;
1304 if (is_gc_pass_two && parameters->options().gc_sections())
1306 // This is executed during the second pass of garbage
1307 // collection. do_layout has been called before and some
1308 // sections have been already discarded. Simply ignore
1309 // such sections this time around.
1310 if (out_sections[i] == NULL)
1312 gold_assert(out_section_offsets[i] == invalid_address);
1313 continue;
1315 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1316 && symtab->gc()->is_section_garbage(this, i))
1318 if (parameters->options().print_gc_sections())
1319 gold_info(_("%s: removing unused section from '%s'"
1320 " in file '%s'"),
1321 program_name, this->section_name(i).c_str(),
1322 this->name().c_str());
1323 out_sections[i] = NULL;
1324 out_section_offsets[i] = invalid_address;
1325 continue;
1329 if (is_gc_pass_two && parameters->options().icf_enabled())
1331 if (out_sections[i] == NULL)
1333 gold_assert(out_section_offsets[i] == invalid_address);
1334 continue;
1336 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1337 && symtab->icf()->is_section_folded(this, i))
1339 if (parameters->options().print_icf_sections())
1341 Section_id folded =
1342 symtab->icf()->get_folded_section(this, i);
1343 Relobj* folded_obj =
1344 reinterpret_cast<Relobj*>(folded.first);
1345 gold_info(_("%s: ICF folding section '%s' in file '%s'"
1346 "into '%s' in file '%s'"),
1347 program_name, this->section_name(i).c_str(),
1348 this->name().c_str(),
1349 folded_obj->section_name(folded.second).c_str(),
1350 folded_obj->name().c_str());
1352 out_sections[i] = NULL;
1353 out_section_offsets[i] = invalid_address;
1354 continue;
1358 // Defer layout here if input files are claimed by plugins. When gc
1359 // is turned on this function is called twice. For the second call
1360 // should_defer_layout should be false.
1361 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1363 gold_assert(!is_gc_pass_two);
1364 this->deferred_layout_.push_back(Deferred_layout(i, name,
1365 pshdrs,
1366 reloc_shndx[i],
1367 reloc_type[i]));
1368 // Put dummy values here; real values will be supplied by
1369 // do_layout_deferred_sections.
1370 out_sections[i] = reinterpret_cast<Output_section*>(2);
1371 out_section_offsets[i] = invalid_address;
1372 continue;
1375 // During gc_pass_two if a section that was previously deferred is
1376 // found, do not layout the section as layout_deferred_sections will
1377 // do it later from gold.cc.
1378 if (is_gc_pass_two
1379 && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
1380 continue;
1382 if (is_gc_pass_one)
1384 // This is during garbage collection. The out_sections are
1385 // assigned in the second call to this function.
1386 out_sections[i] = reinterpret_cast<Output_section*>(1);
1387 out_section_offsets[i] = invalid_address;
1389 else
1391 // When garbage collection is switched on the actual layout
1392 // only happens in the second call.
1393 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1394 reloc_type[i]);
1398 if (!is_gc_pass_two)
1399 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1401 // When doing a relocatable link handle the reloc sections at the
1402 // end. Garbage collection and Identical Code Folding is not
1403 // turned on for relocatable code.
1404 if (emit_relocs)
1405 this->size_relocatable_relocs();
1407 gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
1409 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1410 p != reloc_sections.end();
1411 ++p)
1413 unsigned int i = *p;
1414 const unsigned char* pshdr;
1415 pshdr = section_headers_data + i * This::shdr_size;
1416 typename This::Shdr shdr(pshdr);
1418 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1419 if (data_shndx >= shnum)
1421 // We already warned about this above.
1422 continue;
1425 Output_section* data_section = out_sections[data_shndx];
1426 if (data_section == reinterpret_cast<Output_section*>(2))
1428 // The layout for the data section was deferred, so we need
1429 // to defer the relocation section, too.
1430 const char* name = pnames + shdr.get_sh_name();
1431 this->deferred_layout_relocs_.push_back(
1432 Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
1433 out_sections[i] = reinterpret_cast<Output_section*>(2);
1434 out_section_offsets[i] = invalid_address;
1435 continue;
1437 if (data_section == NULL)
1439 out_sections[i] = NULL;
1440 out_section_offsets[i] = invalid_address;
1441 continue;
1444 Relocatable_relocs* rr = new Relocatable_relocs();
1445 this->set_relocatable_relocs(i, rr);
1447 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1448 rr);
1449 out_sections[i] = os;
1450 out_section_offsets[i] = invalid_address;
1453 // Handle the .eh_frame sections at the end.
1454 gold_assert(!is_gc_pass_one || eh_frame_sections.empty());
1455 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1456 p != eh_frame_sections.end();
1457 ++p)
1459 gold_assert(this->has_eh_frame_);
1460 gold_assert(external_symbols_offset != 0);
1462 unsigned int i = *p;
1463 const unsigned char *pshdr;
1464 pshdr = section_headers_data + i * This::shdr_size;
1465 typename This::Shdr shdr(pshdr);
1467 off_t offset;
1468 Output_section* os = layout->layout_eh_frame(this,
1469 symbols_data,
1470 symbols_size,
1471 symbol_names_data,
1472 symbol_names_size,
1473 i, shdr,
1474 reloc_shndx[i],
1475 reloc_type[i],
1476 &offset);
1477 out_sections[i] = os;
1478 if (os == NULL || offset == -1)
1480 // An object can contain at most one section holding exception
1481 // frame information.
1482 gold_assert(this->discarded_eh_frame_shndx_ == -1U);
1483 this->discarded_eh_frame_shndx_ = i;
1484 out_section_offsets[i] = invalid_address;
1486 else
1487 out_section_offsets[i] = convert_types<Address, off_t>(offset);
1489 // If this section requires special handling, and if there are
1490 // relocs that apply to it, then we must do the special handling
1491 // before we apply the relocs.
1492 if (os != NULL && offset == -1 && reloc_shndx[i] != 0)
1493 this->set_relocs_must_follow_section_writes();
1496 if (is_gc_pass_two)
1498 delete[] gc_sd->section_headers_data;
1499 delete[] gc_sd->section_names_data;
1500 delete[] gc_sd->symbols_data;
1501 delete[] gc_sd->symbol_names_data;
1502 this->set_symbols_data(NULL);
1504 else
1506 delete sd->section_headers;
1507 sd->section_headers = NULL;
1508 delete sd->section_names;
1509 sd->section_names = NULL;
1513 // Layout sections whose layout was deferred while waiting for
1514 // input files from a plugin.
1516 template<int size, bool big_endian>
1517 void
1518 Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1520 typename std::vector<Deferred_layout>::iterator deferred;
1522 for (deferred = this->deferred_layout_.begin();
1523 deferred != this->deferred_layout_.end();
1524 ++deferred)
1526 typename This::Shdr shdr(deferred->shdr_data_);
1527 // If the section is not included, it is because the garbage collector
1528 // decided it is not needed. Avoid reverting that decision.
1529 if (!this->is_section_included(deferred->shndx_))
1530 continue;
1532 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1533 shdr, deferred->reloc_shndx_, deferred->reloc_type_);
1536 this->deferred_layout_.clear();
1538 // Now handle the deferred relocation sections.
1540 Output_sections& out_sections(this->output_sections());
1541 std::vector<Address>& out_section_offsets(this->section_offsets_);
1543 for (deferred = this->deferred_layout_relocs_.begin();
1544 deferred != this->deferred_layout_relocs_.end();
1545 ++deferred)
1547 unsigned int shndx = deferred->shndx_;
1548 typename This::Shdr shdr(deferred->shdr_data_);
1549 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1551 Output_section* data_section = out_sections[data_shndx];
1552 if (data_section == NULL)
1554 out_sections[shndx] = NULL;
1555 out_section_offsets[shndx] = invalid_address;
1556 continue;
1559 Relocatable_relocs* rr = new Relocatable_relocs();
1560 this->set_relocatable_relocs(shndx, rr);
1562 Output_section* os = layout->layout_reloc(this, shndx, shdr,
1563 data_section, rr);
1564 out_sections[shndx] = os;
1565 out_section_offsets[shndx] = invalid_address;
1569 // Add the symbols to the symbol table.
1571 template<int size, bool big_endian>
1572 void
1573 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1574 Read_symbols_data* sd,
1575 Layout*)
1577 if (sd->symbols == NULL)
1579 gold_assert(sd->symbol_names == NULL);
1580 return;
1583 const int sym_size = This::sym_size;
1584 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1585 / sym_size);
1586 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1588 this->error(_("size of symbols is not multiple of symbol size"));
1589 return;
1592 this->symbols_.resize(symcount);
1594 const char* sym_names =
1595 reinterpret_cast<const char*>(sd->symbol_names->data());
1596 symtab->add_from_relobj(this,
1597 sd->symbols->data() + sd->external_symbols_offset,
1598 symcount, this->local_symbol_count_,
1599 sym_names, sd->symbol_names_size,
1600 &this->symbols_,
1601 &this->defined_count_);
1603 delete sd->symbols;
1604 sd->symbols = NULL;
1605 delete sd->symbol_names;
1606 sd->symbol_names = NULL;
1609 // Find out if this object, that is a member of a lib group, should be included
1610 // in the link. We check every symbol defined by this object. If the symbol
1611 // table has a strong undefined reference to that symbol, we have to include
1612 // the object.
1614 template<int size, bool big_endian>
1615 Archive::Should_include
1616 Sized_relobj<size, big_endian>::do_should_include_member(Symbol_table* symtab,
1617 Layout* layout,
1618 Read_symbols_data* sd,
1619 std::string* why)
1621 char* tmpbuf = NULL;
1622 size_t tmpbuflen = 0;
1623 const char* sym_names =
1624 reinterpret_cast<const char*>(sd->symbol_names->data());
1625 const unsigned char* syms =
1626 sd->symbols->data() + sd->external_symbols_offset;
1627 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1628 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1629 / sym_size);
1631 const unsigned char* p = syms;
1633 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1635 elfcpp::Sym<size, big_endian> sym(p);
1636 unsigned int st_shndx = sym.get_st_shndx();
1637 if (st_shndx == elfcpp::SHN_UNDEF)
1638 continue;
1640 unsigned int st_name = sym.get_st_name();
1641 const char* name = sym_names + st_name;
1642 Symbol* symbol;
1643 Archive::Should_include t = Archive::should_include_member(symtab,
1644 layout,
1645 name,
1646 &symbol, why,
1647 &tmpbuf,
1648 &tmpbuflen);
1649 if (t == Archive::SHOULD_INCLUDE_YES)
1651 if (tmpbuf != NULL)
1652 free(tmpbuf);
1653 return t;
1656 if (tmpbuf != NULL)
1657 free(tmpbuf);
1658 return Archive::SHOULD_INCLUDE_UNKNOWN;
1661 // First pass over the local symbols. Here we add their names to
1662 // *POOL and *DYNPOOL, and we store the symbol value in
1663 // THIS->LOCAL_VALUES_. This function is always called from a
1664 // singleton thread. This is followed by a call to
1665 // finalize_local_symbols.
1667 template<int size, bool big_endian>
1668 void
1669 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1670 Stringpool* dynpool)
1672 gold_assert(this->symtab_shndx_ != -1U);
1673 if (this->symtab_shndx_ == 0)
1675 // This object has no symbols. Weird but legal.
1676 return;
1679 // Read the symbol table section header.
1680 const unsigned int symtab_shndx = this->symtab_shndx_;
1681 typename This::Shdr symtabshdr(this,
1682 this->elf_file_.section_header(symtab_shndx));
1683 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1685 // Read the local symbols.
1686 const int sym_size = This::sym_size;
1687 const unsigned int loccount = this->local_symbol_count_;
1688 gold_assert(loccount == symtabshdr.get_sh_info());
1689 off_t locsize = loccount * sym_size;
1690 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1691 locsize, true, true);
1693 // Read the symbol names.
1694 const unsigned int strtab_shndx =
1695 this->adjust_shndx(symtabshdr.get_sh_link());
1696 section_size_type strtab_size;
1697 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1698 &strtab_size,
1699 true);
1700 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1702 // Loop over the local symbols.
1704 const Output_sections& out_sections(this->output_sections());
1705 unsigned int shnum = this->shnum();
1706 unsigned int count = 0;
1707 unsigned int dyncount = 0;
1708 // Skip the first, dummy, symbol.
1709 psyms += sym_size;
1710 bool discard_all = parameters->options().discard_all();
1711 bool discard_locals = parameters->options().discard_locals();
1712 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1714 elfcpp::Sym<size, big_endian> sym(psyms);
1716 Symbol_value<size>& lv(this->local_values_[i]);
1718 bool is_ordinary;
1719 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1720 &is_ordinary);
1721 lv.set_input_shndx(shndx, is_ordinary);
1723 if (sym.get_st_type() == elfcpp::STT_SECTION)
1724 lv.set_is_section_symbol();
1725 else if (sym.get_st_type() == elfcpp::STT_TLS)
1726 lv.set_is_tls_symbol();
1728 // Save the input symbol value for use in do_finalize_local_symbols().
1729 lv.set_input_value(sym.get_st_value());
1731 // Decide whether this symbol should go into the output file.
1733 if ((shndx < shnum && out_sections[shndx] == NULL)
1734 || shndx == this->discarded_eh_frame_shndx_)
1736 lv.set_no_output_symtab_entry();
1737 gold_assert(!lv.needs_output_dynsym_entry());
1738 continue;
1741 if (sym.get_st_type() == elfcpp::STT_SECTION)
1743 lv.set_no_output_symtab_entry();
1744 gold_assert(!lv.needs_output_dynsym_entry());
1745 continue;
1748 if (sym.get_st_name() >= strtab_size)
1750 this->error(_("local symbol %u section name out of range: %u >= %u"),
1751 i, sym.get_st_name(),
1752 static_cast<unsigned int>(strtab_size));
1753 lv.set_no_output_symtab_entry();
1754 continue;
1757 const char* name = pnames + sym.get_st_name();
1759 // If needed, add the symbol to the dynamic symbol table string pool.
1760 if (lv.needs_output_dynsym_entry())
1762 dynpool->add(name, true, NULL);
1763 ++dyncount;
1766 if (discard_all && lv.may_be_discarded_from_output_symtab())
1768 lv.set_no_output_symtab_entry();
1769 continue;
1772 // If --discard-locals option is used, discard all temporary local
1773 // symbols. These symbols start with system-specific local label
1774 // prefixes, typically .L for ELF system. We want to be compatible
1775 // with GNU ld so here we essentially use the same check in
1776 // bfd_is_local_label(). The code is different because we already
1777 // know that:
1779 // - the symbol is local and thus cannot have global or weak binding.
1780 // - the symbol is not a section symbol.
1781 // - the symbol has a name.
1783 // We do not discard a symbol if it needs a dynamic symbol entry.
1784 if (discard_locals
1785 && sym.get_st_type() != elfcpp::STT_FILE
1786 && !lv.needs_output_dynsym_entry()
1787 && lv.may_be_discarded_from_output_symtab()
1788 && parameters->target().is_local_label_name(name))
1790 lv.set_no_output_symtab_entry();
1791 continue;
1794 // Discard the local symbol if -retain_symbols_file is specified
1795 // and the local symbol is not in that file.
1796 if (!parameters->options().should_retain_symbol(name))
1798 lv.set_no_output_symtab_entry();
1799 continue;
1802 // Add the symbol to the symbol table string pool.
1803 pool->add(name, true, NULL);
1804 ++count;
1807 this->output_local_symbol_count_ = count;
1808 this->output_local_dynsym_count_ = dyncount;
1811 // Finalize the local symbols. Here we set the final value in
1812 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1813 // This function is always called from a singleton thread. The actual
1814 // output of the local symbols will occur in a separate task.
1816 template<int size, bool big_endian>
1817 unsigned int
1818 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1819 off_t off,
1820 Symbol_table* symtab)
1822 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1824 const unsigned int loccount = this->local_symbol_count_;
1825 this->local_symbol_offset_ = off;
1827 const bool relocatable = parameters->options().relocatable();
1828 const Output_sections& out_sections(this->output_sections());
1829 const std::vector<Address>& out_offsets(this->section_offsets_);
1830 unsigned int shnum = this->shnum();
1832 for (unsigned int i = 1; i < loccount; ++i)
1834 Symbol_value<size>& lv(this->local_values_[i]);
1836 bool is_ordinary;
1837 unsigned int shndx = lv.input_shndx(&is_ordinary);
1839 // Set the output symbol value.
1841 if (!is_ordinary)
1843 if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
1844 lv.set_output_value(lv.input_value());
1845 else
1847 this->error(_("unknown section index %u for local symbol %u"),
1848 shndx, i);
1849 lv.set_output_value(0);
1852 else
1854 if (shndx >= shnum)
1856 this->error(_("local symbol %u section index %u out of range"),
1857 i, shndx);
1858 shndx = 0;
1861 Output_section* os = out_sections[shndx];
1862 Address secoffset = out_offsets[shndx];
1863 if (symtab->is_section_folded(this, shndx))
1865 gold_assert (os == NULL && secoffset == invalid_address);
1866 // Get the os of the section it is folded onto.
1867 Section_id folded = symtab->icf()->get_folded_section(this,
1868 shndx);
1869 gold_assert(folded.first != NULL);
1870 Sized_relobj<size, big_endian>* folded_obj = reinterpret_cast
1871 <Sized_relobj<size, big_endian>*>(folded.first);
1872 os = folded_obj->output_section(folded.second);
1873 gold_assert(os != NULL);
1874 secoffset = folded_obj->get_output_section_offset(folded.second);
1876 // This could be a relaxed input section.
1877 if (secoffset == invalid_address)
1879 const Output_relaxed_input_section* relaxed_section =
1880 os->find_relaxed_input_section(folded_obj, folded.second);
1881 gold_assert(relaxed_section != NULL);
1882 secoffset = relaxed_section->address() - os->address();
1886 if (os == NULL)
1888 // This local symbol belongs to a section we are discarding.
1889 // In some cases when applying relocations later, we will
1890 // attempt to match it to the corresponding kept section,
1891 // so we leave the input value unchanged here.
1892 continue;
1894 else if (secoffset == invalid_address)
1896 uint64_t start;
1898 // This is a SHF_MERGE section or one which otherwise
1899 // requires special handling.
1900 if (shndx == this->discarded_eh_frame_shndx_)
1902 // This local symbol belongs to a discarded .eh_frame
1903 // section. Just treat it like the case in which
1904 // os == NULL above.
1905 gold_assert(this->has_eh_frame_);
1906 continue;
1908 else if (!lv.is_section_symbol())
1910 // This is not a section symbol. We can determine
1911 // the final value now.
1912 lv.set_output_value(os->output_address(this, shndx,
1913 lv.input_value()));
1915 else if (!os->find_starting_output_address(this, shndx, &start))
1917 // This is a section symbol, but apparently not one in a
1918 // merged section. First check to see if this is a relaxed
1919 // input section. If so, use its address. Otherwise just
1920 // use the start of the output section. This happens with
1921 // relocatable links when the input object has section
1922 // symbols for arbitrary non-merge sections.
1923 const Output_section_data* posd =
1924 os->find_relaxed_input_section(this, shndx);
1925 if (posd != NULL)
1927 Address relocatable_link_adjustment =
1928 relocatable ? os->address() : 0;
1929 lv.set_output_value(posd->address()
1930 - relocatable_link_adjustment);
1932 else
1933 lv.set_output_value(os->address());
1935 else
1937 // We have to consider the addend to determine the
1938 // value to use in a relocation. START is the start
1939 // of this input section. If we are doing a relocatable
1940 // link, use offset from start output section instead of
1941 // address.
1942 Address adjusted_start =
1943 relocatable ? start - os->address() : start;
1944 Merged_symbol_value<size>* msv =
1945 new Merged_symbol_value<size>(lv.input_value(),
1946 adjusted_start);
1947 lv.set_merged_symbol_value(msv);
1950 else if (lv.is_tls_symbol())
1951 lv.set_output_value(os->tls_offset()
1952 + secoffset
1953 + lv.input_value());
1954 else
1955 lv.set_output_value((relocatable ? 0 : os->address())
1956 + secoffset
1957 + lv.input_value());
1960 if (!lv.is_output_symtab_index_set())
1962 lv.set_output_symtab_index(index);
1963 ++index;
1966 return index;
1969 // Set the output dynamic symbol table indexes for the local variables.
1971 template<int size, bool big_endian>
1972 unsigned int
1973 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1975 const unsigned int loccount = this->local_symbol_count_;
1976 for (unsigned int i = 1; i < loccount; ++i)
1978 Symbol_value<size>& lv(this->local_values_[i]);
1979 if (lv.needs_output_dynsym_entry())
1981 lv.set_output_dynsym_index(index);
1982 ++index;
1985 return index;
1988 // Set the offset where local dynamic symbol information will be stored.
1989 // Returns the count of local symbols contributed to the symbol table by
1990 // this object.
1992 template<int size, bool big_endian>
1993 unsigned int
1994 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1996 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1997 this->local_dynsym_offset_ = off;
1998 return this->output_local_dynsym_count_;
2001 // If Symbols_data is not NULL get the section flags from here otherwise
2002 // get it from the file.
2004 template<int size, bool big_endian>
2005 uint64_t
2006 Sized_relobj<size, big_endian>::do_section_flags(unsigned int shndx)
2008 Symbols_data* sd = this->get_symbols_data();
2009 if (sd != NULL)
2011 const unsigned char* pshdrs = sd->section_headers_data
2012 + This::shdr_size * shndx;
2013 typename This::Shdr shdr(pshdrs);
2014 return shdr.get_sh_flags();
2016 // If sd is NULL, read the section header from the file.
2017 return this->elf_file_.section_flags(shndx);
2020 // Get the section's ent size from Symbols_data. Called by get_section_contents
2021 // in icf.cc
2023 template<int size, bool big_endian>
2024 uint64_t
2025 Sized_relobj<size, big_endian>::do_section_entsize(unsigned int shndx)
2027 Symbols_data* sd = this->get_symbols_data();
2028 gold_assert (sd != NULL);
2030 const unsigned char* pshdrs = sd->section_headers_data
2031 + This::shdr_size * shndx;
2032 typename This::Shdr shdr(pshdrs);
2033 return shdr.get_sh_entsize();
2037 // Write out the local symbols.
2039 template<int size, bool big_endian>
2040 void
2041 Sized_relobj<size, big_endian>::write_local_symbols(
2042 Output_file* of,
2043 const Stringpool* sympool,
2044 const Stringpool* dynpool,
2045 Output_symtab_xindex* symtab_xindex,
2046 Output_symtab_xindex* dynsym_xindex)
2048 const bool strip_all = parameters->options().strip_all();
2049 if (strip_all)
2051 if (this->output_local_dynsym_count_ == 0)
2052 return;
2053 this->output_local_symbol_count_ = 0;
2056 gold_assert(this->symtab_shndx_ != -1U);
2057 if (this->symtab_shndx_ == 0)
2059 // This object has no symbols. Weird but legal.
2060 return;
2063 // Read the symbol table section header.
2064 const unsigned int symtab_shndx = this->symtab_shndx_;
2065 typename This::Shdr symtabshdr(this,
2066 this->elf_file_.section_header(symtab_shndx));
2067 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2068 const unsigned int loccount = this->local_symbol_count_;
2069 gold_assert(loccount == symtabshdr.get_sh_info());
2071 // Read the local symbols.
2072 const int sym_size = This::sym_size;
2073 off_t locsize = loccount * sym_size;
2074 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
2075 locsize, true, false);
2077 // Read the symbol names.
2078 const unsigned int strtab_shndx =
2079 this->adjust_shndx(symtabshdr.get_sh_link());
2080 section_size_type strtab_size;
2081 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
2082 &strtab_size,
2083 false);
2084 const char* pnames = reinterpret_cast<const char*>(pnamesu);
2086 // Get views into the output file for the portions of the symbol table
2087 // and the dynamic symbol table that we will be writing.
2088 off_t output_size = this->output_local_symbol_count_ * sym_size;
2089 unsigned char* oview = NULL;
2090 if (output_size > 0)
2091 oview = of->get_output_view(this->local_symbol_offset_, output_size);
2093 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2094 unsigned char* dyn_oview = NULL;
2095 if (dyn_output_size > 0)
2096 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2097 dyn_output_size);
2099 const Output_sections out_sections(this->output_sections());
2101 gold_assert(this->local_values_.size() == loccount);
2103 unsigned char* ov = oview;
2104 unsigned char* dyn_ov = dyn_oview;
2105 psyms += sym_size;
2106 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2108 elfcpp::Sym<size, big_endian> isym(psyms);
2110 Symbol_value<size>& lv(this->local_values_[i]);
2112 bool is_ordinary;
2113 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
2114 &is_ordinary);
2115 if (is_ordinary)
2117 gold_assert(st_shndx < out_sections.size());
2118 if (out_sections[st_shndx] == NULL)
2119 continue;
2120 st_shndx = out_sections[st_shndx]->out_shndx();
2121 if (st_shndx >= elfcpp::SHN_LORESERVE)
2123 if (lv.has_output_symtab_entry())
2124 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
2125 if (lv.has_output_dynsym_entry())
2126 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
2127 st_shndx = elfcpp::SHN_XINDEX;
2131 // Write the symbol to the output symbol table.
2132 if (lv.has_output_symtab_entry())
2134 elfcpp::Sym_write<size, big_endian> osym(ov);
2136 gold_assert(isym.get_st_name() < strtab_size);
2137 const char* name = pnames + isym.get_st_name();
2138 osym.put_st_name(sympool->get_offset(name));
2139 osym.put_st_value(this->local_values_[i].value(this, 0));
2140 osym.put_st_size(isym.get_st_size());
2141 osym.put_st_info(isym.get_st_info());
2142 osym.put_st_other(isym.get_st_other());
2143 osym.put_st_shndx(st_shndx);
2145 ov += sym_size;
2148 // Write the symbol to the output dynamic symbol table.
2149 if (lv.has_output_dynsym_entry())
2151 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2152 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2154 gold_assert(isym.get_st_name() < strtab_size);
2155 const char* name = pnames + isym.get_st_name();
2156 osym.put_st_name(dynpool->get_offset(name));
2157 osym.put_st_value(this->local_values_[i].value(this, 0));
2158 osym.put_st_size(isym.get_st_size());
2159 osym.put_st_info(isym.get_st_info());
2160 osym.put_st_other(isym.get_st_other());
2161 osym.put_st_shndx(st_shndx);
2163 dyn_ov += sym_size;
2168 if (output_size > 0)
2170 gold_assert(ov - oview == output_size);
2171 of->write_output_view(this->local_symbol_offset_, output_size, oview);
2174 if (dyn_output_size > 0)
2176 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2177 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2178 dyn_oview);
2182 // Set *INFO to symbolic information about the offset OFFSET in the
2183 // section SHNDX. Return true if we found something, false if we
2184 // found nothing.
2186 template<int size, bool big_endian>
2187 bool
2188 Sized_relobj<size, big_endian>::get_symbol_location_info(
2189 unsigned int shndx,
2190 off_t offset,
2191 Symbol_location_info* info)
2193 if (this->symtab_shndx_ == 0)
2194 return false;
2196 section_size_type symbols_size;
2197 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
2198 &symbols_size,
2199 false);
2201 unsigned int symbol_names_shndx =
2202 this->adjust_shndx(this->section_link(this->symtab_shndx_));
2203 section_size_type names_size;
2204 const unsigned char* symbol_names_u =
2205 this->section_contents(symbol_names_shndx, &names_size, false);
2206 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
2208 const int sym_size = This::sym_size;
2209 const size_t count = symbols_size / sym_size;
2211 const unsigned char* p = symbols;
2212 for (size_t i = 0; i < count; ++i, p += sym_size)
2214 elfcpp::Sym<size, big_endian> sym(p);
2216 if (sym.get_st_type() == elfcpp::STT_FILE)
2218 if (sym.get_st_name() >= names_size)
2219 info->source_file = "(invalid)";
2220 else
2221 info->source_file = symbol_names + sym.get_st_name();
2222 continue;
2225 bool is_ordinary;
2226 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2227 &is_ordinary);
2228 if (is_ordinary
2229 && st_shndx == shndx
2230 && static_cast<off_t>(sym.get_st_value()) <= offset
2231 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
2232 > offset))
2234 if (sym.get_st_name() > names_size)
2235 info->enclosing_symbol_name = "(invalid)";
2236 else
2238 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
2239 if (parameters->options().do_demangle())
2241 char* demangled_name = cplus_demangle(
2242 info->enclosing_symbol_name.c_str(),
2243 DMGL_ANSI | DMGL_PARAMS);
2244 if (demangled_name != NULL)
2246 info->enclosing_symbol_name.assign(demangled_name);
2247 free(demangled_name);
2251 return true;
2255 return false;
2258 // Look for a kept section corresponding to the given discarded section,
2259 // and return its output address. This is used only for relocations in
2260 // debugging sections. If we can't find the kept section, return 0.
2262 template<int size, bool big_endian>
2263 typename Sized_relobj<size, big_endian>::Address
2264 Sized_relobj<size, big_endian>::map_to_kept_section(
2265 unsigned int shndx,
2266 bool* found) const
2268 Relobj* kept_object;
2269 unsigned int kept_shndx;
2270 if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
2272 Sized_relobj<size, big_endian>* kept_relobj =
2273 static_cast<Sized_relobj<size, big_endian>*>(kept_object);
2274 Output_section* os = kept_relobj->output_section(kept_shndx);
2275 Address offset = kept_relobj->get_output_section_offset(kept_shndx);
2276 if (os != NULL && offset != invalid_address)
2278 *found = true;
2279 return os->address() + offset;
2282 *found = false;
2283 return 0;
2286 // Get symbol counts.
2288 template<int size, bool big_endian>
2289 void
2290 Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
2291 const Symbol_table*,
2292 size_t* defined,
2293 size_t* used) const
2295 *defined = this->defined_count_;
2296 size_t count = 0;
2297 for (Symbols::const_iterator p = this->symbols_.begin();
2298 p != this->symbols_.end();
2299 ++p)
2300 if (*p != NULL
2301 && (*p)->source() == Symbol::FROM_OBJECT
2302 && (*p)->object() == this
2303 && (*p)->is_defined())
2304 ++count;
2305 *used = count;
2308 // Input_objects methods.
2310 // Add a regular relocatable object to the list. Return false if this
2311 // object should be ignored.
2313 bool
2314 Input_objects::add_object(Object* obj)
2316 // Print the filename if the -t/--trace option is selected.
2317 if (parameters->options().trace())
2318 gold_info("%s", obj->name().c_str());
2320 if (!obj->is_dynamic())
2321 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
2322 else
2324 // See if this is a duplicate SONAME.
2325 Dynobj* dynobj = static_cast<Dynobj*>(obj);
2326 const char* soname = dynobj->soname();
2328 std::pair<Unordered_set<std::string>::iterator, bool> ins =
2329 this->sonames_.insert(soname);
2330 if (!ins.second)
2332 // We have already seen a dynamic object with this soname.
2333 return false;
2336 this->dynobj_list_.push_back(dynobj);
2339 // Add this object to the cross-referencer if requested.
2340 if (parameters->options().user_set_print_symbol_counts()
2341 || parameters->options().cref())
2343 if (this->cref_ == NULL)
2344 this->cref_ = new Cref();
2345 this->cref_->add_object(obj);
2348 return true;
2351 // For each dynamic object, record whether we've seen all of its
2352 // explicit dependencies.
2354 void
2355 Input_objects::check_dynamic_dependencies() const
2357 bool issued_copy_dt_needed_error = false;
2358 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
2359 p != this->dynobj_list_.end();
2360 ++p)
2362 const Dynobj::Needed& needed((*p)->needed());
2363 bool found_all = true;
2364 Dynobj::Needed::const_iterator pneeded;
2365 for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
2367 if (this->sonames_.find(*pneeded) == this->sonames_.end())
2369 found_all = false;
2370 break;
2373 (*p)->set_has_unknown_needed_entries(!found_all);
2375 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2376 // that gold does not support. However, they cause no trouble
2377 // unless there is a DT_NEEDED entry that we don't know about;
2378 // warn only in that case.
2379 if (!found_all
2380 && !issued_copy_dt_needed_error
2381 && (parameters->options().copy_dt_needed_entries()
2382 || parameters->options().add_needed()))
2384 const char* optname;
2385 if (parameters->options().copy_dt_needed_entries())
2386 optname = "--copy-dt-needed-entries";
2387 else
2388 optname = "--add-needed";
2389 gold_error(_("%s is not supported but is required for %s in %s"),
2390 optname, (*pneeded).c_str(), (*p)->name().c_str());
2391 issued_copy_dt_needed_error = true;
2396 // Start processing an archive.
2398 void
2399 Input_objects::archive_start(Archive* archive)
2401 if (parameters->options().user_set_print_symbol_counts()
2402 || parameters->options().cref())
2404 if (this->cref_ == NULL)
2405 this->cref_ = new Cref();
2406 this->cref_->add_archive_start(archive);
2410 // Stop processing an archive.
2412 void
2413 Input_objects::archive_stop(Archive* archive)
2415 if (parameters->options().user_set_print_symbol_counts()
2416 || parameters->options().cref())
2417 this->cref_->add_archive_stop(archive);
2420 // Print symbol counts
2422 void
2423 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
2425 if (parameters->options().user_set_print_symbol_counts()
2426 && this->cref_ != NULL)
2427 this->cref_->print_symbol_counts(symtab);
2430 // Print a cross reference table.
2432 void
2433 Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
2435 if (parameters->options().cref() && this->cref_ != NULL)
2436 this->cref_->print_cref(symtab, f);
2439 // Relocate_info methods.
2441 // Return a string describing the location of a relocation. This is
2442 // only used in error messages.
2444 template<int size, bool big_endian>
2445 std::string
2446 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
2448 // See if we can get line-number information from debugging sections.
2449 std::string filename;
2450 std::string file_and_lineno; // Better than filename-only, if available.
2452 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
2453 // This will be "" if we failed to parse the debug info for any reason.
2454 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
2456 std::string ret(this->object->name());
2457 ret += ':';
2458 Symbol_location_info info;
2459 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
2461 ret += " in function ";
2462 ret += info.enclosing_symbol_name;
2463 ret += ":";
2464 filename = info.source_file;
2467 if (!file_and_lineno.empty())
2468 ret += file_and_lineno;
2469 else
2471 if (!filename.empty())
2472 ret += filename;
2473 ret += "(";
2474 ret += this->object->section_name(this->data_shndx);
2475 char buf[100];
2476 // Offsets into sections have to be positive.
2477 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
2478 ret += buf;
2479 ret += ")";
2481 return ret;
2484 } // End namespace gold.
2486 namespace
2489 using namespace gold;
2491 // Read an ELF file with the header and return the appropriate
2492 // instance of Object.
2494 template<int size, bool big_endian>
2495 Object*
2496 make_elf_sized_object(const std::string& name, Input_file* input_file,
2497 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
2498 bool* punconfigured)
2500 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
2501 ehdr.get_e_ident()[elfcpp::EI_OSABI],
2502 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
2503 if (target == NULL)
2504 gold_fatal(_("%s: unsupported ELF machine number %d"),
2505 name.c_str(), ehdr.get_e_machine());
2507 if (!parameters->target_valid())
2508 set_parameters_target(target);
2509 else if (target != &parameters->target())
2511 if (punconfigured != NULL)
2512 *punconfigured = true;
2513 else
2514 gold_error(_("%s: incompatible target"), name.c_str());
2515 return NULL;
2518 return target->make_elf_object<size, big_endian>(name, input_file, offset,
2519 ehdr);
2522 } // End anonymous namespace.
2524 namespace gold
2527 // Return whether INPUT_FILE is an ELF object.
2529 bool
2530 is_elf_object(Input_file* input_file, off_t offset,
2531 const unsigned char** start, int *read_size)
2533 off_t filesize = input_file->file().filesize();
2534 int want = elfcpp::Elf_recognizer::max_header_size;
2535 if (filesize - offset < want)
2536 want = filesize - offset;
2538 const unsigned char* p = input_file->file().get_view(offset, 0, want,
2539 true, false);
2540 *start = p;
2541 *read_size = want;
2543 return elfcpp::Elf_recognizer::is_elf_file(p, want);
2546 // Read an ELF file and return the appropriate instance of Object.
2548 Object*
2549 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
2550 const unsigned char* p, section_offset_type bytes,
2551 bool* punconfigured)
2553 if (punconfigured != NULL)
2554 *punconfigured = false;
2556 std::string error;
2557 bool big_endian = false;
2558 int size = 0;
2559 if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
2560 &big_endian, &error))
2562 gold_error(_("%s: %s"), name.c_str(), error.c_str());
2563 return NULL;
2566 if (size == 32)
2568 if (big_endian)
2570 #ifdef HAVE_TARGET_32_BIG
2571 elfcpp::Ehdr<32, true> ehdr(p);
2572 return make_elf_sized_object<32, true>(name, input_file,
2573 offset, ehdr, punconfigured);
2574 #else
2575 if (punconfigured != NULL)
2576 *punconfigured = true;
2577 else
2578 gold_error(_("%s: not configured to support "
2579 "32-bit big-endian object"),
2580 name.c_str());
2581 return NULL;
2582 #endif
2584 else
2586 #ifdef HAVE_TARGET_32_LITTLE
2587 elfcpp::Ehdr<32, false> ehdr(p);
2588 return make_elf_sized_object<32, false>(name, input_file,
2589 offset, ehdr, punconfigured);
2590 #else
2591 if (punconfigured != NULL)
2592 *punconfigured = true;
2593 else
2594 gold_error(_("%s: not configured to support "
2595 "32-bit little-endian object"),
2596 name.c_str());
2597 return NULL;
2598 #endif
2601 else if (size == 64)
2603 if (big_endian)
2605 #ifdef HAVE_TARGET_64_BIG
2606 elfcpp::Ehdr<64, true> ehdr(p);
2607 return make_elf_sized_object<64, true>(name, input_file,
2608 offset, ehdr, punconfigured);
2609 #else
2610 if (punconfigured != NULL)
2611 *punconfigured = true;
2612 else
2613 gold_error(_("%s: not configured to support "
2614 "64-bit big-endian object"),
2615 name.c_str());
2616 return NULL;
2617 #endif
2619 else
2621 #ifdef HAVE_TARGET_64_LITTLE
2622 elfcpp::Ehdr<64, false> ehdr(p);
2623 return make_elf_sized_object<64, false>(name, input_file,
2624 offset, ehdr, punconfigured);
2625 #else
2626 if (punconfigured != NULL)
2627 *punconfigured = true;
2628 else
2629 gold_error(_("%s: not configured to support "
2630 "64-bit little-endian object"),
2631 name.c_str());
2632 return NULL;
2633 #endif
2636 else
2637 gold_unreachable();
2640 // Instantiate the templates we need.
2642 #ifdef HAVE_TARGET_32_LITTLE
2643 template
2644 void
2645 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2646 Read_symbols_data*);
2647 #endif
2649 #ifdef HAVE_TARGET_32_BIG
2650 template
2651 void
2652 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2653 Read_symbols_data*);
2654 #endif
2656 #ifdef HAVE_TARGET_64_LITTLE
2657 template
2658 void
2659 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2660 Read_symbols_data*);
2661 #endif
2663 #ifdef HAVE_TARGET_64_BIG
2664 template
2665 void
2666 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2667 Read_symbols_data*);
2668 #endif
2670 #ifdef HAVE_TARGET_32_LITTLE
2671 template
2672 class Sized_relobj<32, false>;
2673 #endif
2675 #ifdef HAVE_TARGET_32_BIG
2676 template
2677 class Sized_relobj<32, true>;
2678 #endif
2680 #ifdef HAVE_TARGET_64_LITTLE
2681 template
2682 class Sized_relobj<64, false>;
2683 #endif
2685 #ifdef HAVE_TARGET_64_BIG
2686 template
2687 class Sized_relobj<64, true>;
2688 #endif
2690 #ifdef HAVE_TARGET_32_LITTLE
2691 template
2692 struct Relocate_info<32, false>;
2693 #endif
2695 #ifdef HAVE_TARGET_32_BIG
2696 template
2697 struct Relocate_info<32, true>;
2698 #endif
2700 #ifdef HAVE_TARGET_64_LITTLE
2701 template
2702 struct Relocate_info<64, false>;
2703 #endif
2705 #ifdef HAVE_TARGET_64_BIG
2706 template
2707 struct Relocate_info<64, true>;
2708 #endif
2710 #ifdef HAVE_TARGET_32_LITTLE
2711 template
2712 void
2713 Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
2715 template
2716 void
2717 Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
2718 const unsigned char*);
2719 #endif
2721 #ifdef HAVE_TARGET_32_BIG
2722 template
2723 void
2724 Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
2726 template
2727 void
2728 Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
2729 const unsigned char*);
2730 #endif
2732 #ifdef HAVE_TARGET_64_LITTLE
2733 template
2734 void
2735 Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
2737 template
2738 void
2739 Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
2740 const unsigned char*);
2741 #endif
2743 #ifdef HAVE_TARGET_64_BIG
2744 template
2745 void
2746 Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
2748 template
2749 void
2750 Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
2751 const unsigned char*);
2752 #endif
2754 } // End namespace gold.