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.
29 #include "libiberty.h"
32 #include "target-select.h"
33 #include "dwarf_reader.h"
48 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
49 // section and read it in. SYMTAB_SHNDX is the index of the symbol
50 // table we care about.
52 template<int size
, bool big_endian
>
54 Xindex::initialize_symtab_xindex(Object
* object
, unsigned int symtab_shndx
)
56 if (!this->symtab_xindex_
.empty())
59 gold_assert(symtab_shndx
!= 0);
61 // Look through the sections in reverse order, on the theory that it
62 // is more likely to be near the end than the beginning.
63 unsigned int i
= object
->shnum();
67 if (object
->section_type(i
) == elfcpp::SHT_SYMTAB_SHNDX
68 && this->adjust_shndx(object
->section_link(i
)) == symtab_shndx
)
70 this->read_symtab_xindex
<size
, big_endian
>(object
, i
, NULL
);
75 object
->error(_("missing SHT_SYMTAB_SHNDX section"));
78 // Read in the symtab_xindex_ array, given the section index of the
79 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
82 template<int size
, bool big_endian
>
84 Xindex::read_symtab_xindex(Object
* object
, unsigned int xindex_shndx
,
85 const unsigned char* pshdrs
)
87 section_size_type bytecount
;
88 const unsigned char* contents
;
90 contents
= object
->section_contents(xindex_shndx
, &bytecount
, false);
93 const unsigned char* p
= (pshdrs
95 * elfcpp::Elf_sizes
<size
>::shdr_size
));
96 typename
elfcpp::Shdr
<size
, big_endian
> shdr(p
);
97 bytecount
= convert_to_section_size_type(shdr
.get_sh_size());
98 contents
= object
->get_view(shdr
.get_sh_offset(), bytecount
, true, false);
101 gold_assert(this->symtab_xindex_
.empty());
102 this->symtab_xindex_
.reserve(bytecount
/ 4);
103 for (section_size_type i
= 0; i
< bytecount
; i
+= 4)
105 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
106 // We preadjust the section indexes we save.
107 this->symtab_xindex_
.push_back(this->adjust_shndx(shndx
));
111 // Symbol symndx has a section of SHN_XINDEX; return the real section
115 Xindex::sym_xindex_to_shndx(Object
* object
, unsigned int symndx
)
117 if (symndx
>= this->symtab_xindex_
.size())
119 object
->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
121 return elfcpp::SHN_UNDEF
;
123 unsigned int shndx
= this->symtab_xindex_
[symndx
];
124 if (shndx
< elfcpp::SHN_LORESERVE
|| shndx
>= object
->shnum())
126 object
->error(_("extended index for symbol %u out of range: %u"),
128 return elfcpp::SHN_UNDEF
;
135 // Report an error for this object file. This is used by the
136 // elfcpp::Elf_file interface, and also called by the Object code
140 Object::error(const char* format
, ...) const
143 va_start(args
, format
);
145 if (vasprintf(&buf
, format
, args
) < 0)
148 gold_error(_("%s: %s"), this->name().c_str(), buf
);
152 // Return a view of the contents of a section.
155 Object::section_contents(unsigned int shndx
, section_size_type
* plen
,
158 Location
loc(this->do_section_contents(shndx
));
159 *plen
= convert_to_section_size_type(loc
.data_size
);
162 static const unsigned char empty
[1] = { '\0' };
165 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
168 // Read the section data into SD. This is code common to Sized_relobj
169 // and Sized_dynobj, so we put it into Object.
171 template<int size
, bool big_endian
>
173 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
174 Read_symbols_data
* sd
)
176 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
178 // Read the section headers.
179 const off_t shoff
= elf_file
->shoff();
180 const unsigned int shnum
= this->shnum();
181 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
,
184 // Read the section names.
185 const unsigned char* pshdrs
= sd
->section_headers
->data();
186 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
187 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
189 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
190 this->error(_("section name section has wrong type: %u"),
191 static_cast<unsigned int>(shdrnames
.get_sh_type()));
193 sd
->section_names_size
=
194 convert_to_section_size_type(shdrnames
.get_sh_size());
195 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
196 sd
->section_names_size
, false,
200 // If NAME is the name of a special .gnu.warning section, arrange for
201 // the warning to be issued. SHNDX is the section index. Return
202 // whether it is a warning section.
205 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
206 Symbol_table
* symtab
)
208 const char warn_prefix
[] = ".gnu.warning.";
209 const int warn_prefix_len
= sizeof warn_prefix
- 1;
210 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
212 // Read the section contents to get the warning text. It would
213 // be nicer if we only did this if we have to actually issue a
214 // warning. Unfortunately, warnings are issued as we relocate
215 // sections. That means that we can not lock the object then,
216 // as we might try to issue the same warning multiple times
218 section_size_type len
;
219 const unsigned char* contents
= this->section_contents(shndx
, &len
,
223 const char* warning
= name
+ warn_prefix_len
;
224 contents
= reinterpret_cast<const unsigned char*>(warning
);
225 len
= strlen(warning
);
227 std::string
warning(reinterpret_cast<const char*>(contents
), len
);
228 symtab
->add_warning(name
+ warn_prefix_len
, this, warning
);
234 // If NAME is the name of the special section which indicates that
235 // this object was compiled with -fstack-split, mark it accordingly.
238 Object::handle_split_stack_section(const char* name
)
240 if (strcmp(name
, ".note.GNU-split-stack") == 0)
242 this->uses_split_stack_
= true;
245 if (strcmp(name
, ".note.GNU-no-split-stack") == 0)
247 this->has_no_split_stack_
= true;
255 // To copy the symbols data read from the file to a local data structure.
256 // This function is called from do_layout only while doing garbage
260 Relobj::copy_symbols_data(Symbols_data
* gc_sd
, Read_symbols_data
* sd
,
261 unsigned int section_header_size
)
263 gc_sd
->section_headers_data
=
264 new unsigned char[(section_header_size
)];
265 memcpy(gc_sd
->section_headers_data
, sd
->section_headers
->data(),
266 section_header_size
);
267 gc_sd
->section_names_data
=
268 new unsigned char[sd
->section_names_size
];
269 memcpy(gc_sd
->section_names_data
, sd
->section_names
->data(),
270 sd
->section_names_size
);
271 gc_sd
->section_names_size
= sd
->section_names_size
;
272 if (sd
->symbols
!= NULL
)
274 gc_sd
->symbols_data
=
275 new unsigned char[sd
->symbols_size
];
276 memcpy(gc_sd
->symbols_data
, sd
->symbols
->data(),
281 gc_sd
->symbols_data
= NULL
;
283 gc_sd
->symbols_size
= sd
->symbols_size
;
284 gc_sd
->external_symbols_offset
= sd
->external_symbols_offset
;
285 if (sd
->symbol_names
!= NULL
)
287 gc_sd
->symbol_names_data
=
288 new unsigned char[sd
->symbol_names_size
];
289 memcpy(gc_sd
->symbol_names_data
, sd
->symbol_names
->data(),
290 sd
->symbol_names_size
);
294 gc_sd
->symbol_names_data
= NULL
;
296 gc_sd
->symbol_names_size
= sd
->symbol_names_size
;
299 // This function determines if a particular section name must be included
300 // in the link. This is used during garbage collection to determine the
301 // roots of the worklist.
304 Relobj::is_section_name_included(const char* name
)
306 if (is_prefix_of(".ctors", name
)
307 || is_prefix_of(".dtors", name
)
308 || is_prefix_of(".note", name
)
309 || is_prefix_of(".init", name
)
310 || is_prefix_of(".fini", name
)
311 || is_prefix_of(".gcc_except_table", name
)
312 || is_prefix_of(".jcr", name
)
313 || is_prefix_of(".preinit_array", name
)
314 || (is_prefix_of(".text", name
)
315 && strstr(name
, "personality"))
316 || (is_prefix_of(".data", name
)
317 && strstr(name
, "personality"))
318 || (is_prefix_of(".gnu.linkonce.d", name
)
319 && strstr(name
, "personality")))
326 // Class Sized_relobj.
328 template<int size
, bool big_endian
>
329 Sized_relobj
<size
, big_endian
>::Sized_relobj(
330 const std::string
& name
,
331 Input_file
* input_file
,
333 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
334 : Relobj(name
, input_file
, offset
),
335 elf_file_(this, ehdr
),
337 local_symbol_count_(0),
338 output_local_symbol_count_(0),
339 output_local_dynsym_count_(0),
342 local_symbol_offset_(0),
343 local_dynsym_offset_(0),
345 local_got_offsets_(),
346 kept_comdat_sections_(),
347 has_eh_frame_(false),
348 discarded_eh_frame_shndx_(-1U)
352 template<int size
, bool big_endian
>
353 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
357 // Set up an object file based on the file header. This sets up the
358 // section information.
360 template<int size
, bool big_endian
>
362 Sized_relobj
<size
, big_endian
>::do_setup()
364 const unsigned int shnum
= this->elf_file_
.shnum();
365 this->set_shnum(shnum
);
368 // Find the SHT_SYMTAB section, given the section headers. The ELF
369 // standard says that maybe in the future there can be more than one
370 // SHT_SYMTAB section. Until somebody figures out how that could
371 // work, we assume there is only one.
373 template<int size
, bool big_endian
>
375 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
377 const unsigned int shnum
= this->shnum();
378 this->symtab_shndx_
= 0;
381 // Look through the sections in reverse order, since gas tends
382 // to put the symbol table at the end.
383 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
384 unsigned int i
= shnum
;
385 unsigned int xindex_shndx
= 0;
386 unsigned int xindex_link
= 0;
390 p
-= This::shdr_size
;
391 typename
This::Shdr
shdr(p
);
392 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
394 this->symtab_shndx_
= i
;
395 if (xindex_shndx
> 0 && xindex_link
== i
)
398 new Xindex(this->elf_file_
.large_shndx_offset());
399 xindex
->read_symtab_xindex
<size
, big_endian
>(this,
402 this->set_xindex(xindex
);
407 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
408 // one. This will work if it follows the SHT_SYMTAB
410 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX
)
413 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
419 // Return the Xindex structure to use for object with lots of
422 template<int size
, bool big_endian
>
424 Sized_relobj
<size
, big_endian
>::do_initialize_xindex()
426 gold_assert(this->symtab_shndx_
!= -1U);
427 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
428 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->symtab_shndx_
);
432 // Return whether SHDR has the right type and flags to be a GNU
433 // .eh_frame section.
435 template<int size
, bool big_endian
>
437 Sized_relobj
<size
, big_endian
>::check_eh_frame_flags(
438 const elfcpp::Shdr
<size
, big_endian
>* shdr
) const
440 return (shdr
->get_sh_type() == elfcpp::SHT_PROGBITS
441 && (shdr
->get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
444 // Return whether there is a GNU .eh_frame section, given the section
445 // headers and the section names.
447 template<int size
, bool big_endian
>
449 Sized_relobj
<size
, big_endian
>::find_eh_frame(
450 const unsigned char* pshdrs
,
452 section_size_type names_size
) const
454 const unsigned int shnum
= this->shnum();
455 const unsigned char* p
= pshdrs
+ This::shdr_size
;
456 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
458 typename
This::Shdr
shdr(p
);
459 if (this->check_eh_frame_flags(&shdr
))
461 if (shdr
.get_sh_name() >= names_size
)
463 this->error(_("bad section name offset for section %u: %lu"),
464 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
468 const char* name
= names
+ shdr
.get_sh_name();
469 if (strcmp(name
, ".eh_frame") == 0)
476 // Read the sections and symbols from an object file.
478 template<int size
, bool big_endian
>
480 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
482 this->read_section_data(&this->elf_file_
, sd
);
484 const unsigned char* const pshdrs
= sd
->section_headers
->data();
486 this->find_symtab(pshdrs
);
488 const unsigned char* namesu
= sd
->section_names
->data();
489 const char* names
= reinterpret_cast<const char*>(namesu
);
490 if (memmem(names
, sd
->section_names_size
, ".eh_frame", 10) != NULL
)
492 if (this->find_eh_frame(pshdrs
, names
, sd
->section_names_size
))
493 this->has_eh_frame_
= true;
497 sd
->symbols_size
= 0;
498 sd
->external_symbols_offset
= 0;
499 sd
->symbol_names
= NULL
;
500 sd
->symbol_names_size
= 0;
502 if (this->symtab_shndx_
== 0)
504 // No symbol table. Weird but legal.
508 // Get the symbol table section header.
509 typename
This::Shdr
symtabshdr(pshdrs
510 + this->symtab_shndx_
* This::shdr_size
);
511 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
513 // If this object has a .eh_frame section, we need all the symbols.
514 // Otherwise we only need the external symbols. While it would be
515 // simpler to just always read all the symbols, I've seen object
516 // files with well over 2000 local symbols, which for a 64-bit
517 // object file format is over 5 pages that we don't need to read
520 const int sym_size
= This::sym_size
;
521 const unsigned int loccount
= symtabshdr
.get_sh_info();
522 this->local_symbol_count_
= loccount
;
523 this->local_values_
.resize(loccount
);
524 section_offset_type locsize
= loccount
* sym_size
;
525 off_t dataoff
= symtabshdr
.get_sh_offset();
526 section_size_type datasize
=
527 convert_to_section_size_type(symtabshdr
.get_sh_size());
528 off_t extoff
= dataoff
+ locsize
;
529 section_size_type extsize
= datasize
- locsize
;
531 off_t readoff
= this->has_eh_frame_
? dataoff
: extoff
;
532 section_size_type readsize
= this->has_eh_frame_
? datasize
: extsize
;
536 // No external symbols. Also weird but also legal.
540 File_view
* fvsymtab
= this->get_lasting_view(readoff
, readsize
, true, false);
542 // Read the section header for the symbol names.
543 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
544 if (strtab_shndx
>= this->shnum())
546 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
549 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
550 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
552 this->error(_("symbol table name section has wrong type: %u"),
553 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
557 // Read the symbol names.
558 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
559 strtabshdr
.get_sh_size(),
562 sd
->symbols
= fvsymtab
;
563 sd
->symbols_size
= readsize
;
564 sd
->external_symbols_offset
= this->has_eh_frame_
? locsize
: 0;
565 sd
->symbol_names
= fvstrtab
;
566 sd
->symbol_names_size
=
567 convert_to_section_size_type(strtabshdr
.get_sh_size());
570 // Return the section index of symbol SYM. Set *VALUE to its value in
571 // the object file. Set *IS_ORDINARY if this is an ordinary section
572 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
573 // Note that for a symbol which is not defined in this object file,
574 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
575 // the final value of the symbol in the link.
577 template<int size
, bool big_endian
>
579 Sized_relobj
<size
, big_endian
>::symbol_section_and_value(unsigned int sym
,
583 section_size_type symbols_size
;
584 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
588 const size_t count
= symbols_size
/ This::sym_size
;
589 gold_assert(sym
< count
);
591 elfcpp::Sym
<size
, big_endian
> elfsym(symbols
+ sym
* This::sym_size
);
592 *value
= elfsym
.get_st_value();
594 return this->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
597 // Return whether to include a section group in the link. LAYOUT is
598 // used to keep track of which section groups we have already seen.
599 // INDEX is the index of the section group and SHDR is the section
600 // header. If we do not want to include this group, we set bits in
601 // OMIT for each section which should be discarded.
603 template<int size
, bool big_endian
>
605 Sized_relobj
<size
, big_endian
>::include_section_group(
606 Symbol_table
* symtab
,
610 const unsigned char* shdrs
,
611 const char* section_names
,
612 section_size_type section_names_size
,
613 std::vector
<bool>* omit
)
615 // Read the section contents.
616 typename
This::Shdr
shdr(shdrs
+ index
* This::shdr_size
);
617 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
618 shdr
.get_sh_size(), true, false);
619 const elfcpp::Elf_Word
* pword
=
620 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
622 // The first word contains flags. We only care about COMDAT section
623 // groups. Other section groups are always included in the link
624 // just like ordinary sections.
625 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
627 // Look up the group signature, which is the name of a symbol. This
628 // is a lot of effort to go to to read a string. Why didn't they
629 // just have the group signature point into the string table, rather
630 // than indirect through a symbol?
632 // Get the appropriate symbol table header (this will normally be
633 // the single SHT_SYMTAB section, but in principle it need not be).
634 const unsigned int link
= this->adjust_shndx(shdr
.get_sh_link());
635 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
637 // Read the symbol table entry.
638 unsigned int symndx
= shdr
.get_sh_info();
639 if (symndx
>= symshdr
.get_sh_size() / This::sym_size
)
641 this->error(_("section group %u info %u out of range"),
645 off_t symoff
= symshdr
.get_sh_offset() + symndx
* This::sym_size
;
646 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true,
648 elfcpp::Sym
<size
, big_endian
> sym(psym
);
650 // Read the symbol table names.
651 section_size_type symnamelen
;
652 const unsigned char* psymnamesu
;
653 psymnamesu
= this->section_contents(this->adjust_shndx(symshdr
.get_sh_link()),
655 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
657 // Get the section group signature.
658 if (sym
.get_st_name() >= symnamelen
)
660 this->error(_("symbol %u name offset %u out of range"),
661 symndx
, sym
.get_st_name());
665 std::string
signature(psymnames
+ sym
.get_st_name());
667 // It seems that some versions of gas will create a section group
668 // associated with a section symbol, and then fail to give a name to
669 // the section symbol. In such a case, use the name of the section.
670 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
673 unsigned int sym_shndx
= this->adjust_sym_shndx(symndx
,
676 if (!is_ordinary
|| sym_shndx
>= this->shnum())
678 this->error(_("symbol %u invalid section index %u"),
682 typename
This::Shdr
member_shdr(shdrs
+ sym_shndx
* This::shdr_size
);
683 if (member_shdr
.get_sh_name() < section_names_size
)
684 signature
= section_names
+ member_shdr
.get_sh_name();
687 // Record this section group in the layout, and see whether we've already
688 // seen one with the same signature.
691 Kept_section
* kept_section
= NULL
;
693 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
695 include_group
= true;
700 include_group
= layout
->find_or_add_kept_section(signature
,
702 true, &kept_section
);
706 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
708 std::vector
<unsigned int> shndxes
;
709 bool relocate_group
= include_group
&& parameters
->options().relocatable();
711 shndxes
.reserve(count
- 1);
713 for (size_t i
= 1; i
< count
; ++i
)
715 elfcpp::Elf_Word shndx
=
716 this->adjust_shndx(elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
));
719 shndxes
.push_back(shndx
);
721 if (shndx
>= this->shnum())
723 this->error(_("section %u in section group %u out of range"),
728 // Check for an earlier section number, since we're going to get
729 // it wrong--we may have already decided to include the section.
731 this->error(_("invalid section group %u refers to earlier section %u"),
734 // Get the name of the member section.
735 typename
This::Shdr
member_shdr(shdrs
+ shndx
* This::shdr_size
);
736 if (member_shdr
.get_sh_name() >= section_names_size
)
738 // This is an error, but it will be diagnosed eventually
739 // in do_layout, so we don't need to do anything here but
743 std::string
mname(section_names
+ member_shdr
.get_sh_name());
748 kept_section
->add_comdat_section(mname
, shndx
,
749 member_shdr
.get_sh_size());
753 (*omit
)[shndx
] = true;
757 Relobj
* kept_object
= kept_section
->object();
758 if (kept_section
->is_comdat())
760 // Find the corresponding kept section, and store
761 // that info in the discarded section table.
762 unsigned int kept_shndx
;
764 if (kept_section
->find_comdat_section(mname
, &kept_shndx
,
767 // We don't keep a mapping for this section if
768 // it has a different size. The mapping is only
769 // used for relocation processing, and we don't
770 // want to treat the sections as similar if the
771 // sizes are different. Checking the section
772 // size is the approach used by the GNU linker.
773 if (kept_size
== member_shdr
.get_sh_size())
774 this->set_kept_comdat_section(shndx
, kept_object
,
780 // The existing section is a linkonce section. Add
781 // a mapping if there is exactly one section in the
782 // group (which is true when COUNT == 2) and if it
785 && (kept_section
->linkonce_size()
786 == member_shdr
.get_sh_size()))
787 this->set_kept_comdat_section(shndx
, kept_object
,
788 kept_section
->shndx());
795 layout
->layout_group(symtab
, this, index
, name
, signature
.c_str(),
796 shdr
, flags
, &shndxes
);
798 return include_group
;
801 // Whether to include a linkonce section in the link. NAME is the
802 // name of the section and SHDR is the section header.
804 // Linkonce sections are a GNU extension implemented in the original
805 // GNU linker before section groups were defined. The semantics are
806 // that we only include one linkonce section with a given name. The
807 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
808 // where T is the type of section and SYMNAME is the name of a symbol.
809 // In an attempt to make linkonce sections interact well with section
810 // groups, we try to identify SYMNAME and use it like a section group
811 // signature. We want to block section groups with that signature,
812 // but not other linkonce sections with that signature. We also use
813 // the full name of the linkonce section as a normal section group
816 template<int size
, bool big_endian
>
818 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
822 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
824 typename
elfcpp::Elf_types
<size
>::Elf_WXword sh_size
= shdr
.get_sh_size();
825 // In general the symbol name we want will be the string following
826 // the last '.'. However, we have to handle the case of
827 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
828 // some versions of gcc. So we use a heuristic: if the name starts
829 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
830 // we look for the last '.'. We can't always simply skip
831 // ".gnu.linkonce.X", because we have to deal with cases like
832 // ".gnu.linkonce.d.rel.ro.local".
833 const char* const linkonce_t
= ".gnu.linkonce.t.";
835 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
836 symname
= name
+ strlen(linkonce_t
);
838 symname
= strrchr(name
, '.') + 1;
839 std::string
sig1(symname
);
840 std::string
sig2(name
);
843 bool include1
= layout
->find_or_add_kept_section(sig1
, this, index
, false,
845 bool include2
= layout
->find_or_add_kept_section(sig2
, this, index
, false,
850 // We are not including this section because we already saw the
851 // name of the section as a signature. This normally implies
852 // that the kept section is another linkonce section. If it is
853 // the same size, record it as the section which corresponds to
855 if (kept2
->object() != NULL
856 && !kept2
->is_comdat()
857 && kept2
->linkonce_size() == sh_size
)
858 this->set_kept_comdat_section(index
, kept2
->object(), kept2
->shndx());
862 // The section is being discarded on the basis of its symbol
863 // name. This means that the corresponding kept section was
864 // part of a comdat group, and it will be difficult to identify
865 // the specific section within that group that corresponds to
866 // this linkonce section. We'll handle the simple case where
867 // the group has only one member section. Otherwise, it's not
869 unsigned int kept_shndx
;
871 if (kept1
->object() != NULL
872 && kept1
->is_comdat()
873 && kept1
->find_single_comdat_section(&kept_shndx
, &kept_size
)
874 && kept_size
== sh_size
)
875 this->set_kept_comdat_section(index
, kept1
->object(), kept_shndx
);
879 kept1
->set_linkonce_size(sh_size
);
880 kept2
->set_linkonce_size(sh_size
);
883 return include1
&& include2
;
886 // Layout an input section.
888 template<int size
, bool big_endian
>
890 Sized_relobj
<size
, big_endian
>::layout_section(Layout
* layout
,
893 typename
This::Shdr
& shdr
,
894 unsigned int reloc_shndx
,
895 unsigned int reloc_type
)
898 Output_section
* os
= layout
->layout(this, shndx
, name
, shdr
,
899 reloc_shndx
, reloc_type
, &offset
);
901 this->output_sections()[shndx
] = os
;
903 this->section_offsets_
[shndx
] = invalid_address
;
905 this->section_offsets_
[shndx
] = convert_types
<Address
, off_t
>(offset
);
907 // If this section requires special handling, and if there are
908 // relocs that apply to it, then we must do the special handling
909 // before we apply the relocs.
910 if (offset
== -1 && reloc_shndx
!= 0)
911 this->set_relocs_must_follow_section_writes();
914 // Lay out the input sections. We walk through the sections and check
915 // whether they should be included in the link. If they should, we
916 // pass them to the Layout object, which will return an output section
918 // During garbage collection (--gc-sections) and identical code folding
919 // (--icf), this function is called twice. When it is called the first
920 // time, it is for setting up some sections as roots to a work-list for
921 // --gc-sections and to do comdat processing. Actual layout happens the
922 // second time around after all the relevant sections have been determined.
923 // The first time, is_worklist_ready or is_icf_ready is false. It is then
924 // set to true after the garbage collection worklist or identical code
925 // folding is processed and the relevant sections to be kept are
926 // determined. Then, this function is called again to layout the sections.
928 template<int size
, bool big_endian
>
930 Sized_relobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
932 Read_symbols_data
* sd
)
934 const unsigned int shnum
= this->shnum();
935 bool is_gc_pass_one
= ((parameters
->options().gc_sections()
936 && !symtab
->gc()->is_worklist_ready())
937 || (parameters
->options().icf_enabled()
938 && !symtab
->icf()->is_icf_ready()));
940 bool is_gc_pass_two
= ((parameters
->options().gc_sections()
941 && symtab
->gc()->is_worklist_ready())
942 || (parameters
->options().icf_enabled()
943 && symtab
->icf()->is_icf_ready()));
945 bool is_gc_or_icf
= (parameters
->options().gc_sections()
946 || parameters
->options().icf_enabled());
948 // Both is_gc_pass_one and is_gc_pass_two should not be true.
949 gold_assert(!(is_gc_pass_one
&& is_gc_pass_two
));
953 Symbols_data
* gc_sd
= NULL
;
956 // During garbage collection save the symbols data to use it when
957 // re-entering this function.
958 gc_sd
= new Symbols_data
;
959 this->copy_symbols_data(gc_sd
, sd
, This::shdr_size
* shnum
);
960 this->set_symbols_data(gc_sd
);
962 else if (is_gc_pass_two
)
964 gc_sd
= this->get_symbols_data();
967 const unsigned char* section_headers_data
= NULL
;
968 section_size_type section_names_size
;
969 const unsigned char* symbols_data
= NULL
;
970 section_size_type symbols_size
;
971 section_offset_type external_symbols_offset
;
972 const unsigned char* symbol_names_data
= NULL
;
973 section_size_type symbol_names_size
;
977 section_headers_data
= gc_sd
->section_headers_data
;
978 section_names_size
= gc_sd
->section_names_size
;
979 symbols_data
= gc_sd
->symbols_data
;
980 symbols_size
= gc_sd
->symbols_size
;
981 external_symbols_offset
= gc_sd
->external_symbols_offset
;
982 symbol_names_data
= gc_sd
->symbol_names_data
;
983 symbol_names_size
= gc_sd
->symbol_names_size
;
987 section_headers_data
= sd
->section_headers
->data();
988 section_names_size
= sd
->section_names_size
;
989 if (sd
->symbols
!= NULL
)
990 symbols_data
= sd
->symbols
->data();
991 symbols_size
= sd
->symbols_size
;
992 external_symbols_offset
= sd
->external_symbols_offset
;
993 if (sd
->symbol_names
!= NULL
)
994 symbol_names_data
= sd
->symbol_names
->data();
995 symbol_names_size
= sd
->symbol_names_size
;
998 // Get the section headers.
999 const unsigned char* shdrs
= section_headers_data
;
1000 const unsigned char* pshdrs
;
1002 // Get the section names.
1003 const unsigned char* pnamesu
= (is_gc_or_icf
)
1004 ? gc_sd
->section_names_data
1005 : sd
->section_names
->data();
1007 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1009 // If any input files have been claimed by plugins, we need to defer
1010 // actual layout until the replacement files have arrived.
1011 const bool should_defer_layout
=
1012 (parameters
->options().has_plugins()
1013 && parameters
->options().plugins()->should_defer_layout());
1014 unsigned int num_sections_to_defer
= 0;
1016 // For each section, record the index of the reloc section if any.
1017 // Use 0 to mean that there is no reloc section, -1U to mean that
1018 // there is more than one.
1019 std::vector
<unsigned int> reloc_shndx(shnum
, 0);
1020 std::vector
<unsigned int> reloc_type(shnum
, elfcpp::SHT_NULL
);
1021 // Skip the first, dummy, section.
1022 pshdrs
= shdrs
+ This::shdr_size
;
1023 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
1025 typename
This::Shdr
shdr(pshdrs
);
1027 // Count the number of sections whose layout will be deferred.
1028 if (should_defer_layout
&& (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1029 ++num_sections_to_defer
;
1031 unsigned int sh_type
= shdr
.get_sh_type();
1032 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
1034 unsigned int target_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1035 if (target_shndx
== 0 || target_shndx
>= shnum
)
1037 this->error(_("relocation section %u has bad info %u"),
1042 if (reloc_shndx
[target_shndx
] != 0)
1043 reloc_shndx
[target_shndx
] = -1U;
1046 reloc_shndx
[target_shndx
] = i
;
1047 reloc_type
[target_shndx
] = sh_type
;
1052 Output_sections
& out_sections(this->output_sections());
1053 std::vector
<Address
>& out_section_offsets(this->section_offsets_
);
1055 if (!is_gc_pass_two
)
1057 out_sections
.resize(shnum
);
1058 out_section_offsets
.resize(shnum
);
1061 // If we are only linking for symbols, then there is nothing else to
1063 if (this->input_file()->just_symbols())
1065 if (!is_gc_pass_two
)
1067 delete sd
->section_headers
;
1068 sd
->section_headers
= NULL
;
1069 delete sd
->section_names
;
1070 sd
->section_names
= NULL
;
1075 if (num_sections_to_defer
> 0)
1077 parameters
->options().plugins()->add_deferred_layout_object(this);
1078 this->deferred_layout_
.reserve(num_sections_to_defer
);
1081 // Whether we've seen a .note.GNU-stack section.
1082 bool seen_gnu_stack
= false;
1083 // The flags of a .note.GNU-stack section.
1084 uint64_t gnu_stack_flags
= 0;
1086 // Keep track of which sections to omit.
1087 std::vector
<bool> omit(shnum
, false);
1089 // Keep track of reloc sections when emitting relocations.
1090 const bool relocatable
= parameters
->options().relocatable();
1091 const bool emit_relocs
= (relocatable
1092 || parameters
->options().emit_relocs());
1093 std::vector
<unsigned int> reloc_sections
;
1095 // Keep track of .eh_frame sections.
1096 std::vector
<unsigned int> eh_frame_sections
;
1098 // Skip the first, dummy, section.
1099 pshdrs
= shdrs
+ This::shdr_size
;
1100 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
1102 typename
This::Shdr
shdr(pshdrs
);
1104 if (shdr
.get_sh_name() >= section_names_size
)
1106 this->error(_("bad section name offset for section %u: %lu"),
1107 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
1111 const char* name
= pnames
+ shdr
.get_sh_name();
1113 if (!is_gc_pass_two
)
1115 if (this->handle_gnu_warning_section(name
, i
, symtab
))
1121 // The .note.GNU-stack section is special. It gives the
1122 // protection flags that this object file requires for the stack
1124 if (strcmp(name
, ".note.GNU-stack") == 0)
1126 seen_gnu_stack
= true;
1127 gnu_stack_flags
|= shdr
.get_sh_flags();
1131 // The .note.GNU-split-stack section is also special. It
1132 // indicates that the object was compiled with
1134 if (this->handle_split_stack_section(name
))
1136 if (!parameters
->options().relocatable()
1137 && !parameters
->options().shared())
1141 // Skip attributes section.
1142 if (parameters
->target().is_attributes_section(name
))
1147 bool discard
= omit
[i
];
1150 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
1152 if (!this->include_section_group(symtab
, layout
, i
, name
,
1158 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
1159 && Layout::is_linkonce(name
))
1161 if (!this->include_linkonce_section(layout
, i
, name
, shdr
))
1168 // Do not include this section in the link.
1169 out_sections
[i
] = NULL
;
1170 out_section_offsets
[i
] = invalid_address
;
1175 if (is_gc_pass_one
&& parameters
->options().gc_sections())
1177 if (is_section_name_included(name
)
1178 || shdr
.get_sh_type() == elfcpp::SHT_INIT_ARRAY
1179 || shdr
.get_sh_type() == elfcpp::SHT_FINI_ARRAY
)
1181 symtab
->gc()->worklist().push(Section_id(this, i
));
1183 // If the section name XXX can be represented as a C identifier
1184 // it cannot be discarded if there are references to
1185 // __start_XXX and __stop_XXX symbols. These need to be
1186 // specially handled.
1187 if (is_cident(name
))
1189 symtab
->gc()->add_cident_section(name
, Section_id(this, i
));
1193 // When doing a relocatable link we are going to copy input
1194 // reloc sections into the output. We only want to copy the
1195 // ones associated with sections which are not being discarded.
1196 // However, we don't know that yet for all sections. So save
1197 // reloc sections and process them later. Garbage collection is
1198 // not triggered when relocatable code is desired.
1200 && (shdr
.get_sh_type() == elfcpp::SHT_REL
1201 || shdr
.get_sh_type() == elfcpp::SHT_RELA
))
1203 reloc_sections
.push_back(i
);
1207 if (relocatable
&& shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
1210 // The .eh_frame section is special. It holds exception frame
1211 // information that we need to read in order to generate the
1212 // exception frame header. We process these after all the other
1213 // sections so that the exception frame reader can reliably
1214 // determine which sections are being discarded, and discard the
1215 // corresponding information.
1217 && strcmp(name
, ".eh_frame") == 0
1218 && this->check_eh_frame_flags(&shdr
))
1222 out_sections
[i
] = reinterpret_cast<Output_section
*>(1);
1223 out_section_offsets
[i
] = invalid_address
;
1226 eh_frame_sections
.push_back(i
);
1230 if (is_gc_pass_two
&& parameters
->options().gc_sections())
1232 // This is executed during the second pass of garbage
1233 // collection. do_layout has been called before and some
1234 // sections have been already discarded. Simply ignore
1235 // such sections this time around.
1236 if (out_sections
[i
] == NULL
)
1238 gold_assert(out_section_offsets
[i
] == invalid_address
);
1241 if (((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1242 && symtab
->gc()->is_section_garbage(this, i
))
1244 if (parameters
->options().print_gc_sections())
1245 gold_info(_("%s: removing unused section from '%s'"
1247 program_name
, this->section_name(i
).c_str(),
1248 this->name().c_str());
1249 out_sections
[i
] = NULL
;
1250 out_section_offsets
[i
] = invalid_address
;
1255 if (is_gc_pass_two
&& parameters
->options().icf_enabled())
1257 if (out_sections
[i
] == NULL
)
1259 gold_assert(out_section_offsets
[i
] == invalid_address
);
1262 if (((shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
) != 0)
1263 && symtab
->icf()->is_section_folded(this, i
))
1265 if (parameters
->options().print_icf_sections())
1268 symtab
->icf()->get_folded_section(this, i
);
1269 Relobj
* folded_obj
=
1270 reinterpret_cast<Relobj
*>(folded
.first
);
1271 gold_info(_("%s: ICF folding section '%s' in file '%s'"
1272 "into '%s' in file '%s'"),
1273 program_name
, this->section_name(i
).c_str(),
1274 this->name().c_str(),
1275 folded_obj
->section_name(folded
.second
).c_str(),
1276 folded_obj
->name().c_str());
1278 out_sections
[i
] = NULL
;
1279 out_section_offsets
[i
] = invalid_address
;
1284 // Defer layout here if input files are claimed by plugins. When gc
1285 // is turned on this function is called twice. For the second call
1286 // should_defer_layout should be false.
1287 if (should_defer_layout
&& (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1289 gold_assert(!is_gc_pass_two
);
1290 this->deferred_layout_
.push_back(Deferred_layout(i
, name
,
1294 // Put dummy values here; real values will be supplied by
1295 // do_layout_deferred_sections.
1296 out_sections
[i
] = reinterpret_cast<Output_section
*>(2);
1297 out_section_offsets
[i
] = invalid_address
;
1301 // During gc_pass_two if a section that was previously deferred is
1302 // found, do not layout the section as layout_deferred_sections will
1303 // do it later from gold.cc.
1305 && (out_sections
[i
] == reinterpret_cast<Output_section
*>(2)))
1310 // This is during garbage collection. The out_sections are
1311 // assigned in the second call to this function.
1312 out_sections
[i
] = reinterpret_cast<Output_section
*>(1);
1313 out_section_offsets
[i
] = invalid_address
;
1317 // When garbage collection is switched on the actual layout
1318 // only happens in the second call.
1319 this->layout_section(layout
, i
, name
, shdr
, reloc_shndx
[i
],
1324 if (!is_gc_pass_two
)
1325 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
);
1327 // When doing a relocatable link handle the reloc sections at the
1328 // end. Garbage collection and Identical Code Folding is not
1329 // turned on for relocatable code.
1331 this->size_relocatable_relocs();
1333 gold_assert(!(is_gc_or_icf
) || reloc_sections
.empty());
1335 for (std::vector
<unsigned int>::const_iterator p
= reloc_sections
.begin();
1336 p
!= reloc_sections
.end();
1339 unsigned int i
= *p
;
1340 const unsigned char* pshdr
;
1341 pshdr
= section_headers_data
+ i
* This::shdr_size
;
1342 typename
This::Shdr
shdr(pshdr
);
1344 unsigned int data_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1345 if (data_shndx
>= shnum
)
1347 // We already warned about this above.
1351 Output_section
* data_section
= out_sections
[data_shndx
];
1352 if (data_section
== NULL
)
1354 out_sections
[i
] = NULL
;
1355 out_section_offsets
[i
] = invalid_address
;
1359 Relocatable_relocs
* rr
= new Relocatable_relocs();
1360 this->set_relocatable_relocs(i
, rr
);
1362 Output_section
* os
= layout
->layout_reloc(this, i
, shdr
, data_section
,
1364 out_sections
[i
] = os
;
1365 out_section_offsets
[i
] = invalid_address
;
1368 // Handle the .eh_frame sections at the end.
1369 gold_assert(!is_gc_pass_one
|| eh_frame_sections
.empty());
1370 for (std::vector
<unsigned int>::const_iterator p
= eh_frame_sections
.begin();
1371 p
!= eh_frame_sections
.end();
1374 gold_assert(this->has_eh_frame_
);
1375 gold_assert(external_symbols_offset
!= 0);
1377 unsigned int i
= *p
;
1378 const unsigned char *pshdr
;
1379 pshdr
= section_headers_data
+ i
* This::shdr_size
;
1380 typename
This::Shdr
shdr(pshdr
);
1383 Output_section
* os
= layout
->layout_eh_frame(this,
1392 out_sections
[i
] = os
;
1393 if (os
== NULL
|| offset
== -1)
1395 // An object can contain at most one section holding exception
1396 // frame information.
1397 gold_assert(this->discarded_eh_frame_shndx_
== -1U);
1398 this->discarded_eh_frame_shndx_
= i
;
1399 out_section_offsets
[i
] = invalid_address
;
1402 out_section_offsets
[i
] = convert_types
<Address
, off_t
>(offset
);
1404 // If this section requires special handling, and if there are
1405 // relocs that apply to it, then we must do the special handling
1406 // before we apply the relocs.
1407 if (os
!= NULL
&& offset
== -1 && reloc_shndx
[i
] != 0)
1408 this->set_relocs_must_follow_section_writes();
1413 delete[] gc_sd
->section_headers_data
;
1414 delete[] gc_sd
->section_names_data
;
1415 delete[] gc_sd
->symbols_data
;
1416 delete[] gc_sd
->symbol_names_data
;
1417 this->set_symbols_data(NULL
);
1421 delete sd
->section_headers
;
1422 sd
->section_headers
= NULL
;
1423 delete sd
->section_names
;
1424 sd
->section_names
= NULL
;
1428 // Layout sections whose layout was deferred while waiting for
1429 // input files from a plugin.
1431 template<int size
, bool big_endian
>
1433 Sized_relobj
<size
, big_endian
>::do_layout_deferred_sections(Layout
* layout
)
1435 typename
std::vector
<Deferred_layout
>::iterator deferred
;
1437 for (deferred
= this->deferred_layout_
.begin();
1438 deferred
!= this->deferred_layout_
.end();
1441 typename
This::Shdr
shdr(deferred
->shdr_data_
);
1442 this->layout_section(layout
, deferred
->shndx_
, deferred
->name_
.c_str(),
1443 shdr
, deferred
->reloc_shndx_
, deferred
->reloc_type_
);
1446 this->deferred_layout_
.clear();
1449 // Add the symbols to the symbol table.
1451 template<int size
, bool big_endian
>
1453 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1454 Read_symbols_data
* sd
,
1457 if (sd
->symbols
== NULL
)
1459 gold_assert(sd
->symbol_names
== NULL
);
1463 const int sym_size
= This::sym_size
;
1464 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
1466 if (symcount
* sym_size
!= sd
->symbols_size
- sd
->external_symbols_offset
)
1468 this->error(_("size of symbols is not multiple of symbol size"));
1472 this->symbols_
.resize(symcount
);
1474 const char* sym_names
=
1475 reinterpret_cast<const char*>(sd
->symbol_names
->data());
1476 symtab
->add_from_relobj(this,
1477 sd
->symbols
->data() + sd
->external_symbols_offset
,
1478 symcount
, this->local_symbol_count_
,
1479 sym_names
, sd
->symbol_names_size
,
1481 &this->defined_count_
);
1485 delete sd
->symbol_names
;
1486 sd
->symbol_names
= NULL
;
1489 // Find out if this object, that is a member of a lib group, should be included
1490 // in the link. We check every symbol defined by this object. If the symbol
1491 // table has a strong undefined reference to that symbol, we have to include
1494 template<int size
, bool big_endian
>
1495 Archive::Should_include
1496 Sized_relobj
<size
, big_endian
>::do_should_include_member(Symbol_table
* symtab
,
1497 Read_symbols_data
* sd
,
1500 char* tmpbuf
= NULL
;
1501 size_t tmpbuflen
= 0;
1502 const char* sym_names
=
1503 reinterpret_cast<const char*>(sd
->symbol_names
->data());
1504 const unsigned char* syms
=
1505 sd
->symbols
->data() + sd
->external_symbols_offset
;
1506 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1507 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
1510 const unsigned char* p
= syms
;
1512 for (size_t i
= 0; i
< symcount
; ++i
, p
+= sym_size
)
1514 elfcpp::Sym
<size
, big_endian
> sym(p
);
1515 unsigned int st_shndx
= sym
.get_st_shndx();
1516 if (st_shndx
== elfcpp::SHN_UNDEF
)
1519 unsigned int st_name
= sym
.get_st_name();
1520 const char* name
= sym_names
+ st_name
;
1522 Archive::Should_include t
= Archive::should_include_member(symtab
, name
,
1526 if (t
== Archive::SHOULD_INCLUDE_YES
)
1535 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1538 // First pass over the local symbols. Here we add their names to
1539 // *POOL and *DYNPOOL, and we store the symbol value in
1540 // THIS->LOCAL_VALUES_. This function is always called from a
1541 // singleton thread. This is followed by a call to
1542 // finalize_local_symbols.
1544 template<int size
, bool big_endian
>
1546 Sized_relobj
<size
, big_endian
>::do_count_local_symbols(Stringpool
* pool
,
1547 Stringpool
* dynpool
)
1549 gold_assert(this->symtab_shndx_
!= -1U);
1550 if (this->symtab_shndx_
== 0)
1552 // This object has no symbols. Weird but legal.
1556 // Read the symbol table section header.
1557 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1558 typename
This::Shdr
symtabshdr(this,
1559 this->elf_file_
.section_header(symtab_shndx
));
1560 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1562 // Read the local symbols.
1563 const int sym_size
= This::sym_size
;
1564 const unsigned int loccount
= this->local_symbol_count_
;
1565 gold_assert(loccount
== symtabshdr
.get_sh_info());
1566 off_t locsize
= loccount
* sym_size
;
1567 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1568 locsize
, true, true);
1570 // Read the symbol names.
1571 const unsigned int strtab_shndx
=
1572 this->adjust_shndx(symtabshdr
.get_sh_link());
1573 section_size_type strtab_size
;
1574 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1577 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1579 // Loop over the local symbols.
1581 const Output_sections
& out_sections(this->output_sections());
1582 unsigned int shnum
= this->shnum();
1583 unsigned int count
= 0;
1584 unsigned int dyncount
= 0;
1585 // Skip the first, dummy, symbol.
1587 bool discard_all
= parameters
->options().discard_all();
1588 bool discard_locals
= parameters
->options().discard_locals();
1589 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1591 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1593 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1596 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
1598 lv
.set_input_shndx(shndx
, is_ordinary
);
1600 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1601 lv
.set_is_section_symbol();
1602 else if (sym
.get_st_type() == elfcpp::STT_TLS
)
1603 lv
.set_is_tls_symbol();
1605 // Save the input symbol value for use in do_finalize_local_symbols().
1606 lv
.set_input_value(sym
.get_st_value());
1608 // Decide whether this symbol should go into the output file.
1610 if ((shndx
< shnum
&& out_sections
[shndx
] == NULL
)
1611 || shndx
== this->discarded_eh_frame_shndx_
)
1613 lv
.set_no_output_symtab_entry();
1614 gold_assert(!lv
.needs_output_dynsym_entry());
1618 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1620 lv
.set_no_output_symtab_entry();
1621 gold_assert(!lv
.needs_output_dynsym_entry());
1625 if (sym
.get_st_name() >= strtab_size
)
1627 this->error(_("local symbol %u section name out of range: %u >= %u"),
1628 i
, sym
.get_st_name(),
1629 static_cast<unsigned int>(strtab_size
));
1630 lv
.set_no_output_symtab_entry();
1634 const char* name
= pnames
+ sym
.get_st_name();
1636 // If needed, add the symbol to the dynamic symbol table string pool.
1637 if (lv
.needs_output_dynsym_entry())
1639 dynpool
->add(name
, true, NULL
);
1643 if (discard_all
&& lv
.may_be_discarded_from_output_symtab())
1645 lv
.set_no_output_symtab_entry();
1649 // If --discard-locals option is used, discard all temporary local
1650 // symbols. These symbols start with system-specific local label
1651 // prefixes, typically .L for ELF system. We want to be compatible
1652 // with GNU ld so here we essentially use the same check in
1653 // bfd_is_local_label(). The code is different because we already
1656 // - the symbol is local and thus cannot have global or weak binding.
1657 // - the symbol is not a section symbol.
1658 // - the symbol has a name.
1660 // We do not discard a symbol if it needs a dynamic symbol entry.
1662 && sym
.get_st_type() != elfcpp::STT_FILE
1663 && !lv
.needs_output_dynsym_entry()
1664 && lv
.may_be_discarded_from_output_symtab()
1665 && parameters
->target().is_local_label_name(name
))
1667 lv
.set_no_output_symtab_entry();
1671 // Discard the local symbol if -retain_symbols_file is specified
1672 // and the local symbol is not in that file.
1673 if (!parameters
->options().should_retain_symbol(name
))
1675 lv
.set_no_output_symtab_entry();
1679 // Add the symbol to the symbol table string pool.
1680 pool
->add(name
, true, NULL
);
1684 this->output_local_symbol_count_
= count
;
1685 this->output_local_dynsym_count_
= dyncount
;
1688 // Finalize the local symbols. Here we set the final value in
1689 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1690 // This function is always called from a singleton thread. The actual
1691 // output of the local symbols will occur in a separate task.
1693 template<int size
, bool big_endian
>
1695 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
1697 Symbol_table
* symtab
)
1699 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1701 const unsigned int loccount
= this->local_symbol_count_
;
1702 this->local_symbol_offset_
= off
;
1704 const bool relocatable
= parameters
->options().relocatable();
1705 const Output_sections
& out_sections(this->output_sections());
1706 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
1707 unsigned int shnum
= this->shnum();
1709 for (unsigned int i
= 1; i
< loccount
; ++i
)
1711 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1714 unsigned int shndx
= lv
.input_shndx(&is_ordinary
);
1716 // Set the output symbol value.
1720 if (shndx
== elfcpp::SHN_ABS
|| Symbol::is_common_shndx(shndx
))
1721 lv
.set_output_value(lv
.input_value());
1724 this->error(_("unknown section index %u for local symbol %u"),
1726 lv
.set_output_value(0);
1733 this->error(_("local symbol %u section index %u out of range"),
1738 Output_section
* os
= out_sections
[shndx
];
1739 Address secoffset
= out_offsets
[shndx
];
1740 if (symtab
->is_section_folded(this, shndx
))
1742 gold_assert (os
== NULL
&& secoffset
== invalid_address
);
1743 // Get the os of the section it is folded onto.
1744 Section_id folded
= symtab
->icf()->get_folded_section(this,
1746 gold_assert(folded
.first
!= NULL
);
1747 Sized_relobj
<size
, big_endian
>* folded_obj
= reinterpret_cast
1748 <Sized_relobj
<size
, big_endian
>*>(folded
.first
);
1749 os
= folded_obj
->output_section(folded
.second
);
1750 gold_assert(os
!= NULL
);
1751 secoffset
= folded_obj
->get_output_section_offset(folded
.second
);
1753 // This could be a relaxed input section.
1754 if (secoffset
== invalid_address
)
1756 const Output_relaxed_input_section
* relaxed_section
=
1757 os
->find_relaxed_input_section(folded_obj
, folded
.second
);
1758 gold_assert(relaxed_section
!= NULL
);
1759 secoffset
= relaxed_section
->address() - os
->address();
1765 // This local symbol belongs to a section we are discarding.
1766 // In some cases when applying relocations later, we will
1767 // attempt to match it to the corresponding kept section,
1768 // so we leave the input value unchanged here.
1771 else if (secoffset
== invalid_address
)
1775 // This is a SHF_MERGE section or one which otherwise
1776 // requires special handling.
1777 if (shndx
== this->discarded_eh_frame_shndx_
)
1779 // This local symbol belongs to a discarded .eh_frame
1780 // section. Just treat it like the case in which
1781 // os == NULL above.
1782 gold_assert(this->has_eh_frame_
);
1785 else if (!lv
.is_section_symbol())
1787 // This is not a section symbol. We can determine
1788 // the final value now.
1789 lv
.set_output_value(os
->output_address(this, shndx
,
1792 else if (!os
->find_starting_output_address(this, shndx
, &start
))
1794 // This is a section symbol, but apparently not one in a
1795 // merged section. First check to see if this is a relaxed
1796 // input section. If so, use its address. Otherwise just
1797 // use the start of the output section. This happens with
1798 // relocatable links when the input object has section
1799 // symbols for arbitrary non-merge sections.
1800 const Output_section_data
* posd
=
1801 os
->find_relaxed_input_section(this, shndx
);
1803 lv
.set_output_value(posd
->address());
1805 lv
.set_output_value(os
->address());
1809 // We have to consider the addend to determine the
1810 // value to use in a relocation. START is the start
1811 // of this input section.
1812 Merged_symbol_value
<size
>* msv
=
1813 new Merged_symbol_value
<size
>(lv
.input_value(), start
);
1814 lv
.set_merged_symbol_value(msv
);
1817 else if (lv
.is_tls_symbol())
1818 lv
.set_output_value(os
->tls_offset()
1820 + lv
.input_value());
1822 lv
.set_output_value((relocatable
? 0 : os
->address())
1824 + lv
.input_value());
1827 if (!lv
.is_output_symtab_index_set())
1829 lv
.set_output_symtab_index(index
);
1836 // Set the output dynamic symbol table indexes for the local variables.
1838 template<int size
, bool big_endian
>
1840 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_indexes(unsigned int index
)
1842 const unsigned int loccount
= this->local_symbol_count_
;
1843 for (unsigned int i
= 1; i
< loccount
; ++i
)
1845 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1846 if (lv
.needs_output_dynsym_entry())
1848 lv
.set_output_dynsym_index(index
);
1855 // Set the offset where local dynamic symbol information will be stored.
1856 // Returns the count of local symbols contributed to the symbol table by
1859 template<int size
, bool big_endian
>
1861 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_offset(off_t off
)
1863 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1864 this->local_dynsym_offset_
= off
;
1865 return this->output_local_dynsym_count_
;
1868 // If Symbols_data is not NULL get the section flags from here otherwise
1869 // get it from the file.
1871 template<int size
, bool big_endian
>
1873 Sized_relobj
<size
, big_endian
>::do_section_flags(unsigned int shndx
)
1875 Symbols_data
* sd
= this->get_symbols_data();
1878 const unsigned char* pshdrs
= sd
->section_headers_data
1879 + This::shdr_size
* shndx
;
1880 typename
This::Shdr
shdr(pshdrs
);
1881 return shdr
.get_sh_flags();
1883 // If sd is NULL, read the section header from the file.
1884 return this->elf_file_
.section_flags(shndx
);
1887 // Get the section's ent size from Symbols_data. Called by get_section_contents
1890 template<int size
, bool big_endian
>
1892 Sized_relobj
<size
, big_endian
>::do_section_entsize(unsigned int shndx
)
1894 Symbols_data
* sd
= this->get_symbols_data();
1895 gold_assert (sd
!= NULL
);
1897 const unsigned char* pshdrs
= sd
->section_headers_data
1898 + This::shdr_size
* shndx
;
1899 typename
This::Shdr
shdr(pshdrs
);
1900 return shdr
.get_sh_entsize();
1904 // Write out the local symbols.
1906 template<int size
, bool big_endian
>
1908 Sized_relobj
<size
, big_endian
>::write_local_symbols(
1910 const Stringpool
* sympool
,
1911 const Stringpool
* dynpool
,
1912 Output_symtab_xindex
* symtab_xindex
,
1913 Output_symtab_xindex
* dynsym_xindex
)
1915 const bool strip_all
= parameters
->options().strip_all();
1918 if (this->output_local_dynsym_count_
== 0)
1920 this->output_local_symbol_count_
= 0;
1923 gold_assert(this->symtab_shndx_
!= -1U);
1924 if (this->symtab_shndx_
== 0)
1926 // This object has no symbols. Weird but legal.
1930 // Read the symbol table section header.
1931 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1932 typename
This::Shdr
symtabshdr(this,
1933 this->elf_file_
.section_header(symtab_shndx
));
1934 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1935 const unsigned int loccount
= this->local_symbol_count_
;
1936 gold_assert(loccount
== symtabshdr
.get_sh_info());
1938 // Read the local symbols.
1939 const int sym_size
= This::sym_size
;
1940 off_t locsize
= loccount
* sym_size
;
1941 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1942 locsize
, true, false);
1944 // Read the symbol names.
1945 const unsigned int strtab_shndx
=
1946 this->adjust_shndx(symtabshdr
.get_sh_link());
1947 section_size_type strtab_size
;
1948 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1951 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1953 // Get views into the output file for the portions of the symbol table
1954 // and the dynamic symbol table that we will be writing.
1955 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
1956 unsigned char* oview
= NULL
;
1957 if (output_size
> 0)
1958 oview
= of
->get_output_view(this->local_symbol_offset_
, output_size
);
1960 off_t dyn_output_size
= this->output_local_dynsym_count_
* sym_size
;
1961 unsigned char* dyn_oview
= NULL
;
1962 if (dyn_output_size
> 0)
1963 dyn_oview
= of
->get_output_view(this->local_dynsym_offset_
,
1966 const Output_sections
out_sections(this->output_sections());
1968 gold_assert(this->local_values_
.size() == loccount
);
1970 unsigned char* ov
= oview
;
1971 unsigned char* dyn_ov
= dyn_oview
;
1973 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1975 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
1977 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1980 unsigned int st_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
1984 gold_assert(st_shndx
< out_sections
.size());
1985 if (out_sections
[st_shndx
] == NULL
)
1987 st_shndx
= out_sections
[st_shndx
]->out_shndx();
1988 if (st_shndx
>= elfcpp::SHN_LORESERVE
)
1990 if (lv
.has_output_symtab_entry())
1991 symtab_xindex
->add(lv
.output_symtab_index(), st_shndx
);
1992 if (lv
.has_output_dynsym_entry())
1993 dynsym_xindex
->add(lv
.output_dynsym_index(), st_shndx
);
1994 st_shndx
= elfcpp::SHN_XINDEX
;
1998 // Write the symbol to the output symbol table.
1999 if (lv
.has_output_symtab_entry())
2001 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
2003 gold_assert(isym
.get_st_name() < strtab_size
);
2004 const char* name
= pnames
+ isym
.get_st_name();
2005 osym
.put_st_name(sympool
->get_offset(name
));
2006 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
2007 osym
.put_st_size(isym
.get_st_size());
2008 osym
.put_st_info(isym
.get_st_info());
2009 osym
.put_st_other(isym
.get_st_other());
2010 osym
.put_st_shndx(st_shndx
);
2015 // Write the symbol to the output dynamic symbol table.
2016 if (lv
.has_output_dynsym_entry())
2018 gold_assert(dyn_ov
< dyn_oview
+ dyn_output_size
);
2019 elfcpp::Sym_write
<size
, big_endian
> osym(dyn_ov
);
2021 gold_assert(isym
.get_st_name() < strtab_size
);
2022 const char* name
= pnames
+ isym
.get_st_name();
2023 osym
.put_st_name(dynpool
->get_offset(name
));
2024 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
2025 osym
.put_st_size(isym
.get_st_size());
2026 osym
.put_st_info(isym
.get_st_info());
2027 osym
.put_st_other(isym
.get_st_other());
2028 osym
.put_st_shndx(st_shndx
);
2035 if (output_size
> 0)
2037 gold_assert(ov
- oview
== output_size
);
2038 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
2041 if (dyn_output_size
> 0)
2043 gold_assert(dyn_ov
- dyn_oview
== dyn_output_size
);
2044 of
->write_output_view(this->local_dynsym_offset_
, dyn_output_size
,
2049 // Set *INFO to symbolic information about the offset OFFSET in the
2050 // section SHNDX. Return true if we found something, false if we
2053 template<int size
, bool big_endian
>
2055 Sized_relobj
<size
, big_endian
>::get_symbol_location_info(
2058 Symbol_location_info
* info
)
2060 if (this->symtab_shndx_
== 0)
2063 section_size_type symbols_size
;
2064 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
2068 unsigned int symbol_names_shndx
=
2069 this->adjust_shndx(this->section_link(this->symtab_shndx_
));
2070 section_size_type names_size
;
2071 const unsigned char* symbol_names_u
=
2072 this->section_contents(symbol_names_shndx
, &names_size
, false);
2073 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
2075 const int sym_size
= This::sym_size
;
2076 const size_t count
= symbols_size
/ sym_size
;
2078 const unsigned char* p
= symbols
;
2079 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
2081 elfcpp::Sym
<size
, big_endian
> sym(p
);
2083 if (sym
.get_st_type() == elfcpp::STT_FILE
)
2085 if (sym
.get_st_name() >= names_size
)
2086 info
->source_file
= "(invalid)";
2088 info
->source_file
= symbol_names
+ sym
.get_st_name();
2093 unsigned int st_shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
2096 && st_shndx
== shndx
2097 && static_cast<off_t
>(sym
.get_st_value()) <= offset
2098 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
2101 if (sym
.get_st_name() > names_size
)
2102 info
->enclosing_symbol_name
= "(invalid)";
2105 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
2106 if (parameters
->options().do_demangle())
2108 char* demangled_name
= cplus_demangle(
2109 info
->enclosing_symbol_name
.c_str(),
2110 DMGL_ANSI
| DMGL_PARAMS
);
2111 if (demangled_name
!= NULL
)
2113 info
->enclosing_symbol_name
.assign(demangled_name
);
2114 free(demangled_name
);
2125 // Look for a kept section corresponding to the given discarded section,
2126 // and return its output address. This is used only for relocations in
2127 // debugging sections. If we can't find the kept section, return 0.
2129 template<int size
, bool big_endian
>
2130 typename Sized_relobj
<size
, big_endian
>::Address
2131 Sized_relobj
<size
, big_endian
>::map_to_kept_section(
2135 Relobj
* kept_object
;
2136 unsigned int kept_shndx
;
2137 if (this->get_kept_comdat_section(shndx
, &kept_object
, &kept_shndx
))
2139 Sized_relobj
<size
, big_endian
>* kept_relobj
=
2140 static_cast<Sized_relobj
<size
, big_endian
>*>(kept_object
);
2141 Output_section
* os
= kept_relobj
->output_section(kept_shndx
);
2142 Address offset
= kept_relobj
->get_output_section_offset(kept_shndx
);
2143 if (os
!= NULL
&& offset
!= invalid_address
)
2146 return os
->address() + offset
;
2153 // Get symbol counts.
2155 template<int size
, bool big_endian
>
2157 Sized_relobj
<size
, big_endian
>::do_get_global_symbol_counts(
2158 const Symbol_table
*,
2162 *defined
= this->defined_count_
;
2164 for (Symbols::const_iterator p
= this->symbols_
.begin();
2165 p
!= this->symbols_
.end();
2168 && (*p
)->source() == Symbol::FROM_OBJECT
2169 && (*p
)->object() == this
2170 && (*p
)->is_defined())
2175 // Input_objects methods.
2177 // Add a regular relocatable object to the list. Return false if this
2178 // object should be ignored.
2181 Input_objects::add_object(Object
* obj
)
2183 // Print the filename if the -t/--trace option is selected.
2184 if (parameters
->options().trace())
2185 gold_info("%s", obj
->name().c_str());
2187 if (!obj
->is_dynamic())
2188 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
2191 // See if this is a duplicate SONAME.
2192 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
2193 const char* soname
= dynobj
->soname();
2195 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
2196 this->sonames_
.insert(soname
);
2199 // We have already seen a dynamic object with this soname.
2203 this->dynobj_list_
.push_back(dynobj
);
2206 // Add this object to the cross-referencer if requested.
2207 if (parameters
->options().user_set_print_symbol_counts()
2208 || parameters
->options().cref())
2210 if (this->cref_
== NULL
)
2211 this->cref_
= new Cref();
2212 this->cref_
->add_object(obj
);
2218 // For each dynamic object, record whether we've seen all of its
2219 // explicit dependencies.
2222 Input_objects::check_dynamic_dependencies() const
2224 bool issued_copy_dt_needed_error
= false;
2225 for (Dynobj_list::const_iterator p
= this->dynobj_list_
.begin();
2226 p
!= this->dynobj_list_
.end();
2229 const Dynobj::Needed
& needed((*p
)->needed());
2230 bool found_all
= true;
2231 Dynobj::Needed::const_iterator pneeded
;
2232 for (pneeded
= needed
.begin(); pneeded
!= needed
.end(); ++pneeded
)
2234 if (this->sonames_
.find(*pneeded
) == this->sonames_
.end())
2240 (*p
)->set_has_unknown_needed_entries(!found_all
);
2242 // --copy-dt-needed-entries aka --add-needed is a GNU ld option
2243 // that gold does not support. However, they cause no trouble
2244 // unless there is a DT_NEEDED entry that we don't know about;
2245 // warn only in that case.
2247 && !issued_copy_dt_needed_error
2248 && (parameters
->options().copy_dt_needed_entries()
2249 || parameters
->options().add_needed()))
2251 const char* optname
;
2252 if (parameters
->options().copy_dt_needed_entries())
2253 optname
= "--copy-dt-needed-entries";
2255 optname
= "--add-needed";
2256 gold_error(_("%s is not supported but is required for %s in %s"),
2257 optname
, (*pneeded
).c_str(), (*p
)->name().c_str());
2258 issued_copy_dt_needed_error
= true;
2263 // Start processing an archive.
2266 Input_objects::archive_start(Archive
* archive
)
2268 if (parameters
->options().user_set_print_symbol_counts()
2269 || parameters
->options().cref())
2271 if (this->cref_
== NULL
)
2272 this->cref_
= new Cref();
2273 this->cref_
->add_archive_start(archive
);
2277 // Stop processing an archive.
2280 Input_objects::archive_stop(Archive
* archive
)
2282 if (parameters
->options().user_set_print_symbol_counts()
2283 || parameters
->options().cref())
2284 this->cref_
->add_archive_stop(archive
);
2287 // Print symbol counts
2290 Input_objects::print_symbol_counts(const Symbol_table
* symtab
) const
2292 if (parameters
->options().user_set_print_symbol_counts()
2293 && this->cref_
!= NULL
)
2294 this->cref_
->print_symbol_counts(symtab
);
2297 // Print a cross reference table.
2300 Input_objects::print_cref(const Symbol_table
* symtab
, FILE* f
) const
2302 if (parameters
->options().cref() && this->cref_
!= NULL
)
2303 this->cref_
->print_cref(symtab
, f
);
2306 // Relocate_info methods.
2308 // Return a string describing the location of a relocation. This is
2309 // only used in error messages.
2311 template<int size
, bool big_endian
>
2313 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
2315 // See if we can get line-number information from debugging sections.
2316 std::string filename
;
2317 std::string file_and_lineno
; // Better than filename-only, if available.
2319 Sized_dwarf_line_info
<size
, big_endian
> line_info(this->object
);
2320 // This will be "" if we failed to parse the debug info for any reason.
2321 file_and_lineno
= line_info
.addr2line(this->data_shndx
, offset
);
2323 std::string
ret(this->object
->name());
2325 Symbol_location_info info
;
2326 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
2328 ret
+= " in function ";
2329 ret
+= info
.enclosing_symbol_name
;
2331 filename
= info
.source_file
;
2334 if (!file_and_lineno
.empty())
2335 ret
+= file_and_lineno
;
2338 if (!filename
.empty())
2341 ret
+= this->object
->section_name(this->data_shndx
);
2343 // Offsets into sections have to be positive.
2344 snprintf(buf
, sizeof(buf
), "+0x%lx", static_cast<long>(offset
));
2351 } // End namespace gold.
2356 using namespace gold
;
2358 // Read an ELF file with the header and return the appropriate
2359 // instance of Object.
2361 template<int size
, bool big_endian
>
2363 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
2364 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
,
2365 bool* punconfigured
)
2367 Target
* target
= select_target(ehdr
.get_e_machine(), size
, big_endian
,
2368 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
2369 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
2371 gold_fatal(_("%s: unsupported ELF machine number %d"),
2372 name
.c_str(), ehdr
.get_e_machine());
2374 if (!parameters
->target_valid())
2375 set_parameters_target(target
);
2376 else if (target
!= ¶meters
->target())
2378 if (punconfigured
!= NULL
)
2379 *punconfigured
= true;
2381 gold_error(_("%s: incompatible target"), name
.c_str());
2385 return target
->make_elf_object
<size
, big_endian
>(name
, input_file
, offset
,
2389 } // End anonymous namespace.
2394 // Return whether INPUT_FILE is an ELF object.
2397 is_elf_object(Input_file
* input_file
, off_t offset
,
2398 const unsigned char** start
, int *read_size
)
2400 off_t filesize
= input_file
->file().filesize();
2401 int want
= elfcpp::Elf_recognizer::max_header_size
;
2402 if (filesize
- offset
< want
)
2403 want
= filesize
- offset
;
2405 const unsigned char* p
= input_file
->file().get_view(offset
, 0, want
,
2410 return elfcpp::Elf_recognizer::is_elf_file(p
, want
);
2413 // Read an ELF file and return the appropriate instance of Object.
2416 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
2417 const unsigned char* p
, section_offset_type bytes
,
2418 bool* punconfigured
)
2420 if (punconfigured
!= NULL
)
2421 *punconfigured
= false;
2424 bool big_endian
= false;
2426 if (!elfcpp::Elf_recognizer::is_valid_header(p
, bytes
, &size
,
2427 &big_endian
, &error
))
2429 gold_error(_("%s: %s"), name
.c_str(), error
.c_str());
2437 #ifdef HAVE_TARGET_32_BIG
2438 elfcpp::Ehdr
<32, true> ehdr(p
);
2439 return make_elf_sized_object
<32, true>(name
, input_file
,
2440 offset
, ehdr
, punconfigured
);
2442 if (punconfigured
!= NULL
)
2443 *punconfigured
= true;
2445 gold_error(_("%s: not configured to support "
2446 "32-bit big-endian object"),
2453 #ifdef HAVE_TARGET_32_LITTLE
2454 elfcpp::Ehdr
<32, false> ehdr(p
);
2455 return make_elf_sized_object
<32, false>(name
, input_file
,
2456 offset
, ehdr
, punconfigured
);
2458 if (punconfigured
!= NULL
)
2459 *punconfigured
= true;
2461 gold_error(_("%s: not configured to support "
2462 "32-bit little-endian object"),
2468 else if (size
== 64)
2472 #ifdef HAVE_TARGET_64_BIG
2473 elfcpp::Ehdr
<64, true> ehdr(p
);
2474 return make_elf_sized_object
<64, true>(name
, input_file
,
2475 offset
, ehdr
, punconfigured
);
2477 if (punconfigured
!= NULL
)
2478 *punconfigured
= true;
2480 gold_error(_("%s: not configured to support "
2481 "64-bit big-endian object"),
2488 #ifdef HAVE_TARGET_64_LITTLE
2489 elfcpp::Ehdr
<64, false> ehdr(p
);
2490 return make_elf_sized_object
<64, false>(name
, input_file
,
2491 offset
, ehdr
, punconfigured
);
2493 if (punconfigured
!= NULL
)
2494 *punconfigured
= true;
2496 gold_error(_("%s: not configured to support "
2497 "64-bit little-endian object"),
2507 // Instantiate the templates we need.
2509 #ifdef HAVE_TARGET_32_LITTLE
2512 Object::read_section_data
<32, false>(elfcpp::Elf_file
<32, false, Object
>*,
2513 Read_symbols_data
*);
2516 #ifdef HAVE_TARGET_32_BIG
2519 Object::read_section_data
<32, true>(elfcpp::Elf_file
<32, true, Object
>*,
2520 Read_symbols_data
*);
2523 #ifdef HAVE_TARGET_64_LITTLE
2526 Object::read_section_data
<64, false>(elfcpp::Elf_file
<64, false, Object
>*,
2527 Read_symbols_data
*);
2530 #ifdef HAVE_TARGET_64_BIG
2533 Object::read_section_data
<64, true>(elfcpp::Elf_file
<64, true, Object
>*,
2534 Read_symbols_data
*);
2537 #ifdef HAVE_TARGET_32_LITTLE
2539 class Sized_relobj
<32, false>;
2542 #ifdef HAVE_TARGET_32_BIG
2544 class Sized_relobj
<32, true>;
2547 #ifdef HAVE_TARGET_64_LITTLE
2549 class Sized_relobj
<64, false>;
2552 #ifdef HAVE_TARGET_64_BIG
2554 class Sized_relobj
<64, true>;
2557 #ifdef HAVE_TARGET_32_LITTLE
2559 struct Relocate_info
<32, false>;
2562 #ifdef HAVE_TARGET_32_BIG
2564 struct Relocate_info
<32, true>;
2567 #ifdef HAVE_TARGET_64_LITTLE
2569 struct Relocate_info
<64, false>;
2572 #ifdef HAVE_TARGET_64_BIG
2574 struct Relocate_info
<64, true>;
2577 #ifdef HAVE_TARGET_32_LITTLE
2580 Xindex::initialize_symtab_xindex
<32, false>(Object
*, unsigned int);
2584 Xindex::read_symtab_xindex
<32, false>(Object
*, unsigned int,
2585 const unsigned char*);
2588 #ifdef HAVE_TARGET_32_BIG
2591 Xindex::initialize_symtab_xindex
<32, true>(Object
*, unsigned int);
2595 Xindex::read_symtab_xindex
<32, true>(Object
*, unsigned int,
2596 const unsigned char*);
2599 #ifdef HAVE_TARGET_64_LITTLE
2602 Xindex::initialize_symtab_xindex
<64, false>(Object
*, unsigned int);
2606 Xindex::read_symtab_xindex
<64, false>(Object
*, unsigned int,
2607 const unsigned char*);
2610 #ifdef HAVE_TARGET_64_BIG
2613 Xindex::initialize_symtab_xindex
<64, true>(Object
*, unsigned int);
2617 Xindex::read_symtab_xindex
<64, true>(Object
*, unsigned int,
2618 const unsigned char*);
2621 } // End namespace gold.