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