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