]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/powerpc.cc
Default initialize enum flags to 0
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
2571583a 3// Copyright (C) 2008-2017 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
dc3714f3 26#include <set>
ec661b9d 27#include <algorithm>
42cacb20 28#include "elfcpp.h"
9d5781f8 29#include "dwarf.h"
42cacb20
DE
30#include "parameters.h"
31#include "reloc.h"
32#include "powerpc.h"
33#include "object.h"
34#include "symtab.h"
35#include "layout.h"
36#include "output.h"
37#include "copy-relocs.h"
38#include "target.h"
39#include "target-reloc.h"
40#include "target-select.h"
41#include "tls.h"
42#include "errors.h"
f345227a 43#include "gc.h"
42cacb20
DE
44
45namespace
46{
47
48using namespace gold;
49
50template<int size, bool big_endian>
51class Output_data_plt_powerpc;
52
ec661b9d
AM
53template<int size, bool big_endian>
54class Output_data_brlt_powerpc;
55
cf43a2fe
AM
56template<int size, bool big_endian>
57class Output_data_got_powerpc;
58
59template<int size, bool big_endian>
60class Output_data_glink;
61
ec661b9d
AM
62template<int size, bool big_endian>
63class Stub_table;
64
d49044c7
AM
65template<int size, bool big_endian>
66class Output_data_save_res;
67
a3e60ddb
AM
68template<int size, bool big_endian>
69class Target_powerpc;
70
71struct Stub_table_owner
72{
dc60b26d
AM
73 Stub_table_owner()
74 : output_section(NULL), owner(NULL)
75 { }
76
a3e60ddb
AM
77 Output_section* output_section;
78 const Output_section::Input_section* owner;
79};
80
4d9aa155
AM
81inline bool
82is_branch_reloc(unsigned int r_type);
83
cf43a2fe
AM
84template<int size, bool big_endian>
85class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
86{
87public:
dd93cd0a 88 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
89 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
90 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 91
cf43a2fe
AM
92 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
93 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
94 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
5edad15d
AM
95 special_(0), relatoc_(0), toc_(0), no_toc_opt_(),
96 has_small_toc_reloc_(false), opd_valid_(false), opd_ent_(),
97 access_from_map_(), has14_(), stub_table_index_(),
b4f7960d
AM
98 e_flags_(ehdr.get_e_flags()), st_other_()
99 {
100 this->set_abiversion(0);
101 }
cf43a2fe
AM
102
103 ~Powerpc_relobj()
104 { }
105
b4f7960d
AM
106 // Read the symbols then set up st_other vector.
107 void
108 do_read_symbols(Read_symbols_data*);
109
5edad15d
AM
110 // Arrange to always relocate .toc first.
111 virtual void
112 do_relocate_sections(
113 const Symbol_table* symtab, const Layout* layout,
114 const unsigned char* pshdrs, Output_file* of,
115 typename Sized_relobj_file<size, big_endian>::Views* pviews);
116
117 // The .toc section index.
118 unsigned int
119 toc_shndx() const
120 {
121 return this->toc_;
122 }
123
124 // Mark .toc entry at OFF as not optimizable.
125 void
126 set_no_toc_opt(Address off)
127 {
128 if (this->no_toc_opt_.empty())
129 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
130 / (size / 8));
131 off /= size / 8;
132 if (off < this->no_toc_opt_.size())
133 this->no_toc_opt_[off] = true;
134 }
135
136 // Mark the entire .toc as not optimizable.
137 void
138 set_no_toc_opt()
139 {
140 this->no_toc_opt_.resize(1);
141 this->no_toc_opt_[0] = true;
142 }
143
144 // Return true if code using the .toc entry at OFF should not be edited.
145 bool
146 no_toc_opt(Address off) const
147 {
148 if (this->no_toc_opt_.empty())
149 return false;
150 off /= size / 8;
151 if (off >= this->no_toc_opt_.size())
152 return true;
153 return this->no_toc_opt_[off];
154 }
155
c9269dff 156 // The .got2 section shndx.
cf43a2fe
AM
157 unsigned int
158 got2_shndx() const
159 {
160 if (size == 32)
c9269dff 161 return this->special_;
cf43a2fe
AM
162 else
163 return 0;
164 }
165
c9269dff
AM
166 // The .opd section shndx.
167 unsigned int
168 opd_shndx() const
169 {
170 if (size == 32)
171 return 0;
172 else
173 return this->special_;
174 }
175
176 // Init OPD entry arrays.
177 void
178 init_opd(size_t opd_size)
179 {
180 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 181 this->opd_ent_.resize(count);
c9269dff
AM
182 }
183
184 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
185 unsigned int
186 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
187 {
188 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
189 gold_assert(ndx < this->opd_ent_.size());
190 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 191 if (value != NULL)
bfdfa4cd
AM
192 *value = this->opd_ent_[ndx].off;
193 return this->opd_ent_[ndx].shndx;
c9269dff
AM
194 }
195
196 // Set section and offset of function entry for .opd + R_OFF.
197 void
dd93cd0a 198 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
199 {
200 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
201 gold_assert(ndx < this->opd_ent_.size());
202 this->opd_ent_[ndx].shndx = shndx;
203 this->opd_ent_[ndx].off = value;
204 }
205
206 // Return discard flag for .opd + R_OFF.
207 bool
208 get_opd_discard(Address r_off) const
209 {
210 size_t ndx = this->opd_ent_ndx(r_off);
211 gold_assert(ndx < this->opd_ent_.size());
212 return this->opd_ent_[ndx].discard;
213 }
214
215 // Set discard flag for .opd + R_OFF.
216 void
217 set_opd_discard(Address r_off)
218 {
219 size_t ndx = this->opd_ent_ndx(r_off);
220 gold_assert(ndx < this->opd_ent_.size());
221 this->opd_ent_[ndx].discard = true;
c9269dff
AM
222 }
223
e81fea4d
AM
224 bool
225 opd_valid() const
226 { return this->opd_valid_; }
227
228 void
229 set_opd_valid()
230 { this->opd_valid_ = true; }
231
c9269dff
AM
232 // Examine .rela.opd to build info about function entry points.
233 void
234 scan_opd_relocs(size_t reloc_count,
235 const unsigned char* prelocs,
236 const unsigned char* plocal_syms);
237
5edad15d
AM
238 // Returns true if a code sequence loading a TOC entry can be
239 // converted into code calculating a TOC pointer relative offset.
240 bool
241 make_toc_relative(Target_powerpc<size, big_endian>* target,
242 Address* value);
243
26a4e9cb
AM
244 // Perform the Sized_relobj_file method, then set up opd info from
245 // .opd relocs.
c9269dff
AM
246 void
247 do_read_relocs(Read_relocs_data*);
248
cf43a2fe
AM
249 bool
250 do_find_special_sections(Read_symbols_data* sd);
251
ec4dbad3
AM
252 // Adjust this local symbol value. Return false if the symbol
253 // should be discarded from the output file.
254 bool
255 do_adjust_local_symbol(Symbol_value<size>* lv) const
256 {
257 if (size == 64 && this->opd_shndx() != 0)
258 {
259 bool is_ordinary;
260 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
261 return true;
262 if (this->get_opd_discard(lv->input_value()))
263 return false;
264 }
265 return true;
266 }
267
6c77229c
AM
268 Access_from*
269 access_from_map()
270 { return &this->access_from_map_; }
271
272 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
273 // section at DST_OFF.
274 void
efc6fa12 275 add_reference(Relobj* src_obj,
6c77229c
AM
276 unsigned int src_indx,
277 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
278 {
279 Section_id src_id(src_obj, src_indx);
280 this->access_from_map_[dst_off].insert(src_id);
281 }
282
283 // Add a reference to the code section specified by the .opd entry
284 // at DST_OFF
285 void
286 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
287 {
288 size_t ndx = this->opd_ent_ndx(dst_off);
289 if (ndx >= this->opd_ent_.size())
290 this->opd_ent_.resize(ndx + 1);
291 this->opd_ent_[ndx].gc_mark = true;
292 }
293
294 void
295 process_gc_mark(Symbol_table* symtab)
296 {
297 for (size_t i = 0; i < this->opd_ent_.size(); i++)
298 if (this->opd_ent_[i].gc_mark)
299 {
300 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 301 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
302 }
303 }
304
dd93cd0a
AM
305 // Return offset in output GOT section that this object will use
306 // as a TOC pointer. Won't be just a constant with multi-toc support.
307 Address
308 toc_base_offset() const
309 { return 0x8000; }
310
d8f5a274
AM
311 void
312 set_has_small_toc_reloc()
313 { has_small_toc_reloc_ = true; }
314
315 bool
316 has_small_toc_reloc() const
317 { return has_small_toc_reloc_; }
318
ec661b9d
AM
319 void
320 set_has_14bit_branch(unsigned int shndx)
321 {
322 if (shndx >= this->has14_.size())
323 this->has14_.resize(shndx + 1);
324 this->has14_[shndx] = true;
325 }
326
327 bool
328 has_14bit_branch(unsigned int shndx) const
329 { return shndx < this->has14_.size() && this->has14_[shndx]; }
330
331 void
a3e60ddb 332 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 333 {
a3e60ddb 334 if (shndx >= this->stub_table_index_.size())
dc60b26d 335 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 336 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
337 }
338
339 Stub_table<size, big_endian>*
340 stub_table(unsigned int shndx)
341 {
a3e60ddb
AM
342 if (shndx < this->stub_table_index_.size())
343 {
344 Target_powerpc<size, big_endian>* target
345 = static_cast<Target_powerpc<size, big_endian>*>(
346 parameters->sized_target<size, big_endian>());
347 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
348 if (indx < target->stub_tables().size())
349 return target->stub_tables()[indx];
a3e60ddb 350 }
ec661b9d
AM
351 return NULL;
352 }
353
a3e60ddb
AM
354 void
355 clear_stub_table()
356 {
357 this->stub_table_index_.clear();
358 }
359
b4f7960d
AM
360 int
361 abiversion() const
362 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
363
364 // Set ABI version for input and output
365 void
366 set_abiversion(int ver);
367
368 unsigned int
369 ppc64_local_entry_offset(const Symbol* sym) const
370 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
371
372 unsigned int
373 ppc64_local_entry_offset(unsigned int symndx) const
374 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
375
cf43a2fe 376private:
bfdfa4cd
AM
377 struct Opd_ent
378 {
379 unsigned int shndx;
c6de8ed4
AM
380 bool discard : 1;
381 bool gc_mark : 1;
26a4e9cb 382 Address off;
bfdfa4cd
AM
383 };
384
385 // Return index into opd_ent_ array for .opd entry at OFF.
386 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
387 // apart when the language doesn't use the last 8-byte word, the
388 // environment pointer. Thus dividing the entry section offset by
389 // 16 will give an index into opd_ent_ that works for either layout
390 // of .opd. (It leaves some elements of the vector unused when .opd
391 // entries are spaced 24 bytes apart, but we don't know the spacing
392 // until relocations are processed, and in any case it is possible
393 // for an object to have some entries spaced 16 bytes apart and
394 // others 24 bytes apart.)
c9269dff
AM
395 size_t
396 opd_ent_ndx(size_t off) const
397 { return off >> 4;}
398
399 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
400 unsigned int special_;
bfdfa4cd 401
5edad15d
AM
402 // For 64-bit the .rela.toc and .toc section shdnx.
403 unsigned int relatoc_;
404 unsigned int toc_;
405
406 // For 64-bit, an array with one entry per 64-bit word in the .toc
407 // section, set if accesses using that word cannot be optimised.
408 std::vector<bool> no_toc_opt_;
409
d8f5a274
AM
410 // For 64-bit, whether this object uses small model relocs to access
411 // the toc.
412 bool has_small_toc_reloc_;
413
bfdfa4cd
AM
414 // Set at the start of gc_process_relocs, when we know opd_ent_
415 // vector is valid. The flag could be made atomic and set in
416 // do_read_relocs with memory_order_release and then tested with
417 // memory_order_acquire, potentially resulting in fewer entries in
418 // access_from_map_.
419 bool opd_valid_;
420
c9269dff
AM
421 // The first 8-byte word of an OPD entry gives the address of the
422 // entry point of the function. Relocatable object files have a
bfdfa4cd 423 // relocation on this word. The following vector records the
c9269dff 424 // section and offset specified by these relocations.
bfdfa4cd
AM
425 std::vector<Opd_ent> opd_ent_;
426
e81fea4d 427 // References made to this object's .opd section when running
bfdfa4cd
AM
428 // gc_process_relocs for another object, before the opd_ent_ vector
429 // is valid for this object.
e81fea4d 430 Access_from access_from_map_;
ec661b9d
AM
431
432 // Whether input section has a 14-bit branch reloc.
433 std::vector<bool> has14_;
434
435 // The stub table to use for a given input section.
a3e60ddb 436 std::vector<unsigned int> stub_table_index_;
b4f7960d
AM
437
438 // Header e_flags
439 elfcpp::Elf_Word e_flags_;
440
441 // ELF st_other field for local symbols.
442 std::vector<unsigned char> st_other_;
cf43a2fe
AM
443};
444
dc3714f3
AM
445template<int size, bool big_endian>
446class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
447{
448public:
449 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
450
451 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
452 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
453 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
b4f7960d
AM
454 opd_shndx_(0), opd_ent_(), e_flags_(ehdr.get_e_flags())
455 {
456 this->set_abiversion(0);
457 }
dc3714f3
AM
458
459 ~Powerpc_dynobj()
460 { }
461
462 // Call Sized_dynobj::do_read_symbols to read the symbols then
463 // read .opd from a dynamic object, filling in opd_ent_ vector,
464 void
465 do_read_symbols(Read_symbols_data*);
466
467 // The .opd section shndx.
468 unsigned int
469 opd_shndx() const
470 {
471 return this->opd_shndx_;
472 }
473
474 // The .opd section address.
475 Address
476 opd_address() const
477 {
478 return this->opd_address_;
479 }
480
481 // Init OPD entry arrays.
482 void
483 init_opd(size_t opd_size)
484 {
485 size_t count = this->opd_ent_ndx(opd_size);
486 this->opd_ent_.resize(count);
487 }
488
489 // Return section and offset of function entry for .opd + R_OFF.
490 unsigned int
491 get_opd_ent(Address r_off, Address* value = NULL) const
492 {
493 size_t ndx = this->opd_ent_ndx(r_off);
494 gold_assert(ndx < this->opd_ent_.size());
495 gold_assert(this->opd_ent_[ndx].shndx != 0);
496 if (value != NULL)
497 *value = this->opd_ent_[ndx].off;
498 return this->opd_ent_[ndx].shndx;
499 }
500
501 // Set section and offset of function entry for .opd + R_OFF.
502 void
503 set_opd_ent(Address r_off, unsigned int shndx, Address value)
504 {
505 size_t ndx = this->opd_ent_ndx(r_off);
506 gold_assert(ndx < this->opd_ent_.size());
507 this->opd_ent_[ndx].shndx = shndx;
508 this->opd_ent_[ndx].off = value;
509 }
510
b4f7960d
AM
511 int
512 abiversion() const
513 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
514
515 // Set ABI version for input and output.
516 void
517 set_abiversion(int ver);
518
dc3714f3
AM
519private:
520 // Used to specify extent of executable sections.
521 struct Sec_info
522 {
523 Sec_info(Address start_, Address len_, unsigned int shndx_)
524 : start(start_), len(len_), shndx(shndx_)
525 { }
526
527 bool
528 operator<(const Sec_info& that) const
529 { return this->start < that.start; }
530
531 Address start;
532 Address len;
533 unsigned int shndx;
534 };
535
536 struct Opd_ent
537 {
538 unsigned int shndx;
539 Address off;
540 };
541
542 // Return index into opd_ent_ array for .opd entry at OFF.
543 size_t
544 opd_ent_ndx(size_t off) const
545 { return off >> 4;}
546
547 // For 64-bit the .opd section shndx and address.
548 unsigned int opd_shndx_;
549 Address opd_address_;
550
551 // The first 8-byte word of an OPD entry gives the address of the
552 // entry point of the function. Records the section and offset
553 // corresponding to the address. Note that in dynamic objects,
554 // offset is *not* relative to the section.
555 std::vector<Opd_ent> opd_ent_;
b4f7960d
AM
556
557 // Header e_flags
558 elfcpp::Elf_Word e_flags_;
dc3714f3
AM
559};
560
5edad15d
AM
561// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
562// base class will emit.
563
564template<int sh_type, int size, bool big_endian>
565class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
566{
567 public:
568 Powerpc_copy_relocs()
569 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
570 { }
571
572 // Emit any saved relocations which turn out to be needed. This is
573 // called after all the relocs have been scanned.
574 void
575 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
576};
577
42cacb20
DE
578template<int size, bool big_endian>
579class Target_powerpc : public Sized_target<size, big_endian>
580{
581 public:
d83ce4e3
AM
582 typedef
583 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 584 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 585 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
c9269dff 586 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
587 // Offset of tp and dtp pointers from start of TLS block.
588 static const Address tp_offset = 0x7000;
589 static const Address dtp_offset = 0x8000;
42cacb20
DE
590
591 Target_powerpc()
592 : Sized_target<size, big_endian>(&powerpc_info),
ec661b9d 593 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
5edad15d 594 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 595 tlsld_got_offset_(-1U),
9e69ed50 596 stub_tables_(), branch_lookup_table_(), branch_info_(),
a3e60ddb 597 plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
d49044c7 598 stub_group_size_(0), savres_section_(0)
42cacb20
DE
599 {
600 }
601
2e702c99 602 // Process the relocations to determine unreferenced sections for
6d03d481
ST
603 // garbage collection.
604 void
ad0f2072 605 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
606 Layout* layout,
607 Sized_relobj_file<size, big_endian>* object,
608 unsigned int data_shndx,
609 unsigned int sh_type,
610 const unsigned char* prelocs,
611 size_t reloc_count,
612 Output_section* output_section,
613 bool needs_special_offset_handling,
614 size_t local_symbol_count,
615 const unsigned char* plocal_symbols);
6d03d481 616
42cacb20
DE
617 // Scan the relocations to look for symbol adjustments.
618 void
ad0f2072 619 scan_relocs(Symbol_table* symtab,
42cacb20 620 Layout* layout,
6fa2a40b 621 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
622 unsigned int data_shndx,
623 unsigned int sh_type,
624 const unsigned char* prelocs,
625 size_t reloc_count,
626 Output_section* output_section,
627 bool needs_special_offset_handling,
628 size_t local_symbol_count,
629 const unsigned char* plocal_symbols);
921b5322
AM
630
631 // Map input .toc section to output .got section.
632 const char*
633 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
634 {
635 if (size == 64 && strcmp(name, ".toc") == 0)
636 {
637 *plen = 4;
638 return ".got";
639 }
640 return NULL;
641 }
642
f3a0ed29
AM
643 // Provide linker defined save/restore functions.
644 void
645 define_save_restore_funcs(Layout*, Symbol_table*);
646
ec661b9d
AM
647 // No stubs unless a final link.
648 bool
649 do_may_relax() const
650 { return !parameters->options().relocatable(); }
651
652 bool
653 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
654
9d5781f8
AM
655 void
656 do_plt_fde_location(const Output_data*, unsigned char*,
657 uint64_t*, off_t*) const;
658
ec661b9d
AM
659 // Stash info about branches, for stub generation.
660 void
661 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
662 unsigned int data_shndx, Address r_offset,
663 unsigned int r_type, unsigned int r_sym, Address addend)
664 {
665 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
666 this->branch_info_.push_back(info);
667 if (r_type == elfcpp::R_POWERPC_REL14
668 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
669 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
670 ppc_object->set_has_14bit_branch(data_shndx);
671 }
672
f43ba157
AM
673 void
674 do_define_standard_symbols(Symbol_table*, Layout*);
675
42cacb20
DE
676 // Finalize the sections.
677 void
f59f41f3 678 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
679
680 // Return the value to use for a dynamic which requires special
681 // treatment.
682 uint64_t
683 do_dynsym_value(const Symbol*) const;
684
c9824451
AM
685 // Return the PLT address to use for a local symbol.
686 uint64_t
687 do_plt_address_for_local(const Relobj*, unsigned int) const;
688
689 // Return the PLT address to use for a global symbol.
690 uint64_t
691 do_plt_address_for_global(const Symbol*) const;
692
bd73a62d
AM
693 // Return the offset to use for the GOT_INDX'th got entry which is
694 // for a local tls symbol specified by OBJECT, SYMNDX.
695 int64_t
696 do_tls_offset_for_local(const Relobj* object,
697 unsigned int symndx,
698 unsigned int got_indx) const;
699
700 // Return the offset to use for the GOT_INDX'th got entry which is
701 // for global tls symbol GSYM.
702 int64_t
703 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
704
dc3714f3
AM
705 void
706 do_function_location(Symbol_location*) const;
707
4d9aa155
AM
708 bool
709 do_can_check_for_function_pointers() const
710 { return true; }
711
bbec1a5d
AM
712 // Adjust -fsplit-stack code which calls non-split-stack code.
713 void
714 do_calls_non_split(Relobj* object, unsigned int shndx,
715 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 716 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
717 unsigned char* view, section_size_type view_size,
718 std::string* from, std::string* to) const;
719
42cacb20
DE
720 // Relocate a section.
721 void
722 relocate_section(const Relocate_info<size, big_endian>*,
723 unsigned int sh_type,
724 const unsigned char* prelocs,
725 size_t reloc_count,
726 Output_section* output_section,
727 bool needs_special_offset_handling,
728 unsigned char* view,
c9269dff 729 Address view_address,
364c7fa5
ILT
730 section_size_type view_size,
731 const Reloc_symbol_changes*);
42cacb20
DE
732
733 // Scan the relocs during a relocatable link.
734 void
ad0f2072 735 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 736 Layout* layout,
6fa2a40b 737 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
738 unsigned int data_shndx,
739 unsigned int sh_type,
740 const unsigned char* prelocs,
741 size_t reloc_count,
742 Output_section* output_section,
743 bool needs_special_offset_handling,
744 size_t local_symbol_count,
745 const unsigned char* plocal_symbols,
746 Relocatable_relocs*);
747
4d625b70
CC
748 // Scan the relocs for --emit-relocs.
749 void
750 emit_relocs_scan(Symbol_table* symtab,
751 Layout* layout,
752 Sized_relobj_file<size, big_endian>* object,
753 unsigned int data_shndx,
754 unsigned int sh_type,
755 const unsigned char* prelocs,
756 size_t reloc_count,
757 Output_section* output_section,
758 bool needs_special_offset_handling,
759 size_t local_symbol_count,
760 const unsigned char* plocal_syms,
761 Relocatable_relocs* rr);
762
7404fe1b 763 // Emit relocations for a section.
42cacb20 764 void
7404fe1b
AM
765 relocate_relocs(const Relocate_info<size, big_endian>*,
766 unsigned int sh_type,
767 const unsigned char* prelocs,
768 size_t reloc_count,
769 Output_section* output_section,
62fe925a
RM
770 typename elfcpp::Elf_types<size>::Elf_Off
771 offset_in_output_section,
7404fe1b
AM
772 unsigned char*,
773 Address view_address,
774 section_size_type,
775 unsigned char* reloc_view,
776 section_size_type reloc_view_size);
42cacb20
DE
777
778 // Return whether SYM is defined by the ABI.
779 bool
9c2d0ef9 780 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 781 {
cf43a2fe 782 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
783 }
784
785 // Return the size of the GOT section.
786 section_size_type
0e70b911 787 got_size() const
42cacb20
DE
788 {
789 gold_assert(this->got_ != NULL);
790 return this->got_->data_size();
791 }
792
cf43a2fe
AM
793 // Get the PLT section.
794 const Output_data_plt_powerpc<size, big_endian>*
795 plt_section() const
796 {
797 gold_assert(this->plt_ != NULL);
798 return this->plt_;
799 }
800
e5d5f5ed
AM
801 // Get the IPLT section.
802 const Output_data_plt_powerpc<size, big_endian>*
803 iplt_section() const
804 {
805 gold_assert(this->iplt_ != NULL);
806 return this->iplt_;
807 }
808
cf43a2fe
AM
809 // Get the .glink section.
810 const Output_data_glink<size, big_endian>*
811 glink_section() const
812 {
813 gold_assert(this->glink_ != NULL);
814 return this->glink_;
815 }
816
9055360d
AM
817 Output_data_glink<size, big_endian>*
818 glink_section()
819 {
820 gold_assert(this->glink_ != NULL);
821 return this->glink_;
822 }
823
9d5781f8
AM
824 bool has_glink() const
825 { return this->glink_ != NULL; }
826
cf43a2fe
AM
827 // Get the GOT section.
828 const Output_data_got_powerpc<size, big_endian>*
829 got_section() const
830 {
831 gold_assert(this->got_ != NULL);
832 return this->got_;
833 }
834
26a4e9cb
AM
835 // Get the GOT section, creating it if necessary.
836 Output_data_got_powerpc<size, big_endian>*
837 got_section(Symbol_table*, Layout*);
838
cf43a2fe
AM
839 Object*
840 do_make_elf_object(const std::string&, Input_file*, off_t,
841 const elfcpp::Ehdr<size, big_endian>&);
842
0e70b911
CC
843 // Return the number of entries in the GOT.
844 unsigned int
845 got_entry_count() const
846 {
847 if (this->got_ == NULL)
848 return 0;
849 return this->got_size() / (size / 8);
850 }
851
852 // Return the number of entries in the PLT.
853 unsigned int
854 plt_entry_count() const;
855
856 // Return the offset of the first non-reserved PLT entry.
857 unsigned int
b4f7960d
AM
858 first_plt_entry_offset() const
859 {
860 if (size == 32)
861 return 0;
862 if (this->abiversion() >= 2)
863 return 16;
864 return 24;
865 }
0e70b911
CC
866
867 // Return the size of each PLT entry.
868 unsigned int
b4f7960d
AM
869 plt_entry_size() const
870 {
871 if (size == 32)
872 return 4;
873 if (this->abiversion() >= 2)
874 return 8;
875 return 24;
876 }
0e70b911 877
d49044c7
AM
878 Output_data_save_res<size, big_endian>*
879 savres_section() const
880 {
881 return this->savres_section_;
882 }
883
e81fea4d
AM
884 // Add any special sections for this symbol to the gc work list.
885 // For powerpc64, this adds the code section of a function
886 // descriptor.
887 void
888 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
889
890 // Handle target specific gc actions when adding a gc reference from
891 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
892 // and DST_OFF. For powerpc64, this adds a referenc to the code
893 // section of a function descriptor.
894 void
895 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 896 Relobj* src_obj,
e81fea4d 897 unsigned int src_shndx,
efc6fa12 898 Relobj* dst_obj,
e81fea4d
AM
899 unsigned int dst_shndx,
900 Address dst_off) const;
901
ec661b9d
AM
902 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
903 const Stub_tables&
904 stub_tables() const
905 { return this->stub_tables_; }
906
907 const Output_data_brlt_powerpc<size, big_endian>*
908 brlt_section() const
909 { return this->brlt_section_; }
910
911 void
912 add_branch_lookup_table(Address to)
913 {
914 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
915 this->branch_lookup_table_.insert(std::make_pair(to, off));
916 }
917
918 Address
919 find_branch_lookup_table(Address to)
920 {
921 typename Branch_lookup_table::const_iterator p
922 = this->branch_lookup_table_.find(to);
923 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
924 }
925
926 void
927 write_branch_lookup_table(unsigned char *oview)
928 {
929 for (typename Branch_lookup_table::const_iterator p
930 = this->branch_lookup_table_.begin();
931 p != this->branch_lookup_table_.end();
932 ++p)
933 {
4d5effb9 934 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
935 }
936 }
937
9e69ed50
AM
938 bool
939 plt_thread_safe() const
940 { return this->plt_thread_safe_; }
941
b4f7960d
AM
942 int
943 abiversion () const
944 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
945
946 void
947 set_abiversion (int ver)
948 {
949 elfcpp::Elf_Word flags = this->processor_specific_flags();
950 flags &= ~elfcpp::EF_PPC64_ABI;
951 flags |= ver & elfcpp::EF_PPC64_ABI;
952 this->set_processor_specific_flags(flags);
953 }
954
955 // Offset to to save stack slot
956 int
957 stk_toc () const
958 { return this->abiversion() < 2 ? 40 : 24; }
959
42cacb20
DE
960 private:
961
e3deeb9c
AM
962 class Track_tls
963 {
964 public:
965 enum Tls_get_addr
966 {
967 NOT_EXPECTED = 0,
968 EXPECTED = 1,
969 SKIP = 2,
970 NORMAL = 3
971 };
972
973 Track_tls()
974 : tls_get_addr_(NOT_EXPECTED),
975 relinfo_(NULL), relnum_(0), r_offset_(0)
976 { }
977
978 ~Track_tls()
979 {
980 if (this->tls_get_addr_ != NOT_EXPECTED)
981 this->missing();
982 }
983
984 void
985 missing(void)
986 {
987 if (this->relinfo_ != NULL)
988 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
989 _("missing expected __tls_get_addr call"));
990 }
991
992 void
993 expect_tls_get_addr_call(
994 const Relocate_info<size, big_endian>* relinfo,
995 size_t relnum,
996 Address r_offset)
997 {
998 this->tls_get_addr_ = EXPECTED;
999 this->relinfo_ = relinfo;
1000 this->relnum_ = relnum;
1001 this->r_offset_ = r_offset;
1002 }
1003
1004 void
1005 expect_tls_get_addr_call()
1006 { this->tls_get_addr_ = EXPECTED; }
1007
1008 void
1009 skip_next_tls_get_addr_call()
1010 {this->tls_get_addr_ = SKIP; }
1011
1012 Tls_get_addr
1013 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
1014 {
1015 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
1016 || r_type == elfcpp::R_PPC_PLTREL24)
1017 && gsym != NULL
1018 && strcmp(gsym->name(), "__tls_get_addr") == 0);
1019 Tls_get_addr last_tls = this->tls_get_addr_;
1020 this->tls_get_addr_ = NOT_EXPECTED;
1021 if (is_tls_call && last_tls != EXPECTED)
1022 return last_tls;
1023 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1024 {
1025 this->missing();
1026 return EXPECTED;
1027 }
1028 return NORMAL;
1029 }
1030
1031 private:
1032 // What we're up to regarding calls to __tls_get_addr.
1033 // On powerpc, the branch and link insn making a call to
1034 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1035 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1036 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1037 // The marker relocation always comes first, and has the same
1038 // symbol as the reloc on the insn setting up the __tls_get_addr
1039 // argument. This ties the arg setup insn with the call insn,
1040 // allowing ld to safely optimize away the call. We check that
1041 // every call to __tls_get_addr has a marker relocation, and that
1042 // every marker relocation is on a call to __tls_get_addr.
1043 Tls_get_addr tls_get_addr_;
1044 // Info about the last reloc for error message.
1045 const Relocate_info<size, big_endian>* relinfo_;
1046 size_t relnum_;
1047 Address r_offset_;
1048 };
1049
42cacb20 1050 // The class which scans relocations.
e3deeb9c 1051 class Scan : protected Track_tls
42cacb20
DE
1052 {
1053 public:
bfdfa4cd
AM
1054 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1055
42cacb20 1056 Scan()
e3deeb9c 1057 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1058 { }
1059
95a2c8d6 1060 static inline int
88b8e639 1061 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1062
42cacb20 1063 inline void
ad0f2072 1064 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1065 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1066 unsigned int data_shndx,
1067 Output_section* output_section,
1068 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1069 const elfcpp::Sym<size, big_endian>& lsym,
1070 bool is_discarded);
42cacb20
DE
1071
1072 inline void
ad0f2072 1073 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1074 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1075 unsigned int data_shndx,
1076 Output_section* output_section,
1077 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1078 Symbol* gsym);
1079
21bb3914
ST
1080 inline bool
1081 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1082 Target_powerpc* ,
f6971787 1083 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1084 unsigned int ,
2e702c99
RM
1085 Output_section* ,
1086 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1087 unsigned int r_type,
2e702c99 1088 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1089 {
1090 // PowerPC64 .opd is not folded, so any identical function text
1091 // may be folded and we'll still keep function addresses distinct.
1092 // That means no reloc is of concern here.
1093 if (size == 64)
f6971787
AM
1094 {
1095 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1096 <Powerpc_relobj<size, big_endian>*>(relobj);
1097 if (ppcobj->abiversion() == 1)
1098 return false;
1099 }
1100 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155
AM
1101 // function code might be taking the address of the function.
1102 return !is_branch_reloc(r_type);
1103 }
21bb3914
ST
1104
1105 inline bool
1106 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1107 Target_powerpc* ,
f6971787 1108 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1109 unsigned int ,
1110 Output_section* ,
4d9aa155
AM
1111 const elfcpp::Rela<size, big_endian>& ,
1112 unsigned int r_type,
1113 Symbol*)
1114 {
1115 // As above.
1116 if (size == 64)
f6971787
AM
1117 {
1118 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1119 <Powerpc_relobj<size, big_endian>*>(relobj);
1120 if (ppcobj->abiversion() == 1)
1121 return false;
1122 }
4d9aa155
AM
1123 return !is_branch_reloc(r_type);
1124 }
21bb3914 1125
b3ccdeb5 1126 static bool
9055360d
AM
1127 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1128 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1129 unsigned int r_type, bool report_err);
1130
42cacb20
DE
1131 private:
1132 static void
6fa2a40b 1133 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1134 unsigned int r_type);
1135
1136 static void
6fa2a40b 1137 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1138 unsigned int r_type, Symbol*);
1139
1140 static void
1141 generate_tls_call(Symbol_table* symtab, Layout* layout,
1142 Target_powerpc* target);
1143
1144 void
1145 check_non_pic(Relobj*, unsigned int r_type);
1146
1147 // Whether we have issued an error about a non-PIC compilation.
1148 bool issued_non_pic_error_;
1149 };
1150
1611bc4a
AM
1151 bool
1152 symval_for_branch(const Symbol_table* symtab,
6c77229c 1153 const Sized_symbol<size>* gsym,
3ea0a085 1154 Powerpc_relobj<size, big_endian>* object,
1611bc4a 1155 Address *value, unsigned int *dest_shndx);
3ea0a085 1156
42cacb20 1157 // The class which implements relocation.
e3deeb9c 1158 class Relocate : protected Track_tls
42cacb20
DE
1159 {
1160 public:
dd93cd0a
AM
1161 // Use 'at' branch hints when true, 'y' when false.
1162 // FIXME maybe: set this with an option.
1163 static const bool is_isa_v2 = true;
1164
dd93cd0a 1165 Relocate()
e3deeb9c 1166 : Track_tls()
dd93cd0a
AM
1167 { }
1168
42cacb20
DE
1169 // Do a relocation. Return false if the caller should not issue
1170 // any warnings about this relocation.
1171 inline bool
91a65d2f
AM
1172 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1173 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1174 const Sized_symbol<size>*, const Symbol_value<size>*,
1175 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1176 section_size_type);
42cacb20
DE
1177 };
1178
168a4726
AM
1179 class Relocate_comdat_behavior
1180 {
1181 public:
1182 // Decide what the linker should do for relocations that refer to
1183 // discarded comdat sections.
1184 inline Comdat_behavior
1185 get(const char* name)
1186 {
1187 gold::Default_comdat_behavior default_behavior;
1188 Comdat_behavior ret = default_behavior.get(name);
1189 if (ret == CB_WARNING)
1190 {
1191 if (size == 32
1192 && (strcmp(name, ".fixup") == 0
1193 || strcmp(name, ".got2") == 0))
1194 ret = CB_IGNORE;
1195 if (size == 64
1196 && (strcmp(name, ".opd") == 0
1197 || strcmp(name, ".toc") == 0
1198 || strcmp(name, ".toc1") == 0))
1199 ret = CB_IGNORE;
1200 }
1201 return ret;
1202 }
1203 };
1204
dd93cd0a
AM
1205 // Optimize the TLS relocation type based on what we know about the
1206 // symbol. IS_FINAL is true if the final address of this symbol is
1207 // known at link time.
1208
1209 tls::Tls_optimization
1210 optimize_tls_gd(bool is_final)
1211 {
1212 // If we are generating a shared library, then we can't do anything
1213 // in the linker.
1214 if (parameters->options().shared())
1215 return tls::TLSOPT_NONE;
1216
1217 if (!is_final)
1218 return tls::TLSOPT_TO_IE;
1219 return tls::TLSOPT_TO_LE;
1220 }
1221
1222 tls::Tls_optimization
1223 optimize_tls_ld()
1224 {
1225 if (parameters->options().shared())
1226 return tls::TLSOPT_NONE;
1227
1228 return tls::TLSOPT_TO_LE;
1229 }
1230
1231 tls::Tls_optimization
1232 optimize_tls_ie(bool is_final)
1233 {
1234 if (!is_final || parameters->options().shared())
1235 return tls::TLSOPT_NONE;
1236
1237 return tls::TLSOPT_TO_LE;
1238 }
cf43a2fe 1239
cf43a2fe
AM
1240 // Create glink.
1241 void
1242 make_glink_section(Layout*);
42cacb20 1243
cf43a2fe
AM
1244 // Create the PLT section.
1245 void
40b469d7 1246 make_plt_section(Symbol_table*, Layout*);
42cacb20 1247
e5d5f5ed 1248 void
40b469d7 1249 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1250
ec661b9d
AM
1251 void
1252 make_brlt_section(Layout*);
1253
42cacb20
DE
1254 // Create a PLT entry for a global symbol.
1255 void
ec661b9d 1256 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1257
1258 // Create a PLT entry for a local IFUNC symbol.
1259 void
40b469d7 1260 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1261 Sized_relobj_file<size, big_endian>*,
1262 unsigned int);
1263
42cacb20 1264
dd93cd0a
AM
1265 // Create a GOT entry for local dynamic __tls_get_addr.
1266 unsigned int
1267 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1268 Sized_relobj_file<size, big_endian>* object);
1269
42cacb20 1270 unsigned int
dd93cd0a
AM
1271 tlsld_got_offset() const
1272 {
1273 return this->tlsld_got_offset_;
1274 }
42cacb20 1275
42cacb20
DE
1276 // Get the dynamic reloc section, creating it if necessary.
1277 Reloc_section*
1278 rela_dyn_section(Layout*);
1279
b3ccdeb5
AM
1280 // Similarly, but for ifunc symbols get the one for ifunc.
1281 Reloc_section*
1282 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1283
42cacb20
DE
1284 // Copy a relocation against a global symbol.
1285 void
ef9beddf 1286 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1287 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1288 unsigned int shndx, Output_section* output_section,
1289 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1290 {
859d7987 1291 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1292 this->copy_relocs_.copy_reloc(symtab, layout,
1293 symtab->get_sized_symbol<size>(sym),
1294 object, shndx, output_section,
859d7987
CC
1295 r_type, reloc.get_r_offset(),
1296 reloc.get_r_addend(),
1297 this->rela_dyn_section(layout));
42cacb20
DE
1298 }
1299
0cfdc767 1300 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1301 void
a3e60ddb 1302 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1303
1304 // Sort output sections by address.
1305 struct Sort_sections
1306 {
1307 bool
1308 operator()(const Output_section* sec1, const Output_section* sec2)
1309 { return sec1->address() < sec2->address(); }
1310 };
1311
1312 class Branch_info
1313 {
1314 public:
1315 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1316 unsigned int data_shndx,
1317 Address r_offset,
1318 unsigned int r_type,
1319 unsigned int r_sym,
1320 Address addend)
1321 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1322 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1323 { }
1324
1325 ~Branch_info()
1326 { }
1327
1328 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1329 bool
ec661b9d
AM
1330 make_stub(Stub_table<size, big_endian>*,
1331 Stub_table<size, big_endian>*,
1332 Symbol_table*) const;
1333
1334 private:
1335 // The branch location..
1336 Powerpc_relobj<size, big_endian>* object_;
1337 unsigned int shndx_;
1338 Address offset_;
1339 // ..and the branch type and destination.
1340 unsigned int r_type_;
1341 unsigned int r_sym_;
1342 Address addend_;
1343 };
1344
42cacb20
DE
1345 // Information about this specific target which we pass to the
1346 // general Target structure.
1347 static Target::Target_info powerpc_info;
1348
1349 // The types of GOT entries needed for this platform.
0e70b911
CC
1350 // These values are exposed to the ABI in an incremental link.
1351 // Do not renumber existing values without changing the version
1352 // number of the .gnu_incremental_inputs section.
42cacb20
DE
1353 enum Got_type
1354 {
dd93cd0a
AM
1355 GOT_TYPE_STANDARD,
1356 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1357 GOT_TYPE_DTPREL, // entry for @got@dtprel
1358 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
1359 };
1360
ec661b9d 1361 // The GOT section.
cf43a2fe 1362 Output_data_got_powerpc<size, big_endian>* got_;
b3ccdeb5
AM
1363 // The PLT section. This is a container for a table of addresses,
1364 // and their relocations. Each address in the PLT has a dynamic
1365 // relocation (R_*_JMP_SLOT) and each address will have a
1366 // corresponding entry in .glink for lazy resolution of the PLT.
1367 // ppc32 initialises the PLT to point at the .glink entry, while
1368 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1369 // linker adds a stub that loads the PLT entry into ctr then
1370 // branches to ctr. There may be more than one stub for each PLT
1371 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1372 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1373 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1374 // The IPLT section. Like plt_, this is a container for a table of
1375 // addresses and their relocations, specifically for STT_GNU_IFUNC
1376 // functions that resolve locally (STT_GNU_IFUNC functions that
1377 // don't resolve locally go in PLT). Unlike plt_, these have no
1378 // entry in .glink for lazy resolution, and the relocation section
1379 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1380 // the relocation section may contain relocations against
1381 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1382 // relocation section will appear at the end of other dynamic
1383 // relocations, so that ld.so applies these relocations after other
1384 // dynamic relocations. In a static executable, the relocation
1385 // section is emitted and marked with __rela_iplt_start and
1386 // __rela_iplt_end symbols.
e5d5f5ed 1387 Output_data_plt_powerpc<size, big_endian>* iplt_;
ec661b9d
AM
1388 // Section holding long branch destinations.
1389 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1390 // The .glink section.
cf43a2fe 1391 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1392 // The dynamic reloc section.
42cacb20
DE
1393 Reloc_section* rela_dyn_;
1394 // Relocs saved to avoid a COPY reloc.
5edad15d 1395 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1396 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1397 unsigned int tlsld_got_offset_;
ec661b9d
AM
1398
1399 Stub_tables stub_tables_;
1400 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1401 Branch_lookup_table branch_lookup_table_;
1402
1403 typedef std::vector<Branch_info> Branches;
1404 Branches branch_info_;
9e69ed50
AM
1405
1406 bool plt_thread_safe_;
a3e60ddb
AM
1407
1408 bool relax_failed_;
1409 int relax_fail_count_;
1410 int32_t stub_group_size_;
d49044c7
AM
1411
1412 Output_data_save_res<size, big_endian> *savres_section_;
42cacb20
DE
1413};
1414
1415template<>
1416Target::Target_info Target_powerpc<32, true>::powerpc_info =
1417{
1418 32, // size
1419 true, // is_big_endian
1420 elfcpp::EM_PPC, // machine_code
1421 false, // has_make_symbol
1422 false, // has_resolve
1423 false, // has_code_fill
1424 true, // is_default_stack_executable
b3ce541e 1425 false, // can_icf_inline_merge_sections
42cacb20
DE
1426 '\0', // wrap_char
1427 "/usr/lib/ld.so.1", // dynamic_linker
1428 0x10000000, // default_text_segment_address
1429 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1430 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1431 false, // isolate_execinstr
1432 0, // rosegment_gap
8a5e3e08
ILT
1433 elfcpp::SHN_UNDEF, // small_common_shndx
1434 elfcpp::SHN_UNDEF, // large_common_shndx
1435 0, // small_common_section_flags
05a352e6
DK
1436 0, // large_common_section_flags
1437 NULL, // attributes_section
a67858e0 1438 NULL, // attributes_vendor
8d9743bd
MK
1439 "_start", // entry_symbol_name
1440 32, // hash_entry_size
42cacb20
DE
1441};
1442
1443template<>
1444Target::Target_info Target_powerpc<32, false>::powerpc_info =
1445{
1446 32, // size
1447 false, // is_big_endian
1448 elfcpp::EM_PPC, // machine_code
1449 false, // has_make_symbol
1450 false, // has_resolve
1451 false, // has_code_fill
1452 true, // is_default_stack_executable
b3ce541e 1453 false, // can_icf_inline_merge_sections
42cacb20
DE
1454 '\0', // wrap_char
1455 "/usr/lib/ld.so.1", // dynamic_linker
1456 0x10000000, // default_text_segment_address
1457 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1458 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1459 false, // isolate_execinstr
1460 0, // rosegment_gap
8a5e3e08
ILT
1461 elfcpp::SHN_UNDEF, // small_common_shndx
1462 elfcpp::SHN_UNDEF, // large_common_shndx
1463 0, // small_common_section_flags
05a352e6
DK
1464 0, // large_common_section_flags
1465 NULL, // attributes_section
a67858e0 1466 NULL, // attributes_vendor
8d9743bd
MK
1467 "_start", // entry_symbol_name
1468 32, // hash_entry_size
42cacb20
DE
1469};
1470
1471template<>
1472Target::Target_info Target_powerpc<64, true>::powerpc_info =
1473{
1474 64, // size
1475 true, // is_big_endian
1476 elfcpp::EM_PPC64, // machine_code
1477 false, // has_make_symbol
1478 false, // has_resolve
1479 false, // has_code_fill
1480 true, // is_default_stack_executable
b3ce541e 1481 false, // can_icf_inline_merge_sections
42cacb20
DE
1482 '\0', // wrap_char
1483 "/usr/lib/ld.so.1", // dynamic_linker
1484 0x10000000, // default_text_segment_address
1485 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1486 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1487 false, // isolate_execinstr
1488 0, // rosegment_gap
8a5e3e08
ILT
1489 elfcpp::SHN_UNDEF, // small_common_shndx
1490 elfcpp::SHN_UNDEF, // large_common_shndx
1491 0, // small_common_section_flags
05a352e6
DK
1492 0, // large_common_section_flags
1493 NULL, // attributes_section
a67858e0 1494 NULL, // attributes_vendor
8d9743bd
MK
1495 "_start", // entry_symbol_name
1496 32, // hash_entry_size
42cacb20
DE
1497};
1498
1499template<>
1500Target::Target_info Target_powerpc<64, false>::powerpc_info =
1501{
1502 64, // size
1503 false, // is_big_endian
1504 elfcpp::EM_PPC64, // machine_code
1505 false, // has_make_symbol
1506 false, // has_resolve
1507 false, // has_code_fill
1508 true, // is_default_stack_executable
b3ce541e 1509 false, // can_icf_inline_merge_sections
42cacb20
DE
1510 '\0', // wrap_char
1511 "/usr/lib/ld.so.1", // dynamic_linker
1512 0x10000000, // default_text_segment_address
1513 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1514 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1515 false, // isolate_execinstr
1516 0, // rosegment_gap
8a5e3e08
ILT
1517 elfcpp::SHN_UNDEF, // small_common_shndx
1518 elfcpp::SHN_UNDEF, // large_common_shndx
1519 0, // small_common_section_flags
05a352e6
DK
1520 0, // large_common_section_flags
1521 NULL, // attributes_section
a67858e0 1522 NULL, // attributes_vendor
8d9743bd
MK
1523 "_start", // entry_symbol_name
1524 32, // hash_entry_size
42cacb20
DE
1525};
1526
dd93cd0a
AM
1527inline bool
1528is_branch_reloc(unsigned int r_type)
1529{
1530 return (r_type == elfcpp::R_POWERPC_REL24
1531 || r_type == elfcpp::R_PPC_PLTREL24
1532 || r_type == elfcpp::R_PPC_LOCAL24PC
1533 || r_type == elfcpp::R_POWERPC_REL14
1534 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1535 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1536 || r_type == elfcpp::R_POWERPC_ADDR24
1537 || r_type == elfcpp::R_POWERPC_ADDR14
1538 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1539 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1540}
1541
1542// If INSN is an opcode that may be used with an @tls operand, return
1543// the transformed insn for TLS optimisation, otherwise return 0. If
1544// REG is non-zero only match an insn with RB or RA equal to REG.
1545uint32_t
1546at_tls_transform(uint32_t insn, unsigned int reg)
1547{
1548 if ((insn & (0x3f << 26)) != 31 << 26)
1549 return 0;
1550
1551 unsigned int rtra;
1552 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1553 rtra = insn & ((1 << 26) - (1 << 16));
1554 else if (((insn >> 16) & 0x1f) == reg)
1555 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1556 else
1557 return 0;
1558
1559 if ((insn & (0x3ff << 1)) == 266 << 1)
1560 // add -> addi
1561 insn = 14 << 26;
1562 else if ((insn & (0x1f << 1)) == 23 << 1
1563 && ((insn & (0x1f << 6)) < 14 << 6
1564 || ((insn & (0x1f << 6)) >= 16 << 6
1565 && (insn & (0x1f << 6)) < 24 << 6)))
1566 // load and store indexed -> dform
1567 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1568 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1569 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1570 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1571 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1572 // lwax -> lwa
1573 insn = (58 << 26) | 2;
1574 else
1575 return 0;
1576 insn |= rtra;
1577 return insn;
1578}
1579
dd93cd0a 1580
42cacb20
DE
1581template<int size, bool big_endian>
1582class Powerpc_relocate_functions
1583{
dd93cd0a 1584public:
f4baf0d4 1585 enum Overflow_check
dd93cd0a 1586 {
f4baf0d4
AM
1587 CHECK_NONE,
1588 CHECK_SIGNED,
b80eed39
AM
1589 CHECK_UNSIGNED,
1590 CHECK_BITFIELD,
1591 CHECK_LOW_INSN,
1592 CHECK_HIGH_INSN
dd93cd0a
AM
1593 };
1594
f4baf0d4 1595 enum Status
dd93cd0a 1596 {
f4baf0d4
AM
1597 STATUS_OK,
1598 STATUS_OVERFLOW
1599 };
dd93cd0a 1600
42cacb20 1601private:
c9269dff 1602 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1603 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1604 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1605
dd93cd0a
AM
1606 template<int valsize>
1607 static inline bool
1608 has_overflow_signed(Address value)
1609 {
1610 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1611 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1612 limit <<= ((valsize - 1) >> 1);
1613 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1614 return value + limit > (limit << 1) - 1;
1615 }
1616
1617 template<int valsize>
1618 static inline bool
b80eed39 1619 has_overflow_unsigned(Address value)
dd93cd0a
AM
1620 {
1621 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1622 limit <<= ((valsize - 1) >> 1);
1623 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
1624 return value > (limit << 1) - 1;
1625 }
1626
1627 template<int valsize>
1628 static inline bool
1629 has_overflow_bitfield(Address value)
1630 {
1631 return (has_overflow_unsigned<valsize>(value)
1632 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
1633 }
1634
1635 template<int valsize>
f4baf0d4
AM
1636 static inline Status
1637 overflowed(Address value, Overflow_check overflow)
dd93cd0a 1638 {
f4baf0d4 1639 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
1640 {
1641 if (has_overflow_signed<valsize>(value))
f4baf0d4 1642 return STATUS_OVERFLOW;
dd93cd0a 1643 }
b80eed39
AM
1644 else if (overflow == CHECK_UNSIGNED)
1645 {
1646 if (has_overflow_unsigned<valsize>(value))
1647 return STATUS_OVERFLOW;
1648 }
f4baf0d4 1649 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
1650 {
1651 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 1652 return STATUS_OVERFLOW;
dd93cd0a 1653 }
f4baf0d4 1654 return STATUS_OK;
dd93cd0a
AM
1655 }
1656
cf43a2fe 1657 // Do a simple RELA relocation
0cfb0717 1658 template<int fieldsize, int valsize>
f4baf0d4
AM
1659 static inline Status
1660 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1661 {
0cfb0717 1662 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 1663 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1664 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
1665 return overflowed<valsize>(value, overflow);
1666 }
1667
0cfb0717 1668 template<int fieldsize, int valsize>
f4baf0d4 1669 static inline Status
42cacb20
DE
1670 rela(unsigned char* view,
1671 unsigned int right_shift,
0cfb0717 1672 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 1673 Address value,
f4baf0d4 1674 Overflow_check overflow)
42cacb20 1675 {
0cfb0717 1676 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 1677 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1678 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
dd93cd0a 1679 Valtype reloc = value >> right_shift;
42cacb20
DE
1680 val &= ~dst_mask;
1681 reloc &= dst_mask;
0cfb0717 1682 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
dd93cd0a 1683 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1684 }
1685
cf43a2fe 1686 // Do a simple RELA relocation, unaligned.
0cfb0717 1687 template<int fieldsize, int valsize>
f4baf0d4
AM
1688 static inline Status
1689 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1690 {
0cfb0717 1691 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
1692 return overflowed<valsize>(value, overflow);
1693 }
1694
0cfb0717 1695 template<int fieldsize, int valsize>
f4baf0d4 1696 static inline Status
cf43a2fe
AM
1697 rela_ua(unsigned char* view,
1698 unsigned int right_shift,
0cfb0717 1699 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 1700 Address value,
f4baf0d4 1701 Overflow_check overflow)
42cacb20 1702 {
0cfb0717 1703 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 1704 Valtype;
0cfb0717 1705 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
dd93cd0a 1706 Valtype reloc = value >> right_shift;
42cacb20
DE
1707 val &= ~dst_mask;
1708 reloc &= dst_mask;
0cfb0717 1709 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
dd93cd0a 1710 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1711 }
1712
42cacb20 1713public:
dd93cd0a 1714 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 1715 static inline void
dd93cd0a 1716 addr64(unsigned char* view, Address value)
0cfb0717 1717 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 1718
dd93cd0a 1719 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 1720 static inline void
dd93cd0a 1721 addr64_u(unsigned char* view, Address value)
0cfb0717 1722 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
1723
1724 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
1725 static inline Status
1726 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1727 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
1728
1729 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
1730 static inline Status
1731 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1732 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
1733
1734 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
1735 static inline Status
1736 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1737 {
0cfb0717
AM
1738 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1739 value, overflow);
f4baf0d4
AM
1740 if (overflow != CHECK_NONE && (value & 3) != 0)
1741 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1742 return stat;
1743 }
42cacb20
DE
1744
1745 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
1746 static inline Status
1747 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1748 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 1749
dd93cd0a 1750 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
1751 static inline Status
1752 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1753 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 1754
dd93cd0a 1755 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1756 static inline Status
1757 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1758 {
0cfb0717 1759 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 1760 if ((value & 3) != 0)
f4baf0d4 1761 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1762 return stat;
1763 }
42cacb20 1764
a680de9a
PB
1765 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1766 static inline Status
1767 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
1768 {
1769 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
1770 if ((value & 15) != 0)
1771 stat = STATUS_OVERFLOW;
1772 return stat;
1773 }
1774
42cacb20
DE
1775 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1776 static inline void
dd93cd0a 1777 addr16_hi(unsigned char* view, Address value)
0cfb0717 1778 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 1779
c9269dff 1780 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 1781 static inline void
dd93cd0a
AM
1782 addr16_ha(unsigned char* view, Address value)
1783 { This::addr16_hi(view, value + 0x8000); }
42cacb20 1784
dd93cd0a 1785 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 1786 static inline void
dd93cd0a 1787 addr16_hi2(unsigned char* view, Address value)
0cfb0717 1788 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 1789
dd93cd0a 1790 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 1791 static inline void
dd93cd0a
AM
1792 addr16_ha2(unsigned char* view, Address value)
1793 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 1794
dd93cd0a 1795 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 1796 static inline void
dd93cd0a 1797 addr16_hi3(unsigned char* view, Address value)
0cfb0717 1798 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 1799
dd93cd0a 1800 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 1801 static inline void
dd93cd0a
AM
1802 addr16_ha3(unsigned char* view, Address value)
1803 { This::addr16_hi3(view, value + 0x8000); }
1804
1805 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1806 static inline Status
1807 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1808 {
0cfb0717 1809 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
1810 if (overflow != CHECK_NONE && (value & 3) != 0)
1811 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1812 return stat;
1813 }
a680de9a
PB
1814
1815 // R_POWERPC_REL16DX_HA
1816 static inline Status
1817 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
1818 {
1819 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1820 Valtype* wv = reinterpret_cast<Valtype*>(view);
1821 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1822 value += 0x8000;
1823 value = static_cast<SignedAddress>(value) >> 16;
1824 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
1825 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1826 return overflowed<16>(value, overflow);
1827 }
cf43a2fe
AM
1828};
1829
b4f7960d
AM
1830// Set ABI version for input and output.
1831
1832template<int size, bool big_endian>
1833void
1834Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1835{
1836 this->e_flags_ |= ver;
1837 if (this->abiversion() != 0)
1838 {
1839 Target_powerpc<size, big_endian>* target =
1840 static_cast<Target_powerpc<size, big_endian>*>(
1841 parameters->sized_target<size, big_endian>());
1842 if (target->abiversion() == 0)
1843 target->set_abiversion(this->abiversion());
1844 else if (target->abiversion() != this->abiversion())
1845 gold_error(_("%s: ABI version %d is not compatible "
1846 "with ABI version %d output"),
1847 this->name().c_str(),
1848 this->abiversion(), target->abiversion());
1849
1850 }
1851}
1852
5edad15d
AM
1853// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1854// relocatable object, if such sections exists.
cf43a2fe
AM
1855
1856template<int size, bool big_endian>
1857bool
1858Powerpc_relobj<size, big_endian>::do_find_special_sections(
1859 Read_symbols_data* sd)
1860{
c9269dff
AM
1861 const unsigned char* const pshdrs = sd->section_headers->data();
1862 const unsigned char* namesu = sd->section_names->data();
1863 const char* names = reinterpret_cast<const char*>(namesu);
1864 section_size_type names_size = sd->section_names_size;
1865 const unsigned char* s;
1866
dc3714f3
AM
1867 s = this->template find_shdr<size, big_endian>(pshdrs,
1868 size == 32 ? ".got2" : ".opd",
1869 names, names_size, NULL);
c9269dff
AM
1870 if (s != NULL)
1871 {
1872 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1873 this->special_ = ndx;
b4f7960d
AM
1874 if (size == 64)
1875 {
1876 if (this->abiversion() == 0)
1877 this->set_abiversion(1);
1878 else if (this->abiversion() > 1)
1879 gold_error(_("%s: .opd invalid in abiv%d"),
1880 this->name().c_str(), this->abiversion());
1881 }
c9269dff 1882 }
5edad15d
AM
1883 if (size == 64)
1884 {
1885 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
1886 names, names_size, NULL);
1887 if (s != NULL)
1888 {
1889 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1890 this->relatoc_ = ndx;
1891 typename elfcpp::Shdr<size, big_endian> shdr(s);
1892 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
1893 }
1894 }
c9269dff
AM
1895 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1896}
1897
1898// Examine .rela.opd to build info about function entry points.
1899
1900template<int size, bool big_endian>
1901void
1902Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1903 size_t reloc_count,
1904 const unsigned char* prelocs,
1905 const unsigned char* plocal_syms)
1906{
1907 if (size == 64)
cf43a2fe 1908 {
0e123f69
AM
1909 typedef typename elfcpp::Rela<size, big_endian> Reltype;
1910 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 1911 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
1912 Address expected_off = 0;
1913 bool regular = true;
1914 unsigned int opd_ent_size = 0;
c9269dff
AM
1915
1916 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 1917 {
c9269dff
AM
1918 Reltype reloc(prelocs);
1919 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1920 = reloc.get_r_info();
1921 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1922 if (r_type == elfcpp::R_PPC64_ADDR64)
1923 {
1924 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1925 typename elfcpp::Elf_types<size>::Elf_Addr value;
1926 bool is_ordinary;
1927 unsigned int shndx;
1928 if (r_sym < this->local_symbol_count())
1929 {
1930 typename elfcpp::Sym<size, big_endian>
1931 lsym(plocal_syms + r_sym * sym_size);
1932 shndx = lsym.get_st_shndx();
1933 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1934 value = lsym.get_st_value();
1935 }
1936 else
1937 shndx = this->symbol_section_and_value(r_sym, &value,
1938 &is_ordinary);
1939 this->set_opd_ent(reloc.get_r_offset(), shndx,
1940 value + reloc.get_r_addend());
ec4dbad3
AM
1941 if (i == 2)
1942 {
1943 expected_off = reloc.get_r_offset();
1944 opd_ent_size = expected_off;
1945 }
1946 else if (expected_off != reloc.get_r_offset())
1947 regular = false;
1948 expected_off += opd_ent_size;
1949 }
1950 else if (r_type == elfcpp::R_PPC64_TOC)
1951 {
1952 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1953 regular = false;
1954 }
1955 else
1956 {
1957 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1958 this->name().c_str(), r_type);
1959 regular = false;
c9269dff
AM
1960 }
1961 }
ec4dbad3
AM
1962 if (reloc_count <= 2)
1963 opd_ent_size = this->section_size(this->opd_shndx());
1964 if (opd_ent_size != 24 && opd_ent_size != 16)
1965 regular = false;
1966 if (!regular)
1967 {
1968 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1969 this->name().c_str());
1970 opd_ent_size = 0;
1971 }
c9269dff
AM
1972 }
1973}
1974
5edad15d
AM
1975// Returns true if a code sequence loading the TOC entry at VALUE
1976// relative to the TOC pointer can be converted into code calculating
1977// a TOC pointer relative offset.
1978// If so, the TOC pointer relative offset is stored to VALUE.
1979
1980template<int size, bool big_endian>
1981bool
1982Powerpc_relobj<size, big_endian>::make_toc_relative(
1983 Target_powerpc<size, big_endian>* target,
1984 Address* value)
1985{
1986 if (size != 64)
1987 return false;
1988
e666304e
AM
1989 // With -mcmodel=medium code it is quite possible to have
1990 // toc-relative relocs referring to objects outside the TOC.
1991 // Don't try to look at a non-existent TOC.
1992 if (this->toc_shndx() == 0)
1993 return false;
1994
5edad15d
AM
1995 // Convert VALUE back to an address by adding got_base (see below),
1996 // then to an offset in the TOC by subtracting the TOC output
1997 // section address and the TOC output offset. Since this TOC output
1998 // section and the got output section are one and the same, we can
1999 // omit adding and subtracting the output section address.
2000 Address off = (*value + this->toc_base_offset()
2001 - this->output_section_offset(this->toc_shndx()));
2002 // Is this offset in the TOC? -mcmodel=medium code may be using
2003 // TOC relative access to variables outside the TOC. Those of
2004 // course can't be optimized. We also don't try to optimize code
2005 // that is using a different object's TOC.
2006 if (off >= this->section_size(this->toc_shndx()))
2007 return false;
2008
2009 if (this->no_toc_opt(off))
2010 return false;
2011
2012 section_size_type vlen;
2013 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2014 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2015 // The TOC pointer
2016 Address got_base = (target->got_section()->output_section()->address()
2017 + this->toc_base_offset());
2018 addr -= got_base;
857e829e 2019 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2020 return false;
2021
2022 *value = addr;
2023 return true;
2024}
2025
2026// Perform the Sized_relobj_file method, then set up opd info from
2027// .opd relocs.
2028
c9269dff
AM
2029template<int size, bool big_endian>
2030void
2031Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2032{
2033 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2034 if (size == 64)
2035 {
2036 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2037 p != rd->relocs.end();
2038 ++p)
2039 {
2040 if (p->data_shndx == this->opd_shndx())
2041 {
ec4dbad3
AM
2042 uint64_t opd_size = this->section_size(this->opd_shndx());
2043 gold_assert(opd_size == static_cast<size_t>(opd_size));
2044 if (opd_size != 0)
2045 {
2046 this->init_opd(opd_size);
2047 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2048 rd->local_symbols->data());
2049 }
c9269dff
AM
2050 break;
2051 }
cf43a2fe
AM
2052 }
2053 }
cf43a2fe
AM
2054}
2055
b4f7960d
AM
2056// Read the symbols then set up st_other vector.
2057
2058template<int size, bool big_endian>
2059void
2060Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2061{
f35c4853 2062 this->base_read_symbols(sd);
b4f7960d
AM
2063 if (size == 64)
2064 {
2065 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2066 const unsigned char* const pshdrs = sd->section_headers->data();
2067 const unsigned int loccount = this->do_local_symbol_count();
2068 if (loccount != 0)
2069 {
2070 this->st_other_.resize(loccount);
2071 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2072 off_t locsize = loccount * sym_size;
2073 const unsigned int symtab_shndx = this->symtab_shndx();
2074 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2075 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2076 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2077 locsize, true, false);
2078 psyms += sym_size;
2079 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2080 {
2081 elfcpp::Sym<size, big_endian> sym(psyms);
2082 unsigned char st_other = sym.get_st_other();
2083 this->st_other_[i] = st_other;
2084 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2085 {
2086 if (this->abiversion() == 0)
2087 this->set_abiversion(2);
2088 else if (this->abiversion() < 2)
2089 gold_error(_("%s: local symbol %d has invalid st_other"
2090 " for ABI version 1"),
2091 this->name().c_str(), i);
2092 }
2093 }
2094 }
2095 }
2096}
2097
2098template<int size, bool big_endian>
2099void
2100Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2101{
2102 this->e_flags_ |= ver;
2103 if (this->abiversion() != 0)
2104 {
2105 Target_powerpc<size, big_endian>* target =
2106 static_cast<Target_powerpc<size, big_endian>*>(
2107 parameters->sized_target<size, big_endian>());
2108 if (target->abiversion() == 0)
2109 target->set_abiversion(this->abiversion());
2110 else if (target->abiversion() != this->abiversion())
2111 gold_error(_("%s: ABI version %d is not compatible "
2112 "with ABI version %d output"),
2113 this->name().c_str(),
2114 this->abiversion(), target->abiversion());
2115
2116 }
2117}
2118
f35c4853 2119// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2120// read .opd from a dynamic object, filling in opd_ent_ vector,
2121
2122template<int size, bool big_endian>
2123void
2124Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2125{
f35c4853 2126 this->base_read_symbols(sd);
dc3714f3
AM
2127 if (size == 64)
2128 {
2129 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2130 const unsigned char* const pshdrs = sd->section_headers->data();
2131 const unsigned char* namesu = sd->section_names->data();
2132 const char* names = reinterpret_cast<const char*>(namesu);
2133 const unsigned char* s = NULL;
2134 const unsigned char* opd;
2135 section_size_type opd_size;
2136
2137 // Find and read .opd section.
2138 while (1)
2139 {
2140 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2141 sd->section_names_size,
2142 s);
2143 if (s == NULL)
2144 return;
2145
2146 typename elfcpp::Shdr<size, big_endian> shdr(s);
2147 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2148 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2149 {
b4f7960d
AM
2150 if (this->abiversion() == 0)
2151 this->set_abiversion(1);
2152 else if (this->abiversion() > 1)
2153 gold_error(_("%s: .opd invalid in abiv%d"),
2154 this->name().c_str(), this->abiversion());
2155
dc3714f3
AM
2156 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2157 this->opd_address_ = shdr.get_sh_addr();
2158 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2159 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2160 true, false);
2161 break;
2162 }
2163 }
2164
2165 // Build set of executable sections.
2166 // Using a set is probably overkill. There is likely to be only
2167 // a few executable sections, typically .init, .text and .fini,
2168 // and they are generally grouped together.
2169 typedef std::set<Sec_info> Exec_sections;
2170 Exec_sections exec_sections;
2171 s = pshdrs;
2172 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2173 {
2174 typename elfcpp::Shdr<size, big_endian> shdr(s);
2175 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2176 && ((shdr.get_sh_flags()
2177 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2178 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2179 && shdr.get_sh_size() != 0)
2180 {
2181 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2182 shdr.get_sh_size(), i));
2183 }
2184 }
2185 if (exec_sections.empty())
2186 return;
2187
2188 // Look over the OPD entries. This is complicated by the fact
2189 // that some binaries will use two-word entries while others
2190 // will use the standard three-word entries. In most cases
2191 // the third word (the environment pointer for languages like
2192 // Pascal) is unused and will be zero. If the third word is
2193 // used it should not be pointing into executable sections,
2194 // I think.
2195 this->init_opd(opd_size);
2196 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2197 {
2198 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2199 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2200 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2201 if (val == 0)
2202 // Chances are that this is the third word of an OPD entry.
2203 continue;
2204 typename Exec_sections::const_iterator e
2205 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2206 if (e != exec_sections.begin())
2207 {
2208 --e;
2209 if (e->start <= val && val < e->start + e->len)
2210 {
2211 // We have an address in an executable section.
2212 // VAL ought to be the function entry, set it up.
2213 this->set_opd_ent(p - opd, e->shndx, val);
2214 // Skip second word of OPD entry, the TOC pointer.
2215 p += 8;
2216 }
2217 }
2218 // If we didn't match any executable sections, we likely
2219 // have a non-zero third word in the OPD entry.
2220 }
2221 }
2222}
2223
5edad15d
AM
2224// Relocate sections.
2225
2226template<int size, bool big_endian>
2227void
2228Powerpc_relobj<size, big_endian>::do_relocate_sections(
2229 const Symbol_table* symtab, const Layout* layout,
2230 const unsigned char* pshdrs, Output_file* of,
2231 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2232{
2233 unsigned int start = 1;
2234 if (size == 64
2235 && this->relatoc_ != 0
2236 && !parameters->options().relocatable())
2237 {
2238 // Relocate .toc first.
2239 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2240 this->relatoc_, this->relatoc_);
2241 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2242 1, this->relatoc_ - 1);
2243 start = this->relatoc_ + 1;
2244 }
2245 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2246 start, this->shnum() - 1);
2247}
2248
f43ba157 2249// Set up some symbols.
26a4e9cb
AM
2250
2251template<int size, bool big_endian>
2252void
f43ba157
AM
2253Target_powerpc<size, big_endian>::do_define_standard_symbols(
2254 Symbol_table* symtab,
2255 Layout* layout)
26a4e9cb
AM
2256{
2257 if (size == 32)
2258 {
bb66a627
AM
2259 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2260 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2261 // non-relative dynamic relocs). The proper value will be
2262 // updated later.
2263 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2264 if (gotsym != NULL && gotsym->is_undefined())
2265 {
2266 Target_powerpc<size, big_endian>* target =
2267 static_cast<Target_powerpc<size, big_endian>*>(
2268 parameters->sized_target<size, big_endian>());
2269 Output_data_got_powerpc<size, big_endian>* got
2270 = target->got_section(symtab, layout);
2271 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2272 Symbol_table::PREDEFINED,
2273 got, 0, 0,
2274 elfcpp::STT_OBJECT,
bb66a627 2275 elfcpp::STB_LOCAL,
26a4e9cb
AM
2276 elfcpp::STV_HIDDEN, 0,
2277 false, false);
2278 }
2279
2280 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2281 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2282 if (sdasym != NULL && sdasym->is_undefined())
2283 {
2284 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2285 Output_section* os
2286 = layout->add_output_section_data(".sdata", 0,
2287 elfcpp::SHF_ALLOC
2288 | elfcpp::SHF_WRITE,
2289 sdata, ORDER_SMALL_DATA, false);
2290 symtab->define_in_output_data("_SDA_BASE_", NULL,
2291 Symbol_table::PREDEFINED,
2292 os, 32768, 0, elfcpp::STT_OBJECT,
2293 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2294 0, false, false);
2295 }
2296 }
b4f7960d
AM
2297 else
2298 {
2299 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2300 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2301 if (gotsym != NULL && gotsym->is_undefined())
2302 {
2303 Target_powerpc<size, big_endian>* target =
2304 static_cast<Target_powerpc<size, big_endian>*>(
2305 parameters->sized_target<size, big_endian>());
2306 Output_data_got_powerpc<size, big_endian>* got
2307 = target->got_section(symtab, layout);
2308 symtab->define_in_output_data(".TOC.", NULL,
2309 Symbol_table::PREDEFINED,
2310 got, 0x8000, 0,
2311 elfcpp::STT_OBJECT,
2312 elfcpp::STB_LOCAL,
2313 elfcpp::STV_HIDDEN, 0,
2314 false, false);
2315 }
2316 }
26a4e9cb
AM
2317}
2318
cf43a2fe
AM
2319// Set up PowerPC target specific relobj.
2320
2321template<int size, bool big_endian>
2322Object*
2323Target_powerpc<size, big_endian>::do_make_elf_object(
2324 const std::string& name,
2325 Input_file* input_file,
2326 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2327{
2328 int et = ehdr.get_e_type();
957564c9
AS
2329 // ET_EXEC files are valid input for --just-symbols/-R,
2330 // and we treat them as relocatable objects.
2331 if (et == elfcpp::ET_REL
2332 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2333 {
2334 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2335 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2336 obj->setup();
2337 return obj;
2338 }
2339 else if (et == elfcpp::ET_DYN)
2340 {
dc3714f3
AM
2341 Powerpc_dynobj<size, big_endian>* obj =
2342 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2343 obj->setup();
2344 return obj;
2345 }
2346 else
2347 {
c9269dff 2348 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2349 return NULL;
2350 }
2351}
2352
2353template<int size, bool big_endian>
2354class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2355{
2356public:
2357 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2358 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2359
2360 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2361 : Output_data_got<size, big_endian>(),
2362 symtab_(symtab), layout_(layout),
2363 header_ent_cnt_(size == 32 ? 3 : 1),
2364 header_index_(size == 32 ? 0x2000 : 0)
751e4d66
AM
2365 {
2366 if (size == 64)
2367 this->set_addralign(256);
2368 }
cf43a2fe 2369
e84fe78f
AM
2370 // Override all the Output_data_got methods we use so as to first call
2371 // reserve_ent().
2372 bool
2373 add_global(Symbol* gsym, unsigned int got_type)
2374 {
2375 this->reserve_ent();
2376 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2377 }
2378
2379 bool
2380 add_global_plt(Symbol* gsym, unsigned int got_type)
2381 {
2382 this->reserve_ent();
2383 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2384 }
2385
2386 bool
2387 add_global_tls(Symbol* gsym, unsigned int got_type)
2388 { return this->add_global_plt(gsym, got_type); }
2389
2390 void
2391 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2392 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2393 {
2394 this->reserve_ent();
2395 Output_data_got<size, big_endian>::
2396 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2397 }
2398
2399 void
2400 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2401 Output_data_reloc_generic* rel_dyn,
2402 unsigned int r_type_1, unsigned int r_type_2)
2403 {
2404 this->reserve_ent(2);
2405 Output_data_got<size, big_endian>::
2406 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2407 }
2408
2409 bool
2410 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2411 {
2412 this->reserve_ent();
2413 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2414 got_type);
2415 }
2416
2417 bool
2418 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2419 {
2420 this->reserve_ent();
2421 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2422 got_type);
2423 }
2424
2425 bool
2426 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2427 { return this->add_local_plt(object, sym_index, got_type); }
2428
2429 void
2430 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2431 unsigned int got_type,
2432 Output_data_reloc_generic* rel_dyn,
2433 unsigned int r_type)
2434 {
2435 this->reserve_ent(2);
2436 Output_data_got<size, big_endian>::
2437 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2438 }
2439
2440 unsigned int
2441 add_constant(Valtype constant)
2442 {
2443 this->reserve_ent();
2444 return Output_data_got<size, big_endian>::add_constant(constant);
2445 }
2446
dd93cd0a
AM
2447 unsigned int
2448 add_constant_pair(Valtype c1, Valtype c2)
2449 {
2450 this->reserve_ent(2);
e84fe78f 2451 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
2452 }
2453
2454 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
2455 unsigned int
2456 g_o_t() const
2457 {
2458 return this->got_offset(this->header_index_);
42cacb20 2459 }
cf43a2fe 2460
dd93cd0a
AM
2461 // Offset of base used to access the GOT/TOC.
2462 // The got/toc pointer reg will be set to this value.
26a4e9cb 2463 Valtype
dd93cd0a
AM
2464 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2465 {
2466 if (size == 32)
2467 return this->g_o_t();
2468 else
2469 return (this->output_section()->address()
2470 + object->toc_base_offset()
2471 - this->address());
2472 }
2473
cf43a2fe
AM
2474 // Ensure our GOT has a header.
2475 void
2476 set_final_data_size()
2477 {
2478 if (this->header_ent_cnt_ != 0)
2479 this->make_header();
2480 Output_data_got<size, big_endian>::set_final_data_size();
2481 }
2482
2483 // First word of GOT header needs some values that are not
2484 // handled by Output_data_got so poke them in here.
2485 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2486 void
2487 do_write(Output_file* of)
2488 {
c9824451
AM
2489 Valtype val = 0;
2490 if (size == 32 && this->layout_->dynamic_data() != NULL)
2491 val = this->layout_->dynamic_section()->address();
2492 if (size == 64)
2493 val = this->output_section()->address() + 0x8000;
2494 this->replace_constant(this->header_index_, val);
cf43a2fe
AM
2495 Output_data_got<size, big_endian>::do_write(of);
2496 }
2497
2498private:
2499 void
2500 reserve_ent(unsigned int cnt = 1)
2501 {
2502 if (this->header_ent_cnt_ == 0)
2503 return;
2504 if (this->num_entries() + cnt > this->header_index_)
2505 this->make_header();
2506 }
2507
2508 void
2509 make_header()
2510 {
2511 this->header_ent_cnt_ = 0;
2512 this->header_index_ = this->num_entries();
2513 if (size == 32)
2514 {
2515 Output_data_got<size, big_endian>::add_constant(0);
2516 Output_data_got<size, big_endian>::add_constant(0);
2517 Output_data_got<size, big_endian>::add_constant(0);
2518
2519 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
2520 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2521 if (gotsym != NULL)
2522 {
2523 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2524 sym->set_value(this->g_o_t());
2525 }
2526 else
2527 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2528 Symbol_table::PREDEFINED,
2529 this, this->g_o_t(), 0,
2530 elfcpp::STT_OBJECT,
2531 elfcpp::STB_LOCAL,
2532 elfcpp::STV_HIDDEN, 0,
2533 false, false);
cf43a2fe
AM
2534 }
2535 else
2536 Output_data_got<size, big_endian>::add_constant(0);
2537 }
2538
2539 // Stashed pointers.
2540 Symbol_table* symtab_;
2541 Layout* layout_;
2542
2543 // GOT header size.
2544 unsigned int header_ent_cnt_;
2545 // GOT header index.
2546 unsigned int header_index_;
42cacb20
DE
2547};
2548
2549// Get the GOT section, creating it if necessary.
2550
2551template<int size, bool big_endian>
cf43a2fe 2552Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
2553Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2554 Layout* layout)
2555{
2556 if (this->got_ == NULL)
2557 {
2558 gold_assert(symtab != NULL && layout != NULL);
2559
cf43a2fe
AM
2560 this->got_
2561 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
2562
2563 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2564 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 2565 this->got_, ORDER_DATA, false);
42cacb20
DE
2566 }
2567
2568 return this->got_;
2569}
2570
2571// Get the dynamic reloc section, creating it if necessary.
2572
2573template<int size, bool big_endian>
2574typename Target_powerpc<size, big_endian>::Reloc_section*
2575Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2576{
2577 if (this->rela_dyn_ == NULL)
2578 {
2579 gold_assert(layout != NULL);
2580 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2581 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
2582 elfcpp::SHF_ALLOC, this->rela_dyn_,
2583 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
2584 }
2585 return this->rela_dyn_;
2586}
2587
b3ccdeb5
AM
2588// Similarly, but for ifunc symbols get the one for ifunc.
2589
2590template<int size, bool big_endian>
2591typename Target_powerpc<size, big_endian>::Reloc_section*
2592Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2593 Layout* layout,
2594 bool for_ifunc)
2595{
2596 if (!for_ifunc)
2597 return this->rela_dyn_section(layout);
2598
2599 if (this->iplt_ == NULL)
2600 this->make_iplt_section(symtab, layout);
2601 return this->iplt_->rel_plt();
2602}
2603
ec661b9d
AM
2604class Stub_control
2605{
2606 public:
2607 // Determine the stub group size. The group size is the absolute
2608 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 2609 // is passed a negative value, we restrict stubs to be always after
ec661b9d 2610 // the stubbed branches.
1c3a5fbe
AM
2611 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2612 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2613 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2614 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2615 owner_(NULL), output_section_(NULL)
ec661b9d 2616 {
ec661b9d
AM
2617 }
2618
2619 // Return true iff input section can be handled by current stub
2620 // group.
2621 bool
2622 can_add_to_stub_group(Output_section* o,
2623 const Output_section::Input_section* i,
2624 bool has14);
2625
2626 const Output_section::Input_section*
2627 owner()
2628 { return owner_; }
2629
2630 Output_section*
2631 output_section()
2632 { return output_section_; }
2633
a20605cf
AM
2634 void
2635 set_output_and_owner(Output_section* o,
2636 const Output_section::Input_section* i)
2637 {
2638 this->output_section_ = o;
2639 this->owner_ = i;
2640 }
2641
ec661b9d
AM
2642 private:
2643 typedef enum
2644 {
1c3a5fbe 2645 // Initial state.
ec661b9d 2646 NO_GROUP,
1c3a5fbe 2647 // Adding group sections before the stubs.
ec661b9d 2648 FINDING_STUB_SECTION,
1c3a5fbe 2649 // Adding group sections after the stubs.
ec661b9d
AM
2650 HAS_STUB_SECTION
2651 } State;
2652
ec661b9d 2653 uint32_t stub_group_size_;
a5018ae5 2654 bool stubs_always_after_branch_;
ec661b9d 2655 bool suppress_size_errors_;
1c3a5fbe
AM
2656 // True if a stub group can serve multiple output sections.
2657 bool multi_os_;
2658 State state_;
8a37735f
AM
2659 // Current max size of group. Starts at stub_group_size_ but is
2660 // reduced to stub_group_size_/1024 on seeing a section with
2661 // external conditional branches.
2662 uint32_t group_size_;
a5018ae5 2663 uint64_t group_start_addr_;
57f6d32d
AM
2664 // owner_ and output_section_ specify the section to which stubs are
2665 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
2666 const Output_section::Input_section* owner_;
2667 Output_section* output_section_;
2668};
2669
0cfdc767 2670// Return true iff input section can be handled by current stub
a5018ae5
AM
2671// group. Sections are presented to this function in order,
2672// so the first section is the head of the group.
ec661b9d
AM
2673
2674bool
2675Stub_control::can_add_to_stub_group(Output_section* o,
2676 const Output_section::Input_section* i,
2677 bool has14)
2678{
ec661b9d
AM
2679 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2680 uint64_t this_size;
2681 uint64_t start_addr = o->address();
2682
2683 if (whole_sec)
2684 // .init and .fini sections are pasted together to form a single
2685 // function. We can't be adding stubs in the middle of the function.
2686 this_size = o->data_size();
2687 else
2688 {
2689 start_addr += i->relobj()->output_section_offset(i->shndx());
2690 this_size = i->data_size();
2691 }
57f6d32d 2692
a5018ae5 2693 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
2694 uint32_t group_size = this->stub_group_size_;
2695 if (has14)
2696 this->group_size_ = group_size = group_size >> 10;
ec661b9d 2697
57f6d32d 2698 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
2699 gold_warning(_("%s:%s exceeds group size"),
2700 i->relobj()->name().c_str(),
2701 i->relobj()->section_name(i->shndx()).c_str());
2702
afe002dd
AM
2703 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2704 has14 ? " 14bit" : "",
2705 i->relobj()->name().c_str(),
2706 i->relobj()->section_name(i->shndx()).c_str(),
2707 (long long) this_size,
a5018ae5
AM
2708 (this->state_ == NO_GROUP
2709 ? this_size
2710 : (long long) end_addr - this->group_start_addr_));
afe002dd 2711
1c3a5fbe
AM
2712 if (this->state_ == NO_GROUP)
2713 {
2714 // Only here on very first use of Stub_control
2715 this->owner_ = i;
2716 this->output_section_ = o;
2717 this->state_ = FINDING_STUB_SECTION;
2718 this->group_size_ = group_size;
2719 this->group_start_addr_ = start_addr;
2720 return true;
2721 }
2722 else if (!this->multi_os_ && this->output_section_ != o)
2723 ;
2724 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 2725 {
a5018ae5 2726 // Can we add this section, which is after the stubs, to the
57f6d32d 2727 // group?
a5018ae5 2728 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2729 return true;
ec661b9d 2730 }
a5018ae5 2731 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 2732 {
a5018ae5
AM
2733 if ((whole_sec && this->output_section_ == o)
2734 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2735 {
a5018ae5 2736 // Stubs are added at the end of "owner_".
57f6d32d
AM
2737 this->owner_ = i;
2738 this->output_section_ = o;
a5018ae5 2739 return true;
57f6d32d 2740 }
a5018ae5
AM
2741 // The group before the stubs has reached maximum size.
2742 // Now see about adding sections after the stubs to the
2743 // group. If the current section has a 14-bit branch and
2744 // the group before the stubs exceeds group_size_ (because
2745 // they didn't have 14-bit branches), don't add sections
2746 // after the stubs: The size of stubs for such a large
2747 // group may exceed the reach of a 14-bit branch.
2748 if (!this->stubs_always_after_branch_
2749 && this_size <= this->group_size_
2750 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2751 {
a5018ae5
AM
2752 gold_debug(DEBUG_TARGET, "adding after stubs");
2753 this->state_ = HAS_STUB_SECTION;
2754 this->group_start_addr_ = start_addr;
57f6d32d
AM
2755 return true;
2756 }
ec661b9d 2757 }
a5018ae5
AM
2758 else
2759 gold_unreachable();
57f6d32d 2760
1c3a5fbe
AM
2761 gold_debug(DEBUG_TARGET,
2762 !this->multi_os_ && this->output_section_ != o
2763 ? "nope, new output section\n"
2764 : "nope, didn't fit\n");
afe002dd 2765
57f6d32d
AM
2766 // The section fails to fit in the current group. Set up a few
2767 // things for the next group. owner_ and output_section_ will be
2768 // set later after we've retrieved those values for the current
2769 // group.
2770 this->state_ = FINDING_STUB_SECTION;
8a37735f 2771 this->group_size_ = group_size;
a5018ae5 2772 this->group_start_addr_ = start_addr;
57f6d32d 2773 return false;
ec661b9d
AM
2774}
2775
2776// Look over all the input sections, deciding where to place stubs.
2777
2778template<int size, bool big_endian>
2779void
2780Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
2781 const Task*,
2782 bool no_size_errors)
ec661b9d 2783{
1c3a5fbe
AM
2784 Stub_control stub_control(this->stub_group_size_, no_size_errors,
2785 parameters->options().stub_group_multi());
ec661b9d
AM
2786
2787 // Group input sections and insert stub table
a3e60ddb
AM
2788 Stub_table_owner* table_owner = NULL;
2789 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
2790 Layout::Section_list section_list;
2791 layout->get_executable_sections(&section_list);
2792 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
2793 for (Layout::Section_list::iterator o = section_list.begin();
2794 o != section_list.end();
ec661b9d
AM
2795 ++o)
2796 {
2797 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
2798 for (Input_section_list::const_iterator i
2799 = (*o)->input_sections().begin();
2800 i != (*o)->input_sections().end();
ec661b9d
AM
2801 ++i)
2802 {
a3e60ddb
AM
2803 if (i->is_input_section()
2804 || i->is_relaxed_input_section())
ec661b9d
AM
2805 {
2806 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2807 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2808 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2809 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2810 {
a3e60ddb
AM
2811 table_owner->output_section = stub_control.output_section();
2812 table_owner->owner = stub_control.owner();
a20605cf 2813 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 2814 table_owner = NULL;
ec661b9d 2815 }
a3e60ddb
AM
2816 if (table_owner == NULL)
2817 {
2818 table_owner = new Stub_table_owner;
2819 tables.push_back(table_owner);
2820 }
2821 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
2822 }
2823 }
2824 }
a3e60ddb 2825 if (table_owner != NULL)
0cfdc767 2826 {
a5018ae5
AM
2827 table_owner->output_section = stub_control.output_section();
2828 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
2829 }
2830 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
2831 t != tables.end();
2832 ++t)
2833 {
2834 Stub_table<size, big_endian>* stub_table;
2835
2836 if ((*t)->owner->is_input_section())
2837 stub_table = new Stub_table<size, big_endian>(this,
2838 (*t)->output_section,
2839 (*t)->owner);
2840 else if ((*t)->owner->is_relaxed_input_section())
2841 stub_table = static_cast<Stub_table<size, big_endian>*>(
2842 (*t)->owner->relaxed_input_section());
0cfdc767 2843 else
a3e60ddb
AM
2844 gold_unreachable();
2845 this->stub_tables_.push_back(stub_table);
2846 delete *t;
0cfdc767 2847 }
ec661b9d
AM
2848}
2849
a3e60ddb
AM
2850static unsigned long
2851max_branch_delta (unsigned int r_type)
2852{
2853 if (r_type == elfcpp::R_POWERPC_REL14
2854 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
2855 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2856 return 1L << 15;
2857 if (r_type == elfcpp::R_POWERPC_REL24
2858 || r_type == elfcpp::R_PPC_PLTREL24
2859 || r_type == elfcpp::R_PPC_LOCAL24PC)
2860 return 1L << 25;
2861 return 0;
2862}
2863
ec661b9d
AM
2864// If this branch needs a plt call stub, or a long branch stub, make one.
2865
2866template<int size, bool big_endian>
a3e60ddb 2867bool
ec661b9d
AM
2868Target_powerpc<size, big_endian>::Branch_info::make_stub(
2869 Stub_table<size, big_endian>* stub_table,
2870 Stub_table<size, big_endian>* ifunc_stub_table,
2871 Symbol_table* symtab) const
2872{
2873 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2874 if (sym != NULL && sym->is_forwarder())
2875 sym = symtab->resolve_forwards(sym);
2876 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
88b8e639
AM
2877 Target_powerpc<size, big_endian>* target =
2878 static_cast<Target_powerpc<size, big_endian>*>(
2879 parameters->sized_target<size, big_endian>());
dc60b26d
AM
2880 bool ok = true;
2881
ec661b9d 2882 if (gsym != NULL
88b8e639 2883 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
2884 : this->object_->local_has_plt_offset(this->r_sym_))
2885 {
9055360d
AM
2886 if (size == 64
2887 && gsym != NULL
2888 && target->abiversion() >= 2
2889 && !parameters->options().output_is_position_independent()
2890 && !is_branch_reloc(this->r_type_))
2891 target->glink_section()->add_global_entry(gsym);
2892 else
ec661b9d 2893 {
9055360d
AM
2894 if (stub_table == NULL)
2895 stub_table = this->object_->stub_table(this->shndx_);
2896 if (stub_table == NULL)
2897 {
2898 // This is a ref from a data section to an ifunc symbol.
2899 stub_table = ifunc_stub_table;
2900 }
2901 gold_assert(stub_table != NULL);
a3e60ddb
AM
2902 Address from = this->object_->get_output_section_offset(this->shndx_);
2903 if (from != invalid_address)
2904 from += (this->object_->output_section(this->shndx_)->address()
2905 + this->offset_);
9055360d 2906 if (gsym != NULL)
dc60b26d
AM
2907 ok = stub_table->add_plt_call_entry(from,
2908 this->object_, gsym,
2909 this->r_type_, this->addend_);
9055360d 2910 else
dc60b26d
AM
2911 ok = stub_table->add_plt_call_entry(from,
2912 this->object_, this->r_sym_,
2913 this->r_type_, this->addend_);
ec661b9d 2914 }
ec661b9d
AM
2915 }
2916 else
2917 {
cbcb23fa 2918 Address max_branch_offset = max_branch_delta(this->r_type_);
a3e60ddb
AM
2919 if (max_branch_offset == 0)
2920 return true;
ec661b9d
AM
2921 Address from = this->object_->get_output_section_offset(this->shndx_);
2922 gold_assert(from != invalid_address);
2923 from += (this->object_->output_section(this->shndx_)->address()
2924 + this->offset_);
2925 Address to;
2926 if (gsym != NULL)
2927 {
2928 switch (gsym->source())
2929 {
2930 case Symbol::FROM_OBJECT:
2931 {
2932 Object* symobj = gsym->object();
2933 if (symobj->is_dynamic()
2934 || symobj->pluginobj() != NULL)
a3e60ddb 2935 return true;
ec661b9d
AM
2936 bool is_ordinary;
2937 unsigned int shndx = gsym->shndx(&is_ordinary);
2938 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 2939 return true;
ec661b9d
AM
2940 }
2941 break;
2942
2943 case Symbol::IS_UNDEFINED:
a3e60ddb 2944 return true;
ec661b9d
AM
2945
2946 default:
2947 break;
2948 }
2949 Symbol_table::Compute_final_value_status status;
2950 to = symtab->compute_final_value<size>(gsym, &status);
2951 if (status != Symbol_table::CFVS_OK)
a3e60ddb 2952 return true;
9055360d
AM
2953 if (size == 64)
2954 to += this->object_->ppc64_local_entry_offset(gsym);
ec661b9d
AM
2955 }
2956 else
2957 {
2958 const Symbol_value<size>* psymval
2959 = this->object_->local_symbol(this->r_sym_);
2960 Symbol_value<size> symval;
0f125432
CC
2961 if (psymval->is_section_symbol())
2962 symval.set_is_section_symbol();
ec661b9d
AM
2963 typedef Sized_relobj_file<size, big_endian> ObjType;
2964 typename ObjType::Compute_final_local_value_status status
2965 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2966 &symval, symtab);
2967 if (status != ObjType::CFLV_OK
2968 || !symval.has_output_value())
a3e60ddb 2969 return true;
ec661b9d 2970 to = symval.value(this->object_, 0);
9055360d
AM
2971 if (size == 64)
2972 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
ec661b9d 2973 }
cbcb23fa
AM
2974 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
2975 to += this->addend_;
ec661b9d
AM
2976 if (stub_table == NULL)
2977 stub_table = this->object_->stub_table(this->shndx_);
9055360d 2978 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
2979 {
2980 unsigned int dest_shndx;
1611bc4a
AM
2981 if (!target->symval_for_branch(symtab, gsym, this->object_,
2982 &to, &dest_shndx))
2983 return true;
ec661b9d
AM
2984 }
2985 Address delta = to - from;
2986 if (delta + max_branch_offset >= 2 * max_branch_offset)
2987 {
0cfdc767
AM
2988 if (stub_table == NULL)
2989 {
2990 gold_warning(_("%s:%s: branch in non-executable section,"
2991 " no long branch stub for you"),
2992 this->object_->name().c_str(),
2993 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 2994 return true;
0cfdc767 2995 }
d49044c7
AM
2996 bool save_res = (size == 64
2997 && gsym != NULL
2998 && gsym->source() == Symbol::IN_OUTPUT_DATA
2999 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3000 ok = stub_table->add_long_branch_entry(this->object_,
3001 this->r_type_,
3002 from, to, save_res);
ec661b9d
AM
3003 }
3004 }
dc60b26d
AM
3005 if (!ok)
3006 gold_debug(DEBUG_TARGET,
3007 "branch at %s:%s+%#lx\n"
3008 "can't reach stub attached to %s:%s",
3009 this->object_->name().c_str(),
3010 this->object_->section_name(this->shndx_).c_str(),
3011 (unsigned long) this->offset_,
3012 stub_table->relobj()->name().c_str(),
3013 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3014
3015 return ok;
ec661b9d
AM
3016}
3017
3018// Relaxation hook. This is where we do stub generation.
3019
3020template<int size, bool big_endian>
3021bool
3022Target_powerpc<size, big_endian>::do_relax(int pass,
3023 const Input_objects*,
3024 Symbol_table* symtab,
3025 Layout* layout,
3026 const Task* task)
3027{
3028 unsigned int prev_brlt_size = 0;
3029 if (pass == 1)
ec661b9d 3030 {
b4f7960d
AM
3031 bool thread_safe
3032 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3033 if (size == 64
3034 && this->abiversion() < 2
3035 && !thread_safe
3036 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3037 {
e2458743 3038 static const char* const thread_starter[] =
9e69ed50
AM
3039 {
3040 "pthread_create",
3041 /* libstdc++ */
3042 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3043 /* librt */
3044 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3045 "mq_notify", "create_timer",
3046 /* libanl */
3047 "getaddrinfo_a",
3048 /* libgomp */
80272b8c 3049 "GOMP_parallel",
9e69ed50 3050 "GOMP_parallel_start",
80272b8c 3051 "GOMP_parallel_loop_static",
9e69ed50 3052 "GOMP_parallel_loop_static_start",
80272b8c 3053 "GOMP_parallel_loop_dynamic",
9e69ed50 3054 "GOMP_parallel_loop_dynamic_start",
80272b8c 3055 "GOMP_parallel_loop_guided",
9e69ed50 3056 "GOMP_parallel_loop_guided_start",
80272b8c 3057 "GOMP_parallel_loop_runtime",
9e69ed50 3058 "GOMP_parallel_loop_runtime_start",
80272b8c 3059 "GOMP_parallel_sections",
43819297 3060 "GOMP_parallel_sections_start",
f9dffbf0
AM
3061 /* libgo */
3062 "__go_go",
9e69ed50
AM
3063 };
3064
e2458743
AM
3065 if (parameters->options().shared())
3066 thread_safe = true;
3067 else
9e69ed50 3068 {
e2458743
AM
3069 for (unsigned int i = 0;
3070 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3071 i++)
3072 {
3073 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3074 thread_safe = (sym != NULL
3075 && sym->in_reg()
3076 && sym->in_real_elf());
3077 if (thread_safe)
3078 break;
3079 }
9e69ed50 3080 }
ec661b9d 3081 }
9e69ed50 3082 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3083 }
3084
3085 if (pass == 1)
3086 {
3087 this->stub_group_size_ = parameters->options().stub_group_size();
3088 bool no_size_errors = true;
3089 if (this->stub_group_size_ == 1)
3090 this->stub_group_size_ = 0x1c00000;
3091 else if (this->stub_group_size_ == -1)
3092 this->stub_group_size_ = -0x1e00000;
3093 else
3094 no_size_errors = false;
3095 this->group_sections(layout, task, no_size_errors);
3096 }
3097 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3098 {
3099 this->branch_lookup_table_.clear();
3100 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3101 p != this->stub_tables_.end();
3102 ++p)
3103 {
3104 (*p)->clear_stubs(true);
3105 }
3106 this->stub_tables_.clear();
3107 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3108 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3109 program_name, this->stub_group_size_);
3110 this->group_sections(layout, task, true);
ec661b9d
AM
3111 }
3112
3113 // We need address of stub tables valid for make_stub.
3114 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3115 p != this->stub_tables_.end();
3116 ++p)
3117 {
3118 const Powerpc_relobj<size, big_endian>* object
3119 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3120 Address off = object->get_output_section_offset((*p)->shndx());
3121 gold_assert(off != invalid_address);
3122 Output_section* os = (*p)->output_section();
3123 (*p)->set_address_and_size(os, off);
3124 }
3125
9e69ed50
AM
3126 if (pass != 1)
3127 {
3128 // Clear plt call stubs, long branch stubs and branch lookup table.
3129 prev_brlt_size = this->branch_lookup_table_.size();
3130 this->branch_lookup_table_.clear();
3131 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3132 p != this->stub_tables_.end();
3133 ++p)
3134 {
a3e60ddb 3135 (*p)->clear_stubs(false);
9e69ed50
AM
3136 }
3137 }
3138
3139 // Build all the stubs.
a3e60ddb 3140 this->relax_failed_ = false;
ec661b9d
AM
3141 Stub_table<size, big_endian>* ifunc_stub_table
3142 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3143 Stub_table<size, big_endian>* one_stub_table
3144 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3145 for (typename Branches::const_iterator b = this->branch_info_.begin();
3146 b != this->branch_info_.end();
3147 b++)
3148 {
a3e60ddb
AM
3149 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3150 && !this->relax_failed_)
3151 {
3152 this->relax_failed_ = true;
3153 this->relax_fail_count_++;
3154 if (this->relax_fail_count_ < 3)
3155 return true;
3156 }
ec661b9d
AM
3157 }
3158
9e69ed50 3159 // Did anything change size?
ec661b9d
AM
3160 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3161 bool again = num_huge_branches != prev_brlt_size;
3162 if (size == 64 && num_huge_branches != 0)
3163 this->make_brlt_section(layout);
3164 if (size == 64 && again)
3165 this->brlt_section_->set_current_size(num_huge_branches);
3166
3167 typedef Unordered_set<Output_section*> Output_sections;
3168 Output_sections os_need_update;
3169 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3170 p != this->stub_tables_.end();
3171 ++p)
3172 {
3173 if ((*p)->size_update())
3174 {
3175 again = true;
9d5781f8 3176 (*p)->add_eh_frame(layout);
ec661b9d
AM
3177 os_need_update.insert((*p)->output_section());
3178 }
3179 }
3180
9e69ed50
AM
3181 // Set output section offsets for all input sections in an output
3182 // section that just changed size. Anything past the stubs will
3183 // need updating.
ec661b9d
AM
3184 for (typename Output_sections::iterator p = os_need_update.begin();
3185 p != os_need_update.end();
3186 p++)
3187 {
3188 Output_section* os = *p;
3189 Address off = 0;
3190 typedef Output_section::Input_section_list Input_section_list;
3191 for (Input_section_list::const_iterator i = os->input_sections().begin();
3192 i != os->input_sections().end();
3193 ++i)
3194 {
3195 off = align_address(off, i->addralign());
3196 if (i->is_input_section() || i->is_relaxed_input_section())
3197 i->relobj()->set_section_offset(i->shndx(), off);
3198 if (i->is_relaxed_input_section())
3199 {
3200 Stub_table<size, big_endian>* stub_table
3201 = static_cast<Stub_table<size, big_endian>*>(
3202 i->relaxed_input_section());
6395d38b
HS
3203 Address stub_table_size = stub_table->set_address_and_size(os, off);
3204 off += stub_table_size;
3205 // After a few iterations, set current stub table size
3206 // as min size threshold, so later stub tables can only
3207 // grow in size.
3208 if (pass >= 4)
3209 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3210 }
3211 else
3212 off += i->data_size();
3213 }
6830ee24
AM
3214 // If .branch_lt is part of this output section, then we have
3215 // just done the offset adjustment.
ec661b9d
AM
3216 os->clear_section_offsets_need_adjustment();
3217 }
3218
3219 if (size == 64
3220 && !again
3221 && num_huge_branches != 0
3222 && parameters->options().output_is_position_independent())
3223 {
3224 // Fill in the BRLT relocs.
06f30c9d 3225 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3226 for (typename Branch_lookup_table::const_iterator p
3227 = this->branch_lookup_table_.begin();
3228 p != this->branch_lookup_table_.end();
3229 ++p)
3230 {
3231 this->brlt_section_->add_reloc(p->first, p->second);
3232 }
06f30c9d 3233 this->brlt_section_->finalize_brlt_sizes();
ec661b9d
AM
3234 }
3235 return again;
3236}
3237
9d5781f8
AM
3238template<int size, bool big_endian>
3239void
3240Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3241 unsigned char* oview,
3242 uint64_t* paddress,
3243 off_t* plen) const
3244{
3245 uint64_t address = plt->address();
3246 off_t len = plt->data_size();
3247
3248 if (plt == this->glink_)
3249 {
3250 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3251 if (len == 0)
3252 {
3253 gold_assert(parameters->doing_static_link());
3254 // Static linking may need stubs, to support ifunc and long
3255 // branches. We need to create an output section for
3256 // .eh_frame early in the link process, to have a place to
3257 // attach stub .eh_frame info. We also need to have
3258 // registered a CIE that matches the stub CIE. Both of
3259 // these requirements are satisfied by creating an FDE and
3260 // CIE for .glink, even though static linking will leave
3261 // .glink zero length.
3262 // ??? Hopefully generating an FDE with a zero address range
3263 // won't confuse anything that consumes .eh_frame info.
3264 }
3265 else if (size == 64)
9d5781f8
AM
3266 {
3267 // There is one word before __glink_PLTresolve
3268 address += 8;
3269 len -= 8;
3270 }
3271 else if (parameters->options().output_is_position_independent())
3272 {
3273 // There are two FDEs for a position independent glink.
3274 // The first covers the branch table, the second
3275 // __glink_PLTresolve at the end of glink.
3276 off_t resolve_size = this->glink_->pltresolve_size;
5fe7ffdc 3277 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3278 len -= resolve_size;
3279 else
3280 {
3281 address += len - resolve_size;
3282 len = resolve_size;
3283 }
3284 }
3285 }
3286 else
3287 {
3288 // Must be a stub table.
3289 const Stub_table<size, big_endian>* stub_table
3290 = static_cast<const Stub_table<size, big_endian>*>(plt);
3291 uint64_t stub_address = stub_table->stub_address();
3292 len -= stub_address - address;
3293 address = stub_address;
3294 }
3295
3296 *paddress = address;
3297 *plen = len;
3298}
3299
42cacb20
DE
3300// A class to handle the PLT data.
3301
3302template<int size, bool big_endian>
cf43a2fe 3303class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3304{
3305 public:
3306 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3307 size, big_endian> Reloc_section;
3308
e5d5f5ed
AM
3309 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3310 Reloc_section* plt_rel,
e5d5f5ed
AM
3311 const char* name)
3312 : Output_section_data_build(size == 32 ? 4 : 8),
3313 rel_(plt_rel),
3314 targ_(targ),
e5d5f5ed
AM
3315 name_(name)
3316 { }
42cacb20
DE
3317
3318 // Add an entry to the PLT.
03e25981 3319 void
cf43a2fe 3320 add_entry(Symbol*);
42cacb20 3321
03e25981 3322 void
e5d5f5ed
AM
3323 add_ifunc_entry(Symbol*);
3324
03e25981 3325 void
e5d5f5ed
AM
3326 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3327
42cacb20 3328 // Return the .rela.plt section data.
e5d5f5ed 3329 Reloc_section*
cf43a2fe
AM
3330 rel_plt() const
3331 {
42cacb20
DE
3332 return this->rel_;
3333 }
3334
0e70b911
CC
3335 // Return the number of PLT entries.
3336 unsigned int
3337 entry_count() const
d83ce4e3 3338 {
b3ccdeb5
AM
3339 if (this->current_data_size() == 0)
3340 return 0;
b4f7960d
AM
3341 return ((this->current_data_size() - this->first_plt_entry_offset())
3342 / this->plt_entry_size());
d83ce4e3 3343 }
0e70b911 3344
42cacb20 3345 protected:
42cacb20 3346 void
cf43a2fe 3347 do_adjust_output_section(Output_section* os)
42cacb20 3348 {
cf43a2fe 3349 os->set_entsize(0);
42cacb20
DE
3350 }
3351
6ce78956
AM
3352 // Write to a map file.
3353 void
3354 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 3355 { mapfile->print_output_data(this, this->name_); }
6ce78956 3356
cf43a2fe 3357 private:
b4f7960d
AM
3358 // Return the offset of the first non-reserved PLT entry.
3359 unsigned int
3360 first_plt_entry_offset() const
3361 {
3362 // IPLT has no reserved entry.
3363 if (this->name_[3] == 'I')
3364 return 0;
3365 return this->targ_->first_plt_entry_offset();
3366 }
3367
3368 // Return the size of each PLT entry.
3369 unsigned int
3370 plt_entry_size() const
3371 {
3372 return this->targ_->plt_entry_size();
3373 }
cf43a2fe 3374
42cacb20
DE
3375 // Write out the PLT data.
3376 void
3377 do_write(Output_file*);
3378
3379 // The reloc section.
3380 Reloc_section* rel_;
cf43a2fe
AM
3381 // Allows access to .glink for do_write.
3382 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
3383 // What to report in map file.
3384 const char *name_;
42cacb20
DE
3385};
3386
e5d5f5ed 3387// Add an entry to the PLT.
42cacb20
DE
3388
3389template<int size, bool big_endian>
03e25981 3390void
e5d5f5ed 3391Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 3392{
e5d5f5ed
AM
3393 if (!gsym->has_plt_offset())
3394 {
ec661b9d 3395 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3396 if (off == 0)
3397 off += this->first_plt_entry_offset();
3398 gsym->set_plt_offset(off);
3399 gsym->set_needs_dynsym_entry();
3400 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3401 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 3402 off += this->plt_entry_size();
e5d5f5ed
AM
3403 this->set_current_data_size(off);
3404 }
42cacb20
DE
3405}
3406
e5d5f5ed 3407// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
3408
3409template<int size, bool big_endian>
03e25981 3410void
e5d5f5ed 3411Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 3412{
cf43a2fe
AM
3413 if (!gsym->has_plt_offset())
3414 {
ec661b9d 3415 section_size_type off = this->current_data_size();
cf43a2fe 3416 gsym->set_plt_offset(off);
e5d5f5ed 3417 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3418 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3419 dynrel = elfcpp::R_PPC64_JMP_IREL;
3420 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 3421 off += this->plt_entry_size();
e5d5f5ed
AM
3422 this->set_current_data_size(off);
3423 }
3424}
3425
3426// Add an entry for a local ifunc symbol to the IPLT.
3427
3428template<int size, bool big_endian>
03e25981 3429void
e5d5f5ed
AM
3430Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3431 Sized_relobj_file<size, big_endian>* relobj,
3432 unsigned int local_sym_index)
3433{
3434 if (!relobj->local_has_plt_offset(local_sym_index))
3435 {
ec661b9d 3436 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3437 relobj->set_local_plt_offset(local_sym_index, off);
3438 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3439 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3440 dynrel = elfcpp::R_PPC64_JMP_IREL;
3441 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3442 this, off, 0);
b4f7960d 3443 off += this->plt_entry_size();
cf43a2fe
AM
3444 this->set_current_data_size(off);
3445 }
42cacb20
DE
3446}
3447
dd93cd0a 3448static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 3449static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 3450static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
3451static const uint32_t add_3_3_2 = 0x7c631214;
3452static const uint32_t add_3_3_13 = 0x7c636a14;
3453static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
3454static const uint32_t add_11_2_11 = 0x7d625a14;
3455static const uint32_t add_11_11_2 = 0x7d6b1214;
3456static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 3457static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 3458static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 3459static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 3460static const uint32_t addi_12_1 = 0x39810000;
b4f7960d 3461static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
3462static const uint32_t addis_0_2 = 0x3c020000;
3463static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 3464static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 3465static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
3466static const uint32_t addis_11_11 = 0x3d6b0000;
3467static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 3468static const uint32_t addis_12_1 = 0x3d810000;
397998fc 3469static const uint32_t addis_12_2 = 0x3d820000;
c9269dff 3470static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
3471static const uint32_t b = 0x48000000;
3472static const uint32_t bcl_20_31 = 0x429f0005;
3473static const uint32_t bctr = 0x4e800420;
f3a0ed29 3474static const uint32_t blr = 0x4e800020;
9e69ed50 3475static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 3476static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 3477static const uint32_t cmpldi_2_0 = 0x28220000;
dd93cd0a
AM
3478static const uint32_t cror_15_15_15 = 0x4def7b82;
3479static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29
AM
3480static const uint32_t ld_0_1 = 0xe8010000;
3481static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 3482static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 3483static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 3484static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 3485static const uint32_t ld_2_12 = 0xe84c0000;
b4f7960d
AM
3486static const uint32_t ld_11_2 = 0xe9620000;
3487static const uint32_t ld_11_11 = 0xe96b0000;
3488static const uint32_t ld_12_2 = 0xe9820000;
3489static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 3490static const uint32_t ld_12_12 = 0xe98c0000;
f3a0ed29 3491static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 3492static const uint32_t li_0_0 = 0x38000000;
f3a0ed29 3493static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 3494static const uint32_t lis_0 = 0x3c000000;
549dba71 3495static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
3496static const uint32_t lis_11 = 0x3d600000;
3497static const uint32_t lis_12 = 0x3d800000;
b4f7960d 3498static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff
AM
3499static const uint32_t lwz_0_12 = 0x800c0000;
3500static const uint32_t lwz_11_11 = 0x816b0000;
3501static const uint32_t lwz_11_30 = 0x817e0000;
3502static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 3503static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 3504static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 3505static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff
AM
3506static const uint32_t mflr_12 = 0x7d8802a6;
3507static const uint32_t mtctr_0 = 0x7c0903a6;
3508static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 3509static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 3510static const uint32_t mtlr_0 = 0x7c0803a6;
c9269dff 3511static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 3512static const uint32_t nop = 0x60000000;
c9269dff 3513static const uint32_t ori_0_0_0 = 0x60000000;
b4f7960d 3514static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
3515static const uint32_t std_0_1 = 0xf8010000;
3516static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 3517static const uint32_t std_2_1 = 0xf8410000;
f3a0ed29
AM
3518static const uint32_t stfd_0_1 = 0xd8010000;
3519static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 3520static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
3521static const uint32_t sub_12_12_11 = 0x7d8b6050;
3522static const uint32_t xor_2_12_12 = 0x7d826278;
3523static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20
DE
3524
3525// Write out the PLT.
3526
3527template<int size, bool big_endian>
3528void
3529Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3530{
b3ccdeb5 3531 if (size == 32 && this->name_[3] != 'I')
cf43a2fe 3532 {
ec661b9d 3533 const section_size_type offset = this->offset();
cf43a2fe
AM
3534 const section_size_type oview_size
3535 = convert_to_section_size_type(this->data_size());
3536 unsigned char* const oview = of->get_output_view(offset, oview_size);
3537 unsigned char* pov = oview;
3538 unsigned char* endpov = oview + oview_size;
3539
e5d5f5ed 3540 // The address of the .glink branch table
cf43a2fe
AM
3541 const Output_data_glink<size, big_endian>* glink
3542 = this->targ_->glink_section();
ec661b9d 3543 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
3544
3545 while (pov < endpov)
3546 {
3547 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3548 pov += 4;
3549 branch_tab += 4;
3550 }
3551
3552 of->write_output_view(offset, oview_size, oview);
3553 }
3554}
3555
3556// Create the PLT section.
3557
3558template<int size, bool big_endian>
3559void
40b469d7
AM
3560Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3561 Layout* layout)
cf43a2fe
AM
3562{
3563 if (this->plt_ == NULL)
3564 {
40b469d7
AM
3565 if (this->got_ == NULL)
3566 this->got_section(symtab, layout);
3567
cf43a2fe
AM
3568 if (this->glink_ == NULL)
3569 make_glink_section(layout);
3570
3571 // Ensure that .rela.dyn always appears before .rela.plt This is
3572 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 3573 // needs to include .rela.plt in its range.
cf43a2fe
AM
3574 this->rela_dyn_section(layout);
3575
e5d5f5ed
AM
3576 Reloc_section* plt_rel = new Reloc_section(false);
3577 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3578 elfcpp::SHF_ALLOC, plt_rel,
3579 ORDER_DYNAMIC_PLT_RELOCS, false);
3580 this->plt_
3581 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 3582 "** PLT");
cf43a2fe
AM
3583 layout->add_output_section_data(".plt",
3584 (size == 32
3585 ? elfcpp::SHT_PROGBITS
3586 : elfcpp::SHT_NOBITS),
3587 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3588 this->plt_,
3589 (size == 32
3590 ? ORDER_SMALL_DATA
3591 : ORDER_SMALL_BSS),
3592 false);
3254d32c
AM
3593
3594 Output_section* rela_plt_os = plt_rel->output_section();
3595 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
3596 }
3597}
3598
e5d5f5ed
AM
3599// Create the IPLT section.
3600
3601template<int size, bool big_endian>
3602void
40b469d7
AM
3603Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3604 Layout* layout)
e5d5f5ed
AM
3605{
3606 if (this->iplt_ == NULL)
3607 {
40b469d7 3608 this->make_plt_section(symtab, layout);
e5d5f5ed
AM
3609
3610 Reloc_section* iplt_rel = new Reloc_section(false);
3611 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3612 this->iplt_
3613 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 3614 "** IPLT");
e5d5f5ed 3615 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
3616 }
3617}
3618
ec661b9d 3619// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
3620
3621template<int size, bool big_endian>
ec661b9d 3622class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
3623{
3624 public:
ec661b9d
AM
3625 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3626 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3627 size, big_endian> Reloc_section;
c9269dff 3628
ec661b9d
AM
3629 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3630 Reloc_section* brlt_rel)
3631 : Output_section_data_build(size == 32 ? 4 : 8),
3632 rel_(brlt_rel),
3633 targ_(targ)
3634 { }
cf43a2fe 3635
06f30c9d
CC
3636 void
3637 reset_brlt_sizes()
3638 {
3639 this->reset_data_size();
3640 this->rel_->reset_data_size();
3641 }
3642
3643 void
3644 finalize_brlt_sizes()
3645 {
3646 this->finalize_data_size();
3647 this->rel_->finalize_data_size();
3648 }
3649
ec661b9d 3650 // Add a reloc for an entry in the BRLT.
cf43a2fe 3651 void
ec661b9d
AM
3652 add_reloc(Address to, unsigned int off)
3653 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 3654
ec661b9d 3655 // Update section and reloc section size.
e5d5f5ed 3656 void
ec661b9d
AM
3657 set_current_size(unsigned int num_branches)
3658 {
3659 this->reset_address_and_file_offset();
3660 this->set_current_data_size(num_branches * 16);
3661 this->finalize_data_size();
3662 Output_section* os = this->output_section();
3663 os->set_section_offsets_need_adjustment();
3664 if (this->rel_ != NULL)
3665 {
0e123f69 3666 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
3667 this->rel_->reset_address_and_file_offset();
3668 this->rel_->set_current_data_size(num_branches * reloc_size);
3669 this->rel_->finalize_data_size();
3670 Output_section* os = this->rel_->output_section();
3671 os->set_section_offsets_need_adjustment();
3672 }
3673 }
cf43a2fe 3674
ec661b9d
AM
3675 protected:
3676 void
3677 do_adjust_output_section(Output_section* os)
3678 {
3679 os->set_entsize(0);
3680 }
e5d5f5ed 3681
ec661b9d
AM
3682 // Write to a map file.
3683 void
3684 do_print_to_mapfile(Mapfile* mapfile) const
3685 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 3686
ec661b9d
AM
3687 private:
3688 // Write out the BRLT data.
3689 void
3690 do_write(Output_file*);
c9824451 3691
ec661b9d
AM
3692 // The reloc section.
3693 Reloc_section* rel_;
3694 Target_powerpc<size, big_endian>* targ_;
3695};
cf43a2fe 3696
ec661b9d
AM
3697// Make the branch lookup table section.
3698
3699template<int size, bool big_endian>
3700void
3701Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3702{
3703 if (size == 64 && this->brlt_section_ == NULL)
3704 {
3705 Reloc_section* brlt_rel = NULL;
3706 bool is_pic = parameters->options().output_is_position_independent();
3707 if (is_pic)
3708 {
6830ee24
AM
3709 // When PIC we can't fill in .branch_lt (like .plt it can be
3710 // a bss style section) but must initialise at runtime via
ec661b9d
AM
3711 // dynamic relocats.
3712 this->rela_dyn_section(layout);
3713 brlt_rel = new Reloc_section(false);
3714 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3715 }
3716 this->brlt_section_
3717 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3718 if (this->plt_ && is_pic)
3719 this->plt_->output_section()
3720 ->add_output_section_data(this->brlt_section_);
3721 else
6830ee24 3722 layout->add_output_section_data(".branch_lt",
ec661b9d
AM
3723 (is_pic ? elfcpp::SHT_NOBITS
3724 : elfcpp::SHT_PROGBITS),
3725 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3726 this->brlt_section_,
3727 (is_pic ? ORDER_SMALL_BSS
3728 : ORDER_SMALL_DATA),
3729 false);
3730 }
3731}
3732
6830ee24 3733// Write out .branch_lt when non-PIC.
ec661b9d
AM
3734
3735template<int size, bool big_endian>
3736void
3737Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3738{
3739 if (size == 64 && !parameters->options().output_is_position_independent())
3740 {
3741 const section_size_type offset = this->offset();
3742 const section_size_type oview_size
3743 = convert_to_section_size_type(this->data_size());
3744 unsigned char* const oview = of->get_output_view(offset, oview_size);
3745
3746 this->targ_->write_branch_lookup_table(oview);
3747 of->write_output_view(offset, oview_size, oview);
3748 }
3749}
3750
9e69ed50
AM
3751static inline uint32_t
3752l(uint32_t a)
3753{
3754 return a & 0xffff;
3755}
3756
3757static inline uint32_t
3758hi(uint32_t a)
3759{
3760 return l(a >> 16);
3761}
3762
3763static inline uint32_t
3764ha(uint32_t a)
3765{
3766 return hi(a + 0x8000);
3767}
3768
9d5781f8
AM
3769template<int size>
3770struct Eh_cie
3771{
3772 static const unsigned char eh_frame_cie[12];
3773};
3774
3775template<int size>
3776const unsigned char Eh_cie<size>::eh_frame_cie[] =
3777{
3778 1, // CIE version.
3779 'z', 'R', 0, // Augmentation string.
3780 4, // Code alignment.
3781 0x80 - size / 8 , // Data alignment.
3782 65, // RA reg.
3783 1, // Augmentation size.
3784 (elfcpp::DW_EH_PE_pcrel
3785 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3786 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3787};
3788
b4f7960d
AM
3789// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3790static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
3791{
3792 0, 0, 0, 0, // Replaced with offset to .glink.
3793 0, 0, 0, 0, // Replaced with size of .glink.
3794 0, // Augmentation size.
3795 elfcpp::DW_CFA_advance_loc + 1,
3796 elfcpp::DW_CFA_register, 65, 12,
3797 elfcpp::DW_CFA_advance_loc + 4,
3798 elfcpp::DW_CFA_restore_extended, 65
3799};
3800
b4f7960d
AM
3801// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3802static const unsigned char glink_eh_frame_fde_64v2[] =
3803{
3804 0, 0, 0, 0, // Replaced with offset to .glink.
3805 0, 0, 0, 0, // Replaced with size of .glink.
3806 0, // Augmentation size.
3807 elfcpp::DW_CFA_advance_loc + 1,
3808 elfcpp::DW_CFA_register, 65, 0,
3809 elfcpp::DW_CFA_advance_loc + 4,
3810 elfcpp::DW_CFA_restore_extended, 65
3811};
3812
9d5781f8
AM
3813// Describe __glink_PLTresolve use of LR, 32-bit version.
3814static const unsigned char glink_eh_frame_fde_32[] =
3815{
3816 0, 0, 0, 0, // Replaced with offset to .glink.
3817 0, 0, 0, 0, // Replaced with size of .glink.
3818 0, // Augmentation size.
3819 elfcpp::DW_CFA_advance_loc + 2,
3820 elfcpp::DW_CFA_register, 65, 0,
3821 elfcpp::DW_CFA_advance_loc + 4,
3822 elfcpp::DW_CFA_restore_extended, 65
3823};
3824
3825static const unsigned char default_fde[] =
3826{
3827 0, 0, 0, 0, // Replaced with offset to stubs.
3828 0, 0, 0, 0, // Replaced with size of stubs.
3829 0, // Augmentation size.
3830 elfcpp::DW_CFA_nop, // Pad.
3831 elfcpp::DW_CFA_nop,
3832 elfcpp::DW_CFA_nop
3833};
3834
9e69ed50
AM
3835template<bool big_endian>
3836static inline void
3837write_insn(unsigned char* p, uint32_t v)
3838{
3839 elfcpp::Swap<32, big_endian>::writeval(p, v);
3840}
3841
ec661b9d
AM
3842// Stub_table holds information about plt and long branch stubs.
3843// Stubs are built in an area following some input section determined
3844// by group_sections(). This input section is converted to a relaxed
3845// input section allowing it to be resized to accommodate the stubs
3846
3847template<int size, bool big_endian>
3848class Stub_table : public Output_relaxed_input_section
3849{
3850 public:
3851 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3852 static const Address invalid_address = static_cast<Address>(0) - 1;
3853
a3e60ddb
AM
3854 Stub_table(Target_powerpc<size, big_endian>* targ,
3855 Output_section* output_section,
3856 const Output_section::Input_section* owner)
3857 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
3858 owner->relobj()
3859 ->section_addralign(owner->shndx())),
ec661b9d 3860 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
3861 orig_data_size_(owner->current_data_size()),
3862 plt_size_(0), last_plt_size_(0),
6395d38b
HS
3863 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
3864 eh_frame_added_(false), need_save_res_(false)
a3e60ddb
AM
3865 {
3866 this->set_output_section(output_section);
ec661b9d 3867
a3e60ddb
AM
3868 std::vector<Output_relaxed_input_section*> new_relaxed;
3869 new_relaxed.push_back(this);
3870 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3871 }
ec661b9d
AM
3872
3873 // Add a plt call stub.
a3e60ddb
AM
3874 bool
3875 add_plt_call_entry(Address,
3876 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
3877 const Symbol*,
3878 unsigned int,
3879 Address);
3880
a3e60ddb
AM
3881 bool
3882 add_plt_call_entry(Address,
3883 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
3884 unsigned int,
3885 unsigned int,
3886 Address);
3887
3888 // Find a given plt call stub.
3889 Address
3890 find_plt_call_entry(const Symbol*) const;
3891
3892 Address
3893 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3894 unsigned int) const;
3895
3896 Address
3897 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3898 const Symbol*,
3899 unsigned int,
3900 Address) const;
3901
3902 Address
3903 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3904 unsigned int,
3905 unsigned int,
3906 Address) const;
3907
3908 // Add a long branch stub.
a3e60ddb
AM
3909 bool
3910 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
d49044c7 3911 unsigned int, Address, Address, bool);
ec661b9d
AM
3912
3913 Address
9d5781f8
AM
3914 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3915 Address) const;
ec661b9d 3916
a3e60ddb
AM
3917 bool
3918 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
3919 {
cbcb23fa 3920 Address max_branch_offset = max_branch_delta(r_type);
a3e60ddb
AM
3921 if (max_branch_offset == 0)
3922 return true;
3923 gold_assert(from != invalid_address);
3924 Address loc = off + this->stub_address();
3925 return loc - from + max_branch_offset < 2 * max_branch_offset;
3926 }
3927
ec661b9d 3928 void
a3e60ddb 3929 clear_stubs(bool all)
cf43a2fe 3930 {
9e69ed50
AM
3931 this->plt_call_stubs_.clear();
3932 this->plt_size_ = 0;
ec661b9d
AM
3933 this->long_branch_stubs_.clear();
3934 this->branch_size_ = 0;
d49044c7 3935 this->need_save_res_ = false;
a3e60ddb
AM
3936 if (all)
3937 {
3938 this->last_plt_size_ = 0;
3939 this->last_branch_size_ = 0;
3940 }
cf43a2fe
AM
3941 }
3942
ec661b9d
AM
3943 Address
3944 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 3945 {
ec661b9d
AM
3946 Address start_off = off;
3947 off += this->orig_data_size_;
3948 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
3949 if (this->need_save_res_)
3950 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
3951 if (my_size != 0)
3952 off = align_address(off, this->stub_align());
3953 // Include original section size and alignment padding in size
3954 my_size += off - start_off;
6395d38b
HS
3955 // Ensure new size is always larger than min size
3956 // threshold. Alignment requirement is included in "my_size", so
3957 // increase "my_size" does not invalidate alignment.
3958 if (my_size < this->min_size_threshold_)
3959 my_size = this->min_size_threshold_;
ec661b9d
AM
3960 this->reset_address_and_file_offset();
3961 this->set_current_data_size(my_size);
3962 this->set_address_and_file_offset(os->address() + start_off,
3963 os->offset() + start_off);
3964 return my_size;
cf43a2fe
AM
3965 }
3966
ec661b9d 3967 Address
9d5781f8 3968 stub_address() const
ec661b9d
AM
3969 {
3970 return align_address(this->address() + this->orig_data_size_,
3971 this->stub_align());
3972 }
3973
3974 Address
9d5781f8 3975 stub_offset() const
ec661b9d
AM
3976 {
3977 return align_address(this->offset() + this->orig_data_size_,
3978 this->stub_align());
3979 }
3980
3981 section_size_type
3982 plt_size() const
3983 { return this->plt_size_; }
3984
6395d38b
HS
3985 void set_min_size_threshold(Address min_size)
3986 { this->min_size_threshold_ = min_size; }
3987
ec661b9d
AM
3988 bool
3989 size_update()
3990 {
3991 Output_section* os = this->output_section();
3992 if (os->addralign() < this->stub_align())
3993 {
3994 os->set_addralign(this->stub_align());
3995 // FIXME: get rid of the insane checkpointing.
3996 // We can't increase alignment of the input section to which
3997 // stubs are attached; The input section may be .init which
3998 // is pasted together with other .init sections to form a
3999 // function. Aligning might insert zero padding resulting in
4000 // sigill. However we do need to increase alignment of the
4001 // output section so that the align_address() on offset in
4002 // set_address_and_size() adds the same padding as the
4003 // align_address() on address in stub_address().
4004 // What's more, we need this alignment for the layout done in
4005 // relaxation_loop_body() so that the output section starts at
4006 // a suitably aligned address.
4007 os->checkpoint_set_addralign(this->stub_align());
4008 }
9e69ed50
AM
4009 if (this->last_plt_size_ != this->plt_size_
4010 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4011 {
9e69ed50
AM
4012 this->last_plt_size_ = this->plt_size_;
4013 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4014 return true;
4015 }
4016 return false;
4017 }
4018
9d5781f8
AM
4019 // Add .eh_frame info for this stub section. Unlike other linker
4020 // generated .eh_frame this is added late in the link, because we
4021 // only want the .eh_frame info if this particular stub section is
4022 // non-empty.
4023 void
4024 add_eh_frame(Layout* layout)
4025 {
4026 if (!this->eh_frame_added_)
4027 {
4028 if (!parameters->options().ld_generated_unwind_info())
4029 return;
4030
4031 // Since we add stub .eh_frame info late, it must be placed
4032 // after all other linker generated .eh_frame info so that
4033 // merge mapping need not be updated for input sections.
4034 // There is no provision to use a different CIE to that used
4035 // by .glink.
4036 if (!this->targ_->has_glink())
4037 return;
4038
4039 layout->add_eh_frame_for_plt(this,
4040 Eh_cie<size>::eh_frame_cie,
4041 sizeof (Eh_cie<size>::eh_frame_cie),
4042 default_fde,
4043 sizeof (default_fde));
4044 this->eh_frame_added_ = true;
4045 }
4046 }
4047
ec661b9d
AM
4048 Target_powerpc<size, big_endian>*
4049 targ() const
4050 { return targ_; }
6ce78956 4051
cf43a2fe 4052 private:
9e69ed50
AM
4053 class Plt_stub_ent;
4054 class Plt_stub_ent_hash;
4055 typedef Unordered_map<Plt_stub_ent, unsigned int,
4056 Plt_stub_ent_hash> Plt_stub_entries;
4057
4058 // Alignment of stub section.
ec661b9d 4059 unsigned int
9e69ed50
AM
4060 stub_align() const
4061 {
4062 if (size == 32)
4063 return 16;
4064 unsigned int min_align = 32;
4065 unsigned int user_align = 1 << parameters->options().plt_align();
4066 return std::max(user_align, min_align);
4067 }
cf43a2fe 4068
91c2b899
AM
4069 // Return the plt offset for the given call stub.
4070 Address
4071 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4072 {
4073 const Symbol* gsym = p->first.sym_;
4074 if (gsym != NULL)
4075 {
4076 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4077 && gsym->can_use_relative_reloc(false));
4078 return gsym->plt_offset();
4079 }
4080 else
4081 {
4082 *is_iplt = true;
4083 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4084 unsigned int local_sym_index = p->first.locsym_;
4085 return relobj->local_plt_offset(local_sym_index);
4086 }
4087 }
4088
9e69ed50 4089 // Size of a given plt call stub.
ec661b9d 4090 unsigned int
9e69ed50
AM
4091 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4092 {
4093 if (size == 32)
4094 return 16;
4095
91c2b899
AM
4096 bool is_iplt;
4097 Address plt_addr = this->plt_off(p, &is_iplt);
4098 if (is_iplt)
4099 plt_addr += this->targ_->iplt_section()->address();
9e69ed50 4100 else
91c2b899
AM
4101 plt_addr += this->targ_->plt_section()->address();
4102 Address got_addr = this->targ_->got_section()->output_section()->address();
9e69ed50
AM
4103 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4104 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
91c2b899
AM
4105 got_addr += ppcobj->toc_base_offset();
4106 Address off = plt_addr - got_addr;
b4f7960d
AM
4107 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4108 if (this->targ_->abiversion() < 2)
4109 {
4110 bool static_chain = parameters->options().plt_static_chain();
4111 bool thread_safe = this->targ_->plt_thread_safe();
4112 bytes += (4
4113 + 4 * static_chain
4114 + 8 * thread_safe
4115 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4116 }
9e69ed50
AM
4117 unsigned int align = 1 << parameters->options().plt_align();
4118 if (align > 1)
4119 bytes = (bytes + align - 1) & -align;
4120 return bytes;
4121 }
ec661b9d
AM
4122
4123 // Return long branch stub size.
4124 unsigned int
4125 branch_stub_size(Address to)
4126 {
9e69ed50
AM
4127 Address loc
4128 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
4129 if (to - loc + (1 << 25) < 2 << 25)
ec661b9d
AM
4130 return 4;
4131 if (size == 64 || !parameters->options().output_is_position_independent())
4132 return 16;
4133 return 32;
4134 }
4135
4136 // Write out stubs.
cf43a2fe
AM
4137 void
4138 do_write(Output_file*);
4139
ec661b9d
AM
4140 // Plt call stub keys.
4141 class Plt_stub_ent
cf43a2fe 4142 {
d1a8cabd 4143 public:
ec661b9d 4144 Plt_stub_ent(const Symbol* sym)
c9824451
AM
4145 : sym_(sym), object_(0), addend_(0), locsym_(0)
4146 { }
4147
ec661b9d
AM
4148 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
4149 unsigned int locsym_index)
c9824451
AM
4150 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4151 { }
4152
ec661b9d
AM
4153 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
4154 const Symbol* sym,
4155 unsigned int r_type,
4156 Address addend)
e5d5f5ed 4157 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4158 {
4159 if (size != 32)
ec661b9d 4160 this->addend_ = addend;
d1a8cabd 4161 else if (parameters->options().output_is_position_independent()
ec661b9d 4162 && r_type == elfcpp::R_PPC_PLTREL24)
cf43a2fe 4163 {
ec661b9d 4164 this->addend_ = addend;
e5d5f5ed 4165 if (this->addend_ >= 32768)
d1a8cabd 4166 this->object_ = object;
cf43a2fe
AM
4167 }
4168 }
4169
ec661b9d
AM
4170 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
4171 unsigned int locsym_index,
4172 unsigned int r_type,
4173 Address addend)
e5d5f5ed
AM
4174 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4175 {
4176 if (size != 32)
ec661b9d 4177 this->addend_ = addend;
e5d5f5ed 4178 else if (parameters->options().output_is_position_independent()
ec661b9d
AM
4179 && r_type == elfcpp::R_PPC_PLTREL24)
4180 this->addend_ = addend;
e5d5f5ed
AM
4181 }
4182
ec661b9d 4183 bool operator==(const Plt_stub_ent& that) const
cf43a2fe
AM
4184 {
4185 return (this->sym_ == that.sym_
4186 && this->object_ == that.object_
e5d5f5ed
AM
4187 && this->addend_ == that.addend_
4188 && this->locsym_ == that.locsym_);
cf43a2fe 4189 }
c9269dff
AM
4190
4191 const Symbol* sym_;
e5d5f5ed
AM
4192 const Sized_relobj_file<size, big_endian>* object_;
4193 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4194 unsigned int locsym_;
cf43a2fe
AM
4195 };
4196
ec661b9d 4197 class Plt_stub_ent_hash
cf43a2fe 4198 {
d1a8cabd 4199 public:
ec661b9d 4200 size_t operator()(const Plt_stub_ent& ent) const
cf43a2fe
AM
4201 {
4202 return (reinterpret_cast<uintptr_t>(ent.sym_)
4203 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
4204 ^ ent.addend_
4205 ^ ent.locsym_);
cf43a2fe 4206 }
ec661b9d
AM
4207 };
4208
4209 // Long branch stub keys.
4210 class Branch_stub_ent
4211 {
4212 public:
d49044c7
AM
4213 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4214 Address to, bool save_res)
4215 : dest_(to), toc_base_off_(0), save_res_(save_res)
ec661b9d
AM
4216 {
4217 if (size == 64)
4218 toc_base_off_ = obj->toc_base_offset();
4219 }
4220
4221 bool operator==(const Branch_stub_ent& that) const
4222 {
4223 return (this->dest_ == that.dest_
4224 && (size == 32
4225 || this->toc_base_off_ == that.toc_base_off_));
4226 }
cf43a2fe 4227
ec661b9d
AM
4228 Address dest_;
4229 unsigned int toc_base_off_;
d49044c7 4230 bool save_res_;
ec661b9d 4231 };
cf43a2fe 4232
ec661b9d
AM
4233 class Branch_stub_ent_hash
4234 {
4235 public:
4236 size_t operator()(const Branch_stub_ent& ent) const
4237 { return ent.dest_ ^ ent.toc_base_off_; }
4238 };
cf43a2fe 4239
ec661b9d 4240 // In a sane world this would be a global.
cf43a2fe 4241 Target_powerpc<size, big_endian>* targ_;
ec661b9d 4242 // Map sym/object/addend to stub offset.
ec661b9d
AM
4243 Plt_stub_entries plt_call_stubs_;
4244 // Map destination address to stub offset.
4245 typedef Unordered_map<Branch_stub_ent, unsigned int,
4246 Branch_stub_ent_hash> Branch_stub_entries;
4247 Branch_stub_entries long_branch_stubs_;
4248 // size of input section
4249 section_size_type orig_data_size_;
4250 // size of stubs
9e69ed50 4251 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
4252 // Some rare cases cause (PR/20529) fluctuation in stub table
4253 // size, which leads to an endless relax loop. This is to be fixed
4254 // by, after the first few iterations, allowing only increase of
4255 // stub table size. This variable sets the minimal possible size of
4256 // a stub table, it is zero for the first few iterations, then
4257 // increases monotonically.
4258 Address min_size_threshold_;
9d5781f8
AM
4259 // Whether .eh_frame info has been created for this stub section.
4260 bool eh_frame_added_;
d49044c7
AM
4261 // Set if this stub group needs a copy of out-of-line register
4262 // save/restore functions.
4263 bool need_save_res_;
cf43a2fe
AM
4264};
4265
ec661b9d 4266// Add a plt call stub, if we do not already have one for this
d1a8cabd 4267// sym/object/addend combo.
cf43a2fe
AM
4268
4269template<int size, bool big_endian>
a3e60ddb 4270bool
ec661b9d 4271Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4272 Address from,
c9824451 4273 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4274 const Symbol* gsym,
ec661b9d
AM
4275 unsigned int r_type,
4276 Address addend)
cf43a2fe 4277{
ec661b9d 4278 Plt_stub_ent ent(object, gsym, r_type, addend);
9055360d 4279 unsigned int off = this->plt_size_;
9e69ed50
AM
4280 std::pair<typename Plt_stub_entries::iterator, bool> p
4281 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
4282 if (p.second)
4283 this->plt_size_ = off + this->plt_call_size(p.first);
a3e60ddb 4284 return this->can_reach_stub(from, off, r_type);
cf43a2fe
AM
4285}
4286
e5d5f5ed 4287template<int size, bool big_endian>
a3e60ddb 4288bool
ec661b9d 4289Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4290 Address from,
c9824451 4291 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4292 unsigned int locsym_index,
ec661b9d
AM
4293 unsigned int r_type,
4294 Address addend)
e5d5f5ed 4295{
ec661b9d 4296 Plt_stub_ent ent(object, locsym_index, r_type, addend);
9055360d 4297 unsigned int off = this->plt_size_;
9e69ed50
AM
4298 std::pair<typename Plt_stub_entries::iterator, bool> p
4299 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
4300 if (p.second)
4301 this->plt_size_ = off + this->plt_call_size(p.first);
a3e60ddb 4302 return this->can_reach_stub(from, off, r_type);
e5d5f5ed
AM
4303}
4304
ec661b9d
AM
4305// Find a plt call stub.
4306
cf43a2fe 4307template<int size, bool big_endian>
ec5b8187 4308typename Stub_table<size, big_endian>::Address
ec661b9d 4309Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4310 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4311 const Symbol* gsym,
ec661b9d
AM
4312 unsigned int r_type,
4313 Address addend) const
c9824451 4314{
ec661b9d
AM
4315 Plt_stub_ent ent(object, gsym, r_type, addend);
4316 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4317 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
c9824451
AM
4318}
4319
4320template<int size, bool big_endian>
ec5b8187 4321typename Stub_table<size, big_endian>::Address
ec661b9d 4322Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 4323{
ec661b9d
AM
4324 Plt_stub_ent ent(gsym);
4325 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4326 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
cf43a2fe
AM
4327}
4328
e5d5f5ed 4329template<int size, bool big_endian>
ec5b8187 4330typename Stub_table<size, big_endian>::Address
ec661b9d 4331Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4332 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4333 unsigned int locsym_index,
ec661b9d
AM
4334 unsigned int r_type,
4335 Address addend) const
e5d5f5ed 4336{
ec661b9d
AM
4337 Plt_stub_ent ent(object, locsym_index, r_type, addend);
4338 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4339 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
c9824451
AM
4340}
4341
4342template<int size, bool big_endian>
ec5b8187 4343typename Stub_table<size, big_endian>::Address
ec661b9d 4344Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
4345 const Sized_relobj_file<size, big_endian>* object,
4346 unsigned int locsym_index) const
4347{
ec661b9d
AM
4348 Plt_stub_ent ent(object, locsym_index);
4349 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4350 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
4351}
4352
4353// Add a long branch stub if we don't already have one to given
4354// destination.
4355
4356template<int size, bool big_endian>
a3e60ddb 4357bool
ec661b9d
AM
4358Stub_table<size, big_endian>::add_long_branch_entry(
4359 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
4360 unsigned int r_type,
4361 Address from,
d49044c7
AM
4362 Address to,
4363 bool save_res)
ec661b9d 4364{
d49044c7 4365 Branch_stub_ent ent(object, to, save_res);
ec661b9d
AM
4366 Address off = this->branch_size_;
4367 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
4368 {
d49044c7
AM
4369 if (save_res)
4370 this->need_save_res_ = true;
4371 else
4372 {
4373 unsigned int stub_size = this->branch_stub_size(to);
4374 this->branch_size_ = off + stub_size;
4375 if (size == 64 && stub_size != 4)
4376 this->targ_->add_branch_lookup_table(to);
4377 }
ec661b9d 4378 }
a3e60ddb 4379 return this->can_reach_stub(from, off, r_type);
ec661b9d
AM
4380}
4381
d49044c7 4382// Find long branch stub offset.
ec661b9d
AM
4383
4384template<int size, bool big_endian>
ec5b8187 4385typename Stub_table<size, big_endian>::Address
ec661b9d
AM
4386Stub_table<size, big_endian>::find_long_branch_entry(
4387 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 4388 Address to) const
ec661b9d 4389{
d49044c7 4390 Branch_stub_ent ent(object, to, false);
ec661b9d
AM
4391 typename Branch_stub_entries::const_iterator p
4392 = this->long_branch_stubs_.find(ent);
d49044c7
AM
4393 if (p == this->long_branch_stubs_.end())
4394 return invalid_address;
4395 if (p->first.save_res_)
4396 return to - this->targ_->savres_section()->address() + this->branch_size_;
4397 return p->second;
e5d5f5ed
AM
4398}
4399
ec661b9d
AM
4400// A class to handle .glink.
4401
4402template<int size, bool big_endian>
4403class Output_data_glink : public Output_section_data
4404{
4405 public:
9055360d
AM
4406 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4407 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
4408 static const int pltresolve_size = 16*4;
4409
4410 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
4411 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4412 end_branch_table_(), ge_size_(0)
ec661b9d
AM
4413 { }
4414
9d5781f8 4415 void
9055360d 4416 add_eh_frame(Layout* layout);
9d5781f8 4417
9055360d
AM
4418 void
4419 add_global_entry(const Symbol*);
4420
4421 Address
4422 find_global_entry(const Symbol*) const;
4423
4424 Address
4425 global_entry_address() const
4426 {
4427 gold_assert(this->is_data_size_valid());
4428 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4429 return this->address() + global_entry_off;
9d5781f8
AM
4430 }
4431
ec661b9d
AM
4432 protected:
4433 // Write to a map file.
4434 void
4435 do_print_to_mapfile(Mapfile* mapfile) const
4436 { mapfile->print_output_data(this, _("** glink")); }
4437
4438 private:
4439 void
4440 set_final_data_size();
4441
4442 // Write out .glink
4443 void
4444 do_write(Output_file*);
4445
4446 // Allows access to .got and .plt for do_write.
4447 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
4448
4449 // Map sym to stub offset.
4450 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4451 Global_entry_stub_entries global_entry_stubs_;
4452
4453 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
4454};
4455
9055360d
AM
4456template<int size, bool big_endian>
4457void
4458Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4459{
4460 if (!parameters->options().ld_generated_unwind_info())
4461 return;
4462
4463 if (size == 64)
4464 {
4465 if (this->targ_->abiversion() < 2)
4466 layout->add_eh_frame_for_plt(this,
4467 Eh_cie<64>::eh_frame_cie,
4468 sizeof (Eh_cie<64>::eh_frame_cie),
4469 glink_eh_frame_fde_64v1,
4470 sizeof (glink_eh_frame_fde_64v1));
4471 else
4472 layout->add_eh_frame_for_plt(this,
4473 Eh_cie<64>::eh_frame_cie,
4474 sizeof (Eh_cie<64>::eh_frame_cie),
4475 glink_eh_frame_fde_64v2,
4476 sizeof (glink_eh_frame_fde_64v2));
4477 }
4478 else
4479 {
4480 // 32-bit .glink can use the default since the CIE return
4481 // address reg, LR, is valid.
4482 layout->add_eh_frame_for_plt(this,
4483 Eh_cie<32>::eh_frame_cie,
4484 sizeof (Eh_cie<32>::eh_frame_cie),
4485 default_fde,
4486 sizeof (default_fde));
4487 // Except where LR is used in a PIC __glink_PLTresolve.
4488 if (parameters->options().output_is_position_independent())
4489 layout->add_eh_frame_for_plt(this,
4490 Eh_cie<32>::eh_frame_cie,
4491 sizeof (Eh_cie<32>::eh_frame_cie),
4492 glink_eh_frame_fde_32,
4493 sizeof (glink_eh_frame_fde_32));
4494 }
4495}
4496
4497template<int size, bool big_endian>
4498void
4499Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4500{
4501 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4502 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4503 if (p.second)
4504 this->ge_size_ += 16;
4505}
4506
4507template<int size, bool big_endian>
4508typename Output_data_glink<size, big_endian>::Address
4509Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4510{
4511 typename Global_entry_stub_entries::const_iterator p
4512 = this->global_entry_stubs_.find(gsym);
4513 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4514}
4515
cf43a2fe
AM
4516template<int size, bool big_endian>
4517void
4518Output_data_glink<size, big_endian>::set_final_data_size()
4519{
ec661b9d
AM
4520 unsigned int count = this->targ_->plt_entry_count();
4521 section_size_type total = 0;
cf43a2fe
AM
4522
4523 if (count != 0)
4524 {
4525 if (size == 32)
4526 {
cf43a2fe
AM
4527 // space for branch table
4528 total += 4 * (count - 1);
4529
4530 total += -total & 15;
4531 total += this->pltresolve_size;
4532 }
4533 else
4534 {
cf43a2fe
AM
4535 total += this->pltresolve_size;
4536
4537 // space for branch table
b4f7960d
AM
4538 total += 4 * count;
4539 if (this->targ_->abiversion() < 2)
4540 {
4541 total += 4 * count;
4542 if (count > 0x8000)
4543 total += 4 * (count - 0x8000);
4544 }
cf43a2fe
AM
4545 }
4546 }
9055360d
AM
4547 this->end_branch_table_ = total;
4548 total = (total + 15) & -16;
4549 total += this->ge_size_;
cf43a2fe
AM
4550
4551 this->set_data_size(total);
4552}
4553
ec661b9d 4554// Write out plt and long branch stub code.
cf43a2fe
AM
4555
4556template<int size, bool big_endian>
4557void
ec661b9d 4558Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 4559{
ec661b9d
AM
4560 if (this->plt_call_stubs_.empty()
4561 && this->long_branch_stubs_.empty())
4562 return;
4563
4564 const section_size_type start_off = this->offset();
4565 const section_size_type off = this->stub_offset();
42cacb20 4566 const section_size_type oview_size =
ec661b9d 4567 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 4568 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 4569 unsigned char* p;
42cacb20 4570
cf43a2fe
AM
4571 if (size == 64)
4572 {
ec661b9d
AM
4573 const Output_data_got_powerpc<size, big_endian>* got
4574 = this->targ_->got_section();
dd93cd0a 4575 Address got_os_addr = got->output_section()->address();
c9269dff 4576
ec661b9d 4577 if (!this->plt_call_stubs_.empty())
cf43a2fe 4578 {
ec661b9d
AM
4579 // The base address of the .plt section.
4580 Address plt_base = this->targ_->plt_section()->address();
4581 Address iplt_base = invalid_address;
4582
4583 // Write out plt call stubs.
4584 typename Plt_stub_entries::const_iterator cs;
4585 for (cs = this->plt_call_stubs_.begin();
4586 cs != this->plt_call_stubs_.end();
4587 ++cs)
e5d5f5ed 4588 {
91c2b899
AM
4589 bool is_iplt;
4590 Address pltoff = this->plt_off(cs, &is_iplt);
9e69ed50 4591 Address plt_addr = pltoff;
91c2b899 4592 if (is_iplt)
ec661b9d
AM
4593 {
4594 if (iplt_base == invalid_address)
4595 iplt_base = this->targ_->iplt_section()->address();
4596 plt_addr += iplt_base;
4597 }
4598 else
4599 plt_addr += plt_base;
4600 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4601 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4602 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 4603 Address off = plt_addr - got_addr;
ec661b9d 4604
9e69ed50 4605 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
ec661b9d
AM
4606 gold_error(_("%s: linkage table error against `%s'"),
4607 cs->first.object_->name().c_str(),
4608 cs->first.sym_->demangled_name().c_str());
4609
b4f7960d
AM
4610 bool plt_load_toc = this->targ_->abiversion() < 2;
4611 bool static_chain
4612 = plt_load_toc && parameters->options().plt_static_chain();
4613 bool thread_safe
4614 = plt_load_toc && this->targ_->plt_thread_safe();
9e69ed50
AM
4615 bool use_fake_dep = false;
4616 Address cmp_branch_off = 0;
4617 if (thread_safe)
4618 {
4619 unsigned int pltindex
4620 = ((pltoff - this->targ_->first_plt_entry_offset())
4621 / this->targ_->plt_entry_size());
4622 Address glinkoff
4623 = (this->targ_->glink_section()->pltresolve_size
4624 + pltindex * 8);
4625 if (pltindex > 32768)
4626 glinkoff += (pltindex - 32768) * 4;
4627 Address to
4628 = this->targ_->glink_section()->address() + glinkoff;
4629 Address from
4630 = (this->stub_address() + cs->second + 24
4631 + 4 * (ha(off) != 0)
4632 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4633 + 4 * static_chain);
4634 cmp_branch_off = to - from;
4635 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4636 }
4637
ec661b9d 4638 p = oview + cs->second;
9e69ed50 4639 if (ha(off) != 0)
ec661b9d 4640 {
b4f7960d
AM
4641 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4642 p += 4;
397998fc
AM
4643 if (plt_load_toc)
4644 {
4645 write_insn<big_endian>(p, addis_11_2 + ha(off));
4646 p += 4;
4647 write_insn<big_endian>(p, ld_12_11 + l(off));
4648 p += 4;
4649 }
4650 else
4651 {
4652 write_insn<big_endian>(p, addis_12_2 + ha(off));
4653 p += 4;
4654 write_insn<big_endian>(p, ld_12_12 + l(off));
4655 p += 4;
4656 }
b4f7960d
AM
4657 if (plt_load_toc
4658 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 4659 {
b4f7960d
AM
4660 write_insn<big_endian>(p, addi_11_11 + l(off));
4661 p += 4;
9e69ed50 4662 off = 0;
ec661b9d 4663 }
b4f7960d
AM
4664 write_insn<big_endian>(p, mtctr_12);
4665 p += 4;
4666 if (plt_load_toc)
9e69ed50 4667 {
b4f7960d
AM
4668 if (use_fake_dep)
4669 {
4670 write_insn<big_endian>(p, xor_2_12_12);
4671 p += 4;
4672 write_insn<big_endian>(p, add_11_11_2);
4673 p += 4;
4674 }
4675 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4676 p += 4;
4677 if (static_chain)
4678 {
4679 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4680 p += 4;
4681 }
9e69ed50 4682 }
ec661b9d
AM
4683 }
4684 else
4685 {
b4f7960d
AM
4686 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4687 p += 4;
4688 write_insn<big_endian>(p, ld_12_2 + l(off));
4689 p += 4;
4690 if (plt_load_toc
4691 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 4692 {
b4f7960d
AM
4693 write_insn<big_endian>(p, addi_2_2 + l(off));
4694 p += 4;
9e69ed50 4695 off = 0;
ec661b9d 4696 }
b4f7960d
AM
4697 write_insn<big_endian>(p, mtctr_12);
4698 p += 4;
4699 if (plt_load_toc)
9e69ed50 4700 {
b4f7960d
AM
4701 if (use_fake_dep)
4702 {
4703 write_insn<big_endian>(p, xor_11_12_12);
4704 p += 4;
4705 write_insn<big_endian>(p, add_2_2_11);
4706 p += 4;
4707 }
4708 if (static_chain)
4709 {
4710 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4711 p += 4;
4712 }
4713 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4714 p += 4;
9e69ed50 4715 }
ec661b9d 4716 }
9e69ed50
AM
4717 if (thread_safe && !use_fake_dep)
4718 {
b4f7960d
AM
4719 write_insn<big_endian>(p, cmpldi_2_0);
4720 p += 4;
4721 write_insn<big_endian>(p, bnectr_p4);
4722 p += 4;
9e69ed50
AM
4723 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4724 }
4725 else
4726 write_insn<big_endian>(p, bctr);
e5d5f5ed 4727 }
ec661b9d
AM
4728 }
4729
4730 // Write out long branch stubs.
4731 typename Branch_stub_entries::const_iterator bs;
4732 for (bs = this->long_branch_stubs_.begin();
4733 bs != this->long_branch_stubs_.end();
4734 ++bs)
4735 {
d49044c7
AM
4736 if (bs->first.save_res_)
4737 continue;
ec661b9d
AM
4738 p = oview + this->plt_size_ + bs->second;
4739 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4740 Address delta = bs->first.dest_ - loc;
4741 if (delta + (1 << 25) < 2 << 25)
4742 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
e5d5f5ed 4743 else
cf43a2fe 4744 {
ec661b9d
AM
4745 Address brlt_addr
4746 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4747 gold_assert(brlt_addr != invalid_address);
4748 brlt_addr += this->targ_->brlt_section()->address();
4749 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4750 Address brltoff = brlt_addr - got_addr;
4751 if (ha(brltoff) == 0)
4752 {
b4f7960d 4753 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
ec661b9d
AM
4754 }
4755 else
cf43a2fe 4756 {
397998fc
AM
4757 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
4758 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
cf43a2fe 4759 }
b4f7960d 4760 write_insn<big_endian>(p, mtctr_12), p += 4;
ec661b9d 4761 write_insn<big_endian>(p, bctr);
cf43a2fe 4762 }
ec661b9d
AM
4763 }
4764 }
4765 else
4766 {
4767 if (!this->plt_call_stubs_.empty())
4768 {
4769 // The base address of the .plt section.
4770 Address plt_base = this->targ_->plt_section()->address();
4771 Address iplt_base = invalid_address;
4772 // The address of _GLOBAL_OFFSET_TABLE_.
4773 Address g_o_t = invalid_address;
4774
4775 // Write out plt call stubs.
4776 typename Plt_stub_entries::const_iterator cs;
4777 for (cs = this->plt_call_stubs_.begin();
4778 cs != this->plt_call_stubs_.end();
4779 ++cs)
cf43a2fe 4780 {
91c2b899
AM
4781 bool is_iplt;
4782 Address plt_addr = this->plt_off(cs, &is_iplt);
4783 if (is_iplt)
ec661b9d
AM
4784 {
4785 if (iplt_base == invalid_address)
4786 iplt_base = this->targ_->iplt_section()->address();
4787 plt_addr += iplt_base;
4788 }
4789 else
4790 plt_addr += plt_base;
4791
4792 p = oview + cs->second;
4793 if (parameters->options().output_is_position_independent())
4794 {
4795 Address got_addr;
4796 const Powerpc_relobj<size, big_endian>* ppcobj
4797 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4798 (cs->first.object_));
4799 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4800 {
4801 unsigned int got2 = ppcobj->got2_shndx();
4802 got_addr = ppcobj->get_output_section_offset(got2);
4803 gold_assert(got_addr != invalid_address);
4804 got_addr += (ppcobj->output_section(got2)->address()
4805 + cs->first.addend_);
4806 }
4807 else
4808 {
4809 if (g_o_t == invalid_address)
4810 {
4811 const Output_data_got_powerpc<size, big_endian>* got
4812 = this->targ_->got_section();
4813 g_o_t = got->address() + got->g_o_t();
4814 }
4815 got_addr = g_o_t;
4816 }
4817
9e69ed50
AM
4818 Address off = plt_addr - got_addr;
4819 if (ha(off) == 0)
ec661b9d 4820 {
9e69ed50 4821 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
ec661b9d
AM
4822 write_insn<big_endian>(p + 4, mtctr_11);
4823 write_insn<big_endian>(p + 8, bctr);
4824 }
4825 else
4826 {
9e69ed50
AM
4827 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4828 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
ec661b9d
AM
4829 write_insn<big_endian>(p + 8, mtctr_11);
4830 write_insn<big_endian>(p + 12, bctr);
4831 }
4832 }
4833 else
4834 {
4835 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4836 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4837 write_insn<big_endian>(p + 8, mtctr_11);
4838 write_insn<big_endian>(p + 12, bctr);
4839 }
4840 }
4841 }
4842
4843 // Write out long branch stubs.
4844 typename Branch_stub_entries::const_iterator bs;
4845 for (bs = this->long_branch_stubs_.begin();
4846 bs != this->long_branch_stubs_.end();
4847 ++bs)
4848 {
d49044c7
AM
4849 if (bs->first.save_res_)
4850 continue;
ec661b9d
AM
4851 p = oview + this->plt_size_ + bs->second;
4852 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4853 Address delta = bs->first.dest_ - loc;
4854 if (delta + (1 << 25) < 2 << 25)
4855 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4856 else if (!parameters->options().output_is_position_independent())
4857 {
4858 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4859 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4860 write_insn<big_endian>(p + 8, mtctr_12);
4861 write_insn<big_endian>(p + 12, bctr);
4862 }
4863 else
4864 {
4865 delta -= 8;
4866 write_insn<big_endian>(p + 0, mflr_0);
4867 write_insn<big_endian>(p + 4, bcl_20_31);
4868 write_insn<big_endian>(p + 8, mflr_12);
4869 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4870 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4871 write_insn<big_endian>(p + 20, mtlr_0);
4872 write_insn<big_endian>(p + 24, mtctr_12);
4873 write_insn<big_endian>(p + 28, bctr);
cf43a2fe
AM
4874 }
4875 }
ec661b9d 4876 }
d49044c7
AM
4877 if (this->need_save_res_)
4878 {
4879 p = oview + this->plt_size_ + this->branch_size_;
4880 memcpy (p, this->targ_->savres_section()->contents(),
4881 this->targ_->savres_section()->data_size());
4882 }
ec661b9d
AM
4883}
4884
4885// Write out .glink.
4886
4887template<int size, bool big_endian>
4888void
4889Output_data_glink<size, big_endian>::do_write(Output_file* of)
4890{
4891 const section_size_type off = this->offset();
4892 const section_size_type oview_size =
4893 convert_to_section_size_type(this->data_size());
4894 unsigned char* const oview = of->get_output_view(off, oview_size);
4895 unsigned char* p;
4896
4897 // The base address of the .plt section.
4898 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4899 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 4900
ec661b9d
AM
4901 if (size == 64)
4902 {
9055360d 4903 if (this->end_branch_table_ != 0)
b4f7960d 4904 {
9055360d
AM
4905 // Write pltresolve stub.
4906 p = oview;
4907 Address after_bcl = this->address() + 16;
4908 Address pltoff = plt_base - after_bcl;
4909
4910 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 4911
b4f7960d 4912 if (this->targ_->abiversion() < 2)
cf43a2fe 4913 {
9055360d
AM
4914 write_insn<big_endian>(p, mflr_12), p += 4;
4915 write_insn<big_endian>(p, bcl_20_31), p += 4;
4916 write_insn<big_endian>(p, mflr_11), p += 4;
4917 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4918 write_insn<big_endian>(p, mtlr_12), p += 4;
4919 write_insn<big_endian>(p, add_11_2_11), p += 4;
4920 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4921 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
4922 write_insn<big_endian>(p, mtctr_12), p += 4;
4923 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
4924 }
4925 else
4926 {
4927 write_insn<big_endian>(p, mflr_0), p += 4;
4928 write_insn<big_endian>(p, bcl_20_31), p += 4;
4929 write_insn<big_endian>(p, mflr_11), p += 4;
4930 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4931 write_insn<big_endian>(p, mtlr_0), p += 4;
4932 write_insn<big_endian>(p, sub_12_12_11), p += 4;
4933 write_insn<big_endian>(p, add_11_2_11), p += 4;
4934 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
4935 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4936 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
4937 write_insn<big_endian>(p, mtctr_12), p += 4;
4938 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
4939 }
4940 write_insn<big_endian>(p, bctr), p += 4;
4941 while (p < oview + this->pltresolve_size)
4942 write_insn<big_endian>(p, nop), p += 4;
4943
4944 // Write lazy link call stubs.
4945 uint32_t indx = 0;
4946 while (p < oview + this->end_branch_table_)
4947 {
4948 if (this->targ_->abiversion() < 2)
b4f7960d 4949 {
9055360d
AM
4950 if (indx < 0x8000)
4951 {
4952 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4953 }
4954 else
4955 {
bbec1a5d 4956 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
4957 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4958 }
b4f7960d 4959 }
9055360d
AM
4960 uint32_t branch_off = 8 - (p - oview);
4961 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4962 indx++;
cf43a2fe 4963 }
9055360d
AM
4964 }
4965
4966 Address plt_base = this->targ_->plt_section()->address();
4967 Address iplt_base = invalid_address;
4968 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4969 Address global_entry_base = this->address() + global_entry_off;
4970 typename Global_entry_stub_entries::const_iterator ge;
4971 for (ge = this->global_entry_stubs_.begin();
4972 ge != this->global_entry_stubs_.end();
4973 ++ge)
4974 {
4975 p = oview + global_entry_off + ge->second;
4976 Address plt_addr = ge->first->plt_offset();
4977 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
4978 && ge->first->can_use_relative_reloc(false))
4979 {
4980 if (iplt_base == invalid_address)
4981 iplt_base = this->targ_->iplt_section()->address();
4982 plt_addr += iplt_base;
4983 }
4984 else
4985 plt_addr += plt_base;
4986 Address my_addr = global_entry_base + ge->second;
4987 Address off = plt_addr - my_addr;
4988
4989 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
4990 gold_error(_("%s: linkage table error against `%s'"),
4991 ge->first->object()->name().c_str(),
4992 ge->first->demangled_name().c_str());
4993
4994 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
4995 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
4996 write_insn<big_endian>(p, mtctr_12), p += 4;
4997 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
4998 }
4999 }
5000 else
5001 {
ec661b9d
AM
5002 const Output_data_got_powerpc<size, big_endian>* got
5003 = this->targ_->got_section();
dd93cd0a
AM
5004 // The address of _GLOBAL_OFFSET_TABLE_.
5005 Address g_o_t = got->address() + got->g_o_t();
c9269dff 5006
cf43a2fe 5007 // Write out pltresolve branch table.
ec661b9d 5008 p = oview;
cf43a2fe 5009 unsigned int the_end = oview_size - this->pltresolve_size;
c9269dff 5010 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
5011 while (p < end_p - 8 * 4)
5012 write_insn<big_endian>(p, b + end_p - p), p += 4;
5013 while (p < end_p)
5014 write_insn<big_endian>(p, nop), p += 4;
42cacb20 5015
cf43a2fe
AM
5016 // Write out pltresolve call stub.
5017 if (parameters->options().output_is_position_independent())
42cacb20 5018 {
ec661b9d 5019 Address res0_off = 0;
dd93cd0a
AM
5020 Address after_bcl_off = the_end + 12;
5021 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe
AM
5022
5023 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5024 write_insn<big_endian>(p + 4, mflr_0);
5025 write_insn<big_endian>(p + 8, bcl_20_31);
5026 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5027 write_insn<big_endian>(p + 16, mflr_12);
5028 write_insn<big_endian>(p + 20, mtlr_0);
5029 write_insn<big_endian>(p + 24, sub_11_11_12);
5030
dd93cd0a 5031 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe
AM
5032
5033 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5034 if (ha(got_bcl) == ha(got_bcl + 4))
5035 {
5036 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5037 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5038 }
5039 else
5040 {
5041 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5042 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5043 }
5044 write_insn<big_endian>(p + 40, mtctr_0);
5045 write_insn<big_endian>(p + 44, add_0_11_11);
5046 write_insn<big_endian>(p + 48, add_11_0_11);
5047 write_insn<big_endian>(p + 52, bctr);
5048 write_insn<big_endian>(p + 56, nop);
5049 write_insn<big_endian>(p + 60, nop);
42cacb20 5050 }
cf43a2fe 5051 else
42cacb20 5052 {
ec661b9d 5053 Address res0 = this->address();
cf43a2fe
AM
5054
5055 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5056 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5057 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5058 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5059 else
5060 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5061 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5062 write_insn<big_endian>(p + 16, mtctr_0);
5063 write_insn<big_endian>(p + 20, add_0_11_11);
5064 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5065 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5066 else
5067 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5068 write_insn<big_endian>(p + 28, add_11_0_11);
5069 write_insn<big_endian>(p + 32, bctr);
5070 write_insn<big_endian>(p + 36, nop);
5071 write_insn<big_endian>(p + 40, nop);
5072 write_insn<big_endian>(p + 44, nop);
5073 write_insn<big_endian>(p + 48, nop);
5074 write_insn<big_endian>(p + 52, nop);
5075 write_insn<big_endian>(p + 56, nop);
5076 write_insn<big_endian>(p + 60, nop);
42cacb20 5077 }
cf43a2fe 5078 p += 64;
42cacb20
DE
5079 }
5080
cf43a2fe
AM
5081 of->write_output_view(off, oview_size, oview);
5082}
5083
f3a0ed29
AM
5084
5085// A class to handle linker generated save/restore functions.
5086
5087template<int size, bool big_endian>
5088class Output_data_save_res : public Output_section_data_build
5089{
5090 public:
5091 Output_data_save_res(Symbol_table* symtab);
5092
d49044c7
AM
5093 const unsigned char*
5094 contents() const
5095 {
5096 return contents_;
5097 }
5098
f3a0ed29
AM
5099 protected:
5100 // Write to a map file.
5101 void
5102 do_print_to_mapfile(Mapfile* mapfile) const
5103 { mapfile->print_output_data(this, _("** save/restore")); }
5104
5105 void
5106 do_write(Output_file*);
5107
5108 private:
5109 // The maximum size of save/restore contents.
5110 static const unsigned int savres_max = 218*4;
5111
5112 void
5113 savres_define(Symbol_table* symtab,
5114 const char *name,
5115 unsigned int lo, unsigned int hi,
5116 unsigned char* write_ent(unsigned char*, int),
5117 unsigned char* write_tail(unsigned char*, int));
5118
5119 unsigned char *contents_;
5120};
5121
5122template<bool big_endian>
5123static unsigned char*
5124savegpr0(unsigned char* p, int r)
5125{
5126 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5127 write_insn<big_endian>(p, insn);
5128 return p + 4;
5129}
5130
5131template<bool big_endian>
5132static unsigned char*
5133savegpr0_tail(unsigned char* p, int r)
5134{
5135 p = savegpr0<big_endian>(p, r);
5136 uint32_t insn = std_0_1 + 16;
5137 write_insn<big_endian>(p, insn);
5138 p = p + 4;
5139 write_insn<big_endian>(p, blr);
5140 return p + 4;
5141}
5142
5143template<bool big_endian>
62fe925a 5144static unsigned char*
f3a0ed29
AM
5145restgpr0(unsigned char* p, int r)
5146{
5147 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5148 write_insn<big_endian>(p, insn);
5149 return p + 4;
5150}
5151
5152template<bool big_endian>
62fe925a 5153static unsigned char*
f3a0ed29
AM
5154restgpr0_tail(unsigned char* p, int r)
5155{
5156 uint32_t insn = ld_0_1 + 16;
5157 write_insn<big_endian>(p, insn);
5158 p = p + 4;
5159 p = restgpr0<big_endian>(p, r);
5160 write_insn<big_endian>(p, mtlr_0);
5161 p = p + 4;
5162 if (r == 29)
5163 {
5164 p = restgpr0<big_endian>(p, 30);
5165 p = restgpr0<big_endian>(p, 31);
5166 }
5167 write_insn<big_endian>(p, blr);
5168 return p + 4;
5169}
5170
5171template<bool big_endian>
62fe925a 5172static unsigned char*
f3a0ed29
AM
5173savegpr1(unsigned char* p, int r)
5174{
5175 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5176 write_insn<big_endian>(p, insn);
5177 return p + 4;
5178}
5179
5180template<bool big_endian>
62fe925a 5181static unsigned char*
f3a0ed29
AM
5182savegpr1_tail(unsigned char* p, int r)
5183{
5184 p = savegpr1<big_endian>(p, r);
5185 write_insn<big_endian>(p, blr);
5186 return p + 4;
5187}
5188
5189template<bool big_endian>
62fe925a 5190static unsigned char*
f3a0ed29
AM
5191restgpr1(unsigned char* p, int r)
5192{
5193 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5194 write_insn<big_endian>(p, insn);
5195 return p + 4;
5196}
5197
5198template<bool big_endian>
62fe925a 5199static unsigned char*
f3a0ed29
AM
5200restgpr1_tail(unsigned char* p, int r)
5201{
5202 p = restgpr1<big_endian>(p, r);
5203 write_insn<big_endian>(p, blr);
5204 return p + 4;
5205}
5206
5207template<bool big_endian>
62fe925a 5208static unsigned char*
f3a0ed29
AM
5209savefpr(unsigned char* p, int r)
5210{
5211 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5212 write_insn<big_endian>(p, insn);
5213 return p + 4;
5214}
5215
5216template<bool big_endian>
62fe925a 5217static unsigned char*
f3a0ed29
AM
5218savefpr0_tail(unsigned char* p, int r)
5219{
5220 p = savefpr<big_endian>(p, r);
5221 write_insn<big_endian>(p, std_0_1 + 16);
5222 p = p + 4;
5223 write_insn<big_endian>(p, blr);
5224 return p + 4;
5225}
5226
5227template<bool big_endian>
62fe925a 5228static unsigned char*
f3a0ed29
AM
5229restfpr(unsigned char* p, int r)
5230{
5231 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5232 write_insn<big_endian>(p, insn);
5233 return p + 4;
5234}
5235
5236template<bool big_endian>
62fe925a 5237static unsigned char*
f3a0ed29
AM
5238restfpr0_tail(unsigned char* p, int r)
5239{
5240 write_insn<big_endian>(p, ld_0_1 + 16);
5241 p = p + 4;
5242 p = restfpr<big_endian>(p, r);
5243 write_insn<big_endian>(p, mtlr_0);
5244 p = p + 4;
5245 if (r == 29)
5246 {
5247 p = restfpr<big_endian>(p, 30);
5248 p = restfpr<big_endian>(p, 31);
5249 }
5250 write_insn<big_endian>(p, blr);
5251 return p + 4;
5252}
5253
5254template<bool big_endian>
62fe925a 5255static unsigned char*
f3a0ed29
AM
5256savefpr1_tail(unsigned char* p, int r)
5257{
5258 p = savefpr<big_endian>(p, r);
5259 write_insn<big_endian>(p, blr);
5260 return p + 4;
5261}
5262
5263template<bool big_endian>
62fe925a 5264static unsigned char*
f3a0ed29
AM
5265restfpr1_tail(unsigned char* p, int r)
5266{
5267 p = restfpr<big_endian>(p, r);
5268 write_insn<big_endian>(p, blr);
5269 return p + 4;
5270}
5271
5272template<bool big_endian>
62fe925a 5273static unsigned char*
f3a0ed29
AM
5274savevr(unsigned char* p, int r)
5275{
5276 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5277 write_insn<big_endian>(p, insn);
5278 p = p + 4;
5279 insn = stvx_0_12_0 + (r << 21);
5280 write_insn<big_endian>(p, insn);
5281 return p + 4;
5282}
5283
5284template<bool big_endian>
62fe925a 5285static unsigned char*
f3a0ed29
AM
5286savevr_tail(unsigned char* p, int r)
5287{
5288 p = savevr<big_endian>(p, r);
5289 write_insn<big_endian>(p, blr);
5290 return p + 4;
5291}
5292
5293template<bool big_endian>
62fe925a 5294static unsigned char*
f3a0ed29
AM
5295restvr(unsigned char* p, int r)
5296{
5297 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5298 write_insn<big_endian>(p, insn);
5299 p = p + 4;
5300 insn = lvx_0_12_0 + (r << 21);
5301 write_insn<big_endian>(p, insn);
5302 return p + 4;
5303}
5304
5305template<bool big_endian>
62fe925a 5306static unsigned char*
f3a0ed29
AM
5307restvr_tail(unsigned char* p, int r)
5308{
5309 p = restvr<big_endian>(p, r);
5310 write_insn<big_endian>(p, blr);
5311 return p + 4;
5312}
5313
5314
5315template<int size, bool big_endian>
5316Output_data_save_res<size, big_endian>::Output_data_save_res(
5317 Symbol_table* symtab)
5318 : Output_section_data_build(4),
5319 contents_(NULL)
5320{
5321 this->savres_define(symtab,
5322 "_savegpr0_", 14, 31,
5323 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5324 this->savres_define(symtab,
5325 "_restgpr0_", 14, 29,
5326 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5327 this->savres_define(symtab,
5328 "_restgpr0_", 30, 31,
5329 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5330 this->savres_define(symtab,
5331 "_savegpr1_", 14, 31,
5332 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5333 this->savres_define(symtab,
5334 "_restgpr1_", 14, 31,
5335 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5336 this->savres_define(symtab,
5337 "_savefpr_", 14, 31,
5338 savefpr<big_endian>, savefpr0_tail<big_endian>);
5339 this->savres_define(symtab,
5340 "_restfpr_", 14, 29,
5341 restfpr<big_endian>, restfpr0_tail<big_endian>);
5342 this->savres_define(symtab,
5343 "_restfpr_", 30, 31,
5344 restfpr<big_endian>, restfpr0_tail<big_endian>);
5345 this->savres_define(symtab,
5346 "._savef", 14, 31,
5347 savefpr<big_endian>, savefpr1_tail<big_endian>);
5348 this->savres_define(symtab,
5349 "._restf", 14, 31,
5350 restfpr<big_endian>, restfpr1_tail<big_endian>);
5351 this->savres_define(symtab,
5352 "_savevr_", 20, 31,
5353 savevr<big_endian>, savevr_tail<big_endian>);
5354 this->savres_define(symtab,
5355 "_restvr_", 20, 31,
5356 restvr<big_endian>, restvr_tail<big_endian>);
5357}
5358
5359template<int size, bool big_endian>
5360void
5361Output_data_save_res<size, big_endian>::savres_define(
5362 Symbol_table* symtab,
5363 const char *name,
5364 unsigned int lo, unsigned int hi,
5365 unsigned char* write_ent(unsigned char*, int),
5366 unsigned char* write_tail(unsigned char*, int))
5367{
5368 size_t len = strlen(name);
5369 bool writing = false;
5370 char sym[16];
5371
5372 memcpy(sym, name, len);
5373 sym[len + 2] = 0;
5374
5375 for (unsigned int i = lo; i <= hi; i++)
5376 {
5377 sym[len + 0] = i / 10 + '0';
5378 sym[len + 1] = i % 10 + '0';
5379 Symbol* gsym = symtab->lookup(sym);
5380 bool refd = gsym != NULL && gsym->is_undefined();
5381 writing = writing || refd;
5382 if (writing)
5383 {
5384 if (this->contents_ == NULL)
5385 this->contents_ = new unsigned char[this->savres_max];
5386
ec661b9d 5387 section_size_type value = this->current_data_size();
f3a0ed29
AM
5388 unsigned char* p = this->contents_ + value;
5389 if (i != hi)
5390 p = write_ent(p, i);
5391 else
5392 p = write_tail(p, i);
ec661b9d 5393 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
5394 this->set_current_data_size(cur_size);
5395 if (refd)
5396 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5397 this, value, cur_size - value,
5398 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5399 elfcpp::STV_HIDDEN, 0, false, false);
5400 }
5401 }
5402}
5403
5404// Write out save/restore.
5405
5406template<int size, bool big_endian>
5407void
5408Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5409{
ec661b9d 5410 const section_size_type off = this->offset();
f3a0ed29
AM
5411 const section_size_type oview_size =
5412 convert_to_section_size_type(this->data_size());
5413 unsigned char* const oview = of->get_output_view(off, oview_size);
5414 memcpy(oview, this->contents_, oview_size);
5415 of->write_output_view(off, oview_size, oview);
5416}
5417
5418
cf43a2fe 5419// Create the glink section.
42cacb20 5420
cf43a2fe
AM
5421template<int size, bool big_endian>
5422void
5423Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5424{
5425 if (this->glink_ == NULL)
5426 {
5427 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 5428 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
5429 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5430 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5431 this->glink_, ORDER_TEXT, false);
5432 }
42cacb20
DE
5433}
5434
5435// Create a PLT entry for a global symbol.
5436
5437template<int size, bool big_endian>
5438void
ec661b9d
AM
5439Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5440 Layout* layout,
5441 Symbol* gsym)
42cacb20 5442{
e5d5f5ed
AM
5443 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5444 && gsym->can_use_relative_reloc(false))
5445 {
5446 if (this->iplt_ == NULL)
40b469d7 5447 this->make_iplt_section(symtab, layout);
03e25981 5448 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
5449 }
5450 else
5451 {
5452 if (this->plt_ == NULL)
40b469d7 5453 this->make_plt_section(symtab, layout);
03e25981 5454 this->plt_->add_entry(gsym);
e5d5f5ed 5455 }
e5d5f5ed 5456}
42cacb20 5457
e5d5f5ed 5458// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 5459
e5d5f5ed
AM
5460template<int size, bool big_endian>
5461void
5462Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 5463 Symbol_table* symtab,
e5d5f5ed 5464 Layout* layout,
ec661b9d
AM
5465 Sized_relobj_file<size, big_endian>* relobj,
5466 unsigned int r_sym)
e5d5f5ed
AM
5467{
5468 if (this->iplt_ == NULL)
40b469d7 5469 this->make_iplt_section(symtab, layout);
03e25981 5470 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
5471}
5472
0e70b911
CC
5473// Return the number of entries in the PLT.
5474
5475template<int size, bool big_endian>
5476unsigned int
5477Target_powerpc<size, big_endian>::plt_entry_count() const
5478{
5479 if (this->plt_ == NULL)
5480 return 0;
b3ccdeb5 5481 return this->plt_->entry_count();
0e70b911
CC
5482}
5483
dd93cd0a 5484// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
5485
5486template<int size, bool big_endian>
5487unsigned int
dd93cd0a 5488Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
5489 Symbol_table* symtab,
5490 Layout* layout,
5491 Sized_relobj_file<size, big_endian>* object)
42cacb20 5492{
dd93cd0a 5493 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
5494 {
5495 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5496 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
5497 Output_data_got_powerpc<size, big_endian>* got
5498 = this->got_section(symtab, layout);
5499 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
5500 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5501 got_offset, 0);
dd93cd0a 5502 this->tlsld_got_offset_ = got_offset;
42cacb20 5503 }
dd93cd0a 5504 return this->tlsld_got_offset_;
42cacb20
DE
5505}
5506
95a2c8d6
RS
5507// Get the Reference_flags for a particular relocation.
5508
5509template<int size, bool big_endian>
5510int
88b8e639
AM
5511Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5512 unsigned int r_type,
5513 const Target_powerpc* target)
95a2c8d6 5514{
88b8e639
AM
5515 int ref = 0;
5516
95a2c8d6
RS
5517 switch (r_type)
5518 {
5519 case elfcpp::R_POWERPC_NONE:
5520 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5521 case elfcpp::R_POWERPC_GNU_VTENTRY:
5522 case elfcpp::R_PPC64_TOC:
5523 // No symbol reference.
88b8e639 5524 break;
95a2c8d6 5525
dd93cd0a
AM
5526 case elfcpp::R_PPC64_ADDR64:
5527 case elfcpp::R_PPC64_UADDR64:
5528 case elfcpp::R_POWERPC_ADDR32:
5529 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 5530 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 5531 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
5532 case elfcpp::R_POWERPC_ADDR16_LO:
5533 case elfcpp::R_POWERPC_ADDR16_HI:
5534 case elfcpp::R_POWERPC_ADDR16_HA:
88b8e639
AM
5535 ref = Symbol::ABSOLUTE_REF;
5536 break;
95a2c8d6 5537
dd93cd0a
AM
5538 case elfcpp::R_POWERPC_ADDR24:
5539 case elfcpp::R_POWERPC_ADDR14:
5540 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5541 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
5542 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5543 break;
dd93cd0a 5544
e5d5f5ed 5545 case elfcpp::R_PPC64_REL64:
dd93cd0a 5546 case elfcpp::R_POWERPC_REL32:
95a2c8d6 5547 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
5548 case elfcpp::R_POWERPC_REL16:
5549 case elfcpp::R_POWERPC_REL16_LO:
5550 case elfcpp::R_POWERPC_REL16_HI:
5551 case elfcpp::R_POWERPC_REL16_HA:
88b8e639
AM
5552 ref = Symbol::RELATIVE_REF;
5553 break;
95a2c8d6 5554
dd93cd0a 5555 case elfcpp::R_POWERPC_REL24:
95a2c8d6 5556 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
5557 case elfcpp::R_POWERPC_REL14:
5558 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5559 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
5560 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5561 break;
95a2c8d6
RS
5562
5563 case elfcpp::R_POWERPC_GOT16:
5564 case elfcpp::R_POWERPC_GOT16_LO:
5565 case elfcpp::R_POWERPC_GOT16_HI:
5566 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
5567 case elfcpp::R_PPC64_GOT16_DS:
5568 case elfcpp::R_PPC64_GOT16_LO_DS:
95a2c8d6
RS
5569 case elfcpp::R_PPC64_TOC16:
5570 case elfcpp::R_PPC64_TOC16_LO:
5571 case elfcpp::R_PPC64_TOC16_HI:
5572 case elfcpp::R_PPC64_TOC16_HA:
5573 case elfcpp::R_PPC64_TOC16_DS:
5574 case elfcpp::R_PPC64_TOC16_LO_DS:
32d849b3 5575 ref = Symbol::RELATIVE_REF;
88b8e639 5576 break;
95a2c8d6
RS
5577
5578 case elfcpp::R_POWERPC_GOT_TPREL16:
5579 case elfcpp::R_POWERPC_TLS:
88b8e639
AM
5580 ref = Symbol::TLS_REF;
5581 break;
95a2c8d6
RS
5582
5583 case elfcpp::R_POWERPC_COPY:
5584 case elfcpp::R_POWERPC_GLOB_DAT:
5585 case elfcpp::R_POWERPC_JMP_SLOT:
5586 case elfcpp::R_POWERPC_RELATIVE:
5587 case elfcpp::R_POWERPC_DTPMOD:
5588 default:
5589 // Not expected. We will give an error later.
88b8e639 5590 break;
95a2c8d6 5591 }
88b8e639
AM
5592
5593 if (size == 64 && target->abiversion() < 2)
5594 ref |= Symbol::FUNC_DESC_ABI;
5595 return ref;
95a2c8d6
RS
5596}
5597
42cacb20
DE
5598// Report an unsupported relocation against a local symbol.
5599
5600template<int size, bool big_endian>
5601void
5602Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
5603 Sized_relobj_file<size, big_endian>* object,
5604 unsigned int r_type)
42cacb20
DE
5605{
5606 gold_error(_("%s: unsupported reloc %u against local symbol"),
5607 object->name().c_str(), r_type);
5608}
5609
5610// We are about to emit a dynamic relocation of type R_TYPE. If the
5611// dynamic linker does not support it, issue an error.
5612
5613template<int size, bool big_endian>
5614void
5615Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5616 unsigned int r_type)
5617{
5618 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5619
5620 // These are the relocation types supported by glibc for both 32-bit
5621 // and 64-bit powerpc.
5622 switch (r_type)
5623 {
3ea0a085 5624 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
5625 case elfcpp::R_POWERPC_RELATIVE:
5626 case elfcpp::R_POWERPC_GLOB_DAT:
5627 case elfcpp::R_POWERPC_DTPMOD:
5628 case elfcpp::R_POWERPC_DTPREL:
5629 case elfcpp::R_POWERPC_TPREL:
5630 case elfcpp::R_POWERPC_JMP_SLOT:
5631 case elfcpp::R_POWERPC_COPY:
3ea0a085 5632 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 5633 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 5634 case elfcpp::R_POWERPC_UADDR32:
42cacb20 5635 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
5636 case elfcpp::R_POWERPC_ADDR16:
5637 case elfcpp::R_POWERPC_UADDR16:
5638 case elfcpp::R_POWERPC_ADDR16_LO:
5639 case elfcpp::R_POWERPC_ADDR16_HI:
5640 case elfcpp::R_POWERPC_ADDR16_HA:
5641 case elfcpp::R_POWERPC_ADDR14:
5642 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5643 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5644 case elfcpp::R_POWERPC_REL32:
42cacb20 5645 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
5646 case elfcpp::R_POWERPC_TPREL16:
5647 case elfcpp::R_POWERPC_TPREL16_LO:
5648 case elfcpp::R_POWERPC_TPREL16_HI:
5649 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
5650 return;
5651
5652 default:
5653 break;
5654 }
5655
5656 if (size == 64)
5657 {
5658 switch (r_type)
5659 {
5660 // These are the relocation types supported only on 64-bit.
5661 case elfcpp::R_PPC64_ADDR64:
42cacb20 5662 case elfcpp::R_PPC64_UADDR64:
3ea0a085 5663 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 5664 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 5665 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
5666 case elfcpp::R_PPC64_ADDR16_HIGH:
5667 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
5668 case elfcpp::R_PPC64_ADDR16_HIGHER:
5669 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5670 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5671 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 5672 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
5673 case elfcpp::R_POWERPC_ADDR30:
5674 case elfcpp::R_PPC64_TPREL16_DS:
5675 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5676 case elfcpp::R_PPC64_TPREL16_HIGH:
5677 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
5678 case elfcpp::R_PPC64_TPREL16_HIGHER:
5679 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5680 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5681 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
5682 return;
5683
5684 default:
5685 break;
5686 }
5687 }
5688 else
5689 {
5690 switch (r_type)
5691 {
5692 // These are the relocation types supported only on 32-bit.
3ea0a085
AM
5693 // ??? glibc ld.so doesn't need to support these.
5694 case elfcpp::R_POWERPC_DTPREL16:
5695 case elfcpp::R_POWERPC_DTPREL16_LO:
5696 case elfcpp::R_POWERPC_DTPREL16_HI:
5697 case elfcpp::R_POWERPC_DTPREL16_HA:
5698 return;
42cacb20
DE
5699
5700 default:
5701 break;
5702 }
5703 }
5704
5705 // This prevents us from issuing more than one error per reloc
5706 // section. But we can still wind up issuing more than one
5707 // error per object file.
5708 if (this->issued_non_pic_error_)
5709 return;
33aea2fd 5710 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
5711 object->error(_("requires unsupported dynamic reloc; "
5712 "recompile with -fPIC"));
5713 this->issued_non_pic_error_ = true;
5714 return;
5715}
5716
e5d5f5ed
AM
5717// Return whether we need to make a PLT entry for a relocation of the
5718// given type against a STT_GNU_IFUNC symbol.
5719
5720template<int size, bool big_endian>
5721bool
5722Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 5723 Target_powerpc<size, big_endian>* target,
e5d5f5ed 5724 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
5725 unsigned int r_type,
5726 bool report_err)
e5d5f5ed 5727{
c9824451
AM
5728 // In non-pic code any reference will resolve to the plt call stub
5729 // for the ifunc symbol.
9055360d
AM
5730 if ((size == 32 || target->abiversion() >= 2)
5731 && !parameters->options().output_is_position_independent())
c9824451
AM
5732 return true;
5733
e5d5f5ed
AM
5734 switch (r_type)
5735 {
b3ccdeb5 5736 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
5737 case elfcpp::R_POWERPC_ADDR32:
5738 case elfcpp::R_POWERPC_UADDR32:
5739 if (size == 32)
b3ccdeb5 5740 return false;
e5d5f5ed
AM
5741 break;
5742
5743 case elfcpp::R_PPC64_ADDR64:
5744 case elfcpp::R_PPC64_UADDR64:
5745 if (size == 64)
b3ccdeb5 5746 return false;
e5d5f5ed
AM
5747 break;
5748
b3ccdeb5 5749 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
5750 case elfcpp::R_POWERPC_GOT16:
5751 case elfcpp::R_POWERPC_GOT16_LO:
5752 case elfcpp::R_POWERPC_GOT16_HI:
5753 case elfcpp::R_POWERPC_GOT16_HA:
5754 case elfcpp::R_PPC64_GOT16_DS:
5755 case elfcpp::R_PPC64_GOT16_LO_DS:
b3ccdeb5 5756 return false;
e5d5f5ed 5757
b3ccdeb5 5758 // Function calls are good, and these do need a PLT entry.
e5d5f5ed
AM
5759 case elfcpp::R_POWERPC_ADDR24:
5760 case elfcpp::R_POWERPC_ADDR14:
5761 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5762 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5763 case elfcpp::R_POWERPC_REL24:
5764 case elfcpp::R_PPC_PLTREL24:
5765 case elfcpp::R_POWERPC_REL14:
5766 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5767 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5768 return true;
5769
5770 default:
5771 break;
5772 }
5773
5774 // Anything else is a problem.
5775 // If we are building a static executable, the libc startup function
5776 // responsible for applying indirect function relocations is going
5777 // to complain about the reloc type.
5778 // If we are building a dynamic executable, we will have a text
5779 // relocation. The dynamic loader will set the text segment
5780 // writable and non-executable to apply text relocations. So we'll
5781 // segfault when trying to run the indirection function to resolve
5782 // the reloc.
b3ccdeb5
AM
5783 if (report_err)
5784 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
5785 object->name().c_str(), r_type);
5786 return false;
5787}
5788
5edad15d
AM
5789// Return TRUE iff INSN is one we expect on a _LO variety toc/got
5790// reloc.
5791
5792static bool
5793ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
5794{
5795 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
5796 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
5797 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
5798 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
5799 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
5800 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
5801 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
5802 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
5803 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
5804 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
5805 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
5806 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
5807 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
5808 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
5809 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
5810 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
5811 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
5812 /* Exclude lfqu by testing reloc. If relocs are ever
5813 defined for the reduced D field in psq_lu then those
5814 will need testing too. */
5815 && r_type != elfcpp::R_PPC64_TOC16_LO
5816 && r_type != elfcpp::R_POWERPC_GOT16_LO)
5817 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
5818 && (insn & 1) == 0)
5819 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
5820 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
5821 /* Exclude stfqu. psq_stu as above for psq_lu. */
5822 && r_type != elfcpp::R_PPC64_TOC16_LO
5823 && r_type != elfcpp::R_POWERPC_GOT16_LO)
5824 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
5825 && (insn & 1) == 0));
5826}
5827
42cacb20
DE
5828// Scan a relocation for a local symbol.
5829
5830template<int size, bool big_endian>
5831inline void
5832Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
5833 Symbol_table* symtab,
5834 Layout* layout,
5835 Target_powerpc<size, big_endian>* target,
5836 Sized_relobj_file<size, big_endian>* object,
5837 unsigned int data_shndx,
5838 Output_section* output_section,
5839 const elfcpp::Rela<size, big_endian>& reloc,
5840 unsigned int r_type,
e5d5f5ed 5841 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 5842 bool is_discarded)
42cacb20 5843{
e3deeb9c
AM
5844 this->maybe_skip_tls_get_addr_call(r_type, NULL);
5845
5846 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5847 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5848 {
5849 this->expect_tls_get_addr_call();
5850 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5851 if (tls_type != tls::TLSOPT_NONE)
5852 this->skip_next_tls_get_addr_call();
5853 }
5854 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5855 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5856 {
5857 this->expect_tls_get_addr_call();
5858 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5859 if (tls_type != tls::TLSOPT_NONE)
5860 this->skip_next_tls_get_addr_call();
5861 }
5862
dd93cd0a
AM
5863 Powerpc_relobj<size, big_endian>* ppc_object
5864 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5865
bfdfa4cd
AM
5866 if (is_discarded)
5867 {
5868 if (size == 64
5869 && data_shndx == ppc_object->opd_shndx()
5870 && r_type == elfcpp::R_PPC64_ADDR64)
5871 ppc_object->set_opd_discard(reloc.get_r_offset());
5872 return;
5873 }
5874
e5d5f5ed
AM
5875 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5876 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 5877 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 5878 {
ec661b9d
AM
5879 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5880 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5881 r_type, r_sym, reloc.get_r_addend());
5882 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 5883 }
e5d5f5ed 5884
42cacb20
DE
5885 switch (r_type)
5886 {
5887 case elfcpp::R_POWERPC_NONE:
5888 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5889 case elfcpp::R_POWERPC_GNU_VTENTRY:
6ce78956 5890 case elfcpp::R_PPC64_TOCSAVE:
7404fe1b 5891 case elfcpp::R_POWERPC_TLS:
549dba71 5892 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
5893 break;
5894
5895 case elfcpp::R_PPC64_TOC:
5896 {
5897 Output_data_got_powerpc<size, big_endian>* got
5898 = target->got_section(symtab, layout);
5899 if (parameters->options().output_is_position_independent())
5900 {
bfdfa4cd
AM
5901 Address off = reloc.get_r_offset();
5902 if (size == 64
9055360d 5903 && target->abiversion() < 2
bfdfa4cd
AM
5904 && data_shndx == ppc_object->opd_shndx()
5905 && ppc_object->get_opd_discard(off - 8))
5906 break;
5907
dd93cd0a 5908 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 5909 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
5910 rela_dyn->add_output_section_relative(got->output_section(),
5911 elfcpp::R_POWERPC_RELATIVE,
5912 output_section,
bfdfa4cd
AM
5913 object, data_shndx, off,
5914 symobj->toc_base_offset());
dd93cd0a
AM
5915 }
5916 }
42cacb20
DE
5917 break;
5918
5919 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 5920 case elfcpp::R_PPC64_UADDR64:
42cacb20 5921 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
5922 case elfcpp::R_POWERPC_UADDR32:
5923 case elfcpp::R_POWERPC_ADDR24:
c9269dff 5924 case elfcpp::R_POWERPC_ADDR16:
42cacb20 5925 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
5926 case elfcpp::R_POWERPC_ADDR16_HI:
5927 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 5928 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
5929 case elfcpp::R_PPC64_ADDR16_HIGH:
5930 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
5931 case elfcpp::R_PPC64_ADDR16_HIGHER:
5932 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5933 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5934 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5935 case elfcpp::R_PPC64_ADDR16_DS:
5936 case elfcpp::R_PPC64_ADDR16_LO_DS:
5937 case elfcpp::R_POWERPC_ADDR14:
5938 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5939 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
5940 // If building a shared library (or a position-independent
5941 // executable), we need to create a dynamic relocation for
5942 // this location.
c9824451 5943 if (parameters->options().output_is_position_independent()
9055360d 5944 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 5945 {
b3ccdeb5
AM
5946 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5947 is_ifunc);
1f98a074 5948 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
5949 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5950 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 5951 {
b3ccdeb5
AM
5952 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5953 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 5954 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
5955 output_section, data_shndx,
5956 reloc.get_r_offset(),
c9824451 5957 reloc.get_r_addend(), false);
2e702c99 5958 }
1f98a074 5959 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 5960 {
dd93cd0a 5961 check_non_pic(object, r_type);
dd93cd0a
AM
5962 rela_dyn->add_local(object, r_sym, r_type, output_section,
5963 data_shndx, reloc.get_r_offset(),
5964 reloc.get_r_addend());
2e702c99 5965 }
1f98a074
AM
5966 else
5967 {
5968 gold_assert(lsym.get_st_value() == 0);
5969 unsigned int shndx = lsym.get_st_shndx();
5970 bool is_ordinary;
5971 shndx = object->adjust_sym_shndx(r_sym, shndx,
5972 &is_ordinary);
5973 if (!is_ordinary)
5974 object->error(_("section symbol %u has bad shndx %u"),
5975 r_sym, shndx);
5976 else
5977 rela_dyn->add_local_section(object, shndx, r_type,
5978 output_section, data_shndx,
5979 reloc.get_r_offset());
5980 }
2e702c99 5981 }
42cacb20
DE
5982 break;
5983
5984 case elfcpp::R_POWERPC_REL24:
c9824451 5985 case elfcpp::R_PPC_PLTREL24:
42cacb20 5986 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
5987 case elfcpp::R_POWERPC_REL14:
5988 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5989 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 5990 if (!is_ifunc)
0e123f69
AM
5991 {
5992 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5993 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5994 r_type, r_sym, reloc.get_r_addend());
5995 }
ec661b9d
AM
5996 break;
5997
5998 case elfcpp::R_PPC64_REL64:
5999 case elfcpp::R_POWERPC_REL32:
dd93cd0a 6000 case elfcpp::R_POWERPC_REL16:
6ce78956 6001 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 6002 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 6003 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 6004 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 6005 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 6006 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 6007 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 6008 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
6009 case elfcpp::R_PPC64_SECTOFF_DS:
6010 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6011 case elfcpp::R_POWERPC_TPREL16:
6012 case elfcpp::R_POWERPC_TPREL16_LO:
6013 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 6014 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
6015 case elfcpp::R_PPC64_TPREL16_DS:
6016 case elfcpp::R_PPC64_TPREL16_LO_DS:
6017 case elfcpp::R_PPC64_TPREL16_HIGH:
6018 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 6019 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 6020 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 6021 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 6022 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
6023 case elfcpp::R_POWERPC_DTPREL16:
6024 case elfcpp::R_POWERPC_DTPREL16_LO:
6025 case elfcpp::R_POWERPC_DTPREL16_HI:
6026 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
6027 case elfcpp::R_PPC64_DTPREL16_DS:
6028 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
6029 case elfcpp::R_PPC64_DTPREL16_HIGH:
6030 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6031 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6032 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6033 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6034 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
6035 case elfcpp::R_PPC64_TLSGD:
6036 case elfcpp::R_PPC64_TLSLD:
45965137 6037 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
6038 break;
6039
6040 case elfcpp::R_POWERPC_GOT16:
6041 case elfcpp::R_POWERPC_GOT16_LO:
6042 case elfcpp::R_POWERPC_GOT16_HI:
6043 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
6044 case elfcpp::R_PPC64_GOT16_DS:
6045 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 6046 {
c9269dff 6047 // The symbol requires a GOT entry.
dd93cd0a
AM
6048 Output_data_got_powerpc<size, big_endian>* got
6049 = target->got_section(symtab, layout);
6050 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 6051
e5d5f5ed 6052 if (!parameters->options().output_is_position_independent())
42cacb20 6053 {
b01a4b04
AM
6054 if (is_ifunc
6055 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
6056 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6057 else
6058 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6059 }
6060 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6061 {
6062 // If we are generating a shared object or a pie, this
6063 // symbol's GOT entry will be set by a dynamic relocation.
6064 unsigned int off;
6065 off = got->add_constant(0);
6066 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 6067
b3ccdeb5
AM
6068 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6069 is_ifunc);
6070 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6071 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 6072 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 6073 got, off, 0, false);
2e702c99 6074 }
42cacb20
DE
6075 }
6076 break;
6077
cf43a2fe
AM
6078 case elfcpp::R_PPC64_TOC16:
6079 case elfcpp::R_PPC64_TOC16_LO:
6080 case elfcpp::R_PPC64_TOC16_HI:
6081 case elfcpp::R_PPC64_TOC16_HA:
6082 case elfcpp::R_PPC64_TOC16_DS:
6083 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
6084 // We need a GOT section.
6085 target->got_section(symtab, layout);
6086 break;
6087
dd93cd0a
AM
6088 case elfcpp::R_POWERPC_GOT_TLSGD16:
6089 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6090 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6091 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6092 {
6093 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6094 if (tls_type == tls::TLSOPT_NONE)
6095 {
6096 Output_data_got_powerpc<size, big_endian>* got
6097 = target->got_section(symtab, layout);
6098 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
6099 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6100 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6101 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
6102 }
6103 else if (tls_type == tls::TLSOPT_TO_LE)
6104 {
6105 // no GOT relocs needed for Local Exec.
6106 }
6107 else
6108 gold_unreachable();
6109 }
42cacb20
DE
6110 break;
6111
dd93cd0a
AM
6112 case elfcpp::R_POWERPC_GOT_TLSLD16:
6113 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6114 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6115 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6116 {
6117 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6118 if (tls_type == tls::TLSOPT_NONE)
6119 target->tlsld_got_offset(symtab, layout, object);
6120 else if (tls_type == tls::TLSOPT_TO_LE)
6121 {
6122 // no GOT relocs needed for Local Exec.
7404fe1b
AM
6123 if (parameters->options().emit_relocs())
6124 {
6125 Output_section* os = layout->tls_segment()->first_section();
6126 gold_assert(os != NULL);
6127 os->set_needs_symtab_index();
6128 }
dd93cd0a
AM
6129 }
6130 else
6131 gold_unreachable();
6132 }
42cacb20 6133 break;
42cacb20 6134
dd93cd0a
AM
6135 case elfcpp::R_POWERPC_GOT_DTPREL16:
6136 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6137 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6138 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6139 {
6140 Output_data_got_powerpc<size, big_endian>* got
6141 = target->got_section(symtab, layout);
6142 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 6143 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
6144 }
6145 break;
42cacb20 6146
dd93cd0a
AM
6147 case elfcpp::R_POWERPC_GOT_TPREL16:
6148 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6149 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6150 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6151 {
6152 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6153 if (tls_type == tls::TLSOPT_NONE)
6154 {
dd93cd0a 6155 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
6156 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6157 {
6158 Output_data_got_powerpc<size, big_endian>* got
6159 = target->got_section(symtab, layout);
6160 unsigned int off = got->add_constant(0);
6161 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6162
6163 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6164 rela_dyn->add_symbolless_local_addend(object, r_sym,
6165 elfcpp::R_POWERPC_TPREL,
6166 got, off, 0);
6167 }
dd93cd0a
AM
6168 }
6169 else if (tls_type == tls::TLSOPT_TO_LE)
6170 {
6171 // no GOT relocs needed for Local Exec.
6172 }
6173 else
6174 gold_unreachable();
6175 }
6176 break;
6177
6178 default:
6179 unsupported_reloc_local(object, r_type);
6180 break;
6181 }
d8f5a274 6182
5edad15d
AM
6183 if (size == 64
6184 && parameters->options().toc_optimize())
6185 {
6186 if (data_shndx == ppc_object->toc_shndx())
6187 {
6188 bool ok = true;
6189 if (r_type != elfcpp::R_PPC64_ADDR64
6190 || (is_ifunc && target->abiversion() < 2))
6191 ok = false;
6192 else if (parameters->options().output_is_position_independent())
6193 {
6194 if (is_ifunc)
6195 ok = false;
6196 else
6197 {
6198 unsigned int shndx = lsym.get_st_shndx();
6199 if (shndx >= elfcpp::SHN_LORESERVE
6200 && shndx != elfcpp::SHN_XINDEX)
6201 ok = false;
6202 }
6203 }
6204 if (!ok)
6205 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6206 }
6207
6208 enum {no_check, check_lo, check_ha} insn_check;
6209 switch (r_type)
6210 {
6211 default:
6212 insn_check = no_check;
6213 break;
6214
6215 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6216 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6217 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6218 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6219 case elfcpp::R_POWERPC_GOT16_HA:
6220 case elfcpp::R_PPC64_TOC16_HA:
6221 insn_check = check_ha;
6222 break;
6223
6224 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6225 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6226 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6227 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6228 case elfcpp::R_POWERPC_GOT16_LO:
6229 case elfcpp::R_PPC64_GOT16_LO_DS:
6230 case elfcpp::R_PPC64_TOC16_LO:
6231 case elfcpp::R_PPC64_TOC16_LO_DS:
6232 insn_check = check_lo;
6233 break;
6234 }
6235
6236 section_size_type slen;
6237 const unsigned char* view = NULL;
6238 if (insn_check != no_check)
6239 {
6240 view = ppc_object->section_contents(data_shndx, &slen, false);
6241 section_size_type off =
6242 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6243 if (off < slen)
6244 {
6245 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6246 if (insn_check == check_lo
6247 ? !ok_lo_toc_insn(insn, r_type)
6248 : ((insn & ((0x3f << 26) | 0x1f << 16))
6249 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6250 {
6251 ppc_object->set_no_toc_opt();
6252 gold_warning(_("%s: toc optimization is not supported "
6253 "for %#08x instruction"),
6254 ppc_object->name().c_str(), insn);
6255 }
6256 }
6257 }
6258
6259 switch (r_type)
6260 {
6261 default:
6262 break;
6263 case elfcpp::R_PPC64_TOC16:
6264 case elfcpp::R_PPC64_TOC16_LO:
6265 case elfcpp::R_PPC64_TOC16_HI:
6266 case elfcpp::R_PPC64_TOC16_HA:
6267 case elfcpp::R_PPC64_TOC16_DS:
6268 case elfcpp::R_PPC64_TOC16_LO_DS:
6269 unsigned int shndx = lsym.get_st_shndx();
6270 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6271 bool is_ordinary;
6272 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6273 if (is_ordinary && shndx == ppc_object->toc_shndx())
6274 {
6275 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6276 if (dst_off < ppc_object->section_size(shndx))
6277 {
6278 bool ok = false;
6279 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6280 ok = true;
6281 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6282 {
6283 // Need to check that the insn is a ld
6284 if (!view)
6285 view = ppc_object->section_contents(data_shndx,
6286 &slen,
6287 false);
6288 section_size_type off =
6289 (convert_to_section_size_type(reloc.get_r_offset())
6290 + (big_endian ? -2 : 3));
6291 if (off < slen
6292 && (view[off] & (0x3f << 2)) == 58u << 2)
6293 ok = true;
6294 }
6295 if (!ok)
6296 ppc_object->set_no_toc_opt(dst_off);
6297 }
6298 }
6299 break;
6300 }
6301 }
6302
f159cdb6
AM
6303 if (size == 32)
6304 {
6305 switch (r_type)
6306 {
6307 case elfcpp::R_POWERPC_REL32:
6308 if (ppc_object->got2_shndx() != 0
6309 && parameters->options().output_is_position_independent())
6310 {
6311 unsigned int shndx = lsym.get_st_shndx();
6312 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6313 bool is_ordinary;
6314 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6315 if (is_ordinary && shndx == ppc_object->got2_shndx()
6316 && (ppc_object->section_flags(data_shndx)
6317 & elfcpp::SHF_EXECINSTR) != 0)
6318 gold_error(_("%s: unsupported -mbss-plt code"),
6319 ppc_object->name().c_str());
6320 }
6321 break;
6322 default:
6323 break;
6324 }
6325 }
6326
d8f5a274
AM
6327 switch (r_type)
6328 {
6329 case elfcpp::R_POWERPC_GOT_TLSLD16:
6330 case elfcpp::R_POWERPC_GOT_TLSGD16:
6331 case elfcpp::R_POWERPC_GOT_TPREL16:
6332 case elfcpp::R_POWERPC_GOT_DTPREL16:
6333 case elfcpp::R_POWERPC_GOT16:
6334 case elfcpp::R_PPC64_GOT16_DS:
6335 case elfcpp::R_PPC64_TOC16:
6336 case elfcpp::R_PPC64_TOC16_DS:
6337 ppc_object->set_has_small_toc_reloc();
6338 default:
6339 break;
6340 }
dd93cd0a
AM
6341}
6342
6343// Report an unsupported relocation against a global symbol.
6344
6345template<int size, bool big_endian>
6346void
6347Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6348 Sized_relobj_file<size, big_endian>* object,
6349 unsigned int r_type,
6350 Symbol* gsym)
6351{
42cacb20
DE
6352 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6353 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6354}
6355
6356// Scan a relocation for a global symbol.
6357
6358template<int size, bool big_endian>
6359inline void
6360Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
6361 Symbol_table* symtab,
6362 Layout* layout,
6363 Target_powerpc<size, big_endian>* target,
6364 Sized_relobj_file<size, big_endian>* object,
6365 unsigned int data_shndx,
6366 Output_section* output_section,
6367 const elfcpp::Rela<size, big_endian>& reloc,
6368 unsigned int r_type,
6369 Symbol* gsym)
42cacb20 6370{
e3deeb9c
AM
6371 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6372 return;
6373
6374 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6375 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6376 {
6377 this->expect_tls_get_addr_call();
6378 const bool final = gsym->final_value_is_known();
6379 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6380 if (tls_type != tls::TLSOPT_NONE)
6381 this->skip_next_tls_get_addr_call();
6382 }
6383 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6384 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6385 {
6386 this->expect_tls_get_addr_call();
6387 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6388 if (tls_type != tls::TLSOPT_NONE)
6389 this->skip_next_tls_get_addr_call();
6390 }
6391
dd93cd0a
AM
6392 Powerpc_relobj<size, big_endian>* ppc_object
6393 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6394
e5d5f5ed 6395 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 6396 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
6397 bool pushed_ifunc = false;
6398 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 6399 {
0e123f69 6400 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 6401 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 6402 r_type, r_sym, reloc.get_r_addend());
ec661b9d 6403 target->make_plt_entry(symtab, layout, gsym);
9055360d 6404 pushed_ifunc = true;
ec661b9d 6405 }
e5d5f5ed 6406
42cacb20
DE
6407 switch (r_type)
6408 {
6409 case elfcpp::R_POWERPC_NONE:
6410 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6411 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 6412 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 6413 case elfcpp::R_POWERPC_TLS:
549dba71 6414 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
6415 break;
6416
6417 case elfcpp::R_PPC64_TOC:
6418 {
6419 Output_data_got_powerpc<size, big_endian>* got
6420 = target->got_section(symtab, layout);
6421 if (parameters->options().output_is_position_independent())
6422 {
bfdfa4cd
AM
6423 Address off = reloc.get_r_offset();
6424 if (size == 64
6425 && data_shndx == ppc_object->opd_shndx()
6426 && ppc_object->get_opd_discard(off - 8))
6427 break;
6428
dd93cd0a
AM
6429 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6430 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6431 if (data_shndx != ppc_object->opd_shndx())
6432 symobj = static_cast
6433 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6434 rela_dyn->add_output_section_relative(got->output_section(),
6435 elfcpp::R_POWERPC_RELATIVE,
6436 output_section,
bfdfa4cd 6437 object, data_shndx, off,
dd93cd0a
AM
6438 symobj->toc_base_offset());
6439 }
6440 }
42cacb20
DE
6441 break;
6442
c9269dff 6443 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 6444 if (size == 64
9055360d 6445 && target->abiversion() < 2
bfdfa4cd
AM
6446 && data_shndx == ppc_object->opd_shndx()
6447 && (gsym->is_defined_in_discarded_section()
6448 || gsym->object() != object))
6449 {
6450 ppc_object->set_opd_discard(reloc.get_r_offset());
6451 break;
6452 }
d8e90251 6453 // Fall through.
dd93cd0a 6454 case elfcpp::R_PPC64_UADDR64:
c9269dff 6455 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
6456 case elfcpp::R_POWERPC_UADDR32:
6457 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
6458 case elfcpp::R_POWERPC_ADDR16:
6459 case elfcpp::R_POWERPC_ADDR16_LO:
6460 case elfcpp::R_POWERPC_ADDR16_HI:
6461 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 6462 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
6463 case elfcpp::R_PPC64_ADDR16_HIGH:
6464 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
6465 case elfcpp::R_PPC64_ADDR16_HIGHER:
6466 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6467 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6468 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6469 case elfcpp::R_PPC64_ADDR16_DS:
6470 case elfcpp::R_PPC64_ADDR16_LO_DS:
6471 case elfcpp::R_POWERPC_ADDR14:
6472 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6473 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 6474 {
c9269dff
AM
6475 // Make a PLT entry if necessary.
6476 if (gsym->needs_plt_entry())
6477 {
9055360d
AM
6478 // Since this is not a PC-relative relocation, we may be
6479 // taking the address of a function. In that case we need to
6480 // set the entry in the dynamic symbol table to the address of
6481 // the PLT call stub.
6482 bool need_ifunc_plt = false;
6483 if ((size == 32 || target->abiversion() >= 2)
6484 && gsym->is_from_dynobj()
6485 && !parameters->options().output_is_position_independent())
6486 {
6487 gsym->set_needs_dynsym_value();
6488 need_ifunc_plt = true;
6489 }
6490 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 6491 {
0e123f69 6492 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 6493 target->push_branch(ppc_object, data_shndx,
0e123f69 6494 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
6495 reloc.get_r_addend());
6496 target->make_plt_entry(symtab, layout, gsym);
6497 }
c9269dff
AM
6498 }
6499 // Make a dynamic relocation if necessary.
88b8e639 6500 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 6501 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 6502 {
a82bef93
ST
6503 if (!parameters->options().output_is_position_independent()
6504 && gsym->may_need_copy_reloc())
c9269dff
AM
6505 {
6506 target->copy_reloc(symtab, layout, object,
6507 data_shndx, output_section, gsym, reloc);
6508 }
9055360d
AM
6509 else if ((((size == 32
6510 && r_type == elfcpp::R_POWERPC_ADDR32)
6511 || (size == 64
6512 && r_type == elfcpp::R_PPC64_ADDR64
6513 && target->abiversion() >= 2))
627b30b7
AM
6514 && gsym->can_use_relative_reloc(false)
6515 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6516 && parameters->options().shared()))
6517 || (size == 64
6518 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 6519 && target->abiversion() < 2
627b30b7
AM
6520 && (gsym->can_use_relative_reloc(false)
6521 || data_shndx == ppc_object->opd_shndx())))
2e702c99 6522 {
b3ccdeb5
AM
6523 Reloc_section* rela_dyn
6524 = target->rela_dyn_section(symtab, layout, is_ifunc);
6525 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6526 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
6527 rela_dyn->add_symbolless_global_addend(
6528 gsym, dynrel, output_section, object, data_shndx,
6529 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
6530 }
6531 else
6532 {
b3ccdeb5
AM
6533 Reloc_section* rela_dyn
6534 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 6535 check_non_pic(object, r_type);
dd93cd0a
AM
6536 rela_dyn->add_global(gsym, r_type, output_section,
6537 object, data_shndx,
6538 reloc.get_r_offset(),
6539 reloc.get_r_addend());
5edad15d
AM
6540
6541 if (size == 64
6542 && parameters->options().toc_optimize()
6543 && data_shndx == ppc_object->toc_shndx())
6544 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
6545 }
6546 }
42cacb20
DE
6547 }
6548 break;
6549
cf43a2fe 6550 case elfcpp::R_PPC_PLTREL24:
42cacb20 6551 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
6552 if (!is_ifunc)
6553 {
0e123f69 6554 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 6555 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 6556 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
6557 if (gsym->needs_plt_entry()
6558 || (!gsym->final_value_is_known()
6559 && (gsym->is_undefined()
6560 || gsym->is_from_dynobj()
6561 || gsym->is_preemptible())))
6562 target->make_plt_entry(symtab, layout, gsym);
6563 }
d8e90251 6564 // Fall through.
42cacb20 6565
3ea0a085 6566 case elfcpp::R_PPC64_REL64:
dd93cd0a 6567 case elfcpp::R_POWERPC_REL32:
3ea0a085 6568 // Make a dynamic relocation if necessary.
88b8e639 6569 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 6570 {
a82bef93
ST
6571 if (!parameters->options().output_is_position_independent()
6572 && gsym->may_need_copy_reloc())
3ea0a085
AM
6573 {
6574 target->copy_reloc(symtab, layout, object,
6575 data_shndx, output_section, gsym,
6576 reloc);
6577 }
6578 else
6579 {
b3ccdeb5
AM
6580 Reloc_section* rela_dyn
6581 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
6582 check_non_pic(object, r_type);
6583 rela_dyn->add_global(gsym, r_type, output_section, object,
6584 data_shndx, reloc.get_r_offset(),
6585 reloc.get_r_addend());
6586 }
6587 }
6588 break;
6589
ec661b9d
AM
6590 case elfcpp::R_POWERPC_REL14:
6591 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6592 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 6593 if (!is_ifunc)
0e123f69
AM
6594 {
6595 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6596 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6597 r_type, r_sym, reloc.get_r_addend());
6598 }
ec661b9d
AM
6599 break;
6600
6ce78956
AM
6601 case elfcpp::R_POWERPC_REL16:
6602 case elfcpp::R_POWERPC_REL16_LO:
6603 case elfcpp::R_POWERPC_REL16_HI:
6604 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 6605 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 6606 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 6607 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 6608 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 6609 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
6610 case elfcpp::R_PPC64_SECTOFF_DS:
6611 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6612 case elfcpp::R_POWERPC_TPREL16:
6613 case elfcpp::R_POWERPC_TPREL16_LO:
6614 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 6615 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
6616 case elfcpp::R_PPC64_TPREL16_DS:
6617 case elfcpp::R_PPC64_TPREL16_LO_DS:
6618 case elfcpp::R_PPC64_TPREL16_HIGH:
6619 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 6620 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 6621 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 6622 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 6623 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
6624 case elfcpp::R_POWERPC_DTPREL16:
6625 case elfcpp::R_POWERPC_DTPREL16_LO:
6626 case elfcpp::R_POWERPC_DTPREL16_HI:
6627 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
6628 case elfcpp::R_PPC64_DTPREL16_DS:
6629 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
6630 case elfcpp::R_PPC64_DTPREL16_HIGH:
6631 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6632 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6633 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6634 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6635 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
6636 case elfcpp::R_PPC64_TLSGD:
6637 case elfcpp::R_PPC64_TLSLD:
45965137 6638 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
6639 break;
6640
42cacb20
DE
6641 case elfcpp::R_POWERPC_GOT16:
6642 case elfcpp::R_POWERPC_GOT16_LO:
6643 case elfcpp::R_POWERPC_GOT16_HI:
6644 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
6645 case elfcpp::R_PPC64_GOT16_DS:
6646 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 6647 {
c9269dff
AM
6648 // The symbol requires a GOT entry.
6649 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
6650
6651 got = target->got_section(symtab, layout);
2e702c99 6652 if (gsym->final_value_is_known())
2e702c99 6653 {
b01a4b04
AM
6654 if (is_ifunc
6655 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
6656 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6657 else
6658 got->add_global(gsym, GOT_TYPE_STANDARD);
6659 }
6660 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6661 {
6662 // If we are generating a shared object or a pie, this
6663 // symbol's GOT entry will be set by a dynamic relocation.
6664 unsigned int off = got->add_constant(0);
6665 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6666
b3ccdeb5
AM
6667 Reloc_section* rela_dyn
6668 = target->rela_dyn_section(symtab, layout, is_ifunc);
6669
e5d5f5ed 6670 if (gsym->can_use_relative_reloc(false)
9055360d
AM
6671 && !((size == 32
6672 || target->abiversion() >= 2)
e5d5f5ed
AM
6673 && gsym->visibility() == elfcpp::STV_PROTECTED
6674 && parameters->options().shared()))
2e702c99 6675 {
b3ccdeb5
AM
6676 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6677 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
6678 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6679 }
6680 else
6681 {
6682 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6683 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 6684 }
2e702c99 6685 }
42cacb20
DE
6686 }
6687 break;
6688
cf43a2fe
AM
6689 case elfcpp::R_PPC64_TOC16:
6690 case elfcpp::R_PPC64_TOC16_LO:
6691 case elfcpp::R_PPC64_TOC16_HI:
6692 case elfcpp::R_PPC64_TOC16_HA:
6693 case elfcpp::R_PPC64_TOC16_DS:
6694 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
6695 // We need a GOT section.
6696 target->got_section(symtab, layout);
6697 break;
6698
dd93cd0a
AM
6699 case elfcpp::R_POWERPC_GOT_TLSGD16:
6700 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6701 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6702 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6703 {
6704 const bool final = gsym->final_value_is_known();
6705 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6706 if (tls_type == tls::TLSOPT_NONE)
6707 {
6708 Output_data_got_powerpc<size, big_endian>* got
6709 = target->got_section(symtab, layout);
b3ccdeb5
AM
6710 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6711 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
6712 elfcpp::R_POWERPC_DTPMOD,
6713 elfcpp::R_POWERPC_DTPREL);
6714 }
6715 else if (tls_type == tls::TLSOPT_TO_IE)
6716 {
acc276d8
AM
6717 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6718 {
6719 Output_data_got_powerpc<size, big_endian>* got
6720 = target->got_section(symtab, layout);
6721 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6722 if (gsym->is_undefined()
6723 || gsym->is_from_dynobj())
6724 {
6725 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6726 elfcpp::R_POWERPC_TPREL);
6727 }
6728 else
6729 {
6730 unsigned int off = got->add_constant(0);
6731 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6732 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6733 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6734 got, off, 0);
6735 }
6736 }
dd93cd0a
AM
6737 }
6738 else if (tls_type == tls::TLSOPT_TO_LE)
6739 {
6740 // no GOT relocs needed for Local Exec.
6741 }
6742 else
6743 gold_unreachable();
6744 }
42cacb20
DE
6745 break;
6746
dd93cd0a
AM
6747 case elfcpp::R_POWERPC_GOT_TLSLD16:
6748 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6749 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6750 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6751 {
6752 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6753 if (tls_type == tls::TLSOPT_NONE)
6754 target->tlsld_got_offset(symtab, layout, object);
6755 else if (tls_type == tls::TLSOPT_TO_LE)
6756 {
6757 // no GOT relocs needed for Local Exec.
7404fe1b
AM
6758 if (parameters->options().emit_relocs())
6759 {
6760 Output_section* os = layout->tls_segment()->first_section();
6761 gold_assert(os != NULL);
6762 os->set_needs_symtab_index();
6763 }
dd93cd0a
AM
6764 }
6765 else
6766 gold_unreachable();
6767 }
6768 break;
6769
6770 case elfcpp::R_POWERPC_GOT_DTPREL16:
6771 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6772 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6773 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6774 {
6775 Output_data_got_powerpc<size, big_endian>* got
6776 = target->got_section(symtab, layout);
bd73a62d
AM
6777 if (!gsym->final_value_is_known()
6778 && (gsym->is_from_dynobj()
6779 || gsym->is_undefined()
6780 || gsym->is_preemptible()))
6781 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6782 target->rela_dyn_section(layout),
6783 elfcpp::R_POWERPC_DTPREL);
6784 else
6785 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
6786 }
6787 break;
6788
6789 case elfcpp::R_POWERPC_GOT_TPREL16:
6790 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6791 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6792 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6793 {
6794 const bool final = gsym->final_value_is_known();
6795 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6796 if (tls_type == tls::TLSOPT_NONE)
6797 {
acc276d8
AM
6798 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6799 {
6800 Output_data_got_powerpc<size, big_endian>* got
6801 = target->got_section(symtab, layout);
6802 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6803 if (gsym->is_undefined()
6804 || gsym->is_from_dynobj())
6805 {
6806 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6807 elfcpp::R_POWERPC_TPREL);
6808 }
6809 else
6810 {
6811 unsigned int off = got->add_constant(0);
6812 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6813 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6814 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6815 got, off, 0);
6816 }
6817 }
dd93cd0a
AM
6818 }
6819 else if (tls_type == tls::TLSOPT_TO_LE)
6820 {
6821 // no GOT relocs needed for Local Exec.
6822 }
6823 else
6824 gold_unreachable();
6825 }
42cacb20
DE
6826 break;
6827
6828 default:
6829 unsupported_reloc_global(object, r_type, gsym);
6830 break;
6831 }
d8f5a274 6832
5edad15d
AM
6833 if (size == 64
6834 && parameters->options().toc_optimize())
6835 {
6836 if (data_shndx == ppc_object->toc_shndx())
6837 {
6838 bool ok = true;
6839 if (r_type != elfcpp::R_PPC64_ADDR64
6840 || (is_ifunc && target->abiversion() < 2))
6841 ok = false;
6842 else if (parameters->options().output_is_position_independent()
6843 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
6844 ok = false;
6845 if (!ok)
6846 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6847 }
6848
6849 enum {no_check, check_lo, check_ha} insn_check;
6850 switch (r_type)
6851 {
6852 default:
6853 insn_check = no_check;
6854 break;
6855
6856 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6857 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6858 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6859 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6860 case elfcpp::R_POWERPC_GOT16_HA:
6861 case elfcpp::R_PPC64_TOC16_HA:
6862 insn_check = check_ha;
6863 break;
6864
6865 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6866 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6867 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6868 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6869 case elfcpp::R_POWERPC_GOT16_LO:
6870 case elfcpp::R_PPC64_GOT16_LO_DS:
6871 case elfcpp::R_PPC64_TOC16_LO:
6872 case elfcpp::R_PPC64_TOC16_LO_DS:
6873 insn_check = check_lo;
6874 break;
6875 }
6876
6877 section_size_type slen;
6878 const unsigned char* view = NULL;
6879 if (insn_check != no_check)
6880 {
6881 view = ppc_object->section_contents(data_shndx, &slen, false);
6882 section_size_type off =
6883 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6884 if (off < slen)
6885 {
6886 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6887 if (insn_check == check_lo
6888 ? !ok_lo_toc_insn(insn, r_type)
6889 : ((insn & ((0x3f << 26) | 0x1f << 16))
6890 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6891 {
6892 ppc_object->set_no_toc_opt();
6893 gold_warning(_("%s: toc optimization is not supported "
6894 "for %#08x instruction"),
6895 ppc_object->name().c_str(), insn);
6896 }
6897 }
6898 }
6899
6900 switch (r_type)
6901 {
6902 default:
6903 break;
6904 case elfcpp::R_PPC64_TOC16:
6905 case elfcpp::R_PPC64_TOC16_LO:
6906 case elfcpp::R_PPC64_TOC16_HI:
6907 case elfcpp::R_PPC64_TOC16_HA:
6908 case elfcpp::R_PPC64_TOC16_DS:
6909 case elfcpp::R_PPC64_TOC16_LO_DS:
6910 if (gsym->source() == Symbol::FROM_OBJECT
6911 && !gsym->object()->is_dynamic())
6912 {
6913 Powerpc_relobj<size, big_endian>* sym_object
6914 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6915 bool is_ordinary;
6916 unsigned int shndx = gsym->shndx(&is_ordinary);
6917 if (shndx == sym_object->toc_shndx())
6918 {
6919 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
6920 Address dst_off = sym->value() + reloc.get_r_offset();
6921 if (dst_off < sym_object->section_size(shndx))
6922 {
6923 bool ok = false;
6924 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6925 ok = true;
6926 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6927 {
6928 // Need to check that the insn is a ld
6929 if (!view)
6930 view = ppc_object->section_contents(data_shndx,
6931 &slen,
6932 false);
6933 section_size_type off =
6934 (convert_to_section_size_type(reloc.get_r_offset())
6935 + (big_endian ? -2 : 3));
6936 if (off < slen
6937 && (view[off] & (0x3f << 2)) == (58u << 2))
6938 ok = true;
6939 }
6940 if (!ok)
6941 sym_object->set_no_toc_opt(dst_off);
6942 }
6943 }
6944 }
6945 break;
6946 }
6947 }
6948
f159cdb6
AM
6949 if (size == 32)
6950 {
6951 switch (r_type)
6952 {
6953 case elfcpp::R_PPC_LOCAL24PC:
6954 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
6955 gold_error(_("%s: unsupported -mbss-plt code"),
6956 ppc_object->name().c_str());
6957 break;
6958 default:
6959 break;
6960 }
6961 }
6962
d8f5a274
AM
6963 switch (r_type)
6964 {
6965 case elfcpp::R_POWERPC_GOT_TLSLD16:
6966 case elfcpp::R_POWERPC_GOT_TLSGD16:
6967 case elfcpp::R_POWERPC_GOT_TPREL16:
6968 case elfcpp::R_POWERPC_GOT_DTPREL16:
6969 case elfcpp::R_POWERPC_GOT16:
6970 case elfcpp::R_PPC64_GOT16_DS:
6971 case elfcpp::R_PPC64_TOC16:
6972 case elfcpp::R_PPC64_TOC16_DS:
6973 ppc_object->set_has_small_toc_reloc();
6974 default:
6975 break;
6976 }
42cacb20
DE
6977}
6978
6d03d481
ST
6979// Process relocations for gc.
6980
6981template<int size, bool big_endian>
6982void
6983Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
6984 Symbol_table* symtab,
6985 Layout* layout,
6986 Sized_relobj_file<size, big_endian>* object,
6987 unsigned int data_shndx,
6988 unsigned int,
6989 const unsigned char* prelocs,
6990 size_t reloc_count,
6991 Output_section* output_section,
6992 bool needs_special_offset_handling,
6993 size_t local_symbol_count,
6994 const unsigned char* plocal_symbols)
6d03d481
ST
6995{
6996 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
6997 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6998 Classify_reloc;
6999
e81fea4d
AM
7000 Powerpc_relobj<size, big_endian>* ppc_object
7001 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7002 if (size == 64)
7003 ppc_object->set_opd_valid();
7004 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7005 {
7006 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7007 for (p = ppc_object->access_from_map()->begin();
7008 p != ppc_object->access_from_map()->end();
7009 ++p)
7010 {
7011 Address dst_off = p->first;
7012 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7013 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7014 for (s = p->second.begin(); s != p->second.end(); ++s)
7015 {
efc6fa12 7016 Relobj* src_obj = s->first;
e81fea4d
AM
7017 unsigned int src_indx = s->second;
7018 symtab->gc()->add_reference(src_obj, src_indx,
7019 ppc_object, dst_indx);
7020 }
7021 p->second.clear();
7022 }
7023 ppc_object->access_from_map()->clear();
c6de8ed4 7024 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
7025 // Don't look at .opd relocs as .opd will reference everything.
7026 return;
7027 }
6d03d481 7028
4d625b70 7029 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
7030 symtab,
7031 layout,
7032 this,
7033 object,
7034 data_shndx,
7035 prelocs,
7036 reloc_count,
7037 output_section,
7038 needs_special_offset_handling,
7039 local_symbol_count,
7040 plocal_symbols);
7041}
7042
e81fea4d
AM
7043// Handle target specific gc actions when adding a gc reference from
7044// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7045// and DST_OFF. For powerpc64, this adds a referenc to the code
7046// section of a function descriptor.
7047
7048template<int size, bool big_endian>
7049void
7050Target_powerpc<size, big_endian>::do_gc_add_reference(
7051 Symbol_table* symtab,
efc6fa12 7052 Relobj* src_obj,
e81fea4d 7053 unsigned int src_shndx,
efc6fa12 7054 Relobj* dst_obj,
e81fea4d
AM
7055 unsigned int dst_shndx,
7056 Address dst_off) const
7057{
6c77229c
AM
7058 if (size != 64 || dst_obj->is_dynamic())
7059 return;
7060
e81fea4d
AM
7061 Powerpc_relobj<size, big_endian>* ppc_object
7062 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 7063 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
7064 {
7065 if (ppc_object->opd_valid())
7066 {
7067 dst_shndx = ppc_object->get_opd_ent(dst_off);
7068 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7069 }
7070 else
7071 {
7072 // If we haven't run scan_opd_relocs, we must delay
7073 // processing this function descriptor reference.
7074 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7075 }
7076 }
7077}
7078
7079// Add any special sections for this symbol to the gc work list.
7080// For powerpc64, this adds the code section of a function
7081// descriptor.
7082
7083template<int size, bool big_endian>
7084void
7085Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7086 Symbol_table* symtab,
7087 Symbol* sym) const
7088{
7089 if (size == 64)
7090 {
7091 Powerpc_relobj<size, big_endian>* ppc_object
7092 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7093 bool is_ordinary;
7094 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 7095 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
7096 {
7097 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7098 Address dst_off = gsym->value();
c6de8ed4
AM
7099 if (ppc_object->opd_valid())
7100 {
7101 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
7102 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7103 dst_indx));
c6de8ed4
AM
7104 }
7105 else
7106 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
7107 }
7108 }
7109}
7110
dc3714f3
AM
7111// For a symbol location in .opd, set LOC to the location of the
7112// function entry.
7113
7114template<int size, bool big_endian>
7115void
7116Target_powerpc<size, big_endian>::do_function_location(
7117 Symbol_location* loc) const
7118{
a2d7bf59 7119 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
7120 {
7121 if (loc->object->is_dynamic())
7122 {
7123 Powerpc_dynobj<size, big_endian>* ppc_object
7124 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7125 if (loc->shndx == ppc_object->opd_shndx())
7126 {
7127 Address dest_off;
7128 Address off = loc->offset - ppc_object->opd_address();
7129 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7130 loc->offset = dest_off;
7131 }
7132 }
7133 else
7134 {
7135 const Powerpc_relobj<size, big_endian>* ppc_object
7136 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7137 if (loc->shndx == ppc_object->opd_shndx())
7138 {
7139 Address dest_off;
7140 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7141 loc->offset = dest_off;
7142 }
7143 }
7144 }
7145}
7146
bbec1a5d
AM
7147// FNOFFSET in section SHNDX in OBJECT is the start of a function
7148// compiled with -fsplit-stack. The function calls non-split-stack
7149// code. Change the function to ensure it has enough stack space to
7150// call some random function.
7151
7152template<int size, bool big_endian>
7153void
7154Target_powerpc<size, big_endian>::do_calls_non_split(
7155 Relobj* object,
7156 unsigned int shndx,
7157 section_offset_type fnoffset,
7158 section_size_type fnsize,
6e0813d3
CC
7159 const unsigned char* prelocs,
7160 size_t reloc_count,
bbec1a5d
AM
7161 unsigned char* view,
7162 section_size_type view_size,
7163 std::string* from,
7164 std::string* to) const
7165{
7166 // 32-bit not supported.
7167 if (size == 32)
7168 {
7169 // warn
7170 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
7171 prelocs, reloc_count, view, view_size,
7172 from, to);
bbec1a5d
AM
7173 return;
7174 }
7175
7176 // The function always starts with
7177 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7178 // addis %r12,%r1,-allocate@ha
7179 // addi %r12,%r12,-allocate@l
7180 // cmpld %r12,%r0
7181 // but note that the addis or addi may be replaced with a nop
7182
7183 unsigned char *entry = view + fnoffset;
7184 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7185
7186 if ((insn & 0xffff0000) == addis_2_12)
7187 {
7188 /* Skip ELFv2 global entry code. */
7189 entry += 8;
7190 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7191 }
7192
7193 unsigned char *pinsn = entry;
7194 bool ok = false;
7195 const uint32_t ld_private_ss = 0xe80d8fc0;
7196 if (insn == ld_private_ss)
7197 {
7198 int32_t allocate = 0;
7199 while (1)
7200 {
7201 pinsn += 4;
7202 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7203 if ((insn & 0xffff0000) == addis_12_1)
7204 allocate += (insn & 0xffff) << 16;
7205 else if ((insn & 0xffff0000) == addi_12_1
7206 || (insn & 0xffff0000) == addi_12_12)
7207 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7208 else if (insn != nop)
7209 break;
7210 }
7211 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7212 {
7213 int extra = parameters->options().split_stack_adjust_size();
7214 allocate -= extra;
7215 if (allocate >= 0 || extra < 0)
7216 {
7217 object->error(_("split-stack stack size overflow at "
7218 "section %u offset %0zx"),
7219 shndx, static_cast<size_t>(fnoffset));
7220 return;
7221 }
7222 pinsn = entry + 4;
7223 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7224 if (insn != addis_12_1)
7225 {
7226 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7227 pinsn += 4;
7228 insn = addi_12_12 | (allocate & 0xffff);
7229 if (insn != addi_12_12)
7230 {
7231 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7232 pinsn += 4;
7233 }
7234 }
7235 else
7236 {
7237 insn = addi_12_1 | (allocate & 0xffff);
7238 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7239 pinsn += 4;
7240 }
7241 if (pinsn != entry + 12)
7242 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7243
7244 ok = true;
7245 }
7246 }
7247
7248 if (!ok)
7249 {
7250 if (!object->has_no_split_stack())
7251 object->error(_("failed to match split-stack sequence at "
7252 "section %u offset %0zx"),
7253 shndx, static_cast<size_t>(fnoffset));
7254 }
7255}
7256
42cacb20
DE
7257// Scan relocations for a section.
7258
7259template<int size, bool big_endian>
7260void
7261Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
7262 Symbol_table* symtab,
7263 Layout* layout,
7264 Sized_relobj_file<size, big_endian>* object,
7265 unsigned int data_shndx,
7266 unsigned int sh_type,
7267 const unsigned char* prelocs,
7268 size_t reloc_count,
7269 Output_section* output_section,
7270 bool needs_special_offset_handling,
7271 size_t local_symbol_count,
7272 const unsigned char* plocal_symbols)
42cacb20
DE
7273{
7274 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
7275 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7276 Classify_reloc;
42cacb20
DE
7277
7278 if (sh_type == elfcpp::SHT_REL)
7279 {
7280 gold_error(_("%s: unsupported REL reloc section"),
7281 object->name().c_str());
7282 return;
7283 }
7284
4d625b70 7285 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
7286 symtab,
7287 layout,
7288 this,
7289 object,
7290 data_shndx,
7291 prelocs,
7292 reloc_count,
7293 output_section,
7294 needs_special_offset_handling,
7295 local_symbol_count,
7296 plocal_symbols);
7297}
7298
ec4dbad3
AM
7299// Functor class for processing the global symbol table.
7300// Removes symbols defined on discarded opd entries.
7301
7302template<bool big_endian>
7303class Global_symbol_visitor_opd
7304{
7305 public:
7306 Global_symbol_visitor_opd()
7307 { }
7308
7309 void
7310 operator()(Sized_symbol<64>* sym)
7311 {
7312 if (sym->has_symtab_index()
7313 || sym->source() != Symbol::FROM_OBJECT
7314 || !sym->in_real_elf())
7315 return;
7316
6c77229c
AM
7317 if (sym->object()->is_dynamic())
7318 return;
7319
ec4dbad3
AM
7320 Powerpc_relobj<64, big_endian>* symobj
7321 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 7322 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
7323 return;
7324
7325 bool is_ordinary;
7326 unsigned int shndx = sym->shndx(&is_ordinary);
7327 if (shndx == symobj->opd_shndx()
7328 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
7329 {
7330 sym->set_undefined();
e3ee8ed4 7331 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
7332 sym->set_is_defined_in_discarded_section();
7333 sym->set_symtab_index(-1U);
7334 }
ec4dbad3
AM
7335 }
7336};
7337
f3a0ed29
AM
7338template<int size, bool big_endian>
7339void
7340Target_powerpc<size, big_endian>::define_save_restore_funcs(
7341 Layout* layout,
7342 Symbol_table* symtab)
7343{
7344 if (size == 64)
7345 {
d49044c7
AM
7346 Output_data_save_res<size, big_endian>* savres
7347 = new Output_data_save_res<size, big_endian>(symtab);
7348 this->savres_section_ = savres;
f3a0ed29
AM
7349 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7350 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7351 savres, ORDER_TEXT, false);
7352 }
7353}
7354
d8f5a274
AM
7355// Sort linker created .got section first (for the header), then input
7356// sections belonging to files using small model code.
7357
7358template<bool big_endian>
7359class Sort_toc_sections
7360{
7361 public:
7362 bool
7363 operator()(const Output_section::Input_section& is1,
7364 const Output_section::Input_section& is2) const
7365 {
7366 if (!is1.is_input_section() && is2.is_input_section())
7367 return true;
7368 bool small1
7369 = (is1.is_input_section()
7370 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
7371 ->has_small_toc_reloc()));
7372 bool small2
7373 = (is2.is_input_section()
7374 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
7375 ->has_small_toc_reloc()));
7376 return small1 && !small2;
7377 }
7378};
7379
42cacb20
DE
7380// Finalize the sections.
7381
7382template<int size, bool big_endian>
7383void
d5b40221
DK
7384Target_powerpc<size, big_endian>::do_finalize_sections(
7385 Layout* layout,
f59f41f3 7386 const Input_objects*,
ec4dbad3 7387 Symbol_table* symtab)
42cacb20 7388{
c9824451
AM
7389 if (parameters->doing_static_link())
7390 {
7391 // At least some versions of glibc elf-init.o have a strong
7392 // reference to __rela_iplt marker syms. A weak ref would be
7393 // better..
7394 if (this->iplt_ != NULL)
7395 {
7396 Reloc_section* rel = this->iplt_->rel_plt();
7397 symtab->define_in_output_data("__rela_iplt_start", NULL,
7398 Symbol_table::PREDEFINED, rel, 0, 0,
7399 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7400 elfcpp::STV_HIDDEN, 0, false, true);
7401 symtab->define_in_output_data("__rela_iplt_end", NULL,
7402 Symbol_table::PREDEFINED, rel, 0, 0,
7403 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7404 elfcpp::STV_HIDDEN, 0, true, true);
7405 }
7406 else
7407 {
7408 symtab->define_as_constant("__rela_iplt_start", NULL,
7409 Symbol_table::PREDEFINED, 0, 0,
7410 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7411 elfcpp::STV_HIDDEN, 0, true, false);
7412 symtab->define_as_constant("__rela_iplt_end", NULL,
7413 Symbol_table::PREDEFINED, 0, 0,
7414 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7415 elfcpp::STV_HIDDEN, 0, true, false);
7416 }
7417 }
7418
ec4dbad3
AM
7419 if (size == 64)
7420 {
7421 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
7422 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
7423
7424 if (!parameters->options().relocatable())
7425 {
7426 this->define_save_restore_funcs(layout, symtab);
7427
7428 // Annoyingly, we need to make these sections now whether or
7429 // not we need them. If we delay until do_relax then we
7430 // need to mess with the relaxation machinery checkpointing.
7431 this->got_section(symtab, layout);
7432 this->make_brlt_section(layout);
d8f5a274
AM
7433
7434 if (parameters->options().toc_sort())
7435 {
7436 Output_section* os = this->got_->output_section();
7437 if (os != NULL && os->input_sections().size() > 1)
7438 std::stable_sort(os->input_sections().begin(),
7439 os->input_sections().end(),
7440 Sort_toc_sections<big_endian>());
7441 }
ec661b9d 7442 }
ec4dbad3
AM
7443 }
7444
42cacb20 7445 // Fill in some more dynamic tags.
c9269dff 7446 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 7447 if (odyn != NULL)
cf43a2fe 7448 {
c9824451
AM
7449 const Reloc_section* rel_plt = (this->plt_ == NULL
7450 ? NULL
7451 : this->plt_->rel_plt());
7452 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
7453 this->rela_dyn_, true, size == 32);
7454
7455 if (size == 32)
dd93cd0a 7456 {
c9824451
AM
7457 if (this->got_ != NULL)
7458 {
7459 this->got_->finalize_data_size();
7460 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
7461 this->got_, this->got_->g_o_t());
7462 }
dd93cd0a 7463 }
c9824451 7464 else
dd93cd0a 7465 {
c9824451
AM
7466 if (this->glink_ != NULL)
7467 {
7468 this->glink_->finalize_data_size();
7469 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
7470 this->glink_,
ec661b9d 7471 (this->glink_->pltresolve_size
c9824451
AM
7472 - 32));
7473 }
dd93cd0a 7474 }
c9269dff 7475 }
cf43a2fe 7476
42cacb20
DE
7477 // Emit any relocs we saved in an attempt to avoid generating COPY
7478 // relocs.
7479 if (this->copy_relocs_.any_saved_relocs())
7480 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7481}
7482
5edad15d
AM
7483// Emit any saved relocs, and mark toc entries using any of these
7484// relocs as not optimizable.
aba6bc71 7485
5edad15d
AM
7486template<int sh_type, int size, bool big_endian>
7487void
7488Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
7489 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 7490{
5edad15d
AM
7491 if (size == 64
7492 && parameters->options().toc_optimize())
7493 {
7494 for (typename Copy_relocs<sh_type, size, big_endian>::
7495 Copy_reloc_entries::iterator p = this->entries_.begin();
7496 p != this->entries_.end();
7497 ++p)
7498 {
7499 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
7500 entry = *p;
7501
7502 // If the symbol is no longer defined in a dynamic object,
7503 // then we emitted a COPY relocation. If it is still
7504 // dynamic then we'll need dynamic relocations and thus
7505 // can't optimize toc entries.
7506 if (entry.sym_->is_from_dynobj())
7507 {
7508 Powerpc_relobj<size, big_endian>* ppc_object
7509 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
7510 if (entry.shndx_ == ppc_object->toc_shndx())
7511 ppc_object->set_no_toc_opt(entry.address_);
7512 }
7513 }
7514 }
7515
7516 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
7517}
7518
3ea0a085
AM
7519// Return the value to use for a branch relocation.
7520
7521template<int size, bool big_endian>
1611bc4a 7522bool
3ea0a085 7523Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 7524 const Symbol_table* symtab,
3ea0a085
AM
7525 const Sized_symbol<size>* gsym,
7526 Powerpc_relobj<size, big_endian>* object,
1611bc4a 7527 Address *value,
3ea0a085
AM
7528 unsigned int *dest_shndx)
7529{
9055360d
AM
7530 if (size == 32 || this->abiversion() >= 2)
7531 gold_unreachable();
3ea0a085 7532 *dest_shndx = 0;
3ea0a085
AM
7533
7534 // If the symbol is defined in an opd section, ie. is a function
7535 // descriptor, use the function descriptor code entry address
7536 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 7537 if (gsym != NULL
0e123f69
AM
7538 && (gsym->source() != Symbol::FROM_OBJECT
7539 || gsym->object()->is_dynamic()))
1611bc4a 7540 return true;
3ea0a085
AM
7541 if (gsym != NULL)
7542 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7543 unsigned int shndx = symobj->opd_shndx();
7544 if (shndx == 0)
1611bc4a 7545 return true;
3ea0a085 7546 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 7547 if (opd_addr == invalid_address)
1611bc4a 7548 return true;
c6905c28 7549 opd_addr += symobj->output_section_address(shndx);
1611bc4a 7550 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
7551 {
7552 Address sec_off;
1611bc4a 7553 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
7554 if (symtab->is_section_folded(symobj, *dest_shndx))
7555 {
7556 Section_id folded
7557 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7558 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7559 *dest_shndx = folded.second;
7560 }
3ea0a085 7561 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
7562 if (sec_addr == invalid_address)
7563 return false;
7564
3ea0a085 7565 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 7566 *value = sec_addr + sec_off;
3ea0a085 7567 }
1611bc4a 7568 return true;
3ea0a085
AM
7569}
7570
42cacb20
DE
7571// Perform a relocation.
7572
7573template<int size, bool big_endian>
7574inline bool
7575Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 7576 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 7577 unsigned int,
d83ce4e3
AM
7578 Target_powerpc* target,
7579 Output_section* os,
7580 size_t relnum,
91a65d2f 7581 const unsigned char* preloc,
d83ce4e3
AM
7582 const Sized_symbol<size>* gsym,
7583 const Symbol_value<size>* psymval,
7584 unsigned char* view,
c9269dff
AM
7585 Address address,
7586 section_size_type view_size)
42cacb20 7587{
0e804863
ILT
7588 if (view == NULL)
7589 return true;
7590
91a65d2f
AM
7591 const elfcpp::Rela<size, big_endian> rela(preloc);
7592 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
e3deeb9c 7593 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
dd93cd0a 7594 {
e3deeb9c
AM
7595 case Track_tls::NOT_EXPECTED:
7596 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7597 _("__tls_get_addr call lacks marker reloc"));
7598 break;
7599 case Track_tls::EXPECTED:
7600 // We have already complained.
7601 break;
7602 case Track_tls::SKIP:
7603 return true;
7604 case Track_tls::NORMAL:
7605 break;
dd93cd0a 7606 }
dd93cd0a 7607
42cacb20 7608 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
dd93cd0a 7609 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
0e123f69 7610 typedef typename elfcpp::Rela<size, big_endian> Reltype;
dcfc7dd4
AM
7611 // Offset from start of insn to d-field reloc.
7612 const int d_offset = big_endian ? 2 : 0;
7613
3ea0a085
AM
7614 Powerpc_relobj<size, big_endian>* const object
7615 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 7616 Address value = 0;
0cfb0717 7617 bool has_stub_value = false;
e5d5f5ed 7618 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
b3ccdeb5 7619 if ((gsym != NULL
88b8e639 7620 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
b3ccdeb5
AM
7621 : object->local_has_plt_offset(r_sym))
7622 && (!psymval->is_ifunc_symbol()
9055360d 7623 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 7624 {
9055360d
AM
7625 if (size == 64
7626 && gsym != NULL
7627 && target->abiversion() >= 2
7628 && !parameters->options().output_is_position_independent()
7629 && !is_branch_reloc(r_type))
ec661b9d 7630 {
faa2211d
AM
7631 Address off = target->glink_section()->find_global_entry(gsym);
7632 if (off != invalid_address)
6ec65f28
AM
7633 {
7634 value = target->glink_section()->global_entry_address() + off;
7635 has_stub_value = true;
7636 }
ec661b9d 7637 }
c9824451 7638 else
9055360d
AM
7639 {
7640 Stub_table<size, big_endian>* stub_table
7641 = object->stub_table(relinfo->data_shndx);
7642 if (stub_table == NULL)
7643 {
7644 // This is a ref from a data section to an ifunc symbol.
7645 if (target->stub_tables().size() != 0)
7646 stub_table = target->stub_tables()[0];
7647 }
faa2211d
AM
7648 if (stub_table != NULL)
7649 {
7650 Address off;
7651 if (gsym != NULL)
7652 off = stub_table->find_plt_call_entry(object, gsym, r_type,
7653 rela.get_r_addend());
7654 else
7655 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
7656 rela.get_r_addend());
7657 if (off != invalid_address)
7658 {
7659 value = stub_table->stub_address() + off;
7660 has_stub_value = true;
7661 }
7662 }
9055360d 7663 }
faa2211d
AM
7664 // We don't care too much about bogus debug references to
7665 // non-local functions, but otherwise there had better be a plt
7666 // call stub or global entry stub as appropriate.
7667 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 7668 }
cf43a2fe
AM
7669
7670 if (r_type == elfcpp::R_POWERPC_GOT16
7671 || r_type == elfcpp::R_POWERPC_GOT16_LO
7672 || r_type == elfcpp::R_POWERPC_GOT16_HI
7673 || r_type == elfcpp::R_POWERPC_GOT16_HA
7674 || r_type == elfcpp::R_PPC64_GOT16_DS
7675 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 7676 {
cf43a2fe
AM
7677 if (gsym != NULL)
7678 {
7679 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7680 value = gsym->got_offset(GOT_TYPE_STANDARD);
7681 }
7682 else
7683 {
cf43a2fe
AM
7684 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7685 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
7686 }
dd93cd0a 7687 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
7688 }
7689 else if (r_type == elfcpp::R_PPC64_TOC)
7690 {
c9269dff 7691 value = (target->got_section()->output_section()->address()
dd93cd0a 7692 + object->toc_base_offset());
cf43a2fe
AM
7693 }
7694 else if (gsym != NULL
7695 && (r_type == elfcpp::R_POWERPC_REL24
7696 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 7697 && has_stub_value)
cf43a2fe 7698 {
c9269dff
AM
7699 if (size == 64)
7700 {
7701 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7702 Valtype* wv = reinterpret_cast<Valtype*>(view);
7703 bool can_plt_call = false;
7704 if (rela.get_r_offset() + 8 <= view_size)
7705 {
3ea0a085 7706 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 7707 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
7708 if ((insn & 1) != 0
7709 && (insn2 == nop
7710 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 7711 {
b4f7960d
AM
7712 elfcpp::Swap<32, big_endian>::
7713 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
7714 can_plt_call = true;
7715 }
7716 }
7717 if (!can_plt_call)
3ea0a085
AM
7718 {
7719 // If we don't have a branch and link followed by a nop,
7720 // we can't go via the plt because there is no place to
7721 // put a toc restoring instruction.
7722 // Unless we know we won't be returning.
7723 if (strcmp(gsym->name(), "__libc_start_main") == 0)
7724 can_plt_call = true;
7725 }
7726 if (!can_plt_call)
7727 {
ba8ca3e7
AM
7728 // g++ as of 20130507 emits self-calls without a
7729 // following nop. This is arguably wrong since we have
7730 // conflicting information. On the one hand a global
7731 // symbol and on the other a local call sequence, but
7732 // don't error for this special case.
7733 // It isn't possible to cheaply verify we have exactly
7734 // such a call. Allow all calls to the same section.
3ea0a085 7735 bool ok = false;
c9824451 7736 Address code = value;
3ea0a085
AM
7737 if (gsym->source() == Symbol::FROM_OBJECT
7738 && gsym->object() == object)
7739 {
9055360d
AM
7740 unsigned int dest_shndx = 0;
7741 if (target->abiversion() < 2)
7742 {
7743 Address addend = rela.get_r_addend();
1611bc4a
AM
7744 code = psymval->value(object, addend);
7745 target->symval_for_branch(relinfo->symtab, gsym, object,
7746 &code, &dest_shndx);
9055360d 7747 }
3ea0a085
AM
7748 bool is_ordinary;
7749 if (dest_shndx == 0)
7750 dest_shndx = gsym->shndx(&is_ordinary);
7751 ok = dest_shndx == relinfo->data_shndx;
7752 }
7753 if (!ok)
c9824451
AM
7754 {
7755 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7756 _("call lacks nop, can't restore toc; "
7757 "recompile with -fPIC"));
7758 value = code;
7759 }
3ea0a085 7760 }
c9269dff 7761 }
cf43a2fe 7762 }
dd93cd0a
AM
7763 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7764 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7765 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7766 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7767 {
7768 // First instruction of a global dynamic sequence, arg setup insn.
7769 const bool final = gsym == NULL || gsym->final_value_is_known();
7770 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7771 enum Got_type got_type = GOT_TYPE_STANDARD;
7772 if (tls_type == tls::TLSOPT_NONE)
7773 got_type = GOT_TYPE_TLSGD;
7774 else if (tls_type == tls::TLSOPT_TO_IE)
7775 got_type = GOT_TYPE_TPREL;
7776 if (got_type != GOT_TYPE_STANDARD)
7777 {
7778 if (gsym != NULL)
7779 {
7780 gold_assert(gsym->has_got_offset(got_type));
7781 value = gsym->got_offset(got_type);
7782 }
7783 else
7784 {
dd93cd0a
AM
7785 gold_assert(object->local_has_got_offset(r_sym, got_type));
7786 value = object->local_got_offset(r_sym, got_type);
7787 }
7788 value -= target->got_section()->got_base_offset(object);
7789 }
7790 if (tls_type == tls::TLSOPT_TO_IE)
7791 {
7792 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7793 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7794 {
dcfc7dd4 7795 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
7796 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7797 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
7798 if (size == 32)
7799 insn |= 32 << 26; // lwz
7800 else
7801 insn |= 58 << 26; // ld
7802 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7803 }
7804 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7805 - elfcpp::R_POWERPC_GOT_TLSGD16);
7806 }
7807 else if (tls_type == tls::TLSOPT_TO_LE)
7808 {
7809 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7810 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7811 {
dcfc7dd4 7812 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
7813 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7814 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 7815 if (size == 32)
0f81d3f0
AM
7816 insn |= addis_0_2;
7817 else
7818 insn |= addis_0_13;
dd93cd0a
AM
7819 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7820 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7821 value = psymval->value(object, rela.get_r_addend());
7822 }
7823 else
7824 {
dcfc7dd4 7825 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
7826 Insn insn = nop;
7827 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7828 r_type = elfcpp::R_POWERPC_NONE;
7829 }
7830 }
7831 }
7832 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7833 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7834 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7835 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7836 {
7837 // First instruction of a local dynamic sequence, arg setup insn.
7838 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7839 if (tls_type == tls::TLSOPT_NONE)
7840 {
7841 value = target->tlsld_got_offset();
7842 value -= target->got_section()->got_base_offset(object);
7843 }
7844 else
7845 {
7846 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7847 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7848 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7849 {
dcfc7dd4 7850 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
7851 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7852 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 7853 if (size == 32)
0f81d3f0
AM
7854 insn |= addis_0_2;
7855 else
7856 insn |= addis_0_13;
dd93cd0a
AM
7857 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7858 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 7859 value = dtp_offset;
dd93cd0a
AM
7860 }
7861 else
7862 {
dcfc7dd4 7863 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
7864 Insn insn = nop;
7865 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7866 r_type = elfcpp::R_POWERPC_NONE;
7867 }
7868 }
7869 }
7870 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
7871 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
7872 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
7873 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
7874 {
7875 // Accesses relative to a local dynamic sequence address,
7876 // no optimisation here.
7877 if (gsym != NULL)
7878 {
7879 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
7880 value = gsym->got_offset(GOT_TYPE_DTPREL);
7881 }
7882 else
7883 {
dd93cd0a
AM
7884 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
7885 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
7886 }
7887 value -= target->got_section()->got_base_offset(object);
7888 }
7889 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7890 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7891 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7892 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7893 {
7894 // First instruction of initial exec sequence.
7895 const bool final = gsym == NULL || gsym->final_value_is_known();
7896 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7897 if (tls_type == tls::TLSOPT_NONE)
7898 {
7899 if (gsym != NULL)
7900 {
7901 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
7902 value = gsym->got_offset(GOT_TYPE_TPREL);
7903 }
7904 else
7905 {
dd93cd0a
AM
7906 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
7907 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
7908 }
7909 value -= target->got_section()->got_base_offset(object);
7910 }
7911 else
7912 {
7913 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7914 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7915 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7916 {
dcfc7dd4 7917 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
7918 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7919 insn &= (1 << 26) - (1 << 21); // extract rt from ld
7920 if (size == 32)
7921 insn |= addis_0_2;
7922 else
7923 insn |= addis_0_13;
7924 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7925 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7926 value = psymval->value(object, rela.get_r_addend());
7927 }
7928 else
7929 {
dcfc7dd4 7930 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
7931 Insn insn = nop;
7932 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7933 r_type = elfcpp::R_POWERPC_NONE;
7934 }
7935 }
7936 }
7937 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7938 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7939 {
7940 // Second instruction of a global dynamic sequence,
7941 // the __tls_get_addr call
e3deeb9c 7942 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
7943 const bool final = gsym == NULL || gsym->final_value_is_known();
7944 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7945 if (tls_type != tls::TLSOPT_NONE)
7946 {
7947 if (tls_type == tls::TLSOPT_TO_IE)
7948 {
7949 Insn* iview = reinterpret_cast<Insn*>(view);
7950 Insn insn = add_3_3_13;
7951 if (size == 32)
7952 insn = add_3_3_2;
7953 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7954 r_type = elfcpp::R_POWERPC_NONE;
7955 }
7956 else
7957 {
7958 Insn* iview = reinterpret_cast<Insn*>(view);
7959 Insn insn = addi_3_3;
7960 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7961 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 7962 view += d_offset;
dd93cd0a
AM
7963 value = psymval->value(object, rela.get_r_addend());
7964 }
e3deeb9c 7965 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
7966 }
7967 }
7968 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7969 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7970 {
7971 // Second instruction of a local dynamic sequence,
7972 // the __tls_get_addr call
e3deeb9c 7973 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
7974 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7975 if (tls_type == tls::TLSOPT_TO_LE)
7976 {
7977 Insn* iview = reinterpret_cast<Insn*>(view);
7978 Insn insn = addi_3_3;
7979 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
e3deeb9c 7980 this->skip_next_tls_get_addr_call();
dd93cd0a 7981 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 7982 view += d_offset;
7404fe1b 7983 value = dtp_offset;
dd93cd0a
AM
7984 }
7985 }
7986 else if (r_type == elfcpp::R_POWERPC_TLS)
7987 {
7988 // Second instruction of an initial exec sequence
7989 const bool final = gsym == NULL || gsym->final_value_is_known();
7990 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7991 if (tls_type == tls::TLSOPT_TO_LE)
7992 {
7993 Insn* iview = reinterpret_cast<Insn*>(view);
7994 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7995 unsigned int reg = size == 32 ? 2 : 13;
7996 insn = at_tls_transform(insn, reg);
7997 gold_assert(insn != 0);
7998 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7999 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8000 view += d_offset;
dd93cd0a
AM
8001 value = psymval->value(object, rela.get_r_addend());
8002 }
8003 }
0cfb0717 8004 else if (!has_stub_value)
cf43a2fe 8005 {
dd93cd0a 8006 Address addend = 0;
cbcb23fa 8007 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
cf43a2fe 8008 addend = rela.get_r_addend();
c9824451 8009 value = psymval->value(object, addend);
dd93cd0a 8010 if (size == 64 && is_branch_reloc(r_type))
9055360d
AM
8011 {
8012 if (target->abiversion() >= 2)
8013 {
8014 if (gsym != NULL)
8015 value += object->ppc64_local_entry_offset(gsym);
8016 else
8017 value += object->ppc64_local_entry_offset(r_sym);
8018 }
8019 else
1611bc4a
AM
8020 {
8021 unsigned int dest_shndx;
8022 target->symval_for_branch(relinfo->symtab, gsym, object,
8023 &value, &dest_shndx);
8024 }
9055360d 8025 }
cbcb23fa 8026 Address max_branch_offset = max_branch_delta(r_type);
ec661b9d
AM
8027 if (max_branch_offset != 0
8028 && value - address + max_branch_offset >= 2 * max_branch_offset)
8029 {
8030 Stub_table<size, big_endian>* stub_table
8031 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
8032 if (stub_table != NULL)
8033 {
8034 Address off = stub_table->find_long_branch_entry(object, value);
8035 if (off != invalid_address)
0cfb0717
AM
8036 {
8037 value = (stub_table->stub_address() + stub_table->plt_size()
8038 + off);
8039 has_stub_value = true;
8040 }
0cfdc767 8041 }
ec661b9d 8042 }
42cacb20
DE
8043 }
8044
42cacb20
DE
8045 switch (r_type)
8046 {
dd93cd0a
AM
8047 case elfcpp::R_PPC64_REL64:
8048 case elfcpp::R_POWERPC_REL32:
8049 case elfcpp::R_POWERPC_REL24:
8050 case elfcpp::R_PPC_PLTREL24:
8051 case elfcpp::R_PPC_LOCAL24PC:
8052 case elfcpp::R_POWERPC_REL16:
8053 case elfcpp::R_POWERPC_REL16_LO:
8054 case elfcpp::R_POWERPC_REL16_HI:
8055 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8056 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a
AM
8057 case elfcpp::R_POWERPC_REL14:
8058 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8059 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8060 value -= address;
8061 break;
8062
42cacb20
DE
8063 case elfcpp::R_PPC64_TOC16:
8064 case elfcpp::R_PPC64_TOC16_LO:
8065 case elfcpp::R_PPC64_TOC16_HI:
8066 case elfcpp::R_PPC64_TOC16_HA:
8067 case elfcpp::R_PPC64_TOC16_DS:
8068 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 8069 // Subtract the TOC base address.
c9269dff 8070 value -= (target->got_section()->output_section()->address()
dd93cd0a 8071 + object->toc_base_offset());
42cacb20
DE
8072 break;
8073
cf43a2fe
AM
8074 case elfcpp::R_POWERPC_SECTOFF:
8075 case elfcpp::R_POWERPC_SECTOFF_LO:
8076 case elfcpp::R_POWERPC_SECTOFF_HI:
8077 case elfcpp::R_POWERPC_SECTOFF_HA:
8078 case elfcpp::R_PPC64_SECTOFF_DS:
8079 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8080 if (os != NULL)
8081 value -= os->address();
42cacb20
DE
8082 break;
8083
dd93cd0a
AM
8084 case elfcpp::R_PPC64_TPREL16_DS:
8085 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
8086 case elfcpp::R_PPC64_TPREL16_HIGH:
8087 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8088 if (size != 64)
f9c6b907 8089 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 8090 break;
d8e90251 8091 // Fall through.
dd93cd0a
AM
8092 case elfcpp::R_POWERPC_TPREL16:
8093 case elfcpp::R_POWERPC_TPREL16_LO:
8094 case elfcpp::R_POWERPC_TPREL16_HI:
8095 case elfcpp::R_POWERPC_TPREL16_HA:
8096 case elfcpp::R_POWERPC_TPREL:
8097 case elfcpp::R_PPC64_TPREL16_HIGHER:
8098 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8099 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8100 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8101 // tls symbol values are relative to tls_segment()->vaddr()
8102 value -= tp_offset;
8103 break;
8104
8105 case elfcpp::R_PPC64_DTPREL16_DS:
8106 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8107 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8108 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8109 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8110 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8111 if (size != 64)
8112 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8113 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8114 break;
d8e90251 8115 // Fall through.
dd93cd0a
AM
8116 case elfcpp::R_POWERPC_DTPREL16:
8117 case elfcpp::R_POWERPC_DTPREL16_LO:
8118 case elfcpp::R_POWERPC_DTPREL16_HI:
8119 case elfcpp::R_POWERPC_DTPREL16_HA:
8120 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
8121 case elfcpp::R_PPC64_DTPREL16_HIGH:
8122 case elfcpp::R_PPC64_DTPREL16_HIGHA:
dd93cd0a
AM
8123 // tls symbol values are relative to tls_segment()->vaddr()
8124 value -= dtp_offset;
8125 break;
8126
45965137
AM
8127 case elfcpp::R_PPC64_ADDR64_LOCAL:
8128 if (gsym != NULL)
8129 value += object->ppc64_local_entry_offset(gsym);
8130 else
8131 value += object->ppc64_local_entry_offset(r_sym);
8132 break;
8133
42cacb20
DE
8134 default:
8135 break;
8136 }
8137
dd93cd0a 8138 Insn branch_bit = 0;
42cacb20
DE
8139 switch (r_type)
8140 {
dd93cd0a
AM
8141 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8142 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8143 branch_bit = 1 << 21;
d8e90251 8144 // Fall through.
dd93cd0a
AM
8145 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8146 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8147 {
8148 Insn* iview = reinterpret_cast<Insn*>(view);
8149 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8150 insn &= ~(1 << 21);
8151 insn |= branch_bit;
8152 if (this->is_isa_v2)
8153 {
8154 // Set 'a' bit. This is 0b00010 in BO field for branch
8155 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8156 // for branch on CTR insns (BO == 1a00t or 1a01t).
8157 if ((insn & (0x14 << 21)) == (0x04 << 21))
8158 insn |= 0x02 << 21;
8159 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8160 insn |= 0x08 << 21;
8161 else
8162 break;
8163 }
8164 else
8165 {
8166 // Invert 'y' bit if not the default.
8167 if (static_cast<Signed_address>(value) < 0)
8168 insn ^= 1 << 21;
8169 }
8170 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8171 }
8172 break;
8173
8174 default:
8175 break;
8176 }
8177
aba6bc71
AM
8178 if (size == 64)
8179 {
aba6bc71
AM
8180 switch (r_type)
8181 {
8182 default:
8183 break;
8184
5edad15d
AM
8185 // Multi-instruction sequences that access the GOT/TOC can
8186 // be optimized, eg.
8187 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8188 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8189 // and
8190 // addis ra,r2,0; addi rb,ra,x@toc@l;
8191 // to nop; addi rb,r2,x@toc;
8192 // FIXME: the @got sequence shown above is not yet
8193 // optimized. Note that gcc as of 2017-01-07 doesn't use
8194 // the ELF @got relocs except for TLS, instead using the
8195 // PowerOpen variant of a compiler managed GOT (called TOC).
8196 // The PowerOpen TOC sequence equivalent to the first
8197 // example is optimized.
aba6bc71
AM
8198 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8199 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8200 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8201 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8202 case elfcpp::R_POWERPC_GOT16_HA:
8203 case elfcpp::R_PPC64_TOC16_HA:
d8f5a274 8204 if (parameters->options().toc_optimize())
aba6bc71 8205 {
dcfc7dd4 8206 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8207 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8208 if (r_type == elfcpp::R_PPC64_TOC16_HA
8209 && object->make_toc_relative(target, &value))
8210 {
8211 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8212 == ((15u << 26) | (2 << 16)));
8213 }
8214 if (((insn & ((0x3f << 26) | 0x1f << 16))
8215 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8216 && value + 0x8000 < 0x10000)
aba6bc71
AM
8217 {
8218 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8219 return true;
8220 }
8221 }
8222 break;
8223
8224 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8225 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8226 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8227 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8228 case elfcpp::R_POWERPC_GOT16_LO:
8229 case elfcpp::R_PPC64_GOT16_LO_DS:
8230 case elfcpp::R_PPC64_TOC16_LO:
8231 case elfcpp::R_PPC64_TOC16_LO_DS:
d8f5a274 8232 if (parameters->options().toc_optimize())
aba6bc71 8233 {
dcfc7dd4 8234 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8235 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8236 bool changed = false;
8237 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8238 && object->make_toc_relative(target, &value))
8239 {
8240 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8241 insn ^= (14u << 26) ^ (58u << 26);
8242 r_type = elfcpp::R_PPC64_TOC16_LO;
8243 changed = true;
8244 }
8245 if (ok_lo_toc_insn(insn, r_type)
8246 && value + 0x8000 < 0x10000)
aba6bc71
AM
8247 {
8248 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8249 {
8250 // Transform addic to addi when we change reg.
8251 insn &= ~((0x3f << 26) | (0x1f << 16));
8252 insn |= (14u << 26) | (2 << 16);
8253 }
8254 else
8255 {
8256 insn &= ~(0x1f << 16);
8257 insn |= 2 << 16;
8258 }
5edad15d 8259 changed = true;
aba6bc71 8260 }
5edad15d
AM
8261 if (changed)
8262 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
8263 }
8264 break;
549dba71
AM
8265
8266 case elfcpp::R_PPC64_ENTRY:
8267 value = (target->got_section()->output_section()->address()
8268 + object->toc_base_offset());
8269 if (value + 0x80008000 <= 0xffffffff
8270 && !parameters->options().output_is_position_independent())
8271 {
8272 Insn* iview = reinterpret_cast<Insn*>(view);
8273 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8274 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8275
8276 if ((insn1 & ~0xfffc) == ld_2_12
8277 && insn2 == add_2_2_12)
8278 {
8279 insn1 = lis_2 + ha(value);
8280 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8281 insn2 = addi_2_2 + l(value);
8282 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8283 return true;
8284 }
8285 }
8286 else
8287 {
8288 value -= address;
8289 if (value + 0x80008000 <= 0xffffffff)
8290 {
8291 Insn* iview = reinterpret_cast<Insn*>(view);
8292 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8293 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8294
8295 if ((insn1 & ~0xfffc) == ld_2_12
8296 && insn2 == add_2_2_12)
8297 {
8298 insn1 = addis_2_12 + ha(value);
8299 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8300 insn2 = addi_2_2 + l(value);
8301 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8302 return true;
8303 }
8304 }
8305 }
8306 break;
e3a7574e
AM
8307
8308 case elfcpp::R_POWERPC_REL16_LO:
8309 // If we are generating a non-PIC executable, edit
8310 // 0: addis 2,12,.TOC.-0b@ha
8311 // addi 2,2,.TOC.-0b@l
8312 // used by ELFv2 global entry points to set up r2, to
8313 // lis 2,.TOC.@ha
8314 // addi 2,2,.TOC.@l
8315 // if .TOC. is in range. */
8316 if (value + address - 4 + 0x80008000 <= 0xffffffff
8317 && relnum != 0
8318 && preloc != NULL
8319 && target->abiversion() >= 2
8320 && !parameters->options().output_is_position_independent()
4f038ee5 8321 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
8322 && gsym != NULL
8323 && strcmp(gsym->name(), ".TOC.") == 0)
8324 {
0e123f69 8325 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
8326 Reltype prev_rela(preloc - reloc_size);
8327 if ((prev_rela.get_r_info()
8328 == elfcpp::elf_r_info<size>(r_sym,
8329 elfcpp::R_POWERPC_REL16_HA))
8330 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
8331 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
8332 {
dcfc7dd4 8333 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
8334 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
8335 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
8336
8337 if ((insn1 & 0xffff0000) == addis_2_12
8338 && (insn2 & 0xffff0000) == addi_2_2)
8339 {
8340 insn1 = lis_2 + ha(value + address - 4);
8341 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
8342 insn2 = addi_2_2 + l(value + address - 4);
8343 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
8344 if (relinfo->rr)
8345 {
8346 relinfo->rr->set_strategy(relnum - 1,
8347 Relocatable_relocs::RELOC_SPECIAL);
8348 relinfo->rr->set_strategy(relnum,
8349 Relocatable_relocs::RELOC_SPECIAL);
8350 }
8351 return true;
8352 }
8353 }
8354 }
8355 break;
aba6bc71
AM
8356 }
8357 }
8358
f4baf0d4 8359 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 8360 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
8361 switch (r_type)
8362 {
8363 case elfcpp::R_POWERPC_ADDR32:
8364 case elfcpp::R_POWERPC_UADDR32:
8365 if (size == 64)
f4baf0d4 8366 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
8367 break;
8368
8369 case elfcpp::R_POWERPC_REL32:
a680de9a 8370 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 8371 if (size == 64)
f4baf0d4 8372 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
8373 break;
8374
dd93cd0a 8375 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 8376 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
8377 break;
8378
b80eed39
AM
8379 case elfcpp::R_POWERPC_ADDR16:
8380 // We really should have three separate relocations,
8381 // one for 16-bit data, one for insns with 16-bit signed fields,
8382 // and one for insns with 16-bit unsigned fields.
8383 overflow = Reloc::CHECK_BITFIELD;
8384 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
8385 overflow = Reloc::CHECK_LOW_INSN;
8386 break;
8387
f9c6b907
AM
8388 case elfcpp::R_POWERPC_ADDR16_HI:
8389 case elfcpp::R_POWERPC_ADDR16_HA:
8390 case elfcpp::R_POWERPC_GOT16_HI:
8391 case elfcpp::R_POWERPC_GOT16_HA:
8392 case elfcpp::R_POWERPC_PLT16_HI:
8393 case elfcpp::R_POWERPC_PLT16_HA:
8394 case elfcpp::R_POWERPC_SECTOFF_HI:
8395 case elfcpp::R_POWERPC_SECTOFF_HA:
8396 case elfcpp::R_PPC64_TOC16_HI:
8397 case elfcpp::R_PPC64_TOC16_HA:
8398 case elfcpp::R_PPC64_PLTGOT16_HI:
8399 case elfcpp::R_PPC64_PLTGOT16_HA:
8400 case elfcpp::R_POWERPC_TPREL16_HI:
8401 case elfcpp::R_POWERPC_TPREL16_HA:
8402 case elfcpp::R_POWERPC_DTPREL16_HI:
8403 case elfcpp::R_POWERPC_DTPREL16_HA:
8404 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8405 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8406 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8407 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8408 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8409 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8410 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8411 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8412 case elfcpp::R_POWERPC_REL16_HI:
8413 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
8414 if (size != 32)
8415 overflow = Reloc::CHECK_HIGH_INSN;
8416 break;
8417
dd93cd0a
AM
8418 case elfcpp::R_POWERPC_REL16:
8419 case elfcpp::R_PPC64_TOC16:
8420 case elfcpp::R_POWERPC_GOT16:
8421 case elfcpp::R_POWERPC_SECTOFF:
8422 case elfcpp::R_POWERPC_TPREL16:
8423 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
8424 case elfcpp::R_POWERPC_GOT_TLSGD16:
8425 case elfcpp::R_POWERPC_GOT_TLSLD16:
8426 case elfcpp::R_POWERPC_GOT_TPREL16:
8427 case elfcpp::R_POWERPC_GOT_DTPREL16:
8428 overflow = Reloc::CHECK_LOW_INSN;
8429 break;
8430
8431 case elfcpp::R_POWERPC_ADDR24:
8432 case elfcpp::R_POWERPC_ADDR14:
8433 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8434 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8435 case elfcpp::R_PPC64_ADDR16_DS:
8436 case elfcpp::R_POWERPC_REL24:
8437 case elfcpp::R_PPC_PLTREL24:
8438 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
8439 case elfcpp::R_PPC64_TPREL16_DS:
8440 case elfcpp::R_PPC64_DTPREL16_DS:
8441 case elfcpp::R_PPC64_TOC16_DS:
8442 case elfcpp::R_PPC64_GOT16_DS:
8443 case elfcpp::R_PPC64_SECTOFF_DS:
8444 case elfcpp::R_POWERPC_REL14:
8445 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8446 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
f4baf0d4 8447 overflow = Reloc::CHECK_SIGNED;
42cacb20 8448 break;
dd93cd0a 8449 }
42cacb20 8450
dcfc7dd4 8451 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
8452 Insn insn = 0;
8453
b80eed39
AM
8454 if (overflow == Reloc::CHECK_LOW_INSN
8455 || overflow == Reloc::CHECK_HIGH_INSN)
8456 {
a680de9a 8457 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 8458
a47622ac
AM
8459 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
8460 overflow = Reloc::CHECK_BITFIELD;
8461 else if (overflow == Reloc::CHECK_LOW_INSN
8462 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
8463 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
8464 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
8465 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
8466 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
8467 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 8468 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
8469 else
8470 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
8471 }
8472
a680de9a 8473 bool maybe_dq_reloc = false;
3ea0a085 8474 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 8475 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
8476 switch (r_type)
8477 {
8478 case elfcpp::R_POWERPC_NONE:
8479 case elfcpp::R_POWERPC_TLS:
8480 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8481 case elfcpp::R_POWERPC_GNU_VTENTRY:
42cacb20
DE
8482 break;
8483
8484 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 8485 case elfcpp::R_PPC64_REL64:
cf43a2fe 8486 case elfcpp::R_PPC64_TOC:
45965137 8487 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
8488 Reloc::addr64(view, value);
8489 break;
8490
8491 case elfcpp::R_POWERPC_TPREL:
8492 case elfcpp::R_POWERPC_DTPREL:
8493 if (size == 64)
8494 Reloc::addr64(view, value);
8495 else
3ea0a085 8496 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
8497 break;
8498
8499 case elfcpp::R_PPC64_UADDR64:
8500 Reloc::addr64_u(view, value);
42cacb20
DE
8501 break;
8502
8503 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 8504 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
8505 break;
8506
acc276d8 8507 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8508 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 8509 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
8510 break;
8511
8512 case elfcpp::R_POWERPC_ADDR24:
8513 case elfcpp::R_POWERPC_REL24:
8514 case elfcpp::R_PPC_PLTREL24:
8515 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 8516 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
8517 break;
8518
dd93cd0a
AM
8519 case elfcpp::R_POWERPC_GOT_DTPREL16:
8520 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
8521 case elfcpp::R_POWERPC_GOT_TPREL16:
8522 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
8523 if (size == 64)
8524 {
ec86f434 8525 // On ppc64 these are all ds form
a680de9a 8526 maybe_dq_reloc = true;
dd93cd0a
AM
8527 break;
8528 }
c25aa1e1 8529 // Fall through.
cf43a2fe 8530 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 8531 case elfcpp::R_POWERPC_REL16:
cf43a2fe 8532 case elfcpp::R_PPC64_TOC16:
42cacb20 8533 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 8534 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
8535 case elfcpp::R_POWERPC_TPREL16:
8536 case elfcpp::R_POWERPC_DTPREL16:
8537 case elfcpp::R_POWERPC_GOT_TLSGD16:
8538 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 8539 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 8540 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 8541 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 8542 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe 8543 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
8544 case elfcpp::R_POWERPC_TPREL16_LO:
8545 case elfcpp::R_POWERPC_DTPREL16_LO:
8546 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8547 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
8548 if (size == 64)
8549 status = Reloc::addr16(view, value, overflow);
8550 else
8551 maybe_dq_reloc = true;
dd93cd0a
AM
8552 break;
8553
8554 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 8555 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
8556 break;
8557
f9c6b907
AM
8558 case elfcpp::R_PPC64_ADDR16_HIGH:
8559 case elfcpp::R_PPC64_TPREL16_HIGH:
8560 case elfcpp::R_PPC64_DTPREL16_HIGH:
8561 if (size == 32)
8562 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8563 goto unsupp;
d8e90251 8564 // Fall through.
cf43a2fe 8565 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 8566 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 8567 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 8568 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe 8569 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
8570 case elfcpp::R_POWERPC_TPREL16_HI:
8571 case elfcpp::R_POWERPC_DTPREL16_HI:
8572 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8573 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8574 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8575 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8576 Reloc::addr16_hi(view, value);
42cacb20
DE
8577 break;
8578
f9c6b907
AM
8579 case elfcpp::R_PPC64_ADDR16_HIGHA:
8580 case elfcpp::R_PPC64_TPREL16_HIGHA:
8581 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8582 if (size == 32)
8583 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8584 goto unsupp;
d8e90251 8585 // Fall through.
cf43a2fe 8586 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8587 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 8588 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 8589 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe 8590 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
8591 case elfcpp::R_POWERPC_TPREL16_HA:
8592 case elfcpp::R_POWERPC_DTPREL16_HA:
8593 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8594 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8595 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8596 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8597 Reloc::addr16_ha(view, value);
42cacb20
DE
8598 break;
8599
a680de9a
PB
8600 case elfcpp::R_POWERPC_REL16DX_HA:
8601 status = Reloc::addr16dx_ha(view, value, overflow);
8602 break;
8603
dd93cd0a
AM
8604 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8605 if (size == 32)
8606 // R_PPC_EMB_NADDR16_LO
8607 goto unsupp;
d8e90251 8608 // Fall through.
dd93cd0a
AM
8609 case elfcpp::R_PPC64_ADDR16_HIGHER:
8610 case elfcpp::R_PPC64_TPREL16_HIGHER:
8611 Reloc::addr16_hi2(view, value);
42cacb20
DE
8612 break;
8613
dd93cd0a
AM
8614 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8615 if (size == 32)
8616 // R_PPC_EMB_NADDR16_HI
8617 goto unsupp;
d8e90251 8618 // Fall through.
dd93cd0a
AM
8619 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8620 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8621 Reloc::addr16_ha2(view, value);
42cacb20
DE
8622 break;
8623
dd93cd0a
AM
8624 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8625 if (size == 32)
8626 // R_PPC_EMB_NADDR16_HA
8627 goto unsupp;
d8e90251 8628 // Fall through.
dd93cd0a
AM
8629 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8630 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8631 Reloc::addr16_hi3(view, value);
42cacb20
DE
8632 break;
8633
dd93cd0a
AM
8634 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8635 if (size == 32)
8636 // R_PPC_EMB_SDAI16
8637 goto unsupp;
d8e90251 8638 // Fall through.
dd93cd0a
AM
8639 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8640 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8641 Reloc::addr16_ha3(view, value);
8642 break;
8643
8644 case elfcpp::R_PPC64_DTPREL16_DS:
8645 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8646 if (size == 32)
8647 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8648 goto unsupp;
d8e90251 8649 // Fall through.
dd93cd0a
AM
8650 case elfcpp::R_PPC64_TPREL16_DS:
8651 case elfcpp::R_PPC64_TPREL16_LO_DS:
8652 if (size == 32)
8653 // R_PPC_TLSGD, R_PPC_TLSLD
8654 break;
d8e90251 8655 // Fall through.
cf43a2fe
AM
8656 case elfcpp::R_PPC64_ADDR16_DS:
8657 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
8658 case elfcpp::R_PPC64_TOC16_DS:
8659 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
8660 case elfcpp::R_PPC64_GOT16_DS:
8661 case elfcpp::R_PPC64_GOT16_LO_DS:
8662 case elfcpp::R_PPC64_SECTOFF_DS:
8663 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 8664 maybe_dq_reloc = true;
dd93cd0a
AM
8665 break;
8666
8667 case elfcpp::R_POWERPC_ADDR14:
8668 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8669 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8670 case elfcpp::R_POWERPC_REL14:
8671 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8672 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 8673 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
8674 break;
8675
8676 case elfcpp::R_POWERPC_COPY:
8677 case elfcpp::R_POWERPC_GLOB_DAT:
8678 case elfcpp::R_POWERPC_JMP_SLOT:
8679 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 8680 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
8681 case elfcpp::R_PPC64_JMP_IREL:
8682 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
8683 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8684 _("unexpected reloc %u in object file"),
8685 r_type);
8686 break;
8687
dd93cd0a
AM
8688 case elfcpp::R_PPC_EMB_SDA21:
8689 if (size == 32)
8690 goto unsupp;
8691 else
8692 {
8693 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8694 }
8695 break;
8696
8697 case elfcpp::R_PPC_EMB_SDA2I16:
8698 case elfcpp::R_PPC_EMB_SDA2REL:
8699 if (size == 32)
8700 goto unsupp;
8701 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
8702 break;
8703
dd93cd0a
AM
8704 case elfcpp::R_POWERPC_PLT32:
8705 case elfcpp::R_POWERPC_PLTREL32:
8706 case elfcpp::R_POWERPC_PLT16_LO:
8707 case elfcpp::R_POWERPC_PLT16_HI:
8708 case elfcpp::R_POWERPC_PLT16_HA:
8709 case elfcpp::R_PPC_SDAREL16:
8710 case elfcpp::R_POWERPC_ADDR30:
8711 case elfcpp::R_PPC64_PLT64:
8712 case elfcpp::R_PPC64_PLTREL64:
8713 case elfcpp::R_PPC64_PLTGOT16:
8714 case elfcpp::R_PPC64_PLTGOT16_LO:
8715 case elfcpp::R_PPC64_PLTGOT16_HI:
8716 case elfcpp::R_PPC64_PLTGOT16_HA:
8717 case elfcpp::R_PPC64_PLT16_LO_DS:
8718 case elfcpp::R_PPC64_PLTGOT16_DS:
8719 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a
AM
8720 case elfcpp::R_PPC_EMB_RELSDA:
8721 case elfcpp::R_PPC_TOC16:
42cacb20 8722 default:
dd93cd0a 8723 unsupp:
42cacb20
DE
8724 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8725 _("unsupported reloc %u"),
8726 r_type);
8727 break;
8728 }
a680de9a
PB
8729
8730 if (maybe_dq_reloc)
8731 {
8732 if (insn == 0)
8733 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8734
8735 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
8736 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8737 && (insn & 3) == 1))
8738 status = Reloc::addr16_dq(view, value, overflow);
8739 else if (size == 64
8740 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8741 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8742 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
8743 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
8744 status = Reloc::addr16_ds(view, value, overflow);
8745 else
8746 status = Reloc::addr16(view, value, overflow);
8747 }
8748
0cfb0717 8749 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
8750 && (has_stub_value
8751 || !(gsym != NULL
282c9750 8752 && gsym->is_undefined()
3ffaac20 8753 && is_branch_reloc(r_type))))
0cfb0717
AM
8754 {
8755 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8756 _("relocation overflow"));
8757 if (has_stub_value)
8758 gold_info(_("try relinking with a smaller --stub-group-size"));
8759 }
42cacb20
DE
8760
8761 return true;
8762}
8763
42cacb20
DE
8764// Relocate section data.
8765
8766template<int size, bool big_endian>
8767void
8768Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
8769 const Relocate_info<size, big_endian>* relinfo,
8770 unsigned int sh_type,
8771 const unsigned char* prelocs,
8772 size_t reloc_count,
8773 Output_section* output_section,
8774 bool needs_special_offset_handling,
8775 unsigned char* view,
c9269dff 8776 Address address,
d83ce4e3
AM
8777 section_size_type view_size,
8778 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
8779{
8780 typedef Target_powerpc<size, big_endian> Powerpc;
8781 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
8782 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
8783 Powerpc_comdat_behavior;
4d625b70
CC
8784 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8785 Classify_reloc;
42cacb20
DE
8786
8787 gold_assert(sh_type == elfcpp::SHT_RELA);
8788
4d625b70
CC
8789 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
8790 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
8791 relinfo,
8792 this,
8793 prelocs,
8794 reloc_count,
8795 output_section,
8796 needs_special_offset_handling,
8797 view,
8798 address,
364c7fa5
ILT
8799 view_size,
8800 reloc_symbol_changes);
42cacb20
DE
8801}
8802
4d625b70 8803template<int size, bool big_endian>
cf43a2fe 8804class Powerpc_scan_relocatable_reloc
42cacb20 8805{
cf43a2fe 8806public:
0e123f69
AM
8807 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8808 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
8809 static const int sh_type = elfcpp::SHT_RELA;
8810
8811 // Return the symbol referred to by the relocation.
8812 static inline unsigned int
8813 get_r_sym(const Reltype* reloc)
8814 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
8815
8816 // Return the type of the relocation.
8817 static inline unsigned int
8818 get_r_type(const Reltype* reloc)
8819 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
8820
cf43a2fe
AM
8821 // Return the strategy to use for a local symbol which is not a
8822 // section symbol, given the relocation type.
8823 inline Relocatable_relocs::Reloc_strategy
8824 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
8825 {
8826 if (r_type == 0 && r_sym == 0)
8827 return Relocatable_relocs::RELOC_DISCARD;
8828 return Relocatable_relocs::RELOC_COPY;
8829 }
8830
8831 // Return the strategy to use for a local symbol which is a section
8832 // symbol, given the relocation type.
8833 inline Relocatable_relocs::Reloc_strategy
8834 local_section_strategy(unsigned int, Relobj*)
8835 {
8836 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
8837 }
8838
8839 // Return the strategy to use for a global symbol, given the
8840 // relocation type, the object, and the symbol index.
8841 inline Relocatable_relocs::Reloc_strategy
8842 global_strategy(unsigned int r_type, Relobj*, unsigned int)
8843 {
8844 if (r_type == elfcpp::R_PPC_PLTREL24)
8845 return Relocatable_relocs::RELOC_SPECIAL;
8846 return Relocatable_relocs::RELOC_COPY;
8847 }
8848};
42cacb20
DE
8849
8850// Scan the relocs during a relocatable link.
8851
8852template<int size, bool big_endian>
8853void
8854Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
8855 Symbol_table* symtab,
8856 Layout* layout,
8857 Sized_relobj_file<size, big_endian>* object,
8858 unsigned int data_shndx,
8859 unsigned int sh_type,
8860 const unsigned char* prelocs,
8861 size_t reloc_count,
8862 Output_section* output_section,
8863 bool needs_special_offset_handling,
8864 size_t local_symbol_count,
8865 const unsigned char* plocal_symbols,
8866 Relocatable_relocs* rr)
42cacb20 8867{
4d625b70
CC
8868 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
8869
42cacb20
DE
8870 gold_assert(sh_type == elfcpp::SHT_RELA);
8871
4d625b70 8872 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
8873 symtab,
8874 layout,
8875 object,
8876 data_shndx,
8877 prelocs,
8878 reloc_count,
8879 output_section,
8880 needs_special_offset_handling,
8881 local_symbol_count,
8882 plocal_symbols,
8883 rr);
8884}
8885
4d625b70
CC
8886// Scan the relocs for --emit-relocs.
8887
8888template<int size, bool big_endian>
8889void
8890Target_powerpc<size, big_endian>::emit_relocs_scan(
8891 Symbol_table* symtab,
8892 Layout* layout,
8893 Sized_relobj_file<size, big_endian>* object,
8894 unsigned int data_shndx,
8895 unsigned int sh_type,
8896 const unsigned char* prelocs,
8897 size_t reloc_count,
8898 Output_section* output_section,
8899 bool needs_special_offset_handling,
8900 size_t local_symbol_count,
8901 const unsigned char* plocal_syms,
8902 Relocatable_relocs* rr)
8903{
8904 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8905 Classify_reloc;
8906 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8907 Emit_relocs_strategy;
8908
8909 gold_assert(sh_type == elfcpp::SHT_RELA);
8910
8911 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8912 symtab,
8913 layout,
8914 object,
8915 data_shndx,
8916 prelocs,
8917 reloc_count,
8918 output_section,
8919 needs_special_offset_handling,
8920 local_symbol_count,
8921 plocal_syms,
8922 rr);
8923}
8924
7404fe1b 8925// Emit relocations for a section.
dd93cd0a
AM
8926// This is a modified version of the function by the same name in
8927// target-reloc.h. Using relocate_special_relocatable for
8928// R_PPC_PLTREL24 would require duplication of the entire body of the
8929// loop, so we may as well duplicate the whole thing.
42cacb20
DE
8930
8931template<int size, bool big_endian>
8932void
7404fe1b 8933Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
8934 const Relocate_info<size, big_endian>* relinfo,
8935 unsigned int sh_type,
8936 const unsigned char* prelocs,
8937 size_t reloc_count,
8938 Output_section* output_section,
62fe925a 8939 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 8940 unsigned char*,
dd93cd0a 8941 Address view_address,
cf43a2fe 8942 section_size_type,
42cacb20
DE
8943 unsigned char* reloc_view,
8944 section_size_type reloc_view_size)
8945{
8946 gold_assert(sh_type == elfcpp::SHT_RELA);
8947
0e123f69
AM
8948 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8949 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
8950 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
8951 // Offset from start of insn to d-field reloc.
8952 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
8953
8954 Powerpc_relobj<size, big_endian>* const object
8955 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8956 const unsigned int local_count = object->local_symbol_count();
8957 unsigned int got2_shndx = object->got2_shndx();
c9269dff 8958 Address got2_addend = 0;
cf43a2fe 8959 if (got2_shndx != 0)
c9269dff
AM
8960 {
8961 got2_addend = object->get_output_section_offset(got2_shndx);
8962 gold_assert(got2_addend != invalid_address);
8963 }
cf43a2fe
AM
8964
8965 unsigned char* pwrite = reloc_view;
7404fe1b 8966 bool zap_next = false;
cf43a2fe
AM
8967 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8968 {
91a65d2f 8969 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
8970 if (strategy == Relocatable_relocs::RELOC_DISCARD)
8971 continue;
8972
8973 Reltype reloc(prelocs);
8974 Reltype_write reloc_write(pwrite);
8975
7404fe1b 8976 Address offset = reloc.get_r_offset();
cf43a2fe 8977 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
8978 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
8979 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
8980 const unsigned int orig_r_sym = r_sym;
8981 typename elfcpp::Elf_types<size>::Elf_Swxword addend
8982 = reloc.get_r_addend();
8983 const Symbol* gsym = NULL;
8984
8985 if (zap_next)
8986 {
8987 // We could arrange to discard these and other relocs for
8988 // tls optimised sequences in the strategy methods, but for
8989 // now do as BFD ld does.
8990 r_type = elfcpp::R_POWERPC_NONE;
8991 zap_next = false;
8992 }
cf43a2fe
AM
8993
8994 // Get the new symbol index.
9215b98b 8995 Output_section* os = NULL;
cf43a2fe
AM
8996 if (r_sym < local_count)
8997 {
8998 switch (strategy)
8999 {
9000 case Relocatable_relocs::RELOC_COPY:
9001 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 9002 if (r_sym != 0)
dd93cd0a 9003 {
7404fe1b
AM
9004 r_sym = object->symtab_index(r_sym);
9005 gold_assert(r_sym != -1U);
dd93cd0a 9006 }
cf43a2fe
AM
9007 break;
9008
9009 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9010 {
9011 // We are adjusting a section symbol. We need to find
9012 // the symbol table index of the section symbol for
9013 // the output section corresponding to input section
9014 // in which this symbol is defined.
9015 gold_assert(r_sym < local_count);
9016 bool is_ordinary;
9017 unsigned int shndx =
9018 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9019 gold_assert(is_ordinary);
9215b98b 9020 os = object->output_section(shndx);
cf43a2fe
AM
9021 gold_assert(os != NULL);
9022 gold_assert(os->needs_symtab_index());
7404fe1b 9023 r_sym = os->symtab_index();
cf43a2fe
AM
9024 }
9025 break;
9026
9027 default:
9028 gold_unreachable();
9029 }
9030 }
9031 else
9032 {
7404fe1b 9033 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
9034 gold_assert(gsym != NULL);
9035 if (gsym->is_forwarder())
9036 gsym = relinfo->symtab->resolve_forwards(gsym);
9037
9038 gold_assert(gsym->has_symtab_index());
7404fe1b 9039 r_sym = gsym->symtab_index();
cf43a2fe
AM
9040 }
9041
9042 // Get the new offset--the location in the output section where
9043 // this relocation should be applied.
cf43a2fe 9044 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9045 offset += offset_in_output_section;
cf43a2fe
AM
9046 else
9047 {
c9269dff
AM
9048 section_offset_type sot_offset =
9049 convert_types<section_offset_type, Address>(offset);
cf43a2fe 9050 section_offset_type new_sot_offset =
c9269dff
AM
9051 output_section->output_offset(object, relinfo->data_shndx,
9052 sot_offset);
cf43a2fe 9053 gold_assert(new_sot_offset != -1);
7404fe1b 9054 offset = new_sot_offset;
cf43a2fe
AM
9055 }
9056
dd93cd0a
AM
9057 // In an object file, r_offset is an offset within the section.
9058 // In an executable or dynamic object, generated by
9059 // --emit-relocs, r_offset is an absolute address.
7404fe1b 9060 if (!parameters->options().relocatable())
dd93cd0a 9061 {
7404fe1b 9062 offset += view_address;
dd93cd0a 9063 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9064 offset -= offset_in_output_section;
dd93cd0a
AM
9065 }
9066
cf43a2fe 9067 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
9068 if (strategy == Relocatable_relocs::RELOC_COPY)
9069 ;
9070 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9071 {
7404fe1b 9072 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9215b98b
AM
9073 gold_assert(os != NULL);
9074 addend = psymval->value(object, addend) - os->address();
cf43a2fe
AM
9075 }
9076 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9077 {
e3a7574e
AM
9078 if (size == 32)
9079 {
9080 if (addend >= 32768)
9081 addend += got2_addend;
9082 }
9083 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9084 {
9085 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 9086 addend -= d_offset;
e3a7574e
AM
9087 }
9088 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9089 {
9090 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 9091 addend -= d_offset + 4;
e3a7574e 9092 }
cf43a2fe
AM
9093 }
9094 else
9095 gold_unreachable();
9096
7404fe1b
AM
9097 if (!parameters->options().relocatable())
9098 {
9099 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9100 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9101 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9102 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9103 {
9104 // First instruction of a global dynamic sequence,
9105 // arg setup insn.
9106 const bool final = gsym == NULL || gsym->final_value_is_known();
9107 switch (this->optimize_tls_gd(final))
9108 {
9109 case tls::TLSOPT_TO_IE:
9110 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9111 - elfcpp::R_POWERPC_GOT_TLSGD16);
9112 break;
9113 case tls::TLSOPT_TO_LE:
9114 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9115 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9116 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9117 else
9118 {
9119 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9120 offset -= d_offset;
7404fe1b
AM
9121 }
9122 break;
9123 default:
9124 break;
9125 }
9126 }
9127 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9128 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9129 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9130 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9131 {
9132 // First instruction of a local dynamic sequence,
9133 // arg setup insn.
9134 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9135 {
9136 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9137 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9138 {
9139 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9140 const Output_section* os = relinfo->layout->tls_segment()
9141 ->first_section();
9142 gold_assert(os != NULL);
9143 gold_assert(os->needs_symtab_index());
9144 r_sym = os->symtab_index();
9145 addend = dtp_offset;
9146 }
9147 else
9148 {
9149 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9150 offset -= d_offset;
7404fe1b
AM
9151 }
9152 }
9153 }
9154 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9155 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9156 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9157 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9158 {
9159 // First instruction of initial exec sequence.
9160 const bool final = gsym == NULL || gsym->final_value_is_known();
9161 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9162 {
9163 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9164 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9165 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9166 else
9167 {
9168 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9169 offset -= d_offset;
7404fe1b
AM
9170 }
9171 }
9172 }
9173 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9174 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9175 {
9176 // Second instruction of a global dynamic sequence,
9177 // the __tls_get_addr call
9178 const bool final = gsym == NULL || gsym->final_value_is_known();
9179 switch (this->optimize_tls_gd(final))
9180 {
9181 case tls::TLSOPT_TO_IE:
9182 r_type = elfcpp::R_POWERPC_NONE;
9183 zap_next = true;
9184 break;
9185 case tls::TLSOPT_TO_LE:
9186 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9187 offset += d_offset;
7404fe1b
AM
9188 zap_next = true;
9189 break;
9190 default:
9191 break;
9192 }
9193 }
9194 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9195 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9196 {
9197 // Second instruction of a local dynamic sequence,
9198 // the __tls_get_addr call
9199 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9200 {
9201 const Output_section* os = relinfo->layout->tls_segment()
9202 ->first_section();
9203 gold_assert(os != NULL);
9204 gold_assert(os->needs_symtab_index());
9205 r_sym = os->symtab_index();
9206 addend = dtp_offset;
9207 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9208 offset += d_offset;
7404fe1b
AM
9209 zap_next = true;
9210 }
9211 }
9212 else if (r_type == elfcpp::R_POWERPC_TLS)
9213 {
9214 // Second instruction of an initial exec sequence
9215 const bool final = gsym == NULL || gsym->final_value_is_known();
9216 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9217 {
9218 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9219 offset += d_offset;
7404fe1b
AM
9220 }
9221 }
9222 }
9223
9224 reloc_write.put_r_offset(offset);
9225 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9226 reloc_write.put_r_addend(addend);
cf43a2fe
AM
9227
9228 pwrite += reloc_size;
9229 }
9230
9231 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9232 == reloc_view_size);
42cacb20
DE
9233}
9234
ec661b9d 9235// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
9236// treatment. This is how we support equality comparisons of function
9237// pointers across shared library boundaries, as described in the
9238// processor specific ABI supplement.
9239
9240template<int size, bool big_endian>
9241uint64_t
9242Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9243{
cf43a2fe
AM
9244 if (size == 32)
9245 {
9246 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
9247 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9248 p != this->stub_tables_.end();
9249 ++p)
9250 {
9251 Address off = (*p)->find_plt_call_entry(gsym);
9252 if (off != invalid_address)
9253 return (*p)->stub_address() + off;
9254 }
c9824451 9255 }
9055360d
AM
9256 else if (this->abiversion() >= 2)
9257 {
faa2211d
AM
9258 Address off = this->glink_section()->find_global_entry(gsym);
9259 if (off != invalid_address)
9055360d
AM
9260 return this->glink_section()->global_entry_address() + off;
9261 }
ec661b9d 9262 gold_unreachable();
c9824451
AM
9263}
9264
9265// Return the PLT address to use for a local symbol.
9266template<int size, bool big_endian>
9267uint64_t
9268Target_powerpc<size, big_endian>::do_plt_address_for_local(
9269 const Relobj* object,
9270 unsigned int symndx) const
9271{
9272 if (size == 32)
9273 {
9274 const Sized_relobj<size, big_endian>* relobj
9275 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
9276 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9277 p != this->stub_tables_.end();
9278 ++p)
9279 {
9280 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
9281 symndx);
9282 if (off != invalid_address)
9283 return (*p)->stub_address() + off;
9284 }
c9824451 9285 }
ec661b9d 9286 gold_unreachable();
c9824451
AM
9287}
9288
9289// Return the PLT address to use for a global symbol.
9290template<int size, bool big_endian>
9291uint64_t
9292Target_powerpc<size, big_endian>::do_plt_address_for_global(
9293 const Symbol* gsym) const
9294{
9295 if (size == 32)
9296 {
ec661b9d
AM
9297 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9298 p != this->stub_tables_.end();
9299 ++p)
9300 {
9301 Address off = (*p)->find_plt_call_entry(gsym);
9302 if (off != invalid_address)
9303 return (*p)->stub_address() + off;
9304 }
cf43a2fe 9305 }
9055360d
AM
9306 else if (this->abiversion() >= 2)
9307 {
faa2211d
AM
9308 Address off = this->glink_section()->find_global_entry(gsym);
9309 if (off != invalid_address)
9055360d
AM
9310 return this->glink_section()->global_entry_address() + off;
9311 }
ec661b9d 9312 gold_unreachable();
42cacb20
DE
9313}
9314
bd73a62d
AM
9315// Return the offset to use for the GOT_INDX'th got entry which is
9316// for a local tls symbol specified by OBJECT, SYMNDX.
9317template<int size, bool big_endian>
9318int64_t
9319Target_powerpc<size, big_endian>::do_tls_offset_for_local(
9320 const Relobj* object,
9321 unsigned int symndx,
9322 unsigned int got_indx) const
9323{
9324 const Powerpc_relobj<size, big_endian>* ppc_object
9325 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
9326 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
9327 {
9328 for (Got_type got_type = GOT_TYPE_TLSGD;
9329 got_type <= GOT_TYPE_TPREL;
9330 got_type = Got_type(got_type + 1))
9331 if (ppc_object->local_has_got_offset(symndx, got_type))
9332 {
9333 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
9334 if (got_type == GOT_TYPE_TLSGD)
9335 off += size / 8;
9336 if (off == got_indx * (size / 8))
9337 {
9338 if (got_type == GOT_TYPE_TPREL)
9339 return -tp_offset;
9340 else
9341 return -dtp_offset;
9342 }
9343 }
9344 }
9345 gold_unreachable();
9346}
9347
9348// Return the offset to use for the GOT_INDX'th got entry which is
9349// for global tls symbol GSYM.
9350template<int size, bool big_endian>
9351int64_t
9352Target_powerpc<size, big_endian>::do_tls_offset_for_global(
9353 Symbol* gsym,
9354 unsigned int got_indx) const
9355{
9356 if (gsym->type() == elfcpp::STT_TLS)
9357 {
9358 for (Got_type got_type = GOT_TYPE_TLSGD;
9359 got_type <= GOT_TYPE_TPREL;
9360 got_type = Got_type(got_type + 1))
9361 if (gsym->has_got_offset(got_type))
9362 {
9363 unsigned int off = gsym->got_offset(got_type);
9364 if (got_type == GOT_TYPE_TLSGD)
9365 off += size / 8;
9366 if (off == got_indx * (size / 8))
9367 {
9368 if (got_type == GOT_TYPE_TPREL)
9369 return -tp_offset;
9370 else
9371 return -dtp_offset;
9372 }
9373 }
9374 }
9375 gold_unreachable();
9376}
9377
42cacb20
DE
9378// The selector for powerpc object files.
9379
9380template<int size, bool big_endian>
9381class Target_selector_powerpc : public Target_selector
9382{
9383public:
9384 Target_selector_powerpc()
edc27beb
AM
9385 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
9386 size, big_endian,
03ef7571
ILT
9387 (size == 64
9388 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
9389 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
9390 (size == 64
9391 ? (big_endian ? "elf64ppc" : "elf64lppc")
9392 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
9393 { }
9394
2e702c99
RM
9395 virtual Target*
9396 do_instantiate_target()
7f055c20 9397 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
9398};
9399
9400Target_selector_powerpc<32, true> target_selector_ppc32;
9401Target_selector_powerpc<32, false> target_selector_ppc32le;
9402Target_selector_powerpc<64, true> target_selector_ppc64;
9403Target_selector_powerpc<64, false> target_selector_ppc64le;
9404
decdd3bc
AM
9405// Instantiate these constants for -O0
9406template<int size, bool big_endian>
9407const int Output_data_glink<size, big_endian>::pltresolve_size;
9408template<int size, bool big_endian>
9055360d
AM
9409const typename Output_data_glink<size, big_endian>::Address
9410 Output_data_glink<size, big_endian>::invalid_address;
9411template<int size, bool big_endian>
decdd3bc
AM
9412const typename Stub_table<size, big_endian>::Address
9413 Stub_table<size, big_endian>::invalid_address;
9414template<int size, bool big_endian>
9415const typename Target_powerpc<size, big_endian>::Address
9416 Target_powerpc<size, big_endian>::invalid_address;
9417
42cacb20 9418} // End anonymous namespace.