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