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"
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();
162 // The class which scans relocations.
167 : issued_non_pic_error_(false)
171 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
172 Sized_relobj
<size
, big_endian
>* object
,
173 unsigned int data_shndx
,
174 Output_section
* output_section
,
175 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
176 const elfcpp::Sym
<size
, big_endian
>& lsym
);
179 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
180 Sized_relobj
<size
, big_endian
>* object
,
181 unsigned int data_shndx
,
182 Output_section
* output_section
,
183 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
188 unsupported_reloc_local(Sized_relobj
<size
, big_endian
>*,
189 unsigned int r_type
);
192 unsupported_reloc_global(Sized_relobj
<size
, big_endian
>*,
193 unsigned int r_type
, Symbol
*);
196 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
197 Target_powerpc
* target
);
200 check_non_pic(Relobj
*, unsigned int r_type
);
202 // Whether we have issued an error about a non-PIC compilation.
203 bool issued_non_pic_error_
;
206 // The class which implements relocation.
210 // Do a relocation. Return false if the caller should not issue
211 // any warnings about this relocation.
213 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
214 Output_section
*, size_t relnum
,
215 const elfcpp::Rela
<size
, big_endian
>&,
216 unsigned int r_type
, const Sized_symbol
<size
>*,
217 const Symbol_value
<size
>*,
219 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
223 // Do a TLS relocation.
225 relocate_tls(const Relocate_info
<size
, big_endian
>*,
226 Target_powerpc
* target
,
227 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
228 unsigned int r_type
, const Sized_symbol
<size
>*,
229 const Symbol_value
<size
>*,
231 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
235 // A class which returns the size required for a relocation type,
236 // used while scanning relocs during a relocatable link.
237 class Relocatable_size_for_reloc
241 get_size_for_reloc(unsigned int, Relobj
*);
244 // Get the GOT section, creating it if necessary.
245 Output_data_got
<size
, big_endian
>*
246 got_section(Symbol_table
*, Layout
*);
251 gold_assert (this->got2_
!= NULL
);
255 // Get the TOC section.
259 gold_assert (this->toc_
!= NULL
);
263 // Create a PLT entry for a global symbol.
265 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
267 // Create a GOT entry for the TLS module index.
269 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
270 Sized_relobj
<size
, big_endian
>* object
);
272 // Get the PLT section.
273 const Output_data_plt_powerpc
<size
, big_endian
>*
276 gold_assert(this->plt_
!= NULL
);
280 // Get the dynamic reloc section, creating it if necessary.
282 rela_dyn_section(Layout
*);
284 // Copy a relocation against a global symbol.
286 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
287 Sized_relobj
<size
, big_endian
>* object
,
288 unsigned int shndx
, Output_section
* output_section
,
289 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
291 this->copy_relocs_
.copy_reloc(symtab
, layout
,
292 symtab
->get_sized_symbol
<size
>(sym
),
293 object
, shndx
, output_section
,
294 reloc
, this->rela_dyn_section(layout
));
297 // Information about this specific target which we pass to the
298 // general Target structure.
299 static Target::Target_info powerpc_info
;
301 // The types of GOT entries needed for this platform.
304 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
305 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
306 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
310 Output_data_got
<size
, big_endian
>* got_
;
312 Output_data_space
* got2_
;
314 Output_data_space
* toc_
;
316 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
317 // The dynamic reloc section.
318 Reloc_section
* rela_dyn_
;
319 // Relocs saved to avoid a COPY reloc.
320 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
321 // Space for variables copied with a COPY reloc.
322 Output_data_space
* dynbss_
;
323 // Offset of the GOT entry for the TLS module index;
324 unsigned int got_mod_index_offset_
;
328 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
331 true, // is_big_endian
332 elfcpp::EM_PPC
, // machine_code
333 false, // has_make_symbol
334 false, // has_resolve
335 false, // has_code_fill
336 true, // is_default_stack_executable
338 "/usr/lib/ld.so.1", // dynamic_linker
339 0x10000000, // default_text_segment_address
340 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
341 4 * 1024, // common_pagesize (overridable by -z common-page-size)
342 elfcpp::SHN_UNDEF
, // small_common_shndx
343 elfcpp::SHN_UNDEF
, // large_common_shndx
344 0, // small_common_section_flags
345 0, // large_common_section_flags
346 NULL
, // attributes_section
347 NULL
// attributes_vendor
351 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
354 false, // is_big_endian
355 elfcpp::EM_PPC
, // machine_code
356 false, // has_make_symbol
357 false, // has_resolve
358 false, // has_code_fill
359 true, // is_default_stack_executable
361 "/usr/lib/ld.so.1", // dynamic_linker
362 0x10000000, // default_text_segment_address
363 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
364 4 * 1024, // common_pagesize (overridable by -z common-page-size)
365 elfcpp::SHN_UNDEF
, // small_common_shndx
366 elfcpp::SHN_UNDEF
, // large_common_shndx
367 0, // small_common_section_flags
368 0, // large_common_section_flags
369 NULL
, // attributes_section
370 NULL
// attributes_vendor
374 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
377 true, // is_big_endian
378 elfcpp::EM_PPC64
, // machine_code
379 false, // has_make_symbol
380 false, // has_resolve
381 false, // has_code_fill
382 true, // is_default_stack_executable
384 "/usr/lib/ld.so.1", // dynamic_linker
385 0x10000000, // default_text_segment_address
386 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
387 8 * 1024, // common_pagesize (overridable by -z common-page-size)
388 elfcpp::SHN_UNDEF
, // small_common_shndx
389 elfcpp::SHN_UNDEF
, // large_common_shndx
390 0, // small_common_section_flags
391 0, // large_common_section_flags
392 NULL
, // attributes_section
393 NULL
// attributes_vendor
397 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
400 false, // is_big_endian
401 elfcpp::EM_PPC64
, // 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 8 * 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
419 template<int size
, bool big_endian
>
420 class Powerpc_relocate_functions
423 // Do a simple relocation with the addend in the relocation.
424 template<int valsize
>
426 rela(unsigned char* view
,
427 unsigned int right_shift
,
428 elfcpp::Elf_Xword dst_mask
,
429 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
430 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
432 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
433 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
434 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
435 Valtype reloc
= ((value
+ addend
) >> right_shift
);
440 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
443 // Do a simple relocation using a symbol value with the addend in
445 template<int valsize
>
447 rela(unsigned char* view
,
448 unsigned int right_shift
,
449 elfcpp::Elf_Xword dst_mask
,
450 const Sized_relobj
<size
, big_endian
>* object
,
451 const Symbol_value
<size
>* psymval
,
452 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
454 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
455 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
456 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
457 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
462 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
465 // Do a simple relocation using a symbol value with the addend in
466 // the relocation, unaligned.
467 template<int valsize
>
469 rela_ua(unsigned char* view
, unsigned int right_shift
,
470 elfcpp::Elf_Xword dst_mask
,
471 const Sized_relobj
<size
, big_endian
>* object
,
472 const Symbol_value
<size
>* psymval
,
473 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
475 typedef typename
elfcpp::Swap_unaligned
<valsize
,
476 big_endian
>::Valtype Valtype
;
477 unsigned char* wv
= view
;
478 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
479 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
484 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
487 // Do a simple PC relative relocation with a Symbol_value with the
488 // addend in the relocation.
489 template<int valsize
>
491 pcrela(unsigned char* view
, unsigned int right_shift
,
492 elfcpp::Elf_Xword dst_mask
,
493 const Sized_relobj
<size
, big_endian
>* object
,
494 const Symbol_value
<size
>* psymval
,
495 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
496 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
498 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
499 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
500 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
501 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
507 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
510 template<int valsize
>
512 pcrela_unaligned(unsigned char* view
,
513 const Sized_relobj
<size
, big_endian
>* object
,
514 const Symbol_value
<size
>* psymval
,
515 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
516 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
518 typedef typename
elfcpp::Swap_unaligned
<valsize
,
519 big_endian
>::Valtype Valtype
;
520 unsigned char* wv
= view
;
521 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
523 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
526 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
527 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
529 // R_POWERPC_REL32: (Symbol + Addend - Address)
531 rel32(unsigned char* view
,
532 const Sized_relobj
<size
, big_endian
>* object
,
533 const Symbol_value
<size
>* psymval
,
534 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
535 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
536 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
538 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
540 rel24(unsigned char* view
,
541 const Sized_relobj
<size
, big_endian
>* object
,
542 const Symbol_value
<size
>* psymval
,
543 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
544 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
546 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
547 psymval
, addend
, address
);
550 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
552 rel14(unsigned char* view
,
553 const Sized_relobj
<size
, big_endian
>* object
,
554 const Symbol_value
<size
>* psymval
,
555 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
556 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
558 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
559 psymval
, addend
, address
);
562 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
564 addr16(unsigned char* view
,
565 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
566 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
567 { This_reloc::rela16(view
, value
, addend
); }
570 addr16(unsigned char* view
,
571 const Sized_relobj
<size
, big_endian
>* object
,
572 const Symbol_value
<size
>* psymval
,
573 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
574 { This_reloc::rela16(view
, object
, psymval
, addend
); }
576 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
578 addr16_ds(unsigned char* view
,
579 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
580 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
582 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
585 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
587 addr16_lo(unsigned char* view
,
588 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
589 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
590 { This_reloc::rela16(view
, value
, addend
); }
593 addr16_lo(unsigned char* view
,
594 const Sized_relobj
<size
, big_endian
>* object
,
595 const Symbol_value
<size
>* psymval
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
597 { This_reloc::rela16(view
, object
, psymval
, addend
); }
599 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
601 addr16_hi(unsigned char* view
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
603 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
605 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
609 addr16_hi(unsigned char* view
,
610 const Sized_relobj
<size
, big_endian
>* object
,
611 const Symbol_value
<size
>* psymval
,
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
614 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
617 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
618 // final value of the low 16 bits of the
619 // relocation is negative, add one.
621 addr16_ha(unsigned char* view
,
622 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
623 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
625 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
627 reloc
= value
+ addend
;
633 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
637 addr16_ha(unsigned char* view
,
638 const Sized_relobj
<size
, big_endian
>* object
,
639 const Symbol_value
<size
>* psymval
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
642 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
644 reloc
= psymval
->value(object
, addend
);
650 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
653 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
655 rel16(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
,
659 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
660 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
662 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
664 rel16_lo(unsigned char* view
,
665 const Sized_relobj
<size
, big_endian
>* object
,
666 const Symbol_value
<size
>* psymval
,
667 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
668 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
669 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
671 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
673 rel16_hi(unsigned char* view
,
674 const Sized_relobj
<size
, big_endian
>* object
,
675 const Symbol_value
<size
>* psymval
,
676 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
677 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
679 This::template pcrela
<16>(view
, 16, 0xffff, object
,
680 psymval
, addend
, address
);
683 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
684 // final value of the low 16 bits of the
685 // relocation is negative, add one.
687 rel16_ha(unsigned char* view
,
688 const Sized_relobj
<size
, big_endian
>* object
,
689 const Symbol_value
<size
>* psymval
,
690 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
691 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
693 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
695 reloc
= (psymval
->value(object
, addend
) - address
);
700 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
704 // Get the GOT section, creating it if necessary.
706 template<int size
, bool big_endian
>
707 Output_data_got
<size
, big_endian
>*
708 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
711 if (this->got_
== NULL
)
713 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
715 this->got_
= new Output_data_got
<size
, big_endian
>();
717 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
718 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
721 // Create the GOT2 or TOC in the .got section.
724 this->got2_
= new Output_data_space(4, "** GOT2");
725 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
732 this->toc_
= new Output_data_space(8, "** TOC");
733 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
739 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
740 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
742 0, 0, elfcpp::STT_OBJECT
,
744 elfcpp::STV_HIDDEN
, 0,
751 // Get the dynamic reloc section, creating it if necessary.
753 template<int size
, bool big_endian
>
754 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
755 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
757 if (this->rela_dyn_
== NULL
)
759 gold_assert(layout
!= NULL
);
760 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
761 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
762 elfcpp::SHF_ALLOC
, this->rela_dyn_
, true);
764 return this->rela_dyn_
;
767 // A class to handle the PLT data.
769 template<int size
, bool big_endian
>
770 class Output_data_plt_powerpc
: public Output_section_data
773 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
774 size
, big_endian
> Reloc_section
;
776 Output_data_plt_powerpc(Layout
*);
778 // Add an entry to the PLT.
779 void add_entry(Symbol
* gsym
);
781 // Return the .rela.plt section data.
782 const Reloc_section
* rel_plt() const
788 void do_adjust_output_section(Output_section
* os
);
791 // The size of an entry in the PLT.
792 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
794 // Set the final size.
796 set_final_data_size()
798 unsigned int full_count
= this->count_
+ 4;
800 this->set_data_size(full_count
* base_plt_entry_size
);
803 // Write out the PLT data.
805 do_write(Output_file
*);
807 // The reloc section.
809 // The number of PLT entries.
813 // Create the PLT section. The ordinary .got section is an argument,
814 // since we need to refer to the start.
816 template<int size
, bool big_endian
>
817 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
818 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
820 this->rel_
= new Reloc_section(false);
821 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
822 elfcpp::SHF_ALLOC
, this->rel_
, true);
825 template<int size
, bool big_endian
>
827 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
832 // Add an entry to the PLT.
834 template<int size
, bool big_endian
>
836 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
838 gold_assert(!gsym
->has_plt_offset());
839 unsigned int index
= this->count_
+ + 4;
840 section_offset_type plt_offset
;
843 plt_offset
= index
* base_plt_entry_size
;
847 gsym
->set_plt_offset(plt_offset
);
851 gsym
->set_needs_dynsym_entry();
852 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
856 static const unsigned int addis_11_11
= 0x3d6b0000;
857 static const unsigned int addis_11_30
= 0x3d7e0000;
858 static const unsigned int addis_12_12
= 0x3d8c0000;
859 static const unsigned int addi_11_11
= 0x396b0000;
860 static const unsigned int add_0_11_11
= 0x7c0b5a14;
861 static const unsigned int add_11_0_11
= 0x7d605a14;
862 static const unsigned int b
= 0x48000000;
863 static const unsigned int bcl_20_31
= 0x429f0005;
864 static const unsigned int bctr
= 0x4e800420;
865 static const unsigned int lis_11
= 0x3d600000;
866 static const unsigned int lis_12
= 0x3d800000;
867 static const unsigned int lwzu_0_12
= 0x840c0000;
868 static const unsigned int lwz_0_12
= 0x800c0000;
869 static const unsigned int lwz_11_11
= 0x816b0000;
870 static const unsigned int lwz_11_30
= 0x817e0000;
871 static const unsigned int lwz_12_12
= 0x818c0000;
872 static const unsigned int mflr_0
= 0x7c0802a6;
873 static const unsigned int mflr_12
= 0x7d8802a6;
874 static const unsigned int mtctr_0
= 0x7c0903a6;
875 static const unsigned int mtctr_11
= 0x7d6903a6;
876 static const unsigned int mtlr_0
= 0x7c0803a6;
877 static const unsigned int nop
= 0x60000000;
878 static const unsigned int sub_11_11_12
= 0x7d6c5850;
880 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
881 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
882 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
883 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
884 /* ld %r11,xxx+16@l(%r12) */
887 // Write out the PLT.
889 template<int size
, bool big_endian
>
891 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
893 const off_t offset
= this->offset();
894 const section_size_type oview_size
=
895 convert_to_section_size_type(this->data_size());
896 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
897 unsigned char* pov
= oview
;
899 memset(pov
, 0, base_plt_entry_size
* 4);
900 pov
+= base_plt_entry_size
* 4;
902 unsigned int plt_offset
= base_plt_entry_size
* 4;
903 const unsigned int count
= this->count_
;
907 for (unsigned int i
= 0; i
< count
; i
++)
913 for (unsigned int i
= 0; i
< count
; i
++)
915 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
916 lwz_11_30
+ plt_offset
);
917 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
918 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
919 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
920 pov
+= base_plt_entry_size
;
921 plt_offset
+= base_plt_entry_size
;
925 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
927 of
->write_output_view(offset
, oview_size
, oview
);
930 // Create a PLT entry for a global symbol.
932 template<int size
, bool big_endian
>
934 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
938 if (gsym
->has_plt_offset())
941 if (this->plt_
== NULL
)
943 // Create the GOT section first.
944 this->got_section(symtab
, layout
);
946 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
947 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
949 | elfcpp::SHF_EXECINSTR
950 | elfcpp::SHF_WRITE
),
953 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
954 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
956 0, 0, elfcpp::STT_OBJECT
,
958 elfcpp::STV_HIDDEN
, 0,
962 this->plt_
->add_entry(gsym
);
965 // Create a GOT entry for the TLS module index.
967 template<int size
, bool big_endian
>
969 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(Symbol_table
* symtab
,
971 Sized_relobj
<size
, big_endian
>* object
)
973 if (this->got_mod_index_offset_
== -1U)
975 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
976 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
977 Output_data_got
<size
, big_endian
>* got
;
978 unsigned int got_offset
;
980 got
= this->got_section(symtab
, layout
);
981 got_offset
= got
->add_constant(0);
982 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
984 got
->add_constant(0);
985 this->got_mod_index_offset_
= got_offset
;
987 return this->got_mod_index_offset_
;
990 // Optimize the TLS relocation type based on what we know about the
991 // symbol. IS_FINAL is true if the final address of this symbol is
992 // known at link time.
994 static tls::Tls_optimization
995 optimize_tls_reloc(bool /* is_final */, int r_type
)
997 // If we are generating a shared library, then we can't do anything
999 if (parameters
->options().shared())
1000 return tls::TLSOPT_NONE
;
1009 // Report an unsupported relocation against a local symbol.
1011 template<int size
, bool big_endian
>
1013 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1014 Sized_relobj
<size
, big_endian
>* object
,
1015 unsigned int r_type
)
1017 gold_error(_("%s: unsupported reloc %u against local symbol"),
1018 object
->name().c_str(), r_type
);
1021 // We are about to emit a dynamic relocation of type R_TYPE. If the
1022 // dynamic linker does not support it, issue an error.
1024 template<int size
, bool big_endian
>
1026 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1027 unsigned int r_type
)
1029 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1031 // These are the relocation types supported by glibc for both 32-bit
1032 // and 64-bit powerpc.
1035 case elfcpp::R_POWERPC_RELATIVE
:
1036 case elfcpp::R_POWERPC_GLOB_DAT
:
1037 case elfcpp::R_POWERPC_DTPMOD
:
1038 case elfcpp::R_POWERPC_DTPREL
:
1039 case elfcpp::R_POWERPC_TPREL
:
1040 case elfcpp::R_POWERPC_JMP_SLOT
:
1041 case elfcpp::R_POWERPC_COPY
:
1042 case elfcpp::R_POWERPC_ADDR32
:
1043 case elfcpp::R_POWERPC_ADDR24
:
1044 case elfcpp::R_POWERPC_REL24
:
1055 // These are the relocation types supported only on 64-bit.
1056 case elfcpp::R_PPC64_ADDR64
:
1057 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1058 case elfcpp::R_PPC64_TPREL16_DS
:
1059 case elfcpp::R_POWERPC_TPREL16
:
1060 case elfcpp::R_POWERPC_TPREL16_LO
:
1061 case elfcpp::R_POWERPC_TPREL16_HI
:
1062 case elfcpp::R_POWERPC_TPREL16_HA
:
1063 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1064 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1065 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1066 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1067 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1068 case elfcpp::R_POWERPC_ADDR16_LO
:
1069 case elfcpp::R_POWERPC_ADDR16_HI
:
1070 case elfcpp::R_POWERPC_ADDR16_HA
:
1071 case elfcpp::R_POWERPC_ADDR30
:
1072 case elfcpp::R_PPC64_UADDR64
:
1073 case elfcpp::R_POWERPC_UADDR32
:
1074 case elfcpp::R_POWERPC_ADDR16
:
1075 case elfcpp::R_POWERPC_UADDR16
:
1076 case elfcpp::R_PPC64_ADDR16_DS
:
1077 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1078 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1079 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1080 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1081 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1082 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1083 case elfcpp::R_POWERPC_REL32
:
1084 case elfcpp::R_PPC64_REL64
:
1095 // These are the relocation types supported only on 32-bit.
1102 // This prevents us from issuing more than one error per reloc
1103 // section. But we can still wind up issuing more than one
1104 // error per object file.
1105 if (this->issued_non_pic_error_
)
1107 gold_assert(parameters
->options().output_is_position_independent());
1108 object
->error(_("requires unsupported dynamic reloc; "
1109 "recompile with -fPIC"));
1110 this->issued_non_pic_error_
= true;
1114 // Scan a relocation for a local symbol.
1116 template<int size
, bool big_endian
>
1118 Target_powerpc
<size
, big_endian
>::Scan::local(
1119 Symbol_table
* symtab
,
1121 Target_powerpc
<size
, big_endian
>* target
,
1122 Sized_relobj
<size
, big_endian
>* object
,
1123 unsigned int data_shndx
,
1124 Output_section
* output_section
,
1125 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1126 unsigned int r_type
,
1127 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1131 case elfcpp::R_POWERPC_NONE
:
1132 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1133 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1136 case elfcpp::R_PPC64_ADDR64
:
1137 case elfcpp::R_POWERPC_ADDR32
:
1138 case elfcpp::R_POWERPC_ADDR16_HA
:
1139 case elfcpp::R_POWERPC_ADDR16_LO
:
1140 // If building a shared library (or a position-independent
1141 // executable), we need to create a dynamic relocation for
1143 if (parameters
->options().output_is_position_independent())
1145 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1147 check_non_pic(object
, r_type
);
1148 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1150 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1151 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1152 data_shndx
, reloc
.get_r_offset(),
1153 reloc
.get_r_addend());
1157 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1158 gold_assert(lsym
.get_st_value() == 0);
1159 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1160 output_section
, data_shndx
,
1161 reloc
.get_r_offset(),
1162 reloc
.get_r_addend());
1167 case elfcpp::R_POWERPC_REL24
:
1168 case elfcpp::R_PPC_LOCAL24PC
:
1169 case elfcpp::R_POWERPC_REL32
:
1170 case elfcpp::R_PPC_REL16_LO
:
1171 case elfcpp::R_PPC_REL16_HA
:
1174 case elfcpp::R_POWERPC_GOT16
:
1175 case elfcpp::R_POWERPC_GOT16_LO
:
1176 case elfcpp::R_POWERPC_GOT16_HI
:
1177 case elfcpp::R_POWERPC_GOT16_HA
:
1178 case elfcpp::R_PPC64_TOC16
:
1179 case elfcpp::R_PPC64_TOC16_LO
:
1180 case elfcpp::R_PPC64_TOC16_HI
:
1181 case elfcpp::R_PPC64_TOC16_HA
:
1182 case elfcpp::R_PPC64_TOC16_DS
:
1183 case elfcpp::R_PPC64_TOC16_LO_DS
:
1185 // The symbol requires a GOT entry.
1186 Output_data_got
<size
, big_endian
>* got
;
1189 got
= target
->got_section(symtab
, layout
);
1190 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1192 // If we are generating a shared object, we need to add a
1193 // dynamic relocation for this symbol's GOT entry.
1194 if (parameters
->options().output_is_position_independent())
1196 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1198 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1201 off
= got
->add_constant(0);
1202 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1203 rela_dyn
->add_local_relative(object
, r_sym
,
1204 elfcpp::R_POWERPC_RELATIVE
,
1209 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1213 case elfcpp::R_PPC64_TOC
:
1214 // We need a GOT section.
1215 target
->got_section(symtab
, layout
);
1218 // These are relocations which should only be seen by the
1219 // dynamic linker, and should never be seen here.
1220 case elfcpp::R_POWERPC_COPY
:
1221 case elfcpp::R_POWERPC_GLOB_DAT
:
1222 case elfcpp::R_POWERPC_JMP_SLOT
:
1223 case elfcpp::R_POWERPC_RELATIVE
:
1224 case elfcpp::R_POWERPC_DTPMOD
:
1225 gold_error(_("%s: unexpected reloc %u in object file"),
1226 object
->name().c_str(), r_type
);
1230 unsupported_reloc_local(object
, r_type
);
1235 // Report an unsupported relocation against a global symbol.
1237 template<int size
, bool big_endian
>
1239 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1240 Sized_relobj
<size
, big_endian
>* object
,
1241 unsigned int r_type
,
1244 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1245 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1248 // Scan a relocation for a global symbol.
1250 template<int size
, bool big_endian
>
1252 Target_powerpc
<size
, big_endian
>::Scan::global(
1253 Symbol_table
* symtab
,
1255 Target_powerpc
<size
, big_endian
>* target
,
1256 Sized_relobj
<size
, big_endian
>* object
,
1257 unsigned int data_shndx
,
1258 Output_section
* output_section
,
1259 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1260 unsigned int r_type
,
1265 case elfcpp::R_POWERPC_NONE
:
1266 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1267 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1270 case elfcpp::R_PPC_PLTREL24
:
1271 // If the symbol is fully resolved, this is just a PC32 reloc.
1272 // Otherwise we need a PLT entry.
1273 if (gsym
->final_value_is_known())
1275 // If building a shared library, we can also skip the PLT entry
1276 // if the symbol is defined in the output file and is protected
1278 if (gsym
->is_defined()
1279 && !gsym
->is_from_dynobj()
1280 && !gsym
->is_preemptible())
1282 target
->make_plt_entry(symtab
, layout
, gsym
);
1285 case elfcpp::R_POWERPC_ADDR16
:
1286 case elfcpp::R_POWERPC_ADDR16_LO
:
1287 case elfcpp::R_POWERPC_ADDR16_HI
:
1288 case elfcpp::R_POWERPC_ADDR16_HA
:
1289 case elfcpp::R_POWERPC_ADDR32
:
1290 case elfcpp::R_PPC64_ADDR64
:
1292 // Make a PLT entry if necessary.
1293 if (gsym
->needs_plt_entry())
1295 target
->make_plt_entry(symtab
, layout
, gsym
);
1296 // Since this is not a PC-relative relocation, we may be
1297 // taking the address of a function. In that case we need to
1298 // set the entry in the dynamic symbol table to the address of
1300 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1301 gsym
->set_needs_dynsym_value();
1303 // Make a dynamic relocation if necessary.
1304 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1306 if (gsym
->may_need_copy_reloc())
1308 target
->copy_reloc(symtab
, layout
, object
,
1309 data_shndx
, output_section
, gsym
, reloc
);
1311 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1312 || r_type
== elfcpp::R_PPC64_ADDR64
)
1313 && gsym
->can_use_relative_reloc(false))
1315 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1316 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1317 output_section
, object
,
1318 data_shndx
, reloc
.get_r_offset(),
1319 reloc
.get_r_addend());
1323 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1325 check_non_pic(object
, r_type
);
1326 if (gsym
->is_from_dynobj()
1327 || gsym
->is_undefined()
1328 || gsym
->is_preemptible())
1329 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1331 reloc
.get_r_offset(),
1332 reloc
.get_r_addend());
1334 rela_dyn
->add_global_relative(gsym
, r_type
,
1335 output_section
, object
,
1337 reloc
.get_r_offset(),
1338 reloc
.get_r_addend());
1344 case elfcpp::R_POWERPC_REL24
:
1345 case elfcpp::R_PPC_LOCAL24PC
:
1346 case elfcpp::R_PPC_REL16
:
1347 case elfcpp::R_PPC_REL16_LO
:
1348 case elfcpp::R_PPC_REL16_HI
:
1349 case elfcpp::R_PPC_REL16_HA
:
1351 if (gsym
->needs_plt_entry())
1352 target
->make_plt_entry(symtab
, layout
, gsym
);
1353 // Make a dynamic relocation if necessary.
1354 int flags
= Symbol::NON_PIC_REF
;
1355 if (gsym
->type() == elfcpp::STT_FUNC
)
1356 flags
|= Symbol::FUNCTION_CALL
;
1357 if (gsym
->needs_dynamic_reloc(flags
))
1359 if (gsym
->may_need_copy_reloc())
1361 target
->copy_reloc(symtab
, layout
, object
,
1362 data_shndx
, output_section
, gsym
,
1367 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1368 check_non_pic(object
, r_type
);
1369 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1370 data_shndx
, reloc
.get_r_offset(),
1371 reloc
.get_r_addend());
1377 case elfcpp::R_POWERPC_GOT16
:
1378 case elfcpp::R_POWERPC_GOT16_LO
:
1379 case elfcpp::R_POWERPC_GOT16_HI
:
1380 case elfcpp::R_POWERPC_GOT16_HA
:
1381 case elfcpp::R_PPC64_TOC16
:
1382 case elfcpp::R_PPC64_TOC16_LO
:
1383 case elfcpp::R_PPC64_TOC16_HI
:
1384 case elfcpp::R_PPC64_TOC16_HA
:
1385 case elfcpp::R_PPC64_TOC16_DS
:
1386 case elfcpp::R_PPC64_TOC16_LO_DS
:
1388 // The symbol requires a GOT entry.
1389 Output_data_got
<size
, big_endian
>* got
;
1391 got
= target
->got_section(symtab
, layout
);
1392 if (gsym
->final_value_is_known())
1393 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1396 // If this symbol is not fully resolved, we need to add a
1397 // dynamic relocation for it.
1398 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1399 if (gsym
->is_from_dynobj()
1400 || gsym
->is_undefined()
1401 || gsym
->is_preemptible())
1402 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1403 elfcpp::R_POWERPC_GLOB_DAT
);
1404 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1406 unsigned int off
= got
->add_constant(0);
1408 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1409 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1416 case elfcpp::R_PPC64_TOC
:
1417 // We need a GOT section.
1418 target
->got_section(symtab
, layout
);
1421 case elfcpp::R_POWERPC_GOT_TPREL16
:
1422 case elfcpp::R_POWERPC_TLS
:
1426 // These are relocations which should only be seen by the
1427 // dynamic linker, and should never be seen here.
1428 case elfcpp::R_POWERPC_COPY
:
1429 case elfcpp::R_POWERPC_GLOB_DAT
:
1430 case elfcpp::R_POWERPC_JMP_SLOT
:
1431 case elfcpp::R_POWERPC_RELATIVE
:
1432 case elfcpp::R_POWERPC_DTPMOD
:
1433 gold_error(_("%s: unexpected reloc %u in object file"),
1434 object
->name().c_str(), r_type
);
1438 unsupported_reloc_global(object
, r_type
, gsym
);
1443 // Process relocations for gc.
1445 template<int size
, bool big_endian
>
1447 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1448 Symbol_table
* symtab
,
1450 Sized_relobj
<size
, big_endian
>* object
,
1451 unsigned int data_shndx
,
1453 const unsigned char* prelocs
,
1455 Output_section
* output_section
,
1456 bool needs_special_offset_handling
,
1457 size_t local_symbol_count
,
1458 const unsigned char* plocal_symbols
)
1460 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1461 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1463 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1472 needs_special_offset_handling
,
1477 // Scan relocations for a section.
1479 template<int size
, bool big_endian
>
1481 Target_powerpc
<size
, big_endian
>::scan_relocs(
1482 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
>(
1532 needs_special_offset_handling
,
1537 // Finalize the sections.
1539 template<int size
, bool big_endian
>
1541 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
1543 const Input_objects
*,
1546 // Fill in some more dynamic tags.
1547 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1550 if (this->plt_
!= NULL
1551 && this->plt_
->output_section() != NULL
)
1553 const Output_data
* od
= this->plt_
->rel_plt();
1554 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1555 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1556 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1558 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->plt_
);
1561 if (this->rela_dyn_
!= NULL
1562 && this->rela_dyn_
->output_section() != NULL
)
1564 const Output_data
* od
= this->rela_dyn_
;
1565 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1566 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1567 odyn
->add_constant(elfcpp::DT_RELAENT
,
1568 elfcpp::Elf_sizes
<size
>::rela_size
);
1571 if (!parameters
->options().shared())
1573 // The value of the DT_DEBUG tag is filled in by the dynamic
1574 // linker at run time, and used by the debugger.
1575 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1579 // Emit any relocs we saved in an attempt to avoid generating COPY
1581 if (this->copy_relocs_
.any_saved_relocs())
1582 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1585 // Perform a relocation.
1587 template<int size
, bool big_endian
>
1589 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1590 const Relocate_info
<size
, big_endian
>* relinfo
,
1591 Target_powerpc
* target
,
1594 const elfcpp::Rela
<size
, big_endian
>& rela
,
1595 unsigned int r_type
,
1596 const Sized_symbol
<size
>* gsym
,
1597 const Symbol_value
<size
>* psymval
,
1598 unsigned char* view
,
1599 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1600 section_size_type
/* view_size */)
1602 const unsigned int toc_base_offset
= 0x8000;
1603 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1605 // Pick the value to use for symbols defined in shared objects.
1606 Symbol_value
<size
> symval
;
1608 && gsym
->use_plt_offset(r_type
== elfcpp::R_POWERPC_REL24
1609 || r_type
== elfcpp::R_PPC_LOCAL24PC
1610 || r_type
== elfcpp::R_PPC_REL16
1611 || r_type
== elfcpp::R_PPC_REL16_LO
1612 || r_type
== elfcpp::R_PPC_REL16_HI
1613 || r_type
== elfcpp::R_PPC_REL16_HA
))
1615 elfcpp::Elf_Xword value
;
1617 value
= target
->plt_section()->address() + gsym
->plt_offset();
1619 symval
.set_output_value(value
);
1624 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1625 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1627 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1628 // pointer points to the beginning, not the end, of the table.
1629 // So we just use the plain offset.
1630 bool have_got_offset
= false;
1631 unsigned int got_offset
= 0;
1632 unsigned int got2_offset
= 0;
1635 case elfcpp::R_PPC64_TOC16
:
1636 case elfcpp::R_PPC64_TOC16_LO
:
1637 case elfcpp::R_PPC64_TOC16_HI
:
1638 case elfcpp::R_PPC64_TOC16_HA
:
1639 case elfcpp::R_PPC64_TOC16_DS
:
1640 case elfcpp::R_PPC64_TOC16_LO_DS
:
1641 // Subtract the TOC base address.
1642 addend
-= target
->toc_section()->address() + toc_base_offset
;
1645 case elfcpp::R_POWERPC_GOT16
:
1646 case elfcpp::R_POWERPC_GOT16_LO
:
1647 case elfcpp::R_POWERPC_GOT16_HI
:
1648 case elfcpp::R_POWERPC_GOT16_HA
:
1649 case elfcpp::R_PPC64_GOT16_DS
:
1650 case elfcpp::R_PPC64_GOT16_LO_DS
:
1653 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1654 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1658 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1659 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1660 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1662 have_got_offset
= true;
1665 // R_PPC_PLTREL24 is rather special. If non-zero,
1666 // the addend specifies the GOT pointer offset within .got2.
1667 case elfcpp::R_PPC_PLTREL24
:
1668 if (addend
>= 32768)
1670 Output_data_space
* got2
;
1671 got2
= target
->got2_section();
1672 got2_offset
= got2
->offset();
1673 addend
+= got2_offset
;
1675 have_got_offset
= true;
1684 case elfcpp::R_POWERPC_NONE
:
1685 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1686 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1689 case elfcpp::R_POWERPC_REL32
:
1690 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1693 case elfcpp::R_POWERPC_REL24
:
1694 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1697 case elfcpp::R_POWERPC_REL14
:
1698 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1701 case elfcpp::R_PPC_PLTREL24
:
1702 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1705 case elfcpp::R_PPC_LOCAL24PC
:
1706 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1709 case elfcpp::R_PPC64_ADDR64
:
1710 if (!parameters
->options().output_is_position_independent())
1711 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1715 case elfcpp::R_POWERPC_ADDR32
:
1716 if (!parameters
->options().output_is_position_independent())
1717 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1721 case elfcpp::R_POWERPC_ADDR16_LO
:
1722 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1725 case elfcpp::R_POWERPC_ADDR16_HI
:
1726 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1729 case elfcpp::R_POWERPC_ADDR16_HA
:
1730 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1733 case elfcpp::R_PPC_REL16_LO
:
1734 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1737 case elfcpp::R_PPC_REL16_HI
:
1738 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1741 case elfcpp::R_PPC_REL16_HA
:
1742 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1745 case elfcpp::R_POWERPC_GOT16
:
1746 Reloc::addr16(view
, got_offset
, addend
);
1749 case elfcpp::R_POWERPC_GOT16_LO
:
1750 Reloc::addr16_lo(view
, got_offset
, addend
);
1753 case elfcpp::R_POWERPC_GOT16_HI
:
1754 Reloc::addr16_hi(view
, got_offset
, addend
);
1757 case elfcpp::R_POWERPC_GOT16_HA
:
1758 Reloc::addr16_ha(view
, got_offset
, addend
);
1761 case elfcpp::R_PPC64_TOC16
:
1762 Reloc::addr16(view
, got_offset
, addend
);
1765 case elfcpp::R_PPC64_TOC16_LO
:
1766 Reloc::addr16_lo(view
, got_offset
, addend
);
1769 case elfcpp::R_PPC64_TOC16_HI
:
1770 Reloc::addr16_hi(view
, got_offset
, addend
);
1773 case elfcpp::R_PPC64_TOC16_HA
:
1774 Reloc::addr16_ha(view
, got_offset
, addend
);
1777 case elfcpp::R_PPC64_TOC16_DS
:
1778 case elfcpp::R_PPC64_TOC16_LO_DS
:
1779 Reloc::addr16_ds(view
, got_offset
, addend
);
1782 case elfcpp::R_PPC64_TOC
:
1784 elfcpp::Elf_types
<64>::Elf_Addr value
;
1785 value
= target
->toc_section()->address() + toc_base_offset
;
1786 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1790 case elfcpp::R_POWERPC_COPY
:
1791 case elfcpp::R_POWERPC_GLOB_DAT
:
1792 case elfcpp::R_POWERPC_JMP_SLOT
:
1793 case elfcpp::R_POWERPC_RELATIVE
:
1794 // This is an outstanding tls reloc, which is unexpected when
1796 case elfcpp::R_POWERPC_DTPMOD
:
1797 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1798 _("unexpected reloc %u in object file"),
1803 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1804 _("unsupported reloc %u"),
1812 // Perform a TLS relocation.
1814 template<int size
, bool big_endian
>
1816 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1817 const Relocate_info
<size
, big_endian
>* relinfo
,
1818 Target_powerpc
<size
, big_endian
>* target
,
1820 const elfcpp::Rela
<size
, big_endian
>& rela
,
1821 unsigned int r_type
,
1822 const Sized_symbol
<size
>* gsym
,
1823 const Symbol_value
<size
>* psymval
,
1824 unsigned char* view
,
1825 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1828 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1829 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1830 const Sized_relobj
<size
, big_endian
>* object
= relinfo
->object
;
1832 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1833 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1835 const bool is_final
=
1837 ? !parameters
->options().output_is_position_independent()
1838 : gsym
->final_value_is_known());
1839 const tls::Tls_optimization optimized_type
1840 = optimize_tls_reloc(is_final
, r_type
);
1848 // Relocate section data.
1850 template<int size
, bool big_endian
>
1852 Target_powerpc
<size
, big_endian
>::relocate_section(
1853 const Relocate_info
<size
, big_endian
>* relinfo
,
1854 unsigned int sh_type
,
1855 const unsigned char* prelocs
,
1857 Output_section
* output_section
,
1858 bool needs_special_offset_handling
,
1859 unsigned char* view
,
1860 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1861 section_size_type view_size
,
1862 const Reloc_symbol_changes
* reloc_symbol_changes
)
1864 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1865 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
1867 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1869 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
1876 needs_special_offset_handling
,
1880 reloc_symbol_changes
);
1883 // Return the size of a relocation while scanning during a relocatable
1886 template<int size
, bool big_endian
>
1888 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
1892 // We are always SHT_RELA, so we should never get here.
1897 // Scan the relocs during a relocatable link.
1899 template<int size
, bool big_endian
>
1901 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
1902 Symbol_table
* symtab
,
1904 Sized_relobj
<size
, big_endian
>* object
,
1905 unsigned int data_shndx
,
1906 unsigned int sh_type
,
1907 const unsigned char* prelocs
,
1909 Output_section
* output_section
,
1910 bool needs_special_offset_handling
,
1911 size_t local_symbol_count
,
1912 const unsigned char* plocal_symbols
,
1913 Relocatable_relocs
* rr
)
1915 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1917 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
1918 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
1920 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
1921 Scan_relocatable_relocs
>(
1929 needs_special_offset_handling
,
1935 // Relocate a section during a relocatable link.
1937 template<int size
, bool big_endian
>
1939 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
1940 const Relocate_info
<size
, big_endian
>* relinfo
,
1941 unsigned int sh_type
,
1942 const unsigned char* prelocs
,
1944 Output_section
* output_section
,
1945 off_t offset_in_output_section
,
1946 const Relocatable_relocs
* rr
,
1947 unsigned char* view
,
1948 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
1949 section_size_type view_size
,
1950 unsigned char* reloc_view
,
1951 section_size_type reloc_view_size
)
1953 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1955 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
1960 offset_in_output_section
,
1969 // Return the value to use for a dynamic which requires special
1970 // treatment. This is how we support equality comparisons of function
1971 // pointers across shared library boundaries, as described in the
1972 // processor specific ABI supplement.
1974 template<int size
, bool big_endian
>
1976 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
1978 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1979 return this->plt_section()->address() + gsym
->plt_offset();
1982 // The selector for powerpc object files.
1984 template<int size
, bool big_endian
>
1985 class Target_selector_powerpc
: public Target_selector
1988 Target_selector_powerpc()
1989 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
1991 (big_endian
? "elf64-powerpc" : "elf64-powerpcle") :
1992 (big_endian
? "elf32-powerpc" : "elf32-powerpcle")))
1995 Target
* do_recognize(int machine
, int, int)
2000 if (machine
!= elfcpp::EM_PPC64
)
2005 if (machine
!= elfcpp::EM_PPC
)
2013 return this->instantiate_target();
2016 Target
* do_instantiate_target()
2017 { return new Target_powerpc
<size
, big_endian
>(); }
2020 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2021 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2022 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2023 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2025 } // End anonymous namespace.