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