* corefile.c (generic_search): Delete disabled code.
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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"
92de84a6 36#include "cref.h"
4c50553d 37#include "reloc.h"
f6ce93d6
ILT
38#include "object.h"
39#include "dynobj.h"
5995b570 40#include "plugin.h"
bae7f79e
ILT
41
42namespace gold
43{
44
d491d34e
ILT
45// Class Xindex.
46
47// Initialize the symtab_xindex_ array. Find the SHT_SYMTAB_SHNDX
48// section and read it in. SYMTAB_SHNDX is the index of the symbol
49// table we care about.
50
51template<int size, bool big_endian>
52void
53Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
54{
55 if (!this->symtab_xindex_.empty())
56 return;
57
58 gold_assert(symtab_shndx != 0);
59
60 // Look through the sections in reverse order, on the theory that it
61 // is more likely to be near the end than the beginning.
62 unsigned int i = object->shnum();
63 while (i > 0)
64 {
65 --i;
66 if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
67 && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
68 {
69 this->read_symtab_xindex<size, big_endian>(object, i, NULL);
70 return;
71 }
72 }
73
74 object->error(_("missing SHT_SYMTAB_SHNDX section"));
75}
76
77// Read in the symtab_xindex_ array, given the section index of the
78// SHT_SYMTAB_SHNDX section. If PSHDRS is not NULL, it points at the
79// section headers.
80
81template<int size, bool big_endian>
82void
83Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
84 const unsigned char* pshdrs)
85{
86 section_size_type bytecount;
87 const unsigned char* contents;
88 if (pshdrs == NULL)
89 contents = object->section_contents(xindex_shndx, &bytecount, false);
90 else
91 {
92 const unsigned char* p = (pshdrs
93 + (xindex_shndx
94 * elfcpp::Elf_sizes<size>::shdr_size));
95 typename elfcpp::Shdr<size, big_endian> shdr(p);
96 bytecount = convert_to_section_size_type(shdr.get_sh_size());
97 contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
98 }
99
100 gold_assert(this->symtab_xindex_.empty());
101 this->symtab_xindex_.reserve(bytecount / 4);
102 for (section_size_type i = 0; i < bytecount; i += 4)
103 {
104 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
105 // We preadjust the section indexes we save.
106 this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
107 }
108}
109
110// Symbol symndx has a section of SHN_XINDEX; return the real section
111// index.
112
113unsigned int
114Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
115{
116 if (symndx >= this->symtab_xindex_.size())
117 {
118 object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
119 symndx);
120 return elfcpp::SHN_UNDEF;
121 }
122 unsigned int shndx = this->symtab_xindex_[symndx];
123 if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
124 {
125 object->error(_("extended index for symbol %u out of range: %u"),
126 symndx, shndx);
127 return elfcpp::SHN_UNDEF;
128 }
129 return shndx;
130}
131
645f8123
ILT
132// Class Object.
133
dbe717ef
ILT
134// Set the target based on fields in the ELF file header.
135
136void
137Object::set_target(int machine, int size, bool big_endian, int osabi,
138 int abiversion)
139{
140 Target* target = select_target(machine, size, big_endian, osabi, abiversion);
141 if (target == NULL)
75f2446e
ILT
142 gold_fatal(_("%s: unsupported ELF machine number %d"),
143 this->name().c_str(), machine);
dbe717ef
ILT
144 this->target_ = target;
145}
146
75f2446e
ILT
147// Report an error for this object file. This is used by the
148// elfcpp::Elf_file interface, and also called by the Object code
149// itself.
645f8123
ILT
150
151void
75f2446e 152Object::error(const char* format, ...) const
645f8123
ILT
153{
154 va_list args;
645f8123 155 va_start(args, format);
75f2446e
ILT
156 char* buf = NULL;
157 if (vasprintf(&buf, format, args) < 0)
158 gold_nomem();
645f8123 159 va_end(args);
75f2446e
ILT
160 gold_error(_("%s: %s"), this->name().c_str(), buf);
161 free(buf);
645f8123
ILT
162}
163
164// Return a view of the contents of a section.
165
166const unsigned char*
8383303e
ILT
167Object::section_contents(unsigned int shndx, section_size_type* plen,
168 bool cache)
645f8123
ILT
169{
170 Location loc(this->do_section_contents(shndx));
8383303e 171 *plen = convert_to_section_size_type(loc.data_size);
39d0cb0e 172 return this->get_view(loc.file_offset, *plen, true, cache);
645f8123
ILT
173}
174
dbe717ef
ILT
175// Read the section data into SD. This is code common to Sized_relobj
176// and Sized_dynobj, so we put it into Object.
177
178template<int size, bool big_endian>
179void
180Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
181 Read_symbols_data* sd)
182{
183 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
184
185 // Read the section headers.
186 const off_t shoff = elf_file->shoff();
187 const unsigned int shnum = this->shnum();
39d0cb0e
ILT
188 sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
189 true, true);
dbe717ef
ILT
190
191 // Read the section names.
192 const unsigned char* pshdrs = sd->section_headers->data();
193 const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
194 typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
195
196 if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
75f2446e
ILT
197 this->error(_("section name section has wrong type: %u"),
198 static_cast<unsigned int>(shdrnames.get_sh_type()));
dbe717ef 199
8383303e
ILT
200 sd->section_names_size =
201 convert_to_section_size_type(shdrnames.get_sh_size());
dbe717ef 202 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
39d0cb0e
ILT
203 sd->section_names_size, false,
204 false);
dbe717ef
ILT
205}
206
207// If NAME is the name of a special .gnu.warning section, arrange for
208// the warning to be issued. SHNDX is the section index. Return
209// whether it is a warning section.
210
211bool
212Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
213 Symbol_table* symtab)
214{
215 const char warn_prefix[] = ".gnu.warning.";
216 const int warn_prefix_len = sizeof warn_prefix - 1;
217 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
218 {
cb295612
ILT
219 // Read the section contents to get the warning text. It would
220 // be nicer if we only did this if we have to actually issue a
221 // warning. Unfortunately, warnings are issued as we relocate
222 // sections. That means that we can not lock the object then,
223 // as we might try to issue the same warning multiple times
224 // simultaneously.
225 section_size_type len;
226 const unsigned char* contents = this->section_contents(shndx, &len,
227 false);
228 std::string warning(reinterpret_cast<const char*>(contents), len);
229 symtab->add_warning(name + warn_prefix_len, this, warning);
dbe717ef
ILT
230 return true;
231 }
232 return false;
233}
234
f6ce93d6 235// Class Sized_relobj.
bae7f79e
ILT
236
237template<int size, bool big_endian>
f6ce93d6 238Sized_relobj<size, big_endian>::Sized_relobj(
bae7f79e
ILT
239 const std::string& name,
240 Input_file* input_file,
241 off_t offset,
242 const elfcpp::Ehdr<size, big_endian>& ehdr)
f6ce93d6 243 : Relobj(name, input_file, offset),
645f8123 244 elf_file_(this, ehdr),
dbe717ef 245 symtab_shndx_(-1U),
61ba1cf9
ILT
246 local_symbol_count_(0),
247 output_local_symbol_count_(0),
7bf1f802 248 output_local_dynsym_count_(0),
730cdc88 249 symbols_(),
92de84a6 250 defined_count_(0),
61ba1cf9 251 local_symbol_offset_(0),
7bf1f802 252 local_dynsym_offset_(0),
e727fa71 253 local_values_(),
730cdc88 254 local_got_offsets_(),
ef9beddf
ILT
255 kept_comdat_sections_(),
256 comdat_groups_(),
730cdc88 257 has_eh_frame_(false)
bae7f79e 258{
bae7f79e
ILT
259}
260
261template<int size, bool big_endian>
f6ce93d6 262Sized_relobj<size, big_endian>::~Sized_relobj()
bae7f79e
ILT
263{
264}
265
645f8123 266// Set up an object file based on the file header. This sets up the
bae7f79e
ILT
267// target and reads the section information.
268
269template<int size, bool big_endian>
270void
f6ce93d6 271Sized_relobj<size, big_endian>::setup(
bae7f79e
ILT
272 const elfcpp::Ehdr<size, big_endian>& ehdr)
273{
dbe717ef
ILT
274 this->set_target(ehdr.get_e_machine(), size, big_endian,
275 ehdr.get_e_ident()[elfcpp::EI_OSABI],
276 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
12e14209 277
dbe717ef 278 const unsigned int shnum = this->elf_file_.shnum();
a2fb1b05 279 this->set_shnum(shnum);
dbe717ef 280}
12e14209 281
dbe717ef
ILT
282// Find the SHT_SYMTAB section, given the section headers. The ELF
283// standard says that maybe in the future there can be more than one
284// SHT_SYMTAB section. Until somebody figures out how that could
285// work, we assume there is only one.
12e14209 286
dbe717ef
ILT
287template<int size, bool big_endian>
288void
289Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
290{
291 const unsigned int shnum = this->shnum();
292 this->symtab_shndx_ = 0;
293 if (shnum > 0)
bae7f79e 294 {
dbe717ef
ILT
295 // Look through the sections in reverse order, since gas tends
296 // to put the symbol table at the end.
297 const unsigned char* p = pshdrs + shnum * This::shdr_size;
298 unsigned int i = shnum;
d491d34e
ILT
299 unsigned int xindex_shndx = 0;
300 unsigned int xindex_link = 0;
dbe717ef 301 while (i > 0)
bae7f79e 302 {
dbe717ef
ILT
303 --i;
304 p -= This::shdr_size;
305 typename This::Shdr shdr(p);
306 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
307 {
308 this->symtab_shndx_ = i;
d491d34e
ILT
309 if (xindex_shndx > 0 && xindex_link == i)
310 {
311 Xindex* xindex =
312 new Xindex(this->elf_file_.large_shndx_offset());
313 xindex->read_symtab_xindex<size, big_endian>(this,
314 xindex_shndx,
315 pshdrs);
316 this->set_xindex(xindex);
317 }
dbe717ef
ILT
318 break;
319 }
d491d34e
ILT
320
321 // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
322 // one. This will work if it follows the SHT_SYMTAB
323 // section.
324 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
325 {
326 xindex_shndx = i;
327 xindex_link = this->adjust_shndx(shdr.get_sh_link());
328 }
bae7f79e 329 }
bae7f79e
ILT
330 }
331}
332
d491d34e
ILT
333// Return the Xindex structure to use for object with lots of
334// sections.
335
336template<int size, bool big_endian>
337Xindex*
338Sized_relobj<size, big_endian>::do_initialize_xindex()
339{
340 gold_assert(this->symtab_shndx_ != -1U);
341 Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
342 xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
343 return xindex;
344}
345
730cdc88
ILT
346// Return whether SHDR has the right type and flags to be a GNU
347// .eh_frame section.
348
349template<int size, bool big_endian>
350bool
351Sized_relobj<size, big_endian>::check_eh_frame_flags(
352 const elfcpp::Shdr<size, big_endian>* shdr) const
353{
2c38906f 354 return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
1650c4ff 355 && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
730cdc88
ILT
356}
357
358// Return whether there is a GNU .eh_frame section, given the section
359// headers and the section names.
360
361template<int size, bool big_endian>
362bool
8383303e
ILT
363Sized_relobj<size, big_endian>::find_eh_frame(
364 const unsigned char* pshdrs,
365 const char* names,
366 section_size_type names_size) const
730cdc88
ILT
367{
368 const unsigned int shnum = this->shnum();
369 const unsigned char* p = pshdrs + This::shdr_size;
370 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
371 {
372 typename This::Shdr shdr(p);
373 if (this->check_eh_frame_flags(&shdr))
374 {
375 if (shdr.get_sh_name() >= names_size)
376 {
377 this->error(_("bad section name offset for section %u: %lu"),
378 i, static_cast<unsigned long>(shdr.get_sh_name()));
379 continue;
380 }
381
382 const char* name = names + shdr.get_sh_name();
383 if (strcmp(name, ".eh_frame") == 0)
384 return true;
385 }
386 }
387 return false;
388}
389
12e14209 390// Read the sections and symbols from an object file.
bae7f79e
ILT
391
392template<int size, bool big_endian>
12e14209 393void
f6ce93d6 394Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
bae7f79e 395{
dbe717ef 396 this->read_section_data(&this->elf_file_, sd);
12e14209 397
dbe717ef
ILT
398 const unsigned char* const pshdrs = sd->section_headers->data();
399
400 this->find_symtab(pshdrs);
12e14209 401
730cdc88
ILT
402 const unsigned char* namesu = sd->section_names->data();
403 const char* names = reinterpret_cast<const char*>(namesu);
1650c4ff
ILT
404 if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
405 {
406 if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
407 this->has_eh_frame_ = true;
408 }
730cdc88 409
75f2446e
ILT
410 sd->symbols = NULL;
411 sd->symbols_size = 0;
730cdc88 412 sd->external_symbols_offset = 0;
75f2446e
ILT
413 sd->symbol_names = NULL;
414 sd->symbol_names_size = 0;
415
645f8123 416 if (this->symtab_shndx_ == 0)
bae7f79e
ILT
417 {
418 // No symbol table. Weird but legal.
12e14209 419 return;
bae7f79e
ILT
420 }
421
12e14209
ILT
422 // Get the symbol table section header.
423 typename This::Shdr symtabshdr(pshdrs
645f8123 424 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 425 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
bae7f79e 426
730cdc88
ILT
427 // If this object has a .eh_frame section, we need all the symbols.
428 // Otherwise we only need the external symbols. While it would be
429 // simpler to just always read all the symbols, I've seen object
430 // files with well over 2000 local symbols, which for a 64-bit
431 // object file format is over 5 pages that we don't need to read
432 // now.
433
75f65a3e 434 const int sym_size = This::sym_size;
92e059d8
ILT
435 const unsigned int loccount = symtabshdr.get_sh_info();
436 this->local_symbol_count_ = loccount;
7bf1f802 437 this->local_values_.resize(loccount);
8383303e 438 section_offset_type locsize = loccount * sym_size;
730cdc88 439 off_t dataoff = symtabshdr.get_sh_offset();
8383303e
ILT
440 section_size_type datasize =
441 convert_to_section_size_type(symtabshdr.get_sh_size());
730cdc88 442 off_t extoff = dataoff + locsize;
8383303e 443 section_size_type extsize = datasize - locsize;
75f65a3e 444
730cdc88 445 off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
8383303e 446 section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
730cdc88 447
3f2e6a2d
CC
448 if (readsize == 0)
449 {
450 // No external symbols. Also weird but also legal.
451 return;
452 }
453
39d0cb0e 454 File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
bae7f79e
ILT
455
456 // Read the section header for the symbol names.
d491d34e 457 unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
dbe717ef 458 if (strtab_shndx >= this->shnum())
bae7f79e 459 {
75f2446e
ILT
460 this->error(_("invalid symbol table name index: %u"), strtab_shndx);
461 return;
bae7f79e 462 }
dbe717ef 463 typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
bae7f79e
ILT
464 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
465 {
75f2446e
ILT
466 this->error(_("symbol table name section has wrong type: %u"),
467 static_cast<unsigned int>(strtabshdr.get_sh_type()));
468 return;
bae7f79e
ILT
469 }
470
471 // Read the symbol names.
472 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
39d0cb0e
ILT
473 strtabshdr.get_sh_size(),
474 false, true);
bae7f79e 475
12e14209 476 sd->symbols = fvsymtab;
730cdc88
ILT
477 sd->symbols_size = readsize;
478 sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
12e14209 479 sd->symbol_names = fvstrtab;
8383303e
ILT
480 sd->symbol_names_size =
481 convert_to_section_size_type(strtabshdr.get_sh_size());
a2fb1b05
ILT
482}
483
730cdc88 484// Return the section index of symbol SYM. Set *VALUE to its value in
d491d34e
ILT
485// the object file. Set *IS_ORDINARY if this is an ordinary section
486// index. not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
487// Note that for a symbol which is not defined in this object file,
488// this will set *VALUE to 0 and return SHN_UNDEF; it will not return
489// the final value of the symbol in the link.
730cdc88
ILT
490
491template<int size, bool big_endian>
492unsigned int
493Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
d491d34e
ILT
494 Address* value,
495 bool* is_ordinary)
730cdc88 496{
8383303e 497 section_size_type symbols_size;
730cdc88
ILT
498 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
499 &symbols_size,
500 false);
501
502 const size_t count = symbols_size / This::sym_size;
503 gold_assert(sym < count);
504
505 elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
506 *value = elfsym.get_st_value();
d491d34e
ILT
507
508 return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
730cdc88
ILT
509}
510
a2fb1b05
ILT
511// Return whether to include a section group in the link. LAYOUT is
512// used to keep track of which section groups we have already seen.
513// INDEX is the index of the section group and SHDR is the section
514// header. If we do not want to include this group, we set bits in
515// OMIT for each section which should be discarded.
516
517template<int size, bool big_endian>
518bool
f6ce93d6 519Sized_relobj<size, big_endian>::include_section_group(
6a74a719 520 Symbol_table* symtab,
a2fb1b05
ILT
521 Layout* layout,
522 unsigned int index,
6a74a719 523 const char* name,
e94cf127
CC
524 const unsigned char* shdrs,
525 const char* section_names,
526 section_size_type section_names_size,
a2fb1b05
ILT
527 std::vector<bool>* omit)
528{
529 // Read the section contents.
e94cf127 530 typename This::Shdr shdr(shdrs + index * This::shdr_size);
a2fb1b05 531 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
39d0cb0e 532 shdr.get_sh_size(), true, false);
a2fb1b05
ILT
533 const elfcpp::Elf_Word* pword =
534 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
535
536 // The first word contains flags. We only care about COMDAT section
537 // groups. Other section groups are always included in the link
538 // just like ordinary sections.
f6ce93d6 539 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
a2fb1b05
ILT
540
541 // Look up the group signature, which is the name of a symbol. This
542 // is a lot of effort to go to to read a string. Why didn't they
6a74a719
ILT
543 // just have the group signature point into the string table, rather
544 // than indirect through a symbol?
a2fb1b05
ILT
545
546 // Get the appropriate symbol table header (this will normally be
547 // the single SHT_SYMTAB section, but in principle it need not be).
d491d34e 548 const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
645f8123 549 typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
a2fb1b05
ILT
550
551 // Read the symbol table entry.
d491d34e
ILT
552 unsigned int symndx = shdr.get_sh_info();
553 if (symndx >= symshdr.get_sh_size() / This::sym_size)
a2fb1b05 554 {
75f2446e 555 this->error(_("section group %u info %u out of range"),
d491d34e 556 index, symndx);
75f2446e 557 return false;
a2fb1b05 558 }
d491d34e 559 off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
39d0cb0e
ILT
560 const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
561 false);
a2fb1b05
ILT
562 elfcpp::Sym<size, big_endian> sym(psym);
563
a2fb1b05 564 // Read the symbol table names.
8383303e 565 section_size_type symnamelen;
645f8123 566 const unsigned char* psymnamesu;
d491d34e
ILT
567 psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
568 &symnamelen, true);
a2fb1b05
ILT
569 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
570
571 // Get the section group signature.
645f8123 572 if (sym.get_st_name() >= symnamelen)
a2fb1b05 573 {
75f2446e 574 this->error(_("symbol %u name offset %u out of range"),
d491d34e 575 symndx, sym.get_st_name());
75f2446e 576 return false;
a2fb1b05
ILT
577 }
578
e94cf127 579 std::string signature(psymnames + sym.get_st_name());
a2fb1b05 580
ead1e424
ILT
581 // It seems that some versions of gas will create a section group
582 // associated with a section symbol, and then fail to give a name to
583 // the section symbol. In such a case, use the name of the section.
645f8123 584 if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
ead1e424 585 {
d491d34e
ILT
586 bool is_ordinary;
587 unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
588 sym.get_st_shndx(),
589 &is_ordinary);
590 if (!is_ordinary || sym_shndx >= this->shnum())
591 {
592 this->error(_("symbol %u invalid section index %u"),
593 symndx, sym_shndx);
594 return false;
595 }
e94cf127
CC
596 typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
597 if (member_shdr.get_sh_name() < section_names_size)
598 signature = section_names + member_shdr.get_sh_name();
ead1e424
ILT
599 }
600
e94cf127
CC
601 // Record this section group in the layout, and see whether we've already
602 // seen one with the same signature.
603 bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
604 || layout->add_comdat(this, index, signature, true));
605
ef9beddf 606 Sized_relobj<size, big_endian>* kept_object = NULL;
e94cf127 607 Comdat_group* kept_group = NULL;
6a74a719 608
e94cf127 609 if (!include_group)
6a74a719 610 {
e94cf127
CC
611 // This group is being discarded. Find the object and group
612 // that was kept in its place.
613 unsigned int kept_group_index = 0;
ef9beddf
ILT
614 Relobj* kept_relobj = layout->find_kept_object(signature,
615 &kept_group_index);
616 kept_object = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
e94cf127
CC
617 if (kept_object != NULL)
618 kept_group = kept_object->find_comdat_group(kept_group_index);
619 }
620 else if (flags & elfcpp::GRP_COMDAT)
621 {
622 // This group is being kept. Create the table to map section names
623 // to section indexes and add it to the table of groups.
624 kept_group = new Comdat_group();
625 this->add_comdat_group(index, kept_group);
6a74a719 626 }
a2fb1b05 627
a2fb1b05 628 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
8825ac63
ILT
629
630 std::vector<unsigned int> shndxes;
631 bool relocate_group = include_group && parameters->options().relocatable();
632 if (relocate_group)
633 shndxes.reserve(count - 1);
634
a2fb1b05
ILT
635 for (size_t i = 1; i < count; ++i)
636 {
f6ce93d6 637 elfcpp::Elf_Word secnum =
8825ac63
ILT
638 this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
639
640 if (relocate_group)
641 shndxes.push_back(secnum);
642
a2fb1b05
ILT
643 if (secnum >= this->shnum())
644 {
75f2446e
ILT
645 this->error(_("section %u in section group %u out of range"),
646 secnum, index);
647 continue;
a2fb1b05 648 }
55438702
ILT
649
650 // Check for an earlier section number, since we're going to get
651 // it wrong--we may have already decided to include the section.
652 if (secnum < index)
653 this->error(_("invalid section group %u refers to earlier section %u"),
654 index, secnum);
655
e94cf127
CC
656 // Get the name of the member section.
657 typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
658 if (member_shdr.get_sh_name() >= section_names_size)
659 {
660 // This is an error, but it will be diagnosed eventually
661 // in do_layout, so we don't need to do anything here but
662 // ignore it.
663 continue;
664 }
665 std::string mname(section_names + member_shdr.get_sh_name());
666
667 if (!include_group)
668 {
669 (*omit)[secnum] = true;
670 if (kept_group != NULL)
671 {
672 // Find the corresponding kept section, and store that info
673 // in the discarded section table.
674 Comdat_group::const_iterator p = kept_group->find(mname);
675 if (p != kept_group->end())
676 {
677 Kept_comdat_section* kept =
678 new Kept_comdat_section(kept_object, p->second);
679 this->set_kept_comdat_section(secnum, kept);
680 }
681 }
682 }
683 else if (flags & elfcpp::GRP_COMDAT)
684 {
685 // Add the section to the kept group table.
686 gold_assert(kept_group != NULL);
687 kept_group->insert(std::make_pair(mname, secnum));
688 }
a2fb1b05
ILT
689 }
690
8825ac63
ILT
691 if (relocate_group)
692 layout->layout_group(symtab, this, index, name, signature.c_str(),
693 shdr, flags, &shndxes);
694
e94cf127 695 return include_group;
a2fb1b05
ILT
696}
697
698// Whether to include a linkonce section in the link. NAME is the
699// name of the section and SHDR is the section header.
700
701// Linkonce sections are a GNU extension implemented in the original
702// GNU linker before section groups were defined. The semantics are
703// that we only include one linkonce section with a given name. The
704// name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
705// where T is the type of section and SYMNAME is the name of a symbol.
706// In an attempt to make linkonce sections interact well with section
707// groups, we try to identify SYMNAME and use it like a section group
708// signature. We want to block section groups with that signature,
709// but not other linkonce sections with that signature. We also use
710// the full name of the linkonce section as a normal section group
711// signature.
712
713template<int size, bool big_endian>
714bool
f6ce93d6 715Sized_relobj<size, big_endian>::include_linkonce_section(
a2fb1b05 716 Layout* layout,
e94cf127 717 unsigned int index,
a2fb1b05
ILT
718 const char* name,
719 const elfcpp::Shdr<size, big_endian>&)
720{
ad435a24
ILT
721 // In general the symbol name we want will be the string following
722 // the last '.'. However, we have to handle the case of
723 // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
724 // some versions of gcc. So we use a heuristic: if the name starts
725 // with ".gnu.linkonce.t.", we use everything after that. Otherwise
726 // we look for the last '.'. We can't always simply skip
727 // ".gnu.linkonce.X", because we have to deal with cases like
728 // ".gnu.linkonce.d.rel.ro.local".
729 const char* const linkonce_t = ".gnu.linkonce.t.";
730 const char* symname;
731 if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
732 symname = name + strlen(linkonce_t);
733 else
734 symname = strrchr(name, '.') + 1;
e94cf127
CC
735 std::string sig1(symname);
736 std::string sig2(name);
737 bool include1 = layout->add_comdat(this, index, sig1, false);
738 bool include2 = layout->add_comdat(this, index, sig2, true);
739
740 if (!include2)
741 {
742 // The section is being discarded on the basis of its section
743 // name (i.e., the kept section was also a linkonce section).
744 // In this case, the section index stored with the layout object
745 // is the linkonce section that was kept.
746 unsigned int kept_group_index = 0;
ef9beddf
ILT
747 Relobj* kept_relobj = layout->find_kept_object(sig2, &kept_group_index);
748 if (kept_relobj != NULL)
e94cf127 749 {
ef9beddf
ILT
750 Sized_relobj<size, big_endian>* kept_object
751 = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
e94cf127
CC
752 Kept_comdat_section* kept =
753 new Kept_comdat_section(kept_object, kept_group_index);
754 this->set_kept_comdat_section(index, kept);
755 }
756 }
757 else if (!include1)
758 {
759 // The section is being discarded on the basis of its symbol
760 // name. This means that the corresponding kept section was
761 // part of a comdat group, and it will be difficult to identify
762 // the specific section within that group that corresponds to
763 // this linkonce section. We'll handle the simple case where
764 // the group has only one member section. Otherwise, it's not
765 // worth the effort.
766 unsigned int kept_group_index = 0;
ef9beddf
ILT
767 Relobj* kept_relobj = layout->find_kept_object(sig1, &kept_group_index);
768 if (kept_relobj != NULL)
e94cf127 769 {
ef9beddf
ILT
770 Sized_relobj<size, big_endian>* kept_object =
771 static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
e94cf127
CC
772 Comdat_group* kept_group =
773 kept_object->find_comdat_group(kept_group_index);
774 if (kept_group != NULL && kept_group->size() == 1)
775 {
776 Comdat_group::const_iterator p = kept_group->begin();
777 gold_assert(p != kept_group->end());
778 Kept_comdat_section* kept =
779 new Kept_comdat_section(kept_object, p->second);
780 this->set_kept_comdat_section(index, kept);
781 }
782 }
783 }
784
a783673b 785 return include1 && include2;
a2fb1b05
ILT
786}
787
5995b570
CC
788// Layout an input section.
789
790template<int size, bool big_endian>
791inline void
792Sized_relobj<size, big_endian>::layout_section(Layout* layout,
793 unsigned int shndx,
794 const char* name,
795 typename This::Shdr& shdr,
796 unsigned int reloc_shndx,
797 unsigned int reloc_type)
798{
799 off_t offset;
800 Output_section* os = layout->layout(this, shndx, name, shdr,
801 reloc_shndx, reloc_type, &offset);
802
803 this->output_sections()[shndx] = os;
804 if (offset == -1)
805 this->section_offsets_[shndx] = invalid_address;
806 else
807 this->section_offsets_[shndx] = convert_types<Address, off_t>(offset);
808
809 // If this section requires special handling, and if there are
810 // relocs that apply to it, then we must do the special handling
811 // before we apply the relocs.
812 if (offset == -1 && reloc_shndx != 0)
813 this->set_relocs_must_follow_section_writes();
814}
815
a2fb1b05
ILT
816// Lay out the input sections. We walk through the sections and check
817// whether they should be included in the link. If they should, we
818// pass them to the Layout object, which will return an output section
819// and an offset.
820
821template<int size, bool big_endian>
822void
7e1edb90 823Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
f6ce93d6 824 Layout* layout,
12e14209 825 Read_symbols_data* sd)
a2fb1b05 826{
dbe717ef 827 const unsigned int shnum = this->shnum();
12e14209
ILT
828 if (shnum == 0)
829 return;
a2fb1b05
ILT
830
831 // Get the section headers.
e94cf127
CC
832 const unsigned char* shdrs = sd->section_headers->data();
833 const unsigned char* pshdrs;
a2fb1b05
ILT
834
835 // Get the section names.
12e14209 836 const unsigned char* pnamesu = sd->section_names->data();
a2fb1b05
ILT
837 const char* pnames = reinterpret_cast<const char*>(pnamesu);
838
5995b570
CC
839 // If any input files have been claimed by plugins, we need to defer
840 // actual layout until the replacement files have arrived.
841 const bool should_defer_layout =
842 (parameters->options().has_plugins()
843 && parameters->options().plugins()->should_defer_layout());
844 unsigned int num_sections_to_defer = 0;
845
730cdc88
ILT
846 // For each section, record the index of the reloc section if any.
847 // Use 0 to mean that there is no reloc section, -1U to mean that
848 // there is more than one.
849 std::vector<unsigned int> reloc_shndx(shnum, 0);
850 std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
851 // Skip the first, dummy, section.
e94cf127 852 pshdrs = shdrs + This::shdr_size;
730cdc88
ILT
853 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
854 {
855 typename This::Shdr shdr(pshdrs);
856
5995b570
CC
857 // Count the number of sections whose layout will be deferred.
858 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
859 ++num_sections_to_defer;
860
730cdc88
ILT
861 unsigned int sh_type = shdr.get_sh_type();
862 if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
863 {
d491d34e 864 unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
730cdc88
ILT
865 if (target_shndx == 0 || target_shndx >= shnum)
866 {
867 this->error(_("relocation section %u has bad info %u"),
868 i, target_shndx);
869 continue;
870 }
871
872 if (reloc_shndx[target_shndx] != 0)
873 reloc_shndx[target_shndx] = -1U;
874 else
875 {
876 reloc_shndx[target_shndx] = i;
877 reloc_type[target_shndx] = sh_type;
878 }
879 }
880 }
881
ef9beddf
ILT
882 Output_sections& out_sections(this->output_sections());
883 std::vector<Address>& out_section_offsets(this->section_offsets_);
884
885 out_sections.resize(shnum);
886 out_section_offsets.resize(shnum);
a2fb1b05 887
88dd47ac
ILT
888 // If we are only linking for symbols, then there is nothing else to
889 // do here.
890 if (this->input_file()->just_symbols())
891 {
892 delete sd->section_headers;
893 sd->section_headers = NULL;
894 delete sd->section_names;
895 sd->section_names = NULL;
896 return;
897 }
898
5995b570
CC
899 if (num_sections_to_defer > 0)
900 {
901 parameters->options().plugins()->add_deferred_layout_object(this);
902 this->deferred_layout_.reserve(num_sections_to_defer);
903 }
904
35cdfc9a
ILT
905 // Whether we've seen a .note.GNU-stack section.
906 bool seen_gnu_stack = false;
907 // The flags of a .note.GNU-stack section.
908 uint64_t gnu_stack_flags = 0;
909
a2fb1b05
ILT
910 // Keep track of which sections to omit.
911 std::vector<bool> omit(shnum, false);
912
7019cd25 913 // Keep track of reloc sections when emitting relocations.
8851ecca
ILT
914 const bool relocatable = parameters->options().relocatable();
915 const bool emit_relocs = (relocatable
916 || parameters->options().emit_relocs());
6a74a719
ILT
917 std::vector<unsigned int> reloc_sections;
918
730cdc88
ILT
919 // Keep track of .eh_frame sections.
920 std::vector<unsigned int> eh_frame_sections;
921
f6ce93d6 922 // Skip the first, dummy, section.
e94cf127 923 pshdrs = shdrs + This::shdr_size;
f6ce93d6 924 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
a2fb1b05 925 {
75f65a3e 926 typename This::Shdr shdr(pshdrs);
a2fb1b05 927
12e14209 928 if (shdr.get_sh_name() >= sd->section_names_size)
a2fb1b05 929 {
75f2446e
ILT
930 this->error(_("bad section name offset for section %u: %lu"),
931 i, static_cast<unsigned long>(shdr.get_sh_name()));
932 return;
a2fb1b05
ILT
933 }
934
935 const char* name = pnames + shdr.get_sh_name();
936
dbe717ef 937 if (this->handle_gnu_warning_section(name, i, symtab))
f6ce93d6 938 {
8851ecca 939 if (!relocatable)
f6ce93d6
ILT
940 omit[i] = true;
941 }
942
35cdfc9a
ILT
943 // The .note.GNU-stack section is special. It gives the
944 // protection flags that this object file requires for the stack
945 // in memory.
946 if (strcmp(name, ".note.GNU-stack") == 0)
947 {
948 seen_gnu_stack = true;
949 gnu_stack_flags |= shdr.get_sh_flags();
950 omit[i] = true;
951 }
952
a2fb1b05
ILT
953 bool discard = omit[i];
954 if (!discard)
955 {
956 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
957 {
e94cf127
CC
958 if (!this->include_section_group(symtab, layout, i, name, shdrs,
959 pnames, sd->section_names_size,
6a74a719 960 &omit))
a2fb1b05
ILT
961 discard = true;
962 }
cba134d6
ILT
963 else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
964 && Layout::is_linkonce(name))
a2fb1b05 965 {
e94cf127 966 if (!this->include_linkonce_section(layout, i, name, shdr))
a2fb1b05
ILT
967 discard = true;
968 }
969 }
970
971 if (discard)
972 {
973 // Do not include this section in the link.
ef9beddf 974 out_sections[i] = NULL;
eff45813 975 out_section_offsets[i] = invalid_address;
a2fb1b05
ILT
976 continue;
977 }
978
6a74a719
ILT
979 // When doing a relocatable link we are going to copy input
980 // reloc sections into the output. We only want to copy the
981 // ones associated with sections which are not being discarded.
982 // However, we don't know that yet for all sections. So save
983 // reloc sections and process them later.
7019cd25 984 if (emit_relocs
6a74a719
ILT
985 && (shdr.get_sh_type() == elfcpp::SHT_REL
986 || shdr.get_sh_type() == elfcpp::SHT_RELA))
987 {
988 reloc_sections.push_back(i);
989 continue;
990 }
991
8851ecca 992 if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
6a74a719
ILT
993 continue;
994
730cdc88
ILT
995 // The .eh_frame section is special. It holds exception frame
996 // information that we need to read in order to generate the
997 // exception frame header. We process these after all the other
998 // sections so that the exception frame reader can reliably
999 // determine which sections are being discarded, and discard the
1000 // corresponding information.
8851ecca 1001 if (!relocatable
730cdc88
ILT
1002 && strcmp(name, ".eh_frame") == 0
1003 && this->check_eh_frame_flags(&shdr))
1004 {
1005 eh_frame_sections.push_back(i);
1006 continue;
1007 }
1008
5995b570
CC
1009 if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1010 {
1011 this->deferred_layout_.push_back(Deferred_layout(i, name, pshdrs,
1012 reloc_shndx[i],
1013 reloc_type[i]));
a2fb1b05 1014
5995b570
CC
1015 // Put dummy values here; real values will be supplied by
1016 // do_layout_deferred_sections.
1017 out_sections[i] = reinterpret_cast<Output_section*>(1);
1018 out_section_offsets[i] = invalid_address;
1019 }
ef9beddf 1020 else
5995b570
CC
1021 {
1022 this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1023 reloc_type[i]);
1024 }
12e14209
ILT
1025 }
1026
35cdfc9a
ILT
1027 layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1028
6a74a719
ILT
1029 // When doing a relocatable link handle the reloc sections at the
1030 // end.
7019cd25 1031 if (emit_relocs)
6a74a719
ILT
1032 this->size_relocatable_relocs();
1033 for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1034 p != reloc_sections.end();
1035 ++p)
1036 {
1037 unsigned int i = *p;
1038 const unsigned char* pshdr;
1039 pshdr = sd->section_headers->data() + i * This::shdr_size;
1040 typename This::Shdr shdr(pshdr);
1041
d491d34e 1042 unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
6a74a719
ILT
1043 if (data_shndx >= shnum)
1044 {
1045 // We already warned about this above.
1046 continue;
1047 }
1048
ef9beddf 1049 Output_section* data_section = out_sections[data_shndx];
6a74a719
ILT
1050 if (data_section == NULL)
1051 {
ef9beddf 1052 out_sections[i] = NULL;
eff45813 1053 out_section_offsets[i] = invalid_address;
6a74a719
ILT
1054 continue;
1055 }
1056
1057 Relocatable_relocs* rr = new Relocatable_relocs();
1058 this->set_relocatable_relocs(i, rr);
1059
1060 Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1061 rr);
ef9beddf 1062 out_sections[i] = os;
eff45813 1063 out_section_offsets[i] = invalid_address;
6a74a719
ILT
1064 }
1065
730cdc88
ILT
1066 // Handle the .eh_frame sections at the end.
1067 for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1068 p != eh_frame_sections.end();
1069 ++p)
1070 {
1071 gold_assert(this->has_eh_frame_);
1072 gold_assert(sd->external_symbols_offset != 0);
1073
1074 unsigned int i = *p;
1075 const unsigned char *pshdr;
1076 pshdr = sd->section_headers->data() + i * This::shdr_size;
1077 typename This::Shdr shdr(pshdr);
1078
1079 off_t offset;
1080 Output_section* os = layout->layout_eh_frame(this,
1081 sd->symbols->data(),
1082 sd->symbols_size,
1083 sd->symbol_names->data(),
1084 sd->symbol_names_size,
1085 i, shdr,
1086 reloc_shndx[i],
1087 reloc_type[i],
1088 &offset);
ef9beddf
ILT
1089 out_sections[i] = os;
1090 if (offset == -1)
eff45813 1091 out_section_offsets[i] = invalid_address;
ef9beddf
ILT
1092 else
1093 out_section_offsets[i] = convert_types<Address, off_t>(offset);
730cdc88
ILT
1094
1095 // If this section requires special handling, and if there are
1096 // relocs that apply to it, then we must do the special handling
1097 // before we apply the relocs.
1098 if (offset == -1 && reloc_shndx[i] != 0)
1099 this->set_relocs_must_follow_section_writes();
1100 }
1101
12e14209
ILT
1102 delete sd->section_headers;
1103 sd->section_headers = NULL;
1104 delete sd->section_names;
1105 sd->section_names = NULL;
1106}
1107
5995b570
CC
1108// Layout sections whose layout was deferred while waiting for
1109// input files from a plugin.
1110
1111template<int size, bool big_endian>
1112void
1113Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1114{
1115 typename std::vector<Deferred_layout>::iterator deferred;
1116
1117 for (deferred = this->deferred_layout_.begin();
1118 deferred != this->deferred_layout_.end();
1119 ++deferred)
1120 {
1121 typename This::Shdr shdr(deferred->shdr_data_);
1122 this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1123 shdr, deferred->reloc_shndx_, deferred->reloc_type_);
1124 }
1125
1126 this->deferred_layout_.clear();
1127}
1128
12e14209
ILT
1129// Add the symbols to the symbol table.
1130
1131template<int size, bool big_endian>
1132void
f6ce93d6 1133Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
12e14209
ILT
1134 Read_symbols_data* sd)
1135{
1136 if (sd->symbols == NULL)
1137 {
a3ad94ed 1138 gold_assert(sd->symbol_names == NULL);
12e14209
ILT
1139 return;
1140 }
a2fb1b05 1141
12e14209 1142 const int sym_size = This::sym_size;
730cdc88
ILT
1143 size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1144 / sym_size);
8383303e 1145 if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
12e14209 1146 {
75f2446e
ILT
1147 this->error(_("size of symbols is not multiple of symbol size"));
1148 return;
a2fb1b05 1149 }
12e14209 1150
730cdc88 1151 this->symbols_.resize(symcount);
12e14209 1152
12e14209
ILT
1153 const char* sym_names =
1154 reinterpret_cast<const char*>(sd->symbol_names->data());
730cdc88
ILT
1155 symtab->add_from_relobj(this,
1156 sd->symbols->data() + sd->external_symbols_offset,
7fcd3aa9 1157 symcount, this->local_symbol_count_,
d491d34e 1158 sym_names, sd->symbol_names_size,
92de84a6
ILT
1159 &this->symbols_,
1160 &this->defined_count_);
12e14209
ILT
1161
1162 delete sd->symbols;
1163 sd->symbols = NULL;
1164 delete sd->symbol_names;
1165 sd->symbol_names = NULL;
bae7f79e
ILT
1166}
1167
cb295612
ILT
1168// First pass over the local symbols. Here we add their names to
1169// *POOL and *DYNPOOL, and we store the symbol value in
1170// THIS->LOCAL_VALUES_. This function is always called from a
1171// singleton thread. This is followed by a call to
1172// finalize_local_symbols.
75f65a3e
ILT
1173
1174template<int size, bool big_endian>
7bf1f802
ILT
1175void
1176Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1177 Stringpool* dynpool)
75f65a3e 1178{
a3ad94ed 1179 gold_assert(this->symtab_shndx_ != -1U);
645f8123 1180 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
1181 {
1182 // This object has no symbols. Weird but legal.
7bf1f802 1183 return;
61ba1cf9
ILT
1184 }
1185
75f65a3e 1186 // Read the symbol table section header.
645f8123
ILT
1187 const unsigned int symtab_shndx = this->symtab_shndx_;
1188 typename This::Shdr symtabshdr(this,
1189 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 1190 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
75f65a3e
ILT
1191
1192 // Read the local symbols.
75f65a3e 1193 const int sym_size = This::sym_size;
92e059d8 1194 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 1195 gold_assert(loccount == symtabshdr.get_sh_info());
75f65a3e
ILT
1196 off_t locsize = loccount * sym_size;
1197 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
39d0cb0e 1198 locsize, true, true);
75f65a3e 1199
75f65a3e 1200 // Read the symbol names.
d491d34e
ILT
1201 const unsigned int strtab_shndx =
1202 this->adjust_shndx(symtabshdr.get_sh_link());
8383303e 1203 section_size_type strtab_size;
645f8123 1204 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57
ILT
1205 &strtab_size,
1206 true);
75f65a3e
ILT
1207 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1208
1209 // Loop over the local symbols.
1210
ef9beddf 1211 const Output_sections& out_sections(this->output_sections());
75f65a3e 1212 unsigned int shnum = this->shnum();
61ba1cf9 1213 unsigned int count = 0;
7bf1f802 1214 unsigned int dyncount = 0;
75f65a3e
ILT
1215 // Skip the first, dummy, symbol.
1216 psyms += sym_size;
61ba1cf9 1217 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
75f65a3e
ILT
1218 {
1219 elfcpp::Sym<size, big_endian> sym(psyms);
1220
b8e6aad9
ILT
1221 Symbol_value<size>& lv(this->local_values_[i]);
1222
d491d34e
ILT
1223 bool is_ordinary;
1224 unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1225 &is_ordinary);
1226 lv.set_input_shndx(shndx, is_ordinary);
75f65a3e 1227
063f12a8
ILT
1228 if (sym.get_st_type() == elfcpp::STT_SECTION)
1229 lv.set_is_section_symbol();
7bf1f802
ILT
1230 else if (sym.get_st_type() == elfcpp::STT_TLS)
1231 lv.set_is_tls_symbol();
1232
1233 // Save the input symbol value for use in do_finalize_local_symbols().
1234 lv.set_input_value(sym.get_st_value());
1235
1236 // Decide whether this symbol should go into the output file.
063f12a8 1237
ef9beddf 1238 if (shndx < shnum && out_sections[shndx] == NULL)
7bf1f802
ILT
1239 {
1240 lv.set_no_output_symtab_entry();
dceae3c1 1241 gold_assert(!lv.needs_output_dynsym_entry());
7bf1f802
ILT
1242 continue;
1243 }
1244
1245 if (sym.get_st_type() == elfcpp::STT_SECTION)
1246 {
1247 lv.set_no_output_symtab_entry();
dceae3c1 1248 gold_assert(!lv.needs_output_dynsym_entry());
7bf1f802
ILT
1249 continue;
1250 }
1251
1252 if (sym.get_st_name() >= strtab_size)
1253 {
1254 this->error(_("local symbol %u section name out of range: %u >= %u"),
1255 i, sym.get_st_name(),
1256 static_cast<unsigned int>(strtab_size));
1257 lv.set_no_output_symtab_entry();
1258 continue;
1259 }
1260
1261 // Add the symbol to the symbol table string pool.
1262 const char* name = pnames + sym.get_st_name();
1263 pool->add(name, true, NULL);
1264 ++count;
1265
1266 // If needed, add the symbol to the dynamic symbol table string pool.
1267 if (lv.needs_output_dynsym_entry())
1268 {
1269 dynpool->add(name, true, NULL);
1270 ++dyncount;
1271 }
1272 }
1273
1274 this->output_local_symbol_count_ = count;
1275 this->output_local_dynsym_count_ = dyncount;
1276}
1277
cb295612 1278// Finalize the local symbols. Here we set the final value in
7bf1f802 1279// THIS->LOCAL_VALUES_ and set their output symbol table indexes.
17a1d0a9 1280// This function is always called from a singleton thread. The actual
7bf1f802
ILT
1281// output of the local symbols will occur in a separate task.
1282
1283template<int size, bool big_endian>
1284unsigned int
1285Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
d491d34e 1286 off_t off)
7bf1f802
ILT
1287{
1288 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1289
1290 const unsigned int loccount = this->local_symbol_count_;
1291 this->local_symbol_offset_ = off;
1292
ef9beddf
ILT
1293 const Output_sections& out_sections(this->output_sections());
1294 const std::vector<Address>& out_offsets(this->section_offsets_);
7bf1f802
ILT
1295 unsigned int shnum = this->shnum();
1296
1297 for (unsigned int i = 1; i < loccount; ++i)
1298 {
1299 Symbol_value<size>& lv(this->local_values_[i]);
1300
d491d34e
ILT
1301 bool is_ordinary;
1302 unsigned int shndx = lv.input_shndx(&is_ordinary);
7bf1f802
ILT
1303
1304 // Set the output symbol value.
ef9beddf 1305
d491d34e 1306 if (!is_ordinary)
75f65a3e 1307 {
0dfbdef4 1308 if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
7bf1f802 1309 lv.set_output_value(lv.input_value());
61ba1cf9 1310 else
75f65a3e 1311 {
75f2446e
ILT
1312 this->error(_("unknown section index %u for local symbol %u"),
1313 shndx, i);
1314 lv.set_output_value(0);
75f65a3e 1315 }
75f65a3e
ILT
1316 }
1317 else
1318 {
1319 if (shndx >= shnum)
1320 {
75f2446e
ILT
1321 this->error(_("local symbol %u section index %u out of range"),
1322 i, shndx);
1323 shndx = 0;
75f65a3e
ILT
1324 }
1325
ef9beddf 1326 Output_section* os = out_sections[shndx];
b8e6aad9
ILT
1327
1328 if (os == NULL)
61ba1cf9 1329 {
e94cf127
CC
1330 // This local symbol belongs to a section we are discarding.
1331 // In some cases when applying relocations later, we will
1332 // attempt to match it to the corresponding kept section,
1333 // so we leave the input value unchanged here.
61ba1cf9
ILT
1334 continue;
1335 }
eff45813 1336 else if (out_offsets[shndx] == invalid_address)
7bf1f802 1337 {
a9a60db6
ILT
1338 // This is a SHF_MERGE section or one which otherwise
1339 // requires special handling. We get the output address
1340 // of the start of the merged section. If this is not a
1341 // section symbol, we can then determine the final
1342 // value. If it is a section symbol, we can not, as in
1343 // that case we have to consider the addend to determine
1344 // the value to use in a relocation.
a9a60db6 1345 if (!lv.is_section_symbol())
8d32f935
ILT
1346 lv.set_output_value(os->output_address(this, shndx,
1347 lv.input_value()));
a9a60db6
ILT
1348 else
1349 {
8d32f935
ILT
1350 section_offset_type start =
1351 os->starting_output_address(this, shndx);
a9a60db6
ILT
1352 Merged_symbol_value<size>* msv =
1353 new Merged_symbol_value<size>(lv.input_value(), start);
1354 lv.set_merged_symbol_value(msv);
1355 }
7bf1f802
ILT
1356 }
1357 else if (lv.is_tls_symbol())
a9a60db6 1358 lv.set_output_value(os->tls_offset()
ef9beddf 1359 + out_offsets[shndx]
7bf1f802 1360 + lv.input_value());
b8e6aad9 1361 else
a9a60db6 1362 lv.set_output_value(os->address()
ef9beddf 1363 + out_offsets[shndx]
7bf1f802 1364 + lv.input_value());
75f65a3e
ILT
1365 }
1366
7bf1f802
ILT
1367 if (lv.needs_output_symtab_entry())
1368 {
1369 lv.set_output_symtab_index(index);
1370 ++index;
1371 }
1372 }
1373 return index;
1374}
645f8123 1375
7bf1f802 1376// Set the output dynamic symbol table indexes for the local variables.
c06b7b0b 1377
7bf1f802
ILT
1378template<int size, bool big_endian>
1379unsigned int
1380Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1381{
1382 const unsigned int loccount = this->local_symbol_count_;
1383 for (unsigned int i = 1; i < loccount; ++i)
1384 {
1385 Symbol_value<size>& lv(this->local_values_[i]);
1386 if (lv.needs_output_dynsym_entry())
1387 {
1388 lv.set_output_dynsym_index(index);
1389 ++index;
1390 }
75f65a3e 1391 }
7bf1f802
ILT
1392 return index;
1393}
75f65a3e 1394
7bf1f802
ILT
1395// Set the offset where local dynamic symbol information will be stored.
1396// Returns the count of local symbols contributed to the symbol table by
1397// this object.
61ba1cf9 1398
7bf1f802
ILT
1399template<int size, bool big_endian>
1400unsigned int
1401Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1402{
1403 gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1404 this->local_dynsym_offset_ = off;
1405 return this->output_local_dynsym_count_;
75f65a3e
ILT
1406}
1407
61ba1cf9
ILT
1408// Write out the local symbols.
1409
1410template<int size, bool big_endian>
1411void
17a1d0a9
ILT
1412Sized_relobj<size, big_endian>::write_local_symbols(
1413 Output_file* of,
1414 const Stringpool* sympool,
d491d34e
ILT
1415 const Stringpool* dynpool,
1416 Output_symtab_xindex* symtab_xindex,
1417 Output_symtab_xindex* dynsym_xindex)
61ba1cf9 1418{
99e9a495
ILT
1419 const bool strip_all = parameters->options().strip_all();
1420 if (strip_all)
1421 {
1422 if (this->output_local_dynsym_count_ == 0)
1423 return;
1424 this->output_local_symbol_count_ = 0;
1425 }
9e2dcb77 1426
a3ad94ed 1427 gold_assert(this->symtab_shndx_ != -1U);
645f8123 1428 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
1429 {
1430 // This object has no symbols. Weird but legal.
1431 return;
1432 }
1433
1434 // Read the symbol table section header.
645f8123
ILT
1435 const unsigned int symtab_shndx = this->symtab_shndx_;
1436 typename This::Shdr symtabshdr(this,
1437 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 1438 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8 1439 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 1440 gold_assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
1441
1442 // Read the local symbols.
1443 const int sym_size = This::sym_size;
92e059d8 1444 off_t locsize = loccount * sym_size;
61ba1cf9 1445 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
39d0cb0e 1446 locsize, true, false);
61ba1cf9 1447
61ba1cf9 1448 // Read the symbol names.
d491d34e
ILT
1449 const unsigned int strtab_shndx =
1450 this->adjust_shndx(symtabshdr.get_sh_link());
8383303e 1451 section_size_type strtab_size;
645f8123 1452 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57 1453 &strtab_size,
cb295612 1454 false);
61ba1cf9
ILT
1455 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1456
7bf1f802
ILT
1457 // Get views into the output file for the portions of the symbol table
1458 // and the dynamic symbol table that we will be writing.
61ba1cf9 1459 off_t output_size = this->output_local_symbol_count_ * sym_size;
f2619d6c 1460 unsigned char* oview = NULL;
7bf1f802
ILT
1461 if (output_size > 0)
1462 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1463
1464 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1465 unsigned char* dyn_oview = NULL;
1466 if (dyn_output_size > 0)
1467 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1468 dyn_output_size);
61ba1cf9 1469
ef9beddf 1470 const Output_sections out_sections(this->output_sections());
c06b7b0b 1471
a3ad94ed 1472 gold_assert(this->local_values_.size() == loccount);
61ba1cf9 1473
61ba1cf9 1474 unsigned char* ov = oview;
7bf1f802 1475 unsigned char* dyn_ov = dyn_oview;
c06b7b0b 1476 psyms += sym_size;
92e059d8 1477 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
1478 {
1479 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6 1480
d491d34e
ILT
1481 Symbol_value<size>& lv(this->local_values_[i]);
1482
1483 bool is_ordinary;
1484 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1485 &is_ordinary);
1486 if (is_ordinary)
61ba1cf9 1487 {
ef9beddf
ILT
1488 gold_assert(st_shndx < out_sections.size());
1489 if (out_sections[st_shndx] == NULL)
61ba1cf9 1490 continue;
ef9beddf 1491 st_shndx = out_sections[st_shndx]->out_shndx();
d491d34e
ILT
1492 if (st_shndx >= elfcpp::SHN_LORESERVE)
1493 {
99e9a495 1494 if (lv.needs_output_symtab_entry() && !strip_all)
d491d34e
ILT
1495 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1496 if (lv.needs_output_dynsym_entry())
1497 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1498 st_shndx = elfcpp::SHN_XINDEX;
1499 }
61ba1cf9
ILT
1500 }
1501
7bf1f802 1502 // Write the symbol to the output symbol table.
99e9a495 1503 if (!strip_all && lv.needs_output_symtab_entry())
7bf1f802
ILT
1504 {
1505 elfcpp::Sym_write<size, big_endian> osym(ov);
1506
1507 gold_assert(isym.get_st_name() < strtab_size);
1508 const char* name = pnames + isym.get_st_name();
1509 osym.put_st_name(sympool->get_offset(name));
1510 osym.put_st_value(this->local_values_[i].value(this, 0));
1511 osym.put_st_size(isym.get_st_size());
1512 osym.put_st_info(isym.get_st_info());
1513 osym.put_st_other(isym.get_st_other());
1514 osym.put_st_shndx(st_shndx);
1515
1516 ov += sym_size;
1517 }
1518
1519 // Write the symbol to the output dynamic symbol table.
d491d34e 1520 if (lv.needs_output_dynsym_entry())
7bf1f802
ILT
1521 {
1522 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1523 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1524
1525 gold_assert(isym.get_st_name() < strtab_size);
1526 const char* name = pnames + isym.get_st_name();
1527 osym.put_st_name(dynpool->get_offset(name));
1528 osym.put_st_value(this->local_values_[i].value(this, 0));
1529 osym.put_st_size(isym.get_st_size());
1530 osym.put_st_info(isym.get_st_info());
1531 osym.put_st_other(isym.get_st_other());
1532 osym.put_st_shndx(st_shndx);
1533
1534 dyn_ov += sym_size;
1535 }
1536 }
f6ce93d6 1537
61ba1cf9 1538
7bf1f802
ILT
1539 if (output_size > 0)
1540 {
1541 gold_assert(ov - oview == output_size);
1542 of->write_output_view(this->local_symbol_offset_, output_size, oview);
61ba1cf9
ILT
1543 }
1544
7bf1f802
ILT
1545 if (dyn_output_size > 0)
1546 {
1547 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1548 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1549 dyn_oview);
1550 }
61ba1cf9
ILT
1551}
1552
f7e2ee48
ILT
1553// Set *INFO to symbolic information about the offset OFFSET in the
1554// section SHNDX. Return true if we found something, false if we
1555// found nothing.
1556
1557template<int size, bool big_endian>
1558bool
1559Sized_relobj<size, big_endian>::get_symbol_location_info(
1560 unsigned int shndx,
1561 off_t offset,
1562 Symbol_location_info* info)
1563{
1564 if (this->symtab_shndx_ == 0)
1565 return false;
1566
8383303e 1567 section_size_type symbols_size;
f7e2ee48
ILT
1568 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1569 &symbols_size,
1570 false);
1571
d491d34e
ILT
1572 unsigned int symbol_names_shndx =
1573 this->adjust_shndx(this->section_link(this->symtab_shndx_));
8383303e 1574 section_size_type names_size;
f7e2ee48
ILT
1575 const unsigned char* symbol_names_u =
1576 this->section_contents(symbol_names_shndx, &names_size, false);
1577 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1578
1579 const int sym_size = This::sym_size;
1580 const size_t count = symbols_size / sym_size;
1581
1582 const unsigned char* p = symbols;
1583 for (size_t i = 0; i < count; ++i, p += sym_size)
1584 {
1585 elfcpp::Sym<size, big_endian> sym(p);
1586
1587 if (sym.get_st_type() == elfcpp::STT_FILE)
1588 {
1589 if (sym.get_st_name() >= names_size)
1590 info->source_file = "(invalid)";
1591 else
1592 info->source_file = symbol_names + sym.get_st_name();
d491d34e 1593 continue;
f7e2ee48 1594 }
d491d34e
ILT
1595
1596 bool is_ordinary;
1597 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1598 &is_ordinary);
1599 if (is_ordinary
1600 && st_shndx == shndx
1601 && static_cast<off_t>(sym.get_st_value()) <= offset
1602 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1603 > offset))
f7e2ee48
ILT
1604 {
1605 if (sym.get_st_name() > names_size)
1606 info->enclosing_symbol_name = "(invalid)";
1607 else
a2b1aa12
ILT
1608 {
1609 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
086a1841 1610 if (parameters->options().do_demangle())
a2b1aa12
ILT
1611 {
1612 char* demangled_name = cplus_demangle(
1613 info->enclosing_symbol_name.c_str(),
1614 DMGL_ANSI | DMGL_PARAMS);
1615 if (demangled_name != NULL)
1616 {
1617 info->enclosing_symbol_name.assign(demangled_name);
1618 free(demangled_name);
1619 }
1620 }
1621 }
f7e2ee48
ILT
1622 return true;
1623 }
1624 }
1625
1626 return false;
1627}
1628
e94cf127
CC
1629// Look for a kept section corresponding to the given discarded section,
1630// and return its output address. This is used only for relocations in
1631// debugging sections. If we can't find the kept section, return 0.
1632
1633template<int size, bool big_endian>
1634typename Sized_relobj<size, big_endian>::Address
1635Sized_relobj<size, big_endian>::map_to_kept_section(
1636 unsigned int shndx,
1637 bool* found) const
1638{
1639 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1640 if (kept != NULL)
1641 {
1642 gold_assert(kept->object_ != NULL);
1643 *found = true;
ef9beddf
ILT
1644 Output_section* os = kept->object_->output_section(kept->shndx_);
1645 Address offset = kept->object_->get_output_section_offset(kept->shndx_);
eff45813 1646 gold_assert(os != NULL && offset != invalid_address);
ef9beddf 1647 return os->address() + offset;
e94cf127
CC
1648 }
1649 *found = false;
1650 return 0;
1651}
1652
92de84a6
ILT
1653// Get symbol counts.
1654
1655template<int size, bool big_endian>
1656void
1657Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
1658 const Symbol_table*,
1659 size_t* defined,
1660 size_t* used) const
1661{
1662 *defined = this->defined_count_;
1663 size_t count = 0;
1664 for (Symbols::const_iterator p = this->symbols_.begin();
1665 p != this->symbols_.end();
1666 ++p)
1667 if (*p != NULL
1668 && (*p)->source() == Symbol::FROM_OBJECT
1669 && (*p)->object() == this
1670 && (*p)->is_defined())
1671 ++count;
1672 *used = count;
1673}
1674
54dc6425
ILT
1675// Input_objects methods.
1676
008db82e
ILT
1677// Add a regular relocatable object to the list. Return false if this
1678// object should be ignored.
f6ce93d6 1679
008db82e 1680bool
54dc6425
ILT
1681Input_objects::add_object(Object* obj)
1682{
fbfba508 1683 // Set the global target from the first object file we recognize.
019cdb1a 1684 Target* target = obj->target();
8851ecca 1685 if (!parameters->target_valid())
fbfba508 1686 set_parameters_target(target);
8851ecca 1687 else if (target != &parameters->target())
019cdb1a 1688 {
fbfba508 1689 obj->error(_("incompatible target"));
019cdb1a
ILT
1690 return false;
1691 }
1692
c5818ff1
CC
1693 // Print the filename if the -t/--trace option is selected.
1694 if (parameters->options().trace())
1695 gold_info("%s", obj->name().c_str());
1696
008db82e 1697 if (!obj->is_dynamic())
f6ce93d6 1698 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
008db82e
ILT
1699 else
1700 {
1701 // See if this is a duplicate SONAME.
1702 Dynobj* dynobj = static_cast<Dynobj*>(obj);
9a2d6984 1703 const char* soname = dynobj->soname();
008db82e
ILT
1704
1705 std::pair<Unordered_set<std::string>::iterator, bool> ins =
9a2d6984 1706 this->sonames_.insert(soname);
008db82e
ILT
1707 if (!ins.second)
1708 {
1709 // We have already seen a dynamic object with this soname.
1710 return false;
1711 }
1712
1713 this->dynobj_list_.push_back(dynobj);
9a2d6984
ILT
1714
1715 // If this is -lc, remember the directory in which we found it.
1716 // We use this when issuing warnings about undefined symbols: as
1717 // a heuristic, we don't warn about system libraries found in
1718 // the same directory as -lc.
1719 if (strncmp(soname, "libc.so", 7) == 0)
1720 {
1721 const char* object_name = dynobj->name().c_str();
1722 const char* base = lbasename(object_name);
1723 if (base != object_name)
1724 this->system_library_directory_.assign(object_name,
1725 base - 1 - object_name);
1726 }
008db82e 1727 }
75f65a3e 1728
92de84a6
ILT
1729 // Add this object to the cross-referencer if requested.
1730 if (parameters->options().user_set_print_symbol_counts())
1731 {
1732 if (this->cref_ == NULL)
1733 this->cref_ = new Cref();
1734 this->cref_->add_object(obj);
1735 }
1736
008db82e 1737 return true;
54dc6425
ILT
1738}
1739
9a2d6984
ILT
1740// Return whether an object was found in the system library directory.
1741
1742bool
1743Input_objects::found_in_system_library_directory(const Object* object) const
1744{
1745 return (!this->system_library_directory_.empty()
1746 && object->name().compare(0,
1747 this->system_library_directory_.size(),
1748 this->system_library_directory_) == 0);
1749}
1750
e2827e5f
ILT
1751// For each dynamic object, record whether we've seen all of its
1752// explicit dependencies.
1753
1754void
1755Input_objects::check_dynamic_dependencies() const
1756{
1757 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1758 p != this->dynobj_list_.end();
1759 ++p)
1760 {
1761 const Dynobj::Needed& needed((*p)->needed());
1762 bool found_all = true;
1763 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1764 pneeded != needed.end();
1765 ++pneeded)
1766 {
1767 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1768 {
1769 found_all = false;
1770 break;
1771 }
1772 }
1773 (*p)->set_has_unknown_needed_entries(!found_all);
1774 }
1775}
1776
92de84a6
ILT
1777// Start processing an archive.
1778
1779void
1780Input_objects::archive_start(Archive* archive)
1781{
1782 if (parameters->options().user_set_print_symbol_counts())
1783 {
1784 if (this->cref_ == NULL)
1785 this->cref_ = new Cref();
1786 this->cref_->add_archive_start(archive);
1787 }
1788}
1789
1790// Stop processing an archive.
1791
1792void
1793Input_objects::archive_stop(Archive* archive)
1794{
1795 if (parameters->options().user_set_print_symbol_counts())
1796 this->cref_->add_archive_stop(archive);
1797}
1798
1799// Print symbol counts
1800
1801void
1802Input_objects::print_symbol_counts(const Symbol_table* symtab) const
1803{
1804 if (parameters->options().user_set_print_symbol_counts()
1805 && this->cref_ != NULL)
1806 this->cref_->print_symbol_counts(symtab);
1807}
1808
92e059d8
ILT
1809// Relocate_info methods.
1810
1811// Return a string describing the location of a relocation. This is
1812// only used in error messages.
1813
1814template<int size, bool big_endian>
1815std::string
f7e2ee48 1816Relocate_info<size, big_endian>::location(size_t, off_t offset) const
92e059d8 1817{
5c2c6c95
ILT
1818 // See if we can get line-number information from debugging sections.
1819 std::string filename;
1820 std::string file_and_lineno; // Better than filename-only, if available.
4c50553d 1821
a55ce7fe 1822 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
24badc65
ILT
1823 // This will be "" if we failed to parse the debug info for any reason.
1824 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
4c50553d 1825
92e059d8 1826 std::string ret(this->object->name());
f7e2ee48
ILT
1827 ret += ':';
1828 Symbol_location_info info;
1829 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1830 {
1831 ret += " in function ";
1832 ret += info.enclosing_symbol_name;
1833 ret += ":";
5c2c6c95
ILT
1834 filename = info.source_file;
1835 }
1836
1837 if (!file_and_lineno.empty())
1838 ret += file_and_lineno;
1839 else
1840 {
1841 if (!filename.empty())
1842 ret += filename;
1843 ret += "(";
1844 ret += this->object->section_name(this->data_shndx);
1845 char buf[100];
1846 // Offsets into sections have to be positive.
1847 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1848 ret += buf;
1849 ret += ")";
f7e2ee48 1850 }
92e059d8
ILT
1851 return ret;
1852}
1853
bae7f79e
ILT
1854} // End namespace gold.
1855
1856namespace
1857{
1858
1859using namespace gold;
1860
1861// Read an ELF file with the header and return the appropriate
1862// instance of Object.
1863
1864template<int size, bool big_endian>
1865Object*
1866make_elf_sized_object(const std::string& name, Input_file* input_file,
1867 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1868{
1869 int et = ehdr.get_e_type();
bae7f79e
ILT
1870 if (et == elfcpp::ET_REL)
1871 {
f6ce93d6
ILT
1872 Sized_relobj<size, big_endian>* obj =
1873 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
1874 obj->setup(ehdr);
1875 return obj;
1876 }
dbe717ef
ILT
1877 else if (et == elfcpp::ET_DYN)
1878 {
1879 Sized_dynobj<size, big_endian>* obj =
1880 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1881 obj->setup(ehdr);
1882 return obj;
1883 }
bae7f79e
ILT
1884 else
1885 {
75f2446e
ILT
1886 gold_error(_("%s: unsupported ELF file type %d"),
1887 name.c_str(), et);
1888 return NULL;
bae7f79e
ILT
1889 }
1890}
1891
1892} // End anonymous namespace.
1893
1894namespace gold
1895{
1896
1897// Read an ELF file and return the appropriate instance of Object.
1898
1899Object*
1900make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
8383303e 1901 const unsigned char* p, section_offset_type bytes)
bae7f79e
ILT
1902{
1903 if (bytes < elfcpp::EI_NIDENT)
1904 {
75f2446e
ILT
1905 gold_error(_("%s: ELF file too short"), name.c_str());
1906 return NULL;
bae7f79e
ILT
1907 }
1908
1909 int v = p[elfcpp::EI_VERSION];
1910 if (v != elfcpp::EV_CURRENT)
1911 {
1912 if (v == elfcpp::EV_NONE)
75f2446e 1913 gold_error(_("%s: invalid ELF version 0"), name.c_str());
bae7f79e 1914 else
75f2446e
ILT
1915 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1916 return NULL;
bae7f79e
ILT
1917 }
1918
1919 int c = p[elfcpp::EI_CLASS];
1920 if (c == elfcpp::ELFCLASSNONE)
1921 {
75f2446e
ILT
1922 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1923 return NULL;
bae7f79e
ILT
1924 }
1925 else if (c != elfcpp::ELFCLASS32
1926 && c != elfcpp::ELFCLASS64)
1927 {
75f2446e
ILT
1928 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1929 return NULL;
bae7f79e
ILT
1930 }
1931
1932 int d = p[elfcpp::EI_DATA];
1933 if (d == elfcpp::ELFDATANONE)
1934 {
75f2446e
ILT
1935 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1936 return NULL;
bae7f79e
ILT
1937 }
1938 else if (d != elfcpp::ELFDATA2LSB
1939 && d != elfcpp::ELFDATA2MSB)
1940 {
75f2446e
ILT
1941 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1942 return NULL;
bae7f79e
ILT
1943 }
1944
1945 bool big_endian = d == elfcpp::ELFDATA2MSB;
1946
1947 if (c == elfcpp::ELFCLASS32)
1948 {
1949 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1950 {
75f2446e
ILT
1951 gold_error(_("%s: ELF file too short"), name.c_str());
1952 return NULL;
bae7f79e
ILT
1953 }
1954 if (big_endian)
1955 {
193a53d9 1956#ifdef HAVE_TARGET_32_BIG
bae7f79e
ILT
1957 elfcpp::Ehdr<32, true> ehdr(p);
1958 return make_elf_sized_object<32, true>(name, input_file,
1959 offset, ehdr);
193a53d9 1960#else
75f2446e
ILT
1961 gold_error(_("%s: not configured to support "
1962 "32-bit big-endian object"),
1963 name.c_str());
1964 return NULL;
193a53d9 1965#endif
bae7f79e
ILT
1966 }
1967 else
1968 {
193a53d9 1969#ifdef HAVE_TARGET_32_LITTLE
bae7f79e
ILT
1970 elfcpp::Ehdr<32, false> ehdr(p);
1971 return make_elf_sized_object<32, false>(name, input_file,
1972 offset, ehdr);
193a53d9 1973#else
75f2446e
ILT
1974 gold_error(_("%s: not configured to support "
1975 "32-bit little-endian object"),
1976 name.c_str());
1977 return NULL;
193a53d9 1978#endif
bae7f79e
ILT
1979 }
1980 }
1981 else
1982 {
c165fb93 1983 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
bae7f79e 1984 {
75f2446e
ILT
1985 gold_error(_("%s: ELF file too short"), name.c_str());
1986 return NULL;
bae7f79e
ILT
1987 }
1988 if (big_endian)
1989 {
193a53d9 1990#ifdef HAVE_TARGET_64_BIG
bae7f79e
ILT
1991 elfcpp::Ehdr<64, true> ehdr(p);
1992 return make_elf_sized_object<64, true>(name, input_file,
1993 offset, ehdr);
193a53d9 1994#else
75f2446e
ILT
1995 gold_error(_("%s: not configured to support "
1996 "64-bit big-endian object"),
1997 name.c_str());
1998 return NULL;
193a53d9 1999#endif
bae7f79e
ILT
2000 }
2001 else
2002 {
193a53d9 2003#ifdef HAVE_TARGET_64_LITTLE
bae7f79e
ILT
2004 elfcpp::Ehdr<64, false> ehdr(p);
2005 return make_elf_sized_object<64, false>(name, input_file,
2006 offset, ehdr);
193a53d9 2007#else
75f2446e
ILT
2008 gold_error(_("%s: not configured to support "
2009 "64-bit little-endian object"),
2010 name.c_str());
2011 return NULL;
193a53d9 2012#endif
bae7f79e
ILT
2013 }
2014 }
2015}
2016
04bf7072
ILT
2017// Instantiate the templates we need.
2018
2019#ifdef HAVE_TARGET_32_LITTLE
2020template
2021void
2022Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2023 Read_symbols_data*);
2024#endif
2025
2026#ifdef HAVE_TARGET_32_BIG
2027template
2028void
2029Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2030 Read_symbols_data*);
2031#endif
2032
2033#ifdef HAVE_TARGET_64_LITTLE
2034template
2035void
2036Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2037 Read_symbols_data*);
2038#endif
2039
2040#ifdef HAVE_TARGET_64_BIG
2041template
2042void
2043Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2044 Read_symbols_data*);
2045#endif
bae7f79e 2046
193a53d9 2047#ifdef HAVE_TARGET_32_LITTLE
bae7f79e 2048template
f6ce93d6 2049class Sized_relobj<32, false>;
193a53d9 2050#endif
bae7f79e 2051
193a53d9 2052#ifdef HAVE_TARGET_32_BIG
bae7f79e 2053template
f6ce93d6 2054class Sized_relobj<32, true>;
193a53d9 2055#endif
bae7f79e 2056
193a53d9 2057#ifdef HAVE_TARGET_64_LITTLE
bae7f79e 2058template
f6ce93d6 2059class Sized_relobj<64, false>;
193a53d9 2060#endif
bae7f79e 2061
193a53d9 2062#ifdef HAVE_TARGET_64_BIG
bae7f79e 2063template
f6ce93d6 2064class Sized_relobj<64, true>;
193a53d9 2065#endif
bae7f79e 2066
193a53d9 2067#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
2068template
2069struct Relocate_info<32, false>;
193a53d9 2070#endif
92e059d8 2071
193a53d9 2072#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
2073template
2074struct Relocate_info<32, true>;
193a53d9 2075#endif
92e059d8 2076
193a53d9 2077#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
2078template
2079struct Relocate_info<64, false>;
193a53d9 2080#endif
92e059d8 2081
193a53d9 2082#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
2083template
2084struct Relocate_info<64, true>;
193a53d9 2085#endif
92e059d8 2086
bae7f79e 2087} // End namespace gold.
This page took 0.212025 seconds and 4 git commands to generate.