1 // object.cc -- support for an object file for linking in gold
3 // Copyright 2006, 2007 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 // Set the target based on fields in the ELF file header.
48 Object::set_target(int machine
, int size
, bool big_endian
, int osabi
,
51 Target
* target
= select_target(machine
, size
, big_endian
, osabi
, abiversion
);
53 gold_fatal(_("%s: unsupported ELF machine number %d"),
54 this->name().c_str(), machine
);
55 this->target_
= target
;
58 // Report an error for this object file. This is used by the
59 // elfcpp::Elf_file interface, and also called by the Object code
63 Object::error(const char* format
, ...) const
66 va_start(args
, format
);
68 if (vasprintf(&buf
, format
, args
) < 0)
71 gold_error(_("%s: %s"), this->name().c_str(), buf
);
75 // Return a view of the contents of a section.
78 Object::section_contents(unsigned int shndx
, off_t
* plen
, bool cache
)
80 Location
loc(this->do_section_contents(shndx
));
81 *plen
= loc
.data_size
;
82 return this->get_view(loc
.file_offset
, loc
.data_size
, cache
);
85 // Read the section data into SD. This is code common to Sized_relobj
86 // and Sized_dynobj, so we put it into Object.
88 template<int size
, bool big_endian
>
90 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
91 Read_symbols_data
* sd
)
93 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
95 // Read the section headers.
96 const off_t shoff
= elf_file
->shoff();
97 const unsigned int shnum
= this->shnum();
98 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
, true);
100 // Read the section names.
101 const unsigned char* pshdrs
= sd
->section_headers
->data();
102 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
103 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
105 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
106 this->error(_("section name section has wrong type: %u"),
107 static_cast<unsigned int>(shdrnames
.get_sh_type()));
109 sd
->section_names_size
= shdrnames
.get_sh_size();
110 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
111 sd
->section_names_size
, false);
114 // If NAME is the name of a special .gnu.warning section, arrange for
115 // the warning to be issued. SHNDX is the section index. Return
116 // whether it is a warning section.
119 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
120 Symbol_table
* symtab
)
122 const char warn_prefix
[] = ".gnu.warning.";
123 const int warn_prefix_len
= sizeof warn_prefix
- 1;
124 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
126 symtab
->add_warning(name
+ warn_prefix_len
, this, shndx
);
132 // Class Sized_relobj.
134 template<int size
, bool big_endian
>
135 Sized_relobj
<size
, big_endian
>::Sized_relobj(
136 const std::string
& name
,
137 Input_file
* input_file
,
139 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
140 : Relobj(name
, input_file
, offset
),
141 elf_file_(this, ehdr
),
143 local_symbol_count_(0),
144 output_local_symbol_count_(0),
146 local_symbol_offset_(0),
148 local_got_offsets_(),
153 template<int size
, bool big_endian
>
154 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
158 // Set up an object file based on the file header. This sets up the
159 // target and reads the section information.
161 template<int size
, bool big_endian
>
163 Sized_relobj
<size
, big_endian
>::setup(
164 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
166 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
167 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
168 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
170 const unsigned int shnum
= this->elf_file_
.shnum();
171 this->set_shnum(shnum
);
174 // Find the SHT_SYMTAB section, given the section headers. The ELF
175 // standard says that maybe in the future there can be more than one
176 // SHT_SYMTAB section. Until somebody figures out how that could
177 // work, we assume there is only one.
179 template<int size
, bool big_endian
>
181 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
183 const unsigned int shnum
= this->shnum();
184 this->symtab_shndx_
= 0;
187 // Look through the sections in reverse order, since gas tends
188 // to put the symbol table at the end.
189 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
190 unsigned int i
= shnum
;
194 p
-= This::shdr_size
;
195 typename
This::Shdr
shdr(p
);
196 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
198 this->symtab_shndx_
= i
;
205 // Return whether SHDR has the right type and flags to be a GNU
206 // .eh_frame section.
208 template<int size
, bool big_endian
>
210 Sized_relobj
<size
, big_endian
>::check_eh_frame_flags(
211 const elfcpp::Shdr
<size
, big_endian
>* shdr
) const
213 return (shdr
->get_sh_size() > 0
214 && shdr
->get_sh_type() == elfcpp::SHT_PROGBITS
215 && shdr
->get_sh_flags() == elfcpp::SHF_ALLOC
);
218 // Return whether there is a GNU .eh_frame section, given the section
219 // headers and the section names.
221 template<int size
, bool big_endian
>
223 Sized_relobj
<size
, big_endian
>::find_eh_frame(const unsigned char* pshdrs
,
225 off_t names_size
) const
227 const unsigned int shnum
= this->shnum();
228 const unsigned char* p
= pshdrs
+ This::shdr_size
;
229 for (unsigned int i
= 1; i
< shnum
; ++i
, p
+= This::shdr_size
)
231 typename
This::Shdr
shdr(p
);
232 if (this->check_eh_frame_flags(&shdr
))
234 if (shdr
.get_sh_name() >= names_size
)
236 this->error(_("bad section name offset for section %u: %lu"),
237 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
241 const char* name
= names
+ shdr
.get_sh_name();
242 if (strcmp(name
, ".eh_frame") == 0)
249 // Read the sections and symbols from an object file.
251 template<int size
, bool big_endian
>
253 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
255 this->read_section_data(&this->elf_file_
, sd
);
257 const unsigned char* const pshdrs
= sd
->section_headers
->data();
259 this->find_symtab(pshdrs
);
261 const unsigned char* namesu
= sd
->section_names
->data();
262 const char* names
= reinterpret_cast<const char*>(namesu
);
263 if (this->find_eh_frame(pshdrs
, names
, sd
->section_names_size
))
264 this->has_eh_frame_
= true;
267 sd
->symbols_size
= 0;
268 sd
->external_symbols_offset
= 0;
269 sd
->symbol_names
= NULL
;
270 sd
->symbol_names_size
= 0;
272 if (this->symtab_shndx_
== 0)
274 // No symbol table. Weird but legal.
278 // Get the symbol table section header.
279 typename
This::Shdr
symtabshdr(pshdrs
280 + this->symtab_shndx_
* This::shdr_size
);
281 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
283 // If this object has a .eh_frame section, we need all the symbols.
284 // Otherwise we only need the external symbols. While it would be
285 // simpler to just always read all the symbols, I've seen object
286 // files with well over 2000 local symbols, which for a 64-bit
287 // object file format is over 5 pages that we don't need to read
290 const int sym_size
= This::sym_size
;
291 const unsigned int loccount
= symtabshdr
.get_sh_info();
292 this->local_symbol_count_
= loccount
;
293 off_t locsize
= loccount
* sym_size
;
294 off_t dataoff
= symtabshdr
.get_sh_offset();
295 off_t datasize
= symtabshdr
.get_sh_size();
296 off_t extoff
= dataoff
+ locsize
;
297 off_t extsize
= datasize
- locsize
;
299 off_t readoff
= this->has_eh_frame_
? dataoff
: extoff
;
300 off_t readsize
= this->has_eh_frame_
? datasize
: extsize
;
302 File_view
* fvsymtab
= this->get_lasting_view(readoff
, readsize
, false);
304 // Read the section header for the symbol names.
305 unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
306 if (strtab_shndx
>= this->shnum())
308 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
311 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
312 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
314 this->error(_("symbol table name section has wrong type: %u"),
315 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
319 // Read the symbol names.
320 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
321 strtabshdr
.get_sh_size(), true);
323 sd
->symbols
= fvsymtab
;
324 sd
->symbols_size
= readsize
;
325 sd
->external_symbols_offset
= this->has_eh_frame_
? locsize
: 0;
326 sd
->symbol_names
= fvstrtab
;
327 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
330 // Return the section index of symbol SYM. Set *VALUE to its value in
331 // the object file. Note that for a symbol which is not defined in
332 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
333 // it will not return the final value of the symbol in the link.
335 template<int size
, bool big_endian
>
337 Sized_relobj
<size
, big_endian
>::symbol_section_and_value(unsigned int sym
,
341 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
345 const size_t count
= symbols_size
/ This::sym_size
;
346 gold_assert(sym
< count
);
348 elfcpp::Sym
<size
, big_endian
> elfsym(symbols
+ sym
* This::sym_size
);
349 *value
= elfsym
.get_st_value();
350 // FIXME: Handle SHN_XINDEX.
351 return elfsym
.get_st_shndx();
354 // Return whether to include a section group in the link. LAYOUT is
355 // used to keep track of which section groups we have already seen.
356 // INDEX is the index of the section group and SHDR is the section
357 // header. If we do not want to include this group, we set bits in
358 // OMIT for each section which should be discarded.
360 template<int size
, bool big_endian
>
362 Sized_relobj
<size
, big_endian
>::include_section_group(
365 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
366 std::vector
<bool>* omit
)
368 // Read the section contents.
369 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
370 shdr
.get_sh_size(), false);
371 const elfcpp::Elf_Word
* pword
=
372 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
374 // The first word contains flags. We only care about COMDAT section
375 // groups. Other section groups are always included in the link
376 // just like ordinary sections.
377 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
378 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
381 // Look up the group signature, which is the name of a symbol. This
382 // is a lot of effort to go to to read a string. Why didn't they
383 // just use the name of the SHT_GROUP section as the group
386 // Get the appropriate symbol table header (this will normally be
387 // the single SHT_SYMTAB section, but in principle it need not be).
388 const unsigned int link
= shdr
.get_sh_link();
389 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
391 // Read the symbol table entry.
392 if (shdr
.get_sh_info() >= symshdr
.get_sh_size() / This::sym_size
)
394 this->error(_("section group %u info %u out of range"),
395 index
, shdr
.get_sh_info());
398 off_t symoff
= symshdr
.get_sh_offset() + shdr
.get_sh_info() * This::sym_size
;
399 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true);
400 elfcpp::Sym
<size
, big_endian
> sym(psym
);
402 // Read the symbol table names.
404 const unsigned char* psymnamesu
;
405 psymnamesu
= this->section_contents(symshdr
.get_sh_link(), &symnamelen
,
407 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
409 // Get the section group signature.
410 if (sym
.get_st_name() >= symnamelen
)
412 this->error(_("symbol %u name offset %u out of range"),
413 shdr
.get_sh_info(), sym
.get_st_name());
417 const char* signature
= psymnames
+ sym
.get_st_name();
419 // It seems that some versions of gas will create a section group
420 // associated with a section symbol, and then fail to give a name to
421 // the section symbol. In such a case, use the name of the section.
424 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
426 secname
= this->section_name(sym
.get_st_shndx());
427 signature
= secname
.c_str();
430 // Record this section group, and see whether we've already seen one
431 // with the same signature.
432 if (layout
->add_comdat(signature
, true))
435 // This is a duplicate. We want to discard the sections in this
437 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
438 for (size_t i
= 1; i
< count
; ++i
)
440 elfcpp::Elf_Word secnum
=
441 elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
);
442 if (secnum
>= this->shnum())
444 this->error(_("section %u in section group %u out of range"),
448 (*omit
)[secnum
] = true;
454 // Whether to include a linkonce section in the link. NAME is the
455 // name of the section and SHDR is the section header.
457 // Linkonce sections are a GNU extension implemented in the original
458 // GNU linker before section groups were defined. The semantics are
459 // that we only include one linkonce section with a given name. The
460 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
461 // where T is the type of section and SYMNAME is the name of a symbol.
462 // In an attempt to make linkonce sections interact well with section
463 // groups, we try to identify SYMNAME and use it like a section group
464 // signature. We want to block section groups with that signature,
465 // but not other linkonce sections with that signature. We also use
466 // the full name of the linkonce section as a normal section group
469 template<int size
, bool big_endian
>
471 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
474 const elfcpp::Shdr
<size
, big_endian
>&)
476 // In general the symbol name we want will be the string following
477 // the last '.'. However, we have to handle the case of
478 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
479 // some versions of gcc. So we use a heuristic: if the name starts
480 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
481 // we look for the last '.'. We can't always simply skip
482 // ".gnu.linkonce.X", because we have to deal with cases like
483 // ".gnu.linkonce.d.rel.ro.local".
484 const char* const linkonce_t
= ".gnu.linkonce.t.";
486 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
487 symname
= name
+ strlen(linkonce_t
);
489 symname
= strrchr(name
, '.') + 1;
490 bool include1
= layout
->add_comdat(symname
, false);
491 bool include2
= layout
->add_comdat(name
, true);
492 return include1
&& include2
;
495 // Lay out the input sections. We walk through the sections and check
496 // whether they should be included in the link. If they should, we
497 // pass them to the Layout object, which will return an output section
500 template<int size
, bool big_endian
>
502 Sized_relobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
504 Read_symbols_data
* sd
)
506 const unsigned int shnum
= this->shnum();
510 // Get the section headers.
511 const unsigned char* pshdrs
= sd
->section_headers
->data();
513 // Get the section names.
514 const unsigned char* pnamesu
= sd
->section_names
->data();
515 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
517 // For each section, record the index of the reloc section if any.
518 // Use 0 to mean that there is no reloc section, -1U to mean that
519 // there is more than one.
520 std::vector
<unsigned int> reloc_shndx(shnum
, 0);
521 std::vector
<unsigned int> reloc_type(shnum
, elfcpp::SHT_NULL
);
522 // Skip the first, dummy, section.
523 pshdrs
+= This::shdr_size
;
524 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
526 typename
This::Shdr
shdr(pshdrs
);
528 unsigned int sh_type
= shdr
.get_sh_type();
529 if (sh_type
== elfcpp::SHT_REL
|| sh_type
== elfcpp::SHT_RELA
)
531 unsigned int target_shndx
= shdr
.get_sh_info();
532 if (target_shndx
== 0 || target_shndx
>= shnum
)
534 this->error(_("relocation section %u has bad info %u"),
539 if (reloc_shndx
[target_shndx
] != 0)
540 reloc_shndx
[target_shndx
] = -1U;
543 reloc_shndx
[target_shndx
] = i
;
544 reloc_type
[target_shndx
] = sh_type
;
549 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
550 map_sections
.resize(shnum
);
552 // Whether we've seen a .note.GNU-stack section.
553 bool seen_gnu_stack
= false;
554 // The flags of a .note.GNU-stack section.
555 uint64_t gnu_stack_flags
= 0;
557 // Keep track of which sections to omit.
558 std::vector
<bool> omit(shnum
, false);
560 // Keep track of .eh_frame sections.
561 std::vector
<unsigned int> eh_frame_sections
;
563 // Skip the first, dummy, section.
564 pshdrs
= sd
->section_headers
->data() + This::shdr_size
;
565 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
567 typename
This::Shdr
shdr(pshdrs
);
569 if (shdr
.get_sh_name() >= sd
->section_names_size
)
571 this->error(_("bad section name offset for section %u: %lu"),
572 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
576 const char* name
= pnames
+ shdr
.get_sh_name();
578 if (this->handle_gnu_warning_section(name
, i
, symtab
))
580 if (!parameters
->output_is_object())
584 // The .note.GNU-stack section is special. It gives the
585 // protection flags that this object file requires for the stack
587 if (strcmp(name
, ".note.GNU-stack") == 0)
589 seen_gnu_stack
= true;
590 gnu_stack_flags
|= shdr
.get_sh_flags();
594 bool discard
= omit
[i
];
597 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
599 if (!this->include_section_group(layout
, i
, shdr
, &omit
))
602 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
603 && Layout::is_linkonce(name
))
605 if (!this->include_linkonce_section(layout
, name
, shdr
))
612 // Do not include this section in the link.
613 map_sections
[i
].output_section
= NULL
;
617 // The .eh_frame section is special. It holds exception frame
618 // information that we need to read in order to generate the
619 // exception frame header. We process these after all the other
620 // sections so that the exception frame reader can reliably
621 // determine which sections are being discarded, and discard the
622 // corresponding information.
623 if (!parameters
->output_is_object()
624 && strcmp(name
, ".eh_frame") == 0
625 && this->check_eh_frame_flags(&shdr
))
627 eh_frame_sections
.push_back(i
);
632 Output_section
* os
= layout
->layout(this, i
, name
, shdr
,
633 reloc_shndx
[i
], reloc_type
[i
],
636 map_sections
[i
].output_section
= os
;
637 map_sections
[i
].offset
= offset
;
639 // If this section requires special handling, and if there are
640 // relocs that apply to it, then we must do the special handling
641 // before we apply the relocs.
642 if (offset
== -1 && reloc_shndx
[i
] != 0)
643 this->set_relocs_must_follow_section_writes();
646 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
);
648 // Handle the .eh_frame sections at the end.
649 for (std::vector
<unsigned int>::const_iterator p
= eh_frame_sections
.begin();
650 p
!= eh_frame_sections
.end();
653 gold_assert(this->has_eh_frame_
);
654 gold_assert(sd
->external_symbols_offset
!= 0);
657 const unsigned char *pshdr
;
658 pshdr
= sd
->section_headers
->data() + i
* This::shdr_size
;
659 typename
This::Shdr
shdr(pshdr
);
662 Output_section
* os
= layout
->layout_eh_frame(this,
665 sd
->symbol_names
->data(),
666 sd
->symbol_names_size
,
671 map_sections
[i
].output_section
= os
;
672 map_sections
[i
].offset
= offset
;
674 // If this section requires special handling, and if there are
675 // relocs that apply to it, then we must do the special handling
676 // before we apply the relocs.
677 if (offset
== -1 && reloc_shndx
[i
] != 0)
678 this->set_relocs_must_follow_section_writes();
681 delete sd
->section_headers
;
682 sd
->section_headers
= NULL
;
683 delete sd
->section_names
;
684 sd
->section_names
= NULL
;
687 // Add the symbols to the symbol table.
689 template<int size
, bool big_endian
>
691 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
692 Read_symbols_data
* sd
)
694 if (sd
->symbols
== NULL
)
696 gold_assert(sd
->symbol_names
== NULL
);
700 const int sym_size
= This::sym_size
;
701 size_t symcount
= ((sd
->symbols_size
- sd
->external_symbols_offset
)
703 if (static_cast<off_t
>(symcount
* sym_size
)
704 != sd
->symbols_size
- sd
->external_symbols_offset
)
706 this->error(_("size of symbols is not multiple of symbol size"));
710 this->symbols_
.resize(symcount
);
712 const char* sym_names
=
713 reinterpret_cast<const char*>(sd
->symbol_names
->data());
714 symtab
->add_from_relobj(this,
715 sd
->symbols
->data() + sd
->external_symbols_offset
,
716 symcount
, sym_names
, sd
->symbol_names_size
,
721 delete sd
->symbol_names
;
722 sd
->symbol_names
= NULL
;
725 // Finalize the local symbols. Here we record the file offset at
726 // which they should be output, we add their names to *POOL, and we
727 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
728 // This function is always called from the main thread. The actual
729 // output of the local symbols will occur in a separate task.
731 template<int size
, bool big_endian
>
733 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
737 gold_assert(this->symtab_shndx_
!= -1U);
738 if (this->symtab_shndx_
== 0)
740 // This object has no symbols. Weird but legal.
744 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
746 this->local_symbol_offset_
= off
;
748 // Read the symbol table section header.
749 const unsigned int symtab_shndx
= this->symtab_shndx_
;
750 typename
This::Shdr
symtabshdr(this,
751 this->elf_file_
.section_header(symtab_shndx
));
752 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
754 // Read the local symbols.
755 const int sym_size
= This::sym_size
;
756 const unsigned int loccount
= this->local_symbol_count_
;
757 gold_assert(loccount
== symtabshdr
.get_sh_info());
758 off_t locsize
= loccount
* sym_size
;
759 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
762 this->local_values_
.resize(loccount
);
764 // Read the symbol names.
765 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
767 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
770 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
772 // Loop over the local symbols.
774 const std::vector
<Map_to_output
>& mo(this->map_to_output());
775 unsigned int shnum
= this->shnum();
776 unsigned int count
= 0;
777 // Skip the first, dummy, symbol.
779 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
781 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
783 Symbol_value
<size
>& lv(this->local_values_
[i
]);
785 unsigned int shndx
= sym
.get_st_shndx();
786 lv
.set_input_shndx(shndx
);
788 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
789 lv
.set_is_section_symbol();
791 if (shndx
>= elfcpp::SHN_LORESERVE
)
793 if (shndx
== elfcpp::SHN_ABS
)
794 lv
.set_output_value(sym
.get_st_value());
797 // FIXME: Handle SHN_XINDEX.
798 this->error(_("unknown section index %u for local symbol %u"),
800 lv
.set_output_value(0);
807 this->error(_("local symbol %u section index %u out of range"),
812 Output_section
* os
= mo
[shndx
].output_section
;
816 lv
.set_output_value(0);
817 lv
.set_no_output_symtab_entry();
821 if (mo
[shndx
].offset
== -1)
822 lv
.set_input_value(sym
.get_st_value());
824 lv
.set_output_value(mo
[shndx
].output_section
->address()
826 + sym
.get_st_value());
829 // Decide whether this symbol should go into the output file.
831 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
833 lv
.set_no_output_symtab_entry();
837 if (sym
.get_st_name() >= strtab_size
)
839 this->error(_("local symbol %u section name out of range: %u >= %u"),
840 i
, sym
.get_st_name(),
841 static_cast<unsigned int>(strtab_size
));
842 lv
.set_no_output_symtab_entry();
846 const char* name
= pnames
+ sym
.get_st_name();
847 pool
->add(name
, true, NULL
);
848 lv
.set_output_symtab_index(index
);
853 this->output_local_symbol_count_
= count
;
858 // Return the value of the local symbol symndx.
859 template<int size
, bool big_endian
>
860 typename
elfcpp::Elf_types
<size
>::Elf_Addr
861 Sized_relobj
<size
, big_endian
>::local_symbol_value(unsigned int symndx
) const
863 gold_assert(symndx
< this->local_symbol_count_
);
864 gold_assert(symndx
< this->local_values_
.size());
865 const Symbol_value
<size
>& lv(this->local_values_
[symndx
]);
866 return lv
.value(this, 0);
869 // Return the value of a local symbol defined in input section SHNDX,
870 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
871 // indicates whether the symbol is a section symbol. This handles
872 // SHF_MERGE sections.
873 template<int size
, bool big_endian
>
874 typename
elfcpp::Elf_types
<size
>::Elf_Addr
875 Sized_relobj
<size
, big_endian
>::local_value(unsigned int shndx
,
877 bool is_section_symbol
,
878 Address addend
) const
880 const std::vector
<Map_to_output
>& mo(this->map_to_output());
881 Output_section
* os
= mo
[shndx
].output_section
;
884 gold_assert(mo
[shndx
].offset
== -1);
886 // Do the mapping required by the output section. If this is not a
887 // section symbol, then we want to map the symbol value, and then
888 // include the addend. If this is a section symbol, then we need to
889 // include the addend to figure out where in the section we are,
890 // before we do the mapping. This will do the right thing provided
891 // the assembler is careful to only convert a relocation in a merged
892 // section to a section symbol if there is a zero addend. If the
893 // assembler does not do this, then in general we can't know what to
894 // do, because we can't distinguish the addend for the instruction
895 // format from the addend for the section offset.
897 if (is_section_symbol
)
898 return os
->output_address(this, shndx
, value
+ addend
);
900 return addend
+ os
->output_address(this, shndx
, value
);
903 // Write out the local symbols.
905 template<int size
, bool big_endian
>
907 Sized_relobj
<size
, big_endian
>::write_local_symbols(Output_file
* of
,
908 const Stringpool
* sympool
)
910 if (parameters
->strip_all())
913 gold_assert(this->symtab_shndx_
!= -1U);
914 if (this->symtab_shndx_
== 0)
916 // This object has no symbols. Weird but legal.
920 // Read the symbol table section header.
921 const unsigned int symtab_shndx
= this->symtab_shndx_
;
922 typename
This::Shdr
symtabshdr(this,
923 this->elf_file_
.section_header(symtab_shndx
));
924 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
925 const unsigned int loccount
= this->local_symbol_count_
;
926 gold_assert(loccount
== symtabshdr
.get_sh_info());
928 // Read the local symbols.
929 const int sym_size
= This::sym_size
;
930 off_t locsize
= loccount
* sym_size
;
931 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
934 // Read the symbol names.
935 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
937 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
940 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
942 // Get a view into the output file.
943 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
944 unsigned char* oview
= of
->get_output_view(this->local_symbol_offset_
,
947 const std::vector
<Map_to_output
>& mo(this->map_to_output());
949 gold_assert(this->local_values_
.size() == loccount
);
951 unsigned char* ov
= oview
;
953 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
955 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
957 if (!this->local_values_
[i
].needs_output_symtab_entry())
960 unsigned int st_shndx
= isym
.get_st_shndx();
961 if (st_shndx
< elfcpp::SHN_LORESERVE
)
963 gold_assert(st_shndx
< mo
.size());
964 if (mo
[st_shndx
].output_section
== NULL
)
966 st_shndx
= mo
[st_shndx
].output_section
->out_shndx();
969 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
971 gold_assert(isym
.get_st_name() < strtab_size
);
972 const char* name
= pnames
+ isym
.get_st_name();
973 osym
.put_st_name(sympool
->get_offset(name
));
974 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
975 osym
.put_st_size(isym
.get_st_size());
976 osym
.put_st_info(isym
.get_st_info());
977 osym
.put_st_other(isym
.get_st_other());
978 osym
.put_st_shndx(st_shndx
);
983 gold_assert(ov
- oview
== output_size
);
985 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
988 // Set *INFO to symbolic information about the offset OFFSET in the
989 // section SHNDX. Return true if we found something, false if we
992 template<int size
, bool big_endian
>
994 Sized_relobj
<size
, big_endian
>::get_symbol_location_info(
997 Symbol_location_info
* info
)
999 if (this->symtab_shndx_
== 0)
1003 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
1007 unsigned int symbol_names_shndx
= this->section_link(this->symtab_shndx_
);
1009 const unsigned char* symbol_names_u
=
1010 this->section_contents(symbol_names_shndx
, &names_size
, false);
1011 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
1013 const int sym_size
= This::sym_size
;
1014 const size_t count
= symbols_size
/ sym_size
;
1016 const unsigned char* p
= symbols
;
1017 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
1019 elfcpp::Sym
<size
, big_endian
> sym(p
);
1021 if (sym
.get_st_type() == elfcpp::STT_FILE
)
1023 if (sym
.get_st_name() >= names_size
)
1024 info
->source_file
= "(invalid)";
1026 info
->source_file
= symbol_names
+ sym
.get_st_name();
1028 else if (sym
.get_st_shndx() == shndx
1029 && static_cast<off_t
>(sym
.get_st_value()) <= offset
1030 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
1033 if (sym
.get_st_name() > names_size
)
1034 info
->enclosing_symbol_name
= "(invalid)";
1037 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
1038 if (parameters
->demangle())
1040 char* demangled_name
= cplus_demangle(
1041 info
->enclosing_symbol_name
.c_str(),
1042 DMGL_ANSI
| DMGL_PARAMS
);
1043 if (demangled_name
!= NULL
)
1045 info
->enclosing_symbol_name
.assign(demangled_name
);
1046 free(demangled_name
);
1057 // Input_objects methods.
1059 // Add a regular relocatable object to the list. Return false if this
1060 // object should be ignored.
1063 Input_objects::add_object(Object
* obj
)
1065 Target
* target
= obj
->target();
1066 if (this->target_
== NULL
)
1067 this->target_
= target
;
1068 else if (this->target_
!= target
)
1070 gold_error(_("%s: incompatible target"), obj
->name().c_str());
1074 if (!obj
->is_dynamic())
1075 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
1078 // See if this is a duplicate SONAME.
1079 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
1080 const char* soname
= dynobj
->soname();
1082 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
1083 this->sonames_
.insert(soname
);
1086 // We have already seen a dynamic object with this soname.
1090 this->dynobj_list_
.push_back(dynobj
);
1092 // If this is -lc, remember the directory in which we found it.
1093 // We use this when issuing warnings about undefined symbols: as
1094 // a heuristic, we don't warn about system libraries found in
1095 // the same directory as -lc.
1096 if (strncmp(soname
, "libc.so", 7) == 0)
1098 const char* object_name
= dynobj
->name().c_str();
1099 const char* base
= lbasename(object_name
);
1100 if (base
!= object_name
)
1101 this->system_library_directory_
.assign(object_name
,
1102 base
- 1 - object_name
);
1106 set_parameters_target(target
);
1111 // Return whether an object was found in the system library directory.
1114 Input_objects::found_in_system_library_directory(const Object
* object
) const
1116 return (!this->system_library_directory_
.empty()
1117 && object
->name().compare(0,
1118 this->system_library_directory_
.size(),
1119 this->system_library_directory_
) == 0);
1122 // For each dynamic object, record whether we've seen all of its
1123 // explicit dependencies.
1126 Input_objects::check_dynamic_dependencies() const
1128 for (Dynobj_list::const_iterator p
= this->dynobj_list_
.begin();
1129 p
!= this->dynobj_list_
.end();
1132 const Dynobj::Needed
& needed((*p
)->needed());
1133 bool found_all
= true;
1134 for (Dynobj::Needed::const_iterator pneeded
= needed
.begin();
1135 pneeded
!= needed
.end();
1138 if (this->sonames_
.find(*pneeded
) == this->sonames_
.end())
1144 (*p
)->set_has_unknown_needed_entries(!found_all
);
1148 // Relocate_info methods.
1150 // Return a string describing the location of a relocation. This is
1151 // only used in error messages.
1153 template<int size
, bool big_endian
>
1155 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
1157 // See if we can get line-number information from debugging sections.
1158 std::string filename
;
1159 std::string file_and_lineno
; // Better than filename-only, if available.
1161 Sized_dwarf_line_info
<size
, big_endian
> line_info(this->object
);
1162 // This will be "" if we failed to parse the debug info for any reason.
1163 file_and_lineno
= line_info
.addr2line(this->data_shndx
, offset
);
1165 std::string
ret(this->object
->name());
1167 Symbol_location_info info
;
1168 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
1170 ret
+= " in function ";
1171 ret
+= info
.enclosing_symbol_name
;
1173 filename
= info
.source_file
;
1176 if (!file_and_lineno
.empty())
1177 ret
+= file_and_lineno
;
1180 if (!filename
.empty())
1183 ret
+= this->object
->section_name(this->data_shndx
);
1185 // Offsets into sections have to be positive.
1186 snprintf(buf
, sizeof(buf
), "+0x%lx", static_cast<long>(offset
));
1193 } // End namespace gold.
1198 using namespace gold
;
1200 // Read an ELF file with the header and return the appropriate
1201 // instance of Object.
1203 template<int size
, bool big_endian
>
1205 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
1206 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
1208 int et
= ehdr
.get_e_type();
1209 if (et
== elfcpp::ET_REL
)
1211 Sized_relobj
<size
, big_endian
>* obj
=
1212 new Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1216 else if (et
== elfcpp::ET_DYN
)
1218 Sized_dynobj
<size
, big_endian
>* obj
=
1219 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
1225 gold_error(_("%s: unsupported ELF file type %d"),
1231 } // End anonymous namespace.
1236 // Read an ELF file and return the appropriate instance of Object.
1239 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1240 const unsigned char* p
, off_t bytes
)
1242 if (bytes
< elfcpp::EI_NIDENT
)
1244 gold_error(_("%s: ELF file too short"), name
.c_str());
1248 int v
= p
[elfcpp::EI_VERSION
];
1249 if (v
!= elfcpp::EV_CURRENT
)
1251 if (v
== elfcpp::EV_NONE
)
1252 gold_error(_("%s: invalid ELF version 0"), name
.c_str());
1254 gold_error(_("%s: unsupported ELF version %d"), name
.c_str(), v
);
1258 int c
= p
[elfcpp::EI_CLASS
];
1259 if (c
== elfcpp::ELFCLASSNONE
)
1261 gold_error(_("%s: invalid ELF class 0"), name
.c_str());
1264 else if (c
!= elfcpp::ELFCLASS32
1265 && c
!= elfcpp::ELFCLASS64
)
1267 gold_error(_("%s: unsupported ELF class %d"), name
.c_str(), c
);
1271 int d
= p
[elfcpp::EI_DATA
];
1272 if (d
== elfcpp::ELFDATANONE
)
1274 gold_error(_("%s: invalid ELF data encoding"), name
.c_str());
1277 else if (d
!= elfcpp::ELFDATA2LSB
1278 && d
!= elfcpp::ELFDATA2MSB
)
1280 gold_error(_("%s: unsupported ELF data encoding %d"), name
.c_str(), d
);
1284 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
1286 if (c
== elfcpp::ELFCLASS32
)
1288 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1290 gold_error(_("%s: ELF file too short"), name
.c_str());
1295 #ifdef HAVE_TARGET_32_BIG
1296 elfcpp::Ehdr
<32, true> ehdr(p
);
1297 return make_elf_sized_object
<32, true>(name
, input_file
,
1300 gold_error(_("%s: not configured to support "
1301 "32-bit big-endian object"),
1308 #ifdef HAVE_TARGET_32_LITTLE
1309 elfcpp::Ehdr
<32, false> ehdr(p
);
1310 return make_elf_sized_object
<32, false>(name
, input_file
,
1313 gold_error(_("%s: not configured to support "
1314 "32-bit little-endian object"),
1322 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1324 gold_error(_("%s: ELF file too short"), name
.c_str());
1329 #ifdef HAVE_TARGET_64_BIG
1330 elfcpp::Ehdr
<64, true> ehdr(p
);
1331 return make_elf_sized_object
<64, true>(name
, input_file
,
1334 gold_error(_("%s: not configured to support "
1335 "64-bit big-endian object"),
1342 #ifdef HAVE_TARGET_64_LITTLE
1343 elfcpp::Ehdr
<64, false> ehdr(p
);
1344 return make_elf_sized_object
<64, false>(name
, input_file
,
1347 gold_error(_("%s: not configured to support "
1348 "64-bit little-endian object"),
1356 // Instantiate the templates we need. We could use the configure
1357 // script to restrict this to only the ones for implemented targets.
1359 #ifdef HAVE_TARGET_32_LITTLE
1361 class Sized_relobj
<32, false>;
1364 #ifdef HAVE_TARGET_32_BIG
1366 class Sized_relobj
<32, true>;
1369 #ifdef HAVE_TARGET_64_LITTLE
1371 class Sized_relobj
<64, false>;
1374 #ifdef HAVE_TARGET_64_BIG
1376 class Sized_relobj
<64, true>;
1379 #ifdef HAVE_TARGET_32_LITTLE
1381 struct Relocate_info
<32, false>;
1384 #ifdef HAVE_TARGET_32_BIG
1386 struct Relocate_info
<32, true>;
1389 #ifdef HAVE_TARGET_64_LITTLE
1391 struct Relocate_info
<64, false>;
1394 #ifdef HAVE_TARGET_64_BIG
1396 struct Relocate_info
<64, true>;
1399 } // End namespace gold.