2006-09-27 Dave Brolley <brolley@redhat.com>
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
3#include "gold.h"
4
5#include <cerrno>
6#include <cstring>
7#include <cassert>
8
9#include "object.h"
14bfc3f5 10#include "target-select.h"
a2fb1b05 11#include "layout.h"
bae7f79e
ILT
12
13namespace gold
14{
15
16// Class Object.
17
18const unsigned char*
19Object::get_view(off_t start, off_t size)
20{
21 return this->input_file_->file().get_view(start + this->offset_, size);
22}
23
24void
25Object::read(off_t start, off_t size, void* p)
26{
27 this->input_file_->file().read(start + this->offset_, size, p);
28}
29
30File_view*
31Object::get_lasting_view(off_t start, off_t size)
32{
33 return this->input_file_->file().get_lasting_view(start + this->offset_,
34 size);
35}
36
37// Class Sized_object.
38
39template<int size, bool big_endian>
40Sized_object<size, big_endian>::Sized_object(
41 const std::string& name,
42 Input_file* input_file,
43 off_t offset,
44 const elfcpp::Ehdr<size, big_endian>& ehdr)
14bfc3f5 45 : Object(name, input_file, false, offset),
bae7f79e 46 flags_(ehdr.get_e_flags()),
bae7f79e 47 shoff_(ehdr.get_e_shoff()),
bae7f79e 48 shstrndx_(0),
14bfc3f5
ILT
49 symtab_shnum_(0),
50 symbols_(NULL)
bae7f79e 51{
a2fb1b05 52 if (ehdr.get_e_ehsize() != This::ehdr_size)
bae7f79e
ILT
53 {
54 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
55 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
a2fb1b05 56 This::ehdr_size);
bae7f79e
ILT
57 gold_exit(false);
58 }
a2fb1b05 59 if (ehdr.get_e_shentsize() != This::shdr_size)
bae7f79e
ILT
60 {
61 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
62 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
a2fb1b05 63 This::shdr_size);
bae7f79e
ILT
64 gold_exit(false);
65 }
66}
67
68template<int size, bool big_endian>
69Sized_object<size, big_endian>::~Sized_object()
70{
71}
72
73// Set up an object file bsaed on the file header. This sets up the
74// target and reads the section information.
75
76template<int size, bool big_endian>
77void
78Sized_object<size, big_endian>::setup(
79 const elfcpp::Ehdr<size, big_endian>& ehdr)
80{
a2fb1b05
ILT
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]);
14bfc3f5
ILT
85 if (target == NULL)
86 {
87 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
a2fb1b05 88 program_name, this->name().c_str(), machine);
14bfc3f5
ILT
89 gold_exit(false);
90 }
91 this->set_target(target);
bae7f79e
ILT
92 unsigned int shnum = ehdr.get_e_shnum();
93 unsigned int shstrndx = ehdr.get_e_shstrndx();
94 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
95 && this->shoff_ != 0)
96 {
a2fb1b05 97 const unsigned char* p = this->get_view (this->shoff_, This::shdr_size);
bae7f79e
ILT
98 elfcpp::Shdr<size, big_endian> shdr(p);
99 if (shnum == 0)
100 shnum = shdr.get_sh_size();
101 if (shstrndx == elfcpp::SHN_XINDEX)
102 shstrndx = shdr.get_sh_link();
103 }
a2fb1b05 104 this->set_shnum(shnum);
bae7f79e
ILT
105 this->shstrndx_ = shstrndx;
106
107 if (shnum == 0)
108 return;
109
110 // Find the SHT_SYMTAB section.
a2fb1b05
ILT
111 const unsigned char* p = this->get_view (this->shoff_,
112 shnum * This::shdr_size);
bae7f79e 113 // Skip the first section, which is always empty.
a2fb1b05 114 p += This::shdr_size;
bae7f79e
ILT
115 for (unsigned int i = 1; i < shnum; ++i)
116 {
117 elfcpp::Shdr<size, big_endian> shdr(p);
118 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
119 {
120 this->symtab_shnum_ = i;
121 break;
122 }
a2fb1b05 123 p += This::shdr_size;
bae7f79e
ILT
124 }
125}
126
127// Read the symbols and relocations from an object file.
128
129template<int size, bool big_endian>
130Read_symbols_data
131Sized_object<size, big_endian>::do_read_symbols()
132{
133 if (this->symtab_shnum_ == 0)
134 {
135 // No symbol table. Weird but legal.
136 Read_symbols_data ret;
137 ret.symbols = NULL;
138 ret.symbols_size = 0;
54dc6425 139 ret.first_global = 0;
bae7f79e
ILT
140 ret.symbol_names = NULL;
141 ret.symbol_names_size = 0;
142 return ret;
143 }
144
a2fb1b05 145 const int shdr_size = This::shdr_size;
bae7f79e
ILT
146
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);
152
153 // Read the symbol table.
54dc6425
ILT
154 File_view* fvsymtab = this->get_lasting_view(symtabshdr.get_sh_offset(),
155 symtabshdr.get_sh_size());
bae7f79e
ILT
156
157 // Read the section header for the symbol names.
158 unsigned int strtab_shnum = symtabshdr.get_sh_link();
a2fb1b05 159 if (strtab_shnum == 0 || strtab_shnum >= this->shnum())
bae7f79e
ILT
160 {
161 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
162 program_name, this->name().c_str(), strtab_shnum);
163 gold_exit(false);
164 }
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)
169 {
170 fprintf(stderr,
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()));
174 gold_exit(false);
175 }
176
177 // Read the symbol names.
178 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
179 strtabshdr.get_sh_size());
180
181 Read_symbols_data ret;
182 ret.symbols = fvsymtab;
54dc6425
ILT
183 ret.symbols_size = symtabshdr.get_sh_size();
184 ret.first_global = symtabshdr.get_sh_info();
bae7f79e
ILT
185 ret.symbol_names = fvstrtab;
186 ret.symbol_names_size = strtabshdr.get_sh_size();
187
188 return ret;
189}
190
191// Add the symbols to the symbol table.
192
193template<int size, bool big_endian>
194void
14bfc3f5
ILT
195Sized_object<size, big_endian>::do_add_symbols(Symbol_table* symtab,
196 Read_symbols_data sd)
bae7f79e
ILT
197{
198 if (sd.symbols == NULL)
199 {
200 assert(sd.symbol_names == NULL);
201 return;
202 }
203
a2fb1b05 204 const int sym_size = This::sym_size;
14bfc3f5
ILT
205 size_t symcount = sd.symbols_size / sym_size;
206 if (symcount * sym_size != sd.symbols_size)
bae7f79e 207 {
14bfc3f5
ILT
208 fprintf(stderr,
209 _("%s: %s: size of symbols is not multiple of symbol size\n"),
210 program_name, this->name().c_str());
211 gold_exit(false);
bae7f79e 212 }
14bfc3f5 213
14bfc3f5
ILT
214 const char* sym_names =
215 reinterpret_cast<const char*>(sd.symbol_names->data());
54dc6425
ILT
216
217 // We only add the global symbols to the symbol table.
218 if (symcount > sd.first_global)
219 {
220 this->symbols_ = new Symbol*[symcount - sd.first_global];
221
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);
226
227 symtab->add_from_object(this, syms, symcount - sd.first_global,
228 sym_names, sd.symbol_names_size, this->symbols_);
229 }
230
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);
a2fb1b05
ILT
237
238 delete sd.symbols;
239 delete sd.symbol_names;
240}
241
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.
247
248template<int size, bool big_endian>
249bool
250Sized_object<size, big_endian>::include_section_group(
251 Layout* layout,
252 unsigned int index,
253 const elfcpp::Shdr<size, big_endian>& shdr,
254 std::vector<bool>* omit)
255{
256 // Read the section contents.
257 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
258 shdr.get_sh_size());
259 const elfcpp::Elf_Word* pword =
260 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
261
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)
267 return true;
268
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
272 // signature?
273
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())
277 {
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());
280 gold_exit(false);
281 }
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);
285
286 // Read the symbol table entry.
287 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
288 {
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());
291 gold_exit(false);
292 }
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);
296
297 // Read the section header for the symbol table names.
298 if (symshdr.get_sh_link() >= this->shnum())
299 {
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());
303 gold_exit(false);
304 }
305 off_t symnameoff = this->shoff_ + symshdr.get_sh_link() * This::shdr_size;
306 const unsigned char* psymnamehdr = this->get_view(symnameoff,
307 This::shdr_size);
308 elfcpp::Shdr<size, big_endian> symnamehdr(psymnamehdr);
309
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);
314
315 // Get the section group signature.
316 if (sym.get_st_name() >= symnamehdr.get_sh_size())
317 {
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(),
320 sym.get_st_name());
321 gold_exit(false);
322 }
323
324 const char* signature = psymnames + sym.get_st_name();
325
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))
329 return true;
330
331 // This is a duplicate. We want to discard the sections in this
332 // group.
333 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
334 for (size_t i = 1; i < count; ++i)
335 {
336 elfcpp::Elf_Word secnum = elfcpp::read_elf_word<big_endian>(pword + i);
337 if (secnum >= this->shnum())
338 {
339 fprintf(stderr,
340 _("%s: %s: section %u in section group %u out of range"),
341 program_name, this->name().c_str(), secnum,
342 index);
343 gold_exit(false);
344 }
345 (*omit)[secnum] = true;
346 }
347
348 return false;
349}
350
351// Whether to include a linkonce section in the link. NAME is the
352// name of the section and SHDR is the section header.
353
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
364// signature.
365
366template<int size, bool big_endian>
367bool
368Sized_object<size, big_endian>::include_linkonce_section(
369 Layout* layout,
370 const char* name,
371 const elfcpp::Shdr<size, big_endian>&)
372{
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;
377}
378
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
382// and an offset.
383
384template<int size, bool big_endian>
385void
386Sized_object<size, big_endian>::do_layout(Layout* layout)
387{
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());
391
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);
396
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);
405
406 std::vector<Map_to_output>& map_sections(this->map_to_output());
407 map_sections.reserve(shnum);
408
409 // Keep track of which sections to omit.
410 std::vector<bool> omit(shnum, false);
411
412 for (unsigned int i = 0; i < shnum; ++i)
413 {
414 elfcpp::Shdr<size, big_endian> shdr(pshdrs);
415
416 if (shdr.get_sh_name() >= names_size)
417 {
418 fprintf(stderr,
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()));
422 gold_exit(false);
423 }
424
425 const char* name = pnames + shdr.get_sh_name();
426
427 bool discard = omit[i];
428 if (!discard)
429 {
430 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
431 {
432 if (!this->include_section_group(layout, i, shdr, &omit))
433 discard = true;
434 }
435 else if (Layout::is_linkonce(name))
436 {
437 if (!this->include_linkonce_section(layout, name, shdr))
438 discard = true;
439 }
440 }
441
442 if (discard)
443 {
444 // Do not include this section in the link.
445 map_sections[i].output_section = NULL;
446 continue;
447 }
448
449 off_t offset;
450 Output_section* os = layout->layout(this, name, shdr, &offset);
451
452 map_sections[i].output_section = os;
453 map_sections[i].offset = offset;
454
455 pshdrs += This::shdr_size;
456 }
bae7f79e
ILT
457}
458
54dc6425
ILT
459// Input_objects methods.
460
461void
462Input_objects::add_object(Object* obj)
463{
464 this->object_list_.push_back(obj);
465 if (obj->is_dynamic())
466 this->any_dynamic_ = true;
467}
468
bae7f79e
ILT
469} // End namespace gold.
470
471namespace
472{
473
474using namespace gold;
475
476// Read an ELF file with the header and return the appropriate
477// instance of Object.
478
479template<int size, bool big_endian>
480Object*
481make_elf_sized_object(const std::string& name, Input_file* input_file,
482 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
483{
484 int et = ehdr.get_e_type();
485 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
486 {
487 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
488 program_name, name.c_str(), static_cast<int>(et));
489 gold_exit(false);
490 }
491
492 if (et == elfcpp::ET_REL)
493 {
494 Sized_object<size, big_endian>* obj =
495 new Sized_object<size, big_endian>(name, input_file, offset, ehdr);
496 obj->setup(ehdr);
497 return obj;
498 }
499 else
500 {
501 // elfcpp::ET_DYN
502 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
503 program_name, name.c_str());
504 gold_exit(false);
505// Sized_dynobj<size, big_endian>* obj =
506// new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
507// offset, ehdr);
508// obj->setup(ehdr);
509// return obj;
510 }
511}
512
513} // End anonymous namespace.
514
515namespace gold
516{
517
518// Read an ELF file and return the appropriate instance of Object.
519
520Object*
521make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
522 const unsigned char* p, off_t bytes)
523{
524 if (bytes < elfcpp::EI_NIDENT)
525 {
526 fprintf(stderr, _("%s: %s: ELF file too short\n"),
527 program_name, name.c_str());
528 gold_exit(false);
529 }
530
531 int v = p[elfcpp::EI_VERSION];
532 if (v != elfcpp::EV_CURRENT)
533 {
534 if (v == elfcpp::EV_NONE)
535 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
536 program_name, name.c_str());
537 else
538 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
539 program_name, name.c_str(), v);
540 gold_exit(false);
541 }
542
543 int c = p[elfcpp::EI_CLASS];
544 if (c == elfcpp::ELFCLASSNONE)
545 {
546 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
547 program_name, name.c_str());
548 gold_exit(false);
549 }
550 else if (c != elfcpp::ELFCLASS32
551 && c != elfcpp::ELFCLASS64)
552 {
553 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
554 program_name, name.c_str(), c);
555 gold_exit(false);
556 }
557
558 int d = p[elfcpp::EI_DATA];
559 if (d == elfcpp::ELFDATANONE)
560 {
561 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
562 program_name, name.c_str());
563 gold_exit(false);
564 }
565 else if (d != elfcpp::ELFDATA2LSB
566 && d != elfcpp::ELFDATA2MSB)
567 {
568 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
569 program_name, name.c_str(), d);
570 gold_exit(false);
571 }
572
573 bool big_endian = d == elfcpp::ELFDATA2MSB;
574
575 if (c == elfcpp::ELFCLASS32)
576 {
577 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
578 {
579 fprintf(stderr, _("%s: %s: ELF file too short\n"),
580 program_name, name.c_str());
581 gold_exit(false);
582 }
583 if (big_endian)
584 {
585 elfcpp::Ehdr<32, true> ehdr(p);
586 return make_elf_sized_object<32, true>(name, input_file,
587 offset, ehdr);
588 }
589 else
590 {
591 elfcpp::Ehdr<32, false> ehdr(p);
592 return make_elf_sized_object<32, false>(name, input_file,
593 offset, ehdr);
594 }
595 }
596 else
597 {
598 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
599 {
600 fprintf(stderr, _("%s: %s: ELF file too short\n"),
601 program_name, name.c_str());
602 gold_exit(false);
603 }
604 if (big_endian)
605 {
606 elfcpp::Ehdr<64, true> ehdr(p);
607 return make_elf_sized_object<64, true>(name, input_file,
608 offset, ehdr);
609 }
610 else
611 {
612 elfcpp::Ehdr<64, false> ehdr(p);
613 return make_elf_sized_object<64, false>(name, input_file,
614 offset, ehdr);
615 }
616 }
617}
618
619// Instantiate the templates we need. We could use the configure
620// script to restrict this to only the ones for implemented targets.
621
622template
623class Sized_object<32, false>;
624
625template
626class Sized_object<32, true>;
627
628template
629class Sized_object<64, false>;
630
631template
632class Sized_object<64, true>;
633
634} // End namespace gold.
This page took 0.051429 seconds and 4 git commands to generate.