gdb/
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
2e702c99 3// Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
42cacb20
DE
4// Written by David S. Miller <davem@davemloft.net>
5// and David Edelsohn <edelsohn@gnu.org>
6
7// This file is part of gold.
8
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 3 of the License, or
12// (at your option) any later version.
13
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22// MA 02110-1301, USA.
23
24#include "gold.h"
25
26#include "elfcpp.h"
27#include "parameters.h"
28#include "reloc.h"
29#include "powerpc.h"
30#include "object.h"
31#include "symtab.h"
32#include "layout.h"
33#include "output.h"
34#include "copy-relocs.h"
35#include "target.h"
36#include "target-reloc.h"
37#include "target-select.h"
38#include "tls.h"
39#include "errors.h"
f345227a 40#include "gc.h"
42cacb20
DE
41
42namespace
43{
44
45using namespace gold;
46
47template<int size, bool big_endian>
48class Output_data_plt_powerpc;
49
cf43a2fe
AM
50template<int size, bool big_endian>
51class Output_data_got_powerpc;
52
53template<int size, bool big_endian>
54class Output_data_glink;
55
56template<int size, bool big_endian>
57class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
58{
59public:
60 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
61 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
62 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
63 got2_section_(0)
64 { }
65
66 ~Powerpc_relobj()
67 { }
68
69 unsigned int
70 got2_shndx() const
71 {
72 if (size == 32)
73 return this->got2_section_;
74 else
75 return 0;
76 }
77
cf43a2fe
AM
78 bool
79 do_find_special_sections(Read_symbols_data* sd);
80
81private:
82 unsigned int got2_section_;
83};
84
42cacb20
DE
85template<int size, bool big_endian>
86class Target_powerpc : public Sized_target<size, big_endian>
87{
88 public:
d83ce4e3
AM
89 typedef
90 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
42cacb20
DE
91
92 Target_powerpc()
93 : Sized_target<size, big_endian>(&powerpc_info),
cf43a2fe 94 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
42cacb20
DE
95 copy_relocs_(elfcpp::R_POWERPC_COPY),
96 dynbss_(NULL), got_mod_index_offset_(-1U)
97 {
98 }
99
2e702c99 100 // Process the relocations to determine unreferenced sections for
6d03d481
ST
101 // garbage collection.
102 void
ad0f2072 103 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
104 Layout* layout,
105 Sized_relobj_file<size, big_endian>* object,
106 unsigned int data_shndx,
107 unsigned int sh_type,
108 const unsigned char* prelocs,
109 size_t reloc_count,
110 Output_section* output_section,
111 bool needs_special_offset_handling,
112 size_t local_symbol_count,
113 const unsigned char* plocal_symbols);
6d03d481 114
42cacb20
DE
115 // Scan the relocations to look for symbol adjustments.
116 void
ad0f2072 117 scan_relocs(Symbol_table* symtab,
42cacb20 118 Layout* layout,
6fa2a40b 119 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
120 unsigned int data_shndx,
121 unsigned int sh_type,
122 const unsigned char* prelocs,
123 size_t reloc_count,
124 Output_section* output_section,
125 bool needs_special_offset_handling,
126 size_t local_symbol_count,
127 const unsigned char* plocal_symbols);
921b5322
AM
128
129 // Map input .toc section to output .got section.
130 const char*
131 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
132 {
133 if (size == 64 && strcmp(name, ".toc") == 0)
134 {
135 *plen = 4;
136 return ".got";
137 }
138 return NULL;
139 }
140
42cacb20
DE
141 // Finalize the sections.
142 void
f59f41f3 143 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
144
145 // Return the value to use for a dynamic which requires special
146 // treatment.
147 uint64_t
148 do_dynsym_value(const Symbol*) const;
149
150 // Relocate a section.
151 void
152 relocate_section(const Relocate_info<size, big_endian>*,
153 unsigned int sh_type,
154 const unsigned char* prelocs,
155 size_t reloc_count,
156 Output_section* output_section,
157 bool needs_special_offset_handling,
158 unsigned char* view,
159 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
364c7fa5
ILT
160 section_size_type view_size,
161 const Reloc_symbol_changes*);
42cacb20
DE
162
163 // Scan the relocs during a relocatable link.
164 void
ad0f2072 165 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 166 Layout* layout,
6fa2a40b 167 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
168 unsigned int data_shndx,
169 unsigned int sh_type,
170 const unsigned char* prelocs,
171 size_t reloc_count,
172 Output_section* output_section,
173 bool needs_special_offset_handling,
174 size_t local_symbol_count,
175 const unsigned char* plocal_symbols,
176 Relocatable_relocs*);
177
178 // Relocate a section during a relocatable link.
179 void
180 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
181 unsigned int sh_type,
182 const unsigned char* prelocs,
183 size_t reloc_count,
184 Output_section* output_section,
185 off_t offset_in_output_section,
186 const Relocatable_relocs*,
cf43a2fe
AM
187 unsigned char*,
188 typename elfcpp::Elf_types<size>::Elf_Addr,
189 section_size_type,
42cacb20
DE
190 unsigned char* reloc_view,
191 section_size_type reloc_view_size);
192
193 // Return whether SYM is defined by the ABI.
194 bool
9c2d0ef9 195 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 196 {
cf43a2fe 197 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
198 }
199
200 // Return the size of the GOT section.
201 section_size_type
0e70b911 202 got_size() const
42cacb20
DE
203 {
204 gold_assert(this->got_ != NULL);
205 return this->got_->data_size();
206 }
207
cf43a2fe
AM
208 // Get the PLT section.
209 const Output_data_plt_powerpc<size, big_endian>*
210 plt_section() const
211 {
212 gold_assert(this->plt_ != NULL);
213 return this->plt_;
214 }
215
216 // Get the .glink section.
217 const Output_data_glink<size, big_endian>*
218 glink_section() const
219 {
220 gold_assert(this->glink_ != NULL);
221 return this->glink_;
222 }
223
224 // Get the GOT section.
225 const Output_data_got_powerpc<size, big_endian>*
226 got_section() const
227 {
228 gold_assert(this->got_ != NULL);
229 return this->got_;
230 }
231
232 protected:
233 Object*
234 do_make_elf_object(const std::string&, Input_file*, off_t,
235 const elfcpp::Ehdr<size, big_endian>&);
236
0e70b911
CC
237 // Return the number of entries in the GOT.
238 unsigned int
239 got_entry_count() const
240 {
241 if (this->got_ == NULL)
242 return 0;
243 return this->got_size() / (size / 8);
244 }
245
246 // Return the number of entries in the PLT.
247 unsigned int
248 plt_entry_count() const;
249
250 // Return the offset of the first non-reserved PLT entry.
251 unsigned int
252 first_plt_entry_offset() const;
253
254 // Return the size of each PLT entry.
255 unsigned int
256 plt_entry_size() const;
257
42cacb20
DE
258 private:
259
260 // The class which scans relocations.
261 class Scan
262 {
263 public:
264 Scan()
265 : issued_non_pic_error_(false)
266 { }
267
95a2c8d6
RS
268 static inline int
269 get_reference_flags(unsigned int r_type);
270
42cacb20 271 inline void
ad0f2072 272 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 273 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
274 unsigned int data_shndx,
275 Output_section* output_section,
276 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
277 const elfcpp::Sym<size, big_endian>& lsym);
278
279 inline void
ad0f2072 280 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 281 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
282 unsigned int data_shndx,
283 Output_section* output_section,
284 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
285 Symbol* gsym);
286
21bb3914
ST
287 inline bool
288 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
289 Target_powerpc* ,
2e702c99 290 Sized_relobj_file<size, big_endian>* ,
21bb3914 291 unsigned int ,
2e702c99
RM
292 Output_section* ,
293 const elfcpp::Rela<size, big_endian>& ,
294 unsigned int ,
295 const elfcpp::Sym<size, big_endian>&)
21bb3914
ST
296 { return false; }
297
298 inline bool
299 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
300 Target_powerpc* ,
2e702c99
RM
301 Sized_relobj_file<size, big_endian>* ,
302 unsigned int ,
303 Output_section* ,
304 const elfcpp::Rela<size,
21bb3914
ST
305 big_endian>& ,
306 unsigned int , Symbol*)
307 { return false; }
308
42cacb20
DE
309 private:
310 static void
6fa2a40b 311 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
312 unsigned int r_type);
313
314 static void
6fa2a40b 315 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
316 unsigned int r_type, Symbol*);
317
318 static void
319 generate_tls_call(Symbol_table* symtab, Layout* layout,
320 Target_powerpc* target);
321
322 void
323 check_non_pic(Relobj*, unsigned int r_type);
324
325 // Whether we have issued an error about a non-PIC compilation.
326 bool issued_non_pic_error_;
327 };
328
329 // The class which implements relocation.
330 class Relocate
331 {
332 public:
333 // Do a relocation. Return false if the caller should not issue
334 // any warnings about this relocation.
335 inline bool
336 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
031cdbed
ILT
337 Output_section*, size_t relnum,
338 const elfcpp::Rela<size, big_endian>&,
42cacb20
DE
339 unsigned int r_type, const Sized_symbol<size>*,
340 const Symbol_value<size>*,
341 unsigned char*,
342 typename elfcpp::Elf_types<size>::Elf_Addr,
343 section_size_type);
344
345 private:
346 // Do a TLS relocation.
347 inline void
348 relocate_tls(const Relocate_info<size, big_endian>*,
349 Target_powerpc* target,
2e702c99 350 size_t relnum, const elfcpp::Rela<size, big_endian>&,
42cacb20
DE
351 unsigned int r_type, const Sized_symbol<size>*,
352 const Symbol_value<size>*,
353 unsigned char*,
354 typename elfcpp::Elf_types<size>::Elf_Addr,
355 section_size_type);
356 };
357
358 // A class which returns the size required for a relocation type,
359 // used while scanning relocs during a relocatable link.
360 class Relocatable_size_for_reloc
361 {
362 public:
363 unsigned int
cf43a2fe
AM
364 get_size_for_reloc(unsigned int, Relobj*)
365 {
366 gold_unreachable();
367 return 0;
368 }
42cacb20
DE
369 };
370
cf43a2fe
AM
371 // Adjust TLS relocation type based on the options and whether this
372 // is a local symbol.
373 static tls::Tls_optimization
374 optimize_tls_reloc(bool is_final, int r_type);
375
42cacb20 376 // Get the GOT section, creating it if necessary.
cf43a2fe 377 Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
378 got_section(Symbol_table*, Layout*);
379
cf43a2fe
AM
380 // Create glink.
381 void
382 make_glink_section(Layout*);
42cacb20 383
cf43a2fe
AM
384 // Create the PLT section.
385 void
386 make_plt_section(Layout*);
42cacb20
DE
387
388 // Create a PLT entry for a global symbol.
389 void
cf43a2fe
AM
390 make_plt_entry(Layout*, Symbol*,
391 const elfcpp::Rela<size, big_endian>&,
392 const Sized_relobj<size, big_endian>* object);
42cacb20
DE
393
394 // Create a GOT entry for the TLS module index.
395 unsigned int
396 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 397 Sized_relobj_file<size, big_endian>* object);
42cacb20 398
42cacb20
DE
399 // Get the dynamic reloc section, creating it if necessary.
400 Reloc_section*
401 rela_dyn_section(Layout*);
402
42cacb20
DE
403 // Copy a relocation against a global symbol.
404 void
ef9beddf 405 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 406 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
407 unsigned int shndx, Output_section* output_section,
408 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
409 {
410 this->copy_relocs_.copy_reloc(symtab, layout,
411 symtab->get_sized_symbol<size>(sym),
412 object, shndx, output_section,
413 reloc, this->rela_dyn_section(layout));
414 }
415
416 // Information about this specific target which we pass to the
417 // general Target structure.
418 static Target::Target_info powerpc_info;
419
420 // The types of GOT entries needed for this platform.
0e70b911
CC
421 // These values are exposed to the ABI in an incremental link.
422 // Do not renumber existing values without changing the version
423 // number of the .gnu_incremental_inputs section.
42cacb20
DE
424 enum Got_type
425 {
426 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
427 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
428 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
429 };
430
cf43a2fe
AM
431 // The GOT output section.
432 Output_data_got_powerpc<size, big_endian>* got_;
433 // The PLT output section.
42cacb20 434 Output_data_plt_powerpc<size, big_endian>* plt_;
cf43a2fe
AM
435 // The .glink output section.
436 Output_data_glink<size, big_endian>* glink_;
437 // The dynamic reloc output section.
42cacb20
DE
438 Reloc_section* rela_dyn_;
439 // Relocs saved to avoid a COPY reloc.
440 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
441 // Space for variables copied with a COPY reloc.
442 Output_data_space* dynbss_;
443 // Offset of the GOT entry for the TLS module index;
444 unsigned int got_mod_index_offset_;
445};
446
447template<>
448Target::Target_info Target_powerpc<32, true>::powerpc_info =
449{
450 32, // size
451 true, // is_big_endian
452 elfcpp::EM_PPC, // machine_code
453 false, // has_make_symbol
454 false, // has_resolve
455 false, // has_code_fill
456 true, // is_default_stack_executable
b3ce541e 457 false, // can_icf_inline_merge_sections
42cacb20
DE
458 '\0', // wrap_char
459 "/usr/lib/ld.so.1", // dynamic_linker
460 0x10000000, // default_text_segment_address
461 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 462 4 * 1024, // common_pagesize (overridable by -z common-page-size)
2e702c99
RM
463 false, // isolate_execinstr
464 0, // rosegment_gap
8a5e3e08
ILT
465 elfcpp::SHN_UNDEF, // small_common_shndx
466 elfcpp::SHN_UNDEF, // large_common_shndx
467 0, // small_common_section_flags
05a352e6
DK
468 0, // large_common_section_flags
469 NULL, // attributes_section
470 NULL // attributes_vendor
42cacb20
DE
471};
472
473template<>
474Target::Target_info Target_powerpc<32, false>::powerpc_info =
475{
476 32, // size
477 false, // is_big_endian
478 elfcpp::EM_PPC, // machine_code
479 false, // has_make_symbol
480 false, // has_resolve
481 false, // has_code_fill
482 true, // is_default_stack_executable
b3ce541e 483 false, // can_icf_inline_merge_sections
42cacb20
DE
484 '\0', // wrap_char
485 "/usr/lib/ld.so.1", // dynamic_linker
486 0x10000000, // default_text_segment_address
487 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 488 4 * 1024, // common_pagesize (overridable by -z common-page-size)
2e702c99
RM
489 false, // isolate_execinstr
490 0, // rosegment_gap
8a5e3e08
ILT
491 elfcpp::SHN_UNDEF, // small_common_shndx
492 elfcpp::SHN_UNDEF, // large_common_shndx
493 0, // small_common_section_flags
05a352e6
DK
494 0, // large_common_section_flags
495 NULL, // attributes_section
496 NULL // attributes_vendor
42cacb20
DE
497};
498
499template<>
500Target::Target_info Target_powerpc<64, true>::powerpc_info =
501{
502 64, // size
503 true, // is_big_endian
504 elfcpp::EM_PPC64, // machine_code
505 false, // has_make_symbol
506 false, // has_resolve
507 false, // has_code_fill
508 true, // is_default_stack_executable
b3ce541e 509 false, // can_icf_inline_merge_sections
42cacb20
DE
510 '\0', // wrap_char
511 "/usr/lib/ld.so.1", // dynamic_linker
512 0x10000000, // default_text_segment_address
513 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 514 8 * 1024, // common_pagesize (overridable by -z common-page-size)
2e702c99
RM
515 false, // isolate_execinstr
516 0, // rosegment_gap
8a5e3e08
ILT
517 elfcpp::SHN_UNDEF, // small_common_shndx
518 elfcpp::SHN_UNDEF, // large_common_shndx
519 0, // small_common_section_flags
05a352e6
DK
520 0, // large_common_section_flags
521 NULL, // attributes_section
522 NULL // attributes_vendor
42cacb20
DE
523};
524
525template<>
526Target::Target_info Target_powerpc<64, false>::powerpc_info =
527{
528 64, // size
529 false, // is_big_endian
530 elfcpp::EM_PPC64, // machine_code
531 false, // has_make_symbol
532 false, // has_resolve
533 false, // has_code_fill
534 true, // is_default_stack_executable
b3ce541e 535 false, // can_icf_inline_merge_sections
42cacb20
DE
536 '\0', // wrap_char
537 "/usr/lib/ld.so.1", // dynamic_linker
538 0x10000000, // default_text_segment_address
539 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 540 8 * 1024, // common_pagesize (overridable by -z common-page-size)
2e702c99
RM
541 false, // isolate_execinstr
542 0, // rosegment_gap
8a5e3e08
ILT
543 elfcpp::SHN_UNDEF, // small_common_shndx
544 elfcpp::SHN_UNDEF, // large_common_shndx
545 0, // small_common_section_flags
05a352e6
DK
546 0, // large_common_section_flags
547 NULL, // attributes_section
548 NULL // attributes_vendor
42cacb20
DE
549};
550
551template<int size, bool big_endian>
552class Powerpc_relocate_functions
553{
554private:
cf43a2fe 555 // Do a simple RELA relocation
42cacb20
DE
556 template<int valsize>
557 static inline void
558 rela(unsigned char* view,
559 unsigned int right_shift,
560 elfcpp::Elf_Xword dst_mask,
561 typename elfcpp::Swap<size, big_endian>::Valtype value,
562 typename elfcpp::Swap<size, big_endian>::Valtype addend)
563 {
564 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
565 Valtype* wv = reinterpret_cast<Valtype*>(view);
566 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
cf43a2fe 567 Valtype reloc = (value + addend) >> right_shift;
42cacb20
DE
568
569 val &= ~dst_mask;
570 reloc &= dst_mask;
571
572 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
573 }
574
cf43a2fe 575 // Do a simple RELA relocation, unaligned.
42cacb20
DE
576 template<int valsize>
577 static inline void
cf43a2fe
AM
578 rela_ua(unsigned char* view,
579 unsigned int right_shift,
42cacb20 580 elfcpp::Elf_Xword dst_mask,
cf43a2fe 581 typename elfcpp::Swap<size, big_endian>::Valtype value,
42cacb20
DE
582 typename elfcpp::Swap<size, big_endian>::Valtype addend)
583 {
584 typedef typename elfcpp::Swap_unaligned<valsize,
cf43a2fe 585 big_endian>::Valtype Valtype;
42cacb20
DE
586 Valtype* wv = reinterpret_cast<Valtype*>(view);
587 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
cf43a2fe 588 Valtype reloc = (value + addend) >> right_shift;
42cacb20
DE
589
590 val &= ~dst_mask;
591 reloc &= dst_mask;
592
cf43a2fe 593 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
42cacb20
DE
594 }
595
596 typedef Powerpc_relocate_functions<size, big_endian> This;
597 typedef Relocate_functions<size, big_endian> This_reloc;
598public:
599 // R_POWERPC_REL32: (Symbol + Addend - Address)
600 static inline void
601 rel32(unsigned char* view,
cf43a2fe 602 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
603 typename elfcpp::Elf_types<size>::Elf_Addr addend,
604 typename elfcpp::Elf_types<size>::Elf_Addr address)
cf43a2fe 605 { This_reloc::pcrela32(view, value, addend, address); }
42cacb20
DE
606
607 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
608 static inline void
609 rel24(unsigned char* view,
cf43a2fe 610 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
611 typename elfcpp::Elf_types<size>::Elf_Addr addend,
612 typename elfcpp::Elf_types<size>::Elf_Addr address)
613 {
cf43a2fe 614 This::template rela<32>(view, 0, 0x03fffffc, value - address, addend);
42cacb20
DE
615 }
616
617 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
618 static inline void
619 rel14(unsigned char* view,
cf43a2fe 620 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
621 typename elfcpp::Elf_types<size>::Elf_Addr addend,
622 typename elfcpp::Elf_types<size>::Elf_Addr address)
623 {
cf43a2fe 624 This::template rela<32>(view, 0, 0xfffc, value - address, addend);
42cacb20
DE
625 }
626
627 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
628 static inline void
629 addr16(unsigned char* view,
630 typename elfcpp::Elf_types<size>::Elf_Addr value,
631 typename elfcpp::Elf_types<size>::Elf_Addr addend)
632 { This_reloc::rela16(view, value, addend); }
633
42cacb20
DE
634 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
635 static inline void
636 addr16_ds(unsigned char* view,
637 typename elfcpp::Elf_types<size>::Elf_Addr value,
638 typename elfcpp::Elf_types<size>::Elf_Addr addend)
639 {
640 This::template rela<16>(view, 0, 0xfffc, value, addend);
641 }
642
643 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
644 static inline void
645 addr16_lo(unsigned char* view,
646 typename elfcpp::Elf_types<size>::Elf_Addr value,
647 typename elfcpp::Elf_types<size>::Elf_Addr addend)
648 { This_reloc::rela16(view, value, addend); }
649
42cacb20
DE
650 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
651 static inline void
652 addr16_hi(unsigned char* view,
653 typename elfcpp::Elf_types<size>::Elf_Addr value,
654 typename elfcpp::Elf_types<size>::Elf_Addr addend)
655 {
656 This::template rela<16>(view, 16, 0xffff, value, addend);
657 }
658
42cacb20
DE
659 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
660 // final value of the low 16 bits of the
661 // relocation is negative, add one.
662 static inline void
663 addr16_ha(unsigned char* view,
664 typename elfcpp::Elf_types<size>::Elf_Addr value,
665 typename elfcpp::Elf_types<size>::Elf_Addr addend)
666 {
cf43a2fe 667 This::addr16_hi(view, value + 0x8000, addend);
42cacb20
DE
668 }
669
6ce78956 670 // R_POWERPC_REL16: (Symbol + Addend - Address) & 0xffff
42cacb20
DE
671 static inline void
672 rel16(unsigned char* view,
cf43a2fe 673 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
674 typename elfcpp::Elf_types<size>::Elf_Addr addend,
675 typename elfcpp::Elf_types<size>::Elf_Addr address)
cf43a2fe 676 { This_reloc::pcrela16(view, value, addend, address); }
42cacb20 677
6ce78956 678 // R_POWERPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
42cacb20
DE
679 static inline void
680 rel16_lo(unsigned char* view,
cf43a2fe 681 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
682 typename elfcpp::Elf_types<size>::Elf_Addr addend,
683 typename elfcpp::Elf_types<size>::Elf_Addr address)
cf43a2fe 684 { This_reloc::pcrela16(view, value, addend, address); }
42cacb20 685
6ce78956 686 // R_POWERPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
42cacb20
DE
687 static inline void
688 rel16_hi(unsigned char* view,
cf43a2fe 689 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
690 typename elfcpp::Elf_types<size>::Elf_Addr addend,
691 typename elfcpp::Elf_types<size>::Elf_Addr address)
d83ce4e3 692 {
cf43a2fe 693 This::template rela<16>(view, 16, 0xffff, value - address, addend);
42cacb20
DE
694 }
695
6ce78956 696 // R_POWERPC_REL16_HA: Same as R_POWERPC_REL16_HI except that if the
42cacb20
DE
697 // final value of the low 16 bits of the
698 // relocation is negative, add one.
699 static inline void
700 rel16_ha(unsigned char* view,
cf43a2fe 701 typename elfcpp::Elf_types<size>::Elf_Addr value,
42cacb20
DE
702 typename elfcpp::Elf_types<size>::Elf_Addr addend,
703 typename elfcpp::Elf_types<size>::Elf_Addr address)
d83ce4e3 704 {
cf43a2fe
AM
705 This::rel16_hi(view, value + 0x8000, addend, address);
706 }
707};
708
709// Stash away the index of .got2 in a relocatable object, if such
710// a section exists.
711
712template<int size, bool big_endian>
713bool
714Powerpc_relobj<size, big_endian>::do_find_special_sections(
715 Read_symbols_data* sd)
716{
717 if (size == 32)
718 {
719 const unsigned char* const pshdrs = sd->section_headers->data();
720 const unsigned char* namesu = sd->section_names->data();
721 const char* names = reinterpret_cast<const char*>(namesu);
722 section_size_type names_size = sd->section_names_size;
723 const unsigned char* s;
724
725 s = this->find_shdr(pshdrs, ".got2", names, names_size, NULL);
726 if (s != NULL)
727 {
728 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
d1a8cabd 729 this->got2_section_ = ndx;
cf43a2fe
AM
730 }
731 }
732 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
733}
734
735// Set up PowerPC target specific relobj.
736
737template<int size, bool big_endian>
738Object*
739Target_powerpc<size, big_endian>::do_make_elf_object(
740 const std::string& name,
741 Input_file* input_file,
742 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
743{
744 int et = ehdr.get_e_type();
745 if (et == elfcpp::ET_REL)
746 {
747 Powerpc_relobj<size, big_endian>* obj =
748 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
749 obj->setup();
750 return obj;
751 }
752 else if (et == elfcpp::ET_DYN)
753 {
754 Sized_dynobj<size, big_endian>* obj =
755 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
756 obj->setup();
757 return obj;
758 }
759 else
760 {
761 gold_error(_("%s: unsupported ELF file type %d"),
762 name.c_str(), et);
763 return NULL;
764 }
765}
766
767template<int size, bool big_endian>
768class Output_data_got_powerpc : public Output_data_got<size, big_endian>
769{
770public:
771 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
772 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
773
774 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
775 : Output_data_got<size, big_endian>(),
776 symtab_(symtab), layout_(layout),
777 header_ent_cnt_(size == 32 ? 3 : 1),
778 header_index_(size == 32 ? 0x2000 : 0)
779 {}
780
781 class Got_entry;
782
783 // Create a new GOT entry and return its offset.
784 unsigned int
785 add_got_entry(Got_entry got_entry)
42cacb20 786 {
cf43a2fe
AM
787 this->reserve_ent();
788 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
789 }
42cacb20 790
cf43a2fe
AM
791 // Create a pair of new GOT entries and return the offset of the first.
792 unsigned int
793 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
794 {
795 this->reserve_ent(2);
796 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
797 got_entry_2);
798 }
42cacb20 799
cf43a2fe
AM
800 // Value of _GLOBAL_OFFSET_TABLE_
801 unsigned int
802 g_o_t() const
803 {
804 return this->got_offset(this->header_index_);
42cacb20 805 }
cf43a2fe
AM
806
807 // Ensure our GOT has a header.
808 void
809 set_final_data_size()
810 {
811 if (this->header_ent_cnt_ != 0)
812 this->make_header();
813 Output_data_got<size, big_endian>::set_final_data_size();
814 }
815
816 // First word of GOT header needs some values that are not
817 // handled by Output_data_got so poke them in here.
818 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
819 void
820 do_write(Output_file* of)
821 {
6ce78956
AM
822 this->replace_constant(this->header_index_,
823 (size == 32
824 ? this->layout_->dynamic_section()->address()
825 : this->address() + 0x8000));
cf43a2fe
AM
826
827 Output_data_got<size, big_endian>::do_write(of);
828 }
829
830private:
831 void
832 reserve_ent(unsigned int cnt = 1)
833 {
834 if (this->header_ent_cnt_ == 0)
835 return;
836 if (this->num_entries() + cnt > this->header_index_)
837 this->make_header();
838 }
839
840 void
841 make_header()
842 {
843 this->header_ent_cnt_ = 0;
844 this->header_index_ = this->num_entries();
845 if (size == 32)
846 {
847 Output_data_got<size, big_endian>::add_constant(0);
848 Output_data_got<size, big_endian>::add_constant(0);
849 Output_data_got<size, big_endian>::add_constant(0);
850
851 // Define _GLOBAL_OFFSET_TABLE_ at the header
852 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
853 Symbol_table::PREDEFINED,
854 this, this->g_o_t(), 0,
855 elfcpp::STT_OBJECT,
856 elfcpp::STB_LOCAL,
857 elfcpp::STV_HIDDEN,
858 0, false, false);
859 }
860 else
861 Output_data_got<size, big_endian>::add_constant(0);
862 }
863
864 // Stashed pointers.
865 Symbol_table* symtab_;
866 Layout* layout_;
867
868 // GOT header size.
869 unsigned int header_ent_cnt_;
870 // GOT header index.
871 unsigned int header_index_;
42cacb20
DE
872};
873
874// Get the GOT section, creating it if necessary.
875
876template<int size, bool big_endian>
cf43a2fe 877Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
878Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
879 Layout* layout)
880{
881 if (this->got_ == NULL)
882 {
883 gold_assert(symtab != NULL && layout != NULL);
884
cf43a2fe
AM
885 this->got_
886 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
887
888 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
889 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 890 this->got_, ORDER_DATA, false);
42cacb20
DE
891 }
892
893 return this->got_;
894}
895
896// Get the dynamic reloc section, creating it if necessary.
897
898template<int size, bool big_endian>
899typename Target_powerpc<size, big_endian>::Reloc_section*
900Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
901{
902 if (this->rela_dyn_ == NULL)
903 {
904 gold_assert(layout != NULL);
905 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
906 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
907 elfcpp::SHF_ALLOC, this->rela_dyn_,
908 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
909 }
910 return this->rela_dyn_;
911}
912
913// A class to handle the PLT data.
914
915template<int size, bool big_endian>
cf43a2fe 916class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
917{
918 public:
919 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
920 size, big_endian> Reloc_section;
921
cf43a2fe 922 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
42cacb20
DE
923
924 // Add an entry to the PLT.
cf43a2fe
AM
925 void
926 add_entry(Symbol*);
42cacb20
DE
927
928 // Return the .rela.plt section data.
cf43a2fe
AM
929 const Reloc_section*
930 rel_plt() const
931 {
42cacb20
DE
932 return this->rel_;
933 }
934
0e70b911
CC
935 // Return the number of PLT entries.
936 unsigned int
937 entry_count() const
d83ce4e3
AM
938 {
939 return ((this->current_data_size() - initial_plt_entry_size)
940 / plt_entry_size);
941 }
0e70b911
CC
942
943 // Return the offset of the first non-reserved PLT entry.
944 static unsigned int
945 first_plt_entry_offset()
cf43a2fe 946 { return initial_plt_entry_size; }
0e70b911
CC
947
948 // Return the size of a PLT entry.
949 static unsigned int
950 get_plt_entry_size()
cf43a2fe 951 { return plt_entry_size; }
0e70b911 952
42cacb20 953 protected:
42cacb20 954 void
cf43a2fe 955 do_adjust_output_section(Output_section* os)
42cacb20 956 {
cf43a2fe 957 os->set_entsize(0);
42cacb20
DE
958 }
959
6ce78956
AM
960 // Write to a map file.
961 void
962 do_print_to_mapfile(Mapfile* mapfile) const
963 { mapfile->print_output_data(this, _("** PLT")); }
964
cf43a2fe
AM
965 private:
966 // The size of an entry in the PLT.
967 static const int plt_entry_size = size == 32 ? 4 : 24;
968 // The size of the first reserved entry.
969 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
970
42cacb20
DE
971 // Write out the PLT data.
972 void
973 do_write(Output_file*);
974
975 // The reloc section.
976 Reloc_section* rel_;
cf43a2fe
AM
977 // Allows access to .glink for do_write.
978 Target_powerpc<size, big_endian>* targ_;
42cacb20
DE
979};
980
cf43a2fe 981// Create the PLT section.
42cacb20
DE
982
983template<int size, bool big_endian>
d83ce4e3
AM
984Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
985 Layout* layout,
986 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
987 : Output_section_data_build(size == 32 ? 4 : 8),
988 targ_(targ)
42cacb20
DE
989{
990 this->rel_ = new Reloc_section(false);
991 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
992 elfcpp::SHF_ALLOC, this->rel_,
993 ORDER_DYNAMIC_PLT_RELOCS, false);
42cacb20
DE
994}
995
42cacb20
DE
996// Add an entry to the PLT.
997
998template<int size, bool big_endian>
999void
1000Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1001{
cf43a2fe
AM
1002 if (!gsym->has_plt_offset())
1003 {
1004 off_t off = this->current_data_size();
1005
1006 if (off == 0)
1007 off += initial_plt_entry_size;
1008 gsym->set_plt_offset(off);
1009 gsym->set_needs_dynsym_entry();
1010 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1011 off += plt_entry_size;
1012 this->set_current_data_size(off);
1013 }
42cacb20
DE
1014}
1015
d83ce4e3
AM
1016static const uint32_t addis_11_11 = 0x3d6b0000;
1017static const uint32_t addis_11_30 = 0x3d7e0000;
1018static const uint32_t addis_12_12 = 0x3d8c0000;
1019static const uint32_t addi_11_11 = 0x396b0000;
1020static const uint32_t add_0_11_11 = 0x7c0b5a14;
1021static const uint32_t add_11_0_11 = 0x7d605a14;
1022static const uint32_t b = 0x48000000;
1023static const uint32_t bcl_20_31 = 0x429f0005;
1024static const uint32_t bctr = 0x4e800420;
1025static const uint32_t blrl = 0x4e800021;
1026static const uint32_t lis_11 = 0x3d600000;
1027static const uint32_t lis_12 = 0x3d800000;
1028static const uint32_t lwzu_0_12 = 0x840c0000;
1029static const uint32_t lwz_0_12 = 0x800c0000;
1030static const uint32_t lwz_11_11 = 0x816b0000;
1031static const uint32_t lwz_11_30 = 0x817e0000;
1032static const uint32_t lwz_12_12 = 0x818c0000;
1033static const uint32_t mflr_0 = 0x7c0802a6;
1034static const uint32_t mflr_12 = 0x7d8802a6;
1035static const uint32_t mtctr_0 = 0x7c0903a6;
1036static const uint32_t mtctr_11 = 0x7d6903a6;
1037static const uint32_t mtlr_0 = 0x7c0803a6;
1038static const uint32_t nop = 0x60000000;
1039static const uint32_t sub_11_11_12 = 0x7d6c5850;
1040static const uint32_t addis_12_2 = 0x3d820000;
1041static const uint32_t std_2_1 = 0xf8410000;
1042static const uint32_t ld_11_12 = 0xe96c0000;
1043static const uint32_t ld_2_12 = 0xe84c0000;
1044static const uint32_t addi_12_12 = 0x398c0000;
1045static const uint32_t ld_11_2 = 0xe9620000;
1046static const uint32_t addi_2_2 = 0x38420000;
1047static const uint32_t ld_2_2 = 0xe8420000;
1048static const uint32_t mflr_11 = 0x7d6802a6;
1049static const uint32_t ld_2_11 = 0xe84b0000;
1050static const uint32_t mtlr_12 = 0x7d8803a6;
1051static const uint32_t add_12_2_11 = 0x7d825a14;
1052static const uint32_t li_0_0 = 0x38000000;
1053static const uint32_t lis_0_0 = 0x3c000000;
1054static const uint32_t ori_0_0_0 = 0x60000000;
42cacb20
DE
1055
1056// Write out the PLT.
1057
1058template<int size, bool big_endian>
1059void
1060Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1061{
cf43a2fe
AM
1062 if (size == 32)
1063 {
1064 const off_t offset = this->offset();
1065 const section_size_type oview_size
1066 = convert_to_section_size_type(this->data_size());
1067 unsigned char* const oview = of->get_output_view(offset, oview_size);
1068 unsigned char* pov = oview;
1069 unsigned char* endpov = oview + oview_size;
1070
1071 // The address the .glink branch table
1072 const Output_data_glink<size, big_endian>* glink
1073 = this->targ_->glink_section();
1074 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1075 = glink->address() + glink->pltresolve();
1076
1077 while (pov < endpov)
1078 {
1079 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1080 pov += 4;
1081 branch_tab += 4;
1082 }
1083
1084 of->write_output_view(offset, oview_size, oview);
1085 }
1086}
1087
1088// Create the PLT section.
1089
1090template<int size, bool big_endian>
1091void
1092Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1093{
1094 if (this->plt_ == NULL)
1095 {
1096 if (this->glink_ == NULL)
1097 make_glink_section(layout);
1098
1099 // Ensure that .rela.dyn always appears before .rela.plt This is
1100 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1101 // needs to include .rela.plt in it's range.
1102 this->rela_dyn_section(layout);
1103
1104 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1105 layout->add_output_section_data(".plt",
1106 (size == 32
1107 ? elfcpp::SHT_PROGBITS
1108 : elfcpp::SHT_NOBITS),
1109 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1110 this->plt_,
1111 (size == 32
1112 ? ORDER_SMALL_DATA
1113 : ORDER_SMALL_BSS),
1114 false);
1115 }
1116}
1117
1118// A class to handle .glink.
1119
1120template<int size, bool big_endian>
1121class Output_data_glink : public Output_section_data
1122{
1123 public:
1124 Output_data_glink(Target_powerpc<size, big_endian>*);
1125
1126 // Add an entry
1127 void
1128 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1129 const Sized_relobj<size, big_endian>*);
cf43a2fe
AM
1130
1131 unsigned int
1132 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1133 const Sized_relobj<size, big_endian>*) const;
cf43a2fe
AM
1134
1135 unsigned int
1136 glink_entry_size() const
1137 {
1138 if (size == 32)
1139 return 4 * 4;
1140 else
1141 // FIXME: We should be using multiple glink sections for
1142 // stubs to support > 33M applications.
1143 return 8 * 4;
1144 }
1145
1146 off_t
1147 pltresolve() const
1148 {
1149 return this->pltresolve_;
1150 }
1151
6ce78956
AM
1152 protected:
1153 // Write to a map file.
1154 void
1155 do_print_to_mapfile(Mapfile* mapfile) const
1156 { mapfile->print_output_data(this, _("** glink")); }
1157
cf43a2fe
AM
1158 private:
1159 static const int pltresolve_size = 16*4;
1160
1161 void
1162 set_final_data_size();
1163
1164 // Write out .glink
1165 void
1166 do_write(Output_file*);
1167
d1a8cabd 1168 class Glink_sym_ent
cf43a2fe 1169 {
d1a8cabd
AM
1170 public:
1171 Glink_sym_ent(const Symbol* sym,
cf43a2fe 1172 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd
AM
1173 const Sized_relobj<size, big_endian>* object)
1174 : sym_(sym), object_(0), addend_(0)
cf43a2fe
AM
1175 {
1176 if (size != 32)
1177 this->addend_ = reloc.get_r_addend();
d1a8cabd
AM
1178 else if (parameters->options().output_is_position_independent()
1179 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1180 == elfcpp::R_PPC_PLTREL24))
cf43a2fe 1181 {
d1a8cabd 1182 this->addend_ = reloc.get_r_addend();
cf43a2fe 1183 if (this->addend_ != 0)
d1a8cabd 1184 this->object_ = object;
cf43a2fe
AM
1185 }
1186 }
1187
d1a8cabd 1188 const Symbol* sym_;
cf43a2fe 1189 const Sized_relobj<size, big_endian>* object_;
cf43a2fe
AM
1190 unsigned int addend_;
1191
1192 bool operator==(const Glink_sym_ent& that) const
1193 {
1194 return (this->sym_ == that.sym_
1195 && this->object_ == that.object_
d1a8cabd 1196 && this->addend_ == that.addend_);
cf43a2fe
AM
1197 }
1198 };
1199
d1a8cabd 1200 class Glink_sym_ent_hash
cf43a2fe 1201 {
d1a8cabd 1202 public:
cf43a2fe
AM
1203 size_t operator()(const Glink_sym_ent& ent) const
1204 {
1205 return (reinterpret_cast<uintptr_t>(ent.sym_)
1206 ^ reinterpret_cast<uintptr_t>(ent.object_)
cf43a2fe
AM
1207 ^ ent.addend_);
1208 }
1209 };
1210
d1a8cabd 1211 // Map sym/object/addend to index.
cf43a2fe
AM
1212 typedef Unordered_map<Glink_sym_ent, unsigned int,
1213 Glink_sym_ent_hash> Glink_entries;
1214 Glink_entries glink_entries_;
1215
1216 // Offset of pltresolve stub (actually, branch table for 32-bit)
1217 off_t pltresolve_;
1218
1219 // Allows access to .got and .plt for do_write.
1220 Target_powerpc<size, big_endian>* targ_;
1221};
1222
1223// Create the glink section.
1224
1225template<int size, bool big_endian>
d83ce4e3
AM
1226Output_data_glink<size, big_endian>::Output_data_glink(
1227 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
1228 : Output_section_data(16),
1229 pltresolve_(0), targ_(targ)
1230{
1231}
1232
1233// Add an entry to glink, if we do not already have one for this
d1a8cabd 1234// sym/object/addend combo.
cf43a2fe
AM
1235
1236template<int size, bool big_endian>
1237void
d83ce4e3
AM
1238Output_data_glink<size, big_endian>::add_entry(
1239 const Symbol* gsym,
1240 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1241 const Sized_relobj<size, big_endian>* object)
cf43a2fe 1242{
d1a8cabd 1243 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe 1244 unsigned int indx = this->glink_entries_.size();
d83ce4e3 1245 this->glink_entries_.insert(std::make_pair(ent, indx));
cf43a2fe
AM
1246}
1247
1248template<int size, bool big_endian>
1249unsigned int
d83ce4e3
AM
1250Output_data_glink<size, big_endian>::find_entry(
1251 const Symbol* gsym,
1252 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1253 const Sized_relobj<size, big_endian>* object) const
cf43a2fe 1254{
d1a8cabd 1255 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe
AM
1256 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1257 gold_assert(p != this->glink_entries_.end());
1258 return p->second;
1259}
1260
1261template<int size, bool big_endian>
1262void
1263Output_data_glink<size, big_endian>::set_final_data_size()
1264{
1265 unsigned int count = this->glink_entries_.size();
1266 off_t total = count;
1267
1268 if (count != 0)
1269 {
1270 if (size == 32)
1271 {
1272 total *= 16;
1273 this->pltresolve_ = total;
1274
1275 // space for branch table
1276 total += 4 * (count - 1);
1277
1278 total += -total & 15;
1279 total += this->pltresolve_size;
1280 }
1281 else
1282 {
1283 total *= 32;
1284 this->pltresolve_ = total;
1285 total += this->pltresolve_size;
1286
1287 // space for branch table
1288 total += 8 * count;
1289 if (count > 0x8000)
1290 total += 4 * (count - 0x8000);
1291 }
1292 }
1293
1294 this->set_data_size(total);
1295}
1296
1297static inline uint32_t
1298l(uint32_t a)
1299{
1300 return a & 0xffff;
1301}
1302
1303static inline uint32_t
1304hi(uint32_t a)
1305{
1306 return l(a >> 16);
1307}
1308
1309static inline uint32_t
1310ha(uint32_t a)
1311{
1312 return hi(a + 0x8000);
1313}
1314
1315template<bool big_endian>
1316static inline void
1317write_insn(unsigned char *p, uint32_t v)
1318{
1319 elfcpp::Swap<32, big_endian>::writeval(p, v);
1320}
1321
1322// Write out .glink.
1323
1324template<int size, bool big_endian>
1325void
1326Output_data_glink<size, big_endian>::do_write(Output_file* of)
1327{
1328 const off_t off = this->offset();
42cacb20
DE
1329 const section_size_type oview_size =
1330 convert_to_section_size_type(this->data_size());
cf43a2fe
AM
1331 unsigned char* const oview = of->get_output_view(off, oview_size);
1332 unsigned char *p;
42cacb20 1333
cf43a2fe
AM
1334 // The base address of the .plt section.
1335 uint32_t plt_base = this->targ_->plt_section()->address();
1336
1337 // The address of _GLOBAL_OFFSET_TABLE_.
1338 const Output_data_got_powerpc<size, big_endian> *got;
1339 typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1340 got = this->targ_->got_section();
1341 g_o_t = got->address() + got->g_o_t();
1342
1343 if (size == 64)
1344 {
1345 // Write out call stubs.
1346 typename Glink_entries::const_iterator g;
1347 for (g = this->glink_entries_.begin();
1348 g != this->glink_entries_.end();
1349 ++g)
1350 {
1351 uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1352 uint64_t got_addr = g_o_t;
1353 uint64_t pltoff = plt_addr - got_addr;
1354
1355 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1356 gold_error(_("%s: linkage table error against `%s'"),
1357 g->first.object_->name().c_str(),
1358 g->first.sym_->demangled_name().c_str());
1359
1360 p = oview + g->second * this->glink_entry_size();
1361 if (ha(pltoff) != 0)
1362 {
1363 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1364 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1365 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1366 if (ha(pltoff + 16) != ha(pltoff))
1367 {
1368 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1369 pltoff = 0;
1370 }
1371 write_insn<big_endian>(p, mtctr_11), p += 4;
1372 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1373 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1374 write_insn<big_endian>(p, bctr), p += 4;
1375 }
1376 else
1377 {
1378 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1379 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1380 if (ha(pltoff + 16) != ha(pltoff))
1381 {
1382 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1383 pltoff = 0;
1384 }
1385 write_insn<big_endian>(p, mtctr_11), p += 4;
1386 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1387 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1388 write_insn<big_endian>(p, bctr), p += 4;
1389 }
1390 }
1391
1392 // Write pltresolve stub.
1393 p = oview + this->pltresolve_;
1394 uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1395 uint64_t pltoff = plt_base - after_bcl;
1396
1397 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1398
1399 write_insn<big_endian>(p, mflr_12), p += 4;
1400 write_insn<big_endian>(p, bcl_20_31), p += 4;
1401 write_insn<big_endian>(p, mflr_11), p += 4;
1402 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1403 write_insn<big_endian>(p, mtlr_12), p += 4;
1404 write_insn<big_endian>(p, add_12_2_11), p += 4;
1405 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1406 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1407 write_insn<big_endian>(p, mtctr_11), p += 4;
1408 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1409 write_insn<big_endian>(p, bctr), p += 4;
1410 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1411 write_insn<big_endian>(p, nop), p += 4;
1412
1413 // Write lazy link call stubs.
1414 uint32_t indx = 0;
1415 while (p < oview + oview_size)
1416 {
1417 if (indx < 0x8000)
1418 {
1419 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1420 }
1421 else
1422 {
1423 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1424 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1425 }
1426 uint16_t branch_off = this->pltresolve_ + 8 - (p - oview);
1427 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1428 indx++;
1429 }
1430 }
1431 else
1432 {
1433 // Write out call stubs.
1434 typename Glink_entries::const_iterator g;
1435 for (g = this->glink_entries_.begin();
1436 g != this->glink_entries_.end();
1437 ++g)
1438 {
1439 uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1440 uint32_t got_addr;
1441
1442 p = oview + g->second * this->glink_entry_size();
1443 if (parameters->options().output_is_position_independent())
1444 {
d1a8cabd
AM
1445 const Powerpc_relobj<size, big_endian>* object = static_cast
1446 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1447 if (object != NULL)
1448 {
1449 unsigned int got2 = object->got2_shndx();
1450 got_addr = (g->first.object_->output_section(got2)->address()
1451 + g->first.object_->output_section_offset(got2)
1452 + g->first.addend_);
1453 }
cf43a2fe
AM
1454 else
1455 got_addr = g_o_t;
1456
1457 uint32_t pltoff = plt_addr - got_addr;
1458 if (ha(pltoff) == 0)
1459 {
1460 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1461 write_insn<big_endian>(p + 4, mtctr_11);
1462 write_insn<big_endian>(p + 8, bctr);
1463 }
1464 else
1465 {
1466 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1467 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1468 write_insn<big_endian>(p + 8, mtctr_11);
1469 write_insn<big_endian>(p + 12, bctr);
1470 }
1471 }
1472 else
1473 {
1474 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1475 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1476 write_insn<big_endian>(p + 8, mtctr_11);
1477 write_insn<big_endian>(p + 12, bctr);
1478 }
1479 }
42cacb20 1480
cf43a2fe
AM
1481 // Write out pltresolve branch table.
1482 p = oview + this->pltresolve_;
1483 unsigned int the_end = oview_size - this->pltresolve_size;
1484 unsigned char *end_p = oview + the_end;
1485 while (p < end_p - 8 * 4)
1486 write_insn<big_endian>(p, b + end_p - p), p += 4;
1487 while (p < end_p)
1488 write_insn<big_endian>(p, nop), p += 4;
42cacb20 1489
cf43a2fe
AM
1490 // Write out pltresolve call stub.
1491 if (parameters->options().output_is_position_independent())
42cacb20 1492 {
cf43a2fe
AM
1493 uint32_t res0_off = this->pltresolve_;
1494 uint32_t after_bcl_off = the_end + 12;
1495 uint32_t bcl_res0 = after_bcl_off - res0_off;
1496
1497 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1498 write_insn<big_endian>(p + 4, mflr_0);
1499 write_insn<big_endian>(p + 8, bcl_20_31);
1500 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1501 write_insn<big_endian>(p + 16, mflr_12);
1502 write_insn<big_endian>(p + 20, mtlr_0);
1503 write_insn<big_endian>(p + 24, sub_11_11_12);
1504
1505 uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1506
1507 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1508 if (ha(got_bcl) == ha(got_bcl + 4))
1509 {
1510 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1511 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1512 }
1513 else
1514 {
1515 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1516 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1517 }
1518 write_insn<big_endian>(p + 40, mtctr_0);
1519 write_insn<big_endian>(p + 44, add_0_11_11);
1520 write_insn<big_endian>(p + 48, add_11_0_11);
1521 write_insn<big_endian>(p + 52, bctr);
1522 write_insn<big_endian>(p + 56, nop);
1523 write_insn<big_endian>(p + 60, nop);
42cacb20 1524 }
cf43a2fe 1525 else
42cacb20 1526 {
cf43a2fe
AM
1527 uint32_t res0 = this->pltresolve_ + this->address();
1528
1529 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1530 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1531 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1532 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1533 else
1534 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1535 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1536 write_insn<big_endian>(p + 16, mtctr_0);
1537 write_insn<big_endian>(p + 20, add_0_11_11);
1538 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1539 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1540 else
1541 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1542 write_insn<big_endian>(p + 28, add_11_0_11);
1543 write_insn<big_endian>(p + 32, bctr);
1544 write_insn<big_endian>(p + 36, nop);
1545 write_insn<big_endian>(p + 40, nop);
1546 write_insn<big_endian>(p + 44, nop);
1547 write_insn<big_endian>(p + 48, nop);
1548 write_insn<big_endian>(p + 52, nop);
1549 write_insn<big_endian>(p + 56, nop);
1550 write_insn<big_endian>(p + 60, nop);
42cacb20 1551 }
cf43a2fe 1552 p += 64;
42cacb20
DE
1553 }
1554
cf43a2fe
AM
1555 of->write_output_view(off, oview_size, oview);
1556}
1557
1558// Create the glink section.
42cacb20 1559
cf43a2fe
AM
1560template<int size, bool big_endian>
1561void
1562Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1563{
1564 if (this->glink_ == NULL)
1565 {
1566 this->glink_ = new Output_data_glink<size, big_endian>(this);
1567 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1568 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1569 this->glink_, ORDER_TEXT, false);
1570 }
42cacb20
DE
1571}
1572
1573// Create a PLT entry for a global symbol.
1574
1575template<int size, bool big_endian>
1576void
d83ce4e3
AM
1577Target_powerpc<size, big_endian>::make_plt_entry(
1578 Layout* layout,
1579 Symbol* gsym,
1580 const elfcpp::Rela<size, big_endian>& reloc,
1581 const Sized_relobj<size, big_endian>* object)
42cacb20 1582{
42cacb20 1583 if (this->plt_ == NULL)
cf43a2fe 1584 this->make_plt_section(layout);
42cacb20 1585
cf43a2fe 1586 this->plt_->add_entry(gsym);
612a8d3d 1587
d1a8cabd 1588 this->glink_->add_entry(gsym, reloc, object);
42cacb20
DE
1589}
1590
0e70b911
CC
1591// Return the number of entries in the PLT.
1592
1593template<int size, bool big_endian>
1594unsigned int
1595Target_powerpc<size, big_endian>::plt_entry_count() const
1596{
1597 if (this->plt_ == NULL)
1598 return 0;
1599 return this->plt_->entry_count();
1600}
1601
1602// Return the offset of the first non-reserved PLT entry.
1603
1604template<int size, bool big_endian>
1605unsigned int
1606Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1607{
1608 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1609}
1610
1611// Return the size of each PLT entry.
1612
1613template<int size, bool big_endian>
1614unsigned int
1615Target_powerpc<size, big_endian>::plt_entry_size() const
1616{
1617 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1618}
1619
42cacb20
DE
1620// Create a GOT entry for the TLS module index.
1621
1622template<int size, bool big_endian>
1623unsigned int
6fa2a40b
CC
1624Target_powerpc<size, big_endian>::got_mod_index_entry(
1625 Symbol_table* symtab,
1626 Layout* layout,
1627 Sized_relobj_file<size, big_endian>* object)
42cacb20
DE
1628{
1629 if (this->got_mod_index_offset_ == -1U)
1630 {
1631 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1632 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
cf43a2fe 1633 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
1634 unsigned int got_offset;
1635
1636 got = this->got_section(symtab, layout);
cf43a2fe 1637 got->reserve_ent(2);
42cacb20
DE
1638 got_offset = got->add_constant(0);
1639 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1640 got_offset, 0);
1641 got->add_constant(0);
1642 this->got_mod_index_offset_ = got_offset;
1643 }
1644 return this->got_mod_index_offset_;
1645}
1646
1647// Optimize the TLS relocation type based on what we know about the
1648// symbol. IS_FINAL is true if the final address of this symbol is
1649// known at link time.
1650
cf43a2fe
AM
1651template<int size, bool big_endian>
1652tls::Tls_optimization
1653Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
42cacb20
DE
1654{
1655 // If we are generating a shared library, then we can't do anything
1656 // in the linker.
1657 if (parameters->options().shared())
1658 return tls::TLSOPT_NONE;
cf43a2fe
AM
1659 // FIXME
1660 return tls::TLSOPT_NONE;
42cacb20
DE
1661}
1662
95a2c8d6
RS
1663// Get the Reference_flags for a particular relocation.
1664
1665template<int size, bool big_endian>
1666int
d83ce4e3 1667Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
95a2c8d6
RS
1668{
1669 switch (r_type)
1670 {
1671 case elfcpp::R_POWERPC_NONE:
1672 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1673 case elfcpp::R_POWERPC_GNU_VTENTRY:
1674 case elfcpp::R_PPC64_TOC:
1675 // No symbol reference.
1676 return 0;
1677
1678 case elfcpp::R_POWERPC_ADDR16:
1679 case elfcpp::R_POWERPC_ADDR16_LO:
1680 case elfcpp::R_POWERPC_ADDR16_HI:
1681 case elfcpp::R_POWERPC_ADDR16_HA:
1682 case elfcpp::R_POWERPC_ADDR32:
1683 case elfcpp::R_PPC64_ADDR64:
1684 return Symbol::ABSOLUTE_REF;
1685
1686 case elfcpp::R_POWERPC_REL24:
1687 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
1688 case elfcpp::R_POWERPC_REL16:
1689 case elfcpp::R_POWERPC_REL16_LO:
1690 case elfcpp::R_POWERPC_REL16_HI:
1691 case elfcpp::R_POWERPC_REL16_HA:
95a2c8d6
RS
1692 return Symbol::RELATIVE_REF;
1693
1694 case elfcpp::R_PPC_PLTREL24:
1695 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1696
1697 case elfcpp::R_POWERPC_GOT16:
1698 case elfcpp::R_POWERPC_GOT16_LO:
1699 case elfcpp::R_POWERPC_GOT16_HI:
1700 case elfcpp::R_POWERPC_GOT16_HA:
1701 case elfcpp::R_PPC64_TOC16:
1702 case elfcpp::R_PPC64_TOC16_LO:
1703 case elfcpp::R_PPC64_TOC16_HI:
1704 case elfcpp::R_PPC64_TOC16_HA:
1705 case elfcpp::R_PPC64_TOC16_DS:
1706 case elfcpp::R_PPC64_TOC16_LO_DS:
1707 // Absolute in GOT.
1708 return Symbol::ABSOLUTE_REF;
1709
1710 case elfcpp::R_POWERPC_GOT_TPREL16:
1711 case elfcpp::R_POWERPC_TLS:
1712 return Symbol::TLS_REF;
1713
1714 case elfcpp::R_POWERPC_COPY:
1715 case elfcpp::R_POWERPC_GLOB_DAT:
1716 case elfcpp::R_POWERPC_JMP_SLOT:
1717 case elfcpp::R_POWERPC_RELATIVE:
1718 case elfcpp::R_POWERPC_DTPMOD:
1719 default:
1720 // Not expected. We will give an error later.
1721 return 0;
1722 }
1723}
1724
42cacb20
DE
1725// Report an unsupported relocation against a local symbol.
1726
1727template<int size, bool big_endian>
1728void
1729Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
1730 Sized_relobj_file<size, big_endian>* object,
1731 unsigned int r_type)
42cacb20
DE
1732{
1733 gold_error(_("%s: unsupported reloc %u against local symbol"),
1734 object->name().c_str(), r_type);
1735}
1736
1737// We are about to emit a dynamic relocation of type R_TYPE. If the
1738// dynamic linker does not support it, issue an error.
1739
1740template<int size, bool big_endian>
1741void
1742Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1743 unsigned int r_type)
1744{
1745 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1746
1747 // These are the relocation types supported by glibc for both 32-bit
1748 // and 64-bit powerpc.
1749 switch (r_type)
1750 {
1751 case elfcpp::R_POWERPC_RELATIVE:
1752 case elfcpp::R_POWERPC_GLOB_DAT:
1753 case elfcpp::R_POWERPC_DTPMOD:
1754 case elfcpp::R_POWERPC_DTPREL:
1755 case elfcpp::R_POWERPC_TPREL:
1756 case elfcpp::R_POWERPC_JMP_SLOT:
1757 case elfcpp::R_POWERPC_COPY:
1758 case elfcpp::R_POWERPC_ADDR32:
1759 case elfcpp::R_POWERPC_ADDR24:
1760 case elfcpp::R_POWERPC_REL24:
1761 return;
1762
1763 default:
1764 break;
1765 }
1766
1767 if (size == 64)
1768 {
1769 switch (r_type)
1770 {
1771 // These are the relocation types supported only on 64-bit.
1772 case elfcpp::R_PPC64_ADDR64:
1773 case elfcpp::R_PPC64_TPREL16_LO_DS:
1774 case elfcpp::R_PPC64_TPREL16_DS:
1775 case elfcpp::R_POWERPC_TPREL16:
1776 case elfcpp::R_POWERPC_TPREL16_LO:
1777 case elfcpp::R_POWERPC_TPREL16_HI:
1778 case elfcpp::R_POWERPC_TPREL16_HA:
1779 case elfcpp::R_PPC64_TPREL16_HIGHER:
1780 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1781 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1782 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1783 case elfcpp::R_PPC64_ADDR16_LO_DS:
1784 case elfcpp::R_POWERPC_ADDR16_LO:
1785 case elfcpp::R_POWERPC_ADDR16_HI:
1786 case elfcpp::R_POWERPC_ADDR16_HA:
1787 case elfcpp::R_POWERPC_ADDR30:
1788 case elfcpp::R_PPC64_UADDR64:
1789 case elfcpp::R_POWERPC_UADDR32:
1790 case elfcpp::R_POWERPC_ADDR16:
1791 case elfcpp::R_POWERPC_UADDR16:
1792 case elfcpp::R_PPC64_ADDR16_DS:
1793 case elfcpp::R_PPC64_ADDR16_HIGHER:
1794 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1795 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1796 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1797 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1798 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1799 case elfcpp::R_POWERPC_REL32:
1800 case elfcpp::R_PPC64_REL64:
1801 return;
1802
1803 default:
1804 break;
1805 }
1806 }
1807 else
1808 {
1809 switch (r_type)
1810 {
1811 // These are the relocation types supported only on 32-bit.
1812
1813 default:
1814 break;
1815 }
1816 }
1817
1818 // This prevents us from issuing more than one error per reloc
1819 // section. But we can still wind up issuing more than one
1820 // error per object file.
1821 if (this->issued_non_pic_error_)
1822 return;
33aea2fd 1823 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
1824 object->error(_("requires unsupported dynamic reloc; "
1825 "recompile with -fPIC"));
1826 this->issued_non_pic_error_ = true;
1827 return;
1828}
1829
1830// Scan a relocation for a local symbol.
1831
1832template<int size, bool big_endian>
1833inline void
1834Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
1835 Symbol_table* symtab,
1836 Layout* layout,
1837 Target_powerpc<size, big_endian>* target,
1838 Sized_relobj_file<size, big_endian>* object,
1839 unsigned int data_shndx,
1840 Output_section* output_section,
1841 const elfcpp::Rela<size, big_endian>& reloc,
1842 unsigned int r_type,
1843 const elfcpp::Sym<size, big_endian>& lsym)
42cacb20
DE
1844{
1845 switch (r_type)
1846 {
1847 case elfcpp::R_POWERPC_NONE:
1848 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1849 case elfcpp::R_POWERPC_GNU_VTENTRY:
6ce78956 1850 case elfcpp::R_PPC64_TOCSAVE:
42cacb20
DE
1851 break;
1852
1853 case elfcpp::R_PPC64_ADDR64:
1854 case elfcpp::R_POWERPC_ADDR32:
1855 case elfcpp::R_POWERPC_ADDR16_HA:
1856 case elfcpp::R_POWERPC_ADDR16_LO:
1857 // If building a shared library (or a position-independent
1858 // executable), we need to create a dynamic relocation for
1859 // this location.
1860 if (parameters->options().output_is_position_independent())
2e702c99
RM
1861 {
1862 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20
DE
1863
1864 check_non_pic(object, r_type);
2e702c99
RM
1865 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1866 {
1867 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1868 rela_dyn->add_local(object, r_sym, r_type, output_section,
42cacb20
DE
1869 data_shndx, reloc.get_r_offset(),
1870 reloc.get_r_addend());
2e702c99
RM
1871 }
1872 else
1873 {
42cacb20 1874 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2e702c99
RM
1875 gold_assert(lsym.get_st_value() == 0);
1876 rela_dyn->add_local_relative(object, r_sym, r_type,
42cacb20
DE
1877 output_section, data_shndx,
1878 reloc.get_r_offset(),
397b129b 1879 reloc.get_r_addend(), false);
2e702c99
RM
1880 }
1881 }
42cacb20
DE
1882 break;
1883
1884 case elfcpp::R_POWERPC_REL24:
1885 case elfcpp::R_PPC_LOCAL24PC:
1886 case elfcpp::R_POWERPC_REL32:
6ce78956
AM
1887 case elfcpp::R_POWERPC_REL16_LO:
1888 case elfcpp::R_POWERPC_REL16_HA:
42cacb20
DE
1889 break;
1890
1891 case elfcpp::R_POWERPC_GOT16:
1892 case elfcpp::R_POWERPC_GOT16_LO:
1893 case elfcpp::R_POWERPC_GOT16_HI:
1894 case elfcpp::R_POWERPC_GOT16_HA:
42cacb20 1895 {
cf43a2fe
AM
1896 // The symbol requires a GOT entry.
1897 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
1898 unsigned int r_sym;
1899
1900 got = target->got_section(symtab, layout);
1901 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1902
1903 // If we are generating a shared object, we need to add a
1904 // dynamic relocation for this symbol's GOT entry.
1905 if (parameters->options().output_is_position_independent())
1906 {
1907 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1908 {
1909 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1910 unsigned int off;
1911
1912 off = got->add_constant(0);
1913 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1914 rela_dyn->add_local_relative(object, r_sym,
1915 elfcpp::R_POWERPC_RELATIVE,
397b129b 1916 got, off, 0, false);
42cacb20 1917 }
2e702c99 1918 }
42cacb20
DE
1919 else
1920 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1921 }
1922 break;
1923
cf43a2fe
AM
1924 case elfcpp::R_PPC64_TOC16:
1925 case elfcpp::R_PPC64_TOC16_LO:
1926 case elfcpp::R_PPC64_TOC16_HI:
1927 case elfcpp::R_PPC64_TOC16_HA:
1928 case elfcpp::R_PPC64_TOC16_DS:
1929 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
1930 case elfcpp::R_PPC64_TOC:
1931 // We need a GOT section.
1932 target->got_section(symtab, layout);
1933 break;
1934
1935 // These are relocations which should only be seen by the
1936 // dynamic linker, and should never be seen here.
1937 case elfcpp::R_POWERPC_COPY:
1938 case elfcpp::R_POWERPC_GLOB_DAT:
1939 case elfcpp::R_POWERPC_JMP_SLOT:
1940 case elfcpp::R_POWERPC_RELATIVE:
1941 case elfcpp::R_POWERPC_DTPMOD:
1942 gold_error(_("%s: unexpected reloc %u in object file"),
1943 object->name().c_str(), r_type);
1944 break;
1945
1946 default:
1947 unsupported_reloc_local(object, r_type);
1948 break;
1949 }
1950}
1951
1952// Report an unsupported relocation against a global symbol.
1953
1954template<int size, bool big_endian>
1955void
1956Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
d83ce4e3
AM
1957 Sized_relobj_file<size, big_endian>* object,
1958 unsigned int r_type,
1959 Symbol* gsym)
42cacb20
DE
1960{
1961 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1962 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1963}
1964
1965// Scan a relocation for a global symbol.
1966
1967template<int size, bool big_endian>
1968inline void
1969Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
1970 Symbol_table* symtab,
1971 Layout* layout,
1972 Target_powerpc<size, big_endian>* target,
1973 Sized_relobj_file<size, big_endian>* object,
1974 unsigned int data_shndx,
1975 Output_section* output_section,
1976 const elfcpp::Rela<size, big_endian>& reloc,
1977 unsigned int r_type,
1978 Symbol* gsym)
42cacb20
DE
1979{
1980 switch (r_type)
1981 {
1982 case elfcpp::R_POWERPC_NONE:
1983 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1984 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 1985 case elfcpp::R_PPC_LOCAL24PC:
42cacb20
DE
1986 break;
1987
1988 case elfcpp::R_POWERPC_ADDR16:
1989 case elfcpp::R_POWERPC_ADDR16_LO:
1990 case elfcpp::R_POWERPC_ADDR16_HI:
1991 case elfcpp::R_POWERPC_ADDR16_HA:
1992 case elfcpp::R_POWERPC_ADDR32:
1993 case elfcpp::R_PPC64_ADDR64:
1994 {
cf43a2fe
AM
1995 // Make a PLT entry if necessary.
1996 if (gsym->needs_plt_entry())
1997 {
1998 target->make_plt_entry(layout, gsym, reloc, 0);
2e702c99
RM
1999 // Since this is not a PC-relative relocation, we may be
2000 // taking the address of a function. In that case we need to
2001 // set the entry in the dynamic symbol table to the address of
2002 // the PLT entry.
cf43a2fe
AM
2003 if (size == 32
2004 && gsym->is_from_dynobj() && !parameters->options().shared())
2e702c99 2005 gsym->set_needs_dynsym_value();
cf43a2fe
AM
2006 }
2007 // Make a dynamic relocation if necessary.
2008 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2009 {
2010 if (gsym->may_need_copy_reloc())
2011 {
2012 target->copy_reloc(symtab, layout, object,
2013 data_shndx, output_section, gsym, reloc);
2014 }
2015 else if ((r_type == elfcpp::R_POWERPC_ADDR32
42cacb20 2016 || r_type == elfcpp::R_PPC64_ADDR64)
2e702c99
RM
2017 && gsym->can_use_relative_reloc(false))
2018 {
2019 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2020 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
42cacb20
DE
2021 output_section, object,
2022 data_shndx, reloc.get_r_offset(),
13cf9988 2023 reloc.get_r_addend(), false);
2e702c99
RM
2024 }
2025 else
2026 {
2027 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20
DE
2028
2029 check_non_pic(object, r_type);
2030 if (gsym->is_from_dynobj()
2031 || gsym->is_undefined()
2032 || gsym->is_preemptible())
2033 rela_dyn->add_global(gsym, r_type, output_section,
2034 object, data_shndx,
2035 reloc.get_r_offset(),
2036 reloc.get_r_addend());
2037 else
2038 rela_dyn->add_global_relative(gsym, r_type,
2039 output_section, object,
2040 data_shndx,
2041 reloc.get_r_offset(),
13cf9988 2042 reloc.get_r_addend(), false);
2e702c99
RM
2043 }
2044 }
42cacb20
DE
2045 }
2046 break;
2047
cf43a2fe 2048 case elfcpp::R_PPC_PLTREL24:
42cacb20 2049 case elfcpp::R_POWERPC_REL24:
42cacb20 2050 {
cf43a2fe
AM
2051 if (gsym->needs_plt_entry()
2052 || (!gsym->final_value_is_known()
2053 && !(gsym->is_defined()
2054 && !gsym->is_from_dynobj()
2055 && !gsym->is_preemptible())))
d1a8cabd 2056 target->make_plt_entry(layout, gsym, reloc, object);
42cacb20 2057 // Make a dynamic relocation if necessary.
95a2c8d6 2058 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
42cacb20 2059 {
966d4097 2060 if (gsym->may_need_copy_reloc())
42cacb20
DE
2061 {
2062 target->copy_reloc(symtab, layout, object,
2063 data_shndx, output_section, gsym,
2064 reloc);
2065 }
2066 else
2067 {
2068 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2069 check_non_pic(object, r_type);
2070 rela_dyn->add_global(gsym, r_type, output_section, object,
2071 data_shndx, reloc.get_r_offset(),
2072 reloc.get_r_addend());
2073 }
2074 }
2075 }
2076 break;
2077
6ce78956
AM
2078 case elfcpp::R_POWERPC_REL16:
2079 case elfcpp::R_POWERPC_REL16_LO:
2080 case elfcpp::R_POWERPC_REL16_HI:
2081 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe
AM
2082 break;
2083
42cacb20
DE
2084 case elfcpp::R_POWERPC_GOT16:
2085 case elfcpp::R_POWERPC_GOT16_LO:
2086 case elfcpp::R_POWERPC_GOT16_HI:
2087 case elfcpp::R_POWERPC_GOT16_HA:
42cacb20 2088 {
cf43a2fe
AM
2089 // The symbol requires a GOT entry.
2090 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
2091
2092 got = target->got_section(symtab, layout);
2e702c99
RM
2093 if (gsym->final_value_is_known())
2094 got->add_global(gsym, GOT_TYPE_STANDARD);
2095 else
2096 {
2097 // If this symbol is not fully resolved, we need to add a
2098 // dynamic relocation for it.
2099 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2100 if (gsym->is_from_dynobj()
2101 || gsym->is_undefined()
2102 || gsym->is_preemptible())
2103 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
83896202 2104 elfcpp::R_POWERPC_GLOB_DAT);
2e702c99
RM
2105 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2106 {
42cacb20
DE
2107 unsigned int off = got->add_constant(0);
2108
2109 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2110 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
13cf9988 2111 got, off, 0, false);
42cacb20 2112 }
2e702c99 2113 }
42cacb20
DE
2114 }
2115 break;
2116
2117 case elfcpp::R_PPC64_TOC:
cf43a2fe
AM
2118 case elfcpp::R_PPC64_TOC16:
2119 case elfcpp::R_PPC64_TOC16_LO:
2120 case elfcpp::R_PPC64_TOC16_HI:
2121 case elfcpp::R_PPC64_TOC16_HA:
2122 case elfcpp::R_PPC64_TOC16_DS:
2123 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
2124 // We need a GOT section.
2125 target->got_section(symtab, layout);
2126 break;
2127
2128 case elfcpp::R_POWERPC_GOT_TPREL16:
2129 case elfcpp::R_POWERPC_TLS:
2130 // XXX TLS
2131 break;
2132
2133 // These are relocations which should only be seen by the
2134 // dynamic linker, and should never be seen here.
2135 case elfcpp::R_POWERPC_COPY:
2136 case elfcpp::R_POWERPC_GLOB_DAT:
2137 case elfcpp::R_POWERPC_JMP_SLOT:
2138 case elfcpp::R_POWERPC_RELATIVE:
2139 case elfcpp::R_POWERPC_DTPMOD:
2140 gold_error(_("%s: unexpected reloc %u in object file"),
2141 object->name().c_str(), r_type);
2142 break;
2143
2144 default:
2145 unsupported_reloc_global(object, r_type, gsym);
2146 break;
2147 }
2148}
2149
6d03d481
ST
2150// Process relocations for gc.
2151
2152template<int size, bool big_endian>
2153void
2154Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
2155 Symbol_table* symtab,
2156 Layout* layout,
2157 Sized_relobj_file<size, big_endian>* object,
2158 unsigned int data_shndx,
2159 unsigned int,
2160 const unsigned char* prelocs,
2161 size_t reloc_count,
2162 Output_section* output_section,
2163 bool needs_special_offset_handling,
2164 size_t local_symbol_count,
2165 const unsigned char* plocal_symbols)
6d03d481
ST
2166{
2167 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 2168 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6d03d481 2169
41cbeecc 2170 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3ff2ccb0 2171 typename Target_powerpc::Relocatable_size_for_reloc>(
6d03d481
ST
2172 symtab,
2173 layout,
2174 this,
2175 object,
2176 data_shndx,
2177 prelocs,
2178 reloc_count,
2179 output_section,
2180 needs_special_offset_handling,
2181 local_symbol_count,
2182 plocal_symbols);
2183}
2184
42cacb20
DE
2185// Scan relocations for a section.
2186
2187template<int size, bool big_endian>
2188void
2189Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
2190 Symbol_table* symtab,
2191 Layout* layout,
2192 Sized_relobj_file<size, big_endian>* object,
2193 unsigned int data_shndx,
2194 unsigned int sh_type,
2195 const unsigned char* prelocs,
2196 size_t reloc_count,
2197 Output_section* output_section,
2198 bool needs_special_offset_handling,
2199 size_t local_symbol_count,
2200 const unsigned char* plocal_symbols)
42cacb20
DE
2201{
2202 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 2203 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
42cacb20
DE
2204
2205 if (sh_type == elfcpp::SHT_REL)
2206 {
2207 gold_error(_("%s: unsupported REL reloc section"),
2208 object->name().c_str());
2209 return;
2210 }
2211
cf43a2fe
AM
2212 if (size == 32)
2213 {
2214 static Output_data_space* sdata;
2215
2216 // Define _SDA_BASE_ at the start of the .sdata section.
2217 if (sdata == NULL)
2218 {
2219 // layout->find_output_section(".sdata") == NULL
2220 sdata = new Output_data_space(4, "** sdata");
2221 Output_section* os
2222 = layout->add_output_section_data(".sdata", 0,
2223 elfcpp::SHF_ALLOC
2224 | elfcpp::SHF_WRITE,
2225 sdata, ORDER_SMALL_DATA, false);
2226 symtab->define_in_output_data("_SDA_BASE_", NULL,
2227 Symbol_table::PREDEFINED,
2228 os, 32768, 0, elfcpp::STT_OBJECT,
2229 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2230 0, false, false);
2231 }
2232 }
42cacb20 2233
2ea97941 2234 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
42cacb20
DE
2235 symtab,
2236 layout,
2237 this,
2238 object,
2239 data_shndx,
2240 prelocs,
2241 reloc_count,
2242 output_section,
2243 needs_special_offset_handling,
2244 local_symbol_count,
2245 plocal_symbols);
2246}
2247
2248// Finalize the sections.
2249
2250template<int size, bool big_endian>
2251void
d5b40221
DK
2252Target_powerpc<size, big_endian>::do_finalize_sections(
2253 Layout* layout,
f59f41f3
DK
2254 const Input_objects*,
2255 Symbol_table*)
42cacb20
DE
2256{
2257 // Fill in some more dynamic tags.
ea715a34
ILT
2258 const Reloc_section* rel_plt = (this->plt_ == NULL
2259 ? NULL
2260 : this->plt_->rel_plt());
2261 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
612a8d3d 2262 this->rela_dyn_, true, size == 32);
42cacb20 2263
cf43a2fe
AM
2264 if (size == 32)
2265 {
2266 this->got_->finalize_data_size();
2267 Output_data_dynamic* odyn = layout->dynamic_data();
2268 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2269 this->got_, this->got_->g_o_t());
2270 }
2271
42cacb20
DE
2272 // Emit any relocs we saved in an attempt to avoid generating COPY
2273 // relocs.
2274 if (this->copy_relocs_.any_saved_relocs())
2275 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2276}
2277
2278// Perform a relocation.
2279
2280template<int size, bool big_endian>
2281inline bool
2282Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3
AM
2283 const Relocate_info<size, big_endian>* relinfo,
2284 Target_powerpc* target,
2285 Output_section* os,
2286 size_t relnum,
2287 const elfcpp::Rela<size, big_endian>& rela,
2288 unsigned int r_type,
2289 const Sized_symbol<size>* gsym,
2290 const Symbol_value<size>* psymval,
2291 unsigned char* view,
2292 typename elfcpp::Elf_types<size>::Elf_Addr address,
2293 section_size_type /* view_size */)
42cacb20
DE
2294{
2295 const unsigned int toc_base_offset = 0x8000;
2296 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
cf43a2fe
AM
2297 const Powerpc_relobj<size, big_endian>* const object
2298 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2299 elfcpp::Elf_Xword value;
2300
2301 if (r_type == elfcpp::R_POWERPC_GOT16
2302 || r_type == elfcpp::R_POWERPC_GOT16_LO
2303 || r_type == elfcpp::R_POWERPC_GOT16_HI
2304 || r_type == elfcpp::R_POWERPC_GOT16_HA
2305 || r_type == elfcpp::R_PPC64_GOT16_DS
2306 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 2307 {
cf43a2fe
AM
2308 if (gsym != NULL)
2309 {
2310 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2311 value = gsym->got_offset(GOT_TYPE_STANDARD);
2312 }
2313 else
2314 {
2315 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2316 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2317 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2318 }
2319 value -= target->got_section()->g_o_t();
2320 }
2321 else if (r_type == elfcpp::R_PPC64_TOC)
2322 {
2323 value = target->got_section()->address() + toc_base_offset;
2324 }
2325 else if (gsym != NULL
2326 && (r_type == elfcpp::R_POWERPC_REL24
2327 || r_type == elfcpp::R_PPC_PLTREL24)
2328 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2329 {
2330 const Output_data_glink<size, big_endian>* glink;
2331
2332 glink = target->glink_section();
d1a8cabd 2333 unsigned int glink_index = glink->find_entry(gsym, rela, object);
cf43a2fe
AM
2334 value = glink->address() + glink_index * glink->glink_entry_size();
2335 }
2336 else
2337 {
2338 elfcpp::Elf_Xword addend = 0;
2339 if (r_type != elfcpp::R_PPC_PLTREL24)
2340 addend = rela.get_r_addend();
2341 value = psymval->value(object, addend);
42cacb20
DE
2342 }
2343
42cacb20
DE
2344 switch (r_type)
2345 {
2346 case elfcpp::R_PPC64_TOC16:
2347 case elfcpp::R_PPC64_TOC16_LO:
2348 case elfcpp::R_PPC64_TOC16_HI:
2349 case elfcpp::R_PPC64_TOC16_HA:
2350 case elfcpp::R_PPC64_TOC16_DS:
2351 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
2352 // Subtract the TOC base address.
2353 value -= target->got_section()->address() + toc_base_offset;
42cacb20
DE
2354 break;
2355
cf43a2fe
AM
2356 case elfcpp::R_POWERPC_SECTOFF:
2357 case elfcpp::R_POWERPC_SECTOFF_LO:
2358 case elfcpp::R_POWERPC_SECTOFF_HI:
2359 case elfcpp::R_POWERPC_SECTOFF_HA:
2360 case elfcpp::R_PPC64_SECTOFF_DS:
2361 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2362 if (os != NULL)
2363 value -= os->address();
42cacb20
DE
2364 break;
2365
2366 default:
2367 break;
2368 }
2369
2370 switch (r_type)
2371 {
2372 case elfcpp::R_POWERPC_NONE:
2373 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2374 case elfcpp::R_POWERPC_GNU_VTENTRY:
2375 break;
2376
2377 case elfcpp::R_POWERPC_REL32:
cf43a2fe 2378 Reloc::rel32(view, value, 0, address);
42cacb20
DE
2379 break;
2380
2381 case elfcpp::R_POWERPC_REL24:
42cacb20 2382 case elfcpp::R_PPC_PLTREL24:
cf43a2fe
AM
2383 case elfcpp::R_PPC_LOCAL24PC:
2384 Reloc::rel24(view, value, 0, address);
42cacb20
DE
2385 break;
2386
cf43a2fe
AM
2387 case elfcpp::R_POWERPC_REL14:
2388 Reloc::rel14(view, value, 0, address);
42cacb20
DE
2389 break;
2390
2391 case elfcpp::R_PPC64_ADDR64:
cf43a2fe
AM
2392 case elfcpp::R_PPC64_TOC:
2393 Relocate_functions<size, big_endian>::rela64(view, value, 0);
42cacb20
DE
2394 break;
2395
2396 case elfcpp::R_POWERPC_ADDR32:
cf43a2fe 2397 Relocate_functions<size, big_endian>::rela32(view, value, 0);
42cacb20
DE
2398 break;
2399
cf43a2fe
AM
2400 case elfcpp::R_POWERPC_ADDR16:
2401 case elfcpp::R_PPC64_TOC16:
42cacb20 2402 case elfcpp::R_POWERPC_GOT16:
cf43a2fe
AM
2403 case elfcpp::R_POWERPC_SECTOFF:
2404 Reloc::addr16(view, value, 0);
42cacb20
DE
2405 break;
2406
cf43a2fe
AM
2407 case elfcpp::R_POWERPC_ADDR16_LO:
2408 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 2409 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe
AM
2410 case elfcpp::R_POWERPC_SECTOFF_LO:
2411 Reloc::addr16_lo(view, value, 0);
42cacb20
DE
2412 break;
2413
cf43a2fe
AM
2414 case elfcpp::R_POWERPC_ADDR16_HI:
2415 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 2416 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe
AM
2417 case elfcpp::R_POWERPC_SECTOFF_HI:
2418 Reloc::addr16_hi(view, value, 0);
42cacb20
DE
2419 break;
2420
cf43a2fe
AM
2421 case elfcpp::R_POWERPC_ADDR16_HA:
2422 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 2423 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe
AM
2424 case elfcpp::R_POWERPC_SECTOFF_HA:
2425 Reloc::addr16_ha(view, value, 0);
42cacb20
DE
2426 break;
2427
6ce78956 2428 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 2429 Reloc::rel16_lo(view, value, 0, address);
42cacb20
DE
2430 break;
2431
6ce78956 2432 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 2433 Reloc::rel16_hi(view, value, 0, address);
42cacb20
DE
2434 break;
2435
6ce78956 2436 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 2437 Reloc::rel16_ha(view, value, 0, address);
42cacb20
DE
2438 break;
2439
cf43a2fe
AM
2440 case elfcpp::R_PPC64_ADDR16_DS:
2441 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
2442 case elfcpp::R_PPC64_TOC16_DS:
2443 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
2444 case elfcpp::R_PPC64_GOT16_DS:
2445 case elfcpp::R_PPC64_GOT16_LO_DS:
2446 case elfcpp::R_PPC64_SECTOFF_DS:
2447 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2448 Reloc::addr16_ds(view, value, 0);
42cacb20
DE
2449 break;
2450
2451 case elfcpp::R_POWERPC_COPY:
2452 case elfcpp::R_POWERPC_GLOB_DAT:
2453 case elfcpp::R_POWERPC_JMP_SLOT:
2454 case elfcpp::R_POWERPC_RELATIVE:
2455 // This is an outstanding tls reloc, which is unexpected when
2456 // linking.
2457 case elfcpp::R_POWERPC_DTPMOD:
2458 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2459 _("unexpected reloc %u in object file"),
2460 r_type);
2461 break;
2462
6ce78956
AM
2463 case elfcpp::R_PPC64_TOCSAVE:
2464 // For the time being this can be ignored.
2465 break;
2466
42cacb20
DE
2467 default:
2468 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2469 _("unsupported reloc %u"),
2470 r_type);
2471 break;
2472 }
2473
2474 return true;
2475}
2476
2477// Perform a TLS relocation.
2478
2479template<int size, bool big_endian>
2480inline void
2481Target_powerpc<size, big_endian>::Relocate::relocate_tls(
d83ce4e3
AM
2482 const Relocate_info<size, big_endian>* relinfo,
2483 Target_powerpc<size, big_endian>* target,
2484 size_t relnum,
2485 const elfcpp::Rela<size, big_endian>& rela,
2486 unsigned int r_type,
2487 const Sized_symbol<size>* gsym,
2488 const Symbol_value<size>* psymval,
2489 unsigned char* view,
2490 typename elfcpp::Elf_types<size>::Elf_Addr address,
2491 section_size_type)
42cacb20
DE
2492{
2493 Output_segment* tls_segment = relinfo->layout->tls_segment();
6fa2a40b 2494 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
42cacb20
DE
2495
2496 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2497 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2498
2499 const bool is_final =
2500 (gsym == NULL
2501 ? !parameters->options().output_is_position_independent()
2502 : gsym->final_value_is_known());
42cacb20
DE
2503
2504 switch (r_type)
2505 {
2506 // XXX
2507 }
2508}
2509
2510// Relocate section data.
2511
2512template<int size, bool big_endian>
2513void
2514Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
2515 const Relocate_info<size, big_endian>* relinfo,
2516 unsigned int sh_type,
2517 const unsigned char* prelocs,
2518 size_t reloc_count,
2519 Output_section* output_section,
2520 bool needs_special_offset_handling,
2521 unsigned char* view,
2522 typename elfcpp::Elf_types<size>::Elf_Addr address,
2523 section_size_type view_size,
2524 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
2525{
2526 typedef Target_powerpc<size, big_endian> Powerpc;
2527 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2528
2529 gold_assert(sh_type == elfcpp::SHT_RELA);
2530
2531 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
d83ce4e3 2532 Powerpc_relocate>(
42cacb20
DE
2533 relinfo,
2534 this,
2535 prelocs,
2536 reloc_count,
2537 output_section,
2538 needs_special_offset_handling,
2539 view,
2540 address,
364c7fa5
ILT
2541 view_size,
2542 reloc_symbol_changes);
42cacb20
DE
2543}
2544
cf43a2fe 2545class Powerpc_scan_relocatable_reloc
42cacb20 2546{
cf43a2fe
AM
2547public:
2548 // Return the strategy to use for a local symbol which is not a
2549 // section symbol, given the relocation type.
2550 inline Relocatable_relocs::Reloc_strategy
2551 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2552 {
2553 if (r_type == 0 && r_sym == 0)
2554 return Relocatable_relocs::RELOC_DISCARD;
2555 return Relocatable_relocs::RELOC_COPY;
2556 }
2557
2558 // Return the strategy to use for a local symbol which is a section
2559 // symbol, given the relocation type.
2560 inline Relocatable_relocs::Reloc_strategy
2561 local_section_strategy(unsigned int, Relobj*)
2562 {
2563 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2564 }
2565
2566 // Return the strategy to use for a global symbol, given the
2567 // relocation type, the object, and the symbol index.
2568 inline Relocatable_relocs::Reloc_strategy
2569 global_strategy(unsigned int r_type, Relobj*, unsigned int)
2570 {
2571 if (r_type == elfcpp::R_PPC_PLTREL24)
2572 return Relocatable_relocs::RELOC_SPECIAL;
2573 return Relocatable_relocs::RELOC_COPY;
2574 }
2575};
42cacb20
DE
2576
2577// Scan the relocs during a relocatable link.
2578
2579template<int size, bool big_endian>
2580void
2581Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
2582 Symbol_table* symtab,
2583 Layout* layout,
2584 Sized_relobj_file<size, big_endian>* object,
2585 unsigned int data_shndx,
2586 unsigned int sh_type,
2587 const unsigned char* prelocs,
2588 size_t reloc_count,
2589 Output_section* output_section,
2590 bool needs_special_offset_handling,
2591 size_t local_symbol_count,
2592 const unsigned char* plocal_symbols,
2593 Relocatable_relocs* rr)
42cacb20
DE
2594{
2595 gold_assert(sh_type == elfcpp::SHT_RELA);
2596
42cacb20 2597 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
d83ce4e3 2598 Powerpc_scan_relocatable_reloc>(
42cacb20
DE
2599 symtab,
2600 layout,
2601 object,
2602 data_shndx,
2603 prelocs,
2604 reloc_count,
2605 output_section,
2606 needs_special_offset_handling,
2607 local_symbol_count,
2608 plocal_symbols,
2609 rr);
2610}
2611
2612// Relocate a section during a relocatable link.
2613
2614template<int size, bool big_endian>
2615void
2616Target_powerpc<size, big_endian>::relocate_for_relocatable(
2617 const Relocate_info<size, big_endian>* relinfo,
2618 unsigned int sh_type,
2619 const unsigned char* prelocs,
2620 size_t reloc_count,
2621 Output_section* output_section,
2622 off_t offset_in_output_section,
2623 const Relocatable_relocs* rr,
cf43a2fe
AM
2624 unsigned char*,
2625 typename elfcpp::Elf_types<size>::Elf_Addr,
2626 section_size_type,
42cacb20
DE
2627 unsigned char* reloc_view,
2628 section_size_type reloc_view_size)
2629{
2630 gold_assert(sh_type == elfcpp::SHT_RELA);
2631
cf43a2fe
AM
2632 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2633 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2634 Reltype;
2635 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2636 Reltype_write;
2637 const int reloc_size
2638 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2639 const Address invalid_address = static_cast<Address>(0) - 1;
2640
2641 Powerpc_relobj<size, big_endian>* const object
2642 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2643 const unsigned int local_count = object->local_symbol_count();
2644 unsigned int got2_shndx = object->got2_shndx();
2645 typename elfcpp::Elf_types<size>::Elf_Swxword got2_addend = 0;
2646 if (got2_shndx != 0)
2647 got2_addend = object->get_output_section_offset(got2_shndx);
2648
2649 unsigned char* pwrite = reloc_view;
2650
2651 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2652 {
2653 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2654 if (strategy == Relocatable_relocs::RELOC_DISCARD)
2655 continue;
2656
2657 Reltype reloc(prelocs);
2658 Reltype_write reloc_write(pwrite);
2659
2660 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2661 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2662 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2663
2664 // Get the new symbol index.
2665
2666 unsigned int new_symndx;
2667 if (r_sym < local_count)
2668 {
2669 switch (strategy)
2670 {
2671 case Relocatable_relocs::RELOC_COPY:
2672 case Relocatable_relocs::RELOC_SPECIAL:
2673 new_symndx = object->symtab_index(r_sym);
2674 gold_assert(new_symndx != -1U);
2675 break;
2676
2677 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2678 {
2679 // We are adjusting a section symbol. We need to find
2680 // the symbol table index of the section symbol for
2681 // the output section corresponding to input section
2682 // in which this symbol is defined.
2683 gold_assert(r_sym < local_count);
2684 bool is_ordinary;
2685 unsigned int shndx =
2686 object->local_symbol_input_shndx(r_sym, &is_ordinary);
2687 gold_assert(is_ordinary);
2688 Output_section* os = object->output_section(shndx);
2689 gold_assert(os != NULL);
2690 gold_assert(os->needs_symtab_index());
2691 new_symndx = os->symtab_index();
2692 }
2693 break;
2694
2695 default:
2696 gold_unreachable();
2697 }
2698 }
2699 else
2700 {
2701 const Symbol* gsym = object->global_symbol(r_sym);
2702 gold_assert(gsym != NULL);
2703 if (gsym->is_forwarder())
2704 gsym = relinfo->symtab->resolve_forwards(gsym);
2705
2706 gold_assert(gsym->has_symtab_index());
2707 new_symndx = gsym->symtab_index();
2708 }
2709
2710 // Get the new offset--the location in the output section where
2711 // this relocation should be applied.
2712
2713 Address offset = reloc.get_r_offset();
2714 Address new_offset;
2715 if (static_cast<Address>(offset_in_output_section) != invalid_address)
2716 new_offset = offset + offset_in_output_section;
2717 else
2718 {
2719 section_offset_type sot_offset =
2720 convert_types<section_offset_type, Address>(offset);
2721 section_offset_type new_sot_offset =
2722 output_section->output_offset(object, relinfo->data_shndx,
2723 sot_offset);
2724 gold_assert(new_sot_offset != -1);
2725 new_offset = new_sot_offset;
2726 }
2727
2728 reloc_write.put_r_offset(new_offset);
2729 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2730
2731 // Handle the reloc addend based on the strategy.
2732 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2733 addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2734 get_reloc_addend(&reloc);
2735
2736 if (strategy == Relocatable_relocs::RELOC_COPY)
2737 ;
2738 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2739 {
2740 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2741
2742 addend = psymval->value(object, addend);
2743 }
2744 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2745 {
2746 if (addend >= 32768)
2747 addend += got2_addend;
2748 }
2749 else
2750 gold_unreachable();
2751
2752 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2753 set_reloc_addend(&reloc_write, addend);
2754
2755 pwrite += reloc_size;
2756 }
2757
2758 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2759 == reloc_view_size);
42cacb20
DE
2760}
2761
2762// Return the value to use for a dynamic which requires special
2763// treatment. This is how we support equality comparisons of function
2764// pointers across shared library boundaries, as described in the
2765// processor specific ABI supplement.
2766
2767template<int size, bool big_endian>
2768uint64_t
2769Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2770{
cf43a2fe
AM
2771 if (size == 32)
2772 {
2773 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2774 return this->plt_section()->address() + gsym->plt_offset();
2775 }
2776 else
2777 gold_unreachable();
42cacb20
DE
2778}
2779
2780// The selector for powerpc object files.
2781
2782template<int size, bool big_endian>
2783class Target_selector_powerpc : public Target_selector
2784{
2785public:
2786 Target_selector_powerpc()
2787 : Target_selector(elfcpp::EM_NONE, size, big_endian,
03ef7571
ILT
2788 (size == 64
2789 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2790 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2791 (size == 64
2792 ? (big_endian ? "elf64ppc" : "elf64lppc")
2793 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
2794 { }
2795
2e702c99
RM
2796 virtual Target*
2797 do_recognize(Input_file*, off_t, int machine, int, int)
42cacb20
DE
2798 {
2799 switch (size)
2800 {
2801 case 64:
2ea97941 2802 if (machine != elfcpp::EM_PPC64)
42cacb20
DE
2803 return NULL;
2804 break;
2805
2806 case 32:
2ea97941 2807 if (machine != elfcpp::EM_PPC)
42cacb20
DE
2808 return NULL;
2809 break;
2810
2811 default:
2812 return NULL;
2813 }
2814
7f055c20 2815 return this->instantiate_target();
42cacb20
DE
2816 }
2817
2e702c99
RM
2818 virtual Target*
2819 do_instantiate_target()
7f055c20 2820 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
2821};
2822
2823Target_selector_powerpc<32, true> target_selector_ppc32;
2824Target_selector_powerpc<32, false> target_selector_ppc32le;
2825Target_selector_powerpc<64, true> target_selector_ppc64;
2826Target_selector_powerpc<64, false> target_selector_ppc64le;
2827
2828} // End anonymous namespace.
This page took 0.318913 seconds and 4 git commands to generate.