merge from gcc
[deliverable/binutils-gdb.git] / gold / reloc.cc
CommitLineData
61ba1cf9
ILT
1// reloc.cc -- relocate input files for gold.
2
6d03d481 3// Copyright 2006, 2007, 2008, 2009 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);
6d03d481 65 this->object_->set_relocs_data(rd);
17a1d0a9
ILT
66 this->object_->release();
67
ef15dade
ST
68 // If garbage collection or identical comdat folding is desired, we
69 // process the relocs first before scanning them. Scanning of relocs is
70 // done only after garbage or identical sections is identified.
71 if (parameters->options().gc_sections() || parameters->options().icf())
72 {
73 workqueue->queue_next(new Gc_process_relocs(this->options_,
6d03d481
ST
74 this->symtab_,
75 this->layout_,
76 this->object_, rd,
77 this->symtab_lock_,
78 this->blocker_));
79 }
80 else
81 {
82 workqueue->queue_next(new Scan_relocs(this->options_, this->symtab_,
83 this->layout_, this->object_, rd,
84 this->symtab_lock_,
85 this->blocker_));
86 }
92e059d8
ILT
87}
88
c7912668
ILT
89// Return a debugging name for the task.
90
91std::string
92Read_relocs::get_name() const
93{
94 return "Read_relocs " + this->object_->name();
95}
96
6d03d481
ST
97// Gc_process_relocs methods.
98
99// These tasks process the relocations read by Read_relocs and
100// determine which sections are referenced and which are garbage.
101// This task is done only when --gc-sections is used.
102
103Task_token*
104Gc_process_relocs::is_runnable()
105{
106 if (this->object_->is_locked())
107 return this->object_->token();
108 return NULL;
109}
110
111void
112Gc_process_relocs::locks(Task_locker* tl)
113{
114 tl->add(this, this->object_->token());
115 tl->add(this, this->blocker_);
116}
117
118void
119Gc_process_relocs::run(Workqueue*)
120{
121 this->object_->gc_process_relocs(this->options_, this->symtab_, this->layout_,
122 this->rd_);
123 this->object_->release();
124}
125
126// Return a debugging name for the task.
127
128std::string
129Gc_process_relocs::get_name() const
130{
131 return "Gc_process_relocs " + this->object_->name();
132}
133
92e059d8
ILT
134// Scan_relocs methods.
135
136// These tasks scan the relocations read by Read_relocs and mark up
137// the symbol table to indicate which relocations are required. We
138// use a lock on the symbol table to keep them from interfering with
139// each other.
140
17a1d0a9
ILT
141Task_token*
142Scan_relocs::is_runnable()
92e059d8 143{
17a1d0a9
ILT
144 if (!this->symtab_lock_->is_writable())
145 return this->symtab_lock_;
146 if (this->object_->is_locked())
147 return this->object_->token();
148 return NULL;
92e059d8
ILT
149}
150
151// Return the locks we hold: one on the file, one on the symbol table
152// and one blocker.
153
17a1d0a9
ILT
154void
155Scan_relocs::locks(Task_locker* tl)
92e059d8 156{
17a1d0a9
ILT
157 tl->add(this, this->object_->token());
158 tl->add(this, this->symtab_lock_);
159 tl->add(this, this->blocker_);
92e059d8
ILT
160}
161
162// Scan the relocs.
163
164void
165Scan_relocs::run(Workqueue*)
166{
ead1e424
ILT
167 this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
168 this->rd_);
17a1d0a9 169 this->object_->release();
92e059d8
ILT
170 delete this->rd_;
171 this->rd_ = NULL;
172}
173
c7912668
ILT
174// Return a debugging name for the task.
175
176std::string
177Scan_relocs::get_name() const
178{
179 return "Scan_relocs " + this->object_->name();
180}
181
61ba1cf9
ILT
182// Relocate_task methods.
183
730cdc88 184// We may have to wait for the output sections to be written.
61ba1cf9 185
17a1d0a9
ILT
186Task_token*
187Relocate_task::is_runnable()
61ba1cf9 188{
730cdc88
ILT
189 if (this->object_->relocs_must_follow_section_writes()
190 && this->output_sections_blocker_->is_blocked())
17a1d0a9 191 return this->output_sections_blocker_;
730cdc88 192
c7912668 193 if (this->object_->is_locked())
17a1d0a9 194 return this->object_->token();
c7912668 195
17a1d0a9 196 return NULL;
61ba1cf9
ILT
197}
198
199// We want to lock the file while we run. We want to unblock
730cdc88 200// INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
17a1d0a9 201// INPUT_SECTIONS_BLOCKER may be NULL.
61ba1cf9 202
17a1d0a9
ILT
203void
204Relocate_task::locks(Task_locker* tl)
61ba1cf9 205{
17a1d0a9
ILT
206 if (this->input_sections_blocker_ != NULL)
207 tl->add(this, this->input_sections_blocker_);
208 tl->add(this, this->final_blocker_);
209 tl->add(this, this->object_->token());
61ba1cf9
ILT
210}
211
212// Run the task.
213
214void
215Relocate_task::run(Workqueue*)
216{
92e059d8 217 this->object_->relocate(this->options_, this->symtab_, this->layout_,
61ba1cf9 218 this->of_);
cb295612
ILT
219
220 // This is normally the last thing we will do with an object, so
221 // uncache all views.
222 this->object_->clear_view_cache_marks();
223
17a1d0a9 224 this->object_->release();
61ba1cf9
ILT
225}
226
c7912668
ILT
227// Return a debugging name for the task.
228
229std::string
230Relocate_task::get_name() const
231{
232 return "Relocate_task " + this->object_->name();
233}
234
92e059d8
ILT
235// Read the relocs and local symbols from the object file and store
236// the information in RD.
237
238template<int size, bool big_endian>
239void
f6ce93d6 240Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
92e059d8
ILT
241{
242 rd->relocs.clear();
243
244 unsigned int shnum = this->shnum();
245 if (shnum == 0)
246 return;
247
248 rd->relocs.reserve(shnum / 2);
249
ef9beddf
ILT
250 const Output_sections& out_sections(this->output_sections());
251 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 252
645f8123 253 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
9eb9fa57 254 shnum * This::shdr_size,
39d0cb0e 255 true, true);
92e059d8
ILT
256 // Skip the first, dummy, section.
257 const unsigned char *ps = pshdrs + This::shdr_size;
258 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
259 {
260 typename This::Shdr shdr(ps);
261
262 unsigned int sh_type = shdr.get_sh_type();
263 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
264 continue;
265
d491d34e 266 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
92e059d8
ILT
267 if (shndx >= shnum)
268 {
75f2446e
ILT
269 this->error(_("relocation section %u has bad info %u"),
270 i, shndx);
271 continue;
92e059d8
ILT
272 }
273
ef9beddf 274 Output_section* os = out_sections[shndx];
730cdc88 275 if (os == NULL)
92e059d8
ILT
276 continue;
277
ead1e424
ILT
278 // We are scanning relocations in order to fill out the GOT and
279 // PLT sections. Relocations for sections which are not
280 // allocated (typically debugging sections) should not add new
6a74a719 281 // GOT and PLT entries. So we skip them unless this is a
031cdbed
ILT
282 // relocatable link or we need to emit relocations. FIXME: What
283 // should we do if a linker script maps a section with SHF_ALLOC
284 // clear to a section with SHF_ALLOC set?
7019cd25
ILT
285 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
286 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
287 != 0);
288 if (!is_section_allocated
8851ecca
ILT
289 && !parameters->options().relocatable()
290 && !parameters->options().emit_relocs())
7019cd25 291 continue;
ead1e424 292
d491d34e 293 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
92e059d8 294 {
75f2446e
ILT
295 this->error(_("relocation section %u uses unexpected "
296 "symbol table %u"),
d491d34e 297 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 298 continue;
92e059d8
ILT
299 }
300
301 off_t sh_size = shdr.get_sh_size();
302
303 unsigned int reloc_size;
304 if (sh_type == elfcpp::SHT_REL)
305 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
306 else
307 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
308 if (reloc_size != shdr.get_sh_entsize())
309 {
75f2446e
ILT
310 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
311 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
312 reloc_size);
313 continue;
92e059d8
ILT
314 }
315
316 size_t reloc_count = sh_size / reloc_size;
f5c3f225 317 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
92e059d8 318 {
75f2446e
ILT
319 this->error(_("reloc section %u size %lu uneven"),
320 i, static_cast<unsigned long>(sh_size));
321 continue;
92e059d8
ILT
322 }
323
324 rd->relocs.push_back(Section_relocs());
325 Section_relocs& sr(rd->relocs.back());
326 sr.reloc_shndx = i;
327 sr.data_shndx = shndx;
9eb9fa57 328 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
39d0cb0e 329 true, true);
92e059d8
ILT
330 sr.sh_type = sh_type;
331 sr.reloc_count = reloc_count;
730cdc88 332 sr.output_section = os;
a45248e0 333 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
7019cd25 334 sr.is_data_section_allocated = is_section_allocated;
92e059d8
ILT
335 }
336
337 // Read the local symbols.
a3ad94ed 338 gold_assert(this->symtab_shndx_ != -1U);
645f8123 339 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
92e059d8
ILT
340 rd->local_symbols = NULL;
341 else
342 {
343 typename This::Shdr symtabshdr(pshdrs
645f8123 344 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 345 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8
ILT
346 const int sym_size = This::sym_size;
347 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 348 gold_assert(loccount == symtabshdr.get_sh_info());
92e059d8
ILT
349 off_t locsize = loccount * sym_size;
350 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
39d0cb0e 351 locsize, true, true);
92e059d8
ILT
352 }
353}
354
6d03d481
ST
355// Process the relocs to generate mappings from source sections to referenced
356// sections. This is used during garbage colletion to determine garbage
357// sections.
358
359template<int size, bool big_endian>
360void
361Sized_relobj<size, big_endian>::do_gc_process_relocs(const General_options& options,
362 Symbol_table* symtab,
363 Layout* layout,
364 Read_relocs_data* rd)
365{
029ba973
ILT
366 Sized_target<size, big_endian>* target =
367 parameters->sized_target<size, big_endian>();
6d03d481
ST
368
369 const unsigned char* local_symbols;
370 if (rd->local_symbols == NULL)
371 local_symbols = NULL;
372 else
373 local_symbols = rd->local_symbols->data();
374
375 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
376 p != rd->relocs.end();
377 ++p)
378 {
379 if (!parameters->options().relocatable())
380 {
381 // As noted above, when not generating an object file, we
382 // only scan allocated sections. We may see a non-allocated
383 // section here if we are emitting relocs.
384 if (p->is_data_section_allocated)
385 target->gc_process_relocs(options, symtab, layout, this,
386 p->data_shndx, p->sh_type,
387 p->contents->data(), p->reloc_count,
388 p->output_section,
389 p->needs_special_offset_handling,
390 this->local_symbol_count_,
391 local_symbols);
392 }
393 }
394}
395
396
92e059d8
ILT
397// Scan the relocs and adjust the symbol table. This looks for
398// relocations which require GOT/PLT/COPY relocations.
399
400template<int size, bool big_endian>
401void
f6ce93d6 402Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
92e059d8 403 Symbol_table* symtab,
ead1e424 404 Layout* layout,
92e059d8
ILT
405 Read_relocs_data* rd)
406{
029ba973
ILT
407 Sized_target<size, big_endian>* target =
408 parameters->sized_target<size, big_endian>();
92e059d8
ILT
409
410 const unsigned char* local_symbols;
411 if (rd->local_symbols == NULL)
412 local_symbols = NULL;
413 else
414 local_symbols = rd->local_symbols->data();
415
416 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
417 p != rd->relocs.end();
418 ++p)
419 {
6d03d481
ST
420 // When garbage collection is on, unreferenced sections are not included
421 // in the link that would have been included normally. This is known only
422 // after Read_relocs hence this check has to be done again.
ef15dade 423 if (parameters->options().gc_sections() || parameters->options().icf())
6d03d481
ST
424 {
425 if (p->output_section == NULL)
426 continue;
427 }
8851ecca 428 if (!parameters->options().relocatable())
7019cd25
ILT
429 {
430 // As noted above, when not generating an object file, we
431 // only scan allocated sections. We may see a non-allocated
432 // section here if we are emitting relocs.
433 if (p->is_data_section_allocated)
434 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
435 p->sh_type, p->contents->data(),
436 p->reloc_count, p->output_section,
437 p->needs_special_offset_handling,
438 this->local_symbol_count_,
439 local_symbols);
8851ecca 440 if (parameters->options().emit_relocs())
7019cd25
ILT
441 this->emit_relocs_scan(options, symtab, layout, local_symbols, p);
442 }
6a74a719
ILT
443 else
444 {
445 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
446 gold_assert(rr != NULL);
447 rr->set_reloc_count(p->reloc_count);
448 target->scan_relocatable_relocs(options, symtab, layout, this,
449 p->data_shndx, p->sh_type,
450 p->contents->data(),
451 p->reloc_count,
452 p->output_section,
453 p->needs_special_offset_handling,
454 this->local_symbol_count_,
455 local_symbols,
456 rr);
457 }
458
92e059d8
ILT
459 delete p->contents;
460 p->contents = NULL;
461 }
462
463 if (rd->local_symbols != NULL)
464 {
465 delete rd->local_symbols;
466 rd->local_symbols = NULL;
467 }
468}
469
7019cd25
ILT
470// This is a strategy class we use when scanning for --emit-relocs.
471
472template<int sh_type>
473class Emit_relocs_strategy
474{
475 public:
476 // A local non-section symbol.
477 inline Relocatable_relocs::Reloc_strategy
68943102 478 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
7019cd25
ILT
479 { return Relocatable_relocs::RELOC_COPY; }
480
481 // A local section symbol.
482 inline Relocatable_relocs::Reloc_strategy
483 local_section_strategy(unsigned int, Relobj*)
484 {
485 if (sh_type == elfcpp::SHT_RELA)
486 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
487 else
488 {
489 // The addend is stored in the section contents. Since this
490 // is not a relocatable link, we are going to apply the
491 // relocation contents to the section as usual. This means
492 // that we have no way to record the original addend. If the
493 // original addend is not zero, there is basically no way for
494 // the user to handle this correctly. Caveat emptor.
495 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
496 }
497 }
498
499 // A global symbol.
500 inline Relocatable_relocs::Reloc_strategy
501 global_strategy(unsigned int, Relobj*, unsigned int)
502 { return Relocatable_relocs::RELOC_COPY; }
503};
504
505// Scan the input relocations for --emit-relocs.
506
507template<int size, bool big_endian>
508void
509Sized_relobj<size, big_endian>::emit_relocs_scan(
510 const General_options& options,
511 Symbol_table* symtab,
512 Layout* layout,
513 const unsigned char* plocal_syms,
514 const Read_relocs_data::Relocs_list::iterator& p)
515{
516 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
517 gold_assert(rr != NULL);
518 rr->set_reloc_count(p->reloc_count);
519
520 if (p->sh_type == elfcpp::SHT_REL)
521 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(options, symtab, layout,
522 plocal_syms, p, rr);
523 else
524 {
525 gold_assert(p->sh_type == elfcpp::SHT_RELA);
526 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(options, symtab,
527 layout, plocal_syms, p,
528 rr);
529 }
530}
531
532// Scan the input relocation for --emit-relocs, templatized on the
533// type of the relocation section.
534
535template<int size, bool big_endian>
536template<int sh_type>
537void
538Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
539 const General_options& options,
540 Symbol_table* symtab,
541 Layout* layout,
542 const unsigned char* plocal_syms,
543 const Read_relocs_data::Relocs_list::iterator& p,
544 Relocatable_relocs* rr)
545{
546 scan_relocatable_relocs<size, big_endian, sh_type,
547 Emit_relocs_strategy<sh_type> >(
548 options,
549 symtab,
550 layout,
551 this,
552 p->data_shndx,
553 p->contents->data(),
554 p->reloc_count,
555 p->output_section,
556 p->needs_special_offset_handling,
557 this->local_symbol_count_,
558 plocal_syms,
559 rr);
560}
561
61ba1cf9
ILT
562// Relocate the input sections and write out the local symbols.
563
564template<int size, bool big_endian>
565void
f6ce93d6 566Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
61ba1cf9 567 const Symbol_table* symtab,
92e059d8 568 const Layout* layout,
61ba1cf9
ILT
569 Output_file* of)
570{
571 unsigned int shnum = this->shnum();
572
573 // Read the section headers.
645f8123 574 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
9eb9fa57 575 shnum * This::shdr_size,
39d0cb0e 576 true, true);
61ba1cf9
ILT
577
578 Views views;
579 views.resize(shnum);
580
581 // Make two passes over the sections. The first one copies the
582 // section data to the output file. The second one applies
583 // relocations.
584
585 this->write_sections(pshdrs, of, &views);
586
a9a60db6
ILT
587 // To speed up relocations, we set up hash tables for fast lookup of
588 // input offsets to output addresses.
589 this->initialize_input_to_output_maps();
590
61ba1cf9
ILT
591 // Apply relocations.
592
92e059d8 593 this->relocate_sections(options, symtab, layout, pshdrs, &views);
61ba1cf9 594
a9a60db6
ILT
595 // After we've done the relocations, we release the hash tables,
596 // since we no longer need them.
597 this->free_input_to_output_maps();
598
61ba1cf9
ILT
599 // Write out the accumulated views.
600 for (unsigned int i = 1; i < shnum; ++i)
601 {
602 if (views[i].view != NULL)
730cdc88 603 {
96803768
ILT
604 if (!views[i].is_postprocessing_view)
605 {
606 if (views[i].is_input_output_view)
607 of->write_input_output_view(views[i].offset,
608 views[i].view_size,
609 views[i].view);
610 else
611 of->write_output_view(views[i].offset, views[i].view_size,
612 views[i].view);
613 }
730cdc88 614 }
61ba1cf9
ILT
615 }
616
617 // Write out the local symbols.
d491d34e
ILT
618 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
619 layout->symtab_xindex(), layout->dynsym_xindex());
cb295612
ILT
620
621 // We should no longer need the local symbol values.
622 this->clear_local_symbols();
61ba1cf9
ILT
623}
624
cb295612
ILT
625// Sort a Read_multiple vector by file offset.
626struct Read_multiple_compare
627{
628 inline bool
629 operator()(const File_read::Read_multiple_entry& rme1,
630 const File_read::Read_multiple_entry& rme2) const
631 { return rme1.file_offset < rme2.file_offset; }
632};
633
61ba1cf9
ILT
634// Write section data to the output file. PSHDRS points to the
635// section headers. Record the views in *PVIEWS for use when
636// relocating.
637
638template<int size, bool big_endian>
639void
f6ce93d6 640Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
61ba1cf9 641 Output_file* of,
cb295612 642 Views* pviews)
61ba1cf9
ILT
643{
644 unsigned int shnum = this->shnum();
ef9beddf
ILT
645 const Output_sections& out_sections(this->output_sections());
646 const std::vector<Address>& out_offsets(this->section_offsets_);
61ba1cf9 647
cb295612
ILT
648 File_read::Read_multiple rm;
649 bool is_sorted = true;
650
61ba1cf9
ILT
651 const unsigned char* p = pshdrs + This::shdr_size;
652 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
653 {
654 View_size* pvs = &(*pviews)[i];
655
656 pvs->view = NULL;
657
ef9beddf 658 const Output_section* os = out_sections[i];
61ba1cf9
ILT
659 if (os == NULL)
660 continue;
ef9beddf 661 Address output_offset = out_offsets[i];
61ba1cf9
ILT
662
663 typename This::Shdr shdr(p);
664
665 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
666 continue;
667
8851ecca
ILT
668 if ((parameters->options().relocatable()
669 || parameters->options().emit_relocs())
6a74a719
ILT
670 && (shdr.get_sh_type() == elfcpp::SHT_REL
671 || shdr.get_sh_type() == elfcpp::SHT_RELA)
672 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
673 {
7019cd25
ILT
674 // This is a reloc section in a relocatable link or when
675 // emitting relocs. We don't need to read the input file.
676 // The size and file offset are stored in the
677 // Relocatable_relocs structure.
6a74a719
ILT
678 Relocatable_relocs* rr = this->relocatable_relocs(i);
679 gold_assert(rr != NULL);
680 Output_data* posd = rr->output_data();
681 gold_assert(posd != NULL);
682
683 pvs->offset = posd->offset();
684 pvs->view_size = posd->data_size();
685 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
686 pvs->address = posd->address();
687 pvs->is_input_output_view = false;
688 pvs->is_postprocessing_view = false;
689
690 continue;
691 }
692
96803768
ILT
693 // In the normal case, this input section is simply mapped to
694 // the output section at offset OUTPUT_OFFSET.
695
eff45813
CC
696 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
697 // handled specially--e.g., a .eh_frame section. The relocation
96803768
ILT
698 // routines need to check for each reloc where it should be
699 // applied. For this case, we need an input/output view for the
700 // entire contents of the section in the output file. We don't
701 // want to copy the contents of the input section to the output
702 // section; the output section contents were already written,
703 // and we waited for them in Relocate_task::is_runnable because
704 // relocs_must_follow_section_writes is set for the object.
705
706 // Regardless of which of the above cases is true, we have to
707 // check requires_postprocessing of the output section. If that
708 // is false, then we work with views of the output file
709 // directly. If it is true, then we work with a separate
710 // buffer, and the output section is responsible for writing the
711 // final data to the output file.
712
713 off_t output_section_offset;
ef9beddf 714 Address output_section_size;
96803768
ILT
715 if (!os->requires_postprocessing())
716 {
717 output_section_offset = os->offset();
ef9beddf 718 output_section_size = convert_types<Address, off_t>(os->data_size());
96803768
ILT
719 }
720 else
721 {
722 output_section_offset = 0;
ef9beddf
ILT
723 output_section_size =
724 convert_types<Address, off_t>(os->postprocessing_buffer_size());
96803768
ILT
725 }
726
730cdc88 727 off_t view_start;
fe8718a4 728 section_size_type view_size;
eff45813 729 if (output_offset != invalid_address)
730cdc88 730 {
96803768 731 view_start = output_section_offset + output_offset;
fe8718a4 732 view_size = convert_to_section_size_type(shdr.get_sh_size());
730cdc88
ILT
733 }
734 else
735 {
96803768 736 view_start = output_section_offset;
fe8718a4 737 view_size = convert_to_section_size_type(output_section_size);
730cdc88 738 }
61ba1cf9 739
730cdc88 740 if (view_size == 0)
ead1e424
ILT
741 continue;
742
eff45813 743 gold_assert(output_offset == invalid_address
ef9beddf 744 || output_offset + view_size <= output_section_size);
ead1e424 745
730cdc88 746 unsigned char* view;
96803768
ILT
747 if (os->requires_postprocessing())
748 {
749 unsigned char* buffer = os->postprocessing_buffer();
750 view = buffer + view_start;
eff45813 751 if (output_offset != invalid_address)
cb295612
ILT
752 {
753 off_t sh_offset = shdr.get_sh_offset();
754 if (!rm.empty() && rm.back().file_offset > sh_offset)
755 is_sorted = false;
756 rm.push_back(File_read::Read_multiple_entry(sh_offset,
757 view_size, view));
758 }
96803768 759 }
730cdc88
ILT
760 else
761 {
eff45813 762 if (output_offset == invalid_address)
96803768
ILT
763 view = of->get_input_output_view(view_start, view_size);
764 else
765 {
766 view = of->get_output_view(view_start, view_size);
cb295612
ILT
767 off_t sh_offset = shdr.get_sh_offset();
768 if (!rm.empty() && rm.back().file_offset > sh_offset)
769 is_sorted = false;
770 rm.push_back(File_read::Read_multiple_entry(sh_offset,
771 view_size, view));
96803768 772 }
730cdc88 773 }
92e059d8 774
61ba1cf9 775 pvs->view = view;
730cdc88 776 pvs->address = os->address();
eff45813 777 if (output_offset != invalid_address)
730cdc88
ILT
778 pvs->address += output_offset;
779 pvs->offset = view_start;
780 pvs->view_size = view_size;
eff45813 781 pvs->is_input_output_view = output_offset == invalid_address;
96803768 782 pvs->is_postprocessing_view = os->requires_postprocessing();
61ba1cf9 783 }
cb295612
ILT
784
785 // Actually read the data.
786 if (!rm.empty())
787 {
788 if (!is_sorted)
789 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
790 this->read_multiple(rm);
791 }
61ba1cf9
ILT
792}
793
794// Relocate section data. VIEWS points to the section data as views
795// in the output file.
796
797template<int size, bool big_endian>
798void
f6ce93d6 799Sized_relobj<size, big_endian>::relocate_sections(
92e059d8
ILT
800 const General_options& options,
801 const Symbol_table* symtab,
802 const Layout* layout,
803 const unsigned char* pshdrs,
804 Views* pviews)
61ba1cf9
ILT
805{
806 unsigned int shnum = this->shnum();
029ba973
ILT
807 Sized_target<size, big_endian>* target =
808 parameters->sized_target<size, big_endian>();
61ba1cf9 809
ef9beddf
ILT
810 const Output_sections& out_sections(this->output_sections());
811 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 812
92e059d8
ILT
813 Relocate_info<size, big_endian> relinfo;
814 relinfo.options = &options;
815 relinfo.symtab = symtab;
816 relinfo.layout = layout;
817 relinfo.object = this;
92e059d8 818
61ba1cf9
ILT
819 const unsigned char* p = pshdrs + This::shdr_size;
820 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
821 {
822 typename This::Shdr shdr(p);
823
824 unsigned int sh_type = shdr.get_sh_type();
825 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
826 continue;
827
1307d6cd
ILT
828 off_t sh_size = shdr.get_sh_size();
829 if (sh_size == 0)
830 continue;
831
d491d34e 832 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
61ba1cf9
ILT
833 if (index >= this->shnum())
834 {
75f2446e
ILT
835 this->error(_("relocation section %u has bad info %u"),
836 i, index);
837 continue;
61ba1cf9
ILT
838 }
839
ef9beddf 840 Output_section* os = out_sections[index];
730cdc88 841 if (os == NULL)
61ba1cf9
ILT
842 {
843 // This relocation section is against a section which we
844 // discarded.
845 continue;
846 }
ef9beddf 847 Address output_offset = out_offsets[index];
61ba1cf9 848
a3ad94ed 849 gold_assert((*pviews)[index].view != NULL);
8851ecca 850 if (parameters->options().relocatable())
6a74a719 851 gold_assert((*pviews)[i].view != NULL);
61ba1cf9 852
d491d34e 853 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
61ba1cf9 854 {
75f2446e
ILT
855 gold_error(_("relocation section %u uses unexpected "
856 "symbol table %u"),
d491d34e 857 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 858 continue;
61ba1cf9
ILT
859 }
860
61ba1cf9 861 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
39d0cb0e 862 sh_size, true, false);
61ba1cf9
ILT
863
864 unsigned int reloc_size;
865 if (sh_type == elfcpp::SHT_REL)
866 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
867 else
868 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
869
870 if (reloc_size != shdr.get_sh_entsize())
871 {
75f2446e
ILT
872 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
873 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
730cdc88 874 reloc_size);
75f2446e 875 continue;
61ba1cf9
ILT
876 }
877
878 size_t reloc_count = sh_size / reloc_size;
f5c3f225 879 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
61ba1cf9 880 {
75f2446e
ILT
881 gold_error(_("reloc section %u size %lu uneven"),
882 i, static_cast<unsigned long>(sh_size));
883 continue;
61ba1cf9
ILT
884 }
885
eff45813 886 gold_assert(output_offset != invalid_address
96803768
ILT
887 || this->relocs_must_follow_section_writes());
888
92e059d8
ILT
889 relinfo.reloc_shndx = i;
890 relinfo.data_shndx = index;
364c7fa5
ILT
891 unsigned char* view = (*pviews)[index].view;
892 Address address = (*pviews)[index].address;
893 section_size_type view_size = (*pviews)[index].view_size;
894
895 Reloc_symbol_changes* reloc_map = NULL;
896 if (this->uses_split_stack() && output_offset != invalid_address)
897 {
898 typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
899 if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
900 this->split_stack_adjust(symtab, pshdrs, sh_type, index,
901 prelocs, reloc_count, view, view_size,
902 &reloc_map);
903 }
904
8851ecca 905 if (!parameters->options().relocatable())
7019cd25 906 {
364c7fa5 907 target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
eff45813 908 output_offset == invalid_address,
364c7fa5 909 view, address, view_size, reloc_map);
8851ecca 910 if (parameters->options().emit_relocs())
7019cd25 911 this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count,
364c7fa5
ILT
912 os, output_offset, view, address, view_size,
913 (*pviews)[i].view, (*pviews)[i].view_size);
7019cd25 914 }
6a74a719
ILT
915 else
916 {
917 Relocatable_relocs* rr = this->relocatable_relocs(i);
364c7fa5
ILT
918 target->relocate_for_relocatable(&relinfo, sh_type, prelocs,
919 reloc_count, os, output_offset, rr,
920 view, address, view_size,
6a74a719
ILT
921 (*pviews)[i].view,
922 (*pviews)[i].view_size);
923 }
61ba1cf9
ILT
924 }
925}
926
7019cd25
ILT
927// Emit the relocs for --emit-relocs.
928
929template<int size, bool big_endian>
930void
931Sized_relobj<size, big_endian>::emit_relocs(
932 const Relocate_info<size, big_endian>* relinfo,
933 unsigned int i,
934 unsigned int sh_type,
935 const unsigned char* prelocs,
936 size_t reloc_count,
937 Output_section* output_section,
ef9beddf 938 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
7019cd25
ILT
939 unsigned char* view,
940 typename elfcpp::Elf_types<size>::Elf_Addr address,
941 section_size_type view_size,
942 unsigned char* reloc_view,
943 section_size_type reloc_view_size)
944{
945 if (sh_type == elfcpp::SHT_REL)
946 this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs,
947 reloc_count, output_section,
948 offset_in_output_section,
949 view, address, view_size,
950 reloc_view, reloc_view_size);
951 else
952 {
953 gold_assert(sh_type == elfcpp::SHT_RELA);
954 this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs,
955 reloc_count, output_section,
956 offset_in_output_section,
957 view, address, view_size,
958 reloc_view, reloc_view_size);
959 }
960}
961
962// Emit the relocs for --emit-relocs, templatized on the type of the
963// relocation section.
964
965template<int size, bool big_endian>
966template<int sh_type>
967void
968Sized_relobj<size, big_endian>::emit_relocs_reltype(
969 const Relocate_info<size, big_endian>* relinfo,
970 unsigned int i,
971 const unsigned char* prelocs,
972 size_t reloc_count,
973 Output_section* output_section,
ef9beddf 974 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
7019cd25
ILT
975 unsigned char* view,
976 typename elfcpp::Elf_types<size>::Elf_Addr address,
977 section_size_type view_size,
978 unsigned char* reloc_view,
979 section_size_type reloc_view_size)
980{
981 const Relocatable_relocs* rr = this->relocatable_relocs(i);
982 relocate_for_relocatable<size, big_endian, sh_type>(
983 relinfo,
984 prelocs,
985 reloc_count,
986 output_section,
987 offset_in_output_section,
988 rr,
989 view,
990 address,
991 view_size,
992 reloc_view,
993 reloc_view_size);
994}
995
a9a60db6
ILT
996// Create merge hash tables for the local symbols. These are used to
997// speed up relocations.
998
999template<int size, bool big_endian>
1000void
1001Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
1002{
1003 const unsigned int loccount = this->local_symbol_count_;
1004 for (unsigned int i = 1; i < loccount; ++i)
1005 {
1006 Symbol_value<size>& lv(this->local_values_[i]);
1007 lv.initialize_input_to_output_map(this);
1008 }
1009}
1010
1011// Free merge hash tables for the local symbols.
1012
1013template<int size, bool big_endian>
1014void
1015Sized_relobj<size, big_endian>::free_input_to_output_maps()
1016{
1017 const unsigned int loccount = this->local_symbol_count_;
1018 for (unsigned int i = 1; i < loccount; ++i)
1019 {
1020 Symbol_value<size>& lv(this->local_values_[i]);
1021 lv.free_input_to_output_map();
1022 }
1023}
1024
364c7fa5
ILT
1025// If an object was compiled with -fsplit-stack, this is called to
1026// check whether any relocations refer to functions defined in objects
1027// which were not compiled with -fsplit-stack. If they were, then we
1028// need to apply some target-specific adjustments to request
1029// additional stack space.
1030
1031template<int size, bool big_endian>
1032void
1033Sized_relobj<size, big_endian>::split_stack_adjust(
1034 const Symbol_table* symtab,
1035 const unsigned char* pshdrs,
1036 unsigned int sh_type,
1037 unsigned int shndx,
1038 const unsigned char* prelocs,
1039 size_t reloc_count,
1040 unsigned char* view,
1041 section_size_type view_size,
1042 Reloc_symbol_changes** reloc_map)
1043{
1044 if (sh_type == elfcpp::SHT_REL)
1045 this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
1046 prelocs, reloc_count,
1047 view, view_size,
1048 reloc_map);
1049 else
1050 {
1051 gold_assert(sh_type == elfcpp::SHT_RELA);
1052 this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
1053 prelocs, reloc_count,
1054 view, view_size,
1055 reloc_map);
1056 }
1057}
1058
1059// Adjust for -fsplit-stack, templatized on the type of the relocation
1060// section.
1061
1062template<int size, bool big_endian>
1063template<int sh_type>
1064void
1065Sized_relobj<size, big_endian>::split_stack_adjust_reltype(
1066 const Symbol_table* symtab,
1067 const unsigned char* pshdrs,
1068 unsigned int shndx,
1069 const unsigned char* prelocs,
1070 size_t reloc_count,
1071 unsigned char* view,
1072 section_size_type view_size,
1073 Reloc_symbol_changes** reloc_map)
1074{
1075 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
1076 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
1077
1078 size_t local_count = this->local_symbol_count();
1079
1080 std::vector<section_offset_type> non_split_refs;
1081
1082 const unsigned char* pr = prelocs;
1083 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1084 {
1085 Reltype reloc(pr);
1086
1087 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1088 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1089 if (r_sym < local_count)
1090 continue;
1091
1092 const Symbol* gsym = this->global_symbol(r_sym);
1093 gold_assert(gsym != NULL);
1094 if (gsym->is_forwarder())
1095 gsym = symtab->resolve_forwards(gsym);
1096
1097 // See if this relocation refers to a function defined in an
1098 // object compiled without -fsplit-stack. Note that we don't
1099 // care about the type of relocation--this means that in some
1100 // cases we will ask for a large stack unnecessarily, but this
1101 // is not fatal. FIXME: Some targets have symbols which are
1102 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1103 if (gsym->type() == elfcpp::STT_FUNC
1104 && !gsym->is_undefined()
1105 && gsym->source() == Symbol::FROM_OBJECT
1106 && !gsym->object()->uses_split_stack())
1107 {
1108 section_offset_type offset =
1109 convert_to_section_size_type(reloc.get_r_offset());
1110 non_split_refs.push_back(offset);
1111 }
1112 }
1113
1114 if (non_split_refs.empty())
1115 return;
1116
1117 // At this point, every entry in NON_SPLIT_REFS indicates a
1118 // relocation which refers to a function in an object compiled
1119 // without -fsplit-stack. We now have to convert that list into a
1120 // set of offsets to functions. First, we find all the functions.
1121
1122 Function_offsets function_offsets;
1123 this->find_functions(pshdrs, shndx, &function_offsets);
1124 if (function_offsets.empty())
1125 return;
1126
1127 // Now get a list of the function with references to non split-stack
1128 // code.
1129
1130 Function_offsets calls_non_split;
1131 for (std::vector<section_offset_type>::const_iterator p
1132 = non_split_refs.begin();
1133 p != non_split_refs.end();
1134 ++p)
1135 {
1136 Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
1137 if (low == function_offsets.end())
1138 --low;
1139 else if (low->first == *p)
1140 ;
1141 else if (low == function_offsets.begin())
1142 continue;
1143 else
1144 --low;
1145
1146 calls_non_split.insert(*low);
1147 }
1148 if (calls_non_split.empty())
1149 return;
1150
1151 // Now we have a set of functions to adjust. The adjustments are
1152 // target specific. Besides changing the output section view
1153 // however, it likes, the target may request a relocation change
1154 // from one global symbol name to another.
1155
1156 for (Function_offsets::const_iterator p = calls_non_split.begin();
1157 p != calls_non_split.end();
1158 ++p)
1159 {
1160 std::string from;
1161 std::string to;
1162 parameters->target().calls_non_split(this, shndx, p->first, p->second,
1163 view, view_size, &from, &to);
1164 if (!from.empty())
1165 {
1166 gold_assert(!to.empty());
1167 Symbol* tosym = NULL;
1168
1169 // Find relocations in the relevant function which are for
1170 // FROM.
1171 pr = prelocs;
1172 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1173 {
1174 Reltype reloc(pr);
1175
1176 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
1177 reloc.get_r_info();
1178 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1179 if (r_sym < local_count)
1180 continue;
1181
1182 section_offset_type offset =
1183 convert_to_section_size_type(reloc.get_r_offset());
1184 if (offset < p->first
1185 || (offset
1186 >= (p->first
1187 + static_cast<section_offset_type>(p->second))))
1188 continue;
1189
1190 const Symbol* gsym = this->global_symbol(r_sym);
1191 if (from == gsym->name())
1192 {
1193 if (tosym == NULL)
1194 {
1195 tosym = symtab->lookup(to.c_str());
1196 if (tosym == NULL)
1197 {
1198 this->error(_("could not convert call "
1199 "to '%s' to '%s'"),
1200 from.c_str(), to.c_str());
1201 break;
1202 }
1203 }
1204
1205 if (*reloc_map == NULL)
1206 *reloc_map = new Reloc_symbol_changes(reloc_count);
1207 (*reloc_map)->set(i, tosym);
1208 }
1209 }
1210 }
1211 }
1212}
1213
1214// Find all the function in this object defined in section SHNDX.
1215// Store their offsets in the section in FUNCTION_OFFSETS.
1216
1217template<int size, bool big_endian>
1218void
1219Sized_relobj<size, big_endian>::find_functions(
1220 const unsigned char* pshdrs,
1221 unsigned int shndx,
1222 Sized_relobj<size, big_endian>::Function_offsets* function_offsets)
1223{
1224 // We need to read the symbols to find the functions. If we wanted
1225 // to, we could cache reading the symbols across all sections in the
1226 // object.
1227 const unsigned int symtab_shndx = this->symtab_shndx_;
1228 typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
1229 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1230
1231 typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
1232 symtabshdr.get_sh_size();
1233 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1234 sh_size, true, true);
1235
1236 const int sym_size = This::sym_size;
1237 const unsigned int symcount = sh_size / sym_size;
1238 for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
1239 {
1240 typename elfcpp::Sym<size, big_endian> isym(psyms);
1241
1242 // FIXME: Some targets can have functions which do not have type
1243 // STT_FUNC, e.g., STT_ARM_TFUNC.
1244 if (isym.get_st_type() != elfcpp::STT_FUNC
1245 || isym.get_st_size() == 0)
1246 continue;
1247
1248 bool is_ordinary;
1249 unsigned int sym_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1250 &is_ordinary);
1251 if (!is_ordinary || sym_shndx != shndx)
1252 continue;
1253
1254 section_offset_type value =
1255 convert_to_section_size_type(isym.get_st_value());
1256 section_size_type fnsize =
1257 convert_to_section_size_type(isym.get_st_size());
1258
1259 (*function_offsets)[value] = fnsize;
1260 }
1261}
1262
a9a60db6
ILT
1263// Class Merged_symbol_value.
1264
1265template<int size>
1266void
1267Merged_symbol_value<size>::initialize_input_to_output_map(
1268 const Relobj* object,
1269 unsigned int input_shndx)
1270{
1271 Object_merge_map* map = object->merge_map();
1272 map->initialize_input_to_output_map<size>(input_shndx,
1273 this->output_start_address_,
1274 &this->output_addresses_);
1275}
1276
1277// Get the output value corresponding to an input offset if we
1278// couldn't find it in the hash table.
1279
1280template<int size>
1281typename elfcpp::Elf_types<size>::Elf_Addr
1282Merged_symbol_value<size>::value_from_output_section(
1283 const Relobj* object,
1284 unsigned int input_shndx,
1285 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1286{
1287 section_offset_type output_offset;
1288 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
1289 input_offset,
1290 &output_offset);
1291
1292 // If this assertion fails, it means that some relocation was
1293 // against a portion of an input merge section which we didn't map
1294 // to the output file and we didn't explicitly discard. We should
1295 // always map all portions of input merge sections.
1296 gold_assert(found);
1297
1298 if (output_offset == -1)
1299 return 0;
1300 else
1301 return this->output_start_address_ + output_offset;
1302}
1303
730cdc88
ILT
1304// Track_relocs methods.
1305
1306// Initialize the class to track the relocs. This gets the object,
1307// the reloc section index, and the type of the relocs. This returns
1308// false if something goes wrong.
1309
1310template<int size, bool big_endian>
1311bool
1312Track_relocs<size, big_endian>::initialize(
b696e6d4 1313 Object* object,
730cdc88
ILT
1314 unsigned int reloc_shndx,
1315 unsigned int reloc_type)
1316{
730cdc88
ILT
1317 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1318 // section for the .eh_frame section. We can't handle that case.
1319 if (reloc_shndx == -1U)
1320 return false;
1321
1322 // If RELOC_SHNDX is 0, there is no reloc section.
1323 if (reloc_shndx == 0)
1324 return true;
1325
1326 // Get the contents of the reloc section.
1327 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1328
1329 if (reloc_type == elfcpp::SHT_REL)
1330 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1331 else if (reloc_type == elfcpp::SHT_RELA)
1332 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1333 else
1334 gold_unreachable();
1335
1336 if (this->len_ % this->reloc_size_ != 0)
1337 {
1338 object->error(_("reloc section size %zu is not a multiple of "
1339 "reloc size %d\n"),
1340 static_cast<size_t>(this->len_),
1341 this->reloc_size_);
1342 return false;
1343 }
1344
1345 return true;
1346}
1347
1348// Return the offset of the next reloc, or -1 if there isn't one.
1349
1350template<int size, bool big_endian>
1351off_t
1352Track_relocs<size, big_endian>::next_offset() const
1353{
1354 if (this->pos_ >= this->len_)
1355 return -1;
1356
1357 // Rel and Rela start out the same, so we can always use Rel to find
1358 // the r_offset value.
1359 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1360 return rel.get_r_offset();
1361}
1362
1363// Return the index of the symbol referenced by the next reloc, or -1U
1364// if there aren't any more relocs.
1365
1366template<int size, bool big_endian>
1367unsigned int
1368Track_relocs<size, big_endian>::next_symndx() const
1369{
1370 if (this->pos_ >= this->len_)
1371 return -1U;
1372
1373 // Rel and Rela start out the same, so we can use Rel to find the
1374 // symbol index.
1375 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1376 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1377}
1378
1379// Advance to the next reloc whose r_offset is greater than or equal
1380// to OFFSET. Return the number of relocs we skip.
1381
1382template<int size, bool big_endian>
1383int
1384Track_relocs<size, big_endian>::advance(off_t offset)
1385{
1386 int ret = 0;
1387 while (this->pos_ < this->len_)
1388 {
1389 // Rel and Rela start out the same, so we can always use Rel to
1390 // find the r_offset value.
1391 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1392 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1393 break;
1394 ++ret;
1395 this->pos_ += this->reloc_size_;
1396 }
1397 return ret;
1398}
1399
12c0daef 1400// Instantiate the templates we need.
61ba1cf9 1401
193a53d9 1402#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1403template
1404void
f6ce93d6 1405Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1406#endif
92e059d8 1407
193a53d9 1408#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1409template
1410void
f6ce93d6 1411Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1412#endif
92e059d8 1413
193a53d9 1414#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1415template
1416void
f6ce93d6 1417Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1418#endif
92e059d8 1419
193a53d9 1420#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1421template
1422void
f6ce93d6 1423Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1424#endif
92e059d8 1425
6d03d481
ST
1426#ifdef HAVE_TARGET_32_LITTLE
1427template
1428void
1429Sized_relobj<32, false>::do_gc_process_relocs(const General_options& options,
1430 Symbol_table* symtab,
1431 Layout* layout,
1432 Read_relocs_data* rd);
1433#endif
1434
1435#ifdef HAVE_TARGET_32_BIG
1436template
1437void
1438Sized_relobj<32, true>::do_gc_process_relocs(const General_options& options,
1439 Symbol_table* symtab,
1440 Layout* layout,
1441 Read_relocs_data* rd);
1442#endif
1443
1444#ifdef HAVE_TARGET_64_LITTLE
1445template
1446void
1447Sized_relobj<64, false>::do_gc_process_relocs(const General_options& options,
1448 Symbol_table* symtab,
1449 Layout* layout,
1450 Read_relocs_data* rd);
1451#endif
1452
1453#ifdef HAVE_TARGET_64_BIG
1454template
1455void
1456Sized_relobj<64, true>::do_gc_process_relocs(const General_options& options,
1457 Symbol_table* symtab,
1458 Layout* layout,
1459 Read_relocs_data* rd);
1460#endif
1461
193a53d9 1462#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1463template
1464void
f6ce93d6 1465Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
92e059d8 1466 Symbol_table* symtab,
ead1e424 1467 Layout* layout,
92e059d8 1468 Read_relocs_data* rd);
193a53d9 1469#endif
92e059d8 1470
193a53d9 1471#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1472template
1473void
f6ce93d6 1474Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
92e059d8 1475 Symbol_table* symtab,
ead1e424 1476 Layout* layout,
92e059d8 1477 Read_relocs_data* rd);
193a53d9 1478#endif
92e059d8 1479
193a53d9 1480#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1481template
1482void
f6ce93d6 1483Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
92e059d8 1484 Symbol_table* symtab,
ead1e424 1485 Layout* layout,
92e059d8 1486 Read_relocs_data* rd);
193a53d9 1487#endif
92e059d8 1488
193a53d9 1489#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1490template
1491void
f6ce93d6 1492Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
92e059d8 1493 Symbol_table* symtab,
ead1e424 1494 Layout* layout,
92e059d8 1495 Read_relocs_data* rd);
193a53d9 1496#endif
92e059d8 1497
193a53d9 1498#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9
ILT
1499template
1500void
f6ce93d6 1501Sized_relobj<32, false>::do_relocate(const General_options& options,
61ba1cf9 1502 const Symbol_table* symtab,
92e059d8 1503 const Layout* layout,
61ba1cf9 1504 Output_file* of);
193a53d9 1505#endif
61ba1cf9 1506
193a53d9 1507#ifdef HAVE_TARGET_32_BIG
61ba1cf9
ILT
1508template
1509void
f6ce93d6 1510Sized_relobj<32, true>::do_relocate(const General_options& options,
61ba1cf9 1511 const Symbol_table* symtab,
92e059d8 1512 const Layout* layout,
61ba1cf9 1513 Output_file* of);
193a53d9 1514#endif
61ba1cf9 1515
193a53d9 1516#ifdef HAVE_TARGET_64_LITTLE
61ba1cf9
ILT
1517template
1518void
f6ce93d6 1519Sized_relobj<64, false>::do_relocate(const General_options& options,
61ba1cf9 1520 const Symbol_table* symtab,
92e059d8 1521 const Layout* layout,
61ba1cf9 1522 Output_file* of);
193a53d9 1523#endif
61ba1cf9 1524
193a53d9 1525#ifdef HAVE_TARGET_64_BIG
61ba1cf9
ILT
1526template
1527void
f6ce93d6 1528Sized_relobj<64, true>::do_relocate(const General_options& options,
61ba1cf9 1529 const Symbol_table* symtab,
92e059d8 1530 const Layout* layout,
61ba1cf9 1531 Output_file* of);
193a53d9 1532#endif
61ba1cf9 1533
a9a60db6
ILT
1534#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1535template
1536class Merged_symbol_value<32>;
1537#endif
1538
1539#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1540template
1541class Merged_symbol_value<64>;
1542#endif
1543
1544#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1545template
1546class Symbol_value<32>;
1547#endif
1548
1549#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1550template
1551class Symbol_value<64>;
1552#endif
1553
730cdc88
ILT
1554#ifdef HAVE_TARGET_32_LITTLE
1555template
1556class Track_relocs<32, false>;
1557#endif
1558
1559#ifdef HAVE_TARGET_32_BIG
1560template
1561class Track_relocs<32, true>;
1562#endif
1563
1564#ifdef HAVE_TARGET_64_LITTLE
1565template
1566class Track_relocs<64, false>;
1567#endif
1568
1569#ifdef HAVE_TARGET_64_BIG
1570template
1571class Track_relocs<64, true>;
1572#endif
1573
61ba1cf9 1574} // End namespace gold.
This page took 0.206117 seconds and 4 git commands to generate.