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.
223 unsigned int shndx
= psymval
->input_shndx(&is_ordinary
);
225 && shndx
!= elfcpp::SHN_UNDEF
226 && !object
->is_section_included(shndx
))
228 if (comdat_behavior
== CB_UNDETERMINED
)
231 object
->section_name(relinfo
->data_shndx
).c_str();
232 comdat_behavior
= get_comdat_behavior(name
);
234 if (comdat_behavior
== CB_PRETEND
)
237 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
238 object
->map_to_kept_section(shndx
, &found
);
240 symval
.set_output_value(value
+ psymval
->input_value());
242 symval
.set_output_value(0);
246 if (comdat_behavior
== CB_WARNING
)
247 gold_warning_at_location(relinfo
, i
, offset
,
248 _("Relocation refers to discarded "
250 symval
.set_output_value(0);
252 symval
.set_no_output_symtab_entry();
258 const Symbol
* gsym
= object
->global_symbol(r_sym
);
259 gold_assert(gsym
!= NULL
);
260 if (gsym
->is_forwarder())
261 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
263 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
264 if (sym
->has_symtab_index())
265 symval
.set_output_symtab_index(sym
->symtab_index());
267 symval
.set_no_output_symtab_entry();
268 symval
.set_output_value(sym
->value());
272 if (!relocate
.relocate(relinfo
, target
, i
, reloc
, r_type
, sym
, psymval
,
273 view
+ offset
, view_address
+ offset
, view_size
))
276 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
278 gold_error_at_location(relinfo
, i
, offset
,
279 _("reloc has bad offset %zu"),
280 static_cast<size_t>(offset
));
285 && sym
->is_undefined()
286 && sym
->binding() != elfcpp::STB_WEAK
287 && (!parameters
->options().shared() // -shared
288 || parameters
->options().defs())) // -z defs
289 gold_undefined_symbol(sym
, relinfo
, i
, offset
);
291 if (sym
!= NULL
&& sym
->has_warning())
292 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
296 // This class may be used as a typical class for the
297 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
298 // template parameter Classify_reloc must be a class type which
299 // provides a function get_size_for_reloc which returns the number of
300 // bytes to which a reloc applies. This class is intended to capture
301 // the most typical target behaviour, while still permitting targets
302 // to define their own independent class for Scan_relocatable_reloc.
304 template<int sh_type
, typename Classify_reloc
>
305 class Default_scan_relocatable_relocs
308 // Return the strategy to use for a local symbol which is not a
309 // section symbol, given the relocation type.
310 inline Relocatable_relocs::Reloc_strategy
311 local_non_section_strategy(unsigned int, Relobj
*)
312 { return Relocatable_relocs::RELOC_COPY
; }
314 // Return the strategy to use for a local symbol which is a section
315 // symbol, given the relocation type.
316 inline Relocatable_relocs::Reloc_strategy
317 local_section_strategy(unsigned int r_type
, Relobj
* object
)
319 if (sh_type
== elfcpp::SHT_RELA
)
320 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
323 Classify_reloc classify
;
324 switch (classify
.get_size_for_reloc(r_type
, object
))
327 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
329 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
331 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
333 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
335 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
342 // Return the strategy to use for a global symbol, given the
343 // relocation type, the object, and the symbol index.
344 inline Relocatable_relocs::Reloc_strategy
345 global_strategy(unsigned int, Relobj
*, unsigned int)
346 { return Relocatable_relocs::RELOC_COPY
; }
349 // Scan relocs during a relocatable link. This is a default
350 // definition which should work for most targets.
351 // Scan_relocatable_reloc must name a class type which provides three
352 // functions which return a Relocatable_relocs::Reloc_strategy code:
353 // global_strategy, local_non_section_strategy, and
354 // local_section_strategy. Most targets should be able to use
355 // Default_scan_relocatable_relocs as this class.
357 template<int size
, bool big_endian
, int sh_type
,
358 typename Scan_relocatable_reloc
>
360 scan_relocatable_relocs(
361 const General_options
&,
364 Sized_relobj
<size
, big_endian
>* object
,
365 unsigned int data_shndx
,
366 const unsigned char* prelocs
,
368 Output_section
* output_section
,
369 bool needs_special_offset_handling
,
370 size_t local_symbol_count
,
371 const unsigned char* plocal_syms
,
372 Relocatable_relocs
* rr
)
374 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
375 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
376 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
377 Scan_relocatable_reloc scan
;
379 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
381 Reltype
reloc(prelocs
);
383 Relocatable_relocs::Reloc_strategy strategy
;
385 if (needs_special_offset_handling
386 && !output_section
->is_input_address_mapped(object
, data_shndx
,
387 reloc
.get_r_offset()))
388 strategy
= Relocatable_relocs::RELOC_DISCARD
;
391 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
393 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
394 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
396 if (r_sym
>= local_symbol_count
)
397 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
400 gold_assert(plocal_syms
!= NULL
);
401 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
403 unsigned int shndx
= lsym
.get_st_shndx();
405 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
407 && shndx
!= elfcpp::SHN_UNDEF
408 && !object
->is_section_included(shndx
))
410 // RELOC is a relocation against a local symbol
411 // defined in a section we are discarding. Discard
412 // the reloc. FIXME: Should we issue a warning?
413 strategy
= Relocatable_relocs::RELOC_DISCARD
;
415 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
416 strategy
= scan
.local_non_section_strategy(r_type
, object
);
419 strategy
= scan
.local_section_strategy(r_type
, object
);
420 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
422 section_offset_type dummy
;
423 Output_section
* os
= object
->output_section(shndx
,
425 os
->set_needs_symtab_index();
431 rr
->set_next_reloc_strategy(strategy
);
435 // Relocate relocs during a relocatable link. This is a default
436 // definition which should work for most targets.
438 template<int size
, bool big_endian
, int sh_type
>
440 relocate_for_relocatable(
441 const Relocate_info
<size
, big_endian
>* relinfo
,
442 const unsigned char* prelocs
,
444 Output_section
* output_section
,
445 off_t offset_in_output_section
,
446 const Relocatable_relocs
* rr
,
448 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
450 unsigned char* reloc_view
,
451 section_size_type reloc_view_size
)
453 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
454 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
456 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
458 Sized_relobj
<size
, big_endian
>* const object
= relinfo
->object
;
459 const unsigned int local_count
= object
->local_symbol_count();
461 unsigned char* pwrite
= reloc_view
;
463 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
465 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
466 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
469 Reltype
reloc(prelocs
);
470 Reltype_write
reloc_write(pwrite
);
472 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
473 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
474 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
476 // Get the new symbol index.
478 unsigned int new_symndx
;
479 if (r_sym
< local_count
)
483 case Relocatable_relocs::RELOC_COPY
:
484 new_symndx
= object
->symtab_index(r_sym
);
485 gold_assert(new_symndx
!= -1U);
488 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
489 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
490 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
491 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
492 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
493 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
495 // We are adjusting a section symbol. We need to find
496 // the symbol table index of the section symbol for
497 // the output section corresponding to input section
498 // in which this symbol is defined.
499 gold_assert(r_sym
< local_count
);
502 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
503 gold_assert(is_ordinary
);
504 section_offset_type dummy
;
505 Output_section
* os
= object
->output_section(shndx
, &dummy
);
506 gold_assert(os
!= NULL
);
507 gold_assert(os
->needs_symtab_index());
508 new_symndx
= os
->symtab_index();
518 const Symbol
* gsym
= object
->global_symbol(r_sym
);
519 gold_assert(gsym
!= NULL
);
520 if (gsym
->is_forwarder())
521 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
523 gold_assert(gsym
->has_symtab_index());
524 new_symndx
= gsym
->symtab_index();
527 // Get the new offset--the location in the output section where
528 // this relocation should be applied.
530 off_t offset
= reloc
.get_r_offset();
532 if (offset_in_output_section
!= -1)
533 new_offset
= offset
+ offset_in_output_section
;
536 new_offset
= output_section
->output_offset(object
,
539 gold_assert(new_offset
!= -1);
542 // In an object file, r_offset is an offset within the section.
543 // In an executable or dynamic object, generated by
544 // --emit-relocs, r_offset is an absolute address.
545 if (!parameters
->options().relocatable())
546 new_offset
+= view_address
;
548 reloc_write
.put_r_offset(new_offset
);
549 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
551 // Handle the reloc addend based on the strategy.
553 if (strategy
== Relocatable_relocs::RELOC_COPY
)
555 if (sh_type
== elfcpp::SHT_RELA
)
556 Reloc_types
<sh_type
, size
, big_endian
>::
557 copy_reloc_addend(&reloc_write
,
562 // The relocation uses a section symbol in the input file.
563 // We are adjusting it to use a section symbol in the output
564 // file. The input section symbol refers to some address in
565 // the input section. We need the relocation in the output
566 // file to refer to that same address. This adjustment to
567 // the addend is the same calculation we use for a simple
568 // absolute relocation for the input section symbol.
570 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
572 unsigned char* padd
= view
+ offset
;
575 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
577 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
578 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
579 get_reloc_addend(&reloc
);
580 addend
= psymval
->value(object
, addend
);
581 Reloc_types
<sh_type
, size
, big_endian
>::
582 set_reloc_addend(&reloc_write
, addend
);
586 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
589 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
590 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
594 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
595 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
599 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
600 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
604 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
605 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
614 pwrite
+= reloc_size
;
617 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
621 } // End namespace gold.
623 #endif // !defined(GOLD_TARGET_RELOC_H)