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 // Copy a relocation against a global symbol.
289 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
290 Sized_relobj
<size
, big_endian
>* object
,
291 unsigned int shndx
, Output_section
* output_section
,
292 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
294 this->copy_relocs_
.copy_reloc(symtab
, layout
,
295 symtab
->get_sized_symbol
<size
>(sym
),
296 object
, shndx
, output_section
,
297 reloc
, this->rela_dyn_section(layout
));
300 // Information about this specific target which we pass to the
301 // general Target structure.
302 static Target::Target_info powerpc_info
;
304 // The types of GOT entries needed for this platform.
307 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
308 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
309 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
313 Output_data_got
<size
, big_endian
>* got_
;
315 Output_data_space
* got2_
;
317 Output_data_space
* toc_
;
319 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
320 // The dynamic reloc section.
321 Reloc_section
* rela_dyn_
;
322 // Relocs saved to avoid a COPY reloc.
323 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
324 // Space for variables copied with a COPY reloc.
325 Output_data_space
* dynbss_
;
326 // Offset of the GOT entry for the TLS module index;
327 unsigned int got_mod_index_offset_
;
331 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
334 true, // is_big_endian
335 elfcpp::EM_PPC
, // machine_code
336 false, // has_make_symbol
337 false, // has_resolve
338 false, // has_code_fill
339 true, // is_default_stack_executable
341 "/usr/lib/ld.so.1", // dynamic_linker
342 0x10000000, // default_text_segment_address
343 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
344 4 * 1024, // common_pagesize (overridable by -z common-page-size)
345 elfcpp::SHN_UNDEF
, // small_common_shndx
346 elfcpp::SHN_UNDEF
, // large_common_shndx
347 0, // small_common_section_flags
348 0 // large_common_section_flags
352 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
355 false, // is_big_endian
356 elfcpp::EM_PPC
, // machine_code
357 false, // has_make_symbol
358 false, // has_resolve
359 false, // has_code_fill
360 true, // is_default_stack_executable
362 "/usr/lib/ld.so.1", // dynamic_linker
363 0x10000000, // default_text_segment_address
364 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
365 4 * 1024, // common_pagesize (overridable by -z common-page-size)
366 elfcpp::SHN_UNDEF
, // small_common_shndx
367 elfcpp::SHN_UNDEF
, // large_common_shndx
368 0, // small_common_section_flags
369 0 // large_common_section_flags
373 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
376 true, // is_big_endian
377 elfcpp::EM_PPC64
, // machine_code
378 false, // has_make_symbol
379 false, // has_resolve
380 false, // has_code_fill
381 true, // is_default_stack_executable
383 "/usr/lib/ld.so.1", // dynamic_linker
384 0x10000000, // default_text_segment_address
385 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
386 8 * 1024, // common_pagesize (overridable by -z common-page-size)
387 elfcpp::SHN_UNDEF
, // small_common_shndx
388 elfcpp::SHN_UNDEF
, // large_common_shndx
389 0, // small_common_section_flags
390 0 // large_common_section_flags
394 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
397 false, // is_big_endian
398 elfcpp::EM_PPC64
, // machine_code
399 false, // has_make_symbol
400 false, // has_resolve
401 false, // has_code_fill
402 true, // is_default_stack_executable
404 "/usr/lib/ld.so.1", // dynamic_linker
405 0x10000000, // default_text_segment_address
406 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
407 8 * 1024, // common_pagesize (overridable by -z common-page-size)
408 elfcpp::SHN_UNDEF
, // small_common_shndx
409 elfcpp::SHN_UNDEF
, // large_common_shndx
410 0, // small_common_section_flags
411 0 // large_common_section_flags
414 template<int size
, bool big_endian
>
415 class Powerpc_relocate_functions
418 // Do a simple relocation with the addend in the relocation.
419 template<int valsize
>
421 rela(unsigned char* view
,
422 unsigned int right_shift
,
423 elfcpp::Elf_Xword dst_mask
,
424 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
425 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
427 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
428 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
429 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
430 Valtype reloc
= ((value
+ addend
) >> right_shift
);
435 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
438 // Do a simple relocation using a symbol value with the addend in
440 template<int valsize
>
442 rela(unsigned char* view
,
443 unsigned int right_shift
,
444 elfcpp::Elf_Xword dst_mask
,
445 const Sized_relobj
<size
, big_endian
>* object
,
446 const Symbol_value
<size
>* psymval
,
447 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
449 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
450 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
451 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
452 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
457 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
460 // Do a simple relocation using a symbol value with the addend in
461 // the relocation, unaligned.
462 template<int valsize
>
464 rela_ua(unsigned char* view
, unsigned int right_shift
,
465 elfcpp::Elf_Xword dst_mask
,
466 const Sized_relobj
<size
, big_endian
>* object
,
467 const Symbol_value
<size
>* psymval
,
468 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
470 typedef typename
elfcpp::Swap_unaligned
<valsize
,
471 big_endian
>::Valtype Valtype
;
472 unsigned char* wv
= view
;
473 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
474 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
479 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
482 // Do a simple PC relative relocation with a Symbol_value with the
483 // addend in the relocation.
484 template<int valsize
>
486 pcrela(unsigned char* view
, unsigned int right_shift
,
487 elfcpp::Elf_Xword dst_mask
,
488 const Sized_relobj
<size
, big_endian
>* object
,
489 const Symbol_value
<size
>* psymval
,
490 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
491 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
493 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
494 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
495 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
496 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
502 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
505 template<int valsize
>
507 pcrela_unaligned(unsigned char* view
,
508 const Sized_relobj
<size
, big_endian
>* object
,
509 const Symbol_value
<size
>* psymval
,
510 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
511 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
513 typedef typename
elfcpp::Swap_unaligned
<valsize
,
514 big_endian
>::Valtype Valtype
;
515 unsigned char* wv
= view
;
516 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
518 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
521 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
522 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
524 // R_POWERPC_REL32: (Symbol + Addend - Address)
526 rel32(unsigned char* view
,
527 const Sized_relobj
<size
, big_endian
>* object
,
528 const Symbol_value
<size
>* psymval
,
529 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
530 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
531 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
533 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
535 rel24(unsigned char* view
,
536 const Sized_relobj
<size
, big_endian
>* object
,
537 const Symbol_value
<size
>* psymval
,
538 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
539 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
541 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
542 psymval
, addend
, address
);
545 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
547 rel14(unsigned char* view
,
548 const Sized_relobj
<size
, big_endian
>* object
,
549 const Symbol_value
<size
>* psymval
,
550 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
551 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
553 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
554 psymval
, addend
, address
);
557 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
559 addr16(unsigned char* view
,
560 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
561 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
562 { This_reloc::rela16(view
, value
, addend
); }
565 addr16(unsigned char* view
,
566 const Sized_relobj
<size
, big_endian
>* object
,
567 const Symbol_value
<size
>* psymval
,
568 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
569 { This_reloc::rela16(view
, object
, psymval
, addend
); }
571 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
573 addr16_ds(unsigned char* view
,
574 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
575 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
577 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
580 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
582 addr16_lo(unsigned char* view
,
583 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
584 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
585 { This_reloc::rela16(view
, value
, addend
); }
588 addr16_lo(unsigned char* view
,
589 const Sized_relobj
<size
, big_endian
>* object
,
590 const Symbol_value
<size
>* psymval
,
591 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
592 { This_reloc::rela16(view
, object
, psymval
, addend
); }
594 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
596 addr16_hi(unsigned char* view
,
597 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
600 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
604 addr16_hi(unsigned char* view
,
605 const Sized_relobj
<size
, big_endian
>* object
,
606 const Symbol_value
<size
>* psymval
,
607 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
609 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
612 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
613 // final value of the low 16 bits of the
614 // relocation is negative, add one.
616 addr16_ha(unsigned char* view
,
617 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
618 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
620 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
622 reloc
= value
+ addend
;
628 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
632 addr16_ha(unsigned char* view
,
633 const Sized_relobj
<size
, big_endian
>* object
,
634 const Symbol_value
<size
>* psymval
,
635 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
637 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
639 reloc
= psymval
->value(object
, addend
);
645 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
648 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
650 rel16(unsigned char* view
,
651 const Sized_relobj
<size
, big_endian
>* object
,
652 const Symbol_value
<size
>* psymval
,
653 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
654 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
655 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
657 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
659 rel16_lo(unsigned char* view
,
660 const Sized_relobj
<size
, big_endian
>* object
,
661 const Symbol_value
<size
>* psymval
,
662 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
663 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
664 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
666 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
668 rel16_hi(unsigned char* view
,
669 const Sized_relobj
<size
, big_endian
>* object
,
670 const Symbol_value
<size
>* psymval
,
671 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
672 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
674 This::template pcrela
<16>(view
, 16, 0xffff, object
,
675 psymval
, addend
, address
);
678 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
679 // final value of the low 16 bits of the
680 // relocation is negative, add one.
682 rel16_ha(unsigned char* view
,
683 const Sized_relobj
<size
, big_endian
>* object
,
684 const Symbol_value
<size
>* psymval
,
685 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
686 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
688 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
690 reloc
= (psymval
->value(object
, addend
) - address
);
695 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
699 // Get the GOT section, creating it if necessary.
701 template<int size
, bool big_endian
>
702 Output_data_got
<size
, big_endian
>*
703 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
706 if (this->got_
== NULL
)
708 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
710 this->got_
= new Output_data_got
<size
, big_endian
>();
712 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
713 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
716 // Create the GOT2 or TOC in the .got section.
719 this->got2_
= new Output_data_space(4, "** GOT2");
720 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
727 this->toc_
= new Output_data_space(8, "** TOC");
728 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
734 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
735 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
737 0, 0, elfcpp::STT_OBJECT
,
739 elfcpp::STV_HIDDEN
, 0,
746 // Get the dynamic reloc section, creating it if necessary.
748 template<int size
, bool big_endian
>
749 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
750 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
752 if (this->rela_dyn_
== NULL
)
754 gold_assert(layout
!= NULL
);
755 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
756 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
757 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
759 return this->rela_dyn_
;
762 // A class to handle the PLT data.
764 template<int size
, bool big_endian
>
765 class Output_data_plt_powerpc
: public Output_section_data
768 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
769 size
, big_endian
> Reloc_section
;
771 Output_data_plt_powerpc(Layout
*);
773 // Add an entry to the PLT.
774 void add_entry(Symbol
* gsym
);
776 // Return the .rela.plt section data.
777 const Reloc_section
* rel_plt() const
783 void do_adjust_output_section(Output_section
* os
);
786 // The size of an entry in the PLT.
787 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
789 // Set the final size.
791 set_final_data_size()
793 unsigned int full_count
= this->count_
+ 4;
795 this->set_data_size(full_count
* base_plt_entry_size
);
798 // Write out the PLT data.
800 do_write(Output_file
*);
802 // The reloc section.
804 // The number of PLT entries.
808 // Create the PLT section. The ordinary .got section is an argument,
809 // since we need to refer to the start.
811 template<int size
, bool big_endian
>
812 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
813 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
815 this->rel_
= new Reloc_section(false);
816 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
817 elfcpp::SHF_ALLOC
, this->rel_
);
820 template<int size
, bool big_endian
>
822 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
827 // Add an entry to the PLT.
829 template<int size
, bool big_endian
>
831 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
833 gold_assert(!gsym
->has_plt_offset());
834 unsigned int index
= this->count_
+ + 4;
835 section_offset_type plt_offset
;
838 plt_offset
= index
* base_plt_entry_size
;
842 gsym
->set_plt_offset(plt_offset
);
846 gsym
->set_needs_dynsym_entry();
847 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
851 static const unsigned int addis_11_11
= 0x3d6b0000;
852 static const unsigned int addis_11_30
= 0x3d7e0000;
853 static const unsigned int addis_12_12
= 0x3d8c0000;
854 static const unsigned int addi_11_11
= 0x396b0000;
855 static const unsigned int add_0_11_11
= 0x7c0b5a14;
856 static const unsigned int add_11_0_11
= 0x7d605a14;
857 static const unsigned int b
= 0x48000000;
858 static const unsigned int bcl_20_31
= 0x429f0005;
859 static const unsigned int bctr
= 0x4e800420;
860 static const unsigned int lis_11
= 0x3d600000;
861 static const unsigned int lis_12
= 0x3d800000;
862 static const unsigned int lwzu_0_12
= 0x840c0000;
863 static const unsigned int lwz_0_12
= 0x800c0000;
864 static const unsigned int lwz_11_11
= 0x816b0000;
865 static const unsigned int lwz_11_30
= 0x817e0000;
866 static const unsigned int lwz_12_12
= 0x818c0000;
867 static const unsigned int mflr_0
= 0x7c0802a6;
868 static const unsigned int mflr_12
= 0x7d8802a6;
869 static const unsigned int mtctr_0
= 0x7c0903a6;
870 static const unsigned int mtctr_11
= 0x7d6903a6;
871 static const unsigned int mtlr_0
= 0x7c0803a6;
872 static const unsigned int nop
= 0x60000000;
873 static const unsigned int sub_11_11_12
= 0x7d6c5850;
875 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
876 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
877 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
878 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
879 /* ld %r11,xxx+16@l(%r12) */
882 // Write out the PLT.
884 template<int size
, bool big_endian
>
886 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
888 const off_t offset
= this->offset();
889 const section_size_type oview_size
=
890 convert_to_section_size_type(this->data_size());
891 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
892 unsigned char* pov
= oview
;
894 memset(pov
, 0, base_plt_entry_size
* 4);
895 pov
+= base_plt_entry_size
* 4;
897 unsigned int plt_offset
= base_plt_entry_size
* 4;
898 const unsigned int count
= this->count_
;
902 for (unsigned int i
= 0; i
< count
; i
++)
908 for (unsigned int i
= 0; i
< count
; i
++)
910 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
911 lwz_11_30
+ plt_offset
);
912 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
913 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
914 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
915 pov
+= base_plt_entry_size
;
916 plt_offset
+= base_plt_entry_size
;
920 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
922 of
->write_output_view(offset
, oview_size
, oview
);
925 // Create a PLT entry for a global symbol.
927 template<int size
, bool big_endian
>
929 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
933 if (gsym
->has_plt_offset())
936 if (this->plt_
== NULL
)
938 // Create the GOT section first.
939 this->got_section(symtab
, layout
);
941 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
942 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
944 | elfcpp::SHF_EXECINSTR
945 | elfcpp::SHF_WRITE
),
948 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
949 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
951 0, 0, elfcpp::STT_OBJECT
,
953 elfcpp::STV_HIDDEN
, 0,
957 this->plt_
->add_entry(gsym
);
960 // Create a GOT entry for the TLS module index.
962 template<int size
, bool big_endian
>
964 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
966 Sized_relobj
<size
, big_endian
>* object
)
968 if (this->got_mod_index_offset_
== -1U)
970 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
971 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
972 Output_data_got
<size
, big_endian
>* got
;
973 unsigned int got_offset
;
975 got
= this->got_section(symtab
, layout
);
976 got_offset
= got
->add_constant(0);
977 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
979 got
->add_constant(0);
980 this->got_mod_index_offset_
= got_offset
;
982 return this->got_mod_index_offset_
;
985 // Optimize the TLS relocation type based on what we know about the
986 // symbol. IS_FINAL is true if the final address of this symbol is
987 // known at link time.
989 static tls::Tls_optimization
990 optimize_tls_reloc(bool /* is_final */, int r_type
)
992 // If we are generating a shared library, then we can't do anything
994 if (parameters
->options().shared())
995 return tls::TLSOPT_NONE
;
1004 // Report an unsupported relocation against a local symbol.
1006 template<int size
, bool big_endian
>
1008 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1009 Sized_relobj
<size
, big_endian
>* object
,
1010 unsigned int r_type
)
1012 gold_error(_("%s: unsupported reloc %u against local symbol"),
1013 object
->name().c_str(), r_type
);
1016 // We are about to emit a dynamic relocation of type R_TYPE. If the
1017 // dynamic linker does not support it, issue an error.
1019 template<int size
, bool big_endian
>
1021 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1022 unsigned int r_type
)
1024 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1026 // These are the relocation types supported by glibc for both 32-bit
1027 // and 64-bit powerpc.
1030 case elfcpp::R_POWERPC_RELATIVE
:
1031 case elfcpp::R_POWERPC_GLOB_DAT
:
1032 case elfcpp::R_POWERPC_DTPMOD
:
1033 case elfcpp::R_POWERPC_DTPREL
:
1034 case elfcpp::R_POWERPC_TPREL
:
1035 case elfcpp::R_POWERPC_JMP_SLOT
:
1036 case elfcpp::R_POWERPC_COPY
:
1037 case elfcpp::R_POWERPC_ADDR32
:
1038 case elfcpp::R_POWERPC_ADDR24
:
1039 case elfcpp::R_POWERPC_REL24
:
1050 // These are the relocation types supported only on 64-bit.
1051 case elfcpp::R_PPC64_ADDR64
:
1052 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1053 case elfcpp::R_PPC64_TPREL16_DS
:
1054 case elfcpp::R_POWERPC_TPREL16
:
1055 case elfcpp::R_POWERPC_TPREL16_LO
:
1056 case elfcpp::R_POWERPC_TPREL16_HI
:
1057 case elfcpp::R_POWERPC_TPREL16_HA
:
1058 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1059 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1060 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1061 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1062 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1063 case elfcpp::R_POWERPC_ADDR16_LO
:
1064 case elfcpp::R_POWERPC_ADDR16_HI
:
1065 case elfcpp::R_POWERPC_ADDR16_HA
:
1066 case elfcpp::R_POWERPC_ADDR30
:
1067 case elfcpp::R_PPC64_UADDR64
:
1068 case elfcpp::R_POWERPC_UADDR32
:
1069 case elfcpp::R_POWERPC_ADDR16
:
1070 case elfcpp::R_POWERPC_UADDR16
:
1071 case elfcpp::R_PPC64_ADDR16_DS
:
1072 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1073 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1074 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1075 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1076 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1077 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1078 case elfcpp::R_POWERPC_REL32
:
1079 case elfcpp::R_PPC64_REL64
:
1090 // These are the relocation types supported only on 32-bit.
1097 // This prevents us from issuing more than one error per reloc
1098 // section. But we can still wind up issuing more than one
1099 // error per object file.
1100 if (this->issued_non_pic_error_
)
1102 gold_assert(parameters
->options().output_is_position_independent());
1103 object
->error(_("requires unsupported dynamic reloc; "
1104 "recompile with -fPIC"));
1105 this->issued_non_pic_error_
= true;
1109 // Scan a relocation for a local symbol.
1111 template<int size
, bool big_endian
>
1113 Target_powerpc
<size
, big_endian
>::Scan::local(
1114 const General_options
&,
1115 Symbol_table
* symtab
,
1117 Target_powerpc
<size
, big_endian
>* target
,
1118 Sized_relobj
<size
, big_endian
>* object
,
1119 unsigned int data_shndx
,
1120 Output_section
* output_section
,
1121 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1122 unsigned int r_type
,
1123 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1127 case elfcpp::R_POWERPC_NONE
:
1128 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1129 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1132 case elfcpp::R_PPC64_ADDR64
:
1133 case elfcpp::R_POWERPC_ADDR32
:
1134 case elfcpp::R_POWERPC_ADDR16_HA
:
1135 case elfcpp::R_POWERPC_ADDR16_LO
:
1136 // If building a shared library (or a position-independent
1137 // executable), we need to create a dynamic relocation for
1139 if (parameters
->options().output_is_position_independent())
1141 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1143 check_non_pic(object
, r_type
);
1144 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1146 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1147 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1148 data_shndx
, reloc
.get_r_offset(),
1149 reloc
.get_r_addend());
1153 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1154 gold_assert(lsym
.get_st_value() == 0);
1155 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1156 output_section
, data_shndx
,
1157 reloc
.get_r_offset(),
1158 reloc
.get_r_addend());
1163 case elfcpp::R_POWERPC_REL24
:
1164 case elfcpp::R_PPC_LOCAL24PC
:
1165 case elfcpp::R_POWERPC_REL32
:
1166 case elfcpp::R_PPC_REL16_LO
:
1167 case elfcpp::R_PPC_REL16_HA
:
1170 case elfcpp::R_POWERPC_GOT16
:
1171 case elfcpp::R_POWERPC_GOT16_LO
:
1172 case elfcpp::R_POWERPC_GOT16_HI
:
1173 case elfcpp::R_POWERPC_GOT16_HA
:
1174 case elfcpp::R_PPC64_TOC16
:
1175 case elfcpp::R_PPC64_TOC16_LO
:
1176 case elfcpp::R_PPC64_TOC16_HI
:
1177 case elfcpp::R_PPC64_TOC16_HA
:
1178 case elfcpp::R_PPC64_TOC16_DS
:
1179 case elfcpp::R_PPC64_TOC16_LO_DS
:
1181 // The symbol requires a GOT entry.
1182 Output_data_got
<size
, big_endian
>* got
;
1185 got
= target
->got_section(symtab
, layout
);
1186 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1188 // If we are generating a shared object, we need to add a
1189 // dynamic relocation for this symbol's GOT entry.
1190 if (parameters
->options().output_is_position_independent())
1192 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1194 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1197 off
= got
->add_constant(0);
1198 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1199 rela_dyn
->add_local_relative(object
, r_sym
,
1200 elfcpp::R_POWERPC_RELATIVE
,
1205 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1209 case elfcpp::R_PPC64_TOC
:
1210 // We need a GOT section.
1211 target
->got_section(symtab
, layout
);
1214 // These are relocations which should only be seen by the
1215 // dynamic linker, and should never be seen here.
1216 case elfcpp::R_POWERPC_COPY
:
1217 case elfcpp::R_POWERPC_GLOB_DAT
:
1218 case elfcpp::R_POWERPC_JMP_SLOT
:
1219 case elfcpp::R_POWERPC_RELATIVE
:
1220 case elfcpp::R_POWERPC_DTPMOD
:
1221 gold_error(_("%s: unexpected reloc %u in object file"),
1222 object
->name().c_str(), r_type
);
1226 unsupported_reloc_local(object
, r_type
);
1231 // Report an unsupported relocation against a global symbol.
1233 template<int size
, bool big_endian
>
1235 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1236 Sized_relobj
<size
, big_endian
>* object
,
1237 unsigned int r_type
,
1240 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1241 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1244 // Scan a relocation for a global symbol.
1246 template<int size
, bool big_endian
>
1248 Target_powerpc
<size
, big_endian
>::Scan::global(
1249 const General_options
&,
1250 Symbol_table
* symtab
,
1252 Target_powerpc
<size
, big_endian
>* target
,
1253 Sized_relobj
<size
, big_endian
>* object
,
1254 unsigned int data_shndx
,
1255 Output_section
* output_section
,
1256 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1257 unsigned int r_type
,
1262 case elfcpp::R_POWERPC_NONE
:
1263 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1264 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1267 case elfcpp::R_PPC_PLTREL24
:
1268 // If the symbol is fully resolved, this is just a PC32 reloc.
1269 // Otherwise we need a PLT entry.
1270 if (gsym
->final_value_is_known())
1272 // If building a shared library, we can also skip the PLT entry
1273 // if the symbol is defined in the output file and is protected
1275 if (gsym
->is_defined()
1276 && !gsym
->is_from_dynobj()
1277 && !gsym
->is_preemptible())
1279 target
->make_plt_entry(symtab
, layout
, gsym
);
1282 case elfcpp::R_POWERPC_ADDR16
:
1283 case elfcpp::R_POWERPC_ADDR16_LO
:
1284 case elfcpp::R_POWERPC_ADDR16_HI
:
1285 case elfcpp::R_POWERPC_ADDR16_HA
:
1286 case elfcpp::R_POWERPC_ADDR32
:
1287 case elfcpp::R_PPC64_ADDR64
:
1289 // Make a PLT entry if necessary.
1290 if (gsym
->needs_plt_entry())
1292 target
->make_plt_entry(symtab
, layout
, gsym
);
1293 // Since this is not a PC-relative relocation, we may be
1294 // taking the address of a function. In that case we need to
1295 // set the entry in the dynamic symbol table to the address of
1297 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1298 gsym
->set_needs_dynsym_value();
1300 // Make a dynamic relocation if necessary.
1301 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1303 if (gsym
->may_need_copy_reloc())
1305 target
->copy_reloc(symtab
, layout
, object
,
1306 data_shndx
, output_section
, gsym
, reloc
);
1308 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1309 || r_type
== elfcpp::R_PPC64_ADDR64
)
1310 && gsym
->can_use_relative_reloc(false))
1312 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1313 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1314 output_section
, object
,
1315 data_shndx
, reloc
.get_r_offset(),
1316 reloc
.get_r_addend());
1320 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1322 check_non_pic(object
, r_type
);
1323 if (gsym
->is_from_dynobj()
1324 || gsym
->is_undefined()
1325 || gsym
->is_preemptible())
1326 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1328 reloc
.get_r_offset(),
1329 reloc
.get_r_addend());
1331 rela_dyn
->add_global_relative(gsym
, r_type
,
1332 output_section
, object
,
1334 reloc
.get_r_offset(),
1335 reloc
.get_r_addend());
1341 case elfcpp::R_POWERPC_REL24
:
1342 case elfcpp::R_PPC_LOCAL24PC
:
1343 case elfcpp::R_PPC_REL16
:
1344 case elfcpp::R_PPC_REL16_LO
:
1345 case elfcpp::R_PPC_REL16_HI
:
1346 case elfcpp::R_PPC_REL16_HA
:
1348 if (gsym
->needs_plt_entry())
1349 target
->make_plt_entry(symtab
, layout
, gsym
);
1350 // Make a dynamic relocation if necessary.
1351 int flags
= Symbol::NON_PIC_REF
;
1352 if (gsym
->type() == elfcpp::STT_FUNC
)
1353 flags
|= Symbol::FUNCTION_CALL
;
1354 if (gsym
->needs_dynamic_reloc(flags
))
1356 if (gsym
->may_need_copy_reloc())
1358 target
->copy_reloc(symtab
, layout
, object
,
1359 data_shndx
, output_section
, gsym
,
1364 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1365 check_non_pic(object
, r_type
);
1366 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1367 data_shndx
, reloc
.get_r_offset(),
1368 reloc
.get_r_addend());
1374 case elfcpp::R_POWERPC_GOT16
:
1375 case elfcpp::R_POWERPC_GOT16_LO
:
1376 case elfcpp::R_POWERPC_GOT16_HI
:
1377 case elfcpp::R_POWERPC_GOT16_HA
:
1378 case elfcpp::R_PPC64_TOC16
:
1379 case elfcpp::R_PPC64_TOC16_LO
:
1380 case elfcpp::R_PPC64_TOC16_HI
:
1381 case elfcpp::R_PPC64_TOC16_HA
:
1382 case elfcpp::R_PPC64_TOC16_DS
:
1383 case elfcpp::R_PPC64_TOC16_LO_DS
:
1385 // The symbol requires a GOT entry.
1386 Output_data_got
<size
, big_endian
>* got
;
1388 got
= target
->got_section(symtab
, layout
);
1389 if (gsym
->final_value_is_known())
1390 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1393 // If this symbol is not fully resolved, we need to add a
1394 // dynamic relocation for it.
1395 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1396 if (gsym
->is_from_dynobj()
1397 || gsym
->is_undefined()
1398 || gsym
->is_preemptible())
1399 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1400 elfcpp::R_POWERPC_GLOB_DAT
);
1401 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1403 unsigned int off
= got
->add_constant(0);
1405 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1406 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1413 case elfcpp::R_PPC64_TOC
:
1414 // We need a GOT section.
1415 target
->got_section(symtab
, layout
);
1418 case elfcpp::R_POWERPC_GOT_TPREL16
:
1419 case elfcpp::R_POWERPC_TLS
:
1423 // These are relocations which should only be seen by the
1424 // dynamic linker, and should never be seen here.
1425 case elfcpp::R_POWERPC_COPY
:
1426 case elfcpp::R_POWERPC_GLOB_DAT
:
1427 case elfcpp::R_POWERPC_JMP_SLOT
:
1428 case elfcpp::R_POWERPC_RELATIVE
:
1429 case elfcpp::R_POWERPC_DTPMOD
:
1430 gold_error(_("%s: unexpected reloc %u in object file"),
1431 object
->name().c_str(), r_type
);
1435 unsupported_reloc_global(object
, r_type
, gsym
);
1440 // Process relocations for gc.
1442 template<int size
, bool big_endian
>
1444 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1445 const General_options
& options
,
1446 Symbol_table
* symtab
,
1448 Sized_relobj
<size
, big_endian
>* object
,
1449 unsigned int data_shndx
,
1451 const unsigned char* prelocs
,
1453 Output_section
* output_section
,
1454 bool needs_special_offset_handling
,
1455 size_t local_symbol_count
,
1456 const unsigned char* plocal_symbols
)
1458 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1459 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1461 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1471 needs_special_offset_handling
,
1476 // Scan relocations for a section.
1478 template<int size
, bool big_endian
>
1480 Target_powerpc
<size
, big_endian
>::scan_relocs(
1481 const General_options
& options
,
1482 Symbol_table
* symtab
,
1484 Sized_relobj
<size
, big_endian
>* object
,
1485 unsigned int data_shndx
,
1486 unsigned int sh_type
,
1487 const unsigned char* prelocs
,
1489 Output_section
* output_section
,
1490 bool needs_special_offset_handling
,
1491 size_t local_symbol_count
,
1492 const unsigned char* plocal_symbols
)
1494 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1495 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1496 static Output_data_space
* sdata
;
1498 if (sh_type
== elfcpp::SHT_REL
)
1500 gold_error(_("%s: unsupported REL reloc section"),
1501 object
->name().c_str());
1505 // Define _SDA_BASE_ at the start of the .sdata section.
1508 // layout->find_output_section(".sdata") == NULL
1509 sdata
= new Output_data_space(4, "** sdata");
1510 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1512 | elfcpp::SHF_WRITE
,
1514 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1519 elfcpp::STV_HIDDEN
, 0,
1523 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1533 needs_special_offset_handling
,
1538 // Finalize the sections.
1540 template<int size
, bool big_endian
>
1542 Target_powerpc
<size
, big_endian
>::do_finalize_sections(Layout
* layout
)
1544 // Fill in some more dynamic tags.
1545 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1548 if (this->plt_
!= NULL
)
1550 const Output_data
* od
= this->plt_
->rel_plt();
1551 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1552 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1553 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1555 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1558 if (this->rela_dyn_
!= NULL
)
1560 const Output_data
* od
= this->rela_dyn_
;
1561 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1562 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1563 odyn
->add_constant(elfcpp::DT_RELAENT
,
1564 elfcpp::Elf_sizes
<size
>::rela_size
);
1567 if (!parameters
->options().shared())
1569 // The value of the DT_DEBUG tag is filled in by the dynamic
1570 // linker at run time, and used by the debugger.
1571 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1575 // Emit any relocs we saved in an attempt to avoid generating COPY
1577 if (this->copy_relocs_
.any_saved_relocs())
1578 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1581 // Perform a relocation.
1583 template<int size
, bool big_endian
>
1585 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1586 const Relocate_info
<size
, big_endian
>* relinfo
,
1587 Target_powerpc
* target
,
1590 const elfcpp::Rela
<size
, big_endian
>& rela
,
1591 unsigned int r_type
,
1592 const Sized_symbol
<size
>* gsym
,
1593 const Symbol_value
<size
>* psymval
,
1594 unsigned char* view
,
1595 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1596 section_size_type
/* view_size */)
1598 const unsigned int toc_base_offset
= 0x8000;
1599 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1601 // Pick the value to use for symbols defined in shared objects.
1602 Symbol_value
<size
> symval
;
1604 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1605 || r_type
== elfcpp::R_PPC_LOCAL24PC
1606 || r_type
== elfcpp::R_PPC_REL16
1607 || r_type
== elfcpp::R_PPC_REL16_LO
1608 || r_type
== elfcpp::R_PPC_REL16_HI
1609 || r_type
== elfcpp::R_PPC_REL16_HA
))
1611 elfcpp::Elf_Xword value
;
1613 value
= target
->plt_section()->address() + gsym
->plt_offset();
1615 symval
.set_output_value(value
);
1620 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1621 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1623 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1624 // pointer points to the beginning, not the end, of the table.
1625 // So we just use the plain offset.
1626 bool have_got_offset
= false;
1627 unsigned int got_offset
= 0;
1628 unsigned int got2_offset
= 0;
1631 case elfcpp::R_PPC64_TOC16
:
1632 case elfcpp::R_PPC64_TOC16_LO
:
1633 case elfcpp::R_PPC64_TOC16_HI
:
1634 case elfcpp::R_PPC64_TOC16_HA
:
1635 case elfcpp::R_PPC64_TOC16_DS
:
1636 case elfcpp::R_PPC64_TOC16_LO_DS
:
1637 // Subtract the TOC base address.
1638 addend
-= target
->toc_section()->address() + toc_base_offset
;
1641 case elfcpp::R_POWERPC_GOT16
:
1642 case elfcpp::R_POWERPC_GOT16_LO
:
1643 case elfcpp::R_POWERPC_GOT16_HI
:
1644 case elfcpp::R_POWERPC_GOT16_HA
:
1645 case elfcpp::R_PPC64_GOT16_DS
:
1646 case elfcpp::R_PPC64_GOT16_LO_DS
:
1649 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1650 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1654 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1655 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1656 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1658 have_got_offset
= true;
1661 // R_PPC_PLTREL24 is rather special. If non-zero,
1662 // the addend specifies the GOT pointer offset within .got2.
1663 case elfcpp::R_PPC_PLTREL24
:
1664 if (addend
>= 32768)
1666 Output_data_space
* got2
;
1667 got2
= target
->got2_section();
1668 got2_offset
= got2
->offset();
1669 addend
+= got2_offset
;
1671 have_got_offset
= true;
1680 case elfcpp::R_POWERPC_NONE
:
1681 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1682 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1685 case elfcpp::R_POWERPC_REL32
:
1686 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1689 case elfcpp::R_POWERPC_REL24
:
1690 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1693 case elfcpp::R_POWERPC_REL14
:
1694 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1697 case elfcpp::R_PPC_PLTREL24
:
1698 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1701 case elfcpp::R_PPC_LOCAL24PC
:
1702 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1705 case elfcpp::R_PPC64_ADDR64
:
1706 if (!parameters
->options().output_is_position_independent())
1707 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1711 case elfcpp::R_POWERPC_ADDR32
:
1712 if (!parameters
->options().output_is_position_independent())
1713 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1717 case elfcpp::R_POWERPC_ADDR16_LO
:
1718 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1721 case elfcpp::R_POWERPC_ADDR16_HI
:
1722 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1725 case elfcpp::R_POWERPC_ADDR16_HA
:
1726 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1729 case elfcpp::R_PPC_REL16_LO
:
1730 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1733 case elfcpp::R_PPC_REL16_HI
:
1734 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1737 case elfcpp::R_PPC_REL16_HA
:
1738 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1741 case elfcpp::R_POWERPC_GOT16
:
1742 Reloc::addr16(view
, got_offset
, addend
);
1745 case elfcpp::R_POWERPC_GOT16_LO
:
1746 Reloc::addr16_lo(view
, got_offset
, addend
);
1749 case elfcpp::R_POWERPC_GOT16_HI
:
1750 Reloc::addr16_hi(view
, got_offset
, addend
);
1753 case elfcpp::R_POWERPC_GOT16_HA
:
1754 Reloc::addr16_ha(view
, got_offset
, addend
);
1757 case elfcpp::R_PPC64_TOC16
:
1758 Reloc::addr16(view
, got_offset
, addend
);
1761 case elfcpp::R_PPC64_TOC16_LO
:
1762 Reloc::addr16_lo(view
, got_offset
, addend
);
1765 case elfcpp::R_PPC64_TOC16_HI
:
1766 Reloc::addr16_hi(view
, got_offset
, addend
);
1769 case elfcpp::R_PPC64_TOC16_HA
:
1770 Reloc::addr16_ha(view
, got_offset
, addend
);
1773 case elfcpp::R_PPC64_TOC16_DS
:
1774 case elfcpp::R_PPC64_TOC16_LO_DS
:
1775 Reloc::addr16_ds(view
, got_offset
, addend
);
1778 case elfcpp::R_PPC64_TOC
:
1780 elfcpp::Elf_types
<64>::Elf_Addr value
;
1781 value
= target
->toc_section()->address() + toc_base_offset
;
1782 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1786 case elfcpp::R_POWERPC_COPY
:
1787 case elfcpp::R_POWERPC_GLOB_DAT
:
1788 case elfcpp::R_POWERPC_JMP_SLOT
:
1789 case elfcpp::R_POWERPC_RELATIVE
:
1790 // This is an outstanding tls reloc, which is unexpected when
1792 case elfcpp::R_POWERPC_DTPMOD
:
1793 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1794 _("unexpected reloc %u in object file"),
1799 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1800 _("unsupported reloc %u"),
1808 // Perform a TLS relocation.
1810 template<int size
, bool big_endian
>
1812 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1813 const Relocate_info
<size
, big_endian
>* relinfo
,
1814 Target_powerpc
<size
, big_endian
>* target
,
1816 const elfcpp::Rela
<size
, big_endian
>& rela
,
1817 unsigned int r_type
,
1818 const Sized_symbol
<size
>* gsym
,
1819 const Symbol_value
<size
>* psymval
,
1820 unsigned char* view
,
1821 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1824 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1825 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1826 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1828 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1829 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1831 const bool is_final
=
1833 ? !parameters
->options().output_is_position_independent()
1834 : gsym
->final_value_is_known());
1835 const tls::Tls_optimization optimized_type
1836 = optimize_tls_reloc(is_final
, r_type
);
1844 // Relocate section data.
1846 template<int size
, bool big_endian
>
1848 Target_powerpc
<size
, big_endian
>::relocate_section(
1849 const Relocate_info
<size
, big_endian
>* relinfo
,
1850 unsigned int sh_type
,
1851 const unsigned char* prelocs
,
1853 Output_section
* output_section
,
1854 bool needs_special_offset_handling
,
1855 unsigned char* view
,
1856 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1857 section_size_type view_size
)
1859 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1860 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1862 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1864 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1871 needs_special_offset_handling
,
1877 // Return the size of a relocation while scanning during a relocatable
1880 template<int size
, bool big_endian
>
1882 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1886 // We are always SHT_RELA, so we should never get here.
1891 // Scan the relocs during a relocatable link.
1893 template<int size
, bool big_endian
>
1895 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1896 const General_options
& options
,
1897 Symbol_table
* symtab
,
1899 Sized_relobj
<size
, big_endian
>* object
,
1900 unsigned int data_shndx
,
1901 unsigned int sh_type
,
1902 const unsigned char* prelocs
,
1904 Output_section
* output_section
,
1905 bool needs_special_offset_handling
,
1906 size_t local_symbol_count
,
1907 const unsigned char* plocal_symbols
,
1908 Relocatable_relocs
* rr
)
1910 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1912 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1913 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1915 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1916 Scan_relocatable_relocs
>(
1925 needs_special_offset_handling
,
1931 // Relocate a section during a relocatable link.
1933 template<int size
, bool big_endian
>
1935 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1936 const Relocate_info
<size
, big_endian
>* relinfo
,
1937 unsigned int sh_type
,
1938 const unsigned char* prelocs
,
1940 Output_section
* output_section
,
1941 off_t offset_in_output_section
,
1942 const Relocatable_relocs
* rr
,
1943 unsigned char* view
,
1944 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1945 section_size_type view_size
,
1946 unsigned char* reloc_view
,
1947 section_size_type reloc_view_size
)
1949 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1951 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1956 offset_in_output_section
,
1965 // Return the value to use for a dynamic which requires special
1966 // treatment. This is how we support equality comparisons of function
1967 // pointers across shared library boundaries, as described in the
1968 // processor specific ABI supplement.
1970 template<int size
, bool big_endian
>
1972 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1974 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1975 return this->plt_section()->address() + gsym
->plt_offset();
1978 // The selector for powerpc object files.
1980 template<int size
, bool big_endian
>
1981 class Target_selector_powerpc
: public Target_selector
1984 Target_selector_powerpc()
1985 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1987 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1988 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1991 Target
* do_recognize(int machine
, int, int)
1996 if (machine
!= elfcpp::EM_PPC64
)
2001 if (machine
!= elfcpp::EM_PPC
)
2009 return this->instantiate_target();
2012 Target
* do_instantiate_target()
2013 { return new Target_powerpc
<size
, big_endian
>(); }
2016 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2017 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2018 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2019 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2021 } // End anonymous namespace.