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