daily update
[binutils.git] / gold / powerpc.cc
blob4e7406eb837d103c16570b6dc3bc8a04d6bf5f48
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(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, Relobj* object,
284 unsigned int shndx, Output_section* output_section,
285 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
287 this->copy_relocs_.copy_reloc(symtab, layout,
288 symtab->get_sized_symbol<size>(sym),
289 object, shndx, output_section,
290 reloc, this->rela_dyn_section(layout));
293 // Information about this specific target which we pass to the
294 // general Target structure.
295 static Target::Target_info powerpc_info;
297 // The types of GOT entries needed for this platform.
298 enum Got_type
300 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
301 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
302 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
305 // The GOT section.
306 Output_data_got<size, big_endian>* got_;
307 // The GOT2 section.
308 Output_data_space* got2_;
309 // The TOC section.
310 Output_data_space* toc_;
311 // The PLT section.
312 Output_data_plt_powerpc<size, big_endian>* plt_;
313 // The dynamic reloc section.
314 Reloc_section* rela_dyn_;
315 // Relocs saved to avoid a COPY reloc.
316 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
317 // Space for variables copied with a COPY reloc.
318 Output_data_space* dynbss_;
319 // Offset of the GOT entry for the TLS module index;
320 unsigned int got_mod_index_offset_;
323 template<>
324 Target::Target_info Target_powerpc<32, true>::powerpc_info =
326 32, // size
327 true, // is_big_endian
328 elfcpp::EM_PPC, // machine_code
329 false, // has_make_symbol
330 false, // has_resolve
331 false, // has_code_fill
332 true, // is_default_stack_executable
333 '\0', // wrap_char
334 "/usr/lib/ld.so.1", // dynamic_linker
335 0x10000000, // default_text_segment_address
336 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
337 4 * 1024 // common_pagesize (overridable by -z common-page-size)
340 template<>
341 Target::Target_info Target_powerpc<32, false>::powerpc_info =
343 32, // size
344 false, // is_big_endian
345 elfcpp::EM_PPC, // machine_code
346 false, // has_make_symbol
347 false, // has_resolve
348 false, // has_code_fill
349 true, // is_default_stack_executable
350 '\0', // wrap_char
351 "/usr/lib/ld.so.1", // dynamic_linker
352 0x10000000, // default_text_segment_address
353 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
354 4 * 1024 // common_pagesize (overridable by -z common-page-size)
357 template<>
358 Target::Target_info Target_powerpc<64, true>::powerpc_info =
360 64, // size
361 true, // is_big_endian
362 elfcpp::EM_PPC64, // machine_code
363 false, // has_make_symbol
364 false, // has_resolve
365 false, // has_code_fill
366 true, // is_default_stack_executable
367 '\0', // wrap_char
368 "/usr/lib/ld.so.1", // dynamic_linker
369 0x10000000, // default_text_segment_address
370 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
371 8 * 1024 // common_pagesize (overridable by -z common-page-size)
374 template<>
375 Target::Target_info Target_powerpc<64, false>::powerpc_info =
377 64, // size
378 false, // is_big_endian
379 elfcpp::EM_PPC64, // machine_code
380 false, // has_make_symbol
381 false, // has_resolve
382 false, // has_code_fill
383 true, // is_default_stack_executable
384 '\0', // wrap_char
385 "/usr/lib/ld.so.1", // dynamic_linker
386 0x10000000, // default_text_segment_address
387 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
388 8 * 1024 // common_pagesize (overridable by -z common-page-size)
391 template<int size, bool big_endian>
392 class Powerpc_relocate_functions
394 private:
395 // Do a simple relocation with the addend in the relocation.
396 template<int valsize>
397 static inline void
398 rela(unsigned char* view,
399 unsigned int right_shift,
400 elfcpp::Elf_Xword dst_mask,
401 typename elfcpp::Swap<size, big_endian>::Valtype value,
402 typename elfcpp::Swap<size, big_endian>::Valtype addend)
404 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
405 Valtype* wv = reinterpret_cast<Valtype*>(view);
406 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
407 Valtype reloc = ((value + addend) >> right_shift);
409 val &= ~dst_mask;
410 reloc &= dst_mask;
412 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
415 // Do a simple relocation using a symbol value with the addend in
416 // the relocation.
417 template<int valsize>
418 static inline void
419 rela(unsigned char* view,
420 unsigned int right_shift,
421 elfcpp::Elf_Xword dst_mask,
422 const Sized_relobj<size, big_endian>* object,
423 const Symbol_value<size>* psymval,
424 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
426 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
427 Valtype* wv = reinterpret_cast<Valtype*>(view);
428 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
429 Valtype reloc = (psymval->value(object, addend) >> right_shift);
431 val &= ~dst_mask;
432 reloc &= dst_mask;
434 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
437 // Do a simple relocation using a symbol value with the addend in
438 // the relocation, unaligned.
439 template<int valsize>
440 static inline void
441 rela_ua(unsigned char* view, unsigned int right_shift,
442 elfcpp::Elf_Xword dst_mask,
443 const Sized_relobj<size, big_endian>* object,
444 const Symbol_value<size>* psymval,
445 typename elfcpp::Swap<size, big_endian>::Valtype addend)
447 typedef typename elfcpp::Swap_unaligned<valsize,
448 big_endian>::Valtype Valtype;
449 unsigned char* wv = view;
450 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
451 Valtype reloc = (psymval->value(object, addend) >> right_shift);
453 val &= ~dst_mask;
454 reloc &= dst_mask;
456 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
459 // Do a simple PC relative relocation with a Symbol_value with the
460 // addend in the relocation.
461 template<int valsize>
462 static inline void
463 pcrela(unsigned char* view, unsigned int right_shift,
464 elfcpp::Elf_Xword dst_mask,
465 const Sized_relobj<size, big_endian>* object,
466 const Symbol_value<size>* psymval,
467 typename elfcpp::Swap<size, big_endian>::Valtype addend,
468 typename elfcpp::Elf_types<size>::Elf_Addr address)
470 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
471 Valtype* wv = reinterpret_cast<Valtype*>(view);
472 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
473 Valtype reloc = ((psymval->value(object, addend) - address)
474 >> right_shift);
476 val &= ~dst_mask;
477 reloc &= dst_mask;
479 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
482 template<int valsize>
483 static inline void
484 pcrela_unaligned(unsigned char* view,
485 const Sized_relobj<size, big_endian>* object,
486 const Symbol_value<size>* psymval,
487 typename elfcpp::Swap<size, big_endian>::Valtype addend,
488 typename elfcpp::Elf_types<size>::Elf_Addr address)
490 typedef typename elfcpp::Swap_unaligned<valsize,
491 big_endian>::Valtype Valtype;
492 unsigned char* wv = view;
493 Valtype reloc = (psymval->value(object, addend) - address);
495 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
498 typedef Powerpc_relocate_functions<size, big_endian> This;
499 typedef Relocate_functions<size, big_endian> This_reloc;
500 public:
501 // R_POWERPC_REL32: (Symbol + Addend - Address)
502 static inline void
503 rel32(unsigned char* view,
504 const Sized_relobj<size, big_endian>* object,
505 const Symbol_value<size>* psymval,
506 typename elfcpp::Elf_types<size>::Elf_Addr addend,
507 typename elfcpp::Elf_types<size>::Elf_Addr address)
508 { This_reloc::pcrela32(view, object, psymval, addend, address); }
510 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
511 static inline void
512 rel24(unsigned char* view,
513 const Sized_relobj<size, big_endian>* object,
514 const Symbol_value<size>* psymval,
515 typename elfcpp::Elf_types<size>::Elf_Addr addend,
516 typename elfcpp::Elf_types<size>::Elf_Addr address)
518 This::template pcrela<32>(view, 0, 0x03fffffc, object,
519 psymval, addend, address);
522 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
523 static inline void
524 rel14(unsigned char* view,
525 const Sized_relobj<size, big_endian>* object,
526 const Symbol_value<size>* psymval,
527 typename elfcpp::Elf_types<size>::Elf_Addr addend,
528 typename elfcpp::Elf_types<size>::Elf_Addr address)
530 This::template pcrela<32>(view, 0, 0x0000fffc, object,
531 psymval, addend, address);
534 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
535 static inline void
536 addr16(unsigned char* view,
537 typename elfcpp::Elf_types<size>::Elf_Addr value,
538 typename elfcpp::Elf_types<size>::Elf_Addr addend)
539 { This_reloc::rela16(view, value, addend); }
541 static inline void
542 addr16(unsigned char* view,
543 const Sized_relobj<size, big_endian>* object,
544 const Symbol_value<size>* psymval,
545 typename elfcpp::Elf_types<size>::Elf_Addr addend)
546 { This_reloc::rela16(view, object, psymval, addend); }
548 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
549 static inline void
550 addr16_ds(unsigned char* view,
551 typename elfcpp::Elf_types<size>::Elf_Addr value,
552 typename elfcpp::Elf_types<size>::Elf_Addr addend)
554 This::template rela<16>(view, 0, 0xfffc, value, addend);
557 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
558 static inline void
559 addr16_lo(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_lo(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_HI: ((Symbol + Addend) >> 16) & 0xffff
572 static inline void
573 addr16_hi(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, 16, 0xffff, value, addend);
580 static inline void
581 addr16_hi(unsigned char* view,
582 const Sized_relobj<size, big_endian>* object,
583 const Symbol_value<size>* psymval,
584 typename elfcpp::Elf_types<size>::Elf_Addr addend)
586 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
589 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
590 // final value of the low 16 bits of the
591 // relocation is negative, add one.
592 static inline void
593 addr16_ha(unsigned char* view,
594 typename elfcpp::Elf_types<size>::Elf_Addr value,
595 typename elfcpp::Elf_types<size>::Elf_Addr addend)
597 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
599 reloc = value + addend;
601 if (reloc & 0x8000)
602 reloc += 0x10000;
603 reloc >>= 16;
605 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
608 static inline void
609 addr16_ha(unsigned char* view,
610 const Sized_relobj<size, big_endian>* object,
611 const Symbol_value<size>* psymval,
612 typename elfcpp::Elf_types<size>::Elf_Addr addend)
614 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
616 reloc = psymval->value(object, addend);
618 if (reloc & 0x8000)
619 reloc += 0x10000;
620 reloc >>= 16;
622 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
625 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
626 static inline void
627 rel16(unsigned char* view,
628 const Sized_relobj<size, big_endian>* object,
629 const Symbol_value<size>* psymval,
630 typename elfcpp::Elf_types<size>::Elf_Addr addend,
631 typename elfcpp::Elf_types<size>::Elf_Addr address)
632 { This_reloc::pcrela16(view, object, psymval, addend, address); }
634 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
635 static inline void
636 rel16_lo(unsigned char* view,
637 const Sized_relobj<size, big_endian>* object,
638 const Symbol_value<size>* psymval,
639 typename elfcpp::Elf_types<size>::Elf_Addr addend,
640 typename elfcpp::Elf_types<size>::Elf_Addr address)
641 { This_reloc::pcrela16(view, object, psymval, addend, address); }
643 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
644 static inline void
645 rel16_hi(unsigned char* view,
646 const Sized_relobj<size, big_endian>* object,
647 const Symbol_value<size>* psymval,
648 typename elfcpp::Elf_types<size>::Elf_Addr addend,
649 typename elfcpp::Elf_types<size>::Elf_Addr address)
651 This::template pcrela<16>(view, 16, 0xffff, object,
652 psymval, addend, address);
655 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
656 // final value of the low 16 bits of the
657 // relocation is negative, add one.
658 static inline void
659 rel16_ha(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)
665 typedef typename elfcpp::Swap<16, true>::Valtype Valtype;
666 Valtype* wv = reinterpret_cast<Valtype*>(view);
667 Valtype val = elfcpp::Swap<16, true>::readval(wv);
668 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
670 reloc = (psymval->value(object, addend) - address);
671 if (reloc & 0x8000)
672 reloc += 0x10000;
673 reloc >>= 16;
675 val &= ~static_cast<Valtype>(0xffff);
676 reloc &= static_cast<Valtype>(0xffff);
678 elfcpp::Swap<16, true>::writeval(wv, val | reloc);
682 // Get the GOT section, creating it if necessary.
684 template<int size, bool big_endian>
685 Output_data_got<size, big_endian>*
686 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
687 Layout* layout)
689 if (this->got_ == NULL)
691 gold_assert(symtab != NULL && layout != NULL);
693 this->got_ = new Output_data_got<size, big_endian>();
695 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
696 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
697 this->got_);
699 // Create the GOT2 or TOC in the .got section.
700 if (size == 32)
702 this->got2_ = new Output_data_space(4, "** GOT2");
703 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
704 elfcpp::SHF_ALLOC
705 | elfcpp::SHF_WRITE,
706 this->got2_);
708 else
710 this->toc_ = new Output_data_space(8, "** TOC");
711 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
712 elfcpp::SHF_ALLOC
713 | elfcpp::SHF_WRITE,
714 this->toc_);
717 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
718 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
719 this->got_,
720 0, 0, elfcpp::STT_OBJECT,
721 elfcpp::STB_LOCAL,
722 elfcpp::STV_HIDDEN, 0,
723 false, false);
726 return this->got_;
729 // Get the dynamic reloc section, creating it if necessary.
731 template<int size, bool big_endian>
732 typename Target_powerpc<size, big_endian>::Reloc_section*
733 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
735 if (this->rela_dyn_ == NULL)
737 gold_assert(layout != NULL);
738 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
739 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
740 elfcpp::SHF_ALLOC, this->rela_dyn_);
742 return this->rela_dyn_;
745 // A class to handle the PLT data.
747 template<int size, bool big_endian>
748 class Output_data_plt_powerpc : public Output_section_data
750 public:
751 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
752 size, big_endian> Reloc_section;
754 Output_data_plt_powerpc(Layout*);
756 // Add an entry to the PLT.
757 void add_entry(Symbol* gsym);
759 // Return the .rela.plt section data.
760 const Reloc_section* rel_plt() const
762 return this->rel_;
765 protected:
766 void do_adjust_output_section(Output_section* os);
768 private:
769 // The size of an entry in the PLT.
770 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
772 // Set the final size.
773 void
774 set_final_data_size()
776 unsigned int full_count = this->count_ + 4;
778 this->set_data_size(full_count * base_plt_entry_size);
781 // Write out the PLT data.
782 void
783 do_write(Output_file*);
785 // The reloc section.
786 Reloc_section* rel_;
787 // The number of PLT entries.
788 unsigned int count_;
791 // Create the PLT section. The ordinary .got section is an argument,
792 // since we need to refer to the start.
794 template<int size, bool big_endian>
795 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
796 : Output_section_data(size == 32 ? 4 : 8), count_(0)
798 this->rel_ = new Reloc_section(false);
799 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
800 elfcpp::SHF_ALLOC, this->rel_);
803 template<int size, bool big_endian>
804 void
805 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
807 os->set_entsize(0);
810 // Add an entry to the PLT.
812 template<int size, bool big_endian>
813 void
814 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
816 gold_assert(!gsym->has_plt_offset());
817 unsigned int index = this->count_+ + 4;
818 section_offset_type plt_offset;
820 if (index < 8192)
821 plt_offset = index * base_plt_entry_size;
822 else
823 gold_unreachable();
825 gsym->set_plt_offset(plt_offset);
827 ++this->count_;
829 gsym->set_needs_dynsym_entry();
830 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
831 plt_offset, 0);
834 static const unsigned int addis_11_11 = 0x3d6b0000;
835 static const unsigned int addis_11_30 = 0x3d7e0000;
836 static const unsigned int addis_12_12 = 0x3d8c0000;
837 static const unsigned int addi_11_11 = 0x396b0000;
838 static const unsigned int add_0_11_11 = 0x7c0b5a14;
839 static const unsigned int add_11_0_11 = 0x7d605a14;
840 static const unsigned int b = 0x48000000;
841 static const unsigned int bcl_20_31 = 0x429f0005;
842 static const unsigned int bctr = 0x4e800420;
843 static const unsigned int lis_11 = 0x3d600000;
844 static const unsigned int lis_12 = 0x3d800000;
845 static const unsigned int lwzu_0_12 = 0x840c0000;
846 static const unsigned int lwz_0_12 = 0x800c0000;
847 static const unsigned int lwz_11_11 = 0x816b0000;
848 static const unsigned int lwz_11_30 = 0x817e0000;
849 static const unsigned int lwz_12_12 = 0x818c0000;
850 static const unsigned int mflr_0 = 0x7c0802a6;
851 static const unsigned int mflr_12 = 0x7d8802a6;
852 static const unsigned int mtctr_0 = 0x7c0903a6;
853 static const unsigned int mtctr_11 = 0x7d6903a6;
854 static const unsigned int mtlr_0 = 0x7c0803a6;
855 static const unsigned int nop = 0x60000000;
856 static const unsigned int sub_11_11_12 = 0x7d6c5850;
858 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
859 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
860 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
861 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
862 /* ld %r11,xxx+16@l(%r12) */
865 // Write out the PLT.
867 template<int size, bool big_endian>
868 void
869 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
871 const off_t offset = this->offset();
872 const section_size_type oview_size =
873 convert_to_section_size_type(this->data_size());
874 unsigned char* const oview = of->get_output_view(offset, oview_size);
875 unsigned char* pov = oview;
877 memset(pov, 0, base_plt_entry_size * 4);
878 pov += base_plt_entry_size * 4;
880 unsigned int plt_offset = base_plt_entry_size * 4;
881 const unsigned int count = this->count_;
883 if (size == 64)
885 for (unsigned int i = 0; i < count; i++)
889 else
891 for (unsigned int i = 0; i < count; i++)
893 elfcpp::Swap<32, true>::writeval(pov + 0x00,
894 lwz_11_30 + plt_offset);
895 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
896 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
897 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
898 pov += base_plt_entry_size;
899 plt_offset += base_plt_entry_size;
903 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
905 of->write_output_view(offset, oview_size, oview);
908 // Create a PLT entry for a global symbol.
910 template<int size, bool big_endian>
911 void
912 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
913 Layout* layout,
914 Symbol* gsym)
916 if (gsym->has_plt_offset())
917 return;
919 if (this->plt_ == NULL)
921 // Create the GOT section first.
922 this->got_section(symtab, layout);
924 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
925 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
926 (elfcpp::SHF_ALLOC
927 | elfcpp::SHF_EXECINSTR
928 | elfcpp::SHF_WRITE),
929 this->plt_);
931 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
932 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
933 this->plt_,
934 0, 0, elfcpp::STT_OBJECT,
935 elfcpp::STB_LOCAL,
936 elfcpp::STV_HIDDEN, 0,
937 false, false);
940 this->plt_->add_entry(gsym);
943 // Create a GOT entry for the TLS module index.
945 template<int size, bool big_endian>
946 unsigned int
947 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
948 Layout* layout,
949 Sized_relobj<size, big_endian>* object)
951 if (this->got_mod_index_offset_ == -1U)
953 gold_assert(symtab != NULL && layout != NULL && object != NULL);
954 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
955 Output_data_got<size, big_endian>* got;
956 unsigned int got_offset;
958 got = this->got_section(symtab, layout);
959 got_offset = got->add_constant(0);
960 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
961 got_offset, 0);
962 got->add_constant(0);
963 this->got_mod_index_offset_ = got_offset;
965 return this->got_mod_index_offset_;
968 // Optimize the TLS relocation type based on what we know about the
969 // symbol. IS_FINAL is true if the final address of this symbol is
970 // known at link time.
972 static tls::Tls_optimization
973 optimize_tls_reloc(bool /* is_final */, int r_type)
975 // If we are generating a shared library, then we can't do anything
976 // in the linker.
977 if (parameters->options().shared())
978 return tls::TLSOPT_NONE;
979 switch (r_type)
981 // XXX
982 default:
983 gold_unreachable();
987 // Report an unsupported relocation against a local symbol.
989 template<int size, bool big_endian>
990 void
991 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
992 Sized_relobj<size, big_endian>* object,
993 unsigned int r_type)
995 gold_error(_("%s: unsupported reloc %u against local symbol"),
996 object->name().c_str(), r_type);
999 // We are about to emit a dynamic relocation of type R_TYPE. If the
1000 // dynamic linker does not support it, issue an error.
1002 template<int size, bool big_endian>
1003 void
1004 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1005 unsigned int r_type)
1007 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1009 // These are the relocation types supported by glibc for both 32-bit
1010 // and 64-bit powerpc.
1011 switch (r_type)
1013 case elfcpp::R_POWERPC_RELATIVE:
1014 case elfcpp::R_POWERPC_GLOB_DAT:
1015 case elfcpp::R_POWERPC_DTPMOD:
1016 case elfcpp::R_POWERPC_DTPREL:
1017 case elfcpp::R_POWERPC_TPREL:
1018 case elfcpp::R_POWERPC_JMP_SLOT:
1019 case elfcpp::R_POWERPC_COPY:
1020 case elfcpp::R_POWERPC_ADDR32:
1021 case elfcpp::R_POWERPC_ADDR24:
1022 case elfcpp::R_POWERPC_REL24:
1023 return;
1025 default:
1026 break;
1029 if (size == 64)
1031 switch (r_type)
1033 // These are the relocation types supported only on 64-bit.
1034 case elfcpp::R_PPC64_ADDR64:
1035 case elfcpp::R_PPC64_TPREL16_LO_DS:
1036 case elfcpp::R_PPC64_TPREL16_DS:
1037 case elfcpp::R_POWERPC_TPREL16:
1038 case elfcpp::R_POWERPC_TPREL16_LO:
1039 case elfcpp::R_POWERPC_TPREL16_HI:
1040 case elfcpp::R_POWERPC_TPREL16_HA:
1041 case elfcpp::R_PPC64_TPREL16_HIGHER:
1042 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1043 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1044 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1045 case elfcpp::R_PPC64_ADDR16_LO_DS:
1046 case elfcpp::R_POWERPC_ADDR16_LO:
1047 case elfcpp::R_POWERPC_ADDR16_HI:
1048 case elfcpp::R_POWERPC_ADDR16_HA:
1049 case elfcpp::R_POWERPC_ADDR30:
1050 case elfcpp::R_PPC64_UADDR64:
1051 case elfcpp::R_POWERPC_UADDR32:
1052 case elfcpp::R_POWERPC_ADDR16:
1053 case elfcpp::R_POWERPC_UADDR16:
1054 case elfcpp::R_PPC64_ADDR16_DS:
1055 case elfcpp::R_PPC64_ADDR16_HIGHER:
1056 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1057 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1058 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1059 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1060 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1061 case elfcpp::R_POWERPC_REL32:
1062 case elfcpp::R_PPC64_REL64:
1063 return;
1065 default:
1066 break;
1069 else
1071 switch (r_type)
1073 // These are the relocation types supported only on 32-bit.
1075 default:
1076 break;
1080 // This prevents us from issuing more than one error per reloc
1081 // section. But we can still wind up issuing more than one
1082 // error per object file.
1083 if (this->issued_non_pic_error_)
1084 return;
1085 object->error(_("requires unsupported dynamic reloc; "
1086 "recompile with -fPIC"));
1087 this->issued_non_pic_error_ = true;
1088 return;
1091 // Scan a relocation for a local symbol.
1093 template<int size, bool big_endian>
1094 inline void
1095 Target_powerpc<size, big_endian>::Scan::local(
1096 const General_options&,
1097 Symbol_table* symtab,
1098 Layout* layout,
1099 Target_powerpc<size, big_endian>* target,
1100 Sized_relobj<size, big_endian>* object,
1101 unsigned int data_shndx,
1102 Output_section* output_section,
1103 const elfcpp::Rela<size, big_endian>& reloc,
1104 unsigned int r_type,
1105 const elfcpp::Sym<size, big_endian>& lsym)
1107 switch (r_type)
1109 case elfcpp::R_POWERPC_NONE:
1110 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1111 case elfcpp::R_POWERPC_GNU_VTENTRY:
1112 break;
1114 case elfcpp::R_PPC64_ADDR64:
1115 case elfcpp::R_POWERPC_ADDR32:
1116 case elfcpp::R_POWERPC_ADDR16_HA:
1117 case elfcpp::R_POWERPC_ADDR16_LO:
1118 // If building a shared library (or a position-independent
1119 // executable), we need to create a dynamic relocation for
1120 // this location.
1121 if (parameters->options().output_is_position_independent())
1123 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1125 check_non_pic(object, r_type);
1126 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1128 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1129 rela_dyn->add_local(object, r_sym, r_type, output_section,
1130 data_shndx, reloc.get_r_offset(),
1131 reloc.get_r_addend());
1133 else
1135 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1136 gold_assert(lsym.get_st_value() == 0);
1137 rela_dyn->add_local_relative(object, r_sym, r_type,
1138 output_section, data_shndx,
1139 reloc.get_r_offset(),
1140 reloc.get_r_addend());
1143 break;
1145 case elfcpp::R_POWERPC_REL24:
1146 case elfcpp::R_PPC_LOCAL24PC:
1147 case elfcpp::R_POWERPC_REL32:
1148 case elfcpp::R_PPC_REL16_LO:
1149 case elfcpp::R_PPC_REL16_HA:
1150 break;
1152 case elfcpp::R_POWERPC_GOT16:
1153 case elfcpp::R_POWERPC_GOT16_LO:
1154 case elfcpp::R_POWERPC_GOT16_HI:
1155 case elfcpp::R_POWERPC_GOT16_HA:
1156 case elfcpp::R_PPC64_TOC16:
1157 case elfcpp::R_PPC64_TOC16_LO:
1158 case elfcpp::R_PPC64_TOC16_HI:
1159 case elfcpp::R_PPC64_TOC16_HA:
1160 case elfcpp::R_PPC64_TOC16_DS:
1161 case elfcpp::R_PPC64_TOC16_LO_DS:
1163 // The symbol requires a GOT entry.
1164 Output_data_got<size, big_endian>* got;
1165 unsigned int r_sym;
1167 got = target->got_section(symtab, layout);
1168 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1170 // If we are generating a shared object, we need to add a
1171 // dynamic relocation for this symbol's GOT entry.
1172 if (parameters->options().output_is_position_independent())
1174 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1176 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1177 unsigned int off;
1179 off = got->add_constant(0);
1180 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1181 rela_dyn->add_local_relative(object, r_sym,
1182 elfcpp::R_POWERPC_RELATIVE,
1183 got, off, 0);
1186 else
1187 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1189 break;
1191 case elfcpp::R_PPC64_TOC:
1192 // We need a GOT section.
1193 target->got_section(symtab, layout);
1194 break;
1196 // These are relocations which should only be seen by the
1197 // dynamic linker, and should never be seen here.
1198 case elfcpp::R_POWERPC_COPY:
1199 case elfcpp::R_POWERPC_GLOB_DAT:
1200 case elfcpp::R_POWERPC_JMP_SLOT:
1201 case elfcpp::R_POWERPC_RELATIVE:
1202 case elfcpp::R_POWERPC_DTPMOD:
1203 gold_error(_("%s: unexpected reloc %u in object file"),
1204 object->name().c_str(), r_type);
1205 break;
1207 default:
1208 unsupported_reloc_local(object, r_type);
1209 break;
1213 // Report an unsupported relocation against a global symbol.
1215 template<int size, bool big_endian>
1216 void
1217 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1218 Sized_relobj<size, big_endian>* object,
1219 unsigned int r_type,
1220 Symbol* gsym)
1222 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1223 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1226 // Scan a relocation for a global symbol.
1228 template<int size, bool big_endian>
1229 inline void
1230 Target_powerpc<size, big_endian>::Scan::global(
1231 const General_options&,
1232 Symbol_table* symtab,
1233 Layout* layout,
1234 Target_powerpc<size, big_endian>* target,
1235 Sized_relobj<size, big_endian>* object,
1236 unsigned int data_shndx,
1237 Output_section* output_section,
1238 const elfcpp::Rela<size, big_endian>& reloc,
1239 unsigned int r_type,
1240 Symbol* gsym)
1242 switch (r_type)
1244 case elfcpp::R_POWERPC_NONE:
1245 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1246 case elfcpp::R_POWERPC_GNU_VTENTRY:
1247 break;
1249 case elfcpp::R_PPC_PLTREL24:
1250 // If the symbol is fully resolved, this is just a PC32 reloc.
1251 // Otherwise we need a PLT entry.
1252 if (gsym->final_value_is_known())
1253 break;
1254 // If building a shared library, we can also skip the PLT entry
1255 // if the symbol is defined in the output file and is protected
1256 // or hidden.
1257 if (gsym->is_defined()
1258 && !gsym->is_from_dynobj()
1259 && !gsym->is_preemptible())
1260 break;
1261 target->make_plt_entry(symtab, layout, gsym);
1262 break;
1264 case elfcpp::R_POWERPC_ADDR16:
1265 case elfcpp::R_POWERPC_ADDR16_LO:
1266 case elfcpp::R_POWERPC_ADDR16_HI:
1267 case elfcpp::R_POWERPC_ADDR16_HA:
1268 case elfcpp::R_POWERPC_ADDR32:
1269 case elfcpp::R_PPC64_ADDR64:
1271 // Make a PLT entry if necessary.
1272 if (gsym->needs_plt_entry())
1274 target->make_plt_entry(symtab, layout, gsym);
1275 // Since this is not a PC-relative relocation, we may be
1276 // taking the address of a function. In that case we need to
1277 // set the entry in the dynamic symbol table to the address of
1278 // the PLT entry.
1279 if (gsym->is_from_dynobj() && !parameters->options().shared())
1280 gsym->set_needs_dynsym_value();
1282 // Make a dynamic relocation if necessary.
1283 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1285 if (target->may_need_copy_reloc(gsym))
1287 target->copy_reloc(symtab, layout, object,
1288 data_shndx, output_section, gsym, reloc);
1290 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1291 || r_type == elfcpp::R_PPC64_ADDR64)
1292 && gsym->can_use_relative_reloc(false))
1294 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1295 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1296 output_section, object,
1297 data_shndx, reloc.get_r_offset(),
1298 reloc.get_r_addend());
1300 else
1302 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1304 check_non_pic(object, r_type);
1305 if (gsym->is_from_dynobj()
1306 || gsym->is_undefined()
1307 || gsym->is_preemptible())
1308 rela_dyn->add_global(gsym, r_type, output_section,
1309 object, data_shndx,
1310 reloc.get_r_offset(),
1311 reloc.get_r_addend());
1312 else
1313 rela_dyn->add_global_relative(gsym, r_type,
1314 output_section, object,
1315 data_shndx,
1316 reloc.get_r_offset(),
1317 reloc.get_r_addend());
1321 break;
1323 case elfcpp::R_POWERPC_REL24:
1324 case elfcpp::R_PPC_LOCAL24PC:
1325 case elfcpp::R_PPC_REL16:
1326 case elfcpp::R_PPC_REL16_LO:
1327 case elfcpp::R_PPC_REL16_HI:
1328 case elfcpp::R_PPC_REL16_HA:
1330 if (gsym->needs_plt_entry())
1331 target->make_plt_entry(symtab, layout, gsym);
1332 // Make a dynamic relocation if necessary.
1333 int flags = Symbol::NON_PIC_REF;
1334 if (gsym->type() == elfcpp::STT_FUNC)
1335 flags |= Symbol::FUNCTION_CALL;
1336 if (gsym->needs_dynamic_reloc(flags))
1338 if (target->may_need_copy_reloc(gsym))
1340 target->copy_reloc(symtab, layout, object,
1341 data_shndx, output_section, gsym,
1342 reloc);
1344 else
1346 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1347 check_non_pic(object, r_type);
1348 rela_dyn->add_global(gsym, r_type, output_section, object,
1349 data_shndx, reloc.get_r_offset(),
1350 reloc.get_r_addend());
1354 break;
1356 case elfcpp::R_POWERPC_GOT16:
1357 case elfcpp::R_POWERPC_GOT16_LO:
1358 case elfcpp::R_POWERPC_GOT16_HI:
1359 case elfcpp::R_POWERPC_GOT16_HA:
1360 case elfcpp::R_PPC64_TOC16:
1361 case elfcpp::R_PPC64_TOC16_LO:
1362 case elfcpp::R_PPC64_TOC16_HI:
1363 case elfcpp::R_PPC64_TOC16_HA:
1364 case elfcpp::R_PPC64_TOC16_DS:
1365 case elfcpp::R_PPC64_TOC16_LO_DS:
1367 // The symbol requires a GOT entry.
1368 Output_data_got<size, big_endian>* got;
1370 got = target->got_section(symtab, layout);
1371 if (gsym->final_value_is_known())
1372 got->add_global(gsym, GOT_TYPE_STANDARD);
1373 else
1375 // If this symbol is not fully resolved, we need to add a
1376 // dynamic relocation for it.
1377 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1378 if (gsym->is_from_dynobj()
1379 || gsym->is_undefined()
1380 || gsym->is_preemptible())
1381 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1382 elfcpp::R_POWERPC_GLOB_DAT);
1383 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1385 unsigned int off = got->add_constant(0);
1387 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1388 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1389 got, off, 0);
1393 break;
1395 case elfcpp::R_PPC64_TOC:
1396 // We need a GOT section.
1397 target->got_section(symtab, layout);
1398 break;
1400 case elfcpp::R_POWERPC_GOT_TPREL16:
1401 case elfcpp::R_POWERPC_TLS:
1402 // XXX TLS
1403 break;
1405 // These are relocations which should only be seen by the
1406 // dynamic linker, and should never be seen here.
1407 case elfcpp::R_POWERPC_COPY:
1408 case elfcpp::R_POWERPC_GLOB_DAT:
1409 case elfcpp::R_POWERPC_JMP_SLOT:
1410 case elfcpp::R_POWERPC_RELATIVE:
1411 case elfcpp::R_POWERPC_DTPMOD:
1412 gold_error(_("%s: unexpected reloc %u in object file"),
1413 object->name().c_str(), r_type);
1414 break;
1416 default:
1417 unsupported_reloc_global(object, r_type, gsym);
1418 break;
1422 // Scan relocations for a section.
1424 template<int size, bool big_endian>
1425 void
1426 Target_powerpc<size, big_endian>::scan_relocs(
1427 const General_options& options,
1428 Symbol_table* symtab,
1429 Layout* layout,
1430 Sized_relobj<size, big_endian>* object,
1431 unsigned int data_shndx,
1432 unsigned int sh_type,
1433 const unsigned char* prelocs,
1434 size_t reloc_count,
1435 Output_section* output_section,
1436 bool needs_special_offset_handling,
1437 size_t local_symbol_count,
1438 const unsigned char* plocal_symbols)
1440 typedef Target_powerpc<size, big_endian> Powerpc;
1441 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1442 static Output_data_space* sdata;
1444 if (sh_type == elfcpp::SHT_REL)
1446 gold_error(_("%s: unsupported REL reloc section"),
1447 object->name().c_str());
1448 return;
1451 // Define _SDA_BASE_ at the start of the .sdata section.
1452 if (sdata == NULL)
1454 // layout->find_output_section(".sdata") == NULL
1455 sdata = new Output_data_space(4, "** sdata");
1456 Output_section* os = layout->add_output_section_data(".sdata", 0,
1457 elfcpp::SHF_ALLOC
1458 | elfcpp::SHF_WRITE,
1459 sdata);
1460 symtab->define_in_output_data("_SDA_BASE_", NULL,
1462 32768, 0,
1463 elfcpp::STT_OBJECT,
1464 elfcpp::STB_LOCAL,
1465 elfcpp::STV_HIDDEN, 0,
1466 false, false);
1469 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1470 options,
1471 symtab,
1472 layout,
1473 this,
1474 object,
1475 data_shndx,
1476 prelocs,
1477 reloc_count,
1478 output_section,
1479 needs_special_offset_handling,
1480 local_symbol_count,
1481 plocal_symbols);
1484 // Finalize the sections.
1486 template<int size, bool big_endian>
1487 void
1488 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1490 // Fill in some more dynamic tags.
1491 Output_data_dynamic* const odyn = layout->dynamic_data();
1492 if (odyn != NULL)
1494 if (this->plt_ != NULL)
1496 const Output_data* od = this->plt_->rel_plt();
1497 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1498 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1499 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1501 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1504 if (this->rela_dyn_ != NULL)
1506 const Output_data* od = this->rela_dyn_;
1507 odyn->add_section_address(elfcpp::DT_RELA, od);
1508 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1509 odyn->add_constant(elfcpp::DT_RELAENT,
1510 elfcpp::Elf_sizes<size>::rela_size);
1513 if (!parameters->options().shared())
1515 // The value of the DT_DEBUG tag is filled in by the dynamic
1516 // linker at run time, and used by the debugger.
1517 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1521 // Emit any relocs we saved in an attempt to avoid generating COPY
1522 // relocs.
1523 if (this->copy_relocs_.any_saved_relocs())
1524 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1527 // Perform a relocation.
1529 template<int size, bool big_endian>
1530 inline bool
1531 Target_powerpc<size, big_endian>::Relocate::relocate(
1532 const Relocate_info<size, big_endian>* relinfo,
1533 Target_powerpc* target,
1534 size_t relnum,
1535 const elfcpp::Rela<size, big_endian>& rela,
1536 unsigned int r_type,
1537 const Sized_symbol<size>* gsym,
1538 const Symbol_value<size>* psymval,
1539 unsigned char* view,
1540 typename elfcpp::Elf_types<size>::Elf_Addr address,
1541 section_size_type /* view_size */)
1543 const unsigned int toc_base_offset = 0x8000;
1544 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1546 // Pick the value to use for symbols defined in shared objects.
1547 Symbol_value<size> symval;
1548 if (gsym != NULL
1549 && (gsym->is_from_dynobj()
1550 || (parameters->options().shared()
1551 && (gsym->is_undefined() || gsym->is_preemptible())))
1552 && gsym->has_plt_offset())
1554 elfcpp::Elf_Xword value;
1556 value = target->plt_section()->address() + gsym->plt_offset();
1558 symval.set_output_value(value);
1560 psymval = &symval;
1563 const Sized_relobj<size, big_endian>* object = relinfo->object;
1564 elfcpp::Elf_Xword addend = rela.get_r_addend();
1566 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1567 // pointer points to the beginning, not the end, of the table.
1568 // So we just use the plain offset.
1569 bool have_got_offset = false;
1570 unsigned int got_offset = 0;
1571 unsigned int got2_offset = 0;
1572 switch (r_type)
1574 case elfcpp::R_PPC64_TOC16:
1575 case elfcpp::R_PPC64_TOC16_LO:
1576 case elfcpp::R_PPC64_TOC16_HI:
1577 case elfcpp::R_PPC64_TOC16_HA:
1578 case elfcpp::R_PPC64_TOC16_DS:
1579 case elfcpp::R_PPC64_TOC16_LO_DS:
1580 // Subtract the TOC base address.
1581 addend -= target->toc_section()->address() + toc_base_offset;
1582 /* FALLTHRU */
1584 case elfcpp::R_POWERPC_GOT16:
1585 case elfcpp::R_POWERPC_GOT16_LO:
1586 case elfcpp::R_POWERPC_GOT16_HI:
1587 case elfcpp::R_POWERPC_GOT16_HA:
1588 case elfcpp::R_PPC64_GOT16_DS:
1589 case elfcpp::R_PPC64_GOT16_LO_DS:
1590 if (gsym != NULL)
1592 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1593 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1595 else
1597 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1598 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1599 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1601 have_got_offset = true;
1602 break;
1604 // R_PPC_PLTREL24 is rather special. If non-zero,
1605 // the addend specifies the GOT pointer offset within .got2.
1606 case elfcpp::R_PPC_PLTREL24:
1607 if (addend >= 32768)
1609 Output_data_space* got2;
1610 got2 = target->got2_section();
1611 got2_offset = got2->offset();
1612 addend += got2_offset;
1614 have_got_offset = true;
1615 break;
1617 default:
1618 break;
1621 switch (r_type)
1623 case elfcpp::R_POWERPC_NONE:
1624 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1625 case elfcpp::R_POWERPC_GNU_VTENTRY:
1626 break;
1628 case elfcpp::R_POWERPC_REL32:
1629 Reloc::rel32(view, object, psymval, addend, address);
1630 break;
1632 case elfcpp::R_POWERPC_REL24:
1633 Reloc::rel24(view, object, psymval, addend, address);
1634 break;
1636 case elfcpp::R_POWERPC_REL14:
1637 Reloc::rel14(view, object, psymval, addend, address);
1638 break;
1640 case elfcpp::R_PPC_PLTREL24:
1641 Reloc::rel24(view, object, psymval, addend, address);
1642 break;
1644 case elfcpp::R_PPC_LOCAL24PC:
1645 Reloc::rel24(view, object, psymval, addend, address);
1646 break;
1648 case elfcpp::R_PPC64_ADDR64:
1649 if (!parameters->options().output_is_position_independent())
1650 Relocate_functions<size, big_endian>::rela64(view, object,
1651 psymval, addend);
1652 break;
1654 case elfcpp::R_POWERPC_ADDR32:
1655 if (!parameters->options().output_is_position_independent())
1656 Relocate_functions<size, big_endian>::rela32(view, object,
1657 psymval, addend);
1658 break;
1660 case elfcpp::R_POWERPC_ADDR16_LO:
1661 Reloc::addr16_lo(view, object, psymval, addend);
1662 break;
1664 case elfcpp::R_POWERPC_ADDR16_HI:
1665 Reloc::addr16_hi(view, object, psymval, addend);
1666 break;
1668 case elfcpp::R_POWERPC_ADDR16_HA:
1669 Reloc::addr16_ha(view, object, psymval, addend);
1670 break;
1672 case elfcpp::R_PPC_REL16_LO:
1673 Reloc::rel16_lo(view, object, psymval, addend, address);
1674 break;
1676 case elfcpp::R_PPC_REL16_HI:
1677 Reloc::rel16_lo(view, object, psymval, addend, address);
1678 break;
1680 case elfcpp::R_PPC_REL16_HA:
1681 Reloc::rel16_lo(view, object, psymval, addend, address);
1682 break;
1684 case elfcpp::R_POWERPC_GOT16:
1685 Reloc::addr16(view, got_offset, addend);
1686 break;
1688 case elfcpp::R_POWERPC_GOT16_LO:
1689 Reloc::addr16_lo(view, got_offset, addend);
1690 break;
1692 case elfcpp::R_POWERPC_GOT16_HI:
1693 Reloc::addr16_hi(view, got_offset, addend);
1694 break;
1696 case elfcpp::R_POWERPC_GOT16_HA:
1697 Reloc::addr16_ha(view, got_offset, addend);
1698 break;
1700 case elfcpp::R_PPC64_TOC16:
1701 Reloc::addr16(view, got_offset, addend);
1702 break;
1704 case elfcpp::R_PPC64_TOC16_LO:
1705 Reloc::addr16_lo(view, got_offset, addend);
1706 break;
1708 case elfcpp::R_PPC64_TOC16_HI:
1709 Reloc::addr16_hi(view, got_offset, addend);
1710 break;
1712 case elfcpp::R_PPC64_TOC16_HA:
1713 Reloc::addr16_ha(view, got_offset, addend);
1714 break;
1716 case elfcpp::R_PPC64_TOC16_DS:
1717 case elfcpp::R_PPC64_TOC16_LO_DS:
1718 Reloc::addr16_ds(view, got_offset, addend);
1719 break;
1721 case elfcpp::R_PPC64_TOC:
1723 elfcpp::Elf_types<64>::Elf_Addr value;
1724 value = target->toc_section()->address() + toc_base_offset;
1725 Relocate_functions<64, false>::rela64(view, value, addend);
1727 break;
1729 case elfcpp::R_POWERPC_COPY:
1730 case elfcpp::R_POWERPC_GLOB_DAT:
1731 case elfcpp::R_POWERPC_JMP_SLOT:
1732 case elfcpp::R_POWERPC_RELATIVE:
1733 // This is an outstanding tls reloc, which is unexpected when
1734 // linking.
1735 case elfcpp::R_POWERPC_DTPMOD:
1736 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1737 _("unexpected reloc %u in object file"),
1738 r_type);
1739 break;
1741 default:
1742 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1743 _("unsupported reloc %u"),
1744 r_type);
1745 break;
1748 return true;
1751 // Perform a TLS relocation.
1753 template<int size, bool big_endian>
1754 inline void
1755 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1756 const Relocate_info<size, big_endian>* relinfo,
1757 Target_powerpc<size, big_endian>* target,
1758 size_t relnum,
1759 const elfcpp::Rela<size, big_endian>& rela,
1760 unsigned int r_type,
1761 const Sized_symbol<size>* gsym,
1762 const Symbol_value<size>* psymval,
1763 unsigned char* view,
1764 typename elfcpp::Elf_types<size>::Elf_Addr address,
1765 section_size_type)
1767 Output_segment* tls_segment = relinfo->layout->tls_segment();
1768 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1769 const Sized_relobj<size, big_endian>* object = relinfo->object;
1771 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1772 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1774 const bool is_final =
1775 (gsym == NULL
1776 ? !parameters->options().output_is_position_independent()
1777 : gsym->final_value_is_known());
1778 const tls::Tls_optimization optimized_type
1779 = optimize_tls_reloc(is_final, r_type);
1781 switch (r_type)
1783 // XXX
1787 // Relocate section data.
1789 template<int size, bool big_endian>
1790 void
1791 Target_powerpc<size, big_endian>::relocate_section(
1792 const Relocate_info<size, big_endian>* relinfo,
1793 unsigned int sh_type,
1794 const unsigned char* prelocs,
1795 size_t reloc_count,
1796 Output_section* output_section,
1797 bool needs_special_offset_handling,
1798 unsigned char* view,
1799 typename elfcpp::Elf_types<size>::Elf_Addr address,
1800 section_size_type view_size)
1802 typedef Target_powerpc<size, big_endian> Powerpc;
1803 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1805 gold_assert(sh_type == elfcpp::SHT_RELA);
1807 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1808 Powerpc_relocate>(
1809 relinfo,
1810 this,
1811 prelocs,
1812 reloc_count,
1813 output_section,
1814 needs_special_offset_handling,
1815 view,
1816 address,
1817 view_size);
1820 // Return the size of a relocation while scanning during a relocatable
1821 // link.
1823 template<int size, bool big_endian>
1824 unsigned int
1825 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1826 unsigned int,
1827 Relobj*)
1829 // We are always SHT_RELA, so we should never get here.
1830 gold_unreachable();
1831 return 0;
1834 // Scan the relocs during a relocatable link.
1836 template<int size, bool big_endian>
1837 void
1838 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1839 const General_options& options,
1840 Symbol_table* symtab,
1841 Layout* layout,
1842 Sized_relobj<size, big_endian>* object,
1843 unsigned int data_shndx,
1844 unsigned int sh_type,
1845 const unsigned char* prelocs,
1846 size_t reloc_count,
1847 Output_section* output_section,
1848 bool needs_special_offset_handling,
1849 size_t local_symbol_count,
1850 const unsigned char* plocal_symbols,
1851 Relocatable_relocs* rr)
1853 gold_assert(sh_type == elfcpp::SHT_RELA);
1855 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1856 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1858 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1859 Scan_relocatable_relocs>(
1860 options,
1861 symtab,
1862 layout,
1863 object,
1864 data_shndx,
1865 prelocs,
1866 reloc_count,
1867 output_section,
1868 needs_special_offset_handling,
1869 local_symbol_count,
1870 plocal_symbols,
1871 rr);
1874 // Relocate a section during a relocatable link.
1876 template<int size, bool big_endian>
1877 void
1878 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1879 const Relocate_info<size, big_endian>* relinfo,
1880 unsigned int sh_type,
1881 const unsigned char* prelocs,
1882 size_t reloc_count,
1883 Output_section* output_section,
1884 off_t offset_in_output_section,
1885 const Relocatable_relocs* rr,
1886 unsigned char* view,
1887 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1888 section_size_type view_size,
1889 unsigned char* reloc_view,
1890 section_size_type reloc_view_size)
1892 gold_assert(sh_type == elfcpp::SHT_RELA);
1894 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1895 relinfo,
1896 prelocs,
1897 reloc_count,
1898 output_section,
1899 offset_in_output_section,
1901 view,
1902 view_address,
1903 view_size,
1904 reloc_view,
1905 reloc_view_size);
1908 // Return the value to use for a dynamic which requires special
1909 // treatment. This is how we support equality comparisons of function
1910 // pointers across shared library boundaries, as described in the
1911 // processor specific ABI supplement.
1913 template<int size, bool big_endian>
1914 uint64_t
1915 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1917 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1918 return this->plt_section()->address() + gsym->plt_offset();
1921 // The selector for powerpc object files.
1923 template<int size, bool big_endian>
1924 class Target_selector_powerpc : public Target_selector
1926 public:
1927 Target_selector_powerpc()
1928 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1929 (size == 64 ?
1930 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1931 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1934 Target* instantiated_target_;
1936 Target* do_recognize(int machine, int, int)
1938 switch (size)
1940 case 64:
1941 if (machine != elfcpp::EM_PPC64)
1942 return NULL;
1943 break;
1945 case 32:
1946 if (machine != elfcpp::EM_PPC)
1947 return NULL;
1948 break;
1950 default:
1951 return NULL;
1954 return do_instantiate_target();
1957 Target* do_instantiate_target()
1959 if (this->instantiated_target_ == NULL)
1960 this->instantiated_target_ = new Target_powerpc<size, big_endian>();
1961 return this->instantiated_target_;
1965 Target_selector_powerpc<32, true> target_selector_ppc32;
1966 Target_selector_powerpc<32, false> target_selector_ppc32le;
1967 Target_selector_powerpc<64, true> target_selector_ppc64;
1968 Target_selector_powerpc<64, false> target_selector_ppc64le;
1970 } // End anonymous namespace.