1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009 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 // Process the relocations to determine unreferenced sections for
65 // garbage collection.
67 gc_process_relocs(const General_options
& options
,
70 Sized_relobj
<size
, big_endian
>* object
,
71 unsigned int data_shndx
,
73 const unsigned char* prelocs
,
75 Output_section
* output_section
,
76 bool needs_special_offset_handling
,
77 size_t local_symbol_count
,
78 const unsigned char* plocal_symbols
);
80 // Scan the relocations to look for symbol adjustments.
82 scan_relocs(const General_options
& options
,
85 Sized_relobj
<size
, big_endian
>* object
,
86 unsigned int data_shndx
,
88 const unsigned char* prelocs
,
90 Output_section
* output_section
,
91 bool needs_special_offset_handling
,
92 size_t local_symbol_count
,
93 const unsigned char* plocal_symbols
);
94 // Finalize the sections.
96 do_finalize_sections(Layout
*);
98 // Return the value to use for a dynamic which requires special
101 do_dynsym_value(const Symbol
*) const;
103 // Relocate a section.
105 relocate_section(const Relocate_info
<size
, big_endian
>*,
106 unsigned int sh_type
,
107 const unsigned char* prelocs
,
109 Output_section
* output_section
,
110 bool needs_special_offset_handling
,
112 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
113 section_size_type view_size
);
115 // Scan the relocs during a relocatable link.
117 scan_relocatable_relocs(const General_options
& options
,
118 Symbol_table
* symtab
,
120 Sized_relobj
<size
, big_endian
>* object
,
121 unsigned int data_shndx
,
122 unsigned int sh_type
,
123 const unsigned char* prelocs
,
125 Output_section
* output_section
,
126 bool needs_special_offset_handling
,
127 size_t local_symbol_count
,
128 const unsigned char* plocal_symbols
,
129 Relocatable_relocs
*);
131 // Relocate a section during a relocatable link.
133 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
134 unsigned int sh_type
,
135 const unsigned char* prelocs
,
137 Output_section
* output_section
,
138 off_t offset_in_output_section
,
139 const Relocatable_relocs
*,
141 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
142 section_size_type view_size
,
143 unsigned char* reloc_view
,
144 section_size_type reloc_view_size
);
146 // Return whether SYM is defined by the ABI.
148 do_is_defined_by_abi(const Symbol
* sym
) const
150 return strcmp(sym
->name(), "___tls_get_addr") == 0;
153 // Return the size of the GOT section.
157 gold_assert(this->got_
!= NULL
);
158 return this->got_
->data_size();
163 // The class which scans relocations.
168 : issued_non_pic_error_(false)
172 local(const General_options
& options
, Symbol_table
* symtab
,
173 Layout
* layout
, Target_powerpc
* target
,
174 Sized_relobj
<size
, big_endian
>* object
,
175 unsigned int data_shndx
,
176 Output_section
* output_section
,
177 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
178 const elfcpp::Sym
<size
, big_endian
>& lsym
);
181 global(const General_options
& options
, Symbol_table
* symtab
,
182 Layout
* layout
, Target_powerpc
* target
,
183 Sized_relobj
<size
, big_endian
>* object
,
184 unsigned int data_shndx
,
185 Output_section
* output_section
,
186 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
191 unsupported_reloc_local(Sized_relobj
<size
, big_endian
>*,
192 unsigned int r_type
);
195 unsupported_reloc_global(Sized_relobj
<size
, big_endian
>*,
196 unsigned int r_type
, Symbol
*);
199 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
200 Target_powerpc
* target
);
203 check_non_pic(Relobj
*, unsigned int r_type
);
205 // Whether we have issued an error about a non-PIC compilation.
206 bool issued_non_pic_error_
;
209 // The class which implements relocation.
213 // Do a relocation. Return false if the caller should not issue
214 // any warnings about this relocation.
216 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
217 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
218 unsigned int r_type
, const Sized_symbol
<size
>*,
219 const Symbol_value
<size
>*,
221 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
225 // Do a TLS relocation.
227 relocate_tls(const Relocate_info
<size
, big_endian
>*,
228 Target_powerpc
* target
,
229 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
230 unsigned int r_type
, const Sized_symbol
<size
>*,
231 const Symbol_value
<size
>*,
233 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
237 // A class which returns the size required for a relocation type,
238 // used while scanning relocs during a relocatable link.
239 class Relocatable_size_for_reloc
243 get_size_for_reloc(unsigned int, Relobj
*);
246 // Get the GOT section, creating it if necessary.
247 Output_data_got
<size
, big_endian
>*
248 got_section(Symbol_table
*, Layout
*);
253 gold_assert (this->got2_
!= NULL
);
257 // Get the TOC section.
261 gold_assert (this->toc_
!= NULL
);
265 // Create a PLT entry for a global symbol.
267 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
269 // Create a GOT entry for the TLS module index.
271 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
272 Sized_relobj
<size
, big_endian
>* object
);
274 // Get the PLT section.
275 const Output_data_plt_powerpc
<size
, big_endian
>*
278 gold_assert(this->plt_
!= NULL
);
282 // Get the dynamic reloc section, creating it if necessary.
284 rela_dyn_section(Layout
*);
286 // Return true if the symbol may need a COPY relocation.
287 // References from an executable object to non-function symbols
288 // defined in a dynamic object may need a COPY relocation.
290 may_need_copy_reloc(Symbol
* gsym
)
292 return (!parameters
->options().shared()
293 && gsym
->is_from_dynobj()
294 && gsym
->type() != elfcpp::STT_FUNC
);
297 // Copy a relocation against a global symbol.
299 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
300 Sized_relobj
<size
, big_endian
>* object
,
301 unsigned int shndx
, Output_section
* output_section
,
302 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
304 this->copy_relocs_
.copy_reloc(symtab
, layout
,
305 symtab
->get_sized_symbol
<size
>(sym
),
306 object
, shndx
, output_section
,
307 reloc
, this->rela_dyn_section(layout
));
310 // Information about this specific target which we pass to the
311 // general Target structure.
312 static Target::Target_info powerpc_info
;
314 // The types of GOT entries needed for this platform.
317 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
318 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
319 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
323 Output_data_got
<size
, big_endian
>* got_
;
325 Output_data_space
* got2_
;
327 Output_data_space
* toc_
;
329 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
330 // The dynamic reloc section.
331 Reloc_section
* rela_dyn_
;
332 // Relocs saved to avoid a COPY reloc.
333 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
334 // Space for variables copied with a COPY reloc.
335 Output_data_space
* dynbss_
;
336 // Offset of the GOT entry for the TLS module index;
337 unsigned int got_mod_index_offset_
;
341 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
344 true, // 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
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)
358 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
361 false, // is_big_endian
362 elfcpp::EM_PPC
, // machine_code
363 false, // has_make_symbol
364 false, // has_resolve
365 false, // has_code_fill
366 true, // is_default_stack_executable
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 4 * 1024 // common_pagesize (overridable by -z common-page-size)
375 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
378 true, // 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
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)
392 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
395 false, // is_big_endian
396 elfcpp::EM_PPC64
, // machine_code
397 false, // has_make_symbol
398 false, // has_resolve
399 false, // has_code_fill
400 true, // is_default_stack_executable
402 "/usr/lib/ld.so.1", // dynamic_linker
403 0x10000000, // default_text_segment_address
404 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
405 8 * 1024 // common_pagesize (overridable by -z common-page-size)
408 template<int size
, bool big_endian
>
409 class Powerpc_relocate_functions
412 // Do a simple relocation with the addend in the relocation.
413 template<int valsize
>
415 rela(unsigned char* view
,
416 unsigned int right_shift
,
417 elfcpp::Elf_Xword dst_mask
,
418 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
419 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
421 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
422 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
423 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
424 Valtype reloc
= ((value
+ addend
) >> right_shift
);
429 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
432 // Do a simple relocation using a symbol value with the addend in
434 template<int valsize
>
436 rela(unsigned char* view
,
437 unsigned int right_shift
,
438 elfcpp::Elf_Xword dst_mask
,
439 const Sized_relobj
<size
, big_endian
>* object
,
440 const Symbol_value
<size
>* psymval
,
441 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
443 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
444 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
445 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
446 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
451 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
454 // Do a simple relocation using a symbol value with the addend in
455 // the relocation, unaligned.
456 template<int valsize
>
458 rela_ua(unsigned char* view
, unsigned int right_shift
,
459 elfcpp::Elf_Xword dst_mask
,
460 const Sized_relobj
<size
, big_endian
>* object
,
461 const Symbol_value
<size
>* psymval
,
462 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
464 typedef typename
elfcpp::Swap_unaligned
<valsize
,
465 big_endian
>::Valtype Valtype
;
466 unsigned char* wv
= view
;
467 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
468 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
473 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
476 // Do a simple PC relative relocation with a Symbol_value with the
477 // addend in the relocation.
478 template<int valsize
>
480 pcrela(unsigned char* view
, unsigned int right_shift
,
481 elfcpp::Elf_Xword dst_mask
,
482 const Sized_relobj
<size
, big_endian
>* object
,
483 const Symbol_value
<size
>* psymval
,
484 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
485 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
487 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
488 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
489 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
490 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
496 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
499 template<int valsize
>
501 pcrela_unaligned(unsigned char* view
,
502 const Sized_relobj
<size
, big_endian
>* object
,
503 const Symbol_value
<size
>* psymval
,
504 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
505 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
507 typedef typename
elfcpp::Swap_unaligned
<valsize
,
508 big_endian
>::Valtype Valtype
;
509 unsigned char* wv
= view
;
510 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
512 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
515 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
516 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
518 // R_POWERPC_REL32: (Symbol + Addend - Address)
520 rel32(unsigned char* view
,
521 const Sized_relobj
<size
, big_endian
>* object
,
522 const Symbol_value
<size
>* psymval
,
523 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
524 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
525 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
527 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
529 rel24(unsigned char* view
,
530 const Sized_relobj
<size
, big_endian
>* object
,
531 const Symbol_value
<size
>* psymval
,
532 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
533 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
535 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
536 psymval
, addend
, address
);
539 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
541 rel14(unsigned char* view
,
542 const Sized_relobj
<size
, big_endian
>* object
,
543 const Symbol_value
<size
>* psymval
,
544 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
545 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
547 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
548 psymval
, addend
, address
);
551 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
553 addr16(unsigned char* view
,
554 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
555 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
556 { This_reloc::rela16(view
, value
, addend
); }
559 addr16(unsigned char* view
,
560 const Sized_relobj
<size
, big_endian
>* object
,
561 const Symbol_value
<size
>* psymval
,
562 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
563 { This_reloc::rela16(view
, object
, psymval
, addend
); }
565 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
567 addr16_ds(unsigned char* view
,
568 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
569 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
571 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
574 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
576 addr16_lo(unsigned char* view
,
577 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
578 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
579 { This_reloc::rela16(view
, value
, addend
); }
582 addr16_lo(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
)
586 { This_reloc::rela16(view
, object
, psymval
, addend
); }
588 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
590 addr16_hi(unsigned char* view
,
591 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
592 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
594 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
598 addr16_hi(unsigned char* view
,
599 const Sized_relobj
<size
, big_endian
>* object
,
600 const Symbol_value
<size
>* psymval
,
601 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
603 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
606 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
607 // final value of the low 16 bits of the
608 // relocation is negative, add one.
610 addr16_ha(unsigned char* view
,
611 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
614 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
616 reloc
= value
+ addend
;
622 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
626 addr16_ha(unsigned char* view
,
627 const Sized_relobj
<size
, big_endian
>* object
,
628 const Symbol_value
<size
>* psymval
,
629 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
631 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
633 reloc
= psymval
->value(object
, addend
);
639 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
642 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
644 rel16(unsigned char* view
,
645 const Sized_relobj
<size
, big_endian
>* object
,
646 const Symbol_value
<size
>* psymval
,
647 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
648 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
649 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
651 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
653 rel16_lo(unsigned char* view
,
654 const Sized_relobj
<size
, big_endian
>* object
,
655 const Symbol_value
<size
>* psymval
,
656 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
657 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
658 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
660 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
662 rel16_hi(unsigned char* view
,
663 const Sized_relobj
<size
, big_endian
>* object
,
664 const Symbol_value
<size
>* psymval
,
665 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
666 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
668 This::template pcrela
<16>(view
, 16, 0xffff, object
,
669 psymval
, addend
, address
);
672 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
673 // final value of the low 16 bits of the
674 // relocation is negative, add one.
676 rel16_ha(unsigned char* view
,
677 const Sized_relobj
<size
, big_endian
>* object
,
678 const Symbol_value
<size
>* psymval
,
679 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
680 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
682 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
684 reloc
= (psymval
->value(object
, addend
) - address
);
689 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
693 // Get the GOT section, creating it if necessary.
695 template<int size
, bool big_endian
>
696 Output_data_got
<size
, big_endian
>*
697 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
700 if (this->got_
== NULL
)
702 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
704 this->got_
= new Output_data_got
<size
, big_endian
>();
706 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
707 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
710 // Create the GOT2 or TOC in the .got section.
713 this->got2_
= new Output_data_space(4, "** GOT2");
714 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
721 this->toc_
= new Output_data_space(8, "** TOC");
722 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
728 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
729 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
731 0, 0, elfcpp::STT_OBJECT
,
733 elfcpp::STV_HIDDEN
, 0,
740 // Get the dynamic reloc section, creating it if necessary.
742 template<int size
, bool big_endian
>
743 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
744 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
746 if (this->rela_dyn_
== NULL
)
748 gold_assert(layout
!= NULL
);
749 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
750 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
751 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
753 return this->rela_dyn_
;
756 // A class to handle the PLT data.
758 template<int size
, bool big_endian
>
759 class Output_data_plt_powerpc
: public Output_section_data
762 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
763 size
, big_endian
> Reloc_section
;
765 Output_data_plt_powerpc(Layout
*);
767 // Add an entry to the PLT.
768 void add_entry(Symbol
* gsym
);
770 // Return the .rela.plt section data.
771 const Reloc_section
* rel_plt() const
777 void do_adjust_output_section(Output_section
* os
);
780 // The size of an entry in the PLT.
781 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
783 // Set the final size.
785 set_final_data_size()
787 unsigned int full_count
= this->count_
+ 4;
789 this->set_data_size(full_count
* base_plt_entry_size
);
792 // Write out the PLT data.
794 do_write(Output_file
*);
796 // The reloc section.
798 // The number of PLT entries.
802 // Create the PLT section. The ordinary .got section is an argument,
803 // since we need to refer to the start.
805 template<int size
, bool big_endian
>
806 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
807 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
809 this->rel_
= new Reloc_section(false);
810 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
811 elfcpp::SHF_ALLOC
, this->rel_
);
814 template<int size
, bool big_endian
>
816 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
821 // Add an entry to the PLT.
823 template<int size
, bool big_endian
>
825 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
827 gold_assert(!gsym
->has_plt_offset());
828 unsigned int index
= this->count_
+ + 4;
829 section_offset_type plt_offset
;
832 plt_offset
= index
* base_plt_entry_size
;
836 gsym
->set_plt_offset(plt_offset
);
840 gsym
->set_needs_dynsym_entry();
841 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
845 static const unsigned int addis_11_11
= 0x3d6b0000;
846 static const unsigned int addis_11_30
= 0x3d7e0000;
847 static const unsigned int addis_12_12
= 0x3d8c0000;
848 static const unsigned int addi_11_11
= 0x396b0000;
849 static const unsigned int add_0_11_11
= 0x7c0b5a14;
850 static const unsigned int add_11_0_11
= 0x7d605a14;
851 static const unsigned int b
= 0x48000000;
852 static const unsigned int bcl_20_31
= 0x429f0005;
853 static const unsigned int bctr
= 0x4e800420;
854 static const unsigned int lis_11
= 0x3d600000;
855 static const unsigned int lis_12
= 0x3d800000;
856 static const unsigned int lwzu_0_12
= 0x840c0000;
857 static const unsigned int lwz_0_12
= 0x800c0000;
858 static const unsigned int lwz_11_11
= 0x816b0000;
859 static const unsigned int lwz_11_30
= 0x817e0000;
860 static const unsigned int lwz_12_12
= 0x818c0000;
861 static const unsigned int mflr_0
= 0x7c0802a6;
862 static const unsigned int mflr_12
= 0x7d8802a6;
863 static const unsigned int mtctr_0
= 0x7c0903a6;
864 static const unsigned int mtctr_11
= 0x7d6903a6;
865 static const unsigned int mtlr_0
= 0x7c0803a6;
866 static const unsigned int nop
= 0x60000000;
867 static const unsigned int sub_11_11_12
= 0x7d6c5850;
869 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
870 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
871 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
872 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
873 /* ld %r11,xxx+16@l(%r12) */
876 // Write out the PLT.
878 template<int size
, bool big_endian
>
880 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
882 const off_t offset
= this->offset();
883 const section_size_type oview_size
=
884 convert_to_section_size_type(this->data_size());
885 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
886 unsigned char* pov
= oview
;
888 memset(pov
, 0, base_plt_entry_size
* 4);
889 pov
+= base_plt_entry_size
* 4;
891 unsigned int plt_offset
= base_plt_entry_size
* 4;
892 const unsigned int count
= this->count_
;
896 for (unsigned int i
= 0; i
< count
; i
++)
902 for (unsigned int i
= 0; i
< count
; i
++)
904 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
905 lwz_11_30
+ plt_offset
);
906 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
907 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
908 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
909 pov
+= base_plt_entry_size
;
910 plt_offset
+= base_plt_entry_size
;
914 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
916 of
->write_output_view(offset
, oview_size
, oview
);
919 // Create a PLT entry for a global symbol.
921 template<int size
, bool big_endian
>
923 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
927 if (gsym
->has_plt_offset())
930 if (this->plt_
== NULL
)
932 // Create the GOT section first.
933 this->got_section(symtab
, layout
);
935 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
936 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
938 | elfcpp::SHF_EXECINSTR
939 | elfcpp::SHF_WRITE
),
942 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
943 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
945 0, 0, elfcpp::STT_OBJECT
,
947 elfcpp::STV_HIDDEN
, 0,
951 this->plt_
->add_entry(gsym
);
954 // Create a GOT entry for the TLS module index.
956 template<int size
, bool big_endian
>
958 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
960 Sized_relobj
<size
, big_endian
>* object
)
962 if (this->got_mod_index_offset_
== -1U)
964 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
965 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
966 Output_data_got
<size
, big_endian
>* got
;
967 unsigned int got_offset
;
969 got
= this->got_section(symtab
, layout
);
970 got_offset
= got
->add_constant(0);
971 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
973 got
->add_constant(0);
974 this->got_mod_index_offset_
= got_offset
;
976 return this->got_mod_index_offset_
;
979 // Optimize the TLS relocation type based on what we know about the
980 // symbol. IS_FINAL is true if the final address of this symbol is
981 // known at link time.
983 static tls::Tls_optimization
984 optimize_tls_reloc(bool /* is_final */, int r_type
)
986 // If we are generating a shared library, then we can't do anything
988 if (parameters
->options().shared())
989 return tls::TLSOPT_NONE
;
998 // Report an unsupported relocation against a local symbol.
1000 template<int size
, bool big_endian
>
1002 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1003 Sized_relobj
<size
, big_endian
>* object
,
1004 unsigned int r_type
)
1006 gold_error(_("%s: unsupported reloc %u against local symbol"),
1007 object
->name().c_str(), r_type
);
1010 // We are about to emit a dynamic relocation of type R_TYPE. If the
1011 // dynamic linker does not support it, issue an error.
1013 template<int size
, bool big_endian
>
1015 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1016 unsigned int r_type
)
1018 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1020 // These are the relocation types supported by glibc for both 32-bit
1021 // and 64-bit powerpc.
1024 case elfcpp::R_POWERPC_RELATIVE
:
1025 case elfcpp::R_POWERPC_GLOB_DAT
:
1026 case elfcpp::R_POWERPC_DTPMOD
:
1027 case elfcpp::R_POWERPC_DTPREL
:
1028 case elfcpp::R_POWERPC_TPREL
:
1029 case elfcpp::R_POWERPC_JMP_SLOT
:
1030 case elfcpp::R_POWERPC_COPY
:
1031 case elfcpp::R_POWERPC_ADDR32
:
1032 case elfcpp::R_POWERPC_ADDR24
:
1033 case elfcpp::R_POWERPC_REL24
:
1044 // These are the relocation types supported only on 64-bit.
1045 case elfcpp::R_PPC64_ADDR64
:
1046 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1047 case elfcpp::R_PPC64_TPREL16_DS
:
1048 case elfcpp::R_POWERPC_TPREL16
:
1049 case elfcpp::R_POWERPC_TPREL16_LO
:
1050 case elfcpp::R_POWERPC_TPREL16_HI
:
1051 case elfcpp::R_POWERPC_TPREL16_HA
:
1052 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1053 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1054 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1055 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1056 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1057 case elfcpp::R_POWERPC_ADDR16_LO
:
1058 case elfcpp::R_POWERPC_ADDR16_HI
:
1059 case elfcpp::R_POWERPC_ADDR16_HA
:
1060 case elfcpp::R_POWERPC_ADDR30
:
1061 case elfcpp::R_PPC64_UADDR64
:
1062 case elfcpp::R_POWERPC_UADDR32
:
1063 case elfcpp::R_POWERPC_ADDR16
:
1064 case elfcpp::R_POWERPC_UADDR16
:
1065 case elfcpp::R_PPC64_ADDR16_DS
:
1066 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1067 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1068 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1069 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1070 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1071 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1072 case elfcpp::R_POWERPC_REL32
:
1073 case elfcpp::R_PPC64_REL64
:
1084 // These are the relocation types supported only on 32-bit.
1091 // This prevents us from issuing more than one error per reloc
1092 // section. But we can still wind up issuing more than one
1093 // error per object file.
1094 if (this->issued_non_pic_error_
)
1096 object
->error(_("requires unsupported dynamic reloc; "
1097 "recompile with -fPIC"));
1098 this->issued_non_pic_error_
= true;
1102 // Scan a relocation for a local symbol.
1104 template<int size
, bool big_endian
>
1106 Target_powerpc
<size
, big_endian
>::Scan::local(
1107 const General_options
&,
1108 Symbol_table
* symtab
,
1110 Target_powerpc
<size
, big_endian
>* target
,
1111 Sized_relobj
<size
, big_endian
>* object
,
1112 unsigned int data_shndx
,
1113 Output_section
* output_section
,
1114 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1115 unsigned int r_type
,
1116 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1120 case elfcpp::R_POWERPC_NONE
:
1121 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1122 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1125 case elfcpp::R_PPC64_ADDR64
:
1126 case elfcpp::R_POWERPC_ADDR32
:
1127 case elfcpp::R_POWERPC_ADDR16_HA
:
1128 case elfcpp::R_POWERPC_ADDR16_LO
:
1129 // If building a shared library (or a position-independent
1130 // executable), we need to create a dynamic relocation for
1132 if (parameters
->options().output_is_position_independent())
1134 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1136 check_non_pic(object
, r_type
);
1137 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1139 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1140 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1141 data_shndx
, reloc
.get_r_offset(),
1142 reloc
.get_r_addend());
1146 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1147 gold_assert(lsym
.get_st_value() == 0);
1148 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1149 output_section
, data_shndx
,
1150 reloc
.get_r_offset(),
1151 reloc
.get_r_addend());
1156 case elfcpp::R_POWERPC_REL24
:
1157 case elfcpp::R_PPC_LOCAL24PC
:
1158 case elfcpp::R_POWERPC_REL32
:
1159 case elfcpp::R_PPC_REL16_LO
:
1160 case elfcpp::R_PPC_REL16_HA
:
1163 case elfcpp::R_POWERPC_GOT16
:
1164 case elfcpp::R_POWERPC_GOT16_LO
:
1165 case elfcpp::R_POWERPC_GOT16_HI
:
1166 case elfcpp::R_POWERPC_GOT16_HA
:
1167 case elfcpp::R_PPC64_TOC16
:
1168 case elfcpp::R_PPC64_TOC16_LO
:
1169 case elfcpp::R_PPC64_TOC16_HI
:
1170 case elfcpp::R_PPC64_TOC16_HA
:
1171 case elfcpp::R_PPC64_TOC16_DS
:
1172 case elfcpp::R_PPC64_TOC16_LO_DS
:
1174 // The symbol requires a GOT entry.
1175 Output_data_got
<size
, big_endian
>* got
;
1178 got
= target
->got_section(symtab
, layout
);
1179 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1181 // If we are generating a shared object, we need to add a
1182 // dynamic relocation for this symbol's GOT entry.
1183 if (parameters
->options().output_is_position_independent())
1185 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1187 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1190 off
= got
->add_constant(0);
1191 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1192 rela_dyn
->add_local_relative(object
, r_sym
,
1193 elfcpp::R_POWERPC_RELATIVE
,
1198 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1202 case elfcpp::R_PPC64_TOC
:
1203 // We need a GOT section.
1204 target
->got_section(symtab
, layout
);
1207 // These are relocations which should only be seen by the
1208 // dynamic linker, and should never be seen here.
1209 case elfcpp::R_POWERPC_COPY
:
1210 case elfcpp::R_POWERPC_GLOB_DAT
:
1211 case elfcpp::R_POWERPC_JMP_SLOT
:
1212 case elfcpp::R_POWERPC_RELATIVE
:
1213 case elfcpp::R_POWERPC_DTPMOD
:
1214 gold_error(_("%s: unexpected reloc %u in object file"),
1215 object
->name().c_str(), r_type
);
1219 unsupported_reloc_local(object
, r_type
);
1224 // Report an unsupported relocation against a global symbol.
1226 template<int size
, bool big_endian
>
1228 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1229 Sized_relobj
<size
, big_endian
>* object
,
1230 unsigned int r_type
,
1233 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1234 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1237 // Scan a relocation for a global symbol.
1239 template<int size
, bool big_endian
>
1241 Target_powerpc
<size
, big_endian
>::Scan::global(
1242 const General_options
&,
1243 Symbol_table
* symtab
,
1245 Target_powerpc
<size
, big_endian
>* target
,
1246 Sized_relobj
<size
, big_endian
>* object
,
1247 unsigned int data_shndx
,
1248 Output_section
* output_section
,
1249 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1250 unsigned int r_type
,
1255 case elfcpp::R_POWERPC_NONE
:
1256 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1257 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1260 case elfcpp::R_PPC_PLTREL24
:
1261 // If the symbol is fully resolved, this is just a PC32 reloc.
1262 // Otherwise we need a PLT entry.
1263 if (gsym
->final_value_is_known())
1265 // If building a shared library, we can also skip the PLT entry
1266 // if the symbol is defined in the output file and is protected
1268 if (gsym
->is_defined()
1269 && !gsym
->is_from_dynobj()
1270 && !gsym
->is_preemptible())
1272 target
->make_plt_entry(symtab
, layout
, gsym
);
1275 case elfcpp::R_POWERPC_ADDR16
:
1276 case elfcpp::R_POWERPC_ADDR16_LO
:
1277 case elfcpp::R_POWERPC_ADDR16_HI
:
1278 case elfcpp::R_POWERPC_ADDR16_HA
:
1279 case elfcpp::R_POWERPC_ADDR32
:
1280 case elfcpp::R_PPC64_ADDR64
:
1282 // Make a PLT entry if necessary.
1283 if (gsym
->needs_plt_entry())
1285 target
->make_plt_entry(symtab
, layout
, gsym
);
1286 // Since this is not a PC-relative relocation, we may be
1287 // taking the address of a function. In that case we need to
1288 // set the entry in the dynamic symbol table to the address of
1290 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1291 gsym
->set_needs_dynsym_value();
1293 // Make a dynamic relocation if necessary.
1294 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1296 if (target
->may_need_copy_reloc(gsym
))
1298 target
->copy_reloc(symtab
, layout
, object
,
1299 data_shndx
, output_section
, gsym
, reloc
);
1301 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1302 || r_type
== elfcpp::R_PPC64_ADDR64
)
1303 && gsym
->can_use_relative_reloc(false))
1305 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1306 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1307 output_section
, object
,
1308 data_shndx
, reloc
.get_r_offset(),
1309 reloc
.get_r_addend());
1313 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1315 check_non_pic(object
, r_type
);
1316 if (gsym
->is_from_dynobj()
1317 || gsym
->is_undefined()
1318 || gsym
->is_preemptible())
1319 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1321 reloc
.get_r_offset(),
1322 reloc
.get_r_addend());
1324 rela_dyn
->add_global_relative(gsym
, r_type
,
1325 output_section
, object
,
1327 reloc
.get_r_offset(),
1328 reloc
.get_r_addend());
1334 case elfcpp::R_POWERPC_REL24
:
1335 case elfcpp::R_PPC_LOCAL24PC
:
1336 case elfcpp::R_PPC_REL16
:
1337 case elfcpp::R_PPC_REL16_LO
:
1338 case elfcpp::R_PPC_REL16_HI
:
1339 case elfcpp::R_PPC_REL16_HA
:
1341 if (gsym
->needs_plt_entry())
1342 target
->make_plt_entry(symtab
, layout
, gsym
);
1343 // Make a dynamic relocation if necessary.
1344 int flags
= Symbol::NON_PIC_REF
;
1345 if (gsym
->type() == elfcpp::STT_FUNC
)
1346 flags
|= Symbol::FUNCTION_CALL
;
1347 if (gsym
->needs_dynamic_reloc(flags
))
1349 if (target
->may_need_copy_reloc(gsym
))
1351 target
->copy_reloc(symtab
, layout
, object
,
1352 data_shndx
, output_section
, gsym
,
1357 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1358 check_non_pic(object
, r_type
);
1359 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1360 data_shndx
, reloc
.get_r_offset(),
1361 reloc
.get_r_addend());
1367 case elfcpp::R_POWERPC_GOT16
:
1368 case elfcpp::R_POWERPC_GOT16_LO
:
1369 case elfcpp::R_POWERPC_GOT16_HI
:
1370 case elfcpp::R_POWERPC_GOT16_HA
:
1371 case elfcpp::R_PPC64_TOC16
:
1372 case elfcpp::R_PPC64_TOC16_LO
:
1373 case elfcpp::R_PPC64_TOC16_HI
:
1374 case elfcpp::R_PPC64_TOC16_HA
:
1375 case elfcpp::R_PPC64_TOC16_DS
:
1376 case elfcpp::R_PPC64_TOC16_LO_DS
:
1378 // The symbol requires a GOT entry.
1379 Output_data_got
<size
, big_endian
>* got
;
1381 got
= target
->got_section(symtab
, layout
);
1382 if (gsym
->final_value_is_known())
1383 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1386 // If this symbol is not fully resolved, we need to add a
1387 // dynamic relocation for it.
1388 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1389 if (gsym
->is_from_dynobj()
1390 || gsym
->is_undefined()
1391 || gsym
->is_preemptible())
1392 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1393 elfcpp::R_POWERPC_GLOB_DAT
);
1394 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1396 unsigned int off
= got
->add_constant(0);
1398 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1399 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1406 case elfcpp::R_PPC64_TOC
:
1407 // We need a GOT section.
1408 target
->got_section(symtab
, layout
);
1411 case elfcpp::R_POWERPC_GOT_TPREL16
:
1412 case elfcpp::R_POWERPC_TLS
:
1416 // These are relocations which should only be seen by the
1417 // dynamic linker, and should never be seen here.
1418 case elfcpp::R_POWERPC_COPY
:
1419 case elfcpp::R_POWERPC_GLOB_DAT
:
1420 case elfcpp::R_POWERPC_JMP_SLOT
:
1421 case elfcpp::R_POWERPC_RELATIVE
:
1422 case elfcpp::R_POWERPC_DTPMOD
:
1423 gold_error(_("%s: unexpected reloc %u in object file"),
1424 object
->name().c_str(), r_type
);
1428 unsupported_reloc_global(object
, r_type
, gsym
);
1433 // Process relocations for gc.
1435 template<int size
, bool big_endian
>
1437 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1438 const General_options
& options
,
1439 Symbol_table
* symtab
,
1441 Sized_relobj
<size
, big_endian
>* object
,
1442 unsigned int data_shndx
,
1444 const unsigned char* prelocs
,
1446 Output_section
* output_section
,
1447 bool needs_special_offset_handling
,
1448 size_t local_symbol_count
,
1449 const unsigned char* plocal_symbols
)
1451 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1452 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1454 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1464 needs_special_offset_handling
,
1469 // Scan relocations for a section.
1471 template<int size
, bool big_endian
>
1473 Target_powerpc
<size
, big_endian
>::scan_relocs(
1474 const General_options
& options
,
1475 Symbol_table
* symtab
,
1477 Sized_relobj
<size
, big_endian
>* object
,
1478 unsigned int data_shndx
,
1479 unsigned int sh_type
,
1480 const unsigned char* prelocs
,
1482 Output_section
* output_section
,
1483 bool needs_special_offset_handling
,
1484 size_t local_symbol_count
,
1485 const unsigned char* plocal_symbols
)
1487 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1488 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1489 static Output_data_space
* sdata
;
1491 if (sh_type
== elfcpp::SHT_REL
)
1493 gold_error(_("%s: unsupported REL reloc section"),
1494 object
->name().c_str());
1498 // Define _SDA_BASE_ at the start of the .sdata section.
1501 // layout->find_output_section(".sdata") == NULL
1502 sdata
= new Output_data_space(4, "** sdata");
1503 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1505 | elfcpp::SHF_WRITE
,
1507 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1512 elfcpp::STV_HIDDEN
, 0,
1516 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1526 needs_special_offset_handling
,
1531 // Finalize the sections.
1533 template<int size
, bool big_endian
>
1535 Target_powerpc
<size
, big_endian
>::do_finalize_sections(Layout
* layout
)
1537 // Fill in some more dynamic tags.
1538 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1541 if (this->plt_
!= NULL
)
1543 const Output_data
* od
= this->plt_
->rel_plt();
1544 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1545 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1546 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1548 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1551 if (this->rela_dyn_
!= NULL
)
1553 const Output_data
* od
= this->rela_dyn_
;
1554 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1555 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1556 odyn
->add_constant(elfcpp::DT_RELAENT
,
1557 elfcpp::Elf_sizes
<size
>::rela_size
);
1560 if (!parameters
->options().shared())
1562 // The value of the DT_DEBUG tag is filled in by the dynamic
1563 // linker at run time, and used by the debugger.
1564 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1568 // Emit any relocs we saved in an attempt to avoid generating COPY
1570 if (this->copy_relocs_
.any_saved_relocs())
1571 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1574 // Perform a relocation.
1576 template<int size
, bool big_endian
>
1578 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1579 const Relocate_info
<size
, big_endian
>* relinfo
,
1580 Target_powerpc
* target
,
1582 const elfcpp::Rela
<size
, big_endian
>& rela
,
1583 unsigned int r_type
,
1584 const Sized_symbol
<size
>* gsym
,
1585 const Symbol_value
<size
>* psymval
,
1586 unsigned char* view
,
1587 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1588 section_size_type
/* view_size */)
1590 const unsigned int toc_base_offset
= 0x8000;
1591 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1593 // Pick the value to use for symbols defined in shared objects.
1594 Symbol_value
<size
> symval
;
1596 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1597 || r_type
== elfcpp::R_PPC_LOCAL24PC
1598 || r_type
== elfcpp::R_PPC_REL16
1599 || r_type
== elfcpp::R_PPC_REL16_LO
1600 || r_type
== elfcpp::R_PPC_REL16_HI
1601 || r_type
== elfcpp::R_PPC_REL16_HA
))
1603 elfcpp::Elf_Xword value
;
1605 value
= target
->plt_section()->address() + gsym
->plt_offset();
1607 symval
.set_output_value(value
);
1612 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1613 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1615 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1616 // pointer points to the beginning, not the end, of the table.
1617 // So we just use the plain offset.
1618 bool have_got_offset
= false;
1619 unsigned int got_offset
= 0;
1620 unsigned int got2_offset
= 0;
1623 case elfcpp::R_PPC64_TOC16
:
1624 case elfcpp::R_PPC64_TOC16_LO
:
1625 case elfcpp::R_PPC64_TOC16_HI
:
1626 case elfcpp::R_PPC64_TOC16_HA
:
1627 case elfcpp::R_PPC64_TOC16_DS
:
1628 case elfcpp::R_PPC64_TOC16_LO_DS
:
1629 // Subtract the TOC base address.
1630 addend
-= target
->toc_section()->address() + toc_base_offset
;
1633 case elfcpp::R_POWERPC_GOT16
:
1634 case elfcpp::R_POWERPC_GOT16_LO
:
1635 case elfcpp::R_POWERPC_GOT16_HI
:
1636 case elfcpp::R_POWERPC_GOT16_HA
:
1637 case elfcpp::R_PPC64_GOT16_DS
:
1638 case elfcpp::R_PPC64_GOT16_LO_DS
:
1641 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1642 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1646 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1647 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1648 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1650 have_got_offset
= true;
1653 // R_PPC_PLTREL24 is rather special. If non-zero,
1654 // the addend specifies the GOT pointer offset within .got2.
1655 case elfcpp::R_PPC_PLTREL24
:
1656 if (addend
>= 32768)
1658 Output_data_space
* got2
;
1659 got2
= target
->got2_section();
1660 got2_offset
= got2
->offset();
1661 addend
+= got2_offset
;
1663 have_got_offset
= true;
1672 case elfcpp::R_POWERPC_NONE
:
1673 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1674 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1677 case elfcpp::R_POWERPC_REL32
:
1678 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1681 case elfcpp::R_POWERPC_REL24
:
1682 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1685 case elfcpp::R_POWERPC_REL14
:
1686 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1689 case elfcpp::R_PPC_PLTREL24
:
1690 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1693 case elfcpp::R_PPC_LOCAL24PC
:
1694 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1697 case elfcpp::R_PPC64_ADDR64
:
1698 if (!parameters
->options().output_is_position_independent())
1699 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1703 case elfcpp::R_POWERPC_ADDR32
:
1704 if (!parameters
->options().output_is_position_independent())
1705 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1709 case elfcpp::R_POWERPC_ADDR16_LO
:
1710 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1713 case elfcpp::R_POWERPC_ADDR16_HI
:
1714 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1717 case elfcpp::R_POWERPC_ADDR16_HA
:
1718 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1721 case elfcpp::R_PPC_REL16_LO
:
1722 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1725 case elfcpp::R_PPC_REL16_HI
:
1726 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1729 case elfcpp::R_PPC_REL16_HA
:
1730 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1733 case elfcpp::R_POWERPC_GOT16
:
1734 Reloc::addr16(view
, got_offset
, addend
);
1737 case elfcpp::R_POWERPC_GOT16_LO
:
1738 Reloc::addr16_lo(view
, got_offset
, addend
);
1741 case elfcpp::R_POWERPC_GOT16_HI
:
1742 Reloc::addr16_hi(view
, got_offset
, addend
);
1745 case elfcpp::R_POWERPC_GOT16_HA
:
1746 Reloc::addr16_ha(view
, got_offset
, addend
);
1749 case elfcpp::R_PPC64_TOC16
:
1750 Reloc::addr16(view
, got_offset
, addend
);
1753 case elfcpp::R_PPC64_TOC16_LO
:
1754 Reloc::addr16_lo(view
, got_offset
, addend
);
1757 case elfcpp::R_PPC64_TOC16_HI
:
1758 Reloc::addr16_hi(view
, got_offset
, addend
);
1761 case elfcpp::R_PPC64_TOC16_HA
:
1762 Reloc::addr16_ha(view
, got_offset
, addend
);
1765 case elfcpp::R_PPC64_TOC16_DS
:
1766 case elfcpp::R_PPC64_TOC16_LO_DS
:
1767 Reloc::addr16_ds(view
, got_offset
, addend
);
1770 case elfcpp::R_PPC64_TOC
:
1772 elfcpp::Elf_types
<64>::Elf_Addr value
;
1773 value
= target
->toc_section()->address() + toc_base_offset
;
1774 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1778 case elfcpp::R_POWERPC_COPY
:
1779 case elfcpp::R_POWERPC_GLOB_DAT
:
1780 case elfcpp::R_POWERPC_JMP_SLOT
:
1781 case elfcpp::R_POWERPC_RELATIVE
:
1782 // This is an outstanding tls reloc, which is unexpected when
1784 case elfcpp::R_POWERPC_DTPMOD
:
1785 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1786 _("unexpected reloc %u in object file"),
1791 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1792 _("unsupported reloc %u"),
1800 // Perform a TLS relocation.
1802 template<int size
, bool big_endian
>
1804 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1805 const Relocate_info
<size
, big_endian
>* relinfo
,
1806 Target_powerpc
<size
, big_endian
>* target
,
1808 const elfcpp::Rela
<size
, big_endian
>& rela
,
1809 unsigned int r_type
,
1810 const Sized_symbol
<size
>* gsym
,
1811 const Symbol_value
<size
>* psymval
,
1812 unsigned char* view
,
1813 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1816 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1817 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1818 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1820 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1821 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1823 const bool is_final
=
1825 ? !parameters
->options().output_is_position_independent()
1826 : gsym
->final_value_is_known());
1827 const tls::Tls_optimization optimized_type
1828 = optimize_tls_reloc(is_final
, r_type
);
1836 // Relocate section data.
1838 template<int size
, bool big_endian
>
1840 Target_powerpc
<size
, big_endian
>::relocate_section(
1841 const Relocate_info
<size
, big_endian
>* relinfo
,
1842 unsigned int sh_type
,
1843 const unsigned char* prelocs
,
1845 Output_section
* output_section
,
1846 bool needs_special_offset_handling
,
1847 unsigned char* view
,
1848 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1849 section_size_type view_size
)
1851 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1852 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1854 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1856 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1863 needs_special_offset_handling
,
1869 // Return the size of a relocation while scanning during a relocatable
1872 template<int size
, bool big_endian
>
1874 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1878 // We are always SHT_RELA, so we should never get here.
1883 // Scan the relocs during a relocatable link.
1885 template<int size
, bool big_endian
>
1887 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1888 const General_options
& options
,
1889 Symbol_table
* symtab
,
1891 Sized_relobj
<size
, big_endian
>* object
,
1892 unsigned int data_shndx
,
1893 unsigned int sh_type
,
1894 const unsigned char* prelocs
,
1896 Output_section
* output_section
,
1897 bool needs_special_offset_handling
,
1898 size_t local_symbol_count
,
1899 const unsigned char* plocal_symbols
,
1900 Relocatable_relocs
* rr
)
1902 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1904 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1905 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1907 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1908 Scan_relocatable_relocs
>(
1917 needs_special_offset_handling
,
1923 // Relocate a section during a relocatable link.
1925 template<int size
, bool big_endian
>
1927 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1928 const Relocate_info
<size
, big_endian
>* relinfo
,
1929 unsigned int sh_type
,
1930 const unsigned char* prelocs
,
1932 Output_section
* output_section
,
1933 off_t offset_in_output_section
,
1934 const Relocatable_relocs
* rr
,
1935 unsigned char* view
,
1936 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1937 section_size_type view_size
,
1938 unsigned char* reloc_view
,
1939 section_size_type reloc_view_size
)
1941 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1943 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1948 offset_in_output_section
,
1957 // Return the value to use for a dynamic which requires special
1958 // treatment. This is how we support equality comparisons of function
1959 // pointers across shared library boundaries, as described in the
1960 // processor specific ABI supplement.
1962 template<int size
, bool big_endian
>
1964 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1966 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1967 return this->plt_section()->address() + gsym
->plt_offset();
1970 // The selector for powerpc object files.
1972 template<int size
, bool big_endian
>
1973 class Target_selector_powerpc
: public Target_selector
1976 Target_selector_powerpc()
1977 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1979 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1980 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1983 Target
* instantiated_target_
;
1985 Target
* do_recognize(int machine
, int, int)
1990 if (machine
!= elfcpp::EM_PPC64
)
1995 if (machine
!= elfcpp::EM_PPC
)
2003 return do_instantiate_target();
2006 Target
* do_instantiate_target()
2008 if (this->instantiated_target_
== NULL
)
2009 this->instantiated_target_
= new Target_powerpc
<size
, big_endian
>();
2010 return this->instantiated_target_
;
2014 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2015 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2016 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2017 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2019 } // End anonymous namespace.