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