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