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 Output_section
*, size_t relnum
,
218 const elfcpp::Rela
<size
, big_endian
>&,
219 unsigned int r_type
, const Sized_symbol
<size
>*,
220 const Symbol_value
<size
>*,
222 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
226 // Do a TLS relocation.
228 relocate_tls(const Relocate_info
<size
, big_endian
>*,
229 Target_powerpc
* target
,
230 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
231 unsigned int r_type
, const Sized_symbol
<size
>*,
232 const Symbol_value
<size
>*,
234 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
238 // A class which returns the size required for a relocation type,
239 // used while scanning relocs during a relocatable link.
240 class Relocatable_size_for_reloc
244 get_size_for_reloc(unsigned int, Relobj
*);
247 // Get the GOT section, creating it if necessary.
248 Output_data_got
<size
, big_endian
>*
249 got_section(Symbol_table
*, Layout
*);
254 gold_assert (this->got2_
!= NULL
);
258 // Get the TOC section.
262 gold_assert (this->toc_
!= NULL
);
266 // Create a PLT entry for a global symbol.
268 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
270 // Create a GOT entry for the TLS module index.
272 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
273 Sized_relobj
<size
, big_endian
>* object
);
275 // Get the PLT section.
276 const Output_data_plt_powerpc
<size
, big_endian
>*
279 gold_assert(this->plt_
!= NULL
);
283 // Get the dynamic reloc section, creating it if necessary.
285 rela_dyn_section(Layout
*);
287 // Return true if the symbol may need a COPY relocation.
288 // References from an executable object to non-function symbols
289 // defined in a dynamic object may need a COPY relocation.
291 may_need_copy_reloc(Symbol
* gsym
)
293 return (!parameters
->options().shared()
294 && gsym
->is_from_dynobj()
295 && gsym
->type() != elfcpp::STT_FUNC
);
298 // Copy a relocation against a global symbol.
300 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
301 Sized_relobj
<size
, big_endian
>* object
,
302 unsigned int shndx
, Output_section
* output_section
,
303 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
305 this->copy_relocs_
.copy_reloc(symtab
, layout
,
306 symtab
->get_sized_symbol
<size
>(sym
),
307 object
, shndx
, output_section
,
308 reloc
, this->rela_dyn_section(layout
));
311 // Information about this specific target which we pass to the
312 // general Target structure.
313 static Target::Target_info powerpc_info
;
315 // The types of GOT entries needed for this platform.
318 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
319 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
320 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
324 Output_data_got
<size
, big_endian
>* got_
;
326 Output_data_space
* got2_
;
328 Output_data_space
* toc_
;
330 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
331 // The dynamic reloc section.
332 Reloc_section
* rela_dyn_
;
333 // Relocs saved to avoid a COPY reloc.
334 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
335 // Space for variables copied with a COPY reloc.
336 Output_data_space
* dynbss_
;
337 // Offset of the GOT entry for the TLS module index;
338 unsigned int got_mod_index_offset_
;
342 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
345 true, // 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
<32, false>::powerpc_info
=
362 false, // is_big_endian
363 elfcpp::EM_PPC
, // 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 4 * 1024 // common_pagesize (overridable by -z common-page-size)
376 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
379 true, // 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)
393 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
396 false, // is_big_endian
397 elfcpp::EM_PPC64
, // machine_code
398 false, // has_make_symbol
399 false, // has_resolve
400 false, // has_code_fill
401 true, // is_default_stack_executable
403 "/usr/lib/ld.so.1", // dynamic_linker
404 0x10000000, // default_text_segment_address
405 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
406 8 * 1024 // common_pagesize (overridable by -z common-page-size)
409 template<int size
, bool big_endian
>
410 class Powerpc_relocate_functions
413 // Do a simple relocation with the addend in the relocation.
414 template<int valsize
>
416 rela(unsigned char* view
,
417 unsigned int right_shift
,
418 elfcpp::Elf_Xword dst_mask
,
419 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
420 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
422 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
423 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
424 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
425 Valtype reloc
= ((value
+ addend
) >> right_shift
);
430 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
433 // Do a simple relocation using a symbol value with the addend in
435 template<int valsize
>
437 rela(unsigned char* view
,
438 unsigned int right_shift
,
439 elfcpp::Elf_Xword dst_mask
,
440 const Sized_relobj
<size
, big_endian
>* object
,
441 const Symbol_value
<size
>* psymval
,
442 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
444 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
445 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
446 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
447 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
452 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
455 // Do a simple relocation using a symbol value with the addend in
456 // the relocation, unaligned.
457 template<int valsize
>
459 rela_ua(unsigned char* view
, unsigned int right_shift
,
460 elfcpp::Elf_Xword dst_mask
,
461 const Sized_relobj
<size
, big_endian
>* object
,
462 const Symbol_value
<size
>* psymval
,
463 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
465 typedef typename
elfcpp::Swap_unaligned
<valsize
,
466 big_endian
>::Valtype Valtype
;
467 unsigned char* wv
= view
;
468 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
469 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
474 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
477 // Do a simple PC relative relocation with a Symbol_value with the
478 // addend in the relocation.
479 template<int valsize
>
481 pcrela(unsigned char* view
, unsigned int right_shift
,
482 elfcpp::Elf_Xword dst_mask
,
483 const Sized_relobj
<size
, big_endian
>* object
,
484 const Symbol_value
<size
>* psymval
,
485 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
486 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
488 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
489 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
490 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
491 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
497 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
500 template<int valsize
>
502 pcrela_unaligned(unsigned char* view
,
503 const Sized_relobj
<size
, big_endian
>* object
,
504 const Symbol_value
<size
>* psymval
,
505 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
506 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
508 typedef typename
elfcpp::Swap_unaligned
<valsize
,
509 big_endian
>::Valtype Valtype
;
510 unsigned char* wv
= view
;
511 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
513 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
516 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
517 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
519 // R_POWERPC_REL32: (Symbol + Addend - Address)
521 rel32(unsigned char* view
,
522 const Sized_relobj
<size
, big_endian
>* object
,
523 const Symbol_value
<size
>* psymval
,
524 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
525 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
526 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
528 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
530 rel24(unsigned char* view
,
531 const Sized_relobj
<size
, big_endian
>* object
,
532 const Symbol_value
<size
>* psymval
,
533 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
534 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
536 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
537 psymval
, addend
, address
);
540 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
542 rel14(unsigned char* view
,
543 const Sized_relobj
<size
, big_endian
>* object
,
544 const Symbol_value
<size
>* psymval
,
545 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
546 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
548 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
549 psymval
, addend
, address
);
552 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
554 addr16(unsigned char* view
,
555 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
556 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
557 { This_reloc::rela16(view
, value
, addend
); }
560 addr16(unsigned char* view
,
561 const Sized_relobj
<size
, big_endian
>* object
,
562 const Symbol_value
<size
>* psymval
,
563 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
564 { This_reloc::rela16(view
, object
, psymval
, addend
); }
566 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
568 addr16_ds(unsigned char* view
,
569 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
570 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
572 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
575 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
577 addr16_lo(unsigned char* view
,
578 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
579 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
580 { This_reloc::rela16(view
, value
, addend
); }
583 addr16_lo(unsigned char* view
,
584 const Sized_relobj
<size
, big_endian
>* object
,
585 const Symbol_value
<size
>* psymval
,
586 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
587 { This_reloc::rela16(view
, object
, psymval
, addend
); }
589 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
591 addr16_hi(unsigned char* view
,
592 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
593 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
595 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
599 addr16_hi(unsigned char* view
,
600 const Sized_relobj
<size
, big_endian
>* object
,
601 const Symbol_value
<size
>* psymval
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
604 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
607 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
608 // final value of the low 16 bits of the
609 // relocation is negative, add one.
611 addr16_ha(unsigned char* view
,
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
613 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
615 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
617 reloc
= value
+ addend
;
623 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
627 addr16_ha(unsigned char* view
,
628 const Sized_relobj
<size
, big_endian
>* object
,
629 const Symbol_value
<size
>* psymval
,
630 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
632 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
634 reloc
= psymval
->value(object
, addend
);
640 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
643 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
645 rel16(unsigned char* view
,
646 const Sized_relobj
<size
, big_endian
>* object
,
647 const Symbol_value
<size
>* psymval
,
648 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
649 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
650 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
652 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
654 rel16_lo(unsigned char* view
,
655 const Sized_relobj
<size
, big_endian
>* object
,
656 const Symbol_value
<size
>* psymval
,
657 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
658 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
659 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
661 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
663 rel16_hi(unsigned char* view
,
664 const Sized_relobj
<size
, big_endian
>* object
,
665 const Symbol_value
<size
>* psymval
,
666 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
667 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
669 This::template pcrela
<16>(view
, 16, 0xffff, object
,
670 psymval
, addend
, address
);
673 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
674 // final value of the low 16 bits of the
675 // relocation is negative, add one.
677 rel16_ha(unsigned char* view
,
678 const Sized_relobj
<size
, big_endian
>* object
,
679 const Symbol_value
<size
>* psymval
,
680 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
681 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
683 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
685 reloc
= (psymval
->value(object
, addend
) - address
);
690 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
694 // Get the GOT section, creating it if necessary.
696 template<int size
, bool big_endian
>
697 Output_data_got
<size
, big_endian
>*
698 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
701 if (this->got_
== NULL
)
703 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
705 this->got_
= new Output_data_got
<size
, big_endian
>();
707 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
708 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
711 // Create the GOT2 or TOC in the .got section.
714 this->got2_
= new Output_data_space(4, "** GOT2");
715 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
722 this->toc_
= new Output_data_space(8, "** TOC");
723 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
729 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
730 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
732 0, 0, elfcpp::STT_OBJECT
,
734 elfcpp::STV_HIDDEN
, 0,
741 // Get the dynamic reloc section, creating it if necessary.
743 template<int size
, bool big_endian
>
744 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
745 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
747 if (this->rela_dyn_
== NULL
)
749 gold_assert(layout
!= NULL
);
750 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
751 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
752 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
754 return this->rela_dyn_
;
757 // A class to handle the PLT data.
759 template<int size
, bool big_endian
>
760 class Output_data_plt_powerpc
: public Output_section_data
763 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
764 size
, big_endian
> Reloc_section
;
766 Output_data_plt_powerpc(Layout
*);
768 // Add an entry to the PLT.
769 void add_entry(Symbol
* gsym
);
771 // Return the .rela.plt section data.
772 const Reloc_section
* rel_plt() const
778 void do_adjust_output_section(Output_section
* os
);
781 // The size of an entry in the PLT.
782 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
784 // Set the final size.
786 set_final_data_size()
788 unsigned int full_count
= this->count_
+ 4;
790 this->set_data_size(full_count
* base_plt_entry_size
);
793 // Write out the PLT data.
795 do_write(Output_file
*);
797 // The reloc section.
799 // The number of PLT entries.
803 // Create the PLT section. The ordinary .got section is an argument,
804 // since we need to refer to the start.
806 template<int size
, bool big_endian
>
807 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
808 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
810 this->rel_
= new Reloc_section(false);
811 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
812 elfcpp::SHF_ALLOC
, this->rel_
);
815 template<int size
, bool big_endian
>
817 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
822 // Add an entry to the PLT.
824 template<int size
, bool big_endian
>
826 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
828 gold_assert(!gsym
->has_plt_offset());
829 unsigned int index
= this->count_
+ + 4;
830 section_offset_type plt_offset
;
833 plt_offset
= index
* base_plt_entry_size
;
837 gsym
->set_plt_offset(plt_offset
);
841 gsym
->set_needs_dynsym_entry();
842 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
846 static const unsigned int addis_11_11
= 0x3d6b0000;
847 static const unsigned int addis_11_30
= 0x3d7e0000;
848 static const unsigned int addis_12_12
= 0x3d8c0000;
849 static const unsigned int addi_11_11
= 0x396b0000;
850 static const unsigned int add_0_11_11
= 0x7c0b5a14;
851 static const unsigned int add_11_0_11
= 0x7d605a14;
852 static const unsigned int b
= 0x48000000;
853 static const unsigned int bcl_20_31
= 0x429f0005;
854 static const unsigned int bctr
= 0x4e800420;
855 static const unsigned int lis_11
= 0x3d600000;
856 static const unsigned int lis_12
= 0x3d800000;
857 static const unsigned int lwzu_0_12
= 0x840c0000;
858 static const unsigned int lwz_0_12
= 0x800c0000;
859 static const unsigned int lwz_11_11
= 0x816b0000;
860 static const unsigned int lwz_11_30
= 0x817e0000;
861 static const unsigned int lwz_12_12
= 0x818c0000;
862 static const unsigned int mflr_0
= 0x7c0802a6;
863 static const unsigned int mflr_12
= 0x7d8802a6;
864 static const unsigned int mtctr_0
= 0x7c0903a6;
865 static const unsigned int mtctr_11
= 0x7d6903a6;
866 static const unsigned int mtlr_0
= 0x7c0803a6;
867 static const unsigned int nop
= 0x60000000;
868 static const unsigned int sub_11_11_12
= 0x7d6c5850;
870 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
871 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
872 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
873 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
874 /* ld %r11,xxx+16@l(%r12) */
877 // Write out the PLT.
879 template<int size
, bool big_endian
>
881 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
883 const off_t offset
= this->offset();
884 const section_size_type oview_size
=
885 convert_to_section_size_type(this->data_size());
886 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
887 unsigned char* pov
= oview
;
889 memset(pov
, 0, base_plt_entry_size
* 4);
890 pov
+= base_plt_entry_size
* 4;
892 unsigned int plt_offset
= base_plt_entry_size
* 4;
893 const unsigned int count
= this->count_
;
897 for (unsigned int i
= 0; i
< count
; i
++)
903 for (unsigned int i
= 0; i
< count
; i
++)
905 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
906 lwz_11_30
+ plt_offset
);
907 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
908 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
909 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
910 pov
+= base_plt_entry_size
;
911 plt_offset
+= base_plt_entry_size
;
915 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
917 of
->write_output_view(offset
, oview_size
, oview
);
920 // Create a PLT entry for a global symbol.
922 template<int size
, bool big_endian
>
924 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
928 if (gsym
->has_plt_offset())
931 if (this->plt_
== NULL
)
933 // Create the GOT section first.
934 this->got_section(symtab
, layout
);
936 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
937 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
939 | elfcpp::SHF_EXECINSTR
940 | elfcpp::SHF_WRITE
),
943 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
944 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
946 0, 0, elfcpp::STT_OBJECT
,
948 elfcpp::STV_HIDDEN
, 0,
952 this->plt_
->add_entry(gsym
);
955 // Create a GOT entry for the TLS module index.
957 template<int size
, bool big_endian
>
959 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
961 Sized_relobj
<size
, big_endian
>* object
)
963 if (this->got_mod_index_offset_
== -1U)
965 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
966 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
967 Output_data_got
<size
, big_endian
>* got
;
968 unsigned int got_offset
;
970 got
= this->got_section(symtab
, layout
);
971 got_offset
= got
->add_constant(0);
972 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
974 got
->add_constant(0);
975 this->got_mod_index_offset_
= got_offset
;
977 return this->got_mod_index_offset_
;
980 // Optimize the TLS relocation type based on what we know about the
981 // symbol. IS_FINAL is true if the final address of this symbol is
982 // known at link time.
984 static tls::Tls_optimization
985 optimize_tls_reloc(bool /* is_final */, int r_type
)
987 // If we are generating a shared library, then we can't do anything
989 if (parameters
->options().shared())
990 return tls::TLSOPT_NONE
;
999 // Report an unsupported relocation against a local symbol.
1001 template<int size
, bool big_endian
>
1003 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1004 Sized_relobj
<size
, big_endian
>* object
,
1005 unsigned int r_type
)
1007 gold_error(_("%s: unsupported reloc %u against local symbol"),
1008 object
->name().c_str(), r_type
);
1011 // We are about to emit a dynamic relocation of type R_TYPE. If the
1012 // dynamic linker does not support it, issue an error.
1014 template<int size
, bool big_endian
>
1016 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1017 unsigned int r_type
)
1019 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1021 // These are the relocation types supported by glibc for both 32-bit
1022 // and 64-bit powerpc.
1025 case elfcpp::R_POWERPC_RELATIVE
:
1026 case elfcpp::R_POWERPC_GLOB_DAT
:
1027 case elfcpp::R_POWERPC_DTPMOD
:
1028 case elfcpp::R_POWERPC_DTPREL
:
1029 case elfcpp::R_POWERPC_TPREL
:
1030 case elfcpp::R_POWERPC_JMP_SLOT
:
1031 case elfcpp::R_POWERPC_COPY
:
1032 case elfcpp::R_POWERPC_ADDR32
:
1033 case elfcpp::R_POWERPC_ADDR24
:
1034 case elfcpp::R_POWERPC_REL24
:
1045 // These are the relocation types supported only on 64-bit.
1046 case elfcpp::R_PPC64_ADDR64
:
1047 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1048 case elfcpp::R_PPC64_TPREL16_DS
:
1049 case elfcpp::R_POWERPC_TPREL16
:
1050 case elfcpp::R_POWERPC_TPREL16_LO
:
1051 case elfcpp::R_POWERPC_TPREL16_HI
:
1052 case elfcpp::R_POWERPC_TPREL16_HA
:
1053 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1054 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1055 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1056 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1057 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1058 case elfcpp::R_POWERPC_ADDR16_LO
:
1059 case elfcpp::R_POWERPC_ADDR16_HI
:
1060 case elfcpp::R_POWERPC_ADDR16_HA
:
1061 case elfcpp::R_POWERPC_ADDR30
:
1062 case elfcpp::R_PPC64_UADDR64
:
1063 case elfcpp::R_POWERPC_UADDR32
:
1064 case elfcpp::R_POWERPC_ADDR16
:
1065 case elfcpp::R_POWERPC_UADDR16
:
1066 case elfcpp::R_PPC64_ADDR16_DS
:
1067 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1068 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1069 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1070 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1071 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1072 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1073 case elfcpp::R_POWERPC_REL32
:
1074 case elfcpp::R_PPC64_REL64
:
1085 // These are the relocation types supported only on 32-bit.
1092 // This prevents us from issuing more than one error per reloc
1093 // section. But we can still wind up issuing more than one
1094 // error per object file.
1095 if (this->issued_non_pic_error_
)
1097 gold_assert(parameters
->options().output_is_position_independent());
1098 object
->error(_("requires unsupported dynamic reloc; "
1099 "recompile with -fPIC"));
1100 this->issued_non_pic_error_
= true;
1104 // Scan a relocation for a local symbol.
1106 template<int size
, bool big_endian
>
1108 Target_powerpc
<size
, big_endian
>::Scan::local(
1109 const General_options
&,
1110 Symbol_table
* symtab
,
1112 Target_powerpc
<size
, big_endian
>* target
,
1113 Sized_relobj
<size
, big_endian
>* object
,
1114 unsigned int data_shndx
,
1115 Output_section
* output_section
,
1116 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1117 unsigned int r_type
,
1118 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1122 case elfcpp::R_POWERPC_NONE
:
1123 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1124 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1127 case elfcpp::R_PPC64_ADDR64
:
1128 case elfcpp::R_POWERPC_ADDR32
:
1129 case elfcpp::R_POWERPC_ADDR16_HA
:
1130 case elfcpp::R_POWERPC_ADDR16_LO
:
1131 // If building a shared library (or a position-independent
1132 // executable), we need to create a dynamic relocation for
1134 if (parameters
->options().output_is_position_independent())
1136 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1138 check_non_pic(object
, r_type
);
1139 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1141 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1142 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1143 data_shndx
, reloc
.get_r_offset(),
1144 reloc
.get_r_addend());
1148 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1149 gold_assert(lsym
.get_st_value() == 0);
1150 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1151 output_section
, data_shndx
,
1152 reloc
.get_r_offset(),
1153 reloc
.get_r_addend());
1158 case elfcpp::R_POWERPC_REL24
:
1159 case elfcpp::R_PPC_LOCAL24PC
:
1160 case elfcpp::R_POWERPC_REL32
:
1161 case elfcpp::R_PPC_REL16_LO
:
1162 case elfcpp::R_PPC_REL16_HA
:
1165 case elfcpp::R_POWERPC_GOT16
:
1166 case elfcpp::R_POWERPC_GOT16_LO
:
1167 case elfcpp::R_POWERPC_GOT16_HI
:
1168 case elfcpp::R_POWERPC_GOT16_HA
:
1169 case elfcpp::R_PPC64_TOC16
:
1170 case elfcpp::R_PPC64_TOC16_LO
:
1171 case elfcpp::R_PPC64_TOC16_HI
:
1172 case elfcpp::R_PPC64_TOC16_HA
:
1173 case elfcpp::R_PPC64_TOC16_DS
:
1174 case elfcpp::R_PPC64_TOC16_LO_DS
:
1176 // The symbol requires a GOT entry.
1177 Output_data_got
<size
, big_endian
>* got
;
1180 got
= target
->got_section(symtab
, layout
);
1181 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1183 // If we are generating a shared object, we need to add a
1184 // dynamic relocation for this symbol's GOT entry.
1185 if (parameters
->options().output_is_position_independent())
1187 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1189 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1192 off
= got
->add_constant(0);
1193 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1194 rela_dyn
->add_local_relative(object
, r_sym
,
1195 elfcpp::R_POWERPC_RELATIVE
,
1200 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1204 case elfcpp::R_PPC64_TOC
:
1205 // We need a GOT section.
1206 target
->got_section(symtab
, layout
);
1209 // These are relocations which should only be seen by the
1210 // dynamic linker, and should never be seen here.
1211 case elfcpp::R_POWERPC_COPY
:
1212 case elfcpp::R_POWERPC_GLOB_DAT
:
1213 case elfcpp::R_POWERPC_JMP_SLOT
:
1214 case elfcpp::R_POWERPC_RELATIVE
:
1215 case elfcpp::R_POWERPC_DTPMOD
:
1216 gold_error(_("%s: unexpected reloc %u in object file"),
1217 object
->name().c_str(), r_type
);
1221 unsupported_reloc_local(object
, r_type
);
1226 // Report an unsupported relocation against a global symbol.
1228 template<int size
, bool big_endian
>
1230 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1231 Sized_relobj
<size
, big_endian
>* object
,
1232 unsigned int r_type
,
1235 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1236 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1239 // Scan a relocation for a global symbol.
1241 template<int size
, bool big_endian
>
1243 Target_powerpc
<size
, big_endian
>::Scan::global(
1244 const General_options
&,
1245 Symbol_table
* symtab
,
1247 Target_powerpc
<size
, big_endian
>* target
,
1248 Sized_relobj
<size
, big_endian
>* object
,
1249 unsigned int data_shndx
,
1250 Output_section
* output_section
,
1251 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1252 unsigned int r_type
,
1257 case elfcpp::R_POWERPC_NONE
:
1258 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1259 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1262 case elfcpp::R_PPC_PLTREL24
:
1263 // If the symbol is fully resolved, this is just a PC32 reloc.
1264 // Otherwise we need a PLT entry.
1265 if (gsym
->final_value_is_known())
1267 // If building a shared library, we can also skip the PLT entry
1268 // if the symbol is defined in the output file and is protected
1270 if (gsym
->is_defined()
1271 && !gsym
->is_from_dynobj()
1272 && !gsym
->is_preemptible())
1274 target
->make_plt_entry(symtab
, layout
, gsym
);
1277 case elfcpp::R_POWERPC_ADDR16
:
1278 case elfcpp::R_POWERPC_ADDR16_LO
:
1279 case elfcpp::R_POWERPC_ADDR16_HI
:
1280 case elfcpp::R_POWERPC_ADDR16_HA
:
1281 case elfcpp::R_POWERPC_ADDR32
:
1282 case elfcpp::R_PPC64_ADDR64
:
1284 // Make a PLT entry if necessary.
1285 if (gsym
->needs_plt_entry())
1287 target
->make_plt_entry(symtab
, layout
, gsym
);
1288 // Since this is not a PC-relative relocation, we may be
1289 // taking the address of a function. In that case we need to
1290 // set the entry in the dynamic symbol table to the address of
1292 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1293 gsym
->set_needs_dynsym_value();
1295 // Make a dynamic relocation if necessary.
1296 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1298 if (target
->may_need_copy_reloc(gsym
))
1300 target
->copy_reloc(symtab
, layout
, object
,
1301 data_shndx
, output_section
, gsym
, reloc
);
1303 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1304 || r_type
== elfcpp::R_PPC64_ADDR64
)
1305 && gsym
->can_use_relative_reloc(false))
1307 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1308 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1309 output_section
, object
,
1310 data_shndx
, reloc
.get_r_offset(),
1311 reloc
.get_r_addend());
1315 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1317 check_non_pic(object
, r_type
);
1318 if (gsym
->is_from_dynobj()
1319 || gsym
->is_undefined()
1320 || gsym
->is_preemptible())
1321 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1323 reloc
.get_r_offset(),
1324 reloc
.get_r_addend());
1326 rela_dyn
->add_global_relative(gsym
, r_type
,
1327 output_section
, object
,
1329 reloc
.get_r_offset(),
1330 reloc
.get_r_addend());
1336 case elfcpp::R_POWERPC_REL24
:
1337 case elfcpp::R_PPC_LOCAL24PC
:
1338 case elfcpp::R_PPC_REL16
:
1339 case elfcpp::R_PPC_REL16_LO
:
1340 case elfcpp::R_PPC_REL16_HI
:
1341 case elfcpp::R_PPC_REL16_HA
:
1343 if (gsym
->needs_plt_entry())
1344 target
->make_plt_entry(symtab
, layout
, gsym
);
1345 // Make a dynamic relocation if necessary.
1346 int flags
= Symbol::NON_PIC_REF
;
1347 if (gsym
->type() == elfcpp::STT_FUNC
)
1348 flags
|= Symbol::FUNCTION_CALL
;
1349 if (gsym
->needs_dynamic_reloc(flags
))
1351 if (target
->may_need_copy_reloc(gsym
))
1353 target
->copy_reloc(symtab
, layout
, object
,
1354 data_shndx
, output_section
, gsym
,
1359 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1360 check_non_pic(object
, r_type
);
1361 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1362 data_shndx
, reloc
.get_r_offset(),
1363 reloc
.get_r_addend());
1369 case elfcpp::R_POWERPC_GOT16
:
1370 case elfcpp::R_POWERPC_GOT16_LO
:
1371 case elfcpp::R_POWERPC_GOT16_HI
:
1372 case elfcpp::R_POWERPC_GOT16_HA
:
1373 case elfcpp::R_PPC64_TOC16
:
1374 case elfcpp::R_PPC64_TOC16_LO
:
1375 case elfcpp::R_PPC64_TOC16_HI
:
1376 case elfcpp::R_PPC64_TOC16_HA
:
1377 case elfcpp::R_PPC64_TOC16_DS
:
1378 case elfcpp::R_PPC64_TOC16_LO_DS
:
1380 // The symbol requires a GOT entry.
1381 Output_data_got
<size
, big_endian
>* got
;
1383 got
= target
->got_section(symtab
, layout
);
1384 if (gsym
->final_value_is_known())
1385 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1388 // If this symbol is not fully resolved, we need to add a
1389 // dynamic relocation for it.
1390 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1391 if (gsym
->is_from_dynobj()
1392 || gsym
->is_undefined()
1393 || gsym
->is_preemptible())
1394 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1395 elfcpp::R_POWERPC_GLOB_DAT
);
1396 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1398 unsigned int off
= got
->add_constant(0);
1400 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1401 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1408 case elfcpp::R_PPC64_TOC
:
1409 // We need a GOT section.
1410 target
->got_section(symtab
, layout
);
1413 case elfcpp::R_POWERPC_GOT_TPREL16
:
1414 case elfcpp::R_POWERPC_TLS
:
1418 // These are relocations which should only be seen by the
1419 // dynamic linker, and should never be seen here.
1420 case elfcpp::R_POWERPC_COPY
:
1421 case elfcpp::R_POWERPC_GLOB_DAT
:
1422 case elfcpp::R_POWERPC_JMP_SLOT
:
1423 case elfcpp::R_POWERPC_RELATIVE
:
1424 case elfcpp::R_POWERPC_DTPMOD
:
1425 gold_error(_("%s: unexpected reloc %u in object file"),
1426 object
->name().c_str(), r_type
);
1430 unsupported_reloc_global(object
, r_type
, gsym
);
1435 // Process relocations for gc.
1437 template<int size
, bool big_endian
>
1439 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1440 const General_options
& options
,
1441 Symbol_table
* symtab
,
1443 Sized_relobj
<size
, big_endian
>* object
,
1444 unsigned int data_shndx
,
1446 const unsigned char* prelocs
,
1448 Output_section
* output_section
,
1449 bool needs_special_offset_handling
,
1450 size_t local_symbol_count
,
1451 const unsigned char* plocal_symbols
)
1453 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1454 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1456 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1466 needs_special_offset_handling
,
1471 // Scan relocations for a section.
1473 template<int size
, bool big_endian
>
1475 Target_powerpc
<size
, big_endian
>::scan_relocs(
1476 const General_options
& options
,
1477 Symbol_table
* symtab
,
1479 Sized_relobj
<size
, big_endian
>* object
,
1480 unsigned int data_shndx
,
1481 unsigned int sh_type
,
1482 const unsigned char* prelocs
,
1484 Output_section
* output_section
,
1485 bool needs_special_offset_handling
,
1486 size_t local_symbol_count
,
1487 const unsigned char* plocal_symbols
)
1489 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1490 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1491 static Output_data_space
* sdata
;
1493 if (sh_type
== elfcpp::SHT_REL
)
1495 gold_error(_("%s: unsupported REL reloc section"),
1496 object
->name().c_str());
1500 // Define _SDA_BASE_ at the start of the .sdata section.
1503 // layout->find_output_section(".sdata") == NULL
1504 sdata
= new Output_data_space(4, "** sdata");
1505 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1507 | elfcpp::SHF_WRITE
,
1509 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1514 elfcpp::STV_HIDDEN
, 0,
1518 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1528 needs_special_offset_handling
,
1533 // Finalize the sections.
1535 template<int size
, bool big_endian
>
1537 Target_powerpc
<size
, big_endian
>::do_finalize_sections(Layout
* layout
)
1539 // Fill in some more dynamic tags.
1540 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1543 if (this->plt_
!= NULL
)
1545 const Output_data
* od
= this->plt_
->rel_plt();
1546 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1547 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1548 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1550 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1553 if (this->rela_dyn_
!= NULL
)
1555 const Output_data
* od
= this->rela_dyn_
;
1556 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1557 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1558 odyn
->add_constant(elfcpp::DT_RELAENT
,
1559 elfcpp::Elf_sizes
<size
>::rela_size
);
1562 if (!parameters
->options().shared())
1564 // The value of the DT_DEBUG tag is filled in by the dynamic
1565 // linker at run time, and used by the debugger.
1566 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1570 // Emit any relocs we saved in an attempt to avoid generating COPY
1572 if (this->copy_relocs_
.any_saved_relocs())
1573 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1576 // Perform a relocation.
1578 template<int size
, bool big_endian
>
1580 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1581 const Relocate_info
<size
, big_endian
>* relinfo
,
1582 Target_powerpc
* target
,
1585 const elfcpp::Rela
<size
, big_endian
>& rela
,
1586 unsigned int r_type
,
1587 const Sized_symbol
<size
>* gsym
,
1588 const Symbol_value
<size
>* psymval
,
1589 unsigned char* view
,
1590 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1591 section_size_type
/* view_size */)
1593 const unsigned int toc_base_offset
= 0x8000;
1594 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1596 // Pick the value to use for symbols defined in shared objects.
1597 Symbol_value
<size
> symval
;
1599 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1600 || r_type
== elfcpp::R_PPC_LOCAL24PC
1601 || r_type
== elfcpp::R_PPC_REL16
1602 || r_type
== elfcpp::R_PPC_REL16_LO
1603 || r_type
== elfcpp::R_PPC_REL16_HI
1604 || r_type
== elfcpp::R_PPC_REL16_HA
))
1606 elfcpp::Elf_Xword value
;
1608 value
= target
->plt_section()->address() + gsym
->plt_offset();
1610 symval
.set_output_value(value
);
1615 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1616 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1618 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1619 // pointer points to the beginning, not the end, of the table.
1620 // So we just use the plain offset.
1621 bool have_got_offset
= false;
1622 unsigned int got_offset
= 0;
1623 unsigned int got2_offset
= 0;
1626 case elfcpp::R_PPC64_TOC16
:
1627 case elfcpp::R_PPC64_TOC16_LO
:
1628 case elfcpp::R_PPC64_TOC16_HI
:
1629 case elfcpp::R_PPC64_TOC16_HA
:
1630 case elfcpp::R_PPC64_TOC16_DS
:
1631 case elfcpp::R_PPC64_TOC16_LO_DS
:
1632 // Subtract the TOC base address.
1633 addend
-= target
->toc_section()->address() + toc_base_offset
;
1636 case elfcpp::R_POWERPC_GOT16
:
1637 case elfcpp::R_POWERPC_GOT16_LO
:
1638 case elfcpp::R_POWERPC_GOT16_HI
:
1639 case elfcpp::R_POWERPC_GOT16_HA
:
1640 case elfcpp::R_PPC64_GOT16_DS
:
1641 case elfcpp::R_PPC64_GOT16_LO_DS
:
1644 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1645 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1649 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1650 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1651 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1653 have_got_offset
= true;
1656 // R_PPC_PLTREL24 is rather special. If non-zero,
1657 // the addend specifies the GOT pointer offset within .got2.
1658 case elfcpp::R_PPC_PLTREL24
:
1659 if (addend
>= 32768)
1661 Output_data_space
* got2
;
1662 got2
= target
->got2_section();
1663 got2_offset
= got2
->offset();
1664 addend
+= got2_offset
;
1666 have_got_offset
= true;
1675 case elfcpp::R_POWERPC_NONE
:
1676 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1677 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1680 case elfcpp::R_POWERPC_REL32
:
1681 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1684 case elfcpp::R_POWERPC_REL24
:
1685 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1688 case elfcpp::R_POWERPC_REL14
:
1689 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1692 case elfcpp::R_PPC_PLTREL24
:
1693 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1696 case elfcpp::R_PPC_LOCAL24PC
:
1697 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1700 case elfcpp::R_PPC64_ADDR64
:
1701 if (!parameters
->options().output_is_position_independent())
1702 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1706 case elfcpp::R_POWERPC_ADDR32
:
1707 if (!parameters
->options().output_is_position_independent())
1708 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1712 case elfcpp::R_POWERPC_ADDR16_LO
:
1713 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1716 case elfcpp::R_POWERPC_ADDR16_HI
:
1717 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1720 case elfcpp::R_POWERPC_ADDR16_HA
:
1721 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1724 case elfcpp::R_PPC_REL16_LO
:
1725 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1728 case elfcpp::R_PPC_REL16_HI
:
1729 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1732 case elfcpp::R_PPC_REL16_HA
:
1733 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1736 case elfcpp::R_POWERPC_GOT16
:
1737 Reloc::addr16(view
, got_offset
, addend
);
1740 case elfcpp::R_POWERPC_GOT16_LO
:
1741 Reloc::addr16_lo(view
, got_offset
, addend
);
1744 case elfcpp::R_POWERPC_GOT16_HI
:
1745 Reloc::addr16_hi(view
, got_offset
, addend
);
1748 case elfcpp::R_POWERPC_GOT16_HA
:
1749 Reloc::addr16_ha(view
, got_offset
, addend
);
1752 case elfcpp::R_PPC64_TOC16
:
1753 Reloc::addr16(view
, got_offset
, addend
);
1756 case elfcpp::R_PPC64_TOC16_LO
:
1757 Reloc::addr16_lo(view
, got_offset
, addend
);
1760 case elfcpp::R_PPC64_TOC16_HI
:
1761 Reloc::addr16_hi(view
, got_offset
, addend
);
1764 case elfcpp::R_PPC64_TOC16_HA
:
1765 Reloc::addr16_ha(view
, got_offset
, addend
);
1768 case elfcpp::R_PPC64_TOC16_DS
:
1769 case elfcpp::R_PPC64_TOC16_LO_DS
:
1770 Reloc::addr16_ds(view
, got_offset
, addend
);
1773 case elfcpp::R_PPC64_TOC
:
1775 elfcpp::Elf_types
<64>::Elf_Addr value
;
1776 value
= target
->toc_section()->address() + toc_base_offset
;
1777 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1781 case elfcpp::R_POWERPC_COPY
:
1782 case elfcpp::R_POWERPC_GLOB_DAT
:
1783 case elfcpp::R_POWERPC_JMP_SLOT
:
1784 case elfcpp::R_POWERPC_RELATIVE
:
1785 // This is an outstanding tls reloc, which is unexpected when
1787 case elfcpp::R_POWERPC_DTPMOD
:
1788 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1789 _("unexpected reloc %u in object file"),
1794 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1795 _("unsupported reloc %u"),
1803 // Perform a TLS relocation.
1805 template<int size
, bool big_endian
>
1807 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1808 const Relocate_info
<size
, big_endian
>* relinfo
,
1809 Target_powerpc
<size
, big_endian
>* target
,
1811 const elfcpp::Rela
<size
, big_endian
>& rela
,
1812 unsigned int r_type
,
1813 const Sized_symbol
<size
>* gsym
,
1814 const Symbol_value
<size
>* psymval
,
1815 unsigned char* view
,
1816 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1819 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1820 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1821 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1823 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1824 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1826 const bool is_final
=
1828 ? !parameters
->options().output_is_position_independent()
1829 : gsym
->final_value_is_known());
1830 const tls::Tls_optimization optimized_type
1831 = optimize_tls_reloc(is_final
, r_type
);
1839 // Relocate section data.
1841 template<int size
, bool big_endian
>
1843 Target_powerpc
<size
, big_endian
>::relocate_section(
1844 const Relocate_info
<size
, big_endian
>* relinfo
,
1845 unsigned int sh_type
,
1846 const unsigned char* prelocs
,
1848 Output_section
* output_section
,
1849 bool needs_special_offset_handling
,
1850 unsigned char* view
,
1851 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1852 section_size_type view_size
)
1854 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1855 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1857 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1859 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1866 needs_special_offset_handling
,
1872 // Return the size of a relocation while scanning during a relocatable
1875 template<int size
, bool big_endian
>
1877 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1881 // We are always SHT_RELA, so we should never get here.
1886 // Scan the relocs during a relocatable link.
1888 template<int size
, bool big_endian
>
1890 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1891 const General_options
& options
,
1892 Symbol_table
* symtab
,
1894 Sized_relobj
<size
, big_endian
>* object
,
1895 unsigned int data_shndx
,
1896 unsigned int sh_type
,
1897 const unsigned char* prelocs
,
1899 Output_section
* output_section
,
1900 bool needs_special_offset_handling
,
1901 size_t local_symbol_count
,
1902 const unsigned char* plocal_symbols
,
1903 Relocatable_relocs
* rr
)
1905 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1907 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1908 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1910 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1911 Scan_relocatable_relocs
>(
1920 needs_special_offset_handling
,
1926 // Relocate a section during a relocatable link.
1928 template<int size
, bool big_endian
>
1930 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1931 const Relocate_info
<size
, big_endian
>* relinfo
,
1932 unsigned int sh_type
,
1933 const unsigned char* prelocs
,
1935 Output_section
* output_section
,
1936 off_t offset_in_output_section
,
1937 const Relocatable_relocs
* rr
,
1938 unsigned char* view
,
1939 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1940 section_size_type view_size
,
1941 unsigned char* reloc_view
,
1942 section_size_type reloc_view_size
)
1944 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1946 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1951 offset_in_output_section
,
1960 // Return the value to use for a dynamic which requires special
1961 // treatment. This is how we support equality comparisons of function
1962 // pointers across shared library boundaries, as described in the
1963 // processor specific ABI supplement.
1965 template<int size
, bool big_endian
>
1967 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1969 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1970 return this->plt_section()->address() + gsym
->plt_offset();
1973 // The selector for powerpc object files.
1975 template<int size
, bool big_endian
>
1976 class Target_selector_powerpc
: public Target_selector
1979 Target_selector_powerpc()
1980 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1982 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1983 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1986 Target
* do_recognize(int machine
, int, int)
1991 if (machine
!= elfcpp::EM_PPC64
)
1996 if (machine
!= elfcpp::EM_PPC
)
2004 return this->instantiate_target();
2007 Target
* do_instantiate_target()
2008 { return new Target_powerpc
<size
, big_endian
>(); }
2011 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2012 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2013 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2014 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2016 } // End anonymous namespace.