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