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