1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 #ifndef GOLD_TARGET_RELOC_H
4 #define GOLD_TARGET_RELOC_H
13 // Pick the ELF relocation accessor class and the size based on
14 // SH_TYPE, which is either SHT_REL or SHT_RELA.
16 template<int sh_type
, int size
, bool big_endian
>
19 template<int size
, bool big_endian
>
20 struct Reloc_types
<elfcpp::SHT_REL
, size
, big_endian
>
22 typedef typename
elfcpp::Rel
<size
, big_endian
> Reloc
;
23 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rel_size
;
26 template<int size
, bool big_endian
>
27 struct Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>
29 typedef typename
elfcpp::Rela
<size
, big_endian
> Reloc
;
30 static const int reloc_size
= elfcpp::Elf_sizes
<size
>::rela_size
;
33 // This function implements the generic part of reloc scanning. This
34 // is an inline function which takes a class whose operator()
35 // implements the machine specific part of scanning. We do it this
36 // way to avoidmaking a function call for each relocation, and to
37 // avoid repeating the generic code for each target.
39 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
43 const General_options
& options
,
47 Sized_relobj
<size
, big_endian
>* object
,
48 const unsigned char* prelocs
,
51 const unsigned char* plocal_syms
,
54 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
55 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
56 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
59 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
61 Reltype
reloc(prelocs
);
63 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
64 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
65 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
67 if (r_sym
< local_count
)
69 assert(plocal_syms
!= NULL
);
70 typename
elfcpp::Sym
<size
, big_endian
> lsym(plocal_syms
72 const unsigned int shndx
= lsym
.get_st_shndx();
73 if (shndx
< elfcpp::SHN_LORESERVE
74 && shndx
!= elfcpp::SHN_UNDEF
75 && !object
->is_section_included(lsym
.get_st_shndx()))
77 // RELOC is a relocation against a local symbol in a
78 // section we are discarding. We can ignore this
79 // relocation. It will eventually become a reloc
80 // against the value zero.
82 // FIXME: We should issue a warning if this is an
83 // allocated section; is this the best place to do it?
85 // FIXME: The old GNU linker would in some cases look
86 // for the linkonce section which caused this section to
87 // be discarded, and, if the other section was the same
88 // size, change the reloc to refer to the other section.
89 // That seems risky and weird to me, and I don't know of
90 // any case where it is actually required.
95 scan
.local(options
, symtab
, layout
, target
, object
, reloc
, r_type
,
100 Symbol
* gsym
= global_syms
[r_sym
- local_count
];
101 assert(gsym
!= NULL
);
102 if (gsym
->is_forwarder())
103 gsym
= symtab
->resolve_forwards(gsym
);
105 scan
.global(options
, symtab
, layout
, target
, object
, reloc
, r_type
,
111 // This function implements the generic part of relocation processing.
112 // This is an inline function which take a class whose operator()
113 // implements the machine specific part of relocation. We do it this
114 // way to avoid making a function call for each relocation, and to
115 // avoid repeating the generic relocation handling code for each
118 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
119 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
120 // RELOCATE implements operator() to do a relocation.
122 // PRELOCS points to the relocation data. RELOC_COUNT is the number
123 // of relocs. VIEW is the section data, VIEW_ADDRESS is its memory
124 // address, and VIEW_SIZE is the size.
126 template<int size
, bool big_endian
, typename Target_type
, int sh_type
,
130 const Relocate_info
<size
, big_endian
>* relinfo
,
132 const unsigned char* prelocs
,
135 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
138 typedef typename Reloc_types
<sh_type
, size
, big_endian
>::Reloc Reltype
;
139 const int reloc_size
= Reloc_types
<sh_type
, size
, big_endian
>::reloc_size
;
142 unsigned int local_count
= relinfo
->local_symbol_count
;
143 typename
elfcpp::Elf_types
<size
>::Elf_Addr
*local_values
= relinfo
->values
;
144 Symbol
** global_syms
= relinfo
->symbols
;
146 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
148 Reltype
reloc(prelocs
);
150 off_t offset
= reloc
.get_r_offset();
152 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
153 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
154 unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
156 Sized_symbol
<size
>* sym
;
157 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
159 if (r_sym
< local_count
)
162 value
= local_values
[r_sym
];
166 Symbol
* gsym
= global_syms
[r_sym
- local_count
];
167 assert(gsym
!= NULL
);
168 if (gsym
->is_forwarder())
169 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
171 sym
= static_cast<Sized_symbol
<size
>*>(gsym
);
172 value
= sym
->value();
175 if (!relocate
.relocate(relinfo
, target
, i
, reloc
, r_type
, sym
, value
,
176 view
+ offset
, view_address
+ offset
, view_size
))
179 if (offset
< 0 || offset
>= view_size
)
181 fprintf(stderr
, _("%s: %s: reloc has bad offset %zu\n"),
182 program_name
, relinfo
->location(i
, offset
).c_str(),
183 static_cast<size_t>(offset
));
188 && sym
->is_undefined()
189 && sym
->binding() != elfcpp::STB_WEAK
)
191 fprintf(stderr
, _("%s: %s: undefined reference to '%s'\n"),
192 program_name
, relinfo
->location(i
, offset
).c_str(),
197 if (sym
!= NULL
&& sym
->has_warning())
198 relinfo
->symtab
->issue_warning(sym
, relinfo
->location(i
, offset
));
202 } // End namespace gold.
204 #endif // !defined(GOLD_TARGET_RELOC_H)