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 "target-select.h"
30 #include "dwarf_reader.h"
42 // Set the target based on fields in the ELF file header.
45 Object::set_target(int machine
, int size
, bool big_endian
, int osabi
,
48 Target
* target
= select_target(machine
, size
, big_endian
, osabi
, abiversion
);
50 gold_fatal(_("%s: unsupported ELF machine number %d"),
51 this->name().c_str(), machine
);
52 this->target_
= target
;
55 // Report an error for this object file. This is used by the
56 // elfcpp::Elf_file interface, and also called by the Object code
60 Object::error(const char* format
, ...) const
63 va_start(args
, format
);
65 if (vasprintf(&buf
, format
, args
) < 0)
68 gold_error(_("%s: %s"), this->name().c_str(), buf
);
72 // Return a view of the contents of a section.
75 Object::section_contents(unsigned int shndx
, off_t
* plen
, bool cache
)
77 Location
loc(this->do_section_contents(shndx
));
78 *plen
= loc
.data_size
;
79 return this->get_view(loc
.file_offset
, loc
.data_size
, cache
);
82 // Read the section data into SD. This is code common to Sized_relobj
83 // and Sized_dynobj, so we put it into Object.
85 template<int size
, bool big_endian
>
87 Object::read_section_data(elfcpp::Elf_file
<size
, big_endian
, Object
>* elf_file
,
88 Read_symbols_data
* sd
)
90 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
92 // Read the section headers.
93 const off_t shoff
= elf_file
->shoff();
94 const unsigned int shnum
= this->shnum();
95 sd
->section_headers
= this->get_lasting_view(shoff
, shnum
* shdr_size
, true);
97 // Read the section names.
98 const unsigned char* pshdrs
= sd
->section_headers
->data();
99 const unsigned char* pshdrnames
= pshdrs
+ elf_file
->shstrndx() * shdr_size
;
100 typename
elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
102 if (shdrnames
.get_sh_type() != elfcpp::SHT_STRTAB
)
103 this->error(_("section name section has wrong type: %u"),
104 static_cast<unsigned int>(shdrnames
.get_sh_type()));
106 sd
->section_names_size
= shdrnames
.get_sh_size();
107 sd
->section_names
= this->get_lasting_view(shdrnames
.get_sh_offset(),
108 sd
->section_names_size
, false);
111 // If NAME is the name of a special .gnu.warning section, arrange for
112 // the warning to be issued. SHNDX is the section index. Return
113 // whether it is a warning section.
116 Object::handle_gnu_warning_section(const char* name
, unsigned int shndx
,
117 Symbol_table
* symtab
)
119 const char warn_prefix
[] = ".gnu.warning.";
120 const int warn_prefix_len
= sizeof warn_prefix
- 1;
121 if (strncmp(name
, warn_prefix
, warn_prefix_len
) == 0)
123 symtab
->add_warning(name
+ warn_prefix_len
, this, shndx
);
129 // Class Sized_relobj.
131 template<int size
, bool big_endian
>
132 Sized_relobj
<size
, big_endian
>::Sized_relobj(
133 const std::string
& name
,
134 Input_file
* input_file
,
136 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
137 : Relobj(name
, input_file
, offset
),
138 elf_file_(this, ehdr
),
140 local_symbol_count_(0),
141 output_local_symbol_count_(0),
143 local_symbol_offset_(0),
149 template<int size
, bool big_endian
>
150 Sized_relobj
<size
, big_endian
>::~Sized_relobj()
154 // Set up an object file based on the file header. This sets up the
155 // target and reads the section information.
157 template<int size
, bool big_endian
>
159 Sized_relobj
<size
, big_endian
>::setup(
160 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
162 this->set_target(ehdr
.get_e_machine(), size
, big_endian
,
163 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
164 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
166 const unsigned int shnum
= this->elf_file_
.shnum();
167 this->set_shnum(shnum
);
170 // Find the SHT_SYMTAB section, given the section headers. The ELF
171 // standard says that maybe in the future there can be more than one
172 // SHT_SYMTAB section. Until somebody figures out how that could
173 // work, we assume there is only one.
175 template<int size
, bool big_endian
>
177 Sized_relobj
<size
, big_endian
>::find_symtab(const unsigned char* pshdrs
)
179 const unsigned int shnum
= this->shnum();
180 this->symtab_shndx_
= 0;
183 // Look through the sections in reverse order, since gas tends
184 // to put the symbol table at the end.
185 const unsigned char* p
= pshdrs
+ shnum
* This::shdr_size
;
186 unsigned int i
= shnum
;
190 p
-= This::shdr_size
;
191 typename
This::Shdr
shdr(p
);
192 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
194 this->symtab_shndx_
= i
;
201 // Read the sections and symbols from an object file.
203 template<int size
, bool big_endian
>
205 Sized_relobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
* sd
)
207 this->read_section_data(&this->elf_file_
, sd
);
209 const unsigned char* const pshdrs
= sd
->section_headers
->data();
211 this->find_symtab(pshdrs
);
214 sd
->symbols_size
= 0;
215 sd
->symbol_names
= NULL
;
216 sd
->symbol_names_size
= 0;
218 if (this->symtab_shndx_
== 0)
220 // No symbol table. Weird but legal.
224 // Get the symbol table section header.
225 typename
This::Shdr
symtabshdr(pshdrs
226 + this->symtab_shndx_
* This::shdr_size
);
227 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
229 // We only need the external symbols.
230 const int sym_size
= This::sym_size
;
231 const unsigned int loccount
= symtabshdr
.get_sh_info();
232 this->local_symbol_count_
= loccount
;
233 off_t locsize
= loccount
* sym_size
;
234 off_t extoff
= symtabshdr
.get_sh_offset() + locsize
;
235 off_t extsize
= symtabshdr
.get_sh_size() - locsize
;
237 // Read the symbol table.
238 File_view
* fvsymtab
= this->get_lasting_view(extoff
, extsize
, false);
240 // Read the section header for the symbol names.
241 unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
242 if (strtab_shndx
>= this->shnum())
244 this->error(_("invalid symbol table name index: %u"), strtab_shndx
);
247 typename
This::Shdr
strtabshdr(pshdrs
+ strtab_shndx
* This::shdr_size
);
248 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
250 this->error(_("symbol table name section has wrong type: %u"),
251 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
255 // Read the symbol names.
256 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
257 strtabshdr
.get_sh_size(), true);
259 sd
->symbols
= fvsymtab
;
260 sd
->symbols_size
= extsize
;
261 sd
->symbol_names
= fvstrtab
;
262 sd
->symbol_names_size
= strtabshdr
.get_sh_size();
265 // Return whether to include a section group in the link. LAYOUT is
266 // used to keep track of which section groups we have already seen.
267 // INDEX is the index of the section group and SHDR is the section
268 // header. If we do not want to include this group, we set bits in
269 // OMIT for each section which should be discarded.
271 template<int size
, bool big_endian
>
273 Sized_relobj
<size
, big_endian
>::include_section_group(
276 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
277 std::vector
<bool>* omit
)
279 // Read the section contents.
280 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
281 shdr
.get_sh_size(), false);
282 const elfcpp::Elf_Word
* pword
=
283 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
285 // The first word contains flags. We only care about COMDAT section
286 // groups. Other section groups are always included in the link
287 // just like ordinary sections.
288 elfcpp::Elf_Word flags
= elfcpp::Swap
<32, big_endian
>::readval(pword
);
289 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
292 // Look up the group signature, which is the name of a symbol. This
293 // is a lot of effort to go to to read a string. Why didn't they
294 // just use the name of the SHT_GROUP section as the group
297 // Get the appropriate symbol table header (this will normally be
298 // the single SHT_SYMTAB section, but in principle it need not be).
299 const unsigned int link
= shdr
.get_sh_link();
300 typename
This::Shdr
symshdr(this, this->elf_file_
.section_header(link
));
302 // Read the symbol table entry.
303 if (shdr
.get_sh_info() >= symshdr
.get_sh_size() / This::sym_size
)
305 this->error(_("section group %u info %u out of range"),
306 index
, shdr
.get_sh_info());
309 off_t symoff
= symshdr
.get_sh_offset() + shdr
.get_sh_info() * This::sym_size
;
310 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
, true);
311 elfcpp::Sym
<size
, big_endian
> sym(psym
);
313 // Read the symbol table names.
315 const unsigned char* psymnamesu
;
316 psymnamesu
= this->section_contents(symshdr
.get_sh_link(), &symnamelen
,
318 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
320 // Get the section group signature.
321 if (sym
.get_st_name() >= symnamelen
)
323 this->error(_("symbol %u name offset %u out of range"),
324 shdr
.get_sh_info(), sym
.get_st_name());
328 const char* signature
= psymnames
+ sym
.get_st_name();
330 // It seems that some versions of gas will create a section group
331 // associated with a section symbol, and then fail to give a name to
332 // the section symbol. In such a case, use the name of the section.
335 if (signature
[0] == '\0' && sym
.get_st_type() == elfcpp::STT_SECTION
)
337 secname
= this->section_name(sym
.get_st_shndx());
338 signature
= secname
.c_str();
341 // Record this section group, and see whether we've already seen one
342 // with the same signature.
343 if (layout
->add_comdat(signature
, true))
346 // This is a duplicate. We want to discard the sections in this
348 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
349 for (size_t i
= 1; i
< count
; ++i
)
351 elfcpp::Elf_Word secnum
=
352 elfcpp::Swap
<32, big_endian
>::readval(pword
+ i
);
353 if (secnum
>= this->shnum())
355 this->error(_("section %u in section group %u out of range"),
359 (*omit
)[secnum
] = true;
365 // Whether to include a linkonce section in the link. NAME is the
366 // name of the section and SHDR is the section header.
368 // Linkonce sections are a GNU extension implemented in the original
369 // GNU linker before section groups were defined. The semantics are
370 // that we only include one linkonce section with a given name. The
371 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
372 // where T is the type of section and SYMNAME is the name of a symbol.
373 // In an attempt to make linkonce sections interact well with section
374 // groups, we try to identify SYMNAME and use it like a section group
375 // signature. We want to block section groups with that signature,
376 // but not other linkonce sections with that signature. We also use
377 // the full name of the linkonce section as a normal section group
380 template<int size
, bool big_endian
>
382 Sized_relobj
<size
, big_endian
>::include_linkonce_section(
385 const elfcpp::Shdr
<size
, big_endian
>&)
387 // In general the symbol name we want will be the string following
388 // the last '.'. However, we have to handle the case of
389 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
390 // some versions of gcc. So we use a heuristic: if the name starts
391 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
392 // we look for the last '.'. We can't always simply skip
393 // ".gnu.linkonce.X", because we have to deal with cases like
394 // ".gnu.linkonce.d.rel.ro.local".
395 const char* const linkonce_t
= ".gnu.linkonce.t.";
397 if (strncmp(name
, linkonce_t
, strlen(linkonce_t
)) == 0)
398 symname
= name
+ strlen(linkonce_t
);
400 symname
= strrchr(name
, '.') + 1;
401 bool include1
= layout
->add_comdat(symname
, false);
402 bool include2
= layout
->add_comdat(name
, true);
403 return include1
&& include2
;
406 // Lay out the input sections. We walk through the sections and check
407 // whether they should be included in the link. If they should, we
408 // pass them to the Layout object, which will return an output section
411 template<int size
, bool big_endian
>
413 Sized_relobj
<size
, big_endian
>::do_layout(Symbol_table
* symtab
,
415 Read_symbols_data
* sd
)
417 const unsigned int shnum
= this->shnum();
421 // Get the section headers.
422 const unsigned char* pshdrs
= sd
->section_headers
->data();
424 // Get the section names.
425 const unsigned char* pnamesu
= sd
->section_names
->data();
426 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
428 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
429 map_sections
.resize(shnum
);
431 // Whether we've seen a .note.GNU-stack section.
432 bool seen_gnu_stack
= false;
433 // The flags of a .note.GNU-stack section.
434 uint64_t gnu_stack_flags
= 0;
436 // Keep track of which sections to omit.
437 std::vector
<bool> omit(shnum
, false);
439 // Skip the first, dummy, section.
440 pshdrs
+= This::shdr_size
;
441 for (unsigned int i
= 1; i
< shnum
; ++i
, pshdrs
+= This::shdr_size
)
443 typename
This::Shdr
shdr(pshdrs
);
445 if (shdr
.get_sh_name() >= sd
->section_names_size
)
447 this->error(_("bad section name offset for section %u: %lu"),
448 i
, static_cast<unsigned long>(shdr
.get_sh_name()));
452 const char* name
= pnames
+ shdr
.get_sh_name();
454 if (this->handle_gnu_warning_section(name
, i
, symtab
))
456 if (!parameters
->output_is_object())
460 // The .note.GNU-stack section is special. It gives the
461 // protection flags that this object file requires for the stack
463 if (strcmp(name
, ".note.GNU-stack") == 0)
465 seen_gnu_stack
= true;
466 gnu_stack_flags
|= shdr
.get_sh_flags();
470 bool discard
= omit
[i
];
473 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
475 if (!this->include_section_group(layout
, i
, shdr
, &omit
))
478 else if ((shdr
.get_sh_flags() & elfcpp::SHF_GROUP
) == 0
479 && Layout::is_linkonce(name
))
481 if (!this->include_linkonce_section(layout
, name
, shdr
))
488 // Do not include this section in the link.
489 map_sections
[i
].output_section
= NULL
;
494 Output_section
* os
= layout
->layout(this, i
, name
, shdr
, &offset
);
496 map_sections
[i
].output_section
= os
;
497 map_sections
[i
].offset
= offset
;
500 layout
->layout_gnu_stack(seen_gnu_stack
, gnu_stack_flags
);
502 delete sd
->section_headers
;
503 sd
->section_headers
= NULL
;
504 delete sd
->section_names
;
505 sd
->section_names
= NULL
;
508 // Add the symbols to the symbol table.
510 template<int size
, bool big_endian
>
512 Sized_relobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
513 Read_symbols_data
* sd
)
515 if (sd
->symbols
== NULL
)
517 gold_assert(sd
->symbol_names
== NULL
);
521 const int sym_size
= This::sym_size
;
522 size_t symcount
= sd
->symbols_size
/ sym_size
;
523 if (static_cast<off_t
>(symcount
* sym_size
) != sd
->symbols_size
)
525 this->error(_("size of symbols is not multiple of symbol size"));
529 this->symbols_
= new Symbol
*[symcount
];
531 const char* sym_names
=
532 reinterpret_cast<const char*>(sd
->symbol_names
->data());
533 symtab
->add_from_relobj(this, sd
->symbols
->data(), symcount
, sym_names
,
534 sd
->symbol_names_size
, this->symbols_
);
538 delete sd
->symbol_names
;
539 sd
->symbol_names
= NULL
;
542 // Finalize the local symbols. Here we record the file offset at
543 // which they should be output, we add their names to *POOL, and we
544 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
545 // This function is always called from the main thread. The actual
546 // output of the local symbols will occur in a separate task.
548 template<int size
, bool big_endian
>
550 Sized_relobj
<size
, big_endian
>::do_finalize_local_symbols(unsigned int index
,
554 gold_assert(this->symtab_shndx_
!= -1U);
555 if (this->symtab_shndx_
== 0)
557 // This object has no symbols. Weird but legal.
561 gold_assert(off
== static_cast<off_t
>(align_address(off
, size
>> 3)));
563 this->local_symbol_offset_
= off
;
565 // Read the symbol table section header.
566 const unsigned int symtab_shndx
= this->symtab_shndx_
;
567 typename
This::Shdr
symtabshdr(this,
568 this->elf_file_
.section_header(symtab_shndx
));
569 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
571 // Read the local symbols.
572 const int sym_size
= This::sym_size
;
573 const unsigned int loccount
= this->local_symbol_count_
;
574 gold_assert(loccount
== symtabshdr
.get_sh_info());
575 off_t locsize
= loccount
* sym_size
;
576 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
579 this->local_values_
.resize(loccount
);
581 // Read the symbol names.
582 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
584 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
587 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
589 // Loop over the local symbols.
591 const std::vector
<Map_to_output
>& mo(this->map_to_output());
592 unsigned int shnum
= this->shnum();
593 unsigned int count
= 0;
594 // Skip the first, dummy, symbol.
596 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
598 elfcpp::Sym
<size
, big_endian
> sym(psyms
);
600 Symbol_value
<size
>& lv(this->local_values_
[i
]);
602 unsigned int shndx
= sym
.get_st_shndx();
603 lv
.set_input_shndx(shndx
);
605 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
606 lv
.set_is_section_symbol();
608 if (shndx
>= elfcpp::SHN_LORESERVE
)
610 if (shndx
== elfcpp::SHN_ABS
)
611 lv
.set_output_value(sym
.get_st_value());
614 // FIXME: Handle SHN_XINDEX.
615 this->error(_("unknown section index %u for local symbol %u"),
617 lv
.set_output_value(0);
624 this->error(_("local symbol %u section index %u out of range"),
629 Output_section
* os
= mo
[shndx
].output_section
;
633 lv
.set_output_value(0);
634 lv
.set_no_output_symtab_entry();
638 if (mo
[shndx
].offset
== -1)
639 lv
.set_input_value(sym
.get_st_value());
641 lv
.set_output_value(mo
[shndx
].output_section
->address()
643 + sym
.get_st_value());
646 // Decide whether this symbol should go into the output file.
648 if (sym
.get_st_type() == elfcpp::STT_SECTION
)
650 lv
.set_no_output_symtab_entry();
654 if (sym
.get_st_name() >= strtab_size
)
656 this->error(_("local symbol %u section name out of range: %u >= %u"),
657 i
, sym
.get_st_name(),
658 static_cast<unsigned int>(strtab_size
));
659 lv
.set_no_output_symtab_entry();
663 const char* name
= pnames
+ sym
.get_st_name();
664 pool
->add(name
, true, NULL
);
665 lv
.set_output_symtab_index(index
);
670 this->output_local_symbol_count_
= count
;
675 // Return the value of the local symbol symndx.
676 template<int size
, bool big_endian
>
677 typename
elfcpp::Elf_types
<size
>::Elf_Addr
678 Sized_relobj
<size
, big_endian
>::local_symbol_value(unsigned int symndx
) const
680 gold_assert(symndx
< this->local_symbol_count_
);
681 gold_assert(symndx
< this->local_values_
.size());
682 const Symbol_value
<size
>& lv(this->local_values_
[symndx
]);
683 return lv
.value(this, 0);
686 // Return the value of a local symbol defined in input section SHNDX,
687 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
688 // indicates whether the symbol is a section symbol. This handles
689 // SHF_MERGE sections.
690 template<int size
, bool big_endian
>
691 typename
elfcpp::Elf_types
<size
>::Elf_Addr
692 Sized_relobj
<size
, big_endian
>::local_value(unsigned int shndx
,
694 bool is_section_symbol
,
695 Address addend
) const
697 const std::vector
<Map_to_output
>& mo(this->map_to_output());
698 Output_section
* os
= mo
[shndx
].output_section
;
701 gold_assert(mo
[shndx
].offset
== -1);
703 // Do the mapping required by the output section. If this is not a
704 // section symbol, then we want to map the symbol value, and then
705 // include the addend. If this is a section symbol, then we need to
706 // include the addend to figure out where in the section we are,
707 // before we do the mapping. This will do the right thing provided
708 // the assembler is careful to only convert a relocation in a merged
709 // section to a section symbol if there is a zero addend. If the
710 // assembler does not do this, then in general we can't know what to
711 // do, because we can't distinguish the addend for the instruction
712 // format from the addend for the section offset.
714 if (is_section_symbol
)
715 return os
->output_address(this, shndx
, value
+ addend
);
717 return addend
+ os
->output_address(this, shndx
, value
);
720 // Write out the local symbols.
722 template<int size
, bool big_endian
>
724 Sized_relobj
<size
, big_endian
>::write_local_symbols(Output_file
* of
,
725 const Stringpool
* sympool
)
727 if (parameters
->strip_all())
730 gold_assert(this->symtab_shndx_
!= -1U);
731 if (this->symtab_shndx_
== 0)
733 // This object has no symbols. Weird but legal.
737 // Read the symbol table section header.
738 const unsigned int symtab_shndx
= this->symtab_shndx_
;
739 typename
This::Shdr
symtabshdr(this,
740 this->elf_file_
.section_header(symtab_shndx
));
741 gold_assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
742 const unsigned int loccount
= this->local_symbol_count_
;
743 gold_assert(loccount
== symtabshdr
.get_sh_info());
745 // Read the local symbols.
746 const int sym_size
= This::sym_size
;
747 off_t locsize
= loccount
* sym_size
;
748 const unsigned char* psyms
= this->get_view(symtabshdr
.get_sh_offset(),
751 // Read the symbol names.
752 const unsigned int strtab_shndx
= symtabshdr
.get_sh_link();
754 const unsigned char* pnamesu
= this->section_contents(strtab_shndx
,
757 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
759 // Get a view into the output file.
760 off_t output_size
= this->output_local_symbol_count_
* sym_size
;
761 unsigned char* oview
= of
->get_output_view(this->local_symbol_offset_
,
764 const std::vector
<Map_to_output
>& mo(this->map_to_output());
766 gold_assert(this->local_values_
.size() == loccount
);
768 unsigned char* ov
= oview
;
770 for (unsigned int i
= 1; i
< loccount
; ++i
, psyms
+= sym_size
)
772 elfcpp::Sym
<size
, big_endian
> isym(psyms
);
774 if (!this->local_values_
[i
].needs_output_symtab_entry())
777 unsigned int st_shndx
= isym
.get_st_shndx();
778 if (st_shndx
< elfcpp::SHN_LORESERVE
)
780 gold_assert(st_shndx
< mo
.size());
781 if (mo
[st_shndx
].output_section
== NULL
)
783 st_shndx
= mo
[st_shndx
].output_section
->out_shndx();
786 elfcpp::Sym_write
<size
, big_endian
> osym(ov
);
788 gold_assert(isym
.get_st_name() < strtab_size
);
789 const char* name
= pnames
+ isym
.get_st_name();
790 osym
.put_st_name(sympool
->get_offset(name
));
791 osym
.put_st_value(this->local_values_
[i
].value(this, 0));
792 osym
.put_st_size(isym
.get_st_size());
793 osym
.put_st_info(isym
.get_st_info());
794 osym
.put_st_other(isym
.get_st_other());
795 osym
.put_st_shndx(st_shndx
);
800 gold_assert(ov
- oview
== output_size
);
802 of
->write_output_view(this->local_symbol_offset_
, output_size
, oview
);
805 // Set *INFO to symbolic information about the offset OFFSET in the
806 // section SHNDX. Return true if we found something, false if we
809 template<int size
, bool big_endian
>
811 Sized_relobj
<size
, big_endian
>::get_symbol_location_info(
814 Symbol_location_info
* info
)
816 if (this->symtab_shndx_
== 0)
820 const unsigned char* symbols
= this->section_contents(this->symtab_shndx_
,
824 unsigned int symbol_names_shndx
= this->section_link(this->symtab_shndx_
);
826 const unsigned char* symbol_names_u
=
827 this->section_contents(symbol_names_shndx
, &names_size
, false);
828 const char* symbol_names
= reinterpret_cast<const char*>(symbol_names_u
);
830 const int sym_size
= This::sym_size
;
831 const size_t count
= symbols_size
/ sym_size
;
833 const unsigned char* p
= symbols
;
834 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
836 elfcpp::Sym
<size
, big_endian
> sym(p
);
838 if (sym
.get_st_type() == elfcpp::STT_FILE
)
840 if (sym
.get_st_name() >= names_size
)
841 info
->source_file
= "(invalid)";
843 info
->source_file
= symbol_names
+ sym
.get_st_name();
845 else if (sym
.get_st_shndx() == shndx
846 && static_cast<off_t
>(sym
.get_st_value()) <= offset
847 && (static_cast<off_t
>(sym
.get_st_value() + sym
.get_st_size())
850 if (sym
.get_st_name() > names_size
)
851 info
->enclosing_symbol_name
= "(invalid)";
853 info
->enclosing_symbol_name
= symbol_names
+ sym
.get_st_name();
861 // Input_objects methods.
863 // Add a regular relocatable object to the list. Return false if this
864 // object should be ignored.
867 Input_objects::add_object(Object
* obj
)
869 Target
* target
= obj
->target();
870 if (this->target_
== NULL
)
871 this->target_
= target
;
872 else if (this->target_
!= target
)
874 gold_error(_("%s: incompatible target"), obj
->name().c_str());
878 if (!obj
->is_dynamic())
879 this->relobj_list_
.push_back(static_cast<Relobj
*>(obj
));
882 // See if this is a duplicate SONAME.
883 Dynobj
* dynobj
= static_cast<Dynobj
*>(obj
);
885 std::pair
<Unordered_set
<std::string
>::iterator
, bool> ins
=
886 this->sonames_
.insert(dynobj
->soname());
889 // We have already seen a dynamic object with this soname.
893 this->dynobj_list_
.push_back(dynobj
);
896 set_parameters_size_and_endianness(target
->get_size(),
897 target
->is_big_endian());
902 // Relocate_info methods.
904 // Return a string describing the location of a relocation. This is
905 // only used in error messages.
907 template<int size
, bool big_endian
>
909 Relocate_info
<size
, big_endian
>::location(size_t, off_t offset
) const
911 // See if we can get line-number information from debugging sections.
912 std::string filename
;
913 std::string file_and_lineno
; // Better than filename-only, if available.
914 for (unsigned int shndx
= 0; shndx
< this->object
->shnum(); ++shndx
)
915 if (this->object
->section_name(shndx
) == ".debug_line")
917 off_t debuglines_size
;
918 const unsigned char* debuglines
= this->object
->section_contents(
919 shndx
, &debuglines_size
, false);
922 Dwarf_line_info
<size
, big_endian
> line_info(debuglines
,
924 line_info
.read_line_mappings();
925 file_and_lineno
= line_info
.addr2line(this->data_shndx
, offset
);
930 std::string
ret(this->object
->name());
932 Symbol_location_info info
;
933 if (this->object
->get_symbol_location_info(this->data_shndx
, offset
, &info
))
935 ret
+= " in function ";
936 // We could demangle this name before printing, but we don't
937 // bother because gcc runs linker output through a demangle
938 // filter itself. The only advantage to demangling here is if
939 // someone might call ld directly, rather than via gcc. If we
940 // did want to demangle, cplus_demangle() is in libiberty.
941 ret
+= info
.enclosing_symbol_name
;
943 filename
= info
.source_file
;
946 if (!file_and_lineno
.empty())
947 ret
+= file_and_lineno
;
950 if (!filename
.empty())
953 ret
+= this->object
->section_name(this->data_shndx
);
955 // Offsets into sections have to be positive.
956 snprintf(buf
, sizeof(buf
), "+0x%lx", static_cast<long>(offset
));
963 } // End namespace gold.
968 using namespace gold
;
970 // Read an ELF file with the header and return the appropriate
971 // instance of Object.
973 template<int size
, bool big_endian
>
975 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
976 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
978 int et
= ehdr
.get_e_type();
979 if (et
== elfcpp::ET_REL
)
981 Sized_relobj
<size
, big_endian
>* obj
=
982 new Sized_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
986 else if (et
== elfcpp::ET_DYN
)
988 Sized_dynobj
<size
, big_endian
>* obj
=
989 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
995 gold_error(_("%s: unsupported ELF file type %d"),
1001 } // End anonymous namespace.
1006 // Read an ELF file and return the appropriate instance of Object.
1009 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
1010 const unsigned char* p
, off_t bytes
)
1012 if (bytes
< elfcpp::EI_NIDENT
)
1014 gold_error(_("%s: ELF file too short"), name
.c_str());
1018 int v
= p
[elfcpp::EI_VERSION
];
1019 if (v
!= elfcpp::EV_CURRENT
)
1021 if (v
== elfcpp::EV_NONE
)
1022 gold_error(_("%s: invalid ELF version 0"), name
.c_str());
1024 gold_error(_("%s: unsupported ELF version %d"), name
.c_str(), v
);
1028 int c
= p
[elfcpp::EI_CLASS
];
1029 if (c
== elfcpp::ELFCLASSNONE
)
1031 gold_error(_("%s: invalid ELF class 0"), name
.c_str());
1034 else if (c
!= elfcpp::ELFCLASS32
1035 && c
!= elfcpp::ELFCLASS64
)
1037 gold_error(_("%s: unsupported ELF class %d"), name
.c_str(), c
);
1041 int d
= p
[elfcpp::EI_DATA
];
1042 if (d
== elfcpp::ELFDATANONE
)
1044 gold_error(_("%s: invalid ELF data encoding"), name
.c_str());
1047 else if (d
!= elfcpp::ELFDATA2LSB
1048 && d
!= elfcpp::ELFDATA2MSB
)
1050 gold_error(_("%s: unsupported ELF data encoding %d"), name
.c_str(), d
);
1054 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
1056 if (c
== elfcpp::ELFCLASS32
)
1058 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1060 gold_error(_("%s: ELF file too short"), name
.c_str());
1065 #ifdef HAVE_TARGET_32_BIG
1066 elfcpp::Ehdr
<32, true> ehdr(p
);
1067 return make_elf_sized_object
<32, true>(name
, input_file
,
1070 gold_error(_("%s: not configured to support "
1071 "32-bit big-endian object"),
1078 #ifdef HAVE_TARGET_32_LITTLE
1079 elfcpp::Ehdr
<32, false> ehdr(p
);
1080 return make_elf_sized_object
<32, false>(name
, input_file
,
1083 gold_error(_("%s: not configured to support "
1084 "32-bit little-endian object"),
1092 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
1094 gold_error(_("%s: ELF file too short"), name
.c_str());
1099 #ifdef HAVE_TARGET_64_BIG
1100 elfcpp::Ehdr
<64, true> ehdr(p
);
1101 return make_elf_sized_object
<64, true>(name
, input_file
,
1104 gold_error(_("%s: not configured to support "
1105 "64-bit big-endian object"),
1112 #ifdef HAVE_TARGET_64_LITTLE
1113 elfcpp::Ehdr
<64, false> ehdr(p
);
1114 return make_elf_sized_object
<64, false>(name
, input_file
,
1117 gold_error(_("%s: not configured to support "
1118 "64-bit little-endian object"),
1126 // Instantiate the templates we need. We could use the configure
1127 // script to restrict this to only the ones for implemented targets.
1129 #ifdef HAVE_TARGET_32_LITTLE
1131 class Sized_relobj
<32, false>;
1134 #ifdef HAVE_TARGET_32_BIG
1136 class Sized_relobj
<32, true>;
1139 #ifdef HAVE_TARGET_64_LITTLE
1141 class Sized_relobj
<64, false>;
1144 #ifdef HAVE_TARGET_64_BIG
1146 class Sized_relobj
<64, true>;
1149 #ifdef HAVE_TARGET_32_LITTLE
1151 struct Relocate_info
<32, false>;
1154 #ifdef HAVE_TARGET_32_BIG
1156 struct Relocate_info
<32, true>;
1159 #ifdef HAVE_TARGET_64_LITTLE
1161 struct Relocate_info
<64, false>;
1164 #ifdef HAVE_TARGET_64_BIG
1166 struct Relocate_info
<64, true>;
1169 } // End namespace gold.