2009-01-15 Andrew Stubbs <ams@codesourcery.com>
[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{
8851ecca
ILT
1419 if (parameters->options().strip_all()
1420 && this->output_local_dynsym_count_ == 0)
9e2dcb77
ILT
1421 return;
1422
a3ad94ed 1423 gold_assert(this->symtab_shndx_ != -1U);
645f8123 1424 if (this->symtab_shndx_ == 0)
61ba1cf9
ILT
1425 {
1426 // This object has no symbols. Weird but legal.
1427 return;
1428 }
1429
1430 // Read the symbol table section header.
645f8123
ILT
1431 const unsigned int symtab_shndx = this->symtab_shndx_;
1432 typename This::Shdr symtabshdr(this,
1433 this->elf_file_.section_header(symtab_shndx));
a3ad94ed 1434 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8 1435 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 1436 gold_assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
1437
1438 // Read the local symbols.
1439 const int sym_size = This::sym_size;
92e059d8 1440 off_t locsize = loccount * sym_size;
61ba1cf9 1441 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
39d0cb0e 1442 locsize, true, false);
61ba1cf9 1443
61ba1cf9 1444 // Read the symbol names.
d491d34e
ILT
1445 const unsigned int strtab_shndx =
1446 this->adjust_shndx(symtabshdr.get_sh_link());
8383303e 1447 section_size_type strtab_size;
645f8123 1448 const unsigned char* pnamesu = this->section_contents(strtab_shndx,
9eb9fa57 1449 &strtab_size,
cb295612 1450 false);
61ba1cf9
ILT
1451 const char* pnames = reinterpret_cast<const char*>(pnamesu);
1452
7bf1f802
ILT
1453 // Get views into the output file for the portions of the symbol table
1454 // and the dynamic symbol table that we will be writing.
61ba1cf9 1455 off_t output_size = this->output_local_symbol_count_ * sym_size;
f2619d6c 1456 unsigned char* oview = NULL;
7bf1f802
ILT
1457 if (output_size > 0)
1458 oview = of->get_output_view(this->local_symbol_offset_, output_size);
1459
1460 off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1461 unsigned char* dyn_oview = NULL;
1462 if (dyn_output_size > 0)
1463 dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1464 dyn_output_size);
61ba1cf9 1465
ef9beddf 1466 const Output_sections out_sections(this->output_sections());
c06b7b0b 1467
a3ad94ed 1468 gold_assert(this->local_values_.size() == loccount);
61ba1cf9 1469
61ba1cf9 1470 unsigned char* ov = oview;
7bf1f802 1471 unsigned char* dyn_ov = dyn_oview;
c06b7b0b 1472 psyms += sym_size;
92e059d8 1473 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
1474 {
1475 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6 1476
d491d34e
ILT
1477 Symbol_value<size>& lv(this->local_values_[i]);
1478
1479 bool is_ordinary;
1480 unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1481 &is_ordinary);
1482 if (is_ordinary)
61ba1cf9 1483 {
ef9beddf
ILT
1484 gold_assert(st_shndx < out_sections.size());
1485 if (out_sections[st_shndx] == NULL)
61ba1cf9 1486 continue;
ef9beddf 1487 st_shndx = out_sections[st_shndx]->out_shndx();
d491d34e
ILT
1488 if (st_shndx >= elfcpp::SHN_LORESERVE)
1489 {
1490 if (lv.needs_output_symtab_entry())
1491 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1492 if (lv.needs_output_dynsym_entry())
1493 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1494 st_shndx = elfcpp::SHN_XINDEX;
1495 }
61ba1cf9
ILT
1496 }
1497
7bf1f802 1498 // Write the symbol to the output symbol table.
8851ecca 1499 if (!parameters->options().strip_all()
d491d34e 1500 && lv.needs_output_symtab_entry())
7bf1f802
ILT
1501 {
1502 elfcpp::Sym_write<size, big_endian> osym(ov);
1503
1504 gold_assert(isym.get_st_name() < strtab_size);
1505 const char* name = pnames + isym.get_st_name();
1506 osym.put_st_name(sympool->get_offset(name));
1507 osym.put_st_value(this->local_values_[i].value(this, 0));
1508 osym.put_st_size(isym.get_st_size());
1509 osym.put_st_info(isym.get_st_info());
1510 osym.put_st_other(isym.get_st_other());
1511 osym.put_st_shndx(st_shndx);
1512
1513 ov += sym_size;
1514 }
1515
1516 // Write the symbol to the output dynamic symbol table.
d491d34e 1517 if (lv.needs_output_dynsym_entry())
7bf1f802
ILT
1518 {
1519 gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1520 elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1521
1522 gold_assert(isym.get_st_name() < strtab_size);
1523 const char* name = pnames + isym.get_st_name();
1524 osym.put_st_name(dynpool->get_offset(name));
1525 osym.put_st_value(this->local_values_[i].value(this, 0));
1526 osym.put_st_size(isym.get_st_size());
1527 osym.put_st_info(isym.get_st_info());
1528 osym.put_st_other(isym.get_st_other());
1529 osym.put_st_shndx(st_shndx);
1530
1531 dyn_ov += sym_size;
1532 }
1533 }
f6ce93d6 1534
61ba1cf9 1535
7bf1f802
ILT
1536 if (output_size > 0)
1537 {
1538 gold_assert(ov - oview == output_size);
1539 of->write_output_view(this->local_symbol_offset_, output_size, oview);
61ba1cf9
ILT
1540 }
1541
7bf1f802
ILT
1542 if (dyn_output_size > 0)
1543 {
1544 gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1545 of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1546 dyn_oview);
1547 }
61ba1cf9
ILT
1548}
1549
f7e2ee48
ILT
1550// Set *INFO to symbolic information about the offset OFFSET in the
1551// section SHNDX. Return true if we found something, false if we
1552// found nothing.
1553
1554template<int size, bool big_endian>
1555bool
1556Sized_relobj<size, big_endian>::get_symbol_location_info(
1557 unsigned int shndx,
1558 off_t offset,
1559 Symbol_location_info* info)
1560{
1561 if (this->symtab_shndx_ == 0)
1562 return false;
1563
8383303e 1564 section_size_type symbols_size;
f7e2ee48
ILT
1565 const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1566 &symbols_size,
1567 false);
1568
d491d34e
ILT
1569 unsigned int symbol_names_shndx =
1570 this->adjust_shndx(this->section_link(this->symtab_shndx_));
8383303e 1571 section_size_type names_size;
f7e2ee48
ILT
1572 const unsigned char* symbol_names_u =
1573 this->section_contents(symbol_names_shndx, &names_size, false);
1574 const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1575
1576 const int sym_size = This::sym_size;
1577 const size_t count = symbols_size / sym_size;
1578
1579 const unsigned char* p = symbols;
1580 for (size_t i = 0; i < count; ++i, p += sym_size)
1581 {
1582 elfcpp::Sym<size, big_endian> sym(p);
1583
1584 if (sym.get_st_type() == elfcpp::STT_FILE)
1585 {
1586 if (sym.get_st_name() >= names_size)
1587 info->source_file = "(invalid)";
1588 else
1589 info->source_file = symbol_names + sym.get_st_name();
d491d34e 1590 continue;
f7e2ee48 1591 }
d491d34e
ILT
1592
1593 bool is_ordinary;
1594 unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1595 &is_ordinary);
1596 if (is_ordinary
1597 && st_shndx == shndx
1598 && static_cast<off_t>(sym.get_st_value()) <= offset
1599 && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1600 > offset))
f7e2ee48
ILT
1601 {
1602 if (sym.get_st_name() > names_size)
1603 info->enclosing_symbol_name = "(invalid)";
1604 else
a2b1aa12
ILT
1605 {
1606 info->enclosing_symbol_name = symbol_names + sym.get_st_name();
086a1841 1607 if (parameters->options().do_demangle())
a2b1aa12
ILT
1608 {
1609 char* demangled_name = cplus_demangle(
1610 info->enclosing_symbol_name.c_str(),
1611 DMGL_ANSI | DMGL_PARAMS);
1612 if (demangled_name != NULL)
1613 {
1614 info->enclosing_symbol_name.assign(demangled_name);
1615 free(demangled_name);
1616 }
1617 }
1618 }
f7e2ee48
ILT
1619 return true;
1620 }
1621 }
1622
1623 return false;
1624}
1625
e94cf127
CC
1626// Look for a kept section corresponding to the given discarded section,
1627// and return its output address. This is used only for relocations in
1628// debugging sections. If we can't find the kept section, return 0.
1629
1630template<int size, bool big_endian>
1631typename Sized_relobj<size, big_endian>::Address
1632Sized_relobj<size, big_endian>::map_to_kept_section(
1633 unsigned int shndx,
1634 bool* found) const
1635{
1636 Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1637 if (kept != NULL)
1638 {
1639 gold_assert(kept->object_ != NULL);
1640 *found = true;
ef9beddf
ILT
1641 Output_section* os = kept->object_->output_section(kept->shndx_);
1642 Address offset = kept->object_->get_output_section_offset(kept->shndx_);
eff45813 1643 gold_assert(os != NULL && offset != invalid_address);
ef9beddf 1644 return os->address() + offset;
e94cf127
CC
1645 }
1646 *found = false;
1647 return 0;
1648}
1649
92de84a6
ILT
1650// Get symbol counts.
1651
1652template<int size, bool big_endian>
1653void
1654Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
1655 const Symbol_table*,
1656 size_t* defined,
1657 size_t* used) const
1658{
1659 *defined = this->defined_count_;
1660 size_t count = 0;
1661 for (Symbols::const_iterator p = this->symbols_.begin();
1662 p != this->symbols_.end();
1663 ++p)
1664 if (*p != NULL
1665 && (*p)->source() == Symbol::FROM_OBJECT
1666 && (*p)->object() == this
1667 && (*p)->is_defined())
1668 ++count;
1669 *used = count;
1670}
1671
54dc6425
ILT
1672// Input_objects methods.
1673
008db82e
ILT
1674// Add a regular relocatable object to the list. Return false if this
1675// object should be ignored.
f6ce93d6 1676
008db82e 1677bool
54dc6425
ILT
1678Input_objects::add_object(Object* obj)
1679{
fbfba508 1680 // Set the global target from the first object file we recognize.
019cdb1a 1681 Target* target = obj->target();
8851ecca 1682 if (!parameters->target_valid())
fbfba508 1683 set_parameters_target(target);
8851ecca 1684 else if (target != &parameters->target())
019cdb1a 1685 {
fbfba508 1686 obj->error(_("incompatible target"));
019cdb1a
ILT
1687 return false;
1688 }
1689
c5818ff1
CC
1690 // Print the filename if the -t/--trace option is selected.
1691 if (parameters->options().trace())
1692 gold_info("%s", obj->name().c_str());
1693
008db82e 1694 if (!obj->is_dynamic())
f6ce93d6 1695 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
008db82e
ILT
1696 else
1697 {
1698 // See if this is a duplicate SONAME.
1699 Dynobj* dynobj = static_cast<Dynobj*>(obj);
9a2d6984 1700 const char* soname = dynobj->soname();
008db82e
ILT
1701
1702 std::pair<Unordered_set<std::string>::iterator, bool> ins =
9a2d6984 1703 this->sonames_.insert(soname);
008db82e
ILT
1704 if (!ins.second)
1705 {
1706 // We have already seen a dynamic object with this soname.
1707 return false;
1708 }
1709
1710 this->dynobj_list_.push_back(dynobj);
9a2d6984
ILT
1711
1712 // If this is -lc, remember the directory in which we found it.
1713 // We use this when issuing warnings about undefined symbols: as
1714 // a heuristic, we don't warn about system libraries found in
1715 // the same directory as -lc.
1716 if (strncmp(soname, "libc.so", 7) == 0)
1717 {
1718 const char* object_name = dynobj->name().c_str();
1719 const char* base = lbasename(object_name);
1720 if (base != object_name)
1721 this->system_library_directory_.assign(object_name,
1722 base - 1 - object_name);
1723 }
008db82e 1724 }
75f65a3e 1725
92de84a6
ILT
1726 // Add this object to the cross-referencer if requested.
1727 if (parameters->options().user_set_print_symbol_counts())
1728 {
1729 if (this->cref_ == NULL)
1730 this->cref_ = new Cref();
1731 this->cref_->add_object(obj);
1732 }
1733
008db82e 1734 return true;
54dc6425
ILT
1735}
1736
9a2d6984
ILT
1737// Return whether an object was found in the system library directory.
1738
1739bool
1740Input_objects::found_in_system_library_directory(const Object* object) const
1741{
1742 return (!this->system_library_directory_.empty()
1743 && object->name().compare(0,
1744 this->system_library_directory_.size(),
1745 this->system_library_directory_) == 0);
1746}
1747
e2827e5f
ILT
1748// For each dynamic object, record whether we've seen all of its
1749// explicit dependencies.
1750
1751void
1752Input_objects::check_dynamic_dependencies() const
1753{
1754 for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1755 p != this->dynobj_list_.end();
1756 ++p)
1757 {
1758 const Dynobj::Needed& needed((*p)->needed());
1759 bool found_all = true;
1760 for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1761 pneeded != needed.end();
1762 ++pneeded)
1763 {
1764 if (this->sonames_.find(*pneeded) == this->sonames_.end())
1765 {
1766 found_all = false;
1767 break;
1768 }
1769 }
1770 (*p)->set_has_unknown_needed_entries(!found_all);
1771 }
1772}
1773
92de84a6
ILT
1774// Start processing an archive.
1775
1776void
1777Input_objects::archive_start(Archive* archive)
1778{
1779 if (parameters->options().user_set_print_symbol_counts())
1780 {
1781 if (this->cref_ == NULL)
1782 this->cref_ = new Cref();
1783 this->cref_->add_archive_start(archive);
1784 }
1785}
1786
1787// Stop processing an archive.
1788
1789void
1790Input_objects::archive_stop(Archive* archive)
1791{
1792 if (parameters->options().user_set_print_symbol_counts())
1793 this->cref_->add_archive_stop(archive);
1794}
1795
1796// Print symbol counts
1797
1798void
1799Input_objects::print_symbol_counts(const Symbol_table* symtab) const
1800{
1801 if (parameters->options().user_set_print_symbol_counts()
1802 && this->cref_ != NULL)
1803 this->cref_->print_symbol_counts(symtab);
1804}
1805
92e059d8
ILT
1806// Relocate_info methods.
1807
1808// Return a string describing the location of a relocation. This is
1809// only used in error messages.
1810
1811template<int size, bool big_endian>
1812std::string
f7e2ee48 1813Relocate_info<size, big_endian>::location(size_t, off_t offset) const
92e059d8 1814{
5c2c6c95
ILT
1815 // See if we can get line-number information from debugging sections.
1816 std::string filename;
1817 std::string file_and_lineno; // Better than filename-only, if available.
4c50553d 1818
a55ce7fe 1819 Sized_dwarf_line_info<size, big_endian> line_info(this->object);
24badc65
ILT
1820 // This will be "" if we failed to parse the debug info for any reason.
1821 file_and_lineno = line_info.addr2line(this->data_shndx, offset);
4c50553d 1822
92e059d8 1823 std::string ret(this->object->name());
f7e2ee48
ILT
1824 ret += ':';
1825 Symbol_location_info info;
1826 if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1827 {
1828 ret += " in function ";
1829 ret += info.enclosing_symbol_name;
1830 ret += ":";
5c2c6c95
ILT
1831 filename = info.source_file;
1832 }
1833
1834 if (!file_and_lineno.empty())
1835 ret += file_and_lineno;
1836 else
1837 {
1838 if (!filename.empty())
1839 ret += filename;
1840 ret += "(";
1841 ret += this->object->section_name(this->data_shndx);
1842 char buf[100];
1843 // Offsets into sections have to be positive.
1844 snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1845 ret += buf;
1846 ret += ")";
f7e2ee48 1847 }
92e059d8
ILT
1848 return ret;
1849}
1850
bae7f79e
ILT
1851} // End namespace gold.
1852
1853namespace
1854{
1855
1856using namespace gold;
1857
1858// Read an ELF file with the header and return the appropriate
1859// instance of Object.
1860
1861template<int size, bool big_endian>
1862Object*
1863make_elf_sized_object(const std::string& name, Input_file* input_file,
1864 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1865{
1866 int et = ehdr.get_e_type();
bae7f79e
ILT
1867 if (et == elfcpp::ET_REL)
1868 {
f6ce93d6
ILT
1869 Sized_relobj<size, big_endian>* obj =
1870 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
1871 obj->setup(ehdr);
1872 return obj;
1873 }
dbe717ef
ILT
1874 else if (et == elfcpp::ET_DYN)
1875 {
1876 Sized_dynobj<size, big_endian>* obj =
1877 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1878 obj->setup(ehdr);
1879 return obj;
1880 }
bae7f79e
ILT
1881 else
1882 {
75f2446e
ILT
1883 gold_error(_("%s: unsupported ELF file type %d"),
1884 name.c_str(), et);
1885 return NULL;
bae7f79e
ILT
1886 }
1887}
1888
1889} // End anonymous namespace.
1890
1891namespace gold
1892{
1893
1894// Read an ELF file and return the appropriate instance of Object.
1895
1896Object*
1897make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
8383303e 1898 const unsigned char* p, section_offset_type bytes)
bae7f79e
ILT
1899{
1900 if (bytes < elfcpp::EI_NIDENT)
1901 {
75f2446e
ILT
1902 gold_error(_("%s: ELF file too short"), name.c_str());
1903 return NULL;
bae7f79e
ILT
1904 }
1905
1906 int v = p[elfcpp::EI_VERSION];
1907 if (v != elfcpp::EV_CURRENT)
1908 {
1909 if (v == elfcpp::EV_NONE)
75f2446e 1910 gold_error(_("%s: invalid ELF version 0"), name.c_str());
bae7f79e 1911 else
75f2446e
ILT
1912 gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1913 return NULL;
bae7f79e
ILT
1914 }
1915
1916 int c = p[elfcpp::EI_CLASS];
1917 if (c == elfcpp::ELFCLASSNONE)
1918 {
75f2446e
ILT
1919 gold_error(_("%s: invalid ELF class 0"), name.c_str());
1920 return NULL;
bae7f79e
ILT
1921 }
1922 else if (c != elfcpp::ELFCLASS32
1923 && c != elfcpp::ELFCLASS64)
1924 {
75f2446e
ILT
1925 gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1926 return NULL;
bae7f79e
ILT
1927 }
1928
1929 int d = p[elfcpp::EI_DATA];
1930 if (d == elfcpp::ELFDATANONE)
1931 {
75f2446e
ILT
1932 gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1933 return NULL;
bae7f79e
ILT
1934 }
1935 else if (d != elfcpp::ELFDATA2LSB
1936 && d != elfcpp::ELFDATA2MSB)
1937 {
75f2446e
ILT
1938 gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1939 return NULL;
bae7f79e
ILT
1940 }
1941
1942 bool big_endian = d == elfcpp::ELFDATA2MSB;
1943
1944 if (c == elfcpp::ELFCLASS32)
1945 {
1946 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1947 {
75f2446e
ILT
1948 gold_error(_("%s: ELF file too short"), name.c_str());
1949 return NULL;
bae7f79e
ILT
1950 }
1951 if (big_endian)
1952 {
193a53d9 1953#ifdef HAVE_TARGET_32_BIG
bae7f79e
ILT
1954 elfcpp::Ehdr<32, true> ehdr(p);
1955 return make_elf_sized_object<32, true>(name, input_file,
1956 offset, ehdr);
193a53d9 1957#else
75f2446e
ILT
1958 gold_error(_("%s: not configured to support "
1959 "32-bit big-endian object"),
1960 name.c_str());
1961 return NULL;
193a53d9 1962#endif
bae7f79e
ILT
1963 }
1964 else
1965 {
193a53d9 1966#ifdef HAVE_TARGET_32_LITTLE
bae7f79e
ILT
1967 elfcpp::Ehdr<32, false> ehdr(p);
1968 return make_elf_sized_object<32, false>(name, input_file,
1969 offset, ehdr);
193a53d9 1970#else
75f2446e
ILT
1971 gold_error(_("%s: not configured to support "
1972 "32-bit little-endian object"),
1973 name.c_str());
1974 return NULL;
193a53d9 1975#endif
bae7f79e
ILT
1976 }
1977 }
1978 else
1979 {
c165fb93 1980 if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
bae7f79e 1981 {
75f2446e
ILT
1982 gold_error(_("%s: ELF file too short"), name.c_str());
1983 return NULL;
bae7f79e
ILT
1984 }
1985 if (big_endian)
1986 {
193a53d9 1987#ifdef HAVE_TARGET_64_BIG
bae7f79e
ILT
1988 elfcpp::Ehdr<64, true> ehdr(p);
1989 return make_elf_sized_object<64, true>(name, input_file,
1990 offset, ehdr);
193a53d9 1991#else
75f2446e
ILT
1992 gold_error(_("%s: not configured to support "
1993 "64-bit big-endian object"),
1994 name.c_str());
1995 return NULL;
193a53d9 1996#endif
bae7f79e
ILT
1997 }
1998 else
1999 {
193a53d9 2000#ifdef HAVE_TARGET_64_LITTLE
bae7f79e
ILT
2001 elfcpp::Ehdr<64, false> ehdr(p);
2002 return make_elf_sized_object<64, false>(name, input_file,
2003 offset, ehdr);
193a53d9 2004#else
75f2446e
ILT
2005 gold_error(_("%s: not configured to support "
2006 "64-bit little-endian object"),
2007 name.c_str());
2008 return NULL;
193a53d9 2009#endif
bae7f79e
ILT
2010 }
2011 }
2012}
2013
04bf7072
ILT
2014// Instantiate the templates we need.
2015
2016#ifdef HAVE_TARGET_32_LITTLE
2017template
2018void
2019Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2020 Read_symbols_data*);
2021#endif
2022
2023#ifdef HAVE_TARGET_32_BIG
2024template
2025void
2026Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2027 Read_symbols_data*);
2028#endif
2029
2030#ifdef HAVE_TARGET_64_LITTLE
2031template
2032void
2033Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2034 Read_symbols_data*);
2035#endif
2036
2037#ifdef HAVE_TARGET_64_BIG
2038template
2039void
2040Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2041 Read_symbols_data*);
2042#endif
bae7f79e 2043
193a53d9 2044#ifdef HAVE_TARGET_32_LITTLE
bae7f79e 2045template
f6ce93d6 2046class Sized_relobj<32, false>;
193a53d9 2047#endif
bae7f79e 2048
193a53d9 2049#ifdef HAVE_TARGET_32_BIG
bae7f79e 2050template
f6ce93d6 2051class Sized_relobj<32, true>;
193a53d9 2052#endif
bae7f79e 2053
193a53d9 2054#ifdef HAVE_TARGET_64_LITTLE
bae7f79e 2055template
f6ce93d6 2056class Sized_relobj<64, false>;
193a53d9 2057#endif
bae7f79e 2058
193a53d9 2059#ifdef HAVE_TARGET_64_BIG
bae7f79e 2060template
f6ce93d6 2061class Sized_relobj<64, true>;
193a53d9 2062#endif
bae7f79e 2063
193a53d9 2064#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
2065template
2066struct Relocate_info<32, false>;
193a53d9 2067#endif
92e059d8 2068
193a53d9 2069#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
2070template
2071struct Relocate_info<32, true>;
193a53d9 2072#endif
92e059d8 2073
193a53d9 2074#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
2075template
2076struct Relocate_info<64, false>;
193a53d9 2077#endif
92e059d8 2078
193a53d9 2079#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
2080template
2081struct Relocate_info<64, true>;
193a53d9 2082#endif
92e059d8 2083
bae7f79e 2084} // End namespace gold.
This page took 0.216751 seconds and 4 git commands to generate.