1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
24 #ifndef GOLD_TARGET_RELOC_H
25 #define GOLD_TARGET_RELOC_H
31 #include "reloc-types.h"
36 // This function implements the generic part of reloc scanning. The
37 // template parameter Scan must be a class type which provides two
38 // functions: local() and global(). Those functions implement the
39 // machine specific part of scanning. We do it this way to
40 // avoid making a function call for each relocation, and to avoid
41 // repeating the generic code for each target.
43 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
50 Sized_relobj_file
<size
, big_endian
>* object
,
51 unsigned int data_shndx
,
52 const unsigned char* prelocs
,
54 Output_section
* output_section
,
55 bool needs_special_offset_handling
,
57 const unsigned char* plocal_syms
)
59 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
60 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
61 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
64 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
66 Reltype
reloc(prelocs
);
68 if (needs_special_offset_handling
69 && !output_section
->is_input_address_mapped(object
, data_shndx
,
70 reloc
.get_r_offset()))
73 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
74 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
75 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
77 if (r_sym
< local_count
)
79 gold_assert(plocal_syms
!= NULL
);
80 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
82 unsigned int shndx
= lsym
.get_st_shndx();
84 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
85 // If RELOC is a relocation against a local symbol in a
86 // section we are discarding then we can ignore it. It will
87 // eventually become a reloc against the value zero.
89 // FIXME: We should issue a warning if this is an
90 // allocated section; is this the best place to do it?
92 // FIXME: The old GNU linker would in some cases look
93 // for the linkonce section which caused this section to
94 // be discarded, and, if the other section was the same
95 // size, change the reloc to refer to the other section.
96 // That seems risky and weird to me, and I don't know of
97 // any case where it is actually required.
98 bool is_discarded
= (is_ordinary
99 && shndx
!= elfcpp::SHN_UNDEF
100 && !object
->is_section_included(shndx
)
101 && !symtab
->is_section_folded(object
, shndx
));
102 scan
.local(symtab
, layout
, target
, object
, data_shndx
,
103 output_section
, reloc
, r_type
, lsym
, is_discarded
);
107 Symbol
* gsym
= object
->global_symbol(r_sym
);
108 gold_assert(gsym
!= NULL
);
109 if (gsym
->is_forwarder())
110 gsym
= symtab
->resolve_forwards(gsym
);
112 scan
.global(symtab
, layout
, target
, object
, data_shndx
,
113 output_section
, reloc
, r_type
, gsym
);
118 // Behavior for relocations to discarded comdat sections.
122 CB_UNDETERMINED
, // Not yet determined -- need to look at section name.
123 CB_PRETEND
, // Attempt to map to the corresponding kept section.
124 CB_IGNORE
, // Ignore the relocation.
125 CB_WARNING
// Print a warning.
128 class Default_comdat_behavior
131 // Decide what the linker should do for relocations that refer to
132 // discarded comdat sections. This decision is based on the name of
133 // the section being relocated.
135 inline Comdat_behavior
136 get(const char* name
)
138 if (Layout::is_debug_info_section(name
))
140 if (strcmp(name
, ".eh_frame") == 0
141 || 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 // Return true if we are should issue an error saying that SYM is an
173 // undefined symbol. This is called if there is a relocation against
177 issue_undefined_symbol_error(const Symbol
* sym
)
179 // We only report global symbols.
183 // We only report undefined symbols.
184 if (!sym
->is_undefined() && !sym
->is_placeholder())
187 // We don't report weak symbols.
188 if (sym
->binding() == elfcpp::STB_WEAK
)
191 // We don't report symbols defined in discarded sections.
192 if (sym
->is_defined_in_discarded_section())
195 // If the target defines this symbol, don't report it here.
196 if (parameters
->target().is_defined_by_abi(sym
))
199 // See if we've been told to ignore whether this symbol is
201 const char* const u
= parameters
->options().unresolved_symbols();
204 if (strcmp(u
, "ignore-all") == 0)
206 if (strcmp(u
, "report-all") == 0)
208 if (strcmp(u
, "ignore-in-object-files") == 0 && !sym
->in_dyn())
210 if (strcmp(u
, "ignore-in-shared-libs") == 0 && !sym
->in_reg())
214 // When creating a shared library, only report unresolved symbols if
216 if (parameters
->options().shared() && !parameters
->options().defs())
219 // Otherwise issue a warning.
223 // This function implements the generic part of relocation processing.
224 // The template parameter Relocate must be a class type which provides
225 // a single function, relocate(), which implements the machine
226 // specific part of a relocation.
228 // The template parameter Relocate_comdat_behavior is a class type
229 // which provides a single function, get(), which determines what the
230 // linker should do for relocations that refer to discarded comdat
233 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
234 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
235 // RELOCATE implements operator() to do a relocation.
237 // PRELOCS points to the relocation data. RELOC_COUNT is the number
238 // of relocs. OUTPUT_SECTION is the output section.
239 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
240 // mapped to output offsets.
242 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
243 // VIEW_SIZE is the size. These refer to the input section, unless
244 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
245 // the output section.
247 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
248 // not NULL, it is a vector indexed by relocation index. If that
249 // entry is not NULL, it points to a global symbol which used as the
250 // symbol for the relocation, ignoring the symbol index in the
253 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
255 typename Relocate_comdat_behavior
>
258 const Relocate_info
<size
, big_endian
>* relinfo
,
260 const unsigned char* prelocs
,
262 Output_section
* output_section
,
263 bool needs_special_offset_handling
,
265 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
266 section_size_type view_size
,
267 const Reloc_symbol_changes
* reloc_symbol_changes
)
269 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
270 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
272 Relocate_comdat_behavior relocate_comdat_behavior
;
274 Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
275 unsigned int local_count
= object
->local_symbol_count();
277 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
279 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
281 Reltype
reloc(prelocs
);
283 section_offset_type offset
=
284 convert_to_section_size_type(reloc
.get_r_offset());
286 if (needs_special_offset_handling
)
288 offset
= output_section
->output_offset(relinfo
->object
,
295 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
296 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
297 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
299 const Sized_symbol
<size
>* sym
;
301 Symbol_value
<size
> symval
;
302 const Symbol_value
<size
> *psymval
;
303 bool is_defined_in_discarded_section
;
305 if (r_sym
< local_count
306 && (reloc_symbol_changes
== NULL
307 || (*reloc_symbol_changes
)[i
] == NULL
))
310 psymval
= object
->local_symbol(r_sym
);
312 // If the local symbol belongs to a section we are discarding,
313 // and that section is a debug section, try to find the
314 // corresponding kept section and map this symbol to its
315 // counterpart in the kept section. The symbol must not
316 // correspond to a section we are folding.
318 shndx
= psymval
->input_shndx(&is_ordinary
);
319 is_defined_in_discarded_section
=
321 && shndx
!= elfcpp::SHN_UNDEF
322 && !object
->is_section_included(shndx
)
323 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
328 if (reloc_symbol_changes
!= NULL
329 && (*reloc_symbol_changes
)[i
] != NULL
)
330 gsym
= (*reloc_symbol_changes
)[i
];
333 gsym
= object
->global_symbol(r_sym
);
334 gold_assert(gsym
!= NULL
);
335 if (gsym
->is_forwarder())
336 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
339 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
340 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
341 symval
.set_output_symtab_index(sym
->symtab_index());
343 symval
.set_no_output_symtab_entry();
344 symval
.set_output_value(sym
->value());
345 if (gsym
->type() == elfcpp::STT_TLS
)
346 symval
.set_is_tls_symbol();
347 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
348 symval
.set_is_ifunc_symbol();
351 is_defined_in_discarded_section
=
352 (gsym
->is_defined_in_discarded_section()
353 && gsym
->is_undefined());
357 Symbol_value
<size
> symval2
;
358 if (is_defined_in_discarded_section
)
360 if (comdat_behavior
== CB_UNDETERMINED
)
362 std::string name
= object
->section_name(relinfo
->data_shndx
);
363 comdat_behavior
= relocate_comdat_behavior
.get(name
.c_str());
365 if (comdat_behavior
== CB_PRETEND
)
367 // FIXME: This case does not work for global symbols.
368 // We have no place to store the original section index.
369 // Fortunately this does not matter for comdat sections,
370 // only for sections explicitly discarded by a linker
373 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
374 object
->map_to_kept_section(shndx
, &found
);
376 symval2
.set_output_value(value
+ psymval
->input_value());
378 symval2
.set_output_value(0);
382 if (comdat_behavior
== CB_WARNING
)
383 gold_warning_at_location(relinfo
, i
, offset
,
384 _("relocation refers to discarded "
386 symval2
.set_output_value(0);
388 symval2
.set_no_output_symtab_entry();
392 if (!relocate
.relocate(relinfo
, target
, output_section
, i
, reloc
,
393 r_type
, sym
, psymval
, view
+ offset
,
394 view_address
+ offset
, view_size
))
397 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
399 gold_error_at_location(relinfo
, i
, offset
,
400 _("reloc has bad offset %zu"),
401 static_cast<size_t>(offset
));
405 if (issue_undefined_symbol_error(sym
))
406 gold_undefined_symbol_at_location(sym
, relinfo
, i
, offset
);
408 && sym
->visibility() != elfcpp::STV_DEFAULT
409 && (sym
->is_undefined() || sym
->is_from_dynobj()))
410 visibility_error(sym
);
412 if (sym
!= NULL
&& sym
->has_warning())
413 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
417 // Apply an incremental relocation.
419 template<int size
, bool big_endian
, typename Target_type
,
422 apply_relocation(const Relocate_info
<size
, big_endian
>* relinfo
,
424 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
426 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
429 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
430 section_size_type view_size
)
432 // Construct the ELF relocation in a temporary buffer.
433 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
434 unsigned char relbuf
[reloc_size
];
435 elfcpp::Rela
<size
, big_endian
> rel(relbuf
);
436 elfcpp::Rela_write
<size
, big_endian
> orel(relbuf
);
437 orel
.put_r_offset(r_offset
);
438 orel
.put_r_info(elfcpp::elf_r_info
<size
>(0, r_type
));
439 orel
.put_r_addend(r_addend
);
441 // Setup a Symbol_value for the global symbol.
442 const Sized_symbol
<size
>* sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
443 Symbol_value
<size
> symval
;
444 gold_assert(sym
->has_symtab_index() && sym
->symtab_index() != -1U);
445 symval
.set_output_symtab_index(sym
->symtab_index());
446 symval
.set_output_value(sym
->value());
447 if (gsym
->type() == elfcpp::STT_TLS
)
448 symval
.set_is_tls_symbol();
449 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
450 symval
.set_is_ifunc_symbol();
453 relocate
.relocate(relinfo
, target
, NULL
, -1U, rel
, r_type
, sym
, &symval
,
454 view
+ r_offset
, address
+ r_offset
, view_size
);
457 // This class may be used as a typical class for the
458 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
459 // template parameter Classify_reloc must be a class type which
460 // provides a function get_size_for_reloc which returns the number of
461 // bytes to which a reloc applies. This class is intended to capture
462 // the most typical target behaviour, while still permitting targets
463 // to define their own independent class for Scan_relocatable_reloc.
465 template<int sh_type
, typename Classify_reloc
>
466 class Default_scan_relocatable_relocs
469 // Return the strategy to use for a local symbol which is not a
470 // section symbol, given the relocation type.
471 inline Relocatable_relocs::Reloc_strategy
472 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
474 // We assume that relocation type 0 is NONE. Targets which are
475 // different must override.
476 if (r_type
== 0 && r_sym
== 0)
477 return Relocatable_relocs::RELOC_DISCARD
;
478 return Relocatable_relocs::RELOC_COPY
;
481 // Return the strategy to use for a local symbol which is a section
482 // symbol, given the relocation type.
483 inline Relocatable_relocs::Reloc_strategy
484 local_section_strategy(unsigned int r_type
, Relobj
* object
)
486 if (sh_type
== elfcpp::SHT_RELA
)
487 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
490 Classify_reloc classify
;
491 switch (classify
.get_size_for_reloc(r_type
, object
))
494 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
496 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
498 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
500 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
502 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
509 // Return the strategy to use for a global symbol, given the
510 // relocation type, the object, and the symbol index.
511 inline Relocatable_relocs::Reloc_strategy
512 global_strategy(unsigned int, Relobj
*, unsigned int)
513 { return Relocatable_relocs::RELOC_COPY
; }
516 // Scan relocs during a relocatable link. This is a default
517 // definition which should work for most targets.
518 // Scan_relocatable_reloc must name a class type which provides three
519 // functions which return a Relocatable_relocs::Reloc_strategy code:
520 // global_strategy, local_non_section_strategy, and
521 // local_section_strategy. Most targets should be able to use
522 // Default_scan_relocatable_relocs as this class.
524 template<int size
, bool big_endian
, int sh_type
,
525 typename Scan_relocatable_reloc
>
527 scan_relocatable_relocs(
530 Sized_relobj_file
<size
, big_endian
>* object
,
531 unsigned int data_shndx
,
532 const unsigned char* prelocs
,
534 Output_section
* output_section
,
535 bool needs_special_offset_handling
,
536 size_t local_symbol_count
,
537 const unsigned char* plocal_syms
,
538 Relocatable_relocs
* rr
)
540 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
541 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
542 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
543 Scan_relocatable_reloc scan
;
545 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
547 Reltype
reloc(prelocs
);
549 Relocatable_relocs::Reloc_strategy strategy
;
551 if (needs_special_offset_handling
552 && !output_section
->is_input_address_mapped(object
, data_shndx
,
553 reloc
.get_r_offset()))
554 strategy
= Relocatable_relocs::RELOC_DISCARD
;
557 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
559 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
560 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
562 if (r_sym
>= local_symbol_count
)
563 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
566 gold_assert(plocal_syms
!= NULL
);
567 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
569 unsigned int shndx
= lsym
.get_st_shndx();
571 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
573 && shndx
!= elfcpp::SHN_UNDEF
574 && !object
->is_section_included(shndx
))
576 // RELOC is a relocation against a local symbol
577 // defined in a section we are discarding. Discard
578 // the reloc. FIXME: Should we issue a warning?
579 strategy
= Relocatable_relocs::RELOC_DISCARD
;
581 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
582 strategy
= scan
.local_non_section_strategy(r_type
, object
,
586 strategy
= scan
.local_section_strategy(r_type
, object
);
587 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
588 object
->output_section(shndx
)->set_needs_symtab_index();
591 if (strategy
== Relocatable_relocs::RELOC_COPY
)
592 object
->set_must_have_output_symtab_entry(r_sym
);
596 rr
->set_next_reloc_strategy(strategy
);
600 // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
601 // This is a default definition which should work for most targets.
603 template<int size
, bool big_endian
, int sh_type
>
606 const Relocate_info
<size
, big_endian
>* relinfo
,
607 const unsigned char* prelocs
,
609 Output_section
* output_section
,
610 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
611 const Relocatable_relocs
* rr
,
613 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
614 section_size_type view_size
,
615 unsigned char* reloc_view
,
616 section_size_type reloc_view_size
)
618 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
619 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
620 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
622 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
623 const Address invalid_address
= static_cast<Address
>(0) - 1;
625 Sized_relobj_file
<size
, big_endian
>* const object
= relinfo
->object
;
626 const unsigned int local_count
= object
->local_symbol_count();
628 unsigned char* pwrite
= reloc_view
;
630 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
632 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
633 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
636 if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
638 // Target wants to handle this relocation.
639 Sized_target
<size
, big_endian
>* target
=
640 parameters
->sized_target
<size
, big_endian
>();
641 target
->relocate_special_relocatable(relinfo
, sh_type
, prelocs
,
643 offset_in_output_section
,
646 pwrite
+= reloc_size
;
649 Reltype
reloc(prelocs
);
650 Reltype_write
reloc_write(pwrite
);
652 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
653 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
654 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
656 // Get the new symbol index.
658 unsigned int new_symndx
;
659 if (r_sym
< local_count
)
663 case Relocatable_relocs::RELOC_COPY
:
668 new_symndx
= object
->symtab_index(r_sym
);
669 gold_assert(new_symndx
!= -1U);
673 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
674 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
675 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
676 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
677 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
678 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
679 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
681 // We are adjusting a section symbol. We need to find
682 // the symbol table index of the section symbol for
683 // the output section corresponding to input section
684 // in which this symbol is defined.
685 gold_assert(r_sym
< local_count
);
688 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
689 gold_assert(is_ordinary
);
690 Output_section
* os
= object
->output_section(shndx
);
691 gold_assert(os
!= NULL
);
692 gold_assert(os
->needs_symtab_index());
693 new_symndx
= os
->symtab_index();
703 const Symbol
* gsym
= object
->global_symbol(r_sym
);
704 gold_assert(gsym
!= NULL
);
705 if (gsym
->is_forwarder())
706 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
708 gold_assert(gsym
->has_symtab_index());
709 new_symndx
= gsym
->symtab_index();
712 // Get the new offset--the location in the output section where
713 // this relocation should be applied.
715 Address offset
= reloc
.get_r_offset();
717 if (offset_in_output_section
!= invalid_address
)
718 new_offset
= offset
+ offset_in_output_section
;
721 section_offset_type sot_offset
=
722 convert_types
<section_offset_type
, Address
>(offset
);
723 section_offset_type new_sot_offset
=
724 output_section
->output_offset(object
, relinfo
->data_shndx
,
726 gold_assert(new_sot_offset
!= -1);
727 new_offset
= new_sot_offset
;
730 // In an object file, r_offset is an offset within the section.
731 // In an executable or dynamic object, generated by
732 // --emit-relocs, r_offset is an absolute address.
733 if (!parameters
->options().relocatable())
735 new_offset
+= view_address
;
736 if (offset_in_output_section
!= invalid_address
)
737 new_offset
-= offset_in_output_section
;
740 reloc_write
.put_r_offset(new_offset
);
741 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
743 // Handle the reloc addend based on the strategy.
745 if (strategy
== Relocatable_relocs::RELOC_COPY
)
747 if (sh_type
== elfcpp::SHT_RELA
)
748 Reloc_types
<sh_type
, size
, big_endian
>::
749 copy_reloc_addend(&reloc_write
,
754 // The relocation uses a section symbol in the input file.
755 // We are adjusting it to use a section symbol in the output
756 // file. The input section symbol refers to some address in
757 // the input section. We need the relocation in the output
758 // file to refer to that same address. This adjustment to
759 // the addend is the same calculation we use for a simple
760 // absolute relocation for the input section symbol.
762 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
764 unsigned char* padd
= view
+ offset
;
767 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
769 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
770 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
771 get_reloc_addend(&reloc
);
772 addend
= psymval
->value(object
, addend
);
773 Reloc_types
<sh_type
, size
, big_endian
>::
774 set_reloc_addend(&reloc_write
, addend
);
778 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
781 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
782 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
786 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
787 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
791 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
792 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
796 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
797 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
801 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
802 Relocate_functions
<size
, big_endian
>::rel32_unaligned(padd
,
812 pwrite
+= reloc_size
;
815 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
819 } // End namespace gold.
821 #endif // !defined(GOLD_TARGET_RELOC_H)