include/ChangeLog
[deliverable/binutils-gdb.git] / gold / target-reloc.h
CommitLineData
61ba1cf9
ILT
1// target-reloc.h -- target specific relocation support -*- C++ -*-
2
191f1a2d 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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);
84 if (is_ordinary
ead1e424 85 && shndx != elfcpp::SHN_UNDEF
ce97fa81
ST
86 && !object->is_section_included(shndx)
87 && !symtab->is_section_folded(object, shndx))
92e059d8
ILT
88 {
89 // RELOC is a relocation against a local symbol in a
90 // section we are discarding. We can ignore this
91 // relocation. It will eventually become a reloc
92 // against the value zero.
93 //
94 // FIXME: We should issue a warning if this is an
95 // allocated section; is this the best place to do it?
96 //
97 // FIXME: The old GNU linker would in some cases look
98 // for the linkonce section which caused this section to
99 // be discarded, and, if the other section was the same
100 // size, change the reloc to refer to the other section.
101 // That seems risky and weird to me, and I don't know of
102 // any case where it is actually required.
103
104 continue;
105 }
ad0f2072 106 scan.local(symtab, layout, target, object, data_shndx,
07f397ab 107 output_section, reloc, r_type, lsym);
92e059d8
ILT
108 }
109 else
110 {
730cdc88 111 Symbol* gsym = object->global_symbol(r_sym);
a3ad94ed 112 gold_assert(gsym != NULL);
92e059d8
ILT
113 if (gsym->is_forwarder())
114 gsym = symtab->resolve_forwards(gsym);
115
ad0f2072 116 scan.global(symtab, layout, target, object, data_shndx,
07f397ab 117 output_section, reloc, r_type, gsym);
92e059d8
ILT
118 }
119 }
120}
121
e94cf127
CC
122// Behavior for relocations to discarded comdat sections.
123
124enum Comdat_behavior
125{
126 CB_UNDETERMINED, // Not yet determined -- need to look at section name.
127 CB_PRETEND, // Attempt to map to the corresponding kept section.
128 CB_IGNORE, // Ignore the relocation.
129 CB_WARNING // Print a warning.
130};
131
132// Decide what the linker should do for relocations that refer to discarded
133// comdat sections. This decision is based on the name of the section being
134// relocated.
135
136inline Comdat_behavior
137get_comdat_behavior(const char* name)
138{
139 if (Layout::is_debug_info_section(name))
140 return CB_PRETEND;
141 if (strcmp(name, ".eh_frame") == 0
142 || strcmp(name, ".gcc_except_table") == 0)
143 return CB_IGNORE;
144 return CB_WARNING;
145}
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;
206 if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn())
207 return false;
208 if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg())
209 return false;
210 }
211
212 // When creating a shared library, only report unresolved symbols if
213 // -z defs was used.
214 if (parameters->options().shared() && !parameters->options().defs())
215 return false;
216
217 // Otherwise issue a warning.
218 return true;
191f1a2d
ILT
219}
220
92e059d8 221// This function implements the generic part of relocation processing.
6a74a719
ILT
222// The template parameter Relocate must be a class type which provides
223// a single function, relocate(), which implements the machine
224// specific part of a relocation.
61ba1cf9
ILT
225
226// SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
92e059d8
ILT
227// the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
228// RELOCATE implements operator() to do a relocation.
61ba1cf9 229
92e059d8 230// PRELOCS points to the relocation data. RELOC_COUNT is the number
730cdc88
ILT
231// of relocs. OUTPUT_SECTION is the output section.
232// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
233// mapped to output offsets.
234
235// VIEW is the section data, VIEW_ADDRESS is its memory address, and
236// VIEW_SIZE is the size. These refer to the input section, unless
237// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
238// the output section.
61ba1cf9 239
364c7fa5
ILT
240// RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
241// not NULL, it is a vector indexed by relocation index. If that
242// entry is not NULL, it points to a global symbol which used as the
243// symbol for the relocation, ignoring the symbol index in the
244// relocation.
245
ead1e424
ILT
246template<int size, bool big_endian, typename Target_type, int sh_type,
247 typename Relocate>
61ba1cf9
ILT
248inline void
249relocate_section(
92e059d8 250 const Relocate_info<size, big_endian>* relinfo,
ead1e424 251 Target_type* target,
61ba1cf9
ILT
252 const unsigned char* prelocs,
253 size_t reloc_count,
730cdc88
ILT
254 Output_section* output_section,
255 bool needs_special_offset_handling,
61ba1cf9
ILT
256 unsigned char* view,
257 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
364c7fa5
ILT
258 section_size_type view_size,
259 const Reloc_symbol_changes* reloc_symbol_changes)
61ba1cf9
ILT
260{
261 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
262 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
263 Relocate relocate;
264
6fa2a40b 265 Sized_relobj_file<size, big_endian>* object = relinfo->object;
730cdc88 266 unsigned int local_count = object->local_symbol_count();
92e059d8 267
e94cf127
CC
268 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
269
61ba1cf9
ILT
270 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
271 {
272 Reltype reloc(prelocs);
273
fe8718a4
ILT
274 section_offset_type offset =
275 convert_to_section_size_type(reloc.get_r_offset());
61ba1cf9 276
730cdc88
ILT
277 if (needs_special_offset_handling)
278 {
279 offset = output_section->output_offset(relinfo->object,
280 relinfo->data_shndx,
281 offset);
282 if (offset == -1)
283 continue;
284 }
285
61ba1cf9
ILT
286 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
287 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
288 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
289
c06b7b0b 290 const Sized_symbol<size>* sym;
61ba1cf9 291
b8e6aad9
ILT
292 Symbol_value<size> symval;
293 const Symbol_value<size> *psymval;
880cd20d
ILT
294 bool is_defined_in_discarded_section;
295 unsigned int shndx;
364c7fa5
ILT
296 if (r_sym < local_count
297 && (reloc_symbol_changes == NULL
298 || (*reloc_symbol_changes)[i] == NULL))
61ba1cf9
ILT
299 {
300 sym = NULL;
730cdc88 301 psymval = object->local_symbol(r_sym);
e94cf127
CC
302
303 // If the local symbol belongs to a section we are discarding,
304 // and that section is a debug section, try to find the
305 // corresponding kept section and map this symbol to its
ef15dade
ST
306 // counterpart in the kept section. The symbol must not
307 // correspond to a section we are folding.
e94cf127 308 bool is_ordinary;
880cd20d
ILT
309 shndx = psymval->input_shndx(&is_ordinary);
310 is_defined_in_discarded_section =
311 (is_ordinary
312 && shndx != elfcpp::SHN_UNDEF
313 && !object->is_section_included(shndx)
314 && !relinfo->symtab->is_section_folded(object, shndx));
61ba1cf9
ILT
315 }
316 else
317 {
364c7fa5
ILT
318 const Symbol* gsym;
319 if (reloc_symbol_changes != NULL
320 && (*reloc_symbol_changes)[i] != NULL)
321 gsym = (*reloc_symbol_changes)[i];
322 else
323 {
324 gsym = object->global_symbol(r_sym);
325 gold_assert(gsym != NULL);
326 if (gsym->is_forwarder())
327 gsym = relinfo->symtab->resolve_forwards(gsym);
328 }
61ba1cf9 329
c06b7b0b 330 sym = static_cast<const Sized_symbol<size>*>(gsym);
d3bbad62 331 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
b8e6aad9
ILT
332 symval.set_output_symtab_index(sym->symtab_index());
333 else
334 symval.set_no_output_symtab_entry();
335 symval.set_output_value(sym->value());
7223e9ca
ILT
336 if (gsym->type() == elfcpp::STT_TLS)
337 symval.set_is_tls_symbol();
338 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
339 symval.set_is_ifunc_symbol();
b8e6aad9 340 psymval = &symval;
880cd20d
ILT
341
342 is_defined_in_discarded_section =
343 (gsym->is_defined_in_discarded_section()
344 && gsym->is_undefined());
345 shndx = 0;
346 }
347
348 Symbol_value<size> symval2;
349 if (is_defined_in_discarded_section)
350 {
351 if (comdat_behavior == CB_UNDETERMINED)
352 {
353 std::string name = object->section_name(relinfo->data_shndx);
354 comdat_behavior = get_comdat_behavior(name.c_str());
355 }
356 if (comdat_behavior == CB_PRETEND)
357 {
358 // FIXME: This case does not work for global symbols.
359 // We have no place to store the original section index.
360 // Fortunately this does not matter for comdat sections,
361 // only for sections explicitly discarded by a linker
362 // script.
363 bool found;
364 typename elfcpp::Elf_types<size>::Elf_Addr value =
365 object->map_to_kept_section(shndx, &found);
366 if (found)
367 symval2.set_output_value(value + psymval->input_value());
368 else
369 symval2.set_output_value(0);
370 }
371 else
372 {
373 if (comdat_behavior == CB_WARNING)
374 gold_warning_at_location(relinfo, i, offset,
375 _("relocation refers to discarded "
376 "section"));
377 symval2.set_output_value(0);
378 }
379 symval2.set_no_output_symtab_entry();
380 psymval = &symval2;
ead1e424 381 }
61ba1cf9 382
031cdbed
ILT
383 if (!relocate.relocate(relinfo, target, output_section, i, reloc,
384 r_type, sym, psymval, view + offset,
385 view_address + offset, view_size))
ead1e424
ILT
386 continue;
387
fe8718a4 388 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
ead1e424 389 {
75f2446e
ILT
390 gold_error_at_location(relinfo, i, offset,
391 _("reloc has bad offset %zu"),
392 static_cast<size_t>(offset));
393 continue;
61ba1cf9
ILT
394 }
395
191f1a2d 396 if (issue_undefined_symbol_error(sym))
f073bbf7 397 gold_undefined_symbol_at_location(sym, relinfo, i, offset);
837504c4
ILT
398 else if (sym != NULL
399 && sym->visibility() != elfcpp::STV_DEFAULT
400 && (sym->is_undefined() || sym->is_from_dynobj()))
401 visibility_error(sym);
f6ce93d6
ILT
402
403 if (sym != NULL && sym->has_warning())
75f2446e 404 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
61ba1cf9
ILT
405 }
406}
407
94a3fc8b
CC
408// Apply an incremental relocation.
409
410template<int size, bool big_endian, typename Target_type,
411 typename Relocate>
412void
413apply_relocation(const Relocate_info<size, big_endian>* relinfo,
414 Target_type* target,
415 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
416 unsigned int r_type,
417 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
418 const Symbol* gsym,
419 unsigned char* view,
420 typename elfcpp::Elf_types<size>::Elf_Addr address,
421 section_size_type view_size)
422{
423 // Construct the ELF relocation in a temporary buffer.
424 const int reloc_size = elfcpp::Elf_sizes<64>::rela_size;
425 unsigned char relbuf[reloc_size];
426 elfcpp::Rela<64, false> rel(relbuf);
427 elfcpp::Rela_write<64, false> orel(relbuf);
428 orel.put_r_offset(r_offset);
429 orel.put_r_info(elfcpp::elf_r_info<64>(0, r_type));
430 orel.put_r_addend(r_addend);
431
432 // Setup a Symbol_value for the global symbol.
433 const Sized_symbol<64>* sym = static_cast<const Sized_symbol<64>*>(gsym);
434 Symbol_value<64> symval;
435 gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
436 symval.set_output_symtab_index(sym->symtab_index());
437 symval.set_output_value(sym->value());
438 if (gsym->type() == elfcpp::STT_TLS)
439 symval.set_is_tls_symbol();
440 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
441 symval.set_is_ifunc_symbol();
442
443 Relocate relocate;
444 relocate.relocate(relinfo, target, NULL, -1U, rel, r_type, sym, &symval,
445 view + r_offset, address + r_offset, view_size);
446}
447
6a74a719
ILT
448// This class may be used as a typical class for the
449// Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
450// template parameter Classify_reloc must be a class type which
451// provides a function get_size_for_reloc which returns the number of
452// bytes to which a reloc applies. This class is intended to capture
453// the most typical target behaviour, while still permitting targets
454// to define their own independent class for Scan_relocatable_reloc.
455
456template<int sh_type, typename Classify_reloc>
457class Default_scan_relocatable_relocs
458{
459 public:
460 // Return the strategy to use for a local symbol which is not a
461 // section symbol, given the relocation type.
462 inline Relocatable_relocs::Reloc_strategy
68943102 463 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
c2508178
ILT
464 {
465 // We assume that relocation type 0 is NONE. Targets which are
466 // different must override.
68943102 467 if (r_type == 0 && r_sym == 0)
c2508178
ILT
468 return Relocatable_relocs::RELOC_DISCARD;
469 return Relocatable_relocs::RELOC_COPY;
470 }
6a74a719
ILT
471
472 // Return the strategy to use for a local symbol which is a section
473 // symbol, given the relocation type.
474 inline Relocatable_relocs::Reloc_strategy
475 local_section_strategy(unsigned int r_type, Relobj* object)
476 {
477 if (sh_type == elfcpp::SHT_RELA)
478 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
479 else
480 {
481 Classify_reloc classify;
482 switch (classify.get_size_for_reloc(r_type, object))
483 {
484 case 0:
7019cd25 485 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
6a74a719
ILT
486 case 1:
487 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
488 case 2:
489 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
490 case 4:
491 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
492 case 8:
493 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
494 default:
495 gold_unreachable();
496 }
497 }
498 }
499
500 // Return the strategy to use for a global symbol, given the
501 // relocation type, the object, and the symbol index.
502 inline Relocatable_relocs::Reloc_strategy
68943102
ILT
503 global_strategy(unsigned int, Relobj*, unsigned int)
504 { return Relocatable_relocs::RELOC_COPY; }
6a74a719
ILT
505};
506
507// Scan relocs during a relocatable link. This is a default
508// definition which should work for most targets.
509// Scan_relocatable_reloc must name a class type which provides three
510// functions which return a Relocatable_relocs::Reloc_strategy code:
511// global_strategy, local_non_section_strategy, and
512// local_section_strategy. Most targets should be able to use
513// Default_scan_relocatable_relocs as this class.
514
7019cd25 515template<int size, bool big_endian, int sh_type,
6a74a719
ILT
516 typename Scan_relocatable_reloc>
517void
518scan_relocatable_relocs(
6a74a719
ILT
519 Symbol_table*,
520 Layout*,
6fa2a40b 521 Sized_relobj_file<size, big_endian>* object,
6a74a719
ILT
522 unsigned int data_shndx,
523 const unsigned char* prelocs,
524 size_t reloc_count,
525 Output_section* output_section,
526 bool needs_special_offset_handling,
527 size_t local_symbol_count,
528 const unsigned char* plocal_syms,
529 Relocatable_relocs* rr)
530{
531 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
532 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
533 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
534 Scan_relocatable_reloc scan;
535
536 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
537 {
538 Reltype reloc(prelocs);
539
540 Relocatable_relocs::Reloc_strategy strategy;
541
542 if (needs_special_offset_handling
543 && !output_section->is_input_address_mapped(object, data_shndx,
544 reloc.get_r_offset()))
545 strategy = Relocatable_relocs::RELOC_DISCARD;
546 else
547 {
548 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
549 reloc.get_r_info();
550 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
551 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
552
553 if (r_sym >= local_symbol_count)
554 strategy = scan.global_strategy(r_type, object, r_sym);
555 else
556 {
557 gold_assert(plocal_syms != NULL);
558 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
559 + r_sym * sym_size);
d491d34e
ILT
560 unsigned int shndx = lsym.get_st_shndx();
561 bool is_ordinary;
562 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
563 if (is_ordinary
6a74a719 564 && shndx != elfcpp::SHN_UNDEF
d491d34e 565 && !object->is_section_included(shndx))
6a74a719
ILT
566 {
567 // RELOC is a relocation against a local symbol
568 // defined in a section we are discarding. Discard
569 // the reloc. FIXME: Should we issue a warning?
570 strategy = Relocatable_relocs::RELOC_DISCARD;
571 }
572 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
68943102
ILT
573 strategy = scan.local_non_section_strategy(r_type, object,
574 r_sym);
6a74a719
ILT
575 else
576 {
577 strategy = scan.local_section_strategy(r_type, object);
578 if (strategy != Relocatable_relocs::RELOC_DISCARD)
ef9beddf 579 object->output_section(shndx)->set_needs_symtab_index();
6a74a719 580 }
d3bbad62
ILT
581
582 if (strategy == Relocatable_relocs::RELOC_COPY)
583 object->set_must_have_output_symtab_entry(r_sym);
6a74a719
ILT
584 }
585 }
586
587 rr->set_next_reloc_strategy(strategy);
588 }
589}
590
591// Relocate relocs during a relocatable link. This is a default
592// definition which should work for most targets.
593
7019cd25 594template<int size, bool big_endian, int sh_type>
6a74a719
ILT
595void
596relocate_for_relocatable(
597 const Relocate_info<size, big_endian>* relinfo,
598 const unsigned char* prelocs,
599 size_t reloc_count,
600 Output_section* output_section,
ef9beddf 601 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
6a74a719
ILT
602 const Relocatable_relocs* rr,
603 unsigned char* view,
6be6f3bd 604 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5c388529 605 section_size_type view_size,
6a74a719
ILT
606 unsigned char* reloc_view,
607 section_size_type reloc_view_size)
608{
ef9beddf 609 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6a74a719
ILT
610 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
611 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
612 Reltype_write;
613 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
eff45813 614 const Address invalid_address = static_cast<Address>(0) - 1;
6a74a719 615
6fa2a40b 616 Sized_relobj_file<size, big_endian>* const object = relinfo->object;
6a74a719
ILT
617 const unsigned int local_count = object->local_symbol_count();
618
619 unsigned char* pwrite = reloc_view;
620
621 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
622 {
623 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
624 if (strategy == Relocatable_relocs::RELOC_DISCARD)
625 continue;
626
5c388529
DK
627 if (strategy == Relocatable_relocs::RELOC_SPECIAL)
628 {
629 // Target wants to handle this relocation.
630 Sized_target<size, big_endian>* target =
631 parameters->sized_target<size, big_endian>();
632 target->relocate_special_relocatable(relinfo, sh_type, prelocs,
633 i, output_section,
634 offset_in_output_section,
635 view, view_address,
636 view_size, pwrite);
637 pwrite += reloc_size;
638 continue;
639 }
6a74a719
ILT
640 Reltype reloc(prelocs);
641 Reltype_write reloc_write(pwrite);
642
643 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
644 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
645 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
646
647 // Get the new symbol index.
648
649 unsigned int new_symndx;
650 if (r_sym < local_count)
651 {
652 switch (strategy)
653 {
654 case Relocatable_relocs::RELOC_COPY:
818bf354
ILT
655 if (r_sym == 0)
656 new_symndx = 0;
657 else
658 {
659 new_symndx = object->symtab_index(r_sym);
660 gold_assert(new_symndx != -1U);
661 }
6a74a719
ILT
662 break;
663
664 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7019cd25 665 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
6a74a719
ILT
666 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
667 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
668 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
669 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
670 {
671 // We are adjusting a section symbol. We need to find
672 // the symbol table index of the section symbol for
673 // the output section corresponding to input section
674 // in which this symbol is defined.
675 gold_assert(r_sym < local_count);
d491d34e
ILT
676 bool is_ordinary;
677 unsigned int shndx =
678 object->local_symbol_input_shndx(r_sym, &is_ordinary);
679 gold_assert(is_ordinary);
ef9beddf 680 Output_section* os = object->output_section(shndx);
6a74a719
ILT
681 gold_assert(os != NULL);
682 gold_assert(os->needs_symtab_index());
683 new_symndx = os->symtab_index();
684 }
685 break;
686
687 default:
688 gold_unreachable();
689 }
690 }
691 else
692 {
693 const Symbol* gsym = object->global_symbol(r_sym);
694 gold_assert(gsym != NULL);
695 if (gsym->is_forwarder())
696 gsym = relinfo->symtab->resolve_forwards(gsym);
697
698 gold_assert(gsym->has_symtab_index());
699 new_symndx = gsym->symtab_index();
700 }
701
702 // Get the new offset--the location in the output section where
703 // this relocation should be applied.
704
ef9beddf
ILT
705 Address offset = reloc.get_r_offset();
706 Address new_offset;
eff45813 707 if (offset_in_output_section != invalid_address)
6a74a719
ILT
708 new_offset = offset + offset_in_output_section;
709 else
710 {
ef9beddf
ILT
711 section_offset_type sot_offset =
712 convert_types<section_offset_type, Address>(offset);
713 section_offset_type new_sot_offset =
714 output_section->output_offset(object, relinfo->data_shndx,
715 sot_offset);
716 gold_assert(new_sot_offset != -1);
717 new_offset = new_sot_offset;
6a74a719
ILT
718 }
719
6be6f3bd
ILT
720 // In an object file, r_offset is an offset within the section.
721 // In an executable or dynamic object, generated by
722 // --emit-relocs, r_offset is an absolute address.
723 if (!parameters->options().relocatable())
e09ad04a
ILT
724 {
725 new_offset += view_address;
eff45813 726 if (offset_in_output_section != invalid_address)
e09ad04a
ILT
727 new_offset -= offset_in_output_section;
728 }
6be6f3bd 729
6a74a719
ILT
730 reloc_write.put_r_offset(new_offset);
731 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
732
733 // Handle the reloc addend based on the strategy.
734
735 if (strategy == Relocatable_relocs::RELOC_COPY)
736 {
737 if (sh_type == elfcpp::SHT_RELA)
738 Reloc_types<sh_type, size, big_endian>::
739 copy_reloc_addend(&reloc_write,
740 &reloc);
741 }
742 else
743 {
744 // The relocation uses a section symbol in the input file.
745 // We are adjusting it to use a section symbol in the output
746 // file. The input section symbol refers to some address in
747 // the input section. We need the relocation in the output
748 // file to refer to that same address. This adjustment to
749 // the addend is the same calculation we use for a simple
750 // absolute relocation for the input section symbol.
751
752 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
753
754 unsigned char* padd = view + offset;
755 switch (strategy)
756 {
757 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
758 {
759 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
760 addend = Reloc_types<sh_type, size, big_endian>::
761 get_reloc_addend(&reloc);
762 addend = psymval->value(object, addend);
763 Reloc_types<sh_type, size, big_endian>::
764 set_reloc_addend(&reloc_write, addend);
765 }
766 break;
767
7019cd25
ILT
768 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
769 break;
770
6a74a719
ILT
771 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
772 Relocate_functions<size, big_endian>::rel8(padd, object,
773 psymval);
774 break;
775
776 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
777 Relocate_functions<size, big_endian>::rel16(padd, object,
778 psymval);
779 break;
780
781 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
782 Relocate_functions<size, big_endian>::rel32(padd, object,
783 psymval);
784 break;
785
786 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
787 Relocate_functions<size, big_endian>::rel64(padd, object,
788 psymval);
789 break;
790
791 default:
792 gold_unreachable();
793 }
794 }
795
796 pwrite += reloc_size;
797 }
798
799 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
800 == reloc_view_size);
801}
802
61ba1cf9
ILT
803} // End namespace gold.
804
805#endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.250556 seconds and 4 git commands to generate.