Implement -q/--emit-relocs.
[deliverable/binutils-gdb.git] / gold / target-reloc.h
1 // target-reloc.h -- target specific relocation support -*- C++ -*-
2
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
23 #ifndef GOLD_TARGET_RELOC_H
24 #define GOLD_TARGET_RELOC_H
25
26 #include "elfcpp.h"
27 #include "symtab.h"
28 #include "reloc.h"
29 #include "reloc-types.h"
30
31 namespace gold
32 {
33
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.
40
41 template<int size, bool big_endian, typename Target_type, int sh_type,
42 typename Scan>
43 inline void
44 scan_relocs(
45 const General_options& options,
46 Symbol_table* symtab,
47 Layout* layout,
48 Target_type* target,
49 Sized_relobj<size, big_endian>* object,
50 unsigned int data_shndx,
51 const unsigned char* prelocs,
52 size_t reloc_count,
53 Output_section* output_section,
54 bool needs_special_offset_handling,
55 size_t local_count,
56 const unsigned char* plocal_syms)
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
67 if (needs_special_offset_handling
68 && !output_section->is_input_address_mapped(object, data_shndx,
69 reloc.get_r_offset()))
70 continue;
71
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 {
78 gold_assert(plocal_syms != NULL);
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
83 && shndx != elfcpp::SHN_UNDEF
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
104 scan.local(options, symtab, layout, target, object, data_shndx,
105 output_section, reloc, r_type, lsym);
106 }
107 else
108 {
109 Symbol* gsym = object->global_symbol(r_sym);
110 gold_assert(gsym != NULL);
111 if (gsym->is_forwarder())
112 gsym = symtab->resolve_forwards(gsym);
113
114 scan.global(options, symtab, layout, target, object, data_shndx,
115 output_section, reloc, r_type, gsym);
116 }
117 }
118 }
119
120 // This function implements the generic part of relocation processing.
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.
124
125 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
126 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
127 // RELOCATE implements operator() to do a relocation.
128
129 // PRELOCS points to the relocation data. RELOC_COUNT is the number
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.
138
139 template<int size, bool big_endian, typename Target_type, int sh_type,
140 typename Relocate>
141 inline void
142 relocate_section(
143 const Relocate_info<size, big_endian>* relinfo,
144 Target_type* target,
145 const unsigned char* prelocs,
146 size_t reloc_count,
147 Output_section* output_section,
148 bool needs_special_offset_handling,
149 unsigned char* view,
150 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
151 section_size_type view_size)
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
157 Sized_relobj<size, big_endian>* object = relinfo->object;
158 unsigned int local_count = object->local_symbol_count();
159
160 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
161 {
162 Reltype reloc(prelocs);
163
164 section_offset_type offset =
165 convert_to_section_size_type(reloc.get_r_offset());
166
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
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
180 const Sized_symbol<size>* sym;
181
182 Symbol_value<size> symval;
183 const Symbol_value<size> *psymval;
184 if (r_sym < local_count)
185 {
186 sym = NULL;
187 psymval = object->local_symbol(r_sym);
188 }
189 else
190 {
191 const Symbol* gsym = object->global_symbol(r_sym);
192 gold_assert(gsym != NULL);
193 if (gsym->is_forwarder())
194 gsym = relinfo->symtab->resolve_forwards(gsym);
195
196 sym = static_cast<const Sized_symbol<size>*>(gsym);
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;
203 }
204
205 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
206 view + offset, view_address + offset, view_size))
207 continue;
208
209 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
210 {
211 gold_error_at_location(relinfo, i, offset,
212 _("reloc has bad offset %zu"),
213 static_cast<size_t>(offset));
214 continue;
215 }
216
217 if (sym != NULL
218 && sym->is_undefined()
219 && sym->binding() != elfcpp::STB_WEAK
220 && !parameters->output_is_shared())
221 gold_undefined_symbol(sym, relinfo, i, offset);
222
223 if (sym != NULL && sym->has_warning())
224 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
225 }
226 }
227
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
236 template<int sh_type, typename Classify_reloc>
237 class 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_ADJUST_FOR_SECTION_0;
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
289 template<int size, bool big_endian, int sh_type,
290 typename Scan_relocatable_reloc>
291 void
292 scan_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
368 template<int size, bool big_endian, int sh_type>
369 void
370 relocate_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_0:
420 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
421 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
422 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
423 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
424 {
425 // We are adjusting a section symbol. We need to find
426 // the symbol table index of the section symbol for
427 // the output section corresponding to input section
428 // in which this symbol is defined.
429 gold_assert(r_sym < local_count);
430 unsigned int shndx = object->local_symbol_input_shndx(r_sym);
431 section_offset_type dummy;
432 Output_section* os = object->output_section(shndx, &dummy);
433 gold_assert(os != NULL);
434 gold_assert(os->needs_symtab_index());
435 new_symndx = os->symtab_index();
436 }
437 break;
438
439 default:
440 gold_unreachable();
441 }
442 }
443 else
444 {
445 const Symbol* gsym = object->global_symbol(r_sym);
446 gold_assert(gsym != NULL);
447 if (gsym->is_forwarder())
448 gsym = relinfo->symtab->resolve_forwards(gsym);
449
450 gold_assert(gsym->has_symtab_index());
451 new_symndx = gsym->symtab_index();
452 }
453
454 // Get the new offset--the location in the output section where
455 // this relocation should be applied.
456
457 off_t offset = reloc.get_r_offset();
458 off_t new_offset;
459 if (offset_in_output_section != -1)
460 new_offset = offset + offset_in_output_section;
461 else
462 {
463 new_offset = output_section->output_offset(object,
464 relinfo->data_shndx,
465 offset);
466 gold_assert(new_offset != -1);
467 }
468
469 reloc_write.put_r_offset(new_offset);
470 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
471
472 // Handle the reloc addend based on the strategy.
473
474 if (strategy == Relocatable_relocs::RELOC_COPY)
475 {
476 if (sh_type == elfcpp::SHT_RELA)
477 Reloc_types<sh_type, size, big_endian>::
478 copy_reloc_addend(&reloc_write,
479 &reloc);
480 }
481 else
482 {
483 // The relocation uses a section symbol in the input file.
484 // We are adjusting it to use a section symbol in the output
485 // file. The input section symbol refers to some address in
486 // the input section. We need the relocation in the output
487 // file to refer to that same address. This adjustment to
488 // the addend is the same calculation we use for a simple
489 // absolute relocation for the input section symbol.
490
491 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
492
493 unsigned char* padd = view + offset;
494 switch (strategy)
495 {
496 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
497 {
498 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
499 addend = Reloc_types<sh_type, size, big_endian>::
500 get_reloc_addend(&reloc);
501 addend = psymval->value(object, addend);
502 Reloc_types<sh_type, size, big_endian>::
503 set_reloc_addend(&reloc_write, addend);
504 }
505 break;
506
507 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
508 break;
509
510 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
511 Relocate_functions<size, big_endian>::rel8(padd, object,
512 psymval);
513 break;
514
515 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
516 Relocate_functions<size, big_endian>::rel16(padd, object,
517 psymval);
518 break;
519
520 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
521 Relocate_functions<size, big_endian>::rel32(padd, object,
522 psymval);
523 break;
524
525 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
526 Relocate_functions<size, big_endian>::rel64(padd, object,
527 psymval);
528 break;
529
530 default:
531 gold_unreachable();
532 }
533 }
534
535 pwrite += reloc_size;
536 }
537
538 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
539 == reloc_view_size);
540 }
541
542 } // End namespace gold.
543
544 #endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.044204 seconds and 5 git commands to generate.