bfd/
[binutils.git] / gold / powerpc.cc
bloba940fd853fd422fa934871f59dba356e98e3c9cc
1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
24 #include "gold.h"
26 #include "elfcpp.h"
27 #include "parameters.h"
28 #include "reloc.h"
29 #include "powerpc.h"
30 #include "object.h"
31 #include "symtab.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "copy-relocs.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39 #include "errors.h"
41 namespace
44 using namespace gold;
46 template<int size, bool big_endian>
47 class Output_data_plt_powerpc;
49 template<int size, bool big_endian>
50 class Target_powerpc : public Sized_target<size, big_endian>
52 public:
53 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
55 Target_powerpc()
56 : Sized_target<size, big_endian>(&powerpc_info),
57 got_(NULL), got2_(NULL), toc_(NULL),
58 plt_(NULL), rela_dyn_(NULL),
59 copy_relocs_(elfcpp::R_POWERPC_COPY),
60 dynbss_(NULL), got_mod_index_offset_(-1U)
64 // Process the relocations to determine unreferenced sections for
65 // garbage collection.
66 void
67 gc_process_relocs(const General_options& options,
68 Symbol_table* symtab,
69 Layout* layout,
70 Sized_relobj<size, big_endian>* object,
71 unsigned int data_shndx,
72 unsigned int sh_type,
73 const unsigned char* prelocs,
74 size_t reloc_count,
75 Output_section* output_section,
76 bool needs_special_offset_handling,
77 size_t local_symbol_count,
78 const unsigned char* plocal_symbols);
80 // Scan the relocations to look for symbol adjustments.
81 void
82 scan_relocs(const General_options& options,
83 Symbol_table* symtab,
84 Layout* layout,
85 Sized_relobj<size, big_endian>* object,
86 unsigned int data_shndx,
87 unsigned int sh_type,
88 const unsigned char* prelocs,
89 size_t reloc_count,
90 Output_section* output_section,
91 bool needs_special_offset_handling,
92 size_t local_symbol_count,
93 const unsigned char* plocal_symbols);
94 // Finalize the sections.
95 void
96 do_finalize_sections(Layout*);
98 // Return the value to use for a dynamic which requires special
99 // treatment.
100 uint64_t
101 do_dynsym_value(const Symbol*) const;
103 // Relocate a section.
104 void
105 relocate_section(const Relocate_info<size, big_endian>*,
106 unsigned int sh_type,
107 const unsigned char* prelocs,
108 size_t reloc_count,
109 Output_section* output_section,
110 bool needs_special_offset_handling,
111 unsigned char* view,
112 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
113 section_size_type view_size);
115 // Scan the relocs during a relocatable link.
116 void
117 scan_relocatable_relocs(const General_options& options,
118 Symbol_table* symtab,
119 Layout* layout,
120 Sized_relobj<size, big_endian>* object,
121 unsigned int data_shndx,
122 unsigned int sh_type,
123 const unsigned char* prelocs,
124 size_t reloc_count,
125 Output_section* output_section,
126 bool needs_special_offset_handling,
127 size_t local_symbol_count,
128 const unsigned char* plocal_symbols,
129 Relocatable_relocs*);
131 // Relocate a section during a relocatable link.
132 void
133 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
134 unsigned int sh_type,
135 const unsigned char* prelocs,
136 size_t reloc_count,
137 Output_section* output_section,
138 off_t offset_in_output_section,
139 const Relocatable_relocs*,
140 unsigned char* view,
141 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
142 section_size_type view_size,
143 unsigned char* reloc_view,
144 section_size_type reloc_view_size);
146 // Return whether SYM is defined by the ABI.
147 bool
148 do_is_defined_by_abi(const Symbol* sym) const
150 return strcmp(sym->name(), "___tls_get_addr") == 0;
153 // Return the size of the GOT section.
154 section_size_type
155 got_size()
157 gold_assert(this->got_ != NULL);
158 return this->got_->data_size();
161 private:
163 // The class which scans relocations.
164 class Scan
166 public:
167 Scan()
168 : issued_non_pic_error_(false)
171 inline void
172 local(const General_options& options, Symbol_table* symtab,
173 Layout* layout, Target_powerpc* target,
174 Sized_relobj<size, big_endian>* object,
175 unsigned int data_shndx,
176 Output_section* output_section,
177 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
178 const elfcpp::Sym<size, big_endian>& lsym);
180 inline void
181 global(const General_options& options, Symbol_table* symtab,
182 Layout* layout, Target_powerpc* target,
183 Sized_relobj<size, big_endian>* object,
184 unsigned int data_shndx,
185 Output_section* output_section,
186 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
187 Symbol* gsym);
189 private:
190 static void
191 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
192 unsigned int r_type);
194 static void
195 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
196 unsigned int r_type, Symbol*);
198 static void
199 generate_tls_call(Symbol_table* symtab, Layout* layout,
200 Target_powerpc* target);
202 void
203 check_non_pic(Relobj*, unsigned int r_type);
205 // Whether we have issued an error about a non-PIC compilation.
206 bool issued_non_pic_error_;
209 // The class which implements relocation.
210 class Relocate
212 public:
213 // Do a relocation. Return false if the caller should not issue
214 // any warnings about this relocation.
215 inline bool
216 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
217 Output_section*, size_t relnum,
218 const elfcpp::Rela<size, big_endian>&,
219 unsigned int r_type, const Sized_symbol<size>*,
220 const Symbol_value<size>*,
221 unsigned char*,
222 typename elfcpp::Elf_types<size>::Elf_Addr,
223 section_size_type);
225 private:
226 // Do a TLS relocation.
227 inline void
228 relocate_tls(const Relocate_info<size, big_endian>*,
229 Target_powerpc* target,
230 size_t relnum, const elfcpp::Rela<size, big_endian>&,
231 unsigned int r_type, const Sized_symbol<size>*,
232 const Symbol_value<size>*,
233 unsigned char*,
234 typename elfcpp::Elf_types<size>::Elf_Addr,
235 section_size_type);
238 // A class which returns the size required for a relocation type,
239 // used while scanning relocs during a relocatable link.
240 class Relocatable_size_for_reloc
242 public:
243 unsigned int
244 get_size_for_reloc(unsigned int, Relobj*);
247 // Get the GOT section, creating it if necessary.
248 Output_data_got<size, big_endian>*
249 got_section(Symbol_table*, Layout*);
251 Output_data_space*
252 got2_section() const
254 gold_assert (this->got2_ != NULL);
255 return this->got2_;
258 // Get the TOC section.
259 Output_data_space*
260 toc_section() const
262 gold_assert (this->toc_ != NULL);
263 return this->toc_;
266 // Create a PLT entry for a global symbol.
267 void
268 make_plt_entry(Symbol_table*, Layout*, Symbol*);
270 // Create a GOT entry for the TLS module index.
271 unsigned int
272 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
273 Sized_relobj<size, big_endian>* object);
275 // Get the PLT section.
276 const Output_data_plt_powerpc<size, big_endian>*
277 plt_section() const
279 gold_assert(this->plt_ != NULL);
280 return this->plt_;
283 // Get the dynamic reloc section, creating it if necessary.
284 Reloc_section*
285 rela_dyn_section(Layout*);
287 // Return true if the symbol may need a COPY relocation.
288 // References from an executable object to non-function symbols
289 // defined in a dynamic object may need a COPY relocation.
290 bool
291 may_need_copy_reloc(Symbol* gsym)
293 return (!parameters->options().shared()
294 && gsym->is_from_dynobj()
295 && gsym->type() != elfcpp::STT_FUNC);
298 // Copy a relocation against a global symbol.
299 void
300 copy_reloc(Symbol_table* symtab, Layout* layout,
301 Sized_relobj<size, big_endian>* object,
302 unsigned int shndx, Output_section* output_section,
303 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
305 this->copy_relocs_.copy_reloc(symtab, layout,
306 symtab->get_sized_symbol<size>(sym),
307 object, shndx, output_section,
308 reloc, this->rela_dyn_section(layout));
311 // Information about this specific target which we pass to the
312 // general Target structure.
313 static Target::Target_info powerpc_info;
315 // The types of GOT entries needed for this platform.
316 enum Got_type
318 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
319 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
320 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
323 // The GOT section.
324 Output_data_got<size, big_endian>* got_;
325 // The GOT2 section.
326 Output_data_space* got2_;
327 // The TOC section.
328 Output_data_space* toc_;
329 // The PLT section.
330 Output_data_plt_powerpc<size, big_endian>* plt_;
331 // The dynamic reloc section.
332 Reloc_section* rela_dyn_;
333 // Relocs saved to avoid a COPY reloc.
334 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
335 // Space for variables copied with a COPY reloc.
336 Output_data_space* dynbss_;
337 // Offset of the GOT entry for the TLS module index;
338 unsigned int got_mod_index_offset_;
341 template<>
342 Target::Target_info Target_powerpc<32, true>::powerpc_info =
344 32, // size
345 true, // is_big_endian
346 elfcpp::EM_PPC, // machine_code
347 false, // has_make_symbol
348 false, // has_resolve
349 false, // has_code_fill
350 true, // is_default_stack_executable
351 '\0', // wrap_char
352 "/usr/lib/ld.so.1", // dynamic_linker
353 0x10000000, // default_text_segment_address
354 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
355 4 * 1024, // common_pagesize (overridable by -z common-page-size)
356 elfcpp::SHN_UNDEF, // small_common_shndx
357 elfcpp::SHN_UNDEF, // large_common_shndx
358 0, // small_common_section_flags
359 0 // large_common_section_flags
362 template<>
363 Target::Target_info Target_powerpc<32, false>::powerpc_info =
365 32, // size
366 false, // is_big_endian
367 elfcpp::EM_PPC, // machine_code
368 false, // has_make_symbol
369 false, // has_resolve
370 false, // has_code_fill
371 true, // is_default_stack_executable
372 '\0', // wrap_char
373 "/usr/lib/ld.so.1", // dynamic_linker
374 0x10000000, // default_text_segment_address
375 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
376 4 * 1024, // common_pagesize (overridable by -z common-page-size)
377 elfcpp::SHN_UNDEF, // small_common_shndx
378 elfcpp::SHN_UNDEF, // large_common_shndx
379 0, // small_common_section_flags
380 0 // large_common_section_flags
383 template<>
384 Target::Target_info Target_powerpc<64, true>::powerpc_info =
386 64, // size
387 true, // is_big_endian
388 elfcpp::EM_PPC64, // machine_code
389 false, // has_make_symbol
390 false, // has_resolve
391 false, // has_code_fill
392 true, // is_default_stack_executable
393 '\0', // wrap_char
394 "/usr/lib/ld.so.1", // dynamic_linker
395 0x10000000, // default_text_segment_address
396 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
397 8 * 1024, // common_pagesize (overridable by -z common-page-size)
398 elfcpp::SHN_UNDEF, // small_common_shndx
399 elfcpp::SHN_UNDEF, // large_common_shndx
400 0, // small_common_section_flags
401 0 // large_common_section_flags
404 template<>
405 Target::Target_info Target_powerpc<64, false>::powerpc_info =
407 64, // size
408 false, // is_big_endian
409 elfcpp::EM_PPC64, // machine_code
410 false, // has_make_symbol
411 false, // has_resolve
412 false, // has_code_fill
413 true, // is_default_stack_executable
414 '\0', // wrap_char
415 "/usr/lib/ld.so.1", // dynamic_linker
416 0x10000000, // default_text_segment_address
417 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
418 8 * 1024, // common_pagesize (overridable by -z common-page-size)
419 elfcpp::SHN_UNDEF, // small_common_shndx
420 elfcpp::SHN_UNDEF, // large_common_shndx
421 0, // small_common_section_flags
422 0 // large_common_section_flags
425 template<int size, bool big_endian>
426 class Powerpc_relocate_functions
428 private:
429 // Do a simple relocation with the addend in the relocation.
430 template<int valsize>
431 static inline void
432 rela(unsigned char* view,
433 unsigned int right_shift,
434 elfcpp::Elf_Xword dst_mask,
435 typename elfcpp::Swap<size, big_endian>::Valtype value,
436 typename elfcpp::Swap<size, big_endian>::Valtype addend)
438 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
439 Valtype* wv = reinterpret_cast<Valtype*>(view);
440 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
441 Valtype reloc = ((value + addend) >> right_shift);
443 val &= ~dst_mask;
444 reloc &= dst_mask;
446 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
449 // Do a simple relocation using a symbol value with the addend in
450 // the relocation.
451 template<int valsize>
452 static inline void
453 rela(unsigned char* view,
454 unsigned int right_shift,
455 elfcpp::Elf_Xword dst_mask,
456 const Sized_relobj<size, big_endian>* object,
457 const Symbol_value<size>* psymval,
458 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
460 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
461 Valtype* wv = reinterpret_cast<Valtype*>(view);
462 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
463 Valtype reloc = (psymval->value(object, addend) >> right_shift);
465 val &= ~dst_mask;
466 reloc &= dst_mask;
468 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
471 // Do a simple relocation using a symbol value with the addend in
472 // the relocation, unaligned.
473 template<int valsize>
474 static inline void
475 rela_ua(unsigned char* view, unsigned int right_shift,
476 elfcpp::Elf_Xword dst_mask,
477 const Sized_relobj<size, big_endian>* object,
478 const Symbol_value<size>* psymval,
479 typename elfcpp::Swap<size, big_endian>::Valtype addend)
481 typedef typename elfcpp::Swap_unaligned<valsize,
482 big_endian>::Valtype Valtype;
483 unsigned char* wv = view;
484 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
485 Valtype reloc = (psymval->value(object, addend) >> right_shift);
487 val &= ~dst_mask;
488 reloc &= dst_mask;
490 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
493 // Do a simple PC relative relocation with a Symbol_value with the
494 // addend in the relocation.
495 template<int valsize>
496 static inline void
497 pcrela(unsigned char* view, unsigned int right_shift,
498 elfcpp::Elf_Xword dst_mask,
499 const Sized_relobj<size, big_endian>* object,
500 const Symbol_value<size>* psymval,
501 typename elfcpp::Swap<size, big_endian>::Valtype addend,
502 typename elfcpp::Elf_types<size>::Elf_Addr address)
504 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
505 Valtype* wv = reinterpret_cast<Valtype*>(view);
506 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
507 Valtype reloc = ((psymval->value(object, addend) - address)
508 >> right_shift);
510 val &= ~dst_mask;
511 reloc &= dst_mask;
513 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
516 template<int valsize>
517 static inline void
518 pcrela_unaligned(unsigned char* view,
519 const Sized_relobj<size, big_endian>* object,
520 const Symbol_value<size>* psymval,
521 typename elfcpp::Swap<size, big_endian>::Valtype addend,
522 typename elfcpp::Elf_types<size>::Elf_Addr address)
524 typedef typename elfcpp::Swap_unaligned<valsize,
525 big_endian>::Valtype Valtype;
526 unsigned char* wv = view;
527 Valtype reloc = (psymval->value(object, addend) - address);
529 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
532 typedef Powerpc_relocate_functions<size, big_endian> This;
533 typedef Relocate_functions<size, big_endian> This_reloc;
534 public:
535 // R_POWERPC_REL32: (Symbol + Addend - Address)
536 static inline void
537 rel32(unsigned char* view,
538 const Sized_relobj<size, big_endian>* object,
539 const Symbol_value<size>* psymval,
540 typename elfcpp::Elf_types<size>::Elf_Addr addend,
541 typename elfcpp::Elf_types<size>::Elf_Addr address)
542 { This_reloc::pcrela32(view, object, psymval, addend, address); }
544 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
545 static inline void
546 rel24(unsigned char* view,
547 const Sized_relobj<size, big_endian>* object,
548 const Symbol_value<size>* psymval,
549 typename elfcpp::Elf_types<size>::Elf_Addr addend,
550 typename elfcpp::Elf_types<size>::Elf_Addr address)
552 This::template pcrela<32>(view, 0, 0x03fffffc, object,
553 psymval, addend, address);
556 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
557 static inline void
558 rel14(unsigned char* view,
559 const Sized_relobj<size, big_endian>* object,
560 const Symbol_value<size>* psymval,
561 typename elfcpp::Elf_types<size>::Elf_Addr addend,
562 typename elfcpp::Elf_types<size>::Elf_Addr address)
564 This::template pcrela<32>(view, 0, 0x0000fffc, object,
565 psymval, addend, address);
568 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
569 static inline void
570 addr16(unsigned char* view,
571 typename elfcpp::Elf_types<size>::Elf_Addr value,
572 typename elfcpp::Elf_types<size>::Elf_Addr addend)
573 { This_reloc::rela16(view, value, addend); }
575 static inline void
576 addr16(unsigned char* view,
577 const Sized_relobj<size, big_endian>* object,
578 const Symbol_value<size>* psymval,
579 typename elfcpp::Elf_types<size>::Elf_Addr addend)
580 { This_reloc::rela16(view, object, psymval, addend); }
582 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
583 static inline void
584 addr16_ds(unsigned char* view,
585 typename elfcpp::Elf_types<size>::Elf_Addr value,
586 typename elfcpp::Elf_types<size>::Elf_Addr addend)
588 This::template rela<16>(view, 0, 0xfffc, value, addend);
591 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
592 static inline void
593 addr16_lo(unsigned char* view,
594 typename elfcpp::Elf_types<size>::Elf_Addr value,
595 typename elfcpp::Elf_types<size>::Elf_Addr addend)
596 { This_reloc::rela16(view, value, addend); }
598 static inline void
599 addr16_lo(unsigned char* view,
600 const Sized_relobj<size, big_endian>* object,
601 const Symbol_value<size>* psymval,
602 typename elfcpp::Elf_types<size>::Elf_Addr addend)
603 { This_reloc::rela16(view, object, psymval, addend); }
605 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
606 static inline void
607 addr16_hi(unsigned char* view,
608 typename elfcpp::Elf_types<size>::Elf_Addr value,
609 typename elfcpp::Elf_types<size>::Elf_Addr addend)
611 This::template rela<16>(view, 16, 0xffff, value, addend);
614 static inline void
615 addr16_hi(unsigned char* view,
616 const Sized_relobj<size, big_endian>* object,
617 const Symbol_value<size>* psymval,
618 typename elfcpp::Elf_types<size>::Elf_Addr addend)
620 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
623 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
624 // final value of the low 16 bits of the
625 // relocation is negative, add one.
626 static inline void
627 addr16_ha(unsigned char* view,
628 typename elfcpp::Elf_types<size>::Elf_Addr value,
629 typename elfcpp::Elf_types<size>::Elf_Addr addend)
631 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
633 reloc = value + addend;
635 if (reloc & 0x8000)
636 reloc += 0x10000;
637 reloc >>= 16;
639 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
642 static inline void
643 addr16_ha(unsigned char* view,
644 const Sized_relobj<size, big_endian>* object,
645 const Symbol_value<size>* psymval,
646 typename elfcpp::Elf_types<size>::Elf_Addr addend)
648 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
650 reloc = psymval->value(object, addend);
652 if (reloc & 0x8000)
653 reloc += 0x10000;
654 reloc >>= 16;
656 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
659 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
660 static inline void
661 rel16(unsigned char* view,
662 const Sized_relobj<size, big_endian>* object,
663 const Symbol_value<size>* psymval,
664 typename elfcpp::Elf_types<size>::Elf_Addr addend,
665 typename elfcpp::Elf_types<size>::Elf_Addr address)
666 { This_reloc::pcrela16(view, object, psymval, addend, address); }
668 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
669 static inline void
670 rel16_lo(unsigned char* view,
671 const Sized_relobj<size, big_endian>* object,
672 const Symbol_value<size>* psymval,
673 typename elfcpp::Elf_types<size>::Elf_Addr addend,
674 typename elfcpp::Elf_types<size>::Elf_Addr address)
675 { This_reloc::pcrela16(view, object, psymval, addend, address); }
677 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
678 static inline void
679 rel16_hi(unsigned char* view,
680 const Sized_relobj<size, big_endian>* object,
681 const Symbol_value<size>* psymval,
682 typename elfcpp::Elf_types<size>::Elf_Addr addend,
683 typename elfcpp::Elf_types<size>::Elf_Addr address)
685 This::template pcrela<16>(view, 16, 0xffff, object,
686 psymval, addend, address);
689 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
690 // final value of the low 16 bits of the
691 // relocation is negative, add one.
692 static inline void
693 rel16_ha(unsigned char* view,
694 const Sized_relobj<size, big_endian>* object,
695 const Symbol_value<size>* psymval,
696 typename elfcpp::Elf_types<size>::Elf_Addr addend,
697 typename elfcpp::Elf_types<size>::Elf_Addr address)
699 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
701 reloc = (psymval->value(object, addend) - address);
702 if (reloc & 0x8000)
703 reloc += 0x10000;
704 reloc >>= 16;
706 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
710 // Get the GOT section, creating it if necessary.
712 template<int size, bool big_endian>
713 Output_data_got<size, big_endian>*
714 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
715 Layout* layout)
717 if (this->got_ == NULL)
719 gold_assert(symtab != NULL && layout != NULL);
721 this->got_ = new Output_data_got<size, big_endian>();
723 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
724 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
725 this->got_);
727 // Create the GOT2 or TOC in the .got section.
728 if (size == 32)
730 this->got2_ = new Output_data_space(4, "** GOT2");
731 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
732 elfcpp::SHF_ALLOC
733 | elfcpp::SHF_WRITE,
734 this->got2_);
736 else
738 this->toc_ = new Output_data_space(8, "** TOC");
739 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
740 elfcpp::SHF_ALLOC
741 | elfcpp::SHF_WRITE,
742 this->toc_);
745 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
746 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
747 this->got_,
748 0, 0, elfcpp::STT_OBJECT,
749 elfcpp::STB_LOCAL,
750 elfcpp::STV_HIDDEN, 0,
751 false, false);
754 return this->got_;
757 // Get the dynamic reloc section, creating it if necessary.
759 template<int size, bool big_endian>
760 typename Target_powerpc<size, big_endian>::Reloc_section*
761 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
763 if (this->rela_dyn_ == NULL)
765 gold_assert(layout != NULL);
766 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
767 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
768 elfcpp::SHF_ALLOC, this->rela_dyn_);
770 return this->rela_dyn_;
773 // A class to handle the PLT data.
775 template<int size, bool big_endian>
776 class Output_data_plt_powerpc : public Output_section_data
778 public:
779 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
780 size, big_endian> Reloc_section;
782 Output_data_plt_powerpc(Layout*);
784 // Add an entry to the PLT.
785 void add_entry(Symbol* gsym);
787 // Return the .rela.plt section data.
788 const Reloc_section* rel_plt() const
790 return this->rel_;
793 protected:
794 void do_adjust_output_section(Output_section* os);
796 private:
797 // The size of an entry in the PLT.
798 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
800 // Set the final size.
801 void
802 set_final_data_size()
804 unsigned int full_count = this->count_ + 4;
806 this->set_data_size(full_count * base_plt_entry_size);
809 // Write out the PLT data.
810 void
811 do_write(Output_file*);
813 // The reloc section.
814 Reloc_section* rel_;
815 // The number of PLT entries.
816 unsigned int count_;
819 // Create the PLT section. The ordinary .got section is an argument,
820 // since we need to refer to the start.
822 template<int size, bool big_endian>
823 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
824 : Output_section_data(size == 32 ? 4 : 8), count_(0)
826 this->rel_ = new Reloc_section(false);
827 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
828 elfcpp::SHF_ALLOC, this->rel_);
831 template<int size, bool big_endian>
832 void
833 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
835 os->set_entsize(0);
838 // Add an entry to the PLT.
840 template<int size, bool big_endian>
841 void
842 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
844 gold_assert(!gsym->has_plt_offset());
845 unsigned int index = this->count_+ + 4;
846 section_offset_type plt_offset;
848 if (index < 8192)
849 plt_offset = index * base_plt_entry_size;
850 else
851 gold_unreachable();
853 gsym->set_plt_offset(plt_offset);
855 ++this->count_;
857 gsym->set_needs_dynsym_entry();
858 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
859 plt_offset, 0);
862 static const unsigned int addis_11_11 = 0x3d6b0000;
863 static const unsigned int addis_11_30 = 0x3d7e0000;
864 static const unsigned int addis_12_12 = 0x3d8c0000;
865 static const unsigned int addi_11_11 = 0x396b0000;
866 static const unsigned int add_0_11_11 = 0x7c0b5a14;
867 static const unsigned int add_11_0_11 = 0x7d605a14;
868 static const unsigned int b = 0x48000000;
869 static const unsigned int bcl_20_31 = 0x429f0005;
870 static const unsigned int bctr = 0x4e800420;
871 static const unsigned int lis_11 = 0x3d600000;
872 static const unsigned int lis_12 = 0x3d800000;
873 static const unsigned int lwzu_0_12 = 0x840c0000;
874 static const unsigned int lwz_0_12 = 0x800c0000;
875 static const unsigned int lwz_11_11 = 0x816b0000;
876 static const unsigned int lwz_11_30 = 0x817e0000;
877 static const unsigned int lwz_12_12 = 0x818c0000;
878 static const unsigned int mflr_0 = 0x7c0802a6;
879 static const unsigned int mflr_12 = 0x7d8802a6;
880 static const unsigned int mtctr_0 = 0x7c0903a6;
881 static const unsigned int mtctr_11 = 0x7d6903a6;
882 static const unsigned int mtlr_0 = 0x7c0803a6;
883 static const unsigned int nop = 0x60000000;
884 static const unsigned int sub_11_11_12 = 0x7d6c5850;
886 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
887 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
888 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
889 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
890 /* ld %r11,xxx+16@l(%r12) */
893 // Write out the PLT.
895 template<int size, bool big_endian>
896 void
897 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
899 const off_t offset = this->offset();
900 const section_size_type oview_size =
901 convert_to_section_size_type(this->data_size());
902 unsigned char* const oview = of->get_output_view(offset, oview_size);
903 unsigned char* pov = oview;
905 memset(pov, 0, base_plt_entry_size * 4);
906 pov += base_plt_entry_size * 4;
908 unsigned int plt_offset = base_plt_entry_size * 4;
909 const unsigned int count = this->count_;
911 if (size == 64)
913 for (unsigned int i = 0; i < count; i++)
917 else
919 for (unsigned int i = 0; i < count; i++)
921 elfcpp::Swap<32, true>::writeval(pov + 0x00,
922 lwz_11_30 + plt_offset);
923 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
924 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
925 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
926 pov += base_plt_entry_size;
927 plt_offset += base_plt_entry_size;
931 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
933 of->write_output_view(offset, oview_size, oview);
936 // Create a PLT entry for a global symbol.
938 template<int size, bool big_endian>
939 void
940 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
941 Layout* layout,
942 Symbol* gsym)
944 if (gsym->has_plt_offset())
945 return;
947 if (this->plt_ == NULL)
949 // Create the GOT section first.
950 this->got_section(symtab, layout);
952 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
953 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
954 (elfcpp::SHF_ALLOC
955 | elfcpp::SHF_EXECINSTR
956 | elfcpp::SHF_WRITE),
957 this->plt_);
959 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
960 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
961 this->plt_,
962 0, 0, elfcpp::STT_OBJECT,
963 elfcpp::STB_LOCAL,
964 elfcpp::STV_HIDDEN, 0,
965 false, false);
968 this->plt_->add_entry(gsym);
971 // Create a GOT entry for the TLS module index.
973 template<int size, bool big_endian>
974 unsigned int
975 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
976 Layout* layout,
977 Sized_relobj<size, big_endian>* object)
979 if (this->got_mod_index_offset_ == -1U)
981 gold_assert(symtab != NULL && layout != NULL && object != NULL);
982 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
983 Output_data_got<size, big_endian>* got;
984 unsigned int got_offset;
986 got = this->got_section(symtab, layout);
987 got_offset = got->add_constant(0);
988 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
989 got_offset, 0);
990 got->add_constant(0);
991 this->got_mod_index_offset_ = got_offset;
993 return this->got_mod_index_offset_;
996 // Optimize the TLS relocation type based on what we know about the
997 // symbol. IS_FINAL is true if the final address of this symbol is
998 // known at link time.
1000 static tls::Tls_optimization
1001 optimize_tls_reloc(bool /* is_final */, int r_type)
1003 // If we are generating a shared library, then we can't do anything
1004 // in the linker.
1005 if (parameters->options().shared())
1006 return tls::TLSOPT_NONE;
1007 switch (r_type)
1009 // XXX
1010 default:
1011 gold_unreachable();
1015 // Report an unsupported relocation against a local symbol.
1017 template<int size, bool big_endian>
1018 void
1019 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1020 Sized_relobj<size, big_endian>* object,
1021 unsigned int r_type)
1023 gold_error(_("%s: unsupported reloc %u against local symbol"),
1024 object->name().c_str(), r_type);
1027 // We are about to emit a dynamic relocation of type R_TYPE. If the
1028 // dynamic linker does not support it, issue an error.
1030 template<int size, bool big_endian>
1031 void
1032 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1033 unsigned int r_type)
1035 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1037 // These are the relocation types supported by glibc for both 32-bit
1038 // and 64-bit powerpc.
1039 switch (r_type)
1041 case elfcpp::R_POWERPC_RELATIVE:
1042 case elfcpp::R_POWERPC_GLOB_DAT:
1043 case elfcpp::R_POWERPC_DTPMOD:
1044 case elfcpp::R_POWERPC_DTPREL:
1045 case elfcpp::R_POWERPC_TPREL:
1046 case elfcpp::R_POWERPC_JMP_SLOT:
1047 case elfcpp::R_POWERPC_COPY:
1048 case elfcpp::R_POWERPC_ADDR32:
1049 case elfcpp::R_POWERPC_ADDR24:
1050 case elfcpp::R_POWERPC_REL24:
1051 return;
1053 default:
1054 break;
1057 if (size == 64)
1059 switch (r_type)
1061 // These are the relocation types supported only on 64-bit.
1062 case elfcpp::R_PPC64_ADDR64:
1063 case elfcpp::R_PPC64_TPREL16_LO_DS:
1064 case elfcpp::R_PPC64_TPREL16_DS:
1065 case elfcpp::R_POWERPC_TPREL16:
1066 case elfcpp::R_POWERPC_TPREL16_LO:
1067 case elfcpp::R_POWERPC_TPREL16_HI:
1068 case elfcpp::R_POWERPC_TPREL16_HA:
1069 case elfcpp::R_PPC64_TPREL16_HIGHER:
1070 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1071 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1072 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1073 case elfcpp::R_PPC64_ADDR16_LO_DS:
1074 case elfcpp::R_POWERPC_ADDR16_LO:
1075 case elfcpp::R_POWERPC_ADDR16_HI:
1076 case elfcpp::R_POWERPC_ADDR16_HA:
1077 case elfcpp::R_POWERPC_ADDR30:
1078 case elfcpp::R_PPC64_UADDR64:
1079 case elfcpp::R_POWERPC_UADDR32:
1080 case elfcpp::R_POWERPC_ADDR16:
1081 case elfcpp::R_POWERPC_UADDR16:
1082 case elfcpp::R_PPC64_ADDR16_DS:
1083 case elfcpp::R_PPC64_ADDR16_HIGHER:
1084 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1085 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1086 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1087 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1088 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1089 case elfcpp::R_POWERPC_REL32:
1090 case elfcpp::R_PPC64_REL64:
1091 return;
1093 default:
1094 break;
1097 else
1099 switch (r_type)
1101 // These are the relocation types supported only on 32-bit.
1103 default:
1104 break;
1108 // This prevents us from issuing more than one error per reloc
1109 // section. But we can still wind up issuing more than one
1110 // error per object file.
1111 if (this->issued_non_pic_error_)
1112 return;
1113 gold_assert(parameters->options().output_is_position_independent());
1114 object->error(_("requires unsupported dynamic reloc; "
1115 "recompile with -fPIC"));
1116 this->issued_non_pic_error_ = true;
1117 return;
1120 // Scan a relocation for a local symbol.
1122 template<int size, bool big_endian>
1123 inline void
1124 Target_powerpc<size, big_endian>::Scan::local(
1125 const General_options&,
1126 Symbol_table* symtab,
1127 Layout* layout,
1128 Target_powerpc<size, big_endian>* target,
1129 Sized_relobj<size, big_endian>* object,
1130 unsigned int data_shndx,
1131 Output_section* output_section,
1132 const elfcpp::Rela<size, big_endian>& reloc,
1133 unsigned int r_type,
1134 const elfcpp::Sym<size, big_endian>& lsym)
1136 switch (r_type)
1138 case elfcpp::R_POWERPC_NONE:
1139 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1140 case elfcpp::R_POWERPC_GNU_VTENTRY:
1141 break;
1143 case elfcpp::R_PPC64_ADDR64:
1144 case elfcpp::R_POWERPC_ADDR32:
1145 case elfcpp::R_POWERPC_ADDR16_HA:
1146 case elfcpp::R_POWERPC_ADDR16_LO:
1147 // If building a shared library (or a position-independent
1148 // executable), we need to create a dynamic relocation for
1149 // this location.
1150 if (parameters->options().output_is_position_independent())
1152 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1154 check_non_pic(object, r_type);
1155 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1157 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1158 rela_dyn->add_local(object, r_sym, r_type, output_section,
1159 data_shndx, reloc.get_r_offset(),
1160 reloc.get_r_addend());
1162 else
1164 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1165 gold_assert(lsym.get_st_value() == 0);
1166 rela_dyn->add_local_relative(object, r_sym, r_type,
1167 output_section, data_shndx,
1168 reloc.get_r_offset(),
1169 reloc.get_r_addend());
1172 break;
1174 case elfcpp::R_POWERPC_REL24:
1175 case elfcpp::R_PPC_LOCAL24PC:
1176 case elfcpp::R_POWERPC_REL32:
1177 case elfcpp::R_PPC_REL16_LO:
1178 case elfcpp::R_PPC_REL16_HA:
1179 break;
1181 case elfcpp::R_POWERPC_GOT16:
1182 case elfcpp::R_POWERPC_GOT16_LO:
1183 case elfcpp::R_POWERPC_GOT16_HI:
1184 case elfcpp::R_POWERPC_GOT16_HA:
1185 case elfcpp::R_PPC64_TOC16:
1186 case elfcpp::R_PPC64_TOC16_LO:
1187 case elfcpp::R_PPC64_TOC16_HI:
1188 case elfcpp::R_PPC64_TOC16_HA:
1189 case elfcpp::R_PPC64_TOC16_DS:
1190 case elfcpp::R_PPC64_TOC16_LO_DS:
1192 // The symbol requires a GOT entry.
1193 Output_data_got<size, big_endian>* got;
1194 unsigned int r_sym;
1196 got = target->got_section(symtab, layout);
1197 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1199 // If we are generating a shared object, we need to add a
1200 // dynamic relocation for this symbol's GOT entry.
1201 if (parameters->options().output_is_position_independent())
1203 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1205 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1206 unsigned int off;
1208 off = got->add_constant(0);
1209 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1210 rela_dyn->add_local_relative(object, r_sym,
1211 elfcpp::R_POWERPC_RELATIVE,
1212 got, off, 0);
1215 else
1216 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1218 break;
1220 case elfcpp::R_PPC64_TOC:
1221 // We need a GOT section.
1222 target->got_section(symtab, layout);
1223 break;
1225 // These are relocations which should only be seen by the
1226 // dynamic linker, and should never be seen here.
1227 case elfcpp::R_POWERPC_COPY:
1228 case elfcpp::R_POWERPC_GLOB_DAT:
1229 case elfcpp::R_POWERPC_JMP_SLOT:
1230 case elfcpp::R_POWERPC_RELATIVE:
1231 case elfcpp::R_POWERPC_DTPMOD:
1232 gold_error(_("%s: unexpected reloc %u in object file"),
1233 object->name().c_str(), r_type);
1234 break;
1236 default:
1237 unsupported_reloc_local(object, r_type);
1238 break;
1242 // Report an unsupported relocation against a global symbol.
1244 template<int size, bool big_endian>
1245 void
1246 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1247 Sized_relobj<size, big_endian>* object,
1248 unsigned int r_type,
1249 Symbol* gsym)
1251 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1252 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1255 // Scan a relocation for a global symbol.
1257 template<int size, bool big_endian>
1258 inline void
1259 Target_powerpc<size, big_endian>::Scan::global(
1260 const General_options&,
1261 Symbol_table* symtab,
1262 Layout* layout,
1263 Target_powerpc<size, big_endian>* target,
1264 Sized_relobj<size, big_endian>* object,
1265 unsigned int data_shndx,
1266 Output_section* output_section,
1267 const elfcpp::Rela<size, big_endian>& reloc,
1268 unsigned int r_type,
1269 Symbol* gsym)
1271 switch (r_type)
1273 case elfcpp::R_POWERPC_NONE:
1274 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1275 case elfcpp::R_POWERPC_GNU_VTENTRY:
1276 break;
1278 case elfcpp::R_PPC_PLTREL24:
1279 // If the symbol is fully resolved, this is just a PC32 reloc.
1280 // Otherwise we need a PLT entry.
1281 if (gsym->final_value_is_known())
1282 break;
1283 // If building a shared library, we can also skip the PLT entry
1284 // if the symbol is defined in the output file and is protected
1285 // or hidden.
1286 if (gsym->is_defined()
1287 && !gsym->is_from_dynobj()
1288 && !gsym->is_preemptible())
1289 break;
1290 target->make_plt_entry(symtab, layout, gsym);
1291 break;
1293 case elfcpp::R_POWERPC_ADDR16:
1294 case elfcpp::R_POWERPC_ADDR16_LO:
1295 case elfcpp::R_POWERPC_ADDR16_HI:
1296 case elfcpp::R_POWERPC_ADDR16_HA:
1297 case elfcpp::R_POWERPC_ADDR32:
1298 case elfcpp::R_PPC64_ADDR64:
1300 // Make a PLT entry if necessary.
1301 if (gsym->needs_plt_entry())
1303 target->make_plt_entry(symtab, layout, gsym);
1304 // Since this is not a PC-relative relocation, we may be
1305 // taking the address of a function. In that case we need to
1306 // set the entry in the dynamic symbol table to the address of
1307 // the PLT entry.
1308 if (gsym->is_from_dynobj() && !parameters->options().shared())
1309 gsym->set_needs_dynsym_value();
1311 // Make a dynamic relocation if necessary.
1312 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1314 if (target->may_need_copy_reloc(gsym))
1316 target->copy_reloc(symtab, layout, object,
1317 data_shndx, output_section, gsym, reloc);
1319 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1320 || r_type == elfcpp::R_PPC64_ADDR64)
1321 && gsym->can_use_relative_reloc(false))
1323 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1324 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1325 output_section, object,
1326 data_shndx, reloc.get_r_offset(),
1327 reloc.get_r_addend());
1329 else
1331 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1333 check_non_pic(object, r_type);
1334 if (gsym->is_from_dynobj()
1335 || gsym->is_undefined()
1336 || gsym->is_preemptible())
1337 rela_dyn->add_global(gsym, r_type, output_section,
1338 object, data_shndx,
1339 reloc.get_r_offset(),
1340 reloc.get_r_addend());
1341 else
1342 rela_dyn->add_global_relative(gsym, r_type,
1343 output_section, object,
1344 data_shndx,
1345 reloc.get_r_offset(),
1346 reloc.get_r_addend());
1350 break;
1352 case elfcpp::R_POWERPC_REL24:
1353 case elfcpp::R_PPC_LOCAL24PC:
1354 case elfcpp::R_PPC_REL16:
1355 case elfcpp::R_PPC_REL16_LO:
1356 case elfcpp::R_PPC_REL16_HI:
1357 case elfcpp::R_PPC_REL16_HA:
1359 if (gsym->needs_plt_entry())
1360 target->make_plt_entry(symtab, layout, gsym);
1361 // Make a dynamic relocation if necessary.
1362 int flags = Symbol::NON_PIC_REF;
1363 if (gsym->type() == elfcpp::STT_FUNC)
1364 flags |= Symbol::FUNCTION_CALL;
1365 if (gsym->needs_dynamic_reloc(flags))
1367 if (target->may_need_copy_reloc(gsym))
1369 target->copy_reloc(symtab, layout, object,
1370 data_shndx, output_section, gsym,
1371 reloc);
1373 else
1375 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1376 check_non_pic(object, r_type);
1377 rela_dyn->add_global(gsym, r_type, output_section, object,
1378 data_shndx, reloc.get_r_offset(),
1379 reloc.get_r_addend());
1383 break;
1385 case elfcpp::R_POWERPC_GOT16:
1386 case elfcpp::R_POWERPC_GOT16_LO:
1387 case elfcpp::R_POWERPC_GOT16_HI:
1388 case elfcpp::R_POWERPC_GOT16_HA:
1389 case elfcpp::R_PPC64_TOC16:
1390 case elfcpp::R_PPC64_TOC16_LO:
1391 case elfcpp::R_PPC64_TOC16_HI:
1392 case elfcpp::R_PPC64_TOC16_HA:
1393 case elfcpp::R_PPC64_TOC16_DS:
1394 case elfcpp::R_PPC64_TOC16_LO_DS:
1396 // The symbol requires a GOT entry.
1397 Output_data_got<size, big_endian>* got;
1399 got = target->got_section(symtab, layout);
1400 if (gsym->final_value_is_known())
1401 got->add_global(gsym, GOT_TYPE_STANDARD);
1402 else
1404 // If this symbol is not fully resolved, we need to add a
1405 // dynamic relocation for it.
1406 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1407 if (gsym->is_from_dynobj()
1408 || gsym->is_undefined()
1409 || gsym->is_preemptible())
1410 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1411 elfcpp::R_POWERPC_GLOB_DAT);
1412 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1414 unsigned int off = got->add_constant(0);
1416 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1417 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1418 got, off, 0);
1422 break;
1424 case elfcpp::R_PPC64_TOC:
1425 // We need a GOT section.
1426 target->got_section(symtab, layout);
1427 break;
1429 case elfcpp::R_POWERPC_GOT_TPREL16:
1430 case elfcpp::R_POWERPC_TLS:
1431 // XXX TLS
1432 break;
1434 // These are relocations which should only be seen by the
1435 // dynamic linker, and should never be seen here.
1436 case elfcpp::R_POWERPC_COPY:
1437 case elfcpp::R_POWERPC_GLOB_DAT:
1438 case elfcpp::R_POWERPC_JMP_SLOT:
1439 case elfcpp::R_POWERPC_RELATIVE:
1440 case elfcpp::R_POWERPC_DTPMOD:
1441 gold_error(_("%s: unexpected reloc %u in object file"),
1442 object->name().c_str(), r_type);
1443 break;
1445 default:
1446 unsupported_reloc_global(object, r_type, gsym);
1447 break;
1451 // Process relocations for gc.
1453 template<int size, bool big_endian>
1454 void
1455 Target_powerpc<size, big_endian>::gc_process_relocs(
1456 const General_options& options,
1457 Symbol_table* symtab,
1458 Layout* layout,
1459 Sized_relobj<size, big_endian>* object,
1460 unsigned int data_shndx,
1461 unsigned int,
1462 const unsigned char* prelocs,
1463 size_t reloc_count,
1464 Output_section* output_section,
1465 bool needs_special_offset_handling,
1466 size_t local_symbol_count,
1467 const unsigned char* plocal_symbols)
1469 typedef Target_powerpc<size, big_endian> Powerpc;
1470 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1472 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1473 options,
1474 symtab,
1475 layout,
1476 this,
1477 object,
1478 data_shndx,
1479 prelocs,
1480 reloc_count,
1481 output_section,
1482 needs_special_offset_handling,
1483 local_symbol_count,
1484 plocal_symbols);
1487 // Scan relocations for a section.
1489 template<int size, bool big_endian>
1490 void
1491 Target_powerpc<size, big_endian>::scan_relocs(
1492 const General_options& options,
1493 Symbol_table* symtab,
1494 Layout* layout,
1495 Sized_relobj<size, big_endian>* object,
1496 unsigned int data_shndx,
1497 unsigned int sh_type,
1498 const unsigned char* prelocs,
1499 size_t reloc_count,
1500 Output_section* output_section,
1501 bool needs_special_offset_handling,
1502 size_t local_symbol_count,
1503 const unsigned char* plocal_symbols)
1505 typedef Target_powerpc<size, big_endian> Powerpc;
1506 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1507 static Output_data_space* sdata;
1509 if (sh_type == elfcpp::SHT_REL)
1511 gold_error(_("%s: unsupported REL reloc section"),
1512 object->name().c_str());
1513 return;
1516 // Define _SDA_BASE_ at the start of the .sdata section.
1517 if (sdata == NULL)
1519 // layout->find_output_section(".sdata") == NULL
1520 sdata = new Output_data_space(4, "** sdata");
1521 Output_section* os = layout->add_output_section_data(".sdata", 0,
1522 elfcpp::SHF_ALLOC
1523 | elfcpp::SHF_WRITE,
1524 sdata);
1525 symtab->define_in_output_data("_SDA_BASE_", NULL,
1527 32768, 0,
1528 elfcpp::STT_OBJECT,
1529 elfcpp::STB_LOCAL,
1530 elfcpp::STV_HIDDEN, 0,
1531 false, false);
1534 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1535 options,
1536 symtab,
1537 layout,
1538 this,
1539 object,
1540 data_shndx,
1541 prelocs,
1542 reloc_count,
1543 output_section,
1544 needs_special_offset_handling,
1545 local_symbol_count,
1546 plocal_symbols);
1549 // Finalize the sections.
1551 template<int size, bool big_endian>
1552 void
1553 Target_powerpc<size, big_endian>::do_finalize_sections(Layout* layout)
1555 // Fill in some more dynamic tags.
1556 Output_data_dynamic* const odyn = layout->dynamic_data();
1557 if (odyn != NULL)
1559 if (this->plt_ != NULL)
1561 const Output_data* od = this->plt_->rel_plt();
1562 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1563 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1564 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1566 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1569 if (this->rela_dyn_ != NULL)
1571 const Output_data* od = this->rela_dyn_;
1572 odyn->add_section_address(elfcpp::DT_RELA, od);
1573 odyn->add_section_size(elfcpp::DT_RELASZ, od);
1574 odyn->add_constant(elfcpp::DT_RELAENT,
1575 elfcpp::Elf_sizes<size>::rela_size);
1578 if (!parameters->options().shared())
1580 // The value of the DT_DEBUG tag is filled in by the dynamic
1581 // linker at run time, and used by the debugger.
1582 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1586 // Emit any relocs we saved in an attempt to avoid generating COPY
1587 // relocs.
1588 if (this->copy_relocs_.any_saved_relocs())
1589 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1592 // Perform a relocation.
1594 template<int size, bool big_endian>
1595 inline bool
1596 Target_powerpc<size, big_endian>::Relocate::relocate(
1597 const Relocate_info<size, big_endian>* relinfo,
1598 Target_powerpc* target,
1599 Output_section*,
1600 size_t relnum,
1601 const elfcpp::Rela<size, big_endian>& rela,
1602 unsigned int r_type,
1603 const Sized_symbol<size>* gsym,
1604 const Symbol_value<size>* psymval,
1605 unsigned char* view,
1606 typename elfcpp::Elf_types<size>::Elf_Addr address,
1607 section_size_type /* view_size */)
1609 const unsigned int toc_base_offset = 0x8000;
1610 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1612 // Pick the value to use for symbols defined in shared objects.
1613 Symbol_value<size> symval;
1614 if (gsym != NULL
1615 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1616 || r_type == elfcpp::R_PPC_LOCAL24PC
1617 || r_type == elfcpp::R_PPC_REL16
1618 || r_type == elfcpp::R_PPC_REL16_LO
1619 || r_type == elfcpp::R_PPC_REL16_HI
1620 || r_type == elfcpp::R_PPC_REL16_HA))
1622 elfcpp::Elf_Xword value;
1624 value = target->plt_section()->address() + gsym->plt_offset();
1626 symval.set_output_value(value);
1628 psymval = &symval;
1631 const Sized_relobj<size, big_endian>* object = relinfo->object;
1632 elfcpp::Elf_Xword addend = rela.get_r_addend();
1634 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1635 // pointer points to the beginning, not the end, of the table.
1636 // So we just use the plain offset.
1637 bool have_got_offset = false;
1638 unsigned int got_offset = 0;
1639 unsigned int got2_offset = 0;
1640 switch (r_type)
1642 case elfcpp::R_PPC64_TOC16:
1643 case elfcpp::R_PPC64_TOC16_LO:
1644 case elfcpp::R_PPC64_TOC16_HI:
1645 case elfcpp::R_PPC64_TOC16_HA:
1646 case elfcpp::R_PPC64_TOC16_DS:
1647 case elfcpp::R_PPC64_TOC16_LO_DS:
1648 // Subtract the TOC base address.
1649 addend -= target->toc_section()->address() + toc_base_offset;
1650 /* FALLTHRU */
1652 case elfcpp::R_POWERPC_GOT16:
1653 case elfcpp::R_POWERPC_GOT16_LO:
1654 case elfcpp::R_POWERPC_GOT16_HI:
1655 case elfcpp::R_POWERPC_GOT16_HA:
1656 case elfcpp::R_PPC64_GOT16_DS:
1657 case elfcpp::R_PPC64_GOT16_LO_DS:
1658 if (gsym != NULL)
1660 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1661 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1663 else
1665 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1666 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1667 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1669 have_got_offset = true;
1670 break;
1672 // R_PPC_PLTREL24 is rather special. If non-zero,
1673 // the addend specifies the GOT pointer offset within .got2.
1674 case elfcpp::R_PPC_PLTREL24:
1675 if (addend >= 32768)
1677 Output_data_space* got2;
1678 got2 = target->got2_section();
1679 got2_offset = got2->offset();
1680 addend += got2_offset;
1682 have_got_offset = true;
1683 break;
1685 default:
1686 break;
1689 switch (r_type)
1691 case elfcpp::R_POWERPC_NONE:
1692 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1693 case elfcpp::R_POWERPC_GNU_VTENTRY:
1694 break;
1696 case elfcpp::R_POWERPC_REL32:
1697 Reloc::rel32(view, object, psymval, addend, address);
1698 break;
1700 case elfcpp::R_POWERPC_REL24:
1701 Reloc::rel24(view, object, psymval, addend, address);
1702 break;
1704 case elfcpp::R_POWERPC_REL14:
1705 Reloc::rel14(view, object, psymval, addend, address);
1706 break;
1708 case elfcpp::R_PPC_PLTREL24:
1709 Reloc::rel24(view, object, psymval, addend, address);
1710 break;
1712 case elfcpp::R_PPC_LOCAL24PC:
1713 Reloc::rel24(view, object, psymval, addend, address);
1714 break;
1716 case elfcpp::R_PPC64_ADDR64:
1717 if (!parameters->options().output_is_position_independent())
1718 Relocate_functions<size, big_endian>::rela64(view, object,
1719 psymval, addend);
1720 break;
1722 case elfcpp::R_POWERPC_ADDR32:
1723 if (!parameters->options().output_is_position_independent())
1724 Relocate_functions<size, big_endian>::rela32(view, object,
1725 psymval, addend);
1726 break;
1728 case elfcpp::R_POWERPC_ADDR16_LO:
1729 Reloc::addr16_lo(view, object, psymval, addend);
1730 break;
1732 case elfcpp::R_POWERPC_ADDR16_HI:
1733 Reloc::addr16_hi(view, object, psymval, addend);
1734 break;
1736 case elfcpp::R_POWERPC_ADDR16_HA:
1737 Reloc::addr16_ha(view, object, psymval, addend);
1738 break;
1740 case elfcpp::R_PPC_REL16_LO:
1741 Reloc::rel16_lo(view, object, psymval, addend, address);
1742 break;
1744 case elfcpp::R_PPC_REL16_HI:
1745 Reloc::rel16_lo(view, object, psymval, addend, address);
1746 break;
1748 case elfcpp::R_PPC_REL16_HA:
1749 Reloc::rel16_ha(view, object, psymval, addend, address);
1750 break;
1752 case elfcpp::R_POWERPC_GOT16:
1753 Reloc::addr16(view, got_offset, addend);
1754 break;
1756 case elfcpp::R_POWERPC_GOT16_LO:
1757 Reloc::addr16_lo(view, got_offset, addend);
1758 break;
1760 case elfcpp::R_POWERPC_GOT16_HI:
1761 Reloc::addr16_hi(view, got_offset, addend);
1762 break;
1764 case elfcpp::R_POWERPC_GOT16_HA:
1765 Reloc::addr16_ha(view, got_offset, addend);
1766 break;
1768 case elfcpp::R_PPC64_TOC16:
1769 Reloc::addr16(view, got_offset, addend);
1770 break;
1772 case elfcpp::R_PPC64_TOC16_LO:
1773 Reloc::addr16_lo(view, got_offset, addend);
1774 break;
1776 case elfcpp::R_PPC64_TOC16_HI:
1777 Reloc::addr16_hi(view, got_offset, addend);
1778 break;
1780 case elfcpp::R_PPC64_TOC16_HA:
1781 Reloc::addr16_ha(view, got_offset, addend);
1782 break;
1784 case elfcpp::R_PPC64_TOC16_DS:
1785 case elfcpp::R_PPC64_TOC16_LO_DS:
1786 Reloc::addr16_ds(view, got_offset, addend);
1787 break;
1789 case elfcpp::R_PPC64_TOC:
1791 elfcpp::Elf_types<64>::Elf_Addr value;
1792 value = target->toc_section()->address() + toc_base_offset;
1793 Relocate_functions<64, false>::rela64(view, value, addend);
1795 break;
1797 case elfcpp::R_POWERPC_COPY:
1798 case elfcpp::R_POWERPC_GLOB_DAT:
1799 case elfcpp::R_POWERPC_JMP_SLOT:
1800 case elfcpp::R_POWERPC_RELATIVE:
1801 // This is an outstanding tls reloc, which is unexpected when
1802 // linking.
1803 case elfcpp::R_POWERPC_DTPMOD:
1804 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1805 _("unexpected reloc %u in object file"),
1806 r_type);
1807 break;
1809 default:
1810 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1811 _("unsupported reloc %u"),
1812 r_type);
1813 break;
1816 return true;
1819 // Perform a TLS relocation.
1821 template<int size, bool big_endian>
1822 inline void
1823 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1824 const Relocate_info<size, big_endian>* relinfo,
1825 Target_powerpc<size, big_endian>* target,
1826 size_t relnum,
1827 const elfcpp::Rela<size, big_endian>& rela,
1828 unsigned int r_type,
1829 const Sized_symbol<size>* gsym,
1830 const Symbol_value<size>* psymval,
1831 unsigned char* view,
1832 typename elfcpp::Elf_types<size>::Elf_Addr address,
1833 section_size_type)
1835 Output_segment* tls_segment = relinfo->layout->tls_segment();
1836 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1837 const Sized_relobj<size, big_endian>* object = relinfo->object;
1839 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1840 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1842 const bool is_final =
1843 (gsym == NULL
1844 ? !parameters->options().output_is_position_independent()
1845 : gsym->final_value_is_known());
1846 const tls::Tls_optimization optimized_type
1847 = optimize_tls_reloc(is_final, r_type);
1849 switch (r_type)
1851 // XXX
1855 // Relocate section data.
1857 template<int size, bool big_endian>
1858 void
1859 Target_powerpc<size, big_endian>::relocate_section(
1860 const Relocate_info<size, big_endian>* relinfo,
1861 unsigned int sh_type,
1862 const unsigned char* prelocs,
1863 size_t reloc_count,
1864 Output_section* output_section,
1865 bool needs_special_offset_handling,
1866 unsigned char* view,
1867 typename elfcpp::Elf_types<size>::Elf_Addr address,
1868 section_size_type view_size)
1870 typedef Target_powerpc<size, big_endian> Powerpc;
1871 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1873 gold_assert(sh_type == elfcpp::SHT_RELA);
1875 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1876 Powerpc_relocate>(
1877 relinfo,
1878 this,
1879 prelocs,
1880 reloc_count,
1881 output_section,
1882 needs_special_offset_handling,
1883 view,
1884 address,
1885 view_size);
1888 // Return the size of a relocation while scanning during a relocatable
1889 // link.
1891 template<int size, bool big_endian>
1892 unsigned int
1893 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1894 unsigned int,
1895 Relobj*)
1897 // We are always SHT_RELA, so we should never get here.
1898 gold_unreachable();
1899 return 0;
1902 // Scan the relocs during a relocatable link.
1904 template<int size, bool big_endian>
1905 void
1906 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1907 const General_options& options,
1908 Symbol_table* symtab,
1909 Layout* layout,
1910 Sized_relobj<size, big_endian>* object,
1911 unsigned int data_shndx,
1912 unsigned int sh_type,
1913 const unsigned char* prelocs,
1914 size_t reloc_count,
1915 Output_section* output_section,
1916 bool needs_special_offset_handling,
1917 size_t local_symbol_count,
1918 const unsigned char* plocal_symbols,
1919 Relocatable_relocs* rr)
1921 gold_assert(sh_type == elfcpp::SHT_RELA);
1923 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1924 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1926 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1927 Scan_relocatable_relocs>(
1928 options,
1929 symtab,
1930 layout,
1931 object,
1932 data_shndx,
1933 prelocs,
1934 reloc_count,
1935 output_section,
1936 needs_special_offset_handling,
1937 local_symbol_count,
1938 plocal_symbols,
1939 rr);
1942 // Relocate a section during a relocatable link.
1944 template<int size, bool big_endian>
1945 void
1946 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1947 const Relocate_info<size, big_endian>* relinfo,
1948 unsigned int sh_type,
1949 const unsigned char* prelocs,
1950 size_t reloc_count,
1951 Output_section* output_section,
1952 off_t offset_in_output_section,
1953 const Relocatable_relocs* rr,
1954 unsigned char* view,
1955 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1956 section_size_type view_size,
1957 unsigned char* reloc_view,
1958 section_size_type reloc_view_size)
1960 gold_assert(sh_type == elfcpp::SHT_RELA);
1962 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1963 relinfo,
1964 prelocs,
1965 reloc_count,
1966 output_section,
1967 offset_in_output_section,
1969 view,
1970 view_address,
1971 view_size,
1972 reloc_view,
1973 reloc_view_size);
1976 // Return the value to use for a dynamic which requires special
1977 // treatment. This is how we support equality comparisons of function
1978 // pointers across shared library boundaries, as described in the
1979 // processor specific ABI supplement.
1981 template<int size, bool big_endian>
1982 uint64_t
1983 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1985 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1986 return this->plt_section()->address() + gsym->plt_offset();
1989 // The selector for powerpc object files.
1991 template<int size, bool big_endian>
1992 class Target_selector_powerpc : public Target_selector
1994 public:
1995 Target_selector_powerpc()
1996 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1997 (size == 64 ?
1998 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1999 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
2002 Target* do_recognize(int machine, int, int)
2004 switch (size)
2006 case 64:
2007 if (machine != elfcpp::EM_PPC64)
2008 return NULL;
2009 break;
2011 case 32:
2012 if (machine != elfcpp::EM_PPC)
2013 return NULL;
2014 break;
2016 default:
2017 return NULL;
2020 return this->instantiate_target();
2023 Target* do_instantiate_target()
2024 { return new Target_powerpc<size, big_endian>(); }
2027 Target_selector_powerpc<32, true> target_selector_ppc32;
2028 Target_selector_powerpc<32, false> target_selector_ppc32le;
2029 Target_selector_powerpc<64, true> target_selector_ppc64;
2030 Target_selector_powerpc<64, false> target_selector_ppc64le;
2032 } // End anonymous namespace.