2006-11-08 Jie Zhang <jzhang918@gmail.com>
[binutils.git] / gold / reloc.h
blob9b0518e5d90d1ee5f3538fc8d087f77fbc93f1a8
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 Relobj;
14 class Read_relocs_data;
15 class Stringpool;
16 class Layout;
18 // A class to read the relocations for an object file, and then queue
19 // up a task to see if they require any GOT/PLT/COPY relocations in
20 // the symbol table.
22 class Read_relocs : public Task
24 public:
25 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
26 // unblocked when the Scan_relocs task completes.
27 Read_relocs(const General_options& options, Symbol_table* symtab,
28 Layout* layout, Relobj* object, Task_token* symtab_lock,
29 Task_token* blocker)
30 : options_(options), symtab_(symtab), layout_(layout), object_(object),
31 symtab_lock_(symtab_lock), blocker_(blocker)
32 { }
34 // The standard Task methods.
36 Is_runnable_type
37 is_runnable(Workqueue*);
39 Task_locker*
40 locks(Workqueue*);
42 void
43 run(Workqueue*);
45 private:
46 const General_options& options_;
47 Symbol_table* symtab_;
48 Layout* layout_;
49 Relobj* object_;
50 Task_token* symtab_lock_;
51 Task_token* blocker_;
54 // Scan the relocations for an object to see if they require any
55 // GOT/PLT/COPY relocations.
57 class Scan_relocs : public Task
59 public:
60 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
61 // unblocked when the task completes.
62 Scan_relocs(const General_options& options, Symbol_table* symtab,
63 Layout* layout, Relobj* object, Read_relocs_data* rd,
64 Task_token* symtab_lock, Task_token* blocker)
65 : options_(options), symtab_(symtab), layout_(layout), object_(object),
66 rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
67 { }
69 // The standard Task methods.
71 Is_runnable_type
72 is_runnable(Workqueue*);
74 Task_locker*
75 locks(Workqueue*);
77 void
78 run(Workqueue*);
80 private:
81 class Scan_relocs_locker;
83 const General_options& options_;
84 Symbol_table* symtab_;
85 Layout* layout_;
86 Relobj* object_;
87 Read_relocs_data* rd_;
88 Task_token* symtab_lock_;
89 Task_token* blocker_;
92 // A class to perform all the relocations for an object file.
94 class Relocate_task : public Task
96 public:
97 Relocate_task(const General_options& options, const Symbol_table* symtab,
98 const Layout* layout, Relobj* object, Output_file* of,
99 Task_token* final_blocker)
100 : options_(options), symtab_(symtab), layout_(layout), object_(object),
101 of_(of), final_blocker_(final_blocker)
104 // The standard Task methods.
106 Is_runnable_type
107 is_runnable(Workqueue*);
109 Task_locker*
110 locks(Workqueue*);
112 void
113 run(Workqueue*);
115 private:
116 class Relocate_locker;
118 const General_options& options_;
119 const Symbol_table* symtab_;
120 const Layout* layout_;
121 Relobj* object_;
122 Output_file* of_;
123 Task_token* final_blocker_;
126 // Standard relocation routines which are used on many targets. Here
127 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
129 template<int size, bool big_endian>
130 class Relocate_functions
132 private:
133 // Do a simple relocation with the addend in the section contents.
134 // VALSIZE is the size of the value.
135 template<int valsize>
136 static inline void
137 rel(unsigned char* view,
138 typename elfcpp::Swap<valsize, big_endian>::Valtype value)
140 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
141 Valtype* wv = reinterpret_cast<Valtype*>(view);
142 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
143 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
146 // Do a simple PC relative relocation with the addend in the section
147 // contents. VALSIZE is the size of the value.
148 template<int valsize>
149 static inline void
150 pcrel(unsigned char* view,
151 typename elfcpp::Swap<valsize, big_endian>::Valtype value,
152 typename elfcpp::Elf_types<size>::Elf_Addr address)
154 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
155 Valtype* wv = reinterpret_cast<Valtype*>(view);
156 Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
157 elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
160 typedef Relocate_functions<size, big_endian> This;
162 public:
163 // Do a simple 8-bit REL relocation with the addend in the object
164 // file data.
165 static inline void
166 rel8(unsigned char* view, unsigned char value)
168 This::template rel<8>(view, value);
171 // Do a simple 8-bit PC relative relocation with the addend in the
172 // object file data.
173 static inline void
174 pcrel8(unsigned char* view, unsigned char value,
175 typename elfcpp::Elf_types<size>::Elf_Addr address)
177 This::template pcrel<8>(view, value, address);
180 // Do a simple 16-bit REL relocation with the addend in the object
181 // file data.
182 static inline void
183 rel16(unsigned char* view, elfcpp::Elf_Half value)
185 This::template rel<16>(view, value);
188 // Do a simple 32-bit PC relative REL relocation with the addend in
189 // the object file data.
190 static inline void
191 pcrel16(unsigned char* view, elfcpp::Elf_Word value,
192 typename elfcpp::Elf_types<size>::Elf_Addr address)
194 This::template pcrel<16>(view, value, address);
197 // Do a simple 32-bit REL relocation with the addend in the section
198 // contents.
199 static inline void
200 rel32(unsigned char* view, elfcpp::Elf_Word value)
202 This::template rel<32>(view, value);
205 // Do a simple 32-bit PC relative REL relocation with the addend in
206 // the section contents.
207 static inline void
208 pcrel32(unsigned char* view, elfcpp::Elf_Word value,
209 typename elfcpp::Elf_types<size>::Elf_Addr address)
211 This::template pcrel<32>(view, value, address);
214 // Do a simple 64-bit REL relocation with the addend in the section
215 // contents.
216 static inline void
217 rel64(unsigned char* view, elfcpp::Elf_Xword value)
219 This::template rel<64>(view, value);
222 // Do a simple 64-bit PC relative REL relocation with the addend in
223 // the section contents.
224 static inline void
225 pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
226 typename elfcpp::Elf_types<size>::Elf_Addr address)
228 This::template pcrel<64>(view, value, address);
232 } // End namespace gold.
234 #endif // !defined(GOLD_RELOC_H)