Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
2 | ||
880cd20d | 3 | // Copyright 2006, 2007, 2008, 2009, 2010 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 |
ce97fa81 ST |
86 | && !object->is_section_included(shndx) |
87 | && !symtab->is_section_folded(object, shndx)) | |
92e059d8 ILT |
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 | } | |
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 | ||
837504c4 ILT |
147 | // Give an error for a symbol with non-default visibility which is not |
148 | // defined locally. | |
149 | ||
150 | inline void | |
151 | visibility_error(const Symbol* sym) | |
152 | { | |
153 | const char* v; | |
154 | switch (sym->visibility()) | |
155 | { | |
156 | case elfcpp::STV_INTERNAL: | |
157 | v = _("internal"); | |
158 | break; | |
159 | case elfcpp::STV_HIDDEN: | |
160 | v = _("hidden"); | |
161 | break; | |
162 | case elfcpp::STV_PROTECTED: | |
163 | v = _("protected"); | |
164 | break; | |
165 | default: | |
166 | gold_unreachable(); | |
167 | } | |
168 | gold_error(_("%s symbol '%s' is not defined locally"), | |
169 | v, sym->name()); | |
170 | } | |
171 | ||
92e059d8 | 172 | // This function implements the generic part of relocation processing. |
6a74a719 ILT |
173 | // The template parameter Relocate must be a class type which provides |
174 | // a single function, relocate(), which implements the machine | |
175 | // specific part of a relocation. | |
61ba1cf9 ILT |
176 | |
177 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of | |
92e059d8 ILT |
178 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
179 | // RELOCATE implements operator() to do a relocation. | |
61ba1cf9 | 180 | |
92e059d8 | 181 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
730cdc88 ILT |
182 | // of relocs. OUTPUT_SECTION is the output section. |
183 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be | |
184 | // mapped to output offsets. | |
185 | ||
186 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and | |
187 | // VIEW_SIZE is the size. These refer to the input section, unless | |
188 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to | |
189 | // the output section. | |
61ba1cf9 | 190 | |
364c7fa5 ILT |
191 | // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is |
192 | // not NULL, it is a vector indexed by relocation index. If that | |
193 | // entry is not NULL, it points to a global symbol which used as the | |
194 | // symbol for the relocation, ignoring the symbol index in the | |
195 | // relocation. | |
196 | ||
ead1e424 ILT |
197 | template<int size, bool big_endian, typename Target_type, int sh_type, |
198 | typename Relocate> | |
61ba1cf9 ILT |
199 | inline void |
200 | relocate_section( | |
92e059d8 | 201 | const Relocate_info<size, big_endian>* relinfo, |
ead1e424 | 202 | Target_type* target, |
61ba1cf9 ILT |
203 | const unsigned char* prelocs, |
204 | size_t reloc_count, | |
730cdc88 ILT |
205 | Output_section* output_section, |
206 | bool needs_special_offset_handling, | |
61ba1cf9 ILT |
207 | unsigned char* view, |
208 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
364c7fa5 ILT |
209 | section_size_type view_size, |
210 | const Reloc_symbol_changes* reloc_symbol_changes) | |
61ba1cf9 ILT |
211 | { |
212 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
213 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
214 | Relocate relocate; | |
215 | ||
730cdc88 ILT |
216 | Sized_relobj<size, big_endian>* object = relinfo->object; |
217 | unsigned int local_count = object->local_symbol_count(); | |
92e059d8 | 218 | |
e94cf127 CC |
219 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; |
220 | ||
61ba1cf9 ILT |
221 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
222 | { | |
223 | Reltype reloc(prelocs); | |
224 | ||
fe8718a4 ILT |
225 | section_offset_type offset = |
226 | convert_to_section_size_type(reloc.get_r_offset()); | |
61ba1cf9 | 227 | |
730cdc88 ILT |
228 | if (needs_special_offset_handling) |
229 | { | |
230 | offset = output_section->output_offset(relinfo->object, | |
231 | relinfo->data_shndx, | |
232 | offset); | |
233 | if (offset == -1) | |
234 | continue; | |
235 | } | |
236 | ||
61ba1cf9 ILT |
237 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); |
238 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
239 | unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
240 | ||
c06b7b0b | 241 | const Sized_symbol<size>* sym; |
61ba1cf9 | 242 | |
b8e6aad9 ILT |
243 | Symbol_value<size> symval; |
244 | const Symbol_value<size> *psymval; | |
880cd20d ILT |
245 | bool is_defined_in_discarded_section; |
246 | unsigned int shndx; | |
364c7fa5 ILT |
247 | if (r_sym < local_count |
248 | && (reloc_symbol_changes == NULL | |
249 | || (*reloc_symbol_changes)[i] == NULL)) | |
61ba1cf9 ILT |
250 | { |
251 | sym = NULL; | |
730cdc88 | 252 | psymval = object->local_symbol(r_sym); |
e94cf127 CC |
253 | |
254 | // If the local symbol belongs to a section we are discarding, | |
255 | // and that section is a debug section, try to find the | |
256 | // corresponding kept section and map this symbol to its | |
ef15dade ST |
257 | // counterpart in the kept section. The symbol must not |
258 | // correspond to a section we are folding. | |
e94cf127 | 259 | bool is_ordinary; |
880cd20d ILT |
260 | shndx = psymval->input_shndx(&is_ordinary); |
261 | is_defined_in_discarded_section = | |
262 | (is_ordinary | |
263 | && shndx != elfcpp::SHN_UNDEF | |
264 | && !object->is_section_included(shndx) | |
265 | && !relinfo->symtab->is_section_folded(object, shndx)); | |
61ba1cf9 ILT |
266 | } |
267 | else | |
268 | { | |
364c7fa5 ILT |
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 | } | |
61ba1cf9 | 280 | |
c06b7b0b | 281 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
d3bbad62 | 282 | if (sym->has_symtab_index() && sym->symtab_index() != -1U) |
b8e6aad9 ILT |
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; | |
880cd20d ILT |
288 | |
289 | is_defined_in_discarded_section = | |
290 | (gsym->is_defined_in_discarded_section() | |
291 | && gsym->is_undefined()); | |
292 | shndx = 0; | |
293 | } | |
294 | ||
295 | Symbol_value<size> symval2; | |
296 | if (is_defined_in_discarded_section) | |
297 | { | |
298 | if (comdat_behavior == CB_UNDETERMINED) | |
299 | { | |
300 | std::string name = object->section_name(relinfo->data_shndx); | |
301 | comdat_behavior = get_comdat_behavior(name.c_str()); | |
302 | } | |
303 | if (comdat_behavior == CB_PRETEND) | |
304 | { | |
305 | // FIXME: This case does not work for global symbols. | |
306 | // We have no place to store the original section index. | |
307 | // Fortunately this does not matter for comdat sections, | |
308 | // only for sections explicitly discarded by a linker | |
309 | // script. | |
310 | bool found; | |
311 | typename elfcpp::Elf_types<size>::Elf_Addr value = | |
312 | object->map_to_kept_section(shndx, &found); | |
313 | if (found) | |
314 | symval2.set_output_value(value + psymval->input_value()); | |
315 | else | |
316 | symval2.set_output_value(0); | |
317 | } | |
318 | else | |
319 | { | |
320 | if (comdat_behavior == CB_WARNING) | |
321 | gold_warning_at_location(relinfo, i, offset, | |
322 | _("relocation refers to discarded " | |
323 | "section")); | |
324 | symval2.set_output_value(0); | |
325 | } | |
326 | symval2.set_no_output_symtab_entry(); | |
327 | psymval = &symval2; | |
ead1e424 | 328 | } |
61ba1cf9 | 329 | |
031cdbed ILT |
330 | if (!relocate.relocate(relinfo, target, output_section, i, reloc, |
331 | r_type, sym, psymval, view + offset, | |
332 | view_address + offset, view_size)) | |
ead1e424 ILT |
333 | continue; |
334 | ||
fe8718a4 | 335 | if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) |
ead1e424 | 336 | { |
75f2446e ILT |
337 | gold_error_at_location(relinfo, i, offset, |
338 | _("reloc has bad offset %zu"), | |
339 | static_cast<size_t>(offset)); | |
340 | continue; | |
61ba1cf9 ILT |
341 | } |
342 | ||
ead1e424 ILT |
343 | if (sym != NULL |
344 | && sym->is_undefined() | |
436ca963 | 345 | && sym->binding() != elfcpp::STB_WEAK |
880cd20d | 346 | && !is_defined_in_discarded_section |
9c2d0ef9 | 347 | && !target->is_defined_by_abi(sym) |
eb42429a ILT |
348 | && (!parameters->options().shared() // -shared |
349 | || parameters->options().defs())) // -z defs | |
f073bbf7 | 350 | gold_undefined_symbol_at_location(sym, relinfo, i, offset); |
837504c4 ILT |
351 | else if (sym != NULL |
352 | && sym->visibility() != elfcpp::STV_DEFAULT | |
353 | && (sym->is_undefined() || sym->is_from_dynobj())) | |
354 | visibility_error(sym); | |
f6ce93d6 ILT |
355 | |
356 | if (sym != NULL && sym->has_warning()) | |
75f2446e | 357 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
61ba1cf9 ILT |
358 | } |
359 | } | |
360 | ||
6a74a719 ILT |
361 | // This class may be used as a typical class for the |
362 | // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The | |
363 | // template parameter Classify_reloc must be a class type which | |
364 | // provides a function get_size_for_reloc which returns the number of | |
365 | // bytes to which a reloc applies. This class is intended to capture | |
366 | // the most typical target behaviour, while still permitting targets | |
367 | // to define their own independent class for Scan_relocatable_reloc. | |
368 | ||
369 | template<int sh_type, typename Classify_reloc> | |
370 | class Default_scan_relocatable_relocs | |
371 | { | |
372 | public: | |
373 | // Return the strategy to use for a local symbol which is not a | |
374 | // section symbol, given the relocation type. | |
375 | inline Relocatable_relocs::Reloc_strategy | |
68943102 | 376 | local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) |
c2508178 ILT |
377 | { |
378 | // We assume that relocation type 0 is NONE. Targets which are | |
379 | // different must override. | |
68943102 | 380 | if (r_type == 0 && r_sym == 0) |
c2508178 ILT |
381 | return Relocatable_relocs::RELOC_DISCARD; |
382 | return Relocatable_relocs::RELOC_COPY; | |
383 | } | |
6a74a719 ILT |
384 | |
385 | // Return the strategy to use for a local symbol which is a section | |
386 | // symbol, given the relocation type. | |
387 | inline Relocatable_relocs::Reloc_strategy | |
388 | local_section_strategy(unsigned int r_type, Relobj* object) | |
389 | { | |
390 | if (sh_type == elfcpp::SHT_RELA) | |
391 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; | |
392 | else | |
393 | { | |
394 | Classify_reloc classify; | |
395 | switch (classify.get_size_for_reloc(r_type, object)) | |
396 | { | |
397 | case 0: | |
7019cd25 | 398 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
6a74a719 ILT |
399 | case 1: |
400 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; | |
401 | case 2: | |
402 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; | |
403 | case 4: | |
404 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; | |
405 | case 8: | |
406 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; | |
407 | default: | |
408 | gold_unreachable(); | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | // Return the strategy to use for a global symbol, given the | |
414 | // relocation type, the object, and the symbol index. | |
415 | inline Relocatable_relocs::Reloc_strategy | |
68943102 ILT |
416 | global_strategy(unsigned int, Relobj*, unsigned int) |
417 | { return Relocatable_relocs::RELOC_COPY; } | |
6a74a719 ILT |
418 | }; |
419 | ||
420 | // Scan relocs during a relocatable link. This is a default | |
421 | // definition which should work for most targets. | |
422 | // Scan_relocatable_reloc must name a class type which provides three | |
423 | // functions which return a Relocatable_relocs::Reloc_strategy code: | |
424 | // global_strategy, local_non_section_strategy, and | |
425 | // local_section_strategy. Most targets should be able to use | |
426 | // Default_scan_relocatable_relocs as this class. | |
427 | ||
7019cd25 | 428 | template<int size, bool big_endian, int sh_type, |
6a74a719 ILT |
429 | typename Scan_relocatable_reloc> |
430 | void | |
431 | scan_relocatable_relocs( | |
6a74a719 ILT |
432 | Symbol_table*, |
433 | Layout*, | |
434 | Sized_relobj<size, big_endian>* object, | |
435 | unsigned int data_shndx, | |
436 | const unsigned char* prelocs, | |
437 | size_t reloc_count, | |
438 | Output_section* output_section, | |
439 | bool needs_special_offset_handling, | |
440 | size_t local_symbol_count, | |
441 | const unsigned char* plocal_syms, | |
442 | Relocatable_relocs* rr) | |
443 | { | |
444 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
445 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
446 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
447 | Scan_relocatable_reloc scan; | |
448 | ||
449 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
450 | { | |
451 | Reltype reloc(prelocs); | |
452 | ||
453 | Relocatable_relocs::Reloc_strategy strategy; | |
454 | ||
455 | if (needs_special_offset_handling | |
456 | && !output_section->is_input_address_mapped(object, data_shndx, | |
457 | reloc.get_r_offset())) | |
458 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
459 | else | |
460 | { | |
461 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = | |
462 | reloc.get_r_info(); | |
463 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
464 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
465 | ||
466 | if (r_sym >= local_symbol_count) | |
467 | strategy = scan.global_strategy(r_type, object, r_sym); | |
468 | else | |
469 | { | |
470 | gold_assert(plocal_syms != NULL); | |
471 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms | |
472 | + r_sym * sym_size); | |
d491d34e ILT |
473 | unsigned int shndx = lsym.get_st_shndx(); |
474 | bool is_ordinary; | |
475 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
476 | if (is_ordinary | |
6a74a719 | 477 | && shndx != elfcpp::SHN_UNDEF |
d491d34e | 478 | && !object->is_section_included(shndx)) |
6a74a719 ILT |
479 | { |
480 | // RELOC is a relocation against a local symbol | |
481 | // defined in a section we are discarding. Discard | |
482 | // the reloc. FIXME: Should we issue a warning? | |
483 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
484 | } | |
485 | else if (lsym.get_st_type() != elfcpp::STT_SECTION) | |
68943102 ILT |
486 | strategy = scan.local_non_section_strategy(r_type, object, |
487 | r_sym); | |
6a74a719 ILT |
488 | else |
489 | { | |
490 | strategy = scan.local_section_strategy(r_type, object); | |
491 | if (strategy != Relocatable_relocs::RELOC_DISCARD) | |
ef9beddf | 492 | object->output_section(shndx)->set_needs_symtab_index(); |
6a74a719 | 493 | } |
d3bbad62 ILT |
494 | |
495 | if (strategy == Relocatable_relocs::RELOC_COPY) | |
496 | object->set_must_have_output_symtab_entry(r_sym); | |
6a74a719 ILT |
497 | } |
498 | } | |
499 | ||
500 | rr->set_next_reloc_strategy(strategy); | |
501 | } | |
502 | } | |
503 | ||
504 | // Relocate relocs during a relocatable link. This is a default | |
505 | // definition which should work for most targets. | |
506 | ||
7019cd25 | 507 | template<int size, bool big_endian, int sh_type> |
6a74a719 ILT |
508 | void |
509 | relocate_for_relocatable( | |
510 | const Relocate_info<size, big_endian>* relinfo, | |
511 | const unsigned char* prelocs, | |
512 | size_t reloc_count, | |
513 | Output_section* output_section, | |
ef9beddf | 514 | typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, |
6a74a719 ILT |
515 | const Relocatable_relocs* rr, |
516 | unsigned char* view, | |
6be6f3bd | 517 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
5c388529 | 518 | section_size_type view_size, |
6a74a719 ILT |
519 | unsigned char* reloc_view, |
520 | section_size_type reloc_view_size) | |
521 | { | |
ef9beddf | 522 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
6a74a719 ILT |
523 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; |
524 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write | |
525 | Reltype_write; | |
526 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
eff45813 | 527 | const Address invalid_address = static_cast<Address>(0) - 1; |
6a74a719 ILT |
528 | |
529 | Sized_relobj<size, big_endian>* const object = relinfo->object; | |
530 | const unsigned int local_count = object->local_symbol_count(); | |
531 | ||
532 | unsigned char* pwrite = reloc_view; | |
533 | ||
534 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
535 | { | |
536 | Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i); | |
537 | if (strategy == Relocatable_relocs::RELOC_DISCARD) | |
538 | continue; | |
539 | ||
5c388529 DK |
540 | if (strategy == Relocatable_relocs::RELOC_SPECIAL) |
541 | { | |
542 | // Target wants to handle this relocation. | |
543 | Sized_target<size, big_endian>* target = | |
544 | parameters->sized_target<size, big_endian>(); | |
545 | target->relocate_special_relocatable(relinfo, sh_type, prelocs, | |
546 | i, output_section, | |
547 | offset_in_output_section, | |
548 | view, view_address, | |
549 | view_size, pwrite); | |
550 | pwrite += reloc_size; | |
551 | continue; | |
552 | } | |
6a74a719 ILT |
553 | Reltype reloc(prelocs); |
554 | Reltype_write reloc_write(pwrite); | |
555 | ||
556 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
557 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
558 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
559 | ||
560 | // Get the new symbol index. | |
561 | ||
562 | unsigned int new_symndx; | |
563 | if (r_sym < local_count) | |
564 | { | |
565 | switch (strategy) | |
566 | { | |
567 | case Relocatable_relocs::RELOC_COPY: | |
818bf354 ILT |
568 | if (r_sym == 0) |
569 | new_symndx = 0; | |
570 | else | |
571 | { | |
572 | new_symndx = object->symtab_index(r_sym); | |
573 | gold_assert(new_symndx != -1U); | |
574 | } | |
6a74a719 ILT |
575 | break; |
576 | ||
577 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
7019cd25 | 578 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
6a74a719 ILT |
579 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
580 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
581 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
582 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
583 | { | |
584 | // We are adjusting a section symbol. We need to find | |
585 | // the symbol table index of the section symbol for | |
586 | // the output section corresponding to input section | |
587 | // in which this symbol is defined. | |
588 | gold_assert(r_sym < local_count); | |
d491d34e ILT |
589 | bool is_ordinary; |
590 | unsigned int shndx = | |
591 | object->local_symbol_input_shndx(r_sym, &is_ordinary); | |
592 | gold_assert(is_ordinary); | |
ef9beddf | 593 | Output_section* os = object->output_section(shndx); |
6a74a719 ILT |
594 | gold_assert(os != NULL); |
595 | gold_assert(os->needs_symtab_index()); | |
596 | new_symndx = os->symtab_index(); | |
597 | } | |
598 | break; | |
599 | ||
600 | default: | |
601 | gold_unreachable(); | |
602 | } | |
603 | } | |
604 | else | |
605 | { | |
606 | const Symbol* gsym = object->global_symbol(r_sym); | |
607 | gold_assert(gsym != NULL); | |
608 | if (gsym->is_forwarder()) | |
609 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
610 | ||
611 | gold_assert(gsym->has_symtab_index()); | |
612 | new_symndx = gsym->symtab_index(); | |
613 | } | |
614 | ||
615 | // Get the new offset--the location in the output section where | |
616 | // this relocation should be applied. | |
617 | ||
ef9beddf ILT |
618 | Address offset = reloc.get_r_offset(); |
619 | Address new_offset; | |
eff45813 | 620 | if (offset_in_output_section != invalid_address) |
6a74a719 ILT |
621 | new_offset = offset + offset_in_output_section; |
622 | else | |
623 | { | |
ef9beddf ILT |
624 | section_offset_type sot_offset = |
625 | convert_types<section_offset_type, Address>(offset); | |
626 | section_offset_type new_sot_offset = | |
627 | output_section->output_offset(object, relinfo->data_shndx, | |
628 | sot_offset); | |
629 | gold_assert(new_sot_offset != -1); | |
630 | new_offset = new_sot_offset; | |
6a74a719 ILT |
631 | } |
632 | ||
6be6f3bd ILT |
633 | // In an object file, r_offset is an offset within the section. |
634 | // In an executable or dynamic object, generated by | |
635 | // --emit-relocs, r_offset is an absolute address. | |
636 | if (!parameters->options().relocatable()) | |
e09ad04a ILT |
637 | { |
638 | new_offset += view_address; | |
eff45813 | 639 | if (offset_in_output_section != invalid_address) |
e09ad04a ILT |
640 | new_offset -= offset_in_output_section; |
641 | } | |
6be6f3bd | 642 | |
6a74a719 ILT |
643 | reloc_write.put_r_offset(new_offset); |
644 | reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type)); | |
645 | ||
646 | // Handle the reloc addend based on the strategy. | |
647 | ||
648 | if (strategy == Relocatable_relocs::RELOC_COPY) | |
649 | { | |
650 | if (sh_type == elfcpp::SHT_RELA) | |
651 | Reloc_types<sh_type, size, big_endian>:: | |
652 | copy_reloc_addend(&reloc_write, | |
653 | &reloc); | |
654 | } | |
655 | else | |
656 | { | |
657 | // The relocation uses a section symbol in the input file. | |
658 | // We are adjusting it to use a section symbol in the output | |
659 | // file. The input section symbol refers to some address in | |
660 | // the input section. We need the relocation in the output | |
661 | // file to refer to that same address. This adjustment to | |
662 | // the addend is the same calculation we use for a simple | |
663 | // absolute relocation for the input section symbol. | |
664 | ||
665 | const Symbol_value<size>* psymval = object->local_symbol(r_sym); | |
666 | ||
667 | unsigned char* padd = view + offset; | |
668 | switch (strategy) | |
669 | { | |
670 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
671 | { | |
672 | typename elfcpp::Elf_types<size>::Elf_Swxword addend; | |
673 | addend = Reloc_types<sh_type, size, big_endian>:: | |
674 | get_reloc_addend(&reloc); | |
675 | addend = psymval->value(object, addend); | |
676 | Reloc_types<sh_type, size, big_endian>:: | |
677 | set_reloc_addend(&reloc_write, addend); | |
678 | } | |
679 | break; | |
680 | ||
7019cd25 ILT |
681 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
682 | break; | |
683 | ||
6a74a719 ILT |
684 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
685 | Relocate_functions<size, big_endian>::rel8(padd, object, | |
686 | psymval); | |
687 | break; | |
688 | ||
689 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
690 | Relocate_functions<size, big_endian>::rel16(padd, object, | |
691 | psymval); | |
692 | break; | |
693 | ||
694 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
695 | Relocate_functions<size, big_endian>::rel32(padd, object, | |
696 | psymval); | |
697 | break; | |
698 | ||
699 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
700 | Relocate_functions<size, big_endian>::rel64(padd, object, | |
701 | psymval); | |
702 | break; | |
703 | ||
704 | default: | |
705 | gold_unreachable(); | |
706 | } | |
707 | } | |
708 | ||
709 | pwrite += reloc_size; | |
710 | } | |
711 | ||
712 | gold_assert(static_cast<section_size_type>(pwrite - reloc_view) | |
713 | == reloc_view_size); | |
714 | } | |
715 | ||
61ba1cf9 ILT |
716 | } // End namespace gold. |
717 | ||
718 | #endif // !defined(GOLD_TARGET_RELOC_H) |