2008-04-11 Cary Coutant <ccoutant@google.com>
[binutils.git] / gold / target-reloc.h
blobeab3c37eb4a0605339f24cb6698e0a8544012131
1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_TARGET_RELOC_H
24 #define GOLD_TARGET_RELOC_H
26 #include "elfcpp.h"
27 #include "symtab.h"
28 #include "reloc.h"
29 #include "reloc-types.h"
31 namespace gold
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.
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)
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;
63 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
65 Reltype reloc(prelocs);
67 if (needs_special_offset_handling
68 && !output_section->is_input_address_mapped(object, data_shndx,
69 reloc.get_r_offset()))
70 continue;
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);
76 if (r_sym < local_count)
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()))
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.
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.
101 continue;
104 scan.local(options, symtab, layout, target, object, data_shndx,
105 output_section, reloc, r_type, lsym);
107 else
109 Symbol* gsym = object->global_symbol(r_sym);
110 gold_assert(gsym != NULL);
111 if (gsym->is_forwarder())
112 gsym = symtab->resolve_forwards(gsym);
114 scan.global(options, symtab, layout, target, object, data_shndx,
115 output_section, reloc, r_type, gsym);
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.
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.
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.
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.
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)
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;
157 Sized_relobj<size, big_endian>* object = relinfo->object;
158 unsigned int local_count = object->local_symbol_count();
160 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
162 Reltype reloc(prelocs);
164 section_offset_type offset =
165 convert_to_section_size_type(reloc.get_r_offset());
167 if (needs_special_offset_handling)
169 offset = output_section->output_offset(relinfo->object,
170 relinfo->data_shndx,
171 offset);
172 if (offset == -1)
173 continue;
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);
180 const Sized_symbol<size>* sym;
182 Symbol_value<size> symval;
183 const Symbol_value<size> *psymval;
184 if (r_sym < local_count)
186 sym = NULL;
187 psymval = object->local_symbol(r_sym);
189 else
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);
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;
205 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
206 view + offset, view_address + offset, view_size))
207 continue;
209 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
211 gold_error_at_location(relinfo, i, offset,
212 _("reloc has bad offset %zu"),
213 static_cast<size_t>(offset));
214 continue;
217 if (sym != NULL
218 && sym->is_undefined()
219 && sym->binding() != elfcpp::STB_WEAK
220 && (!parameters->options().shared() // -shared
221 || parameters->options().defs())) // -z defs
222 gold_undefined_symbol(sym, relinfo, i, offset);
224 if (sym != NULL && sym->has_warning())
225 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
229 // This class may be used as a typical class for the
230 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
231 // template parameter Classify_reloc must be a class type which
232 // provides a function get_size_for_reloc which returns the number of
233 // bytes to which a reloc applies. This class is intended to capture
234 // the most typical target behaviour, while still permitting targets
235 // to define their own independent class for Scan_relocatable_reloc.
237 template<int sh_type, typename Classify_reloc>
238 class Default_scan_relocatable_relocs
240 public:
241 // Return the strategy to use for a local symbol which is not a
242 // section symbol, given the relocation type.
243 inline Relocatable_relocs::Reloc_strategy
244 local_non_section_strategy(unsigned int, Relobj*)
245 { return Relocatable_relocs::RELOC_COPY; }
247 // Return the strategy to use for a local symbol which is a section
248 // symbol, given the relocation type.
249 inline Relocatable_relocs::Reloc_strategy
250 local_section_strategy(unsigned int r_type, Relobj* object)
252 if (sh_type == elfcpp::SHT_RELA)
253 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
254 else
256 Classify_reloc classify;
257 switch (classify.get_size_for_reloc(r_type, object))
259 case 0:
260 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
261 case 1:
262 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
263 case 2:
264 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
265 case 4:
266 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
267 case 8:
268 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
269 default:
270 gold_unreachable();
275 // Return the strategy to use for a global symbol, given the
276 // relocation type, the object, and the symbol index.
277 inline Relocatable_relocs::Reloc_strategy
278 global_strategy(unsigned int, Relobj*, unsigned int)
279 { return Relocatable_relocs::RELOC_COPY; }
282 // Scan relocs during a relocatable link. This is a default
283 // definition which should work for most targets.
284 // Scan_relocatable_reloc must name a class type which provides three
285 // functions which return a Relocatable_relocs::Reloc_strategy code:
286 // global_strategy, local_non_section_strategy, and
287 // local_section_strategy. Most targets should be able to use
288 // Default_scan_relocatable_relocs as this class.
290 template<int size, bool big_endian, int sh_type,
291 typename Scan_relocatable_reloc>
292 void
293 scan_relocatable_relocs(
294 const General_options&,
295 Symbol_table*,
296 Layout*,
297 Sized_relobj<size, big_endian>* object,
298 unsigned int data_shndx,
299 const unsigned char* prelocs,
300 size_t reloc_count,
301 Output_section* output_section,
302 bool needs_special_offset_handling,
303 size_t local_symbol_count,
304 const unsigned char* plocal_syms,
305 Relocatable_relocs* rr)
307 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
308 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
309 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
310 Scan_relocatable_reloc scan;
312 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
314 Reltype reloc(prelocs);
316 Relocatable_relocs::Reloc_strategy strategy;
318 if (needs_special_offset_handling
319 && !output_section->is_input_address_mapped(object, data_shndx,
320 reloc.get_r_offset()))
321 strategy = Relocatable_relocs::RELOC_DISCARD;
322 else
324 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
325 reloc.get_r_info();
326 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
327 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
329 if (r_sym >= local_symbol_count)
330 strategy = scan.global_strategy(r_type, object, r_sym);
331 else
333 gold_assert(plocal_syms != NULL);
334 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
335 + r_sym * sym_size);
336 const unsigned int shndx = lsym.get_st_shndx();
337 if (shndx < elfcpp::SHN_LORESERVE
338 && shndx != elfcpp::SHN_UNDEF
339 && !object->is_section_included(lsym.get_st_shndx()))
341 // RELOC is a relocation against a local symbol
342 // defined in a section we are discarding. Discard
343 // the reloc. FIXME: Should we issue a warning?
344 strategy = Relocatable_relocs::RELOC_DISCARD;
346 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
347 strategy = scan.local_non_section_strategy(r_type, object);
348 else
350 strategy = scan.local_section_strategy(r_type, object);
351 if (strategy != Relocatable_relocs::RELOC_DISCARD)
353 section_offset_type dummy;
354 Output_section* os = object->output_section(shndx,
355 &dummy);
356 os->set_needs_symtab_index();
362 rr->set_next_reloc_strategy(strategy);
366 // Relocate relocs during a relocatable link. This is a default
367 // definition which should work for most targets.
369 template<int size, bool big_endian, int sh_type>
370 void
371 relocate_for_relocatable(
372 const Relocate_info<size, big_endian>* relinfo,
373 const unsigned char* prelocs,
374 size_t reloc_count,
375 Output_section* output_section,
376 off_t offset_in_output_section,
377 const Relocatable_relocs* rr,
378 unsigned char* view,
379 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
380 section_size_type,
381 unsigned char* reloc_view,
382 section_size_type reloc_view_size)
384 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
385 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
386 Reltype_write;
387 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
389 Sized_relobj<size, big_endian>* const object = relinfo->object;
390 const unsigned int local_count = object->local_symbol_count();
392 unsigned char* pwrite = reloc_view;
394 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
396 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
397 if (strategy == Relocatable_relocs::RELOC_DISCARD)
398 continue;
400 Reltype reloc(prelocs);
401 Reltype_write reloc_write(pwrite);
403 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
404 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
405 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
407 // Get the new symbol index.
409 unsigned int new_symndx;
410 if (r_sym < local_count)
412 switch (strategy)
414 case Relocatable_relocs::RELOC_COPY:
415 new_symndx = object->symtab_index(r_sym);
416 gold_assert(new_symndx != -1U);
417 break;
419 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
420 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
421 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
422 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
423 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
424 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
426 // We are adjusting a section symbol. We need to find
427 // the symbol table index of the section symbol for
428 // the output section corresponding to input section
429 // in which this symbol is defined.
430 gold_assert(r_sym < local_count);
431 unsigned int shndx = object->local_symbol_input_shndx(r_sym);
432 section_offset_type dummy;
433 Output_section* os = object->output_section(shndx, &dummy);
434 gold_assert(os != NULL);
435 gold_assert(os->needs_symtab_index());
436 new_symndx = os->symtab_index();
438 break;
440 default:
441 gold_unreachable();
444 else
446 const Symbol* gsym = object->global_symbol(r_sym);
447 gold_assert(gsym != NULL);
448 if (gsym->is_forwarder())
449 gsym = relinfo->symtab->resolve_forwards(gsym);
451 gold_assert(gsym->has_symtab_index());
452 new_symndx = gsym->symtab_index();
455 // Get the new offset--the location in the output section where
456 // this relocation should be applied.
458 off_t offset = reloc.get_r_offset();
459 off_t new_offset;
460 if (offset_in_output_section != -1)
461 new_offset = offset + offset_in_output_section;
462 else
464 new_offset = output_section->output_offset(object,
465 relinfo->data_shndx,
466 offset);
467 gold_assert(new_offset != -1);
470 // In an object file, r_offset is an offset within the section.
471 // In an executable or dynamic object, generated by
472 // --emit-relocs, r_offset is an absolute address.
473 if (!parameters->options().relocatable())
474 new_offset += view_address;
476 reloc_write.put_r_offset(new_offset);
477 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
479 // Handle the reloc addend based on the strategy.
481 if (strategy == Relocatable_relocs::RELOC_COPY)
483 if (sh_type == elfcpp::SHT_RELA)
484 Reloc_types<sh_type, size, big_endian>::
485 copy_reloc_addend(&reloc_write,
486 &reloc);
488 else
490 // The relocation uses a section symbol in the input file.
491 // We are adjusting it to use a section symbol in the output
492 // file. The input section symbol refers to some address in
493 // the input section. We need the relocation in the output
494 // file to refer to that same address. This adjustment to
495 // the addend is the same calculation we use for a simple
496 // absolute relocation for the input section symbol.
498 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
500 unsigned char* padd = view + offset;
501 switch (strategy)
503 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
505 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
506 addend = Reloc_types<sh_type, size, big_endian>::
507 get_reloc_addend(&reloc);
508 addend = psymval->value(object, addend);
509 Reloc_types<sh_type, size, big_endian>::
510 set_reloc_addend(&reloc_write, addend);
512 break;
514 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
515 break;
517 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
518 Relocate_functions<size, big_endian>::rel8(padd, object,
519 psymval);
520 break;
522 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
523 Relocate_functions<size, big_endian>::rel16(padd, object,
524 psymval);
525 break;
527 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
528 Relocate_functions<size, big_endian>::rel32(padd, object,
529 psymval);
530 break;
532 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
533 Relocate_functions<size, big_endian>::rel64(padd, object,
534 psymval);
535 break;
537 default:
538 gold_unreachable();
542 pwrite += reloc_size;
545 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
546 == reloc_view_size);
549 } // End namespace gold.
551 #endif // !defined(GOLD_TARGET_RELOC_H)