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