* reply_mig_hack.awk: Check for `auto const mach_msg_type_t'
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
3#include "gold.h"
4
5#include <cerrno>
6#include <cstring>
645f8123 7#include <cstdarg>
bae7f79e 8
14bfc3f5 9#include "target-select.h"
a2fb1b05 10#include "layout.h"
61ba1cf9 11#include "output.h"
f6ce93d6
ILT
12#include "symtab.h"
13#include "object.h"
14#include "dynobj.h"
bae7f79e
ILT
15
16namespace gold
17{
18
645f8123
ILT
19// Class Object.
20
dbe717ef
ILT
21// Set the target based on fields in the ELF file header.
22
23void
24Object::set_target(int machine, int size, bool big_endian, int osabi,
25 int abiversion)
26{
27 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
28 if (target == NULL)
29 {
30 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
31 program_name, this->name().c_str(), machine);
32 gold_exit(false);
33 }
34 this->target_ = target;
35}
36
645f8123
ILT
37// Report an error for the elfcpp::Elf_file interface.
38
39void
40Object::error(const char* format, ...)
41{
42 va_list args;
43
44 fprintf(stderr, "%s: %s: ", program_name, this->name().c_str());
45 va_start(args, format);
46 vfprintf(stderr, format, args);
47 va_end(args);
48 putc('\n', stderr);
49
50 gold_exit(false);
51}
52
53// Return a view of the contents of a section.
54
55const unsigned char*
56Object::section_contents(unsigned int shndx, off_t* plen)
57{
58 Location loc(this->do_section_contents(shndx));
59 *plen = loc.data_size;
60 return this->get_view(loc.file_offset, loc.data_size);
61}
62
dbe717ef
ILT
63// Read the section data into SD. This is code common to Sized_relobj
64// and Sized_dynobj, so we put it into Object.
65
66template<int size, bool big_endian>
67void
68Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
69 Read_symbols_data* sd)
70{
71 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
72
73 // Read the section headers.
74 const off_t shoff = elf_file->shoff();
75 const unsigned int shnum = this->shnum();
76 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size);
77
78 // Read the section names.
79 const unsigned char* pshdrs = sd->section_headers->data();
80 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
81 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
82
83 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
84 {
85 fprintf(stderr,
86 _("%s: %s: section name section has wrong type: %u\n"),
87 program_name, this->name().c_str(),
88 static_cast<unsigned int>(shdrnames.get_sh_type()));
89 gold_exit(false);
90 }
91
92 sd->section_names_size = shdrnames.get_sh_size();
93 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
94 sd->section_names_size);
95}
96
97// If NAME is the name of a special .gnu.warning section, arrange for
98// the warning to be issued. SHNDX is the section index. Return
99// whether it is a warning section.
100
101bool
102Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
103 Symbol_table* symtab)
104{
105 const char warn_prefix[] = ".gnu.warning.";
106 const int warn_prefix_len = sizeof warn_prefix - 1;
107 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
108 {
109 symtab->add_warning(name + warn_prefix_len, this, shndx);
110 return true;
111 }
112 return false;
113}
114
f6ce93d6 115// Class Sized_relobj.
bae7f79e
ILT
116
117template<int size, bool big_endian>
f6ce93d6 118Sized_relobj<size, big_endian>::Sized_relobj(
bae7f79e
ILT
119 const std::string& name,
120 Input_file* input_file,
121 off_t offset,
122 const elfcpp::Ehdr<size, big_endian>& ehdr)
f6ce93d6 123 : Relobj(name, input_file, offset),
645f8123 124 elf_file_(this, ehdr),
dbe717ef 125 symtab_shndx_(-1U),
61ba1cf9
ILT
126 local_symbol_count_(0),
127 output_local_symbol_count_(0),
75f65a3e 128 symbols_(NULL),
61ba1cf9 129 local_symbol_offset_(0),
c06b7b0b
ILT
130 local_values_(),
131 local_indexes_()
bae7f79e 132{
bae7f79e
ILT
133}
134
135template<int size, bool big_endian>
f6ce93d6 136Sized_relobj<size, big_endian>::~Sized_relobj()
bae7f79e
ILT
137{
138}
139
645f8123 140// Set up an object file based on the file header. This sets up the
bae7f79e
ILT
141// target and reads the section information.
142
143template<int size, bool big_endian>
144void
f6ce93d6 145Sized_relobj<size, big_endian>::setup(
bae7f79e
ILT
146 const elfcpp::Ehdr<size, big_endian>& ehdr)
147{
dbe717ef
ILT
148 this->set_target(ehdr.get_e_machine(), size, big_endian,
149 ehdr.get_e_ident()[elfcpp::EI_OSABI],
150 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
12e14209 151
dbe717ef 152 const unsigned int shnum = this->elf_file_.shnum();
a2fb1b05 153 this->set_shnum(shnum);
dbe717ef 154}
12e14209 155
dbe717ef
ILT
156// Find the SHT_SYMTAB section, given the section headers. The ELF
157// standard says that maybe in the future there can be more than one
158// SHT_SYMTAB section. Until somebody figures out how that could
159// work, we assume there is only one.
12e14209 160
dbe717ef
ILT
161template<int size, bool big_endian>
162void
163Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
164{
165 const unsigned int shnum = this->shnum();
166 this->symtab_shndx_ = 0;
167 if (shnum > 0)
bae7f79e 168 {
dbe717ef
ILT
169 // Look through the sections in reverse order, since gas tends
170 // to put the symbol table at the end.
171 const unsigned char* p = pshdrs + shnum * This::shdr_size;
172 unsigned int i = shnum;
173 while (i > 0)
bae7f79e 174 {
dbe717ef
ILT
175 --i;
176 p -= This::shdr_size;
177 typename This::Shdr shdr(p);
178 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
179 {
180 this->symtab_shndx_ = i;
181 break;
182 }
bae7f79e 183 }
bae7f79e
ILT
184 }
185}
186
12e14209 187// Read the sections and symbols from an object file.
bae7f79e
ILT
188
189template<int size, bool big_endian>
12e14209 190void
f6ce93d6 191Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
bae7f79e 192{
dbe717ef 193 this->read_section_data(&this->elf_file_, sd);
12e14209 194
dbe717ef
ILT
195 const unsigned char* const pshdrs = sd->section_headers->data();
196
197 this->find_symtab(pshdrs);
12e14209 198
645f8123 199 if (this->symtab_shndx_ == 0)
bae7f79e
ILT
200 {
201 // No symbol table. Weird but legal.
12e14209
ILT
202 sd->symbols = NULL;
203 sd->symbols_size = 0;
204 sd->symbol_names = NULL;
205 sd->symbol_names_size = 0;
206 return;
bae7f79e
ILT
207 }
208
12e14209
ILT
209 // Get the symbol table section header.
210 typename This::Shdr symtabshdr(pshdrs
645f8123 211 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 212 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
bae7f79e 213
75f65a3e
ILT
214 // We only need the external symbols.
215 const int sym_size = This::sym_size;
92e059d8
ILT
216 const unsigned int loccount = symtabshdr.get_sh_info();
217 this->local_symbol_count_ = loccount;
218 off_t locsize = loccount * sym_size;
75f65a3e
ILT
219 off_t extoff = symtabshdr.get_sh_offset() + locsize;
220 off_t extsize = symtabshdr.get_sh_size() - locsize;
221
bae7f79e 222 // Read the symbol table.
75f65a3e 223 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
bae7f79e
ILT
224
225 // Read the section header for the symbol names.
dbe717ef
ILT
226 unsigned int strtab_shndx = symtabshdr.get_sh_link();
227 if (strtab_shndx >= this->shnum())
bae7f79e
ILT
228 {
229 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
dbe717ef 230 program_name, this->name().c_str(), strtab_shndx);
bae7f79e
ILT
231 gold_exit(false);
232 }
dbe717ef 233 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
bae7f79e
ILT
234 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
235 {
236 fprintf(stderr,
237 _("%s: %s: symbol table name section has wrong type: %u\n"),
238 program_name, this->name().c_str(),
239 static_cast<unsigned int>(strtabshdr.get_sh_type()));
240 gold_exit(false);
241 }
242
243 // Read the symbol names.
244 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
245 strtabshdr.get_sh_size());
246
12e14209
ILT
247 sd->symbols = fvsymtab;
248 sd->symbols_size = extsize;
249 sd->symbol_names = fvstrtab;
250 sd->symbol_names_size = strtabshdr.get_sh_size();
a2fb1b05
ILT
251}
252
253// Return whether to include a section group in the link. LAYOUT is
254// used to keep track of which section groups we have already seen.
255// INDEX is the index of the section group and SHDR is the section
256// header. If we do not want to include this group, we set bits in
257// OMIT for each section which should be discarded.
258
259template<int size, bool big_endian>
260bool
f6ce93d6 261Sized_relobj<size, big_endian>::include_section_group(
a2fb1b05
ILT
262 Layout* layout,
263 unsigned int index,
264 const elfcpp::Shdr<size, big_endian>& shdr,
265 std::vector<bool>* omit)
266{
267 // Read the section contents.
268 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
269 shdr.get_sh_size());
270 const elfcpp::Elf_Word* pword =
271 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
272
273 // The first word contains flags. We only care about COMDAT section
274 // groups. Other section groups are always included in the link
275 // just like ordinary sections.
f6ce93d6 276 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
a2fb1b05
ILT
277 if ((flags & elfcpp::GRP_COMDAT) == 0)
278 return true;
279
280 // Look up the group signature, which is the name of a symbol. This
281 // is a lot of effort to go to to read a string. Why didn't they
282 // just use the name of the SHT_GROUP section as the group
283 // signature?
284
285 // Get the appropriate symbol table header (this will normally be
286 // the single SHT_SYMTAB section, but in principle it need not be).
645f8123
ILT
287 const unsigned int link = shdr.get_sh_link();
288 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
a2fb1b05
ILT
289
290 // Read the symbol table entry.
291 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
292 {
293 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
294 program_name, this->name().c_str(), index, shdr.get_sh_info());
295 gold_exit(false);
296 }
297 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
298 const unsigned char* psym = this->get_view(symoff, This::sym_size);
299 elfcpp::Sym<size, big_endian> sym(psym);
300
a2fb1b05 301 // Read the symbol table names.
645f8123
ILT
302 off_t symnamelen;
303 const unsigned char* psymnamesu;
304 psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen);
a2fb1b05
ILT
305 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
306
307 // Get the section group signature.
645f8123 308 if (sym.get_st_name() >= symnamelen)
a2fb1b05
ILT
309 {
310 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
311 program_name, this->name().c_str(), shdr.get_sh_info(),
312 sym.get_st_name());
313 gold_exit(false);
314 }
315
316 const char* signature = psymnames + sym.get_st_name();
317
ead1e424
ILT
318 // It seems that some versions of gas will create a section group
319 // associated with a section symbol, and then fail to give a name to
320 // the section symbol. In such a case, use the name of the section.
321 // FIXME.
645f8123
ILT
322 std::string secname;
323 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
ead1e424 324 {
645f8123
ILT
325 secname = this->section_name(sym.get_st_shndx());
326 signature = secname.c_str();
ead1e424
ILT
327 }
328
a2fb1b05
ILT
329 // Record this section group, and see whether we've already seen one
330 // with the same signature.
331 if (layout->add_comdat(signature, true))
332 return true;
333
334 // This is a duplicate. We want to discard the sections in this
335 // group.
336 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
337 for (size_t i = 1; i < count; ++i)
338 {
f6ce93d6
ILT
339 elfcpp::Elf_Word secnum =
340 elfcpp::Swap<32, big_endian>::readval(pword + i);
a2fb1b05
ILT
341 if (secnum >= this->shnum())
342 {
343 fprintf(stderr,
344 _("%s: %s: section %u in section group %u out of range"),
345 program_name, this->name().c_str(), secnum,
346 index);
347 gold_exit(false);
348 }
349 (*omit)[secnum] = true;
350 }
351
352 return false;
353}
354
355// Whether to include a linkonce section in the link. NAME is the
356// name of the section and SHDR is the section header.
357
358// Linkonce sections are a GNU extension implemented in the original
359// GNU linker before section groups were defined. The semantics are
360// that we only include one linkonce section with a given name. The
361// name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
362// where T is the type of section and SYMNAME is the name of a symbol.
363// In an attempt to make linkonce sections interact well with section
364// groups, we try to identify SYMNAME and use it like a section group
365// signature. We want to block section groups with that signature,
366// but not other linkonce sections with that signature. We also use
367// the full name of the linkonce section as a normal section group
368// signature.
369
370template<int size, bool big_endian>
371bool
f6ce93d6 372Sized_relobj<size, big_endian>::include_linkonce_section(
a2fb1b05
ILT
373 Layout* layout,
374 const char* name,
375 const elfcpp::Shdr<size, big_endian>&)
376{
377 const char* symname = strrchr(name, '.') + 1;
a783673b
ILT
378 bool include1 = layout->add_comdat(symname, false);
379 bool include2 = layout->add_comdat(name, true);
380 return include1 && include2;
a2fb1b05
ILT
381}
382
383// Lay out the input sections. We walk through the sections and check
384// whether they should be included in the link. If they should, we
385// pass them to the Layout object, which will return an output section
386// and an offset.
387
388template<int size, bool big_endian>
389void
f6ce93d6
ILT
390Sized_relobj<size, big_endian>::do_layout(const General_options& options,
391 Symbol_table* symtab,
392 Layout* layout,
12e14209 393 Read_symbols_data* sd)
a2fb1b05 394{
dbe717ef 395 const unsigned int shnum = this->shnum();
12e14209
ILT
396 if (shnum == 0)
397 return;
a2fb1b05
ILT
398
399 // Get the section headers.
12e14209 400 const unsigned char* pshdrs = sd->section_headers->data();
a2fb1b05
ILT
401
402 // Get the section names.
12e14209 403 const unsigned char* pnamesu = sd->section_names->data();
a2fb1b05
ILT
404 const char* pnames = reinterpret_cast<const char*>(pnamesu);
405
406 std::vector<Map_to_output>& map_sections(this->map_to_output());
61ba1cf9 407 map_sections.resize(shnum);
a2fb1b05
ILT
408
409 // Keep track of which sections to omit.
410 std::vector<bool> omit(shnum, false);
411
f6ce93d6
ILT
412 // Skip the first, dummy, section.
413 pshdrs += This::shdr_size;
414 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
a2fb1b05 415 {
75f65a3e 416 typename This::Shdr shdr(pshdrs);
a2fb1b05 417
12e14209 418 if (shdr.get_sh_name() >= sd->section_names_size)
a2fb1b05
ILT
419 {
420 fprintf(stderr,
421 _("%s: %s: bad section name offset for section %u: %lu\n"),
422 program_name, this->name().c_str(), i,
423 static_cast<unsigned long>(shdr.get_sh_name()));
424 gold_exit(false);
425 }
426
427 const char* name = pnames + shdr.get_sh_name();
428
dbe717ef 429 if (this->handle_gnu_warning_section(name, i, symtab))
f6ce93d6 430 {
f6ce93d6
ILT
431 if (!options.is_relocatable())
432 omit[i] = true;
433 }
434
a2fb1b05
ILT
435 bool discard = omit[i];
436 if (!discard)
437 {
438 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
439 {
440 if (!this->include_section_group(layout, i, shdr, &omit))
441 discard = true;
442 }
443 else if (Layout::is_linkonce(name))
444 {
445 if (!this->include_linkonce_section(layout, name, shdr))
446 discard = true;
447 }
448 }
449
450 if (discard)
451 {
452 // Do not include this section in the link.
453 map_sections[i].output_section = NULL;
454 continue;
455 }
456
457 off_t offset;
ead1e424 458 Output_section* os = layout->layout(this, i, name, shdr, &offset);
a2fb1b05
ILT
459
460 map_sections[i].output_section = os;
461 map_sections[i].offset = offset;
12e14209
ILT
462 }
463
464 delete sd->section_headers;
465 sd->section_headers = NULL;
466 delete sd->section_names;
467 sd->section_names = NULL;
468}
469
470// Add the symbols to the symbol table.
471
472template<int size, bool big_endian>
473void
f6ce93d6 474Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
12e14209
ILT
475 Read_symbols_data* sd)
476{
477 if (sd->symbols == NULL)
478 {
a3ad94ed 479 gold_assert(sd->symbol_names == NULL);
12e14209
ILT
480 return;
481 }
a2fb1b05 482
12e14209
ILT
483 const int sym_size = This::sym_size;
484 size_t symcount = sd->symbols_size / sym_size;
485 if (symcount * sym_size != sd->symbols_size)
486 {
487 fprintf(stderr,
488 _("%s: %s: size of symbols is not multiple of symbol size\n"),
489 program_name, this->name().c_str());
490 gold_exit(false);
a2fb1b05 491 }
12e14209
ILT
492
493 this->symbols_ = new Symbol*[symcount];
494
12e14209
ILT
495 const char* sym_names =
496 reinterpret_cast<const char*>(sd->symbol_names->data());
dbe717ef
ILT
497 symtab->add_from_relobj(this, sd->symbols->data(), symcount, sym_names,
498 sd->symbol_names_size, this->symbols_);
12e14209
ILT
499
500 delete sd->symbols;
501 sd->symbols = NULL;
502 delete sd->symbol_names;
503 sd->symbol_names = NULL;
bae7f79e
ILT
504}
505
75f65a3e 506// Finalize the local symbols. Here we record the file offset at
61ba1cf9 507// which they should be output, we add their names to *POOL, and we
c06b7b0b
ILT
508// add their values to THIS->LOCAL_VALUES_ and their indexes in the
509// output symbol table to THIS->LOCAL_INDEXES_. Return the symbol
510// index. This function is always called from the main thread. The
511// actual output of the local symbols will occur in a separate task.
75f65a3e
ILT
512
513template<int size, bool big_endian>
c06b7b0b
ILT
514unsigned int
515Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
516 off_t off,
75f65a3e
ILT
517 Stringpool* pool)
518{
a3ad94ed 519 gold_assert(this->symtab_shndx_ != -1U);
645f8123 520 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
521 {
522 // This object has no symbols. Weird but legal.
c06b7b0b 523 return index;
61ba1cf9
ILT
524 }
525
a3ad94ed 526 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
61ba1cf9 527
75f65a3e
ILT
528 this->local_symbol_offset_ = off;
529
530 // Read the symbol table section header.
645f8123
ILT
531 const unsigned int symtab_shndx = this->symtab_shndx_;
532 typename This::Shdr symtabshdr(this,
533 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 534 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
75f65a3e
ILT
535
536 // Read the local symbols.
75f65a3e 537 const int sym_size = This::sym_size;
92e059d8 538 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 539 gold_assert(loccount == symtabshdr.get_sh_info());
75f65a3e
ILT
540 off_t locsize = loccount * sym_size;
541 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
542 locsize);
543
c06b7b0b
ILT
544 this->local_values_.resize(loccount);
545 this->local_indexes_.resize(loccount);
61ba1cf9 546
75f65a3e 547 // Read the symbol names.
645f8123
ILT
548 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
549 off_t strtab_size;
550 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
551 &strtab_size);
75f65a3e
ILT
552 const char* pnames = reinterpret_cast<const char*>(pnamesu);
553
554 // Loop over the local symbols.
555
c06b7b0b 556 const std::vector<Map_to_output>& mo(this->map_to_output());
75f65a3e 557 unsigned int shnum = this->shnum();
61ba1cf9 558 unsigned int count = 0;
75f65a3e
ILT
559 // Skip the first, dummy, symbol.
560 psyms += sym_size;
61ba1cf9 561 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
75f65a3e
ILT
562 {
563 elfcpp::Sym<size, big_endian> sym(psyms);
564
565 unsigned int shndx = sym.get_st_shndx();
566
567 if (shndx >= elfcpp::SHN_LORESERVE)
568 {
61ba1cf9 569 if (shndx == elfcpp::SHN_ABS)
c06b7b0b 570 this->local_values_[i] = sym.get_st_value();
61ba1cf9 571 else
75f65a3e 572 {
61ba1cf9 573 // FIXME: Handle SHN_XINDEX.
75f65a3e
ILT
574 fprintf(stderr,
575 _("%s: %s: unknown section index %u "
576 "for local symbol %u\n"),
577 program_name, this->name().c_str(), shndx, i);
578 gold_exit(false);
579 }
75f65a3e
ILT
580 }
581 else
582 {
583 if (shndx >= shnum)
584 {
585 fprintf(stderr,
586 _("%s: %s: local symbol %u section index %u "
587 "out of range\n"),
588 program_name, this->name().c_str(), i, shndx);
589 gold_exit(false);
590 }
591
592 if (mo[shndx].output_section == NULL)
61ba1cf9 593 {
c06b7b0b
ILT
594 this->local_values_[i] = 0;
595 this->local_indexes_[i] = -1U;
61ba1cf9
ILT
596 continue;
597 }
598
c06b7b0b
ILT
599 this->local_values_[i] = (mo[shndx].output_section->address()
600 + mo[shndx].offset
601 + sym.get_st_value());
75f65a3e
ILT
602 }
603
c06b7b0b
ILT
604 // Decide whether this symbol should go into the output file.
605
606 if (sym.get_st_type() == elfcpp::STT_SECTION)
f6ce93d6 607 {
c06b7b0b
ILT
608 this->local_indexes_[i] = -1U;
609 continue;
610 }
645f8123 611
c06b7b0b
ILT
612 if (sym.get_st_name() >= strtab_size)
613 {
614 fprintf(stderr,
615 _("%s: %s: local symbol %u section name "
616 "out of range: %u >= %u\n"),
617 program_name, this->name().c_str(),
618 i, sym.get_st_name(),
619 static_cast<unsigned int>(strtab_size));
620 gold_exit(false);
f6ce93d6 621 }
c06b7b0b
ILT
622
623 const char* name = pnames + sym.get_st_name();
624 pool->add(name, NULL);
625 this->local_indexes_[i] = index;
626 ++index;
627 off += sym_size;
628 ++count;
75f65a3e
ILT
629 }
630
61ba1cf9
ILT
631 this->output_local_symbol_count_ = count;
632
c06b7b0b 633 return index;
75f65a3e
ILT
634}
635
61ba1cf9
ILT
636// Write out the local symbols.
637
638template<int size, bool big_endian>
639void
f6ce93d6 640Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
61ba1cf9
ILT
641 const Stringpool* sympool)
642{
a3ad94ed 643 gold_assert(this->symtab_shndx_ != -1U);
645f8123 644 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
645 {
646 // This object has no symbols. Weird but legal.
647 return;
648 }
649
650 // Read the symbol table section header.
645f8123
ILT
651 const unsigned int symtab_shndx = this->symtab_shndx_;
652 typename This::Shdr symtabshdr(this,
653 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 654 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8 655 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 656 gold_assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
657
658 // Read the local symbols.
659 const int sym_size = This::sym_size;
92e059d8 660 off_t locsize = loccount * sym_size;
61ba1cf9
ILT
661 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
662 locsize);
663
61ba1cf9 664 // Read the symbol names.
645f8123
ILT
665 const unsigned int strtab_shndx = symtabshdr.get_sh_link();
666 off_t strtab_size;
667 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
668 &strtab_size);
61ba1cf9
ILT
669 const char* pnames = reinterpret_cast<const char*>(pnamesu);
670
671 // Get a view into the output file.
672 off_t output_size = this->output_local_symbol_count_ * sym_size;
673 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
674 output_size);
675
c06b7b0b
ILT
676 const std::vector<Map_to_output>& mo(this->map_to_output());
677
a3ad94ed
ILT
678 gold_assert(this->local_values_.size() == loccount);
679 gold_assert(this->local_indexes_.size() == loccount);
61ba1cf9 680
61ba1cf9 681 unsigned char* ov = oview;
c06b7b0b 682 psyms += sym_size;
92e059d8 683 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
684 {
685 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6 686
c06b7b0b 687 if (this->local_indexes_[i] == -1U)
f6ce93d6 688 continue;
a3ad94ed 689 gold_assert(this->local_indexes_[i] != 0);
61ba1cf9
ILT
690
691 unsigned int st_shndx = isym.get_st_shndx();
692 if (st_shndx < elfcpp::SHN_LORESERVE)
693 {
a3ad94ed 694 gold_assert(st_shndx < mo.size());
61ba1cf9
ILT
695 if (mo[st_shndx].output_section == NULL)
696 continue;
ead1e424 697 st_shndx = mo[st_shndx].output_section->out_shndx();
61ba1cf9
ILT
698 }
699
f6ce93d6
ILT
700 elfcpp::Sym_write<size, big_endian> osym(ov);
701
a3ad94ed 702 gold_assert(isym.get_st_name() < strtab_size);
c06b7b0b
ILT
703 const char* name = pnames + isym.get_st_name();
704 osym.put_st_name(sympool->get_offset(name));
705 osym.put_st_value(this->local_values_[i]);
61ba1cf9
ILT
706 osym.put_st_size(isym.get_st_size());
707 osym.put_st_info(isym.get_st_info());
708 osym.put_st_other(isym.get_st_other());
709 osym.put_st_shndx(st_shndx);
710
711 ov += sym_size;
712 }
713
a3ad94ed 714 gold_assert(ov - oview == output_size);
61ba1cf9
ILT
715
716 of->write_output_view(this->local_symbol_offset_, output_size, oview);
717}
718
54dc6425
ILT
719// Input_objects methods.
720
008db82e
ILT
721// Add a regular relocatable object to the list. Return false if this
722// object should be ignored.
f6ce93d6 723
008db82e 724bool
54dc6425
ILT
725Input_objects::add_object(Object* obj)
726{
008db82e 727 if (!obj->is_dynamic())
f6ce93d6 728 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
008db82e
ILT
729 else
730 {
731 // See if this is a duplicate SONAME.
732 Dynobj* dynobj = static_cast<Dynobj*>(obj);
733
734 std::pair<Unordered_set<std::string>::iterator, bool> ins =
735 this->sonames_.insert(dynobj->soname());
736 if (!ins.second)
737 {
738 // We have already seen a dynamic object with this soname.
739 return false;
740 }
741
742 this->dynobj_list_.push_back(dynobj);
743 }
75f65a3e
ILT
744
745 Target* target = obj->target();
746 if (this->target_ == NULL)
747 this->target_ = target;
748 else if (this->target_ != target)
749 {
750 fprintf(stderr, "%s: %s: incompatible target\n",
751 program_name, obj->name().c_str());
752 gold_exit(false);
753 }
008db82e
ILT
754
755 return true;
54dc6425
ILT
756}
757
92e059d8
ILT
758// Relocate_info methods.
759
760// Return a string describing the location of a relocation. This is
761// only used in error messages.
762
763template<int size, bool big_endian>
764std::string
765Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
766{
767 std::string ret(this->object->name());
768 ret += ": reloc ";
769 char buf[100];
770 snprintf(buf, sizeof buf, "%zu", relnum);
771 ret += buf;
772 ret += " in reloc section ";
773 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
774 ret += buf;
775 ret += " (" + this->object->section_name(this->reloc_shndx);
776 ret += ") for section ";
777 snprintf(buf, sizeof buf, "%u", this->data_shndx);
778 ret += buf;
779 ret += " (" + this->object->section_name(this->data_shndx) + ")";
780 return ret;
781}
782
bae7f79e
ILT
783} // End namespace gold.
784
785namespace
786{
787
788using namespace gold;
789
790// Read an ELF file with the header and return the appropriate
791// instance of Object.
792
793template<int size, bool big_endian>
794Object*
795make_elf_sized_object(const std::string& name, Input_file* input_file,
796 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
797{
798 int et = ehdr.get_e_type();
bae7f79e
ILT
799 if (et == elfcpp::ET_REL)
800 {
f6ce93d6
ILT
801 Sized_relobj<size, big_endian>* obj =
802 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
803 obj->setup(ehdr);
804 return obj;
805 }
dbe717ef
ILT
806 else if (et == elfcpp::ET_DYN)
807 {
808 Sized_dynobj<size, big_endian>* obj =
809 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
810 obj->setup(ehdr);
811 return obj;
812 }
bae7f79e
ILT
813 else
814 {
dbe717ef
ILT
815 fprintf(stderr, _("%s: %s: unsupported ELF file type %d\n"),
816 program_name, name.c_str(), et);
bae7f79e 817 gold_exit(false);
bae7f79e
ILT
818 }
819}
820
821} // End anonymous namespace.
822
823namespace gold
824{
825
826// Read an ELF file and return the appropriate instance of Object.
827
828Object*
829make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
830 const unsigned char* p, off_t bytes)
831{
832 if (bytes < elfcpp::EI_NIDENT)
833 {
834 fprintf(stderr, _("%s: %s: ELF file too short\n"),
835 program_name, name.c_str());
836 gold_exit(false);
837 }
838
839 int v = p[elfcpp::EI_VERSION];
840 if (v != elfcpp::EV_CURRENT)
841 {
842 if (v == elfcpp::EV_NONE)
843 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
844 program_name, name.c_str());
845 else
846 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
847 program_name, name.c_str(), v);
848 gold_exit(false);
849 }
850
851 int c = p[elfcpp::EI_CLASS];
852 if (c == elfcpp::ELFCLASSNONE)
853 {
854 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
855 program_name, name.c_str());
856 gold_exit(false);
857 }
858 else if (c != elfcpp::ELFCLASS32
859 && c != elfcpp::ELFCLASS64)
860 {
861 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
862 program_name, name.c_str(), c);
863 gold_exit(false);
864 }
865
866 int d = p[elfcpp::EI_DATA];
867 if (d == elfcpp::ELFDATANONE)
868 {
869 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
870 program_name, name.c_str());
871 gold_exit(false);
872 }
873 else if (d != elfcpp::ELFDATA2LSB
874 && d != elfcpp::ELFDATA2MSB)
875 {
876 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
877 program_name, name.c_str(), d);
878 gold_exit(false);
879 }
880
881 bool big_endian = d == elfcpp::ELFDATA2MSB;
882
883 if (c == elfcpp::ELFCLASS32)
884 {
885 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
886 {
887 fprintf(stderr, _("%s: %s: ELF file too short\n"),
888 program_name, name.c_str());
889 gold_exit(false);
890 }
891 if (big_endian)
892 {
893 elfcpp::Ehdr<32, true> ehdr(p);
894 return make_elf_sized_object<32, true>(name, input_file,
895 offset, ehdr);
896 }
897 else
898 {
899 elfcpp::Ehdr<32, false> ehdr(p);
900 return make_elf_sized_object<32, false>(name, input_file,
901 offset, ehdr);
902 }
903 }
904 else
905 {
906 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
907 {
908 fprintf(stderr, _("%s: %s: ELF file too short\n"),
909 program_name, name.c_str());
910 gold_exit(false);
911 }
912 if (big_endian)
913 {
914 elfcpp::Ehdr<64, true> ehdr(p);
915 return make_elf_sized_object<64, true>(name, input_file,
916 offset, ehdr);
917 }
918 else
919 {
920 elfcpp::Ehdr<64, false> ehdr(p);
921 return make_elf_sized_object<64, false>(name, input_file,
922 offset, ehdr);
923 }
924 }
925}
926
927// Instantiate the templates we need. We could use the configure
928// script to restrict this to only the ones for implemented targets.
929
930template
f6ce93d6 931class Sized_relobj<32, false>;
bae7f79e
ILT
932
933template
f6ce93d6 934class Sized_relobj<32, true>;
bae7f79e
ILT
935
936template
f6ce93d6 937class Sized_relobj<64, false>;
bae7f79e
ILT
938
939template
f6ce93d6 940class Sized_relobj<64, true>;
bae7f79e 941
92e059d8
ILT
942template
943struct Relocate_info<32, false>;
944
945template
946struct Relocate_info<32, true>;
947
948template
949struct Relocate_info<64, false>;
950
951template
952struct Relocate_info<64, true>;
953
bae7f79e 954} // End namespace gold.
This page took 0.094213 seconds and 4 git commands to generate.