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