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