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