1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007 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
29 #include "reloc-types.h"
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
,
45 const General_options
& options
,
49 Sized_relobj
<size
, big_endian
>* object
,
50 unsigned int data_shndx
,
51 const unsigned char* prelocs
,
53 Output_section
* output_section
,
54 bool needs_special_offset_handling
,
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
;
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()))
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
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?
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.
104 scan
.local(options
, symtab
, layout
, target
, object
, data_shndx
,
105 output_section
, reloc
, r_type
, lsym
);
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
,
143 const Relocate_info
<size
, big_endian
>* relinfo
,
145 const unsigned char* prelocs
,
147 Output_section
* output_section
,
148 bool needs_special_offset_handling
,
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
;
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
,
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
)
187 psymval
= object
->local_symbol(r_sym
);
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());
200 symval
.set_no_output_symtab_entry();
201 symval
.set_output_value(sym
->value());
205 if (!relocate
.relocate(relinfo
, target
, i
, reloc
, r_type
, sym
, psymval
,
206 view
+ offset
, view_address
+ offset
, view_size
))
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
));
218 && sym
->is_undefined()
219 && sym
->binding() != elfcpp::STB_WEAK
220 && !parameters
->output_is_shared())
221 gold_undefined_symbol(sym
, relinfo
, i
, offset
);
223 if (sym
!= NULL
&& sym
->has_warning())
224 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
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.
236 template<int sh_type
, typename Classify_reloc
>
237 class Default_scan_relocatable_relocs
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
; }
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
)
251 if (sh_type
== elfcpp::SHT_RELA
)
252 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
255 Classify_reloc classify
;
256 switch (classify
.get_size_for_reloc(r_type
, object
))
259 return Relocatable_relocs::RELOC_COPY
;
261 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
263 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
265 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
267 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
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
; }
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.
289 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
290 typename Scan_relocatable_reloc
>
292 scan_relocatable_relocs(
293 const General_options
&,
296 Sized_relobj
<size
, big_endian
>* object
,
297 unsigned int data_shndx
,
298 const unsigned char* prelocs
,
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
)
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
;
311 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
313 Reltype
reloc(prelocs
);
315 Relocatable_relocs::Reloc_strategy strategy
;
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
;
323 typename
elfcpp::Elf_types
<size
>::Elf_WXword 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
);
328 if (r_sym
>= local_symbol_count
)
329 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
332 gold_assert(plocal_syms
!= NULL
);
333 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
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()))
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
;
345 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
346 strategy
= scan
.local_non_section_strategy(r_type
, object
);
349 strategy
= scan
.local_section_strategy(r_type
, object
);
350 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
352 section_offset_type dummy
;
353 Output_section
* os
= object
->output_section(shndx
,
355 os
->set_needs_symtab_index();
361 rr
->set_next_reloc_strategy(strategy
);
365 // Relocate relocs during a relocatable link. This is a default
366 // definition which should work for most targets.
368 template<int size
, bool big_endian
, typename Target_type
, int sh_type
>
370 relocate_for_relocatable(
371 const Relocate_info
<size
, big_endian
>* relinfo
,
372 const unsigned char* prelocs
,
374 Output_section
* output_section
,
375 off_t offset_in_output_section
,
376 const Relocatable_relocs
* rr
,
378 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
380 unsigned char* reloc_view
,
381 section_size_type reloc_view_size
)
383 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
384 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
386 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
388 Sized_relobj
<size
, big_endian
>* const object
= relinfo
->object
;
389 const unsigned int local_count
= object
->local_symbol_count();
391 unsigned char* pwrite
= reloc_view
;
393 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
395 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
396 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
399 Reltype
reloc(prelocs
);
400 Reltype_write
reloc_write(pwrite
);
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
);
406 // Get the new symbol index.
408 unsigned int new_symndx
;
409 if (r_sym
< local_count
)
413 case Relocatable_relocs::RELOC_COPY
:
414 new_symndx
= object
->symtab_index(r_sym
);
415 gold_assert(new_symndx
!= -1U);
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
:
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();
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
);
449 gold_assert(gsym
->has_symtab_index());
450 new_symndx
= gsym
->symtab_index();
453 // Get the new offset--the location in the output section where
454 // this relocation should be applied.
456 off_t offset
= reloc
.get_r_offset();
458 if (offset_in_output_section
!= -1)
459 new_offset
= offset
+ offset_in_output_section
;
462 new_offset
= output_section
->output_offset(object
,
465 gold_assert(new_offset
!= -1);
468 reloc_write
.put_r_offset(new_offset
);
469 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
471 // Handle the reloc addend based on the strategy.
473 if (strategy
== Relocatable_relocs::RELOC_COPY
)
475 if (sh_type
== elfcpp::SHT_RELA
)
476 Reloc_types
<sh_type
, size
, big_endian
>::
477 copy_reloc_addend(&reloc_write
,
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.
490 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
492 unsigned char* padd
= view
+ offset
;
495 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
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
);
506 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
507 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
511 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
512 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
516 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
517 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
521 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
522 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
531 pwrite
+= reloc_size
;
534 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
538 } // End namespace gold.
540 #endif // !defined(GOLD_TARGET_RELOC_H)