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