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