1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
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.
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.
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.
27 #include "parameters.h"
34 #include "copy-relocs.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
46 template<int size
, bool big_endian
>
47 class Output_data_plt_powerpc
;
49 template<int size
, bool big_endian
>
50 class Target_powerpc
: public Sized_target
<size
, big_endian
>
53 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
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)
64 // Scan the relocations to look for symbol adjustments.
66 scan_relocs(const General_options
& options
,
69 Sized_relobj
<size
, big_endian
>* object
,
70 unsigned int data_shndx
,
72 const unsigned char* prelocs
,
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.
80 do_finalize_sections(Layout
*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol
*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info
<size
, big_endian
>*,
91 const unsigned char* prelocs
,
93 Output_section
* output_section
,
94 bool needs_special_offset_handling
,
96 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
97 section_size_type view_size
);
99 // Scan the relocs during a relocatable link.
101 scan_relocatable_relocs(const General_options
& options
,
102 Symbol_table
* symtab
,
104 Sized_relobj
<size
, big_endian
>* object
,
105 unsigned int data_shndx
,
106 unsigned int sh_type
,
107 const unsigned char* prelocs
,
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
*);
115 // Relocate a section during a relocatable link.
117 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
118 unsigned int sh_type
,
119 const unsigned char* prelocs
,
121 Output_section
* output_section
,
122 off_t offset_in_output_section
,
123 const Relocatable_relocs
*,
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
);
130 // Return whether SYM is defined by the ABI.
132 do_is_defined_by_abi(const Symbol
* sym
) const
134 return strcmp(sym
->name(), "___tls_get_addr") == 0;
137 // Return the size of the GOT section.
141 gold_assert(this->got_
!= NULL
);
142 return this->got_
->data_size();
147 // The class which scans relocations.
152 : issued_non_pic_error_(false)
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
);
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
,
175 unsupported_reloc_local(Sized_relobj
<size
, big_endian
>*,
176 unsigned int r_type
);
179 unsupported_reloc_global(Sized_relobj
<size
, big_endian
>*,
180 unsigned int r_type
, Symbol
*);
183 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
184 Target_powerpc
* target
);
187 check_non_pic(Relobj
*, unsigned int r_type
);
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_
;
193 // The class which implements relocation.
197 // Do a relocation. Return false if the caller should not issue
198 // any warnings about this relocation.
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
>*,
205 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
209 // Do a TLS relocation.
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
>*,
217 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
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
227 get_size_for_reloc(unsigned int, Relobj
*);
230 // Get the GOT section, creating it if necessary.
231 Output_data_got
<size
, big_endian
>*
232 got_section(Symbol_table
*, Layout
*);
237 gold_assert (this->got2_
!= NULL
);
241 // Get the TOC section.
245 gold_assert (this->toc_
!= NULL
);
249 // Create a PLT entry for a global symbol.
251 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
253 // Create a GOT entry for the TLS module index.
255 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
256 Sized_relobj
<size
, big_endian
>* object
);
258 // Get the PLT section.
259 const Output_data_plt_powerpc
<size
, big_endian
>*
262 gold_assert(this->plt_
!= NULL
);
266 // Get the dynamic reloc section, creating it if necessary.
268 rela_dyn_section(Layout
*);
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.
274 may_need_copy_reloc(Symbol
* gsym
)
276 return (!parameters
->options().shared()
277 && gsym
->is_from_dynobj()
278 && gsym
->type() != elfcpp::STT_FUNC
);
281 // Copy a relocation against a global symbol.
283 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
284 Sized_relobj
<size
, big_endian
>* object
,
285 unsigned int shndx
, Output_section
* output_section
,
286 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
288 this->copy_relocs_
.copy_reloc(symtab
, layout
,
289 symtab
->get_sized_symbol
<size
>(sym
),
290 object
, shndx
, output_section
,
291 reloc
, this->rela_dyn_section(layout
));
294 // Information about this specific target which we pass to the
295 // general Target structure.
296 static Target::Target_info powerpc_info
;
298 // The types of GOT entries needed for this platform.
301 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
302 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
303 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
307 Output_data_got
<size
, big_endian
>* got_
;
309 Output_data_space
* got2_
;
311 Output_data_space
* toc_
;
313 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
314 // The dynamic reloc section.
315 Reloc_section
* rela_dyn_
;
316 // Relocs saved to avoid a COPY reloc.
317 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
318 // Space for variables copied with a COPY reloc.
319 Output_data_space
* dynbss_
;
320 // Offset of the GOT entry for the TLS module index;
321 unsigned int got_mod_index_offset_
;
325 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
328 true, // is_big_endian
329 elfcpp::EM_PPC
, // machine_code
330 false, // has_make_symbol
331 false, // has_resolve
332 false, // has_code_fill
333 true, // is_default_stack_executable
335 "/usr/lib/ld.so.1", // dynamic_linker
336 0x10000000, // default_text_segment_address
337 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
338 4 * 1024 // common_pagesize (overridable by -z common-page-size)
342 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
345 false, // is_big_endian
346 elfcpp::EM_PPC
, // machine_code
347 false, // has_make_symbol
348 false, // has_resolve
349 false, // has_code_fill
350 true, // is_default_stack_executable
352 "/usr/lib/ld.so.1", // dynamic_linker
353 0x10000000, // default_text_segment_address
354 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
355 4 * 1024 // common_pagesize (overridable by -z common-page-size)
359 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
362 true, // is_big_endian
363 elfcpp::EM_PPC64
, // machine_code
364 false, // has_make_symbol
365 false, // has_resolve
366 false, // has_code_fill
367 true, // is_default_stack_executable
369 "/usr/lib/ld.so.1", // dynamic_linker
370 0x10000000, // default_text_segment_address
371 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
372 8 * 1024 // common_pagesize (overridable by -z common-page-size)
376 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
379 false, // is_big_endian
380 elfcpp::EM_PPC64
, // machine_code
381 false, // has_make_symbol
382 false, // has_resolve
383 false, // has_code_fill
384 true, // is_default_stack_executable
386 "/usr/lib/ld.so.1", // dynamic_linker
387 0x10000000, // default_text_segment_address
388 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
389 8 * 1024 // common_pagesize (overridable by -z common-page-size)
392 template<int size
, bool big_endian
>
393 class Powerpc_relocate_functions
396 // Do a simple relocation with the addend in the relocation.
397 template<int valsize
>
399 rela(unsigned char* view
,
400 unsigned int right_shift
,
401 elfcpp::Elf_Xword dst_mask
,
402 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
403 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
405 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
406 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
407 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
408 Valtype reloc
= ((value
+ addend
) >> right_shift
);
413 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
416 // Do a simple relocation using a symbol value with the addend in
418 template<int valsize
>
420 rela(unsigned char* view
,
421 unsigned int right_shift
,
422 elfcpp::Elf_Xword dst_mask
,
423 const Sized_relobj
<size
, big_endian
>* object
,
424 const Symbol_value
<size
>* psymval
,
425 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
427 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
428 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
429 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
430 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
435 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
438 // Do a simple relocation using a symbol value with the addend in
439 // the relocation, unaligned.
440 template<int valsize
>
442 rela_ua(unsigned char* view
, unsigned int right_shift
,
443 elfcpp::Elf_Xword dst_mask
,
444 const Sized_relobj
<size
, big_endian
>* object
,
445 const Symbol_value
<size
>* psymval
,
446 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
448 typedef typename
elfcpp::Swap_unaligned
<valsize
,
449 big_endian
>::Valtype Valtype
;
450 unsigned char* wv
= view
;
451 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
452 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
457 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
460 // Do a simple PC relative relocation with a Symbol_value with the
461 // addend in the relocation.
462 template<int valsize
>
464 pcrela(unsigned char* view
, unsigned int right_shift
,
465 elfcpp::Elf_Xword dst_mask
,
466 const Sized_relobj
<size
, big_endian
>* object
,
467 const Symbol_value
<size
>* psymval
,
468 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
469 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
471 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
472 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
473 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
474 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
480 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
483 template<int valsize
>
485 pcrela_unaligned(unsigned char* view
,
486 const Sized_relobj
<size
, big_endian
>* object
,
487 const Symbol_value
<size
>* psymval
,
488 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
489 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
491 typedef typename
elfcpp::Swap_unaligned
<valsize
,
492 big_endian
>::Valtype Valtype
;
493 unsigned char* wv
= view
;
494 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
496 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
499 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
500 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
502 // R_POWERPC_REL32: (Symbol + Addend - Address)
504 rel32(unsigned char* view
,
505 const Sized_relobj
<size
, big_endian
>* object
,
506 const Symbol_value
<size
>* psymval
,
507 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
508 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
509 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
511 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
513 rel24(unsigned char* view
,
514 const Sized_relobj
<size
, big_endian
>* object
,
515 const Symbol_value
<size
>* psymval
,
516 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
517 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
519 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
520 psymval
, addend
, address
);
523 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
525 rel14(unsigned char* view
,
526 const Sized_relobj
<size
, big_endian
>* object
,
527 const Symbol_value
<size
>* psymval
,
528 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
529 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
531 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
532 psymval
, addend
, address
);
535 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
537 addr16(unsigned char* view
,
538 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
539 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
540 { This_reloc::rela16(view
, value
, addend
); }
543 addr16(unsigned char* view
,
544 const Sized_relobj
<size
, big_endian
>* object
,
545 const Symbol_value
<size
>* psymval
,
546 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
547 { This_reloc::rela16(view
, object
, psymval
, addend
); }
549 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
551 addr16_ds(unsigned char* view
,
552 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
553 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
555 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
558 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
560 addr16_lo(unsigned char* view
,
561 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
562 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
563 { This_reloc::rela16(view
, value
, addend
); }
566 addr16_lo(unsigned char* view
,
567 const Sized_relobj
<size
, big_endian
>* object
,
568 const Symbol_value
<size
>* psymval
,
569 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
570 { This_reloc::rela16(view
, object
, psymval
, addend
); }
572 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
574 addr16_hi(unsigned char* view
,
575 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
576 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
578 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
582 addr16_hi(unsigned char* view
,
583 const Sized_relobj
<size
, big_endian
>* object
,
584 const Symbol_value
<size
>* psymval
,
585 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
587 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
590 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
591 // final value of the low 16 bits of the
592 // relocation is negative, add one.
594 addr16_ha(unsigned char* view
,
595 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
600 reloc
= value
+ addend
;
606 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
610 addr16_ha(unsigned char* view
,
611 const Sized_relobj
<size
, big_endian
>* object
,
612 const Symbol_value
<size
>* psymval
,
613 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
615 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
617 reloc
= psymval
->value(object
, addend
);
623 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
626 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
628 rel16(unsigned char* view
,
629 const Sized_relobj
<size
, big_endian
>* object
,
630 const Symbol_value
<size
>* psymval
,
631 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
632 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
633 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
635 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
637 rel16_lo(unsigned char* view
,
638 const Sized_relobj
<size
, big_endian
>* object
,
639 const Symbol_value
<size
>* psymval
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
641 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
642 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
644 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
646 rel16_hi(unsigned char* view
,
647 const Sized_relobj
<size
, big_endian
>* object
,
648 const Symbol_value
<size
>* psymval
,
649 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
650 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
652 This::template pcrela
<16>(view
, 16, 0xffff, object
,
653 psymval
, addend
, address
);
656 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
657 // final value of the low 16 bits of the
658 // relocation is negative, add one.
660 rel16_ha(unsigned char* view
,
661 const Sized_relobj
<size
, big_endian
>* object
,
662 const Symbol_value
<size
>* psymval
,
663 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
664 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
666 typedef typename
elfcpp::Swap
<16, true>::Valtype Valtype
;
667 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
668 Valtype val
= elfcpp::Swap
<16, true>::readval(wv
);
669 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
671 reloc
= (psymval
->value(object
, addend
) - address
);
676 val
&= ~static_cast<Valtype
>(0xffff);
677 reloc
&= static_cast<Valtype
>(0xffff);
679 elfcpp::Swap
<16, true>::writeval(wv
, val
| reloc
);
683 // Get the GOT section, creating it if necessary.
685 template<int size
, bool big_endian
>
686 Output_data_got
<size
, big_endian
>*
687 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
690 if (this->got_
== NULL
)
692 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
694 this->got_
= new Output_data_got
<size
, big_endian
>();
696 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
697 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
700 // Create the GOT2 or TOC in the .got section.
703 this->got2_
= new Output_data_space(4, "** GOT2");
704 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
711 this->toc_
= new Output_data_space(8, "** TOC");
712 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
718 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
719 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
721 0, 0, elfcpp::STT_OBJECT
,
723 elfcpp::STV_HIDDEN
, 0,
730 // Get the dynamic reloc section, creating it if necessary.
732 template<int size
, bool big_endian
>
733 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
734 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
736 if (this->rela_dyn_
== NULL
)
738 gold_assert(layout
!= NULL
);
739 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
740 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
741 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
743 return this->rela_dyn_
;
746 // A class to handle the PLT data.
748 template<int size
, bool big_endian
>
749 class Output_data_plt_powerpc
: public Output_section_data
752 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
753 size
, big_endian
> Reloc_section
;
755 Output_data_plt_powerpc(Layout
*);
757 // Add an entry to the PLT.
758 void add_entry(Symbol
* gsym
);
760 // Return the .rela.plt section data.
761 const Reloc_section
* rel_plt() const
767 void do_adjust_output_section(Output_section
* os
);
770 // The size of an entry in the PLT.
771 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
773 // Set the final size.
775 set_final_data_size()
777 unsigned int full_count
= this->count_
+ 4;
779 this->set_data_size(full_count
* base_plt_entry_size
);
782 // Write out the PLT data.
784 do_write(Output_file
*);
786 // The reloc section.
788 // The number of PLT entries.
792 // Create the PLT section. The ordinary .got section is an argument,
793 // since we need to refer to the start.
795 template<int size
, bool big_endian
>
796 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
797 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
799 this->rel_
= new Reloc_section(false);
800 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
801 elfcpp::SHF_ALLOC
, this->rel_
);
804 template<int size
, bool big_endian
>
806 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
811 // Add an entry to the PLT.
813 template<int size
, bool big_endian
>
815 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
817 gold_assert(!gsym
->has_plt_offset());
818 unsigned int index
= this->count_
+ + 4;
819 section_offset_type plt_offset
;
822 plt_offset
= index
* base_plt_entry_size
;
826 gsym
->set_plt_offset(plt_offset
);
830 gsym
->set_needs_dynsym_entry();
831 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
835 static const unsigned int addis_11_11
= 0x3d6b0000;
836 static const unsigned int addis_11_30
= 0x3d7e0000;
837 static const unsigned int addis_12_12
= 0x3d8c0000;
838 static const unsigned int addi_11_11
= 0x396b0000;
839 static const unsigned int add_0_11_11
= 0x7c0b5a14;
840 static const unsigned int add_11_0_11
= 0x7d605a14;
841 static const unsigned int b
= 0x48000000;
842 static const unsigned int bcl_20_31
= 0x429f0005;
843 static const unsigned int bctr
= 0x4e800420;
844 static const unsigned int lis_11
= 0x3d600000;
845 static const unsigned int lis_12
= 0x3d800000;
846 static const unsigned int lwzu_0_12
= 0x840c0000;
847 static const unsigned int lwz_0_12
= 0x800c0000;
848 static const unsigned int lwz_11_11
= 0x816b0000;
849 static const unsigned int lwz_11_30
= 0x817e0000;
850 static const unsigned int lwz_12_12
= 0x818c0000;
851 static const unsigned int mflr_0
= 0x7c0802a6;
852 static const unsigned int mflr_12
= 0x7d8802a6;
853 static const unsigned int mtctr_0
= 0x7c0903a6;
854 static const unsigned int mtctr_11
= 0x7d6903a6;
855 static const unsigned int mtlr_0
= 0x7c0803a6;
856 static const unsigned int nop
= 0x60000000;
857 static const unsigned int sub_11_11_12
= 0x7d6c5850;
859 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
860 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
861 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
862 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
863 /* ld %r11,xxx+16@l(%r12) */
866 // Write out the PLT.
868 template<int size
, bool big_endian
>
870 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
872 const off_t offset
= this->offset();
873 const section_size_type oview_size
=
874 convert_to_section_size_type(this->data_size());
875 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
876 unsigned char* pov
= oview
;
878 memset(pov
, 0, base_plt_entry_size
* 4);
879 pov
+= base_plt_entry_size
* 4;
881 unsigned int plt_offset
= base_plt_entry_size
* 4;
882 const unsigned int count
= this->count_
;
886 for (unsigned int i
= 0; i
< count
; i
++)
892 for (unsigned int i
= 0; i
< count
; i
++)
894 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
895 lwz_11_30
+ plt_offset
);
896 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
897 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
898 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
899 pov
+= base_plt_entry_size
;
900 plt_offset
+= base_plt_entry_size
;
904 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
906 of
->write_output_view(offset
, oview_size
, oview
);
909 // Create a PLT entry for a global symbol.
911 template<int size
, bool big_endian
>
913 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
917 if (gsym
->has_plt_offset())
920 if (this->plt_
== NULL
)
922 // Create the GOT section first.
923 this->got_section(symtab
, layout
);
925 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
926 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
928 | elfcpp::SHF_EXECINSTR
929 | elfcpp::SHF_WRITE
),
932 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
933 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
935 0, 0, elfcpp::STT_OBJECT
,
937 elfcpp::STV_HIDDEN
, 0,
941 this->plt_
->add_entry(gsym
);
944 // Create a GOT entry for the TLS module index.
946 template<int size
, bool big_endian
>
948 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
950 Sized_relobj
<size
, big_endian
>* object
)
952 if (this->got_mod_index_offset_
== -1U)
954 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
955 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
956 Output_data_got
<size
, big_endian
>* got
;
957 unsigned int got_offset
;
959 got
= this->got_section(symtab
, layout
);
960 got_offset
= got
->add_constant(0);
961 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
963 got
->add_constant(0);
964 this->got_mod_index_offset_
= got_offset
;
966 return this->got_mod_index_offset_
;
969 // Optimize the TLS relocation type based on what we know about the
970 // symbol. IS_FINAL is true if the final address of this symbol is
971 // known at link time.
973 static tls::Tls_optimization
974 optimize_tls_reloc(bool /* is_final */, int r_type
)
976 // If we are generating a shared library, then we can't do anything
978 if (parameters
->options().shared())
979 return tls::TLSOPT_NONE
;
988 // Report an unsupported relocation against a local symbol.
990 template<int size
, bool big_endian
>
992 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
993 Sized_relobj
<size
, big_endian
>* object
,
996 gold_error(_("%s: unsupported reloc %u against local symbol"),
997 object
->name().c_str(), r_type
);
1000 // We are about to emit a dynamic relocation of type R_TYPE. If the
1001 // dynamic linker does not support it, issue an error.
1003 template<int size
, bool big_endian
>
1005 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1006 unsigned int r_type
)
1008 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1010 // These are the relocation types supported by glibc for both 32-bit
1011 // and 64-bit powerpc.
1014 case elfcpp::R_POWERPC_RELATIVE
:
1015 case elfcpp::R_POWERPC_GLOB_DAT
:
1016 case elfcpp::R_POWERPC_DTPMOD
:
1017 case elfcpp::R_POWERPC_DTPREL
:
1018 case elfcpp::R_POWERPC_TPREL
:
1019 case elfcpp::R_POWERPC_JMP_SLOT
:
1020 case elfcpp::R_POWERPC_COPY
:
1021 case elfcpp::R_POWERPC_ADDR32
:
1022 case elfcpp::R_POWERPC_ADDR24
:
1023 case elfcpp::R_POWERPC_REL24
:
1034 // These are the relocation types supported only on 64-bit.
1035 case elfcpp::R_PPC64_ADDR64
:
1036 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1037 case elfcpp::R_PPC64_TPREL16_DS
:
1038 case elfcpp::R_POWERPC_TPREL16
:
1039 case elfcpp::R_POWERPC_TPREL16_LO
:
1040 case elfcpp::R_POWERPC_TPREL16_HI
:
1041 case elfcpp::R_POWERPC_TPREL16_HA
:
1042 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1043 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1044 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1045 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1046 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1047 case elfcpp::R_POWERPC_ADDR16_LO
:
1048 case elfcpp::R_POWERPC_ADDR16_HI
:
1049 case elfcpp::R_POWERPC_ADDR16_HA
:
1050 case elfcpp::R_POWERPC_ADDR30
:
1051 case elfcpp::R_PPC64_UADDR64
:
1052 case elfcpp::R_POWERPC_UADDR32
:
1053 case elfcpp::R_POWERPC_ADDR16
:
1054 case elfcpp::R_POWERPC_UADDR16
:
1055 case elfcpp::R_PPC64_ADDR16_DS
:
1056 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1057 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1058 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1059 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1060 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1061 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1062 case elfcpp::R_POWERPC_REL32
:
1063 case elfcpp::R_PPC64_REL64
:
1074 // These are the relocation types supported only on 32-bit.
1081 // This prevents us from issuing more than one error per reloc
1082 // section. But we can still wind up issuing more than one
1083 // error per object file.
1084 if (this->issued_non_pic_error_
)
1086 object
->error(_("requires unsupported dynamic reloc; "
1087 "recompile with -fPIC"));
1088 this->issued_non_pic_error_
= true;
1092 // Scan a relocation for a local symbol.
1094 template<int size
, bool big_endian
>
1096 Target_powerpc
<size
, big_endian
>::Scan::local(
1097 const General_options
&,
1098 Symbol_table
* symtab
,
1100 Target_powerpc
<size
, big_endian
>* target
,
1101 Sized_relobj
<size
, big_endian
>* object
,
1102 unsigned int data_shndx
,
1103 Output_section
* output_section
,
1104 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1105 unsigned int r_type
,
1106 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1110 case elfcpp::R_POWERPC_NONE
:
1111 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1112 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1115 case elfcpp::R_PPC64_ADDR64
:
1116 case elfcpp::R_POWERPC_ADDR32
:
1117 case elfcpp::R_POWERPC_ADDR16_HA
:
1118 case elfcpp::R_POWERPC_ADDR16_LO
:
1119 // If building a shared library (or a position-independent
1120 // executable), we need to create a dynamic relocation for
1122 if (parameters
->options().output_is_position_independent())
1124 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1126 check_non_pic(object
, r_type
);
1127 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1129 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1130 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1131 data_shndx
, reloc
.get_r_offset(),
1132 reloc
.get_r_addend());
1136 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1137 gold_assert(lsym
.get_st_value() == 0);
1138 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1139 output_section
, data_shndx
,
1140 reloc
.get_r_offset(),
1141 reloc
.get_r_addend());
1146 case elfcpp::R_POWERPC_REL24
:
1147 case elfcpp::R_PPC_LOCAL24PC
:
1148 case elfcpp::R_POWERPC_REL32
:
1149 case elfcpp::R_PPC_REL16_LO
:
1150 case elfcpp::R_PPC_REL16_HA
:
1153 case elfcpp::R_POWERPC_GOT16
:
1154 case elfcpp::R_POWERPC_GOT16_LO
:
1155 case elfcpp::R_POWERPC_GOT16_HI
:
1156 case elfcpp::R_POWERPC_GOT16_HA
:
1157 case elfcpp::R_PPC64_TOC16
:
1158 case elfcpp::R_PPC64_TOC16_LO
:
1159 case elfcpp::R_PPC64_TOC16_HI
:
1160 case elfcpp::R_PPC64_TOC16_HA
:
1161 case elfcpp::R_PPC64_TOC16_DS
:
1162 case elfcpp::R_PPC64_TOC16_LO_DS
:
1164 // The symbol requires a GOT entry.
1165 Output_data_got
<size
, big_endian
>* got
;
1168 got
= target
->got_section(symtab
, layout
);
1169 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1171 // If we are generating a shared object, we need to add a
1172 // dynamic relocation for this symbol's GOT entry.
1173 if (parameters
->options().output_is_position_independent())
1175 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1177 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1180 off
= got
->add_constant(0);
1181 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1182 rela_dyn
->add_local_relative(object
, r_sym
,
1183 elfcpp::R_POWERPC_RELATIVE
,
1188 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1192 case elfcpp::R_PPC64_TOC
:
1193 // We need a GOT section.
1194 target
->got_section(symtab
, layout
);
1197 // These are relocations which should only be seen by the
1198 // dynamic linker, and should never be seen here.
1199 case elfcpp::R_POWERPC_COPY
:
1200 case elfcpp::R_POWERPC_GLOB_DAT
:
1201 case elfcpp::R_POWERPC_JMP_SLOT
:
1202 case elfcpp::R_POWERPC_RELATIVE
:
1203 case elfcpp::R_POWERPC_DTPMOD
:
1204 gold_error(_("%s: unexpected reloc %u in object file"),
1205 object
->name().c_str(), r_type
);
1209 unsupported_reloc_local(object
, r_type
);
1214 // Report an unsupported relocation against a global symbol.
1216 template<int size
, bool big_endian
>
1218 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1219 Sized_relobj
<size
, big_endian
>* object
,
1220 unsigned int r_type
,
1223 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1224 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1227 // Scan a relocation for a global symbol.
1229 template<int size
, bool big_endian
>
1231 Target_powerpc
<size
, big_endian
>::Scan::global(
1232 const General_options
&,
1233 Symbol_table
* symtab
,
1235 Target_powerpc
<size
, big_endian
>* target
,
1236 Sized_relobj
<size
, big_endian
>* object
,
1237 unsigned int data_shndx
,
1238 Output_section
* output_section
,
1239 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1240 unsigned int r_type
,
1245 case elfcpp::R_POWERPC_NONE
:
1246 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1247 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1250 case elfcpp::R_PPC_PLTREL24
:
1251 // If the symbol is fully resolved, this is just a PC32 reloc.
1252 // Otherwise we need a PLT entry.
1253 if (gsym
->final_value_is_known())
1255 // If building a shared library, we can also skip the PLT entry
1256 // if the symbol is defined in the output file and is protected
1258 if (gsym
->is_defined()
1259 && !gsym
->is_from_dynobj()
1260 && !gsym
->is_preemptible())
1262 target
->make_plt_entry(symtab
, layout
, gsym
);
1265 case elfcpp::R_POWERPC_ADDR16
:
1266 case elfcpp::R_POWERPC_ADDR16_LO
:
1267 case elfcpp::R_POWERPC_ADDR16_HI
:
1268 case elfcpp::R_POWERPC_ADDR16_HA
:
1269 case elfcpp::R_POWERPC_ADDR32
:
1270 case elfcpp::R_PPC64_ADDR64
:
1272 // Make a PLT entry if necessary.
1273 if (gsym
->needs_plt_entry())
1275 target
->make_plt_entry(symtab
, layout
, gsym
);
1276 // Since this is not a PC-relative relocation, we may be
1277 // taking the address of a function. In that case we need to
1278 // set the entry in the dynamic symbol table to the address of
1280 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1281 gsym
->set_needs_dynsym_value();
1283 // Make a dynamic relocation if necessary.
1284 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1286 if (target
->may_need_copy_reloc(gsym
))
1288 target
->copy_reloc(symtab
, layout
, object
,
1289 data_shndx
, output_section
, gsym
, reloc
);
1291 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1292 || r_type
== elfcpp::R_PPC64_ADDR64
)
1293 && gsym
->can_use_relative_reloc(false))
1295 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1296 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1297 output_section
, object
,
1298 data_shndx
, reloc
.get_r_offset(),
1299 reloc
.get_r_addend());
1303 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1305 check_non_pic(object
, r_type
);
1306 if (gsym
->is_from_dynobj()
1307 || gsym
->is_undefined()
1308 || gsym
->is_preemptible())
1309 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1311 reloc
.get_r_offset(),
1312 reloc
.get_r_addend());
1314 rela_dyn
->add_global_relative(gsym
, r_type
,
1315 output_section
, object
,
1317 reloc
.get_r_offset(),
1318 reloc
.get_r_addend());
1324 case elfcpp::R_POWERPC_REL24
:
1325 case elfcpp::R_PPC_LOCAL24PC
:
1326 case elfcpp::R_PPC_REL16
:
1327 case elfcpp::R_PPC_REL16_LO
:
1328 case elfcpp::R_PPC_REL16_HI
:
1329 case elfcpp::R_PPC_REL16_HA
:
1331 if (gsym
->needs_plt_entry())
1332 target
->make_plt_entry(symtab
, layout
, gsym
);
1333 // Make a dynamic relocation if necessary.
1334 int flags
= Symbol::NON_PIC_REF
;
1335 if (gsym
->type() == elfcpp::STT_FUNC
)
1336 flags
|= Symbol::FUNCTION_CALL
;
1337 if (gsym
->needs_dynamic_reloc(flags
))
1339 if (target
->may_need_copy_reloc(gsym
))
1341 target
->copy_reloc(symtab
, layout
, object
,
1342 data_shndx
, output_section
, gsym
,
1347 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1348 check_non_pic(object
, r_type
);
1349 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1350 data_shndx
, reloc
.get_r_offset(),
1351 reloc
.get_r_addend());
1357 case elfcpp::R_POWERPC_GOT16
:
1358 case elfcpp::R_POWERPC_GOT16_LO
:
1359 case elfcpp::R_POWERPC_GOT16_HI
:
1360 case elfcpp::R_POWERPC_GOT16_HA
:
1361 case elfcpp::R_PPC64_TOC16
:
1362 case elfcpp::R_PPC64_TOC16_LO
:
1363 case elfcpp::R_PPC64_TOC16_HI
:
1364 case elfcpp::R_PPC64_TOC16_HA
:
1365 case elfcpp::R_PPC64_TOC16_DS
:
1366 case elfcpp::R_PPC64_TOC16_LO_DS
:
1368 // The symbol requires a GOT entry.
1369 Output_data_got
<size
, big_endian
>* got
;
1371 got
= target
->got_section(symtab
, layout
);
1372 if (gsym
->final_value_is_known())
1373 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1376 // If this symbol is not fully resolved, we need to add a
1377 // dynamic relocation for it.
1378 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1379 if (gsym
->is_from_dynobj()
1380 || gsym
->is_undefined()
1381 || gsym
->is_preemptible())
1382 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1383 elfcpp::R_POWERPC_GLOB_DAT
);
1384 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1386 unsigned int off
= got
->add_constant(0);
1388 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1389 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1396 case elfcpp::R_PPC64_TOC
:
1397 // We need a GOT section.
1398 target
->got_section(symtab
, layout
);
1401 case elfcpp::R_POWERPC_GOT_TPREL16
:
1402 case elfcpp::R_POWERPC_TLS
:
1406 // These are relocations which should only be seen by the
1407 // dynamic linker, and should never be seen here.
1408 case elfcpp::R_POWERPC_COPY
:
1409 case elfcpp::R_POWERPC_GLOB_DAT
:
1410 case elfcpp::R_POWERPC_JMP_SLOT
:
1411 case elfcpp::R_POWERPC_RELATIVE
:
1412 case elfcpp::R_POWERPC_DTPMOD
:
1413 gold_error(_("%s: unexpected reloc %u in object file"),
1414 object
->name().c_str(), r_type
);
1418 unsupported_reloc_global(object
, r_type
, gsym
);
1423 // Scan relocations for a section.
1425 template<int size
, bool big_endian
>
1427 Target_powerpc
<size
, big_endian
>::scan_relocs(
1428 const General_options
& options
,
1429 Symbol_table
* symtab
,
1431 Sized_relobj
<size
, big_endian
>* object
,
1432 unsigned int data_shndx
,
1433 unsigned int sh_type
,
1434 const unsigned char* prelocs
,
1436 Output_section
* output_section
,
1437 bool needs_special_offset_handling
,
1438 size_t local_symbol_count
,
1439 const unsigned char* plocal_symbols
)
1441 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1442 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1443 static Output_data_space
* sdata
;
1445 if (sh_type
== elfcpp::SHT_REL
)
1447 gold_error(_("%s: unsupported REL reloc section"),
1448 object
->name().c_str());
1452 // Define _SDA_BASE_ at the start of the .sdata section.
1455 // layout->find_output_section(".sdata") == NULL
1456 sdata
= new Output_data_space(4, "** sdata");
1457 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1459 | elfcpp::SHF_WRITE
,
1461 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1466 elfcpp::STV_HIDDEN
, 0,
1470 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1480 needs_special_offset_handling
,
1485 // Finalize the sections.
1487 template<int size
, bool big_endian
>
1489 Target_powerpc
<size
, big_endian
>::do_finalize_sections(Layout
* layout
)
1491 // Fill in some more dynamic tags.
1492 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1495 if (this->plt_
!= NULL
)
1497 const Output_data
* od
= this->plt_
->rel_plt();
1498 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1499 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1500 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1502 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1505 if (this->rela_dyn_
!= NULL
)
1507 const Output_data
* od
= this->rela_dyn_
;
1508 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1509 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1510 odyn
->add_constant(elfcpp::DT_RELAENT
,
1511 elfcpp::Elf_sizes
<size
>::rela_size
);
1514 if (!parameters
->options().shared())
1516 // The value of the DT_DEBUG tag is filled in by the dynamic
1517 // linker at run time, and used by the debugger.
1518 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1522 // Emit any relocs we saved in an attempt to avoid generating COPY
1524 if (this->copy_relocs_
.any_saved_relocs())
1525 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1528 // Perform a relocation.
1530 template<int size
, bool big_endian
>
1532 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1533 const Relocate_info
<size
, big_endian
>* relinfo
,
1534 Target_powerpc
* target
,
1536 const elfcpp::Rela
<size
, big_endian
>& rela
,
1537 unsigned int r_type
,
1538 const Sized_symbol
<size
>* gsym
,
1539 const Symbol_value
<size
>* psymval
,
1540 unsigned char* view
,
1541 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1542 section_size_type
/* view_size */)
1544 const unsigned int toc_base_offset
= 0x8000;
1545 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1547 // Pick the value to use for symbols defined in shared objects.
1548 Symbol_value
<size
> symval
;
1550 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1551 || r_type
== elfcpp::R_PPC_LOCAL24PC
1552 || r_type
== elfcpp::R_PPC_REL16
1553 || r_type
== elfcpp::R_PPC_REL16_LO
1554 || r_type
== elfcpp::R_PPC_REL16_HI
1555 || r_type
== elfcpp::R_PPC_REL16_HA
))
1557 elfcpp::Elf_Xword value
;
1559 value
= target
->plt_section()->address() + gsym
->plt_offset();
1561 symval
.set_output_value(value
);
1566 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1567 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1569 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1570 // pointer points to the beginning, not the end, of the table.
1571 // So we just use the plain offset.
1572 bool have_got_offset
= false;
1573 unsigned int got_offset
= 0;
1574 unsigned int got2_offset
= 0;
1577 case elfcpp::R_PPC64_TOC16
:
1578 case elfcpp::R_PPC64_TOC16_LO
:
1579 case elfcpp::R_PPC64_TOC16_HI
:
1580 case elfcpp::R_PPC64_TOC16_HA
:
1581 case elfcpp::R_PPC64_TOC16_DS
:
1582 case elfcpp::R_PPC64_TOC16_LO_DS
:
1583 // Subtract the TOC base address.
1584 addend
-= target
->toc_section()->address() + toc_base_offset
;
1587 case elfcpp::R_POWERPC_GOT16
:
1588 case elfcpp::R_POWERPC_GOT16_LO
:
1589 case elfcpp::R_POWERPC_GOT16_HI
:
1590 case elfcpp::R_POWERPC_GOT16_HA
:
1591 case elfcpp::R_PPC64_GOT16_DS
:
1592 case elfcpp::R_PPC64_GOT16_LO_DS
:
1595 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1596 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1600 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1601 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1602 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1604 have_got_offset
= true;
1607 // R_PPC_PLTREL24 is rather special. If non-zero,
1608 // the addend specifies the GOT pointer offset within .got2.
1609 case elfcpp::R_PPC_PLTREL24
:
1610 if (addend
>= 32768)
1612 Output_data_space
* got2
;
1613 got2
= target
->got2_section();
1614 got2_offset
= got2
->offset();
1615 addend
+= got2_offset
;
1617 have_got_offset
= true;
1626 case elfcpp::R_POWERPC_NONE
:
1627 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1628 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1631 case elfcpp::R_POWERPC_REL32
:
1632 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1635 case elfcpp::R_POWERPC_REL24
:
1636 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1639 case elfcpp::R_POWERPC_REL14
:
1640 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1643 case elfcpp::R_PPC_PLTREL24
:
1644 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1647 case elfcpp::R_PPC_LOCAL24PC
:
1648 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1651 case elfcpp::R_PPC64_ADDR64
:
1652 if (!parameters
->options().output_is_position_independent())
1653 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1657 case elfcpp::R_POWERPC_ADDR32
:
1658 if (!parameters
->options().output_is_position_independent())
1659 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1663 case elfcpp::R_POWERPC_ADDR16_LO
:
1664 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1667 case elfcpp::R_POWERPC_ADDR16_HI
:
1668 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1671 case elfcpp::R_POWERPC_ADDR16_HA
:
1672 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1675 case elfcpp::R_PPC_REL16_LO
:
1676 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1679 case elfcpp::R_PPC_REL16_HI
:
1680 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1683 case elfcpp::R_PPC_REL16_HA
:
1684 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1687 case elfcpp::R_POWERPC_GOT16
:
1688 Reloc::addr16(view
, got_offset
, addend
);
1691 case elfcpp::R_POWERPC_GOT16_LO
:
1692 Reloc::addr16_lo(view
, got_offset
, addend
);
1695 case elfcpp::R_POWERPC_GOT16_HI
:
1696 Reloc::addr16_hi(view
, got_offset
, addend
);
1699 case elfcpp::R_POWERPC_GOT16_HA
:
1700 Reloc::addr16_ha(view
, got_offset
, addend
);
1703 case elfcpp::R_PPC64_TOC16
:
1704 Reloc::addr16(view
, got_offset
, addend
);
1707 case elfcpp::R_PPC64_TOC16_LO
:
1708 Reloc::addr16_lo(view
, got_offset
, addend
);
1711 case elfcpp::R_PPC64_TOC16_HI
:
1712 Reloc::addr16_hi(view
, got_offset
, addend
);
1715 case elfcpp::R_PPC64_TOC16_HA
:
1716 Reloc::addr16_ha(view
, got_offset
, addend
);
1719 case elfcpp::R_PPC64_TOC16_DS
:
1720 case elfcpp::R_PPC64_TOC16_LO_DS
:
1721 Reloc::addr16_ds(view
, got_offset
, addend
);
1724 case elfcpp::R_PPC64_TOC
:
1726 elfcpp::Elf_types
<64>::Elf_Addr value
;
1727 value
= target
->toc_section()->address() + toc_base_offset
;
1728 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1732 case elfcpp::R_POWERPC_COPY
:
1733 case elfcpp::R_POWERPC_GLOB_DAT
:
1734 case elfcpp::R_POWERPC_JMP_SLOT
:
1735 case elfcpp::R_POWERPC_RELATIVE
:
1736 // This is an outstanding tls reloc, which is unexpected when
1738 case elfcpp::R_POWERPC_DTPMOD
:
1739 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1740 _("unexpected reloc %u in object file"),
1745 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1746 _("unsupported reloc %u"),
1754 // Perform a TLS relocation.
1756 template<int size
, bool big_endian
>
1758 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1759 const Relocate_info
<size
, big_endian
>* relinfo
,
1760 Target_powerpc
<size
, big_endian
>* target
,
1762 const elfcpp::Rela
<size
, big_endian
>& rela
,
1763 unsigned int r_type
,
1764 const Sized_symbol
<size
>* gsym
,
1765 const Symbol_value
<size
>* psymval
,
1766 unsigned char* view
,
1767 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1770 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1771 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1772 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1774 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1775 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1777 const bool is_final
=
1779 ? !parameters
->options().output_is_position_independent()
1780 : gsym
->final_value_is_known());
1781 const tls::Tls_optimization optimized_type
1782 = optimize_tls_reloc(is_final
, r_type
);
1790 // Relocate section data.
1792 template<int size
, bool big_endian
>
1794 Target_powerpc
<size
, big_endian
>::relocate_section(
1795 const Relocate_info
<size
, big_endian
>* relinfo
,
1796 unsigned int sh_type
,
1797 const unsigned char* prelocs
,
1799 Output_section
* output_section
,
1800 bool needs_special_offset_handling
,
1801 unsigned char* view
,
1802 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1803 section_size_type view_size
)
1805 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1806 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1808 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1810 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1817 needs_special_offset_handling
,
1823 // Return the size of a relocation while scanning during a relocatable
1826 template<int size
, bool big_endian
>
1828 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1832 // We are always SHT_RELA, so we should never get here.
1837 // Scan the relocs during a relocatable link.
1839 template<int size
, bool big_endian
>
1841 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1842 const General_options
& options
,
1843 Symbol_table
* symtab
,
1845 Sized_relobj
<size
, big_endian
>* object
,
1846 unsigned int data_shndx
,
1847 unsigned int sh_type
,
1848 const unsigned char* prelocs
,
1850 Output_section
* output_section
,
1851 bool needs_special_offset_handling
,
1852 size_t local_symbol_count
,
1853 const unsigned char* plocal_symbols
,
1854 Relocatable_relocs
* rr
)
1856 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1858 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1859 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1861 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1862 Scan_relocatable_relocs
>(
1871 needs_special_offset_handling
,
1877 // Relocate a section during a relocatable link.
1879 template<int size
, bool big_endian
>
1881 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1882 const Relocate_info
<size
, big_endian
>* relinfo
,
1883 unsigned int sh_type
,
1884 const unsigned char* prelocs
,
1886 Output_section
* output_section
,
1887 off_t offset_in_output_section
,
1888 const Relocatable_relocs
* rr
,
1889 unsigned char* view
,
1890 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1891 section_size_type view_size
,
1892 unsigned char* reloc_view
,
1893 section_size_type reloc_view_size
)
1895 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1897 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1902 offset_in_output_section
,
1911 // Return the value to use for a dynamic which requires special
1912 // treatment. This is how we support equality comparisons of function
1913 // pointers across shared library boundaries, as described in the
1914 // processor specific ABI supplement.
1916 template<int size
, bool big_endian
>
1918 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1920 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1921 return this->plt_section()->address() + gsym
->plt_offset();
1924 // The selector for powerpc object files.
1926 template<int size
, bool big_endian
>
1927 class Target_selector_powerpc
: public Target_selector
1930 Target_selector_powerpc()
1931 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1933 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1934 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1937 Target
* instantiated_target_
;
1939 Target
* do_recognize(int machine
, int, int)
1944 if (machine
!= elfcpp::EM_PPC64
)
1949 if (machine
!= elfcpp::EM_PPC
)
1957 return do_instantiate_target();
1960 Target
* do_instantiate_target()
1962 if (this->instantiated_target_
== NULL
)
1963 this->instantiated_target_
= new Target_powerpc
<size
, big_endian
>();
1964 return this->instantiated_target_
;
1968 Target_selector_powerpc
<32, true> target_selector_ppc32
;
1969 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
1970 Target_selector_powerpc
<64, true> target_selector_ppc64
;
1971 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
1973 } // End anonymous namespace.