1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010 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"
47 template<int size
, bool big_endian
>
48 class Output_data_plt_powerpc
;
50 template<int size
, bool big_endian
>
51 class Target_powerpc
: public Sized_target
<size
, big_endian
>
54 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
57 : Sized_target
<size
, big_endian
>(&powerpc_info
),
58 got_(NULL
), got2_(NULL
), toc_(NULL
),
59 plt_(NULL
), rela_dyn_(NULL
),
60 copy_relocs_(elfcpp::R_POWERPC_COPY
),
61 dynbss_(NULL
), got_mod_index_offset_(-1U)
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
68 gc_process_relocs(Symbol_table
* symtab
,
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(Symbol_table
* symtab
,
84 Sized_relobj
<size
, big_endian
>* object
,
85 unsigned int data_shndx
,
87 const unsigned char* prelocs
,
89 Output_section
* output_section
,
90 bool needs_special_offset_handling
,
91 size_t local_symbol_count
,
92 const unsigned char* plocal_symbols
);
93 // Finalize the sections.
95 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
97 // Return the value to use for a dynamic which requires special
100 do_dynsym_value(const Symbol
*) const;
102 // Relocate a section.
104 relocate_section(const Relocate_info
<size
, big_endian
>*,
105 unsigned int sh_type
,
106 const unsigned char* prelocs
,
108 Output_section
* output_section
,
109 bool needs_special_offset_handling
,
111 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
112 section_size_type view_size
,
113 const Reloc_symbol_changes
*);
115 // Scan the relocs during a relocatable link.
117 scan_relocatable_relocs(Symbol_table
* symtab
,
119 Sized_relobj
<size
, big_endian
>* object
,
120 unsigned int data_shndx
,
121 unsigned int sh_type
,
122 const unsigned char* prelocs
,
124 Output_section
* output_section
,
125 bool needs_special_offset_handling
,
126 size_t local_symbol_count
,
127 const unsigned char* plocal_symbols
,
128 Relocatable_relocs
*);
130 // Relocate a section during a relocatable link.
132 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
133 unsigned int sh_type
,
134 const unsigned char* prelocs
,
136 Output_section
* output_section
,
137 off_t offset_in_output_section
,
138 const Relocatable_relocs
*,
140 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
141 section_size_type view_size
,
142 unsigned char* reloc_view
,
143 section_size_type reloc_view_size
);
145 // Return whether SYM is defined by the ABI.
147 do_is_defined_by_abi(const Symbol
* sym
) const
149 return strcmp(sym
->name(), "___tls_get_addr") == 0;
152 // Return the size of the GOT section.
156 gold_assert(this->got_
!= NULL
);
157 return this->got_
->data_size();
160 // Return the number of entries in the GOT.
162 got_entry_count() const
164 if (this->got_
== NULL
)
166 return this->got_size() / (size
/ 8);
169 // Return the number of entries in the PLT.
171 plt_entry_count() const;
173 // Return the offset of the first non-reserved PLT entry.
175 first_plt_entry_offset() const;
177 // Return the size of each PLT entry.
179 plt_entry_size() const;
183 // The class which scans relocations.
188 : issued_non_pic_error_(false)
192 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
193 Sized_relobj
<size
, big_endian
>* object
,
194 unsigned int data_shndx
,
195 Output_section
* output_section
,
196 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
197 const elfcpp::Sym
<size
, big_endian
>& lsym
);
200 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
201 Sized_relobj
<size
, big_endian
>* object
,
202 unsigned int data_shndx
,
203 Output_section
* output_section
,
204 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
208 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
210 Sized_relobj
<size
, big_endian
>* ,
213 const elfcpp::Rela
<size
, big_endian
>& ,
215 const elfcpp::Sym
<size
, big_endian
>&)
219 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
221 Sized_relobj
<size
, big_endian
>* ,
224 const elfcpp::Rela
<size
,
226 unsigned int , Symbol
*)
231 unsupported_reloc_local(Sized_relobj
<size
, big_endian
>*,
232 unsigned int r_type
);
235 unsupported_reloc_global(Sized_relobj
<size
, big_endian
>*,
236 unsigned int r_type
, Symbol
*);
239 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
240 Target_powerpc
* target
);
243 check_non_pic(Relobj
*, unsigned int r_type
);
245 // Whether we have issued an error about a non-PIC compilation.
246 bool issued_non_pic_error_
;
249 // The class which implements relocation.
253 // Do a relocation. Return false if the caller should not issue
254 // any warnings about this relocation.
256 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
257 Output_section
*, size_t relnum
,
258 const elfcpp::Rela
<size
, big_endian
>&,
259 unsigned int r_type
, const Sized_symbol
<size
>*,
260 const Symbol_value
<size
>*,
262 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
266 // Do a TLS relocation.
268 relocate_tls(const Relocate_info
<size
, big_endian
>*,
269 Target_powerpc
* target
,
270 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
271 unsigned int r_type
, const Sized_symbol
<size
>*,
272 const Symbol_value
<size
>*,
274 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
278 // A class which returns the size required for a relocation type,
279 // used while scanning relocs during a relocatable link.
280 class Relocatable_size_for_reloc
284 get_size_for_reloc(unsigned int, Relobj
*);
287 // Get the GOT section, creating it if necessary.
288 Output_data_got
<size
, big_endian
>*
289 got_section(Symbol_table
*, Layout
*);
294 gold_assert(this->got2_
!= NULL
);
298 // Get the TOC section.
302 gold_assert(this->toc_
!= NULL
);
306 // Create a PLT entry for a global symbol.
308 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
310 // Create a GOT entry for the TLS module index.
312 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
313 Sized_relobj
<size
, big_endian
>* object
);
315 // Get the PLT section.
316 const Output_data_plt_powerpc
<size
, big_endian
>*
319 gold_assert(this->plt_
!= NULL
);
323 // Get the dynamic reloc section, creating it if necessary.
325 rela_dyn_section(Layout
*);
327 // Copy a relocation against a global symbol.
329 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
330 Sized_relobj
<size
, big_endian
>* object
,
331 unsigned int shndx
, Output_section
* output_section
,
332 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
334 this->copy_relocs_
.copy_reloc(symtab
, layout
,
335 symtab
->get_sized_symbol
<size
>(sym
),
336 object
, shndx
, output_section
,
337 reloc
, this->rela_dyn_section(layout
));
340 // Information about this specific target which we pass to the
341 // general Target structure.
342 static Target::Target_info powerpc_info
;
344 // The types of GOT entries needed for this platform.
345 // These values are exposed to the ABI in an incremental link.
346 // Do not renumber existing values without changing the version
347 // number of the .gnu_incremental_inputs section.
350 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
351 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
352 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
356 Output_data_got
<size
, big_endian
>* got_
;
358 Output_data_space
* got2_
;
360 Output_data_space
* toc_
;
362 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
363 // The dynamic reloc section.
364 Reloc_section
* rela_dyn_
;
365 // Relocs saved to avoid a COPY reloc.
366 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
367 // Space for variables copied with a COPY reloc.
368 Output_data_space
* dynbss_
;
369 // Offset of the GOT entry for the TLS module index;
370 unsigned int got_mod_index_offset_
;
374 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
377 true, // is_big_endian
378 elfcpp::EM_PPC
, // 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 4 * 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
392 NULL
, // attributes_section
393 NULL
// attributes_vendor
397 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
400 false, // is_big_endian
401 elfcpp::EM_PPC
, // machine_code
402 false, // has_make_symbol
403 false, // has_resolve
404 false, // has_code_fill
405 true, // is_default_stack_executable
407 "/usr/lib/ld.so.1", // dynamic_linker
408 0x10000000, // default_text_segment_address
409 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
410 4 * 1024, // common_pagesize (overridable by -z common-page-size)
411 elfcpp::SHN_UNDEF
, // small_common_shndx
412 elfcpp::SHN_UNDEF
, // large_common_shndx
413 0, // small_common_section_flags
414 0, // large_common_section_flags
415 NULL
, // attributes_section
416 NULL
// attributes_vendor
420 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
423 true, // is_big_endian
424 elfcpp::EM_PPC64
, // machine_code
425 false, // has_make_symbol
426 false, // has_resolve
427 false, // has_code_fill
428 true, // is_default_stack_executable
430 "/usr/lib/ld.so.1", // dynamic_linker
431 0x10000000, // default_text_segment_address
432 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
433 8 * 1024, // common_pagesize (overridable by -z common-page-size)
434 elfcpp::SHN_UNDEF
, // small_common_shndx
435 elfcpp::SHN_UNDEF
, // large_common_shndx
436 0, // small_common_section_flags
437 0, // large_common_section_flags
438 NULL
, // attributes_section
439 NULL
// attributes_vendor
443 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
446 false, // is_big_endian
447 elfcpp::EM_PPC64
, // machine_code
448 false, // has_make_symbol
449 false, // has_resolve
450 false, // has_code_fill
451 true, // is_default_stack_executable
453 "/usr/lib/ld.so.1", // dynamic_linker
454 0x10000000, // default_text_segment_address
455 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
456 8 * 1024, // common_pagesize (overridable by -z common-page-size)
457 elfcpp::SHN_UNDEF
, // small_common_shndx
458 elfcpp::SHN_UNDEF
, // large_common_shndx
459 0, // small_common_section_flags
460 0, // large_common_section_flags
461 NULL
, // attributes_section
462 NULL
// attributes_vendor
465 template<int size
, bool big_endian
>
466 class Powerpc_relocate_functions
469 // Do a simple relocation with the addend in the relocation.
470 template<int valsize
>
472 rela(unsigned char* view
,
473 unsigned int right_shift
,
474 elfcpp::Elf_Xword dst_mask
,
475 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
476 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
478 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
479 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
480 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
481 Valtype reloc
= ((value
+ addend
) >> right_shift
);
486 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
489 // Do a simple relocation using a symbol value with the addend in
491 template<int valsize
>
493 rela(unsigned char* view
,
494 unsigned int right_shift
,
495 elfcpp::Elf_Xword dst_mask
,
496 const Sized_relobj
<size
, big_endian
>* object
,
497 const Symbol_value
<size
>* psymval
,
498 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
500 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
501 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
502 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
503 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
508 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
511 // Do a simple relocation using a symbol value with the addend in
512 // the relocation, unaligned.
513 template<int valsize
>
515 rela_ua(unsigned char* view
, unsigned int right_shift
,
516 elfcpp::Elf_Xword dst_mask
,
517 const Sized_relobj
<size
, big_endian
>* object
,
518 const Symbol_value
<size
>* psymval
,
519 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
521 typedef typename
elfcpp::Swap_unaligned
<valsize
,
522 big_endian
>::Valtype Valtype
;
523 unsigned char* wv
= view
;
524 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
525 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
530 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
533 // Do a simple PC relative relocation with a Symbol_value with the
534 // addend in the relocation.
535 template<int valsize
>
537 pcrela(unsigned char* view
, unsigned int right_shift
,
538 elfcpp::Elf_Xword dst_mask
,
539 const Sized_relobj
<size
, big_endian
>* object
,
540 const Symbol_value
<size
>* psymval
,
541 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
542 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
544 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
545 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
546 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
547 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
553 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
556 template<int valsize
>
558 pcrela_unaligned(unsigned char* view
,
559 const Sized_relobj
<size
, big_endian
>* object
,
560 const Symbol_value
<size
>* psymval
,
561 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
562 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
564 typedef typename
elfcpp::Swap_unaligned
<valsize
,
565 big_endian
>::Valtype Valtype
;
566 unsigned char* wv
= view
;
567 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
569 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
572 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
573 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
575 // R_POWERPC_REL32: (Symbol + Addend - Address)
577 rel32(unsigned char* view
,
578 const Sized_relobj
<size
, big_endian
>* object
,
579 const Symbol_value
<size
>* psymval
,
580 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
581 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
582 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
584 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
586 rel24(unsigned char* view
,
587 const Sized_relobj
<size
, big_endian
>* object
,
588 const Symbol_value
<size
>* psymval
,
589 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
590 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
592 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
593 psymval
, addend
, address
);
596 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
598 rel14(unsigned char* view
,
599 const Sized_relobj
<size
, big_endian
>* object
,
600 const Symbol_value
<size
>* psymval
,
601 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
604 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
605 psymval
, addend
, address
);
608 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
610 addr16(unsigned char* view
,
611 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
613 { This_reloc::rela16(view
, value
, addend
); }
616 addr16(unsigned char* view
,
617 const Sized_relobj
<size
, big_endian
>* object
,
618 const Symbol_value
<size
>* psymval
,
619 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
620 { This_reloc::rela16(view
, object
, psymval
, addend
); }
622 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
624 addr16_ds(unsigned char* view
,
625 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
626 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
628 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
631 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
633 addr16_lo(unsigned char* view
,
634 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
635 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
636 { This_reloc::rela16(view
, value
, addend
); }
639 addr16_lo(unsigned char* view
,
640 const Sized_relobj
<size
, big_endian
>* object
,
641 const Symbol_value
<size
>* psymval
,
642 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
643 { This_reloc::rela16(view
, object
, psymval
, addend
); }
645 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
647 addr16_hi(unsigned char* view
,
648 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
649 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
651 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
655 addr16_hi(unsigned char* view
,
656 const Sized_relobj
<size
, big_endian
>* object
,
657 const Symbol_value
<size
>* psymval
,
658 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
660 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
663 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
664 // final value of the low 16 bits of the
665 // relocation is negative, add one.
667 addr16_ha(unsigned char* view
,
668 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
669 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
671 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
673 reloc
= value
+ addend
;
679 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
683 addr16_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
)
688 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
690 reloc
= psymval
->value(object
, addend
);
696 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
699 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
701 rel16(unsigned char* view
,
702 const Sized_relobj
<size
, big_endian
>* object
,
703 const Symbol_value
<size
>* psymval
,
704 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
705 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
706 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
708 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
710 rel16_lo(unsigned char* view
,
711 const Sized_relobj
<size
, big_endian
>* object
,
712 const Symbol_value
<size
>* psymval
,
713 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
714 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
715 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
717 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
719 rel16_hi(unsigned char* view
,
720 const Sized_relobj
<size
, big_endian
>* object
,
721 const Symbol_value
<size
>* psymval
,
722 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
723 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
725 This::template pcrela
<16>(view
, 16, 0xffff, object
,
726 psymval
, addend
, address
);
729 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
730 // final value of the low 16 bits of the
731 // relocation is negative, add one.
733 rel16_ha(unsigned char* view
,
734 const Sized_relobj
<size
, big_endian
>* object
,
735 const Symbol_value
<size
>* psymval
,
736 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
737 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
739 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
741 reloc
= (psymval
->value(object
, addend
) - address
);
746 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
750 // Get the GOT section, creating it if necessary.
752 template<int size
, bool big_endian
>
753 Output_data_got
<size
, big_endian
>*
754 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
757 if (this->got_
== NULL
)
759 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
761 this->got_
= new Output_data_got
<size
, big_endian
>();
763 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
764 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
765 this->got_
, ORDER_DATA
, false);
767 // Create the GOT2 or TOC in the .got section.
770 this->got2_
= new Output_data_space(4, "** GOT2");
771 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
774 this->got2_
, ORDER_DATA
, false);
778 this->toc_
= new Output_data_space(8, "** TOC");
779 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
782 this->toc_
, ORDER_DATA
, false);
785 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
786 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
787 Symbol_table::PREDEFINED
,
789 0, 0, elfcpp::STT_OBJECT
,
791 elfcpp::STV_HIDDEN
, 0,
798 // Get the dynamic reloc section, creating it if necessary.
800 template<int size
, bool big_endian
>
801 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
802 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
804 if (this->rela_dyn_
== NULL
)
806 gold_assert(layout
!= NULL
);
807 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
808 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
809 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
810 ORDER_DYNAMIC_RELOCS
, false);
812 return this->rela_dyn_
;
815 // A class to handle the PLT data.
817 template<int size
, bool big_endian
>
818 class Output_data_plt_powerpc
: public Output_section_data
821 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
822 size
, big_endian
> Reloc_section
;
824 Output_data_plt_powerpc(Layout
*);
826 // Add an entry to the PLT.
827 void add_entry(Symbol
* gsym
);
829 // Return the .rela.plt section data.
830 const Reloc_section
* rel_plt() const
835 // Return the number of PLT entries.
838 { return this->count_
; }
840 // Return the offset of the first non-reserved PLT entry.
842 first_plt_entry_offset()
843 { return 4 * base_plt_entry_size
; }
845 // Return the size of a PLT entry.
848 { return base_plt_entry_size
; }
851 void do_adjust_output_section(Output_section
* os
);
854 // The size of an entry in the PLT.
855 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
857 // Set the final size.
859 set_final_data_size()
861 unsigned int full_count
= this->count_
+ 4;
863 this->set_data_size(full_count
* base_plt_entry_size
);
866 // Write out the PLT data.
868 do_write(Output_file
*);
870 // The reloc section.
872 // The number of PLT entries.
876 // Create the PLT section. The ordinary .got section is an argument,
877 // since we need to refer to the start.
879 template<int size
, bool big_endian
>
880 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
881 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
883 this->rel_
= new Reloc_section(false);
884 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
885 elfcpp::SHF_ALLOC
, this->rel_
,
886 ORDER_DYNAMIC_PLT_RELOCS
, false);
889 template<int size
, bool big_endian
>
891 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
896 // Add an entry to the PLT.
898 template<int size
, bool big_endian
>
900 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
902 gold_assert(!gsym
->has_plt_offset());
903 unsigned int index
= this->count_
+ + 4;
904 section_offset_type plt_offset
;
907 plt_offset
= index
* base_plt_entry_size
;
911 gsym
->set_plt_offset(plt_offset
);
915 gsym
->set_needs_dynsym_entry();
916 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
920 static const unsigned int addis_11_11
= 0x3d6b0000;
921 static const unsigned int addis_11_30
= 0x3d7e0000;
922 static const unsigned int addis_12_12
= 0x3d8c0000;
923 static const unsigned int addi_11_11
= 0x396b0000;
924 static const unsigned int add_0_11_11
= 0x7c0b5a14;
925 static const unsigned int add_11_0_11
= 0x7d605a14;
926 static const unsigned int b
= 0x48000000;
927 static const unsigned int bcl_20_31
= 0x429f0005;
928 static const unsigned int bctr
= 0x4e800420;
929 static const unsigned int lis_11
= 0x3d600000;
930 static const unsigned int lis_12
= 0x3d800000;
931 static const unsigned int lwzu_0_12
= 0x840c0000;
932 static const unsigned int lwz_0_12
= 0x800c0000;
933 static const unsigned int lwz_11_11
= 0x816b0000;
934 static const unsigned int lwz_11_30
= 0x817e0000;
935 static const unsigned int lwz_12_12
= 0x818c0000;
936 static const unsigned int mflr_0
= 0x7c0802a6;
937 static const unsigned int mflr_12
= 0x7d8802a6;
938 static const unsigned int mtctr_0
= 0x7c0903a6;
939 static const unsigned int mtctr_11
= 0x7d6903a6;
940 static const unsigned int mtlr_0
= 0x7c0803a6;
941 static const unsigned int nop
= 0x60000000;
942 static const unsigned int sub_11_11_12
= 0x7d6c5850;
944 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
945 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
946 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
947 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
948 /* ld %r11,xxx+16@l(%r12) */
951 // Write out the PLT.
953 template<int size
, bool big_endian
>
955 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
957 const off_t offset
= this->offset();
958 const section_size_type oview_size
=
959 convert_to_section_size_type(this->data_size());
960 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
961 unsigned char* pov
= oview
;
963 memset(pov
, 0, base_plt_entry_size
* 4);
964 pov
+= base_plt_entry_size
* 4;
966 unsigned int plt_offset
= base_plt_entry_size
* 4;
967 const unsigned int count
= this->count_
;
971 for (unsigned int i
= 0; i
< count
; i
++)
977 for (unsigned int i
= 0; i
< count
; i
++)
979 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
980 lwz_11_30
+ plt_offset
);
981 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
982 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
983 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
984 pov
+= base_plt_entry_size
;
985 plt_offset
+= base_plt_entry_size
;
989 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
991 of
->write_output_view(offset
, oview_size
, oview
);
994 // Create a PLT entry for a global symbol.
996 template<int size
, bool big_endian
>
998 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
1002 if (gsym
->has_plt_offset())
1005 if (this->plt_
== NULL
)
1007 // Create the GOT section first.
1008 this->got_section(symtab
, layout
);
1010 // Ensure that .rela.dyn always appears before .rela.plt This is
1011 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1012 // needs to include .rela.plt in it's range.
1013 this->rela_dyn_section(layout
);
1015 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
1016 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1018 | elfcpp::SHF_EXECINSTR
1019 | elfcpp::SHF_WRITE
),
1020 this->plt_
, ORDER_PLT
, false);
1022 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1023 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
1024 Symbol_table::PREDEFINED
,
1026 0, 0, elfcpp::STT_OBJECT
,
1028 elfcpp::STV_HIDDEN
, 0,
1032 this->plt_
->add_entry(gsym
);
1035 // Return the number of entries in the PLT.
1037 template<int size
, bool big_endian
>
1039 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
1041 if (this->plt_
== NULL
)
1043 return this->plt_
->entry_count();
1046 // Return the offset of the first non-reserved PLT entry.
1048 template<int size
, bool big_endian
>
1050 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
1052 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
1055 // Return the size of each PLT entry.
1057 template<int size
, bool big_endian
>
1059 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
1061 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
1064 // Create a GOT entry for the TLS module index.
1066 template<int size
, bool big_endian
>
1068 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
1070 Sized_relobj
<size
, big_endian
>* object
)
1072 if (this->got_mod_index_offset_
== -1U)
1074 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1075 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1076 Output_data_got
<size
, big_endian
>* got
;
1077 unsigned int got_offset
;
1079 got
= this->got_section(symtab
, layout
);
1080 got_offset
= got
->add_constant(0);
1081 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
1083 got
->add_constant(0);
1084 this->got_mod_index_offset_
= got_offset
;
1086 return this->got_mod_index_offset_
;
1089 // Optimize the TLS relocation type based on what we know about the
1090 // symbol. IS_FINAL is true if the final address of this symbol is
1091 // known at link time.
1093 static tls::Tls_optimization
1094 optimize_tls_reloc(bool /* is_final */, int r_type
)
1096 // If we are generating a shared library, then we can't do anything
1098 if (parameters
->options().shared())
1099 return tls::TLSOPT_NONE
;
1108 // Report an unsupported relocation against a local symbol.
1110 template<int size
, bool big_endian
>
1112 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1113 Sized_relobj
<size
, big_endian
>* object
,
1114 unsigned int r_type
)
1116 gold_error(_("%s: unsupported reloc %u against local symbol"),
1117 object
->name().c_str(), r_type
);
1120 // We are about to emit a dynamic relocation of type R_TYPE. If the
1121 // dynamic linker does not support it, issue an error.
1123 template<int size
, bool big_endian
>
1125 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1126 unsigned int r_type
)
1128 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1130 // These are the relocation types supported by glibc for both 32-bit
1131 // and 64-bit powerpc.
1134 case elfcpp::R_POWERPC_RELATIVE
:
1135 case elfcpp::R_POWERPC_GLOB_DAT
:
1136 case elfcpp::R_POWERPC_DTPMOD
:
1137 case elfcpp::R_POWERPC_DTPREL
:
1138 case elfcpp::R_POWERPC_TPREL
:
1139 case elfcpp::R_POWERPC_JMP_SLOT
:
1140 case elfcpp::R_POWERPC_COPY
:
1141 case elfcpp::R_POWERPC_ADDR32
:
1142 case elfcpp::R_POWERPC_ADDR24
:
1143 case elfcpp::R_POWERPC_REL24
:
1154 // These are the relocation types supported only on 64-bit.
1155 case elfcpp::R_PPC64_ADDR64
:
1156 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1157 case elfcpp::R_PPC64_TPREL16_DS
:
1158 case elfcpp::R_POWERPC_TPREL16
:
1159 case elfcpp::R_POWERPC_TPREL16_LO
:
1160 case elfcpp::R_POWERPC_TPREL16_HI
:
1161 case elfcpp::R_POWERPC_TPREL16_HA
:
1162 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1163 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1164 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1165 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1166 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1167 case elfcpp::R_POWERPC_ADDR16_LO
:
1168 case elfcpp::R_POWERPC_ADDR16_HI
:
1169 case elfcpp::R_POWERPC_ADDR16_HA
:
1170 case elfcpp::R_POWERPC_ADDR30
:
1171 case elfcpp::R_PPC64_UADDR64
:
1172 case elfcpp::R_POWERPC_UADDR32
:
1173 case elfcpp::R_POWERPC_ADDR16
:
1174 case elfcpp::R_POWERPC_UADDR16
:
1175 case elfcpp::R_PPC64_ADDR16_DS
:
1176 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1177 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1178 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1179 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1180 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1181 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1182 case elfcpp::R_POWERPC_REL32
:
1183 case elfcpp::R_PPC64_REL64
:
1194 // These are the relocation types supported only on 32-bit.
1201 // This prevents us from issuing more than one error per reloc
1202 // section. But we can still wind up issuing more than one
1203 // error per object file.
1204 if (this->issued_non_pic_error_
)
1206 gold_assert(parameters
->options().output_is_position_independent());
1207 object
->error(_("requires unsupported dynamic reloc; "
1208 "recompile with -fPIC"));
1209 this->issued_non_pic_error_
= true;
1213 // Scan a relocation for a local symbol.
1215 template<int size
, bool big_endian
>
1217 Target_powerpc
<size
, big_endian
>::Scan::local(
1218 Symbol_table
* symtab
,
1220 Target_powerpc
<size
, big_endian
>* target
,
1221 Sized_relobj
<size
, big_endian
>* object
,
1222 unsigned int data_shndx
,
1223 Output_section
* output_section
,
1224 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1225 unsigned int r_type
,
1226 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1230 case elfcpp::R_POWERPC_NONE
:
1231 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1232 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1235 case elfcpp::R_PPC64_ADDR64
:
1236 case elfcpp::R_POWERPC_ADDR32
:
1237 case elfcpp::R_POWERPC_ADDR16_HA
:
1238 case elfcpp::R_POWERPC_ADDR16_LO
:
1239 // If building a shared library (or a position-independent
1240 // executable), we need to create a dynamic relocation for
1242 if (parameters
->options().output_is_position_independent())
1244 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1246 check_non_pic(object
, r_type
);
1247 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1249 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1250 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1251 data_shndx
, reloc
.get_r_offset(),
1252 reloc
.get_r_addend());
1256 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1257 gold_assert(lsym
.get_st_value() == 0);
1258 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1259 output_section
, data_shndx
,
1260 reloc
.get_r_offset(),
1261 reloc
.get_r_addend());
1266 case elfcpp::R_POWERPC_REL24
:
1267 case elfcpp::R_PPC_LOCAL24PC
:
1268 case elfcpp::R_POWERPC_REL32
:
1269 case elfcpp::R_PPC_REL16_LO
:
1270 case elfcpp::R_PPC_REL16_HA
:
1273 case elfcpp::R_POWERPC_GOT16
:
1274 case elfcpp::R_POWERPC_GOT16_LO
:
1275 case elfcpp::R_POWERPC_GOT16_HI
:
1276 case elfcpp::R_POWERPC_GOT16_HA
:
1277 case elfcpp::R_PPC64_TOC16
:
1278 case elfcpp::R_PPC64_TOC16_LO
:
1279 case elfcpp::R_PPC64_TOC16_HI
:
1280 case elfcpp::R_PPC64_TOC16_HA
:
1281 case elfcpp::R_PPC64_TOC16_DS
:
1282 case elfcpp::R_PPC64_TOC16_LO_DS
:
1284 // The symbol requires a GOT entry.
1285 Output_data_got
<size
, big_endian
>* got
;
1288 got
= target
->got_section(symtab
, layout
);
1289 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1291 // If we are generating a shared object, we need to add a
1292 // dynamic relocation for this symbol's GOT entry.
1293 if (parameters
->options().output_is_position_independent())
1295 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1297 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1300 off
= got
->add_constant(0);
1301 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1302 rela_dyn
->add_local_relative(object
, r_sym
,
1303 elfcpp::R_POWERPC_RELATIVE
,
1308 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1312 case elfcpp::R_PPC64_TOC
:
1313 // We need a GOT section.
1314 target
->got_section(symtab
, layout
);
1317 // These are relocations which should only be seen by the
1318 // dynamic linker, and should never be seen here.
1319 case elfcpp::R_POWERPC_COPY
:
1320 case elfcpp::R_POWERPC_GLOB_DAT
:
1321 case elfcpp::R_POWERPC_JMP_SLOT
:
1322 case elfcpp::R_POWERPC_RELATIVE
:
1323 case elfcpp::R_POWERPC_DTPMOD
:
1324 gold_error(_("%s: unexpected reloc %u in object file"),
1325 object
->name().c_str(), r_type
);
1329 unsupported_reloc_local(object
, r_type
);
1334 // Report an unsupported relocation against a global symbol.
1336 template<int size
, bool big_endian
>
1338 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1339 Sized_relobj
<size
, big_endian
>* object
,
1340 unsigned int r_type
,
1343 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1344 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1347 // Scan a relocation for a global symbol.
1349 template<int size
, bool big_endian
>
1351 Target_powerpc
<size
, big_endian
>::Scan::global(
1352 Symbol_table
* symtab
,
1354 Target_powerpc
<size
, big_endian
>* target
,
1355 Sized_relobj
<size
, big_endian
>* object
,
1356 unsigned int data_shndx
,
1357 Output_section
* output_section
,
1358 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1359 unsigned int r_type
,
1364 case elfcpp::R_POWERPC_NONE
:
1365 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1366 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1369 case elfcpp::R_PPC_PLTREL24
:
1370 // If the symbol is fully resolved, this is just a PC32 reloc.
1371 // Otherwise we need a PLT entry.
1372 if (gsym
->final_value_is_known())
1374 // If building a shared library, we can also skip the PLT entry
1375 // if the symbol is defined in the output file and is protected
1377 if (gsym
->is_defined()
1378 && !gsym
->is_from_dynobj()
1379 && !gsym
->is_preemptible())
1381 target
->make_plt_entry(symtab
, layout
, gsym
);
1384 case elfcpp::R_POWERPC_ADDR16
:
1385 case elfcpp::R_POWERPC_ADDR16_LO
:
1386 case elfcpp::R_POWERPC_ADDR16_HI
:
1387 case elfcpp::R_POWERPC_ADDR16_HA
:
1388 case elfcpp::R_POWERPC_ADDR32
:
1389 case elfcpp::R_PPC64_ADDR64
:
1391 // Make a PLT entry if necessary.
1392 if (gsym
->needs_plt_entry())
1394 target
->make_plt_entry(symtab
, layout
, gsym
);
1395 // Since this is not a PC-relative relocation, we may be
1396 // taking the address of a function. In that case we need to
1397 // set the entry in the dynamic symbol table to the address of
1399 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1400 gsym
->set_needs_dynsym_value();
1402 // Make a dynamic relocation if necessary.
1403 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1405 if (gsym
->may_need_copy_reloc())
1407 target
->copy_reloc(symtab
, layout
, object
,
1408 data_shndx
, output_section
, gsym
, reloc
);
1410 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1411 || r_type
== elfcpp::R_PPC64_ADDR64
)
1412 && gsym
->can_use_relative_reloc(false))
1414 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1415 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1416 output_section
, object
,
1417 data_shndx
, reloc
.get_r_offset(),
1418 reloc
.get_r_addend());
1422 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1424 check_non_pic(object
, r_type
);
1425 if (gsym
->is_from_dynobj()
1426 || gsym
->is_undefined()
1427 || gsym
->is_preemptible())
1428 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1430 reloc
.get_r_offset(),
1431 reloc
.get_r_addend());
1433 rela_dyn
->add_global_relative(gsym
, r_type
,
1434 output_section
, object
,
1436 reloc
.get_r_offset(),
1437 reloc
.get_r_addend());
1443 case elfcpp::R_POWERPC_REL24
:
1444 case elfcpp::R_PPC_LOCAL24PC
:
1445 case elfcpp::R_PPC_REL16
:
1446 case elfcpp::R_PPC_REL16_LO
:
1447 case elfcpp::R_PPC_REL16_HI
:
1448 case elfcpp::R_PPC_REL16_HA
:
1450 if (gsym
->needs_plt_entry())
1451 target
->make_plt_entry(symtab
, layout
, gsym
);
1452 // Make a dynamic relocation if necessary.
1453 int flags
= Symbol::NON_PIC_REF
;
1454 if (gsym
->type() == elfcpp::STT_FUNC
)
1455 flags
|= Symbol::FUNCTION_CALL
;
1456 if (gsym
->needs_dynamic_reloc(flags
))
1458 if (gsym
->may_need_copy_reloc())
1460 target
->copy_reloc(symtab
, layout
, object
,
1461 data_shndx
, output_section
, gsym
,
1466 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1467 check_non_pic(object
, r_type
);
1468 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1469 data_shndx
, reloc
.get_r_offset(),
1470 reloc
.get_r_addend());
1476 case elfcpp::R_POWERPC_GOT16
:
1477 case elfcpp::R_POWERPC_GOT16_LO
:
1478 case elfcpp::R_POWERPC_GOT16_HI
:
1479 case elfcpp::R_POWERPC_GOT16_HA
:
1480 case elfcpp::R_PPC64_TOC16
:
1481 case elfcpp::R_PPC64_TOC16_LO
:
1482 case elfcpp::R_PPC64_TOC16_HI
:
1483 case elfcpp::R_PPC64_TOC16_HA
:
1484 case elfcpp::R_PPC64_TOC16_DS
:
1485 case elfcpp::R_PPC64_TOC16_LO_DS
:
1487 // The symbol requires a GOT entry.
1488 Output_data_got
<size
, big_endian
>* got
;
1490 got
= target
->got_section(symtab
, layout
);
1491 if (gsym
->final_value_is_known())
1492 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1495 // If this symbol is not fully resolved, we need to add a
1496 // dynamic relocation for it.
1497 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1498 if (gsym
->is_from_dynobj()
1499 || gsym
->is_undefined()
1500 || gsym
->is_preemptible())
1501 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1502 elfcpp::R_POWERPC_GLOB_DAT
);
1503 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1505 unsigned int off
= got
->add_constant(0);
1507 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1508 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1515 case elfcpp::R_PPC64_TOC
:
1516 // We need a GOT section.
1517 target
->got_section(symtab
, layout
);
1520 case elfcpp::R_POWERPC_GOT_TPREL16
:
1521 case elfcpp::R_POWERPC_TLS
:
1525 // These are relocations which should only be seen by the
1526 // dynamic linker, and should never be seen here.
1527 case elfcpp::R_POWERPC_COPY
:
1528 case elfcpp::R_POWERPC_GLOB_DAT
:
1529 case elfcpp::R_POWERPC_JMP_SLOT
:
1530 case elfcpp::R_POWERPC_RELATIVE
:
1531 case elfcpp::R_POWERPC_DTPMOD
:
1532 gold_error(_("%s: unexpected reloc %u in object file"),
1533 object
->name().c_str(), r_type
);
1537 unsupported_reloc_global(object
, r_type
, gsym
);
1542 // Process relocations for gc.
1544 template<int size
, bool big_endian
>
1546 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1547 Symbol_table
* symtab
,
1549 Sized_relobj
<size
, big_endian
>* object
,
1550 unsigned int data_shndx
,
1552 const unsigned char* prelocs
,
1554 Output_section
* output_section
,
1555 bool needs_special_offset_handling
,
1556 size_t local_symbol_count
,
1557 const unsigned char* plocal_symbols
)
1559 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1560 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1562 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
1563 typename
Target_powerpc::Relocatable_size_for_reloc
>(
1572 needs_special_offset_handling
,
1577 // Scan relocations for a section.
1579 template<int size
, bool big_endian
>
1581 Target_powerpc
<size
, big_endian
>::scan_relocs(
1582 Symbol_table
* symtab
,
1584 Sized_relobj
<size
, big_endian
>* object
,
1585 unsigned int data_shndx
,
1586 unsigned int sh_type
,
1587 const unsigned char* prelocs
,
1589 Output_section
* output_section
,
1590 bool needs_special_offset_handling
,
1591 size_t local_symbol_count
,
1592 const unsigned char* plocal_symbols
)
1594 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1595 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1596 static Output_data_space
* sdata
;
1598 if (sh_type
== elfcpp::SHT_REL
)
1600 gold_error(_("%s: unsupported REL reloc section"),
1601 object
->name().c_str());
1605 // Define _SDA_BASE_ at the start of the .sdata section.
1608 // layout->find_output_section(".sdata") == NULL
1609 sdata
= new Output_data_space(4, "** sdata");
1610 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1612 | elfcpp::SHF_WRITE
,
1616 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1617 Symbol_table::PREDEFINED
,
1622 elfcpp::STV_HIDDEN
, 0,
1626 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1635 needs_special_offset_handling
,
1640 // Finalize the sections.
1642 template<int size
, bool big_endian
>
1644 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
1646 const Input_objects
*,
1649 // Fill in some more dynamic tags.
1650 const Reloc_section
* rel_plt
= (this->plt_
== NULL
1652 : this->plt_
->rel_plt());
1653 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
1654 this->rela_dyn_
, true, size
== 32);
1656 // Emit any relocs we saved in an attempt to avoid generating COPY
1658 if (this->copy_relocs_
.any_saved_relocs())
1659 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1662 // Perform a relocation.
1664 template<int size
, bool big_endian
>
1666 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1667 const Relocate_info
<size
, big_endian
>* relinfo
,
1668 Target_powerpc
* target
,
1671 const elfcpp::Rela
<size
, big_endian
>& rela
,
1672 unsigned int r_type
,
1673 const Sized_symbol
<size
>* gsym
,
1674 const Symbol_value
<size
>* psymval
,
1675 unsigned char* view
,
1676 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1677 section_size_type
/* view_size */)
1679 const unsigned int toc_base_offset
= 0x8000;
1680 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1682 // Pick the value to use for symbols defined in shared objects.
1683 Symbol_value
<size
> symval
;
1685 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1686 || r_type
== elfcpp::R_PPC_LOCAL24PC
1687 || r_type
== elfcpp::R_PPC_REL16
1688 || r_type
== elfcpp::R_PPC_REL16_LO
1689 || r_type
== elfcpp::R_PPC_REL16_HI
1690 || r_type
== elfcpp::R_PPC_REL16_HA
))
1692 elfcpp::Elf_Xword value
;
1694 value
= target
->plt_section()->address() + gsym
->plt_offset();
1696 symval
.set_output_value(value
);
1701 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1702 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1704 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1705 // pointer points to the beginning, not the end, of the table.
1706 // So we just use the plain offset.
1707 unsigned int got_offset
= 0;
1708 unsigned int got2_offset
= 0;
1711 case elfcpp::R_PPC64_TOC16
:
1712 case elfcpp::R_PPC64_TOC16_LO
:
1713 case elfcpp::R_PPC64_TOC16_HI
:
1714 case elfcpp::R_PPC64_TOC16_HA
:
1715 case elfcpp::R_PPC64_TOC16_DS
:
1716 case elfcpp::R_PPC64_TOC16_LO_DS
:
1717 // Subtract the TOC base address.
1718 addend
-= target
->toc_section()->address() + toc_base_offset
;
1721 case elfcpp::R_POWERPC_GOT16
:
1722 case elfcpp::R_POWERPC_GOT16_LO
:
1723 case elfcpp::R_POWERPC_GOT16_HI
:
1724 case elfcpp::R_POWERPC_GOT16_HA
:
1725 case elfcpp::R_PPC64_GOT16_DS
:
1726 case elfcpp::R_PPC64_GOT16_LO_DS
:
1729 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1730 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1734 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1735 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1736 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1740 // R_PPC_PLTREL24 is rather special. If non-zero,
1741 // the addend specifies the GOT pointer offset within .got2.
1742 case elfcpp::R_PPC_PLTREL24
:
1743 if (addend
>= 32768)
1745 Output_data_space
* got2
;
1746 got2
= target
->got2_section();
1747 got2_offset
= got2
->offset();
1748 addend
+= got2_offset
;
1758 case elfcpp::R_POWERPC_NONE
:
1759 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1760 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1763 case elfcpp::R_POWERPC_REL32
:
1764 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1767 case elfcpp::R_POWERPC_REL24
:
1768 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1771 case elfcpp::R_POWERPC_REL14
:
1772 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1775 case elfcpp::R_PPC_PLTREL24
:
1776 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1779 case elfcpp::R_PPC_LOCAL24PC
:
1780 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1783 case elfcpp::R_PPC64_ADDR64
:
1784 if (!parameters
->options().output_is_position_independent())
1785 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1789 case elfcpp::R_POWERPC_ADDR32
:
1790 if (!parameters
->options().output_is_position_independent())
1791 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1795 case elfcpp::R_POWERPC_ADDR16_LO
:
1796 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1799 case elfcpp::R_POWERPC_ADDR16_HI
:
1800 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1803 case elfcpp::R_POWERPC_ADDR16_HA
:
1804 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1807 case elfcpp::R_PPC_REL16_LO
:
1808 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1811 case elfcpp::R_PPC_REL16_HI
:
1812 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1815 case elfcpp::R_PPC_REL16_HA
:
1816 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1819 case elfcpp::R_POWERPC_GOT16
:
1820 Reloc::addr16(view
, got_offset
, addend
);
1823 case elfcpp::R_POWERPC_GOT16_LO
:
1824 Reloc::addr16_lo(view
, got_offset
, addend
);
1827 case elfcpp::R_POWERPC_GOT16_HI
:
1828 Reloc::addr16_hi(view
, got_offset
, addend
);
1831 case elfcpp::R_POWERPC_GOT16_HA
:
1832 Reloc::addr16_ha(view
, got_offset
, addend
);
1835 case elfcpp::R_PPC64_TOC16
:
1836 Reloc::addr16(view
, got_offset
, addend
);
1839 case elfcpp::R_PPC64_TOC16_LO
:
1840 Reloc::addr16_lo(view
, got_offset
, addend
);
1843 case elfcpp::R_PPC64_TOC16_HI
:
1844 Reloc::addr16_hi(view
, got_offset
, addend
);
1847 case elfcpp::R_PPC64_TOC16_HA
:
1848 Reloc::addr16_ha(view
, got_offset
, addend
);
1851 case elfcpp::R_PPC64_TOC16_DS
:
1852 case elfcpp::R_PPC64_TOC16_LO_DS
:
1853 Reloc::addr16_ds(view
, got_offset
, addend
);
1856 case elfcpp::R_PPC64_TOC
:
1858 elfcpp::Elf_types
<64>::Elf_Addr value
;
1859 value
= target
->toc_section()->address() + toc_base_offset
;
1860 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1864 case elfcpp::R_POWERPC_COPY
:
1865 case elfcpp::R_POWERPC_GLOB_DAT
:
1866 case elfcpp::R_POWERPC_JMP_SLOT
:
1867 case elfcpp::R_POWERPC_RELATIVE
:
1868 // This is an outstanding tls reloc, which is unexpected when
1870 case elfcpp::R_POWERPC_DTPMOD
:
1871 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1872 _("unexpected reloc %u in object file"),
1877 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1878 _("unsupported reloc %u"),
1886 // Perform a TLS relocation.
1888 template<int size
, bool big_endian
>
1890 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1891 const Relocate_info
<size
, big_endian
>* relinfo
,
1892 Target_powerpc
<size
, big_endian
>* target
,
1894 const elfcpp::Rela
<size
, big_endian
>& rela
,
1895 unsigned int r_type
,
1896 const Sized_symbol
<size
>* gsym
,
1897 const Symbol_value
<size
>* psymval
,
1898 unsigned char* view
,
1899 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1902 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1903 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1904 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1906 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1907 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1909 const bool is_final
=
1911 ? !parameters
->options().output_is_position_independent()
1912 : gsym
->final_value_is_known());
1913 const tls::Tls_optimization optimized_type
1914 = optimize_tls_reloc(is_final
, r_type
);
1922 // Relocate section data.
1924 template<int size
, bool big_endian
>
1926 Target_powerpc
<size
, big_endian
>::relocate_section(
1927 const Relocate_info
<size
, big_endian
>* relinfo
,
1928 unsigned int sh_type
,
1929 const unsigned char* prelocs
,
1931 Output_section
* output_section
,
1932 bool needs_special_offset_handling
,
1933 unsigned char* view
,
1934 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1935 section_size_type view_size
,
1936 const Reloc_symbol_changes
* reloc_symbol_changes
)
1938 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1939 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1941 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1943 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1950 needs_special_offset_handling
,
1954 reloc_symbol_changes
);
1957 // Return the size of a relocation while scanning during a relocatable
1960 template<int size
, bool big_endian
>
1962 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1966 // We are always SHT_RELA, so we should never get here.
1971 // Scan the relocs during a relocatable link.
1973 template<int size
, bool big_endian
>
1975 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1976 Symbol_table
* symtab
,
1978 Sized_relobj
<size
, big_endian
>* object
,
1979 unsigned int data_shndx
,
1980 unsigned int sh_type
,
1981 const unsigned char* prelocs
,
1983 Output_section
* output_section
,
1984 bool needs_special_offset_handling
,
1985 size_t local_symbol_count
,
1986 const unsigned char* plocal_symbols
,
1987 Relocatable_relocs
* rr
)
1989 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1991 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1992 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1994 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1995 Scan_relocatable_relocs
>(
2003 needs_special_offset_handling
,
2009 // Relocate a section during a relocatable link.
2011 template<int size
, bool big_endian
>
2013 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
2014 const Relocate_info
<size
, big_endian
>* relinfo
,
2015 unsigned int sh_type
,
2016 const unsigned char* prelocs
,
2018 Output_section
* output_section
,
2019 off_t offset_in_output_section
,
2020 const Relocatable_relocs
* rr
,
2021 unsigned char* view
,
2022 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2023 section_size_type view_size
,
2024 unsigned char* reloc_view
,
2025 section_size_type reloc_view_size
)
2027 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2029 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
2034 offset_in_output_section
,
2043 // Return the value to use for a dynamic which requires special
2044 // treatment. This is how we support equality comparisons of function
2045 // pointers across shared library boundaries, as described in the
2046 // processor specific ABI supplement.
2048 template<int size
, bool big_endian
>
2050 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
2052 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2053 return this->plt_section()->address() + gsym
->plt_offset();
2056 // The selector for powerpc object files.
2058 template<int size
, bool big_endian
>
2059 class Target_selector_powerpc
: public Target_selector
2062 Target_selector_powerpc()
2063 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
2065 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
2066 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
2069 Target
* do_recognize(int machine
, int, int)
2074 if (machine
!= elfcpp::EM_PPC64
)
2079 if (machine
!= elfcpp::EM_PPC
)
2087 return this->instantiate_target();
2090 Target
* do_instantiate_target()
2091 { return new Target_powerpc
<size
, big_endian
>(); }
2094 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2095 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2096 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2097 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2099 } // End anonymous namespace.