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