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
);
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 // 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 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
229 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
230 // RELOCATE implements operator() to do a relocation.
232 // PRELOCS points to the relocation data. RELOC_COUNT is the number
233 // of relocs. OUTPUT_SECTION is the output section.
234 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
235 // mapped to output offsets.
237 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
238 // VIEW_SIZE is the size. These refer to the input section, unless
239 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
240 // the output section.
242 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
243 // not NULL, it is a vector indexed by relocation index. If that
244 // entry is not NULL, it points to a global symbol which used as the
245 // symbol for the relocation, ignoring the symbol index in the
248 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
252 const Relocate_info
<size
, big_endian
>* relinfo
,
254 const unsigned char* prelocs
,
256 Output_section
* output_section
,
257 bool needs_special_offset_handling
,
259 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
260 section_size_type view_size
,
261 const Reloc_symbol_changes
* reloc_symbol_changes
)
263 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
264 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
267 Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
268 unsigned int local_count
= object
->local_symbol_count();
270 Comdat_behavior comdat_behavior
= CB_UNDETERMINED
;
272 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
274 Reltype
reloc(prelocs
);
276 section_offset_type offset
=
277 convert_to_section_size_type(reloc
.get_r_offset());
279 if (needs_special_offset_handling
)
281 offset
= output_section
->output_offset(relinfo
->object
,
288 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
289 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
290 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
292 const Sized_symbol
<size
>* sym
;
294 Symbol_value
<size
> symval
;
295 const Symbol_value
<size
> *psymval
;
296 bool is_defined_in_discarded_section
;
298 if (r_sym
< local_count
299 && (reloc_symbol_changes
== NULL
300 || (*reloc_symbol_changes
)[i
] == NULL
))
303 psymval
= object
->local_symbol(r_sym
);
305 // If the local symbol belongs to a section we are discarding,
306 // and that section is a debug section, try to find the
307 // corresponding kept section and map this symbol to its
308 // counterpart in the kept section. The symbol must not
309 // correspond to a section we are folding.
311 shndx
= psymval
->input_shndx(&is_ordinary
);
312 is_defined_in_discarded_section
=
314 && shndx
!= elfcpp::SHN_UNDEF
315 && !object
->is_section_included(shndx
)
316 && !relinfo
->symtab
->is_section_folded(object
, shndx
));
321 if (reloc_symbol_changes
!= NULL
322 && (*reloc_symbol_changes
)[i
] != NULL
)
323 gsym
= (*reloc_symbol_changes
)[i
];
326 gsym
= object
->global_symbol(r_sym
);
327 gold_assert(gsym
!= NULL
);
328 if (gsym
->is_forwarder())
329 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
332 sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
333 if (sym
->has_symtab_index() && sym
->symtab_index() != -1U)
334 symval
.set_output_symtab_index(sym
->symtab_index());
336 symval
.set_no_output_symtab_entry();
337 symval
.set_output_value(sym
->value());
338 if (gsym
->type() == elfcpp::STT_TLS
)
339 symval
.set_is_tls_symbol();
340 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
341 symval
.set_is_ifunc_symbol();
344 is_defined_in_discarded_section
=
345 (gsym
->is_defined_in_discarded_section()
346 && gsym
->is_undefined());
350 Symbol_value
<size
> symval2
;
351 if (is_defined_in_discarded_section
)
353 if (comdat_behavior
== CB_UNDETERMINED
)
355 std::string name
= object
->section_name(relinfo
->data_shndx
);
356 comdat_behavior
= get_comdat_behavior(name
.c_str());
358 if (comdat_behavior
== CB_PRETEND
)
360 // FIXME: This case does not work for global symbols.
361 // We have no place to store the original section index.
362 // Fortunately this does not matter for comdat sections,
363 // only for sections explicitly discarded by a linker
366 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
=
367 object
->map_to_kept_section(shndx
, &found
);
369 symval2
.set_output_value(value
+ psymval
->input_value());
371 symval2
.set_output_value(0);
375 if (comdat_behavior
== CB_WARNING
)
376 gold_warning_at_location(relinfo
, i
, offset
,
377 _("relocation refers to discarded "
379 symval2
.set_output_value(0);
381 symval2
.set_no_output_symtab_entry();
385 if (!relocate
.relocate(relinfo
, target
, output_section
, i
, reloc
,
386 r_type
, sym
, psymval
, view
+ offset
,
387 view_address
+ offset
, view_size
))
390 if (offset
< 0 || static_cast<section_size_type
>(offset
) >= view_size
)
392 gold_error_at_location(relinfo
, i
, offset
,
393 _("reloc has bad offset %zu"),
394 static_cast<size_t>(offset
));
398 if (issue_undefined_symbol_error(sym
))
399 gold_undefined_symbol_at_location(sym
, relinfo
, i
, offset
);
401 && sym
->visibility() != elfcpp::STV_DEFAULT
402 && (sym
->is_undefined() || sym
->is_from_dynobj()))
403 visibility_error(sym
);
405 if (sym
!= NULL
&& sym
->has_warning())
406 relinfo
->symtab
->issue_warning(sym
, relinfo
, i
, offset
);
410 // Apply an incremental relocation.
412 template<int size
, bool big_endian
, typename Target_type
,
415 apply_relocation(const Relocate_info
<size
, big_endian
>* relinfo
,
417 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
419 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
422 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
423 section_size_type view_size
)
425 // Construct the ELF relocation in a temporary buffer.
426 const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
427 unsigned char relbuf
[reloc_size
];
428 elfcpp::Rela
<size
, big_endian
> rel(relbuf
);
429 elfcpp::Rela_write
<size
, big_endian
> orel(relbuf
);
430 orel
.put_r_offset(r_offset
);
431 orel
.put_r_info(elfcpp::elf_r_info
<size
>(0, r_type
));
432 orel
.put_r_addend(r_addend
);
434 // Setup a Symbol_value for the global symbol.
435 const Sized_symbol
<size
>* sym
= static_cast<const Sized_symbol
<size
>*>(gsym
);
436 Symbol_value
<size
> symval
;
437 gold_assert(sym
->has_symtab_index() && sym
->symtab_index() != -1U);
438 symval
.set_output_symtab_index(sym
->symtab_index());
439 symval
.set_output_value(sym
->value());
440 if (gsym
->type() == elfcpp::STT_TLS
)
441 symval
.set_is_tls_symbol();
442 else if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
443 symval
.set_is_ifunc_symbol();
446 relocate
.relocate(relinfo
, target
, NULL
, -1U, rel
, r_type
, sym
, &symval
,
447 view
+ r_offset
, address
+ r_offset
, view_size
);
450 // This class may be used as a typical class for the
451 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
452 // template parameter Classify_reloc must be a class type which
453 // provides a function get_size_for_reloc which returns the number of
454 // bytes to which a reloc applies. This class is intended to capture
455 // the most typical target behaviour, while still permitting targets
456 // to define their own independent class for Scan_relocatable_reloc.
458 template<int sh_type
, typename Classify_reloc
>
459 class Default_scan_relocatable_relocs
462 // Return the strategy to use for a local symbol which is not a
463 // section symbol, given the relocation type.
464 inline Relocatable_relocs::Reloc_strategy
465 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
467 // We assume that relocation type 0 is NONE. Targets which are
468 // different must override.
469 if (r_type
== 0 && r_sym
== 0)
470 return Relocatable_relocs::RELOC_DISCARD
;
471 return Relocatable_relocs::RELOC_COPY
;
474 // Return the strategy to use for a local symbol which is a section
475 // symbol, given the relocation type.
476 inline Relocatable_relocs::Reloc_strategy
477 local_section_strategy(unsigned int r_type
, Relobj
* object
)
479 if (sh_type
== elfcpp::SHT_RELA
)
480 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
483 Classify_reloc classify
;
484 switch (classify
.get_size_for_reloc(r_type
, object
))
487 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
;
489 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
;
491 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
;
493 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
;
495 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
;
502 // Return the strategy to use for a global symbol, given the
503 // relocation type, the object, and the symbol index.
504 inline Relocatable_relocs::Reloc_strategy
505 global_strategy(unsigned int, Relobj
*, unsigned int)
506 { return Relocatable_relocs::RELOC_COPY
; }
509 // Scan relocs during a relocatable link. This is a default
510 // definition which should work for most targets.
511 // Scan_relocatable_reloc must name a class type which provides three
512 // functions which return a Relocatable_relocs::Reloc_strategy code:
513 // global_strategy, local_non_section_strategy, and
514 // local_section_strategy. Most targets should be able to use
515 // Default_scan_relocatable_relocs as this class.
517 template<int size
, bool big_endian
, int sh_type
,
518 typename Scan_relocatable_reloc
>
520 scan_relocatable_relocs(
523 Sized_relobj_file
<size
, big_endian
>* object
,
524 unsigned int data_shndx
,
525 const unsigned char* prelocs
,
527 Output_section
* output_section
,
528 bool needs_special_offset_handling
,
529 size_t local_symbol_count
,
530 const unsigned char* plocal_syms
,
531 Relocatable_relocs
* rr
)
533 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
534 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
535 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
536 Scan_relocatable_reloc scan
;
538 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
540 Reltype
reloc(prelocs
);
542 Relocatable_relocs::Reloc_strategy strategy
;
544 if (needs_special_offset_handling
545 && !output_section
->is_input_address_mapped(object
, data_shndx
,
546 reloc
.get_r_offset()))
547 strategy
= Relocatable_relocs::RELOC_DISCARD
;
550 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
=
552 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
553 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
555 if (r_sym
>= local_symbol_count
)
556 strategy
= scan
.global_strategy(r_type
, object
, r_sym
);
559 gold_assert(plocal_syms
!= NULL
);
560 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
562 unsigned int shndx
= lsym
.get_st_shndx();
564 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
566 && shndx
!= elfcpp::SHN_UNDEF
567 && !object
->is_section_included(shndx
))
569 // RELOC is a relocation against a local symbol
570 // defined in a section we are discarding. Discard
571 // the reloc. FIXME: Should we issue a warning?
572 strategy
= Relocatable_relocs::RELOC_DISCARD
;
574 else if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
575 strategy
= scan
.local_non_section_strategy(r_type
, object
,
579 strategy
= scan
.local_section_strategy(r_type
, object
);
580 if (strategy
!= Relocatable_relocs::RELOC_DISCARD
)
581 object
->output_section(shndx
)->set_needs_symtab_index();
584 if (strategy
== Relocatable_relocs::RELOC_COPY
)
585 object
->set_must_have_output_symtab_entry(r_sym
);
589 rr
->set_next_reloc_strategy(strategy
);
593 // Relocate relocs during a relocatable link. This is a default
594 // definition which should work for most targets.
596 template<int size
, bool big_endian
, int sh_type
>
598 relocate_for_relocatable(
599 const Relocate_info
<size
, big_endian
>* relinfo
,
600 const unsigned char* prelocs
,
602 Output_section
* output_section
,
603 typename
elfcpp::Elf_types
<size
>::Elf_Addr offset_in_output_section
,
604 const Relocatable_relocs
* rr
,
606 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
607 section_size_type view_size
,
608 unsigned char* reloc_view
,
609 section_size_type reloc_view_size
)
611 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
612 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
613 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc_write
615 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
616 const Address invalid_address
= static_cast<Address
>(0) - 1;
618 Sized_relobj_file
<size
, big_endian
>* const object
= relinfo
->object
;
619 const unsigned int local_count
= object
->local_symbol_count();
621 unsigned char* pwrite
= reloc_view
;
623 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
625 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
626 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
629 if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
631 // Target wants to handle this relocation.
632 Sized_target
<size
, big_endian
>* target
=
633 parameters
->sized_target
<size
, big_endian
>();
634 target
->relocate_special_relocatable(relinfo
, sh_type
, prelocs
,
636 offset_in_output_section
,
639 pwrite
+= reloc_size
;
642 Reltype
reloc(prelocs
);
643 Reltype_write
reloc_write(pwrite
);
645 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
646 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
647 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
649 // Get the new symbol index.
651 unsigned int new_symndx
;
652 if (r_sym
< local_count
)
656 case Relocatable_relocs::RELOC_COPY
:
661 new_symndx
= object
->symtab_index(r_sym
);
662 gold_assert(new_symndx
!= -1U);
666 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
667 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
668 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
669 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
670 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
671 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
672 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
674 // We are adjusting a section symbol. We need to find
675 // the symbol table index of the section symbol for
676 // the output section corresponding to input section
677 // in which this symbol is defined.
678 gold_assert(r_sym
< local_count
);
681 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
682 gold_assert(is_ordinary
);
683 Output_section
* os
= object
->output_section(shndx
);
684 gold_assert(os
!= NULL
);
685 gold_assert(os
->needs_symtab_index());
686 new_symndx
= os
->symtab_index();
696 const Symbol
* gsym
= object
->global_symbol(r_sym
);
697 gold_assert(gsym
!= NULL
);
698 if (gsym
->is_forwarder())
699 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
701 gold_assert(gsym
->has_symtab_index());
702 new_symndx
= gsym
->symtab_index();
705 // Get the new offset--the location in the output section where
706 // this relocation should be applied.
708 Address offset
= reloc
.get_r_offset();
710 if (offset_in_output_section
!= invalid_address
)
711 new_offset
= offset
+ offset_in_output_section
;
714 section_offset_type sot_offset
=
715 convert_types
<section_offset_type
, Address
>(offset
);
716 section_offset_type new_sot_offset
=
717 output_section
->output_offset(object
, relinfo
->data_shndx
,
719 gold_assert(new_sot_offset
!= -1);
720 new_offset
= new_sot_offset
;
723 // In an object file, r_offset is an offset within the section.
724 // In an executable or dynamic object, generated by
725 // --emit-relocs, r_offset is an absolute address.
726 if (!parameters
->options().relocatable())
728 new_offset
+= view_address
;
729 if (offset_in_output_section
!= invalid_address
)
730 new_offset
-= offset_in_output_section
;
733 reloc_write
.put_r_offset(new_offset
);
734 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
736 // Handle the reloc addend based on the strategy.
738 if (strategy
== Relocatable_relocs::RELOC_COPY
)
740 if (sh_type
== elfcpp::SHT_RELA
)
741 Reloc_types
<sh_type
, size
, big_endian
>::
742 copy_reloc_addend(&reloc_write
,
747 // The relocation uses a section symbol in the input file.
748 // We are adjusting it to use a section symbol in the output
749 // file. The input section symbol refers to some address in
750 // the input section. We need the relocation in the output
751 // file to refer to that same address. This adjustment to
752 // the addend is the same calculation we use for a simple
753 // absolute relocation for the input section symbol.
755 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
757 unsigned char* padd
= view
+ offset
;
760 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
762 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
763 addend
= Reloc_types
<sh_type
, size
, big_endian
>::
764 get_reloc_addend(&reloc
);
765 addend
= psymval
->value(object
, addend
);
766 Reloc_types
<sh_type
, size
, big_endian
>::
767 set_reloc_addend(&reloc_write
, addend
);
771 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0
:
774 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1
:
775 Relocate_functions
<size
, big_endian
>::rel8(padd
, object
,
779 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2
:
780 Relocate_functions
<size
, big_endian
>::rel16(padd
, object
,
784 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4
:
785 Relocate_functions
<size
, big_endian
>::rel32(padd
, object
,
789 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8
:
790 Relocate_functions
<size
, big_endian
>::rel64(padd
, object
,
794 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED
:
795 Relocate_functions
<size
, big_endian
>::rel32_unaligned(padd
,
805 pwrite
+= reloc_size
;
808 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
812 } // End namespace gold.
814 #endif // !defined(GOLD_TARGET_RELOC_H)