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