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