Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / target-reloc.h
CommitLineData
61ba1cf9
ILT
1// target-reloc.h -- target specific relocation support -*- C++ -*-
2
219d1afa 3// Copyright (C) 2006-2018 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
4d625b70
CC
42template<int size, bool big_endian, typename Target_type,
43 typename Scan, typename Classify_reloc>
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 57{
4d625b70
CC
58 typedef typename Classify_reloc::Reltype Reltype;
59 const int reloc_size = Classify_reloc::reloc_size;
92e059d8
ILT
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
4d625b70
CC
72 unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
73 unsigned int r_type = Classify_reloc::get_r_type(&reloc);
92e059d8
ILT
74
75 if (r_sym < local_count)
76 {
a3ad94ed 77 gold_assert(plocal_syms != NULL);
92e059d8
ILT
78 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
79 + r_sym * sym_size);
d491d34e
ILT
80 unsigned int shndx = lsym.get_st_shndx();
81 bool is_ordinary;
82 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
bfdfa4cd
AM
83 // If RELOC is a relocation against a local symbol in a
84 // section we are discarding then we can ignore it. It will
85 // eventually become a reloc against the value zero.
86 //
87 // FIXME: We should issue a warning if this is an
88 // allocated section; is this the best place to do it?
62fe925a 89 //
bfdfa4cd
AM
90 // FIXME: The old GNU linker would in some cases look
91 // for the linkonce section which caused this section to
92 // be discarded, and, if the other section was the same
93 // size, change the reloc to refer to the other section.
94 // That seems risky and weird to me, and I don't know of
95 // any case where it is actually required.
96 bool is_discarded = (is_ordinary
97 && shndx != elfcpp::SHN_UNDEF
98 && !object->is_section_included(shndx)
99 && !symtab->is_section_folded(object, shndx));
ad0f2072 100 scan.local(symtab, layout, target, object, data_shndx,
bfdfa4cd 101 output_section, reloc, r_type, lsym, is_discarded);
92e059d8
ILT
102 }
103 else
104 {
730cdc88 105 Symbol* gsym = object->global_symbol(r_sym);
a3ad94ed 106 gold_assert(gsym != NULL);
92e059d8
ILT
107 if (gsym->is_forwarder())
108 gsym = symtab->resolve_forwards(gsym);
109
ad0f2072 110 scan.global(symtab, layout, target, object, data_shndx,
07f397ab 111 output_section, reloc, r_type, gsym);
92e059d8
ILT
112 }
113 }
114}
115
e94cf127
CC
116// Behavior for relocations to discarded comdat sections.
117
118enum Comdat_behavior
119{
120 CB_UNDETERMINED, // Not yet determined -- need to look at section name.
121 CB_PRETEND, // Attempt to map to the corresponding kept section.
122 CB_IGNORE, // Ignore the relocation.
123 CB_WARNING // Print a warning.
124};
125
168a4726 126class Default_comdat_behavior
e94cf127 127{
168a4726
AM
128 public:
129 // Decide what the linker should do for relocations that refer to
130 // discarded comdat sections. This decision is based on the name of
131 // the section being relocated.
132
133 inline Comdat_behavior
134 get(const char* name)
135 {
136 if (Layout::is_debug_info_section(name))
137 return CB_PRETEND;
138 if (strcmp(name, ".eh_frame") == 0
139 || strcmp(name, ".gcc_except_table") == 0)
140 return CB_IGNORE;
141 return CB_WARNING;
142 }
143};
e94cf127 144
837504c4
ILT
145// Give an error for a symbol with non-default visibility which is not
146// defined locally.
147
148inline void
149visibility_error(const Symbol* sym)
150{
151 const char* v;
152 switch (sym->visibility())
153 {
154 case elfcpp::STV_INTERNAL:
155 v = _("internal");
156 break;
157 case elfcpp::STV_HIDDEN:
158 v = _("hidden");
159 break;
160 case elfcpp::STV_PROTECTED:
161 v = _("protected");
162 break;
163 default:
164 gold_unreachable();
165 }
166 gold_error(_("%s symbol '%s' is not defined locally"),
167 v, sym->name());
168}
169
191f1a2d
ILT
170// Return true if we are should issue an error saying that SYM is an
171// undefined symbol. This is called if there is a relocation against
172// SYM.
173
174inline bool
175issue_undefined_symbol_error(const Symbol* sym)
176{
beabb2c6
ILT
177 // We only report global symbols.
178 if (sym == NULL)
179 return false;
180
181 // We only report undefined symbols.
182 if (!sym->is_undefined() && !sym->is_placeholder())
183 return false;
184
185 // We don't report weak symbols.
d1bddd3c 186 if (sym->is_weak_undefined())
beabb2c6
ILT
187 return false;
188
648c5cbb
CC
189 // We don't report symbols defined in discarded sections,
190 // unless they're placeholder symbols that should have been
191 // provided by a plugin.
192 if (sym->is_defined_in_discarded_section() && !sym->is_placeholder())
beabb2c6
ILT
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
d1bddd3c
CC
214 // If the symbol is hidden, report it.
215 if (sym->visibility() == elfcpp::STV_HIDDEN)
216 return true;
217
beabb2c6
ILT
218 // When creating a shared library, only report unresolved symbols if
219 // -z defs was used.
220 if (parameters->options().shared() && !parameters->options().defs())
221 return false;
222
223 // Otherwise issue a warning.
224 return true;
191f1a2d
ILT
225}
226
92e059d8 227// This function implements the generic part of relocation processing.
6a74a719
ILT
228// The template parameter Relocate must be a class type which provides
229// a single function, relocate(), which implements the machine
230// specific part of a relocation.
61ba1cf9 231
168a4726
AM
232// The template parameter Relocate_comdat_behavior is a class type
233// which provides a single function, get(), which determines what the
234// linker should do for relocations that refer to discarded comdat
235// sections.
236
61ba1cf9 237// SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
92e059d8
ILT
238// the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
239// RELOCATE implements operator() to do a relocation.
61ba1cf9 240
92e059d8 241// PRELOCS points to the relocation data. RELOC_COUNT is the number
730cdc88
ILT
242// of relocs. OUTPUT_SECTION is the output section.
243// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
244// mapped to output offsets.
245
246// VIEW is the section data, VIEW_ADDRESS is its memory address, and
247// VIEW_SIZE is the size. These refer to the input section, unless
248// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
249// the output section.
61ba1cf9 250
364c7fa5
ILT
251// RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
252// not NULL, it is a vector indexed by relocation index. If that
253// entry is not NULL, it points to a global symbol which used as the
254// symbol for the relocation, ignoring the symbol index in the
255// relocation.
256
4d625b70 257template<int size, bool big_endian, typename Target_type,
168a4726 258 typename Relocate,
4d625b70
CC
259 typename Relocate_comdat_behavior,
260 typename Classify_reloc>
61ba1cf9
ILT
261inline void
262relocate_section(
92e059d8 263 const Relocate_info<size, big_endian>* relinfo,
ead1e424 264 Target_type* target,
61ba1cf9
ILT
265 const unsigned char* prelocs,
266 size_t reloc_count,
730cdc88
ILT
267 Output_section* output_section,
268 bool needs_special_offset_handling,
61ba1cf9
ILT
269 unsigned char* view,
270 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
364c7fa5
ILT
271 section_size_type view_size,
272 const Reloc_symbol_changes* reloc_symbol_changes)
61ba1cf9 273{
4d625b70
CC
274 typedef typename Classify_reloc::Reltype Reltype;
275 const int reloc_size = Classify_reloc::reloc_size;
61ba1cf9 276 Relocate relocate;
168a4726 277 Relocate_comdat_behavior relocate_comdat_behavior;
61ba1cf9 278
6fa2a40b 279 Sized_relobj_file<size, big_endian>* object = relinfo->object;
730cdc88 280 unsigned int local_count = object->local_symbol_count();
92e059d8 281
e94cf127
CC
282 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
283
61ba1cf9
ILT
284 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
285 {
286 Reltype reloc(prelocs);
287
fe8718a4
ILT
288 section_offset_type offset =
289 convert_to_section_size_type(reloc.get_r_offset());
61ba1cf9 290
730cdc88
ILT
291 if (needs_special_offset_handling)
292 {
293 offset = output_section->output_offset(relinfo->object,
294 relinfo->data_shndx,
295 offset);
296 if (offset == -1)
297 continue;
298 }
299
4d625b70 300 unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
61ba1cf9 301
c06b7b0b 302 const Sized_symbol<size>* sym;
61ba1cf9 303
b8e6aad9
ILT
304 Symbol_value<size> symval;
305 const Symbol_value<size> *psymval;
880cd20d
ILT
306 bool is_defined_in_discarded_section;
307 unsigned int shndx;
c54b27d1 308 const Symbol* gsym = NULL;
364c7fa5
ILT
309 if (r_sym < local_count
310 && (reloc_symbol_changes == NULL
311 || (*reloc_symbol_changes)[i] == NULL))
61ba1cf9
ILT
312 {
313 sym = NULL;
730cdc88 314 psymval = object->local_symbol(r_sym);
e94cf127
CC
315
316 // If the local symbol belongs to a section we are discarding,
317 // and that section is a debug section, try to find the
318 // corresponding kept section and map this symbol to its
62fe925a 319 // counterpart in the kept section. The symbol must not
ef15dade 320 // correspond to a section we are folding.
e94cf127 321 bool is_ordinary;
880cd20d
ILT
322 shndx = psymval->input_shndx(&is_ordinary);
323 is_defined_in_discarded_section =
324 (is_ordinary
325 && shndx != elfcpp::SHN_UNDEF
326 && !object->is_section_included(shndx)
327 && !relinfo->symtab->is_section_folded(object, shndx));
61ba1cf9
ILT
328 }
329 else
330 {
364c7fa5
ILT
331 if (reloc_symbol_changes != NULL
332 && (*reloc_symbol_changes)[i] != NULL)
333 gsym = (*reloc_symbol_changes)[i];
334 else
335 {
336 gsym = object->global_symbol(r_sym);
337 gold_assert(gsym != NULL);
338 if (gsym->is_forwarder())
339 gsym = relinfo->symtab->resolve_forwards(gsym);
340 }
61ba1cf9 341
c06b7b0b 342 sym = static_cast<const Sized_symbol<size>*>(gsym);
d3bbad62 343 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
b8e6aad9
ILT
344 symval.set_output_symtab_index(sym->symtab_index());
345 else
346 symval.set_no_output_symtab_entry();
347 symval.set_output_value(sym->value());
7223e9ca
ILT
348 if (gsym->type() == elfcpp::STT_TLS)
349 symval.set_is_tls_symbol();
350 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
351 symval.set_is_ifunc_symbol();
b8e6aad9 352 psymval = &symval;
880cd20d
ILT
353
354 is_defined_in_discarded_section =
355 (gsym->is_defined_in_discarded_section()
356 && gsym->is_undefined());
357 shndx = 0;
358 }
359
360 Symbol_value<size> symval2;
361 if (is_defined_in_discarded_section)
362 {
363 if (comdat_behavior == CB_UNDETERMINED)
364 {
365 std::string name = object->section_name(relinfo->data_shndx);
168a4726 366 comdat_behavior = relocate_comdat_behavior.get(name.c_str());
880cd20d
ILT
367 }
368 if (comdat_behavior == CB_PRETEND)
369 {
370 // FIXME: This case does not work for global symbols.
371 // We have no place to store the original section index.
372 // Fortunately this does not matter for comdat sections,
373 // only for sections explicitly discarded by a linker
374 // script.
375 bool found;
376 typename elfcpp::Elf_types<size>::Elf_Addr value =
377 object->map_to_kept_section(shndx, &found);
378 if (found)
379 symval2.set_output_value(value + psymval->input_value());
380 else
381 symval2.set_output_value(0);
382 }
383 else
384 {
385 if (comdat_behavior == CB_WARNING)
c54b27d1
CC
386 {
387 if (gsym == NULL)
388 {
389 gold_warning_at_location(
390 relinfo, i, offset,
391 _("relocation refers to local symbol %d "
392 "defined in discarded section"),
393 r_sym);
394 }
395 else
396 {
397 gold_warning_at_location(
398 relinfo, i, offset,
399 _("relocation refers to symbol \"%s\" "
400 "defined in discarded section"),
401 gsym->demangled_name().c_str());
402 }
403 }
880cd20d
ILT
404 symval2.set_output_value(0);
405 }
406 symval2.set_no_output_symtab_entry();
407 psymval = &symval2;
ead1e424 408 }
61ba1cf9 409
0e804863
ILT
410 // If OFFSET is out of range, still let the target decide to
411 // ignore the relocation. Pass in NULL as the VIEW argument so
412 // that it can return quickly without trashing an invalid memory
413 // address.
414 unsigned char *v = view + offset;
415 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
416 v = NULL;
417
4d625b70
CC
418 if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target,
419 output_section, i, prelocs, sym, psymval,
91a65d2f 420 v, view_address + offset, view_size))
ead1e424
ILT
421 continue;
422
0e804863 423 if (v == NULL)
ead1e424 424 {
75f2446e
ILT
425 gold_error_at_location(relinfo, i, offset,
426 _("reloc has bad offset %zu"),
427 static_cast<size_t>(offset));
428 continue;
61ba1cf9
ILT
429 }
430
191f1a2d 431 if (issue_undefined_symbol_error(sym))
1a221d3d 432 gold_undefined_symbol_at_location(sym, relinfo, i, offset);
837504c4
ILT
433 else if (sym != NULL
434 && sym->visibility() != elfcpp::STV_DEFAULT
d1bddd3c 435 && (sym->is_strong_undefined() || sym->is_from_dynobj()))
837504c4 436 visibility_error(sym);
f6ce93d6
ILT
437
438 if (sym != NULL && sym->has_warning())
75f2446e 439 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
61ba1cf9
ILT
440 }
441}
442
94a3fc8b
CC
443// Apply an incremental relocation.
444
445template<int size, bool big_endian, typename Target_type,
446 typename Relocate>
447void
448apply_relocation(const Relocate_info<size, big_endian>* relinfo,
449 Target_type* target,
450 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
451 unsigned int r_type,
452 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
453 const Symbol* gsym,
454 unsigned char* view,
455 typename elfcpp::Elf_types<size>::Elf_Addr address,
456 section_size_type view_size)
457{
458 // Construct the ELF relocation in a temporary buffer.
d55525b9 459 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
94a3fc8b 460 unsigned char relbuf[reloc_size];
d55525b9 461 elfcpp::Rela_write<size, big_endian> orel(relbuf);
94a3fc8b 462 orel.put_r_offset(r_offset);
d55525b9 463 orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
94a3fc8b
CC
464 orel.put_r_addend(r_addend);
465
466 // Setup a Symbol_value for the global symbol.
d55525b9
L
467 const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
468 Symbol_value<size> symval;
94a3fc8b
CC
469 gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U);
470 symval.set_output_symtab_index(sym->symtab_index());
471 symval.set_output_value(sym->value());
472 if (gsym->type() == elfcpp::STT_TLS)
473 symval.set_is_tls_symbol();
474 else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
475 symval.set_is_ifunc_symbol();
476
477 Relocate relocate;
91a65d2f
AM
478 relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL,
479 -1U, relbuf, sym, &symval,
94a3fc8b
CC
480 view + r_offset, address + r_offset, view_size);
481}
482
4d625b70
CC
483// A class for inquiring about properties of a relocation,
484// used while scanning relocs during a relocatable link and
485// garbage collection. This class may be used as the default
486// for SHT_RELA targets, but SHT_REL targets must implement
487// a derived class that overrides get_size_for_reloc.
488// The MIPS-64 target also needs to override the methods
489// for accessing the r_sym and r_type fields of a relocation,
490// due to its non-standard use of the r_info field.
491
492template<int sh_type_, int size, bool big_endian>
493class Default_classify_reloc
494{
495 public:
496 typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc
497 Reltype;
498 typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write
499 Reltype_write;
500 static const int reloc_size =
501 Reloc_types<sh_type_, size, big_endian>::reloc_size;
502 static const int sh_type = sh_type_;
503
504 // Return the symbol referred to by the relocation.
505 static inline unsigned int
506 get_r_sym(const Reltype* reloc)
507 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
508
509 // Return the type of the relocation.
510 static inline unsigned int
511 get_r_type(const Reltype* reloc)
512 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
513
514 // Return the explicit addend of the relocation (return 0 for SHT_REL).
515 static inline typename elfcpp::Elf_types<size>::Elf_Swxword
516 get_r_addend(const Reltype* reloc)
517 { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); }
518
519 // Write the r_info field to a new reloc, using the r_info field from
520 // the original reloc, replacing the r_sym field with R_SYM.
521 static inline void
522 put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym)
523 {
524 unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info());
525 new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
526 }
527
528 // Write the r_addend field to a new reloc.
529 static inline void
530 put_r_addend(Reltype_write* to,
531 typename elfcpp::Elf_types<size>::Elf_Swxword addend)
532 { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); }
533
534 // Return the size of the addend of the relocation (only used for SHT_REL).
535 static unsigned int
536 get_size_for_reloc(unsigned int, Relobj*)
537 {
538 gold_unreachable();
539 return 0;
540 }
541};
542
6a74a719 543// This class may be used as a typical class for the
4d625b70
CC
544// Scan_relocatable_reloc parameter to scan_relocatable_relocs.
545// This class is intended to capture the most typical target behaviour,
546// while still permitting targets to define their own independent class
547// for Scan_relocatable_reloc.
548
549template<typename Classify_reloc>
6a74a719
ILT
550class Default_scan_relocatable_relocs
551{
552 public:
4d625b70
CC
553 typedef typename Classify_reloc::Reltype Reltype;
554 static const int reloc_size = Classify_reloc::reloc_size;
555 static const int sh_type = Classify_reloc::sh_type;
556
557 // Return the symbol referred to by the relocation.
558 static inline unsigned int
559 get_r_sym(const Reltype* reloc)
560 { return Classify_reloc::get_r_sym(reloc); }
561
562 // Return the type of the relocation.
563 static inline unsigned int
564 get_r_type(const Reltype* reloc)
565 { return Classify_reloc::get_r_type(reloc); }
566
6a74a719
ILT
567 // Return the strategy to use for a local symbol which is not a
568 // section symbol, given the relocation type.
569 inline Relocatable_relocs::Reloc_strategy
68943102 570 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
c2508178
ILT
571 {
572 // We assume that relocation type 0 is NONE. Targets which are
573 // different must override.
68943102 574 if (r_type == 0 && r_sym == 0)
c2508178
ILT
575 return Relocatable_relocs::RELOC_DISCARD;
576 return Relocatable_relocs::RELOC_COPY;
577 }
6a74a719
ILT
578
579 // Return the strategy to use for a local symbol which is a section
580 // symbol, given the relocation type.
581 inline Relocatable_relocs::Reloc_strategy
582 local_section_strategy(unsigned int r_type, Relobj* object)
583 {
584 if (sh_type == elfcpp::SHT_RELA)
585 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
586 else
587 {
4d625b70 588 switch (Classify_reloc::get_size_for_reloc(r_type, object))
6a74a719
ILT
589 {
590 case 0:
7019cd25 591 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
6a74a719
ILT
592 case 1:
593 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
594 case 2:
595 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
596 case 4:
597 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
598 case 8:
599 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
600 default:
601 gold_unreachable();
602 }
603 }
604 }
605
606 // Return the strategy to use for a global symbol, given the
607 // relocation type, the object, and the symbol index.
608 inline Relocatable_relocs::Reloc_strategy
68943102
ILT
609 global_strategy(unsigned int, Relobj*, unsigned int)
610 { return Relocatable_relocs::RELOC_COPY; }
6a74a719
ILT
611};
612
4d625b70
CC
613// This is a strategy class used with scan_relocatable_relocs
614// and --emit-relocs.
615
616template<typename Classify_reloc>
617class Default_emit_relocs_strategy
618{
619 public:
620 typedef typename Classify_reloc::Reltype Reltype;
621 static const int reloc_size = Classify_reloc::reloc_size;
622 static const int sh_type = Classify_reloc::sh_type;
623
624 // Return the symbol referred to by the relocation.
625 static inline unsigned int
626 get_r_sym(const Reltype* reloc)
627 { return Classify_reloc::get_r_sym(reloc); }
628
629 // Return the type of the relocation.
630 static inline unsigned int
631 get_r_type(const Reltype* reloc)
632 { return Classify_reloc::get_r_type(reloc); }
633
634 // A local non-section symbol.
635 inline Relocatable_relocs::Reloc_strategy
636 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
637 { return Relocatable_relocs::RELOC_COPY; }
638
639 // A local section symbol.
640 inline Relocatable_relocs::Reloc_strategy
641 local_section_strategy(unsigned int, Relobj*)
642 {
643 if (sh_type == elfcpp::SHT_RELA)
644 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
645 else
646 {
647 // The addend is stored in the section contents. Since this
648 // is not a relocatable link, we are going to apply the
649 // relocation contents to the section as usual. This means
650 // that we have no way to record the original addend. If the
651 // original addend is not zero, there is basically no way for
652 // the user to handle this correctly. Caveat emptor.
653 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
654 }
655 }
656
657 // A global symbol.
658 inline Relocatable_relocs::Reloc_strategy
659 global_strategy(unsigned int, Relobj*, unsigned int)
660 { return Relocatable_relocs::RELOC_COPY; }
661};
662
6a74a719
ILT
663// Scan relocs during a relocatable link. This is a default
664// definition which should work for most targets.
665// Scan_relocatable_reloc must name a class type which provides three
666// functions which return a Relocatable_relocs::Reloc_strategy code:
667// global_strategy, local_non_section_strategy, and
668// local_section_strategy. Most targets should be able to use
669// Default_scan_relocatable_relocs as this class.
670
4d625b70 671template<int size, bool big_endian, typename Scan_relocatable_reloc>
6a74a719
ILT
672void
673scan_relocatable_relocs(
6a74a719
ILT
674 Symbol_table*,
675 Layout*,
6fa2a40b 676 Sized_relobj_file<size, big_endian>* object,
6a74a719
ILT
677 unsigned int data_shndx,
678 const unsigned char* prelocs,
679 size_t reloc_count,
680 Output_section* output_section,
681 bool needs_special_offset_handling,
682 size_t local_symbol_count,
683 const unsigned char* plocal_syms,
684 Relocatable_relocs* rr)
685{
4d625b70
CC
686 typedef typename Scan_relocatable_reloc::Reltype Reltype;
687 const int reloc_size = Scan_relocatable_reloc::reloc_size;
6a74a719
ILT
688 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
689 Scan_relocatable_reloc scan;
690
691 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
692 {
693 Reltype reloc(prelocs);
694
695 Relocatable_relocs::Reloc_strategy strategy;
696
697 if (needs_special_offset_handling
698 && !output_section->is_input_address_mapped(object, data_shndx,
699 reloc.get_r_offset()))
700 strategy = Relocatable_relocs::RELOC_DISCARD;
701 else
702 {
4d625b70
CC
703 const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc);
704 const unsigned int r_type =
705 Scan_relocatable_reloc::get_r_type(&reloc);
6a74a719
ILT
706
707 if (r_sym >= local_symbol_count)
708 strategy = scan.global_strategy(r_type, object, r_sym);
709 else
710 {
711 gold_assert(plocal_syms != NULL);
712 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
713 + r_sym * sym_size);
d491d34e
ILT
714 unsigned int shndx = lsym.get_st_shndx();
715 bool is_ordinary;
716 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
717 if (is_ordinary
6a74a719 718 && shndx != elfcpp::SHN_UNDEF
d491d34e 719 && !object->is_section_included(shndx))
6a74a719
ILT
720 {
721 // RELOC is a relocation against a local symbol
722 // defined in a section we are discarding. Discard
723 // the reloc. FIXME: Should we issue a warning?
724 strategy = Relocatable_relocs::RELOC_DISCARD;
725 }
726 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
68943102
ILT
727 strategy = scan.local_non_section_strategy(r_type, object,
728 r_sym);
6a74a719
ILT
729 else
730 {
731 strategy = scan.local_section_strategy(r_type, object);
732 if (strategy != Relocatable_relocs::RELOC_DISCARD)
ef9beddf 733 object->output_section(shndx)->set_needs_symtab_index();
6a74a719 734 }
d3bbad62
ILT
735
736 if (strategy == Relocatable_relocs::RELOC_COPY)
737 object->set_must_have_output_symtab_entry(r_sym);
6a74a719
ILT
738 }
739 }
740
741 rr->set_next_reloc_strategy(strategy);
742 }
743}
744
7404fe1b
AM
745// Relocate relocs. Called for a relocatable link, and for --emit-relocs.
746// This is a default definition which should work for most targets.
6a74a719 747
4d625b70 748template<int size, bool big_endian, typename Classify_reloc>
6a74a719 749void
7404fe1b 750relocate_relocs(
6a74a719
ILT
751 const Relocate_info<size, big_endian>* relinfo,
752 const unsigned char* prelocs,
753 size_t reloc_count,
754 Output_section* output_section,
62fe925a 755 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6a74a719 756 unsigned char* view,
6be6f3bd 757 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5c388529 758 section_size_type view_size,
6a74a719
ILT
759 unsigned char* reloc_view,
760 section_size_type reloc_view_size)
761{
ef9beddf 762 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4d625b70
CC
763 typedef typename Classify_reloc::Reltype Reltype;
764 typedef typename Classify_reloc::Reltype_write Reltype_write;
765 const int reloc_size = Classify_reloc::reloc_size;
eff45813 766 const Address invalid_address = static_cast<Address>(0) - 1;
6a74a719 767
6fa2a40b 768 Sized_relobj_file<size, big_endian>* const object = relinfo->object;
6a74a719
ILT
769 const unsigned int local_count = object->local_symbol_count();
770
771 unsigned char* pwrite = reloc_view;
772
033bfb73
CC
773 const bool relocatable = parameters->options().relocatable();
774
6a74a719
ILT
775 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
776 {
91a65d2f 777 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
6a74a719
ILT
778 if (strategy == Relocatable_relocs::RELOC_DISCARD)
779 continue;
780
5c388529
DK
781 if (strategy == Relocatable_relocs::RELOC_SPECIAL)
782 {
783 // Target wants to handle this relocation.
784 Sized_target<size, big_endian>* target =
785 parameters->sized_target<size, big_endian>();
4d625b70
CC
786 target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type,
787 prelocs, i, output_section,
5c388529
DK
788 offset_in_output_section,
789 view, view_address,
790 view_size, pwrite);
791 pwrite += reloc_size;
792 continue;
793 }
6a74a719
ILT
794 Reltype reloc(prelocs);
795 Reltype_write reloc_write(pwrite);
796
4d625b70 797 const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
6a74a719
ILT
798
799 // Get the new symbol index.
800
9215b98b 801 Output_section* os = NULL;
6a74a719
ILT
802 unsigned int new_symndx;
803 if (r_sym < local_count)
804 {
805 switch (strategy)
806 {
807 case Relocatable_relocs::RELOC_COPY:
818bf354
ILT
808 if (r_sym == 0)
809 new_symndx = 0;
810 else
811 {
812 new_symndx = object->symtab_index(r_sym);
813 gold_assert(new_symndx != -1U);
814 }
6a74a719
ILT
815 break;
816
817 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7019cd25 818 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
6a74a719
ILT
819 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
820 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
821 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
822 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
2c339f71 823 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
6a74a719
ILT
824 {
825 // We are adjusting a section symbol. We need to find
826 // the symbol table index of the section symbol for
827 // the output section corresponding to input section
828 // in which this symbol is defined.
829 gold_assert(r_sym < local_count);
d491d34e
ILT
830 bool is_ordinary;
831 unsigned int shndx =
832 object->local_symbol_input_shndx(r_sym, &is_ordinary);
833 gold_assert(is_ordinary);
9215b98b 834 os = object->output_section(shndx);
6a74a719
ILT
835 gold_assert(os != NULL);
836 gold_assert(os->needs_symtab_index());
837 new_symndx = os->symtab_index();
838 }
839 break;
840
841 default:
842 gold_unreachable();
843 }
844 }
845 else
846 {
847 const Symbol* gsym = object->global_symbol(r_sym);
848 gold_assert(gsym != NULL);
849 if (gsym->is_forwarder())
850 gsym = relinfo->symtab->resolve_forwards(gsym);
851
852 gold_assert(gsym->has_symtab_index());
853 new_symndx = gsym->symtab_index();
854 }
855
856 // Get the new offset--the location in the output section where
857 // this relocation should be applied.
858
ef9beddf
ILT
859 Address offset = reloc.get_r_offset();
860 Address new_offset;
eff45813 861 if (offset_in_output_section != invalid_address)
6a74a719
ILT
862 new_offset = offset + offset_in_output_section;
863 else
864 {
ef9beddf
ILT
865 section_offset_type sot_offset =
866 convert_types<section_offset_type, Address>(offset);
867 section_offset_type new_sot_offset =
868 output_section->output_offset(object, relinfo->data_shndx,
869 sot_offset);
870 gold_assert(new_sot_offset != -1);
871 new_offset = new_sot_offset;
6a74a719
ILT
872 }
873
6be6f3bd
ILT
874 // In an object file, r_offset is an offset within the section.
875 // In an executable or dynamic object, generated by
876 // --emit-relocs, r_offset is an absolute address.
033bfb73 877 if (!relocatable)
e09ad04a
ILT
878 {
879 new_offset += view_address;
eff45813 880 if (offset_in_output_section != invalid_address)
e09ad04a
ILT
881 new_offset -= offset_in_output_section;
882 }
6be6f3bd 883
6a74a719 884 reloc_write.put_r_offset(new_offset);
4d625b70 885 Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx);
6a74a719
ILT
886
887 // Handle the reloc addend based on the strategy.
888
889 if (strategy == Relocatable_relocs::RELOC_COPY)
890 {
4d625b70
CC
891 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
892 Classify_reloc::put_r_addend(&reloc_write,
893 Classify_reloc::get_r_addend(&reloc));
6a74a719
ILT
894 }
895 else
896 {
897 // The relocation uses a section symbol in the input file.
898 // We are adjusting it to use a section symbol in the output
899 // file. The input section symbol refers to some address in
900 // the input section. We need the relocation in the output
901 // file to refer to that same address. This adjustment to
902 // the addend is the same calculation we use for a simple
903 // absolute relocation for the input section symbol.
904
905 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
906
907 unsigned char* padd = view + offset;
908 switch (strategy)
909 {
910 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
911 {
033bfb73
CC
912 typename elfcpp::Elf_types<size>::Elf_Swxword addend
913 = Classify_reloc::get_r_addend(&reloc);
914 addend = psymval->value(object, addend);
915 // In a relocatable link, the symbol value is relative to
916 // the start of the output section. For a non-relocatable
917 // link, we need to adjust the addend.
918 if (!relocatable)
919 {
920 gold_assert(os != NULL);
921 addend -= os->address();
922 }
4d625b70 923 Classify_reloc::put_r_addend(&reloc_write, addend);
6a74a719
ILT
924 }
925 break;
926
7019cd25
ILT
927 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
928 break;
929
6a74a719
ILT
930 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
931 Relocate_functions<size, big_endian>::rel8(padd, object,
932 psymval);
933 break;
934
935 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
936 Relocate_functions<size, big_endian>::rel16(padd, object,
937 psymval);
938 break;
939
940 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
941 Relocate_functions<size, big_endian>::rel32(padd, object,
942 psymval);
943 break;
944
945 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
946 Relocate_functions<size, big_endian>::rel64(padd, object,
947 psymval);
948 break;
949
2c339f71
DK
950 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
951 Relocate_functions<size, big_endian>::rel32_unaligned(padd,
952 object,
953 psymval);
954 break;
955
6a74a719
ILT
956 default:
957 gold_unreachable();
958 }
959 }
960
961 pwrite += reloc_size;
962 }
963
964 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
965 == reloc_view_size);
966}
967
61ba1cf9
ILT
968} // End namespace gold.
969
970#endif // !defined(GOLD_TARGET_RELOC_H)
This page took 0.535218 seconds and 4 git commands to generate.