2010-06-16 Vincent Rivire <vincent.riviere@freesbee.fr>
[binutils.git] / gold / object.cc
blobb034ee209acc311f13516cbaec15c1cc6a538b3f
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"
43 namespace gold
46 // Struct Read_symbols_data.
48 // Destroy any remaining File_view objects.
50 Read_symbols_data::~Read_symbols_data()
52 if (this->section_headers != NULL)
53 delete this->section_headers;
54 if (this->section_names != NULL)
55 delete this->section_names;
56 if (this->symbols != NULL)
57 delete this->symbols;
58 if (this->symbol_names != NULL)
59 delete this->symbol_names;
60 if (this->versym != NULL)
61 delete this->versym;
62 if (this->verdef != NULL)
63 delete this->verdef;
64 if (this->verneed != NULL)
65 delete this->verneed;
68 // Class Xindex.
70 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
71 // section and read it in. SYMTAB_SHNDX is the index of the symbol
72 // table we care about.
74 template<int size, bool big_endian>
75 void
76 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
78 if (!this->symtab_xindex_.empty())
79 return;
81 gold_assert(symtab_shndx != 0);
83 // Look through the sections in reverse order, on the theory that it
84 // is more likely to be near the end than the beginning.
85 unsigned int i = object->shnum();
86 while (i > 0)
88 --i;
89 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
90 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
92 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
93 return;
97 object->error(_("missing SHT_SYMTAB_SHNDX section"));
100 // Read in the symtab_xindex_ array, given the section index of the
101 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
102 // section headers.
104 template<int size, bool big_endian>
105 void
106 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
107 const unsigned char* pshdrs)
109 section_size_type bytecount;
110 const unsigned char* contents;
111 if (pshdrs == NULL)
112 contents = object->section_contents(xindex_shndx, &bytecount, false);
113 else
115 const unsigned char* p = (pshdrs
116 + (xindex_shndx
117 * elfcpp::Elf_sizes<size>::shdr_size));
118 typename elfcpp::Shdr<size, big_endian> shdr(p);
119 bytecount = convert_to_section_size_type(shdr.get_sh_size());
120 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
123 gold_assert(this->symtab_xindex_.empty());
124 this->symtab_xindex_.reserve(bytecount / 4);
125 for (section_size_type i = 0; i < bytecount; i += 4)
127 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
128 // We preadjust the section indexes we save.
129 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
133 // Symbol symndx has a section of SHN_XINDEX; return the real section
134 // index.
136 unsigned int
137 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
139 if (symndx >= this->symtab_xindex_.size())
141 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
142 symndx);
143 return elfcpp::SHN_UNDEF;
145 unsigned int shndx = this->symtab_xindex_[symndx];
146 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
148 object->error(_("extended index for symbol %u out of range: %u"),
149 symndx, shndx);
150 return elfcpp::SHN_UNDEF;
152 return shndx;
155 // Class Object.
157 // Report an error for this object file. This is used by the
158 // elfcpp::Elf_file interface, and also called by the Object code
159 // itself.
161 void
162 Object::error(const char* format, ...) const
164 va_list args;
165 va_start(args, format);
166 char* buf = NULL;
167 if (vasprintf(&buf, format, args) < 0)
168 gold_nomem();
169 va_end(args);
170 gold_error(_("%s: %s"), this->name().c_str(), buf);
171 free(buf);
174 // Return a view of the contents of a section.
176 const unsigned char*
177 Object::section_contents(unsigned int shndx, section_size_type* plen,
178 bool cache)
180 Location loc(this->do_section_contents(shndx));
181 *plen = convert_to_section_size_type(loc.data_size);
182 if (*plen == 0)
184 static const unsigned char empty[1] = { '\0' };
185 return empty;
187 return this->get_view(loc.file_offset, *plen, true, cache);
190 // Read the section data into SD. This is code common to Sized_relobj
191 // and Sized_dynobj, so we put it into Object.
193 template<int size, bool big_endian>
194 void
195 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
196 Read_symbols_data* sd)
198 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
200 // Read the section headers.
201 const off_t shoff = elf_file->shoff();
202 const unsigned int shnum = this->shnum();
203 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
204 true, true);
206 // Read the section names.
207 const unsigned char* pshdrs = sd->section_headers->data();
208 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
209 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
211 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
212 this->error(_("section name section has wrong type: %u"),
213 static_cast<unsigned int>(shdrnames.get_sh_type()));
215 sd->section_names_size =
216 convert_to_section_size_type(shdrnames.get_sh_size());
217 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
218 sd->section_names_size, false,
219 false);
222 // If NAME is the name of a special .gnu.warning section, arrange for
223 // the warning to be issued. SHNDX is the section index. Return
224 // whether it is a warning section.
226 bool
227 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
228 Symbol_table* symtab)
230 const char warn_prefix[] = ".gnu.warning.";
231 const int warn_prefix_len = sizeof warn_prefix - 1;
232 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
234 // Read the section contents to get the warning text. It would
235 // be nicer if we only did this if we have to actually issue a
236 // warning. Unfortunately, warnings are issued as we relocate
237 // sections. That means that we can not lock the object then,
238 // as we might try to issue the same warning multiple times
239 // simultaneously.
240 section_size_type len;
241 const unsigned char* contents = this->section_contents(shndx, &len,
242 false);
243 if (len == 0)
245 const char* warning = name + warn_prefix_len;
246 contents = reinterpret_cast<const unsigned char*>(warning);
247 len = strlen(warning);
249 std::string warning(reinterpret_cast<const char*>(contents), len);
250 symtab->add_warning(name + warn_prefix_len, this, warning);
251 return true;
253 return false;
256 // If NAME is the name of the special section which indicates that
257 // this object was compiled with -fstack-split, mark it accordingly.
259 bool
260 Object::handle_split_stack_section(const char* name)
262 if (strcmp(name, ".note.GNU-split-stack") == 0)
264 this->uses_split_stack_ = true;
265 return true;
267 if (strcmp(name, ".note.GNU-no-split-stack") == 0)
269 this->has_no_split_stack_ = true;
270 return true;
272 return false;
275 // Class Relobj
277 // To copy the symbols data read from the file to a local data structure.
278 // This function is called from do_layout only while doing garbage
279 // collection.
281 void
282 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
283 unsigned int section_header_size)
285 gc_sd->section_headers_data =
286 new unsigned char[(section_header_size)];
287 memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
288 section_header_size);
289 gc_sd->section_names_data =
290 new unsigned char[sd->section_names_size];
291 memcpy(gc_sd->section_names_data, sd->section_names->data(),
292 sd->section_names_size);
293 gc_sd->section_names_size = sd->section_names_size;
294 if (sd->symbols != NULL)
296 gc_sd->symbols_data =
297 new unsigned char[sd->symbols_size];
298 memcpy(gc_sd->symbols_data, sd->symbols->data(),
299 sd->symbols_size);
301 else
303 gc_sd->symbols_data = NULL;
305 gc_sd->symbols_size = sd->symbols_size;
306 gc_sd->external_symbols_offset = sd->external_symbols_offset;
307 if (sd->symbol_names != NULL)
309 gc_sd->symbol_names_data =
310 new unsigned char[sd->symbol_names_size];
311 memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
312 sd->symbol_names_size);
314 else
316 gc_sd->symbol_names_data = NULL;
318 gc_sd->symbol_names_size = sd->symbol_names_size;
321 // This function determines if a particular section name must be included
322 // in the link. This is used during garbage collection to determine the
323 // roots of the worklist.
325 bool
326 Relobj::is_section_name_included(const char* name)
328 if (is_prefix_of(".ctors", name)
329 || is_prefix_of(".dtors", name)
330 || is_prefix_of(".note", name)
331 || is_prefix_of(".init", name)
332 || is_prefix_of(".fini", name)
333 || is_prefix_of(".gcc_except_table", name)
334 || is_prefix_of(".jcr", name)
335 || is_prefix_of(".preinit_array", name)
336 || (is_prefix_of(".text", name)
337 && strstr(name, "personality"))
338 || (is_prefix_of(".data", name)
339 && strstr(name, "personality"))
340 || (is_prefix_of(".gnu.linkonce.d", name)
341 && strstr(name, "personality")))
343 return true;
345 return false;
348 // Class Sized_relobj.
350 template<int size, bool big_endian>
351 Sized_relobj<size, big_endian>::Sized_relobj(
352 const std::string& name,
353 Input_file* input_file,
354 off_t offset,
355 const elfcpp::Ehdr<size, big_endian>& ehdr)
356 : Relobj(name, input_file, offset),
357 elf_file_(this, ehdr),
358 symtab_shndx_(-1U),
359 local_symbol_count_(0),
360 output_local_symbol_count_(0),
361 output_local_dynsym_count_(0),
362 symbols_(),
363 defined_count_(0),
364 local_symbol_offset_(0),
365 local_dynsym_offset_(0),
366 local_values_(),
367 local_got_offsets_(),
368 kept_comdat_sections_(),
369 has_eh_frame_(false),
370 discarded_eh_frame_shndx_(-1U)
374 template<int size, bool big_endian>
375 Sized_relobj<size, big_endian>::~Sized_relobj()
379 // Set up an object file based on the file header. This sets up the
380 // section information.
382 template<int size, bool big_endian>
383 void
384 Sized_relobj<size, big_endian>::do_setup()
386 const unsigned int shnum = this->elf_file_.shnum();
387 this->set_shnum(shnum);
390 // Find the SHT_SYMTAB section, given the section headers. The ELF
391 // standard says that maybe in the future there can be more than one
392 // SHT_SYMTAB section. Until somebody figures out how that could
393 // work, we assume there is only one.
395 template<int size, bool big_endian>
396 void
397 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
399 const unsigned int shnum = this->shnum();
400 this->symtab_shndx_ = 0;
401 if (shnum > 0)
403 // Look through the sections in reverse order, since gas tends
404 // to put the symbol table at the end.
405 const unsigned char* p = pshdrs + shnum * This::shdr_size;
406 unsigned int i = shnum;
407 unsigned int xindex_shndx = 0;
408 unsigned int xindex_link = 0;
409 while (i > 0)
411 --i;
412 p -= This::shdr_size;
413 typename This::Shdr shdr(p);
414 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
416 this->symtab_shndx_ = i;
417 if (xindex_shndx > 0 && xindex_link == i)
419 Xindex* xindex =
420 new Xindex(this->elf_file_.large_shndx_offset());
421 xindex->read_symtab_xindex<size, big_endian>(this,
422 xindex_shndx,
423 pshdrs);
424 this->set_xindex(xindex);
426 break;
429 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
430 // one. This will work if it follows the SHT_SYMTAB
431 // section.
432 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
434 xindex_shndx = i;
435 xindex_link = this->adjust_shndx(shdr.get_sh_link());
441 // Return the Xindex structure to use for object with lots of
442 // sections.
444 template<int size, bool big_endian>
445 Xindex*
446 Sized_relobj<size, big_endian>::do_initialize_xindex()
448 gold_assert(this->symtab_shndx_ != -1U);
449 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
450 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
451 return xindex;
454 // Return whether SHDR has the right type and flags to be a GNU
455 // .eh_frame section.
457 template<int size, bool big_endian>
458 bool
459 Sized_relobj<size, big_endian>::check_eh_frame_flags(
460 const elfcpp::Shdr<size, big_endian>* shdr) const
462 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
463 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
466 // Return whether there is a GNU .eh_frame section, given the section
467 // headers and the section names.
469 template<int size, bool big_endian>
470 bool
471 Sized_relobj<size, big_endian>::find_eh_frame(
472 const unsigned char* pshdrs,
473 const char* names,
474 section_size_type names_size) const
476 const unsigned int shnum = this->shnum();
477 const unsigned char* p = pshdrs + This::shdr_size;
478 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
480 typename This::Shdr shdr(p);
481 if (this->check_eh_frame_flags(&shdr))
483 if (shdr.get_sh_name() >= names_size)
485 this->error(_("bad section name offset for section %u: %lu"),
486 i, static_cast<unsigned long>(shdr.get_sh_name()));
487 continue;
490 const char* name = names + shdr.get_sh_name();
491 if (strcmp(name, ".eh_frame") == 0)
492 return true;
495 return false;
498 // Read the sections and symbols from an object file.
500 template<int size, bool big_endian>
501 void
502 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
504 this->read_section_data(&this->elf_file_, sd);
506 const unsigned char* const pshdrs = sd->section_headers->data();
508 this->find_symtab(pshdrs);
510 const unsigned char* namesu = sd->section_names->data();
511 const char* names = reinterpret_cast<const char*>(namesu);
512 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
514 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
515 this->has_eh_frame_ = true;
518 sd->symbols = NULL;
519 sd->symbols_size = 0;
520 sd->external_symbols_offset = 0;
521 sd->symbol_names = NULL;
522 sd->symbol_names_size = 0;
524 if (this->symtab_shndx_ == 0)
526 // No symbol table. Weird but legal.
527 return;
530 // Get the symbol table section header.
531 typename This::Shdr symtabshdr(pshdrs
532 + this->symtab_shndx_ * This::shdr_size);
533 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
535 // If this object has a .eh_frame section, we need all the symbols.
536 // Otherwise we only need the external symbols. While it would be
537 // simpler to just always read all the symbols, I've seen object
538 // files with well over 2000 local symbols, which for a 64-bit
539 // object file format is over 5 pages that we don't need to read
540 // now.
542 const int sym_size = This::sym_size;
543 const unsigned int loccount = symtabshdr.get_sh_info();
544 this->local_symbol_count_ = loccount;
545 this->local_values_.resize(loccount);
546 section_offset_type locsize = loccount * sym_size;
547 off_t dataoff = symtabshdr.get_sh_offset();
548 section_size_type datasize =
549 convert_to_section_size_type(symtabshdr.get_sh_size());
550 off_t extoff = dataoff + locsize;
551 section_size_type extsize = datasize - locsize;
553 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
554 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
556 if (readsize == 0)
558 // No external symbols. Also weird but also legal.
559 return;
562 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
564 // Read the section header for the symbol names.
565 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
566 if (strtab_shndx >= this->shnum())
568 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
569 return;
571 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
572 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
574 this->error(_("symbol table name section has wrong type: %u"),
575 static_cast<unsigned int>(strtabshdr.get_sh_type()));
576 return;
579 // Read the symbol names.
580 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
581 strtabshdr.get_sh_size(),
582 false, true);
584 sd->symbols = fvsymtab;
585 sd->symbols_size = readsize;
586 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
587 sd->symbol_names = fvstrtab;
588 sd->symbol_names_size =
589 convert_to_section_size_type(strtabshdr.get_sh_size());
592 // Return the section index of symbol SYM. Set *VALUE to its value in
593 // the object file. Set *IS_ORDINARY if this is an ordinary section
594 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
595 // Note that for a symbol which is not defined in this object file,
596 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
597 // the final value of the symbol in the link.
599 template<int size, bool big_endian>
600 unsigned int
601 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
602 Address* value,
603 bool* is_ordinary)
605 section_size_type symbols_size;
606 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
607 &symbols_size,
608 false);
610 const size_t count = symbols_size / This::sym_size;
611 gold_assert(sym < count);
613 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
614 *value = elfsym.get_st_value();
616 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
619 // Return whether to include a section group in the link. LAYOUT is
620 // used to keep track of which section groups we have already seen.
621 // INDEX is the index of the section group and SHDR is the section
622 // header. If we do not want to include this group, we set bits in
623 // OMIT for each section which should be discarded.
625 template<int size, bool big_endian>
626 bool
627 Sized_relobj<size, big_endian>::include_section_group(
628 Symbol_table* symtab,
629 Layout* layout,
630 unsigned int index,
631 const char* name,
632 const unsigned char* shdrs,
633 const char* section_names,
634 section_size_type section_names_size,
635 std::vector<bool>* omit)
637 // Read the section contents.
638 typename This::Shdr shdr(shdrs + index * This::shdr_size);
639 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
640 shdr.get_sh_size(), true, false);
641 const elfcpp::Elf_Word* pword =
642 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
644 // The first word contains flags. We only care about COMDAT section
645 // groups. Other section groups are always included in the link
646 // just like ordinary sections.
647 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
649 // Look up the group signature, which is the name of a symbol. This
650 // is a lot of effort to go to to read a string. Why didn't they
651 // just have the group signature point into the string table, rather
652 // than indirect through a symbol?
654 // Get the appropriate symbol table header (this will normally be
655 // the single SHT_SYMTAB section, but in principle it need not be).
656 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
657 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
659 // Read the symbol table entry.
660 unsigned int symndx = shdr.get_sh_info();
661 if (symndx >= symshdr.get_sh_size() / This::sym_size)
663 this->error(_("section group %u info %u out of range"),
664 index, symndx);
665 return false;
667 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
668 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
669 false);
670 elfcpp::Sym<size, big_endian> sym(psym);
672 // Read the symbol table names.
673 section_size_type symnamelen;
674 const unsigned char* psymnamesu;
675 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
676 &symnamelen, true);
677 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
679 // Get the section group signature.
680 if (sym.get_st_name() >= symnamelen)
682 this->error(_("symbol %u name offset %u out of range"),
683 symndx, sym.get_st_name());
684 return false;
687 std::string signature(psymnames + sym.get_st_name());
689 // It seems that some versions of gas will create a section group
690 // associated with a section symbol, and then fail to give a name to
691 // the section symbol. In such a case, use the name of the section.
692 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
694 bool is_ordinary;
695 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
696 sym.get_st_shndx(),
697 &is_ordinary);
698 if (!is_ordinary || sym_shndx >= this->shnum())
700 this->error(_("symbol %u invalid section index %u"),
701 symndx, sym_shndx);
702 return false;
704 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
705 if (member_shdr.get_sh_name() < section_names_size)
706 signature = section_names + member_shdr.get_sh_name();
709 // Record this section group in the layout, and see whether we've already
710 // seen one with the same signature.
711 bool include_group;
712 bool is_comdat;
713 Kept_section* kept_section = NULL;
715 if ((flags & elfcpp::GRP_COMDAT) == 0)
717 include_group = true;
718 is_comdat = false;
720 else
722 include_group = layout->find_or_add_kept_section(signature,
723 this, index, true,
724 true, &kept_section);
725 is_comdat = true;
728 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
730 std::vector<unsigned int> shndxes;
731 bool relocate_group = include_group && parameters->options().relocatable();
732 if (relocate_group)
733 shndxes.reserve(count - 1);
735 for (size_t i = 1; i < count; ++i)
737 elfcpp::Elf_Word shndx =
738 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
740 if (relocate_group)
741 shndxes.push_back(shndx);
743 if (shndx >= this->shnum())
745 this->error(_("section %u in section group %u out of range"),
746 shndx, index);
747 continue;
750 // Check for an earlier section number, since we're going to get
751 // it wrong--we may have already decided to include the section.
752 if (shndx < index)
753 this->error(_("invalid section group %u refers to earlier section %u"),
754 index, shndx);
756 // Get the name of the member section.
757 typename This::Shdr member_shdr(shdrs + shndx * This::shdr_size);
758 if (member_shdr.get_sh_name() >= section_names_size)
760 // This is an error, but it will be diagnosed eventually
761 // in do_layout, so we don't need to do anything here but
762 // ignore it.
763 continue;
765 std::string mname(section_names + member_shdr.get_sh_name());
767 if (include_group)
769 if (is_comdat)
770 kept_section->add_comdat_section(mname, shndx,
771 member_shdr.get_sh_size());
773 else
775 (*omit)[shndx] = true;
777 if (is_comdat)
779 Relobj* kept_object = kept_section->object();
780 if (kept_section->is_comdat())
782 // Find the corresponding kept section, and store
783 // that info in the discarded section table.
784 unsigned int kept_shndx;
785 uint64_t kept_size;
786 if (kept_section->find_comdat_section(mname, &kept_shndx,
787 &kept_size))
789 // We don't keep a mapping for this section if
790 // it has a different size. The mapping is only
791 // used for relocation processing, and we don't
792 // want to treat the sections as similar if the
793 // sizes are different. Checking the section
794 // size is the approach used by the GNU linker.
795 if (kept_size == member_shdr.get_sh_size())
796 this->set_kept_comdat_section(shndx, kept_object,
797 kept_shndx);
800 else
802 // The existing section is a linkonce section. Add
803 // a mapping if there is exactly one section in the
804 // group (which is true when COUNT == 2) and if it
805 // is the same size.
806 if (count == 2
807 && (kept_section->linkonce_size()
808 == member_shdr.get_sh_size()))
809 this->set_kept_comdat_section(shndx, kept_object,
810 kept_section->shndx());
816 if (relocate_group)
817 layout->layout_group(symtab, this, index, name, signature.c_str(),
818 shdr, flags, &shndxes);
820 return include_group;
823 // Whether to include a linkonce section in the link. NAME is the
824 // name of the section and SHDR is the section header.
826 // Linkonce sections are a GNU extension implemented in the original
827 // GNU linker before section groups were defined. The semantics are
828 // that we only include one linkonce section with a given name. The
829 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
830 // where T is the type of section and SYMNAME is the name of a symbol.
831 // In an attempt to make linkonce sections interact well with section
832 // groups, we try to identify SYMNAME and use it like a section group
833 // signature. We want to block section groups with that signature,
834 // but not other linkonce sections with that signature. We also use
835 // the full name of the linkonce section as a normal section group
836 // signature.
838 template<int size, bool big_endian>
839 bool
840 Sized_relobj<size, big_endian>::include_linkonce_section(
841 Layout* layout,
842 unsigned int index,
843 const char* name,
844 const elfcpp::Shdr<size, big_endian>& shdr)
846 typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
847 // In general the symbol name we want will be the string following
848 // the last '.'. However, we have to handle the case of
849 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
850 // some versions of gcc. So we use a heuristic: if the name starts
851 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
852 // we look for the last '.'. We can't always simply skip
853 // ".gnu.linkonce.X", because we have to deal with cases like
854 // ".gnu.linkonce.d.rel.ro.local".
855 const char* const linkonce_t = ".gnu.linkonce.t.";
856 const char* symname;
857 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
858 symname = name + strlen(linkonce_t);
859 else
860 symname = strrchr(name, '.') + 1;
861 std::string sig1(symname);
862 std::string sig2(name);
863 Kept_section* kept1;
864 Kept_section* kept2;
865 bool include1 = layout->find_or_add_kept_section(sig1, this, index, false,
866 false, &kept1);
867 bool include2 = layout->find_or_add_kept_section(sig2, this, index, false,
868 true, &kept2);
870 if (!include2)
872 // We are not including this section because we already saw the
873 // name of the section as a signature. This normally implies
874 // that the kept section is another linkonce section. If it is
875 // the same size, record it as the section which corresponds to
876 // this one.
877 if (kept2->object() != NULL
878 && !kept2->is_comdat()
879 && kept2->linkonce_size() == sh_size)
880 this->set_kept_comdat_section(index, kept2->object(), kept2->shndx());
882 else if (!include1)
884 // The section is being discarded on the basis of its symbol
885 // name. This means that the corresponding kept section was
886 // part of a comdat group, and it will be difficult to identify
887 // the specific section within that group that corresponds to
888 // this linkonce section. We'll handle the simple case where
889 // the group has only one member section. Otherwise, it's not
890 // worth the effort.
891 unsigned int kept_shndx;
892 uint64_t kept_size;
893 if (kept1->object() != NULL
894 && kept1->is_comdat()
895 && kept1->find_single_comdat_section(&kept_shndx, &kept_size)
896 && kept_size == sh_size)
897 this->set_kept_comdat_section(index, kept1->object(), kept_shndx);
899 else
901 kept1->set_linkonce_size(sh_size);
902 kept2->set_linkonce_size(sh_size);
905 return include1 && include2;
908 // Layout an input section.
910 template<int size, bool big_endian>
911 inline void
912 Sized_relobj<size, big_endian>::layout_section(Layout* layout,
913 unsigned int shndx,
914 const char* name,
915 typename This::Shdr& shdr,
916 unsigned int reloc_shndx,
917 unsigned int reloc_type)
919 off_t offset;
920 Output_section* os = layout->layout(this, shndx, name, shdr,
921 reloc_shndx, reloc_type, &offset);
923 this->output_sections()[shndx] = os;
924 if (offset == -1)
925 this->section_offsets_[shndx] = invalid_address;
926 else
927 this->section_offsets_[shndx] = convert_types<Address, off_t>(offset);
929 // If this section requires special handling, and if there are
930 // relocs that apply to it, then we must do the special handling
931 // before we apply the relocs.
932 if (offset == -1 && reloc_shndx != 0)
933 this->set_relocs_must_follow_section_writes();
936 // Lay out the input sections. We walk through the sections and check
937 // whether they should be included in the link. If they should, we
938 // pass them to the Layout object, which will return an output section
939 // and an offset.
940 // During garbage collection (--gc-sections) and identical code folding
941 // (--icf), this function is called twice. When it is called the first
942 // time, it is for setting up some sections as roots to a work-list for
943 // --gc-sections and to do comdat processing. Actual layout happens the
944 // second time around after all the relevant sections have been determined.
945 // The first time, is_worklist_ready or is_icf_ready is false. It is then
946 // set to true after the garbage collection worklist or identical code
947 // folding is processed and the relevant sections to be kept are
948 // determined. Then, this function is called again to layout the sections.
950 template<int size, bool big_endian>
951 void
952 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
953 Layout* layout,
954 Read_symbols_data* sd)
956 const unsigned int shnum = this->shnum();
957 bool is_gc_pass_one = ((parameters->options().gc_sections()
958 && !symtab->gc()->is_worklist_ready())
959 || (parameters->options().icf_enabled()
960 && !symtab->icf()->is_icf_ready()));
962 bool is_gc_pass_two = ((parameters->options().gc_sections()
963 && symtab->gc()->is_worklist_ready())
964 || (parameters->options().icf_enabled()
965 && symtab->icf()->is_icf_ready()));
967 bool is_gc_or_icf = (parameters->options().gc_sections()
968 || parameters->options().icf_enabled());
970 // Both is_gc_pass_one and is_gc_pass_two should not be true.
971 gold_assert(!(is_gc_pass_one && is_gc_pass_two));
973 if (shnum == 0)
974 return;
975 Symbols_data* gc_sd = NULL;
976 if (is_gc_pass_one)
978 // During garbage collection save the symbols data to use it when
979 // re-entering this function.
980 gc_sd = new Symbols_data;
981 this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
982 this->set_symbols_data(gc_sd);
984 else if (is_gc_pass_two)
986 gc_sd = this->get_symbols_data();
989 const unsigned char* section_headers_data = NULL;
990 section_size_type section_names_size;
991 const unsigned char* symbols_data = NULL;
992 section_size_type symbols_size;
993 section_offset_type external_symbols_offset;
994 const unsigned char* symbol_names_data = NULL;
995 section_size_type symbol_names_size;
997 if (is_gc_or_icf)
999 section_headers_data = gc_sd->section_headers_data;
1000 section_names_size = gc_sd->section_names_size;
1001 symbols_data = gc_sd->symbols_data;
1002 symbols_size = gc_sd->symbols_size;
1003 external_symbols_offset = gc_sd->external_symbols_offset;
1004 symbol_names_data = gc_sd->symbol_names_data;
1005 symbol_names_size = gc_sd->symbol_names_size;
1007 else
1009 section_headers_data = sd->section_headers->data();
1010 section_names_size = sd->section_names_size;
1011 if (sd->symbols != NULL)
1012 symbols_data = sd->symbols->data();
1013 symbols_size = sd->symbols_size;
1014 external_symbols_offset = sd->external_symbols_offset;
1015 if (sd->symbol_names != NULL)
1016 symbol_names_data = sd->symbol_names->data();
1017 symbol_names_size = sd->symbol_names_size;
1020 // Get the section headers.
1021 const unsigned char* shdrs = section_headers_data;
1022 const unsigned char* pshdrs;
1024 // Get the section names.
1025 const unsigned char* pnamesu = (is_gc_or_icf)
1026 ? gc_sd->section_names_data
1027 : sd->section_names->data();
1029 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1031 // If any input files have been claimed by plugins, we need to defer
1032 // actual layout until the replacement files have arrived.
1033 const bool should_defer_layout =
1034 (parameters->options().has_plugins()
1035 && parameters->options().plugins()->should_defer_layout());
1036 unsigned int num_sections_to_defer = 0;
1038 // For each section, record the index of the reloc section if any.
1039 // Use 0 to mean that there is no reloc section, -1U to mean that
1040 // there is more than one.
1041 std::vector<unsigned int> reloc_shndx(shnum, 0);
1042 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
1043 // Skip the first, dummy, section.
1044 pshdrs = shdrs + This::shdr_size;
1045 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1047 typename This::Shdr shdr(pshdrs);
1049 // Count the number of sections whose layout will be deferred.
1050 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1051 ++num_sections_to_defer;
1053 unsigned int sh_type = shdr.get_sh_type();
1054 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
1056 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
1057 if (target_shndx == 0 || target_shndx >= shnum)
1059 this->error(_("relocation section %u has bad info %u"),
1060 i, target_shndx);
1061 continue;
1064 if (reloc_shndx[target_shndx] != 0)
1065 reloc_shndx[target_shndx] = -1U;
1066 else
1068 reloc_shndx[target_shndx] = i;
1069 reloc_type[target_shndx] = sh_type;
1074 Output_sections& out_sections(this->output_sections());
1075 std::vector<Address>& out_section_offsets(this->section_offsets_);
1077 if (!is_gc_pass_two)
1079 out_sections.resize(shnum);
1080 out_section_offsets.resize(shnum);
1083 // If we are only linking for symbols, then there is nothing else to
1084 // do here.
1085 if (this->input_file()->just_symbols())
1087 if (!is_gc_pass_two)
1089 delete sd->section_headers;
1090 sd->section_headers = NULL;
1091 delete sd->section_names;
1092 sd->section_names = NULL;
1094 return;
1097 if (num_sections_to_defer > 0)
1099 parameters->options().plugins()->add_deferred_layout_object(this);
1100 this->deferred_layout_.reserve(num_sections_to_defer);
1103 // Whether we've seen a .note.GNU-stack section.
1104 bool seen_gnu_stack = false;
1105 // The flags of a .note.GNU-stack section.
1106 uint64_t gnu_stack_flags = 0;
1108 // Keep track of which sections to omit.
1109 std::vector<bool> omit(shnum, false);
1111 // Keep track of reloc sections when emitting relocations.
1112 const bool relocatable = parameters->options().relocatable();
1113 const bool emit_relocs = (relocatable
1114 || parameters->options().emit_relocs());
1115 std::vector<unsigned int> reloc_sections;
1117 // Keep track of .eh_frame sections.
1118 std::vector<unsigned int> eh_frame_sections;
1120 // Skip the first, dummy, section.
1121 pshdrs = shdrs + This::shdr_size;
1122 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1124 typename This::Shdr shdr(pshdrs);
1126 if (shdr.get_sh_name() >= section_names_size)
1128 this->error(_("bad section name offset for section %u: %lu"),
1129 i, static_cast<unsigned long>(shdr.get_sh_name()));
1130 return;
1133 const char* name = pnames + shdr.get_sh_name();
1135 if (!is_gc_pass_two)
1137 if (this->handle_gnu_warning_section(name, i, symtab))
1139 if (!relocatable)
1140 omit[i] = true;
1143 // The .note.GNU-stack section is special. It gives the
1144 // protection flags that this object file requires for the stack
1145 // in memory.
1146 if (strcmp(name, ".note.GNU-stack") == 0)
1148 seen_gnu_stack = true;
1149 gnu_stack_flags |= shdr.get_sh_flags();
1150 omit[i] = true;
1153 // The .note.GNU-split-stack section is also special. It
1154 // indicates that the object was compiled with
1155 // -fsplit-stack.
1156 if (this->handle_split_stack_section(name))
1158 if (!parameters->options().relocatable()
1159 && !parameters->options().shared())
1160 omit[i] = true;
1163 // Skip attributes section.
1164 if (parameters->target().is_attributes_section(name))
1166 omit[i] = true;
1169 bool discard = omit[i];
1170 if (!discard)
1172 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
1174 if (!this->include_section_group(symtab, layout, i, name,
1175 shdrs, pnames,
1176 section_names_size,
1177 &omit))
1178 discard = true;
1180 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
1181 && Layout::is_linkonce(name))
1183 if (!this->include_linkonce_section(layout, i, name, shdr))
1184 discard = true;
1188 if (discard)
1190 // Do not include this section in the link.
1191 out_sections[i] = NULL;
1192 out_section_offsets[i] = invalid_address;
1193 continue;
1197 if (is_gc_pass_one && parameters->options().gc_sections())
1199 if (is_section_name_included(name)
1200 || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1201 || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
1203 symtab->gc()->worklist().push(Section_id(this, i));
1205 // If the section name XXX can be represented as a C identifier
1206 // it cannot be discarded if there are references to
1207 // __start_XXX and __stop_XXX symbols. These need to be
1208 // specially handled.
1209 if (is_cident(name))
1211 symtab->gc()->add_cident_section(name, Section_id(this, i));
1215 // When doing a relocatable link we are going to copy input
1216 // reloc sections into the output. We only want to copy the
1217 // ones associated with sections which are not being discarded.
1218 // However, we don't know that yet for all sections. So save
1219 // reloc sections and process them later. Garbage collection is
1220 // not triggered when relocatable code is desired.
1221 if (emit_relocs
1222 && (shdr.get_sh_type() == elfcpp::SHT_REL
1223 || shdr.get_sh_type() == elfcpp::SHT_RELA))
1225 reloc_sections.push_back(i);
1226 continue;
1229 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
1230 continue;
1232 // The .eh_frame section is special. It holds exception frame
1233 // information that we need to read in order to generate the
1234 // exception frame header. We process these after all the other
1235 // sections so that the exception frame reader can reliably
1236 // determine which sections are being discarded, and discard the
1237 // corresponding information.
1238 if (!relocatable
1239 && strcmp(name, ".eh_frame") == 0
1240 && this->check_eh_frame_flags(&shdr))
1242 if (is_gc_pass_one)
1244 out_sections[i] = reinterpret_cast<Output_section*>(1);
1245 out_section_offsets[i] = invalid_address;
1247 else
1248 eh_frame_sections.push_back(i);
1249 continue;
1252 if (is_gc_pass_two && parameters->options().gc_sections())
1254 // This is executed during the second pass of garbage
1255 // collection. do_layout has been called before and some
1256 // sections have been already discarded. Simply ignore
1257 // such sections this time around.
1258 if (out_sections[i] == NULL)
1260 gold_assert(out_section_offsets[i] == invalid_address);
1261 continue;
1263 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1264 && symtab->gc()->is_section_garbage(this, i))
1266 if (parameters->options().print_gc_sections())
1267 gold_info(_("%s: removing unused section from '%s'"
1268 " in file '%s'"),
1269 program_name, this->section_name(i).c_str(),
1270 this->name().c_str());
1271 out_sections[i] = NULL;
1272 out_section_offsets[i] = invalid_address;
1273 continue;
1277 if (is_gc_pass_two && parameters->options().icf_enabled())
1279 if (out_sections[i] == NULL)
1281 gold_assert(out_section_offsets[i] == invalid_address);
1282 continue;
1284 if (((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1285 && symtab->icf()->is_section_folded(this, i))
1287 if (parameters->options().print_icf_sections())
1289 Section_id folded =
1290 symtab->icf()->get_folded_section(this, i);
1291 Relobj* folded_obj =
1292 reinterpret_cast<Relobj*>(folded.first);
1293 gold_info(_("%s: ICF folding section '%s' in file '%s'"
1294 "into '%s' in file '%s'"),
1295 program_name, this->section_name(i).c_str(),
1296 this->name().c_str(),
1297 folded_obj->section_name(folded.second).c_str(),
1298 folded_obj->name().c_str());
1300 out_sections[i] = NULL;
1301 out_section_offsets[i] = invalid_address;
1302 continue;
1306 // Defer layout here if input files are claimed by plugins. When gc
1307 // is turned on this function is called twice. For the second call
1308 // should_defer_layout should be false.
1309 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1311 gold_assert(!is_gc_pass_two);
1312 this->deferred_layout_.push_back(Deferred_layout(i, name,
1313 pshdrs,
1314 reloc_shndx[i],
1315 reloc_type[i]));
1316 // Put dummy values here; real values will be supplied by
1317 // do_layout_deferred_sections.
1318 out_sections[i] = reinterpret_cast<Output_section*>(2);
1319 out_section_offsets[i] = invalid_address;
1320 continue;
1323 // During gc_pass_two if a section that was previously deferred is
1324 // found, do not layout the section as layout_deferred_sections will
1325 // do it later from gold.cc.
1326 if (is_gc_pass_two
1327 && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
1328 continue;
1330 if (is_gc_pass_one)
1332 // This is during garbage collection. The out_sections are
1333 // assigned in the second call to this function.
1334 out_sections[i] = reinterpret_cast<Output_section*>(1);
1335 out_section_offsets[i] = invalid_address;
1337 else
1339 // When garbage collection is switched on the actual layout
1340 // only happens in the second call.
1341 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1342 reloc_type[i]);
1346 if (!is_gc_pass_two)
1347 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1349 // When doing a relocatable link handle the reloc sections at the
1350 // end. Garbage collection and Identical Code Folding is not
1351 // turned on for relocatable code.
1352 if (emit_relocs)
1353 this->size_relocatable_relocs();
1355 gold_assert(!(is_gc_or_icf) || reloc_sections.empty());
1357 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1358 p != reloc_sections.end();
1359 ++p)
1361 unsigned int i = *p;
1362 const unsigned char* pshdr;
1363 pshdr = section_headers_data + i * This::shdr_size;
1364 typename This::Shdr shdr(pshdr);
1366 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1367 if (data_shndx >= shnum)
1369 // We already warned about this above.
1370 continue;
1373 Output_section* data_section = out_sections[data_shndx];
1374 if (data_section == reinterpret_cast<Output_section*>(2))
1376 // The layout for the data section was deferred, so we need
1377 // to defer the relocation section, too.
1378 const char* name = pnames + shdr.get_sh_name();
1379 this->deferred_layout_relocs_.push_back(
1380 Deferred_layout(i, name, pshdr, 0, elfcpp::SHT_NULL));
1381 out_sections[i] = reinterpret_cast<Output_section*>(2);
1382 out_section_offsets[i] = invalid_address;
1383 continue;
1385 if (data_section == NULL)
1387 out_sections[i] = NULL;
1388 out_section_offsets[i] = invalid_address;
1389 continue;
1392 Relocatable_relocs* rr = new Relocatable_relocs();
1393 this->set_relocatable_relocs(i, rr);
1395 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1396 rr);
1397 out_sections[i] = os;
1398 out_section_offsets[i] = invalid_address;
1401 // Handle the .eh_frame sections at the end.
1402 gold_assert(!is_gc_pass_one || eh_frame_sections.empty());
1403 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1404 p != eh_frame_sections.end();
1405 ++p)
1407 gold_assert(this->has_eh_frame_);
1408 gold_assert(external_symbols_offset != 0);
1410 unsigned int i = *p;
1411 const unsigned char *pshdr;
1412 pshdr = section_headers_data + i * This::shdr_size;
1413 typename This::Shdr shdr(pshdr);
1415 off_t offset;
1416 Output_section* os = layout->layout_eh_frame(this,
1417 symbols_data,
1418 symbols_size,
1419 symbol_names_data,
1420 symbol_names_size,
1421 i, shdr,
1422 reloc_shndx[i],
1423 reloc_type[i],
1424 &offset);
1425 out_sections[i] = os;
1426 if (os == NULL || offset == -1)
1428 // An object can contain at most one section holding exception
1429 // frame information.
1430 gold_assert(this->discarded_eh_frame_shndx_ == -1U);
1431 this->discarded_eh_frame_shndx_ = i;
1432 out_section_offsets[i] = invalid_address;
1434 else
1435 out_section_offsets[i] = convert_types<Address, off_t>(offset);
1437 // If this section requires special handling, and if there are
1438 // relocs that apply to it, then we must do the special handling
1439 // before we apply the relocs.
1440 if (os != NULL && offset == -1 && reloc_shndx[i] != 0)
1441 this->set_relocs_must_follow_section_writes();
1444 if (is_gc_pass_two)
1446 delete[] gc_sd->section_headers_data;
1447 delete[] gc_sd->section_names_data;
1448 delete[] gc_sd->symbols_data;
1449 delete[] gc_sd->symbol_names_data;
1450 this->set_symbols_data(NULL);
1452 else
1454 delete sd->section_headers;
1455 sd->section_headers = NULL;
1456 delete sd->section_names;
1457 sd->section_names = NULL;
1461 // Layout sections whose layout was deferred while waiting for
1462 // input files from a plugin.
1464 template<int size, bool big_endian>
1465 void
1466 Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1468 typename std::vector<Deferred_layout>::iterator deferred;
1470 for (deferred = this->deferred_layout_.begin();
1471 deferred != this->deferred_layout_.end();
1472 ++deferred)
1474 typename This::Shdr shdr(deferred->shdr_data_);
1475 // If the section is not included, it is because the garbage collector
1476 // decided it is not needed. Avoid reverting that decision.
1477 if (!this->is_section_included(deferred->shndx_))
1478 continue;
1480 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1481 shdr, deferred->reloc_shndx_, deferred->reloc_type_);
1484 this->deferred_layout_.clear();
1486 // Now handle the deferred relocation sections.
1488 Output_sections& out_sections(this->output_sections());
1489 std::vector<Address>& out_section_offsets(this->section_offsets_);
1491 for (deferred = this->deferred_layout_relocs_.begin();
1492 deferred != this->deferred_layout_relocs_.end();
1493 ++deferred)
1495 unsigned int shndx = deferred->shndx_;
1496 typename This::Shdr shdr(deferred->shdr_data_);
1497 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1499 Output_section* data_section = out_sections[data_shndx];
1500 if (data_section == NULL)
1502 out_sections[shndx] = NULL;
1503 out_section_offsets[shndx] = invalid_address;
1504 continue;
1507 Relocatable_relocs* rr = new Relocatable_relocs();
1508 this->set_relocatable_relocs(shndx, rr);
1510 Output_section* os = layout->layout_reloc(this, shndx, shdr,
1511 data_section, rr);
1512 out_sections[shndx] = os;
1513 out_section_offsets[shndx] = invalid_address;
1517 // Add the symbols to the symbol table.
1519 template<int size, bool big_endian>
1520 void
1521 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1522 Read_symbols_data* sd,
1523 Layout*)
1525 if (sd->symbols == NULL)
1527 gold_assert(sd->symbol_names == NULL);
1528 return;
1531 const int sym_size = This::sym_size;
1532 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1533 / sym_size);
1534 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1536 this->error(_("size of symbols is not multiple of symbol size"));
1537 return;
1540 this->symbols_.resize(symcount);
1542 const char* sym_names =
1543 reinterpret_cast<const char*>(sd->symbol_names->data());
1544 symtab->add_from_relobj(this,
1545 sd->symbols->data() + sd->external_symbols_offset,
1546 symcount, this->local_symbol_count_,
1547 sym_names, sd->symbol_names_size,
1548 &this->symbols_,
1549 &this->defined_count_);
1551 delete sd->symbols;
1552 sd->symbols = NULL;
1553 delete sd->symbol_names;
1554 sd->symbol_names = NULL;
1557 // Find out if this object, that is a member of a lib group, should be included
1558 // in the link. We check every symbol defined by this object. If the symbol
1559 // table has a strong undefined reference to that symbol, we have to include
1560 // the object.
1562 template<int size, bool big_endian>
1563 Archive::Should_include
1564 Sized_relobj<size, big_endian>::do_should_include_member(Symbol_table* symtab,
1565 Read_symbols_data* sd,
1566 std::string* why)
1568 char* tmpbuf = NULL;
1569 size_t tmpbuflen = 0;
1570 const char* sym_names =
1571 reinterpret_cast<const char*>(sd->symbol_names->data());
1572 const unsigned char* syms =
1573 sd->symbols->data() + sd->external_symbols_offset;
1574 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1575 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1576 / sym_size);
1578 const unsigned char* p = syms;
1580 for (size_t i = 0; i < symcount; ++i, p += sym_size)
1582 elfcpp::Sym<size, big_endian> sym(p);
1583 unsigned int st_shndx = sym.get_st_shndx();
1584 if (st_shndx == elfcpp::SHN_UNDEF)
1585 continue;
1587 unsigned int st_name = sym.get_st_name();
1588 const char* name = sym_names + st_name;
1589 Symbol* symbol;
1590 Archive::Should_include t = Archive::should_include_member(symtab, name,
1591 &symbol, why,
1592 &tmpbuf,
1593 &tmpbuflen);
1594 if (t == Archive::SHOULD_INCLUDE_YES)
1596 if (tmpbuf != NULL)
1597 free(tmpbuf);
1598 return t;
1601 if (tmpbuf != NULL)
1602 free(tmpbuf);
1603 return Archive::SHOULD_INCLUDE_UNKNOWN;
1606 // First pass over the local symbols. Here we add their names to
1607 // *POOL and *DYNPOOL, and we store the symbol value in
1608 // THIS->LOCAL_VALUES_. This function is always called from a
1609 // singleton thread. This is followed by a call to
1610 // finalize_local_symbols.
1612 template<int size, bool big_endian>
1613 void
1614 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1615 Stringpool* dynpool)
1617 gold_assert(this->symtab_shndx_ != -1U);
1618 if (this->symtab_shndx_ == 0)
1620 // This object has no symbols. Weird but legal.
1621 return;
1624 // Read the symbol table section header.
1625 const unsigned int symtab_shndx = this->symtab_shndx_;
1626 typename This::Shdr symtabshdr(this,
1627 this->elf_file_.section_header(symtab_shndx));
1628 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1630 // Read the local symbols.
1631 const int sym_size = This::sym_size;
1632 const unsigned int loccount = this->local_symbol_count_;
1633 gold_assert(loccount == symtabshdr.get_sh_info());
1634 off_t locsize = loccount * sym_size;
1635 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1636 locsize, true, true);
1638 // Read the symbol names.
1639 const unsigned int strtab_shndx =
1640 this->adjust_shndx(symtabshdr.get_sh_link());
1641 section_size_type strtab_size;
1642 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1643 &strtab_size,
1644 true);
1645 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1647 // Loop over the local symbols.
1649 const Output_sections& out_sections(this->output_sections());
1650 unsigned int shnum = this->shnum();
1651 unsigned int count = 0;
1652 unsigned int dyncount = 0;
1653 // Skip the first, dummy, symbol.
1654 psyms += sym_size;
1655 bool discard_all = parameters->options().discard_all();
1656 bool discard_locals = parameters->options().discard_locals();
1657 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1659 elfcpp::Sym<size, big_endian> sym(psyms);
1661 Symbol_value<size>& lv(this->local_values_[i]);
1663 bool is_ordinary;
1664 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1665 &is_ordinary);
1666 lv.set_input_shndx(shndx, is_ordinary);
1668 if (sym.get_st_type() == elfcpp::STT_SECTION)
1669 lv.set_is_section_symbol();
1670 else if (sym.get_st_type() == elfcpp::STT_TLS)
1671 lv.set_is_tls_symbol();
1673 // Save the input symbol value for use in do_finalize_local_symbols().
1674 lv.set_input_value(sym.get_st_value());
1676 // Decide whether this symbol should go into the output file.
1678 if ((shndx < shnum && out_sections[shndx] == NULL)
1679 || shndx == this->discarded_eh_frame_shndx_)
1681 lv.set_no_output_symtab_entry();
1682 gold_assert(!lv.needs_output_dynsym_entry());
1683 continue;
1686 if (sym.get_st_type() == elfcpp::STT_SECTION)
1688 lv.set_no_output_symtab_entry();
1689 gold_assert(!lv.needs_output_dynsym_entry());
1690 continue;
1693 if (sym.get_st_name() >= strtab_size)
1695 this->error(_("local symbol %u section name out of range: %u >= %u"),
1696 i, sym.get_st_name(),
1697 static_cast<unsigned int>(strtab_size));
1698 lv.set_no_output_symtab_entry();
1699 continue;
1702 const char* name = pnames + sym.get_st_name();
1704 // If needed, add the symbol to the dynamic symbol table string pool.
1705 if (lv.needs_output_dynsym_entry())
1707 dynpool->add(name, true, NULL);
1708 ++dyncount;
1711 if (discard_all && lv.may_be_discarded_from_output_symtab())
1713 lv.set_no_output_symtab_entry();
1714 continue;
1717 // If --discard-locals option is used, discard all temporary local
1718 // symbols. These symbols start with system-specific local label
1719 // prefixes, typically .L for ELF system. We want to be compatible
1720 // with GNU ld so here we essentially use the same check in
1721 // bfd_is_local_label(). The code is different because we already
1722 // know that:
1724 // - the symbol is local and thus cannot have global or weak binding.
1725 // - the symbol is not a section symbol.
1726 // - the symbol has a name.
1728 // We do not discard a symbol if it needs a dynamic symbol entry.
1729 if (discard_locals
1730 && sym.get_st_type() != elfcpp::STT_FILE
1731 && !lv.needs_output_dynsym_entry()
1732 && lv.may_be_discarded_from_output_symtab()
1733 && parameters->target().is_local_label_name(name))
1735 lv.set_no_output_symtab_entry();
1736 continue;
1739 // Discard the local symbol if -retain_symbols_file is specified
1740 // and the local symbol is not in that file.
1741 if (!parameters->options().should_retain_symbol(name))
1743 lv.set_no_output_symtab_entry();
1744 continue;
1747 // Add the symbol to the symbol table string pool.
1748 pool->add(name, true, NULL);
1749 ++count;
1752 this->output_local_symbol_count_ = count;
1753 this->output_local_dynsym_count_ = dyncount;
1756 // Finalize the local symbols. Here we set the final value in
1757 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1758 // This function is always called from a singleton thread. The actual
1759 // output of the local symbols will occur in a separate task.
1761 template<int size, bool big_endian>
1762 unsigned int
1763 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1764 off_t off,
1765 Symbol_table* symtab)
1767 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1769 const unsigned int loccount = this->local_symbol_count_;
1770 this->local_symbol_offset_ = off;
1772 const bool relocatable = parameters->options().relocatable();
1773 const Output_sections& out_sections(this->output_sections());
1774 const std::vector<Address>& out_offsets(this->section_offsets_);
1775 unsigned int shnum = this->shnum();
1777 for (unsigned int i = 1; i < loccount; ++i)
1779 Symbol_value<size>& lv(this->local_values_[i]);
1781 bool is_ordinary;
1782 unsigned int shndx = lv.input_shndx(&is_ordinary);
1784 // Set the output symbol value.
1786 if (!is_ordinary)
1788 if (shndx == elfcpp::SHN_ABS || Symbol::is_common_shndx(shndx))
1789 lv.set_output_value(lv.input_value());
1790 else
1792 this->error(_("unknown section index %u for local symbol %u"),
1793 shndx, i);
1794 lv.set_output_value(0);
1797 else
1799 if (shndx >= shnum)
1801 this->error(_("local symbol %u section index %u out of range"),
1802 i, shndx);
1803 shndx = 0;
1806 Output_section* os = out_sections[shndx];
1807 Address secoffset = out_offsets[shndx];
1808 if (symtab->is_section_folded(this, shndx))
1810 gold_assert (os == NULL && secoffset == invalid_address);
1811 // Get the os of the section it is folded onto.
1812 Section_id folded = symtab->icf()->get_folded_section(this,
1813 shndx);
1814 gold_assert(folded.first != NULL);
1815 Sized_relobj<size, big_endian>* folded_obj = reinterpret_cast
1816 <Sized_relobj<size, big_endian>*>(folded.first);
1817 os = folded_obj->output_section(folded.second);
1818 gold_assert(os != NULL);
1819 secoffset = folded_obj->get_output_section_offset(folded.second);
1821 // This could be a relaxed input section.
1822 if (secoffset == invalid_address)
1824 const Output_relaxed_input_section* relaxed_section =
1825 os->find_relaxed_input_section(folded_obj, folded.second);
1826 gold_assert(relaxed_section != NULL);
1827 secoffset = relaxed_section->address() - os->address();
1831 if (os == NULL)
1833 // This local symbol belongs to a section we are discarding.
1834 // In some cases when applying relocations later, we will
1835 // attempt to match it to the corresponding kept section,
1836 // so we leave the input value unchanged here.
1837 continue;
1839 else if (secoffset == invalid_address)
1841 uint64_t start;
1843 // This is a SHF_MERGE section or one which otherwise
1844 // requires special handling.
1845 if (shndx == this->discarded_eh_frame_shndx_)
1847 // This local symbol belongs to a discarded .eh_frame
1848 // section. Just treat it like the case in which
1849 // os == NULL above.
1850 gold_assert(this->has_eh_frame_);
1851 continue;
1853 else if (!lv.is_section_symbol())
1855 // This is not a section symbol. We can determine
1856 // the final value now.
1857 lv.set_output_value(os->output_address(this, shndx,
1858 lv.input_value()));
1860 else if (!os->find_starting_output_address(this, shndx, &start))
1862 // This is a section symbol, but apparently not one in a
1863 // merged section. First check to see if this is a relaxed
1864 // input section. If so, use its address. Otherwise just
1865 // use the start of the output section. This happens with
1866 // relocatable links when the input object has section
1867 // symbols for arbitrary non-merge sections.
1868 const Output_section_data* posd =
1869 os->find_relaxed_input_section(this, shndx);
1870 if (posd != NULL)
1872 Address relocatable_link_adjustment =
1873 relocatable ? os->address() : 0;
1874 lv.set_output_value(posd->address()
1875 - relocatable_link_adjustment);
1877 else
1878 lv.set_output_value(os->address());
1880 else
1882 // We have to consider the addend to determine the
1883 // value to use in a relocation. START is the start
1884 // of this input section. If we are doing a relocatable
1885 // link, use offset from start output section instead of
1886 // address.
1887 Address adjusted_start =
1888 relocatable ? start - os->address() : start;
1889 Merged_symbol_value<size>* msv =
1890 new Merged_symbol_value<size>(lv.input_value(),
1891 adjusted_start);
1892 lv.set_merged_symbol_value(msv);
1895 else if (lv.is_tls_symbol())
1896 lv.set_output_value(os->tls_offset()
1897 + secoffset
1898 + lv.input_value());
1899 else
1900 lv.set_output_value((relocatable ? 0 : os->address())
1901 + secoffset
1902 + lv.input_value());
1905 if (!lv.is_output_symtab_index_set())
1907 lv.set_output_symtab_index(index);
1908 ++index;
1911 return index;
1914 // Set the output dynamic symbol table indexes for the local variables.
1916 template<int size, bool big_endian>
1917 unsigned int
1918 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1920 const unsigned int loccount = this->local_symbol_count_;
1921 for (unsigned int i = 1; i < loccount; ++i)
1923 Symbol_value<size>& lv(this->local_values_[i]);
1924 if (lv.needs_output_dynsym_entry())
1926 lv.set_output_dynsym_index(index);
1927 ++index;
1930 return index;
1933 // Set the offset where local dynamic symbol information will be stored.
1934 // Returns the count of local symbols contributed to the symbol table by
1935 // this object.
1937 template<int size, bool big_endian>
1938 unsigned int
1939 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1941 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1942 this->local_dynsym_offset_ = off;
1943 return this->output_local_dynsym_count_;
1946 // If Symbols_data is not NULL get the section flags from here otherwise
1947 // get it from the file.
1949 template<int size, bool big_endian>
1950 uint64_t
1951 Sized_relobj<size, big_endian>::do_section_flags(unsigned int shndx)
1953 Symbols_data* sd = this->get_symbols_data();
1954 if (sd != NULL)
1956 const unsigned char* pshdrs = sd->section_headers_data
1957 + This::shdr_size * shndx;
1958 typename This::Shdr shdr(pshdrs);
1959 return shdr.get_sh_flags();
1961 // If sd is NULL, read the section header from the file.
1962 return this->elf_file_.section_flags(shndx);
1965 // Get the section's ent size from Symbols_data. Called by get_section_contents
1966 // in icf.cc
1968 template<int size, bool big_endian>
1969 uint64_t
1970 Sized_relobj<size, big_endian>::do_section_entsize(unsigned int shndx)
1972 Symbols_data* sd = this->get_symbols_data();
1973 gold_assert (sd != NULL);
1975 const unsigned char* pshdrs = sd->section_headers_data
1976 + This::shdr_size * shndx;
1977 typename This::Shdr shdr(pshdrs);
1978 return shdr.get_sh_entsize();
1982 // Write out the local symbols.
1984 template<int size, bool big_endian>
1985 void
1986 Sized_relobj<size, big_endian>::write_local_symbols(
1987 Output_file* of,
1988 const Stringpool* sympool,
1989 const Stringpool* dynpool,
1990 Output_symtab_xindex* symtab_xindex,
1991 Output_symtab_xindex* dynsym_xindex)
1993 const bool strip_all = parameters->options().strip_all();
1994 if (strip_all)
1996 if (this->output_local_dynsym_count_ == 0)
1997 return;
1998 this->output_local_symbol_count_ = 0;
2001 gold_assert(this->symtab_shndx_ != -1U);
2002 if (this->symtab_shndx_ == 0)
2004 // This object has no symbols. Weird but legal.
2005 return;
2008 // Read the symbol table section header.
2009 const unsigned int symtab_shndx = this->symtab_shndx_;
2010 typename This::Shdr symtabshdr(this,
2011 this->elf_file_.section_header(symtab_shndx));
2012 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2013 const unsigned int loccount = this->local_symbol_count_;
2014 gold_assert(loccount == symtabshdr.get_sh_info());
2016 // Read the local symbols.
2017 const int sym_size = This::sym_size;
2018 off_t locsize = loccount * sym_size;
2019 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
2020 locsize, true, false);
2022 // Read the symbol names.
2023 const unsigned int strtab_shndx =
2024 this->adjust_shndx(symtabshdr.get_sh_link());
2025 section_size_type strtab_size;
2026 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
2027 &strtab_size,
2028 false);
2029 const char* pnames = reinterpret_cast<const char*>(pnamesu);
2031 // Get views into the output file for the portions of the symbol table
2032 // and the dynamic symbol table that we will be writing.
2033 off_t output_size = this->output_local_symbol_count_ * sym_size;
2034 unsigned char* oview = NULL;
2035 if (output_size > 0)
2036 oview = of->get_output_view(this->local_symbol_offset_, output_size);
2038 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
2039 unsigned char* dyn_oview = NULL;
2040 if (dyn_output_size > 0)
2041 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
2042 dyn_output_size);
2044 const Output_sections out_sections(this->output_sections());
2046 gold_assert(this->local_values_.size() == loccount);
2048 unsigned char* ov = oview;
2049 unsigned char* dyn_ov = dyn_oview;
2050 psyms += sym_size;
2051 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2053 elfcpp::Sym<size, big_endian> isym(psyms);
2055 Symbol_value<size>& lv(this->local_values_[i]);
2057 bool is_ordinary;
2058 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
2059 &is_ordinary);
2060 if (is_ordinary)
2062 gold_assert(st_shndx < out_sections.size());
2063 if (out_sections[st_shndx] == NULL)
2064 continue;
2065 st_shndx = out_sections[st_shndx]->out_shndx();
2066 if (st_shndx >= elfcpp::SHN_LORESERVE)
2068 if (lv.has_output_symtab_entry())
2069 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
2070 if (lv.has_output_dynsym_entry())
2071 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
2072 st_shndx = elfcpp::SHN_XINDEX;
2076 // Write the symbol to the output symbol table.
2077 if (lv.has_output_symtab_entry())
2079 elfcpp::Sym_write<size, big_endian> osym(ov);
2081 gold_assert(isym.get_st_name() < strtab_size);
2082 const char* name = pnames + isym.get_st_name();
2083 osym.put_st_name(sympool->get_offset(name));
2084 osym.put_st_value(this->local_values_[i].value(this, 0));
2085 osym.put_st_size(isym.get_st_size());
2086 osym.put_st_info(isym.get_st_info());
2087 osym.put_st_other(isym.get_st_other());
2088 osym.put_st_shndx(st_shndx);
2090 ov += sym_size;
2093 // Write the symbol to the output dynamic symbol table.
2094 if (lv.has_output_dynsym_entry())
2096 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
2097 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
2099 gold_assert(isym.get_st_name() < strtab_size);
2100 const char* name = pnames + isym.get_st_name();
2101 osym.put_st_name(dynpool->get_offset(name));
2102 osym.put_st_value(this->local_values_[i].value(this, 0));
2103 osym.put_st_size(isym.get_st_size());
2104 osym.put_st_info(isym.get_st_info());
2105 osym.put_st_other(isym.get_st_other());
2106 osym.put_st_shndx(st_shndx);
2108 dyn_ov += sym_size;
2113 if (output_size > 0)
2115 gold_assert(ov - oview == output_size);
2116 of->write_output_view(this->local_symbol_offset_, output_size, oview);
2119 if (dyn_output_size > 0)
2121 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
2122 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
2123 dyn_oview);
2127 // Set *INFO to symbolic information about the offset OFFSET in the
2128 // section SHNDX. Return true if we found something, false if we
2129 // found nothing.
2131 template<int size, bool big_endian>
2132 bool
2133 Sized_relobj<size, big_endian>::get_symbol_location_info(
2134 unsigned int shndx,
2135 off_t offset,
2136 Symbol_location_info* info)
2138 if (this->symtab_shndx_ == 0)
2139 return false;
2141 section_size_type symbols_size;
2142 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
2143 &symbols_size,
2144 false);
2146 unsigned int symbol_names_shndx =
2147 this->adjust_shndx(this->section_link(this->symtab_shndx_));
2148 section_size_type names_size;
2149 const unsigned char* symbol_names_u =
2150 this->section_contents(symbol_names_shndx, &names_size, false);
2151 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
2153 const int sym_size = This::sym_size;
2154 const size_t count = symbols_size / sym_size;
2156 const unsigned char* p = symbols;
2157 for (size_t i = 0; i < count; ++i, p += sym_size)
2159 elfcpp::Sym<size, big_endian> sym(p);
2161 if (sym.get_st_type() == elfcpp::STT_FILE)
2163 if (sym.get_st_name() >= names_size)
2164 info->source_file = "(invalid)";
2165 else
2166 info->source_file = symbol_names + sym.get_st_name();
2167 continue;
2170 bool is_ordinary;
2171 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
2172 &is_ordinary);
2173 if (is_ordinary
2174 && st_shndx == shndx
2175 && static_cast<off_t>(sym.get_st_value()) <= offset
2176 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
2177 > offset))
2179 if (sym.get_st_name() > names_size)
2180 info->enclosing_symbol_name = "(invalid)";
2181 else
2183 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
2184 if (parameters->options().do_demangle())
2186 char* demangled_name = cplus_demangle(
2187 info->enclosing_symbol_name.c_str(),
2188 DMGL_ANSI | DMGL_PARAMS);
2189 if (demangled_name != NULL)
2191 info->enclosing_symbol_name.assign(demangled_name);
2192 free(demangled_name);
2196 return true;
2200 return false;
2203 // Look for a kept section corresponding to the given discarded section,
2204 // and return its output address. This is used only for relocations in
2205 // debugging sections. If we can't find the kept section, return 0.
2207 template<int size, bool big_endian>
2208 typename Sized_relobj<size, big_endian>::Address
2209 Sized_relobj<size, big_endian>::map_to_kept_section(
2210 unsigned int shndx,
2211 bool* found) const
2213 Relobj* kept_object;
2214 unsigned int kept_shndx;
2215 if (this->get_kept_comdat_section(shndx, &kept_object, &kept_shndx))
2217 Sized_relobj<size, big_endian>* kept_relobj =
2218 static_cast<Sized_relobj<size, big_endian>*>(kept_object);
2219 Output_section* os = kept_relobj->output_section(kept_shndx);
2220 Address offset = kept_relobj->get_output_section_offset(kept_shndx);
2221 if (os != NULL && offset != invalid_address)
2223 *found = true;
2224 return os->address() + offset;
2227 *found = false;
2228 return 0;
2231 // Get symbol counts.
2233 template<int size, bool big_endian>
2234 void
2235 Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
2236 const Symbol_table*,
2237 size_t* defined,
2238 size_t* used) const
2240 *defined = this->defined_count_;
2241 size_t count = 0;
2242 for (Symbols::const_iterator p = this->symbols_.begin();
2243 p != this->symbols_.end();
2244 ++p)
2245 if (*p != NULL
2246 && (*p)->source() == Symbol::FROM_OBJECT
2247 && (*p)->object() == this
2248 && (*p)->is_defined())
2249 ++count;
2250 *used = count;
2253 // Input_objects methods.
2255 // Add a regular relocatable object to the list. Return false if this
2256 // object should be ignored.
2258 bool
2259 Input_objects::add_object(Object* obj)
2261 // Print the filename if the -t/--trace option is selected.
2262 if (parameters->options().trace())
2263 gold_info("%s", obj->name().c_str());
2265 if (!obj->is_dynamic())
2266 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
2267 else
2269 // See if this is a duplicate SONAME.
2270 Dynobj* dynobj = static_cast<Dynobj*>(obj);
2271 const char* soname = dynobj->soname();
2273 std::pair<Unordered_set<std::string>::iterator, bool> ins =
2274 this->sonames_.insert(soname);
2275 if (!ins.second)
2277 // We have already seen a dynamic object with this soname.
2278 return false;
2281 this->dynobj_list_.push_back(dynobj);
2284 // Add this object to the cross-referencer if requested.
2285 if (parameters->options().user_set_print_symbol_counts()
2286 || parameters->options().cref())
2288 if (this->cref_ == NULL)
2289 this->cref_ = new Cref();
2290 this->cref_->add_object(obj);
2293 return true;
2296 // For each dynamic object, record whether we've seen all of its
2297 // explicit dependencies.
2299 void
2300 Input_objects::check_dynamic_dependencies() const
2302 bool issued_copy_dt_needed_error = false;
2303 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
2304 p != this->dynobj_list_.end();
2305 ++p)
2307 const Dynobj::Needed& needed((*p)->needed());
2308 bool found_all = true;
2309 Dynobj::Needed::const_iterator pneeded;
2310 for (pneeded = needed.begin(); pneeded != needed.end(); ++pneeded)
2312 if (this->sonames_.find(*pneeded) == this->sonames_.end())
2314 found_all = false;
2315 break;
2318 (*p)->set_has_unknown_needed_entries(!found_all);
2320 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2321 // that gold does not support. However, they cause no trouble
2322 // unless there is a DT_NEEDED entry that we don't know about;
2323 // warn only in that case.
2324 if (!found_all
2325 && !issued_copy_dt_needed_error
2326 && (parameters->options().copy_dt_needed_entries()
2327 || parameters->options().add_needed()))
2329 const char* optname;
2330 if (parameters->options().copy_dt_needed_entries())
2331 optname = "--copy-dt-needed-entries";
2332 else
2333 optname = "--add-needed";
2334 gold_error(_("%s is not supported but is required for %s in %s"),
2335 optname, (*pneeded).c_str(), (*p)->name().c_str());
2336 issued_copy_dt_needed_error = true;
2341 // Start processing an archive.
2343 void
2344 Input_objects::archive_start(Archive* archive)
2346 if (parameters->options().user_set_print_symbol_counts()
2347 || parameters->options().cref())
2349 if (this->cref_ == NULL)
2350 this->cref_ = new Cref();
2351 this->cref_->add_archive_start(archive);
2355 // Stop processing an archive.
2357 void
2358 Input_objects::archive_stop(Archive* archive)
2360 if (parameters->options().user_set_print_symbol_counts()
2361 || parameters->options().cref())
2362 this->cref_->add_archive_stop(archive);
2365 // Print symbol counts
2367 void
2368 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
2370 if (parameters->options().user_set_print_symbol_counts()
2371 && this->cref_ != NULL)
2372 this->cref_->print_symbol_counts(symtab);
2375 // Print a cross reference table.
2377 void
2378 Input_objects::print_cref(const Symbol_table* symtab, FILE* f) const
2380 if (parameters->options().cref() && this->cref_ != NULL)
2381 this->cref_->print_cref(symtab, f);
2384 // Relocate_info methods.
2386 // Return a string describing the location of a relocation. This is
2387 // only used in error messages.
2389 template<int size, bool big_endian>
2390 std::string
2391 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
2393 // See if we can get line-number information from debugging sections.
2394 std::string filename;
2395 std::string file_and_lineno; // Better than filename-only, if available.
2397 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
2398 // This will be "" if we failed to parse the debug info for any reason.
2399 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
2401 std::string ret(this->object->name());
2402 ret += ':';
2403 Symbol_location_info info;
2404 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
2406 ret += " in function ";
2407 ret += info.enclosing_symbol_name;
2408 ret += ":";
2409 filename = info.source_file;
2412 if (!file_and_lineno.empty())
2413 ret += file_and_lineno;
2414 else
2416 if (!filename.empty())
2417 ret += filename;
2418 ret += "(";
2419 ret += this->object->section_name(this->data_shndx);
2420 char buf[100];
2421 // Offsets into sections have to be positive.
2422 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
2423 ret += buf;
2424 ret += ")";
2426 return ret;
2429 } // End namespace gold.
2431 namespace
2434 using namespace gold;
2436 // Read an ELF file with the header and return the appropriate
2437 // instance of Object.
2439 template<int size, bool big_endian>
2440 Object*
2441 make_elf_sized_object(const std::string& name, Input_file* input_file,
2442 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr,
2443 bool* punconfigured)
2445 Target* target = select_target(ehdr.get_e_machine(), size, big_endian,
2446 ehdr.get_e_ident()[elfcpp::EI_OSABI],
2447 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
2448 if (target == NULL)
2449 gold_fatal(_("%s: unsupported ELF machine number %d"),
2450 name.c_str(), ehdr.get_e_machine());
2452 if (!parameters->target_valid())
2453 set_parameters_target(target);
2454 else if (target != &parameters->target())
2456 if (punconfigured != NULL)
2457 *punconfigured = true;
2458 else
2459 gold_error(_("%s: incompatible target"), name.c_str());
2460 return NULL;
2463 return target->make_elf_object<size, big_endian>(name, input_file, offset,
2464 ehdr);
2467 } // End anonymous namespace.
2469 namespace gold
2472 // Return whether INPUT_FILE is an ELF object.
2474 bool
2475 is_elf_object(Input_file* input_file, off_t offset,
2476 const unsigned char** start, int *read_size)
2478 off_t filesize = input_file->file().filesize();
2479 int want = elfcpp::Elf_recognizer::max_header_size;
2480 if (filesize - offset < want)
2481 want = filesize - offset;
2483 const unsigned char* p = input_file->file().get_view(offset, 0, want,
2484 true, false);
2485 *start = p;
2486 *read_size = want;
2488 return elfcpp::Elf_recognizer::is_elf_file(p, want);
2491 // Read an ELF file and return the appropriate instance of Object.
2493 Object*
2494 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
2495 const unsigned char* p, section_offset_type bytes,
2496 bool* punconfigured)
2498 if (punconfigured != NULL)
2499 *punconfigured = false;
2501 std::string error;
2502 bool big_endian = false;
2503 int size = 0;
2504 if (!elfcpp::Elf_recognizer::is_valid_header(p, bytes, &size,
2505 &big_endian, &error))
2507 gold_error(_("%s: %s"), name.c_str(), error.c_str());
2508 return NULL;
2511 if (size == 32)
2513 if (big_endian)
2515 #ifdef HAVE_TARGET_32_BIG
2516 elfcpp::Ehdr<32, true> ehdr(p);
2517 return make_elf_sized_object<32, true>(name, input_file,
2518 offset, ehdr, punconfigured);
2519 #else
2520 if (punconfigured != NULL)
2521 *punconfigured = true;
2522 else
2523 gold_error(_("%s: not configured to support "
2524 "32-bit big-endian object"),
2525 name.c_str());
2526 return NULL;
2527 #endif
2529 else
2531 #ifdef HAVE_TARGET_32_LITTLE
2532 elfcpp::Ehdr<32, false> ehdr(p);
2533 return make_elf_sized_object<32, false>(name, input_file,
2534 offset, ehdr, punconfigured);
2535 #else
2536 if (punconfigured != NULL)
2537 *punconfigured = true;
2538 else
2539 gold_error(_("%s: not configured to support "
2540 "32-bit little-endian object"),
2541 name.c_str());
2542 return NULL;
2543 #endif
2546 else if (size == 64)
2548 if (big_endian)
2550 #ifdef HAVE_TARGET_64_BIG
2551 elfcpp::Ehdr<64, true> ehdr(p);
2552 return make_elf_sized_object<64, true>(name, input_file,
2553 offset, ehdr, punconfigured);
2554 #else
2555 if (punconfigured != NULL)
2556 *punconfigured = true;
2557 else
2558 gold_error(_("%s: not configured to support "
2559 "64-bit big-endian object"),
2560 name.c_str());
2561 return NULL;
2562 #endif
2564 else
2566 #ifdef HAVE_TARGET_64_LITTLE
2567 elfcpp::Ehdr<64, false> ehdr(p);
2568 return make_elf_sized_object<64, false>(name, input_file,
2569 offset, ehdr, punconfigured);
2570 #else
2571 if (punconfigured != NULL)
2572 *punconfigured = true;
2573 else
2574 gold_error(_("%s: not configured to support "
2575 "64-bit little-endian object"),
2576 name.c_str());
2577 return NULL;
2578 #endif
2581 else
2582 gold_unreachable();
2585 // Instantiate the templates we need.
2587 #ifdef HAVE_TARGET_32_LITTLE
2588 template
2589 void
2590 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2591 Read_symbols_data*);
2592 #endif
2594 #ifdef HAVE_TARGET_32_BIG
2595 template
2596 void
2597 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2598 Read_symbols_data*);
2599 #endif
2601 #ifdef HAVE_TARGET_64_LITTLE
2602 template
2603 void
2604 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2605 Read_symbols_data*);
2606 #endif
2608 #ifdef HAVE_TARGET_64_BIG
2609 template
2610 void
2611 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2612 Read_symbols_data*);
2613 #endif
2615 #ifdef HAVE_TARGET_32_LITTLE
2616 template
2617 class Sized_relobj<32, false>;
2618 #endif
2620 #ifdef HAVE_TARGET_32_BIG
2621 template
2622 class Sized_relobj<32, true>;
2623 #endif
2625 #ifdef HAVE_TARGET_64_LITTLE
2626 template
2627 class Sized_relobj<64, false>;
2628 #endif
2630 #ifdef HAVE_TARGET_64_BIG
2631 template
2632 class Sized_relobj<64, true>;
2633 #endif
2635 #ifdef HAVE_TARGET_32_LITTLE
2636 template
2637 struct Relocate_info<32, false>;
2638 #endif
2640 #ifdef HAVE_TARGET_32_BIG
2641 template
2642 struct Relocate_info<32, true>;
2643 #endif
2645 #ifdef HAVE_TARGET_64_LITTLE
2646 template
2647 struct Relocate_info<64, false>;
2648 #endif
2650 #ifdef HAVE_TARGET_64_BIG
2651 template
2652 struct Relocate_info<64, true>;
2653 #endif
2655 #ifdef HAVE_TARGET_32_LITTLE
2656 template
2657 void
2658 Xindex::initialize_symtab_xindex<32, false>(Object*, unsigned int);
2660 template
2661 void
2662 Xindex::read_symtab_xindex<32, false>(Object*, unsigned int,
2663 const unsigned char*);
2664 #endif
2666 #ifdef HAVE_TARGET_32_BIG
2667 template
2668 void
2669 Xindex::initialize_symtab_xindex<32, true>(Object*, unsigned int);
2671 template
2672 void
2673 Xindex::read_symtab_xindex<32, true>(Object*, unsigned int,
2674 const unsigned char*);
2675 #endif
2677 #ifdef HAVE_TARGET_64_LITTLE
2678 template
2679 void
2680 Xindex::initialize_symtab_xindex<64, false>(Object*, unsigned int);
2682 template
2683 void
2684 Xindex::read_symtab_xindex<64, false>(Object*, unsigned int,
2685 const unsigned char*);
2686 #endif
2688 #ifdef HAVE_TARGET_64_BIG
2689 template
2690 void
2691 Xindex::initialize_symtab_xindex<64, true>(Object*, unsigned int);
2693 template
2694 void
2695 Xindex::read_symtab_xindex<64, true>(Object*, unsigned int,
2696 const unsigned char*);
2697 #endif
2699 } // End namespace gold.