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