]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/powerpc.cc
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
d87bef3a 3// Copyright (C) 2008-2023 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"
724436fc 44#include "attributes.h"
42cacb20
DE
45
46namespace
47{
48
49using namespace gold;
50
51template<int size, bool big_endian>
52class Output_data_plt_powerpc;
53
ec661b9d
AM
54template<int size, bool big_endian>
55class Output_data_brlt_powerpc;
56
cf43a2fe
AM
57template<int size, bool big_endian>
58class Output_data_got_powerpc;
59
60template<int size, bool big_endian>
61class Output_data_glink;
62
ec661b9d
AM
63template<int size, bool big_endian>
64class Stub_table;
65
d49044c7
AM
66template<int size, bool big_endian>
67class Output_data_save_res;
68
a3e60ddb
AM
69template<int size, bool big_endian>
70class Target_powerpc;
71
72struct Stub_table_owner
73{
dc60b26d
AM
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
a3e60ddb
AM
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80};
81
32f59844 82template<int size>
23cedd1d
AM
83inline bool is_branch_reloc(unsigned int);
84
85template<int size>
86inline bool is_plt16_reloc(unsigned int);
4d9aa155 87
590b87ff
AM
88// Counter incremented on every Powerpc_relobj constructed.
89static uint32_t object_id = 0;
90
cf43a2fe
AM
91template<int size, bool big_endian>
92class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93{
94public:
dd93cd0a 95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 98
cf43a2fe
AM
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
590b87ff
AM
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
724436fc
AM
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
b4f7960d
AM
107 {
108 this->set_abiversion(0);
109 }
cf43a2fe
AM
110
111 ~Powerpc_relobj()
724436fc 112 { delete this->attributes_section_data_; }
cf43a2fe 113
b4f7960d
AM
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
5edad15d
AM
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
c9269dff 164 // The .got2 section shndx.
cf43a2fe
AM
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
c9269dff 169 return this->special_;
cf43a2fe
AM
170 else
171 return 0;
172 }
173
c9269dff
AM
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 189 this->opd_ent_.resize(count);
c9269dff
AM
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 199 if (value != NULL)
bfdfa4cd
AM
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
c9269dff
AM
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
dd93cd0a 206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
c9269dff
AM
230 }
231
e81fea4d
AM
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
c9269dff
AM
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
5edad15d
AM
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
c9b8abb7
AM
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
257
26a4e9cb
AM
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
c9269dff
AM
260 void
261 do_read_relocs(Read_relocs_data*);
262
cf43a2fe
AM
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
265
ec4dbad3
AM
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
270 {
271 if (size == 64 && this->opd_shndx() != 0)
272 {
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
278 }
279 return true;
280 }
281
6c77229c
AM
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
285
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
efc6fa12 289 add_reference(Relobj* src_obj,
6c77229c
AM
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292 {
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
295 }
296
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301 {
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
306 }
307
308 void
309 process_gc_mark(Symbol_table* symtab)
310 {
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
313 {
314 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
316 }
317 }
318
d8f5a274
AM
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
322
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
326
ec661b9d
AM
327 void
328 set_has_14bit_branch(unsigned int shndx)
329 {
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
333 }
334
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
338
339 void
a3e60ddb 340 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 341 {
a3e60ddb 342 if (shndx >= this->stub_table_index_.size())
dc60b26d 343 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 344 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
345 }
346
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
349 {
a3e60ddb
AM
350 if (shndx < this->stub_table_index_.size())
351 {
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
a3e60ddb 358 }
ec661b9d
AM
359 return NULL;
360 }
361
a3e60ddb
AM
362 void
363 clear_stub_table()
364 {
365 this->stub_table_index_.clear();
366 }
367
590b87ff
AM
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
371
b4f7960d
AM
372 int
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
379
7ee7ff70
AM
380 unsigned int
381 st_other (unsigned int symndx) const
382 {
383 return this->st_other_[symndx];
384 }
385
b4f7960d
AM
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393
32f59844
AM
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
397
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
401
724436fc
AM
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
406
cf43a2fe 407private:
bfdfa4cd
AM
408 struct Opd_ent
409 {
410 unsigned int shndx;
c6de8ed4
AM
411 bool discard : 1;
412 bool gc_mark : 1;
26a4e9cb 413 Address off;
bfdfa4cd
AM
414 };
415
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
c9269dff
AM
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
429
590b87ff
AM
430 // Per object unique identifier
431 uint32_t uniq_;
432
c9269dff
AM
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
bfdfa4cd 435
5edad15d
AM
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
439
d8f5a274
AM
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
443
bfdfa4cd
AM
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
450
590b87ff
AM
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
453
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
457
c9269dff
AM
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
bfdfa4cd 460 // relocation on this word. The following vector records the
c9269dff 461 // section and offset specified by these relocations.
bfdfa4cd
AM
462 std::vector<Opd_ent> opd_ent_;
463
e81fea4d 464 // References made to this object's .opd section when running
bfdfa4cd
AM
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
e81fea4d 467 Access_from access_from_map_;
ec661b9d
AM
468
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
471
472 // The stub table to use for a given input section.
a3e60ddb 473 std::vector<unsigned int> stub_table_index_;
b4f7960d 474
b4f7960d
AM
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
724436fc
AM
477
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
cf43a2fe
AM
480};
481
dc3714f3
AM
482template<int size, bool big_endian>
483class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484{
485public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
724436fc
AM
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
b4f7960d
AM
493 {
494 this->set_abiversion(0);
495 }
dc3714f3
AM
496
497 ~Powerpc_dynobj()
724436fc 498 { delete this->attributes_section_data_; }
dc3714f3
AM
499
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
504
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
508 {
509 return this->opd_shndx_;
510 }
511
512 // The .opd section address.
513 Address
514 opd_address() const
515 {
516 return this->opd_address_;
517 }
518
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
522 {
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
525 }
526
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
530 {
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
537 }
538
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
542 {
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
547 }
548
b4f7960d
AM
549 int
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
556
724436fc
AM
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
561
dc3714f3
AM
562private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
565 {
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
568 { }
569
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
573
574 Address start;
575 Address len;
576 unsigned int shndx;
577 };
578
579 struct Opd_ent
580 {
581 unsigned int shndx;
582 Address off;
583 };
584
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
589
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
593
590b87ff
AM
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
596
dc3714f3
AM
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
724436fc
AM
602
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
dc3714f3
AM
605};
606
5edad15d
AM
607// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608// base class will emit.
609
610template<int sh_type, int size, bool big_endian>
611class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612{
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616 { }
617
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622};
623
f19c3684
AM
624// The types of GOT entries needed for this platform.
625// These values are exposed to the ABI in an incremental link, but
626// powerpc does not support incremental linking as yet.
627enum Got_type
628 {
629 GOT_TYPE_STANDARD = 0,
630 GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
631 GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
632 GOT_TYPE_TPREL = 3, // entry for @got@tprel
633 GOT_TYPE_SMALL = 4,
634 GOT_TYPE_SMALL_TLSGD = 5,
635 GOT_TYPE_SMALL_DTPREL = 6,
636 GOT_TYPE_SMALL_TPREL = 7
637 };
638
42cacb20
DE
639template<int size, bool big_endian>
640class Target_powerpc : public Sized_target<size, big_endian>
641{
642 public:
d83ce4e3
AM
643 typedef
644 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 645 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 646 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 647 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 648 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
649 // Offset of tp and dtp pointers from start of TLS block.
650 static const Address tp_offset = 0x7000;
651 static const Address dtp_offset = 0x8000;
42cacb20
DE
652
653 Target_powerpc()
654 : Sized_target<size, big_endian>(&powerpc_info),
f19c3684
AM
655 got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
656 brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 657 tlsld_got_offset_(-1U),
7e57d19e 658 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
63e5eea2 659 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
7ee7ff70 660 plt_localentry0_init_(false), has_localentry0_(false),
4e0e019f 661 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
7ee7ff70 662 relax_failed_(false), relax_fail_count_(0),
34e0882b 663 stub_group_size_(0), savres_section_(0),
724436fc
AM
664 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
665 attributes_section_data_(NULL),
666 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
42cacb20
DE
667 {
668 }
669
2e702c99 670 // Process the relocations to determine unreferenced sections for
6d03d481
ST
671 // garbage collection.
672 void
ad0f2072 673 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
674 Layout* layout,
675 Sized_relobj_file<size, big_endian>* object,
676 unsigned int data_shndx,
677 unsigned int sh_type,
678 const unsigned char* prelocs,
679 size_t reloc_count,
680 Output_section* output_section,
681 bool needs_special_offset_handling,
682 size_t local_symbol_count,
683 const unsigned char* plocal_symbols);
6d03d481 684
42cacb20
DE
685 // Scan the relocations to look for symbol adjustments.
686 void
ad0f2072 687 scan_relocs(Symbol_table* symtab,
42cacb20 688 Layout* layout,
6fa2a40b 689 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
690 unsigned int data_shndx,
691 unsigned int sh_type,
692 const unsigned char* prelocs,
693 size_t reloc_count,
694 Output_section* output_section,
695 bool needs_special_offset_handling,
696 size_t local_symbol_count,
697 const unsigned char* plocal_symbols);
921b5322
AM
698
699 // Map input .toc section to output .got section.
700 const char*
701 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
702 {
703 if (size == 64 && strcmp(name, ".toc") == 0)
704 {
705 *plen = 4;
706 return ".got";
707 }
708 return NULL;
709 }
710
f3a0ed29
AM
711 // Provide linker defined save/restore functions.
712 void
713 define_save_restore_funcs(Layout*, Symbol_table*);
714
ec661b9d
AM
715 // No stubs unless a final link.
716 bool
717 do_may_relax() const
718 { return !parameters->options().relocatable(); }
719
720 bool
721 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
722
9d5781f8
AM
723 void
724 do_plt_fde_location(const Output_data*, unsigned char*,
725 uint64_t*, off_t*) const;
726
ec661b9d
AM
727 // Stash info about branches, for stub generation.
728 void
729 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
730 unsigned int data_shndx, Address r_offset,
731 unsigned int r_type, unsigned int r_sym, Address addend)
732 {
733 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
734 this->branch_info_.push_back(info);
735 if (r_type == elfcpp::R_POWERPC_REL14
736 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
737 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
738 ppc_object->set_has_14bit_branch(data_shndx);
739 }
740
7e57d19e
AM
741 // Return whether the last branch is a plt call, and if so, mark the
742 // branch as having an R_PPC64_TOCSAVE.
743 bool
744 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
745 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
746 {
747 return (size == 64
748 && !this->branch_info_.empty()
749 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
750 r_offset, this, symtab));
751 }
752
753 // Say the given location, that of a nop in a function prologue with
754 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
755 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
756 void
757 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
758 unsigned int shndx, Address offset)
759 {
760 Symbol_location loc;
761 loc.object = ppc_object;
762 loc.shndx = shndx;
763 loc.offset = offset;
764 this->tocsave_loc_.insert(loc);
765 }
766
767 // Accessor
b0d0d02b 768 const Tocsave_loc*
7e57d19e
AM
769 tocsave_loc() const
770 {
b0d0d02b 771 return &this->tocsave_loc_;
7e57d19e
AM
772 }
773
f43ba157
AM
774 void
775 do_define_standard_symbols(Symbol_table*, Layout*);
776
42cacb20
DE
777 // Finalize the sections.
778 void
f59f41f3 779 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
780
781 // Return the value to use for a dynamic which requires special
782 // treatment.
783 uint64_t
784 do_dynsym_value(const Symbol*) const;
785
c9824451
AM
786 // Return the PLT address to use for a local symbol.
787 uint64_t
788 do_plt_address_for_local(const Relobj*, unsigned int) const;
789
790 // Return the PLT address to use for a global symbol.
791 uint64_t
792 do_plt_address_for_global(const Symbol*) const;
793
bd73a62d
AM
794 // Return the offset to use for the GOT_INDX'th got entry which is
795 // for a local tls symbol specified by OBJECT, SYMNDX.
796 int64_t
797 do_tls_offset_for_local(const Relobj* object,
798 unsigned int symndx,
f19c3684 799 Output_data_got_base* got,
e4d49a0f
AM
800 unsigned int got_indx,
801 uint64_t addend) const;
bd73a62d
AM
802
803 // Return the offset to use for the GOT_INDX'th got entry which is
804 // for global tls symbol GSYM.
805 int64_t
f19c3684
AM
806 do_tls_offset_for_global(Symbol* gsym,
807 Output_data_got_base* got, unsigned int got_indx,
e4d49a0f 808 uint64_t addend) const;
bd73a62d 809
dc3714f3
AM
810 void
811 do_function_location(Symbol_location*) const;
812
4d9aa155
AM
813 bool
814 do_can_check_for_function_pointers() const
815 { return true; }
816
bbec1a5d
AM
817 // Adjust -fsplit-stack code which calls non-split-stack code.
818 void
819 do_calls_non_split(Relobj* object, unsigned int shndx,
820 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 821 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
822 unsigned char* view, section_size_type view_size,
823 std::string* from, std::string* to) const;
824
42cacb20
DE
825 // Relocate a section.
826 void
827 relocate_section(const Relocate_info<size, big_endian>*,
828 unsigned int sh_type,
829 const unsigned char* prelocs,
830 size_t reloc_count,
831 Output_section* output_section,
832 bool needs_special_offset_handling,
833 unsigned char* view,
c9269dff 834 Address view_address,
364c7fa5
ILT
835 section_size_type view_size,
836 const Reloc_symbol_changes*);
42cacb20
DE
837
838 // Scan the relocs during a relocatable link.
839 void
ad0f2072 840 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 841 Layout* layout,
6fa2a40b 842 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
843 unsigned int data_shndx,
844 unsigned int sh_type,
845 const unsigned char* prelocs,
846 size_t reloc_count,
847 Output_section* output_section,
848 bool needs_special_offset_handling,
849 size_t local_symbol_count,
850 const unsigned char* plocal_symbols,
851 Relocatable_relocs*);
852
4d625b70
CC
853 // Scan the relocs for --emit-relocs.
854 void
855 emit_relocs_scan(Symbol_table* symtab,
856 Layout* layout,
857 Sized_relobj_file<size, big_endian>* object,
858 unsigned int data_shndx,
859 unsigned int sh_type,
860 const unsigned char* prelocs,
861 size_t reloc_count,
862 Output_section* output_section,
863 bool needs_special_offset_handling,
864 size_t local_symbol_count,
865 const unsigned char* plocal_syms,
866 Relocatable_relocs* rr);
867
7404fe1b 868 // Emit relocations for a section.
42cacb20 869 void
7404fe1b
AM
870 relocate_relocs(const Relocate_info<size, big_endian>*,
871 unsigned int sh_type,
872 const unsigned char* prelocs,
873 size_t reloc_count,
874 Output_section* output_section,
62fe925a
RM
875 typename elfcpp::Elf_types<size>::Elf_Off
876 offset_in_output_section,
7404fe1b
AM
877 unsigned char*,
878 Address view_address,
879 section_size_type,
880 unsigned char* reloc_view,
881 section_size_type reloc_view_size);
42cacb20
DE
882
883 // Return whether SYM is defined by the ABI.
884 bool
9c2d0ef9 885 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 886 {
cf43a2fe 887 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
888 }
889
f19c3684 890 // Return the size of the GOT section, for incremental linking
42cacb20 891 section_size_type
0e70b911 892 got_size() const
42cacb20
DE
893 {
894 gold_assert(this->got_ != NULL);
f19c3684
AM
895 return this->got_->data_size() + (this->biggot_
896 ? this->biggot_->data_size() : 0);
42cacb20
DE
897 }
898
cf43a2fe
AM
899 // Get the PLT section.
900 const Output_data_plt_powerpc<size, big_endian>*
901 plt_section() const
902 {
903 gold_assert(this->plt_ != NULL);
904 return this->plt_;
905 }
906
e5d5f5ed
AM
907 // Get the IPLT section.
908 const Output_data_plt_powerpc<size, big_endian>*
909 iplt_section() const
910 {
911 gold_assert(this->iplt_ != NULL);
912 return this->iplt_;
913 }
914
2d7ad24e
AM
915 // Get the LPLT section.
916 const Output_data_plt_powerpc<size, big_endian>*
917 lplt_section() const
918 {
919 return this->lplt_;
920 }
921
08be3224
AM
922 // Return the plt offset and section for the given global sym.
923 Address
924 plt_off(const Symbol* gsym,
925 const Output_data_plt_powerpc<size, big_endian>** sec) const
926 {
927 if (gsym->type() == elfcpp::STT_GNU_IFUNC
928 && gsym->can_use_relative_reloc(false))
929 *sec = this->iplt_section();
f21dbd7c 930 else if (!parameters->doing_static_link())
08be3224 931 *sec = this->plt_section();
f21dbd7c
AM
932 else
933 *sec = this->lplt_section();
08be3224
AM
934 return gsym->plt_offset();
935 }
936
937 // Return the plt offset and section for the given local sym.
938 Address
939 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
940 unsigned int local_sym_index,
941 const Output_data_plt_powerpc<size, big_endian>** sec) const
942 {
2d7ad24e
AM
943 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
944 if (lsym->is_ifunc_symbol())
945 *sec = this->iplt_section();
946 else
947 *sec = this->lplt_section();
08be3224
AM
948 return relobj->local_plt_offset(local_sym_index);
949 }
950
cf43a2fe
AM
951 // Get the .glink section.
952 const Output_data_glink<size, big_endian>*
953 glink_section() const
954 {
955 gold_assert(this->glink_ != NULL);
956 return this->glink_;
957 }
958
9055360d
AM
959 Output_data_glink<size, big_endian>*
960 glink_section()
961 {
962 gold_assert(this->glink_ != NULL);
963 return this->glink_;
964 }
965
9d5781f8
AM
966 bool has_glink() const
967 { return this->glink_ != NULL; }
968
cf43a2fe
AM
969 // Get the GOT section.
970 const Output_data_got_powerpc<size, big_endian>*
f19c3684 971 got_section(Got_type got_type) const
cf43a2fe
AM
972 {
973 gold_assert(this->got_ != NULL);
f19c3684
AM
974 if (size == 32 || (got_type & GOT_TYPE_SMALL))
975 return this->got_;
976 gold_assert(this->biggot_ != NULL);
977 return this->biggot_;
cf43a2fe
AM
978 }
979
26a4e9cb
AM
980 // Get the GOT section, creating it if necessary.
981 Output_data_got_powerpc<size, big_endian>*
f19c3684 982 got_section(Symbol_table*, Layout*, Got_type);
26a4e9cb 983
a19da04b
AM
984 // The toc/got pointer reg will be set to this value.
985 Address
986 toc_pointer() const
987 {
988 return this->got_->address() + this->got_->g_o_t();
989 }
990
991 // Offset of base used to access the GOT/TOC relative to the GOT section.
992 Address
f19c3684 993 got_base_offset(Got_type got_type) const
a19da04b 994 {
f19c3684
AM
995 if (size == 32 || (got_type & GOT_TYPE_SMALL))
996 return this->got_->g_o_t();
997 return this->toc_pointer() - this->biggot_->address();
a19da04b
AM
998 }
999
cf43a2fe
AM
1000 Object*
1001 do_make_elf_object(const std::string&, Input_file*, off_t,
1002 const elfcpp::Ehdr<size, big_endian>&);
1003
0e70b911
CC
1004 // Return the number of entries in the GOT.
1005 unsigned int
1006 got_entry_count() const
1007 {
1008 if (this->got_ == NULL)
1009 return 0;
1010 return this->got_size() / (size / 8);
1011 }
1012
1013 // Return the number of entries in the PLT.
1014 unsigned int
1015 plt_entry_count() const;
1016
1017 // Return the offset of the first non-reserved PLT entry.
1018 unsigned int
b4f7960d
AM
1019 first_plt_entry_offset() const
1020 {
1021 if (size == 32)
1022 return 0;
1023 if (this->abiversion() >= 2)
1024 return 16;
1025 return 24;
1026 }
0e70b911
CC
1027
1028 // Return the size of each PLT entry.
1029 unsigned int
b4f7960d
AM
1030 plt_entry_size() const
1031 {
1032 if (size == 32)
1033 return 4;
1034 if (this->abiversion() >= 2)
1035 return 8;
1036 return 24;
1037 }
0e70b911 1038
d49044c7
AM
1039 Output_data_save_res<size, big_endian>*
1040 savres_section() const
1041 {
1042 return this->savres_section_;
1043 }
1044
e81fea4d
AM
1045 // Add any special sections for this symbol to the gc work list.
1046 // For powerpc64, this adds the code section of a function
1047 // descriptor.
1048 void
1049 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1050
1051 // Handle target specific gc actions when adding a gc reference from
1052 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1053 // and DST_OFF. For powerpc64, this adds a referenc to the code
1054 // section of a function descriptor.
1055 void
1056 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 1057 Relobj* src_obj,
e81fea4d 1058 unsigned int src_shndx,
efc6fa12 1059 Relobj* dst_obj,
e81fea4d
AM
1060 unsigned int dst_shndx,
1061 Address dst_off) const;
1062
ec661b9d
AM
1063 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1064 const Stub_tables&
1065 stub_tables() const
1066 { return this->stub_tables_; }
1067
1068 const Output_data_brlt_powerpc<size, big_endian>*
1069 brlt_section() const
1070 { return this->brlt_section_; }
1071
1072 void
1073 add_branch_lookup_table(Address to)
1074 {
1075 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1076 this->branch_lookup_table_.insert(std::make_pair(to, off));
1077 }
1078
1079 Address
1080 find_branch_lookup_table(Address to)
1081 {
1082 typename Branch_lookup_table::const_iterator p
1083 = this->branch_lookup_table_.find(to);
1084 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1085 }
1086
1087 void
1088 write_branch_lookup_table(unsigned char *oview)
1089 {
1090 for (typename Branch_lookup_table::const_iterator p
1091 = this->branch_lookup_table_.begin();
1092 p != this->branch_lookup_table_.end();
1093 ++p)
1094 {
4d5effb9 1095 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
1096 }
1097 }
1098
590b87ff
AM
1099 // Wrapper used after relax to define a local symbol in output data,
1100 // from the end if value < 0.
1101 void
1102 define_local(Symbol_table* symtab, const char* name,
1103 Output_data* od, Address value, unsigned int symsize)
1104 {
1105 Symbol* sym
1106 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1107 od, value, symsize, elfcpp::STT_NOTYPE,
1108 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1109 static_cast<Signed_address>(value) < 0,
1110 false);
1111 // We are creating this symbol late, so need to fix up things
1112 // done early in Layout::finalize.
1113 sym->set_dynsym_index(-1U);
1114 }
1115
63e5eea2
AM
1116 void
1117 set_power10_relocs()
1118 {
1119 this->power10_relocs_ = true;
1120 }
1121
e4dff765 1122 bool
7c1f4227 1123 power10_stubs() const
e4dff765 1124 {
63e5eea2
AM
1125 return (this->power10_relocs_
1126 && (parameters->options().power10_stubs_enum()
1127 != General_options::POWER10_STUBS_NO));
afd2ea23
AM
1128 }
1129
1130 bool
1131 power10_stubs_auto() const
1132 {
1133 return (parameters->options().power10_stubs_enum()
1134 == General_options::POWER10_STUBS_AUTO);
e4dff765
AM
1135 }
1136
9e69ed50
AM
1137 bool
1138 plt_thread_safe() const
1139 { return this->plt_thread_safe_; }
1140
7ee7ff70
AM
1141 bool
1142 plt_localentry0() const
1143 { return this->plt_localentry0_; }
1144
63e5eea2
AM
1145 bool
1146 has_localentry0() const
1147 { return this->has_localentry0_; }
1148
7ee7ff70
AM
1149 void
1150 set_has_localentry0()
1151 {
1152 this->has_localentry0_ = true;
1153 }
1154
1155 bool
1156 is_elfv2_localentry0(const Symbol* gsym) const
1157 {
1158 return (size == 64
1159 && this->abiversion() >= 2
1160 && this->plt_localentry0()
1161 && gsym->type() == elfcpp::STT_FUNC
1162 && gsym->is_defined()
565ed01a
AM
1163 && gsym->nonvis() >> 3 == 0
1164 && !gsym->non_zero_localentry());
7ee7ff70
AM
1165 }
1166
1167 bool
1168 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1169 unsigned int r_sym) const
1170 {
1171 const Powerpc_relobj<size, big_endian>* ppc_object
1172 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1173
1174 if (size == 64
1175 && this->abiversion() >= 2
1176 && this->plt_localentry0()
1177 && ppc_object->st_other(r_sym) >> 5 == 0)
1178 {
1179 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1180 bool is_ordinary;
1181 if (!psymval->is_ifunc_symbol()
1182 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1183 && is_ordinary)
1184 return true;
1185 }
1186 return false;
1187 }
1188
252dcdf4
AM
1189 bool
1190 tprel_opt() const
4e0e019f 1191 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
252dcdf4
AM
1192
1193 void
4e0e019f
AM
1194 set_no_tprel_opt()
1195 { this->no_tprel_opt_ = true; }
252dcdf4 1196
565ed01a
AM
1197 // Remember any symbols seen with non-zero localentry, even those
1198 // not providing a definition
1199 bool
1200 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1201 const char*)
1202 {
1203 if (size == 64)
1204 {
1205 unsigned char st_other = sym.get_st_other();
1206 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1207 to->set_non_zero_localentry();
1208 }
1209 // We haven't resolved anything, continue normal processing.
1210 return false;
1211 }
1212
b4f7960d 1213 int
aacb3b6d 1214 abiversion() const
b4f7960d
AM
1215 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1216
1217 void
aacb3b6d 1218 set_abiversion(int ver)
b4f7960d
AM
1219 {
1220 elfcpp::Elf_Word flags = this->processor_specific_flags();
1221 flags &= ~elfcpp::EF_PPC64_ABI;
1222 flags |= ver & elfcpp::EF_PPC64_ABI;
1223 this->set_processor_specific_flags(flags);
1224 }
1225
34e0882b
AM
1226 Symbol*
1227 tls_get_addr_opt() const
1228 { return this->tls_get_addr_opt_; }
1229
1230 Symbol*
1231 tls_get_addr() const
1232 { return this->tls_get_addr_; }
1233
1234 // If optimizing __tls_get_addr calls, whether this is the
1235 // "__tls_get_addr" symbol.
1236 bool
1237 is_tls_get_addr_opt(const Symbol* gsym) const
1238 {
1239 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1240 || gsym == this->tls_get_addr_opt_);
1241 }
1242
1243 bool
1244 replace_tls_get_addr(const Symbol* gsym) const
1245 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1246
1247 void
1248 set_has_tls_get_addr_opt()
1249 { this->has_tls_get_addr_opt_ = true; }
1250
aacb3b6d 1251 // Offset to toc save stack slot
b4f7960d 1252 int
aacb3b6d 1253 stk_toc() const
b4f7960d
AM
1254 { return this->abiversion() < 2 ? 40 : 24; }
1255
34e0882b
AM
1256 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1257 // so use the CR save slot. Used only by __tls_get_addr call stub,
1258 // relying on __tls_get_addr not saving CR itself.
1259 int
1260 stk_linker() const
1261 { return this->abiversion() < 2 ? 32 : 8; }
1262
724436fc
AM
1263 // Merge object attributes from input object with those in the output.
1264 void
6f3fe02b 1265 merge_object_attributes(const Object*, const Attributes_section_data*);
724436fc 1266
f21dbd7c
AM
1267 bool
1268 symval_for_branch(const Symbol_table* symtab,
1269 const Sized_symbol<size>* gsym,
1270 Powerpc_relobj<size, big_endian>* object,
1271 Address *value, unsigned int *dest_shndx);
1272
42cacb20
DE
1273 private:
1274
e3deeb9c
AM
1275 class Track_tls
1276 {
1277 public:
1278 enum Tls_get_addr
1279 {
1280 NOT_EXPECTED = 0,
1281 EXPECTED = 1,
1282 SKIP = 2,
1283 NORMAL = 3
1284 };
1285
1286 Track_tls()
aacb3b6d 1287 : tls_get_addr_state_(NOT_EXPECTED),
e3deeb9c
AM
1288 relinfo_(NULL), relnum_(0), r_offset_(0)
1289 { }
1290
1291 ~Track_tls()
1292 {
aacb3b6d 1293 if (this->tls_get_addr_state_ != NOT_EXPECTED)
e3deeb9c
AM
1294 this->missing();
1295 }
1296
1297 void
1298 missing(void)
1299 {
1300 if (this->relinfo_ != NULL)
1301 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1302 _("missing expected __tls_get_addr call"));
1303 }
1304
1305 void
1306 expect_tls_get_addr_call(
1307 const Relocate_info<size, big_endian>* relinfo,
1308 size_t relnum,
1309 Address r_offset)
1310 {
aacb3b6d 1311 this->tls_get_addr_state_ = EXPECTED;
e3deeb9c
AM
1312 this->relinfo_ = relinfo;
1313 this->relnum_ = relnum;
1314 this->r_offset_ = r_offset;
1315 }
1316
1317 void
1318 expect_tls_get_addr_call()
aacb3b6d 1319 { this->tls_get_addr_state_ = EXPECTED; }
e3deeb9c
AM
1320
1321 void
1322 skip_next_tls_get_addr_call()
aacb3b6d 1323 {this->tls_get_addr_state_ = SKIP; }
e3deeb9c
AM
1324
1325 Tls_get_addr
34e0882b
AM
1326 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1327 unsigned int r_type, const Symbol* gsym)
e3deeb9c 1328 {
32f59844
AM
1329 bool is_tls_call
1330 = ((r_type == elfcpp::R_POWERPC_REL24
1331 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 1332 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
32f59844
AM
1333 || r_type == elfcpp::R_PPC_PLTREL24
1334 || is_plt16_reloc<size>(r_type)
e4dff765
AM
1335 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1336 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
32f59844
AM
1337 || r_type == elfcpp::R_POWERPC_PLTSEQ
1338 || r_type == elfcpp::R_POWERPC_PLTCALL
1339 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1340 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1341 && gsym != NULL
1342 && (gsym == target->tls_get_addr()
1343 || gsym == target->tls_get_addr_opt()));
aacb3b6d
AM
1344 Tls_get_addr last_tls = this->tls_get_addr_state_;
1345 this->tls_get_addr_state_ = NOT_EXPECTED;
e3deeb9c
AM
1346 if (is_tls_call && last_tls != EXPECTED)
1347 return last_tls;
1348 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1349 {
1350 this->missing();
1351 return EXPECTED;
1352 }
1353 return NORMAL;
1354 }
1355
1356 private:
1357 // What we're up to regarding calls to __tls_get_addr.
1358 // On powerpc, the branch and link insn making a call to
1359 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1360 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
32f59844 1361 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
e3deeb9c
AM
1362 // The marker relocation always comes first, and has the same
1363 // symbol as the reloc on the insn setting up the __tls_get_addr
1364 // argument. This ties the arg setup insn with the call insn,
1365 // allowing ld to safely optimize away the call. We check that
1366 // every call to __tls_get_addr has a marker relocation, and that
1367 // every marker relocation is on a call to __tls_get_addr.
aacb3b6d 1368 Tls_get_addr tls_get_addr_state_;
e3deeb9c
AM
1369 // Info about the last reloc for error message.
1370 const Relocate_info<size, big_endian>* relinfo_;
1371 size_t relnum_;
1372 Address r_offset_;
1373 };
1374
42cacb20 1375 // The class which scans relocations.
e3deeb9c 1376 class Scan : protected Track_tls
42cacb20
DE
1377 {
1378 public:
bfdfa4cd
AM
1379 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1380
42cacb20 1381 Scan()
e3deeb9c 1382 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1383 { }
1384
95a2c8d6 1385 static inline int
88b8e639 1386 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1387
42cacb20 1388 inline void
ad0f2072 1389 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1390 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1391 unsigned int data_shndx,
1392 Output_section* output_section,
1393 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1394 const elfcpp::Sym<size, big_endian>& lsym,
1395 bool is_discarded);
42cacb20
DE
1396
1397 inline void
ad0f2072 1398 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1399 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1400 unsigned int data_shndx,
1401 Output_section* output_section,
1402 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1403 Symbol* gsym);
1404
21bb3914
ST
1405 inline bool
1406 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1407 Target_powerpc* ,
f6971787 1408 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1409 unsigned int ,
2e702c99
RM
1410 Output_section* ,
1411 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1412 unsigned int r_type,
2e702c99 1413 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1414 {
1415 // PowerPC64 .opd is not folded, so any identical function text
1416 // may be folded and we'll still keep function addresses distinct.
1417 // That means no reloc is of concern here.
1418 if (size == 64)
f6971787
AM
1419 {
1420 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1421 <Powerpc_relobj<size, big_endian>*>(relobj);
1422 if (ppcobj->abiversion() == 1)
1423 return false;
1424 }
1425 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155 1426 // function code might be taking the address of the function.
32f59844 1427 return !is_branch_reloc<size>(r_type);
4d9aa155 1428 }
21bb3914
ST
1429
1430 inline bool
1431 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1432 Target_powerpc* ,
f6971787 1433 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1434 unsigned int ,
1435 Output_section* ,
4d9aa155
AM
1436 const elfcpp::Rela<size, big_endian>& ,
1437 unsigned int r_type,
1438 Symbol*)
1439 {
1440 // As above.
1441 if (size == 64)
f6971787
AM
1442 {
1443 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1444 <Powerpc_relobj<size, big_endian>*>(relobj);
1445 if (ppcobj->abiversion() == 1)
1446 return false;
1447 }
32f59844 1448 return !is_branch_reloc<size>(r_type);
4d9aa155 1449 }
21bb3914 1450
b3ccdeb5 1451 static bool
9055360d
AM
1452 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1453 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1454 unsigned int r_type, bool report_err);
1455
42cacb20
DE
1456 private:
1457 static void
6fa2a40b 1458 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1459 unsigned int r_type);
1460
1461 static void
6fa2a40b 1462 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1463 unsigned int r_type, Symbol*);
1464
1465 static void
1466 generate_tls_call(Symbol_table* symtab, Layout* layout,
1467 Target_powerpc* target);
1468
1469 void
1470 check_non_pic(Relobj*, unsigned int r_type);
1471
1472 // Whether we have issued an error about a non-PIC compilation.
1473 bool issued_non_pic_error_;
1474 };
1475
1476 // The class which implements relocation.
e3deeb9c 1477 class Relocate : protected Track_tls
42cacb20
DE
1478 {
1479 public:
dd93cd0a
AM
1480 // Use 'at' branch hints when true, 'y' when false.
1481 // FIXME maybe: set this with an option.
1482 static const bool is_isa_v2 = true;
1483
dd93cd0a 1484 Relocate()
e3deeb9c 1485 : Track_tls()
dd93cd0a
AM
1486 { }
1487
42cacb20
DE
1488 // Do a relocation. Return false if the caller should not issue
1489 // any warnings about this relocation.
1490 inline bool
91a65d2f
AM
1491 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1492 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1493 const Sized_symbol<size>*, const Symbol_value<size>*,
1494 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1495 section_size_type);
42cacb20
DE
1496 };
1497
168a4726
AM
1498 class Relocate_comdat_behavior
1499 {
1500 public:
1501 // Decide what the linker should do for relocations that refer to
1502 // discarded comdat sections.
1503 inline Comdat_behavior
1504 get(const char* name)
1505 {
1506 gold::Default_comdat_behavior default_behavior;
1507 Comdat_behavior ret = default_behavior.get(name);
43193fe9 1508 if (ret == CB_ERROR)
168a4726
AM
1509 {
1510 if (size == 32
1511 && (strcmp(name, ".fixup") == 0
1512 || strcmp(name, ".got2") == 0))
1513 ret = CB_IGNORE;
1514 if (size == 64
1515 && (strcmp(name, ".opd") == 0
1516 || strcmp(name, ".toc") == 0
1517 || strcmp(name, ".toc1") == 0))
1518 ret = CB_IGNORE;
1519 }
1520 return ret;
1521 }
1522 };
1523
dd93cd0a
AM
1524 // Optimize the TLS relocation type based on what we know about the
1525 // symbol. IS_FINAL is true if the final address of this symbol is
1526 // known at link time.
1527
1528 tls::Tls_optimization
1529 optimize_tls_gd(bool is_final)
1530 {
1531 // If we are generating a shared library, then we can't do anything
1532 // in the linker.
aacb3b6d
AM
1533 if (parameters->options().shared()
1534 || !parameters->options().tls_optimize())
dd93cd0a
AM
1535 return tls::TLSOPT_NONE;
1536
1537 if (!is_final)
1538 return tls::TLSOPT_TO_IE;
1539 return tls::TLSOPT_TO_LE;
1540 }
1541
1542 tls::Tls_optimization
1543 optimize_tls_ld()
1544 {
aacb3b6d
AM
1545 if (parameters->options().shared()
1546 || !parameters->options().tls_optimize())
dd93cd0a
AM
1547 return tls::TLSOPT_NONE;
1548
1549 return tls::TLSOPT_TO_LE;
1550 }
1551
1552 tls::Tls_optimization
1553 optimize_tls_ie(bool is_final)
1554 {
aacb3b6d
AM
1555 if (!is_final
1556 || parameters->options().shared()
1557 || !parameters->options().tls_optimize())
dd93cd0a
AM
1558 return tls::TLSOPT_NONE;
1559
1560 return tls::TLSOPT_TO_LE;
1561 }
cf43a2fe 1562
cf43a2fe
AM
1563 // Create glink.
1564 void
1565 make_glink_section(Layout*);
42cacb20 1566
cf43a2fe
AM
1567 // Create the PLT section.
1568 void
40b469d7 1569 make_plt_section(Symbol_table*, Layout*);
42cacb20 1570
e5d5f5ed 1571 void
40b469d7 1572 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1573
2d7ad24e 1574 void
f21dbd7c 1575 make_lplt_section(Symbol_table*, Layout*);
2d7ad24e 1576
ec661b9d
AM
1577 void
1578 make_brlt_section(Layout*);
1579
42cacb20
DE
1580 // Create a PLT entry for a global symbol.
1581 void
ec661b9d 1582 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1583
1584 // Create a PLT entry for a local IFUNC symbol.
1585 void
40b469d7 1586 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1587 Sized_relobj_file<size, big_endian>*,
1588 unsigned int);
1589
2d7ad24e
AM
1590 // Create a PLT entry for a local non-IFUNC symbol.
1591 void
f21dbd7c 1592 make_local_plt_entry(Symbol_table*, Layout*,
2d7ad24e
AM
1593 Sized_relobj_file<size, big_endian>*,
1594 unsigned int);
1595
f21dbd7c
AM
1596 void
1597 make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
42cacb20 1598
dd93cd0a
AM
1599 // Create a GOT entry for local dynamic __tls_get_addr.
1600 unsigned int
1601 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1602 Sized_relobj_file<size, big_endian>* object);
1603
42cacb20 1604 unsigned int
dd93cd0a
AM
1605 tlsld_got_offset() const
1606 {
1607 return this->tlsld_got_offset_;
1608 }
42cacb20 1609
42cacb20
DE
1610 // Get the dynamic reloc section, creating it if necessary.
1611 Reloc_section*
1612 rela_dyn_section(Layout*);
1613
b3ccdeb5
AM
1614 // Similarly, but for ifunc symbols get the one for ifunc.
1615 Reloc_section*
1616 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1617
42cacb20
DE
1618 // Copy a relocation against a global symbol.
1619 void
ef9beddf 1620 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1621 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1622 unsigned int shndx, Output_section* output_section,
1623 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1624 {
859d7987 1625 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1626 this->copy_relocs_.copy_reloc(symtab, layout,
1627 symtab->get_sized_symbol<size>(sym),
1628 object, shndx, output_section,
859d7987
CC
1629 r_type, reloc.get_r_offset(),
1630 reloc.get_r_addend(),
1631 this->rela_dyn_section(layout));
42cacb20
DE
1632 }
1633
0cfdc767 1634 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1635 void
a3e60ddb 1636 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1637
1638 // Sort output sections by address.
1639 struct Sort_sections
1640 {
1641 bool
1642 operator()(const Output_section* sec1, const Output_section* sec2)
1643 { return sec1->address() < sec2->address(); }
1644 };
1645
1646 class Branch_info
1647 {
1648 public:
1649 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1650 unsigned int data_shndx,
1651 Address r_offset,
1652 unsigned int r_type,
1653 unsigned int r_sym,
1654 Address addend)
1655 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1656 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1657 { }
1658
1659 ~Branch_info()
1660 { }
1661
7e57d19e
AM
1662 // Return whether this branch is going via a plt call stub, and if
1663 // so, mark it as having an R_PPC64_TOCSAVE.
1664 bool
1665 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1666 unsigned int shndx, Address offset,
1667 Target_powerpc* target, Symbol_table* symtab);
1668
ec661b9d 1669 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1670 bool
ec661b9d
AM
1671 make_stub(Stub_table<size, big_endian>*,
1672 Stub_table<size, big_endian>*,
1673 Symbol_table*) const;
1674
1675 private:
1676 // The branch location..
1677 Powerpc_relobj<size, big_endian>* object_;
1678 unsigned int shndx_;
1679 Address offset_;
1680 // ..and the branch type and destination.
7e57d19e
AM
1681 unsigned int r_type_ : 31;
1682 unsigned int tocsave_ : 1;
ec661b9d
AM
1683 unsigned int r_sym_;
1684 Address addend_;
1685 };
1686
42cacb20
DE
1687 // Information about this specific target which we pass to the
1688 // general Target structure.
1689 static Target::Target_info powerpc_info;
1690
f19c3684
AM
1691 // The small GOT section used by ppc32, and by ppc64 for entries that
1692 // must be addresseed +/-32k from the got pointer.
cf43a2fe 1693 Output_data_got_powerpc<size, big_endian>* got_;
f19c3684
AM
1694 // Another GOT section used for entries that can be addressed +/- 2G
1695 // from the got pointer.
1696 Output_data_got_powerpc<size, big_endian>* biggot_;
1697
b3ccdeb5
AM
1698 // The PLT section. This is a container for a table of addresses,
1699 // and their relocations. Each address in the PLT has a dynamic
1700 // relocation (R_*_JMP_SLOT) and each address will have a
1701 // corresponding entry in .glink for lazy resolution of the PLT.
1702 // ppc32 initialises the PLT to point at the .glink entry, while
1703 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1704 // linker adds a stub that loads the PLT entry into ctr then
1705 // branches to ctr. There may be more than one stub for each PLT
1706 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1707 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1708 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1709 // The IPLT section. Like plt_, this is a container for a table of
1710 // addresses and their relocations, specifically for STT_GNU_IFUNC
1711 // functions that resolve locally (STT_GNU_IFUNC functions that
1712 // don't resolve locally go in PLT). Unlike plt_, these have no
1713 // entry in .glink for lazy resolution, and the relocation section
1714 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1715 // the relocation section may contain relocations against
1716 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1717 // relocation section will appear at the end of other dynamic
1718 // relocations, so that ld.so applies these relocations after other
1719 // dynamic relocations. In a static executable, the relocation
1720 // section is emitted and marked with __rela_iplt_start and
1721 // __rela_iplt_end symbols.
e5d5f5ed 1722 Output_data_plt_powerpc<size, big_endian>* iplt_;
2d7ad24e
AM
1723 // A PLT style section for local, non-ifunc symbols
1724 Output_data_plt_powerpc<size, big_endian>* lplt_;
ec661b9d
AM
1725 // Section holding long branch destinations.
1726 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1727 // The .glink section.
cf43a2fe 1728 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1729 // The dynamic reloc section.
42cacb20
DE
1730 Reloc_section* rela_dyn_;
1731 // Relocs saved to avoid a COPY reloc.
5edad15d 1732 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1733 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1734 unsigned int tlsld_got_offset_;
ec661b9d
AM
1735
1736 Stub_tables stub_tables_;
1737 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1738 Branch_lookup_table branch_lookup_table_;
1739
1740 typedef std::vector<Branch_info> Branches;
1741 Branches branch_info_;
7e57d19e 1742 Tocsave_loc tocsave_loc_;
9e69ed50 1743
63e5eea2 1744 bool power10_relocs_;
9e69ed50 1745 bool plt_thread_safe_;
7ee7ff70
AM
1746 bool plt_localentry0_;
1747 bool plt_localentry0_init_;
1748 bool has_localentry0_;
34e0882b 1749 bool has_tls_get_addr_opt_;
4e0e019f 1750 bool no_tprel_opt_;
a3e60ddb
AM
1751
1752 bool relax_failed_;
1753 int relax_fail_count_;
1754 int32_t stub_group_size_;
d49044c7
AM
1755
1756 Output_data_save_res<size, big_endian> *savres_section_;
34e0882b
AM
1757
1758 // The "__tls_get_addr" symbol, if present
1759 Symbol* tls_get_addr_;
1760 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1761 Symbol* tls_get_addr_opt_;
724436fc
AM
1762
1763 // Attributes in output.
1764 Attributes_section_data* attributes_section_data_;
1765
1766 // Last input file to change various attribute tags
1767 const char* last_fp_;
1768 const char* last_ld_;
1769 const char* last_vec_;
1770 const char* last_struct_;
42cacb20
DE
1771};
1772
1773template<>
1774Target::Target_info Target_powerpc<32, true>::powerpc_info =
1775{
1776 32, // size
1777 true, // is_big_endian
1778 elfcpp::EM_PPC, // machine_code
1779 false, // has_make_symbol
1780 false, // has_resolve
1781 false, // has_code_fill
1782 true, // is_default_stack_executable
b3ce541e 1783 false, // can_icf_inline_merge_sections
42cacb20
DE
1784 '\0', // wrap_char
1785 "/usr/lib/ld.so.1", // dynamic_linker
1786 0x10000000, // default_text_segment_address
1787 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1788 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1789 false, // isolate_execinstr
1790 0, // rosegment_gap
8a5e3e08
ILT
1791 elfcpp::SHN_UNDEF, // small_common_shndx
1792 elfcpp::SHN_UNDEF, // large_common_shndx
1793 0, // small_common_section_flags
05a352e6
DK
1794 0, // large_common_section_flags
1795 NULL, // attributes_section
a67858e0 1796 NULL, // attributes_vendor
8d9743bd
MK
1797 "_start", // entry_symbol_name
1798 32, // hash_entry_size
bce5a025 1799 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1800};
1801
1802template<>
1803Target::Target_info Target_powerpc<32, false>::powerpc_info =
1804{
1805 32, // size
1806 false, // is_big_endian
1807 elfcpp::EM_PPC, // machine_code
1808 false, // has_make_symbol
1809 false, // has_resolve
1810 false, // has_code_fill
1811 true, // is_default_stack_executable
b3ce541e 1812 false, // can_icf_inline_merge_sections
42cacb20
DE
1813 '\0', // wrap_char
1814 "/usr/lib/ld.so.1", // dynamic_linker
1815 0x10000000, // default_text_segment_address
1816 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1817 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1818 false, // isolate_execinstr
1819 0, // rosegment_gap
8a5e3e08
ILT
1820 elfcpp::SHN_UNDEF, // small_common_shndx
1821 elfcpp::SHN_UNDEF, // large_common_shndx
1822 0, // small_common_section_flags
05a352e6
DK
1823 0, // large_common_section_flags
1824 NULL, // attributes_section
a67858e0 1825 NULL, // attributes_vendor
8d9743bd
MK
1826 "_start", // entry_symbol_name
1827 32, // hash_entry_size
bce5a025 1828 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1829};
1830
1831template<>
1832Target::Target_info Target_powerpc<64, true>::powerpc_info =
1833{
1834 64, // size
1835 true, // is_big_endian
1836 elfcpp::EM_PPC64, // machine_code
1837 false, // has_make_symbol
565ed01a 1838 true, // has_resolve
42cacb20 1839 false, // has_code_fill
ec769010 1840 false, // is_default_stack_executable
b3ce541e 1841 false, // can_icf_inline_merge_sections
42cacb20
DE
1842 '\0', // wrap_char
1843 "/usr/lib/ld.so.1", // dynamic_linker
1844 0x10000000, // default_text_segment_address
1845 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1846 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1847 false, // isolate_execinstr
1848 0, // rosegment_gap
8a5e3e08
ILT
1849 elfcpp::SHN_UNDEF, // small_common_shndx
1850 elfcpp::SHN_UNDEF, // large_common_shndx
1851 0, // small_common_section_flags
05a352e6
DK
1852 0, // large_common_section_flags
1853 NULL, // attributes_section
a67858e0 1854 NULL, // attributes_vendor
8d9743bd
MK
1855 "_start", // entry_symbol_name
1856 32, // hash_entry_size
bce5a025 1857 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1858};
1859
1860template<>
1861Target::Target_info Target_powerpc<64, false>::powerpc_info =
1862{
1863 64, // size
1864 false, // is_big_endian
1865 elfcpp::EM_PPC64, // machine_code
1866 false, // has_make_symbol
565ed01a 1867 true, // has_resolve
42cacb20 1868 false, // has_code_fill
ec769010 1869 false, // is_default_stack_executable
b3ce541e 1870 false, // can_icf_inline_merge_sections
42cacb20
DE
1871 '\0', // wrap_char
1872 "/usr/lib/ld.so.1", // dynamic_linker
1873 0x10000000, // default_text_segment_address
1874 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1875 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1876 false, // isolate_execinstr
1877 0, // rosegment_gap
8a5e3e08
ILT
1878 elfcpp::SHN_UNDEF, // small_common_shndx
1879 elfcpp::SHN_UNDEF, // large_common_shndx
1880 0, // small_common_section_flags
05a352e6
DK
1881 0, // large_common_section_flags
1882 NULL, // attributes_section
a67858e0 1883 NULL, // attributes_vendor
8d9743bd
MK
1884 "_start", // entry_symbol_name
1885 32, // hash_entry_size
bce5a025 1886 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1887};
1888
32f59844 1889template<int size>
dd93cd0a
AM
1890inline bool
1891is_branch_reloc(unsigned int r_type)
1892{
1893 return (r_type == elfcpp::R_POWERPC_REL24
32f59844 1894 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 1895 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
dd93cd0a
AM
1896 || r_type == elfcpp::R_PPC_PLTREL24
1897 || r_type == elfcpp::R_PPC_LOCAL24PC
1898 || r_type == elfcpp::R_POWERPC_REL14
1899 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1900 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1901 || r_type == elfcpp::R_POWERPC_ADDR24
1902 || r_type == elfcpp::R_POWERPC_ADDR14
1903 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1904 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1905}
1906
08be3224
AM
1907// Reloc resolves to plt entry.
1908template<int size>
1909inline bool
1910is_plt16_reloc(unsigned int r_type)
1911{
1912 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1913 || r_type == elfcpp::R_POWERPC_PLT16_HI
1914 || r_type == elfcpp::R_POWERPC_PLT16_HA
1915 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1916}
1917
f19c3684 1918// GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
4290b0ab
AM
1919inline bool
1920is_got_reloc(unsigned int r_type)
1921{
1922 return (r_type == elfcpp::R_POWERPC_GOT16
1923 || r_type == elfcpp::R_POWERPC_GOT16_LO
1924 || r_type == elfcpp::R_POWERPC_GOT16_HI
1925 || r_type == elfcpp::R_POWERPC_GOT16_HA
1926 || r_type == elfcpp::R_PPC64_GOT16_DS
1927 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1928 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1929}
1930
dd93cd0a
AM
1931// If INSN is an opcode that may be used with an @tls operand, return
1932// the transformed insn for TLS optimisation, otherwise return 0. If
1933// REG is non-zero only match an insn with RB or RA equal to REG.
1934uint32_t
1935at_tls_transform(uint32_t insn, unsigned int reg)
1936{
1937 if ((insn & (0x3f << 26)) != 31 << 26)
1938 return 0;
1939
1940 unsigned int rtra;
1941 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1942 rtra = insn & ((1 << 26) - (1 << 16));
1943 else if (((insn >> 16) & 0x1f) == reg)
1944 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1945 else
1946 return 0;
1947
1948 if ((insn & (0x3ff << 1)) == 266 << 1)
1949 // add -> addi
1950 insn = 14 << 26;
1951 else if ((insn & (0x1f << 1)) == 23 << 1
1952 && ((insn & (0x1f << 6)) < 14 << 6
1953 || ((insn & (0x1f << 6)) >= 16 << 6
1954 && (insn & (0x1f << 6)) < 24 << 6)))
1955 // load and store indexed -> dform
1956 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1957 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1958 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1959 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1960 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1961 // lwax -> lwa
1962 insn = (58 << 26) | 2;
1963 else
1964 return 0;
1965 insn |= rtra;
1966 return insn;
1967}
1968
dd93cd0a 1969
42cacb20
DE
1970template<int size, bool big_endian>
1971class Powerpc_relocate_functions
1972{
dd93cd0a 1973public:
f4baf0d4 1974 enum Overflow_check
dd93cd0a 1975 {
f4baf0d4
AM
1976 CHECK_NONE,
1977 CHECK_SIGNED,
b80eed39
AM
1978 CHECK_UNSIGNED,
1979 CHECK_BITFIELD,
1980 CHECK_LOW_INSN,
1981 CHECK_HIGH_INSN
dd93cd0a
AM
1982 };
1983
f4baf0d4 1984 enum Status
dd93cd0a 1985 {
f4baf0d4
AM
1986 STATUS_OK,
1987 STATUS_OVERFLOW
1988 };
dd93cd0a 1989
42cacb20 1990private:
c9269dff 1991 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1992 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1993 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1994
dd93cd0a
AM
1995 template<int valsize>
1996 static inline bool
1997 has_overflow_signed(Address value)
1998 {
1999 // limit = 1 << (valsize - 1) without shift count exceeding size of type
2000 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2001 limit <<= ((valsize - 1) >> 1);
2002 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2003 return value + limit > (limit << 1) - 1;
2004 }
2005
2006 template<int valsize>
2007 static inline bool
b80eed39 2008 has_overflow_unsigned(Address value)
dd93cd0a
AM
2009 {
2010 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2011 limit <<= ((valsize - 1) >> 1);
2012 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
2013 return value > (limit << 1) - 1;
2014 }
2015
2016 template<int valsize>
2017 static inline bool
2018 has_overflow_bitfield(Address value)
2019 {
2020 return (has_overflow_unsigned<valsize>(value)
2021 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
2022 }
2023
2024 template<int valsize>
f4baf0d4
AM
2025 static inline Status
2026 overflowed(Address value, Overflow_check overflow)
dd93cd0a 2027 {
f4baf0d4 2028 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
2029 {
2030 if (has_overflow_signed<valsize>(value))
f4baf0d4 2031 return STATUS_OVERFLOW;
dd93cd0a 2032 }
b80eed39
AM
2033 else if (overflow == CHECK_UNSIGNED)
2034 {
2035 if (has_overflow_unsigned<valsize>(value))
2036 return STATUS_OVERFLOW;
2037 }
f4baf0d4 2038 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
2039 {
2040 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 2041 return STATUS_OVERFLOW;
dd93cd0a 2042 }
f4baf0d4 2043 return STATUS_OK;
dd93cd0a
AM
2044 }
2045
cf43a2fe 2046 // Do a simple RELA relocation
0cfb0717 2047 template<int fieldsize, int valsize>
f4baf0d4
AM
2048 static inline Status
2049 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2050 {
0cfb0717 2051 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 2052 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2053 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
2054 return overflowed<valsize>(value, overflow);
2055 }
2056
0cfb0717 2057 template<int fieldsize, int valsize>
f4baf0d4 2058 static inline Status
42cacb20
DE
2059 rela(unsigned char* view,
2060 unsigned int right_shift,
0cfb0717 2061 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2062 Address value,
f4baf0d4 2063 Overflow_check overflow)
42cacb20 2064 {
0cfb0717 2065 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 2066 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2067 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
6a010cf6
AM
2068 if (overflow == CHECK_SIGNED)
2069 value = static_cast<SignedAddress>(value) >> right_shift;
2070 else
2071 value = value >> right_shift;
2072 Valtype reloc = value;
42cacb20
DE
2073 val &= ~dst_mask;
2074 reloc &= dst_mask;
0cfb0717 2075 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
6a010cf6 2076 return overflowed<valsize>(value, overflow);
42cacb20
DE
2077 }
2078
cf43a2fe 2079 // Do a simple RELA relocation, unaligned.
0cfb0717 2080 template<int fieldsize, int valsize>
f4baf0d4
AM
2081 static inline Status
2082 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2083 {
0cfb0717 2084 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
2085 return overflowed<valsize>(value, overflow);
2086 }
2087
0cfb0717 2088 template<int fieldsize, int valsize>
f4baf0d4 2089 static inline Status
cf43a2fe
AM
2090 rela_ua(unsigned char* view,
2091 unsigned int right_shift,
0cfb0717 2092 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2093 Address value,
f4baf0d4 2094 Overflow_check overflow)
42cacb20 2095 {
0cfb0717 2096 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 2097 Valtype;
0cfb0717 2098 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
6a010cf6
AM
2099 if (overflow == CHECK_SIGNED)
2100 value = static_cast<SignedAddress>(value) >> right_shift;
2101 else
2102 value = value >> right_shift;
2103 Valtype reloc = value;
42cacb20
DE
2104 val &= ~dst_mask;
2105 reloc &= dst_mask;
0cfb0717 2106 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
6a010cf6 2107 return overflowed<valsize>(value, overflow);
42cacb20
DE
2108 }
2109
42cacb20 2110public:
dd93cd0a 2111 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 2112 static inline void
dd93cd0a 2113 addr64(unsigned char* view, Address value)
0cfb0717 2114 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 2115
dd93cd0a 2116 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 2117 static inline void
dd93cd0a 2118 addr64_u(unsigned char* view, Address value)
0cfb0717 2119 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
2120
2121 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
2122 static inline Status
2123 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2124 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
2125
2126 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
2127 static inline Status
2128 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2129 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
2130
2131 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
2132 static inline Status
2133 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2134 {
0cfb0717
AM
2135 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2136 value, overflow);
f4baf0d4
AM
2137 if (overflow != CHECK_NONE && (value & 3) != 0)
2138 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2139 return stat;
2140 }
42cacb20
DE
2141
2142 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
2143 static inline Status
2144 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2145 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 2146
dd93cd0a 2147 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
2148 static inline Status
2149 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2150 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 2151
dd93cd0a 2152 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2153 static inline Status
2154 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2155 {
0cfb0717 2156 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 2157 if ((value & 3) != 0)
f4baf0d4 2158 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2159 return stat;
2160 }
42cacb20 2161
a680de9a
PB
2162 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2163 static inline Status
2164 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2165 {
2166 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2167 if ((value & 15) != 0)
2168 stat = STATUS_OVERFLOW;
2169 return stat;
2170 }
2171
42cacb20
DE
2172 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2173 static inline void
dd93cd0a 2174 addr16_hi(unsigned char* view, Address value)
0cfb0717 2175 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 2176
c9269dff 2177 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 2178 static inline void
dd93cd0a
AM
2179 addr16_ha(unsigned char* view, Address value)
2180 { This::addr16_hi(view, value + 0x8000); }
42cacb20 2181
dd93cd0a 2182 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 2183 static inline void
dd93cd0a 2184 addr16_hi2(unsigned char* view, Address value)
0cfb0717 2185 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 2186
dd93cd0a 2187 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 2188 static inline void
dd93cd0a
AM
2189 addr16_ha2(unsigned char* view, Address value)
2190 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 2191
dd93cd0a 2192 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 2193 static inline void
dd93cd0a 2194 addr16_hi3(unsigned char* view, Address value)
0cfb0717 2195 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 2196
dd93cd0a 2197 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 2198 static inline void
dd93cd0a
AM
2199 addr16_ha3(unsigned char* view, Address value)
2200 { This::addr16_hi3(view, value + 0x8000); }
2201
2202 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2203 static inline Status
2204 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2205 {
0cfb0717 2206 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
2207 if (overflow != CHECK_NONE && (value & 3) != 0)
2208 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2209 return stat;
2210 }
a680de9a
PB
2211
2212 // R_POWERPC_REL16DX_HA
2213 static inline Status
2214 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2215 {
2216 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2217 Valtype* wv = reinterpret_cast<Valtype*>(view);
2218 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2219 value += 0x8000;
2220 value = static_cast<SignedAddress>(value) >> 16;
2221 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2222 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2223 return overflowed<16>(value, overflow);
2224 }
e4dff765
AM
2225
2226 // R_PPC64_D34
2227 static inline Status
2228 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2229 {
2230 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2231 value, overflow);
2232 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2233 return stat;
2234 }
2235
2236 // R_PPC64_D34_HI30
2237 static inline void
2238 addr34_hi(unsigned char *view, uint64_t value)
2239 { This::addr34(view, value >> 34, CHECK_NONE);}
2240
2241 // R_PPC64_D34_HA30
2242 static inline void
2243 addr34_ha(unsigned char *view, uint64_t value)
2244 { This::addr34_hi(view, value + (1ULL << 33));}
2245
2246 // R_PPC64_D28
2247 static inline Status
2248 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2249 {
2250 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2251 value, overflow);
2252 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2253 return stat;
2254 }
2255
2256 // R_PPC64_ADDR16_HIGHER34
2257 static inline void
2258 addr16_higher34(unsigned char* view, uint64_t value)
2259 { This::addr16(view, value >> 34, CHECK_NONE); }
2260
2261 // R_PPC64_ADDR16_HIGHERA34
2262 static inline void
2263 addr16_highera34(unsigned char* view, uint64_t value)
2264 { This::addr16_higher34(view, value + (1ULL << 33)); }
2265
2266 // R_PPC64_ADDR16_HIGHEST34
2267 static inline void
2268 addr16_highest34(unsigned char* view, uint64_t value)
2269 { This::addr16(view, value >> 50, CHECK_NONE); }
2270
2271 // R_PPC64_ADDR16_HIGHESTA34
2272 static inline void
2273 addr16_highesta34(unsigned char* view, uint64_t value)
2274 { This::addr16_highest34(view, value + (1ULL << 33)); }
cf43a2fe
AM
2275};
2276
b4f7960d
AM
2277// Set ABI version for input and output.
2278
2279template<int size, bool big_endian>
2280void
2281Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2282{
2283 this->e_flags_ |= ver;
2284 if (this->abiversion() != 0)
2285 {
2286 Target_powerpc<size, big_endian>* target =
2287 static_cast<Target_powerpc<size, big_endian>*>(
2288 parameters->sized_target<size, big_endian>());
2289 if (target->abiversion() == 0)
2290 target->set_abiversion(this->abiversion());
2291 else if (target->abiversion() != this->abiversion())
2292 gold_error(_("%s: ABI version %d is not compatible "
2293 "with ABI version %d output"),
2294 this->name().c_str(),
2295 this->abiversion(), target->abiversion());
2296
2297 }
2298}
2299
5edad15d
AM
2300// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2301// relocatable object, if such sections exists.
cf43a2fe
AM
2302
2303template<int size, bool big_endian>
2304bool
2305Powerpc_relobj<size, big_endian>::do_find_special_sections(
2306 Read_symbols_data* sd)
2307{
c9269dff
AM
2308 const unsigned char* const pshdrs = sd->section_headers->data();
2309 const unsigned char* namesu = sd->section_names->data();
2310 const char* names = reinterpret_cast<const char*>(namesu);
2311 section_size_type names_size = sd->section_names_size;
2312 const unsigned char* s;
2313
dc3714f3
AM
2314 s = this->template find_shdr<size, big_endian>(pshdrs,
2315 size == 32 ? ".got2" : ".opd",
2316 names, names_size, NULL);
c9269dff
AM
2317 if (s != NULL)
2318 {
2319 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2320 this->special_ = ndx;
b4f7960d
AM
2321 if (size == 64)
2322 {
2323 if (this->abiversion() == 0)
2324 this->set_abiversion(1);
2325 else if (this->abiversion() > 1)
2326 gold_error(_("%s: .opd invalid in abiv%d"),
2327 this->name().c_str(), this->abiversion());
2328 }
c9269dff 2329 }
5edad15d
AM
2330 if (size == 64)
2331 {
2332 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2333 names, names_size, NULL);
2334 if (s != NULL)
2335 {
2336 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2337 this->relatoc_ = ndx;
2338 typename elfcpp::Shdr<size, big_endian> shdr(s);
2339 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2340 }
2341 }
c9269dff
AM
2342 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2343}
2344
2345// Examine .rela.opd to build info about function entry points.
2346
2347template<int size, bool big_endian>
2348void
2349Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2350 size_t reloc_count,
2351 const unsigned char* prelocs,
2352 const unsigned char* plocal_syms)
2353{
2354 if (size == 64)
cf43a2fe 2355 {
0e123f69
AM
2356 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2357 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2358 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2359 Address expected_off = 0;
2360 bool regular = true;
2361 unsigned int opd_ent_size = 0;
c9269dff
AM
2362
2363 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2364 {
c9269dff
AM
2365 Reltype reloc(prelocs);
2366 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2367 = reloc.get_r_info();
2368 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2369 if (r_type == elfcpp::R_PPC64_ADDR64)
2370 {
2371 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2372 typename elfcpp::Elf_types<size>::Elf_Addr value;
2373 bool is_ordinary;
2374 unsigned int shndx;
2375 if (r_sym < this->local_symbol_count())
2376 {
2377 typename elfcpp::Sym<size, big_endian>
2378 lsym(plocal_syms + r_sym * sym_size);
2379 shndx = lsym.get_st_shndx();
2380 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2381 value = lsym.get_st_value();
2382 }
2383 else
2384 shndx = this->symbol_section_and_value(r_sym, &value,
2385 &is_ordinary);
2386 this->set_opd_ent(reloc.get_r_offset(), shndx,
2387 value + reloc.get_r_addend());
ec4dbad3
AM
2388 if (i == 2)
2389 {
2390 expected_off = reloc.get_r_offset();
2391 opd_ent_size = expected_off;
2392 }
2393 else if (expected_off != reloc.get_r_offset())
2394 regular = false;
2395 expected_off += opd_ent_size;
2396 }
2397 else if (r_type == elfcpp::R_PPC64_TOC)
2398 {
2399 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2400 regular = false;
2401 }
2402 else
2403 {
2404 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2405 this->name().c_str(), r_type);
2406 regular = false;
c9269dff
AM
2407 }
2408 }
ec4dbad3
AM
2409 if (reloc_count <= 2)
2410 opd_ent_size = this->section_size(this->opd_shndx());
2411 if (opd_ent_size != 24 && opd_ent_size != 16)
2412 regular = false;
2413 if (!regular)
2414 {
2415 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2416 this->name().c_str());
2417 opd_ent_size = 0;
2418 }
c9269dff
AM
2419 }
2420}
2421
5edad15d
AM
2422// Returns true if a code sequence loading the TOC entry at VALUE
2423// relative to the TOC pointer can be converted into code calculating
2424// a TOC pointer relative offset.
2425// If so, the TOC pointer relative offset is stored to VALUE.
2426
2427template<int size, bool big_endian>
2428bool
2429Powerpc_relobj<size, big_endian>::make_toc_relative(
2430 Target_powerpc<size, big_endian>* target,
2431 Address* value)
2432{
2433 if (size != 64)
2434 return false;
2435
e666304e
AM
2436 // With -mcmodel=medium code it is quite possible to have
2437 // toc-relative relocs referring to objects outside the TOC.
2438 // Don't try to look at a non-existent TOC.
a19da04b
AM
2439 if (this->toc_shndx() == 0
2440 || this->output_section(this->toc_shndx()) == 0)
e666304e
AM
2441 return false;
2442
5edad15d
AM
2443 // Convert VALUE back to an address by adding got_base (see below),
2444 // then to an offset in the TOC by subtracting the TOC output
a19da04b
AM
2445 // section address and the TOC output offset.
2446 Address off = (*value + target->toc_pointer()
2447 - this->output_section(this->toc_shndx())->address()
5edad15d
AM
2448 - this->output_section_offset(this->toc_shndx()));
2449 // Is this offset in the TOC? -mcmodel=medium code may be using
2450 // TOC relative access to variables outside the TOC. Those of
2451 // course can't be optimized. We also don't try to optimize code
2452 // that is using a different object's TOC.
2453 if (off >= this->section_size(this->toc_shndx()))
2454 return false;
2455
2456 if (this->no_toc_opt(off))
2457 return false;
2458
2459 section_size_type vlen;
2460 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2461 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2462 // The TOC pointer
a19da04b 2463 Address got_base = target->toc_pointer();
5edad15d 2464 addr -= got_base;
857e829e 2465 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2466 return false;
2467
2468 *value = addr;
2469 return true;
2470}
2471
c9b8abb7
AM
2472template<int size, bool big_endian>
2473bool
2474Powerpc_relobj<size, big_endian>::make_got_relative(
2475 Target_powerpc<size, big_endian>* target,
2476 const Symbol_value<size>* psymval,
2477 Address addend,
2478 Address* value)
2479{
2480 Address addr = psymval->value(this, addend);
a19da04b 2481 Address got_base = target->toc_pointer();
c9b8abb7
AM
2482 addr -= got_base;
2483 if (addr + 0x80008000 > 0xffffffff)
2484 return false;
2485
2486 *value = addr;
2487 return true;
2488}
2489
5edad15d
AM
2490// Perform the Sized_relobj_file method, then set up opd info from
2491// .opd relocs.
2492
c9269dff
AM
2493template<int size, bool big_endian>
2494void
2495Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2496{
2497 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2498 if (size == 64)
2499 {
2500 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2501 p != rd->relocs.end();
2502 ++p)
2503 {
2504 if (p->data_shndx == this->opd_shndx())
2505 {
ec4dbad3
AM
2506 uint64_t opd_size = this->section_size(this->opd_shndx());
2507 gold_assert(opd_size == static_cast<size_t>(opd_size));
2508 if (opd_size != 0)
2509 {
2510 this->init_opd(opd_size);
2511 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2512 rd->local_symbols->data());
2513 }
c9269dff
AM
2514 break;
2515 }
cf43a2fe
AM
2516 }
2517 }
cf43a2fe
AM
2518}
2519
b4f7960d
AM
2520// Read the symbols then set up st_other vector.
2521
2522template<int size, bool big_endian>
2523void
2524Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2525{
f35c4853 2526 this->base_read_symbols(sd);
724436fc
AM
2527 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2528 return;
b4f7960d
AM
2529 if (size == 64)
2530 {
2531 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2532 const unsigned char* const pshdrs = sd->section_headers->data();
2533 const unsigned int loccount = this->do_local_symbol_count();
2534 if (loccount != 0)
2535 {
2536 this->st_other_.resize(loccount);
2537 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2538 off_t locsize = loccount * sym_size;
2539 const unsigned int symtab_shndx = this->symtab_shndx();
2540 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2541 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2542 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2543 locsize, true, false);
2544 psyms += sym_size;
2545 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2546 {
2547 elfcpp::Sym<size, big_endian> sym(psyms);
2548 unsigned char st_other = sym.get_st_other();
2549 this->st_other_[i] = st_other;
2550 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2551 {
2552 if (this->abiversion() == 0)
2553 this->set_abiversion(2);
2554 else if (this->abiversion() < 2)
2555 gold_error(_("%s: local symbol %d has invalid st_other"
2556 " for ABI version 1"),
2557 this->name().c_str(), i);
2558 }
2559 }
2560 }
2561 }
724436fc
AM
2562
2563 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2564 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2565 bool merge_attributes = false;
2566 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2567 {
2568 elfcpp::Shdr<size, big_endian> shdr(ps);
2569 switch (shdr.get_sh_type())
2570 {
2571 case elfcpp::SHT_GNU_ATTRIBUTES:
2572 {
2573 gold_assert(this->attributes_section_data_ == NULL);
2574 section_offset_type section_offset = shdr.get_sh_offset();
2575 section_size_type section_size =
2576 convert_to_section_size_type(shdr.get_sh_size());
2577 const unsigned char* view =
2578 this->get_view(section_offset, section_size, true, false);
2579 this->attributes_section_data_ =
2580 new Attributes_section_data(view, section_size);
2581 }
2582 break;
2583
2584 case elfcpp::SHT_SYMTAB:
2585 {
2586 // Sometimes an object has no contents except the section
2587 // name string table and an empty symbol table with the
2588 // undefined symbol. We don't want to merge
2589 // processor-specific flags from such an object.
2590 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2591 elfcpp::Elf_sizes<size>::sym_size;
2592 if (shdr.get_sh_size() > sym_size)
2593 merge_attributes = true;
2594 }
2595 break;
2596
2597 case elfcpp::SHT_STRTAB:
2598 break;
2599
2600 default:
2601 merge_attributes = true;
2602 break;
2603 }
2604 }
2605
2606 if (!merge_attributes)
2607 {
2608 // Should rarely happen.
2609 delete this->attributes_section_data_;
2610 this->attributes_section_data_ = NULL;
2611 }
b4f7960d
AM
2612}
2613
2614template<int size, bool big_endian>
2615void
2616Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2617{
2618 this->e_flags_ |= ver;
2619 if (this->abiversion() != 0)
2620 {
2621 Target_powerpc<size, big_endian>* target =
2622 static_cast<Target_powerpc<size, big_endian>*>(
2623 parameters->sized_target<size, big_endian>());
2624 if (target->abiversion() == 0)
2625 target->set_abiversion(this->abiversion());
2626 else if (target->abiversion() != this->abiversion())
2627 gold_error(_("%s: ABI version %d is not compatible "
2628 "with ABI version %d output"),
2629 this->name().c_str(),
2630 this->abiversion(), target->abiversion());
2631
2632 }
2633}
2634
f35c4853 2635// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2636// read .opd from a dynamic object, filling in opd_ent_ vector,
2637
2638template<int size, bool big_endian>
2639void
2640Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2641{
f35c4853 2642 this->base_read_symbols(sd);
724436fc
AM
2643 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2644 const unsigned char* ps =
2645 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2646 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2647 {
2648 elfcpp::Shdr<size, big_endian> shdr(ps);
2649 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2650 {
2651 section_offset_type section_offset = shdr.get_sh_offset();
2652 section_size_type section_size =
2653 convert_to_section_size_type(shdr.get_sh_size());
2654 const unsigned char* view =
2655 this->get_view(section_offset, section_size, true, false);
2656 this->attributes_section_data_ =
2657 new Attributes_section_data(view, section_size);
2658 break;
2659 }
2660 }
dc3714f3
AM
2661 if (size == 64)
2662 {
dc3714f3
AM
2663 const unsigned char* const pshdrs = sd->section_headers->data();
2664 const unsigned char* namesu = sd->section_names->data();
2665 const char* names = reinterpret_cast<const char*>(namesu);
2666 const unsigned char* s = NULL;
2667 const unsigned char* opd;
2668 section_size_type opd_size;
2669
2670 // Find and read .opd section.
2671 while (1)
2672 {
2673 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2674 sd->section_names_size,
2675 s);
2676 if (s == NULL)
2677 return;
2678
2679 typename elfcpp::Shdr<size, big_endian> shdr(s);
2680 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2681 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2682 {
b4f7960d
AM
2683 if (this->abiversion() == 0)
2684 this->set_abiversion(1);
2685 else if (this->abiversion() > 1)
2686 gold_error(_("%s: .opd invalid in abiv%d"),
2687 this->name().c_str(), this->abiversion());
2688
dc3714f3
AM
2689 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2690 this->opd_address_ = shdr.get_sh_addr();
2691 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2692 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2693 true, false);
2694 break;
2695 }
2696 }
2697
2698 // Build set of executable sections.
2699 // Using a set is probably overkill. There is likely to be only
2700 // a few executable sections, typically .init, .text and .fini,
2701 // and they are generally grouped together.
2702 typedef std::set<Sec_info> Exec_sections;
2703 Exec_sections exec_sections;
2704 s = pshdrs;
2705 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2706 {
2707 typename elfcpp::Shdr<size, big_endian> shdr(s);
2708 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2709 && ((shdr.get_sh_flags()
2710 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2711 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2712 && shdr.get_sh_size() != 0)
2713 {
2714 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2715 shdr.get_sh_size(), i));
2716 }
2717 }
2718 if (exec_sections.empty())
2719 return;
2720
2721 // Look over the OPD entries. This is complicated by the fact
2722 // that some binaries will use two-word entries while others
2723 // will use the standard three-word entries. In most cases
2724 // the third word (the environment pointer for languages like
2725 // Pascal) is unused and will be zero. If the third word is
2726 // used it should not be pointing into executable sections,
2727 // I think.
2728 this->init_opd(opd_size);
2729 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2730 {
2731 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2732 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2733 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2734 if (val == 0)
2735 // Chances are that this is the third word of an OPD entry.
2736 continue;
2737 typename Exec_sections::const_iterator e
2738 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2739 if (e != exec_sections.begin())
2740 {
2741 --e;
2742 if (e->start <= val && val < e->start + e->len)
2743 {
2744 // We have an address in an executable section.
2745 // VAL ought to be the function entry, set it up.
2746 this->set_opd_ent(p - opd, e->shndx, val);
2747 // Skip second word of OPD entry, the TOC pointer.
2748 p += 8;
2749 }
2750 }
2751 // If we didn't match any executable sections, we likely
2752 // have a non-zero third word in the OPD entry.
2753 }
2754 }
2755}
2756
5edad15d
AM
2757// Relocate sections.
2758
2759template<int size, bool big_endian>
2760void
2761Powerpc_relobj<size, big_endian>::do_relocate_sections(
2762 const Symbol_table* symtab, const Layout* layout,
2763 const unsigned char* pshdrs, Output_file* of,
2764 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2765{
2766 unsigned int start = 1;
2767 if (size == 64
2768 && this->relatoc_ != 0
2769 && !parameters->options().relocatable())
2770 {
2771 // Relocate .toc first.
2772 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2773 this->relatoc_, this->relatoc_);
2774 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2775 1, this->relatoc_ - 1);
2776 start = this->relatoc_ + 1;
2777 }
2778 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2779 start, this->shnum() - 1);
2780}
2781
f43ba157 2782// Set up some symbols.
26a4e9cb
AM
2783
2784template<int size, bool big_endian>
2785void
f43ba157
AM
2786Target_powerpc<size, big_endian>::do_define_standard_symbols(
2787 Symbol_table* symtab,
2788 Layout* layout)
26a4e9cb
AM
2789{
2790 if (size == 32)
2791 {
bb66a627
AM
2792 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2793 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2794 // non-relative dynamic relocs). The proper value will be
2795 // updated later.
2796 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2797 if (gotsym != NULL && gotsym->is_undefined())
2798 {
2799 Target_powerpc<size, big_endian>* target =
2800 static_cast<Target_powerpc<size, big_endian>*>(
2801 parameters->sized_target<size, big_endian>());
2802 Output_data_got_powerpc<size, big_endian>* got
f19c3684 2803 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
26a4e9cb
AM
2804 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2805 Symbol_table::PREDEFINED,
2806 got, 0, 0,
2807 elfcpp::STT_OBJECT,
bb66a627 2808 elfcpp::STB_LOCAL,
26a4e9cb
AM
2809 elfcpp::STV_HIDDEN, 0,
2810 false, false);
2811 }
2812
2813 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2814 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2815 if (sdasym != NULL && sdasym->is_undefined())
2816 {
2817 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2818 Output_section* os
2819 = layout->add_output_section_data(".sdata", 0,
2820 elfcpp::SHF_ALLOC
2821 | elfcpp::SHF_WRITE,
2822 sdata, ORDER_SMALL_DATA, false);
2823 symtab->define_in_output_data("_SDA_BASE_", NULL,
2824 Symbol_table::PREDEFINED,
2825 os, 32768, 0, elfcpp::STT_OBJECT,
2826 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2827 0, false, false);
2828 }
2829 }
b4f7960d
AM
2830 else
2831 {
2832 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2833 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2834 if (gotsym != NULL && gotsym->is_undefined())
2835 {
2836 Target_powerpc<size, big_endian>* target =
2837 static_cast<Target_powerpc<size, big_endian>*>(
2838 parameters->sized_target<size, big_endian>());
2839 Output_data_got_powerpc<size, big_endian>* got
f19c3684 2840 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
b4f7960d
AM
2841 symtab->define_in_output_data(".TOC.", NULL,
2842 Symbol_table::PREDEFINED,
2843 got, 0x8000, 0,
2844 elfcpp::STT_OBJECT,
2845 elfcpp::STB_LOCAL,
2846 elfcpp::STV_HIDDEN, 0,
2847 false, false);
2848 }
2849 }
34e0882b
AM
2850
2851 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2852 if (parameters->options().tls_get_addr_optimize()
2853 && this->tls_get_addr_ != NULL
2854 && this->tls_get_addr_->in_reg())
2855 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2856 if (this->tls_get_addr_opt_ != NULL)
2857 {
2858 if (this->tls_get_addr_->is_undefined()
2859 || this->tls_get_addr_->is_from_dynobj())
2860 {
2861 // Make it seem as if references to __tls_get_addr are
2862 // really to __tls_get_addr_opt, so the latter symbol is
2863 // made dynamic, not the former.
2864 this->tls_get_addr_->clear_in_reg();
2865 this->tls_get_addr_opt_->set_in_reg();
2866 }
2867 // We have a non-dynamic definition for __tls_get_addr.
2868 // Make __tls_get_addr_opt the same, if it does not already have
2869 // a non-dynamic definition.
2870 else if (this->tls_get_addr_opt_->is_undefined()
2871 || this->tls_get_addr_opt_->is_from_dynobj())
2872 {
2873 Sized_symbol<size>* from
2874 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2875 Sized_symbol<size>* to
2876 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2877 symtab->clone<size>(to, from);
2878 }
2879 }
26a4e9cb
AM
2880}
2881
cf43a2fe
AM
2882// Set up PowerPC target specific relobj.
2883
2884template<int size, bool big_endian>
2885Object*
2886Target_powerpc<size, big_endian>::do_make_elf_object(
2887 const std::string& name,
2888 Input_file* input_file,
2889 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2890{
2891 int et = ehdr.get_e_type();
957564c9
AS
2892 // ET_EXEC files are valid input for --just-symbols/-R,
2893 // and we treat them as relocatable objects.
2894 if (et == elfcpp::ET_REL
2895 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2896 {
2897 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2898 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2899 obj->setup();
2900 return obj;
2901 }
2902 else if (et == elfcpp::ET_DYN)
2903 {
dc3714f3
AM
2904 Powerpc_dynobj<size, big_endian>* obj =
2905 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2906 obj->setup();
2907 return obj;
2908 }
2909 else
2910 {
c9269dff 2911 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2912 return NULL;
2913 }
2914}
2915
2916template<int size, bool big_endian>
2917class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2918{
2919public:
2920 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2921 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2922
f19c3684
AM
2923 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2924 Got_type got_type)
cf43a2fe
AM
2925 : Output_data_got<size, big_endian>(),
2926 symtab_(symtab), layout_(layout),
2927 header_ent_cnt_(size == 32 ? 3 : 1),
a19da04b 2928 header_index_(size == 32 ? 0x2000 : -1u)
751e4d66
AM
2929 {
2930 if (size == 64)
2931 this->set_addralign(256);
f19c3684 2932 if (size == 64 && (got_type & GOT_TYPE_SMALL))
a19da04b 2933 this->make_header();
751e4d66 2934 }
cf43a2fe 2935
e84fe78f
AM
2936 // Override all the Output_data_got methods we use so as to first call
2937 // reserve_ent().
2938 bool
5cdb4f14 2939 add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
e84fe78f
AM
2940 {
2941 this->reserve_ent();
5cdb4f14
AM
2942 return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2943 addend);
e84fe78f
AM
2944 }
2945
2946 bool
5cdb4f14 2947 add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
e84fe78f
AM
2948 {
2949 this->reserve_ent();
5cdb4f14
AM
2950 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2951 addend);
e84fe78f
AM
2952 }
2953
2954 bool
5cdb4f14
AM
2955 add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2956 { return this->add_global_plt(gsym, got_type, addend); }
e84fe78f
AM
2957
2958 void
2959 add_global_with_rel(Symbol* gsym, unsigned int got_type,
5cdb4f14
AM
2960 Output_data_reloc_generic* rel_dyn,
2961 unsigned int r_type, uint64_t addend)
e84fe78f
AM
2962 {
2963 this->reserve_ent();
2964 Output_data_got<size, big_endian>::
5cdb4f14 2965 add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
e84fe78f
AM
2966 }
2967
2968 void
2969 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2970 Output_data_reloc_generic* rel_dyn,
5cdb4f14
AM
2971 unsigned int r_type_1, unsigned int r_type_2,
2972 uint64_t addend)
e84fe78f 2973 {
aacb3b6d
AM
2974 if (gsym->has_got_offset(got_type))
2975 return;
2976
e84fe78f
AM
2977 this->reserve_ent(2);
2978 Output_data_got<size, big_endian>::
5cdb4f14
AM
2979 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
2980 addend);
e84fe78f
AM
2981 }
2982
2983 bool
5cdb4f14
AM
2984 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
2985 uint64_t addend)
e84fe78f
AM
2986 {
2987 this->reserve_ent();
2988 return Output_data_got<size, big_endian>::add_local(object, sym_index,
5cdb4f14 2989 got_type, addend);
e84fe78f
AM
2990 }
2991
2992 bool
5cdb4f14
AM
2993 add_local_plt(Relobj* object, unsigned int sym_index,
2994 unsigned int got_type, uint64_t addend)
e84fe78f
AM
2995 {
2996 this->reserve_ent();
2997 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
5cdb4f14 2998 got_type, addend);
e84fe78f
AM
2999 }
3000
3001 bool
5cdb4f14
AM
3002 add_local_tls(Relobj* object, unsigned int sym_index,
3003 unsigned int got_type, uint64_t addend)
3004 { return this->add_local_plt(object, sym_index, got_type, addend); }
e84fe78f
AM
3005
3006 void
3007 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3008 unsigned int got_type,
3009 Output_data_reloc_generic* rel_dyn,
5cdb4f14 3010 unsigned int r_type, uint64_t addend)
e84fe78f 3011 {
5cdb4f14 3012 if (object->local_has_got_offset(sym_index, got_type, addend))
aacb3b6d
AM
3013 return;
3014
e84fe78f
AM
3015 this->reserve_ent(2);
3016 Output_data_got<size, big_endian>::
5cdb4f14 3017 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
e84fe78f
AM
3018 }
3019
3020 unsigned int
3021 add_constant(Valtype constant)
3022 {
3023 this->reserve_ent();
3024 return Output_data_got<size, big_endian>::add_constant(constant);
3025 }
3026
dd93cd0a
AM
3027 unsigned int
3028 add_constant_pair(Valtype c1, Valtype c2)
3029 {
3030 this->reserve_ent(2);
e84fe78f 3031 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
3032 }
3033
a19da04b 3034 // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
cf43a2fe
AM
3035 unsigned int
3036 g_o_t() const
dd93cd0a
AM
3037 {
3038 if (size == 32)
a19da04b 3039 return this->got_offset(this->header_index_);
f19c3684 3040 else if (this->header_index_ != -1u)
a19da04b 3041 return this->got_offset(this->header_index_) + 0x8000;
f19c3684
AM
3042 else
3043 gold_unreachable();
dd93cd0a
AM
3044 }
3045
cf43a2fe
AM
3046 // Ensure our GOT has a header.
3047 void
3048 set_final_data_size()
3049 {
a19da04b 3050 if (size == 32 && this->header_ent_cnt_ != 0)
cf43a2fe
AM
3051 this->make_header();
3052 Output_data_got<size, big_endian>::set_final_data_size();
3053 }
3054
3055 // First word of GOT header needs some values that are not
3056 // handled by Output_data_got so poke them in here.
3057 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3058 void
3059 do_write(Output_file* of)
3060 {
f19c3684
AM
3061 if (this->header_index_ != -1u)
3062 {
3063 Valtype val = 0;
3064 if (size == 32 && this->layout_->dynamic_data() != NULL)
3065 val = this->layout_->dynamic_section()->address();
3066 if (size == 64)
3067 val = this->address() + this->g_o_t();
3068 this->replace_constant(this->header_index_, val);
3069 }
cf43a2fe
AM
3070 Output_data_got<size, big_endian>::do_write(of);
3071 }
3072
3073private:
3074 void
3075 reserve_ent(unsigned int cnt = 1)
3076 {
a19da04b 3077 if (size != 32 || this->header_ent_cnt_ == 0)
cf43a2fe
AM
3078 return;
3079 if (this->num_entries() + cnt > this->header_index_)
3080 this->make_header();
3081 }
3082
3083 void
3084 make_header()
3085 {
3086 this->header_ent_cnt_ = 0;
3087 this->header_index_ = this->num_entries();
3088 if (size == 32)
3089 {
3090 Output_data_got<size, big_endian>::add_constant(0);
3091 Output_data_got<size, big_endian>::add_constant(0);
3092 Output_data_got<size, big_endian>::add_constant(0);
3093
3094 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
3095 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3096 if (gotsym != NULL)
3097 {
3098 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3099 sym->set_value(this->g_o_t());
3100 }
3101 else
3102 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3103 Symbol_table::PREDEFINED,
3104 this, this->g_o_t(), 0,
3105 elfcpp::STT_OBJECT,
3106 elfcpp::STB_LOCAL,
3107 elfcpp::STV_HIDDEN, 0,
3108 false, false);
cf43a2fe
AM
3109 }
3110 else
3111 Output_data_got<size, big_endian>::add_constant(0);
3112 }
3113
3114 // Stashed pointers.
3115 Symbol_table* symtab_;
3116 Layout* layout_;
3117
3118 // GOT header size.
3119 unsigned int header_ent_cnt_;
3120 // GOT header index.
3121 unsigned int header_index_;
42cacb20
DE
3122};
3123
3124// Get the GOT section, creating it if necessary.
3125
3126template<int size, bool big_endian>
cf43a2fe 3127Output_data_got_powerpc<size, big_endian>*
42cacb20 3128Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
f19c3684
AM
3129 Layout* layout,
3130 Got_type got_type)
42cacb20
DE
3131{
3132 if (this->got_ == NULL)
3133 {
3134 gold_assert(symtab != NULL && layout != NULL);
3135
cf43a2fe 3136 this->got_
f19c3684
AM
3137 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3138 GOT_TYPE_SMALL);
42cacb20
DE
3139
3140 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3141 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 3142 this->got_, ORDER_DATA, false);
42cacb20
DE
3143 }
3144
f19c3684
AM
3145 if (size == 32 || (got_type & GOT_TYPE_SMALL))
3146 return this->got_;
3147
3148 if (this->biggot_ == NULL)
3149 {
3150 this->biggot_
3151 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3152 GOT_TYPE_STANDARD);
3153
3154 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3155 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3156 this->biggot_, ORDER_DATA, false);
3157 }
3158
3159 return this->biggot_;
42cacb20
DE
3160}
3161
3162// Get the dynamic reloc section, creating it if necessary.
3163
3164template<int size, bool big_endian>
3165typename Target_powerpc<size, big_endian>::Reloc_section*
3166Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3167{
3168 if (this->rela_dyn_ == NULL)
3169 {
3170 gold_assert(layout != NULL);
3171 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3172 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
3173 elfcpp::SHF_ALLOC, this->rela_dyn_,
3174 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
3175 }
3176 return this->rela_dyn_;
3177}
3178
b3ccdeb5
AM
3179// Similarly, but for ifunc symbols get the one for ifunc.
3180
3181template<int size, bool big_endian>
3182typename Target_powerpc<size, big_endian>::Reloc_section*
3183Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3184 Layout* layout,
3185 bool for_ifunc)
3186{
3187 if (!for_ifunc)
3188 return this->rela_dyn_section(layout);
3189
3190 if (this->iplt_ == NULL)
3191 this->make_iplt_section(symtab, layout);
3192 return this->iplt_->rel_plt();
3193}
3194
ec661b9d
AM
3195class Stub_control
3196{
3197 public:
3198 // Determine the stub group size. The group size is the absolute
3199 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 3200 // is passed a negative value, we restrict stubs to be always after
ec661b9d 3201 // the stubbed branches.
1c3a5fbe
AM
3202 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3203 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3204 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3205 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3206 owner_(NULL), output_section_(NULL)
ec661b9d 3207 {
ec661b9d
AM
3208 }
3209
3210 // Return true iff input section can be handled by current stub
3211 // group.
3212 bool
3213 can_add_to_stub_group(Output_section* o,
3214 const Output_section::Input_section* i,
3215 bool has14);
3216
3217 const Output_section::Input_section*
3218 owner()
3219 { return owner_; }
3220
3221 Output_section*
3222 output_section()
3223 { return output_section_; }
3224
a20605cf
AM
3225 void
3226 set_output_and_owner(Output_section* o,
3227 const Output_section::Input_section* i)
3228 {
3229 this->output_section_ = o;
3230 this->owner_ = i;
3231 }
3232
ec661b9d
AM
3233 private:
3234 typedef enum
3235 {
1c3a5fbe 3236 // Initial state.
ec661b9d 3237 NO_GROUP,
1c3a5fbe 3238 // Adding group sections before the stubs.
ec661b9d 3239 FINDING_STUB_SECTION,
1c3a5fbe 3240 // Adding group sections after the stubs.
ec661b9d
AM
3241 HAS_STUB_SECTION
3242 } State;
3243
ec661b9d 3244 uint32_t stub_group_size_;
a5018ae5 3245 bool stubs_always_after_branch_;
ec661b9d 3246 bool suppress_size_errors_;
1c3a5fbe
AM
3247 // True if a stub group can serve multiple output sections.
3248 bool multi_os_;
3249 State state_;
8a37735f
AM
3250 // Current max size of group. Starts at stub_group_size_ but is
3251 // reduced to stub_group_size_/1024 on seeing a section with
3252 // external conditional branches.
3253 uint32_t group_size_;
a5018ae5 3254 uint64_t group_start_addr_;
57f6d32d
AM
3255 // owner_ and output_section_ specify the section to which stubs are
3256 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
3257 const Output_section::Input_section* owner_;
3258 Output_section* output_section_;
3259};
3260
0cfdc767 3261// Return true iff input section can be handled by current stub
a5018ae5
AM
3262// group. Sections are presented to this function in order,
3263// so the first section is the head of the group.
ec661b9d
AM
3264
3265bool
3266Stub_control::can_add_to_stub_group(Output_section* o,
3267 const Output_section::Input_section* i,
3268 bool has14)
3269{
ec661b9d
AM
3270 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3271 uint64_t this_size;
3272 uint64_t start_addr = o->address();
3273
3274 if (whole_sec)
3275 // .init and .fini sections are pasted together to form a single
3276 // function. We can't be adding stubs in the middle of the function.
3277 this_size = o->data_size();
3278 else
3279 {
3280 start_addr += i->relobj()->output_section_offset(i->shndx());
3281 this_size = i->data_size();
3282 }
57f6d32d 3283
a5018ae5 3284 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
3285 uint32_t group_size = this->stub_group_size_;
3286 if (has14)
3287 this->group_size_ = group_size = group_size >> 10;
ec661b9d 3288
57f6d32d 3289 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
3290 gold_warning(_("%s:%s exceeds group size"),
3291 i->relobj()->name().c_str(),
3292 i->relobj()->section_name(i->shndx()).c_str());
3293
afe002dd
AM
3294 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3295 has14 ? " 14bit" : "",
3296 i->relobj()->name().c_str(),
3297 i->relobj()->section_name(i->shndx()).c_str(),
3298 (long long) this_size,
a5018ae5
AM
3299 (this->state_ == NO_GROUP
3300 ? this_size
3301 : (long long) end_addr - this->group_start_addr_));
afe002dd 3302
1c3a5fbe
AM
3303 if (this->state_ == NO_GROUP)
3304 {
3305 // Only here on very first use of Stub_control
3306 this->owner_ = i;
3307 this->output_section_ = o;
3308 this->state_ = FINDING_STUB_SECTION;
3309 this->group_size_ = group_size;
3310 this->group_start_addr_ = start_addr;
3311 return true;
3312 }
3313 else if (!this->multi_os_ && this->output_section_ != o)
3314 ;
3315 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 3316 {
a5018ae5 3317 // Can we add this section, which is after the stubs, to the
57f6d32d 3318 // group?
a5018ae5 3319 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3320 return true;
ec661b9d 3321 }
a5018ae5 3322 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 3323 {
a5018ae5
AM
3324 if ((whole_sec && this->output_section_ == o)
3325 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3326 {
a5018ae5 3327 // Stubs are added at the end of "owner_".
57f6d32d
AM
3328 this->owner_ = i;
3329 this->output_section_ = o;
a5018ae5 3330 return true;
57f6d32d 3331 }
a5018ae5
AM
3332 // The group before the stubs has reached maximum size.
3333 // Now see about adding sections after the stubs to the
3334 // group. If the current section has a 14-bit branch and
3335 // the group before the stubs exceeds group_size_ (because
3336 // they didn't have 14-bit branches), don't add sections
3337 // after the stubs: The size of stubs for such a large
3338 // group may exceed the reach of a 14-bit branch.
3339 if (!this->stubs_always_after_branch_
3340 && this_size <= this->group_size_
3341 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3342 {
a5018ae5
AM
3343 gold_debug(DEBUG_TARGET, "adding after stubs");
3344 this->state_ = HAS_STUB_SECTION;
3345 this->group_start_addr_ = start_addr;
57f6d32d
AM
3346 return true;
3347 }
ec661b9d 3348 }
a5018ae5
AM
3349 else
3350 gold_unreachable();
57f6d32d 3351
1c3a5fbe
AM
3352 gold_debug(DEBUG_TARGET,
3353 !this->multi_os_ && this->output_section_ != o
3354 ? "nope, new output section\n"
3355 : "nope, didn't fit\n");
afe002dd 3356
57f6d32d
AM
3357 // The section fails to fit in the current group. Set up a few
3358 // things for the next group. owner_ and output_section_ will be
3359 // set later after we've retrieved those values for the current
3360 // group.
3361 this->state_ = FINDING_STUB_SECTION;
8a37735f 3362 this->group_size_ = group_size;
a5018ae5 3363 this->group_start_addr_ = start_addr;
57f6d32d 3364 return false;
ec661b9d
AM
3365}
3366
3367// Look over all the input sections, deciding where to place stubs.
3368
3369template<int size, bool big_endian>
3370void
3371Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
3372 const Task*,
3373 bool no_size_errors)
ec661b9d 3374{
1c3a5fbe
AM
3375 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3376 parameters->options().stub_group_multi());
ec661b9d
AM
3377
3378 // Group input sections and insert stub table
a3e60ddb
AM
3379 Stub_table_owner* table_owner = NULL;
3380 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
3381 Layout::Section_list section_list;
3382 layout->get_executable_sections(&section_list);
3383 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
3384 for (Layout::Section_list::iterator o = section_list.begin();
3385 o != section_list.end();
ec661b9d
AM
3386 ++o)
3387 {
3388 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
3389 for (Input_section_list::const_iterator i
3390 = (*o)->input_sections().begin();
3391 i != (*o)->input_sections().end();
ec661b9d
AM
3392 ++i)
3393 {
a3e60ddb
AM
3394 if (i->is_input_section()
3395 || i->is_relaxed_input_section())
ec661b9d
AM
3396 {
3397 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3398 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3399 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3400 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3401 {
a3e60ddb
AM
3402 table_owner->output_section = stub_control.output_section();
3403 table_owner->owner = stub_control.owner();
a20605cf 3404 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 3405 table_owner = NULL;
ec661b9d 3406 }
a3e60ddb
AM
3407 if (table_owner == NULL)
3408 {
3409 table_owner = new Stub_table_owner;
3410 tables.push_back(table_owner);
3411 }
3412 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
3413 }
3414 }
3415 }
a3e60ddb 3416 if (table_owner != NULL)
0cfdc767 3417 {
a5018ae5
AM
3418 table_owner->output_section = stub_control.output_section();
3419 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
3420 }
3421 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3422 t != tables.end();
3423 ++t)
3424 {
3425 Stub_table<size, big_endian>* stub_table;
3426
3427 if ((*t)->owner->is_input_section())
3428 stub_table = new Stub_table<size, big_endian>(this,
3429 (*t)->output_section,
590b87ff
AM
3430 (*t)->owner,
3431 this->stub_tables_.size());
a3e60ddb
AM
3432 else if ((*t)->owner->is_relaxed_input_section())
3433 stub_table = static_cast<Stub_table<size, big_endian>*>(
3434 (*t)->owner->relaxed_input_section());
0cfdc767 3435 else
a3e60ddb
AM
3436 gold_unreachable();
3437 this->stub_tables_.push_back(stub_table);
3438 delete *t;
0cfdc767 3439 }
ec661b9d
AM
3440}
3441
32f59844 3442template<int size>
a3e60ddb
AM
3443static unsigned long
3444max_branch_delta (unsigned int r_type)
3445{
3446 if (r_type == elfcpp::R_POWERPC_REL14
3447 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3448 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3449 return 1L << 15;
3450 if (r_type == elfcpp::R_POWERPC_REL24
32f59844 3451 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 3452 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
a3e60ddb
AM
3453 || r_type == elfcpp::R_PPC_PLTREL24
3454 || r_type == elfcpp::R_PPC_LOCAL24PC)
3455 return 1L << 25;
3456 return 0;
3457}
3458
7e57d19e
AM
3459// Return whether this branch is going via a plt call stub.
3460
3461template<int size, bool big_endian>
3462bool
3463Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3464 Powerpc_relobj<size, big_endian>* ppc_object,
3465 unsigned int shndx,
3466 Address offset,
3467 Target_powerpc* target,
3468 Symbol_table* symtab)
3469{
3470 if (this->object_ != ppc_object
3471 || this->shndx_ != shndx
3472 || this->offset_ != offset)
3473 return false;
3474
3475 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3476 if (sym != NULL && sym->is_forwarder())
3477 sym = symtab->resolve_forwards(sym);
2778747c
AM
3478 if (target->replace_tls_get_addr(sym))
3479 sym = target->tls_get_addr_opt();
7e57d19e
AM
3480 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3481 if (gsym != NULL
7ee7ff70
AM
3482 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3483 && !target->is_elfv2_localentry0(gsym))
3484 : (this->object_->local_has_plt_offset(this->r_sym_)
3485 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3486 {
3487 this->tocsave_ = 1;
3488 return true;
3489 }
3490 return false;
3491}
3492
ec661b9d
AM
3493// If this branch needs a plt call stub, or a long branch stub, make one.
3494
3495template<int size, bool big_endian>
a3e60ddb 3496bool
ec661b9d
AM
3497Target_powerpc<size, big_endian>::Branch_info::make_stub(
3498 Stub_table<size, big_endian>* stub_table,
3499 Stub_table<size, big_endian>* ifunc_stub_table,
3500 Symbol_table* symtab) const
3501{
3502 Symbol* sym = this->object_->global_symbol(this->r_sym_);
88b8e639
AM
3503 Target_powerpc<size, big_endian>* target =
3504 static_cast<Target_powerpc<size, big_endian>*>(
3505 parameters->sized_target<size, big_endian>());
34e0882b
AM
3506 if (sym != NULL && sym->is_forwarder())
3507 sym = symtab->resolve_forwards(sym);
2778747c
AM
3508 if (target->replace_tls_get_addr(sym))
3509 sym = target->tls_get_addr_opt();
34e0882b 3510 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
dc60b26d
AM
3511 bool ok = true;
3512
ec661b9d 3513 if (gsym != NULL
88b8e639 3514 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3515 : this->object_->local_has_plt_offset(this->r_sym_))
3516 {
9055360d
AM
3517 if (size == 64
3518 && gsym != NULL
3519 && target->abiversion() >= 2
3520 && !parameters->options().output_is_position_independent()
32f59844 3521 && !is_branch_reloc<size>(this->r_type_))
9055360d
AM
3522 target->glink_section()->add_global_entry(gsym);
3523 else
ec661b9d 3524 {
64b5d6d7
AM
3525 if (stub_table == NULL
3526 && !(size == 32
3527 && gsym != NULL
3528 && !parameters->options().output_is_position_independent()
32f59844 3529 && !is_branch_reloc<size>(this->r_type_)))
9055360d
AM
3530 stub_table = this->object_->stub_table(this->shndx_);
3531 if (stub_table == NULL)
3532 {
64b5d6d7
AM
3533 // This is a ref from a data section to an ifunc symbol,
3534 // or a non-branch reloc for which we always want to use
3535 // one set of stubs for resolving function addresses.
9055360d
AM
3536 stub_table = ifunc_stub_table;
3537 }
3538 gold_assert(stub_table != NULL);
a3e60ddb
AM
3539 Address from = this->object_->get_output_section_offset(this->shndx_);
3540 if (from != invalid_address)
3541 from += (this->object_->output_section(this->shndx_)->address()
3542 + this->offset_);
9055360d 3543 if (gsym != NULL)
dc60b26d
AM
3544 ok = stub_table->add_plt_call_entry(from,
3545 this->object_, gsym,
7e57d19e
AM
3546 this->r_type_, this->addend_,
3547 this->tocsave_);
9055360d 3548 else
dc60b26d
AM
3549 ok = stub_table->add_plt_call_entry(from,
3550 this->object_, this->r_sym_,
7e57d19e
AM
3551 this->r_type_, this->addend_,
3552 this->tocsave_);
ec661b9d 3553 }
ec661b9d
AM
3554 }
3555 else
3556 {
32f59844 3557 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
a3e60ddb
AM
3558 if (max_branch_offset == 0)
3559 return true;
ec661b9d
AM
3560 Address from = this->object_->get_output_section_offset(this->shndx_);
3561 gold_assert(from != invalid_address);
3562 from += (this->object_->output_section(this->shndx_)->address()
3563 + this->offset_);
3564 Address to;
e3b53295 3565 unsigned int other = 0;
ec661b9d
AM
3566 if (gsym != NULL)
3567 {
3568 switch (gsym->source())
3569 {
3570 case Symbol::FROM_OBJECT:
3571 {
3572 Object* symobj = gsym->object();
3573 if (symobj->is_dynamic()
3574 || symobj->pluginobj() != NULL)
a3e60ddb 3575 return true;
ec661b9d
AM
3576 bool is_ordinary;
3577 unsigned int shndx = gsym->shndx(&is_ordinary);
3578 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3579 return true;
ec661b9d
AM
3580 }
3581 break;
3582
3583 case Symbol::IS_UNDEFINED:
a3e60ddb 3584 return true;
ec661b9d
AM
3585
3586 default:
3587 break;
3588 }
3589 Symbol_table::Compute_final_value_status status;
3590 to = symtab->compute_final_value<size>(gsym, &status);
3591 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3592 return true;
e3b53295
AM
3593 if (size == 64)
3594 other = gsym->nonvis() >> 3;
ec661b9d
AM
3595 }
3596 else
3597 {
3598 const Symbol_value<size>* psymval
3599 = this->object_->local_symbol(this->r_sym_);
3600 Symbol_value<size> symval;
0f125432
CC
3601 if (psymval->is_section_symbol())
3602 symval.set_is_section_symbol();
ec661b9d
AM
3603 typedef Sized_relobj_file<size, big_endian> ObjType;
3604 typename ObjType::Compute_final_local_value_status status
3605 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3606 &symval, symtab);
3607 if (status != ObjType::CFLV_OK
3608 || !symval.has_output_value())
a3e60ddb 3609 return true;
ec661b9d 3610 to = symval.value(this->object_, 0);
e3b53295
AM
3611 if (size == 64)
3612 other = this->object_->st_other(this->r_sym_) >> 5;
ec661b9d 3613 }
cbcb23fa
AM
3614 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3615 to += this->addend_;
ec661b9d
AM
3616 if (stub_table == NULL)
3617 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3618 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3619 {
3620 unsigned int dest_shndx;
1611bc4a
AM
3621 if (!target->symval_for_branch(symtab, gsym, this->object_,
3622 &to, &dest_shndx))
3623 return true;
ec661b9d 3624 }
fa40fbe4
AM
3625 unsigned int local_ent = 0;
3626 if (size == 64
eb97d024
AM
3627 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
3628 && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
fa40fbe4
AM
3629 local_ent = elfcpp::ppc64_decode_local_entry(other);
3630 Address delta = to + local_ent - from;
32f59844
AM
3631 if (delta + max_branch_offset >= 2 * max_branch_offset
3632 || (size == 64
eb97d024
AM
3633 && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3634 || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844
AM
3635 && (gsym != NULL
3636 ? this->object_->ppc64_needs_toc(gsym)
3637 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3638 {
0cfdc767
AM
3639 if (stub_table == NULL)
3640 {
3641 gold_warning(_("%s:%s: branch in non-executable section,"
3642 " no long branch stub for you"),
3643 this->object_->name().c_str(),
3644 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3645 return true;
0cfdc767 3646 }
d49044c7
AM
3647 bool save_res = (size == 64
3648 && gsym != NULL
3649 && gsym->source() == Symbol::IN_OUTPUT_DATA
3650 && gsym->output_data() == target->savres_section());
a19da04b 3651 ok = stub_table->add_long_branch_entry(this->r_type_,
fa40fbe4 3652 from, to, other, save_res);
ec661b9d
AM
3653 }
3654 }
dc60b26d
AM
3655 if (!ok)
3656 gold_debug(DEBUG_TARGET,
3657 "branch at %s:%s+%#lx\n"
3658 "can't reach stub attached to %s:%s",
3659 this->object_->name().c_str(),
3660 this->object_->section_name(this->shndx_).c_str(),
3661 (unsigned long) this->offset_,
3662 stub_table->relobj()->name().c_str(),
3663 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3664
3665 return ok;
ec661b9d
AM
3666}
3667
3668// Relaxation hook. This is where we do stub generation.
3669
3670template<int size, bool big_endian>
3671bool
3672Target_powerpc<size, big_endian>::do_relax(int pass,
3673 const Input_objects*,
3674 Symbol_table* symtab,
3675 Layout* layout,
3676 const Task* task)
3677{
3678 unsigned int prev_brlt_size = 0;
3679 if (pass == 1)
ec661b9d 3680 {
b4f7960d
AM
3681 bool thread_safe
3682 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3683 if (size == 64
3684 && this->abiversion() < 2
3685 && !thread_safe
3686 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3687 {
e2458743 3688 static const char* const thread_starter[] =
9e69ed50
AM
3689 {
3690 "pthread_create",
3691 /* libstdc++ */
3692 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3693 /* librt */
3694 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3695 "mq_notify", "create_timer",
3696 /* libanl */
3697 "getaddrinfo_a",
3698 /* libgomp */
80272b8c 3699 "GOMP_parallel",
9e69ed50 3700 "GOMP_parallel_start",
80272b8c 3701 "GOMP_parallel_loop_static",
9e69ed50 3702 "GOMP_parallel_loop_static_start",
80272b8c 3703 "GOMP_parallel_loop_dynamic",
9e69ed50 3704 "GOMP_parallel_loop_dynamic_start",
80272b8c 3705 "GOMP_parallel_loop_guided",
9e69ed50 3706 "GOMP_parallel_loop_guided_start",
80272b8c 3707 "GOMP_parallel_loop_runtime",
9e69ed50 3708 "GOMP_parallel_loop_runtime_start",
80272b8c 3709 "GOMP_parallel_sections",
43819297 3710 "GOMP_parallel_sections_start",
f9dffbf0
AM
3711 /* libgo */
3712 "__go_go",
9e69ed50
AM
3713 };
3714
e2458743
AM
3715 if (parameters->options().shared())
3716 thread_safe = true;
3717 else
9e69ed50 3718 {
e2458743
AM
3719 for (unsigned int i = 0;
3720 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3721 i++)
3722 {
3723 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3724 thread_safe = (sym != NULL
3725 && sym->in_reg()
3726 && sym->in_real_elf());
3727 if (thread_safe)
3728 break;
3729 }
9e69ed50 3730 }
ec661b9d 3731 }
9e69ed50 3732 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3733 }
3734
3735 if (pass == 1)
3736 {
3737 this->stub_group_size_ = parameters->options().stub_group_size();
3738 bool no_size_errors = true;
3739 if (this->stub_group_size_ == 1)
3740 this->stub_group_size_ = 0x1c00000;
3741 else if (this->stub_group_size_ == -1)
3742 this->stub_group_size_ = -0x1e00000;
3743 else
3744 no_size_errors = false;
3745 this->group_sections(layout, task, no_size_errors);
3746 }
3747 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3748 {
3749 this->branch_lookup_table_.clear();
3750 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3751 p != this->stub_tables_.end();
3752 ++p)
3753 {
3754 (*p)->clear_stubs(true);
3755 }
3756 this->stub_tables_.clear();
3757 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3758 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3759 program_name, this->stub_group_size_);
3760 this->group_sections(layout, task, true);
ec661b9d
AM
3761 }
3762
3763 // We need address of stub tables valid for make_stub.
3764 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3765 p != this->stub_tables_.end();
3766 ++p)
3767 {
3768 const Powerpc_relobj<size, big_endian>* object
3769 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3770 Address off = object->get_output_section_offset((*p)->shndx());
3771 gold_assert(off != invalid_address);
3772 Output_section* os = (*p)->output_section();
3773 (*p)->set_address_and_size(os, off);
3774 }
3775
9e69ed50
AM
3776 if (pass != 1)
3777 {
3778 // Clear plt call stubs, long branch stubs and branch lookup table.
3779 prev_brlt_size = this->branch_lookup_table_.size();
3780 this->branch_lookup_table_.clear();
3781 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3782 p != this->stub_tables_.end();
3783 ++p)
3784 {
a3e60ddb 3785 (*p)->clear_stubs(false);
9e69ed50
AM
3786 }
3787 }
3788
3789 // Build all the stubs.
a3e60ddb 3790 this->relax_failed_ = false;
ec661b9d
AM
3791 Stub_table<size, big_endian>* ifunc_stub_table
3792 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3793 Stub_table<size, big_endian>* one_stub_table
3794 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3795 for (typename Branches::const_iterator b = this->branch_info_.begin();
3796 b != this->branch_info_.end();
3797 b++)
3798 {
a3e60ddb
AM
3799 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3800 && !this->relax_failed_)
3801 {
3802 this->relax_failed_ = true;
3803 this->relax_fail_count_++;
3804 if (this->relax_fail_count_ < 3)
3805 return true;
3806 }
ec661b9d 3807 }
32f59844
AM
3808 bool do_resize = false;
3809 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3810 p != this->stub_tables_.end();
3811 ++p)
3812 if ((*p)->need_resize())
3813 {
3814 do_resize = true;
3815 break;
3816 }
3817 if (do_resize)
3818 {
3819 this->branch_lookup_table_.clear();
3820 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3821 p != this->stub_tables_.end();
3822 ++p)
3823 (*p)->set_resizing(true);
3824 for (typename Branches::const_iterator b = this->branch_info_.begin();
3825 b != this->branch_info_.end();
3826 b++)
3827 {
3828 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3829 && !this->relax_failed_)
3830 {
3831 this->relax_failed_ = true;
3832 this->relax_fail_count_++;
3833 if (this->relax_fail_count_ < 3)
3834 return true;
3835 }
3836 }
3837 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3838 p != this->stub_tables_.end();
3839 ++p)
3840 (*p)->set_resizing(false);
3841 }
ec661b9d 3842
9e69ed50 3843 // Did anything change size?
ec661b9d
AM
3844 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3845 bool again = num_huge_branches != prev_brlt_size;
3846 if (size == 64 && num_huge_branches != 0)
3847 this->make_brlt_section(layout);
3848 if (size == 64 && again)
3849 this->brlt_section_->set_current_size(num_huge_branches);
3850
be897fb7
AM
3851 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3852 p != this->stub_tables_.rend();
3853 ++p)
3854 (*p)->remove_eh_frame(layout);
3855
3856 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3857 p != this->stub_tables_.end();
3858 ++p)
3859 (*p)->add_eh_frame(layout);
3860
ec661b9d
AM
3861 typedef Unordered_set<Output_section*> Output_sections;
3862 Output_sections os_need_update;
3863 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3864 p != this->stub_tables_.end();
3865 ++p)
3866 {
3867 if ((*p)->size_update())
3868 {
3869 again = true;
3870 os_need_update.insert((*p)->output_section());
3871 }
3872 }
3873
9e69ed50
AM
3874 // Set output section offsets for all input sections in an output
3875 // section that just changed size. Anything past the stubs will
3876 // need updating.
ec661b9d
AM
3877 for (typename Output_sections::iterator p = os_need_update.begin();
3878 p != os_need_update.end();
3879 p++)
3880 {
3881 Output_section* os = *p;
3882 Address off = 0;
3883 typedef Output_section::Input_section_list Input_section_list;
3884 for (Input_section_list::const_iterator i = os->input_sections().begin();
3885 i != os->input_sections().end();
3886 ++i)
3887 {
3888 off = align_address(off, i->addralign());
3889 if (i->is_input_section() || i->is_relaxed_input_section())
3890 i->relobj()->set_section_offset(i->shndx(), off);
3891 if (i->is_relaxed_input_section())
3892 {
3893 Stub_table<size, big_endian>* stub_table
3894 = static_cast<Stub_table<size, big_endian>*>(
3895 i->relaxed_input_section());
6395d38b
HS
3896 Address stub_table_size = stub_table->set_address_and_size(os, off);
3897 off += stub_table_size;
3898 // After a few iterations, set current stub table size
3899 // as min size threshold, so later stub tables can only
3900 // grow in size.
3901 if (pass >= 4)
3902 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3903 }
3904 else
3905 off += i->data_size();
3906 }
6830ee24
AM
3907 // If .branch_lt is part of this output section, then we have
3908 // just done the offset adjustment.
ec661b9d
AM
3909 os->clear_section_offsets_need_adjustment();
3910 }
3911
3912 if (size == 64
3913 && !again
3914 && num_huge_branches != 0
3915 && parameters->options().output_is_position_independent())
3916 {
3917 // Fill in the BRLT relocs.
06f30c9d 3918 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3919 for (typename Branch_lookup_table::const_iterator p
3920 = this->branch_lookup_table_.begin();
3921 p != this->branch_lookup_table_.end();
3922 ++p)
3923 {
3924 this->brlt_section_->add_reloc(p->first, p->second);
3925 }
06f30c9d 3926 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3927 }
590b87ff
AM
3928
3929 if (!again
3930 && (parameters->options().user_set_emit_stub_syms()
3931 ? parameters->options().emit_stub_syms()
3932 : (size == 64
3933 || parameters->options().output_is_position_independent()
3934 || parameters->options().emit_relocs())))
3935 {
3936 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3937 p != this->stub_tables_.end();
3938 ++p)
3939 (*p)->define_stub_syms(symtab);
3940
3941 if (this->glink_ != NULL)
3942 {
9e390558 3943 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3944 Address value = -stub_size;
3945 if (size == 64)
3946 {
3947 value = 8;
3948 stub_size -= 8;
3949 }
3950 this->define_local(symtab, "__glink_PLTresolve",
3951 this->glink_, value, stub_size);
3952
3953 if (size != 64)
3954 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3955 }
3956 }
3957
ec661b9d
AM
3958 return again;
3959}
3960
9d5781f8
AM
3961template<int size, bool big_endian>
3962void
3963Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3964 unsigned char* oview,
3965 uint64_t* paddress,
3966 off_t* plen) const
3967{
3968 uint64_t address = plt->address();
3969 off_t len = plt->data_size();
3970
3971 if (plt == this->glink_)
3972 {
3973 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3974 if (len == 0)
3975 {
5fe7ffdc
AM
3976 // Static linking may need stubs, to support ifunc and long
3977 // branches. We need to create an output section for
3978 // .eh_frame early in the link process, to have a place to
3979 // attach stub .eh_frame info. We also need to have
3980 // registered a CIE that matches the stub CIE. Both of
3981 // these requirements are satisfied by creating an FDE and
3982 // CIE for .glink, even though static linking will leave
3983 // .glink zero length.
3984 // ??? Hopefully generating an FDE with a zero address range
3985 // won't confuse anything that consumes .eh_frame info.
3986 }
3987 else if (size == 64)
9d5781f8
AM
3988 {
3989 // There is one word before __glink_PLTresolve
3990 address += 8;
3991 len -= 8;
3992 }
3993 else if (parameters->options().output_is_position_independent())
3994 {
3995 // There are two FDEs for a position independent glink.
3996 // The first covers the branch table, the second
3997 // __glink_PLTresolve at the end of glink.
9e390558 3998 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 3999 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
4000 len -= resolve_size;
4001 else
4002 {
4003 address += len - resolve_size;
4004 len = resolve_size;
4005 }
4006 }
4007 }
4008 else
4009 {
4010 // Must be a stub table.
4011 const Stub_table<size, big_endian>* stub_table
4012 = static_cast<const Stub_table<size, big_endian>*>(plt);
4013 uint64_t stub_address = stub_table->stub_address();
4014 len -= stub_address - address;
4015 address = stub_address;
4016 }
4017
4018 *paddress = address;
4019 *plen = len;
4020}
4021
42cacb20
DE
4022// A class to handle the PLT data.
4023
4024template<int size, bool big_endian>
cf43a2fe 4025class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
4026{
4027 public:
4028 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4029 size, big_endian> Reloc_section;
4030
e5d5f5ed 4031 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
f21dbd7c 4032 Symbol_table* symtab,
e5d5f5ed 4033 Reloc_section* plt_rel,
e5d5f5ed
AM
4034 const char* name)
4035 : Output_section_data_build(size == 32 ? 4 : 8),
f21dbd7c 4036 rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
e5d5f5ed 4037 { }
42cacb20
DE
4038
4039 // Add an entry to the PLT.
03e25981 4040 void
f21dbd7c 4041 add_entry(Symbol*, bool = false);
42cacb20 4042
03e25981 4043 void
e5d5f5ed
AM
4044 add_ifunc_entry(Symbol*);
4045
2d7ad24e
AM
4046 void
4047 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4048
03e25981 4049 void
e5d5f5ed
AM
4050 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4051
42cacb20 4052 // Return the .rela.plt section data.
e5d5f5ed 4053 Reloc_section*
cf43a2fe
AM
4054 rel_plt() const
4055 {
42cacb20
DE
4056 return this->rel_;
4057 }
4058
0e70b911
CC
4059 // Return the number of PLT entries.
4060 unsigned int
4061 entry_count() const
d83ce4e3 4062 {
b3ccdeb5
AM
4063 if (this->current_data_size() == 0)
4064 return 0;
b4f7960d
AM
4065 return ((this->current_data_size() - this->first_plt_entry_offset())
4066 / this->plt_entry_size());
d83ce4e3 4067 }
0e70b911 4068
42cacb20 4069 protected:
42cacb20 4070 void
cf43a2fe 4071 do_adjust_output_section(Output_section* os)
42cacb20 4072 {
cf43a2fe 4073 os->set_entsize(0);
42cacb20
DE
4074 }
4075
6ce78956
AM
4076 // Write to a map file.
4077 void
4078 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4079 { mapfile->print_output_data(this, this->name_); }
6ce78956 4080
cf43a2fe 4081 private:
f21dbd7c
AM
4082 struct Local_plt_ent
4083 {
4084 Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4085 { rsym_ = rsym; u.obj_ = obj; }
4086 Local_plt_ent(Symbol* sym)
4087 { rsym_ = -1u; u.gsym_ = sym; }
4088 ~Local_plt_ent()
4089 { }
4090
4091 unsigned int rsym_;
4092 union
4093 {
4094 Sized_relobj_file<size, big_endian>* obj_;
4095 Symbol* gsym_;
4096 } u;
4097 };
4098
b4f7960d
AM
4099 // Return the offset of the first non-reserved PLT entry.
4100 unsigned int
4101 first_plt_entry_offset() const
4102 {
2d7ad24e
AM
4103 // IPLT and LPLT have no reserved entry.
4104 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4105 return 0;
4106 return this->targ_->first_plt_entry_offset();
4107 }
4108
4109 // Return the size of each PLT entry.
4110 unsigned int
4111 plt_entry_size() const
4112 {
4113 return this->targ_->plt_entry_size();
4114 }
cf43a2fe 4115
42cacb20
DE
4116 // Write out the PLT data.
4117 void
4118 do_write(Output_file*);
4119
4120 // The reloc section.
4121 Reloc_section* rel_;
cf43a2fe
AM
4122 // Allows access to .glink for do_write.
4123 Target_powerpc<size, big_endian>* targ_;
f21dbd7c 4124 Symbol_table* symtab_;
e5d5f5ed
AM
4125 // What to report in map file.
4126 const char *name_;
f21dbd7c
AM
4127
4128 std::vector<Local_plt_ent> sym_ents_;
42cacb20
DE
4129};
4130
e5d5f5ed 4131// Add an entry to the PLT.
42cacb20
DE
4132
4133template<int size, bool big_endian>
03e25981 4134void
f21dbd7c
AM
4135Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
4136 bool stash)
42cacb20 4137{
e5d5f5ed
AM
4138 if (!gsym->has_plt_offset())
4139 {
ec661b9d 4140 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4141 if (off == 0)
4142 off += this->first_plt_entry_offset();
4143 gsym->set_plt_offset(off);
f21dbd7c
AM
4144 if (this->rel_)
4145 {
4146 gsym->set_needs_dynsym_entry();
4147 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4148 this->rel_->add_global(gsym, dynrel, this, off, 0);
4149 }
b4f7960d 4150 off += this->plt_entry_size();
e5d5f5ed 4151 this->set_current_data_size(off);
f21dbd7c
AM
4152 if (stash)
4153 {
4154 Local_plt_ent sym(gsym);
4155 this->sym_ents_.push_back(sym);
4156 }
e5d5f5ed 4157 }
42cacb20
DE
4158}
4159
e5d5f5ed 4160// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4161
4162template<int size, bool big_endian>
03e25981 4163void
e5d5f5ed 4164Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4165{
cf43a2fe
AM
4166 if (!gsym->has_plt_offset())
4167 {
ec661b9d 4168 section_size_type off = this->current_data_size();
cf43a2fe 4169 gsym->set_plt_offset(off);
e5d5f5ed 4170 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4171 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4172 dynrel = elfcpp::R_PPC64_JMP_IREL;
4173 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4174 off += this->plt_entry_size();
e5d5f5ed
AM
4175 this->set_current_data_size(off);
4176 }
4177}
4178
2d7ad24e
AM
4179// Add an entry for a local symbol to the PLT.
4180
4181template<int size, bool big_endian>
4182void
4183Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4184 Sized_relobj_file<size, big_endian>* relobj,
4185 unsigned int local_sym_index)
4186{
4187 if (!relobj->local_has_plt_offset(local_sym_index))
4188 {
4189 section_size_type off = this->current_data_size();
4190 relobj->set_local_plt_offset(local_sym_index, off);
4191 if (this->rel_)
4192 {
4193 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4194 if (size == 64 && this->targ_->abiversion() < 2)
4195 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4196 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4197 dynrel, this, off, 0);
4198 }
4199 off += this->plt_entry_size();
4200 this->set_current_data_size(off);
f21dbd7c
AM
4201 Local_plt_ent sym(relobj, local_sym_index);
4202 this->sym_ents_.push_back(sym);
2d7ad24e
AM
4203 }
4204}
4205
e5d5f5ed
AM
4206// Add an entry for a local ifunc symbol to the IPLT.
4207
4208template<int size, bool big_endian>
03e25981 4209void
e5d5f5ed
AM
4210Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4211 Sized_relobj_file<size, big_endian>* relobj,
4212 unsigned int local_sym_index)
4213{
4214 if (!relobj->local_has_plt_offset(local_sym_index))
4215 {
ec661b9d 4216 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4217 relobj->set_local_plt_offset(local_sym_index, off);
4218 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4219 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4220 dynrel = elfcpp::R_PPC64_JMP_IREL;
4221 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4222 this, off, 0);
b4f7960d 4223 off += this->plt_entry_size();
cf43a2fe
AM
4224 this->set_current_data_size(off);
4225 }
42cacb20
DE
4226}
4227
dd93cd0a 4228static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4229static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4230static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4231static const uint32_t add_3_3_2 = 0x7c631214;
4232static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4233static const uint32_t add_3_12_2 = 0x7c6c1214;
4234static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4235static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4236static const uint32_t add_11_2_11 = 0x7d625a14;
4237static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4238static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4239static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4240static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4241static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4242static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4243static const uint32_t addi_12_1 = 0x39810000;
32f59844 4244static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4245static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4246static const uint32_t addis_0_2 = 0x3c020000;
4247static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4248static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4249static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4250static const uint32_t addis_11_11 = 0x3d6b0000;
4251static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4252static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4253static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4254static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4255static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4256static const uint32_t b = 0x48000000;
4257static const uint32_t bcl_20_31 = 0x429f0005;
4258static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4259static const uint32_t bctrl = 0x4e800421;
4260static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4261static const uint32_t blr = 0x4e800020;
9e69ed50 4262static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4263static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4264static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4265static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4266static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4267static const uint32_t cror_15_15_15 = 0x4def7b82;
4268static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29 4269static const uint32_t ld_0_1 = 0xe8010000;
63e5eea2 4270static const uint32_t ld_0_11 = 0xe80b0000;
f3a0ed29 4271static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4272static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4273static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4274static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4275static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4276static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4277static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4278static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4279static const uint32_t ld_11_11 = 0xe96b0000;
4280static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4281static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4282static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4283static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4284static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4285static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4286static const uint32_t li_0_0 = 0x38000000;
e4dff765 4287static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4288static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4289static const uint32_t lis_0 = 0x3c000000;
549dba71 4290static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4291static const uint32_t lis_11 = 0x3d600000;
4292static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4293static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4294static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4295static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4296static const uint32_t lwz_11_11 = 0x816b0000;
4297static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4298static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4299static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4300static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4301static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4302static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4303static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4304static const uint32_t mr_0_3 = 0x7c601b78;
4305static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4306static const uint32_t mtctr_0 = 0x7c0903a6;
4307static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4308static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4309static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4310static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4311static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4312static const uint32_t nop = 0x60000000;
c9269dff 4313static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4314static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4315static const uint32_t ori_12_12_0 = 0x618c0000;
4316static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4317static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4318static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4319static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4320static const uint32_t std_0_1 = 0xf8010000;
4321static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4322static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4323static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4324static const uint32_t stfd_0_1 = 0xd8010000;
4325static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4326static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4327static const uint32_t sub_12_12_11 = 0x7d8b6050;
4328static const uint32_t xor_2_12_12 = 0x7d826278;
4329static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4330
e4dff765
AM
4331static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4332static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4333static const uint64_t pnop = 0x0700000000000000ULL;
4334
42cacb20
DE
4335// Write out the PLT.
4336
4337template<int size, bool big_endian>
4338void
4339Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4340{
c21736ae
AM
4341 if (!this->sym_ents_.empty()
4342 && !parameters->options().output_is_position_independent())
f21dbd7c
AM
4343 {
4344 const section_size_type offset = this->offset();
4345 const section_size_type oview_size
4346 = convert_to_section_size_type(this->data_size());
4347 unsigned char* const oview = of->get_output_view(offset, oview_size);
4348 unsigned char* pov = oview;
4349 unsigned char* endpov = oview + oview_size;
4350
4351 for (typename std::vector<Local_plt_ent>::iterator e
4352 = this->sym_ents_.begin();
4353 e != this->sym_ents_.end();
4354 e++)
4355 {
4356 typename elfcpp::Elf_types<size>::Elf_Addr val;
4357 Sized_symbol<size>* gsym = NULL;
4358 Powerpc_relobj<size, big_endian>* obj = NULL;
4359 if (e->rsym_ == -1u)
4360 {
4361 gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4362 val = gsym->value();
4363 }
4364 else
4365 {
4366 obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4367 val = obj->local_symbol(e->rsym_)->value(obj, 0);
4368 }
4369 if (this->targ_->abiversion() >= 2)
4370 {
4371 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4372 pov += size / 8;
4373 }
4374 else
4375 {
4376 unsigned int shndx;
4377 this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4378 &val, &shndx);
4379 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4380 pov += size / 8;
4381 val = this->targ_->toc_pointer();
4382 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4383 pov += size / 8;
4384 if (this->plt_entry_size() > 16)
4385 {
4386 elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4387 pov += size / 8;
4388 }
4389 }
4390 }
4391 gold_assert(pov == endpov);
4392 }
4393
2d7ad24e 4394 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4395 {
ec661b9d 4396 const section_size_type offset = this->offset();
cf43a2fe
AM
4397 const section_size_type oview_size
4398 = convert_to_section_size_type(this->data_size());
4399 unsigned char* const oview = of->get_output_view(offset, oview_size);
4400 unsigned char* pov = oview;
4401 unsigned char* endpov = oview + oview_size;
4402
e5d5f5ed 4403 // The address of the .glink branch table
cf43a2fe
AM
4404 const Output_data_glink<size, big_endian>* glink
4405 = this->targ_->glink_section();
ec661b9d 4406 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4407
4408 while (pov < endpov)
4409 {
4410 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4411 pov += 4;
4412 branch_tab += 4;
4413 }
4414
4415 of->write_output_view(offset, oview_size, oview);
4416 }
4417}
4418
4419// Create the PLT section.
4420
4421template<int size, bool big_endian>
4422void
40b469d7
AM
4423Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4424 Layout* layout)
cf43a2fe
AM
4425{
4426 if (this->plt_ == NULL)
4427 {
40b469d7 4428 if (this->got_ == NULL)
f19c3684 4429 this->got_section(symtab, layout, GOT_TYPE_SMALL);
40b469d7 4430
cf43a2fe
AM
4431 if (this->glink_ == NULL)
4432 make_glink_section(layout);
4433
4434 // Ensure that .rela.dyn always appears before .rela.plt This is
4435 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4436 // needs to include .rela.plt in its range.
cf43a2fe
AM
4437 this->rela_dyn_section(layout);
4438
e5d5f5ed
AM
4439 Reloc_section* plt_rel = new Reloc_section(false);
4440 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4441 elfcpp::SHF_ALLOC, plt_rel,
4442 ORDER_DYNAMIC_PLT_RELOCS, false);
4443 this->plt_
f21dbd7c 4444 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
e5d5f5ed 4445 "** PLT");
cf43a2fe
AM
4446 layout->add_output_section_data(".plt",
4447 (size == 32
4448 ? elfcpp::SHT_PROGBITS
4449 : elfcpp::SHT_NOBITS),
4450 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4451 this->plt_,
4452 (size == 32
4453 ? ORDER_SMALL_DATA
4454 : ORDER_SMALL_BSS),
4455 false);
3254d32c
AM
4456
4457 Output_section* rela_plt_os = plt_rel->output_section();
4458 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4459 }
4460}
4461
e5d5f5ed
AM
4462// Create the IPLT section.
4463
4464template<int size, bool big_endian>
4465void
40b469d7
AM
4466Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4467 Layout* layout)
e5d5f5ed
AM
4468{
4469 if (this->iplt_ == NULL)
4470 {
40b469d7 4471 this->make_plt_section(symtab, layout);
f21dbd7c 4472 this->make_lplt_section(symtab, layout);
e5d5f5ed
AM
4473
4474 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4475 if (this->rela_dyn_->output_section())
4476 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed 4477 this->iplt_
f21dbd7c 4478 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
b4f7960d 4479 "** IPLT");
6528b6eb
AM
4480 if (this->plt_->output_section())
4481 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4482 }
4483}
4484
2d7ad24e
AM
4485// Create the LPLT section.
4486
4487template<int size, bool big_endian>
4488void
f21dbd7c
AM
4489Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4490 Layout* layout)
2d7ad24e
AM
4491{
4492 if (this->lplt_ == NULL)
4493 {
4494 Reloc_section* lplt_rel = NULL;
4495 if (parameters->options().output_is_position_independent())
4496 {
4497 lplt_rel = new Reloc_section(false);
4498 this->rela_dyn_section(layout);
4499 if (this->rela_dyn_->output_section())
4500 this->rela_dyn_->output_section()
4501 ->add_output_section_data(lplt_rel);
4502 }
4503 this->lplt_
f21dbd7c 4504 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
2d7ad24e
AM
4505 "** LPLT");
4506 this->make_brlt_section(layout);
4507 if (this->brlt_section_ && this->brlt_section_->output_section())
4508 this->brlt_section_->output_section()
4509 ->add_output_section_data(this->lplt_);
4510 else
4511 layout->add_output_section_data(".branch_lt",
4512 elfcpp::SHT_PROGBITS,
4513 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4514 this->lplt_,
4515 ORDER_RELRO,
4516 true);
4517 }
4518}
4519
ec661b9d 4520// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4521
4522template<int size, bool big_endian>
ec661b9d 4523class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4524{
4525 public:
ec661b9d
AM
4526 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4527 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4528 size, big_endian> Reloc_section;
c9269dff 4529
ec661b9d
AM
4530 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4531 Reloc_section* brlt_rel)
4532 : Output_section_data_build(size == 32 ? 4 : 8),
4533 rel_(brlt_rel),
4534 targ_(targ)
4535 { }
cf43a2fe 4536
06f30c9d
CC
4537 void
4538 reset_brlt_sizes()
4539 {
4540 this->reset_data_size();
4541 this->rel_->reset_data_size();
4542 }
4543
4544 void
4545 finalize_brlt_sizes()
4546 {
4547 this->finalize_data_size();
4548 this->rel_->finalize_data_size();
4549 }
4550
ec661b9d 4551 // Add a reloc for an entry in the BRLT.
cf43a2fe 4552 void
ec661b9d
AM
4553 add_reloc(Address to, unsigned int off)
4554 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 4555
ec661b9d 4556 // Update section and reloc section size.
e5d5f5ed 4557 void
ec661b9d
AM
4558 set_current_size(unsigned int num_branches)
4559 {
4560 this->reset_address_and_file_offset();
4561 this->set_current_data_size(num_branches * 16);
4562 this->finalize_data_size();
4563 Output_section* os = this->output_section();
4564 os->set_section_offsets_need_adjustment();
4565 if (this->rel_ != NULL)
4566 {
0e123f69 4567 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
4568 this->rel_->reset_address_and_file_offset();
4569 this->rel_->set_current_data_size(num_branches * reloc_size);
4570 this->rel_->finalize_data_size();
4571 Output_section* os = this->rel_->output_section();
4572 os->set_section_offsets_need_adjustment();
4573 }
4574 }
cf43a2fe 4575
ec661b9d
AM
4576 protected:
4577 void
4578 do_adjust_output_section(Output_section* os)
4579 {
4580 os->set_entsize(0);
4581 }
e5d5f5ed 4582
ec661b9d
AM
4583 // Write to a map file.
4584 void
4585 do_print_to_mapfile(Mapfile* mapfile) const
4586 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4587
ec661b9d
AM
4588 private:
4589 // Write out the BRLT data.
4590 void
4591 do_write(Output_file*);
c9824451 4592
ec661b9d
AM
4593 // The reloc section.
4594 Reloc_section* rel_;
4595 Target_powerpc<size, big_endian>* targ_;
4596};
cf43a2fe 4597
ec661b9d
AM
4598// Make the branch lookup table section.
4599
4600template<int size, bool big_endian>
4601void
4602Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4603{
4604 if (size == 64 && this->brlt_section_ == NULL)
4605 {
4606 Reloc_section* brlt_rel = NULL;
4607 bool is_pic = parameters->options().output_is_position_independent();
4608 if (is_pic)
4609 {
54483898
AM
4610 // When PIC we can't fill in .branch_lt but must initialise at
4611 // runtime via dynamic relocations.
ec661b9d
AM
4612 this->rela_dyn_section(layout);
4613 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4614 if (this->rela_dyn_->output_section())
4615 this->rela_dyn_->output_section()
4616 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4617 }
4618 this->brlt_section_
4619 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4620 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4621 this->plt_->output_section()
4622 ->add_output_section_data(this->brlt_section_);
4623 else
6830ee24 4624 layout->add_output_section_data(".branch_lt",
54483898 4625 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4626 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4627 this->brlt_section_,
54483898
AM
4628 ORDER_RELRO,
4629 true);
ec661b9d
AM
4630 }
4631}
4632
6830ee24 4633// Write out .branch_lt when non-PIC.
ec661b9d
AM
4634
4635template<int size, bool big_endian>
4636void
4637Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4638{
4639 if (size == 64 && !parameters->options().output_is_position_independent())
4640 {
4641 const section_size_type offset = this->offset();
4642 const section_size_type oview_size
4643 = convert_to_section_size_type(this->data_size());
4644 unsigned char* const oview = of->get_output_view(offset, oview_size);
4645
4646 this->targ_->write_branch_lookup_table(oview);
4647 of->write_output_view(offset, oview_size, oview);
4648 }
4649}
4650
9e69ed50
AM
4651static inline uint32_t
4652l(uint32_t a)
4653{
4654 return a & 0xffff;
4655}
4656
4657static inline uint32_t
4658hi(uint32_t a)
4659{
4660 return l(a >> 16);
4661}
4662
4663static inline uint32_t
4664ha(uint32_t a)
4665{
4666 return hi(a + 0x8000);
4667}
4668
e4dff765
AM
4669static inline uint64_t
4670d34(uint64_t v)
4671{
4672 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4673}
4674
4675static inline uint64_t
4676ha34(uint64_t v)
4677{
4678 return (v + (1ULL << 33)) >> 34;
4679}
4680
9d5781f8
AM
4681template<int size>
4682struct Eh_cie
4683{
4684 static const unsigned char eh_frame_cie[12];
4685};
4686
4687template<int size>
4688const unsigned char Eh_cie<size>::eh_frame_cie[] =
4689{
4690 1, // CIE version.
4691 'z', 'R', 0, // Augmentation string.
4692 4, // Code alignment.
4693 0x80 - size / 8 , // Data alignment.
4694 65, // RA reg.
4695 1, // Augmentation size.
4696 (elfcpp::DW_EH_PE_pcrel
4697 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4698 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4699};
4700
b4f7960d
AM
4701// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4702static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4703{
4704 0, 0, 0, 0, // Replaced with offset to .glink.
4705 0, 0, 0, 0, // Replaced with size of .glink.
4706 0, // Augmentation size.
63e5eea2 4707 elfcpp::DW_CFA_advance_loc + 2,
9d5781f8 4708 elfcpp::DW_CFA_register, 65, 12,
63e5eea2 4709 elfcpp::DW_CFA_advance_loc + 4,
9d5781f8
AM
4710 elfcpp::DW_CFA_restore_extended, 65
4711};
4712
b4f7960d
AM
4713// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4714static const unsigned char glink_eh_frame_fde_64v2[] =
4715{
4716 0, 0, 0, 0, // Replaced with offset to .glink.
4717 0, 0, 0, 0, // Replaced with size of .glink.
4718 0, // Augmentation size.
63e5eea2 4719 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d 4720 elfcpp::DW_CFA_register, 65, 0,
63e5eea2
AM
4721 elfcpp::DW_CFA_advance_loc + 2,
4722 elfcpp::DW_CFA_restore_extended, 65
4723};
4724
4725static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4726{
4727 0, 0, 0, 0, // Replaced with offset to .glink.
4728 0, 0, 0, 0, // Replaced with size of .glink.
4729 0, // Augmentation size.
4730 elfcpp::DW_CFA_advance_loc + 3,
4731 elfcpp::DW_CFA_register, 65, 0,
4732 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d
AM
4733 elfcpp::DW_CFA_restore_extended, 65
4734};
4735
9d5781f8
AM
4736// Describe __glink_PLTresolve use of LR, 32-bit version.
4737static const unsigned char glink_eh_frame_fde_32[] =
4738{
4739 0, 0, 0, 0, // Replaced with offset to .glink.
4740 0, 0, 0, 0, // Replaced with size of .glink.
4741 0, // Augmentation size.
4742 elfcpp::DW_CFA_advance_loc + 2,
4743 elfcpp::DW_CFA_register, 65, 0,
4744 elfcpp::DW_CFA_advance_loc + 4,
4745 elfcpp::DW_CFA_restore_extended, 65
4746};
4747
4748static const unsigned char default_fde[] =
4749{
4750 0, 0, 0, 0, // Replaced with offset to stubs.
4751 0, 0, 0, 0, // Replaced with size of stubs.
4752 0, // Augmentation size.
4753 elfcpp::DW_CFA_nop, // Pad.
4754 elfcpp::DW_CFA_nop,
4755 elfcpp::DW_CFA_nop
4756};
4757
9e69ed50
AM
4758template<bool big_endian>
4759static inline void
4760write_insn(unsigned char* p, uint32_t v)
4761{
4762 elfcpp::Swap<32, big_endian>::writeval(p, v);
4763}
4764
691d2e9a
AM
4765template<int size>
4766static inline unsigned int
4767param_plt_align()
4768{
4769 if (!parameters->options().user_set_plt_align())
4770 return size == 64 ? 32 : 8;
4771 return 1 << parameters->options().plt_align();
4772}
4773
ec661b9d
AM
4774// Stub_table holds information about plt and long branch stubs.
4775// Stubs are built in an area following some input section determined
4776// by group_sections(). This input section is converted to a relaxed
4777// input section allowing it to be resized to accommodate the stubs
4778
4779template<int size, bool big_endian>
4780class Stub_table : public Output_relaxed_input_section
4781{
4782 public:
7e57d19e
AM
4783 struct Plt_stub_ent
4784 {
4785 Plt_stub_ent(unsigned int off, unsigned int indx)
eb97d024
AM
4786 : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4787 toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
7e57d19e
AM
4788 { }
4789
4790 unsigned int off_;
afd2ea23 4791 unsigned int indx_;
eb97d024
AM
4792 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4793 // toc stub, p9off_ is offset to p9notoc stub
4794 unsigned int tocoff_ : 8;
4795 unsigned int p9off_ : 8;
4796 // The size of the toc stub, used to locate blr on tls_get_addr stub.
4797 unsigned int tsize_ : 8;
4798 // Stub revision management
32f59844 4799 unsigned int iter_ : 1;
eb97d024 4800 // The three types of stubs.
afd2ea23 4801 unsigned int toc_ : 1;
eb97d024
AM
4802 unsigned int notoc_ : 1;
4803 unsigned int p9notoc_ : 1;
4804 // Each with a possible variant saving r2 first
7e57d19e 4805 unsigned int r2save_ : 1;
eb97d024 4806 // Handy cached info from symbol
7ee7ff70 4807 unsigned int localentry0_ : 1;
7e57d19e 4808 };
32f59844
AM
4809 struct Branch_stub_ent
4810 {
eb97d024
AM
4811 Branch_stub_ent(unsigned int off)
4812 : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4813 p9notoc_(0), save_res_(0), other_(0)
32f59844
AM
4814 { }
4815
4816 unsigned int off_;
eb97d024
AM
4817 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4818 // toc stub, p9off_ is offset to p9notoc stub
4819 unsigned int tocoff_ : 8;
4820 unsigned int p9off_ : 8;
4821 // Stub revision management
afd2ea23 4822 unsigned int iter_ : 1;
eb97d024 4823 // Four types of stubs.
afd2ea23 4824 unsigned int toc_ : 1;
eb97d024
AM
4825 unsigned int notoc_ : 1;
4826 unsigned int p9notoc_ : 1;
afd2ea23 4827 unsigned int save_res_ : 1;
eb97d024 4828 // Handy cached info from symbol
fa40fbe4 4829 unsigned int other_ : 3;
32f59844 4830 };
ec661b9d
AM
4831 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4832 static const Address invalid_address = static_cast<Address>(0) - 1;
4833
a3e60ddb
AM
4834 Stub_table(Target_powerpc<size, big_endian>* targ,
4835 Output_section* output_section,
590b87ff
AM
4836 const Output_section::Input_section* owner,
4837 uint32_t id)
a3e60ddb
AM
4838 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4839 owner->relobj()
4840 ->section_addralign(owner->shndx())),
ec661b9d 4841 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4842 orig_data_size_(owner->current_data_size()),
4843 plt_size_(0), last_plt_size_(0),
6395d38b 4844 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4845 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4846 uniq_(id)
a3e60ddb
AM
4847 {
4848 this->set_output_section(output_section);
ec661b9d 4849
a3e60ddb
AM
4850 std::vector<Output_relaxed_input_section*> new_relaxed;
4851 new_relaxed.push_back(this);
4852 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4853 }
ec661b9d
AM
4854
4855 // Add a plt call stub.
a3e60ddb
AM
4856 bool
4857 add_plt_call_entry(Address,
4858 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4859 const Symbol*,
4860 unsigned int,
7e57d19e
AM
4861 Address,
4862 bool);
ec661b9d 4863
a3e60ddb
AM
4864 bool
4865 add_plt_call_entry(Address,
4866 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4867 unsigned int,
4868 unsigned int,
7e57d19e
AM
4869 Address,
4870 bool);
ec661b9d
AM
4871
4872 // Find a given plt call stub.
7e57d19e 4873 const Plt_stub_ent*
ec661b9d
AM
4874 find_plt_call_entry(const Symbol*) const;
4875
7e57d19e 4876 const Plt_stub_ent*
ec661b9d
AM
4877 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4878 unsigned int) const;
4879
7e57d19e 4880 const Plt_stub_ent*
ec661b9d
AM
4881 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4882 const Symbol*,
4883 unsigned int,
4884 Address) const;
4885
7e57d19e 4886 const Plt_stub_ent*
ec661b9d
AM
4887 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4888 unsigned int,
4889 unsigned int,
4890 Address) const;
4891
4892 // Add a long branch stub.
a3e60ddb 4893 bool
a19da04b 4894 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
ec661b9d 4895
32f59844 4896 const Branch_stub_ent*
a19da04b 4897 find_long_branch_entry(Address) const;
ec661b9d 4898
a3e60ddb
AM
4899 bool
4900 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4901 {
32f59844 4902 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4903 if (max_branch_offset == 0)
4904 return true;
4905 gold_assert(from != invalid_address);
4906 Address loc = off + this->stub_address();
4907 return loc - from + max_branch_offset < 2 * max_branch_offset;
4908 }
4909
ec661b9d 4910 void
a3e60ddb 4911 clear_stubs(bool all)
cf43a2fe 4912 {
9e69ed50
AM
4913 this->plt_call_stubs_.clear();
4914 this->plt_size_ = 0;
ec661b9d
AM
4915 this->long_branch_stubs_.clear();
4916 this->branch_size_ = 0;
d49044c7 4917 this->need_save_res_ = false;
a3e60ddb
AM
4918 if (all)
4919 {
4920 this->last_plt_size_ = 0;
4921 this->last_branch_size_ = 0;
4922 }
cf43a2fe
AM
4923 }
4924
32f59844
AM
4925 bool
4926 need_resize() const
4927 { return need_resize_; }
4928
4929 void
4930 set_resizing(bool val)
4931 {
4932 this->resizing_ = val;
4933 if (val)
4934 {
4935 this->need_resize_ = false;
4936 this->plt_size_ = 0;
4937 this->branch_size_ = 0;
4938 this->need_save_res_ = false;
4939 }
4940 }
4941
ec661b9d
AM
4942 Address
4943 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4944 {
ec661b9d
AM
4945 Address start_off = off;
4946 off += this->orig_data_size_;
4947 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4948 if (this->need_save_res_)
4949 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4950 if (my_size != 0)
4951 off = align_address(off, this->stub_align());
4952 // Include original section size and alignment padding in size
4953 my_size += off - start_off;
6395d38b
HS
4954 // Ensure new size is always larger than min size
4955 // threshold. Alignment requirement is included in "my_size", so
4956 // increase "my_size" does not invalidate alignment.
4957 if (my_size < this->min_size_threshold_)
4958 my_size = this->min_size_threshold_;
ec661b9d
AM
4959 this->reset_address_and_file_offset();
4960 this->set_current_data_size(my_size);
4961 this->set_address_and_file_offset(os->address() + start_off,
4962 os->offset() + start_off);
4963 return my_size;
cf43a2fe
AM
4964 }
4965
ec661b9d 4966 Address
9d5781f8 4967 stub_address() const
ec661b9d
AM
4968 {
4969 return align_address(this->address() + this->orig_data_size_,
4970 this->stub_align());
4971 }
4972
4973 Address
9d5781f8 4974 stub_offset() const
ec661b9d
AM
4975 {
4976 return align_address(this->offset() + this->orig_data_size_,
4977 this->stub_align());
4978 }
4979
4980 section_size_type
4981 plt_size() const
4982 { return this->plt_size_; }
4983
32f59844
AM
4984 section_size_type
4985 branch_size() const
4986 { return this->branch_size_; }
4987
590b87ff
AM
4988 void
4989 set_min_size_threshold(Address min_size)
6395d38b
HS
4990 { this->min_size_threshold_ = min_size; }
4991
590b87ff
AM
4992 void
4993 define_stub_syms(Symbol_table*);
4994
ec661b9d
AM
4995 bool
4996 size_update()
4997 {
4998 Output_section* os = this->output_section();
4999 if (os->addralign() < this->stub_align())
5000 {
5001 os->set_addralign(this->stub_align());
5002 // FIXME: get rid of the insane checkpointing.
5003 // We can't increase alignment of the input section to which
5004 // stubs are attached; The input section may be .init which
5005 // is pasted together with other .init sections to form a
5006 // function. Aligning might insert zero padding resulting in
5007 // sigill. However we do need to increase alignment of the
5008 // output section so that the align_address() on offset in
5009 // set_address_and_size() adds the same padding as the
5010 // align_address() on address in stub_address().
5011 // What's more, we need this alignment for the layout done in
5012 // relaxation_loop_body() so that the output section starts at
5013 // a suitably aligned address.
5014 os->checkpoint_set_addralign(this->stub_align());
5015 }
9e69ed50
AM
5016 if (this->last_plt_size_ != this->plt_size_
5017 || this->last_branch_size_ != this->branch_size_)
ec661b9d 5018 {
9e69ed50
AM
5019 this->last_plt_size_ = this->plt_size_;
5020 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
5021 return true;
5022 }
5023 return false;
5024 }
5025
34e0882b
AM
5026 // Add .eh_frame info for this stub section.
5027 void
5028 add_eh_frame(Layout* layout);
be897fb7 5029
34e0882b 5030 // Remove .eh_frame info for this stub section.
be897fb7 5031 void
34e0882b 5032 remove_eh_frame(Layout* layout);
9d5781f8 5033
ec661b9d
AM
5034 Target_powerpc<size, big_endian>*
5035 targ() const
5036 { return targ_; }
6ce78956 5037
cf43a2fe 5038 private:
bdab445c
AM
5039 class Plt_stub_key;
5040 class Plt_stub_key_hash;
bdab445c
AM
5041 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5042 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
5043 class Branch_stub_key;
5044 class Branch_stub_key_hash;
5045 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5046 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
5047
5048 // Alignment of stub section.
ec661b9d 5049 unsigned int
9e69ed50
AM
5050 stub_align() const
5051 {
691d2e9a 5052 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
5053 unsigned int user_align = 1 << parameters->options().plt_align();
5054 return std::max(user_align, min_align);
5055 }
cf43a2fe 5056
91c2b899
AM
5057 // Return the plt offset for the given call stub.
5058 Address
08be3224
AM
5059 plt_off(typename Plt_stub_entries::const_iterator p,
5060 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
5061 {
5062 const Symbol* gsym = p->first.sym_;
5063 if (gsym != NULL)
08be3224 5064 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
5065 else
5066 {
91c2b899
AM
5067 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5068 unsigned int local_sym_index = p->first.locsym_;
08be3224 5069 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
5070 }
5071 }
5072
9e69ed50 5073 // Size of a given plt call stub.
ec661b9d 5074 unsigned int
afd2ea23 5075 plt_call_size(typename Plt_stub_entries::iterator p) const;
34e0882b
AM
5076
5077 unsigned int
5078 plt_call_align(unsigned int bytes) const
5079 {
691d2e9a 5080 unsigned int align = param_plt_align<size>();
9e390558 5081 return (bytes + align - 1) & -align;
9e69ed50 5082 }
ec661b9d
AM
5083
5084 // Return long branch stub size.
5085 unsigned int
afd2ea23 5086 branch_stub_size(typename Branch_stub_entries::iterator p,
32f59844
AM
5087 bool* need_lt);
5088
afd2ea23
AM
5089 void
5090 build_tls_opt_head(unsigned char** pp, bool save_lr);
32f59844 5091
afd2ea23
AM
5092 void
5093 build_tls_opt_tail(unsigned char* p);
ec661b9d 5094
f073a3e8
AM
5095 void
5096 plt_error(const Plt_stub_key& p);
5097
ec661b9d 5098 // Write out stubs.
cf43a2fe
AM
5099 void
5100 do_write(Output_file*);
5101
ec661b9d 5102 // Plt call stub keys.
bdab445c 5103 class Plt_stub_key
cf43a2fe 5104 {
d1a8cabd 5105 public:
bdab445c 5106 Plt_stub_key(const Symbol* sym)
c9824451
AM
5107 : sym_(sym), object_(0), addend_(0), locsym_(0)
5108 { }
5109
bdab445c 5110 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 5111 unsigned int locsym_index)
c9824451
AM
5112 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5113 { }
5114
bdab445c 5115 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5116 const Symbol* sym,
5117 unsigned int r_type,
5118 Address addend)
e5d5f5ed 5119 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
5120 {
5121 if (size != 32)
ec661b9d 5122 this->addend_ = addend;
d1a8cabd 5123 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5124 && (r_type == elfcpp::R_PPC_PLTREL24
5125 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 5126 {
ec661b9d 5127 this->addend_ = addend;
e5d5f5ed 5128 if (this->addend_ >= 32768)
d1a8cabd 5129 this->object_ = object;
cf43a2fe
AM
5130 }
5131 }
5132
bdab445c 5133 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5134 unsigned int locsym_index,
5135 unsigned int r_type,
5136 Address addend)
e5d5f5ed
AM
5137 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5138 {
5139 if (size != 32)
ec661b9d 5140 this->addend_ = addend;
e5d5f5ed 5141 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5142 && (r_type == elfcpp::R_PPC_PLTREL24
5143 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 5144 this->addend_ = addend;
e5d5f5ed
AM
5145 }
5146
bdab445c 5147 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
5148 {
5149 return (this->sym_ == that.sym_
5150 && this->object_ == that.object_
e5d5f5ed
AM
5151 && this->addend_ == that.addend_
5152 && this->locsym_ == that.locsym_);
cf43a2fe 5153 }
c9269dff
AM
5154
5155 const Symbol* sym_;
e5d5f5ed
AM
5156 const Sized_relobj_file<size, big_endian>* object_;
5157 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5158 unsigned int locsym_;
cf43a2fe
AM
5159 };
5160
bdab445c 5161 class Plt_stub_key_hash
cf43a2fe 5162 {
d1a8cabd 5163 public:
bdab445c 5164 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
5165 {
5166 return (reinterpret_cast<uintptr_t>(ent.sym_)
5167 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
5168 ^ ent.addend_
5169 ^ ent.locsym_);
cf43a2fe 5170 }
ec661b9d
AM
5171 };
5172
5173 // Long branch stub keys.
32f59844 5174 class Branch_stub_key
ec661b9d
AM
5175 {
5176 public:
a19da04b
AM
5177 Branch_stub_key(Address to)
5178 : dest_(to)
5179 { }
ec661b9d 5180
32f59844 5181 bool operator==(const Branch_stub_key& that) const
ec661b9d 5182 {
a19da04b 5183 return this->dest_ == that.dest_;
ec661b9d 5184 }
cf43a2fe 5185
ec661b9d 5186 Address dest_;
ec661b9d 5187 };
cf43a2fe 5188
32f59844 5189 class Branch_stub_key_hash
ec661b9d
AM
5190 {
5191 public:
32f59844 5192 size_t operator()(const Branch_stub_key& key) const
a19da04b 5193 { return key.dest_; }
ec661b9d 5194 };
cf43a2fe 5195
ec661b9d 5196 // In a sane world this would be a global.
cf43a2fe 5197 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5198 // Map sym/object/addend to stub offset.
ec661b9d
AM
5199 Plt_stub_entries plt_call_stubs_;
5200 // Map destination address to stub offset.
ec661b9d
AM
5201 Branch_stub_entries long_branch_stubs_;
5202 // size of input section
5203 section_size_type orig_data_size_;
5204 // size of stubs
9e69ed50 5205 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5206 // Some rare cases cause (PR/20529) fluctuation in stub table
5207 // size, which leads to an endless relax loop. This is to be fixed
5208 // by, after the first few iterations, allowing only increase of
5209 // stub table size. This variable sets the minimal possible size of
5210 // a stub table, it is zero for the first few iterations, then
5211 // increases monotonically.
5212 Address min_size_threshold_;
d49044c7
AM
5213 // Set if this stub group needs a copy of out-of-line register
5214 // save/restore functions.
5215 bool need_save_res_;
32f59844
AM
5216 // Set when notoc_/r2save_ changes after sizing a stub
5217 bool need_resize_;
5218 // Set when resizing stubs
5219 bool resizing_;
590b87ff
AM
5220 // Per stub table unique identifier.
5221 uint32_t uniq_;
cf43a2fe
AM
5222};
5223
ec661b9d 5224// Add a plt call stub, if we do not already have one for this
d1a8cabd 5225// sym/object/addend combo.
cf43a2fe
AM
5226
5227template<int size, bool big_endian>
a3e60ddb 5228bool
ec661b9d 5229Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5230 Address from,
c9824451 5231 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5232 const Symbol* gsym,
ec661b9d 5233 unsigned int r_type,
7e57d19e
AM
5234 Address addend,
5235 bool tocsave)
cf43a2fe 5236{
bdab445c
AM
5237 Plt_stub_key key(object, gsym, r_type, addend);
5238 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5239 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5240 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5241 if (size == 64)
7ee7ff70 5242 {
32f59844 5243 if (p.second
7ee7ff70
AM
5244 && this->targ_->is_elfv2_localentry0(gsym))
5245 {
5246 p.first->second.localentry0_ = 1;
5247 this->targ_->set_has_localentry0();
5248 }
eb97d024
AM
5249 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5250 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844 5251 {
eb97d024
AM
5252 if (this->targ_->power10_stubs()
5253 && (!this->targ_->power10_stubs_auto()
5254 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5255 {
5256 if (!p.second && !p.first->second.notoc_)
5257 this->need_resize_ = true;
5258 p.first->second.notoc_ = 1;
5259 }
5260 else
5261 {
5262 if (!p.second && !p.first->second.p9notoc_)
5263 this->need_resize_ = true;
5264 p.first->second.p9notoc_ = 1;
5265 }
32f59844 5266 }
afd2ea23 5267 else
32f59844 5268 {
afd2ea23 5269 if (!p.second && !p.first->second.toc_)
32f59844 5270 this->need_resize_ = true;
afd2ea23
AM
5271 p.first->second.toc_ = 1;
5272 if (!tocsave && !p.first->second.localentry0_)
5273 {
5274 if (!p.second && !p.first->second.r2save_)
5275 this->need_resize_ = true;
5276 p.first->second.r2save_ = 1;
5277 }
32f59844
AM
5278 }
5279 }
5280 if (p.second || (this->resizing_ && !p.first->second.iter_))
5281 {
5282 if (this->resizing_)
5283 {
5284 p.first->second.iter_ = 1;
5285 p.first->second.off_ = this->plt_size_;
5286 }
5287 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5288 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5289 this->targ_->set_has_tls_get_addr_opt();
7ee7ff70 5290 }
32f59844 5291 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5292}
5293
e5d5f5ed 5294template<int size, bool big_endian>
a3e60ddb 5295bool
ec661b9d 5296Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5297 Address from,
c9824451 5298 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5299 unsigned int locsym_index,
ec661b9d 5300 unsigned int r_type,
7e57d19e
AM
5301 Address addend,
5302 bool tocsave)
e5d5f5ed 5303{
bdab445c
AM
5304 Plt_stub_key key(object, locsym_index, r_type, addend);
5305 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5306 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5307 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5308 if (size == 64)
7ee7ff70 5309 {
32f59844 5310 if (p.second
7ee7ff70
AM
5311 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5312 {
5313 p.first->second.localentry0_ = 1;
5314 this->targ_->set_has_localentry0();
5315 }
eb97d024
AM
5316 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5317 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844 5318 {
eb97d024
AM
5319 if (this->targ_->power10_stubs()
5320 && (!this->targ_->power10_stubs_auto()
5321 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5322 {
5323 if (!p.second && !p.first->second.notoc_)
5324 this->need_resize_ = true;
5325 p.first->second.notoc_ = 1;
5326 }
5327 else
5328 {
5329 if (!p.second && !p.first->second.p9notoc_)
5330 this->need_resize_ = true;
5331 p.first->second.p9notoc_ = 1;
5332 }
32f59844 5333 }
afd2ea23 5334 else
32f59844 5335 {
afd2ea23 5336 if (!p.second && !p.first->second.toc_)
32f59844 5337 this->need_resize_ = true;
afd2ea23
AM
5338 p.first->second.toc_ = 1;
5339 if (!tocsave && !p.first->second.localentry0_)
5340 {
5341 if (!p.second && !p.first->second.r2save_)
5342 this->need_resize_ = true;
5343 p.first->second.r2save_ = 1;
5344 }
32f59844 5345 }
7ee7ff70 5346 }
32f59844
AM
5347 if (p.second || (this->resizing_ && !p.first->second.iter_))
5348 {
5349 if (this->resizing_)
5350 {
5351 p.first->second.iter_ = 1;
5352 p.first->second.off_ = this->plt_size_;
5353 }
5354 this->plt_size_ += this->plt_call_size(p.first);
32f59844
AM
5355 }
5356 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5357}
5358
ec661b9d
AM
5359// Find a plt call stub.
5360
cf43a2fe 5361template<int size, bool big_endian>
7e57d19e 5362const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5363Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5364 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5365 const Symbol* gsym,
ec661b9d
AM
5366 unsigned int r_type,
5367 Address addend) const
c9824451 5368{
bdab445c
AM
5369 Plt_stub_key key(object, gsym, r_type, addend);
5370 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5371 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5372 return NULL;
5373 return &p->second;
c9824451
AM
5374}
5375
5376template<int size, bool big_endian>
7e57d19e 5377const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5378Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5379{
bdab445c
AM
5380 Plt_stub_key key(gsym);
5381 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5382 if (p == this->plt_call_stubs_.end())
5383 return NULL;
5384 return &p->second;
cf43a2fe
AM
5385}
5386
e5d5f5ed 5387template<int size, bool big_endian>
7e57d19e 5388const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5389Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5390 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5391 unsigned int locsym_index,
ec661b9d
AM
5392 unsigned int r_type,
5393 Address addend) const
e5d5f5ed 5394{
bdab445c
AM
5395 Plt_stub_key key(object, locsym_index, r_type, addend);
5396 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5397 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5398 return NULL;
5399 return &p->second;
c9824451
AM
5400}
5401
5402template<int size, bool big_endian>
7e57d19e 5403const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5404Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5405 const Sized_relobj_file<size, big_endian>* object,
5406 unsigned int locsym_index) const
5407{
bdab445c
AM
5408 Plt_stub_key key(object, locsym_index);
5409 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5410 if (p == this->plt_call_stubs_.end())
5411 return NULL;
5412 return &p->second;
ec661b9d
AM
5413}
5414
5415// Add a long branch stub if we don't already have one to given
5416// destination.
5417
5418template<int size, bool big_endian>
a3e60ddb 5419bool
ec661b9d 5420Stub_table<size, big_endian>::add_long_branch_entry(
a3e60ddb
AM
5421 unsigned int r_type,
5422 Address from,
d49044c7 5423 Address to,
fa40fbe4 5424 unsigned int other,
d49044c7 5425 bool save_res)
ec661b9d 5426{
a19da04b 5427 Branch_stub_key key(to);
eb97d024 5428 Branch_stub_ent ent(this->branch_size_);
590b87ff 5429 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844 5430 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
eb97d024 5431 if (save_res)
ec661b9d 5432 {
eb97d024 5433 if (!p.second && !p.first->second.save_res_)
afd2ea23 5434 this->need_resize_ = true;
eb97d024
AM
5435 p.first->second.save_res_ = true;
5436 }
5437 else if (size == 64
5438 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5439 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5440 {
5441 if (this->targ_->power10_stubs()
5442 && (!this->targ_->power10_stubs_auto()
5443 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5444 {
5445 if (!p.second && !p.first->second.notoc_)
5446 this->need_resize_ = true;
5447 p.first->second.notoc_ = true;
5448 }
5449 else
5450 {
5451 if (!p.second && !p.first->second.p9notoc_)
5452 this->need_resize_ = true;
5453 p.first->second.p9notoc_ = true;
5454 }
32f59844 5455 }
afd2ea23
AM
5456 else
5457 {
5458 if (!p.second && !p.first->second.toc_)
5459 this->need_resize_ = true;
5460 p.first->second.toc_ = true;
5461 }
e3b53295 5462 if (size == 64 && p.first->second.other_ == 0)
fa40fbe4 5463 p.first->second.other_ = other;
32f59844
AM
5464 if (p.second || (this->resizing_ && !p.first->second.iter_))
5465 {
5466 if (this->resizing_)
5467 {
5468 p.first->second.iter_ = 1;
5469 p.first->second.off_ = this->branch_size_;
5470 }
d49044c7
AM
5471 if (save_res)
5472 this->need_save_res_ = true;
5473 else
5474 {
32f59844
AM
5475 bool need_lt = false;
5476 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5477 this->branch_size_ += stub_size;
5478 if (size == 64 && need_lt)
d49044c7
AM
5479 this->targ_->add_branch_lookup_table(to);
5480 }
ec661b9d 5481 }
32f59844 5482 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5483}
5484
d49044c7 5485// Find long branch stub offset.
ec661b9d
AM
5486
5487template<int size, bool big_endian>
32f59844 5488const typename Stub_table<size, big_endian>::Branch_stub_ent*
a19da04b 5489Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
ec661b9d 5490{
a19da04b 5491 Branch_stub_key key(to);
ec661b9d 5492 typename Branch_stub_entries::const_iterator p
32f59844 5493 = this->long_branch_stubs_.find(key);
d49044c7 5494 if (p == this->long_branch_stubs_.end())
32f59844
AM
5495 return NULL;
5496 return &p->second;
e5d5f5ed
AM
5497}
5498
220f9906
AM
5499template<bool big_endian>
5500static void
5501eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5502{
220f9906
AM
5503 delta /= 4;
5504 if (delta < 64)
5505 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5506 else if (delta < 256)
34e0882b 5507 {
220f9906
AM
5508 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5509 fde.push_back(delta);
5510 }
5511 else if (delta < 65536)
5512 {
5513 fde.resize(fde.size() + 3);
5514 unsigned char *p = &*fde.end() - 3;
5515 *p++ = elfcpp::DW_CFA_advance_loc2;
5516 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5517 }
5518 else
5519 {
5520 fde.resize(fde.size() + 5);
5521 unsigned char *p = &*fde.end() - 5;
5522 *p++ = elfcpp::DW_CFA_advance_loc4;
5523 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5524 }
220f9906
AM
5525}
5526
5527template<typename T>
5528static bool
5529stub_sort(T s1, T s2)
5530{
5531 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5532}
5533
5534// Add .eh_frame info for this stub section. Unlike other linker
5535// generated .eh_frame this is added late in the link, because we
5536// only want the .eh_frame info if this particular stub section is
5537// non-empty.
5538
5539template<int size, bool big_endian>
5540void
5541Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5542{
220f9906
AM
5543 if (size != 64
5544 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5545 return;
5546
5547 // Since we add stub .eh_frame info late, it must be placed
5548 // after all other linker generated .eh_frame info so that
5549 // merge mapping need not be updated for input sections.
5550 // There is no provision to use a different CIE to that used
5551 // by .glink.
5552 if (!this->targ_->has_glink())
5553 return;
5554
afd2ea23 5555 typedef typename Plt_stub_entries::iterator plt_iter;
220f9906
AM
5556 std::vector<plt_iter> calls;
5557 if (!this->plt_call_stubs_.empty())
5558 for (plt_iter cs = this->plt_call_stubs_.begin();
5559 cs != this->plt_call_stubs_.end();
5560 ++cs)
eb97d024
AM
5561 if (cs->second.p9notoc_
5562 || (cs->second.toc_
5563 && cs->second.r2save_
5564 && !cs->second.localentry0_
5565 && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
220f9906
AM
5566 calls.push_back(cs);
5567 if (calls.size() > 1)
5568 std::stable_sort(calls.begin(), calls.end(),
5569 stub_sort<plt_iter>);
5570
5571 typedef typename Branch_stub_entries::const_iterator branch_iter;
5572 std::vector<branch_iter> branches;
e4dff765 5573 if (!this->long_branch_stubs_.empty()
7c1f4227 5574 && !this->targ_->power10_stubs())
220f9906
AM
5575 for (branch_iter bs = this->long_branch_stubs_.begin();
5576 bs != this->long_branch_stubs_.end();
5577 ++bs)
5578 if (bs->second.notoc_)
5579 branches.push_back(bs);
5580 if (branches.size() > 1)
5581 std::stable_sort(branches.begin(), branches.end(),
5582 stub_sort<branch_iter>);
5583
5584 if (calls.empty() && branches.empty())
34e0882b
AM
5585 return;
5586
220f9906
AM
5587 unsigned int last_eh_loc = 0;
5588 // offset pcrel sdata4, size udata4, and augmentation size byte.
5589 std::vector<unsigned char> fde(9, 0);
5590
5591 for (unsigned int i = 0; i < calls.size(); i++)
5592 {
5593 plt_iter cs = calls[i];
5594 unsigned int off = cs->second.off_;
5595 // The __tls_get_addr_opt call stub needs to describe where
5596 // it saves LR, to support exceptions that might be thrown
5597 // from __tls_get_addr, and to support asynchronous exceptions.
5598 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5599 {
5600 off += 7 * 4;
eb97d024
AM
5601 if (cs->second.toc_
5602 && cs->second.r2save_
220f9906
AM
5603 && !cs->second.localentry0_)
5604 {
eb97d024 5605 off += cs->second.tocoff_ + 2 * 4;
220f9906
AM
5606 eh_advance<big_endian>(fde, off - last_eh_loc);
5607 fde.resize(fde.size() + 6);
5608 unsigned char* p = &*fde.end() - 6;
5609 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5610 *p++ = 65;
5611 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
eb97d024 5612 unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
220f9906
AM
5613 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5614 *p++ = elfcpp::DW_CFA_restore_extended;
5615 *p++ = 65;
5616 last_eh_loc = off + delta;
eb97d024 5617 off = cs->second.off_ + 7 * 4;
220f9906
AM
5618 }
5619 }
5620 // notoc stubs also should describe LR changes, to support
5621 // asynchronous exceptions.
eb97d024
AM
5622 if (cs->second.p9notoc_)
5623 {
5624 off += cs->second.p9off_;
5625 off += (cs->second.r2save_ ? 4 : 0) + 8;
5626 eh_advance<big_endian>(fde, off - last_eh_loc);
5627 fde.resize(fde.size() + 6);
5628 unsigned char* p = &*fde.end() - 6;
5629 *p++ = elfcpp::DW_CFA_register;
5630 *p++ = 65;
5631 *p++ = 12;
5632 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5633 *p++ = elfcpp::DW_CFA_restore_extended;
5634 *p++ = 65;
5635 last_eh_loc = off + 8;
5636 }
220f9906
AM
5637 }
5638
5639 for (unsigned int i = 0; i < branches.size(); i++)
5640 {
5641 branch_iter bs = branches[i];
5642 unsigned int off = bs->second.off_ + 8;
5643 eh_advance<big_endian>(fde, off - last_eh_loc);
5644 fde.resize(fde.size() + 6);
5645 unsigned char* p = &*fde.end() - 6;
5646 *p++ = elfcpp::DW_CFA_register;
5647 *p++ = 65;
5648 *p++ = 12;
5649 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5650 *p++ = elfcpp::DW_CFA_restore_extended;
5651 *p++ = 65;
5652 last_eh_loc = off + 8;
5653 }
5654
34e0882b
AM
5655 layout->add_eh_frame_for_plt(this,
5656 Eh_cie<size>::eh_frame_cie,
5657 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5658 &*fde.begin(), fde.size());
34e0882b
AM
5659}
5660
5661template<int size, bool big_endian>
5662void
5663Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5664{
220f9906
AM
5665 if (size == 64
5666 && parameters->options().ld_generated_unwind_info()
5667 && this->targ_->has_glink())
5668 layout->remove_eh_frame_for_plt(this,
5669 Eh_cie<size>::eh_frame_cie,
5670 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5671}
5672
ec661b9d
AM
5673// A class to handle .glink.
5674
5675template<int size, bool big_endian>
5676class Output_data_glink : public Output_section_data
5677{
5678 public:
9055360d
AM
5679 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5680 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5681
5682 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5683 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5684 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5685 { }
5686
9d5781f8 5687 void
9055360d 5688 add_eh_frame(Layout* layout);
9d5781f8 5689
9055360d
AM
5690 void
5691 add_global_entry(const Symbol*);
5692
5693 Address
5694 find_global_entry(const Symbol*) const;
5695
9e390558
AM
5696 unsigned int
5697 global_entry_align(unsigned int off) const
5698 {
691d2e9a 5699 unsigned int align = param_plt_align<size>();
9e390558
AM
5700 return (off + align - 1) & -align;
5701 }
5702
5703 unsigned int
5704 global_entry_off() const
5705 {
5706 return this->global_entry_align(this->end_branch_table_);
5707 }
5708
9055360d
AM
5709 Address
5710 global_entry_address() const
5711 {
5712 gold_assert(this->is_data_size_valid());
9e390558
AM
5713 return this->address() + this->global_entry_off();
5714 }
5715
5716 int
5717 pltresolve_size() const
5718 {
5719 if (size == 64)
5720 return (8
63e5eea2
AM
5721 + (this->targ_->abiversion() < 2 ? 11 * 4
5722 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
9e390558 5723 return 16 * 4;
9d5781f8
AM
5724 }
5725
ec661b9d
AM
5726 protected:
5727 // Write to a map file.
5728 void
5729 do_print_to_mapfile(Mapfile* mapfile) const
5730 { mapfile->print_output_data(this, _("** glink")); }
5731
5732 private:
5733 void
5734 set_final_data_size();
5735
5736 // Write out .glink
5737 void
5738 do_write(Output_file*);
5739
5740 // Allows access to .got and .plt for do_write.
5741 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5742
5743 // Map sym to stub offset.
5744 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5745 Global_entry_stub_entries global_entry_stubs_;
5746
5747 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5748};
5749
9055360d
AM
5750template<int size, bool big_endian>
5751void
5752Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5753{
5754 if (!parameters->options().ld_generated_unwind_info())
5755 return;
5756
5757 if (size == 64)
5758 {
5759 if (this->targ_->abiversion() < 2)
5760 layout->add_eh_frame_for_plt(this,
5761 Eh_cie<64>::eh_frame_cie,
5762 sizeof (Eh_cie<64>::eh_frame_cie),
5763 glink_eh_frame_fde_64v1,
5764 sizeof (glink_eh_frame_fde_64v1));
63e5eea2
AM
5765 else if (this->targ_->has_localentry0())
5766 layout->add_eh_frame_for_plt(this,
5767 Eh_cie<64>::eh_frame_cie,
5768 sizeof (Eh_cie<64>::eh_frame_cie),
5769 glink_eh_frame_fde_64v2_localentry0,
5770 sizeof (glink_eh_frame_fde_64v2));
9055360d
AM
5771 else
5772 layout->add_eh_frame_for_plt(this,
5773 Eh_cie<64>::eh_frame_cie,
5774 sizeof (Eh_cie<64>::eh_frame_cie),
5775 glink_eh_frame_fde_64v2,
5776 sizeof (glink_eh_frame_fde_64v2));
5777 }
5778 else
5779 {
5780 // 32-bit .glink can use the default since the CIE return
5781 // address reg, LR, is valid.
5782 layout->add_eh_frame_for_plt(this,
5783 Eh_cie<32>::eh_frame_cie,
5784 sizeof (Eh_cie<32>::eh_frame_cie),
5785 default_fde,
5786 sizeof (default_fde));
5787 // Except where LR is used in a PIC __glink_PLTresolve.
5788 if (parameters->options().output_is_position_independent())
5789 layout->add_eh_frame_for_plt(this,
5790 Eh_cie<32>::eh_frame_cie,
5791 sizeof (Eh_cie<32>::eh_frame_cie),
5792 glink_eh_frame_fde_32,
5793 sizeof (glink_eh_frame_fde_32));
5794 }
5795}
5796
5797template<int size, bool big_endian>
5798void
5799Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5800{
9e390558 5801 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5802 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5803 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5804 if (p.second)
407aa07c 5805 this->ge_size_ = off + 16;
9055360d
AM
5806}
5807
5808template<int size, bool big_endian>
5809typename Output_data_glink<size, big_endian>::Address
5810Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5811{
5812 typename Global_entry_stub_entries::const_iterator p
5813 = this->global_entry_stubs_.find(gsym);
5814 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5815}
5816
cf43a2fe
AM
5817template<int size, bool big_endian>
5818void
5819Output_data_glink<size, big_endian>::set_final_data_size()
5820{
ec661b9d
AM
5821 unsigned int count = this->targ_->plt_entry_count();
5822 section_size_type total = 0;
cf43a2fe
AM
5823
5824 if (count != 0)
5825 {
5826 if (size == 32)
5827 {
cf43a2fe
AM
5828 // space for branch table
5829 total += 4 * (count - 1);
5830
5831 total += -total & 15;
9e390558 5832 total += this->pltresolve_size();
cf43a2fe
AM
5833 }
5834 else
5835 {
9e390558 5836 total += this->pltresolve_size();
cf43a2fe
AM
5837
5838 // space for branch table
b4f7960d
AM
5839 total += 4 * count;
5840 if (this->targ_->abiversion() < 2)
5841 {
5842 total += 4 * count;
5843 if (count > 0x8000)
5844 total += 4 * (count - 0x8000);
5845 }
cf43a2fe
AM
5846 }
5847 }
9055360d 5848 this->end_branch_table_ = total;
9e390558 5849 total = this->global_entry_align(total);
9055360d 5850 total += this->ge_size_;
cf43a2fe
AM
5851
5852 this->set_data_size(total);
5853}
5854
590b87ff
AM
5855// Define symbols on stubs, identifying the stub.
5856
5857template<int size, bool big_endian>
5858void
5859Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5860{
5861 if (!this->plt_call_stubs_.empty())
5862 {
5863 // The key for the plt call stub hash table includes addresses,
5864 // therefore traversal order depends on those addresses, which
5865 // can change between runs if gold is a PIE. Unfortunately the
5866 // output .symtab ordering depends on the order in which symbols
5867 // are added to the linker symtab. We want reproducible output
5868 // so must sort the call stub symbols.
afd2ea23 5869 typedef typename Plt_stub_entries::iterator plt_iter;
590b87ff
AM
5870 std::vector<plt_iter> sorted;
5871 sorted.resize(this->plt_call_stubs_.size());
5872
5873 for (plt_iter cs = this->plt_call_stubs_.begin();
5874 cs != this->plt_call_stubs_.end();
5875 ++cs)
bdab445c 5876 sorted[cs->second.indx_] = cs;
590b87ff
AM
5877
5878 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5879 {
5880 plt_iter cs = sorted[i];
5881 char add[10];
5882 add[0] = 0;
5883 if (cs->first.addend_ != 0)
5884 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5885 char obj[10];
5886 obj[0] = 0;
5887 if (cs->first.object_)
590b87ff
AM
5888 {
5889 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5890 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5891 sprintf(obj, "%x:", ppcobj->uniq());
5892 }
5893 char localname[9];
5894 const char *symname;
5895 if (cs->first.sym_ == NULL)
5896 {
5897 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5898 symname = localname;
5899 }
34e0882b
AM
5900 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5901 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5902 else
5903 symname = cs->first.sym_->name();
94de2a2c
JC
5904 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5905 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5906 Address value
5907 = this->stub_address() - this->address() + cs->second.off_;
eb97d024 5908 unsigned int stub_size = this->plt_call_size(cs);
590b87ff
AM
5909 this->targ_->define_local(symtab, name, this, value, stub_size);
5910 }
5911 }
5912
afd2ea23 5913 typedef typename Branch_stub_entries::iterator branch_iter;
590b87ff
AM
5914 for (branch_iter bs = this->long_branch_stubs_.begin();
5915 bs != this->long_branch_stubs_.end();
5916 ++bs)
5917 {
32f59844 5918 if (bs->second.save_res_)
590b87ff
AM
5919 continue;
5920
5921 char* name = new char[8 + 13 + 16 + 1];
5922 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5923 static_cast<unsigned long long>(bs->first.dest_));
5924 Address value = (this->stub_address() - this->address()
32f59844
AM
5925 + this->plt_size_ + bs->second.off_);
5926 bool need_lt = false;
5927 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5928 this->targ_->define_local(symtab, name, this, value, stub_size);
5929 }
5930}
5931
32f59844
AM
5932// Emit the start of a __tls_get_addr_opt plt call stub.
5933
5934template<int size, bool big_endian>
afd2ea23
AM
5935void
5936Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5937 bool save_lr)
32f59844 5938{
afd2ea23
AM
5939 unsigned char* p = *pp;
5940 if (size == 64)
32f59844 5941 {
afd2ea23
AM
5942 write_insn<big_endian>(p, ld_11_3 + 0);
5943 p += 4;
5944 write_insn<big_endian>(p, ld_12_3 + 8);
5945 p += 4;
5946 write_insn<big_endian>(p, mr_0_3);
5947 p += 4;
5948 write_insn<big_endian>(p, cmpdi_11_0);
5949 p += 4;
5950 write_insn<big_endian>(p, add_3_12_13);
5951 p += 4;
5952 write_insn<big_endian>(p, beqlr);
5953 p += 4;
5954 write_insn<big_endian>(p, mr_3_0);
5955 p += 4;
5956 if (save_lr)
32f59844 5957 {
afd2ea23 5958 write_insn<big_endian>(p, mflr_11);
32f59844 5959 p += 4;
afd2ea23 5960 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
32f59844
AM
5961 p += 4;
5962 }
32f59844 5963 }
afd2ea23 5964 else
32f59844 5965 {
afd2ea23 5966 write_insn<big_endian>(p, lwz_11_3 + 0);
32f59844 5967 p += 4;
afd2ea23 5968 write_insn<big_endian>(p, lwz_12_3 + 4);
32f59844 5969 p += 4;
afd2ea23 5970 write_insn<big_endian>(p, mr_0_3);
32f59844 5971 p += 4;
afd2ea23
AM
5972 write_insn<big_endian>(p, cmpwi_11_0);
5973 p += 4;
5974 write_insn<big_endian>(p, add_3_12_2);
5975 p += 4;
5976 write_insn<big_endian>(p, beqlr);
5977 p += 4;
5978 write_insn<big_endian>(p, mr_3_0);
5979 p += 4;
5980 write_insn<big_endian>(p, nop);
32f59844 5981 p += 4;
32f59844 5982 }
afd2ea23
AM
5983 *pp = p;
5984}
5985
5986// Emit the tail of a __tls_get_addr_opt plt call stub.
5987
5988template<int size, bool big_endian>
5989void
5990Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5991{
5992 write_insn<big_endian>(p, bctrl);
5993 p += 4;
5994 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5995 p += 4;
5996 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5997 p += 4;
5998 write_insn<big_endian>(p, mtlr_11);
5999 p += 4;
6000 write_insn<big_endian>(p, blr);
32f59844
AM
6001}
6002
e4dff765
AM
6003// Emit pc-relative plt call stub code.
6004
6005template<bool big_endian>
6006static unsigned char*
7c1f4227 6007build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
e4dff765
AM
6008{
6009 uint64_t insn;
6010 if (off - odd + (1ULL << 33) < 1ULL << 34)
6011 {
6012 off -= odd;
6013 if (odd)
6014 {
6015 write_insn<big_endian>(p, nop);
6016 p += 4;
6017 }
6018 if (load)
6019 insn = pld_12_pc;
6020 else
6021 insn = paddi_12_pc;
6022 insn |= d34(off);
6023 write_insn<big_endian>(p, insn >> 32);
6024 p += 4;
6025 write_insn<big_endian>(p, insn & 0xffffffff);
6026 }
6027 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6028 {
6029 off -= 8 - odd;
6030 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6031 p += 4;
6032 if (!odd)
6033 {
6034 write_insn<big_endian>(p, sldi_11_11_34);
6035 p += 4;
6036 }
6037 insn = paddi_12_pc | d34(off);
6038 write_insn<big_endian>(p, insn >> 32);
6039 p += 4;
6040 write_insn<big_endian>(p, insn & 0xffffffff);
6041 p += 4;
6042 if (odd)
6043 {
6044 write_insn<big_endian>(p, sldi_11_11_34);
6045 p += 4;
6046 }
6047 if (load)
6048 write_insn<big_endian>(p, ldx_12_11_12);
6049 else
6050 write_insn<big_endian>(p, add_12_11_12);
6051 }
6052 else
6053 {
6054 off -= odd + 8;
6055 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6056 p += 4;
6057 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6058 p += 4;
6059 if (odd)
6060 {
6061 write_insn<big_endian>(p, sldi_11_11_34);
6062 p += 4;
6063 }
6064 insn = paddi_12_pc | d34(off);
6065 write_insn<big_endian>(p, insn >> 32);
6066 p += 4;
6067 write_insn<big_endian>(p, insn & 0xffffffff);
6068 p += 4;
6069 if (!odd)
6070 {
6071 write_insn<big_endian>(p, sldi_11_11_34);
6072 p += 4;
6073 }
6074 if (load)
6075 write_insn<big_endian>(p, ldx_12_11_12);
6076 else
6077 write_insn<big_endian>(p, add_12_11_12);
6078 }
6079 p += 4;
6080 return p;
6081}
6082
32f59844
AM
6083// Gets the address of a label (1:) in r11 and builds an offset in r12,
6084// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6085// mflr %r12
6086// bcl 20,31,1f
6087// 1: mflr %r11
6088// mtlr %r12
6089// lis %r12,xxx-1b@highest
6090// ori %r12,%r12,xxx-1b@higher
6091// sldi %r12,%r12,32
6092// oris %r12,%r12,xxx-1b@high
6093// ori %r12,%r12,xxx-1b@l
6094// add/ldx %r12,%r11,%r12
6095
6096template<bool big_endian>
6097static unsigned char*
6098build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6099{
6100 write_insn<big_endian>(p, mflr_12);
6101 p += 4;
6102 write_insn<big_endian>(p, bcl_20_31);
6103 p += 4;
6104 write_insn<big_endian>(p, mflr_11);
6105 p += 4;
6106 write_insn<big_endian>(p, mtlr_12);
6107 p += 4;
6108 if (off + 0x8000 < 0x10000)
6109 {
6110 if (load)
6111 write_insn<big_endian>(p, ld_12_11 + l(off));
6112 else
6113 write_insn<big_endian>(p, addi_12_11 + l(off));
6114 }
6115 else if (off + 0x80008000ULL < 0x100000000ULL)
6116 {
6117 write_insn<big_endian>(p, addis_12_11 + ha(off));
6118 p += 4;
6119 if (load)
6120 write_insn<big_endian>(p, ld_12_12 + l(off));
6121 else
6122 write_insn<big_endian>(p, addi_12_12 + l(off));
6123 }
6124 else
6125 {
6126 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6127 {
6128 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6129 p += 4;
6130 }
6131 else
6132 {
6133 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6134 p += 4;
6135 if (((off >> 32) & 0xffff) != 0)
6136 {
6137 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6138 p += 4;
6139 }
6140 }
6141 if (((off >> 32) & 0xffffffffULL) != 0)
6142 {
6143 write_insn<big_endian>(p, sldi_12_12_32);
6144 p += 4;
6145 }
6146 if (hi(off) != 0)
6147 {
6148 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6149 p += 4;
6150 }
6151 if (l(off) != 0)
6152 {
6153 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6154 p += 4;
6155 }
6156 if (load)
6157 write_insn<big_endian>(p, ldx_12_11_12);
6158 else
6159 write_insn<big_endian>(p, add_12_11_12);
6160 }
6161 p += 4;
6162 return p;
6163}
6164
6165// Size of a given plt call stub.
6166
6167template<int size, bool big_endian>
6168unsigned int
6169Stub_table<size, big_endian>::plt_call_size(
afd2ea23 6170 typename Plt_stub_entries::iterator p) const
32f59844
AM
6171{
6172 if (size == 32)
6173 {
eb97d024
AM
6174 unsigned int bytes = 4 * 4;
6175 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6176 bytes = 12 * 4;
6177 return this->plt_call_align(bytes);
32f59844
AM
6178 }
6179
6180 const Output_data_plt_powerpc<size, big_endian>* plt;
6181 uint64_t plt_addr = this->plt_off(p, &plt);
6182 plt_addr += plt->address();
afd2ea23
AM
6183 if (this->targ_->power10_stubs()
6184 && this->targ_->power10_stubs_auto())
32f59844 6185 {
afd2ea23
AM
6186 unsigned int bytes = 0;
6187 if (p->second.notoc_)
6188 {
6189 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6190 bytes = 7 * 4;
6191 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6192 uint64_t odd = from & 4;
6193 uint64_t off = plt_addr - from;
6194 if (off - odd + (1ULL << 33) < 1ULL << 34)
6195 bytes += odd + 4 * 4;
6196 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6197 bytes += 7 * 4;
6198 else
6199 bytes += 8 * 4;
6200 bytes = this->plt_call_align(bytes);
6201 }
afd2ea23
AM
6202 if (p->second.toc_)
6203 {
6204 p->second.tocoff_ = bytes;
6205 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6206 {
6207 bytes += 7 * 4;
6208 if (p->second.r2save_ && !p->second.localentry0_)
eb97d024 6209 bytes += 2 * 4 + 4 * 4;
afd2ea23
AM
6210 }
6211 if (p->second.r2save_)
6212 bytes += 4;
a19da04b 6213 uint64_t got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6214 uint64_t off = plt_addr - got_addr;
6215 bytes += 3 * 4 + 4 * (ha(off) != 0);
eb97d024
AM
6216 p->second.tsize_ = bytes - p->second.tocoff_;
6217 bytes = this->plt_call_align(bytes);
afd2ea23 6218 }
eb97d024
AM
6219 if (p->second.p9notoc_)
6220 {
6221 p->second.p9off_ = bytes;
6222 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6223 bytes += 7 * 4;
6224 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6225 uint64_t off = plt_addr - from;
6226 if (off + 0x8000 < 0x10000)
6227 bytes += 7 * 4;
6228 else if (off + 0x80008000ULL < 0x100000000ULL)
6229 bytes += 8 * 4;
6230 else
6231 {
6232 bytes += 8 * 4;
6233 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6234 && ((off >> 32) & 0xffff) != 0)
6235 bytes += 4;
6236 if (((off >> 32) & 0xffffffffULL) != 0)
6237 bytes += 4;
6238 if (hi(off) != 0)
6239 bytes += 4;
6240 if (l(off) != 0)
6241 bytes += 4;
6242 }
6243 bytes = this->plt_call_align(bytes);
6244 }
6245 return bytes;
32f59844 6246 }
afd2ea23
AM
6247 else
6248 {
6249 unsigned int bytes = 0;
6250 unsigned int tail = 0;
6251 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6252 {
6253 bytes = 7 * 4;
6254 if (p->second.r2save_ && !p->second.localentry0_)
6255 {
6256 bytes = 9 * 4;
6257 tail = 4 * 4;
6258 }
6259 }
32f59844 6260
afd2ea23
AM
6261 if (p->second.r2save_)
6262 bytes += 4;
32f59844 6263
afd2ea23
AM
6264 if (this->targ_->power10_stubs())
6265 {
6266 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6267 uint64_t odd = from & 4;
6268 uint64_t off = plt_addr - from;
6269 if (off - odd + (1ULL << 33) < 1ULL << 34)
6270 bytes += odd + 4 * 4;
6271 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6272 bytes += 7 * 4;
6273 else
6274 bytes += 8 * 4;
eb97d024 6275 return this->plt_call_align(bytes + tail);
afd2ea23 6276 }
e4dff765 6277
eb97d024 6278 if (p->second.p9notoc_)
32f59844 6279 {
afd2ea23
AM
6280 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6281 uint64_t off = plt_addr - from;
6282 if (off + 0x8000 < 0x10000)
6283 bytes += 7 * 4;
6284 else if (off + 0x80008000ULL < 0x100000000ULL)
6285 bytes += 8 * 4;
6286 else
6287 {
6288 bytes += 8 * 4;
6289 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6290 && ((off >> 32) & 0xffff) != 0)
6291 bytes += 4;
6292 if (((off >> 32) & 0xffffffffULL) != 0)
6293 bytes += 4;
6294 if (hi(off) != 0)
6295 bytes += 4;
6296 if (l(off) != 0)
6297 bytes += 4;
6298 }
eb97d024 6299 return this->plt_call_align(bytes + tail);
32f59844 6300 }
32f59844 6301
a19da04b 6302 uint64_t got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6303 uint64_t off = plt_addr - got_addr;
6304 bytes += 3 * 4 + 4 * (ha(off) != 0);
6305 if (this->targ_->abiversion() < 2)
6306 {
6307 bool static_chain = parameters->options().plt_static_chain();
6308 bool thread_safe = this->targ_->plt_thread_safe();
6309 bytes += (4
6310 + 4 * static_chain
6311 + 8 * thread_safe
6312 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6313 }
eb97d024 6314 return this->plt_call_align(bytes + tail);
32f59844 6315 }
32f59844
AM
6316}
6317
6318// Return long branch stub size.
6319
6320template<int size, bool big_endian>
6321unsigned int
6322Stub_table<size, big_endian>::branch_stub_size(
afd2ea23 6323 typename Branch_stub_entries::iterator p,
32f59844
AM
6324 bool* need_lt)
6325{
6326 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6327 if (size == 32)
6328 {
6329 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6330 return 4;
6331 if (parameters->options().output_is_position_independent())
6332 return 32;
6333 return 16;
6334 }
6335
6336 uint64_t off = p->first.dest_ - loc;
afd2ea23 6337 unsigned int bytes = 0;
32f59844
AM
6338 if (p->second.notoc_)
6339 {
7c1f4227 6340 if (this->targ_->power10_stubs())
e4dff765
AM
6341 {
6342 Address odd = loc & 4;
6343 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6344 bytes = odd + 12;
6345 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6346 bytes = odd + 16;
6347 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6348 bytes = 28;
6349 else
6350 bytes = 32;
6351 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6352 return bytes;
6353 p->second.tocoff_ = bytes;
6354 }
6355 else
6356 {
6357 off -= 8;
6358 if (off + 0x8000 < 0x10000)
6359 return 24;
6360 if (off + 0x80008000ULL < 0x100000000ULL)
6361 {
6362 if (off + 24 + (1 << 25) < 2 << 25)
6363 return 28;
6364 return 32;
6365 }
6366
6367 bytes = 32;
6368 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6369 && ((off >> 32) & 0xffff) != 0)
6370 bytes += 4;
6371 if (((off >> 32) & 0xffffffffULL) != 0)
6372 bytes += 4;
6373 if (hi(off) != 0)
6374 bytes += 4;
6375 if (l(off) != 0)
6376 bytes += 4;
6377 return bytes;
6378 }
32f59844
AM
6379 }
6380
fa40fbe4 6381 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
32f59844 6382 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6383 return bytes + 4;
6384 if (!this->targ_->power10_stubs()
6385 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
e4dff765 6386 *need_lt = true;
afd2ea23 6387 return bytes + 16;
32f59844
AM
6388}
6389
f073a3e8
AM
6390template<int size, bool big_endian>
6391void
6392Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6393{
6394 if (p.sym_)
6395 gold_error(_("linkage table error against `%s'"),
6396 p.sym_->demangled_name().c_str());
6397 else
6398 gold_error(_("linkage table error against `%s:[local %u]'"),
6399 p.object_->name().c_str(),
6400 p.locsym_);
6401}
6402
ec661b9d 6403// Write out plt and long branch stub code.
cf43a2fe
AM
6404
6405template<int size, bool big_endian>
6406void
ec661b9d 6407Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6408{
ec661b9d
AM
6409 if (this->plt_call_stubs_.empty()
6410 && this->long_branch_stubs_.empty())
6411 return;
6412
6413 const section_size_type start_off = this->offset();
6414 const section_size_type off = this->stub_offset();
42cacb20 6415 const section_size_type oview_size =
ec661b9d 6416 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6417 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6418 unsigned char* p;
42cacb20 6419
e4dff765 6420 if (size == 64
7c1f4227 6421 && this->targ_->power10_stubs())
e4dff765
AM
6422 {
6423 if (!this->plt_call_stubs_.empty())
6424 {
6425 // Write out plt call stubs.
6426 typename Plt_stub_entries::const_iterator cs;
6427 for (cs = this->plt_call_stubs_.begin();
6428 cs != this->plt_call_stubs_.end();
6429 ++cs)
6430 {
6431 p = oview + cs->second.off_;
e4dff765
AM
6432 const Output_data_plt_powerpc<size, big_endian>* plt;
6433 Address pltoff = this->plt_off(cs, &plt);
6434 Address plt_addr = pltoff + plt->address();
afd2ea23
AM
6435 if (this->targ_->power10_stubs_auto())
6436 {
6437 if (cs->second.notoc_)
6438 {
6439 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6440 this->build_tls_opt_head(&p, false);
6441 Address from = this->stub_address() + (p - oview);
6442 Address delta = plt_addr - from;
6443 p = build_power10_offset<big_endian>(p, delta, from & 4,
6444 true);
6445 write_insn<big_endian>(p, mtctr_12);
6446 p += 4;
6447 write_insn<big_endian>(p, bctr);
6448 p += 4;
6449 p = oview + this->plt_call_align(p - oview);
6450 }
6451 if (cs->second.toc_)
6452 {
6453 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6454 {
6455 bool save_lr
6456 = cs->second.r2save_ && !cs->second.localentry0_;
6457 this->build_tls_opt_head(&p, save_lr);
6458 }
a19da04b 6459 Address got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6460 Address off = plt_addr - got_addr;
6461
6462 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6463 this->plt_error(cs->first);
6464
6465 if (cs->second.r2save_)
6466 {
6467 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6468 p += 4;
6469 }
6470 if (ha(off) != 0)
6471 {
6472 write_insn<big_endian>(p, addis_12_2 + ha(off));
6473 p += 4;
6474 write_insn<big_endian>(p, ld_12_12 + l(off));
6475 p += 4;
6476 }
6477 else
6478 {
6479 write_insn<big_endian>(p, ld_12_2 + l(off));
6480 p += 4;
6481 }
6482 write_insn<big_endian>(p, mtctr_12);
6483 p += 4;
6484 if (cs->second.r2save_
6485 && !cs->second.localentry0_
6486 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6487 this->build_tls_opt_tail(p);
6488 else
6489 write_insn<big_endian>(p, bctr);
6490 }
eb97d024
AM
6491 if (cs->second.p9notoc_)
6492 {
6493 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6494 this->build_tls_opt_head(&p, false);
6495 Address from = this->stub_address() + (p - oview);
6496 Address delta = plt_addr - from;
6497 p = build_notoc_offset<big_endian>(p, delta, true);
6498 write_insn<big_endian>(p, mtctr_12);
6499 p += 4;
6500 write_insn<big_endian>(p, bctr);
6501 p += 4;
6502 p = oview + this->plt_call_align(p - oview);
6503 }
afd2ea23
AM
6504 }
6505 else
6506 {
6507 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6508 {
6509 bool save_lr
6510 = cs->second.r2save_ && !cs->second.localentry0_;
6511 this->build_tls_opt_head(&p, save_lr);
6512 }
6513 if (cs->second.r2save_)
6514 {
6515 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6516 p += 4;
6517 }
6518 Address from = this->stub_address() + (p - oview);
6519 Address delta = plt_addr - from;
6520 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6521 write_insn<big_endian>(p, mtctr_12);
6522 p += 4;
6523 if (cs->second.r2save_
6524 && !cs->second.localentry0_
6525 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6526 this->build_tls_opt_tail(p);
6527 else
6528 write_insn<big_endian>(p, bctr);
6529 }
e4dff765
AM
6530 }
6531 }
6532
6533 // Write out long branch stubs.
6534 typename Branch_stub_entries::const_iterator bs;
6535 for (bs = this->long_branch_stubs_.begin();
6536 bs != this->long_branch_stubs_.end();
6537 ++bs)
6538 {
6539 if (bs->second.save_res_)
6540 continue;
6541 Address off = this->plt_size_ + bs->second.off_;
6542 p = oview + off;
6543 Address loc = this->stub_address() + off;
6544 Address delta = bs->first.dest_ - loc;
afd2ea23 6545 if (this->targ_->power10_stubs_auto())
e4dff765 6546 {
afd2ea23
AM
6547 if (bs->second.notoc_)
6548 {
6549 unsigned char* startp = p;
6550 p = build_power10_offset<big_endian>(p, delta,
6551 loc & 4, false);
6552 delta -= p - startp;
6553 startp = p;
6554 if (delta + (1 << 25) < 2 << 25)
6555 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6556 else
6557 {
6558 write_insn<big_endian>(p, mtctr_12);
6559 p += 4;
6560 write_insn<big_endian>(p, bctr);
6561 }
6562 p += 4;
6563 delta -= p - startp;
6564 }
6565 if (bs->second.toc_)
6566 {
fa40fbe4 6567 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6568 if (delta + (1 << 25) >= 2 << 25)
6569 {
6570 Address brlt_addr
6571 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6572 gold_assert(brlt_addr != invalid_address);
6573 brlt_addr += this->targ_->brlt_section()->address();
a19da04b 6574 Address got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6575 Address brltoff = brlt_addr - got_addr;
6576 if (ha(brltoff) == 0)
6577 {
6578 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6579 p += 4;
6580 }
6581 else
6582 {
6583 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6584 p += 4;
6585 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6586 p += 4;
6587 }
6588 }
6589 if (delta + (1 << 25) < 2 << 25)
6590 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6591 else
6592 {
6593 write_insn<big_endian>(p, mtctr_12);
6594 p += 4;
6595 write_insn<big_endian>(p, bctr);
6596 }
6597 }
eb97d024
AM
6598 if (bs->second.p9notoc_)
6599 {
6600 unsigned char* startp = p;
6601 p = build_notoc_offset<big_endian>(p, delta, false);
6602 delta -= p - startp;
6603 startp = p;
6604 if (delta + (1 << 25) < 2 << 25)
6605 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6606 else
6607 {
6608 write_insn<big_endian>(p, mtctr_12);
6609 p += 4;
6610 write_insn<big_endian>(p, bctr);
6611 }
6612 p += 4;
6613 delta -= p - startp;
6614 }
e4dff765 6615 }
e4dff765
AM
6616 else
6617 {
fa40fbe4
AM
6618 if (!bs->second.notoc_)
6619 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6620 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6621 {
6622 unsigned char* startp = p;
6623 p = build_power10_offset<big_endian>(p, delta,
6624 loc & 4, false);
6625 delta -= p - startp;
6626 }
6627 if (delta + (1 << 25) < 2 << 25)
6628 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6629 else
6630 {
6631 write_insn<big_endian>(p, mtctr_12);
6632 p += 4;
6633 write_insn<big_endian>(p, bctr);
6634 }
e4dff765
AM
6635 }
6636 }
6637 }
6638 else if (size == 64)
cf43a2fe 6639 {
c9269dff 6640
32f59844
AM
6641 if (!this->plt_call_stubs_.empty()
6642 && this->targ_->abiversion() >= 2)
cf43a2fe 6643 {
32f59844
AM
6644 // Write out plt call stubs for ELFv2.
6645 typename Plt_stub_entries::const_iterator cs;
6646 for (cs = this->plt_call_stubs_.begin();
6647 cs != this->plt_call_stubs_.end();
6648 ++cs)
6649 {
6650 const Output_data_plt_powerpc<size, big_endian>* plt;
6651 Address pltoff = this->plt_off(cs, &plt);
6652 Address plt_addr = pltoff + plt->address();
6653
6654 p = oview + cs->second.off_;
afd2ea23
AM
6655 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6656 {
6657 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6658 this->build_tls_opt_head(&p, save_lr);
6659 }
32f59844
AM
6660 if (cs->second.r2save_)
6661 {
6662 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6663 p += 4;
6664 }
eb97d024 6665 if (cs->second.p9notoc_)
32f59844
AM
6666 {
6667 Address from = this->stub_address() + (p - oview) + 8;
6668 Address off = plt_addr - from;
6669 p = build_notoc_offset<big_endian>(p, off, true);
6670 }
6671 else
6672 {
a19da04b 6673 Address got_addr = this->targ_->toc_pointer();
32f59844
AM
6674 Address off = plt_addr - got_addr;
6675
6676 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6677 this->plt_error(cs->first);
6678
6679 if (ha(off) != 0)
6680 {
6681 write_insn<big_endian>(p, addis_12_2 + ha(off));
6682 p += 4;
6683 write_insn<big_endian>(p, ld_12_12 + l(off));
6684 p += 4;
6685 }
6686 else
6687 {
6688 write_insn<big_endian>(p, ld_12_2 + l(off));
6689 p += 4;
6690 }
6691 }
6692 write_insn<big_endian>(p, mtctr_12);
6693 p += 4;
afd2ea23
AM
6694 if (cs->second.r2save_
6695 && !cs->second.localentry0_
6696 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6697 this->build_tls_opt_tail(p);
6698 else
32f59844
AM
6699 write_insn<big_endian>(p, bctr);
6700 }
6701 }
6702 else if (!this->plt_call_stubs_.empty())
6703 {
6704 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6705 typename Plt_stub_entries::const_iterator cs;
6706 for (cs = this->plt_call_stubs_.begin();
6707 cs != this->plt_call_stubs_.end();
6708 ++cs)
e5d5f5ed 6709 {
08be3224
AM
6710 const Output_data_plt_powerpc<size, big_endian>* plt;
6711 Address pltoff = this->plt_off(cs, &plt);
6712 Address plt_addr = pltoff + plt->address();
a19da04b 6713 Address got_addr = this->targ_->toc_pointer();
9e69ed50 6714 Address off = plt_addr - got_addr;
ec661b9d 6715
32f59844
AM
6716 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6717 || cs->second.notoc_)
f073a3e8 6718 this->plt_error(cs->first);
ec661b9d 6719
32f59844
AM
6720 bool static_chain = parameters->options().plt_static_chain();
6721 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6722 bool use_fake_dep = false;
6723 Address cmp_branch_off = 0;
407aa07c 6724 if (thread_safe)
9e69ed50
AM
6725 {
6726 unsigned int pltindex
6727 = ((pltoff - this->targ_->first_plt_entry_offset())
6728 / this->targ_->plt_entry_size());
6729 Address glinkoff
9e390558 6730 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6731 + pltindex * 8);
6732 if (pltindex > 32768)
6733 glinkoff += (pltindex - 32768) * 4;
6734 Address to
6735 = this->targ_->glink_section()->address() + glinkoff;
6736 Address from
7e57d19e
AM
6737 = (this->stub_address() + cs->second.off_ + 20
6738 + 4 * cs->second.r2save_
9e69ed50
AM
6739 + 4 * (ha(off) != 0)
6740 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6741 + 4 * static_chain);
6742 cmp_branch_off = to - from;
6743 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6744 }
6745
bdab445c 6746 p = oview + cs->second.off_;
afd2ea23
AM
6747 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6748 {
6749 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6750 this->build_tls_opt_head(&p, save_lr);
6751 use_fake_dep = thread_safe;
6752 }
32f59844 6753 if (cs->second.r2save_)
34e0882b 6754 {
32f59844 6755 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6756 p += 4;
34e0882b 6757 }
9e69ed50 6758 if (ha(off) != 0)
ec661b9d 6759 {
32f59844
AM
6760 write_insn<big_endian>(p, addis_11_2 + ha(off));
6761 p += 4;
6762 write_insn<big_endian>(p, ld_12_11 + l(off));
6763 p += 4;
6764 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6765 {
32f59844 6766 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6767 p += 4;
32f59844 6768 off = 0;
397998fc 6769 }
32f59844
AM
6770 write_insn<big_endian>(p, mtctr_12);
6771 p += 4;
6772 if (use_fake_dep)
397998fc 6773 {
32f59844 6774 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6775 p += 4;
32f59844 6776 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6777 p += 4;
6778 }
32f59844 6779 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6780 p += 4;
32f59844 6781 if (static_chain)
9e69ed50 6782 {
32f59844 6783 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6784 p += 4;
9e69ed50 6785 }
ec661b9d
AM
6786 }
6787 else
6788 {
b4f7960d
AM
6789 write_insn<big_endian>(p, ld_12_2 + l(off));
6790 p += 4;
32f59844 6791 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6792 {
b4f7960d
AM
6793 write_insn<big_endian>(p, addi_2_2 + l(off));
6794 p += 4;
9e69ed50 6795 off = 0;
ec661b9d 6796 }
b4f7960d
AM
6797 write_insn<big_endian>(p, mtctr_12);
6798 p += 4;
32f59844 6799 if (use_fake_dep)
9e69ed50 6800 {
32f59844
AM
6801 write_insn<big_endian>(p, xor_11_12_12);
6802 p += 4;
6803 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6804 p += 4;
9e69ed50 6805 }
32f59844
AM
6806 if (static_chain)
6807 {
6808 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6809 p += 4;
6810 }
6811 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6812 p += 4;
34e0882b 6813 }
afd2ea23
AM
6814 if (cs->second.r2save_
6815 && !cs->second.localentry0_
6816 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6817 this->build_tls_opt_tail(p);
34e0882b 6818 else if (thread_safe && !use_fake_dep)
9e69ed50 6819 {
b4f7960d
AM
6820 write_insn<big_endian>(p, cmpldi_2_0);
6821 p += 4;
6822 write_insn<big_endian>(p, bnectr_p4);
6823 p += 4;
9e69ed50
AM
6824 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6825 }
6826 else
407aa07c 6827 write_insn<big_endian>(p, bctr);
e5d5f5ed 6828 }
ec661b9d
AM
6829 }
6830
6831 // Write out long branch stubs.
6832 typename Branch_stub_entries::const_iterator bs;
6833 for (bs = this->long_branch_stubs_.begin();
6834 bs != this->long_branch_stubs_.end();
6835 ++bs)
6836 {
32f59844 6837 if (bs->second.save_res_)
d49044c7 6838 continue;
32f59844
AM
6839 Address off = this->plt_size_ + bs->second.off_;
6840 p = oview + off;
6841 Address loc = this->stub_address() + off;
ec661b9d 6842 Address delta = bs->first.dest_ - loc;
eb97d024 6843 if (!bs->second.p9notoc_)
fa40fbe4 6844 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
eb97d024 6845 if (bs->second.p9notoc_)
32f59844
AM
6846 {
6847 unsigned char* startp = p;
6848 p = build_notoc_offset<big_endian>(p, off, false);
6849 delta -= p - startp;
6850 }
6851 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6852 {
ec661b9d
AM
6853 Address brlt_addr
6854 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6855 gold_assert(brlt_addr != invalid_address);
6856 brlt_addr += this->targ_->brlt_section()->address();
a19da04b 6857 Address got_addr = this->targ_->toc_pointer();
ec661b9d
AM
6858 Address brltoff = brlt_addr - got_addr;
6859 if (ha(brltoff) == 0)
6860 {
32f59844
AM
6861 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6862 p += 4;
ec661b9d
AM
6863 }
6864 else
cf43a2fe 6865 {
32f59844
AM
6866 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6867 p += 4;
6868 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6869 p += 4;
cf43a2fe 6870 }
32f59844
AM
6871 }
6872 if (delta + (1 << 25) < 2 << 25)
6873 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6874 else
6875 {
6876 write_insn<big_endian>(p, mtctr_12);
6877 p += 4;
407aa07c 6878 write_insn<big_endian>(p, bctr);
cf43a2fe 6879 }
ec661b9d
AM
6880 }
6881 }
32f59844 6882 else // size == 32
ec661b9d
AM
6883 {
6884 if (!this->plt_call_stubs_.empty())
6885 {
ec661b9d
AM
6886 // The address of _GLOBAL_OFFSET_TABLE_.
6887 Address g_o_t = invalid_address;
6888
6889 // Write out plt call stubs.
6890 typename Plt_stub_entries::const_iterator cs;
6891 for (cs = this->plt_call_stubs_.begin();
6892 cs != this->plt_call_stubs_.end();
6893 ++cs)
cf43a2fe 6894 {
08be3224
AM
6895 const Output_data_plt_powerpc<size, big_endian>* plt;
6896 Address plt_addr = this->plt_off(cs, &plt);
6897 plt_addr += plt->address();
ec661b9d 6898
bdab445c 6899 p = oview + cs->second.off_;
afd2ea23
AM
6900 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6901 this->build_tls_opt_head(&p, false);
ec661b9d
AM
6902 if (parameters->options().output_is_position_independent())
6903 {
6904 Address got_addr;
6905 const Powerpc_relobj<size, big_endian>* ppcobj
6906 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6907 (cs->first.object_));
6908 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6909 {
6910 unsigned int got2 = ppcobj->got2_shndx();
6911 got_addr = ppcobj->get_output_section_offset(got2);
6912 gold_assert(got_addr != invalid_address);
6913 got_addr += (ppcobj->output_section(got2)->address()
6914 + cs->first.addend_);
6915 }
6916 else
6917 {
6918 if (g_o_t == invalid_address)
a19da04b 6919 g_o_t = this->targ_->toc_pointer();
ec661b9d
AM
6920 got_addr = g_o_t;
6921 }
6922
9e69ed50
AM
6923 Address off = plt_addr - got_addr;
6924 if (ha(off) == 0)
9e390558 6925 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6926 else
6927 {
9e390558
AM
6928 write_insn<big_endian>(p, addis_11_30 + ha(off));
6929 p += 4;
6930 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6931 }
6932 }
6933 else
6934 {
9e390558
AM
6935 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6936 p += 4;
6937 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6938 }
9e390558
AM
6939 p += 4;
6940 write_insn<big_endian>(p, mtctr_11);
6941 p += 4;
407aa07c 6942 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6943 }
6944 }
6945
6946 // Write out long branch stubs.
6947 typename Branch_stub_entries::const_iterator bs;
6948 for (bs = this->long_branch_stubs_.begin();
6949 bs != this->long_branch_stubs_.end();
6950 ++bs)
6951 {
32f59844 6952 if (bs->second.save_res_)
d49044c7 6953 continue;
32f59844
AM
6954 Address off = this->plt_size_ + bs->second.off_;
6955 p = oview + off;
6956 Address loc = this->stub_address() + off;
ec661b9d
AM
6957 Address delta = bs->first.dest_ - loc;
6958 if (delta + (1 << 25) < 2 << 25)
6959 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6960 else if (!parameters->options().output_is_position_independent())
6961 {
9e390558
AM
6962 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6963 p += 4;
6964 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6965 }
6966 else
6967 {
6968 delta -= 8;
9e390558
AM
6969 write_insn<big_endian>(p, mflr_0);
6970 p += 4;
6971 write_insn<big_endian>(p, bcl_20_31);
6972 p += 4;
6973 write_insn<big_endian>(p, mflr_12);
6974 p += 4;
6975 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6976 p += 4;
6977 write_insn<big_endian>(p, addi_12_12 + l(delta));
6978 p += 4;
6979 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6980 }
9e390558
AM
6981 p += 4;
6982 write_insn<big_endian>(p, mtctr_12);
6983 p += 4;
407aa07c 6984 write_insn<big_endian>(p, bctr);
cf43a2fe 6985 }
ec661b9d 6986 }
d49044c7
AM
6987 if (this->need_save_res_)
6988 {
6989 p = oview + this->plt_size_ + this->branch_size_;
6990 memcpy (p, this->targ_->savres_section()->contents(),
6991 this->targ_->savres_section()->data_size());
6992 }
ec661b9d
AM
6993}
6994
6995// Write out .glink.
6996
6997template<int size, bool big_endian>
6998void
6999Output_data_glink<size, big_endian>::do_write(Output_file* of)
7000{
7001 const section_size_type off = this->offset();
7002 const section_size_type oview_size =
7003 convert_to_section_size_type(this->data_size());
7004 unsigned char* const oview = of->get_output_view(off, oview_size);
7005 unsigned char* p;
7006
7007 // The base address of the .plt section.
7008 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7009 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 7010
ec661b9d
AM
7011 if (size == 64)
7012 {
9055360d 7013 if (this->end_branch_table_ != 0)
b4f7960d 7014 {
9055360d
AM
7015 // Write pltresolve stub.
7016 p = oview;
7017 Address after_bcl = this->address() + 16;
7018 Address pltoff = plt_base - after_bcl;
7019
7020 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 7021
b4f7960d 7022 if (this->targ_->abiversion() < 2)
cf43a2fe 7023 {
9055360d
AM
7024 write_insn<big_endian>(p, mflr_12), p += 4;
7025 write_insn<big_endian>(p, bcl_20_31), p += 4;
7026 write_insn<big_endian>(p, mflr_11), p += 4;
7027 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
7028 write_insn<big_endian>(p, mtlr_12), p += 4;
7029 write_insn<big_endian>(p, add_11_2_11), p += 4;
7030 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7031 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
7032 write_insn<big_endian>(p, mtctr_12), p += 4;
7033 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
7034 }
7035 else
7036 {
63e5eea2
AM
7037 if (this->targ_->has_localentry0())
7038 {
7039 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
7040 }
9055360d
AM
7041 write_insn<big_endian>(p, mflr_0), p += 4;
7042 write_insn<big_endian>(p, bcl_20_31), p += 4;
7043 write_insn<big_endian>(p, mflr_11), p += 4;
9055360d 7044 write_insn<big_endian>(p, mtlr_0), p += 4;
63e5eea2
AM
7045 if (this->targ_->has_localentry0())
7046 {
7047 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
7048 }
7049 else
7050 {
7051 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
7052 }
9055360d 7053 write_insn<big_endian>(p, sub_12_12_11), p += 4;
63e5eea2
AM
7054 write_insn<big_endian>(p, add_11_0_11), p += 4;
7055 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
9055360d
AM
7056 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7057 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
7058 write_insn<big_endian>(p, mtctr_12), p += 4;
7059 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
7060 }
407aa07c 7061 write_insn<big_endian>(p, bctr), p += 4;
9e390558 7062 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
7063
7064 // Write lazy link call stubs.
7065 uint32_t indx = 0;
7066 while (p < oview + this->end_branch_table_)
7067 {
7068 if (this->targ_->abiversion() < 2)
b4f7960d 7069 {
9055360d
AM
7070 if (indx < 0x8000)
7071 {
7072 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
7073 }
7074 else
7075 {
bbec1a5d 7076 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
7077 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
7078 }
b4f7960d 7079 }
9055360d
AM
7080 uint32_t branch_off = 8 - (p - oview);
7081 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
7082 indx++;
cf43a2fe 7083 }
9055360d
AM
7084 }
7085
7086 Address plt_base = this->targ_->plt_section()->address();
7087 Address iplt_base = invalid_address;
9e390558 7088 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
7089 Address global_entry_base = this->address() + global_entry_off;
7090 typename Global_entry_stub_entries::const_iterator ge;
7091 for (ge = this->global_entry_stubs_.begin();
7092 ge != this->global_entry_stubs_.end();
7093 ++ge)
7094 {
7095 p = oview + global_entry_off + ge->second;
7096 Address plt_addr = ge->first->plt_offset();
7097 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7098 && ge->first->can_use_relative_reloc(false))
7099 {
7100 if (iplt_base == invalid_address)
7101 iplt_base = this->targ_->iplt_section()->address();
7102 plt_addr += iplt_base;
7103 }
7104 else
7105 plt_addr += plt_base;
7106 Address my_addr = global_entry_base + ge->second;
7107 Address off = plt_addr - my_addr;
7108
7109 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 7110 gold_error(_("linkage table error against `%s'"),
9055360d
AM
7111 ge->first->demangled_name().c_str());
7112
7113 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
7114 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
7115 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 7116 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
7117 }
7118 }
7119 else
7120 {
dd93cd0a 7121 // The address of _GLOBAL_OFFSET_TABLE_.
a19da04b 7122 Address g_o_t = this->targ_->toc_pointer();
c9269dff 7123
cf43a2fe 7124 // Write out pltresolve branch table.
ec661b9d 7125 p = oview;
9e390558 7126 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 7127 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
7128 while (p < end_p - 8 * 4)
7129 write_insn<big_endian>(p, b + end_p - p), p += 4;
7130 while (p < end_p)
7131 write_insn<big_endian>(p, nop), p += 4;
42cacb20 7132
cf43a2fe 7133 // Write out pltresolve call stub.
9e390558 7134 end_p = oview + oview_size;
cf43a2fe 7135 if (parameters->options().output_is_position_independent())
42cacb20 7136 {
ec661b9d 7137 Address res0_off = 0;
dd93cd0a
AM
7138 Address after_bcl_off = the_end + 12;
7139 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 7140
9e390558
AM
7141 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7142 p += 4;
7143 write_insn<big_endian>(p, mflr_0);
7144 p += 4;
7145 write_insn<big_endian>(p, bcl_20_31);
7146 p += 4;
7147 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7148 p += 4;
7149 write_insn<big_endian>(p, mflr_12);
7150 p += 4;
7151 write_insn<big_endian>(p, mtlr_0);
7152 p += 4;
7153 write_insn<big_endian>(p, sub_11_11_12);
7154 p += 4;
cf43a2fe 7155
dd93cd0a 7156 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 7157
9e390558
AM
7158 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7159 p += 4;
cf43a2fe
AM
7160 if (ha(got_bcl) == ha(got_bcl + 4))
7161 {
9e390558
AM
7162 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7163 p += 4;
7164 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
7165 }
7166 else
7167 {
9e390558
AM
7168 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7169 p += 4;
7170 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 7171 }
9e390558
AM
7172 p += 4;
7173 write_insn<big_endian>(p, mtctr_0);
7174 p += 4;
7175 write_insn<big_endian>(p, add_0_11_11);
7176 p += 4;
7177 write_insn<big_endian>(p, add_11_0_11);
42cacb20 7178 }
cf43a2fe 7179 else
42cacb20 7180 {
ec661b9d 7181 Address res0 = this->address();
cf43a2fe 7182
9e390558
AM
7183 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7184 p += 4;
7185 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7186 p += 4;
cf43a2fe 7187 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7188 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 7189 else
9e390558
AM
7190 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7191 p += 4;
7192 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7193 p += 4;
7194 write_insn<big_endian>(p, mtctr_0);
7195 p += 4;
7196 write_insn<big_endian>(p, add_0_11_11);
7197 p += 4;
cf43a2fe 7198 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7199 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 7200 else
9e390558
AM
7201 write_insn<big_endian>(p, lwz_12_12 + 4);
7202 p += 4;
7203 write_insn<big_endian>(p, add_11_0_11);
7204 }
7205 p += 4;
407aa07c
AM
7206 write_insn<big_endian>(p, bctr);
7207 p += 4;
9e390558
AM
7208 while (p < end_p)
7209 {
7210 write_insn<big_endian>(p, nop);
7211 p += 4;
42cacb20
DE
7212 }
7213 }
7214
cf43a2fe
AM
7215 of->write_output_view(off, oview_size, oview);
7216}
7217
f3a0ed29
AM
7218
7219// A class to handle linker generated save/restore functions.
7220
7221template<int size, bool big_endian>
7222class Output_data_save_res : public Output_section_data_build
7223{
7224 public:
7225 Output_data_save_res(Symbol_table* symtab);
7226
d49044c7
AM
7227 const unsigned char*
7228 contents() const
7229 {
7230 return contents_;
7231 }
7232
f3a0ed29
AM
7233 protected:
7234 // Write to a map file.
7235 void
7236 do_print_to_mapfile(Mapfile* mapfile) const
7237 { mapfile->print_output_data(this, _("** save/restore")); }
7238
7239 void
7240 do_write(Output_file*);
7241
7242 private:
7243 // The maximum size of save/restore contents.
7244 static const unsigned int savres_max = 218*4;
7245
7246 void
7247 savres_define(Symbol_table* symtab,
7248 const char *name,
7249 unsigned int lo, unsigned int hi,
7250 unsigned char* write_ent(unsigned char*, int),
7251 unsigned char* write_tail(unsigned char*, int));
7252
7253 unsigned char *contents_;
7254};
7255
7256template<bool big_endian>
7257static unsigned char*
7258savegpr0(unsigned char* p, int r)
7259{
7260 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7261 write_insn<big_endian>(p, insn);
7262 return p + 4;
7263}
7264
7265template<bool big_endian>
7266static unsigned char*
7267savegpr0_tail(unsigned char* p, int r)
7268{
7269 p = savegpr0<big_endian>(p, r);
7270 uint32_t insn = std_0_1 + 16;
7271 write_insn<big_endian>(p, insn);
7272 p = p + 4;
7273 write_insn<big_endian>(p, blr);
7274 return p + 4;
7275}
7276
7277template<bool big_endian>
62fe925a 7278static unsigned char*
f3a0ed29
AM
7279restgpr0(unsigned char* p, int r)
7280{
7281 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7282 write_insn<big_endian>(p, insn);
7283 return p + 4;
7284}
7285
7286template<bool big_endian>
62fe925a 7287static unsigned char*
f3a0ed29
AM
7288restgpr0_tail(unsigned char* p, int r)
7289{
7290 uint32_t insn = ld_0_1 + 16;
7291 write_insn<big_endian>(p, insn);
7292 p = p + 4;
7293 p = restgpr0<big_endian>(p, r);
7294 write_insn<big_endian>(p, mtlr_0);
7295 p = p + 4;
7296 if (r == 29)
7297 {
7298 p = restgpr0<big_endian>(p, 30);
7299 p = restgpr0<big_endian>(p, 31);
7300 }
7301 write_insn<big_endian>(p, blr);
7302 return p + 4;
7303}
7304
7305template<bool big_endian>
62fe925a 7306static unsigned char*
f3a0ed29
AM
7307savegpr1(unsigned char* p, int r)
7308{
7309 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7310 write_insn<big_endian>(p, insn);
7311 return p + 4;
7312}
7313
7314template<bool big_endian>
62fe925a 7315static unsigned char*
f3a0ed29
AM
7316savegpr1_tail(unsigned char* p, int r)
7317{
7318 p = savegpr1<big_endian>(p, r);
7319 write_insn<big_endian>(p, blr);
7320 return p + 4;
7321}
7322
7323template<bool big_endian>
62fe925a 7324static unsigned char*
f3a0ed29
AM
7325restgpr1(unsigned char* p, int r)
7326{
7327 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7328 write_insn<big_endian>(p, insn);
7329 return p + 4;
7330}
7331
7332template<bool big_endian>
62fe925a 7333static unsigned char*
f3a0ed29
AM
7334restgpr1_tail(unsigned char* p, int r)
7335{
7336 p = restgpr1<big_endian>(p, r);
7337 write_insn<big_endian>(p, blr);
7338 return p + 4;
7339}
7340
7341template<bool big_endian>
62fe925a 7342static unsigned char*
f3a0ed29
AM
7343savefpr(unsigned char* p, int r)
7344{
7345 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7346 write_insn<big_endian>(p, insn);
7347 return p + 4;
7348}
7349
7350template<bool big_endian>
62fe925a 7351static unsigned char*
f3a0ed29
AM
7352savefpr0_tail(unsigned char* p, int r)
7353{
7354 p = savefpr<big_endian>(p, r);
7355 write_insn<big_endian>(p, std_0_1 + 16);
7356 p = p + 4;
7357 write_insn<big_endian>(p, blr);
7358 return p + 4;
7359}
7360
7361template<bool big_endian>
62fe925a 7362static unsigned char*
f3a0ed29
AM
7363restfpr(unsigned char* p, int r)
7364{
7365 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7366 write_insn<big_endian>(p, insn);
7367 return p + 4;
7368}
7369
7370template<bool big_endian>
62fe925a 7371static unsigned char*
f3a0ed29
AM
7372restfpr0_tail(unsigned char* p, int r)
7373{
7374 write_insn<big_endian>(p, ld_0_1 + 16);
7375 p = p + 4;
7376 p = restfpr<big_endian>(p, r);
7377 write_insn<big_endian>(p, mtlr_0);
7378 p = p + 4;
7379 if (r == 29)
7380 {
7381 p = restfpr<big_endian>(p, 30);
7382 p = restfpr<big_endian>(p, 31);
7383 }
7384 write_insn<big_endian>(p, blr);
7385 return p + 4;
7386}
7387
7388template<bool big_endian>
62fe925a 7389static unsigned char*
f3a0ed29
AM
7390savefpr1_tail(unsigned char* p, int r)
7391{
7392 p = savefpr<big_endian>(p, r);
7393 write_insn<big_endian>(p, blr);
7394 return p + 4;
7395}
7396
7397template<bool big_endian>
62fe925a 7398static unsigned char*
f3a0ed29
AM
7399restfpr1_tail(unsigned char* p, int r)
7400{
7401 p = restfpr<big_endian>(p, r);
7402 write_insn<big_endian>(p, blr);
7403 return p + 4;
7404}
7405
7406template<bool big_endian>
62fe925a 7407static unsigned char*
f3a0ed29
AM
7408savevr(unsigned char* p, int r)
7409{
7410 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7411 write_insn<big_endian>(p, insn);
7412 p = p + 4;
7413 insn = stvx_0_12_0 + (r << 21);
7414 write_insn<big_endian>(p, insn);
7415 return p + 4;
7416}
7417
7418template<bool big_endian>
62fe925a 7419static unsigned char*
f3a0ed29
AM
7420savevr_tail(unsigned char* p, int r)
7421{
7422 p = savevr<big_endian>(p, r);
7423 write_insn<big_endian>(p, blr);
7424 return p + 4;
7425}
7426
7427template<bool big_endian>
62fe925a 7428static unsigned char*
f3a0ed29
AM
7429restvr(unsigned char* p, int r)
7430{
7431 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7432 write_insn<big_endian>(p, insn);
7433 p = p + 4;
7434 insn = lvx_0_12_0 + (r << 21);
7435 write_insn<big_endian>(p, insn);
7436 return p + 4;
7437}
7438
7439template<bool big_endian>
62fe925a 7440static unsigned char*
f3a0ed29
AM
7441restvr_tail(unsigned char* p, int r)
7442{
7443 p = restvr<big_endian>(p, r);
7444 write_insn<big_endian>(p, blr);
7445 return p + 4;
7446}
7447
7448
7449template<int size, bool big_endian>
7450Output_data_save_res<size, big_endian>::Output_data_save_res(
7451 Symbol_table* symtab)
7452 : Output_section_data_build(4),
7453 contents_(NULL)
7454{
7455 this->savres_define(symtab,
7456 "_savegpr0_", 14, 31,
7457 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7458 this->savres_define(symtab,
7459 "_restgpr0_", 14, 29,
7460 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7461 this->savres_define(symtab,
7462 "_restgpr0_", 30, 31,
7463 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7464 this->savres_define(symtab,
7465 "_savegpr1_", 14, 31,
7466 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7467 this->savres_define(symtab,
7468 "_restgpr1_", 14, 31,
7469 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7470 this->savres_define(symtab,
7471 "_savefpr_", 14, 31,
7472 savefpr<big_endian>, savefpr0_tail<big_endian>);
7473 this->savres_define(symtab,
7474 "_restfpr_", 14, 29,
7475 restfpr<big_endian>, restfpr0_tail<big_endian>);
7476 this->savres_define(symtab,
7477 "_restfpr_", 30, 31,
7478 restfpr<big_endian>, restfpr0_tail<big_endian>);
7479 this->savres_define(symtab,
7480 "._savef", 14, 31,
7481 savefpr<big_endian>, savefpr1_tail<big_endian>);
7482 this->savres_define(symtab,
7483 "._restf", 14, 31,
7484 restfpr<big_endian>, restfpr1_tail<big_endian>);
7485 this->savres_define(symtab,
7486 "_savevr_", 20, 31,
7487 savevr<big_endian>, savevr_tail<big_endian>);
7488 this->savres_define(symtab,
7489 "_restvr_", 20, 31,
7490 restvr<big_endian>, restvr_tail<big_endian>);
7491}
7492
7493template<int size, bool big_endian>
7494void
7495Output_data_save_res<size, big_endian>::savres_define(
7496 Symbol_table* symtab,
7497 const char *name,
7498 unsigned int lo, unsigned int hi,
7499 unsigned char* write_ent(unsigned char*, int),
7500 unsigned char* write_tail(unsigned char*, int))
7501{
7502 size_t len = strlen(name);
7503 bool writing = false;
7504 char sym[16];
7505
7506 memcpy(sym, name, len);
7507 sym[len + 2] = 0;
7508
7509 for (unsigned int i = lo; i <= hi; i++)
7510 {
7511 sym[len + 0] = i / 10 + '0';
7512 sym[len + 1] = i % 10 + '0';
7513 Symbol* gsym = symtab->lookup(sym);
7514 bool refd = gsym != NULL && gsym->is_undefined();
7515 writing = writing || refd;
7516 if (writing)
7517 {
7518 if (this->contents_ == NULL)
7519 this->contents_ = new unsigned char[this->savres_max];
7520
ec661b9d 7521 section_size_type value = this->current_data_size();
f3a0ed29
AM
7522 unsigned char* p = this->contents_ + value;
7523 if (i != hi)
7524 p = write_ent(p, i);
7525 else
7526 p = write_tail(p, i);
ec661b9d 7527 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7528 this->set_current_data_size(cur_size);
7529 if (refd)
7530 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7531 this, value, cur_size - value,
7532 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7533 elfcpp::STV_HIDDEN, 0, false, false);
7534 }
7535 }
7536}
7537
7538// Write out save/restore.
7539
7540template<int size, bool big_endian>
7541void
7542Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7543{
ec661b9d 7544 const section_size_type off = this->offset();
f3a0ed29
AM
7545 const section_size_type oview_size =
7546 convert_to_section_size_type(this->data_size());
7547 unsigned char* const oview = of->get_output_view(off, oview_size);
7548 memcpy(oview, this->contents_, oview_size);
7549 of->write_output_view(off, oview_size, oview);
7550}
7551
7552
cf43a2fe 7553// Create the glink section.
42cacb20 7554
cf43a2fe
AM
7555template<int size, bool big_endian>
7556void
7557Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7558{
7559 if (this->glink_ == NULL)
7560 {
7561 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7562 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7563 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7564 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7565 this->glink_, ORDER_TEXT, false);
7566 }
42cacb20
DE
7567}
7568
7569// Create a PLT entry for a global symbol.
7570
7571template<int size, bool big_endian>
7572void
ec661b9d
AM
7573Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7574 Layout* layout,
7575 Symbol* gsym)
42cacb20 7576{
e5d5f5ed
AM
7577 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7578 && gsym->can_use_relative_reloc(false))
7579 {
7580 if (this->iplt_ == NULL)
40b469d7 7581 this->make_iplt_section(symtab, layout);
03e25981 7582 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7583 }
7584 else
7585 {
7586 if (this->plt_ == NULL)
40b469d7 7587 this->make_plt_section(symtab, layout);
03e25981 7588 this->plt_->add_entry(gsym);
e5d5f5ed 7589 }
e5d5f5ed 7590}
42cacb20 7591
2d7ad24e
AM
7592// Make a PLT entry for a local symbol.
7593
7594template<int size, bool big_endian>
7595void
7596Target_powerpc<size, big_endian>::make_local_plt_entry(
f21dbd7c 7597 Symbol_table* symtab,
2d7ad24e
AM
7598 Layout* layout,
7599 Sized_relobj_file<size, big_endian>* relobj,
7600 unsigned int r_sym)
7601{
7602 if (this->lplt_ == NULL)
f21dbd7c 7603 this->make_lplt_section(symtab, layout);
2d7ad24e
AM
7604 this->lplt_->add_local_entry(relobj, r_sym);
7605}
7606
f21dbd7c
AM
7607template<int size, bool big_endian>
7608void
7609Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7610 Layout* layout,
7611 Symbol* gsym)
7612{
7613 if (this->lplt_ == NULL)
7614 this->make_lplt_section(symtab, layout);
7615 this->lplt_->add_entry(gsym, true);
7616}
7617
e5d5f5ed 7618// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7619
e5d5f5ed
AM
7620template<int size, bool big_endian>
7621void
7622Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7623 Symbol_table* symtab,
e5d5f5ed 7624 Layout* layout,
ec661b9d
AM
7625 Sized_relobj_file<size, big_endian>* relobj,
7626 unsigned int r_sym)
e5d5f5ed
AM
7627{
7628 if (this->iplt_ == NULL)
40b469d7 7629 this->make_iplt_section(symtab, layout);
03e25981 7630 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7631}
7632
0e70b911
CC
7633// Return the number of entries in the PLT.
7634
7635template<int size, bool big_endian>
7636unsigned int
7637Target_powerpc<size, big_endian>::plt_entry_count() const
7638{
7639 if (this->plt_ == NULL)
7640 return 0;
b3ccdeb5 7641 return this->plt_->entry_count();
0e70b911
CC
7642}
7643
dd93cd0a 7644// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7645
7646template<int size, bool big_endian>
7647unsigned int
dd93cd0a 7648Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7649 Symbol_table* symtab,
7650 Layout* layout,
7651 Sized_relobj_file<size, big_endian>* object)
42cacb20 7652{
dd93cd0a 7653 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7654 {
7655 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7656 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a 7657 Output_data_got_powerpc<size, big_endian>* got
f19c3684 7658 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a 7659 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7660 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7661 got_offset, 0);
dd93cd0a 7662 this->tlsld_got_offset_ = got_offset;
42cacb20 7663 }
dd93cd0a 7664 return this->tlsld_got_offset_;
42cacb20
DE
7665}
7666
95a2c8d6
RS
7667// Get the Reference_flags for a particular relocation.
7668
7669template<int size, bool big_endian>
7670int
88b8e639
AM
7671Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7672 unsigned int r_type,
7673 const Target_powerpc* target)
95a2c8d6 7674{
88b8e639
AM
7675 int ref = 0;
7676
95a2c8d6
RS
7677 switch (r_type)
7678 {
6158b25f
AM
7679 case elfcpp::R_PPC64_TOC:
7680 if (size != 64)
7681 break;
7682 // Fall through.
95a2c8d6
RS
7683 case elfcpp::R_POWERPC_NONE:
7684 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7685 case elfcpp::R_POWERPC_GNU_VTENTRY:
95a2c8d6 7686 // No symbol reference.
88b8e639 7687 break;
95a2c8d6 7688
dd93cd0a
AM
7689 case elfcpp::R_PPC64_ADDR64:
7690 case elfcpp::R_PPC64_UADDR64:
89c52ae3
AM
7691 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7692 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7693 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7694 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7695 case elfcpp::R_PPC64_D34:
7696 case elfcpp::R_PPC64_D34_LO:
7697 case elfcpp::R_PPC64_D34_HI30:
7698 case elfcpp::R_PPC64_D34_HA30:
7699 case elfcpp::R_PPC64_D28:
6158b25f
AM
7700 if (size != 64)
7701 break;
7702 // Fall through.
7703 case elfcpp::R_POWERPC_ADDR32:
7704 case elfcpp::R_POWERPC_UADDR32:
7705 case elfcpp::R_POWERPC_ADDR16:
7706 case elfcpp::R_POWERPC_UADDR16:
7707 case elfcpp::R_POWERPC_ADDR16_LO:
7708 case elfcpp::R_POWERPC_ADDR16_HI:
7709 case elfcpp::R_POWERPC_ADDR16_HA:
88b8e639
AM
7710 ref = Symbol::ABSOLUTE_REF;
7711 break;
95a2c8d6 7712
dd93cd0a
AM
7713 case elfcpp::R_POWERPC_ADDR24:
7714 case elfcpp::R_POWERPC_ADDR14:
7715 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7716 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7717 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7718 break;
dd93cd0a 7719
95a2c8d6 7720 case elfcpp::R_PPC_LOCAL24PC:
6158b25f
AM
7721 if (size != 32)
7722 break;
7723 // Fall through.
7724 ref = Symbol::RELATIVE_REF;
7725 break;
7726
7727 case elfcpp::R_PPC64_REL64:
c432bbba
AM
7728 case elfcpp::R_PPC64_REL16_HIGH:
7729 case elfcpp::R_PPC64_REL16_HIGHA:
7730 case elfcpp::R_PPC64_REL16_HIGHER:
7731 case elfcpp::R_PPC64_REL16_HIGHERA:
7732 case elfcpp::R_PPC64_REL16_HIGHEST:
7733 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7734 case elfcpp::R_PPC64_PCREL34:
7735 case elfcpp::R_PPC64_REL16_HIGHER34:
7736 case elfcpp::R_PPC64_REL16_HIGHERA34:
7737 case elfcpp::R_PPC64_REL16_HIGHEST34:
7738 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7739 case elfcpp::R_PPC64_PCREL28:
6158b25f
AM
7740 if (size != 64)
7741 break;
7742 // Fall through.
7743 case elfcpp::R_POWERPC_REL32:
7744 case elfcpp::R_POWERPC_REL16:
7745 case elfcpp::R_POWERPC_REL16_LO:
7746 case elfcpp::R_POWERPC_REL16_HI:
7747 case elfcpp::R_POWERPC_REL16_HA:
88b8e639
AM
7748 ref = Symbol::RELATIVE_REF;
7749 break;
95a2c8d6 7750
6158b25f
AM
7751 case elfcpp::R_PPC_PLTREL24:
7752 if (size != 32)
7753 break;
7754 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7755 break;
7756
32f59844 7757 case elfcpp::R_PPC64_REL24_NOTOC:
6158b25f
AM
7758 case elfcpp::R_PPC64_REL24_P9NOTOC:
7759 case elfcpp::R_PPC64_PLT16_LO_DS:
7760 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7761 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7762 case elfcpp::R_PPC64_PLT_PCREL34:
7763 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7764 if (size != 64)
32f59844
AM
7765 break;
7766 // Fall through.
dd93cd0a 7767 case elfcpp::R_POWERPC_REL24:
dd93cd0a
AM
7768 case elfcpp::R_POWERPC_REL14:
7769 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7770 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
f21dbd7c
AM
7771 case elfcpp::R_POWERPC_PLT16_LO:
7772 case elfcpp::R_POWERPC_PLT16_HI:
7773 case elfcpp::R_POWERPC_PLT16_HA:
f21dbd7c 7774 case elfcpp::R_POWERPC_PLTSEQ:
f21dbd7c 7775 case elfcpp::R_POWERPC_PLTCALL:
88b8e639
AM
7776 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7777 break;
95a2c8d6 7778
e5d5f5ed
AM
7779 case elfcpp::R_PPC64_GOT16_DS:
7780 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7781 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7782 case elfcpp::R_PPC64_TOC16:
7783 case elfcpp::R_PPC64_TOC16_LO:
7784 case elfcpp::R_PPC64_TOC16_HI:
7785 case elfcpp::R_PPC64_TOC16_HA:
7786 case elfcpp::R_PPC64_TOC16_DS:
7787 case elfcpp::R_PPC64_TOC16_LO_DS:
6158b25f
AM
7788 if (size != 64)
7789 break;
7790 // Fall through.
7791 case elfcpp::R_POWERPC_GOT16:
7792 case elfcpp::R_POWERPC_GOT16_LO:
7793 case elfcpp::R_POWERPC_GOT16_HI:
7794 case elfcpp::R_POWERPC_GOT16_HA:
32d849b3 7795 ref = Symbol::RELATIVE_REF;
88b8e639 7796 break;
95a2c8d6 7797
89c52ae3
AM
7798 case elfcpp::R_PPC64_TLSGD:
7799 case elfcpp::R_PPC64_TLSLD:
7800 case elfcpp::R_PPC64_TPREL34:
7801 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
7802 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7803 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7804 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7805 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
6158b25f
AM
7806 if (size != 64)
7807 break;
7808 // Fall through.
7809 case elfcpp::R_POWERPC_GOT_TPREL16:
7810 case elfcpp::R_POWERPC_TLS:
88b8e639
AM
7811 ref = Symbol::TLS_REF;
7812 break;
95a2c8d6
RS
7813
7814 case elfcpp::R_POWERPC_COPY:
7815 case elfcpp::R_POWERPC_GLOB_DAT:
7816 case elfcpp::R_POWERPC_JMP_SLOT:
7817 case elfcpp::R_POWERPC_RELATIVE:
7818 case elfcpp::R_POWERPC_DTPMOD:
7819 default:
7820 // Not expected. We will give an error later.
88b8e639 7821 break;
95a2c8d6 7822 }
88b8e639
AM
7823
7824 if (size == 64 && target->abiversion() < 2)
7825 ref |= Symbol::FUNC_DESC_ABI;
7826 return ref;
95a2c8d6
RS
7827}
7828
42cacb20
DE
7829// Report an unsupported relocation against a local symbol.
7830
7831template<int size, bool big_endian>
7832void
7833Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7834 Sized_relobj_file<size, big_endian>* object,
7835 unsigned int r_type)
42cacb20
DE
7836{
7837 gold_error(_("%s: unsupported reloc %u against local symbol"),
7838 object->name().c_str(), r_type);
7839}
7840
7841// We are about to emit a dynamic relocation of type R_TYPE. If the
7842// dynamic linker does not support it, issue an error.
7843
7844template<int size, bool big_endian>
7845void
7846Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7847 unsigned int r_type)
7848{
7849 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7850
7851 // These are the relocation types supported by glibc for both 32-bit
7852 // and 64-bit powerpc.
7853 switch (r_type)
7854 {
3ea0a085 7855 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7856 case elfcpp::R_POWERPC_RELATIVE:
7857 case elfcpp::R_POWERPC_GLOB_DAT:
7858 case elfcpp::R_POWERPC_DTPMOD:
7859 case elfcpp::R_POWERPC_DTPREL:
7860 case elfcpp::R_POWERPC_TPREL:
7861 case elfcpp::R_POWERPC_JMP_SLOT:
7862 case elfcpp::R_POWERPC_COPY:
3ea0a085 7863 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7864 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7865 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7866 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7867 case elfcpp::R_POWERPC_ADDR16:
7868 case elfcpp::R_POWERPC_UADDR16:
7869 case elfcpp::R_POWERPC_ADDR16_LO:
7870 case elfcpp::R_POWERPC_ADDR16_HI:
7871 case elfcpp::R_POWERPC_ADDR16_HA:
7872 case elfcpp::R_POWERPC_ADDR14:
7873 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7874 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7875 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7876 case elfcpp::R_POWERPC_TPREL16:
7877 case elfcpp::R_POWERPC_TPREL16_LO:
7878 case elfcpp::R_POWERPC_TPREL16_HI:
7879 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7880 return;
7881
7882 default:
7883 break;
7884 }
7885
7886 if (size == 64)
7887 {
7888 switch (r_type)
7889 {
7890 // These are the relocation types supported only on 64-bit.
7891 case elfcpp::R_PPC64_ADDR64:
42cacb20 7892 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7893 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7894 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7895 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7896 case elfcpp::R_PPC64_ADDR16_HIGH:
7897 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7898 case elfcpp::R_PPC64_ADDR16_HIGHER:
7899 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7900 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7901 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7902 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7903 case elfcpp::R_POWERPC_ADDR30:
7904 case elfcpp::R_PPC64_TPREL16_DS:
7905 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7906 case elfcpp::R_PPC64_TPREL16_HIGH:
7907 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7908 case elfcpp::R_PPC64_TPREL16_HIGHER:
7909 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7910 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7911 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7912 return;
7913
7914 default:
7915 break;
7916 }
7917 }
7918 else
7919 {
7920 switch (r_type)
7921 {
7922 // These are the relocation types supported only on 32-bit.
3ea0a085 7923 // ??? glibc ld.so doesn't need to support these.
e59a1001 7924 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7925 case elfcpp::R_POWERPC_DTPREL16:
7926 case elfcpp::R_POWERPC_DTPREL16_LO:
7927 case elfcpp::R_POWERPC_DTPREL16_HI:
7928 case elfcpp::R_POWERPC_DTPREL16_HA:
7929 return;
42cacb20
DE
7930
7931 default:
7932 break;
7933 }
7934 }
7935
7936 // This prevents us from issuing more than one error per reloc
7937 // section. But we can still wind up issuing more than one
7938 // error per object file.
7939 if (this->issued_non_pic_error_)
7940 return;
33aea2fd 7941 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7942 object->error(_("requires unsupported dynamic reloc; "
7943 "recompile with -fPIC"));
7944 this->issued_non_pic_error_ = true;
7945 return;
7946}
7947
e5d5f5ed
AM
7948// Return whether we need to make a PLT entry for a relocation of the
7949// given type against a STT_GNU_IFUNC symbol.
7950
7951template<int size, bool big_endian>
7952bool
7953Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7954 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7955 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7956 unsigned int r_type,
7957 bool report_err)
e5d5f5ed 7958{
c9824451
AM
7959 // In non-pic code any reference will resolve to the plt call stub
7960 // for the ifunc symbol.
9055360d
AM
7961 if ((size == 32 || target->abiversion() >= 2)
7962 && !parameters->options().output_is_position_independent())
c9824451
AM
7963 return true;
7964
e5d5f5ed
AM
7965 switch (r_type)
7966 {
b3ccdeb5 7967 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7968 case elfcpp::R_POWERPC_ADDR32:
7969 case elfcpp::R_POWERPC_UADDR32:
7970 if (size == 32)
b3ccdeb5 7971 return false;
e5d5f5ed
AM
7972 break;
7973
7974 case elfcpp::R_PPC64_ADDR64:
7975 case elfcpp::R_PPC64_UADDR64:
7976 if (size == 64)
b3ccdeb5 7977 return false;
e5d5f5ed
AM
7978 break;
7979
b3ccdeb5 7980 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7981 case elfcpp::R_POWERPC_GOT16:
7982 case elfcpp::R_POWERPC_GOT16_LO:
7983 case elfcpp::R_POWERPC_GOT16_HI:
7984 case elfcpp::R_POWERPC_GOT16_HA:
7985 case elfcpp::R_PPC64_GOT16_DS:
7986 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7987 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7988 return false;
e5d5f5ed 7989
08be3224
AM
7990 // PLT relocs are OK and need a PLT entry.
7991 case elfcpp::R_POWERPC_PLT16_LO:
7992 case elfcpp::R_POWERPC_PLT16_HI:
7993 case elfcpp::R_POWERPC_PLT16_HA:
7994 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
7995 case elfcpp::R_POWERPC_PLTSEQ:
7996 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7997 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7998 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7999 case elfcpp::R_PPC64_PLT_PCREL34:
8000 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8001 return true;
8002 break;
8003
b3ccdeb5 8004 // Function calls are good, and these do need a PLT entry.
32f59844
AM
8005 case elfcpp::R_PPC64_REL24_NOTOC:
8006 if (size == 32)
8007 break;
8008 // Fall through.
eb97d024 8009 case elfcpp::R_PPC64_REL24_P9NOTOC:
e5d5f5ed
AM
8010 case elfcpp::R_POWERPC_ADDR24:
8011 case elfcpp::R_POWERPC_ADDR14:
8012 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8013 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8014 case elfcpp::R_POWERPC_REL24:
8015 case elfcpp::R_PPC_PLTREL24:
8016 case elfcpp::R_POWERPC_REL14:
8017 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8018 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8019 return true;
8020
8021 default:
8022 break;
8023 }
8024
8025 // Anything else is a problem.
8026 // If we are building a static executable, the libc startup function
8027 // responsible for applying indirect function relocations is going
8028 // to complain about the reloc type.
8029 // If we are building a dynamic executable, we will have a text
8030 // relocation. The dynamic loader will set the text segment
8031 // writable and non-executable to apply text relocations. So we'll
8032 // segfault when trying to run the indirection function to resolve
8033 // the reloc.
b3ccdeb5
AM
8034 if (report_err)
8035 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
8036 object->name().c_str(), r_type);
8037 return false;
8038}
8039
5edad15d
AM
8040// Return TRUE iff INSN is one we expect on a _LO variety toc/got
8041// reloc.
8042
8043static bool
8044ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8045{
8046 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8047 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8048 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8049 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8050 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8051 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8052 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8053 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8054 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8055 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8056 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8057 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8058 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8059 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8060 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8061 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8062 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8063 /* Exclude lfqu by testing reloc. If relocs are ever
8064 defined for the reduced D field in psq_lu then those
8065 will need testing too. */
8066 && r_type != elfcpp::R_PPC64_TOC16_LO
8067 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8068 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8069 && (insn & 1) == 0)
8070 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8071 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8072 /* Exclude stfqu. psq_stu as above for psq_lu. */
8073 && r_type != elfcpp::R_PPC64_TOC16_LO
8074 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8075 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8076 && (insn & 1) == 0));
8077}
8078
42cacb20
DE
8079// Scan a relocation for a local symbol.
8080
8081template<int size, bool big_endian>
8082inline void
8083Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
8084 Symbol_table* symtab,
8085 Layout* layout,
8086 Target_powerpc<size, big_endian>* target,
8087 Sized_relobj_file<size, big_endian>* object,
8088 unsigned int data_shndx,
8089 Output_section* output_section,
8090 const elfcpp::Rela<size, big_endian>& reloc,
8091 unsigned int r_type,
e5d5f5ed 8092 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 8093 bool is_discarded)
42cacb20 8094{
0af4fcc2
AM
8095 Powerpc_relobj<size, big_endian>* ppc_object
8096 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8097
c7fdac09 8098 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
8099
8100 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8101 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8102 {
8103 this->expect_tls_get_addr_call();
c7fdac09
AM
8104 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8105 if (tls_type != tls::TLSOPT_NONE)
8106 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8107 }
8108 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8109 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8110 {
8111 this->expect_tls_get_addr_call();
c7fdac09
AM
8112 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8113 if (tls_type != tls::TLSOPT_NONE)
8114 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8115 }
8116
bfdfa4cd
AM
8117 if (is_discarded)
8118 {
8119 if (size == 64
8120 && data_shndx == ppc_object->opd_shndx()
8121 && r_type == elfcpp::R_PPC64_ADDR64)
8122 ppc_object->set_opd_discard(reloc.get_r_offset());
8123 return;
8124 }
8125
e5d5f5ed
AM
8126 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8127 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 8128 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 8129 {
ec661b9d
AM
8130 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8131 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8132 r_type, r_sym, reloc.get_r_addend());
8133 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 8134 }
e5d5f5ed 8135
42cacb20
DE
8136 switch (r_type)
8137 {
8138 case elfcpp::R_POWERPC_NONE:
8139 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8140 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 8141 case elfcpp::R_POWERPC_TLS:
549dba71 8142 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8143 case elfcpp::R_POWERPC_PLTSEQ:
8144 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8145 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8146 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8147 case elfcpp::R_PPC64_PCREL_OPT:
8148 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8149 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8150 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8151 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8152 case elfcpp::R_PPC64_REL16_HIGHER34:
8153 case elfcpp::R_PPC64_REL16_HIGHERA34:
8154 case elfcpp::R_PPC64_REL16_HIGHEST34:
8155 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8156 case elfcpp::R_PPC64_D34:
8157 case elfcpp::R_PPC64_D34_LO:
8158 case elfcpp::R_PPC64_D34_HI30:
8159 case elfcpp::R_PPC64_D34_HA30:
8160 case elfcpp::R_PPC64_D28:
8161 case elfcpp::R_PPC64_PCREL34:
8162 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8163 case elfcpp::R_PPC64_TPREL34:
8164 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8165 break;
8166
8167 case elfcpp::R_PPC64_TOC:
8168 {
8169 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8170 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a
AM
8171 if (parameters->options().output_is_position_independent())
8172 {
bfdfa4cd
AM
8173 Address off = reloc.get_r_offset();
8174 if (size == 64
9055360d 8175 && target->abiversion() < 2
bfdfa4cd
AM
8176 && data_shndx == ppc_object->opd_shndx()
8177 && ppc_object->get_opd_discard(off - 8))
8178 break;
8179
dd93cd0a 8180 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
a1251fdc 8181 Address got_off = got->g_o_t();
dd93cd0a
AM
8182 rela_dyn->add_output_section_relative(got->output_section(),
8183 elfcpp::R_POWERPC_RELATIVE,
8184 output_section,
bfdfa4cd 8185 object, data_shndx, off,
a19da04b 8186 got_off);
dd93cd0a
AM
8187 }
8188 }
42cacb20
DE
8189 break;
8190
8191 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 8192 case elfcpp::R_PPC64_UADDR64:
42cacb20 8193 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8194 case elfcpp::R_POWERPC_UADDR32:
8195 case elfcpp::R_POWERPC_ADDR24:
c9269dff 8196 case elfcpp::R_POWERPC_ADDR16:
42cacb20 8197 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
8198 case elfcpp::R_POWERPC_ADDR16_HI:
8199 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8200 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8201 case elfcpp::R_PPC64_ADDR16_HIGH:
8202 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8203 case elfcpp::R_PPC64_ADDR16_HIGHER:
8204 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8205 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8206 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8207 case elfcpp::R_PPC64_ADDR16_DS:
8208 case elfcpp::R_PPC64_ADDR16_LO_DS:
8209 case elfcpp::R_POWERPC_ADDR14:
8210 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8211 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
8212 // If building a shared library (or a position-independent
8213 // executable), we need to create a dynamic relocation for
8214 // this location.
c9824451 8215 if (parameters->options().output_is_position_independent()
9055360d 8216 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 8217 {
b3ccdeb5
AM
8218 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8219 is_ifunc);
1f98a074 8220 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
8221 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8222 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 8223 {
b3ccdeb5
AM
8224 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8225 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8226 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
8227 output_section, data_shndx,
8228 reloc.get_r_offset(),
c9824451 8229 reloc.get_r_addend(), false);
2e702c99 8230 }
1f98a074 8231 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 8232 {
dd93cd0a 8233 check_non_pic(object, r_type);
dd93cd0a
AM
8234 rela_dyn->add_local(object, r_sym, r_type, output_section,
8235 data_shndx, reloc.get_r_offset(),
8236 reloc.get_r_addend());
2e702c99 8237 }
1f98a074
AM
8238 else
8239 {
8240 gold_assert(lsym.get_st_value() == 0);
8241 unsigned int shndx = lsym.get_st_shndx();
8242 bool is_ordinary;
8243 shndx = object->adjust_sym_shndx(r_sym, shndx,
8244 &is_ordinary);
8245 if (!is_ordinary)
8246 object->error(_("section symbol %u has bad shndx %u"),
8247 r_sym, shndx);
8248 else
8249 rela_dyn->add_local_section(object, shndx, r_type,
8250 output_section, data_shndx,
8251 reloc.get_r_offset());
8252 }
2e702c99 8253 }
42cacb20
DE
8254 break;
8255
e4dff765
AM
8256 case elfcpp::R_PPC64_PLT_PCREL34:
8257 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
8258 case elfcpp::R_POWERPC_PLT16_LO:
8259 case elfcpp::R_POWERPC_PLT16_HI:
8260 case elfcpp::R_POWERPC_PLT16_HA:
8261 case elfcpp::R_PPC64_PLT16_LO_DS:
8262 if (!is_ifunc)
8263 {
8264 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
f21dbd7c 8265 target->make_local_plt_entry(symtab, layout, object, r_sym);
2d7ad24e
AM
8266 }
8267 break;
8268
32f59844
AM
8269 case elfcpp::R_PPC64_REL24_NOTOC:
8270 if (size == 32)
8271 break;
8272 // Fall through.
eb97d024 8273 case elfcpp::R_PPC64_REL24_P9NOTOC:
42cacb20 8274 case elfcpp::R_POWERPC_REL24:
c9824451 8275 case elfcpp::R_PPC_PLTREL24:
42cacb20 8276 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
8277 case elfcpp::R_POWERPC_REL14:
8278 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8279 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8280 if (!is_ifunc)
0e123f69
AM
8281 {
8282 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8283 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8284 r_type, r_sym, reloc.get_r_addend());
8285 }
ec661b9d
AM
8286 break;
8287
7e57d19e
AM
8288 case elfcpp::R_PPC64_TOCSAVE:
8289 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8290 // caller has already saved r2 and thus a plt call stub need not
8291 // save r2.
8292 if (size == 64
8293 && target->mark_pltcall(ppc_object, data_shndx,
8294 reloc.get_r_offset() - 4, symtab))
8295 {
8296 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8297 unsigned int shndx = lsym.get_st_shndx();
8298 bool is_ordinary;
8299 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8300 if (!is_ordinary)
8301 object->error(_("tocsave symbol %u has bad shndx %u"),
8302 r_sym, shndx);
8303 else
8304 target->add_tocsave(ppc_object, shndx,
8305 lsym.get_st_value() + reloc.get_r_addend());
8306 }
8307 break;
8308
ec661b9d
AM
8309 case elfcpp::R_PPC64_REL64:
8310 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8311 case elfcpp::R_POWERPC_REL16:
6ce78956 8312 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 8313 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 8314 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8315 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8316 case elfcpp::R_PPC64_REL16_HIGH:
8317 case elfcpp::R_PPC64_REL16_HIGHA:
8318 case elfcpp::R_PPC64_REL16_HIGHER:
8319 case elfcpp::R_PPC64_REL16_HIGHERA:
8320 case elfcpp::R_PPC64_REL16_HIGHEST:
8321 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8322 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8323 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8324 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8325 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8326 case elfcpp::R_PPC64_SECTOFF_DS:
8327 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8328 case elfcpp::R_POWERPC_TPREL16:
8329 case elfcpp::R_POWERPC_TPREL16_LO:
8330 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8331 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8332 case elfcpp::R_PPC64_TPREL16_DS:
8333 case elfcpp::R_PPC64_TPREL16_LO_DS:
8334 case elfcpp::R_PPC64_TPREL16_HIGH:
8335 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8336 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8337 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8338 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8339 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8340 case elfcpp::R_POWERPC_DTPREL16:
8341 case elfcpp::R_POWERPC_DTPREL16_LO:
8342 case elfcpp::R_POWERPC_DTPREL16_HI:
8343 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8344 case elfcpp::R_PPC64_DTPREL16_DS:
8345 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8346 case elfcpp::R_PPC64_DTPREL16_HIGH:
8347 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8348 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8349 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8350 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8351 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8352 case elfcpp::R_PPC64_TLSGD:
8353 case elfcpp::R_PPC64_TLSLD:
45965137 8354 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
8355 break;
8356
e4dff765 8357 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8358 case elfcpp::R_POWERPC_GOT16:
8359 case elfcpp::R_POWERPC_GOT16_LO:
8360 case elfcpp::R_POWERPC_GOT16_HI:
8361 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8362 case elfcpp::R_PPC64_GOT16_DS:
8363 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8364 {
c9269dff 8365 // The symbol requires a GOT entry.
f19c3684
AM
8366 Got_type got_type = ((size == 32
8367 || r_type == elfcpp::R_POWERPC_GOT16
8368 || r_type == elfcpp::R_PPC64_GOT16_DS)
8369 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
dd93cd0a 8370 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8371 = target->got_section(symtab, layout, got_type);
dd93cd0a 8372 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8373 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
42cacb20 8374
e5d5f5ed 8375 if (!parameters->options().output_is_position_independent())
42cacb20 8376 {
b01a4b04
AM
8377 if (is_ifunc
8378 && (size == 32 || target->abiversion() >= 2))
f19c3684 8379 got->add_local_plt(object, r_sym, got_type, addend);
e5d5f5ed 8380 else
f19c3684 8381 got->add_local(object, r_sym, got_type, addend);
e5d5f5ed 8382 }
f19c3684 8383 else if (!object->local_has_got_offset(r_sym, got_type, addend))
e5d5f5ed
AM
8384 {
8385 // If we are generating a shared object or a pie, this
8386 // symbol's GOT entry will be set by a dynamic relocation.
8387 unsigned int off;
8388 off = got->add_constant(0);
f19c3684 8389 object->set_local_got_offset(r_sym, got_type, off, addend);
42cacb20 8390
b3ccdeb5
AM
8391 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8392 is_ifunc);
8393 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8394 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8395 rela_dyn->add_local_relative(object, r_sym, dynrel,
5cdb4f14 8396 got, off, addend, false);
2e702c99 8397 }
42cacb20
DE
8398 }
8399 break;
8400
cf43a2fe
AM
8401 case elfcpp::R_PPC64_TOC16:
8402 case elfcpp::R_PPC64_TOC16_LO:
8403 case elfcpp::R_PPC64_TOC16_HI:
8404 case elfcpp::R_PPC64_TOC16_HA:
8405 case elfcpp::R_PPC64_TOC16_DS:
8406 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20 8407 // We need a GOT section.
f19c3684 8408 target->got_section(symtab, layout, GOT_TYPE_SMALL);
42cacb20
DE
8409 break;
8410
87c69f97 8411 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8412 case elfcpp::R_POWERPC_GOT_TLSGD16:
8413 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8414 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8415 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8416 {
c7fdac09 8417 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
dd93cd0a
AM
8418 if (tls_type == tls::TLSOPT_NONE)
8419 {
f19c3684
AM
8420 Got_type got_type = ((size == 32
8421 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8422 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
dd93cd0a 8423 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8424 = target->got_section(symtab, layout, got_type);
dd93cd0a 8425 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8426 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
bd73a62d 8427 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
f19c3684 8428 got->add_local_tls_pair(object, r_sym, got_type,
5cdb4f14
AM
8429 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8430 addend);
dd93cd0a
AM
8431 }
8432 else if (tls_type == tls::TLSOPT_TO_LE)
8433 {
8434 // no GOT relocs needed for Local Exec.
8435 }
8436 else
8437 gold_unreachable();
8438 }
42cacb20
DE
8439 break;
8440
87c69f97 8441 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8442 case elfcpp::R_POWERPC_GOT_TLSLD16:
8443 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8444 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8445 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8446 {
c7fdac09 8447 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
8448 if (tls_type == tls::TLSOPT_NONE)
8449 target->tlsld_got_offset(symtab, layout, object);
8450 else if (tls_type == tls::TLSOPT_TO_LE)
8451 {
8452 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8453 if (parameters->options().emit_relocs())
8454 {
8455 Output_section* os = layout->tls_segment()->first_section();
8456 gold_assert(os != NULL);
8457 os->set_needs_symtab_index();
8458 }
dd93cd0a
AM
8459 }
8460 else
8461 gold_unreachable();
8462 }
42cacb20 8463 break;
42cacb20 8464
87c69f97 8465 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
8466 case elfcpp::R_POWERPC_GOT_DTPREL16:
8467 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8468 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8469 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8470 {
f19c3684
AM
8471 Got_type got_type = ((size == 32
8472 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8473 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 8474 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8475 = target->got_section(symtab, layout, got_type);
dd93cd0a 8476 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8477 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684 8478 got->add_local_tls(object, r_sym, got_type, addend);
dd93cd0a
AM
8479 }
8480 break;
42cacb20 8481
87c69f97 8482 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
8483 case elfcpp::R_POWERPC_GOT_TPREL16:
8484 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8485 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8486 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8487 {
0af4fcc2 8488 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
dd93cd0a
AM
8489 if (tls_type == tls::TLSOPT_NONE)
8490 {
dd93cd0a 8491 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8492 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684
AM
8493 Got_type got_type = ((size == 32
8494 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8495 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8496 if (!object->local_has_got_offset(r_sym, got_type, addend))
acc276d8
AM
8497 {
8498 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8499 = target->got_section(symtab, layout, got_type);
acc276d8 8500 unsigned int off = got->add_constant(0);
f19c3684 8501 object->set_local_got_offset(r_sym, got_type, off, addend);
acc276d8
AM
8502
8503 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8504 rela_dyn->add_symbolless_local_addend(object, r_sym,
8505 elfcpp::R_POWERPC_TPREL,
5cdb4f14 8506 got, off, addend);
acc276d8 8507 }
dd93cd0a
AM
8508 }
8509 else if (tls_type == tls::TLSOPT_TO_LE)
8510 {
8511 // no GOT relocs needed for Local Exec.
8512 }
8513 else
8514 gold_unreachable();
8515 }
8516 break;
8517
8518 default:
8519 unsupported_reloc_local(object, r_type);
8520 break;
8521 }
d8f5a274 8522
5edad15d
AM
8523 if (size == 64
8524 && parameters->options().toc_optimize())
8525 {
8526 if (data_shndx == ppc_object->toc_shndx())
8527 {
8528 bool ok = true;
8529 if (r_type != elfcpp::R_PPC64_ADDR64
8530 || (is_ifunc && target->abiversion() < 2))
8531 ok = false;
8532 else if (parameters->options().output_is_position_independent())
8533 {
8534 if (is_ifunc)
8535 ok = false;
8536 else
8537 {
8538 unsigned int shndx = lsym.get_st_shndx();
8539 if (shndx >= elfcpp::SHN_LORESERVE
8540 && shndx != elfcpp::SHN_XINDEX)
8541 ok = false;
8542 }
8543 }
8544 if (!ok)
8545 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8546 }
8547
8548 enum {no_check, check_lo, check_ha} insn_check;
8549 switch (r_type)
8550 {
8551 default:
8552 insn_check = no_check;
8553 break;
8554
8555 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8556 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8557 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8558 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8559 case elfcpp::R_POWERPC_GOT16_HA:
8560 case elfcpp::R_PPC64_TOC16_HA:
8561 insn_check = check_ha;
8562 break;
8563
8564 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8565 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8566 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8567 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8568 case elfcpp::R_POWERPC_GOT16_LO:
8569 case elfcpp::R_PPC64_GOT16_LO_DS:
8570 case elfcpp::R_PPC64_TOC16_LO:
8571 case elfcpp::R_PPC64_TOC16_LO_DS:
8572 insn_check = check_lo;
8573 break;
8574 }
8575
8576 section_size_type slen;
8577 const unsigned char* view = NULL;
8578 if (insn_check != no_check)
8579 {
8580 view = ppc_object->section_contents(data_shndx, &slen, false);
8581 section_size_type off =
8582 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8583 if (off < slen)
8584 {
8585 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8586 if (insn_check == check_lo
8587 ? !ok_lo_toc_insn(insn, r_type)
8588 : ((insn & ((0x3f << 26) | 0x1f << 16))
8589 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8590 {
8591 ppc_object->set_no_toc_opt();
8592 gold_warning(_("%s: toc optimization is not supported "
8593 "for %#08x instruction"),
8594 ppc_object->name().c_str(), insn);
8595 }
8596 }
8597 }
8598
8599 switch (r_type)
8600 {
8601 default:
8602 break;
8603 case elfcpp::R_PPC64_TOC16:
8604 case elfcpp::R_PPC64_TOC16_LO:
8605 case elfcpp::R_PPC64_TOC16_HI:
8606 case elfcpp::R_PPC64_TOC16_HA:
8607 case elfcpp::R_PPC64_TOC16_DS:
8608 case elfcpp::R_PPC64_TOC16_LO_DS:
8609 unsigned int shndx = lsym.get_st_shndx();
8610 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8611 bool is_ordinary;
8612 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8613 if (is_ordinary && shndx == ppc_object->toc_shndx())
8614 {
412294da 8615 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8616 if (dst_off < ppc_object->section_size(shndx))
8617 {
8618 bool ok = false;
8619 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8620 ok = true;
8621 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8622 {
8623 // Need to check that the insn is a ld
8624 if (!view)
8625 view = ppc_object->section_contents(data_shndx,
8626 &slen,
8627 false);
8628 section_size_type off =
8629 (convert_to_section_size_type(reloc.get_r_offset())
8630 + (big_endian ? -2 : 3));
8631 if (off < slen
8632 && (view[off] & (0x3f << 2)) == 58u << 2)
8633 ok = true;
8634 }
8635 if (!ok)
8636 ppc_object->set_no_toc_opt(dst_off);
8637 }
8638 }
8639 break;
8640 }
8641 }
8642
f159cdb6
AM
8643 if (size == 32)
8644 {
8645 switch (r_type)
8646 {
8647 case elfcpp::R_POWERPC_REL32:
8648 if (ppc_object->got2_shndx() != 0
8649 && parameters->options().output_is_position_independent())
8650 {
8651 unsigned int shndx = lsym.get_st_shndx();
8652 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8653 bool is_ordinary;
8654 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8655 if (is_ordinary && shndx == ppc_object->got2_shndx()
8656 && (ppc_object->section_flags(data_shndx)
8657 & elfcpp::SHF_EXECINSTR) != 0)
8658 gold_error(_("%s: unsupported -mbss-plt code"),
8659 ppc_object->name().c_str());
8660 }
8661 break;
8662 default:
8663 break;
8664 }
8665 }
8666
d8f5a274
AM
8667 switch (r_type)
8668 {
8669 case elfcpp::R_POWERPC_GOT_TLSLD16:
8670 case elfcpp::R_POWERPC_GOT_TLSGD16:
8671 case elfcpp::R_POWERPC_GOT_TPREL16:
8672 case elfcpp::R_POWERPC_GOT_DTPREL16:
8673 case elfcpp::R_POWERPC_GOT16:
8674 case elfcpp::R_PPC64_GOT16_DS:
8675 case elfcpp::R_PPC64_TOC16:
8676 case elfcpp::R_PPC64_TOC16_DS:
8677 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8678 break;
8679 default:
8680 break;
8681 }
8682
8683 switch (r_type)
8684 {
89c52ae3
AM
8685 case elfcpp::R_PPC64_TPREL16_DS:
8686 case elfcpp::R_PPC64_TPREL16_LO_DS:
8687 case elfcpp::R_PPC64_TPREL16_HIGH:
8688 case elfcpp::R_PPC64_TPREL16_HIGHA:
8689 case elfcpp::R_PPC64_TPREL16_HIGHER:
8690 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8691 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8692 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8693 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
8694 if (size != 64)
8695 break;
8696 // Fall through.
8697 case elfcpp::R_POWERPC_TPREL16:
8698 case elfcpp::R_POWERPC_TPREL16_LO:
8699 case elfcpp::R_POWERPC_TPREL16_HI:
8700 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8701 layout->set_has_static_tls();
8702 break;
8703 default:
8704 break;
8705 }
8706
252dcdf4
AM
8707 switch (r_type)
8708 {
8709 case elfcpp::R_POWERPC_TPREL16_HA:
8710 if (target->tprel_opt())
8711 {
8712 section_size_type slen;
8713 const unsigned char* view = NULL;
8714 view = ppc_object->section_contents(data_shndx, &slen, false);
8715 section_size_type off
8716 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8717 if (off < slen)
8718 {
8719 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8720 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8721 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 8722 target->set_no_tprel_opt();
252dcdf4
AM
8723 }
8724 }
8725 break;
8726
8727 case elfcpp::R_PPC64_TPREL16_HIGH:
8728 case elfcpp::R_PPC64_TPREL16_HIGHA:
8729 case elfcpp::R_PPC64_TPREL16_HIGHER:
8730 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8731 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8732 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8733 if (size != 64)
8734 break;
8735 // Fall through.
8736 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 8737 target->set_no_tprel_opt();
252dcdf4
AM
8738 break;
8739 default:
8740 break;
8741 }
8742
89c52ae3
AM
8743 switch (r_type)
8744 {
8745 case elfcpp::R_PPC64_D34:
8746 case elfcpp::R_PPC64_D34_LO:
8747 case elfcpp::R_PPC64_D34_HI30:
8748 case elfcpp::R_PPC64_D34_HA30:
8749 case elfcpp::R_PPC64_D28:
8750 case elfcpp::R_PPC64_PCREL34:
8751 case elfcpp::R_PPC64_PCREL28:
8752 case elfcpp::R_PPC64_TPREL34:
8753 case elfcpp::R_PPC64_DTPREL34:
8754 case elfcpp::R_PPC64_PLT_PCREL34:
8755 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8756 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
8757 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8758 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8759 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8760 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 8761 target->set_power10_relocs();
89c52ae3 8762 break;
d8f5a274
AM
8763 default:
8764 break;
8765 }
dd93cd0a
AM
8766}
8767
8768// Report an unsupported relocation against a global symbol.
8769
8770template<int size, bool big_endian>
8771void
8772Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8773 Sized_relobj_file<size, big_endian>* object,
8774 unsigned int r_type,
8775 Symbol* gsym)
8776{
42cacb20
DE
8777 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8778 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8779}
8780
8781// Scan a relocation for a global symbol.
8782
8783template<int size, bool big_endian>
8784inline void
8785Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8786 Symbol_table* symtab,
8787 Layout* layout,
8788 Target_powerpc<size, big_endian>* target,
8789 Sized_relobj_file<size, big_endian>* object,
8790 unsigned int data_shndx,
8791 Output_section* output_section,
8792 const elfcpp::Rela<size, big_endian>& reloc,
8793 unsigned int r_type,
8794 Symbol* gsym)
42cacb20 8795{
0af4fcc2
AM
8796 Powerpc_relobj<size, big_endian>* ppc_object
8797 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8798
8799 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8800 {
0af4fcc2
AM
8801 case Track_tls::SKIP:
8802 return;
8803 default:
8804 break;
8805 }
e3deeb9c 8806
34e0882b
AM
8807 if (target->replace_tls_get_addr(gsym))
8808 // Change a __tls_get_addr reference to __tls_get_addr_opt
8809 // so dynamic relocs are emitted against the latter symbol.
8810 gsym = target->tls_get_addr_opt();
8811
e3deeb9c
AM
8812 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8813 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8814 {
8815 this->expect_tls_get_addr_call();
c7fdac09
AM
8816 bool final = gsym->final_value_is_known();
8817 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8818 if (tls_type != tls::TLSOPT_NONE)
8819 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8820 }
8821 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8822 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8823 {
8824 this->expect_tls_get_addr_call();
c7fdac09
AM
8825 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8826 if (tls_type != tls::TLSOPT_NONE)
8827 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8828 }
8829
e5d5f5ed 8830 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8831 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8832 bool pushed_ifunc = false;
8833 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8834 {
0e123f69 8835 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8836 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8837 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8838 target->make_plt_entry(symtab, layout, gsym);
9055360d 8839 pushed_ifunc = true;
ec661b9d 8840 }
e5d5f5ed 8841
42cacb20
DE
8842 switch (r_type)
8843 {
8844 case elfcpp::R_POWERPC_NONE:
8845 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8846 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8847 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8848 case elfcpp::R_POWERPC_TLS:
549dba71 8849 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8850 case elfcpp::R_POWERPC_PLTSEQ:
8851 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8852 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8853 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8854 case elfcpp::R_PPC64_PCREL_OPT:
8855 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8856 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8857 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8858 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8859 case elfcpp::R_PPC64_REL16_HIGHER34:
8860 case elfcpp::R_PPC64_REL16_HIGHERA34:
8861 case elfcpp::R_PPC64_REL16_HIGHEST34:
8862 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8863 case elfcpp::R_PPC64_D34:
8864 case elfcpp::R_PPC64_D34_LO:
8865 case elfcpp::R_PPC64_D34_HI30:
8866 case elfcpp::R_PPC64_D34_HA30:
8867 case elfcpp::R_PPC64_D28:
8868 case elfcpp::R_PPC64_PCREL34:
8869 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8870 case elfcpp::R_PPC64_TPREL34:
8871 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8872 break;
8873
8874 case elfcpp::R_PPC64_TOC:
8875 {
8876 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8877 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a
AM
8878 if (parameters->options().output_is_position_independent())
8879 {
bfdfa4cd
AM
8880 Address off = reloc.get_r_offset();
8881 if (size == 64
8882 && data_shndx == ppc_object->opd_shndx()
8883 && ppc_object->get_opd_discard(off - 8))
8884 break;
8885
dd93cd0a 8886 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
a1251fdc 8887 Address got_off = got->g_o_t();
dd93cd0a
AM
8888 rela_dyn->add_output_section_relative(got->output_section(),
8889 elfcpp::R_POWERPC_RELATIVE,
8890 output_section,
bfdfa4cd 8891 object, data_shndx, off,
a19da04b 8892 got_off);
dd93cd0a
AM
8893 }
8894 }
42cacb20
DE
8895 break;
8896
c9269dff 8897 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8898 if (size == 64
9055360d 8899 && target->abiversion() < 2
bfdfa4cd
AM
8900 && data_shndx == ppc_object->opd_shndx()
8901 && (gsym->is_defined_in_discarded_section()
8902 || gsym->object() != object))
8903 {
8904 ppc_object->set_opd_discard(reloc.get_r_offset());
8905 break;
8906 }
d8e90251 8907 // Fall through.
dd93cd0a 8908 case elfcpp::R_PPC64_UADDR64:
c9269dff 8909 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8910 case elfcpp::R_POWERPC_UADDR32:
8911 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8912 case elfcpp::R_POWERPC_ADDR16:
8913 case elfcpp::R_POWERPC_ADDR16_LO:
8914 case elfcpp::R_POWERPC_ADDR16_HI:
8915 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8916 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8917 case elfcpp::R_PPC64_ADDR16_HIGH:
8918 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8919 case elfcpp::R_PPC64_ADDR16_HIGHER:
8920 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8921 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8922 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8923 case elfcpp::R_PPC64_ADDR16_DS:
8924 case elfcpp::R_PPC64_ADDR16_LO_DS:
8925 case elfcpp::R_POWERPC_ADDR14:
8926 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8927 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8928 {
c9269dff
AM
8929 // Make a PLT entry if necessary.
8930 if (gsym->needs_plt_entry())
8931 {
9055360d
AM
8932 // Since this is not a PC-relative relocation, we may be
8933 // taking the address of a function. In that case we need to
8934 // set the entry in the dynamic symbol table to the address of
8935 // the PLT call stub.
8936 bool need_ifunc_plt = false;
8937 if ((size == 32 || target->abiversion() >= 2)
8938 && gsym->is_from_dynobj()
8939 && !parameters->options().output_is_position_independent())
8940 {
8941 gsym->set_needs_dynsym_value();
8942 need_ifunc_plt = true;
8943 }
8944 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8945 {
0e123f69 8946 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8947 target->push_branch(ppc_object, data_shndx,
0e123f69 8948 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8949 reloc.get_r_addend());
8950 target->make_plt_entry(symtab, layout, gsym);
8951 }
c9269dff
AM
8952 }
8953 // Make a dynamic relocation if necessary.
88b8e639 8954 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8955 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8956 {
a82bef93
ST
8957 if (!parameters->options().output_is_position_independent()
8958 && gsym->may_need_copy_reloc())
c9269dff
AM
8959 {
8960 target->copy_reloc(symtab, layout, object,
8961 data_shndx, output_section, gsym, reloc);
8962 }
9055360d
AM
8963 else if ((((size == 32
8964 && r_type == elfcpp::R_POWERPC_ADDR32)
8965 || (size == 64
8966 && r_type == elfcpp::R_PPC64_ADDR64
8967 && target->abiversion() >= 2))
627b30b7
AM
8968 && gsym->can_use_relative_reloc(false)
8969 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8970 && parameters->options().shared()))
8971 || (size == 64
8972 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8973 && target->abiversion() < 2
627b30b7
AM
8974 && (gsym->can_use_relative_reloc(false)
8975 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8976 {
b3ccdeb5
AM
8977 Reloc_section* rela_dyn
8978 = target->rela_dyn_section(symtab, layout, is_ifunc);
8979 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8980 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8981 rela_dyn->add_symbolless_global_addend(
8982 gsym, dynrel, output_section, object, data_shndx,
8983 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
8984 }
8985 else
8986 {
b3ccdeb5
AM
8987 Reloc_section* rela_dyn
8988 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 8989 check_non_pic(object, r_type);
dd93cd0a
AM
8990 rela_dyn->add_global(gsym, r_type, output_section,
8991 object, data_shndx,
8992 reloc.get_r_offset(),
8993 reloc.get_r_addend());
5edad15d
AM
8994
8995 if (size == 64
8996 && parameters->options().toc_optimize()
8997 && data_shndx == ppc_object->toc_shndx())
8998 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
8999 }
9000 }
42cacb20
DE
9001 }
9002 break;
9003
e4dff765
AM
9004 case elfcpp::R_PPC64_PLT_PCREL34:
9005 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
9006 case elfcpp::R_POWERPC_PLT16_LO:
9007 case elfcpp::R_POWERPC_PLT16_HI:
9008 case elfcpp::R_POWERPC_PLT16_HA:
9009 case elfcpp::R_PPC64_PLT16_LO_DS:
9010 if (!pushed_ifunc)
f21dbd7c
AM
9011 {
9012 if (!parameters->doing_static_link())
9013 target->make_plt_entry(symtab, layout, gsym);
9014 else
9015 target->make_local_plt_entry(symtab, layout, gsym);
9016 }
08be3224
AM
9017 break;
9018
32f59844
AM
9019 case elfcpp::R_PPC64_REL24_NOTOC:
9020 if (size == 32)
9021 break;
9022 // Fall through.
eb97d024 9023 case elfcpp::R_PPC64_REL24_P9NOTOC:
cf43a2fe 9024 case elfcpp::R_PPC_PLTREL24:
42cacb20 9025 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
9026 if (!is_ifunc)
9027 {
0e123f69 9028 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 9029 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 9030 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
9031 if (gsym->needs_plt_entry()
9032 || (!gsym->final_value_is_known()
9033 && (gsym->is_undefined()
9034 || gsym->is_from_dynobj()
9035 || gsym->is_preemptible())))
9036 target->make_plt_entry(symtab, layout, gsym);
9037 }
d8e90251 9038 // Fall through.
42cacb20 9039
3ea0a085 9040 case elfcpp::R_PPC64_REL64:
dd93cd0a 9041 case elfcpp::R_POWERPC_REL32:
3ea0a085 9042 // Make a dynamic relocation if necessary.
88b8e639 9043 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 9044 {
a82bef93
ST
9045 if (!parameters->options().output_is_position_independent()
9046 && gsym->may_need_copy_reloc())
3ea0a085
AM
9047 {
9048 target->copy_reloc(symtab, layout, object,
9049 data_shndx, output_section, gsym,
9050 reloc);
9051 }
9052 else
9053 {
b3ccdeb5
AM
9054 Reloc_section* rela_dyn
9055 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
9056 check_non_pic(object, r_type);
9057 rela_dyn->add_global(gsym, r_type, output_section, object,
9058 data_shndx, reloc.get_r_offset(),
9059 reloc.get_r_addend());
9060 }
9061 }
9062 break;
9063
ec661b9d
AM
9064 case elfcpp::R_POWERPC_REL14:
9065 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9066 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 9067 if (!is_ifunc)
0e123f69
AM
9068 {
9069 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9070 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9071 r_type, r_sym, reloc.get_r_addend());
9072 }
ec661b9d
AM
9073 break;
9074
7e57d19e
AM
9075 case elfcpp::R_PPC64_TOCSAVE:
9076 // R_PPC64_TOCSAVE follows a call instruction to indicate the
9077 // caller has already saved r2 and thus a plt call stub need not
9078 // save r2.
9079 if (size == 64
9080 && target->mark_pltcall(ppc_object, data_shndx,
9081 reloc.get_r_offset() - 4, symtab))
9082 {
9083 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9084 bool is_ordinary;
9085 unsigned int shndx = gsym->shndx(&is_ordinary);
9086 if (!is_ordinary)
9087 object->error(_("tocsave symbol %u has bad shndx %u"),
9088 r_sym, shndx);
9089 else
9090 {
9091 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9092 target->add_tocsave(ppc_object, shndx,
9093 sym->value() + reloc.get_r_addend());
9094 }
9095 }
9096 break;
9097
6ce78956
AM
9098 case elfcpp::R_POWERPC_REL16:
9099 case elfcpp::R_POWERPC_REL16_LO:
9100 case elfcpp::R_POWERPC_REL16_HI:
9101 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 9102 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
9103 case elfcpp::R_PPC64_REL16_HIGH:
9104 case elfcpp::R_PPC64_REL16_HIGHA:
9105 case elfcpp::R_PPC64_REL16_HIGHER:
9106 case elfcpp::R_PPC64_REL16_HIGHERA:
9107 case elfcpp::R_PPC64_REL16_HIGHEST:
9108 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 9109 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 9110 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 9111 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 9112 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
9113 case elfcpp::R_PPC64_SECTOFF_DS:
9114 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9115 case elfcpp::R_POWERPC_TPREL16:
9116 case elfcpp::R_POWERPC_TPREL16_LO:
9117 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 9118 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
9119 case elfcpp::R_PPC64_TPREL16_DS:
9120 case elfcpp::R_PPC64_TPREL16_LO_DS:
9121 case elfcpp::R_PPC64_TPREL16_HIGH:
9122 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 9123 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 9124 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 9125 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 9126 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
9127 case elfcpp::R_POWERPC_DTPREL16:
9128 case elfcpp::R_POWERPC_DTPREL16_LO:
9129 case elfcpp::R_POWERPC_DTPREL16_HI:
9130 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
9131 case elfcpp::R_PPC64_DTPREL16_DS:
9132 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
9133 case elfcpp::R_PPC64_DTPREL16_HIGH:
9134 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9135 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9136 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9137 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9138 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
9139 case elfcpp::R_PPC64_TLSGD:
9140 case elfcpp::R_PPC64_TLSLD:
45965137 9141 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
9142 break;
9143
e4dff765 9144 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
9145 case elfcpp::R_POWERPC_GOT16:
9146 case elfcpp::R_POWERPC_GOT16_LO:
9147 case elfcpp::R_POWERPC_GOT16_HI:
9148 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
9149 case elfcpp::R_PPC64_GOT16_DS:
9150 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 9151 {
c9269dff
AM
9152 // The symbol requires a GOT entry.
9153 Output_data_got_powerpc<size, big_endian>* got;
5cdb4f14 9154 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684
AM
9155 Got_type got_type = ((size == 32
9156 || r_type == elfcpp::R_POWERPC_GOT16
9157 || r_type == elfcpp::R_PPC64_GOT16_DS)
9158 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
42cacb20 9159
f19c3684 9160 got = target->got_section(symtab, layout, got_type);
2e702c99 9161 if (gsym->final_value_is_known())
2e702c99 9162 {
b01a4b04
AM
9163 if (is_ifunc
9164 && (size == 32 || target->abiversion() >= 2))
f19c3684 9165 got->add_global_plt(gsym, got_type, addend);
e5d5f5ed 9166 else
f19c3684 9167 got->add_global(gsym, got_type, addend);
e5d5f5ed 9168 }
f19c3684 9169 else if (!gsym->has_got_offset(got_type, addend))
e5d5f5ed
AM
9170 {
9171 // If we are generating a shared object or a pie, this
9172 // symbol's GOT entry will be set by a dynamic relocation.
9173 unsigned int off = got->add_constant(0);
f19c3684 9174 gsym->set_got_offset(got_type, off, addend);
e5d5f5ed 9175
b3ccdeb5
AM
9176 Reloc_section* rela_dyn
9177 = target->rela_dyn_section(symtab, layout, is_ifunc);
9178
e5d5f5ed 9179 if (gsym->can_use_relative_reloc(false)
9055360d
AM
9180 && !((size == 32
9181 || target->abiversion() >= 2)
e5d5f5ed
AM
9182 && gsym->visibility() == elfcpp::STV_PROTECTED
9183 && parameters->options().shared()))
2e702c99 9184 {
b3ccdeb5
AM
9185 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9186 : elfcpp::R_POWERPC_RELATIVE);
5cdb4f14
AM
9187 rela_dyn->add_global_relative(gsym, dynrel, got, off,
9188 addend, false);
e5d5f5ed
AM
9189 }
9190 else
9191 {
9192 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5cdb4f14 9193 rela_dyn->add_global(gsym, dynrel, got, off, addend);
42cacb20 9194 }
2e702c99 9195 }
42cacb20
DE
9196 }
9197 break;
9198
cf43a2fe
AM
9199 case elfcpp::R_PPC64_TOC16:
9200 case elfcpp::R_PPC64_TOC16_LO:
9201 case elfcpp::R_PPC64_TOC16_HI:
9202 case elfcpp::R_PPC64_TOC16_HA:
9203 case elfcpp::R_PPC64_TOC16_DS:
9204 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20 9205 // We need a GOT section.
f19c3684 9206 target->got_section(symtab, layout, GOT_TYPE_SMALL);
42cacb20
DE
9207 break;
9208
87c69f97 9209 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
9210 case elfcpp::R_POWERPC_GOT_TLSGD16:
9211 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9212 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9213 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9214 {
c7fdac09
AM
9215 bool final = gsym->final_value_is_known();
9216 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
dd93cd0a
AM
9217 if (tls_type == tls::TLSOPT_NONE)
9218 {
f19c3684
AM
9219 Got_type got_type = ((size == 32
9220 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9221 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
dd93cd0a 9222 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9223 = target->got_section(symtab, layout, got_type);
b3ccdeb5 9224 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9225 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684 9226 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
dd93cd0a 9227 elfcpp::R_POWERPC_DTPMOD,
5cdb4f14
AM
9228 elfcpp::R_POWERPC_DTPREL,
9229 addend);
dd93cd0a
AM
9230 }
9231 else if (tls_type == tls::TLSOPT_TO_IE)
9232 {
f19c3684
AM
9233 Got_type got_type = ((size == 32
9234 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9235 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9236 if (!gsym->has_got_offset(got_type))
acc276d8
AM
9237 {
9238 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9239 = target->got_section(symtab, layout, got_type);
acc276d8 9240 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9241 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
acc276d8
AM
9242 if (gsym->is_undefined()
9243 || gsym->is_from_dynobj())
9244 {
f19c3684 9245 got->add_global_with_rel(gsym, got_type, rela_dyn,
5cdb4f14 9246 elfcpp::R_POWERPC_TPREL, addend);
acc276d8
AM
9247 }
9248 else
9249 {
9250 unsigned int off = got->add_constant(0);
f19c3684 9251 gsym->set_got_offset(got_type, off);
acc276d8
AM
9252 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9253 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5cdb4f14 9254 got, off, addend);
acc276d8
AM
9255 }
9256 }
dd93cd0a
AM
9257 }
9258 else if (tls_type == tls::TLSOPT_TO_LE)
9259 {
9260 // no GOT relocs needed for Local Exec.
9261 }
9262 else
9263 gold_unreachable();
9264 }
42cacb20
DE
9265 break;
9266
87c69f97 9267 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
9268 case elfcpp::R_POWERPC_GOT_TLSLD16:
9269 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9270 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9271 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9272 {
c7fdac09 9273 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
9274 if (tls_type == tls::TLSOPT_NONE)
9275 target->tlsld_got_offset(symtab, layout, object);
9276 else if (tls_type == tls::TLSOPT_TO_LE)
9277 {
9278 // no GOT relocs needed for Local Exec.
7404fe1b
AM
9279 if (parameters->options().emit_relocs())
9280 {
9281 Output_section* os = layout->tls_segment()->first_section();
9282 gold_assert(os != NULL);
9283 os->set_needs_symtab_index();
9284 }
dd93cd0a
AM
9285 }
9286 else
9287 gold_unreachable();
9288 }
9289 break;
9290
87c69f97 9291 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
9292 case elfcpp::R_POWERPC_GOT_DTPREL16:
9293 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9294 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9295 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9296 {
f19c3684
AM
9297 Got_type got_type = ((size == 32
9298 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9299 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 9300 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9301 = target->got_section(symtab, layout, got_type);
5cdb4f14 9302 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
bd73a62d
AM
9303 if (!gsym->final_value_is_known()
9304 && (gsym->is_from_dynobj()
9305 || gsym->is_undefined()
9306 || gsym->is_preemptible()))
f19c3684 9307 got->add_global_with_rel(gsym, got_type,
bd73a62d 9308 target->rela_dyn_section(layout),
5cdb4f14 9309 elfcpp::R_POWERPC_DTPREL, addend);
bd73a62d 9310 else
f19c3684 9311 got->add_global_tls(gsym, got_type, addend);
dd93cd0a
AM
9312 }
9313 break;
9314
87c69f97 9315 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
9316 case elfcpp::R_POWERPC_GOT_TPREL16:
9317 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9318 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9319 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9320 {
0af4fcc2
AM
9321 bool final = gsym->final_value_is_known();
9322 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
9323 if (tls_type == tls::TLSOPT_NONE)
9324 {
f19c3684
AM
9325 Got_type got_type = ((size == 32
9326 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9327 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9328 if (!gsym->has_got_offset(got_type))
acc276d8
AM
9329 {
9330 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9331 = target->got_section(symtab, layout, got_type);
acc276d8 9332 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9333 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
acc276d8
AM
9334 if (gsym->is_undefined()
9335 || gsym->is_from_dynobj())
9336 {
f19c3684 9337 got->add_global_with_rel(gsym, got_type, rela_dyn,
5cdb4f14 9338 elfcpp::R_POWERPC_TPREL, addend);
acc276d8
AM
9339 }
9340 else
9341 {
9342 unsigned int off = got->add_constant(0);
f19c3684 9343 gsym->set_got_offset(got_type, off);
acc276d8
AM
9344 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9345 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5cdb4f14 9346 got, off, addend);
acc276d8
AM
9347 }
9348 }
dd93cd0a
AM
9349 }
9350 else if (tls_type == tls::TLSOPT_TO_LE)
9351 {
9352 // no GOT relocs needed for Local Exec.
9353 }
9354 else
9355 gold_unreachable();
9356 }
42cacb20
DE
9357 break;
9358
9359 default:
9360 unsupported_reloc_global(object, r_type, gsym);
9361 break;
9362 }
d8f5a274 9363
5edad15d
AM
9364 if (size == 64
9365 && parameters->options().toc_optimize())
9366 {
9367 if (data_shndx == ppc_object->toc_shndx())
9368 {
9369 bool ok = true;
9370 if (r_type != elfcpp::R_PPC64_ADDR64
9371 || (is_ifunc && target->abiversion() < 2))
9372 ok = false;
9373 else if (parameters->options().output_is_position_independent()
9374 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9375 ok = false;
9376 if (!ok)
9377 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9378 }
9379
9380 enum {no_check, check_lo, check_ha} insn_check;
9381 switch (r_type)
9382 {
9383 default:
9384 insn_check = no_check;
9385 break;
9386
9387 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9388 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9389 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9390 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9391 case elfcpp::R_POWERPC_GOT16_HA:
9392 case elfcpp::R_PPC64_TOC16_HA:
9393 insn_check = check_ha;
9394 break;
9395
9396 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9397 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9398 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9399 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9400 case elfcpp::R_POWERPC_GOT16_LO:
9401 case elfcpp::R_PPC64_GOT16_LO_DS:
9402 case elfcpp::R_PPC64_TOC16_LO:
9403 case elfcpp::R_PPC64_TOC16_LO_DS:
9404 insn_check = check_lo;
9405 break;
9406 }
9407
9408 section_size_type slen;
9409 const unsigned char* view = NULL;
9410 if (insn_check != no_check)
9411 {
9412 view = ppc_object->section_contents(data_shndx, &slen, false);
9413 section_size_type off =
9414 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9415 if (off < slen)
9416 {
9417 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9418 if (insn_check == check_lo
9419 ? !ok_lo_toc_insn(insn, r_type)
9420 : ((insn & ((0x3f << 26) | 0x1f << 16))
9421 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9422 {
9423 ppc_object->set_no_toc_opt();
9424 gold_warning(_("%s: toc optimization is not supported "
9425 "for %#08x instruction"),
9426 ppc_object->name().c_str(), insn);
9427 }
9428 }
9429 }
9430
9431 switch (r_type)
9432 {
9433 default:
9434 break;
9435 case elfcpp::R_PPC64_TOC16:
9436 case elfcpp::R_PPC64_TOC16_LO:
9437 case elfcpp::R_PPC64_TOC16_HI:
9438 case elfcpp::R_PPC64_TOC16_HA:
9439 case elfcpp::R_PPC64_TOC16_DS:
9440 case elfcpp::R_PPC64_TOC16_LO_DS:
9441 if (gsym->source() == Symbol::FROM_OBJECT
9442 && !gsym->object()->is_dynamic())
9443 {
9444 Powerpc_relobj<size, big_endian>* sym_object
9445 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9446 bool is_ordinary;
9447 unsigned int shndx = gsym->shndx(&is_ordinary);
9448 if (shndx == sym_object->toc_shndx())
9449 {
9450 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 9451 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
9452 if (dst_off < sym_object->section_size(shndx))
9453 {
9454 bool ok = false;
9455 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9456 ok = true;
9457 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9458 {
9459 // Need to check that the insn is a ld
9460 if (!view)
9461 view = ppc_object->section_contents(data_shndx,
9462 &slen,
9463 false);
9464 section_size_type off =
9465 (convert_to_section_size_type(reloc.get_r_offset())
9466 + (big_endian ? -2 : 3));
9467 if (off < slen
9468 && (view[off] & (0x3f << 2)) == (58u << 2))
9469 ok = true;
9470 }
9471 if (!ok)
9472 sym_object->set_no_toc_opt(dst_off);
9473 }
9474 }
9475 }
9476 break;
9477 }
9478 }
9479
f159cdb6
AM
9480 if (size == 32)
9481 {
9482 switch (r_type)
9483 {
9484 case elfcpp::R_PPC_LOCAL24PC:
9485 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9486 gold_error(_("%s: unsupported -mbss-plt code"),
9487 ppc_object->name().c_str());
9488 break;
9489 default:
9490 break;
9491 }
9492 }
9493
d8f5a274
AM
9494 switch (r_type)
9495 {
9496 case elfcpp::R_POWERPC_GOT_TLSLD16:
9497 case elfcpp::R_POWERPC_GOT_TLSGD16:
9498 case elfcpp::R_POWERPC_GOT_TPREL16:
9499 case elfcpp::R_POWERPC_GOT_DTPREL16:
9500 case elfcpp::R_POWERPC_GOT16:
9501 case elfcpp::R_PPC64_GOT16_DS:
9502 case elfcpp::R_PPC64_TOC16:
9503 case elfcpp::R_PPC64_TOC16_DS:
9504 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
9505 break;
9506 default:
9507 break;
9508 }
9509
9510 switch (r_type)
9511 {
89c52ae3
AM
9512 case elfcpp::R_PPC64_TPREL16_DS:
9513 case elfcpp::R_PPC64_TPREL16_LO_DS:
9514 case elfcpp::R_PPC64_TPREL16_HIGH:
9515 case elfcpp::R_PPC64_TPREL16_HIGHA:
9516 case elfcpp::R_PPC64_TPREL16_HIGHER:
9517 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9518 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9519 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9520 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
9521 if (size != 64)
9522 break;
9523 // Fall through.
9524 case elfcpp::R_POWERPC_TPREL16:
9525 case elfcpp::R_POWERPC_TPREL16_LO:
9526 case elfcpp::R_POWERPC_TPREL16_HI:
9527 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
9528 layout->set_has_static_tls();
9529 break;
9530 default:
9531 break;
9532 }
9533
252dcdf4
AM
9534 switch (r_type)
9535 {
9536 case elfcpp::R_POWERPC_TPREL16_HA:
9537 if (target->tprel_opt())
9538 {
9539 section_size_type slen;
9540 const unsigned char* view = NULL;
9541 view = ppc_object->section_contents(data_shndx, &slen, false);
9542 section_size_type off
9543 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9544 if (off < slen)
9545 {
9546 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9547 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9548 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 9549 target->set_no_tprel_opt();
252dcdf4
AM
9550 }
9551 }
9552 break;
9553
9554 case elfcpp::R_PPC64_TPREL16_HIGH:
9555 case elfcpp::R_PPC64_TPREL16_HIGHA:
9556 case elfcpp::R_PPC64_TPREL16_HIGHER:
9557 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9558 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9559 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9560 if (size != 64)
9561 break;
9562 // Fall through.
9563 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 9564 target->set_no_tprel_opt();
252dcdf4
AM
9565 break;
9566 default:
9567 break;
9568 }
9569
89c52ae3
AM
9570 switch (r_type)
9571 {
9572 case elfcpp::R_PPC64_D34:
9573 case elfcpp::R_PPC64_D34_LO:
9574 case elfcpp::R_PPC64_D34_HI30:
9575 case elfcpp::R_PPC64_D34_HA30:
9576 case elfcpp::R_PPC64_D28:
9577 case elfcpp::R_PPC64_PCREL34:
9578 case elfcpp::R_PPC64_PCREL28:
9579 case elfcpp::R_PPC64_TPREL34:
9580 case elfcpp::R_PPC64_DTPREL34:
9581 case elfcpp::R_PPC64_PLT_PCREL34:
9582 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9583 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
9584 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9585 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9586 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9587 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 9588 target->set_power10_relocs();
89c52ae3 9589 break;
d8f5a274
AM
9590 default:
9591 break;
9592 }
42cacb20
DE
9593}
9594
6d03d481
ST
9595// Process relocations for gc.
9596
9597template<int size, bool big_endian>
9598void
9599Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
9600 Symbol_table* symtab,
9601 Layout* layout,
9602 Sized_relobj_file<size, big_endian>* object,
9603 unsigned int data_shndx,
9604 unsigned int,
9605 const unsigned char* prelocs,
9606 size_t reloc_count,
9607 Output_section* output_section,
9608 bool needs_special_offset_handling,
9609 size_t local_symbol_count,
9610 const unsigned char* plocal_symbols)
6d03d481
ST
9611{
9612 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9613 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9614 Classify_reloc;
9615
e81fea4d
AM
9616 Powerpc_relobj<size, big_endian>* ppc_object
9617 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9618 if (size == 64)
9619 ppc_object->set_opd_valid();
9620 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9621 {
9622 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9623 for (p = ppc_object->access_from_map()->begin();
9624 p != ppc_object->access_from_map()->end();
9625 ++p)
9626 {
9627 Address dst_off = p->first;
9628 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9629 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9630 for (s = p->second.begin(); s != p->second.end(); ++s)
9631 {
efc6fa12 9632 Relobj* src_obj = s->first;
e81fea4d
AM
9633 unsigned int src_indx = s->second;
9634 symtab->gc()->add_reference(src_obj, src_indx,
9635 ppc_object, dst_indx);
9636 }
9637 p->second.clear();
9638 }
9639 ppc_object->access_from_map()->clear();
c6de8ed4 9640 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
9641 // Don't look at .opd relocs as .opd will reference everything.
9642 return;
9643 }
6d03d481 9644
4d625b70 9645 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9646 symtab,
9647 layout,
9648 this,
9649 object,
9650 data_shndx,
9651 prelocs,
9652 reloc_count,
9653 output_section,
9654 needs_special_offset_handling,
9655 local_symbol_count,
9656 plocal_symbols);
9657}
9658
e81fea4d
AM
9659// Handle target specific gc actions when adding a gc reference from
9660// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9661// and DST_OFF. For powerpc64, this adds a referenc to the code
9662// section of a function descriptor.
9663
9664template<int size, bool big_endian>
9665void
9666Target_powerpc<size, big_endian>::do_gc_add_reference(
9667 Symbol_table* symtab,
efc6fa12 9668 Relobj* src_obj,
e81fea4d 9669 unsigned int src_shndx,
efc6fa12 9670 Relobj* dst_obj,
e81fea4d
AM
9671 unsigned int dst_shndx,
9672 Address dst_off) const
9673{
6c77229c
AM
9674 if (size != 64 || dst_obj->is_dynamic())
9675 return;
9676
e81fea4d
AM
9677 Powerpc_relobj<size, big_endian>* ppc_object
9678 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9679 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9680 {
9681 if (ppc_object->opd_valid())
9682 {
9683 dst_shndx = ppc_object->get_opd_ent(dst_off);
9684 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9685 }
9686 else
9687 {
9688 // If we haven't run scan_opd_relocs, we must delay
9689 // processing this function descriptor reference.
9690 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9691 }
9692 }
9693}
9694
9695// Add any special sections for this symbol to the gc work list.
9696// For powerpc64, this adds the code section of a function
9697// descriptor.
9698
9699template<int size, bool big_endian>
9700void
9701Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9702 Symbol_table* symtab,
9703 Symbol* sym) const
9704{
6a31512f 9705 if (size == 64 && sym->object()->pluginobj() == NULL)
e81fea4d
AM
9706 {
9707 Powerpc_relobj<size, big_endian>* ppc_object
9708 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9709 bool is_ordinary;
9710 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9711 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9712 {
9713 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9714 Address dst_off = gsym->value();
c6de8ed4
AM
9715 if (ppc_object->opd_valid())
9716 {
9717 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9718 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9719 dst_indx));
c6de8ed4
AM
9720 }
9721 else
9722 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9723 }
9724 }
9725}
9726
dc3714f3
AM
9727// For a symbol location in .opd, set LOC to the location of the
9728// function entry.
9729
9730template<int size, bool big_endian>
9731void
9732Target_powerpc<size, big_endian>::do_function_location(
9733 Symbol_location* loc) const
9734{
a2d7bf59 9735 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9736 {
9737 if (loc->object->is_dynamic())
9738 {
9739 Powerpc_dynobj<size, big_endian>* ppc_object
9740 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9741 if (loc->shndx == ppc_object->opd_shndx())
9742 {
9743 Address dest_off;
9744 Address off = loc->offset - ppc_object->opd_address();
9745 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9746 loc->offset = dest_off;
9747 }
9748 }
9749 else
9750 {
9751 const Powerpc_relobj<size, big_endian>* ppc_object
9752 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9753 if (loc->shndx == ppc_object->opd_shndx())
9754 {
9755 Address dest_off;
9756 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9757 loc->offset = dest_off;
9758 }
9759 }
9760 }
9761}
9762
bbec1a5d
AM
9763// FNOFFSET in section SHNDX in OBJECT is the start of a function
9764// compiled with -fsplit-stack. The function calls non-split-stack
9765// code. Change the function to ensure it has enough stack space to
9766// call some random function.
9767
9768template<int size, bool big_endian>
9769void
9770Target_powerpc<size, big_endian>::do_calls_non_split(
9771 Relobj* object,
9772 unsigned int shndx,
9773 section_offset_type fnoffset,
9774 section_size_type fnsize,
6e0813d3
CC
9775 const unsigned char* prelocs,
9776 size_t reloc_count,
bbec1a5d
AM
9777 unsigned char* view,
9778 section_size_type view_size,
9779 std::string* from,
9780 std::string* to) const
9781{
9782 // 32-bit not supported.
9783 if (size == 32)
9784 {
9785 // warn
9786 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9787 prelocs, reloc_count, view, view_size,
9788 from, to);
bbec1a5d
AM
9789 return;
9790 }
9791
9792 // The function always starts with
9793 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9794 // addis %r12,%r1,-allocate@ha
9795 // addi %r12,%r12,-allocate@l
9796 // cmpld %r12,%r0
9797 // but note that the addis or addi may be replaced with a nop
9798
9799 unsigned char *entry = view + fnoffset;
9800 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9801
9802 if ((insn & 0xffff0000) == addis_2_12)
9803 {
9804 /* Skip ELFv2 global entry code. */
9805 entry += 8;
9806 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9807 }
9808
9809 unsigned char *pinsn = entry;
9810 bool ok = false;
9811 const uint32_t ld_private_ss = 0xe80d8fc0;
9812 if (insn == ld_private_ss)
9813 {
9814 int32_t allocate = 0;
9815 while (1)
9816 {
9817 pinsn += 4;
9818 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9819 if ((insn & 0xffff0000) == addis_12_1)
9820 allocate += (insn & 0xffff) << 16;
9821 else if ((insn & 0xffff0000) == addi_12_1
9822 || (insn & 0xffff0000) == addi_12_12)
9823 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9824 else if (insn != nop)
9825 break;
9826 }
9827 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9828 {
9829 int extra = parameters->options().split_stack_adjust_size();
9830 allocate -= extra;
9831 if (allocate >= 0 || extra < 0)
9832 {
9833 object->error(_("split-stack stack size overflow at "
9834 "section %u offset %0zx"),
9835 shndx, static_cast<size_t>(fnoffset));
9836 return;
9837 }
9838 pinsn = entry + 4;
9839 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9840 if (insn != addis_12_1)
9841 {
9842 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9843 pinsn += 4;
9844 insn = addi_12_12 | (allocate & 0xffff);
9845 if (insn != addi_12_12)
9846 {
9847 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9848 pinsn += 4;
9849 }
9850 }
9851 else
9852 {
9853 insn = addi_12_1 | (allocate & 0xffff);
9854 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9855 pinsn += 4;
9856 }
9857 if (pinsn != entry + 12)
9858 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9859
9860 ok = true;
9861 }
9862 }
9863
9864 if (!ok)
9865 {
9866 if (!object->has_no_split_stack())
9867 object->error(_("failed to match split-stack sequence at "
9868 "section %u offset %0zx"),
9869 shndx, static_cast<size_t>(fnoffset));
9870 }
9871}
9872
42cacb20
DE
9873// Scan relocations for a section.
9874
9875template<int size, bool big_endian>
9876void
9877Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9878 Symbol_table* symtab,
9879 Layout* layout,
9880 Sized_relobj_file<size, big_endian>* object,
9881 unsigned int data_shndx,
9882 unsigned int sh_type,
9883 const unsigned char* prelocs,
9884 size_t reloc_count,
9885 Output_section* output_section,
9886 bool needs_special_offset_handling,
9887 size_t local_symbol_count,
9888 const unsigned char* plocal_symbols)
42cacb20
DE
9889{
9890 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9891 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9892 Classify_reloc;
42cacb20 9893
7ee7ff70
AM
9894 if (!this->plt_localentry0_init_)
9895 {
9896 bool plt_localentry0 = false;
9897 if (size == 64
9898 && this->abiversion() >= 2)
9899 {
9900 if (parameters->options().user_set_plt_localentry())
9901 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9902 if (plt_localentry0
9903 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9904 gold_warning(_("--plt-localentry is especially dangerous without "
9905 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9906 }
9907 this->plt_localentry0_ = plt_localentry0;
9908 this->plt_localentry0_init_ = true;
9909 }
9910
42cacb20
DE
9911 if (sh_type == elfcpp::SHT_REL)
9912 {
9913 gold_error(_("%s: unsupported REL reloc section"),
9914 object->name().c_str());
9915 return;
9916 }
9917
4d625b70 9918 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9919 symtab,
9920 layout,
9921 this,
9922 object,
9923 data_shndx,
9924 prelocs,
9925 reloc_count,
9926 output_section,
9927 needs_special_offset_handling,
9928 local_symbol_count,
9929 plocal_symbols);
63e5eea2
AM
9930
9931 if (this->plt_localentry0_ && this->power10_relocs_)
9932 {
9933 gold_warning(_("--plt-localentry is incompatible with "
9934 "power10 pc-relative code"));
9935 this->plt_localentry0_ = false;
9936 }
42cacb20
DE
9937}
9938
ec4dbad3
AM
9939// Functor class for processing the global symbol table.
9940// Removes symbols defined on discarded opd entries.
9941
9942template<bool big_endian>
9943class Global_symbol_visitor_opd
9944{
9945 public:
9946 Global_symbol_visitor_opd()
9947 { }
9948
9949 void
9950 operator()(Sized_symbol<64>* sym)
9951 {
9952 if (sym->has_symtab_index()
9953 || sym->source() != Symbol::FROM_OBJECT
9954 || !sym->in_real_elf())
9955 return;
9956
6c77229c
AM
9957 if (sym->object()->is_dynamic())
9958 return;
9959
ec4dbad3
AM
9960 Powerpc_relobj<64, big_endian>* symobj
9961 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9962 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9963 return;
9964
9965 bool is_ordinary;
9966 unsigned int shndx = sym->shndx(&is_ordinary);
9967 if (shndx == symobj->opd_shndx()
9968 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9969 {
9970 sym->set_undefined();
e3ee8ed4 9971 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9972 sym->set_is_defined_in_discarded_section();
9973 sym->set_symtab_index(-1U);
9974 }
ec4dbad3
AM
9975 }
9976};
9977
f3a0ed29
AM
9978template<int size, bool big_endian>
9979void
9980Target_powerpc<size, big_endian>::define_save_restore_funcs(
9981 Layout* layout,
9982 Symbol_table* symtab)
9983{
9984 if (size == 64)
9985 {
d49044c7
AM
9986 Output_data_save_res<size, big_endian>* savres
9987 = new Output_data_save_res<size, big_endian>(symtab);
9988 this->savres_section_ = savres;
f3a0ed29
AM
9989 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9990 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9991 savres, ORDER_TEXT, false);
9992 }
9993}
9994
d8f5a274
AM
9995// Sort linker created .got section first (for the header), then input
9996// sections belonging to files using small model code.
9997
9998template<bool big_endian>
9999class Sort_toc_sections
10000{
f19c3684
AM
10001 const Output_section_data*
10002 small_got_section() const
10003 {
10004 return (static_cast<Target_powerpc<64, big_endian>*>(
10005 parameters->sized_target<64, big_endian>())
10006 ->got_section(GOT_TYPE_SMALL));
10007 }
10008
10009 int
10010 rank(const Output_section::Input_section& isec) const
10011 {
10012 if (!isec.is_input_section())
10013 {
10014 if (isec.output_section_data() == this->small_got_section())
10015 return 0;
10016 return 2;
10017 }
10018 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
10019 ->has_small_toc_reloc())
10020 return 1;
10021 return 3;
10022 }
10023
d8f5a274
AM
10024 public:
10025 bool
10026 operator()(const Output_section::Input_section& is1,
10027 const Output_section::Input_section& is2) const
10028 {
f19c3684 10029 return rank(is1) < rank(is2);
d8f5a274
AM
10030 }
10031};
10032
42cacb20
DE
10033// Finalize the sections.
10034
10035template<int size, bool big_endian>
10036void
d5b40221
DK
10037Target_powerpc<size, big_endian>::do_finalize_sections(
10038 Layout* layout,
724436fc 10039 const Input_objects* input_objects,
ec4dbad3 10040 Symbol_table* symtab)
42cacb20 10041{
c9824451
AM
10042 if (parameters->doing_static_link())
10043 {
10044 // At least some versions of glibc elf-init.o have a strong
10045 // reference to __rela_iplt marker syms. A weak ref would be
10046 // better..
10047 if (this->iplt_ != NULL)
10048 {
10049 Reloc_section* rel = this->iplt_->rel_plt();
10050 symtab->define_in_output_data("__rela_iplt_start", NULL,
10051 Symbol_table::PREDEFINED, rel, 0, 0,
10052 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10053 elfcpp::STV_HIDDEN, 0, false, true);
10054 symtab->define_in_output_data("__rela_iplt_end", NULL,
10055 Symbol_table::PREDEFINED, rel, 0, 0,
10056 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10057 elfcpp::STV_HIDDEN, 0, true, true);
10058 }
10059 else
10060 {
10061 symtab->define_as_constant("__rela_iplt_start", NULL,
10062 Symbol_table::PREDEFINED, 0, 0,
10063 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10064 elfcpp::STV_HIDDEN, 0, true, false);
10065 symtab->define_as_constant("__rela_iplt_end", NULL,
10066 Symbol_table::PREDEFINED, 0, 0,
10067 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10068 elfcpp::STV_HIDDEN, 0, true, false);
10069 }
10070 }
10071
ec4dbad3
AM
10072 if (size == 64)
10073 {
10074 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10075 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
10076
10077 if (!parameters->options().relocatable())
10078 {
10079 this->define_save_restore_funcs(layout, symtab);
10080
10081 // Annoyingly, we need to make these sections now whether or
10082 // not we need them. If we delay until do_relax then we
10083 // need to mess with the relaxation machinery checkpointing.
f19c3684 10084 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
ec661b9d 10085 this->make_brlt_section(layout);
d8f5a274 10086
f19c3684
AM
10087 // FIXME, maybe. Here we could run through all the got
10088 // entries in the small got section, removing any duplicates
10089 // found in the big got section and renumbering offsets.
10090
d8f5a274
AM
10091 if (parameters->options().toc_sort())
10092 {
10093 Output_section* os = this->got_->output_section();
10094 if (os != NULL && os->input_sections().size() > 1)
10095 std::stable_sort(os->input_sections().begin(),
10096 os->input_sections().end(),
10097 Sort_toc_sections<big_endian>());
10098 }
ec661b9d 10099 }
ec4dbad3
AM
10100 }
10101
42cacb20 10102 // Fill in some more dynamic tags.
c9269dff 10103 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 10104 if (odyn != NULL)
cf43a2fe 10105 {
c9824451
AM
10106 const Reloc_section* rel_plt = (this->plt_ == NULL
10107 ? NULL
10108 : this->plt_->rel_plt());
10109 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
10110 this->rela_dyn_, true, size == 32);
10111
10112 if (size == 32)
dd93cd0a 10113 {
c9824451
AM
10114 if (this->got_ != NULL)
10115 {
10116 this->got_->finalize_data_size();
10117 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10118 this->got_, this->got_->g_o_t());
10119 }
34e0882b
AM
10120 if (this->has_tls_get_addr_opt_)
10121 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 10122 }
c9824451 10123 else
dd93cd0a 10124 {
c9824451
AM
10125 if (this->glink_ != NULL)
10126 {
10127 this->glink_->finalize_data_size();
10128 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10129 this->glink_,
9e390558 10130 (this->glink_->pltresolve_size()
c9824451
AM
10131 - 32));
10132 }
34e0882b 10133 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 10134 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
10135 ((this->has_localentry0_
10136 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10137 | (this->has_tls_get_addr_opt_
10138 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 10139 }
c9269dff 10140 }
cf43a2fe 10141
42cacb20
DE
10142 // Emit any relocs we saved in an attempt to avoid generating COPY
10143 // relocs.
10144 if (this->copy_relocs_.any_saved_relocs())
10145 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
10146
10147 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10148 p != input_objects->relobj_end();
10149 ++p)
10150 {
10151 Powerpc_relobj<size, big_endian>* ppc_relobj
10152 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10153 if (ppc_relobj->attributes_section_data())
6f3fe02b 10154 this->merge_object_attributes(ppc_relobj,
724436fc
AM
10155 ppc_relobj->attributes_section_data());
10156 }
10157 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10158 p != input_objects->dynobj_end();
10159 ++p)
10160 {
10161 Powerpc_dynobj<size, big_endian>* ppc_dynobj
10162 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10163 if (ppc_dynobj->attributes_section_data())
6f3fe02b 10164 this->merge_object_attributes(ppc_dynobj,
724436fc
AM
10165 ppc_dynobj->attributes_section_data());
10166 }
10167
10168 // Create a .gnu.attributes section if we have merged any attributes
10169 // from inputs.
10170 if (this->attributes_section_data_ != NULL
10171 && this->attributes_section_data_->size() != 0)
10172 {
10173 Output_attributes_section_data* attributes_section
10174 = new Output_attributes_section_data(*this->attributes_section_data_);
10175 layout->add_output_section_data(".gnu.attributes",
10176 elfcpp::SHT_GNU_ATTRIBUTES, 0,
10177 attributes_section, ORDER_INVALID, false);
10178 }
10179}
10180
10181// Merge object attributes from input file called NAME with those of the
10182// output. The input object attributes are in the object pointed by PASD.
10183
10184template<int size, bool big_endian>
10185void
10186Target_powerpc<size, big_endian>::merge_object_attributes(
6f3fe02b 10187 const Object* obj,
724436fc
AM
10188 const Attributes_section_data* pasd)
10189{
10190 // Return if there is no attributes section data.
10191 if (pasd == NULL)
10192 return;
10193
10194 // Create output object attributes.
10195 if (this->attributes_section_data_ == NULL)
10196 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10197
10198 const int vendor = Object_attribute::OBJ_ATTR_GNU;
10199 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10200 Object_attribute* out_attr
10201 = this->attributes_section_data_->known_attributes(vendor);
10202
6f3fe02b 10203 const char* name = obj->name().c_str();
724436fc
AM
10204 const char* err;
10205 const char* first;
10206 const char* second;
10207 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10208 int in_fp = in_attr[tag].int_value() & 0xf;
10209 int out_fp = out_attr[tag].int_value() & 0xf;
6f3fe02b 10210 bool warn_only = obj->is_dynamic();
724436fc
AM
10211 if (in_fp != out_fp)
10212 {
10213 err = NULL;
10214 if ((in_fp & 3) == 0)
10215 ;
10216 else if ((out_fp & 3) == 0)
10217 {
6f3fe02b
AM
10218 if (!warn_only)
10219 {
10220 out_fp |= in_fp & 3;
10221 out_attr[tag].set_int_value(out_fp);
10222 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10223 this->last_fp_ = name;
10224 }
724436fc
AM
10225 }
10226 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10227 {
10228 err = N_("%s uses hard float, %s uses soft float");
10229 first = this->last_fp_;
10230 second = name;
10231 }
10232 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10233 {
10234 err = N_("%s uses hard float, %s uses soft float");
10235 first = name;
10236 second = this->last_fp_;
10237 }
10238 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10239 {
10240 err = N_("%s uses double-precision hard float, "
10241 "%s uses single-precision hard float");
10242 first = this->last_fp_;
10243 second = name;
10244 }
10245 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10246 {
10247 err = N_("%s uses double-precision hard float, "
10248 "%s uses single-precision hard float");
10249 first = name;
10250 second = this->last_fp_;
10251 }
10252
10253 if (err || (in_fp & 0xc) == 0)
10254 ;
10255 else if ((out_fp & 0xc) == 0)
10256 {
6f3fe02b
AM
10257 if (!warn_only)
10258 {
10259 out_fp |= in_fp & 0xc;
10260 out_attr[tag].set_int_value(out_fp);
10261 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10262 this->last_ld_ = name;
10263 }
724436fc
AM
10264 }
10265 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10266 {
10267 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10268 first = name;
10269 second = this->last_ld_;
10270 }
10271 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10272 {
10273 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10274 first = this->last_ld_;
10275 second = name;
10276 }
10277 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10278 {
10279 err = N_("%s uses IBM long double, %s uses IEEE long double");
10280 first = this->last_ld_;
10281 second = name;
10282 }
10283 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10284 {
10285 err = N_("%s uses IBM long double, %s uses IEEE long double");
10286 first = name;
10287 second = this->last_ld_;
10288 }
10289
10290 if (err)
10291 {
10292 if (parameters->options().warn_mismatch())
6f3fe02b
AM
10293 {
10294 if (warn_only)
10295 gold_warning(_(err), first, second);
10296 else
10297 gold_error(_(err), first, second);
10298 }
724436fc
AM
10299 // Arrange for this attribute to be deleted. It's better to
10300 // say "don't know" about a file than to wrongly claim compliance.
6f3fe02b
AM
10301 if (!warn_only)
10302 out_attr[tag].set_type(0);
724436fc
AM
10303 }
10304 }
10305
10306 if (size == 32)
10307 {
10308 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10309 int in_vec = in_attr[tag].int_value() & 3;
10310 int out_vec = out_attr[tag].int_value() & 3;
10311 if (in_vec != out_vec)
10312 {
10313 err = NULL;
10314 if (in_vec == 0)
10315 ;
10316 else if (out_vec == 0)
10317 {
10318 out_vec = in_vec;
10319 out_attr[tag].set_int_value(out_vec);
10320 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10321 this->last_vec_ = name;
10322 }
10323 // For now, allow generic to transition to AltiVec or SPE
10324 // without a warning. If GCC marked files with their stack
10325 // alignment and used don't-care markings for files which are
10326 // not affected by the vector ABI, we could warn about this
10327 // case too. */
10328 else if (in_vec == 1)
10329 ;
10330 else if (out_vec == 1)
10331 {
10332 out_vec = in_vec;
10333 out_attr[tag].set_int_value(out_vec);
10334 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10335 this->last_vec_ = name;
10336 }
10337 else if (out_vec < in_vec)
10338 {
10339 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10340 first = this->last_vec_;
10341 second = name;
10342 }
10343 else if (out_vec > in_vec)
10344 {
10345 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10346 first = name;
10347 second = this->last_vec_;
10348 }
10349 if (err)
10350 {
10351 if (parameters->options().warn_mismatch())
10352 gold_error(_(err), first, second);
10353 out_attr[tag].set_type(0);
10354 }
10355 }
10356
10357 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10358 int in_struct = in_attr[tag].int_value() & 3;
10359 int out_struct = out_attr[tag].int_value() & 3;
10360 if (in_struct != out_struct)
10361 {
10362 err = NULL;
10363 if (in_struct == 0 || in_struct == 3)
10364 ;
10365 else if (out_struct == 0)
10366 {
10367 out_struct = in_struct;
10368 out_attr[tag].set_int_value(out_struct);
10369 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10370 this->last_struct_ = name;
10371 }
10372 else if (out_struct < in_struct)
10373 {
10374 err = N_("%s uses r3/r4 for small structure returns, "
10375 "%s uses memory");
10376 first = this->last_struct_;
10377 second = name;
10378 }
10379 else if (out_struct > in_struct)
10380 {
10381 err = N_("%s uses r3/r4 for small structure returns, "
10382 "%s uses memory");
10383 first = name;
10384 second = this->last_struct_;
10385 }
10386 if (err)
10387 {
10388 if (parameters->options().warn_mismatch())
10389 gold_error(_(err), first, second);
10390 out_attr[tag].set_type(0);
10391 }
10392 }
10393 }
10394
10395 // Merge Tag_compatibility attributes and any common GNU ones.
10396 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
10397}
10398
5edad15d
AM
10399// Emit any saved relocs, and mark toc entries using any of these
10400// relocs as not optimizable.
aba6bc71 10401
5edad15d
AM
10402template<int sh_type, int size, bool big_endian>
10403void
10404Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10405 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 10406{
5edad15d
AM
10407 if (size == 64
10408 && parameters->options().toc_optimize())
10409 {
10410 for (typename Copy_relocs<sh_type, size, big_endian>::
10411 Copy_reloc_entries::iterator p = this->entries_.begin();
10412 p != this->entries_.end();
10413 ++p)
10414 {
10415 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10416 entry = *p;
10417
10418 // If the symbol is no longer defined in a dynamic object,
10419 // then we emitted a COPY relocation. If it is still
10420 // dynamic then we'll need dynamic relocations and thus
10421 // can't optimize toc entries.
10422 if (entry.sym_->is_from_dynobj())
10423 {
10424 Powerpc_relobj<size, big_endian>* ppc_object
10425 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10426 if (entry.shndx_ == ppc_object->toc_shndx())
10427 ppc_object->set_no_toc_opt(entry.address_);
10428 }
10429 }
10430 }
10431
10432 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
10433}
10434
3ea0a085
AM
10435// Return the value to use for a branch relocation.
10436
10437template<int size, bool big_endian>
1611bc4a 10438bool
3ea0a085 10439Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 10440 const Symbol_table* symtab,
3ea0a085
AM
10441 const Sized_symbol<size>* gsym,
10442 Powerpc_relobj<size, big_endian>* object,
1611bc4a 10443 Address *value,
3ea0a085
AM
10444 unsigned int *dest_shndx)
10445{
9055360d
AM
10446 if (size == 32 || this->abiversion() >= 2)
10447 gold_unreachable();
3ea0a085 10448 *dest_shndx = 0;
3ea0a085
AM
10449
10450 // If the symbol is defined in an opd section, ie. is a function
10451 // descriptor, use the function descriptor code entry address
10452 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 10453 if (gsym != NULL
0e123f69
AM
10454 && (gsym->source() != Symbol::FROM_OBJECT
10455 || gsym->object()->is_dynamic()))
1611bc4a 10456 return true;
3ea0a085
AM
10457 if (gsym != NULL)
10458 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10459 unsigned int shndx = symobj->opd_shndx();
10460 if (shndx == 0)
1611bc4a 10461 return true;
3ea0a085 10462 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 10463 if (opd_addr == invalid_address)
1611bc4a 10464 return true;
c6905c28 10465 opd_addr += symobj->output_section_address(shndx);
1611bc4a 10466 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
10467 {
10468 Address sec_off;
1611bc4a 10469 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
10470 if (symtab->is_section_folded(symobj, *dest_shndx))
10471 {
10472 Section_id folded
10473 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10474 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10475 *dest_shndx = folded.second;
10476 }
3ea0a085 10477 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
10478 if (sec_addr == invalid_address)
10479 return false;
10480
3ea0a085 10481 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 10482 *value = sec_addr + sec_off;
3ea0a085 10483 }
1611bc4a 10484 return true;
3ea0a085
AM
10485}
10486
c9b8abb7
AM
10487template<int size>
10488static bool
10489relative_value_is_known(const Sized_symbol<size>* gsym)
10490{
10491 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10492 return false;
10493
10494 if (gsym->is_from_dynobj()
10495 || gsym->is_undefined()
10496 || gsym->is_preemptible())
10497 return false;
10498
10499 if (gsym->is_absolute())
10500 return !parameters->options().output_is_position_independent();
10501
10502 return true;
10503}
10504
10505template<int size>
10506static bool
10507relative_value_is_known(const Symbol_value<size>* psymval)
10508{
10509 if (psymval->is_ifunc_symbol())
10510 return false;
10511
10512 bool is_ordinary;
10513 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10514
10515 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10516}
10517
0c951c25
AM
10518// PCREL_OPT in one instance flags to the linker that a pair of insns:
10519// pld ra,symbol@got@pcrel
10520// load/store rt,0(ra)
10521// or
10522// pla ra,symbol@pcrel
10523// load/store rt,0(ra)
10524// may be translated to
10525// pload/pstore rt,symbol@pcrel
10526// nop.
10527// This function returns true if the optimization is possible, placing
10528// the prefix insn in *PINSN1 and a NOP in *PINSN2.
10529//
10530// On entry to this function, the linker has already determined that
10531// the pld can be replaced with pla: *PINSN1 is that pla insn,
10532// while *PINSN2 is the second instruction.
10533
10534inline bool
10535xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10536{
10537 uint32_t insn2 = *pinsn2 >> 32;
10538 uint64_t i1new;
10539
10540 // Check that regs match.
10541 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10542 return false;
10543
10544 switch ((insn2 >> 26) & 63)
10545 {
10546 default:
10547 return false;
10548
10549 case 32: // lwz
10550 case 34: // lbz
10551 case 36: // stw
10552 case 38: // stb
10553 case 40: // lhz
10554 case 42: // lha
10555 case 44: // sth
10556 case 48: // lfs
10557 case 50: // lfd
10558 case 52: // stfs
10559 case 54: // stfd
10560 // These are the PMLS cases, where we just need to tack a prefix
10561 // on the insn. Check that the D field is zero.
10562 if ((insn2 & 0xffff) != 0)
10563 return false;
10564 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10565 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10566 break;
10567
10568 case 58: // lwa, ld
10569 if ((insn2 & 0xfffd) != 0)
10570 return false;
10571 i1new = ((1ULL << 58) | (1ULL << 52)
10572 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10573 | (insn2 & (31ULL << 21)));
10574 break;
10575
10576 case 57: // lxsd, lxssp
10577 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10578 return false;
10579 i1new = ((1ULL << 58) | (1ULL << 52)
10580 | ((40ULL | (insn2 & 3)) << 26)
10581 | (insn2 & (31ULL << 21)));
10582 break;
10583
10584 case 61: // stxsd, stxssp, lxv, stxv
10585 if ((insn2 & 3) == 0)
10586 return false;
10587 else if ((insn2 & 3) >= 2)
10588 {
10589 if ((insn2 & 0xfffc) != 0)
10590 return false;
10591 i1new = ((1ULL << 58) | (1ULL << 52)
10592 | ((44ULL | (insn2 & 3)) << 26)
10593 | (insn2 & (31ULL << 21)));
10594 }
10595 else
10596 {
10597 if ((insn2 & 0xfff0) != 0)
10598 return false;
10599 i1new = ((1ULL << 58) | (1ULL << 52)
10600 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10601 | (insn2 & (31ULL << 21)));
10602 }
10603 break;
10604
10605 case 56: // lq
10606 if ((insn2 & 0xffff) != 0)
10607 return false;
10608 i1new = ((1ULL << 58) | (1ULL << 52)
10609 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10610 break;
10611
10612 case 62: // std, stq
10613 if ((insn2 & 0xfffd) != 0)
10614 return false;
10615 i1new = ((1ULL << 58) | (1ULL << 52)
10616 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10617 | (insn2 & (31ULL << 21)));
10618 break;
10619 }
10620
10621 *pinsn1 = i1new;
10622 *pinsn2 = (uint64_t) nop << 32;
10623 return true;
10624}
10625
42cacb20
DE
10626// Perform a relocation.
10627
10628template<int size, bool big_endian>
10629inline bool
10630Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 10631 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 10632 unsigned int,
d83ce4e3
AM
10633 Target_powerpc* target,
10634 Output_section* os,
10635 size_t relnum,
91a65d2f 10636 const unsigned char* preloc,
d83ce4e3
AM
10637 const Sized_symbol<size>* gsym,
10638 const Symbol_value<size>* psymval,
10639 unsigned char* view,
c9269dff
AM
10640 Address address,
10641 section_size_type view_size)
42cacb20 10642{
23cedd1d
AM
10643 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10644 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10645 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10646
0e804863
ILT
10647 if (view == NULL)
10648 return true;
10649
34e0882b
AM
10650 if (target->replace_tls_get_addr(gsym))
10651 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10652
91a65d2f
AM
10653 const elfcpp::Rela<size, big_endian> rela(preloc);
10654 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
0af4fcc2
AM
10655 Powerpc_relobj<size, big_endian>* const object
10656 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
34e0882b 10657 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 10658 {
e3deeb9c 10659 case Track_tls::NOT_EXPECTED:
c7fdac09
AM
10660 // No warning. This will result in really old code without tls
10661 // marker relocs being mis-optimised, but there shouldn't be too
10662 // much of that code around. The problem with warning is that
10663 // glibc and libphobos both construct direct calls to
10664 // __tls_get_addr in a way that is harmless.
e3deeb9c
AM
10665 break;
10666 case Track_tls::EXPECTED:
10667 // We have already complained.
10668 break;
10669 case Track_tls::SKIP:
23cedd1d 10670 if (is_plt16_reloc<size>(r_type)
32f59844
AM
10671 || r_type == elfcpp::R_POWERPC_PLTSEQ
10672 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
10673 {
10674 Insn* iview = reinterpret_cast<Insn*>(view);
10675 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10676 }
10677 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10678 {
10679 Insn* iview = reinterpret_cast<Insn*>(view);
10680 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10681 }
e4dff765
AM
10682 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10683 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10684 {
10685 Insn* iview = reinterpret_cast<Insn*>(view);
10686 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10687 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10688 }
e3deeb9c
AM
10689 return true;
10690 case Track_tls::NORMAL:
10691 break;
dd93cd0a 10692 }
dd93cd0a 10693
dcfc7dd4
AM
10694 // Offset from start of insn to d-field reloc.
10695 const int d_offset = big_endian ? 2 : 0;
10696
dd93cd0a 10697 Address value = 0;
0cfb0717 10698 bool has_stub_value = false;
7ee7ff70 10699 bool localentry0 = false;
e5d5f5ed 10700 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6158b25f
AM
10701 bool pltcall_to_direct = false;
10702
f21dbd7c
AM
10703 if (is_plt16_reloc<size>(r_type)
10704 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10705 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10706 || r_type == elfcpp::R_POWERPC_PLTSEQ
10707 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10708 || r_type == elfcpp::R_POWERPC_PLTCALL
10709 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10710 {
10711 // It would be possible to replace inline plt calls with direct
10712 // calls if the PLTCALL is in range. The only difficulty is
10713 // that the decision depends on the PLTCALL reloc, and we don't
10714 // know the address of that instruction when processing others
10715 // in the sequence. So the decision needs to be made in
6158b25f
AM
10716 // do_relax().
10717 pltcall_to_direct = !(gsym != NULL
10718 ? gsym->has_plt_offset()
10719 : object->local_has_plt_offset(r_sym));
10720 }
10721 else if ((gsym != NULL
10722 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10723 : psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
10724 && !is_got_reloc(r_type)
10725 && (!psymval->is_ifunc_symbol()
10726 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type,
10727 false)))
dd93cd0a 10728 {
9055360d
AM
10729 if (size == 64
10730 && gsym != NULL
10731 && target->abiversion() >= 2
10732 && !parameters->options().output_is_position_independent()
32f59844 10733 && !is_branch_reloc<size>(r_type))
ec661b9d 10734 {
faa2211d
AM
10735 Address off = target->glink_section()->find_global_entry(gsym);
10736 if (off != invalid_address)
6ec65f28
AM
10737 {
10738 value = target->glink_section()->global_entry_address() + off;
10739 has_stub_value = true;
10740 }
ec661b9d 10741 }
c9824451 10742 else
9055360d 10743 {
64b5d6d7
AM
10744 Stub_table<size, big_endian>* stub_table = NULL;
10745 if (target->stub_tables().size() == 1)
10746 stub_table = target->stub_tables()[0];
10747 if (stub_table == NULL
10748 && !(size == 32
10749 && gsym != NULL
10750 && !parameters->options().output_is_position_independent()
32f59844 10751 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10752 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10753 if (stub_table == NULL)
10754 {
64b5d6d7
AM
10755 // This is a ref from a data section to an ifunc symbol,
10756 // or a non-branch reloc for which we always want to use
10757 // one set of stubs for resolving function addresses.
9055360d
AM
10758 if (target->stub_tables().size() != 0)
10759 stub_table = target->stub_tables()[0];
10760 }
faa2211d
AM
10761 if (stub_table != NULL)
10762 {
7e57d19e 10763 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10764 if (gsym != NULL)
7e57d19e 10765 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10766 rela.get_r_addend());
10767 else
7e57d19e 10768 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10769 rela.get_r_addend());
7e57d19e 10770 if (ent != NULL)
faa2211d 10771 {
7e57d19e
AM
10772 value = stub_table->stub_address() + ent->off_;
10773 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10774 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10775 size_t reloc_count = shdr.get_sh_size() / reloc_size;
eb97d024
AM
10776 if (size == 64)
10777 {
10778 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10779 {
10780 if (!ent->notoc_)
10781 value += ent->p9off_;
10782 }
10783 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10784 value += ent->p9off_;
10785 else
10786 value += ent->tocoff_;
10787 }
7e57d19e
AM
10788 if (size == 64
10789 && ent->r2save_
a993d270
AM
10790 && !(gsym != NULL
10791 && target->is_tls_get_addr_opt(gsym)))
afd2ea23 10792 {
eb97d024
AM
10793 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10794 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
a993d270
AM
10795 {
10796 if (!(target->power10_stubs()
10797 && target->power10_stubs_auto()))
10798 value += 4;
10799 }
10800 else if (relnum < reloc_count - 1)
10801 {
10802 Reltype next_rela(preloc + reloc_size);
10803 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10804 == elfcpp::R_PPC64_TOCSAVE
10805 && (next_rela.get_r_offset()
10806 == rela.get_r_offset() + 4))
10807 value += 4;
10808 }
7e57d19e 10809 }
7ee7ff70 10810 localentry0 = ent->localentry0_;
faa2211d
AM
10811 has_stub_value = true;
10812 }
10813 }
9055360d 10814 }
faa2211d
AM
10815 // We don't care too much about bogus debug references to
10816 // non-local functions, but otherwise there had better be a plt
10817 // call stub or global entry stub as appropriate.
10818 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10819 }
cf43a2fe 10820
6158b25f
AM
10821 if (!pltcall_to_direct && (is_plt16_reloc<size>(r_type)
10822 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10823 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10824 {
10825 const Output_data_plt_powerpc<size, big_endian>* plt;
10826 if (gsym)
10827 value = target->plt_off(gsym, &plt);
10828 else
10829 value = target->plt_off(object, r_sym, &plt);
10830 value += plt->address();
10831
10832 if (size == 64)
e4dff765
AM
10833 {
10834 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10835 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
a19da04b 10836 value -= target->toc_pointer();
e4dff765 10837 }
08be3224
AM
10838 else if (parameters->options().output_is_position_independent())
10839 {
10840 if (rela.get_r_addend() >= 32768)
10841 {
10842 unsigned int got2 = object->got2_shndx();
10843 value -= (object->get_output_section_offset(got2)
10844 + object->output_section(got2)->address()
10845 + rela.get_r_addend());
10846 }
10847 else
a19da04b 10848 value -= target->toc_pointer();
08be3224
AM
10849 }
10850 }
6158b25f 10851 else if (pltcall_to_direct
23cedd1d 10852 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10853 || r_type == elfcpp::R_POWERPC_PLTSEQ
10854 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10855 {
10856 Insn* iview = reinterpret_cast<Insn*>(view);
10857 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10858 r_type = elfcpp::R_POWERPC_NONE;
10859 }
6158b25f 10860 else if (pltcall_to_direct
e4dff765
AM
10861 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10862 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10863 {
10864 Insn* iview = reinterpret_cast<Insn*>(view);
10865 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10866 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10867 r_type = elfcpp::R_POWERPC_NONE;
10868 }
4290b0ab 10869 else if (is_got_reloc(r_type))
42cacb20 10870 {
5cdb4f14 10871 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
10872 Got_type got_type = ((size == 32
10873 || r_type == elfcpp::R_POWERPC_GOT16
10874 || r_type == elfcpp::R_PPC64_GOT16_DS)
10875 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
cf43a2fe 10876 if (gsym != NULL)
f19c3684 10877 value = gsym->got_offset(got_type, addend);
cf43a2fe 10878 else
f19c3684 10879 value = object->local_got_offset(r_sym, got_type, addend);
e4dff765 10880 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
f19c3684 10881 value += target->got_section(got_type)->address();
e4dff765 10882 else
f19c3684 10883 value -= target->got_base_offset(got_type);
cf43a2fe
AM
10884 }
10885 else if (r_type == elfcpp::R_PPC64_TOC)
10886 {
a19da04b 10887 value = target->toc_pointer();
cf43a2fe
AM
10888 }
10889 else if (gsym != NULL
10890 && (r_type == elfcpp::R_POWERPC_REL24
10891 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10892 && has_stub_value)
cf43a2fe 10893 {
c9269dff
AM
10894 if (size == 64)
10895 {
10896 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10897 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10898 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10899 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10900 {
3ea0a085 10901 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10902 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10903 if ((insn & 1) != 0
10904 && (insn2 == nop
10905 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10906 {
b4f7960d
AM
10907 elfcpp::Swap<32, big_endian>::
10908 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10909 can_plt_call = true;
10910 }
10911 }
10912 if (!can_plt_call)
3ea0a085
AM
10913 {
10914 // If we don't have a branch and link followed by a nop,
10915 // we can't go via the plt because there is no place to
10916 // put a toc restoring instruction.
10917 // Unless we know we won't be returning.
10918 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10919 can_plt_call = true;
10920 }
10921 if (!can_plt_call)
10922 {
ba8ca3e7
AM
10923 // g++ as of 20130507 emits self-calls without a
10924 // following nop. This is arguably wrong since we have
10925 // conflicting information. On the one hand a global
10926 // symbol and on the other a local call sequence, but
10927 // don't error for this special case.
10928 // It isn't possible to cheaply verify we have exactly
10929 // such a call. Allow all calls to the same section.
3ea0a085 10930 bool ok = false;
c9824451 10931 Address code = value;
3ea0a085
AM
10932 if (gsym->source() == Symbol::FROM_OBJECT
10933 && gsym->object() == object)
10934 {
9055360d
AM
10935 unsigned int dest_shndx = 0;
10936 if (target->abiversion() < 2)
10937 {
10938 Address addend = rela.get_r_addend();
1611bc4a
AM
10939 code = psymval->value(object, addend);
10940 target->symval_for_branch(relinfo->symtab, gsym, object,
10941 &code, &dest_shndx);
9055360d 10942 }
3ea0a085
AM
10943 bool is_ordinary;
10944 if (dest_shndx == 0)
10945 dest_shndx = gsym->shndx(&is_ordinary);
10946 ok = dest_shndx == relinfo->data_shndx;
10947 }
10948 if (!ok)
c9824451
AM
10949 {
10950 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10951 _("call lacks nop, can't restore toc; "
10952 "recompile with -fPIC"));
10953 value = code;
10954 }
3ea0a085 10955 }
c9269dff 10956 }
cf43a2fe 10957 }
dd93cd0a
AM
10958 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10959 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10960 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3 10961 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
87c69f97 10962 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a
AM
10963 {
10964 // First instruction of a global dynamic sequence, arg setup insn.
c7fdac09
AM
10965 bool final = gsym == NULL || gsym->final_value_is_known();
10966 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
f19c3684
AM
10967 Got_type got_type = ((size == 32
10968 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10969 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
dd93cd0a 10970 if (tls_type == tls::TLSOPT_NONE)
f19c3684 10971 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
dd93cd0a 10972 else if (tls_type == tls::TLSOPT_TO_IE)
f19c3684
AM
10973 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10974 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
dd93cd0a 10975 {
5cdb4f14 10976 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
dd93cd0a 10977 if (gsym != NULL)
5cdb4f14 10978 value = gsym->got_offset(got_type, addend);
dd93cd0a 10979 else
5cdb4f14 10980 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 10981 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
f19c3684 10982 value += target->got_section(got_type)->address();
89c52ae3 10983 else
f19c3684 10984 value -= target->got_base_offset(got_type);
dd93cd0a
AM
10985 }
10986 if (tls_type == tls::TLSOPT_TO_IE)
10987 {
87c69f97 10988 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 10989 {
89c52ae3
AM
10990 Insn* iview = reinterpret_cast<Insn*>(view);
10991 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10992 pinsn <<= 32;
10993 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10994 // pla -> pld
10995 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10996 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10997 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10998 pinsn & 0xffffffff);
87c69f97 10999 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
89c52ae3
AM
11000 }
11001 else
11002 {
11003 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11004 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11005 {
11006 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11007 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11008 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
11009 if (size == 32)
11010 insn |= 32 << 26; // lwz
11011 else
11012 insn |= 58 << 26; // ld
11013 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11014 }
11015 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11016 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 11017 }
dd93cd0a
AM
11018 }
11019 else if (tls_type == tls::TLSOPT_TO_LE)
11020 {
87c69f97 11021 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 11022 {
89c52ae3
AM
11023 Insn* iview = reinterpret_cast<Insn*>(view);
11024 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11025 pinsn <<= 32;
11026 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11027 // pla pcrel -> paddi r13
11028 pinsn += (-1ULL << 52) + (13ULL << 16);
11029 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11030 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11031 pinsn & 0xffffffff);
11032 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
11033 value = psymval->value(object, rela.get_r_addend());
11034 }
11035 else
11036 {
89c52ae3
AM
11037 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11038 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11039 {
11040 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11041 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11042 insn &= (1 << 26) - (1 << 21); // extract rt
11043 if (size == 32)
11044 insn |= addis_0_2;
11045 else
11046 insn |= addis_0_13;
11047 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11048 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11049 value = psymval->value(object, rela.get_r_addend());
11050 }
11051 else
11052 {
11053 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11054 Insn insn = nop;
11055 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11056 r_type = elfcpp::R_POWERPC_NONE;
11057 }
dd93cd0a
AM
11058 }
11059 }
11060 }
11061 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11062 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11063 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3 11064 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
87c69f97 11065 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
dd93cd0a
AM
11066 {
11067 // First instruction of a local dynamic sequence, arg setup insn.
c7fdac09 11068 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
11069 if (tls_type == tls::TLSOPT_NONE)
11070 {
11071 value = target->tlsld_got_offset();
87c69f97 11072 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
f19c3684 11073 value += target->got_section(GOT_TYPE_SMALL)->address();
89c52ae3 11074 else
f19c3684 11075 value -= target->got_base_offset(GOT_TYPE_SMALL);
dd93cd0a
AM
11076 }
11077 else
11078 {
11079 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 11080 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
11081 {
11082 Insn* iview = reinterpret_cast<Insn*>(view);
11083 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11084 pinsn <<= 32;
11085 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11086 // pla pcrel -> paddi r13
11087 pinsn += (-1ULL << 52) + (13ULL << 16);
11088 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11089 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11090 pinsn & 0xffffffff);
11091 r_type = elfcpp::R_PPC64_TPREL34;
11092 value = dtp_offset;
11093 }
11094 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11095 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 11096 {
dcfc7dd4 11097 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
11098 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11099 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 11100 if (size == 32)
0f81d3f0
AM
11101 insn |= addis_0_2;
11102 else
11103 insn |= addis_0_13;
dd93cd0a
AM
11104 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11105 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 11106 value = dtp_offset;
dd93cd0a
AM
11107 }
11108 else
11109 {
dcfc7dd4 11110 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11111 Insn insn = nop;
11112 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11113 r_type = elfcpp::R_POWERPC_NONE;
11114 }
11115 }
11116 }
11117 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11118 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11119 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3 11120 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
87c69f97 11121 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
dd93cd0a
AM
11122 {
11123 // Accesses relative to a local dynamic sequence address,
11124 // no optimisation here.
5cdb4f14 11125 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
11126 Got_type got_type = ((size == 32
11127 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11128 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 11129 if (gsym != NULL)
f19c3684 11130 value = gsym->got_offset(got_type, addend);
dd93cd0a 11131 else
f19c3684 11132 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 11133 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
f19c3684 11134 value += target->got_section(got_type)->address();
89c52ae3 11135 else
f19c3684 11136 value -= target->got_base_offset(got_type);
dd93cd0a
AM
11137 }
11138 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11139 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11140 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3 11141 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
87c69f97 11142 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
dd93cd0a
AM
11143 {
11144 // First instruction of initial exec sequence.
0af4fcc2
AM
11145 bool final = gsym == NULL || gsym->final_value_is_known();
11146 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
11147 if (tls_type == tls::TLSOPT_NONE)
11148 {
5cdb4f14 11149 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
11150 Got_type got_type = ((size == 32
11151 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11152 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
dd93cd0a 11153 if (gsym != NULL)
f19c3684 11154 value = gsym->got_offset(got_type, addend);
dd93cd0a 11155 else
f19c3684 11156 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 11157 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
f19c3684 11158 value += target->got_section(got_type)->address();
89c52ae3 11159 else
f19c3684 11160 value -= target->got_base_offset(got_type);
dd93cd0a
AM
11161 }
11162 else
11163 {
11164 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 11165 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
11166 {
11167 Insn* iview = reinterpret_cast<Insn*>(view);
11168 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11169 pinsn <<= 32;
11170 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11171 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11172 pinsn += ((2ULL << 56) + (-1ULL << 52)
11173 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11174 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11175 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11176 pinsn & 0xffffffff);
11177 r_type = elfcpp::R_PPC64_TPREL34;
11178 value = psymval->value(object, rela.get_r_addend());
11179 }
11180 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11181 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 11182 {
dcfc7dd4 11183 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11184 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11185 insn &= (1 << 26) - (1 << 21); // extract rt from ld
11186 if (size == 32)
11187 insn |= addis_0_2;
11188 else
11189 insn |= addis_0_13;
11190 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11191 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11192 value = psymval->value(object, rela.get_r_addend());
11193 }
11194 else
11195 {
dcfc7dd4 11196 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11197 Insn insn = nop;
11198 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11199 r_type = elfcpp::R_POWERPC_NONE;
11200 }
11201 }
11202 }
11203 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11204 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11205 {
11206 // Second instruction of a global dynamic sequence,
11207 // the __tls_get_addr call
e3deeb9c 11208 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
c7fdac09
AM
11209 bool final = gsym == NULL || gsym->final_value_is_known();
11210 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
dd93cd0a
AM
11211 if (tls_type != tls::TLSOPT_NONE)
11212 {
11213 if (tls_type == tls::TLSOPT_TO_IE)
11214 {
11215 Insn* iview = reinterpret_cast<Insn*>(view);
11216 Insn insn = add_3_3_13;
11217 if (size == 32)
11218 insn = add_3_3_2;
11219 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11220 r_type = elfcpp::R_POWERPC_NONE;
11221 }
11222 else
11223 {
89c52ae3
AM
11224 bool is_pcrel = false;
11225 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11226 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11227 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11228 if (relnum < reloc_count - 1)
11229 {
11230 Reltype next_rela(preloc + reloc_size);
11231 unsigned int r_type2
11232 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11233 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
eb97d024 11234 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
89c52ae3
AM
11235 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11236 && next_rela.get_r_offset() == rela.get_r_offset())
11237 is_pcrel = true;
11238 }
dd93cd0a 11239 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
11240 if (is_pcrel)
11241 {
11242 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11243 r_type = elfcpp::R_POWERPC_NONE;
11244 }
11245 else
11246 {
11247 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11248 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11249 view += d_offset;
11250 value = psymval->value(object, rela.get_r_addend());
11251 }
dd93cd0a 11252 }
e3deeb9c 11253 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
11254 }
11255 }
11256 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11257 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11258 {
11259 // Second instruction of a local dynamic sequence,
11260 // the __tls_get_addr call
e3deeb9c 11261 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
c7fdac09 11262 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
11263 if (tls_type == tls::TLSOPT_TO_LE)
11264 {
89c52ae3
AM
11265 bool is_pcrel = false;
11266 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11267 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11268 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11269 if (relnum < reloc_count - 1)
11270 {
11271 Reltype next_rela(preloc + reloc_size);
11272 unsigned int r_type2
11273 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11274 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
eb97d024 11275 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
89c52ae3
AM
11276 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11277 && next_rela.get_r_offset() == rela.get_r_offset())
11278 is_pcrel = true;
11279 }
dd93cd0a 11280 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
11281 if (is_pcrel)
11282 {
11283 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11284 r_type = elfcpp::R_POWERPC_NONE;
11285 }
11286 else
11287 {
11288 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11289 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11290 view += d_offset;
11291 value = dtp_offset;
11292 }
e3deeb9c 11293 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
11294 }
11295 }
11296 else if (r_type == elfcpp::R_POWERPC_TLS)
11297 {
11298 // Second instruction of an initial exec sequence
0af4fcc2
AM
11299 bool final = gsym == NULL || gsym->final_value_is_known();
11300 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
11301 if (tls_type == tls::TLSOPT_TO_LE)
11302 {
89c52ae3
AM
11303 Address roff = rela.get_r_offset() & 3;
11304 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
11305 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11306 unsigned int reg = size == 32 ? 2 : 13;
11307 insn = at_tls_transform(insn, reg);
11308 gold_assert(insn != 0);
89c52ae3
AM
11309 if (roff == 0)
11310 {
11311 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11312 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11313 view += d_offset;
11314 value = psymval->value(object, rela.get_r_addend());
11315 }
11316 else if (roff == 1)
11317 {
11318 // For pcrel IE to LE we already have the full offset
11319 // and thus don't need an addi here. A nop or mr will do.
11320 if ((insn & (0x3f << 26)) == 14 << 26)
11321 {
11322 // Extract regs from addi rt,ra,si.
11323 unsigned int rt = (insn >> 21) & 0x1f;
11324 unsigned int ra = (insn >> 16) & 0x1f;
11325 if (rt == ra)
11326 insn = nop;
11327 else
11328 {
11329 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11330 insn = (rt << 16) | (ra << 21) | (ra << 11);
11331 insn |= (31u << 26) | (444u << 1);
11332 }
11333 }
11334 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11335 r_type = elfcpp::R_POWERPC_NONE;
11336 }
dd93cd0a
AM
11337 }
11338 }
0cfb0717 11339 else if (!has_stub_value)
cf43a2fe 11340 {
6158b25f
AM
11341 if (pltcall_to_direct && (r_type == elfcpp::R_POWERPC_PLTCALL
11342 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
11343 {
11344 // PLTCALL without plt entry => convert to direct call
11345 Insn* iview = reinterpret_cast<Insn*>(view);
11346 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11347 insn = (insn & 1) | b;
11348 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11349 if (size == 32)
11350 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
11351 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11352 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
11353 else
11354 r_type = elfcpp::R_POWERPC_REL24;
11355 }
dd93cd0a 11356 Address addend = 0;
08be3224
AM
11357 if (!(size == 32
11358 && (r_type == elfcpp::R_PPC_PLTREL24
11359 || r_type == elfcpp::R_POWERPC_PLT16_LO
11360 || r_type == elfcpp::R_POWERPC_PLT16_HI
11361 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 11362 addend = rela.get_r_addend();
c9824451 11363 value = psymval->value(object, addend);
fa40fbe4 11364 unsigned int local_ent = 0;
32f59844 11365 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
11366 {
11367 if (target->abiversion() >= 2)
11368 {
11369 if (gsym != NULL)
fa40fbe4 11370 local_ent = object->ppc64_local_entry_offset(gsym);
9055360d 11371 else
fa40fbe4 11372 local_ent = object->ppc64_local_entry_offset(r_sym);
9055360d
AM
11373 }
11374 else
1611bc4a
AM
11375 {
11376 unsigned int dest_shndx;
11377 target->symval_for_branch(relinfo->symtab, gsym, object,
11378 &value, &dest_shndx);
11379 }
9055360d 11380 }
fa40fbe4
AM
11381 Address max_branch = max_branch_delta<size>(r_type);
11382 if (max_branch != 0
11383 && (value + local_ent - address + max_branch >= 2 * max_branch
32f59844 11384 || (size == 64
eb97d024
AM
11385 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11386 || r_type == elfcpp::R_PPC64_REL24_NOTOC)
32f59844
AM
11387 && (gsym != NULL
11388 ? object->ppc64_needs_toc(gsym)
11389 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
11390 {
11391 Stub_table<size, big_endian>* stub_table
11392 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
11393 if (stub_table != NULL)
11394 {
32f59844 11395 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
a19da04b 11396 = stub_table->find_long_branch_entry(value);
32f59844 11397 if (ent != NULL)
0cfb0717 11398 {
32f59844
AM
11399 if (ent->save_res_)
11400 value = (value - target->savres_section()->address()
89c905a3
AM
11401 + stub_table->stub_address()
11402 + stub_table->plt_size()
32f59844
AM
11403 + stub_table->branch_size());
11404 else
afd2ea23
AM
11405 {
11406 value = (stub_table->stub_address()
11407 + stub_table->plt_size()
11408 + ent->off_);
eb97d024
AM
11409 if (size == 64)
11410 {
11411 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11412 {
11413 if (!ent->notoc_)
11414 value += ent->p9off_;
11415 }
11416 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11417 value += ent->p9off_;
11418 else
11419 value += ent->tocoff_;
11420 }
afd2ea23 11421 }
0cfb0717
AM
11422 has_stub_value = true;
11423 }
0cfdc767 11424 }
ec661b9d 11425 }
fa40fbe4
AM
11426 if (!has_stub_value)
11427 value += local_ent;
42cacb20
DE
11428 }
11429
42cacb20
DE
11430 switch (r_type)
11431 {
32f59844
AM
11432 case elfcpp::R_PPC64_REL24_NOTOC:
11433 if (size == 32)
11434 break;
11435 // Fall through.
eb97d024 11436 case elfcpp::R_PPC64_REL24_P9NOTOC:
dd93cd0a
AM
11437 case elfcpp::R_PPC64_REL64:
11438 case elfcpp::R_POWERPC_REL32:
11439 case elfcpp::R_POWERPC_REL24:
11440 case elfcpp::R_PPC_PLTREL24:
11441 case elfcpp::R_PPC_LOCAL24PC:
11442 case elfcpp::R_POWERPC_REL16:
11443 case elfcpp::R_POWERPC_REL16_LO:
11444 case elfcpp::R_POWERPC_REL16_HI:
11445 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 11446 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
11447 case elfcpp::R_PPC64_REL16_HIGH:
11448 case elfcpp::R_PPC64_REL16_HIGHA:
11449 case elfcpp::R_PPC64_REL16_HIGHER:
11450 case elfcpp::R_PPC64_REL16_HIGHERA:
11451 case elfcpp::R_PPC64_REL16_HIGHEST:
11452 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11453 case elfcpp::R_POWERPC_REL14:
11454 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11455 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11456 case elfcpp::R_PPC64_PCREL34:
11457 case elfcpp::R_PPC64_GOT_PCREL34:
11458 case elfcpp::R_PPC64_PLT_PCREL34:
11459 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11460 case elfcpp::R_PPC64_PCREL28:
87c69f97
AM
11461 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11462 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11463 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11464 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11465 case elfcpp::R_PPC64_REL16_HIGHER34:
11466 case elfcpp::R_PPC64_REL16_HIGHERA34:
11467 case elfcpp::R_PPC64_REL16_HIGHEST34:
11468 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
11469 value -= address;
11470 break;
11471
42cacb20
DE
11472 case elfcpp::R_PPC64_TOC16:
11473 case elfcpp::R_PPC64_TOC16_LO:
11474 case elfcpp::R_PPC64_TOC16_HI:
11475 case elfcpp::R_PPC64_TOC16_HA:
11476 case elfcpp::R_PPC64_TOC16_DS:
11477 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 11478 // Subtract the TOC base address.
a19da04b 11479 value -= target->toc_pointer();
42cacb20
DE
11480 break;
11481
cf43a2fe
AM
11482 case elfcpp::R_POWERPC_SECTOFF:
11483 case elfcpp::R_POWERPC_SECTOFF_LO:
11484 case elfcpp::R_POWERPC_SECTOFF_HI:
11485 case elfcpp::R_POWERPC_SECTOFF_HA:
11486 case elfcpp::R_PPC64_SECTOFF_DS:
11487 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11488 if (os != NULL)
11489 value -= os->address();
42cacb20
DE
11490 break;
11491
dd93cd0a
AM
11492 case elfcpp::R_PPC64_TPREL16_DS:
11493 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
11494 case elfcpp::R_PPC64_TPREL16_HIGH:
11495 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 11496 if (size != 64)
f9c6b907 11497 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 11498 break;
d8e90251 11499 // Fall through.
dd93cd0a
AM
11500 case elfcpp::R_POWERPC_TPREL16:
11501 case elfcpp::R_POWERPC_TPREL16_LO:
11502 case elfcpp::R_POWERPC_TPREL16_HI:
11503 case elfcpp::R_POWERPC_TPREL16_HA:
11504 case elfcpp::R_POWERPC_TPREL:
11505 case elfcpp::R_PPC64_TPREL16_HIGHER:
11506 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11507 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11508 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 11509 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
11510 // tls symbol values are relative to tls_segment()->vaddr()
11511 value -= tp_offset;
11512 break;
11513
11514 case elfcpp::R_PPC64_DTPREL16_DS:
11515 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11516 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11517 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11518 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11519 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11520 if (size != 64)
11521 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11522 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11523 break;
d8e90251 11524 // Fall through.
dd93cd0a
AM
11525 case elfcpp::R_POWERPC_DTPREL16:
11526 case elfcpp::R_POWERPC_DTPREL16_LO:
11527 case elfcpp::R_POWERPC_DTPREL16_HI:
11528 case elfcpp::R_POWERPC_DTPREL16_HA:
11529 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
11530 case elfcpp::R_PPC64_DTPREL16_HIGH:
11531 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 11532 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
11533 // tls symbol values are relative to tls_segment()->vaddr()
11534 value -= dtp_offset;
11535 break;
11536
45965137
AM
11537 case elfcpp::R_PPC64_ADDR64_LOCAL:
11538 if (gsym != NULL)
11539 value += object->ppc64_local_entry_offset(gsym);
11540 else
11541 value += object->ppc64_local_entry_offset(r_sym);
11542 break;
11543
42cacb20
DE
11544 default:
11545 break;
11546 }
11547
dd93cd0a 11548 Insn branch_bit = 0;
42cacb20
DE
11549 switch (r_type)
11550 {
dd93cd0a
AM
11551 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11552 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11553 branch_bit = 1 << 21;
d8e90251 11554 // Fall through.
dd93cd0a
AM
11555 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11556 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11557 {
11558 Insn* iview = reinterpret_cast<Insn*>(view);
11559 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11560 insn &= ~(1 << 21);
11561 insn |= branch_bit;
11562 if (this->is_isa_v2)
11563 {
11564 // Set 'a' bit. This is 0b00010 in BO field for branch
11565 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11566 // for branch on CTR insns (BO == 1a00t or 1a01t).
11567 if ((insn & (0x14 << 21)) == (0x04 << 21))
11568 insn |= 0x02 << 21;
11569 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11570 insn |= 0x08 << 21;
11571 else
11572 break;
11573 }
11574 else
11575 {
11576 // Invert 'y' bit if not the default.
11577 if (static_cast<Signed_address>(value) < 0)
11578 insn ^= 1 << 21;
11579 }
11580 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11581 }
11582 break;
11583
08be3224
AM
11584 case elfcpp::R_POWERPC_PLT16_HA:
11585 if (size == 32
11586 && !parameters->options().output_is_position_independent())
11587 {
11588 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11589 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11590
11591 // Convert addis to lis.
11592 if ((insn & (0x3f << 26)) == 15u << 26
11593 && (insn & (0x1f << 16)) != 0)
11594 {
11595 insn &= ~(0x1f << 16);
11596 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11597 }
11598 }
11599 break;
11600
dd93cd0a
AM
11601 default:
11602 break;
11603 }
11604
252dcdf4
AM
11605 if (gsym
11606 ? relative_value_is_known(gsym)
11607 : relative_value_is_known(psymval))
aba6bc71 11608 {
0c951c25
AM
11609 Insn* iview;
11610 Insn* iview2;
11611 Insn insn;
11612 uint64_t pinsn, pinsn2;
11613
aba6bc71
AM
11614 switch (r_type)
11615 {
11616 default:
11617 break;
11618
5edad15d
AM
11619 // Multi-instruction sequences that access the GOT/TOC can
11620 // be optimized, eg.
11621 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11622 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11623 // and
11624 // addis ra,r2,0; addi rb,ra,x@toc@l;
11625 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
11626 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11627 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11628 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11629 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11630 case elfcpp::R_POWERPC_GOT16_HA:
11631 case elfcpp::R_PPC64_TOC16_HA:
252dcdf4 11632 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11633 {
0c951c25
AM
11634 iview = reinterpret_cast<Insn*>(view - d_offset);
11635 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
11636 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11637 && object->make_toc_relative(target, &value))
11638 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11639 && object->make_got_relative(target, psymval,
11640 rela.get_r_addend(),
11641 &value)))
5edad15d
AM
11642 {
11643 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11644 == ((15u << 26) | (2 << 16)));
11645 }
11646 if (((insn & ((0x3f << 26) | 0x1f << 16))
11647 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11648 && value + 0x8000 < 0x10000)
aba6bc71
AM
11649 {
11650 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11651 return true;
11652 }
11653 }
11654 break;
11655
11656 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11657 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11658 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11659 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11660 case elfcpp::R_POWERPC_GOT16_LO:
11661 case elfcpp::R_PPC64_GOT16_LO_DS:
11662 case elfcpp::R_PPC64_TOC16_LO:
11663 case elfcpp::R_PPC64_TOC16_LO_DS:
252dcdf4 11664 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11665 {
0c951c25
AM
11666 iview = reinterpret_cast<Insn*>(view - d_offset);
11667 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 11668 bool changed = false;
c9b8abb7
AM
11669 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11670 && object->make_toc_relative(target, &value))
11671 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11672 && object->make_got_relative(target, psymval,
11673 rela.get_r_addend(),
11674 &value)))
5edad15d
AM
11675 {
11676 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11677 insn ^= (14u << 26) ^ (58u << 26);
11678 r_type = elfcpp::R_PPC64_TOC16_LO;
11679 changed = true;
11680 }
11681 if (ok_lo_toc_insn(insn, r_type)
11682 && value + 0x8000 < 0x10000)
aba6bc71
AM
11683 {
11684 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11685 {
11686 // Transform addic to addi when we change reg.
11687 insn &= ~((0x3f << 26) | (0x1f << 16));
11688 insn |= (14u << 26) | (2 << 16);
11689 }
11690 else
11691 {
11692 insn &= ~(0x1f << 16);
11693 insn |= 2 << 16;
11694 }
5edad15d 11695 changed = true;
aba6bc71 11696 }
5edad15d
AM
11697 if (changed)
11698 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
11699 }
11700 break;
549dba71 11701
c9b8abb7 11702 case elfcpp::R_PPC64_GOT_PCREL34:
252dcdf4 11703 if (size == 64 && parameters->options().toc_optimize())
c9b8abb7 11704 {
0c951c25
AM
11705 iview = reinterpret_cast<Insn*>(view);
11706 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11707 pinsn <<= 32;
11708 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11709 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
11710 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11711 break;
11712
11713 Address relval = psymval->value(object, rela.get_r_addend());
11714 relval -= address;
11715 if (relval + (1ULL << 33) < 1ULL << 34)
11716 {
11717 value = relval;
11718 // Replace with paddi
0c951c25
AM
11719 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11720 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11721 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11722 pinsn & 0xffffffff);
11723 goto pcrelopt;
c9b8abb7
AM
11724 }
11725 }
11726 break;
11727
0c951c25 11728 case elfcpp::R_PPC64_PCREL34:
252dcdf4
AM
11729 if (size == 64)
11730 {
11731 iview = reinterpret_cast<Insn*>(view);
11732 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11733 pinsn <<= 32;
11734 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11735 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11736 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11737 | (14ULL << 26) /* paddi */))
11738 break;
0c951c25 11739
252dcdf4
AM
11740 pcrelopt:
11741 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11742 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11743 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11744 if (relnum >= reloc_count - 1)
11745 break;
0c951c25 11746
252dcdf4
AM
11747 Reltype next_rela(preloc + reloc_size);
11748 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11749 != elfcpp::R_PPC64_PCREL_OPT)
11750 || next_rela.get_r_offset() != rela.get_r_offset())
11751 break;
0c951c25 11752
252dcdf4
AM
11753 Address off = next_rela.get_r_addend();
11754 if (off == 0)
11755 off = 8; // zero means next insn.
11756 if (off + rela.get_r_offset() + 4 > view_size)
11757 break;
0c951c25 11758
252dcdf4
AM
11759 iview2 = reinterpret_cast<Insn*>(view + off);
11760 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11761 pinsn2 <<= 32;
11762 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11763 break;
11764 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11765 {
11766 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11767 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11768 pinsn & 0xffffffff);
11769 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11770 }
11771 }
0c951c25
AM
11772 break;
11773
9a23f96e 11774 case elfcpp::R_POWERPC_TPREL16_HA:
252dcdf4 11775 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11776 {
11777 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
252dcdf4
AM
11778 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11779 return true;
9a23f96e
AM
11780 }
11781 break;
11782
11783 case elfcpp::R_PPC64_TPREL16_LO_DS:
11784 if (size == 32)
11785 // R_PPC_TLSGD, R_PPC_TLSLD
11786 break;
11787 // Fall through.
11788 case elfcpp::R_POWERPC_TPREL16_LO:
252dcdf4 11789 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11790 {
11791 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11792 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11793 insn &= ~(0x1f << 16);
11794 insn |= (size == 32 ? 2 : 13) << 16;
11795 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11796 }
11797 break;
11798
549dba71 11799 case elfcpp::R_PPC64_ENTRY:
252dcdf4 11800 if (size == 64)
549dba71 11801 {
a19da04b 11802 value = target->toc_pointer();
252dcdf4
AM
11803 if (value + 0x80008000 <= 0xffffffff
11804 && !parameters->options().output_is_position_independent())
549dba71
AM
11805 {
11806 Insn* iview = reinterpret_cast<Insn*>(view);
11807 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11808 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11809
11810 if ((insn1 & ~0xfffc) == ld_2_12
11811 && insn2 == add_2_2_12)
11812 {
252dcdf4 11813 insn1 = lis_2 + ha(value);
549dba71
AM
11814 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11815 insn2 = addi_2_2 + l(value);
11816 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11817 return true;
11818 }
11819 }
252dcdf4
AM
11820 else
11821 {
11822 value -= address;
11823 if (value + 0x80008000 <= 0xffffffff)
11824 {
11825 Insn* iview = reinterpret_cast<Insn*>(view);
11826 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11827 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11828
11829 if ((insn1 & ~0xfffc) == ld_2_12
11830 && insn2 == add_2_2_12)
11831 {
11832 insn1 = addis_2_12 + ha(value);
11833 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11834 insn2 = addi_2_2 + l(value);
11835 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11836 return true;
11837 }
11838 }
11839 }
549dba71
AM
11840 }
11841 break;
e3a7574e
AM
11842
11843 case elfcpp::R_POWERPC_REL16_LO:
11844 // If we are generating a non-PIC executable, edit
11845 // 0: addis 2,12,.TOC.-0b@ha
11846 // addi 2,2,.TOC.-0b@l
11847 // used by ELFv2 global entry points to set up r2, to
11848 // lis 2,.TOC.@ha
11849 // addi 2,2,.TOC.@l
11850 // if .TOC. is in range. */
252dcdf4
AM
11851 if (size == 64
11852 && value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11853 && relnum + 1 > 1
e3a7574e
AM
11854 && preloc != NULL
11855 && target->abiversion() >= 2
11856 && !parameters->options().output_is_position_independent()
4f038ee5 11857 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11858 && gsym != NULL
11859 && strcmp(gsym->name(), ".TOC.") == 0)
11860 {
0e123f69 11861 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11862 Reltype prev_rela(preloc - reloc_size);
11863 if ((prev_rela.get_r_info()
11864 == elfcpp::elf_r_info<size>(r_sym,
11865 elfcpp::R_POWERPC_REL16_HA))
11866 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11867 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11868 {
dcfc7dd4 11869 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11870 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11871 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11872
11873 if ((insn1 & 0xffff0000) == addis_2_12
11874 && (insn2 & 0xffff0000) == addi_2_2)
11875 {
11876 insn1 = lis_2 + ha(value + address - 4);
11877 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11878 insn2 = addi_2_2 + l(value + address - 4);
11879 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11880 if (relinfo->rr)
11881 {
11882 relinfo->rr->set_strategy(relnum - 1,
11883 Relocatable_relocs::RELOC_SPECIAL);
11884 relinfo->rr->set_strategy(relnum,
11885 Relocatable_relocs::RELOC_SPECIAL);
11886 }
11887 return true;
11888 }
11889 }
11890 }
11891 break;
aba6bc71
AM
11892 }
11893 }
11894
f4baf0d4 11895 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11896 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11897 switch (r_type)
11898 {
11899 case elfcpp::R_POWERPC_ADDR32:
11900 case elfcpp::R_POWERPC_UADDR32:
11901 if (size == 64)
f4baf0d4 11902 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11903 break;
11904
11905 case elfcpp::R_POWERPC_REL32:
a680de9a 11906 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11907 if (size == 64)
f4baf0d4 11908 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11909 break;
11910
dd93cd0a 11911 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11912 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11913 break;
11914
b80eed39
AM
11915 case elfcpp::R_POWERPC_ADDR16:
11916 // We really should have three separate relocations,
11917 // one for 16-bit data, one for insns with 16-bit signed fields,
11918 // and one for insns with 16-bit unsigned fields.
11919 overflow = Reloc::CHECK_BITFIELD;
11920 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11921 overflow = Reloc::CHECK_LOW_INSN;
11922 break;
11923
f9c6b907
AM
11924 case elfcpp::R_POWERPC_ADDR16_HI:
11925 case elfcpp::R_POWERPC_ADDR16_HA:
11926 case elfcpp::R_POWERPC_GOT16_HI:
11927 case elfcpp::R_POWERPC_GOT16_HA:
11928 case elfcpp::R_POWERPC_PLT16_HI:
11929 case elfcpp::R_POWERPC_PLT16_HA:
11930 case elfcpp::R_POWERPC_SECTOFF_HI:
11931 case elfcpp::R_POWERPC_SECTOFF_HA:
11932 case elfcpp::R_PPC64_TOC16_HI:
11933 case elfcpp::R_PPC64_TOC16_HA:
11934 case elfcpp::R_PPC64_PLTGOT16_HI:
11935 case elfcpp::R_PPC64_PLTGOT16_HA:
11936 case elfcpp::R_POWERPC_TPREL16_HI:
11937 case elfcpp::R_POWERPC_TPREL16_HA:
11938 case elfcpp::R_POWERPC_DTPREL16_HI:
11939 case elfcpp::R_POWERPC_DTPREL16_HA:
11940 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11941 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11942 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11943 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11944 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11945 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11946 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11947 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11948 case elfcpp::R_POWERPC_REL16_HI:
11949 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11950 if (size != 32)
11951 overflow = Reloc::CHECK_HIGH_INSN;
11952 break;
11953
dd93cd0a
AM
11954 case elfcpp::R_POWERPC_REL16:
11955 case elfcpp::R_PPC64_TOC16:
11956 case elfcpp::R_POWERPC_GOT16:
11957 case elfcpp::R_POWERPC_SECTOFF:
11958 case elfcpp::R_POWERPC_TPREL16:
11959 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11960 case elfcpp::R_POWERPC_GOT_TLSGD16:
11961 case elfcpp::R_POWERPC_GOT_TLSLD16:
11962 case elfcpp::R_POWERPC_GOT_TPREL16:
11963 case elfcpp::R_POWERPC_GOT_DTPREL16:
11964 overflow = Reloc::CHECK_LOW_INSN;
11965 break;
11966
32f59844
AM
11967 case elfcpp::R_PPC64_REL24_NOTOC:
11968 if (size == 32)
11969 break;
11970 // Fall through.
eb97d024 11971 case elfcpp::R_PPC64_REL24_P9NOTOC:
b80eed39
AM
11972 case elfcpp::R_POWERPC_ADDR24:
11973 case elfcpp::R_POWERPC_ADDR14:
11974 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11975 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11976 case elfcpp::R_PPC64_ADDR16_DS:
11977 case elfcpp::R_POWERPC_REL24:
11978 case elfcpp::R_PPC_PLTREL24:
11979 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
11980 case elfcpp::R_PPC64_TPREL16_DS:
11981 case elfcpp::R_PPC64_DTPREL16_DS:
11982 case elfcpp::R_PPC64_TOC16_DS:
11983 case elfcpp::R_PPC64_GOT16_DS:
11984 case elfcpp::R_PPC64_SECTOFF_DS:
11985 case elfcpp::R_POWERPC_REL14:
11986 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11987 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11988 case elfcpp::R_PPC64_D34:
11989 case elfcpp::R_PPC64_PCREL34:
11990 case elfcpp::R_PPC64_GOT_PCREL34:
11991 case elfcpp::R_PPC64_PLT_PCREL34:
11992 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11993 case elfcpp::R_PPC64_D28:
11994 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
11995 case elfcpp::R_PPC64_TPREL34:
11996 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
11997 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11998 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11999 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12000 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
f4baf0d4 12001 overflow = Reloc::CHECK_SIGNED;
42cacb20 12002 break;
dd93cd0a 12003 }
42cacb20 12004
dcfc7dd4 12005 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
12006 Insn insn = 0;
12007
b80eed39
AM
12008 if (overflow == Reloc::CHECK_LOW_INSN
12009 || overflow == Reloc::CHECK_HIGH_INSN)
12010 {
a680de9a 12011 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 12012
a47622ac
AM
12013 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
12014 overflow = Reloc::CHECK_BITFIELD;
12015 else if (overflow == Reloc::CHECK_LOW_INSN
12016 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
12017 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
12018 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
12019 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
12020 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
12021 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 12022 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
12023 else
12024 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
12025 }
12026
a680de9a 12027 bool maybe_dq_reloc = false;
3ea0a085 12028 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 12029 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
12030 switch (r_type)
12031 {
12032 case elfcpp::R_POWERPC_NONE:
12033 case elfcpp::R_POWERPC_TLS:
12034 case elfcpp::R_POWERPC_GNU_VTINHERIT:
12035 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
12036 case elfcpp::R_POWERPC_PLTSEQ:
12037 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
12038 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12039 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 12040 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
12041 break;
12042
12043 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 12044 case elfcpp::R_PPC64_REL64:
cf43a2fe 12045 case elfcpp::R_PPC64_TOC:
45965137 12046 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
12047 Reloc::addr64(view, value);
12048 break;
12049
12050 case elfcpp::R_POWERPC_TPREL:
12051 case elfcpp::R_POWERPC_DTPREL:
12052 if (size == 64)
12053 Reloc::addr64(view, value);
12054 else
3ea0a085 12055 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
12056 break;
12057
12058 case elfcpp::R_PPC64_UADDR64:
12059 Reloc::addr64_u(view, value);
42cacb20
DE
12060 break;
12061
12062 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 12063 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
12064 break;
12065
acc276d8 12066 case elfcpp::R_POWERPC_REL32:
dd93cd0a 12067 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 12068 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
12069 break;
12070
32f59844
AM
12071 case elfcpp::R_PPC64_REL24_NOTOC:
12072 if (size == 32)
12073 goto unsupp; // R_PPC_EMB_RELSDA
12074 // Fall through.
eb97d024 12075 case elfcpp::R_PPC64_REL24_P9NOTOC:
dd93cd0a
AM
12076 case elfcpp::R_POWERPC_ADDR24:
12077 case elfcpp::R_POWERPC_REL24:
12078 case elfcpp::R_PPC_PLTREL24:
12079 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 12080 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
12081 break;
12082
dd93cd0a
AM
12083 case elfcpp::R_POWERPC_GOT_DTPREL16:
12084 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
12085 case elfcpp::R_POWERPC_GOT_TPREL16:
12086 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
12087 if (size == 64)
12088 {
ec86f434 12089 // On ppc64 these are all ds form
a680de9a 12090 maybe_dq_reloc = true;
dd93cd0a
AM
12091 break;
12092 }
c25aa1e1 12093 // Fall through.
cf43a2fe 12094 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 12095 case elfcpp::R_POWERPC_REL16:
cf43a2fe 12096 case elfcpp::R_PPC64_TOC16:
42cacb20 12097 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 12098 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
12099 case elfcpp::R_POWERPC_TPREL16:
12100 case elfcpp::R_POWERPC_DTPREL16:
12101 case elfcpp::R_POWERPC_GOT_TLSGD16:
12102 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 12103 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 12104 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 12105 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 12106 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 12107 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 12108 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
12109 case elfcpp::R_POWERPC_TPREL16_LO:
12110 case elfcpp::R_POWERPC_DTPREL16_LO:
12111 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12112 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
12113 if (size == 64)
12114 status = Reloc::addr16(view, value, overflow);
12115 else
12116 maybe_dq_reloc = true;
dd93cd0a
AM
12117 break;
12118
12119 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 12120 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
12121 break;
12122
f9c6b907
AM
12123 case elfcpp::R_PPC64_ADDR16_HIGH:
12124 case elfcpp::R_PPC64_TPREL16_HIGH:
12125 case elfcpp::R_PPC64_DTPREL16_HIGH:
12126 if (size == 32)
12127 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12128 goto unsupp;
d8e90251 12129 // Fall through.
cf43a2fe 12130 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 12131 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 12132 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 12133 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 12134 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 12135 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 12136 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
12137 case elfcpp::R_POWERPC_TPREL16_HI:
12138 case elfcpp::R_POWERPC_DTPREL16_HI:
12139 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12140 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12141 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12142 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12143 Reloc::addr16_hi(view, value);
42cacb20
DE
12144 break;
12145
f9c6b907
AM
12146 case elfcpp::R_PPC64_ADDR16_HIGHA:
12147 case elfcpp::R_PPC64_TPREL16_HIGHA:
12148 case elfcpp::R_PPC64_DTPREL16_HIGHA:
12149 if (size == 32)
12150 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12151 goto unsupp;
d8e90251 12152 // Fall through.
cf43a2fe 12153 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 12154 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 12155 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 12156 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 12157 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 12158 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 12159 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
12160 case elfcpp::R_POWERPC_TPREL16_HA:
12161 case elfcpp::R_POWERPC_DTPREL16_HA:
12162 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12163 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12164 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12165 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12166 Reloc::addr16_ha(view, value);
42cacb20
DE
12167 break;
12168
a680de9a
PB
12169 case elfcpp::R_POWERPC_REL16DX_HA:
12170 status = Reloc::addr16dx_ha(view, value, overflow);
12171 break;
12172
dd93cd0a
AM
12173 case elfcpp::R_PPC64_DTPREL16_HIGHER:
12174 if (size == 32)
12175 // R_PPC_EMB_NADDR16_LO
12176 goto unsupp;
d8e90251 12177 // Fall through.
dd93cd0a 12178 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 12179 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
12180 case elfcpp::R_PPC64_TPREL16_HIGHER:
12181 Reloc::addr16_hi2(view, value);
42cacb20
DE
12182 break;
12183
dd93cd0a
AM
12184 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12185 if (size == 32)
12186 // R_PPC_EMB_NADDR16_HI
12187 goto unsupp;
d8e90251 12188 // Fall through.
dd93cd0a 12189 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 12190 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
12191 case elfcpp::R_PPC64_TPREL16_HIGHERA:
12192 Reloc::addr16_ha2(view, value);
42cacb20
DE
12193 break;
12194
dd93cd0a
AM
12195 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12196 if (size == 32)
12197 // R_PPC_EMB_NADDR16_HA
12198 goto unsupp;
d8e90251 12199 // Fall through.
dd93cd0a 12200 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 12201 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
12202 case elfcpp::R_PPC64_TPREL16_HIGHEST:
12203 Reloc::addr16_hi3(view, value);
42cacb20
DE
12204 break;
12205
dd93cd0a
AM
12206 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12207 if (size == 32)
12208 // R_PPC_EMB_SDAI16
12209 goto unsupp;
d8e90251 12210 // Fall through.
dd93cd0a 12211 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 12212 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
12213 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12214 Reloc::addr16_ha3(view, value);
12215 break;
12216
12217 case elfcpp::R_PPC64_DTPREL16_DS:
12218 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12219 if (size == 32)
12220 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12221 goto unsupp;
d8e90251 12222 // Fall through.
dd93cd0a
AM
12223 case elfcpp::R_PPC64_TPREL16_DS:
12224 case elfcpp::R_PPC64_TPREL16_LO_DS:
12225 if (size == 32)
12226 // R_PPC_TLSGD, R_PPC_TLSLD
12227 break;
d8e90251 12228 // Fall through.
cf43a2fe
AM
12229 case elfcpp::R_PPC64_ADDR16_DS:
12230 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
12231 case elfcpp::R_PPC64_TOC16_DS:
12232 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
12233 case elfcpp::R_PPC64_GOT16_DS:
12234 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 12235 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
12236 case elfcpp::R_PPC64_SECTOFF_DS:
12237 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 12238 maybe_dq_reloc = true;
dd93cd0a
AM
12239 break;
12240
12241 case elfcpp::R_POWERPC_ADDR14:
12242 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12243 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12244 case elfcpp::R_POWERPC_REL14:
12245 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12246 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 12247 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
12248 break;
12249
12250 case elfcpp::R_POWERPC_COPY:
12251 case elfcpp::R_POWERPC_GLOB_DAT:
12252 case elfcpp::R_POWERPC_JMP_SLOT:
12253 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 12254 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
12255 case elfcpp::R_PPC64_JMP_IREL:
12256 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
12257 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12258 _("unexpected reloc %u in object file"),
12259 r_type);
12260 break;
12261
7e57d19e 12262 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 12263 if (size == 32)
7e57d19e 12264 // R_PPC_EMB_SDA21
dd93cd0a
AM
12265 goto unsupp;
12266 else
12267 {
7e57d19e
AM
12268 Symbol_location loc;
12269 loc.object = relinfo->object;
12270 loc.shndx = relinfo->data_shndx;
12271 loc.offset = rela.get_r_offset();
b0d0d02b
AM
12272 const Tocsave_loc *tocsave = target->tocsave_loc();
12273 if (tocsave->find(loc) != tocsave->end())
7e57d19e
AM
12274 {
12275 // If we've generated plt calls using this tocsave, then
12276 // the nop needs to be changed to save r2.
12277 Insn* iview = reinterpret_cast<Insn*>(view);
12278 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12279 elfcpp::Swap<32, big_endian>::
12280 writeval(iview, std_2_1 + target->stk_toc());
12281 }
dd93cd0a
AM
12282 }
12283 break;
12284
12285 case elfcpp::R_PPC_EMB_SDA2I16:
12286 case elfcpp::R_PPC_EMB_SDA2REL:
12287 if (size == 32)
12288 goto unsupp;
12289 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
12290 break;
12291
e4dff765
AM
12292 case elfcpp::R_PPC64_D34:
12293 case elfcpp::R_PPC64_D34_LO:
12294 case elfcpp::R_PPC64_PCREL34:
12295 case elfcpp::R_PPC64_GOT_PCREL34:
12296 case elfcpp::R_PPC64_PLT_PCREL34:
12297 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
12298 case elfcpp::R_PPC64_TPREL34:
12299 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
12300 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12301 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12302 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12303 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
12304 if (size == 32)
12305 goto unsupp;
12306 status = Reloc::addr34(view, value, overflow);
12307 break;
12308
12309 case elfcpp::R_PPC64_D34_HI30:
12310 if (size == 32)
12311 goto unsupp;
12312 Reloc::addr34_hi(view, value);
12313 break;
12314
12315 case elfcpp::R_PPC64_D34_HA30:
12316 if (size == 32)
12317 goto unsupp;
12318 Reloc::addr34_ha(view, value);
12319 break;
12320
12321 case elfcpp::R_PPC64_D28:
12322 case elfcpp::R_PPC64_PCREL28:
12323 if (size == 32)
12324 goto unsupp;
12325 status = Reloc::addr28(view, value, overflow);
12326 break;
12327
12328 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12329 case elfcpp::R_PPC64_REL16_HIGHER34:
12330 if (size == 32)
12331 goto unsupp;
12332 Reloc::addr16_higher34(view, value);
12333 break;
12334
12335 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12336 case elfcpp::R_PPC64_REL16_HIGHERA34:
12337 if (size == 32)
12338 goto unsupp;
12339 Reloc::addr16_highera34(view, value);
12340 break;
12341
12342 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12343 case elfcpp::R_PPC64_REL16_HIGHEST34:
12344 if (size == 32)
12345 goto unsupp;
12346 Reloc::addr16_highest34(view, value);
12347 break;
12348
12349 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12350 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12351 if (size == 32)
12352 goto unsupp;
12353 Reloc::addr16_highesta34(view, value);
12354 break;
12355
dd93cd0a
AM
12356 case elfcpp::R_POWERPC_PLT32:
12357 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
12358 case elfcpp::R_PPC_SDAREL16:
12359 case elfcpp::R_POWERPC_ADDR30:
12360 case elfcpp::R_PPC64_PLT64:
12361 case elfcpp::R_PPC64_PLTREL64:
12362 case elfcpp::R_PPC64_PLTGOT16:
12363 case elfcpp::R_PPC64_PLTGOT16_LO:
12364 case elfcpp::R_PPC64_PLTGOT16_HI:
12365 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
12366 case elfcpp::R_PPC64_PLTGOT16_DS:
12367 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 12368 case elfcpp::R_PPC_TOC16:
42cacb20 12369 default:
dd93cd0a 12370 unsupp:
42cacb20
DE
12371 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12372 _("unsupported reloc %u"),
12373 r_type);
12374 break;
12375 }
a680de9a
PB
12376
12377 if (maybe_dq_reloc)
12378 {
12379 if (insn == 0)
12380 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12381
12382 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12383 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12384 && (insn & 3) == 1))
12385 status = Reloc::addr16_dq(view, value, overflow);
12386 else if (size == 64
12387 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12388 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12389 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12390 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12391 status = Reloc::addr16_ds(view, value, overflow);
12392 else
12393 status = Reloc::addr16(view, value, overflow);
12394 }
12395
0cfb0717 12396 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
12397 && (has_stub_value
12398 || !(gsym != NULL
282c9750 12399 && gsym->is_undefined()
32f59844 12400 && is_branch_reloc<size>(r_type))))
0cfb0717
AM
12401 {
12402 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12403 _("relocation overflow"));
12404 if (has_stub_value)
12405 gold_info(_("try relinking with a smaller --stub-group-size"));
12406 }
42cacb20
DE
12407
12408 return true;
12409}
12410
42cacb20
DE
12411// Relocate section data.
12412
12413template<int size, bool big_endian>
12414void
12415Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
12416 const Relocate_info<size, big_endian>* relinfo,
12417 unsigned int sh_type,
12418 const unsigned char* prelocs,
12419 size_t reloc_count,
12420 Output_section* output_section,
12421 bool needs_special_offset_handling,
12422 unsigned char* view,
c9269dff 12423 Address address,
d83ce4e3
AM
12424 section_size_type view_size,
12425 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
12426{
12427 typedef Target_powerpc<size, big_endian> Powerpc;
12428 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
12429 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12430 Powerpc_comdat_behavior;
4d625b70
CC
12431 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12432 Classify_reloc;
42cacb20
DE
12433
12434 gold_assert(sh_type == elfcpp::SHT_RELA);
12435
4d625b70
CC
12436 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12437 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
12438 relinfo,
12439 this,
12440 prelocs,
12441 reloc_count,
12442 output_section,
12443 needs_special_offset_handling,
12444 view,
12445 address,
364c7fa5
ILT
12446 view_size,
12447 reloc_symbol_changes);
42cacb20
DE
12448}
12449
4d625b70 12450template<int size, bool big_endian>
cf43a2fe 12451class Powerpc_scan_relocatable_reloc
42cacb20 12452{
cf43a2fe 12453public:
0e123f69
AM
12454 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12455 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
12456 static const int sh_type = elfcpp::SHT_RELA;
12457
12458 // Return the symbol referred to by the relocation.
12459 static inline unsigned int
12460 get_r_sym(const Reltype* reloc)
12461 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12462
12463 // Return the type of the relocation.
12464 static inline unsigned int
12465 get_r_type(const Reltype* reloc)
12466 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12467
cf43a2fe
AM
12468 // Return the strategy to use for a local symbol which is not a
12469 // section symbol, given the relocation type.
12470 inline Relocatable_relocs::Reloc_strategy
12471 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12472 {
12473 if (r_type == 0 && r_sym == 0)
12474 return Relocatable_relocs::RELOC_DISCARD;
12475 return Relocatable_relocs::RELOC_COPY;
12476 }
12477
12478 // Return the strategy to use for a local symbol which is a section
12479 // symbol, given the relocation type.
12480 inline Relocatable_relocs::Reloc_strategy
12481 local_section_strategy(unsigned int, Relobj*)
12482 {
12483 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12484 }
12485
12486 // Return the strategy to use for a global symbol, given the
12487 // relocation type, the object, and the symbol index.
12488 inline Relocatable_relocs::Reloc_strategy
12489 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12490 {
08be3224
AM
12491 if (size == 32
12492 && (r_type == elfcpp::R_PPC_PLTREL24
12493 || r_type == elfcpp::R_POWERPC_PLT16_LO
12494 || r_type == elfcpp::R_POWERPC_PLT16_HI
12495 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
12496 return Relocatable_relocs::RELOC_SPECIAL;
12497 return Relocatable_relocs::RELOC_COPY;
12498 }
12499};
42cacb20
DE
12500
12501// Scan the relocs during a relocatable link.
12502
12503template<int size, bool big_endian>
12504void
12505Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
12506 Symbol_table* symtab,
12507 Layout* layout,
12508 Sized_relobj_file<size, big_endian>* object,
12509 unsigned int data_shndx,
12510 unsigned int sh_type,
12511 const unsigned char* prelocs,
12512 size_t reloc_count,
12513 Output_section* output_section,
12514 bool needs_special_offset_handling,
12515 size_t local_symbol_count,
12516 const unsigned char* plocal_symbols,
12517 Relocatable_relocs* rr)
42cacb20 12518{
4d625b70
CC
12519 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12520
42cacb20
DE
12521 gold_assert(sh_type == elfcpp::SHT_RELA);
12522
4d625b70 12523 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
12524 symtab,
12525 layout,
12526 object,
12527 data_shndx,
12528 prelocs,
12529 reloc_count,
12530 output_section,
12531 needs_special_offset_handling,
12532 local_symbol_count,
12533 plocal_symbols,
12534 rr);
12535}
12536
4d625b70
CC
12537// Scan the relocs for --emit-relocs.
12538
12539template<int size, bool big_endian>
12540void
12541Target_powerpc<size, big_endian>::emit_relocs_scan(
12542 Symbol_table* symtab,
12543 Layout* layout,
12544 Sized_relobj_file<size, big_endian>* object,
12545 unsigned int data_shndx,
12546 unsigned int sh_type,
12547 const unsigned char* prelocs,
12548 size_t reloc_count,
12549 Output_section* output_section,
12550 bool needs_special_offset_handling,
12551 size_t local_symbol_count,
12552 const unsigned char* plocal_syms,
12553 Relocatable_relocs* rr)
12554{
12555 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12556 Classify_reloc;
12557 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12558 Emit_relocs_strategy;
12559
12560 gold_assert(sh_type == elfcpp::SHT_RELA);
12561
12562 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12563 symtab,
12564 layout,
12565 object,
12566 data_shndx,
12567 prelocs,
12568 reloc_count,
12569 output_section,
12570 needs_special_offset_handling,
12571 local_symbol_count,
12572 plocal_syms,
12573 rr);
12574}
12575
7404fe1b 12576// Emit relocations for a section.
dd93cd0a
AM
12577// This is a modified version of the function by the same name in
12578// target-reloc.h. Using relocate_special_relocatable for
12579// R_PPC_PLTREL24 would require duplication of the entire body of the
12580// loop, so we may as well duplicate the whole thing.
42cacb20
DE
12581
12582template<int size, bool big_endian>
12583void
7404fe1b 12584Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
12585 const Relocate_info<size, big_endian>* relinfo,
12586 unsigned int sh_type,
12587 const unsigned char* prelocs,
12588 size_t reloc_count,
12589 Output_section* output_section,
62fe925a 12590 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 12591 unsigned char*,
dd93cd0a 12592 Address view_address,
cf43a2fe 12593 section_size_type,
42cacb20
DE
12594 unsigned char* reloc_view,
12595 section_size_type reloc_view_size)
12596{
12597 gold_assert(sh_type == elfcpp::SHT_RELA);
12598
0e123f69
AM
12599 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12600 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12601 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
12602 // Offset from start of insn to d-field reloc.
12603 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
12604
12605 Powerpc_relobj<size, big_endian>* const object
12606 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12607 const unsigned int local_count = object->local_symbol_count();
12608 unsigned int got2_shndx = object->got2_shndx();
c9269dff 12609 Address got2_addend = 0;
cf43a2fe 12610 if (got2_shndx != 0)
c9269dff
AM
12611 {
12612 got2_addend = object->get_output_section_offset(got2_shndx);
12613 gold_assert(got2_addend != invalid_address);
12614 }
cf43a2fe 12615
033bfb73
CC
12616 const bool relocatable = parameters->options().relocatable();
12617
cf43a2fe 12618 unsigned char* pwrite = reloc_view;
7404fe1b 12619 bool zap_next = false;
cf43a2fe
AM
12620 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12621 {
91a65d2f 12622 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
12623 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12624 continue;
12625
12626 Reltype reloc(prelocs);
12627 Reltype_write reloc_write(pwrite);
12628
7404fe1b 12629 Address offset = reloc.get_r_offset();
cf43a2fe 12630 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
12631 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12632 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12633 const unsigned int orig_r_sym = r_sym;
12634 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12635 = reloc.get_r_addend();
12636 const Symbol* gsym = NULL;
12637
12638 if (zap_next)
12639 {
12640 // We could arrange to discard these and other relocs for
12641 // tls optimised sequences in the strategy methods, but for
12642 // now do as BFD ld does.
12643 r_type = elfcpp::R_POWERPC_NONE;
12644 zap_next = false;
12645 }
cf43a2fe
AM
12646
12647 // Get the new symbol index.
9215b98b 12648 Output_section* os = NULL;
cf43a2fe
AM
12649 if (r_sym < local_count)
12650 {
12651 switch (strategy)
12652 {
12653 case Relocatable_relocs::RELOC_COPY:
12654 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 12655 if (r_sym != 0)
dd93cd0a 12656 {
7404fe1b
AM
12657 r_sym = object->symtab_index(r_sym);
12658 gold_assert(r_sym != -1U);
dd93cd0a 12659 }
cf43a2fe
AM
12660 break;
12661
12662 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12663 {
12664 // We are adjusting a section symbol. We need to find
12665 // the symbol table index of the section symbol for
12666 // the output section corresponding to input section
12667 // in which this symbol is defined.
12668 gold_assert(r_sym < local_count);
12669 bool is_ordinary;
12670 unsigned int shndx =
12671 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12672 gold_assert(is_ordinary);
9215b98b 12673 os = object->output_section(shndx);
cf43a2fe
AM
12674 gold_assert(os != NULL);
12675 gold_assert(os->needs_symtab_index());
7404fe1b 12676 r_sym = os->symtab_index();
cf43a2fe
AM
12677 }
12678 break;
12679
12680 default:
12681 gold_unreachable();
12682 }
12683 }
12684 else
12685 {
7404fe1b 12686 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
12687 gold_assert(gsym != NULL);
12688 if (gsym->is_forwarder())
12689 gsym = relinfo->symtab->resolve_forwards(gsym);
12690
12691 gold_assert(gsym->has_symtab_index());
7404fe1b 12692 r_sym = gsym->symtab_index();
cf43a2fe
AM
12693 }
12694
12695 // Get the new offset--the location in the output section where
12696 // this relocation should be applied.
cf43a2fe 12697 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12698 offset += offset_in_output_section;
cf43a2fe
AM
12699 else
12700 {
c9269dff
AM
12701 section_offset_type sot_offset =
12702 convert_types<section_offset_type, Address>(offset);
cf43a2fe 12703 section_offset_type new_sot_offset =
c9269dff
AM
12704 output_section->output_offset(object, relinfo->data_shndx,
12705 sot_offset);
cf43a2fe 12706 gold_assert(new_sot_offset != -1);
7404fe1b 12707 offset = new_sot_offset;
cf43a2fe
AM
12708 }
12709
dd93cd0a
AM
12710 // In an object file, r_offset is an offset within the section.
12711 // In an executable or dynamic object, generated by
12712 // --emit-relocs, r_offset is an absolute address.
033bfb73 12713 if (!relocatable)
dd93cd0a 12714 {
7404fe1b 12715 offset += view_address;
dd93cd0a 12716 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12717 offset -= offset_in_output_section;
dd93cd0a
AM
12718 }
12719
cf43a2fe 12720 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12721 if (strategy == Relocatable_relocs::RELOC_COPY)
12722 ;
12723 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12724 {
7404fe1b 12725 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12726 addend = psymval->value(object, addend);
12727 // In a relocatable link, the symbol value is relative to
12728 // the start of the output section. For a non-relocatable
12729 // link, we need to adjust the addend.
12730 if (!relocatable)
12731 {
12732 gold_assert(os != NULL);
12733 addend -= os->address();
12734 }
cf43a2fe
AM
12735 }
12736 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12737 {
e3a7574e
AM
12738 if (size == 32)
12739 {
12740 if (addend >= 32768)
12741 addend += got2_addend;
12742 }
12743 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12744 {
12745 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12746 addend -= d_offset;
e3a7574e
AM
12747 }
12748 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12749 {
12750 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12751 addend -= d_offset + 4;
e3a7574e 12752 }
cf43a2fe
AM
12753 }
12754 else
12755 gold_unreachable();
12756
033bfb73 12757 if (!relocatable)
7404fe1b
AM
12758 {
12759 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12760 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12761 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12762 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12763 {
12764 // First instruction of a global dynamic sequence,
12765 // arg setup insn.
c7fdac09
AM
12766 bool final = gsym == NULL || gsym->final_value_is_known();
12767 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
0af4fcc2 12768 switch (tls_type)
7404fe1b
AM
12769 {
12770 case tls::TLSOPT_TO_IE:
12771 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12772 - elfcpp::R_POWERPC_GOT_TLSGD16);
12773 break;
12774 case tls::TLSOPT_TO_LE:
12775 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12776 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12777 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12778 else
12779 {
12780 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12781 offset -= d_offset;
7404fe1b
AM
12782 }
12783 break;
12784 default:
12785 break;
12786 }
12787 }
12788 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12789 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12790 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12791 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12792 {
12793 // First instruction of a local dynamic sequence,
12794 // arg setup insn.
c7fdac09 12795 tls::Tls_optimization tls_type = this->optimize_tls_ld();
0af4fcc2 12796 if (tls_type == tls::TLSOPT_TO_LE)
7404fe1b
AM
12797 {
12798 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12799 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12800 {
12801 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12802 const Output_section* os = relinfo->layout->tls_segment()
12803 ->first_section();
12804 gold_assert(os != NULL);
12805 gold_assert(os->needs_symtab_index());
12806 r_sym = os->symtab_index();
12807 addend = dtp_offset;
12808 }
12809 else
12810 {
12811 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12812 offset -= d_offset;
7404fe1b
AM
12813 }
12814 }
12815 }
12816 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12817 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12818 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12819 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12820 {
12821 // First instruction of initial exec sequence.
0af4fcc2 12822 bool final = gsym == NULL || gsym->final_value_is_known();
7404fe1b
AM
12823 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12824 {
12825 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12826 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12827 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12828 else
12829 {
12830 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12831 offset -= d_offset;
7404fe1b
AM
12832 }
12833 }
12834 }
12835 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12836 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12837 {
12838 // Second instruction of a global dynamic sequence,
12839 // the __tls_get_addr call
c7fdac09
AM
12840 bool final = gsym == NULL || gsym->final_value_is_known();
12841 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
0af4fcc2 12842 switch (tls_type)
7404fe1b
AM
12843 {
12844 case tls::TLSOPT_TO_IE:
12845 r_type = elfcpp::R_POWERPC_NONE;
12846 zap_next = true;
12847 break;
12848 case tls::TLSOPT_TO_LE:
12849 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12850 offset += d_offset;
7404fe1b
AM
12851 zap_next = true;
12852 break;
12853 default:
12854 break;
12855 }
12856 }
12857 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12858 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12859 {
12860 // Second instruction of a local dynamic sequence,
12861 // the __tls_get_addr call
c7fdac09 12862 tls::Tls_optimization tls_type = this->optimize_tls_ld();
0af4fcc2 12863 if (tls_type == tls::TLSOPT_TO_LE)
7404fe1b
AM
12864 {
12865 const Output_section* os = relinfo->layout->tls_segment()
12866 ->first_section();
12867 gold_assert(os != NULL);
12868 gold_assert(os->needs_symtab_index());
12869 r_sym = os->symtab_index();
12870 addend = dtp_offset;
12871 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12872 offset += d_offset;
7404fe1b
AM
12873 zap_next = true;
12874 }
12875 }
12876 else if (r_type == elfcpp::R_POWERPC_TLS)
12877 {
12878 // Second instruction of an initial exec sequence
0af4fcc2 12879 bool final = gsym == NULL || gsym->final_value_is_known();
7404fe1b
AM
12880 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12881 {
12882 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12883 offset += d_offset;
7404fe1b
AM
12884 }
12885 }
12886 }
12887
12888 reloc_write.put_r_offset(offset);
12889 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12890 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12891
12892 pwrite += reloc_size;
12893 }
12894
12895 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12896 == reloc_view_size);
42cacb20
DE
12897}
12898
ec661b9d 12899// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12900// treatment. This is how we support equality comparisons of function
12901// pointers across shared library boundaries, as described in the
12902// processor specific ABI supplement.
12903
12904template<int size, bool big_endian>
12905uint64_t
12906Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12907{
cf43a2fe
AM
12908 if (size == 32)
12909 {
12910 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12911 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12912 p != this->stub_tables_.end();
12913 ++p)
12914 {
7e57d19e
AM
12915 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12916 = (*p)->find_plt_call_entry(gsym);
12917 if (ent != NULL)
12918 return (*p)->stub_address() + ent->off_;
ec661b9d 12919 }
c9824451 12920 }
9055360d
AM
12921 else if (this->abiversion() >= 2)
12922 {
faa2211d
AM
12923 Address off = this->glink_section()->find_global_entry(gsym);
12924 if (off != invalid_address)
9055360d
AM
12925 return this->glink_section()->global_entry_address() + off;
12926 }
ec661b9d 12927 gold_unreachable();
c9824451
AM
12928}
12929
12930// Return the PLT address to use for a local symbol.
12931template<int size, bool big_endian>
12932uint64_t
12933Target_powerpc<size, big_endian>::do_plt_address_for_local(
12934 const Relobj* object,
12935 unsigned int symndx) const
12936{
12937 if (size == 32)
12938 {
12939 const Sized_relobj<size, big_endian>* relobj
12940 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12941 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12942 p != this->stub_tables_.end();
12943 ++p)
12944 {
7e57d19e
AM
12945 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12946 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12947 if (ent != NULL)
12948 return (*p)->stub_address() + ent->off_;
ec661b9d 12949 }
c9824451 12950 }
ec661b9d 12951 gold_unreachable();
c9824451
AM
12952}
12953
12954// Return the PLT address to use for a global symbol.
12955template<int size, bool big_endian>
12956uint64_t
12957Target_powerpc<size, big_endian>::do_plt_address_for_global(
12958 const Symbol* gsym) const
12959{
12960 if (size == 32)
12961 {
ec661b9d
AM
12962 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12963 p != this->stub_tables_.end();
12964 ++p)
12965 {
7e57d19e
AM
12966 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12967 = (*p)->find_plt_call_entry(gsym);
12968 if (ent != NULL)
12969 return (*p)->stub_address() + ent->off_;
ec661b9d 12970 }
cf43a2fe 12971 }
9055360d
AM
12972 else if (this->abiversion() >= 2)
12973 {
faa2211d
AM
12974 Address off = this->glink_section()->find_global_entry(gsym);
12975 if (off != invalid_address)
9055360d
AM
12976 return this->glink_section()->global_entry_address() + off;
12977 }
ec661b9d 12978 gold_unreachable();
42cacb20
DE
12979}
12980
bd73a62d
AM
12981// Return the offset to use for the GOT_INDX'th got entry which is
12982// for a local tls symbol specified by OBJECT, SYMNDX.
12983template<int size, bool big_endian>
12984int64_t
12985Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12986 const Relobj* object,
12987 unsigned int symndx,
f19c3684 12988 Output_data_got_base* got,
e4d49a0f
AM
12989 unsigned int got_indx,
12990 uint64_t addend) const
bd73a62d
AM
12991{
12992 const Powerpc_relobj<size, big_endian>* ppc_object
12993 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12994 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12995 {
f19c3684
AM
12996 for (Got_type got_type = (size == 32
12997 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
12998 got_type <= GOT_TYPE_SMALL_TPREL;
bd73a62d 12999 got_type = Got_type(got_type + 1))
f19c3684
AM
13000 if (got_type != GOT_TYPE_SMALL
13001 && ppc_object->local_has_got_offset(symndx, got_type, addend))
bd73a62d 13002 {
e4d49a0f
AM
13003 unsigned int off
13004 = ppc_object->local_got_offset(symndx, got_type, addend);
f19c3684 13005 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
bd73a62d 13006 off += size / 8;
f19c3684
AM
13007 if (off == got_indx * (size / 8)
13008 && (size == 32 || got == this->got_section(got_type)))
bd73a62d 13009 {
f19c3684 13010 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
bd73a62d
AM
13011 return -tp_offset;
13012 else
13013 return -dtp_offset;
13014 }
13015 }
13016 }
13017 gold_unreachable();
13018}
13019
13020// Return the offset to use for the GOT_INDX'th got entry which is
13021// for global tls symbol GSYM.
13022template<int size, bool big_endian>
13023int64_t
13024Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13025 Symbol* gsym,
f19c3684 13026 Output_data_got_base* got,
e4d49a0f
AM
13027 unsigned int got_indx,
13028 uint64_t addend) const
bd73a62d
AM
13029{
13030 if (gsym->type() == elfcpp::STT_TLS)
13031 {
f19c3684
AM
13032 for (Got_type got_type = (size == 32
13033 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13034 got_type <= GOT_TYPE_SMALL_TPREL;
bd73a62d 13035 got_type = Got_type(got_type + 1))
f19c3684
AM
13036 if (got_type != GOT_TYPE_SMALL
13037 && gsym->has_got_offset(got_type, addend))
bd73a62d 13038 {
e4d49a0f 13039 unsigned int off = gsym->got_offset(got_type, addend);
f19c3684 13040 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
bd73a62d 13041 off += size / 8;
f19c3684
AM
13042 if (off == got_indx * (size / 8)
13043 && (size == 32 || got == this->got_section(got_type)))
bd73a62d 13044 {
f19c3684 13045 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
bd73a62d
AM
13046 return -tp_offset;
13047 else
13048 return -dtp_offset;
13049 }
13050 }
13051 }
13052 gold_unreachable();
13053}
13054
42cacb20
DE
13055// The selector for powerpc object files.
13056
13057template<int size, bool big_endian>
13058class Target_selector_powerpc : public Target_selector
13059{
13060public:
13061 Target_selector_powerpc()
edc27beb
AM
13062 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13063 size, big_endian,
03ef7571
ILT
13064 (size == 64
13065 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13066 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13067 (size == 64
13068 ? (big_endian ? "elf64ppc" : "elf64lppc")
13069 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
13070 { }
13071
2e702c99
RM
13072 virtual Target*
13073 do_instantiate_target()
7f055c20 13074 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
13075};
13076
13077Target_selector_powerpc<32, true> target_selector_ppc32;
13078Target_selector_powerpc<32, false> target_selector_ppc32le;
13079Target_selector_powerpc<64, true> target_selector_ppc64;
13080Target_selector_powerpc<64, false> target_selector_ppc64le;
13081
decdd3bc
AM
13082// Instantiate these constants for -O0
13083template<int size, bool big_endian>
9055360d
AM
13084const typename Output_data_glink<size, big_endian>::Address
13085 Output_data_glink<size, big_endian>::invalid_address;
13086template<int size, bool big_endian>
decdd3bc
AM
13087const typename Stub_table<size, big_endian>::Address
13088 Stub_table<size, big_endian>::invalid_address;
13089template<int size, bool big_endian>
13090const typename Target_powerpc<size, big_endian>::Address
13091 Target_powerpc<size, big_endian>::invalid_address;
13092
42cacb20 13093} // End anonymous namespace.