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