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