* fileread.cc (File_read::find_view): Add byteshift and vshifted
[binutils-gdb.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007, 2008 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, section_size_type* plen,
79 bool cache)
80 {
81 Location loc(this->do_section_contents(shndx));
82 *plen = convert_to_section_size_type(loc.data_size);
83 return this->get_view(loc.file_offset, *plen, true, cache);
84 }
85
86 // Read the section data into SD. This is code common to Sized_relobj
87 // and Sized_dynobj, so we put it into Object.
88
89 template<int size, bool big_endian>
90 void
91 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
92 Read_symbols_data* sd)
93 {
94 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
95
96 // Read the section headers.
97 const off_t shoff = elf_file->shoff();
98 const unsigned int shnum = this->shnum();
99 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
100 true, true);
101
102 // Read the section names.
103 const unsigned char* pshdrs = sd->section_headers->data();
104 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
105 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
106
107 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
108 this->error(_("section name section has wrong type: %u"),
109 static_cast<unsigned int>(shdrnames.get_sh_type()));
110
111 sd->section_names_size =
112 convert_to_section_size_type(shdrnames.get_sh_size());
113 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
114 sd->section_names_size, false,
115 false);
116 }
117
118 // If NAME is the name of a special .gnu.warning section, arrange for
119 // the warning to be issued. SHNDX is the section index. Return
120 // whether it is a warning section.
121
122 bool
123 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
124 Symbol_table* symtab)
125 {
126 const char warn_prefix[] = ".gnu.warning.";
127 const int warn_prefix_len = sizeof warn_prefix - 1;
128 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
129 {
130 // Read the section contents to get the warning text. It would
131 // be nicer if we only did this if we have to actually issue a
132 // warning. Unfortunately, warnings are issued as we relocate
133 // sections. That means that we can not lock the object then,
134 // as we might try to issue the same warning multiple times
135 // simultaneously.
136 section_size_type len;
137 const unsigned char* contents = this->section_contents(shndx, &len,
138 false);
139 std::string warning(reinterpret_cast<const char*>(contents), len);
140 symtab->add_warning(name + warn_prefix_len, this, warning);
141 return true;
142 }
143 return false;
144 }
145
146 // Class Sized_relobj.
147
148 template<int size, bool big_endian>
149 Sized_relobj<size, big_endian>::Sized_relobj(
150 const std::string& name,
151 Input_file* input_file,
152 off_t offset,
153 const elfcpp::Ehdr<size, big_endian>& ehdr)
154 : Relobj(name, input_file, offset),
155 elf_file_(this, ehdr),
156 symtab_shndx_(-1U),
157 local_symbol_count_(0),
158 output_local_symbol_count_(0),
159 output_local_dynsym_count_(0),
160 symbols_(),
161 local_symbol_offset_(0),
162 local_dynsym_offset_(0),
163 local_values_(),
164 local_got_offsets_(),
165 has_eh_frame_(false)
166 {
167 }
168
169 template<int size, bool big_endian>
170 Sized_relobj<size, big_endian>::~Sized_relobj()
171 {
172 }
173
174 // Set up an object file based on the file header. This sets up the
175 // target and reads the section information.
176
177 template<int size, bool big_endian>
178 void
179 Sized_relobj<size, big_endian>::setup(
180 const elfcpp::Ehdr<size, big_endian>& ehdr)
181 {
182 this->set_target(ehdr.get_e_machine(), size, big_endian,
183 ehdr.get_e_ident()[elfcpp::EI_OSABI],
184 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
185
186 const unsigned int shnum = this->elf_file_.shnum();
187 this->set_shnum(shnum);
188 }
189
190 // Find the SHT_SYMTAB section, given the section headers. The ELF
191 // standard says that maybe in the future there can be more than one
192 // SHT_SYMTAB section. Until somebody figures out how that could
193 // work, we assume there is only one.
194
195 template<int size, bool big_endian>
196 void
197 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
198 {
199 const unsigned int shnum = this->shnum();
200 this->symtab_shndx_ = 0;
201 if (shnum > 0)
202 {
203 // Look through the sections in reverse order, since gas tends
204 // to put the symbol table at the end.
205 const unsigned char* p = pshdrs + shnum * This::shdr_size;
206 unsigned int i = shnum;
207 while (i > 0)
208 {
209 --i;
210 p -= This::shdr_size;
211 typename This::Shdr shdr(p);
212 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
213 {
214 this->symtab_shndx_ = i;
215 break;
216 }
217 }
218 }
219 }
220
221 // Return whether SHDR has the right type and flags to be a GNU
222 // .eh_frame section.
223
224 template<int size, bool big_endian>
225 bool
226 Sized_relobj<size, big_endian>::check_eh_frame_flags(
227 const elfcpp::Shdr<size, big_endian>* shdr) const
228 {
229 return (shdr->get_sh_size() > 0
230 && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
231 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
232 }
233
234 // Return whether there is a GNU .eh_frame section, given the section
235 // headers and the section names.
236
237 template<int size, bool big_endian>
238 bool
239 Sized_relobj<size, big_endian>::find_eh_frame(
240 const unsigned char* pshdrs,
241 const char* names,
242 section_size_type names_size) const
243 {
244 const unsigned int shnum = this->shnum();
245 const unsigned char* p = pshdrs + This::shdr_size;
246 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
247 {
248 typename This::Shdr shdr(p);
249 if (this->check_eh_frame_flags(&shdr))
250 {
251 if (shdr.get_sh_name() >= names_size)
252 {
253 this->error(_("bad section name offset for section %u: %lu"),
254 i, static_cast<unsigned long>(shdr.get_sh_name()));
255 continue;
256 }
257
258 const char* name = names + shdr.get_sh_name();
259 if (strcmp(name, ".eh_frame") == 0)
260 return true;
261 }
262 }
263 return false;
264 }
265
266 // Read the sections and symbols from an object file.
267
268 template<int size, bool big_endian>
269 void
270 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
271 {
272 this->read_section_data(&this->elf_file_, sd);
273
274 const unsigned char* const pshdrs = sd->section_headers->data();
275
276 this->find_symtab(pshdrs);
277
278 const unsigned char* namesu = sd->section_names->data();
279 const char* names = reinterpret_cast<const char*>(namesu);
280 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
281 {
282 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
283 this->has_eh_frame_ = true;
284 }
285
286 sd->symbols = NULL;
287 sd->symbols_size = 0;
288 sd->external_symbols_offset = 0;
289 sd->symbol_names = NULL;
290 sd->symbol_names_size = 0;
291
292 if (this->symtab_shndx_ == 0)
293 {
294 // No symbol table. Weird but legal.
295 return;
296 }
297
298 // Get the symbol table section header.
299 typename This::Shdr symtabshdr(pshdrs
300 + this->symtab_shndx_ * This::shdr_size);
301 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
302
303 // If this object has a .eh_frame section, we need all the symbols.
304 // Otherwise we only need the external symbols. While it would be
305 // simpler to just always read all the symbols, I've seen object
306 // files with well over 2000 local symbols, which for a 64-bit
307 // object file format is over 5 pages that we don't need to read
308 // now.
309
310 const int sym_size = This::sym_size;
311 const unsigned int loccount = symtabshdr.get_sh_info();
312 this->local_symbol_count_ = loccount;
313 this->local_values_.resize(loccount);
314 section_offset_type locsize = loccount * sym_size;
315 off_t dataoff = symtabshdr.get_sh_offset();
316 section_size_type datasize =
317 convert_to_section_size_type(symtabshdr.get_sh_size());
318 off_t extoff = dataoff + locsize;
319 section_size_type extsize = datasize - locsize;
320
321 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
322 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
323
324 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
325
326 // Read the section header for the symbol names.
327 unsigned int strtab_shndx = symtabshdr.get_sh_link();
328 if (strtab_shndx >= this->shnum())
329 {
330 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
331 return;
332 }
333 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
334 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
335 {
336 this->error(_("symbol table name section has wrong type: %u"),
337 static_cast<unsigned int>(strtabshdr.get_sh_type()));
338 return;
339 }
340
341 // Read the symbol names.
342 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
343 strtabshdr.get_sh_size(),
344 false, true);
345
346 sd->symbols = fvsymtab;
347 sd->symbols_size = readsize;
348 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
349 sd->symbol_names = fvstrtab;
350 sd->symbol_names_size =
351 convert_to_section_size_type(strtabshdr.get_sh_size());
352 }
353
354 // Return the section index of symbol SYM. Set *VALUE to its value in
355 // the object file. Note that for a symbol which is not defined in
356 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
357 // it will not return the final value of the symbol in the link.
358
359 template<int size, bool big_endian>
360 unsigned int
361 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
362 Address* value)
363 {
364 section_size_type symbols_size;
365 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
366 &symbols_size,
367 false);
368
369 const size_t count = symbols_size / This::sym_size;
370 gold_assert(sym < count);
371
372 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
373 *value = elfsym.get_st_value();
374 // FIXME: Handle SHN_XINDEX.
375 return elfsym.get_st_shndx();
376 }
377
378 // Return whether to include a section group in the link. LAYOUT is
379 // used to keep track of which section groups we have already seen.
380 // INDEX is the index of the section group and SHDR is the section
381 // header. If we do not want to include this group, we set bits in
382 // OMIT for each section which should be discarded.
383
384 template<int size, bool big_endian>
385 bool
386 Sized_relobj<size, big_endian>::include_section_group(
387 Symbol_table* symtab,
388 Layout* layout,
389 unsigned int index,
390 const char* name,
391 const elfcpp::Shdr<size, big_endian>& shdr,
392 std::vector<bool>* omit)
393 {
394 // Read the section contents.
395 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
396 shdr.get_sh_size(), true, false);
397 const elfcpp::Elf_Word* pword =
398 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
399
400 // The first word contains flags. We only care about COMDAT section
401 // groups. Other section groups are always included in the link
402 // just like ordinary sections.
403 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
404
405 // Look up the group signature, which is the name of a symbol. This
406 // is a lot of effort to go to to read a string. Why didn't they
407 // just have the group signature point into the string table, rather
408 // than indirect through a symbol?
409
410 // Get the appropriate symbol table header (this will normally be
411 // the single SHT_SYMTAB section, but in principle it need not be).
412 const unsigned int link = shdr.get_sh_link();
413 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
414
415 // Read the symbol table entry.
416 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
417 {
418 this->error(_("section group %u info %u out of range"),
419 index, shdr.get_sh_info());
420 return false;
421 }
422 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
423 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
424 false);
425 elfcpp::Sym<size, big_endian> sym(psym);
426
427 // Read the symbol table names.
428 section_size_type symnamelen;
429 const unsigned char* psymnamesu;
430 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
431 true);
432 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
433
434 // Get the section group signature.
435 if (sym.get_st_name() >= symnamelen)
436 {
437 this->error(_("symbol %u name offset %u out of range"),
438 shdr.get_sh_info(), sym.get_st_name());
439 return false;
440 }
441
442 const char* signature = psymnames + sym.get_st_name();
443
444 // It seems that some versions of gas will create a section group
445 // associated with a section symbol, and then fail to give a name to
446 // the section symbol. In such a case, use the name of the section.
447 // FIXME.
448 std::string secname;
449 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
450 {
451 secname = this->section_name(sym.get_st_shndx());
452 signature = secname.c_str();
453 }
454
455 // Record this section group, and see whether we've already seen one
456 // with the same signature.
457
458 if ((flags & elfcpp::GRP_COMDAT) == 0
459 || layout->add_comdat(signature, true))
460 {
461 if (parameters->options().relocatable())
462 layout->layout_group(symtab, this, index, name, signature, shdr,
463 pword);
464 return true;
465 }
466
467 // This is a duplicate. We want to discard the sections in this
468 // group.
469 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
470 for (size_t i = 1; i < count; ++i)
471 {
472 elfcpp::Elf_Word secnum =
473 elfcpp::Swap<32, big_endian>::readval(pword + i);
474 if (secnum >= this->shnum())
475 {
476 this->error(_("section %u in section group %u out of range"),
477 secnum, index);
478 continue;
479 }
480 (*omit)[secnum] = true;
481 }
482
483 return false;
484 }
485
486 // Whether to include a linkonce section in the link. NAME is the
487 // name of the section and SHDR is the section header.
488
489 // Linkonce sections are a GNU extension implemented in the original
490 // GNU linker before section groups were defined. The semantics are
491 // that we only include one linkonce section with a given name. The
492 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
493 // where T is the type of section and SYMNAME is the name of a symbol.
494 // In an attempt to make linkonce sections interact well with section
495 // groups, we try to identify SYMNAME and use it like a section group
496 // signature. We want to block section groups with that signature,
497 // but not other linkonce sections with that signature. We also use
498 // the full name of the linkonce section as a normal section group
499 // signature.
500
501 template<int size, bool big_endian>
502 bool
503 Sized_relobj<size, big_endian>::include_linkonce_section(
504 Layout* layout,
505 const char* name,
506 const elfcpp::Shdr<size, big_endian>&)
507 {
508 // In general the symbol name we want will be the string following
509 // the last '.'. However, we have to handle the case of
510 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
511 // some versions of gcc. So we use a heuristic: if the name starts
512 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
513 // we look for the last '.'. We can't always simply skip
514 // ".gnu.linkonce.X", because we have to deal with cases like
515 // ".gnu.linkonce.d.rel.ro.local".
516 const char* const linkonce_t = ".gnu.linkonce.t.";
517 const char* symname;
518 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
519 symname = name + strlen(linkonce_t);
520 else
521 symname = strrchr(name, '.') + 1;
522 bool include1 = layout->add_comdat(symname, false);
523 bool include2 = layout->add_comdat(name, true);
524 return include1 && include2;
525 }
526
527 // Lay out the input sections. We walk through the sections and check
528 // whether they should be included in the link. If they should, we
529 // pass them to the Layout object, which will return an output section
530 // and an offset.
531
532 template<int size, bool big_endian>
533 void
534 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
535 Layout* layout,
536 Read_symbols_data* sd)
537 {
538 const unsigned int shnum = this->shnum();
539 if (shnum == 0)
540 return;
541
542 // Get the section headers.
543 const unsigned char* pshdrs = sd->section_headers->data();
544
545 // Get the section names.
546 const unsigned char* pnamesu = sd->section_names->data();
547 const char* pnames = reinterpret_cast<const char*>(pnamesu);
548
549 // For each section, record the index of the reloc section if any.
550 // Use 0 to mean that there is no reloc section, -1U to mean that
551 // there is more than one.
552 std::vector<unsigned int> reloc_shndx(shnum, 0);
553 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
554 // Skip the first, dummy, section.
555 pshdrs += This::shdr_size;
556 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
557 {
558 typename This::Shdr shdr(pshdrs);
559
560 unsigned int sh_type = shdr.get_sh_type();
561 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
562 {
563 unsigned int target_shndx = shdr.get_sh_info();
564 if (target_shndx == 0 || target_shndx >= shnum)
565 {
566 this->error(_("relocation section %u has bad info %u"),
567 i, target_shndx);
568 continue;
569 }
570
571 if (reloc_shndx[target_shndx] != 0)
572 reloc_shndx[target_shndx] = -1U;
573 else
574 {
575 reloc_shndx[target_shndx] = i;
576 reloc_type[target_shndx] = sh_type;
577 }
578 }
579 }
580
581 std::vector<Map_to_output>& map_sections(this->map_to_output());
582 map_sections.resize(shnum);
583
584 // If we are only linking for symbols, then there is nothing else to
585 // do here.
586 if (this->input_file()->just_symbols())
587 {
588 delete sd->section_headers;
589 sd->section_headers = NULL;
590 delete sd->section_names;
591 sd->section_names = NULL;
592 return;
593 }
594
595 // Whether we've seen a .note.GNU-stack section.
596 bool seen_gnu_stack = false;
597 // The flags of a .note.GNU-stack section.
598 uint64_t gnu_stack_flags = 0;
599
600 // Keep track of which sections to omit.
601 std::vector<bool> omit(shnum, false);
602
603 // Keep track of reloc sections when emitting relocations.
604 const bool relocatable = parameters->options().relocatable();
605 const bool emit_relocs = (relocatable
606 || parameters->options().emit_relocs());
607 std::vector<unsigned int> reloc_sections;
608
609 // Keep track of .eh_frame sections.
610 std::vector<unsigned int> eh_frame_sections;
611
612 // Skip the first, dummy, section.
613 pshdrs = sd->section_headers->data() + This::shdr_size;
614 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
615 {
616 typename This::Shdr shdr(pshdrs);
617
618 if (shdr.get_sh_name() >= sd->section_names_size)
619 {
620 this->error(_("bad section name offset for section %u: %lu"),
621 i, static_cast<unsigned long>(shdr.get_sh_name()));
622 return;
623 }
624
625 const char* name = pnames + shdr.get_sh_name();
626
627 if (this->handle_gnu_warning_section(name, i, symtab))
628 {
629 if (!relocatable)
630 omit[i] = true;
631 }
632
633 // The .note.GNU-stack section is special. It gives the
634 // protection flags that this object file requires for the stack
635 // in memory.
636 if (strcmp(name, ".note.GNU-stack") == 0)
637 {
638 seen_gnu_stack = true;
639 gnu_stack_flags |= shdr.get_sh_flags();
640 omit[i] = true;
641 }
642
643 bool discard = omit[i];
644 if (!discard)
645 {
646 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
647 {
648 if (!this->include_section_group(symtab, layout, i, name, shdr,
649 &omit))
650 discard = true;
651 }
652 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
653 && Layout::is_linkonce(name))
654 {
655 if (!this->include_linkonce_section(layout, name, shdr))
656 discard = true;
657 }
658 }
659
660 if (discard)
661 {
662 // Do not include this section in the link.
663 map_sections[i].output_section = NULL;
664 continue;
665 }
666
667 // When doing a relocatable link we are going to copy input
668 // reloc sections into the output. We only want to copy the
669 // ones associated with sections which are not being discarded.
670 // However, we don't know that yet for all sections. So save
671 // reloc sections and process them later.
672 if (emit_relocs
673 && (shdr.get_sh_type() == elfcpp::SHT_REL
674 || shdr.get_sh_type() == elfcpp::SHT_RELA))
675 {
676 reloc_sections.push_back(i);
677 continue;
678 }
679
680 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
681 continue;
682
683 // The .eh_frame section is special. It holds exception frame
684 // information that we need to read in order to generate the
685 // exception frame header. We process these after all the other
686 // sections so that the exception frame reader can reliably
687 // determine which sections are being discarded, and discard the
688 // corresponding information.
689 if (!relocatable
690 && strcmp(name, ".eh_frame") == 0
691 && this->check_eh_frame_flags(&shdr))
692 {
693 eh_frame_sections.push_back(i);
694 continue;
695 }
696
697 off_t offset;
698 Output_section* os = layout->layout(this, i, name, shdr,
699 reloc_shndx[i], reloc_type[i],
700 &offset);
701
702 map_sections[i].output_section = os;
703 map_sections[i].offset = offset;
704
705 // If this section requires special handling, and if there are
706 // relocs that apply to it, then we must do the special handling
707 // before we apply the relocs.
708 if (offset == -1 && reloc_shndx[i] != 0)
709 this->set_relocs_must_follow_section_writes();
710 }
711
712 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
713
714 // When doing a relocatable link handle the reloc sections at the
715 // end.
716 if (emit_relocs)
717 this->size_relocatable_relocs();
718 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
719 p != reloc_sections.end();
720 ++p)
721 {
722 unsigned int i = *p;
723 const unsigned char* pshdr;
724 pshdr = sd->section_headers->data() + i * This::shdr_size;
725 typename This::Shdr shdr(pshdr);
726
727 unsigned int data_shndx = shdr.get_sh_info();
728 if (data_shndx >= shnum)
729 {
730 // We already warned about this above.
731 continue;
732 }
733
734 Output_section* data_section = map_sections[data_shndx].output_section;
735 if (data_section == NULL)
736 {
737 map_sections[i].output_section = NULL;
738 continue;
739 }
740
741 Relocatable_relocs* rr = new Relocatable_relocs();
742 this->set_relocatable_relocs(i, rr);
743
744 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
745 rr);
746 map_sections[i].output_section = os;
747 map_sections[i].offset = -1;
748 }
749
750 // Handle the .eh_frame sections at the end.
751 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
752 p != eh_frame_sections.end();
753 ++p)
754 {
755 gold_assert(this->has_eh_frame_);
756 gold_assert(sd->external_symbols_offset != 0);
757
758 unsigned int i = *p;
759 const unsigned char *pshdr;
760 pshdr = sd->section_headers->data() + i * This::shdr_size;
761 typename This::Shdr shdr(pshdr);
762
763 off_t offset;
764 Output_section* os = layout->layout_eh_frame(this,
765 sd->symbols->data(),
766 sd->symbols_size,
767 sd->symbol_names->data(),
768 sd->symbol_names_size,
769 i, shdr,
770 reloc_shndx[i],
771 reloc_type[i],
772 &offset);
773 map_sections[i].output_section = os;
774 map_sections[i].offset = offset;
775
776 // If this section requires special handling, and if there are
777 // relocs that apply to it, then we must do the special handling
778 // before we apply the relocs.
779 if (offset == -1 && reloc_shndx[i] != 0)
780 this->set_relocs_must_follow_section_writes();
781 }
782
783 delete sd->section_headers;
784 sd->section_headers = NULL;
785 delete sd->section_names;
786 sd->section_names = NULL;
787 }
788
789 // Add the symbols to the symbol table.
790
791 template<int size, bool big_endian>
792 void
793 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
794 Read_symbols_data* sd)
795 {
796 if (sd->symbols == NULL)
797 {
798 gold_assert(sd->symbol_names == NULL);
799 return;
800 }
801
802 const int sym_size = This::sym_size;
803 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
804 / sym_size);
805 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
806 {
807 this->error(_("size of symbols is not multiple of symbol size"));
808 return;
809 }
810
811 this->symbols_.resize(symcount);
812
813 const char* sym_names =
814 reinterpret_cast<const char*>(sd->symbol_names->data());
815 symtab->add_from_relobj(this,
816 sd->symbols->data() + sd->external_symbols_offset,
817 symcount, sym_names, sd->symbol_names_size,
818 &this->symbols_);
819
820 delete sd->symbols;
821 sd->symbols = NULL;
822 delete sd->symbol_names;
823 sd->symbol_names = NULL;
824 }
825
826 // First pass over the local symbols. Here we add their names to
827 // *POOL and *DYNPOOL, and we store the symbol value in
828 // THIS->LOCAL_VALUES_. This function is always called from a
829 // singleton thread. This is followed by a call to
830 // finalize_local_symbols.
831
832 template<int size, bool big_endian>
833 void
834 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
835 Stringpool* dynpool)
836 {
837 gold_assert(this->symtab_shndx_ != -1U);
838 if (this->symtab_shndx_ == 0)
839 {
840 // This object has no symbols. Weird but legal.
841 return;
842 }
843
844 // Read the symbol table section header.
845 const unsigned int symtab_shndx = this->symtab_shndx_;
846 typename This::Shdr symtabshdr(this,
847 this->elf_file_.section_header(symtab_shndx));
848 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
849
850 // Read the local symbols.
851 const int sym_size = This::sym_size;
852 const unsigned int loccount = this->local_symbol_count_;
853 gold_assert(loccount == symtabshdr.get_sh_info());
854 off_t locsize = loccount * sym_size;
855 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
856 locsize, true, true);
857
858 // Read the symbol names.
859 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
860 section_size_type strtab_size;
861 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
862 &strtab_size,
863 true);
864 const char* pnames = reinterpret_cast<const char*>(pnamesu);
865
866 // Loop over the local symbols.
867
868 const std::vector<Map_to_output>& mo(this->map_to_output());
869 unsigned int shnum = this->shnum();
870 unsigned int count = 0;
871 unsigned int dyncount = 0;
872 // Skip the first, dummy, symbol.
873 psyms += sym_size;
874 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
875 {
876 elfcpp::Sym<size, big_endian> sym(psyms);
877
878 Symbol_value<size>& lv(this->local_values_[i]);
879
880 unsigned int shndx = sym.get_st_shndx();
881 lv.set_input_shndx(shndx);
882
883 if (sym.get_st_type() == elfcpp::STT_SECTION)
884 lv.set_is_section_symbol();
885 else if (sym.get_st_type() == elfcpp::STT_TLS)
886 lv.set_is_tls_symbol();
887
888 // Save the input symbol value for use in do_finalize_local_symbols().
889 lv.set_input_value(sym.get_st_value());
890
891 // Decide whether this symbol should go into the output file.
892
893 if (shndx < shnum && mo[shndx].output_section == NULL)
894 {
895 lv.set_no_output_symtab_entry();
896 gold_assert(!lv.needs_output_dynsym_entry());
897 continue;
898 }
899
900 if (sym.get_st_type() == elfcpp::STT_SECTION)
901 {
902 lv.set_no_output_symtab_entry();
903 gold_assert(!lv.needs_output_dynsym_entry());
904 continue;
905 }
906
907 if (sym.get_st_name() >= strtab_size)
908 {
909 this->error(_("local symbol %u section name out of range: %u >= %u"),
910 i, sym.get_st_name(),
911 static_cast<unsigned int>(strtab_size));
912 lv.set_no_output_symtab_entry();
913 continue;
914 }
915
916 // Add the symbol to the symbol table string pool.
917 const char* name = pnames + sym.get_st_name();
918 pool->add(name, true, NULL);
919 ++count;
920
921 // If needed, add the symbol to the dynamic symbol table string pool.
922 if (lv.needs_output_dynsym_entry())
923 {
924 dynpool->add(name, true, NULL);
925 ++dyncount;
926 }
927 }
928
929 this->output_local_symbol_count_ = count;
930 this->output_local_dynsym_count_ = dyncount;
931 }
932
933 // Finalize the local symbols. Here we set the final value in
934 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
935 // This function is always called from a singleton thread. The actual
936 // output of the local symbols will occur in a separate task.
937
938 template<int size, bool big_endian>
939 unsigned int
940 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
941 off_t off)
942 {
943 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
944
945 const unsigned int loccount = this->local_symbol_count_;
946 this->local_symbol_offset_ = off;
947
948 const std::vector<Map_to_output>& mo(this->map_to_output());
949 unsigned int shnum = this->shnum();
950
951 for (unsigned int i = 1; i < loccount; ++i)
952 {
953 Symbol_value<size>& lv(this->local_values_[i]);
954
955 unsigned int shndx = lv.input_shndx();
956
957 // Set the output symbol value.
958
959 if (shndx >= elfcpp::SHN_LORESERVE)
960 {
961 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
962 lv.set_output_value(lv.input_value());
963 else
964 {
965 // FIXME: Handle SHN_XINDEX.
966 this->error(_("unknown section index %u for local symbol %u"),
967 shndx, i);
968 lv.set_output_value(0);
969 }
970 }
971 else
972 {
973 if (shndx >= shnum)
974 {
975 this->error(_("local symbol %u section index %u out of range"),
976 i, shndx);
977 shndx = 0;
978 }
979
980 Output_section* os = mo[shndx].output_section;
981
982 if (os == NULL)
983 {
984 lv.set_output_value(0);
985 continue;
986 }
987 else if (mo[shndx].offset == -1)
988 {
989 // This is a SHF_MERGE section or one which otherwise
990 // requires special handling. We get the output address
991 // of the start of the merged section. If this is not a
992 // section symbol, we can then determine the final
993 // value. If it is a section symbol, we can not, as in
994 // that case we have to consider the addend to determine
995 // the value to use in a relocation.
996 if (!lv.is_section_symbol())
997 lv.set_output_value(os->output_address(this, shndx,
998 lv.input_value()));
999 else
1000 {
1001 section_offset_type start =
1002 os->starting_output_address(this, shndx);
1003 Merged_symbol_value<size>* msv =
1004 new Merged_symbol_value<size>(lv.input_value(), start);
1005 lv.set_merged_symbol_value(msv);
1006 }
1007 }
1008 else if (lv.is_tls_symbol())
1009 lv.set_output_value(os->tls_offset()
1010 + mo[shndx].offset
1011 + lv.input_value());
1012 else
1013 lv.set_output_value(os->address()
1014 + mo[shndx].offset
1015 + lv.input_value());
1016 }
1017
1018 if (lv.needs_output_symtab_entry())
1019 {
1020 lv.set_output_symtab_index(index);
1021 ++index;
1022 }
1023 }
1024 return index;
1025 }
1026
1027 // Set the output dynamic symbol table indexes for the local variables.
1028
1029 template<int size, bool big_endian>
1030 unsigned int
1031 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1032 {
1033 const unsigned int loccount = this->local_symbol_count_;
1034 for (unsigned int i = 1; i < loccount; ++i)
1035 {
1036 Symbol_value<size>& lv(this->local_values_[i]);
1037 if (lv.needs_output_dynsym_entry())
1038 {
1039 lv.set_output_dynsym_index(index);
1040 ++index;
1041 }
1042 }
1043 return index;
1044 }
1045
1046 // Set the offset where local dynamic symbol information will be stored.
1047 // Returns the count of local symbols contributed to the symbol table by
1048 // this object.
1049
1050 template<int size, bool big_endian>
1051 unsigned int
1052 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1053 {
1054 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1055 this->local_dynsym_offset_ = off;
1056 return this->output_local_dynsym_count_;
1057 }
1058
1059 // Write out the local symbols.
1060
1061 template<int size, bool big_endian>
1062 void
1063 Sized_relobj<size, big_endian>::write_local_symbols(
1064 Output_file* of,
1065 const Stringpool* sympool,
1066 const Stringpool* dynpool)
1067 {
1068 if (parameters->options().strip_all()
1069 && this->output_local_dynsym_count_ == 0)
1070 return;
1071
1072 gold_assert(this->symtab_shndx_ != -1U);
1073 if (this->symtab_shndx_ == 0)
1074 {
1075 // This object has no symbols. Weird but legal.
1076 return;
1077 }
1078
1079 // Read the symbol table section header.
1080 const unsigned int symtab_shndx = this->symtab_shndx_;
1081 typename This::Shdr symtabshdr(this,
1082 this->elf_file_.section_header(symtab_shndx));
1083 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1084 const unsigned int loccount = this->local_symbol_count_;
1085 gold_assert(loccount == symtabshdr.get_sh_info());
1086
1087 // Read the local symbols.
1088 const int sym_size = This::sym_size;
1089 off_t locsize = loccount * sym_size;
1090 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1091 locsize, true, false);
1092
1093 // Read the symbol names.
1094 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
1095 section_size_type strtab_size;
1096 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1097 &strtab_size,
1098 false);
1099 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1100
1101 // Get views into the output file for the portions of the symbol table
1102 // and the dynamic symbol table that we will be writing.
1103 off_t output_size = this->output_local_symbol_count_ * sym_size;
1104 unsigned char* oview = NULL;
1105 if (output_size > 0)
1106 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1107
1108 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1109 unsigned char* dyn_oview = NULL;
1110 if (dyn_output_size > 0)
1111 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1112 dyn_output_size);
1113
1114 const std::vector<Map_to_output>& mo(this->map_to_output());
1115
1116 gold_assert(this->local_values_.size() == loccount);
1117
1118 unsigned char* ov = oview;
1119 unsigned char* dyn_ov = dyn_oview;
1120 psyms += sym_size;
1121 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1122 {
1123 elfcpp::Sym<size, big_endian> isym(psyms);
1124
1125 unsigned int st_shndx = isym.get_st_shndx();
1126 if (st_shndx < elfcpp::SHN_LORESERVE)
1127 {
1128 gold_assert(st_shndx < mo.size());
1129 if (mo[st_shndx].output_section == NULL)
1130 continue;
1131 st_shndx = mo[st_shndx].output_section->out_shndx();
1132 }
1133
1134 // Write the symbol to the output symbol table.
1135 if (!parameters->options().strip_all()
1136 && this->local_values_[i].needs_output_symtab_entry())
1137 {
1138 elfcpp::Sym_write<size, big_endian> osym(ov);
1139
1140 gold_assert(isym.get_st_name() < strtab_size);
1141 const char* name = pnames + isym.get_st_name();
1142 osym.put_st_name(sympool->get_offset(name));
1143 osym.put_st_value(this->local_values_[i].value(this, 0));
1144 osym.put_st_size(isym.get_st_size());
1145 osym.put_st_info(isym.get_st_info());
1146 osym.put_st_other(isym.get_st_other());
1147 osym.put_st_shndx(st_shndx);
1148
1149 ov += sym_size;
1150 }
1151
1152 // Write the symbol to the output dynamic symbol table.
1153 if (this->local_values_[i].needs_output_dynsym_entry())
1154 {
1155 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1156 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1157
1158 gold_assert(isym.get_st_name() < strtab_size);
1159 const char* name = pnames + isym.get_st_name();
1160 osym.put_st_name(dynpool->get_offset(name));
1161 osym.put_st_value(this->local_values_[i].value(this, 0));
1162 osym.put_st_size(isym.get_st_size());
1163 osym.put_st_info(isym.get_st_info());
1164 osym.put_st_other(isym.get_st_other());
1165 osym.put_st_shndx(st_shndx);
1166
1167 dyn_ov += sym_size;
1168 }
1169 }
1170
1171
1172 if (output_size > 0)
1173 {
1174 gold_assert(ov - oview == output_size);
1175 of->write_output_view(this->local_symbol_offset_, output_size, oview);
1176 }
1177
1178 if (dyn_output_size > 0)
1179 {
1180 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1181 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1182 dyn_oview);
1183 }
1184 }
1185
1186 // Set *INFO to symbolic information about the offset OFFSET in the
1187 // section SHNDX. Return true if we found something, false if we
1188 // found nothing.
1189
1190 template<int size, bool big_endian>
1191 bool
1192 Sized_relobj<size, big_endian>::get_symbol_location_info(
1193 unsigned int shndx,
1194 off_t offset,
1195 Symbol_location_info* info)
1196 {
1197 if (this->symtab_shndx_ == 0)
1198 return false;
1199
1200 section_size_type symbols_size;
1201 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1202 &symbols_size,
1203 false);
1204
1205 unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1206 section_size_type names_size;
1207 const unsigned char* symbol_names_u =
1208 this->section_contents(symbol_names_shndx, &names_size, false);
1209 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1210
1211 const int sym_size = This::sym_size;
1212 const size_t count = symbols_size / sym_size;
1213
1214 const unsigned char* p = symbols;
1215 for (size_t i = 0; i < count; ++i, p += sym_size)
1216 {
1217 elfcpp::Sym<size, big_endian> sym(p);
1218
1219 if (sym.get_st_type() == elfcpp::STT_FILE)
1220 {
1221 if (sym.get_st_name() >= names_size)
1222 info->source_file = "(invalid)";
1223 else
1224 info->source_file = symbol_names + sym.get_st_name();
1225 }
1226 else if (sym.get_st_shndx() == shndx
1227 && static_cast<off_t>(sym.get_st_value()) <= offset
1228 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1229 > offset))
1230 {
1231 if (sym.get_st_name() > names_size)
1232 info->enclosing_symbol_name = "(invalid)";
1233 else
1234 {
1235 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1236 if (parameters->options().do_demangle())
1237 {
1238 char* demangled_name = cplus_demangle(
1239 info->enclosing_symbol_name.c_str(),
1240 DMGL_ANSI | DMGL_PARAMS);
1241 if (demangled_name != NULL)
1242 {
1243 info->enclosing_symbol_name.assign(demangled_name);
1244 free(demangled_name);
1245 }
1246 }
1247 }
1248 return true;
1249 }
1250 }
1251
1252 return false;
1253 }
1254
1255 // Input_objects methods.
1256
1257 // Add a regular relocatable object to the list. Return false if this
1258 // object should be ignored.
1259
1260 bool
1261 Input_objects::add_object(Object* obj)
1262 {
1263 // Set the global target from the first object file we recognize.
1264 Target* target = obj->target();
1265 if (!parameters->target_valid())
1266 set_parameters_target(target);
1267 else if (target != &parameters->target())
1268 {
1269 obj->error(_("incompatible target"));
1270 return false;
1271 }
1272
1273 if (!obj->is_dynamic())
1274 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1275 else
1276 {
1277 // See if this is a duplicate SONAME.
1278 Dynobj* dynobj = static_cast<Dynobj*>(obj);
1279 const char* soname = dynobj->soname();
1280
1281 std::pair<Unordered_set<std::string>::iterator, bool> ins =
1282 this->sonames_.insert(soname);
1283 if (!ins.second)
1284 {
1285 // We have already seen a dynamic object with this soname.
1286 return false;
1287 }
1288
1289 this->dynobj_list_.push_back(dynobj);
1290
1291 // If this is -lc, remember the directory in which we found it.
1292 // We use this when issuing warnings about undefined symbols: as
1293 // a heuristic, we don't warn about system libraries found in
1294 // the same directory as -lc.
1295 if (strncmp(soname, "libc.so", 7) == 0)
1296 {
1297 const char* object_name = dynobj->name().c_str();
1298 const char* base = lbasename(object_name);
1299 if (base != object_name)
1300 this->system_library_directory_.assign(object_name,
1301 base - 1 - object_name);
1302 }
1303 }
1304
1305 return true;
1306 }
1307
1308 // Return whether an object was found in the system library directory.
1309
1310 bool
1311 Input_objects::found_in_system_library_directory(const Object* object) const
1312 {
1313 return (!this->system_library_directory_.empty()
1314 && object->name().compare(0,
1315 this->system_library_directory_.size(),
1316 this->system_library_directory_) == 0);
1317 }
1318
1319 // For each dynamic object, record whether we've seen all of its
1320 // explicit dependencies.
1321
1322 void
1323 Input_objects::check_dynamic_dependencies() const
1324 {
1325 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1326 p != this->dynobj_list_.end();
1327 ++p)
1328 {
1329 const Dynobj::Needed& needed((*p)->needed());
1330 bool found_all = true;
1331 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1332 pneeded != needed.end();
1333 ++pneeded)
1334 {
1335 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1336 {
1337 found_all = false;
1338 break;
1339 }
1340 }
1341 (*p)->set_has_unknown_needed_entries(!found_all);
1342 }
1343 }
1344
1345 // Relocate_info methods.
1346
1347 // Return a string describing the location of a relocation. This is
1348 // only used in error messages.
1349
1350 template<int size, bool big_endian>
1351 std::string
1352 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1353 {
1354 // See if we can get line-number information from debugging sections.
1355 std::string filename;
1356 std::string file_and_lineno; // Better than filename-only, if available.
1357
1358 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1359 // This will be "" if we failed to parse the debug info for any reason.
1360 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1361
1362 std::string ret(this->object->name());
1363 ret += ':';
1364 Symbol_location_info info;
1365 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1366 {
1367 ret += " in function ";
1368 ret += info.enclosing_symbol_name;
1369 ret += ":";
1370 filename = info.source_file;
1371 }
1372
1373 if (!file_and_lineno.empty())
1374 ret += file_and_lineno;
1375 else
1376 {
1377 if (!filename.empty())
1378 ret += filename;
1379 ret += "(";
1380 ret += this->object->section_name(this->data_shndx);
1381 char buf[100];
1382 // Offsets into sections have to be positive.
1383 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1384 ret += buf;
1385 ret += ")";
1386 }
1387 return ret;
1388 }
1389
1390 } // End namespace gold.
1391
1392 namespace
1393 {
1394
1395 using namespace gold;
1396
1397 // Read an ELF file with the header and return the appropriate
1398 // instance of Object.
1399
1400 template<int size, bool big_endian>
1401 Object*
1402 make_elf_sized_object(const std::string& name, Input_file* input_file,
1403 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1404 {
1405 int et = ehdr.get_e_type();
1406 if (et == elfcpp::ET_REL)
1407 {
1408 Sized_relobj<size, big_endian>* obj =
1409 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1410 obj->setup(ehdr);
1411 return obj;
1412 }
1413 else if (et == elfcpp::ET_DYN)
1414 {
1415 Sized_dynobj<size, big_endian>* obj =
1416 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1417 obj->setup(ehdr);
1418 return obj;
1419 }
1420 else
1421 {
1422 gold_error(_("%s: unsupported ELF file type %d"),
1423 name.c_str(), et);
1424 return NULL;
1425 }
1426 }
1427
1428 } // End anonymous namespace.
1429
1430 namespace gold
1431 {
1432
1433 // Read an ELF file and return the appropriate instance of Object.
1434
1435 Object*
1436 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1437 const unsigned char* p, section_offset_type bytes)
1438 {
1439 if (bytes < elfcpp::EI_NIDENT)
1440 {
1441 gold_error(_("%s: ELF file too short"), name.c_str());
1442 return NULL;
1443 }
1444
1445 int v = p[elfcpp::EI_VERSION];
1446 if (v != elfcpp::EV_CURRENT)
1447 {
1448 if (v == elfcpp::EV_NONE)
1449 gold_error(_("%s: invalid ELF version 0"), name.c_str());
1450 else
1451 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1452 return NULL;
1453 }
1454
1455 int c = p[elfcpp::EI_CLASS];
1456 if (c == elfcpp::ELFCLASSNONE)
1457 {
1458 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1459 return NULL;
1460 }
1461 else if (c != elfcpp::ELFCLASS32
1462 && c != elfcpp::ELFCLASS64)
1463 {
1464 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1465 return NULL;
1466 }
1467
1468 int d = p[elfcpp::EI_DATA];
1469 if (d == elfcpp::ELFDATANONE)
1470 {
1471 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1472 return NULL;
1473 }
1474 else if (d != elfcpp::ELFDATA2LSB
1475 && d != elfcpp::ELFDATA2MSB)
1476 {
1477 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1478 return NULL;
1479 }
1480
1481 bool big_endian = d == elfcpp::ELFDATA2MSB;
1482
1483 if (c == elfcpp::ELFCLASS32)
1484 {
1485 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1486 {
1487 gold_error(_("%s: ELF file too short"), name.c_str());
1488 return NULL;
1489 }
1490 if (big_endian)
1491 {
1492 #ifdef HAVE_TARGET_32_BIG
1493 elfcpp::Ehdr<32, true> ehdr(p);
1494 return make_elf_sized_object<32, true>(name, input_file,
1495 offset, ehdr);
1496 #else
1497 gold_error(_("%s: not configured to support "
1498 "32-bit big-endian object"),
1499 name.c_str());
1500 return NULL;
1501 #endif
1502 }
1503 else
1504 {
1505 #ifdef HAVE_TARGET_32_LITTLE
1506 elfcpp::Ehdr<32, false> ehdr(p);
1507 return make_elf_sized_object<32, false>(name, input_file,
1508 offset, ehdr);
1509 #else
1510 gold_error(_("%s: not configured to support "
1511 "32-bit little-endian object"),
1512 name.c_str());
1513 return NULL;
1514 #endif
1515 }
1516 }
1517 else
1518 {
1519 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1520 {
1521 gold_error(_("%s: ELF file too short"), name.c_str());
1522 return NULL;
1523 }
1524 if (big_endian)
1525 {
1526 #ifdef HAVE_TARGET_64_BIG
1527 elfcpp::Ehdr<64, true> ehdr(p);
1528 return make_elf_sized_object<64, true>(name, input_file,
1529 offset, ehdr);
1530 #else
1531 gold_error(_("%s: not configured to support "
1532 "64-bit big-endian object"),
1533 name.c_str());
1534 return NULL;
1535 #endif
1536 }
1537 else
1538 {
1539 #ifdef HAVE_TARGET_64_LITTLE
1540 elfcpp::Ehdr<64, false> ehdr(p);
1541 return make_elf_sized_object<64, false>(name, input_file,
1542 offset, ehdr);
1543 #else
1544 gold_error(_("%s: not configured to support "
1545 "64-bit little-endian object"),
1546 name.c_str());
1547 return NULL;
1548 #endif
1549 }
1550 }
1551 }
1552
1553 // Instantiate the templates we need.
1554
1555 #ifdef HAVE_TARGET_32_LITTLE
1556 template
1557 void
1558 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1559 Read_symbols_data*);
1560 #endif
1561
1562 #ifdef HAVE_TARGET_32_BIG
1563 template
1564 void
1565 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1566 Read_symbols_data*);
1567 #endif
1568
1569 #ifdef HAVE_TARGET_64_LITTLE
1570 template
1571 void
1572 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1573 Read_symbols_data*);
1574 #endif
1575
1576 #ifdef HAVE_TARGET_64_BIG
1577 template
1578 void
1579 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1580 Read_symbols_data*);
1581 #endif
1582
1583 #ifdef HAVE_TARGET_32_LITTLE
1584 template
1585 class Sized_relobj<32, false>;
1586 #endif
1587
1588 #ifdef HAVE_TARGET_32_BIG
1589 template
1590 class Sized_relobj<32, true>;
1591 #endif
1592
1593 #ifdef HAVE_TARGET_64_LITTLE
1594 template
1595 class Sized_relobj<64, false>;
1596 #endif
1597
1598 #ifdef HAVE_TARGET_64_BIG
1599 template
1600 class Sized_relobj<64, true>;
1601 #endif
1602
1603 #ifdef HAVE_TARGET_32_LITTLE
1604 template
1605 struct Relocate_info<32, false>;
1606 #endif
1607
1608 #ifdef HAVE_TARGET_32_BIG
1609 template
1610 struct Relocate_info<32, true>;
1611 #endif
1612
1613 #ifdef HAVE_TARGET_64_LITTLE
1614 template
1615 struct Relocate_info<64, false>;
1616 #endif
1617
1618 #ifdef HAVE_TARGET_64_BIG
1619 template
1620 struct Relocate_info<64, true>;
1621 #endif
1622
1623 } // End namespace gold.