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
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 unsigned int shndx
= lsym
.get_st_shndx();
83 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
85 && shndx
!= elfcpp::SHN_UNDEF
86 && !object
->is_section_included(shndx
))
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.
93 // FIXME: We should issue a warning if this is an
94 // allocated section; is this the best place to do it?
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.
106 scan
.local(options
, symtab
, layout
, target
, object
, data_shndx
,
107 output_section
, reloc
, r_type
, lsym
);
111 Symbol
* gsym
= object
->global_symbol(r_sym
);
112 gold_assert(gsym
!= NULL
);
113 if (gsym
->is_forwarder())
114 gsym
= symtab
->resolve_forwards(gsym
);
116 scan
.global(options
, symtab
, layout
, target
, object
, data_shndx
,
117 output_section
, reloc
, r_type
, gsym
);
122 // Behavior for relocations to discarded comdat sections.
126 CB_UNDETERMINED
, // Not yet determined -- need to look at section name.
127 CB_PRETEND
, // Attempt to map to the corresponding kept section.
128 CB_IGNORE
, // Ignore the relocation.
129 CB_WARNING
// Print a warning.
132 // Decide what the linker should do for relocations that refer to discarded
133 // comdat sections. This decision is based on the name of the section being
136 inline Comdat_behavior
137 get_comdat_behavior(const char* name
)
139 if (Layout::is_debug_info_section(name
))
141 if (strcmp(name
, ".eh_frame") == 0
142 || strcmp(name
, ".gcc_except_table") == 0)
147 // This function implements the generic part of relocation processing.
148 // The template parameter Relocate must be a class type which provides
149 // a single function, relocate(), which implements the machine
150 // specific part of a relocation.
152 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
153 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
154 // RELOCATE implements operator() to do a relocation.
156 // PRELOCS points to the relocation data. RELOC_COUNT is the number
157 // of relocs. OUTPUT_SECTION is the output section.
158 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
159 // mapped to output offsets.
161 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
162 // VIEW_SIZE is the size. These refer to the input section, unless
163 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
164 // the output section.
166 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
170 const Relocate_info
<size
, big_endian
>* relinfo
,
172 const unsigned char* prelocs
,
174 Output_section
* output_section
,
175 bool needs_special_offset_handling
,
177 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
178 section_size_type view_size
)
180 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
181 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
184 Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
185 unsigned int local_count
= object
->local_symbol_count();
187 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
189 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
191 Reltype
reloc(prelocs
);
193 section_offset_type offset
=
194 convert_to_section_size_type(reloc
.get_r_offset());
196 if (needs_special_offset_handling
)
198 offset
= output_section
->output_offset(relinfo
->object
,
205 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
206 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
207 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
209 const Sized_symbol
<size
>* sym
;
211 Symbol_value
<size
> symval
;
212 const Symbol_value
<size
> *psymval
;
213 if (r_sym
< local_count
)
216 psymval
= object
->local_symbol(r_sym
);
218 // If the local symbol belongs to a section we are discarding,
219 // and that section is a debug section, try to find the
220 // corresponding kept section and map this symbol to its
221 // counterpart in the kept section. The symbol must not
222 // correspond to a section we are folding.
224 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
226 && shndx
!= elfcpp::SHN_UNDEF
227 && !object
->is_section_included(shndx
)
228 && !(relinfo
->symtab
->is_section_folded(object
, shndx
)))
230 if (comdat_behavior
== CB_UNDETERMINED
)
232 std::string name
= object
->section_name(relinfo
->data_shndx
);
233 comdat_behavior
= get_comdat_behavior(name
.c_str());
235 if (comdat_behavior
== CB_PRETEND
)
238 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
239 object
->map_to_kept_section(shndx
, &found
);
241 symval
.set_output_value(value
+ psymval
->input_value());
243 symval
.set_output_value(0);
247 if (comdat_behavior
== CB_WARNING
)
248 gold_warning_at_location(relinfo
, i
, offset
,
249 _("Relocation refers to discarded "
251 symval
.set_output_value(0);
253 symval
.set_no_output_symtab_entry();
259 const Symbol
* gsym
= object
->global_symbol(r_sym
);
260 gold_assert(gsym
!= NULL
);
261 if (gsym
->is_forwarder())
262 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
264 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
265 if (sym
->has_symtab_index())
266 symval
.set_output_symtab_index(sym
->symtab_index());
268 symval
.set_no_output_symtab_entry();
269 symval
.set_output_value(sym
->value());
273 if (!relocate
.relocate(relinfo
, target
, output_section
, i
, reloc
,
274 r_type
, sym
, psymval
, view
+ offset
,
275 view_address
+ offset
, view_size
))
278 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
280 gold_error_at_location(relinfo
, i
, offset
,
281 _("reloc has bad offset %zu"),
282 static_cast<size_t>(offset
));
287 && sym
->is_undefined()
288 && sym
->binding() != elfcpp::STB_WEAK
289 && !target
->is_defined_by_abi(sym
)
290 && (!parameters
->options().shared() // -shared
291 || parameters
->options().defs())) // -z defs
292 gold_undefined_symbol_at_location(sym
, relinfo
, i
, offset
);
294 if (sym
!= NULL
&& sym
->has_warning())
295 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
299 // This class may be used as a typical class for the
300 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
301 // template parameter Classify_reloc must be a class type which
302 // provides a function get_size_for_reloc which returns the number of
303 // bytes to which a reloc applies. This class is intended to capture
304 // the most typical target behaviour, while still permitting targets
305 // to define their own independent class for Scan_relocatable_reloc.
307 template<int sh_type
, typename Classify_reloc
>
308 class Default_scan_relocatable_relocs
311 // Return the strategy to use for a local symbol which is not a
312 // section symbol, given the relocation type.
313 inline Relocatable_relocs::Reloc_strategy
314 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
316 // We assume that relocation type 0 is NONE. Targets which are
317 // different must override.
318 if (r_type
== 0 && r_sym
== 0)
319 return Relocatable_relocs::RELOC_DISCARD
;
320 return Relocatable_relocs::RELOC_COPY
;
323 // Return the strategy to use for a local symbol which is a section
324 // symbol, given the relocation type.
325 inline Relocatable_relocs::Reloc_strategy
326 local_section_strategy(unsigned int r_type
, Relobj
* object
)
328 if (sh_type
== elfcpp::SHT_RELA
)
329 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
332 Classify_reloc classify
;
333 switch (classify
.get_size_for_reloc(r_type
, object
))
336 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
338 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
340 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
342 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
344 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
351 // Return the strategy to use for a global symbol, given the
352 // relocation type, the object, and the symbol index.
353 inline Relocatable_relocs::Reloc_strategy
354 global_strategy(unsigned int, Relobj
*, unsigned int)
355 { return Relocatable_relocs::RELOC_COPY
; }
358 // Scan relocs during a relocatable link. This is a default
359 // definition which should work for most targets.
360 // Scan_relocatable_reloc must name a class type which provides three
361 // functions which return a Relocatable_relocs::Reloc_strategy code:
362 // global_strategy, local_non_section_strategy, and
363 // local_section_strategy. Most targets should be able to use
364 // Default_scan_relocatable_relocs as this class.
366 template<int size
, bool big_endian
, int sh_type
,
367 typename Scan_relocatable_reloc
>
369 scan_relocatable_relocs(
370 const General_options
&,
373 Sized_relobj
<size
, big_endian
>* object
,
374 unsigned int data_shndx
,
375 const unsigned char* prelocs
,
377 Output_section
* output_section
,
378 bool needs_special_offset_handling
,
379 size_t local_symbol_count
,
380 const unsigned char* plocal_syms
,
381 Relocatable_relocs
* rr
)
383 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
384 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
385 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
386 Scan_relocatable_reloc scan
;
388 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
390 Reltype
reloc(prelocs
);
392 Relocatable_relocs::Reloc_strategy strategy
;
394 if (needs_special_offset_handling
395 && !output_section
->is_input_address_mapped(object
, data_shndx
,
396 reloc
.get_r_offset()))
397 strategy
= Relocatable_relocs::RELOC_DISCARD
;
400 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
402 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
403 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
405 if (r_sym
>= local_symbol_count
)
406 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
409 gold_assert(plocal_syms
!= NULL
);
410 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
412 unsigned int shndx
= lsym
.get_st_shndx();
414 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
416 && shndx
!= elfcpp::SHN_UNDEF
417 && !object
->is_section_included(shndx
))
419 // RELOC is a relocation against a local symbol
420 // defined in a section we are discarding. Discard
421 // the reloc. FIXME: Should we issue a warning?
422 strategy
= Relocatable_relocs::RELOC_DISCARD
;
424 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
425 strategy
= scan
.local_non_section_strategy(r_type
, object
,
429 strategy
= scan
.local_section_strategy(r_type
, object
);
430 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
431 object
->output_section(shndx
)->set_needs_symtab_index();
436 rr
->set_next_reloc_strategy(strategy
);
440 // Relocate relocs during a relocatable link. This is a default
441 // definition which should work for most targets.
443 template<int size
, bool big_endian
, int sh_type
>
445 relocate_for_relocatable(
446 const Relocate_info
<size
, big_endian
>* relinfo
,
447 const unsigned char* prelocs
,
449 Output_section
* output_section
,
450 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
451 const Relocatable_relocs
* rr
,
453 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
455 unsigned char* reloc_view
,
456 section_size_type reloc_view_size
)
458 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
459 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
460 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
462 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
463 const Address invalid_address
= static_cast<Address
>(0) - 1;
465 Sized_relobj
<size
, big_endian
>* const object
= relinfo
->object
;
466 const unsigned int local_count
= object
->local_symbol_count();
468 unsigned char* pwrite
= reloc_view
;
470 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
472 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
473 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
476 Reltype
reloc(prelocs
);
477 Reltype_write
reloc_write(pwrite
);
479 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
480 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
481 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
483 // Get the new symbol index.
485 unsigned int new_symndx
;
486 if (r_sym
< local_count
)
490 case Relocatable_relocs::RELOC_COPY
:
491 new_symndx
= object
->symtab_index(r_sym
);
492 gold_assert(new_symndx
!= -1U);
495 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
496 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
497 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
498 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
499 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
500 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
502 // We are adjusting a section symbol. We need to find
503 // the symbol table index of the section symbol for
504 // the output section corresponding to input section
505 // in which this symbol is defined.
506 gold_assert(r_sym
< local_count
);
509 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
510 gold_assert(is_ordinary
);
511 Output_section
* os
= object
->output_section(shndx
);
512 gold_assert(os
!= NULL
);
513 gold_assert(os
->needs_symtab_index());
514 new_symndx
= os
->symtab_index();
524 const Symbol
* gsym
= object
->global_symbol(r_sym
);
525 gold_assert(gsym
!= NULL
);
526 if (gsym
->is_forwarder())
527 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
529 gold_assert(gsym
->has_symtab_index());
530 new_symndx
= gsym
->symtab_index();
533 // Get the new offset--the location in the output section where
534 // this relocation should be applied.
536 Address offset
= reloc
.get_r_offset();
538 if (offset_in_output_section
!= invalid_address
)
539 new_offset
= offset
+ offset_in_output_section
;
542 section_offset_type sot_offset
=
543 convert_types
<section_offset_type
, Address
>(offset
);
544 section_offset_type new_sot_offset
=
545 output_section
->output_offset(object
, relinfo
->data_shndx
,
547 gold_assert(new_sot_offset
!= -1);
548 new_offset
= new_sot_offset
;
551 // In an object file, r_offset is an offset within the section.
552 // In an executable or dynamic object, generated by
553 // --emit-relocs, r_offset is an absolute address.
554 if (!parameters
->options().relocatable())
556 new_offset
+= view_address
;
557 if (offset_in_output_section
!= invalid_address
)
558 new_offset
-= offset_in_output_section
;
561 reloc_write
.put_r_offset(new_offset
);
562 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
564 // Handle the reloc addend based on the strategy.
566 if (strategy
== Relocatable_relocs::RELOC_COPY
)
568 if (sh_type
== elfcpp::SHT_RELA
)
569 Reloc_types
<sh_type
, size
, big_endian
>::
570 copy_reloc_addend(&reloc_write
,
575 // The relocation uses a section symbol in the input file.
576 // We are adjusting it to use a section symbol in the output
577 // file. The input section symbol refers to some address in
578 // the input section. We need the relocation in the output
579 // file to refer to that same address. This adjustment to
580 // the addend is the same calculation we use for a simple
581 // absolute relocation for the input section symbol.
583 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
585 unsigned char* padd
= view
+ offset
;
588 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
590 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
591 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
592 get_reloc_addend(&reloc
);
593 addend
= psymval
->value(object
, addend
);
594 Reloc_types
<sh_type
, size
, big_endian
>::
595 set_reloc_addend(&reloc_write
, addend
);
599 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
602 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
603 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
607 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
608 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
612 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
613 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
617 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
618 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
627 pwrite
+= reloc_size
;
630 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
634 } // End namespace gold.
636 #endif // !defined(GOLD_TARGET_RELOC_H)