* Makefile.am: Remove spurious sanitization marker.
[binutils.git] / gold / powerpc.cc
blob71f2ae468e535bfbdfd6ec4a45025aa0fb68d44d
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.
24 #include "gold.h"
26 #include "elfcpp.h"
27 #include "parameters.h"
28 #include "reloc.h"
29 #include "powerpc.h"
30 #include "object.h"
31 #include "symtab.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "copy-relocs.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39 #include "errors.h"
40 #include "gc.h"
42 namespace
45 using namespace gold;
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>
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
56 Target_powerpc()
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.
67 void
68 gc_process_relocs(const General_options& options,
69 Symbol_table* symtab,
70 Layout* layout,
71 Sized_relobj<size, big_endian>* object,
72 unsigned int data_shndx,
73 unsigned int sh_type,
74 const unsigned char* prelocs,
75 size_t reloc_count,
76 Output_section* output_section,
77 bool needs_special_offset_handling,
78 size_t local_symbol_count,
79 const unsigned char* plocal_symbols);
81 // Scan the relocations to look for symbol adjustments.
82 void
83 scan_relocs(const General_options& options,
84 Symbol_table* symtab,
85 Layout* layout,
86 Sized_relobj<size, big_endian>* object,
87 unsigned int data_shndx,
88 unsigned int sh_type,
89 const unsigned char* prelocs,
90 size_t reloc_count,
91 Output_section* output_section,
92 bool needs_special_offset_handling,
93 size_t local_symbol_count,
94 const unsigned char* plocal_symbols);
95 // Finalize the sections.
96 void
97 do_finalize_sections(Layout*);
99 // Return the value to use for a dynamic which requires special
100 // treatment.
101 uint64_t
102 do_dynsym_value(const Symbol*) const;
104 // Relocate a section.
105 void
106 relocate_section(const Relocate_info<size, big_endian>*,
107 unsigned int sh_type,
108 const unsigned char* prelocs,
109 size_t reloc_count,
110 Output_section* output_section,
111 bool needs_special_offset_handling,
112 unsigned char* view,
113 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
114 section_size_type view_size,
115 const Reloc_symbol_changes*);
117 // Scan the relocs during a relocatable link.
118 void
119 scan_relocatable_relocs(const General_options& options,
120 Symbol_table* symtab,
121 Layout* layout,
122 Sized_relobj<size, big_endian>* object,
123 unsigned int data_shndx,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
126 size_t reloc_count,
127 Output_section* output_section,
128 bool needs_special_offset_handling,
129 size_t local_symbol_count,
130 const unsigned char* plocal_symbols,
131 Relocatable_relocs*);
133 // Relocate a section during a relocatable link.
134 void
135 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
136 unsigned int sh_type,
137 const unsigned char* prelocs,
138 size_t reloc_count,
139 Output_section* output_section,
140 off_t offset_in_output_section,
141 const Relocatable_relocs*,
142 unsigned char* view,
143 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
144 section_size_type view_size,
145 unsigned char* reloc_view,
146 section_size_type reloc_view_size);
148 // Return whether SYM is defined by the ABI.
149 bool
150 do_is_defined_by_abi(const Symbol* sym) const
152 return strcmp(sym->name(), "___tls_get_addr") == 0;
155 // Return the size of the GOT section.
156 section_size_type
157 got_size()
159 gold_assert(this->got_ != NULL);
160 return this->got_->data_size();
163 private:
165 // The class which scans relocations.
166 class Scan
168 public:
169 Scan()
170 : issued_non_pic_error_(false)
173 inline void
174 local(const General_options& options, Symbol_table* symtab,
175 Layout* layout, Target_powerpc* target,
176 Sized_relobj<size, big_endian>* object,
177 unsigned int data_shndx,
178 Output_section* output_section,
179 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
180 const elfcpp::Sym<size, big_endian>& lsym);
182 inline void
183 global(const General_options& options, Symbol_table* symtab,
184 Layout* layout, Target_powerpc* target,
185 Sized_relobj<size, big_endian>* object,
186 unsigned int data_shndx,
187 Output_section* output_section,
188 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
189 Symbol* gsym);
191 private:
192 static void
193 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
194 unsigned int r_type);
196 static void
197 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
198 unsigned int r_type, Symbol*);
200 static void
201 generate_tls_call(Symbol_table* symtab, Layout* layout,
202 Target_powerpc* target);
204 void
205 check_non_pic(Relobj*, unsigned int r_type);
207 // Whether we have issued an error about a non-PIC compilation.
208 bool issued_non_pic_error_;
211 // The class which implements relocation.
212 class Relocate
214 public:
215 // Do a relocation. Return false if the caller should not issue
216 // any warnings about this relocation.
217 inline bool
218 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
219 Output_section*, size_t relnum,
220 const elfcpp::Rela<size, big_endian>&,
221 unsigned int r_type, const Sized_symbol<size>*,
222 const Symbol_value<size>*,
223 unsigned char*,
224 typename elfcpp::Elf_types<size>::Elf_Addr,
225 section_size_type);
227 private:
228 // Do a TLS relocation.
229 inline void
230 relocate_tls(const Relocate_info<size, big_endian>*,
231 Target_powerpc* target,
232 size_t relnum, const elfcpp::Rela<size, big_endian>&,
233 unsigned int r_type, const Sized_symbol<size>*,
234 const Symbol_value<size>*,
235 unsigned char*,
236 typename elfcpp::Elf_types<size>::Elf_Addr,
237 section_size_type);
240 // A class which returns the size required for a relocation type,
241 // used while scanning relocs during a relocatable link.
242 class Relocatable_size_for_reloc
244 public:
245 unsigned int
246 get_size_for_reloc(unsigned int, Relobj*);
249 // Get the GOT section, creating it if necessary.
250 Output_data_got<size, big_endian>*
251 got_section(Symbol_table*, Layout*);
253 Output_data_space*
254 got2_section() const
256 gold_assert (this->got2_ != NULL);
257 return this->got2_;
260 // Get the TOC section.
261 Output_data_space*
262 toc_section() const
264 gold_assert (this->toc_ != NULL);
265 return this->toc_;
268 // Create a PLT entry for a global symbol.
269 void
270 make_plt_entry(Symbol_table*, Layout*, Symbol*);
272 // Create a GOT entry for the TLS module index.
273 unsigned int
274 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
275 Sized_relobj<size, big_endian>* object);
277 // Get the PLT section.
278 const Output_data_plt_powerpc<size, big_endian>*
279 plt_section() const
281 gold_assert(this->plt_ != NULL);
282 return this->plt_;
285 // Get the dynamic reloc section, creating it if necessary.
286 Reloc_section*
287 rela_dyn_section(Layout*);
289 // Copy a relocation against a global symbol.
290 void
291 copy_reloc(Symbol_table* symtab, Layout* layout,
292 Sized_relobj<size, big_endian>* object,
293 unsigned int shndx, Output_section* output_section,
294 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
296 this->copy_relocs_.copy_reloc(symtab, layout,
297 symtab->get_sized_symbol<size>(sym),
298 object, shndx, output_section,
299 reloc, this->rela_dyn_section(layout));
302 // Information about this specific target which we pass to the
303 // general Target structure.
304 static Target::Target_info powerpc_info;
306 // The types of GOT entries needed for this platform.
307 enum Got_type
309 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
310 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
311 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
314 // The GOT section.
315 Output_data_got<size, big_endian>* got_;
316 // The GOT2 section.
317 Output_data_space* got2_;
318 // The TOC section.
319 Output_data_space* toc_;
320 // The PLT section.
321 Output_data_plt_powerpc<size, big_endian>* plt_;
322 // The dynamic reloc section.
323 Reloc_section* rela_dyn_;
324 // Relocs saved to avoid a COPY reloc.
325 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
326 // Space for variables copied with a COPY reloc.
327 Output_data_space* dynbss_;
328 // Offset of the GOT entry for the TLS module index;
329 unsigned int got_mod_index_offset_;
332 template<>
333 Target::Target_info Target_powerpc<32, true>::powerpc_info =
335 32, // size
336 true, // is_big_endian
337 elfcpp::EM_PPC, // machine_code
338 false, // has_make_symbol
339 false, // has_resolve
340 false, // has_code_fill
341 true, // is_default_stack_executable
342 '\0', // wrap_char
343 "/usr/lib/ld.so.1", // dynamic_linker
344 0x10000000, // default_text_segment_address
345 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
346 4 * 1024, // common_pagesize (overridable by -z common-page-size)
347 elfcpp::SHN_UNDEF, // small_common_shndx
348 elfcpp::SHN_UNDEF, // large_common_shndx
349 0, // small_common_section_flags
350 0 // large_common_section_flags
353 template<>
354 Target::Target_info Target_powerpc<32, false>::powerpc_info =
356 32, // size
357 false, // is_big_endian
358 elfcpp::EM_PPC, // machine_code
359 false, // has_make_symbol
360 false, // has_resolve
361 false, // has_code_fill
362 true, // is_default_stack_executable
363 '\0', // wrap_char
364 "/usr/lib/ld.so.1", // dynamic_linker
365 0x10000000, // default_text_segment_address
366 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
367 4 * 1024, // common_pagesize (overridable by -z common-page-size)
368 elfcpp::SHN_UNDEF, // small_common_shndx
369 elfcpp::SHN_UNDEF, // large_common_shndx
370 0, // small_common_section_flags
371 0 // large_common_section_flags
374 template<>
375 Target::Target_info Target_powerpc<64, true>::powerpc_info =
377 64, // size
378 true, // is_big_endian
379 elfcpp::EM_PPC64, // machine_code
380 false, // has_make_symbol
381 false, // has_resolve
382 false, // has_code_fill
383 true, // is_default_stack_executable
384 '\0', // wrap_char
385 "/usr/lib/ld.so.1", // dynamic_linker
386 0x10000000, // default_text_segment_address
387 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
388 8 * 1024, // common_pagesize (overridable by -z common-page-size)
389 elfcpp::SHN_UNDEF, // small_common_shndx
390 elfcpp::SHN_UNDEF, // large_common_shndx
391 0, // small_common_section_flags
392 0 // large_common_section_flags
395 template<>
396 Target::Target_info Target_powerpc<64, false>::powerpc_info =
398 64, // size
399 false, // is_big_endian
400 elfcpp::EM_PPC64, // machine_code
401 false, // has_make_symbol
402 false, // has_resolve
403 false, // has_code_fill
404 true, // is_default_stack_executable
405 '\0', // wrap_char
406 "/usr/lib/ld.so.1", // dynamic_linker
407 0x10000000, // default_text_segment_address
408 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
409 8 * 1024, // common_pagesize (overridable by -z common-page-size)
410 elfcpp::SHN_UNDEF, // small_common_shndx
411 elfcpp::SHN_UNDEF, // large_common_shndx
412 0, // small_common_section_flags
413 0 // large_common_section_flags
416 template<int size, bool big_endian>
417 class Powerpc_relocate_functions
419 private:
420 // Do a simple relocation with the addend in the relocation.
421 template<int valsize>
422 static inline void
423 rela(unsigned char* view,
424 unsigned int right_shift,
425 elfcpp::Elf_Xword dst_mask,
426 typename elfcpp::Swap<size, big_endian>::Valtype value,
427 typename elfcpp::Swap<size, big_endian>::Valtype addend)
429 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
430 Valtype* wv = reinterpret_cast<Valtype*>(view);
431 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
432 Valtype reloc = ((value + addend) >> right_shift);
434 val &= ~dst_mask;
435 reloc &= dst_mask;
437 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
440 // Do a simple relocation using a symbol value with the addend in
441 // the relocation.
442 template<int valsize>
443 static inline void
444 rela(unsigned char* view,
445 unsigned int right_shift,
446 elfcpp::Elf_Xword dst_mask,
447 const Sized_relobj<size, big_endian>* object,
448 const Symbol_value<size>* psymval,
449 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
451 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
452 Valtype* wv = reinterpret_cast<Valtype*>(view);
453 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
454 Valtype reloc = (psymval->value(object, addend) >> right_shift);
456 val &= ~dst_mask;
457 reloc &= dst_mask;
459 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
462 // Do a simple relocation using a symbol value with the addend in
463 // the relocation, unaligned.
464 template<int valsize>
465 static inline void
466 rela_ua(unsigned char* view, unsigned int right_shift,
467 elfcpp::Elf_Xword dst_mask,
468 const Sized_relobj<size, big_endian>* object,
469 const Symbol_value<size>* psymval,
470 typename elfcpp::Swap<size, big_endian>::Valtype addend)
472 typedef typename elfcpp::Swap_unaligned<valsize,
473 big_endian>::Valtype Valtype;
474 unsigned char* wv = view;
475 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
476 Valtype reloc = (psymval->value(object, addend) >> right_shift);
478 val &= ~dst_mask;
479 reloc &= dst_mask;
481 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
484 // Do a simple PC relative relocation with a Symbol_value with the
485 // addend in the relocation.
486 template<int valsize>
487 static inline void
488 pcrela(unsigned char* view, unsigned int right_shift,
489 elfcpp::Elf_Xword dst_mask,
490 const Sized_relobj<size, big_endian>* object,
491 const Symbol_value<size>* psymval,
492 typename elfcpp::Swap<size, big_endian>::Valtype addend,
493 typename elfcpp::Elf_types<size>::Elf_Addr address)
495 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
496 Valtype* wv = reinterpret_cast<Valtype*>(view);
497 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
498 Valtype reloc = ((psymval->value(object, addend) - address)
499 >> right_shift);
501 val &= ~dst_mask;
502 reloc &= dst_mask;
504 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
507 template<int valsize>
508 static inline void
509 pcrela_unaligned(unsigned char* view,
510 const Sized_relobj<size, big_endian>* object,
511 const Symbol_value<size>* psymval,
512 typename elfcpp::Swap<size, big_endian>::Valtype addend,
513 typename elfcpp::Elf_types<size>::Elf_Addr address)
515 typedef typename elfcpp::Swap_unaligned<valsize,
516 big_endian>::Valtype Valtype;
517 unsigned char* wv = view;
518 Valtype reloc = (psymval->value(object, addend) - address);
520 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
523 typedef Powerpc_relocate_functions<size, big_endian> This;
524 typedef Relocate_functions<size, big_endian> This_reloc;
525 public:
526 // R_POWERPC_REL32: (Symbol + Addend - Address)
527 static inline void
528 rel32(unsigned char* view,
529 const Sized_relobj<size, big_endian>* object,
530 const Symbol_value<size>* psymval,
531 typename elfcpp::Elf_types<size>::Elf_Addr addend,
532 typename elfcpp::Elf_types<size>::Elf_Addr address)
533 { This_reloc::pcrela32(view, object, psymval, addend, address); }
535 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
536 static inline void
537 rel24(unsigned char* view,
538 const Sized_relobj<size, big_endian>* object,
539 const Symbol_value<size>* psymval,
540 typename elfcpp::Elf_types<size>::Elf_Addr addend,
541 typename elfcpp::Elf_types<size>::Elf_Addr address)
543 This::template pcrela<32>(view, 0, 0x03fffffc, object,
544 psymval, addend, address);
547 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
548 static inline void
549 rel14(unsigned char* view,
550 const Sized_relobj<size, big_endian>* object,
551 const Symbol_value<size>* psymval,
552 typename elfcpp::Elf_types<size>::Elf_Addr addend,
553 typename elfcpp::Elf_types<size>::Elf_Addr address)
555 This::template pcrela<32>(view, 0, 0x0000fffc, object,
556 psymval, addend, address);
559 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
560 static inline void
561 addr16(unsigned char* view,
562 typename elfcpp::Elf_types<size>::Elf_Addr value,
563 typename elfcpp::Elf_types<size>::Elf_Addr addend)
564 { This_reloc::rela16(view, value, addend); }
566 static inline void
567 addr16(unsigned char* view,
568 const Sized_relobj<size, big_endian>* object,
569 const Symbol_value<size>* psymval,
570 typename elfcpp::Elf_types<size>::Elf_Addr addend)
571 { This_reloc::rela16(view, object, psymval, addend); }
573 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
574 static inline void
575 addr16_ds(unsigned char* view,
576 typename elfcpp::Elf_types<size>::Elf_Addr value,
577 typename elfcpp::Elf_types<size>::Elf_Addr addend)
579 This::template rela<16>(view, 0, 0xfffc, value, addend);
582 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
583 static inline void
584 addr16_lo(unsigned char* view,
585 typename elfcpp::Elf_types<size>::Elf_Addr value,
586 typename elfcpp::Elf_types<size>::Elf_Addr addend)
587 { This_reloc::rela16(view, value, addend); }
589 static inline void
590 addr16_lo(unsigned char* view,
591 const Sized_relobj<size, big_endian>* object,
592 const Symbol_value<size>* psymval,
593 typename elfcpp::Elf_types<size>::Elf_Addr addend)
594 { This_reloc::rela16(view, object, psymval, addend); }
596 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
597 static inline void
598 addr16_hi(unsigned char* view,
599 typename elfcpp::Elf_types<size>::Elf_Addr value,
600 typename elfcpp::Elf_types<size>::Elf_Addr addend)
602 This::template rela<16>(view, 16, 0xffff, value, addend);
605 static inline void
606 addr16_hi(unsigned char* view,
607 const Sized_relobj<size, big_endian>* object,
608 const Symbol_value<size>* psymval,
609 typename elfcpp::Elf_types<size>::Elf_Addr addend)
611 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
614 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
615 // final value of the low 16 bits of the
616 // relocation is negative, add one.
617 static inline void
618 addr16_ha(unsigned char* view,
619 typename elfcpp::Elf_types<size>::Elf_Addr value,
620 typename elfcpp::Elf_types<size>::Elf_Addr addend)
622 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
624 reloc = value + addend;
626 if (reloc & 0x8000)
627 reloc += 0x10000;
628 reloc >>= 16;
630 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
633 static inline void
634 addr16_ha(unsigned char* view,
635 const Sized_relobj<size, big_endian>* object,
636 const Symbol_value<size>* psymval,
637 typename elfcpp::Elf_types<size>::Elf_Addr addend)
639 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
641 reloc = psymval->value(object, addend);
643 if (reloc & 0x8000)
644 reloc += 0x10000;
645 reloc >>= 16;
647 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
650 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
651 static inline void
652 rel16(unsigned char* view,
653 const Sized_relobj<size, big_endian>* object,
654 const Symbol_value<size>* psymval,
655 typename elfcpp::Elf_types<size>::Elf_Addr addend,
656 typename elfcpp::Elf_types<size>::Elf_Addr address)
657 { This_reloc::pcrela16(view, object, psymval, addend, address); }
659 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
660 static inline void
661 rel16_lo(unsigned char* view,
662 const Sized_relobj<size, big_endian>* object,
663 const Symbol_value<size>* psymval,
664 typename elfcpp::Elf_types<size>::Elf_Addr addend,
665 typename elfcpp::Elf_types<size>::Elf_Addr address)
666 { This_reloc::pcrela16(view, object, psymval, addend, address); }
668 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
669 static inline void
670 rel16_hi(unsigned char* view,
671 const Sized_relobj<size, big_endian>* object,
672 const Symbol_value<size>* psymval,
673 typename elfcpp::Elf_types<size>::Elf_Addr addend,
674 typename elfcpp::Elf_types<size>::Elf_Addr address)
676 This::template pcrela<16>(view, 16, 0xffff, object,
677 psymval, addend, address);
680 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
681 // final value of the low 16 bits of the
682 // relocation is negative, add one.
683 static inline void
684 rel16_ha(unsigned char* view,
685 const Sized_relobj<size, big_endian>* object,
686 const Symbol_value<size>* psymval,
687 typename elfcpp::Elf_types<size>::Elf_Addr addend,
688 typename elfcpp::Elf_types<size>::Elf_Addr address)
690 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
692 reloc = (psymval->value(object, addend) - address);
693 if (reloc & 0x8000)
694 reloc += 0x10000;
695 reloc >>= 16;
697 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
701 // Get the GOT section, creating it if necessary.
703 template<int size, bool big_endian>
704 Output_data_got<size, big_endian>*
705 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
706 Layout* layout)
708 if (this->got_ == NULL)
710 gold_assert(symtab != NULL && layout != NULL);
712 this->got_ = new Output_data_got<size, big_endian>();
714 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
715 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
716 this->got_);
718 // Create the GOT2 or TOC in the .got section.
719 if (size == 32)
721 this->got2_ = new Output_data_space(4, "** GOT2");
722 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
723 elfcpp::SHF_ALLOC
724 | elfcpp::SHF_WRITE,
725 this->got2_);
727 else
729 this->toc_ = new Output_data_space(8, "** TOC");
730 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
731 elfcpp::SHF_ALLOC
732 | elfcpp::SHF_WRITE,
733 this->toc_);
736 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
737 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
738 this->got_,
739 0, 0, elfcpp::STT_OBJECT,
740 elfcpp::STB_LOCAL,
741 elfcpp::STV_HIDDEN, 0,
742 false, false);
745 return this->got_;
748 // Get the dynamic reloc section, creating it if necessary.
750 template<int size, bool big_endian>
751 typename Target_powerpc<size, big_endian>::Reloc_section*
752 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
754 if (this->rela_dyn_ == NULL)
756 gold_assert(layout != NULL);
757 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
758 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
759 elfcpp::SHF_ALLOC, this->rela_dyn_);
761 return this->rela_dyn_;
764 // A class to handle the PLT data.
766 template<int size, bool big_endian>
767 class Output_data_plt_powerpc : public Output_section_data
769 public:
770 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
771 size, big_endian> Reloc_section;
773 Output_data_plt_powerpc(Layout*);
775 // Add an entry to the PLT.
776 void add_entry(Symbol* gsym);
778 // Return the .rela.plt section data.
779 const Reloc_section* rel_plt() const
781 return this->rel_;
784 protected:
785 void do_adjust_output_section(Output_section* os);
787 private:
788 // The size of an entry in the PLT.
789 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
791 // Set the final size.
792 void
793 set_final_data_size()
795 unsigned int full_count = this->count_ + 4;
797 this->set_data_size(full_count * base_plt_entry_size);
800 // Write out the PLT data.
801 void
802 do_write(Output_file*);
804 // The reloc section.
805 Reloc_section* rel_;
806 // The number of PLT entries.
807 unsigned int count_;
810 // Create the PLT section. The ordinary .got section is an argument,
811 // since we need to refer to the start.
813 template<int size, bool big_endian>
814 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
815 : Output_section_data(size == 32 ? 4 : 8), count_(0)
817 this->rel_ = new Reloc_section(false);
818 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
819 elfcpp::SHF_ALLOC, this->rel_);
822 template<int size, bool big_endian>
823 void
824 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
826 os->set_entsize(0);
829 // Add an entry to the PLT.
831 template<int size, bool big_endian>
832 void
833 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
835 gold_assert(!gsym->has_plt_offset());
836 unsigned int index = this->count_+ + 4;
837 section_offset_type plt_offset;
839 if (index < 8192)
840 plt_offset = index * base_plt_entry_size;
841 else
842 gold_unreachable();
844 gsym->set_plt_offset(plt_offset);
846 ++this->count_;
848 gsym->set_needs_dynsym_entry();
849 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
850 plt_offset, 0);
853 static const unsigned int addis_11_11 = 0x3d6b0000;
854 static const unsigned int addis_11_30 = 0x3d7e0000;
855 static const unsigned int addis_12_12 = 0x3d8c0000;
856 static const unsigned int addi_11_11 = 0x396b0000;
857 static const unsigned int add_0_11_11 = 0x7c0b5a14;
858 static const unsigned int add_11_0_11 = 0x7d605a14;
859 static const unsigned int b = 0x48000000;
860 static const unsigned int bcl_20_31 = 0x429f0005;
861 static const unsigned int bctr = 0x4e800420;
862 static const unsigned int lis_11 = 0x3d600000;
863 static const unsigned int lis_12 = 0x3d800000;
864 static const unsigned int lwzu_0_12 = 0x840c0000;
865 static const unsigned int lwz_0_12 = 0x800c0000;
866 static const unsigned int lwz_11_11 = 0x816b0000;
867 static const unsigned int lwz_11_30 = 0x817e0000;
868 static const unsigned int lwz_12_12 = 0x818c0000;
869 static const unsigned int mflr_0 = 0x7c0802a6;
870 static const unsigned int mflr_12 = 0x7d8802a6;
871 static const unsigned int mtctr_0 = 0x7c0903a6;
872 static const unsigned int mtctr_11 = 0x7d6903a6;
873 static const unsigned int mtlr_0 = 0x7c0803a6;
874 static const unsigned int nop = 0x60000000;
875 static const unsigned int sub_11_11_12 = 0x7d6c5850;
877 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
878 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
879 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
880 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
881 /* ld %r11,xxx+16@l(%r12) */
884 // Write out the PLT.
886 template<int size, bool big_endian>
887 void
888 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
890 const off_t offset = this->offset();
891 const section_size_type oview_size =
892 convert_to_section_size_type(this->data_size());
893 unsigned char* const oview = of->get_output_view(offset, oview_size);
894 unsigned char* pov = oview;
896 memset(pov, 0, base_plt_entry_size * 4);
897 pov += base_plt_entry_size * 4;
899 unsigned int plt_offset = base_plt_entry_size * 4;
900 const unsigned int count = this->count_;
902 if (size == 64)
904 for (unsigned int i = 0; i < count; i++)
908 else
910 for (unsigned int i = 0; i < count; i++)
912 elfcpp::Swap<32, true>::writeval(pov + 0x00,
913 lwz_11_30 + plt_offset);
914 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
915 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
916 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
917 pov += base_plt_entry_size;
918 plt_offset += base_plt_entry_size;
922 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
924 of->write_output_view(offset, oview_size, oview);
927 // Create a PLT entry for a global symbol.
929 template<int size, bool big_endian>
930 void
931 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
932 Layout* layout,
933 Symbol* gsym)
935 if (gsym->has_plt_offset())
936 return;
938 if (this->plt_ == NULL)
940 // Create the GOT section first.
941 this->got_section(symtab, layout);
943 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
944 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
945 (elfcpp::SHF_ALLOC
946 | elfcpp::SHF_EXECINSTR
947 | elfcpp::SHF_WRITE),
948 this->plt_);
950 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
951 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
952 this->plt_,
953 0, 0, elfcpp::STT_OBJECT,
954 elfcpp::STB_LOCAL,
955 elfcpp::STV_HIDDEN, 0,
956 false, false);
959 this->plt_->add_entry(gsym);
962 // Create a GOT entry for the TLS module index.
964 template<int size, bool big_endian>
965 unsigned int
966 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
967 Layout* layout,
968 Sized_relobj<size, big_endian>* object)
970 if (this->got_mod_index_offset_ == -1U)
972 gold_assert(symtab != NULL && layout != NULL && object != NULL);
973 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
974 Output_data_got<size, big_endian>* got;
975 unsigned int got_offset;
977 got = this->got_section(symtab, layout);
978 got_offset = got->add_constant(0);
979 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
980 got_offset, 0);
981 got->add_constant(0);
982 this->got_mod_index_offset_ = got_offset;
984 return this->got_mod_index_offset_;
987 // Optimize the TLS relocation type based on what we know about the
988 // symbol. IS_FINAL is true if the final address of this symbol is
989 // known at link time.
991 static tls::Tls_optimization
992 optimize_tls_reloc(bool /* is_final */, int r_type)
994 // If we are generating a shared library, then we can't do anything
995 // in the linker.
996 if (parameters->options().shared())
997 return tls::TLSOPT_NONE;
998 switch (r_type)
1000 // XXX
1001 default:
1002 gold_unreachable();
1006 // Report an unsupported relocation against a local symbol.
1008 template<int size, bool big_endian>
1009 void
1010 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1011 Sized_relobj<size, big_endian>* object,
1012 unsigned int r_type)
1014 gold_error(_("%s: unsupported reloc %u against local symbol"),
1015 object->name().c_str(), r_type);
1018 // We are about to emit a dynamic relocation of type R_TYPE. If the
1019 // dynamic linker does not support it, issue an error.
1021 template<int size, bool big_endian>
1022 void
1023 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1024 unsigned int r_type)
1026 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1028 // These are the relocation types supported by glibc for both 32-bit
1029 // and 64-bit powerpc.
1030 switch (r_type)
1032 case elfcpp::R_POWERPC_RELATIVE:
1033 case elfcpp::R_POWERPC_GLOB_DAT:
1034 case elfcpp::R_POWERPC_DTPMOD:
1035 case elfcpp::R_POWERPC_DTPREL:
1036 case elfcpp::R_POWERPC_TPREL:
1037 case elfcpp::R_POWERPC_JMP_SLOT:
1038 case elfcpp::R_POWERPC_COPY:
1039 case elfcpp::R_POWERPC_ADDR32:
1040 case elfcpp::R_POWERPC_ADDR24:
1041 case elfcpp::R_POWERPC_REL24:
1042 return;
1044 default:
1045 break;
1048 if (size == 64)
1050 switch (r_type)
1052 // These are the relocation types supported only on 64-bit.
1053 case elfcpp::R_PPC64_ADDR64:
1054 case elfcpp::R_PPC64_TPREL16_LO_DS:
1055 case elfcpp::R_PPC64_TPREL16_DS:
1056 case elfcpp::R_POWERPC_TPREL16:
1057 case elfcpp::R_POWERPC_TPREL16_LO:
1058 case elfcpp::R_POWERPC_TPREL16_HI:
1059 case elfcpp::R_POWERPC_TPREL16_HA:
1060 case elfcpp::R_PPC64_TPREL16_HIGHER:
1061 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1062 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1063 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1064 case elfcpp::R_PPC64_ADDR16_LO_DS:
1065 case elfcpp::R_POWERPC_ADDR16_LO:
1066 case elfcpp::R_POWERPC_ADDR16_HI:
1067 case elfcpp::R_POWERPC_ADDR16_HA:
1068 case elfcpp::R_POWERPC_ADDR30:
1069 case elfcpp::R_PPC64_UADDR64:
1070 case elfcpp::R_POWERPC_UADDR32:
1071 case elfcpp::R_POWERPC_ADDR16:
1072 case elfcpp::R_POWERPC_UADDR16:
1073 case elfcpp::R_PPC64_ADDR16_DS:
1074 case elfcpp::R_PPC64_ADDR16_HIGHER:
1075 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1076 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1077 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1078 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1079 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1080 case elfcpp::R_POWERPC_REL32:
1081 case elfcpp::R_PPC64_REL64:
1082 return;
1084 default:
1085 break;
1088 else
1090 switch (r_type)
1092 // These are the relocation types supported only on 32-bit.
1094 default:
1095 break;
1099 // This prevents us from issuing more than one error per reloc
1100 // section. But we can still wind up issuing more than one
1101 // error per object file.
1102 if (this->issued_non_pic_error_)
1103 return;
1104 gold_assert(parameters->options().output_is_position_independent());
1105 object->error(_("requires unsupported dynamic reloc; "
1106 "recompile with -fPIC"));
1107 this->issued_non_pic_error_ = true;
1108 return;
1111 // Scan a relocation for a local symbol.
1113 template<int size, bool big_endian>
1114 inline void
1115 Target_powerpc<size, big_endian>::Scan::local(
1116 const General_options&,
1117 Symbol_table* symtab,
1118 Layout* layout,
1119 Target_powerpc<size, big_endian>* target,
1120 Sized_relobj<size, big_endian>* object,
1121 unsigned int data_shndx,
1122 Output_section* output_section,
1123 const elfcpp::Rela<size, big_endian>& reloc,
1124 unsigned int r_type,
1125 const elfcpp::Sym<size, big_endian>& lsym)
1127 switch (r_type)
1129 case elfcpp::R_POWERPC_NONE:
1130 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1131 case elfcpp::R_POWERPC_GNU_VTENTRY:
1132 break;
1134 case elfcpp::R_PPC64_ADDR64:
1135 case elfcpp::R_POWERPC_ADDR32:
1136 case elfcpp::R_POWERPC_ADDR16_HA:
1137 case elfcpp::R_POWERPC_ADDR16_LO:
1138 // If building a shared library (or a position-independent
1139 // executable), we need to create a dynamic relocation for
1140 // this location.
1141 if (parameters->options().output_is_position_independent())
1143 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1145 check_non_pic(object, r_type);
1146 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1148 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1149 rela_dyn->add_local(object, r_sym, r_type, output_section,
1150 data_shndx, reloc.get_r_offset(),
1151 reloc.get_r_addend());
1153 else
1155 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1156 gold_assert(lsym.get_st_value() == 0);
1157 rela_dyn->add_local_relative(object, r_sym, r_type,
1158 output_section, data_shndx,
1159 reloc.get_r_offset(),
1160 reloc.get_r_addend());
1163 break;
1165 case elfcpp::R_POWERPC_REL24:
1166 case elfcpp::R_PPC_LOCAL24PC:
1167 case elfcpp::R_POWERPC_REL32:
1168 case elfcpp::R_PPC_REL16_LO:
1169 case elfcpp::R_PPC_REL16_HA:
1170 break;
1172 case elfcpp::R_POWERPC_GOT16:
1173 case elfcpp::R_POWERPC_GOT16_LO:
1174 case elfcpp::R_POWERPC_GOT16_HI:
1175 case elfcpp::R_POWERPC_GOT16_HA:
1176 case elfcpp::R_PPC64_TOC16:
1177 case elfcpp::R_PPC64_TOC16_LO:
1178 case elfcpp::R_PPC64_TOC16_HI:
1179 case elfcpp::R_PPC64_TOC16_HA:
1180 case elfcpp::R_PPC64_TOC16_DS:
1181 case elfcpp::R_PPC64_TOC16_LO_DS:
1183 // The symbol requires a GOT entry.
1184 Output_data_got<size, big_endian>* got;
1185 unsigned int r_sym;
1187 got = target->got_section(symtab, layout);
1188 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1190 // If we are generating a shared object, we need to add a
1191 // dynamic relocation for this symbol's GOT entry.
1192 if (parameters->options().output_is_position_independent())
1194 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1196 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1197 unsigned int off;
1199 off = got->add_constant(0);
1200 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1201 rela_dyn->add_local_relative(object, r_sym,
1202 elfcpp::R_POWERPC_RELATIVE,
1203 got, off, 0);
1206 else
1207 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1209 break;
1211 case elfcpp::R_PPC64_TOC:
1212 // We need a GOT section.
1213 target->got_section(symtab, layout);
1214 break;
1216 // These are relocations which should only be seen by the
1217 // dynamic linker, and should never be seen here.
1218 case elfcpp::R_POWERPC_COPY:
1219 case elfcpp::R_POWERPC_GLOB_DAT:
1220 case elfcpp::R_POWERPC_JMP_SLOT:
1221 case elfcpp::R_POWERPC_RELATIVE:
1222 case elfcpp::R_POWERPC_DTPMOD:
1223 gold_error(_("%s: unexpected reloc %u in object file"),
1224 object->name().c_str(), r_type);
1225 break;
1227 default:
1228 unsupported_reloc_local(object, r_type);
1229 break;
1233 // Report an unsupported relocation against a global symbol.
1235 template<int size, bool big_endian>
1236 void
1237 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1238 Sized_relobj<size, big_endian>* object,
1239 unsigned int r_type,
1240 Symbol* gsym)
1242 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1243 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1246 // Scan a relocation for a global symbol.
1248 template<int size, bool big_endian>
1249 inline void
1250 Target_powerpc<size, big_endian>::Scan::global(
1251 const General_options&,
1252 Symbol_table* symtab,
1253 Layout* layout,
1254 Target_powerpc<size, big_endian>* target,
1255 Sized_relobj<size, big_endian>* object,
1256 unsigned int data_shndx,
1257 Output_section* output_section,
1258 const elfcpp::Rela<size, big_endian>& reloc,
1259 unsigned int r_type,
1260 Symbol* gsym)
1262 switch (r_type)
1264 case elfcpp::R_POWERPC_NONE:
1265 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1266 case elfcpp::R_POWERPC_GNU_VTENTRY:
1267 break;
1269 case elfcpp::R_PPC_PLTREL24:
1270 // If the symbol is fully resolved, this is just a PC32 reloc.
1271 // Otherwise we need a PLT entry.
1272 if (gsym->final_value_is_known())
1273 break;
1274 // If building a shared library, we can also skip the PLT entry
1275 // if the symbol is defined in the output file and is protected
1276 // or hidden.
1277 if (gsym->is_defined()
1278 && !gsym->is_from_dynobj()
1279 && !gsym->is_preemptible())
1280 break;
1281 target->make_plt_entry(symtab, layout, gsym);
1282 break;
1284 case elfcpp::R_POWERPC_ADDR16:
1285 case elfcpp::R_POWERPC_ADDR16_LO:
1286 case elfcpp::R_POWERPC_ADDR16_HI:
1287 case elfcpp::R_POWERPC_ADDR16_HA:
1288 case elfcpp::R_POWERPC_ADDR32:
1289 case elfcpp::R_PPC64_ADDR64:
1291 // Make a PLT entry if necessary.
1292 if (gsym->needs_plt_entry())
1294 target->make_plt_entry(symtab, layout, gsym);
1295 // Since this is not a PC-relative relocation, we may be
1296 // taking the address of a function. In that case we need to
1297 // set the entry in the dynamic symbol table to the address of
1298 // the PLT entry.
1299 if (gsym->is_from_dynobj() && !parameters->options().shared())
1300 gsym->set_needs_dynsym_value();
1302 // Make a dynamic relocation if necessary.
1303 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1305 if (gsym->may_need_copy_reloc())
1307 target->copy_reloc(symtab, layout, object,
1308 data_shndx, output_section, gsym, reloc);
1310 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1311 || r_type == elfcpp::R_PPC64_ADDR64)
1312 && gsym->can_use_relative_reloc(false))
1314 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1315 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1316 output_section, object,
1317 data_shndx, reloc.get_r_offset(),
1318 reloc.get_r_addend());
1320 else
1322 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1324 check_non_pic(object, r_type);
1325 if (gsym->is_from_dynobj()
1326 || gsym->is_undefined()
1327 || gsym->is_preemptible())
1328 rela_dyn->add_global(gsym, r_type, output_section,
1329 object, data_shndx,
1330 reloc.get_r_offset(),
1331 reloc.get_r_addend());
1332 else
1333 rela_dyn->add_global_relative(gsym, r_type,
1334 output_section, object,
1335 data_shndx,
1336 reloc.get_r_offset(),
1337 reloc.get_r_addend());
1341 break;
1343 case elfcpp::R_POWERPC_REL24:
1344 case elfcpp::R_PPC_LOCAL24PC:
1345 case elfcpp::R_PPC_REL16:
1346 case elfcpp::R_PPC_REL16_LO:
1347 case elfcpp::R_PPC_REL16_HI:
1348 case elfcpp::R_PPC_REL16_HA:
1350 if (gsym->needs_plt_entry())
1351 target->make_plt_entry(symtab, layout, gsym);
1352 // Make a dynamic relocation if necessary.
1353 int flags = Symbol::NON_PIC_REF;
1354 if (gsym->type() == elfcpp::STT_FUNC)
1355 flags |= Symbol::FUNCTION_CALL;
1356 if (gsym->needs_dynamic_reloc(flags))
1358 if (gsym->may_need_copy_reloc())
1360 target->copy_reloc(symtab, layout, object,
1361 data_shndx, output_section, gsym,
1362 reloc);
1364 else
1366 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1367 check_non_pic(object, r_type);
1368 rela_dyn->add_global(gsym, r_type, output_section, object,
1369 data_shndx, reloc.get_r_offset(),
1370 reloc.get_r_addend());
1374 break;
1376 case elfcpp::R_POWERPC_GOT16:
1377 case elfcpp::R_POWERPC_GOT16_LO:
1378 case elfcpp::R_POWERPC_GOT16_HI:
1379 case elfcpp::R_POWERPC_GOT16_HA:
1380 case elfcpp::R_PPC64_TOC16:
1381 case elfcpp::R_PPC64_TOC16_LO:
1382 case elfcpp::R_PPC64_TOC16_HI:
1383 case elfcpp::R_PPC64_TOC16_HA:
1384 case elfcpp::R_PPC64_TOC16_DS:
1385 case elfcpp::R_PPC64_TOC16_LO_DS:
1387 // The symbol requires a GOT entry.
1388 Output_data_got<size, big_endian>* got;
1390 got = target->got_section(symtab, layout);
1391 if (gsym->final_value_is_known())
1392 got->add_global(gsym, GOT_TYPE_STANDARD);
1393 else
1395 // If this symbol is not fully resolved, we need to add a
1396 // dynamic relocation for it.
1397 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1398 if (gsym->is_from_dynobj()
1399 || gsym->is_undefined()
1400 || gsym->is_preemptible())
1401 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1402 elfcpp::R_POWERPC_GLOB_DAT);
1403 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1405 unsigned int off = got->add_constant(0);
1407 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1408 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1409 got, off, 0);
1413 break;
1415 case elfcpp::R_PPC64_TOC:
1416 // We need a GOT section.
1417 target->got_section(symtab, layout);
1418 break;
1420 case elfcpp::R_POWERPC_GOT_TPREL16:
1421 case elfcpp::R_POWERPC_TLS:
1422 // XXX TLS
1423 break;
1425 // These are relocations which should only be seen by the
1426 // dynamic linker, and should never be seen here.
1427 case elfcpp::R_POWERPC_COPY:
1428 case elfcpp::R_POWERPC_GLOB_DAT:
1429 case elfcpp::R_POWERPC_JMP_SLOT:
1430 case elfcpp::R_POWERPC_RELATIVE:
1431 case elfcpp::R_POWERPC_DTPMOD:
1432 gold_error(_("%s: unexpected reloc %u in object file"),
1433 object->name().c_str(), r_type);
1434 break;
1436 default:
1437 unsupported_reloc_global(object, r_type, gsym);
1438 break;
1442 // Process relocations for gc.
1444 template<int size, bool big_endian>
1445 void
1446 Target_powerpc<size, big_endian>::gc_process_relocs(
1447 const General_options& options,
1448 Symbol_table* symtab,
1449 Layout* layout,
1450 Sized_relobj<size, big_endian>* object,
1451 unsigned int data_shndx,
1452 unsigned int,
1453 const unsigned char* prelocs,
1454 size_t reloc_count,
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>(
1464 options,
1465 symtab,
1466 layout,
1467 this,
1468 object,
1469 data_shndx,
1470 prelocs,
1471 reloc_count,
1472 output_section,
1473 needs_special_offset_handling,
1474 local_symbol_count,
1475 plocal_symbols);
1478 // Scan relocations for a section.
1480 template<int size, bool big_endian>
1481 void
1482 Target_powerpc<size, big_endian>::scan_relocs(
1483 const General_options& options,
1484 Symbol_table* symtab,
1485 Layout* layout,
1486 Sized_relobj<size, big_endian>* object,
1487 unsigned int data_shndx,
1488 unsigned int sh_type,
1489 const unsigned char* prelocs,
1490 size_t reloc_count,
1491 Output_section* output_section,
1492 bool needs_special_offset_handling,
1493 size_t local_symbol_count,
1494 const unsigned char* plocal_symbols)
1496 typedef Target_powerpc<size, big_endian> Powerpc;
1497 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1498 static Output_data_space* sdata;
1500 if (sh_type == elfcpp::SHT_REL)
1502 gold_error(_("%s: unsupported REL reloc section"),
1503 object->name().c_str());
1504 return;
1507 // Define _SDA_BASE_ at the start of the .sdata section.
1508 if (sdata == NULL)
1510 // layout->find_output_section(".sdata") == NULL
1511 sdata = new Output_data_space(4, "** sdata");
1512 Output_section* os = layout->add_output_section_data(".sdata", 0,
1513 elfcpp::SHF_ALLOC
1514 | elfcpp::SHF_WRITE,
1515 sdata);
1516 symtab->define_in_output_data("_SDA_BASE_", NULL,
1518 32768, 0,
1519 elfcpp::STT_OBJECT,
1520 elfcpp::STB_LOCAL,
1521 elfcpp::STV_HIDDEN, 0,
1522 false, false);
1525 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1526 options,
1527 symtab,
1528 layout,
1529 this,
1530 object,
1531 data_shndx,
1532 prelocs,
1533 reloc_count,
1534 output_section,
1535 needs_special_offset_handling,
1536 local_symbol_count,
1537 plocal_symbols);
1540 // Finalize the sections.
1542 template<int size, bool big_endian>
1543 void
1544 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1546 // Fill in some more dynamic tags.
1547 Output_data_dynamic* const odyn = layout->dynamic_data();
1548 if (odyn != NULL)
1550 if (this->plt_ != NULL)
1552 const Output_data* od = this->plt_->rel_plt();
1553 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1554 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1555 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1557 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1560 if (this->rela_dyn_ != NULL)
1562 const Output_data* od = this->rela_dyn_;
1563 odyn->add_section_address(elfcpp::DT_RELA, od);
1564 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1565 odyn->add_constant(elfcpp::DT_RELAENT,
1566 elfcpp::Elf_sizes<size>::rela_size);
1569 if (!parameters->options().shared())
1571 // The value of the DT_DEBUG tag is filled in by the dynamic
1572 // linker at run time, and used by the debugger.
1573 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1577 // Emit any relocs we saved in an attempt to avoid generating COPY
1578 // relocs.
1579 if (this->copy_relocs_.any_saved_relocs())
1580 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1583 // Perform a relocation.
1585 template<int size, bool big_endian>
1586 inline bool
1587 Target_powerpc<size, big_endian>::Relocate::relocate(
1588 const Relocate_info<size, big_endian>* relinfo,
1589 Target_powerpc* target,
1590 Output_section*,
1591 size_t relnum,
1592 const elfcpp::Rela<size, big_endian>& rela,
1593 unsigned int r_type,
1594 const Sized_symbol<size>* gsym,
1595 const Symbol_value<size>* psymval,
1596 unsigned char* view,
1597 typename elfcpp::Elf_types<size>::Elf_Addr address,
1598 section_size_type /* view_size */)
1600 const unsigned int toc_base_offset = 0x8000;
1601 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1603 // Pick the value to use for symbols defined in shared objects.
1604 Symbol_value<size> symval;
1605 if (gsym != NULL
1606 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1607 || r_type == elfcpp::R_PPC_LOCAL24PC
1608 || r_type == elfcpp::R_PPC_REL16
1609 || r_type == elfcpp::R_PPC_REL16_LO
1610 || r_type == elfcpp::R_PPC_REL16_HI
1611 || r_type == elfcpp::R_PPC_REL16_HA))
1613 elfcpp::Elf_Xword value;
1615 value = target->plt_section()->address() + gsym->plt_offset();
1617 symval.set_output_value(value);
1619 psymval = &symval;
1622 const Sized_relobj<size, big_endian>* object = relinfo->object;
1623 elfcpp::Elf_Xword addend = rela.get_r_addend();
1625 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1626 // pointer points to the beginning, not the end, of the table.
1627 // So we just use the plain offset.
1628 bool have_got_offset = false;
1629 unsigned int got_offset = 0;
1630 unsigned int got2_offset = 0;
1631 switch (r_type)
1633 case elfcpp::R_PPC64_TOC16:
1634 case elfcpp::R_PPC64_TOC16_LO:
1635 case elfcpp::R_PPC64_TOC16_HI:
1636 case elfcpp::R_PPC64_TOC16_HA:
1637 case elfcpp::R_PPC64_TOC16_DS:
1638 case elfcpp::R_PPC64_TOC16_LO_DS:
1639 // Subtract the TOC base address.
1640 addend -= target->toc_section()->address() + toc_base_offset;
1641 /* FALLTHRU */
1643 case elfcpp::R_POWERPC_GOT16:
1644 case elfcpp::R_POWERPC_GOT16_LO:
1645 case elfcpp::R_POWERPC_GOT16_HI:
1646 case elfcpp::R_POWERPC_GOT16_HA:
1647 case elfcpp::R_PPC64_GOT16_DS:
1648 case elfcpp::R_PPC64_GOT16_LO_DS:
1649 if (gsym != NULL)
1651 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1652 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1654 else
1656 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1657 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1658 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1660 have_got_offset = true;
1661 break;
1663 // R_PPC_PLTREL24 is rather special. If non-zero,
1664 // the addend specifies the GOT pointer offset within .got2.
1665 case elfcpp::R_PPC_PLTREL24:
1666 if (addend >= 32768)
1668 Output_data_space* got2;
1669 got2 = target->got2_section();
1670 got2_offset = got2->offset();
1671 addend += got2_offset;
1673 have_got_offset = true;
1674 break;
1676 default:
1677 break;
1680 switch (r_type)
1682 case elfcpp::R_POWERPC_NONE:
1683 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1684 case elfcpp::R_POWERPC_GNU_VTENTRY:
1685 break;
1687 case elfcpp::R_POWERPC_REL32:
1688 Reloc::rel32(view, object, psymval, addend, address);
1689 break;
1691 case elfcpp::R_POWERPC_REL24:
1692 Reloc::rel24(view, object, psymval, addend, address);
1693 break;
1695 case elfcpp::R_POWERPC_REL14:
1696 Reloc::rel14(view, object, psymval, addend, address);
1697 break;
1699 case elfcpp::R_PPC_PLTREL24:
1700 Reloc::rel24(view, object, psymval, addend, address);
1701 break;
1703 case elfcpp::R_PPC_LOCAL24PC:
1704 Reloc::rel24(view, object, psymval, addend, address);
1705 break;
1707 case elfcpp::R_PPC64_ADDR64:
1708 if (!parameters->options().output_is_position_independent())
1709 Relocate_functions<size, big_endian>::rela64(view, object,
1710 psymval, addend);
1711 break;
1713 case elfcpp::R_POWERPC_ADDR32:
1714 if (!parameters->options().output_is_position_independent())
1715 Relocate_functions<size, big_endian>::rela32(view, object,
1716 psymval, addend);
1717 break;
1719 case elfcpp::R_POWERPC_ADDR16_LO:
1720 Reloc::addr16_lo(view, object, psymval, addend);
1721 break;
1723 case elfcpp::R_POWERPC_ADDR16_HI:
1724 Reloc::addr16_hi(view, object, psymval, addend);
1725 break;
1727 case elfcpp::R_POWERPC_ADDR16_HA:
1728 Reloc::addr16_ha(view, object, psymval, addend);
1729 break;
1731 case elfcpp::R_PPC_REL16_LO:
1732 Reloc::rel16_lo(view, object, psymval, addend, address);
1733 break;
1735 case elfcpp::R_PPC_REL16_HI:
1736 Reloc::rel16_lo(view, object, psymval, addend, address);
1737 break;
1739 case elfcpp::R_PPC_REL16_HA:
1740 Reloc::rel16_ha(view, object, psymval, addend, address);
1741 break;
1743 case elfcpp::R_POWERPC_GOT16:
1744 Reloc::addr16(view, got_offset, addend);
1745 break;
1747 case elfcpp::R_POWERPC_GOT16_LO:
1748 Reloc::addr16_lo(view, got_offset, addend);
1749 break;
1751 case elfcpp::R_POWERPC_GOT16_HI:
1752 Reloc::addr16_hi(view, got_offset, addend);
1753 break;
1755 case elfcpp::R_POWERPC_GOT16_HA:
1756 Reloc::addr16_ha(view, got_offset, addend);
1757 break;
1759 case elfcpp::R_PPC64_TOC16:
1760 Reloc::addr16(view, got_offset, addend);
1761 break;
1763 case elfcpp::R_PPC64_TOC16_LO:
1764 Reloc::addr16_lo(view, got_offset, addend);
1765 break;
1767 case elfcpp::R_PPC64_TOC16_HI:
1768 Reloc::addr16_hi(view, got_offset, addend);
1769 break;
1771 case elfcpp::R_PPC64_TOC16_HA:
1772 Reloc::addr16_ha(view, got_offset, addend);
1773 break;
1775 case elfcpp::R_PPC64_TOC16_DS:
1776 case elfcpp::R_PPC64_TOC16_LO_DS:
1777 Reloc::addr16_ds(view, got_offset, addend);
1778 break;
1780 case elfcpp::R_PPC64_TOC:
1782 elfcpp::Elf_types<64>::Elf_Addr value;
1783 value = target->toc_section()->address() + toc_base_offset;
1784 Relocate_functions<64, false>::rela64(view, value, addend);
1786 break;
1788 case elfcpp::R_POWERPC_COPY:
1789 case elfcpp::R_POWERPC_GLOB_DAT:
1790 case elfcpp::R_POWERPC_JMP_SLOT:
1791 case elfcpp::R_POWERPC_RELATIVE:
1792 // This is an outstanding tls reloc, which is unexpected when
1793 // linking.
1794 case elfcpp::R_POWERPC_DTPMOD:
1795 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1796 _("unexpected reloc %u in object file"),
1797 r_type);
1798 break;
1800 default:
1801 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1802 _("unsupported reloc %u"),
1803 r_type);
1804 break;
1807 return true;
1810 // Perform a TLS relocation.
1812 template<int size, bool big_endian>
1813 inline void
1814 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1815 const Relocate_info<size, big_endian>* relinfo,
1816 Target_powerpc<size, big_endian>* target,
1817 size_t relnum,
1818 const elfcpp::Rela<size, big_endian>& rela,
1819 unsigned int r_type,
1820 const Sized_symbol<size>* gsym,
1821 const Symbol_value<size>* psymval,
1822 unsigned char* view,
1823 typename elfcpp::Elf_types<size>::Elf_Addr address,
1824 section_size_type)
1826 Output_segment* tls_segment = relinfo->layout->tls_segment();
1827 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1828 const Sized_relobj<size, big_endian>* object = relinfo->object;
1830 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1831 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1833 const bool is_final =
1834 (gsym == NULL
1835 ? !parameters->options().output_is_position_independent()
1836 : gsym->final_value_is_known());
1837 const tls::Tls_optimization optimized_type
1838 = optimize_tls_reloc(is_final, r_type);
1840 switch (r_type)
1842 // XXX
1846 // Relocate section data.
1848 template<int size, bool big_endian>
1849 void
1850 Target_powerpc<size, big_endian>::relocate_section(
1851 const Relocate_info<size, big_endian>* relinfo,
1852 unsigned int sh_type,
1853 const unsigned char* prelocs,
1854 size_t reloc_count,
1855 Output_section* output_section,
1856 bool needs_special_offset_handling,
1857 unsigned char* view,
1858 typename elfcpp::Elf_types<size>::Elf_Addr address,
1859 section_size_type view_size,
1860 const Reloc_symbol_changes* reloc_symbol_changes)
1862 typedef Target_powerpc<size, big_endian> Powerpc;
1863 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1865 gold_assert(sh_type == elfcpp::SHT_RELA);
1867 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1868 Powerpc_relocate>(
1869 relinfo,
1870 this,
1871 prelocs,
1872 reloc_count,
1873 output_section,
1874 needs_special_offset_handling,
1875 view,
1876 address,
1877 view_size,
1878 reloc_symbol_changes);
1881 // Return the size of a relocation while scanning during a relocatable
1882 // link.
1884 template<int size, bool big_endian>
1885 unsigned int
1886 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1887 unsigned int,
1888 Relobj*)
1890 // We are always SHT_RELA, so we should never get here.
1891 gold_unreachable();
1892 return 0;
1895 // Scan the relocs during a relocatable link.
1897 template<int size, bool big_endian>
1898 void
1899 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1900 const General_options& options,
1901 Symbol_table* symtab,
1902 Layout* layout,
1903 Sized_relobj<size, big_endian>* object,
1904 unsigned int data_shndx,
1905 unsigned int sh_type,
1906 const unsigned char* prelocs,
1907 size_t reloc_count,
1908 Output_section* output_section,
1909 bool needs_special_offset_handling,
1910 size_t local_symbol_count,
1911 const unsigned char* plocal_symbols,
1912 Relocatable_relocs* rr)
1914 gold_assert(sh_type == elfcpp::SHT_RELA);
1916 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1917 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1919 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1920 Scan_relocatable_relocs>(
1921 options,
1922 symtab,
1923 layout,
1924 object,
1925 data_shndx,
1926 prelocs,
1927 reloc_count,
1928 output_section,
1929 needs_special_offset_handling,
1930 local_symbol_count,
1931 plocal_symbols,
1932 rr);
1935 // Relocate a section during a relocatable link.
1937 template<int size, bool big_endian>
1938 void
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,
1943 size_t reloc_count,
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>(
1956 relinfo,
1957 prelocs,
1958 reloc_count,
1959 output_section,
1960 offset_in_output_section,
1962 view,
1963 view_address,
1964 view_size,
1965 reloc_view,
1966 reloc_view_size);
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>
1975 uint64_t
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
1987 public:
1988 Target_selector_powerpc()
1989 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1990 (size == 64 ?
1991 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1992 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1995 Target* do_recognize(int machine, int, int)
1997 switch (size)
1999 case 64:
2000 if (machine != elfcpp::EM_PPC64)
2001 return NULL;
2002 break;
2004 case 32:
2005 if (machine != elfcpp::EM_PPC)
2006 return NULL;
2007 break;
2009 default:
2010 return NULL;
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.