Compress all debug sections.
[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_postprocessing_view)
380             {
381               if (views[i].is_input_output_view)
382                 of->write_input_output_view(views[i].offset,
383                                             views[i].view_size,
384                                             views[i].view);
385               else
386                 of->write_output_view(views[i].offset, views[i].view_size,
387                                       views[i].view);
388             }
389         }
390     }
391
392   // Write out the local symbols.
393   this->write_local_symbols(of, layout->sympool());
394 }
395
396 // Write section data to the output file.  PSHDRS points to the
397 // section headers.  Record the views in *PVIEWS for use when
398 // relocating.
399
400 template<int size, bool big_endian>
401 void
402 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
403                                                Output_file* of,
404                                                Views* pviews)
405 {
406   unsigned int shnum = this->shnum();
407   std::vector<Map_to_output>& map_sections(this->map_to_output());
408
409   const unsigned char* p = pshdrs + This::shdr_size;
410   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
411     {
412       View_size* pvs = &(*pviews)[i];
413
414       pvs->view = NULL;
415
416       const Output_section* os = map_sections[i].output_section;
417       if (os == NULL)
418         continue;
419       off_t output_offset = map_sections[i].offset;
420
421       typename This::Shdr shdr(p);
422
423       if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
424         continue;
425
426       // In the normal case, this input section is simply mapped to
427       // the output section at offset OUTPUT_OFFSET.
428
429       // However, if OUTPUT_OFFSET == -1, then input data is handled
430       // specially--e.g., a .eh_frame section.  The relocation
431       // routines need to check for each reloc where it should be
432       // applied.  For this case, we need an input/output view for the
433       // entire contents of the section in the output file.  We don't
434       // want to copy the contents of the input section to the output
435       // section; the output section contents were already written,
436       // and we waited for them in Relocate_task::is_runnable because
437       // relocs_must_follow_section_writes is set for the object.
438
439       // Regardless of which of the above cases is true, we have to
440       // check requires_postprocessing of the output section.  If that
441       // is false, then we work with views of the output file
442       // directly.  If it is true, then we work with a separate
443       // buffer, and the output section is responsible for writing the
444       // final data to the output file.
445
446       off_t output_section_offset;
447       off_t output_section_size;
448       if (!os->requires_postprocessing())
449         {
450           output_section_offset = os->offset();
451           output_section_size = os->data_size();
452         }
453       else
454         {
455           output_section_offset = 0;
456           output_section_size = os->postprocessing_buffer_size();
457         }
458
459       off_t view_start;
460       off_t view_size;
461       if (output_offset != -1)
462         {
463           view_start = output_section_offset + output_offset;
464           view_size = shdr.get_sh_size();
465         }
466       else
467         {
468           view_start = output_section_offset;
469           view_size = output_section_size;
470         }
471
472       if (view_size == 0)
473         continue;
474
475       gold_assert(output_offset == -1
476                   || (output_offset >= 0
477                       && output_offset + view_size <= output_section_size));
478
479       unsigned char* view;
480       if (os->requires_postprocessing())
481         {
482           unsigned char* buffer = os->postprocessing_buffer();
483           view = buffer + view_start;
484           if (output_offset != -1)
485             this->read(shdr.get_sh_offset(), view_size, view);
486         }
487       else
488         {
489           if (output_offset == -1)
490             view = of->get_input_output_view(view_start, view_size);
491           else
492             {
493               view = of->get_output_view(view_start, view_size);
494               this->read(shdr.get_sh_offset(), view_size, view);
495             }
496         }
497
498       pvs->view = view;
499       pvs->address = os->address();
500       if (output_offset != -1)
501         pvs->address += output_offset;
502       pvs->offset = view_start;
503       pvs->view_size = view_size;
504       pvs->is_input_output_view = output_offset == -1;
505       pvs->is_postprocessing_view = os->requires_postprocessing();
506     }
507 }
508
509 // Relocate section data.  VIEWS points to the section data as views
510 // in the output file.
511
512 template<int size, bool big_endian>
513 void
514 Sized_relobj<size, big_endian>::relocate_sections(
515     const General_options& options,
516     const Symbol_table* symtab,
517     const Layout* layout,
518     const unsigned char* pshdrs,
519     Views* pviews)
520 {
521   unsigned int shnum = this->shnum();
522   Sized_target<size, big_endian>* target = this->sized_target();
523
524   std::vector<Map_to_output>& map_sections(this->map_to_output());
525
526   Relocate_info<size, big_endian> relinfo;
527   relinfo.options = &options;
528   relinfo.symtab = symtab;
529   relinfo.layout = layout;
530   relinfo.object = this;
531
532   const unsigned char* p = pshdrs + This::shdr_size;
533   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
534     {
535       typename This::Shdr shdr(p);
536
537       unsigned int sh_type = shdr.get_sh_type();
538       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
539         continue;
540
541       unsigned int index = shdr.get_sh_info();
542       if (index >= this->shnum())
543         {
544           this->error(_("relocation section %u has bad info %u"),
545                       i, index);
546           continue;
547         }
548
549       Output_section* os = map_sections[index].output_section;
550       if (os == NULL)
551         {
552           // This relocation section is against a section which we
553           // discarded.
554           continue;
555         }
556       off_t output_offset = map_sections[index].offset;
557
558       gold_assert((*pviews)[index].view != NULL);
559
560       if (shdr.get_sh_link() != this->symtab_shndx_)
561         {
562           gold_error(_("relocation section %u uses unexpected "
563                        "symbol table %u"),
564                      i, shdr.get_sh_link());
565           continue;
566         }
567
568       off_t sh_size = shdr.get_sh_size();
569       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
570                                                     sh_size, false);
571
572       unsigned int reloc_size;
573       if (sh_type == elfcpp::SHT_REL)
574         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
575       else
576         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
577
578       if (reloc_size != shdr.get_sh_entsize())
579         {
580           gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
581                      i, static_cast<unsigned long>(shdr.get_sh_entsize()),
582                      reloc_size);
583           continue;
584         }
585
586       size_t reloc_count = sh_size / reloc_size;
587       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
588         {
589           gold_error(_("reloc section %u size %lu uneven"),
590                      i, static_cast<unsigned long>(sh_size));
591           continue;
592         }
593
594       gold_assert(output_offset != -1
595                   || this->relocs_must_follow_section_writes());
596
597       relinfo.reloc_shndx = i;
598       relinfo.data_shndx = index;
599       target->relocate_section(&relinfo,
600                                sh_type,
601                                prelocs,
602                                reloc_count,
603                                os,
604                                output_offset == -1,
605                                (*pviews)[index].view,
606                                (*pviews)[index].address,
607                                (*pviews)[index].view_size);
608     }
609 }
610
611 // Copy_relocs::Copy_reloc_entry methods.
612
613 // Return whether we should emit this reloc.  We should emit it if the
614 // symbol is still defined in a dynamic object.  If we should not emit
615 // it, we clear it, to save ourselves the test next time.
616
617 template<int size, bool big_endian>
618 bool
619 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
620 {
621   if (this->sym_ == NULL)
622     return false;
623   if (this->sym_->is_from_dynobj())
624     return true;
625   this->sym_ = NULL;
626   return false;
627 }
628
629 // Emit a reloc into a SHT_REL section.
630
631 template<int size, bool big_endian>
632 void
633 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
634     Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
635 {
636   this->sym_->set_needs_dynsym_entry();
637   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
638                          this->relobj_, this->shndx_, this->address_);
639 }
640
641 // Emit a reloc into a SHT_RELA section.
642
643 template<int size, bool big_endian>
644 void
645 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
646     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
647 {
648   this->sym_->set_needs_dynsym_entry();
649   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
650                          this->relobj_, this->shndx_, this->address_,
651                          this->addend_);
652 }
653
654 // Copy_relocs methods.
655
656 // Return whether we need a COPY reloc for a relocation against GSYM.
657 // The relocation is being applied to section SHNDX in OBJECT.
658
659 template<int size, bool big_endian>
660 bool
661 Copy_relocs<size, big_endian>::need_copy_reloc(
662     const General_options*,
663     Relobj* object,
664     unsigned int shndx,
665     Sized_symbol<size>* sym)
666 {
667   // FIXME: Handle -z nocopyrelocs.
668
669   if (sym->symsize() == 0)
670     return false;
671
672   // If this is a readonly section, then we need a COPY reloc.
673   // Otherwise we can use a dynamic reloc.
674   if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
675     return true;
676
677   return false;
678 }
679
680 // Save a Rel reloc.
681
682 template<int size, bool big_endian>
683 void
684 Copy_relocs<size, big_endian>::save(
685     Symbol* sym,
686     Relobj* relobj,
687     unsigned int shndx,
688     Output_section* output_section,
689     const elfcpp::Rel<size, big_endian>& rel)
690 {
691   unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
692   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
693                                             output_section,
694                                             rel.get_r_offset(), 0));
695 }
696
697 // Save a Rela reloc.
698
699 template<int size, bool big_endian>
700 void
701 Copy_relocs<size, big_endian>::save(
702     Symbol* sym,
703     Relobj* relobj,
704     unsigned int shndx,
705     Output_section* output_section,
706     const elfcpp::Rela<size, big_endian>& rela)
707 {
708   unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
709   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
710                                             output_section,
711                                             rela.get_r_offset(),
712                                             rela.get_r_addend()));
713 }
714
715 // Return whether there are any relocs to emit.  We don't want to emit
716 // a reloc if the symbol is no longer defined in a dynamic object.
717
718 template<int size, bool big_endian>
719 bool
720 Copy_relocs<size, big_endian>::any_to_emit()
721 {
722   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
723        p != this->entries_.end();
724        ++p)
725     {
726       if (p->should_emit())
727         return true;
728     }
729   return false;
730 }
731
732 // Emit relocs.
733
734 template<int size, bool big_endian>
735 template<int sh_type>
736 void
737 Copy_relocs<size, big_endian>::emit(
738     Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
739 {
740   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
741        p != this->entries_.end();
742        ++p)
743     {
744       if (p->should_emit())
745         p->emit(reloc_data);
746     }
747 }
748
749 // Track_relocs methods.
750
751 // Initialize the class to track the relocs.  This gets the object,
752 // the reloc section index, and the type of the relocs.  This returns
753 // false if something goes wrong.
754
755 template<int size, bool big_endian>
756 bool
757 Track_relocs<size, big_endian>::initialize(
758     Object* object,
759     unsigned int reloc_shndx,
760     unsigned int reloc_type)
761 {
762   // If RELOC_SHNDX is -1U, it means there is more than one reloc
763   // section for the .eh_frame section.  We can't handle that case.
764   if (reloc_shndx == -1U)
765     return false;
766
767   // If RELOC_SHNDX is 0, there is no reloc section.
768   if (reloc_shndx == 0)
769     return true;
770
771   // Get the contents of the reloc section.
772   this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
773
774   if (reloc_type == elfcpp::SHT_REL)
775     this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
776   else if (reloc_type == elfcpp::SHT_RELA)
777     this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
778   else
779     gold_unreachable();
780
781   if (this->len_ % this->reloc_size_ != 0)
782     {
783       object->error(_("reloc section size %zu is not a multiple of "
784                       "reloc size %d\n"),
785                     static_cast<size_t>(this->len_),
786                     this->reloc_size_);
787       return false;
788     }
789
790   return true;
791 }
792
793 // Return the offset of the next reloc, or -1 if there isn't one.
794
795 template<int size, bool big_endian>
796 off_t
797 Track_relocs<size, big_endian>::next_offset() const
798 {
799   if (this->pos_ >= this->len_)
800     return -1;
801
802   // Rel and Rela start out the same, so we can always use Rel to find
803   // the r_offset value.
804   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
805   return rel.get_r_offset();
806 }
807
808 // Return the index of the symbol referenced by the next reloc, or -1U
809 // if there aren't any more relocs.
810
811 template<int size, bool big_endian>
812 unsigned int
813 Track_relocs<size, big_endian>::next_symndx() const
814 {
815   if (this->pos_ >= this->len_)
816     return -1U;
817
818   // Rel and Rela start out the same, so we can use Rel to find the
819   // symbol index.
820   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
821   return elfcpp::elf_r_sym<size>(rel.get_r_info());
822 }
823
824 // Advance to the next reloc whose r_offset is greater than or equal
825 // to OFFSET.  Return the number of relocs we skip.
826
827 template<int size, bool big_endian>
828 int
829 Track_relocs<size, big_endian>::advance(off_t offset)
830 {
831   int ret = 0;
832   while (this->pos_ < this->len_)
833     {
834       // Rel and Rela start out the same, so we can always use Rel to
835       // find the r_offset value.
836       elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
837       if (static_cast<off_t>(rel.get_r_offset()) >= offset)
838         break;
839       ++ret;
840       this->pos_ += this->reloc_size_;
841     }
842   return ret;
843 }
844
845 // Instantiate the templates we need.  We could use the configure
846 // script to restrict this to only the ones for implemented targets.
847
848 #ifdef HAVE_TARGET_32_LITTLE
849 template
850 void
851 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
852 #endif
853
854 #ifdef HAVE_TARGET_32_BIG
855 template
856 void
857 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
858 #endif
859
860 #ifdef HAVE_TARGET_64_LITTLE
861 template
862 void
863 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
864 #endif
865
866 #ifdef HAVE_TARGET_64_BIG
867 template
868 void
869 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
870 #endif
871
872 #ifdef HAVE_TARGET_32_LITTLE
873 template
874 void
875 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
876                                         Symbol_table* symtab,
877                                         Layout* layout,
878                                         Read_relocs_data* rd);
879 #endif
880
881 #ifdef HAVE_TARGET_32_BIG
882 template
883 void
884 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
885                                        Symbol_table* symtab,
886                                        Layout* layout,
887                                        Read_relocs_data* rd);
888 #endif
889
890 #ifdef HAVE_TARGET_64_LITTLE
891 template
892 void
893 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
894                                         Symbol_table* symtab,
895                                         Layout* layout,
896                                         Read_relocs_data* rd);
897 #endif
898
899 #ifdef HAVE_TARGET_64_BIG
900 template
901 void
902 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
903                                        Symbol_table* symtab,
904                                        Layout* layout,
905                                        Read_relocs_data* rd);
906 #endif
907
908 #ifdef HAVE_TARGET_32_LITTLE
909 template
910 void
911 Sized_relobj<32, false>::do_relocate(const General_options& options,
912                                      const Symbol_table* symtab,
913                                      const Layout* layout,
914                                      Output_file* of);
915 #endif
916
917 #ifdef HAVE_TARGET_32_BIG
918 template
919 void
920 Sized_relobj<32, true>::do_relocate(const General_options& options,
921                                     const Symbol_table* symtab,
922                                     const Layout* layout,
923                                     Output_file* of);
924 #endif
925
926 #ifdef HAVE_TARGET_64_LITTLE
927 template
928 void
929 Sized_relobj<64, false>::do_relocate(const General_options& options,
930                                      const Symbol_table* symtab,
931                                      const Layout* layout,
932                                      Output_file* of);
933 #endif
934
935 #ifdef HAVE_TARGET_64_BIG
936 template
937 void
938 Sized_relobj<64, true>::do_relocate(const General_options& options,
939                                     const Symbol_table* symtab,
940                                     const Layout* layout,
941                                     Output_file* of);
942 #endif
943
944 #ifdef HAVE_TARGET_32_LITTLE
945 template
946 class Copy_relocs<32, false>;
947 #endif
948
949 #ifdef HAVE_TARGET_32_BIG
950 template
951 class Copy_relocs<32, true>;
952 #endif
953
954 #ifdef HAVE_TARGET_64_LITTLE
955 template
956 class Copy_relocs<64, false>;
957 #endif
958
959 #ifdef HAVE_TARGET_64_BIG
960 template
961 class Copy_relocs<64, true>;
962 #endif
963
964 #ifdef HAVE_TARGET_32_LITTLE
965 template
966 void
967 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
968     Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
969 #endif
970
971 #ifdef HAVE_TARGET_32_BIG
972 template
973 void
974 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
975     Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
976 #endif
977
978 #ifdef HAVE_TARGET_64_LITTLE
979 template
980 void
981 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
982     Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
983 #endif
984
985 #ifdef HAVE_TARGET_64_BIG
986 template
987 void
988 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
989     Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
990 #endif
991
992 #ifdef HAVE_TARGET_32_LITTLE
993 template
994 void
995 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
996     Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
997 #endif
998
999 #ifdef HAVE_TARGET_32_BIG
1000 template
1001 void
1002 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1003     Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
1004 #endif
1005
1006 #ifdef HAVE_TARGET_64_LITTLE
1007 template
1008 void
1009 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1010     Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
1011 #endif
1012
1013 #ifdef HAVE_TARGET_64_BIG
1014 template
1015 void
1016 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1017     Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
1018 #endif
1019
1020 #ifdef HAVE_TARGET_32_LITTLE
1021 template
1022 class Track_relocs<32, false>;
1023 #endif
1024
1025 #ifdef HAVE_TARGET_32_BIG
1026 template
1027 class Track_relocs<32, true>;
1028 #endif
1029
1030 #ifdef HAVE_TARGET_64_LITTLE
1031 template
1032 class Track_relocs<64, false>;
1033 #endif
1034
1035 #ifdef HAVE_TARGET_64_BIG
1036 template
1037 class Track_relocs<64, true>;
1038 #endif
1039
1040 } // End namespace gold.