1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "libiberty.h"
31 #include "target-select.h"
32 #include "dwarf_reader.h"
47 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
48 // section and read it in. SYMTAB_SHNDX is the index of the symbol
49 // table we care about.
51 template<int size
, bool big_endian
>
53 Xindex::initialize_symtab_xindex(Object
* object
, unsigned int symtab_shndx
)
55 if (!this->symtab_xindex_
.empty())
58 gold_assert(symtab_shndx
!= 0);
60 // Look through the sections in reverse order, on the theory that it
61 // is more likely to be near the end than the beginning.
62 unsigned int i
= object
->shnum();
66 if (object
->section_type(i
) == elfcpp::SHT_SYMTAB_SHNDX
67 && this->adjust_shndx(object
->section_link(i
)) == symtab_shndx
)
69 this->read_symtab_xindex
<size
, big_endian
>(object
, i
, NULL
);
74 object
->error(_("missing SHT_SYMTAB_SHNDX section"));
77 // Read in the symtab_xindex_ array, given the section index of the
78 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
81 template<int size
, bool big_endian
>
83 Xindex::read_symtab_xindex(Object
* object
, unsigned int xindex_shndx
,
84 const unsigned char* pshdrs
)
86 section_size_type bytecount
;
87 const unsigned char* contents
;
89 contents
= object
->section_contents(xindex_shndx
, &bytecount
, false);
92 const unsigned char* p
= (pshdrs
94 * elfcpp::Elf_sizes
<size
>::shdr_size
));
95 typename
elfcpp::Shdr
<size
, big_endian
> shdr(p
);
96 bytecount
= convert_to_section_size_type(shdr
.get_sh_size());
97 contents
= object
->get_view(shdr
.get_sh_offset(), bytecount
, true, false);
100 gold_assert(this->symtab_xindex_
.empty());
101 this->symtab_xindex_
.reserve(bytecount
/ 4);
102 for (section_size_type i
= 0; i
< bytecount
; i
+= 4)
104 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
105 // We preadjust the section indexes we save.
106 this->symtab_xindex_
.push_back(this->adjust_shndx(shndx
));
110 // Symbol symndx has a section of SHN_XINDEX; return the real section
114 Xindex::sym_xindex_to_shndx(Object
* object
, unsigned int symndx
)
116 if (symndx
>= this->symtab_xindex_
.size())
118 object
->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
120 return elfcpp::SHN_UNDEF
;
122 unsigned int shndx
= this->symtab_xindex_
[symndx
];
123 if (shndx
< elfcpp::SHN_LORESERVE
|| shndx
>= object
->shnum())
125 object
->error(_("extended index for symbol %u out of range: %u"),
127 return elfcpp::SHN_UNDEF
;
134 // Set the target based on fields in the ELF file header.
137 Object::set_target(int machine
, int size
, bool big_endian
, int osabi
,
140 Target
* target
= select_target(machine
, size
, big_endian
, osabi
, abiversion
);
142 gold_fatal(_("%s: unsupported ELF machine number %d"),
143 this->name().c_str(), machine
);
144 this->target_
= target
;
147 // Report an error for this object file. This is used by the
148 // elfcpp::Elf_file interface, and also called by the Object code
152 Object::error(const char* format
, ...) const
155 va_start(args
, format
);
157 if (vasprintf(&buf
, format
, args
) < 0)
160 gold_error(_("%s: %s"), this->name().c_str(), buf
);
164 // Return a view of the contents of a section.
167 Object::section_contents(unsigned int shndx
, section_size_type
* plen
,
170 Location
loc(this->do_section_contents(shndx
));
171 *plen
= convert_to_section_size_type(loc
.data_size
);
172 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
175 // Read the section data into SD. This is code common to Sized_relobj
176 // and Sized_dynobj, so we put it into Object.
178 template<int size
, bool big_endian
>
180 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
181 Read_symbols_data
* sd
)
183 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
185 // Read the section headers.
186 const off_t shoff
= elf_file
->shoff();
187 const unsigned int shnum
= this->shnum();
188 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
,
191 // Read the section names.
192 const unsigned char* pshdrs
= sd
->section_headers
->data();
193 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
194 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
196 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
197 this->error(_("section name section has wrong type: %u"),
198 static_cast<unsigned int>(shdrnames
.get_sh_type()));
200 sd
->section_names_size
=
201 convert_to_section_size_type(shdrnames
.get_sh_size());
202 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
203 sd
->section_names_size
, false,
207 // If NAME is the name of a special .gnu.warning section, arrange for
208 // the warning to be issued. SHNDX is the section index. Return
209 // whether it is a warning section.
212 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
213 Symbol_table
* symtab
)
215 const char warn_prefix
[] = ".gnu.warning.";
216 const int warn_prefix_len
= sizeof warn_prefix
- 1;
217 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
219 // Read the section contents to get the warning text. It would
220 // be nicer if we only did this if we have to actually issue a
221 // warning. Unfortunately, warnings are issued as we relocate
222 // sections. That means that we can not lock the object then,
223 // as we might try to issue the same warning multiple times
225 section_size_type len
;
226 const unsigned char* contents
= this->section_contents(shndx
, &len
,
228 std::string
warning(reinterpret_cast<const char*>(contents
), len
);
229 symtab
->add_warning(name
+ warn_prefix_len
, this, warning
);
235 // Class Sized_relobj.
237 template<int size
, bool big_endian
>
238 Sized_relobj
<size
, big_endian
>::Sized_relobj(
239 const std::string
& name
,
240 Input_file
* input_file
,
242 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
243 : Relobj(name
, input_file
, offset
),
244 elf_file_(this, ehdr
),
246 local_symbol_count_(0),
247 output_local_symbol_count_(0),
248 output_local_dynsym_count_(0),
251 local_symbol_offset_(0),
252 local_dynsym_offset_(0),
254 local_got_offsets_(),
255 kept_comdat_sections_(),
261 template<int size
, bool big_endian
>
262 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
266 // Set up an object file based on the file header. This sets up the
267 // target and reads the section information.
269 template<int size
, bool big_endian
>
271 Sized_relobj
<size
, big_endian
>::setup(
272 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
274 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
275 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
276 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
278 const unsigned int shnum
= this->elf_file_
.shnum();
279 this->set_shnum(shnum
);
282 // Find the SHT_SYMTAB section, given the section headers. The ELF
283 // standard says that maybe in the future there can be more than one
284 // SHT_SYMTAB section. Until somebody figures out how that could
285 // work, we assume there is only one.
287 template<int size
, bool big_endian
>
289 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
291 const unsigned int shnum
= this->shnum();
292 this->symtab_shndx_
= 0;
295 // Look through the sections in reverse order, since gas tends
296 // to put the symbol table at the end.
297 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
298 unsigned int i
= shnum
;
299 unsigned int xindex_shndx
= 0;
300 unsigned int xindex_link
= 0;
304 p
-= This::shdr_size
;
305 typename
This::Shdr
shdr(p
);
306 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
308 this->symtab_shndx_
= i
;
309 if (xindex_shndx
> 0 && xindex_link
== i
)
312 new Xindex(this->elf_file_
.large_shndx_offset());
313 xindex
->read_symtab_xindex
<size
, big_endian
>(this,
316 this->set_xindex(xindex
);
321 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
322 // one. This will work if it follows the SHT_SYMTAB
324 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX
)
327 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
333 // Return the Xindex structure to use for object with lots of
336 template<int size
, bool big_endian
>
338 Sized_relobj
<size
, big_endian
>::do_initialize_xindex()
340 gold_assert(this->symtab_shndx_
!= -1U);
341 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
342 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->symtab_shndx_
);
346 // Return whether SHDR has the right type and flags to be a GNU
347 // .eh_frame section.
349 template<int size
, bool big_endian
>
351 Sized_relobj
<size
, big_endian
>::check_eh_frame_flags(
352 const elfcpp::Shdr
<size
, big_endian
>* shdr
) const
354 return (shdr
->get_sh_type() == elfcpp::SHT_PROGBITS
355 && (shdr
->get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
358 // Return whether there is a GNU .eh_frame section, given the section
359 // headers and the section names.
361 template<int size
, bool big_endian
>
363 Sized_relobj
<size
, big_endian
>::find_eh_frame(
364 const unsigned char* pshdrs
,
366 section_size_type names_size
) const
368 const unsigned int shnum
= this->shnum();
369 const unsigned char* p
= pshdrs
+ This::shdr_size
;
370 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
372 typename
This::Shdr
shdr(p
);
373 if (this->check_eh_frame_flags(&shdr
))
375 if (shdr
.get_sh_name() >= names_size
)
377 this->error(_("bad section name offset for section %u: %lu"),
378 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
382 const char* name
= names
+ shdr
.get_sh_name();
383 if (strcmp(name
, ".eh_frame") == 0)
390 // Read the sections and symbols from an object file.
392 template<int size
, bool big_endian
>
394 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
396 this->read_section_data(&this->elf_file_
, sd
);
398 const unsigned char* const pshdrs
= sd
->section_headers
->data();
400 this->find_symtab(pshdrs
);
402 const unsigned char* namesu
= sd
->section_names
->data();
403 const char* names
= reinterpret_cast<const char*>(namesu
);
404 if (memmem(names
, sd
->section_names_size
, ".eh_frame", 10) != NULL
)
406 if (this->find_eh_frame(pshdrs
, names
, sd
->section_names_size
))
407 this->has_eh_frame_
= true;
411 sd
->symbols_size
= 0;
412 sd
->external_symbols_offset
= 0;
413 sd
->symbol_names
= NULL
;
414 sd
->symbol_names_size
= 0;
416 if (this->symtab_shndx_
== 0)
418 // No symbol table. Weird but legal.
422 // Get the symbol table section header.
423 typename
This::Shdr
symtabshdr(pshdrs
424 + this->symtab_shndx_
* This::shdr_size
);
425 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
427 // If this object has a .eh_frame section, we need all the symbols.
428 // Otherwise we only need the external symbols. While it would be
429 // simpler to just always read all the symbols, I've seen object
430 // files with well over 2000 local symbols, which for a 64-bit
431 // object file format is over 5 pages that we don't need to read
434 const int sym_size
= This::sym_size
;
435 const unsigned int loccount
= symtabshdr
.get_sh_info();
436 this->local_symbol_count_
= loccount
;
437 this->local_values_
.resize(loccount
);
438 section_offset_type locsize
= loccount
* sym_size
;
439 off_t dataoff
= symtabshdr
.get_sh_offset();
440 section_size_type datasize
=
441 convert_to_section_size_type(symtabshdr
.get_sh_size());
442 off_t extoff
= dataoff
+ locsize
;
443 section_size_type extsize
= datasize
- locsize
;
445 off_t readoff
= this->has_eh_frame_
? dataoff
: extoff
;
446 section_size_type readsize
= this->has_eh_frame_
? datasize
: extsize
;
450 // No external symbols. Also weird but also legal.
454 File_view
* fvsymtab
= this->get_lasting_view(readoff
, readsize
, true, false);
456 // Read the section header for the symbol names.
457 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
458 if (strtab_shndx
>= this->shnum())
460 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
463 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
464 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
466 this->error(_("symbol table name section has wrong type: %u"),
467 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
471 // Read the symbol names.
472 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
473 strtabshdr
.get_sh_size(),
476 sd
->symbols
= fvsymtab
;
477 sd
->symbols_size
= readsize
;
478 sd
->external_symbols_offset
= this->has_eh_frame_
? locsize
: 0;
479 sd
->symbol_names
= fvstrtab
;
480 sd
->symbol_names_size
=
481 convert_to_section_size_type(strtabshdr
.get_sh_size());
484 // Return the section index of symbol SYM. Set *VALUE to its value in
485 // the object file. Set *IS_ORDINARY if this is an ordinary section
486 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
487 // Note that for a symbol which is not defined in this object file,
488 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
489 // the final value of the symbol in the link.
491 template<int size
, bool big_endian
>
493 Sized_relobj
<size
, big_endian
>::symbol_section_and_value(unsigned int sym
,
497 section_size_type symbols_size
;
498 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
502 const size_t count
= symbols_size
/ This::sym_size
;
503 gold_assert(sym
< count
);
505 elfcpp::Sym
<size
, big_endian
> elfsym(symbols
+ sym
* This::sym_size
);
506 *value
= elfsym
.get_st_value();
508 return this->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
511 // Return whether to include a section group in the link. LAYOUT is
512 // used to keep track of which section groups we have already seen.
513 // INDEX is the index of the section group and SHDR is the section
514 // header. If we do not want to include this group, we set bits in
515 // OMIT for each section which should be discarded.
517 template<int size
, bool big_endian
>
519 Sized_relobj
<size
, big_endian
>::include_section_group(
520 Symbol_table
* symtab
,
524 const unsigned char* shdrs
,
525 const char* section_names
,
526 section_size_type section_names_size
,
527 std::vector
<bool>* omit
)
529 // Read the section contents.
530 typename
This::Shdr
shdr(shdrs
+ index
* This::shdr_size
);
531 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
532 shdr
.get_sh_size(), true, false);
533 const elfcpp::Elf_Word
* pword
=
534 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
536 // The first word contains flags. We only care about COMDAT section
537 // groups. Other section groups are always included in the link
538 // just like ordinary sections.
539 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
541 // Look up the group signature, which is the name of a symbol. This
542 // is a lot of effort to go to to read a string. Why didn't they
543 // just have the group signature point into the string table, rather
544 // than indirect through a symbol?
546 // Get the appropriate symbol table header (this will normally be
547 // the single SHT_SYMTAB section, but in principle it need not be).
548 const unsigned int link
= this->adjust_shndx(shdr
.get_sh_link());
549 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
551 // Read the symbol table entry.
552 unsigned int symndx
= shdr
.get_sh_info();
553 if (symndx
>= symshdr
.get_sh_size() / This::sym_size
)
555 this->error(_("section group %u info %u out of range"),
559 off_t symoff
= symshdr
.get_sh_offset() + symndx
* This::sym_size
;
560 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true,
562 elfcpp::Sym
<size
, big_endian
> sym(psym
);
564 // Read the symbol table names.
565 section_size_type symnamelen
;
566 const unsigned char* psymnamesu
;
567 psymnamesu
= this->section_contents(this->adjust_shndx(symshdr
.get_sh_link()),
569 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
571 // Get the section group signature.
572 if (sym
.get_st_name() >= symnamelen
)
574 this->error(_("symbol %u name offset %u out of range"),
575 symndx
, sym
.get_st_name());
579 std::string
signature(psymnames
+ sym
.get_st_name());
581 // It seems that some versions of gas will create a section group
582 // associated with a section symbol, and then fail to give a name to
583 // the section symbol. In such a case, use the name of the section.
584 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
587 unsigned int sym_shndx
= this->adjust_sym_shndx(symndx
,
590 if (!is_ordinary
|| sym_shndx
>= this->shnum())
592 this->error(_("symbol %u invalid section index %u"),
596 typename
This::Shdr
member_shdr(shdrs
+ sym_shndx
* This::shdr_size
);
597 if (member_shdr
.get_sh_name() < section_names_size
)
598 signature
= section_names
+ member_shdr
.get_sh_name();
601 // Record this section group in the layout, and see whether we've already
602 // seen one with the same signature.
603 bool include_group
= ((flags
& elfcpp::GRP_COMDAT
) == 0
604 || layout
->add_comdat(this, index
, signature
, true));
606 Sized_relobj
<size
, big_endian
>* kept_object
= NULL
;
607 Comdat_group
* kept_group
= NULL
;
611 // This group is being discarded. Find the object and group
612 // that was kept in its place.
613 unsigned int kept_group_index
= 0;
614 Relobj
* kept_relobj
= layout
->find_kept_object(signature
,
616 kept_object
= static_cast<Sized_relobj
<size
, big_endian
>*>(kept_relobj
);
617 if (kept_object
!= NULL
)
618 kept_group
= kept_object
->find_comdat_group(kept_group_index
);
620 else if (flags
& elfcpp::GRP_COMDAT
)
622 // This group is being kept. Create the table to map section names
623 // to section indexes and add it to the table of groups.
624 kept_group
= new Comdat_group();
625 this->add_comdat_group(index
, kept_group
);
628 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
630 std::vector
<unsigned int> shndxes
;
631 bool relocate_group
= include_group
&& parameters
->options().relocatable();
633 shndxes
.reserve(count
- 1);
635 for (size_t i
= 1; i
< count
; ++i
)
637 elfcpp::Elf_Word secnum
=
638 this->adjust_shndx(elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
));
641 shndxes
.push_back(secnum
);
643 if (secnum
>= this->shnum())
645 this->error(_("section %u in section group %u out of range"),
650 // Check for an earlier section number, since we're going to get
651 // it wrong--we may have already decided to include the section.
653 this->error(_("invalid section group %u refers to earlier section %u"),
656 // Get the name of the member section.
657 typename
This::Shdr
member_shdr(shdrs
+ secnum
* This::shdr_size
);
658 if (member_shdr
.get_sh_name() >= section_names_size
)
660 // This is an error, but it will be diagnosed eventually
661 // in do_layout, so we don't need to do anything here but
665 std::string
mname(section_names
+ member_shdr
.get_sh_name());
669 (*omit
)[secnum
] = true;
670 if (kept_group
!= NULL
)
672 // Find the corresponding kept section, and store that info
673 // in the discarded section table.
674 Comdat_group::const_iterator p
= kept_group
->find(mname
);
675 if (p
!= kept_group
->end())
677 Kept_comdat_section
* kept
=
678 new Kept_comdat_section(kept_object
, p
->second
);
679 this->set_kept_comdat_section(secnum
, kept
);
683 else if (flags
& elfcpp::GRP_COMDAT
)
685 // Add the section to the kept group table.
686 gold_assert(kept_group
!= NULL
);
687 kept_group
->insert(std::make_pair(mname
, secnum
));
692 layout
->layout_group(symtab
, this, index
, name
, signature
.c_str(),
693 shdr
, flags
, &shndxes
);
695 return include_group
;
698 // Whether to include a linkonce section in the link. NAME is the
699 // name of the section and SHDR is the section header.
701 // Linkonce sections are a GNU extension implemented in the original
702 // GNU linker before section groups were defined. The semantics are
703 // that we only include one linkonce section with a given name. The
704 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
705 // where T is the type of section and SYMNAME is the name of a symbol.
706 // In an attempt to make linkonce sections interact well with section
707 // groups, we try to identify SYMNAME and use it like a section group
708 // signature. We want to block section groups with that signature,
709 // but not other linkonce sections with that signature. We also use
710 // the full name of the linkonce section as a normal section group
713 template<int size
, bool big_endian
>
715 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
719 const elfcpp::Shdr
<size
, big_endian
>&)
721 // In general the symbol name we want will be the string following
722 // the last '.'. However, we have to handle the case of
723 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
724 // some versions of gcc. So we use a heuristic: if the name starts
725 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
726 // we look for the last '.'. We can't always simply skip
727 // ".gnu.linkonce.X", because we have to deal with cases like
728 // ".gnu.linkonce.d.rel.ro.local".
729 const char* const linkonce_t
= ".gnu.linkonce.t.";
731 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
732 symname
= name
+ strlen(linkonce_t
);
734 symname
= strrchr(name
, '.') + 1;
735 std::string
sig1(symname
);
736 std::string
sig2(name
);
737 bool include1
= layout
->add_comdat(this, index
, sig1
, false);
738 bool include2
= layout
->add_comdat(this, index
, sig2
, true);
742 // The section is being discarded on the basis of its section
743 // name (i.e., the kept section was also a linkonce section).
744 // In this case, the section index stored with the layout object
745 // is the linkonce section that was kept.
746 unsigned int kept_group_index
= 0;
747 Relobj
* kept_relobj
= layout
->find_kept_object(sig2
, &kept_group_index
);
748 if (kept_relobj
!= NULL
)
750 Sized_relobj
<size
, big_endian
>* kept_object
751 = static_cast<Sized_relobj
<size
, big_endian
>*>(kept_relobj
);
752 Kept_comdat_section
* kept
=
753 new Kept_comdat_section(kept_object
, kept_group_index
);
754 this->set_kept_comdat_section(index
, kept
);
759 // The section is being discarded on the basis of its symbol
760 // name. This means that the corresponding kept section was
761 // part of a comdat group, and it will be difficult to identify
762 // the specific section within that group that corresponds to
763 // this linkonce section. We'll handle the simple case where
764 // the group has only one member section. Otherwise, it's not
766 unsigned int kept_group_index
= 0;
767 Relobj
* kept_relobj
= layout
->find_kept_object(sig1
, &kept_group_index
);
768 if (kept_relobj
!= NULL
)
770 Sized_relobj
<size
, big_endian
>* kept_object
=
771 static_cast<Sized_relobj
<size
, big_endian
>*>(kept_relobj
);
772 Comdat_group
* kept_group
=
773 kept_object
->find_comdat_group(kept_group_index
);
774 if (kept_group
!= NULL
&& kept_group
->size() == 1)
776 Comdat_group::const_iterator p
= kept_group
->begin();
777 gold_assert(p
!= kept_group
->end());
778 Kept_comdat_section
* kept
=
779 new Kept_comdat_section(kept_object
, p
->second
);
780 this->set_kept_comdat_section(index
, kept
);
785 return include1
&& include2
;
788 // Layout an input section.
790 template<int size
, bool big_endian
>
792 Sized_relobj
<size
, big_endian
>::layout_section(Layout
* layout
,
795 typename
This::Shdr
& shdr
,
796 unsigned int reloc_shndx
,
797 unsigned int reloc_type
)
800 Output_section
* os
= layout
->layout(this, shndx
, name
, shdr
,
801 reloc_shndx
, reloc_type
, &offset
);
803 this->output_sections()[shndx
] = os
;
805 this->section_offsets_
[shndx
] = invalid_address
;
807 this->section_offsets_
[shndx
] = convert_types
<Address
, off_t
>(offset
);
809 // If this section requires special handling, and if there are
810 // relocs that apply to it, then we must do the special handling
811 // before we apply the relocs.
812 if (offset
== -1 && reloc_shndx
!= 0)
813 this->set_relocs_must_follow_section_writes();
816 // Lay out the input sections. We walk through the sections and check
817 // whether they should be included in the link. If they should, we
818 // pass them to the Layout object, which will return an output section
821 template<int size
, bool big_endian
>
823 Sized_relobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
825 Read_symbols_data
* sd
)
827 const unsigned int shnum
= this->shnum();
831 // Get the section headers.
832 const unsigned char* shdrs
= sd
->section_headers
->data();
833 const unsigned char* pshdrs
;
835 // Get the section names.
836 const unsigned char* pnamesu
= sd
->section_names
->data();
837 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
839 // If any input files have been claimed by plugins, we need to defer
840 // actual layout until the replacement files have arrived.
841 const bool should_defer_layout
=
842 (parameters
->options().has_plugins()
843 && parameters
->options().plugins()->should_defer_layout());
844 unsigned int num_sections_to_defer
= 0;
846 // For each section, record the index of the reloc section if any.
847 // Use 0 to mean that there is no reloc section, -1U to mean that
848 // there is more than one.
849 std::vector
<unsigned int> reloc_shndx(shnum
, 0);
850 std::vector
<unsigned int> reloc_type(shnum
, elfcpp::SHT_NULL
);
851 // Skip the first, dummy, section.
852 pshdrs
= shdrs
+ This::shdr_size
;
853 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
855 typename
This::Shdr
shdr(pshdrs
);
857 // Count the number of sections whose layout will be deferred.
858 if (should_defer_layout
&& (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
859 ++num_sections_to_defer
;
861 unsigned int sh_type
= shdr
.get_sh_type();
862 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
864 unsigned int target_shndx
= this->adjust_shndx(shdr
.get_sh_info());
865 if (target_shndx
== 0 || target_shndx
>= shnum
)
867 this->error(_("relocation section %u has bad info %u"),
872 if (reloc_shndx
[target_shndx
] != 0)
873 reloc_shndx
[target_shndx
] = -1U;
876 reloc_shndx
[target_shndx
] = i
;
877 reloc_type
[target_shndx
] = sh_type
;
882 Output_sections
& out_sections(this->output_sections());
883 std::vector
<Address
>& out_section_offsets(this->section_offsets_
);
885 out_sections
.resize(shnum
);
886 out_section_offsets
.resize(shnum
);
888 // If we are only linking for symbols, then there is nothing else to
890 if (this->input_file()->just_symbols())
892 delete sd
->section_headers
;
893 sd
->section_headers
= NULL
;
894 delete sd
->section_names
;
895 sd
->section_names
= NULL
;
899 if (num_sections_to_defer
> 0)
901 parameters
->options().plugins()->add_deferred_layout_object(this);
902 this->deferred_layout_
.reserve(num_sections_to_defer
);
905 // Whether we've seen a .note.GNU-stack section.
906 bool seen_gnu_stack
= false;
907 // The flags of a .note.GNU-stack section.
908 uint64_t gnu_stack_flags
= 0;
910 // Keep track of which sections to omit.
911 std::vector
<bool> omit(shnum
, false);
913 // Keep track of reloc sections when emitting relocations.
914 const bool relocatable
= parameters
->options().relocatable();
915 const bool emit_relocs
= (relocatable
916 || parameters
->options().emit_relocs());
917 std::vector
<unsigned int> reloc_sections
;
919 // Keep track of .eh_frame sections.
920 std::vector
<unsigned int> eh_frame_sections
;
922 // Skip the first, dummy, section.
923 pshdrs
= shdrs
+ This::shdr_size
;
924 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
926 typename
This::Shdr
shdr(pshdrs
);
928 if (shdr
.get_sh_name() >= sd
->section_names_size
)
930 this->error(_("bad section name offset for section %u: %lu"),
931 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
935 const char* name
= pnames
+ shdr
.get_sh_name();
937 if (this->handle_gnu_warning_section(name
, i
, symtab
))
943 // The .note.GNU-stack section is special. It gives the
944 // protection flags that this object file requires for the stack
946 if (strcmp(name
, ".note.GNU-stack") == 0)
948 seen_gnu_stack
= true;
949 gnu_stack_flags
|= shdr
.get_sh_flags();
953 bool discard
= omit
[i
];
956 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
958 if (!this->include_section_group(symtab
, layout
, i
, name
, shdrs
,
959 pnames
, sd
->section_names_size
,
963 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
964 && Layout::is_linkonce(name
))
966 if (!this->include_linkonce_section(layout
, i
, name
, shdr
))
973 // Do not include this section in the link.
974 out_sections
[i
] = NULL
;
975 out_section_offsets
[i
] = invalid_address
;
979 // When doing a relocatable link we are going to copy input
980 // reloc sections into the output. We only want to copy the
981 // ones associated with sections which are not being discarded.
982 // However, we don't know that yet for all sections. So save
983 // reloc sections and process them later.
985 && (shdr
.get_sh_type() == elfcpp::SHT_REL
986 || shdr
.get_sh_type() == elfcpp::SHT_RELA
))
988 reloc_sections
.push_back(i
);
992 if (relocatable
&& shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
995 // The .eh_frame section is special. It holds exception frame
996 // information that we need to read in order to generate the
997 // exception frame header. We process these after all the other
998 // sections so that the exception frame reader can reliably
999 // determine which sections are being discarded, and discard the
1000 // corresponding information.
1002 && strcmp(name
, ".eh_frame") == 0
1003 && this->check_eh_frame_flags(&shdr
))
1005 eh_frame_sections
.push_back(i
);
1009 if (should_defer_layout
&& (shdr
.get_sh_flags() & elfcpp::SHF_ALLOC
))
1011 this->deferred_layout_
.push_back(Deferred_layout(i
, name
, pshdrs
,
1015 // Put dummy values here; real values will be supplied by
1016 // do_layout_deferred_sections.
1017 out_sections
[i
] = reinterpret_cast<Output_section
*>(1);
1018 out_section_offsets
[i
] = invalid_address
;
1022 this->layout_section(layout
, i
, name
, shdr
, reloc_shndx
[i
],
1027 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
);
1029 // When doing a relocatable link handle the reloc sections at the
1032 this->size_relocatable_relocs();
1033 for (std::vector
<unsigned int>::const_iterator p
= reloc_sections
.begin();
1034 p
!= reloc_sections
.end();
1037 unsigned int i
= *p
;
1038 const unsigned char* pshdr
;
1039 pshdr
= sd
->section_headers
->data() + i
* This::shdr_size
;
1040 typename
This::Shdr
shdr(pshdr
);
1042 unsigned int data_shndx
= this->adjust_shndx(shdr
.get_sh_info());
1043 if (data_shndx
>= shnum
)
1045 // We already warned about this above.
1049 Output_section
* data_section
= out_sections
[data_shndx
];
1050 if (data_section
== NULL
)
1052 out_sections
[i
] = NULL
;
1053 out_section_offsets
[i
] = invalid_address
;
1057 Relocatable_relocs
* rr
= new Relocatable_relocs();
1058 this->set_relocatable_relocs(i
, rr
);
1060 Output_section
* os
= layout
->layout_reloc(this, i
, shdr
, data_section
,
1062 out_sections
[i
] = os
;
1063 out_section_offsets
[i
] = invalid_address
;
1066 // Handle the .eh_frame sections at the end.
1067 for (std::vector
<unsigned int>::const_iterator p
= eh_frame_sections
.begin();
1068 p
!= eh_frame_sections
.end();
1071 gold_assert(this->has_eh_frame_
);
1072 gold_assert(sd
->external_symbols_offset
!= 0);
1074 unsigned int i
= *p
;
1075 const unsigned char *pshdr
;
1076 pshdr
= sd
->section_headers
->data() + i
* This::shdr_size
;
1077 typename
This::Shdr
shdr(pshdr
);
1080 Output_section
* os
= layout
->layout_eh_frame(this,
1081 sd
->symbols
->data(),
1083 sd
->symbol_names
->data(),
1084 sd
->symbol_names_size
,
1089 out_sections
[i
] = os
;
1091 out_section_offsets
[i
] = invalid_address
;
1093 out_section_offsets
[i
] = convert_types
<Address
, off_t
>(offset
);
1095 // If this section requires special handling, and if there are
1096 // relocs that apply to it, then we must do the special handling
1097 // before we apply the relocs.
1098 if (offset
== -1 && reloc_shndx
[i
] != 0)
1099 this->set_relocs_must_follow_section_writes();
1102 delete sd
->section_headers
;
1103 sd
->section_headers
= NULL
;
1104 delete sd
->section_names
;
1105 sd
->section_names
= NULL
;
1108 // Layout sections whose layout was deferred while waiting for
1109 // input files from a plugin.
1111 template<int size
, bool big_endian
>
1113 Sized_relobj
<size
, big_endian
>::do_layout_deferred_sections(Layout
* layout
)
1115 typename
std::vector
<Deferred_layout
>::iterator deferred
;
1117 for (deferred
= this->deferred_layout_
.begin();
1118 deferred
!= this->deferred_layout_
.end();
1121 typename
This::Shdr
shdr(deferred
->shdr_data_
);
1122 this->layout_section(layout
, deferred
->shndx_
, deferred
->name_
.c_str(),
1123 shdr
, deferred
->reloc_shndx_
, deferred
->reloc_type_
);
1126 this->deferred_layout_
.clear();
1129 // Add the symbols to the symbol table.
1131 template<int size
, bool big_endian
>
1133 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1134 Read_symbols_data
* sd
)
1136 if (sd
->symbols
== NULL
)
1138 gold_assert(sd
->symbol_names
== NULL
);
1142 const int sym_size
= This::sym_size
;
1143 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
1145 if (symcount
* sym_size
!= sd
->symbols_size
- sd
->external_symbols_offset
)
1147 this->error(_("size of symbols is not multiple of symbol size"));
1151 this->symbols_
.resize(symcount
);
1153 const char* sym_names
=
1154 reinterpret_cast<const char*>(sd
->symbol_names
->data());
1155 symtab
->add_from_relobj(this,
1156 sd
->symbols
->data() + sd
->external_symbols_offset
,
1157 symcount
, this->local_symbol_count_
,
1158 sym_names
, sd
->symbol_names_size
,
1160 &this->defined_count_
);
1164 delete sd
->symbol_names
;
1165 sd
->symbol_names
= NULL
;
1168 // First pass over the local symbols. Here we add their names to
1169 // *POOL and *DYNPOOL, and we store the symbol value in
1170 // THIS->LOCAL_VALUES_. This function is always called from a
1171 // singleton thread. This is followed by a call to
1172 // finalize_local_symbols.
1174 template<int size
, bool big_endian
>
1176 Sized_relobj
<size
, big_endian
>::do_count_local_symbols(Stringpool
* pool
,
1177 Stringpool
* dynpool
)
1179 gold_assert(this->symtab_shndx_
!= -1U);
1180 if (this->symtab_shndx_
== 0)
1182 // This object has no symbols. Weird but legal.
1186 // Read the symbol table section header.
1187 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1188 typename
This::Shdr
symtabshdr(this,
1189 this->elf_file_
.section_header(symtab_shndx
));
1190 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1192 // Read the local symbols.
1193 const int sym_size
= This::sym_size
;
1194 const unsigned int loccount
= this->local_symbol_count_
;
1195 gold_assert(loccount
== symtabshdr
.get_sh_info());
1196 off_t locsize
= loccount
* sym_size
;
1197 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1198 locsize
, true, true);
1200 // Read the symbol names.
1201 const unsigned int strtab_shndx
=
1202 this->adjust_shndx(symtabshdr
.get_sh_link());
1203 section_size_type strtab_size
;
1204 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1207 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1209 // Loop over the local symbols.
1211 const Output_sections
& out_sections(this->output_sections());
1212 unsigned int shnum
= this->shnum();
1213 unsigned int count
= 0;
1214 unsigned int dyncount
= 0;
1215 // Skip the first, dummy, symbol.
1217 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1219 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1221 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1224 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
1226 lv
.set_input_shndx(shndx
, is_ordinary
);
1228 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1229 lv
.set_is_section_symbol();
1230 else if (sym
.get_st_type() == elfcpp::STT_TLS
)
1231 lv
.set_is_tls_symbol();
1233 // Save the input symbol value for use in do_finalize_local_symbols().
1234 lv
.set_input_value(sym
.get_st_value());
1236 // Decide whether this symbol should go into the output file.
1238 if (shndx
< shnum
&& out_sections
[shndx
] == NULL
)
1240 lv
.set_no_output_symtab_entry();
1241 gold_assert(!lv
.needs_output_dynsym_entry());
1245 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1247 lv
.set_no_output_symtab_entry();
1248 gold_assert(!lv
.needs_output_dynsym_entry());
1252 if (sym
.get_st_name() >= strtab_size
)
1254 this->error(_("local symbol %u section name out of range: %u >= %u"),
1255 i
, sym
.get_st_name(),
1256 static_cast<unsigned int>(strtab_size
));
1257 lv
.set_no_output_symtab_entry();
1261 // Add the symbol to the symbol table string pool.
1262 const char* name
= pnames
+ sym
.get_st_name();
1263 pool
->add(name
, true, NULL
);
1266 // If needed, add the symbol to the dynamic symbol table string pool.
1267 if (lv
.needs_output_dynsym_entry())
1269 dynpool
->add(name
, true, NULL
);
1274 this->output_local_symbol_count_
= count
;
1275 this->output_local_dynsym_count_
= dyncount
;
1278 // Finalize the local symbols. Here we set the final value in
1279 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1280 // This function is always called from a singleton thread. The actual
1281 // output of the local symbols will occur in a separate task.
1283 template<int size
, bool big_endian
>
1285 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
1288 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1290 const unsigned int loccount
= this->local_symbol_count_
;
1291 this->local_symbol_offset_
= off
;
1293 const Output_sections
& out_sections(this->output_sections());
1294 const std::vector
<Address
>& out_offsets(this->section_offsets_
);
1295 unsigned int shnum
= this->shnum();
1297 for (unsigned int i
= 1; i
< loccount
; ++i
)
1299 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1302 unsigned int shndx
= lv
.input_shndx(&is_ordinary
);
1304 // Set the output symbol value.
1308 if (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
)
1309 lv
.set_output_value(lv
.input_value());
1312 this->error(_("unknown section index %u for local symbol %u"),
1314 lv
.set_output_value(0);
1321 this->error(_("local symbol %u section index %u out of range"),
1326 Output_section
* os
= out_sections
[shndx
];
1330 // This local symbol belongs to a section we are discarding.
1331 // In some cases when applying relocations later, we will
1332 // attempt to match it to the corresponding kept section,
1333 // so we leave the input value unchanged here.
1336 else if (out_offsets
[shndx
] == invalid_address
)
1338 // This is a SHF_MERGE section or one which otherwise
1339 // requires special handling. We get the output address
1340 // of the start of the merged section. If this is not a
1341 // section symbol, we can then determine the final
1342 // value. If it is a section symbol, we can not, as in
1343 // that case we have to consider the addend to determine
1344 // the value to use in a relocation.
1345 if (!lv
.is_section_symbol())
1346 lv
.set_output_value(os
->output_address(this, shndx
,
1350 section_offset_type start
=
1351 os
->starting_output_address(this, shndx
);
1352 Merged_symbol_value
<size
>* msv
=
1353 new Merged_symbol_value
<size
>(lv
.input_value(), start
);
1354 lv
.set_merged_symbol_value(msv
);
1357 else if (lv
.is_tls_symbol())
1358 lv
.set_output_value(os
->tls_offset()
1359 + out_offsets
[shndx
]
1360 + lv
.input_value());
1362 lv
.set_output_value(os
->address()
1363 + out_offsets
[shndx
]
1364 + lv
.input_value());
1367 if (lv
.needs_output_symtab_entry())
1369 lv
.set_output_symtab_index(index
);
1376 // Set the output dynamic symbol table indexes for the local variables.
1378 template<int size
, bool big_endian
>
1380 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_indexes(unsigned int index
)
1382 const unsigned int loccount
= this->local_symbol_count_
;
1383 for (unsigned int i
= 1; i
< loccount
; ++i
)
1385 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1386 if (lv
.needs_output_dynsym_entry())
1388 lv
.set_output_dynsym_index(index
);
1395 // Set the offset where local dynamic symbol information will be stored.
1396 // Returns the count of local symbols contributed to the symbol table by
1399 template<int size
, bool big_endian
>
1401 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_offset(off_t off
)
1403 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1404 this->local_dynsym_offset_
= off
;
1405 return this->output_local_dynsym_count_
;
1408 // Write out the local symbols.
1410 template<int size
, bool big_endian
>
1412 Sized_relobj
<size
, big_endian
>::write_local_symbols(
1414 const Stringpool
* sympool
,
1415 const Stringpool
* dynpool
,
1416 Output_symtab_xindex
* symtab_xindex
,
1417 Output_symtab_xindex
* dynsym_xindex
)
1419 const bool strip_all
= parameters
->options().strip_all();
1422 if (this->output_local_dynsym_count_
== 0)
1424 this->output_local_symbol_count_
= 0;
1427 gold_assert(this->symtab_shndx_
!= -1U);
1428 if (this->symtab_shndx_
== 0)
1430 // This object has no symbols. Weird but legal.
1434 // Read the symbol table section header.
1435 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1436 typename
This::Shdr
symtabshdr(this,
1437 this->elf_file_
.section_header(symtab_shndx
));
1438 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1439 const unsigned int loccount
= this->local_symbol_count_
;
1440 gold_assert(loccount
== symtabshdr
.get_sh_info());
1442 // Read the local symbols.
1443 const int sym_size
= This::sym_size
;
1444 off_t locsize
= loccount
* sym_size
;
1445 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1446 locsize
, true, false);
1448 // Read the symbol names.
1449 const unsigned int strtab_shndx
=
1450 this->adjust_shndx(symtabshdr
.get_sh_link());
1451 section_size_type strtab_size
;
1452 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1455 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1457 // Get views into the output file for the portions of the symbol table
1458 // and the dynamic symbol table that we will be writing.
1459 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
1460 unsigned char* oview
= NULL
;
1461 if (output_size
> 0)
1462 oview
= of
->get_output_view(this->local_symbol_offset_
, output_size
);
1464 off_t dyn_output_size
= this->output_local_dynsym_count_
* sym_size
;
1465 unsigned char* dyn_oview
= NULL
;
1466 if (dyn_output_size
> 0)
1467 dyn_oview
= of
->get_output_view(this->local_dynsym_offset_
,
1470 const Output_sections
out_sections(this->output_sections());
1472 gold_assert(this->local_values_
.size() == loccount
);
1474 unsigned char* ov
= oview
;
1475 unsigned char* dyn_ov
= dyn_oview
;
1477 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1479 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
1481 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1484 unsigned int st_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
1488 gold_assert(st_shndx
< out_sections
.size());
1489 if (out_sections
[st_shndx
] == NULL
)
1491 st_shndx
= out_sections
[st_shndx
]->out_shndx();
1492 if (st_shndx
>= elfcpp::SHN_LORESERVE
)
1494 if (lv
.needs_output_symtab_entry() && !strip_all
)
1495 symtab_xindex
->add(lv
.output_symtab_index(), st_shndx
);
1496 if (lv
.needs_output_dynsym_entry())
1497 dynsym_xindex
->add(lv
.output_dynsym_index(), st_shndx
);
1498 st_shndx
= elfcpp::SHN_XINDEX
;
1502 // Write the symbol to the output symbol table.
1503 if (!strip_all
&& lv
.needs_output_symtab_entry())
1505 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
1507 gold_assert(isym
.get_st_name() < strtab_size
);
1508 const char* name
= pnames
+ isym
.get_st_name();
1509 osym
.put_st_name(sympool
->get_offset(name
));
1510 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
1511 osym
.put_st_size(isym
.get_st_size());
1512 osym
.put_st_info(isym
.get_st_info());
1513 osym
.put_st_other(isym
.get_st_other());
1514 osym
.put_st_shndx(st_shndx
);
1519 // Write the symbol to the output dynamic symbol table.
1520 if (lv
.needs_output_dynsym_entry())
1522 gold_assert(dyn_ov
< dyn_oview
+ dyn_output_size
);
1523 elfcpp::Sym_write
<size
, big_endian
> osym(dyn_ov
);
1525 gold_assert(isym
.get_st_name() < strtab_size
);
1526 const char* name
= pnames
+ isym
.get_st_name();
1527 osym
.put_st_name(dynpool
->get_offset(name
));
1528 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
1529 osym
.put_st_size(isym
.get_st_size());
1530 osym
.put_st_info(isym
.get_st_info());
1531 osym
.put_st_other(isym
.get_st_other());
1532 osym
.put_st_shndx(st_shndx
);
1539 if (output_size
> 0)
1541 gold_assert(ov
- oview
== output_size
);
1542 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
1545 if (dyn_output_size
> 0)
1547 gold_assert(dyn_ov
- dyn_oview
== dyn_output_size
);
1548 of
->write_output_view(this->local_dynsym_offset_
, dyn_output_size
,
1553 // Set *INFO to symbolic information about the offset OFFSET in the
1554 // section SHNDX. Return true if we found something, false if we
1557 template<int size
, bool big_endian
>
1559 Sized_relobj
<size
, big_endian
>::get_symbol_location_info(
1562 Symbol_location_info
* info
)
1564 if (this->symtab_shndx_
== 0)
1567 section_size_type symbols_size
;
1568 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
1572 unsigned int symbol_names_shndx
=
1573 this->adjust_shndx(this->section_link(this->symtab_shndx_
));
1574 section_size_type names_size
;
1575 const unsigned char* symbol_names_u
=
1576 this->section_contents(symbol_names_shndx
, &names_size
, false);
1577 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
1579 const int sym_size
= This::sym_size
;
1580 const size_t count
= symbols_size
/ sym_size
;
1582 const unsigned char* p
= symbols
;
1583 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
1585 elfcpp::Sym
<size
, big_endian
> sym(p
);
1587 if (sym
.get_st_type() == elfcpp::STT_FILE
)
1589 if (sym
.get_st_name() >= names_size
)
1590 info
->source_file
= "(invalid)";
1592 info
->source_file
= symbol_names
+ sym
.get_st_name();
1597 unsigned int st_shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
1600 && st_shndx
== shndx
1601 && static_cast<off_t
>(sym
.get_st_value()) <= offset
1602 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
1605 if (sym
.get_st_name() > names_size
)
1606 info
->enclosing_symbol_name
= "(invalid)";
1609 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
1610 if (parameters
->options().do_demangle())
1612 char* demangled_name
= cplus_demangle(
1613 info
->enclosing_symbol_name
.c_str(),
1614 DMGL_ANSI
| DMGL_PARAMS
);
1615 if (demangled_name
!= NULL
)
1617 info
->enclosing_symbol_name
.assign(demangled_name
);
1618 free(demangled_name
);
1629 // Look for a kept section corresponding to the given discarded section,
1630 // and return its output address. This is used only for relocations in
1631 // debugging sections. If we can't find the kept section, return 0.
1633 template<int size
, bool big_endian
>
1634 typename Sized_relobj
<size
, big_endian
>::Address
1635 Sized_relobj
<size
, big_endian
>::map_to_kept_section(
1639 Kept_comdat_section
*kept
= this->get_kept_comdat_section(shndx
);
1642 gold_assert(kept
->object_
!= NULL
);
1644 Output_section
* os
= kept
->object_
->output_section(kept
->shndx_
);
1645 Address offset
= kept
->object_
->get_output_section_offset(kept
->shndx_
);
1646 gold_assert(os
!= NULL
&& offset
!= invalid_address
);
1647 return os
->address() + offset
;
1653 // Get symbol counts.
1655 template<int size
, bool big_endian
>
1657 Sized_relobj
<size
, big_endian
>::do_get_global_symbol_counts(
1658 const Symbol_table
*,
1662 *defined
= this->defined_count_
;
1664 for (Symbols::const_iterator p
= this->symbols_
.begin();
1665 p
!= this->symbols_
.end();
1668 && (*p
)->source() == Symbol::FROM_OBJECT
1669 && (*p
)->object() == this
1670 && (*p
)->is_defined())
1675 // Input_objects methods.
1677 // Add a regular relocatable object to the list. Return false if this
1678 // object should be ignored.
1681 Input_objects::add_object(Object
* obj
)
1683 // Set the global target from the first object file we recognize.
1684 Target
* target
= obj
->target();
1685 if (!parameters
->target_valid())
1686 set_parameters_target(target
);
1687 else if (target
!= ¶meters
->target())
1689 obj
->error(_("incompatible target"));
1693 // Print the filename if the -t/--trace option is selected.
1694 if (parameters
->options().trace())
1695 gold_info("%s", obj
->name().c_str());
1697 if (!obj
->is_dynamic())
1698 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
1701 // See if this is a duplicate SONAME.
1702 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
1703 const char* soname
= dynobj
->soname();
1705 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
1706 this->sonames_
.insert(soname
);
1709 // We have already seen a dynamic object with this soname.
1713 this->dynobj_list_
.push_back(dynobj
);
1715 // If this is -lc, remember the directory in which we found it.
1716 // We use this when issuing warnings about undefined symbols: as
1717 // a heuristic, we don't warn about system libraries found in
1718 // the same directory as -lc.
1719 if (strncmp(soname
, "libc.so", 7) == 0)
1721 const char* object_name
= dynobj
->name().c_str();
1722 const char* base
= lbasename(object_name
);
1723 if (base
!= object_name
)
1724 this->system_library_directory_
.assign(object_name
,
1725 base
- 1 - object_name
);
1729 // Add this object to the cross-referencer if requested.
1730 if (parameters
->options().user_set_print_symbol_counts())
1732 if (this->cref_
== NULL
)
1733 this->cref_
= new Cref();
1734 this->cref_
->add_object(obj
);
1740 // Return whether an object was found in the system library directory.
1743 Input_objects::found_in_system_library_directory(const Object
* object
) const
1745 return (!this->system_library_directory_
.empty()
1746 && object
->name().compare(0,
1747 this->system_library_directory_
.size(),
1748 this->system_library_directory_
) == 0);
1751 // For each dynamic object, record whether we've seen all of its
1752 // explicit dependencies.
1755 Input_objects::check_dynamic_dependencies() const
1757 for (Dynobj_list::const_iterator p
= this->dynobj_list_
.begin();
1758 p
!= this->dynobj_list_
.end();
1761 const Dynobj::Needed
& needed((*p
)->needed());
1762 bool found_all
= true;
1763 for (Dynobj::Needed::const_iterator pneeded
= needed
.begin();
1764 pneeded
!= needed
.end();
1767 if (this->sonames_
.find(*pneeded
) == this->sonames_
.end())
1773 (*p
)->set_has_unknown_needed_entries(!found_all
);
1777 // Start processing an archive.
1780 Input_objects::archive_start(Archive
* archive
)
1782 if (parameters
->options().user_set_print_symbol_counts())
1784 if (this->cref_
== NULL
)
1785 this->cref_
= new Cref();
1786 this->cref_
->add_archive_start(archive
);
1790 // Stop processing an archive.
1793 Input_objects::archive_stop(Archive
* archive
)
1795 if (parameters
->options().user_set_print_symbol_counts())
1796 this->cref_
->add_archive_stop(archive
);
1799 // Print symbol counts
1802 Input_objects::print_symbol_counts(const Symbol_table
* symtab
) const
1804 if (parameters
->options().user_set_print_symbol_counts()
1805 && this->cref_
!= NULL
)
1806 this->cref_
->print_symbol_counts(symtab
);
1809 // Relocate_info methods.
1811 // Return a string describing the location of a relocation. This is
1812 // only used in error messages.
1814 template<int size
, bool big_endian
>
1816 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
1818 // See if we can get line-number information from debugging sections.
1819 std::string filename
;
1820 std::string file_and_lineno
; // Better than filename-only, if available.
1822 Sized_dwarf_line_info
<size
, big_endian
> line_info(this->object
);
1823 // This will be "" if we failed to parse the debug info for any reason.
1824 file_and_lineno
= line_info
.addr2line(this->data_shndx
, offset
);
1826 std::string
ret(this->object
->name());
1828 Symbol_location_info info
;
1829 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
1831 ret
+= " in function ";
1832 ret
+= info
.enclosing_symbol_name
;
1834 filename
= info
.source_file
;
1837 if (!file_and_lineno
.empty())
1838 ret
+= file_and_lineno
;
1841 if (!filename
.empty())
1844 ret
+= this->object
->section_name(this->data_shndx
);
1846 // Offsets into sections have to be positive.
1847 snprintf(buf
, sizeof(buf
), "+0x%lx", static_cast<long>(offset
));
1854 } // End namespace gold.
1859 using namespace gold
;
1861 // Read an ELF file with the header and return the appropriate
1862 // instance of Object.
1864 template<int size
, bool big_endian
>
1866 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
1867 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1869 int et
= ehdr
.get_e_type();
1870 if (et
== elfcpp::ET_REL
)
1872 Sized_relobj
<size
, big_endian
>* obj
=
1873 new Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1877 else if (et
== elfcpp::ET_DYN
)
1879 Sized_dynobj
<size
, big_endian
>* obj
=
1880 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1886 gold_error(_("%s: unsupported ELF file type %d"),
1892 } // End anonymous namespace.
1897 // Read an ELF file and return the appropriate instance of Object.
1900 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1901 const unsigned char* p
, section_offset_type bytes
)
1903 if (bytes
< elfcpp::EI_NIDENT
)
1905 gold_error(_("%s: ELF file too short"), name
.c_str());
1909 int v
= p
[elfcpp::EI_VERSION
];
1910 if (v
!= elfcpp::EV_CURRENT
)
1912 if (v
== elfcpp::EV_NONE
)
1913 gold_error(_("%s: invalid ELF version 0"), name
.c_str());
1915 gold_error(_("%s: unsupported ELF version %d"), name
.c_str(), v
);
1919 int c
= p
[elfcpp::EI_CLASS
];
1920 if (c
== elfcpp::ELFCLASSNONE
)
1922 gold_error(_("%s: invalid ELF class 0"), name
.c_str());
1925 else if (c
!= elfcpp::ELFCLASS32
1926 && c
!= elfcpp::ELFCLASS64
)
1928 gold_error(_("%s: unsupported ELF class %d"), name
.c_str(), c
);
1932 int d
= p
[elfcpp::EI_DATA
];
1933 if (d
== elfcpp::ELFDATANONE
)
1935 gold_error(_("%s: invalid ELF data encoding"), name
.c_str());
1938 else if (d
!= elfcpp::ELFDATA2LSB
1939 && d
!= elfcpp::ELFDATA2MSB
)
1941 gold_error(_("%s: unsupported ELF data encoding %d"), name
.c_str(), d
);
1945 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
1947 if (c
== elfcpp::ELFCLASS32
)
1949 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1951 gold_error(_("%s: ELF file too short"), name
.c_str());
1956 #ifdef HAVE_TARGET_32_BIG
1957 elfcpp::Ehdr
<32, true> ehdr(p
);
1958 return make_elf_sized_object
<32, true>(name
, input_file
,
1961 gold_error(_("%s: not configured to support "
1962 "32-bit big-endian object"),
1969 #ifdef HAVE_TARGET_32_LITTLE
1970 elfcpp::Ehdr
<32, false> ehdr(p
);
1971 return make_elf_sized_object
<32, false>(name
, input_file
,
1974 gold_error(_("%s: not configured to support "
1975 "32-bit little-endian object"),
1983 if (bytes
< elfcpp::Elf_sizes
<64>::ehdr_size
)
1985 gold_error(_("%s: ELF file too short"), name
.c_str());
1990 #ifdef HAVE_TARGET_64_BIG
1991 elfcpp::Ehdr
<64, true> ehdr(p
);
1992 return make_elf_sized_object
<64, true>(name
, input_file
,
1995 gold_error(_("%s: not configured to support "
1996 "64-bit big-endian object"),
2003 #ifdef HAVE_TARGET_64_LITTLE
2004 elfcpp::Ehdr
<64, false> ehdr(p
);
2005 return make_elf_sized_object
<64, false>(name
, input_file
,
2008 gold_error(_("%s: not configured to support "
2009 "64-bit little-endian object"),
2017 // Instantiate the templates we need.
2019 #ifdef HAVE_TARGET_32_LITTLE
2022 Object::read_section_data
<32, false>(elfcpp::Elf_file
<32, false, Object
>*,
2023 Read_symbols_data
*);
2026 #ifdef HAVE_TARGET_32_BIG
2029 Object::read_section_data
<32, true>(elfcpp::Elf_file
<32, true, Object
>*,
2030 Read_symbols_data
*);
2033 #ifdef HAVE_TARGET_64_LITTLE
2036 Object::read_section_data
<64, false>(elfcpp::Elf_file
<64, false, Object
>*,
2037 Read_symbols_data
*);
2040 #ifdef HAVE_TARGET_64_BIG
2043 Object::read_section_data
<64, true>(elfcpp::Elf_file
<64, true, Object
>*,
2044 Read_symbols_data
*);
2047 #ifdef HAVE_TARGET_32_LITTLE
2049 class Sized_relobj
<32, false>;
2052 #ifdef HAVE_TARGET_32_BIG
2054 class Sized_relobj
<32, true>;
2057 #ifdef HAVE_TARGET_64_LITTLE
2059 class Sized_relobj
<64, false>;
2062 #ifdef HAVE_TARGET_64_BIG
2064 class Sized_relobj
<64, true>;
2067 #ifdef HAVE_TARGET_32_LITTLE
2069 struct Relocate_info
<32, false>;
2072 #ifdef HAVE_TARGET_32_BIG
2074 struct Relocate_info
<32, true>;
2077 #ifdef HAVE_TARGET_64_LITTLE
2079 struct Relocate_info
<64, false>;
2082 #ifdef HAVE_TARGET_64_BIG
2084 struct Relocate_info
<64, true>;
2087 } // End namespace gold.