1 // object.cc -- support for an object file for linking in gold
10 #include "target-select.h"
19 Object::get_view(off_t start
, off_t size
)
21 return this->input_file_
->file().get_view(start
+ this->offset_
, size
);
25 Object::read(off_t start
, off_t size
, void* p
)
27 this->input_file_
->file().read(start
+ this->offset_
, size
, p
);
31 Object::get_lasting_view(off_t start
, off_t size
)
33 return this->input_file_
->file().get_lasting_view(start
+ this->offset_
,
37 // Class Sized_object.
39 template<int size
, bool big_endian
>
40 Sized_object
<size
, big_endian
>::Sized_object(
41 const std::string
& name
,
42 Input_file
* input_file
,
44 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
45 : Object(name
, input_file
, false, offset
),
46 flags_(ehdr
.get_e_flags()),
47 shoff_(ehdr
.get_e_shoff()),
52 if (ehdr
.get_e_ehsize() != This::ehdr_size
)
54 fprintf(stderr
, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
55 program_name
, this->name().c_str(), ehdr
.get_e_ehsize(),
59 if (ehdr
.get_e_shentsize() != This::shdr_size
)
61 fprintf(stderr
, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
62 program_name
, this->name().c_str(), ehdr
.get_e_shentsize(),
68 template<int size
, bool big_endian
>
69 Sized_object
<size
, big_endian
>::~Sized_object()
73 // Set up an object file bsaed on the file header. This sets up the
74 // target and reads the section information.
76 template<int size
, bool big_endian
>
78 Sized_object
<size
, big_endian
>::setup(
79 const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
81 int machine
= ehdr
.get_e_machine();
82 Target
* target
= select_target(machine
, size
, big_endian
,
83 ehdr
.get_e_ident()[elfcpp::EI_OSABI
],
84 ehdr
.get_e_ident()[elfcpp::EI_ABIVERSION
]);
87 fprintf(stderr
, _("%s: %s: unsupported ELF machine number %d\n"),
88 program_name
, this->name().c_str(), machine
);
91 this->set_target(target
);
92 unsigned int shnum
= ehdr
.get_e_shnum();
93 unsigned int shstrndx
= ehdr
.get_e_shstrndx();
94 if ((shnum
== 0 || shstrndx
== elfcpp::SHN_XINDEX
)
97 const unsigned char* p
= this->get_view (this->shoff_
, This::shdr_size
);
98 elfcpp::Shdr
<size
, big_endian
> shdr(p
);
100 shnum
= shdr
.get_sh_size();
101 if (shstrndx
== elfcpp::SHN_XINDEX
)
102 shstrndx
= shdr
.get_sh_link();
104 this->set_shnum(shnum
);
105 this->shstrndx_
= shstrndx
;
110 // Find the SHT_SYMTAB section.
111 const unsigned char* p
= this->get_view (this->shoff_
,
112 shnum
* This::shdr_size
);
113 // Skip the first section, which is always empty.
114 p
+= This::shdr_size
;
115 for (unsigned int i
= 1; i
< shnum
; ++i
)
117 elfcpp::Shdr
<size
, big_endian
> shdr(p
);
118 if (shdr
.get_sh_type() == elfcpp::SHT_SYMTAB
)
120 this->symtab_shnum_
= i
;
123 p
+= This::shdr_size
;
127 // Read the symbols and relocations from an object file.
129 template<int size
, bool big_endian
>
131 Sized_object
<size
, big_endian
>::do_read_symbols()
133 if (this->symtab_shnum_
== 0)
135 // No symbol table. Weird but legal.
136 Read_symbols_data ret
;
138 ret
.symbols_size
= 0;
139 ret
.first_global
= 0;
140 ret
.symbol_names
= NULL
;
141 ret
.symbol_names_size
= 0;
145 const int shdr_size
= This::shdr_size
;
147 // Read the symbol table section header.
148 off_t symtabshdroff
= this->shoff_
+ (this->symtab_shnum_
* shdr_size
);
149 const unsigned char* psymtabshdr
= this->get_view(symtabshdroff
, shdr_size
);
150 elfcpp::Shdr
<size
, big_endian
> symtabshdr(psymtabshdr
);
151 assert(symtabshdr
.get_sh_type() == elfcpp::SHT_SYMTAB
);
153 // Read the symbol table.
154 File_view
* fvsymtab
= this->get_lasting_view(symtabshdr
.get_sh_offset(),
155 symtabshdr
.get_sh_size());
157 // Read the section header for the symbol names.
158 unsigned int strtab_shnum
= symtabshdr
.get_sh_link();
159 if (strtab_shnum
== 0 || strtab_shnum
>= this->shnum())
161 fprintf(stderr
, _("%s: %s: invalid symbol table name index: %u\n"),
162 program_name
, this->name().c_str(), strtab_shnum
);
165 off_t strtabshdroff
= this->shoff_
+ (strtab_shnum
* shdr_size
);
166 const unsigned char *pstrtabshdr
= this->get_view(strtabshdroff
, shdr_size
);
167 elfcpp::Shdr
<size
, big_endian
> strtabshdr(pstrtabshdr
);
168 if (strtabshdr
.get_sh_type() != elfcpp::SHT_STRTAB
)
171 _("%s: %s: symbol table name section has wrong type: %u\n"),
172 program_name
, this->name().c_str(),
173 static_cast<unsigned int>(strtabshdr
.get_sh_type()));
177 // Read the symbol names.
178 File_view
* fvstrtab
= this->get_lasting_view(strtabshdr
.get_sh_offset(),
179 strtabshdr
.get_sh_size());
181 Read_symbols_data ret
;
182 ret
.symbols
= fvsymtab
;
183 ret
.symbols_size
= symtabshdr
.get_sh_size();
184 ret
.first_global
= symtabshdr
.get_sh_info();
185 ret
.symbol_names
= fvstrtab
;
186 ret
.symbol_names_size
= strtabshdr
.get_sh_size();
191 // Add the symbols to the symbol table.
193 template<int size
, bool big_endian
>
195 Sized_object
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
196 Read_symbols_data sd
)
198 if (sd
.symbols
== NULL
)
200 assert(sd
.symbol_names
== NULL
);
204 const int sym_size
= This::sym_size
;
205 size_t symcount
= sd
.symbols_size
/ sym_size
;
206 if (symcount
* sym_size
!= sd
.symbols_size
)
209 _("%s: %s: size of symbols is not multiple of symbol size\n"),
210 program_name
, this->name().c_str());
214 const char* sym_names
=
215 reinterpret_cast<const char*>(sd
.symbol_names
->data());
217 // We only add the global symbols to the symbol table.
218 if (symcount
> sd
.first_global
)
220 this->symbols_
= new Symbol
*[symcount
- sd
.first_global
];
222 const unsigned char* symdata
= sd
.symbols
->data();
223 symdata
+= sd
.first_global
* sym_size
;
224 const elfcpp::Sym
<size
, big_endian
>* syms
=
225 reinterpret_cast<const elfcpp::Sym
<size
, big_endian
>*>(symdata
);
227 symtab
->add_from_object(this, syms
, symcount
- sd
.first_global
,
228 sym_names
, sd
.symbol_names_size
, this->symbols_
);
231 // Add the names of the local symbols. FIXME: We shouldn't do this
232 // if we are stripping symbols.
233 const elfcpp::Sym
<size
, big_endian
>* local_syms
=
234 reinterpret_cast<const elfcpp::Sym
<size
, big_endian
>*>(sd
.symbols
->data());
235 symtab
->add_local_symbol_names(this, local_syms
, sd
.first_global
,
236 sym_names
, sd
.symbol_names_size
);
239 delete sd
.symbol_names
;
242 // Return whether to include a section group in the link. LAYOUT is
243 // used to keep track of which section groups we have already seen.
244 // INDEX is the index of the section group and SHDR is the section
245 // header. If we do not want to include this group, we set bits in
246 // OMIT for each section which should be discarded.
248 template<int size
, bool big_endian
>
250 Sized_object
<size
, big_endian
>::include_section_group(
253 const elfcpp::Shdr
<size
, big_endian
>& shdr
,
254 std::vector
<bool>* omit
)
256 // Read the section contents.
257 const unsigned char* pcon
= this->get_view(shdr
.get_sh_offset(),
259 const elfcpp::Elf_Word
* pword
=
260 reinterpret_cast<const elfcpp::Elf_Word
*>(pcon
);
262 // The first word contains flags. We only care about COMDAT section
263 // groups. Other section groups are always included in the link
264 // just like ordinary sections.
265 elfcpp::Elf_Word flags
= elfcpp::read_elf_word
<big_endian
>(pword
);
266 if ((flags
& elfcpp::GRP_COMDAT
) == 0)
269 // Look up the group signature, which is the name of a symbol. This
270 // is a lot of effort to go to to read a string. Why didn't they
271 // just use the name of the SHT_GROUP section as the group
274 // Get the appropriate symbol table header (this will normally be
275 // the single SHT_SYMTAB section, but in principle it need not be).
276 if (shdr
.get_sh_link() >= this->shnum())
278 fprintf(stderr
, _("%s: %s: section group %u link %u out of range\n"),
279 program_name
, this->name().c_str(), index
, shdr
.get_sh_link());
282 off_t off
= this->shoff_
+ shdr
.get_sh_link() * This::shdr_size
;
283 const unsigned char* psymshdr
= this->get_view(off
, This::shdr_size
);
284 elfcpp::Shdr
<size
, big_endian
> symshdr(psymshdr
);
286 // Read the symbol table entry.
287 if (shdr
.get_sh_info() >= symshdr
.get_sh_size() / This::sym_size
)
289 fprintf(stderr
, _("%s: %s: section group %u info %u out of range\n"),
290 program_name
, this->name().c_str(), index
, shdr
.get_sh_info());
293 off_t symoff
= symshdr
.get_sh_offset() + shdr
.get_sh_info() * This::sym_size
;
294 const unsigned char* psym
= this->get_view(symoff
, This::sym_size
);
295 elfcpp::Sym
<size
, big_endian
> sym(psym
);
297 // Read the section header for the symbol table names.
298 if (symshdr
.get_sh_link() >= this->shnum())
300 fprintf(stderr
, _("%s; %s: symtab section %u link %u out of range\n"),
301 program_name
, this->name().c_str(), shdr
.get_sh_link(),
302 symshdr
.get_sh_link());
305 off_t symnameoff
= this->shoff_
+ symshdr
.get_sh_link() * This::shdr_size
;
306 const unsigned char* psymnamehdr
= this->get_view(symnameoff
,
308 elfcpp::Shdr
<size
, big_endian
> symnamehdr(psymnamehdr
);
310 // Read the symbol table names.
311 const unsigned char *psymnamesu
= this->get_view(symnamehdr
.get_sh_offset(),
312 symnamehdr
.get_sh_size());
313 const char* psymnames
= reinterpret_cast<const char*>(psymnamesu
);
315 // Get the section group signature.
316 if (sym
.get_st_name() >= symnamehdr
.get_sh_size())
318 fprintf(stderr
, _("%s: %s: symbol %u name offset %u out of range\n"),
319 program_name
, this->name().c_str(), shdr
.get_sh_info(),
324 const char* signature
= psymnames
+ sym
.get_st_name();
326 // Record this section group, and see whether we've already seen one
327 // with the same signature.
328 if (layout
->add_comdat(signature
, true))
331 // This is a duplicate. We want to discard the sections in this
333 size_t count
= shdr
.get_sh_size() / sizeof(elfcpp::Elf_Word
);
334 for (size_t i
= 1; i
< count
; ++i
)
336 elfcpp::Elf_Word secnum
= elfcpp::read_elf_word
<big_endian
>(pword
+ i
);
337 if (secnum
>= this->shnum())
340 _("%s: %s: section %u in section group %u out of range"),
341 program_name
, this->name().c_str(), secnum
,
345 (*omit
)[secnum
] = true;
351 // Whether to include a linkonce section in the link. NAME is the
352 // name of the section and SHDR is the section header.
354 // Linkonce sections are a GNU extension implemented in the original
355 // GNU linker before section groups were defined. The semantics are
356 // that we only include one linkonce section with a given name. The
357 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
358 // where T is the type of section and SYMNAME is the name of a symbol.
359 // In an attempt to make linkonce sections interact well with section
360 // groups, we try to identify SYMNAME and use it like a section group
361 // signature. We want to block section groups with that signature,
362 // but not other linkonce sections with that signature. We also use
363 // the full name of the linkonce section as a normal section group
366 template<int size
, bool big_endian
>
368 Sized_object
<size
, big_endian
>::include_linkonce_section(
371 const elfcpp::Shdr
<size
, big_endian
>&)
373 const char* symname
= strrchr(name
, '.') + 1;
374 bool omit1
= layout
->add_comdat(symname
, false);
375 bool omit2
= layout
->add_comdat(name
, true);
376 return omit1
|| omit2
;
379 // Lay out the input sections. We walk through the sections and check
380 // whether they should be included in the link. If they should, we
381 // pass them to the Layout object, which will return an output section
384 template<int size
, bool big_endian
>
386 Sized_object
<size
, big_endian
>::do_layout(Layout
* layout
)
388 // This is always called from the main thread. Lock the file to
389 // keep the error checks happy.
390 Task_locker_obj
<File_read
> frl(this->input_file()->file());
392 // Get the section headers.
393 unsigned int shnum
= this->shnum();
394 const unsigned char* pshdrs
= this->get_view(this->shoff_
,
395 shnum
* This::shdr_size
);
397 // Get the section names.
398 const unsigned char* pshdrnames
= pshdrs
+ this->shstrndx_
* This::shdr_size
;
399 elfcpp::Shdr
<size
, big_endian
> shdrnames(pshdrnames
);
400 typename
elfcpp::Elf_types
<size
>::Elf_WXword names_size
=
401 shdrnames
.get_sh_size();
402 const unsigned char* pnamesu
= this->get_view(shdrnames
.get_sh_offset(),
403 shdrnames
.get_sh_size());
404 const char* pnames
= reinterpret_cast<const char*>(pnamesu
);
406 std::vector
<Map_to_output
>& map_sections(this->map_to_output());
407 map_sections
.reserve(shnum
);
409 // Keep track of which sections to omit.
410 std::vector
<bool> omit(shnum
, false);
412 for (unsigned int i
= 0; i
< shnum
; ++i
)
414 elfcpp::Shdr
<size
, big_endian
> shdr(pshdrs
);
416 if (shdr
.get_sh_name() >= names_size
)
419 _("%s: %s: bad section name offset for section %u: %lu\n"),
420 program_name
, this->name().c_str(), i
,
421 static_cast<unsigned long>(shdr
.get_sh_name()));
425 const char* name
= pnames
+ shdr
.get_sh_name();
427 bool discard
= omit
[i
];
430 if (shdr
.get_sh_type() == elfcpp::SHT_GROUP
)
432 if (!this->include_section_group(layout
, i
, shdr
, &omit
))
435 else if (Layout::is_linkonce(name
))
437 if (!this->include_linkonce_section(layout
, name
, shdr
))
444 // Do not include this section in the link.
445 map_sections
[i
].output_section
= NULL
;
450 Output_section
* os
= layout
->layout(this, name
, shdr
, &offset
);
452 map_sections
[i
].output_section
= os
;
453 map_sections
[i
].offset
= offset
;
455 pshdrs
+= This::shdr_size
;
459 // Input_objects methods.
462 Input_objects::add_object(Object
* obj
)
464 this->object_list_
.push_back(obj
);
465 if (obj
->is_dynamic())
466 this->any_dynamic_
= true;
469 } // End namespace gold.
474 using namespace gold
;
476 // Read an ELF file with the header and return the appropriate
477 // instance of Object.
479 template<int size
, bool big_endian
>
481 make_elf_sized_object(const std::string
& name
, Input_file
* input_file
,
482 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
484 int et
= ehdr
.get_e_type();
485 if (et
!= elfcpp::ET_REL
&& et
!= elfcpp::ET_DYN
)
487 fprintf(stderr
, "%s: %s: unsupported ELF type %d\n",
488 program_name
, name
.c_str(), static_cast<int>(et
));
492 if (et
== elfcpp::ET_REL
)
494 Sized_object
<size
, big_endian
>* obj
=
495 new Sized_object
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
502 fprintf(stderr
, _("%s: %s: dynamic objects are not yet supported\n"),
503 program_name
, name
.c_str());
505 // Sized_dynobj<size, big_endian>* obj =
506 // new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
513 } // End anonymous namespace.
518 // Read an ELF file and return the appropriate instance of Object.
521 make_elf_object(const std::string
& name
, Input_file
* input_file
, off_t offset
,
522 const unsigned char* p
, off_t bytes
)
524 if (bytes
< elfcpp::EI_NIDENT
)
526 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
527 program_name
, name
.c_str());
531 int v
= p
[elfcpp::EI_VERSION
];
532 if (v
!= elfcpp::EV_CURRENT
)
534 if (v
== elfcpp::EV_NONE
)
535 fprintf(stderr
, _("%s: %s: invalid ELF version 0\n"),
536 program_name
, name
.c_str());
538 fprintf(stderr
, _("%s: %s: unsupported ELF version %d\n"),
539 program_name
, name
.c_str(), v
);
543 int c
= p
[elfcpp::EI_CLASS
];
544 if (c
== elfcpp::ELFCLASSNONE
)
546 fprintf(stderr
, _("%s: %s: invalid ELF class 0\n"),
547 program_name
, name
.c_str());
550 else if (c
!= elfcpp::ELFCLASS32
551 && c
!= elfcpp::ELFCLASS64
)
553 fprintf(stderr
, _("%s: %s: unsupported ELF class %d\n"),
554 program_name
, name
.c_str(), c
);
558 int d
= p
[elfcpp::EI_DATA
];
559 if (d
== elfcpp::ELFDATANONE
)
561 fprintf(stderr
, _("%s: %s: invalid ELF data encoding\n"),
562 program_name
, name
.c_str());
565 else if (d
!= elfcpp::ELFDATA2LSB
566 && d
!= elfcpp::ELFDATA2MSB
)
568 fprintf(stderr
, _("%s: %s: unsupported ELF data encoding %d\n"),
569 program_name
, name
.c_str(), d
);
573 bool big_endian
= d
== elfcpp::ELFDATA2MSB
;
575 if (c
== elfcpp::ELFCLASS32
)
577 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
579 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
580 program_name
, name
.c_str());
585 elfcpp::Ehdr
<32, true> ehdr(p
);
586 return make_elf_sized_object
<32, true>(name
, input_file
,
591 elfcpp::Ehdr
<32, false> ehdr(p
);
592 return make_elf_sized_object
<32, false>(name
, input_file
,
598 if (bytes
< elfcpp::Elf_sizes
<32>::ehdr_size
)
600 fprintf(stderr
, _("%s: %s: ELF file too short\n"),
601 program_name
, name
.c_str());
606 elfcpp::Ehdr
<64, true> ehdr(p
);
607 return make_elf_sized_object
<64, true>(name
, input_file
,
612 elfcpp::Ehdr
<64, false> ehdr(p
);
613 return make_elf_sized_object
<64, false>(name
, input_file
,
619 // Instantiate the templates we need. We could use the configure
620 // script to restrict this to only the ones for implemented targets.
623 class Sized_object
<32, false>;
626 class Sized_object
<32, true>;
629 class Sized_object
<64, false>;
632 class Sized_object
<64, true>;
634 } // End namespace gold.