From Craig Silverstein: Add support for --demangle.
[deliverable/binutils-gdb.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "demangle.h"
29 #include "libiberty.h"
30
31 #include "target-select.h"
32 #include "dwarf_reader.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "symtab.h"
36 #include "reloc.h"
37 #include "object.h"
38 #include "dynobj.h"
39
40 namespace gold
41 {
42
43 // Class Object.
44
45 // Set the target based on fields in the ELF file header.
46
47 void
48 Object::set_target(int machine, int size, bool big_endian, int osabi,
49 int abiversion)
50 {
51 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
52 if (target == NULL)
53 gold_fatal(_("%s: unsupported ELF machine number %d"),
54 this->name().c_str(), machine);
55 this->target_ = target;
56 }
57
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
60 // itself.
61
62 void
63 Object::error(const char* format, ...) const
64 {
65 va_list args;
66 va_start(args, format);
67 char* buf = NULL;
68 if (vasprintf(&buf, format, args) < 0)
69 gold_nomem();
70 va_end(args);
71 gold_error(_("%s: %s"), this->name().c_str(), buf);
72 free(buf);
73 }
74
75 // Return a view of the contents of a section.
76
77 const unsigned char*
78 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
79 {
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);
83 }
84
85 // Read the section data into SD. This is code common to Sized_relobj
86 // and Sized_dynobj, so we put it into Object.
87
88 template<int size, bool big_endian>
89 void
90 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
91 Read_symbols_data* sd)
92 {
93 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
94
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);
99
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);
104
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()));
108
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);
112 }
113
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.
117
118 bool
119 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
120 Symbol_table* symtab)
121 {
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)
125 {
126 symtab->add_warning(name + warn_prefix_len, this, shndx);
127 return true;
128 }
129 return false;
130 }
131
132 // Class Sized_relobj.
133
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,
138 off_t offset,
139 const elfcpp::Ehdr<size, big_endian>& ehdr)
140 : Relobj(name, input_file, offset),
141 elf_file_(this, ehdr),
142 symtab_shndx_(-1U),
143 local_symbol_count_(0),
144 output_local_symbol_count_(0),
145 symbols_(),
146 local_symbol_offset_(0),
147 local_values_(),
148 local_got_offsets_(),
149 has_eh_frame_(false)
150 {
151 }
152
153 template<int size, bool big_endian>
154 Sized_relobj<size, big_endian>::~Sized_relobj()
155 {
156 }
157
158 // Set up an object file based on the file header. This sets up the
159 // target and reads the section information.
160
161 template<int size, bool big_endian>
162 void
163 Sized_relobj<size, big_endian>::setup(
164 const elfcpp::Ehdr<size, big_endian>& ehdr)
165 {
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]);
169
170 const unsigned int shnum = this->elf_file_.shnum();
171 this->set_shnum(shnum);
172 }
173
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.
178
179 template<int size, bool big_endian>
180 void
181 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
182 {
183 const unsigned int shnum = this->shnum();
184 this->symtab_shndx_ = 0;
185 if (shnum > 0)
186 {
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;
191 while (i > 0)
192 {
193 --i;
194 p -= This::shdr_size;
195 typename This::Shdr shdr(p);
196 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
197 {
198 this->symtab_shndx_ = i;
199 break;
200 }
201 }
202 }
203 }
204
205 // Return whether SHDR has the right type and flags to be a GNU
206 // .eh_frame section.
207
208 template<int size, bool big_endian>
209 bool
210 Sized_relobj<size, big_endian>::check_eh_frame_flags(
211 const elfcpp::Shdr<size, big_endian>* shdr) const
212 {
213 return (shdr->get_sh_size() > 0
214 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
215 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
216 }
217
218 // Return whether there is a GNU .eh_frame section, given the section
219 // headers and the section names.
220
221 template<int size, bool big_endian>
222 bool
223 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
224 const char* names,
225 off_t names_size) const
226 {
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)
230 {
231 typename This::Shdr shdr(p);
232 if (this->check_eh_frame_flags(&shdr))
233 {
234 if (shdr.get_sh_name() >= names_size)
235 {
236 this->error(_("bad section name offset for section %u: %lu"),
237 i, static_cast<unsigned long>(shdr.get_sh_name()));
238 continue;
239 }
240
241 const char* name = names + shdr.get_sh_name();
242 if (strcmp(name, ".eh_frame") == 0)
243 return true;
244 }
245 }
246 return false;
247 }
248
249 // Read the sections and symbols from an object file.
250
251 template<int size, bool big_endian>
252 void
253 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
254 {
255 this->read_section_data(&this->elf_file_, sd);
256
257 const unsigned char* const pshdrs = sd->section_headers->data();
258
259 this->find_symtab(pshdrs);
260
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;
265
266 sd->symbols = NULL;
267 sd->symbols_size = 0;
268 sd->external_symbols_offset = 0;
269 sd->symbol_names = NULL;
270 sd->symbol_names_size = 0;
271
272 if (this->symtab_shndx_ == 0)
273 {
274 // No symbol table. Weird but legal.
275 return;
276 }
277
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);
282
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
288 // now.
289
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;
298
299 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
300 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
301
302 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
303
304 // Read the section header for the symbol names.
305 unsigned int strtab_shndx = symtabshdr.get_sh_link();
306 if (strtab_shndx >= this->shnum())
307 {
308 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
309 return;
310 }
311 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
312 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
313 {
314 this->error(_("symbol table name section has wrong type: %u"),
315 static_cast<unsigned int>(strtabshdr.get_sh_type()));
316 return;
317 }
318
319 // Read the symbol names.
320 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
321 strtabshdr.get_sh_size(), true);
322
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();
328 }
329
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.
334
335 template<int size, bool big_endian>
336 unsigned int
337 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
338 Address* value)
339 {
340 off_t symbols_size;
341 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
342 &symbols_size,
343 false);
344
345 const size_t count = symbols_size / This::sym_size;
346 gold_assert(sym < count);
347
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();
352 }
353
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.
359
360 template<int size, bool big_endian>
361 bool
362 Sized_relobj<size, big_endian>::include_section_group(
363 Layout* layout,
364 unsigned int index,
365 const elfcpp::Shdr<size, big_endian>& shdr,
366 std::vector<bool>* omit)
367 {
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);
373
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)
379 return true;
380
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
384 // signature?
385
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));
390
391 // Read the symbol table entry.
392 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
393 {
394 this->error(_("section group %u info %u out of range"),
395 index, shdr.get_sh_info());
396 return false;
397 }
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);
401
402 // Read the symbol table names.
403 off_t symnamelen;
404 const unsigned char* psymnamesu;
405 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
406 true);
407 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
408
409 // Get the section group signature.
410 if (sym.get_st_name() >= symnamelen)
411 {
412 this->error(_("symbol %u name offset %u out of range"),
413 shdr.get_sh_info(), sym.get_st_name());
414 return false;
415 }
416
417 const char* signature = psymnames + sym.get_st_name();
418
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.
422 // FIXME.
423 std::string secname;
424 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
425 {
426 secname = this->section_name(sym.get_st_shndx());
427 signature = secname.c_str();
428 }
429
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))
433 return true;
434
435 // This is a duplicate. We want to discard the sections in this
436 // group.
437 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
438 for (size_t i = 1; i < count; ++i)
439 {
440 elfcpp::Elf_Word secnum =
441 elfcpp::Swap<32, big_endian>::readval(pword + i);
442 if (secnum >= this->shnum())
443 {
444 this->error(_("section %u in section group %u out of range"),
445 secnum, index);
446 continue;
447 }
448 (*omit)[secnum] = true;
449 }
450
451 return false;
452 }
453
454 // Whether to include a linkonce section in the link. NAME is the
455 // name of the section and SHDR is the section header.
456
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
467 // signature.
468
469 template<int size, bool big_endian>
470 bool
471 Sized_relobj<size, big_endian>::include_linkonce_section(
472 Layout* layout,
473 const char* name,
474 const elfcpp::Shdr<size, big_endian>&)
475 {
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.";
485 const char* symname;
486 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
487 symname = name + strlen(linkonce_t);
488 else
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;
493 }
494
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
498 // and an offset.
499
500 template<int size, bool big_endian>
501 void
502 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
503 Layout* layout,
504 Read_symbols_data* sd)
505 {
506 const unsigned int shnum = this->shnum();
507 if (shnum == 0)
508 return;
509
510 // Get the section headers.
511 const unsigned char* pshdrs = sd->section_headers->data();
512
513 // Get the section names.
514 const unsigned char* pnamesu = sd->section_names->data();
515 const char* pnames = reinterpret_cast<const char*>(pnamesu);
516
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)
525 {
526 typename This::Shdr shdr(pshdrs);
527
528 unsigned int sh_type = shdr.get_sh_type();
529 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
530 {
531 unsigned int target_shndx = shdr.get_sh_info();
532 if (target_shndx == 0 || target_shndx >= shnum)
533 {
534 this->error(_("relocation section %u has bad info %u"),
535 i, target_shndx);
536 continue;
537 }
538
539 if (reloc_shndx[target_shndx] != 0)
540 reloc_shndx[target_shndx] = -1U;
541 else
542 {
543 reloc_shndx[target_shndx] = i;
544 reloc_type[target_shndx] = sh_type;
545 }
546 }
547 }
548
549 std::vector<Map_to_output>& map_sections(this->map_to_output());
550 map_sections.resize(shnum);
551
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;
556
557 // Keep track of which sections to omit.
558 std::vector<bool> omit(shnum, false);
559
560 // Keep track of .eh_frame sections.
561 std::vector<unsigned int> eh_frame_sections;
562
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)
566 {
567 typename This::Shdr shdr(pshdrs);
568
569 if (shdr.get_sh_name() >= sd->section_names_size)
570 {
571 this->error(_("bad section name offset for section %u: %lu"),
572 i, static_cast<unsigned long>(shdr.get_sh_name()));
573 return;
574 }
575
576 const char* name = pnames + shdr.get_sh_name();
577
578 if (this->handle_gnu_warning_section(name, i, symtab))
579 {
580 if (!parameters->output_is_object())
581 omit[i] = true;
582 }
583
584 // The .note.GNU-stack section is special. It gives the
585 // protection flags that this object file requires for the stack
586 // in memory.
587 if (strcmp(name, ".note.GNU-stack") == 0)
588 {
589 seen_gnu_stack = true;
590 gnu_stack_flags |= shdr.get_sh_flags();
591 omit[i] = true;
592 }
593
594 bool discard = omit[i];
595 if (!discard)
596 {
597 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
598 {
599 if (!this->include_section_group(layout, i, shdr, &omit))
600 discard = true;
601 }
602 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
603 && Layout::is_linkonce(name))
604 {
605 if (!this->include_linkonce_section(layout, name, shdr))
606 discard = true;
607 }
608 }
609
610 if (discard)
611 {
612 // Do not include this section in the link.
613 map_sections[i].output_section = NULL;
614 continue;
615 }
616
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))
626 {
627 eh_frame_sections.push_back(i);
628 continue;
629 }
630
631 off_t offset;
632 Output_section* os = layout->layout(this, i, name, shdr,
633 reloc_shndx[i], reloc_type[i],
634 &offset);
635
636 map_sections[i].output_section = os;
637 map_sections[i].offset = offset;
638
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();
644 }
645
646 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
647
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();
651 ++p)
652 {
653 gold_assert(this->has_eh_frame_);
654 gold_assert(sd->external_symbols_offset != 0);
655
656 unsigned int i = *p;
657 const unsigned char *pshdr;
658 pshdr = sd->section_headers->data() + i * This::shdr_size;
659 typename This::Shdr shdr(pshdr);
660
661 off_t offset;
662 Output_section* os = layout->layout_eh_frame(this,
663 sd->symbols->data(),
664 sd->symbols_size,
665 sd->symbol_names->data(),
666 sd->symbol_names_size,
667 i, shdr,
668 reloc_shndx[i],
669 reloc_type[i],
670 &offset);
671 map_sections[i].output_section = os;
672 map_sections[i].offset = offset;
673
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();
679 }
680
681 delete sd->section_headers;
682 sd->section_headers = NULL;
683 delete sd->section_names;
684 sd->section_names = NULL;
685 }
686
687 // Add the symbols to the symbol table.
688
689 template<int size, bool big_endian>
690 void
691 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
692 Read_symbols_data* sd)
693 {
694 if (sd->symbols == NULL)
695 {
696 gold_assert(sd->symbol_names == NULL);
697 return;
698 }
699
700 const int sym_size = This::sym_size;
701 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
702 / sym_size);
703 if (static_cast<off_t>(symcount * sym_size)
704 != sd->symbols_size - sd->external_symbols_offset)
705 {
706 this->error(_("size of symbols is not multiple of symbol size"));
707 return;
708 }
709
710 this->symbols_.resize(symcount);
711
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,
717 &this->symbols_);
718
719 delete sd->symbols;
720 sd->symbols = NULL;
721 delete sd->symbol_names;
722 sd->symbol_names = NULL;
723 }
724
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.
730
731 template<int size, bool big_endian>
732 unsigned int
733 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
734 off_t off,
735 Stringpool* pool)
736 {
737 gold_assert(this->symtab_shndx_ != -1U);
738 if (this->symtab_shndx_ == 0)
739 {
740 // This object has no symbols. Weird but legal.
741 return index;
742 }
743
744 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
745
746 this->local_symbol_offset_ = off;
747
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);
753
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(),
760 locsize, true);
761
762 this->local_values_.resize(loccount);
763
764 // Read the symbol names.
765 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
766 off_t strtab_size;
767 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
768 &strtab_size,
769 true);
770 const char* pnames = reinterpret_cast<const char*>(pnamesu);
771
772 // Loop over the local symbols.
773
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.
778 psyms += sym_size;
779 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
780 {
781 elfcpp::Sym<size, big_endian> sym(psyms);
782
783 Symbol_value<size>& lv(this->local_values_[i]);
784
785 unsigned int shndx = sym.get_st_shndx();
786 lv.set_input_shndx(shndx);
787
788 if (sym.get_st_type() == elfcpp::STT_SECTION)
789 lv.set_is_section_symbol();
790
791 if (shndx >= elfcpp::SHN_LORESERVE)
792 {
793 if (shndx == elfcpp::SHN_ABS)
794 lv.set_output_value(sym.get_st_value());
795 else
796 {
797 // FIXME: Handle SHN_XINDEX.
798 this->error(_("unknown section index %u for local symbol %u"),
799 shndx, i);
800 lv.set_output_value(0);
801 }
802 }
803 else
804 {
805 if (shndx >= shnum)
806 {
807 this->error(_("local symbol %u section index %u out of range"),
808 i, shndx);
809 shndx = 0;
810 }
811
812 Output_section* os = mo[shndx].output_section;
813
814 if (os == NULL)
815 {
816 lv.set_output_value(0);
817 lv.set_no_output_symtab_entry();
818 continue;
819 }
820
821 if (mo[shndx].offset == -1)
822 lv.set_input_value(sym.get_st_value());
823 else
824 lv.set_output_value(mo[shndx].output_section->address()
825 + mo[shndx].offset
826 + sym.get_st_value());
827 }
828
829 // Decide whether this symbol should go into the output file.
830
831 if (sym.get_st_type() == elfcpp::STT_SECTION)
832 {
833 lv.set_no_output_symtab_entry();
834 continue;
835 }
836
837 if (sym.get_st_name() >= strtab_size)
838 {
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();
843 continue;
844 }
845
846 const char* name = pnames + sym.get_st_name();
847 pool->add(name, true, NULL);
848 lv.set_output_symtab_index(index);
849 ++index;
850 ++count;
851 }
852
853 this->output_local_symbol_count_ = count;
854
855 return index;
856 }
857
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
862 {
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);
867 }
868
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,
876 Address value,
877 bool is_section_symbol,
878 Address addend) const
879 {
880 const std::vector<Map_to_output>& mo(this->map_to_output());
881 Output_section* os = mo[shndx].output_section;
882 if (os == NULL)
883 return addend;
884 gold_assert(mo[shndx].offset == -1);
885
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.
896
897 if (is_section_symbol)
898 return os->output_address(this, shndx, value + addend);
899 else
900 return addend + os->output_address(this, shndx, value);
901 }
902
903 // Write out the local symbols.
904
905 template<int size, bool big_endian>
906 void
907 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
908 const Stringpool* sympool)
909 {
910 if (parameters->strip_all())
911 return;
912
913 gold_assert(this->symtab_shndx_ != -1U);
914 if (this->symtab_shndx_ == 0)
915 {
916 // This object has no symbols. Weird but legal.
917 return;
918 }
919
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());
927
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(),
932 locsize, false);
933
934 // Read the symbol names.
935 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
936 off_t strtab_size;
937 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
938 &strtab_size,
939 true);
940 const char* pnames = reinterpret_cast<const char*>(pnamesu);
941
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_,
945 output_size);
946
947 const std::vector<Map_to_output>& mo(this->map_to_output());
948
949 gold_assert(this->local_values_.size() == loccount);
950
951 unsigned char* ov = oview;
952 psyms += sym_size;
953 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
954 {
955 elfcpp::Sym<size, big_endian> isym(psyms);
956
957 if (!this->local_values_[i].needs_output_symtab_entry())
958 continue;
959
960 unsigned int st_shndx = isym.get_st_shndx();
961 if (st_shndx < elfcpp::SHN_LORESERVE)
962 {
963 gold_assert(st_shndx < mo.size());
964 if (mo[st_shndx].output_section == NULL)
965 continue;
966 st_shndx = mo[st_shndx].output_section->out_shndx();
967 }
968
969 elfcpp::Sym_write<size, big_endian> osym(ov);
970
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);
979
980 ov += sym_size;
981 }
982
983 gold_assert(ov - oview == output_size);
984
985 of->write_output_view(this->local_symbol_offset_, output_size, oview);
986 }
987
988 // Set *INFO to symbolic information about the offset OFFSET in the
989 // section SHNDX. Return true if we found something, false if we
990 // found nothing.
991
992 template<int size, bool big_endian>
993 bool
994 Sized_relobj<size, big_endian>::get_symbol_location_info(
995 unsigned int shndx,
996 off_t offset,
997 Symbol_location_info* info)
998 {
999 if (this->symtab_shndx_ == 0)
1000 return false;
1001
1002 off_t symbols_size;
1003 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1004 &symbols_size,
1005 false);
1006
1007 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1008 off_t names_size;
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);
1012
1013 const int sym_size = This::sym_size;
1014 const size_t count = symbols_size / sym_size;
1015
1016 const unsigned char* p = symbols;
1017 for (size_t i = 0; i < count; ++i, p += sym_size)
1018 {
1019 elfcpp::Sym<size, big_endian> sym(p);
1020
1021 if (sym.get_st_type() == elfcpp::STT_FILE)
1022 {
1023 if (sym.get_st_name() >= names_size)
1024 info->source_file = "(invalid)";
1025 else
1026 info->source_file = symbol_names + sym.get_st_name();
1027 }
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())
1031 > offset))
1032 {
1033 if (sym.get_st_name() > names_size)
1034 info->enclosing_symbol_name = "(invalid)";
1035 else
1036 {
1037 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1038 if (parameters->demangle())
1039 {
1040 char* demangled_name = cplus_demangle(
1041 info->enclosing_symbol_name.c_str(),
1042 DMGL_ANSI | DMGL_PARAMS);
1043 if (demangled_name != NULL)
1044 {
1045 info->enclosing_symbol_name.assign(demangled_name);
1046 free(demangled_name);
1047 }
1048 }
1049 }
1050 return true;
1051 }
1052 }
1053
1054 return false;
1055 }
1056
1057 // Input_objects methods.
1058
1059 // Add a regular relocatable object to the list. Return false if this
1060 // object should be ignored.
1061
1062 bool
1063 Input_objects::add_object(Object* obj)
1064 {
1065 Target* target = obj->target();
1066 if (this->target_ == NULL)
1067 this->target_ = target;
1068 else if (this->target_ != target)
1069 {
1070 gold_error(_("%s: incompatible target"), obj->name().c_str());
1071 return false;
1072 }
1073
1074 if (!obj->is_dynamic())
1075 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1076 else
1077 {
1078 // See if this is a duplicate SONAME.
1079 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1080 const char* soname = dynobj->soname();
1081
1082 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1083 this->sonames_.insert(soname);
1084 if (!ins.second)
1085 {
1086 // We have already seen a dynamic object with this soname.
1087 return false;
1088 }
1089
1090 this->dynobj_list_.push_back(dynobj);
1091
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)
1097 {
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);
1103 }
1104 }
1105
1106 set_parameters_size_and_endianness(target->get_size(),
1107 target->is_big_endian());
1108
1109 return true;
1110 }
1111
1112 // Return whether an object was found in the system library directory.
1113
1114 bool
1115 Input_objects::found_in_system_library_directory(const Object* object) const
1116 {
1117 return (!this->system_library_directory_.empty()
1118 && object->name().compare(0,
1119 this->system_library_directory_.size(),
1120 this->system_library_directory_) == 0);
1121 }
1122
1123 // For each dynamic object, record whether we've seen all of its
1124 // explicit dependencies.
1125
1126 void
1127 Input_objects::check_dynamic_dependencies() const
1128 {
1129 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1130 p != this->dynobj_list_.end();
1131 ++p)
1132 {
1133 const Dynobj::Needed& needed((*p)->needed());
1134 bool found_all = true;
1135 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1136 pneeded != needed.end();
1137 ++pneeded)
1138 {
1139 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1140 {
1141 found_all = false;
1142 break;
1143 }
1144 }
1145 (*p)->set_has_unknown_needed_entries(!found_all);
1146 }
1147 }
1148
1149 // Relocate_info methods.
1150
1151 // Return a string describing the location of a relocation. This is
1152 // only used in error messages.
1153
1154 template<int size, bool big_endian>
1155 std::string
1156 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1157 {
1158 // See if we can get line-number information from debugging sections.
1159 std::string filename;
1160 std::string file_and_lineno; // Better than filename-only, if available.
1161
1162 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1163 // This will be "" if we failed to parse the debug info for any reason.
1164 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1165
1166 std::string ret(this->object->name());
1167 ret += ':';
1168 Symbol_location_info info;
1169 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1170 {
1171 ret += " in function ";
1172 ret += info.enclosing_symbol_name;
1173 ret += ":";
1174 filename = info.source_file;
1175 }
1176
1177 if (!file_and_lineno.empty())
1178 ret += file_and_lineno;
1179 else
1180 {
1181 if (!filename.empty())
1182 ret += filename;
1183 ret += "(";
1184 ret += this->object->section_name(this->data_shndx);
1185 char buf[100];
1186 // Offsets into sections have to be positive.
1187 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1188 ret += buf;
1189 ret += ")";
1190 }
1191 return ret;
1192 }
1193
1194 } // End namespace gold.
1195
1196 namespace
1197 {
1198
1199 using namespace gold;
1200
1201 // Read an ELF file with the header and return the appropriate
1202 // instance of Object.
1203
1204 template<int size, bool big_endian>
1205 Object*
1206 make_elf_sized_object(const std::string& name, Input_file* input_file,
1207 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1208 {
1209 int et = ehdr.get_e_type();
1210 if (et == elfcpp::ET_REL)
1211 {
1212 Sized_relobj<size, big_endian>* obj =
1213 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1214 obj->setup(ehdr);
1215 return obj;
1216 }
1217 else if (et == elfcpp::ET_DYN)
1218 {
1219 Sized_dynobj<size, big_endian>* obj =
1220 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1221 obj->setup(ehdr);
1222 return obj;
1223 }
1224 else
1225 {
1226 gold_error(_("%s: unsupported ELF file type %d"),
1227 name.c_str(), et);
1228 return NULL;
1229 }
1230 }
1231
1232 } // End anonymous namespace.
1233
1234 namespace gold
1235 {
1236
1237 // Read an ELF file and return the appropriate instance of Object.
1238
1239 Object*
1240 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1241 const unsigned char* p, off_t bytes)
1242 {
1243 if (bytes < elfcpp::EI_NIDENT)
1244 {
1245 gold_error(_("%s: ELF file too short"), name.c_str());
1246 return NULL;
1247 }
1248
1249 int v = p[elfcpp::EI_VERSION];
1250 if (v != elfcpp::EV_CURRENT)
1251 {
1252 if (v == elfcpp::EV_NONE)
1253 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1254 else
1255 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1256 return NULL;
1257 }
1258
1259 int c = p[elfcpp::EI_CLASS];
1260 if (c == elfcpp::ELFCLASSNONE)
1261 {
1262 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1263 return NULL;
1264 }
1265 else if (c != elfcpp::ELFCLASS32
1266 && c != elfcpp::ELFCLASS64)
1267 {
1268 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1269 return NULL;
1270 }
1271
1272 int d = p[elfcpp::EI_DATA];
1273 if (d == elfcpp::ELFDATANONE)
1274 {
1275 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1276 return NULL;
1277 }
1278 else if (d != elfcpp::ELFDATA2LSB
1279 && d != elfcpp::ELFDATA2MSB)
1280 {
1281 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1282 return NULL;
1283 }
1284
1285 bool big_endian = d == elfcpp::ELFDATA2MSB;
1286
1287 if (c == elfcpp::ELFCLASS32)
1288 {
1289 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1290 {
1291 gold_error(_("%s: ELF file too short"), name.c_str());
1292 return NULL;
1293 }
1294 if (big_endian)
1295 {
1296 #ifdef HAVE_TARGET_32_BIG
1297 elfcpp::Ehdr<32, true> ehdr(p);
1298 return make_elf_sized_object<32, true>(name, input_file,
1299 offset, ehdr);
1300 #else
1301 gold_error(_("%s: not configured to support "
1302 "32-bit big-endian object"),
1303 name.c_str());
1304 return NULL;
1305 #endif
1306 }
1307 else
1308 {
1309 #ifdef HAVE_TARGET_32_LITTLE
1310 elfcpp::Ehdr<32, false> ehdr(p);
1311 return make_elf_sized_object<32, false>(name, input_file,
1312 offset, ehdr);
1313 #else
1314 gold_error(_("%s: not configured to support "
1315 "32-bit little-endian object"),
1316 name.c_str());
1317 return NULL;
1318 #endif
1319 }
1320 }
1321 else
1322 {
1323 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1324 {
1325 gold_error(_("%s: ELF file too short"), name.c_str());
1326 return NULL;
1327 }
1328 if (big_endian)
1329 {
1330 #ifdef HAVE_TARGET_64_BIG
1331 elfcpp::Ehdr<64, true> ehdr(p);
1332 return make_elf_sized_object<64, true>(name, input_file,
1333 offset, ehdr);
1334 #else
1335 gold_error(_("%s: not configured to support "
1336 "64-bit big-endian object"),
1337 name.c_str());
1338 return NULL;
1339 #endif
1340 }
1341 else
1342 {
1343 #ifdef HAVE_TARGET_64_LITTLE
1344 elfcpp::Ehdr<64, false> ehdr(p);
1345 return make_elf_sized_object<64, false>(name, input_file,
1346 offset, ehdr);
1347 #else
1348 gold_error(_("%s: not configured to support "
1349 "64-bit little-endian object"),
1350 name.c_str());
1351 return NULL;
1352 #endif
1353 }
1354 }
1355 }
1356
1357 // Instantiate the templates we need. We could use the configure
1358 // script to restrict this to only the ones for implemented targets.
1359
1360 #ifdef HAVE_TARGET_32_LITTLE
1361 template
1362 class Sized_relobj<32, false>;
1363 #endif
1364
1365 #ifdef HAVE_TARGET_32_BIG
1366 template
1367 class Sized_relobj<32, true>;
1368 #endif
1369
1370 #ifdef HAVE_TARGET_64_LITTLE
1371 template
1372 class Sized_relobj<64, false>;
1373 #endif
1374
1375 #ifdef HAVE_TARGET_64_BIG
1376 template
1377 class Sized_relobj<64, true>;
1378 #endif
1379
1380 #ifdef HAVE_TARGET_32_LITTLE
1381 template
1382 struct Relocate_info<32, false>;
1383 #endif
1384
1385 #ifdef HAVE_TARGET_32_BIG
1386 template
1387 struct Relocate_info<32, true>;
1388 #endif
1389
1390 #ifdef HAVE_TARGET_64_LITTLE
1391 template
1392 struct Relocate_info<64, false>;
1393 #endif
1394
1395 #ifdef HAVE_TARGET_64_BIG
1396 template
1397 struct Relocate_info<64, true>;
1398 #endif
1399
1400 } // End namespace gold.
This page took 0.074791 seconds and 5 git commands to generate.