Make output views accessible to Target during do_relocate().
[external/binutils.git] / gold / reloc.cc
1 // reloc.cc -- relocate input files for gold.
2
3 // Copyright (C) 2006-2015 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 "layout.h"
29 #include "symtab.h"
30 #include "output.h"
31 #include "merge.h"
32 #include "object.h"
33 #include "target-reloc.h"
34 #include "reloc.h"
35 #include "icf.h"
36 #include "compressed_output.h"
37 #include "incremental.h"
38
39 namespace gold
40 {
41
42 // Read_relocs methods.
43
44 // These tasks just read the relocation information from the file.
45 // After reading it, the start another task to process the
46 // information.  These tasks requires access to the file.
47
48 Task_token*
49 Read_relocs::is_runnable()
50 {
51   return this->object_->is_locked() ? this->object_->token() : NULL;
52 }
53
54 // Lock the file.
55
56 void
57 Read_relocs::locks(Task_locker* tl)
58 {
59   Task_token* token = this->object_->token();
60   if (token != NULL)
61     tl->add(this, token);
62 }
63
64 // Read the relocations and then start a Scan_relocs_task.
65
66 void
67 Read_relocs::run(Workqueue* workqueue)
68 {
69   Read_relocs_data* rd = new Read_relocs_data;
70   this->object_->read_relocs(rd);
71   this->object_->set_relocs_data(rd);
72   this->object_->release();
73
74   // If garbage collection or identical comdat folding is desired, we  
75   // process the relocs first before scanning them.  Scanning of relocs is
76   // done only after garbage or identical sections is identified.
77   if (parameters->options().gc_sections()
78       || parameters->options().icf_enabled())
79     {
80       workqueue->queue_next(new Gc_process_relocs(this->symtab_,
81                                                   this->layout_, 
82                                                   this->object_, rd,
83                                                   this->this_blocker_,
84                                                   this->next_blocker_));
85     }
86   else
87     {
88       workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_,
89                                             this->object_, rd,
90                                             this->this_blocker_,
91                                             this->next_blocker_));
92     }
93 }
94
95 // Return a debugging name for the task.
96
97 std::string
98 Read_relocs::get_name() const
99 {
100   return "Read_relocs " + this->object_->name();
101 }
102
103 // Gc_process_relocs methods.
104
105 Gc_process_relocs::~Gc_process_relocs()
106 {
107   if (this->this_blocker_ != NULL)
108     delete this->this_blocker_;
109 }
110
111 // These tasks process the relocations read by Read_relocs and
112 // determine which sections are referenced and which are garbage.
113 // This task is done only when --gc-sections is used.  This is blocked
114 // by THIS_BLOCKER_.  It unblocks NEXT_BLOCKER_.
115
116 Task_token*
117 Gc_process_relocs::is_runnable()
118 {
119   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
120     return this->this_blocker_;
121   if (this->object_->is_locked())
122     return this->object_->token();
123   return NULL;
124 }
125
126 void
127 Gc_process_relocs::locks(Task_locker* tl)
128 {
129   tl->add(this, this->object_->token());
130   tl->add(this, this->next_blocker_);
131 }
132
133 void
134 Gc_process_relocs::run(Workqueue*)
135 {
136   this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_);
137   this->object_->release();
138 }
139
140 // Return a debugging name for the task.
141
142 std::string
143 Gc_process_relocs::get_name() const
144 {
145   return "Gc_process_relocs " + this->object_->name();
146 }
147
148 // Scan_relocs methods.
149
150 Scan_relocs::~Scan_relocs()
151 {
152   if (this->this_blocker_ != NULL)
153     delete this->this_blocker_;
154 }
155
156 // These tasks scan the relocations read by Read_relocs and mark up
157 // the symbol table to indicate which relocations are required.  We
158 // use a lock on the symbol table to keep them from interfering with
159 // each other.
160
161 Task_token*
162 Scan_relocs::is_runnable()
163 {
164   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
165     return this->this_blocker_;
166   if (this->object_->is_locked())
167     return this->object_->token();
168   return NULL;
169 }
170
171 // Return the locks we hold: one on the file, one on the symbol table
172 // and one blocker.
173
174 void
175 Scan_relocs::locks(Task_locker* tl)
176 {
177   Task_token* token = this->object_->token();
178   if (token != NULL)
179     tl->add(this, token);
180   tl->add(this, this->next_blocker_);
181 }
182
183 // Scan the relocs.
184
185 void
186 Scan_relocs::run(Workqueue*)
187 {
188   this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_);
189   delete this->rd_;
190   this->rd_ = NULL;
191   this->object_->release();
192 }
193
194 // Return a debugging name for the task.
195
196 std::string
197 Scan_relocs::get_name() const
198 {
199   return "Scan_relocs " + this->object_->name();
200 }
201
202 // Relocate_task methods.
203
204 // We may have to wait for the output sections to be written.
205
206 Task_token*
207 Relocate_task::is_runnable()
208 {
209   if (this->object_->relocs_must_follow_section_writes()
210       && this->output_sections_blocker_->is_blocked())
211     return this->output_sections_blocker_;
212
213   if (this->object_->is_locked())
214     return this->object_->token();
215
216   return NULL;
217 }
218
219 // We want to lock the file while we run.  We want to unblock
220 // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
221 // INPUT_SECTIONS_BLOCKER may be NULL.
222
223 void
224 Relocate_task::locks(Task_locker* tl)
225 {
226   if (this->input_sections_blocker_ != NULL)
227     tl->add(this, this->input_sections_blocker_);
228   tl->add(this, this->final_blocker_);
229   Task_token* token = this->object_->token();
230   if (token != NULL)
231     tl->add(this, token);
232 }
233
234 // Run the task.
235
236 void
237 Relocate_task::run(Workqueue*)
238 {
239   this->object_->relocate(this->symtab_, this->layout_, this->of_);
240
241   // This is normally the last thing we will do with an object, so
242   // uncache all views.
243   this->object_->clear_view_cache_marks();
244
245   this->object_->release();
246 }
247
248 // Return a debugging name for the task.
249
250 std::string
251 Relocate_task::get_name() const
252 {
253   return "Relocate_task " + this->object_->name();
254 }
255
256 // Read the relocs and local symbols from the object file and store
257 // the information in RD.
258
259 template<int size, bool big_endian>
260 void
261 Sized_relobj_file<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
262 {
263   rd->relocs.clear();
264
265   unsigned int shnum = this->shnum();
266   if (shnum == 0)
267     return;
268
269   rd->relocs.reserve(shnum / 2);
270
271   const Output_sections& out_sections(this->output_sections());
272   const std::vector<Address>& out_offsets(this->section_offsets());
273
274   const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
275                                                shnum * This::shdr_size,
276                                                true, true);
277   // Skip the first, dummy, section.
278   const unsigned char* ps = pshdrs + This::shdr_size;
279   for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
280     {
281       typename This::Shdr shdr(ps);
282
283       unsigned int sh_type = shdr.get_sh_type();
284       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
285         continue;
286
287       unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
288       if (shndx >= shnum)
289         {
290           this->error(_("relocation section %u has bad info %u"),
291                       i, shndx);
292           continue;
293         }
294
295       Output_section* os = out_sections[shndx];
296       if (os == NULL)
297         continue;
298
299       // We are scanning relocations in order to fill out the GOT and
300       // PLT sections.  Relocations for sections which are not
301       // allocated (typically debugging sections) should not add new
302       // GOT and PLT entries.  So we skip them unless this is a
303       // relocatable link or we need to emit relocations.  FIXME: What
304       // should we do if a linker script maps a section with SHF_ALLOC
305       // clear to a section with SHF_ALLOC set?
306       typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
307       bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
308                                    != 0);
309       if (!is_section_allocated
310           && !parameters->options().relocatable()
311           && !parameters->options().emit_relocs()
312           && !parameters->incremental())
313         continue;
314
315       if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
316         {
317           this->error(_("relocation section %u uses unexpected "
318                         "symbol table %u"),
319                       i, this->adjust_shndx(shdr.get_sh_link()));
320           continue;
321         }
322
323       off_t sh_size = shdr.get_sh_size();
324
325       if (sh_size == 0)
326         continue;
327
328       unsigned int reloc_size;
329       if (sh_type == elfcpp::SHT_REL)
330         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
331       else
332         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
333       if (reloc_size != shdr.get_sh_entsize())
334         {
335           this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
336                       i, static_cast<unsigned long>(shdr.get_sh_entsize()),
337                       reloc_size);
338           continue;
339         }
340
341       size_t reloc_count = sh_size / reloc_size;
342       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
343         {
344           this->error(_("reloc section %u size %lu uneven"),
345                       i, static_cast<unsigned long>(sh_size));
346           continue;
347         }
348
349       rd->relocs.push_back(Section_relocs());
350       Section_relocs& sr(rd->relocs.back());
351       sr.reloc_shndx = i;
352       sr.data_shndx = shndx;
353       sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
354                                            true, true);
355       sr.sh_type = sh_type;
356       sr.reloc_count = reloc_count;
357       sr.output_section = os;
358       sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
359       sr.is_data_section_allocated = is_section_allocated;
360     }
361
362   // Read the local symbols.
363   gold_assert(this->symtab_shndx_ != -1U);
364   if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
365     rd->local_symbols = NULL;
366   else
367     {
368       typename This::Shdr symtabshdr(pshdrs
369                                      + this->symtab_shndx_ * This::shdr_size);
370       gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
371       const int sym_size = This::sym_size;
372       const unsigned int loccount = this->local_symbol_count_;
373       gold_assert(loccount == symtabshdr.get_sh_info());
374       off_t locsize = loccount * sym_size;
375       rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
376                                                  locsize, true, true);
377     }
378 }
379
380 // Process the relocs to generate mappings from source sections to referenced
381 // sections.  This is used during garbage collection to determine garbage
382 // sections.
383
384 template<int size, bool big_endian>
385 void
386 Sized_relobj_file<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab,
387                                                           Layout* layout,
388                                                           Read_relocs_data* rd)
389 {  
390   Sized_target<size, big_endian>* target =
391     parameters->sized_target<size, big_endian>();
392
393   const unsigned char* local_symbols;
394   if (rd->local_symbols == NULL)
395     local_symbols = NULL;
396   else
397     local_symbols = rd->local_symbols->data();
398
399   for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
400        p != rd->relocs.end();
401        ++p)
402     {
403       if (!parameters->options().relocatable())
404           {
405             // As noted above, when not generating an object file, we
406             // only scan allocated sections.  We may see a non-allocated
407             // section here if we are emitting relocs.
408             if (p->is_data_section_allocated)
409               target->gc_process_relocs(symtab, layout, this, 
410                                         p->data_shndx, p->sh_type, 
411                                         p->contents->data(), p->reloc_count, 
412                                         p->output_section,
413                                         p->needs_special_offset_handling,
414                                         this->local_symbol_count_, 
415                                         local_symbols);
416         }
417     }
418 }
419
420
421 // Scan the relocs and adjust the symbol table.  This looks for
422 // relocations which require GOT/PLT/COPY relocations.
423
424 template<int size, bool big_endian>
425 void
426 Sized_relobj_file<size, big_endian>::do_scan_relocs(Symbol_table* symtab,
427                                                Layout* layout,
428                                                Read_relocs_data* rd)
429 {
430   Sized_target<size, big_endian>* target =
431     parameters->sized_target<size, big_endian>();
432
433   const unsigned char* local_symbols;
434   if (rd->local_symbols == NULL)
435     local_symbols = NULL;
436   else
437     local_symbols = rd->local_symbols->data();
438
439   // For incremental links, allocate the counters for incremental relocations.
440   if (layout->incremental_inputs() != NULL)
441     this->allocate_incremental_reloc_counts();
442
443   for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
444        p != rd->relocs.end();
445        ++p)
446     {
447       // When garbage collection is on, unreferenced sections are not included
448       // in the link that would have been included normally. This is known only
449       // after Read_relocs hence this check has to be done again.
450       if (parameters->options().gc_sections()
451           || parameters->options().icf_enabled())
452         {
453           if (p->output_section == NULL)
454             continue;
455         }
456       if (!parameters->options().relocatable())
457         {
458           // As noted above, when not generating an object file, we
459           // only scan allocated sections.  We may see a non-allocated
460           // section here if we are emitting relocs.
461           if (p->is_data_section_allocated)
462             target->scan_relocs(symtab, layout, this, p->data_shndx,
463                                 p->sh_type, p->contents->data(),
464                                 p->reloc_count, p->output_section,
465                                 p->needs_special_offset_handling,
466                                 this->local_symbol_count_,
467                                 local_symbols);
468           if (parameters->options().emit_relocs())
469             this->emit_relocs_scan(symtab, layout, local_symbols, p);
470           if (layout->incremental_inputs() != NULL)
471             this->incremental_relocs_scan(p);
472         }
473       else
474         {
475           Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
476           gold_assert(rr != NULL);
477           rr->set_reloc_count(p->reloc_count);
478           target->scan_relocatable_relocs(symtab, layout, this,
479                                           p->data_shndx, p->sh_type,
480                                           p->contents->data(),
481                                           p->reloc_count,
482                                           p->output_section,
483                                           p->needs_special_offset_handling,
484                                           this->local_symbol_count_,
485                                           local_symbols,
486                                           rr);
487         }
488
489       delete p->contents;
490       p->contents = NULL;
491     }
492
493   // For incremental links, finalize the allocation of relocations.
494   if (layout->incremental_inputs() != NULL)
495     this->finalize_incremental_relocs(layout, true);
496
497   if (rd->local_symbols != NULL)
498     {
499       delete rd->local_symbols;
500       rd->local_symbols = NULL;
501     }
502 }
503
504 // This is a strategy class we use when scanning for --emit-relocs.
505
506 template<int sh_type>
507 class Emit_relocs_strategy
508 {
509  public:
510   // A local non-section symbol.
511   inline Relocatable_relocs::Reloc_strategy
512   local_non_section_strategy(unsigned int, Relobj*, unsigned int)
513   { return Relocatable_relocs::RELOC_COPY; }
514
515   // A local section symbol.
516   inline Relocatable_relocs::Reloc_strategy
517   local_section_strategy(unsigned int, Relobj*)
518   {
519     if (sh_type == elfcpp::SHT_RELA)
520       return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
521     else
522       {
523         // The addend is stored in the section contents.  Since this
524         // is not a relocatable link, we are going to apply the
525         // relocation contents to the section as usual.  This means
526         // that we have no way to record the original addend.  If the
527         // original addend is not zero, there is basically no way for
528         // the user to handle this correctly.  Caveat emptor.
529         return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
530       }
531   }
532
533   // A global symbol.
534   inline Relocatable_relocs::Reloc_strategy
535   global_strategy(unsigned int, Relobj*, unsigned int)
536   { return Relocatable_relocs::RELOC_COPY; }
537 };
538
539 // Scan the input relocations for --emit-relocs.
540
541 template<int size, bool big_endian>
542 void
543 Sized_relobj_file<size, big_endian>::emit_relocs_scan(
544     Symbol_table* symtab,
545     Layout* layout,
546     const unsigned char* plocal_syms,
547     const Read_relocs_data::Relocs_list::iterator& p)
548 {
549   Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
550   gold_assert(rr != NULL);
551   rr->set_reloc_count(p->reloc_count);
552
553   if (p->sh_type == elfcpp::SHT_REL)
554     this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout,
555                                                     plocal_syms, p, rr);
556   else
557     {
558       gold_assert(p->sh_type == elfcpp::SHT_RELA);
559       this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout,
560                                                        plocal_syms, p, rr);
561     }
562 }
563
564 // Scan the input relocation for --emit-relocs, templatized on the
565 // type of the relocation section.
566
567 template<int size, bool big_endian>
568 template<int sh_type>
569 void
570 Sized_relobj_file<size, big_endian>::emit_relocs_scan_reltype(
571     Symbol_table* symtab,
572     Layout* layout,
573     const unsigned char* plocal_syms,
574     const Read_relocs_data::Relocs_list::iterator& p,
575     Relocatable_relocs* rr)
576 {
577   scan_relocatable_relocs<size, big_endian, sh_type,
578                           Emit_relocs_strategy<sh_type> >(
579     symtab,
580     layout,
581     this,
582     p->data_shndx,
583     p->contents->data(),
584     p->reloc_count,
585     p->output_section,
586     p->needs_special_offset_handling,
587     this->local_symbol_count_,
588     plocal_syms,
589     rr);
590 }
591
592 // Scan the input relocations for --incremental.
593
594 template<int size, bool big_endian>
595 void
596 Sized_relobj_file<size, big_endian>::incremental_relocs_scan(
597     const Read_relocs_data::Relocs_list::iterator& p)
598 {
599   if (p->sh_type == elfcpp::SHT_REL)
600     this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p);
601   else
602     {
603       gold_assert(p->sh_type == elfcpp::SHT_RELA);
604       this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p);
605     }
606 }
607
608 // Scan the input relocation for --incremental, templatized on the
609 // type of the relocation section.
610
611 template<int size, bool big_endian>
612 template<int sh_type>
613 void
614 Sized_relobj_file<size, big_endian>::incremental_relocs_scan_reltype(
615     const Read_relocs_data::Relocs_list::iterator& p)
616 {
617   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
618   const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
619   const unsigned char* prelocs = p->contents->data();
620   size_t reloc_count = p->reloc_count;
621
622   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
623     {
624       Reltype reloc(prelocs);
625
626       if (p->needs_special_offset_handling
627           && !p->output_section->is_input_address_mapped(this, p->data_shndx,
628                                                          reloc.get_r_offset()))
629         continue;
630
631       typename elfcpp::Elf_types<size>::Elf_WXword r_info =
632           reloc.get_r_info();
633       const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
634
635       if (r_sym >= this->local_symbol_count_)
636         this->count_incremental_reloc(r_sym - this->local_symbol_count_);
637     }
638 }
639
640 // Relocate the input sections and write out the local symbols.
641
642 template<int size, bool big_endian>
643 void
644 Sized_relobj_file<size, big_endian>::do_relocate(const Symbol_table* symtab,
645                                                  const Layout* layout,
646                                                  Output_file* of)
647 {
648   unsigned int shnum = this->shnum();
649
650   // Read the section headers.
651   const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
652                                                shnum * This::shdr_size,
653                                                true, true);
654
655   Views views;
656   views.resize(shnum);
657
658   // Make two passes over the sections.  The first one copies the
659   // section data to the output file.  The second one applies
660   // relocations.
661
662   this->write_sections(layout, pshdrs, of, &views);
663
664   // To speed up relocations, we set up hash tables for fast lookup of
665   // input offsets to output addresses.
666   this->initialize_input_to_output_maps();
667
668   // Make the views available through get_output_view() for the duration
669   // of this routine.  This RAII class will reset output_views_ to NULL
670   // when the views go out of scope.
671   struct Set_output_views
672   {
673     Set_output_views(const Views** ppviews, const Views* pviews)
674     {
675       ppviews_ = ppviews;
676       *ppviews = pviews;
677     }
678
679     ~Set_output_views()
680     { *ppviews_ = NULL; }
681
682     const Views** ppviews_;
683   };
684   Set_output_views set_output_views(&this->output_views_, &views);
685
686   // Apply relocations.
687
688   this->relocate_sections(symtab, layout, pshdrs, of, &views);
689
690   // After we've done the relocations, we release the hash tables,
691   // since we no longer need them.
692   this->free_input_to_output_maps();
693
694   // Write out the accumulated views.
695   for (unsigned int i = 1; i < shnum; ++i)
696     {
697       if (views[i].view != NULL)
698         {
699           if (views[i].is_ctors_reverse_view)
700             this->reverse_words(views[i].view, views[i].view_size);
701           if (!views[i].is_postprocessing_view)
702             {
703               if (views[i].is_input_output_view)
704                 of->write_input_output_view(views[i].offset,
705                                             views[i].view_size,
706                                             views[i].view);
707               else
708                 of->write_output_view(views[i].offset, views[i].view_size,
709                                       views[i].view);
710             }
711         }
712     }
713
714   // Write out the local symbols.
715   this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
716                             layout->symtab_xindex(), layout->dynsym_xindex(),
717                             layout->symtab_section_offset());
718 }
719
720 // Sort a Read_multiple vector by file offset.
721 struct Read_multiple_compare
722 {
723   inline bool
724   operator()(const File_read::Read_multiple_entry& rme1,
725              const File_read::Read_multiple_entry& rme2) const
726   { return rme1.file_offset < rme2.file_offset; }
727 };
728
729 // Write section data to the output file.  PSHDRS points to the
730 // section headers.  Record the views in *PVIEWS for use when
731 // relocating.
732
733 template<int size, bool big_endian>
734 void
735 Sized_relobj_file<size, big_endian>::write_sections(const Layout* layout,
736                                                     const unsigned char* pshdrs,
737                                                     Output_file* of,
738                                                     Views* pviews)
739 {
740   unsigned int shnum = this->shnum();
741   const Output_sections& out_sections(this->output_sections());
742   const std::vector<Address>& out_offsets(this->section_offsets());
743
744   File_read::Read_multiple rm;
745   bool is_sorted = true;
746
747   const unsigned char* p = pshdrs + This::shdr_size;
748   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
749     {
750       View_size* pvs = &(*pviews)[i];
751
752       pvs->view = NULL;
753
754       const Output_section* os = out_sections[i];
755       if (os == NULL)
756         continue;
757       Address output_offset = out_offsets[i];
758
759       typename This::Shdr shdr(p);
760
761       if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
762         continue;
763
764       if ((parameters->options().relocatable()
765            || parameters->options().emit_relocs())
766           && (shdr.get_sh_type() == elfcpp::SHT_REL
767               || shdr.get_sh_type() == elfcpp::SHT_RELA)
768           && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
769         {
770           // This is a reloc section in a relocatable link or when
771           // emitting relocs.  We don't need to read the input file.
772           // The size and file offset are stored in the
773           // Relocatable_relocs structure.
774           Relocatable_relocs* rr = this->relocatable_relocs(i);
775           gold_assert(rr != NULL);
776           Output_data* posd = rr->output_data();
777           gold_assert(posd != NULL);
778
779           pvs->offset = posd->offset();
780           pvs->view_size = posd->data_size();
781           pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
782           pvs->address = posd->address();
783           pvs->is_input_output_view = false;
784           pvs->is_postprocessing_view = false;
785           pvs->is_ctors_reverse_view = false;
786
787           continue;
788         }
789
790       // In the normal case, this input section is simply mapped to
791       // the output section at offset OUTPUT_OFFSET.
792
793       // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
794       // handled specially--e.g., a .eh_frame section.  The relocation
795       // routines need to check for each reloc where it should be
796       // applied.  For this case, we need an input/output view for the
797       // entire contents of the section in the output file.  We don't
798       // want to copy the contents of the input section to the output
799       // section; the output section contents were already written,
800       // and we waited for them in Relocate_task::is_runnable because
801       // relocs_must_follow_section_writes is set for the object.
802
803       // Regardless of which of the above cases is true, we have to
804       // check requires_postprocessing of the output section.  If that
805       // is false, then we work with views of the output file
806       // directly.  If it is true, then we work with a separate
807       // buffer, and the output section is responsible for writing the
808       // final data to the output file.
809
810       off_t output_section_offset;
811       Address output_section_size;
812       if (!os->requires_postprocessing())
813         {
814           output_section_offset = os->offset();
815           output_section_size = convert_types<Address, off_t>(os->data_size());
816         }
817       else
818         {
819           output_section_offset = 0;
820           output_section_size =
821               convert_types<Address, off_t>(os->postprocessing_buffer_size());
822         }
823
824       off_t view_start;
825       section_size_type view_size;
826       bool must_decompress = false;
827       if (output_offset != invalid_address)
828         {
829           view_start = output_section_offset + output_offset;
830           view_size = convert_to_section_size_type(shdr.get_sh_size());
831           section_size_type uncompressed_size;
832           if (this->section_is_compressed(i, &uncompressed_size))
833             {
834               view_size = uncompressed_size;
835               must_decompress = true;
836             }
837         }
838       else
839         {
840           view_start = output_section_offset;
841           view_size = convert_to_section_size_type(output_section_size);
842         }
843
844       if (view_size == 0)
845         continue;
846
847       gold_assert(output_offset == invalid_address
848                   || output_offset + view_size <= output_section_size);
849
850       unsigned char* view;
851       if (os->requires_postprocessing())
852         {
853           unsigned char* buffer = os->postprocessing_buffer();
854           view = buffer + view_start;
855           if (output_offset != invalid_address && !must_decompress)
856             {
857               off_t sh_offset = shdr.get_sh_offset();
858               if (!rm.empty() && rm.back().file_offset > sh_offset)
859                 is_sorted = false;
860               rm.push_back(File_read::Read_multiple_entry(sh_offset,
861                                                           view_size, view));
862             }
863         }
864       else
865         {
866           if (output_offset == invalid_address)
867             view = of->get_input_output_view(view_start, view_size);
868           else
869             {
870               view = of->get_output_view(view_start, view_size);
871               if (!must_decompress)
872                 {
873                   off_t sh_offset = shdr.get_sh_offset();
874                   if (!rm.empty() && rm.back().file_offset > sh_offset)
875                     is_sorted = false;
876                   rm.push_back(File_read::Read_multiple_entry(sh_offset,
877                                                               view_size, view));
878                 }
879             }
880         }
881
882       if (must_decompress)
883         {
884           // Read and decompress the section.
885           section_size_type len;
886           const unsigned char* p = this->section_contents(i, &len, false);
887           if (!decompress_input_section(p, len, view, view_size,
888                                         size, big_endian,
889                                         shdr.get_sh_flags()))
890             this->error(_("could not decompress section %s"),
891                         this->section_name(i).c_str());
892         }
893
894       pvs->view = view;
895       pvs->address = os->address();
896       if (output_offset != invalid_address)
897         pvs->address += output_offset;
898       pvs->offset = view_start;
899       pvs->view_size = view_size;
900       pvs->is_input_output_view = output_offset == invalid_address;
901       pvs->is_postprocessing_view = os->requires_postprocessing();
902       pvs->is_ctors_reverse_view =
903         (!parameters->options().relocatable()
904          && view_size > size / 8
905          && (strcmp(os->name(), ".init_array") == 0
906              || strcmp(os->name(), ".fini_array") == 0)
907          && layout->is_ctors_in_init_array(this, i));
908     }
909
910   // Actually read the data.
911   if (!rm.empty())
912     {
913       if (!is_sorted)
914         std::sort(rm.begin(), rm.end(), Read_multiple_compare());
915       this->read_multiple(rm);
916     }
917 }
918
919 // Relocate section data.  VIEWS points to the section data as views
920 // in the output file.
921
922 template<int size, bool big_endian>
923 void
924 Sized_relobj_file<size, big_endian>::do_relocate_sections(
925     const Symbol_table* symtab,
926     const Layout* layout,
927     const unsigned char* pshdrs,
928     Output_file* of,
929     Views* pviews)
930 {
931   unsigned int shnum = this->shnum();
932   Sized_target<size, big_endian>* target =
933     parameters->sized_target<size, big_endian>();
934
935   const Output_sections& out_sections(this->output_sections());
936   const std::vector<Address>& out_offsets(this->section_offsets());
937
938   Relocate_info<size, big_endian> relinfo;
939   relinfo.symtab = symtab;
940   relinfo.layout = layout;
941   relinfo.object = this;
942
943   const unsigned char* p = pshdrs + This::shdr_size;
944   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
945     {
946       typename This::Shdr shdr(p);
947
948       unsigned int sh_type = shdr.get_sh_type();
949       if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
950         continue;
951
952       off_t sh_size = shdr.get_sh_size();
953       if (sh_size == 0)
954         continue;
955
956       unsigned int index = this->adjust_shndx(shdr.get_sh_info());
957       if (index >= this->shnum())
958         {
959           this->error(_("relocation section %u has bad info %u"),
960                       i, index);
961           continue;
962         }
963
964       Output_section* os = out_sections[index];
965       if (os == NULL)
966         {
967           // This relocation section is against a section which we
968           // discarded.
969           continue;
970         }
971       Address output_offset = out_offsets[index];
972
973       gold_assert((*pviews)[index].view != NULL);
974       if (parameters->options().relocatable())
975         gold_assert((*pviews)[i].view != NULL);
976
977       if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
978         {
979           gold_error(_("relocation section %u uses unexpected "
980                        "symbol table %u"),
981                      i, this->adjust_shndx(shdr.get_sh_link()));
982           continue;
983         }
984
985       const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
986                                                     sh_size, true, false);
987
988       unsigned int reloc_size;
989       if (sh_type == elfcpp::SHT_REL)
990         reloc_size = elfcpp::Elf_sizes<size>::rel_size;
991       else
992         reloc_size = elfcpp::Elf_sizes<size>::rela_size;
993
994       if (reloc_size != shdr.get_sh_entsize())
995         {
996           gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
997                      i, static_cast<unsigned long>(shdr.get_sh_entsize()),
998                      reloc_size);
999           continue;
1000         }
1001
1002       size_t reloc_count = sh_size / reloc_size;
1003       if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
1004         {
1005           gold_error(_("reloc section %u size %lu uneven"),
1006                      i, static_cast<unsigned long>(sh_size));
1007           continue;
1008         }
1009
1010       gold_assert(output_offset != invalid_address
1011                   || this->relocs_must_follow_section_writes());
1012
1013       relinfo.reloc_shndx = i;
1014       relinfo.reloc_shdr = p;
1015       relinfo.data_shndx = index;
1016       relinfo.data_shdr = pshdrs + index * This::shdr_size;
1017       unsigned char* view = (*pviews)[index].view;
1018       Address address = (*pviews)[index].address;
1019       section_size_type view_size = (*pviews)[index].view_size;
1020
1021       Reloc_symbol_changes* reloc_map = NULL;
1022       if (this->uses_split_stack() && output_offset != invalid_address)
1023         {
1024           typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
1025           if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
1026             this->split_stack_adjust(symtab, pshdrs, sh_type, index,
1027                                      prelocs, reloc_count, view, view_size,
1028                                      &reloc_map);
1029         }
1030
1031       Relocatable_relocs* rr = NULL;
1032       if (parameters->options().emit_relocs()
1033           || parameters->options().relocatable())
1034         rr = this->relocatable_relocs(i);
1035       relinfo.rr = rr;
1036
1037       if (!parameters->options().relocatable())
1038         {
1039           target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
1040                                    output_offset == invalid_address,
1041                                    view, address, view_size, reloc_map);
1042           if (parameters->options().emit_relocs())
1043             target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
1044                                     os, output_offset,
1045                                     view, address, view_size,
1046                                     (*pviews)[i].view,
1047                                     (*pviews)[i].view_size);
1048           if (parameters->incremental())
1049             this->incremental_relocs_write(&relinfo, sh_type, prelocs,
1050                                            reloc_count, os, output_offset, of);
1051         }
1052       else
1053         target->relocate_relocs(&relinfo, sh_type, prelocs, reloc_count,
1054                                 os, output_offset,
1055                                 view, address, view_size,
1056                                 (*pviews)[i].view,
1057                                 (*pviews)[i].view_size);
1058     }
1059 }
1060
1061 // Return the output view for section SHNDX.
1062
1063 template<int size, bool big_endian>
1064 const unsigned char*
1065 Sized_relobj_file<size, big_endian>::do_get_output_view(
1066     unsigned int shndx,
1067     section_size_type* plen) const
1068 {
1069   gold_assert(this->output_views_ != NULL);
1070   gold_assert(shndx < this->output_views_->size());
1071   const View_size& v = (*this->output_views_)[shndx];
1072   *plen = v.view_size;
1073   return v.view;
1074 }
1075
1076 // Write the incremental relocs.
1077
1078 template<int size, bool big_endian>
1079 void
1080 Sized_relobj_file<size, big_endian>::incremental_relocs_write(
1081     const Relocate_info<size, big_endian>* relinfo,
1082     unsigned int sh_type,
1083     const unsigned char* prelocs,
1084     size_t reloc_count,
1085     Output_section* output_section,
1086     Address output_offset,
1087     Output_file* of)
1088 {
1089   if (sh_type == elfcpp::SHT_REL)
1090     this->incremental_relocs_write_reltype<elfcpp::SHT_REL>(
1091         relinfo,
1092         prelocs,
1093         reloc_count,
1094         output_section,
1095         output_offset,
1096         of);
1097   else
1098     {
1099       gold_assert(sh_type == elfcpp::SHT_RELA);
1100       this->incremental_relocs_write_reltype<elfcpp::SHT_RELA>(
1101           relinfo,
1102           prelocs,
1103           reloc_count,
1104           output_section,
1105           output_offset,
1106           of);
1107     }
1108 }
1109
1110 // Write the incremental relocs, templatized on the type of the
1111 // relocation section.
1112
1113 template<int size, bool big_endian>
1114 template<int sh_type>
1115 void
1116 Sized_relobj_file<size, big_endian>::incremental_relocs_write_reltype(
1117     const Relocate_info<size, big_endian>* relinfo,
1118     const unsigned char* prelocs,
1119     size_t reloc_count,
1120     Output_section* output_section,
1121     Address output_offset,
1122     Output_file* of)
1123 {
1124   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
1125   const unsigned int reloc_size =
1126       Reloc_types<sh_type, size, big_endian>::reloc_size;
1127   const unsigned int sizeof_addr = size / 8;
1128   const unsigned int incr_reloc_size =
1129       Incremental_relocs_reader<size, big_endian>::reloc_size;
1130
1131   unsigned int out_shndx = output_section->out_shndx();
1132
1133   // Get a view for the .gnu_incremental_relocs section.
1134
1135   Incremental_inputs* inputs = relinfo->layout->incremental_inputs();
1136   gold_assert(inputs != NULL);
1137   const off_t relocs_off = inputs->relocs_section()->offset();
1138   const off_t relocs_size = inputs->relocs_section()->data_size();
1139   unsigned char* const view = of->get_output_view(relocs_off, relocs_size);
1140
1141   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1142     {
1143       Reloc reloc(prelocs);
1144
1145       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1146       const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1147       const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1148
1149       if (r_sym < this->local_symbol_count_)
1150         continue;
1151
1152       // Get the new offset--the location in the output section where
1153       // this relocation should be applied.
1154
1155       Address offset = reloc.get_r_offset();
1156       if (output_offset != invalid_address)
1157         offset += output_offset;
1158       else
1159         {
1160           section_offset_type sot_offset =
1161               convert_types<section_offset_type, Address>(offset);
1162           section_offset_type new_sot_offset =
1163               output_section->output_offset(relinfo->object,
1164                                             relinfo->data_shndx,
1165                                             sot_offset);
1166           gold_assert(new_sot_offset != -1);
1167           offset += new_sot_offset;
1168         }
1169
1170       // Get the addend.
1171       typename elfcpp::Elf_types<size>::Elf_Swxword addend;
1172       if (sh_type == elfcpp::SHT_RELA)
1173         addend =
1174             Reloc_types<sh_type, size, big_endian>::get_reloc_addend(&reloc);
1175       else
1176         {
1177           // FIXME: Get the addend for SHT_REL.
1178           addend = 0;
1179         }
1180
1181       // Get the index of the output relocation.
1182
1183       unsigned int reloc_index =
1184           this->next_incremental_reloc_index(r_sym - this->local_symbol_count_);
1185
1186       // Write the relocation.
1187
1188       unsigned char* pov = view + reloc_index * incr_reloc_size;
1189       elfcpp::Swap<32, big_endian>::writeval(pov, r_type);
1190       elfcpp::Swap<32, big_endian>::writeval(pov + 4, out_shndx);
1191       elfcpp::Swap<size, big_endian>::writeval(pov + 8, offset);
1192       elfcpp::Swap<size, big_endian>::writeval(pov + 8 + sizeof_addr, addend);
1193       of->write_output_view(pov - view, incr_reloc_size, view);
1194     }
1195 }
1196
1197 // Create merge hash tables for the local symbols.  These are used to
1198 // speed up relocations.
1199
1200 template<int size, bool big_endian>
1201 void
1202 Sized_relobj_file<size, big_endian>::initialize_input_to_output_maps()
1203 {
1204   const unsigned int loccount = this->local_symbol_count_;
1205   for (unsigned int i = 1; i < loccount; ++i)
1206     {
1207       Symbol_value<size>& lv(this->local_values_[i]);
1208       lv.initialize_input_to_output_map(this);
1209     }
1210 }
1211
1212 // Free merge hash tables for the local symbols.
1213
1214 template<int size, bool big_endian>
1215 void
1216 Sized_relobj_file<size, big_endian>::free_input_to_output_maps()
1217 {
1218   const unsigned int loccount = this->local_symbol_count_;
1219   for (unsigned int i = 1; i < loccount; ++i)
1220     {
1221       Symbol_value<size>& lv(this->local_values_[i]);
1222       lv.free_input_to_output_map();
1223     }
1224 }
1225
1226 // If an object was compiled with -fsplit-stack, this is called to
1227 // check whether any relocations refer to functions defined in objects
1228 // which were not compiled with -fsplit-stack.  If they were, then we
1229 // need to apply some target-specific adjustments to request
1230 // additional stack space.
1231
1232 template<int size, bool big_endian>
1233 void
1234 Sized_relobj_file<size, big_endian>::split_stack_adjust(
1235     const Symbol_table* symtab,
1236     const unsigned char* pshdrs,
1237     unsigned int sh_type,
1238     unsigned int shndx,
1239     const unsigned char* prelocs,
1240     size_t reloc_count,
1241     unsigned char* view,
1242     section_size_type view_size,
1243     Reloc_symbol_changes** reloc_map)
1244 {
1245   if (sh_type == elfcpp::SHT_REL)
1246     this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
1247                                                       prelocs, reloc_count,
1248                                                       view, view_size,
1249                                                       reloc_map);
1250   else
1251     {
1252       gold_assert(sh_type == elfcpp::SHT_RELA);
1253       this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
1254                                                          prelocs, reloc_count,
1255                                                          view, view_size,
1256                                                          reloc_map);
1257     }
1258 }
1259
1260 // Adjust for -fsplit-stack, templatized on the type of the relocation
1261 // section.
1262
1263 template<int size, bool big_endian>
1264 template<int sh_type>
1265 void
1266 Sized_relobj_file<size, big_endian>::split_stack_adjust_reltype(
1267     const Symbol_table* symtab,
1268     const unsigned char* pshdrs,
1269     unsigned int shndx,
1270     const unsigned char* prelocs,
1271     size_t reloc_count,
1272     unsigned char* view,
1273     section_size_type view_size,
1274     Reloc_symbol_changes** reloc_map)
1275 {
1276   typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
1277   const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
1278
1279   size_t local_count = this->local_symbol_count();
1280
1281   std::vector<section_offset_type> non_split_refs;
1282
1283   const unsigned char* pr = prelocs;
1284   for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1285     {
1286       Reltype reloc(pr);
1287
1288       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1289       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1290       if (r_sym < local_count)
1291         continue;
1292
1293       const Symbol* gsym = this->global_symbol(r_sym);
1294       gold_assert(gsym != NULL);
1295       if (gsym->is_forwarder())
1296         gsym = symtab->resolve_forwards(gsym);
1297
1298       // See if this relocation refers to a function defined in an
1299       // object compiled without -fsplit-stack.  Note that we don't
1300       // care about the type of relocation--this means that in some
1301       // cases we will ask for a large stack unnecessarily, but this
1302       // is not fatal.  FIXME: Some targets have symbols which are
1303       // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
1304       if (!gsym->is_undefined()
1305           && gsym->source() == Symbol::FROM_OBJECT
1306           && !gsym->object()->uses_split_stack())
1307         {
1308           unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1309           if (parameters->target().is_call_to_non_split(gsym, r_type))
1310             {
1311               section_offset_type offset =
1312                 convert_to_section_size_type(reloc.get_r_offset());
1313               non_split_refs.push_back(offset);
1314             }
1315         }
1316     }
1317
1318   if (non_split_refs.empty())
1319     return;
1320
1321   // At this point, every entry in NON_SPLIT_REFS indicates a
1322   // relocation which refers to a function in an object compiled
1323   // without -fsplit-stack.  We now have to convert that list into a
1324   // set of offsets to functions.  First, we find all the functions.
1325
1326   Function_offsets function_offsets;
1327   this->find_functions(pshdrs, shndx, &function_offsets);
1328   if (function_offsets.empty())
1329     return;
1330
1331   // Now get a list of the function with references to non split-stack
1332   // code.
1333
1334   Function_offsets calls_non_split;
1335   for (std::vector<section_offset_type>::const_iterator p
1336          = non_split_refs.begin();
1337        p != non_split_refs.end();
1338        ++p)
1339     {
1340       Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
1341       if (low == function_offsets.end())
1342         --low;
1343       else if (low->first == *p)
1344         ;
1345       else if (low == function_offsets.begin())
1346         continue;
1347       else
1348         --low;
1349
1350       calls_non_split.insert(*low);
1351     }
1352   if (calls_non_split.empty())
1353     return;
1354
1355   // Now we have a set of functions to adjust.  The adjustments are
1356   // target specific.  Besides changing the output section view
1357   // however, it likes, the target may request a relocation change
1358   // from one global symbol name to another.
1359
1360   for (Function_offsets::const_iterator p = calls_non_split.begin();
1361        p != calls_non_split.end();
1362        ++p)
1363     {
1364       std::string from;
1365       std::string to;
1366       parameters->target().calls_non_split(this, shndx, p->first, p->second,
1367                                            view, view_size, &from, &to);
1368       if (!from.empty())
1369         {
1370           gold_assert(!to.empty());
1371           Symbol* tosym = NULL;
1372
1373           // Find relocations in the relevant function which are for
1374           // FROM.
1375           pr = prelocs;
1376           for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1377             {
1378               Reltype reloc(pr);
1379
1380               typename elfcpp::Elf_types<size>::Elf_WXword r_info =
1381                 reloc.get_r_info();
1382               unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1383               if (r_sym < local_count)
1384                 continue;
1385
1386               section_offset_type offset =
1387                 convert_to_section_size_type(reloc.get_r_offset());
1388               if (offset < p->first
1389                   || (offset
1390                       >= (p->first
1391                           + static_cast<section_offset_type>(p->second))))
1392                 continue;
1393
1394               const Symbol* gsym = this->global_symbol(r_sym);
1395               if (from == gsym->name())
1396                 {
1397                   if (tosym == NULL)
1398                     {
1399                       tosym = symtab->lookup(to.c_str());
1400                       if (tosym == NULL)
1401                         {
1402                           this->error(_("could not convert call "
1403                                         "to '%s' to '%s'"),
1404                                       from.c_str(), to.c_str());
1405                           break;
1406                         }
1407                     }
1408
1409                   if (*reloc_map == NULL)
1410                     *reloc_map = new Reloc_symbol_changes(reloc_count);
1411                   (*reloc_map)->set(i, tosym);
1412                 }
1413             }
1414         }
1415     }
1416 }
1417
1418 // Find all the function in this object defined in section SHNDX.
1419 // Store their offsets in the section in FUNCTION_OFFSETS.
1420
1421 template<int size, bool big_endian>
1422 void
1423 Sized_relobj_file<size, big_endian>::find_functions(
1424     const unsigned char* pshdrs,
1425     unsigned int shndx,
1426     Sized_relobj_file<size, big_endian>::Function_offsets* function_offsets)
1427 {
1428   // We need to read the symbols to find the functions.  If we wanted
1429   // to, we could cache reading the symbols across all sections in the
1430   // object.
1431   const unsigned int symtab_shndx = this->symtab_shndx_;
1432   typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
1433   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1434
1435   typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
1436     symtabshdr.get_sh_size();
1437   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1438                                               sh_size, true, true);
1439
1440   const int sym_size = This::sym_size;
1441   const unsigned int symcount = sh_size / sym_size;
1442   for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
1443     {
1444       typename elfcpp::Sym<size, big_endian> isym(psyms);
1445
1446       // FIXME: Some targets can have functions which do not have type
1447       // STT_FUNC, e.g., STT_ARM_TFUNC.
1448       if (isym.get_st_type() != elfcpp::STT_FUNC
1449           || isym.get_st_size() == 0)
1450         continue;
1451
1452       bool is_ordinary;
1453       Symbol_location loc;
1454       loc.shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1455                                          &is_ordinary);
1456       if (!is_ordinary)
1457         continue;
1458
1459       loc.object = this;
1460       loc.offset = isym.get_st_value();
1461       parameters->target().function_location(&loc);
1462
1463       if (loc.shndx != shndx)
1464         continue;
1465
1466       section_offset_type value =
1467         convert_to_section_size_type(loc.offset);
1468       section_size_type fnsize =
1469         convert_to_section_size_type(isym.get_st_size());
1470
1471       (*function_offsets)[value] = fnsize;
1472     }
1473 }
1474
1475 // Reverse the words in a section.  Used for .ctors sections mapped to
1476 // .init_array sections.  See ctors_sections_in_init_array in
1477 // layout.cc.
1478
1479 template<int size, bool big_endian>
1480 void
1481 Sized_relobj_file<size, big_endian>::reverse_words(unsigned char* view,
1482                                                    section_size_type view_size)
1483 {
1484   typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
1485   Valtype* vview = reinterpret_cast<Valtype*>(view);
1486   section_size_type vview_size = view_size / (size / 8);
1487   for (section_size_type i = 0; i < vview_size / 2; ++i)
1488     {
1489       Valtype tmp = vview[i];
1490       vview[i] = vview[vview_size - 1 - i];
1491       vview[vview_size - 1 - i] = tmp;
1492     }
1493 }
1494
1495 // Class Merged_symbol_value.
1496
1497 template<int size>
1498 void
1499 Merged_symbol_value<size>::initialize_input_to_output_map(
1500     const Relobj* object,
1501     unsigned int input_shndx)
1502 {
1503   object->initialize_input_to_output_map<size>(input_shndx,
1504                                                this->output_start_address_,
1505                                                &this->output_addresses_);
1506 }
1507
1508 // Get the output value corresponding to an input offset if we
1509 // couldn't find it in the hash table.
1510
1511 template<int size>
1512 typename elfcpp::Elf_types<size>::Elf_Addr
1513 Merged_symbol_value<size>::value_from_output_section(
1514     const Relobj* object,
1515     unsigned int input_shndx,
1516     typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1517 {
1518   section_offset_type output_offset;
1519   bool found = object->merge_output_offset(input_shndx, input_offset,
1520                                            &output_offset);
1521
1522   // If this assertion fails, it means that some relocation was
1523   // against a portion of an input merge section which we didn't map
1524   // to the output file and we didn't explicitly discard.  We should
1525   // always map all portions of input merge sections.
1526   gold_assert(found);
1527
1528   if (output_offset == -1)
1529     return 0;
1530   else
1531     return this->output_start_address_ + output_offset;
1532 }
1533
1534 // Track_relocs methods.
1535
1536 // Initialize the class to track the relocs.  This gets the object,
1537 // the reloc section index, and the type of the relocs.  This returns
1538 // false if something goes wrong.
1539
1540 template<int size, bool big_endian>
1541 bool
1542 Track_relocs<size, big_endian>::initialize(
1543     Object* object,
1544     unsigned int reloc_shndx,
1545     unsigned int reloc_type)
1546 {
1547   // If RELOC_SHNDX is -1U, it means there is more than one reloc
1548   // section for the .eh_frame section.  We can't handle that case.
1549   if (reloc_shndx == -1U)
1550     return false;
1551
1552   // If RELOC_SHNDX is 0, there is no reloc section.
1553   if (reloc_shndx == 0)
1554     return true;
1555
1556   // Get the contents of the reloc section.
1557   this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1558
1559   if (reloc_type == elfcpp::SHT_REL)
1560     this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1561   else if (reloc_type == elfcpp::SHT_RELA)
1562     this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1563   else
1564     gold_unreachable();
1565
1566   if (this->len_ % this->reloc_size_ != 0)
1567     {
1568       object->error(_("reloc section size %zu is not a multiple of "
1569                       "reloc size %d\n"),
1570                     static_cast<size_t>(this->len_),
1571                     this->reloc_size_);
1572       return false;
1573     }
1574
1575   return true;
1576 }
1577
1578 // Return the offset of the next reloc, or -1 if there isn't one.
1579
1580 template<int size, bool big_endian>
1581 off_t
1582 Track_relocs<size, big_endian>::next_offset() const
1583 {
1584   if (this->pos_ >= this->len_)
1585     return -1;
1586
1587   // Rel and Rela start out the same, so we can always use Rel to find
1588   // the r_offset value.
1589   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1590   return rel.get_r_offset();
1591 }
1592
1593 // Return the index of the symbol referenced by the next reloc, or -1U
1594 // if there aren't any more relocs.
1595
1596 template<int size, bool big_endian>
1597 unsigned int
1598 Track_relocs<size, big_endian>::next_symndx() const
1599 {
1600   if (this->pos_ >= this->len_)
1601     return -1U;
1602
1603   // Rel and Rela start out the same, so we can use Rel to find the
1604   // symbol index.
1605   elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1606   return elfcpp::elf_r_sym<size>(rel.get_r_info());
1607 }
1608
1609 // Return the addend of the next reloc, or 0 if there isn't one.
1610
1611 template<int size, bool big_endian>
1612 uint64_t
1613 Track_relocs<size, big_endian>::next_addend() const
1614 {
1615   if (this->pos_ >= this->len_)
1616     return 0;
1617   if (this->reloc_size_ == elfcpp::Elf_sizes<size>::rel_size)
1618     return 0;
1619   elfcpp::Rela<size, big_endian> rela(this->prelocs_ + this->pos_);
1620   return rela.get_r_addend();
1621 }
1622
1623 // Advance to the next reloc whose r_offset is greater than or equal
1624 // to OFFSET.  Return the number of relocs we skip.
1625
1626 template<int size, bool big_endian>
1627 int
1628 Track_relocs<size, big_endian>::advance(off_t offset)
1629 {
1630   int ret = 0;
1631   while (this->pos_ < this->len_)
1632     {
1633       // Rel and Rela start out the same, so we can always use Rel to
1634       // find the r_offset value.
1635       elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1636       if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1637         break;
1638       ++ret;
1639       this->pos_ += this->reloc_size_;
1640     }
1641   return ret;
1642 }
1643
1644 // Instantiate the templates we need.
1645
1646 #ifdef HAVE_TARGET_32_LITTLE
1647 template
1648 void
1649 Sized_relobj_file<32, false>::do_read_relocs(Read_relocs_data* rd);
1650 #endif
1651
1652 #ifdef HAVE_TARGET_32_BIG
1653 template
1654 void
1655 Sized_relobj_file<32, true>::do_read_relocs(Read_relocs_data* rd);
1656 #endif
1657
1658 #ifdef HAVE_TARGET_64_LITTLE
1659 template
1660 void
1661 Sized_relobj_file<64, false>::do_read_relocs(Read_relocs_data* rd);
1662 #endif
1663
1664 #ifdef HAVE_TARGET_64_BIG
1665 template
1666 void
1667 Sized_relobj_file<64, true>::do_read_relocs(Read_relocs_data* rd);
1668 #endif
1669
1670 #ifdef HAVE_TARGET_32_LITTLE
1671 template
1672 void
1673 Sized_relobj_file<32, false>::do_gc_process_relocs(Symbol_table* symtab,
1674                                                    Layout* layout,
1675                                                    Read_relocs_data* rd);
1676 #endif
1677
1678 #ifdef HAVE_TARGET_32_BIG
1679 template
1680 void
1681 Sized_relobj_file<32, true>::do_gc_process_relocs(Symbol_table* symtab,
1682                                                   Layout* layout,
1683                                                   Read_relocs_data* rd);
1684 #endif
1685
1686 #ifdef HAVE_TARGET_64_LITTLE
1687 template
1688 void
1689 Sized_relobj_file<64, false>::do_gc_process_relocs(Symbol_table* symtab,
1690                                                    Layout* layout,
1691                                                    Read_relocs_data* rd);
1692 #endif
1693
1694 #ifdef HAVE_TARGET_64_BIG
1695 template
1696 void
1697 Sized_relobj_file<64, true>::do_gc_process_relocs(Symbol_table* symtab,
1698                                                   Layout* layout,
1699                                                   Read_relocs_data* rd);
1700 #endif
1701
1702 #ifdef HAVE_TARGET_32_LITTLE
1703 template
1704 void
1705 Sized_relobj_file<32, false>::do_scan_relocs(Symbol_table* symtab,
1706                                              Layout* layout,
1707                                              Read_relocs_data* rd);
1708 #endif
1709
1710 #ifdef HAVE_TARGET_32_BIG
1711 template
1712 void
1713 Sized_relobj_file<32, true>::do_scan_relocs(Symbol_table* symtab,
1714                                             Layout* layout,
1715                                             Read_relocs_data* rd);
1716 #endif
1717
1718 #ifdef HAVE_TARGET_64_LITTLE
1719 template
1720 void
1721 Sized_relobj_file<64, false>::do_scan_relocs(Symbol_table* symtab,
1722                                              Layout* layout,
1723                                              Read_relocs_data* rd);
1724 #endif
1725
1726 #ifdef HAVE_TARGET_64_BIG
1727 template
1728 void
1729 Sized_relobj_file<64, true>::do_scan_relocs(Symbol_table* symtab,
1730                                             Layout* layout,
1731                                             Read_relocs_data* rd);
1732 #endif
1733
1734 #ifdef HAVE_TARGET_32_LITTLE
1735 template
1736 void
1737 Sized_relobj_file<32, false>::do_relocate(const Symbol_table* symtab,
1738                                           const Layout* layout,
1739                                           Output_file* of);
1740 #endif
1741
1742 #ifdef HAVE_TARGET_32_BIG
1743 template
1744 void
1745 Sized_relobj_file<32, true>::do_relocate(const Symbol_table* symtab,
1746                                          const Layout* layout,
1747                                          Output_file* of);
1748 #endif
1749
1750 #ifdef HAVE_TARGET_64_LITTLE
1751 template
1752 void
1753 Sized_relobj_file<64, false>::do_relocate(const Symbol_table* symtab,
1754                                           const Layout* layout,
1755                                           Output_file* of);
1756 #endif
1757
1758 #ifdef HAVE_TARGET_64_BIG
1759 template
1760 void
1761 Sized_relobj_file<64, true>::do_relocate(const Symbol_table* symtab,
1762                                          const Layout* layout,
1763                                          Output_file* of);
1764 #endif
1765
1766 #ifdef HAVE_TARGET_32_LITTLE
1767 template
1768 void
1769 Sized_relobj_file<32, false>::do_relocate_sections(
1770     const Symbol_table* symtab,
1771     const Layout* layout,
1772     const unsigned char* pshdrs,
1773     Output_file* of,
1774     Views* pviews);
1775
1776 template
1777 const unsigned char*
1778 Sized_relobj_file<32, false>::do_get_output_view(
1779     unsigned int shndx,
1780     section_size_type* plen) const;
1781 #endif
1782
1783 #ifdef HAVE_TARGET_32_BIG
1784 template
1785 void
1786 Sized_relobj_file<32, true>::do_relocate_sections(
1787     const Symbol_table* symtab,
1788     const Layout* layout,
1789     const unsigned char* pshdrs,
1790     Output_file* of,
1791     Views* pviews);
1792
1793 template
1794 const unsigned char*
1795 Sized_relobj_file<32, true>::do_get_output_view(
1796     unsigned int shndx,
1797     section_size_type* plen) const;
1798 #endif
1799
1800 #ifdef HAVE_TARGET_64_LITTLE
1801 template
1802 void
1803 Sized_relobj_file<64, false>::do_relocate_sections(
1804     const Symbol_table* symtab,
1805     const Layout* layout,
1806     const unsigned char* pshdrs,
1807     Output_file* of,
1808     Views* pviews);
1809
1810 template
1811 const unsigned char*
1812 Sized_relobj_file<64, false>::do_get_output_view(
1813     unsigned int shndx,
1814     section_size_type* plen) const;
1815 #endif
1816
1817 #ifdef HAVE_TARGET_64_BIG
1818 template
1819 void
1820 Sized_relobj_file<64, true>::do_relocate_sections(
1821     const Symbol_table* symtab,
1822     const Layout* layout,
1823     const unsigned char* pshdrs,
1824     Output_file* of,
1825     Views* pviews);
1826
1827 template
1828 const unsigned char*
1829 Sized_relobj_file<64, true>::do_get_output_view(
1830     unsigned int shndx,
1831     section_size_type* plen) const;
1832 #endif
1833
1834 #ifdef HAVE_TARGET_32_LITTLE
1835 template
1836 void
1837 Sized_relobj_file<32, false>::initialize_input_to_output_maps();
1838
1839 template
1840 void
1841 Sized_relobj_file<32, false>::free_input_to_output_maps();
1842 #endif
1843
1844 #ifdef HAVE_TARGET_32_BIG
1845 template
1846 void
1847 Sized_relobj_file<32, true>::initialize_input_to_output_maps();
1848
1849 template
1850 void
1851 Sized_relobj_file<32, true>::free_input_to_output_maps();
1852 #endif
1853
1854 #ifdef HAVE_TARGET_64_LITTLE
1855 template
1856 void
1857 Sized_relobj_file<64, false>::initialize_input_to_output_maps();
1858
1859 template
1860 void
1861 Sized_relobj_file<64, false>::free_input_to_output_maps();
1862 #endif
1863
1864 #ifdef HAVE_TARGET_64_BIG
1865 template
1866 void
1867 Sized_relobj_file<64, true>::initialize_input_to_output_maps();
1868
1869 template
1870 void
1871 Sized_relobj_file<64, true>::free_input_to_output_maps();
1872 #endif
1873
1874 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1875 template
1876 class Merged_symbol_value<32>;
1877 #endif
1878
1879 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1880 template
1881 class Merged_symbol_value<64>;
1882 #endif
1883
1884 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1885 template
1886 class Symbol_value<32>;
1887 #endif
1888
1889 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1890 template
1891 class Symbol_value<64>;
1892 #endif
1893
1894 #ifdef HAVE_TARGET_32_LITTLE
1895 template
1896 class Track_relocs<32, false>;
1897 #endif
1898
1899 #ifdef HAVE_TARGET_32_BIG
1900 template
1901 class Track_relocs<32, true>;
1902 #endif
1903
1904 #ifdef HAVE_TARGET_64_LITTLE
1905 template
1906 class Track_relocs<64, false>;
1907 #endif
1908
1909 #ifdef HAVE_TARGET_64_BIG
1910 template
1911 class Track_relocs<64, true>;
1912 #endif
1913
1914 } // End namespace gold.