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 // Do a simple PC relative relocation with the addend in the section
196 // contents. VALSIZE is the size of the value.
197 template<int valsize
>
199 pcrel(unsigned char* view
,
200 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
201 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
203 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
204 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
205 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
206 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
209 // Do a simple PC relative relocation with a Symbol_value with the
210 // addend in the section contents. VALSIZE is the size of the
212 template<int valsize
>
214 pcrel(unsigned char* view
,
215 const Sized_relobj
<size
, big_endian
>* object
,
216 const Symbol_value
<size
>* psymval
,
217 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
219 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
220 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
221 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
222 x
= psymval
->value(object
, x
);
223 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
- address
);
226 typedef Relocate_functions
<size
, big_endian
> This
;
229 // Do a simple 8-bit REL relocation with the addend in the section
232 rel8(unsigned char* view
, unsigned char value
)
233 { This::template rel
<8>(view
, value
); }
236 rel8(unsigned char* view
,
237 const Sized_relobj
<size
, big_endian
>* object
,
238 const Symbol_value
<size
>* psymval
)
239 { This::template rel
<8>(view
, object
, psymval
); }
241 // Do a simple 8-bit PC relative relocation with the addend in the
244 pcrel8(unsigned char* view
, unsigned char value
,
245 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
246 { This::template pcrel
<8>(view
, value
, address
); }
249 pcrel8(unsigned char* view
,
250 const Sized_relobj
<size
, big_endian
>* object
,
251 const Symbol_value
<size
>* psymval
,
252 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
253 { This::template pcrel
<8>(view
, object
, psymval
, address
); }
255 // Do a simple 16-bit REL relocation with the addend in the section
258 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
259 { This::template rel
<16>(view
, value
); }
262 rel16(unsigned char* view
,
263 const Sized_relobj
<size
, big_endian
>* object
,
264 const Symbol_value
<size
>* psymval
)
265 { This::template rel
<16>(view
, object
, psymval
); }
267 // Do a simple 32-bit PC relative REL relocation with the addend in
268 // the section contents.
270 pcrel16(unsigned char* view
, elfcpp::Elf_Word value
,
271 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
272 { This::template pcrel
<16>(view
, value
, address
); }
275 pcrel16(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
<16>(view
, object
, psymval
, address
); }
281 // Do a simple 32-bit REL relocation with the addend in the section
284 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
285 { This::template rel
<32>(view
, value
); }
288 rel32(unsigned char* view
,
289 const Sized_relobj
<size
, big_endian
>* object
,
290 const Symbol_value
<size
>* psymval
)
291 { This::template rel
<32>(view
, object
, psymval
); }
293 // Do a simple 32-bit PC relative REL relocation with the addend in
294 // the section contents.
296 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
297 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
298 { This::template pcrel
<32>(view
, value
, address
); }
301 pcrel32(unsigned char* view
,
302 const Sized_relobj
<size
, big_endian
>* object
,
303 const Symbol_value
<size
>* psymval
,
304 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
305 { This::template pcrel
<32>(view
, object
, psymval
, address
); }
307 // Do a simple 64-bit REL relocation with the addend in the section
310 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
311 { This::template rel
<64>(view
, value
); }
314 rel64(unsigned char* view
,
315 const Sized_relobj
<size
, big_endian
>* object
,
316 const Symbol_value
<size
>* psymval
)
317 { This::template rel
<64>(view
, object
, psymval
); }
319 // Do a simple 64-bit PC relative REL relocation with the addend in
320 // the section contents.
322 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
323 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
324 { This::template pcrel
<64>(view
, value
, address
); }
327 pcrel64(unsigned char* view
,
328 const Sized_relobj
<size
, big_endian
>* object
,
329 const Symbol_value
<size
>* psymval
,
330 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
331 { This::template pcrel
<64>(view
, object
, psymval
, address
); }
334 // We try to avoid COPY relocations when possible. A COPY relocation
335 // may be required when an executable refers to a variable defined in
336 // a shared library. COPY relocations are problematic because they
337 // tie the executable to the exact size of the variable in the shared
338 // library. We can avoid them if all the references to the variable
339 // are in a writeable section. In that case we can simply use dynamic
340 // relocations. However, when scanning relocs, we don't know when we
341 // see the relocation whether we will be forced to use a COPY
342 // relocation or not. So we have to save the relocation during the
343 // reloc scanning, and then emit it as a dynamic relocation if
344 // necessary. This class implements that. It is used by the target
347 template<int size
, bool big_endian
>
355 // Return whether we need a COPY reloc for a reloc against GSYM,
356 // which is being applied to section SHNDX in OBJECT.
358 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
359 Sized_symbol
<size
>* gsym
);
361 // Save a Rel against SYM for possible emission later. SHNDX is the
362 // index of the section to which the reloc is being applied.
364 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
365 const elfcpp::Rel
<size
, big_endian
>&);
367 // Save a Rela against SYM for possible emission later.
369 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
370 const elfcpp::Rela
<size
, big_endian
>&);
372 // Return whether there are any relocs to emit. This also discards
373 // entries which need not be emitted.
377 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
378 // is still defined in the dynamic object).
379 template<int sh_type
>
381 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
384 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
385 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
387 // This POD class holds the entries we are saving.
388 class Copy_reloc_entry
391 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
392 Relobj
* relobj
, unsigned int shndx
,
393 Address address
, Addend addend
)
394 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
395 shndx_(shndx
), address_(address
), addend_(addend
)
398 // Return whether we should emit this reloc. If we should not
399 // emit, we clear it.
406 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
409 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
413 unsigned int reloc_type_
;
420 // A list of relocs to be saved.
421 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
423 // The list of relocs we are saving.
424 Copy_reloc_entries entries_
;
427 } // End namespace gold.
429 #endif // !defined(GOLD_RELOC_H)