tmpfs - Fix tmpfs_fid, fix NFS exports
[dragonfly.git] / contrib / binutils-2.25 / gold / reloc.cc
blobb353d448e0cda654fa2c9afb518bf3ca617f1548
1 // reloc.cc -- relocate input files for gold.
3 // Copyright (C) 2006-2014 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <algorithm>
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"
39 namespace gold
42 // Read_relocs methods.
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.
48 Task_token*
49 Read_relocs::is_runnable()
51 return this->object_->is_locked() ? this->object_->token() : NULL;
54 // Lock the file.
56 void
57 Read_relocs::locks(Task_locker* tl)
59 Task_token* token = this->object_->token();
60 if (token != NULL)
61 tl->add(this, token);
64 // Read the relocations and then start a Scan_relocs_task.
66 void
67 Read_relocs::run(Workqueue* workqueue)
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();
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())
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_));
86 else
88 workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_,
89 this->object_, rd,
90 this->this_blocker_,
91 this->next_blocker_));
95 // Return a debugging name for the task.
97 std::string
98 Read_relocs::get_name() const
100 return "Read_relocs " + this->object_->name();
103 // Gc_process_relocs methods.
105 Gc_process_relocs::~Gc_process_relocs()
107 if (this->this_blocker_ != NULL)
108 delete this->this_blocker_;
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_.
116 Task_token*
117 Gc_process_relocs::is_runnable()
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;
126 void
127 Gc_process_relocs::locks(Task_locker* tl)
129 tl->add(this, this->object_->token());
130 tl->add(this, this->next_blocker_);
133 void
134 Gc_process_relocs::run(Workqueue*)
136 this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_);
137 this->object_->release();
140 // Return a debugging name for the task.
142 std::string
143 Gc_process_relocs::get_name() const
145 return "Gc_process_relocs " + this->object_->name();
148 // Scan_relocs methods.
150 Scan_relocs::~Scan_relocs()
152 if (this->this_blocker_ != NULL)
153 delete this->this_blocker_;
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.
161 Task_token*
162 Scan_relocs::is_runnable()
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;
171 // Return the locks we hold: one on the file, one on the symbol table
172 // and one blocker.
174 void
175 Scan_relocs::locks(Task_locker* tl)
177 Task_token* token = this->object_->token();
178 if (token != NULL)
179 tl->add(this, token);
180 tl->add(this, this->next_blocker_);
183 // Scan the relocs.
185 void
186 Scan_relocs::run(Workqueue*)
188 this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_);
189 delete this->rd_;
190 this->rd_ = NULL;
191 this->object_->release();
194 // Return a debugging name for the task.
196 std::string
197 Scan_relocs::get_name() const
199 return "Scan_relocs " + this->object_->name();
202 // Relocate_task methods.
204 // We may have to wait for the output sections to be written.
206 Task_token*
207 Relocate_task::is_runnable()
209 if (this->object_->relocs_must_follow_section_writes()
210 && this->output_sections_blocker_->is_blocked())
211 return this->output_sections_blocker_;
213 if (this->object_->is_locked())
214 return this->object_->token();
216 return NULL;
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.
223 void
224 Relocate_task::locks(Task_locker* tl)
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);
234 // Run the task.
236 void
237 Relocate_task::run(Workqueue*)
239 this->object_->relocate(this->symtab_, this->layout_, this->of_);
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();
245 this->object_->release();
248 // Return a debugging name for the task.
250 std::string
251 Relocate_task::get_name() const
253 return "Relocate_task " + this->object_->name();
256 // Read the relocs and local symbols from the object file and store
257 // the information in RD.
259 template<int size, bool big_endian>
260 void
261 Sized_relobj_file<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
263 rd->relocs.clear();
265 unsigned int shnum = this->shnum();
266 if (shnum == 0)
267 return;
269 rd->relocs.reserve(shnum / 2);
271 const Output_sections& out_sections(this->output_sections());
272 const std::vector<Address>& out_offsets(this->section_offsets());
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)
281 typename This::Shdr shdr(ps);
283 unsigned int sh_type = shdr.get_sh_type();
284 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
285 continue;
287 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
288 if (shndx >= shnum)
290 this->error(_("relocation section %u has bad info %u"),
291 i, shndx);
292 continue;
295 Output_section* os = out_sections[shndx];
296 if (os == NULL)
297 continue;
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;
315 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
317 this->error(_("relocation section %u uses unexpected "
318 "symbol table %u"),
319 i, this->adjust_shndx(shdr.get_sh_link()));
320 continue;
323 off_t sh_size = shdr.get_sh_size();
325 if (sh_size == 0)
326 continue;
328 unsigned int reloc_size;
329 if (sh_type == elfcpp::SHT_REL)
330 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
331 else
332 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
333 if (reloc_size != shdr.get_sh_entsize())
335 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
336 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
337 reloc_size);
338 continue;
341 size_t reloc_count = sh_size / reloc_size;
342 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
344 this->error(_("reloc section %u size %lu uneven"),
345 i, static_cast<unsigned long>(sh_size));
346 continue;
349 rd->relocs.push_back(Section_relocs());
350 Section_relocs& sr(rd->relocs.back());
351 sr.reloc_shndx = i;
352 sr.data_shndx = shndx;
353 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
354 true, true);
355 sr.sh_type = sh_type;
356 sr.reloc_count = reloc_count;
357 sr.output_section = os;
358 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
359 sr.is_data_section_allocated = is_section_allocated;
362 // Read the local symbols.
363 gold_assert(this->symtab_shndx_ != -1U);
364 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
365 rd->local_symbols = NULL;
366 else
368 typename This::Shdr symtabshdr(pshdrs
369 + this->symtab_shndx_ * This::shdr_size);
370 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
371 const int sym_size = This::sym_size;
372 const unsigned int loccount = this->local_symbol_count_;
373 gold_assert(loccount == symtabshdr.get_sh_info());
374 off_t locsize = loccount * sym_size;
375 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
376 locsize, true, true);
380 // Process the relocs to generate mappings from source sections to referenced
381 // sections. This is used during garbage collection to determine garbage
382 // sections.
384 template<int size, bool big_endian>
385 void
386 Sized_relobj_file<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab,
387 Layout* layout,
388 Read_relocs_data* rd)
390 Sized_target<size, big_endian>* target =
391 parameters->sized_target<size, big_endian>();
393 const unsigned char* local_symbols;
394 if (rd->local_symbols == NULL)
395 local_symbols = NULL;
396 else
397 local_symbols = rd->local_symbols->data();
399 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
400 p != rd->relocs.end();
401 ++p)
403 if (!parameters->options().relocatable())
405 // As noted above, when not generating an object file, we
406 // only scan allocated sections. We may see a non-allocated
407 // section here if we are emitting relocs.
408 if (p->is_data_section_allocated)
409 target->gc_process_relocs(symtab, layout, this,
410 p->data_shndx, p->sh_type,
411 p->contents->data(), p->reloc_count,
412 p->output_section,
413 p->needs_special_offset_handling,
414 this->local_symbol_count_,
415 local_symbols);
421 // Scan the relocs and adjust the symbol table. This looks for
422 // relocations which require GOT/PLT/COPY relocations.
424 template<int size, bool big_endian>
425 void
426 Sized_relobj_file<size, big_endian>::do_scan_relocs(Symbol_table* symtab,
427 Layout* layout,
428 Read_relocs_data* rd)
430 Sized_target<size, big_endian>* target =
431 parameters->sized_target<size, big_endian>();
433 const unsigned char* local_symbols;
434 if (rd->local_symbols == NULL)
435 local_symbols = NULL;
436 else
437 local_symbols = rd->local_symbols->data();
439 // For incremental links, allocate the counters for incremental relocations.
440 if (layout->incremental_inputs() != NULL)
441 this->allocate_incremental_reloc_counts();
443 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
444 p != rd->relocs.end();
445 ++p)
447 // When garbage collection is on, unreferenced sections are not included
448 // in the link that would have been included normally. This is known only
449 // after Read_relocs hence this check has to be done again.
450 if (parameters->options().gc_sections()
451 || parameters->options().icf_enabled())
453 if (p->output_section == NULL)
454 continue;
456 if (!parameters->options().relocatable())
458 // As noted above, when not generating an object file, we
459 // only scan allocated sections. We may see a non-allocated
460 // section here if we are emitting relocs.
461 if (p->is_data_section_allocated)
462 target->scan_relocs(symtab, layout, this, p->data_shndx,
463 p->sh_type, p->contents->data(),
464 p->reloc_count, p->output_section,
465 p->needs_special_offset_handling,
466 this->local_symbol_count_,
467 local_symbols);
468 if (parameters->options().emit_relocs())
469 this->emit_relocs_scan(symtab, layout, local_symbols, p);
470 if (layout->incremental_inputs() != NULL)
471 this->incremental_relocs_scan(p);
473 else
475 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
476 gold_assert(rr != NULL);
477 rr->set_reloc_count(p->reloc_count);
478 target->scan_relocatable_relocs(symtab, layout, this,
479 p->data_shndx, p->sh_type,
480 p->contents->data(),
481 p->reloc_count,
482 p->output_section,
483 p->needs_special_offset_handling,
484 this->local_symbol_count_,
485 local_symbols,
486 rr);
489 delete p->contents;
490 p->contents = NULL;
493 // For incremental links, finalize the allocation of relocations.
494 if (layout->incremental_inputs() != NULL)
495 this->finalize_incremental_relocs(layout, true);
497 if (rd->local_symbols != NULL)
499 delete rd->local_symbols;
500 rd->local_symbols = NULL;
504 // This is a strategy class we use when scanning for --emit-relocs.
506 template<int sh_type>
507 class Emit_relocs_strategy
509 public:
510 // A local non-section symbol.
511 inline Relocatable_relocs::Reloc_strategy
512 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
513 { return Relocatable_relocs::RELOC_COPY; }
515 // A local section symbol.
516 inline Relocatable_relocs::Reloc_strategy
517 local_section_strategy(unsigned int, Relobj*)
519 if (sh_type == elfcpp::SHT_RELA)
520 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
521 else
523 // The addend is stored in the section contents. Since this
524 // is not a relocatable link, we are going to apply the
525 // relocation contents to the section as usual. This means
526 // that we have no way to record the original addend. If the
527 // original addend is not zero, there is basically no way for
528 // the user to handle this correctly. Caveat emptor.
529 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
533 // A global symbol.
534 inline Relocatable_relocs::Reloc_strategy
535 global_strategy(unsigned int, Relobj*, unsigned int)
536 { return Relocatable_relocs::RELOC_COPY; }
539 // Scan the input relocations for --emit-relocs.
541 template<int size, bool big_endian>
542 void
543 Sized_relobj_file<size, big_endian>::emit_relocs_scan(
544 Symbol_table* symtab,
545 Layout* layout,
546 const unsigned char* plocal_syms,
547 const Read_relocs_data::Relocs_list::iterator& p)
549 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
550 gold_assert(rr != NULL);
551 rr->set_reloc_count(p->reloc_count);
553 if (p->sh_type == elfcpp::SHT_REL)
554 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout,
555 plocal_syms, p, rr);
556 else
558 gold_assert(p->sh_type == elfcpp::SHT_RELA);
559 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout,
560 plocal_syms, p, rr);
564 // Scan the input relocation for --emit-relocs, templatized on the
565 // type of the relocation section.
567 template<int size, bool big_endian>
568 template<int sh_type>
569 void
570 Sized_relobj_file<size, big_endian>::emit_relocs_scan_reltype(
571 Symbol_table* symtab,
572 Layout* layout,
573 const unsigned char* plocal_syms,
574 const Read_relocs_data::Relocs_list::iterator& p,
575 Relocatable_relocs* rr)
577 scan_relocatable_relocs<size, big_endian, sh_type,
578 Emit_relocs_strategy<sh_type> >(
579 symtab,
580 layout,
581 this,
582 p->data_shndx,
583 p->contents->data(),
584 p->reloc_count,
585 p->output_section,
586 p->needs_special_offset_handling,
587 this->local_symbol_count_,
588 plocal_syms,
589 rr);
592 // Scan the input relocations for --incremental.
594 template<int size, bool big_endian>
595 void
596 Sized_relobj_file<size, big_endian>::incremental_relocs_scan(
597 const Read_relocs_data::Relocs_list::iterator& p)
599 if (p->sh_type == elfcpp::SHT_REL)
600 this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p);
601 else
603 gold_assert(p->sh_type == elfcpp::SHT_RELA);
604 this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p);
608 // Scan the input relocation for --incremental, templatized on the
609 // type of the relocation section.
611 template<int size, bool big_endian>
612 template<int sh_type>
613 void
614 Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype(
615 const Read_relocs_data::Relocs_list::iterator& p)
617 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
618 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
619 const unsigned char* prelocs = p->contents->data();
620 size_t reloc_count = p->reloc_count;
622 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
624 Reltype reloc(prelocs);
626 if (p->needs_special_offset_handling
627 && !p->output_section->is_input_address_mapped(this, p->data_shndx,
628 reloc.get_r_offset()))
629 continue;
631 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
632 reloc.get_r_info();
633 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
635 if (r_sym >= this->local_symbol_count_)
636 this->count_incremental_reloc(r_sym - this->local_symbol_count_);
640 // Relocate the input sections and write out the local symbols.
642 template<int size, bool big_endian>
643 void
644 Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab,
645 const Layout* layout,
646 Output_file* of)
648 unsigned int shnum = this->shnum();
650 // Read the section headers.
651 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
652 shnum * This::shdr_size,
653 true, true);
655 Views views;
656 views.resize(shnum);
658 // Make two passes over the sections. The first one copies the
659 // section data to the output file. The second one applies
660 // relocations.
662 this->write_sections(layout, pshdrs, of, &views);
664 // To speed up relocations, we set up hash tables for fast lookup of
665 // input offsets to output addresses.
666 this->initialize_input_to_output_maps();
668 // Apply relocations.
670 this->relocate_sections(symtab, layout, pshdrs, of, &views);
672 // After we've done the relocations, we release the hash tables,
673 // since we no longer need them.
674 this->free_input_to_output_maps();
676 // Write out the accumulated views.
677 for (unsigned int i = 1; i < shnum; ++i)
679 if (views[i].view != NULL)
681 if (views[i].is_ctors_reverse_view)
682 this->reverse_words(views[i].view, views[i].view_size);
683 if (!views[i].is_postprocessing_view)
685 if (views[i].is_input_output_view)
686 of->write_input_output_view(views[i].offset,
687 views[i].view_size,
688 views[i].view);
689 else
690 of->write_output_view(views[i].offset, views[i].view_size,
691 views[i].view);
696 // Write out the local symbols.
697 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
698 layout->symtab_xindex(), layout->dynsym_xindex(),
699 layout->symtab_section_offset());
702 // Sort a Read_multiple vector by file offset.
703 struct Read_multiple_compare
705 inline bool
706 operator()(const File_read::Read_multiple_entry& rme1,
707 const File_read::Read_multiple_entry& rme2) const
708 { return rme1.file_offset < rme2.file_offset; }
711 // Write section data to the output file. PSHDRS points to the
712 // section headers. Record the views in *PVIEWS for use when
713 // relocating.
715 template<int size, bool big_endian>
716 void
717 Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout,
718 const unsigned char* pshdrs,
719 Output_file* of,
720 Views* pviews)
722 unsigned int shnum = this->shnum();
723 const Output_sections& out_sections(this->output_sections());
724 const std::vector<Address>& out_offsets(this->section_offsets());
726 File_read::Read_multiple rm;
727 bool is_sorted = true;
729 const unsigned char* p = pshdrs + This::shdr_size;
730 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
732 View_size* pvs = &(*pviews)[i];
734 pvs->view = NULL;
736 const Output_section* os = out_sections[i];
737 if (os == NULL)
738 continue;
739 Address output_offset = out_offsets[i];
741 typename This::Shdr shdr(p);
743 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
744 continue;
746 if ((parameters->options().relocatable()
747 || parameters->options().emit_relocs())
748 && (shdr.get_sh_type() == elfcpp::SHT_REL
749 || shdr.get_sh_type() == elfcpp::SHT_RELA)
750 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
752 // This is a reloc section in a relocatable link or when
753 // emitting relocs. We don't need to read the input file.
754 // The size and file offset are stored in the
755 // Relocatable_relocs structure.
756 Relocatable_relocs* rr = this->relocatable_relocs(i);
757 gold_assert(rr != NULL);
758 Output_data* posd = rr->output_data();
759 gold_assert(posd != NULL);
761 pvs->offset = posd->offset();
762 pvs->view_size = posd->data_size();
763 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
764 pvs->address = posd->address();
765 pvs->is_input_output_view = false;
766 pvs->is_postprocessing_view = false;
767 pvs->is_ctors_reverse_view = false;
769 continue;
772 // In the normal case, this input section is simply mapped to
773 // the output section at offset OUTPUT_OFFSET.
775 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
776 // handled specially--e.g., a .eh_frame section. The relocation
777 // routines need to check for each reloc where it should be
778 // applied. For this case, we need an input/output view for the
779 // entire contents of the section in the output file. We don't
780 // want to copy the contents of the input section to the output
781 // section; the output section contents were already written,
782 // and we waited for them in Relocate_task::is_runnable because
783 // relocs_must_follow_section_writes is set for the object.
785 // Regardless of which of the above cases is true, we have to
786 // check requires_postprocessing of the output section. If that
787 // is false, then we work with views of the output file
788 // directly. If it is true, then we work with a separate
789 // buffer, and the output section is responsible for writing the
790 // final data to the output file.
792 off_t output_section_offset;
793 Address output_section_size;
794 if (!os->requires_postprocessing())
796 output_section_offset = os->offset();
797 output_section_size = convert_types<Address, off_t>(os->data_size());
799 else
801 output_section_offset = 0;
802 output_section_size =
803 convert_types<Address, off_t>(os->postprocessing_buffer_size());
806 off_t view_start;
807 section_size_type view_size;
808 bool must_decompress = false;
809 if (output_offset != invalid_address)
811 view_start = output_section_offset + output_offset;
812 view_size = convert_to_section_size_type(shdr.get_sh_size());
813 section_size_type uncompressed_size;
814 if (this->section_is_compressed(i, &uncompressed_size))
816 view_size = uncompressed_size;
817 must_decompress = true;
820 else
822 view_start = output_section_offset;
823 view_size = convert_to_section_size_type(output_section_size);
826 if (view_size == 0)
827 continue;
829 gold_assert(output_offset == invalid_address
830 || output_offset + view_size <= output_section_size);
832 unsigned char* view;
833 if (os->requires_postprocessing())
835 unsigned char* buffer = os->postprocessing_buffer();
836 view = buffer + view_start;
837 if (output_offset != invalid_address && !must_decompress)
839 off_t sh_offset = shdr.get_sh_offset();
840 if (!rm.empty() && rm.back().file_offset > sh_offset)
841 is_sorted = false;
842 rm.push_back(File_read::Read_multiple_entry(sh_offset,
843 view_size, view));
846 else
848 if (output_offset == invalid_address)
849 view = of->get_input_output_view(view_start, view_size);
850 else
852 view = of->get_output_view(view_start, view_size);
853 if (!must_decompress)
855 off_t sh_offset = shdr.get_sh_offset();
856 if (!rm.empty() && rm.back().file_offset > sh_offset)
857 is_sorted = false;
858 rm.push_back(File_read::Read_multiple_entry(sh_offset,
859 view_size, view));
864 if (must_decompress)
866 // Read and decompress the section.
867 section_size_type len;
868 const unsigned char* p = this->section_contents(i, &len, false);
869 if (!decompress_input_section(p, len, view, view_size))
870 this->error(_("could not decompress section %s"),
871 this->section_name(i).c_str());
874 pvs->view = view;
875 pvs->address = os->address();
876 if (output_offset != invalid_address)
877 pvs->address += output_offset;
878 pvs->offset = view_start;
879 pvs->view_size = view_size;
880 pvs->is_input_output_view = output_offset == invalid_address;
881 pvs->is_postprocessing_view = os->requires_postprocessing();
882 pvs->is_ctors_reverse_view =
883 (!parameters->options().relocatable()
884 && view_size > size / 8
885 && (strcmp(os->name(), ".init_array") == 0
886 || strcmp(os->name(), ".fini_array") == 0)
887 && layout->is_ctors_in_init_array(this, i));
890 // Actually read the data.
891 if (!rm.empty())
893 if (!is_sorted)
894 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
895 this->read_multiple(rm);
899 // Relocate section data. VIEWS points to the section data as views
900 // in the output file.
902 template<int size, bool big_endian>
903 void
904 Sized_relobj_file<size, big_endian>::do_relocate_sections(
905 const Symbol_table* symtab,
906 const Layout* layout,
907 const unsigned char* pshdrs,
908 Output_file* of,
909 Views* pviews)
911 unsigned int shnum = this->shnum();
912 Sized_target<size, big_endian>* target =
913 parameters->sized_target<size, big_endian>();
915 const Output_sections& out_sections(this->output_sections());
916 const std::vector<Address>& out_offsets(this->section_offsets());
918 Relocate_info<size, big_endian> relinfo;
919 relinfo.symtab = symtab;
920 relinfo.layout = layout;
921 relinfo.object = this;
923 const unsigned char* p = pshdrs + This::shdr_size;
924 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
926 typename This::Shdr shdr(p);
928 unsigned int sh_type = shdr.get_sh_type();
929 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
930 continue;
932 off_t sh_size = shdr.get_sh_size();
933 if (sh_size == 0)
934 continue;
936 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
937 if (index >= this->shnum())
939 this->error(_("relocation section %u has bad info %u"),
940 i, index);
941 continue;
944 Output_section* os = out_sections[index];
945 if (os == NULL)
947 // This relocation section is against a section which we
948 // discarded.
949 continue;
951 Address output_offset = out_offsets[index];
953 gold_assert((*pviews)[index].view != NULL);
954 if (parameters->options().relocatable())
955 gold_assert((*pviews)[i].view != NULL);
957 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
959 gold_error(_("relocation section %u uses unexpected "
960 "symbol table %u"),
961 i, this->adjust_shndx(shdr.get_sh_link()));
962 continue;
965 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
966 sh_size, true, false);
968 unsigned int reloc_size;
969 if (sh_type == elfcpp::SHT_REL)
970 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
971 else
972 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
974 if (reloc_size != shdr.get_sh_entsize())
976 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
977 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
978 reloc_size);
979 continue;
982 size_t reloc_count = sh_size / reloc_size;
983 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
985 gold_error(_("reloc section %u size %lu uneven"),
986 i, static_cast<unsigned long>(sh_size));
987 continue;
990 gold_assert(output_offset != invalid_address
991 || this->relocs_must_follow_section_writes());
993 relinfo.reloc_shndx = i;
994 relinfo.reloc_shdr = p;
995 relinfo.data_shndx = index;
996 relinfo.data_shdr = pshdrs + index * This::shdr_size;
997 unsigned char* view = (*pviews)[index].view;
998 Address address = (*pviews)[index].address;
999 section_size_type view_size = (*pviews)[index].view_size;
1001 Reloc_symbol_changes* reloc_map = NULL;
1002 if (this->uses_split_stack() && output_offset != invalid_address)
1004 typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
1005 if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
1006 this->split_stack_adjust(symtab, pshdrs, sh_type, index,
1007 prelocs, reloc_count, view, view_size,
1008 &reloc_map);
1011 if (!parameters->options().relocatable())
1013 target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
1014 output_offset == invalid_address,
1015 view, address, view_size, reloc_map);
1016 if (parameters->options().emit_relocs())
1018 Relocatable_relocs* rr = this->relocatable_relocs(i);
1019 target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
1020 os, output_offset, rr,
1021 view, address, view_size,
1022 (*pviews)[i].view,
1023 (*pviews)[i].view_size);
1025 if (parameters->incremental())
1026 this->incremental_relocs_write(&relinfo, sh_type, prelocs,
1027 reloc_count, os, output_offset, of);
1029 else
1031 Relocatable_relocs* rr = this->relocatable_relocs(i);
1032 target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
1033 os, output_offset, rr,
1034 view, address, view_size,
1035 (*pviews)[i].view,
1036 (*pviews)[i].view_size);
1041 // Write the incremental relocs.
1043 template<int size, bool big_endian>
1044 void
1045 Sized_relobj_file<size, big_endian>::incremental_relocs_write(
1046 const Relocate_info<size, big_endian>* relinfo,
1047 unsigned int sh_type,
1048 const unsigned char* prelocs,
1049 size_t reloc_count,
1050 Output_section* output_section,
1051 Address output_offset,
1052 Output_file* of)
1054 if (sh_type == elfcpp::SHT_REL)
1055 this->incremental_relocs_write_reltype<elfcpp::SHT_REL>(
1056 relinfo,
1057 prelocs,
1058 reloc_count,
1059 output_section,
1060 output_offset,
1061 of);
1062 else
1064 gold_assert(sh_type == elfcpp::SHT_RELA);
1065 this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>(
1066 relinfo,
1067 prelocs,
1068 reloc_count,
1069 output_section,
1070 output_offset,
1071 of);
1075 // Write the incremental relocs, templatized on the type of the
1076 // relocation section.
1078 template<int size, bool big_endian>
1079 template<int sh_type>
1080 void
1081 Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype(
1082 const Relocate_info<size, big_endian>* relinfo,
1083 const unsigned char* prelocs,
1084 size_t reloc_count,
1085 Output_section* output_section,
1086 Address output_offset,
1087 Output_file* of)
1089 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
1090 const unsigned int reloc_size =
1091 Reloc_types<sh_type, size, big_endian>::reloc_size;
1092 const unsigned int sizeof_addr = size / 8;
1093 const unsigned int incr_reloc_size =
1094 Incremental_relocs_reader<size, big_endian>::reloc_size;
1096 unsigned int out_shndx = output_section->out_shndx();
1098 // Get a view for the .gnu_incremental_relocs section.
1100 Incremental_inputs* inputs = relinfo->layout->incremental_inputs();
1101 gold_assert(inputs != NULL);
1102 const off_t relocs_off = inputs->relocs_section()->offset();
1103 const off_t relocs_size = inputs->relocs_section()->data_size();
1104 unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
1106 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1108 Reloc reloc(prelocs);
1110 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1111 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1112 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1114 if (r_sym < this->local_symbol_count_)
1115 continue;
1117 // Get the new offset--the location in the output section where
1118 // this relocation should be applied.
1120 Address offset = reloc.get_r_offset();
1121 if (output_offset != invalid_address)
1122 offset += output_offset;
1123 else
1125 section_offset_type sot_offset =
1126 convert_types<section_offset_type, Address>(offset);
1127 section_offset_type new_sot_offset =
1128 output_section->output_offset(relinfo->object,
1129 relinfo->data_shndx,
1130 sot_offset);
1131 gold_assert(new_sot_offset != -1);
1132 offset += new_sot_offset;
1135 // Get the addend.
1136 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
1137 if (sh_type == elfcpp::SHT_RELA)
1138 addend =
1139 Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc);
1140 else
1142 // FIXME: Get the addend for SHT_REL.
1143 addend = 0;
1146 // Get the index of the output relocation.
1148 unsigned int reloc_index =
1149 this->next_incremental_reloc_index(r_sym - this->local_symbol_count_);
1151 // Write the relocation.
1153 unsigned char* pov = view + reloc_index * incr_reloc_size;
1154 elfcpp::Swap<32, big_endian>::writeval(pov, r_type);
1155 elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx);
1156 elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset);
1157 elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend);
1158 of->write_output_view(pov - view, incr_reloc_size, view);
1162 // Create merge hash tables for the local symbols. These are used to
1163 // speed up relocations.
1165 template<int size, bool big_endian>
1166 void
1167 Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps()
1169 const unsigned int loccount = this->local_symbol_count_;
1170 for (unsigned int i = 1; i < loccount; ++i)
1172 Symbol_value<size>& lv(this->local_values_[i]);
1173 lv.initialize_input_to_output_map(this);
1177 // Free merge hash tables for the local symbols.
1179 template<int size, bool big_endian>
1180 void
1181 Sized_relobj_file<size, big_endian>::free_input_to_output_maps()
1183 const unsigned int loccount = this->local_symbol_count_;
1184 for (unsigned int i = 1; i < loccount; ++i)
1186 Symbol_value<size>& lv(this->local_values_[i]);
1187 lv.free_input_to_output_map();
1191 // If an object was compiled with -fsplit-stack, this is called to
1192 // check whether any relocations refer to functions defined in objects
1193 // which were not compiled with -fsplit-stack. If they were, then we
1194 // need to apply some target-specific adjustments to request
1195 // additional stack space.
1197 template<int size, bool big_endian>
1198 void
1199 Sized_relobj_file<size, big_endian>::split_stack_adjust(
1200 const Symbol_table* symtab,
1201 const unsigned char* pshdrs,
1202 unsigned int sh_type,
1203 unsigned int shndx,
1204 const unsigned char* prelocs,
1205 size_t reloc_count,
1206 unsigned char* view,
1207 section_size_type view_size,
1208 Reloc_symbol_changes** reloc_map)
1210 if (sh_type == elfcpp::SHT_REL)
1211 this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
1212 prelocs, reloc_count,
1213 view, view_size,
1214 reloc_map);
1215 else
1217 gold_assert(sh_type == elfcpp::SHT_RELA);
1218 this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
1219 prelocs, reloc_count,
1220 view, view_size,
1221 reloc_map);
1225 // Adjust for -fsplit-stack, templatized on the type of the relocation
1226 // section.
1228 template<int size, bool big_endian>
1229 template<int sh_type>
1230 void
1231 Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype(
1232 const Symbol_table* symtab,
1233 const unsigned char* pshdrs,
1234 unsigned int shndx,
1235 const unsigned char* prelocs,
1236 size_t reloc_count,
1237 unsigned char* view,
1238 section_size_type view_size,
1239 Reloc_symbol_changes** reloc_map)
1241 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
1242 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
1244 size_t local_count = this->local_symbol_count();
1246 std::vector<section_offset_type> non_split_refs;
1248 const unsigned char* pr = prelocs;
1249 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1251 Reltype reloc(pr);
1253 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1254 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1255 if (r_sym < local_count)
1256 continue;
1258 const Symbol* gsym = this->global_symbol(r_sym);
1259 gold_assert(gsym != NULL);
1260 if (gsym->is_forwarder())
1261 gsym = symtab->resolve_forwards(gsym);
1263 // See if this relocation refers to a function defined in an
1264 // object compiled without -fsplit-stack. Note that we don't
1265 // care about the type of relocation--this means that in some
1266 // cases we will ask for a large stack unnecessarily, but this
1267 // is not fatal. FIXME: Some targets have symbols which are
1268 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1269 if (!gsym->is_undefined()
1270 && gsym->source() == Symbol::FROM_OBJECT
1271 && !gsym->object()->uses_split_stack())
1273 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1274 if (parameters->target().is_call_to_non_split(gsym, r_type))
1276 section_offset_type offset =
1277 convert_to_section_size_type(reloc.get_r_offset());
1278 non_split_refs.push_back(offset);
1283 if (non_split_refs.empty())
1284 return;
1286 // At this point, every entry in NON_SPLIT_REFS indicates a
1287 // relocation which refers to a function in an object compiled
1288 // without -fsplit-stack. We now have to convert that list into a
1289 // set of offsets to functions. First, we find all the functions.
1291 Function_offsets function_offsets;
1292 this->find_functions(pshdrs, shndx, &function_offsets);
1293 if (function_offsets.empty())
1294 return;
1296 // Now get a list of the function with references to non split-stack
1297 // code.
1299 Function_offsets calls_non_split;
1300 for (std::vector<section_offset_type>::const_iterator p
1301 = non_split_refs.begin();
1302 p != non_split_refs.end();
1303 ++p)
1305 Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
1306 if (low == function_offsets.end())
1307 --low;
1308 else if (low->first == *p)
1310 else if (low == function_offsets.begin())
1311 continue;
1312 else
1313 --low;
1315 calls_non_split.insert(*low);
1317 if (calls_non_split.empty())
1318 return;
1320 // Now we have a set of functions to adjust. The adjustments are
1321 // target specific. Besides changing the output section view
1322 // however, it likes, the target may request a relocation change
1323 // from one global symbol name to another.
1325 for (Function_offsets::const_iterator p = calls_non_split.begin();
1326 p != calls_non_split.end();
1327 ++p)
1329 std::string from;
1330 std::string to;
1331 parameters->target().calls_non_split(this, shndx, p->first, p->second,
1332 view, view_size, &from, &to);
1333 if (!from.empty())
1335 gold_assert(!to.empty());
1336 Symbol* tosym = NULL;
1338 // Find relocations in the relevant function which are for
1339 // FROM.
1340 pr = prelocs;
1341 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1343 Reltype reloc(pr);
1345 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
1346 reloc.get_r_info();
1347 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1348 if (r_sym < local_count)
1349 continue;
1351 section_offset_type offset =
1352 convert_to_section_size_type(reloc.get_r_offset());
1353 if (offset < p->first
1354 || (offset
1355 >= (p->first
1356 + static_cast<section_offset_type>(p->second))))
1357 continue;
1359 const Symbol* gsym = this->global_symbol(r_sym);
1360 if (from == gsym->name())
1362 if (tosym == NULL)
1364 tosym = symtab->lookup(to.c_str());
1365 if (tosym == NULL)
1367 this->error(_("could not convert call "
1368 "to '%s' to '%s'"),
1369 from.c_str(), to.c_str());
1370 break;
1374 if (*reloc_map == NULL)
1375 *reloc_map = new Reloc_symbol_changes(reloc_count);
1376 (*reloc_map)->set(i, tosym);
1383 // Find all the function in this object defined in section SHNDX.
1384 // Store their offsets in the section in FUNCTION_OFFSETS.
1386 template<int size, bool big_endian>
1387 void
1388 Sized_relobj_file<size, big_endian>::find_functions(
1389 const unsigned char* pshdrs,
1390 unsigned int shndx,
1391 Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets)
1393 // We need to read the symbols to find the functions. If we wanted
1394 // to, we could cache reading the symbols across all sections in the
1395 // object.
1396 const unsigned int symtab_shndx = this->symtab_shndx_;
1397 typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
1398 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1400 typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
1401 symtabshdr.get_sh_size();
1402 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1403 sh_size, true, true);
1405 const int sym_size = This::sym_size;
1406 const unsigned int symcount = sh_size / sym_size;
1407 for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
1409 typename elfcpp::Sym<size, big_endian> isym(psyms);
1411 // FIXME: Some targets can have functions which do not have type
1412 // STT_FUNC, e.g., STT_ARM_TFUNC.
1413 if (isym.get_st_type() != elfcpp::STT_FUNC
1414 || isym.get_st_size() == 0)
1415 continue;
1417 bool is_ordinary;
1418 Symbol_location loc;
1419 loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1420 &is_ordinary);
1421 if (!is_ordinary)
1422 continue;
1424 loc.object = this;
1425 loc.offset = isym.get_st_value();
1426 parameters->target().function_location(&loc);
1428 if (loc.shndx != shndx)
1429 continue;
1431 section_offset_type value =
1432 convert_to_section_size_type(loc.offset);
1433 section_size_type fnsize =
1434 convert_to_section_size_type(isym.get_st_size());
1436 (*function_offsets)[value] = fnsize;
1440 // Reverse the words in a section. Used for .ctors sections mapped to
1441 // .init_array sections. See ctors_sections_in_init_array in
1442 // layout.cc.
1444 template<int size, bool big_endian>
1445 void
1446 Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view,
1447 section_size_type view_size)
1449 typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1450 Valtype* vview = reinterpret_cast<Valtype*>(view);
1451 section_size_type vview_size = view_size / (size / 8);
1452 for (section_size_type i = 0; i < vview_size / 2; ++i)
1454 Valtype tmp = vview[i];
1455 vview[i] = vview[vview_size - 1 - i];
1456 vview[vview_size - 1 - i] = tmp;
1460 // Class Merged_symbol_value.
1462 template<int size>
1463 void
1464 Merged_symbol_value<size>::initialize_input_to_output_map(
1465 const Relobj* object,
1466 unsigned int input_shndx)
1468 Object_merge_map* map = object->merge_map();
1469 map->initialize_input_to_output_map<size>(input_shndx,
1470 this->output_start_address_,
1471 &this->output_addresses_);
1474 // Get the output value corresponding to an input offset if we
1475 // couldn't find it in the hash table.
1477 template<int size>
1478 typename elfcpp::Elf_types<size>::Elf_Addr
1479 Merged_symbol_value<size>::value_from_output_section(
1480 const Relobj* object,
1481 unsigned int input_shndx,
1482 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1484 section_offset_type output_offset;
1485 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
1486 input_offset,
1487 &output_offset);
1489 // If this assertion fails, it means that some relocation was
1490 // against a portion of an input merge section which we didn't map
1491 // to the output file and we didn't explicitly discard. We should
1492 // always map all portions of input merge sections.
1493 gold_assert(found);
1495 if (output_offset == -1)
1496 return 0;
1497 else
1498 return this->output_start_address_ + output_offset;
1501 // Track_relocs methods.
1503 // Initialize the class to track the relocs. This gets the object,
1504 // the reloc section index, and the type of the relocs. This returns
1505 // false if something goes wrong.
1507 template<int size, bool big_endian>
1508 bool
1509 Track_relocs<size, big_endian>::initialize(
1510 Object* object,
1511 unsigned int reloc_shndx,
1512 unsigned int reloc_type)
1514 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1515 // section for the .eh_frame section. We can't handle that case.
1516 if (reloc_shndx == -1U)
1517 return false;
1519 // If RELOC_SHNDX is 0, there is no reloc section.
1520 if (reloc_shndx == 0)
1521 return true;
1523 // Get the contents of the reloc section.
1524 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1526 if (reloc_type == elfcpp::SHT_REL)
1527 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1528 else if (reloc_type == elfcpp::SHT_RELA)
1529 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1530 else
1531 gold_unreachable();
1533 if (this->len_ % this->reloc_size_ != 0)
1535 object->error(_("reloc section size %zu is not a multiple of "
1536 "reloc size %d\n"),
1537 static_cast<size_t>(this->len_),
1538 this->reloc_size_);
1539 return false;
1542 return true;
1545 // Return the offset of the next reloc, or -1 if there isn't one.
1547 template<int size, bool big_endian>
1548 off_t
1549 Track_relocs<size, big_endian>::next_offset() const
1551 if (this->pos_ >= this->len_)
1552 return -1;
1554 // Rel and Rela start out the same, so we can always use Rel to find
1555 // the r_offset value.
1556 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1557 return rel.get_r_offset();
1560 // Return the index of the symbol referenced by the next reloc, or -1U
1561 // if there aren't any more relocs.
1563 template<int size, bool big_endian>
1564 unsigned int
1565 Track_relocs<size, big_endian>::next_symndx() const
1567 if (this->pos_ >= this->len_)
1568 return -1U;
1570 // Rel and Rela start out the same, so we can use Rel to find the
1571 // symbol index.
1572 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1573 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1576 // Return the addend of the next reloc, or 0 if there isn't one.
1578 template<int size, bool big_endian>
1579 uint64_t
1580 Track_relocs<size, big_endian>::next_addend() const
1582 if (this->pos_ >= this->len_)
1583 return 0;
1584 if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size)
1585 return 0;
1586 elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_);
1587 return rela.get_r_addend();
1590 // Advance to the next reloc whose r_offset is greater than or equal
1591 // to OFFSET. Return the number of relocs we skip.
1593 template<int size, bool big_endian>
1595 Track_relocs<size, big_endian>::advance(off_t offset)
1597 int ret = 0;
1598 while (this->pos_ < this->len_)
1600 // Rel and Rela start out the same, so we can always use Rel to
1601 // find the r_offset value.
1602 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1603 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1604 break;
1605 ++ret;
1606 this->pos_ += this->reloc_size_;
1608 return ret;
1611 // Instantiate the templates we need.
1613 #ifdef HAVE_TARGET_32_LITTLE
1614 template
1615 void
1616 Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd);
1617 #endif
1619 #ifdef HAVE_TARGET_32_BIG
1620 template
1621 void
1622 Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd);
1623 #endif
1625 #ifdef HAVE_TARGET_64_LITTLE
1626 template
1627 void
1628 Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd);
1629 #endif
1631 #ifdef HAVE_TARGET_64_BIG
1632 template
1633 void
1634 Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd);
1635 #endif
1637 #ifdef HAVE_TARGET_32_LITTLE
1638 template
1639 void
1640 Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab,
1641 Layout* layout,
1642 Read_relocs_data* rd);
1643 #endif
1645 #ifdef HAVE_TARGET_32_BIG
1646 template
1647 void
1648 Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab,
1649 Layout* layout,
1650 Read_relocs_data* rd);
1651 #endif
1653 #ifdef HAVE_TARGET_64_LITTLE
1654 template
1655 void
1656 Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab,
1657 Layout* layout,
1658 Read_relocs_data* rd);
1659 #endif
1661 #ifdef HAVE_TARGET_64_BIG
1662 template
1663 void
1664 Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab,
1665 Layout* layout,
1666 Read_relocs_data* rd);
1667 #endif
1669 #ifdef HAVE_TARGET_32_LITTLE
1670 template
1671 void
1672 Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab,
1673 Layout* layout,
1674 Read_relocs_data* rd);
1675 #endif
1677 #ifdef HAVE_TARGET_32_BIG
1678 template
1679 void
1680 Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab,
1681 Layout* layout,
1682 Read_relocs_data* rd);
1683 #endif
1685 #ifdef HAVE_TARGET_64_LITTLE
1686 template
1687 void
1688 Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab,
1689 Layout* layout,
1690 Read_relocs_data* rd);
1691 #endif
1693 #ifdef HAVE_TARGET_64_BIG
1694 template
1695 void
1696 Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab,
1697 Layout* layout,
1698 Read_relocs_data* rd);
1699 #endif
1701 #ifdef HAVE_TARGET_32_LITTLE
1702 template
1703 void
1704 Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab,
1705 const Layout* layout,
1706 Output_file* of);
1707 #endif
1709 #ifdef HAVE_TARGET_32_BIG
1710 template
1711 void
1712 Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab,
1713 const Layout* layout,
1714 Output_file* of);
1715 #endif
1717 #ifdef HAVE_TARGET_64_LITTLE
1718 template
1719 void
1720 Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab,
1721 const Layout* layout,
1722 Output_file* of);
1723 #endif
1725 #ifdef HAVE_TARGET_64_BIG
1726 template
1727 void
1728 Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab,
1729 const Layout* layout,
1730 Output_file* of);
1731 #endif
1733 #ifdef HAVE_TARGET_32_LITTLE
1734 template
1735 void
1736 Sized_relobj_file<32, false>::do_relocate_sections(
1737 const Symbol_table* symtab,
1738 const Layout* layout,
1739 const unsigned char* pshdrs,
1740 Output_file* of,
1741 Views* pviews);
1742 #endif
1744 #ifdef HAVE_TARGET_32_BIG
1745 template
1746 void
1747 Sized_relobj_file<32, true>::do_relocate_sections(
1748 const Symbol_table* symtab,
1749 const Layout* layout,
1750 const unsigned char* pshdrs,
1751 Output_file* of,
1752 Views* pviews);
1753 #endif
1755 #ifdef HAVE_TARGET_64_LITTLE
1756 template
1757 void
1758 Sized_relobj_file<64, false>::do_relocate_sections(
1759 const Symbol_table* symtab,
1760 const Layout* layout,
1761 const unsigned char* pshdrs,
1762 Output_file* of,
1763 Views* pviews);
1764 #endif
1766 #ifdef HAVE_TARGET_64_BIG
1767 template
1768 void
1769 Sized_relobj_file<64, true>::do_relocate_sections(
1770 const Symbol_table* symtab,
1771 const Layout* layout,
1772 const unsigned char* pshdrs,
1773 Output_file* of,
1774 Views* pviews);
1775 #endif
1777 #ifdef HAVE_TARGET_32_LITTLE
1778 template
1779 void
1780 Sized_relobj_file<32, false>::initialize_input_to_output_maps();
1782 template
1783 void
1784 Sized_relobj_file<32, false>::free_input_to_output_maps();
1785 #endif
1787 #ifdef HAVE_TARGET_32_BIG
1788 template
1789 void
1790 Sized_relobj_file<32, true>::initialize_input_to_output_maps();
1792 template
1793 void
1794 Sized_relobj_file<32, true>::free_input_to_output_maps();
1795 #endif
1797 #ifdef HAVE_TARGET_64_LITTLE
1798 template
1799 void
1800 Sized_relobj_file<64, false>::initialize_input_to_output_maps();
1802 template
1803 void
1804 Sized_relobj_file<64, false>::free_input_to_output_maps();
1805 #endif
1807 #ifdef HAVE_TARGET_64_BIG
1808 template
1809 void
1810 Sized_relobj_file<64, true>::initialize_input_to_output_maps();
1812 template
1813 void
1814 Sized_relobj_file<64, true>::free_input_to_output_maps();
1815 #endif
1817 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1818 template
1819 class Merged_symbol_value<32>;
1820 #endif
1822 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1823 template
1824 class Merged_symbol_value<64>;
1825 #endif
1827 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1828 template
1829 class Symbol_value<32>;
1830 #endif
1832 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1833 template
1834 class Symbol_value<64>;
1835 #endif
1837 #ifdef HAVE_TARGET_32_LITTLE
1838 template
1839 class Track_relocs<32, false>;
1840 #endif
1842 #ifdef HAVE_TARGET_32_BIG
1843 template
1844 class Track_relocs<32, true>;
1845 #endif
1847 #ifdef HAVE_TARGET_64_LITTLE
1848 template
1849 class Track_relocs<64, false>;
1850 #endif
1852 #ifdef HAVE_TARGET_64_BIG
1853 template
1854 class Track_relocs<64, true>;
1855 #endif
1857 } // End namespace gold.