]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/reloc.h
Add licensing text to every source file.
[thirdparty/binutils-gdb.git] / gold / reloc.h
CommitLineData
61ba1cf9
ILT
1// reloc.h -- relocate input files for gold -*- 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_RELOC_H
24#define GOLD_RELOC_H
25
92e059d8
ILT
26#include <byteswap.h>
27
61ba1cf9
ILT
28#include "workqueue.h"
29
30namespace gold
31{
32
a3ad94ed 33class General_options;
f6ce93d6 34class Relobj;
92e059d8 35class Read_relocs_data;
a3ad94ed 36class Symbol;
ead1e424 37class Layout;
92e059d8 38
5a6f7e2d
ILT
39template<int size>
40class Sized_symbol;
41
b8e6aad9
ILT
42template<int size, bool big_endian>
43class Sized_relobj;
44
45template<int size>
46class Symbol_value;
47
5a6f7e2d
ILT
48template<int sh_type, bool dynamic, int size, bool big_endian>
49class Output_data_reloc;
50
92e059d8
ILT
51// A class to read the relocations for an object file, and then queue
52// up a task to see if they require any GOT/PLT/COPY relocations in
53// the symbol table.
54
55class Read_relocs : public Task
56{
57 public:
58 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
59 // unblocked when the Scan_relocs task completes.
60 Read_relocs(const General_options& options, Symbol_table* symtab,
f6ce93d6 61 Layout* layout, Relobj* object, Task_token* symtab_lock,
92e059d8 62 Task_token* blocker)
ead1e424 63 : options_(options), symtab_(symtab), layout_(layout), object_(object),
92e059d8
ILT
64 symtab_lock_(symtab_lock), blocker_(blocker)
65 { }
66
67 // The standard Task methods.
68
69 Is_runnable_type
70 is_runnable(Workqueue*);
71
72 Task_locker*
73 locks(Workqueue*);
74
75 void
76 run(Workqueue*);
77
78 private:
79 const General_options& options_;
80 Symbol_table* symtab_;
ead1e424 81 Layout* layout_;
f6ce93d6 82 Relobj* object_;
92e059d8
ILT
83 Task_token* symtab_lock_;
84 Task_token* blocker_;
85};
86
87// Scan the relocations for an object to see if they require any
88// GOT/PLT/COPY relocations.
89
90class Scan_relocs : public Task
91{
92 public:
93 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
94 // unblocked when the task completes.
95 Scan_relocs(const General_options& options, Symbol_table* symtab,
f6ce93d6 96 Layout* layout, Relobj* object, Read_relocs_data* rd,
ead1e424
ILT
97 Task_token* symtab_lock, Task_token* blocker)
98 : options_(options), symtab_(symtab), layout_(layout), object_(object),
99 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
92e059d8
ILT
100 { }
101
102 // The standard Task methods.
103
104 Is_runnable_type
105 is_runnable(Workqueue*);
106
107 Task_locker*
108 locks(Workqueue*);
109
110 void
111 run(Workqueue*);
112
113 private:
114 class Scan_relocs_locker;
115
116 const General_options& options_;
117 Symbol_table* symtab_;
ead1e424 118 Layout* layout_;
f6ce93d6 119 Relobj* object_;
92e059d8
ILT
120 Read_relocs_data* rd_;
121 Task_token* symtab_lock_;
122 Task_token* blocker_;
123};
124
125// A class to perform all the relocations for an object file.
126
61ba1cf9
ILT
127class Relocate_task : public Task
128{
129 public:
130 Relocate_task(const General_options& options, const Symbol_table* symtab,
f6ce93d6 131 const Layout* layout, Relobj* object, Output_file* of,
61ba1cf9 132 Task_token* final_blocker)
92e059d8 133 : options_(options), symtab_(symtab), layout_(layout), object_(object),
61ba1cf9
ILT
134 of_(of), final_blocker_(final_blocker)
135 { }
136
137 // The standard Task methods.
138
139 Is_runnable_type
140 is_runnable(Workqueue*);
141
142 Task_locker*
143 locks(Workqueue*);
144
145 void
146 run(Workqueue*);
147
148 private:
149 class Relocate_locker;
150
151 const General_options& options_;
152 const Symbol_table* symtab_;
92e059d8 153 const Layout* layout_;
f6ce93d6 154 Relobj* object_;
61ba1cf9
ILT
155 Output_file* of_;
156 Task_token* final_blocker_;
157};
158
92e059d8
ILT
159// Standard relocation routines which are used on many targets. Here
160// SIZE and BIG_ENDIAN refer to the target, not the relocation type.
161
162template<int size, bool big_endian>
163class Relocate_functions
164{
165private:
166 // Do a simple relocation with the addend in the section contents.
167 // VALSIZE is the size of the value.
168 template<int valsize>
169 static inline void
f6ce93d6
ILT
170 rel(unsigned char* view,
171 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
92e059d8 172 {
f6ce93d6 173 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
92e059d8 174 Valtype* wv = reinterpret_cast<Valtype*>(view);
f6ce93d6
ILT
175 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
176 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
92e059d8
ILT
177 }
178
b8e6aad9
ILT
179 // Do a simple relocation using a Symbol_value with the addend in
180 // the section contents. VALSIZE is the size of the value to
181 // relocate.
182 template<int valsize>
183 static inline void
184 rel(unsigned char* view,
185 const Sized_relobj<size, big_endian>* object,
186 const Symbol_value<size>* psymval)
187 {
188 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
189 Valtype* wv = reinterpret_cast<Valtype*>(view);
190 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
191 x = psymval->value(object, x);
192 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
193 }
194
92e059d8
ILT
195 // Do a simple PC relative relocation with the addend in the section
196 // contents. VALSIZE is the size of the value.
197 template<int valsize>
198 static inline void
f6ce93d6
ILT
199 pcrel(unsigned char* view,
200 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
92e059d8
ILT
201 typename elfcpp::Elf_types<size>::Elf_Addr address)
202 {
f6ce93d6 203 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
92e059d8 204 Valtype* wv = reinterpret_cast<Valtype*>(view);
f6ce93d6
ILT
205 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
206 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
92e059d8
ILT
207 }
208
b8e6aad9
ILT
209 // Do a simple PC relative relocation with a Symbol_value with the
210 // addend in the section contents. VALSIZE is the size of the
211 // value.
212 template<int valsize>
213 static inline void
214 pcrel(unsigned char* view,
215 const Sized_relobj<size, big_endian>* object,
216 const Symbol_value<size>* psymval,
217 typename elfcpp::Elf_types<size>::Elf_Addr address)
218 {
219 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
220 Valtype* wv = reinterpret_cast<Valtype*>(view);
221 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
222 x = psymval->value(object, x);
223 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
224 }
225
92e059d8
ILT
226 typedef Relocate_functions<size, big_endian> This;
227
228public:
b8e6aad9
ILT
229 // Do a simple 8-bit REL relocation with the addend in the section
230 // contents.
92e059d8
ILT
231 static inline void
232 rel8(unsigned char* view, unsigned char value)
b8e6aad9
ILT
233 { This::template rel<8>(view, value); }
234
235 static inline void
236 rel8(unsigned char* view,
237 const Sized_relobj<size, big_endian>* object,
238 const Symbol_value<size>* psymval)
239 { This::template rel<8>(view, object, psymval); }
92e059d8
ILT
240
241 // Do a simple 8-bit PC relative relocation with the addend in the
b8e6aad9 242 // section contents.
92e059d8
ILT
243 static inline void
244 pcrel8(unsigned char* view, unsigned char value,
245 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
246 { This::template pcrel<8>(view, value, address); }
247
248 static inline void
249 pcrel8(unsigned char* view,
250 const Sized_relobj<size, big_endian>* object,
251 const Symbol_value<size>* psymval,
252 typename elfcpp::Elf_types<size>::Elf_Addr address)
253 { This::template pcrel<8>(view, object, psymval, address); }
92e059d8 254
b8e6aad9
ILT
255 // Do a simple 16-bit REL relocation with the addend in the section
256 // contents.
92e059d8
ILT
257 static inline void
258 rel16(unsigned char* view, elfcpp::Elf_Half value)
b8e6aad9
ILT
259 { This::template rel<16>(view, value); }
260
261 static inline void
262 rel16(unsigned char* view,
263 const Sized_relobj<size, big_endian>* object,
264 const Symbol_value<size>* psymval)
265 { This::template rel<16>(view, object, psymval); }
92e059d8
ILT
266
267 // Do a simple 32-bit PC relative REL relocation with the addend in
b8e6aad9 268 // the section contents.
92e059d8
ILT
269 static inline void
270 pcrel16(unsigned char* view, elfcpp::Elf_Word value,
271 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
272 { This::template pcrel<16>(view, value, address); }
273
274 static inline void
275 pcrel16(unsigned char* view,
276 const Sized_relobj<size, big_endian>* object,
277 const Symbol_value<size>* psymval,
278 typename elfcpp::Elf_types<size>::Elf_Addr address)
279 { This::template pcrel<16>(view, object, psymval, address); }
92e059d8
ILT
280
281 // Do a simple 32-bit REL relocation with the addend in the section
282 // contents.
283 static inline void
284 rel32(unsigned char* view, elfcpp::Elf_Word value)
b8e6aad9
ILT
285 { This::template rel<32>(view, value); }
286
287 static inline void
288 rel32(unsigned char* view,
289 const Sized_relobj<size, big_endian>* object,
290 const Symbol_value<size>* psymval)
291 { This::template rel<32>(view, object, psymval); }
92e059d8
ILT
292
293 // Do a simple 32-bit PC relative REL relocation with the addend in
294 // the section contents.
295 static inline void
296 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
297 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
298 { This::template pcrel<32>(view, value, address); }
299
300 static inline void
301 pcrel32(unsigned char* view,
302 const Sized_relobj<size, big_endian>* object,
303 const Symbol_value<size>* psymval,
304 typename elfcpp::Elf_types<size>::Elf_Addr address)
305 { This::template pcrel<32>(view, object, psymval, address); }
92e059d8
ILT
306
307 // Do a simple 64-bit REL relocation with the addend in the section
308 // contents.
309 static inline void
f6ce93d6 310 rel64(unsigned char* view, elfcpp::Elf_Xword value)
b8e6aad9
ILT
311 { This::template rel<64>(view, value); }
312
313 static inline void
314 rel64(unsigned char* view,
315 const Sized_relobj<size, big_endian>* object,
316 const Symbol_value<size>* psymval)
317 { This::template rel<64>(view, object, psymval); }
92e059d8
ILT
318
319 // Do a simple 64-bit PC relative REL relocation with the addend in
320 // the section contents.
321 static inline void
f6ce93d6 322 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
92e059d8 323 typename elfcpp::Elf_types<size>::Elf_Addr address)
b8e6aad9
ILT
324 { This::template pcrel<64>(view, value, address); }
325
326 static inline void
327 pcrel64(unsigned char* view,
328 const Sized_relobj<size, big_endian>* object,
329 const Symbol_value<size>* psymval,
330 typename elfcpp::Elf_types<size>::Elf_Addr address)
331 { This::template pcrel<64>(view, object, psymval, address); }
5a6f7e2d
ILT
332};
333
334// We try to avoid COPY relocations when possible. A COPY relocation
335// may be required when an executable refers to a variable defined in
336// a shared library. COPY relocations are problematic because they
337// tie the executable to the exact size of the variable in the shared
338// library. We can avoid them if all the references to the variable
339// are in a writeable section. In that case we can simply use dynamic
340// relocations. However, when scanning relocs, we don't know when we
341// see the relocation whether we will be forced to use a COPY
342// relocation or not. So we have to save the relocation during the
343// reloc scanning, and then emit it as a dynamic relocation if
344// necessary. This class implements that. It is used by the target
345// specific code.
346
347template<int size, bool big_endian>
348class Copy_relocs
349{
350 public:
351 Copy_relocs()
352 : entries_()
353 { }
a3ad94ed
ILT
354
355 // Return whether we need a COPY reloc for a reloc against GSYM,
356 // which is being applied to section SHNDX in OBJECT.
357 static bool
358 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
5a6f7e2d
ILT
359 Sized_symbol<size>* gsym);
360
361 // Save a Rel against SYM for possible emission later. SHNDX is the
362 // index of the section to which the reloc is being applied.
363 void
364 save(Symbol* sym, Relobj*, unsigned int shndx,
365 const elfcpp::Rel<size, big_endian>&);
366
367 // Save a Rela against SYM for possible emission later.
368 void
369 save(Symbol* sym, Relobj*, unsigned int shndx,
370 const elfcpp::Rela<size, big_endian>&);
371
372 // Return whether there are any relocs to emit. This also discards
373 // entries which need not be emitted.
374 bool
375 any_to_emit();
376
377 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
378 // is still defined in the dynamic object).
379 template<int sh_type>
380 void
381 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
382
383 private:
384 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
385 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
386
387 // This POD class holds the entries we are saving.
388 class Copy_reloc_entry
389 {
390 public:
391 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
392 Relobj* relobj, unsigned int shndx,
393 Address address, Addend addend)
394 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
395 shndx_(shndx), address_(address), addend_(addend)
396 { }
397
398 // Return whether we should emit this reloc. If we should not
399 // emit, we clear it.
400 bool
401 should_emit();
402
403 // Emit this reloc.
404
405 void
406 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
407
408 void
409 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
410
411 private:
412 Symbol* sym_;
413 unsigned int reloc_type_;
414 Relobj* relobj_;
415 unsigned int shndx_;
416 Address address_;
417 Addend addend_;
418 };
419
420 // A list of relocs to be saved.
421 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
422
423 // The list of relocs we are saving.
424 Copy_reloc_entries entries_;
92e059d8
ILT
425};
426
61ba1cf9
ILT
427} // End namespace gold.
428
429#endif // !defined(GOLD_RELOC_H)