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