Rewrite workqueue. This version eliminates the master thread, and
[external/binutils.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 // Copyright 2006, 2007 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 "workqueue.h"
26 #include "object.h"
27 #include "symtab.h"
28 #include "output.h"
29 #include "reloc.h"
30
31 namespace gold
32 {
33
34 // Read_relocs methods.
35
36 // These tasks just read the relocation information from the file.
37 // After reading it, the start another task to process the
38 // information.  These tasks requires access to the file.
39
40 Task_token*
41 Read_relocs::is_runnable()
42 {
43   return this->object_->is_locked() ? this->object_->token() : NULL;
44 }
45
46 // Lock the file.
47
48 void
49 Read_relocs::locks(Task_locker* tl)
50 {
51   tl->add(this, this->object_->token());
52 }
53
54 // Read the relocations and then start a Scan_relocs_task.
55
56 void
57 Read_relocs::run(Workqueue* workqueue)
58 {
59   Read_relocs_data *rd = new Read_relocs_data;
60   this->object_->read_relocs(rd);
61   this->object_->release();
62
63   workqueue->queue_front(new Scan_relocs(this->options_, this->symtab_,
64                                          this->layout_, this->object_, rd,
65                                          this->symtab_lock_, this->blocker_));
66 }
67
68 // Return a debugging name for the task.
69
70 std::string
71 Read_relocs::get_name() const
72 {
73   return "Read_relocs " + this->object_->name();
74 }
75
76 // Scan_relocs methods.
77
78 // These tasks scan the relocations read by Read_relocs and mark up
79 // the symbol table to indicate which relocations are required.  We
80 // use a lock on the symbol table to keep them from interfering with
81 // each other.
82
83 Task_token*
84 Scan_relocs::is_runnable()
85 {
86   if (!this->symtab_lock_->is_writable())
87     return this->symtab_lock_;
88   if (this->object_->is_locked())
89     return this->object_->token();
90   return NULL;
91 }
92
93 // Return the locks we hold: one on the file, one on the symbol table
94 // and one blocker.
95
96 void
97 Scan_relocs::locks(Task_locker* tl)
98 {
99   tl->add(this, this->object_->token());
100   tl->add(this, this->symtab_lock_);
101   tl->add(this, this->blocker_);
102 }
103
104 // Scan the relocs.
105
106 void
107 Scan_relocs::run(Workqueue*)
108 {
109   this->object_->scan_relocs(this->options_, this->symtab_, this->layout_,
110                              this->rd_);
111   this->object_->release();
112   delete this->rd_;
113   this->rd_ = NULL;
114 }
115
116 // Return a debugging name for the task.
117
118 std::string
119 Scan_relocs::get_name() const
120 {
121   return "Scan_relocs " + this->object_->name();
122 }
123
124 // Relocate_task methods.
125
126 // We may have to wait for the output sections to be written.
127
128 Task_token*
129 Relocate_task::is_runnable()
130 {
131   if (this->object_->relocs_must_follow_section_writes()
132       && this->output_sections_blocker_->is_blocked())
133     return this->output_sections_blocker_;
134
135   if (this->object_->is_locked())
136     return this->object_->token();
137
138   return NULL;
139 }
140
141 // We want to lock the file while we run.  We want to unblock
142 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
143 // INPUT_SECTIONS_BLOCKER may be NULL.
144
145 void
146 Relocate_task::locks(Task_locker* tl)
147 {
148   if (this->input_sections_blocker_ != NULL)
149     tl->add(this, this->input_sections_blocker_);
150   tl->add(this, this->final_blocker_);
151   tl->add(this, this->object_->token());
152 }
153
154 // Run the task.
155
156 void
157 Relocate_task::run(Workqueue*)
158 {
159   this->object_->relocate(this->options_, this->symtab_, this->layout_,
160                           this->of_);
161   this->object_->release();
162 }
163
164 // Return a debugging name for the task.
165
166 std::string
167 Relocate_task::get_name() const
168 {
169   return "Relocate_task " + this->object_->name();
170 }
171
172 // Read the relocs and local symbols from the object file and store
173 // the information in RD.
174
175 template<int size, bool big_endian>
176 void
177 Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
178 {
179   rd->relocs.clear();
180
181   unsigned int shnum = this->shnum();
182   if (shnum == 0)
183     return;
184
185   rd->relocs.reserve(shnum / 2);
186
187   std::vector<Map_to_output>& map_sections(this->map_to_output());
188
189   const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
190                                                shnum * This::shdr_size,
191                                                true);
192   // Skip the first, dummy, section.
193   const unsigned char *ps = pshdrs + This::shdr_size;
194   for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
195     {
196       typename This::Shdr shdr(ps);
197
198       unsigned int sh_type = shdr.get_sh_type();
199       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
200         continue;
201
202       unsigned int shndx = shdr.get_sh_info();
203       if (shndx >= shnum)
204         {
205           this->error(_("relocation section %u has bad info %u"),
206                       i, shndx);
207           continue;
208         }
209
210       Output_section* os = map_sections[shndx].output_section;
211       if (os == NULL)
212         continue;
213
214       // We are scanning relocations in order to fill out the GOT and
215       // PLT sections.  Relocations for sections which are not
216       // allocated (typically debugging sections) should not add new
217       // GOT and PLT entries.  So we skip them.
218       typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
219       if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
220         continue;
221
222       if (shdr.get_sh_link() != this->symtab_shndx_)
223         {
224           this->error(_("relocation section %u uses unexpected "
225                         "symbol table %u"),
226                       i, shdr.get_sh_link());
227           continue;
228         }
229
230       off_t sh_size = shdr.get_sh_size();
231
232       unsigned int reloc_size;
233       if (sh_type == elfcpp::SHT_REL)
234         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
235       else
236         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
237       if (reloc_size != shdr.get_sh_entsize())
238         {
239           this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
240                       i, static_cast<unsigned long>(shdr.get_sh_entsize()),
241                       reloc_size);
242           continue;
243         }
244
245       size_t reloc_count = sh_size / reloc_size;
246       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
247         {
248           this->error(_("reloc section %u size %lu uneven"),
249                       i, static_cast<unsigned long>(sh_size));
250           continue;
251         }
252
253       rd->relocs.push_back(Section_relocs());
254       Section_relocs& sr(rd->relocs.back());
255       sr.reloc_shndx = i;
256       sr.data_shndx = shndx;
257       sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
258                                            true);
259       sr.sh_type = sh_type;
260       sr.reloc_count = reloc_count;
261       sr.output_section = os;
262       sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
263     }
264
265   // Read the local symbols.
266   gold_assert(this->symtab_shndx_ != -1U);
267   if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
268     rd->local_symbols = NULL;
269   else
270     {
271       typename This::Shdr symtabshdr(pshdrs
272                                      + this->symtab_shndx_ * This::shdr_size);
273       gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
274       const int sym_size = This::sym_size;
275       const unsigned int loccount = this->local_symbol_count_;
276       gold_assert(loccount == symtabshdr.get_sh_info());
277       off_t locsize = loccount * sym_size;
278       rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
279                                                  locsize, true);
280     }
281 }
282
283 // Scan the relocs and adjust the symbol table.  This looks for
284 // relocations which require GOT/PLT/COPY relocations.
285
286 template<int size, bool big_endian>
287 void
288 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
289                                                Symbol_table* symtab,
290                                                Layout* layout,
291                                                Read_relocs_data* rd)
292 {
293   Sized_target<size, big_endian>* target = this->sized_target();
294
295   const unsigned char* local_symbols;
296   if (rd->local_symbols == NULL)
297     local_symbols = NULL;
298   else
299     local_symbols = rd->local_symbols->data();
300
301   for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
302        p != rd->relocs.end();
303        ++p)
304     {
305       target->scan_relocs(options, symtab, layout, this, p->data_shndx,
306                           p->sh_type, p->contents->data(), p->reloc_count,
307                           p->output_section, p->needs_special_offset_handling,
308                           this->local_symbol_count_,
309                           local_symbols);
310       delete p->contents;
311       p->contents = NULL;
312     }
313
314   if (rd->local_symbols != NULL)
315     {
316       delete rd->local_symbols;
317       rd->local_symbols = NULL;
318     }
319 }
320
321 // Relocate the input sections and write out the local symbols.
322
323 template<int size, bool big_endian>
324 void
325 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
326                                             const Symbol_table* symtab,
327                                             const Layout* layout,
328                                             Output_file* of)
329 {
330   unsigned int shnum = this->shnum();
331
332   // Read the section headers.
333   const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
334                                                shnum * This::shdr_size,
335                                                true);
336
337   Views views;
338   views.resize(shnum);
339
340   // Make two passes over the sections.  The first one copies the
341   // section data to the output file.  The second one applies
342   // relocations.
343
344   this->write_sections(pshdrs, of, &views);
345
346   // Apply relocations.
347
348   this->relocate_sections(options, symtab, layout, pshdrs, &views);
349
350   // Write out the accumulated views.
351   for (unsigned int i = 1; i < shnum; ++i)
352     {
353       if (views[i].view != NULL)
354         {
355           if (!views[i].is_postprocessing_view)
356             {
357               if (views[i].is_input_output_view)
358                 of->write_input_output_view(views[i].offset,
359                                             views[i].view_size,
360                                             views[i].view);
361               else
362                 of->write_output_view(views[i].offset, views[i].view_size,
363                                       views[i].view);
364             }
365         }
366     }
367
368   // Write out the local symbols.
369   this->write_local_symbols(of, layout->sympool(), layout->dynpool());
370 }
371
372 // Write section data to the output file.  PSHDRS points to the
373 // section headers.  Record the views in *PVIEWS for use when
374 // relocating.
375
376 template<int size, bool big_endian>
377 void
378 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
379                                                Output_file* of,
380                                                Views* pviews) const
381 {
382   unsigned int shnum = this->shnum();
383   const std::vector<Map_to_output>& map_sections(this->map_to_output());
384
385   const unsigned char* p = pshdrs + This::shdr_size;
386   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
387     {
388       View_size* pvs = &(*pviews)[i];
389
390       pvs->view = NULL;
391
392       const Output_section* os = map_sections[i].output_section;
393       if (os == NULL)
394         continue;
395       off_t output_offset = map_sections[i].offset;
396
397       typename This::Shdr shdr(p);
398
399       if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
400         continue;
401
402       // In the normal case, this input section is simply mapped to
403       // the output section at offset OUTPUT_OFFSET.
404
405       // However, if OUTPUT_OFFSET == -1, then input data is handled
406       // specially--e.g., a .eh_frame section.  The relocation
407       // routines need to check for each reloc where it should be
408       // applied.  For this case, we need an input/output view for the
409       // entire contents of the section in the output file.  We don't
410       // want to copy the contents of the input section to the output
411       // section; the output section contents were already written,
412       // and we waited for them in Relocate_task::is_runnable because
413       // relocs_must_follow_section_writes is set for the object.
414
415       // Regardless of which of the above cases is true, we have to
416       // check requires_postprocessing of the output section.  If that
417       // is false, then we work with views of the output file
418       // directly.  If it is true, then we work with a separate
419       // buffer, and the output section is responsible for writing the
420       // final data to the output file.
421
422       off_t output_section_offset;
423       off_t output_section_size;
424       if (!os->requires_postprocessing())
425         {
426           output_section_offset = os->offset();
427           output_section_size = os->data_size();
428         }
429       else
430         {
431           output_section_offset = 0;
432           output_section_size = os->postprocessing_buffer_size();
433         }
434
435       off_t view_start;
436       off_t view_size;
437       if (output_offset != -1)
438         {
439           view_start = output_section_offset + output_offset;
440           view_size = shdr.get_sh_size();
441         }
442       else
443         {
444           view_start = output_section_offset;
445           view_size = output_section_size;
446         }
447
448       if (view_size == 0)
449         continue;
450
451       gold_assert(output_offset == -1
452                   || (output_offset >= 0
453                       && output_offset + view_size <= output_section_size));
454
455       unsigned char* view;
456       if (os->requires_postprocessing())
457         {
458           unsigned char* buffer = os->postprocessing_buffer();
459           view = buffer + view_start;
460           if (output_offset != -1)
461             this->read(shdr.get_sh_offset(), view_size, view);
462         }
463       else
464         {
465           if (output_offset == -1)
466             view = of->get_input_output_view(view_start, view_size);
467           else
468             {
469               view = of->get_output_view(view_start, view_size);
470               this->read(shdr.get_sh_offset(), view_size, view);
471             }
472         }
473
474       pvs->view = view;
475       pvs->address = os->address();
476       if (output_offset != -1)
477         pvs->address += output_offset;
478       pvs->offset = view_start;
479       pvs->view_size = view_size;
480       pvs->is_input_output_view = output_offset == -1;
481       pvs->is_postprocessing_view = os->requires_postprocessing();
482     }
483 }
484
485 // Relocate section data.  VIEWS points to the section data as views
486 // in the output file.
487
488 template<int size, bool big_endian>
489 void
490 Sized_relobj<size, big_endian>::relocate_sections(
491     const General_options& options,
492     const Symbol_table* symtab,
493     const Layout* layout,
494     const unsigned char* pshdrs,
495     Views* pviews)
496 {
497   unsigned int shnum = this->shnum();
498   Sized_target<size, big_endian>* target = this->sized_target();
499
500   const std::vector<Map_to_output>& map_sections(this->map_to_output());
501
502   Relocate_info<size, big_endian> relinfo;
503   relinfo.options = &options;
504   relinfo.symtab = symtab;
505   relinfo.layout = layout;
506   relinfo.object = this;
507
508   const unsigned char* p = pshdrs + This::shdr_size;
509   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
510     {
511       typename This::Shdr shdr(p);
512
513       unsigned int sh_type = shdr.get_sh_type();
514       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
515         continue;
516
517       unsigned int index = shdr.get_sh_info();
518       if (index >= this->shnum())
519         {
520           this->error(_("relocation section %u has bad info %u"),
521                       i, index);
522           continue;
523         }
524
525       Output_section* os = map_sections[index].output_section;
526       if (os == NULL)
527         {
528           // This relocation section is against a section which we
529           // discarded.
530           continue;
531         }
532       off_t output_offset = map_sections[index].offset;
533
534       gold_assert((*pviews)[index].view != NULL);
535
536       if (shdr.get_sh_link() != this->symtab_shndx_)
537         {
538           gold_error(_("relocation section %u uses unexpected "
539                        "symbol table %u"),
540                      i, shdr.get_sh_link());
541           continue;
542         }
543
544       off_t sh_size = shdr.get_sh_size();
545       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
546                                                     sh_size, false);
547
548       unsigned int reloc_size;
549       if (sh_type == elfcpp::SHT_REL)
550         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
551       else
552         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
553
554       if (reloc_size != shdr.get_sh_entsize())
555         {
556           gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
557                      i, static_cast<unsigned long>(shdr.get_sh_entsize()),
558                      reloc_size);
559           continue;
560         }
561
562       size_t reloc_count = sh_size / reloc_size;
563       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
564         {
565           gold_error(_("reloc section %u size %lu uneven"),
566                      i, static_cast<unsigned long>(sh_size));
567           continue;
568         }
569
570       gold_assert(output_offset != -1
571                   || this->relocs_must_follow_section_writes());
572
573       relinfo.reloc_shndx = i;
574       relinfo.data_shndx = index;
575       target->relocate_section(&relinfo,
576                                sh_type,
577                                prelocs,
578                                reloc_count,
579                                os,
580                                output_offset == -1,
581                                (*pviews)[index].view,
582                                (*pviews)[index].address,
583                                (*pviews)[index].view_size);
584     }
585 }
586
587 // Copy_relocs::Copy_reloc_entry methods.
588
589 // Return whether we should emit this reloc.  We should emit it if the
590 // symbol is still defined in a dynamic object.  If we should not emit
591 // it, we clear it, to save ourselves the test next time.
592
593 template<int size, bool big_endian>
594 bool
595 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
596 {
597   if (this->sym_ == NULL)
598     return false;
599   if (this->sym_->is_from_dynobj())
600     return true;
601   this->sym_ = NULL;
602   return false;
603 }
604
605 // Emit a reloc into a SHT_REL section.
606
607 template<int size, bool big_endian>
608 void
609 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
610     Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
611 {
612   this->sym_->set_needs_dynsym_entry();
613   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
614                          this->relobj_, this->shndx_, this->address_);
615 }
616
617 // Emit a reloc into a SHT_RELA section.
618
619 template<int size, bool big_endian>
620 void
621 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
622     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
623 {
624   this->sym_->set_needs_dynsym_entry();
625   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
626                          this->relobj_, this->shndx_, this->address_,
627                          this->addend_);
628 }
629
630 // Copy_relocs methods.
631
632 // Return whether we need a COPY reloc for a relocation against GSYM.
633 // The relocation is being applied to section SHNDX in OBJECT.
634
635 template<int size, bool big_endian>
636 bool
637 Copy_relocs<size, big_endian>::need_copy_reloc(
638     const General_options*,
639     Relobj* object,
640     unsigned int shndx,
641     Sized_symbol<size>* sym)
642 {
643   // FIXME: Handle -z nocopyrelocs.
644
645   if (sym->symsize() == 0)
646     return false;
647
648   // If this is a readonly section, then we need a COPY reloc.
649   // Otherwise we can use a dynamic reloc.
650   if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
651     return true;
652
653   return false;
654 }
655
656 // Save a Rel reloc.
657
658 template<int size, bool big_endian>
659 void
660 Copy_relocs<size, big_endian>::save(
661     Symbol* sym,
662     Relobj* relobj,
663     unsigned int shndx,
664     Output_section* output_section,
665     const elfcpp::Rel<size, big_endian>& rel)
666 {
667   unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
668   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
669                                             output_section,
670                                             rel.get_r_offset(), 0));
671 }
672
673 // Save a Rela reloc.
674
675 template<int size, bool big_endian>
676 void
677 Copy_relocs<size, big_endian>::save(
678     Symbol* sym,
679     Relobj* relobj,
680     unsigned int shndx,
681     Output_section* output_section,
682     const elfcpp::Rela<size, big_endian>& rela)
683 {
684   unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
685   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
686                                             output_section,
687                                             rela.get_r_offset(),
688                                             rela.get_r_addend()));
689 }
690
691 // Return whether there are any relocs to emit.  We don't want to emit
692 // a reloc if the symbol is no longer defined in a dynamic object.
693
694 template<int size, bool big_endian>
695 bool
696 Copy_relocs<size, big_endian>::any_to_emit()
697 {
698   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
699        p != this->entries_.end();
700        ++p)
701     {
702       if (p->should_emit())
703         return true;
704     }
705   return false;
706 }
707
708 // Emit relocs.
709
710 template<int size, bool big_endian>
711 template<int sh_type>
712 void
713 Copy_relocs<size, big_endian>::emit(
714     Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
715 {
716   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
717        p != this->entries_.end();
718        ++p)
719     {
720       if (p->should_emit())
721         p->emit(reloc_data);
722     }
723 }
724
725 // Track_relocs methods.
726
727 // Initialize the class to track the relocs.  This gets the object,
728 // the reloc section index, and the type of the relocs.  This returns
729 // false if something goes wrong.
730
731 template<int size, bool big_endian>
732 bool
733 Track_relocs<size, big_endian>::initialize(
734     Object* object,
735     unsigned int reloc_shndx,
736     unsigned int reloc_type)
737 {
738   // If RELOC_SHNDX is -1U, it means there is more than one reloc
739   // section for the .eh_frame section.  We can't handle that case.
740   if (reloc_shndx == -1U)
741     return false;
742
743   // If RELOC_SHNDX is 0, there is no reloc section.
744   if (reloc_shndx == 0)
745     return true;
746
747   // Get the contents of the reloc section.
748   this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
749
750   if (reloc_type == elfcpp::SHT_REL)
751     this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
752   else if (reloc_type == elfcpp::SHT_RELA)
753     this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
754   else
755     gold_unreachable();
756
757   if (this->len_ % this->reloc_size_ != 0)
758     {
759       object->error(_("reloc section size %zu is not a multiple of "
760                       "reloc size %d\n"),
761                     static_cast<size_t>(this->len_),
762                     this->reloc_size_);
763       return false;
764     }
765
766   return true;
767 }
768
769 // Return the offset of the next reloc, or -1 if there isn't one.
770
771 template<int size, bool big_endian>
772 off_t
773 Track_relocs<size, big_endian>::next_offset() const
774 {
775   if (this->pos_ >= this->len_)
776     return -1;
777
778   // Rel and Rela start out the same, so we can always use Rel to find
779   // the r_offset value.
780   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
781   return rel.get_r_offset();
782 }
783
784 // Return the index of the symbol referenced by the next reloc, or -1U
785 // if there aren't any more relocs.
786
787 template<int size, bool big_endian>
788 unsigned int
789 Track_relocs<size, big_endian>::next_symndx() const
790 {
791   if (this->pos_ >= this->len_)
792     return -1U;
793
794   // Rel and Rela start out the same, so we can use Rel to find the
795   // symbol index.
796   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
797   return elfcpp::elf_r_sym<size>(rel.get_r_info());
798 }
799
800 // Advance to the next reloc whose r_offset is greater than or equal
801 // to OFFSET.  Return the number of relocs we skip.
802
803 template<int size, bool big_endian>
804 int
805 Track_relocs<size, big_endian>::advance(off_t offset)
806 {
807   int ret = 0;
808   while (this->pos_ < this->len_)
809     {
810       // Rel and Rela start out the same, so we can always use Rel to
811       // find the r_offset value.
812       elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
813       if (static_cast<off_t>(rel.get_r_offset()) >= offset)
814         break;
815       ++ret;
816       this->pos_ += this->reloc_size_;
817     }
818   return ret;
819 }
820
821 // Instantiate the templates we need.  We could use the configure
822 // script to restrict this to only the ones for implemented targets.
823
824 #ifdef HAVE_TARGET_32_LITTLE
825 template
826 void
827 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
828 #endif
829
830 #ifdef HAVE_TARGET_32_BIG
831 template
832 void
833 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
834 #endif
835
836 #ifdef HAVE_TARGET_64_LITTLE
837 template
838 void
839 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
840 #endif
841
842 #ifdef HAVE_TARGET_64_BIG
843 template
844 void
845 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
846 #endif
847
848 #ifdef HAVE_TARGET_32_LITTLE
849 template
850 void
851 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
852                                         Symbol_table* symtab,
853                                         Layout* layout,
854                                         Read_relocs_data* rd);
855 #endif
856
857 #ifdef HAVE_TARGET_32_BIG
858 template
859 void
860 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
861                                        Symbol_table* symtab,
862                                        Layout* layout,
863                                        Read_relocs_data* rd);
864 #endif
865
866 #ifdef HAVE_TARGET_64_LITTLE
867 template
868 void
869 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
870                                         Symbol_table* symtab,
871                                         Layout* layout,
872                                         Read_relocs_data* rd);
873 #endif
874
875 #ifdef HAVE_TARGET_64_BIG
876 template
877 void
878 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
879                                        Symbol_table* symtab,
880                                        Layout* layout,
881                                        Read_relocs_data* rd);
882 #endif
883
884 #ifdef HAVE_TARGET_32_LITTLE
885 template
886 void
887 Sized_relobj<32, false>::do_relocate(const General_options& options,
888                                      const Symbol_table* symtab,
889                                      const Layout* layout,
890                                      Output_file* of);
891 #endif
892
893 #ifdef HAVE_TARGET_32_BIG
894 template
895 void
896 Sized_relobj<32, true>::do_relocate(const General_options& options,
897                                     const Symbol_table* symtab,
898                                     const Layout* layout,
899                                     Output_file* of);
900 #endif
901
902 #ifdef HAVE_TARGET_64_LITTLE
903 template
904 void
905 Sized_relobj<64, false>::do_relocate(const General_options& options,
906                                      const Symbol_table* symtab,
907                                      const Layout* layout,
908                                      Output_file* of);
909 #endif
910
911 #ifdef HAVE_TARGET_64_BIG
912 template
913 void
914 Sized_relobj<64, true>::do_relocate(const General_options& options,
915                                     const Symbol_table* symtab,
916                                     const Layout* layout,
917                                     Output_file* of);
918 #endif
919
920 #ifdef HAVE_TARGET_32_LITTLE
921 template
922 class Copy_relocs<32, false>;
923 #endif
924
925 #ifdef HAVE_TARGET_32_BIG
926 template
927 class Copy_relocs<32, true>;
928 #endif
929
930 #ifdef HAVE_TARGET_64_LITTLE
931 template
932 class Copy_relocs<64, false>;
933 #endif
934
935 #ifdef HAVE_TARGET_64_BIG
936 template
937 class Copy_relocs<64, true>;
938 #endif
939
940 #ifdef HAVE_TARGET_32_LITTLE
941 template
942 void
943 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
944     Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
945 #endif
946
947 #ifdef HAVE_TARGET_32_BIG
948 template
949 void
950 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
951     Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
952 #endif
953
954 #ifdef HAVE_TARGET_64_LITTLE
955 template
956 void
957 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
958     Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
959 #endif
960
961 #ifdef HAVE_TARGET_64_BIG
962 template
963 void
964 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
965     Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
966 #endif
967
968 #ifdef HAVE_TARGET_32_LITTLE
969 template
970 void
971 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
972     Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
973 #endif
974
975 #ifdef HAVE_TARGET_32_BIG
976 template
977 void
978 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
979     Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
980 #endif
981
982 #ifdef HAVE_TARGET_64_LITTLE
983 template
984 void
985 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
986     Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
987 #endif
988
989 #ifdef HAVE_TARGET_64_BIG
990 template
991 void
992 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
993     Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
994 #endif
995
996 #ifdef HAVE_TARGET_32_LITTLE
997 template
998 class Track_relocs<32, false>;
999 #endif
1000
1001 #ifdef HAVE_TARGET_32_BIG
1002 template
1003 class Track_relocs<32, true>;
1004 #endif
1005
1006 #ifdef HAVE_TARGET_64_LITTLE
1007 template
1008 class Track_relocs<64, false>;
1009 #endif
1010
1011 #ifdef HAVE_TARGET_64_BIG
1012 template
1013 class Track_relocs<64, true>;
1014 #endif
1015
1016 } // End namespace gold.