1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 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
30 #include "reloc-types.h"
35 // This function implements the generic part of reloc scanning. The
36 // template parameter Scan must be a class type which provides two
37 // functions: local() and global(). Those functions implement the
38 // machine specific part of scanning. We do it this way to
39 // avoid making a function call for each relocation, and to avoid
40 // repeating the generic code for each target.
42 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
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
)
87 && !symtab
->is_section_folded(object
, shndx
))
89 // RELOC is a relocation against a local symbol in a
90 // section we are discarding. We can ignore this
91 // relocation. It will eventually become a reloc
92 // against the value zero.
94 // FIXME: We should issue a warning if this is an
95 // allocated section; is this the best place to do it?
97 // FIXME: The old GNU linker would in some cases look
98 // for the linkonce section which caused this section to
99 // be discarded, and, if the other section was the same
100 // size, change the reloc to refer to the other section.
101 // That seems risky and weird to me, and I don't know of
102 // any case where it is actually required.
106 scan
.local(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(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 // Give an error for a symbol with non-default visibility which is not
151 visibility_error(const Symbol
* sym
)
154 switch (sym
->visibility())
156 case elfcpp::STV_INTERNAL
:
159 case elfcpp::STV_HIDDEN
:
162 case elfcpp::STV_PROTECTED
:
168 gold_error(_("%s symbol '%s' is not defined locally"),
172 // This function implements the generic part of relocation processing.
173 // The template parameter Relocate must be a class type which provides
174 // a single function, relocate(), which implements the machine
175 // specific part of a relocation.
177 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
178 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
179 // RELOCATE implements operator() to do a relocation.
181 // PRELOCS points to the relocation data. RELOC_COUNT is the number
182 // of relocs. OUTPUT_SECTION is the output section.
183 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
184 // mapped to output offsets.
186 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
187 // VIEW_SIZE is the size. These refer to the input section, unless
188 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
189 // the output section.
191 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
192 // not NULL, it is a vector indexed by relocation index. If that
193 // entry is not NULL, it points to a global symbol which used as the
194 // symbol for the relocation, ignoring the symbol index in the
197 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
201 const Relocate_info
<size
, big_endian
>* relinfo
,
203 const unsigned char* prelocs
,
205 Output_section
* output_section
,
206 bool needs_special_offset_handling
,
208 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
209 section_size_type view_size
,
210 const Reloc_symbol_changes
* reloc_symbol_changes
)
212 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
213 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
216 Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
217 unsigned int local_count
= object
->local_symbol_count();
219 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
221 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
223 Reltype
reloc(prelocs
);
225 section_offset_type offset
=
226 convert_to_section_size_type(reloc
.get_r_offset());
228 if (needs_special_offset_handling
)
230 offset
= output_section
->output_offset(relinfo
->object
,
237 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
238 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
239 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
241 const Sized_symbol
<size
>* sym
;
243 Symbol_value
<size
> symval
;
244 const Symbol_value
<size
> *psymval
;
245 bool is_defined_in_discarded_section
;
247 if (r_sym
< local_count
248 && (reloc_symbol_changes
== NULL
249 || (*reloc_symbol_changes
)[i
] == NULL
))
252 psymval
= object
->local_symbol(r_sym
);
254 // If the local symbol belongs to a section we are discarding,
255 // and that section is a debug section, try to find the
256 // corresponding kept section and map this symbol to its
257 // counterpart in the kept section. The symbol must not
258 // correspond to a section we are folding.
260 shndx
= psymval
->input_shndx(&is_ordinary
);
261 is_defined_in_discarded_section
=
263 && shndx
!= elfcpp::SHN_UNDEF
264 && !object
->is_section_included(shndx
)
265 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
270 if (reloc_symbol_changes
!= NULL
271 && (*reloc_symbol_changes
)[i
] != NULL
)
272 gsym
= (*reloc_symbol_changes
)[i
];
275 gsym
= object
->global_symbol(r_sym
);
276 gold_assert(gsym
!= NULL
);
277 if (gsym
->is_forwarder())
278 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
281 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
282 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
283 symval
.set_output_symtab_index(sym
->symtab_index());
285 symval
.set_no_output_symtab_entry();
286 symval
.set_output_value(sym
->value());
287 if (gsym
->type() == elfcpp::STT_TLS
)
288 symval
.set_is_tls_symbol();
289 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
290 symval
.set_is_ifunc_symbol();
293 is_defined_in_discarded_section
=
294 (gsym
->is_defined_in_discarded_section()
295 && gsym
->is_undefined());
299 Symbol_value
<size
> symval2
;
300 if (is_defined_in_discarded_section
)
302 if (comdat_behavior
== CB_UNDETERMINED
)
304 std::string name
= object
->section_name(relinfo
->data_shndx
);
305 comdat_behavior
= get_comdat_behavior(name
.c_str());
307 if (comdat_behavior
== CB_PRETEND
)
309 // FIXME: This case does not work for global symbols.
310 // We have no place to store the original section index.
311 // Fortunately this does not matter for comdat sections,
312 // only for sections explicitly discarded by a linker
315 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
316 object
->map_to_kept_section(shndx
, &found
);
318 symval2
.set_output_value(value
+ psymval
->input_value());
320 symval2
.set_output_value(0);
324 if (comdat_behavior
== CB_WARNING
)
325 gold_warning_at_location(relinfo
, i
, offset
,
326 _("relocation refers to discarded "
328 symval2
.set_output_value(0);
330 symval2
.set_no_output_symtab_entry();
334 if (!relocate
.relocate(relinfo
, target
, output_section
, i
, reloc
,
335 r_type
, sym
, psymval
, view
+ offset
,
336 view_address
+ offset
, view_size
))
339 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
341 gold_error_at_location(relinfo
, i
, offset
,
342 _("reloc has bad offset %zu"),
343 static_cast<size_t>(offset
));
348 && (sym
->is_undefined() || sym
->is_placeholder())
349 && sym
->binding() != elfcpp::STB_WEAK
350 && !is_defined_in_discarded_section
351 && !target
->is_defined_by_abi(sym
)
352 && (!parameters
->options().shared() // -shared
353 || parameters
->options().defs())) // -z defs
354 gold_undefined_symbol_at_location(sym
, relinfo
, i
, offset
);
356 && sym
->visibility() != elfcpp::STV_DEFAULT
357 && (sym
->is_undefined() || sym
->is_from_dynobj()))
358 visibility_error(sym
);
360 if (sym
!= NULL
&& sym
->has_warning())
361 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
365 // This class may be used as a typical class for the
366 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
367 // template parameter Classify_reloc must be a class type which
368 // provides a function get_size_for_reloc which returns the number of
369 // bytes to which a reloc applies. This class is intended to capture
370 // the most typical target behaviour, while still permitting targets
371 // to define their own independent class for Scan_relocatable_reloc.
373 template<int sh_type
, typename Classify_reloc
>
374 class Default_scan_relocatable_relocs
377 // Return the strategy to use for a local symbol which is not a
378 // section symbol, given the relocation type.
379 inline Relocatable_relocs::Reloc_strategy
380 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
382 // We assume that relocation type 0 is NONE. Targets which are
383 // different must override.
384 if (r_type
== 0 && r_sym
== 0)
385 return Relocatable_relocs::RELOC_DISCARD
;
386 return Relocatable_relocs::RELOC_COPY
;
389 // Return the strategy to use for a local symbol which is a section
390 // symbol, given the relocation type.
391 inline Relocatable_relocs::Reloc_strategy
392 local_section_strategy(unsigned int r_type
, Relobj
* object
)
394 if (sh_type
== elfcpp::SHT_RELA
)
395 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
398 Classify_reloc classify
;
399 switch (classify
.get_size_for_reloc(r_type
, object
))
402 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
404 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
406 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
408 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
410 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
417 // Return the strategy to use for a global symbol, given the
418 // relocation type, the object, and the symbol index.
419 inline Relocatable_relocs::Reloc_strategy
420 global_strategy(unsigned int, Relobj
*, unsigned int)
421 { return Relocatable_relocs::RELOC_COPY
; }
424 // Scan relocs during a relocatable link. This is a default
425 // definition which should work for most targets.
426 // Scan_relocatable_reloc must name a class type which provides three
427 // functions which return a Relocatable_relocs::Reloc_strategy code:
428 // global_strategy, local_non_section_strategy, and
429 // local_section_strategy. Most targets should be able to use
430 // Default_scan_relocatable_relocs as this class.
432 template<int size
, bool big_endian
, int sh_type
,
433 typename Scan_relocatable_reloc
>
435 scan_relocatable_relocs(
438 Sized_relobj
<size
, big_endian
>* object
,
439 unsigned int data_shndx
,
440 const unsigned char* prelocs
,
442 Output_section
* output_section
,
443 bool needs_special_offset_handling
,
444 size_t local_symbol_count
,
445 const unsigned char* plocal_syms
,
446 Relocatable_relocs
* rr
)
448 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
449 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
450 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
451 Scan_relocatable_reloc scan
;
453 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
455 Reltype
reloc(prelocs
);
457 Relocatable_relocs::Reloc_strategy strategy
;
459 if (needs_special_offset_handling
460 && !output_section
->is_input_address_mapped(object
, data_shndx
,
461 reloc
.get_r_offset()))
462 strategy
= Relocatable_relocs::RELOC_DISCARD
;
465 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
467 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
468 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
470 if (r_sym
>= local_symbol_count
)
471 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
474 gold_assert(plocal_syms
!= NULL
);
475 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
477 unsigned int shndx
= lsym
.get_st_shndx();
479 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
481 && shndx
!= elfcpp::SHN_UNDEF
482 && !object
->is_section_included(shndx
))
484 // RELOC is a relocation against a local symbol
485 // defined in a section we are discarding. Discard
486 // the reloc. FIXME: Should we issue a warning?
487 strategy
= Relocatable_relocs::RELOC_DISCARD
;
489 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
490 strategy
= scan
.local_non_section_strategy(r_type
, object
,
494 strategy
= scan
.local_section_strategy(r_type
, object
);
495 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
496 object
->output_section(shndx
)->set_needs_symtab_index();
499 if (strategy
== Relocatable_relocs::RELOC_COPY
)
500 object
->set_must_have_output_symtab_entry(r_sym
);
504 rr
->set_next_reloc_strategy(strategy
);
508 // Relocate relocs during a relocatable link. This is a default
509 // definition which should work for most targets.
511 template<int size
, bool big_endian
, int sh_type
>
513 relocate_for_relocatable(
514 const Relocate_info
<size
, big_endian
>* relinfo
,
515 const unsigned char* prelocs
,
517 Output_section
* output_section
,
518 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
519 const Relocatable_relocs
* rr
,
521 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
522 section_size_type view_size
,
523 unsigned char* reloc_view
,
524 section_size_type reloc_view_size
)
526 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
527 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
528 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
530 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
531 const Address invalid_address
= static_cast<Address
>(0) - 1;
533 Sized_relobj
<size
, big_endian
>* const object
= relinfo
->object
;
534 const unsigned int local_count
= object
->local_symbol_count();
536 unsigned char* pwrite
= reloc_view
;
538 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
540 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
541 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
544 if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
546 // Target wants to handle this relocation.
547 Sized_target
<size
, big_endian
>* target
=
548 parameters
->sized_target
<size
, big_endian
>();
549 target
->relocate_special_relocatable(relinfo
, sh_type
, prelocs
,
551 offset_in_output_section
,
554 pwrite
+= reloc_size
;
557 Reltype
reloc(prelocs
);
558 Reltype_write
reloc_write(pwrite
);
560 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
561 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
562 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
564 // Get the new symbol index.
566 unsigned int new_symndx
;
567 if (r_sym
< local_count
)
571 case Relocatable_relocs::RELOC_COPY
:
576 new_symndx
= object
->symtab_index(r_sym
);
577 gold_assert(new_symndx
!= -1U);
581 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
582 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
583 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
584 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
585 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
586 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
588 // We are adjusting a section symbol. We need to find
589 // the symbol table index of the section symbol for
590 // the output section corresponding to input section
591 // in which this symbol is defined.
592 gold_assert(r_sym
< local_count
);
595 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
596 gold_assert(is_ordinary
);
597 Output_section
* os
= object
->output_section(shndx
);
598 gold_assert(os
!= NULL
);
599 gold_assert(os
->needs_symtab_index());
600 new_symndx
= os
->symtab_index();
610 const Symbol
* gsym
= object
->global_symbol(r_sym
);
611 gold_assert(gsym
!= NULL
);
612 if (gsym
->is_forwarder())
613 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
615 gold_assert(gsym
->has_symtab_index());
616 new_symndx
= gsym
->symtab_index();
619 // Get the new offset--the location in the output section where
620 // this relocation should be applied.
622 Address offset
= reloc
.get_r_offset();
624 if (offset_in_output_section
!= invalid_address
)
625 new_offset
= offset
+ offset_in_output_section
;
628 section_offset_type sot_offset
=
629 convert_types
<section_offset_type
, Address
>(offset
);
630 section_offset_type new_sot_offset
=
631 output_section
->output_offset(object
, relinfo
->data_shndx
,
633 gold_assert(new_sot_offset
!= -1);
634 new_offset
= new_sot_offset
;
637 // In an object file, r_offset is an offset within the section.
638 // In an executable or dynamic object, generated by
639 // --emit-relocs, r_offset is an absolute address.
640 if (!parameters
->options().relocatable())
642 new_offset
+= view_address
;
643 if (offset_in_output_section
!= invalid_address
)
644 new_offset
-= offset_in_output_section
;
647 reloc_write
.put_r_offset(new_offset
);
648 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
650 // Handle the reloc addend based on the strategy.
652 if (strategy
== Relocatable_relocs::RELOC_COPY
)
654 if (sh_type
== elfcpp::SHT_RELA
)
655 Reloc_types
<sh_type
, size
, big_endian
>::
656 copy_reloc_addend(&reloc_write
,
661 // The relocation uses a section symbol in the input file.
662 // We are adjusting it to use a section symbol in the output
663 // file. The input section symbol refers to some address in
664 // the input section. We need the relocation in the output
665 // file to refer to that same address. This adjustment to
666 // the addend is the same calculation we use for a simple
667 // absolute relocation for the input section symbol.
669 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
671 unsigned char* padd
= view
+ offset
;
674 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
676 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
677 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
678 get_reloc_addend(&reloc
);
679 addend
= psymval
->value(object
, addend
);
680 Reloc_types
<sh_type
, size
, big_endian
>::
681 set_reloc_addend(&reloc_write
, addend
);
685 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
688 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
689 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
693 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
694 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
698 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
699 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
703 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
704 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
713 pwrite
+= reloc_size
;
716 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
720 } // End namespace gold.
722 #endif // !defined(GOLD_TARGET_RELOC_H)