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"
45 // Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
46 // section and read it in. SYMTAB_SHNDX is the index of the symbol
47 // table we care about.
49 template<int size
, bool big_endian
>
51 Xindex::initialize_symtab_xindex(Object
* object
, unsigned int symtab_shndx
)
53 if (!this->symtab_xindex_
.empty())
56 gold_assert(symtab_shndx
!= 0);
58 // Look through the sections in reverse order, on the theory that it
59 // is more likely to be near the end than the beginning.
60 unsigned int i
= object
->shnum();
64 if (object
->section_type(i
) == elfcpp::SHT_SYMTAB_SHNDX
65 && this->adjust_shndx(object
->section_link(i
)) == symtab_shndx
)
67 this->read_symtab_xindex
<size
, big_endian
>(object
, i
, NULL
);
72 object
->error(_("missing SHT_SYMTAB_SHNDX section"));
75 // Read in the symtab_xindex_ array, given the section index of the
76 // SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
79 template<int size
, bool big_endian
>
81 Xindex::read_symtab_xindex(Object
* object
, unsigned int xindex_shndx
,
82 const unsigned char* pshdrs
)
84 section_size_type bytecount
;
85 const unsigned char* contents
;
87 contents
= object
->section_contents(xindex_shndx
, &bytecount
, false);
90 const unsigned char* p
= (pshdrs
92 * elfcpp::Elf_sizes
<size
>::shdr_size
));
93 typename
elfcpp::Shdr
<size
, big_endian
> shdr(p
);
94 bytecount
= convert_to_section_size_type(shdr
.get_sh_size());
95 contents
= object
->get_view(shdr
.get_sh_offset(), bytecount
, true, false);
98 gold_assert(this->symtab_xindex_
.empty());
99 this->symtab_xindex_
.reserve(bytecount
/ 4);
100 for (section_size_type i
= 0; i
< bytecount
; i
+= 4)
102 unsigned int shndx
= elfcpp::Swap
<32, big_endian
>::readval(contents
+ i
);
103 // We preadjust the section indexes we save.
104 this->symtab_xindex_
.push_back(this->adjust_shndx(shndx
));
108 // Symbol symndx has a section of SHN_XINDEX; return the real section
112 Xindex::sym_xindex_to_shndx(Object
* object
, unsigned int symndx
)
114 if (symndx
>= this->symtab_xindex_
.size())
116 object
->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
118 return elfcpp::SHN_UNDEF
;
120 unsigned int shndx
= this->symtab_xindex_
[symndx
];
121 if (shndx
< elfcpp::SHN_LORESERVE
|| shndx
>= object
->shnum())
123 object
->error(_("extended index for symbol %u out of range: %u"),
125 return elfcpp::SHN_UNDEF
;
132 // Set the target based on fields in the ELF file header.
135 Object::set_target(int machine
, int size
, bool big_endian
, int osabi
,
138 Target
* target
= select_target(machine
, size
, big_endian
, osabi
, abiversion
);
140 gold_fatal(_("%s: unsupported ELF machine number %d"),
141 this->name().c_str(), machine
);
142 this->target_
= target
;
145 // Report an error for this object file. This is used by the
146 // elfcpp::Elf_file interface, and also called by the Object code
150 Object::error(const char* format
, ...) const
153 va_start(args
, format
);
155 if (vasprintf(&buf
, format
, args
) < 0)
158 gold_error(_("%s: %s"), this->name().c_str(), buf
);
162 // Return a view of the contents of a section.
165 Object::section_contents(unsigned int shndx
, section_size_type
* plen
,
168 Location
loc(this->do_section_contents(shndx
));
169 *plen
= convert_to_section_size_type(loc
.data_size
);
170 return this->get_view(loc
.file_offset
, *plen
, true, cache
);
173 // Read the section data into SD. This is code common to Sized_relobj
174 // and Sized_dynobj, so we put it into Object.
176 template<int size
, bool big_endian
>
178 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
179 Read_symbols_data
* sd
)
181 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
183 // Read the section headers.
184 const off_t shoff
= elf_file
->shoff();
185 const unsigned int shnum
= this->shnum();
186 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
,
189 // Read the section names.
190 const unsigned char* pshdrs
= sd
->section_headers
->data();
191 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
192 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
194 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
195 this->error(_("section name section has wrong type: %u"),
196 static_cast<unsigned int>(shdrnames
.get_sh_type()));
198 sd
->section_names_size
=
199 convert_to_section_size_type(shdrnames
.get_sh_size());
200 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
201 sd
->section_names_size
, false,
205 // If NAME is the name of a special .gnu.warning section, arrange for
206 // the warning to be issued. SHNDX is the section index. Return
207 // whether it is a warning section.
210 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
211 Symbol_table
* symtab
)
213 const char warn_prefix
[] = ".gnu.warning.";
214 const int warn_prefix_len
= sizeof warn_prefix
- 1;
215 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
217 // Read the section contents to get the warning text. It would
218 // be nicer if we only did this if we have to actually issue a
219 // warning. Unfortunately, warnings are issued as we relocate
220 // sections. That means that we can not lock the object then,
221 // as we might try to issue the same warning multiple times
223 section_size_type len
;
224 const unsigned char* contents
= this->section_contents(shndx
, &len
,
226 std::string
warning(reinterpret_cast<const char*>(contents
), len
);
227 symtab
->add_warning(name
+ warn_prefix_len
, this, warning
);
235 // Return the output address of the input section SHNDX.
237 Relobj::output_section_address(unsigned int shndx
) const
239 section_offset_type offset
;
240 Output_section
* os
= this->output_section(shndx
, &offset
);
241 gold_assert(os
!= NULL
&& offset
!= -1);
242 return os
->address() + offset
;
245 // Class Sized_relobj.
247 template<int size
, bool big_endian
>
248 Sized_relobj
<size
, big_endian
>::Sized_relobj(
249 const std::string
& name
,
250 Input_file
* input_file
,
252 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
253 : Relobj(name
, input_file
, offset
),
254 elf_file_(this, ehdr
),
256 local_symbol_count_(0),
257 output_local_symbol_count_(0),
258 output_local_dynsym_count_(0),
260 local_symbol_offset_(0),
261 local_dynsym_offset_(0),
263 local_got_offsets_(),
268 template<int size
, bool big_endian
>
269 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
273 // Set up an object file based on the file header. This sets up the
274 // target and reads the section information.
276 template<int size
, bool big_endian
>
278 Sized_relobj
<size
, big_endian
>::setup(
279 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
281 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
282 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
283 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
285 const unsigned int shnum
= this->elf_file_
.shnum();
286 this->set_shnum(shnum
);
289 // Find the SHT_SYMTAB section, given the section headers. The ELF
290 // standard says that maybe in the future there can be more than one
291 // SHT_SYMTAB section. Until somebody figures out how that could
292 // work, we assume there is only one.
294 template<int size
, bool big_endian
>
296 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
298 const unsigned int shnum
= this->shnum();
299 this->symtab_shndx_
= 0;
302 // Look through the sections in reverse order, since gas tends
303 // to put the symbol table at the end.
304 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
305 unsigned int i
= shnum
;
306 unsigned int xindex_shndx
= 0;
307 unsigned int xindex_link
= 0;
311 p
-= This::shdr_size
;
312 typename
This::Shdr
shdr(p
);
313 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
315 this->symtab_shndx_
= i
;
316 if (xindex_shndx
> 0 && xindex_link
== i
)
319 new Xindex(this->elf_file_
.large_shndx_offset());
320 xindex
->read_symtab_xindex
<size
, big_endian
>(this,
323 this->set_xindex(xindex
);
328 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
329 // one. This will work if it follows the SHT_SYMTAB
331 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX
)
334 xindex_link
= this->adjust_shndx(shdr
.get_sh_link());
340 // Return the Xindex structure to use for object with lots of
343 template<int size
, bool big_endian
>
345 Sized_relobj
<size
, big_endian
>::do_initialize_xindex()
347 gold_assert(this->symtab_shndx_
!= -1U);
348 Xindex
* xindex
= new Xindex(this->elf_file_
.large_shndx_offset());
349 xindex
->initialize_symtab_xindex
<size
, big_endian
>(this, this->symtab_shndx_
);
353 // Return whether SHDR has the right type and flags to be a GNU
354 // .eh_frame section.
356 template<int size
, bool big_endian
>
358 Sized_relobj
<size
, big_endian
>::check_eh_frame_flags(
359 const elfcpp::Shdr
<size
, big_endian
>* shdr
) const
361 return (shdr
->get_sh_type() == elfcpp::SHT_PROGBITS
362 && (shdr
->get_sh_flags() & elfcpp::SHF_ALLOC
) != 0);
365 // Return whether there is a GNU .eh_frame section, given the section
366 // headers and the section names.
368 template<int size
, bool big_endian
>
370 Sized_relobj
<size
, big_endian
>::find_eh_frame(
371 const unsigned char* pshdrs
,
373 section_size_type names_size
) const
375 const unsigned int shnum
= this->shnum();
376 const unsigned char* p
= pshdrs
+ This::shdr_size
;
377 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
379 typename
This::Shdr
shdr(p
);
380 if (this->check_eh_frame_flags(&shdr
))
382 if (shdr
.get_sh_name() >= names_size
)
384 this->error(_("bad section name offset for section %u: %lu"),
385 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
389 const char* name
= names
+ shdr
.get_sh_name();
390 if (strcmp(name
, ".eh_frame") == 0)
397 // Read the sections and symbols from an object file.
399 template<int size
, bool big_endian
>
401 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
403 this->read_section_data(&this->elf_file_
, sd
);
405 const unsigned char* const pshdrs
= sd
->section_headers
->data();
407 this->find_symtab(pshdrs
);
409 const unsigned char* namesu
= sd
->section_names
->data();
410 const char* names
= reinterpret_cast<const char*>(namesu
);
411 if (memmem(names
, sd
->section_names_size
, ".eh_frame", 10) != NULL
)
413 if (this->find_eh_frame(pshdrs
, names
, sd
->section_names_size
))
414 this->has_eh_frame_
= true;
418 sd
->symbols_size
= 0;
419 sd
->external_symbols_offset
= 0;
420 sd
->symbol_names
= NULL
;
421 sd
->symbol_names_size
= 0;
423 if (this->symtab_shndx_
== 0)
425 // No symbol table. Weird but legal.
429 // Get the symbol table section header.
430 typename
This::Shdr
symtabshdr(pshdrs
431 + this->symtab_shndx_
* This::shdr_size
);
432 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
434 // If this object has a .eh_frame section, we need all the symbols.
435 // Otherwise we only need the external symbols. While it would be
436 // simpler to just always read all the symbols, I've seen object
437 // files with well over 2000 local symbols, which for a 64-bit
438 // object file format is over 5 pages that we don't need to read
441 const int sym_size
= This::sym_size
;
442 const unsigned int loccount
= symtabshdr
.get_sh_info();
443 this->local_symbol_count_
= loccount
;
444 this->local_values_
.resize(loccount
);
445 section_offset_type locsize
= loccount
* sym_size
;
446 off_t dataoff
= symtabshdr
.get_sh_offset();
447 section_size_type datasize
=
448 convert_to_section_size_type(symtabshdr
.get_sh_size());
449 off_t extoff
= dataoff
+ locsize
;
450 section_size_type extsize
= datasize
- locsize
;
452 off_t readoff
= this->has_eh_frame_
? dataoff
: extoff
;
453 section_size_type readsize
= this->has_eh_frame_
? datasize
: extsize
;
455 File_view
* fvsymtab
= this->get_lasting_view(readoff
, readsize
, true, false);
457 // Read the section header for the symbol names.
458 unsigned int strtab_shndx
= this->adjust_shndx(symtabshdr
.get_sh_link());
459 if (strtab_shndx
>= this->shnum())
461 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
464 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
465 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
467 this->error(_("symbol table name section has wrong type: %u"),
468 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
472 // Read the symbol names.
473 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
474 strtabshdr
.get_sh_size(),
477 sd
->symbols
= fvsymtab
;
478 sd
->symbols_size
= readsize
;
479 sd
->external_symbols_offset
= this->has_eh_frame_
? locsize
: 0;
480 sd
->symbol_names
= fvstrtab
;
481 sd
->symbol_names_size
=
482 convert_to_section_size_type(strtabshdr
.get_sh_size());
485 // Return the section index of symbol SYM. Set *VALUE to its value in
486 // the object file. Set *IS_ORDINARY if this is an ordinary section
487 // index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
488 // Note that for a symbol which is not defined in this object file,
489 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
490 // the final value of the symbol in the link.
492 template<int size
, bool big_endian
>
494 Sized_relobj
<size
, big_endian
>::symbol_section_and_value(unsigned int sym
,
498 section_size_type symbols_size
;
499 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
503 const size_t count
= symbols_size
/ This::sym_size
;
504 gold_assert(sym
< count
);
506 elfcpp::Sym
<size
, big_endian
> elfsym(symbols
+ sym
* This::sym_size
);
507 *value
= elfsym
.get_st_value();
509 return this->adjust_sym_shndx(sym
, elfsym
.get_st_shndx(), is_ordinary
);
512 // Return whether to include a section group in the link. LAYOUT is
513 // used to keep track of which section groups we have already seen.
514 // INDEX is the index of the section group and SHDR is the section
515 // header. If we do not want to include this group, we set bits in
516 // OMIT for each section which should be discarded.
518 template<int size
, bool big_endian
>
520 Sized_relobj
<size
, big_endian
>::include_section_group(
521 Symbol_table
* symtab
,
525 const unsigned char* shdrs
,
526 const char* section_names
,
527 section_size_type section_names_size
,
528 std::vector
<bool>* omit
)
530 // Read the section contents.
531 typename
This::Shdr
shdr(shdrs
+ index
* This::shdr_size
);
532 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
533 shdr
.get_sh_size(), true, false);
534 const elfcpp::Elf_Word
* pword
=
535 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
537 // The first word contains flags. We only care about COMDAT section
538 // groups. Other section groups are always included in the link
539 // just like ordinary sections.
540 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
542 // Look up the group signature, which is the name of a symbol. This
543 // is a lot of effort to go to to read a string. Why didn't they
544 // just have the group signature point into the string table, rather
545 // than indirect through a symbol?
547 // Get the appropriate symbol table header (this will normally be
548 // the single SHT_SYMTAB section, but in principle it need not be).
549 const unsigned int link
= this->adjust_shndx(shdr
.get_sh_link());
550 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
552 // Read the symbol table entry.
553 unsigned int symndx
= shdr
.get_sh_info();
554 if (symndx
>= symshdr
.get_sh_size() / This::sym_size
)
556 this->error(_("section group %u info %u out of range"),
560 off_t symoff
= symshdr
.get_sh_offset() + symndx
* This::sym_size
;
561 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true,
563 elfcpp::Sym
<size
, big_endian
> sym(psym
);
565 // Read the symbol table names.
566 section_size_type symnamelen
;
567 const unsigned char* psymnamesu
;
568 psymnamesu
= this->section_contents(this->adjust_shndx(symshdr
.get_sh_link()),
570 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
572 // Get the section group signature.
573 if (sym
.get_st_name() >= symnamelen
)
575 this->error(_("symbol %u name offset %u out of range"),
576 symndx
, sym
.get_st_name());
580 std::string
signature(psymnames
+ sym
.get_st_name());
582 // It seems that some versions of gas will create a section group
583 // associated with a section symbol, and then fail to give a name to
584 // the section symbol. In such a case, use the name of the section.
585 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
588 unsigned int sym_shndx
= this->adjust_sym_shndx(symndx
,
591 if (!is_ordinary
|| sym_shndx
>= this->shnum())
593 this->error(_("symbol %u invalid section index %u"),
597 typename
This::Shdr
member_shdr(shdrs
+ sym_shndx
* This::shdr_size
);
598 if (member_shdr
.get_sh_name() < section_names_size
)
599 signature
= section_names
+ member_shdr
.get_sh_name();
602 // Record this section group in the layout, and see whether we've already
603 // seen one with the same signature.
604 bool include_group
= ((flags
& elfcpp::GRP_COMDAT
) == 0
605 || layout
->add_comdat(this, index
, signature
, true));
607 if (include_group
&& parameters
->options().relocatable())
608 layout
->layout_group(symtab
, this, index
, name
, signature
.c_str(),
611 Relobj
* kept_object
= NULL
;
612 Comdat_group
* kept_group
= NULL
;
616 // This group is being discarded. Find the object and group
617 // that was kept in its place.
618 unsigned int kept_group_index
= 0;
619 kept_object
= layout
->find_kept_object(signature
, &kept_group_index
);
620 if (kept_object
!= NULL
)
621 kept_group
= kept_object
->find_comdat_group(kept_group_index
);
623 else if (flags
& elfcpp::GRP_COMDAT
)
625 // This group is being kept. Create the table to map section names
626 // to section indexes and add it to the table of groups.
627 kept_group
= new Comdat_group();
628 this->add_comdat_group(index
, kept_group
);
631 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
632 for (size_t i
= 1; i
< count
; ++i
)
634 elfcpp::Elf_Word secnum
=
635 elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
);
636 if (secnum
>= this->shnum())
638 this->error(_("section %u in section group %u out of range"),
643 // Check for an earlier section number, since we're going to get
644 // it wrong--we may have already decided to include the section.
646 this->error(_("invalid section group %u refers to earlier section %u"),
649 // Get the name of the member section.
650 typename
This::Shdr
member_shdr(shdrs
+ secnum
* This::shdr_size
);
651 if (member_shdr
.get_sh_name() >= section_names_size
)
653 // This is an error, but it will be diagnosed eventually
654 // in do_layout, so we don't need to do anything here but
658 std::string
mname(section_names
+ member_shdr
.get_sh_name());
662 (*omit
)[secnum
] = true;
663 if (kept_group
!= NULL
)
665 // Find the corresponding kept section, and store that info
666 // in the discarded section table.
667 Comdat_group::const_iterator p
= kept_group
->find(mname
);
668 if (p
!= kept_group
->end())
670 Kept_comdat_section
* kept
=
671 new Kept_comdat_section(kept_object
, p
->second
);
672 this->set_kept_comdat_section(secnum
, kept
);
676 else if (flags
& elfcpp::GRP_COMDAT
)
678 // Add the section to the kept group table.
679 gold_assert(kept_group
!= NULL
);
680 kept_group
->insert(std::make_pair(mname
, secnum
));
684 return include_group
;
687 // Whether to include a linkonce section in the link. NAME is the
688 // name of the section and SHDR is the section header.
690 // Linkonce sections are a GNU extension implemented in the original
691 // GNU linker before section groups were defined. The semantics are
692 // that we only include one linkonce section with a given name. The
693 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
694 // where T is the type of section and SYMNAME is the name of a symbol.
695 // In an attempt to make linkonce sections interact well with section
696 // groups, we try to identify SYMNAME and use it like a section group
697 // signature. We want to block section groups with that signature,
698 // but not other linkonce sections with that signature. We also use
699 // the full name of the linkonce section as a normal section group
702 template<int size
, bool big_endian
>
704 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
708 const elfcpp::Shdr
<size
, big_endian
>&)
710 // In general the symbol name we want will be the string following
711 // the last '.'. However, we have to handle the case of
712 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
713 // some versions of gcc. So we use a heuristic: if the name starts
714 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
715 // we look for the last '.'. We can't always simply skip
716 // ".gnu.linkonce.X", because we have to deal with cases like
717 // ".gnu.linkonce.d.rel.ro.local".
718 const char* const linkonce_t
= ".gnu.linkonce.t.";
720 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
721 symname
= name
+ strlen(linkonce_t
);
723 symname
= strrchr(name
, '.') + 1;
724 std::string
sig1(symname
);
725 std::string
sig2(name
);
726 bool include1
= layout
->add_comdat(this, index
, sig1
, false);
727 bool include2
= layout
->add_comdat(this, index
, sig2
, true);
731 // The section is being discarded on the basis of its section
732 // name (i.e., the kept section was also a linkonce section).
733 // In this case, the section index stored with the layout object
734 // is the linkonce section that was kept.
735 unsigned int kept_group_index
= 0;
736 Relobj
* kept_object
= layout
->find_kept_object(sig2
, &kept_group_index
);
737 if (kept_object
!= NULL
)
739 Kept_comdat_section
* kept
=
740 new Kept_comdat_section(kept_object
, kept_group_index
);
741 this->set_kept_comdat_section(index
, kept
);
746 // The section is being discarded on the basis of its symbol
747 // name. This means that the corresponding kept section was
748 // part of a comdat group, and it will be difficult to identify
749 // the specific section within that group that corresponds to
750 // this linkonce section. We'll handle the simple case where
751 // the group has only one member section. Otherwise, it's not
753 unsigned int kept_group_index
= 0;
754 Relobj
* kept_object
= layout
->find_kept_object(sig1
, &kept_group_index
);
755 if (kept_object
!= NULL
)
757 Comdat_group
* kept_group
=
758 kept_object
->find_comdat_group(kept_group_index
);
759 if (kept_group
!= NULL
&& kept_group
->size() == 1)
761 Comdat_group::const_iterator p
= kept_group
->begin();
762 gold_assert(p
!= kept_group
->end());
763 Kept_comdat_section
* kept
=
764 new Kept_comdat_section(kept_object
, p
->second
);
765 this->set_kept_comdat_section(index
, kept
);
770 return include1
&& include2
;
773 // Lay out the input sections. We walk through the sections and check
774 // whether they should be included in the link. If they should, we
775 // pass them to the Layout object, which will return an output section
778 template<int size
, bool big_endian
>
780 Sized_relobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
782 Read_symbols_data
* sd
)
784 const unsigned int shnum
= this->shnum();
788 // Get the section headers.
789 const unsigned char* shdrs
= sd
->section_headers
->data();
790 const unsigned char* pshdrs
;
792 // Get the section names.
793 const unsigned char* pnamesu
= sd
->section_names
->data();
794 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
796 // For each section, record the index of the reloc section if any.
797 // Use 0 to mean that there is no reloc section, -1U to mean that
798 // there is more than one.
799 std::vector
<unsigned int> reloc_shndx(shnum
, 0);
800 std::vector
<unsigned int> reloc_type(shnum
, elfcpp::SHT_NULL
);
801 // Skip the first, dummy, section.
802 pshdrs
= shdrs
+ This::shdr_size
;
803 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
805 typename
This::Shdr
shdr(pshdrs
);
807 unsigned int sh_type
= shdr
.get_sh_type();
808 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
810 unsigned int target_shndx
= this->adjust_shndx(shdr
.get_sh_info());
811 if (target_shndx
== 0 || target_shndx
>= shnum
)
813 this->error(_("relocation section %u has bad info %u"),
818 if (reloc_shndx
[target_shndx
] != 0)
819 reloc_shndx
[target_shndx
] = -1U;
822 reloc_shndx
[target_shndx
] = i
;
823 reloc_type
[target_shndx
] = sh_type
;
828 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
829 map_sections
.resize(shnum
);
831 // If we are only linking for symbols, then there is nothing else to
833 if (this->input_file()->just_symbols())
835 delete sd
->section_headers
;
836 sd
->section_headers
= NULL
;
837 delete sd
->section_names
;
838 sd
->section_names
= NULL
;
842 // Whether we've seen a .note.GNU-stack section.
843 bool seen_gnu_stack
= false;
844 // The flags of a .note.GNU-stack section.
845 uint64_t gnu_stack_flags
= 0;
847 // Keep track of which sections to omit.
848 std::vector
<bool> omit(shnum
, false);
850 // Keep track of reloc sections when emitting relocations.
851 const bool relocatable
= parameters
->options().relocatable();
852 const bool emit_relocs
= (relocatable
853 || parameters
->options().emit_relocs());
854 std::vector
<unsigned int> reloc_sections
;
856 // Keep track of .eh_frame sections.
857 std::vector
<unsigned int> eh_frame_sections
;
859 // Skip the first, dummy, section.
860 pshdrs
= shdrs
+ This::shdr_size
;
861 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
863 typename
This::Shdr
shdr(pshdrs
);
865 if (shdr
.get_sh_name() >= sd
->section_names_size
)
867 this->error(_("bad section name offset for section %u: %lu"),
868 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
872 const char* name
= pnames
+ shdr
.get_sh_name();
874 if (this->handle_gnu_warning_section(name
, i
, symtab
))
880 // The .note.GNU-stack section is special. It gives the
881 // protection flags that this object file requires for the stack
883 if (strcmp(name
, ".note.GNU-stack") == 0)
885 seen_gnu_stack
= true;
886 gnu_stack_flags
|= shdr
.get_sh_flags();
890 bool discard
= omit
[i
];
893 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
895 if (!this->include_section_group(symtab
, layout
, i
, name
, shdrs
,
896 pnames
, sd
->section_names_size
,
900 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
901 && Layout::is_linkonce(name
))
903 if (!this->include_linkonce_section(layout
, i
, name
, shdr
))
910 // Do not include this section in the link.
911 map_sections
[i
].output_section
= NULL
;
915 // When doing a relocatable link we are going to copy input
916 // reloc sections into the output. We only want to copy the
917 // ones associated with sections which are not being discarded.
918 // However, we don't know that yet for all sections. So save
919 // reloc sections and process them later.
921 && (shdr
.get_sh_type() == elfcpp::SHT_REL
922 || shdr
.get_sh_type() == elfcpp::SHT_RELA
))
924 reloc_sections
.push_back(i
);
928 if (relocatable
&& shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
931 // The .eh_frame section is special. It holds exception frame
932 // information that we need to read in order to generate the
933 // exception frame header. We process these after all the other
934 // sections so that the exception frame reader can reliably
935 // determine which sections are being discarded, and discard the
936 // corresponding information.
938 && strcmp(name
, ".eh_frame") == 0
939 && this->check_eh_frame_flags(&shdr
))
941 eh_frame_sections
.push_back(i
);
946 Output_section
* os
= layout
->layout(this, i
, name
, shdr
,
947 reloc_shndx
[i
], reloc_type
[i
],
950 map_sections
[i
].output_section
= os
;
951 map_sections
[i
].offset
= offset
;
953 // If this section requires special handling, and if there are
954 // relocs that apply to it, then we must do the special handling
955 // before we apply the relocs.
956 if (offset
== -1 && reloc_shndx
[i
] != 0)
957 this->set_relocs_must_follow_section_writes();
960 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
);
962 // When doing a relocatable link handle the reloc sections at the
965 this->size_relocatable_relocs();
966 for (std::vector
<unsigned int>::const_iterator p
= reloc_sections
.begin();
967 p
!= reloc_sections
.end();
971 const unsigned char* pshdr
;
972 pshdr
= sd
->section_headers
->data() + i
* This::shdr_size
;
973 typename
This::Shdr
shdr(pshdr
);
975 unsigned int data_shndx
= this->adjust_shndx(shdr
.get_sh_info());
976 if (data_shndx
>= shnum
)
978 // We already warned about this above.
982 Output_section
* data_section
= map_sections
[data_shndx
].output_section
;
983 if (data_section
== NULL
)
985 map_sections
[i
].output_section
= NULL
;
989 Relocatable_relocs
* rr
= new Relocatable_relocs();
990 this->set_relocatable_relocs(i
, rr
);
992 Output_section
* os
= layout
->layout_reloc(this, i
, shdr
, data_section
,
994 map_sections
[i
].output_section
= os
;
995 map_sections
[i
].offset
= -1;
998 // Handle the .eh_frame sections at the end.
999 for (std::vector
<unsigned int>::const_iterator p
= eh_frame_sections
.begin();
1000 p
!= eh_frame_sections
.end();
1003 gold_assert(this->has_eh_frame_
);
1004 gold_assert(sd
->external_symbols_offset
!= 0);
1006 unsigned int i
= *p
;
1007 const unsigned char *pshdr
;
1008 pshdr
= sd
->section_headers
->data() + i
* This::shdr_size
;
1009 typename
This::Shdr
shdr(pshdr
);
1012 Output_section
* os
= layout
->layout_eh_frame(this,
1013 sd
->symbols
->data(),
1015 sd
->symbol_names
->data(),
1016 sd
->symbol_names_size
,
1021 map_sections
[i
].output_section
= os
;
1022 map_sections
[i
].offset
= offset
;
1024 // If this section requires special handling, and if there are
1025 // relocs that apply to it, then we must do the special handling
1026 // before we apply the relocs.
1027 if (offset
== -1 && reloc_shndx
[i
] != 0)
1028 this->set_relocs_must_follow_section_writes();
1031 delete sd
->section_headers
;
1032 sd
->section_headers
= NULL
;
1033 delete sd
->section_names
;
1034 sd
->section_names
= NULL
;
1037 // Add the symbols to the symbol table.
1039 template<int size
, bool big_endian
>
1041 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1042 Read_symbols_data
* sd
)
1044 if (sd
->symbols
== NULL
)
1046 gold_assert(sd
->symbol_names
== NULL
);
1050 const int sym_size
= This::sym_size
;
1051 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
1053 if (symcount
* sym_size
!= sd
->symbols_size
- sd
->external_symbols_offset
)
1055 this->error(_("size of symbols is not multiple of symbol size"));
1059 this->symbols_
.resize(symcount
);
1061 const char* sym_names
=
1062 reinterpret_cast<const char*>(sd
->symbol_names
->data());
1063 symtab
->add_from_relobj(this,
1064 sd
->symbols
->data() + sd
->external_symbols_offset
,
1065 symcount
, this->local_symbol_count_
,
1066 sym_names
, sd
->symbol_names_size
,
1071 delete sd
->symbol_names
;
1072 sd
->symbol_names
= NULL
;
1075 // First pass over the local symbols. Here we add their names to
1076 // *POOL and *DYNPOOL, and we store the symbol value in
1077 // THIS->LOCAL_VALUES_. This function is always called from a
1078 // singleton thread. This is followed by a call to
1079 // finalize_local_symbols.
1081 template<int size
, bool big_endian
>
1083 Sized_relobj
<size
, big_endian
>::do_count_local_symbols(Stringpool
* pool
,
1084 Stringpool
* dynpool
)
1086 gold_assert(this->symtab_shndx_
!= -1U);
1087 if (this->symtab_shndx_
== 0)
1089 // This object has no symbols. Weird but legal.
1093 // Read the symbol table section header.
1094 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1095 typename
This::Shdr
symtabshdr(this,
1096 this->elf_file_
.section_header(symtab_shndx
));
1097 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1099 // Read the local symbols.
1100 const int sym_size
= This::sym_size
;
1101 const unsigned int loccount
= this->local_symbol_count_
;
1102 gold_assert(loccount
== symtabshdr
.get_sh_info());
1103 off_t locsize
= loccount
* sym_size
;
1104 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1105 locsize
, true, true);
1107 // Read the symbol names.
1108 const unsigned int strtab_shndx
=
1109 this->adjust_shndx(symtabshdr
.get_sh_link());
1110 section_size_type strtab_size
;
1111 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1114 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1116 // Loop over the local symbols.
1118 const std::vector
<Map_to_output
>& mo(this->map_to_output());
1119 unsigned int shnum
= this->shnum();
1120 unsigned int count
= 0;
1121 unsigned int dyncount
= 0;
1122 // Skip the first, dummy, symbol.
1124 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1126 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
1128 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1131 unsigned int shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
1133 lv
.set_input_shndx(shndx
, is_ordinary
);
1135 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1136 lv
.set_is_section_symbol();
1137 else if (sym
.get_st_type() == elfcpp::STT_TLS
)
1138 lv
.set_is_tls_symbol();
1140 // Save the input symbol value for use in do_finalize_local_symbols().
1141 lv
.set_input_value(sym
.get_st_value());
1143 // Decide whether this symbol should go into the output file.
1145 if (shndx
< shnum
&& mo
[shndx
].output_section
== NULL
)
1147 lv
.set_no_output_symtab_entry();
1148 gold_assert(!lv
.needs_output_dynsym_entry());
1152 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
1154 lv
.set_no_output_symtab_entry();
1155 gold_assert(!lv
.needs_output_dynsym_entry());
1159 if (sym
.get_st_name() >= strtab_size
)
1161 this->error(_("local symbol %u section name out of range: %u >= %u"),
1162 i
, sym
.get_st_name(),
1163 static_cast<unsigned int>(strtab_size
));
1164 lv
.set_no_output_symtab_entry();
1168 // Add the symbol to the symbol table string pool.
1169 const char* name
= pnames
+ sym
.get_st_name();
1170 pool
->add(name
, true, NULL
);
1173 // If needed, add the symbol to the dynamic symbol table string pool.
1174 if (lv
.needs_output_dynsym_entry())
1176 dynpool
->add(name
, true, NULL
);
1181 this->output_local_symbol_count_
= count
;
1182 this->output_local_dynsym_count_
= dyncount
;
1185 // Finalize the local symbols. Here we set the final value in
1186 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1187 // This function is always called from a singleton thread. The actual
1188 // output of the local symbols will occur in a separate task.
1190 template<int size
, bool big_endian
>
1192 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
1195 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1197 const unsigned int loccount
= this->local_symbol_count_
;
1198 this->local_symbol_offset_
= off
;
1200 const std::vector
<Map_to_output
>& mo(this->map_to_output());
1201 unsigned int shnum
= this->shnum();
1203 for (unsigned int i
= 1; i
< loccount
; ++i
)
1205 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1208 unsigned int shndx
= lv
.input_shndx(&is_ordinary
);
1210 // Set the output symbol value.
1214 if (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
)
1215 lv
.set_output_value(lv
.input_value());
1218 this->error(_("unknown section index %u for local symbol %u"),
1220 lv
.set_output_value(0);
1227 this->error(_("local symbol %u section index %u out of range"),
1232 Output_section
* os
= mo
[shndx
].output_section
;
1236 // This local symbol belongs to a section we are discarding.
1237 // In some cases when applying relocations later, we will
1238 // attempt to match it to the corresponding kept section,
1239 // so we leave the input value unchanged here.
1242 else if (mo
[shndx
].offset
== -1)
1244 // This is a SHF_MERGE section or one which otherwise
1245 // requires special handling. We get the output address
1246 // of the start of the merged section. If this is not a
1247 // section symbol, we can then determine the final
1248 // value. If it is a section symbol, we can not, as in
1249 // that case we have to consider the addend to determine
1250 // the value to use in a relocation.
1251 if (!lv
.is_section_symbol())
1252 lv
.set_output_value(os
->output_address(this, shndx
,
1256 section_offset_type start
=
1257 os
->starting_output_address(this, shndx
);
1258 Merged_symbol_value
<size
>* msv
=
1259 new Merged_symbol_value
<size
>(lv
.input_value(), start
);
1260 lv
.set_merged_symbol_value(msv
);
1263 else if (lv
.is_tls_symbol())
1264 lv
.set_output_value(os
->tls_offset()
1266 + lv
.input_value());
1268 lv
.set_output_value(os
->address()
1270 + lv
.input_value());
1273 if (lv
.needs_output_symtab_entry())
1275 lv
.set_output_symtab_index(index
);
1282 // Set the output dynamic symbol table indexes for the local variables.
1284 template<int size
, bool big_endian
>
1286 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_indexes(unsigned int index
)
1288 const unsigned int loccount
= this->local_symbol_count_
;
1289 for (unsigned int i
= 1; i
< loccount
; ++i
)
1291 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1292 if (lv
.needs_output_dynsym_entry())
1294 lv
.set_output_dynsym_index(index
);
1301 // Set the offset where local dynamic symbol information will be stored.
1302 // Returns the count of local symbols contributed to the symbol table by
1305 template<int size
, bool big_endian
>
1307 Sized_relobj
<size
, big_endian
>::do_set_local_dynsym_offset(off_t off
)
1309 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
1310 this->local_dynsym_offset_
= off
;
1311 return this->output_local_dynsym_count_
;
1314 // Write out the local symbols.
1316 template<int size
, bool big_endian
>
1318 Sized_relobj
<size
, big_endian
>::write_local_symbols(
1320 const Stringpool
* sympool
,
1321 const Stringpool
* dynpool
,
1322 Output_symtab_xindex
* symtab_xindex
,
1323 Output_symtab_xindex
* dynsym_xindex
)
1325 if (parameters
->options().strip_all()
1326 && this->output_local_dynsym_count_
== 0)
1329 gold_assert(this->symtab_shndx_
!= -1U);
1330 if (this->symtab_shndx_
== 0)
1332 // This object has no symbols. Weird but legal.
1336 // Read the symbol table section header.
1337 const unsigned int symtab_shndx
= this->symtab_shndx_
;
1338 typename
This::Shdr
symtabshdr(this,
1339 this->elf_file_
.section_header(symtab_shndx
));
1340 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
1341 const unsigned int loccount
= this->local_symbol_count_
;
1342 gold_assert(loccount
== symtabshdr
.get_sh_info());
1344 // Read the local symbols.
1345 const int sym_size
= This::sym_size
;
1346 off_t locsize
= loccount
* sym_size
;
1347 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
1348 locsize
, true, false);
1350 // Read the symbol names.
1351 const unsigned int strtab_shndx
=
1352 this->adjust_shndx(symtabshdr
.get_sh_link());
1353 section_size_type strtab_size
;
1354 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
1357 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
1359 // Get views into the output file for the portions of the symbol table
1360 // and the dynamic symbol table that we will be writing.
1361 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
1362 unsigned char* oview
= NULL
;
1363 if (output_size
> 0)
1364 oview
= of
->get_output_view(this->local_symbol_offset_
, output_size
);
1366 off_t dyn_output_size
= this->output_local_dynsym_count_
* sym_size
;
1367 unsigned char* dyn_oview
= NULL
;
1368 if (dyn_output_size
> 0)
1369 dyn_oview
= of
->get_output_view(this->local_dynsym_offset_
,
1372 const std::vector
<Map_to_output
>& mo(this->map_to_output());
1374 gold_assert(this->local_values_
.size() == loccount
);
1376 unsigned char* ov
= oview
;
1377 unsigned char* dyn_ov
= dyn_oview
;
1379 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
1381 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
1383 Symbol_value
<size
>& lv(this->local_values_
[i
]);
1386 unsigned int st_shndx
= this->adjust_sym_shndx(i
, isym
.get_st_shndx(),
1390 gold_assert(st_shndx
< mo
.size());
1391 if (mo
[st_shndx
].output_section
== NULL
)
1393 st_shndx
= mo
[st_shndx
].output_section
->out_shndx();
1394 if (st_shndx
>= elfcpp::SHN_LORESERVE
)
1396 if (lv
.needs_output_symtab_entry())
1397 symtab_xindex
->add(lv
.output_symtab_index(), st_shndx
);
1398 if (lv
.needs_output_dynsym_entry())
1399 dynsym_xindex
->add(lv
.output_dynsym_index(), st_shndx
);
1400 st_shndx
= elfcpp::SHN_XINDEX
;
1404 // Write the symbol to the output symbol table.
1405 if (!parameters
->options().strip_all()
1406 && lv
.needs_output_symtab_entry())
1408 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
1410 gold_assert(isym
.get_st_name() < strtab_size
);
1411 const char* name
= pnames
+ isym
.get_st_name();
1412 osym
.put_st_name(sympool
->get_offset(name
));
1413 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
1414 osym
.put_st_size(isym
.get_st_size());
1415 osym
.put_st_info(isym
.get_st_info());
1416 osym
.put_st_other(isym
.get_st_other());
1417 osym
.put_st_shndx(st_shndx
);
1422 // Write the symbol to the output dynamic symbol table.
1423 if (lv
.needs_output_dynsym_entry())
1425 gold_assert(dyn_ov
< dyn_oview
+ dyn_output_size
);
1426 elfcpp::Sym_write
<size
, big_endian
> osym(dyn_ov
);
1428 gold_assert(isym
.get_st_name() < strtab_size
);
1429 const char* name
= pnames
+ isym
.get_st_name();
1430 osym
.put_st_name(dynpool
->get_offset(name
));
1431 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
1432 osym
.put_st_size(isym
.get_st_size());
1433 osym
.put_st_info(isym
.get_st_info());
1434 osym
.put_st_other(isym
.get_st_other());
1435 osym
.put_st_shndx(st_shndx
);
1442 if (output_size
> 0)
1444 gold_assert(ov
- oview
== output_size
);
1445 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
1448 if (dyn_output_size
> 0)
1450 gold_assert(dyn_ov
- dyn_oview
== dyn_output_size
);
1451 of
->write_output_view(this->local_dynsym_offset_
, dyn_output_size
,
1456 // Set *INFO to symbolic information about the offset OFFSET in the
1457 // section SHNDX. Return true if we found something, false if we
1460 template<int size
, bool big_endian
>
1462 Sized_relobj
<size
, big_endian
>::get_symbol_location_info(
1465 Symbol_location_info
* info
)
1467 if (this->symtab_shndx_
== 0)
1470 section_size_type symbols_size
;
1471 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
1475 unsigned int symbol_names_shndx
=
1476 this->adjust_shndx(this->section_link(this->symtab_shndx_
));
1477 section_size_type names_size
;
1478 const unsigned char* symbol_names_u
=
1479 this->section_contents(symbol_names_shndx
, &names_size
, false);
1480 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
1482 const int sym_size
= This::sym_size
;
1483 const size_t count
= symbols_size
/ sym_size
;
1485 const unsigned char* p
= symbols
;
1486 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
1488 elfcpp::Sym
<size
, big_endian
> sym(p
);
1490 if (sym
.get_st_type() == elfcpp::STT_FILE
)
1492 if (sym
.get_st_name() >= names_size
)
1493 info
->source_file
= "(invalid)";
1495 info
->source_file
= symbol_names
+ sym
.get_st_name();
1500 unsigned int st_shndx
= this->adjust_sym_shndx(i
, sym
.get_st_shndx(),
1503 && st_shndx
== shndx
1504 && static_cast<off_t
>(sym
.get_st_value()) <= offset
1505 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
1508 if (sym
.get_st_name() > names_size
)
1509 info
->enclosing_symbol_name
= "(invalid)";
1512 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
1513 if (parameters
->options().do_demangle())
1515 char* demangled_name
= cplus_demangle(
1516 info
->enclosing_symbol_name
.c_str(),
1517 DMGL_ANSI
| DMGL_PARAMS
);
1518 if (demangled_name
!= NULL
)
1520 info
->enclosing_symbol_name
.assign(demangled_name
);
1521 free(demangled_name
);
1532 // Look for a kept section corresponding to the given discarded section,
1533 // and return its output address. This is used only for relocations in
1534 // debugging sections. If we can't find the kept section, return 0.
1536 template<int size
, bool big_endian
>
1537 typename Sized_relobj
<size
, big_endian
>::Address
1538 Sized_relobj
<size
, big_endian
>::map_to_kept_section(
1542 Kept_comdat_section
*kept
= this->get_kept_comdat_section(shndx
);
1545 gold_assert(kept
->object_
!= NULL
);
1547 return (static_cast<Address
>
1548 (kept
->object_
->output_section_address(kept
->shndx_
)));
1554 // Input_objects methods.
1556 // Add a regular relocatable object to the list. Return false if this
1557 // object should be ignored.
1560 Input_objects::add_object(Object
* obj
)
1562 // Set the global target from the first object file we recognize.
1563 Target
* target
= obj
->target();
1564 if (!parameters
->target_valid())
1565 set_parameters_target(target
);
1566 else if (target
!= ¶meters
->target())
1568 obj
->error(_("incompatible target"));
1572 // Print the filename if the -t/--trace option is selected.
1573 if (parameters
->options().trace())
1574 gold_info("%s", obj
->name().c_str());
1576 if (!obj
->is_dynamic())
1577 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
1580 // See if this is a duplicate SONAME.
1581 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
1582 const char* soname
= dynobj
->soname();
1584 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
1585 this->sonames_
.insert(soname
);
1588 // We have already seen a dynamic object with this soname.
1592 this->dynobj_list_
.push_back(dynobj
);
1594 // If this is -lc, remember the directory in which we found it.
1595 // We use this when issuing warnings about undefined symbols: as
1596 // a heuristic, we don't warn about system libraries found in
1597 // the same directory as -lc.
1598 if (strncmp(soname
, "libc.so", 7) == 0)
1600 const char* object_name
= dynobj
->name().c_str();
1601 const char* base
= lbasename(object_name
);
1602 if (base
!= object_name
)
1603 this->system_library_directory_
.assign(object_name
,
1604 base
- 1 - object_name
);
1611 // Return whether an object was found in the system library directory.
1614 Input_objects::found_in_system_library_directory(const Object
* object
) const
1616 return (!this->system_library_directory_
.empty()
1617 && object
->name().compare(0,
1618 this->system_library_directory_
.size(),
1619 this->system_library_directory_
) == 0);
1622 // For each dynamic object, record whether we've seen all of its
1623 // explicit dependencies.
1626 Input_objects::check_dynamic_dependencies() const
1628 for (Dynobj_list::const_iterator p
= this->dynobj_list_
.begin();
1629 p
!= this->dynobj_list_
.end();
1632 const Dynobj::Needed
& needed((*p
)->needed());
1633 bool found_all
= true;
1634 for (Dynobj::Needed::const_iterator pneeded
= needed
.begin();
1635 pneeded
!= needed
.end();
1638 if (this->sonames_
.find(*pneeded
) == this->sonames_
.end())
1644 (*p
)->set_has_unknown_needed_entries(!found_all
);
1648 // Relocate_info methods.
1650 // Return a string describing the location of a relocation. This is
1651 // only used in error messages.
1653 template<int size
, bool big_endian
>
1655 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
1657 // See if we can get line-number information from debugging sections.
1658 std::string filename
;
1659 std::string file_and_lineno
; // Better than filename-only, if available.
1661 Sized_dwarf_line_info
<size
, big_endian
> line_info(this->object
);
1662 // This will be "" if we failed to parse the debug info for any reason.
1663 file_and_lineno
= line_info
.addr2line(this->data_shndx
, offset
);
1665 std::string
ret(this->object
->name());
1667 Symbol_location_info info
;
1668 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
1670 ret
+= " in function ";
1671 ret
+= info
.enclosing_symbol_name
;
1673 filename
= info
.source_file
;
1676 if (!file_and_lineno
.empty())
1677 ret
+= file_and_lineno
;
1680 if (!filename
.empty())
1683 ret
+= this->object
->section_name(this->data_shndx
);
1685 // Offsets into sections have to be positive.
1686 snprintf(buf
, sizeof(buf
), "+0x%lx", static_cast<long>(offset
));
1693 } // End namespace gold.
1698 using namespace gold
;
1700 // Read an ELF file with the header and return the appropriate
1701 // instance of Object.
1703 template<int size
, bool big_endian
>
1705 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
1706 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1708 int et
= ehdr
.get_e_type();
1709 if (et
== elfcpp::ET_REL
)
1711 Sized_relobj
<size
, big_endian
>* obj
=
1712 new Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1716 else if (et
== elfcpp::ET_DYN
)
1718 Sized_dynobj
<size
, big_endian
>* obj
=
1719 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1725 gold_error(_("%s: unsupported ELF file type %d"),
1731 } // End anonymous namespace.
1736 // Read an ELF file and return the appropriate instance of Object.
1739 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1740 const unsigned char* p
, section_offset_type bytes
)
1742 if (bytes
< elfcpp::EI_NIDENT
)
1744 gold_error(_("%s: ELF file too short"), name
.c_str());
1748 int v
= p
[elfcpp::EI_VERSION
];
1749 if (v
!= elfcpp::EV_CURRENT
)
1751 if (v
== elfcpp::EV_NONE
)
1752 gold_error(_("%s: invalid ELF version 0"), name
.c_str());
1754 gold_error(_("%s: unsupported ELF version %d"), name
.c_str(), v
);
1758 int c
= p
[elfcpp::EI_CLASS
];
1759 if (c
== elfcpp::ELFCLASSNONE
)
1761 gold_error(_("%s: invalid ELF class 0"), name
.c_str());
1764 else if (c
!= elfcpp::ELFCLASS32
1765 && c
!= elfcpp::ELFCLASS64
)
1767 gold_error(_("%s: unsupported ELF class %d"), name
.c_str(), c
);
1771 int d
= p
[elfcpp::EI_DATA
];
1772 if (d
== elfcpp::ELFDATANONE
)
1774 gold_error(_("%s: invalid ELF data encoding"), name
.c_str());
1777 else if (d
!= elfcpp::ELFDATA2LSB
1778 && d
!= elfcpp::ELFDATA2MSB
)
1780 gold_error(_("%s: unsupported ELF data encoding %d"), name
.c_str(), d
);
1784 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
1786 if (c
== elfcpp::ELFCLASS32
)
1788 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1790 gold_error(_("%s: ELF file too short"), name
.c_str());
1795 #ifdef HAVE_TARGET_32_BIG
1796 elfcpp::Ehdr
<32, true> ehdr(p
);
1797 return make_elf_sized_object
<32, true>(name
, input_file
,
1800 gold_error(_("%s: not configured to support "
1801 "32-bit big-endian object"),
1808 #ifdef HAVE_TARGET_32_LITTLE
1809 elfcpp::Ehdr
<32, false> ehdr(p
);
1810 return make_elf_sized_object
<32, false>(name
, input_file
,
1813 gold_error(_("%s: not configured to support "
1814 "32-bit little-endian object"),
1822 if (bytes
< elfcpp::Elf_sizes
<64>::ehdr_size
)
1824 gold_error(_("%s: ELF file too short"), name
.c_str());
1829 #ifdef HAVE_TARGET_64_BIG
1830 elfcpp::Ehdr
<64, true> ehdr(p
);
1831 return make_elf_sized_object
<64, true>(name
, input_file
,
1834 gold_error(_("%s: not configured to support "
1835 "64-bit big-endian object"),
1842 #ifdef HAVE_TARGET_64_LITTLE
1843 elfcpp::Ehdr
<64, false> ehdr(p
);
1844 return make_elf_sized_object
<64, false>(name
, input_file
,
1847 gold_error(_("%s: not configured to support "
1848 "64-bit little-endian object"),
1856 // Instantiate the templates we need.
1858 #ifdef HAVE_TARGET_32_LITTLE
1861 Object::read_section_data
<32, false>(elfcpp::Elf_file
<32, false, Object
>*,
1862 Read_symbols_data
*);
1865 #ifdef HAVE_TARGET_32_BIG
1868 Object::read_section_data
<32, true>(elfcpp::Elf_file
<32, true, Object
>*,
1869 Read_symbols_data
*);
1872 #ifdef HAVE_TARGET_64_LITTLE
1875 Object::read_section_data
<64, false>(elfcpp::Elf_file
<64, false, Object
>*,
1876 Read_symbols_data
*);
1879 #ifdef HAVE_TARGET_64_BIG
1882 Object::read_section_data
<64, true>(elfcpp::Elf_file
<64, true, Object
>*,
1883 Read_symbols_data
*);
1886 #ifdef HAVE_TARGET_32_LITTLE
1888 class Sized_relobj
<32, false>;
1891 #ifdef HAVE_TARGET_32_BIG
1893 class Sized_relobj
<32, true>;
1896 #ifdef HAVE_TARGET_64_LITTLE
1898 class Sized_relobj
<64, false>;
1901 #ifdef HAVE_TARGET_64_BIG
1903 class Sized_relobj
<64, true>;
1906 #ifdef HAVE_TARGET_32_LITTLE
1908 struct Relocate_info
<32, false>;
1911 #ifdef HAVE_TARGET_32_BIG
1913 struct Relocate_info
<32, true>;
1916 #ifdef HAVE_TARGET_64_LITTLE
1918 struct Relocate_info
<64, false>;
1921 #ifdef HAVE_TARGET_64_BIG
1923 struct Relocate_info
<64, true>;
1926 } // End namespace gold.