Generate a complete exception frame header. Discard duplicate
[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
29 #include "target-select.h"
30 #include "dwarf_reader.h"
31 #include "layout.h"
32 #include "output.h"
33 #include "symtab.h"
34 #include "object.h"
35 #include "dynobj.h"
36
37 namespace gold
38 {
39
40 // Class Object.
41
42 // Set the target based on fields in the ELF file header.
43
44 void
45 Object::set_target(int machine, int size, bool big_endian, int osabi,
46 int abiversion)
47 {
48 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
49 if (target == NULL)
50 gold_fatal(_("%s: unsupported ELF machine number %d"),
51 this->name().c_str(), machine);
52 this->target_ = target;
53 }
54
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
57 // itself.
58
59 void
60 Object::error(const char* format, ...) const
61 {
62 va_list args;
63 va_start(args, format);
64 char* buf = NULL;
65 if (vasprintf(&buf, format, args) < 0)
66 gold_nomem();
67 va_end(args);
68 gold_error(_("%s: %s"), this->name().c_str(), buf);
69 free(buf);
70 }
71
72 // Return a view of the contents of a section.
73
74 const unsigned char*
75 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
76 {
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);
80 }
81
82 // Read the section data into SD. This is code common to Sized_relobj
83 // and Sized_dynobj, so we put it into Object.
84
85 template<int size, bool big_endian>
86 void
87 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
88 Read_symbols_data* sd)
89 {
90 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
91
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);
96
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);
101
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()));
105
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);
109 }
110
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.
114
115 bool
116 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
117 Symbol_table* symtab)
118 {
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)
122 {
123 symtab->add_warning(name + warn_prefix_len, this, shndx);
124 return true;
125 }
126 return false;
127 }
128
129 // Class Sized_relobj.
130
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,
135 off_t offset,
136 const elfcpp::Ehdr<size, big_endian>& ehdr)
137 : Relobj(name, input_file, offset),
138 elf_file_(this, ehdr),
139 symtab_shndx_(-1U),
140 local_symbol_count_(0),
141 output_local_symbol_count_(0),
142 symbols_(),
143 local_symbol_offset_(0),
144 local_values_(),
145 local_got_offsets_(),
146 has_eh_frame_(false)
147 {
148 }
149
150 template<int size, bool big_endian>
151 Sized_relobj<size, big_endian>::~Sized_relobj()
152 {
153 }
154
155 // Set up an object file based on the file header. This sets up the
156 // target and reads the section information.
157
158 template<int size, bool big_endian>
159 void
160 Sized_relobj<size, big_endian>::setup(
161 const elfcpp::Ehdr<size, big_endian>& ehdr)
162 {
163 this->set_target(ehdr.get_e_machine(), size, big_endian,
164 ehdr.get_e_ident()[elfcpp::EI_OSABI],
165 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
166
167 const unsigned int shnum = this->elf_file_.shnum();
168 this->set_shnum(shnum);
169 }
170
171 // Find the SHT_SYMTAB section, given the section headers. The ELF
172 // standard says that maybe in the future there can be more than one
173 // SHT_SYMTAB section. Until somebody figures out how that could
174 // work, we assume there is only one.
175
176 template<int size, bool big_endian>
177 void
178 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
179 {
180 const unsigned int shnum = this->shnum();
181 this->symtab_shndx_ = 0;
182 if (shnum > 0)
183 {
184 // Look through the sections in reverse order, since gas tends
185 // to put the symbol table at the end.
186 const unsigned char* p = pshdrs + shnum * This::shdr_size;
187 unsigned int i = shnum;
188 while (i > 0)
189 {
190 --i;
191 p -= This::shdr_size;
192 typename This::Shdr shdr(p);
193 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
194 {
195 this->symtab_shndx_ = i;
196 break;
197 }
198 }
199 }
200 }
201
202 // Return whether SHDR has the right type and flags to be a GNU
203 // .eh_frame section.
204
205 template<int size, bool big_endian>
206 bool
207 Sized_relobj<size, big_endian>::check_eh_frame_flags(
208 const elfcpp::Shdr<size, big_endian>* shdr) const
209 {
210 return (shdr->get_sh_size() > 0
211 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
212 && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
213 }
214
215 // Return whether there is a GNU .eh_frame section, given the section
216 // headers and the section names.
217
218 template<int size, bool big_endian>
219 bool
220 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
221 const char* names,
222 off_t names_size) const
223 {
224 const unsigned int shnum = this->shnum();
225 const unsigned char* p = pshdrs + This::shdr_size;
226 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
227 {
228 typename This::Shdr shdr(p);
229 if (this->check_eh_frame_flags(&shdr))
230 {
231 if (shdr.get_sh_name() >= names_size)
232 {
233 this->error(_("bad section name offset for section %u: %lu"),
234 i, static_cast<unsigned long>(shdr.get_sh_name()));
235 continue;
236 }
237
238 const char* name = names + shdr.get_sh_name();
239 if (strcmp(name, ".eh_frame") == 0)
240 return true;
241 }
242 }
243 return false;
244 }
245
246 // Read the sections and symbols from an object file.
247
248 template<int size, bool big_endian>
249 void
250 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
251 {
252 this->read_section_data(&this->elf_file_, sd);
253
254 const unsigned char* const pshdrs = sd->section_headers->data();
255
256 this->find_symtab(pshdrs);
257
258 const unsigned char* namesu = sd->section_names->data();
259 const char* names = reinterpret_cast<const char*>(namesu);
260 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
261 this->has_eh_frame_ = true;
262
263 sd->symbols = NULL;
264 sd->symbols_size = 0;
265 sd->external_symbols_offset = 0;
266 sd->symbol_names = NULL;
267 sd->symbol_names_size = 0;
268
269 if (this->symtab_shndx_ == 0)
270 {
271 // No symbol table. Weird but legal.
272 return;
273 }
274
275 // Get the symbol table section header.
276 typename This::Shdr symtabshdr(pshdrs
277 + this->symtab_shndx_ * This::shdr_size);
278 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
279
280 // If this object has a .eh_frame section, we need all the symbols.
281 // Otherwise we only need the external symbols. While it would be
282 // simpler to just always read all the symbols, I've seen object
283 // files with well over 2000 local symbols, which for a 64-bit
284 // object file format is over 5 pages that we don't need to read
285 // now.
286
287 const int sym_size = This::sym_size;
288 const unsigned int loccount = symtabshdr.get_sh_info();
289 this->local_symbol_count_ = loccount;
290 off_t locsize = loccount * sym_size;
291 off_t dataoff = symtabshdr.get_sh_offset();
292 off_t datasize = symtabshdr.get_sh_size();
293 off_t extoff = dataoff + locsize;
294 off_t extsize = datasize - locsize;
295
296 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
297 off_t readsize = this->has_eh_frame_ ? datasize : extsize;
298
299 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
300
301 // Read the section header for the symbol names.
302 unsigned int strtab_shndx = symtabshdr.get_sh_link();
303 if (strtab_shndx >= this->shnum())
304 {
305 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
306 return;
307 }
308 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
309 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
310 {
311 this->error(_("symbol table name section has wrong type: %u"),
312 static_cast<unsigned int>(strtabshdr.get_sh_type()));
313 return;
314 }
315
316 // Read the symbol names.
317 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
318 strtabshdr.get_sh_size(), true);
319
320 sd->symbols = fvsymtab;
321 sd->symbols_size = readsize;
322 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
323 sd->symbol_names = fvstrtab;
324 sd->symbol_names_size = strtabshdr.get_sh_size();
325 }
326
327 // Return the section index of symbol SYM. Set *VALUE to its value in
328 // the object file. Note that for a symbol which is not defined in
329 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
330 // it will not return the final value of the symbol in the link.
331
332 template<int size, bool big_endian>
333 unsigned int
334 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
335 Address* value)
336 {
337 off_t symbols_size;
338 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
339 &symbols_size,
340 false);
341
342 const size_t count = symbols_size / This::sym_size;
343 gold_assert(sym < count);
344
345 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
346 *value = elfsym.get_st_value();
347 // FIXME: Handle SHN_XINDEX.
348 return elfsym.get_st_shndx();
349 }
350
351 // Return whether to include a section group in the link. LAYOUT is
352 // used to keep track of which section groups we have already seen.
353 // INDEX is the index of the section group and SHDR is the section
354 // header. If we do not want to include this group, we set bits in
355 // OMIT for each section which should be discarded.
356
357 template<int size, bool big_endian>
358 bool
359 Sized_relobj<size, big_endian>::include_section_group(
360 Layout* layout,
361 unsigned int index,
362 const elfcpp::Shdr<size, big_endian>& shdr,
363 std::vector<bool>* omit)
364 {
365 // Read the section contents.
366 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
367 shdr.get_sh_size(), false);
368 const elfcpp::Elf_Word* pword =
369 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
370
371 // The first word contains flags. We only care about COMDAT section
372 // groups. Other section groups are always included in the link
373 // just like ordinary sections.
374 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
375 if ((flags & elfcpp::GRP_COMDAT) == 0)
376 return true;
377
378 // Look up the group signature, which is the name of a symbol. This
379 // is a lot of effort to go to to read a string. Why didn't they
380 // just use the name of the SHT_GROUP section as the group
381 // signature?
382
383 // Get the appropriate symbol table header (this will normally be
384 // the single SHT_SYMTAB section, but in principle it need not be).
385 const unsigned int link = shdr.get_sh_link();
386 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
387
388 // Read the symbol table entry.
389 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
390 {
391 this->error(_("section group %u info %u out of range"),
392 index, shdr.get_sh_info());
393 return false;
394 }
395 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
396 const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
397 elfcpp::Sym<size, big_endian> sym(psym);
398
399 // Read the symbol table names.
400 off_t symnamelen;
401 const unsigned char* psymnamesu;
402 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
403 true);
404 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
405
406 // Get the section group signature.
407 if (sym.get_st_name() >= symnamelen)
408 {
409 this->error(_("symbol %u name offset %u out of range"),
410 shdr.get_sh_info(), sym.get_st_name());
411 return false;
412 }
413
414 const char* signature = psymnames + sym.get_st_name();
415
416 // It seems that some versions of gas will create a section group
417 // associated with a section symbol, and then fail to give a name to
418 // the section symbol. In such a case, use the name of the section.
419 // FIXME.
420 std::string secname;
421 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
422 {
423 secname = this->section_name(sym.get_st_shndx());
424 signature = secname.c_str();
425 }
426
427 // Record this section group, and see whether we've already seen one
428 // with the same signature.
429 if (layout->add_comdat(signature, true))
430 return true;
431
432 // This is a duplicate. We want to discard the sections in this
433 // group.
434 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
435 for (size_t i = 1; i < count; ++i)
436 {
437 elfcpp::Elf_Word secnum =
438 elfcpp::Swap<32, big_endian>::readval(pword + i);
439 if (secnum >= this->shnum())
440 {
441 this->error(_("section %u in section group %u out of range"),
442 secnum, index);
443 continue;
444 }
445 (*omit)[secnum] = true;
446 }
447
448 return false;
449 }
450
451 // Whether to include a linkonce section in the link. NAME is the
452 // name of the section and SHDR is the section header.
453
454 // Linkonce sections are a GNU extension implemented in the original
455 // GNU linker before section groups were defined. The semantics are
456 // that we only include one linkonce section with a given name. The
457 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
458 // where T is the type of section and SYMNAME is the name of a symbol.
459 // In an attempt to make linkonce sections interact well with section
460 // groups, we try to identify SYMNAME and use it like a section group
461 // signature. We want to block section groups with that signature,
462 // but not other linkonce sections with that signature. We also use
463 // the full name of the linkonce section as a normal section group
464 // signature.
465
466 template<int size, bool big_endian>
467 bool
468 Sized_relobj<size, big_endian>::include_linkonce_section(
469 Layout* layout,
470 const char* name,
471 const elfcpp::Shdr<size, big_endian>&)
472 {
473 // In general the symbol name we want will be the string following
474 // the last '.'. However, we have to handle the case of
475 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
476 // some versions of gcc. So we use a heuristic: if the name starts
477 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
478 // we look for the last '.'. We can't always simply skip
479 // ".gnu.linkonce.X", because we have to deal with cases like
480 // ".gnu.linkonce.d.rel.ro.local".
481 const char* const linkonce_t = ".gnu.linkonce.t.";
482 const char* symname;
483 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
484 symname = name + strlen(linkonce_t);
485 else
486 symname = strrchr(name, '.') + 1;
487 bool include1 = layout->add_comdat(symname, false);
488 bool include2 = layout->add_comdat(name, true);
489 return include1 && include2;
490 }
491
492 // Lay out the input sections. We walk through the sections and check
493 // whether they should be included in the link. If they should, we
494 // pass them to the Layout object, which will return an output section
495 // and an offset.
496
497 template<int size, bool big_endian>
498 void
499 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
500 Layout* layout,
501 Read_symbols_data* sd)
502 {
503 const unsigned int shnum = this->shnum();
504 if (shnum == 0)
505 return;
506
507 // Get the section headers.
508 const unsigned char* pshdrs = sd->section_headers->data();
509
510 // Get the section names.
511 const unsigned char* pnamesu = sd->section_names->data();
512 const char* pnames = reinterpret_cast<const char*>(pnamesu);
513
514 // For each section, record the index of the reloc section if any.
515 // Use 0 to mean that there is no reloc section, -1U to mean that
516 // there is more than one.
517 std::vector<unsigned int> reloc_shndx(shnum, 0);
518 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
519 // Skip the first, dummy, section.
520 pshdrs += This::shdr_size;
521 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
522 {
523 typename This::Shdr shdr(pshdrs);
524
525 unsigned int sh_type = shdr.get_sh_type();
526 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
527 {
528 unsigned int target_shndx = shdr.get_sh_info();
529 if (target_shndx == 0 || target_shndx >= shnum)
530 {
531 this->error(_("relocation section %u has bad info %u"),
532 i, target_shndx);
533 continue;
534 }
535
536 if (reloc_shndx[target_shndx] != 0)
537 reloc_shndx[target_shndx] = -1U;
538 else
539 {
540 reloc_shndx[target_shndx] = i;
541 reloc_type[target_shndx] = sh_type;
542 }
543 }
544 }
545
546 std::vector<Map_to_output>& map_sections(this->map_to_output());
547 map_sections.resize(shnum);
548
549 // Whether we've seen a .note.GNU-stack section.
550 bool seen_gnu_stack = false;
551 // The flags of a .note.GNU-stack section.
552 uint64_t gnu_stack_flags = 0;
553
554 // Keep track of which sections to omit.
555 std::vector<bool> omit(shnum, false);
556
557 // Keep track of .eh_frame sections.
558 std::vector<unsigned int> eh_frame_sections;
559
560 // Skip the first, dummy, section.
561 pshdrs = sd->section_headers->data() + This::shdr_size;
562 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
563 {
564 typename This::Shdr shdr(pshdrs);
565
566 if (shdr.get_sh_name() >= sd->section_names_size)
567 {
568 this->error(_("bad section name offset for section %u: %lu"),
569 i, static_cast<unsigned long>(shdr.get_sh_name()));
570 return;
571 }
572
573 const char* name = pnames + shdr.get_sh_name();
574
575 if (this->handle_gnu_warning_section(name, i, symtab))
576 {
577 if (!parameters->output_is_object())
578 omit[i] = true;
579 }
580
581 // The .note.GNU-stack section is special. It gives the
582 // protection flags that this object file requires for the stack
583 // in memory.
584 if (strcmp(name, ".note.GNU-stack") == 0)
585 {
586 seen_gnu_stack = true;
587 gnu_stack_flags |= shdr.get_sh_flags();
588 omit[i] = true;
589 }
590
591 bool discard = omit[i];
592 if (!discard)
593 {
594 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
595 {
596 if (!this->include_section_group(layout, i, shdr, &omit))
597 discard = true;
598 }
599 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
600 && Layout::is_linkonce(name))
601 {
602 if (!this->include_linkonce_section(layout, name, shdr))
603 discard = true;
604 }
605 }
606
607 if (discard)
608 {
609 // Do not include this section in the link.
610 map_sections[i].output_section = NULL;
611 continue;
612 }
613
614 // The .eh_frame section is special. It holds exception frame
615 // information that we need to read in order to generate the
616 // exception frame header. We process these after all the other
617 // sections so that the exception frame reader can reliably
618 // determine which sections are being discarded, and discard the
619 // corresponding information.
620 if (!parameters->output_is_object()
621 && strcmp(name, ".eh_frame") == 0
622 && this->check_eh_frame_flags(&shdr))
623 {
624 eh_frame_sections.push_back(i);
625 continue;
626 }
627
628 off_t offset;
629 Output_section* os = layout->layout(this, i, name, shdr,
630 reloc_shndx[i], reloc_type[i],
631 &offset);
632
633 map_sections[i].output_section = os;
634 map_sections[i].offset = offset;
635
636 // If this section requires special handling, and if there are
637 // relocs that apply to it, then we must do the special handling
638 // before we apply the relocs.
639 if (offset == -1 && reloc_shndx[i] != 0)
640 this->set_relocs_must_follow_section_writes();
641 }
642
643 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
644
645 // Handle the .eh_frame sections at the end.
646 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
647 p != eh_frame_sections.end();
648 ++p)
649 {
650 gold_assert(this->has_eh_frame_);
651 gold_assert(sd->external_symbols_offset != 0);
652
653 unsigned int i = *p;
654 const unsigned char *pshdr;
655 pshdr = sd->section_headers->data() + i * This::shdr_size;
656 typename This::Shdr shdr(pshdr);
657
658 off_t offset;
659 Output_section* os = layout->layout_eh_frame(this,
660 sd->symbols->data(),
661 sd->symbols_size,
662 sd->symbol_names->data(),
663 sd->symbol_names_size,
664 i, shdr,
665 reloc_shndx[i],
666 reloc_type[i],
667 &offset);
668 map_sections[i].output_section = os;
669 map_sections[i].offset = offset;
670
671 // If this section requires special handling, and if there are
672 // relocs that apply to it, then we must do the special handling
673 // before we apply the relocs.
674 if (offset == -1 && reloc_shndx[i] != 0)
675 this->set_relocs_must_follow_section_writes();
676 }
677
678 delete sd->section_headers;
679 sd->section_headers = NULL;
680 delete sd->section_names;
681 sd->section_names = NULL;
682 }
683
684 // Add the symbols to the symbol table.
685
686 template<int size, bool big_endian>
687 void
688 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
689 Read_symbols_data* sd)
690 {
691 if (sd->symbols == NULL)
692 {
693 gold_assert(sd->symbol_names == NULL);
694 return;
695 }
696
697 const int sym_size = This::sym_size;
698 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
699 / sym_size);
700 if (static_cast<off_t>(symcount * sym_size)
701 != sd->symbols_size - sd->external_symbols_offset)
702 {
703 this->error(_("size of symbols is not multiple of symbol size"));
704 return;
705 }
706
707 this->symbols_.resize(symcount);
708
709 const char* sym_names =
710 reinterpret_cast<const char*>(sd->symbol_names->data());
711 symtab->add_from_relobj(this,
712 sd->symbols->data() + sd->external_symbols_offset,
713 symcount, sym_names, sd->symbol_names_size,
714 &this->symbols_);
715
716 delete sd->symbols;
717 sd->symbols = NULL;
718 delete sd->symbol_names;
719 sd->symbol_names = NULL;
720 }
721
722 // Finalize the local symbols. Here we record the file offset at
723 // which they should be output, we add their names to *POOL, and we
724 // add their values to THIS->LOCAL_VALUES_. Return the symbol index.
725 // This function is always called from the main thread. The actual
726 // output of the local symbols will occur in a separate task.
727
728 template<int size, bool big_endian>
729 unsigned int
730 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
731 off_t off,
732 Stringpool* pool)
733 {
734 gold_assert(this->symtab_shndx_ != -1U);
735 if (this->symtab_shndx_ == 0)
736 {
737 // This object has no symbols. Weird but legal.
738 return index;
739 }
740
741 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
742
743 this->local_symbol_offset_ = off;
744
745 // Read the symbol table section header.
746 const unsigned int symtab_shndx = this->symtab_shndx_;
747 typename This::Shdr symtabshdr(this,
748 this->elf_file_.section_header(symtab_shndx));
749 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
750
751 // Read the local symbols.
752 const int sym_size = This::sym_size;
753 const unsigned int loccount = this->local_symbol_count_;
754 gold_assert(loccount == symtabshdr.get_sh_info());
755 off_t locsize = loccount * sym_size;
756 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
757 locsize, true);
758
759 this->local_values_.resize(loccount);
760
761 // Read the symbol names.
762 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
763 off_t strtab_size;
764 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
765 &strtab_size,
766 true);
767 const char* pnames = reinterpret_cast<const char*>(pnamesu);
768
769 // Loop over the local symbols.
770
771 const std::vector<Map_to_output>& mo(this->map_to_output());
772 unsigned int shnum = this->shnum();
773 unsigned int count = 0;
774 // Skip the first, dummy, symbol.
775 psyms += sym_size;
776 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
777 {
778 elfcpp::Sym<size, big_endian> sym(psyms);
779
780 Symbol_value<size>& lv(this->local_values_[i]);
781
782 unsigned int shndx = sym.get_st_shndx();
783 lv.set_input_shndx(shndx);
784
785 if (sym.get_st_type() == elfcpp::STT_SECTION)
786 lv.set_is_section_symbol();
787
788 if (shndx >= elfcpp::SHN_LORESERVE)
789 {
790 if (shndx == elfcpp::SHN_ABS)
791 lv.set_output_value(sym.get_st_value());
792 else
793 {
794 // FIXME: Handle SHN_XINDEX.
795 this->error(_("unknown section index %u for local symbol %u"),
796 shndx, i);
797 lv.set_output_value(0);
798 }
799 }
800 else
801 {
802 if (shndx >= shnum)
803 {
804 this->error(_("local symbol %u section index %u out of range"),
805 i, shndx);
806 shndx = 0;
807 }
808
809 Output_section* os = mo[shndx].output_section;
810
811 if (os == NULL)
812 {
813 lv.set_output_value(0);
814 lv.set_no_output_symtab_entry();
815 continue;
816 }
817
818 if (mo[shndx].offset == -1)
819 lv.set_input_value(sym.get_st_value());
820 else
821 lv.set_output_value(mo[shndx].output_section->address()
822 + mo[shndx].offset
823 + sym.get_st_value());
824 }
825
826 // Decide whether this symbol should go into the output file.
827
828 if (sym.get_st_type() == elfcpp::STT_SECTION)
829 {
830 lv.set_no_output_symtab_entry();
831 continue;
832 }
833
834 if (sym.get_st_name() >= strtab_size)
835 {
836 this->error(_("local symbol %u section name out of range: %u >= %u"),
837 i, sym.get_st_name(),
838 static_cast<unsigned int>(strtab_size));
839 lv.set_no_output_symtab_entry();
840 continue;
841 }
842
843 const char* name = pnames + sym.get_st_name();
844 pool->add(name, true, NULL);
845 lv.set_output_symtab_index(index);
846 ++index;
847 ++count;
848 }
849
850 this->output_local_symbol_count_ = count;
851
852 return index;
853 }
854
855 // Return the value of the local symbol symndx.
856 template<int size, bool big_endian>
857 typename elfcpp::Elf_types<size>::Elf_Addr
858 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
859 {
860 gold_assert(symndx < this->local_symbol_count_);
861 gold_assert(symndx < this->local_values_.size());
862 const Symbol_value<size>& lv(this->local_values_[symndx]);
863 return lv.value(this, 0);
864 }
865
866 // Return the value of a local symbol defined in input section SHNDX,
867 // with value VALUE, adding addend ADDEND. IS_SECTION_SYMBOL
868 // indicates whether the symbol is a section symbol. This handles
869 // SHF_MERGE sections.
870 template<int size, bool big_endian>
871 typename elfcpp::Elf_types<size>::Elf_Addr
872 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
873 Address value,
874 bool is_section_symbol,
875 Address addend) const
876 {
877 const std::vector<Map_to_output>& mo(this->map_to_output());
878 Output_section* os = mo[shndx].output_section;
879 if (os == NULL)
880 return addend;
881 gold_assert(mo[shndx].offset == -1);
882
883 // Do the mapping required by the output section. If this is not a
884 // section symbol, then we want to map the symbol value, and then
885 // include the addend. If this is a section symbol, then we need to
886 // include the addend to figure out where in the section we are,
887 // before we do the mapping. This will do the right thing provided
888 // the assembler is careful to only convert a relocation in a merged
889 // section to a section symbol if there is a zero addend. If the
890 // assembler does not do this, then in general we can't know what to
891 // do, because we can't distinguish the addend for the instruction
892 // format from the addend for the section offset.
893
894 if (is_section_symbol)
895 return os->output_address(this, shndx, value + addend);
896 else
897 return addend + os->output_address(this, shndx, value);
898 }
899
900 // Write out the local symbols.
901
902 template<int size, bool big_endian>
903 void
904 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
905 const Stringpool* sympool)
906 {
907 if (parameters->strip_all())
908 return;
909
910 gold_assert(this->symtab_shndx_ != -1U);
911 if (this->symtab_shndx_ == 0)
912 {
913 // This object has no symbols. Weird but legal.
914 return;
915 }
916
917 // Read the symbol table section header.
918 const unsigned int symtab_shndx = this->symtab_shndx_;
919 typename This::Shdr symtabshdr(this,
920 this->elf_file_.section_header(symtab_shndx));
921 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
922 const unsigned int loccount = this->local_symbol_count_;
923 gold_assert(loccount == symtabshdr.get_sh_info());
924
925 // Read the local symbols.
926 const int sym_size = This::sym_size;
927 off_t locsize = loccount * sym_size;
928 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
929 locsize, false);
930
931 // Read the symbol names.
932 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
933 off_t strtab_size;
934 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
935 &strtab_size,
936 true);
937 const char* pnames = reinterpret_cast<const char*>(pnamesu);
938
939 // Get a view into the output file.
940 off_t output_size = this->output_local_symbol_count_ * sym_size;
941 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
942 output_size);
943
944 const std::vector<Map_to_output>& mo(this->map_to_output());
945
946 gold_assert(this->local_values_.size() == loccount);
947
948 unsigned char* ov = oview;
949 psyms += sym_size;
950 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
951 {
952 elfcpp::Sym<size, big_endian> isym(psyms);
953
954 if (!this->local_values_[i].needs_output_symtab_entry())
955 continue;
956
957 unsigned int st_shndx = isym.get_st_shndx();
958 if (st_shndx < elfcpp::SHN_LORESERVE)
959 {
960 gold_assert(st_shndx < mo.size());
961 if (mo[st_shndx].output_section == NULL)
962 continue;
963 st_shndx = mo[st_shndx].output_section->out_shndx();
964 }
965
966 elfcpp::Sym_write<size, big_endian> osym(ov);
967
968 gold_assert(isym.get_st_name() < strtab_size);
969 const char* name = pnames + isym.get_st_name();
970 osym.put_st_name(sympool->get_offset(name));
971 osym.put_st_value(this->local_values_[i].value(this, 0));
972 osym.put_st_size(isym.get_st_size());
973 osym.put_st_info(isym.get_st_info());
974 osym.put_st_other(isym.get_st_other());
975 osym.put_st_shndx(st_shndx);
976
977 ov += sym_size;
978 }
979
980 gold_assert(ov - oview == output_size);
981
982 of->write_output_view(this->local_symbol_offset_, output_size, oview);
983 }
984
985 // Set *INFO to symbolic information about the offset OFFSET in the
986 // section SHNDX. Return true if we found something, false if we
987 // found nothing.
988
989 template<int size, bool big_endian>
990 bool
991 Sized_relobj<size, big_endian>::get_symbol_location_info(
992 unsigned int shndx,
993 off_t offset,
994 Symbol_location_info* info)
995 {
996 if (this->symtab_shndx_ == 0)
997 return false;
998
999 off_t symbols_size;
1000 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1001 &symbols_size,
1002 false);
1003
1004 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1005 off_t names_size;
1006 const unsigned char* symbol_names_u =
1007 this->section_contents(symbol_names_shndx, &names_size, false);
1008 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1009
1010 const int sym_size = This::sym_size;
1011 const size_t count = symbols_size / sym_size;
1012
1013 const unsigned char* p = symbols;
1014 for (size_t i = 0; i < count; ++i, p += sym_size)
1015 {
1016 elfcpp::Sym<size, big_endian> sym(p);
1017
1018 if (sym.get_st_type() == elfcpp::STT_FILE)
1019 {
1020 if (sym.get_st_name() >= names_size)
1021 info->source_file = "(invalid)";
1022 else
1023 info->source_file = symbol_names + sym.get_st_name();
1024 }
1025 else if (sym.get_st_shndx() == shndx
1026 && static_cast<off_t>(sym.get_st_value()) <= offset
1027 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1028 > offset))
1029 {
1030 if (sym.get_st_name() > names_size)
1031 info->enclosing_symbol_name = "(invalid)";
1032 else
1033 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1034 return true;
1035 }
1036 }
1037
1038 return false;
1039 }
1040
1041 // Input_objects methods.
1042
1043 // Add a regular relocatable object to the list. Return false if this
1044 // object should be ignored.
1045
1046 bool
1047 Input_objects::add_object(Object* obj)
1048 {
1049 Target* target = obj->target();
1050 if (this->target_ == NULL)
1051 this->target_ = target;
1052 else if (this->target_ != target)
1053 {
1054 gold_error(_("%s: incompatible target"), obj->name().c_str());
1055 return false;
1056 }
1057
1058 if (!obj->is_dynamic())
1059 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1060 else
1061 {
1062 // See if this is a duplicate SONAME.
1063 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1064
1065 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1066 this->sonames_.insert(dynobj->soname());
1067 if (!ins.second)
1068 {
1069 // We have already seen a dynamic object with this soname.
1070 return false;
1071 }
1072
1073 this->dynobj_list_.push_back(dynobj);
1074 }
1075
1076 set_parameters_size_and_endianness(target->get_size(),
1077 target->is_big_endian());
1078
1079 return true;
1080 }
1081
1082 // Relocate_info methods.
1083
1084 // Return a string describing the location of a relocation. This is
1085 // only used in error messages.
1086
1087 template<int size, bool big_endian>
1088 std::string
1089 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1090 {
1091 // See if we can get line-number information from debugging sections.
1092 std::string filename;
1093 std::string file_and_lineno; // Better than filename-only, if available.
1094 for (unsigned int shndx = 0; shndx < this->object->shnum(); ++shndx)
1095 if (this->object->section_name(shndx) == ".debug_line")
1096 {
1097 off_t debuglines_size;
1098 const unsigned char* debuglines = this->object->section_contents(
1099 shndx, &debuglines_size, false);
1100 if (debuglines)
1101 {
1102 Dwarf_line_info<size, big_endian> line_info(debuglines,
1103 debuglines_size);
1104 line_info.read_line_mappings();
1105 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1106 }
1107 break;
1108 }
1109
1110 std::string ret(this->object->name());
1111 ret += ':';
1112 Symbol_location_info info;
1113 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1114 {
1115 ret += " in function ";
1116 // We could demangle this name before printing, but we don't
1117 // bother because gcc runs linker output through a demangle
1118 // filter itself. The only advantage to demangling here is if
1119 // someone might call ld directly, rather than via gcc. If we
1120 // did want to demangle, cplus_demangle() is in libiberty.
1121 ret += info.enclosing_symbol_name;
1122 ret += ":";
1123 filename = info.source_file;
1124 }
1125
1126 if (!file_and_lineno.empty())
1127 ret += file_and_lineno;
1128 else
1129 {
1130 if (!filename.empty())
1131 ret += filename;
1132 ret += "(";
1133 ret += this->object->section_name(this->data_shndx);
1134 char buf[100];
1135 // Offsets into sections have to be positive.
1136 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1137 ret += buf;
1138 ret += ")";
1139 }
1140 return ret;
1141 }
1142
1143 } // End namespace gold.
1144
1145 namespace
1146 {
1147
1148 using namespace gold;
1149
1150 // Read an ELF file with the header and return the appropriate
1151 // instance of Object.
1152
1153 template<int size, bool big_endian>
1154 Object*
1155 make_elf_sized_object(const std::string& name, Input_file* input_file,
1156 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1157 {
1158 int et = ehdr.get_e_type();
1159 if (et == elfcpp::ET_REL)
1160 {
1161 Sized_relobj<size, big_endian>* obj =
1162 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1163 obj->setup(ehdr);
1164 return obj;
1165 }
1166 else if (et == elfcpp::ET_DYN)
1167 {
1168 Sized_dynobj<size, big_endian>* obj =
1169 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1170 obj->setup(ehdr);
1171 return obj;
1172 }
1173 else
1174 {
1175 gold_error(_("%s: unsupported ELF file type %d"),
1176 name.c_str(), et);
1177 return NULL;
1178 }
1179 }
1180
1181 } // End anonymous namespace.
1182
1183 namespace gold
1184 {
1185
1186 // Read an ELF file and return the appropriate instance of Object.
1187
1188 Object*
1189 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1190 const unsigned char* p, off_t bytes)
1191 {
1192 if (bytes < elfcpp::EI_NIDENT)
1193 {
1194 gold_error(_("%s: ELF file too short"), name.c_str());
1195 return NULL;
1196 }
1197
1198 int v = p[elfcpp::EI_VERSION];
1199 if (v != elfcpp::EV_CURRENT)
1200 {
1201 if (v == elfcpp::EV_NONE)
1202 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1203 else
1204 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1205 return NULL;
1206 }
1207
1208 int c = p[elfcpp::EI_CLASS];
1209 if (c == elfcpp::ELFCLASSNONE)
1210 {
1211 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1212 return NULL;
1213 }
1214 else if (c != elfcpp::ELFCLASS32
1215 && c != elfcpp::ELFCLASS64)
1216 {
1217 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1218 return NULL;
1219 }
1220
1221 int d = p[elfcpp::EI_DATA];
1222 if (d == elfcpp::ELFDATANONE)
1223 {
1224 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1225 return NULL;
1226 }
1227 else if (d != elfcpp::ELFDATA2LSB
1228 && d != elfcpp::ELFDATA2MSB)
1229 {
1230 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1231 return NULL;
1232 }
1233
1234 bool big_endian = d == elfcpp::ELFDATA2MSB;
1235
1236 if (c == elfcpp::ELFCLASS32)
1237 {
1238 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1239 {
1240 gold_error(_("%s: ELF file too short"), name.c_str());
1241 return NULL;
1242 }
1243 if (big_endian)
1244 {
1245 #ifdef HAVE_TARGET_32_BIG
1246 elfcpp::Ehdr<32, true> ehdr(p);
1247 return make_elf_sized_object<32, true>(name, input_file,
1248 offset, ehdr);
1249 #else
1250 gold_error(_("%s: not configured to support "
1251 "32-bit big-endian object"),
1252 name.c_str());
1253 return NULL;
1254 #endif
1255 }
1256 else
1257 {
1258 #ifdef HAVE_TARGET_32_LITTLE
1259 elfcpp::Ehdr<32, false> ehdr(p);
1260 return make_elf_sized_object<32, false>(name, input_file,
1261 offset, ehdr);
1262 #else
1263 gold_error(_("%s: not configured to support "
1264 "32-bit little-endian object"),
1265 name.c_str());
1266 return NULL;
1267 #endif
1268 }
1269 }
1270 else
1271 {
1272 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1273 {
1274 gold_error(_("%s: ELF file too short"), name.c_str());
1275 return NULL;
1276 }
1277 if (big_endian)
1278 {
1279 #ifdef HAVE_TARGET_64_BIG
1280 elfcpp::Ehdr<64, true> ehdr(p);
1281 return make_elf_sized_object<64, true>(name, input_file,
1282 offset, ehdr);
1283 #else
1284 gold_error(_("%s: not configured to support "
1285 "64-bit big-endian object"),
1286 name.c_str());
1287 return NULL;
1288 #endif
1289 }
1290 else
1291 {
1292 #ifdef HAVE_TARGET_64_LITTLE
1293 elfcpp::Ehdr<64, false> ehdr(p);
1294 return make_elf_sized_object<64, false>(name, input_file,
1295 offset, ehdr);
1296 #else
1297 gold_error(_("%s: not configured to support "
1298 "64-bit little-endian object"),
1299 name.c_str());
1300 return NULL;
1301 #endif
1302 }
1303 }
1304 }
1305
1306 // Instantiate the templates we need. We could use the configure
1307 // script to restrict this to only the ones for implemented targets.
1308
1309 #ifdef HAVE_TARGET_32_LITTLE
1310 template
1311 class Sized_relobj<32, false>;
1312 #endif
1313
1314 #ifdef HAVE_TARGET_32_BIG
1315 template
1316 class Sized_relobj<32, true>;
1317 #endif
1318
1319 #ifdef HAVE_TARGET_64_LITTLE
1320 template
1321 class Sized_relobj<64, false>;
1322 #endif
1323
1324 #ifdef HAVE_TARGET_64_BIG
1325 template
1326 class Sized_relobj<64, true>;
1327 #endif
1328
1329 #ifdef HAVE_TARGET_32_LITTLE
1330 template
1331 struct Relocate_info<32, false>;
1332 #endif
1333
1334 #ifdef HAVE_TARGET_32_BIG
1335 template
1336 struct Relocate_info<32, true>;
1337 #endif
1338
1339 #ifdef HAVE_TARGET_64_LITTLE
1340 template
1341 struct Relocate_info<64, false>;
1342 #endif
1343
1344 #ifdef HAVE_TARGET_64_BIG
1345 template
1346 struct Relocate_info<64, true>;
1347 #endif
1348
1349 } // End namespace gold.