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