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