* rs6000-tdep.c: Update for unwinder changes.
[deliverable/binutils-gdb.git] / gold / target-reloc.h
CommitLineData
61ba1cf9
ILT
1// target-reloc.h -- target specific relocation support -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 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"
6a74a719 28#include "reloc.h"
c06b7b0b 29#include "reloc-types.h"
61ba1cf9
ILT
30
31namespace gold
32{
33
6a74a719
ILT
34// This function implements the generic part of reloc scanning. The
35// template parameter Scan must be a class type which provides two
36// functions: local() and global(). Those functions implement the
37// machine specific part of scanning. We do it this way to
38// avoidmaking a function call for each relocation, and to avoid
39// repeating the generic code for each target.
92e059d8 40
ead1e424
ILT
41template<int size, bool big_endian, typename Target_type, int sh_type,
42 typename Scan>
92e059d8
ILT
43inline void
44scan_relocs(
45 const General_options& options,
46 Symbol_table* symtab,
ead1e424
ILT
47 Layout* layout,
48 Target_type* target,
f6ce93d6 49 Sized_relobj<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
d491d34e 86 && !object->is_section_included(shndx))
92e059d8
ILT
87 {
88 // RELOC is a relocation against a local symbol in a
89 // section we are discarding. We can ignore this
90 // relocation. It will eventually become a reloc
91 // against the value zero.
92 //
93 // FIXME: We should issue a warning if this is an
94 // allocated section; is this the best place to do it?
95 //
96 // FIXME: The old GNU linker would in some cases look
97 // for the linkonce section which caused this section to
98 // be discarded, and, if the other section was the same
99 // size, change the reloc to refer to the other section.
100 // That seems risky and weird to me, and I don't know of
101 // any case where it is actually required.
102
103 continue;
104 }
105
a3ad94ed 106 scan.local(options, 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
a3ad94ed 116 scan.global(options, symtab, layout, target, object, data_shndx,
07f397ab 117 output_section, reloc, r_type, gsym);
92e059d8
ILT
118 }
119 }
120}
121
122// This function implements the generic part of relocation processing.
6a74a719
ILT
123// The template parameter Relocate must be a class type which provides
124// a single function, relocate(), which implements the machine
125// specific part of a relocation.
61ba1cf9
ILT
126
127// SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
92e059d8
ILT
128// the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
129// RELOCATE implements operator() to do a relocation.
61ba1cf9 130
92e059d8 131// PRELOCS points to the relocation data. RELOC_COUNT is the number
730cdc88
ILT
132// of relocs. OUTPUT_SECTION is the output section.
133// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
134// mapped to output offsets.
135
136// VIEW is the section data, VIEW_ADDRESS is its memory address, and
137// VIEW_SIZE is the size. These refer to the input section, unless
138// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
139// the output section.
61ba1cf9 140
ead1e424
ILT
141template<int size, bool big_endian, typename Target_type, int sh_type,
142 typename Relocate>
61ba1cf9
ILT
143inline void
144relocate_section(
92e059d8 145 const Relocate_info<size, big_endian>* relinfo,
ead1e424 146 Target_type* target,
61ba1cf9
ILT
147 const unsigned char* prelocs,
148 size_t reloc_count,
730cdc88
ILT
149 Output_section* output_section,
150 bool needs_special_offset_handling,
61ba1cf9
ILT
151 unsigned char* view,
152 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
fe8718a4 153 section_size_type view_size)
61ba1cf9
ILT
154{
155 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
156 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
157 Relocate relocate;
158
730cdc88
ILT
159 Sized_relobj<size, big_endian>* object = relinfo->object;
160 unsigned int local_count = object->local_symbol_count();
92e059d8 161
61ba1cf9
ILT
162 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
163 {
164 Reltype reloc(prelocs);
165
fe8718a4
ILT
166 section_offset_type offset =
167 convert_to_section_size_type(reloc.get_r_offset());
61ba1cf9 168
730cdc88
ILT
169 if (needs_special_offset_handling)
170 {
171 offset = output_section->output_offset(relinfo->object,
172 relinfo->data_shndx,
173 offset);
174 if (offset == -1)
175 continue;
176 }
177
61ba1cf9
ILT
178 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
179 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
180 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
181
c06b7b0b 182 const Sized_symbol<size>* sym;
61ba1cf9 183
b8e6aad9
ILT
184 Symbol_value<size> symval;
185 const Symbol_value<size> *psymval;
61ba1cf9
ILT
186 if (r_sym < local_count)
187 {
188 sym = NULL;
730cdc88 189 psymval = object->local_symbol(r_sym);
61ba1cf9
ILT
190 }
191 else
192 {
730cdc88 193 const Symbol* gsym = object->global_symbol(r_sym);
a3ad94ed 194 gold_assert(gsym != NULL);
61ba1cf9 195 if (gsym->is_forwarder())
92e059d8 196 gsym = relinfo->symtab->resolve_forwards(gsym);
61ba1cf9 197
c06b7b0b 198 sym = static_cast<const Sized_symbol<size>*>(gsym);
b8e6aad9
ILT
199 if (sym->has_symtab_index())
200 symval.set_output_symtab_index(sym->symtab_index());
201 else
202 symval.set_no_output_symtab_entry();
203 symval.set_output_value(sym->value());
204 psymval = &symval;
ead1e424 205 }
61ba1cf9 206
b8e6aad9 207 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
ead1e424
ILT
208 view + offset, view_address + offset, view_size))
209 continue;
210
fe8718a4 211 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
ead1e424 212 {
75f2446e
ILT
213 gold_error_at_location(relinfo, i, offset,
214 _("reloc has bad offset %zu"),
215 static_cast<size_t>(offset));
216 continue;
61ba1cf9
ILT
217 }
218
ead1e424
ILT
219 if (sym != NULL
220 && sym->is_undefined()
436ca963 221 && sym->binding() != elfcpp::STB_WEAK
eb42429a
ILT
222 && (!parameters->options().shared() // -shared
223 || parameters->options().defs())) // -z defs
75f2446e 224 gold_undefined_symbol(sym, relinfo, i, offset);
f6ce93d6
ILT
225
226 if (sym != NULL && sym->has_warning())
75f2446e 227 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
61ba1cf9
ILT
228 }
229}
230
6a74a719
ILT
231// This class may be used as a typical class for the
232// Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
233// template parameter Classify_reloc must be a class type which
234// provides a function get_size_for_reloc which returns the number of
235// bytes to which a reloc applies. This class is intended to capture
236// the most typical target behaviour, while still permitting targets
237// to define their own independent class for Scan_relocatable_reloc.
238
239template<int sh_type, typename Classify_reloc>
240class Default_scan_relocatable_relocs
241{
242 public:
243 // Return the strategy to use for a local symbol which is not a
244 // section symbol, given the relocation type.
245 inline Relocatable_relocs::Reloc_strategy
246 local_non_section_strategy(unsigned int, Relobj*)
247 { return Relocatable_relocs::RELOC_COPY; }
248
249 // Return the strategy to use for a local symbol which is a section
250 // symbol, given the relocation type.
251 inline Relocatable_relocs::Reloc_strategy
252 local_section_strategy(unsigned int r_type, Relobj* object)
253 {
254 if (sh_type == elfcpp::SHT_RELA)
255 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
256 else
257 {
258 Classify_reloc classify;
259 switch (classify.get_size_for_reloc(r_type, object))
260 {
261 case 0:
7019cd25 262 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
6a74a719
ILT
263 case 1:
264 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
265 case 2:
266 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
267 case 4:
268 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
269 case 8:
270 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
271 default:
272 gold_unreachable();
273 }
274 }
275 }
276
277 // Return the strategy to use for a global symbol, given the
278 // relocation type, the object, and the symbol index.
279 inline Relocatable_relocs::Reloc_strategy
280 global_strategy(unsigned int, Relobj*, unsigned int)
281 { return Relocatable_relocs::RELOC_COPY; }
282};
283
284// Scan relocs during a relocatable link. This is a default
285// definition which should work for most targets.
286// Scan_relocatable_reloc must name a class type which provides three
287// functions which return a Relocatable_relocs::Reloc_strategy code:
288// global_strategy, local_non_section_strategy, and
289// local_section_strategy. Most targets should be able to use
290// Default_scan_relocatable_relocs as this class.
291
7019cd25 292template<int size, bool big_endian, int sh_type,
6a74a719
ILT
293 typename Scan_relocatable_reloc>
294void
295scan_relocatable_relocs(
296 const General_options&,
297 Symbol_table*,
298 Layout*,
299 Sized_relobj<size, big_endian>* object,
300 unsigned int data_shndx,
301 const unsigned char* prelocs,
302 size_t reloc_count,
303 Output_section* output_section,
304 bool needs_special_offset_handling,
305 size_t local_symbol_count,
306 const unsigned char* plocal_syms,
307 Relocatable_relocs* rr)
308{
309 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
310 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
311 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
312 Scan_relocatable_reloc scan;
313
314 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
315 {
316 Reltype reloc(prelocs);
317
318 Relocatable_relocs::Reloc_strategy strategy;
319
320 if (needs_special_offset_handling
321 && !output_section->is_input_address_mapped(object, data_shndx,
322 reloc.get_r_offset()))
323 strategy = Relocatable_relocs::RELOC_DISCARD;
324 else
325 {
326 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
327 reloc.get_r_info();
328 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
329 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
330
331 if (r_sym >= local_symbol_count)
332 strategy = scan.global_strategy(r_type, object, r_sym);
333 else
334 {
335 gold_assert(plocal_syms != NULL);
336 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
337 + r_sym * sym_size);
d491d34e
ILT
338 unsigned int shndx = lsym.get_st_shndx();
339 bool is_ordinary;
340 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
341 if (is_ordinary
6a74a719 342 && shndx != elfcpp::SHN_UNDEF
d491d34e 343 && !object->is_section_included(shndx))
6a74a719
ILT
344 {
345 // RELOC is a relocation against a local symbol
346 // defined in a section we are discarding. Discard
347 // the reloc. FIXME: Should we issue a warning?
348 strategy = Relocatable_relocs::RELOC_DISCARD;
349 }
350 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
351 strategy = scan.local_non_section_strategy(r_type, object);
352 else
353 {
354 strategy = scan.local_section_strategy(r_type, object);
355 if (strategy != Relocatable_relocs::RELOC_DISCARD)
356 {
357 section_offset_type dummy;
358 Output_section* os = object->output_section(shndx,
359 &dummy);
360 os->set_needs_symtab_index();
361 }
362 }
363 }
364 }
365
366 rr->set_next_reloc_strategy(strategy);
367 }
368}
369
370// Relocate relocs during a relocatable link. This is a default
371// definition which should work for most targets.
372
7019cd25 373template<int size, bool big_endian, int sh_type>
6a74a719
ILT
374void
375relocate_for_relocatable(
376 const Relocate_info<size, big_endian>* relinfo,
377 const unsigned char* prelocs,
378 size_t reloc_count,
379 Output_section* output_section,
380 off_t offset_in_output_section,
381 const Relocatable_relocs* rr,
382 unsigned char* view,
6be6f3bd 383 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
6a74a719
ILT
384 section_size_type,
385 unsigned char* reloc_view,
386 section_size_type reloc_view_size)
387{
388 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
389 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
390 Reltype_write;
391 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
392
393 Sized_relobj<size, big_endian>* const object = relinfo->object;
394 const unsigned int local_count = object->local_symbol_count();
395
396 unsigned char* pwrite = reloc_view;
397
398 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
399 {
400 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
401 if (strategy == Relocatable_relocs::RELOC_DISCARD)
402 continue;
403
404 Reltype reloc(prelocs);
405 Reltype_write reloc_write(pwrite);
406
407 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
408 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
409 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
410
411 // Get the new symbol index.
412
413 unsigned int new_symndx;
414 if (r_sym < local_count)
415 {
416 switch (strategy)
417 {
418 case Relocatable_relocs::RELOC_COPY:
419 new_symndx = object->symtab_index(r_sym);
420 gold_assert(new_symndx != -1U);
421 break;
422
423 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7019cd25 424 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
6a74a719
ILT
425 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
426 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
427 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
428 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
429 {
430 // We are adjusting a section symbol. We need to find
431 // the symbol table index of the section symbol for
432 // the output section corresponding to input section
433 // in which this symbol is defined.
434 gold_assert(r_sym < local_count);
d491d34e
ILT
435 bool is_ordinary;
436 unsigned int shndx =
437 object->local_symbol_input_shndx(r_sym, &is_ordinary);
438 gold_assert(is_ordinary);
6a74a719
ILT
439 section_offset_type dummy;
440 Output_section* os = object->output_section(shndx, &dummy);
441 gold_assert(os != NULL);
442 gold_assert(os->needs_symtab_index());
443 new_symndx = os->symtab_index();
444 }
445 break;
446
447 default:
448 gold_unreachable();
449 }
450 }
451 else
452 {
453 const Symbol* gsym = object->global_symbol(r_sym);
454 gold_assert(gsym != NULL);
455 if (gsym->is_forwarder())
456 gsym = relinfo->symtab->resolve_forwards(gsym);
457
458 gold_assert(gsym->has_symtab_index());
459 new_symndx = gsym->symtab_index();
460 }
461
462 // Get the new offset--the location in the output section where
463 // this relocation should be applied.
464
465 off_t offset = reloc.get_r_offset();
466 off_t new_offset;
467 if (offset_in_output_section != -1)
468 new_offset = offset + offset_in_output_section;
469 else
470 {
471 new_offset = output_section->output_offset(object,
472 relinfo->data_shndx,
473 offset);
474 gold_assert(new_offset != -1);
475 }
476
6be6f3bd
ILT
477 // In an object file, r_offset is an offset within the section.
478 // In an executable or dynamic object, generated by
479 // --emit-relocs, r_offset is an absolute address.
480 if (!parameters->options().relocatable())
481 new_offset += view_address;
482
6a74a719
ILT
483 reloc_write.put_r_offset(new_offset);
484 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
485
486 // Handle the reloc addend based on the strategy.
487
488 if (strategy == Relocatable_relocs::RELOC_COPY)
489 {
490 if (sh_type == elfcpp::SHT_RELA)
491 Reloc_types<sh_type, size, big_endian>::
492 copy_reloc_addend(&reloc_write,
493 &reloc);
494 }
495 else
496 {
497 // The relocation uses a section symbol in the input file.
498 // We are adjusting it to use a section symbol in the output
499 // file. The input section symbol refers to some address in
500 // the input section. We need the relocation in the output
501 // file to refer to that same address. This adjustment to
502 // the addend is the same calculation we use for a simple
503 // absolute relocation for the input section symbol.
504
505 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
506
507 unsigned char* padd = view + offset;
508 switch (strategy)
509 {
510 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
511 {
512 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
513 addend = Reloc_types<sh_type, size, big_endian>::
514 get_reloc_addend(&reloc);
515 addend = psymval->value(object, addend);
516 Reloc_types<sh_type, size, big_endian>::
517 set_reloc_addend(&reloc_write, addend);
518 }
519 break;
520
7019cd25
ILT
521 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
522 break;
523
6a74a719
ILT
524 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
525 Relocate_functions<size, big_endian>::rel8(padd, object,
526 psymval);
527 break;
528
529 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
530 Relocate_functions<size, big_endian>::rel16(padd, object,
531 psymval);
532 break;
533
534 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
535 Relocate_functions<size, big_endian>::rel32(padd, object,
536 psymval);
537 break;
538
539 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
540 Relocate_functions<size, big_endian>::rel64(padd, object,
541 psymval);
542 break;
543
544 default:
545 gold_unreachable();
546 }
547 }
548
549 pwrite += reloc_size;
550 }
551
552 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
553 == reloc_view_size);
554}
555
61ba1cf9
ILT
556} // End namespace gold.
557
558#endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.114523 seconds and 4 git commands to generate.