Commit | Line | Data |
---|---|---|
42cacb20 DE |
1 | // powerpc.cc -- powerpc target support for gold. |
2 | ||
3 | // Copyright 2008 Free Software Foundation, Inc. | |
4 | // Written by David S. Miller <davem@davemloft.net> | |
5 | // and David Edelsohn <edelsohn@gnu.org> | |
6 | ||
7 | // This file is part of gold. | |
8 | ||
9 | // This program is free software; you can redistribute it and/or modify | |
10 | // it under the terms of the GNU General Public License as published by | |
11 | // the Free Software Foundation; either version 3 of the License, or | |
12 | // (at your option) any later version. | |
13 | ||
14 | // This program is distributed in the hope that it will be useful, | |
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | // GNU General Public License for more details. | |
18 | ||
19 | // You should have received a copy of the GNU General Public License | |
20 | // along with this program; if not, write to the Free Software | |
21 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
22 | // MA 02110-1301, USA. | |
23 | ||
24 | #include "gold.h" | |
25 | ||
26 | #include "elfcpp.h" | |
27 | #include "parameters.h" | |
28 | #include "reloc.h" | |
29 | #include "powerpc.h" | |
30 | #include "object.h" | |
31 | #include "symtab.h" | |
32 | #include "layout.h" | |
33 | #include "output.h" | |
34 | #include "copy-relocs.h" | |
35 | #include "target.h" | |
36 | #include "target-reloc.h" | |
37 | #include "target-select.h" | |
38 | #include "tls.h" | |
39 | #include "errors.h" | |
40 | ||
41 | namespace | |
42 | { | |
43 | ||
44 | using namespace gold; | |
45 | ||
46 | template<int size, bool big_endian> | |
47 | class Output_data_plt_powerpc; | |
48 | ||
49 | template<int size, bool big_endian> | |
50 | class Target_powerpc : public Sized_target<size, big_endian> | |
51 | { | |
52 | public: | |
53 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section; | |
54 | ||
55 | Target_powerpc() | |
56 | : Sized_target<size, big_endian>(&powerpc_info), | |
57 | got_(NULL), got2_(NULL), toc_(NULL), | |
58 | plt_(NULL), rela_dyn_(NULL), | |
59 | copy_relocs_(elfcpp::R_POWERPC_COPY), | |
60 | dynbss_(NULL), got_mod_index_offset_(-1U) | |
61 | { | |
62 | } | |
63 | ||
64 | // Scan the relocations to look for symbol adjustments. | |
65 | void | |
66 | scan_relocs(const General_options& options, | |
67 | Symbol_table* symtab, | |
68 | Layout* layout, | |
69 | Sized_relobj<size, big_endian>* object, | |
70 | unsigned int data_shndx, | |
71 | unsigned int sh_type, | |
72 | const unsigned char* prelocs, | |
73 | size_t reloc_count, | |
74 | Output_section* output_section, | |
75 | bool needs_special_offset_handling, | |
76 | size_t local_symbol_count, | |
77 | const unsigned char* plocal_symbols); | |
78 | // Finalize the sections. | |
79 | void | |
80 | do_finalize_sections(Layout*); | |
81 | ||
82 | // Return the value to use for a dynamic which requires special | |
83 | // treatment. | |
84 | uint64_t | |
85 | do_dynsym_value(const Symbol*) const; | |
86 | ||
87 | // Relocate a section. | |
88 | void | |
89 | relocate_section(const Relocate_info<size, big_endian>*, | |
90 | unsigned int sh_type, | |
91 | const unsigned char* prelocs, | |
92 | size_t reloc_count, | |
93 | Output_section* output_section, | |
94 | bool needs_special_offset_handling, | |
95 | unsigned char* view, | |
96 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
97 | section_size_type view_size); | |
98 | ||
99 | // Scan the relocs during a relocatable link. | |
100 | void | |
101 | scan_relocatable_relocs(const General_options& options, | |
102 | Symbol_table* symtab, | |
103 | Layout* layout, | |
104 | Sized_relobj<size, big_endian>* object, | |
105 | unsigned int data_shndx, | |
106 | unsigned int sh_type, | |
107 | const unsigned char* prelocs, | |
108 | size_t reloc_count, | |
109 | Output_section* output_section, | |
110 | bool needs_special_offset_handling, | |
111 | size_t local_symbol_count, | |
112 | const unsigned char* plocal_symbols, | |
113 | Relocatable_relocs*); | |
114 | ||
115 | // Relocate a section during a relocatable link. | |
116 | void | |
117 | relocate_for_relocatable(const Relocate_info<size, big_endian>*, | |
118 | unsigned int sh_type, | |
119 | const unsigned char* prelocs, | |
120 | size_t reloc_count, | |
121 | Output_section* output_section, | |
122 | off_t offset_in_output_section, | |
123 | const Relocatable_relocs*, | |
124 | unsigned char* view, | |
125 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
126 | section_size_type view_size, | |
127 | unsigned char* reloc_view, | |
128 | section_size_type reloc_view_size); | |
129 | ||
130 | // Return whether SYM is defined by the ABI. | |
131 | bool | |
132 | do_is_defined_by_abi(Symbol* sym) const | |
133 | { | |
134 | return strcmp(sym->name(), "___tls_get_addr") == 0; | |
135 | } | |
136 | ||
137 | // Return the size of the GOT section. | |
138 | section_size_type | |
139 | got_size() | |
140 | { | |
141 | gold_assert(this->got_ != NULL); | |
142 | return this->got_->data_size(); | |
143 | } | |
144 | ||
145 | private: | |
146 | ||
147 | // The class which scans relocations. | |
148 | class Scan | |
149 | { | |
150 | public: | |
151 | Scan() | |
152 | : issued_non_pic_error_(false) | |
153 | { } | |
154 | ||
155 | inline void | |
156 | local(const General_options& options, Symbol_table* symtab, | |
157 | Layout* layout, Target_powerpc* target, | |
158 | Sized_relobj<size, big_endian>* object, | |
159 | unsigned int data_shndx, | |
160 | Output_section* output_section, | |
161 | const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, | |
162 | const elfcpp::Sym<size, big_endian>& lsym); | |
163 | ||
164 | inline void | |
165 | global(const General_options& options, Symbol_table* symtab, | |
166 | Layout* layout, Target_powerpc* target, | |
167 | Sized_relobj<size, big_endian>* object, | |
168 | unsigned int data_shndx, | |
169 | Output_section* output_section, | |
170 | const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type, | |
171 | Symbol* gsym); | |
172 | ||
173 | private: | |
174 | static void | |
175 | unsupported_reloc_local(Sized_relobj<size, big_endian>*, | |
176 | unsigned int r_type); | |
177 | ||
178 | static void | |
179 | unsupported_reloc_global(Sized_relobj<size, big_endian>*, | |
180 | unsigned int r_type, Symbol*); | |
181 | ||
182 | static void | |
183 | generate_tls_call(Symbol_table* symtab, Layout* layout, | |
184 | Target_powerpc* target); | |
185 | ||
186 | void | |
187 | check_non_pic(Relobj*, unsigned int r_type); | |
188 | ||
189 | // Whether we have issued an error about a non-PIC compilation. | |
190 | bool issued_non_pic_error_; | |
191 | }; | |
192 | ||
193 | // The class which implements relocation. | |
194 | class Relocate | |
195 | { | |
196 | public: | |
197 | // Do a relocation. Return false if the caller should not issue | |
198 | // any warnings about this relocation. | |
199 | inline bool | |
200 | relocate(const Relocate_info<size, big_endian>*, Target_powerpc*, | |
201 | size_t relnum, const elfcpp::Rela<size, big_endian>&, | |
202 | unsigned int r_type, const Sized_symbol<size>*, | |
203 | const Symbol_value<size>*, | |
204 | unsigned char*, | |
205 | typename elfcpp::Elf_types<size>::Elf_Addr, | |
206 | section_size_type); | |
207 | ||
208 | private: | |
209 | // Do a TLS relocation. | |
210 | inline void | |
211 | relocate_tls(const Relocate_info<size, big_endian>*, | |
212 | Target_powerpc* target, | |
213 | size_t relnum, const elfcpp::Rela<size, big_endian>&, | |
214 | unsigned int r_type, const Sized_symbol<size>*, | |
215 | const Symbol_value<size>*, | |
216 | unsigned char*, | |
217 | typename elfcpp::Elf_types<size>::Elf_Addr, | |
218 | section_size_type); | |
219 | }; | |
220 | ||
221 | // A class which returns the size required for a relocation type, | |
222 | // used while scanning relocs during a relocatable link. | |
223 | class Relocatable_size_for_reloc | |
224 | { | |
225 | public: | |
226 | unsigned int | |
227 | get_size_for_reloc(unsigned int, Relobj*); | |
228 | }; | |
229 | ||
230 | // Get the GOT section, creating it if necessary. | |
231 | Output_data_got<size, big_endian>* | |
232 | got_section(Symbol_table*, Layout*); | |
233 | ||
234 | Output_data_space* | |
235 | got2_section() const | |
236 | { | |
237 | gold_assert (this->got2_ != NULL); | |
238 | return this->got2_; | |
239 | } | |
240 | ||
241 | // Get the TOC section. | |
242 | Output_data_space* | |
243 | toc_section() const | |
244 | { | |
245 | gold_assert (this->toc_ != NULL); | |
246 | return this->toc_; | |
247 | } | |
248 | ||
249 | // Create a PLT entry for a global symbol. | |
250 | void | |
251 | make_plt_entry(Symbol_table*, Layout*, Symbol*); | |
252 | ||
253 | // Create a GOT entry for the TLS module index. | |
254 | unsigned int | |
255 | got_mod_index_entry(Symbol_table* symtab, Layout* layout, | |
256 | Sized_relobj<size, big_endian>* object); | |
257 | ||
258 | // Get the PLT section. | |
259 | const Output_data_plt_powerpc<size, big_endian>* | |
260 | plt_section() const | |
261 | { | |
262 | gold_assert(this->plt_ != NULL); | |
263 | return this->plt_; | |
264 | } | |
265 | ||
266 | // Get the dynamic reloc section, creating it if necessary. | |
267 | Reloc_section* | |
268 | rela_dyn_section(Layout*); | |
269 | ||
270 | // Return true if the symbol may need a COPY relocation. | |
271 | // References from an executable object to non-function symbols | |
272 | // defined in a dynamic object may need a COPY relocation. | |
273 | bool | |
274 | may_need_copy_reloc(Symbol* gsym) | |
275 | { | |
276 | return (!parameters->options().shared() | |
277 | && gsym->is_from_dynobj() | |
278 | && gsym->type() != elfcpp::STT_FUNC); | |
279 | } | |
280 | ||
281 | // Copy a relocation against a global symbol. | |
282 | void | |
283 | copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object, | |
284 | unsigned int shndx, Output_section* output_section, | |
285 | Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc) | |
286 | { | |
287 | this->copy_relocs_.copy_reloc(symtab, layout, | |
288 | symtab->get_sized_symbol<size>(sym), | |
289 | object, shndx, output_section, | |
290 | reloc, this->rela_dyn_section(layout)); | |
291 | } | |
292 | ||
293 | // Information about this specific target which we pass to the | |
294 | // general Target structure. | |
295 | static Target::Target_info powerpc_info; | |
296 | ||
297 | // The types of GOT entries needed for this platform. | |
298 | enum Got_type | |
299 | { | |
300 | GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol | |
301 | GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset | |
302 | GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair | |
303 | }; | |
304 | ||
305 | // The GOT section. | |
306 | Output_data_got<size, big_endian>* got_; | |
307 | // The GOT2 section. | |
308 | Output_data_space* got2_; | |
309 | // The TOC section. | |
310 | Output_data_space* toc_; | |
311 | // The PLT section. | |
312 | Output_data_plt_powerpc<size, big_endian>* plt_; | |
313 | // The dynamic reloc section. | |
314 | Reloc_section* rela_dyn_; | |
315 | // Relocs saved to avoid a COPY reloc. | |
316 | Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_; | |
317 | // Space for variables copied with a COPY reloc. | |
318 | Output_data_space* dynbss_; | |
319 | // Offset of the GOT entry for the TLS module index; | |
320 | unsigned int got_mod_index_offset_; | |
321 | }; | |
322 | ||
323 | template<> | |
324 | Target::Target_info Target_powerpc<32, true>::powerpc_info = | |
325 | { | |
326 | 32, // size | |
327 | true, // is_big_endian | |
328 | elfcpp::EM_PPC, // machine_code | |
329 | false, // has_make_symbol | |
330 | false, // has_resolve | |
331 | false, // has_code_fill | |
332 | true, // is_default_stack_executable | |
333 | '\0', // wrap_char | |
334 | "/usr/lib/ld.so.1", // dynamic_linker | |
335 | 0x10000000, // default_text_segment_address | |
336 | 64 * 1024, // abi_pagesize (overridable by -z max-page-size) | |
337 | 4 * 1024 // common_pagesize (overridable by -z common-page-size) | |
338 | }; | |
339 | ||
340 | template<> | |
341 | Target::Target_info Target_powerpc<32, false>::powerpc_info = | |
342 | { | |
343 | 32, // size | |
344 | false, // is_big_endian | |
345 | elfcpp::EM_PPC, // machine_code | |
346 | false, // has_make_symbol | |
347 | false, // has_resolve | |
348 | false, // has_code_fill | |
349 | true, // is_default_stack_executable | |
350 | '\0', // wrap_char | |
351 | "/usr/lib/ld.so.1", // dynamic_linker | |
352 | 0x10000000, // default_text_segment_address | |
353 | 64 * 1024, // abi_pagesize (overridable by -z max-page-size) | |
354 | 4 * 1024 // common_pagesize (overridable by -z common-page-size) | |
355 | }; | |
356 | ||
357 | template<> | |
358 | Target::Target_info Target_powerpc<64, true>::powerpc_info = | |
359 | { | |
360 | 64, // size | |
361 | true, // is_big_endian | |
362 | elfcpp::EM_PPC64, // machine_code | |
363 | false, // has_make_symbol | |
364 | false, // has_resolve | |
365 | false, // has_code_fill | |
366 | true, // is_default_stack_executable | |
367 | '\0', // wrap_char | |
368 | "/usr/lib/ld.so.1", // dynamic_linker | |
369 | 0x10000000, // default_text_segment_address | |
370 | 64 * 1024, // abi_pagesize (overridable by -z max-page-size) | |
371 | 8 * 1024 // common_pagesize (overridable by -z common-page-size) | |
372 | }; | |
373 | ||
374 | template<> | |
375 | Target::Target_info Target_powerpc<64, false>::powerpc_info = | |
376 | { | |
377 | 64, // size | |
378 | false, // is_big_endian | |
379 | elfcpp::EM_PPC64, // machine_code | |
380 | false, // has_make_symbol | |
381 | false, // has_resolve | |
382 | false, // has_code_fill | |
383 | true, // is_default_stack_executable | |
384 | '\0', // wrap_char | |
385 | "/usr/lib/ld.so.1", // dynamic_linker | |
386 | 0x10000000, // default_text_segment_address | |
387 | 64 * 1024, // abi_pagesize (overridable by -z max-page-size) | |
388 | 8 * 1024 // common_pagesize (overridable by -z common-page-size) | |
389 | }; | |
390 | ||
391 | template<int size, bool big_endian> | |
392 | class Powerpc_relocate_functions | |
393 | { | |
394 | private: | |
395 | // Do a simple relocation with the addend in the relocation. | |
396 | template<int valsize> | |
397 | static inline void | |
398 | rela(unsigned char* view, | |
399 | unsigned int right_shift, | |
400 | elfcpp::Elf_Xword dst_mask, | |
401 | typename elfcpp::Swap<size, big_endian>::Valtype value, | |
402 | typename elfcpp::Swap<size, big_endian>::Valtype addend) | |
403 | { | |
404 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
405 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
406 | Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv); | |
407 | Valtype reloc = ((value + addend) >> right_shift); | |
408 | ||
409 | val &= ~dst_mask; | |
410 | reloc &= dst_mask; | |
411 | ||
412 | elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc); | |
413 | } | |
414 | ||
415 | // Do a simple relocation using a symbol value with the addend in | |
416 | // the relocation. | |
417 | template<int valsize> | |
418 | static inline void | |
419 | rela(unsigned char* view, | |
420 | unsigned int right_shift, | |
421 | elfcpp::Elf_Xword dst_mask, | |
422 | const Sized_relobj<size, big_endian>* object, | |
423 | const Symbol_value<size>* psymval, | |
424 | typename elfcpp::Swap<valsize, big_endian>::Valtype addend) | |
425 | { | |
426 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
427 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
428 | Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv); | |
429 | Valtype reloc = (psymval->value(object, addend) >> right_shift); | |
430 | ||
431 | val &= ~dst_mask; | |
432 | reloc &= dst_mask; | |
433 | ||
434 | elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc); | |
435 | } | |
436 | ||
437 | // Do a simple relocation using a symbol value with the addend in | |
438 | // the relocation, unaligned. | |
439 | template<int valsize> | |
440 | static inline void | |
441 | rela_ua(unsigned char* view, unsigned int right_shift, | |
442 | elfcpp::Elf_Xword dst_mask, | |
443 | const Sized_relobj<size, big_endian>* object, | |
444 | const Symbol_value<size>* psymval, | |
445 | typename elfcpp::Swap<size, big_endian>::Valtype addend) | |
446 | { | |
447 | typedef typename elfcpp::Swap_unaligned<valsize, | |
448 | big_endian>::Valtype Valtype; | |
449 | unsigned char* wv = view; | |
450 | Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv); | |
451 | Valtype reloc = (psymval->value(object, addend) >> right_shift); | |
452 | ||
453 | val &= ~dst_mask; | |
454 | reloc &= dst_mask; | |
455 | ||
456 | elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc); | |
457 | } | |
458 | ||
459 | // Do a simple PC relative relocation with a Symbol_value with the | |
460 | // addend in the relocation. | |
461 | template<int valsize> | |
462 | static inline void | |
463 | pcrela(unsigned char* view, unsigned int right_shift, | |
464 | elfcpp::Elf_Xword dst_mask, | |
465 | const Sized_relobj<size, big_endian>* object, | |
466 | const Symbol_value<size>* psymval, | |
467 | typename elfcpp::Swap<size, big_endian>::Valtype addend, | |
468 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
469 | { | |
470 | typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype; | |
471 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
472 | Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv); | |
473 | Valtype reloc = ((psymval->value(object, addend) - address) | |
474 | >> right_shift); | |
475 | ||
476 | val &= ~dst_mask; | |
477 | reloc &= dst_mask; | |
478 | ||
479 | elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc); | |
480 | } | |
481 | ||
482 | template<int valsize> | |
483 | static inline void | |
484 | pcrela_unaligned(unsigned char* view, | |
485 | const Sized_relobj<size, big_endian>* object, | |
486 | const Symbol_value<size>* psymval, | |
487 | typename elfcpp::Swap<size, big_endian>::Valtype addend, | |
488 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
489 | { | |
490 | typedef typename elfcpp::Swap_unaligned<valsize, | |
491 | big_endian>::Valtype Valtype; | |
492 | unsigned char* wv = view; | |
493 | Valtype reloc = (psymval->value(object, addend) - address); | |
494 | ||
495 | elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc); | |
496 | } | |
497 | ||
498 | typedef Powerpc_relocate_functions<size, big_endian> This; | |
499 | typedef Relocate_functions<size, big_endian> This_reloc; | |
500 | public: | |
501 | // R_POWERPC_REL32: (Symbol + Addend - Address) | |
502 | static inline void | |
503 | rel32(unsigned char* view, | |
504 | const Sized_relobj<size, big_endian>* object, | |
505 | const Symbol_value<size>* psymval, | |
506 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
507 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
508 | { This_reloc::pcrela32(view, object, psymval, addend, address); } | |
509 | ||
510 | // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc | |
511 | static inline void | |
512 | rel24(unsigned char* view, | |
513 | const Sized_relobj<size, big_endian>* object, | |
514 | const Symbol_value<size>* psymval, | |
515 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
516 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
517 | { | |
518 | This::template pcrela<32>(view, 0, 0x03fffffc, object, | |
519 | psymval, addend, address); | |
520 | } | |
521 | ||
522 | // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc | |
523 | static inline void | |
524 | rel14(unsigned char* view, | |
525 | const Sized_relobj<size, big_endian>* object, | |
526 | const Symbol_value<size>* psymval, | |
527 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
528 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
529 | { | |
530 | This::template pcrela<32>(view, 0, 0x0000fffc, object, | |
531 | psymval, addend, address); | |
532 | } | |
533 | ||
534 | // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff | |
535 | static inline void | |
536 | addr16(unsigned char* view, | |
537 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
538 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
539 | { This_reloc::rela16(view, value, addend); } | |
540 | ||
541 | static inline void | |
542 | addr16(unsigned char* view, | |
543 | const Sized_relobj<size, big_endian>* object, | |
544 | const Symbol_value<size>* psymval, | |
545 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
546 | { This_reloc::rela16(view, object, psymval, addend); } | |
547 | ||
548 | // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc | |
549 | static inline void | |
550 | addr16_ds(unsigned char* view, | |
551 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
552 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
553 | { | |
554 | This::template rela<16>(view, 0, 0xfffc, value, addend); | |
555 | } | |
556 | ||
557 | // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff | |
558 | static inline void | |
559 | addr16_lo(unsigned char* view, | |
560 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
561 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
562 | { This_reloc::rela16(view, value, addend); } | |
563 | ||
564 | static inline void | |
565 | addr16_lo(unsigned char* view, | |
566 | const Sized_relobj<size, big_endian>* object, | |
567 | const Symbol_value<size>* psymval, | |
568 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
569 | { This_reloc::rela16(view, object, psymval, addend); } | |
570 | ||
571 | // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff | |
572 | static inline void | |
573 | addr16_hi(unsigned char* view, | |
574 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
575 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
576 | { | |
577 | This::template rela<16>(view, 16, 0xffff, value, addend); | |
578 | } | |
579 | ||
580 | static inline void | |
581 | addr16_hi(unsigned char* view, | |
582 | const Sized_relobj<size, big_endian>* object, | |
583 | const Symbol_value<size>* psymval, | |
584 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
585 | { | |
586 | This::template rela<16>(view, 16, 0xffff, object, psymval, addend); | |
587 | } | |
588 | ||
589 | // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the | |
590 | // final value of the low 16 bits of the | |
591 | // relocation is negative, add one. | |
592 | static inline void | |
593 | addr16_ha(unsigned char* view, | |
594 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
595 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
596 | { | |
597 | typedef typename elfcpp::Swap<16, true>::Valtype Valtype; | |
598 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
599 | Valtype val = elfcpp::Swap<16, true>::readval(wv); | |
600 | typename elfcpp::Elf_types<size>::Elf_Addr reloc; | |
601 | ||
602 | reloc = value + addend; | |
603 | ||
604 | if (reloc & 0x8000) | |
605 | reloc += 0x10000; | |
606 | reloc >>= 16; | |
607 | ||
5f494ea0 CS |
608 | val &= ~static_cast<Valtype>(0xffff); |
609 | reloc &= static_cast<Valtype>(0xffff); | |
42cacb20 DE |
610 | |
611 | elfcpp::Swap<16, true>::writeval(wv, val | reloc); | |
612 | } | |
613 | ||
614 | static inline void | |
615 | addr16_ha(unsigned char* view, | |
616 | const Sized_relobj<size, big_endian>* object, | |
617 | const Symbol_value<size>* psymval, | |
618 | typename elfcpp::Elf_types<size>::Elf_Addr addend) | |
619 | { | |
620 | typedef typename elfcpp::Swap<16, true>::Valtype Valtype; | |
621 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
622 | Valtype val = elfcpp::Swap<16, true>::readval(wv); | |
623 | typename elfcpp::Elf_types<size>::Elf_Addr reloc; | |
624 | ||
625 | reloc = psymval->value(object, addend); | |
626 | ||
627 | if (reloc & 0x8000) | |
628 | reloc += 0x10000; | |
629 | reloc >>= 16; | |
630 | ||
5f494ea0 CS |
631 | val &= ~static_cast<Valtype>(0xffff); |
632 | reloc &= static_cast<Valtype>(0xffff); | |
42cacb20 DE |
633 | |
634 | elfcpp::Swap<16, true>::writeval(wv, val | reloc); | |
635 | } | |
636 | ||
637 | // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff | |
638 | static inline void | |
639 | rel16(unsigned char* view, | |
640 | const Sized_relobj<size, big_endian>* object, | |
641 | const Symbol_value<size>* psymval, | |
642 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
643 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
644 | { This_reloc::pcrela16(view, object, psymval, addend, address); } | |
645 | ||
646 | // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff | |
647 | static inline void | |
648 | rel16_lo(unsigned char* view, | |
649 | const Sized_relobj<size, big_endian>* object, | |
650 | const Symbol_value<size>* psymval, | |
651 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
652 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
653 | { This_reloc::pcrela16(view, object, psymval, addend, address); } | |
654 | ||
655 | // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff | |
656 | static inline void | |
657 | rel16_hi(unsigned char* view, | |
658 | const Sized_relobj<size, big_endian>* object, | |
659 | const Symbol_value<size>* psymval, | |
660 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
661 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
662 | { | |
663 | This::template pcrela<16>(view, 16, 0xffff, object, | |
664 | psymval, addend, address); | |
665 | } | |
666 | ||
667 | // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the | |
668 | // final value of the low 16 bits of the | |
669 | // relocation is negative, add one. | |
670 | static inline void | |
671 | rel16_ha(unsigned char* view, | |
672 | const Sized_relobj<size, big_endian>* object, | |
673 | const Symbol_value<size>* psymval, | |
674 | typename elfcpp::Elf_types<size>::Elf_Addr addend, | |
675 | typename elfcpp::Elf_types<size>::Elf_Addr address) | |
676 | { | |
677 | typedef typename elfcpp::Swap<16, true>::Valtype Valtype; | |
678 | Valtype* wv = reinterpret_cast<Valtype*>(view); | |
679 | Valtype val = elfcpp::Swap<16, true>::readval(wv); | |
680 | typename elfcpp::Elf_types<size>::Elf_Addr reloc; | |
681 | ||
682 | reloc = (psymval->value(object, addend) - address); | |
683 | if (reloc & 0x8000) | |
684 | reloc += 0x10000; | |
685 | reloc >>= 16; | |
686 | ||
5f494ea0 CS |
687 | val &= ~static_cast<Valtype>(0xffff); |
688 | reloc &= static_cast<Valtype>(0xffff); | |
42cacb20 DE |
689 | |
690 | elfcpp::Swap<16, true>::writeval(wv, val | reloc); | |
691 | } | |
692 | }; | |
693 | ||
694 | // Get the GOT section, creating it if necessary. | |
695 | ||
696 | template<int size, bool big_endian> | |
697 | Output_data_got<size, big_endian>* | |
698 | Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab, | |
699 | Layout* layout) | |
700 | { | |
701 | if (this->got_ == NULL) | |
702 | { | |
703 | gold_assert(symtab != NULL && layout != NULL); | |
704 | ||
705 | this->got_ = new Output_data_got<size, big_endian>(); | |
706 | ||
707 | layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, | |
708 | elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, | |
709 | this->got_); | |
710 | ||
711 | // Create the GOT2 or TOC in the .got section. | |
712 | if (size == 32) | |
713 | { | |
714 | this->got2_ = new Output_data_space(4, "** GOT2"); | |
715 | layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS, | |
716 | elfcpp::SHF_ALLOC | |
717 | | elfcpp::SHF_WRITE, | |
718 | this->got2_); | |
719 | } | |
720 | else | |
721 | { | |
722 | this->toc_ = new Output_data_space(8, "** TOC"); | |
723 | layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS, | |
724 | elfcpp::SHF_ALLOC | |
725 | | elfcpp::SHF_WRITE, | |
726 | this->toc_); | |
727 | } | |
728 | ||
729 | // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section. | |
730 | symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, | |
731 | this->got_, | |
732 | 0, 0, elfcpp::STT_OBJECT, | |
733 | elfcpp::STB_LOCAL, | |
734 | elfcpp::STV_HIDDEN, 0, | |
735 | false, false); | |
736 | } | |
737 | ||
738 | return this->got_; | |
739 | } | |
740 | ||
741 | // Get the dynamic reloc section, creating it if necessary. | |
742 | ||
743 | template<int size, bool big_endian> | |
744 | typename Target_powerpc<size, big_endian>::Reloc_section* | |
745 | Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout) | |
746 | { | |
747 | if (this->rela_dyn_ == NULL) | |
748 | { | |
749 | gold_assert(layout != NULL); | |
750 | this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); | |
751 | layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, | |
752 | elfcpp::SHF_ALLOC, this->rela_dyn_); | |
753 | } | |
754 | return this->rela_dyn_; | |
755 | } | |
756 | ||
757 | // A class to handle the PLT data. | |
758 | ||
759 | template<int size, bool big_endian> | |
760 | class Output_data_plt_powerpc : public Output_section_data | |
761 | { | |
762 | public: | |
763 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, | |
764 | size, big_endian> Reloc_section; | |
765 | ||
766 | Output_data_plt_powerpc(Layout*); | |
767 | ||
768 | // Add an entry to the PLT. | |
769 | void add_entry(Symbol* gsym); | |
770 | ||
771 | // Return the .rela.plt section data. | |
772 | const Reloc_section* rel_plt() const | |
773 | { | |
774 | return this->rel_; | |
775 | } | |
776 | ||
777 | protected: | |
778 | void do_adjust_output_section(Output_section* os); | |
779 | ||
780 | private: | |
781 | // The size of an entry in the PLT. | |
782 | static const int base_plt_entry_size = (size == 32 ? 16 : 24); | |
783 | ||
784 | // Set the final size. | |
785 | void | |
786 | set_final_data_size() | |
787 | { | |
788 | unsigned int full_count = this->count_ + 4; | |
789 | ||
790 | this->set_data_size(full_count * base_plt_entry_size); | |
791 | } | |
792 | ||
793 | // Write out the PLT data. | |
794 | void | |
795 | do_write(Output_file*); | |
796 | ||
797 | // The reloc section. | |
798 | Reloc_section* rel_; | |
799 | // The number of PLT entries. | |
800 | unsigned int count_; | |
801 | }; | |
802 | ||
803 | // Create the PLT section. The ordinary .got section is an argument, | |
804 | // since we need to refer to the start. | |
805 | ||
806 | template<int size, bool big_endian> | |
807 | Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout) | |
808 | : Output_section_data(size == 32 ? 4 : 8), count_(0) | |
809 | { | |
810 | this->rel_ = new Reloc_section(false); | |
811 | layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, | |
812 | elfcpp::SHF_ALLOC, this->rel_); | |
813 | } | |
814 | ||
815 | template<int size, bool big_endian> | |
816 | void | |
817 | Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os) | |
818 | { | |
819 | os->set_entsize(0); | |
820 | } | |
821 | ||
822 | // Add an entry to the PLT. | |
823 | ||
824 | template<int size, bool big_endian> | |
825 | void | |
826 | Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym) | |
827 | { | |
828 | gold_assert(!gsym->has_plt_offset()); | |
829 | unsigned int index = this->count_+ + 4; | |
830 | section_offset_type plt_offset; | |
831 | ||
832 | if (index < 8192) | |
833 | plt_offset = index * base_plt_entry_size; | |
834 | else | |
835 | gold_unreachable(); | |
836 | ||
837 | gsym->set_plt_offset(plt_offset); | |
838 | ||
839 | ++this->count_; | |
840 | ||
841 | gsym->set_needs_dynsym_entry(); | |
842 | this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, | |
843 | plt_offset, 0); | |
844 | } | |
845 | ||
846 | static const unsigned int addis_11_11 = 0x3d6b0000; | |
847 | static const unsigned int addis_11_30 = 0x3d7e0000; | |
848 | static const unsigned int addis_12_12 = 0x3d8c0000; | |
849 | static const unsigned int addi_11_11 = 0x396b0000; | |
850 | static const unsigned int add_0_11_11 = 0x7c0b5a14; | |
851 | static const unsigned int add_11_0_11 = 0x7d605a14; | |
852 | static const unsigned int b = 0x48000000; | |
853 | static const unsigned int bcl_20_31 = 0x429f0005; | |
854 | static const unsigned int bctr = 0x4e800420; | |
855 | static const unsigned int lis_11 = 0x3d600000; | |
856 | static const unsigned int lis_12 = 0x3d800000; | |
857 | static const unsigned int lwzu_0_12 = 0x840c0000; | |
858 | static const unsigned int lwz_0_12 = 0x800c0000; | |
859 | static const unsigned int lwz_11_11 = 0x816b0000; | |
860 | static const unsigned int lwz_11_30 = 0x817e0000; | |
861 | static const unsigned int lwz_12_12 = 0x818c0000; | |
862 | static const unsigned int mflr_0 = 0x7c0802a6; | |
863 | static const unsigned int mflr_12 = 0x7d8802a6; | |
864 | static const unsigned int mtctr_0 = 0x7c0903a6; | |
865 | static const unsigned int mtctr_11 = 0x7d6903a6; | |
866 | static const unsigned int mtlr_0 = 0x7c0803a6; | |
867 | static const unsigned int nop = 0x60000000; | |
868 | static const unsigned int sub_11_11_12 = 0x7d6c5850; | |
869 | ||
870 | static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */ | |
871 | static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */ | |
872 | static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */ | |
873 | static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */ | |
874 | /* ld %r11,xxx+16@l(%r12) */ | |
875 | ||
876 | ||
877 | // Write out the PLT. | |
878 | ||
879 | template<int size, bool big_endian> | |
880 | void | |
881 | Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of) | |
882 | { | |
883 | const off_t offset = this->offset(); | |
884 | const section_size_type oview_size = | |
885 | convert_to_section_size_type(this->data_size()); | |
886 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
887 | unsigned char* pov = oview; | |
888 | ||
889 | memset(pov, 0, base_plt_entry_size * 4); | |
890 | pov += base_plt_entry_size * 4; | |
891 | ||
892 | unsigned int plt_offset = base_plt_entry_size * 4; | |
893 | const unsigned int count = this->count_; | |
894 | ||
895 | if (size == 64) | |
896 | { | |
897 | for (unsigned int i = 0; i < count; i++) | |
898 | { | |
899 | } | |
900 | } | |
901 | else | |
902 | { | |
903 | for (unsigned int i = 0; i < count; i++) | |
904 | { | |
905 | elfcpp::Swap<32, true>::writeval(pov + 0x00, | |
906 | lwz_11_30 + plt_offset); | |
907 | elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11); | |
908 | elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr); | |
ce3ac18a | 909 | elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop); |
42cacb20 DE |
910 | pov += base_plt_entry_size; |
911 | plt_offset += base_plt_entry_size; | |
912 | } | |
913 | } | |
914 | ||
915 | gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); | |
916 | ||
917 | of->write_output_view(offset, oview_size, oview); | |
918 | } | |
919 | ||
920 | // Create a PLT entry for a global symbol. | |
921 | ||
922 | template<int size, bool big_endian> | |
923 | void | |
924 | Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab, | |
925 | Layout* layout, | |
926 | Symbol* gsym) | |
927 | { | |
928 | if (gsym->has_plt_offset()) | |
929 | return; | |
930 | ||
931 | if (this->plt_ == NULL) | |
932 | { | |
933 | // Create the GOT section first. | |
934 | this->got_section(symtab, layout); | |
935 | ||
936 | this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout); | |
937 | layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, | |
938 | (elfcpp::SHF_ALLOC | |
939 | | elfcpp::SHF_EXECINSTR | |
940 | | elfcpp::SHF_WRITE), | |
941 | this->plt_); | |
942 | ||
943 | // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section. | |
944 | symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL, | |
945 | this->plt_, | |
946 | 0, 0, elfcpp::STT_OBJECT, | |
947 | elfcpp::STB_LOCAL, | |
948 | elfcpp::STV_HIDDEN, 0, | |
949 | false, false); | |
950 | } | |
951 | ||
952 | this->plt_->add_entry(gsym); | |
953 | } | |
954 | ||
955 | // Create a GOT entry for the TLS module index. | |
956 | ||
957 | template<int size, bool big_endian> | |
958 | unsigned int | |
959 | Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab, | |
960 | Layout* layout, | |
961 | Sized_relobj<size, big_endian>* object) | |
962 | { | |
963 | if (this->got_mod_index_offset_ == -1U) | |
964 | { | |
965 | gold_assert(symtab != NULL && layout != NULL && object != NULL); | |
966 | Reloc_section* rela_dyn = this->rela_dyn_section(layout); | |
967 | Output_data_got<size, big_endian>* got; | |
968 | unsigned int got_offset; | |
969 | ||
970 | got = this->got_section(symtab, layout); | |
971 | got_offset = got->add_constant(0); | |
972 | rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got, | |
973 | got_offset, 0); | |
974 | got->add_constant(0); | |
975 | this->got_mod_index_offset_ = got_offset; | |
976 | } | |
977 | return this->got_mod_index_offset_; | |
978 | } | |
979 | ||
980 | // Optimize the TLS relocation type based on what we know about the | |
981 | // symbol. IS_FINAL is true if the final address of this symbol is | |
982 | // known at link time. | |
983 | ||
984 | static tls::Tls_optimization | |
985 | optimize_tls_reloc(bool /* is_final */, int r_type) | |
986 | { | |
987 | // If we are generating a shared library, then we can't do anything | |
988 | // in the linker. | |
989 | if (parameters->options().shared()) | |
990 | return tls::TLSOPT_NONE; | |
991 | switch (r_type) | |
992 | { | |
993 | // XXX | |
994 | default: | |
995 | gold_unreachable(); | |
996 | } | |
997 | } | |
998 | ||
999 | // Report an unsupported relocation against a local symbol. | |
1000 | ||
1001 | template<int size, bool big_endian> | |
1002 | void | |
1003 | Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local( | |
1004 | Sized_relobj<size, big_endian>* object, | |
1005 | unsigned int r_type) | |
1006 | { | |
1007 | gold_error(_("%s: unsupported reloc %u against local symbol"), | |
1008 | object->name().c_str(), r_type); | |
1009 | } | |
1010 | ||
1011 | // We are about to emit a dynamic relocation of type R_TYPE. If the | |
1012 | // dynamic linker does not support it, issue an error. | |
1013 | ||
1014 | template<int size, bool big_endian> | |
1015 | void | |
1016 | Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object, | |
1017 | unsigned int r_type) | |
1018 | { | |
1019 | gold_assert(r_type != elfcpp::R_POWERPC_NONE); | |
1020 | ||
1021 | // These are the relocation types supported by glibc for both 32-bit | |
1022 | // and 64-bit powerpc. | |
1023 | switch (r_type) | |
1024 | { | |
1025 | case elfcpp::R_POWERPC_RELATIVE: | |
1026 | case elfcpp::R_POWERPC_GLOB_DAT: | |
1027 | case elfcpp::R_POWERPC_DTPMOD: | |
1028 | case elfcpp::R_POWERPC_DTPREL: | |
1029 | case elfcpp::R_POWERPC_TPREL: | |
1030 | case elfcpp::R_POWERPC_JMP_SLOT: | |
1031 | case elfcpp::R_POWERPC_COPY: | |
1032 | case elfcpp::R_POWERPC_ADDR32: | |
1033 | case elfcpp::R_POWERPC_ADDR24: | |
1034 | case elfcpp::R_POWERPC_REL24: | |
1035 | return; | |
1036 | ||
1037 | default: | |
1038 | break; | |
1039 | } | |
1040 | ||
1041 | if (size == 64) | |
1042 | { | |
1043 | switch (r_type) | |
1044 | { | |
1045 | // These are the relocation types supported only on 64-bit. | |
1046 | case elfcpp::R_PPC64_ADDR64: | |
1047 | case elfcpp::R_PPC64_TPREL16_LO_DS: | |
1048 | case elfcpp::R_PPC64_TPREL16_DS: | |
1049 | case elfcpp::R_POWERPC_TPREL16: | |
1050 | case elfcpp::R_POWERPC_TPREL16_LO: | |
1051 | case elfcpp::R_POWERPC_TPREL16_HI: | |
1052 | case elfcpp::R_POWERPC_TPREL16_HA: | |
1053 | case elfcpp::R_PPC64_TPREL16_HIGHER: | |
1054 | case elfcpp::R_PPC64_TPREL16_HIGHEST: | |
1055 | case elfcpp::R_PPC64_TPREL16_HIGHERA: | |
1056 | case elfcpp::R_PPC64_TPREL16_HIGHESTA: | |
1057 | case elfcpp::R_PPC64_ADDR16_LO_DS: | |
1058 | case elfcpp::R_POWERPC_ADDR16_LO: | |
1059 | case elfcpp::R_POWERPC_ADDR16_HI: | |
1060 | case elfcpp::R_POWERPC_ADDR16_HA: | |
1061 | case elfcpp::R_POWERPC_ADDR30: | |
1062 | case elfcpp::R_PPC64_UADDR64: | |
1063 | case elfcpp::R_POWERPC_UADDR32: | |
1064 | case elfcpp::R_POWERPC_ADDR16: | |
1065 | case elfcpp::R_POWERPC_UADDR16: | |
1066 | case elfcpp::R_PPC64_ADDR16_DS: | |
1067 | case elfcpp::R_PPC64_ADDR16_HIGHER: | |
1068 | case elfcpp::R_PPC64_ADDR16_HIGHEST: | |
1069 | case elfcpp::R_PPC64_ADDR16_HIGHERA: | |
1070 | case elfcpp::R_PPC64_ADDR16_HIGHESTA: | |
1071 | case elfcpp::R_POWERPC_ADDR14_BRTAKEN: | |
1072 | case elfcpp::R_POWERPC_ADDR14_BRNTAKEN: | |
1073 | case elfcpp::R_POWERPC_REL32: | |
1074 | case elfcpp::R_PPC64_REL64: | |
1075 | return; | |
1076 | ||
1077 | default: | |
1078 | break; | |
1079 | } | |
1080 | } | |
1081 | else | |
1082 | { | |
1083 | switch (r_type) | |
1084 | { | |
1085 | // These are the relocation types supported only on 32-bit. | |
1086 | ||
1087 | default: | |
1088 | break; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | // This prevents us from issuing more than one error per reloc | |
1093 | // section. But we can still wind up issuing more than one | |
1094 | // error per object file. | |
1095 | if (this->issued_non_pic_error_) | |
1096 | return; | |
1097 | object->error(_("requires unsupported dynamic reloc; " | |
1098 | "recompile with -fPIC")); | |
1099 | this->issued_non_pic_error_ = true; | |
1100 | return; | |
1101 | } | |
1102 | ||
1103 | // Scan a relocation for a local symbol. | |
1104 | ||
1105 | template<int size, bool big_endian> | |
1106 | inline void | |
1107 | Target_powerpc<size, big_endian>::Scan::local( | |
1108 | const General_options&, | |
1109 | Symbol_table* symtab, | |
1110 | Layout* layout, | |
1111 | Target_powerpc<size, big_endian>* target, | |
1112 | Sized_relobj<size, big_endian>* object, | |
1113 | unsigned int data_shndx, | |
1114 | Output_section* output_section, | |
1115 | const elfcpp::Rela<size, big_endian>& reloc, | |
1116 | unsigned int r_type, | |
1117 | const elfcpp::Sym<size, big_endian>& lsym) | |
1118 | { | |
1119 | switch (r_type) | |
1120 | { | |
1121 | case elfcpp::R_POWERPC_NONE: | |
1122 | case elfcpp::R_POWERPC_GNU_VTINHERIT: | |
1123 | case elfcpp::R_POWERPC_GNU_VTENTRY: | |
1124 | break; | |
1125 | ||
1126 | case elfcpp::R_PPC64_ADDR64: | |
1127 | case elfcpp::R_POWERPC_ADDR32: | |
1128 | case elfcpp::R_POWERPC_ADDR16_HA: | |
1129 | case elfcpp::R_POWERPC_ADDR16_LO: | |
1130 | // If building a shared library (or a position-independent | |
1131 | // executable), we need to create a dynamic relocation for | |
1132 | // this location. | |
1133 | if (parameters->options().output_is_position_independent()) | |
1134 | { | |
1135 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1136 | ||
1137 | check_non_pic(object, r_type); | |
1138 | if (lsym.get_st_type() != elfcpp::STT_SECTION) | |
1139 | { | |
1140 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
1141 | rela_dyn->add_local(object, r_sym, r_type, output_section, | |
1142 | data_shndx, reloc.get_r_offset(), | |
1143 | reloc.get_r_addend()); | |
1144 | } | |
1145 | else | |
1146 | { | |
1147 | unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
1148 | gold_assert(lsym.get_st_value() == 0); | |
1149 | rela_dyn->add_local_relative(object, r_sym, r_type, | |
1150 | output_section, data_shndx, | |
1151 | reloc.get_r_offset(), | |
1152 | reloc.get_r_addend()); | |
1153 | } | |
1154 | } | |
1155 | break; | |
1156 | ||
1157 | case elfcpp::R_POWERPC_REL24: | |
1158 | case elfcpp::R_PPC_LOCAL24PC: | |
1159 | case elfcpp::R_POWERPC_REL32: | |
1160 | case elfcpp::R_PPC_REL16_LO: | |
1161 | case elfcpp::R_PPC_REL16_HA: | |
1162 | break; | |
1163 | ||
1164 | case elfcpp::R_POWERPC_GOT16: | |
1165 | case elfcpp::R_POWERPC_GOT16_LO: | |
1166 | case elfcpp::R_POWERPC_GOT16_HI: | |
1167 | case elfcpp::R_POWERPC_GOT16_HA: | |
1168 | case elfcpp::R_PPC64_TOC16: | |
1169 | case elfcpp::R_PPC64_TOC16_LO: | |
1170 | case elfcpp::R_PPC64_TOC16_HI: | |
1171 | case elfcpp::R_PPC64_TOC16_HA: | |
1172 | case elfcpp::R_PPC64_TOC16_DS: | |
1173 | case elfcpp::R_PPC64_TOC16_LO_DS: | |
1174 | { | |
1175 | // The symbol requires a GOT entry. | |
1176 | Output_data_got<size, big_endian>* got; | |
1177 | unsigned int r_sym; | |
1178 | ||
1179 | got = target->got_section(symtab, layout); | |
1180 | r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); | |
1181 | ||
1182 | // If we are generating a shared object, we need to add a | |
1183 | // dynamic relocation for this symbol's GOT entry. | |
1184 | if (parameters->options().output_is_position_independent()) | |
1185 | { | |
1186 | if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)) | |
1187 | { | |
1188 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1189 | unsigned int off; | |
1190 | ||
1191 | off = got->add_constant(0); | |
1192 | object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off); | |
1193 | rela_dyn->add_local_relative(object, r_sym, | |
1194 | elfcpp::R_POWERPC_RELATIVE, | |
1195 | got, off, 0); | |
1196 | } | |
1197 | } | |
1198 | else | |
1199 | got->add_local(object, r_sym, GOT_TYPE_STANDARD); | |
1200 | } | |
1201 | break; | |
1202 | ||
1203 | case elfcpp::R_PPC64_TOC: | |
1204 | // We need a GOT section. | |
1205 | target->got_section(symtab, layout); | |
1206 | break; | |
1207 | ||
1208 | // These are relocations which should only be seen by the | |
1209 | // dynamic linker, and should never be seen here. | |
1210 | case elfcpp::R_POWERPC_COPY: | |
1211 | case elfcpp::R_POWERPC_GLOB_DAT: | |
1212 | case elfcpp::R_POWERPC_JMP_SLOT: | |
1213 | case elfcpp::R_POWERPC_RELATIVE: | |
1214 | case elfcpp::R_POWERPC_DTPMOD: | |
1215 | gold_error(_("%s: unexpected reloc %u in object file"), | |
1216 | object->name().c_str(), r_type); | |
1217 | break; | |
1218 | ||
1219 | default: | |
1220 | unsupported_reloc_local(object, r_type); | |
1221 | break; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | // Report an unsupported relocation against a global symbol. | |
1226 | ||
1227 | template<int size, bool big_endian> | |
1228 | void | |
1229 | Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global( | |
1230 | Sized_relobj<size, big_endian>* object, | |
1231 | unsigned int r_type, | |
1232 | Symbol* gsym) | |
1233 | { | |
1234 | gold_error(_("%s: unsupported reloc %u against global symbol %s"), | |
1235 | object->name().c_str(), r_type, gsym->demangled_name().c_str()); | |
1236 | } | |
1237 | ||
1238 | // Scan a relocation for a global symbol. | |
1239 | ||
1240 | template<int size, bool big_endian> | |
1241 | inline void | |
1242 | Target_powerpc<size, big_endian>::Scan::global( | |
1243 | const General_options&, | |
1244 | Symbol_table* symtab, | |
1245 | Layout* layout, | |
1246 | Target_powerpc<size, big_endian>* target, | |
1247 | Sized_relobj<size, big_endian>* object, | |
1248 | unsigned int data_shndx, | |
1249 | Output_section* output_section, | |
1250 | const elfcpp::Rela<size, big_endian>& reloc, | |
1251 | unsigned int r_type, | |
1252 | Symbol* gsym) | |
1253 | { | |
1254 | switch (r_type) | |
1255 | { | |
1256 | case elfcpp::R_POWERPC_NONE: | |
1257 | case elfcpp::R_POWERPC_GNU_VTINHERIT: | |
1258 | case elfcpp::R_POWERPC_GNU_VTENTRY: | |
1259 | break; | |
1260 | ||
1261 | case elfcpp::R_PPC_PLTREL24: | |
1262 | // If the symbol is fully resolved, this is just a PC32 reloc. | |
1263 | // Otherwise we need a PLT entry. | |
1264 | if (gsym->final_value_is_known()) | |
1265 | break; | |
1266 | // If building a shared library, we can also skip the PLT entry | |
1267 | // if the symbol is defined in the output file and is protected | |
1268 | // or hidden. | |
1269 | if (gsym->is_defined() | |
1270 | && !gsym->is_from_dynobj() | |
1271 | && !gsym->is_preemptible()) | |
1272 | break; | |
1273 | target->make_plt_entry(symtab, layout, gsym); | |
1274 | break; | |
1275 | ||
1276 | case elfcpp::R_POWERPC_ADDR16: | |
1277 | case elfcpp::R_POWERPC_ADDR16_LO: | |
1278 | case elfcpp::R_POWERPC_ADDR16_HI: | |
1279 | case elfcpp::R_POWERPC_ADDR16_HA: | |
1280 | case elfcpp::R_POWERPC_ADDR32: | |
1281 | case elfcpp::R_PPC64_ADDR64: | |
1282 | { | |
1283 | // Make a PLT entry if necessary. | |
1284 | if (gsym->needs_plt_entry()) | |
1285 | { | |
1286 | target->make_plt_entry(symtab, layout, gsym); | |
1287 | // Since this is not a PC-relative relocation, we may be | |
1288 | // taking the address of a function. In that case we need to | |
1289 | // set the entry in the dynamic symbol table to the address of | |
1290 | // the PLT entry. | |
1291 | if (gsym->is_from_dynobj() && !parameters->options().shared()) | |
1292 | gsym->set_needs_dynsym_value(); | |
1293 | } | |
1294 | // Make a dynamic relocation if necessary. | |
1295 | if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)) | |
1296 | { | |
1297 | if (target->may_need_copy_reloc(gsym)) | |
1298 | { | |
1299 | target->copy_reloc(symtab, layout, object, | |
1300 | data_shndx, output_section, gsym, reloc); | |
1301 | } | |
1302 | else if ((r_type == elfcpp::R_POWERPC_ADDR32 | |
1303 | || r_type == elfcpp::R_PPC64_ADDR64) | |
1304 | && gsym->can_use_relative_reloc(false)) | |
1305 | { | |
1306 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1307 | rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE, | |
1308 | output_section, object, | |
1309 | data_shndx, reloc.get_r_offset(), | |
1310 | reloc.get_r_addend()); | |
1311 | } | |
1312 | else | |
1313 | { | |
1314 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1315 | ||
1316 | check_non_pic(object, r_type); | |
1317 | if (gsym->is_from_dynobj() | |
1318 | || gsym->is_undefined() | |
1319 | || gsym->is_preemptible()) | |
1320 | rela_dyn->add_global(gsym, r_type, output_section, | |
1321 | object, data_shndx, | |
1322 | reloc.get_r_offset(), | |
1323 | reloc.get_r_addend()); | |
1324 | else | |
1325 | rela_dyn->add_global_relative(gsym, r_type, | |
1326 | output_section, object, | |
1327 | data_shndx, | |
1328 | reloc.get_r_offset(), | |
1329 | reloc.get_r_addend()); | |
1330 | } | |
1331 | } | |
1332 | } | |
1333 | break; | |
1334 | ||
1335 | case elfcpp::R_POWERPC_REL24: | |
1336 | case elfcpp::R_PPC_LOCAL24PC: | |
1337 | case elfcpp::R_PPC_REL16: | |
1338 | case elfcpp::R_PPC_REL16_LO: | |
1339 | case elfcpp::R_PPC_REL16_HI: | |
1340 | case elfcpp::R_PPC_REL16_HA: | |
1341 | { | |
1342 | if (gsym->needs_plt_entry()) | |
1343 | target->make_plt_entry(symtab, layout, gsym); | |
1344 | // Make a dynamic relocation if necessary. | |
1345 | int flags = Symbol::NON_PIC_REF; | |
1346 | if (gsym->type() == elfcpp::STT_FUNC) | |
1347 | flags |= Symbol::FUNCTION_CALL; | |
1348 | if (gsym->needs_dynamic_reloc(flags)) | |
1349 | { | |
1350 | if (target->may_need_copy_reloc(gsym)) | |
1351 | { | |
1352 | target->copy_reloc(symtab, layout, object, | |
1353 | data_shndx, output_section, gsym, | |
1354 | reloc); | |
1355 | } | |
1356 | else | |
1357 | { | |
1358 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1359 | check_non_pic(object, r_type); | |
1360 | rela_dyn->add_global(gsym, r_type, output_section, object, | |
1361 | data_shndx, reloc.get_r_offset(), | |
1362 | reloc.get_r_addend()); | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | break; | |
1367 | ||
1368 | case elfcpp::R_POWERPC_GOT16: | |
1369 | case elfcpp::R_POWERPC_GOT16_LO: | |
1370 | case elfcpp::R_POWERPC_GOT16_HI: | |
1371 | case elfcpp::R_POWERPC_GOT16_HA: | |
1372 | case elfcpp::R_PPC64_TOC16: | |
1373 | case elfcpp::R_PPC64_TOC16_LO: | |
1374 | case elfcpp::R_PPC64_TOC16_HI: | |
1375 | case elfcpp::R_PPC64_TOC16_HA: | |
1376 | case elfcpp::R_PPC64_TOC16_DS: | |
1377 | case elfcpp::R_PPC64_TOC16_LO_DS: | |
1378 | { | |
1379 | // The symbol requires a GOT entry. | |
1380 | Output_data_got<size, big_endian>* got; | |
1381 | ||
1382 | got = target->got_section(symtab, layout); | |
1383 | if (gsym->final_value_is_known()) | |
1384 | got->add_global(gsym, GOT_TYPE_STANDARD); | |
1385 | else | |
1386 | { | |
1387 | // If this symbol is not fully resolved, we need to add a | |
1388 | // dynamic relocation for it. | |
1389 | Reloc_section* rela_dyn = target->rela_dyn_section(layout); | |
1390 | if (gsym->is_from_dynobj() | |
1391 | || gsym->is_undefined() | |
1392 | || gsym->is_preemptible()) | |
1393 | got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn, | |
1394 | elfcpp::R_POWERPC_GLOB_DAT); | |
1395 | else if (!gsym->has_got_offset(GOT_TYPE_STANDARD)) | |
1396 | { | |
1397 | unsigned int off = got->add_constant(0); | |
1398 | ||
1399 | gsym->set_got_offset(GOT_TYPE_STANDARD, off); | |
1400 | rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE, | |
1401 | got, off, 0); | |
1402 | } | |
1403 | } | |
1404 | } | |
1405 | break; | |
1406 | ||
1407 | case elfcpp::R_PPC64_TOC: | |
1408 | // We need a GOT section. | |
1409 | target->got_section(symtab, layout); | |
1410 | break; | |
1411 | ||
1412 | case elfcpp::R_POWERPC_GOT_TPREL16: | |
1413 | case elfcpp::R_POWERPC_TLS: | |
1414 | // XXX TLS | |
1415 | break; | |
1416 | ||
1417 | // These are relocations which should only be seen by the | |
1418 | // dynamic linker, and should never be seen here. | |
1419 | case elfcpp::R_POWERPC_COPY: | |
1420 | case elfcpp::R_POWERPC_GLOB_DAT: | |
1421 | case elfcpp::R_POWERPC_JMP_SLOT: | |
1422 | case elfcpp::R_POWERPC_RELATIVE: | |
1423 | case elfcpp::R_POWERPC_DTPMOD: | |
1424 | gold_error(_("%s: unexpected reloc %u in object file"), | |
1425 | object->name().c_str(), r_type); | |
1426 | break; | |
1427 | ||
1428 | default: | |
1429 | unsupported_reloc_global(object, r_type, gsym); | |
1430 | break; | |
1431 | } | |
1432 | } | |
1433 | ||
1434 | // Scan relocations for a section. | |
1435 | ||
1436 | template<int size, bool big_endian> | |
1437 | void | |
1438 | Target_powerpc<size, big_endian>::scan_relocs( | |
1439 | const General_options& options, | |
1440 | Symbol_table* symtab, | |
1441 | Layout* layout, | |
1442 | Sized_relobj<size, big_endian>* object, | |
1443 | unsigned int data_shndx, | |
1444 | unsigned int sh_type, | |
1445 | const unsigned char* prelocs, | |
1446 | size_t reloc_count, | |
1447 | Output_section* output_section, | |
1448 | bool needs_special_offset_handling, | |
1449 | size_t local_symbol_count, | |
1450 | const unsigned char* plocal_symbols) | |
1451 | { | |
1452 | typedef Target_powerpc<size, big_endian> Powerpc; | |
1453 | typedef typename Target_powerpc<size, big_endian>::Scan Scan; | |
1454 | static Output_data_space* sdata; | |
1455 | ||
1456 | if (sh_type == elfcpp::SHT_REL) | |
1457 | { | |
1458 | gold_error(_("%s: unsupported REL reloc section"), | |
1459 | object->name().c_str()); | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | // Define _SDA_BASE_ at the start of the .sdata section. | |
1464 | if (sdata == NULL) | |
1465 | { | |
1466 | // layout->find_output_section(".sdata") == NULL | |
1467 | sdata = new Output_data_space(4, "** sdata"); | |
1468 | Output_section* os = layout->add_output_section_data(".sdata", 0, | |
1469 | elfcpp::SHF_ALLOC | |
1470 | | elfcpp::SHF_WRITE, | |
1471 | sdata); | |
1472 | symtab->define_in_output_data("_SDA_BASE_", NULL, | |
1473 | os, | |
1474 | 32768, 0, | |
1475 | elfcpp::STT_OBJECT, | |
1476 | elfcpp::STB_LOCAL, | |
1477 | elfcpp::STV_HIDDEN, 0, | |
1478 | false, false); | |
1479 | } | |
1480 | ||
1481 | gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>( | |
1482 | options, | |
1483 | symtab, | |
1484 | layout, | |
1485 | this, | |
1486 | object, | |
1487 | data_shndx, | |
1488 | prelocs, | |
1489 | reloc_count, | |
1490 | output_section, | |
1491 | needs_special_offset_handling, | |
1492 | local_symbol_count, | |
1493 | plocal_symbols); | |
1494 | } | |
1495 | ||
1496 | // Finalize the sections. | |
1497 | ||
1498 | template<int size, bool big_endian> | |
1499 | void | |
1500 | Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout) | |
1501 | { | |
1502 | // Fill in some more dynamic tags. | |
1503 | Output_data_dynamic* const odyn = layout->dynamic_data(); | |
1504 | if (odyn != NULL) | |
1505 | { | |
1506 | if (this->plt_ != NULL) | |
1507 | { | |
1508 | const Output_data* od = this->plt_->rel_plt(); | |
1509 | odyn->add_section_size(elfcpp::DT_PLTRELSZ, od); | |
1510 | odyn->add_section_address(elfcpp::DT_JMPREL, od); | |
1511 | odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA); | |
1512 | ||
1513 | odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_); | |
1514 | } | |
1515 | ||
1516 | if (this->rela_dyn_ != NULL) | |
1517 | { | |
1518 | const Output_data* od = this->rela_dyn_; | |
1519 | odyn->add_section_address(elfcpp::DT_RELA, od); | |
1520 | odyn->add_section_size(elfcpp::DT_RELASZ, od); | |
1521 | odyn->add_constant(elfcpp::DT_RELAENT, | |
1522 | elfcpp::Elf_sizes<size>::rela_size); | |
1523 | } | |
1524 | ||
1525 | if (!parameters->options().shared()) | |
1526 | { | |
1527 | // The value of the DT_DEBUG tag is filled in by the dynamic | |
1528 | // linker at run time, and used by the debugger. | |
1529 | odyn->add_constant(elfcpp::DT_DEBUG, 0); | |
1530 | } | |
1531 | } | |
1532 | ||
1533 | // Emit any relocs we saved in an attempt to avoid generating COPY | |
1534 | // relocs. | |
1535 | if (this->copy_relocs_.any_saved_relocs()) | |
1536 | this->copy_relocs_.emit(this->rela_dyn_section(layout)); | |
1537 | } | |
1538 | ||
1539 | // Perform a relocation. | |
1540 | ||
1541 | template<int size, bool big_endian> | |
1542 | inline bool | |
1543 | Target_powerpc<size, big_endian>::Relocate::relocate( | |
1544 | const Relocate_info<size, big_endian>* relinfo, | |
1545 | Target_powerpc* target, | |
1546 | size_t relnum, | |
1547 | const elfcpp::Rela<size, big_endian>& rela, | |
1548 | unsigned int r_type, | |
1549 | const Sized_symbol<size>* gsym, | |
1550 | const Symbol_value<size>* psymval, | |
1551 | unsigned char* view, | |
1552 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
1553 | section_size_type /* view_size */) | |
1554 | { | |
1555 | const unsigned int toc_base_offset = 0x8000; | |
1556 | typedef Powerpc_relocate_functions<size, big_endian> Reloc; | |
1557 | ||
1558 | // Pick the value to use for symbols defined in shared objects. | |
1559 | Symbol_value<size> symval; | |
1560 | if (gsym != NULL | |
1561 | && (gsym->is_from_dynobj() | |
1562 | || (parameters->options().shared() | |
1563 | && (gsym->is_undefined() || gsym->is_preemptible()))) | |
1564 | && gsym->has_plt_offset()) | |
1565 | { | |
1566 | elfcpp::Elf_Xword value; | |
1567 | ||
1568 | value = target->plt_section()->address() + gsym->plt_offset(); | |
1569 | ||
1570 | symval.set_output_value(value); | |
1571 | ||
1572 | psymval = &symval; | |
1573 | } | |
1574 | ||
1575 | const Sized_relobj<size, big_endian>* object = relinfo->object; | |
1576 | elfcpp::Elf_Xword addend = rela.get_r_addend(); | |
1577 | ||
1578 | // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT | |
1579 | // pointer points to the beginning, not the end, of the table. | |
1580 | // So we just use the plain offset. | |
1581 | bool have_got_offset = false; | |
1582 | unsigned int got_offset = 0; | |
1583 | unsigned int got2_offset = 0; | |
1584 | switch (r_type) | |
1585 | { | |
1586 | case elfcpp::R_PPC64_TOC16: | |
1587 | case elfcpp::R_PPC64_TOC16_LO: | |
1588 | case elfcpp::R_PPC64_TOC16_HI: | |
1589 | case elfcpp::R_PPC64_TOC16_HA: | |
1590 | case elfcpp::R_PPC64_TOC16_DS: | |
1591 | case elfcpp::R_PPC64_TOC16_LO_DS: | |
1592 | // Subtract the TOC base address. | |
1593 | addend -= target->toc_section()->address() + toc_base_offset; | |
1594 | /* FALLTHRU */ | |
1595 | ||
1596 | case elfcpp::R_POWERPC_GOT16: | |
1597 | case elfcpp::R_POWERPC_GOT16_LO: | |
1598 | case elfcpp::R_POWERPC_GOT16_HI: | |
1599 | case elfcpp::R_POWERPC_GOT16_HA: | |
1600 | case elfcpp::R_PPC64_GOT16_DS: | |
1601 | case elfcpp::R_PPC64_GOT16_LO_DS: | |
1602 | if (gsym != NULL) | |
1603 | { | |
1604 | gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); | |
1605 | got_offset = gsym->got_offset(GOT_TYPE_STANDARD); | |
1606 | } | |
1607 | else | |
1608 | { | |
1609 | unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); | |
1610 | gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); | |
1611 | got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD); | |
1612 | } | |
1613 | have_got_offset = true; | |
1614 | break; | |
1615 | ||
1616 | // R_PPC_PLTREL24 is rather special. If non-zero, | |
1617 | // the addend specifies the GOT pointer offset within .got2. | |
1618 | case elfcpp::R_PPC_PLTREL24: | |
1619 | if (addend >= 32768) | |
1620 | { | |
1621 | Output_data_space* got2; | |
1622 | got2 = target->got2_section(); | |
1623 | got2_offset = got2->offset(); | |
1624 | addend += got2_offset; | |
1625 | } | |
1626 | have_got_offset = true; | |
1627 | break; | |
1628 | ||
1629 | default: | |
1630 | break; | |
1631 | } | |
1632 | ||
1633 | switch (r_type) | |
1634 | { | |
1635 | case elfcpp::R_POWERPC_NONE: | |
1636 | case elfcpp::R_POWERPC_GNU_VTINHERIT: | |
1637 | case elfcpp::R_POWERPC_GNU_VTENTRY: | |
1638 | break; | |
1639 | ||
1640 | case elfcpp::R_POWERPC_REL32: | |
1641 | Reloc::rel32(view, object, psymval, addend, address); | |
1642 | break; | |
1643 | ||
1644 | case elfcpp::R_POWERPC_REL24: | |
1645 | Reloc::rel24(view, object, psymval, addend, address); | |
1646 | break; | |
1647 | ||
1648 | case elfcpp::R_POWERPC_REL14: | |
1649 | Reloc::rel14(view, object, psymval, addend, address); | |
1650 | break; | |
1651 | ||
1652 | case elfcpp::R_PPC_PLTREL24: | |
1653 | Reloc::rel24(view, object, psymval, addend, address); | |
1654 | break; | |
1655 | ||
1656 | case elfcpp::R_PPC_LOCAL24PC: | |
1657 | Reloc::rel24(view, object, psymval, addend, address); | |
1658 | break; | |
1659 | ||
1660 | case elfcpp::R_PPC64_ADDR64: | |
1661 | if (!parameters->options().output_is_position_independent()) | |
1662 | Relocate_functions<size, big_endian>::rela64(view, object, | |
1663 | psymval, addend); | |
1664 | break; | |
1665 | ||
1666 | case elfcpp::R_POWERPC_ADDR32: | |
1667 | if (!parameters->options().output_is_position_independent()) | |
1668 | Relocate_functions<size, big_endian>::rela32(view, object, | |
1669 | psymval, addend); | |
1670 | break; | |
1671 | ||
1672 | case elfcpp::R_POWERPC_ADDR16_LO: | |
1673 | Reloc::addr16_lo(view, object, psymval, addend); | |
1674 | break; | |
1675 | ||
1676 | case elfcpp::R_POWERPC_ADDR16_HI: | |
1677 | Reloc::addr16_hi(view, object, psymval, addend); | |
1678 | break; | |
1679 | ||
1680 | case elfcpp::R_POWERPC_ADDR16_HA: | |
1681 | Reloc::addr16_ha(view, object, psymval, addend); | |
1682 | break; | |
1683 | ||
1684 | case elfcpp::R_PPC_REL16_LO: | |
1685 | Reloc::rel16_lo(view, object, psymval, addend, address); | |
1686 | break; | |
1687 | ||
1688 | case elfcpp::R_PPC_REL16_HI: | |
1689 | Reloc::rel16_lo(view, object, psymval, addend, address); | |
1690 | break; | |
1691 | ||
1692 | case elfcpp::R_PPC_REL16_HA: | |
1693 | Reloc::rel16_lo(view, object, psymval, addend, address); | |
1694 | break; | |
1695 | ||
1696 | case elfcpp::R_POWERPC_GOT16: | |
1697 | Reloc::addr16(view, got_offset, addend); | |
1698 | break; | |
1699 | ||
1700 | case elfcpp::R_POWERPC_GOT16_LO: | |
1701 | Reloc::addr16_lo(view, got_offset, addend); | |
1702 | break; | |
1703 | ||
1704 | case elfcpp::R_POWERPC_GOT16_HI: | |
1705 | Reloc::addr16_hi(view, got_offset, addend); | |
1706 | break; | |
1707 | ||
1708 | case elfcpp::R_POWERPC_GOT16_HA: | |
1709 | Reloc::addr16_ha(view, got_offset, addend); | |
1710 | break; | |
1711 | ||
1712 | case elfcpp::R_PPC64_TOC16: | |
1713 | Reloc::addr16(view, got_offset, addend); | |
1714 | break; | |
1715 | ||
1716 | case elfcpp::R_PPC64_TOC16_LO: | |
1717 | Reloc::addr16_lo(view, got_offset, addend); | |
1718 | break; | |
1719 | ||
1720 | case elfcpp::R_PPC64_TOC16_HI: | |
1721 | Reloc::addr16_hi(view, got_offset, addend); | |
1722 | break; | |
1723 | ||
1724 | case elfcpp::R_PPC64_TOC16_HA: | |
1725 | Reloc::addr16_ha(view, got_offset, addend); | |
1726 | break; | |
1727 | ||
1728 | case elfcpp::R_PPC64_TOC16_DS: | |
1729 | case elfcpp::R_PPC64_TOC16_LO_DS: | |
1730 | Reloc::addr16_ds(view, got_offset, addend); | |
1731 | break; | |
1732 | ||
1733 | case elfcpp::R_PPC64_TOC: | |
1734 | { | |
1735 | elfcpp::Elf_types<64>::Elf_Addr value; | |
1736 | value = target->toc_section()->address() + toc_base_offset; | |
1737 | Relocate_functions<64, false>::rela64(view, value, addend); | |
1738 | } | |
1739 | break; | |
1740 | ||
1741 | case elfcpp::R_POWERPC_COPY: | |
1742 | case elfcpp::R_POWERPC_GLOB_DAT: | |
1743 | case elfcpp::R_POWERPC_JMP_SLOT: | |
1744 | case elfcpp::R_POWERPC_RELATIVE: | |
1745 | // This is an outstanding tls reloc, which is unexpected when | |
1746 | // linking. | |
1747 | case elfcpp::R_POWERPC_DTPMOD: | |
1748 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
1749 | _("unexpected reloc %u in object file"), | |
1750 | r_type); | |
1751 | break; | |
1752 | ||
1753 | default: | |
1754 | gold_error_at_location(relinfo, relnum, rela.get_r_offset(), | |
1755 | _("unsupported reloc %u"), | |
1756 | r_type); | |
1757 | break; | |
1758 | } | |
1759 | ||
1760 | return true; | |
1761 | } | |
1762 | ||
1763 | // Perform a TLS relocation. | |
1764 | ||
1765 | template<int size, bool big_endian> | |
1766 | inline void | |
1767 | Target_powerpc<size, big_endian>::Relocate::relocate_tls( | |
1768 | const Relocate_info<size, big_endian>* relinfo, | |
1769 | Target_powerpc<size, big_endian>* target, | |
1770 | size_t relnum, | |
1771 | const elfcpp::Rela<size, big_endian>& rela, | |
1772 | unsigned int r_type, | |
1773 | const Sized_symbol<size>* gsym, | |
1774 | const Symbol_value<size>* psymval, | |
1775 | unsigned char* view, | |
1776 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
1777 | section_size_type) | |
1778 | { | |
1779 | Output_segment* tls_segment = relinfo->layout->tls_segment(); | |
1780 | typedef Powerpc_relocate_functions<size, big_endian> Reloc; | |
1781 | const Sized_relobj<size, big_endian>* object = relinfo->object; | |
1782 | ||
1783 | const elfcpp::Elf_Xword addend = rela.get_r_addend(); | |
1784 | typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0); | |
1785 | ||
1786 | const bool is_final = | |
1787 | (gsym == NULL | |
1788 | ? !parameters->options().output_is_position_independent() | |
1789 | : gsym->final_value_is_known()); | |
1790 | const tls::Tls_optimization optimized_type | |
1791 | = optimize_tls_reloc(is_final, r_type); | |
1792 | ||
1793 | switch (r_type) | |
1794 | { | |
1795 | // XXX | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | // Relocate section data. | |
1800 | ||
1801 | template<int size, bool big_endian> | |
1802 | void | |
1803 | Target_powerpc<size, big_endian>::relocate_section( | |
1804 | const Relocate_info<size, big_endian>* relinfo, | |
1805 | unsigned int sh_type, | |
1806 | const unsigned char* prelocs, | |
1807 | size_t reloc_count, | |
1808 | Output_section* output_section, | |
1809 | bool needs_special_offset_handling, | |
1810 | unsigned char* view, | |
1811 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
1812 | section_size_type view_size) | |
1813 | { | |
1814 | typedef Target_powerpc<size, big_endian> Powerpc; | |
1815 | typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate; | |
1816 | ||
1817 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1818 | ||
1819 | gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA, | |
1820 | Powerpc_relocate>( | |
1821 | relinfo, | |
1822 | this, | |
1823 | prelocs, | |
1824 | reloc_count, | |
1825 | output_section, | |
1826 | needs_special_offset_handling, | |
1827 | view, | |
1828 | address, | |
1829 | view_size); | |
1830 | } | |
1831 | ||
1832 | // Return the size of a relocation while scanning during a relocatable | |
1833 | // link. | |
1834 | ||
1835 | template<int size, bool big_endian> | |
1836 | unsigned int | |
1837 | Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc( | |
1838 | unsigned int, | |
1839 | Relobj*) | |
1840 | { | |
1841 | // We are always SHT_RELA, so we should never get here. | |
1842 | gold_unreachable(); | |
1843 | return 0; | |
1844 | } | |
1845 | ||
1846 | // Scan the relocs during a relocatable link. | |
1847 | ||
1848 | template<int size, bool big_endian> | |
1849 | void | |
1850 | Target_powerpc<size, big_endian>::scan_relocatable_relocs( | |
1851 | const General_options& options, | |
1852 | Symbol_table* symtab, | |
1853 | Layout* layout, | |
1854 | Sized_relobj<size, big_endian>* object, | |
1855 | unsigned int data_shndx, | |
1856 | unsigned int sh_type, | |
1857 | const unsigned char* prelocs, | |
1858 | size_t reloc_count, | |
1859 | Output_section* output_section, | |
1860 | bool needs_special_offset_handling, | |
1861 | size_t local_symbol_count, | |
1862 | const unsigned char* plocal_symbols, | |
1863 | Relocatable_relocs* rr) | |
1864 | { | |
1865 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1866 | ||
1867 | typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA, | |
1868 | Relocatable_size_for_reloc> Scan_relocatable_relocs; | |
1869 | ||
1870 | gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA, | |
1871 | Scan_relocatable_relocs>( | |
1872 | options, | |
1873 | symtab, | |
1874 | layout, | |
1875 | object, | |
1876 | data_shndx, | |
1877 | prelocs, | |
1878 | reloc_count, | |
1879 | output_section, | |
1880 | needs_special_offset_handling, | |
1881 | local_symbol_count, | |
1882 | plocal_symbols, | |
1883 | rr); | |
1884 | } | |
1885 | ||
1886 | // Relocate a section during a relocatable link. | |
1887 | ||
1888 | template<int size, bool big_endian> | |
1889 | void | |
1890 | Target_powerpc<size, big_endian>::relocate_for_relocatable( | |
1891 | const Relocate_info<size, big_endian>* relinfo, | |
1892 | unsigned int sh_type, | |
1893 | const unsigned char* prelocs, | |
1894 | size_t reloc_count, | |
1895 | Output_section* output_section, | |
1896 | off_t offset_in_output_section, | |
1897 | const Relocatable_relocs* rr, | |
1898 | unsigned char* view, | |
1899 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
1900 | section_size_type view_size, | |
1901 | unsigned char* reloc_view, | |
1902 | section_size_type reloc_view_size) | |
1903 | { | |
1904 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
1905 | ||
1906 | gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>( | |
1907 | relinfo, | |
1908 | prelocs, | |
1909 | reloc_count, | |
1910 | output_section, | |
1911 | offset_in_output_section, | |
1912 | rr, | |
1913 | view, | |
1914 | view_address, | |
1915 | view_size, | |
1916 | reloc_view, | |
1917 | reloc_view_size); | |
1918 | } | |
1919 | ||
1920 | // Return the value to use for a dynamic which requires special | |
1921 | // treatment. This is how we support equality comparisons of function | |
1922 | // pointers across shared library boundaries, as described in the | |
1923 | // processor specific ABI supplement. | |
1924 | ||
1925 | template<int size, bool big_endian> | |
1926 | uint64_t | |
1927 | Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const | |
1928 | { | |
1929 | gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); | |
1930 | return this->plt_section()->address() + gsym->plt_offset(); | |
1931 | } | |
1932 | ||
1933 | // The selector for powerpc object files. | |
1934 | ||
1935 | template<int size, bool big_endian> | |
1936 | class Target_selector_powerpc : public Target_selector | |
1937 | { | |
1938 | public: | |
1939 | Target_selector_powerpc() | |
1940 | : Target_selector(elfcpp::EM_NONE, size, big_endian, | |
1941 | (size == 64 ? | |
1942 | (big_endian ? "elf64-powerpc" : "elf64-powerpcle") : | |
1943 | (big_endian ? "elf32-powerpc" : "elf32-powerpcle"))) | |
1944 | { } | |
1945 | ||
1946 | Target* instantiated_target_; | |
1947 | ||
1948 | Target* do_recognize(int machine, int, int) | |
1949 | { | |
1950 | switch (size) | |
1951 | { | |
1952 | case 64: | |
1953 | if (machine != elfcpp::EM_PPC64) | |
1954 | return NULL; | |
1955 | break; | |
1956 | ||
1957 | case 32: | |
1958 | if (machine != elfcpp::EM_PPC) | |
1959 | return NULL; | |
1960 | break; | |
1961 | ||
1962 | default: | |
1963 | return NULL; | |
1964 | } | |
1965 | ||
1966 | return do_instantiate_target(); | |
1967 | } | |
1968 | ||
1969 | Target* do_instantiate_target() | |
1970 | { | |
1971 | if (this->instantiated_target_ == NULL) | |
1972 | this->instantiated_target_ = new Target_powerpc<size, big_endian>(); | |
1973 | return this->instantiated_target_; | |
1974 | } | |
1975 | }; | |
1976 | ||
1977 | Target_selector_powerpc<32, true> target_selector_ppc32; | |
1978 | Target_selector_powerpc<32, false> target_selector_ppc32le; | |
1979 | Target_selector_powerpc<64, true> target_selector_ppc64; | |
1980 | Target_selector_powerpc<64, false> target_selector_ppc64le; | |
1981 | ||
1982 | } // End anonymous namespace. |