Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
2 | ||
191f1a2d | 3 | // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 | |
9b547ce6 | 39 | // avoid making a function call for each relocation, and to avoid |
6a74a719 | 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, | |
6fa2a40b | 49 | Sized_relobj_file<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); | |
bfdfa4cd AM |
84 | // If RELOC is a relocation against a local symbol in a |
85 | // section we are discarding then we can ignore it. It will | |
86 | // eventually become a reloc against the value zero. | |
87 | // | |
88 | // FIXME: We should issue a warning if this is an | |
89 | // allocated section; is this the best place to do it? | |
90 | // | |
91 | // FIXME: The old GNU linker would in some cases look | |
92 | // for the linkonce section which caused this section to | |
93 | // be discarded, and, if the other section was the same | |
94 | // size, change the reloc to refer to the other section. | |
95 | // That seems risky and weird to me, and I don't know of | |
96 | // any case where it is actually required. | |
97 | bool is_discarded = (is_ordinary | |
98 | && shndx != elfcpp::SHN_UNDEF | |
99 | && !object->is_section_included(shndx) | |
100 | && !symtab->is_section_folded(object, shndx)); | |
ad0f2072 | 101 | scan.local(symtab, layout, target, object, data_shndx, |
bfdfa4cd | 102 | output_section, reloc, r_type, lsym, is_discarded); |
92e059d8 ILT |
103 | } |
104 | else | |
105 | { | |
730cdc88 | 106 | Symbol* gsym = object->global_symbol(r_sym); |
a3ad94ed | 107 | gold_assert(gsym != NULL); |
92e059d8 ILT |
108 | if (gsym->is_forwarder()) |
109 | gsym = symtab->resolve_forwards(gsym); | |
110 | ||
ad0f2072 | 111 | scan.global(symtab, layout, target, object, data_shndx, |
07f397ab | 112 | output_section, reloc, r_type, gsym); |
92e059d8 ILT |
113 | } |
114 | } | |
115 | } | |
116 | ||
e94cf127 CC |
117 | // Behavior for relocations to discarded comdat sections. |
118 | ||
119 | enum Comdat_behavior | |
120 | { | |
121 | CB_UNDETERMINED, // Not yet determined -- need to look at section name. | |
122 | CB_PRETEND, // Attempt to map to the corresponding kept section. | |
123 | CB_IGNORE, // Ignore the relocation. | |
124 | CB_WARNING // Print a warning. | |
125 | }; | |
126 | ||
168a4726 | 127 | class Default_comdat_behavior |
e94cf127 | 128 | { |
168a4726 AM |
129 | public: |
130 | // Decide what the linker should do for relocations that refer to | |
131 | // discarded comdat sections. This decision is based on the name of | |
132 | // the section being relocated. | |
133 | ||
134 | inline Comdat_behavior | |
135 | get(const char* name) | |
136 | { | |
137 | if (Layout::is_debug_info_section(name)) | |
138 | return CB_PRETEND; | |
139 | if (strcmp(name, ".eh_frame") == 0 | |
140 | || strcmp(name, ".gcc_except_table") == 0) | |
141 | return CB_IGNORE; | |
142 | return CB_WARNING; | |
143 | } | |
144 | }; | |
e94cf127 | 145 | |
837504c4 ILT |
146 | // Give an error for a symbol with non-default visibility which is not |
147 | // defined locally. | |
148 | ||
149 | inline void | |
150 | visibility_error(const Symbol* sym) | |
151 | { | |
152 | const char* v; | |
153 | switch (sym->visibility()) | |
154 | { | |
155 | case elfcpp::STV_INTERNAL: | |
156 | v = _("internal"); | |
157 | break; | |
158 | case elfcpp::STV_HIDDEN: | |
159 | v = _("hidden"); | |
160 | break; | |
161 | case elfcpp::STV_PROTECTED: | |
162 | v = _("protected"); | |
163 | break; | |
164 | default: | |
165 | gold_unreachable(); | |
166 | } | |
167 | gold_error(_("%s symbol '%s' is not defined locally"), | |
168 | v, sym->name()); | |
169 | } | |
170 | ||
191f1a2d ILT |
171 | // Return true if we are should issue an error saying that SYM is an |
172 | // undefined symbol. This is called if there is a relocation against | |
173 | // SYM. | |
174 | ||
175 | inline bool | |
176 | issue_undefined_symbol_error(const Symbol* sym) | |
177 | { | |
beabb2c6 ILT |
178 | // We only report global symbols. |
179 | if (sym == NULL) | |
180 | return false; | |
181 | ||
182 | // We only report undefined symbols. | |
183 | if (!sym->is_undefined() && !sym->is_placeholder()) | |
184 | return false; | |
185 | ||
186 | // We don't report weak symbols. | |
187 | if (sym->binding() == elfcpp::STB_WEAK) | |
188 | return false; | |
189 | ||
190 | // We don't report symbols defined in discarded sections. | |
191 | if (sym->is_defined_in_discarded_section()) | |
192 | return false; | |
193 | ||
194 | // If the target defines this symbol, don't report it here. | |
195 | if (parameters->target().is_defined_by_abi(sym)) | |
196 | return false; | |
197 | ||
198 | // See if we've been told to ignore whether this symbol is | |
199 | // undefined. | |
200 | const char* const u = parameters->options().unresolved_symbols(); | |
201 | if (u != NULL) | |
202 | { | |
203 | if (strcmp(u, "ignore-all") == 0) | |
204 | return false; | |
e2153196 ILT |
205 | if (strcmp(u, "report-all") == 0) |
206 | return true; | |
beabb2c6 ILT |
207 | if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn()) |
208 | return false; | |
209 | if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg()) | |
210 | return false; | |
211 | } | |
212 | ||
213 | // When creating a shared library, only report unresolved symbols if | |
214 | // -z defs was used. | |
215 | if (parameters->options().shared() && !parameters->options().defs()) | |
216 | return false; | |
217 | ||
218 | // Otherwise issue a warning. | |
219 | return true; | |
191f1a2d ILT |
220 | } |
221 | ||
92e059d8 | 222 | // This function implements the generic part of relocation processing. |
6a74a719 ILT |
223 | // The template parameter Relocate must be a class type which provides |
224 | // a single function, relocate(), which implements the machine | |
225 | // specific part of a relocation. | |
61ba1cf9 | 226 | |
168a4726 AM |
227 | // The template parameter Relocate_comdat_behavior is a class type |
228 | // which provides a single function, get(), which determines what the | |
229 | // linker should do for relocations that refer to discarded comdat | |
230 | // sections. | |
231 | ||
61ba1cf9 | 232 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of |
92e059d8 ILT |
233 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
234 | // RELOCATE implements operator() to do a relocation. | |
61ba1cf9 | 235 | |
92e059d8 | 236 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
730cdc88 ILT |
237 | // of relocs. OUTPUT_SECTION is the output section. |
238 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be | |
239 | // mapped to output offsets. | |
240 | ||
241 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and | |
242 | // VIEW_SIZE is the size. These refer to the input section, unless | |
243 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to | |
244 | // the output section. | |
61ba1cf9 | 245 | |
364c7fa5 ILT |
246 | // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is |
247 | // not NULL, it is a vector indexed by relocation index. If that | |
248 | // entry is not NULL, it points to a global symbol which used as the | |
249 | // symbol for the relocation, ignoring the symbol index in the | |
250 | // relocation. | |
251 | ||
ead1e424 | 252 | template<int size, bool big_endian, typename Target_type, int sh_type, |
168a4726 AM |
253 | typename Relocate, |
254 | typename Relocate_comdat_behavior> | |
61ba1cf9 ILT |
255 | inline void |
256 | relocate_section( | |
92e059d8 | 257 | const Relocate_info<size, big_endian>* relinfo, |
ead1e424 | 258 | Target_type* target, |
61ba1cf9 ILT |
259 | const unsigned char* prelocs, |
260 | size_t reloc_count, | |
730cdc88 ILT |
261 | Output_section* output_section, |
262 | bool needs_special_offset_handling, | |
61ba1cf9 ILT |
263 | unsigned char* view, |
264 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
364c7fa5 ILT |
265 | section_size_type view_size, |
266 | const Reloc_symbol_changes* reloc_symbol_changes) | |
61ba1cf9 ILT |
267 | { |
268 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
269 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
270 | Relocate relocate; | |
168a4726 | 271 | Relocate_comdat_behavior relocate_comdat_behavior; |
61ba1cf9 | 272 | |
6fa2a40b | 273 | Sized_relobj_file<size, big_endian>* object = relinfo->object; |
730cdc88 | 274 | unsigned int local_count = object->local_symbol_count(); |
92e059d8 | 275 | |
e94cf127 CC |
276 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; |
277 | ||
61ba1cf9 ILT |
278 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
279 | { | |
280 | Reltype reloc(prelocs); | |
281 | ||
fe8718a4 ILT |
282 | section_offset_type offset = |
283 | convert_to_section_size_type(reloc.get_r_offset()); | |
61ba1cf9 | 284 | |
730cdc88 ILT |
285 | if (needs_special_offset_handling) |
286 | { | |
287 | offset = output_section->output_offset(relinfo->object, | |
288 | relinfo->data_shndx, | |
289 | offset); | |
290 | if (offset == -1) | |
291 | continue; | |
292 | } | |
293 | ||
61ba1cf9 ILT |
294 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); |
295 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
296 | unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
297 | ||
c06b7b0b | 298 | const Sized_symbol<size>* sym; |
61ba1cf9 | 299 | |
b8e6aad9 ILT |
300 | Symbol_value<size> symval; |
301 | const Symbol_value<size> *psymval; | |
880cd20d ILT |
302 | bool is_defined_in_discarded_section; |
303 | unsigned int shndx; | |
364c7fa5 ILT |
304 | if (r_sym < local_count |
305 | && (reloc_symbol_changes == NULL | |
306 | || (*reloc_symbol_changes)[i] == NULL)) | |
61ba1cf9 ILT |
307 | { |
308 | sym = NULL; | |
730cdc88 | 309 | psymval = object->local_symbol(r_sym); |
e94cf127 CC |
310 | |
311 | // If the local symbol belongs to a section we are discarding, | |
312 | // and that section is a debug section, try to find the | |
313 | // corresponding kept section and map this symbol to its | |
ef15dade ST |
314 | // counterpart in the kept section. The symbol must not |
315 | // correspond to a section we are folding. | |
e94cf127 | 316 | bool is_ordinary; |
880cd20d ILT |
317 | shndx = psymval->input_shndx(&is_ordinary); |
318 | is_defined_in_discarded_section = | |
319 | (is_ordinary | |
320 | && shndx != elfcpp::SHN_UNDEF | |
321 | && !object->is_section_included(shndx) | |
322 | && !relinfo->symtab->is_section_folded(object, shndx)); | |
61ba1cf9 ILT |
323 | } |
324 | else | |
325 | { | |
364c7fa5 ILT |
326 | const Symbol* gsym; |
327 | if (reloc_symbol_changes != NULL | |
328 | && (*reloc_symbol_changes)[i] != NULL) | |
329 | gsym = (*reloc_symbol_changes)[i]; | |
330 | else | |
331 | { | |
332 | gsym = object->global_symbol(r_sym); | |
333 | gold_assert(gsym != NULL); | |
334 | if (gsym->is_forwarder()) | |
335 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
336 | } | |
61ba1cf9 | 337 | |
c06b7b0b | 338 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
d3bbad62 | 339 | if (sym->has_symtab_index() && sym->symtab_index() != -1U) |
b8e6aad9 ILT |
340 | symval.set_output_symtab_index(sym->symtab_index()); |
341 | else | |
342 | symval.set_no_output_symtab_entry(); | |
343 | symval.set_output_value(sym->value()); | |
7223e9ca ILT |
344 | if (gsym->type() == elfcpp::STT_TLS) |
345 | symval.set_is_tls_symbol(); | |
346 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) | |
347 | symval.set_is_ifunc_symbol(); | |
b8e6aad9 | 348 | psymval = &symval; |
880cd20d ILT |
349 | |
350 | is_defined_in_discarded_section = | |
351 | (gsym->is_defined_in_discarded_section() | |
352 | && gsym->is_undefined()); | |
353 | shndx = 0; | |
354 | } | |
355 | ||
356 | Symbol_value<size> symval2; | |
357 | if (is_defined_in_discarded_section) | |
358 | { | |
359 | if (comdat_behavior == CB_UNDETERMINED) | |
360 | { | |
361 | std::string name = object->section_name(relinfo->data_shndx); | |
168a4726 | 362 | comdat_behavior = relocate_comdat_behavior.get(name.c_str()); |
880cd20d ILT |
363 | } |
364 | if (comdat_behavior == CB_PRETEND) | |
365 | { | |
366 | // FIXME: This case does not work for global symbols. | |
367 | // We have no place to store the original section index. | |
368 | // Fortunately this does not matter for comdat sections, | |
369 | // only for sections explicitly discarded by a linker | |
370 | // script. | |
371 | bool found; | |
372 | typename elfcpp::Elf_types<size>::Elf_Addr value = | |
373 | object->map_to_kept_section(shndx, &found); | |
374 | if (found) | |
375 | symval2.set_output_value(value + psymval->input_value()); | |
376 | else | |
377 | symval2.set_output_value(0); | |
378 | } | |
379 | else | |
380 | { | |
381 | if (comdat_behavior == CB_WARNING) | |
382 | gold_warning_at_location(relinfo, i, offset, | |
383 | _("relocation refers to discarded " | |
384 | "section")); | |
385 | symval2.set_output_value(0); | |
386 | } | |
387 | symval2.set_no_output_symtab_entry(); | |
388 | psymval = &symval2; | |
ead1e424 | 389 | } |
61ba1cf9 | 390 | |
031cdbed ILT |
391 | if (!relocate.relocate(relinfo, target, output_section, i, reloc, |
392 | r_type, sym, psymval, view + offset, | |
393 | view_address + offset, view_size)) | |
ead1e424 ILT |
394 | continue; |
395 | ||
fe8718a4 | 396 | if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) |
ead1e424 | 397 | { |
75f2446e ILT |
398 | gold_error_at_location(relinfo, i, offset, |
399 | _("reloc has bad offset %zu"), | |
400 | static_cast<size_t>(offset)); | |
401 | continue; | |
61ba1cf9 ILT |
402 | } |
403 | ||
191f1a2d | 404 | if (issue_undefined_symbol_error(sym)) |
f073bbf7 | 405 | gold_undefined_symbol_at_location(sym, relinfo, i, offset); |
837504c4 ILT |
406 | else if (sym != NULL |
407 | && sym->visibility() != elfcpp::STV_DEFAULT | |
408 | && (sym->is_undefined() || sym->is_from_dynobj())) | |
409 | visibility_error(sym); | |
f6ce93d6 ILT |
410 | |
411 | if (sym != NULL && sym->has_warning()) | |
75f2446e | 412 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
61ba1cf9 ILT |
413 | } |
414 | } | |
415 | ||
94a3fc8b CC |
416 | // Apply an incremental relocation. |
417 | ||
418 | template<int size, bool big_endian, typename Target_type, | |
419 | typename Relocate> | |
420 | void | |
421 | apply_relocation(const Relocate_info<size, big_endian>* relinfo, | |
422 | Target_type* target, | |
423 | typename elfcpp::Elf_types<size>::Elf_Addr r_offset, | |
424 | unsigned int r_type, | |
425 | typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, | |
426 | const Symbol* gsym, | |
427 | unsigned char* view, | |
428 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
429 | section_size_type view_size) | |
430 | { | |
431 | // Construct the ELF relocation in a temporary buffer. | |
d55525b9 | 432 | const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; |
94a3fc8b | 433 | unsigned char relbuf[reloc_size]; |
d55525b9 L |
434 | elfcpp::Rela<size, big_endian> rel(relbuf); |
435 | elfcpp::Rela_write<size, big_endian> orel(relbuf); | |
94a3fc8b | 436 | orel.put_r_offset(r_offset); |
d55525b9 | 437 | orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type)); |
94a3fc8b CC |
438 | orel.put_r_addend(r_addend); |
439 | ||
440 | // Setup a Symbol_value for the global symbol. | |
d55525b9 L |
441 | const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym); |
442 | Symbol_value<size> symval; | |
94a3fc8b CC |
443 | gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U); |
444 | symval.set_output_symtab_index(sym->symtab_index()); | |
445 | symval.set_output_value(sym->value()); | |
446 | if (gsym->type() == elfcpp::STT_TLS) | |
447 | symval.set_is_tls_symbol(); | |
448 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) | |
449 | symval.set_is_ifunc_symbol(); | |
450 | ||
451 | Relocate relocate; | |
452 | relocate.relocate(relinfo, target, NULL, -1U, rel, r_type, sym, &symval, | |
453 | view + r_offset, address + r_offset, view_size); | |
454 | } | |
455 | ||
6a74a719 ILT |
456 | // This class may be used as a typical class for the |
457 | // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The | |
458 | // template parameter Classify_reloc must be a class type which | |
459 | // provides a function get_size_for_reloc which returns the number of | |
460 | // bytes to which a reloc applies. This class is intended to capture | |
461 | // the most typical target behaviour, while still permitting targets | |
462 | // to define their own independent class for Scan_relocatable_reloc. | |
463 | ||
464 | template<int sh_type, typename Classify_reloc> | |
465 | class Default_scan_relocatable_relocs | |
466 | { | |
467 | public: | |
468 | // Return the strategy to use for a local symbol which is not a | |
469 | // section symbol, given the relocation type. | |
470 | inline Relocatable_relocs::Reloc_strategy | |
68943102 | 471 | local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) |
c2508178 ILT |
472 | { |
473 | // We assume that relocation type 0 is NONE. Targets which are | |
474 | // different must override. | |
68943102 | 475 | if (r_type == 0 && r_sym == 0) |
c2508178 ILT |
476 | return Relocatable_relocs::RELOC_DISCARD; |
477 | return Relocatable_relocs::RELOC_COPY; | |
478 | } | |
6a74a719 ILT |
479 | |
480 | // Return the strategy to use for a local symbol which is a section | |
481 | // symbol, given the relocation type. | |
482 | inline Relocatable_relocs::Reloc_strategy | |
483 | local_section_strategy(unsigned int r_type, Relobj* object) | |
484 | { | |
485 | if (sh_type == elfcpp::SHT_RELA) | |
486 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; | |
487 | else | |
488 | { | |
489 | Classify_reloc classify; | |
490 | switch (classify.get_size_for_reloc(r_type, object)) | |
491 | { | |
492 | case 0: | |
7019cd25 | 493 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
6a74a719 ILT |
494 | case 1: |
495 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; | |
496 | case 2: | |
497 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; | |
498 | case 4: | |
499 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; | |
500 | case 8: | |
501 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; | |
502 | default: | |
503 | gold_unreachable(); | |
504 | } | |
505 | } | |
506 | } | |
507 | ||
508 | // Return the strategy to use for a global symbol, given the | |
509 | // relocation type, the object, and the symbol index. | |
510 | inline Relocatable_relocs::Reloc_strategy | |
68943102 ILT |
511 | global_strategy(unsigned int, Relobj*, unsigned int) |
512 | { return Relocatable_relocs::RELOC_COPY; } | |
6a74a719 ILT |
513 | }; |
514 | ||
515 | // Scan relocs during a relocatable link. This is a default | |
516 | // definition which should work for most targets. | |
517 | // Scan_relocatable_reloc must name a class type which provides three | |
518 | // functions which return a Relocatable_relocs::Reloc_strategy code: | |
519 | // global_strategy, local_non_section_strategy, and | |
520 | // local_section_strategy. Most targets should be able to use | |
521 | // Default_scan_relocatable_relocs as this class. | |
522 | ||
7019cd25 | 523 | template<int size, bool big_endian, int sh_type, |
6a74a719 ILT |
524 | typename Scan_relocatable_reloc> |
525 | void | |
526 | scan_relocatable_relocs( | |
6a74a719 ILT |
527 | Symbol_table*, |
528 | Layout*, | |
6fa2a40b | 529 | Sized_relobj_file<size, big_endian>* object, |
6a74a719 ILT |
530 | unsigned int data_shndx, |
531 | const unsigned char* prelocs, | |
532 | size_t reloc_count, | |
533 | Output_section* output_section, | |
534 | bool needs_special_offset_handling, | |
535 | size_t local_symbol_count, | |
536 | const unsigned char* plocal_syms, | |
537 | Relocatable_relocs* rr) | |
538 | { | |
539 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; | |
540 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
541 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
542 | Scan_relocatable_reloc scan; | |
543 | ||
544 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
545 | { | |
546 | Reltype reloc(prelocs); | |
547 | ||
548 | Relocatable_relocs::Reloc_strategy strategy; | |
549 | ||
550 | if (needs_special_offset_handling | |
551 | && !output_section->is_input_address_mapped(object, data_shndx, | |
552 | reloc.get_r_offset())) | |
553 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
554 | else | |
555 | { | |
556 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = | |
557 | reloc.get_r_info(); | |
558 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
559 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
560 | ||
561 | if (r_sym >= local_symbol_count) | |
562 | strategy = scan.global_strategy(r_type, object, r_sym); | |
563 | else | |
564 | { | |
565 | gold_assert(plocal_syms != NULL); | |
566 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms | |
567 | + r_sym * sym_size); | |
d491d34e ILT |
568 | unsigned int shndx = lsym.get_st_shndx(); |
569 | bool is_ordinary; | |
570 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); | |
571 | if (is_ordinary | |
6a74a719 | 572 | && shndx != elfcpp::SHN_UNDEF |
d491d34e | 573 | && !object->is_section_included(shndx)) |
6a74a719 ILT |
574 | { |
575 | // RELOC is a relocation against a local symbol | |
576 | // defined in a section we are discarding. Discard | |
577 | // the reloc. FIXME: Should we issue a warning? | |
578 | strategy = Relocatable_relocs::RELOC_DISCARD; | |
579 | } | |
580 | else if (lsym.get_st_type() != elfcpp::STT_SECTION) | |
68943102 ILT |
581 | strategy = scan.local_non_section_strategy(r_type, object, |
582 | r_sym); | |
6a74a719 ILT |
583 | else |
584 | { | |
585 | strategy = scan.local_section_strategy(r_type, object); | |
586 | if (strategy != Relocatable_relocs::RELOC_DISCARD) | |
ef9beddf | 587 | object->output_section(shndx)->set_needs_symtab_index(); |
6a74a719 | 588 | } |
d3bbad62 ILT |
589 | |
590 | if (strategy == Relocatable_relocs::RELOC_COPY) | |
591 | object->set_must_have_output_symtab_entry(r_sym); | |
6a74a719 ILT |
592 | } |
593 | } | |
594 | ||
595 | rr->set_next_reloc_strategy(strategy); | |
596 | } | |
597 | } | |
598 | ||
7404fe1b AM |
599 | // Relocate relocs. Called for a relocatable link, and for --emit-relocs. |
600 | // This is a default definition which should work for most targets. | |
6a74a719 | 601 | |
7019cd25 | 602 | template<int size, bool big_endian, int sh_type> |
6a74a719 | 603 | void |
7404fe1b | 604 | relocate_relocs( |
6a74a719 ILT |
605 | const Relocate_info<size, big_endian>* relinfo, |
606 | const unsigned char* prelocs, | |
607 | size_t reloc_count, | |
608 | Output_section* output_section, | |
ef9beddf | 609 | typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, |
6a74a719 ILT |
610 | const Relocatable_relocs* rr, |
611 | unsigned char* view, | |
6be6f3bd | 612 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
5c388529 | 613 | section_size_type view_size, |
6a74a719 ILT |
614 | unsigned char* reloc_view, |
615 | section_size_type reloc_view_size) | |
616 | { | |
ef9beddf | 617 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
6a74a719 ILT |
618 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; |
619 | typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write | |
620 | Reltype_write; | |
621 | const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; | |
eff45813 | 622 | const Address invalid_address = static_cast<Address>(0) - 1; |
6a74a719 | 623 | |
6fa2a40b | 624 | Sized_relobj_file<size, big_endian>* const object = relinfo->object; |
6a74a719 ILT |
625 | const unsigned int local_count = object->local_symbol_count(); |
626 | ||
627 | unsigned char* pwrite = reloc_view; | |
628 | ||
629 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
630 | { | |
631 | Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i); | |
632 | if (strategy == Relocatable_relocs::RELOC_DISCARD) | |
633 | continue; | |
634 | ||
5c388529 DK |
635 | if (strategy == Relocatable_relocs::RELOC_SPECIAL) |
636 | { | |
637 | // Target wants to handle this relocation. | |
638 | Sized_target<size, big_endian>* target = | |
639 | parameters->sized_target<size, big_endian>(); | |
640 | target->relocate_special_relocatable(relinfo, sh_type, prelocs, | |
641 | i, output_section, | |
642 | offset_in_output_section, | |
643 | view, view_address, | |
644 | view_size, pwrite); | |
645 | pwrite += reloc_size; | |
646 | continue; | |
647 | } | |
6a74a719 ILT |
648 | Reltype reloc(prelocs); |
649 | Reltype_write reloc_write(pwrite); | |
650 | ||
651 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); | |
652 | const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
653 | const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
654 | ||
655 | // Get the new symbol index. | |
656 | ||
657 | unsigned int new_symndx; | |
658 | if (r_sym < local_count) | |
659 | { | |
660 | switch (strategy) | |
661 | { | |
662 | case Relocatable_relocs::RELOC_COPY: | |
818bf354 ILT |
663 | if (r_sym == 0) |
664 | new_symndx = 0; | |
665 | else | |
666 | { | |
667 | new_symndx = object->symtab_index(r_sym); | |
668 | gold_assert(new_symndx != -1U); | |
669 | } | |
6a74a719 ILT |
670 | break; |
671 | ||
672 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
7019cd25 | 673 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
6a74a719 ILT |
674 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
675 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
676 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
677 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
2c339f71 | 678 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
6a74a719 ILT |
679 | { |
680 | // We are adjusting a section symbol. We need to find | |
681 | // the symbol table index of the section symbol for | |
682 | // the output section corresponding to input section | |
683 | // in which this symbol is defined. | |
684 | gold_assert(r_sym < local_count); | |
d491d34e ILT |
685 | bool is_ordinary; |
686 | unsigned int shndx = | |
687 | object->local_symbol_input_shndx(r_sym, &is_ordinary); | |
688 | gold_assert(is_ordinary); | |
ef9beddf | 689 | Output_section* os = object->output_section(shndx); |
6a74a719 ILT |
690 | gold_assert(os != NULL); |
691 | gold_assert(os->needs_symtab_index()); | |
692 | new_symndx = os->symtab_index(); | |
693 | } | |
694 | break; | |
695 | ||
696 | default: | |
697 | gold_unreachable(); | |
698 | } | |
699 | } | |
700 | else | |
701 | { | |
702 | const Symbol* gsym = object->global_symbol(r_sym); | |
703 | gold_assert(gsym != NULL); | |
704 | if (gsym->is_forwarder()) | |
705 | gsym = relinfo->symtab->resolve_forwards(gsym); | |
706 | ||
707 | gold_assert(gsym->has_symtab_index()); | |
708 | new_symndx = gsym->symtab_index(); | |
709 | } | |
710 | ||
711 | // Get the new offset--the location in the output section where | |
712 | // this relocation should be applied. | |
713 | ||
ef9beddf ILT |
714 | Address offset = reloc.get_r_offset(); |
715 | Address new_offset; | |
eff45813 | 716 | if (offset_in_output_section != invalid_address) |
6a74a719 ILT |
717 | new_offset = offset + offset_in_output_section; |
718 | else | |
719 | { | |
ef9beddf ILT |
720 | section_offset_type sot_offset = |
721 | convert_types<section_offset_type, Address>(offset); | |
722 | section_offset_type new_sot_offset = | |
723 | output_section->output_offset(object, relinfo->data_shndx, | |
724 | sot_offset); | |
725 | gold_assert(new_sot_offset != -1); | |
726 | new_offset = new_sot_offset; | |
6a74a719 ILT |
727 | } |
728 | ||
6be6f3bd ILT |
729 | // In an object file, r_offset is an offset within the section. |
730 | // In an executable or dynamic object, generated by | |
731 | // --emit-relocs, r_offset is an absolute address. | |
732 | if (!parameters->options().relocatable()) | |
e09ad04a ILT |
733 | { |
734 | new_offset += view_address; | |
eff45813 | 735 | if (offset_in_output_section != invalid_address) |
e09ad04a ILT |
736 | new_offset -= offset_in_output_section; |
737 | } | |
6be6f3bd | 738 | |
6a74a719 ILT |
739 | reloc_write.put_r_offset(new_offset); |
740 | reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type)); | |
741 | ||
742 | // Handle the reloc addend based on the strategy. | |
743 | ||
744 | if (strategy == Relocatable_relocs::RELOC_COPY) | |
745 | { | |
746 | if (sh_type == elfcpp::SHT_RELA) | |
747 | Reloc_types<sh_type, size, big_endian>:: | |
748 | copy_reloc_addend(&reloc_write, | |
749 | &reloc); | |
750 | } | |
751 | else | |
752 | { | |
753 | // The relocation uses a section symbol in the input file. | |
754 | // We are adjusting it to use a section symbol in the output | |
755 | // file. The input section symbol refers to some address in | |
756 | // the input section. We need the relocation in the output | |
757 | // file to refer to that same address. This adjustment to | |
758 | // the addend is the same calculation we use for a simple | |
759 | // absolute relocation for the input section symbol. | |
760 | ||
761 | const Symbol_value<size>* psymval = object->local_symbol(r_sym); | |
762 | ||
763 | unsigned char* padd = view + offset; | |
764 | switch (strategy) | |
765 | { | |
766 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: | |
767 | { | |
768 | typename elfcpp::Elf_types<size>::Elf_Swxword addend; | |
769 | addend = Reloc_types<sh_type, size, big_endian>:: | |
770 | get_reloc_addend(&reloc); | |
771 | addend = psymval->value(object, addend); | |
772 | Reloc_types<sh_type, size, big_endian>:: | |
773 | set_reloc_addend(&reloc_write, addend); | |
774 | } | |
775 | break; | |
776 | ||
7019cd25 ILT |
777 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
778 | break; | |
779 | ||
6a74a719 ILT |
780 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
781 | Relocate_functions<size, big_endian>::rel8(padd, object, | |
782 | psymval); | |
783 | break; | |
784 | ||
785 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: | |
786 | Relocate_functions<size, big_endian>::rel16(padd, object, | |
787 | psymval); | |
788 | break; | |
789 | ||
790 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: | |
791 | Relocate_functions<size, big_endian>::rel32(padd, object, | |
792 | psymval); | |
793 | break; | |
794 | ||
795 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: | |
796 | Relocate_functions<size, big_endian>::rel64(padd, object, | |
797 | psymval); | |
798 | break; | |
799 | ||
2c339f71 DK |
800 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
801 | Relocate_functions<size, big_endian>::rel32_unaligned(padd, | |
802 | object, | |
803 | psymval); | |
804 | break; | |
805 | ||
6a74a719 ILT |
806 | default: |
807 | gold_unreachable(); | |
808 | } | |
809 | } | |
810 | ||
811 | pwrite += reloc_size; | |
812 | } | |
813 | ||
814 | gold_assert(static_cast<section_size_type>(pwrite - reloc_view) | |
815 | == reloc_view_size); | |
816 | } | |
817 | ||
61ba1cf9 ILT |
818 | } // End namespace gold. |
819 | ||
820 | #endif // !defined(GOLD_TARGET_RELOC_H) |