2007-08-06 Paul Brook <paul@codesourcery.com>
[binutils.git] / gold / reloc.h
blob1aa0d896717f0f363c3ca72630f36e9add70e923
1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 #ifndef GOLD_RELOC_H
4 #define GOLD_RELOC_H
6 #include <byteswap.h>
8 #include "workqueue.h"
10 namespace gold
13 class General_options;
14 class Relobj;
15 class Read_relocs_data;
16 class Symbol;
17 class Layout;
19 template<int size>
20 class Sized_symbol;
22 template<int size, bool big_endian>
23 class Sized_relobj;
25 template<int size>
26 class Symbol_value;
28 template<int sh_type, bool dynamic, int size, bool big_endian>
29 class Output_data_reloc;
31 // A class to read the relocations for an object file, and then queue
32 // up a task to see if they require any GOT/PLT/COPY relocations in
33 // the symbol table.
35 class Read_relocs : public Task
37 public:
38 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
39 // unblocked when the Scan_relocs task completes.
40 Read_relocs(const General_options& options, Symbol_table* symtab,
41 Layout* layout, Relobj* object, Task_token* symtab_lock,
42 Task_token* blocker)
43 : options_(options), symtab_(symtab), layout_(layout), object_(object),
44 symtab_lock_(symtab_lock), blocker_(blocker)
45 { }
47 // The standard Task methods.
49 Is_runnable_type
50 is_runnable(Workqueue*);
52 Task_locker*
53 locks(Workqueue*);
55 void
56 run(Workqueue*);
58 private:
59 const General_options& options_;
60 Symbol_table* symtab_;
61 Layout* layout_;
62 Relobj* object_;
63 Task_token* symtab_lock_;
64 Task_token* blocker_;
67 // Scan the relocations for an object to see if they require any
68 // GOT/PLT/COPY relocations.
70 class Scan_relocs : public Task
72 public:
73 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
74 // unblocked when the task completes.
75 Scan_relocs(const General_options& options, Symbol_table* symtab,
76 Layout* layout, Relobj* object, Read_relocs_data* rd,
77 Task_token* symtab_lock, Task_token* blocker)
78 : options_(options), symtab_(symtab), layout_(layout), object_(object),
79 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
80 { }
82 // The standard Task methods.
84 Is_runnable_type
85 is_runnable(Workqueue*);
87 Task_locker*
88 locks(Workqueue*);
90 void
91 run(Workqueue*);
93 private:
94 class Scan_relocs_locker;
96 const General_options& options_;
97 Symbol_table* symtab_;
98 Layout* layout_;
99 Relobj* object_;
100 Read_relocs_data* rd_;
101 Task_token* symtab_lock_;
102 Task_token* blocker_;
105 // A class to perform all the relocations for an object file.
107 class Relocate_task : public Task
109 public:
110 Relocate_task(const General_options& options, const Symbol_table* symtab,
111 const Layout* layout, Relobj* object, Output_file* of,
112 Task_token* final_blocker)
113 : options_(options), symtab_(symtab), layout_(layout), object_(object),
114 of_(of), final_blocker_(final_blocker)
117 // The standard Task methods.
119 Is_runnable_type
120 is_runnable(Workqueue*);
122 Task_locker*
123 locks(Workqueue*);
125 void
126 run(Workqueue*);
128 private:
129 class Relocate_locker;
131 const General_options& options_;
132 const Symbol_table* symtab_;
133 const Layout* layout_;
134 Relobj* object_;
135 Output_file* of_;
136 Task_token* final_blocker_;
139 // Standard relocation routines which are used on many targets. Here
140 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
142 template<int size, bool big_endian>
143 class Relocate_functions
145 private:
146 // Do a simple relocation with the addend in the section contents.
147 // VALSIZE is the size of the value.
148 template<int valsize>
149 static inline void
150 rel(unsigned char* view,
151 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
153 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
154 Valtype* wv = reinterpret_cast<Valtype*>(view);
155 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
156 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
159 // Do a simple relocation using a Symbol_value with the addend in
160 // the section contents. VALSIZE is the size of the value to
161 // relocate.
162 template<int valsize>
163 static inline void
164 rel(unsigned char* view,
165 const Sized_relobj<size, big_endian>* object,
166 const Symbol_value<size>* psymval)
168 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
169 Valtype* wv = reinterpret_cast<Valtype*>(view);
170 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
171 x = psymval->value(object, x);
172 elfcpp::Swap<valsize, big_endian>::writeval(wv, x);
175 // Do a simple PC relative relocation with the addend in the section
176 // contents. VALSIZE is the size of the value.
177 template<int valsize>
178 static inline void
179 pcrel(unsigned char* view,
180 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
181 typename elfcpp::Elf_types<size>::Elf_Addr address)
183 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
184 Valtype* wv = reinterpret_cast<Valtype*>(view);
185 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
186 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
189 // Do a simple PC relative relocation with a Symbol_value with the
190 // addend in the section contents. VALSIZE is the size of the
191 // value.
192 template<int valsize>
193 static inline void
194 pcrel(unsigned char* view,
195 const Sized_relobj<size, big_endian>* object,
196 const Symbol_value<size>* psymval,
197 typename elfcpp::Elf_types<size>::Elf_Addr address)
199 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
200 Valtype* wv = reinterpret_cast<Valtype*>(view);
201 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
202 x = psymval->value(object, x);
203 elfcpp::Swap<valsize, big_endian>::writeval(wv, x - address);
206 typedef Relocate_functions<size, big_endian> This;
208 public:
209 // Do a simple 8-bit REL relocation with the addend in the section
210 // contents.
211 static inline void
212 rel8(unsigned char* view, unsigned char value)
213 { This::template rel<8>(view, value); }
215 static inline void
216 rel8(unsigned char* view,
217 const Sized_relobj<size, big_endian>* object,
218 const Symbol_value<size>* psymval)
219 { This::template rel<8>(view, object, psymval); }
221 // Do a simple 8-bit PC relative relocation with the addend in the
222 // section contents.
223 static inline void
224 pcrel8(unsigned char* view, unsigned char value,
225 typename elfcpp::Elf_types<size>::Elf_Addr address)
226 { This::template pcrel<8>(view, value, address); }
228 static inline void
229 pcrel8(unsigned char* view,
230 const Sized_relobj<size, big_endian>* object,
231 const Symbol_value<size>* psymval,
232 typename elfcpp::Elf_types<size>::Elf_Addr address)
233 { This::template pcrel<8>(view, object, psymval, address); }
235 // Do a simple 16-bit REL relocation with the addend in the section
236 // contents.
237 static inline void
238 rel16(unsigned char* view, elfcpp::Elf_Half value)
239 { This::template rel<16>(view, value); }
241 static inline void
242 rel16(unsigned char* view,
243 const Sized_relobj<size, big_endian>* object,
244 const Symbol_value<size>* psymval)
245 { This::template rel<16>(view, object, psymval); }
247 // Do a simple 32-bit PC relative REL relocation with the addend in
248 // the section contents.
249 static inline void
250 pcrel16(unsigned char* view, elfcpp::Elf_Word value,
251 typename elfcpp::Elf_types<size>::Elf_Addr address)
252 { This::template pcrel<16>(view, value, address); }
254 static inline void
255 pcrel16(unsigned char* view,
256 const Sized_relobj<size, big_endian>* object,
257 const Symbol_value<size>* psymval,
258 typename elfcpp::Elf_types<size>::Elf_Addr address)
259 { This::template pcrel<16>(view, object, psymval, address); }
261 // Do a simple 32-bit REL relocation with the addend in the section
262 // contents.
263 static inline void
264 rel32(unsigned char* view, elfcpp::Elf_Word value)
265 { This::template rel<32>(view, value); }
267 static inline void
268 rel32(unsigned char* view,
269 const Sized_relobj<size, big_endian>* object,
270 const Symbol_value<size>* psymval)
271 { This::template rel<32>(view, object, psymval); }
273 // Do a simple 32-bit PC relative REL relocation with the addend in
274 // the section contents.
275 static inline void
276 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
277 typename elfcpp::Elf_types<size>::Elf_Addr address)
278 { This::template pcrel<32>(view, value, address); }
280 static inline void
281 pcrel32(unsigned char* view,
282 const Sized_relobj<size, big_endian>* object,
283 const Symbol_value<size>* psymval,
284 typename elfcpp::Elf_types<size>::Elf_Addr address)
285 { This::template pcrel<32>(view, object, psymval, address); }
287 // Do a simple 64-bit REL relocation with the addend in the section
288 // contents.
289 static inline void
290 rel64(unsigned char* view, elfcpp::Elf_Xword value)
291 { This::template rel<64>(view, value); }
293 static inline void
294 rel64(unsigned char* view,
295 const Sized_relobj<size, big_endian>* object,
296 const Symbol_value<size>* psymval)
297 { This::template rel<64>(view, object, psymval); }
299 // Do a simple 64-bit PC relative REL relocation with the addend in
300 // the section contents.
301 static inline void
302 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
303 typename elfcpp::Elf_types<size>::Elf_Addr address)
304 { This::template pcrel<64>(view, value, address); }
306 static inline void
307 pcrel64(unsigned char* view,
308 const Sized_relobj<size, big_endian>* object,
309 const Symbol_value<size>* psymval,
310 typename elfcpp::Elf_types<size>::Elf_Addr address)
311 { This::template pcrel<64>(view, object, psymval, address); }
314 // We try to avoid COPY relocations when possible. A COPY relocation
315 // may be required when an executable refers to a variable defined in
316 // a shared library. COPY relocations are problematic because they
317 // tie the executable to the exact size of the variable in the shared
318 // library. We can avoid them if all the references to the variable
319 // are in a writeable section. In that case we can simply use dynamic
320 // relocations. However, when scanning relocs, we don't know when we
321 // see the relocation whether we will be forced to use a COPY
322 // relocation or not. So we have to save the relocation during the
323 // reloc scanning, and then emit it as a dynamic relocation if
324 // necessary. This class implements that. It is used by the target
325 // specific code.
327 template<int size, bool big_endian>
328 class Copy_relocs
330 public:
331 Copy_relocs()
332 : entries_()
335 // Return whether we need a COPY reloc for a reloc against GSYM,
336 // which is being applied to section SHNDX in OBJECT.
337 static bool
338 need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
339 Sized_symbol<size>* gsym);
341 // Save a Rel against SYM for possible emission later. SHNDX is the
342 // index of the section to which the reloc is being applied.
343 void
344 save(Symbol* sym, Relobj*, unsigned int shndx,
345 const elfcpp::Rel<size, big_endian>&);
347 // Save a Rela against SYM for possible emission later.
348 void
349 save(Symbol* sym, Relobj*, unsigned int shndx,
350 const elfcpp::Rela<size, big_endian>&);
352 // Return whether there are any relocs to emit. This also discards
353 // entries which need not be emitted.
354 bool
355 any_to_emit();
357 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
358 // is still defined in the dynamic object).
359 template<int sh_type>
360 void
361 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
363 private:
364 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
365 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
367 // This POD class holds the entries we are saving.
368 class Copy_reloc_entry
370 public:
371 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
372 Relobj* relobj, unsigned int shndx,
373 Address address, Addend addend)
374 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
375 shndx_(shndx), address_(address), addend_(addend)
378 // Return whether we should emit this reloc. If we should not
379 // emit, we clear it.
380 bool
381 should_emit();
383 // Emit this reloc.
385 void
386 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
388 void
389 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
391 private:
392 Symbol* sym_;
393 unsigned int reloc_type_;
394 Relobj* relobj_;
395 unsigned int shndx_;
396 Address address_;
397 Addend addend_;
400 // A list of relocs to be saved.
401 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
403 // The list of relocs we are saving.
404 Copy_reloc_entries entries_;
407 } // End namespace gold.
409 #endif // !defined(GOLD_RELOC_H)