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