1*fae548d3Szrj // target-reloc.h -- target specific relocation support -*- C++ -*-
2*fae548d3Szrj
3*fae548d3Szrj // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*fae548d3Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*fae548d3Szrj
6*fae548d3Szrj // This file is part of gold.
7*fae548d3Szrj
8*fae548d3Szrj // This program is free software; you can redistribute it and/or modify
9*fae548d3Szrj // it under the terms of the GNU General Public License as published by
10*fae548d3Szrj // the Free Software Foundation; either version 3 of the License, or
11*fae548d3Szrj // (at your option) any later version.
12*fae548d3Szrj
13*fae548d3Szrj // This program is distributed in the hope that it will be useful,
14*fae548d3Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*fae548d3Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*fae548d3Szrj // GNU General Public License for more details.
17*fae548d3Szrj
18*fae548d3Szrj // You should have received a copy of the GNU General Public License
19*fae548d3Szrj // along with this program; if not, write to the Free Software
20*fae548d3Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*fae548d3Szrj // MA 02110-1301, USA.
22*fae548d3Szrj
23*fae548d3Szrj #ifndef GOLD_TARGET_RELOC_H
24*fae548d3Szrj #define GOLD_TARGET_RELOC_H
25*fae548d3Szrj
26*fae548d3Szrj #include "elfcpp.h"
27*fae548d3Szrj #include "symtab.h"
28*fae548d3Szrj #include "object.h"
29*fae548d3Szrj #include "reloc.h"
30*fae548d3Szrj #include "reloc-types.h"
31*fae548d3Szrj
32*fae548d3Szrj namespace gold
33*fae548d3Szrj {
34*fae548d3Szrj
35*fae548d3Szrj // This function implements the generic part of reloc scanning. The
36*fae548d3Szrj // template parameter Scan must be a class type which provides two
37*fae548d3Szrj // functions: local() and global(). Those functions implement the
38*fae548d3Szrj // machine specific part of scanning. We do it this way to
39*fae548d3Szrj // avoid making a function call for each relocation, and to avoid
40*fae548d3Szrj // repeating the generic code for each target.
41*fae548d3Szrj
42*fae548d3Szrj template<int size, bool big_endian, typename Target_type,
43*fae548d3Szrj typename Scan, typename Classify_reloc>
44*fae548d3Szrj inline void
scan_relocs(Symbol_table * symtab,Layout * layout,Target_type * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_count,const unsigned char * plocal_syms)45*fae548d3Szrj scan_relocs(
46*fae548d3Szrj Symbol_table* symtab,
47*fae548d3Szrj Layout* layout,
48*fae548d3Szrj Target_type* target,
49*fae548d3Szrj Sized_relobj_file<size, big_endian>* object,
50*fae548d3Szrj unsigned int data_shndx,
51*fae548d3Szrj const unsigned char* prelocs,
52*fae548d3Szrj size_t reloc_count,
53*fae548d3Szrj Output_section* output_section,
54*fae548d3Szrj bool needs_special_offset_handling,
55*fae548d3Szrj size_t local_count,
56*fae548d3Szrj const unsigned char* plocal_syms)
57*fae548d3Szrj {
58*fae548d3Szrj typedef typename Classify_reloc::Reltype Reltype;
59*fae548d3Szrj const int reloc_size = Classify_reloc::reloc_size;
60*fae548d3Szrj const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
61*fae548d3Szrj Scan scan;
62*fae548d3Szrj
63*fae548d3Szrj for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
64*fae548d3Szrj {
65*fae548d3Szrj Reltype reloc(prelocs);
66*fae548d3Szrj
67*fae548d3Szrj if (needs_special_offset_handling
68*fae548d3Szrj && !output_section->is_input_address_mapped(object, data_shndx,
69*fae548d3Szrj reloc.get_r_offset()))
70*fae548d3Szrj continue;
71*fae548d3Szrj
72*fae548d3Szrj unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
73*fae548d3Szrj unsigned int r_type = Classify_reloc::get_r_type(&reloc);
74*fae548d3Szrj
75*fae548d3Szrj if (r_sym < local_count)
76*fae548d3Szrj {
77*fae548d3Szrj gold_assert(plocal_syms != NULL);
78*fae548d3Szrj typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
79*fae548d3Szrj + r_sym * sym_size);
80*fae548d3Szrj unsigned int shndx = lsym.get_st_shndx();
81*fae548d3Szrj bool is_ordinary;
82*fae548d3Szrj shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
83*fae548d3Szrj // If RELOC is a relocation against a local symbol in a
84*fae548d3Szrj // section we are discarding then we can ignore it. It will
85*fae548d3Szrj // eventually become a reloc against the value zero.
86*fae548d3Szrj //
87*fae548d3Szrj // FIXME: We should issue a warning if this is an
88*fae548d3Szrj // allocated section; is this the best place to do it?
89*fae548d3Szrj //
90*fae548d3Szrj // FIXME: The old GNU linker would in some cases look
91*fae548d3Szrj // for the linkonce section which caused this section to
92*fae548d3Szrj // be discarded, and, if the other section was the same
93*fae548d3Szrj // size, change the reloc to refer to the other section.
94*fae548d3Szrj // That seems risky and weird to me, and I don't know of
95*fae548d3Szrj // any case where it is actually required.
96*fae548d3Szrj bool is_discarded = (is_ordinary
97*fae548d3Szrj && shndx != elfcpp::SHN_UNDEF
98*fae548d3Szrj && !object->is_section_included(shndx)
99*fae548d3Szrj && !symtab->is_section_folded(object, shndx));
100*fae548d3Szrj scan.local(symtab, layout, target, object, data_shndx,
101*fae548d3Szrj output_section, reloc, r_type, lsym, is_discarded);
102*fae548d3Szrj }
103*fae548d3Szrj else
104*fae548d3Szrj {
105*fae548d3Szrj Symbol* gsym = object->global_symbol(r_sym);
106*fae548d3Szrj gold_assert(gsym != NULL);
107*fae548d3Szrj if (gsym->is_forwarder())
108*fae548d3Szrj gsym = symtab->resolve_forwards(gsym);
109*fae548d3Szrj
110*fae548d3Szrj scan.global(symtab, layout, target, object, data_shndx,
111*fae548d3Szrj output_section, reloc, r_type, gsym);
112*fae548d3Szrj }
113*fae548d3Szrj }
114*fae548d3Szrj }
115*fae548d3Szrj
116*fae548d3Szrj // Behavior for relocations to discarded comdat sections.
117*fae548d3Szrj
118*fae548d3Szrj enum Comdat_behavior
119*fae548d3Szrj {
120*fae548d3Szrj CB_UNDETERMINED, // Not yet determined -- need to look at section name.
121*fae548d3Szrj CB_PRETEND, // Attempt to map to the corresponding kept section.
122*fae548d3Szrj CB_IGNORE, // Ignore the relocation.
123*fae548d3Szrj CB_ERROR // Print an error.
124*fae548d3Szrj };
125*fae548d3Szrj
126*fae548d3Szrj class Default_comdat_behavior
127*fae548d3Szrj {
128*fae548d3Szrj public:
129*fae548d3Szrj // Decide what the linker should do for relocations that refer to
130*fae548d3Szrj // discarded comdat sections. This decision is based on the name of
131*fae548d3Szrj // the section being relocated.
132*fae548d3Szrj
133*fae548d3Szrj inline Comdat_behavior
get(const char * name)134*fae548d3Szrj get(const char* name)
135*fae548d3Szrj {
136*fae548d3Szrj if (Layout::is_debug_info_section(name))
137*fae548d3Szrj return CB_PRETEND;
138*fae548d3Szrj if (strcmp(name, ".eh_frame") == 0
139*fae548d3Szrj || strcmp(name, ".gcc_except_table") == 0)
140*fae548d3Szrj return CB_IGNORE;
141*fae548d3Szrj return CB_ERROR;
142*fae548d3Szrj }
143*fae548d3Szrj };
144*fae548d3Szrj
145*fae548d3Szrj // Give an error for a symbol with non-default visibility which is not
146*fae548d3Szrj // defined locally.
147*fae548d3Szrj
148*fae548d3Szrj inline void
visibility_error(const Symbol * sym)149*fae548d3Szrj visibility_error(const Symbol* sym)
150*fae548d3Szrj {
151*fae548d3Szrj const char* v;
152*fae548d3Szrj switch (sym->visibility())
153*fae548d3Szrj {
154*fae548d3Szrj case elfcpp::STV_INTERNAL:
155*fae548d3Szrj v = _("internal");
156*fae548d3Szrj break;
157*fae548d3Szrj case elfcpp::STV_HIDDEN:
158*fae548d3Szrj v = _("hidden");
159*fae548d3Szrj break;
160*fae548d3Szrj case elfcpp::STV_PROTECTED:
161*fae548d3Szrj v = _("protected");
162*fae548d3Szrj break;
163*fae548d3Szrj default:
164*fae548d3Szrj gold_unreachable();
165*fae548d3Szrj }
166*fae548d3Szrj gold_error(_("%s symbol '%s' is not defined locally"),
167*fae548d3Szrj v, sym->name());
168*fae548d3Szrj }
169*fae548d3Szrj
170*fae548d3Szrj // Return true if we are should issue an error saying that SYM is an
171*fae548d3Szrj // undefined symbol. This is called if there is a relocation against
172*fae548d3Szrj // SYM.
173*fae548d3Szrj
174*fae548d3Szrj inline bool
issue_undefined_symbol_error(const Symbol * sym)175*fae548d3Szrj issue_undefined_symbol_error(const Symbol* sym)
176*fae548d3Szrj {
177*fae548d3Szrj // We only report global symbols.
178*fae548d3Szrj if (sym == NULL)
179*fae548d3Szrj return false;
180*fae548d3Szrj
181*fae548d3Szrj // We only report undefined symbols.
182*fae548d3Szrj if (!sym->is_undefined() && !sym->is_placeholder())
183*fae548d3Szrj return false;
184*fae548d3Szrj
185*fae548d3Szrj // We don't report weak symbols.
186*fae548d3Szrj if (sym->is_weak_undefined())
187*fae548d3Szrj return false;
188*fae548d3Szrj
189*fae548d3Szrj // We don't report symbols defined in discarded sections,
190*fae548d3Szrj // unless they're placeholder symbols that should have been
191*fae548d3Szrj // provided by a plugin.
192*fae548d3Szrj if (sym->is_defined_in_discarded_section() && !sym->is_placeholder())
193*fae548d3Szrj return false;
194*fae548d3Szrj
195*fae548d3Szrj // If the target defines this symbol, don't report it here.
196*fae548d3Szrj if (parameters->target().is_defined_by_abi(sym))
197*fae548d3Szrj return false;
198*fae548d3Szrj
199*fae548d3Szrj // See if we've been told to ignore whether this symbol is
200*fae548d3Szrj // undefined.
201*fae548d3Szrj const char* const u = parameters->options().unresolved_symbols();
202*fae548d3Szrj if (u != NULL)
203*fae548d3Szrj {
204*fae548d3Szrj if (strcmp(u, "ignore-all") == 0)
205*fae548d3Szrj return false;
206*fae548d3Szrj if (strcmp(u, "report-all") == 0)
207*fae548d3Szrj return true;
208*fae548d3Szrj if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
209*fae548d3Szrj return false;
210*fae548d3Szrj if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
211*fae548d3Szrj return false;
212*fae548d3Szrj }
213*fae548d3Szrj
214*fae548d3Szrj // If the symbol is hidden, report it.
215*fae548d3Szrj if (sym->visibility() == elfcpp::STV_HIDDEN)
216*fae548d3Szrj return true;
217*fae548d3Szrj
218*fae548d3Szrj // When creating a shared library, only report unresolved symbols if
219*fae548d3Szrj // -z defs was used.
220*fae548d3Szrj if (parameters->options().shared() && !parameters->options().defs())
221*fae548d3Szrj return false;
222*fae548d3Szrj
223*fae548d3Szrj // Otherwise issue a warning.
224*fae548d3Szrj return true;
225*fae548d3Szrj }
226*fae548d3Szrj
227*fae548d3Szrj template<int size, bool big_endian>
228*fae548d3Szrj inline void
issue_discarded_error(const Relocate_info<size,big_endian> * relinfo,size_t shndx,section_offset_type offset,unsigned int r_sym,const Symbol * gsym)229*fae548d3Szrj issue_discarded_error(
230*fae548d3Szrj const Relocate_info<size, big_endian>* relinfo,
231*fae548d3Szrj size_t shndx,
232*fae548d3Szrj section_offset_type offset,
233*fae548d3Szrj unsigned int r_sym,
234*fae548d3Szrj const Symbol* gsym)
235*fae548d3Szrj {
236*fae548d3Szrj Sized_relobj_file<size, big_endian>* object = relinfo->object;
237*fae548d3Szrj
238*fae548d3Szrj if (gsym == NULL)
239*fae548d3Szrj {
240*fae548d3Szrj gold_error_at_location(
241*fae548d3Szrj relinfo, shndx, offset,
242*fae548d3Szrj _("relocation refers to local symbol \"%s\" [%u], "
243*fae548d3Szrj "which is defined in a discarded section"),
244*fae548d3Szrj object->get_symbol_name(r_sym), r_sym);
245*fae548d3Szrj }
246*fae548d3Szrj else
247*fae548d3Szrj {
248*fae548d3Szrj gold_error_at_location(
249*fae548d3Szrj relinfo, shndx, offset,
250*fae548d3Szrj _("relocation refers to global symbol \"%s\", "
251*fae548d3Szrj "which is defined in a discarded section"),
252*fae548d3Szrj gsym->demangled_name().c_str());
253*fae548d3Szrj }
254*fae548d3Szrj
255*fae548d3Szrj bool is_ordinary;
256*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value;
257*fae548d3Szrj unsigned int orig_shndx = object->symbol_section_and_value(r_sym, &value,
258*fae548d3Szrj &is_ordinary);
259*fae548d3Szrj if (orig_shndx != elfcpp::SHN_UNDEF)
260*fae548d3Szrj {
261*fae548d3Szrj unsigned int key_symndx;
262*fae548d3Szrj Relobj* kept_obj = object->find_kept_section_object(orig_shndx,
263*fae548d3Szrj &key_symndx);
264*fae548d3Szrj if (key_symndx != 0)
265*fae548d3Szrj gold_info(_(" section group signature: \"%s\""),
266*fae548d3Szrj object->get_symbol_name(key_symndx));
267*fae548d3Szrj if (kept_obj != NULL)
268*fae548d3Szrj gold_info(_(" prevailing definition is from %s"),
269*fae548d3Szrj kept_obj->name().c_str());
270*fae548d3Szrj }
271*fae548d3Szrj }
272*fae548d3Szrj
273*fae548d3Szrj // This function implements the generic part of relocation processing.
274*fae548d3Szrj // The template parameter Relocate must be a class type which provides
275*fae548d3Szrj // a single function, relocate(), which implements the machine
276*fae548d3Szrj // specific part of a relocation.
277*fae548d3Szrj
278*fae548d3Szrj // The template parameter Relocate_comdat_behavior is a class type
279*fae548d3Szrj // which provides a single function, get(), which determines what the
280*fae548d3Szrj // linker should do for relocations that refer to discarded comdat
281*fae548d3Szrj // sections.
282*fae548d3Szrj
283*fae548d3Szrj // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
284*fae548d3Szrj // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
285*fae548d3Szrj // RELOCATE implements operator() to do a relocation.
286*fae548d3Szrj
287*fae548d3Szrj // PRELOCS points to the relocation data. RELOC_COUNT is the number
288*fae548d3Szrj // of relocs. OUTPUT_SECTION is the output section.
289*fae548d3Szrj // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
290*fae548d3Szrj // mapped to output offsets.
291*fae548d3Szrj
292*fae548d3Szrj // VIEW is the section data, VIEW_ADDRESS is its memory address, and
293*fae548d3Szrj // VIEW_SIZE is the size. These refer to the input section, unless
294*fae548d3Szrj // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
295*fae548d3Szrj // the output section.
296*fae548d3Szrj
297*fae548d3Szrj // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
298*fae548d3Szrj // not NULL, it is a vector indexed by relocation index. If that
299*fae548d3Szrj // entry is not NULL, it points to a global symbol which used as the
300*fae548d3Szrj // symbol for the relocation, ignoring the symbol index in the
301*fae548d3Szrj // relocation.
302*fae548d3Szrj
303*fae548d3Szrj template<int size, bool big_endian, typename Target_type,
304*fae548d3Szrj typename Relocate,
305*fae548d3Szrj typename Relocate_comdat_behavior,
306*fae548d3Szrj typename Classify_reloc>
307*fae548d3Szrj inline void
relocate_section(const Relocate_info<size,big_endian> * relinfo,Target_type * target,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr view_address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)308*fae548d3Szrj relocate_section(
309*fae548d3Szrj const Relocate_info<size, big_endian>* relinfo,
310*fae548d3Szrj Target_type* target,
311*fae548d3Szrj const unsigned char* prelocs,
312*fae548d3Szrj size_t reloc_count,
313*fae548d3Szrj Output_section* output_section,
314*fae548d3Szrj bool needs_special_offset_handling,
315*fae548d3Szrj unsigned char* view,
316*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr view_address,
317*fae548d3Szrj section_size_type view_size,
318*fae548d3Szrj const Reloc_symbol_changes* reloc_symbol_changes)
319*fae548d3Szrj {
320*fae548d3Szrj typedef typename Classify_reloc::Reltype Reltype;
321*fae548d3Szrj const int reloc_size = Classify_reloc::reloc_size;
322*fae548d3Szrj Relocate relocate;
323*fae548d3Szrj Relocate_comdat_behavior relocate_comdat_behavior;
324*fae548d3Szrj
325*fae548d3Szrj Sized_relobj_file<size, big_endian>* object = relinfo->object;
326*fae548d3Szrj unsigned int local_count = object->local_symbol_count();
327*fae548d3Szrj
328*fae548d3Szrj Comdat_behavior comdat_behavior = CB_UNDETERMINED;
329*fae548d3Szrj
330*fae548d3Szrj for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
331*fae548d3Szrj {
332*fae548d3Szrj Reltype reloc(prelocs);
333*fae548d3Szrj
334*fae548d3Szrj section_offset_type offset =
335*fae548d3Szrj convert_to_section_size_type(reloc.get_r_offset());
336*fae548d3Szrj
337*fae548d3Szrj if (needs_special_offset_handling)
338*fae548d3Szrj {
339*fae548d3Szrj offset = output_section->output_offset(relinfo->object,
340*fae548d3Szrj relinfo->data_shndx,
341*fae548d3Szrj offset);
342*fae548d3Szrj if (offset == -1)
343*fae548d3Szrj continue;
344*fae548d3Szrj }
345*fae548d3Szrj
346*fae548d3Szrj unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
347*fae548d3Szrj
348*fae548d3Szrj const Sized_symbol<size>* sym;
349*fae548d3Szrj
350*fae548d3Szrj Symbol_value<size> symval;
351*fae548d3Szrj const Symbol_value<size> *psymval;
352*fae548d3Szrj bool is_defined_in_discarded_section;
353*fae548d3Szrj unsigned int shndx;
354*fae548d3Szrj const Symbol* gsym = NULL;
355*fae548d3Szrj if (r_sym < local_count
356*fae548d3Szrj && (reloc_symbol_changes == NULL
357*fae548d3Szrj || (*reloc_symbol_changes)[i] == NULL))
358*fae548d3Szrj {
359*fae548d3Szrj sym = NULL;
360*fae548d3Szrj psymval = object->local_symbol(r_sym);
361*fae548d3Szrj
362*fae548d3Szrj // If the local symbol belongs to a section we are discarding,
363*fae548d3Szrj // and that section is a debug section, try to find the
364*fae548d3Szrj // corresponding kept section and map this symbol to its
365*fae548d3Szrj // counterpart in the kept section. The symbol must not
366*fae548d3Szrj // correspond to a section we are folding.
367*fae548d3Szrj bool is_ordinary;
368*fae548d3Szrj shndx = psymval->input_shndx(&is_ordinary);
369*fae548d3Szrj is_defined_in_discarded_section =
370*fae548d3Szrj (is_ordinary
371*fae548d3Szrj && shndx != elfcpp::SHN_UNDEF
372*fae548d3Szrj && !object->is_section_included(shndx)
373*fae548d3Szrj && !relinfo->symtab->is_section_folded(object, shndx));
374*fae548d3Szrj }
375*fae548d3Szrj else
376*fae548d3Szrj {
377*fae548d3Szrj if (reloc_symbol_changes != NULL
378*fae548d3Szrj && (*reloc_symbol_changes)[i] != NULL)
379*fae548d3Szrj gsym = (*reloc_symbol_changes)[i];
380*fae548d3Szrj else
381*fae548d3Szrj {
382*fae548d3Szrj gsym = object->global_symbol(r_sym);
383*fae548d3Szrj gold_assert(gsym != NULL);
384*fae548d3Szrj if (gsym->is_forwarder())
385*fae548d3Szrj gsym = relinfo->symtab->resolve_forwards(gsym);
386*fae548d3Szrj }
387*fae548d3Szrj
388*fae548d3Szrj sym = static_cast<const Sized_symbol<size>*>(gsym);
389*fae548d3Szrj if (sym->has_symtab_index() && sym->symtab_index() != -1U)
390*fae548d3Szrj symval.set_output_symtab_index(sym->symtab_index());
391*fae548d3Szrj else
392*fae548d3Szrj symval.set_no_output_symtab_entry();
393*fae548d3Szrj symval.set_output_value(sym->value());
394*fae548d3Szrj if (gsym->type() == elfcpp::STT_TLS)
395*fae548d3Szrj symval.set_is_tls_symbol();
396*fae548d3Szrj else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
397*fae548d3Szrj symval.set_is_ifunc_symbol();
398*fae548d3Szrj psymval = &symval;
399*fae548d3Szrj
400*fae548d3Szrj is_defined_in_discarded_section =
401*fae548d3Szrj (gsym->is_defined_in_discarded_section()
402*fae548d3Szrj && gsym->is_undefined());
403*fae548d3Szrj shndx = 0;
404*fae548d3Szrj }
405*fae548d3Szrj
406*fae548d3Szrj Symbol_value<size> symval2;
407*fae548d3Szrj if (is_defined_in_discarded_section)
408*fae548d3Szrj {
409*fae548d3Szrj std::string name = object->section_name(relinfo->data_shndx);
410*fae548d3Szrj
411*fae548d3Szrj if (comdat_behavior == CB_UNDETERMINED)
412*fae548d3Szrj comdat_behavior = relocate_comdat_behavior.get(name.c_str());
413*fae548d3Szrj
414*fae548d3Szrj if (comdat_behavior == CB_PRETEND)
415*fae548d3Szrj {
416*fae548d3Szrj // FIXME: This case does not work for global symbols.
417*fae548d3Szrj // We have no place to store the original section index.
418*fae548d3Szrj // Fortunately this does not matter for comdat sections,
419*fae548d3Szrj // only for sections explicitly discarded by a linker
420*fae548d3Szrj // script.
421*fae548d3Szrj bool found;
422*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr value =
423*fae548d3Szrj object->map_to_kept_section(shndx, name, &found);
424*fae548d3Szrj if (found)
425*fae548d3Szrj symval2.set_output_value(value + psymval->input_value());
426*fae548d3Szrj else
427*fae548d3Szrj symval2.set_output_value(0);
428*fae548d3Szrj }
429*fae548d3Szrj else
430*fae548d3Szrj {
431*fae548d3Szrj if (comdat_behavior == CB_ERROR)
432*fae548d3Szrj issue_discarded_error(relinfo, i, offset, r_sym, gsym);
433*fae548d3Szrj symval2.set_output_value(0);
434*fae548d3Szrj }
435*fae548d3Szrj symval2.set_no_output_symtab_entry();
436*fae548d3Szrj psymval = &symval2;
437*fae548d3Szrj }
438*fae548d3Szrj
439*fae548d3Szrj // If OFFSET is out of range, still let the target decide to
440*fae548d3Szrj // ignore the relocation. Pass in NULL as the VIEW argument so
441*fae548d3Szrj // that it can return quickly without trashing an invalid memory
442*fae548d3Szrj // address.
443*fae548d3Szrj unsigned char *v = view + offset;
444*fae548d3Szrj if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
445*fae548d3Szrj v = NULL;
446*fae548d3Szrj
447*fae548d3Szrj if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target,
448*fae548d3Szrj output_section, i, prelocs, sym, psymval,
449*fae548d3Szrj v, view_address + offset, view_size))
450*fae548d3Szrj continue;
451*fae548d3Szrj
452*fae548d3Szrj if (v == NULL)
453*fae548d3Szrj {
454*fae548d3Szrj gold_error_at_location(relinfo, i, offset,
455*fae548d3Szrj _("reloc has bad offset %zu"),
456*fae548d3Szrj static_cast<size_t>(offset));
457*fae548d3Szrj continue;
458*fae548d3Szrj }
459*fae548d3Szrj
460*fae548d3Szrj if (issue_undefined_symbol_error(sym))
461*fae548d3Szrj gold_undefined_symbol_at_location(sym, relinfo, i, offset);
462*fae548d3Szrj else if (sym != NULL
463*fae548d3Szrj && sym->visibility() != elfcpp::STV_DEFAULT
464*fae548d3Szrj && (sym->is_strong_undefined() || sym->is_from_dynobj()))
465*fae548d3Szrj visibility_error(sym);
466*fae548d3Szrj
467*fae548d3Szrj if (sym != NULL && sym->has_warning())
468*fae548d3Szrj relinfo->symtab->issue_warning(sym, relinfo, i, offset);
469*fae548d3Szrj }
470*fae548d3Szrj }
471*fae548d3Szrj
472*fae548d3Szrj // Apply an incremental relocation.
473*fae548d3Szrj
474*fae548d3Szrj template<int size, bool big_endian, typename Target_type,
475*fae548d3Szrj typename Relocate>
476*fae548d3Szrj void
apply_relocation(const Relocate_info<size,big_endian> * relinfo,Target_type * target,typename elfcpp::Elf_types<size>::Elf_Addr r_offset,unsigned int r_type,typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,const Symbol * gsym,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr address,section_size_type view_size)477*fae548d3Szrj apply_relocation(const Relocate_info<size, big_endian>* relinfo,
478*fae548d3Szrj Target_type* target,
479*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
480*fae548d3Szrj unsigned int r_type,
481*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
482*fae548d3Szrj const Symbol* gsym,
483*fae548d3Szrj unsigned char* view,
484*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr address,
485*fae548d3Szrj section_size_type view_size)
486*fae548d3Szrj {
487*fae548d3Szrj // Construct the ELF relocation in a temporary buffer.
488*fae548d3Szrj const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
489*fae548d3Szrj unsigned char relbuf[reloc_size];
490*fae548d3Szrj elfcpp::Rela_write<size, big_endian> orel(relbuf);
491*fae548d3Szrj orel.put_r_offset(r_offset);
492*fae548d3Szrj orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
493*fae548d3Szrj orel.put_r_addend(r_addend);
494*fae548d3Szrj
495*fae548d3Szrj // Setup a Symbol_value for the global symbol.
496*fae548d3Szrj const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
497*fae548d3Szrj Symbol_value<size> symval;
498*fae548d3Szrj gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
499*fae548d3Szrj symval.set_output_symtab_index(sym->symtab_index());
500*fae548d3Szrj symval.set_output_value(sym->value());
501*fae548d3Szrj if (gsym->type() == elfcpp::STT_TLS)
502*fae548d3Szrj symval.set_is_tls_symbol();
503*fae548d3Szrj else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
504*fae548d3Szrj symval.set_is_ifunc_symbol();
505*fae548d3Szrj
506*fae548d3Szrj Relocate relocate;
507*fae548d3Szrj relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL,
508*fae548d3Szrj -1U, relbuf, sym, &symval,
509*fae548d3Szrj view + r_offset, address + r_offset, view_size);
510*fae548d3Szrj }
511*fae548d3Szrj
512*fae548d3Szrj // A class for inquiring about properties of a relocation,
513*fae548d3Szrj // used while scanning relocs during a relocatable link and
514*fae548d3Szrj // garbage collection. This class may be used as the default
515*fae548d3Szrj // for SHT_RELA targets, but SHT_REL targets must implement
516*fae548d3Szrj // a derived class that overrides get_size_for_reloc.
517*fae548d3Szrj // The MIPS-64 target also needs to override the methods
518*fae548d3Szrj // for accessing the r_sym and r_type fields of a relocation,
519*fae548d3Szrj // due to its non-standard use of the r_info field.
520*fae548d3Szrj
521*fae548d3Szrj template<int sh_type_, int size, bool big_endian>
522*fae548d3Szrj class Default_classify_reloc
523*fae548d3Szrj {
524*fae548d3Szrj public:
525*fae548d3Szrj typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc
526*fae548d3Szrj Reltype;
527*fae548d3Szrj typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write
528*fae548d3Szrj Reltype_write;
529*fae548d3Szrj static const int reloc_size =
530*fae548d3Szrj Reloc_types<sh_type_, size, big_endian>::reloc_size;
531*fae548d3Szrj static const int sh_type = sh_type_;
532*fae548d3Szrj
533*fae548d3Szrj // Return the symbol referred to by the relocation.
534*fae548d3Szrj static inline unsigned int
get_r_sym(const Reltype * reloc)535*fae548d3Szrj get_r_sym(const Reltype* reloc)
536*fae548d3Szrj { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
537*fae548d3Szrj
538*fae548d3Szrj // Return the type of the relocation.
539*fae548d3Szrj static inline unsigned int
get_r_type(const Reltype * reloc)540*fae548d3Szrj get_r_type(const Reltype* reloc)
541*fae548d3Szrj { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
542*fae548d3Szrj
543*fae548d3Szrj // Return the explicit addend of the relocation (return 0 for SHT_REL).
544*fae548d3Szrj static inline typename elfcpp::Elf_types<size>::Elf_Swxword
get_r_addend(const Reltype * reloc)545*fae548d3Szrj get_r_addend(const Reltype* reloc)
546*fae548d3Szrj { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); }
547*fae548d3Szrj
548*fae548d3Szrj // Write the r_info field to a new reloc, using the r_info field from
549*fae548d3Szrj // the original reloc, replacing the r_sym field with R_SYM.
550*fae548d3Szrj static inline void
put_r_info(Reltype_write * new_reloc,Reltype * reloc,unsigned int r_sym)551*fae548d3Szrj put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
552*fae548d3Szrj {
553*fae548d3Szrj unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info());
554*fae548d3Szrj new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
555*fae548d3Szrj }
556*fae548d3Szrj
557*fae548d3Szrj // Write the r_addend field to a new reloc.
558*fae548d3Szrj static inline void
put_r_addend(Reltype_write * to,typename elfcpp::Elf_types<size>::Elf_Swxword addend)559*fae548d3Szrj put_r_addend(Reltype_write* to,
560*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Swxword addend)
561*fae548d3Szrj { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); }
562*fae548d3Szrj
563*fae548d3Szrj // Return the size of the addend of the relocation (only used for SHT_REL).
564*fae548d3Szrj static unsigned int
get_size_for_reloc(unsigned int,Relobj *)565*fae548d3Szrj get_size_for_reloc(unsigned int, Relobj*)
566*fae548d3Szrj {
567*fae548d3Szrj gold_unreachable();
568*fae548d3Szrj return 0;
569*fae548d3Szrj }
570*fae548d3Szrj };
571*fae548d3Szrj
572*fae548d3Szrj // This class may be used as a typical class for the
573*fae548d3Szrj // Scan_relocatable_reloc parameter to scan_relocatable_relocs.
574*fae548d3Szrj // This class is intended to capture the most typical target behaviour,
575*fae548d3Szrj // while still permitting targets to define their own independent class
576*fae548d3Szrj // for Scan_relocatable_reloc.
577*fae548d3Szrj
578*fae548d3Szrj template<typename Classify_reloc>
579*fae548d3Szrj class Default_scan_relocatable_relocs
580*fae548d3Szrj {
581*fae548d3Szrj public:
582*fae548d3Szrj typedef typename Classify_reloc::Reltype Reltype;
583*fae548d3Szrj static const int reloc_size = Classify_reloc::reloc_size;
584*fae548d3Szrj static const int sh_type = Classify_reloc::sh_type;
585*fae548d3Szrj
586*fae548d3Szrj // Return the symbol referred to by the relocation.
587*fae548d3Szrj static inline unsigned int
get_r_sym(const Reltype * reloc)588*fae548d3Szrj get_r_sym(const Reltype* reloc)
589*fae548d3Szrj { return Classify_reloc::get_r_sym(reloc); }
590*fae548d3Szrj
591*fae548d3Szrj // Return the type of the relocation.
592*fae548d3Szrj static inline unsigned int
get_r_type(const Reltype * reloc)593*fae548d3Szrj get_r_type(const Reltype* reloc)
594*fae548d3Szrj { return Classify_reloc::get_r_type(reloc); }
595*fae548d3Szrj
596*fae548d3Szrj // Return the strategy to use for a local symbol which is not a
597*fae548d3Szrj // section symbol, given the relocation type.
598*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
local_non_section_strategy(unsigned int r_type,Relobj *,unsigned int r_sym)599*fae548d3Szrj local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
600*fae548d3Szrj {
601*fae548d3Szrj // We assume that relocation type 0 is NONE. Targets which are
602*fae548d3Szrj // different must override.
603*fae548d3Szrj if (r_type == 0 && r_sym == 0)
604*fae548d3Szrj return Relocatable_relocs::RELOC_DISCARD;
605*fae548d3Szrj return Relocatable_relocs::RELOC_COPY;
606*fae548d3Szrj }
607*fae548d3Szrj
608*fae548d3Szrj // Return the strategy to use for a local symbol which is a section
609*fae548d3Szrj // symbol, given the relocation type.
610*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int r_type,Relobj * object)611*fae548d3Szrj local_section_strategy(unsigned int r_type, Relobj* object)
612*fae548d3Szrj {
613*fae548d3Szrj if (sh_type == elfcpp::SHT_RELA)
614*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
615*fae548d3Szrj else
616*fae548d3Szrj {
617*fae548d3Szrj switch (Classify_reloc::get_size_for_reloc(r_type, object))
618*fae548d3Szrj {
619*fae548d3Szrj case 0:
620*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
621*fae548d3Szrj case 1:
622*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
623*fae548d3Szrj case 2:
624*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
625*fae548d3Szrj case 4:
626*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
627*fae548d3Szrj case 8:
628*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
629*fae548d3Szrj default:
630*fae548d3Szrj gold_unreachable();
631*fae548d3Szrj }
632*fae548d3Szrj }
633*fae548d3Szrj }
634*fae548d3Szrj
635*fae548d3Szrj // Return the strategy to use for a global symbol, given the
636*fae548d3Szrj // relocation type, the object, and the symbol index.
637*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
global_strategy(unsigned int,Relobj *,unsigned int)638*fae548d3Szrj global_strategy(unsigned int, Relobj*, unsigned int)
639*fae548d3Szrj { return Relocatable_relocs::RELOC_COPY; }
640*fae548d3Szrj };
641*fae548d3Szrj
642*fae548d3Szrj // This is a strategy class used with scan_relocatable_relocs
643*fae548d3Szrj // and --emit-relocs.
644*fae548d3Szrj
645*fae548d3Szrj template<typename Classify_reloc>
646*fae548d3Szrj class Default_emit_relocs_strategy
647*fae548d3Szrj {
648*fae548d3Szrj public:
649*fae548d3Szrj typedef typename Classify_reloc::Reltype Reltype;
650*fae548d3Szrj static const int reloc_size = Classify_reloc::reloc_size;
651*fae548d3Szrj static const int sh_type = Classify_reloc::sh_type;
652*fae548d3Szrj
653*fae548d3Szrj // Return the symbol referred to by the relocation.
654*fae548d3Szrj static inline unsigned int
get_r_sym(const Reltype * reloc)655*fae548d3Szrj get_r_sym(const Reltype* reloc)
656*fae548d3Szrj { return Classify_reloc::get_r_sym(reloc); }
657*fae548d3Szrj
658*fae548d3Szrj // Return the type of the relocation.
659*fae548d3Szrj static inline unsigned int
get_r_type(const Reltype * reloc)660*fae548d3Szrj get_r_type(const Reltype* reloc)
661*fae548d3Szrj { return Classify_reloc::get_r_type(reloc); }
662*fae548d3Szrj
663*fae548d3Szrj // A local non-section symbol.
664*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
local_non_section_strategy(unsigned int,Relobj *,unsigned int)665*fae548d3Szrj local_non_section_strategy(unsigned int, Relobj*, unsigned int)
666*fae548d3Szrj { return Relocatable_relocs::RELOC_COPY; }
667*fae548d3Szrj
668*fae548d3Szrj // A local section symbol.
669*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int,Relobj *)670*fae548d3Szrj local_section_strategy(unsigned int, Relobj*)
671*fae548d3Szrj {
672*fae548d3Szrj if (sh_type == elfcpp::SHT_RELA)
673*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
674*fae548d3Szrj else
675*fae548d3Szrj {
676*fae548d3Szrj // The addend is stored in the section contents. Since this
677*fae548d3Szrj // is not a relocatable link, we are going to apply the
678*fae548d3Szrj // relocation contents to the section as usual. This means
679*fae548d3Szrj // that we have no way to record the original addend. If the
680*fae548d3Szrj // original addend is not zero, there is basically no way for
681*fae548d3Szrj // the user to handle this correctly. Caveat emptor.
682*fae548d3Szrj return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
683*fae548d3Szrj }
684*fae548d3Szrj }
685*fae548d3Szrj
686*fae548d3Szrj // A global symbol.
687*fae548d3Szrj inline Relocatable_relocs::Reloc_strategy
global_strategy(unsigned int,Relobj *,unsigned int)688*fae548d3Szrj global_strategy(unsigned int, Relobj*, unsigned int)
689*fae548d3Szrj { return Relocatable_relocs::RELOC_COPY; }
690*fae548d3Szrj };
691*fae548d3Szrj
692*fae548d3Szrj // Scan relocs during a relocatable link. This is a default
693*fae548d3Szrj // definition which should work for most targets.
694*fae548d3Szrj // Scan_relocatable_reloc must name a class type which provides three
695*fae548d3Szrj // functions which return a Relocatable_relocs::Reloc_strategy code:
696*fae548d3Szrj // global_strategy, local_non_section_strategy, and
697*fae548d3Szrj // local_section_strategy. Most targets should be able to use
698*fae548d3Szrj // Default_scan_relocatable_relocs as this class.
699*fae548d3Szrj
700*fae548d3Szrj template<int size, bool big_endian, typename Scan_relocatable_reloc>
701*fae548d3Szrj void
scan_relocatable_relocs(Symbol_table *,Layout *,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_syms,Relocatable_relocs * rr)702*fae548d3Szrj scan_relocatable_relocs(
703*fae548d3Szrj Symbol_table*,
704*fae548d3Szrj Layout*,
705*fae548d3Szrj Sized_relobj_file<size, big_endian>* object,
706*fae548d3Szrj unsigned int data_shndx,
707*fae548d3Szrj const unsigned char* prelocs,
708*fae548d3Szrj size_t reloc_count,
709*fae548d3Szrj Output_section* output_section,
710*fae548d3Szrj bool needs_special_offset_handling,
711*fae548d3Szrj size_t local_symbol_count,
712*fae548d3Szrj const unsigned char* plocal_syms,
713*fae548d3Szrj Relocatable_relocs* rr)
714*fae548d3Szrj {
715*fae548d3Szrj typedef typename Scan_relocatable_reloc::Reltype Reltype;
716*fae548d3Szrj const int reloc_size = Scan_relocatable_reloc::reloc_size;
717*fae548d3Szrj const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
718*fae548d3Szrj Scan_relocatable_reloc scan;
719*fae548d3Szrj
720*fae548d3Szrj for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
721*fae548d3Szrj {
722*fae548d3Szrj Reltype reloc(prelocs);
723*fae548d3Szrj
724*fae548d3Szrj Relocatable_relocs::Reloc_strategy strategy;
725*fae548d3Szrj
726*fae548d3Szrj if (needs_special_offset_handling
727*fae548d3Szrj && !output_section->is_input_address_mapped(object, data_shndx,
728*fae548d3Szrj reloc.get_r_offset()))
729*fae548d3Szrj strategy = Relocatable_relocs::RELOC_DISCARD;
730*fae548d3Szrj else
731*fae548d3Szrj {
732*fae548d3Szrj const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc);
733*fae548d3Szrj const unsigned int r_type =
734*fae548d3Szrj Scan_relocatable_reloc::get_r_type(&reloc);
735*fae548d3Szrj
736*fae548d3Szrj if (r_sym >= local_symbol_count)
737*fae548d3Szrj strategy = scan.global_strategy(r_type, object, r_sym);
738*fae548d3Szrj else
739*fae548d3Szrj {
740*fae548d3Szrj gold_assert(plocal_syms != NULL);
741*fae548d3Szrj typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
742*fae548d3Szrj + r_sym * sym_size);
743*fae548d3Szrj unsigned int shndx = lsym.get_st_shndx();
744*fae548d3Szrj bool is_ordinary;
745*fae548d3Szrj shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
746*fae548d3Szrj if (is_ordinary
747*fae548d3Szrj && shndx != elfcpp::SHN_UNDEF
748*fae548d3Szrj && !object->is_section_included(shndx))
749*fae548d3Szrj {
750*fae548d3Szrj // RELOC is a relocation against a local symbol
751*fae548d3Szrj // defined in a section we are discarding. Discard
752*fae548d3Szrj // the reloc. FIXME: Should we issue a warning?
753*fae548d3Szrj strategy = Relocatable_relocs::RELOC_DISCARD;
754*fae548d3Szrj }
755*fae548d3Szrj else if (lsym.get_st_type() != elfcpp::STT_SECTION)
756*fae548d3Szrj strategy = scan.local_non_section_strategy(r_type, object,
757*fae548d3Szrj r_sym);
758*fae548d3Szrj else
759*fae548d3Szrj {
760*fae548d3Szrj strategy = scan.local_section_strategy(r_type, object);
761*fae548d3Szrj if (strategy != Relocatable_relocs::RELOC_DISCARD)
762*fae548d3Szrj object->output_section(shndx)->set_needs_symtab_index();
763*fae548d3Szrj }
764*fae548d3Szrj
765*fae548d3Szrj if (strategy == Relocatable_relocs::RELOC_COPY)
766*fae548d3Szrj object->set_must_have_output_symtab_entry(r_sym);
767*fae548d3Szrj }
768*fae548d3Szrj }
769*fae548d3Szrj
770*fae548d3Szrj rr->set_next_reloc_strategy(strategy);
771*fae548d3Szrj }
772*fae548d3Szrj }
773*fae548d3Szrj
774*fae548d3Szrj // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
775*fae548d3Szrj // This is a default definition which should work for most targets.
776*fae548d3Szrj
777*fae548d3Szrj template<int size, bool big_endian, typename Classify_reloc>
778*fae548d3Szrj void
relocate_relocs(const Relocate_info<size,big_endian> * relinfo,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)779*fae548d3Szrj relocate_relocs(
780*fae548d3Szrj const Relocate_info<size, big_endian>* relinfo,
781*fae548d3Szrj const unsigned char* prelocs,
782*fae548d3Szrj size_t reloc_count,
783*fae548d3Szrj Output_section* output_section,
784*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
785*fae548d3Szrj unsigned char* view,
786*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Addr view_address,
787*fae548d3Szrj section_size_type view_size,
788*fae548d3Szrj unsigned char* reloc_view,
789*fae548d3Szrj section_size_type reloc_view_size)
790*fae548d3Szrj {
791*fae548d3Szrj typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
792*fae548d3Szrj typedef typename Classify_reloc::Reltype Reltype;
793*fae548d3Szrj typedef typename Classify_reloc::Reltype_write Reltype_write;
794*fae548d3Szrj const int reloc_size = Classify_reloc::reloc_size;
795*fae548d3Szrj const Address invalid_address = static_cast<Address>(0) - 1;
796*fae548d3Szrj
797*fae548d3Szrj Sized_relobj_file<size, big_endian>* const object = relinfo->object;
798*fae548d3Szrj const unsigned int local_count = object->local_symbol_count();
799*fae548d3Szrj
800*fae548d3Szrj unsigned char* pwrite = reloc_view;
801*fae548d3Szrj
802*fae548d3Szrj const bool relocatable = parameters->options().relocatable();
803*fae548d3Szrj
804*fae548d3Szrj for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
805*fae548d3Szrj {
806*fae548d3Szrj Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
807*fae548d3Szrj if (strategy == Relocatable_relocs::RELOC_DISCARD)
808*fae548d3Szrj continue;
809*fae548d3Szrj
810*fae548d3Szrj if (strategy == Relocatable_relocs::RELOC_SPECIAL)
811*fae548d3Szrj {
812*fae548d3Szrj // Target wants to handle this relocation.
813*fae548d3Szrj Sized_target<size, big_endian>* target =
814*fae548d3Szrj parameters->sized_target<size, big_endian>();
815*fae548d3Szrj target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type,
816*fae548d3Szrj prelocs, i, output_section,
817*fae548d3Szrj offset_in_output_section,
818*fae548d3Szrj view, view_address,
819*fae548d3Szrj view_size, pwrite);
820*fae548d3Szrj pwrite += reloc_size;
821*fae548d3Szrj continue;
822*fae548d3Szrj }
823*fae548d3Szrj Reltype reloc(prelocs);
824*fae548d3Szrj Reltype_write reloc_write(pwrite);
825*fae548d3Szrj
826*fae548d3Szrj const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
827*fae548d3Szrj
828*fae548d3Szrj // Get the new symbol index.
829*fae548d3Szrj
830*fae548d3Szrj Output_section* os = NULL;
831*fae548d3Szrj unsigned int new_symndx;
832*fae548d3Szrj if (r_sym < local_count)
833*fae548d3Szrj {
834*fae548d3Szrj switch (strategy)
835*fae548d3Szrj {
836*fae548d3Szrj case Relocatable_relocs::RELOC_COPY:
837*fae548d3Szrj if (r_sym == 0)
838*fae548d3Szrj new_symndx = 0;
839*fae548d3Szrj else
840*fae548d3Szrj {
841*fae548d3Szrj new_symndx = object->symtab_index(r_sym);
842*fae548d3Szrj gold_assert(new_symndx != -1U);
843*fae548d3Szrj }
844*fae548d3Szrj break;
845*fae548d3Szrj
846*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
847*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
848*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
849*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
850*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
851*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
852*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
853*fae548d3Szrj {
854*fae548d3Szrj // We are adjusting a section symbol. We need to find
855*fae548d3Szrj // the symbol table index of the section symbol for
856*fae548d3Szrj // the output section corresponding to input section
857*fae548d3Szrj // in which this symbol is defined.
858*fae548d3Szrj gold_assert(r_sym < local_count);
859*fae548d3Szrj bool is_ordinary;
860*fae548d3Szrj unsigned int shndx =
861*fae548d3Szrj object->local_symbol_input_shndx(r_sym, &is_ordinary);
862*fae548d3Szrj gold_assert(is_ordinary);
863*fae548d3Szrj os = object->output_section(shndx);
864*fae548d3Szrj gold_assert(os != NULL);
865*fae548d3Szrj gold_assert(os->needs_symtab_index());
866*fae548d3Szrj new_symndx = os->symtab_index();
867*fae548d3Szrj }
868*fae548d3Szrj break;
869*fae548d3Szrj
870*fae548d3Szrj default:
871*fae548d3Szrj gold_unreachable();
872*fae548d3Szrj }
873*fae548d3Szrj }
874*fae548d3Szrj else
875*fae548d3Szrj {
876*fae548d3Szrj const Symbol* gsym = object->global_symbol(r_sym);
877*fae548d3Szrj gold_assert(gsym != NULL);
878*fae548d3Szrj if (gsym->is_forwarder())
879*fae548d3Szrj gsym = relinfo->symtab->resolve_forwards(gsym);
880*fae548d3Szrj
881*fae548d3Szrj gold_assert(gsym->has_symtab_index());
882*fae548d3Szrj new_symndx = gsym->symtab_index();
883*fae548d3Szrj }
884*fae548d3Szrj
885*fae548d3Szrj // Get the new offset--the location in the output section where
886*fae548d3Szrj // this relocation should be applied.
887*fae548d3Szrj
888*fae548d3Szrj Address offset = reloc.get_r_offset();
889*fae548d3Szrj Address new_offset;
890*fae548d3Szrj if (offset_in_output_section != invalid_address)
891*fae548d3Szrj new_offset = offset + offset_in_output_section;
892*fae548d3Szrj else
893*fae548d3Szrj {
894*fae548d3Szrj section_offset_type sot_offset =
895*fae548d3Szrj convert_types<section_offset_type, Address>(offset);
896*fae548d3Szrj section_offset_type new_sot_offset =
897*fae548d3Szrj output_section->output_offset(object, relinfo->data_shndx,
898*fae548d3Szrj sot_offset);
899*fae548d3Szrj gold_assert(new_sot_offset != -1);
900*fae548d3Szrj new_offset = new_sot_offset;
901*fae548d3Szrj }
902*fae548d3Szrj
903*fae548d3Szrj // In an object file, r_offset is an offset within the section.
904*fae548d3Szrj // In an executable or dynamic object, generated by
905*fae548d3Szrj // --emit-relocs, r_offset is an absolute address.
906*fae548d3Szrj if (!relocatable)
907*fae548d3Szrj {
908*fae548d3Szrj new_offset += view_address;
909*fae548d3Szrj if (offset_in_output_section != invalid_address)
910*fae548d3Szrj new_offset -= offset_in_output_section;
911*fae548d3Szrj }
912*fae548d3Szrj
913*fae548d3Szrj reloc_write.put_r_offset(new_offset);
914*fae548d3Szrj Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx);
915*fae548d3Szrj
916*fae548d3Szrj // Handle the reloc addend based on the strategy.
917*fae548d3Szrj
918*fae548d3Szrj if (strategy == Relocatable_relocs::RELOC_COPY)
919*fae548d3Szrj {
920*fae548d3Szrj if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
921*fae548d3Szrj Classify_reloc::put_r_addend(&reloc_write,
922*fae548d3Szrj Classify_reloc::get_r_addend(&reloc));
923*fae548d3Szrj }
924*fae548d3Szrj else
925*fae548d3Szrj {
926*fae548d3Szrj // The relocation uses a section symbol in the input file.
927*fae548d3Szrj // We are adjusting it to use a section symbol in the output
928*fae548d3Szrj // file. The input section symbol refers to some address in
929*fae548d3Szrj // the input section. We need the relocation in the output
930*fae548d3Szrj // file to refer to that same address. This adjustment to
931*fae548d3Szrj // the addend is the same calculation we use for a simple
932*fae548d3Szrj // absolute relocation for the input section symbol.
933*fae548d3Szrj
934*fae548d3Szrj const Symbol_value<size>* psymval = object->local_symbol(r_sym);
935*fae548d3Szrj
936*fae548d3Szrj unsigned char* padd = view + offset;
937*fae548d3Szrj switch (strategy)
938*fae548d3Szrj {
939*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
940*fae548d3Szrj {
941*fae548d3Szrj typename elfcpp::Elf_types<size>::Elf_Swxword addend
942*fae548d3Szrj = Classify_reloc::get_r_addend(&reloc);
943*fae548d3Szrj addend = psymval->value(object, addend);
944*fae548d3Szrj // In a relocatable link, the symbol value is relative to
945*fae548d3Szrj // the start of the output section. For a non-relocatable
946*fae548d3Szrj // link, we need to adjust the addend.
947*fae548d3Szrj if (!relocatable)
948*fae548d3Szrj {
949*fae548d3Szrj gold_assert(os != NULL);
950*fae548d3Szrj addend -= os->address();
951*fae548d3Szrj }
952*fae548d3Szrj Classify_reloc::put_r_addend(&reloc_write, addend);
953*fae548d3Szrj }
954*fae548d3Szrj break;
955*fae548d3Szrj
956*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
957*fae548d3Szrj break;
958*fae548d3Szrj
959*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
960*fae548d3Szrj Relocate_functions<size, big_endian>::rel8(padd, object,
961*fae548d3Szrj psymval);
962*fae548d3Szrj break;
963*fae548d3Szrj
964*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
965*fae548d3Szrj Relocate_functions<size, big_endian>::rel16(padd, object,
966*fae548d3Szrj psymval);
967*fae548d3Szrj break;
968*fae548d3Szrj
969*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
970*fae548d3Szrj Relocate_functions<size, big_endian>::rel32(padd, object,
971*fae548d3Szrj psymval);
972*fae548d3Szrj break;
973*fae548d3Szrj
974*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
975*fae548d3Szrj Relocate_functions<size, big_endian>::rel64(padd, object,
976*fae548d3Szrj psymval);
977*fae548d3Szrj break;
978*fae548d3Szrj
979*fae548d3Szrj case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
980*fae548d3Szrj Relocate_functions<size, big_endian>::rel32_unaligned(padd,
981*fae548d3Szrj object,
982*fae548d3Szrj psymval);
983*fae548d3Szrj break;
984*fae548d3Szrj
985*fae548d3Szrj default:
986*fae548d3Szrj gold_unreachable();
987*fae548d3Szrj }
988*fae548d3Szrj }
989*fae548d3Szrj
990*fae548d3Szrj pwrite += reloc_size;
991*fae548d3Szrj }
992*fae548d3Szrj
993*fae548d3Szrj gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
994*fae548d3Szrj == reloc_view_size);
995*fae548d3Szrj }
996*fae548d3Szrj
997*fae548d3Szrj } // End namespace gold.
998*fae548d3Szrj
999*fae548d3Szrj #endif // !defined(GOLD_TARGET_RELOC_H)
1000