* elf32-spu.c (spu_elf_find_overlays): Call bfd_set_error on errors.
[binutils.git] / gold / powerpc.cc
blob8eac78366edf695de92635bce047e680f0721d7e
1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
24 #include "gold.h"
26 #include "elfcpp.h"
27 #include "parameters.h"
28 #include "reloc.h"
29 #include "powerpc.h"
30 #include "object.h"
31 #include "symtab.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "copy-relocs.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39 #include "errors.h"
41 namespace
44 using namespace gold;
46 template<int size, bool big_endian>
47 class Output_data_plt_powerpc;
49 template<int size, bool big_endian>
50 class Target_powerpc : public Sized_target<size, big_endian>
52 public:
53 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
55 Target_powerpc()
56 : Sized_target<size, big_endian>(&powerpc_info),
57 got_(NULL), got2_(NULL), toc_(NULL),
58 plt_(NULL), rela_dyn_(NULL),
59 copy_relocs_(elfcpp::R_POWERPC_COPY),
60 dynbss_(NULL), got_mod_index_offset_(-1U)
64 // Process the relocations to determine unreferenced sections for
65 // garbage collection.
66 void
67 gc_process_relocs(const General_options& options,
68 Symbol_table* symtab,
69 Layout* layout,
70 Sized_relobj<size, big_endian>* object,
71 unsigned int data_shndx,
72 unsigned int sh_type,
73 const unsigned char* prelocs,
74 size_t reloc_count,
75 Output_section* output_section,
76 bool needs_special_offset_handling,
77 size_t local_symbol_count,
78 const unsigned char* plocal_symbols);
80 // Scan the relocations to look for symbol adjustments.
81 void
82 scan_relocs(const General_options& options,
83 Symbol_table* symtab,
84 Layout* layout,
85 Sized_relobj<size, big_endian>* object,
86 unsigned int data_shndx,
87 unsigned int sh_type,
88 const unsigned char* prelocs,
89 size_t reloc_count,
90 Output_section* output_section,
91 bool needs_special_offset_handling,
92 size_t local_symbol_count,
93 const unsigned char* plocal_symbols);
94 // Finalize the sections.
95 void
96 do_finalize_sections(Layout*);
98 // Return the value to use for a dynamic which requires special
99 // treatment.
100 uint64_t
101 do_dynsym_value(const Symbol*) const;
103 // Relocate a section.
104 void
105 relocate_section(const Relocate_info<size, big_endian>*,
106 unsigned int sh_type,
107 const unsigned char* prelocs,
108 size_t reloc_count,
109 Output_section* output_section,
110 bool needs_special_offset_handling,
111 unsigned char* view,
112 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
113 section_size_type view_size);
115 // Scan the relocs during a relocatable link.
116 void
117 scan_relocatable_relocs(const General_options& options,
118 Symbol_table* symtab,
119 Layout* layout,
120 Sized_relobj<size, big_endian>* object,
121 unsigned int data_shndx,
122 unsigned int sh_type,
123 const unsigned char* prelocs,
124 size_t reloc_count,
125 Output_section* output_section,
126 bool needs_special_offset_handling,
127 size_t local_symbol_count,
128 const unsigned char* plocal_symbols,
129 Relocatable_relocs*);
131 // Relocate a section during a relocatable link.
132 void
133 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
134 unsigned int sh_type,
135 const unsigned char* prelocs,
136 size_t reloc_count,
137 Output_section* output_section,
138 off_t offset_in_output_section,
139 const Relocatable_relocs*,
140 unsigned char* view,
141 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
142 section_size_type view_size,
143 unsigned char* reloc_view,
144 section_size_type reloc_view_size);
146 // Return whether SYM is defined by the ABI.
147 bool
148 do_is_defined_by_abi(const Symbol* sym) const
150 return strcmp(sym->name(), "___tls_get_addr") == 0;
153 // Return the size of the GOT section.
154 section_size_type
155 got_size()
157 gold_assert(this->got_ != NULL);
158 return this->got_->data_size();
161 private:
163 // The class which scans relocations.
164 class Scan
166 public:
167 Scan()
168 : issued_non_pic_error_(false)
171 inline void
172 local(const General_options& options, Symbol_table* symtab,
173 Layout* layout, Target_powerpc* target,
174 Sized_relobj<size, big_endian>* object,
175 unsigned int data_shndx,
176 Output_section* output_section,
177 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
178 const elfcpp::Sym<size, big_endian>& lsym);
180 inline void
181 global(const General_options& options, Symbol_table* symtab,
182 Layout* layout, Target_powerpc* target,
183 Sized_relobj<size, big_endian>* object,
184 unsigned int data_shndx,
185 Output_section* output_section,
186 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
187 Symbol* gsym);
189 private:
190 static void
191 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
192 unsigned int r_type);
194 static void
195 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
196 unsigned int r_type, Symbol*);
198 static void
199 generate_tls_call(Symbol_table* symtab, Layout* layout,
200 Target_powerpc* target);
202 void
203 check_non_pic(Relobj*, unsigned int r_type);
205 // Whether we have issued an error about a non-PIC compilation.
206 bool issued_non_pic_error_;
209 // The class which implements relocation.
210 class Relocate
212 public:
213 // Do a relocation. Return false if the caller should not issue
214 // any warnings about this relocation.
215 inline bool
216 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
217 size_t relnum, const elfcpp::Rela<size, big_endian>&,
218 unsigned int r_type, const Sized_symbol<size>*,
219 const Symbol_value<size>*,
220 unsigned char*,
221 typename elfcpp::Elf_types<size>::Elf_Addr,
222 section_size_type);
224 private:
225 // Do a TLS relocation.
226 inline void
227 relocate_tls(const Relocate_info<size, big_endian>*,
228 Target_powerpc* target,
229 size_t relnum, const elfcpp::Rela<size, big_endian>&,
230 unsigned int r_type, const Sized_symbol<size>*,
231 const Symbol_value<size>*,
232 unsigned char*,
233 typename elfcpp::Elf_types<size>::Elf_Addr,
234 section_size_type);
237 // A class which returns the size required for a relocation type,
238 // used while scanning relocs during a relocatable link.
239 class Relocatable_size_for_reloc
241 public:
242 unsigned int
243 get_size_for_reloc(unsigned int, Relobj*);
246 // Get the GOT section, creating it if necessary.
247 Output_data_got<size, big_endian>*
248 got_section(Symbol_table*, Layout*);
250 Output_data_space*
251 got2_section() const
253 gold_assert (this->got2_ != NULL);
254 return this->got2_;
257 // Get the TOC section.
258 Output_data_space*
259 toc_section() const
261 gold_assert (this->toc_ != NULL);
262 return this->toc_;
265 // Create a PLT entry for a global symbol.
266 void
267 make_plt_entry(Symbol_table*, Layout*, Symbol*);
269 // Create a GOT entry for the TLS module index.
270 unsigned int
271 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
272 Sized_relobj<size, big_endian>* object);
274 // Get the PLT section.
275 const Output_data_plt_powerpc<size, big_endian>*
276 plt_section() const
278 gold_assert(this->plt_ != NULL);
279 return this->plt_;
282 // Get the dynamic reloc section, creating it if necessary.
283 Reloc_section*
284 rela_dyn_section(Layout*);
286 // Return true if the symbol may need a COPY relocation.
287 // References from an executable object to non-function symbols
288 // defined in a dynamic object may need a COPY relocation.
289 bool
290 may_need_copy_reloc(Symbol* gsym)
292 return (!parameters->options().shared()
293 && gsym->is_from_dynobj()
294 && gsym->type() != elfcpp::STT_FUNC);
297 // Copy a relocation against a global symbol.
298 void
299 copy_reloc(Symbol_table* symtab, Layout* layout,
300 Sized_relobj<size, big_endian>* object,
301 unsigned int shndx, Output_section* output_section,
302 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
304 this->copy_relocs_.copy_reloc(symtab, layout,
305 symtab->get_sized_symbol<size>(sym),
306 object, shndx, output_section,
307 reloc, this->rela_dyn_section(layout));
310 // Information about this specific target which we pass to the
311 // general Target structure.
312 static Target::Target_info powerpc_info;
314 // The types of GOT entries needed for this platform.
315 enum Got_type
317 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
318 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
319 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
322 // The GOT section.
323 Output_data_got<size, big_endian>* got_;
324 // The GOT2 section.
325 Output_data_space* got2_;
326 // The TOC section.
327 Output_data_space* toc_;
328 // The PLT section.
329 Output_data_plt_powerpc<size, big_endian>* plt_;
330 // The dynamic reloc section.
331 Reloc_section* rela_dyn_;
332 // Relocs saved to avoid a COPY reloc.
333 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
334 // Space for variables copied with a COPY reloc.
335 Output_data_space* dynbss_;
336 // Offset of the GOT entry for the TLS module index;
337 unsigned int got_mod_index_offset_;
340 template<>
341 Target::Target_info Target_powerpc<32, true>::powerpc_info =
343 32, // size
344 true, // 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<32, false>::powerpc_info =
360 32, // size
361 false, // is_big_endian
362 elfcpp::EM_PPC, // 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 4 * 1024 // common_pagesize (overridable by -z common-page-size)
374 template<>
375 Target::Target_info Target_powerpc<64, true>::powerpc_info =
377 64, // size
378 true, // is_big_endian
379 elfcpp::EM_PPC64, // machine_code
380 false, // has_make_symbol
381 false, // has_resolve
382 false, // has_code_fill
383 true, // is_default_stack_executable
384 '\0', // wrap_char
385 "/usr/lib/ld.so.1", // dynamic_linker
386 0x10000000, // default_text_segment_address
387 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
388 8 * 1024 // common_pagesize (overridable by -z common-page-size)
391 template<>
392 Target::Target_info Target_powerpc<64, false>::powerpc_info =
394 64, // size
395 false, // is_big_endian
396 elfcpp::EM_PPC64, // machine_code
397 false, // has_make_symbol
398 false, // has_resolve
399 false, // has_code_fill
400 true, // is_default_stack_executable
401 '\0', // wrap_char
402 "/usr/lib/ld.so.1", // dynamic_linker
403 0x10000000, // default_text_segment_address
404 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
405 8 * 1024 // common_pagesize (overridable by -z common-page-size)
408 template<int size, bool big_endian>
409 class Powerpc_relocate_functions
411 private:
412 // Do a simple relocation with the addend in the relocation.
413 template<int valsize>
414 static inline void
415 rela(unsigned char* view,
416 unsigned int right_shift,
417 elfcpp::Elf_Xword dst_mask,
418 typename elfcpp::Swap<size, big_endian>::Valtype value,
419 typename elfcpp::Swap<size, big_endian>::Valtype addend)
421 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
422 Valtype* wv = reinterpret_cast<Valtype*>(view);
423 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
424 Valtype reloc = ((value + addend) >> right_shift);
426 val &= ~dst_mask;
427 reloc &= dst_mask;
429 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
432 // Do a simple relocation using a symbol value with the addend in
433 // the relocation.
434 template<int valsize>
435 static inline void
436 rela(unsigned char* view,
437 unsigned int right_shift,
438 elfcpp::Elf_Xword dst_mask,
439 const Sized_relobj<size, big_endian>* object,
440 const Symbol_value<size>* psymval,
441 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
443 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
444 Valtype* wv = reinterpret_cast<Valtype*>(view);
445 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
446 Valtype reloc = (psymval->value(object, addend) >> right_shift);
448 val &= ~dst_mask;
449 reloc &= dst_mask;
451 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
454 // Do a simple relocation using a symbol value with the addend in
455 // the relocation, unaligned.
456 template<int valsize>
457 static inline void
458 rela_ua(unsigned char* view, unsigned int right_shift,
459 elfcpp::Elf_Xword dst_mask,
460 const Sized_relobj<size, big_endian>* object,
461 const Symbol_value<size>* psymval,
462 typename elfcpp::Swap<size, big_endian>::Valtype addend)
464 typedef typename elfcpp::Swap_unaligned<valsize,
465 big_endian>::Valtype Valtype;
466 unsigned char* wv = view;
467 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
468 Valtype reloc = (psymval->value(object, addend) >> right_shift);
470 val &= ~dst_mask;
471 reloc &= dst_mask;
473 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
476 // Do a simple PC relative relocation with a Symbol_value with the
477 // addend in the relocation.
478 template<int valsize>
479 static inline void
480 pcrela(unsigned char* view, unsigned int right_shift,
481 elfcpp::Elf_Xword dst_mask,
482 const Sized_relobj<size, big_endian>* object,
483 const Symbol_value<size>* psymval,
484 typename elfcpp::Swap<size, big_endian>::Valtype addend,
485 typename elfcpp::Elf_types<size>::Elf_Addr address)
487 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
488 Valtype* wv = reinterpret_cast<Valtype*>(view);
489 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
490 Valtype reloc = ((psymval->value(object, addend) - address)
491 >> right_shift);
493 val &= ~dst_mask;
494 reloc &= dst_mask;
496 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
499 template<int valsize>
500 static inline void
501 pcrela_unaligned(unsigned char* view,
502 const Sized_relobj<size, big_endian>* object,
503 const Symbol_value<size>* psymval,
504 typename elfcpp::Swap<size, big_endian>::Valtype addend,
505 typename elfcpp::Elf_types<size>::Elf_Addr address)
507 typedef typename elfcpp::Swap_unaligned<valsize,
508 big_endian>::Valtype Valtype;
509 unsigned char* wv = view;
510 Valtype reloc = (psymval->value(object, addend) - address);
512 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
515 typedef Powerpc_relocate_functions<size, big_endian> This;
516 typedef Relocate_functions<size, big_endian> This_reloc;
517 public:
518 // R_POWERPC_REL32: (Symbol + Addend - Address)
519 static inline void
520 rel32(unsigned char* view,
521 const Sized_relobj<size, big_endian>* object,
522 const Symbol_value<size>* psymval,
523 typename elfcpp::Elf_types<size>::Elf_Addr addend,
524 typename elfcpp::Elf_types<size>::Elf_Addr address)
525 { This_reloc::pcrela32(view, object, psymval, addend, address); }
527 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
528 static inline void
529 rel24(unsigned char* view,
530 const Sized_relobj<size, big_endian>* object,
531 const Symbol_value<size>* psymval,
532 typename elfcpp::Elf_types<size>::Elf_Addr addend,
533 typename elfcpp::Elf_types<size>::Elf_Addr address)
535 This::template pcrela<32>(view, 0, 0x03fffffc, object,
536 psymval, addend, address);
539 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
540 static inline void
541 rel14(unsigned char* view,
542 const Sized_relobj<size, big_endian>* object,
543 const Symbol_value<size>* psymval,
544 typename elfcpp::Elf_types<size>::Elf_Addr addend,
545 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 This::template pcrela<32>(view, 0, 0x0000fffc, object,
548 psymval, addend, address);
551 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
552 static inline void
553 addr16(unsigned char* view,
554 typename elfcpp::Elf_types<size>::Elf_Addr value,
555 typename elfcpp::Elf_types<size>::Elf_Addr addend)
556 { This_reloc::rela16(view, value, addend); }
558 static inline void
559 addr16(unsigned char* view,
560 const Sized_relobj<size, big_endian>* object,
561 const Symbol_value<size>* psymval,
562 typename elfcpp::Elf_types<size>::Elf_Addr addend)
563 { This_reloc::rela16(view, object, psymval, addend); }
565 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
566 static inline void
567 addr16_ds(unsigned char* view,
568 typename elfcpp::Elf_types<size>::Elf_Addr value,
569 typename elfcpp::Elf_types<size>::Elf_Addr addend)
571 This::template rela<16>(view, 0, 0xfffc, value, addend);
574 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
575 static inline void
576 addr16_lo(unsigned char* view,
577 typename elfcpp::Elf_types<size>::Elf_Addr value,
578 typename elfcpp::Elf_types<size>::Elf_Addr addend)
579 { This_reloc::rela16(view, value, addend); }
581 static inline void
582 addr16_lo(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)
586 { This_reloc::rela16(view, object, psymval, addend); }
588 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
589 static inline void
590 addr16_hi(unsigned char* view,
591 typename elfcpp::Elf_types<size>::Elf_Addr value,
592 typename elfcpp::Elf_types<size>::Elf_Addr addend)
594 This::template rela<16>(view, 16, 0xffff, value, addend);
597 static inline void
598 addr16_hi(unsigned char* view,
599 const Sized_relobj<size, big_endian>* object,
600 const Symbol_value<size>* psymval,
601 typename elfcpp::Elf_types<size>::Elf_Addr addend)
603 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
606 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
607 // final value of the low 16 bits of the
608 // relocation is negative, add one.
609 static inline void
610 addr16_ha(unsigned char* view,
611 typename elfcpp::Elf_types<size>::Elf_Addr value,
612 typename elfcpp::Elf_types<size>::Elf_Addr addend)
614 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
616 reloc = value + addend;
618 if (reloc & 0x8000)
619 reloc += 0x10000;
620 reloc >>= 16;
622 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
625 static inline void
626 addr16_ha(unsigned char* view,
627 const Sized_relobj<size, big_endian>* object,
628 const Symbol_value<size>* psymval,
629 typename elfcpp::Elf_types<size>::Elf_Addr addend)
631 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
633 reloc = psymval->value(object, addend);
635 if (reloc & 0x8000)
636 reloc += 0x10000;
637 reloc >>= 16;
639 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
642 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
643 static inline void
644 rel16(unsigned char* view,
645 const Sized_relobj<size, big_endian>* object,
646 const Symbol_value<size>* psymval,
647 typename elfcpp::Elf_types<size>::Elf_Addr addend,
648 typename elfcpp::Elf_types<size>::Elf_Addr address)
649 { This_reloc::pcrela16(view, object, psymval, addend, address); }
651 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
652 static inline void
653 rel16_lo(unsigned char* view,
654 const Sized_relobj<size, big_endian>* object,
655 const Symbol_value<size>* psymval,
656 typename elfcpp::Elf_types<size>::Elf_Addr addend,
657 typename elfcpp::Elf_types<size>::Elf_Addr address)
658 { This_reloc::pcrela16(view, object, psymval, addend, address); }
660 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
661 static inline void
662 rel16_hi(unsigned char* view,
663 const Sized_relobj<size, big_endian>* object,
664 const Symbol_value<size>* psymval,
665 typename elfcpp::Elf_types<size>::Elf_Addr addend,
666 typename elfcpp::Elf_types<size>::Elf_Addr address)
668 This::template pcrela<16>(view, 16, 0xffff, object,
669 psymval, addend, address);
672 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
673 // final value of the low 16 bits of the
674 // relocation is negative, add one.
675 static inline void
676 rel16_ha(unsigned char* view,
677 const Sized_relobj<size, big_endian>* object,
678 const Symbol_value<size>* psymval,
679 typename elfcpp::Elf_types<size>::Elf_Addr addend,
680 typename elfcpp::Elf_types<size>::Elf_Addr address)
682 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
684 reloc = (psymval->value(object, addend) - address);
685 if (reloc & 0x8000)
686 reloc += 0x10000;
687 reloc >>= 16;
689 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
693 // Get the GOT section, creating it if necessary.
695 template<int size, bool big_endian>
696 Output_data_got<size, big_endian>*
697 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
698 Layout* layout)
700 if (this->got_ == NULL)
702 gold_assert(symtab != NULL && layout != NULL);
704 this->got_ = new Output_data_got<size, big_endian>();
706 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
707 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
708 this->got_);
710 // Create the GOT2 or TOC in the .got section.
711 if (size == 32)
713 this->got2_ = new Output_data_space(4, "** GOT2");
714 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
715 elfcpp::SHF_ALLOC
716 | elfcpp::SHF_WRITE,
717 this->got2_);
719 else
721 this->toc_ = new Output_data_space(8, "** TOC");
722 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
723 elfcpp::SHF_ALLOC
724 | elfcpp::SHF_WRITE,
725 this->toc_);
728 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
729 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
730 this->got_,
731 0, 0, elfcpp::STT_OBJECT,
732 elfcpp::STB_LOCAL,
733 elfcpp::STV_HIDDEN, 0,
734 false, false);
737 return this->got_;
740 // Get the dynamic reloc section, creating it if necessary.
742 template<int size, bool big_endian>
743 typename Target_powerpc<size, big_endian>::Reloc_section*
744 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
746 if (this->rela_dyn_ == NULL)
748 gold_assert(layout != NULL);
749 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
750 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
751 elfcpp::SHF_ALLOC, this->rela_dyn_);
753 return this->rela_dyn_;
756 // A class to handle the PLT data.
758 template<int size, bool big_endian>
759 class Output_data_plt_powerpc : public Output_section_data
761 public:
762 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
763 size, big_endian> Reloc_section;
765 Output_data_plt_powerpc(Layout*);
767 // Add an entry to the PLT.
768 void add_entry(Symbol* gsym);
770 // Return the .rela.plt section data.
771 const Reloc_section* rel_plt() const
773 return this->rel_;
776 protected:
777 void do_adjust_output_section(Output_section* os);
779 private:
780 // The size of an entry in the PLT.
781 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
783 // Set the final size.
784 void
785 set_final_data_size()
787 unsigned int full_count = this->count_ + 4;
789 this->set_data_size(full_count * base_plt_entry_size);
792 // Write out the PLT data.
793 void
794 do_write(Output_file*);
796 // The reloc section.
797 Reloc_section* rel_;
798 // The number of PLT entries.
799 unsigned int count_;
802 // Create the PLT section. The ordinary .got section is an argument,
803 // since we need to refer to the start.
805 template<int size, bool big_endian>
806 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
807 : Output_section_data(size == 32 ? 4 : 8), count_(0)
809 this->rel_ = new Reloc_section(false);
810 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
811 elfcpp::SHF_ALLOC, this->rel_);
814 template<int size, bool big_endian>
815 void
816 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
818 os->set_entsize(0);
821 // Add an entry to the PLT.
823 template<int size, bool big_endian>
824 void
825 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
827 gold_assert(!gsym->has_plt_offset());
828 unsigned int index = this->count_+ + 4;
829 section_offset_type plt_offset;
831 if (index < 8192)
832 plt_offset = index * base_plt_entry_size;
833 else
834 gold_unreachable();
836 gsym->set_plt_offset(plt_offset);
838 ++this->count_;
840 gsym->set_needs_dynsym_entry();
841 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
842 plt_offset, 0);
845 static const unsigned int addis_11_11 = 0x3d6b0000;
846 static const unsigned int addis_11_30 = 0x3d7e0000;
847 static const unsigned int addis_12_12 = 0x3d8c0000;
848 static const unsigned int addi_11_11 = 0x396b0000;
849 static const unsigned int add_0_11_11 = 0x7c0b5a14;
850 static const unsigned int add_11_0_11 = 0x7d605a14;
851 static const unsigned int b = 0x48000000;
852 static const unsigned int bcl_20_31 = 0x429f0005;
853 static const unsigned int bctr = 0x4e800420;
854 static const unsigned int lis_11 = 0x3d600000;
855 static const unsigned int lis_12 = 0x3d800000;
856 static const unsigned int lwzu_0_12 = 0x840c0000;
857 static const unsigned int lwz_0_12 = 0x800c0000;
858 static const unsigned int lwz_11_11 = 0x816b0000;
859 static const unsigned int lwz_11_30 = 0x817e0000;
860 static const unsigned int lwz_12_12 = 0x818c0000;
861 static const unsigned int mflr_0 = 0x7c0802a6;
862 static const unsigned int mflr_12 = 0x7d8802a6;
863 static const unsigned int mtctr_0 = 0x7c0903a6;
864 static const unsigned int mtctr_11 = 0x7d6903a6;
865 static const unsigned int mtlr_0 = 0x7c0803a6;
866 static const unsigned int nop = 0x60000000;
867 static const unsigned int sub_11_11_12 = 0x7d6c5850;
869 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
870 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
871 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
872 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
873 /* ld %r11,xxx+16@l(%r12) */
876 // Write out the PLT.
878 template<int size, bool big_endian>
879 void
880 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
882 const off_t offset = this->offset();
883 const section_size_type oview_size =
884 convert_to_section_size_type(this->data_size());
885 unsigned char* const oview = of->get_output_view(offset, oview_size);
886 unsigned char* pov = oview;
888 memset(pov, 0, base_plt_entry_size * 4);
889 pov += base_plt_entry_size * 4;
891 unsigned int plt_offset = base_plt_entry_size * 4;
892 const unsigned int count = this->count_;
894 if (size == 64)
896 for (unsigned int i = 0; i < count; i++)
900 else
902 for (unsigned int i = 0; i < count; i++)
904 elfcpp::Swap<32, true>::writeval(pov + 0x00,
905 lwz_11_30 + plt_offset);
906 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
907 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
908 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
909 pov += base_plt_entry_size;
910 plt_offset += base_plt_entry_size;
914 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
916 of->write_output_view(offset, oview_size, oview);
919 // Create a PLT entry for a global symbol.
921 template<int size, bool big_endian>
922 void
923 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
924 Layout* layout,
925 Symbol* gsym)
927 if (gsym->has_plt_offset())
928 return;
930 if (this->plt_ == NULL)
932 // Create the GOT section first.
933 this->got_section(symtab, layout);
935 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
936 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
937 (elfcpp::SHF_ALLOC
938 | elfcpp::SHF_EXECINSTR
939 | elfcpp::SHF_WRITE),
940 this->plt_);
942 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
943 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
944 this->plt_,
945 0, 0, elfcpp::STT_OBJECT,
946 elfcpp::STB_LOCAL,
947 elfcpp::STV_HIDDEN, 0,
948 false, false);
951 this->plt_->add_entry(gsym);
954 // Create a GOT entry for the TLS module index.
956 template<int size, bool big_endian>
957 unsigned int
958 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
959 Layout* layout,
960 Sized_relobj<size, big_endian>* object)
962 if (this->got_mod_index_offset_ == -1U)
964 gold_assert(symtab != NULL && layout != NULL && object != NULL);
965 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
966 Output_data_got<size, big_endian>* got;
967 unsigned int got_offset;
969 got = this->got_section(symtab, layout);
970 got_offset = got->add_constant(0);
971 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
972 got_offset, 0);
973 got->add_constant(0);
974 this->got_mod_index_offset_ = got_offset;
976 return this->got_mod_index_offset_;
979 // Optimize the TLS relocation type based on what we know about the
980 // symbol. IS_FINAL is true if the final address of this symbol is
981 // known at link time.
983 static tls::Tls_optimization
984 optimize_tls_reloc(bool /* is_final */, int r_type)
986 // If we are generating a shared library, then we can't do anything
987 // in the linker.
988 if (parameters->options().shared())
989 return tls::TLSOPT_NONE;
990 switch (r_type)
992 // XXX
993 default:
994 gold_unreachable();
998 // Report an unsupported relocation against a local symbol.
1000 template<int size, bool big_endian>
1001 void
1002 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1003 Sized_relobj<size, big_endian>* object,
1004 unsigned int r_type)
1006 gold_error(_("%s: unsupported reloc %u against local symbol"),
1007 object->name().c_str(), r_type);
1010 // We are about to emit a dynamic relocation of type R_TYPE. If the
1011 // dynamic linker does not support it, issue an error.
1013 template<int size, bool big_endian>
1014 void
1015 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1016 unsigned int r_type)
1018 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1020 // These are the relocation types supported by glibc for both 32-bit
1021 // and 64-bit powerpc.
1022 switch (r_type)
1024 case elfcpp::R_POWERPC_RELATIVE:
1025 case elfcpp::R_POWERPC_GLOB_DAT:
1026 case elfcpp::R_POWERPC_DTPMOD:
1027 case elfcpp::R_POWERPC_DTPREL:
1028 case elfcpp::R_POWERPC_TPREL:
1029 case elfcpp::R_POWERPC_JMP_SLOT:
1030 case elfcpp::R_POWERPC_COPY:
1031 case elfcpp::R_POWERPC_ADDR32:
1032 case elfcpp::R_POWERPC_ADDR24:
1033 case elfcpp::R_POWERPC_REL24:
1034 return;
1036 default:
1037 break;
1040 if (size == 64)
1042 switch (r_type)
1044 // These are the relocation types supported only on 64-bit.
1045 case elfcpp::R_PPC64_ADDR64:
1046 case elfcpp::R_PPC64_TPREL16_LO_DS:
1047 case elfcpp::R_PPC64_TPREL16_DS:
1048 case elfcpp::R_POWERPC_TPREL16:
1049 case elfcpp::R_POWERPC_TPREL16_LO:
1050 case elfcpp::R_POWERPC_TPREL16_HI:
1051 case elfcpp::R_POWERPC_TPREL16_HA:
1052 case elfcpp::R_PPC64_TPREL16_HIGHER:
1053 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1054 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1055 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1056 case elfcpp::R_PPC64_ADDR16_LO_DS:
1057 case elfcpp::R_POWERPC_ADDR16_LO:
1058 case elfcpp::R_POWERPC_ADDR16_HI:
1059 case elfcpp::R_POWERPC_ADDR16_HA:
1060 case elfcpp::R_POWERPC_ADDR30:
1061 case elfcpp::R_PPC64_UADDR64:
1062 case elfcpp::R_POWERPC_UADDR32:
1063 case elfcpp::R_POWERPC_ADDR16:
1064 case elfcpp::R_POWERPC_UADDR16:
1065 case elfcpp::R_PPC64_ADDR16_DS:
1066 case elfcpp::R_PPC64_ADDR16_HIGHER:
1067 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1068 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1069 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1070 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1071 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1072 case elfcpp::R_POWERPC_REL32:
1073 case elfcpp::R_PPC64_REL64:
1074 return;
1076 default:
1077 break;
1080 else
1082 switch (r_type)
1084 // These are the relocation types supported only on 32-bit.
1086 default:
1087 break;
1091 // This prevents us from issuing more than one error per reloc
1092 // section. But we can still wind up issuing more than one
1093 // error per object file.
1094 if (this->issued_non_pic_error_)
1095 return;
1096 object->error(_("requires unsupported dynamic reloc; "
1097 "recompile with -fPIC"));
1098 this->issued_non_pic_error_ = true;
1099 return;
1102 // Scan a relocation for a local symbol.
1104 template<int size, bool big_endian>
1105 inline void
1106 Target_powerpc<size, big_endian>::Scan::local(
1107 const General_options&,
1108 Symbol_table* symtab,
1109 Layout* layout,
1110 Target_powerpc<size, big_endian>* target,
1111 Sized_relobj<size, big_endian>* object,
1112 unsigned int data_shndx,
1113 Output_section* output_section,
1114 const elfcpp::Rela<size, big_endian>& reloc,
1115 unsigned int r_type,
1116 const elfcpp::Sym<size, big_endian>& lsym)
1118 switch (r_type)
1120 case elfcpp::R_POWERPC_NONE:
1121 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1122 case elfcpp::R_POWERPC_GNU_VTENTRY:
1123 break;
1125 case elfcpp::R_PPC64_ADDR64:
1126 case elfcpp::R_POWERPC_ADDR32:
1127 case elfcpp::R_POWERPC_ADDR16_HA:
1128 case elfcpp::R_POWERPC_ADDR16_LO:
1129 // If building a shared library (or a position-independent
1130 // executable), we need to create a dynamic relocation for
1131 // this location.
1132 if (parameters->options().output_is_position_independent())
1134 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1136 check_non_pic(object, r_type);
1137 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1139 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1140 rela_dyn->add_local(object, r_sym, r_type, output_section,
1141 data_shndx, reloc.get_r_offset(),
1142 reloc.get_r_addend());
1144 else
1146 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1147 gold_assert(lsym.get_st_value() == 0);
1148 rela_dyn->add_local_relative(object, r_sym, r_type,
1149 output_section, data_shndx,
1150 reloc.get_r_offset(),
1151 reloc.get_r_addend());
1154 break;
1156 case elfcpp::R_POWERPC_REL24:
1157 case elfcpp::R_PPC_LOCAL24PC:
1158 case elfcpp::R_POWERPC_REL32:
1159 case elfcpp::R_PPC_REL16_LO:
1160 case elfcpp::R_PPC_REL16_HA:
1161 break;
1163 case elfcpp::R_POWERPC_GOT16:
1164 case elfcpp::R_POWERPC_GOT16_LO:
1165 case elfcpp::R_POWERPC_GOT16_HI:
1166 case elfcpp::R_POWERPC_GOT16_HA:
1167 case elfcpp::R_PPC64_TOC16:
1168 case elfcpp::R_PPC64_TOC16_LO:
1169 case elfcpp::R_PPC64_TOC16_HI:
1170 case elfcpp::R_PPC64_TOC16_HA:
1171 case elfcpp::R_PPC64_TOC16_DS:
1172 case elfcpp::R_PPC64_TOC16_LO_DS:
1174 // The symbol requires a GOT entry.
1175 Output_data_got<size, big_endian>* got;
1176 unsigned int r_sym;
1178 got = target->got_section(symtab, layout);
1179 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1181 // If we are generating a shared object, we need to add a
1182 // dynamic relocation for this symbol's GOT entry.
1183 if (parameters->options().output_is_position_independent())
1185 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1187 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1188 unsigned int off;
1190 off = got->add_constant(0);
1191 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1192 rela_dyn->add_local_relative(object, r_sym,
1193 elfcpp::R_POWERPC_RELATIVE,
1194 got, off, 0);
1197 else
1198 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1200 break;
1202 case elfcpp::R_PPC64_TOC:
1203 // We need a GOT section.
1204 target->got_section(symtab, layout);
1205 break;
1207 // These are relocations which should only be seen by the
1208 // dynamic linker, and should never be seen here.
1209 case elfcpp::R_POWERPC_COPY:
1210 case elfcpp::R_POWERPC_GLOB_DAT:
1211 case elfcpp::R_POWERPC_JMP_SLOT:
1212 case elfcpp::R_POWERPC_RELATIVE:
1213 case elfcpp::R_POWERPC_DTPMOD:
1214 gold_error(_("%s: unexpected reloc %u in object file"),
1215 object->name().c_str(), r_type);
1216 break;
1218 default:
1219 unsupported_reloc_local(object, r_type);
1220 break;
1224 // Report an unsupported relocation against a global symbol.
1226 template<int size, bool big_endian>
1227 void
1228 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1229 Sized_relobj<size, big_endian>* object,
1230 unsigned int r_type,
1231 Symbol* gsym)
1233 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1234 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1237 // Scan a relocation for a global symbol.
1239 template<int size, bool big_endian>
1240 inline void
1241 Target_powerpc<size, big_endian>::Scan::global(
1242 const General_options&,
1243 Symbol_table* symtab,
1244 Layout* layout,
1245 Target_powerpc<size, big_endian>* target,
1246 Sized_relobj<size, big_endian>* object,
1247 unsigned int data_shndx,
1248 Output_section* output_section,
1249 const elfcpp::Rela<size, big_endian>& reloc,
1250 unsigned int r_type,
1251 Symbol* gsym)
1253 switch (r_type)
1255 case elfcpp::R_POWERPC_NONE:
1256 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1257 case elfcpp::R_POWERPC_GNU_VTENTRY:
1258 break;
1260 case elfcpp::R_PPC_PLTREL24:
1261 // If the symbol is fully resolved, this is just a PC32 reloc.
1262 // Otherwise we need a PLT entry.
1263 if (gsym->final_value_is_known())
1264 break;
1265 // If building a shared library, we can also skip the PLT entry
1266 // if the symbol is defined in the output file and is protected
1267 // or hidden.
1268 if (gsym->is_defined()
1269 && !gsym->is_from_dynobj()
1270 && !gsym->is_preemptible())
1271 break;
1272 target->make_plt_entry(symtab, layout, gsym);
1273 break;
1275 case elfcpp::R_POWERPC_ADDR16:
1276 case elfcpp::R_POWERPC_ADDR16_LO:
1277 case elfcpp::R_POWERPC_ADDR16_HI:
1278 case elfcpp::R_POWERPC_ADDR16_HA:
1279 case elfcpp::R_POWERPC_ADDR32:
1280 case elfcpp::R_PPC64_ADDR64:
1282 // Make a PLT entry if necessary.
1283 if (gsym->needs_plt_entry())
1285 target->make_plt_entry(symtab, layout, gsym);
1286 // Since this is not a PC-relative relocation, we may be
1287 // taking the address of a function. In that case we need to
1288 // set the entry in the dynamic symbol table to the address of
1289 // the PLT entry.
1290 if (gsym->is_from_dynobj() && !parameters->options().shared())
1291 gsym->set_needs_dynsym_value();
1293 // Make a dynamic relocation if necessary.
1294 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1296 if (target->may_need_copy_reloc(gsym))
1298 target->copy_reloc(symtab, layout, object,
1299 data_shndx, output_section, gsym, reloc);
1301 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1302 || r_type == elfcpp::R_PPC64_ADDR64)
1303 && gsym->can_use_relative_reloc(false))
1305 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1306 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1307 output_section, object,
1308 data_shndx, reloc.get_r_offset(),
1309 reloc.get_r_addend());
1311 else
1313 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1315 check_non_pic(object, r_type);
1316 if (gsym->is_from_dynobj()
1317 || gsym->is_undefined()
1318 || gsym->is_preemptible())
1319 rela_dyn->add_global(gsym, r_type, output_section,
1320 object, data_shndx,
1321 reloc.get_r_offset(),
1322 reloc.get_r_addend());
1323 else
1324 rela_dyn->add_global_relative(gsym, r_type,
1325 output_section, object,
1326 data_shndx,
1327 reloc.get_r_offset(),
1328 reloc.get_r_addend());
1332 break;
1334 case elfcpp::R_POWERPC_REL24:
1335 case elfcpp::R_PPC_LOCAL24PC:
1336 case elfcpp::R_PPC_REL16:
1337 case elfcpp::R_PPC_REL16_LO:
1338 case elfcpp::R_PPC_REL16_HI:
1339 case elfcpp::R_PPC_REL16_HA:
1341 if (gsym->needs_plt_entry())
1342 target->make_plt_entry(symtab, layout, gsym);
1343 // Make a dynamic relocation if necessary.
1344 int flags = Symbol::NON_PIC_REF;
1345 if (gsym->type() == elfcpp::STT_FUNC)
1346 flags |= Symbol::FUNCTION_CALL;
1347 if (gsym->needs_dynamic_reloc(flags))
1349 if (target->may_need_copy_reloc(gsym))
1351 target->copy_reloc(symtab, layout, object,
1352 data_shndx, output_section, gsym,
1353 reloc);
1355 else
1357 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1358 check_non_pic(object, r_type);
1359 rela_dyn->add_global(gsym, r_type, output_section, object,
1360 data_shndx, reloc.get_r_offset(),
1361 reloc.get_r_addend());
1365 break;
1367 case elfcpp::R_POWERPC_GOT16:
1368 case elfcpp::R_POWERPC_GOT16_LO:
1369 case elfcpp::R_POWERPC_GOT16_HI:
1370 case elfcpp::R_POWERPC_GOT16_HA:
1371 case elfcpp::R_PPC64_TOC16:
1372 case elfcpp::R_PPC64_TOC16_LO:
1373 case elfcpp::R_PPC64_TOC16_HI:
1374 case elfcpp::R_PPC64_TOC16_HA:
1375 case elfcpp::R_PPC64_TOC16_DS:
1376 case elfcpp::R_PPC64_TOC16_LO_DS:
1378 // The symbol requires a GOT entry.
1379 Output_data_got<size, big_endian>* got;
1381 got = target->got_section(symtab, layout);
1382 if (gsym->final_value_is_known())
1383 got->add_global(gsym, GOT_TYPE_STANDARD);
1384 else
1386 // If this symbol is not fully resolved, we need to add a
1387 // dynamic relocation for it.
1388 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1389 if (gsym->is_from_dynobj()
1390 || gsym->is_undefined()
1391 || gsym->is_preemptible())
1392 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1393 elfcpp::R_POWERPC_GLOB_DAT);
1394 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1396 unsigned int off = got->add_constant(0);
1398 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1399 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1400 got, off, 0);
1404 break;
1406 case elfcpp::R_PPC64_TOC:
1407 // We need a GOT section.
1408 target->got_section(symtab, layout);
1409 break;
1411 case elfcpp::R_POWERPC_GOT_TPREL16:
1412 case elfcpp::R_POWERPC_TLS:
1413 // XXX TLS
1414 break;
1416 // These are relocations which should only be seen by the
1417 // dynamic linker, and should never be seen here.
1418 case elfcpp::R_POWERPC_COPY:
1419 case elfcpp::R_POWERPC_GLOB_DAT:
1420 case elfcpp::R_POWERPC_JMP_SLOT:
1421 case elfcpp::R_POWERPC_RELATIVE:
1422 case elfcpp::R_POWERPC_DTPMOD:
1423 gold_error(_("%s: unexpected reloc %u in object file"),
1424 object->name().c_str(), r_type);
1425 break;
1427 default:
1428 unsupported_reloc_global(object, r_type, gsym);
1429 break;
1433 // Process relocations for gc.
1435 template<int size, bool big_endian>
1436 void
1437 Target_powerpc<size, big_endian>::gc_process_relocs(
1438 const General_options& options,
1439 Symbol_table* symtab,
1440 Layout* layout,
1441 Sized_relobj<size, big_endian>* object,
1442 unsigned int data_shndx,
1443 unsigned int,
1444 const unsigned char* prelocs,
1445 size_t reloc_count,
1446 Output_section* output_section,
1447 bool needs_special_offset_handling,
1448 size_t local_symbol_count,
1449 const unsigned char* plocal_symbols)
1451 typedef Target_powerpc<size, big_endian> Powerpc;
1452 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1454 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1455 options,
1456 symtab,
1457 layout,
1458 this,
1459 object,
1460 data_shndx,
1461 prelocs,
1462 reloc_count,
1463 output_section,
1464 needs_special_offset_handling,
1465 local_symbol_count,
1466 plocal_symbols);
1469 // Scan relocations for a section.
1471 template<int size, bool big_endian>
1472 void
1473 Target_powerpc<size, big_endian>::scan_relocs(
1474 const General_options& options,
1475 Symbol_table* symtab,
1476 Layout* layout,
1477 Sized_relobj<size, big_endian>* object,
1478 unsigned int data_shndx,
1479 unsigned int sh_type,
1480 const unsigned char* prelocs,
1481 size_t reloc_count,
1482 Output_section* output_section,
1483 bool needs_special_offset_handling,
1484 size_t local_symbol_count,
1485 const unsigned char* plocal_symbols)
1487 typedef Target_powerpc<size, big_endian> Powerpc;
1488 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1489 static Output_data_space* sdata;
1491 if (sh_type == elfcpp::SHT_REL)
1493 gold_error(_("%s: unsupported REL reloc section"),
1494 object->name().c_str());
1495 return;
1498 // Define _SDA_BASE_ at the start of the .sdata section.
1499 if (sdata == NULL)
1501 // layout->find_output_section(".sdata") == NULL
1502 sdata = new Output_data_space(4, "** sdata");
1503 Output_section* os = layout->add_output_section_data(".sdata", 0,
1504 elfcpp::SHF_ALLOC
1505 | elfcpp::SHF_WRITE,
1506 sdata);
1507 symtab->define_in_output_data("_SDA_BASE_", NULL,
1509 32768, 0,
1510 elfcpp::STT_OBJECT,
1511 elfcpp::STB_LOCAL,
1512 elfcpp::STV_HIDDEN, 0,
1513 false, false);
1516 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1517 options,
1518 symtab,
1519 layout,
1520 this,
1521 object,
1522 data_shndx,
1523 prelocs,
1524 reloc_count,
1525 output_section,
1526 needs_special_offset_handling,
1527 local_symbol_count,
1528 plocal_symbols);
1531 // Finalize the sections.
1533 template<int size, bool big_endian>
1534 void
1535 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1537 // Fill in some more dynamic tags.
1538 Output_data_dynamic* const odyn = layout->dynamic_data();
1539 if (odyn != NULL)
1541 if (this->plt_ != NULL)
1543 const Output_data* od = this->plt_->rel_plt();
1544 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1545 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1546 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1548 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1551 if (this->rela_dyn_ != NULL)
1553 const Output_data* od = this->rela_dyn_;
1554 odyn->add_section_address(elfcpp::DT_RELA, od);
1555 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1556 odyn->add_constant(elfcpp::DT_RELAENT,
1557 elfcpp::Elf_sizes<size>::rela_size);
1560 if (!parameters->options().shared())
1562 // The value of the DT_DEBUG tag is filled in by the dynamic
1563 // linker at run time, and used by the debugger.
1564 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1568 // Emit any relocs we saved in an attempt to avoid generating COPY
1569 // relocs.
1570 if (this->copy_relocs_.any_saved_relocs())
1571 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1574 // Perform a relocation.
1576 template<int size, bool big_endian>
1577 inline bool
1578 Target_powerpc<size, big_endian>::Relocate::relocate(
1579 const Relocate_info<size, big_endian>* relinfo,
1580 Target_powerpc* target,
1581 size_t relnum,
1582 const elfcpp::Rela<size, big_endian>& rela,
1583 unsigned int r_type,
1584 const Sized_symbol<size>* gsym,
1585 const Symbol_value<size>* psymval,
1586 unsigned char* view,
1587 typename elfcpp::Elf_types<size>::Elf_Addr address,
1588 section_size_type /* view_size */)
1590 const unsigned int toc_base_offset = 0x8000;
1591 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1593 // Pick the value to use for symbols defined in shared objects.
1594 Symbol_value<size> symval;
1595 if (gsym != NULL
1596 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1597 || r_type == elfcpp::R_PPC_LOCAL24PC
1598 || r_type == elfcpp::R_PPC_REL16
1599 || r_type == elfcpp::R_PPC_REL16_LO
1600 || r_type == elfcpp::R_PPC_REL16_HI
1601 || r_type == elfcpp::R_PPC_REL16_HA))
1603 elfcpp::Elf_Xword value;
1605 value = target->plt_section()->address() + gsym->plt_offset();
1607 symval.set_output_value(value);
1609 psymval = &symval;
1612 const Sized_relobj<size, big_endian>* object = relinfo->object;
1613 elfcpp::Elf_Xword addend = rela.get_r_addend();
1615 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1616 // pointer points to the beginning, not the end, of the table.
1617 // So we just use the plain offset.
1618 bool have_got_offset = false;
1619 unsigned int got_offset = 0;
1620 unsigned int got2_offset = 0;
1621 switch (r_type)
1623 case elfcpp::R_PPC64_TOC16:
1624 case elfcpp::R_PPC64_TOC16_LO:
1625 case elfcpp::R_PPC64_TOC16_HI:
1626 case elfcpp::R_PPC64_TOC16_HA:
1627 case elfcpp::R_PPC64_TOC16_DS:
1628 case elfcpp::R_PPC64_TOC16_LO_DS:
1629 // Subtract the TOC base address.
1630 addend -= target->toc_section()->address() + toc_base_offset;
1631 /* FALLTHRU */
1633 case elfcpp::R_POWERPC_GOT16:
1634 case elfcpp::R_POWERPC_GOT16_LO:
1635 case elfcpp::R_POWERPC_GOT16_HI:
1636 case elfcpp::R_POWERPC_GOT16_HA:
1637 case elfcpp::R_PPC64_GOT16_DS:
1638 case elfcpp::R_PPC64_GOT16_LO_DS:
1639 if (gsym != NULL)
1641 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1642 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1644 else
1646 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1647 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1648 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1650 have_got_offset = true;
1651 break;
1653 // R_PPC_PLTREL24 is rather special. If non-zero,
1654 // the addend specifies the GOT pointer offset within .got2.
1655 case elfcpp::R_PPC_PLTREL24:
1656 if (addend >= 32768)
1658 Output_data_space* got2;
1659 got2 = target->got2_section();
1660 got2_offset = got2->offset();
1661 addend += got2_offset;
1663 have_got_offset = true;
1664 break;
1666 default:
1667 break;
1670 switch (r_type)
1672 case elfcpp::R_POWERPC_NONE:
1673 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1674 case elfcpp::R_POWERPC_GNU_VTENTRY:
1675 break;
1677 case elfcpp::R_POWERPC_REL32:
1678 Reloc::rel32(view, object, psymval, addend, address);
1679 break;
1681 case elfcpp::R_POWERPC_REL24:
1682 Reloc::rel24(view, object, psymval, addend, address);
1683 break;
1685 case elfcpp::R_POWERPC_REL14:
1686 Reloc::rel14(view, object, psymval, addend, address);
1687 break;
1689 case elfcpp::R_PPC_PLTREL24:
1690 Reloc::rel24(view, object, psymval, addend, address);
1691 break;
1693 case elfcpp::R_PPC_LOCAL24PC:
1694 Reloc::rel24(view, object, psymval, addend, address);
1695 break;
1697 case elfcpp::R_PPC64_ADDR64:
1698 if (!parameters->options().output_is_position_independent())
1699 Relocate_functions<size, big_endian>::rela64(view, object,
1700 psymval, addend);
1701 break;
1703 case elfcpp::R_POWERPC_ADDR32:
1704 if (!parameters->options().output_is_position_independent())
1705 Relocate_functions<size, big_endian>::rela32(view, object,
1706 psymval, addend);
1707 break;
1709 case elfcpp::R_POWERPC_ADDR16_LO:
1710 Reloc::addr16_lo(view, object, psymval, addend);
1711 break;
1713 case elfcpp::R_POWERPC_ADDR16_HI:
1714 Reloc::addr16_hi(view, object, psymval, addend);
1715 break;
1717 case elfcpp::R_POWERPC_ADDR16_HA:
1718 Reloc::addr16_ha(view, object, psymval, addend);
1719 break;
1721 case elfcpp::R_PPC_REL16_LO:
1722 Reloc::rel16_lo(view, object, psymval, addend, address);
1723 break;
1725 case elfcpp::R_PPC_REL16_HI:
1726 Reloc::rel16_lo(view, object, psymval, addend, address);
1727 break;
1729 case elfcpp::R_PPC_REL16_HA:
1730 Reloc::rel16_ha(view, object, psymval, addend, address);
1731 break;
1733 case elfcpp::R_POWERPC_GOT16:
1734 Reloc::addr16(view, got_offset, addend);
1735 break;
1737 case elfcpp::R_POWERPC_GOT16_LO:
1738 Reloc::addr16_lo(view, got_offset, addend);
1739 break;
1741 case elfcpp::R_POWERPC_GOT16_HI:
1742 Reloc::addr16_hi(view, got_offset, addend);
1743 break;
1745 case elfcpp::R_POWERPC_GOT16_HA:
1746 Reloc::addr16_ha(view, got_offset, addend);
1747 break;
1749 case elfcpp::R_PPC64_TOC16:
1750 Reloc::addr16(view, got_offset, addend);
1751 break;
1753 case elfcpp::R_PPC64_TOC16_LO:
1754 Reloc::addr16_lo(view, got_offset, addend);
1755 break;
1757 case elfcpp::R_PPC64_TOC16_HI:
1758 Reloc::addr16_hi(view, got_offset, addend);
1759 break;
1761 case elfcpp::R_PPC64_TOC16_HA:
1762 Reloc::addr16_ha(view, got_offset, addend);
1763 break;
1765 case elfcpp::R_PPC64_TOC16_DS:
1766 case elfcpp::R_PPC64_TOC16_LO_DS:
1767 Reloc::addr16_ds(view, got_offset, addend);
1768 break;
1770 case elfcpp::R_PPC64_TOC:
1772 elfcpp::Elf_types<64>::Elf_Addr value;
1773 value = target->toc_section()->address() + toc_base_offset;
1774 Relocate_functions<64, false>::rela64(view, value, addend);
1776 break;
1778 case elfcpp::R_POWERPC_COPY:
1779 case elfcpp::R_POWERPC_GLOB_DAT:
1780 case elfcpp::R_POWERPC_JMP_SLOT:
1781 case elfcpp::R_POWERPC_RELATIVE:
1782 // This is an outstanding tls reloc, which is unexpected when
1783 // linking.
1784 case elfcpp::R_POWERPC_DTPMOD:
1785 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1786 _("unexpected reloc %u in object file"),
1787 r_type);
1788 break;
1790 default:
1791 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1792 _("unsupported reloc %u"),
1793 r_type);
1794 break;
1797 return true;
1800 // Perform a TLS relocation.
1802 template<int size, bool big_endian>
1803 inline void
1804 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1805 const Relocate_info<size, big_endian>* relinfo,
1806 Target_powerpc<size, big_endian>* target,
1807 size_t relnum,
1808 const elfcpp::Rela<size, big_endian>& rela,
1809 unsigned int r_type,
1810 const Sized_symbol<size>* gsym,
1811 const Symbol_value<size>* psymval,
1812 unsigned char* view,
1813 typename elfcpp::Elf_types<size>::Elf_Addr address,
1814 section_size_type)
1816 Output_segment* tls_segment = relinfo->layout->tls_segment();
1817 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1818 const Sized_relobj<size, big_endian>* object = relinfo->object;
1820 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1821 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1823 const bool is_final =
1824 (gsym == NULL
1825 ? !parameters->options().output_is_position_independent()
1826 : gsym->final_value_is_known());
1827 const tls::Tls_optimization optimized_type
1828 = optimize_tls_reloc(is_final, r_type);
1830 switch (r_type)
1832 // XXX
1836 // Relocate section data.
1838 template<int size, bool big_endian>
1839 void
1840 Target_powerpc<size, big_endian>::relocate_section(
1841 const Relocate_info<size, big_endian>* relinfo,
1842 unsigned int sh_type,
1843 const unsigned char* prelocs,
1844 size_t reloc_count,
1845 Output_section* output_section,
1846 bool needs_special_offset_handling,
1847 unsigned char* view,
1848 typename elfcpp::Elf_types<size>::Elf_Addr address,
1849 section_size_type view_size)
1851 typedef Target_powerpc<size, big_endian> Powerpc;
1852 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1854 gold_assert(sh_type == elfcpp::SHT_RELA);
1856 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1857 Powerpc_relocate>(
1858 relinfo,
1859 this,
1860 prelocs,
1861 reloc_count,
1862 output_section,
1863 needs_special_offset_handling,
1864 view,
1865 address,
1866 view_size);
1869 // Return the size of a relocation while scanning during a relocatable
1870 // link.
1872 template<int size, bool big_endian>
1873 unsigned int
1874 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1875 unsigned int,
1876 Relobj*)
1878 // We are always SHT_RELA, so we should never get here.
1879 gold_unreachable();
1880 return 0;
1883 // Scan the relocs during a relocatable link.
1885 template<int size, bool big_endian>
1886 void
1887 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1888 const General_options& options,
1889 Symbol_table* symtab,
1890 Layout* layout,
1891 Sized_relobj<size, big_endian>* object,
1892 unsigned int data_shndx,
1893 unsigned int sh_type,
1894 const unsigned char* prelocs,
1895 size_t reloc_count,
1896 Output_section* output_section,
1897 bool needs_special_offset_handling,
1898 size_t local_symbol_count,
1899 const unsigned char* plocal_symbols,
1900 Relocatable_relocs* rr)
1902 gold_assert(sh_type == elfcpp::SHT_RELA);
1904 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1905 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1907 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1908 Scan_relocatable_relocs>(
1909 options,
1910 symtab,
1911 layout,
1912 object,
1913 data_shndx,
1914 prelocs,
1915 reloc_count,
1916 output_section,
1917 needs_special_offset_handling,
1918 local_symbol_count,
1919 plocal_symbols,
1920 rr);
1923 // Relocate a section during a relocatable link.
1925 template<int size, bool big_endian>
1926 void
1927 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1928 const Relocate_info<size, big_endian>* relinfo,
1929 unsigned int sh_type,
1930 const unsigned char* prelocs,
1931 size_t reloc_count,
1932 Output_section* output_section,
1933 off_t offset_in_output_section,
1934 const Relocatable_relocs* rr,
1935 unsigned char* view,
1936 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1937 section_size_type view_size,
1938 unsigned char* reloc_view,
1939 section_size_type reloc_view_size)
1941 gold_assert(sh_type == elfcpp::SHT_RELA);
1943 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1944 relinfo,
1945 prelocs,
1946 reloc_count,
1947 output_section,
1948 offset_in_output_section,
1950 view,
1951 view_address,
1952 view_size,
1953 reloc_view,
1954 reloc_view_size);
1957 // Return the value to use for a dynamic which requires special
1958 // treatment. This is how we support equality comparisons of function
1959 // pointers across shared library boundaries, as described in the
1960 // processor specific ABI supplement.
1962 template<int size, bool big_endian>
1963 uint64_t
1964 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1966 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1967 return this->plt_section()->address() + gsym->plt_offset();
1970 // The selector for powerpc object files.
1972 template<int size, bool big_endian>
1973 class Target_selector_powerpc : public Target_selector
1975 public:
1976 Target_selector_powerpc()
1977 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1978 (size == 64 ?
1979 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1980 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1983 Target* instantiated_target_;
1985 Target* do_recognize(int machine, int, int)
1987 switch (size)
1989 case 64:
1990 if (machine != elfcpp::EM_PPC64)
1991 return NULL;
1992 break;
1994 case 32:
1995 if (machine != elfcpp::EM_PPC)
1996 return NULL;
1997 break;
1999 default:
2000 return NULL;
2003 return do_instantiate_target();
2006 Target* do_instantiate_target()
2008 if (this->instantiated_target_ == NULL)
2009 this->instantiated_target_ = new Target_powerpc<size, big_endian>();
2010 return this->instantiated_target_;
2014 Target_selector_powerpc<32, true> target_selector_ppc32;
2015 Target_selector_powerpc<32, false> target_selector_ppc32le;
2016 Target_selector_powerpc<64, true> target_selector_ppc64;
2017 Target_selector_powerpc<64, false> target_selector_ppc64le;
2019 } // End anonymous namespace.