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
,
114 const Reloc_symbol_changes
*);
116 // Scan the relocs during a relocatable link.
118 scan_relocatable_relocs(const General_options
& options
,
119 Symbol_table
* symtab
,
121 Sized_relobj
<size
, big_endian
>* object
,
122 unsigned int data_shndx
,
123 unsigned int sh_type
,
124 const unsigned char* prelocs
,
126 Output_section
* output_section
,
127 bool needs_special_offset_handling
,
128 size_t local_symbol_count
,
129 const unsigned char* plocal_symbols
,
130 Relocatable_relocs
*);
132 // Relocate a section during a relocatable link.
134 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
135 unsigned int sh_type
,
136 const unsigned char* prelocs
,
138 Output_section
* output_section
,
139 off_t offset_in_output_section
,
140 const Relocatable_relocs
*,
142 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
143 section_size_type view_size
,
144 unsigned char* reloc_view
,
145 section_size_type reloc_view_size
);
147 // Return whether SYM is defined by the ABI.
149 do_is_defined_by_abi(const Symbol
* sym
) const
151 return strcmp(sym
->name(), "___tls_get_addr") == 0;
154 // Return the size of the GOT section.
158 gold_assert(this->got_
!= NULL
);
159 return this->got_
->data_size();
164 // The class which scans relocations.
169 : issued_non_pic_error_(false)
173 local(const General_options
& options
, Symbol_table
* symtab
,
174 Layout
* layout
, Target_powerpc
* target
,
175 Sized_relobj
<size
, big_endian
>* object
,
176 unsigned int data_shndx
,
177 Output_section
* output_section
,
178 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
179 const elfcpp::Sym
<size
, big_endian
>& lsym
);
182 global(const General_options
& options
, Symbol_table
* symtab
,
183 Layout
* layout
, Target_powerpc
* target
,
184 Sized_relobj
<size
, big_endian
>* object
,
185 unsigned int data_shndx
,
186 Output_section
* output_section
,
187 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
192 unsupported_reloc_local(Sized_relobj
<size
, big_endian
>*,
193 unsigned int r_type
);
196 unsupported_reloc_global(Sized_relobj
<size
, big_endian
>*,
197 unsigned int r_type
, Symbol
*);
200 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
201 Target_powerpc
* target
);
204 check_non_pic(Relobj
*, unsigned int r_type
);
206 // Whether we have issued an error about a non-PIC compilation.
207 bool issued_non_pic_error_
;
210 // The class which implements relocation.
214 // Do a relocation. Return false if the caller should not issue
215 // any warnings about this relocation.
217 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
218 Output_section
*, size_t relnum
,
219 const elfcpp::Rela
<size
, big_endian
>&,
220 unsigned int r_type
, const Sized_symbol
<size
>*,
221 const Symbol_value
<size
>*,
223 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
227 // Do a TLS relocation.
229 relocate_tls(const Relocate_info
<size
, big_endian
>*,
230 Target_powerpc
* target
,
231 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
232 unsigned int r_type
, const Sized_symbol
<size
>*,
233 const Symbol_value
<size
>*,
235 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
239 // A class which returns the size required for a relocation type,
240 // used while scanning relocs during a relocatable link.
241 class Relocatable_size_for_reloc
245 get_size_for_reloc(unsigned int, Relobj
*);
248 // Get the GOT section, creating it if necessary.
249 Output_data_got
<size
, big_endian
>*
250 got_section(Symbol_table
*, Layout
*);
255 gold_assert (this->got2_
!= NULL
);
259 // Get the TOC section.
263 gold_assert (this->toc_
!= NULL
);
267 // Create a PLT entry for a global symbol.
269 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
271 // Create a GOT entry for the TLS module index.
273 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
274 Sized_relobj
<size
, big_endian
>* object
);
276 // Get the PLT section.
277 const Output_data_plt_powerpc
<size
, big_endian
>*
280 gold_assert(this->plt_
!= NULL
);
284 // Get the dynamic reloc section, creating it if necessary.
286 rela_dyn_section(Layout
*);
288 // Copy a relocation against a global symbol.
290 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
291 Sized_relobj
<size
, big_endian
>* object
,
292 unsigned int shndx
, Output_section
* output_section
,
293 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
295 this->copy_relocs_
.copy_reloc(symtab
, layout
,
296 symtab
->get_sized_symbol
<size
>(sym
),
297 object
, shndx
, output_section
,
298 reloc
, this->rela_dyn_section(layout
));
301 // Information about this specific target which we pass to the
302 // general Target structure.
303 static Target::Target_info powerpc_info
;
305 // The types of GOT entries needed for this platform.
308 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
309 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
310 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
314 Output_data_got
<size
, big_endian
>* got_
;
316 Output_data_space
* got2_
;
318 Output_data_space
* toc_
;
320 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
321 // The dynamic reloc section.
322 Reloc_section
* rela_dyn_
;
323 // Relocs saved to avoid a COPY reloc.
324 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
325 // Space for variables copied with a COPY reloc.
326 Output_data_space
* dynbss_
;
327 // Offset of the GOT entry for the TLS module index;
328 unsigned int got_mod_index_offset_
;
332 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
335 true, // is_big_endian
336 elfcpp::EM_PPC
, // machine_code
337 false, // has_make_symbol
338 false, // has_resolve
339 false, // has_code_fill
340 true, // is_default_stack_executable
342 "/usr/lib/ld.so.1", // dynamic_linker
343 0x10000000, // default_text_segment_address
344 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
345 4 * 1024, // common_pagesize (overridable by -z common-page-size)
346 elfcpp::SHN_UNDEF
, // small_common_shndx
347 elfcpp::SHN_UNDEF
, // large_common_shndx
348 0, // small_common_section_flags
349 0 // large_common_section_flags
353 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
356 false, // is_big_endian
357 elfcpp::EM_PPC
, // machine_code
358 false, // has_make_symbol
359 false, // has_resolve
360 false, // has_code_fill
361 true, // is_default_stack_executable
363 "/usr/lib/ld.so.1", // dynamic_linker
364 0x10000000, // default_text_segment_address
365 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
366 4 * 1024, // common_pagesize (overridable by -z common-page-size)
367 elfcpp::SHN_UNDEF
, // small_common_shndx
368 elfcpp::SHN_UNDEF
, // large_common_shndx
369 0, // small_common_section_flags
370 0 // large_common_section_flags
374 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
377 true, // is_big_endian
378 elfcpp::EM_PPC64
, // machine_code
379 false, // has_make_symbol
380 false, // has_resolve
381 false, // has_code_fill
382 true, // is_default_stack_executable
384 "/usr/lib/ld.so.1", // dynamic_linker
385 0x10000000, // default_text_segment_address
386 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
387 8 * 1024, // common_pagesize (overridable by -z common-page-size)
388 elfcpp::SHN_UNDEF
, // small_common_shndx
389 elfcpp::SHN_UNDEF
, // large_common_shndx
390 0, // small_common_section_flags
391 0 // large_common_section_flags
395 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
398 false, // is_big_endian
399 elfcpp::EM_PPC64
, // machine_code
400 false, // has_make_symbol
401 false, // has_resolve
402 false, // has_code_fill
403 true, // is_default_stack_executable
405 "/usr/lib/ld.so.1", // dynamic_linker
406 0x10000000, // default_text_segment_address
407 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
408 8 * 1024, // common_pagesize (overridable by -z common-page-size)
409 elfcpp::SHN_UNDEF
, // small_common_shndx
410 elfcpp::SHN_UNDEF
, // large_common_shndx
411 0, // small_common_section_flags
412 0 // large_common_section_flags
415 template<int size
, bool big_endian
>
416 class Powerpc_relocate_functions
419 // Do a simple relocation with the addend in the relocation.
420 template<int valsize
>
422 rela(unsigned char* view
,
423 unsigned int right_shift
,
424 elfcpp::Elf_Xword dst_mask
,
425 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
426 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
428 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
429 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
430 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
431 Valtype reloc
= ((value
+ addend
) >> right_shift
);
436 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
439 // Do a simple relocation using a symbol value with the addend in
441 template<int valsize
>
443 rela(unsigned char* view
,
444 unsigned int right_shift
,
445 elfcpp::Elf_Xword dst_mask
,
446 const Sized_relobj
<size
, big_endian
>* object
,
447 const Symbol_value
<size
>* psymval
,
448 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
450 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
451 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
452 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
453 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
458 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
461 // Do a simple relocation using a symbol value with the addend in
462 // the relocation, unaligned.
463 template<int valsize
>
465 rela_ua(unsigned char* view
, unsigned int right_shift
,
466 elfcpp::Elf_Xword dst_mask
,
467 const Sized_relobj
<size
, big_endian
>* object
,
468 const Symbol_value
<size
>* psymval
,
469 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
471 typedef typename
elfcpp::Swap_unaligned
<valsize
,
472 big_endian
>::Valtype Valtype
;
473 unsigned char* wv
= view
;
474 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
475 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
480 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
483 // Do a simple PC relative relocation with a Symbol_value with the
484 // addend in the relocation.
485 template<int valsize
>
487 pcrela(unsigned char* view
, unsigned int right_shift
,
488 elfcpp::Elf_Xword dst_mask
,
489 const Sized_relobj
<size
, big_endian
>* object
,
490 const Symbol_value
<size
>* psymval
,
491 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
492 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
494 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
495 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
496 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
497 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
503 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
506 template<int valsize
>
508 pcrela_unaligned(unsigned char* view
,
509 const Sized_relobj
<size
, big_endian
>* object
,
510 const Symbol_value
<size
>* psymval
,
511 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
512 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
514 typedef typename
elfcpp::Swap_unaligned
<valsize
,
515 big_endian
>::Valtype Valtype
;
516 unsigned char* wv
= view
;
517 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
519 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
522 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
523 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
525 // R_POWERPC_REL32: (Symbol + Addend - Address)
527 rel32(unsigned char* view
,
528 const Sized_relobj
<size
, big_endian
>* object
,
529 const Symbol_value
<size
>* psymval
,
530 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
531 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
532 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
534 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
536 rel24(unsigned char* view
,
537 const Sized_relobj
<size
, big_endian
>* object
,
538 const Symbol_value
<size
>* psymval
,
539 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
540 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
542 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
543 psymval
, addend
, address
);
546 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
548 rel14(unsigned char* view
,
549 const Sized_relobj
<size
, big_endian
>* object
,
550 const Symbol_value
<size
>* psymval
,
551 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
552 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
554 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
555 psymval
, addend
, address
);
558 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
560 addr16(unsigned char* view
,
561 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
562 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
563 { This_reloc::rela16(view
, value
, addend
); }
566 addr16(unsigned char* view
,
567 const Sized_relobj
<size
, big_endian
>* object
,
568 const Symbol_value
<size
>* psymval
,
569 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
570 { This_reloc::rela16(view
, object
, psymval
, addend
); }
572 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
574 addr16_ds(unsigned char* view
,
575 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
576 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
578 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
581 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
583 addr16_lo(unsigned char* view
,
584 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
585 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
586 { This_reloc::rela16(view
, value
, addend
); }
589 addr16_lo(unsigned char* view
,
590 const Sized_relobj
<size
, big_endian
>* object
,
591 const Symbol_value
<size
>* psymval
,
592 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
593 { This_reloc::rela16(view
, object
, psymval
, addend
); }
595 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
597 addr16_hi(unsigned char* view
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
599 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
601 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
605 addr16_hi(unsigned char* view
,
606 const Sized_relobj
<size
, big_endian
>* object
,
607 const Symbol_value
<size
>* psymval
,
608 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
610 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
613 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
614 // final value of the low 16 bits of the
615 // relocation is negative, add one.
617 addr16_ha(unsigned char* view
,
618 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
619 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
621 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
623 reloc
= value
+ addend
;
629 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
633 addr16_ha(unsigned char* view
,
634 const Sized_relobj
<size
, big_endian
>* object
,
635 const Symbol_value
<size
>* psymval
,
636 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
638 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
640 reloc
= psymval
->value(object
, addend
);
646 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
649 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
651 rel16(unsigned char* view
,
652 const Sized_relobj
<size
, big_endian
>* object
,
653 const Symbol_value
<size
>* psymval
,
654 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
655 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
656 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
658 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
660 rel16_lo(unsigned char* view
,
661 const Sized_relobj
<size
, big_endian
>* object
,
662 const Symbol_value
<size
>* psymval
,
663 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
664 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
665 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
667 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
669 rel16_hi(unsigned char* view
,
670 const Sized_relobj
<size
, big_endian
>* object
,
671 const Symbol_value
<size
>* psymval
,
672 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
673 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
675 This::template pcrela
<16>(view
, 16, 0xffff, object
,
676 psymval
, addend
, address
);
679 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
680 // final value of the low 16 bits of the
681 // relocation is negative, add one.
683 rel16_ha(unsigned char* view
,
684 const Sized_relobj
<size
, big_endian
>* object
,
685 const Symbol_value
<size
>* psymval
,
686 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
687 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
689 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
691 reloc
= (psymval
->value(object
, addend
) - address
);
696 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
700 // Get the GOT section, creating it if necessary.
702 template<int size
, bool big_endian
>
703 Output_data_got
<size
, big_endian
>*
704 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
707 if (this->got_
== NULL
)
709 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
711 this->got_
= new Output_data_got
<size
, big_endian
>();
713 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
714 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
717 // Create the GOT2 or TOC in the .got section.
720 this->got2_
= new Output_data_space(4, "** GOT2");
721 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
728 this->toc_
= new Output_data_space(8, "** TOC");
729 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
735 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
736 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
738 0, 0, elfcpp::STT_OBJECT
,
740 elfcpp::STV_HIDDEN
, 0,
747 // Get the dynamic reloc section, creating it if necessary.
749 template<int size
, bool big_endian
>
750 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
751 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
753 if (this->rela_dyn_
== NULL
)
755 gold_assert(layout
!= NULL
);
756 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
757 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
758 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
760 return this->rela_dyn_
;
763 // A class to handle the PLT data.
765 template<int size
, bool big_endian
>
766 class Output_data_plt_powerpc
: public Output_section_data
769 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
770 size
, big_endian
> Reloc_section
;
772 Output_data_plt_powerpc(Layout
*);
774 // Add an entry to the PLT.
775 void add_entry(Symbol
* gsym
);
777 // Return the .rela.plt section data.
778 const Reloc_section
* rel_plt() const
784 void do_adjust_output_section(Output_section
* os
);
787 // The size of an entry in the PLT.
788 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
790 // Set the final size.
792 set_final_data_size()
794 unsigned int full_count
= this->count_
+ 4;
796 this->set_data_size(full_count
* base_plt_entry_size
);
799 // Write out the PLT data.
801 do_write(Output_file
*);
803 // The reloc section.
805 // The number of PLT entries.
809 // Create the PLT section. The ordinary .got section is an argument,
810 // since we need to refer to the start.
812 template<int size
, bool big_endian
>
813 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
814 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
816 this->rel_
= new Reloc_section(false);
817 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
818 elfcpp::SHF_ALLOC
, this->rel_
);
821 template<int size
, bool big_endian
>
823 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
828 // Add an entry to the PLT.
830 template<int size
, bool big_endian
>
832 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
834 gold_assert(!gsym
->has_plt_offset());
835 unsigned int index
= this->count_
+ + 4;
836 section_offset_type plt_offset
;
839 plt_offset
= index
* base_plt_entry_size
;
843 gsym
->set_plt_offset(plt_offset
);
847 gsym
->set_needs_dynsym_entry();
848 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
852 static const unsigned int addis_11_11
= 0x3d6b0000;
853 static const unsigned int addis_11_30
= 0x3d7e0000;
854 static const unsigned int addis_12_12
= 0x3d8c0000;
855 static const unsigned int addi_11_11
= 0x396b0000;
856 static const unsigned int add_0_11_11
= 0x7c0b5a14;
857 static const unsigned int add_11_0_11
= 0x7d605a14;
858 static const unsigned int b
= 0x48000000;
859 static const unsigned int bcl_20_31
= 0x429f0005;
860 static const unsigned int bctr
= 0x4e800420;
861 static const unsigned int lis_11
= 0x3d600000;
862 static const unsigned int lis_12
= 0x3d800000;
863 static const unsigned int lwzu_0_12
= 0x840c0000;
864 static const unsigned int lwz_0_12
= 0x800c0000;
865 static const unsigned int lwz_11_11
= 0x816b0000;
866 static const unsigned int lwz_11_30
= 0x817e0000;
867 static const unsigned int lwz_12_12
= 0x818c0000;
868 static const unsigned int mflr_0
= 0x7c0802a6;
869 static const unsigned int mflr_12
= 0x7d8802a6;
870 static const unsigned int mtctr_0
= 0x7c0903a6;
871 static const unsigned int mtctr_11
= 0x7d6903a6;
872 static const unsigned int mtlr_0
= 0x7c0803a6;
873 static const unsigned int nop
= 0x60000000;
874 static const unsigned int sub_11_11_12
= 0x7d6c5850;
876 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
877 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
878 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
879 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
880 /* ld %r11,xxx+16@l(%r12) */
883 // Write out the PLT.
885 template<int size
, bool big_endian
>
887 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
889 const off_t offset
= this->offset();
890 const section_size_type oview_size
=
891 convert_to_section_size_type(this->data_size());
892 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
893 unsigned char* pov
= oview
;
895 memset(pov
, 0, base_plt_entry_size
* 4);
896 pov
+= base_plt_entry_size
* 4;
898 unsigned int plt_offset
= base_plt_entry_size
* 4;
899 const unsigned int count
= this->count_
;
903 for (unsigned int i
= 0; i
< count
; i
++)
909 for (unsigned int i
= 0; i
< count
; i
++)
911 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
912 lwz_11_30
+ plt_offset
);
913 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
914 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
915 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
916 pov
+= base_plt_entry_size
;
917 plt_offset
+= base_plt_entry_size
;
921 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
923 of
->write_output_view(offset
, oview_size
, oview
);
926 // Create a PLT entry for a global symbol.
928 template<int size
, bool big_endian
>
930 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
934 if (gsym
->has_plt_offset())
937 if (this->plt_
== NULL
)
939 // Create the GOT section first.
940 this->got_section(symtab
, layout
);
942 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
943 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
945 | elfcpp::SHF_EXECINSTR
946 | elfcpp::SHF_WRITE
),
949 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
950 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
952 0, 0, elfcpp::STT_OBJECT
,
954 elfcpp::STV_HIDDEN
, 0,
958 this->plt_
->add_entry(gsym
);
961 // Create a GOT entry for the TLS module index.
963 template<int size
, bool big_endian
>
965 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
967 Sized_relobj
<size
, big_endian
>* object
)
969 if (this->got_mod_index_offset_
== -1U)
971 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
972 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
973 Output_data_got
<size
, big_endian
>* got
;
974 unsigned int got_offset
;
976 got
= this->got_section(symtab
, layout
);
977 got_offset
= got
->add_constant(0);
978 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
980 got
->add_constant(0);
981 this->got_mod_index_offset_
= got_offset
;
983 return this->got_mod_index_offset_
;
986 // Optimize the TLS relocation type based on what we know about the
987 // symbol. IS_FINAL is true if the final address of this symbol is
988 // known at link time.
990 static tls::Tls_optimization
991 optimize_tls_reloc(bool /* is_final */, int r_type
)
993 // If we are generating a shared library, then we can't do anything
995 if (parameters
->options().shared())
996 return tls::TLSOPT_NONE
;
1005 // Report an unsupported relocation against a local symbol.
1007 template<int size
, bool big_endian
>
1009 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1010 Sized_relobj
<size
, big_endian
>* object
,
1011 unsigned int r_type
)
1013 gold_error(_("%s: unsupported reloc %u against local symbol"),
1014 object
->name().c_str(), r_type
);
1017 // We are about to emit a dynamic relocation of type R_TYPE. If the
1018 // dynamic linker does not support it, issue an error.
1020 template<int size
, bool big_endian
>
1022 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1023 unsigned int r_type
)
1025 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1027 // These are the relocation types supported by glibc for both 32-bit
1028 // and 64-bit powerpc.
1031 case elfcpp::R_POWERPC_RELATIVE
:
1032 case elfcpp::R_POWERPC_GLOB_DAT
:
1033 case elfcpp::R_POWERPC_DTPMOD
:
1034 case elfcpp::R_POWERPC_DTPREL
:
1035 case elfcpp::R_POWERPC_TPREL
:
1036 case elfcpp::R_POWERPC_JMP_SLOT
:
1037 case elfcpp::R_POWERPC_COPY
:
1038 case elfcpp::R_POWERPC_ADDR32
:
1039 case elfcpp::R_POWERPC_ADDR24
:
1040 case elfcpp::R_POWERPC_REL24
:
1051 // These are the relocation types supported only on 64-bit.
1052 case elfcpp::R_PPC64_ADDR64
:
1053 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1054 case elfcpp::R_PPC64_TPREL16_DS
:
1055 case elfcpp::R_POWERPC_TPREL16
:
1056 case elfcpp::R_POWERPC_TPREL16_LO
:
1057 case elfcpp::R_POWERPC_TPREL16_HI
:
1058 case elfcpp::R_POWERPC_TPREL16_HA
:
1059 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1060 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1061 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1062 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1063 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1064 case elfcpp::R_POWERPC_ADDR16_LO
:
1065 case elfcpp::R_POWERPC_ADDR16_HI
:
1066 case elfcpp::R_POWERPC_ADDR16_HA
:
1067 case elfcpp::R_POWERPC_ADDR30
:
1068 case elfcpp::R_PPC64_UADDR64
:
1069 case elfcpp::R_POWERPC_UADDR32
:
1070 case elfcpp::R_POWERPC_ADDR16
:
1071 case elfcpp::R_POWERPC_UADDR16
:
1072 case elfcpp::R_PPC64_ADDR16_DS
:
1073 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1074 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1075 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1076 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1077 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1078 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1079 case elfcpp::R_POWERPC_REL32
:
1080 case elfcpp::R_PPC64_REL64
:
1091 // These are the relocation types supported only on 32-bit.
1098 // This prevents us from issuing more than one error per reloc
1099 // section. But we can still wind up issuing more than one
1100 // error per object file.
1101 if (this->issued_non_pic_error_
)
1103 gold_assert(parameters
->options().output_is_position_independent());
1104 object
->error(_("requires unsupported dynamic reloc; "
1105 "recompile with -fPIC"));
1106 this->issued_non_pic_error_
= true;
1110 // Scan a relocation for a local symbol.
1112 template<int size
, bool big_endian
>
1114 Target_powerpc
<size
, big_endian
>::Scan::local(
1115 const General_options
&,
1116 Symbol_table
* symtab
,
1118 Target_powerpc
<size
, big_endian
>* target
,
1119 Sized_relobj
<size
, big_endian
>* object
,
1120 unsigned int data_shndx
,
1121 Output_section
* output_section
,
1122 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1123 unsigned int r_type
,
1124 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1128 case elfcpp::R_POWERPC_NONE
:
1129 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1130 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1133 case elfcpp::R_PPC64_ADDR64
:
1134 case elfcpp::R_POWERPC_ADDR32
:
1135 case elfcpp::R_POWERPC_ADDR16_HA
:
1136 case elfcpp::R_POWERPC_ADDR16_LO
:
1137 // If building a shared library (or a position-independent
1138 // executable), we need to create a dynamic relocation for
1140 if (parameters
->options().output_is_position_independent())
1142 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1144 check_non_pic(object
, r_type
);
1145 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1147 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1148 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1149 data_shndx
, reloc
.get_r_offset(),
1150 reloc
.get_r_addend());
1154 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1155 gold_assert(lsym
.get_st_value() == 0);
1156 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1157 output_section
, data_shndx
,
1158 reloc
.get_r_offset(),
1159 reloc
.get_r_addend());
1164 case elfcpp::R_POWERPC_REL24
:
1165 case elfcpp::R_PPC_LOCAL24PC
:
1166 case elfcpp::R_POWERPC_REL32
:
1167 case elfcpp::R_PPC_REL16_LO
:
1168 case elfcpp::R_PPC_REL16_HA
:
1171 case elfcpp::R_POWERPC_GOT16
:
1172 case elfcpp::R_POWERPC_GOT16_LO
:
1173 case elfcpp::R_POWERPC_GOT16_HI
:
1174 case elfcpp::R_POWERPC_GOT16_HA
:
1175 case elfcpp::R_PPC64_TOC16
:
1176 case elfcpp::R_PPC64_TOC16_LO
:
1177 case elfcpp::R_PPC64_TOC16_HI
:
1178 case elfcpp::R_PPC64_TOC16_HA
:
1179 case elfcpp::R_PPC64_TOC16_DS
:
1180 case elfcpp::R_PPC64_TOC16_LO_DS
:
1182 // The symbol requires a GOT entry.
1183 Output_data_got
<size
, big_endian
>* got
;
1186 got
= target
->got_section(symtab
, layout
);
1187 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1189 // If we are generating a shared object, we need to add a
1190 // dynamic relocation for this symbol's GOT entry.
1191 if (parameters
->options().output_is_position_independent())
1193 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1195 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1198 off
= got
->add_constant(0);
1199 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1200 rela_dyn
->add_local_relative(object
, r_sym
,
1201 elfcpp::R_POWERPC_RELATIVE
,
1206 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1210 case elfcpp::R_PPC64_TOC
:
1211 // We need a GOT section.
1212 target
->got_section(symtab
, layout
);
1215 // These are relocations which should only be seen by the
1216 // dynamic linker, and should never be seen here.
1217 case elfcpp::R_POWERPC_COPY
:
1218 case elfcpp::R_POWERPC_GLOB_DAT
:
1219 case elfcpp::R_POWERPC_JMP_SLOT
:
1220 case elfcpp::R_POWERPC_RELATIVE
:
1221 case elfcpp::R_POWERPC_DTPMOD
:
1222 gold_error(_("%s: unexpected reloc %u in object file"),
1223 object
->name().c_str(), r_type
);
1227 unsupported_reloc_local(object
, r_type
);
1232 // Report an unsupported relocation against a global symbol.
1234 template<int size
, bool big_endian
>
1236 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1237 Sized_relobj
<size
, big_endian
>* object
,
1238 unsigned int r_type
,
1241 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1242 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1245 // Scan a relocation for a global symbol.
1247 template<int size
, bool big_endian
>
1249 Target_powerpc
<size
, big_endian
>::Scan::global(
1250 const General_options
&,
1251 Symbol_table
* symtab
,
1253 Target_powerpc
<size
, big_endian
>* target
,
1254 Sized_relobj
<size
, big_endian
>* object
,
1255 unsigned int data_shndx
,
1256 Output_section
* output_section
,
1257 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1258 unsigned int r_type
,
1263 case elfcpp::R_POWERPC_NONE
:
1264 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1265 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1268 case elfcpp::R_PPC_PLTREL24
:
1269 // If the symbol is fully resolved, this is just a PC32 reloc.
1270 // Otherwise we need a PLT entry.
1271 if (gsym
->final_value_is_known())
1273 // If building a shared library, we can also skip the PLT entry
1274 // if the symbol is defined in the output file and is protected
1276 if (gsym
->is_defined()
1277 && !gsym
->is_from_dynobj()
1278 && !gsym
->is_preemptible())
1280 target
->make_plt_entry(symtab
, layout
, gsym
);
1283 case elfcpp::R_POWERPC_ADDR16
:
1284 case elfcpp::R_POWERPC_ADDR16_LO
:
1285 case elfcpp::R_POWERPC_ADDR16_HI
:
1286 case elfcpp::R_POWERPC_ADDR16_HA
:
1287 case elfcpp::R_POWERPC_ADDR32
:
1288 case elfcpp::R_PPC64_ADDR64
:
1290 // Make a PLT entry if necessary.
1291 if (gsym
->needs_plt_entry())
1293 target
->make_plt_entry(symtab
, layout
, gsym
);
1294 // Since this is not a PC-relative relocation, we may be
1295 // taking the address of a function. In that case we need to
1296 // set the entry in the dynamic symbol table to the address of
1298 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1299 gsym
->set_needs_dynsym_value();
1301 // Make a dynamic relocation if necessary.
1302 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1304 if (gsym
->may_need_copy_reloc())
1306 target
->copy_reloc(symtab
, layout
, object
,
1307 data_shndx
, output_section
, gsym
, reloc
);
1309 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1310 || r_type
== elfcpp::R_PPC64_ADDR64
)
1311 && gsym
->can_use_relative_reloc(false))
1313 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1314 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1315 output_section
, object
,
1316 data_shndx
, reloc
.get_r_offset(),
1317 reloc
.get_r_addend());
1321 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1323 check_non_pic(object
, r_type
);
1324 if (gsym
->is_from_dynobj()
1325 || gsym
->is_undefined()
1326 || gsym
->is_preemptible())
1327 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1329 reloc
.get_r_offset(),
1330 reloc
.get_r_addend());
1332 rela_dyn
->add_global_relative(gsym
, r_type
,
1333 output_section
, object
,
1335 reloc
.get_r_offset(),
1336 reloc
.get_r_addend());
1342 case elfcpp::R_POWERPC_REL24
:
1343 case elfcpp::R_PPC_LOCAL24PC
:
1344 case elfcpp::R_PPC_REL16
:
1345 case elfcpp::R_PPC_REL16_LO
:
1346 case elfcpp::R_PPC_REL16_HI
:
1347 case elfcpp::R_PPC_REL16_HA
:
1349 if (gsym
->needs_plt_entry())
1350 target
->make_plt_entry(symtab
, layout
, gsym
);
1351 // Make a dynamic relocation if necessary.
1352 int flags
= Symbol::NON_PIC_REF
;
1353 if (gsym
->type() == elfcpp::STT_FUNC
)
1354 flags
|= Symbol::FUNCTION_CALL
;
1355 if (gsym
->needs_dynamic_reloc(flags
))
1357 if (gsym
->may_need_copy_reloc())
1359 target
->copy_reloc(symtab
, layout
, object
,
1360 data_shndx
, output_section
, gsym
,
1365 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1366 check_non_pic(object
, r_type
);
1367 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1368 data_shndx
, reloc
.get_r_offset(),
1369 reloc
.get_r_addend());
1375 case elfcpp::R_POWERPC_GOT16
:
1376 case elfcpp::R_POWERPC_GOT16_LO
:
1377 case elfcpp::R_POWERPC_GOT16_HI
:
1378 case elfcpp::R_POWERPC_GOT16_HA
:
1379 case elfcpp::R_PPC64_TOC16
:
1380 case elfcpp::R_PPC64_TOC16_LO
:
1381 case elfcpp::R_PPC64_TOC16_HI
:
1382 case elfcpp::R_PPC64_TOC16_HA
:
1383 case elfcpp::R_PPC64_TOC16_DS
:
1384 case elfcpp::R_PPC64_TOC16_LO_DS
:
1386 // The symbol requires a GOT entry.
1387 Output_data_got
<size
, big_endian
>* got
;
1389 got
= target
->got_section(symtab
, layout
);
1390 if (gsym
->final_value_is_known())
1391 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1394 // If this symbol is not fully resolved, we need to add a
1395 // dynamic relocation for it.
1396 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1397 if (gsym
->is_from_dynobj()
1398 || gsym
->is_undefined()
1399 || gsym
->is_preemptible())
1400 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1401 elfcpp::R_POWERPC_GLOB_DAT
);
1402 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1404 unsigned int off
= got
->add_constant(0);
1406 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1407 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1414 case elfcpp::R_PPC64_TOC
:
1415 // We need a GOT section.
1416 target
->got_section(symtab
, layout
);
1419 case elfcpp::R_POWERPC_GOT_TPREL16
:
1420 case elfcpp::R_POWERPC_TLS
:
1424 // These are relocations which should only be seen by the
1425 // dynamic linker, and should never be seen here.
1426 case elfcpp::R_POWERPC_COPY
:
1427 case elfcpp::R_POWERPC_GLOB_DAT
:
1428 case elfcpp::R_POWERPC_JMP_SLOT
:
1429 case elfcpp::R_POWERPC_RELATIVE
:
1430 case elfcpp::R_POWERPC_DTPMOD
:
1431 gold_error(_("%s: unexpected reloc %u in object file"),
1432 object
->name().c_str(), r_type
);
1436 unsupported_reloc_global(object
, r_type
, gsym
);
1441 // Process relocations for gc.
1443 template<int size
, bool big_endian
>
1445 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1446 const General_options
& options
,
1447 Symbol_table
* symtab
,
1449 Sized_relobj
<size
, big_endian
>* object
,
1450 unsigned int data_shndx
,
1452 const unsigned char* prelocs
,
1454 Output_section
* output_section
,
1455 bool needs_special_offset_handling
,
1456 size_t local_symbol_count
,
1457 const unsigned char* plocal_symbols
)
1459 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1460 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1462 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1472 needs_special_offset_handling
,
1477 // Scan relocations for a section.
1479 template<int size
, bool big_endian
>
1481 Target_powerpc
<size
, big_endian
>::scan_relocs(
1482 const General_options
& options
,
1483 Symbol_table
* symtab
,
1485 Sized_relobj
<size
, big_endian
>* object
,
1486 unsigned int data_shndx
,
1487 unsigned int sh_type
,
1488 const unsigned char* prelocs
,
1490 Output_section
* output_section
,
1491 bool needs_special_offset_handling
,
1492 size_t local_symbol_count
,
1493 const unsigned char* plocal_symbols
)
1495 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1496 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1497 static Output_data_space
* sdata
;
1499 if (sh_type
== elfcpp::SHT_REL
)
1501 gold_error(_("%s: unsupported REL reloc section"),
1502 object
->name().c_str());
1506 // Define _SDA_BASE_ at the start of the .sdata section.
1509 // layout->find_output_section(".sdata") == NULL
1510 sdata
= new Output_data_space(4, "** sdata");
1511 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1513 | elfcpp::SHF_WRITE
,
1515 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1520 elfcpp::STV_HIDDEN
, 0,
1524 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1534 needs_special_offset_handling
,
1539 // Finalize the sections.
1541 template<int size
, bool big_endian
>
1543 Target_powerpc
<size
, big_endian
>::do_finalize_sections(Layout
* layout
)
1545 // Fill in some more dynamic tags.
1546 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1549 if (this->plt_
!= NULL
)
1551 const Output_data
* od
= this->plt_
->rel_plt();
1552 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1553 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1554 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1556 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1559 if (this->rela_dyn_
!= NULL
)
1561 const Output_data
* od
= this->rela_dyn_
;
1562 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1563 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1564 odyn
->add_constant(elfcpp::DT_RELAENT
,
1565 elfcpp::Elf_sizes
<size
>::rela_size
);
1568 if (!parameters
->options().shared())
1570 // The value of the DT_DEBUG tag is filled in by the dynamic
1571 // linker at run time, and used by the debugger.
1572 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1576 // Emit any relocs we saved in an attempt to avoid generating COPY
1578 if (this->copy_relocs_
.any_saved_relocs())
1579 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1582 // Perform a relocation.
1584 template<int size
, bool big_endian
>
1586 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1587 const Relocate_info
<size
, big_endian
>* relinfo
,
1588 Target_powerpc
* target
,
1591 const elfcpp::Rela
<size
, big_endian
>& rela
,
1592 unsigned int r_type
,
1593 const Sized_symbol
<size
>* gsym
,
1594 const Symbol_value
<size
>* psymval
,
1595 unsigned char* view
,
1596 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1597 section_size_type
/* view_size */)
1599 const unsigned int toc_base_offset
= 0x8000;
1600 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1602 // Pick the value to use for symbols defined in shared objects.
1603 Symbol_value
<size
> symval
;
1605 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1606 || r_type
== elfcpp::R_PPC_LOCAL24PC
1607 || r_type
== elfcpp::R_PPC_REL16
1608 || r_type
== elfcpp::R_PPC_REL16_LO
1609 || r_type
== elfcpp::R_PPC_REL16_HI
1610 || r_type
== elfcpp::R_PPC_REL16_HA
))
1612 elfcpp::Elf_Xword value
;
1614 value
= target
->plt_section()->address() + gsym
->plt_offset();
1616 symval
.set_output_value(value
);
1621 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1622 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1624 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1625 // pointer points to the beginning, not the end, of the table.
1626 // So we just use the plain offset.
1627 bool have_got_offset
= false;
1628 unsigned int got_offset
= 0;
1629 unsigned int got2_offset
= 0;
1632 case elfcpp::R_PPC64_TOC16
:
1633 case elfcpp::R_PPC64_TOC16_LO
:
1634 case elfcpp::R_PPC64_TOC16_HI
:
1635 case elfcpp::R_PPC64_TOC16_HA
:
1636 case elfcpp::R_PPC64_TOC16_DS
:
1637 case elfcpp::R_PPC64_TOC16_LO_DS
:
1638 // Subtract the TOC base address.
1639 addend
-= target
->toc_section()->address() + toc_base_offset
;
1642 case elfcpp::R_POWERPC_GOT16
:
1643 case elfcpp::R_POWERPC_GOT16_LO
:
1644 case elfcpp::R_POWERPC_GOT16_HI
:
1645 case elfcpp::R_POWERPC_GOT16_HA
:
1646 case elfcpp::R_PPC64_GOT16_DS
:
1647 case elfcpp::R_PPC64_GOT16_LO_DS
:
1650 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1651 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1655 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1656 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1657 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1659 have_got_offset
= true;
1662 // R_PPC_PLTREL24 is rather special. If non-zero,
1663 // the addend specifies the GOT pointer offset within .got2.
1664 case elfcpp::R_PPC_PLTREL24
:
1665 if (addend
>= 32768)
1667 Output_data_space
* got2
;
1668 got2
= target
->got2_section();
1669 got2_offset
= got2
->offset();
1670 addend
+= got2_offset
;
1672 have_got_offset
= true;
1681 case elfcpp::R_POWERPC_NONE
:
1682 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1683 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1686 case elfcpp::R_POWERPC_REL32
:
1687 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1690 case elfcpp::R_POWERPC_REL24
:
1691 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1694 case elfcpp::R_POWERPC_REL14
:
1695 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1698 case elfcpp::R_PPC_PLTREL24
:
1699 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1702 case elfcpp::R_PPC_LOCAL24PC
:
1703 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1706 case elfcpp::R_PPC64_ADDR64
:
1707 if (!parameters
->options().output_is_position_independent())
1708 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1712 case elfcpp::R_POWERPC_ADDR32
:
1713 if (!parameters
->options().output_is_position_independent())
1714 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1718 case elfcpp::R_POWERPC_ADDR16_LO
:
1719 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1722 case elfcpp::R_POWERPC_ADDR16_HI
:
1723 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1726 case elfcpp::R_POWERPC_ADDR16_HA
:
1727 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1730 case elfcpp::R_PPC_REL16_LO
:
1731 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1734 case elfcpp::R_PPC_REL16_HI
:
1735 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1738 case elfcpp::R_PPC_REL16_HA
:
1739 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1742 case elfcpp::R_POWERPC_GOT16
:
1743 Reloc::addr16(view
, got_offset
, addend
);
1746 case elfcpp::R_POWERPC_GOT16_LO
:
1747 Reloc::addr16_lo(view
, got_offset
, addend
);
1750 case elfcpp::R_POWERPC_GOT16_HI
:
1751 Reloc::addr16_hi(view
, got_offset
, addend
);
1754 case elfcpp::R_POWERPC_GOT16_HA
:
1755 Reloc::addr16_ha(view
, got_offset
, addend
);
1758 case elfcpp::R_PPC64_TOC16
:
1759 Reloc::addr16(view
, got_offset
, addend
);
1762 case elfcpp::R_PPC64_TOC16_LO
:
1763 Reloc::addr16_lo(view
, got_offset
, addend
);
1766 case elfcpp::R_PPC64_TOC16_HI
:
1767 Reloc::addr16_hi(view
, got_offset
, addend
);
1770 case elfcpp::R_PPC64_TOC16_HA
:
1771 Reloc::addr16_ha(view
, got_offset
, addend
);
1774 case elfcpp::R_PPC64_TOC16_DS
:
1775 case elfcpp::R_PPC64_TOC16_LO_DS
:
1776 Reloc::addr16_ds(view
, got_offset
, addend
);
1779 case elfcpp::R_PPC64_TOC
:
1781 elfcpp::Elf_types
<64>::Elf_Addr value
;
1782 value
= target
->toc_section()->address() + toc_base_offset
;
1783 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1787 case elfcpp::R_POWERPC_COPY
:
1788 case elfcpp::R_POWERPC_GLOB_DAT
:
1789 case elfcpp::R_POWERPC_JMP_SLOT
:
1790 case elfcpp::R_POWERPC_RELATIVE
:
1791 // This is an outstanding tls reloc, which is unexpected when
1793 case elfcpp::R_POWERPC_DTPMOD
:
1794 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1795 _("unexpected reloc %u in object file"),
1800 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1801 _("unsupported reloc %u"),
1809 // Perform a TLS relocation.
1811 template<int size
, bool big_endian
>
1813 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1814 const Relocate_info
<size
, big_endian
>* relinfo
,
1815 Target_powerpc
<size
, big_endian
>* target
,
1817 const elfcpp::Rela
<size
, big_endian
>& rela
,
1818 unsigned int r_type
,
1819 const Sized_symbol
<size
>* gsym
,
1820 const Symbol_value
<size
>* psymval
,
1821 unsigned char* view
,
1822 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1825 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1826 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1827 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1829 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1830 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1832 const bool is_final
=
1834 ? !parameters
->options().output_is_position_independent()
1835 : gsym
->final_value_is_known());
1836 const tls::Tls_optimization optimized_type
1837 = optimize_tls_reloc(is_final
, r_type
);
1845 // Relocate section data.
1847 template<int size
, bool big_endian
>
1849 Target_powerpc
<size
, big_endian
>::relocate_section(
1850 const Relocate_info
<size
, big_endian
>* relinfo
,
1851 unsigned int sh_type
,
1852 const unsigned char* prelocs
,
1854 Output_section
* output_section
,
1855 bool needs_special_offset_handling
,
1856 unsigned char* view
,
1857 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1858 section_size_type view_size
,
1859 const Reloc_symbol_changes
* reloc_symbol_changes
)
1861 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1862 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1864 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1866 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1873 needs_special_offset_handling
,
1877 reloc_symbol_changes
);
1880 // Return the size of a relocation while scanning during a relocatable
1883 template<int size
, bool big_endian
>
1885 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1889 // We are always SHT_RELA, so we should never get here.
1894 // Scan the relocs during a relocatable link.
1896 template<int size
, bool big_endian
>
1898 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1899 const General_options
& options
,
1900 Symbol_table
* symtab
,
1902 Sized_relobj
<size
, big_endian
>* object
,
1903 unsigned int data_shndx
,
1904 unsigned int sh_type
,
1905 const unsigned char* prelocs
,
1907 Output_section
* output_section
,
1908 bool needs_special_offset_handling
,
1909 size_t local_symbol_count
,
1910 const unsigned char* plocal_symbols
,
1911 Relocatable_relocs
* rr
)
1913 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1915 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1916 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1918 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1919 Scan_relocatable_relocs
>(
1928 needs_special_offset_handling
,
1934 // Relocate a section during a relocatable link.
1936 template<int size
, bool big_endian
>
1938 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1939 const Relocate_info
<size
, big_endian
>* relinfo
,
1940 unsigned int sh_type
,
1941 const unsigned char* prelocs
,
1943 Output_section
* output_section
,
1944 off_t offset_in_output_section
,
1945 const Relocatable_relocs
* rr
,
1946 unsigned char* view
,
1947 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1948 section_size_type view_size
,
1949 unsigned char* reloc_view
,
1950 section_size_type reloc_view_size
)
1952 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1954 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1959 offset_in_output_section
,
1968 // Return the value to use for a dynamic which requires special
1969 // treatment. This is how we support equality comparisons of function
1970 // pointers across shared library boundaries, as described in the
1971 // processor specific ABI supplement.
1973 template<int size
, bool big_endian
>
1975 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1977 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1978 return this->plt_section()->address() + gsym
->plt_offset();
1981 // The selector for powerpc object files.
1983 template<int size
, bool big_endian
>
1984 class Target_selector_powerpc
: public Target_selector
1987 Target_selector_powerpc()
1988 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1990 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1991 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1994 Target
* do_recognize(int machine
, int, int)
1999 if (machine
!= elfcpp::EM_PPC64
)
2004 if (machine
!= elfcpp::EM_PPC
)
2012 return this->instantiate_target();
2015 Target
* do_instantiate_target()
2016 { return new Target_powerpc
<size
, big_endian
>(); }
2019 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2020 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2021 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2022 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2024 } // End anonymous namespace.