Hash tables, dynamic section, i386 PLT, gold_assert.
[external/binutils.git] / gold / reloc.h
1 // reloc.h -- relocate input files for gold   -*- C++ -*-
2
3 #ifndef GOLD_RELOC_H
4 #define GOLD_RELOC_H
5
6 #include <byteswap.h>
7
8 #include "workqueue.h"
9
10 namespace gold
11 {
12
13 class General_options;
14 class Relobj;
15 class Read_relocs_data;
16 class Stringpool;
17 class Symbol;
18 class Layout;
19
20 // A class to read the relocations for an object file, and then queue
21 // up a task to see if they require any GOT/PLT/COPY relocations in
22 // the symbol table.
23
24 class Read_relocs : public Task
25 {
26  public:
27   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
28   // unblocked when the Scan_relocs task completes.
29   Read_relocs(const General_options& options, Symbol_table* symtab,
30               Layout* layout, Relobj* object, Task_token* symtab_lock,
31               Task_token* blocker)
32     : options_(options), symtab_(symtab), layout_(layout), object_(object),
33       symtab_lock_(symtab_lock), blocker_(blocker)
34   { }
35
36   // The standard Task methods.
37
38   Is_runnable_type
39   is_runnable(Workqueue*);
40
41   Task_locker*
42   locks(Workqueue*);
43
44   void
45   run(Workqueue*);
46
47  private:
48   const General_options& options_;
49   Symbol_table* symtab_;
50   Layout* layout_;
51   Relobj* object_;
52   Task_token* symtab_lock_;
53   Task_token* blocker_;
54 };
55
56 // Scan the relocations for an object to see if they require any
57 // GOT/PLT/COPY relocations.
58
59 class Scan_relocs : public Task
60 {
61  public:
62   // SYMTAB_LOCK is used to lock the symbol table.  BLOCKER should be
63   // unblocked when the task completes.
64   Scan_relocs(const General_options& options, Symbol_table* symtab,
65               Layout* layout, Relobj* object, Read_relocs_data* rd,
66               Task_token* symtab_lock, Task_token* blocker)
67     : options_(options), symtab_(symtab), layout_(layout), object_(object),
68       rd_(rd), symtab_lock_(symtab_lock), blocker_(blocker)
69   { }
70
71   // The standard Task methods.
72
73   Is_runnable_type
74   is_runnable(Workqueue*);
75
76   Task_locker*
77   locks(Workqueue*);
78
79   void
80   run(Workqueue*);
81
82  private:
83   class Scan_relocs_locker;
84
85   const General_options& options_;
86   Symbol_table* symtab_;
87   Layout* layout_;
88   Relobj* object_;
89   Read_relocs_data* rd_;
90   Task_token* symtab_lock_;
91   Task_token* blocker_;
92 };
93
94 // A class to perform all the relocations for an object file.
95
96 class Relocate_task : public Task
97 {
98  public:
99   Relocate_task(const General_options& options, const Symbol_table* symtab,
100                 const Layout* layout, Relobj* object, Output_file* of,
101                 Task_token* final_blocker)
102     : options_(options), symtab_(symtab), layout_(layout), object_(object),
103       of_(of), final_blocker_(final_blocker)
104   { }
105
106   // The standard Task methods.
107
108   Is_runnable_type
109   is_runnable(Workqueue*);
110
111   Task_locker*
112   locks(Workqueue*);
113
114   void
115   run(Workqueue*);
116
117  private:
118   class Relocate_locker;
119
120   const General_options& options_;
121   const Symbol_table* symtab_;
122   const Layout* layout_;
123   Relobj* object_;
124   Output_file* of_;
125   Task_token* final_blocker_;
126 };
127
128 // Standard relocation routines which are used on many targets.  Here
129 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
130
131 template<int size, bool big_endian>
132 class Relocate_functions
133 {
134 private:
135   // Do a simple relocation with the addend in the section contents.
136   // VALSIZE is the size of the value.
137   template<int valsize>
138   static inline void
139   rel(unsigned char* view,
140       typename elfcpp::Swap<valsize, big_endian>::Valtype value)
141   {
142     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
143     Valtype* wv = reinterpret_cast<Valtype*>(view);
144     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
145     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value);
146   }
147
148   // Do a simple PC relative relocation with the addend in the section
149   // contents.  VALSIZE is the size of the value.
150   template<int valsize>
151   static inline void
152   pcrel(unsigned char* view,
153         typename elfcpp::Swap<valsize, big_endian>::Valtype value,
154         typename elfcpp::Elf_types<size>::Elf_Addr address)
155   {
156     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
157     Valtype* wv = reinterpret_cast<Valtype*>(view);
158     Valtype x = elfcpp::Swap<valsize, big_endian>::readval(wv);
159     elfcpp::Swap<valsize, big_endian>::writeval(wv, x + value - address);
160   }
161
162   typedef Relocate_functions<size, big_endian> This;
163
164 public:
165   // Do a simple 8-bit REL relocation with the addend in the object
166   // file data.
167   static inline void
168   rel8(unsigned char* view, unsigned char value)
169   {
170     This::template rel<8>(view, value);
171   }
172
173   // Do a simple 8-bit PC relative relocation with the addend in the
174   // object file data.
175   static inline void
176   pcrel8(unsigned char* view, unsigned char value,
177          typename elfcpp::Elf_types<size>::Elf_Addr address)
178   {
179     This::template pcrel<8>(view, value, address);
180   }
181
182   // Do a simple 16-bit REL relocation with the addend in the object
183   // file data.
184   static inline void
185   rel16(unsigned char* view, elfcpp::Elf_Half value)
186   {
187     This::template rel<16>(view, value);
188   }
189
190   // Do a simple 32-bit PC relative REL relocation with the addend in
191   // the object file data.
192   static inline void
193   pcrel16(unsigned char* view, elfcpp::Elf_Word value,
194           typename elfcpp::Elf_types<size>::Elf_Addr address)
195   {
196     This::template pcrel<16>(view, value, address);
197   }
198
199   // Do a simple 32-bit REL relocation with the addend in the section
200   // contents.
201   static inline void
202   rel32(unsigned char* view, elfcpp::Elf_Word value)
203   {
204     This::template rel<32>(view, value);
205   }
206
207   // Do a simple 32-bit PC relative REL relocation with the addend in
208   // the section contents.
209   static inline void
210   pcrel32(unsigned char* view, elfcpp::Elf_Word value,
211           typename elfcpp::Elf_types<size>::Elf_Addr address)
212   {
213     This::template pcrel<32>(view, value, address);
214   }
215
216   // Do a simple 64-bit REL relocation with the addend in the section
217   // contents.
218   static inline void
219   rel64(unsigned char* view, elfcpp::Elf_Xword value)
220   {
221     This::template rel<64>(view, value);
222   }
223
224   // Do a simple 64-bit PC relative REL relocation with the addend in
225   // the section contents.
226   static inline void
227   pcrel64(unsigned char* view, elfcpp::Elf_Xword value,
228           typename elfcpp::Elf_types<size>::Elf_Addr address)
229   {
230     This::template pcrel<64>(view, value, address);
231   }
232
233   // Return whether we need a COPY reloc for a reloc against GSYM,
234   // which is being applied to section SHNDX in OBJECT.
235   static bool
236   need_copy_reloc(const General_options*, Relobj* object, unsigned int shndx,
237                   Symbol* gsym);
238 };
239
240 } // End namespace gold.
241
242 #endif // !defined(GOLD_RELOC_H)