1 // reloc.h -- relocate input files for gold -*- C++ -*-
13 class General_options
;
15 class Read_relocs_data
;
23 template<int sh_type
, bool dynamic
, int size
, bool big_endian
>
24 class Output_data_reloc
;
26 // A class to read the relocations for an object file, and then queue
27 // up a task to see if they require any GOT/PLT/COPY relocations in
30 class Read_relocs
: public Task
33 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
34 // unblocked when the Scan_relocs task completes.
35 Read_relocs(const General_options
& options
, Symbol_table
* symtab
,
36 Layout
* layout
, Relobj
* object
, Task_token
* symtab_lock
,
38 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
39 symtab_lock_(symtab_lock
), blocker_(blocker
)
42 // The standard Task methods.
45 is_runnable(Workqueue
*);
54 const General_options
& options_
;
55 Symbol_table
* symtab_
;
58 Task_token
* symtab_lock_
;
62 // Scan the relocations for an object to see if they require any
63 // GOT/PLT/COPY relocations.
65 class Scan_relocs
: public Task
68 // SYMTAB_LOCK is used to lock the symbol table. BLOCKER should be
69 // unblocked when the task completes.
70 Scan_relocs(const General_options
& options
, Symbol_table
* symtab
,
71 Layout
* layout
, Relobj
* object
, Read_relocs_data
* rd
,
72 Task_token
* symtab_lock
, Task_token
* blocker
)
73 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
74 rd_(rd
), symtab_lock_(symtab_lock
), blocker_(blocker
)
77 // The standard Task methods.
80 is_runnable(Workqueue
*);
89 class Scan_relocs_locker
;
91 const General_options
& options_
;
92 Symbol_table
* symtab_
;
95 Read_relocs_data
* rd_
;
96 Task_token
* symtab_lock_
;
100 // A class to perform all the relocations for an object file.
102 class Relocate_task
: public Task
105 Relocate_task(const General_options
& options
, const Symbol_table
* symtab
,
106 const Layout
* layout
, Relobj
* object
, Output_file
* of
,
107 Task_token
* final_blocker
)
108 : options_(options
), symtab_(symtab
), layout_(layout
), object_(object
),
109 of_(of
), final_blocker_(final_blocker
)
112 // The standard Task methods.
115 is_runnable(Workqueue
*);
124 class Relocate_locker
;
126 const General_options
& options_
;
127 const Symbol_table
* symtab_
;
128 const Layout
* layout_
;
131 Task_token
* final_blocker_
;
134 // Standard relocation routines which are used on many targets. Here
135 // SIZE and BIG_ENDIAN refer to the target, not the relocation type.
137 template<int size
, bool big_endian
>
138 class Relocate_functions
141 // Do a simple relocation with the addend in the section contents.
142 // VALSIZE is the size of the value.
143 template<int valsize
>
145 rel(unsigned char* view
,
146 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
)
148 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
149 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
150 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
151 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
);
154 // Do a simple PC relative relocation with the addend in the section
155 // contents. VALSIZE is the size of the value.
156 template<int valsize
>
158 pcrel(unsigned char* view
,
159 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype value
,
160 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
162 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
163 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
164 Valtype x
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
165 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, x
+ value
- address
);
168 typedef Relocate_functions
<size
, big_endian
> This
;
171 // Do a simple 8-bit REL relocation with the addend in the object
174 rel8(unsigned char* view
, unsigned char value
)
176 This::template rel
<8>(view
, value
);
179 // Do a simple 8-bit PC relative relocation with the addend in the
182 pcrel8(unsigned char* view
, unsigned char value
,
183 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
185 This::template pcrel
<8>(view
, value
, address
);
188 // Do a simple 16-bit REL relocation with the addend in the object
191 rel16(unsigned char* view
, elfcpp::Elf_Half value
)
193 This::template rel
<16>(view
, value
);
196 // Do a simple 32-bit PC relative REL relocation with the addend in
197 // the object file data.
199 pcrel16(unsigned char* view
, elfcpp::Elf_Word value
,
200 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
202 This::template pcrel
<16>(view
, value
, address
);
205 // Do a simple 32-bit REL relocation with the addend in the section
208 rel32(unsigned char* view
, elfcpp::Elf_Word value
)
210 This::template rel
<32>(view
, value
);
213 // Do a simple 32-bit PC relative REL relocation with the addend in
214 // the section contents.
216 pcrel32(unsigned char* view
, elfcpp::Elf_Word value
,
217 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
219 This::template pcrel
<32>(view
, value
, address
);
222 // Do a simple 64-bit REL relocation with the addend in the section
225 rel64(unsigned char* view
, elfcpp::Elf_Xword value
)
227 This::template rel
<64>(view
, value
);
230 // Do a simple 64-bit PC relative REL relocation with the addend in
231 // the section contents.
233 pcrel64(unsigned char* view
, elfcpp::Elf_Xword value
,
234 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
236 This::template pcrel
<64>(view
, value
, address
);
240 // We try to avoid COPY relocations when possible. A COPY relocation
241 // may be required when an executable refers to a variable defined in
242 // a shared library. COPY relocations are problematic because they
243 // tie the executable to the exact size of the variable in the shared
244 // library. We can avoid them if all the references to the variable
245 // are in a writeable section. In that case we can simply use dynamic
246 // relocations. However, when scanning relocs, we don't know when we
247 // see the relocation whether we will be forced to use a COPY
248 // relocation or not. So we have to save the relocation during the
249 // reloc scanning, and then emit it as a dynamic relocation if
250 // necessary. This class implements that. It is used by the target
253 template<int size
, bool big_endian
>
261 // Return whether we need a COPY reloc for a reloc against GSYM,
262 // which is being applied to section SHNDX in OBJECT.
264 need_copy_reloc(const General_options
*, Relobj
* object
, unsigned int shndx
,
265 Sized_symbol
<size
>* gsym
);
267 // Save a Rel against SYM for possible emission later. SHNDX is the
268 // index of the section to which the reloc is being applied.
270 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
271 const elfcpp::Rel
<size
, big_endian
>&);
273 // Save a Rela against SYM for possible emission later.
275 save(Symbol
* sym
, Relobj
*, unsigned int shndx
,
276 const elfcpp::Rela
<size
, big_endian
>&);
278 // Return whether there are any relocs to emit. This also discards
279 // entries which need not be emitted.
283 // Emit relocs for each symbol which did not get a COPY reloc (i.e.,
284 // is still defined in the dynamic object).
285 template<int sh_type
>
287 emit(Output_data_reloc
<sh_type
, true, size
, big_endian
>*);
290 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
291 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Addend
;
293 // This POD class holds the entries we are saving.
294 class Copy_reloc_entry
297 Copy_reloc_entry(Symbol
* sym
, unsigned int reloc_type
,
298 Relobj
* relobj
, unsigned int shndx
,
299 Address address
, Addend addend
)
300 : sym_(sym
), reloc_type_(reloc_type
), relobj_(relobj
),
301 shndx_(shndx
), address_(address
), addend_(addend
)
304 // Return whether we should emit this reloc. If we should not
305 // emit, we clear it.
312 emit(Output_data_reloc
<elfcpp::SHT_REL
, true, size
, big_endian
>*);
315 emit(Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
>*);
319 unsigned int reloc_type_
;
326 // A list of relocs to be saved.
327 typedef std::vector
<Copy_reloc_entry
> Copy_reloc_entries
;
329 // The list of relocs we are saving.
330 Copy_reloc_entries entries_
;
333 } // End namespace gold.
335 #endif // !defined(GOLD_RELOC_H)