1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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_file
<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
);
84 // If RELOC is a relocation against a local symbol in a
85 // section we are discarding then we can ignore it. It will
86 // eventually become a reloc against the value zero.
88 // FIXME: We should issue a warning if this is an
89 // allocated section; is this the best place to do it?
91 // FIXME: The old GNU linker would in some cases look
92 // for the linkonce section which caused this section to
93 // be discarded, and, if the other section was the same
94 // size, change the reloc to refer to the other section.
95 // That seems risky and weird to me, and I don't know of
96 // any case where it is actually required.
97 bool is_discarded
= (is_ordinary
98 && shndx
!= elfcpp::SHN_UNDEF
99 && !object
->is_section_included(shndx
)
100 && !symtab
->is_section_folded(object
, shndx
));
101 scan
.local(symtab
, layout
, target
, object
, data_shndx
,
102 output_section
, reloc
, r_type
, lsym
, is_discarded
);
106 Symbol
* gsym
= object
->global_symbol(r_sym
);
107 gold_assert(gsym
!= NULL
);
108 if (gsym
->is_forwarder())
109 gsym
= symtab
->resolve_forwards(gsym
);
111 scan
.global(symtab
, layout
, target
, object
, data_shndx
,
112 output_section
, reloc
, r_type
, gsym
);
117 // Behavior for relocations to discarded comdat sections.
121 CB_UNDETERMINED
, // Not yet determined -- need to look at section name.
122 CB_PRETEND
, // Attempt to map to the corresponding kept section.
123 CB_IGNORE
, // Ignore the relocation.
124 CB_WARNING
// Print a warning.
127 // Decide what the linker should do for relocations that refer to discarded
128 // comdat sections. This decision is based on the name of the section being
131 inline Comdat_behavior
132 get_comdat_behavior(const char* name
)
134 if (Layout::is_debug_info_section(name
))
136 if (strcmp(name
, ".eh_frame") == 0
137 || strcmp(name
, ".gcc_except_table") == 0)
142 // Give an error for a symbol with non-default visibility which is not
146 visibility_error(const Symbol
* sym
)
149 switch (sym
->visibility())
151 case elfcpp::STV_INTERNAL
:
154 case elfcpp::STV_HIDDEN
:
157 case elfcpp::STV_PROTECTED
:
163 gold_error(_("%s symbol '%s' is not defined locally"),
167 // Return true if we are should issue an error saying that SYM is an
168 // undefined symbol. This is called if there is a relocation against
172 issue_undefined_symbol_error(const Symbol
* sym
)
174 // We only report global symbols.
178 // We only report undefined symbols.
179 if (!sym
->is_undefined() && !sym
->is_placeholder())
182 // We don't report weak symbols.
183 if (sym
->binding() == elfcpp::STB_WEAK
)
186 // We don't report symbols defined in discarded sections.
187 if (sym
->is_defined_in_discarded_section())
190 // If the target defines this symbol, don't report it here.
191 if (parameters
->target().is_defined_by_abi(sym
))
194 // See if we've been told to ignore whether this symbol is
196 const char* const u
= parameters
->options().unresolved_symbols();
199 if (strcmp(u
, "ignore-all") == 0)
201 if (strcmp(u
, "report-all") == 0)
203 if (strcmp(u
, "ignore-in-object-files") == 0 && !sym
->in_dyn())
205 if (strcmp(u
, "ignore-in-shared-libs") == 0 && !sym
->in_reg())
209 // When creating a shared library, only report unresolved symbols if
211 if (parameters
->options().shared() && !parameters
->options().defs())
214 // Otherwise issue a warning.
218 // This function implements the generic part of relocation processing.
219 // The template parameter Relocate must be a class type which provides
220 // a single function, relocate(), which implements the machine
221 // specific part of a relocation.
223 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
224 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
225 // RELOCATE implements operator() to do a relocation.
227 // PRELOCS points to the relocation data. RELOC_COUNT is the number
228 // of relocs. OUTPUT_SECTION is the output section.
229 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
230 // mapped to output offsets.
232 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
233 // VIEW_SIZE is the size. These refer to the input section, unless
234 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
235 // the output section.
237 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
238 // not NULL, it is a vector indexed by relocation index. If that
239 // entry is not NULL, it points to a global symbol which used as the
240 // symbol for the relocation, ignoring the symbol index in the
243 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
247 const Relocate_info
<size
, big_endian
>* relinfo
,
249 const unsigned char* prelocs
,
251 Output_section
* output_section
,
252 bool needs_special_offset_handling
,
254 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
255 section_size_type view_size
,
256 const Reloc_symbol_changes
* reloc_symbol_changes
)
258 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
259 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
262 Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
263 unsigned int local_count
= object
->local_symbol_count();
265 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
267 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
269 Reltype
reloc(prelocs
);
271 section_offset_type offset
=
272 convert_to_section_size_type(reloc
.get_r_offset());
274 if (needs_special_offset_handling
)
276 offset
= output_section
->output_offset(relinfo
->object
,
283 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
284 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
285 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
287 const Sized_symbol
<size
>* sym
;
289 Symbol_value
<size
> symval
;
290 const Symbol_value
<size
> *psymval
;
291 bool is_defined_in_discarded_section
;
293 if (r_sym
< local_count
294 && (reloc_symbol_changes
== NULL
295 || (*reloc_symbol_changes
)[i
] == NULL
))
298 psymval
= object
->local_symbol(r_sym
);
300 // If the local symbol belongs to a section we are discarding,
301 // and that section is a debug section, try to find the
302 // corresponding kept section and map this symbol to its
303 // counterpart in the kept section. The symbol must not
304 // correspond to a section we are folding.
306 shndx
= psymval
->input_shndx(&is_ordinary
);
307 is_defined_in_discarded_section
=
309 && shndx
!= elfcpp::SHN_UNDEF
310 && !object
->is_section_included(shndx
)
311 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
316 if (reloc_symbol_changes
!= NULL
317 && (*reloc_symbol_changes
)[i
] != NULL
)
318 gsym
= (*reloc_symbol_changes
)[i
];
321 gsym
= object
->global_symbol(r_sym
);
322 gold_assert(gsym
!= NULL
);
323 if (gsym
->is_forwarder())
324 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
327 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
328 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
329 symval
.set_output_symtab_index(sym
->symtab_index());
331 symval
.set_no_output_symtab_entry();
332 symval
.set_output_value(sym
->value());
333 if (gsym
->type() == elfcpp::STT_TLS
)
334 symval
.set_is_tls_symbol();
335 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
336 symval
.set_is_ifunc_symbol();
339 is_defined_in_discarded_section
=
340 (gsym
->is_defined_in_discarded_section()
341 && gsym
->is_undefined());
345 Symbol_value
<size
> symval2
;
346 if (is_defined_in_discarded_section
)
348 if (comdat_behavior
== CB_UNDETERMINED
)
350 std::string name
= object
->section_name(relinfo
->data_shndx
);
351 comdat_behavior
= get_comdat_behavior(name
.c_str());
353 if (comdat_behavior
== CB_PRETEND
)
355 // FIXME: This case does not work for global symbols.
356 // We have no place to store the original section index.
357 // Fortunately this does not matter for comdat sections,
358 // only for sections explicitly discarded by a linker
361 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
362 object
->map_to_kept_section(shndx
, &found
);
364 symval2
.set_output_value(value
+ psymval
->input_value());
366 symval2
.set_output_value(0);
370 if (comdat_behavior
== CB_WARNING
)
371 gold_warning_at_location(relinfo
, i
, offset
,
372 _("relocation refers to discarded "
374 symval2
.set_output_value(0);
376 symval2
.set_no_output_symtab_entry();
380 if (!relocate
.relocate(relinfo
, target
, output_section
, i
, reloc
,
381 r_type
, sym
, psymval
, view
+ offset
,
382 view_address
+ offset
, view_size
))
385 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
387 gold_error_at_location(relinfo
, i
, offset
,
388 _("reloc has bad offset %zu"),
389 static_cast<size_t>(offset
));
393 if (issue_undefined_symbol_error(sym
))
394 gold_undefined_symbol_at_location(sym
, relinfo
, i
, offset
);
396 && sym
->visibility() != elfcpp::STV_DEFAULT
397 && (sym
->is_undefined() || sym
->is_from_dynobj()))
398 visibility_error(sym
);
400 if (sym
!= NULL
&& sym
->has_warning())
401 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
405 // Apply an incremental relocation.
407 template<int size
, bool big_endian
, typename Target_type
,
410 apply_relocation(const Relocate_info
<size
, big_endian
>* relinfo
,
412 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
414 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
417 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
418 section_size_type view_size
)
420 // Construct the ELF relocation in a temporary buffer.
421 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
422 unsigned char relbuf
[reloc_size
];
423 elfcpp::Rela
<size
, big_endian
> rel(relbuf
);
424 elfcpp::Rela_write
<size
, big_endian
> orel(relbuf
);
425 orel
.put_r_offset(r_offset
);
426 orel
.put_r_info(elfcpp::elf_r_info
<size
>(0, r_type
));
427 orel
.put_r_addend(r_addend
);
429 // Setup a Symbol_value for the global symbol.
430 const Sized_symbol
<size
>* sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
431 Symbol_value
<size
> symval
;
432 gold_assert(sym
->has_symtab_index() && sym
->symtab_index() != -1U);
433 symval
.set_output_symtab_index(sym
->symtab_index());
434 symval
.set_output_value(sym
->value());
435 if (gsym
->type() == elfcpp::STT_TLS
)
436 symval
.set_is_tls_symbol();
437 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
438 symval
.set_is_ifunc_symbol();
441 relocate
.relocate(relinfo
, target
, NULL
, -1U, rel
, r_type
, sym
, &symval
,
442 view
+ r_offset
, address
+ r_offset
, view_size
);
445 // This class may be used as a typical class for the
446 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
447 // template parameter Classify_reloc must be a class type which
448 // provides a function get_size_for_reloc which returns the number of
449 // bytes to which a reloc applies. This class is intended to capture
450 // the most typical target behaviour, while still permitting targets
451 // to define their own independent class for Scan_relocatable_reloc.
453 template<int sh_type
, typename Classify_reloc
>
454 class Default_scan_relocatable_relocs
457 // Return the strategy to use for a local symbol which is not a
458 // section symbol, given the relocation type.
459 inline Relocatable_relocs::Reloc_strategy
460 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
462 // We assume that relocation type 0 is NONE. Targets which are
463 // different must override.
464 if (r_type
== 0 && r_sym
== 0)
465 return Relocatable_relocs::RELOC_DISCARD
;
466 return Relocatable_relocs::RELOC_COPY
;
469 // Return the strategy to use for a local symbol which is a section
470 // symbol, given the relocation type.
471 inline Relocatable_relocs::Reloc_strategy
472 local_section_strategy(unsigned int r_type
, Relobj
* object
)
474 if (sh_type
== elfcpp::SHT_RELA
)
475 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
478 Classify_reloc classify
;
479 switch (classify
.get_size_for_reloc(r_type
, object
))
482 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
484 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
486 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
488 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
490 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
497 // Return the strategy to use for a global symbol, given the
498 // relocation type, the object, and the symbol index.
499 inline Relocatable_relocs::Reloc_strategy
500 global_strategy(unsigned int, Relobj
*, unsigned int)
501 { return Relocatable_relocs::RELOC_COPY
; }
504 // Scan relocs during a relocatable link. This is a default
505 // definition which should work for most targets.
506 // Scan_relocatable_reloc must name a class type which provides three
507 // functions which return a Relocatable_relocs::Reloc_strategy code:
508 // global_strategy, local_non_section_strategy, and
509 // local_section_strategy. Most targets should be able to use
510 // Default_scan_relocatable_relocs as this class.
512 template<int size
, bool big_endian
, int sh_type
,
513 typename Scan_relocatable_reloc
>
515 scan_relocatable_relocs(
518 Sized_relobj_file
<size
, big_endian
>* object
,
519 unsigned int data_shndx
,
520 const unsigned char* prelocs
,
522 Output_section
* output_section
,
523 bool needs_special_offset_handling
,
524 size_t local_symbol_count
,
525 const unsigned char* plocal_syms
,
526 Relocatable_relocs
* rr
)
528 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
529 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
530 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
531 Scan_relocatable_reloc scan
;
533 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
535 Reltype
reloc(prelocs
);
537 Relocatable_relocs::Reloc_strategy strategy
;
539 if (needs_special_offset_handling
540 && !output_section
->is_input_address_mapped(object
, data_shndx
,
541 reloc
.get_r_offset()))
542 strategy
= Relocatable_relocs::RELOC_DISCARD
;
545 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
547 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
548 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
550 if (r_sym
>= local_symbol_count
)
551 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
554 gold_assert(plocal_syms
!= NULL
);
555 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
557 unsigned int shndx
= lsym
.get_st_shndx();
559 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
561 && shndx
!= elfcpp::SHN_UNDEF
562 && !object
->is_section_included(shndx
))
564 // RELOC is a relocation against a local symbol
565 // defined in a section we are discarding. Discard
566 // the reloc. FIXME: Should we issue a warning?
567 strategy
= Relocatable_relocs::RELOC_DISCARD
;
569 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
570 strategy
= scan
.local_non_section_strategy(r_type
, object
,
574 strategy
= scan
.local_section_strategy(r_type
, object
);
575 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
576 object
->output_section(shndx
)->set_needs_symtab_index();
579 if (strategy
== Relocatable_relocs::RELOC_COPY
)
580 object
->set_must_have_output_symtab_entry(r_sym
);
584 rr
->set_next_reloc_strategy(strategy
);
588 // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
589 // This is a default definition which should work for most targets.
591 template<int size
, bool big_endian
, int sh_type
>
594 const Relocate_info
<size
, big_endian
>* relinfo
,
595 const unsigned char* prelocs
,
597 Output_section
* output_section
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
599 const Relocatable_relocs
* rr
,
601 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
602 section_size_type view_size
,
603 unsigned char* reloc_view
,
604 section_size_type reloc_view_size
)
606 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
607 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
608 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
610 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
611 const Address invalid_address
= static_cast<Address
>(0) - 1;
613 Sized_relobj_file
<size
, big_endian
>* const object
= relinfo
->object
;
614 const unsigned int local_count
= object
->local_symbol_count();
616 unsigned char* pwrite
= reloc_view
;
618 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
620 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
621 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
624 if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
626 // Target wants to handle this relocation.
627 Sized_target
<size
, big_endian
>* target
=
628 parameters
->sized_target
<size
, big_endian
>();
629 target
->relocate_special_relocatable(relinfo
, sh_type
, prelocs
,
631 offset_in_output_section
,
634 pwrite
+= reloc_size
;
637 Reltype
reloc(prelocs
);
638 Reltype_write
reloc_write(pwrite
);
640 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
641 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
642 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
644 // Get the new symbol index.
646 unsigned int new_symndx
;
647 if (r_sym
< local_count
)
651 case Relocatable_relocs::RELOC_COPY
:
656 new_symndx
= object
->symtab_index(r_sym
);
657 gold_assert(new_symndx
!= -1U);
661 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
662 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
663 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
664 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
665 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
666 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
667 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
669 // We are adjusting a section symbol. We need to find
670 // the symbol table index of the section symbol for
671 // the output section corresponding to input section
672 // in which this symbol is defined.
673 gold_assert(r_sym
< local_count
);
676 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
677 gold_assert(is_ordinary
);
678 Output_section
* os
= object
->output_section(shndx
);
679 gold_assert(os
!= NULL
);
680 gold_assert(os
->needs_symtab_index());
681 new_symndx
= os
->symtab_index();
691 const Symbol
* gsym
= object
->global_symbol(r_sym
);
692 gold_assert(gsym
!= NULL
);
693 if (gsym
->is_forwarder())
694 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
696 gold_assert(gsym
->has_symtab_index());
697 new_symndx
= gsym
->symtab_index();
700 // Get the new offset--the location in the output section where
701 // this relocation should be applied.
703 Address offset
= reloc
.get_r_offset();
705 if (offset_in_output_section
!= invalid_address
)
706 new_offset
= offset
+ offset_in_output_section
;
709 section_offset_type sot_offset
=
710 convert_types
<section_offset_type
, Address
>(offset
);
711 section_offset_type new_sot_offset
=
712 output_section
->output_offset(object
, relinfo
->data_shndx
,
714 gold_assert(new_sot_offset
!= -1);
715 new_offset
= new_sot_offset
;
718 // In an object file, r_offset is an offset within the section.
719 // In an executable or dynamic object, generated by
720 // --emit-relocs, r_offset is an absolute address.
721 if (!parameters
->options().relocatable())
723 new_offset
+= view_address
;
724 if (offset_in_output_section
!= invalid_address
)
725 new_offset
-= offset_in_output_section
;
728 reloc_write
.put_r_offset(new_offset
);
729 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
731 // Handle the reloc addend based on the strategy.
733 if (strategy
== Relocatable_relocs::RELOC_COPY
)
735 if (sh_type
== elfcpp::SHT_RELA
)
736 Reloc_types
<sh_type
, size
, big_endian
>::
737 copy_reloc_addend(&reloc_write
,
742 // The relocation uses a section symbol in the input file.
743 // We are adjusting it to use a section symbol in the output
744 // file. The input section symbol refers to some address in
745 // the input section. We need the relocation in the output
746 // file to refer to that same address. This adjustment to
747 // the addend is the same calculation we use for a simple
748 // absolute relocation for the input section symbol.
750 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
752 unsigned char* padd
= view
+ offset
;
755 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
757 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
758 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
759 get_reloc_addend(&reloc
);
760 addend
= psymval
->value(object
, addend
);
761 Reloc_types
<sh_type
, size
, big_endian
>::
762 set_reloc_addend(&reloc_write
, addend
);
766 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
769 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
770 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
774 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
775 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
779 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
780 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
784 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
785 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
789 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
790 Relocate_functions
<size
, big_endian
>::rel32_unaligned(padd
,
800 pwrite
+= reloc_size
;
803 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
807 } // End namespace gold.
809 #endif // !defined(GOLD_TARGET_RELOC_H)