elfcpp/ChangeLog:
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
6d03d481 3// Copyright 2008, 2009 Free Software Foundation, Inc.
42cacb20
DE
4// Written by David S. Miller <davem@davemloft.net>
5// and David Edelsohn <edelsohn@gnu.org>
6
7// This file is part of gold.
8
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.
13
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.
18
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.
23
24#include "gold.h"
25
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"
f345227a 40#include "gc.h"
42cacb20
DE
41
42namespace
43{
44
45using namespace gold;
46
47template<int size, bool big_endian>
48class Output_data_plt_powerpc;
49
50template<int size, bool big_endian>
51class Target_powerpc : public Sized_target<size, big_endian>
52{
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
55
56 Target_powerpc()
57 : Sized_target<size, big_endian>(&powerpc_info),
58 got_(NULL), got2_(NULL), toc_(NULL),
59 plt_(NULL), rela_dyn_(NULL),
60 copy_relocs_(elfcpp::R_POWERPC_COPY),
61 dynbss_(NULL), got_mod_index_offset_(-1U)
62 {
63 }
64
6d03d481
ST
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
67 void
ad0f2072 68 gc_process_relocs(Symbol_table* symtab,
6d03d481
ST
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);
79
42cacb20
DE
80 // Scan the relocations to look for symbol adjustments.
81 void
ad0f2072 82 scan_relocs(Symbol_table* symtab,
42cacb20
DE
83 Layout* layout,
84 Sized_relobj<size, big_endian>* object,
85 unsigned int data_shndx,
86 unsigned int sh_type,
87 const unsigned char* prelocs,
88 size_t reloc_count,
89 Output_section* output_section,
90 bool needs_special_offset_handling,
91 size_t local_symbol_count,
92 const unsigned char* plocal_symbols);
93 // Finalize the sections.
94 void
f59f41f3 95 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
96
97 // Return the value to use for a dynamic which requires special
98 // treatment.
99 uint64_t
100 do_dynsym_value(const Symbol*) const;
101
102 // Relocate a section.
103 void
104 relocate_section(const Relocate_info<size, big_endian>*,
105 unsigned int sh_type,
106 const unsigned char* prelocs,
107 size_t reloc_count,
108 Output_section* output_section,
109 bool needs_special_offset_handling,
110 unsigned char* view,
111 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
364c7fa5
ILT
112 section_size_type view_size,
113 const Reloc_symbol_changes*);
42cacb20
DE
114
115 // Scan the relocs during a relocatable link.
116 void
ad0f2072 117 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20
DE
118 Layout* layout,
119 Sized_relobj<size, big_endian>* object,
120 unsigned int data_shndx,
121 unsigned int sh_type,
122 const unsigned char* prelocs,
123 size_t reloc_count,
124 Output_section* output_section,
125 bool needs_special_offset_handling,
126 size_t local_symbol_count,
127 const unsigned char* plocal_symbols,
128 Relocatable_relocs*);
129
130 // Relocate a section during a relocatable link.
131 void
132 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
133 unsigned int sh_type,
134 const unsigned char* prelocs,
135 size_t reloc_count,
136 Output_section* output_section,
137 off_t offset_in_output_section,
138 const Relocatable_relocs*,
139 unsigned char* view,
140 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
141 section_size_type view_size,
142 unsigned char* reloc_view,
143 section_size_type reloc_view_size);
144
145 // Return whether SYM is defined by the ABI.
146 bool
9c2d0ef9 147 do_is_defined_by_abi(const Symbol* sym) const
42cacb20
DE
148 {
149 return strcmp(sym->name(), "___tls_get_addr") == 0;
150 }
151
152 // Return the size of the GOT section.
153 section_size_type
154 got_size()
155 {
156 gold_assert(this->got_ != NULL);
157 return this->got_->data_size();
158 }
159
160 private:
161
162 // The class which scans relocations.
163 class Scan
164 {
165 public:
166 Scan()
167 : issued_non_pic_error_(false)
168 { }
169
170 inline void
ad0f2072 171 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
42cacb20
DE
172 Sized_relobj<size, big_endian>* object,
173 unsigned int data_shndx,
174 Output_section* output_section,
175 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
176 const elfcpp::Sym<size, big_endian>& lsym);
177
178 inline void
ad0f2072 179 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
42cacb20
DE
180 Sized_relobj<size, big_endian>* object,
181 unsigned int data_shndx,
182 Output_section* output_section,
183 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
184 Symbol* gsym);
185
21bb3914
ST
186 inline bool
187 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
188 Target_powerpc* ,
189 Sized_relobj<size, big_endian>* ,
190 unsigned int ,
191 Output_section* ,
192 const elfcpp::Rela<size, big_endian>& ,
193 unsigned int ,
194 const elfcpp::Sym<size, big_endian>&)
195 { return false; }
196
197 inline bool
198 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
199 Target_powerpc* ,
200 Sized_relobj<size, big_endian>* ,
201 unsigned int ,
202 Output_section* ,
203 const elfcpp::Rela<size,
204 big_endian>& ,
205 unsigned int , Symbol*)
206 { return false; }
207
42cacb20
DE
208 private:
209 static void
210 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
211 unsigned int r_type);
212
213 static void
214 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
215 unsigned int r_type, Symbol*);
216
217 static void
218 generate_tls_call(Symbol_table* symtab, Layout* layout,
219 Target_powerpc* target);
220
221 void
222 check_non_pic(Relobj*, unsigned int r_type);
223
224 // Whether we have issued an error about a non-PIC compilation.
225 bool issued_non_pic_error_;
226 };
227
228 // The class which implements relocation.
229 class Relocate
230 {
231 public:
232 // Do a relocation. Return false if the caller should not issue
233 // any warnings about this relocation.
234 inline bool
235 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
031cdbed
ILT
236 Output_section*, size_t relnum,
237 const elfcpp::Rela<size, big_endian>&,
42cacb20
DE
238 unsigned int r_type, const Sized_symbol<size>*,
239 const Symbol_value<size>*,
240 unsigned char*,
241 typename elfcpp::Elf_types<size>::Elf_Addr,
242 section_size_type);
243
244 private:
245 // Do a TLS relocation.
246 inline void
247 relocate_tls(const Relocate_info<size, big_endian>*,
248 Target_powerpc* target,
249 size_t relnum, const elfcpp::Rela<size, big_endian>&,
250 unsigned int r_type, const Sized_symbol<size>*,
251 const Symbol_value<size>*,
252 unsigned char*,
253 typename elfcpp::Elf_types<size>::Elf_Addr,
254 section_size_type);
255 };
256
257 // A class which returns the size required for a relocation type,
258 // used while scanning relocs during a relocatable link.
259 class Relocatable_size_for_reloc
260 {
261 public:
262 unsigned int
263 get_size_for_reloc(unsigned int, Relobj*);
264 };
265
266 // Get the GOT section, creating it if necessary.
267 Output_data_got<size, big_endian>*
268 got_section(Symbol_table*, Layout*);
269
270 Output_data_space*
271 got2_section() const
272 {
273 gold_assert (this->got2_ != NULL);
274 return this->got2_;
275 }
276
277 // Get the TOC section.
278 Output_data_space*
279 toc_section() const
280 {
281 gold_assert (this->toc_ != NULL);
282 return this->toc_;
283 }
284
285 // Create a PLT entry for a global symbol.
286 void
287 make_plt_entry(Symbol_table*, Layout*, Symbol*);
288
289 // Create a GOT entry for the TLS module index.
290 unsigned int
291 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
292 Sized_relobj<size, big_endian>* object);
293
294 // Get the PLT section.
295 const Output_data_plt_powerpc<size, big_endian>*
296 plt_section() const
297 {
298 gold_assert(this->plt_ != NULL);
299 return this->plt_;
300 }
301
302 // Get the dynamic reloc section, creating it if necessary.
303 Reloc_section*
304 rela_dyn_section(Layout*);
305
42cacb20
DE
306 // Copy a relocation against a global symbol.
307 void
ef9beddf
ILT
308 copy_reloc(Symbol_table* symtab, Layout* layout,
309 Sized_relobj<size, big_endian>* object,
42cacb20
DE
310 unsigned int shndx, Output_section* output_section,
311 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
312 {
313 this->copy_relocs_.copy_reloc(symtab, layout,
314 symtab->get_sized_symbol<size>(sym),
315 object, shndx, output_section,
316 reloc, this->rela_dyn_section(layout));
317 }
318
319 // Information about this specific target which we pass to the
320 // general Target structure.
321 static Target::Target_info powerpc_info;
322
323 // The types of GOT entries needed for this platform.
324 enum Got_type
325 {
326 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
327 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
328 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
329 };
330
331 // The GOT section.
332 Output_data_got<size, big_endian>* got_;
333 // The GOT2 section.
334 Output_data_space* got2_;
335 // The TOC section.
336 Output_data_space* toc_;
337 // The PLT section.
338 Output_data_plt_powerpc<size, big_endian>* plt_;
339 // The dynamic reloc section.
340 Reloc_section* rela_dyn_;
341 // Relocs saved to avoid a COPY reloc.
342 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
343 // Space for variables copied with a COPY reloc.
344 Output_data_space* dynbss_;
345 // Offset of the GOT entry for the TLS module index;
346 unsigned int got_mod_index_offset_;
347};
348
349template<>
350Target::Target_info Target_powerpc<32, true>::powerpc_info =
351{
352 32, // size
353 true, // is_big_endian
354 elfcpp::EM_PPC, // machine_code
355 false, // has_make_symbol
356 false, // has_resolve
357 false, // has_code_fill
358 true, // is_default_stack_executable
359 '\0', // wrap_char
360 "/usr/lib/ld.so.1", // dynamic_linker
361 0x10000000, // default_text_segment_address
362 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
363 4 * 1024, // common_pagesize (overridable by -z common-page-size)
364 elfcpp::SHN_UNDEF, // small_common_shndx
365 elfcpp::SHN_UNDEF, // large_common_shndx
366 0, // small_common_section_flags
05a352e6
DK
367 0, // large_common_section_flags
368 NULL, // attributes_section
369 NULL // attributes_vendor
42cacb20
DE
370};
371
372template<>
373Target::Target_info Target_powerpc<32, false>::powerpc_info =
374{
375 32, // size
376 false, // is_big_endian
377 elfcpp::EM_PPC, // machine_code
378 false, // has_make_symbol
379 false, // has_resolve
380 false, // has_code_fill
381 true, // is_default_stack_executable
382 '\0', // wrap_char
383 "/usr/lib/ld.so.1", // dynamic_linker
384 0x10000000, // default_text_segment_address
385 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
386 4 * 1024, // common_pagesize (overridable by -z common-page-size)
387 elfcpp::SHN_UNDEF, // small_common_shndx
388 elfcpp::SHN_UNDEF, // large_common_shndx
389 0, // small_common_section_flags
05a352e6
DK
390 0, // large_common_section_flags
391 NULL, // attributes_section
392 NULL // attributes_vendor
42cacb20
DE
393};
394
395template<>
396Target::Target_info Target_powerpc<64, true>::powerpc_info =
397{
398 64, // size
399 true, // is_big_endian
400 elfcpp::EM_PPC64, // machine_code
401 false, // has_make_symbol
402 false, // has_resolve
403 false, // has_code_fill
404 true, // is_default_stack_executable
405 '\0', // wrap_char
406 "/usr/lib/ld.so.1", // dynamic_linker
407 0x10000000, // default_text_segment_address
408 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
409 8 * 1024, // common_pagesize (overridable by -z common-page-size)
410 elfcpp::SHN_UNDEF, // small_common_shndx
411 elfcpp::SHN_UNDEF, // large_common_shndx
412 0, // small_common_section_flags
05a352e6
DK
413 0, // large_common_section_flags
414 NULL, // attributes_section
415 NULL // attributes_vendor
42cacb20
DE
416};
417
418template<>
419Target::Target_info Target_powerpc<64, false>::powerpc_info =
420{
421 64, // size
422 false, // is_big_endian
423 elfcpp::EM_PPC64, // machine_code
424 false, // has_make_symbol
425 false, // has_resolve
426 false, // has_code_fill
427 true, // is_default_stack_executable
428 '\0', // wrap_char
429 "/usr/lib/ld.so.1", // dynamic_linker
430 0x10000000, // default_text_segment_address
431 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
432 8 * 1024, // common_pagesize (overridable by -z common-page-size)
433 elfcpp::SHN_UNDEF, // small_common_shndx
434 elfcpp::SHN_UNDEF, // large_common_shndx
435 0, // small_common_section_flags
05a352e6
DK
436 0, // large_common_section_flags
437 NULL, // attributes_section
438 NULL // attributes_vendor
42cacb20
DE
439};
440
441template<int size, bool big_endian>
442class Powerpc_relocate_functions
443{
444private:
445 // Do a simple relocation with the addend in the relocation.
446 template<int valsize>
447 static inline void
448 rela(unsigned char* view,
449 unsigned int right_shift,
450 elfcpp::Elf_Xword dst_mask,
451 typename elfcpp::Swap<size, big_endian>::Valtype value,
452 typename elfcpp::Swap<size, big_endian>::Valtype addend)
453 {
454 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
455 Valtype* wv = reinterpret_cast<Valtype*>(view);
456 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
457 Valtype reloc = ((value + addend) >> right_shift);
458
459 val &= ~dst_mask;
460 reloc &= dst_mask;
461
462 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
463 }
464
465 // Do a simple relocation using a symbol value with the addend in
466 // the relocation.
467 template<int valsize>
468 static inline void
469 rela(unsigned char* view,
470 unsigned int right_shift,
471 elfcpp::Elf_Xword dst_mask,
472 const Sized_relobj<size, big_endian>* object,
473 const Symbol_value<size>* psymval,
474 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
475 {
476 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
477 Valtype* wv = reinterpret_cast<Valtype*>(view);
478 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
479 Valtype reloc = (psymval->value(object, addend) >> right_shift);
480
481 val &= ~dst_mask;
482 reloc &= dst_mask;
483
484 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
485 }
486
487 // Do a simple relocation using a symbol value with the addend in
488 // the relocation, unaligned.
489 template<int valsize>
490 static inline void
491 rela_ua(unsigned char* view, unsigned int right_shift,
492 elfcpp::Elf_Xword dst_mask,
493 const Sized_relobj<size, big_endian>* object,
494 const Symbol_value<size>* psymval,
495 typename elfcpp::Swap<size, big_endian>::Valtype addend)
496 {
497 typedef typename elfcpp::Swap_unaligned<valsize,
498 big_endian>::Valtype Valtype;
499 unsigned char* wv = view;
500 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
501 Valtype reloc = (psymval->value(object, addend) >> right_shift);
502
503 val &= ~dst_mask;
504 reloc &= dst_mask;
505
506 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
507 }
508
509 // Do a simple PC relative relocation with a Symbol_value with the
510 // addend in the relocation.
511 template<int valsize>
512 static inline void
513 pcrela(unsigned char* view, unsigned int right_shift,
514 elfcpp::Elf_Xword dst_mask,
515 const Sized_relobj<size, big_endian>* object,
516 const Symbol_value<size>* psymval,
517 typename elfcpp::Swap<size, big_endian>::Valtype addend,
518 typename elfcpp::Elf_types<size>::Elf_Addr address)
519 {
520 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
521 Valtype* wv = reinterpret_cast<Valtype*>(view);
522 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
523 Valtype reloc = ((psymval->value(object, addend) - address)
524 >> right_shift);
525
526 val &= ~dst_mask;
527 reloc &= dst_mask;
528
529 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
530 }
531
532 template<int valsize>
533 static inline void
534 pcrela_unaligned(unsigned char* view,
535 const Sized_relobj<size, big_endian>* object,
536 const Symbol_value<size>* psymval,
537 typename elfcpp::Swap<size, big_endian>::Valtype addend,
538 typename elfcpp::Elf_types<size>::Elf_Addr address)
539 {
540 typedef typename elfcpp::Swap_unaligned<valsize,
541 big_endian>::Valtype Valtype;
542 unsigned char* wv = view;
543 Valtype reloc = (psymval->value(object, addend) - address);
544
545 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
546 }
547
548 typedef Powerpc_relocate_functions<size, big_endian> This;
549 typedef Relocate_functions<size, big_endian> This_reloc;
550public:
551 // R_POWERPC_REL32: (Symbol + Addend - Address)
552 static inline void
553 rel32(unsigned char* view,
554 const Sized_relobj<size, big_endian>* object,
555 const Symbol_value<size>* psymval,
556 typename elfcpp::Elf_types<size>::Elf_Addr addend,
557 typename elfcpp::Elf_types<size>::Elf_Addr address)
558 { This_reloc::pcrela32(view, object, psymval, addend, address); }
559
560 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
561 static inline void
562 rel24(unsigned char* view,
563 const Sized_relobj<size, big_endian>* object,
564 const Symbol_value<size>* psymval,
565 typename elfcpp::Elf_types<size>::Elf_Addr addend,
566 typename elfcpp::Elf_types<size>::Elf_Addr address)
567 {
568 This::template pcrela<32>(view, 0, 0x03fffffc, object,
569 psymval, addend, address);
570 }
571
572 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
573 static inline void
574 rel14(unsigned char* view,
575 const Sized_relobj<size, big_endian>* object,
576 const Symbol_value<size>* psymval,
577 typename elfcpp::Elf_types<size>::Elf_Addr addend,
578 typename elfcpp::Elf_types<size>::Elf_Addr address)
579 {
580 This::template pcrela<32>(view, 0, 0x0000fffc, object,
581 psymval, addend, address);
582 }
583
584 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
585 static inline void
586 addr16(unsigned char* view,
587 typename elfcpp::Elf_types<size>::Elf_Addr value,
588 typename elfcpp::Elf_types<size>::Elf_Addr addend)
589 { This_reloc::rela16(view, value, addend); }
590
591 static inline void
592 addr16(unsigned char* view,
593 const Sized_relobj<size, big_endian>* object,
594 const Symbol_value<size>* psymval,
595 typename elfcpp::Elf_types<size>::Elf_Addr addend)
596 { This_reloc::rela16(view, object, psymval, addend); }
597
598 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
599 static inline void
600 addr16_ds(unsigned char* view,
601 typename elfcpp::Elf_types<size>::Elf_Addr value,
602 typename elfcpp::Elf_types<size>::Elf_Addr addend)
603 {
604 This::template rela<16>(view, 0, 0xfffc, value, addend);
605 }
606
607 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
608 static inline void
609 addr16_lo(unsigned char* view,
610 typename elfcpp::Elf_types<size>::Elf_Addr value,
611 typename elfcpp::Elf_types<size>::Elf_Addr addend)
612 { This_reloc::rela16(view, value, addend); }
613
614 static inline void
615 addr16_lo(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)
619 { This_reloc::rela16(view, object, psymval, addend); }
620
621 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
622 static inline void
623 addr16_hi(unsigned char* view,
624 typename elfcpp::Elf_types<size>::Elf_Addr value,
625 typename elfcpp::Elf_types<size>::Elf_Addr addend)
626 {
627 This::template rela<16>(view, 16, 0xffff, value, addend);
628 }
629
630 static inline void
631 addr16_hi(unsigned char* view,
632 const Sized_relobj<size, big_endian>* object,
633 const Symbol_value<size>* psymval,
634 typename elfcpp::Elf_types<size>::Elf_Addr addend)
635 {
636 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
637 }
638
639 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
640 // final value of the low 16 bits of the
641 // relocation is negative, add one.
642 static inline void
643 addr16_ha(unsigned char* view,
644 typename elfcpp::Elf_types<size>::Elf_Addr value,
645 typename elfcpp::Elf_types<size>::Elf_Addr addend)
646 {
42cacb20
DE
647 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
648
649 reloc = value + addend;
650
651 if (reloc & 0x8000)
652 reloc += 0x10000;
653 reloc >>= 16;
654
e6fde208 655 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
42cacb20
DE
656 }
657
658 static inline void
659 addr16_ha(unsigned char* view,
660 const Sized_relobj<size, big_endian>* object,
661 const Symbol_value<size>* psymval,
662 typename elfcpp::Elf_types<size>::Elf_Addr addend)
663 {
42cacb20
DE
664 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
665
666 reloc = psymval->value(object, addend);
667
668 if (reloc & 0x8000)
669 reloc += 0x10000;
670 reloc >>= 16;
671
e6fde208 672 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
42cacb20
DE
673 }
674
675 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
676 static inline void
677 rel16(unsigned char* view,
678 const Sized_relobj<size, big_endian>* object,
679 const Symbol_value<size>* psymval,
680 typename elfcpp::Elf_types<size>::Elf_Addr addend,
681 typename elfcpp::Elf_types<size>::Elf_Addr address)
682 { This_reloc::pcrela16(view, object, psymval, addend, address); }
683
684 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
685 static inline void
686 rel16_lo(unsigned char* view,
687 const Sized_relobj<size, big_endian>* object,
688 const Symbol_value<size>* psymval,
689 typename elfcpp::Elf_types<size>::Elf_Addr addend,
690 typename elfcpp::Elf_types<size>::Elf_Addr address)
691 { This_reloc::pcrela16(view, object, psymval, addend, address); }
692
693 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
694 static inline void
695 rel16_hi(unsigned char* view,
696 const Sized_relobj<size, big_endian>* object,
697 const Symbol_value<size>* psymval,
698 typename elfcpp::Elf_types<size>::Elf_Addr addend,
699 typename elfcpp::Elf_types<size>::Elf_Addr address)
700 {
701 This::template pcrela<16>(view, 16, 0xffff, object,
702 psymval, addend, address);
703 }
704
705 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
706 // final value of the low 16 bits of the
707 // relocation is negative, add one.
708 static inline void
709 rel16_ha(unsigned char* view,
710 const Sized_relobj<size, big_endian>* object,
711 const Symbol_value<size>* psymval,
712 typename elfcpp::Elf_types<size>::Elf_Addr addend,
713 typename elfcpp::Elf_types<size>::Elf_Addr address)
714 {
42cacb20
DE
715 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
716
717 reloc = (psymval->value(object, addend) - address);
718 if (reloc & 0x8000)
719 reloc += 0x10000;
720 reloc >>= 16;
721
83d22aa8 722 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
42cacb20
DE
723 }
724};
725
726// Get the GOT section, creating it if necessary.
727
728template<int size, bool big_endian>
729Output_data_got<size, big_endian>*
730Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
731 Layout* layout)
732{
733 if (this->got_ == NULL)
734 {
735 gold_assert(symtab != NULL && layout != NULL);
736
737 this->got_ = new Output_data_got<size, big_endian>();
738
739 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
740 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 741 this->got_, ORDER_DATA, false);
42cacb20
DE
742
743 // Create the GOT2 or TOC in the .got section.
744 if (size == 32)
745 {
746 this->got2_ = new Output_data_space(4, "** GOT2");
747 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
748 elfcpp::SHF_ALLOC
749 | elfcpp::SHF_WRITE,
22f0da72 750 this->got2_, ORDER_DATA, false);
42cacb20
DE
751 }
752 else
753 {
754 this->toc_ = new Output_data_space(8, "** TOC");
755 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
756 elfcpp::SHF_ALLOC
757 | elfcpp::SHF_WRITE,
22f0da72 758 this->toc_, ORDER_DATA, false);
42cacb20
DE
759 }
760
761 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
762 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
99fff23b 763 Symbol_table::PREDEFINED,
42cacb20
DE
764 this->got_,
765 0, 0, elfcpp::STT_OBJECT,
766 elfcpp::STB_LOCAL,
767 elfcpp::STV_HIDDEN, 0,
768 false, false);
769 }
770
771 return this->got_;
772}
773
774// Get the dynamic reloc section, creating it if necessary.
775
776template<int size, bool big_endian>
777typename Target_powerpc<size, big_endian>::Reloc_section*
778Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
779{
780 if (this->rela_dyn_ == NULL)
781 {
782 gold_assert(layout != NULL);
783 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
784 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
785 elfcpp::SHF_ALLOC, this->rela_dyn_,
786 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
787 }
788 return this->rela_dyn_;
789}
790
791// A class to handle the PLT data.
792
793template<int size, bool big_endian>
794class Output_data_plt_powerpc : public Output_section_data
795{
796 public:
797 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
798 size, big_endian> Reloc_section;
799
800 Output_data_plt_powerpc(Layout*);
801
802 // Add an entry to the PLT.
803 void add_entry(Symbol* gsym);
804
805 // Return the .rela.plt section data.
806 const Reloc_section* rel_plt() const
807 {
808 return this->rel_;
809 }
810
811 protected:
812 void do_adjust_output_section(Output_section* os);
813
814 private:
815 // The size of an entry in the PLT.
816 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
817
818 // Set the final size.
819 void
820 set_final_data_size()
821 {
822 unsigned int full_count = this->count_ + 4;
823
824 this->set_data_size(full_count * base_plt_entry_size);
825 }
826
827 // Write out the PLT data.
828 void
829 do_write(Output_file*);
830
831 // The reloc section.
832 Reloc_section* rel_;
833 // The number of PLT entries.
834 unsigned int count_;
835};
836
837// Create the PLT section. The ordinary .got section is an argument,
838// since we need to refer to the start.
839
840template<int size, bool big_endian>
841Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
842 : Output_section_data(size == 32 ? 4 : 8), count_(0)
843{
844 this->rel_ = new Reloc_section(false);
845 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
846 elfcpp::SHF_ALLOC, this->rel_,
847 ORDER_DYNAMIC_PLT_RELOCS, false);
42cacb20
DE
848}
849
850template<int size, bool big_endian>
851void
852Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
853{
854 os->set_entsize(0);
855}
856
857// Add an entry to the PLT.
858
859template<int size, bool big_endian>
860void
861Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
862{
863 gold_assert(!gsym->has_plt_offset());
864 unsigned int index = this->count_+ + 4;
865 section_offset_type plt_offset;
866
867 if (index < 8192)
868 plt_offset = index * base_plt_entry_size;
869 else
870 gold_unreachable();
871
872 gsym->set_plt_offset(plt_offset);
873
874 ++this->count_;
875
876 gsym->set_needs_dynsym_entry();
877 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
878 plt_offset, 0);
879}
880
881static const unsigned int addis_11_11 = 0x3d6b0000;
882static const unsigned int addis_11_30 = 0x3d7e0000;
883static const unsigned int addis_12_12 = 0x3d8c0000;
884static const unsigned int addi_11_11 = 0x396b0000;
885static const unsigned int add_0_11_11 = 0x7c0b5a14;
886static const unsigned int add_11_0_11 = 0x7d605a14;
887static const unsigned int b = 0x48000000;
888static const unsigned int bcl_20_31 = 0x429f0005;
889static const unsigned int bctr = 0x4e800420;
890static const unsigned int lis_11 = 0x3d600000;
891static const unsigned int lis_12 = 0x3d800000;
892static const unsigned int lwzu_0_12 = 0x840c0000;
893static const unsigned int lwz_0_12 = 0x800c0000;
894static const unsigned int lwz_11_11 = 0x816b0000;
895static const unsigned int lwz_11_30 = 0x817e0000;
896static const unsigned int lwz_12_12 = 0x818c0000;
897static const unsigned int mflr_0 = 0x7c0802a6;
898static const unsigned int mflr_12 = 0x7d8802a6;
899static const unsigned int mtctr_0 = 0x7c0903a6;
900static const unsigned int mtctr_11 = 0x7d6903a6;
901static const unsigned int mtlr_0 = 0x7c0803a6;
902static const unsigned int nop = 0x60000000;
903static const unsigned int sub_11_11_12 = 0x7d6c5850;
904
905static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
906static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
907static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
908static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
909 /* ld %r11,xxx+16@l(%r12) */
910
911
912// Write out the PLT.
913
914template<int size, bool big_endian>
915void
916Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
917{
2ea97941 918 const off_t offset = this->offset();
42cacb20
DE
919 const section_size_type oview_size =
920 convert_to_section_size_type(this->data_size());
2ea97941 921 unsigned char* const oview = of->get_output_view(offset, oview_size);
42cacb20
DE
922 unsigned char* pov = oview;
923
924 memset(pov, 0, base_plt_entry_size * 4);
925 pov += base_plt_entry_size * 4;
926
927 unsigned int plt_offset = base_plt_entry_size * 4;
928 const unsigned int count = this->count_;
929
930 if (size == 64)
931 {
932 for (unsigned int i = 0; i < count; i++)
933 {
934 }
935 }
936 else
937 {
938 for (unsigned int i = 0; i < count; i++)
939 {
940 elfcpp::Swap<32, true>::writeval(pov + 0x00,
941 lwz_11_30 + plt_offset);
942 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
943 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
ce3ac18a 944 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
42cacb20
DE
945 pov += base_plt_entry_size;
946 plt_offset += base_plt_entry_size;
947 }
948 }
949
950 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
951
2ea97941 952 of->write_output_view(offset, oview_size, oview);
42cacb20
DE
953}
954
955// Create a PLT entry for a global symbol.
956
957template<int size, bool big_endian>
958void
959Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
960 Layout* layout,
961 Symbol* gsym)
962{
963 if (gsym->has_plt_offset())
964 return;
965
966 if (this->plt_ == NULL)
967 {
968 // Create the GOT section first.
969 this->got_section(symtab, layout);
970
612a8d3d
DM
971 // Ensure that .rela.dyn always appears before .rela.plt This is
972 // necessary due to how, on PowerPC and some other targets, .rela.dyn
973 // needs to include .rela.plt in it's range.
974 this->rela_dyn_section(layout);
975
42cacb20
DE
976 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
977 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
978 (elfcpp::SHF_ALLOC
979 | elfcpp::SHF_EXECINSTR
980 | elfcpp::SHF_WRITE),
22f0da72 981 this->plt_, ORDER_PLT, false);
42cacb20
DE
982
983 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
984 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
99fff23b 985 Symbol_table::PREDEFINED,
42cacb20
DE
986 this->plt_,
987 0, 0, elfcpp::STT_OBJECT,
988 elfcpp::STB_LOCAL,
989 elfcpp::STV_HIDDEN, 0,
990 false, false);
991 }
992
993 this->plt_->add_entry(gsym);
994}
995
996// Create a GOT entry for the TLS module index.
997
998template<int size, bool big_endian>
999unsigned int
1000Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
1001 Layout* layout,
1002 Sized_relobj<size, big_endian>* object)
1003{
1004 if (this->got_mod_index_offset_ == -1U)
1005 {
1006 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1007 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1008 Output_data_got<size, big_endian>* got;
1009 unsigned int got_offset;
1010
1011 got = this->got_section(symtab, layout);
1012 got_offset = got->add_constant(0);
1013 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1014 got_offset, 0);
1015 got->add_constant(0);
1016 this->got_mod_index_offset_ = got_offset;
1017 }
1018 return this->got_mod_index_offset_;
1019}
1020
1021// Optimize the TLS relocation type based on what we know about the
1022// symbol. IS_FINAL is true if the final address of this symbol is
1023// known at link time.
1024
1025static tls::Tls_optimization
1026optimize_tls_reloc(bool /* is_final */, int r_type)
1027{
1028 // If we are generating a shared library, then we can't do anything
1029 // in the linker.
1030 if (parameters->options().shared())
1031 return tls::TLSOPT_NONE;
1032 switch (r_type)
1033 {
1034 // XXX
1035 default:
1036 gold_unreachable();
1037 }
1038}
1039
1040// Report an unsupported relocation against a local symbol.
1041
1042template<int size, bool big_endian>
1043void
1044Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1045 Sized_relobj<size, big_endian>* object,
1046 unsigned int r_type)
1047{
1048 gold_error(_("%s: unsupported reloc %u against local symbol"),
1049 object->name().c_str(), r_type);
1050}
1051
1052// We are about to emit a dynamic relocation of type R_TYPE. If the
1053// dynamic linker does not support it, issue an error.
1054
1055template<int size, bool big_endian>
1056void
1057Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1058 unsigned int r_type)
1059{
1060 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1061
1062 // These are the relocation types supported by glibc for both 32-bit
1063 // and 64-bit powerpc.
1064 switch (r_type)
1065 {
1066 case elfcpp::R_POWERPC_RELATIVE:
1067 case elfcpp::R_POWERPC_GLOB_DAT:
1068 case elfcpp::R_POWERPC_DTPMOD:
1069 case elfcpp::R_POWERPC_DTPREL:
1070 case elfcpp::R_POWERPC_TPREL:
1071 case elfcpp::R_POWERPC_JMP_SLOT:
1072 case elfcpp::R_POWERPC_COPY:
1073 case elfcpp::R_POWERPC_ADDR32:
1074 case elfcpp::R_POWERPC_ADDR24:
1075 case elfcpp::R_POWERPC_REL24:
1076 return;
1077
1078 default:
1079 break;
1080 }
1081
1082 if (size == 64)
1083 {
1084 switch (r_type)
1085 {
1086 // These are the relocation types supported only on 64-bit.
1087 case elfcpp::R_PPC64_ADDR64:
1088 case elfcpp::R_PPC64_TPREL16_LO_DS:
1089 case elfcpp::R_PPC64_TPREL16_DS:
1090 case elfcpp::R_POWERPC_TPREL16:
1091 case elfcpp::R_POWERPC_TPREL16_LO:
1092 case elfcpp::R_POWERPC_TPREL16_HI:
1093 case elfcpp::R_POWERPC_TPREL16_HA:
1094 case elfcpp::R_PPC64_TPREL16_HIGHER:
1095 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1096 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1097 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1098 case elfcpp::R_PPC64_ADDR16_LO_DS:
1099 case elfcpp::R_POWERPC_ADDR16_LO:
1100 case elfcpp::R_POWERPC_ADDR16_HI:
1101 case elfcpp::R_POWERPC_ADDR16_HA:
1102 case elfcpp::R_POWERPC_ADDR30:
1103 case elfcpp::R_PPC64_UADDR64:
1104 case elfcpp::R_POWERPC_UADDR32:
1105 case elfcpp::R_POWERPC_ADDR16:
1106 case elfcpp::R_POWERPC_UADDR16:
1107 case elfcpp::R_PPC64_ADDR16_DS:
1108 case elfcpp::R_PPC64_ADDR16_HIGHER:
1109 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1110 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1111 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1112 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1113 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1114 case elfcpp::R_POWERPC_REL32:
1115 case elfcpp::R_PPC64_REL64:
1116 return;
1117
1118 default:
1119 break;
1120 }
1121 }
1122 else
1123 {
1124 switch (r_type)
1125 {
1126 // These are the relocation types supported only on 32-bit.
1127
1128 default:
1129 break;
1130 }
1131 }
1132
1133 // This prevents us from issuing more than one error per reloc
1134 // section. But we can still wind up issuing more than one
1135 // error per object file.
1136 if (this->issued_non_pic_error_)
1137 return;
33aea2fd 1138 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
1139 object->error(_("requires unsupported dynamic reloc; "
1140 "recompile with -fPIC"));
1141 this->issued_non_pic_error_ = true;
1142 return;
1143}
1144
1145// Scan a relocation for a local symbol.
1146
1147template<int size, bool big_endian>
1148inline void
1149Target_powerpc<size, big_endian>::Scan::local(
42cacb20
DE
1150 Symbol_table* symtab,
1151 Layout* layout,
1152 Target_powerpc<size, big_endian>* target,
1153 Sized_relobj<size, big_endian>* object,
1154 unsigned int data_shndx,
1155 Output_section* output_section,
1156 const elfcpp::Rela<size, big_endian>& reloc,
1157 unsigned int r_type,
1158 const elfcpp::Sym<size, big_endian>& lsym)
1159{
1160 switch (r_type)
1161 {
1162 case elfcpp::R_POWERPC_NONE:
1163 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1164 case elfcpp::R_POWERPC_GNU_VTENTRY:
1165 break;
1166
1167 case elfcpp::R_PPC64_ADDR64:
1168 case elfcpp::R_POWERPC_ADDR32:
1169 case elfcpp::R_POWERPC_ADDR16_HA:
1170 case elfcpp::R_POWERPC_ADDR16_LO:
1171 // If building a shared library (or a position-independent
1172 // executable), we need to create a dynamic relocation for
1173 // this location.
1174 if (parameters->options().output_is_position_independent())
1175 {
1176 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1177
1178 check_non_pic(object, r_type);
1179 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1180 {
1181 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1182 rela_dyn->add_local(object, r_sym, r_type, output_section,
1183 data_shndx, reloc.get_r_offset(),
1184 reloc.get_r_addend());
1185 }
1186 else
1187 {
1188 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1189 gold_assert(lsym.get_st_value() == 0);
1190 rela_dyn->add_local_relative(object, r_sym, r_type,
1191 output_section, data_shndx,
1192 reloc.get_r_offset(),
1193 reloc.get_r_addend());
1194 }
1195 }
1196 break;
1197
1198 case elfcpp::R_POWERPC_REL24:
1199 case elfcpp::R_PPC_LOCAL24PC:
1200 case elfcpp::R_POWERPC_REL32:
1201 case elfcpp::R_PPC_REL16_LO:
1202 case elfcpp::R_PPC_REL16_HA:
1203 break;
1204
1205 case elfcpp::R_POWERPC_GOT16:
1206 case elfcpp::R_POWERPC_GOT16_LO:
1207 case elfcpp::R_POWERPC_GOT16_HI:
1208 case elfcpp::R_POWERPC_GOT16_HA:
1209 case elfcpp::R_PPC64_TOC16:
1210 case elfcpp::R_PPC64_TOC16_LO:
1211 case elfcpp::R_PPC64_TOC16_HI:
1212 case elfcpp::R_PPC64_TOC16_HA:
1213 case elfcpp::R_PPC64_TOC16_DS:
1214 case elfcpp::R_PPC64_TOC16_LO_DS:
1215 {
1216 // The symbol requires a GOT entry.
1217 Output_data_got<size, big_endian>* got;
1218 unsigned int r_sym;
1219
1220 got = target->got_section(symtab, layout);
1221 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1222
1223 // If we are generating a shared object, we need to add a
1224 // dynamic relocation for this symbol's GOT entry.
1225 if (parameters->options().output_is_position_independent())
1226 {
1227 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1228 {
1229 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1230 unsigned int off;
1231
1232 off = got->add_constant(0);
1233 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1234 rela_dyn->add_local_relative(object, r_sym,
1235 elfcpp::R_POWERPC_RELATIVE,
1236 got, off, 0);
1237 }
1238 }
1239 else
1240 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1241 }
1242 break;
1243
1244 case elfcpp::R_PPC64_TOC:
1245 // We need a GOT section.
1246 target->got_section(symtab, layout);
1247 break;
1248
1249 // These are relocations which should only be seen by the
1250 // dynamic linker, and should never be seen here.
1251 case elfcpp::R_POWERPC_COPY:
1252 case elfcpp::R_POWERPC_GLOB_DAT:
1253 case elfcpp::R_POWERPC_JMP_SLOT:
1254 case elfcpp::R_POWERPC_RELATIVE:
1255 case elfcpp::R_POWERPC_DTPMOD:
1256 gold_error(_("%s: unexpected reloc %u in object file"),
1257 object->name().c_str(), r_type);
1258 break;
1259
1260 default:
1261 unsupported_reloc_local(object, r_type);
1262 break;
1263 }
1264}
1265
1266// Report an unsupported relocation against a global symbol.
1267
1268template<int size, bool big_endian>
1269void
1270Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1271 Sized_relobj<size, big_endian>* object,
1272 unsigned int r_type,
1273 Symbol* gsym)
1274{
1275 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1276 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1277}
1278
1279// Scan a relocation for a global symbol.
1280
1281template<int size, bool big_endian>
1282inline void
1283Target_powerpc<size, big_endian>::Scan::global(
42cacb20
DE
1284 Symbol_table* symtab,
1285 Layout* layout,
1286 Target_powerpc<size, big_endian>* target,
1287 Sized_relobj<size, big_endian>* object,
1288 unsigned int data_shndx,
1289 Output_section* output_section,
1290 const elfcpp::Rela<size, big_endian>& reloc,
1291 unsigned int r_type,
1292 Symbol* gsym)
1293{
1294 switch (r_type)
1295 {
1296 case elfcpp::R_POWERPC_NONE:
1297 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1298 case elfcpp::R_POWERPC_GNU_VTENTRY:
1299 break;
1300
1301 case elfcpp::R_PPC_PLTREL24:
1302 // If the symbol is fully resolved, this is just a PC32 reloc.
1303 // Otherwise we need a PLT entry.
1304 if (gsym->final_value_is_known())
1305 break;
1306 // If building a shared library, we can also skip the PLT entry
1307 // if the symbol is defined in the output file and is protected
1308 // or hidden.
1309 if (gsym->is_defined()
1310 && !gsym->is_from_dynobj()
1311 && !gsym->is_preemptible())
1312 break;
1313 target->make_plt_entry(symtab, layout, gsym);
1314 break;
1315
1316 case elfcpp::R_POWERPC_ADDR16:
1317 case elfcpp::R_POWERPC_ADDR16_LO:
1318 case elfcpp::R_POWERPC_ADDR16_HI:
1319 case elfcpp::R_POWERPC_ADDR16_HA:
1320 case elfcpp::R_POWERPC_ADDR32:
1321 case elfcpp::R_PPC64_ADDR64:
1322 {
1323 // Make a PLT entry if necessary.
1324 if (gsym->needs_plt_entry())
1325 {
1326 target->make_plt_entry(symtab, layout, gsym);
1327 // Since this is not a PC-relative relocation, we may be
1328 // taking the address of a function. In that case we need to
1329 // set the entry in the dynamic symbol table to the address of
1330 // the PLT entry.
1331 if (gsym->is_from_dynobj() && !parameters->options().shared())
1332 gsym->set_needs_dynsym_value();
1333 }
1334 // Make a dynamic relocation if necessary.
1335 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1336 {
966d4097 1337 if (gsym->may_need_copy_reloc())
42cacb20
DE
1338 {
1339 target->copy_reloc(symtab, layout, object,
1340 data_shndx, output_section, gsym, reloc);
1341 }
1342 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1343 || r_type == elfcpp::R_PPC64_ADDR64)
1344 && gsym->can_use_relative_reloc(false))
1345 {
1346 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1347 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1348 output_section, object,
1349 data_shndx, reloc.get_r_offset(),
1350 reloc.get_r_addend());
1351 }
1352 else
1353 {
1354 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1355
1356 check_non_pic(object, r_type);
1357 if (gsym->is_from_dynobj()
1358 || gsym->is_undefined()
1359 || gsym->is_preemptible())
1360 rela_dyn->add_global(gsym, r_type, output_section,
1361 object, data_shndx,
1362 reloc.get_r_offset(),
1363 reloc.get_r_addend());
1364 else
1365 rela_dyn->add_global_relative(gsym, r_type,
1366 output_section, object,
1367 data_shndx,
1368 reloc.get_r_offset(),
1369 reloc.get_r_addend());
1370 }
1371 }
1372 }
1373 break;
1374
1375 case elfcpp::R_POWERPC_REL24:
1376 case elfcpp::R_PPC_LOCAL24PC:
1377 case elfcpp::R_PPC_REL16:
1378 case elfcpp::R_PPC_REL16_LO:
1379 case elfcpp::R_PPC_REL16_HI:
1380 case elfcpp::R_PPC_REL16_HA:
1381 {
1382 if (gsym->needs_plt_entry())
1383 target->make_plt_entry(symtab, layout, gsym);
1384 // Make a dynamic relocation if necessary.
1385 int flags = Symbol::NON_PIC_REF;
1386 if (gsym->type() == elfcpp::STT_FUNC)
1387 flags |= Symbol::FUNCTION_CALL;
1388 if (gsym->needs_dynamic_reloc(flags))
1389 {
966d4097 1390 if (gsym->may_need_copy_reloc())
42cacb20
DE
1391 {
1392 target->copy_reloc(symtab, layout, object,
1393 data_shndx, output_section, gsym,
1394 reloc);
1395 }
1396 else
1397 {
1398 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1399 check_non_pic(object, r_type);
1400 rela_dyn->add_global(gsym, r_type, output_section, object,
1401 data_shndx, reloc.get_r_offset(),
1402 reloc.get_r_addend());
1403 }
1404 }
1405 }
1406 break;
1407
1408 case elfcpp::R_POWERPC_GOT16:
1409 case elfcpp::R_POWERPC_GOT16_LO:
1410 case elfcpp::R_POWERPC_GOT16_HI:
1411 case elfcpp::R_POWERPC_GOT16_HA:
1412 case elfcpp::R_PPC64_TOC16:
1413 case elfcpp::R_PPC64_TOC16_LO:
1414 case elfcpp::R_PPC64_TOC16_HI:
1415 case elfcpp::R_PPC64_TOC16_HA:
1416 case elfcpp::R_PPC64_TOC16_DS:
1417 case elfcpp::R_PPC64_TOC16_LO_DS:
1418 {
1419 // The symbol requires a GOT entry.
1420 Output_data_got<size, big_endian>* got;
1421
1422 got = target->got_section(symtab, layout);
1423 if (gsym->final_value_is_known())
1424 got->add_global(gsym, GOT_TYPE_STANDARD);
1425 else
1426 {
1427 // If this symbol is not fully resolved, we need to add a
1428 // dynamic relocation for it.
1429 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1430 if (gsym->is_from_dynobj()
1431 || gsym->is_undefined()
1432 || gsym->is_preemptible())
1433 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1434 elfcpp::R_POWERPC_GLOB_DAT);
1435 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1436 {
1437 unsigned int off = got->add_constant(0);
1438
1439 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1440 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1441 got, off, 0);
1442 }
1443 }
1444 }
1445 break;
1446
1447 case elfcpp::R_PPC64_TOC:
1448 // We need a GOT section.
1449 target->got_section(symtab, layout);
1450 break;
1451
1452 case elfcpp::R_POWERPC_GOT_TPREL16:
1453 case elfcpp::R_POWERPC_TLS:
1454 // XXX TLS
1455 break;
1456
1457 // These are relocations which should only be seen by the
1458 // dynamic linker, and should never be seen here.
1459 case elfcpp::R_POWERPC_COPY:
1460 case elfcpp::R_POWERPC_GLOB_DAT:
1461 case elfcpp::R_POWERPC_JMP_SLOT:
1462 case elfcpp::R_POWERPC_RELATIVE:
1463 case elfcpp::R_POWERPC_DTPMOD:
1464 gold_error(_("%s: unexpected reloc %u in object file"),
1465 object->name().c_str(), r_type);
1466 break;
1467
1468 default:
1469 unsupported_reloc_global(object, r_type, gsym);
1470 break;
1471 }
1472}
1473
6d03d481
ST
1474// Process relocations for gc.
1475
1476template<int size, bool big_endian>
1477void
1478Target_powerpc<size, big_endian>::gc_process_relocs(
6d03d481
ST
1479 Symbol_table* symtab,
1480 Layout* layout,
1481 Sized_relobj<size, big_endian>* object,
1482 unsigned int data_shndx,
1483 unsigned int,
1484 const unsigned char* prelocs,
1485 size_t reloc_count,
1486 Output_section* output_section,
1487 bool needs_special_offset_handling,
1488 size_t local_symbol_count,
1489 const unsigned char* plocal_symbols)
1490{
1491 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 1492 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6d03d481 1493
41cbeecc 1494 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3ff2ccb0 1495 typename Target_powerpc::Relocatable_size_for_reloc>(
6d03d481
ST
1496 symtab,
1497 layout,
1498 this,
1499 object,
1500 data_shndx,
1501 prelocs,
1502 reloc_count,
1503 output_section,
1504 needs_special_offset_handling,
1505 local_symbol_count,
1506 plocal_symbols);
1507}
1508
42cacb20
DE
1509// Scan relocations for a section.
1510
1511template<int size, bool big_endian>
1512void
1513Target_powerpc<size, big_endian>::scan_relocs(
42cacb20
DE
1514 Symbol_table* symtab,
1515 Layout* layout,
1516 Sized_relobj<size, big_endian>* object,
1517 unsigned int data_shndx,
1518 unsigned int sh_type,
1519 const unsigned char* prelocs,
1520 size_t reloc_count,
1521 Output_section* output_section,
1522 bool needs_special_offset_handling,
1523 size_t local_symbol_count,
1524 const unsigned char* plocal_symbols)
1525{
1526 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 1527 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
42cacb20
DE
1528 static Output_data_space* sdata;
1529
1530 if (sh_type == elfcpp::SHT_REL)
1531 {
1532 gold_error(_("%s: unsupported REL reloc section"),
1533 object->name().c_str());
1534 return;
1535 }
1536
1537 // Define _SDA_BASE_ at the start of the .sdata section.
1538 if (sdata == NULL)
1539 {
1540 // layout->find_output_section(".sdata") == NULL
1541 sdata = new Output_data_space(4, "** sdata");
1542 Output_section* os = layout->add_output_section_data(".sdata", 0,
1543 elfcpp::SHF_ALLOC
1544 | elfcpp::SHF_WRITE,
22f0da72
ILT
1545 sdata,
1546 ORDER_SMALL_DATA,
1547 false);
42cacb20 1548 symtab->define_in_output_data("_SDA_BASE_", NULL,
99fff23b 1549 Symbol_table::PREDEFINED,
42cacb20
DE
1550 os,
1551 32768, 0,
1552 elfcpp::STT_OBJECT,
1553 elfcpp::STB_LOCAL,
1554 elfcpp::STV_HIDDEN, 0,
1555 false, false);
1556 }
1557
2ea97941 1558 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
42cacb20
DE
1559 symtab,
1560 layout,
1561 this,
1562 object,
1563 data_shndx,
1564 prelocs,
1565 reloc_count,
1566 output_section,
1567 needs_special_offset_handling,
1568 local_symbol_count,
1569 plocal_symbols);
1570}
1571
1572// Finalize the sections.
1573
1574template<int size, bool big_endian>
1575void
d5b40221
DK
1576Target_powerpc<size, big_endian>::do_finalize_sections(
1577 Layout* layout,
f59f41f3
DK
1578 const Input_objects*,
1579 Symbol_table*)
42cacb20
DE
1580{
1581 // Fill in some more dynamic tags.
ea715a34
ILT
1582 const Reloc_section* rel_plt = (this->plt_ == NULL
1583 ? NULL
1584 : this->plt_->rel_plt());
1585 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
612a8d3d 1586 this->rela_dyn_, true, size == 32);
42cacb20
DE
1587
1588 // Emit any relocs we saved in an attempt to avoid generating COPY
1589 // relocs.
1590 if (this->copy_relocs_.any_saved_relocs())
1591 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1592}
1593
1594// Perform a relocation.
1595
1596template<int size, bool big_endian>
1597inline bool
1598Target_powerpc<size, big_endian>::Relocate::relocate(
1599 const Relocate_info<size, big_endian>* relinfo,
1600 Target_powerpc* target,
031cdbed 1601 Output_section*,
42cacb20
DE
1602 size_t relnum,
1603 const elfcpp::Rela<size, big_endian>& rela,
1604 unsigned int r_type,
1605 const Sized_symbol<size>* gsym,
1606 const Symbol_value<size>* psymval,
1607 unsigned char* view,
1608 typename elfcpp::Elf_types<size>::Elf_Addr address,
1609 section_size_type /* view_size */)
1610{
1611 const unsigned int toc_base_offset = 0x8000;
1612 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1613
1614 // Pick the value to use for symbols defined in shared objects.
1615 Symbol_value<size> symval;
1616 if (gsym != NULL
de4c45bd
ILT
1617 && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1618 || r_type == elfcpp::R_PPC_LOCAL24PC
1619 || r_type == elfcpp::R_PPC_REL16
1620 || r_type == elfcpp::R_PPC_REL16_LO
1621 || r_type == elfcpp::R_PPC_REL16_HI
1622 || r_type == elfcpp::R_PPC_REL16_HA))
42cacb20
DE
1623 {
1624 elfcpp::Elf_Xword value;
1625
1626 value = target->plt_section()->address() + gsym->plt_offset();
1627
1628 symval.set_output_value(value);
1629
1630 psymval = &symval;
1631 }
1632
1633 const Sized_relobj<size, big_endian>* object = relinfo->object;
1634 elfcpp::Elf_Xword addend = rela.get_r_addend();
1635
1636 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1637 // pointer points to the beginning, not the end, of the table.
1638 // So we just use the plain offset.
42cacb20
DE
1639 unsigned int got_offset = 0;
1640 unsigned int got2_offset = 0;
1641 switch (r_type)
1642 {
1643 case elfcpp::R_PPC64_TOC16:
1644 case elfcpp::R_PPC64_TOC16_LO:
1645 case elfcpp::R_PPC64_TOC16_HI:
1646 case elfcpp::R_PPC64_TOC16_HA:
1647 case elfcpp::R_PPC64_TOC16_DS:
1648 case elfcpp::R_PPC64_TOC16_LO_DS:
1649 // Subtract the TOC base address.
1650 addend -= target->toc_section()->address() + toc_base_offset;
1651 /* FALLTHRU */
1652
1653 case elfcpp::R_POWERPC_GOT16:
1654 case elfcpp::R_POWERPC_GOT16_LO:
1655 case elfcpp::R_POWERPC_GOT16_HI:
1656 case elfcpp::R_POWERPC_GOT16_HA:
1657 case elfcpp::R_PPC64_GOT16_DS:
1658 case elfcpp::R_PPC64_GOT16_LO_DS:
1659 if (gsym != NULL)
1660 {
1661 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1662 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1663 }
1664 else
1665 {
1666 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1667 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1668 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1669 }
42cacb20
DE
1670 break;
1671
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)
1676 {
1677 Output_data_space* got2;
1678 got2 = target->got2_section();
1679 got2_offset = got2->offset();
1680 addend += got2_offset;
1681 }
42cacb20
DE
1682 break;
1683
1684 default:
1685 break;
1686 }
1687
1688 switch (r_type)
1689 {
1690 case elfcpp::R_POWERPC_NONE:
1691 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1692 case elfcpp::R_POWERPC_GNU_VTENTRY:
1693 break;
1694
1695 case elfcpp::R_POWERPC_REL32:
1696 Reloc::rel32(view, object, psymval, addend, address);
1697 break;
1698
1699 case elfcpp::R_POWERPC_REL24:
1700 Reloc::rel24(view, object, psymval, addend, address);
1701 break;
1702
1703 case elfcpp::R_POWERPC_REL14:
1704 Reloc::rel14(view, object, psymval, addend, address);
1705 break;
1706
1707 case elfcpp::R_PPC_PLTREL24:
1708 Reloc::rel24(view, object, psymval, addend, address);
1709 break;
1710
1711 case elfcpp::R_PPC_LOCAL24PC:
1712 Reloc::rel24(view, object, psymval, addend, address);
1713 break;
1714
1715 case elfcpp::R_PPC64_ADDR64:
1716 if (!parameters->options().output_is_position_independent())
1717 Relocate_functions<size, big_endian>::rela64(view, object,
1718 psymval, addend);
1719 break;
1720
1721 case elfcpp::R_POWERPC_ADDR32:
1722 if (!parameters->options().output_is_position_independent())
1723 Relocate_functions<size, big_endian>::rela32(view, object,
1724 psymval, addend);
1725 break;
1726
1727 case elfcpp::R_POWERPC_ADDR16_LO:
1728 Reloc::addr16_lo(view, object, psymval, addend);
1729 break;
1730
1731 case elfcpp::R_POWERPC_ADDR16_HI:
1732 Reloc::addr16_hi(view, object, psymval, addend);
1733 break;
1734
1735 case elfcpp::R_POWERPC_ADDR16_HA:
1736 Reloc::addr16_ha(view, object, psymval, addend);
1737 break;
1738
1739 case elfcpp::R_PPC_REL16_LO:
1740 Reloc::rel16_lo(view, object, psymval, addend, address);
1741 break;
1742
1743 case elfcpp::R_PPC_REL16_HI:
1744 Reloc::rel16_lo(view, object, psymval, addend, address);
1745 break;
1746
1747 case elfcpp::R_PPC_REL16_HA:
83d22aa8 1748 Reloc::rel16_ha(view, object, psymval, addend, address);
42cacb20
DE
1749 break;
1750
1751 case elfcpp::R_POWERPC_GOT16:
1752 Reloc::addr16(view, got_offset, addend);
1753 break;
1754
1755 case elfcpp::R_POWERPC_GOT16_LO:
1756 Reloc::addr16_lo(view, got_offset, addend);
1757 break;
1758
1759 case elfcpp::R_POWERPC_GOT16_HI:
1760 Reloc::addr16_hi(view, got_offset, addend);
1761 break;
1762
1763 case elfcpp::R_POWERPC_GOT16_HA:
1764 Reloc::addr16_ha(view, got_offset, addend);
1765 break;
1766
1767 case elfcpp::R_PPC64_TOC16:
1768 Reloc::addr16(view, got_offset, addend);
1769 break;
1770
1771 case elfcpp::R_PPC64_TOC16_LO:
1772 Reloc::addr16_lo(view, got_offset, addend);
1773 break;
1774
1775 case elfcpp::R_PPC64_TOC16_HI:
1776 Reloc::addr16_hi(view, got_offset, addend);
1777 break;
1778
1779 case elfcpp::R_PPC64_TOC16_HA:
1780 Reloc::addr16_ha(view, got_offset, addend);
1781 break;
1782
1783 case elfcpp::R_PPC64_TOC16_DS:
1784 case elfcpp::R_PPC64_TOC16_LO_DS:
1785 Reloc::addr16_ds(view, got_offset, addend);
1786 break;
1787
1788 case elfcpp::R_PPC64_TOC:
1789 {
1790 elfcpp::Elf_types<64>::Elf_Addr value;
1791 value = target->toc_section()->address() + toc_base_offset;
1792 Relocate_functions<64, false>::rela64(view, value, addend);
1793 }
1794 break;
1795
1796 case elfcpp::R_POWERPC_COPY:
1797 case elfcpp::R_POWERPC_GLOB_DAT:
1798 case elfcpp::R_POWERPC_JMP_SLOT:
1799 case elfcpp::R_POWERPC_RELATIVE:
1800 // This is an outstanding tls reloc, which is unexpected when
1801 // linking.
1802 case elfcpp::R_POWERPC_DTPMOD:
1803 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1804 _("unexpected reloc %u in object file"),
1805 r_type);
1806 break;
1807
1808 default:
1809 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1810 _("unsupported reloc %u"),
1811 r_type);
1812 break;
1813 }
1814
1815 return true;
1816}
1817
1818// Perform a TLS relocation.
1819
1820template<int size, bool big_endian>
1821inline void
1822Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1823 const Relocate_info<size, big_endian>* relinfo,
1824 Target_powerpc<size, big_endian>* target,
1825 size_t relnum,
1826 const elfcpp::Rela<size, big_endian>& rela,
1827 unsigned int r_type,
1828 const Sized_symbol<size>* gsym,
1829 const Symbol_value<size>* psymval,
1830 unsigned char* view,
1831 typename elfcpp::Elf_types<size>::Elf_Addr address,
1832 section_size_type)
1833{
1834 Output_segment* tls_segment = relinfo->layout->tls_segment();
1835 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1836 const Sized_relobj<size, big_endian>* object = relinfo->object;
1837
1838 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1839 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1840
1841 const bool is_final =
1842 (gsym == NULL
1843 ? !parameters->options().output_is_position_independent()
1844 : gsym->final_value_is_known());
1845 const tls::Tls_optimization optimized_type
1846 = optimize_tls_reloc(is_final, r_type);
1847
1848 switch (r_type)
1849 {
1850 // XXX
1851 }
1852}
1853
1854// Relocate section data.
1855
1856template<int size, bool big_endian>
1857void
1858Target_powerpc<size, big_endian>::relocate_section(
1859 const Relocate_info<size, big_endian>* relinfo,
1860 unsigned int sh_type,
1861 const unsigned char* prelocs,
1862 size_t reloc_count,
1863 Output_section* output_section,
1864 bool needs_special_offset_handling,
1865 unsigned char* view,
1866 typename elfcpp::Elf_types<size>::Elf_Addr address,
364c7fa5
ILT
1867 section_size_type view_size,
1868 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
1869{
1870 typedef Target_powerpc<size, big_endian> Powerpc;
1871 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1872
1873 gold_assert(sh_type == elfcpp::SHT_RELA);
1874
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,
364c7fa5
ILT
1885 view_size,
1886 reloc_symbol_changes);
42cacb20
DE
1887}
1888
1889// Return the size of a relocation while scanning during a relocatable
1890// link.
1891
1892template<int size, bool big_endian>
1893unsigned int
1894Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1895 unsigned int,
1896 Relobj*)
1897{
1898 // We are always SHT_RELA, so we should never get here.
1899 gold_unreachable();
1900 return 0;
1901}
1902
1903// Scan the relocs during a relocatable link.
1904
1905template<int size, bool big_endian>
1906void
1907Target_powerpc<size, big_endian>::scan_relocatable_relocs(
42cacb20
DE
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)
1920{
1921 gold_assert(sh_type == elfcpp::SHT_RELA);
1922
1923 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1924 Relocatable_size_for_reloc> Scan_relocatable_relocs;
1925
1926 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1927 Scan_relocatable_relocs>(
42cacb20
DE
1928 symtab,
1929 layout,
1930 object,
1931 data_shndx,
1932 prelocs,
1933 reloc_count,
1934 output_section,
1935 needs_special_offset_handling,
1936 local_symbol_count,
1937 plocal_symbols,
1938 rr);
1939}
1940
1941// Relocate a section during a relocatable link.
1942
1943template<int size, bool big_endian>
1944void
1945Target_powerpc<size, big_endian>::relocate_for_relocatable(
1946 const Relocate_info<size, big_endian>* relinfo,
1947 unsigned int sh_type,
1948 const unsigned char* prelocs,
1949 size_t reloc_count,
1950 Output_section* output_section,
1951 off_t offset_in_output_section,
1952 const Relocatable_relocs* rr,
1953 unsigned char* view,
1954 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1955 section_size_type view_size,
1956 unsigned char* reloc_view,
1957 section_size_type reloc_view_size)
1958{
1959 gold_assert(sh_type == elfcpp::SHT_RELA);
1960
1961 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1962 relinfo,
1963 prelocs,
1964 reloc_count,
1965 output_section,
1966 offset_in_output_section,
1967 rr,
1968 view,
1969 view_address,
1970 view_size,
1971 reloc_view,
1972 reloc_view_size);
1973}
1974
1975// Return the value to use for a dynamic which requires special
1976// treatment. This is how we support equality comparisons of function
1977// pointers across shared library boundaries, as described in the
1978// processor specific ABI supplement.
1979
1980template<int size, bool big_endian>
1981uint64_t
1982Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1983{
1984 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1985 return this->plt_section()->address() + gsym->plt_offset();
1986}
1987
1988// The selector for powerpc object files.
1989
1990template<int size, bool big_endian>
1991class Target_selector_powerpc : public Target_selector
1992{
1993public:
1994 Target_selector_powerpc()
1995 : Target_selector(elfcpp::EM_NONE, size, big_endian,
1996 (size == 64 ?
1997 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
1998 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
1999 { }
2000
2ea97941 2001 Target* do_recognize(int machine, int, int)
42cacb20
DE
2002 {
2003 switch (size)
2004 {
2005 case 64:
2ea97941 2006 if (machine != elfcpp::EM_PPC64)
42cacb20
DE
2007 return NULL;
2008 break;
2009
2010 case 32:
2ea97941 2011 if (machine != elfcpp::EM_PPC)
42cacb20
DE
2012 return NULL;
2013 break;
2014
2015 default:
2016 return NULL;
2017 }
2018
7f055c20 2019 return this->instantiate_target();
42cacb20
DE
2020 }
2021
2022 Target* do_instantiate_target()
7f055c20 2023 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
2024};
2025
2026Target_selector_powerpc<32, true> target_selector_ppc32;
2027Target_selector_powerpc<32, false> target_selector_ppc32le;
2028Target_selector_powerpc<64, true> target_selector_ppc64;
2029Target_selector_powerpc<64, false> target_selector_ppc64le;
2030
2031} // End anonymous namespace.
This page took 0.222256 seconds and 4 git commands to generate.