* corefile.c (generic_search): Delete disabled code.
[deliverable/binutils-gdb.git] / gold / reloc.cc
CommitLineData
61ba1cf9
ILT
1// reloc.cc -- relocate input files for 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
61ba1cf9
ILT
23#include "gold.h"
24
cb295612
ILT
25#include <algorithm>
26
61ba1cf9 27#include "workqueue.h"
5a6f7e2d 28#include "symtab.h"
61ba1cf9 29#include "output.h"
a9a60db6
ILT
30#include "merge.h"
31#include "object.h"
7019cd25 32#include "target-reloc.h"
61ba1cf9
ILT
33#include "reloc.h"
34
35namespace gold
36{
37
92e059d8
ILT
38// Read_relocs methods.
39
40// These tasks just read the relocation information from the file.
41// After reading it, the start another task to process the
42// information. These tasks requires access to the file.
43
17a1d0a9
ILT
44Task_token*
45Read_relocs::is_runnable()
92e059d8 46{
17a1d0a9 47 return this->object_->is_locked() ? this->object_->token() : NULL;
92e059d8
ILT
48}
49
50// Lock the file.
51
17a1d0a9
ILT
52void
53Read_relocs::locks(Task_locker* tl)
92e059d8 54{
17a1d0a9 55 tl->add(this, this->object_->token());
92e059d8
ILT
56}
57
58// Read the relocations and then start a Scan_relocs_task.
59
60void
61Read_relocs::run(Workqueue* workqueue)
62{
63 Read_relocs_data *rd = new Read_relocs_data;
64 this->object_->read_relocs(rd);
17a1d0a9
ILT
65 this->object_->release();
66
da769d56
ILT
67 workqueue->queue_next(new Scan_relocs(this->options_, this->symtab_,
68 this->layout_, this->object_, rd,
69 this->symtab_lock_, this->blocker_));
92e059d8
ILT
70}
71
c7912668
ILT
72// Return a debugging name for the task.
73
74std::string
75Read_relocs::get_name() const
76{
77 return "Read_relocs " + this->object_->name();
78}
79
92e059d8
ILT
80// Scan_relocs methods.
81
82// These tasks scan the relocations read by Read_relocs and mark up
83// the symbol table to indicate which relocations are required. We
84// use a lock on the symbol table to keep them from interfering with
85// each other.
86
17a1d0a9
ILT
87Task_token*
88Scan_relocs::is_runnable()
92e059d8 89{
17a1d0a9
ILT
90 if (!this->symtab_lock_->is_writable())
91 return this->symtab_lock_;
92 if (this->object_->is_locked())
93 return this->object_->token();
94 return NULL;
92e059d8
ILT
95}
96
97// Return the locks we hold: one on the file, one on the symbol table
98// and one blocker.
99
17a1d0a9
ILT
100void
101Scan_relocs::locks(Task_locker* tl)
92e059d8 102{
17a1d0a9
ILT
103 tl->add(this, this->object_->token());
104 tl->add(this, this->symtab_lock_);
105 tl->add(this, this->blocker_);
92e059d8
ILT
106}
107
108// Scan the relocs.
109
110void
111Scan_relocs::run(Workqueue*)
112{
ead1e424
ILT
113 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
114 this->rd_);
17a1d0a9 115 this->object_->release();
92e059d8
ILT
116 delete this->rd_;
117 this->rd_ = NULL;
118}
119
c7912668
ILT
120// Return a debugging name for the task.
121
122std::string
123Scan_relocs::get_name() const
124{
125 return "Scan_relocs " + this->object_->name();
126}
127
61ba1cf9
ILT
128// Relocate_task methods.
129
730cdc88 130// We may have to wait for the output sections to be written.
61ba1cf9 131
17a1d0a9
ILT
132Task_token*
133Relocate_task::is_runnable()
61ba1cf9 134{
730cdc88
ILT
135 if (this->object_->relocs_must_follow_section_writes()
136 && this->output_sections_blocker_->is_blocked())
17a1d0a9 137 return this->output_sections_blocker_;
730cdc88 138
c7912668 139 if (this->object_->is_locked())
17a1d0a9 140 return this->object_->token();
c7912668 141
17a1d0a9 142 return NULL;
61ba1cf9
ILT
143}
144
145// We want to lock the file while we run. We want to unblock
730cdc88 146// INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
17a1d0a9 147// INPUT_SECTIONS_BLOCKER may be NULL.
61ba1cf9 148
17a1d0a9
ILT
149void
150Relocate_task::locks(Task_locker* tl)
61ba1cf9 151{
17a1d0a9
ILT
152 if (this->input_sections_blocker_ != NULL)
153 tl->add(this, this->input_sections_blocker_);
154 tl->add(this, this->final_blocker_);
155 tl->add(this, this->object_->token());
61ba1cf9
ILT
156}
157
158// Run the task.
159
160void
161Relocate_task::run(Workqueue*)
162{
92e059d8 163 this->object_->relocate(this->options_, this->symtab_, this->layout_,
61ba1cf9 164 this->of_);
cb295612
ILT
165
166 // This is normally the last thing we will do with an object, so
167 // uncache all views.
168 this->object_->clear_view_cache_marks();
169
17a1d0a9 170 this->object_->release();
61ba1cf9
ILT
171}
172
c7912668
ILT
173// Return a debugging name for the task.
174
175std::string
176Relocate_task::get_name() const
177{
178 return "Relocate_task " + this->object_->name();
179}
180
92e059d8
ILT
181// Read the relocs and local symbols from the object file and store
182// the information in RD.
183
184template<int size, bool big_endian>
185void
f6ce93d6 186Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
92e059d8
ILT
187{
188 rd->relocs.clear();
189
190 unsigned int shnum = this->shnum();
191 if (shnum == 0)
192 return;
193
194 rd->relocs.reserve(shnum / 2);
195
ef9beddf
ILT
196 const Output_sections& out_sections(this->output_sections());
197 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 198
645f8123 199 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
9eb9fa57 200 shnum * This::shdr_size,
39d0cb0e 201 true, true);
92e059d8
ILT
202 // Skip the first, dummy, section.
203 const unsigned char *ps = pshdrs + This::shdr_size;
204 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
205 {
206 typename This::Shdr shdr(ps);
207
208 unsigned int sh_type = shdr.get_sh_type();
209 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
210 continue;
211
d491d34e 212 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
92e059d8
ILT
213 if (shndx >= shnum)
214 {
75f2446e
ILT
215 this->error(_("relocation section %u has bad info %u"),
216 i, shndx);
217 continue;
92e059d8
ILT
218 }
219
ef9beddf 220 Output_section* os = out_sections[shndx];
730cdc88 221 if (os == NULL)
92e059d8
ILT
222 continue;
223
ead1e424
ILT
224 // We are scanning relocations in order to fill out the GOT and
225 // PLT sections. Relocations for sections which are not
226 // allocated (typically debugging sections) should not add new
6a74a719 227 // GOT and PLT entries. So we skip them unless this is a
7019cd25
ILT
228 // relocatable link or we need to emit relocations.
229 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
230 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
231 != 0);
232 if (!is_section_allocated
8851ecca
ILT
233 && !parameters->options().relocatable()
234 && !parameters->options().emit_relocs())
7019cd25 235 continue;
ead1e424 236
d491d34e 237 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
92e059d8 238 {
75f2446e
ILT
239 this->error(_("relocation section %u uses unexpected "
240 "symbol table %u"),
d491d34e 241 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 242 continue;
92e059d8
ILT
243 }
244
245 off_t sh_size = shdr.get_sh_size();
246
247 unsigned int reloc_size;
248 if (sh_type == elfcpp::SHT_REL)
249 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
250 else
251 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
252 if (reloc_size != shdr.get_sh_entsize())
253 {
75f2446e
ILT
254 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
255 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
256 reloc_size);
257 continue;
92e059d8
ILT
258 }
259
260 size_t reloc_count = sh_size / reloc_size;
f5c3f225 261 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
92e059d8 262 {
75f2446e
ILT
263 this->error(_("reloc section %u size %lu uneven"),
264 i, static_cast<unsigned long>(sh_size));
265 continue;
92e059d8
ILT
266 }
267
268 rd->relocs.push_back(Section_relocs());
269 Section_relocs& sr(rd->relocs.back());
270 sr.reloc_shndx = i;
271 sr.data_shndx = shndx;
9eb9fa57 272 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
39d0cb0e 273 true, true);
92e059d8
ILT
274 sr.sh_type = sh_type;
275 sr.reloc_count = reloc_count;
730cdc88 276 sr.output_section = os;
a45248e0 277 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
7019cd25 278 sr.is_data_section_allocated = is_section_allocated;
92e059d8
ILT
279 }
280
281 // Read the local symbols.
a3ad94ed 282 gold_assert(this->symtab_shndx_ != -1U);
645f8123 283 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
92e059d8
ILT
284 rd->local_symbols = NULL;
285 else
286 {
287 typename This::Shdr symtabshdr(pshdrs
645f8123 288 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 289 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8
ILT
290 const int sym_size = This::sym_size;
291 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 292 gold_assert(loccount == symtabshdr.get_sh_info());
92e059d8
ILT
293 off_t locsize = loccount * sym_size;
294 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
39d0cb0e 295 locsize, true, true);
92e059d8
ILT
296 }
297}
298
299// Scan the relocs and adjust the symbol table. This looks for
300// relocations which require GOT/PLT/COPY relocations.
301
302template<int size, bool big_endian>
303void
f6ce93d6 304Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
92e059d8 305 Symbol_table* symtab,
ead1e424 306 Layout* layout,
92e059d8
ILT
307 Read_relocs_data* rd)
308{
309 Sized_target<size, big_endian>* target = this->sized_target();
310
311 const unsigned char* local_symbols;
312 if (rd->local_symbols == NULL)
313 local_symbols = NULL;
314 else
315 local_symbols = rd->local_symbols->data();
316
317 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
318 p != rd->relocs.end();
319 ++p)
320 {
8851ecca 321 if (!parameters->options().relocatable())
7019cd25
ILT
322 {
323 // As noted above, when not generating an object file, we
324 // only scan allocated sections. We may see a non-allocated
325 // section here if we are emitting relocs.
326 if (p->is_data_section_allocated)
327 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
328 p->sh_type, p->contents->data(),
329 p->reloc_count, p->output_section,
330 p->needs_special_offset_handling,
331 this->local_symbol_count_,
332 local_symbols);
8851ecca 333 if (parameters->options().emit_relocs())
7019cd25
ILT
334 this->emit_relocs_scan(options, symtab, layout, local_symbols, p);
335 }
6a74a719
ILT
336 else
337 {
338 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
339 gold_assert(rr != NULL);
340 rr->set_reloc_count(p->reloc_count);
341 target->scan_relocatable_relocs(options, symtab, layout, this,
342 p->data_shndx, p->sh_type,
343 p->contents->data(),
344 p->reloc_count,
345 p->output_section,
346 p->needs_special_offset_handling,
347 this->local_symbol_count_,
348 local_symbols,
349 rr);
350 }
351
92e059d8
ILT
352 delete p->contents;
353 p->contents = NULL;
354 }
355
356 if (rd->local_symbols != NULL)
357 {
358 delete rd->local_symbols;
359 rd->local_symbols = NULL;
360 }
361}
362
7019cd25
ILT
363// This is a strategy class we use when scanning for --emit-relocs.
364
365template<int sh_type>
366class Emit_relocs_strategy
367{
368 public:
369 // A local non-section symbol.
370 inline Relocatable_relocs::Reloc_strategy
68943102 371 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
7019cd25
ILT
372 { return Relocatable_relocs::RELOC_COPY; }
373
374 // A local section symbol.
375 inline Relocatable_relocs::Reloc_strategy
376 local_section_strategy(unsigned int, Relobj*)
377 {
378 if (sh_type == elfcpp::SHT_RELA)
379 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
380 else
381 {
382 // The addend is stored in the section contents. Since this
383 // is not a relocatable link, we are going to apply the
384 // relocation contents to the section as usual. This means
385 // that we have no way to record the original addend. If the
386 // original addend is not zero, there is basically no way for
387 // the user to handle this correctly. Caveat emptor.
388 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
389 }
390 }
391
392 // A global symbol.
393 inline Relocatable_relocs::Reloc_strategy
394 global_strategy(unsigned int, Relobj*, unsigned int)
395 { return Relocatable_relocs::RELOC_COPY; }
396};
397
398// Scan the input relocations for --emit-relocs.
399
400template<int size, bool big_endian>
401void
402Sized_relobj<size, big_endian>::emit_relocs_scan(
403 const General_options& options,
404 Symbol_table* symtab,
405 Layout* layout,
406 const unsigned char* plocal_syms,
407 const Read_relocs_data::Relocs_list::iterator& p)
408{
409 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
410 gold_assert(rr != NULL);
411 rr->set_reloc_count(p->reloc_count);
412
413 if (p->sh_type == elfcpp::SHT_REL)
414 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(options, symtab, layout,
415 plocal_syms, p, rr);
416 else
417 {
418 gold_assert(p->sh_type == elfcpp::SHT_RELA);
419 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(options, symtab,
420 layout, plocal_syms, p,
421 rr);
422 }
423}
424
425// Scan the input relocation for --emit-relocs, templatized on the
426// type of the relocation section.
427
428template<int size, bool big_endian>
429template<int sh_type>
430void
431Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
432 const General_options& options,
433 Symbol_table* symtab,
434 Layout* layout,
435 const unsigned char* plocal_syms,
436 const Read_relocs_data::Relocs_list::iterator& p,
437 Relocatable_relocs* rr)
438{
439 scan_relocatable_relocs<size, big_endian, sh_type,
440 Emit_relocs_strategy<sh_type> >(
441 options,
442 symtab,
443 layout,
444 this,
445 p->data_shndx,
446 p->contents->data(),
447 p->reloc_count,
448 p->output_section,
449 p->needs_special_offset_handling,
450 this->local_symbol_count_,
451 plocal_syms,
452 rr);
453}
454
61ba1cf9
ILT
455// Relocate the input sections and write out the local symbols.
456
457template<int size, bool big_endian>
458void
f6ce93d6 459Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
61ba1cf9 460 const Symbol_table* symtab,
92e059d8 461 const Layout* layout,
61ba1cf9
ILT
462 Output_file* of)
463{
464 unsigned int shnum = this->shnum();
465
466 // Read the section headers.
645f8123 467 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
9eb9fa57 468 shnum * This::shdr_size,
39d0cb0e 469 true, true);
61ba1cf9
ILT
470
471 Views views;
472 views.resize(shnum);
473
474 // Make two passes over the sections. The first one copies the
475 // section data to the output file. The second one applies
476 // relocations.
477
478 this->write_sections(pshdrs, of, &views);
479
a9a60db6
ILT
480 // To speed up relocations, we set up hash tables for fast lookup of
481 // input offsets to output addresses.
482 this->initialize_input_to_output_maps();
483
61ba1cf9
ILT
484 // Apply relocations.
485
92e059d8 486 this->relocate_sections(options, symtab, layout, pshdrs, &views);
61ba1cf9 487
a9a60db6
ILT
488 // After we've done the relocations, we release the hash tables,
489 // since we no longer need them.
490 this->free_input_to_output_maps();
491
61ba1cf9
ILT
492 // Write out the accumulated views.
493 for (unsigned int i = 1; i < shnum; ++i)
494 {
495 if (views[i].view != NULL)
730cdc88 496 {
96803768
ILT
497 if (!views[i].is_postprocessing_view)
498 {
499 if (views[i].is_input_output_view)
500 of->write_input_output_view(views[i].offset,
501 views[i].view_size,
502 views[i].view);
503 else
504 of->write_output_view(views[i].offset, views[i].view_size,
505 views[i].view);
506 }
730cdc88 507 }
61ba1cf9
ILT
508 }
509
510 // Write out the local symbols.
d491d34e
ILT
511 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
512 layout->symtab_xindex(), layout->dynsym_xindex());
cb295612
ILT
513
514 // We should no longer need the local symbol values.
515 this->clear_local_symbols();
61ba1cf9
ILT
516}
517
cb295612
ILT
518// Sort a Read_multiple vector by file offset.
519struct Read_multiple_compare
520{
521 inline bool
522 operator()(const File_read::Read_multiple_entry& rme1,
523 const File_read::Read_multiple_entry& rme2) const
524 { return rme1.file_offset < rme2.file_offset; }
525};
526
61ba1cf9
ILT
527// Write section data to the output file. PSHDRS points to the
528// section headers. Record the views in *PVIEWS for use when
529// relocating.
530
531template<int size, bool big_endian>
532void
f6ce93d6 533Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
61ba1cf9 534 Output_file* of,
cb295612 535 Views* pviews)
61ba1cf9
ILT
536{
537 unsigned int shnum = this->shnum();
ef9beddf
ILT
538 const Output_sections& out_sections(this->output_sections());
539 const std::vector<Address>& out_offsets(this->section_offsets_);
61ba1cf9 540
cb295612
ILT
541 File_read::Read_multiple rm;
542 bool is_sorted = true;
543
61ba1cf9
ILT
544 const unsigned char* p = pshdrs + This::shdr_size;
545 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
546 {
547 View_size* pvs = &(*pviews)[i];
548
549 pvs->view = NULL;
550
ef9beddf 551 const Output_section* os = out_sections[i];
61ba1cf9
ILT
552 if (os == NULL)
553 continue;
ef9beddf 554 Address output_offset = out_offsets[i];
61ba1cf9
ILT
555
556 typename This::Shdr shdr(p);
557
558 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
559 continue;
560
8851ecca
ILT
561 if ((parameters->options().relocatable()
562 || parameters->options().emit_relocs())
6a74a719
ILT
563 && (shdr.get_sh_type() == elfcpp::SHT_REL
564 || shdr.get_sh_type() == elfcpp::SHT_RELA)
565 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
566 {
7019cd25
ILT
567 // This is a reloc section in a relocatable link or when
568 // emitting relocs. We don't need to read the input file.
569 // The size and file offset are stored in the
570 // Relocatable_relocs structure.
6a74a719
ILT
571 Relocatable_relocs* rr = this->relocatable_relocs(i);
572 gold_assert(rr != NULL);
573 Output_data* posd = rr->output_data();
574 gold_assert(posd != NULL);
575
576 pvs->offset = posd->offset();
577 pvs->view_size = posd->data_size();
578 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
579 pvs->address = posd->address();
580 pvs->is_input_output_view = false;
581 pvs->is_postprocessing_view = false;
582
583 continue;
584 }
585
96803768
ILT
586 // In the normal case, this input section is simply mapped to
587 // the output section at offset OUTPUT_OFFSET.
588
eff45813
CC
589 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
590 // handled specially--e.g., a .eh_frame section. The relocation
96803768
ILT
591 // routines need to check for each reloc where it should be
592 // applied. For this case, we need an input/output view for the
593 // entire contents of the section in the output file. We don't
594 // want to copy the contents of the input section to the output
595 // section; the output section contents were already written,
596 // and we waited for them in Relocate_task::is_runnable because
597 // relocs_must_follow_section_writes is set for the object.
598
599 // Regardless of which of the above cases is true, we have to
600 // check requires_postprocessing of the output section. If that
601 // is false, then we work with views of the output file
602 // directly. If it is true, then we work with a separate
603 // buffer, and the output section is responsible for writing the
604 // final data to the output file.
605
606 off_t output_section_offset;
ef9beddf 607 Address output_section_size;
96803768
ILT
608 if (!os->requires_postprocessing())
609 {
610 output_section_offset = os->offset();
ef9beddf 611 output_section_size = convert_types<Address, off_t>(os->data_size());
96803768
ILT
612 }
613 else
614 {
615 output_section_offset = 0;
ef9beddf
ILT
616 output_section_size =
617 convert_types<Address, off_t>(os->postprocessing_buffer_size());
96803768
ILT
618 }
619
730cdc88 620 off_t view_start;
fe8718a4 621 section_size_type view_size;
eff45813 622 if (output_offset != invalid_address)
730cdc88 623 {
96803768 624 view_start = output_section_offset + output_offset;
fe8718a4 625 view_size = convert_to_section_size_type(shdr.get_sh_size());
730cdc88
ILT
626 }
627 else
628 {
96803768 629 view_start = output_section_offset;
fe8718a4 630 view_size = convert_to_section_size_type(output_section_size);
730cdc88 631 }
61ba1cf9 632
730cdc88 633 if (view_size == 0)
ead1e424
ILT
634 continue;
635
eff45813 636 gold_assert(output_offset == invalid_address
ef9beddf 637 || output_offset + view_size <= output_section_size);
ead1e424 638
730cdc88 639 unsigned char* view;
96803768
ILT
640 if (os->requires_postprocessing())
641 {
642 unsigned char* buffer = os->postprocessing_buffer();
643 view = buffer + view_start;
eff45813 644 if (output_offset != invalid_address)
cb295612
ILT
645 {
646 off_t sh_offset = shdr.get_sh_offset();
647 if (!rm.empty() && rm.back().file_offset > sh_offset)
648 is_sorted = false;
649 rm.push_back(File_read::Read_multiple_entry(sh_offset,
650 view_size, view));
651 }
96803768 652 }
730cdc88
ILT
653 else
654 {
eff45813 655 if (output_offset == invalid_address)
96803768
ILT
656 view = of->get_input_output_view(view_start, view_size);
657 else
658 {
659 view = of->get_output_view(view_start, view_size);
cb295612
ILT
660 off_t sh_offset = shdr.get_sh_offset();
661 if (!rm.empty() && rm.back().file_offset > sh_offset)
662 is_sorted = false;
663 rm.push_back(File_read::Read_multiple_entry(sh_offset,
664 view_size, view));
96803768 665 }
730cdc88 666 }
92e059d8 667
61ba1cf9 668 pvs->view = view;
730cdc88 669 pvs->address = os->address();
eff45813 670 if (output_offset != invalid_address)
730cdc88
ILT
671 pvs->address += output_offset;
672 pvs->offset = view_start;
673 pvs->view_size = view_size;
eff45813 674 pvs->is_input_output_view = output_offset == invalid_address;
96803768 675 pvs->is_postprocessing_view = os->requires_postprocessing();
61ba1cf9 676 }
cb295612
ILT
677
678 // Actually read the data.
679 if (!rm.empty())
680 {
681 if (!is_sorted)
682 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
683 this->read_multiple(rm);
684 }
61ba1cf9
ILT
685}
686
687// Relocate section data. VIEWS points to the section data as views
688// in the output file.
689
690template<int size, bool big_endian>
691void
f6ce93d6 692Sized_relobj<size, big_endian>::relocate_sections(
92e059d8
ILT
693 const General_options& options,
694 const Symbol_table* symtab,
695 const Layout* layout,
696 const unsigned char* pshdrs,
697 Views* pviews)
61ba1cf9
ILT
698{
699 unsigned int shnum = this->shnum();
61ba1cf9
ILT
700 Sized_target<size, big_endian>* target = this->sized_target();
701
ef9beddf
ILT
702 const Output_sections& out_sections(this->output_sections());
703 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 704
92e059d8
ILT
705 Relocate_info<size, big_endian> relinfo;
706 relinfo.options = &options;
707 relinfo.symtab = symtab;
708 relinfo.layout = layout;
709 relinfo.object = this;
92e059d8 710
61ba1cf9
ILT
711 const unsigned char* p = pshdrs + This::shdr_size;
712 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
713 {
714 typename This::Shdr shdr(p);
715
716 unsigned int sh_type = shdr.get_sh_type();
717 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
718 continue;
719
d491d34e 720 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
61ba1cf9
ILT
721 if (index >= this->shnum())
722 {
75f2446e
ILT
723 this->error(_("relocation section %u has bad info %u"),
724 i, index);
725 continue;
61ba1cf9
ILT
726 }
727
ef9beddf 728 Output_section* os = out_sections[index];
730cdc88 729 if (os == NULL)
61ba1cf9
ILT
730 {
731 // This relocation section is against a section which we
732 // discarded.
733 continue;
734 }
ef9beddf 735 Address output_offset = out_offsets[index];
61ba1cf9 736
a3ad94ed 737 gold_assert((*pviews)[index].view != NULL);
8851ecca 738 if (parameters->options().relocatable())
6a74a719 739 gold_assert((*pviews)[i].view != NULL);
61ba1cf9 740
d491d34e 741 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
61ba1cf9 742 {
75f2446e
ILT
743 gold_error(_("relocation section %u uses unexpected "
744 "symbol table %u"),
d491d34e 745 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 746 continue;
61ba1cf9
ILT
747 }
748
749 off_t sh_size = shdr.get_sh_size();
750 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
39d0cb0e 751 sh_size, true, false);
61ba1cf9
ILT
752
753 unsigned int reloc_size;
754 if (sh_type == elfcpp::SHT_REL)
755 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
756 else
757 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
758
759 if (reloc_size != shdr.get_sh_entsize())
760 {
75f2446e
ILT
761 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
762 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
730cdc88 763 reloc_size);
75f2446e 764 continue;
61ba1cf9
ILT
765 }
766
767 size_t reloc_count = sh_size / reloc_size;
f5c3f225 768 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
61ba1cf9 769 {
75f2446e
ILT
770 gold_error(_("reloc section %u size %lu uneven"),
771 i, static_cast<unsigned long>(sh_size));
772 continue;
61ba1cf9
ILT
773 }
774
eff45813 775 gold_assert(output_offset != invalid_address
96803768
ILT
776 || this->relocs_must_follow_section_writes());
777
92e059d8
ILT
778 relinfo.reloc_shndx = i;
779 relinfo.data_shndx = index;
8851ecca 780 if (!parameters->options().relocatable())
7019cd25
ILT
781 {
782 target->relocate_section(&relinfo,
783 sh_type,
784 prelocs,
785 reloc_count,
786 os,
eff45813 787 output_offset == invalid_address,
7019cd25
ILT
788 (*pviews)[index].view,
789 (*pviews)[index].address,
790 (*pviews)[index].view_size);
8851ecca 791 if (parameters->options().emit_relocs())
7019cd25
ILT
792 this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count,
793 os, output_offset,
794 (*pviews)[index].view,
795 (*pviews)[index].address,
796 (*pviews)[index].view_size,
797 (*pviews)[i].view,
798 (*pviews)[i].view_size);
799 }
6a74a719
ILT
800 else
801 {
802 Relocatable_relocs* rr = this->relocatable_relocs(i);
803 target->relocate_for_relocatable(&relinfo,
804 sh_type,
805 prelocs,
806 reloc_count,
807 os,
808 output_offset,
809 rr,
810 (*pviews)[index].view,
811 (*pviews)[index].address,
812 (*pviews)[index].view_size,
813 (*pviews)[i].view,
814 (*pviews)[i].view_size);
815 }
61ba1cf9
ILT
816 }
817}
818
7019cd25
ILT
819// Emit the relocs for --emit-relocs.
820
821template<int size, bool big_endian>
822void
823Sized_relobj<size, big_endian>::emit_relocs(
824 const Relocate_info<size, big_endian>* relinfo,
825 unsigned int i,
826 unsigned int sh_type,
827 const unsigned char* prelocs,
828 size_t reloc_count,
829 Output_section* output_section,
ef9beddf 830 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
7019cd25
ILT
831 unsigned char* view,
832 typename elfcpp::Elf_types<size>::Elf_Addr address,
833 section_size_type view_size,
834 unsigned char* reloc_view,
835 section_size_type reloc_view_size)
836{
837 if (sh_type == elfcpp::SHT_REL)
838 this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs,
839 reloc_count, output_section,
840 offset_in_output_section,
841 view, address, view_size,
842 reloc_view, reloc_view_size);
843 else
844 {
845 gold_assert(sh_type == elfcpp::SHT_RELA);
846 this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs,
847 reloc_count, output_section,
848 offset_in_output_section,
849 view, address, view_size,
850 reloc_view, reloc_view_size);
851 }
852}
853
854// Emit the relocs for --emit-relocs, templatized on the type of the
855// relocation section.
856
857template<int size, bool big_endian>
858template<int sh_type>
859void
860Sized_relobj<size, big_endian>::emit_relocs_reltype(
861 const Relocate_info<size, big_endian>* relinfo,
862 unsigned int i,
863 const unsigned char* prelocs,
864 size_t reloc_count,
865 Output_section* output_section,
ef9beddf 866 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
7019cd25
ILT
867 unsigned char* view,
868 typename elfcpp::Elf_types<size>::Elf_Addr address,
869 section_size_type view_size,
870 unsigned char* reloc_view,
871 section_size_type reloc_view_size)
872{
873 const Relocatable_relocs* rr = this->relocatable_relocs(i);
874 relocate_for_relocatable<size, big_endian, sh_type>(
875 relinfo,
876 prelocs,
877 reloc_count,
878 output_section,
879 offset_in_output_section,
880 rr,
881 view,
882 address,
883 view_size,
884 reloc_view,
885 reloc_view_size);
886}
887
a9a60db6
ILT
888// Create merge hash tables for the local symbols. These are used to
889// speed up relocations.
890
891template<int size, bool big_endian>
892void
893Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
894{
895 const unsigned int loccount = this->local_symbol_count_;
896 for (unsigned int i = 1; i < loccount; ++i)
897 {
898 Symbol_value<size>& lv(this->local_values_[i]);
899 lv.initialize_input_to_output_map(this);
900 }
901}
902
903// Free merge hash tables for the local symbols.
904
905template<int size, bool big_endian>
906void
907Sized_relobj<size, big_endian>::free_input_to_output_maps()
908{
909 const unsigned int loccount = this->local_symbol_count_;
910 for (unsigned int i = 1; i < loccount; ++i)
911 {
912 Symbol_value<size>& lv(this->local_values_[i]);
913 lv.free_input_to_output_map();
914 }
915}
916
917// Class Merged_symbol_value.
918
919template<int size>
920void
921Merged_symbol_value<size>::initialize_input_to_output_map(
922 const Relobj* object,
923 unsigned int input_shndx)
924{
925 Object_merge_map* map = object->merge_map();
926 map->initialize_input_to_output_map<size>(input_shndx,
927 this->output_start_address_,
928 &this->output_addresses_);
929}
930
931// Get the output value corresponding to an input offset if we
932// couldn't find it in the hash table.
933
934template<int size>
935typename elfcpp::Elf_types<size>::Elf_Addr
936Merged_symbol_value<size>::value_from_output_section(
937 const Relobj* object,
938 unsigned int input_shndx,
939 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
940{
941 section_offset_type output_offset;
942 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
943 input_offset,
944 &output_offset);
945
946 // If this assertion fails, it means that some relocation was
947 // against a portion of an input merge section which we didn't map
948 // to the output file and we didn't explicitly discard. We should
949 // always map all portions of input merge sections.
950 gold_assert(found);
951
952 if (output_offset == -1)
953 return 0;
954 else
955 return this->output_start_address_ + output_offset;
956}
957
730cdc88
ILT
958// Track_relocs methods.
959
960// Initialize the class to track the relocs. This gets the object,
961// the reloc section index, and the type of the relocs. This returns
962// false if something goes wrong.
963
964template<int size, bool big_endian>
965bool
966Track_relocs<size, big_endian>::initialize(
b696e6d4 967 Object* object,
730cdc88
ILT
968 unsigned int reloc_shndx,
969 unsigned int reloc_type)
970{
730cdc88
ILT
971 // If RELOC_SHNDX is -1U, it means there is more than one reloc
972 // section for the .eh_frame section. We can't handle that case.
973 if (reloc_shndx == -1U)
974 return false;
975
976 // If RELOC_SHNDX is 0, there is no reloc section.
977 if (reloc_shndx == 0)
978 return true;
979
980 // Get the contents of the reloc section.
981 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
982
983 if (reloc_type == elfcpp::SHT_REL)
984 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
985 else if (reloc_type == elfcpp::SHT_RELA)
986 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
987 else
988 gold_unreachable();
989
990 if (this->len_ % this->reloc_size_ != 0)
991 {
992 object->error(_("reloc section size %zu is not a multiple of "
993 "reloc size %d\n"),
994 static_cast<size_t>(this->len_),
995 this->reloc_size_);
996 return false;
997 }
998
999 return true;
1000}
1001
1002// Return the offset of the next reloc, or -1 if there isn't one.
1003
1004template<int size, bool big_endian>
1005off_t
1006Track_relocs<size, big_endian>::next_offset() const
1007{
1008 if (this->pos_ >= this->len_)
1009 return -1;
1010
1011 // Rel and Rela start out the same, so we can always use Rel to find
1012 // the r_offset value.
1013 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1014 return rel.get_r_offset();
1015}
1016
1017// Return the index of the symbol referenced by the next reloc, or -1U
1018// if there aren't any more relocs.
1019
1020template<int size, bool big_endian>
1021unsigned int
1022Track_relocs<size, big_endian>::next_symndx() const
1023{
1024 if (this->pos_ >= this->len_)
1025 return -1U;
1026
1027 // Rel and Rela start out the same, so we can use Rel to find the
1028 // symbol index.
1029 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1030 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1031}
1032
1033// Advance to the next reloc whose r_offset is greater than or equal
1034// to OFFSET. Return the number of relocs we skip.
1035
1036template<int size, bool big_endian>
1037int
1038Track_relocs<size, big_endian>::advance(off_t offset)
1039{
1040 int ret = 0;
1041 while (this->pos_ < this->len_)
1042 {
1043 // Rel and Rela start out the same, so we can always use Rel to
1044 // find the r_offset value.
1045 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1046 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1047 break;
1048 ++ret;
1049 this->pos_ += this->reloc_size_;
1050 }
1051 return ret;
1052}
1053
12c0daef 1054// Instantiate the templates we need.
61ba1cf9 1055
193a53d9 1056#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1057template
1058void
f6ce93d6 1059Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1060#endif
92e059d8 1061
193a53d9 1062#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1063template
1064void
f6ce93d6 1065Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1066#endif
92e059d8 1067
193a53d9 1068#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1069template
1070void
f6ce93d6 1071Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1072#endif
92e059d8 1073
193a53d9 1074#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1075template
1076void
f6ce93d6 1077Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1078#endif
92e059d8 1079
193a53d9 1080#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1081template
1082void
f6ce93d6 1083Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
92e059d8 1084 Symbol_table* symtab,
ead1e424 1085 Layout* layout,
92e059d8 1086 Read_relocs_data* rd);
193a53d9 1087#endif
92e059d8 1088
193a53d9 1089#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1090template
1091void
f6ce93d6 1092Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
92e059d8 1093 Symbol_table* symtab,
ead1e424 1094 Layout* layout,
92e059d8 1095 Read_relocs_data* rd);
193a53d9 1096#endif
92e059d8 1097
193a53d9 1098#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1099template
1100void
f6ce93d6 1101Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
92e059d8 1102 Symbol_table* symtab,
ead1e424 1103 Layout* layout,
92e059d8 1104 Read_relocs_data* rd);
193a53d9 1105#endif
92e059d8 1106
193a53d9 1107#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1108template
1109void
f6ce93d6 1110Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
92e059d8 1111 Symbol_table* symtab,
ead1e424 1112 Layout* layout,
92e059d8 1113 Read_relocs_data* rd);
193a53d9 1114#endif
92e059d8 1115
193a53d9 1116#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9
ILT
1117template
1118void
f6ce93d6 1119Sized_relobj<32, false>::do_relocate(const General_options& options,
61ba1cf9 1120 const Symbol_table* symtab,
92e059d8 1121 const Layout* layout,
61ba1cf9 1122 Output_file* of);
193a53d9 1123#endif
61ba1cf9 1124
193a53d9 1125#ifdef HAVE_TARGET_32_BIG
61ba1cf9
ILT
1126template
1127void
f6ce93d6 1128Sized_relobj<32, true>::do_relocate(const General_options& options,
61ba1cf9 1129 const Symbol_table* symtab,
92e059d8 1130 const Layout* layout,
61ba1cf9 1131 Output_file* of);
193a53d9 1132#endif
61ba1cf9 1133
193a53d9 1134#ifdef HAVE_TARGET_64_LITTLE
61ba1cf9
ILT
1135template
1136void
f6ce93d6 1137Sized_relobj<64, false>::do_relocate(const General_options& options,
61ba1cf9 1138 const Symbol_table* symtab,
92e059d8 1139 const Layout* layout,
61ba1cf9 1140 Output_file* of);
193a53d9 1141#endif
61ba1cf9 1142
193a53d9 1143#ifdef HAVE_TARGET_64_BIG
61ba1cf9
ILT
1144template
1145void
f6ce93d6 1146Sized_relobj<64, true>::do_relocate(const General_options& options,
61ba1cf9 1147 const Symbol_table* symtab,
92e059d8 1148 const Layout* layout,
61ba1cf9 1149 Output_file* of);
193a53d9 1150#endif
61ba1cf9 1151
a9a60db6
ILT
1152#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1153template
1154class Merged_symbol_value<32>;
1155#endif
1156
1157#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1158template
1159class Merged_symbol_value<64>;
1160#endif
1161
1162#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1163template
1164class Symbol_value<32>;
1165#endif
1166
1167#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1168template
1169class Symbol_value<64>;
1170#endif
1171
730cdc88
ILT
1172#ifdef HAVE_TARGET_32_LITTLE
1173template
1174class Track_relocs<32, false>;
1175#endif
1176
1177#ifdef HAVE_TARGET_32_BIG
1178template
1179class Track_relocs<32, true>;
1180#endif
1181
1182#ifdef HAVE_TARGET_64_LITTLE
1183template
1184class Track_relocs<64, false>;
1185#endif
1186
1187#ifdef HAVE_TARGET_64_BIG
1188template
1189class Track_relocs<64, true>;
1190#endif
1191
61ba1cf9 1192} // End namespace gold.
This page took 0.168156 seconds and 4 git commands to generate.