arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / gold / target-reloc.h
blobf9a435cba7a49b1006635a0ddf7450d78098915b
1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright (C) 2006-2024 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_TARGET_RELOC_H
24 #define GOLD_TARGET_RELOC_H
26 #include "elfcpp.h"
27 #include "symtab.h"
28 #include "object.h"
29 #include "reloc.h"
30 #include "reloc-types.h"
32 namespace gold
35 // This function implements the generic part of reloc scanning. The
36 // template parameter Scan must be a class type which provides two
37 // functions: local() and global(). Those functions implement the
38 // machine specific part of scanning. We do it this way to
39 // avoid making a function call for each relocation, and to avoid
40 // repeating the generic code for each target.
42 template<int size, bool big_endian, typename Target_type,
43 typename Scan, typename Classify_reloc>
44 inline void
45 scan_relocs(
46 Symbol_table* symtab,
47 Layout* layout,
48 Target_type* target,
49 Sized_relobj_file<size, big_endian>* object,
50 unsigned int data_shndx,
51 const unsigned char* prelocs,
52 size_t reloc_count,
53 Output_section* output_section,
54 bool needs_special_offset_handling,
55 size_t local_count,
56 const unsigned char* plocal_syms)
58 typedef typename Classify_reloc::Reltype Reltype;
59 const int reloc_size = Classify_reloc::reloc_size;
60 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
61 Scan scan;
63 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
65 Reltype reloc(prelocs);
67 if (needs_special_offset_handling
68 && !output_section->is_input_address_mapped(object, data_shndx,
69 reloc.get_r_offset()))
70 continue;
72 unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
73 unsigned int r_type = Classify_reloc::get_r_type(&reloc);
75 if (r_sym < local_count)
77 gold_assert(plocal_syms != NULL);
78 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
79 + r_sym * sym_size);
80 unsigned int shndx = lsym.get_st_shndx();
81 bool is_ordinary;
82 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
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.
87 // FIXME: We should issue a warning if this is an
88 // allocated section; is this the best place to do it?
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));
100 scan.local(symtab, layout, target, object, data_shndx,
101 output_section, reloc, r_type, lsym, is_discarded);
103 else
105 Symbol* gsym = object->global_symbol(r_sym);
106 gold_assert(gsym != NULL);
107 if (gsym->is_forwarder())
108 gsym = symtab->resolve_forwards(gsym);
110 scan.global(symtab, layout, target, object, data_shndx,
111 output_section, reloc, r_type, gsym);
116 // Behavior for relocations to discarded comdat sections.
118 enum Comdat_behavior
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_ERROR // Print an error.
126 class Default_comdat_behavior
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.
133 inline Comdat_behavior
134 get(const char* name)
136 if (Layout::is_debug_info_section(name))
137 return CB_PRETEND;
138 if (strcmp(name, ".eh_frame") == 0
139 || is_prefix_of (".gnu.build.attributes", name)
140 || strcmp(name, ".gcc_except_table") == 0)
141 return CB_IGNORE;
142 return CB_ERROR;
146 // Give an error for a symbol with non-default visibility which is not
147 // defined locally.
149 inline void
150 visibility_error(const Symbol* sym)
152 const char* v;
153 switch (sym->visibility())
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();
167 gold_error(_("%s symbol '%s' is not defined locally"),
168 v, sym->name());
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.
175 inline bool
176 issue_undefined_symbol_error(const Symbol* sym)
178 // We only report global symbols.
179 if (sym == NULL)
180 return false;
182 // We only report undefined symbols.
183 if (!sym->is_undefined() && !sym->is_placeholder())
184 return false;
186 // We don't report weak symbols.
187 if (sym->is_weak_undefined())
188 return false;
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())
194 return false;
196 // If the target defines this symbol, don't report it here.
197 if (parameters->target().is_defined_by_abi(sym))
198 return false;
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)
205 if (strcmp(u, "ignore-all") == 0)
206 return false;
207 if (strcmp(u, "report-all") == 0)
208 return true;
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;
215 // If the symbol is hidden, report it.
216 if (sym->visibility() == elfcpp::STV_HIDDEN)
217 return true;
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;
224 // Otherwise issue a warning.
225 return true;
228 template<int size, bool big_endian>
229 inline void
230 issue_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)
237 Sized_relobj_file<size, big_endian>* object = relinfo->object;
239 if (gsym == NULL)
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"),
245 object->get_symbol_name(r_sym).c_str(), r_sym);
247 else
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());
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)
262 unsigned int key_symndx = 0;
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\""),
267 object->get_symbol_name(key_symndx).c_str());
268 if (kept_obj != NULL)
269 gold_info(_(" prevailing definition is from %s"),
270 kept_obj->name().c_str());
274 // This function implements the generic part of relocation processing.
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.
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.
284 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
285 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
286 // RELOCATE implements operator() to do a relocation.
288 // PRELOCS points to the relocation data. RELOC_COUNT is the number
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.
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.
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.
304 template<int size, bool big_endian, typename Target_type,
305 typename Relocate,
306 typename Relocate_comdat_behavior,
307 typename Classify_reloc>
308 inline void
309 relocate_section(
310 const Relocate_info<size, big_endian>* relinfo,
311 Target_type* target,
312 const unsigned char* prelocs,
313 size_t reloc_count,
314 Output_section* output_section,
315 bool needs_special_offset_handling,
316 unsigned char* view,
317 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
318 section_size_type view_size,
319 const Reloc_symbol_changes* reloc_symbol_changes)
321 typedef typename Classify_reloc::Reltype Reltype;
322 const int reloc_size = Classify_reloc::reloc_size;
323 Relocate relocate;
324 Relocate_comdat_behavior relocate_comdat_behavior;
326 Sized_relobj_file<size, big_endian>* object = relinfo->object;
327 unsigned int local_count = object->local_symbol_count();
329 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
331 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
333 Reltype reloc(prelocs);
335 section_offset_type offset =
336 convert_to_section_size_type(reloc.get_r_offset());
338 if (needs_special_offset_handling)
340 offset = output_section->output_offset(relinfo->object,
341 relinfo->data_shndx,
342 offset);
343 if (offset == -1)
344 continue;
347 unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
349 const Sized_symbol<size>* sym;
351 Symbol_value<size> symval;
352 const Symbol_value<size> *psymval;
353 bool is_defined_in_discarded_section;
354 unsigned int shndx;
355 const Symbol* gsym = NULL;
356 if (r_sym < local_count
357 && (reloc_symbol_changes == NULL
358 || (*reloc_symbol_changes)[i] == NULL))
360 sym = NULL;
361 psymval = object->local_symbol(r_sym);
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
366 // counterpart in the kept section. The symbol must not
367 // correspond to a section we are folding.
368 bool is_ordinary;
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));
376 else
378 if (reloc_symbol_changes != NULL
379 && (*reloc_symbol_changes)[i] != NULL)
380 gsym = (*reloc_symbol_changes)[i];
381 else
383 gsym = object->global_symbol(r_sym);
384 gold_assert(gsym != NULL);
385 if (gsym->is_forwarder())
386 gsym = relinfo->symtab->resolve_forwards(gsym);
389 sym = static_cast<const Sized_symbol<size>*>(gsym);
390 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
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());
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();
399 psymval = &symval;
401 is_defined_in_discarded_section =
402 (gsym->is_defined_in_discarded_section()
403 && gsym->is_undefined());
404 shndx = 0;
407 Symbol_value<size> symval2;
408 if (is_defined_in_discarded_section)
410 std::string name = object->section_name(relinfo->data_shndx);
412 if (comdat_behavior == CB_UNDETERMINED)
413 comdat_behavior = relocate_comdat_behavior.get(name.c_str());
415 if (comdat_behavior == CB_PRETEND)
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 =
424 object->map_to_kept_section(shndx, name, &found);
425 if (found)
426 symval2.set_output_value(value + psymval->input_value());
427 else
428 symval2.set_output_value(0);
430 else
432 if (comdat_behavior == CB_ERROR)
433 issue_discarded_error(relinfo, i, offset, r_sym, gsym);
434 symval2.set_output_value(0);
436 symval2.set_no_output_symtab_entry();
437 psymval = &symval2;
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;
448 if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target,
449 output_section, i, prelocs, sym, psymval,
450 v, view_address + offset, view_size))
451 continue;
453 if (v == NULL)
455 gold_error_at_location(relinfo, i, offset,
456 _("reloc has bad offset %zu"),
457 static_cast<size_t>(offset));
458 continue;
461 if (issue_undefined_symbol_error(sym))
462 gold_undefined_symbol_at_location(sym, relinfo, i, offset);
463 else if (sym != NULL
464 && sym->visibility() != elfcpp::STV_DEFAULT
465 && (sym->is_strong_undefined() || sym->is_from_dynobj()))
466 visibility_error(sym);
468 if (sym != NULL && sym->has_warning())
469 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
473 // Apply an incremental relocation.
475 template<int size, bool big_endian, typename Target_type,
476 typename Relocate>
477 void
478 apply_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)
488 // Construct the ELF relocation in a temporary buffer.
489 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
490 unsigned char relbuf[reloc_size];
491 elfcpp::Rela_write<size, big_endian> orel(relbuf);
492 orel.put_r_offset(r_offset);
493 orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type));
494 orel.put_r_addend(r_addend);
496 // Setup a Symbol_value for the global symbol.
497 const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym);
498 Symbol_value<size> symval;
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();
507 Relocate relocate;
508 relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL,
509 -1U, relbuf, sym, &symval,
510 view + r_offset, address + r_offset, view_size);
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.
522 template<int sh_type_, int size, bool big_endian>
523 class Default_classify_reloc
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_;
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()); }
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()); }
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); }
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)
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));
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); }
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*)
568 gold_unreachable();
569 return 0;
573 // This class may be used as a typical class for the
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.
579 template<typename Classify_reloc>
580 class Default_scan_relocatable_relocs
582 public:
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;
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); }
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); }
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
600 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
602 // We assume that relocation type 0 is NONE. Targets which are
603 // different must override.
604 if (r_type == 0 && r_sym == 0)
605 return Relocatable_relocs::RELOC_DISCARD;
606 return Relocatable_relocs::RELOC_COPY;
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)
614 if (sh_type == elfcpp::SHT_RELA)
615 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
616 else
618 switch (Classify_reloc::get_size_for_reloc(r_type, object))
620 case 0:
621 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
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();
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
639 global_strategy(unsigned int, Relobj*, unsigned int)
640 { return Relocatable_relocs::RELOC_COPY; }
643 // This is a strategy class used with scan_relocatable_relocs
644 // and --emit-relocs.
646 template<typename Classify_reloc>
647 class Default_emit_relocs_strategy
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;
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); }
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); }
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; }
669 // A local section symbol.
670 inline Relocatable_relocs::Reloc_strategy
671 local_section_strategy(unsigned int, Relobj*)
673 if (sh_type == elfcpp::SHT_RELA)
674 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
675 else
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;
687 // A global symbol.
688 inline Relocatable_relocs::Reloc_strategy
689 global_strategy(unsigned int, Relobj*, unsigned int)
690 { return Relocatable_relocs::RELOC_COPY; }
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.
701 template<int size, bool big_endian, typename Scan_relocatable_reloc>
702 void
703 scan_relocatable_relocs(
704 Symbol_table*,
705 Layout*,
706 Sized_relobj_file<size, big_endian>* object,
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)
716 typedef typename Scan_relocatable_reloc::Reltype Reltype;
717 const int reloc_size = Scan_relocatable_reloc::reloc_size;
718 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
719 Scan_relocatable_reloc scan;
721 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
723 Reltype reloc(prelocs);
725 Relocatable_relocs::Reloc_strategy strategy;
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
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);
737 if (r_sym >= local_symbol_count)
738 strategy = scan.global_strategy(r_type, object, r_sym);
739 else
741 gold_assert(plocal_syms != NULL);
742 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
743 + r_sym * sym_size);
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
748 && shndx != elfcpp::SHN_UNDEF
749 && !object->is_section_included(shndx))
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;
756 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
757 strategy = scan.local_non_section_strategy(r_type, object,
758 r_sym);
759 else
761 strategy = scan.local_section_strategy(r_type, object);
762 if (strategy != Relocatable_relocs::RELOC_DISCARD)
763 object->output_section(shndx)->set_needs_symtab_index();
766 if (strategy == Relocatable_relocs::RELOC_COPY)
767 object->set_must_have_output_symtab_entry(r_sym);
771 rr->set_next_reloc_strategy(strategy);
775 // Relocate relocs. Called for a relocatable link, and for --emit-relocs.
776 // This is a default definition which should work for most targets.
778 template<int size, bool big_endian, typename Classify_reloc>
779 void
780 relocate_relocs(
781 const Relocate_info<size, big_endian>* relinfo,
782 const unsigned char* prelocs,
783 size_t reloc_count,
784 Output_section* output_section,
785 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
786 unsigned char* view,
787 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
788 section_size_type view_size,
789 unsigned char* reloc_view,
790 section_size_type reloc_view_size)
792 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
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;
796 const Address invalid_address = static_cast<Address>(0) - 1;
798 Sized_relobj_file<size, big_endian>* const object = relinfo->object;
799 const unsigned int local_count = object->local_symbol_count();
801 unsigned char* pwrite = reloc_view;
803 const bool relocatable = parameters->options().relocatable();
805 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
807 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
808 if (strategy == Relocatable_relocs::RELOC_DISCARD)
809 continue;
811 if (strategy == Relocatable_relocs::RELOC_SPECIAL)
813 // Target wants to handle this relocation.
814 Sized_target<size, big_endian>* target =
815 parameters->sized_target<size, big_endian>();
816 target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type,
817 prelocs, i, output_section,
818 offset_in_output_section,
819 view, view_address,
820 view_size, pwrite);
821 pwrite += reloc_size;
822 continue;
824 Reltype reloc(prelocs);
825 Reltype_write reloc_write(pwrite);
827 const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc);
829 // Get the new symbol index.
831 Output_section* os = NULL;
832 unsigned int new_symndx;
833 if (r_sym < local_count)
835 switch (strategy)
837 case Relocatable_relocs::RELOC_COPY:
838 if (r_sym == 0)
839 new_symndx = 0;
840 else
842 new_symndx = object->symtab_index(r_sym);
843 gold_assert(new_symndx != -1U);
845 break;
847 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
848 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
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:
853 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED:
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);
860 bool is_ordinary;
861 unsigned int shndx =
862 object->local_symbol_input_shndx(r_sym, &is_ordinary);
863 gold_assert(is_ordinary);
864 os = object->output_section(shndx);
865 gold_assert(os != NULL);
866 gold_assert(os->needs_symtab_index());
867 new_symndx = os->symtab_index();
869 break;
871 default:
872 gold_unreachable();
875 else
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);
882 gold_assert(gsym->has_symtab_index());
883 new_symndx = gsym->symtab_index();
886 // Get the new offset--the location in the output section where
887 // this relocation should be applied.
889 Address offset = reloc.get_r_offset();
890 Address new_offset;
891 if (offset_in_output_section != invalid_address)
892 new_offset = offset + offset_in_output_section;
893 else
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;
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.
907 if (!relocatable)
909 new_offset += view_address;
910 if (offset_in_output_section != invalid_address)
911 new_offset -= offset_in_output_section;
914 reloc_write.put_r_offset(new_offset);
915 Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx);
917 // Handle the reloc addend based on the strategy.
919 if (strategy == Relocatable_relocs::RELOC_COPY)
921 if (Classify_reloc::sh_type == elfcpp::SHT_RELA)
922 Classify_reloc::put_r_addend(&reloc_write,
923 Classify_reloc::get_r_addend(&reloc));
925 else
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.
935 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
937 unsigned char* padd = view + offset;
938 switch (strategy)
940 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
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)
950 gold_assert(os != NULL);
951 addend -= os->address();
953 Classify_reloc::put_r_addend(&reloc_write, addend);
955 break;
957 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
958 break;
960 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
961 Relocate_functions<size, big_endian>::rel8(padd, object,
962 psymval);
963 break;
965 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
966 Relocate_functions<size, big_endian>::rel16(padd, object,
967 psymval);
968 break;
970 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
971 Relocate_functions<size, big_endian>::rel32(padd, object,
972 psymval);
973 break;
975 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
976 Relocate_functions<size, big_endian>::rel64(padd, object,
977 psymval);
978 break;
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;
986 default:
987 gold_unreachable();
991 pwrite += reloc_size;
994 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
995 == reloc_view_size);
998 } // End namespace gold.
1000 #endif // !defined(GOLD_TARGET_RELOC_H)