Initial -r support.
[external/binutils.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <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 unless this is a
226       // relocatable link.
227       if (!parameters->output_is_object())
228         {
229           typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
230           if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
231             continue;
232         }
233
234       if (shdr.get_sh_link() != this->symtab_shndx_)
235         {
236           this->error(_("relocation section %u uses unexpected "
237                         "symbol table %u"),
238                       i, shdr.get_sh_link());
239           continue;
240         }
241
242       off_t sh_size = shdr.get_sh_size();
243
244       unsigned int reloc_size;
245       if (sh_type == elfcpp::SHT_REL)
246         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
247       else
248         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
249       if (reloc_size != shdr.get_sh_entsize())
250         {
251           this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
252                       i, static_cast<unsigned long>(shdr.get_sh_entsize()),
253                       reloc_size);
254           continue;
255         }
256
257       size_t reloc_count = sh_size / reloc_size;
258       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
259         {
260           this->error(_("reloc section %u size %lu uneven"),
261                       i, static_cast<unsigned long>(sh_size));
262           continue;
263         }
264
265       rd->relocs.push_back(Section_relocs());
266       Section_relocs& sr(rd->relocs.back());
267       sr.reloc_shndx = i;
268       sr.data_shndx = shndx;
269       sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
270                                            true);
271       sr.sh_type = sh_type;
272       sr.reloc_count = reloc_count;
273       sr.output_section = os;
274       sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
275     }
276
277   // Read the local symbols.
278   gold_assert(this->symtab_shndx_ != -1U);
279   if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
280     rd->local_symbols = NULL;
281   else
282     {
283       typename This::Shdr symtabshdr(pshdrs
284                                      + this->symtab_shndx_ * This::shdr_size);
285       gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
286       const int sym_size = This::sym_size;
287       const unsigned int loccount = this->local_symbol_count_;
288       gold_assert(loccount == symtabshdr.get_sh_info());
289       off_t locsize = loccount * sym_size;
290       rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
291                                                  locsize, true);
292     }
293 }
294
295 // Scan the relocs and adjust the symbol table.  This looks for
296 // relocations which require GOT/PLT/COPY relocations.
297
298 template<int size, bool big_endian>
299 void
300 Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
301                                                Symbol_table* symtab,
302                                                Layout* layout,
303                                                Read_relocs_data* rd)
304 {
305   Sized_target<size, big_endian>* target = this->sized_target();
306
307   const unsigned char* local_symbols;
308   if (rd->local_symbols == NULL)
309     local_symbols = NULL;
310   else
311     local_symbols = rd->local_symbols->data();
312
313   for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
314        p != rd->relocs.end();
315        ++p)
316     {
317       if (!parameters->output_is_object())
318         target->scan_relocs(options, symtab, layout, this, p->data_shndx,
319                             p->sh_type, p->contents->data(), p->reloc_count,
320                             p->output_section,
321                             p->needs_special_offset_handling,
322                             this->local_symbol_count_,
323                             local_symbols);
324       else
325         {
326           Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
327           gold_assert(rr != NULL);
328           rr->set_reloc_count(p->reloc_count);
329           target->scan_relocatable_relocs(options, symtab, layout, this,
330                                           p->data_shndx, p->sh_type,
331                                           p->contents->data(),
332                                           p->reloc_count,
333                                           p->output_section,
334                                           p->needs_special_offset_handling,
335                                           this->local_symbol_count_,
336                                           local_symbols,
337                                           rr);
338         }
339
340       delete p->contents;
341       p->contents = NULL;
342     }
343
344   if (rd->local_symbols != NULL)
345     {
346       delete rd->local_symbols;
347       rd->local_symbols = NULL;
348     }
349 }
350
351 // Relocate the input sections and write out the local symbols.
352
353 template<int size, bool big_endian>
354 void
355 Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
356                                             const Symbol_table* symtab,
357                                             const Layout* layout,
358                                             Output_file* of)
359 {
360   unsigned int shnum = this->shnum();
361
362   // Read the section headers.
363   const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
364                                                shnum * This::shdr_size,
365                                                true);
366
367   Views views;
368   views.resize(shnum);
369
370   // Make two passes over the sections.  The first one copies the
371   // section data to the output file.  The second one applies
372   // relocations.
373
374   this->write_sections(pshdrs, of, &views);
375
376   // To speed up relocations, we set up hash tables for fast lookup of
377   // input offsets to output addresses.
378   this->initialize_input_to_output_maps();
379
380   // Apply relocations.
381
382   this->relocate_sections(options, symtab, layout, pshdrs, &views);
383
384   // After we've done the relocations, we release the hash tables,
385   // since we no longer need them.
386   this->free_input_to_output_maps();
387
388   // Write out the accumulated views.
389   for (unsigned int i = 1; i < shnum; ++i)
390     {
391       if (views[i].view != NULL)
392         {
393           if (!views[i].is_postprocessing_view)
394             {
395               if (views[i].is_input_output_view)
396                 of->write_input_output_view(views[i].offset,
397                                             views[i].view_size,
398                                             views[i].view);
399               else
400                 of->write_output_view(views[i].offset, views[i].view_size,
401                                       views[i].view);
402             }
403         }
404     }
405
406   // Write out the local symbols.
407   this->write_local_symbols(of, layout->sympool(), layout->dynpool());
408
409   // We should no longer need the local symbol values.
410   this->clear_local_symbols();
411 }
412
413 // Sort a Read_multiple vector by file offset.
414 struct Read_multiple_compare
415 {
416   inline bool
417   operator()(const File_read::Read_multiple_entry& rme1,
418              const File_read::Read_multiple_entry& rme2) const
419   { return rme1.file_offset < rme2.file_offset; }
420 };
421
422 // Write section data to the output file.  PSHDRS points to the
423 // section headers.  Record the views in *PVIEWS for use when
424 // relocating.
425
426 template<int size, bool big_endian>
427 void
428 Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
429                                                Output_file* of,
430                                                Views* pviews)
431 {
432   unsigned int shnum = this->shnum();
433   const std::vector<Map_to_output>& map_sections(this->map_to_output());
434
435   File_read::Read_multiple rm;
436   bool is_sorted = true;
437
438   const unsigned char* p = pshdrs + This::shdr_size;
439   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
440     {
441       View_size* pvs = &(*pviews)[i];
442
443       pvs->view = NULL;
444
445       const Output_section* os = map_sections[i].output_section;
446       if (os == NULL)
447         continue;
448       off_t output_offset = map_sections[i].offset;
449
450       typename This::Shdr shdr(p);
451
452       if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
453         continue;
454
455       if (parameters->output_is_object()
456           && (shdr.get_sh_type() == elfcpp::SHT_REL
457               || shdr.get_sh_type() == elfcpp::SHT_RELA)
458           && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
459         {
460           // This is a reloc section in a relocatable link.  We don't
461           // need to read the input file.  The size and file offset
462           // are stored in the Relocatable_relocs structure.
463           Relocatable_relocs* rr = this->relocatable_relocs(i);
464           gold_assert(rr != NULL);
465           Output_data* posd = rr->output_data();
466           gold_assert(posd != NULL);
467
468           pvs->offset = posd->offset();
469           pvs->view_size = posd->data_size();
470           pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
471           pvs->address = posd->address();
472           pvs->is_input_output_view = false;
473           pvs->is_postprocessing_view = false;
474
475           continue;
476         }
477
478       // In the normal case, this input section is simply mapped to
479       // the output section at offset OUTPUT_OFFSET.
480
481       // However, if OUTPUT_OFFSET == -1, then input data is handled
482       // specially--e.g., a .eh_frame section.  The relocation
483       // routines need to check for each reloc where it should be
484       // applied.  For this case, we need an input/output view for the
485       // entire contents of the section in the output file.  We don't
486       // want to copy the contents of the input section to the output
487       // section; the output section contents were already written,
488       // and we waited for them in Relocate_task::is_runnable because
489       // relocs_must_follow_section_writes is set for the object.
490
491       // Regardless of which of the above cases is true, we have to
492       // check requires_postprocessing of the output section.  If that
493       // is false, then we work with views of the output file
494       // directly.  If it is true, then we work with a separate
495       // buffer, and the output section is responsible for writing the
496       // final data to the output file.
497
498       off_t output_section_offset;
499       off_t output_section_size;
500       if (!os->requires_postprocessing())
501         {
502           output_section_offset = os->offset();
503           output_section_size = os->data_size();
504         }
505       else
506         {
507           output_section_offset = 0;
508           output_section_size = os->postprocessing_buffer_size();
509         }
510
511       off_t view_start;
512       section_size_type view_size;
513       if (output_offset != -1)
514         {
515           view_start = output_section_offset + output_offset;
516           view_size = convert_to_section_size_type(shdr.get_sh_size());
517         }
518       else
519         {
520           view_start = output_section_offset;
521           view_size = convert_to_section_size_type(output_section_size);
522         }
523
524       if (view_size == 0)
525         continue;
526
527       gold_assert(output_offset == -1
528                   || (output_offset >= 0
529                       && (output_offset + static_cast<off_t>(view_size)
530                           <= output_section_size)));
531
532       unsigned char* view;
533       if (os->requires_postprocessing())
534         {
535           unsigned char* buffer = os->postprocessing_buffer();
536           view = buffer + view_start;
537           if (output_offset != -1)
538             {
539               off_t sh_offset = shdr.get_sh_offset();
540               if (!rm.empty() && rm.back().file_offset > sh_offset)
541                 is_sorted = false;
542               rm.push_back(File_read::Read_multiple_entry(sh_offset,
543                                                           view_size, view));
544             }
545         }
546       else
547         {
548           if (output_offset == -1)
549             view = of->get_input_output_view(view_start, view_size);
550           else
551             {
552               view = of->get_output_view(view_start, view_size);
553               off_t sh_offset = shdr.get_sh_offset();
554               if (!rm.empty() && rm.back().file_offset > sh_offset)
555                 is_sorted = false;
556               rm.push_back(File_read::Read_multiple_entry(sh_offset,
557                                                           view_size, view));
558             }
559         }
560
561       pvs->view = view;
562       pvs->address = os->address();
563       if (output_offset != -1)
564         pvs->address += output_offset;
565       pvs->offset = view_start;
566       pvs->view_size = view_size;
567       pvs->is_input_output_view = output_offset == -1;
568       pvs->is_postprocessing_view = os->requires_postprocessing();
569     }
570
571   // Actually read the data.
572   if (!rm.empty())
573     {
574       if (!is_sorted)
575         std::sort(rm.begin(), rm.end(), Read_multiple_compare());
576       this->read_multiple(rm);
577     }
578 }
579
580 // Relocate section data.  VIEWS points to the section data as views
581 // in the output file.
582
583 template<int size, bool big_endian>
584 void
585 Sized_relobj<size, big_endian>::relocate_sections(
586     const General_options& options,
587     const Symbol_table* symtab,
588     const Layout* layout,
589     const unsigned char* pshdrs,
590     Views* pviews)
591 {
592   unsigned int shnum = this->shnum();
593   Sized_target<size, big_endian>* target = this->sized_target();
594
595   const std::vector<Map_to_output>& map_sections(this->map_to_output());
596
597   Relocate_info<size, big_endian> relinfo;
598   relinfo.options = &options;
599   relinfo.symtab = symtab;
600   relinfo.layout = layout;
601   relinfo.object = this;
602
603   const unsigned char* p = pshdrs + This::shdr_size;
604   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
605     {
606       typename This::Shdr shdr(p);
607
608       unsigned int sh_type = shdr.get_sh_type();
609       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
610         continue;
611
612       unsigned int index = shdr.get_sh_info();
613       if (index >= this->shnum())
614         {
615           this->error(_("relocation section %u has bad info %u"),
616                       i, index);
617           continue;
618         }
619
620       Output_section* os = map_sections[index].output_section;
621       if (os == NULL)
622         {
623           // This relocation section is against a section which we
624           // discarded.
625           continue;
626         }
627       off_t output_offset = map_sections[index].offset;
628
629       gold_assert((*pviews)[index].view != NULL);
630       if (parameters->output_is_object())
631         gold_assert((*pviews)[i].view != NULL);
632
633       if (shdr.get_sh_link() != this->symtab_shndx_)
634         {
635           gold_error(_("relocation section %u uses unexpected "
636                        "symbol table %u"),
637                      i, shdr.get_sh_link());
638           continue;
639         }
640
641       off_t sh_size = shdr.get_sh_size();
642       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
643                                                     sh_size, false);
644
645       unsigned int reloc_size;
646       if (sh_type == elfcpp::SHT_REL)
647         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
648       else
649         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
650
651       if (reloc_size != shdr.get_sh_entsize())
652         {
653           gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
654                      i, static_cast<unsigned long>(shdr.get_sh_entsize()),
655                      reloc_size);
656           continue;
657         }
658
659       size_t reloc_count = sh_size / reloc_size;
660       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
661         {
662           gold_error(_("reloc section %u size %lu uneven"),
663                      i, static_cast<unsigned long>(sh_size));
664           continue;
665         }
666
667       gold_assert(output_offset != -1
668                   || this->relocs_must_follow_section_writes());
669
670       relinfo.reloc_shndx = i;
671       relinfo.data_shndx = index;
672       if (!parameters->output_is_object())
673         target->relocate_section(&relinfo,
674                                  sh_type,
675                                  prelocs,
676                                  reloc_count,
677                                  os,
678                                  output_offset == -1,
679                                  (*pviews)[index].view,
680                                  (*pviews)[index].address,
681                                  (*pviews)[index].view_size);
682       else
683         {
684           Relocatable_relocs* rr = this->relocatable_relocs(i);
685           target->relocate_for_relocatable(&relinfo,
686                                            sh_type,
687                                            prelocs,
688                                            reloc_count,
689                                            os,
690                                            output_offset,
691                                            rr,
692                                            (*pviews)[index].view,
693                                            (*pviews)[index].address,
694                                            (*pviews)[index].view_size,
695                                            (*pviews)[i].view,
696                                            (*pviews)[i].view_size);
697         }
698     }
699 }
700
701 // Create merge hash tables for the local symbols.  These are used to
702 // speed up relocations.
703
704 template<int size, bool big_endian>
705 void
706 Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
707 {
708   const unsigned int loccount = this->local_symbol_count_;
709   for (unsigned int i = 1; i < loccount; ++i)
710     {
711       Symbol_value<size>& lv(this->local_values_[i]);
712       lv.initialize_input_to_output_map(this);
713     }
714 }
715
716 // Free merge hash tables for the local symbols.
717
718 template<int size, bool big_endian>
719 void
720 Sized_relobj<size, big_endian>::free_input_to_output_maps()
721 {
722   const unsigned int loccount = this->local_symbol_count_;
723   for (unsigned int i = 1; i < loccount; ++i)
724     {
725       Symbol_value<size>& lv(this->local_values_[i]);
726       lv.free_input_to_output_map();
727     }
728 }
729
730 // Class Merged_symbol_value.
731
732 template<int size>
733 void
734 Merged_symbol_value<size>::initialize_input_to_output_map(
735     const Relobj* object,
736     unsigned int input_shndx)
737 {
738   Object_merge_map* map = object->merge_map();
739   map->initialize_input_to_output_map<size>(input_shndx,
740                                             this->output_start_address_,
741                                             &this->output_addresses_);
742 }
743
744 // Get the output value corresponding to an input offset if we
745 // couldn't find it in the hash table.
746
747 template<int size>
748 typename elfcpp::Elf_types<size>::Elf_Addr
749 Merged_symbol_value<size>::value_from_output_section(
750     const Relobj* object,
751     unsigned int input_shndx,
752     typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
753 {
754   section_offset_type output_offset;
755   bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
756                                                       input_offset,
757                                                       &output_offset);
758
759   // If this assertion fails, it means that some relocation was
760   // against a portion of an input merge section which we didn't map
761   // to the output file and we didn't explicitly discard.  We should
762   // always map all portions of input merge sections.
763   gold_assert(found);
764
765   if (output_offset == -1)
766     return 0;
767   else
768     return this->output_start_address_ + output_offset;
769 }
770
771 // Copy_relocs::Copy_reloc_entry methods.
772
773 // Return whether we should emit this reloc.  We should emit it if the
774 // symbol is still defined in a dynamic object.  If we should not emit
775 // it, we clear it, to save ourselves the test next time.
776
777 template<int size, bool big_endian>
778 bool
779 Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
780 {
781   if (this->sym_ == NULL)
782     return false;
783   if (this->sym_->is_from_dynobj())
784     return true;
785   this->sym_ = NULL;
786   return false;
787 }
788
789 // Emit a reloc into a SHT_REL section.
790
791 template<int size, bool big_endian>
792 void
793 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
794     Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
795 {
796   this->sym_->set_needs_dynsym_entry();
797   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
798                          this->relobj_, this->shndx_, this->address_);
799 }
800
801 // Emit a reloc into a SHT_RELA section.
802
803 template<int size, bool big_endian>
804 void
805 Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
806     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
807 {
808   this->sym_->set_needs_dynsym_entry();
809   reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
810                          this->relobj_, this->shndx_, this->address_,
811                          this->addend_);
812 }
813
814 // Copy_relocs methods.
815
816 // Return whether we need a COPY reloc for a relocation against GSYM.
817 // The relocation is being applied to section SHNDX in OBJECT.
818
819 template<int size, bool big_endian>
820 bool
821 Copy_relocs<size, big_endian>::need_copy_reloc(
822     const General_options*,
823     Relobj* object,
824     unsigned int shndx,
825     Sized_symbol<size>* sym)
826 {
827   // FIXME: Handle -z nocopyrelocs.
828
829   if (sym->symsize() == 0)
830     return false;
831
832   // If this is a readonly section, then we need a COPY reloc.
833   // Otherwise we can use a dynamic reloc.
834   if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
835     return true;
836
837   return false;
838 }
839
840 // Save a Rel reloc.
841
842 template<int size, bool big_endian>
843 void
844 Copy_relocs<size, big_endian>::save(
845     Symbol* sym,
846     Relobj* relobj,
847     unsigned int shndx,
848     Output_section* output_section,
849     const elfcpp::Rel<size, big_endian>& rel)
850 {
851   unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
852   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
853                                             output_section,
854                                             rel.get_r_offset(), 0));
855 }
856
857 // Save a Rela reloc.
858
859 template<int size, bool big_endian>
860 void
861 Copy_relocs<size, big_endian>::save(
862     Symbol* sym,
863     Relobj* relobj,
864     unsigned int shndx,
865     Output_section* output_section,
866     const elfcpp::Rela<size, big_endian>& rela)
867 {
868   unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
869   this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
870                                             output_section,
871                                             rela.get_r_offset(),
872                                             rela.get_r_addend()));
873 }
874
875 // Return whether there are any relocs to emit.  We don't want to emit
876 // a reloc if the symbol is no longer defined in a dynamic object.
877
878 template<int size, bool big_endian>
879 bool
880 Copy_relocs<size, big_endian>::any_to_emit()
881 {
882   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
883        p != this->entries_.end();
884        ++p)
885     {
886       if (p->should_emit())
887         return true;
888     }
889   return false;
890 }
891
892 // Emit relocs.
893
894 template<int size, bool big_endian>
895 template<int sh_type>
896 void
897 Copy_relocs<size, big_endian>::emit(
898     Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
899 {
900   for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
901        p != this->entries_.end();
902        ++p)
903     {
904       if (p->should_emit())
905         p->emit(reloc_data);
906     }
907 }
908
909 // Track_relocs methods.
910
911 // Initialize the class to track the relocs.  This gets the object,
912 // the reloc section index, and the type of the relocs.  This returns
913 // false if something goes wrong.
914
915 template<int size, bool big_endian>
916 bool
917 Track_relocs<size, big_endian>::initialize(
918     Object* object,
919     unsigned int reloc_shndx,
920     unsigned int reloc_type)
921 {
922   // If RELOC_SHNDX is -1U, it means there is more than one reloc
923   // section for the .eh_frame section.  We can't handle that case.
924   if (reloc_shndx == -1U)
925     return false;
926
927   // If RELOC_SHNDX is 0, there is no reloc section.
928   if (reloc_shndx == 0)
929     return true;
930
931   // Get the contents of the reloc section.
932   this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
933
934   if (reloc_type == elfcpp::SHT_REL)
935     this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
936   else if (reloc_type == elfcpp::SHT_RELA)
937     this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
938   else
939     gold_unreachable();
940
941   if (this->len_ % this->reloc_size_ != 0)
942     {
943       object->error(_("reloc section size %zu is not a multiple of "
944                       "reloc size %d\n"),
945                     static_cast<size_t>(this->len_),
946                     this->reloc_size_);
947       return false;
948     }
949
950   return true;
951 }
952
953 // Return the offset of the next reloc, or -1 if there isn't one.
954
955 template<int size, bool big_endian>
956 off_t
957 Track_relocs<size, big_endian>::next_offset() const
958 {
959   if (this->pos_ >= this->len_)
960     return -1;
961
962   // Rel and Rela start out the same, so we can always use Rel to find
963   // the r_offset value.
964   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
965   return rel.get_r_offset();
966 }
967
968 // Return the index of the symbol referenced by the next reloc, or -1U
969 // if there aren't any more relocs.
970
971 template<int size, bool big_endian>
972 unsigned int
973 Track_relocs<size, big_endian>::next_symndx() const
974 {
975   if (this->pos_ >= this->len_)
976     return -1U;
977
978   // Rel and Rela start out the same, so we can use Rel to find the
979   // symbol index.
980   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
981   return elfcpp::elf_r_sym<size>(rel.get_r_info());
982 }
983
984 // Advance to the next reloc whose r_offset is greater than or equal
985 // to OFFSET.  Return the number of relocs we skip.
986
987 template<int size, bool big_endian>
988 int
989 Track_relocs<size, big_endian>::advance(off_t offset)
990 {
991   int ret = 0;
992   while (this->pos_ < this->len_)
993     {
994       // Rel and Rela start out the same, so we can always use Rel to
995       // find the r_offset value.
996       elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
997       if (static_cast<off_t>(rel.get_r_offset()) >= offset)
998         break;
999       ++ret;
1000       this->pos_ += this->reloc_size_;
1001     }
1002   return ret;
1003 }
1004
1005 // Instantiate the templates we need.  We could use the configure
1006 // script to restrict this to only the ones for implemented targets.
1007
1008 #ifdef HAVE_TARGET_32_LITTLE
1009 template
1010 void
1011 Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
1012 #endif
1013
1014 #ifdef HAVE_TARGET_32_BIG
1015 template
1016 void
1017 Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
1018 #endif
1019
1020 #ifdef HAVE_TARGET_64_LITTLE
1021 template
1022 void
1023 Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
1024 #endif
1025
1026 #ifdef HAVE_TARGET_64_BIG
1027 template
1028 void
1029 Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
1030 #endif
1031
1032 #ifdef HAVE_TARGET_32_LITTLE
1033 template
1034 void
1035 Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
1036                                         Symbol_table* symtab,
1037                                         Layout* layout,
1038                                         Read_relocs_data* rd);
1039 #endif
1040
1041 #ifdef HAVE_TARGET_32_BIG
1042 template
1043 void
1044 Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
1045                                        Symbol_table* symtab,
1046                                        Layout* layout,
1047                                        Read_relocs_data* rd);
1048 #endif
1049
1050 #ifdef HAVE_TARGET_64_LITTLE
1051 template
1052 void
1053 Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
1054                                         Symbol_table* symtab,
1055                                         Layout* layout,
1056                                         Read_relocs_data* rd);
1057 #endif
1058
1059 #ifdef HAVE_TARGET_64_BIG
1060 template
1061 void
1062 Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
1063                                        Symbol_table* symtab,
1064                                        Layout* layout,
1065                                        Read_relocs_data* rd);
1066 #endif
1067
1068 #ifdef HAVE_TARGET_32_LITTLE
1069 template
1070 void
1071 Sized_relobj<32, false>::do_relocate(const General_options& options,
1072                                      const Symbol_table* symtab,
1073                                      const Layout* layout,
1074                                      Output_file* of);
1075 #endif
1076
1077 #ifdef HAVE_TARGET_32_BIG
1078 template
1079 void
1080 Sized_relobj<32, true>::do_relocate(const General_options& options,
1081                                     const Symbol_table* symtab,
1082                                     const Layout* layout,
1083                                     Output_file* of);
1084 #endif
1085
1086 #ifdef HAVE_TARGET_64_LITTLE
1087 template
1088 void
1089 Sized_relobj<64, false>::do_relocate(const General_options& options,
1090                                      const Symbol_table* symtab,
1091                                      const Layout* layout,
1092                                      Output_file* of);
1093 #endif
1094
1095 #ifdef HAVE_TARGET_64_BIG
1096 template
1097 void
1098 Sized_relobj<64, true>::do_relocate(const General_options& options,
1099                                     const Symbol_table* symtab,
1100                                     const Layout* layout,
1101                                     Output_file* of);
1102 #endif
1103
1104 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1105 template
1106 class Merged_symbol_value<32>;
1107 #endif
1108
1109 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1110 template
1111 class Merged_symbol_value<64>;
1112 #endif
1113
1114 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1115 template
1116 class Symbol_value<32>;
1117 #endif
1118
1119 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1120 template
1121 class Symbol_value<64>;
1122 #endif
1123
1124 #ifdef HAVE_TARGET_32_LITTLE
1125 template
1126 class Copy_relocs<32, false>;
1127 #endif
1128
1129 #ifdef HAVE_TARGET_32_BIG
1130 template
1131 class Copy_relocs<32, true>;
1132 #endif
1133
1134 #ifdef HAVE_TARGET_64_LITTLE
1135 template
1136 class Copy_relocs<64, false>;
1137 #endif
1138
1139 #ifdef HAVE_TARGET_64_BIG
1140 template
1141 class Copy_relocs<64, true>;
1142 #endif
1143
1144 #ifdef HAVE_TARGET_32_LITTLE
1145 template
1146 void
1147 Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
1148     Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
1149 #endif
1150
1151 #ifdef HAVE_TARGET_32_BIG
1152 template
1153 void
1154 Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
1155     Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
1156 #endif
1157
1158 #ifdef HAVE_TARGET_64_LITTLE
1159 template
1160 void
1161 Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
1162     Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
1163 #endif
1164
1165 #ifdef HAVE_TARGET_64_BIG
1166 template
1167 void
1168 Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
1169     Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
1170 #endif
1171
1172 #ifdef HAVE_TARGET_32_LITTLE
1173 template
1174 void
1175 Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
1176     Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
1177 #endif
1178
1179 #ifdef HAVE_TARGET_32_BIG
1180 template
1181 void
1182 Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1183     Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
1184 #endif
1185
1186 #ifdef HAVE_TARGET_64_LITTLE
1187 template
1188 void
1189 Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1190     Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
1191 #endif
1192
1193 #ifdef HAVE_TARGET_64_BIG
1194 template
1195 void
1196 Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1197     Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
1198 #endif
1199
1200 #ifdef HAVE_TARGET_32_LITTLE
1201 template
1202 class Track_relocs<32, false>;
1203 #endif
1204
1205 #ifdef HAVE_TARGET_32_BIG
1206 template
1207 class Track_relocs<32, true>;
1208 #endif
1209
1210 #ifdef HAVE_TARGET_64_LITTLE
1211 template
1212 class Track_relocs<64, false>;
1213 #endif
1214
1215 #ifdef HAVE_TARGET_64_BIG
1216 template
1217 class Track_relocs<64, true>;
1218 #endif
1219
1220 } // End namespace gold.