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