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