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