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