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