Remove STT_IFUNC support.
[binutils.git] / gold / powerpc.cc
blob05ce4fcc1ecfe60b3c97f2f07cd9e9456a5c8218
1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008 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 // Scan the relocations to look for symbol adjustments.
65 void
66 scan_relocs(const General_options& options,
67 Symbol_table* symtab,
68 Layout* layout,
69 Sized_relobj<size, big_endian>* object,
70 unsigned int data_shndx,
71 unsigned int sh_type,
72 const unsigned char* prelocs,
73 size_t reloc_count,
74 Output_section* output_section,
75 bool needs_special_offset_handling,
76 size_t local_symbol_count,
77 const unsigned char* plocal_symbols);
78 // Finalize the sections.
79 void
80 do_finalize_sections(Layout*);
82 // Return the value to use for a dynamic which requires special
83 // treatment.
84 uint64_t
85 do_dynsym_value(const Symbol*) const;
87 // Relocate a section.
88 void
89 relocate_section(const Relocate_info<size, big_endian>*,
90 unsigned int sh_type,
91 const unsigned char* prelocs,
92 size_t reloc_count,
93 Output_section* output_section,
94 bool needs_special_offset_handling,
95 unsigned char* view,
96 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
97 section_size_type view_size);
99 // Scan the relocs during a relocatable link.
100 void
101 scan_relocatable_relocs(const General_options& options,
102 Symbol_table* symtab,
103 Layout* layout,
104 Sized_relobj<size, big_endian>* object,
105 unsigned int data_shndx,
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 size_t local_symbol_count,
112 const unsigned char* plocal_symbols,
113 Relocatable_relocs*);
115 // Relocate a section during a relocatable link.
116 void
117 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
118 unsigned int sh_type,
119 const unsigned char* prelocs,
120 size_t reloc_count,
121 Output_section* output_section,
122 off_t offset_in_output_section,
123 const Relocatable_relocs*,
124 unsigned char* view,
125 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
126 section_size_type view_size,
127 unsigned char* reloc_view,
128 section_size_type reloc_view_size);
130 // Return whether SYM is defined by the ABI.
131 bool
132 do_is_defined_by_abi(const Symbol* sym) const
134 return strcmp(sym->name(), "___tls_get_addr") == 0;
137 // Return the size of the GOT section.
138 section_size_type
139 got_size()
141 gold_assert(this->got_ != NULL);
142 return this->got_->data_size();
145 private:
147 // The class which scans relocations.
148 class Scan
150 public:
151 Scan()
152 : issued_non_pic_error_(false)
155 inline void
156 local(const General_options& options, Symbol_table* symtab,
157 Layout* layout, Target_powerpc* target,
158 Sized_relobj<size, big_endian>* object,
159 unsigned int data_shndx,
160 Output_section* output_section,
161 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
162 const elfcpp::Sym<size, big_endian>& lsym);
164 inline void
165 global(const General_options& options, Symbol_table* symtab,
166 Layout* layout, Target_powerpc* target,
167 Sized_relobj<size, big_endian>* object,
168 unsigned int data_shndx,
169 Output_section* output_section,
170 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
171 Symbol* gsym);
173 private:
174 static void
175 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
176 unsigned int r_type);
178 static void
179 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
180 unsigned int r_type, Symbol*);
182 static void
183 generate_tls_call(Symbol_table* symtab, Layout* layout,
184 Target_powerpc* target);
186 void
187 check_non_pic(Relobj*, unsigned int r_type);
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_;
193 // The class which implements relocation.
194 class Relocate
196 public:
197 // Do a relocation. Return false if the caller should not issue
198 // any warnings about this relocation.
199 inline bool
200 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
201 size_t relnum, const elfcpp::Rela<size, big_endian>&,
202 unsigned int r_type, const Sized_symbol<size>*,
203 const Symbol_value<size>*,
204 unsigned char*,
205 typename elfcpp::Elf_types<size>::Elf_Addr,
206 section_size_type);
208 private:
209 // Do a TLS relocation.
210 inline void
211 relocate_tls(const Relocate_info<size, big_endian>*,
212 Target_powerpc* target,
213 size_t relnum, const elfcpp::Rela<size, big_endian>&,
214 unsigned int r_type, const Sized_symbol<size>*,
215 const Symbol_value<size>*,
216 unsigned char*,
217 typename elfcpp::Elf_types<size>::Elf_Addr,
218 section_size_type);
221 // A class which returns the size required for a relocation type,
222 // used while scanning relocs during a relocatable link.
223 class Relocatable_size_for_reloc
225 public:
226 unsigned int
227 get_size_for_reloc(unsigned int, Relobj*);
230 // Get the GOT section, creating it if necessary.
231 Output_data_got<size, big_endian>*
232 got_section(Symbol_table*, Layout*);
234 Output_data_space*
235 got2_section() const
237 gold_assert (this->got2_ != NULL);
238 return this->got2_;
241 // Get the TOC section.
242 Output_data_space*
243 toc_section() const
245 gold_assert (this->toc_ != NULL);
246 return this->toc_;
249 // Create a PLT entry for a global symbol.
250 void
251 make_plt_entry(Symbol_table*, Layout*, Symbol*);
253 // Create a GOT entry for the TLS module index.
254 unsigned int
255 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
256 Sized_relobj<size, big_endian>* object);
258 // Get the PLT section.
259 const Output_data_plt_powerpc<size, big_endian>*
260 plt_section() const
262 gold_assert(this->plt_ != NULL);
263 return this->plt_;
266 // Get the dynamic reloc section, creating it if necessary.
267 Reloc_section*
268 rela_dyn_section(Layout*);
270 // Return true if the symbol may need a COPY relocation.
271 // References from an executable object to non-function symbols
272 // defined in a dynamic object may need a COPY relocation.
273 bool
274 may_need_copy_reloc(Symbol* gsym)
276 return (!parameters->options().shared()
277 && gsym->is_from_dynobj()
278 && gsym->type() != elfcpp::STT_FUNC);
281 // Copy a relocation against a global symbol.
282 void
283 copy_reloc(Symbol_table* symtab, Layout* layout,
284 Sized_relobj<size, big_endian>* object,
285 unsigned int shndx, Output_section* output_section,
286 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
288 this->copy_relocs_.copy_reloc(symtab, layout,
289 symtab->get_sized_symbol<size>(sym),
290 object, shndx, output_section,
291 reloc, this->rela_dyn_section(layout));
294 // Information about this specific target which we pass to the
295 // general Target structure.
296 static Target::Target_info powerpc_info;
298 // The types of GOT entries needed for this platform.
299 enum Got_type
301 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
302 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
303 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
306 // The GOT section.
307 Output_data_got<size, big_endian>* got_;
308 // The GOT2 section.
309 Output_data_space* got2_;
310 // The TOC section.
311 Output_data_space* toc_;
312 // The PLT section.
313 Output_data_plt_powerpc<size, big_endian>* plt_;
314 // The dynamic reloc section.
315 Reloc_section* rela_dyn_;
316 // Relocs saved to avoid a COPY reloc.
317 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
318 // Space for variables copied with a COPY reloc.
319 Output_data_space* dynbss_;
320 // Offset of the GOT entry for the TLS module index;
321 unsigned int got_mod_index_offset_;
324 template<>
325 Target::Target_info Target_powerpc<32, true>::powerpc_info =
327 32, // size
328 true, // is_big_endian
329 elfcpp::EM_PPC, // machine_code
330 false, // has_make_symbol
331 false, // has_resolve
332 false, // has_code_fill
333 true, // is_default_stack_executable
334 '\0', // wrap_char
335 "/usr/lib/ld.so.1", // dynamic_linker
336 0x10000000, // default_text_segment_address
337 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
338 4 * 1024 // common_pagesize (overridable by -z common-page-size)
341 template<>
342 Target::Target_info Target_powerpc<32, false>::powerpc_info =
344 32, // size
345 false, // is_big_endian
346 elfcpp::EM_PPC, // machine_code
347 false, // has_make_symbol
348 false, // has_resolve
349 false, // has_code_fill
350 true, // is_default_stack_executable
351 '\0', // wrap_char
352 "/usr/lib/ld.so.1", // dynamic_linker
353 0x10000000, // default_text_segment_address
354 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
355 4 * 1024 // common_pagesize (overridable by -z common-page-size)
358 template<>
359 Target::Target_info Target_powerpc<64, true>::powerpc_info =
361 64, // size
362 true, // is_big_endian
363 elfcpp::EM_PPC64, // machine_code
364 false, // has_make_symbol
365 false, // has_resolve
366 false, // has_code_fill
367 true, // is_default_stack_executable
368 '\0', // wrap_char
369 "/usr/lib/ld.so.1", // dynamic_linker
370 0x10000000, // default_text_segment_address
371 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
372 8 * 1024 // common_pagesize (overridable by -z common-page-size)
375 template<>
376 Target::Target_info Target_powerpc<64, false>::powerpc_info =
378 64, // size
379 false, // is_big_endian
380 elfcpp::EM_PPC64, // machine_code
381 false, // has_make_symbol
382 false, // has_resolve
383 false, // has_code_fill
384 true, // is_default_stack_executable
385 '\0', // wrap_char
386 "/usr/lib/ld.so.1", // dynamic_linker
387 0x10000000, // default_text_segment_address
388 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
389 8 * 1024 // common_pagesize (overridable by -z common-page-size)
392 template<int size, bool big_endian>
393 class Powerpc_relocate_functions
395 private:
396 // Do a simple relocation with the addend in the relocation.
397 template<int valsize>
398 static inline void
399 rela(unsigned char* view,
400 unsigned int right_shift,
401 elfcpp::Elf_Xword dst_mask,
402 typename elfcpp::Swap<size, big_endian>::Valtype value,
403 typename elfcpp::Swap<size, big_endian>::Valtype addend)
405 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
406 Valtype* wv = reinterpret_cast<Valtype*>(view);
407 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
408 Valtype reloc = ((value + addend) >> right_shift);
410 val &= ~dst_mask;
411 reloc &= dst_mask;
413 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
416 // Do a simple relocation using a symbol value with the addend in
417 // the relocation.
418 template<int valsize>
419 static inline void
420 rela(unsigned char* view,
421 unsigned int right_shift,
422 elfcpp::Elf_Xword dst_mask,
423 const Sized_relobj<size, big_endian>* object,
424 const Symbol_value<size>* psymval,
425 typename elfcpp::Swap<valsize, 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 = (psymval->value(object, 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, unaligned.
440 template<int valsize>
441 static inline void
442 rela_ua(unsigned char* view, unsigned int right_shift,
443 elfcpp::Elf_Xword dst_mask,
444 const Sized_relobj<size, big_endian>* object,
445 const Symbol_value<size>* psymval,
446 typename elfcpp::Swap<size, big_endian>::Valtype addend)
448 typedef typename elfcpp::Swap_unaligned<valsize,
449 big_endian>::Valtype Valtype;
450 unsigned char* wv = view;
451 Valtype val = elfcpp::Swap_unaligned<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_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
460 // Do a simple PC relative relocation with a Symbol_value with the
461 // addend in the relocation.
462 template<int valsize>
463 static inline void
464 pcrela(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,
469 typename elfcpp::Elf_types<size>::Elf_Addr address)
471 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
472 Valtype* wv = reinterpret_cast<Valtype*>(view);
473 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
474 Valtype reloc = ((psymval->value(object, addend) - address)
475 >> right_shift);
477 val &= ~dst_mask;
478 reloc &= dst_mask;
480 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
483 template<int valsize>
484 static inline void
485 pcrela_unaligned(unsigned char* view,
486 const Sized_relobj<size, big_endian>* object,
487 const Symbol_value<size>* psymval,
488 typename elfcpp::Swap<size, big_endian>::Valtype addend,
489 typename elfcpp::Elf_types<size>::Elf_Addr address)
491 typedef typename elfcpp::Swap_unaligned<valsize,
492 big_endian>::Valtype Valtype;
493 unsigned char* wv = view;
494 Valtype reloc = (psymval->value(object, addend) - address);
496 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
499 typedef Powerpc_relocate_functions<size, big_endian> This;
500 typedef Relocate_functions<size, big_endian> This_reloc;
501 public:
502 // R_POWERPC_REL32: (Symbol + Addend - Address)
503 static inline void
504 rel32(unsigned char* view,
505 const Sized_relobj<size, big_endian>* object,
506 const Symbol_value<size>* psymval,
507 typename elfcpp::Elf_types<size>::Elf_Addr addend,
508 typename elfcpp::Elf_types<size>::Elf_Addr address)
509 { This_reloc::pcrela32(view, object, psymval, addend, address); }
511 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
512 static inline void
513 rel24(unsigned char* view,
514 const Sized_relobj<size, big_endian>* object,
515 const Symbol_value<size>* psymval,
516 typename elfcpp::Elf_types<size>::Elf_Addr addend,
517 typename elfcpp::Elf_types<size>::Elf_Addr address)
519 This::template pcrela<32>(view, 0, 0x03fffffc, object,
520 psymval, addend, address);
523 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
524 static inline void
525 rel14(unsigned char* view,
526 const Sized_relobj<size, big_endian>* object,
527 const Symbol_value<size>* psymval,
528 typename elfcpp::Elf_types<size>::Elf_Addr addend,
529 typename elfcpp::Elf_types<size>::Elf_Addr address)
531 This::template pcrela<32>(view, 0, 0x0000fffc, object,
532 psymval, addend, address);
535 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
536 static inline void
537 addr16(unsigned char* view,
538 typename elfcpp::Elf_types<size>::Elf_Addr value,
539 typename elfcpp::Elf_types<size>::Elf_Addr addend)
540 { This_reloc::rela16(view, value, addend); }
542 static inline void
543 addr16(unsigned char* view,
544 const Sized_relobj<size, big_endian>* object,
545 const Symbol_value<size>* psymval,
546 typename elfcpp::Elf_types<size>::Elf_Addr addend)
547 { This_reloc::rela16(view, object, psymval, addend); }
549 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
550 static inline void
551 addr16_ds(unsigned char* view,
552 typename elfcpp::Elf_types<size>::Elf_Addr value,
553 typename elfcpp::Elf_types<size>::Elf_Addr addend)
555 This::template rela<16>(view, 0, 0xfffc, value, addend);
558 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
559 static inline void
560 addr16_lo(unsigned char* view,
561 typename elfcpp::Elf_types<size>::Elf_Addr value,
562 typename elfcpp::Elf_types<size>::Elf_Addr addend)
563 { This_reloc::rela16(view, value, addend); }
565 static inline void
566 addr16_lo(unsigned char* view,
567 const Sized_relobj<size, big_endian>* object,
568 const Symbol_value<size>* psymval,
569 typename elfcpp::Elf_types<size>::Elf_Addr addend)
570 { This_reloc::rela16(view, object, psymval, addend); }
572 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
573 static inline void
574 addr16_hi(unsigned char* view,
575 typename elfcpp::Elf_types<size>::Elf_Addr value,
576 typename elfcpp::Elf_types<size>::Elf_Addr addend)
578 This::template rela<16>(view, 16, 0xffff, value, addend);
581 static inline void
582 addr16_hi(unsigned char* view,
583 const Sized_relobj<size, big_endian>* object,
584 const Symbol_value<size>* psymval,
585 typename elfcpp::Elf_types<size>::Elf_Addr addend)
587 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
590 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
591 // final value of the low 16 bits of the
592 // relocation is negative, add one.
593 static inline void
594 addr16_ha(unsigned char* view,
595 typename elfcpp::Elf_types<size>::Elf_Addr value,
596 typename elfcpp::Elf_types<size>::Elf_Addr addend)
598 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
600 reloc = value + addend;
602 if (reloc & 0x8000)
603 reloc += 0x10000;
604 reloc >>= 16;
606 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
609 static inline void
610 addr16_ha(unsigned char* view,
611 const Sized_relobj<size, big_endian>* object,
612 const Symbol_value<size>* psymval,
613 typename elfcpp::Elf_types<size>::Elf_Addr addend)
615 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
617 reloc = psymval->value(object, addend);
619 if (reloc & 0x8000)
620 reloc += 0x10000;
621 reloc >>= 16;
623 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
626 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
627 static inline void
628 rel16(unsigned char* view,
629 const Sized_relobj<size, big_endian>* object,
630 const Symbol_value<size>* psymval,
631 typename elfcpp::Elf_types<size>::Elf_Addr addend,
632 typename elfcpp::Elf_types<size>::Elf_Addr address)
633 { This_reloc::pcrela16(view, object, psymval, addend, address); }
635 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
636 static inline void
637 rel16_lo(unsigned char* view,
638 const Sized_relobj<size, big_endian>* object,
639 const Symbol_value<size>* psymval,
640 typename elfcpp::Elf_types<size>::Elf_Addr addend,
641 typename elfcpp::Elf_types<size>::Elf_Addr address)
642 { This_reloc::pcrela16(view, object, psymval, addend, address); }
644 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
645 static inline void
646 rel16_hi(unsigned char* view,
647 const Sized_relobj<size, big_endian>* object,
648 const Symbol_value<size>* psymval,
649 typename elfcpp::Elf_types<size>::Elf_Addr addend,
650 typename elfcpp::Elf_types<size>::Elf_Addr address)
652 This::template pcrela<16>(view, 16, 0xffff, object,
653 psymval, addend, address);
656 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
657 // final value of the low 16 bits of the
658 // relocation is negative, add one.
659 static inline void
660 rel16_ha(unsigned char* view,
661 const Sized_relobj<size, big_endian>* object,
662 const Symbol_value<size>* psymval,
663 typename elfcpp::Elf_types<size>::Elf_Addr addend,
664 typename elfcpp::Elf_types<size>::Elf_Addr address)
666 typedef typename elfcpp::Swap<16, true>::Valtype Valtype;
667 Valtype* wv = reinterpret_cast<Valtype*>(view);
668 Valtype val = elfcpp::Swap<16, true>::readval(wv);
669 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
671 reloc = (psymval->value(object, addend) - address);
672 if (reloc & 0x8000)
673 reloc += 0x10000;
674 reloc >>= 16;
676 val &= ~static_cast<Valtype>(0xffff);
677 reloc &= static_cast<Valtype>(0xffff);
679 elfcpp::Swap<16, true>::writeval(wv, val | reloc);
683 // Get the GOT section, creating it if necessary.
685 template<int size, bool big_endian>
686 Output_data_got<size, big_endian>*
687 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
688 Layout* layout)
690 if (this->got_ == NULL)
692 gold_assert(symtab != NULL && layout != NULL);
694 this->got_ = new Output_data_got<size, big_endian>();
696 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
697 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
698 this->got_);
700 // Create the GOT2 or TOC in the .got section.
701 if (size == 32)
703 this->got2_ = new Output_data_space(4, "** GOT2");
704 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
705 elfcpp::SHF_ALLOC
706 | elfcpp::SHF_WRITE,
707 this->got2_);
709 else
711 this->toc_ = new Output_data_space(8, "** TOC");
712 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
713 elfcpp::SHF_ALLOC
714 | elfcpp::SHF_WRITE,
715 this->toc_);
718 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
719 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
720 this->got_,
721 0, 0, elfcpp::STT_OBJECT,
722 elfcpp::STB_LOCAL,
723 elfcpp::STV_HIDDEN, 0,
724 false, false);
727 return this->got_;
730 // Get the dynamic reloc section, creating it if necessary.
732 template<int size, bool big_endian>
733 typename Target_powerpc<size, big_endian>::Reloc_section*
734 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
736 if (this->rela_dyn_ == NULL)
738 gold_assert(layout != NULL);
739 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
740 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
741 elfcpp::SHF_ALLOC, this->rela_dyn_);
743 return this->rela_dyn_;
746 // A class to handle the PLT data.
748 template<int size, bool big_endian>
749 class Output_data_plt_powerpc : public Output_section_data
751 public:
752 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
753 size, big_endian> Reloc_section;
755 Output_data_plt_powerpc(Layout*);
757 // Add an entry to the PLT.
758 void add_entry(Symbol* gsym);
760 // Return the .rela.plt section data.
761 const Reloc_section* rel_plt() const
763 return this->rel_;
766 protected:
767 void do_adjust_output_section(Output_section* os);
769 private:
770 // The size of an entry in the PLT.
771 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
773 // Set the final size.
774 void
775 set_final_data_size()
777 unsigned int full_count = this->count_ + 4;
779 this->set_data_size(full_count * base_plt_entry_size);
782 // Write out the PLT data.
783 void
784 do_write(Output_file*);
786 // The reloc section.
787 Reloc_section* rel_;
788 // The number of PLT entries.
789 unsigned int count_;
792 // Create the PLT section. The ordinary .got section is an argument,
793 // since we need to refer to the start.
795 template<int size, bool big_endian>
796 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
797 : Output_section_data(size == 32 ? 4 : 8), count_(0)
799 this->rel_ = new Reloc_section(false);
800 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
801 elfcpp::SHF_ALLOC, this->rel_);
804 template<int size, bool big_endian>
805 void
806 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
808 os->set_entsize(0);
811 // Add an entry to the PLT.
813 template<int size, bool big_endian>
814 void
815 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
817 gold_assert(!gsym->has_plt_offset());
818 unsigned int index = this->count_+ + 4;
819 section_offset_type plt_offset;
821 if (index < 8192)
822 plt_offset = index * base_plt_entry_size;
823 else
824 gold_unreachable();
826 gsym->set_plt_offset(plt_offset);
828 ++this->count_;
830 gsym->set_needs_dynsym_entry();
831 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
832 plt_offset, 0);
835 static const unsigned int addis_11_11 = 0x3d6b0000;
836 static const unsigned int addis_11_30 = 0x3d7e0000;
837 static const unsigned int addis_12_12 = 0x3d8c0000;
838 static const unsigned int addi_11_11 = 0x396b0000;
839 static const unsigned int add_0_11_11 = 0x7c0b5a14;
840 static const unsigned int add_11_0_11 = 0x7d605a14;
841 static const unsigned int b = 0x48000000;
842 static const unsigned int bcl_20_31 = 0x429f0005;
843 static const unsigned int bctr = 0x4e800420;
844 static const unsigned int lis_11 = 0x3d600000;
845 static const unsigned int lis_12 = 0x3d800000;
846 static const unsigned int lwzu_0_12 = 0x840c0000;
847 static const unsigned int lwz_0_12 = 0x800c0000;
848 static const unsigned int lwz_11_11 = 0x816b0000;
849 static const unsigned int lwz_11_30 = 0x817e0000;
850 static const unsigned int lwz_12_12 = 0x818c0000;
851 static const unsigned int mflr_0 = 0x7c0802a6;
852 static const unsigned int mflr_12 = 0x7d8802a6;
853 static const unsigned int mtctr_0 = 0x7c0903a6;
854 static const unsigned int mtctr_11 = 0x7d6903a6;
855 static const unsigned int mtlr_0 = 0x7c0803a6;
856 static const unsigned int nop = 0x60000000;
857 static const unsigned int sub_11_11_12 = 0x7d6c5850;
859 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
860 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
861 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
862 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
863 /* ld %r11,xxx+16@l(%r12) */
866 // Write out the PLT.
868 template<int size, bool big_endian>
869 void
870 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
872 const off_t offset = this->offset();
873 const section_size_type oview_size =
874 convert_to_section_size_type(this->data_size());
875 unsigned char* const oview = of->get_output_view(offset, oview_size);
876 unsigned char* pov = oview;
878 memset(pov, 0, base_plt_entry_size * 4);
879 pov += base_plt_entry_size * 4;
881 unsigned int plt_offset = base_plt_entry_size * 4;
882 const unsigned int count = this->count_;
884 if (size == 64)
886 for (unsigned int i = 0; i < count; i++)
890 else
892 for (unsigned int i = 0; i < count; i++)
894 elfcpp::Swap<32, true>::writeval(pov + 0x00,
895 lwz_11_30 + plt_offset);
896 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
897 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
898 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
899 pov += base_plt_entry_size;
900 plt_offset += base_plt_entry_size;
904 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
906 of->write_output_view(offset, oview_size, oview);
909 // Create a PLT entry for a global symbol.
911 template<int size, bool big_endian>
912 void
913 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
914 Layout* layout,
915 Symbol* gsym)
917 if (gsym->has_plt_offset())
918 return;
920 if (this->plt_ == NULL)
922 // Create the GOT section first.
923 this->got_section(symtab, layout);
925 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
926 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
927 (elfcpp::SHF_ALLOC
928 | elfcpp::SHF_EXECINSTR
929 | elfcpp::SHF_WRITE),
930 this->plt_);
932 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
933 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
934 this->plt_,
935 0, 0, elfcpp::STT_OBJECT,
936 elfcpp::STB_LOCAL,
937 elfcpp::STV_HIDDEN, 0,
938 false, false);
941 this->plt_->add_entry(gsym);
944 // Create a GOT entry for the TLS module index.
946 template<int size, bool big_endian>
947 unsigned int
948 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
949 Layout* layout,
950 Sized_relobj<size, big_endian>* object)
952 if (this->got_mod_index_offset_ == -1U)
954 gold_assert(symtab != NULL && layout != NULL && object != NULL);
955 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
956 Output_data_got<size, big_endian>* got;
957 unsigned int got_offset;
959 got = this->got_section(symtab, layout);
960 got_offset = got->add_constant(0);
961 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
962 got_offset, 0);
963 got->add_constant(0);
964 this->got_mod_index_offset_ = got_offset;
966 return this->got_mod_index_offset_;
969 // Optimize the TLS relocation type based on what we know about the
970 // symbol. IS_FINAL is true if the final address of this symbol is
971 // known at link time.
973 static tls::Tls_optimization
974 optimize_tls_reloc(bool /* is_final */, int r_type)
976 // If we are generating a shared library, then we can't do anything
977 // in the linker.
978 if (parameters->options().shared())
979 return tls::TLSOPT_NONE;
980 switch (r_type)
982 // XXX
983 default:
984 gold_unreachable();
988 // Report an unsupported relocation against a local symbol.
990 template<int size, bool big_endian>
991 void
992 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
993 Sized_relobj<size, big_endian>* object,
994 unsigned int r_type)
996 gold_error(_("%s: unsupported reloc %u against local symbol"),
997 object->name().c_str(), r_type);
1000 // We are about to emit a dynamic relocation of type R_TYPE. If the
1001 // dynamic linker does not support it, issue an error.
1003 template<int size, bool big_endian>
1004 void
1005 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1006 unsigned int r_type)
1008 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1010 // These are the relocation types supported by glibc for both 32-bit
1011 // and 64-bit powerpc.
1012 switch (r_type)
1014 case elfcpp::R_POWERPC_RELATIVE:
1015 case elfcpp::R_POWERPC_GLOB_DAT:
1016 case elfcpp::R_POWERPC_DTPMOD:
1017 case elfcpp::R_POWERPC_DTPREL:
1018 case elfcpp::R_POWERPC_TPREL:
1019 case elfcpp::R_POWERPC_JMP_SLOT:
1020 case elfcpp::R_POWERPC_COPY:
1021 case elfcpp::R_POWERPC_ADDR32:
1022 case elfcpp::R_POWERPC_ADDR24:
1023 case elfcpp::R_POWERPC_REL24:
1024 return;
1026 default:
1027 break;
1030 if (size == 64)
1032 switch (r_type)
1034 // These are the relocation types supported only on 64-bit.
1035 case elfcpp::R_PPC64_ADDR64:
1036 case elfcpp::R_PPC64_TPREL16_LO_DS:
1037 case elfcpp::R_PPC64_TPREL16_DS:
1038 case elfcpp::R_POWERPC_TPREL16:
1039 case elfcpp::R_POWERPC_TPREL16_LO:
1040 case elfcpp::R_POWERPC_TPREL16_HI:
1041 case elfcpp::R_POWERPC_TPREL16_HA:
1042 case elfcpp::R_PPC64_TPREL16_HIGHER:
1043 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1044 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1045 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1046 case elfcpp::R_PPC64_ADDR16_LO_DS:
1047 case elfcpp::R_POWERPC_ADDR16_LO:
1048 case elfcpp::R_POWERPC_ADDR16_HI:
1049 case elfcpp::R_POWERPC_ADDR16_HA:
1050 case elfcpp::R_POWERPC_ADDR30:
1051 case elfcpp::R_PPC64_UADDR64:
1052 case elfcpp::R_POWERPC_UADDR32:
1053 case elfcpp::R_POWERPC_ADDR16:
1054 case elfcpp::R_POWERPC_UADDR16:
1055 case elfcpp::R_PPC64_ADDR16_DS:
1056 case elfcpp::R_PPC64_ADDR16_HIGHER:
1057 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1058 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1059 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1060 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1061 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1062 case elfcpp::R_POWERPC_REL32:
1063 case elfcpp::R_PPC64_REL64:
1064 return;
1066 default:
1067 break;
1070 else
1072 switch (r_type)
1074 // These are the relocation types supported only on 32-bit.
1076 default:
1077 break;
1081 // This prevents us from issuing more than one error per reloc
1082 // section. But we can still wind up issuing more than one
1083 // error per object file.
1084 if (this->issued_non_pic_error_)
1085 return;
1086 object->error(_("requires unsupported dynamic reloc; "
1087 "recompile with -fPIC"));
1088 this->issued_non_pic_error_ = true;
1089 return;
1092 // Scan a relocation for a local symbol.
1094 template<int size, bool big_endian>
1095 inline void
1096 Target_powerpc<size, big_endian>::Scan::local(
1097 const General_options&,
1098 Symbol_table* symtab,
1099 Layout* layout,
1100 Target_powerpc<size, big_endian>* target,
1101 Sized_relobj<size, big_endian>* object,
1102 unsigned int data_shndx,
1103 Output_section* output_section,
1104 const elfcpp::Rela<size, big_endian>& reloc,
1105 unsigned int r_type,
1106 const elfcpp::Sym<size, big_endian>& lsym)
1108 switch (r_type)
1110 case elfcpp::R_POWERPC_NONE:
1111 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1112 case elfcpp::R_POWERPC_GNU_VTENTRY:
1113 break;
1115 case elfcpp::R_PPC64_ADDR64:
1116 case elfcpp::R_POWERPC_ADDR32:
1117 case elfcpp::R_POWERPC_ADDR16_HA:
1118 case elfcpp::R_POWERPC_ADDR16_LO:
1119 // If building a shared library (or a position-independent
1120 // executable), we need to create a dynamic relocation for
1121 // this location.
1122 if (parameters->options().output_is_position_independent())
1124 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1126 check_non_pic(object, r_type);
1127 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1129 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1130 rela_dyn->add_local(object, r_sym, r_type, output_section,
1131 data_shndx, reloc.get_r_offset(),
1132 reloc.get_r_addend());
1134 else
1136 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1137 gold_assert(lsym.get_st_value() == 0);
1138 rela_dyn->add_local_relative(object, r_sym, r_type,
1139 output_section, data_shndx,
1140 reloc.get_r_offset(),
1141 reloc.get_r_addend());
1144 break;
1146 case elfcpp::R_POWERPC_REL24:
1147 case elfcpp::R_PPC_LOCAL24PC:
1148 case elfcpp::R_POWERPC_REL32:
1149 case elfcpp::R_PPC_REL16_LO:
1150 case elfcpp::R_PPC_REL16_HA:
1151 break;
1153 case elfcpp::R_POWERPC_GOT16:
1154 case elfcpp::R_POWERPC_GOT16_LO:
1155 case elfcpp::R_POWERPC_GOT16_HI:
1156 case elfcpp::R_POWERPC_GOT16_HA:
1157 case elfcpp::R_PPC64_TOC16:
1158 case elfcpp::R_PPC64_TOC16_LO:
1159 case elfcpp::R_PPC64_TOC16_HI:
1160 case elfcpp::R_PPC64_TOC16_HA:
1161 case elfcpp::R_PPC64_TOC16_DS:
1162 case elfcpp::R_PPC64_TOC16_LO_DS:
1164 // The symbol requires a GOT entry.
1165 Output_data_got<size, big_endian>* got;
1166 unsigned int r_sym;
1168 got = target->got_section(symtab, layout);
1169 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1171 // If we are generating a shared object, we need to add a
1172 // dynamic relocation for this symbol's GOT entry.
1173 if (parameters->options().output_is_position_independent())
1175 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1177 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1178 unsigned int off;
1180 off = got->add_constant(0);
1181 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1182 rela_dyn->add_local_relative(object, r_sym,
1183 elfcpp::R_POWERPC_RELATIVE,
1184 got, off, 0);
1187 else
1188 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1190 break;
1192 case elfcpp::R_PPC64_TOC:
1193 // We need a GOT section.
1194 target->got_section(symtab, layout);
1195 break;
1197 // These are relocations which should only be seen by the
1198 // dynamic linker, and should never be seen here.
1199 case elfcpp::R_POWERPC_COPY:
1200 case elfcpp::R_POWERPC_GLOB_DAT:
1201 case elfcpp::R_POWERPC_JMP_SLOT:
1202 case elfcpp::R_POWERPC_RELATIVE:
1203 case elfcpp::R_POWERPC_DTPMOD:
1204 gold_error(_("%s: unexpected reloc %u in object file"),
1205 object->name().c_str(), r_type);
1206 break;
1208 default:
1209 unsupported_reloc_local(object, r_type);
1210 break;
1214 // Report an unsupported relocation against a global symbol.
1216 template<int size, bool big_endian>
1217 void
1218 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1219 Sized_relobj<size, big_endian>* object,
1220 unsigned int r_type,
1221 Symbol* gsym)
1223 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1224 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1227 // Scan a relocation for a global symbol.
1229 template<int size, bool big_endian>
1230 inline void
1231 Target_powerpc<size, big_endian>::Scan::global(
1232 const General_options&,
1233 Symbol_table* symtab,
1234 Layout* layout,
1235 Target_powerpc<size, big_endian>* target,
1236 Sized_relobj<size, big_endian>* object,
1237 unsigned int data_shndx,
1238 Output_section* output_section,
1239 const elfcpp::Rela<size, big_endian>& reloc,
1240 unsigned int r_type,
1241 Symbol* gsym)
1243 switch (r_type)
1245 case elfcpp::R_POWERPC_NONE:
1246 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1247 case elfcpp::R_POWERPC_GNU_VTENTRY:
1248 break;
1250 case elfcpp::R_PPC_PLTREL24:
1251 // If the symbol is fully resolved, this is just a PC32 reloc.
1252 // Otherwise we need a PLT entry.
1253 if (gsym->final_value_is_known())
1254 break;
1255 // If building a shared library, we can also skip the PLT entry
1256 // if the symbol is defined in the output file and is protected
1257 // or hidden.
1258 if (gsym->is_defined()
1259 && !gsym->is_from_dynobj()
1260 && !gsym->is_preemptible())
1261 break;
1262 target->make_plt_entry(symtab, layout, gsym);
1263 break;
1265 case elfcpp::R_POWERPC_ADDR16:
1266 case elfcpp::R_POWERPC_ADDR16_LO:
1267 case elfcpp::R_POWERPC_ADDR16_HI:
1268 case elfcpp::R_POWERPC_ADDR16_HA:
1269 case elfcpp::R_POWERPC_ADDR32:
1270 case elfcpp::R_PPC64_ADDR64:
1272 // Make a PLT entry if necessary.
1273 if (gsym->needs_plt_entry())
1275 target->make_plt_entry(symtab, layout, gsym);
1276 // Since this is not a PC-relative relocation, we may be
1277 // taking the address of a function. In that case we need to
1278 // set the entry in the dynamic symbol table to the address of
1279 // the PLT entry.
1280 if (gsym->is_from_dynobj() && !parameters->options().shared())
1281 gsym->set_needs_dynsym_value();
1283 // Make a dynamic relocation if necessary.
1284 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1286 if (target->may_need_copy_reloc(gsym))
1288 target->copy_reloc(symtab, layout, object,
1289 data_shndx, output_section, gsym, reloc);
1291 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1292 || r_type == elfcpp::R_PPC64_ADDR64)
1293 && gsym->can_use_relative_reloc(false))
1295 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1296 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1297 output_section, object,
1298 data_shndx, reloc.get_r_offset(),
1299 reloc.get_r_addend());
1301 else
1303 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1305 check_non_pic(object, r_type);
1306 if (gsym->is_from_dynobj()
1307 || gsym->is_undefined()
1308 || gsym->is_preemptible())
1309 rela_dyn->add_global(gsym, r_type, output_section,
1310 object, data_shndx,
1311 reloc.get_r_offset(),
1312 reloc.get_r_addend());
1313 else
1314 rela_dyn->add_global_relative(gsym, r_type,
1315 output_section, object,
1316 data_shndx,
1317 reloc.get_r_offset(),
1318 reloc.get_r_addend());
1322 break;
1324 case elfcpp::R_POWERPC_REL24:
1325 case elfcpp::R_PPC_LOCAL24PC:
1326 case elfcpp::R_PPC_REL16:
1327 case elfcpp::R_PPC_REL16_LO:
1328 case elfcpp::R_PPC_REL16_HI:
1329 case elfcpp::R_PPC_REL16_HA:
1331 if (gsym->needs_plt_entry())
1332 target->make_plt_entry(symtab, layout, gsym);
1333 // Make a dynamic relocation if necessary.
1334 int flags = Symbol::NON_PIC_REF;
1335 if (gsym->type() == elfcpp::STT_FUNC)
1336 flags |= Symbol::FUNCTION_CALL;
1337 if (gsym->needs_dynamic_reloc(flags))
1339 if (target->may_need_copy_reloc(gsym))
1341 target->copy_reloc(symtab, layout, object,
1342 data_shndx, output_section, gsym,
1343 reloc);
1345 else
1347 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1348 check_non_pic(object, r_type);
1349 rela_dyn->add_global(gsym, r_type, output_section, object,
1350 data_shndx, reloc.get_r_offset(),
1351 reloc.get_r_addend());
1355 break;
1357 case elfcpp::R_POWERPC_GOT16:
1358 case elfcpp::R_POWERPC_GOT16_LO:
1359 case elfcpp::R_POWERPC_GOT16_HI:
1360 case elfcpp::R_POWERPC_GOT16_HA:
1361 case elfcpp::R_PPC64_TOC16:
1362 case elfcpp::R_PPC64_TOC16_LO:
1363 case elfcpp::R_PPC64_TOC16_HI:
1364 case elfcpp::R_PPC64_TOC16_HA:
1365 case elfcpp::R_PPC64_TOC16_DS:
1366 case elfcpp::R_PPC64_TOC16_LO_DS:
1368 // The symbol requires a GOT entry.
1369 Output_data_got<size, big_endian>* got;
1371 got = target->got_section(symtab, layout);
1372 if (gsym->final_value_is_known())
1373 got->add_global(gsym, GOT_TYPE_STANDARD);
1374 else
1376 // If this symbol is not fully resolved, we need to add a
1377 // dynamic relocation for it.
1378 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1379 if (gsym->is_from_dynobj()
1380 || gsym->is_undefined()
1381 || gsym->is_preemptible())
1382 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1383 elfcpp::R_POWERPC_GLOB_DAT);
1384 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1386 unsigned int off = got->add_constant(0);
1388 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1389 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1390 got, off, 0);
1394 break;
1396 case elfcpp::R_PPC64_TOC:
1397 // We need a GOT section.
1398 target->got_section(symtab, layout);
1399 break;
1401 case elfcpp::R_POWERPC_GOT_TPREL16:
1402 case elfcpp::R_POWERPC_TLS:
1403 // XXX TLS
1404 break;
1406 // These are relocations which should only be seen by the
1407 // dynamic linker, and should never be seen here.
1408 case elfcpp::R_POWERPC_COPY:
1409 case elfcpp::R_POWERPC_GLOB_DAT:
1410 case elfcpp::R_POWERPC_JMP_SLOT:
1411 case elfcpp::R_POWERPC_RELATIVE:
1412 case elfcpp::R_POWERPC_DTPMOD:
1413 gold_error(_("%s: unexpected reloc %u in object file"),
1414 object->name().c_str(), r_type);
1415 break;
1417 default:
1418 unsupported_reloc_global(object, r_type, gsym);
1419 break;
1423 // Scan relocations for a section.
1425 template<int size, bool big_endian>
1426 void
1427 Target_powerpc<size, big_endian>::scan_relocs(
1428 const General_options& options,
1429 Symbol_table* symtab,
1430 Layout* layout,
1431 Sized_relobj<size, big_endian>* object,
1432 unsigned int data_shndx,
1433 unsigned int sh_type,
1434 const unsigned char* prelocs,
1435 size_t reloc_count,
1436 Output_section* output_section,
1437 bool needs_special_offset_handling,
1438 size_t local_symbol_count,
1439 const unsigned char* plocal_symbols)
1441 typedef Target_powerpc<size, big_endian> Powerpc;
1442 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1443 static Output_data_space* sdata;
1445 if (sh_type == elfcpp::SHT_REL)
1447 gold_error(_("%s: unsupported REL reloc section"),
1448 object->name().c_str());
1449 return;
1452 // Define _SDA_BASE_ at the start of the .sdata section.
1453 if (sdata == NULL)
1455 // layout->find_output_section(".sdata") == NULL
1456 sdata = new Output_data_space(4, "** sdata");
1457 Output_section* os = layout->add_output_section_data(".sdata", 0,
1458 elfcpp::SHF_ALLOC
1459 | elfcpp::SHF_WRITE,
1460 sdata);
1461 symtab->define_in_output_data("_SDA_BASE_", NULL,
1463 32768, 0,
1464 elfcpp::STT_OBJECT,
1465 elfcpp::STB_LOCAL,
1466 elfcpp::STV_HIDDEN, 0,
1467 false, false);
1470 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1471 options,
1472 symtab,
1473 layout,
1474 this,
1475 object,
1476 data_shndx,
1477 prelocs,
1478 reloc_count,
1479 output_section,
1480 needs_special_offset_handling,
1481 local_symbol_count,
1482 plocal_symbols);
1485 // Finalize the sections.
1487 template<int size, bool big_endian>
1488 void
1489 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1491 // Fill in some more dynamic tags.
1492 Output_data_dynamic* const odyn = layout->dynamic_data();
1493 if (odyn != NULL)
1495 if (this->plt_ != NULL)
1497 const Output_data* od = this->plt_->rel_plt();
1498 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1499 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1500 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1502 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1505 if (this->rela_dyn_ != NULL)
1507 const Output_data* od = this->rela_dyn_;
1508 odyn->add_section_address(elfcpp::DT_RELA, od);
1509 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1510 odyn->add_constant(elfcpp::DT_RELAENT,
1511 elfcpp::Elf_sizes<size>::rela_size);
1514 if (!parameters->options().shared())
1516 // The value of the DT_DEBUG tag is filled in by the dynamic
1517 // linker at run time, and used by the debugger.
1518 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1522 // Emit any relocs we saved in an attempt to avoid generating COPY
1523 // relocs.
1524 if (this->copy_relocs_.any_saved_relocs())
1525 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1528 // Perform a relocation.
1530 template<int size, bool big_endian>
1531 inline bool
1532 Target_powerpc<size, big_endian>::Relocate::relocate(
1533 const Relocate_info<size, big_endian>* relinfo,
1534 Target_powerpc* target,
1535 size_t relnum,
1536 const elfcpp::Rela<size, big_endian>& rela,
1537 unsigned int r_type,
1538 const Sized_symbol<size>* gsym,
1539 const Symbol_value<size>* psymval,
1540 unsigned char* view,
1541 typename elfcpp::Elf_types<size>::Elf_Addr address,
1542 section_size_type /* view_size */)
1544 const unsigned int toc_base_offset = 0x8000;
1545 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1547 // Pick the value to use for symbols defined in shared objects.
1548 Symbol_value<size> symval;
1549 if (gsym != NULL
1550 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1551 || r_type == elfcpp::R_PPC_LOCAL24PC
1552 || r_type == elfcpp::R_PPC_REL16
1553 || r_type == elfcpp::R_PPC_REL16_LO
1554 || r_type == elfcpp::R_PPC_REL16_HI
1555 || r_type == elfcpp::R_PPC_REL16_HA))
1557 elfcpp::Elf_Xword value;
1559 value = target->plt_section()->address() + gsym->plt_offset();
1561 symval.set_output_value(value);
1563 psymval = &symval;
1566 const Sized_relobj<size, big_endian>* object = relinfo->object;
1567 elfcpp::Elf_Xword addend = rela.get_r_addend();
1569 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1570 // pointer points to the beginning, not the end, of the table.
1571 // So we just use the plain offset.
1572 bool have_got_offset = false;
1573 unsigned int got_offset = 0;
1574 unsigned int got2_offset = 0;
1575 switch (r_type)
1577 case elfcpp::R_PPC64_TOC16:
1578 case elfcpp::R_PPC64_TOC16_LO:
1579 case elfcpp::R_PPC64_TOC16_HI:
1580 case elfcpp::R_PPC64_TOC16_HA:
1581 case elfcpp::R_PPC64_TOC16_DS:
1582 case elfcpp::R_PPC64_TOC16_LO_DS:
1583 // Subtract the TOC base address.
1584 addend -= target->toc_section()->address() + toc_base_offset;
1585 /* FALLTHRU */
1587 case elfcpp::R_POWERPC_GOT16:
1588 case elfcpp::R_POWERPC_GOT16_LO:
1589 case elfcpp::R_POWERPC_GOT16_HI:
1590 case elfcpp::R_POWERPC_GOT16_HA:
1591 case elfcpp::R_PPC64_GOT16_DS:
1592 case elfcpp::R_PPC64_GOT16_LO_DS:
1593 if (gsym != NULL)
1595 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1596 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1598 else
1600 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1601 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1602 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1604 have_got_offset = true;
1605 break;
1607 // R_PPC_PLTREL24 is rather special. If non-zero,
1608 // the addend specifies the GOT pointer offset within .got2.
1609 case elfcpp::R_PPC_PLTREL24:
1610 if (addend >= 32768)
1612 Output_data_space* got2;
1613 got2 = target->got2_section();
1614 got2_offset = got2->offset();
1615 addend += got2_offset;
1617 have_got_offset = true;
1618 break;
1620 default:
1621 break;
1624 switch (r_type)
1626 case elfcpp::R_POWERPC_NONE:
1627 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1628 case elfcpp::R_POWERPC_GNU_VTENTRY:
1629 break;
1631 case elfcpp::R_POWERPC_REL32:
1632 Reloc::rel32(view, object, psymval, addend, address);
1633 break;
1635 case elfcpp::R_POWERPC_REL24:
1636 Reloc::rel24(view, object, psymval, addend, address);
1637 break;
1639 case elfcpp::R_POWERPC_REL14:
1640 Reloc::rel14(view, object, psymval, addend, address);
1641 break;
1643 case elfcpp::R_PPC_PLTREL24:
1644 Reloc::rel24(view, object, psymval, addend, address);
1645 break;
1647 case elfcpp::R_PPC_LOCAL24PC:
1648 Reloc::rel24(view, object, psymval, addend, address);
1649 break;
1651 case elfcpp::R_PPC64_ADDR64:
1652 if (!parameters->options().output_is_position_independent())
1653 Relocate_functions<size, big_endian>::rela64(view, object,
1654 psymval, addend);
1655 break;
1657 case elfcpp::R_POWERPC_ADDR32:
1658 if (!parameters->options().output_is_position_independent())
1659 Relocate_functions<size, big_endian>::rela32(view, object,
1660 psymval, addend);
1661 break;
1663 case elfcpp::R_POWERPC_ADDR16_LO:
1664 Reloc::addr16_lo(view, object, psymval, addend);
1665 break;
1667 case elfcpp::R_POWERPC_ADDR16_HI:
1668 Reloc::addr16_hi(view, object, psymval, addend);
1669 break;
1671 case elfcpp::R_POWERPC_ADDR16_HA:
1672 Reloc::addr16_ha(view, object, psymval, addend);
1673 break;
1675 case elfcpp::R_PPC_REL16_LO:
1676 Reloc::rel16_lo(view, object, psymval, addend, address);
1677 break;
1679 case elfcpp::R_PPC_REL16_HI:
1680 Reloc::rel16_lo(view, object, psymval, addend, address);
1681 break;
1683 case elfcpp::R_PPC_REL16_HA:
1684 Reloc::rel16_lo(view, object, psymval, addend, address);
1685 break;
1687 case elfcpp::R_POWERPC_GOT16:
1688 Reloc::addr16(view, got_offset, addend);
1689 break;
1691 case elfcpp::R_POWERPC_GOT16_LO:
1692 Reloc::addr16_lo(view, got_offset, addend);
1693 break;
1695 case elfcpp::R_POWERPC_GOT16_HI:
1696 Reloc::addr16_hi(view, got_offset, addend);
1697 break;
1699 case elfcpp::R_POWERPC_GOT16_HA:
1700 Reloc::addr16_ha(view, got_offset, addend);
1701 break;
1703 case elfcpp::R_PPC64_TOC16:
1704 Reloc::addr16(view, got_offset, addend);
1705 break;
1707 case elfcpp::R_PPC64_TOC16_LO:
1708 Reloc::addr16_lo(view, got_offset, addend);
1709 break;
1711 case elfcpp::R_PPC64_TOC16_HI:
1712 Reloc::addr16_hi(view, got_offset, addend);
1713 break;
1715 case elfcpp::R_PPC64_TOC16_HA:
1716 Reloc::addr16_ha(view, got_offset, addend);
1717 break;
1719 case elfcpp::R_PPC64_TOC16_DS:
1720 case elfcpp::R_PPC64_TOC16_LO_DS:
1721 Reloc::addr16_ds(view, got_offset, addend);
1722 break;
1724 case elfcpp::R_PPC64_TOC:
1726 elfcpp::Elf_types<64>::Elf_Addr value;
1727 value = target->toc_section()->address() + toc_base_offset;
1728 Relocate_functions<64, false>::rela64(view, value, addend);
1730 break;
1732 case elfcpp::R_POWERPC_COPY:
1733 case elfcpp::R_POWERPC_GLOB_DAT:
1734 case elfcpp::R_POWERPC_JMP_SLOT:
1735 case elfcpp::R_POWERPC_RELATIVE:
1736 // This is an outstanding tls reloc, which is unexpected when
1737 // linking.
1738 case elfcpp::R_POWERPC_DTPMOD:
1739 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1740 _("unexpected reloc %u in object file"),
1741 r_type);
1742 break;
1744 default:
1745 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1746 _("unsupported reloc %u"),
1747 r_type);
1748 break;
1751 return true;
1754 // Perform a TLS relocation.
1756 template<int size, bool big_endian>
1757 inline void
1758 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1759 const Relocate_info<size, big_endian>* relinfo,
1760 Target_powerpc<size, big_endian>* target,
1761 size_t relnum,
1762 const elfcpp::Rela<size, big_endian>& rela,
1763 unsigned int r_type,
1764 const Sized_symbol<size>* gsym,
1765 const Symbol_value<size>* psymval,
1766 unsigned char* view,
1767 typename elfcpp::Elf_types<size>::Elf_Addr address,
1768 section_size_type)
1770 Output_segment* tls_segment = relinfo->layout->tls_segment();
1771 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1772 const Sized_relobj<size, big_endian>* object = relinfo->object;
1774 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1775 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1777 const bool is_final =
1778 (gsym == NULL
1779 ? !parameters->options().output_is_position_independent()
1780 : gsym->final_value_is_known());
1781 const tls::Tls_optimization optimized_type
1782 = optimize_tls_reloc(is_final, r_type);
1784 switch (r_type)
1786 // XXX
1790 // Relocate section data.
1792 template<int size, bool big_endian>
1793 void
1794 Target_powerpc<size, big_endian>::relocate_section(
1795 const Relocate_info<size, big_endian>* relinfo,
1796 unsigned int sh_type,
1797 const unsigned char* prelocs,
1798 size_t reloc_count,
1799 Output_section* output_section,
1800 bool needs_special_offset_handling,
1801 unsigned char* view,
1802 typename elfcpp::Elf_types<size>::Elf_Addr address,
1803 section_size_type view_size)
1805 typedef Target_powerpc<size, big_endian> Powerpc;
1806 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1808 gold_assert(sh_type == elfcpp::SHT_RELA);
1810 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1811 Powerpc_relocate>(
1812 relinfo,
1813 this,
1814 prelocs,
1815 reloc_count,
1816 output_section,
1817 needs_special_offset_handling,
1818 view,
1819 address,
1820 view_size);
1823 // Return the size of a relocation while scanning during a relocatable
1824 // link.
1826 template<int size, bool big_endian>
1827 unsigned int
1828 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1829 unsigned int,
1830 Relobj*)
1832 // We are always SHT_RELA, so we should never get here.
1833 gold_unreachable();
1834 return 0;
1837 // Scan the relocs during a relocatable link.
1839 template<int size, bool big_endian>
1840 void
1841 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1842 const General_options& options,
1843 Symbol_table* symtab,
1844 Layout* layout,
1845 Sized_relobj<size, big_endian>* object,
1846 unsigned int data_shndx,
1847 unsigned int sh_type,
1848 const unsigned char* prelocs,
1849 size_t reloc_count,
1850 Output_section* output_section,
1851 bool needs_special_offset_handling,
1852 size_t local_symbol_count,
1853 const unsigned char* plocal_symbols,
1854 Relocatable_relocs* rr)
1856 gold_assert(sh_type == elfcpp::SHT_RELA);
1858 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1859 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1861 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1862 Scan_relocatable_relocs>(
1863 options,
1864 symtab,
1865 layout,
1866 object,
1867 data_shndx,
1868 prelocs,
1869 reloc_count,
1870 output_section,
1871 needs_special_offset_handling,
1872 local_symbol_count,
1873 plocal_symbols,
1874 rr);
1877 // Relocate a section during a relocatable link.
1879 template<int size, bool big_endian>
1880 void
1881 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1882 const Relocate_info<size, big_endian>* relinfo,
1883 unsigned int sh_type,
1884 const unsigned char* prelocs,
1885 size_t reloc_count,
1886 Output_section* output_section,
1887 off_t offset_in_output_section,
1888 const Relocatable_relocs* rr,
1889 unsigned char* view,
1890 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1891 section_size_type view_size,
1892 unsigned char* reloc_view,
1893 section_size_type reloc_view_size)
1895 gold_assert(sh_type == elfcpp::SHT_RELA);
1897 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1898 relinfo,
1899 prelocs,
1900 reloc_count,
1901 output_section,
1902 offset_in_output_section,
1904 view,
1905 view_address,
1906 view_size,
1907 reloc_view,
1908 reloc_view_size);
1911 // Return the value to use for a dynamic which requires special
1912 // treatment. This is how we support equality comparisons of function
1913 // pointers across shared library boundaries, as described in the
1914 // processor specific ABI supplement.
1916 template<int size, bool big_endian>
1917 uint64_t
1918 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1920 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1921 return this->plt_section()->address() + gsym->plt_offset();
1924 // The selector for powerpc object files.
1926 template<int size, bool big_endian>
1927 class Target_selector_powerpc : public Target_selector
1929 public:
1930 Target_selector_powerpc()
1931 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1932 (size == 64 ?
1933 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1934 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1937 Target* instantiated_target_;
1939 Target* do_recognize(int machine, int, int)
1941 switch (size)
1943 case 64:
1944 if (machine != elfcpp::EM_PPC64)
1945 return NULL;
1946 break;
1948 case 32:
1949 if (machine != elfcpp::EM_PPC)
1950 return NULL;
1951 break;
1953 default:
1954 return NULL;
1957 return do_instantiate_target();
1960 Target* do_instantiate_target()
1962 if (this->instantiated_target_ == NULL)
1963 this->instantiated_target_ = new Target_powerpc<size, big_endian>();
1964 return this->instantiated_target_;
1968 Target_selector_powerpc<32, true> target_selector_ppc32;
1969 Target_selector_powerpc<32, false> target_selector_ppc32le;
1970 Target_selector_powerpc<64, true> target_selector_ppc64;
1971 Target_selector_powerpc<64, false> target_selector_ppc64le;
1973 } // End anonymous namespace.