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