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