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