1 // reloc.h -- relocate input files for gold -*- C++ -*-
13 class General_options;
15 class Read_relocs_data;
22 template<int size, bool big_endian>
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
35 class Read_relocs : public Task
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,
43 : options_(options), symtab_(symtab), layout_(layout), object_(object),
44 symtab_lock_(symtab_lock), blocker_(blocker)
47 // The standard Task methods.
50 is_runnable(Workqueue*);
59 const General_options& options_;
60 Symbol_table* symtab_;
63 Task_token* symtab_lock_;
67 // Scan the relocations for an object to see if they require any
68 // GOT/PLT/COPY relocations.
70 class Scan_relocs : public Task
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)
82 // The standard Task methods.
85 is_runnable(Workqueue*);
94 class Scan_relocs_locker;
96 const General_options& options_;
97 Symbol_table* symtab_;
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
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.
120 is_runnable(Workqueue*);
129 class Relocate_locker;
131 const General_options& options_;
132 const Symbol_table* symtab_;
133 const Layout* layout_;
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
146 // Do a simple relocation with the addend in the section contents.
147 // VALSIZE is the size of the value.
148 template<int valsize>
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
162 template<int valsize>
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>
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
192 template<int valsize>
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;
209 // Do a simple 8-bit REL relocation with the addend in the section
212 rel8(unsigned char* view, unsigned char value)
213 { This::template rel<8>(view, value); }
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
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); }
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
238 rel16(unsigned char* view, elfcpp::Elf_Half value)
239 { This::template rel<16>(view, value); }
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.
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); }
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
264 rel32(unsigned char* view, elfcpp::Elf_Word value)
265 { This::template rel<32>(view, value); }
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.
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); }
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
290 rel64(unsigned char* view, elfcpp::Elf_Xword value)
291 { This::template rel<64>(view, value); }
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.
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); }
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
327 template<int size, bool big_endian>
335 // Return whether we need a COPY reloc for a reloc against GSYM,
336 // which is being applied to section SHNDX in OBJECT.
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.
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.
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.
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>
361 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
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
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.
386 emit(Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>*);
389 emit(Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>*);
393 unsigned int reloc_type_;
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)