1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 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
30 #include "reloc-types.h"
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 // avoidmaking 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, int sh_type,
49 Sized_relobj<size, big_endian>* object,
50 unsigned int data_shndx,
51 const unsigned char* prelocs,
53 Output_section* output_section,
54 bool needs_special_offset_handling,
56 const unsigned char* plocal_syms)
58 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
59 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
60 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
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()))
72 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
73 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
74 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
76 if (r_sym < local_count)
78 gold_assert(plocal_syms != NULL);
79 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
81 unsigned int shndx = lsym.get_st_shndx();
83 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
85 && shndx != elfcpp::SHN_UNDEF
86 && !object->is_section_included(shndx)
87 && !symtab->is_section_folded(object, shndx))
89 // RELOC is a relocation against a local symbol in a
90 // section we are discarding. We can ignore this
91 // relocation. It will eventually become a reloc
92 // against the value zero.
94 // FIXME: We should issue a warning if this is an
95 // allocated section; is this the best place to do it?
97 // FIXME: The old GNU linker would in some cases look
98 // for the linkonce section which caused this section to
99 // be discarded, and, if the other section was the same
100 // size, change the reloc to refer to the other section.
101 // That seems risky and weird to me, and I don't know of
102 // any case where it is actually required.
106 scan.local(symtab, layout, target, object, data_shndx,
107 output_section, reloc, r_type, lsym);
111 Symbol* gsym = object->global_symbol(r_sym);
112 gold_assert(gsym != NULL);
113 if (gsym->is_forwarder())
114 gsym = symtab->resolve_forwards(gsym);
116 scan.global(symtab, layout, target, object, data_shndx,
117 output_section, reloc, r_type, gsym);
122 // Behavior for relocations to discarded comdat sections.
126 CB_UNDETERMINED, // Not yet determined -- need to look at section name.
127 CB_PRETEND, // Attempt to map to the corresponding kept section.
128 CB_IGNORE, // Ignore the relocation.
129 CB_WARNING // Print a warning.
132 // Decide what the linker should do for relocations that refer to discarded
133 // comdat sections. This decision is based on the name of the section being
136 inline Comdat_behavior
137 get_comdat_behavior(const char* name)
139 if (Layout::is_debug_info_section(name))
141 if (strcmp(name, ".eh_frame") == 0
142 || strcmp(name, ".gcc_except_table") == 0)
147 // Give an error for a symbol with non-default visibility which is not
151 visibility_error(const Symbol* sym)
154 switch (sym->visibility())
156 case elfcpp::STV_INTERNAL:
159 case elfcpp::STV_HIDDEN:
162 case elfcpp::STV_PROTECTED:
168 gold_error(_("%s symbol '%s' is not defined locally"),
172 // This function implements the generic part of relocation processing.
173 // The template parameter Relocate must be a class type which provides
174 // a single function, relocate(), which implements the machine
175 // specific part of a relocation.
177 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
178 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
179 // RELOCATE implements operator() to do a relocation.
181 // PRELOCS points to the relocation data. RELOC_COUNT is the number
182 // of relocs. OUTPUT_SECTION is the output section.
183 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
184 // mapped to output offsets.
186 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
187 // VIEW_SIZE is the size. These refer to the input section, unless
188 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
189 // the output section.
191 // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is
192 // not NULL, it is a vector indexed by relocation index. If that
193 // entry is not NULL, it points to a global symbol which used as the
194 // symbol for the relocation, ignoring the symbol index in the
197 template<int size, bool big_endian, typename Target_type, int sh_type,
201 const Relocate_info<size, big_endian>* relinfo,
203 const unsigned char* prelocs,
205 Output_section* output_section,
206 bool needs_special_offset_handling,
208 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
209 section_size_type view_size,
210 const Reloc_symbol_changes* reloc_symbol_changes)
212 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
213 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
216 Sized_relobj<size, big_endian>* object = relinfo->object;
217 unsigned int local_count = object->local_symbol_count();
219 Comdat_behavior comdat_behavior = CB_UNDETERMINED;
221 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
223 Reltype reloc(prelocs);
225 section_offset_type offset =
226 convert_to_section_size_type(reloc.get_r_offset());
228 if (needs_special_offset_handling)
230 offset = output_section->output_offset(relinfo->object,
237 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
238 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
239 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
241 const Sized_symbol<size>* sym;
243 Symbol_value<size> symval;
244 const Symbol_value<size> *psymval;
245 bool is_defined_in_discarded_section;
247 if (r_sym < local_count
248 && (reloc_symbol_changes == NULL
249 || (*reloc_symbol_changes)[i] == NULL))
252 psymval = object->local_symbol(r_sym);
254 // If the local symbol belongs to a section we are discarding,
255 // and that section is a debug section, try to find the
256 // corresponding kept section and map this symbol to its
257 // counterpart in the kept section. The symbol must not
258 // correspond to a section we are folding.
260 shndx = psymval->input_shndx(&is_ordinary);
261 is_defined_in_discarded_section =
263 && shndx != elfcpp::SHN_UNDEF
264 && !object->is_section_included(shndx)
265 && !relinfo->symtab->is_section_folded(object, shndx));
270 if (reloc_symbol_changes != NULL
271 && (*reloc_symbol_changes)[i] != NULL)
272 gsym = (*reloc_symbol_changes)[i];
275 gsym = object->global_symbol(r_sym);
276 gold_assert(gsym != NULL);
277 if (gsym->is_forwarder())
278 gsym = relinfo->symtab->resolve_forwards(gsym);
281 sym = static_cast<const Sized_symbol<size>*>(gsym);
282 if (sym->has_symtab_index() && sym->symtab_index() != -1U)
283 symval.set_output_symtab_index(sym->symtab_index());
285 symval.set_no_output_symtab_entry();
286 symval.set_output_value(sym->value());
289 is_defined_in_discarded_section =
290 (gsym->is_defined_in_discarded_section()
291 && gsym->is_undefined());
295 Symbol_value<size> symval2;
296 if (is_defined_in_discarded_section)
298 if (comdat_behavior == CB_UNDETERMINED)
300 std::string name = object->section_name(relinfo->data_shndx);
301 comdat_behavior = get_comdat_behavior(name.c_str());
303 if (comdat_behavior == CB_PRETEND)
305 // FIXME: This case does not work for global symbols.
306 // We have no place to store the original section index.
307 // Fortunately this does not matter for comdat sections,
308 // only for sections explicitly discarded by a linker
311 typename elfcpp::Elf_types<size>::Elf_Addr value =
312 object->map_to_kept_section(shndx, &found);
314 symval2.set_output_value(value + psymval->input_value());
316 symval2.set_output_value(0);
320 if (comdat_behavior == CB_WARNING)
321 gold_warning_at_location(relinfo, i, offset,
322 _("relocation refers to discarded "
324 symval2.set_output_value(0);
326 symval2.set_no_output_symtab_entry();
330 if (!relocate.relocate(relinfo, target, output_section, i, reloc,
331 r_type, sym, psymval, view + offset,
332 view_address + offset, view_size))
335 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
337 gold_error_at_location(relinfo, i, offset,
338 _("reloc has bad offset %zu"),
339 static_cast<size_t>(offset));
344 && (sym->is_undefined() || sym->is_placeholder())
345 && sym->binding() != elfcpp::STB_WEAK
346 && !is_defined_in_discarded_section
347 && !target->is_defined_by_abi(sym)
348 && (!parameters->options().shared() // -shared
349 || parameters->options().defs())) // -z defs
350 gold_undefined_symbol_at_location(sym, relinfo, i, offset);
352 && sym->visibility() != elfcpp::STV_DEFAULT
353 && (sym->is_undefined() || sym->is_from_dynobj()))
354 visibility_error(sym);
356 if (sym != NULL && sym->has_warning())
357 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
361 // This class may be used as a typical class for the
362 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The
363 // template parameter Classify_reloc must be a class type which
364 // provides a function get_size_for_reloc which returns the number of
365 // bytes to which a reloc applies. This class is intended to capture
366 // the most typical target behaviour, while still permitting targets
367 // to define their own independent class for Scan_relocatable_reloc.
369 template<int sh_type, typename Classify_reloc>
370 class Default_scan_relocatable_relocs
373 // Return the strategy to use for a local symbol which is not a
374 // section symbol, given the relocation type.
375 inline Relocatable_relocs::Reloc_strategy
376 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
378 // We assume that relocation type 0 is NONE. Targets which are
379 // different must override.
380 if (r_type == 0 && r_sym == 0)
381 return Relocatable_relocs::RELOC_DISCARD;
382 return Relocatable_relocs::RELOC_COPY;
385 // Return the strategy to use for a local symbol which is a section
386 // symbol, given the relocation type.
387 inline Relocatable_relocs::Reloc_strategy
388 local_section_strategy(unsigned int r_type, Relobj* object)
390 if (sh_type == elfcpp::SHT_RELA)
391 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
394 Classify_reloc classify;
395 switch (classify.get_size_for_reloc(r_type, object))
398 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
400 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1;
402 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2;
404 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4;
406 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8;
413 // Return the strategy to use for a global symbol, given the
414 // relocation type, the object, and the symbol index.
415 inline Relocatable_relocs::Reloc_strategy
416 global_strategy(unsigned int, Relobj*, unsigned int)
417 { return Relocatable_relocs::RELOC_COPY; }
420 // Scan relocs during a relocatable link. This is a default
421 // definition which should work for most targets.
422 // Scan_relocatable_reloc must name a class type which provides three
423 // functions which return a Relocatable_relocs::Reloc_strategy code:
424 // global_strategy, local_non_section_strategy, and
425 // local_section_strategy. Most targets should be able to use
426 // Default_scan_relocatable_relocs as this class.
428 template<int size, bool big_endian, int sh_type,
429 typename Scan_relocatable_reloc>
431 scan_relocatable_relocs(
434 Sized_relobj<size, big_endian>* object,
435 unsigned int data_shndx,
436 const unsigned char* prelocs,
438 Output_section* output_section,
439 bool needs_special_offset_handling,
440 size_t local_symbol_count,
441 const unsigned char* plocal_syms,
442 Relocatable_relocs* rr)
444 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
445 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
446 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
447 Scan_relocatable_reloc scan;
449 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
451 Reltype reloc(prelocs);
453 Relocatable_relocs::Reloc_strategy strategy;
455 if (needs_special_offset_handling
456 && !output_section->is_input_address_mapped(object, data_shndx,
457 reloc.get_r_offset()))
458 strategy = Relocatable_relocs::RELOC_DISCARD;
461 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
463 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
464 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
466 if (r_sym >= local_symbol_count)
467 strategy = scan.global_strategy(r_type, object, r_sym);
470 gold_assert(plocal_syms != NULL);
471 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
473 unsigned int shndx = lsym.get_st_shndx();
475 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
477 && shndx != elfcpp::SHN_UNDEF
478 && !object->is_section_included(shndx))
480 // RELOC is a relocation against a local symbol
481 // defined in a section we are discarding. Discard
482 // the reloc. FIXME: Should we issue a warning?
483 strategy = Relocatable_relocs::RELOC_DISCARD;
485 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
486 strategy = scan.local_non_section_strategy(r_type, object,
490 strategy = scan.local_section_strategy(r_type, object);
491 if (strategy != Relocatable_relocs::RELOC_DISCARD)
492 object->output_section(shndx)->set_needs_symtab_index();
495 if (strategy == Relocatable_relocs::RELOC_COPY)
496 object->set_must_have_output_symtab_entry(r_sym);
500 rr->set_next_reloc_strategy(strategy);
504 // Relocate relocs during a relocatable link. This is a default
505 // definition which should work for most targets.
507 template<int size, bool big_endian, int sh_type>
509 relocate_for_relocatable(
510 const Relocate_info<size, big_endian>* relinfo,
511 const unsigned char* prelocs,
513 Output_section* output_section,
514 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
515 const Relocatable_relocs* rr,
517 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
518 section_size_type view_size,
519 unsigned char* reloc_view,
520 section_size_type reloc_view_size)
522 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
523 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
524 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write
526 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
527 const Address invalid_address = static_cast<Address>(0) - 1;
529 Sized_relobj<size, big_endian>* const object = relinfo->object;
530 const unsigned int local_count = object->local_symbol_count();
532 unsigned char* pwrite = reloc_view;
534 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
536 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
537 if (strategy == Relocatable_relocs::RELOC_DISCARD)
540 if (strategy == Relocatable_relocs::RELOC_SPECIAL)
542 // Target wants to handle this relocation.
543 Sized_target<size, big_endian>* target =
544 parameters->sized_target<size, big_endian>();
545 target->relocate_special_relocatable(relinfo, sh_type, prelocs,
547 offset_in_output_section,
550 pwrite += reloc_size;
553 Reltype reloc(prelocs);
554 Reltype_write reloc_write(pwrite);
556 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
557 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
558 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
560 // Get the new symbol index.
562 unsigned int new_symndx;
563 if (r_sym < local_count)
567 case Relocatable_relocs::RELOC_COPY:
572 new_symndx = object->symtab_index(r_sym);
573 gold_assert(new_symndx != -1U);
577 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
578 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
579 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
580 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
581 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
582 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
584 // We are adjusting a section symbol. We need to find
585 // the symbol table index of the section symbol for
586 // the output section corresponding to input section
587 // in which this symbol is defined.
588 gold_assert(r_sym < local_count);
591 object->local_symbol_input_shndx(r_sym, &is_ordinary);
592 gold_assert(is_ordinary);
593 Output_section* os = object->output_section(shndx);
594 gold_assert(os != NULL);
595 gold_assert(os->needs_symtab_index());
596 new_symndx = os->symtab_index();
606 const Symbol* gsym = object->global_symbol(r_sym);
607 gold_assert(gsym != NULL);
608 if (gsym->is_forwarder())
609 gsym = relinfo->symtab->resolve_forwards(gsym);
611 gold_assert(gsym->has_symtab_index());
612 new_symndx = gsym->symtab_index();
615 // Get the new offset--the location in the output section where
616 // this relocation should be applied.
618 Address offset = reloc.get_r_offset();
620 if (offset_in_output_section != invalid_address)
621 new_offset = offset + offset_in_output_section;
624 section_offset_type sot_offset =
625 convert_types<section_offset_type, Address>(offset);
626 section_offset_type new_sot_offset =
627 output_section->output_offset(object, relinfo->data_shndx,
629 gold_assert(new_sot_offset != -1);
630 new_offset = new_sot_offset;
633 // In an object file, r_offset is an offset within the section.
634 // In an executable or dynamic object, generated by
635 // --emit-relocs, r_offset is an absolute address.
636 if (!parameters->options().relocatable())
638 new_offset += view_address;
639 if (offset_in_output_section != invalid_address)
640 new_offset -= offset_in_output_section;
643 reloc_write.put_r_offset(new_offset);
644 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
646 // Handle the reloc addend based on the strategy.
648 if (strategy == Relocatable_relocs::RELOC_COPY)
650 if (sh_type == elfcpp::SHT_RELA)
651 Reloc_types<sh_type, size, big_endian>::
652 copy_reloc_addend(&reloc_write,
657 // The relocation uses a section symbol in the input file.
658 // We are adjusting it to use a section symbol in the output
659 // file. The input section symbol refers to some address in
660 // the input section. We need the relocation in the output
661 // file to refer to that same address. This adjustment to
662 // the addend is the same calculation we use for a simple
663 // absolute relocation for the input section symbol.
665 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
667 unsigned char* padd = view + offset;
670 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
672 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
673 addend = Reloc_types<sh_type, size, big_endian>::
674 get_reloc_addend(&reloc);
675 addend = psymval->value(object, addend);
676 Reloc_types<sh_type, size, big_endian>::
677 set_reloc_addend(&reloc_write, addend);
681 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0:
684 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1:
685 Relocate_functions<size, big_endian>::rel8(padd, object,
689 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2:
690 Relocate_functions<size, big_endian>::rel16(padd, object,
694 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4:
695 Relocate_functions<size, big_endian>::rel32(padd, object,
699 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8:
700 Relocate_functions<size, big_endian>::rel64(padd, object,
709 pwrite += reloc_size;
712 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
716 } // End namespace gold.
718 #endif // !defined(GOLD_TARGET_RELOC_H)