Rework stringpool and hash tables so that we always generate the same
[deliverable/binutils-gdb.git] / gold / object.cc
CommitLineData
bae7f79e
ILT
1// object.cc -- support for an object file for linking in gold
2
3#include "gold.h"
4
5#include <cerrno>
6#include <cstring>
7#include <cassert>
8
14bfc3f5 9#include "target-select.h"
a2fb1b05 10#include "layout.h"
61ba1cf9 11#include "output.h"
f6ce93d6
ILT
12#include "symtab.h"
13#include "object.h"
14#include "dynobj.h"
bae7f79e
ILT
15
16namespace gold
17{
18
f6ce93d6 19// Class Sized_relobj.
bae7f79e
ILT
20
21template<int size, bool big_endian>
f6ce93d6 22Sized_relobj<size, big_endian>::Sized_relobj(
bae7f79e
ILT
23 const std::string& name,
24 Input_file* input_file,
25 off_t offset,
26 const elfcpp::Ehdr<size, big_endian>& ehdr)
f6ce93d6 27 : Relobj(name, input_file, offset),
12e14209 28 section_headers_(NULL),
bae7f79e 29 flags_(ehdr.get_e_flags()),
bae7f79e 30 shoff_(ehdr.get_e_shoff()),
bae7f79e 31 shstrndx_(0),
14bfc3f5 32 symtab_shnum_(0),
61ba1cf9
ILT
33 local_symbol_count_(0),
34 output_local_symbol_count_(0),
75f65a3e 35 symbols_(NULL),
61ba1cf9
ILT
36 local_symbol_offset_(0),
37 values_(NULL)
bae7f79e 38{
a2fb1b05 39 if (ehdr.get_e_ehsize() != This::ehdr_size)
bae7f79e
ILT
40 {
41 fprintf(stderr, _("%s: %s: bad e_ehsize field (%d != %d)\n"),
42 program_name, this->name().c_str(), ehdr.get_e_ehsize(),
a2fb1b05 43 This::ehdr_size);
bae7f79e
ILT
44 gold_exit(false);
45 }
a2fb1b05 46 if (ehdr.get_e_shentsize() != This::shdr_size)
bae7f79e
ILT
47 {
48 fprintf(stderr, _("%s: %s: bad e_shentsize field (%d != %d)\n"),
49 program_name, this->name().c_str(), ehdr.get_e_shentsize(),
a2fb1b05 50 This::shdr_size);
bae7f79e
ILT
51 gold_exit(false);
52 }
53}
54
55template<int size, bool big_endian>
f6ce93d6 56Sized_relobj<size, big_endian>::~Sized_relobj()
bae7f79e
ILT
57{
58}
59
75f65a3e
ILT
60// Read the section header for section SHNUM.
61
62template<int size, bool big_endian>
ead1e424 63inline const unsigned char*
f6ce93d6 64Sized_relobj<size, big_endian>::section_header(unsigned int shnum)
75f65a3e 65{
61ba1cf9 66 assert(shnum < this->shnum());
75f65a3e
ILT
67 off_t symtabshdroff = this->shoff_ + shnum * This::shdr_size;
68 return this->get_view(symtabshdroff, This::shdr_size);
69}
70
f6ce93d6
ILT
71// Return the name of section SHNUM. The object must already be
72// locked.
92e059d8
ILT
73
74template<int size, bool big_endian>
75std::string
f6ce93d6 76Sized_relobj<size, big_endian>::do_section_name(unsigned int shnum)
92e059d8 77{
92e059d8
ILT
78 // Read the section names.
79 typename This::Shdr shdrnames(this->section_header(this->shstrndx_));
80 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
81 shdrnames.get_sh_size());
82 const char* pnames = reinterpret_cast<const char*>(pnamesu);
83
84 typename This::Shdr shdr(this->section_header(shnum));
85 if (shdr.get_sh_name() >= shdrnames.get_sh_size())
86 {
87 fprintf(stderr,
88 _("%s: %s: bad section name offset for section %u: %lu\n"),
89 program_name, this->name().c_str(), shnum,
90 static_cast<unsigned long>(shdr.get_sh_name()));
91 gold_exit(false);
92 }
93
94 return std::string(pnames + shdr.get_sh_name());
95}
96
f6ce93d6
ILT
97// Return a view of the contents of section SHNUM. The object does
98// not have to be locked.
99
100template<int size, bool big_endian>
101const unsigned char*
102Sized_relobj<size, big_endian>::do_section_contents(unsigned int shnum,
103 off_t* plen)
104{
105 Task_locker_obj<Object> tl(*this);
106
107 typename This::Shdr shdr(this->section_header(shnum));
108 *plen = shdr.get_sh_size();
109 return this->get_view(shdr.get_sh_offset(), shdr.get_sh_size());
110}
111
bae7f79e
ILT
112// Set up an object file bsaed on the file header. This sets up the
113// target and reads the section information.
114
115template<int size, bool big_endian>
116void
f6ce93d6 117Sized_relobj<size, big_endian>::setup(
bae7f79e
ILT
118 const elfcpp::Ehdr<size, big_endian>& ehdr)
119{
a2fb1b05
ILT
120 int machine = ehdr.get_e_machine();
121 Target* target = select_target(machine, size, big_endian,
122 ehdr.get_e_ident()[elfcpp::EI_OSABI],
123 ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
14bfc3f5
ILT
124 if (target == NULL)
125 {
126 fprintf(stderr, _("%s: %s: unsupported ELF machine number %d\n"),
a2fb1b05 127 program_name, this->name().c_str(), machine);
14bfc3f5
ILT
128 gold_exit(false);
129 }
130 this->set_target(target);
12e14209 131
bae7f79e
ILT
132 unsigned int shnum = ehdr.get_e_shnum();
133 unsigned int shstrndx = ehdr.get_e_shstrndx();
134 if ((shnum == 0 || shstrndx == elfcpp::SHN_XINDEX)
135 && this->shoff_ != 0)
136 {
75f65a3e 137 typename This::Shdr shdr(this->section_header(0));
bae7f79e
ILT
138 if (shnum == 0)
139 shnum = shdr.get_sh_size();
140 if (shstrndx == elfcpp::SHN_XINDEX)
141 shstrndx = shdr.get_sh_link();
142 }
a2fb1b05 143 this->set_shnum(shnum);
bae7f79e
ILT
144 this->shstrndx_ = shstrndx;
145
146 if (shnum == 0)
147 return;
148
12e14209
ILT
149 // We store the section headers in a File_view until do_read_symbols.
150 this->section_headers_ = this->get_lasting_view(this->shoff_,
151 shnum * This::shdr_size);
152
153 // Find the SHT_SYMTAB section. The ELF standard says that maybe in
154 // the future there can be more than one SHT_SYMTAB section. Until
155 // somebody figures out how that could work, we assume there is only
156 // one.
157 const unsigned char* p = this->section_headers_->data();
158
bae7f79e 159 // Skip the first section, which is always empty.
a2fb1b05 160 p += This::shdr_size;
12e14209 161 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
bae7f79e 162 {
75f65a3e 163 typename This::Shdr shdr(p);
bae7f79e
ILT
164 if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
165 {
166 this->symtab_shnum_ = i;
167 break;
168 }
bae7f79e
ILT
169 }
170}
171
12e14209 172// Read the sections and symbols from an object file.
bae7f79e
ILT
173
174template<int size, bool big_endian>
12e14209 175void
f6ce93d6 176Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
bae7f79e 177{
12e14209
ILT
178 // Transfer our view of the section headers to SD.
179 sd->section_headers = this->section_headers_;
180 this->section_headers_ = NULL;
181
182 // Read the section names.
183 const unsigned char* pshdrs = sd->section_headers->data();
184 const unsigned char* pshdrnames = pshdrs + this->shstrndx_ * This::shdr_size;
185 typename This::Shdr shdrnames(pshdrnames);
186 sd->section_names_size = shdrnames.get_sh_size();
187 sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
188 sd->section_names_size);
189
bae7f79e
ILT
190 if (this->symtab_shnum_ == 0)
191 {
192 // No symbol table. Weird but legal.
12e14209
ILT
193 sd->symbols = NULL;
194 sd->symbols_size = 0;
195 sd->symbol_names = NULL;
196 sd->symbol_names_size = 0;
197 return;
bae7f79e
ILT
198 }
199
12e14209
ILT
200 // Get the symbol table section header.
201 typename This::Shdr symtabshdr(pshdrs
202 + this->symtab_shnum_ * This::shdr_size);
bae7f79e
ILT
203 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
204
75f65a3e
ILT
205 // We only need the external symbols.
206 const int sym_size = This::sym_size;
92e059d8
ILT
207 const unsigned int loccount = symtabshdr.get_sh_info();
208 this->local_symbol_count_ = loccount;
209 off_t locsize = loccount * sym_size;
75f65a3e
ILT
210 off_t extoff = symtabshdr.get_sh_offset() + locsize;
211 off_t extsize = symtabshdr.get_sh_size() - locsize;
212
bae7f79e 213 // Read the symbol table.
75f65a3e 214 File_view* fvsymtab = this->get_lasting_view(extoff, extsize);
bae7f79e
ILT
215
216 // Read the section header for the symbol names.
92e059d8 217 unsigned int shnum = this->shnum();
bae7f79e 218 unsigned int strtab_shnum = symtabshdr.get_sh_link();
92e059d8 219 if (strtab_shnum == 0 || strtab_shnum >= shnum)
bae7f79e
ILT
220 {
221 fprintf(stderr, _("%s: %s: invalid symbol table name index: %u\n"),
222 program_name, this->name().c_str(), strtab_shnum);
223 gold_exit(false);
224 }
92e059d8 225 typename This::Shdr strtabshdr(pshdrs + strtab_shnum * This::shdr_size);
bae7f79e
ILT
226 if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
227 {
228 fprintf(stderr,
229 _("%s: %s: symbol table name section has wrong type: %u\n"),
230 program_name, this->name().c_str(),
231 static_cast<unsigned int>(strtabshdr.get_sh_type()));
232 gold_exit(false);
233 }
234
235 // Read the symbol names.
236 File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
237 strtabshdr.get_sh_size());
238
12e14209
ILT
239 sd->symbols = fvsymtab;
240 sd->symbols_size = extsize;
241 sd->symbol_names = fvstrtab;
242 sd->symbol_names_size = strtabshdr.get_sh_size();
a2fb1b05
ILT
243}
244
245// Return whether to include a section group in the link. LAYOUT is
246// used to keep track of which section groups we have already seen.
247// INDEX is the index of the section group and SHDR is the section
248// header. If we do not want to include this group, we set bits in
249// OMIT for each section which should be discarded.
250
251template<int size, bool big_endian>
252bool
f6ce93d6 253Sized_relobj<size, big_endian>::include_section_group(
a2fb1b05
ILT
254 Layout* layout,
255 unsigned int index,
256 const elfcpp::Shdr<size, big_endian>& shdr,
257 std::vector<bool>* omit)
258{
259 // Read the section contents.
260 const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
261 shdr.get_sh_size());
262 const elfcpp::Elf_Word* pword =
263 reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
264
265 // The first word contains flags. We only care about COMDAT section
266 // groups. Other section groups are always included in the link
267 // just like ordinary sections.
f6ce93d6 268 elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
a2fb1b05
ILT
269 if ((flags & elfcpp::GRP_COMDAT) == 0)
270 return true;
271
272 // Look up the group signature, which is the name of a symbol. This
273 // is a lot of effort to go to to read a string. Why didn't they
274 // just use the name of the SHT_GROUP section as the group
275 // signature?
276
277 // Get the appropriate symbol table header (this will normally be
278 // the single SHT_SYMTAB section, but in principle it need not be).
279 if (shdr.get_sh_link() >= this->shnum())
280 {
281 fprintf(stderr, _("%s: %s: section group %u link %u out of range\n"),
282 program_name, this->name().c_str(), index, shdr.get_sh_link());
283 gold_exit(false);
284 }
75f65a3e
ILT
285
286 typename This::Shdr symshdr(this->section_header(shdr.get_sh_link()));
a2fb1b05
ILT
287
288 // Read the symbol table entry.
289 if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
290 {
291 fprintf(stderr, _("%s: %s: section group %u info %u out of range\n"),
292 program_name, this->name().c_str(), index, shdr.get_sh_info());
293 gold_exit(false);
294 }
295 off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
296 const unsigned char* psym = this->get_view(symoff, This::sym_size);
297 elfcpp::Sym<size, big_endian> sym(psym);
298
299 // Read the section header for the symbol table names.
300 if (symshdr.get_sh_link() >= this->shnum())
301 {
302 fprintf(stderr, _("%s; %s: symtab section %u link %u out of range\n"),
303 program_name, this->name().c_str(), shdr.get_sh_link(),
304 symshdr.get_sh_link());
305 gold_exit(false);
306 }
75f65a3e
ILT
307
308 typename This::Shdr symnamehdr(this->section_header(symshdr.get_sh_link()));
a2fb1b05
ILT
309
310 // Read the symbol table names.
311 const unsigned char *psymnamesu = this->get_view(symnamehdr.get_sh_offset(),
312 symnamehdr.get_sh_size());
313 const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
314
315 // Get the section group signature.
316 if (sym.get_st_name() >= symnamehdr.get_sh_size())
317 {
318 fprintf(stderr, _("%s: %s: symbol %u name offset %u out of range\n"),
319 program_name, this->name().c_str(), shdr.get_sh_info(),
320 sym.get_st_name());
321 gold_exit(false);
322 }
323
324 const char* signature = psymnames + sym.get_st_name();
325
ead1e424
ILT
326 // It seems that some versions of gas will create a section group
327 // associated with a section symbol, and then fail to give a name to
328 // the section symbol. In such a case, use the name of the section.
329 // FIXME.
330 if (signature[0] == '\0'
331 && sym.get_st_type() == elfcpp::STT_SECTION
332 && sym.get_st_shndx() < this->shnum())
333 {
334 typename This::Shdr shdrnames(this->section_header(this->shstrndx_));
335 const unsigned char* pnamesu = this->get_view(shdrnames.get_sh_offset(),
336 shdrnames.get_sh_size());
337 const char* pnames = reinterpret_cast<const char*>(pnamesu);
338
339 typename This::Shdr sechdr(this->section_header(sym.get_st_shndx()));
340 if (sechdr.get_sh_name() >= shdrnames.get_sh_size())
341 {
342 fprintf(stderr,
343 _("%s: %s: bad section name offset for section %u: %lu\n"),
344 program_name, this->name().c_str(), sym.get_st_shndx(),
345 static_cast<unsigned long>(sechdr.get_sh_name()));
346 gold_exit(false);
347 }
348
349 signature = pnames + sechdr.get_sh_name();
350 }
351
a2fb1b05
ILT
352 // Record this section group, and see whether we've already seen one
353 // with the same signature.
354 if (layout->add_comdat(signature, true))
355 return true;
356
357 // This is a duplicate. We want to discard the sections in this
358 // group.
359 size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
360 for (size_t i = 1; i < count; ++i)
361 {
f6ce93d6
ILT
362 elfcpp::Elf_Word secnum =
363 elfcpp::Swap<32, big_endian>::readval(pword + i);
a2fb1b05
ILT
364 if (secnum >= this->shnum())
365 {
366 fprintf(stderr,
367 _("%s: %s: section %u in section group %u out of range"),
368 program_name, this->name().c_str(), secnum,
369 index);
370 gold_exit(false);
371 }
372 (*omit)[secnum] = true;
373 }
374
375 return false;
376}
377
378// Whether to include a linkonce section in the link. NAME is the
379// name of the section and SHDR is the section header.
380
381// Linkonce sections are a GNU extension implemented in the original
382// GNU linker before section groups were defined. The semantics are
383// that we only include one linkonce section with a given name. The
384// name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
385// where T is the type of section and SYMNAME is the name of a symbol.
386// In an attempt to make linkonce sections interact well with section
387// groups, we try to identify SYMNAME and use it like a section group
388// signature. We want to block section groups with that signature,
389// but not other linkonce sections with that signature. We also use
390// the full name of the linkonce section as a normal section group
391// signature.
392
393template<int size, bool big_endian>
394bool
f6ce93d6 395Sized_relobj<size, big_endian>::include_linkonce_section(
a2fb1b05
ILT
396 Layout* layout,
397 const char* name,
398 const elfcpp::Shdr<size, big_endian>&)
399{
400 const char* symname = strrchr(name, '.') + 1;
a783673b
ILT
401 bool include1 = layout->add_comdat(symname, false);
402 bool include2 = layout->add_comdat(name, true);
403 return include1 && include2;
a2fb1b05
ILT
404}
405
406// Lay out the input sections. We walk through the sections and check
407// whether they should be included in the link. If they should, we
408// pass them to the Layout object, which will return an output section
409// and an offset.
410
411template<int size, bool big_endian>
412void
f6ce93d6
ILT
413Sized_relobj<size, big_endian>::do_layout(const General_options& options,
414 Symbol_table* symtab,
415 Layout* layout,
12e14209 416 Read_symbols_data* sd)
a2fb1b05 417{
12e14209
ILT
418 unsigned int shnum = this->shnum();
419 if (shnum == 0)
420 return;
a2fb1b05
ILT
421
422 // Get the section headers.
12e14209 423 const unsigned char* pshdrs = sd->section_headers->data();
a2fb1b05
ILT
424
425 // Get the section names.
12e14209 426 const unsigned char* pnamesu = sd->section_names->data();
a2fb1b05
ILT
427 const char* pnames = reinterpret_cast<const char*>(pnamesu);
428
429 std::vector<Map_to_output>& map_sections(this->map_to_output());
61ba1cf9 430 map_sections.resize(shnum);
a2fb1b05
ILT
431
432 // Keep track of which sections to omit.
433 std::vector<bool> omit(shnum, false);
434
f6ce93d6
ILT
435 const char warn_prefix[] = ".gnu.warning.";
436 const int warn_prefix_len = sizeof warn_prefix - 1;
437
438 // Skip the first, dummy, section.
439 pshdrs += This::shdr_size;
440 for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
a2fb1b05 441 {
75f65a3e 442 typename This::Shdr shdr(pshdrs);
a2fb1b05 443
12e14209 444 if (shdr.get_sh_name() >= sd->section_names_size)
a2fb1b05
ILT
445 {
446 fprintf(stderr,
447 _("%s: %s: bad section name offset for section %u: %lu\n"),
448 program_name, this->name().c_str(), i,
449 static_cast<unsigned long>(shdr.get_sh_name()));
450 gold_exit(false);
451 }
452
453 const char* name = pnames + shdr.get_sh_name();
454
f6ce93d6
ILT
455 if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
456 {
457 symtab->add_warning(name + warn_prefix_len, this, i);
458 if (!options.is_relocatable())
459 omit[i] = true;
460 }
461
a2fb1b05
ILT
462 bool discard = omit[i];
463 if (!discard)
464 {
465 if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
466 {
467 if (!this->include_section_group(layout, i, shdr, &omit))
468 discard = true;
469 }
470 else if (Layout::is_linkonce(name))
471 {
472 if (!this->include_linkonce_section(layout, name, shdr))
473 discard = true;
474 }
475 }
476
477 if (discard)
478 {
479 // Do not include this section in the link.
480 map_sections[i].output_section = NULL;
481 continue;
482 }
483
484 off_t offset;
ead1e424 485 Output_section* os = layout->layout(this, i, name, shdr, &offset);
a2fb1b05
ILT
486
487 map_sections[i].output_section = os;
488 map_sections[i].offset = offset;
12e14209
ILT
489 }
490
491 delete sd->section_headers;
492 sd->section_headers = NULL;
493 delete sd->section_names;
494 sd->section_names = NULL;
495}
496
497// Add the symbols to the symbol table.
498
499template<int size, bool big_endian>
500void
f6ce93d6 501Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
12e14209
ILT
502 Read_symbols_data* sd)
503{
504 if (sd->symbols == NULL)
505 {
506 assert(sd->symbol_names == NULL);
507 return;
508 }
a2fb1b05 509
12e14209
ILT
510 const int sym_size = This::sym_size;
511 size_t symcount = sd->symbols_size / sym_size;
512 if (symcount * sym_size != sd->symbols_size)
513 {
514 fprintf(stderr,
515 _("%s: %s: size of symbols is not multiple of symbol size\n"),
516 program_name, this->name().c_str());
517 gold_exit(false);
a2fb1b05 518 }
12e14209
ILT
519
520 this->symbols_ = new Symbol*[symcount];
521
12e14209
ILT
522 const char* sym_names =
523 reinterpret_cast<const char*>(sd->symbol_names->data());
f6ce93d6
ILT
524 symtab->add_from_object<size, big_endian>(this, sd->symbols->data(),
525 symcount, sym_names,
526 sd->symbol_names_size,
527 this->symbols_);
12e14209
ILT
528
529 delete sd->symbols;
530 sd->symbols = NULL;
531 delete sd->symbol_names;
532 sd->symbol_names = NULL;
bae7f79e
ILT
533}
534
75f65a3e 535// Finalize the local symbols. Here we record the file offset at
61ba1cf9
ILT
536// which they should be output, we add their names to *POOL, and we
537// add their values to THIS->VALUES_. Return the new file offset.
538// This function is always called from the main thread. The actual
539// output of the local symbols will occur in a separate task.
75f65a3e
ILT
540
541template<int size, bool big_endian>
542off_t
f6ce93d6 543Sized_relobj<size, big_endian>::do_finalize_local_symbols(off_t off,
75f65a3e
ILT
544 Stringpool* pool)
545{
61ba1cf9
ILT
546 if (this->symtab_shnum_ == 0)
547 {
548 // This object has no symbols. Weird but legal.
549 return off;
550 }
551
ead1e424 552 off = align_address(off, size >> 3);
61ba1cf9 553
75f65a3e
ILT
554 this->local_symbol_offset_ = off;
555
556 // Read the symbol table section header.
557 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
558 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
559
560 // Read the local symbols.
75f65a3e 561 const int sym_size = This::sym_size;
92e059d8
ILT
562 const unsigned int loccount = this->local_symbol_count_;
563 assert(loccount == symtabshdr.get_sh_info());
75f65a3e
ILT
564 off_t locsize = loccount * sym_size;
565 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
566 locsize);
567
61ba1cf9
ILT
568 this->values_ = new typename elfcpp::Elf_types<size>::Elf_Addr[loccount];
569
75f65a3e
ILT
570 // Read the section header for the symbol names.
571 typename This::Shdr strtabshdr(
572 this->section_header(symtabshdr.get_sh_link()));
573 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
574
575 // Read the symbol names.
576 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
577 strtabshdr.get_sh_size());
578 const char* pnames = reinterpret_cast<const char*>(pnamesu);
579
580 // Loop over the local symbols.
581
582 std::vector<Map_to_output>& mo(this->map_to_output());
583 unsigned int shnum = this->shnum();
61ba1cf9 584 unsigned int count = 0;
75f65a3e
ILT
585 // Skip the first, dummy, symbol.
586 psyms += sym_size;
61ba1cf9 587 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
75f65a3e
ILT
588 {
589 elfcpp::Sym<size, big_endian> sym(psyms);
590
591 unsigned int shndx = sym.get_st_shndx();
592
593 if (shndx >= elfcpp::SHN_LORESERVE)
594 {
61ba1cf9
ILT
595 if (shndx == elfcpp::SHN_ABS)
596 this->values_[i] = sym.get_st_value();
597 else
75f65a3e 598 {
61ba1cf9 599 // FIXME: Handle SHN_XINDEX.
75f65a3e
ILT
600 fprintf(stderr,
601 _("%s: %s: unknown section index %u "
602 "for local symbol %u\n"),
603 program_name, this->name().c_str(), shndx, i);
604 gold_exit(false);
605 }
75f65a3e
ILT
606 }
607 else
608 {
609 if (shndx >= shnum)
610 {
611 fprintf(stderr,
612 _("%s: %s: local symbol %u section index %u "
613 "out of range\n"),
614 program_name, this->name().c_str(), i, shndx);
615 gold_exit(false);
616 }
617
618 if (mo[shndx].output_section == NULL)
61ba1cf9
ILT
619 {
620 this->values_[i] = 0;
621 continue;
622 }
623
624 this->values_[i] = (mo[shndx].output_section->address()
ead1e424 625 + mo[shndx].offset
61ba1cf9 626 + sym.get_st_value());
75f65a3e
ILT
627 }
628
f6ce93d6
ILT
629 if (sym.get_st_type() != elfcpp::STT_SECTION)
630 {
f0641a0b 631 pool->add(pnames + sym.get_st_name(), NULL);
f6ce93d6
ILT
632 off += sym_size;
633 ++count;
634 }
75f65a3e
ILT
635 }
636
61ba1cf9
ILT
637 this->output_local_symbol_count_ = count;
638
75f65a3e
ILT
639 return off;
640}
641
61ba1cf9
ILT
642// Write out the local symbols.
643
644template<int size, bool big_endian>
645void
f6ce93d6 646Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
61ba1cf9
ILT
647 const Stringpool* sympool)
648{
649 if (this->symtab_shnum_ == 0)
650 {
651 // This object has no symbols. Weird but legal.
652 return;
653 }
654
655 // Read the symbol table section header.
656 typename This::Shdr symtabshdr(this->section_header(this->symtab_shnum_));
657 assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8
ILT
658 const unsigned int loccount = this->local_symbol_count_;
659 assert(loccount == symtabshdr.get_sh_info());
61ba1cf9
ILT
660
661 // Read the local symbols.
662 const int sym_size = This::sym_size;
92e059d8 663 off_t locsize = loccount * sym_size;
61ba1cf9
ILT
664 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
665 locsize);
666
667 // Read the section header for the symbol names.
668 typename This::Shdr strtabshdr(
669 this->section_header(symtabshdr.get_sh_link()));
670 assert(strtabshdr.get_sh_type() == elfcpp::SHT_STRTAB);
671
672 // Read the symbol names.
673 const unsigned char* pnamesu = this->get_view(strtabshdr.get_sh_offset(),
674 strtabshdr.get_sh_size());
675 const char* pnames = reinterpret_cast<const char*>(pnamesu);
676
677 // Get a view into the output file.
678 off_t output_size = this->output_local_symbol_count_ * sym_size;
679 unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
680 output_size);
681
682 std::vector<Map_to_output>& mo(this->map_to_output());
683
684 psyms += sym_size;
685 unsigned char* ov = oview;
92e059d8 686 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
61ba1cf9
ILT
687 {
688 elfcpp::Sym<size, big_endian> isym(psyms);
f6ce93d6
ILT
689
690 if (isym.get_st_type() == elfcpp::STT_SECTION)
691 continue;
61ba1cf9
ILT
692
693 unsigned int st_shndx = isym.get_st_shndx();
694 if (st_shndx < elfcpp::SHN_LORESERVE)
695 {
696 assert(st_shndx < mo.size());
697 if (mo[st_shndx].output_section == NULL)
698 continue;
ead1e424 699 st_shndx = mo[st_shndx].output_section->out_shndx();
61ba1cf9
ILT
700 }
701
f6ce93d6
ILT
702 elfcpp::Sym_write<size, big_endian> osym(ov);
703
61ba1cf9
ILT
704 osym.put_st_name(sympool->get_offset(pnames + isym.get_st_name()));
705 osym.put_st_value(this->values_[i]);
706 osym.put_st_size(isym.get_st_size());
707 osym.put_st_info(isym.get_st_info());
708 osym.put_st_other(isym.get_st_other());
709 osym.put_st_shndx(st_shndx);
710
711 ov += sym_size;
712 }
713
714 assert(ov - oview == output_size);
715
716 of->write_output_view(this->local_symbol_offset_, output_size, oview);
717}
718
54dc6425
ILT
719// Input_objects methods.
720
f6ce93d6
ILT
721// Add a regular relocatable object to the list.
722
54dc6425
ILT
723void
724Input_objects::add_object(Object* obj)
725{
f6ce93d6
ILT
726 if (obj->is_dynamic())
727 this->dynobj_list_.push_back(static_cast<Dynobj*>(obj));
728 else
729 this->relobj_list_.push_back(static_cast<Relobj*>(obj));
75f65a3e
ILT
730
731 Target* target = obj->target();
732 if (this->target_ == NULL)
733 this->target_ = target;
734 else if (this->target_ != target)
735 {
736 fprintf(stderr, "%s: %s: incompatible target\n",
737 program_name, obj->name().c_str());
738 gold_exit(false);
739 }
54dc6425
ILT
740}
741
92e059d8
ILT
742// Relocate_info methods.
743
744// Return a string describing the location of a relocation. This is
745// only used in error messages.
746
747template<int size, bool big_endian>
748std::string
749Relocate_info<size, big_endian>::location(size_t relnum, off_t) const
750{
751 std::string ret(this->object->name());
752 ret += ": reloc ";
753 char buf[100];
754 snprintf(buf, sizeof buf, "%zu", relnum);
755 ret += buf;
756 ret += " in reloc section ";
757 snprintf(buf, sizeof buf, "%u", this->reloc_shndx);
758 ret += buf;
759 ret += " (" + this->object->section_name(this->reloc_shndx);
760 ret += ") for section ";
761 snprintf(buf, sizeof buf, "%u", this->data_shndx);
762 ret += buf;
763 ret += " (" + this->object->section_name(this->data_shndx) + ")";
764 return ret;
765}
766
bae7f79e
ILT
767} // End namespace gold.
768
769namespace
770{
771
772using namespace gold;
773
774// Read an ELF file with the header and return the appropriate
775// instance of Object.
776
777template<int size, bool big_endian>
778Object*
779make_elf_sized_object(const std::string& name, Input_file* input_file,
780 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
781{
782 int et = ehdr.get_e_type();
783 if (et != elfcpp::ET_REL && et != elfcpp::ET_DYN)
784 {
785 fprintf(stderr, "%s: %s: unsupported ELF type %d\n",
786 program_name, name.c_str(), static_cast<int>(et));
787 gold_exit(false);
788 }
789
790 if (et == elfcpp::ET_REL)
791 {
f6ce93d6
ILT
792 Sized_relobj<size, big_endian>* obj =
793 new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
bae7f79e
ILT
794 obj->setup(ehdr);
795 return obj;
796 }
797 else
798 {
799 // elfcpp::ET_DYN
800 fprintf(stderr, _("%s: %s: dynamic objects are not yet supported\n"),
801 program_name, name.c_str());
802 gold_exit(false);
803// Sized_dynobj<size, big_endian>* obj =
804// new Sized_dynobj<size, big_endian>(this->input_.name(), input_file,
805// offset, ehdr);
806// obj->setup(ehdr);
807// return obj;
808 }
809}
810
811} // End anonymous namespace.
812
813namespace gold
814{
815
816// Read an ELF file and return the appropriate instance of Object.
817
818Object*
819make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
820 const unsigned char* p, off_t bytes)
821{
822 if (bytes < elfcpp::EI_NIDENT)
823 {
824 fprintf(stderr, _("%s: %s: ELF file too short\n"),
825 program_name, name.c_str());
826 gold_exit(false);
827 }
828
829 int v = p[elfcpp::EI_VERSION];
830 if (v != elfcpp::EV_CURRENT)
831 {
832 if (v == elfcpp::EV_NONE)
833 fprintf(stderr, _("%s: %s: invalid ELF version 0\n"),
834 program_name, name.c_str());
835 else
836 fprintf(stderr, _("%s: %s: unsupported ELF version %d\n"),
837 program_name, name.c_str(), v);
838 gold_exit(false);
839 }
840
841 int c = p[elfcpp::EI_CLASS];
842 if (c == elfcpp::ELFCLASSNONE)
843 {
844 fprintf(stderr, _("%s: %s: invalid ELF class 0\n"),
845 program_name, name.c_str());
846 gold_exit(false);
847 }
848 else if (c != elfcpp::ELFCLASS32
849 && c != elfcpp::ELFCLASS64)
850 {
851 fprintf(stderr, _("%s: %s: unsupported ELF class %d\n"),
852 program_name, name.c_str(), c);
853 gold_exit(false);
854 }
855
856 int d = p[elfcpp::EI_DATA];
857 if (d == elfcpp::ELFDATANONE)
858 {
859 fprintf(stderr, _("%s: %s: invalid ELF data encoding\n"),
860 program_name, name.c_str());
861 gold_exit(false);
862 }
863 else if (d != elfcpp::ELFDATA2LSB
864 && d != elfcpp::ELFDATA2MSB)
865 {
866 fprintf(stderr, _("%s: %s: unsupported ELF data encoding %d\n"),
867 program_name, name.c_str(), d);
868 gold_exit(false);
869 }
870
871 bool big_endian = d == elfcpp::ELFDATA2MSB;
872
873 if (c == elfcpp::ELFCLASS32)
874 {
875 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
876 {
877 fprintf(stderr, _("%s: %s: ELF file too short\n"),
878 program_name, name.c_str());
879 gold_exit(false);
880 }
881 if (big_endian)
882 {
883 elfcpp::Ehdr<32, true> ehdr(p);
884 return make_elf_sized_object<32, true>(name, input_file,
885 offset, ehdr);
886 }
887 else
888 {
889 elfcpp::Ehdr<32, false> ehdr(p);
890 return make_elf_sized_object<32, false>(name, input_file,
891 offset, ehdr);
892 }
893 }
894 else
895 {
896 if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
897 {
898 fprintf(stderr, _("%s: %s: ELF file too short\n"),
899 program_name, name.c_str());
900 gold_exit(false);
901 }
902 if (big_endian)
903 {
904 elfcpp::Ehdr<64, true> ehdr(p);
905 return make_elf_sized_object<64, true>(name, input_file,
906 offset, ehdr);
907 }
908 else
909 {
910 elfcpp::Ehdr<64, false> ehdr(p);
911 return make_elf_sized_object<64, false>(name, input_file,
912 offset, ehdr);
913 }
914 }
915}
916
917// Instantiate the templates we need. We could use the configure
918// script to restrict this to only the ones for implemented targets.
919
920template
f6ce93d6 921class Sized_relobj<32, false>;
bae7f79e
ILT
922
923template
f6ce93d6 924class Sized_relobj<32, true>;
bae7f79e
ILT
925
926template
f6ce93d6 927class Sized_relobj<64, false>;
bae7f79e
ILT
928
929template
f6ce93d6 930class Sized_relobj<64, true>;
bae7f79e 931
92e059d8
ILT
932template
933struct Relocate_info<32, false>;
934
935template
936struct Relocate_info<32, true>;
937
938template
939struct Relocate_info<64, false>;
940
941template
942struct Relocate_info<64, true>;
943
bae7f79e 944} // End namespace gold.
This page took 0.130146 seconds and 4 git commands to generate.