Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
2 | ||
ad0f2072 | 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
61ba1cf9 ILT |
23 | #ifndef GOLD_TARGET_RELOC_H |
24 | #define GOLD_TARGET_RELOC_H | |
25 | ||
26 | #include "elfcpp.h" | |
27 | #include "symtab.h" | |
364c7fa5 | 28 | #include "object.h" |
6a74a719 | 29 | #include "reloc.h" |
c06b7b0b | 30 | #include "reloc-types.h" |
61ba1cf9 ILT |
31 | |
32 | namespace gold | |
33 | { | |
34 | ||
6a74a719 ILT |
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 | // avoidmaking a function call for each relocation, and to avoid | |
40 | // repeating the generic code for each target. | |
92e059d8 | 41 | |
ead1e424 ILT |
42 | template<int size, bool big_endian, typename Target_type, int sh_type, |
43 | typename Scan> | |
92e059d8 ILT |
44 | inline void |
45 | scan_relocs( | |
92e059d8 | 46 | Symbol_table* symtab, |
ead1e424 ILT |
47 | Layout* layout, |
48 | Target_type* target, | |
f6ce93d6 | 49 | Sized_relobj<size, big_endian>* object, |
a3ad94ed | 50 | unsigned int data_shndx, |
92e059d8 ILT |
51 | const unsigned char* prelocs, |
52 | size_t reloc_count, | |
730cdc88 ILT |
53 | Output_section* output_section, |
54 | bool needs_special_offset_handling, | |
92e059d8 | 55 | size_t local_count, |
730cdc88 | 56 | const unsigned char* plocal_syms) |
92e059d8 ILT |
57 | { |
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; | |
61 | Scan scan; | |
62 | ||
63 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
64 | { | |
65 | Reltype reloc(prelocs); | |
66 | ||
730cdc88 ILT |
67 | if (needs_special_offset_handling |
68 | && !output_section->is_input_address_mapped(object, data_shndx, | |
69 | reloc.get_r_offset())) | |
70 | continue; | |
71 | ||
92e059d8 ILT |
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); | |
75 | ||
76 | if (r_sym < local_count) | |
77 | { | |
a3ad94ed | 78 | gold_assert(plocal_syms != NULL); |
92e059d8 ILT |
79 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms |
80 | + r_sym * sym_size); | |
d491d34e ILT |
81 | unsigned int shndx = lsym.get_st_shndx(); |
82 | bool is_ordinary; | |
83 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
84 | if (is_ordinary | |
ead1e424 | 85 | && shndx != elfcpp::SHN_UNDEF |
d491d34e | 86 | && !object->is_section_included(shndx)) |
92e059d8 ILT |
87 | { |
88 | // RELOC is a relocation against a local symbol in a | |
89 | // section we are discarding. We can ignore this | |
90 | // relocation. It will eventually become a reloc | |
91 | // against the value zero. | |
92 | // | |
93 | // FIXME: We should issue a warning if this is an | |
94 | // allocated section; is this the best place to do it? | |
95 | // | |
96 | // FIXME: The old GNU linker would in some cases look | |
97 | // for the linkonce section which caused this section to | |
98 | // be discarded, and, if the other section was the same | |
99 | // size, change the reloc to refer to the other section. | |
100 | // That seems risky and weird to me, and I don't know of | |
101 | // any case where it is actually required. | |
102 | ||
103 | continue; | |
104 | } | |
105 | ||
ad0f2072 | 106 | scan.local(symtab, layout, target, object, data_shndx, |
07f397ab | 107 | output_section, reloc, r_type, lsym); |
92e059d8 ILT |
108 | } |
109 | else | |
110 | { | |
730cdc88 | 111 | Symbol* gsym = object->global_symbol(r_sym); |
a3ad94ed | 112 | gold_assert(gsym != NULL); |
92e059d8 ILT |
113 | if (gsym->is_forwarder()) |
114 | gsym = symtab->resolve_forwards(gsym); | |
115 | ||
ad0f2072 | 116 | scan.global(symtab, layout, target, object, data_shndx, |
07f397ab | 117 | output_section, reloc, r_type, gsym); |
92e059d8 ILT |
118 | } |
119 | } | |
120 | } | |
121 | ||
e94cf127 CC |
122 | // Behavior for relocations to discarded comdat sections. |
123 | ||
124 | enum Comdat_behavior | |
125 | { | |
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. | |
130 | }; | |
131 | ||
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 | |
134 | // relocated. | |
135 | ||
136 | inline Comdat_behavior | |
137 | get_comdat_behavior(const char* name) | |
138 | { | |
139 | if (Layout::is_debug_info_section(name)) | |
140 | return CB_PRETEND; | |
141 | if (strcmp(name, ".eh_frame") == 0 | |
142 | || strcmp(name, ".gcc_except_table") == 0) | |
143 | return CB_IGNORE; | |
144 | return CB_WARNING; | |
145 | } | |
146 | ||
92e059d8 | 147 | // This function implements the generic part of relocation processing. |
6a74a719 ILT |
148 | // The template parameter Relocate must be a class type which provides |
149 | // a single function, relocate(), which implements the machine | |
150 | // specific part of a relocation. | |
61ba1cf9 ILT |
151 | |
152 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of | |
92e059d8 ILT |
153 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
154 | // RELOCATE implements operator() to do a relocation. | |
61ba1cf9 | 155 | |
92e059d8 | 156 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
730cdc88 ILT |
157 | // of relocs. OUTPUT_SECTION is the output section. |
158 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be | |
159 | // mapped to output offsets. | |
160 | ||
161 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and | |
162 | // VIEW_SIZE is the size. These refer to the input section, unless | |
163 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to | |
164 | // the output section. | |
61ba1cf9 | 165 | |
364c7fa5 ILT |
166 | // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is |
167 | // not NULL, it is a vector indexed by relocation index. If that | |
168 | // entry is not NULL, it points to a global symbol which used as the | |
169 | // symbol for the relocation, ignoring the symbol index in the | |
170 | // relocation. | |
171 | ||
ead1e424 ILT |
172 | template<int size, bool big_endian, typename Target_type, int sh_type, |
173 | typename Relocate> | |
61ba1cf9 ILT |
174 | inline void |
175 | relocate_section( | |
92e059d8 | 176 | const Relocate_info<size, big_endian>* relinfo, |
ead1e424 | 177 | Target_type* target, |
61ba1cf9 ILT |
178 | const unsigned char* prelocs, |
179 | size_t reloc_count, | |
730cdc88 ILT |
180 | Output_section* output_section, |
181 | bool needs_special_offset_handling, | |
61ba1cf9 ILT |
182 | unsigned char* view, |
183 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
364c7fa5 ILT |
184 | section_size_type view_size, |
185 | const Reloc_symbol_changes* reloc_symbol_changes) | |
61ba1cf9 ILT |
186 | { |
187 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
188 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
189 | Relocate relocate; | |
190 | ||
730cdc88 ILT |
191 | Sized_relobj<size, big_endian>* object = relinfo->object; |
192 | unsigned int local_count = object->local_symbol_count(); | |
92e059d8 | 193 | |
e94cf127 CC |
194 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; |
195 | ||
61ba1cf9 ILT |
196 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
197 | { | |
198 | Reltype reloc(prelocs); | |
199 | ||
fe8718a4 ILT |
200 | section_offset_type offset = |
201 | convert_to_section_size_type(reloc.get_r_offset()); | |
61ba1cf9 | 202 | |
730cdc88 ILT |
203 | if (needs_special_offset_handling) |
204 | { | |
205 | offset = output_section->output_offset(relinfo->object, | |
206 | relinfo->data_shndx, | |
207 | offset); | |
208 | if (offset == -1) | |
209 | continue; | |
210 | } | |
211 | ||
61ba1cf9 ILT |
212 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); |
213 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
214 | unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
215 | ||
c06b7b0b | 216 | const Sized_symbol<size>* sym; |
61ba1cf9 | 217 | |
b8e6aad9 ILT |
218 | Symbol_value<size> symval; |
219 | const Symbol_value<size> *psymval; | |
364c7fa5 ILT |
220 | if (r_sym < local_count |
221 | && (reloc_symbol_changes == NULL | |
222 | || (*reloc_symbol_changes)[i] == NULL)) | |
61ba1cf9 ILT |
223 | { |
224 | sym = NULL; | |
730cdc88 | 225 | psymval = object->local_symbol(r_sym); |
e94cf127 CC |
226 | |
227 | // If the local symbol belongs to a section we are discarding, | |
228 | // and that section is a debug section, try to find the | |
229 | // corresponding kept section and map this symbol to its | |
ef15dade ST |
230 | // counterpart in the kept section. The symbol must not |
231 | // correspond to a section we are folding. | |
e94cf127 CC |
232 | bool is_ordinary; |
233 | unsigned int shndx = psymval->input_shndx(&is_ordinary); | |
234 | if (is_ordinary | |
235 | && shndx != elfcpp::SHN_UNDEF | |
ef15dade ST |
236 | && !object->is_section_included(shndx) |
237 | && !(relinfo->symtab->is_section_folded(object, shndx))) | |
e94cf127 CC |
238 | { |
239 | if (comdat_behavior == CB_UNDETERMINED) | |
240 | { | |
4418b2d5 CC |
241 | std::string name = object->section_name(relinfo->data_shndx); |
242 | comdat_behavior = get_comdat_behavior(name.c_str()); | |
e94cf127 CC |
243 | } |
244 | if (comdat_behavior == CB_PRETEND) | |
245 | { | |
246 | bool found; | |
247 | typename elfcpp::Elf_types<size>::Elf_Addr value = | |
248 | object->map_to_kept_section(shndx, &found); | |
249 | if (found) | |
250 | symval.set_output_value(value + psymval->input_value()); | |
251 | else | |
252 | symval.set_output_value(0); | |
253 | } | |
254 | else | |
255 | { | |
256 | if (comdat_behavior == CB_WARNING) | |
257 | gold_warning_at_location(relinfo, i, offset, | |
364c7fa5 | 258 | _("relocation refers to discarded " |
e94cf127 CC |
259 | "comdat section")); |
260 | symval.set_output_value(0); | |
261 | } | |
262 | symval.set_no_output_symtab_entry(); | |
263 | psymval = &symval; | |
264 | } | |
61ba1cf9 ILT |
265 | } |
266 | else | |
267 | { | |
364c7fa5 ILT |
268 | const Symbol* gsym; |
269 | if (reloc_symbol_changes != NULL | |
270 | && (*reloc_symbol_changes)[i] != NULL) | |
271 | gsym = (*reloc_symbol_changes)[i]; | |
272 | else | |
273 | { | |
274 | gsym = object->global_symbol(r_sym); | |
275 | gold_assert(gsym != NULL); | |
276 | if (gsym->is_forwarder()) | |
277 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
278 | } | |
61ba1cf9 | 279 | |
c06b7b0b | 280 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
b8e6aad9 ILT |
281 | if (sym->has_symtab_index()) |
282 | symval.set_output_symtab_index(sym->symtab_index()); | |
283 | else | |
284 | symval.set_no_output_symtab_entry(); | |
285 | symval.set_output_value(sym->value()); | |
286 | psymval = &symval; | |
ead1e424 | 287 | } |
61ba1cf9 | 288 | |
031cdbed ILT |
289 | if (!relocate.relocate(relinfo, target, output_section, i, reloc, |
290 | r_type, sym, psymval, view + offset, | |
291 | view_address + offset, view_size)) | |
ead1e424 ILT |
292 | continue; |
293 | ||
fe8718a4 | 294 | if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) |
ead1e424 | 295 | { |
75f2446e ILT |
296 | gold_error_at_location(relinfo, i, offset, |
297 | _("reloc has bad offset %zu"), | |
298 | static_cast<size_t>(offset)); | |
299 | continue; | |
61ba1cf9 ILT |
300 | } |
301 | ||
ead1e424 ILT |
302 | if (sym != NULL |
303 | && sym->is_undefined() | |
436ca963 | 304 | && sym->binding() != elfcpp::STB_WEAK |
9c2d0ef9 | 305 | && !target->is_defined_by_abi(sym) |
eb42429a ILT |
306 | && (!parameters->options().shared() // -shared |
307 | || parameters->options().defs())) // -z defs | |
f073bbf7 | 308 | gold_undefined_symbol_at_location(sym, relinfo, i, offset); |
f6ce93d6 ILT |
309 | |
310 | if (sym != NULL && sym->has_warning()) | |
75f2446e | 311 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
61ba1cf9 ILT |
312 | } |
313 | } | |
314 | ||
6a74a719 ILT |
315 | // This class may be used as a typical class for the |
316 | // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The | |
317 | // template parameter Classify_reloc must be a class type which | |
318 | // provides a function get_size_for_reloc which returns the number of | |
319 | // bytes to which a reloc applies. This class is intended to capture | |
320 | // the most typical target behaviour, while still permitting targets | |
321 | // to define their own independent class for Scan_relocatable_reloc. | |
322 | ||
323 | template<int sh_type, typename Classify_reloc> | |
324 | class Default_scan_relocatable_relocs | |
325 | { | |
326 | public: | |
327 | // Return the strategy to use for a local symbol which is not a | |
328 | // section symbol, given the relocation type. | |
329 | inline Relocatable_relocs::Reloc_strategy | |
68943102 | 330 | local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) |
c2508178 ILT |
331 | { |
332 | // We assume that relocation type 0 is NONE. Targets which are | |
333 | // different must override. | |
68943102 | 334 | if (r_type == 0 && r_sym == 0) |
c2508178 ILT |
335 | return Relocatable_relocs::RELOC_DISCARD; |
336 | return Relocatable_relocs::RELOC_COPY; | |
337 | } | |
6a74a719 ILT |
338 | |
339 | // Return the strategy to use for a local symbol which is a section | |
340 | // symbol, given the relocation type. | |
341 | inline Relocatable_relocs::Reloc_strategy | |
342 | local_section_strategy(unsigned int r_type, Relobj* object) | |
343 | { | |
344 | if (sh_type == elfcpp::SHT_RELA) | |
345 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; | |
346 | else | |
347 | { | |
348 | Classify_reloc classify; | |
349 | switch (classify.get_size_for_reloc(r_type, object)) | |
350 | { | |
351 | case 0: | |
7019cd25 | 352 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
6a74a719 ILT |
353 | case 1: |
354 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; | |
355 | case 2: | |
356 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; | |
357 | case 4: | |
358 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; | |
359 | case 8: | |
360 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; | |
361 | default: | |
362 | gold_unreachable(); | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | // Return the strategy to use for a global symbol, given the | |
368 | // relocation type, the object, and the symbol index. | |
369 | inline Relocatable_relocs::Reloc_strategy | |
68943102 ILT |
370 | global_strategy(unsigned int, Relobj*, unsigned int) |
371 | { return Relocatable_relocs::RELOC_COPY; } | |
6a74a719 ILT |
372 | }; |
373 | ||
374 | // Scan relocs during a relocatable link. This is a default | |
375 | // definition which should work for most targets. | |
376 | // Scan_relocatable_reloc must name a class type which provides three | |
377 | // functions which return a Relocatable_relocs::Reloc_strategy code: | |
378 | // global_strategy, local_non_section_strategy, and | |
379 | // local_section_strategy. Most targets should be able to use | |
380 | // Default_scan_relocatable_relocs as this class. | |
381 | ||
7019cd25 | 382 | template<int size, bool big_endian, int sh_type, |
6a74a719 ILT |
383 | typename Scan_relocatable_reloc> |
384 | void | |
385 | scan_relocatable_relocs( | |
6a74a719 ILT |
386 | Symbol_table*, |
387 | Layout*, | |
388 | Sized_relobj<size, big_endian>* object, | |
389 | unsigned int data_shndx, | |
390 | const unsigned char* prelocs, | |
391 | size_t reloc_count, | |
392 | Output_section* output_section, | |
393 | bool needs_special_offset_handling, | |
394 | size_t local_symbol_count, | |
395 | const unsigned char* plocal_syms, | |
396 | Relocatable_relocs* rr) | |
397 | { | |
398 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
399 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
400 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
401 | Scan_relocatable_reloc scan; | |
402 | ||
403 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
404 | { | |
405 | Reltype reloc(prelocs); | |
406 | ||
407 | Relocatable_relocs::Reloc_strategy strategy; | |
408 | ||
409 | if (needs_special_offset_handling | |
410 | && !output_section->is_input_address_mapped(object, data_shndx, | |
411 | reloc.get_r_offset())) | |
412 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
413 | else | |
414 | { | |
415 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = | |
416 | reloc.get_r_info(); | |
417 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
418 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
419 | ||
420 | if (r_sym >= local_symbol_count) | |
421 | strategy = scan.global_strategy(r_type, object, r_sym); | |
422 | else | |
423 | { | |
424 | gold_assert(plocal_syms != NULL); | |
425 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms | |
426 | + r_sym * sym_size); | |
d491d34e ILT |
427 | unsigned int shndx = lsym.get_st_shndx(); |
428 | bool is_ordinary; | |
429 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
430 | if (is_ordinary | |
6a74a719 | 431 | && shndx != elfcpp::SHN_UNDEF |
d491d34e | 432 | && !object->is_section_included(shndx)) |
6a74a719 ILT |
433 | { |
434 | // RELOC is a relocation against a local symbol | |
435 | // defined in a section we are discarding. Discard | |
436 | // the reloc. FIXME: Should we issue a warning? | |
437 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
438 | } | |
439 | else if (lsym.get_st_type() != elfcpp::STT_SECTION) | |
68943102 ILT |
440 | strategy = scan.local_non_section_strategy(r_type, object, |
441 | r_sym); | |
6a74a719 ILT |
442 | else |
443 | { | |
444 | strategy = scan.local_section_strategy(r_type, object); | |
445 | if (strategy != Relocatable_relocs::RELOC_DISCARD) | |
ef9beddf | 446 | object->output_section(shndx)->set_needs_symtab_index(); |
6a74a719 ILT |
447 | } |
448 | } | |
449 | } | |
450 | ||
451 | rr->set_next_reloc_strategy(strategy); | |
452 | } | |
453 | } | |
454 | ||
455 | // Relocate relocs during a relocatable link. This is a default | |
456 | // definition which should work for most targets. | |
457 | ||
7019cd25 | 458 | template<int size, bool big_endian, int sh_type> |
6a74a719 ILT |
459 | void |
460 | relocate_for_relocatable( | |
461 | const Relocate_info<size, big_endian>* relinfo, | |
462 | const unsigned char* prelocs, | |
463 | size_t reloc_count, | |
464 | Output_section* output_section, | |
ef9beddf | 465 | typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, |
6a74a719 ILT |
466 | const Relocatable_relocs* rr, |
467 | unsigned char* view, | |
6be6f3bd | 468 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
6a74a719 ILT |
469 | section_size_type, |
470 | unsigned char* reloc_view, | |
471 | section_size_type reloc_view_size) | |
472 | { | |
ef9beddf | 473 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
6a74a719 ILT |
474 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; |
475 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write | |
476 | Reltype_write; | |
477 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
eff45813 | 478 | const Address invalid_address = static_cast<Address>(0) - 1; |
6a74a719 ILT |
479 | |
480 | Sized_relobj<size, big_endian>* const object = relinfo->object; | |
481 | const unsigned int local_count = object->local_symbol_count(); | |
482 | ||
483 | unsigned char* pwrite = reloc_view; | |
484 | ||
485 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
486 | { | |
487 | Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i); | |
488 | if (strategy == Relocatable_relocs::RELOC_DISCARD) | |
489 | continue; | |
490 | ||
491 | Reltype reloc(prelocs); | |
492 | Reltype_write reloc_write(pwrite); | |
493 | ||
494 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
495 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
496 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
497 | ||
498 | // Get the new symbol index. | |
499 | ||
500 | unsigned int new_symndx; | |
501 | if (r_sym < local_count) | |
502 | { | |
503 | switch (strategy) | |
504 | { | |
505 | case Relocatable_relocs::RELOC_COPY: | |
506 | new_symndx = object->symtab_index(r_sym); | |
507 | gold_assert(new_symndx != -1U); | |
508 | break; | |
509 | ||
510 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
7019cd25 | 511 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
6a74a719 ILT |
512 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
513 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
514 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
515 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
516 | { | |
517 | // We are adjusting a section symbol. We need to find | |
518 | // the symbol table index of the section symbol for | |
519 | // the output section corresponding to input section | |
520 | // in which this symbol is defined. | |
521 | gold_assert(r_sym < local_count); | |
d491d34e ILT |
522 | bool is_ordinary; |
523 | unsigned int shndx = | |
524 | object->local_symbol_input_shndx(r_sym, &is_ordinary); | |
525 | gold_assert(is_ordinary); | |
ef9beddf | 526 | Output_section* os = object->output_section(shndx); |
6a74a719 ILT |
527 | gold_assert(os != NULL); |
528 | gold_assert(os->needs_symtab_index()); | |
529 | new_symndx = os->symtab_index(); | |
530 | } | |
531 | break; | |
532 | ||
533 | default: | |
534 | gold_unreachable(); | |
535 | } | |
536 | } | |
537 | else | |
538 | { | |
539 | const Symbol* gsym = object->global_symbol(r_sym); | |
540 | gold_assert(gsym != NULL); | |
541 | if (gsym->is_forwarder()) | |
542 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
543 | ||
544 | gold_assert(gsym->has_symtab_index()); | |
545 | new_symndx = gsym->symtab_index(); | |
546 | } | |
547 | ||
548 | // Get the new offset--the location in the output section where | |
549 | // this relocation should be applied. | |
550 | ||
ef9beddf ILT |
551 | Address offset = reloc.get_r_offset(); |
552 | Address new_offset; | |
eff45813 | 553 | if (offset_in_output_section != invalid_address) |
6a74a719 ILT |
554 | new_offset = offset + offset_in_output_section; |
555 | else | |
556 | { | |
ef9beddf ILT |
557 | section_offset_type sot_offset = |
558 | convert_types<section_offset_type, Address>(offset); | |
559 | section_offset_type new_sot_offset = | |
560 | output_section->output_offset(object, relinfo->data_shndx, | |
561 | sot_offset); | |
562 | gold_assert(new_sot_offset != -1); | |
563 | new_offset = new_sot_offset; | |
6a74a719 ILT |
564 | } |
565 | ||
6be6f3bd ILT |
566 | // In an object file, r_offset is an offset within the section. |
567 | // In an executable or dynamic object, generated by | |
568 | // --emit-relocs, r_offset is an absolute address. | |
569 | if (!parameters->options().relocatable()) | |
e09ad04a ILT |
570 | { |
571 | new_offset += view_address; | |
eff45813 | 572 | if (offset_in_output_section != invalid_address) |
e09ad04a ILT |
573 | new_offset -= offset_in_output_section; |
574 | } | |
6be6f3bd | 575 | |
6a74a719 ILT |
576 | reloc_write.put_r_offset(new_offset); |
577 | reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type)); | |
578 | ||
579 | // Handle the reloc addend based on the strategy. | |
580 | ||
581 | if (strategy == Relocatable_relocs::RELOC_COPY) | |
582 | { | |
583 | if (sh_type == elfcpp::SHT_RELA) | |
584 | Reloc_types<sh_type, size, big_endian>:: | |
585 | copy_reloc_addend(&reloc_write, | |
586 | &reloc); | |
587 | } | |
588 | else | |
589 | { | |
590 | // The relocation uses a section symbol in the input file. | |
591 | // We are adjusting it to use a section symbol in the output | |
592 | // file. The input section symbol refers to some address in | |
593 | // the input section. We need the relocation in the output | |
594 | // file to refer to that same address. This adjustment to | |
595 | // the addend is the same calculation we use for a simple | |
596 | // absolute relocation for the input section symbol. | |
597 | ||
598 | const Symbol_value<size>* psymval = object->local_symbol(r_sym); | |
599 | ||
600 | unsigned char* padd = view + offset; | |
601 | switch (strategy) | |
602 | { | |
603 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
604 | { | |
605 | typename elfcpp::Elf_types<size>::Elf_Swxword addend; | |
606 | addend = Reloc_types<sh_type, size, big_endian>:: | |
607 | get_reloc_addend(&reloc); | |
608 | addend = psymval->value(object, addend); | |
609 | Reloc_types<sh_type, size, big_endian>:: | |
610 | set_reloc_addend(&reloc_write, addend); | |
611 | } | |
612 | break; | |
613 | ||
7019cd25 ILT |
614 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
615 | break; | |
616 | ||
6a74a719 ILT |
617 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
618 | Relocate_functions<size, big_endian>::rel8(padd, object, | |
619 | psymval); | |
620 | break; | |
621 | ||
622 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
623 | Relocate_functions<size, big_endian>::rel16(padd, object, | |
624 | psymval); | |
625 | break; | |
626 | ||
627 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
628 | Relocate_functions<size, big_endian>::rel32(padd, object, | |
629 | psymval); | |
630 | break; | |
631 | ||
632 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
633 | Relocate_functions<size, big_endian>::rel64(padd, object, | |
634 | psymval); | |
635 | break; | |
636 | ||
637 | default: | |
638 | gold_unreachable(); | |
639 | } | |
640 | } | |
641 | ||
642 | pwrite += reloc_size; | |
643 | } | |
644 | ||
645 | gold_assert(static_cast<section_size_type>(pwrite - reloc_view) | |
646 | == reloc_view_size); | |
647 | } | |
648 | ||
61ba1cf9 ILT |
649 | } // End namespace gold. |
650 | ||
651 | #endif // !defined(GOLD_TARGET_RELOC_H) |