Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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" | |
c06b7b0b | 28 | #include "reloc-types.h" |
61ba1cf9 ILT |
29 | |
30 | namespace gold | |
31 | { | |
32 | ||
92e059d8 | 33 | // This function implements the generic part of reloc scanning. This |
436ca963 ILT |
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. | |
92e059d8 | 38 | |
ead1e424 ILT |
39 | template<int size, bool big_endian, typename Target_type, int sh_type, |
40 | typename Scan> | |
92e059d8 ILT |
41 | inline void |
42 | scan_relocs( | |
43 | const General_options& options, | |
44 | Symbol_table* symtab, | |
ead1e424 ILT |
45 | Layout* layout, |
46 | Target_type* target, | |
f6ce93d6 | 47 | Sized_relobj<size, big_endian>* object, |
a3ad94ed | 48 | unsigned int data_shndx, |
92e059d8 ILT |
49 | const unsigned char* prelocs, |
50 | size_t reloc_count, | |
730cdc88 ILT |
51 | Output_section* output_section, |
52 | bool needs_special_offset_handling, | |
92e059d8 | 53 | size_t local_count, |
730cdc88 | 54 | const unsigned char* plocal_syms) |
92e059d8 ILT |
55 | { |
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; | |
60 | ||
61 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) | |
62 | { | |
63 | Reltype reloc(prelocs); | |
64 | ||
730cdc88 ILT |
65 | if (needs_special_offset_handling |
66 | && !output_section->is_input_address_mapped(object, data_shndx, | |
67 | reloc.get_r_offset())) | |
68 | continue; | |
69 | ||
92e059d8 ILT |
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); | |
73 | ||
74 | if (r_sym < local_count) | |
75 | { | |
a3ad94ed | 76 | gold_assert(plocal_syms != NULL); |
92e059d8 ILT |
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 | |
ead1e424 | 81 | && shndx != elfcpp::SHN_UNDEF |
92e059d8 ILT |
82 | && !object->is_section_included(lsym.get_st_shndx())) |
83 | { | |
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. | |
88 | // | |
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. | |
98 | ||
99 | continue; | |
100 | } | |
101 | ||
a3ad94ed ILT |
102 | scan.local(options, symtab, layout, target, object, data_shndx, |
103 | reloc, r_type, lsym); | |
92e059d8 ILT |
104 | } |
105 | else | |
106 | { | |
730cdc88 | 107 | Symbol* gsym = object->global_symbol(r_sym); |
a3ad94ed | 108 | gold_assert(gsym != NULL); |
92e059d8 ILT |
109 | if (gsym->is_forwarder()) |
110 | gsym = symtab->resolve_forwards(gsym); | |
111 | ||
a3ad94ed ILT |
112 | scan.global(options, symtab, layout, target, object, data_shndx, |
113 | reloc, r_type, gsym); | |
92e059d8 ILT |
114 | } |
115 | } | |
116 | } | |
117 | ||
118 | // This function implements the generic part of relocation processing. | |
72a2eed7 | 119 | // This is an inline function which take a class whose relocate() |
61ba1cf9 ILT |
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. | |
124 | ||
125 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of | |
92e059d8 ILT |
126 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
127 | // RELOCATE implements operator() to do a relocation. | |
61ba1cf9 | 128 | |
92e059d8 | 129 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
730cdc88 ILT |
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. | |
133 | ||
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. | |
61ba1cf9 | 138 | |
ead1e424 ILT |
139 | template<int size, bool big_endian, typename Target_type, int sh_type, |
140 | typename Relocate> | |
61ba1cf9 ILT |
141 | inline void |
142 | relocate_section( | |
92e059d8 | 143 | const Relocate_info<size, big_endian>* relinfo, |
ead1e424 | 144 | Target_type* target, |
61ba1cf9 ILT |
145 | const unsigned char* prelocs, |
146 | size_t reloc_count, | |
730cdc88 ILT |
147 | Output_section* output_section, |
148 | bool needs_special_offset_handling, | |
61ba1cf9 ILT |
149 | unsigned char* view, |
150 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
151 | off_t view_size) | |
152 | { | |
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; | |
156 | ||
730cdc88 ILT |
157 | Sized_relobj<size, big_endian>* object = relinfo->object; |
158 | unsigned int local_count = object->local_symbol_count(); | |
92e059d8 | 159 | |
61ba1cf9 ILT |
160 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
161 | { | |
162 | Reltype reloc(prelocs); | |
163 | ||
164 | off_t offset = reloc.get_r_offset(); | |
61ba1cf9 | 165 | |
730cdc88 ILT |
166 | if (needs_special_offset_handling) |
167 | { | |
168 | offset = output_section->output_offset(relinfo->object, | |
169 | relinfo->data_shndx, | |
170 | offset); | |
171 | if (offset == -1) | |
172 | continue; | |
173 | } | |
174 | ||
61ba1cf9 ILT |
175 | typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); |
176 | unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); | |
177 | unsigned int r_type = elfcpp::elf_r_type<size>(r_info); | |
178 | ||
c06b7b0b | 179 | const Sized_symbol<size>* sym; |
61ba1cf9 | 180 | |
b8e6aad9 ILT |
181 | Symbol_value<size> symval; |
182 | const Symbol_value<size> *psymval; | |
61ba1cf9 ILT |
183 | if (r_sym < local_count) |
184 | { | |
185 | sym = NULL; | |
730cdc88 | 186 | psymval = object->local_symbol(r_sym); |
61ba1cf9 ILT |
187 | } |
188 | else | |
189 | { | |
730cdc88 | 190 | const Symbol* gsym = object->global_symbol(r_sym); |
a3ad94ed | 191 | gold_assert(gsym != NULL); |
61ba1cf9 | 192 | if (gsym->is_forwarder()) |
92e059d8 | 193 | gsym = relinfo->symtab->resolve_forwards(gsym); |
61ba1cf9 | 194 | |
c06b7b0b | 195 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
b8e6aad9 ILT |
196 | if (sym->has_symtab_index()) |
197 | symval.set_output_symtab_index(sym->symtab_index()); | |
198 | else | |
199 | symval.set_no_output_symtab_entry(); | |
200 | symval.set_output_value(sym->value()); | |
201 | psymval = &symval; | |
ead1e424 | 202 | } |
61ba1cf9 | 203 | |
b8e6aad9 | 204 | if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval, |
ead1e424 ILT |
205 | view + offset, view_address + offset, view_size)) |
206 | continue; | |
207 | ||
208 | if (offset < 0 || offset >= view_size) | |
209 | { | |
75f2446e ILT |
210 | gold_error_at_location(relinfo, i, offset, |
211 | _("reloc has bad offset %zu"), | |
212 | static_cast<size_t>(offset)); | |
213 | continue; | |
61ba1cf9 ILT |
214 | } |
215 | ||
ead1e424 ILT |
216 | if (sym != NULL |
217 | && sym->is_undefined() | |
436ca963 ILT |
218 | && sym->binding() != elfcpp::STB_WEAK |
219 | && !parameters->output_is_shared()) | |
75f2446e | 220 | gold_undefined_symbol(sym, relinfo, i, offset); |
f6ce93d6 ILT |
221 | |
222 | if (sym != NULL && sym->has_warning()) | |
75f2446e | 223 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
61ba1cf9 ILT |
224 | } |
225 | } | |
226 | ||
227 | } // End namespace gold. | |
228 | ||
229 | #endif // !defined(GOLD_TARGET_RELOC_H) |