bfd/
[binutils.git] / gold / powerpc.cc
blob05d627be19910834c96b108a41ce4cf8eccafd84
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"
41 namespace
44 using namespace gold;
46 template<int size, bool big_endian>
47 class Output_data_plt_powerpc;
49 template<int size, bool big_endian>
50 class Target_powerpc : public Sized_target<size, big_endian>
52 public:
53 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
55 Target_powerpc()
56 : Sized_target<size, big_endian>(&powerpc_info),
57 got_(NULL), got2_(NULL), toc_(NULL),
58 plt_(NULL), rela_dyn_(NULL),
59 copy_relocs_(elfcpp::R_POWERPC_COPY),
60 dynbss_(NULL), got_mod_index_offset_(-1U)
64 // Process the relocations to determine unreferenced sections for
65 // garbage collection.
66 void
67 gc_process_relocs(const General_options& options,
68 Symbol_table* symtab,
69 Layout* layout,
70 Sized_relobj<size, big_endian>* object,
71 unsigned int data_shndx,
72 unsigned int sh_type,
73 const unsigned char* prelocs,
74 size_t reloc_count,
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.
81 void
82 scan_relocs(const General_options& options,
83 Symbol_table* symtab,
84 Layout* layout,
85 Sized_relobj<size, big_endian>* object,
86 unsigned int data_shndx,
87 unsigned int sh_type,
88 const unsigned char* prelocs,
89 size_t reloc_count,
90 Output_section* output_section,
91 bool needs_special_offset_handling,
92 size_t local_symbol_count,
93 const unsigned char* plocal_symbols);
94 // Finalize the sections.
95 void
96 do_finalize_sections(Layout*);
98 // Return the value to use for a dynamic which requires special
99 // treatment.
100 uint64_t
101 do_dynsym_value(const Symbol*) const;
103 // Relocate a section.
104 void
105 relocate_section(const Relocate_info<size, big_endian>*,
106 unsigned int sh_type,
107 const unsigned char* prelocs,
108 size_t reloc_count,
109 Output_section* output_section,
110 bool needs_special_offset_handling,
111 unsigned char* view,
112 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
113 section_size_type view_size);
115 // Scan the relocs during a relocatable link.
116 void
117 scan_relocatable_relocs(const General_options& options,
118 Symbol_table* symtab,
119 Layout* layout,
120 Sized_relobj<size, big_endian>* object,
121 unsigned int data_shndx,
122 unsigned int sh_type,
123 const unsigned char* prelocs,
124 size_t reloc_count,
125 Output_section* output_section,
126 bool needs_special_offset_handling,
127 size_t local_symbol_count,
128 const unsigned char* plocal_symbols,
129 Relocatable_relocs*);
131 // Relocate a section during a relocatable link.
132 void
133 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
134 unsigned int sh_type,
135 const unsigned char* prelocs,
136 size_t reloc_count,
137 Output_section* output_section,
138 off_t offset_in_output_section,
139 const Relocatable_relocs*,
140 unsigned char* view,
141 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
142 section_size_type view_size,
143 unsigned char* reloc_view,
144 section_size_type reloc_view_size);
146 // Return whether SYM is defined by the ABI.
147 bool
148 do_is_defined_by_abi(const Symbol* sym) const
150 return strcmp(sym->name(), "___tls_get_addr") == 0;
153 // Return the size of the GOT section.
154 section_size_type
155 got_size()
157 gold_assert(this->got_ != NULL);
158 return this->got_->data_size();
161 private:
163 // The class which scans relocations.
164 class Scan
166 public:
167 Scan()
168 : issued_non_pic_error_(false)
171 inline void
172 local(const General_options& options, Symbol_table* symtab,
173 Layout* layout, Target_powerpc* target,
174 Sized_relobj<size, big_endian>* object,
175 unsigned int data_shndx,
176 Output_section* output_section,
177 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
178 const elfcpp::Sym<size, big_endian>& lsym);
180 inline void
181 global(const General_options& options, Symbol_table* symtab,
182 Layout* layout, Target_powerpc* target,
183 Sized_relobj<size, big_endian>* object,
184 unsigned int data_shndx,
185 Output_section* output_section,
186 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
187 Symbol* gsym);
189 private:
190 static void
191 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
192 unsigned int r_type);
194 static void
195 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
196 unsigned int r_type, Symbol*);
198 static void
199 generate_tls_call(Symbol_table* symtab, Layout* layout,
200 Target_powerpc* target);
202 void
203 check_non_pic(Relobj*, unsigned int r_type);
205 // Whether we have issued an error about a non-PIC compilation.
206 bool issued_non_pic_error_;
209 // The class which implements relocation.
210 class Relocate
212 public:
213 // Do a relocation. Return false if the caller should not issue
214 // any warnings about this relocation.
215 inline bool
216 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
217 Output_section*, size_t relnum,
218 const elfcpp::Rela<size, big_endian>&,
219 unsigned int r_type, const Sized_symbol<size>*,
220 const Symbol_value<size>*,
221 unsigned char*,
222 typename elfcpp::Elf_types<size>::Elf_Addr,
223 section_size_type);
225 private:
226 // Do a TLS relocation.
227 inline void
228 relocate_tls(const Relocate_info<size, big_endian>*,
229 Target_powerpc* target,
230 size_t relnum, const elfcpp::Rela<size, big_endian>&,
231 unsigned int r_type, const Sized_symbol<size>*,
232 const Symbol_value<size>*,
233 unsigned char*,
234 typename elfcpp::Elf_types<size>::Elf_Addr,
235 section_size_type);
238 // A class which returns the size required for a relocation type,
239 // used while scanning relocs during a relocatable link.
240 class Relocatable_size_for_reloc
242 public:
243 unsigned int
244 get_size_for_reloc(unsigned int, Relobj*);
247 // Get the GOT section, creating it if necessary.
248 Output_data_got<size, big_endian>*
249 got_section(Symbol_table*, Layout*);
251 Output_data_space*
252 got2_section() const
254 gold_assert (this->got2_ != NULL);
255 return this->got2_;
258 // Get the TOC section.
259 Output_data_space*
260 toc_section() const
262 gold_assert (this->toc_ != NULL);
263 return this->toc_;
266 // Create a PLT entry for a global symbol.
267 void
268 make_plt_entry(Symbol_table*, Layout*, Symbol*);
270 // Create a GOT entry for the TLS module index.
271 unsigned int
272 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
273 Sized_relobj<size, big_endian>* object);
275 // Get the PLT section.
276 const Output_data_plt_powerpc<size, big_endian>*
277 plt_section() const
279 gold_assert(this->plt_ != NULL);
280 return this->plt_;
283 // Get the dynamic reloc section, creating it if necessary.
284 Reloc_section*
285 rela_dyn_section(Layout*);
287 // Copy a relocation against a global symbol.
288 void
289 copy_reloc(Symbol_table* symtab, Layout* layout,
290 Sized_relobj<size, big_endian>* object,
291 unsigned int shndx, Output_section* output_section,
292 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
294 this->copy_relocs_.copy_reloc(symtab, layout,
295 symtab->get_sized_symbol<size>(sym),
296 object, shndx, output_section,
297 reloc, this->rela_dyn_section(layout));
300 // Information about this specific target which we pass to the
301 // general Target structure.
302 static Target::Target_info powerpc_info;
304 // The types of GOT entries needed for this platform.
305 enum Got_type
307 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
308 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
309 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
312 // The GOT section.
313 Output_data_got<size, big_endian>* got_;
314 // The GOT2 section.
315 Output_data_space* got2_;
316 // The TOC section.
317 Output_data_space* toc_;
318 // The PLT section.
319 Output_data_plt_powerpc<size, big_endian>* plt_;
320 // The dynamic reloc section.
321 Reloc_section* rela_dyn_;
322 // Relocs saved to avoid a COPY reloc.
323 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
324 // Space for variables copied with a COPY reloc.
325 Output_data_space* dynbss_;
326 // Offset of the GOT entry for the TLS module index;
327 unsigned int got_mod_index_offset_;
330 template<>
331 Target::Target_info Target_powerpc<32, true>::powerpc_info =
333 32, // size
334 true, // is_big_endian
335 elfcpp::EM_PPC, // machine_code
336 false, // has_make_symbol
337 false, // has_resolve
338 false, // has_code_fill
339 true, // is_default_stack_executable
340 '\0', // wrap_char
341 "/usr/lib/ld.so.1", // dynamic_linker
342 0x10000000, // default_text_segment_address
343 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
344 4 * 1024, // common_pagesize (overridable by -z common-page-size)
345 elfcpp::SHN_UNDEF, // small_common_shndx
346 elfcpp::SHN_UNDEF, // large_common_shndx
347 0, // small_common_section_flags
348 0 // large_common_section_flags
351 template<>
352 Target::Target_info Target_powerpc<32, false>::powerpc_info =
354 32, // size
355 false, // is_big_endian
356 elfcpp::EM_PPC, // machine_code
357 false, // has_make_symbol
358 false, // has_resolve
359 false, // has_code_fill
360 true, // is_default_stack_executable
361 '\0', // wrap_char
362 "/usr/lib/ld.so.1", // dynamic_linker
363 0x10000000, // default_text_segment_address
364 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
365 4 * 1024, // common_pagesize (overridable by -z common-page-size)
366 elfcpp::SHN_UNDEF, // small_common_shndx
367 elfcpp::SHN_UNDEF, // large_common_shndx
368 0, // small_common_section_flags
369 0 // large_common_section_flags
372 template<>
373 Target::Target_info Target_powerpc<64, true>::powerpc_info =
375 64, // size
376 true, // is_big_endian
377 elfcpp::EM_PPC64, // machine_code
378 false, // has_make_symbol
379 false, // has_resolve
380 false, // has_code_fill
381 true, // is_default_stack_executable
382 '\0', // wrap_char
383 "/usr/lib/ld.so.1", // dynamic_linker
384 0x10000000, // default_text_segment_address
385 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
386 8 * 1024, // common_pagesize (overridable by -z common-page-size)
387 elfcpp::SHN_UNDEF, // small_common_shndx
388 elfcpp::SHN_UNDEF, // large_common_shndx
389 0, // small_common_section_flags
390 0 // large_common_section_flags
393 template<>
394 Target::Target_info Target_powerpc<64, false>::powerpc_info =
396 64, // size
397 false, // is_big_endian
398 elfcpp::EM_PPC64, // machine_code
399 false, // has_make_symbol
400 false, // has_resolve
401 false, // has_code_fill
402 true, // is_default_stack_executable
403 '\0', // wrap_char
404 "/usr/lib/ld.so.1", // dynamic_linker
405 0x10000000, // default_text_segment_address
406 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
407 8 * 1024, // common_pagesize (overridable by -z common-page-size)
408 elfcpp::SHN_UNDEF, // small_common_shndx
409 elfcpp::SHN_UNDEF, // large_common_shndx
410 0, // small_common_section_flags
411 0 // large_common_section_flags
414 template<int size, bool big_endian>
415 class Powerpc_relocate_functions
417 private:
418 // Do a simple relocation with the addend in the relocation.
419 template<int valsize>
420 static inline void
421 rela(unsigned char* view,
422 unsigned int right_shift,
423 elfcpp::Elf_Xword dst_mask,
424 typename elfcpp::Swap<size, big_endian>::Valtype value,
425 typename elfcpp::Swap<size, big_endian>::Valtype addend)
427 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
428 Valtype* wv = reinterpret_cast<Valtype*>(view);
429 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
430 Valtype reloc = ((value + addend) >> right_shift);
432 val &= ~dst_mask;
433 reloc &= dst_mask;
435 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
438 // Do a simple relocation using a symbol value with the addend in
439 // the relocation.
440 template<int valsize>
441 static inline void
442 rela(unsigned char* view,
443 unsigned int right_shift,
444 elfcpp::Elf_Xword dst_mask,
445 const Sized_relobj<size, big_endian>* object,
446 const Symbol_value<size>* psymval,
447 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
449 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
450 Valtype* wv = reinterpret_cast<Valtype*>(view);
451 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
452 Valtype reloc = (psymval->value(object, addend) >> right_shift);
454 val &= ~dst_mask;
455 reloc &= dst_mask;
457 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
460 // Do a simple relocation using a symbol value with the addend in
461 // the relocation, unaligned.
462 template<int valsize>
463 static inline void
464 rela_ua(unsigned char* view, unsigned int right_shift,
465 elfcpp::Elf_Xword dst_mask,
466 const Sized_relobj<size, big_endian>* object,
467 const Symbol_value<size>* psymval,
468 typename elfcpp::Swap<size, big_endian>::Valtype addend)
470 typedef typename elfcpp::Swap_unaligned<valsize,
471 big_endian>::Valtype Valtype;
472 unsigned char* wv = view;
473 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
474 Valtype reloc = (psymval->value(object, addend) >> right_shift);
476 val &= ~dst_mask;
477 reloc &= dst_mask;
479 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
482 // Do a simple PC relative relocation with a Symbol_value with the
483 // addend in the relocation.
484 template<int valsize>
485 static inline void
486 pcrela(unsigned char* view, unsigned int right_shift,
487 elfcpp::Elf_Xword dst_mask,
488 const Sized_relobj<size, big_endian>* object,
489 const Symbol_value<size>* psymval,
490 typename elfcpp::Swap<size, big_endian>::Valtype addend,
491 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
494 Valtype* wv = reinterpret_cast<Valtype*>(view);
495 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
496 Valtype reloc = ((psymval->value(object, addend) - address)
497 >> right_shift);
499 val &= ~dst_mask;
500 reloc &= dst_mask;
502 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
505 template<int valsize>
506 static inline void
507 pcrela_unaligned(unsigned char* view,
508 const Sized_relobj<size, big_endian>* object,
509 const Symbol_value<size>* psymval,
510 typename elfcpp::Swap<size, big_endian>::Valtype addend,
511 typename elfcpp::Elf_types<size>::Elf_Addr address)
513 typedef typename elfcpp::Swap_unaligned<valsize,
514 big_endian>::Valtype Valtype;
515 unsigned char* wv = view;
516 Valtype reloc = (psymval->value(object, addend) - address);
518 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
521 typedef Powerpc_relocate_functions<size, big_endian> This;
522 typedef Relocate_functions<size, big_endian> This_reloc;
523 public:
524 // R_POWERPC_REL32: (Symbol + Addend - Address)
525 static inline void
526 rel32(unsigned char* view,
527 const Sized_relobj<size, big_endian>* object,
528 const Symbol_value<size>* psymval,
529 typename elfcpp::Elf_types<size>::Elf_Addr addend,
530 typename elfcpp::Elf_types<size>::Elf_Addr address)
531 { This_reloc::pcrela32(view, object, psymval, addend, address); }
533 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
534 static inline void
535 rel24(unsigned char* view,
536 const Sized_relobj<size, big_endian>* object,
537 const Symbol_value<size>* psymval,
538 typename elfcpp::Elf_types<size>::Elf_Addr addend,
539 typename elfcpp::Elf_types<size>::Elf_Addr address)
541 This::template pcrela<32>(view, 0, 0x03fffffc, object,
542 psymval, addend, address);
545 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
546 static inline void
547 rel14(unsigned char* view,
548 const Sized_relobj<size, big_endian>* object,
549 const Symbol_value<size>* psymval,
550 typename elfcpp::Elf_types<size>::Elf_Addr addend,
551 typename elfcpp::Elf_types<size>::Elf_Addr address)
553 This::template pcrela<32>(view, 0, 0x0000fffc, object,
554 psymval, addend, address);
557 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
558 static inline void
559 addr16(unsigned char* view,
560 typename elfcpp::Elf_types<size>::Elf_Addr value,
561 typename elfcpp::Elf_types<size>::Elf_Addr addend)
562 { This_reloc::rela16(view, value, addend); }
564 static inline void
565 addr16(unsigned char* view,
566 const Sized_relobj<size, big_endian>* object,
567 const Symbol_value<size>* psymval,
568 typename elfcpp::Elf_types<size>::Elf_Addr addend)
569 { This_reloc::rela16(view, object, psymval, addend); }
571 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
572 static inline void
573 addr16_ds(unsigned char* view,
574 typename elfcpp::Elf_types<size>::Elf_Addr value,
575 typename elfcpp::Elf_types<size>::Elf_Addr addend)
577 This::template rela<16>(view, 0, 0xfffc, value, addend);
580 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
581 static inline void
582 addr16_lo(unsigned char* view,
583 typename elfcpp::Elf_types<size>::Elf_Addr value,
584 typename elfcpp::Elf_types<size>::Elf_Addr addend)
585 { This_reloc::rela16(view, value, addend); }
587 static inline void
588 addr16_lo(unsigned char* view,
589 const Sized_relobj<size, big_endian>* object,
590 const Symbol_value<size>* psymval,
591 typename elfcpp::Elf_types<size>::Elf_Addr addend)
592 { This_reloc::rela16(view, object, psymval, addend); }
594 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
595 static inline void
596 addr16_hi(unsigned char* view,
597 typename elfcpp::Elf_types<size>::Elf_Addr value,
598 typename elfcpp::Elf_types<size>::Elf_Addr addend)
600 This::template rela<16>(view, 16, 0xffff, value, addend);
603 static inline void
604 addr16_hi(unsigned char* view,
605 const Sized_relobj<size, big_endian>* object,
606 const Symbol_value<size>* psymval,
607 typename elfcpp::Elf_types<size>::Elf_Addr addend)
609 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
612 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
613 // final value of the low 16 bits of the
614 // relocation is negative, add one.
615 static inline void
616 addr16_ha(unsigned char* view,
617 typename elfcpp::Elf_types<size>::Elf_Addr value,
618 typename elfcpp::Elf_types<size>::Elf_Addr addend)
620 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
622 reloc = value + addend;
624 if (reloc & 0x8000)
625 reloc += 0x10000;
626 reloc >>= 16;
628 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
631 static inline void
632 addr16_ha(unsigned char* view,
633 const Sized_relobj<size, big_endian>* object,
634 const Symbol_value<size>* psymval,
635 typename elfcpp::Elf_types<size>::Elf_Addr addend)
637 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
639 reloc = psymval->value(object, addend);
641 if (reloc & 0x8000)
642 reloc += 0x10000;
643 reloc >>= 16;
645 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
648 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
649 static inline void
650 rel16(unsigned char* view,
651 const Sized_relobj<size, big_endian>* object,
652 const Symbol_value<size>* psymval,
653 typename elfcpp::Elf_types<size>::Elf_Addr addend,
654 typename elfcpp::Elf_types<size>::Elf_Addr address)
655 { This_reloc::pcrela16(view, object, psymval, addend, address); }
657 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
658 static inline void
659 rel16_lo(unsigned char* view,
660 const Sized_relobj<size, big_endian>* object,
661 const Symbol_value<size>* psymval,
662 typename elfcpp::Elf_types<size>::Elf_Addr addend,
663 typename elfcpp::Elf_types<size>::Elf_Addr address)
664 { This_reloc::pcrela16(view, object, psymval, addend, address); }
666 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
667 static inline void
668 rel16_hi(unsigned char* view,
669 const Sized_relobj<size, big_endian>* object,
670 const Symbol_value<size>* psymval,
671 typename elfcpp::Elf_types<size>::Elf_Addr addend,
672 typename elfcpp::Elf_types<size>::Elf_Addr address)
674 This::template pcrela<16>(view, 16, 0xffff, object,
675 psymval, addend, address);
678 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
679 // final value of the low 16 bits of the
680 // relocation is negative, add one.
681 static inline void
682 rel16_ha(unsigned char* view,
683 const Sized_relobj<size, big_endian>* object,
684 const Symbol_value<size>* psymval,
685 typename elfcpp::Elf_types<size>::Elf_Addr addend,
686 typename elfcpp::Elf_types<size>::Elf_Addr address)
688 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
690 reloc = (psymval->value(object, addend) - address);
691 if (reloc & 0x8000)
692 reloc += 0x10000;
693 reloc >>= 16;
695 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
699 // Get the GOT section, creating it if necessary.
701 template<int size, bool big_endian>
702 Output_data_got<size, big_endian>*
703 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
704 Layout* layout)
706 if (this->got_ == NULL)
708 gold_assert(symtab != NULL && layout != NULL);
710 this->got_ = new Output_data_got<size, big_endian>();
712 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
713 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
714 this->got_);
716 // Create the GOT2 or TOC in the .got section.
717 if (size == 32)
719 this->got2_ = new Output_data_space(4, "** GOT2");
720 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
721 elfcpp::SHF_ALLOC
722 | elfcpp::SHF_WRITE,
723 this->got2_);
725 else
727 this->toc_ = new Output_data_space(8, "** TOC");
728 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
729 elfcpp::SHF_ALLOC
730 | elfcpp::SHF_WRITE,
731 this->toc_);
734 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
735 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
736 this->got_,
737 0, 0, elfcpp::STT_OBJECT,
738 elfcpp::STB_LOCAL,
739 elfcpp::STV_HIDDEN, 0,
740 false, false);
743 return this->got_;
746 // Get the dynamic reloc section, creating it if necessary.
748 template<int size, bool big_endian>
749 typename Target_powerpc<size, big_endian>::Reloc_section*
750 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
752 if (this->rela_dyn_ == NULL)
754 gold_assert(layout != NULL);
755 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
756 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
757 elfcpp::SHF_ALLOC, this->rela_dyn_);
759 return this->rela_dyn_;
762 // A class to handle the PLT data.
764 template<int size, bool big_endian>
765 class Output_data_plt_powerpc : public Output_section_data
767 public:
768 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
769 size, big_endian> Reloc_section;
771 Output_data_plt_powerpc(Layout*);
773 // Add an entry to the PLT.
774 void add_entry(Symbol* gsym);
776 // Return the .rela.plt section data.
777 const Reloc_section* rel_plt() const
779 return this->rel_;
782 protected:
783 void do_adjust_output_section(Output_section* os);
785 private:
786 // The size of an entry in the PLT.
787 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
789 // Set the final size.
790 void
791 set_final_data_size()
793 unsigned int full_count = this->count_ + 4;
795 this->set_data_size(full_count * base_plt_entry_size);
798 // Write out the PLT data.
799 void
800 do_write(Output_file*);
802 // The reloc section.
803 Reloc_section* rel_;
804 // The number of PLT entries.
805 unsigned int count_;
808 // Create the PLT section. The ordinary .got section is an argument,
809 // since we need to refer to the start.
811 template<int size, bool big_endian>
812 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
813 : Output_section_data(size == 32 ? 4 : 8), count_(0)
815 this->rel_ = new Reloc_section(false);
816 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
817 elfcpp::SHF_ALLOC, this->rel_);
820 template<int size, bool big_endian>
821 void
822 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
824 os->set_entsize(0);
827 // Add an entry to the PLT.
829 template<int size, bool big_endian>
830 void
831 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
833 gold_assert(!gsym->has_plt_offset());
834 unsigned int index = this->count_+ + 4;
835 section_offset_type plt_offset;
837 if (index < 8192)
838 plt_offset = index * base_plt_entry_size;
839 else
840 gold_unreachable();
842 gsym->set_plt_offset(plt_offset);
844 ++this->count_;
846 gsym->set_needs_dynsym_entry();
847 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
848 plt_offset, 0);
851 static const unsigned int addis_11_11 = 0x3d6b0000;
852 static const unsigned int addis_11_30 = 0x3d7e0000;
853 static const unsigned int addis_12_12 = 0x3d8c0000;
854 static const unsigned int addi_11_11 = 0x396b0000;
855 static const unsigned int add_0_11_11 = 0x7c0b5a14;
856 static const unsigned int add_11_0_11 = 0x7d605a14;
857 static const unsigned int b = 0x48000000;
858 static const unsigned int bcl_20_31 = 0x429f0005;
859 static const unsigned int bctr = 0x4e800420;
860 static const unsigned int lis_11 = 0x3d600000;
861 static const unsigned int lis_12 = 0x3d800000;
862 static const unsigned int lwzu_0_12 = 0x840c0000;
863 static const unsigned int lwz_0_12 = 0x800c0000;
864 static const unsigned int lwz_11_11 = 0x816b0000;
865 static const unsigned int lwz_11_30 = 0x817e0000;
866 static const unsigned int lwz_12_12 = 0x818c0000;
867 static const unsigned int mflr_0 = 0x7c0802a6;
868 static const unsigned int mflr_12 = 0x7d8802a6;
869 static const unsigned int mtctr_0 = 0x7c0903a6;
870 static const unsigned int mtctr_11 = 0x7d6903a6;
871 static const unsigned int mtlr_0 = 0x7c0803a6;
872 static const unsigned int nop = 0x60000000;
873 static const unsigned int sub_11_11_12 = 0x7d6c5850;
875 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
876 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
877 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
878 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
879 /* ld %r11,xxx+16@l(%r12) */
882 // Write out the PLT.
884 template<int size, bool big_endian>
885 void
886 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
888 const off_t offset = this->offset();
889 const section_size_type oview_size =
890 convert_to_section_size_type(this->data_size());
891 unsigned char* const oview = of->get_output_view(offset, oview_size);
892 unsigned char* pov = oview;
894 memset(pov, 0, base_plt_entry_size * 4);
895 pov += base_plt_entry_size * 4;
897 unsigned int plt_offset = base_plt_entry_size * 4;
898 const unsigned int count = this->count_;
900 if (size == 64)
902 for (unsigned int i = 0; i < count; i++)
906 else
908 for (unsigned int i = 0; i < count; i++)
910 elfcpp::Swap<32, true>::writeval(pov + 0x00,
911 lwz_11_30 + plt_offset);
912 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
913 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
914 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
915 pov += base_plt_entry_size;
916 plt_offset += base_plt_entry_size;
920 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
922 of->write_output_view(offset, oview_size, oview);
925 // Create a PLT entry for a global symbol.
927 template<int size, bool big_endian>
928 void
929 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
930 Layout* layout,
931 Symbol* gsym)
933 if (gsym->has_plt_offset())
934 return;
936 if (this->plt_ == NULL)
938 // Create the GOT section first.
939 this->got_section(symtab, layout);
941 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
942 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
943 (elfcpp::SHF_ALLOC
944 | elfcpp::SHF_EXECINSTR
945 | elfcpp::SHF_WRITE),
946 this->plt_);
948 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
949 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
950 this->plt_,
951 0, 0, elfcpp::STT_OBJECT,
952 elfcpp::STB_LOCAL,
953 elfcpp::STV_HIDDEN, 0,
954 false, false);
957 this->plt_->add_entry(gsym);
960 // Create a GOT entry for the TLS module index.
962 template<int size, bool big_endian>
963 unsigned int
964 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
965 Layout* layout,
966 Sized_relobj<size, big_endian>* object)
968 if (this->got_mod_index_offset_ == -1U)
970 gold_assert(symtab != NULL && layout != NULL && object != NULL);
971 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
972 Output_data_got<size, big_endian>* got;
973 unsigned int got_offset;
975 got = this->got_section(symtab, layout);
976 got_offset = got->add_constant(0);
977 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
978 got_offset, 0);
979 got->add_constant(0);
980 this->got_mod_index_offset_ = got_offset;
982 return this->got_mod_index_offset_;
985 // Optimize the TLS relocation type based on what we know about the
986 // symbol. IS_FINAL is true if the final address of this symbol is
987 // known at link time.
989 static tls::Tls_optimization
990 optimize_tls_reloc(bool /* is_final */, int r_type)
992 // If we are generating a shared library, then we can't do anything
993 // in the linker.
994 if (parameters->options().shared())
995 return tls::TLSOPT_NONE;
996 switch (r_type)
998 // XXX
999 default:
1000 gold_unreachable();
1004 // Report an unsupported relocation against a local symbol.
1006 template<int size, bool big_endian>
1007 void
1008 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1009 Sized_relobj<size, big_endian>* object,
1010 unsigned int r_type)
1012 gold_error(_("%s: unsupported reloc %u against local symbol"),
1013 object->name().c_str(), r_type);
1016 // We are about to emit a dynamic relocation of type R_TYPE. If the
1017 // dynamic linker does not support it, issue an error.
1019 template<int size, bool big_endian>
1020 void
1021 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1022 unsigned int r_type)
1024 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1026 // These are the relocation types supported by glibc for both 32-bit
1027 // and 64-bit powerpc.
1028 switch (r_type)
1030 case elfcpp::R_POWERPC_RELATIVE:
1031 case elfcpp::R_POWERPC_GLOB_DAT:
1032 case elfcpp::R_POWERPC_DTPMOD:
1033 case elfcpp::R_POWERPC_DTPREL:
1034 case elfcpp::R_POWERPC_TPREL:
1035 case elfcpp::R_POWERPC_JMP_SLOT:
1036 case elfcpp::R_POWERPC_COPY:
1037 case elfcpp::R_POWERPC_ADDR32:
1038 case elfcpp::R_POWERPC_ADDR24:
1039 case elfcpp::R_POWERPC_REL24:
1040 return;
1042 default:
1043 break;
1046 if (size == 64)
1048 switch (r_type)
1050 // These are the relocation types supported only on 64-bit.
1051 case elfcpp::R_PPC64_ADDR64:
1052 case elfcpp::R_PPC64_TPREL16_LO_DS:
1053 case elfcpp::R_PPC64_TPREL16_DS:
1054 case elfcpp::R_POWERPC_TPREL16:
1055 case elfcpp::R_POWERPC_TPREL16_LO:
1056 case elfcpp::R_POWERPC_TPREL16_HI:
1057 case elfcpp::R_POWERPC_TPREL16_HA:
1058 case elfcpp::R_PPC64_TPREL16_HIGHER:
1059 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1060 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1061 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1062 case elfcpp::R_PPC64_ADDR16_LO_DS:
1063 case elfcpp::R_POWERPC_ADDR16_LO:
1064 case elfcpp::R_POWERPC_ADDR16_HI:
1065 case elfcpp::R_POWERPC_ADDR16_HA:
1066 case elfcpp::R_POWERPC_ADDR30:
1067 case elfcpp::R_PPC64_UADDR64:
1068 case elfcpp::R_POWERPC_UADDR32:
1069 case elfcpp::R_POWERPC_ADDR16:
1070 case elfcpp::R_POWERPC_UADDR16:
1071 case elfcpp::R_PPC64_ADDR16_DS:
1072 case elfcpp::R_PPC64_ADDR16_HIGHER:
1073 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1074 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1075 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1076 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1077 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1078 case elfcpp::R_POWERPC_REL32:
1079 case elfcpp::R_PPC64_REL64:
1080 return;
1082 default:
1083 break;
1086 else
1088 switch (r_type)
1090 // These are the relocation types supported only on 32-bit.
1092 default:
1093 break;
1097 // This prevents us from issuing more than one error per reloc
1098 // section. But we can still wind up issuing more than one
1099 // error per object file.
1100 if (this->issued_non_pic_error_)
1101 return;
1102 gold_assert(parameters->options().output_is_position_independent());
1103 object->error(_("requires unsupported dynamic reloc; "
1104 "recompile with -fPIC"));
1105 this->issued_non_pic_error_ = true;
1106 return;
1109 // Scan a relocation for a local symbol.
1111 template<int size, bool big_endian>
1112 inline void
1113 Target_powerpc<size, big_endian>::Scan::local(
1114 const General_options&,
1115 Symbol_table* symtab,
1116 Layout* layout,
1117 Target_powerpc<size, big_endian>* target,
1118 Sized_relobj<size, big_endian>* object,
1119 unsigned int data_shndx,
1120 Output_section* output_section,
1121 const elfcpp::Rela<size, big_endian>& reloc,
1122 unsigned int r_type,
1123 const elfcpp::Sym<size, big_endian>& lsym)
1125 switch (r_type)
1127 case elfcpp::R_POWERPC_NONE:
1128 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1129 case elfcpp::R_POWERPC_GNU_VTENTRY:
1130 break;
1132 case elfcpp::R_PPC64_ADDR64:
1133 case elfcpp::R_POWERPC_ADDR32:
1134 case elfcpp::R_POWERPC_ADDR16_HA:
1135 case elfcpp::R_POWERPC_ADDR16_LO:
1136 // If building a shared library (or a position-independent
1137 // executable), we need to create a dynamic relocation for
1138 // this location.
1139 if (parameters->options().output_is_position_independent())
1141 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1143 check_non_pic(object, r_type);
1144 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1146 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1147 rela_dyn->add_local(object, r_sym, r_type, output_section,
1148 data_shndx, reloc.get_r_offset(),
1149 reloc.get_r_addend());
1151 else
1153 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1154 gold_assert(lsym.get_st_value() == 0);
1155 rela_dyn->add_local_relative(object, r_sym, r_type,
1156 output_section, data_shndx,
1157 reloc.get_r_offset(),
1158 reloc.get_r_addend());
1161 break;
1163 case elfcpp::R_POWERPC_REL24:
1164 case elfcpp::R_PPC_LOCAL24PC:
1165 case elfcpp::R_POWERPC_REL32:
1166 case elfcpp::R_PPC_REL16_LO:
1167 case elfcpp::R_PPC_REL16_HA:
1168 break;
1170 case elfcpp::R_POWERPC_GOT16:
1171 case elfcpp::R_POWERPC_GOT16_LO:
1172 case elfcpp::R_POWERPC_GOT16_HI:
1173 case elfcpp::R_POWERPC_GOT16_HA:
1174 case elfcpp::R_PPC64_TOC16:
1175 case elfcpp::R_PPC64_TOC16_LO:
1176 case elfcpp::R_PPC64_TOC16_HI:
1177 case elfcpp::R_PPC64_TOC16_HA:
1178 case elfcpp::R_PPC64_TOC16_DS:
1179 case elfcpp::R_PPC64_TOC16_LO_DS:
1181 // The symbol requires a GOT entry.
1182 Output_data_got<size, big_endian>* got;
1183 unsigned int r_sym;
1185 got = target->got_section(symtab, layout);
1186 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1188 // If we are generating a shared object, we need to add a
1189 // dynamic relocation for this symbol's GOT entry.
1190 if (parameters->options().output_is_position_independent())
1192 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1194 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1195 unsigned int off;
1197 off = got->add_constant(0);
1198 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1199 rela_dyn->add_local_relative(object, r_sym,
1200 elfcpp::R_POWERPC_RELATIVE,
1201 got, off, 0);
1204 else
1205 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1207 break;
1209 case elfcpp::R_PPC64_TOC:
1210 // We need a GOT section.
1211 target->got_section(symtab, layout);
1212 break;
1214 // These are relocations which should only be seen by the
1215 // dynamic linker, and should never be seen here.
1216 case elfcpp::R_POWERPC_COPY:
1217 case elfcpp::R_POWERPC_GLOB_DAT:
1218 case elfcpp::R_POWERPC_JMP_SLOT:
1219 case elfcpp::R_POWERPC_RELATIVE:
1220 case elfcpp::R_POWERPC_DTPMOD:
1221 gold_error(_("%s: unexpected reloc %u in object file"),
1222 object->name().c_str(), r_type);
1223 break;
1225 default:
1226 unsupported_reloc_local(object, r_type);
1227 break;
1231 // Report an unsupported relocation against a global symbol.
1233 template<int size, bool big_endian>
1234 void
1235 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1236 Sized_relobj<size, big_endian>* object,
1237 unsigned int r_type,
1238 Symbol* gsym)
1240 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1241 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1244 // Scan a relocation for a global symbol.
1246 template<int size, bool big_endian>
1247 inline void
1248 Target_powerpc<size, big_endian>::Scan::global(
1249 const General_options&,
1250 Symbol_table* symtab,
1251 Layout* layout,
1252 Target_powerpc<size, big_endian>* target,
1253 Sized_relobj<size, big_endian>* object,
1254 unsigned int data_shndx,
1255 Output_section* output_section,
1256 const elfcpp::Rela<size, big_endian>& reloc,
1257 unsigned int r_type,
1258 Symbol* gsym)
1260 switch (r_type)
1262 case elfcpp::R_POWERPC_NONE:
1263 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1264 case elfcpp::R_POWERPC_GNU_VTENTRY:
1265 break;
1267 case elfcpp::R_PPC_PLTREL24:
1268 // If the symbol is fully resolved, this is just a PC32 reloc.
1269 // Otherwise we need a PLT entry.
1270 if (gsym->final_value_is_known())
1271 break;
1272 // If building a shared library, we can also skip the PLT entry
1273 // if the symbol is defined in the output file and is protected
1274 // or hidden.
1275 if (gsym->is_defined()
1276 && !gsym->is_from_dynobj()
1277 && !gsym->is_preemptible())
1278 break;
1279 target->make_plt_entry(symtab, layout, gsym);
1280 break;
1282 case elfcpp::R_POWERPC_ADDR16:
1283 case elfcpp::R_POWERPC_ADDR16_LO:
1284 case elfcpp::R_POWERPC_ADDR16_HI:
1285 case elfcpp::R_POWERPC_ADDR16_HA:
1286 case elfcpp::R_POWERPC_ADDR32:
1287 case elfcpp::R_PPC64_ADDR64:
1289 // Make a PLT entry if necessary.
1290 if (gsym->needs_plt_entry())
1292 target->make_plt_entry(symtab, layout, gsym);
1293 // Since this is not a PC-relative relocation, we may be
1294 // taking the address of a function. In that case we need to
1295 // set the entry in the dynamic symbol table to the address of
1296 // the PLT entry.
1297 if (gsym->is_from_dynobj() && !parameters->options().shared())
1298 gsym->set_needs_dynsym_value();
1300 // Make a dynamic relocation if necessary.
1301 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1303 if (gsym->may_need_copy_reloc())
1305 target->copy_reloc(symtab, layout, object,
1306 data_shndx, output_section, gsym, reloc);
1308 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1309 || r_type == elfcpp::R_PPC64_ADDR64)
1310 && gsym->can_use_relative_reloc(false))
1312 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1313 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1314 output_section, object,
1315 data_shndx, reloc.get_r_offset(),
1316 reloc.get_r_addend());
1318 else
1320 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1322 check_non_pic(object, r_type);
1323 if (gsym->is_from_dynobj()
1324 || gsym->is_undefined()
1325 || gsym->is_preemptible())
1326 rela_dyn->add_global(gsym, r_type, output_section,
1327 object, data_shndx,
1328 reloc.get_r_offset(),
1329 reloc.get_r_addend());
1330 else
1331 rela_dyn->add_global_relative(gsym, r_type,
1332 output_section, object,
1333 data_shndx,
1334 reloc.get_r_offset(),
1335 reloc.get_r_addend());
1339 break;
1341 case elfcpp::R_POWERPC_REL24:
1342 case elfcpp::R_PPC_LOCAL24PC:
1343 case elfcpp::R_PPC_REL16:
1344 case elfcpp::R_PPC_REL16_LO:
1345 case elfcpp::R_PPC_REL16_HI:
1346 case elfcpp::R_PPC_REL16_HA:
1348 if (gsym->needs_plt_entry())
1349 target->make_plt_entry(symtab, layout, gsym);
1350 // Make a dynamic relocation if necessary.
1351 int flags = Symbol::NON_PIC_REF;
1352 if (gsym->type() == elfcpp::STT_FUNC)
1353 flags |= Symbol::FUNCTION_CALL;
1354 if (gsym->needs_dynamic_reloc(flags))
1356 if (gsym->may_need_copy_reloc())
1358 target->copy_reloc(symtab, layout, object,
1359 data_shndx, output_section, gsym,
1360 reloc);
1362 else
1364 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1365 check_non_pic(object, r_type);
1366 rela_dyn->add_global(gsym, r_type, output_section, object,
1367 data_shndx, reloc.get_r_offset(),
1368 reloc.get_r_addend());
1372 break;
1374 case elfcpp::R_POWERPC_GOT16:
1375 case elfcpp::R_POWERPC_GOT16_LO:
1376 case elfcpp::R_POWERPC_GOT16_HI:
1377 case elfcpp::R_POWERPC_GOT16_HA:
1378 case elfcpp::R_PPC64_TOC16:
1379 case elfcpp::R_PPC64_TOC16_LO:
1380 case elfcpp::R_PPC64_TOC16_HI:
1381 case elfcpp::R_PPC64_TOC16_HA:
1382 case elfcpp::R_PPC64_TOC16_DS:
1383 case elfcpp::R_PPC64_TOC16_LO_DS:
1385 // The symbol requires a GOT entry.
1386 Output_data_got<size, big_endian>* got;
1388 got = target->got_section(symtab, layout);
1389 if (gsym->final_value_is_known())
1390 got->add_global(gsym, GOT_TYPE_STANDARD);
1391 else
1393 // If this symbol is not fully resolved, we need to add a
1394 // dynamic relocation for it.
1395 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1396 if (gsym->is_from_dynobj()
1397 || gsym->is_undefined()
1398 || gsym->is_preemptible())
1399 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1400 elfcpp::R_POWERPC_GLOB_DAT);
1401 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1403 unsigned int off = got->add_constant(0);
1405 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1406 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1407 got, off, 0);
1411 break;
1413 case elfcpp::R_PPC64_TOC:
1414 // We need a GOT section.
1415 target->got_section(symtab, layout);
1416 break;
1418 case elfcpp::R_POWERPC_GOT_TPREL16:
1419 case elfcpp::R_POWERPC_TLS:
1420 // XXX TLS
1421 break;
1423 // These are relocations which should only be seen by the
1424 // dynamic linker, and should never be seen here.
1425 case elfcpp::R_POWERPC_COPY:
1426 case elfcpp::R_POWERPC_GLOB_DAT:
1427 case elfcpp::R_POWERPC_JMP_SLOT:
1428 case elfcpp::R_POWERPC_RELATIVE:
1429 case elfcpp::R_POWERPC_DTPMOD:
1430 gold_error(_("%s: unexpected reloc %u in object file"),
1431 object->name().c_str(), r_type);
1432 break;
1434 default:
1435 unsupported_reloc_global(object, r_type, gsym);
1436 break;
1440 // Process relocations for gc.
1442 template<int size, bool big_endian>
1443 void
1444 Target_powerpc<size, big_endian>::gc_process_relocs(
1445 const General_options& options,
1446 Symbol_table* symtab,
1447 Layout* layout,
1448 Sized_relobj<size, big_endian>* object,
1449 unsigned int data_shndx,
1450 unsigned int,
1451 const unsigned char* prelocs,
1452 size_t reloc_count,
1453 Output_section* output_section,
1454 bool needs_special_offset_handling,
1455 size_t local_symbol_count,
1456 const unsigned char* plocal_symbols)
1458 typedef Target_powerpc<size, big_endian> Powerpc;
1459 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1461 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1462 options,
1463 symtab,
1464 layout,
1465 this,
1466 object,
1467 data_shndx,
1468 prelocs,
1469 reloc_count,
1470 output_section,
1471 needs_special_offset_handling,
1472 local_symbol_count,
1473 plocal_symbols);
1476 // Scan relocations for a section.
1478 template<int size, bool big_endian>
1479 void
1480 Target_powerpc<size, big_endian>::scan_relocs(
1481 const General_options& options,
1482 Symbol_table* symtab,
1483 Layout* layout,
1484 Sized_relobj<size, big_endian>* object,
1485 unsigned int data_shndx,
1486 unsigned int sh_type,
1487 const unsigned char* prelocs,
1488 size_t reloc_count,
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());
1502 return;
1505 // Define _SDA_BASE_ at the start of the .sdata section.
1506 if (sdata == NULL)
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,
1511 elfcpp::SHF_ALLOC
1512 | elfcpp::SHF_WRITE,
1513 sdata);
1514 symtab->define_in_output_data("_SDA_BASE_", NULL,
1516 32768, 0,
1517 elfcpp::STT_OBJECT,
1518 elfcpp::STB_LOCAL,
1519 elfcpp::STV_HIDDEN, 0,
1520 false, false);
1523 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1524 options,
1525 symtab,
1526 layout,
1527 this,
1528 object,
1529 data_shndx,
1530 prelocs,
1531 reloc_count,
1532 output_section,
1533 needs_special_offset_handling,
1534 local_symbol_count,
1535 plocal_symbols);
1538 // Finalize the sections.
1540 template<int size, bool big_endian>
1541 void
1542 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1544 // Fill in some more dynamic tags.
1545 Output_data_dynamic* const odyn = layout->dynamic_data();
1546 if (odyn != NULL)
1548 if (this->plt_ != NULL)
1550 const Output_data* od = this->plt_->rel_plt();
1551 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1552 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1553 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1555 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1558 if (this->rela_dyn_ != NULL)
1560 const Output_data* od = this->rela_dyn_;
1561 odyn->add_section_address(elfcpp::DT_RELA, od);
1562 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1563 odyn->add_constant(elfcpp::DT_RELAENT,
1564 elfcpp::Elf_sizes<size>::rela_size);
1567 if (!parameters->options().shared())
1569 // The value of the DT_DEBUG tag is filled in by the dynamic
1570 // linker at run time, and used by the debugger.
1571 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1575 // Emit any relocs we saved in an attempt to avoid generating COPY
1576 // relocs.
1577 if (this->copy_relocs_.any_saved_relocs())
1578 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1581 // Perform a relocation.
1583 template<int size, bool big_endian>
1584 inline bool
1585 Target_powerpc<size, big_endian>::Relocate::relocate(
1586 const Relocate_info<size, big_endian>* relinfo,
1587 Target_powerpc* target,
1588 Output_section*,
1589 size_t relnum,
1590 const elfcpp::Rela<size, big_endian>& rela,
1591 unsigned int r_type,
1592 const Sized_symbol<size>* gsym,
1593 const Symbol_value<size>* psymval,
1594 unsigned char* view,
1595 typename elfcpp::Elf_types<size>::Elf_Addr address,
1596 section_size_type /* view_size */)
1598 const unsigned int toc_base_offset = 0x8000;
1599 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1601 // Pick the value to use for symbols defined in shared objects.
1602 Symbol_value<size> symval;
1603 if (gsym != NULL
1604 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1605 || r_type == elfcpp::R_PPC_LOCAL24PC
1606 || r_type == elfcpp::R_PPC_REL16
1607 || r_type == elfcpp::R_PPC_REL16_LO
1608 || r_type == elfcpp::R_PPC_REL16_HI
1609 || r_type == elfcpp::R_PPC_REL16_HA))
1611 elfcpp::Elf_Xword value;
1613 value = target->plt_section()->address() + gsym->plt_offset();
1615 symval.set_output_value(value);
1617 psymval = &symval;
1620 const Sized_relobj<size, big_endian>* object = relinfo->object;
1621 elfcpp::Elf_Xword addend = rela.get_r_addend();
1623 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1624 // pointer points to the beginning, not the end, of the table.
1625 // So we just use the plain offset.
1626 bool have_got_offset = false;
1627 unsigned int got_offset = 0;
1628 unsigned int got2_offset = 0;
1629 switch (r_type)
1631 case elfcpp::R_PPC64_TOC16:
1632 case elfcpp::R_PPC64_TOC16_LO:
1633 case elfcpp::R_PPC64_TOC16_HI:
1634 case elfcpp::R_PPC64_TOC16_HA:
1635 case elfcpp::R_PPC64_TOC16_DS:
1636 case elfcpp::R_PPC64_TOC16_LO_DS:
1637 // Subtract the TOC base address.
1638 addend -= target->toc_section()->address() + toc_base_offset;
1639 /* FALLTHRU */
1641 case elfcpp::R_POWERPC_GOT16:
1642 case elfcpp::R_POWERPC_GOT16_LO:
1643 case elfcpp::R_POWERPC_GOT16_HI:
1644 case elfcpp::R_POWERPC_GOT16_HA:
1645 case elfcpp::R_PPC64_GOT16_DS:
1646 case elfcpp::R_PPC64_GOT16_LO_DS:
1647 if (gsym != NULL)
1649 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1650 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1652 else
1654 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1655 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1656 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1658 have_got_offset = true;
1659 break;
1661 // R_PPC_PLTREL24 is rather special. If non-zero,
1662 // the addend specifies the GOT pointer offset within .got2.
1663 case elfcpp::R_PPC_PLTREL24:
1664 if (addend >= 32768)
1666 Output_data_space* got2;
1667 got2 = target->got2_section();
1668 got2_offset = got2->offset();
1669 addend += got2_offset;
1671 have_got_offset = true;
1672 break;
1674 default:
1675 break;
1678 switch (r_type)
1680 case elfcpp::R_POWERPC_NONE:
1681 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1682 case elfcpp::R_POWERPC_GNU_VTENTRY:
1683 break;
1685 case elfcpp::R_POWERPC_REL32:
1686 Reloc::rel32(view, object, psymval, addend, address);
1687 break;
1689 case elfcpp::R_POWERPC_REL24:
1690 Reloc::rel24(view, object, psymval, addend, address);
1691 break;
1693 case elfcpp::R_POWERPC_REL14:
1694 Reloc::rel14(view, object, psymval, addend, address);
1695 break;
1697 case elfcpp::R_PPC_PLTREL24:
1698 Reloc::rel24(view, object, psymval, addend, address);
1699 break;
1701 case elfcpp::R_PPC_LOCAL24PC:
1702 Reloc::rel24(view, object, psymval, addend, address);
1703 break;
1705 case elfcpp::R_PPC64_ADDR64:
1706 if (!parameters->options().output_is_position_independent())
1707 Relocate_functions<size, big_endian>::rela64(view, object,
1708 psymval, addend);
1709 break;
1711 case elfcpp::R_POWERPC_ADDR32:
1712 if (!parameters->options().output_is_position_independent())
1713 Relocate_functions<size, big_endian>::rela32(view, object,
1714 psymval, addend);
1715 break;
1717 case elfcpp::R_POWERPC_ADDR16_LO:
1718 Reloc::addr16_lo(view, object, psymval, addend);
1719 break;
1721 case elfcpp::R_POWERPC_ADDR16_HI:
1722 Reloc::addr16_hi(view, object, psymval, addend);
1723 break;
1725 case elfcpp::R_POWERPC_ADDR16_HA:
1726 Reloc::addr16_ha(view, object, psymval, addend);
1727 break;
1729 case elfcpp::R_PPC_REL16_LO:
1730 Reloc::rel16_lo(view, object, psymval, addend, address);
1731 break;
1733 case elfcpp::R_PPC_REL16_HI:
1734 Reloc::rel16_lo(view, object, psymval, addend, address);
1735 break;
1737 case elfcpp::R_PPC_REL16_HA:
1738 Reloc::rel16_ha(view, object, psymval, addend, address);
1739 break;
1741 case elfcpp::R_POWERPC_GOT16:
1742 Reloc::addr16(view, got_offset, addend);
1743 break;
1745 case elfcpp::R_POWERPC_GOT16_LO:
1746 Reloc::addr16_lo(view, got_offset, addend);
1747 break;
1749 case elfcpp::R_POWERPC_GOT16_HI:
1750 Reloc::addr16_hi(view, got_offset, addend);
1751 break;
1753 case elfcpp::R_POWERPC_GOT16_HA:
1754 Reloc::addr16_ha(view, got_offset, addend);
1755 break;
1757 case elfcpp::R_PPC64_TOC16:
1758 Reloc::addr16(view, got_offset, addend);
1759 break;
1761 case elfcpp::R_PPC64_TOC16_LO:
1762 Reloc::addr16_lo(view, got_offset, addend);
1763 break;
1765 case elfcpp::R_PPC64_TOC16_HI:
1766 Reloc::addr16_hi(view, got_offset, addend);
1767 break;
1769 case elfcpp::R_PPC64_TOC16_HA:
1770 Reloc::addr16_ha(view, got_offset, addend);
1771 break;
1773 case elfcpp::R_PPC64_TOC16_DS:
1774 case elfcpp::R_PPC64_TOC16_LO_DS:
1775 Reloc::addr16_ds(view, got_offset, addend);
1776 break;
1778 case elfcpp::R_PPC64_TOC:
1780 elfcpp::Elf_types<64>::Elf_Addr value;
1781 value = target->toc_section()->address() + toc_base_offset;
1782 Relocate_functions<64, false>::rela64(view, value, addend);
1784 break;
1786 case elfcpp::R_POWERPC_COPY:
1787 case elfcpp::R_POWERPC_GLOB_DAT:
1788 case elfcpp::R_POWERPC_JMP_SLOT:
1789 case elfcpp::R_POWERPC_RELATIVE:
1790 // This is an outstanding tls reloc, which is unexpected when
1791 // linking.
1792 case elfcpp::R_POWERPC_DTPMOD:
1793 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1794 _("unexpected reloc %u in object file"),
1795 r_type);
1796 break;
1798 default:
1799 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1800 _("unsupported reloc %u"),
1801 r_type);
1802 break;
1805 return true;
1808 // Perform a TLS relocation.
1810 template<int size, bool big_endian>
1811 inline void
1812 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1813 const Relocate_info<size, big_endian>* relinfo,
1814 Target_powerpc<size, big_endian>* target,
1815 size_t relnum,
1816 const elfcpp::Rela<size, big_endian>& rela,
1817 unsigned int r_type,
1818 const Sized_symbol<size>* gsym,
1819 const Symbol_value<size>* psymval,
1820 unsigned char* view,
1821 typename elfcpp::Elf_types<size>::Elf_Addr address,
1822 section_size_type)
1824 Output_segment* tls_segment = relinfo->layout->tls_segment();
1825 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1826 const Sized_relobj<size, big_endian>* object = relinfo->object;
1828 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1829 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1831 const bool is_final =
1832 (gsym == NULL
1833 ? !parameters->options().output_is_position_independent()
1834 : gsym->final_value_is_known());
1835 const tls::Tls_optimization optimized_type
1836 = optimize_tls_reloc(is_final, r_type);
1838 switch (r_type)
1840 // XXX
1844 // Relocate section data.
1846 template<int size, bool big_endian>
1847 void
1848 Target_powerpc<size, big_endian>::relocate_section(
1849 const Relocate_info<size, big_endian>* relinfo,
1850 unsigned int sh_type,
1851 const unsigned char* prelocs,
1852 size_t reloc_count,
1853 Output_section* output_section,
1854 bool needs_special_offset_handling,
1855 unsigned char* view,
1856 typename elfcpp::Elf_types<size>::Elf_Addr address,
1857 section_size_type view_size)
1859 typedef Target_powerpc<size, big_endian> Powerpc;
1860 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1862 gold_assert(sh_type == elfcpp::SHT_RELA);
1864 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1865 Powerpc_relocate>(
1866 relinfo,
1867 this,
1868 prelocs,
1869 reloc_count,
1870 output_section,
1871 needs_special_offset_handling,
1872 view,
1873 address,
1874 view_size);
1877 // Return the size of a relocation while scanning during a relocatable
1878 // link.
1880 template<int size, bool big_endian>
1881 unsigned int
1882 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1883 unsigned int,
1884 Relobj*)
1886 // We are always SHT_RELA, so we should never get here.
1887 gold_unreachable();
1888 return 0;
1891 // Scan the relocs during a relocatable link.
1893 template<int size, bool big_endian>
1894 void
1895 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1896 const General_options& options,
1897 Symbol_table* symtab,
1898 Layout* layout,
1899 Sized_relobj<size, big_endian>* object,
1900 unsigned int data_shndx,
1901 unsigned int sh_type,
1902 const unsigned char* prelocs,
1903 size_t reloc_count,
1904 Output_section* output_section,
1905 bool needs_special_offset_handling,
1906 size_t local_symbol_count,
1907 const unsigned char* plocal_symbols,
1908 Relocatable_relocs* rr)
1910 gold_assert(sh_type == elfcpp::SHT_RELA);
1912 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1913 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1915 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1916 Scan_relocatable_relocs>(
1917 options,
1918 symtab,
1919 layout,
1920 object,
1921 data_shndx,
1922 prelocs,
1923 reloc_count,
1924 output_section,
1925 needs_special_offset_handling,
1926 local_symbol_count,
1927 plocal_symbols,
1928 rr);
1931 // Relocate a section during a relocatable link.
1933 template<int size, bool big_endian>
1934 void
1935 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1936 const Relocate_info<size, big_endian>* relinfo,
1937 unsigned int sh_type,
1938 const unsigned char* prelocs,
1939 size_t reloc_count,
1940 Output_section* output_section,
1941 off_t offset_in_output_section,
1942 const Relocatable_relocs* rr,
1943 unsigned char* view,
1944 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1945 section_size_type view_size,
1946 unsigned char* reloc_view,
1947 section_size_type reloc_view_size)
1949 gold_assert(sh_type == elfcpp::SHT_RELA);
1951 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1952 relinfo,
1953 prelocs,
1954 reloc_count,
1955 output_section,
1956 offset_in_output_section,
1958 view,
1959 view_address,
1960 view_size,
1961 reloc_view,
1962 reloc_view_size);
1965 // Return the value to use for a dynamic which requires special
1966 // treatment. This is how we support equality comparisons of function
1967 // pointers across shared library boundaries, as described in the
1968 // processor specific ABI supplement.
1970 template<int size, bool big_endian>
1971 uint64_t
1972 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1974 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1975 return this->plt_section()->address() + gsym->plt_offset();
1978 // The selector for powerpc object files.
1980 template<int size, bool big_endian>
1981 class Target_selector_powerpc : public Target_selector
1983 public:
1984 Target_selector_powerpc()
1985 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1986 (size == 64 ?
1987 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1988 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1991 Target* do_recognize(int machine, int, int)
1993 switch (size)
1995 case 64:
1996 if (machine != elfcpp::EM_PPC64)
1997 return NULL;
1998 break;
2000 case 32:
2001 if (machine != elfcpp::EM_PPC)
2002 return NULL;
2003 break;
2005 default:
2006 return NULL;
2009 return this->instantiate_target();
2012 Target* do_instantiate_target()
2013 { return new Target_powerpc<size, big_endian>(); }
2016 Target_selector_powerpc<32, true> target_selector_ppc32;
2017 Target_selector_powerpc<32, false> target_selector_ppc32le;
2018 Target_selector_powerpc<64, true> target_selector_ppc64;
2019 Target_selector_powerpc<64, false> target_selector_ppc64le;
2021 } // End anonymous namespace.