daily update
[binutils.git] / gold / target-reloc.h
blob771869149caaac6a35973edcfb822cd24555a673
1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 // Copyright 2006, 2007 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 "reloc-types.h"
30 namespace gold
33 // This function implements the generic part of reloc scanning. This
34 // is an inline function which takes a class whose member functions
35 // local() and global() implement the machine specific part of scanning.
36 // We do it this way to avoidmaking a function call for each relocation,
37 // and to avoid repeating the generic code for each target.
39 template<int size, bool big_endian, typename Target_type, int sh_type,
40 typename Scan>
41 inline void
42 scan_relocs(
43 const General_options& options,
44 Symbol_table* symtab,
45 Layout* layout,
46 Target_type* target,
47 Sized_relobj<size, big_endian>* object,
48 unsigned int data_shndx,
49 const unsigned char* prelocs,
50 size_t reloc_count,
51 Output_section* output_section,
52 bool needs_special_offset_handling,
53 size_t local_count,
54 const unsigned char* plocal_syms)
56 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
57 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
58 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
59 Scan scan;
61 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
63 Reltype reloc(prelocs);
65 if (needs_special_offset_handling
66 && !output_section->is_input_address_mapped(object, data_shndx,
67 reloc.get_r_offset()))
68 continue;
70 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
71 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
72 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
74 if (r_sym < local_count)
76 gold_assert(plocal_syms != NULL);
77 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
78 + r_sym * sym_size);
79 const unsigned int shndx = lsym.get_st_shndx();
80 if (shndx < elfcpp::SHN_LORESERVE
81 && shndx != elfcpp::SHN_UNDEF
82 && !object->is_section_included(lsym.get_st_shndx()))
84 // RELOC is a relocation against a local symbol in a
85 // section we are discarding. We can ignore this
86 // relocation. It will eventually become a reloc
87 // against the value zero.
89 // FIXME: We should issue a warning if this is an
90 // allocated section; is this the best place to do it?
91 //
92 // FIXME: The old GNU linker would in some cases look
93 // for the linkonce section which caused this section to
94 // be discarded, and, if the other section was the same
95 // size, change the reloc to refer to the other section.
96 // That seems risky and weird to me, and I don't know of
97 // any case where it is actually required.
99 continue;
102 scan.local(options, symtab, layout, target, object, data_shndx,
103 output_section, reloc, r_type, lsym);
105 else
107 Symbol* gsym = object->global_symbol(r_sym);
108 gold_assert(gsym != NULL);
109 if (gsym->is_forwarder())
110 gsym = symtab->resolve_forwards(gsym);
112 scan.global(options, symtab, layout, target, object, data_shndx,
113 output_section, reloc, r_type, gsym);
118 // This function implements the generic part of relocation processing.
119 // This is an inline function which take a class whose relocate()
120 // implements the machine specific part of relocation. We do it this
121 // way to avoid making a function call for each relocation, and to
122 // avoid repeating the generic relocation handling code for each
123 // target.
125 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
126 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
127 // RELOCATE implements operator() to do a relocation.
129 // PRELOCS points to the relocation data. RELOC_COUNT is the number
130 // of relocs. OUTPUT_SECTION is the output section.
131 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
132 // mapped to output offsets.
134 // VIEW is the section data, VIEW_ADDRESS is its memory address, and
135 // VIEW_SIZE is the size. These refer to the input section, unless
136 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
137 // the output section.
139 template<int size, bool big_endian, typename Target_type, int sh_type,
140 typename Relocate>
141 inline void
142 relocate_section(
143 const Relocate_info<size, big_endian>* relinfo,
144 Target_type* target,
145 const unsigned char* prelocs,
146 size_t reloc_count,
147 Output_section* output_section,
148 bool needs_special_offset_handling,
149 unsigned char* view,
150 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
151 section_size_type view_size)
153 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
154 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
155 Relocate relocate;
157 Sized_relobj<size, big_endian>* object = relinfo->object;
158 unsigned int local_count = object->local_symbol_count();
160 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
162 Reltype reloc(prelocs);
164 section_offset_type offset =
165 convert_to_section_size_type(reloc.get_r_offset());
167 if (needs_special_offset_handling)
169 offset = output_section->output_offset(relinfo->object,
170 relinfo->data_shndx,
171 offset);
172 if (offset == -1)
173 continue;
176 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
177 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
178 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
180 const Sized_symbol<size>* sym;
182 Symbol_value<size> symval;
183 const Symbol_value<size> *psymval;
184 if (r_sym < local_count)
186 sym = NULL;
187 psymval = object->local_symbol(r_sym);
189 else
191 const Symbol* gsym = object->global_symbol(r_sym);
192 gold_assert(gsym != NULL);
193 if (gsym->is_forwarder())
194 gsym = relinfo->symtab->resolve_forwards(gsym);
196 sym = static_cast<const Sized_symbol<size>*>(gsym);
197 if (sym->has_symtab_index())
198 symval.set_output_symtab_index(sym->symtab_index());
199 else
200 symval.set_no_output_symtab_entry();
201 symval.set_output_value(sym->value());
202 psymval = &symval;
205 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval,
206 view + offset, view_address + offset, view_size))
207 continue;
209 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size)
211 gold_error_at_location(relinfo, i, offset,
212 _("reloc has bad offset %zu"),
213 static_cast<size_t>(offset));
214 continue;
217 if (sym != NULL
218 && sym->is_undefined()
219 && sym->binding() != elfcpp::STB_WEAK
220 && !parameters->output_is_shared())
221 gold_undefined_symbol(sym, relinfo, i, offset);
223 if (sym != NULL && sym->has_warning())
224 relinfo->symtab->issue_warning(sym, relinfo, i, offset);
228 } // End namespace gold.
230 #endif // !defined(GOLD_TARGET_RELOC_H)