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