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