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