* object.h (Sized_relobj_file::emit_relocs): 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:
dd93cd0a 60 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
c9269dff
AM
61 typedef typename elfcpp::Elf_types<size>::Elf_Off Offset;
62
cf43a2fe
AM
63 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
64 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
65 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
c9269dff 66 special_(0), opd_ent_shndx_(), opd_ent_off_()
cf43a2fe
AM
67 { }
68
69 ~Powerpc_relobj()
70 { }
71
c9269dff 72 // The .got2 section shndx.
cf43a2fe
AM
73 unsigned int
74 got2_shndx() const
75 {
76 if (size == 32)
c9269dff 77 return this->special_;
cf43a2fe
AM
78 else
79 return 0;
80 }
81
c9269dff
AM
82 // The .opd section shndx.
83 unsigned int
84 opd_shndx() const
85 {
86 if (size == 32)
87 return 0;
88 else
89 return this->special_;
90 }
91
92 // Init OPD entry arrays.
93 void
94 init_opd(size_t opd_size)
95 {
96 size_t count = this->opd_ent_ndx(opd_size);
97 this->opd_ent_shndx_.resize(count);
98 this->opd_ent_off_.reserve(count);
99 }
100
101 // Return section and offset of function entry for .opd + R_OFF.
102 void
dd93cd0a 103 get_opd_ent(Address r_off, unsigned int* shndx, Address* value)
c9269dff
AM
104 {
105 size_t ndx = this->opd_ent_ndx(r_off);
106 gold_assert(ndx < this->opd_ent_shndx_.size());
107 gold_assert(this->opd_ent_shndx_[ndx] != 0);
108 *shndx = this->opd_ent_shndx_[ndx];
109 *value = this->opd_ent_off_[ndx];
110 }
111
112 // Set section and offset of function entry for .opd + R_OFF.
113 void
dd93cd0a 114 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
115 {
116 size_t ndx = this->opd_ent_ndx(r_off);
117 gold_assert(ndx < this->opd_ent_shndx_.size());
118 this->opd_ent_shndx_[ndx] = shndx;
119 this->opd_ent_off_[ndx] = value;
120 }
121
122 // Examine .rela.opd to build info about function entry points.
123 void
124 scan_opd_relocs(size_t reloc_count,
125 const unsigned char* prelocs,
126 const unsigned char* plocal_syms);
127
128 void
129 do_read_relocs(Read_relocs_data*);
130
cf43a2fe
AM
131 bool
132 do_find_special_sections(Read_symbols_data* sd);
133
dd93cd0a
AM
134 // Return offset in output GOT section that this object will use
135 // as a TOC pointer. Won't be just a constant with multi-toc support.
136 Address
137 toc_base_offset() const
138 { return 0x8000; }
139
cf43a2fe 140private:
c9269dff
AM
141 // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
142 // at OFF. .opd entries are 24 bytes long, but they can be spaced
143 // 16 bytes apart when the language doesn't use the last 8-byte
144 // word, the environment pointer. Thus dividing the entry section
145 // offset by 16 will give an index into opd_ent_shndx_ and
146 // opd_ent_off_ that works for either layout of .opd. (It leaves
147 // some elements of the vectors unused when .opd entries are spaced
148 // 24 bytes apart, but we don't know the spacing until relocations
149 // are processed, and in any case it is possible for an object to
150 // have some entries spaced 16 bytes apart and others 24 bytes apart.)
151 size_t
152 opd_ent_ndx(size_t off) const
153 { return off >> 4;}
154
155 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
156 unsigned int special_;
157 // The first 8-byte word of an OPD entry gives the address of the
158 // entry point of the function. Relocatable object files have a
159 // relocation on this word. The following two vectors record the
160 // section and offset specified by these relocations.
161 std::vector<unsigned int> opd_ent_shndx_;
162 std::vector<Offset> opd_ent_off_;
cf43a2fe
AM
163};
164
42cacb20
DE
165template<int size, bool big_endian>
166class Target_powerpc : public Sized_target<size, big_endian>
167{
168 public:
d83ce4e3
AM
169 typedef
170 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 171 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 172 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
c9269dff 173 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
174 // Offset of tp and dtp pointers from start of TLS block.
175 static const Address tp_offset = 0x7000;
176 static const Address dtp_offset = 0x8000;
42cacb20
DE
177
178 Target_powerpc()
179 : Sized_target<size, big_endian>(&powerpc_info),
cf43a2fe 180 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
42cacb20 181 copy_relocs_(elfcpp::R_POWERPC_COPY),
dd93cd0a 182 dynbss_(NULL), tlsld_got_offset_(-1U)
42cacb20
DE
183 {
184 }
185
2e702c99 186 // Process the relocations to determine unreferenced sections for
6d03d481
ST
187 // garbage collection.
188 void
ad0f2072 189 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
190 Layout* layout,
191 Sized_relobj_file<size, big_endian>* object,
192 unsigned int data_shndx,
193 unsigned int sh_type,
194 const unsigned char* prelocs,
195 size_t reloc_count,
196 Output_section* output_section,
197 bool needs_special_offset_handling,
198 size_t local_symbol_count,
199 const unsigned char* plocal_symbols);
6d03d481 200
42cacb20
DE
201 // Scan the relocations to look for symbol adjustments.
202 void
ad0f2072 203 scan_relocs(Symbol_table* symtab,
42cacb20 204 Layout* layout,
6fa2a40b 205 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
206 unsigned int data_shndx,
207 unsigned int sh_type,
208 const unsigned char* prelocs,
209 size_t reloc_count,
210 Output_section* output_section,
211 bool needs_special_offset_handling,
212 size_t local_symbol_count,
213 const unsigned char* plocal_symbols);
921b5322
AM
214
215 // Map input .toc section to output .got section.
216 const char*
217 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
218 {
219 if (size == 64 && strcmp(name, ".toc") == 0)
220 {
221 *plen = 4;
222 return ".got";
223 }
224 return NULL;
225 }
226
42cacb20
DE
227 // Finalize the sections.
228 void
f59f41f3 229 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
230
231 // Return the value to use for a dynamic which requires special
232 // treatment.
233 uint64_t
234 do_dynsym_value(const Symbol*) const;
235
236 // Relocate a section.
237 void
238 relocate_section(const Relocate_info<size, big_endian>*,
239 unsigned int sh_type,
240 const unsigned char* prelocs,
241 size_t reloc_count,
242 Output_section* output_section,
243 bool needs_special_offset_handling,
244 unsigned char* view,
c9269dff 245 Address view_address,
364c7fa5
ILT
246 section_size_type view_size,
247 const Reloc_symbol_changes*);
42cacb20
DE
248
249 // Scan the relocs during a relocatable link.
250 void
ad0f2072 251 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 252 Layout* layout,
6fa2a40b 253 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
254 unsigned int data_shndx,
255 unsigned int sh_type,
256 const unsigned char* prelocs,
257 size_t reloc_count,
258 Output_section* output_section,
259 bool needs_special_offset_handling,
260 size_t local_symbol_count,
261 const unsigned char* plocal_symbols,
262 Relocatable_relocs*);
263
7404fe1b 264 // Emit relocations for a section.
42cacb20 265 void
7404fe1b
AM
266 relocate_relocs(const Relocate_info<size, big_endian>*,
267 unsigned int sh_type,
268 const unsigned char* prelocs,
269 size_t reloc_count,
270 Output_section* output_section,
271 off_t offset_in_output_section,
272 const Relocatable_relocs*,
273 unsigned char*,
274 Address view_address,
275 section_size_type,
276 unsigned char* reloc_view,
277 section_size_type reloc_view_size);
42cacb20
DE
278
279 // Return whether SYM is defined by the ABI.
280 bool
9c2d0ef9 281 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 282 {
cf43a2fe 283 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
284 }
285
286 // Return the size of the GOT section.
287 section_size_type
0e70b911 288 got_size() const
42cacb20
DE
289 {
290 gold_assert(this->got_ != NULL);
291 return this->got_->data_size();
292 }
293
cf43a2fe
AM
294 // Get the PLT section.
295 const Output_data_plt_powerpc<size, big_endian>*
296 plt_section() const
297 {
298 gold_assert(this->plt_ != NULL);
299 return this->plt_;
300 }
301
302 // Get the .glink section.
303 const Output_data_glink<size, big_endian>*
304 glink_section() const
305 {
306 gold_assert(this->glink_ != NULL);
307 return this->glink_;
308 }
309
310 // Get the GOT section.
311 const Output_data_got_powerpc<size, big_endian>*
312 got_section() const
313 {
314 gold_assert(this->got_ != NULL);
315 return this->got_;
316 }
317
cf43a2fe
AM
318 Object*
319 do_make_elf_object(const std::string&, Input_file*, off_t,
320 const elfcpp::Ehdr<size, big_endian>&);
321
0e70b911
CC
322 // Return the number of entries in the GOT.
323 unsigned int
324 got_entry_count() const
325 {
326 if (this->got_ == NULL)
327 return 0;
328 return this->got_size() / (size / 8);
329 }
330
331 // Return the number of entries in the PLT.
332 unsigned int
333 plt_entry_count() const;
334
335 // Return the offset of the first non-reserved PLT entry.
336 unsigned int
337 first_plt_entry_offset() const;
338
339 // Return the size of each PLT entry.
340 unsigned int
341 plt_entry_size() const;
342
42cacb20
DE
343 private:
344
345 // The class which scans relocations.
346 class Scan
347 {
348 public:
349 Scan()
350 : issued_non_pic_error_(false)
351 { }
352
95a2c8d6
RS
353 static inline int
354 get_reference_flags(unsigned int r_type);
355
42cacb20 356 inline void
ad0f2072 357 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 358 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
359 unsigned int data_shndx,
360 Output_section* output_section,
361 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
362 const elfcpp::Sym<size, big_endian>& lsym);
363
364 inline void
ad0f2072 365 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 366 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
367 unsigned int data_shndx,
368 Output_section* output_section,
369 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
370 Symbol* gsym);
371
21bb3914
ST
372 inline bool
373 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
374 Target_powerpc* ,
2e702c99 375 Sized_relobj_file<size, big_endian>* ,
21bb3914 376 unsigned int ,
2e702c99
RM
377 Output_section* ,
378 const elfcpp::Rela<size, big_endian>& ,
379 unsigned int ,
380 const elfcpp::Sym<size, big_endian>&)
21bb3914
ST
381 { return false; }
382
383 inline bool
384 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
385 Target_powerpc* ,
2e702c99
RM
386 Sized_relobj_file<size, big_endian>* ,
387 unsigned int ,
388 Output_section* ,
389 const elfcpp::Rela<size,
21bb3914
ST
390 big_endian>& ,
391 unsigned int , Symbol*)
392 { return false; }
393
42cacb20
DE
394 private:
395 static void
6fa2a40b 396 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
397 unsigned int r_type);
398
399 static void
6fa2a40b 400 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
401 unsigned int r_type, Symbol*);
402
403 static void
404 generate_tls_call(Symbol_table* symtab, Layout* layout,
405 Target_powerpc* target);
406
407 void
408 check_non_pic(Relobj*, unsigned int r_type);
409
410 // Whether we have issued an error about a non-PIC compilation.
411 bool issued_non_pic_error_;
412 };
413
414 // The class which implements relocation.
415 class Relocate
416 {
417 public:
dd93cd0a
AM
418 // Use 'at' branch hints when true, 'y' when false.
419 // FIXME maybe: set this with an option.
420 static const bool is_isa_v2 = true;
421
422 enum skip_tls
423 {
424 CALL_NOT_EXPECTED = 0,
425 CALL_EXPECTED = 1,
426 CALL_SKIP = 2
427 };
428
429 Relocate()
430 : call_tls_get_addr_(CALL_NOT_EXPECTED)
431 { }
432
433 ~Relocate()
434 {
435 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
436 {
437 // FIXME: This needs to specify the location somehow.
438 gold_error(_("missing expected __tls_get_addr call"));
439 }
440 }
441
42cacb20
DE
442 // Do a relocation. Return false if the caller should not issue
443 // any warnings about this relocation.
444 inline bool
445 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
031cdbed
ILT
446 Output_section*, size_t relnum,
447 const elfcpp::Rela<size, big_endian>&,
42cacb20
DE
448 unsigned int r_type, const Sized_symbol<size>*,
449 const Symbol_value<size>*,
450 unsigned char*,
451 typename elfcpp::Elf_types<size>::Elf_Addr,
452 section_size_type);
453
dd93cd0a
AM
454 // This is set if we should skip the next reloc, which should be a
455 // call to __tls_get_addr.
456 enum skip_tls call_tls_get_addr_;
42cacb20
DE
457 };
458
459 // A class which returns the size required for a relocation type,
460 // used while scanning relocs during a relocatable link.
461 class Relocatable_size_for_reloc
462 {
463 public:
464 unsigned int
cf43a2fe
AM
465 get_size_for_reloc(unsigned int, Relobj*)
466 {
467 gold_unreachable();
468 return 0;
469 }
42cacb20
DE
470 };
471
dd93cd0a
AM
472 // Optimize the TLS relocation type based on what we know about the
473 // symbol. IS_FINAL is true if the final address of this symbol is
474 // known at link time.
475
476 tls::Tls_optimization
477 optimize_tls_gd(bool is_final)
478 {
479 // If we are generating a shared library, then we can't do anything
480 // in the linker.
481 if (parameters->options().shared())
482 return tls::TLSOPT_NONE;
483
484 if (!is_final)
485 return tls::TLSOPT_TO_IE;
486 return tls::TLSOPT_TO_LE;
487 }
488
489 tls::Tls_optimization
490 optimize_tls_ld()
491 {
492 if (parameters->options().shared())
493 return tls::TLSOPT_NONE;
494
495 return tls::TLSOPT_TO_LE;
496 }
497
498 tls::Tls_optimization
499 optimize_tls_ie(bool is_final)
500 {
501 if (!is_final || parameters->options().shared())
502 return tls::TLSOPT_NONE;
503
504 return tls::TLSOPT_TO_LE;
505 }
cf43a2fe 506
42cacb20 507 // Get the GOT section, creating it if necessary.
cf43a2fe 508 Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
509 got_section(Symbol_table*, Layout*);
510
cf43a2fe
AM
511 // Create glink.
512 void
513 make_glink_section(Layout*);
42cacb20 514
cf43a2fe
AM
515 // Create the PLT section.
516 void
517 make_plt_section(Layout*);
42cacb20
DE
518
519 // Create a PLT entry for a global symbol.
520 void
cf43a2fe
AM
521 make_plt_entry(Layout*, Symbol*,
522 const elfcpp::Rela<size, big_endian>&,
523 const Sized_relobj<size, big_endian>* object);
42cacb20 524
dd93cd0a
AM
525 // Create a GOT entry for local dynamic __tls_get_addr.
526 unsigned int
527 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
528 Sized_relobj_file<size, big_endian>* object);
529
42cacb20 530 unsigned int
dd93cd0a
AM
531 tlsld_got_offset() const
532 {
533 return this->tlsld_got_offset_;
534 }
42cacb20 535
42cacb20
DE
536 // Get the dynamic reloc section, creating it if necessary.
537 Reloc_section*
538 rela_dyn_section(Layout*);
539
42cacb20
DE
540 // Copy a relocation against a global symbol.
541 void
ef9beddf 542 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 543 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
544 unsigned int shndx, Output_section* output_section,
545 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
546 {
547 this->copy_relocs_.copy_reloc(symtab, layout,
548 symtab->get_sized_symbol<size>(sym),
549 object, shndx, output_section,
550 reloc, this->rela_dyn_section(layout));
551 }
552
553 // Information about this specific target which we pass to the
554 // general Target structure.
555 static Target::Target_info powerpc_info;
556
557 // The types of GOT entries needed for this platform.
0e70b911
CC
558 // These values are exposed to the ABI in an incremental link.
559 // Do not renumber existing values without changing the version
560 // number of the .gnu_incremental_inputs section.
42cacb20
DE
561 enum Got_type
562 {
dd93cd0a
AM
563 GOT_TYPE_STANDARD,
564 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
565 GOT_TYPE_DTPREL, // entry for @got@dtprel
566 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
567 };
568
cf43a2fe
AM
569 // The GOT output section.
570 Output_data_got_powerpc<size, big_endian>* got_;
571 // The PLT output section.
42cacb20 572 Output_data_plt_powerpc<size, big_endian>* plt_;
cf43a2fe
AM
573 // The .glink output section.
574 Output_data_glink<size, big_endian>* glink_;
575 // The dynamic reloc output section.
42cacb20
DE
576 Reloc_section* rela_dyn_;
577 // Relocs saved to avoid a COPY reloc.
578 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
579 // Space for variables copied with a COPY reloc.
580 Output_data_space* dynbss_;
dd93cd0a
AM
581 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
582 unsigned int tlsld_got_offset_;
42cacb20
DE
583};
584
585template<>
586Target::Target_info Target_powerpc<32, true>::powerpc_info =
587{
588 32, // size
589 true, // is_big_endian
590 elfcpp::EM_PPC, // machine_code
591 false, // has_make_symbol
592 false, // has_resolve
593 false, // has_code_fill
594 true, // is_default_stack_executable
b3ce541e 595 false, // can_icf_inline_merge_sections
42cacb20
DE
596 '\0', // wrap_char
597 "/usr/lib/ld.so.1", // dynamic_linker
598 0x10000000, // default_text_segment_address
599 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 600 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
601 false, // isolate_execinstr
602 0, // rosegment_gap
8a5e3e08
ILT
603 elfcpp::SHN_UNDEF, // small_common_shndx
604 elfcpp::SHN_UNDEF, // large_common_shndx
605 0, // small_common_section_flags
05a352e6
DK
606 0, // large_common_section_flags
607 NULL, // attributes_section
608 NULL // attributes_vendor
42cacb20
DE
609};
610
611template<>
612Target::Target_info Target_powerpc<32, false>::powerpc_info =
613{
614 32, // size
615 false, // is_big_endian
616 elfcpp::EM_PPC, // machine_code
617 false, // has_make_symbol
618 false, // has_resolve
619 false, // has_code_fill
620 true, // is_default_stack_executable
b3ce541e 621 false, // can_icf_inline_merge_sections
42cacb20
DE
622 '\0', // wrap_char
623 "/usr/lib/ld.so.1", // dynamic_linker
624 0x10000000, // default_text_segment_address
625 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 626 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
627 false, // isolate_execinstr
628 0, // rosegment_gap
8a5e3e08
ILT
629 elfcpp::SHN_UNDEF, // small_common_shndx
630 elfcpp::SHN_UNDEF, // large_common_shndx
631 0, // small_common_section_flags
05a352e6
DK
632 0, // large_common_section_flags
633 NULL, // attributes_section
634 NULL // attributes_vendor
42cacb20
DE
635};
636
637template<>
638Target::Target_info Target_powerpc<64, true>::powerpc_info =
639{
640 64, // size
641 true, // is_big_endian
642 elfcpp::EM_PPC64, // machine_code
643 false, // has_make_symbol
644 false, // has_resolve
645 false, // has_code_fill
646 true, // is_default_stack_executable
b3ce541e 647 false, // can_icf_inline_merge_sections
42cacb20
DE
648 '\0', // wrap_char
649 "/usr/lib/ld.so.1", // dynamic_linker
650 0x10000000, // default_text_segment_address
651 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 652 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
653 false, // isolate_execinstr
654 0, // rosegment_gap
8a5e3e08
ILT
655 elfcpp::SHN_UNDEF, // small_common_shndx
656 elfcpp::SHN_UNDEF, // large_common_shndx
657 0, // small_common_section_flags
05a352e6
DK
658 0, // large_common_section_flags
659 NULL, // attributes_section
660 NULL // attributes_vendor
42cacb20
DE
661};
662
663template<>
664Target::Target_info Target_powerpc<64, false>::powerpc_info =
665{
666 64, // size
667 false, // is_big_endian
668 elfcpp::EM_PPC64, // machine_code
669 false, // has_make_symbol
670 false, // has_resolve
671 false, // has_code_fill
672 true, // is_default_stack_executable
b3ce541e 673 false, // can_icf_inline_merge_sections
42cacb20
DE
674 '\0', // wrap_char
675 "/usr/lib/ld.so.1", // dynamic_linker
676 0x10000000, // default_text_segment_address
677 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 678 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
679 false, // isolate_execinstr
680 0, // rosegment_gap
8a5e3e08
ILT
681 elfcpp::SHN_UNDEF, // small_common_shndx
682 elfcpp::SHN_UNDEF, // large_common_shndx
683 0, // small_common_section_flags
05a352e6
DK
684 0, // large_common_section_flags
685 NULL, // attributes_section
686 NULL // attributes_vendor
42cacb20
DE
687};
688
dd93cd0a
AM
689inline bool
690is_branch_reloc(unsigned int r_type)
691{
692 return (r_type == elfcpp::R_POWERPC_REL24
693 || r_type == elfcpp::R_PPC_PLTREL24
694 || r_type == elfcpp::R_PPC_LOCAL24PC
695 || r_type == elfcpp::R_POWERPC_REL14
696 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
697 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
698 || r_type == elfcpp::R_POWERPC_ADDR24
699 || r_type == elfcpp::R_POWERPC_ADDR14
700 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
701 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
702}
703
704// If INSN is an opcode that may be used with an @tls operand, return
705// the transformed insn for TLS optimisation, otherwise return 0. If
706// REG is non-zero only match an insn with RB or RA equal to REG.
707uint32_t
708at_tls_transform(uint32_t insn, unsigned int reg)
709{
710 if ((insn & (0x3f << 26)) != 31 << 26)
711 return 0;
712
713 unsigned int rtra;
714 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
715 rtra = insn & ((1 << 26) - (1 << 16));
716 else if (((insn >> 16) & 0x1f) == reg)
717 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
718 else
719 return 0;
720
721 if ((insn & (0x3ff << 1)) == 266 << 1)
722 // add -> addi
723 insn = 14 << 26;
724 else if ((insn & (0x1f << 1)) == 23 << 1
725 && ((insn & (0x1f << 6)) < 14 << 6
726 || ((insn & (0x1f << 6)) >= 16 << 6
727 && (insn & (0x1f << 6)) < 24 << 6)))
728 // load and store indexed -> dform
729 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
730 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
731 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
732 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
733 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
734 // lwax -> lwa
735 insn = (58 << 26) | 2;
736 else
737 return 0;
738 insn |= rtra;
739 return insn;
740}
741
742// Modified version of symtab.h class Symbol member
743// Given a direct absolute or pc-relative static relocation against
744// the global symbol, this function returns whether a dynamic relocation
745// is needed.
746
747template<int size>
748bool
749needs_dynamic_reloc(const Symbol* gsym, int flags)
750{
751 // No dynamic relocations in a static link!
752 if (parameters->doing_static_link())
753 return false;
754
755 // A reference to an undefined symbol from an executable should be
756 // statically resolved to 0, and does not need a dynamic relocation.
757 // This matches gnu ld behavior.
758 if (gsym->is_undefined() && !parameters->options().shared())
759 return false;
760
761 // A reference to an absolute symbol does not need a dynamic relocation.
762 if (gsym->is_absolute())
763 return false;
764
765 // An absolute reference within a position-independent output file
766 // will need a dynamic relocation.
767 if ((flags & Symbol::ABSOLUTE_REF)
768 && parameters->options().output_is_position_independent())
769 return true;
770
771 // A function call that can branch to a local PLT entry does not need
772 // a dynamic relocation.
773 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
774 return false;
775
776 // A reference to any PLT entry in a non-position-independent executable
777 // does not need a dynamic relocation.
778 // Except due to having function descriptors on powerpc64 we don't define
779 // functions to their plt code in an executable, so this doesn't apply.
780 if (size == 32
781 && !parameters->options().output_is_position_independent()
782 && gsym->has_plt_offset())
783 return false;
784
785 // A reference to a symbol defined in a dynamic object or to a
786 // symbol that is preemptible will need a dynamic relocation.
787 if (gsym->is_from_dynobj()
788 || gsym->is_undefined()
789 || gsym->is_preemptible())
790 return true;
791
792 // For all other cases, return FALSE.
793 return false;
794}
795
796// Modified version of symtab.h class Symbol member
797// Whether we should use the PLT offset associated with a symbol for
798// a relocation. FLAGS is a set of Reference_flags.
799
800template<int size>
801bool
802use_plt_offset(const Symbol* gsym, int flags)
803{
804 // If the symbol doesn't have a PLT offset, then naturally we
805 // don't want to use it.
806 if (!gsym->has_plt_offset())
807 return false;
808
809 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
810 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
811 return true;
812
813 // If we are going to generate a dynamic relocation, then we will
814 // wind up using that, so no need to use the PLT entry.
815 if (needs_dynamic_reloc<size>(gsym, flags))
816 return false;
817
818 // If the symbol is from a dynamic object, we need to use the PLT
819 // entry.
820 if (gsym->is_from_dynobj())
821 return true;
822
823 // If we are generating a shared object, and gsym symbol is
824 // undefined or preemptible, we need to use the PLT entry.
825 if (parameters->options().shared()
826 && (gsym->is_undefined() || gsym->is_preemptible()))
827 return true;
828
829 // If gsym is a call to a weak undefined symbol, we need to use
830 // the PLT entry; the symbol may be defined by a library loaded
831 // at runtime.
832 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
833 return true;
834
835 // Otherwise we can use the regular definition.
836 return false;
837}
838
42cacb20
DE
839template<int size, bool big_endian>
840class Powerpc_relocate_functions
841{
dd93cd0a
AM
842public:
843 enum overflow_check
844 {
845 check_none,
846 check_signed,
847 check_bitfield
848 };
849
850 enum overflow_status
851 {
852 status_ok,
853 status_overflow
854 };
855
42cacb20 856private:
c9269dff 857 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff
AM
858 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
859
dd93cd0a
AM
860 template<int valsize>
861 static inline bool
862 has_overflow_signed(Address value)
863 {
864 // limit = 1 << (valsize - 1) without shift count exceeding size of type
865 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
866 limit <<= ((valsize - 1) >> 1);
867 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
868 return value + limit > (limit << 1) - 1;
869 }
870
871 template<int valsize>
872 static inline bool
873 has_overflow_bitfield(Address value)
874 {
875 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
876 limit <<= ((valsize - 1) >> 1);
877 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
878 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
879 }
880
881 template<int valsize>
882 static inline enum overflow_status
883 overflowed(Address value, enum overflow_check overflow)
884 {
885 if (overflow == check_signed)
886 {
887 if (has_overflow_signed<valsize>(value))
888 return status_overflow;
889 }
890 else if (overflow == check_bitfield)
891 {
892 if (has_overflow_bitfield<valsize>(value))
893 return status_overflow;
894 }
895 return status_ok;
896 }
897
cf43a2fe 898 // Do a simple RELA relocation
42cacb20 899 template<int valsize>
dd93cd0a
AM
900 static inline enum overflow_status
901 rela(unsigned char* view, Address value, enum overflow_check overflow)
902 {
903 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
904 Valtype* wv = reinterpret_cast<Valtype*>(view);
905 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
906 return overflowed<valsize>(value, overflow);
907 }
908
909 template<int valsize>
910 static inline enum overflow_status
42cacb20
DE
911 rela(unsigned char* view,
912 unsigned int right_shift,
c9269dff
AM
913 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
914 Address value,
dd93cd0a 915 enum overflow_check overflow)
42cacb20
DE
916 {
917 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
918 Valtype* wv = reinterpret_cast<Valtype*>(view);
919 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
dd93cd0a 920 Valtype reloc = value >> right_shift;
42cacb20
DE
921 val &= ~dst_mask;
922 reloc &= dst_mask;
42cacb20 923 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
dd93cd0a 924 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
925 }
926
cf43a2fe 927 // Do a simple RELA relocation, unaligned.
42cacb20 928 template<int valsize>
dd93cd0a
AM
929 static inline enum overflow_status
930 rela_ua(unsigned char* view, Address value, enum overflow_check overflow)
931 {
932 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
933 return overflowed<valsize>(value, overflow);
934 }
935
936 template<int valsize>
937 static inline enum overflow_status
cf43a2fe
AM
938 rela_ua(unsigned char* view,
939 unsigned int right_shift,
c9269dff
AM
940 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
941 Address value,
dd93cd0a 942 enum overflow_check overflow)
42cacb20 943 {
c9269dff
AM
944 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
945 Valtype;
dd93cd0a
AM
946 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
947 Valtype reloc = value >> right_shift;
42cacb20
DE
948 val &= ~dst_mask;
949 reloc &= dst_mask;
dd93cd0a
AM
950 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
951 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
952 }
953
42cacb20 954public:
dd93cd0a 955 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 956 static inline void
dd93cd0a
AM
957 addr64(unsigned char* view, Address value)
958 { This::template rela<64>(view, value, check_none); }
42cacb20 959
dd93cd0a 960 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 961 static inline void
dd93cd0a
AM
962 addr64_u(unsigned char* view, Address value)
963 { This::template rela_ua<64>(view, value, check_none); }
964
965 // R_POWERPC_ADDR32: (Symbol + Addend)
966 static inline enum overflow_status
967 addr32(unsigned char* view, Address value, enum overflow_check overflow)
968 { return This::template rela<32>(view, value, overflow); }
969
970 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
971 static inline enum overflow_status
972 addr32_u(unsigned char* view, Address value, enum overflow_check overflow)
973 { return This::template rela_ua<32>(view, value, overflow); }
974
975 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
976 static inline enum overflow_status
977 addr24(unsigned char* view, Address value, enum overflow_check overflow)
978 {
979 enum overflow_status stat
980 = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
981 if (overflow != check_none && (value & 3) != 0)
982 stat = status_overflow;
983 return stat;
984 }
42cacb20
DE
985
986 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
dd93cd0a
AM
987 static inline enum overflow_status
988 addr16(unsigned char* view, Address value, enum overflow_check overflow)
989 { return This::template rela<16>(view, value, overflow); }
42cacb20 990
dd93cd0a
AM
991 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
992 static inline enum overflow_status
993 addr16_u(unsigned char* view, Address value, enum overflow_check overflow)
994 { return This::template rela_ua<16>(view, value, overflow); }
42cacb20 995
dd93cd0a
AM
996 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
997 static inline enum overflow_status
998 addr16_ds(unsigned char* view, Address value, enum overflow_check overflow)
999 {
1000 enum overflow_status stat
1001 = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1002 if (overflow != check_none && (value & 3) != 0)
1003 stat = status_overflow;
1004 return stat;
1005 }
42cacb20 1006
42cacb20
DE
1007 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1008 static inline void
dd93cd0a
AM
1009 addr16_hi(unsigned char* view, Address value)
1010 { This::template rela<16>(view, 16, 0xffff, value, check_none); }
42cacb20 1011
c9269dff 1012 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 1013 static inline void
dd93cd0a
AM
1014 addr16_ha(unsigned char* view, Address value)
1015 { This::addr16_hi(view, value + 0x8000); }
42cacb20 1016
dd93cd0a 1017 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 1018 static inline void
dd93cd0a
AM
1019 addr16_hi2(unsigned char* view, Address value)
1020 { This::template rela<16>(view, 32, 0xffff, value, check_none); }
42cacb20 1021
dd93cd0a 1022 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 1023 static inline void
dd93cd0a
AM
1024 addr16_ha2(unsigned char* view, Address value)
1025 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 1026
dd93cd0a 1027 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 1028 static inline void
dd93cd0a
AM
1029 addr16_hi3(unsigned char* view, Address value)
1030 { This::template rela<16>(view, 48, 0xffff, value, check_none); }
42cacb20 1031
dd93cd0a 1032 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 1033 static inline void
dd93cd0a
AM
1034 addr16_ha3(unsigned char* view, Address value)
1035 { This::addr16_hi3(view, value + 0x8000); }
1036
1037 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1038 static inline enum overflow_status
1039 addr14(unsigned char* view, Address value, enum overflow_check overflow)
1040 {
1041 enum overflow_status stat
1042 = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1043 if (overflow != check_none && (value & 3) != 0)
1044 stat = status_overflow;
1045 return stat;
1046 }
cf43a2fe
AM
1047};
1048
c9269dff
AM
1049// Stash away the index of .got2 or .opd in a relocatable object, if
1050// such a section exists.
cf43a2fe
AM
1051
1052template<int size, bool big_endian>
1053bool
1054Powerpc_relobj<size, big_endian>::do_find_special_sections(
1055 Read_symbols_data* sd)
1056{
c9269dff
AM
1057 const unsigned char* const pshdrs = sd->section_headers->data();
1058 const unsigned char* namesu = sd->section_names->data();
1059 const char* names = reinterpret_cast<const char*>(namesu);
1060 section_size_type names_size = sd->section_names_size;
1061 const unsigned char* s;
1062
1063 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1064 names, names_size, NULL);
1065 if (s != NULL)
1066 {
1067 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1068 this->special_ = ndx;
1069 }
1070 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1071}
1072
1073// Examine .rela.opd to build info about function entry points.
1074
1075template<int size, bool big_endian>
1076void
1077Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1078 size_t reloc_count,
1079 const unsigned char* prelocs,
1080 const unsigned char* plocal_syms)
1081{
1082 if (size == 64)
cf43a2fe 1083 {
c9269dff
AM
1084 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1085 Reltype;
1086 const int reloc_size
1087 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1088 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1089
1090 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 1091 {
c9269dff
AM
1092 Reltype reloc(prelocs);
1093 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1094 = reloc.get_r_info();
1095 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1096 if (r_type == elfcpp::R_PPC64_ADDR64)
1097 {
1098 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1099 typename elfcpp::Elf_types<size>::Elf_Addr value;
1100 bool is_ordinary;
1101 unsigned int shndx;
1102 if (r_sym < this->local_symbol_count())
1103 {
1104 typename elfcpp::Sym<size, big_endian>
1105 lsym(plocal_syms + r_sym * sym_size);
1106 shndx = lsym.get_st_shndx();
1107 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1108 value = lsym.get_st_value();
1109 }
1110 else
1111 shndx = this->symbol_section_and_value(r_sym, &value,
1112 &is_ordinary);
1113 this->set_opd_ent(reloc.get_r_offset(), shndx,
1114 value + reloc.get_r_addend());
1115 }
1116 }
1117 }
1118}
1119
1120template<int size, bool big_endian>
1121void
1122Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1123{
1124 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1125 if (size == 64)
1126 {
1127 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1128 p != rd->relocs.end();
1129 ++p)
1130 {
1131 if (p->data_shndx == this->opd_shndx())
1132 {
1133 this->init_opd(this->section_size(this->opd_shndx()));
1134 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1135 rd->local_symbols->data());
1136 break;
1137 }
cf43a2fe
AM
1138 }
1139 }
cf43a2fe
AM
1140}
1141
1142// Set up PowerPC target specific relobj.
1143
1144template<int size, bool big_endian>
1145Object*
1146Target_powerpc<size, big_endian>::do_make_elf_object(
1147 const std::string& name,
1148 Input_file* input_file,
1149 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1150{
1151 int et = ehdr.get_e_type();
957564c9
AS
1152 // ET_EXEC files are valid input for --just-symbols/-R,
1153 // and we treat them as relocatable objects.
1154 if (et == elfcpp::ET_REL
1155 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
1156 {
1157 Powerpc_relobj<size, big_endian>* obj =
c9269dff 1158 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
1159 obj->setup();
1160 return obj;
1161 }
1162 else if (et == elfcpp::ET_DYN)
1163 {
1164 Sized_dynobj<size, big_endian>* obj =
c9269dff 1165 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
1166 obj->setup();
1167 return obj;
1168 }
1169 else
1170 {
c9269dff 1171 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
1172 return NULL;
1173 }
1174}
1175
1176template<int size, bool big_endian>
1177class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1178{
1179public:
1180 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1181 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1182
1183 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1184 : Output_data_got<size, big_endian>(),
1185 symtab_(symtab), layout_(layout),
1186 header_ent_cnt_(size == 32 ? 3 : 1),
1187 header_index_(size == 32 ? 0x2000 : 0)
1188 {}
1189
1190 class Got_entry;
1191
1192 // Create a new GOT entry and return its offset.
1193 unsigned int
1194 add_got_entry(Got_entry got_entry)
42cacb20 1195 {
cf43a2fe
AM
1196 this->reserve_ent();
1197 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1198 }
42cacb20 1199
cf43a2fe
AM
1200 // Create a pair of new GOT entries and return the offset of the first.
1201 unsigned int
1202 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1203 {
1204 this->reserve_ent(2);
1205 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1206 got_entry_2);
1207 }
42cacb20 1208
dd93cd0a
AM
1209 unsigned int
1210 add_constant_pair(Valtype c1, Valtype c2)
1211 {
1212 this->reserve_ent(2);
1213 unsigned int got_offset = this->add_constant(c1);
1214 this->add_constant(c2);
1215 return got_offset;
1216 }
1217
1218 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
1219 unsigned int
1220 g_o_t() const
1221 {
1222 return this->got_offset(this->header_index_);
42cacb20 1223 }
cf43a2fe 1224
dd93cd0a
AM
1225 // Offset of base used to access the GOT/TOC.
1226 // The got/toc pointer reg will be set to this value.
1227 typename elfcpp::Elf_types<size>::Elf_Off
1228 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1229 {
1230 if (size == 32)
1231 return this->g_o_t();
1232 else
1233 return (this->output_section()->address()
1234 + object->toc_base_offset()
1235 - this->address());
1236 }
1237
cf43a2fe
AM
1238 // Ensure our GOT has a header.
1239 void
1240 set_final_data_size()
1241 {
1242 if (this->header_ent_cnt_ != 0)
1243 this->make_header();
1244 Output_data_got<size, big_endian>::set_final_data_size();
1245 }
1246
1247 // First word of GOT header needs some values that are not
1248 // handled by Output_data_got so poke them in here.
1249 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1250 void
1251 do_write(Output_file* of)
1252 {
6ce78956
AM
1253 this->replace_constant(this->header_index_,
1254 (size == 32
1255 ? this->layout_->dynamic_section()->address()
dd93cd0a 1256 : this->output_section()->address() + 0x8000));
cf43a2fe
AM
1257
1258 Output_data_got<size, big_endian>::do_write(of);
1259 }
1260
1261private:
1262 void
1263 reserve_ent(unsigned int cnt = 1)
1264 {
1265 if (this->header_ent_cnt_ == 0)
1266 return;
1267 if (this->num_entries() + cnt > this->header_index_)
1268 this->make_header();
1269 }
1270
1271 void
1272 make_header()
1273 {
1274 this->header_ent_cnt_ = 0;
1275 this->header_index_ = this->num_entries();
1276 if (size == 32)
1277 {
1278 Output_data_got<size, big_endian>::add_constant(0);
1279 Output_data_got<size, big_endian>::add_constant(0);
1280 Output_data_got<size, big_endian>::add_constant(0);
1281
1282 // Define _GLOBAL_OFFSET_TABLE_ at the header
1283 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1284 Symbol_table::PREDEFINED,
1285 this, this->g_o_t(), 0,
1286 elfcpp::STT_OBJECT,
1287 elfcpp::STB_LOCAL,
1288 elfcpp::STV_HIDDEN,
1289 0, false, false);
1290 }
1291 else
1292 Output_data_got<size, big_endian>::add_constant(0);
1293 }
1294
1295 // Stashed pointers.
1296 Symbol_table* symtab_;
1297 Layout* layout_;
1298
1299 // GOT header size.
1300 unsigned int header_ent_cnt_;
1301 // GOT header index.
1302 unsigned int header_index_;
42cacb20
DE
1303};
1304
1305// Get the GOT section, creating it if necessary.
1306
1307template<int size, bool big_endian>
cf43a2fe 1308Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
1309Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1310 Layout* layout)
1311{
1312 if (this->got_ == NULL)
1313 {
1314 gold_assert(symtab != NULL && layout != NULL);
1315
cf43a2fe
AM
1316 this->got_
1317 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
1318
1319 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1320 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 1321 this->got_, ORDER_DATA, false);
42cacb20
DE
1322 }
1323
1324 return this->got_;
1325}
1326
1327// Get the dynamic reloc section, creating it if necessary.
1328
1329template<int size, bool big_endian>
1330typename Target_powerpc<size, big_endian>::Reloc_section*
1331Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1332{
1333 if (this->rela_dyn_ == NULL)
1334 {
1335 gold_assert(layout != NULL);
1336 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1337 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
1338 elfcpp::SHF_ALLOC, this->rela_dyn_,
1339 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
1340 }
1341 return this->rela_dyn_;
1342}
1343
1344// A class to handle the PLT data.
1345
1346template<int size, bool big_endian>
cf43a2fe 1347class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
1348{
1349 public:
1350 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1351 size, big_endian> Reloc_section;
1352
cf43a2fe 1353 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
42cacb20
DE
1354
1355 // Add an entry to the PLT.
cf43a2fe
AM
1356 void
1357 add_entry(Symbol*);
42cacb20
DE
1358
1359 // Return the .rela.plt section data.
cf43a2fe
AM
1360 const Reloc_section*
1361 rel_plt() const
1362 {
42cacb20
DE
1363 return this->rel_;
1364 }
1365
0e70b911
CC
1366 // Return the number of PLT entries.
1367 unsigned int
1368 entry_count() const
d83ce4e3
AM
1369 {
1370 return ((this->current_data_size() - initial_plt_entry_size)
1371 / plt_entry_size);
1372 }
0e70b911
CC
1373
1374 // Return the offset of the first non-reserved PLT entry.
1375 static unsigned int
1376 first_plt_entry_offset()
cf43a2fe 1377 { return initial_plt_entry_size; }
0e70b911
CC
1378
1379 // Return the size of a PLT entry.
1380 static unsigned int
1381 get_plt_entry_size()
cf43a2fe 1382 { return plt_entry_size; }
0e70b911 1383
42cacb20 1384 protected:
42cacb20 1385 void
cf43a2fe 1386 do_adjust_output_section(Output_section* os)
42cacb20 1387 {
cf43a2fe 1388 os->set_entsize(0);
42cacb20
DE
1389 }
1390
6ce78956
AM
1391 // Write to a map file.
1392 void
1393 do_print_to_mapfile(Mapfile* mapfile) const
1394 { mapfile->print_output_data(this, _("** PLT")); }
1395
cf43a2fe
AM
1396 private:
1397 // The size of an entry in the PLT.
1398 static const int plt_entry_size = size == 32 ? 4 : 24;
1399 // The size of the first reserved entry.
1400 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1401
42cacb20
DE
1402 // Write out the PLT data.
1403 void
1404 do_write(Output_file*);
1405
1406 // The reloc section.
1407 Reloc_section* rel_;
cf43a2fe
AM
1408 // Allows access to .glink for do_write.
1409 Target_powerpc<size, big_endian>* targ_;
42cacb20
DE
1410};
1411
cf43a2fe 1412// Create the PLT section.
42cacb20
DE
1413
1414template<int size, bool big_endian>
d83ce4e3
AM
1415Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1416 Layout* layout,
1417 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
1418 : Output_section_data_build(size == 32 ? 4 : 8),
1419 targ_(targ)
42cacb20
DE
1420{
1421 this->rel_ = new Reloc_section(false);
1422 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
22f0da72
ILT
1423 elfcpp::SHF_ALLOC, this->rel_,
1424 ORDER_DYNAMIC_PLT_RELOCS, false);
42cacb20
DE
1425}
1426
42cacb20
DE
1427// Add an entry to the PLT.
1428
1429template<int size, bool big_endian>
1430void
1431Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1432{
cf43a2fe
AM
1433 if (!gsym->has_plt_offset())
1434 {
1435 off_t off = this->current_data_size();
1436
1437 if (off == 0)
1438 off += initial_plt_entry_size;
1439 gsym->set_plt_offset(off);
1440 gsym->set_needs_dynsym_entry();
1441 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1442 off += plt_entry_size;
1443 this->set_current_data_size(off);
1444 }
42cacb20
DE
1445}
1446
dd93cd0a
AM
1447static const uint32_t add_0_11_11 = 0x7c0b5a14;
1448static const uint32_t add_3_3_2 = 0x7c631214;
1449static const uint32_t add_3_3_13 = 0x7c636a14;
1450static const uint32_t add_11_0_11 = 0x7d605a14;
1451static const uint32_t add_12_2_11 = 0x7d825a14;
1452static const uint32_t addi_11_11 = 0x396b0000;
1453static const uint32_t addi_12_12 = 0x398c0000;
1454static const uint32_t addi_2_2 = 0x38420000;
1455static const uint32_t addi_3_2 = 0x38620000;
1456static const uint32_t addi_3_3 = 0x38630000;
1457static const uint32_t addis_0_2 = 0x3c020000;
1458static const uint32_t addis_0_13 = 0x3c0d0000;
c9269dff
AM
1459static const uint32_t addis_11_11 = 0x3d6b0000;
1460static const uint32_t addis_11_30 = 0x3d7e0000;
1461static const uint32_t addis_12_12 = 0x3d8c0000;
dd93cd0a
AM
1462static const uint32_t addis_12_2 = 0x3d820000;
1463static const uint32_t addis_3_2 = 0x3c620000;
1464static const uint32_t addis_3_13 = 0x3c6d0000;
c9269dff
AM
1465static const uint32_t b = 0x48000000;
1466static const uint32_t bcl_20_31 = 0x429f0005;
1467static const uint32_t bctr = 0x4e800420;
1468static const uint32_t blrl = 0x4e800021;
dd93cd0a
AM
1469static const uint32_t cror_15_15_15 = 0x4def7b82;
1470static const uint32_t cror_31_31_31 = 0x4ffffb82;
1471static const uint32_t ld_11_12 = 0xe96c0000;
1472static const uint32_t ld_11_2 = 0xe9620000;
1473static const uint32_t ld_2_1 = 0xe8410000;
1474static const uint32_t ld_2_11 = 0xe84b0000;
1475static const uint32_t ld_2_12 = 0xe84c0000;
1476static const uint32_t ld_2_2 = 0xe8420000;
1477static const uint32_t li_0_0 = 0x38000000;
1478static const uint32_t lis_0_0 = 0x3c000000;
c9269dff
AM
1479static const uint32_t lis_11 = 0x3d600000;
1480static const uint32_t lis_12 = 0x3d800000;
c9269dff
AM
1481static const uint32_t lwz_0_12 = 0x800c0000;
1482static const uint32_t lwz_11_11 = 0x816b0000;
1483static const uint32_t lwz_11_30 = 0x817e0000;
1484static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 1485static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 1486static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 1487static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff
AM
1488static const uint32_t mflr_12 = 0x7d8802a6;
1489static const uint32_t mtctr_0 = 0x7c0903a6;
1490static const uint32_t mtctr_11 = 0x7d6903a6;
1491static const uint32_t mtlr_0 = 0x7c0803a6;
c9269dff 1492static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 1493static const uint32_t nop = 0x60000000;
c9269dff 1494static const uint32_t ori_0_0_0 = 0x60000000;
dd93cd0a
AM
1495static const uint32_t std_2_1 = 0xf8410000;
1496static const uint32_t sub_11_11_12 = 0x7d6c5850;
42cacb20
DE
1497
1498// Write out the PLT.
1499
1500template<int size, bool big_endian>
1501void
1502Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1503{
cf43a2fe
AM
1504 if (size == 32)
1505 {
1506 const off_t offset = this->offset();
1507 const section_size_type oview_size
1508 = convert_to_section_size_type(this->data_size());
1509 unsigned char* const oview = of->get_output_view(offset, oview_size);
1510 unsigned char* pov = oview;
1511 unsigned char* endpov = oview + oview_size;
1512
1513 // The address the .glink branch table
1514 const Output_data_glink<size, big_endian>* glink
1515 = this->targ_->glink_section();
1516 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1517 = glink->address() + glink->pltresolve();
1518
1519 while (pov < endpov)
1520 {
1521 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1522 pov += 4;
1523 branch_tab += 4;
1524 }
1525
1526 of->write_output_view(offset, oview_size, oview);
1527 }
1528}
1529
1530// Create the PLT section.
1531
1532template<int size, bool big_endian>
1533void
1534Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1535{
1536 if (this->plt_ == NULL)
1537 {
1538 if (this->glink_ == NULL)
1539 make_glink_section(layout);
1540
1541 // Ensure that .rela.dyn always appears before .rela.plt This is
1542 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1543 // needs to include .rela.plt in it's range.
1544 this->rela_dyn_section(layout);
1545
1546 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1547 layout->add_output_section_data(".plt",
1548 (size == 32
1549 ? elfcpp::SHT_PROGBITS
1550 : elfcpp::SHT_NOBITS),
1551 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1552 this->plt_,
1553 (size == 32
1554 ? ORDER_SMALL_DATA
1555 : ORDER_SMALL_BSS),
1556 false);
1557 }
1558}
1559
1560// A class to handle .glink.
1561
1562template<int size, bool big_endian>
1563class Output_data_glink : public Output_section_data
1564{
1565 public:
c9269dff
AM
1566 static const int pltresolve_size = 16*4;
1567
cf43a2fe
AM
1568 Output_data_glink(Target_powerpc<size, big_endian>*);
1569
1570 // Add an entry
1571 void
1572 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1573 const Sized_relobj<size, big_endian>*);
cf43a2fe
AM
1574
1575 unsigned int
1576 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
d1a8cabd 1577 const Sized_relobj<size, big_endian>*) const;
cf43a2fe
AM
1578
1579 unsigned int
1580 glink_entry_size() const
1581 {
1582 if (size == 32)
1583 return 4 * 4;
1584 else
1585 // FIXME: We should be using multiple glink sections for
1586 // stubs to support > 33M applications.
1587 return 8 * 4;
1588 }
1589
1590 off_t
1591 pltresolve() const
1592 {
1593 return this->pltresolve_;
1594 }
1595
6ce78956
AM
1596 protected:
1597 // Write to a map file.
1598 void
1599 do_print_to_mapfile(Mapfile* mapfile) const
1600 { mapfile->print_output_data(this, _("** glink")); }
1601
cf43a2fe 1602 private:
cf43a2fe
AM
1603 void
1604 set_final_data_size();
1605
1606 // Write out .glink
1607 void
1608 do_write(Output_file*);
1609
d1a8cabd 1610 class Glink_sym_ent
cf43a2fe 1611 {
d1a8cabd
AM
1612 public:
1613 Glink_sym_ent(const Symbol* sym,
cf43a2fe 1614 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1615 const Sized_relobj<size, big_endian>* object)
c9269dff 1616 : sym_(sym), addend_(0), object_(0)
cf43a2fe
AM
1617 {
1618 if (size != 32)
1619 this->addend_ = reloc.get_r_addend();
d1a8cabd
AM
1620 else if (parameters->options().output_is_position_independent()
1621 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1622 == elfcpp::R_PPC_PLTREL24))
cf43a2fe 1623 {
d1a8cabd 1624 this->addend_ = reloc.get_r_addend();
cf43a2fe 1625 if (this->addend_ != 0)
d1a8cabd 1626 this->object_ = object;
cf43a2fe
AM
1627 }
1628 }
1629
cf43a2fe
AM
1630 bool operator==(const Glink_sym_ent& that) const
1631 {
1632 return (this->sym_ == that.sym_
1633 && this->object_ == that.object_
d1a8cabd 1634 && this->addend_ == that.addend_);
cf43a2fe 1635 }
c9269dff
AM
1636
1637 const Symbol* sym_;
1638 unsigned int addend_;
1639 const Sized_relobj<size, big_endian>* object_;
cf43a2fe
AM
1640 };
1641
d1a8cabd 1642 class Glink_sym_ent_hash
cf43a2fe 1643 {
d1a8cabd 1644 public:
cf43a2fe
AM
1645 size_t operator()(const Glink_sym_ent& ent) const
1646 {
1647 return (reinterpret_cast<uintptr_t>(ent.sym_)
1648 ^ reinterpret_cast<uintptr_t>(ent.object_)
cf43a2fe
AM
1649 ^ ent.addend_);
1650 }
1651 };
1652
d1a8cabd 1653 // Map sym/object/addend to index.
cf43a2fe
AM
1654 typedef Unordered_map<Glink_sym_ent, unsigned int,
1655 Glink_sym_ent_hash> Glink_entries;
1656 Glink_entries glink_entries_;
1657
1658 // Offset of pltresolve stub (actually, branch table for 32-bit)
1659 off_t pltresolve_;
1660
1661 // Allows access to .got and .plt for do_write.
1662 Target_powerpc<size, big_endian>* targ_;
1663};
1664
1665// Create the glink section.
1666
1667template<int size, bool big_endian>
d83ce4e3
AM
1668Output_data_glink<size, big_endian>::Output_data_glink(
1669 Target_powerpc<size, big_endian>* targ)
cf43a2fe
AM
1670 : Output_section_data(16),
1671 pltresolve_(0), targ_(targ)
1672{
1673}
1674
1675// Add an entry to glink, if we do not already have one for this
d1a8cabd 1676// sym/object/addend combo.
cf43a2fe
AM
1677
1678template<int size, bool big_endian>
1679void
d83ce4e3
AM
1680Output_data_glink<size, big_endian>::add_entry(
1681 const Symbol* gsym,
1682 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1683 const Sized_relobj<size, big_endian>* object)
cf43a2fe 1684{
d1a8cabd 1685 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe 1686 unsigned int indx = this->glink_entries_.size();
d83ce4e3 1687 this->glink_entries_.insert(std::make_pair(ent, indx));
cf43a2fe
AM
1688}
1689
1690template<int size, bool big_endian>
1691unsigned int
d83ce4e3
AM
1692Output_data_glink<size, big_endian>::find_entry(
1693 const Symbol* gsym,
1694 const elfcpp::Rela<size, big_endian>& reloc,
d1a8cabd 1695 const Sized_relobj<size, big_endian>* object) const
cf43a2fe 1696{
d1a8cabd 1697 Glink_sym_ent ent(gsym, reloc, object);
cf43a2fe
AM
1698 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1699 gold_assert(p != this->glink_entries_.end());
1700 return p->second;
1701}
1702
1703template<int size, bool big_endian>
1704void
1705Output_data_glink<size, big_endian>::set_final_data_size()
1706{
1707 unsigned int count = this->glink_entries_.size();
1708 off_t total = count;
1709
1710 if (count != 0)
1711 {
1712 if (size == 32)
1713 {
1714 total *= 16;
1715 this->pltresolve_ = total;
1716
1717 // space for branch table
1718 total += 4 * (count - 1);
1719
1720 total += -total & 15;
1721 total += this->pltresolve_size;
1722 }
1723 else
1724 {
1725 total *= 32;
1726 this->pltresolve_ = total;
1727 total += this->pltresolve_size;
1728
1729 // space for branch table
1730 total += 8 * count;
1731 if (count > 0x8000)
1732 total += 4 * (count - 0x8000);
1733 }
1734 }
1735
1736 this->set_data_size(total);
1737}
1738
1739static inline uint32_t
1740l(uint32_t a)
1741{
1742 return a & 0xffff;
1743}
1744
1745static inline uint32_t
1746hi(uint32_t a)
1747{
1748 return l(a >> 16);
1749}
1750
1751static inline uint32_t
1752ha(uint32_t a)
1753{
1754 return hi(a + 0x8000);
1755}
1756
1757template<bool big_endian>
1758static inline void
c9269dff 1759write_insn(unsigned char* p, uint32_t v)
cf43a2fe
AM
1760{
1761 elfcpp::Swap<32, big_endian>::writeval(p, v);
1762}
1763
1764// Write out .glink.
1765
1766template<int size, bool big_endian>
1767void
1768Output_data_glink<size, big_endian>::do_write(Output_file* of)
1769{
1770 const off_t off = this->offset();
42cacb20
DE
1771 const section_size_type oview_size =
1772 convert_to_section_size_type(this->data_size());
cf43a2fe 1773 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 1774 unsigned char* p;
42cacb20 1775
cf43a2fe 1776 // The base address of the .plt section.
dd93cd0a
AM
1777 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1778 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 1779
dd93cd0a
AM
1780 const Output_data_got_powerpc<size, big_endian>* got
1781 = this->targ_->got_section();
cf43a2fe
AM
1782
1783 if (size == 64)
1784 {
dd93cd0a 1785 Address got_os_addr = got->output_section()->address();
c9269dff 1786
cf43a2fe
AM
1787 // Write out call stubs.
1788 typename Glink_entries::const_iterator g;
1789 for (g = this->glink_entries_.begin();
1790 g != this->glink_entries_.end();
1791 ++g)
1792 {
dd93cd0a
AM
1793 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1794 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1795 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1796 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1797 Address pltoff = plt_addr - got_addr;
cf43a2fe
AM
1798
1799 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1800 gold_error(_("%s: linkage table error against `%s'"),
1801 g->first.object_->name().c_str(),
1802 g->first.sym_->demangled_name().c_str());
1803
1804 p = oview + g->second * this->glink_entry_size();
1805 if (ha(pltoff) != 0)
1806 {
1807 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1808 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1809 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1810 if (ha(pltoff + 16) != ha(pltoff))
1811 {
1812 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1813 pltoff = 0;
1814 }
1815 write_insn<big_endian>(p, mtctr_11), p += 4;
1816 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1817 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1818 write_insn<big_endian>(p, bctr), p += 4;
1819 }
1820 else
1821 {
1822 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1823 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1824 if (ha(pltoff + 16) != ha(pltoff))
1825 {
1826 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1827 pltoff = 0;
1828 }
1829 write_insn<big_endian>(p, mtctr_11), p += 4;
1830 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1831 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1832 write_insn<big_endian>(p, bctr), p += 4;
1833 }
1834 }
1835
1836 // Write pltresolve stub.
1837 p = oview + this->pltresolve_;
dd93cd0a
AM
1838 Address after_bcl = this->address() + this->pltresolve_ + 16;
1839 Address pltoff = plt_base - after_bcl;
cf43a2fe
AM
1840
1841 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1842
1843 write_insn<big_endian>(p, mflr_12), p += 4;
1844 write_insn<big_endian>(p, bcl_20_31), p += 4;
1845 write_insn<big_endian>(p, mflr_11), p += 4;
1846 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1847 write_insn<big_endian>(p, mtlr_12), p += 4;
1848 write_insn<big_endian>(p, add_12_2_11), p += 4;
1849 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1850 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1851 write_insn<big_endian>(p, mtctr_11), p += 4;
1852 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1853 write_insn<big_endian>(p, bctr), p += 4;
1854 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1855 write_insn<big_endian>(p, nop), p += 4;
1856
1857 // Write lazy link call stubs.
1858 uint32_t indx = 0;
1859 while (p < oview + oview_size)
1860 {
1861 if (indx < 0x8000)
1862 {
1863 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1864 }
1865 else
1866 {
1867 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1868 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1869 }
c9269dff 1870 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
cf43a2fe
AM
1871 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1872 indx++;
1873 }
1874 }
1875 else
1876 {
dd93cd0a
AM
1877 // The address of _GLOBAL_OFFSET_TABLE_.
1878 Address g_o_t = got->address() + got->g_o_t();
c9269dff 1879
cf43a2fe
AM
1880 // Write out call stubs.
1881 typename Glink_entries::const_iterator g;
1882 for (g = this->glink_entries_.begin();
1883 g != this->glink_entries_.end();
1884 ++g)
1885 {
dd93cd0a
AM
1886 Address plt_addr = plt_base + g->first.sym_->plt_offset();
1887 Address got_addr;
1888 const Address invalid_address = static_cast<Address>(-1);
cf43a2fe
AM
1889
1890 p = oview + g->second * this->glink_entry_size();
1891 if (parameters->options().output_is_position_independent())
1892 {
d1a8cabd
AM
1893 const Powerpc_relobj<size, big_endian>* object = static_cast
1894 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1895 if (object != NULL)
1896 {
1897 unsigned int got2 = object->got2_shndx();
c9269dff
AM
1898 got_addr = g->first.object_->get_output_section_offset(got2);
1899 gold_assert(got_addr != invalid_address);
1900 got_addr += (g->first.object_->output_section(got2)->address()
1901 + g->first.addend_);
d1a8cabd 1902 }
cf43a2fe
AM
1903 else
1904 got_addr = g_o_t;
1905
dd93cd0a 1906 Address pltoff = plt_addr - got_addr;
cf43a2fe
AM
1907 if (ha(pltoff) == 0)
1908 {
1909 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1910 write_insn<big_endian>(p + 4, mtctr_11);
1911 write_insn<big_endian>(p + 8, bctr);
1912 }
1913 else
1914 {
1915 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1916 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1917 write_insn<big_endian>(p + 8, mtctr_11);
1918 write_insn<big_endian>(p + 12, bctr);
1919 }
1920 }
1921 else
1922 {
1923 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1924 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1925 write_insn<big_endian>(p + 8, mtctr_11);
1926 write_insn<big_endian>(p + 12, bctr);
1927 }
1928 }
42cacb20 1929
cf43a2fe
AM
1930 // Write out pltresolve branch table.
1931 p = oview + this->pltresolve_;
1932 unsigned int the_end = oview_size - this->pltresolve_size;
c9269dff 1933 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
1934 while (p < end_p - 8 * 4)
1935 write_insn<big_endian>(p, b + end_p - p), p += 4;
1936 while (p < end_p)
1937 write_insn<big_endian>(p, nop), p += 4;
42cacb20 1938
cf43a2fe
AM
1939 // Write out pltresolve call stub.
1940 if (parameters->options().output_is_position_independent())
42cacb20 1941 {
dd93cd0a
AM
1942 Address res0_off = this->pltresolve_;
1943 Address after_bcl_off = the_end + 12;
1944 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe
AM
1945
1946 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1947 write_insn<big_endian>(p + 4, mflr_0);
1948 write_insn<big_endian>(p + 8, bcl_20_31);
1949 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1950 write_insn<big_endian>(p + 16, mflr_12);
1951 write_insn<big_endian>(p + 20, mtlr_0);
1952 write_insn<big_endian>(p + 24, sub_11_11_12);
1953
dd93cd0a 1954 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe
AM
1955
1956 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1957 if (ha(got_bcl) == ha(got_bcl + 4))
1958 {
1959 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1960 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1961 }
1962 else
1963 {
1964 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1965 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1966 }
1967 write_insn<big_endian>(p + 40, mtctr_0);
1968 write_insn<big_endian>(p + 44, add_0_11_11);
1969 write_insn<big_endian>(p + 48, add_11_0_11);
1970 write_insn<big_endian>(p + 52, bctr);
1971 write_insn<big_endian>(p + 56, nop);
1972 write_insn<big_endian>(p + 60, nop);
42cacb20 1973 }
cf43a2fe 1974 else
42cacb20 1975 {
dd93cd0a 1976 Address res0 = this->pltresolve_ + this->address();
cf43a2fe
AM
1977
1978 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1979 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1980 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1981 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1982 else
1983 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1984 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1985 write_insn<big_endian>(p + 16, mtctr_0);
1986 write_insn<big_endian>(p + 20, add_0_11_11);
1987 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1988 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1989 else
1990 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1991 write_insn<big_endian>(p + 28, add_11_0_11);
1992 write_insn<big_endian>(p + 32, bctr);
1993 write_insn<big_endian>(p + 36, nop);
1994 write_insn<big_endian>(p + 40, nop);
1995 write_insn<big_endian>(p + 44, nop);
1996 write_insn<big_endian>(p + 48, nop);
1997 write_insn<big_endian>(p + 52, nop);
1998 write_insn<big_endian>(p + 56, nop);
1999 write_insn<big_endian>(p + 60, nop);
42cacb20 2000 }
cf43a2fe 2001 p += 64;
42cacb20
DE
2002 }
2003
cf43a2fe
AM
2004 of->write_output_view(off, oview_size, oview);
2005}
2006
2007// Create the glink section.
42cacb20 2008
cf43a2fe
AM
2009template<int size, bool big_endian>
2010void
2011Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2012{
2013 if (this->glink_ == NULL)
2014 {
2015 this->glink_ = new Output_data_glink<size, big_endian>(this);
2016 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2017 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2018 this->glink_, ORDER_TEXT, false);
2019 }
42cacb20
DE
2020}
2021
2022// Create a PLT entry for a global symbol.
2023
2024template<int size, bool big_endian>
2025void
d83ce4e3
AM
2026Target_powerpc<size, big_endian>::make_plt_entry(
2027 Layout* layout,
2028 Symbol* gsym,
2029 const elfcpp::Rela<size, big_endian>& reloc,
2030 const Sized_relobj<size, big_endian>* object)
42cacb20 2031{
42cacb20 2032 if (this->plt_ == NULL)
cf43a2fe 2033 this->make_plt_section(layout);
42cacb20 2034
cf43a2fe 2035 this->plt_->add_entry(gsym);
612a8d3d 2036
d1a8cabd 2037 this->glink_->add_entry(gsym, reloc, object);
42cacb20
DE
2038}
2039
0e70b911
CC
2040// Return the number of entries in the PLT.
2041
2042template<int size, bool big_endian>
2043unsigned int
2044Target_powerpc<size, big_endian>::plt_entry_count() const
2045{
2046 if (this->plt_ == NULL)
2047 return 0;
2048 return this->plt_->entry_count();
2049}
2050
2051// Return the offset of the first non-reserved PLT entry.
2052
2053template<int size, bool big_endian>
2054unsigned int
2055Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2056{
2057 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2058}
2059
2060// Return the size of each PLT entry.
2061
2062template<int size, bool big_endian>
2063unsigned int
2064Target_powerpc<size, big_endian>::plt_entry_size() const
2065{
2066 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2067}
2068
dd93cd0a 2069// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
2070
2071template<int size, bool big_endian>
2072unsigned int
dd93cd0a 2073Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
2074 Symbol_table* symtab,
2075 Layout* layout,
2076 Sized_relobj_file<size, big_endian>* object)
42cacb20 2077{
dd93cd0a 2078 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
2079 {
2080 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2081 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
2082 Output_data_got_powerpc<size, big_endian>* got
2083 = this->got_section(symtab, layout);
2084 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
2085 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2086 got_offset, 0);
dd93cd0a 2087 this->tlsld_got_offset_ = got_offset;
42cacb20 2088 }
dd93cd0a 2089 return this->tlsld_got_offset_;
42cacb20
DE
2090}
2091
95a2c8d6
RS
2092// Get the Reference_flags for a particular relocation.
2093
2094template<int size, bool big_endian>
2095int
d83ce4e3 2096Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
95a2c8d6
RS
2097{
2098 switch (r_type)
2099 {
2100 case elfcpp::R_POWERPC_NONE:
2101 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2102 case elfcpp::R_POWERPC_GNU_VTENTRY:
2103 case elfcpp::R_PPC64_TOC:
2104 // No symbol reference.
2105 return 0;
2106
dd93cd0a
AM
2107 case elfcpp::R_PPC64_ADDR64:
2108 case elfcpp::R_PPC64_UADDR64:
2109 case elfcpp::R_POWERPC_ADDR32:
2110 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 2111 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 2112 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
2113 case elfcpp::R_POWERPC_ADDR16_LO:
2114 case elfcpp::R_POWERPC_ADDR16_HI:
2115 case elfcpp::R_POWERPC_ADDR16_HA:
95a2c8d6
RS
2116 return Symbol::ABSOLUTE_REF;
2117
dd93cd0a
AM
2118 case elfcpp::R_POWERPC_ADDR24:
2119 case elfcpp::R_POWERPC_ADDR14:
2120 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2121 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2122 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2123
2124 case elfcpp::R_POWERPC_REL32:
95a2c8d6 2125 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
2126 case elfcpp::R_POWERPC_REL16:
2127 case elfcpp::R_POWERPC_REL16_LO:
2128 case elfcpp::R_POWERPC_REL16_HI:
2129 case elfcpp::R_POWERPC_REL16_HA:
95a2c8d6
RS
2130 return Symbol::RELATIVE_REF;
2131
dd93cd0a 2132 case elfcpp::R_POWERPC_REL24:
95a2c8d6 2133 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
2134 case elfcpp::R_POWERPC_REL14:
2135 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2136 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
95a2c8d6
RS
2137 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2138
2139 case elfcpp::R_POWERPC_GOT16:
2140 case elfcpp::R_POWERPC_GOT16_LO:
2141 case elfcpp::R_POWERPC_GOT16_HI:
2142 case elfcpp::R_POWERPC_GOT16_HA:
2143 case elfcpp::R_PPC64_TOC16:
2144 case elfcpp::R_PPC64_TOC16_LO:
2145 case elfcpp::R_PPC64_TOC16_HI:
2146 case elfcpp::R_PPC64_TOC16_HA:
2147 case elfcpp::R_PPC64_TOC16_DS:
2148 case elfcpp::R_PPC64_TOC16_LO_DS:
2149 // Absolute in GOT.
2150 return Symbol::ABSOLUTE_REF;
2151
2152 case elfcpp::R_POWERPC_GOT_TPREL16:
2153 case elfcpp::R_POWERPC_TLS:
2154 return Symbol::TLS_REF;
2155
2156 case elfcpp::R_POWERPC_COPY:
2157 case elfcpp::R_POWERPC_GLOB_DAT:
2158 case elfcpp::R_POWERPC_JMP_SLOT:
2159 case elfcpp::R_POWERPC_RELATIVE:
2160 case elfcpp::R_POWERPC_DTPMOD:
2161 default:
2162 // Not expected. We will give an error later.
2163 return 0;
2164 }
2165}
2166
42cacb20
DE
2167// Report an unsupported relocation against a local symbol.
2168
2169template<int size, bool big_endian>
2170void
2171Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
2172 Sized_relobj_file<size, big_endian>* object,
2173 unsigned int r_type)
42cacb20
DE
2174{
2175 gold_error(_("%s: unsupported reloc %u against local symbol"),
2176 object->name().c_str(), r_type);
2177}
2178
2179// We are about to emit a dynamic relocation of type R_TYPE. If the
2180// dynamic linker does not support it, issue an error.
2181
2182template<int size, bool big_endian>
2183void
2184Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2185 unsigned int r_type)
2186{
2187 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2188
2189 // These are the relocation types supported by glibc for both 32-bit
2190 // and 64-bit powerpc.
2191 switch (r_type)
2192 {
2193 case elfcpp::R_POWERPC_RELATIVE:
2194 case elfcpp::R_POWERPC_GLOB_DAT:
2195 case elfcpp::R_POWERPC_DTPMOD:
2196 case elfcpp::R_POWERPC_DTPREL:
2197 case elfcpp::R_POWERPC_TPREL:
2198 case elfcpp::R_POWERPC_JMP_SLOT:
2199 case elfcpp::R_POWERPC_COPY:
2200 case elfcpp::R_POWERPC_ADDR32:
2201 case elfcpp::R_POWERPC_ADDR24:
2202 case elfcpp::R_POWERPC_REL24:
2203 return;
2204
2205 default:
2206 break;
2207 }
2208
2209 if (size == 64)
2210 {
2211 switch (r_type)
2212 {
2213 // These are the relocation types supported only on 64-bit.
2214 case elfcpp::R_PPC64_ADDR64:
2215 case elfcpp::R_PPC64_TPREL16_LO_DS:
2216 case elfcpp::R_PPC64_TPREL16_DS:
2217 case elfcpp::R_POWERPC_TPREL16:
2218 case elfcpp::R_POWERPC_TPREL16_LO:
2219 case elfcpp::R_POWERPC_TPREL16_HI:
2220 case elfcpp::R_POWERPC_TPREL16_HA:
2221 case elfcpp::R_PPC64_TPREL16_HIGHER:
2222 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2223 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2224 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2225 case elfcpp::R_PPC64_ADDR16_LO_DS:
2226 case elfcpp::R_POWERPC_ADDR16_LO:
2227 case elfcpp::R_POWERPC_ADDR16_HI:
2228 case elfcpp::R_POWERPC_ADDR16_HA:
2229 case elfcpp::R_POWERPC_ADDR30:
2230 case elfcpp::R_PPC64_UADDR64:
2231 case elfcpp::R_POWERPC_UADDR32:
2232 case elfcpp::R_POWERPC_ADDR16:
2233 case elfcpp::R_POWERPC_UADDR16:
2234 case elfcpp::R_PPC64_ADDR16_DS:
2235 case elfcpp::R_PPC64_ADDR16_HIGHER:
2236 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2237 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2238 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2239 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2240 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2241 case elfcpp::R_POWERPC_REL32:
2242 case elfcpp::R_PPC64_REL64:
2243 return;
2244
2245 default:
2246 break;
2247 }
2248 }
2249 else
2250 {
2251 switch (r_type)
2252 {
2253 // These are the relocation types supported only on 32-bit.
2254
2255 default:
2256 break;
2257 }
2258 }
2259
2260 // This prevents us from issuing more than one error per reloc
2261 // section. But we can still wind up issuing more than one
2262 // error per object file.
2263 if (this->issued_non_pic_error_)
2264 return;
33aea2fd 2265 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
2266 object->error(_("requires unsupported dynamic reloc; "
2267 "recompile with -fPIC"));
2268 this->issued_non_pic_error_ = true;
2269 return;
2270}
2271
2272// Scan a relocation for a local symbol.
2273
2274template<int size, bool big_endian>
2275inline void
2276Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
2277 Symbol_table* symtab,
2278 Layout* layout,
2279 Target_powerpc<size, big_endian>* target,
2280 Sized_relobj_file<size, big_endian>* object,
2281 unsigned int data_shndx,
2282 Output_section* output_section,
2283 const elfcpp::Rela<size, big_endian>& reloc,
2284 unsigned int r_type,
2285 const elfcpp::Sym<size, big_endian>& lsym)
42cacb20 2286{
dd93cd0a
AM
2287 Powerpc_relobj<size, big_endian>* ppc_object
2288 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2289
42cacb20
DE
2290 switch (r_type)
2291 {
2292 case elfcpp::R_POWERPC_NONE:
2293 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2294 case elfcpp::R_POWERPC_GNU_VTENTRY:
6ce78956 2295 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 2296 case elfcpp::R_PPC_EMB_MRKREF:
7404fe1b 2297 case elfcpp::R_POWERPC_TLS:
dd93cd0a
AM
2298 break;
2299
2300 case elfcpp::R_PPC64_TOC:
2301 {
2302 Output_data_got_powerpc<size, big_endian>* got
2303 = target->got_section(symtab, layout);
2304 if (parameters->options().output_is_position_independent())
2305 {
2306 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2307 rela_dyn->add_output_section_relative(got->output_section(),
2308 elfcpp::R_POWERPC_RELATIVE,
2309 output_section,
2310 object, data_shndx,
2311 reloc.get_r_offset(),
2312 ppc_object->toc_base_offset());
2313 }
2314 }
42cacb20
DE
2315 break;
2316
2317 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 2318 case elfcpp::R_PPC64_UADDR64:
42cacb20 2319 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
2320 case elfcpp::R_POWERPC_UADDR32:
2321 case elfcpp::R_POWERPC_ADDR24:
c9269dff 2322 case elfcpp::R_POWERPC_ADDR16:
42cacb20 2323 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
2324 case elfcpp::R_POWERPC_ADDR16_HI:
2325 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a
AM
2326 case elfcpp::R_POWERPC_UADDR16:
2327 case elfcpp::R_PPC64_ADDR16_HIGHER:
2328 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2329 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2330 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2331 case elfcpp::R_PPC64_ADDR16_DS:
2332 case elfcpp::R_PPC64_ADDR16_LO_DS:
2333 case elfcpp::R_POWERPC_ADDR14:
2334 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2335 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
2336 // If building a shared library (or a position-independent
2337 // executable), we need to create a dynamic relocation for
2338 // this location.
2339 if (parameters->options().output_is_position_independent())
2e702c99
RM
2340 {
2341 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20 2342
dd93cd0a
AM
2343 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2344 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99
RM
2345 {
2346 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
2347 rela_dyn->add_local_relative(object, r_sym,
2348 elfcpp::R_POWERPC_RELATIVE,
2349 output_section, data_shndx,
2350 reloc.get_r_offset(),
2351 reloc.get_r_addend(), false);
2e702c99
RM
2352 }
2353 else
2354 {
dd93cd0a 2355 check_non_pic(object, r_type);
42cacb20 2356 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
2357 rela_dyn->add_local(object, r_sym, r_type, output_section,
2358 data_shndx, reloc.get_r_offset(),
2359 reloc.get_r_addend());
2e702c99
RM
2360 }
2361 }
42cacb20
DE
2362 break;
2363
dd93cd0a 2364 case elfcpp::R_POWERPC_REL32:
42cacb20
DE
2365 case elfcpp::R_POWERPC_REL24:
2366 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a 2367 case elfcpp::R_POWERPC_REL16:
6ce78956 2368 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 2369 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 2370 case elfcpp::R_POWERPC_REL16_HA:
dd93cd0a
AM
2371 case elfcpp::R_POWERPC_REL14:
2372 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2373 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2374 case elfcpp::R_POWERPC_SECTOFF:
2375 case elfcpp::R_POWERPC_TPREL16:
2376 case elfcpp::R_POWERPC_DTPREL16:
2377 case elfcpp::R_POWERPC_SECTOFF_LO:
2378 case elfcpp::R_POWERPC_TPREL16_LO:
2379 case elfcpp::R_POWERPC_DTPREL16_LO:
2380 case elfcpp::R_POWERPC_SECTOFF_HI:
2381 case elfcpp::R_POWERPC_TPREL16_HI:
2382 case elfcpp::R_POWERPC_DTPREL16_HI:
2383 case elfcpp::R_POWERPC_SECTOFF_HA:
2384 case elfcpp::R_POWERPC_TPREL16_HA:
2385 case elfcpp::R_POWERPC_DTPREL16_HA:
2386 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2387 case elfcpp::R_PPC64_TPREL16_HIGHER:
2388 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2389 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2390 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2391 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2392 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2393 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2394 case elfcpp::R_PPC64_TPREL16_DS:
2395 case elfcpp::R_PPC64_TPREL16_LO_DS:
2396 case elfcpp::R_PPC64_DTPREL16_DS:
2397 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2398 case elfcpp::R_PPC64_SECTOFF_DS:
2399 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2400 case elfcpp::R_PPC64_TLSGD:
2401 case elfcpp::R_PPC64_TLSLD:
42cacb20
DE
2402 break;
2403
2404 case elfcpp::R_POWERPC_GOT16:
2405 case elfcpp::R_POWERPC_GOT16_LO:
2406 case elfcpp::R_POWERPC_GOT16_HI:
2407 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
2408 case elfcpp::R_PPC64_GOT16_DS:
2409 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 2410 {
c9269dff 2411 // The symbol requires a GOT entry.
dd93cd0a
AM
2412 Output_data_got_powerpc<size, big_endian>* got
2413 = target->got_section(symtab, layout);
2414 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20
DE
2415
2416 // If we are generating a shared object, we need to add a
2417 // dynamic relocation for this symbol's GOT entry.
2418 if (parameters->options().output_is_position_independent())
2419 {
2420 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2421 {
2422 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2423 unsigned int off;
2424
2425 off = got->add_constant(0);
2426 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2427 rela_dyn->add_local_relative(object, r_sym,
2428 elfcpp::R_POWERPC_RELATIVE,
397b129b 2429 got, off, 0, false);
42cacb20 2430 }
2e702c99 2431 }
42cacb20
DE
2432 else
2433 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2434 }
2435 break;
2436
cf43a2fe
AM
2437 case elfcpp::R_PPC64_TOC16:
2438 case elfcpp::R_PPC64_TOC16_LO:
2439 case elfcpp::R_PPC64_TOC16_HI:
2440 case elfcpp::R_PPC64_TOC16_HA:
2441 case elfcpp::R_PPC64_TOC16_DS:
2442 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
2443 // We need a GOT section.
2444 target->got_section(symtab, layout);
2445 break;
2446
dd93cd0a
AM
2447 case elfcpp::R_POWERPC_GOT_TLSGD16:
2448 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2449 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2450 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2451 {
2452 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2453 if (tls_type == tls::TLSOPT_NONE)
2454 {
2455 Output_data_got_powerpc<size, big_endian>* got
2456 = target->got_section(symtab, layout);
2457 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2458 unsigned int shndx = lsym.get_st_shndx();
2459 bool is_ordinary;
2460 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2461 gold_assert(is_ordinary);
2462 got->add_local_pair_with_rel(object, r_sym,
2463 shndx,
2464 GOT_TYPE_TLSGD,
2465 target->rela_dyn_section(layout),
2466 elfcpp::R_POWERPC_DTPMOD,
2467 elfcpp::R_POWERPC_DTPREL);
2468 }
2469 else if (tls_type == tls::TLSOPT_TO_LE)
2470 {
2471 // no GOT relocs needed for Local Exec.
2472 }
2473 else
2474 gold_unreachable();
2475 }
42cacb20
DE
2476 break;
2477
dd93cd0a
AM
2478 case elfcpp::R_POWERPC_GOT_TLSLD16:
2479 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2480 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2481 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2482 {
2483 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2484 if (tls_type == tls::TLSOPT_NONE)
2485 target->tlsld_got_offset(symtab, layout, object);
2486 else if (tls_type == tls::TLSOPT_TO_LE)
2487 {
2488 // no GOT relocs needed for Local Exec.
7404fe1b
AM
2489 if (parameters->options().emit_relocs())
2490 {
2491 Output_section* os = layout->tls_segment()->first_section();
2492 gold_assert(os != NULL);
2493 os->set_needs_symtab_index();
2494 }
dd93cd0a
AM
2495 }
2496 else
2497 gold_unreachable();
2498 }
42cacb20 2499 break;
42cacb20 2500
dd93cd0a
AM
2501 case elfcpp::R_POWERPC_GOT_DTPREL16:
2502 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2503 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2504 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2505 {
2506 Output_data_got_powerpc<size, big_endian>* got
2507 = target->got_section(symtab, layout);
2508 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2509 got->add_local_with_rel(object, r_sym, GOT_TYPE_DTPREL,
2510 target->rela_dyn_section(layout),
2511 elfcpp::R_POWERPC_DTPREL);
2512 }
2513 break;
42cacb20 2514
dd93cd0a
AM
2515 case elfcpp::R_POWERPC_GOT_TPREL16:
2516 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2517 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2518 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2519 {
2520 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2521 if (tls_type == tls::TLSOPT_NONE)
2522 {
2523 Output_data_got_powerpc<size, big_endian>* got
2524 = target->got_section(symtab, layout);
2525 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2526 got->add_local_with_rel(object, r_sym, GOT_TYPE_TPREL,
2527 target->rela_dyn_section(layout),
2528 elfcpp::R_POWERPC_TPREL);
2529 }
2530 else if (tls_type == tls::TLSOPT_TO_LE)
2531 {
2532 // no GOT relocs needed for Local Exec.
2533 }
2534 else
2535 gold_unreachable();
2536 }
2537 break;
2538
2539 default:
2540 unsupported_reloc_local(object, r_type);
2541 break;
2542 }
2543}
2544
2545// Report an unsupported relocation against a global symbol.
2546
2547template<int size, bool big_endian>
2548void
2549Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2550 Sized_relobj_file<size, big_endian>* object,
2551 unsigned int r_type,
2552 Symbol* gsym)
2553{
42cacb20
DE
2554 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2555 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2556}
2557
2558// Scan a relocation for a global symbol.
2559
2560template<int size, bool big_endian>
2561inline void
2562Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
2563 Symbol_table* symtab,
2564 Layout* layout,
2565 Target_powerpc<size, big_endian>* target,
2566 Sized_relobj_file<size, big_endian>* object,
2567 unsigned int data_shndx,
2568 Output_section* output_section,
2569 const elfcpp::Rela<size, big_endian>& reloc,
2570 unsigned int r_type,
2571 Symbol* gsym)
42cacb20 2572{
dd93cd0a
AM
2573 Powerpc_relobj<size, big_endian>* ppc_object
2574 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2575
42cacb20
DE
2576 switch (r_type)
2577 {
2578 case elfcpp::R_POWERPC_NONE:
2579 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2580 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 2581 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a 2582 case elfcpp::R_PPC_EMB_MRKREF:
7404fe1b 2583 case elfcpp::R_POWERPC_TLS:
dd93cd0a
AM
2584 break;
2585
2586 case elfcpp::R_PPC64_TOC:
2587 {
2588 Output_data_got_powerpc<size, big_endian>* got
2589 = target->got_section(symtab, layout);
2590 if (parameters->options().output_is_position_independent())
2591 {
2592 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2593 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2594 if (data_shndx != ppc_object->opd_shndx())
2595 symobj = static_cast
2596 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2597 rela_dyn->add_output_section_relative(got->output_section(),
2598 elfcpp::R_POWERPC_RELATIVE,
2599 output_section,
2600 object, data_shndx,
2601 reloc.get_r_offset(),
2602 symobj->toc_base_offset());
2603 }
2604 }
42cacb20
DE
2605 break;
2606
c9269dff 2607 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 2608 case elfcpp::R_PPC64_UADDR64:
c9269dff 2609 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
2610 case elfcpp::R_POWERPC_UADDR32:
2611 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
2612 case elfcpp::R_POWERPC_ADDR16:
2613 case elfcpp::R_POWERPC_ADDR16_LO:
2614 case elfcpp::R_POWERPC_ADDR16_HI:
2615 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a
AM
2616 case elfcpp::R_POWERPC_UADDR16:
2617 case elfcpp::R_PPC64_ADDR16_HIGHER:
2618 case elfcpp::R_PPC64_ADDR16_HIGHERA:
2619 case elfcpp::R_PPC64_ADDR16_HIGHEST:
2620 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2621 case elfcpp::R_PPC64_ADDR16_DS:
2622 case elfcpp::R_PPC64_ADDR16_LO_DS:
2623 case elfcpp::R_POWERPC_ADDR14:
2624 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2625 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 2626 {
c9269dff
AM
2627 // Make a PLT entry if necessary.
2628 if (gsym->needs_plt_entry())
2629 {
cf43a2fe 2630 target->make_plt_entry(layout, gsym, reloc, 0);
2e702c99
RM
2631 // Since this is not a PC-relative relocation, we may be
2632 // taking the address of a function. In that case we need to
2633 // set the entry in the dynamic symbol table to the address of
2634 // the PLT entry.
cf43a2fe
AM
2635 if (size == 32
2636 && gsym->is_from_dynobj() && !parameters->options().shared())
2e702c99 2637 gsym->set_needs_dynsym_value();
c9269dff
AM
2638 }
2639 // Make a dynamic relocation if necessary.
dd93cd0a 2640 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
c9269dff
AM
2641 {
2642 if (gsym->may_need_copy_reloc())
2643 {
2644 target->copy_reloc(symtab, layout, object,
2645 data_shndx, output_section, gsym, reloc);
2646 }
dd93cd0a
AM
2647 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2648 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2649 && (gsym->can_use_relative_reloc(false)
2650 || data_shndx == ppc_object->opd_shndx()))
2e702c99
RM
2651 {
2652 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2653 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
42cacb20
DE
2654 output_section, object,
2655 data_shndx, reloc.get_r_offset(),
13cf9988 2656 reloc.get_r_addend(), false);
2e702c99
RM
2657 }
2658 else
2659 {
2660 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
42cacb20 2661 check_non_pic(object, r_type);
dd93cd0a
AM
2662 rela_dyn->add_global(gsym, r_type, output_section,
2663 object, data_shndx,
2664 reloc.get_r_offset(),
2665 reloc.get_r_addend());
2e702c99
RM
2666 }
2667 }
42cacb20
DE
2668 }
2669 break;
2670
cf43a2fe 2671 case elfcpp::R_PPC_PLTREL24:
42cacb20 2672 case elfcpp::R_POWERPC_REL24:
42cacb20 2673 {
cf43a2fe
AM
2674 if (gsym->needs_plt_entry()
2675 || (!gsym->final_value_is_known()
2676 && !(gsym->is_defined()
2677 && !gsym->is_from_dynobj()
2678 && !gsym->is_preemptible())))
d1a8cabd 2679 target->make_plt_entry(layout, gsym, reloc, object);
42cacb20 2680 // Make a dynamic relocation if necessary.
dd93cd0a 2681 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
42cacb20 2682 {
966d4097 2683 if (gsym->may_need_copy_reloc())
42cacb20
DE
2684 {
2685 target->copy_reloc(symtab, layout, object,
2686 data_shndx, output_section, gsym,
2687 reloc);
2688 }
2689 else
2690 {
2691 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2692 check_non_pic(object, r_type);
2693 rela_dyn->add_global(gsym, r_type, output_section, object,
2694 data_shndx, reloc.get_r_offset(),
2695 reloc.get_r_addend());
2696 }
2697 }
2698 }
2699 break;
2700
dd93cd0a 2701 case elfcpp::R_POWERPC_REL32:
6ce78956
AM
2702 case elfcpp::R_POWERPC_REL16:
2703 case elfcpp::R_POWERPC_REL16_LO:
2704 case elfcpp::R_POWERPC_REL16_HI:
2705 case elfcpp::R_POWERPC_REL16_HA:
dd93cd0a
AM
2706 case elfcpp::R_POWERPC_REL14:
2707 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2708 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2709 case elfcpp::R_POWERPC_SECTOFF:
2710 case elfcpp::R_POWERPC_TPREL16:
2711 case elfcpp::R_POWERPC_DTPREL16:
2712 case elfcpp::R_POWERPC_SECTOFF_LO:
2713 case elfcpp::R_POWERPC_TPREL16_LO:
2714 case elfcpp::R_POWERPC_DTPREL16_LO:
2715 case elfcpp::R_POWERPC_SECTOFF_HI:
2716 case elfcpp::R_POWERPC_TPREL16_HI:
2717 case elfcpp::R_POWERPC_DTPREL16_HI:
2718 case elfcpp::R_POWERPC_SECTOFF_HA:
2719 case elfcpp::R_POWERPC_TPREL16_HA:
2720 case elfcpp::R_POWERPC_DTPREL16_HA:
2721 case elfcpp::R_PPC64_DTPREL16_HIGHER:
2722 case elfcpp::R_PPC64_TPREL16_HIGHER:
2723 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2724 case elfcpp::R_PPC64_TPREL16_HIGHERA:
2725 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2726 case elfcpp::R_PPC64_TPREL16_HIGHEST:
2727 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2728 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2729 case elfcpp::R_PPC64_TPREL16_DS:
2730 case elfcpp::R_PPC64_TPREL16_LO_DS:
2731 case elfcpp::R_PPC64_DTPREL16_DS:
2732 case elfcpp::R_PPC64_DTPREL16_LO_DS:
2733 case elfcpp::R_PPC64_SECTOFF_DS:
2734 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2735 case elfcpp::R_PPC64_TLSGD:
2736 case elfcpp::R_PPC64_TLSLD:
cf43a2fe
AM
2737 break;
2738
42cacb20
DE
2739 case elfcpp::R_POWERPC_GOT16:
2740 case elfcpp::R_POWERPC_GOT16_LO:
2741 case elfcpp::R_POWERPC_GOT16_HI:
2742 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
2743 case elfcpp::R_PPC64_GOT16_DS:
2744 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 2745 {
c9269dff
AM
2746 // The symbol requires a GOT entry.
2747 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
2748
2749 got = target->got_section(symtab, layout);
2e702c99
RM
2750 if (gsym->final_value_is_known())
2751 got->add_global(gsym, GOT_TYPE_STANDARD);
2752 else
2753 {
2754 // If this symbol is not fully resolved, we need to add a
2755 // dynamic relocation for it.
2756 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2757 if (gsym->is_from_dynobj()
2758 || gsym->is_undefined()
2759 || gsym->is_preemptible())
2760 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
83896202 2761 elfcpp::R_POWERPC_GLOB_DAT);
2e702c99
RM
2762 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2763 {
42cacb20
DE
2764 unsigned int off = got->add_constant(0);
2765
2766 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2767 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
13cf9988 2768 got, off, 0, false);
42cacb20 2769 }
2e702c99 2770 }
42cacb20
DE
2771 }
2772 break;
2773
cf43a2fe
AM
2774 case elfcpp::R_PPC64_TOC16:
2775 case elfcpp::R_PPC64_TOC16_LO:
2776 case elfcpp::R_PPC64_TOC16_HI:
2777 case elfcpp::R_PPC64_TOC16_HA:
2778 case elfcpp::R_PPC64_TOC16_DS:
2779 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
2780 // We need a GOT section.
2781 target->got_section(symtab, layout);
2782 break;
2783
dd93cd0a
AM
2784 case elfcpp::R_POWERPC_GOT_TLSGD16:
2785 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2786 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2787 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2788 {
2789 const bool final = gsym->final_value_is_known();
2790 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2791 if (tls_type == tls::TLSOPT_NONE)
2792 {
2793 Output_data_got_powerpc<size, big_endian>* got
2794 = target->got_section(symtab, layout);
2795 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2796 target->rela_dyn_section(layout),
2797 elfcpp::R_POWERPC_DTPMOD,
2798 elfcpp::R_POWERPC_DTPREL);
2799 }
2800 else if (tls_type == tls::TLSOPT_TO_IE)
2801 {
2802 Output_data_got_powerpc<size, big_endian>* got
2803 = target->got_section(symtab, layout);
2804 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2805 target->rela_dyn_section(layout),
2806 elfcpp::R_POWERPC_TPREL);
2807 }
2808 else if (tls_type == tls::TLSOPT_TO_LE)
2809 {
2810 // no GOT relocs needed for Local Exec.
2811 }
2812 else
2813 gold_unreachable();
2814 }
42cacb20
DE
2815 break;
2816
dd93cd0a
AM
2817 case elfcpp::R_POWERPC_GOT_TLSLD16:
2818 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2819 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2820 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2821 {
2822 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2823 if (tls_type == tls::TLSOPT_NONE)
2824 target->tlsld_got_offset(symtab, layout, object);
2825 else if (tls_type == tls::TLSOPT_TO_LE)
2826 {
2827 // no GOT relocs needed for Local Exec.
7404fe1b
AM
2828 if (parameters->options().emit_relocs())
2829 {
2830 Output_section* os = layout->tls_segment()->first_section();
2831 gold_assert(os != NULL);
2832 os->set_needs_symtab_index();
2833 }
dd93cd0a
AM
2834 }
2835 else
2836 gold_unreachable();
2837 }
2838 break;
2839
2840 case elfcpp::R_POWERPC_GOT_DTPREL16:
2841 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2842 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2843 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2844 {
2845 Output_data_got_powerpc<size, big_endian>* got
2846 = target->got_section(symtab, layout);
2847 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2848 target->rela_dyn_section(layout),
2849 elfcpp::R_POWERPC_DTPREL);
2850 }
2851 break;
2852
2853 case elfcpp::R_POWERPC_GOT_TPREL16:
2854 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2855 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2856 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2857 {
2858 const bool final = gsym->final_value_is_known();
2859 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2860 if (tls_type == tls::TLSOPT_NONE)
2861 {
2862 Output_data_got_powerpc<size, big_endian>* got
2863 = target->got_section(symtab, layout);
2864 got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2865 target->rela_dyn_section(layout),
2866 elfcpp::R_POWERPC_TPREL);
2867 }
2868 else if (tls_type == tls::TLSOPT_TO_LE)
2869 {
2870 // no GOT relocs needed for Local Exec.
2871 }
2872 else
2873 gold_unreachable();
2874 }
42cacb20
DE
2875 break;
2876
2877 default:
2878 unsupported_reloc_global(object, r_type, gsym);
2879 break;
2880 }
2881}
2882
6d03d481
ST
2883// Process relocations for gc.
2884
2885template<int size, bool big_endian>
2886void
2887Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
2888 Symbol_table* symtab,
2889 Layout* layout,
2890 Sized_relobj_file<size, big_endian>* object,
2891 unsigned int data_shndx,
2892 unsigned int,
2893 const unsigned char* prelocs,
2894 size_t reloc_count,
2895 Output_section* output_section,
2896 bool needs_special_offset_handling,
2897 size_t local_symbol_count,
2898 const unsigned char* plocal_symbols)
6d03d481
ST
2899{
2900 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 2901 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6d03d481 2902
41cbeecc 2903 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3ff2ccb0 2904 typename Target_powerpc::Relocatable_size_for_reloc>(
6d03d481
ST
2905 symtab,
2906 layout,
2907 this,
2908 object,
2909 data_shndx,
2910 prelocs,
2911 reloc_count,
2912 output_section,
2913 needs_special_offset_handling,
2914 local_symbol_count,
2915 plocal_symbols);
2916}
2917
42cacb20
DE
2918// Scan relocations for a section.
2919
2920template<int size, bool big_endian>
2921void
2922Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
2923 Symbol_table* symtab,
2924 Layout* layout,
2925 Sized_relobj_file<size, big_endian>* object,
2926 unsigned int data_shndx,
2927 unsigned int sh_type,
2928 const unsigned char* prelocs,
2929 size_t reloc_count,
2930 Output_section* output_section,
2931 bool needs_special_offset_handling,
2932 size_t local_symbol_count,
2933 const unsigned char* plocal_symbols)
42cacb20
DE
2934{
2935 typedef Target_powerpc<size, big_endian> Powerpc;
2ea97941 2936 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
42cacb20
DE
2937
2938 if (sh_type == elfcpp::SHT_REL)
2939 {
2940 gold_error(_("%s: unsupported REL reloc section"),
2941 object->name().c_str());
2942 return;
2943 }
2944
cf43a2fe
AM
2945 if (size == 32)
2946 {
2947 static Output_data_space* sdata;
2948
2949 // Define _SDA_BASE_ at the start of the .sdata section.
2950 if (sdata == NULL)
2951 {
2952 // layout->find_output_section(".sdata") == NULL
2953 sdata = new Output_data_space(4, "** sdata");
2954 Output_section* os
2955 = layout->add_output_section_data(".sdata", 0,
2956 elfcpp::SHF_ALLOC
2957 | elfcpp::SHF_WRITE,
2958 sdata, ORDER_SMALL_DATA, false);
2959 symtab->define_in_output_data("_SDA_BASE_", NULL,
2960 Symbol_table::PREDEFINED,
2961 os, 32768, 0, elfcpp::STT_OBJECT,
2962 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2963 0, false, false);
2964 }
2965 }
42cacb20 2966
2ea97941 2967 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
42cacb20
DE
2968 symtab,
2969 layout,
2970 this,
2971 object,
2972 data_shndx,
2973 prelocs,
2974 reloc_count,
2975 output_section,
2976 needs_special_offset_handling,
2977 local_symbol_count,
2978 plocal_symbols);
2979}
2980
2981// Finalize the sections.
2982
2983template<int size, bool big_endian>
2984void
d5b40221
DK
2985Target_powerpc<size, big_endian>::do_finalize_sections(
2986 Layout* layout,
f59f41f3
DK
2987 const Input_objects*,
2988 Symbol_table*)
42cacb20
DE
2989{
2990 // Fill in some more dynamic tags.
ea715a34
ILT
2991 const Reloc_section* rel_plt = (this->plt_ == NULL
2992 ? NULL
2993 : this->plt_->rel_plt());
2994 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
612a8d3d 2995 this->rela_dyn_, true, size == 32);
42cacb20 2996
c9269dff 2997 Output_data_dynamic* odyn = layout->dynamic_data();
cf43a2fe
AM
2998 if (size == 32)
2999 {
dd93cd0a
AM
3000 if (this->got_ != NULL)
3001 {
3002 this->got_->finalize_data_size();
3003 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3004 this->got_, this->got_->g_o_t());
3005 }
cf43a2fe 3006 }
c9269dff
AM
3007 else
3008 {
dd93cd0a
AM
3009 if (this->glink_ != NULL)
3010 {
3011 this->glink_->finalize_data_size();
3012 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3013 this->glink_,
3014 (this->glink_->pltresolve()
3015 + this->glink_->pltresolve_size - 32));
3016 }
c9269dff 3017 }
cf43a2fe 3018
42cacb20
DE
3019 // Emit any relocs we saved in an attempt to avoid generating COPY
3020 // relocs.
3021 if (this->copy_relocs_.any_saved_relocs())
3022 this->copy_relocs_.emit(this->rela_dyn_section(layout));
3023}
3024
3025// Perform a relocation.
3026
3027template<int size, bool big_endian>
3028inline bool
3029Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3
AM
3030 const Relocate_info<size, big_endian>* relinfo,
3031 Target_powerpc* target,
3032 Output_section* os,
3033 size_t relnum,
3034 const elfcpp::Rela<size, big_endian>& rela,
3035 unsigned int r_type,
3036 const Sized_symbol<size>* gsym,
3037 const Symbol_value<size>* psymval,
3038 unsigned char* view,
c9269dff
AM
3039 Address address,
3040 section_size_type view_size)
42cacb20 3041{
dd93cd0a
AM
3042
3043 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3044 || r_type == elfcpp::R_PPC_PLTREL24)
3045 && gsym != NULL
3046 && strcmp(gsym->name(), "__tls_get_addr") == 0);
3047 enum skip_tls last_tls = this->call_tls_get_addr_;
3048 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3049 if (is_tls_call)
3050 {
3051 if (last_tls == CALL_NOT_EXPECTED)
3052 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3053 _("__tls_get_addr call lacks marker reloc"));
3054 else if (last_tls == CALL_SKIP)
3055 return false;
3056 }
3057 else if (last_tls != CALL_NOT_EXPECTED)
3058 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3059 _("missing expected __tls_get_addr call"));
3060
42cacb20 3061 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
dd93cd0a 3062 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
cf43a2fe
AM
3063 const Powerpc_relobj<size, big_endian>* const object
3064 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a
AM
3065 Address value = 0;
3066 bool has_plt_value = false;
3067 if (gsym != NULL
3068 && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3069 {
3070 const Output_data_glink<size, big_endian>* glink
3071 = target->glink_section();
3072 unsigned int glink_index = glink->find_entry(gsym, rela, object);
3073 value = glink->address() + glink_index * glink->glink_entry_size();
3074 has_plt_value = true;
3075 }
cf43a2fe
AM
3076
3077 if (r_type == elfcpp::R_POWERPC_GOT16
3078 || r_type == elfcpp::R_POWERPC_GOT16_LO
3079 || r_type == elfcpp::R_POWERPC_GOT16_HI
3080 || r_type == elfcpp::R_POWERPC_GOT16_HA
3081 || r_type == elfcpp::R_PPC64_GOT16_DS
3082 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 3083 {
cf43a2fe
AM
3084 if (gsym != NULL)
3085 {
3086 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3087 value = gsym->got_offset(GOT_TYPE_STANDARD);
3088 }
3089 else
3090 {
3091 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3092 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3093 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3094 }
dd93cd0a 3095 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
3096 }
3097 else if (r_type == elfcpp::R_PPC64_TOC)
3098 {
c9269dff 3099 value = (target->got_section()->output_section()->address()
dd93cd0a 3100 + object->toc_base_offset());
cf43a2fe
AM
3101 }
3102 else if (gsym != NULL
3103 && (r_type == elfcpp::R_POWERPC_REL24
3104 || r_type == elfcpp::R_PPC_PLTREL24)
dd93cd0a 3105 && has_plt_value)
cf43a2fe 3106 {
c9269dff
AM
3107 if (size == 64)
3108 {
3109 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3110 Valtype* wv = reinterpret_cast<Valtype*>(view);
3111 bool can_plt_call = false;
3112 if (rela.get_r_offset() + 8 <= view_size)
3113 {
3114 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3115 if (insn2 == nop
3116 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31)
3117 {
3118 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3119 can_plt_call = true;
3120 }
3121 }
3122 if (!can_plt_call)
3123 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3124 _("call lacks nop, can't restore toc"));
3125 }
cf43a2fe 3126 }
dd93cd0a
AM
3127 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3128 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3129 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3130 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3131 {
3132 // First instruction of a global dynamic sequence, arg setup insn.
3133 const bool final = gsym == NULL || gsym->final_value_is_known();
3134 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3135 enum Got_type got_type = GOT_TYPE_STANDARD;
3136 if (tls_type == tls::TLSOPT_NONE)
3137 got_type = GOT_TYPE_TLSGD;
3138 else if (tls_type == tls::TLSOPT_TO_IE)
3139 got_type = GOT_TYPE_TPREL;
3140 if (got_type != GOT_TYPE_STANDARD)
3141 {
3142 if (gsym != NULL)
3143 {
3144 gold_assert(gsym->has_got_offset(got_type));
3145 value = gsym->got_offset(got_type);
3146 }
3147 else
3148 {
3149 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3150 gold_assert(object->local_has_got_offset(r_sym, got_type));
3151 value = object->local_got_offset(r_sym, got_type);
3152 }
3153 value -= target->got_section()->got_base_offset(object);
3154 }
3155 if (tls_type == tls::TLSOPT_TO_IE)
3156 {
3157 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3158 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3159 {
3160 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3161 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3162 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3163 if (size == 32)
3164 insn |= 32 << 26; // lwz
3165 else
3166 insn |= 58 << 26; // ld
3167 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3168 }
3169 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3170 - elfcpp::R_POWERPC_GOT_TLSGD16);
3171 }
3172 else if (tls_type == tls::TLSOPT_TO_LE)
3173 {
3174 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3175 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3176 {
3177 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3178 Insn insn = addis_3_13;
3179 if (size == 32)
3180 insn = addis_3_2;
3181 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3182 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3183 value = psymval->value(object, rela.get_r_addend());
3184 }
3185 else
3186 {
3187 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3188 Insn insn = nop;
3189 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3190 r_type = elfcpp::R_POWERPC_NONE;
3191 }
3192 }
3193 }
3194 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3195 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3196 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3197 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3198 {
3199 // First instruction of a local dynamic sequence, arg setup insn.
3200 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3201 if (tls_type == tls::TLSOPT_NONE)
3202 {
3203 value = target->tlsld_got_offset();
3204 value -= target->got_section()->got_base_offset(object);
3205 }
3206 else
3207 {
3208 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3209 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3210 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3211 {
3212 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3213 Insn insn = addis_3_13;
3214 if (size == 32)
3215 insn = addis_3_2;
3216 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3217 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 3218 value = dtp_offset;
dd93cd0a
AM
3219 }
3220 else
3221 {
3222 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3223 Insn insn = nop;
3224 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3225 r_type = elfcpp::R_POWERPC_NONE;
3226 }
3227 }
3228 }
3229 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3230 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3231 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3232 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3233 {
3234 // Accesses relative to a local dynamic sequence address,
3235 // no optimisation here.
3236 if (gsym != NULL)
3237 {
3238 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3239 value = gsym->got_offset(GOT_TYPE_DTPREL);
3240 }
3241 else
3242 {
3243 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3244 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3245 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3246 }
3247 value -= target->got_section()->got_base_offset(object);
3248 }
3249 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3250 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3251 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3252 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3253 {
3254 // First instruction of initial exec sequence.
3255 const bool final = gsym == NULL || gsym->final_value_is_known();
3256 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3257 if (tls_type == tls::TLSOPT_NONE)
3258 {
3259 if (gsym != NULL)
3260 {
3261 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3262 value = gsym->got_offset(GOT_TYPE_TPREL);
3263 }
3264 else
3265 {
3266 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3267 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3268 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3269 }
3270 value -= target->got_section()->got_base_offset(object);
3271 }
3272 else
3273 {
3274 gold_assert(tls_type == tls::TLSOPT_TO_LE);
3275 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3276 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3277 {
3278 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3279 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3280 insn &= (1 << 26) - (1 << 21); // extract rt from ld
3281 if (size == 32)
3282 insn |= addis_0_2;
3283 else
3284 insn |= addis_0_13;
3285 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3286 r_type = elfcpp::R_POWERPC_TPREL16_HA;
3287 value = psymval->value(object, rela.get_r_addend());
3288 }
3289 else
3290 {
3291 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3292 Insn insn = nop;
3293 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3294 r_type = elfcpp::R_POWERPC_NONE;
3295 }
3296 }
3297 }
3298 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3299 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3300 {
3301 // Second instruction of a global dynamic sequence,
3302 // the __tls_get_addr call
3303 this->call_tls_get_addr_ = CALL_EXPECTED;
3304 const bool final = gsym == NULL || gsym->final_value_is_known();
3305 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3306 if (tls_type != tls::TLSOPT_NONE)
3307 {
3308 if (tls_type == tls::TLSOPT_TO_IE)
3309 {
3310 Insn* iview = reinterpret_cast<Insn*>(view);
3311 Insn insn = add_3_3_13;
3312 if (size == 32)
3313 insn = add_3_3_2;
3314 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3315 r_type = elfcpp::R_POWERPC_NONE;
3316 }
3317 else
3318 {
3319 Insn* iview = reinterpret_cast<Insn*>(view);
3320 Insn insn = addi_3_3;
3321 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3322 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3323 view += 2 * big_endian;
3324 value = psymval->value(object, rela.get_r_addend());
3325 }
3326 this->call_tls_get_addr_ = CALL_SKIP;
3327 }
3328 }
3329 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3330 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3331 {
3332 // Second instruction of a local dynamic sequence,
3333 // the __tls_get_addr call
3334 this->call_tls_get_addr_ = CALL_EXPECTED;
3335 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3336 if (tls_type == tls::TLSOPT_TO_LE)
3337 {
3338 Insn* iview = reinterpret_cast<Insn*>(view);
3339 Insn insn = addi_3_3;
3340 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3341 this->call_tls_get_addr_ = CALL_SKIP;
3342 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3343 view += 2 * big_endian;
7404fe1b 3344 value = dtp_offset;
dd93cd0a
AM
3345 }
3346 }
3347 else if (r_type == elfcpp::R_POWERPC_TLS)
3348 {
3349 // Second instruction of an initial exec sequence
3350 const bool final = gsym == NULL || gsym->final_value_is_known();
3351 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3352 if (tls_type == tls::TLSOPT_TO_LE)
3353 {
3354 Insn* iview = reinterpret_cast<Insn*>(view);
3355 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3356 unsigned int reg = size == 32 ? 2 : 13;
3357 insn = at_tls_transform(insn, reg);
3358 gold_assert(insn != 0);
3359 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3360 r_type = elfcpp::R_POWERPC_TPREL16_LO;
3361 view += 2 * big_endian;
3362 value = psymval->value(object, rela.get_r_addend());
3363 }
3364 }
cf43a2fe
AM
3365 else
3366 {
dd93cd0a 3367 Address addend = 0;
cf43a2fe
AM
3368 if (r_type != elfcpp::R_PPC_PLTREL24)
3369 addend = rela.get_r_addend();
dd93cd0a
AM
3370 if (size == 64 || !has_plt_value)
3371 value = psymval->value(object, addend);
3372 if (size == 64 && is_branch_reloc(r_type))
c9269dff 3373 {
dd93cd0a
AM
3374 // If the symbol is defined in an opd section, ie. is a function
3375 // descriptor, use the function descriptor code entry address
c9269dff
AM
3376 Powerpc_relobj<size, big_endian>* symobj = const_cast
3377 <Powerpc_relobj<size, big_endian>*>(object);
3378 if (gsym != NULL)
3379 symobj = static_cast
3380 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3381 unsigned int shndx = symobj->opd_shndx();
3382 Address opd_addr = symobj->get_output_section_offset(shndx);
3383 gold_assert(opd_addr != invalid_address);
3384 opd_addr += symobj->output_section(shndx)->address();
3385 if (value >= opd_addr
3386 && value < opd_addr + symobj->section_size(shndx))
3387 {
3388 Address sec_off;
3389 symobj->get_opd_ent(value - opd_addr, &shndx, &sec_off);
3390 Address sec_addr = symobj->get_output_section_offset(shndx);
3391 gold_assert(sec_addr != invalid_address);
3392 sec_addr += symobj->output_section(shndx)->address();
3393 value = sec_addr + sec_off;
3394 }
3395 }
42cacb20
DE
3396 }
3397
42cacb20
DE
3398 switch (r_type)
3399 {
dd93cd0a
AM
3400 case elfcpp::R_PPC64_REL64:
3401 case elfcpp::R_POWERPC_REL32:
3402 case elfcpp::R_POWERPC_REL24:
3403 case elfcpp::R_PPC_PLTREL24:
3404 case elfcpp::R_PPC_LOCAL24PC:
3405 case elfcpp::R_POWERPC_REL16:
3406 case elfcpp::R_POWERPC_REL16_LO:
3407 case elfcpp::R_POWERPC_REL16_HI:
3408 case elfcpp::R_POWERPC_REL16_HA:
3409 case elfcpp::R_POWERPC_REL14:
3410 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3411 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3412 value -= address;
3413 break;
3414
42cacb20
DE
3415 case elfcpp::R_PPC64_TOC16:
3416 case elfcpp::R_PPC64_TOC16_LO:
3417 case elfcpp::R_PPC64_TOC16_HI:
3418 case elfcpp::R_PPC64_TOC16_HA:
3419 case elfcpp::R_PPC64_TOC16_DS:
3420 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 3421 // Subtract the TOC base address.
c9269dff 3422 value -= (target->got_section()->output_section()->address()
dd93cd0a 3423 + object->toc_base_offset());
42cacb20
DE
3424 break;
3425
cf43a2fe
AM
3426 case elfcpp::R_POWERPC_SECTOFF:
3427 case elfcpp::R_POWERPC_SECTOFF_LO:
3428 case elfcpp::R_POWERPC_SECTOFF_HI:
3429 case elfcpp::R_POWERPC_SECTOFF_HA:
3430 case elfcpp::R_PPC64_SECTOFF_DS:
3431 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3432 if (os != NULL)
3433 value -= os->address();
42cacb20
DE
3434 break;
3435
dd93cd0a
AM
3436 case elfcpp::R_PPC64_TPREL16_DS:
3437 case elfcpp::R_PPC64_TPREL16_LO_DS:
3438 if (size != 64)
3439 // R_PPC_TLSGD and R_PPC_TLSLD
3440 break;
3441 case elfcpp::R_POWERPC_TPREL16:
3442 case elfcpp::R_POWERPC_TPREL16_LO:
3443 case elfcpp::R_POWERPC_TPREL16_HI:
3444 case elfcpp::R_POWERPC_TPREL16_HA:
3445 case elfcpp::R_POWERPC_TPREL:
3446 case elfcpp::R_PPC64_TPREL16_HIGHER:
3447 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3448 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3449 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3450 // tls symbol values are relative to tls_segment()->vaddr()
3451 value -= tp_offset;
3452 break;
3453
3454 case elfcpp::R_PPC64_DTPREL16_DS:
3455 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3456 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3457 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3458 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3459 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3460 if (size != 64)
3461 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3462 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3463 break;
3464 case elfcpp::R_POWERPC_DTPREL16:
3465 case elfcpp::R_POWERPC_DTPREL16_LO:
3466 case elfcpp::R_POWERPC_DTPREL16_HI:
3467 case elfcpp::R_POWERPC_DTPREL16_HA:
3468 case elfcpp::R_POWERPC_DTPREL:
3469 // tls symbol values are relative to tls_segment()->vaddr()
3470 value -= dtp_offset;
3471 break;
3472
42cacb20
DE
3473 default:
3474 break;
3475 }
3476
dd93cd0a 3477 Insn branch_bit = 0;
42cacb20
DE
3478 switch (r_type)
3479 {
dd93cd0a
AM
3480 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3481 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3482 branch_bit = 1 << 21;
3483 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3484 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3485 {
3486 Insn* iview = reinterpret_cast<Insn*>(view);
3487 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3488 insn &= ~(1 << 21);
3489 insn |= branch_bit;
3490 if (this->is_isa_v2)
3491 {
3492 // Set 'a' bit. This is 0b00010 in BO field for branch
3493 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3494 // for branch on CTR insns (BO == 1a00t or 1a01t).
3495 if ((insn & (0x14 << 21)) == (0x04 << 21))
3496 insn |= 0x02 << 21;
3497 else if ((insn & (0x14 << 21)) == (0x10 << 21))
3498 insn |= 0x08 << 21;
3499 else
3500 break;
3501 }
3502 else
3503 {
3504 // Invert 'y' bit if not the default.
3505 if (static_cast<Signed_address>(value) < 0)
3506 insn ^= 1 << 21;
3507 }
3508 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3509 }
3510 break;
3511
3512 default:
3513 break;
3514 }
3515
3516 enum Reloc::overflow_check overflow = Reloc::check_none;
3517 switch (r_type)
3518 {
3519 case elfcpp::R_POWERPC_ADDR32:
3520 case elfcpp::R_POWERPC_UADDR32:
3521 if (size == 64)
3522 overflow = Reloc::check_bitfield;
42cacb20
DE
3523 break;
3524
3525 case elfcpp::R_POWERPC_REL32:
dd93cd0a
AM
3526 if (size == 64)
3527 overflow = Reloc::check_signed;
3528 break;
3529
3530 case elfcpp::R_POWERPC_ADDR24:
3531 case elfcpp::R_POWERPC_ADDR16:
3532 case elfcpp::R_POWERPC_UADDR16:
3533 case elfcpp::R_PPC64_ADDR16_DS:
3534 case elfcpp::R_POWERPC_ADDR14:
3535 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3536 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3537 overflow = Reloc::check_bitfield;
42cacb20
DE
3538 break;
3539
3540 case elfcpp::R_POWERPC_REL24:
42cacb20 3541 case elfcpp::R_PPC_PLTREL24:
cf43a2fe 3542 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
3543 case elfcpp::R_POWERPC_REL16:
3544 case elfcpp::R_PPC64_TOC16:
3545 case elfcpp::R_POWERPC_GOT16:
3546 case elfcpp::R_POWERPC_SECTOFF:
3547 case elfcpp::R_POWERPC_TPREL16:
3548 case elfcpp::R_POWERPC_DTPREL16:
3549 case elfcpp::R_PPC64_TPREL16_DS:
3550 case elfcpp::R_PPC64_DTPREL16_DS:
3551 case elfcpp::R_PPC64_TOC16_DS:
3552 case elfcpp::R_PPC64_GOT16_DS:
3553 case elfcpp::R_PPC64_SECTOFF_DS:
3554 case elfcpp::R_POWERPC_REL14:
3555 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3556 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3557 case elfcpp::R_POWERPC_GOT_TLSGD16:
3558 case elfcpp::R_POWERPC_GOT_TLSLD16:
3559 case elfcpp::R_POWERPC_GOT_TPREL16:
3560 case elfcpp::R_POWERPC_GOT_DTPREL16:
3561 overflow = Reloc::check_signed;
42cacb20 3562 break;
dd93cd0a 3563 }
42cacb20 3564
dd93cd0a
AM
3565 switch (r_type)
3566 {
3567 case elfcpp::R_POWERPC_NONE:
3568 case elfcpp::R_POWERPC_TLS:
3569 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3570 case elfcpp::R_POWERPC_GNU_VTENTRY:
3571 case elfcpp::R_PPC_EMB_MRKREF:
42cacb20
DE
3572 break;
3573
3574 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 3575 case elfcpp::R_PPC64_REL64:
cf43a2fe 3576 case elfcpp::R_PPC64_TOC:
dd93cd0a
AM
3577 Reloc::addr64(view, value);
3578 break;
3579
3580 case elfcpp::R_POWERPC_TPREL:
3581 case elfcpp::R_POWERPC_DTPREL:
3582 if (size == 64)
3583 Reloc::addr64(view, value);
3584 else
3585 Reloc::addr32(view, value, overflow);
3586 break;
3587
3588 case elfcpp::R_PPC64_UADDR64:
3589 Reloc::addr64_u(view, value);
42cacb20
DE
3590 break;
3591
3592 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
3593 case elfcpp::R_POWERPC_REL32:
3594 Reloc::addr32(view, value, overflow);
3595 break;
3596
3597 case elfcpp::R_POWERPC_UADDR32:
3598 Reloc::addr32_u(view, value, overflow);
3599 break;
3600
3601 case elfcpp::R_POWERPC_ADDR24:
3602 case elfcpp::R_POWERPC_REL24:
3603 case elfcpp::R_PPC_PLTREL24:
3604 case elfcpp::R_PPC_LOCAL24PC:
3605 Reloc::addr24(view, value, overflow);
42cacb20
DE
3606 break;
3607
dd93cd0a
AM
3608 case elfcpp::R_POWERPC_GOT_DTPREL16:
3609 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3610 if (size == 64)
3611 {
3612 Reloc::addr16_ds(view, value, overflow);
3613 break;
3614 }
cf43a2fe 3615 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 3616 case elfcpp::R_POWERPC_REL16:
cf43a2fe 3617 case elfcpp::R_PPC64_TOC16:
42cacb20 3618 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 3619 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
3620 case elfcpp::R_POWERPC_TPREL16:
3621 case elfcpp::R_POWERPC_DTPREL16:
3622 case elfcpp::R_POWERPC_GOT_TLSGD16:
3623 case elfcpp::R_POWERPC_GOT_TLSLD16:
3624 case elfcpp::R_POWERPC_GOT_TPREL16:
cf43a2fe 3625 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 3626 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 3627 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 3628 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe 3629 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
3630 case elfcpp::R_POWERPC_TPREL16_LO:
3631 case elfcpp::R_POWERPC_DTPREL16_LO:
3632 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3633 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3634 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3635 Reloc::addr16(view, value, overflow);
3636 break;
3637
3638 case elfcpp::R_POWERPC_UADDR16:
3639 Reloc::addr16_u(view, value, overflow);
42cacb20
DE
3640 break;
3641
cf43a2fe 3642 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 3643 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 3644 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 3645 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe 3646 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
3647 case elfcpp::R_POWERPC_TPREL16_HI:
3648 case elfcpp::R_POWERPC_DTPREL16_HI:
3649 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3650 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3651 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3652 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3653 Reloc::addr16_hi(view, value);
42cacb20
DE
3654 break;
3655
cf43a2fe 3656 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 3657 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 3658 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 3659 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe 3660 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
3661 case elfcpp::R_POWERPC_TPREL16_HA:
3662 case elfcpp::R_POWERPC_DTPREL16_HA:
3663 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3664 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3665 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3666 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3667 Reloc::addr16_ha(view, value);
42cacb20
DE
3668 break;
3669
dd93cd0a
AM
3670 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3671 if (size == 32)
3672 // R_PPC_EMB_NADDR16_LO
3673 goto unsupp;
3674 case elfcpp::R_PPC64_ADDR16_HIGHER:
3675 case elfcpp::R_PPC64_TPREL16_HIGHER:
3676 Reloc::addr16_hi2(view, value);
42cacb20
DE
3677 break;
3678
dd93cd0a
AM
3679 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3680 if (size == 32)
3681 // R_PPC_EMB_NADDR16_HI
3682 goto unsupp;
3683 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3684 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3685 Reloc::addr16_ha2(view, value);
42cacb20
DE
3686 break;
3687
dd93cd0a
AM
3688 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3689 if (size == 32)
3690 // R_PPC_EMB_NADDR16_HA
3691 goto unsupp;
3692 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3693 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3694 Reloc::addr16_hi3(view, value);
42cacb20
DE
3695 break;
3696
dd93cd0a
AM
3697 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3698 if (size == 32)
3699 // R_PPC_EMB_SDAI16
3700 goto unsupp;
3701 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3702 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3703 Reloc::addr16_ha3(view, value);
3704 break;
3705
3706 case elfcpp::R_PPC64_DTPREL16_DS:
3707 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3708 if (size == 32)
3709 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3710 goto unsupp;
3711 case elfcpp::R_PPC64_TPREL16_DS:
3712 case elfcpp::R_PPC64_TPREL16_LO_DS:
3713 if (size == 32)
3714 // R_PPC_TLSGD, R_PPC_TLSLD
3715 break;
cf43a2fe
AM
3716 case elfcpp::R_PPC64_ADDR16_DS:
3717 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
3718 case elfcpp::R_PPC64_TOC16_DS:
3719 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
3720 case elfcpp::R_PPC64_GOT16_DS:
3721 case elfcpp::R_PPC64_GOT16_LO_DS:
3722 case elfcpp::R_PPC64_SECTOFF_DS:
3723 case elfcpp::R_PPC64_SECTOFF_LO_DS:
dd93cd0a
AM
3724 Reloc::addr16_ds(view, value, overflow);
3725 break;
3726
3727 case elfcpp::R_POWERPC_ADDR14:
3728 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3729 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3730 case elfcpp::R_POWERPC_REL14:
3731 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3732 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3733 Reloc::addr14(view, value, overflow);
42cacb20
DE
3734 break;
3735
3736 case elfcpp::R_POWERPC_COPY:
3737 case elfcpp::R_POWERPC_GLOB_DAT:
3738 case elfcpp::R_POWERPC_JMP_SLOT:
3739 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 3740 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
3741 case elfcpp::R_PPC64_JMP_IREL:
3742 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
3743 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3744 _("unexpected reloc %u in object file"),
3745 r_type);
3746 break;
3747
dd93cd0a
AM
3748 case elfcpp::R_PPC_EMB_SDA21:
3749 if (size == 32)
3750 goto unsupp;
3751 else
3752 {
3753 // R_PPC64_TOCSAVE. For the time being this can be ignored.
3754 }
3755 break;
3756
3757 case elfcpp::R_PPC_EMB_SDA2I16:
3758 case elfcpp::R_PPC_EMB_SDA2REL:
3759 if (size == 32)
3760 goto unsupp;
3761 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
3762 break;
3763
dd93cd0a
AM
3764 case elfcpp::R_POWERPC_PLT32:
3765 case elfcpp::R_POWERPC_PLTREL32:
3766 case elfcpp::R_POWERPC_PLT16_LO:
3767 case elfcpp::R_POWERPC_PLT16_HI:
3768 case elfcpp::R_POWERPC_PLT16_HA:
3769 case elfcpp::R_PPC_SDAREL16:
3770 case elfcpp::R_POWERPC_ADDR30:
3771 case elfcpp::R_PPC64_PLT64:
3772 case elfcpp::R_PPC64_PLTREL64:
3773 case elfcpp::R_PPC64_PLTGOT16:
3774 case elfcpp::R_PPC64_PLTGOT16_LO:
3775 case elfcpp::R_PPC64_PLTGOT16_HI:
3776 case elfcpp::R_PPC64_PLTGOT16_HA:
3777 case elfcpp::R_PPC64_PLT16_LO_DS:
3778 case elfcpp::R_PPC64_PLTGOT16_DS:
3779 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3780 case elfcpp::R_PPC_EMB_RELSEC16:
3781 case elfcpp::R_PPC_EMB_RELST_LO:
3782 case elfcpp::R_PPC_EMB_RELST_HI:
3783 case elfcpp::R_PPC_EMB_RELST_HA:
3784 case elfcpp::R_PPC_EMB_BIT_FLD:
3785 case elfcpp::R_PPC_EMB_RELSDA:
3786 case elfcpp::R_PPC_TOC16:
42cacb20 3787 default:
dd93cd0a 3788 unsupp:
42cacb20
DE
3789 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3790 _("unsupported reloc %u"),
3791 r_type);
3792 break;
3793 }
3794
3795 return true;
3796}
3797
42cacb20
DE
3798// Relocate section data.
3799
3800template<int size, bool big_endian>
3801void
3802Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
3803 const Relocate_info<size, big_endian>* relinfo,
3804 unsigned int sh_type,
3805 const unsigned char* prelocs,
3806 size_t reloc_count,
3807 Output_section* output_section,
3808 bool needs_special_offset_handling,
3809 unsigned char* view,
c9269dff 3810 Address address,
d83ce4e3
AM
3811 section_size_type view_size,
3812 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
3813{
3814 typedef Target_powerpc<size, big_endian> Powerpc;
3815 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
3816
3817 gold_assert(sh_type == elfcpp::SHT_RELA);
3818
3819 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
d83ce4e3 3820 Powerpc_relocate>(
42cacb20
DE
3821 relinfo,
3822 this,
3823 prelocs,
3824 reloc_count,
3825 output_section,
3826 needs_special_offset_handling,
3827 view,
3828 address,
364c7fa5
ILT
3829 view_size,
3830 reloc_symbol_changes);
42cacb20
DE
3831}
3832
cf43a2fe 3833class Powerpc_scan_relocatable_reloc
42cacb20 3834{
cf43a2fe
AM
3835public:
3836 // Return the strategy to use for a local symbol which is not a
3837 // section symbol, given the relocation type.
3838 inline Relocatable_relocs::Reloc_strategy
3839 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
3840 {
3841 if (r_type == 0 && r_sym == 0)
3842 return Relocatable_relocs::RELOC_DISCARD;
3843 return Relocatable_relocs::RELOC_COPY;
3844 }
3845
3846 // Return the strategy to use for a local symbol which is a section
3847 // symbol, given the relocation type.
3848 inline Relocatable_relocs::Reloc_strategy
3849 local_section_strategy(unsigned int, Relobj*)
3850 {
3851 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
3852 }
3853
3854 // Return the strategy to use for a global symbol, given the
3855 // relocation type, the object, and the symbol index.
3856 inline Relocatable_relocs::Reloc_strategy
3857 global_strategy(unsigned int r_type, Relobj*, unsigned int)
3858 {
3859 if (r_type == elfcpp::R_PPC_PLTREL24)
3860 return Relocatable_relocs::RELOC_SPECIAL;
3861 return Relocatable_relocs::RELOC_COPY;
3862 }
3863};
42cacb20
DE
3864
3865// Scan the relocs during a relocatable link.
3866
3867template<int size, bool big_endian>
3868void
3869Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
3870 Symbol_table* symtab,
3871 Layout* layout,
3872 Sized_relobj_file<size, big_endian>* object,
3873 unsigned int data_shndx,
3874 unsigned int sh_type,
3875 const unsigned char* prelocs,
3876 size_t reloc_count,
3877 Output_section* output_section,
3878 bool needs_special_offset_handling,
3879 size_t local_symbol_count,
3880 const unsigned char* plocal_symbols,
3881 Relocatable_relocs* rr)
42cacb20
DE
3882{
3883 gold_assert(sh_type == elfcpp::SHT_RELA);
3884
42cacb20 3885 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
d83ce4e3 3886 Powerpc_scan_relocatable_reloc>(
42cacb20
DE
3887 symtab,
3888 layout,
3889 object,
3890 data_shndx,
3891 prelocs,
3892 reloc_count,
3893 output_section,
3894 needs_special_offset_handling,
3895 local_symbol_count,
3896 plocal_symbols,
3897 rr);
3898}
3899
7404fe1b 3900// Emit relocations for a section.
dd93cd0a
AM
3901// This is a modified version of the function by the same name in
3902// target-reloc.h. Using relocate_special_relocatable for
3903// R_PPC_PLTREL24 would require duplication of the entire body of the
3904// loop, so we may as well duplicate the whole thing.
42cacb20
DE
3905
3906template<int size, bool big_endian>
3907void
7404fe1b 3908Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
3909 const Relocate_info<size, big_endian>* relinfo,
3910 unsigned int sh_type,
3911 const unsigned char* prelocs,
3912 size_t reloc_count,
3913 Output_section* output_section,
3914 off_t offset_in_output_section,
3915 const Relocatable_relocs* rr,
cf43a2fe 3916 unsigned char*,
dd93cd0a 3917 Address view_address,
cf43a2fe 3918 section_size_type,
42cacb20
DE
3919 unsigned char* reloc_view,
3920 section_size_type reloc_view_size)
3921{
3922 gold_assert(sh_type == elfcpp::SHT_RELA);
3923
cf43a2fe
AM
3924 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3925 Reltype;
3926 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3927 Reltype_write;
3928 const int reloc_size
3929 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
cf43a2fe
AM
3930
3931 Powerpc_relobj<size, big_endian>* const object
3932 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3933 const unsigned int local_count = object->local_symbol_count();
3934 unsigned int got2_shndx = object->got2_shndx();
c9269dff 3935 Address got2_addend = 0;
cf43a2fe 3936 if (got2_shndx != 0)
c9269dff
AM
3937 {
3938 got2_addend = object->get_output_section_offset(got2_shndx);
3939 gold_assert(got2_addend != invalid_address);
3940 }
cf43a2fe
AM
3941
3942 unsigned char* pwrite = reloc_view;
7404fe1b 3943 bool zap_next = false;
cf43a2fe
AM
3944 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
3945 {
3946 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
3947 if (strategy == Relocatable_relocs::RELOC_DISCARD)
3948 continue;
3949
3950 Reltype reloc(prelocs);
3951 Reltype_write reloc_write(pwrite);
3952
7404fe1b 3953 Address offset = reloc.get_r_offset();
cf43a2fe 3954 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
3955 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
3956 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
3957 const unsigned int orig_r_sym = r_sym;
3958 typename elfcpp::Elf_types<size>::Elf_Swxword addend
3959 = reloc.get_r_addend();
3960 const Symbol* gsym = NULL;
3961
3962 if (zap_next)
3963 {
3964 // We could arrange to discard these and other relocs for
3965 // tls optimised sequences in the strategy methods, but for
3966 // now do as BFD ld does.
3967 r_type = elfcpp::R_POWERPC_NONE;
3968 zap_next = false;
3969 }
cf43a2fe
AM
3970
3971 // Get the new symbol index.
cf43a2fe
AM
3972 if (r_sym < local_count)
3973 {
3974 switch (strategy)
3975 {
3976 case Relocatable_relocs::RELOC_COPY:
3977 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 3978 if (r_sym != 0)
dd93cd0a 3979 {
7404fe1b
AM
3980 r_sym = object->symtab_index(r_sym);
3981 gold_assert(r_sym != -1U);
dd93cd0a 3982 }
cf43a2fe
AM
3983 break;
3984
3985 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
3986 {
3987 // We are adjusting a section symbol. We need to find
3988 // the symbol table index of the section symbol for
3989 // the output section corresponding to input section
3990 // in which this symbol is defined.
3991 gold_assert(r_sym < local_count);
3992 bool is_ordinary;
3993 unsigned int shndx =
3994 object->local_symbol_input_shndx(r_sym, &is_ordinary);
3995 gold_assert(is_ordinary);
3996 Output_section* os = object->output_section(shndx);
3997 gold_assert(os != NULL);
3998 gold_assert(os->needs_symtab_index());
7404fe1b 3999 r_sym = os->symtab_index();
cf43a2fe
AM
4000 }
4001 break;
4002
4003 default:
4004 gold_unreachable();
4005 }
4006 }
4007 else
4008 {
7404fe1b 4009 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
4010 gold_assert(gsym != NULL);
4011 if (gsym->is_forwarder())
4012 gsym = relinfo->symtab->resolve_forwards(gsym);
4013
4014 gold_assert(gsym->has_symtab_index());
7404fe1b 4015 r_sym = gsym->symtab_index();
cf43a2fe
AM
4016 }
4017
4018 // Get the new offset--the location in the output section where
4019 // this relocation should be applied.
cf43a2fe 4020 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 4021 offset += offset_in_output_section;
cf43a2fe
AM
4022 else
4023 {
c9269dff
AM
4024 section_offset_type sot_offset =
4025 convert_types<section_offset_type, Address>(offset);
cf43a2fe 4026 section_offset_type new_sot_offset =
c9269dff
AM
4027 output_section->output_offset(object, relinfo->data_shndx,
4028 sot_offset);
cf43a2fe 4029 gold_assert(new_sot_offset != -1);
7404fe1b 4030 offset = new_sot_offset;
cf43a2fe
AM
4031 }
4032
dd93cd0a
AM
4033 // In an object file, r_offset is an offset within the section.
4034 // In an executable or dynamic object, generated by
4035 // --emit-relocs, r_offset is an absolute address.
7404fe1b 4036 if (!parameters->options().relocatable())
dd93cd0a 4037 {
7404fe1b 4038 offset += view_address;
dd93cd0a 4039 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 4040 offset -= offset_in_output_section;
dd93cd0a
AM
4041 }
4042
cf43a2fe 4043 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
4044 if (strategy == Relocatable_relocs::RELOC_COPY)
4045 ;
4046 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4047 {
7404fe1b 4048 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
cf43a2fe
AM
4049 addend = psymval->value(object, addend);
4050 }
4051 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4052 {
4053 if (addend >= 32768)
4054 addend += got2_addend;
4055 }
4056 else
4057 gold_unreachable();
4058
7404fe1b
AM
4059 if (!parameters->options().relocatable())
4060 {
4061 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4062 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4063 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4064 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4065 {
4066 // First instruction of a global dynamic sequence,
4067 // arg setup insn.
4068 const bool final = gsym == NULL || gsym->final_value_is_known();
4069 switch (this->optimize_tls_gd(final))
4070 {
4071 case tls::TLSOPT_TO_IE:
4072 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4073 - elfcpp::R_POWERPC_GOT_TLSGD16);
4074 break;
4075 case tls::TLSOPT_TO_LE:
4076 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4077 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4078 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4079 else
4080 {
4081 r_type = elfcpp::R_POWERPC_NONE;
4082 offset -= 2 * big_endian;
4083 }
4084 break;
4085 default:
4086 break;
4087 }
4088 }
4089 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4090 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4091 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4092 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4093 {
4094 // First instruction of a local dynamic sequence,
4095 // arg setup insn.
4096 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4097 {
4098 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4099 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4100 {
4101 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4102 const Output_section* os = relinfo->layout->tls_segment()
4103 ->first_section();
4104 gold_assert(os != NULL);
4105 gold_assert(os->needs_symtab_index());
4106 r_sym = os->symtab_index();
4107 addend = dtp_offset;
4108 }
4109 else
4110 {
4111 r_type = elfcpp::R_POWERPC_NONE;
4112 offset -= 2 * big_endian;
4113 }
4114 }
4115 }
4116 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4117 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4118 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4119 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4120 {
4121 // First instruction of initial exec sequence.
4122 const bool final = gsym == NULL || gsym->final_value_is_known();
4123 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4124 {
4125 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4126 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4127 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4128 else
4129 {
4130 r_type = elfcpp::R_POWERPC_NONE;
4131 offset -= 2 * big_endian;
4132 }
4133 }
4134 }
4135 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4136 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4137 {
4138 // Second instruction of a global dynamic sequence,
4139 // the __tls_get_addr call
4140 const bool final = gsym == NULL || gsym->final_value_is_known();
4141 switch (this->optimize_tls_gd(final))
4142 {
4143 case tls::TLSOPT_TO_IE:
4144 r_type = elfcpp::R_POWERPC_NONE;
4145 zap_next = true;
4146 break;
4147 case tls::TLSOPT_TO_LE:
4148 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4149 offset += 2 * big_endian;
4150 zap_next = true;
4151 break;
4152 default:
4153 break;
4154 }
4155 }
4156 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4157 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4158 {
4159 // Second instruction of a local dynamic sequence,
4160 // the __tls_get_addr call
4161 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4162 {
4163 const Output_section* os = relinfo->layout->tls_segment()
4164 ->first_section();
4165 gold_assert(os != NULL);
4166 gold_assert(os->needs_symtab_index());
4167 r_sym = os->symtab_index();
4168 addend = dtp_offset;
4169 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4170 offset += 2 * big_endian;
4171 zap_next = true;
4172 }
4173 }
4174 else if (r_type == elfcpp::R_POWERPC_TLS)
4175 {
4176 // Second instruction of an initial exec sequence
4177 const bool final = gsym == NULL || gsym->final_value_is_known();
4178 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4179 {
4180 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4181 offset += 2 * big_endian;
4182 }
4183 }
4184 }
4185
4186 reloc_write.put_r_offset(offset);
4187 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4188 reloc_write.put_r_addend(addend);
cf43a2fe
AM
4189
4190 pwrite += reloc_size;
4191 }
4192
4193 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4194 == reloc_view_size);
42cacb20
DE
4195}
4196
4197// Return the value to use for a dynamic which requires special
4198// treatment. This is how we support equality comparisons of function
4199// pointers across shared library boundaries, as described in the
4200// processor specific ABI supplement.
4201
4202template<int size, bool big_endian>
4203uint64_t
4204Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4205{
cf43a2fe
AM
4206 if (size == 32)
4207 {
4208 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4209 return this->plt_section()->address() + gsym->plt_offset();
4210 }
4211 else
4212 gold_unreachable();
42cacb20
DE
4213}
4214
4215// The selector for powerpc object files.
4216
4217template<int size, bool big_endian>
4218class Target_selector_powerpc : public Target_selector
4219{
4220public:
4221 Target_selector_powerpc()
4222 : Target_selector(elfcpp::EM_NONE, size, big_endian,
03ef7571
ILT
4223 (size == 64
4224 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4225 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4226 (size == 64
4227 ? (big_endian ? "elf64ppc" : "elf64lppc")
4228 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
4229 { }
4230
2e702c99
RM
4231 virtual Target*
4232 do_recognize(Input_file*, off_t, int machine, int, int)
42cacb20
DE
4233 {
4234 switch (size)
4235 {
4236 case 64:
2ea97941 4237 if (machine != elfcpp::EM_PPC64)
42cacb20
DE
4238 return NULL;
4239 break;
4240
4241 case 32:
2ea97941 4242 if (machine != elfcpp::EM_PPC)
42cacb20
DE
4243 return NULL;
4244 break;
4245
4246 default:
4247 return NULL;
4248 }
4249
7f055c20 4250 return this->instantiate_target();
42cacb20
DE
4251 }
4252
2e702c99
RM
4253 virtual Target*
4254 do_instantiate_target()
7f055c20 4255 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
4256};
4257
4258Target_selector_powerpc<32, true> target_selector_ppc32;
4259Target_selector_powerpc<32, false> target_selector_ppc32le;
4260Target_selector_powerpc<64, true> target_selector_ppc64;
4261Target_selector_powerpc<64, false> target_selector_ppc64le;
4262
4263} // End anonymous namespace.
This page took 0.601616 seconds and 4 git commands to generate.