1 // reloc.h -- relocate input files for gold -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "workqueue.h"
33 class General_options
;
35 class Read_relocs_data
;
42 template<int size
, bool big_endian
>
48 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
49 class Output_data_reloc
;
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
55 class Read_relocs
: public Task
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
,
61 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
63 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
64 symtab_lock_(symtab_lock
), blocker_(blocker
)
67 // The standard Task methods.
70 is_runnable(Workqueue
*);
79 const General_options
& options_
;
80 Symbol_table
* symtab_
;
83 Task_token
* symtab_lock_
;
87 // Scan the relocations for an object to see if they require any
88 // GOT/PLT/COPY relocations.
90 class Scan_relocs
: public Task
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
,
96 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
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
)
102 // The standard Task methods.
105 is_runnable(Workqueue
*);
114 class Scan_relocs_locker
;
116 const General_options
& options_
;
117 Symbol_table
* symtab_
;
120 Read_relocs_data
* rd_
;
121 Task_token
* symtab_lock_
;
122 Task_token
* blocker_
;
125 // A class to perform all the relocations for an object file.
127 class Relocate_task
: public Task
130 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
131 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
132 Task_token
* final_blocker
)
133 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
134 of_(of
), final_blocker_(final_blocker
)
137 // The standard Task methods.
140 is_runnable(Workqueue
*);
149 class Relocate_locker
;
151 const General_options
& options_
;
152 const Symbol_table
* symtab_
;
153 const Layout
* layout_
;
156 Task_token
* final_blocker_
;
159 // Standard relocation routines which are used on many targets. Here
160 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
162 template<int size
, bool big_endian
>
163 class Relocate_functions
166 // Do a simple relocation with the addend in the section contents.
167 // VALSIZE is the size of the value.
168 template<int valsize
>
170 rel(unsigned char* view
,
171 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
173 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
174 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
175 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
176 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
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
182 template<int valsize
>
184 rel(unsigned char* view
,
185 const Sized_relobj
<size
, big_endian
>* object
,
186 const Symbol_value
<size
>* psymval
)
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
);
195 // Like rel(), but sign-extends the value to SIZE.
196 template<int valsize
>
198 signedrel(unsigned char* view
,
199 const Sized_relobj
<size
, big_endian
>* object
,
200 const Symbol_value
<size
>* psymval
)
202 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
203 typedef typename
elfcpp::Swap
<size
, big_endian
>::Valtype Sizetype
;
204 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
205 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
206 // Fancy formula to sign-extend x to size.
207 const Sizetype mask
= 1U << (sizeof(valsize
) * 8 - 1);
208 Sizetype sign_extended_x
= x
;
209 sign_extended_x
= (sign_extended_x
^ mask
) - mask
;
210 x
= psymval
->value(object
, sign_extended_x
);
211 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
);
214 // Do a simple PC relative relocation with the addend in the section
215 // contents. VALSIZE is the size of the value.
216 template<int valsize
>
218 pcrel(unsigned char* view
,
219 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
220 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
222 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
223 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
224 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
225 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
228 // Do a simple PC relative relocation with a Symbol_value with the
229 // addend in the section contents. VALSIZE is the size of the
231 template<int valsize
>
233 pcrel(unsigned char* view
,
234 const Sized_relobj
<size
, big_endian
>* object
,
235 const Symbol_value
<size
>* psymval
,
236 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
238 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
239 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
240 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
241 x
= psymval
->value(object
, x
);
242 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
245 typedef Relocate_functions
<size
, big_endian
> This
;
248 // Do a simple 8-bit REL relocation with the addend in the section
251 rel8(unsigned char* view
, unsigned char value
)
252 { This::template rel
<8>(view
, value
); }
255 rel8(unsigned char* view
,
256 const Sized_relobj
<size
, big_endian
>* object
,
257 const Symbol_value
<size
>* psymval
)
258 { This::template rel
<8>(view
, object
, psymval
); }
260 // Do an 8-bit REL relocation, sign extending the addend to SIZE.
262 rel8s(unsigned char* view
,
263 const Sized_relobj
<size
, big_endian
>* object
,
264 const Symbol_value
<size
>* psymval
)
265 { This::template signedrel
<8>(view
, object
, psymval
); }
267 // Do a simple 8-bit PC relative relocation with the addend in the
270 pcrel8(unsigned char* view
, unsigned char value
,
271 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
272 { This::template pcrel
<8>(view
, value
, address
); }
275 pcrel8(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
<8>(view
, object
, psymval
, address
); }
281 // Do a simple 16-bit REL relocation with the addend in the section
284 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
285 { This::template rel
<16>(view
, value
); }
288 rel16(unsigned char* view
,
289 const Sized_relobj
<size
, big_endian
>* object
,
290 const Symbol_value
<size
>* psymval
)
291 { This::template rel
<16>(view
, object
, psymval
); }
293 // Do a 16-bit REL relocation, sign extending the addend to SIZE.
295 rel16s(unsigned char* view
,
296 const Sized_relobj
<size
, big_endian
>* object
,
297 const Symbol_value
<size
>* psymval
)
298 { This::template signedrel
<16>(view
, object
, psymval
); }
300 // Do a simple 32-bit PC relative REL relocation with the addend in
301 // the section contents.
303 pcrel16(unsigned char* view
, elfcpp::Elf_Word value
,
304 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
305 { This::template pcrel
<16>(view
, value
, address
); }
308 pcrel16(unsigned char* view
,
309 const Sized_relobj
<size
, big_endian
>* object
,
310 const Symbol_value
<size
>* psymval
,
311 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
312 { This::template pcrel
<16>(view
, object
, psymval
, address
); }
314 // Do a simple 32-bit REL relocation with the addend in the section
317 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
318 { This::template rel
<32>(view
, value
); }
321 rel32(unsigned char* view
,
322 const Sized_relobj
<size
, big_endian
>* object
,
323 const Symbol_value
<size
>* psymval
)
324 { This::template rel
<32>(view
, object
, psymval
); }
326 // Do a 32-bit REL relocation, sign extending the addend to SIZE.
328 rel32s(unsigned char* view
,
329 const Sized_relobj
<size
, big_endian
>* object
,
330 const Symbol_value
<size
>* psymval
)
331 { This::template signedrel
<32>(view
, object
, psymval
); }
333 // Do a simple 32-bit PC relative REL relocation with the addend in
334 // the section contents.
336 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
337 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
338 { This::template pcrel
<32>(view
, value
, address
); }
341 pcrel32(unsigned char* view
,
342 const Sized_relobj
<size
, big_endian
>* object
,
343 const Symbol_value
<size
>* psymval
,
344 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
345 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
347 // Do a simple 64-bit REL relocation with the addend in the section
350 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
351 { This::template rel
<64>(view
, value
); }
354 rel64(unsigned char* view
,
355 const Sized_relobj
<size
, big_endian
>* object
,
356 const Symbol_value
<size
>* psymval
)
357 { This::template rel
<64>(view
, object
, psymval
); }
359 // Do a simple 64-bit PC relative REL relocation with the addend in
360 // the section contents.
362 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
363 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
364 { This::template pcrel
<64>(view
, value
, address
); }
367 pcrel64(unsigned char* view
,
368 const Sized_relobj
<size
, big_endian
>* object
,
369 const Symbol_value
<size
>* psymval
,
370 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
371 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
374 // We try to avoid COPY relocations when possible. A COPY relocation
375 // may be required when an executable refers to a variable defined in
376 // a shared library. COPY relocations are problematic because they
377 // tie the executable to the exact size of the variable in the shared
378 // library. We can avoid them if all the references to the variable
379 // are in a writeable section. In that case we can simply use dynamic
380 // relocations. However, when scanning relocs, we don't know when we
381 // see the relocation whether we will be forced to use a COPY
382 // relocation or not. So we have to save the relocation during the
383 // reloc scanning, and then emit it as a dynamic relocation if
384 // necessary. This class implements that. It is used by the target
387 template<int size
, bool big_endian
>
395 // Return whether we need a COPY reloc for a reloc against GSYM,
396 // which is being applied to section SHNDX in OBJECT.
398 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
399 Sized_symbol
<size
>* gsym
);
401 // Save a Rel against SYM for possible emission later. SHNDX is the
402 // index of the section to which the reloc is being applied.
404 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
405 const elfcpp::Rel
<size
, big_endian
>&);
407 // Save a Rela against SYM for possible emission later.
409 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
410 const elfcpp::Rela
<size
, big_endian
>&);
412 // Return whether there are any relocs to emit. This also discards
413 // entries which need not be emitted.
417 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
418 // is still defined in the dynamic object).
419 template<int sh_type
>
421 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
424 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
425 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
427 // This POD class holds the entries we are saving.
428 class Copy_reloc_entry
431 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
432 Relobj
* relobj
, unsigned int shndx
,
433 Address address
, Addend addend
)
434 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
435 shndx_(shndx
), address_(address
), addend_(addend
)
438 // Return whether we should emit this reloc. If we should not
439 // emit, we clear it.
446 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
449 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
453 unsigned int reloc_type_
;
460 // A list of relocs to be saved.
461 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
463 // The list of relocs we are saving.
464 Copy_reloc_entries entries_
;
467 } // End namespace gold.
469 #endif // !defined(GOLD_RELOC_H)