PR27140, ppc32 segmentation fault in make_stub
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
250d07de 3// Copyright (C) 2008-2021 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
dd93cd0a
AM
319 // Return offset in output GOT section that this object will use
320 // as a TOC pointer. Won't be just a constant with multi-toc support.
321 Address
322 toc_base_offset() const
323 { return 0x8000; }
324
d8f5a274
AM
325 void
326 set_has_small_toc_reloc()
327 { has_small_toc_reloc_ = true; }
328
329 bool
330 has_small_toc_reloc() const
331 { return has_small_toc_reloc_; }
332
ec661b9d
AM
333 void
334 set_has_14bit_branch(unsigned int shndx)
335 {
336 if (shndx >= this->has14_.size())
337 this->has14_.resize(shndx + 1);
338 this->has14_[shndx] = true;
339 }
340
341 bool
342 has_14bit_branch(unsigned int shndx) const
343 { return shndx < this->has14_.size() && this->has14_[shndx]; }
344
345 void
a3e60ddb 346 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 347 {
a3e60ddb 348 if (shndx >= this->stub_table_index_.size())
dc60b26d 349 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 350 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
351 }
352
353 Stub_table<size, big_endian>*
354 stub_table(unsigned int shndx)
355 {
a3e60ddb
AM
356 if (shndx < this->stub_table_index_.size())
357 {
358 Target_powerpc<size, big_endian>* target
359 = static_cast<Target_powerpc<size, big_endian>*>(
360 parameters->sized_target<size, big_endian>());
361 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
362 if (indx < target->stub_tables().size())
363 return target->stub_tables()[indx];
a3e60ddb 364 }
ec661b9d
AM
365 return NULL;
366 }
367
a3e60ddb
AM
368 void
369 clear_stub_table()
370 {
371 this->stub_table_index_.clear();
372 }
373
590b87ff
AM
374 uint32_t
375 uniq() const
376 { return this->uniq_; }
377
b4f7960d
AM
378 int
379 abiversion() const
380 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
381
382 // Set ABI version for input and output
383 void
384 set_abiversion(int ver);
385
7ee7ff70
AM
386 unsigned int
387 st_other (unsigned int symndx) const
388 {
389 return this->st_other_[symndx];
390 }
391
b4f7960d
AM
392 unsigned int
393 ppc64_local_entry_offset(const Symbol* sym) const
394 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
395
396 unsigned int
397 ppc64_local_entry_offset(unsigned int symndx) const
398 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
399
32f59844
AM
400 bool
401 ppc64_needs_toc(const Symbol* sym) const
402 { return sym->nonvis() > 1 << 3; }
403
404 bool
405 ppc64_needs_toc(unsigned int symndx) const
406 { return this->st_other_[symndx] > 1 << 5; }
407
724436fc
AM
408 // The contents of the .gnu.attributes section if there is one.
409 const Attributes_section_data*
410 attributes_section_data() const
411 { return this->attributes_section_data_; }
412
cf43a2fe 413private:
bfdfa4cd
AM
414 struct Opd_ent
415 {
416 unsigned int shndx;
c6de8ed4
AM
417 bool discard : 1;
418 bool gc_mark : 1;
26a4e9cb 419 Address off;
bfdfa4cd
AM
420 };
421
422 // Return index into opd_ent_ array for .opd entry at OFF.
423 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
424 // apart when the language doesn't use the last 8-byte word, the
425 // environment pointer. Thus dividing the entry section offset by
426 // 16 will give an index into opd_ent_ that works for either layout
427 // of .opd. (It leaves some elements of the vector unused when .opd
428 // entries are spaced 24 bytes apart, but we don't know the spacing
429 // until relocations are processed, and in any case it is possible
430 // for an object to have some entries spaced 16 bytes apart and
431 // others 24 bytes apart.)
c9269dff
AM
432 size_t
433 opd_ent_ndx(size_t off) const
434 { return off >> 4;}
435
590b87ff
AM
436 // Per object unique identifier
437 uint32_t uniq_;
438
c9269dff
AM
439 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
440 unsigned int special_;
bfdfa4cd 441
5edad15d
AM
442 // For 64-bit the .rela.toc and .toc section shdnx.
443 unsigned int relatoc_;
444 unsigned int toc_;
445
d8f5a274
AM
446 // For 64-bit, whether this object uses small model relocs to access
447 // the toc.
448 bool has_small_toc_reloc_;
449
bfdfa4cd
AM
450 // Set at the start of gc_process_relocs, when we know opd_ent_
451 // vector is valid. The flag could be made atomic and set in
452 // do_read_relocs with memory_order_release and then tested with
453 // memory_order_acquire, potentially resulting in fewer entries in
454 // access_from_map_.
455 bool opd_valid_;
456
590b87ff
AM
457 // Header e_flags
458 elfcpp::Elf_Word e_flags_;
459
460 // For 64-bit, an array with one entry per 64-bit word in the .toc
461 // section, set if accesses using that word cannot be optimised.
462 std::vector<bool> no_toc_opt_;
463
c9269dff
AM
464 // The first 8-byte word of an OPD entry gives the address of the
465 // entry point of the function. Relocatable object files have a
bfdfa4cd 466 // relocation on this word. The following vector records the
c9269dff 467 // section and offset specified by these relocations.
bfdfa4cd
AM
468 std::vector<Opd_ent> opd_ent_;
469
e81fea4d 470 // References made to this object's .opd section when running
bfdfa4cd
AM
471 // gc_process_relocs for another object, before the opd_ent_ vector
472 // is valid for this object.
e81fea4d 473 Access_from access_from_map_;
ec661b9d
AM
474
475 // Whether input section has a 14-bit branch reloc.
476 std::vector<bool> has14_;
477
478 // The stub table to use for a given input section.
a3e60ddb 479 std::vector<unsigned int> stub_table_index_;
b4f7960d 480
b4f7960d
AM
481 // ELF st_other field for local symbols.
482 std::vector<unsigned char> st_other_;
724436fc
AM
483
484 // Object attributes if there is a .gnu.attributes section.
485 Attributes_section_data* attributes_section_data_;
cf43a2fe
AM
486};
487
dc3714f3
AM
488template<int size, bool big_endian>
489class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
490{
491public:
492 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
493
494 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
495 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
496 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
724436fc
AM
497 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
498 attributes_section_data_(NULL)
b4f7960d
AM
499 {
500 this->set_abiversion(0);
501 }
dc3714f3
AM
502
503 ~Powerpc_dynobj()
724436fc 504 { delete this->attributes_section_data_; }
dc3714f3
AM
505
506 // Call Sized_dynobj::do_read_symbols to read the symbols then
507 // read .opd from a dynamic object, filling in opd_ent_ vector,
508 void
509 do_read_symbols(Read_symbols_data*);
510
511 // The .opd section shndx.
512 unsigned int
513 opd_shndx() const
514 {
515 return this->opd_shndx_;
516 }
517
518 // The .opd section address.
519 Address
520 opd_address() const
521 {
522 return this->opd_address_;
523 }
524
525 // Init OPD entry arrays.
526 void
527 init_opd(size_t opd_size)
528 {
529 size_t count = this->opd_ent_ndx(opd_size);
530 this->opd_ent_.resize(count);
531 }
532
533 // Return section and offset of function entry for .opd + R_OFF.
534 unsigned int
535 get_opd_ent(Address r_off, Address* value = NULL) const
536 {
537 size_t ndx = this->opd_ent_ndx(r_off);
538 gold_assert(ndx < this->opd_ent_.size());
539 gold_assert(this->opd_ent_[ndx].shndx != 0);
540 if (value != NULL)
541 *value = this->opd_ent_[ndx].off;
542 return this->opd_ent_[ndx].shndx;
543 }
544
545 // Set section and offset of function entry for .opd + R_OFF.
546 void
547 set_opd_ent(Address r_off, unsigned int shndx, Address value)
548 {
549 size_t ndx = this->opd_ent_ndx(r_off);
550 gold_assert(ndx < this->opd_ent_.size());
551 this->opd_ent_[ndx].shndx = shndx;
552 this->opd_ent_[ndx].off = value;
553 }
554
b4f7960d
AM
555 int
556 abiversion() const
557 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
558
559 // Set ABI version for input and output.
560 void
561 set_abiversion(int ver);
562
724436fc
AM
563 // The contents of the .gnu.attributes section if there is one.
564 const Attributes_section_data*
565 attributes_section_data() const
566 { return this->attributes_section_data_; }
567
dc3714f3
AM
568private:
569 // Used to specify extent of executable sections.
570 struct Sec_info
571 {
572 Sec_info(Address start_, Address len_, unsigned int shndx_)
573 : start(start_), len(len_), shndx(shndx_)
574 { }
575
576 bool
577 operator<(const Sec_info& that) const
578 { return this->start < that.start; }
579
580 Address start;
581 Address len;
582 unsigned int shndx;
583 };
584
585 struct Opd_ent
586 {
587 unsigned int shndx;
588 Address off;
589 };
590
591 // Return index into opd_ent_ array for .opd entry at OFF.
592 size_t
593 opd_ent_ndx(size_t off) const
594 { return off >> 4;}
595
596 // For 64-bit the .opd section shndx and address.
597 unsigned int opd_shndx_;
598 Address opd_address_;
599
590b87ff
AM
600 // Header e_flags
601 elfcpp::Elf_Word e_flags_;
602
dc3714f3
AM
603 // The first 8-byte word of an OPD entry gives the address of the
604 // entry point of the function. Records the section and offset
605 // corresponding to the address. Note that in dynamic objects,
606 // offset is *not* relative to the section.
607 std::vector<Opd_ent> opd_ent_;
724436fc
AM
608
609 // Object attributes if there is a .gnu.attributes section.
610 Attributes_section_data* attributes_section_data_;
dc3714f3
AM
611};
612
5edad15d
AM
613// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
614// base class will emit.
615
616template<int sh_type, int size, bool big_endian>
617class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
618{
619 public:
620 Powerpc_copy_relocs()
621 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
622 { }
623
624 // Emit any saved relocations which turn out to be needed. This is
625 // called after all the relocs have been scanned.
626 void
627 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
628};
629
42cacb20
DE
630template<int size, bool big_endian>
631class Target_powerpc : public Sized_target<size, big_endian>
632{
633 public:
d83ce4e3
AM
634 typedef
635 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 636 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 637 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 638 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 639 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
640 // Offset of tp and dtp pointers from start of TLS block.
641 static const Address tp_offset = 0x7000;
642 static const Address dtp_offset = 0x8000;
42cacb20
DE
643
644 Target_powerpc()
645 : Sized_target<size, big_endian>(&powerpc_info),
2d7ad24e 646 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
5edad15d 647 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 648 tlsld_got_offset_(-1U),
7e57d19e 649 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
63e5eea2 650 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
7ee7ff70 651 plt_localentry0_init_(false), has_localentry0_(false),
4e0e019f 652 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
7ee7ff70 653 relax_failed_(false), relax_fail_count_(0),
34e0882b 654 stub_group_size_(0), savres_section_(0),
724436fc
AM
655 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
656 attributes_section_data_(NULL),
657 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
42cacb20
DE
658 {
659 }
660
2e702c99 661 // Process the relocations to determine unreferenced sections for
6d03d481
ST
662 // garbage collection.
663 void
ad0f2072 664 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
665 Layout* layout,
666 Sized_relobj_file<size, big_endian>* object,
667 unsigned int data_shndx,
668 unsigned int sh_type,
669 const unsigned char* prelocs,
670 size_t reloc_count,
671 Output_section* output_section,
672 bool needs_special_offset_handling,
673 size_t local_symbol_count,
674 const unsigned char* plocal_symbols);
6d03d481 675
42cacb20
DE
676 // Scan the relocations to look for symbol adjustments.
677 void
ad0f2072 678 scan_relocs(Symbol_table* symtab,
42cacb20 679 Layout* layout,
6fa2a40b 680 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
681 unsigned int data_shndx,
682 unsigned int sh_type,
683 const unsigned char* prelocs,
684 size_t reloc_count,
685 Output_section* output_section,
686 bool needs_special_offset_handling,
687 size_t local_symbol_count,
688 const unsigned char* plocal_symbols);
921b5322
AM
689
690 // Map input .toc section to output .got section.
691 const char*
692 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
693 {
694 if (size == 64 && strcmp(name, ".toc") == 0)
695 {
696 *plen = 4;
697 return ".got";
698 }
699 return NULL;
700 }
701
f3a0ed29
AM
702 // Provide linker defined save/restore functions.
703 void
704 define_save_restore_funcs(Layout*, Symbol_table*);
705
ec661b9d
AM
706 // No stubs unless a final link.
707 bool
708 do_may_relax() const
709 { return !parameters->options().relocatable(); }
710
711 bool
712 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
713
9d5781f8
AM
714 void
715 do_plt_fde_location(const Output_data*, unsigned char*,
716 uint64_t*, off_t*) const;
717
ec661b9d
AM
718 // Stash info about branches, for stub generation.
719 void
720 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
721 unsigned int data_shndx, Address r_offset,
722 unsigned int r_type, unsigned int r_sym, Address addend)
723 {
724 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
725 this->branch_info_.push_back(info);
726 if (r_type == elfcpp::R_POWERPC_REL14
727 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
728 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
729 ppc_object->set_has_14bit_branch(data_shndx);
730 }
731
7e57d19e
AM
732 // Return whether the last branch is a plt call, and if so, mark the
733 // branch as having an R_PPC64_TOCSAVE.
734 bool
735 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
736 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
737 {
738 return (size == 64
739 && !this->branch_info_.empty()
740 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
741 r_offset, this, symtab));
742 }
743
744 // Say the given location, that of a nop in a function prologue with
745 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
746 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
747 void
748 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
749 unsigned int shndx, Address offset)
750 {
751 Symbol_location loc;
752 loc.object = ppc_object;
753 loc.shndx = shndx;
754 loc.offset = offset;
755 this->tocsave_loc_.insert(loc);
756 }
757
758 // Accessor
b0d0d02b 759 const Tocsave_loc*
7e57d19e
AM
760 tocsave_loc() const
761 {
b0d0d02b 762 return &this->tocsave_loc_;
7e57d19e
AM
763 }
764
f43ba157
AM
765 void
766 do_define_standard_symbols(Symbol_table*, Layout*);
767
42cacb20
DE
768 // Finalize the sections.
769 void
f59f41f3 770 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
771
772 // Return the value to use for a dynamic which requires special
773 // treatment.
774 uint64_t
775 do_dynsym_value(const Symbol*) const;
776
c9824451
AM
777 // Return the PLT address to use for a local symbol.
778 uint64_t
779 do_plt_address_for_local(const Relobj*, unsigned int) const;
780
781 // Return the PLT address to use for a global symbol.
782 uint64_t
783 do_plt_address_for_global(const Symbol*) const;
784
bd73a62d
AM
785 // Return the offset to use for the GOT_INDX'th got entry which is
786 // for a local tls symbol specified by OBJECT, SYMNDX.
787 int64_t
788 do_tls_offset_for_local(const Relobj* object,
789 unsigned int symndx,
790 unsigned int got_indx) const;
791
792 // Return the offset to use for the GOT_INDX'th got entry which is
793 // for global tls symbol GSYM.
794 int64_t
795 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
796
dc3714f3
AM
797 void
798 do_function_location(Symbol_location*) const;
799
4d9aa155
AM
800 bool
801 do_can_check_for_function_pointers() const
802 { return true; }
803
bbec1a5d
AM
804 // Adjust -fsplit-stack code which calls non-split-stack code.
805 void
806 do_calls_non_split(Relobj* object, unsigned int shndx,
807 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 808 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
809 unsigned char* view, section_size_type view_size,
810 std::string* from, std::string* to) const;
811
42cacb20
DE
812 // Relocate a section.
813 void
814 relocate_section(const Relocate_info<size, big_endian>*,
815 unsigned int sh_type,
816 const unsigned char* prelocs,
817 size_t reloc_count,
818 Output_section* output_section,
819 bool needs_special_offset_handling,
820 unsigned char* view,
c9269dff 821 Address view_address,
364c7fa5
ILT
822 section_size_type view_size,
823 const Reloc_symbol_changes*);
42cacb20
DE
824
825 // Scan the relocs during a relocatable link.
826 void
ad0f2072 827 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 828 Layout* layout,
6fa2a40b 829 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
830 unsigned int data_shndx,
831 unsigned int sh_type,
832 const unsigned char* prelocs,
833 size_t reloc_count,
834 Output_section* output_section,
835 bool needs_special_offset_handling,
836 size_t local_symbol_count,
837 const unsigned char* plocal_symbols,
838 Relocatable_relocs*);
839
4d625b70
CC
840 // Scan the relocs for --emit-relocs.
841 void
842 emit_relocs_scan(Symbol_table* symtab,
843 Layout* layout,
844 Sized_relobj_file<size, big_endian>* object,
845 unsigned int data_shndx,
846 unsigned int sh_type,
847 const unsigned char* prelocs,
848 size_t reloc_count,
849 Output_section* output_section,
850 bool needs_special_offset_handling,
851 size_t local_symbol_count,
852 const unsigned char* plocal_syms,
853 Relocatable_relocs* rr);
854
7404fe1b 855 // Emit relocations for a section.
42cacb20 856 void
7404fe1b
AM
857 relocate_relocs(const Relocate_info<size, big_endian>*,
858 unsigned int sh_type,
859 const unsigned char* prelocs,
860 size_t reloc_count,
861 Output_section* output_section,
62fe925a
RM
862 typename elfcpp::Elf_types<size>::Elf_Off
863 offset_in_output_section,
7404fe1b
AM
864 unsigned char*,
865 Address view_address,
866 section_size_type,
867 unsigned char* reloc_view,
868 section_size_type reloc_view_size);
42cacb20
DE
869
870 // Return whether SYM is defined by the ABI.
871 bool
9c2d0ef9 872 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 873 {
cf43a2fe 874 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
875 }
876
877 // Return the size of the GOT section.
878 section_size_type
0e70b911 879 got_size() const
42cacb20
DE
880 {
881 gold_assert(this->got_ != NULL);
882 return this->got_->data_size();
883 }
884
cf43a2fe
AM
885 // Get the PLT section.
886 const Output_data_plt_powerpc<size, big_endian>*
887 plt_section() const
888 {
889 gold_assert(this->plt_ != NULL);
890 return this->plt_;
891 }
892
e5d5f5ed
AM
893 // Get the IPLT section.
894 const Output_data_plt_powerpc<size, big_endian>*
895 iplt_section() const
896 {
897 gold_assert(this->iplt_ != NULL);
898 return this->iplt_;
899 }
900
2d7ad24e
AM
901 // Get the LPLT section.
902 const Output_data_plt_powerpc<size, big_endian>*
903 lplt_section() const
904 {
905 return this->lplt_;
906 }
907
08be3224
AM
908 // Return the plt offset and section for the given global sym.
909 Address
910 plt_off(const Symbol* gsym,
911 const Output_data_plt_powerpc<size, big_endian>** sec) const
912 {
913 if (gsym->type() == elfcpp::STT_GNU_IFUNC
914 && gsym->can_use_relative_reloc(false))
915 *sec = this->iplt_section();
916 else
917 *sec = this->plt_section();
918 return gsym->plt_offset();
919 }
920
921 // Return the plt offset and section for the given local sym.
922 Address
923 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
924 unsigned int local_sym_index,
925 const Output_data_plt_powerpc<size, big_endian>** sec) const
926 {
2d7ad24e
AM
927 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
928 if (lsym->is_ifunc_symbol())
929 *sec = this->iplt_section();
930 else
931 *sec = this->lplt_section();
08be3224
AM
932 return relobj->local_plt_offset(local_sym_index);
933 }
934
cf43a2fe
AM
935 // Get the .glink section.
936 const Output_data_glink<size, big_endian>*
937 glink_section() const
938 {
939 gold_assert(this->glink_ != NULL);
940 return this->glink_;
941 }
942
9055360d
AM
943 Output_data_glink<size, big_endian>*
944 glink_section()
945 {
946 gold_assert(this->glink_ != NULL);
947 return this->glink_;
948 }
949
9d5781f8
AM
950 bool has_glink() const
951 { return this->glink_ != NULL; }
952
cf43a2fe
AM
953 // Get the GOT section.
954 const Output_data_got_powerpc<size, big_endian>*
955 got_section() const
956 {
957 gold_assert(this->got_ != NULL);
958 return this->got_;
959 }
960
26a4e9cb
AM
961 // Get the GOT section, creating it if necessary.
962 Output_data_got_powerpc<size, big_endian>*
963 got_section(Symbol_table*, Layout*);
964
cf43a2fe
AM
965 Object*
966 do_make_elf_object(const std::string&, Input_file*, off_t,
967 const elfcpp::Ehdr<size, big_endian>&);
968
0e70b911
CC
969 // Return the number of entries in the GOT.
970 unsigned int
971 got_entry_count() const
972 {
973 if (this->got_ == NULL)
974 return 0;
975 return this->got_size() / (size / 8);
976 }
977
978 // Return the number of entries in the PLT.
979 unsigned int
980 plt_entry_count() const;
981
982 // Return the offset of the first non-reserved PLT entry.
983 unsigned int
b4f7960d
AM
984 first_plt_entry_offset() const
985 {
986 if (size == 32)
987 return 0;
988 if (this->abiversion() >= 2)
989 return 16;
990 return 24;
991 }
0e70b911
CC
992
993 // Return the size of each PLT entry.
994 unsigned int
b4f7960d
AM
995 plt_entry_size() const
996 {
997 if (size == 32)
998 return 4;
999 if (this->abiversion() >= 2)
1000 return 8;
1001 return 24;
1002 }
0e70b911 1003
d49044c7
AM
1004 Output_data_save_res<size, big_endian>*
1005 savres_section() const
1006 {
1007 return this->savres_section_;
1008 }
1009
e81fea4d
AM
1010 // Add any special sections for this symbol to the gc work list.
1011 // For powerpc64, this adds the code section of a function
1012 // descriptor.
1013 void
1014 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1015
1016 // Handle target specific gc actions when adding a gc reference from
1017 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1018 // and DST_OFF. For powerpc64, this adds a referenc to the code
1019 // section of a function descriptor.
1020 void
1021 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 1022 Relobj* src_obj,
e81fea4d 1023 unsigned int src_shndx,
efc6fa12 1024 Relobj* dst_obj,
e81fea4d
AM
1025 unsigned int dst_shndx,
1026 Address dst_off) const;
1027
ec661b9d
AM
1028 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1029 const Stub_tables&
1030 stub_tables() const
1031 { return this->stub_tables_; }
1032
1033 const Output_data_brlt_powerpc<size, big_endian>*
1034 brlt_section() const
1035 { return this->brlt_section_; }
1036
1037 void
1038 add_branch_lookup_table(Address to)
1039 {
1040 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1041 this->branch_lookup_table_.insert(std::make_pair(to, off));
1042 }
1043
1044 Address
1045 find_branch_lookup_table(Address to)
1046 {
1047 typename Branch_lookup_table::const_iterator p
1048 = this->branch_lookup_table_.find(to);
1049 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1050 }
1051
1052 void
1053 write_branch_lookup_table(unsigned char *oview)
1054 {
1055 for (typename Branch_lookup_table::const_iterator p
1056 = this->branch_lookup_table_.begin();
1057 p != this->branch_lookup_table_.end();
1058 ++p)
1059 {
4d5effb9 1060 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
1061 }
1062 }
1063
590b87ff
AM
1064 // Wrapper used after relax to define a local symbol in output data,
1065 // from the end if value < 0.
1066 void
1067 define_local(Symbol_table* symtab, const char* name,
1068 Output_data* od, Address value, unsigned int symsize)
1069 {
1070 Symbol* sym
1071 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1072 od, value, symsize, elfcpp::STT_NOTYPE,
1073 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1074 static_cast<Signed_address>(value) < 0,
1075 false);
1076 // We are creating this symbol late, so need to fix up things
1077 // done early in Layout::finalize.
1078 sym->set_dynsym_index(-1U);
1079 }
1080
63e5eea2
AM
1081 void
1082 set_power10_relocs()
1083 {
1084 this->power10_relocs_ = true;
1085 }
1086
e4dff765 1087 bool
7c1f4227 1088 power10_stubs() const
e4dff765 1089 {
63e5eea2
AM
1090 return (this->power10_relocs_
1091 && (parameters->options().power10_stubs_enum()
1092 != General_options::POWER10_STUBS_NO));
afd2ea23
AM
1093 }
1094
1095 bool
1096 power10_stubs_auto() const
1097 {
1098 return (parameters->options().power10_stubs_enum()
1099 == General_options::POWER10_STUBS_AUTO);
e4dff765
AM
1100 }
1101
9e69ed50
AM
1102 bool
1103 plt_thread_safe() const
1104 { return this->plt_thread_safe_; }
1105
7ee7ff70
AM
1106 bool
1107 plt_localentry0() const
1108 { return this->plt_localentry0_; }
1109
63e5eea2
AM
1110 bool
1111 has_localentry0() const
1112 { return this->has_localentry0_; }
1113
7ee7ff70
AM
1114 void
1115 set_has_localentry0()
1116 {
1117 this->has_localentry0_ = true;
1118 }
1119
1120 bool
1121 is_elfv2_localentry0(const Symbol* gsym) const
1122 {
1123 return (size == 64
1124 && this->abiversion() >= 2
1125 && this->plt_localentry0()
1126 && gsym->type() == elfcpp::STT_FUNC
1127 && gsym->is_defined()
565ed01a
AM
1128 && gsym->nonvis() >> 3 == 0
1129 && !gsym->non_zero_localentry());
7ee7ff70
AM
1130 }
1131
1132 bool
1133 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1134 unsigned int r_sym) const
1135 {
1136 const Powerpc_relobj<size, big_endian>* ppc_object
1137 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1138
1139 if (size == 64
1140 && this->abiversion() >= 2
1141 && this->plt_localentry0()
1142 && ppc_object->st_other(r_sym) >> 5 == 0)
1143 {
1144 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1145 bool is_ordinary;
1146 if (!psymval->is_ifunc_symbol()
1147 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1148 && is_ordinary)
1149 return true;
1150 }
1151 return false;
1152 }
1153
252dcdf4
AM
1154 bool
1155 tprel_opt() const
4e0e019f 1156 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
252dcdf4
AM
1157
1158 void
4e0e019f
AM
1159 set_no_tprel_opt()
1160 { this->no_tprel_opt_ = true; }
252dcdf4 1161
565ed01a
AM
1162 // Remember any symbols seen with non-zero localentry, even those
1163 // not providing a definition
1164 bool
1165 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1166 const char*)
1167 {
1168 if (size == 64)
1169 {
1170 unsigned char st_other = sym.get_st_other();
1171 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1172 to->set_non_zero_localentry();
1173 }
1174 // We haven't resolved anything, continue normal processing.
1175 return false;
1176 }
1177
b4f7960d 1178 int
aacb3b6d 1179 abiversion() const
b4f7960d
AM
1180 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1181
1182 void
aacb3b6d 1183 set_abiversion(int ver)
b4f7960d
AM
1184 {
1185 elfcpp::Elf_Word flags = this->processor_specific_flags();
1186 flags &= ~elfcpp::EF_PPC64_ABI;
1187 flags |= ver & elfcpp::EF_PPC64_ABI;
1188 this->set_processor_specific_flags(flags);
1189 }
1190
34e0882b
AM
1191 Symbol*
1192 tls_get_addr_opt() const
1193 { return this->tls_get_addr_opt_; }
1194
1195 Symbol*
1196 tls_get_addr() const
1197 { return this->tls_get_addr_; }
1198
1199 // If optimizing __tls_get_addr calls, whether this is the
1200 // "__tls_get_addr" symbol.
1201 bool
1202 is_tls_get_addr_opt(const Symbol* gsym) const
1203 {
1204 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1205 || gsym == this->tls_get_addr_opt_);
1206 }
1207
1208 bool
1209 replace_tls_get_addr(const Symbol* gsym) const
1210 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1211
1212 void
1213 set_has_tls_get_addr_opt()
1214 { this->has_tls_get_addr_opt_ = true; }
1215
aacb3b6d 1216 // Offset to toc save stack slot
b4f7960d 1217 int
aacb3b6d 1218 stk_toc() const
b4f7960d
AM
1219 { return this->abiversion() < 2 ? 40 : 24; }
1220
34e0882b
AM
1221 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1222 // so use the CR save slot. Used only by __tls_get_addr call stub,
1223 // relying on __tls_get_addr not saving CR itself.
1224 int
1225 stk_linker() const
1226 { return this->abiversion() < 2 ? 32 : 8; }
1227
724436fc
AM
1228 // Merge object attributes from input object with those in the output.
1229 void
6f3fe02b 1230 merge_object_attributes(const Object*, const Attributes_section_data*);
724436fc 1231
42cacb20
DE
1232 private:
1233
e3deeb9c
AM
1234 class Track_tls
1235 {
1236 public:
1237 enum Tls_get_addr
1238 {
1239 NOT_EXPECTED = 0,
1240 EXPECTED = 1,
1241 SKIP = 2,
1242 NORMAL = 3
1243 };
1244
1245 Track_tls()
aacb3b6d 1246 : tls_get_addr_state_(NOT_EXPECTED),
e3deeb9c
AM
1247 relinfo_(NULL), relnum_(0), r_offset_(0)
1248 { }
1249
1250 ~Track_tls()
1251 {
aacb3b6d 1252 if (this->tls_get_addr_state_ != NOT_EXPECTED)
e3deeb9c
AM
1253 this->missing();
1254 }
1255
1256 void
1257 missing(void)
1258 {
1259 if (this->relinfo_ != NULL)
1260 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1261 _("missing expected __tls_get_addr call"));
1262 }
1263
1264 void
1265 expect_tls_get_addr_call(
1266 const Relocate_info<size, big_endian>* relinfo,
1267 size_t relnum,
1268 Address r_offset)
1269 {
aacb3b6d 1270 this->tls_get_addr_state_ = EXPECTED;
e3deeb9c
AM
1271 this->relinfo_ = relinfo;
1272 this->relnum_ = relnum;
1273 this->r_offset_ = r_offset;
1274 }
1275
1276 void
1277 expect_tls_get_addr_call()
aacb3b6d 1278 { this->tls_get_addr_state_ = EXPECTED; }
e3deeb9c
AM
1279
1280 void
1281 skip_next_tls_get_addr_call()
aacb3b6d 1282 {this->tls_get_addr_state_ = SKIP; }
e3deeb9c
AM
1283
1284 Tls_get_addr
34e0882b
AM
1285 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1286 unsigned int r_type, const Symbol* gsym)
e3deeb9c 1287 {
32f59844
AM
1288 bool is_tls_call
1289 = ((r_type == elfcpp::R_POWERPC_REL24
1290 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1291 || r_type == elfcpp::R_PPC_PLTREL24
1292 || is_plt16_reloc<size>(r_type)
e4dff765
AM
1293 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1294 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
32f59844
AM
1295 || r_type == elfcpp::R_POWERPC_PLTSEQ
1296 || r_type == elfcpp::R_POWERPC_PLTCALL
1297 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1298 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1299 && gsym != NULL
1300 && (gsym == target->tls_get_addr()
1301 || gsym == target->tls_get_addr_opt()));
aacb3b6d
AM
1302 Tls_get_addr last_tls = this->tls_get_addr_state_;
1303 this->tls_get_addr_state_ = NOT_EXPECTED;
e3deeb9c
AM
1304 if (is_tls_call && last_tls != EXPECTED)
1305 return last_tls;
1306 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1307 {
1308 this->missing();
1309 return EXPECTED;
1310 }
1311 return NORMAL;
1312 }
1313
1314 private:
1315 // What we're up to regarding calls to __tls_get_addr.
1316 // On powerpc, the branch and link insn making a call to
1317 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1318 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
32f59844 1319 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
e3deeb9c
AM
1320 // The marker relocation always comes first, and has the same
1321 // symbol as the reloc on the insn setting up the __tls_get_addr
1322 // argument. This ties the arg setup insn with the call insn,
1323 // allowing ld to safely optimize away the call. We check that
1324 // every call to __tls_get_addr has a marker relocation, and that
1325 // every marker relocation is on a call to __tls_get_addr.
aacb3b6d 1326 Tls_get_addr tls_get_addr_state_;
e3deeb9c
AM
1327 // Info about the last reloc for error message.
1328 const Relocate_info<size, big_endian>* relinfo_;
1329 size_t relnum_;
1330 Address r_offset_;
1331 };
1332
42cacb20 1333 // The class which scans relocations.
e3deeb9c 1334 class Scan : protected Track_tls
42cacb20
DE
1335 {
1336 public:
bfdfa4cd
AM
1337 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1338
42cacb20 1339 Scan()
e3deeb9c 1340 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1341 { }
1342
95a2c8d6 1343 static inline int
88b8e639 1344 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1345
42cacb20 1346 inline void
ad0f2072 1347 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1348 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1349 unsigned int data_shndx,
1350 Output_section* output_section,
1351 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1352 const elfcpp::Sym<size, big_endian>& lsym,
1353 bool is_discarded);
42cacb20
DE
1354
1355 inline void
ad0f2072 1356 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1357 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1358 unsigned int data_shndx,
1359 Output_section* output_section,
1360 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1361 Symbol* gsym);
1362
21bb3914
ST
1363 inline bool
1364 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1365 Target_powerpc* ,
f6971787 1366 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1367 unsigned int ,
2e702c99
RM
1368 Output_section* ,
1369 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1370 unsigned int r_type,
2e702c99 1371 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1372 {
1373 // PowerPC64 .opd is not folded, so any identical function text
1374 // may be folded and we'll still keep function addresses distinct.
1375 // That means no reloc is of concern here.
1376 if (size == 64)
f6971787
AM
1377 {
1378 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1379 <Powerpc_relobj<size, big_endian>*>(relobj);
1380 if (ppcobj->abiversion() == 1)
1381 return false;
1382 }
1383 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155 1384 // function code might be taking the address of the function.
32f59844 1385 return !is_branch_reloc<size>(r_type);
4d9aa155 1386 }
21bb3914
ST
1387
1388 inline bool
1389 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1390 Target_powerpc* ,
f6971787 1391 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1392 unsigned int ,
1393 Output_section* ,
4d9aa155
AM
1394 const elfcpp::Rela<size, big_endian>& ,
1395 unsigned int r_type,
1396 Symbol*)
1397 {
1398 // As above.
1399 if (size == 64)
f6971787
AM
1400 {
1401 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1402 <Powerpc_relobj<size, big_endian>*>(relobj);
1403 if (ppcobj->abiversion() == 1)
1404 return false;
1405 }
32f59844 1406 return !is_branch_reloc<size>(r_type);
4d9aa155 1407 }
21bb3914 1408
b3ccdeb5 1409 static bool
9055360d
AM
1410 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1411 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1412 unsigned int r_type, bool report_err);
1413
42cacb20
DE
1414 private:
1415 static void
6fa2a40b 1416 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1417 unsigned int r_type);
1418
1419 static void
6fa2a40b 1420 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1421 unsigned int r_type, Symbol*);
1422
1423 static void
1424 generate_tls_call(Symbol_table* symtab, Layout* layout,
1425 Target_powerpc* target);
1426
1427 void
1428 check_non_pic(Relobj*, unsigned int r_type);
1429
1430 // Whether we have issued an error about a non-PIC compilation.
1431 bool issued_non_pic_error_;
1432 };
1433
1611bc4a
AM
1434 bool
1435 symval_for_branch(const Symbol_table* symtab,
6c77229c 1436 const Sized_symbol<size>* gsym,
3ea0a085 1437 Powerpc_relobj<size, big_endian>* object,
1611bc4a 1438 Address *value, unsigned int *dest_shndx);
3ea0a085 1439
42cacb20 1440 // The class which implements relocation.
e3deeb9c 1441 class Relocate : protected Track_tls
42cacb20
DE
1442 {
1443 public:
dd93cd0a
AM
1444 // Use 'at' branch hints when true, 'y' when false.
1445 // FIXME maybe: set this with an option.
1446 static const bool is_isa_v2 = true;
1447
dd93cd0a 1448 Relocate()
e3deeb9c 1449 : Track_tls()
dd93cd0a
AM
1450 { }
1451
42cacb20
DE
1452 // Do a relocation. Return false if the caller should not issue
1453 // any warnings about this relocation.
1454 inline bool
91a65d2f
AM
1455 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1456 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1457 const Sized_symbol<size>*, const Symbol_value<size>*,
1458 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1459 section_size_type);
42cacb20
DE
1460 };
1461
168a4726
AM
1462 class Relocate_comdat_behavior
1463 {
1464 public:
1465 // Decide what the linker should do for relocations that refer to
1466 // discarded comdat sections.
1467 inline Comdat_behavior
1468 get(const char* name)
1469 {
1470 gold::Default_comdat_behavior default_behavior;
1471 Comdat_behavior ret = default_behavior.get(name);
43193fe9 1472 if (ret == CB_ERROR)
168a4726
AM
1473 {
1474 if (size == 32
1475 && (strcmp(name, ".fixup") == 0
1476 || strcmp(name, ".got2") == 0))
1477 ret = CB_IGNORE;
1478 if (size == 64
1479 && (strcmp(name, ".opd") == 0
1480 || strcmp(name, ".toc") == 0
1481 || strcmp(name, ".toc1") == 0))
1482 ret = CB_IGNORE;
1483 }
1484 return ret;
1485 }
1486 };
1487
dd93cd0a
AM
1488 // Optimize the TLS relocation type based on what we know about the
1489 // symbol. IS_FINAL is true if the final address of this symbol is
1490 // known at link time.
1491
1492 tls::Tls_optimization
1493 optimize_tls_gd(bool is_final)
1494 {
1495 // If we are generating a shared library, then we can't do anything
1496 // in the linker.
aacb3b6d
AM
1497 if (parameters->options().shared()
1498 || !parameters->options().tls_optimize())
dd93cd0a
AM
1499 return tls::TLSOPT_NONE;
1500
1501 if (!is_final)
1502 return tls::TLSOPT_TO_IE;
1503 return tls::TLSOPT_TO_LE;
1504 }
1505
1506 tls::Tls_optimization
1507 optimize_tls_ld()
1508 {
aacb3b6d
AM
1509 if (parameters->options().shared()
1510 || !parameters->options().tls_optimize())
dd93cd0a
AM
1511 return tls::TLSOPT_NONE;
1512
1513 return tls::TLSOPT_TO_LE;
1514 }
1515
1516 tls::Tls_optimization
1517 optimize_tls_ie(bool is_final)
1518 {
aacb3b6d
AM
1519 if (!is_final
1520 || parameters->options().shared()
1521 || !parameters->options().tls_optimize())
dd93cd0a
AM
1522 return tls::TLSOPT_NONE;
1523
1524 return tls::TLSOPT_TO_LE;
1525 }
cf43a2fe 1526
cf43a2fe
AM
1527 // Create glink.
1528 void
1529 make_glink_section(Layout*);
42cacb20 1530
cf43a2fe
AM
1531 // Create the PLT section.
1532 void
40b469d7 1533 make_plt_section(Symbol_table*, Layout*);
42cacb20 1534
e5d5f5ed 1535 void
40b469d7 1536 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1537
2d7ad24e
AM
1538 void
1539 make_lplt_section(Layout*);
1540
ec661b9d
AM
1541 void
1542 make_brlt_section(Layout*);
1543
42cacb20
DE
1544 // Create a PLT entry for a global symbol.
1545 void
ec661b9d 1546 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1547
1548 // Create a PLT entry for a local IFUNC symbol.
1549 void
40b469d7 1550 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1551 Sized_relobj_file<size, big_endian>*,
1552 unsigned int);
1553
2d7ad24e
AM
1554 // Create a PLT entry for a local non-IFUNC symbol.
1555 void
1556 make_local_plt_entry(Layout*,
1557 Sized_relobj_file<size, big_endian>*,
1558 unsigned int);
1559
42cacb20 1560
dd93cd0a
AM
1561 // Create a GOT entry for local dynamic __tls_get_addr.
1562 unsigned int
1563 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1564 Sized_relobj_file<size, big_endian>* object);
1565
42cacb20 1566 unsigned int
dd93cd0a
AM
1567 tlsld_got_offset() const
1568 {
1569 return this->tlsld_got_offset_;
1570 }
42cacb20 1571
42cacb20
DE
1572 // Get the dynamic reloc section, creating it if necessary.
1573 Reloc_section*
1574 rela_dyn_section(Layout*);
1575
b3ccdeb5
AM
1576 // Similarly, but for ifunc symbols get the one for ifunc.
1577 Reloc_section*
1578 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1579
42cacb20
DE
1580 // Copy a relocation against a global symbol.
1581 void
ef9beddf 1582 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1583 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1584 unsigned int shndx, Output_section* output_section,
1585 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1586 {
859d7987 1587 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1588 this->copy_relocs_.copy_reloc(symtab, layout,
1589 symtab->get_sized_symbol<size>(sym),
1590 object, shndx, output_section,
859d7987
CC
1591 r_type, reloc.get_r_offset(),
1592 reloc.get_r_addend(),
1593 this->rela_dyn_section(layout));
42cacb20
DE
1594 }
1595
0cfdc767 1596 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1597 void
a3e60ddb 1598 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1599
1600 // Sort output sections by address.
1601 struct Sort_sections
1602 {
1603 bool
1604 operator()(const Output_section* sec1, const Output_section* sec2)
1605 { return sec1->address() < sec2->address(); }
1606 };
1607
1608 class Branch_info
1609 {
1610 public:
1611 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1612 unsigned int data_shndx,
1613 Address r_offset,
1614 unsigned int r_type,
1615 unsigned int r_sym,
1616 Address addend)
1617 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1618 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1619 { }
1620
1621 ~Branch_info()
1622 { }
1623
7e57d19e
AM
1624 // Return whether this branch is going via a plt call stub, and if
1625 // so, mark it as having an R_PPC64_TOCSAVE.
1626 bool
1627 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1628 unsigned int shndx, Address offset,
1629 Target_powerpc* target, Symbol_table* symtab);
1630
ec661b9d 1631 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1632 bool
ec661b9d
AM
1633 make_stub(Stub_table<size, big_endian>*,
1634 Stub_table<size, big_endian>*,
1635 Symbol_table*) const;
1636
1637 private:
1638 // The branch location..
1639 Powerpc_relobj<size, big_endian>* object_;
1640 unsigned int shndx_;
1641 Address offset_;
1642 // ..and the branch type and destination.
7e57d19e
AM
1643 unsigned int r_type_ : 31;
1644 unsigned int tocsave_ : 1;
ec661b9d
AM
1645 unsigned int r_sym_;
1646 Address addend_;
1647 };
1648
42cacb20
DE
1649 // Information about this specific target which we pass to the
1650 // general Target structure.
1651 static Target::Target_info powerpc_info;
1652
1653 // The types of GOT entries needed for this platform.
0e70b911
CC
1654 // These values are exposed to the ABI in an incremental link.
1655 // Do not renumber existing values without changing the version
1656 // number of the .gnu_incremental_inputs section.
42cacb20
DE
1657 enum Got_type
1658 {
dd93cd0a
AM
1659 GOT_TYPE_STANDARD,
1660 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1661 GOT_TYPE_DTPREL, // entry for @got@dtprel
1662 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
1663 };
1664
ec661b9d 1665 // The GOT section.
cf43a2fe 1666 Output_data_got_powerpc<size, big_endian>* got_;
b3ccdeb5
AM
1667 // The PLT section. This is a container for a table of addresses,
1668 // and their relocations. Each address in the PLT has a dynamic
1669 // relocation (R_*_JMP_SLOT) and each address will have a
1670 // corresponding entry in .glink for lazy resolution of the PLT.
1671 // ppc32 initialises the PLT to point at the .glink entry, while
1672 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1673 // linker adds a stub that loads the PLT entry into ctr then
1674 // branches to ctr. There may be more than one stub for each PLT
1675 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1676 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1677 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1678 // The IPLT section. Like plt_, this is a container for a table of
1679 // addresses and their relocations, specifically for STT_GNU_IFUNC
1680 // functions that resolve locally (STT_GNU_IFUNC functions that
1681 // don't resolve locally go in PLT). Unlike plt_, these have no
1682 // entry in .glink for lazy resolution, and the relocation section
1683 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1684 // the relocation section may contain relocations against
1685 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1686 // relocation section will appear at the end of other dynamic
1687 // relocations, so that ld.so applies these relocations after other
1688 // dynamic relocations. In a static executable, the relocation
1689 // section is emitted and marked with __rela_iplt_start and
1690 // __rela_iplt_end symbols.
e5d5f5ed 1691 Output_data_plt_powerpc<size, big_endian>* iplt_;
2d7ad24e
AM
1692 // A PLT style section for local, non-ifunc symbols
1693 Output_data_plt_powerpc<size, big_endian>* lplt_;
ec661b9d
AM
1694 // Section holding long branch destinations.
1695 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1696 // The .glink section.
cf43a2fe 1697 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1698 // The dynamic reloc section.
42cacb20
DE
1699 Reloc_section* rela_dyn_;
1700 // Relocs saved to avoid a COPY reloc.
5edad15d 1701 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1702 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1703 unsigned int tlsld_got_offset_;
ec661b9d
AM
1704
1705 Stub_tables stub_tables_;
1706 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1707 Branch_lookup_table branch_lookup_table_;
1708
1709 typedef std::vector<Branch_info> Branches;
1710 Branches branch_info_;
7e57d19e 1711 Tocsave_loc tocsave_loc_;
9e69ed50 1712
63e5eea2 1713 bool power10_relocs_;
9e69ed50 1714 bool plt_thread_safe_;
7ee7ff70
AM
1715 bool plt_localentry0_;
1716 bool plt_localentry0_init_;
1717 bool has_localentry0_;
34e0882b 1718 bool has_tls_get_addr_opt_;
4e0e019f 1719 bool no_tprel_opt_;
a3e60ddb
AM
1720
1721 bool relax_failed_;
1722 int relax_fail_count_;
1723 int32_t stub_group_size_;
d49044c7
AM
1724
1725 Output_data_save_res<size, big_endian> *savres_section_;
34e0882b
AM
1726
1727 // The "__tls_get_addr" symbol, if present
1728 Symbol* tls_get_addr_;
1729 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1730 Symbol* tls_get_addr_opt_;
724436fc
AM
1731
1732 // Attributes in output.
1733 Attributes_section_data* attributes_section_data_;
1734
1735 // Last input file to change various attribute tags
1736 const char* last_fp_;
1737 const char* last_ld_;
1738 const char* last_vec_;
1739 const char* last_struct_;
42cacb20
DE
1740};
1741
1742template<>
1743Target::Target_info Target_powerpc<32, true>::powerpc_info =
1744{
1745 32, // size
1746 true, // is_big_endian
1747 elfcpp::EM_PPC, // machine_code
1748 false, // has_make_symbol
1749 false, // has_resolve
1750 false, // has_code_fill
1751 true, // is_default_stack_executable
b3ce541e 1752 false, // can_icf_inline_merge_sections
42cacb20
DE
1753 '\0', // wrap_char
1754 "/usr/lib/ld.so.1", // dynamic_linker
1755 0x10000000, // default_text_segment_address
1756 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1757 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1758 false, // isolate_execinstr
1759 0, // rosegment_gap
8a5e3e08
ILT
1760 elfcpp::SHN_UNDEF, // small_common_shndx
1761 elfcpp::SHN_UNDEF, // large_common_shndx
1762 0, // small_common_section_flags
05a352e6
DK
1763 0, // large_common_section_flags
1764 NULL, // attributes_section
a67858e0 1765 NULL, // attributes_vendor
8d9743bd
MK
1766 "_start", // entry_symbol_name
1767 32, // hash_entry_size
bce5a025 1768 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1769};
1770
1771template<>
1772Target::Target_info Target_powerpc<32, false>::powerpc_info =
1773{
1774 32, // size
1775 false, // is_big_endian
1776 elfcpp::EM_PPC, // machine_code
1777 false, // has_make_symbol
1778 false, // has_resolve
1779 false, // has_code_fill
1780 true, // is_default_stack_executable
b3ce541e 1781 false, // can_icf_inline_merge_sections
42cacb20
DE
1782 '\0', // wrap_char
1783 "/usr/lib/ld.so.1", // dynamic_linker
1784 0x10000000, // default_text_segment_address
1785 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1786 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1787 false, // isolate_execinstr
1788 0, // rosegment_gap
8a5e3e08
ILT
1789 elfcpp::SHN_UNDEF, // small_common_shndx
1790 elfcpp::SHN_UNDEF, // large_common_shndx
1791 0, // small_common_section_flags
05a352e6
DK
1792 0, // large_common_section_flags
1793 NULL, // attributes_section
a67858e0 1794 NULL, // attributes_vendor
8d9743bd
MK
1795 "_start", // entry_symbol_name
1796 32, // hash_entry_size
bce5a025 1797 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1798};
1799
1800template<>
1801Target::Target_info Target_powerpc<64, true>::powerpc_info =
1802{
1803 64, // size
1804 true, // is_big_endian
1805 elfcpp::EM_PPC64, // machine_code
1806 false, // has_make_symbol
565ed01a 1807 true, // has_resolve
42cacb20 1808 false, // has_code_fill
ec769010 1809 false, // is_default_stack_executable
b3ce541e 1810 false, // can_icf_inline_merge_sections
42cacb20
DE
1811 '\0', // wrap_char
1812 "/usr/lib/ld.so.1", // dynamic_linker
1813 0x10000000, // default_text_segment_address
1814 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1815 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1816 false, // isolate_execinstr
1817 0, // rosegment_gap
8a5e3e08
ILT
1818 elfcpp::SHN_UNDEF, // small_common_shndx
1819 elfcpp::SHN_UNDEF, // large_common_shndx
1820 0, // small_common_section_flags
05a352e6
DK
1821 0, // large_common_section_flags
1822 NULL, // attributes_section
a67858e0 1823 NULL, // attributes_vendor
8d9743bd
MK
1824 "_start", // entry_symbol_name
1825 32, // hash_entry_size
bce5a025 1826 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1827};
1828
1829template<>
1830Target::Target_info Target_powerpc<64, false>::powerpc_info =
1831{
1832 64, // size
1833 false, // is_big_endian
1834 elfcpp::EM_PPC64, // machine_code
1835 false, // has_make_symbol
565ed01a 1836 true, // has_resolve
42cacb20 1837 false, // has_code_fill
ec769010 1838 false, // is_default_stack_executable
b3ce541e 1839 false, // can_icf_inline_merge_sections
42cacb20
DE
1840 '\0', // wrap_char
1841 "/usr/lib/ld.so.1", // dynamic_linker
1842 0x10000000, // default_text_segment_address
1843 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1844 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1845 false, // isolate_execinstr
1846 0, // rosegment_gap
8a5e3e08
ILT
1847 elfcpp::SHN_UNDEF, // small_common_shndx
1848 elfcpp::SHN_UNDEF, // large_common_shndx
1849 0, // small_common_section_flags
05a352e6
DK
1850 0, // large_common_section_flags
1851 NULL, // attributes_section
a67858e0 1852 NULL, // attributes_vendor
8d9743bd
MK
1853 "_start", // entry_symbol_name
1854 32, // hash_entry_size
bce5a025 1855 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1856};
1857
32f59844 1858template<int size>
dd93cd0a
AM
1859inline bool
1860is_branch_reloc(unsigned int r_type)
1861{
1862 return (r_type == elfcpp::R_POWERPC_REL24
32f59844 1863 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
dd93cd0a
AM
1864 || r_type == elfcpp::R_PPC_PLTREL24
1865 || r_type == elfcpp::R_PPC_LOCAL24PC
1866 || r_type == elfcpp::R_POWERPC_REL14
1867 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1868 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1869 || r_type == elfcpp::R_POWERPC_ADDR24
1870 || r_type == elfcpp::R_POWERPC_ADDR14
1871 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1872 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1873}
1874
08be3224
AM
1875// Reloc resolves to plt entry.
1876template<int size>
1877inline bool
1878is_plt16_reloc(unsigned int r_type)
1879{
1880 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1881 || r_type == elfcpp::R_POWERPC_PLT16_HI
1882 || r_type == elfcpp::R_POWERPC_PLT16_HA
1883 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1884}
1885
4290b0ab
AM
1886// GOT_TYPE_STANDARD (ie. not TLS) GOT relocs
1887inline bool
1888is_got_reloc(unsigned int r_type)
1889{
1890 return (r_type == elfcpp::R_POWERPC_GOT16
1891 || r_type == elfcpp::R_POWERPC_GOT16_LO
1892 || r_type == elfcpp::R_POWERPC_GOT16_HI
1893 || r_type == elfcpp::R_POWERPC_GOT16_HA
1894 || r_type == elfcpp::R_PPC64_GOT16_DS
1895 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1896 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1897}
1898
dd93cd0a
AM
1899// If INSN is an opcode that may be used with an @tls operand, return
1900// the transformed insn for TLS optimisation, otherwise return 0. If
1901// REG is non-zero only match an insn with RB or RA equal to REG.
1902uint32_t
1903at_tls_transform(uint32_t insn, unsigned int reg)
1904{
1905 if ((insn & (0x3f << 26)) != 31 << 26)
1906 return 0;
1907
1908 unsigned int rtra;
1909 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1910 rtra = insn & ((1 << 26) - (1 << 16));
1911 else if (((insn >> 16) & 0x1f) == reg)
1912 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1913 else
1914 return 0;
1915
1916 if ((insn & (0x3ff << 1)) == 266 << 1)
1917 // add -> addi
1918 insn = 14 << 26;
1919 else if ((insn & (0x1f << 1)) == 23 << 1
1920 && ((insn & (0x1f << 6)) < 14 << 6
1921 || ((insn & (0x1f << 6)) >= 16 << 6
1922 && (insn & (0x1f << 6)) < 24 << 6)))
1923 // load and store indexed -> dform
1924 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1925 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1926 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1927 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1928 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1929 // lwax -> lwa
1930 insn = (58 << 26) | 2;
1931 else
1932 return 0;
1933 insn |= rtra;
1934 return insn;
1935}
1936
dd93cd0a 1937
42cacb20
DE
1938template<int size, bool big_endian>
1939class Powerpc_relocate_functions
1940{
dd93cd0a 1941public:
f4baf0d4 1942 enum Overflow_check
dd93cd0a 1943 {
f4baf0d4
AM
1944 CHECK_NONE,
1945 CHECK_SIGNED,
b80eed39
AM
1946 CHECK_UNSIGNED,
1947 CHECK_BITFIELD,
1948 CHECK_LOW_INSN,
1949 CHECK_HIGH_INSN
dd93cd0a
AM
1950 };
1951
f4baf0d4 1952 enum Status
dd93cd0a 1953 {
f4baf0d4
AM
1954 STATUS_OK,
1955 STATUS_OVERFLOW
1956 };
dd93cd0a 1957
42cacb20 1958private:
c9269dff 1959 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1960 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1961 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1962
dd93cd0a
AM
1963 template<int valsize>
1964 static inline bool
1965 has_overflow_signed(Address value)
1966 {
1967 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1968 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1969 limit <<= ((valsize - 1) >> 1);
1970 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1971 return value + limit > (limit << 1) - 1;
1972 }
1973
1974 template<int valsize>
1975 static inline bool
b80eed39 1976 has_overflow_unsigned(Address value)
dd93cd0a
AM
1977 {
1978 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1979 limit <<= ((valsize - 1) >> 1);
1980 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
1981 return value > (limit << 1) - 1;
1982 }
1983
1984 template<int valsize>
1985 static inline bool
1986 has_overflow_bitfield(Address value)
1987 {
1988 return (has_overflow_unsigned<valsize>(value)
1989 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
1990 }
1991
1992 template<int valsize>
f4baf0d4
AM
1993 static inline Status
1994 overflowed(Address value, Overflow_check overflow)
dd93cd0a 1995 {
f4baf0d4 1996 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
1997 {
1998 if (has_overflow_signed<valsize>(value))
f4baf0d4 1999 return STATUS_OVERFLOW;
dd93cd0a 2000 }
b80eed39
AM
2001 else if (overflow == CHECK_UNSIGNED)
2002 {
2003 if (has_overflow_unsigned<valsize>(value))
2004 return STATUS_OVERFLOW;
2005 }
f4baf0d4 2006 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
2007 {
2008 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 2009 return STATUS_OVERFLOW;
dd93cd0a 2010 }
f4baf0d4 2011 return STATUS_OK;
dd93cd0a
AM
2012 }
2013
cf43a2fe 2014 // Do a simple RELA relocation
0cfb0717 2015 template<int fieldsize, int valsize>
f4baf0d4
AM
2016 static inline Status
2017 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2018 {
0cfb0717 2019 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 2020 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2021 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
2022 return overflowed<valsize>(value, overflow);
2023 }
2024
0cfb0717 2025 template<int fieldsize, int valsize>
f4baf0d4 2026 static inline Status
42cacb20
DE
2027 rela(unsigned char* view,
2028 unsigned int right_shift,
0cfb0717 2029 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2030 Address value,
f4baf0d4 2031 Overflow_check overflow)
42cacb20 2032 {
0cfb0717 2033 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 2034 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2035 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
6a010cf6
AM
2036 if (overflow == CHECK_SIGNED)
2037 value = static_cast<SignedAddress>(value) >> right_shift;
2038 else
2039 value = value >> right_shift;
2040 Valtype reloc = value;
42cacb20
DE
2041 val &= ~dst_mask;
2042 reloc &= dst_mask;
0cfb0717 2043 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
6a010cf6 2044 return overflowed<valsize>(value, overflow);
42cacb20
DE
2045 }
2046
cf43a2fe 2047 // Do a simple RELA relocation, unaligned.
0cfb0717 2048 template<int fieldsize, int valsize>
f4baf0d4
AM
2049 static inline Status
2050 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2051 {
0cfb0717 2052 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
2053 return overflowed<valsize>(value, overflow);
2054 }
2055
0cfb0717 2056 template<int fieldsize, int valsize>
f4baf0d4 2057 static inline Status
cf43a2fe
AM
2058 rela_ua(unsigned char* view,
2059 unsigned int right_shift,
0cfb0717 2060 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2061 Address value,
f4baf0d4 2062 Overflow_check overflow)
42cacb20 2063 {
0cfb0717 2064 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 2065 Valtype;
0cfb0717 2066 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
6a010cf6
AM
2067 if (overflow == CHECK_SIGNED)
2068 value = static_cast<SignedAddress>(value) >> right_shift;
2069 else
2070 value = value >> right_shift;
2071 Valtype reloc = value;
42cacb20
DE
2072 val &= ~dst_mask;
2073 reloc &= dst_mask;
0cfb0717 2074 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
6a010cf6 2075 return overflowed<valsize>(value, overflow);
42cacb20
DE
2076 }
2077
42cacb20 2078public:
dd93cd0a 2079 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 2080 static inline void
dd93cd0a 2081 addr64(unsigned char* view, Address value)
0cfb0717 2082 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 2083
dd93cd0a 2084 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 2085 static inline void
dd93cd0a 2086 addr64_u(unsigned char* view, Address value)
0cfb0717 2087 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
2088
2089 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
2090 static inline Status
2091 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2092 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
2093
2094 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
2095 static inline Status
2096 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2097 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
2098
2099 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
2100 static inline Status
2101 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2102 {
0cfb0717
AM
2103 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2104 value, overflow);
f4baf0d4
AM
2105 if (overflow != CHECK_NONE && (value & 3) != 0)
2106 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2107 return stat;
2108 }
42cacb20
DE
2109
2110 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
2111 static inline Status
2112 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2113 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 2114
dd93cd0a 2115 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
2116 static inline Status
2117 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2118 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 2119
dd93cd0a 2120 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2121 static inline Status
2122 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2123 {
0cfb0717 2124 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 2125 if ((value & 3) != 0)
f4baf0d4 2126 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2127 return stat;
2128 }
42cacb20 2129
a680de9a
PB
2130 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2131 static inline Status
2132 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2133 {
2134 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2135 if ((value & 15) != 0)
2136 stat = STATUS_OVERFLOW;
2137 return stat;
2138 }
2139
42cacb20
DE
2140 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2141 static inline void
dd93cd0a 2142 addr16_hi(unsigned char* view, Address value)
0cfb0717 2143 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 2144
c9269dff 2145 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 2146 static inline void
dd93cd0a
AM
2147 addr16_ha(unsigned char* view, Address value)
2148 { This::addr16_hi(view, value + 0x8000); }
42cacb20 2149
dd93cd0a 2150 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 2151 static inline void
dd93cd0a 2152 addr16_hi2(unsigned char* view, Address value)
0cfb0717 2153 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 2154
dd93cd0a 2155 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 2156 static inline void
dd93cd0a
AM
2157 addr16_ha2(unsigned char* view, Address value)
2158 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 2159
dd93cd0a 2160 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 2161 static inline void
dd93cd0a 2162 addr16_hi3(unsigned char* view, Address value)
0cfb0717 2163 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 2164
dd93cd0a 2165 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 2166 static inline void
dd93cd0a
AM
2167 addr16_ha3(unsigned char* view, Address value)
2168 { This::addr16_hi3(view, value + 0x8000); }
2169
2170 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2171 static inline Status
2172 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2173 {
0cfb0717 2174 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
2175 if (overflow != CHECK_NONE && (value & 3) != 0)
2176 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2177 return stat;
2178 }
a680de9a
PB
2179
2180 // R_POWERPC_REL16DX_HA
2181 static inline Status
2182 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2183 {
2184 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2185 Valtype* wv = reinterpret_cast<Valtype*>(view);
2186 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2187 value += 0x8000;
2188 value = static_cast<SignedAddress>(value) >> 16;
2189 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2190 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2191 return overflowed<16>(value, overflow);
2192 }
e4dff765
AM
2193
2194 // R_PPC64_D34
2195 static inline Status
2196 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2197 {
2198 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2199 value, overflow);
2200 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2201 return stat;
2202 }
2203
2204 // R_PPC64_D34_HI30
2205 static inline void
2206 addr34_hi(unsigned char *view, uint64_t value)
2207 { This::addr34(view, value >> 34, CHECK_NONE);}
2208
2209 // R_PPC64_D34_HA30
2210 static inline void
2211 addr34_ha(unsigned char *view, uint64_t value)
2212 { This::addr34_hi(view, value + (1ULL << 33));}
2213
2214 // R_PPC64_D28
2215 static inline Status
2216 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2217 {
2218 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2219 value, overflow);
2220 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2221 return stat;
2222 }
2223
2224 // R_PPC64_ADDR16_HIGHER34
2225 static inline void
2226 addr16_higher34(unsigned char* view, uint64_t value)
2227 { This::addr16(view, value >> 34, CHECK_NONE); }
2228
2229 // R_PPC64_ADDR16_HIGHERA34
2230 static inline void
2231 addr16_highera34(unsigned char* view, uint64_t value)
2232 { This::addr16_higher34(view, value + (1ULL << 33)); }
2233
2234 // R_PPC64_ADDR16_HIGHEST34
2235 static inline void
2236 addr16_highest34(unsigned char* view, uint64_t value)
2237 { This::addr16(view, value >> 50, CHECK_NONE); }
2238
2239 // R_PPC64_ADDR16_HIGHESTA34
2240 static inline void
2241 addr16_highesta34(unsigned char* view, uint64_t value)
2242 { This::addr16_highest34(view, value + (1ULL << 33)); }
cf43a2fe
AM
2243};
2244
b4f7960d
AM
2245// Set ABI version for input and output.
2246
2247template<int size, bool big_endian>
2248void
2249Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2250{
2251 this->e_flags_ |= ver;
2252 if (this->abiversion() != 0)
2253 {
2254 Target_powerpc<size, big_endian>* target =
2255 static_cast<Target_powerpc<size, big_endian>*>(
2256 parameters->sized_target<size, big_endian>());
2257 if (target->abiversion() == 0)
2258 target->set_abiversion(this->abiversion());
2259 else if (target->abiversion() != this->abiversion())
2260 gold_error(_("%s: ABI version %d is not compatible "
2261 "with ABI version %d output"),
2262 this->name().c_str(),
2263 this->abiversion(), target->abiversion());
2264
2265 }
2266}
2267
5edad15d
AM
2268// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2269// relocatable object, if such sections exists.
cf43a2fe
AM
2270
2271template<int size, bool big_endian>
2272bool
2273Powerpc_relobj<size, big_endian>::do_find_special_sections(
2274 Read_symbols_data* sd)
2275{
c9269dff
AM
2276 const unsigned char* const pshdrs = sd->section_headers->data();
2277 const unsigned char* namesu = sd->section_names->data();
2278 const char* names = reinterpret_cast<const char*>(namesu);
2279 section_size_type names_size = sd->section_names_size;
2280 const unsigned char* s;
2281
dc3714f3
AM
2282 s = this->template find_shdr<size, big_endian>(pshdrs,
2283 size == 32 ? ".got2" : ".opd",
2284 names, names_size, NULL);
c9269dff
AM
2285 if (s != NULL)
2286 {
2287 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2288 this->special_ = ndx;
b4f7960d
AM
2289 if (size == 64)
2290 {
2291 if (this->abiversion() == 0)
2292 this->set_abiversion(1);
2293 else if (this->abiversion() > 1)
2294 gold_error(_("%s: .opd invalid in abiv%d"),
2295 this->name().c_str(), this->abiversion());
2296 }
c9269dff 2297 }
5edad15d
AM
2298 if (size == 64)
2299 {
2300 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2301 names, names_size, NULL);
2302 if (s != NULL)
2303 {
2304 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2305 this->relatoc_ = ndx;
2306 typename elfcpp::Shdr<size, big_endian> shdr(s);
2307 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2308 }
2309 }
c9269dff
AM
2310 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2311}
2312
2313// Examine .rela.opd to build info about function entry points.
2314
2315template<int size, bool big_endian>
2316void
2317Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2318 size_t reloc_count,
2319 const unsigned char* prelocs,
2320 const unsigned char* plocal_syms)
2321{
2322 if (size == 64)
cf43a2fe 2323 {
0e123f69
AM
2324 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2325 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2326 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2327 Address expected_off = 0;
2328 bool regular = true;
2329 unsigned int opd_ent_size = 0;
c9269dff
AM
2330
2331 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2332 {
c9269dff
AM
2333 Reltype reloc(prelocs);
2334 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2335 = reloc.get_r_info();
2336 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2337 if (r_type == elfcpp::R_PPC64_ADDR64)
2338 {
2339 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2340 typename elfcpp::Elf_types<size>::Elf_Addr value;
2341 bool is_ordinary;
2342 unsigned int shndx;
2343 if (r_sym < this->local_symbol_count())
2344 {
2345 typename elfcpp::Sym<size, big_endian>
2346 lsym(plocal_syms + r_sym * sym_size);
2347 shndx = lsym.get_st_shndx();
2348 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2349 value = lsym.get_st_value();
2350 }
2351 else
2352 shndx = this->symbol_section_and_value(r_sym, &value,
2353 &is_ordinary);
2354 this->set_opd_ent(reloc.get_r_offset(), shndx,
2355 value + reloc.get_r_addend());
ec4dbad3
AM
2356 if (i == 2)
2357 {
2358 expected_off = reloc.get_r_offset();
2359 opd_ent_size = expected_off;
2360 }
2361 else if (expected_off != reloc.get_r_offset())
2362 regular = false;
2363 expected_off += opd_ent_size;
2364 }
2365 else if (r_type == elfcpp::R_PPC64_TOC)
2366 {
2367 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2368 regular = false;
2369 }
2370 else
2371 {
2372 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2373 this->name().c_str(), r_type);
2374 regular = false;
c9269dff
AM
2375 }
2376 }
ec4dbad3
AM
2377 if (reloc_count <= 2)
2378 opd_ent_size = this->section_size(this->opd_shndx());
2379 if (opd_ent_size != 24 && opd_ent_size != 16)
2380 regular = false;
2381 if (!regular)
2382 {
2383 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2384 this->name().c_str());
2385 opd_ent_size = 0;
2386 }
c9269dff
AM
2387 }
2388}
2389
5edad15d
AM
2390// Returns true if a code sequence loading the TOC entry at VALUE
2391// relative to the TOC pointer can be converted into code calculating
2392// a TOC pointer relative offset.
2393// If so, the TOC pointer relative offset is stored to VALUE.
2394
2395template<int size, bool big_endian>
2396bool
2397Powerpc_relobj<size, big_endian>::make_toc_relative(
2398 Target_powerpc<size, big_endian>* target,
2399 Address* value)
2400{
2401 if (size != 64)
2402 return false;
2403
e666304e
AM
2404 // With -mcmodel=medium code it is quite possible to have
2405 // toc-relative relocs referring to objects outside the TOC.
2406 // Don't try to look at a non-existent TOC.
2407 if (this->toc_shndx() == 0)
2408 return false;
2409
5edad15d
AM
2410 // Convert VALUE back to an address by adding got_base (see below),
2411 // then to an offset in the TOC by subtracting the TOC output
2412 // section address and the TOC output offset. Since this TOC output
2413 // section and the got output section are one and the same, we can
2414 // omit adding and subtracting the output section address.
2415 Address off = (*value + this->toc_base_offset()
2416 - this->output_section_offset(this->toc_shndx()));
2417 // Is this offset in the TOC? -mcmodel=medium code may be using
2418 // TOC relative access to variables outside the TOC. Those of
2419 // course can't be optimized. We also don't try to optimize code
2420 // that is using a different object's TOC.
2421 if (off >= this->section_size(this->toc_shndx()))
2422 return false;
2423
2424 if (this->no_toc_opt(off))
2425 return false;
2426
2427 section_size_type vlen;
2428 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2429 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2430 // The TOC pointer
2431 Address got_base = (target->got_section()->output_section()->address()
2432 + this->toc_base_offset());
2433 addr -= got_base;
857e829e 2434 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2435 return false;
2436
2437 *value = addr;
2438 return true;
2439}
2440
c9b8abb7
AM
2441template<int size, bool big_endian>
2442bool
2443Powerpc_relobj<size, big_endian>::make_got_relative(
2444 Target_powerpc<size, big_endian>* target,
2445 const Symbol_value<size>* psymval,
2446 Address addend,
2447 Address* value)
2448{
2449 Address addr = psymval->value(this, addend);
2450 Address got_base = (target->got_section()->output_section()->address()
2451 + this->toc_base_offset());
2452 addr -= got_base;
2453 if (addr + 0x80008000 > 0xffffffff)
2454 return false;
2455
2456 *value = addr;
2457 return true;
2458}
2459
5edad15d
AM
2460// Perform the Sized_relobj_file method, then set up opd info from
2461// .opd relocs.
2462
c9269dff
AM
2463template<int size, bool big_endian>
2464void
2465Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2466{
2467 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2468 if (size == 64)
2469 {
2470 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2471 p != rd->relocs.end();
2472 ++p)
2473 {
2474 if (p->data_shndx == this->opd_shndx())
2475 {
ec4dbad3
AM
2476 uint64_t opd_size = this->section_size(this->opd_shndx());
2477 gold_assert(opd_size == static_cast<size_t>(opd_size));
2478 if (opd_size != 0)
2479 {
2480 this->init_opd(opd_size);
2481 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2482 rd->local_symbols->data());
2483 }
c9269dff
AM
2484 break;
2485 }
cf43a2fe
AM
2486 }
2487 }
cf43a2fe
AM
2488}
2489
b4f7960d
AM
2490// Read the symbols then set up st_other vector.
2491
2492template<int size, bool big_endian>
2493void
2494Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2495{
f35c4853 2496 this->base_read_symbols(sd);
724436fc
AM
2497 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2498 return;
b4f7960d
AM
2499 if (size == 64)
2500 {
2501 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2502 const unsigned char* const pshdrs = sd->section_headers->data();
2503 const unsigned int loccount = this->do_local_symbol_count();
2504 if (loccount != 0)
2505 {
2506 this->st_other_.resize(loccount);
2507 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2508 off_t locsize = loccount * sym_size;
2509 const unsigned int symtab_shndx = this->symtab_shndx();
2510 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2511 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2512 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2513 locsize, true, false);
2514 psyms += sym_size;
2515 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2516 {
2517 elfcpp::Sym<size, big_endian> sym(psyms);
2518 unsigned char st_other = sym.get_st_other();
2519 this->st_other_[i] = st_other;
2520 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2521 {
2522 if (this->abiversion() == 0)
2523 this->set_abiversion(2);
2524 else if (this->abiversion() < 2)
2525 gold_error(_("%s: local symbol %d has invalid st_other"
2526 " for ABI version 1"),
2527 this->name().c_str(), i);
2528 }
2529 }
2530 }
2531 }
724436fc
AM
2532
2533 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2534 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2535 bool merge_attributes = false;
2536 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2537 {
2538 elfcpp::Shdr<size, big_endian> shdr(ps);
2539 switch (shdr.get_sh_type())
2540 {
2541 case elfcpp::SHT_GNU_ATTRIBUTES:
2542 {
2543 gold_assert(this->attributes_section_data_ == NULL);
2544 section_offset_type section_offset = shdr.get_sh_offset();
2545 section_size_type section_size =
2546 convert_to_section_size_type(shdr.get_sh_size());
2547 const unsigned char* view =
2548 this->get_view(section_offset, section_size, true, false);
2549 this->attributes_section_data_ =
2550 new Attributes_section_data(view, section_size);
2551 }
2552 break;
2553
2554 case elfcpp::SHT_SYMTAB:
2555 {
2556 // Sometimes an object has no contents except the section
2557 // name string table and an empty symbol table with the
2558 // undefined symbol. We don't want to merge
2559 // processor-specific flags from such an object.
2560 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2561 elfcpp::Elf_sizes<size>::sym_size;
2562 if (shdr.get_sh_size() > sym_size)
2563 merge_attributes = true;
2564 }
2565 break;
2566
2567 case elfcpp::SHT_STRTAB:
2568 break;
2569
2570 default:
2571 merge_attributes = true;
2572 break;
2573 }
2574 }
2575
2576 if (!merge_attributes)
2577 {
2578 // Should rarely happen.
2579 delete this->attributes_section_data_;
2580 this->attributes_section_data_ = NULL;
2581 }
b4f7960d
AM
2582}
2583
2584template<int size, bool big_endian>
2585void
2586Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2587{
2588 this->e_flags_ |= ver;
2589 if (this->abiversion() != 0)
2590 {
2591 Target_powerpc<size, big_endian>* target =
2592 static_cast<Target_powerpc<size, big_endian>*>(
2593 parameters->sized_target<size, big_endian>());
2594 if (target->abiversion() == 0)
2595 target->set_abiversion(this->abiversion());
2596 else if (target->abiversion() != this->abiversion())
2597 gold_error(_("%s: ABI version %d is not compatible "
2598 "with ABI version %d output"),
2599 this->name().c_str(),
2600 this->abiversion(), target->abiversion());
2601
2602 }
2603}
2604
f35c4853 2605// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2606// read .opd from a dynamic object, filling in opd_ent_ vector,
2607
2608template<int size, bool big_endian>
2609void
2610Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2611{
f35c4853 2612 this->base_read_symbols(sd);
724436fc
AM
2613 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2614 const unsigned char* ps =
2615 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2616 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2617 {
2618 elfcpp::Shdr<size, big_endian> shdr(ps);
2619 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2620 {
2621 section_offset_type section_offset = shdr.get_sh_offset();
2622 section_size_type section_size =
2623 convert_to_section_size_type(shdr.get_sh_size());
2624 const unsigned char* view =
2625 this->get_view(section_offset, section_size, true, false);
2626 this->attributes_section_data_ =
2627 new Attributes_section_data(view, section_size);
2628 break;
2629 }
2630 }
dc3714f3
AM
2631 if (size == 64)
2632 {
dc3714f3
AM
2633 const unsigned char* const pshdrs = sd->section_headers->data();
2634 const unsigned char* namesu = sd->section_names->data();
2635 const char* names = reinterpret_cast<const char*>(namesu);
2636 const unsigned char* s = NULL;
2637 const unsigned char* opd;
2638 section_size_type opd_size;
2639
2640 // Find and read .opd section.
2641 while (1)
2642 {
2643 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2644 sd->section_names_size,
2645 s);
2646 if (s == NULL)
2647 return;
2648
2649 typename elfcpp::Shdr<size, big_endian> shdr(s);
2650 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2651 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2652 {
b4f7960d
AM
2653 if (this->abiversion() == 0)
2654 this->set_abiversion(1);
2655 else if (this->abiversion() > 1)
2656 gold_error(_("%s: .opd invalid in abiv%d"),
2657 this->name().c_str(), this->abiversion());
2658
dc3714f3
AM
2659 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2660 this->opd_address_ = shdr.get_sh_addr();
2661 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2662 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2663 true, false);
2664 break;
2665 }
2666 }
2667
2668 // Build set of executable sections.
2669 // Using a set is probably overkill. There is likely to be only
2670 // a few executable sections, typically .init, .text and .fini,
2671 // and they are generally grouped together.
2672 typedef std::set<Sec_info> Exec_sections;
2673 Exec_sections exec_sections;
2674 s = pshdrs;
2675 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2676 {
2677 typename elfcpp::Shdr<size, big_endian> shdr(s);
2678 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2679 && ((shdr.get_sh_flags()
2680 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2681 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2682 && shdr.get_sh_size() != 0)
2683 {
2684 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2685 shdr.get_sh_size(), i));
2686 }
2687 }
2688 if (exec_sections.empty())
2689 return;
2690
2691 // Look over the OPD entries. This is complicated by the fact
2692 // that some binaries will use two-word entries while others
2693 // will use the standard three-word entries. In most cases
2694 // the third word (the environment pointer for languages like
2695 // Pascal) is unused and will be zero. If the third word is
2696 // used it should not be pointing into executable sections,
2697 // I think.
2698 this->init_opd(opd_size);
2699 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2700 {
2701 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2702 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2703 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2704 if (val == 0)
2705 // Chances are that this is the third word of an OPD entry.
2706 continue;
2707 typename Exec_sections::const_iterator e
2708 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2709 if (e != exec_sections.begin())
2710 {
2711 --e;
2712 if (e->start <= val && val < e->start + e->len)
2713 {
2714 // We have an address in an executable section.
2715 // VAL ought to be the function entry, set it up.
2716 this->set_opd_ent(p - opd, e->shndx, val);
2717 // Skip second word of OPD entry, the TOC pointer.
2718 p += 8;
2719 }
2720 }
2721 // If we didn't match any executable sections, we likely
2722 // have a non-zero third word in the OPD entry.
2723 }
2724 }
2725}
2726
5edad15d
AM
2727// Relocate sections.
2728
2729template<int size, bool big_endian>
2730void
2731Powerpc_relobj<size, big_endian>::do_relocate_sections(
2732 const Symbol_table* symtab, const Layout* layout,
2733 const unsigned char* pshdrs, Output_file* of,
2734 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2735{
2736 unsigned int start = 1;
2737 if (size == 64
2738 && this->relatoc_ != 0
2739 && !parameters->options().relocatable())
2740 {
2741 // Relocate .toc first.
2742 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2743 this->relatoc_, this->relatoc_);
2744 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2745 1, this->relatoc_ - 1);
2746 start = this->relatoc_ + 1;
2747 }
2748 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2749 start, this->shnum() - 1);
2d7ad24e
AM
2750
2751 if (!parameters->options().output_is_position_independent())
2752 {
2753 Target_powerpc<size, big_endian>* target
2754 = static_cast<Target_powerpc<size, big_endian>*>(
2755 parameters->sized_target<size, big_endian>());
2756 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2757 {
2758 const section_size_type offset = target->lplt_section()->offset();
2759 const section_size_type oview_size
2760 = convert_to_section_size_type(target->lplt_section()->data_size());
2761 unsigned char* const oview = of->get_output_view(offset, oview_size);
2762
2763 bool modified = false;
2764 unsigned int nsyms = this->local_symbol_count();
2765 for (unsigned int i = 0; i < nsyms; i++)
2766 if (this->local_has_plt_offset(i))
2767 {
2768 Address value = this->local_symbol_value(i, 0);
2d7ad24e
AM
2769 size_t off = this->local_plt_offset(i);
2770 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2771 modified = true;
2772 }
2773 if (modified)
2774 of->write_output_view(offset, oview_size, oview);
2775 }
2776 }
5edad15d
AM
2777}
2778
f43ba157 2779// Set up some symbols.
26a4e9cb
AM
2780
2781template<int size, bool big_endian>
2782void
f43ba157
AM
2783Target_powerpc<size, big_endian>::do_define_standard_symbols(
2784 Symbol_table* symtab,
2785 Layout* layout)
26a4e9cb
AM
2786{
2787 if (size == 32)
2788 {
bb66a627
AM
2789 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2790 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2791 // non-relative dynamic relocs). The proper value will be
2792 // updated later.
2793 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2794 if (gotsym != NULL && gotsym->is_undefined())
2795 {
2796 Target_powerpc<size, big_endian>* target =
2797 static_cast<Target_powerpc<size, big_endian>*>(
2798 parameters->sized_target<size, big_endian>());
2799 Output_data_got_powerpc<size, big_endian>* got
2800 = target->got_section(symtab, layout);
2801 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2802 Symbol_table::PREDEFINED,
2803 got, 0, 0,
2804 elfcpp::STT_OBJECT,
bb66a627 2805 elfcpp::STB_LOCAL,
26a4e9cb
AM
2806 elfcpp::STV_HIDDEN, 0,
2807 false, false);
2808 }
2809
2810 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2811 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2812 if (sdasym != NULL && sdasym->is_undefined())
2813 {
2814 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2815 Output_section* os
2816 = layout->add_output_section_data(".sdata", 0,
2817 elfcpp::SHF_ALLOC
2818 | elfcpp::SHF_WRITE,
2819 sdata, ORDER_SMALL_DATA, false);
2820 symtab->define_in_output_data("_SDA_BASE_", NULL,
2821 Symbol_table::PREDEFINED,
2822 os, 32768, 0, elfcpp::STT_OBJECT,
2823 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2824 0, false, false);
2825 }
2826 }
b4f7960d
AM
2827 else
2828 {
2829 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2830 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2831 if (gotsym != NULL && gotsym->is_undefined())
2832 {
2833 Target_powerpc<size, big_endian>* target =
2834 static_cast<Target_powerpc<size, big_endian>*>(
2835 parameters->sized_target<size, big_endian>());
2836 Output_data_got_powerpc<size, big_endian>* got
2837 = target->got_section(symtab, layout);
2838 symtab->define_in_output_data(".TOC.", NULL,
2839 Symbol_table::PREDEFINED,
2840 got, 0x8000, 0,
2841 elfcpp::STT_OBJECT,
2842 elfcpp::STB_LOCAL,
2843 elfcpp::STV_HIDDEN, 0,
2844 false, false);
2845 }
2846 }
34e0882b
AM
2847
2848 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2849 if (parameters->options().tls_get_addr_optimize()
2850 && this->tls_get_addr_ != NULL
2851 && this->tls_get_addr_->in_reg())
2852 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2853 if (this->tls_get_addr_opt_ != NULL)
2854 {
2855 if (this->tls_get_addr_->is_undefined()
2856 || this->tls_get_addr_->is_from_dynobj())
2857 {
2858 // Make it seem as if references to __tls_get_addr are
2859 // really to __tls_get_addr_opt, so the latter symbol is
2860 // made dynamic, not the former.
2861 this->tls_get_addr_->clear_in_reg();
2862 this->tls_get_addr_opt_->set_in_reg();
2863 }
2864 // We have a non-dynamic definition for __tls_get_addr.
2865 // Make __tls_get_addr_opt the same, if it does not already have
2866 // a non-dynamic definition.
2867 else if (this->tls_get_addr_opt_->is_undefined()
2868 || this->tls_get_addr_opt_->is_from_dynobj())
2869 {
2870 Sized_symbol<size>* from
2871 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2872 Sized_symbol<size>* to
2873 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2874 symtab->clone<size>(to, from);
2875 }
2876 }
26a4e9cb
AM
2877}
2878
cf43a2fe
AM
2879// Set up PowerPC target specific relobj.
2880
2881template<int size, bool big_endian>
2882Object*
2883Target_powerpc<size, big_endian>::do_make_elf_object(
2884 const std::string& name,
2885 Input_file* input_file,
2886 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2887{
2888 int et = ehdr.get_e_type();
957564c9
AS
2889 // ET_EXEC files are valid input for --just-symbols/-R,
2890 // and we treat them as relocatable objects.
2891 if (et == elfcpp::ET_REL
2892 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2893 {
2894 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2895 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2896 obj->setup();
2897 return obj;
2898 }
2899 else if (et == elfcpp::ET_DYN)
2900 {
dc3714f3
AM
2901 Powerpc_dynobj<size, big_endian>* obj =
2902 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2903 obj->setup();
2904 return obj;
2905 }
2906 else
2907 {
c9269dff 2908 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2909 return NULL;
2910 }
2911}
2912
2913template<int size, bool big_endian>
2914class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2915{
2916public:
2917 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2918 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2919
2920 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2921 : Output_data_got<size, big_endian>(),
2922 symtab_(symtab), layout_(layout),
2923 header_ent_cnt_(size == 32 ? 3 : 1),
2924 header_index_(size == 32 ? 0x2000 : 0)
751e4d66
AM
2925 {
2926 if (size == 64)
2927 this->set_addralign(256);
2928 }
cf43a2fe 2929
e84fe78f
AM
2930 // Override all the Output_data_got methods we use so as to first call
2931 // reserve_ent().
2932 bool
2933 add_global(Symbol* gsym, unsigned int got_type)
2934 {
2935 this->reserve_ent();
2936 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2937 }
2938
2939 bool
2940 add_global_plt(Symbol* gsym, unsigned int got_type)
2941 {
2942 this->reserve_ent();
2943 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2944 }
2945
2946 bool
2947 add_global_tls(Symbol* gsym, unsigned int got_type)
2948 { return this->add_global_plt(gsym, got_type); }
2949
2950 void
2951 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2952 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2953 {
2954 this->reserve_ent();
2955 Output_data_got<size, big_endian>::
2956 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2957 }
2958
2959 void
2960 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2961 Output_data_reloc_generic* rel_dyn,
2962 unsigned int r_type_1, unsigned int r_type_2)
2963 {
aacb3b6d
AM
2964 if (gsym->has_got_offset(got_type))
2965 return;
2966
e84fe78f
AM
2967 this->reserve_ent(2);
2968 Output_data_got<size, big_endian>::
2969 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2970 }
2971
2972 bool
2973 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2974 {
2975 this->reserve_ent();
2976 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2977 got_type);
2978 }
2979
2980 bool
2981 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2982 {
2983 this->reserve_ent();
2984 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2985 got_type);
2986 }
2987
2988 bool
2989 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2990 { return this->add_local_plt(object, sym_index, got_type); }
2991
2992 void
2993 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2994 unsigned int got_type,
2995 Output_data_reloc_generic* rel_dyn,
2996 unsigned int r_type)
2997 {
aacb3b6d
AM
2998 if (object->local_has_got_offset(sym_index, got_type))
2999 return;
3000
e84fe78f
AM
3001 this->reserve_ent(2);
3002 Output_data_got<size, big_endian>::
3003 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
3004 }
3005
3006 unsigned int
3007 add_constant(Valtype constant)
3008 {
3009 this->reserve_ent();
3010 return Output_data_got<size, big_endian>::add_constant(constant);
3011 }
3012
dd93cd0a
AM
3013 unsigned int
3014 add_constant_pair(Valtype c1, Valtype c2)
3015 {
3016 this->reserve_ent(2);
e84fe78f 3017 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
3018 }
3019
3020 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
3021 unsigned int
3022 g_o_t() const
3023 {
3024 return this->got_offset(this->header_index_);
42cacb20 3025 }
cf43a2fe 3026
dd93cd0a
AM
3027 // Offset of base used to access the GOT/TOC.
3028 // The got/toc pointer reg will be set to this value.
26a4e9cb 3029 Valtype
dd93cd0a
AM
3030 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
3031 {
3032 if (size == 32)
3033 return this->g_o_t();
3034 else
3035 return (this->output_section()->address()
3036 + object->toc_base_offset()
3037 - this->address());
3038 }
3039
cf43a2fe
AM
3040 // Ensure our GOT has a header.
3041 void
3042 set_final_data_size()
3043 {
3044 if (this->header_ent_cnt_ != 0)
3045 this->make_header();
3046 Output_data_got<size, big_endian>::set_final_data_size();
3047 }
3048
3049 // First word of GOT header needs some values that are not
3050 // handled by Output_data_got so poke them in here.
3051 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3052 void
3053 do_write(Output_file* of)
3054 {
c9824451
AM
3055 Valtype val = 0;
3056 if (size == 32 && this->layout_->dynamic_data() != NULL)
3057 val = this->layout_->dynamic_section()->address();
3058 if (size == 64)
3059 val = this->output_section()->address() + 0x8000;
3060 this->replace_constant(this->header_index_, val);
cf43a2fe
AM
3061 Output_data_got<size, big_endian>::do_write(of);
3062 }
3063
3064private:
3065 void
3066 reserve_ent(unsigned int cnt = 1)
3067 {
3068 if (this->header_ent_cnt_ == 0)
3069 return;
3070 if (this->num_entries() + cnt > this->header_index_)
3071 this->make_header();
3072 }
3073
3074 void
3075 make_header()
3076 {
3077 this->header_ent_cnt_ = 0;
3078 this->header_index_ = this->num_entries();
3079 if (size == 32)
3080 {
3081 Output_data_got<size, big_endian>::add_constant(0);
3082 Output_data_got<size, big_endian>::add_constant(0);
3083 Output_data_got<size, big_endian>::add_constant(0);
3084
3085 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
3086 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3087 if (gotsym != NULL)
3088 {
3089 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3090 sym->set_value(this->g_o_t());
3091 }
3092 else
3093 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3094 Symbol_table::PREDEFINED,
3095 this, this->g_o_t(), 0,
3096 elfcpp::STT_OBJECT,
3097 elfcpp::STB_LOCAL,
3098 elfcpp::STV_HIDDEN, 0,
3099 false, false);
cf43a2fe
AM
3100 }
3101 else
3102 Output_data_got<size, big_endian>::add_constant(0);
3103 }
3104
3105 // Stashed pointers.
3106 Symbol_table* symtab_;
3107 Layout* layout_;
3108
3109 // GOT header size.
3110 unsigned int header_ent_cnt_;
3111 // GOT header index.
3112 unsigned int header_index_;
42cacb20
DE
3113};
3114
3115// Get the GOT section, creating it if necessary.
3116
3117template<int size, bool big_endian>
cf43a2fe 3118Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
3119Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3120 Layout* layout)
3121{
3122 if (this->got_ == NULL)
3123 {
3124 gold_assert(symtab != NULL && layout != NULL);
3125
cf43a2fe
AM
3126 this->got_
3127 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
3128
3129 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3130 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 3131 this->got_, ORDER_DATA, false);
42cacb20
DE
3132 }
3133
3134 return this->got_;
3135}
3136
3137// Get the dynamic reloc section, creating it if necessary.
3138
3139template<int size, bool big_endian>
3140typename Target_powerpc<size, big_endian>::Reloc_section*
3141Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3142{
3143 if (this->rela_dyn_ == NULL)
3144 {
3145 gold_assert(layout != NULL);
3146 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3147 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
3148 elfcpp::SHF_ALLOC, this->rela_dyn_,
3149 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
3150 }
3151 return this->rela_dyn_;
3152}
3153
b3ccdeb5
AM
3154// Similarly, but for ifunc symbols get the one for ifunc.
3155
3156template<int size, bool big_endian>
3157typename Target_powerpc<size, big_endian>::Reloc_section*
3158Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3159 Layout* layout,
3160 bool for_ifunc)
3161{
3162 if (!for_ifunc)
3163 return this->rela_dyn_section(layout);
3164
3165 if (this->iplt_ == NULL)
3166 this->make_iplt_section(symtab, layout);
3167 return this->iplt_->rel_plt();
3168}
3169
ec661b9d
AM
3170class Stub_control
3171{
3172 public:
3173 // Determine the stub group size. The group size is the absolute
3174 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 3175 // is passed a negative value, we restrict stubs to be always after
ec661b9d 3176 // the stubbed branches.
1c3a5fbe
AM
3177 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3178 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3179 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3180 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3181 owner_(NULL), output_section_(NULL)
ec661b9d 3182 {
ec661b9d
AM
3183 }
3184
3185 // Return true iff input section can be handled by current stub
3186 // group.
3187 bool
3188 can_add_to_stub_group(Output_section* o,
3189 const Output_section::Input_section* i,
3190 bool has14);
3191
3192 const Output_section::Input_section*
3193 owner()
3194 { return owner_; }
3195
3196 Output_section*
3197 output_section()
3198 { return output_section_; }
3199
a20605cf
AM
3200 void
3201 set_output_and_owner(Output_section* o,
3202 const Output_section::Input_section* i)
3203 {
3204 this->output_section_ = o;
3205 this->owner_ = i;
3206 }
3207
ec661b9d
AM
3208 private:
3209 typedef enum
3210 {
1c3a5fbe 3211 // Initial state.
ec661b9d 3212 NO_GROUP,
1c3a5fbe 3213 // Adding group sections before the stubs.
ec661b9d 3214 FINDING_STUB_SECTION,
1c3a5fbe 3215 // Adding group sections after the stubs.
ec661b9d
AM
3216 HAS_STUB_SECTION
3217 } State;
3218
ec661b9d 3219 uint32_t stub_group_size_;
a5018ae5 3220 bool stubs_always_after_branch_;
ec661b9d 3221 bool suppress_size_errors_;
1c3a5fbe
AM
3222 // True if a stub group can serve multiple output sections.
3223 bool multi_os_;
3224 State state_;
8a37735f
AM
3225 // Current max size of group. Starts at stub_group_size_ but is
3226 // reduced to stub_group_size_/1024 on seeing a section with
3227 // external conditional branches.
3228 uint32_t group_size_;
a5018ae5 3229 uint64_t group_start_addr_;
57f6d32d
AM
3230 // owner_ and output_section_ specify the section to which stubs are
3231 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
3232 const Output_section::Input_section* owner_;
3233 Output_section* output_section_;
3234};
3235
0cfdc767 3236// Return true iff input section can be handled by current stub
a5018ae5
AM
3237// group. Sections are presented to this function in order,
3238// so the first section is the head of the group.
ec661b9d
AM
3239
3240bool
3241Stub_control::can_add_to_stub_group(Output_section* o,
3242 const Output_section::Input_section* i,
3243 bool has14)
3244{
ec661b9d
AM
3245 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3246 uint64_t this_size;
3247 uint64_t start_addr = o->address();
3248
3249 if (whole_sec)
3250 // .init and .fini sections are pasted together to form a single
3251 // function. We can't be adding stubs in the middle of the function.
3252 this_size = o->data_size();
3253 else
3254 {
3255 start_addr += i->relobj()->output_section_offset(i->shndx());
3256 this_size = i->data_size();
3257 }
57f6d32d 3258
a5018ae5 3259 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
3260 uint32_t group_size = this->stub_group_size_;
3261 if (has14)
3262 this->group_size_ = group_size = group_size >> 10;
ec661b9d 3263
57f6d32d 3264 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
3265 gold_warning(_("%s:%s exceeds group size"),
3266 i->relobj()->name().c_str(),
3267 i->relobj()->section_name(i->shndx()).c_str());
3268
afe002dd
AM
3269 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3270 has14 ? " 14bit" : "",
3271 i->relobj()->name().c_str(),
3272 i->relobj()->section_name(i->shndx()).c_str(),
3273 (long long) this_size,
a5018ae5
AM
3274 (this->state_ == NO_GROUP
3275 ? this_size
3276 : (long long) end_addr - this->group_start_addr_));
afe002dd 3277
1c3a5fbe
AM
3278 if (this->state_ == NO_GROUP)
3279 {
3280 // Only here on very first use of Stub_control
3281 this->owner_ = i;
3282 this->output_section_ = o;
3283 this->state_ = FINDING_STUB_SECTION;
3284 this->group_size_ = group_size;
3285 this->group_start_addr_ = start_addr;
3286 return true;
3287 }
3288 else if (!this->multi_os_ && this->output_section_ != o)
3289 ;
3290 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 3291 {
a5018ae5 3292 // Can we add this section, which is after the stubs, to the
57f6d32d 3293 // group?
a5018ae5 3294 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3295 return true;
ec661b9d 3296 }
a5018ae5 3297 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 3298 {
a5018ae5
AM
3299 if ((whole_sec && this->output_section_ == o)
3300 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3301 {
a5018ae5 3302 // Stubs are added at the end of "owner_".
57f6d32d
AM
3303 this->owner_ = i;
3304 this->output_section_ = o;
a5018ae5 3305 return true;
57f6d32d 3306 }
a5018ae5
AM
3307 // The group before the stubs has reached maximum size.
3308 // Now see about adding sections after the stubs to the
3309 // group. If the current section has a 14-bit branch and
3310 // the group before the stubs exceeds group_size_ (because
3311 // they didn't have 14-bit branches), don't add sections
3312 // after the stubs: The size of stubs for such a large
3313 // group may exceed the reach of a 14-bit branch.
3314 if (!this->stubs_always_after_branch_
3315 && this_size <= this->group_size_
3316 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3317 {
a5018ae5
AM
3318 gold_debug(DEBUG_TARGET, "adding after stubs");
3319 this->state_ = HAS_STUB_SECTION;
3320 this->group_start_addr_ = start_addr;
57f6d32d
AM
3321 return true;
3322 }
ec661b9d 3323 }
a5018ae5
AM
3324 else
3325 gold_unreachable();
57f6d32d 3326
1c3a5fbe
AM
3327 gold_debug(DEBUG_TARGET,
3328 !this->multi_os_ && this->output_section_ != o
3329 ? "nope, new output section\n"
3330 : "nope, didn't fit\n");
afe002dd 3331
57f6d32d
AM
3332 // The section fails to fit in the current group. Set up a few
3333 // things for the next group. owner_ and output_section_ will be
3334 // set later after we've retrieved those values for the current
3335 // group.
3336 this->state_ = FINDING_STUB_SECTION;
8a37735f 3337 this->group_size_ = group_size;
a5018ae5 3338 this->group_start_addr_ = start_addr;
57f6d32d 3339 return false;
ec661b9d
AM
3340}
3341
3342// Look over all the input sections, deciding where to place stubs.
3343
3344template<int size, bool big_endian>
3345void
3346Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
3347 const Task*,
3348 bool no_size_errors)
ec661b9d 3349{
1c3a5fbe
AM
3350 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3351 parameters->options().stub_group_multi());
ec661b9d
AM
3352
3353 // Group input sections and insert stub table
a3e60ddb
AM
3354 Stub_table_owner* table_owner = NULL;
3355 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
3356 Layout::Section_list section_list;
3357 layout->get_executable_sections(&section_list);
3358 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
3359 for (Layout::Section_list::iterator o = section_list.begin();
3360 o != section_list.end();
ec661b9d
AM
3361 ++o)
3362 {
3363 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
3364 for (Input_section_list::const_iterator i
3365 = (*o)->input_sections().begin();
3366 i != (*o)->input_sections().end();
ec661b9d
AM
3367 ++i)
3368 {
a3e60ddb
AM
3369 if (i->is_input_section()
3370 || i->is_relaxed_input_section())
ec661b9d
AM
3371 {
3372 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3373 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3374 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3375 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3376 {
a3e60ddb
AM
3377 table_owner->output_section = stub_control.output_section();
3378 table_owner->owner = stub_control.owner();
a20605cf 3379 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 3380 table_owner = NULL;
ec661b9d 3381 }
a3e60ddb
AM
3382 if (table_owner == NULL)
3383 {
3384 table_owner = new Stub_table_owner;
3385 tables.push_back(table_owner);
3386 }
3387 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
3388 }
3389 }
3390 }
a3e60ddb 3391 if (table_owner != NULL)
0cfdc767 3392 {
a5018ae5
AM
3393 table_owner->output_section = stub_control.output_section();
3394 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
3395 }
3396 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3397 t != tables.end();
3398 ++t)
3399 {
3400 Stub_table<size, big_endian>* stub_table;
3401
3402 if ((*t)->owner->is_input_section())
3403 stub_table = new Stub_table<size, big_endian>(this,
3404 (*t)->output_section,
590b87ff
AM
3405 (*t)->owner,
3406 this->stub_tables_.size());
a3e60ddb
AM
3407 else if ((*t)->owner->is_relaxed_input_section())
3408 stub_table = static_cast<Stub_table<size, big_endian>*>(
3409 (*t)->owner->relaxed_input_section());
0cfdc767 3410 else
a3e60ddb
AM
3411 gold_unreachable();
3412 this->stub_tables_.push_back(stub_table);
3413 delete *t;
0cfdc767 3414 }
ec661b9d
AM
3415}
3416
32f59844 3417template<int size>
a3e60ddb
AM
3418static unsigned long
3419max_branch_delta (unsigned int r_type)
3420{
3421 if (r_type == elfcpp::R_POWERPC_REL14
3422 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3423 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3424 return 1L << 15;
3425 if (r_type == elfcpp::R_POWERPC_REL24
32f59844 3426 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
a3e60ddb
AM
3427 || r_type == elfcpp::R_PPC_PLTREL24
3428 || r_type == elfcpp::R_PPC_LOCAL24PC)
3429 return 1L << 25;
3430 return 0;
3431}
3432
7e57d19e
AM
3433// Return whether this branch is going via a plt call stub.
3434
3435template<int size, bool big_endian>
3436bool
3437Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3438 Powerpc_relobj<size, big_endian>* ppc_object,
3439 unsigned int shndx,
3440 Address offset,
3441 Target_powerpc* target,
3442 Symbol_table* symtab)
3443{
3444 if (this->object_ != ppc_object
3445 || this->shndx_ != shndx
3446 || this->offset_ != offset)
3447 return false;
3448
3449 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3450 if (sym != NULL && sym->is_forwarder())
3451 sym = symtab->resolve_forwards(sym);
2778747c
AM
3452 if (target->replace_tls_get_addr(sym))
3453 sym = target->tls_get_addr_opt();
7e57d19e
AM
3454 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3455 if (gsym != NULL
7ee7ff70
AM
3456 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3457 && !target->is_elfv2_localentry0(gsym))
3458 : (this->object_->local_has_plt_offset(this->r_sym_)
3459 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3460 {
3461 this->tocsave_ = 1;
3462 return true;
3463 }
3464 return false;
3465}
3466
ec661b9d
AM
3467// If this branch needs a plt call stub, or a long branch stub, make one.
3468
3469template<int size, bool big_endian>
a3e60ddb 3470bool
ec661b9d
AM
3471Target_powerpc<size, big_endian>::Branch_info::make_stub(
3472 Stub_table<size, big_endian>* stub_table,
3473 Stub_table<size, big_endian>* ifunc_stub_table,
3474 Symbol_table* symtab) const
3475{
3476 Symbol* sym = this->object_->global_symbol(this->r_sym_);
88b8e639
AM
3477 Target_powerpc<size, big_endian>* target =
3478 static_cast<Target_powerpc<size, big_endian>*>(
3479 parameters->sized_target<size, big_endian>());
34e0882b
AM
3480 if (sym != NULL && sym->is_forwarder())
3481 sym = symtab->resolve_forwards(sym);
2778747c
AM
3482 if (target->replace_tls_get_addr(sym))
3483 sym = target->tls_get_addr_opt();
34e0882b 3484 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
dc60b26d
AM
3485 bool ok = true;
3486
ec661b9d 3487 if (gsym != NULL
88b8e639 3488 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3489 : this->object_->local_has_plt_offset(this->r_sym_))
3490 {
9055360d
AM
3491 if (size == 64
3492 && gsym != NULL
3493 && target->abiversion() >= 2
3494 && !parameters->options().output_is_position_independent()
32f59844 3495 && !is_branch_reloc<size>(this->r_type_))
9055360d
AM
3496 target->glink_section()->add_global_entry(gsym);
3497 else
ec661b9d 3498 {
64b5d6d7
AM
3499 if (stub_table == NULL
3500 && !(size == 32
3501 && gsym != NULL
3502 && !parameters->options().output_is_position_independent()
32f59844 3503 && !is_branch_reloc<size>(this->r_type_)))
9055360d
AM
3504 stub_table = this->object_->stub_table(this->shndx_);
3505 if (stub_table == NULL)
3506 {
64b5d6d7
AM
3507 // This is a ref from a data section to an ifunc symbol,
3508 // or a non-branch reloc for which we always want to use
3509 // one set of stubs for resolving function addresses.
9055360d
AM
3510 stub_table = ifunc_stub_table;
3511 }
3512 gold_assert(stub_table != NULL);
a3e60ddb
AM
3513 Address from = this->object_->get_output_section_offset(this->shndx_);
3514 if (from != invalid_address)
3515 from += (this->object_->output_section(this->shndx_)->address()
3516 + this->offset_);
9055360d 3517 if (gsym != NULL)
dc60b26d
AM
3518 ok = stub_table->add_plt_call_entry(from,
3519 this->object_, gsym,
7e57d19e
AM
3520 this->r_type_, this->addend_,
3521 this->tocsave_);
9055360d 3522 else
dc60b26d
AM
3523 ok = stub_table->add_plt_call_entry(from,
3524 this->object_, this->r_sym_,
7e57d19e
AM
3525 this->r_type_, this->addend_,
3526 this->tocsave_);
ec661b9d 3527 }
ec661b9d
AM
3528 }
3529 else
3530 {
32f59844 3531 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
a3e60ddb
AM
3532 if (max_branch_offset == 0)
3533 return true;
ec661b9d
AM
3534 Address from = this->object_->get_output_section_offset(this->shndx_);
3535 gold_assert(from != invalid_address);
3536 from += (this->object_->output_section(this->shndx_)->address()
3537 + this->offset_);
3538 Address to;
e3b53295 3539 unsigned int other = 0;
ec661b9d
AM
3540 if (gsym != NULL)
3541 {
3542 switch (gsym->source())
3543 {
3544 case Symbol::FROM_OBJECT:
3545 {
3546 Object* symobj = gsym->object();
3547 if (symobj->is_dynamic()
3548 || symobj->pluginobj() != NULL)
a3e60ddb 3549 return true;
ec661b9d
AM
3550 bool is_ordinary;
3551 unsigned int shndx = gsym->shndx(&is_ordinary);
3552 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3553 return true;
ec661b9d
AM
3554 }
3555 break;
3556
3557 case Symbol::IS_UNDEFINED:
a3e60ddb 3558 return true;
ec661b9d
AM
3559
3560 default:
3561 break;
3562 }
3563 Symbol_table::Compute_final_value_status status;
3564 to = symtab->compute_final_value<size>(gsym, &status);
3565 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3566 return true;
e3b53295
AM
3567 if (size == 64)
3568 other = gsym->nonvis() >> 3;
ec661b9d
AM
3569 }
3570 else
3571 {
3572 const Symbol_value<size>* psymval
3573 = this->object_->local_symbol(this->r_sym_);
3574 Symbol_value<size> symval;
0f125432
CC
3575 if (psymval->is_section_symbol())
3576 symval.set_is_section_symbol();
ec661b9d
AM
3577 typedef Sized_relobj_file<size, big_endian> ObjType;
3578 typename ObjType::Compute_final_local_value_status status
3579 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3580 &symval, symtab);
3581 if (status != ObjType::CFLV_OK
3582 || !symval.has_output_value())
a3e60ddb 3583 return true;
ec661b9d 3584 to = symval.value(this->object_, 0);
e3b53295
AM
3585 if (size == 64)
3586 other = this->object_->st_other(this->r_sym_) >> 5;
ec661b9d 3587 }
cbcb23fa
AM
3588 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3589 to += this->addend_;
ec661b9d
AM
3590 if (stub_table == NULL)
3591 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3592 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3593 {
3594 unsigned int dest_shndx;
1611bc4a
AM
3595 if (!target->symval_for_branch(symtab, gsym, this->object_,
3596 &to, &dest_shndx))
3597 return true;
ec661b9d 3598 }
fa40fbe4
AM
3599 unsigned int local_ent = 0;
3600 if (size == 64
3601 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC)
3602 local_ent = elfcpp::ppc64_decode_local_entry(other);
3603 Address delta = to + local_ent - from;
32f59844
AM
3604 if (delta + max_branch_offset >= 2 * max_branch_offset
3605 || (size == 64
3606 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3607 && (gsym != NULL
3608 ? this->object_->ppc64_needs_toc(gsym)
3609 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3610 {
0cfdc767
AM
3611 if (stub_table == NULL)
3612 {
3613 gold_warning(_("%s:%s: branch in non-executable section,"
3614 " no long branch stub for you"),
3615 this->object_->name().c_str(),
3616 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3617 return true;
0cfdc767 3618 }
d49044c7
AM
3619 bool save_res = (size == 64
3620 && gsym != NULL
3621 && gsym->source() == Symbol::IN_OUTPUT_DATA
3622 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3623 ok = stub_table->add_long_branch_entry(this->object_,
3624 this->r_type_,
fa40fbe4 3625 from, to, other, save_res);
ec661b9d
AM
3626 }
3627 }
dc60b26d
AM
3628 if (!ok)
3629 gold_debug(DEBUG_TARGET,
3630 "branch at %s:%s+%#lx\n"
3631 "can't reach stub attached to %s:%s",
3632 this->object_->name().c_str(),
3633 this->object_->section_name(this->shndx_).c_str(),
3634 (unsigned long) this->offset_,
3635 stub_table->relobj()->name().c_str(),
3636 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3637
3638 return ok;
ec661b9d
AM
3639}
3640
3641// Relaxation hook. This is where we do stub generation.
3642
3643template<int size, bool big_endian>
3644bool
3645Target_powerpc<size, big_endian>::do_relax(int pass,
3646 const Input_objects*,
3647 Symbol_table* symtab,
3648 Layout* layout,
3649 const Task* task)
3650{
3651 unsigned int prev_brlt_size = 0;
3652 if (pass == 1)
ec661b9d 3653 {
b4f7960d
AM
3654 bool thread_safe
3655 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3656 if (size == 64
3657 && this->abiversion() < 2
3658 && !thread_safe
3659 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3660 {
e2458743 3661 static const char* const thread_starter[] =
9e69ed50
AM
3662 {
3663 "pthread_create",
3664 /* libstdc++ */
3665 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3666 /* librt */
3667 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3668 "mq_notify", "create_timer",
3669 /* libanl */
3670 "getaddrinfo_a",
3671 /* libgomp */
80272b8c 3672 "GOMP_parallel",
9e69ed50 3673 "GOMP_parallel_start",
80272b8c 3674 "GOMP_parallel_loop_static",
9e69ed50 3675 "GOMP_parallel_loop_static_start",
80272b8c 3676 "GOMP_parallel_loop_dynamic",
9e69ed50 3677 "GOMP_parallel_loop_dynamic_start",
80272b8c 3678 "GOMP_parallel_loop_guided",
9e69ed50 3679 "GOMP_parallel_loop_guided_start",
80272b8c 3680 "GOMP_parallel_loop_runtime",
9e69ed50 3681 "GOMP_parallel_loop_runtime_start",
80272b8c 3682 "GOMP_parallel_sections",
43819297 3683 "GOMP_parallel_sections_start",
f9dffbf0
AM
3684 /* libgo */
3685 "__go_go",
9e69ed50
AM
3686 };
3687
e2458743
AM
3688 if (parameters->options().shared())
3689 thread_safe = true;
3690 else
9e69ed50 3691 {
e2458743
AM
3692 for (unsigned int i = 0;
3693 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3694 i++)
3695 {
3696 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3697 thread_safe = (sym != NULL
3698 && sym->in_reg()
3699 && sym->in_real_elf());
3700 if (thread_safe)
3701 break;
3702 }
9e69ed50 3703 }
ec661b9d 3704 }
9e69ed50 3705 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3706 }
3707
3708 if (pass == 1)
3709 {
3710 this->stub_group_size_ = parameters->options().stub_group_size();
3711 bool no_size_errors = true;
3712 if (this->stub_group_size_ == 1)
3713 this->stub_group_size_ = 0x1c00000;
3714 else if (this->stub_group_size_ == -1)
3715 this->stub_group_size_ = -0x1e00000;
3716 else
3717 no_size_errors = false;
3718 this->group_sections(layout, task, no_size_errors);
3719 }
3720 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3721 {
3722 this->branch_lookup_table_.clear();
3723 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3724 p != this->stub_tables_.end();
3725 ++p)
3726 {
3727 (*p)->clear_stubs(true);
3728 }
3729 this->stub_tables_.clear();
3730 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3731 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3732 program_name, this->stub_group_size_);
3733 this->group_sections(layout, task, true);
ec661b9d
AM
3734 }
3735
3736 // We need address of stub tables valid for make_stub.
3737 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3738 p != this->stub_tables_.end();
3739 ++p)
3740 {
3741 const Powerpc_relobj<size, big_endian>* object
3742 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3743 Address off = object->get_output_section_offset((*p)->shndx());
3744 gold_assert(off != invalid_address);
3745 Output_section* os = (*p)->output_section();
3746 (*p)->set_address_and_size(os, off);
3747 }
3748
9e69ed50
AM
3749 if (pass != 1)
3750 {
3751 // Clear plt call stubs, long branch stubs and branch lookup table.
3752 prev_brlt_size = this->branch_lookup_table_.size();
3753 this->branch_lookup_table_.clear();
3754 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3755 p != this->stub_tables_.end();
3756 ++p)
3757 {
a3e60ddb 3758 (*p)->clear_stubs(false);
9e69ed50
AM
3759 }
3760 }
3761
3762 // Build all the stubs.
a3e60ddb 3763 this->relax_failed_ = false;
ec661b9d
AM
3764 Stub_table<size, big_endian>* ifunc_stub_table
3765 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3766 Stub_table<size, big_endian>* one_stub_table
3767 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3768 for (typename Branches::const_iterator b = this->branch_info_.begin();
3769 b != this->branch_info_.end();
3770 b++)
3771 {
a3e60ddb
AM
3772 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3773 && !this->relax_failed_)
3774 {
3775 this->relax_failed_ = true;
3776 this->relax_fail_count_++;
3777 if (this->relax_fail_count_ < 3)
3778 return true;
3779 }
ec661b9d 3780 }
32f59844
AM
3781 bool do_resize = false;
3782 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3783 p != this->stub_tables_.end();
3784 ++p)
3785 if ((*p)->need_resize())
3786 {
3787 do_resize = true;
3788 break;
3789 }
3790 if (do_resize)
3791 {
3792 this->branch_lookup_table_.clear();
3793 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3794 p != this->stub_tables_.end();
3795 ++p)
3796 (*p)->set_resizing(true);
3797 for (typename Branches::const_iterator b = this->branch_info_.begin();
3798 b != this->branch_info_.end();
3799 b++)
3800 {
3801 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3802 && !this->relax_failed_)
3803 {
3804 this->relax_failed_ = true;
3805 this->relax_fail_count_++;
3806 if (this->relax_fail_count_ < 3)
3807 return true;
3808 }
3809 }
3810 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3811 p != this->stub_tables_.end();
3812 ++p)
3813 (*p)->set_resizing(false);
3814 }
ec661b9d 3815
9e69ed50 3816 // Did anything change size?
ec661b9d
AM
3817 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3818 bool again = num_huge_branches != prev_brlt_size;
3819 if (size == 64 && num_huge_branches != 0)
3820 this->make_brlt_section(layout);
3821 if (size == 64 && again)
3822 this->brlt_section_->set_current_size(num_huge_branches);
3823
be897fb7
AM
3824 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3825 p != this->stub_tables_.rend();
3826 ++p)
3827 (*p)->remove_eh_frame(layout);
3828
3829 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3830 p != this->stub_tables_.end();
3831 ++p)
3832 (*p)->add_eh_frame(layout);
3833
ec661b9d
AM
3834 typedef Unordered_set<Output_section*> Output_sections;
3835 Output_sections os_need_update;
3836 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3837 p != this->stub_tables_.end();
3838 ++p)
3839 {
3840 if ((*p)->size_update())
3841 {
3842 again = true;
3843 os_need_update.insert((*p)->output_section());
3844 }
3845 }
3846
9e69ed50
AM
3847 // Set output section offsets for all input sections in an output
3848 // section that just changed size. Anything past the stubs will
3849 // need updating.
ec661b9d
AM
3850 for (typename Output_sections::iterator p = os_need_update.begin();
3851 p != os_need_update.end();
3852 p++)
3853 {
3854 Output_section* os = *p;
3855 Address off = 0;
3856 typedef Output_section::Input_section_list Input_section_list;
3857 for (Input_section_list::const_iterator i = os->input_sections().begin();
3858 i != os->input_sections().end();
3859 ++i)
3860 {
3861 off = align_address(off, i->addralign());
3862 if (i->is_input_section() || i->is_relaxed_input_section())
3863 i->relobj()->set_section_offset(i->shndx(), off);
3864 if (i->is_relaxed_input_section())
3865 {
3866 Stub_table<size, big_endian>* stub_table
3867 = static_cast<Stub_table<size, big_endian>*>(
3868 i->relaxed_input_section());
6395d38b
HS
3869 Address stub_table_size = stub_table->set_address_and_size(os, off);
3870 off += stub_table_size;
3871 // After a few iterations, set current stub table size
3872 // as min size threshold, so later stub tables can only
3873 // grow in size.
3874 if (pass >= 4)
3875 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3876 }
3877 else
3878 off += i->data_size();
3879 }
6830ee24
AM
3880 // If .branch_lt is part of this output section, then we have
3881 // just done the offset adjustment.
ec661b9d
AM
3882 os->clear_section_offsets_need_adjustment();
3883 }
3884
3885 if (size == 64
3886 && !again
3887 && num_huge_branches != 0
3888 && parameters->options().output_is_position_independent())
3889 {
3890 // Fill in the BRLT relocs.
06f30c9d 3891 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3892 for (typename Branch_lookup_table::const_iterator p
3893 = this->branch_lookup_table_.begin();
3894 p != this->branch_lookup_table_.end();
3895 ++p)
3896 {
3897 this->brlt_section_->add_reloc(p->first, p->second);
3898 }
06f30c9d 3899 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3900 }
590b87ff
AM
3901
3902 if (!again
3903 && (parameters->options().user_set_emit_stub_syms()
3904 ? parameters->options().emit_stub_syms()
3905 : (size == 64
3906 || parameters->options().output_is_position_independent()
3907 || parameters->options().emit_relocs())))
3908 {
3909 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3910 p != this->stub_tables_.end();
3911 ++p)
3912 (*p)->define_stub_syms(symtab);
3913
3914 if (this->glink_ != NULL)
3915 {
9e390558 3916 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3917 Address value = -stub_size;
3918 if (size == 64)
3919 {
3920 value = 8;
3921 stub_size -= 8;
3922 }
3923 this->define_local(symtab, "__glink_PLTresolve",
3924 this->glink_, value, stub_size);
3925
3926 if (size != 64)
3927 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3928 }
3929 }
3930
ec661b9d
AM
3931 return again;
3932}
3933
9d5781f8
AM
3934template<int size, bool big_endian>
3935void
3936Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3937 unsigned char* oview,
3938 uint64_t* paddress,
3939 off_t* plen) const
3940{
3941 uint64_t address = plt->address();
3942 off_t len = plt->data_size();
3943
3944 if (plt == this->glink_)
3945 {
3946 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3947 if (len == 0)
3948 {
3949 gold_assert(parameters->doing_static_link());
3950 // Static linking may need stubs, to support ifunc and long
3951 // branches. We need to create an output section for
3952 // .eh_frame early in the link process, to have a place to
3953 // attach stub .eh_frame info. We also need to have
3954 // registered a CIE that matches the stub CIE. Both of
3955 // these requirements are satisfied by creating an FDE and
3956 // CIE for .glink, even though static linking will leave
3957 // .glink zero length.
3958 // ??? Hopefully generating an FDE with a zero address range
3959 // won't confuse anything that consumes .eh_frame info.
3960 }
3961 else if (size == 64)
9d5781f8
AM
3962 {
3963 // There is one word before __glink_PLTresolve
3964 address += 8;
3965 len -= 8;
3966 }
3967 else if (parameters->options().output_is_position_independent())
3968 {
3969 // There are two FDEs for a position independent glink.
3970 // The first covers the branch table, the second
3971 // __glink_PLTresolve at the end of glink.
9e390558 3972 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 3973 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3974 len -= resolve_size;
3975 else
3976 {
3977 address += len - resolve_size;
3978 len = resolve_size;
3979 }
3980 }
3981 }
3982 else
3983 {
3984 // Must be a stub table.
3985 const Stub_table<size, big_endian>* stub_table
3986 = static_cast<const Stub_table<size, big_endian>*>(plt);
3987 uint64_t stub_address = stub_table->stub_address();
3988 len -= stub_address - address;
3989 address = stub_address;
3990 }
3991
3992 *paddress = address;
3993 *plen = len;
3994}
3995
42cacb20
DE
3996// A class to handle the PLT data.
3997
3998template<int size, bool big_endian>
cf43a2fe 3999class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
4000{
4001 public:
4002 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4003 size, big_endian> Reloc_section;
4004
e5d5f5ed
AM
4005 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4006 Reloc_section* plt_rel,
e5d5f5ed
AM
4007 const char* name)
4008 : Output_section_data_build(size == 32 ? 4 : 8),
4009 rel_(plt_rel),
4010 targ_(targ),
e5d5f5ed
AM
4011 name_(name)
4012 { }
42cacb20
DE
4013
4014 // Add an entry to the PLT.
03e25981 4015 void
cf43a2fe 4016 add_entry(Symbol*);
42cacb20 4017
03e25981 4018 void
e5d5f5ed
AM
4019 add_ifunc_entry(Symbol*);
4020
2d7ad24e
AM
4021 void
4022 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4023
03e25981 4024 void
e5d5f5ed
AM
4025 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4026
42cacb20 4027 // Return the .rela.plt section data.
e5d5f5ed 4028 Reloc_section*
cf43a2fe
AM
4029 rel_plt() const
4030 {
42cacb20
DE
4031 return this->rel_;
4032 }
4033
0e70b911
CC
4034 // Return the number of PLT entries.
4035 unsigned int
4036 entry_count() const
d83ce4e3 4037 {
b3ccdeb5
AM
4038 if (this->current_data_size() == 0)
4039 return 0;
b4f7960d
AM
4040 return ((this->current_data_size() - this->first_plt_entry_offset())
4041 / this->plt_entry_size());
d83ce4e3 4042 }
0e70b911 4043
42cacb20 4044 protected:
42cacb20 4045 void
cf43a2fe 4046 do_adjust_output_section(Output_section* os)
42cacb20 4047 {
cf43a2fe 4048 os->set_entsize(0);
42cacb20
DE
4049 }
4050
6ce78956
AM
4051 // Write to a map file.
4052 void
4053 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4054 { mapfile->print_output_data(this, this->name_); }
6ce78956 4055
cf43a2fe 4056 private:
b4f7960d
AM
4057 // Return the offset of the first non-reserved PLT entry.
4058 unsigned int
4059 first_plt_entry_offset() const
4060 {
2d7ad24e
AM
4061 // IPLT and LPLT have no reserved entry.
4062 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4063 return 0;
4064 return this->targ_->first_plt_entry_offset();
4065 }
4066
4067 // Return the size of each PLT entry.
4068 unsigned int
4069 plt_entry_size() const
4070 {
4071 return this->targ_->plt_entry_size();
4072 }
cf43a2fe 4073
42cacb20
DE
4074 // Write out the PLT data.
4075 void
4076 do_write(Output_file*);
4077
4078 // The reloc section.
4079 Reloc_section* rel_;
cf43a2fe
AM
4080 // Allows access to .glink for do_write.
4081 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
4082 // What to report in map file.
4083 const char *name_;
42cacb20
DE
4084};
4085
e5d5f5ed 4086// Add an entry to the PLT.
42cacb20
DE
4087
4088template<int size, bool big_endian>
03e25981 4089void
e5d5f5ed 4090Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 4091{
e5d5f5ed
AM
4092 if (!gsym->has_plt_offset())
4093 {
ec661b9d 4094 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4095 if (off == 0)
4096 off += this->first_plt_entry_offset();
4097 gsym->set_plt_offset(off);
4098 gsym->set_needs_dynsym_entry();
4099 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4100 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 4101 off += this->plt_entry_size();
e5d5f5ed
AM
4102 this->set_current_data_size(off);
4103 }
42cacb20
DE
4104}
4105
e5d5f5ed 4106// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4107
4108template<int size, bool big_endian>
03e25981 4109void
e5d5f5ed 4110Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4111{
cf43a2fe
AM
4112 if (!gsym->has_plt_offset())
4113 {
ec661b9d 4114 section_size_type off = this->current_data_size();
cf43a2fe 4115 gsym->set_plt_offset(off);
e5d5f5ed 4116 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4117 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4118 dynrel = elfcpp::R_PPC64_JMP_IREL;
4119 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4120 off += this->plt_entry_size();
e5d5f5ed
AM
4121 this->set_current_data_size(off);
4122 }
4123}
4124
2d7ad24e
AM
4125// Add an entry for a local symbol to the PLT.
4126
4127template<int size, bool big_endian>
4128void
4129Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4130 Sized_relobj_file<size, big_endian>* relobj,
4131 unsigned int local_sym_index)
4132{
4133 if (!relobj->local_has_plt_offset(local_sym_index))
4134 {
4135 section_size_type off = this->current_data_size();
4136 relobj->set_local_plt_offset(local_sym_index, off);
4137 if (this->rel_)
4138 {
4139 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4140 if (size == 64 && this->targ_->abiversion() < 2)
4141 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4142 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4143 dynrel, this, off, 0);
4144 }
4145 off += this->plt_entry_size();
4146 this->set_current_data_size(off);
4147 }
4148}
4149
e5d5f5ed
AM
4150// Add an entry for a local ifunc symbol to the IPLT.
4151
4152template<int size, bool big_endian>
03e25981 4153void
e5d5f5ed
AM
4154Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4155 Sized_relobj_file<size, big_endian>* relobj,
4156 unsigned int local_sym_index)
4157{
4158 if (!relobj->local_has_plt_offset(local_sym_index))
4159 {
ec661b9d 4160 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4161 relobj->set_local_plt_offset(local_sym_index, off);
4162 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4163 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4164 dynrel = elfcpp::R_PPC64_JMP_IREL;
4165 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4166 this, off, 0);
b4f7960d 4167 off += this->plt_entry_size();
cf43a2fe
AM
4168 this->set_current_data_size(off);
4169 }
42cacb20
DE
4170}
4171
dd93cd0a 4172static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4173static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4174static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4175static const uint32_t add_3_3_2 = 0x7c631214;
4176static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4177static const uint32_t add_3_12_2 = 0x7c6c1214;
4178static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4179static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4180static const uint32_t add_11_2_11 = 0x7d625a14;
4181static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4182static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4183static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4184static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4185static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4186static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4187static const uint32_t addi_12_1 = 0x39810000;
32f59844 4188static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4189static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4190static const uint32_t addis_0_2 = 0x3c020000;
4191static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4192static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4193static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4194static const uint32_t addis_11_11 = 0x3d6b0000;
4195static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4196static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4197static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4198static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4199static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4200static const uint32_t b = 0x48000000;
4201static const uint32_t bcl_20_31 = 0x429f0005;
4202static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4203static const uint32_t bctrl = 0x4e800421;
4204static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4205static const uint32_t blr = 0x4e800020;
9e69ed50 4206static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4207static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4208static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4209static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4210static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4211static const uint32_t cror_15_15_15 = 0x4def7b82;
4212static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29 4213static const uint32_t ld_0_1 = 0xe8010000;
63e5eea2 4214static const uint32_t ld_0_11 = 0xe80b0000;
f3a0ed29 4215static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4216static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4217static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4218static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4219static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4220static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4221static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4222static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4223static const uint32_t ld_11_11 = 0xe96b0000;
4224static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4225static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4226static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4227static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4228static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4229static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4230static const uint32_t li_0_0 = 0x38000000;
e4dff765 4231static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4232static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4233static const uint32_t lis_0 = 0x3c000000;
549dba71 4234static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4235static const uint32_t lis_11 = 0x3d600000;
4236static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4237static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4238static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4239static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4240static const uint32_t lwz_11_11 = 0x816b0000;
4241static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4242static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4243static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4244static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4245static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4246static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4247static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4248static const uint32_t mr_0_3 = 0x7c601b78;
4249static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4250static const uint32_t mtctr_0 = 0x7c0903a6;
4251static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4252static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4253static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4254static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4255static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4256static const uint32_t nop = 0x60000000;
c9269dff 4257static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4258static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4259static const uint32_t ori_12_12_0 = 0x618c0000;
4260static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4261static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4262static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4263static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4264static const uint32_t std_0_1 = 0xf8010000;
4265static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4266static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4267static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4268static const uint32_t stfd_0_1 = 0xd8010000;
4269static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4270static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4271static const uint32_t sub_12_12_11 = 0x7d8b6050;
4272static const uint32_t xor_2_12_12 = 0x7d826278;
4273static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4274
e4dff765
AM
4275static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4276static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4277static const uint64_t pnop = 0x0700000000000000ULL;
4278
42cacb20
DE
4279// Write out the PLT.
4280
4281template<int size, bool big_endian>
4282void
4283Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4284{
2d7ad24e 4285 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4286 {
ec661b9d 4287 const section_size_type offset = this->offset();
cf43a2fe
AM
4288 const section_size_type oview_size
4289 = convert_to_section_size_type(this->data_size());
4290 unsigned char* const oview = of->get_output_view(offset, oview_size);
4291 unsigned char* pov = oview;
4292 unsigned char* endpov = oview + oview_size;
4293
e5d5f5ed 4294 // The address of the .glink branch table
cf43a2fe
AM
4295 const Output_data_glink<size, big_endian>* glink
4296 = this->targ_->glink_section();
ec661b9d 4297 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4298
4299 while (pov < endpov)
4300 {
4301 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4302 pov += 4;
4303 branch_tab += 4;
4304 }
4305
4306 of->write_output_view(offset, oview_size, oview);
4307 }
4308}
4309
4310// Create the PLT section.
4311
4312template<int size, bool big_endian>
4313void
40b469d7
AM
4314Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4315 Layout* layout)
cf43a2fe
AM
4316{
4317 if (this->plt_ == NULL)
4318 {
40b469d7
AM
4319 if (this->got_ == NULL)
4320 this->got_section(symtab, layout);
4321
cf43a2fe
AM
4322 if (this->glink_ == NULL)
4323 make_glink_section(layout);
4324
4325 // Ensure that .rela.dyn always appears before .rela.plt This is
4326 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4327 // needs to include .rela.plt in its range.
cf43a2fe
AM
4328 this->rela_dyn_section(layout);
4329
e5d5f5ed
AM
4330 Reloc_section* plt_rel = new Reloc_section(false);
4331 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4332 elfcpp::SHF_ALLOC, plt_rel,
4333 ORDER_DYNAMIC_PLT_RELOCS, false);
4334 this->plt_
4335 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 4336 "** PLT");
cf43a2fe
AM
4337 layout->add_output_section_data(".plt",
4338 (size == 32
4339 ? elfcpp::SHT_PROGBITS
4340 : elfcpp::SHT_NOBITS),
4341 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4342 this->plt_,
4343 (size == 32
4344 ? ORDER_SMALL_DATA
4345 : ORDER_SMALL_BSS),
4346 false);
3254d32c
AM
4347
4348 Output_section* rela_plt_os = plt_rel->output_section();
4349 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4350 }
4351}
4352
e5d5f5ed
AM
4353// Create the IPLT section.
4354
4355template<int size, bool big_endian>
4356void
40b469d7
AM
4357Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4358 Layout* layout)
e5d5f5ed
AM
4359{
4360 if (this->iplt_ == NULL)
4361 {
40b469d7 4362 this->make_plt_section(symtab, layout);
2d7ad24e 4363 this->make_lplt_section(layout);
e5d5f5ed
AM
4364
4365 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4366 if (this->rela_dyn_->output_section())
4367 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
4368 this->iplt_
4369 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 4370 "** IPLT");
6528b6eb
AM
4371 if (this->plt_->output_section())
4372 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4373 }
4374}
4375
2d7ad24e
AM
4376// Create the LPLT section.
4377
4378template<int size, bool big_endian>
4379void
4380Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4381{
4382 if (this->lplt_ == NULL)
4383 {
4384 Reloc_section* lplt_rel = NULL;
4385 if (parameters->options().output_is_position_independent())
4386 {
4387 lplt_rel = new Reloc_section(false);
4388 this->rela_dyn_section(layout);
4389 if (this->rela_dyn_->output_section())
4390 this->rela_dyn_->output_section()
4391 ->add_output_section_data(lplt_rel);
4392 }
4393 this->lplt_
4394 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4395 "** LPLT");
4396 this->make_brlt_section(layout);
4397 if (this->brlt_section_ && this->brlt_section_->output_section())
4398 this->brlt_section_->output_section()
4399 ->add_output_section_data(this->lplt_);
4400 else
4401 layout->add_output_section_data(".branch_lt",
4402 elfcpp::SHT_PROGBITS,
4403 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4404 this->lplt_,
4405 ORDER_RELRO,
4406 true);
4407 }
4408}
4409
ec661b9d 4410// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4411
4412template<int size, bool big_endian>
ec661b9d 4413class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4414{
4415 public:
ec661b9d
AM
4416 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4417 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4418 size, big_endian> Reloc_section;
c9269dff 4419
ec661b9d
AM
4420 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4421 Reloc_section* brlt_rel)
4422 : Output_section_data_build(size == 32 ? 4 : 8),
4423 rel_(brlt_rel),
4424 targ_(targ)
4425 { }
cf43a2fe 4426
06f30c9d
CC
4427 void
4428 reset_brlt_sizes()
4429 {
4430 this->reset_data_size();
4431 this->rel_->reset_data_size();
4432 }
4433
4434 void
4435 finalize_brlt_sizes()
4436 {
4437 this->finalize_data_size();
4438 this->rel_->finalize_data_size();
4439 }
4440
ec661b9d 4441 // Add a reloc for an entry in the BRLT.
cf43a2fe 4442 void
ec661b9d
AM
4443 add_reloc(Address to, unsigned int off)
4444 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 4445
ec661b9d 4446 // Update section and reloc section size.
e5d5f5ed 4447 void
ec661b9d
AM
4448 set_current_size(unsigned int num_branches)
4449 {
4450 this->reset_address_and_file_offset();
4451 this->set_current_data_size(num_branches * 16);
4452 this->finalize_data_size();
4453 Output_section* os = this->output_section();
4454 os->set_section_offsets_need_adjustment();
4455 if (this->rel_ != NULL)
4456 {
0e123f69 4457 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
4458 this->rel_->reset_address_and_file_offset();
4459 this->rel_->set_current_data_size(num_branches * reloc_size);
4460 this->rel_->finalize_data_size();
4461 Output_section* os = this->rel_->output_section();
4462 os->set_section_offsets_need_adjustment();
4463 }
4464 }
cf43a2fe 4465
ec661b9d
AM
4466 protected:
4467 void
4468 do_adjust_output_section(Output_section* os)
4469 {
4470 os->set_entsize(0);
4471 }
e5d5f5ed 4472
ec661b9d
AM
4473 // Write to a map file.
4474 void
4475 do_print_to_mapfile(Mapfile* mapfile) const
4476 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4477
ec661b9d
AM
4478 private:
4479 // Write out the BRLT data.
4480 void
4481 do_write(Output_file*);
c9824451 4482
ec661b9d
AM
4483 // The reloc section.
4484 Reloc_section* rel_;
4485 Target_powerpc<size, big_endian>* targ_;
4486};
cf43a2fe 4487
ec661b9d
AM
4488// Make the branch lookup table section.
4489
4490template<int size, bool big_endian>
4491void
4492Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4493{
4494 if (size == 64 && this->brlt_section_ == NULL)
4495 {
4496 Reloc_section* brlt_rel = NULL;
4497 bool is_pic = parameters->options().output_is_position_independent();
4498 if (is_pic)
4499 {
54483898
AM
4500 // When PIC we can't fill in .branch_lt but must initialise at
4501 // runtime via dynamic relocations.
ec661b9d
AM
4502 this->rela_dyn_section(layout);
4503 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4504 if (this->rela_dyn_->output_section())
4505 this->rela_dyn_->output_section()
4506 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4507 }
4508 this->brlt_section_
4509 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4510 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4511 this->plt_->output_section()
4512 ->add_output_section_data(this->brlt_section_);
4513 else
6830ee24 4514 layout->add_output_section_data(".branch_lt",
54483898 4515 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4516 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4517 this->brlt_section_,
54483898
AM
4518 ORDER_RELRO,
4519 true);
ec661b9d
AM
4520 }
4521}
4522
6830ee24 4523// Write out .branch_lt when non-PIC.
ec661b9d
AM
4524
4525template<int size, bool big_endian>
4526void
4527Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4528{
4529 if (size == 64 && !parameters->options().output_is_position_independent())
4530 {
4531 const section_size_type offset = this->offset();
4532 const section_size_type oview_size
4533 = convert_to_section_size_type(this->data_size());
4534 unsigned char* const oview = of->get_output_view(offset, oview_size);
4535
4536 this->targ_->write_branch_lookup_table(oview);
4537 of->write_output_view(offset, oview_size, oview);
4538 }
4539}
4540
9e69ed50
AM
4541static inline uint32_t
4542l(uint32_t a)
4543{
4544 return a & 0xffff;
4545}
4546
4547static inline uint32_t
4548hi(uint32_t a)
4549{
4550 return l(a >> 16);
4551}
4552
4553static inline uint32_t
4554ha(uint32_t a)
4555{
4556 return hi(a + 0x8000);
4557}
4558
e4dff765
AM
4559static inline uint64_t
4560d34(uint64_t v)
4561{
4562 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4563}
4564
4565static inline uint64_t
4566ha34(uint64_t v)
4567{
4568 return (v + (1ULL << 33)) >> 34;
4569}
4570
9d5781f8
AM
4571template<int size>
4572struct Eh_cie
4573{
4574 static const unsigned char eh_frame_cie[12];
4575};
4576
4577template<int size>
4578const unsigned char Eh_cie<size>::eh_frame_cie[] =
4579{
4580 1, // CIE version.
4581 'z', 'R', 0, // Augmentation string.
4582 4, // Code alignment.
4583 0x80 - size / 8 , // Data alignment.
4584 65, // RA reg.
4585 1, // Augmentation size.
4586 (elfcpp::DW_EH_PE_pcrel
4587 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4588 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4589};
4590
b4f7960d
AM
4591// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4592static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4593{
4594 0, 0, 0, 0, // Replaced with offset to .glink.
4595 0, 0, 0, 0, // Replaced with size of .glink.
4596 0, // Augmentation size.
63e5eea2 4597 elfcpp::DW_CFA_advance_loc + 2,
9d5781f8 4598 elfcpp::DW_CFA_register, 65, 12,
63e5eea2 4599 elfcpp::DW_CFA_advance_loc + 4,
9d5781f8
AM
4600 elfcpp::DW_CFA_restore_extended, 65
4601};
4602
b4f7960d
AM
4603// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4604static const unsigned char glink_eh_frame_fde_64v2[] =
4605{
4606 0, 0, 0, 0, // Replaced with offset to .glink.
4607 0, 0, 0, 0, // Replaced with size of .glink.
4608 0, // Augmentation size.
63e5eea2 4609 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d 4610 elfcpp::DW_CFA_register, 65, 0,
63e5eea2
AM
4611 elfcpp::DW_CFA_advance_loc + 2,
4612 elfcpp::DW_CFA_restore_extended, 65
4613};
4614
4615static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4616{
4617 0, 0, 0, 0, // Replaced with offset to .glink.
4618 0, 0, 0, 0, // Replaced with size of .glink.
4619 0, // Augmentation size.
4620 elfcpp::DW_CFA_advance_loc + 3,
4621 elfcpp::DW_CFA_register, 65, 0,
4622 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d
AM
4623 elfcpp::DW_CFA_restore_extended, 65
4624};
4625
9d5781f8
AM
4626// Describe __glink_PLTresolve use of LR, 32-bit version.
4627static const unsigned char glink_eh_frame_fde_32[] =
4628{
4629 0, 0, 0, 0, // Replaced with offset to .glink.
4630 0, 0, 0, 0, // Replaced with size of .glink.
4631 0, // Augmentation size.
4632 elfcpp::DW_CFA_advance_loc + 2,
4633 elfcpp::DW_CFA_register, 65, 0,
4634 elfcpp::DW_CFA_advance_loc + 4,
4635 elfcpp::DW_CFA_restore_extended, 65
4636};
4637
4638static const unsigned char default_fde[] =
4639{
4640 0, 0, 0, 0, // Replaced with offset to stubs.
4641 0, 0, 0, 0, // Replaced with size of stubs.
4642 0, // Augmentation size.
4643 elfcpp::DW_CFA_nop, // Pad.
4644 elfcpp::DW_CFA_nop,
4645 elfcpp::DW_CFA_nop
4646};
4647
9e69ed50
AM
4648template<bool big_endian>
4649static inline void
4650write_insn(unsigned char* p, uint32_t v)
4651{
4652 elfcpp::Swap<32, big_endian>::writeval(p, v);
4653}
4654
691d2e9a
AM
4655template<int size>
4656static inline unsigned int
4657param_plt_align()
4658{
4659 if (!parameters->options().user_set_plt_align())
4660 return size == 64 ? 32 : 8;
4661 return 1 << parameters->options().plt_align();
4662}
4663
ec661b9d
AM
4664// Stub_table holds information about plt and long branch stubs.
4665// Stubs are built in an area following some input section determined
4666// by group_sections(). This input section is converted to a relaxed
4667// input section allowing it to be resized to accommodate the stubs
4668
4669template<int size, bool big_endian>
4670class Stub_table : public Output_relaxed_input_section
4671{
4672 public:
7e57d19e
AM
4673 struct Plt_stub_ent
4674 {
4675 Plt_stub_ent(unsigned int off, unsigned int indx)
afd2ea23
AM
4676 : off_(off), indx_(indx), iter_(0), notoc_(0), toc_(0),
4677 r2save_(0), localentry0_(0), tocoff_(0)
7e57d19e
AM
4678 { }
4679
4680 unsigned int off_;
afd2ea23 4681 unsigned int indx_;
32f59844
AM
4682 unsigned int iter_ : 1;
4683 unsigned int notoc_ : 1;
afd2ea23 4684 unsigned int toc_ : 1;
7e57d19e 4685 unsigned int r2save_ : 1;
7ee7ff70 4686 unsigned int localentry0_ : 1;
afd2ea23 4687 unsigned int tocoff_ : 8;
7e57d19e 4688 };
32f59844
AM
4689 struct Branch_stub_ent
4690 {
4691 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
afd2ea23 4692 : off_(off), iter_(0), notoc_(notoc), toc_(0), save_res_(save_res),
fa40fbe4 4693 other_(0), tocoff_(0)
32f59844
AM
4694 { }
4695
4696 unsigned int off_;
afd2ea23
AM
4697 unsigned int iter_ : 1;
4698 unsigned int notoc_ : 1;
4699 unsigned int toc_ : 1;
4700 unsigned int save_res_ : 1;
fa40fbe4 4701 unsigned int other_ : 3;
afd2ea23 4702 unsigned int tocoff_ : 8;
32f59844 4703 };
ec661b9d
AM
4704 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4705 static const Address invalid_address = static_cast<Address>(0) - 1;
4706
a3e60ddb
AM
4707 Stub_table(Target_powerpc<size, big_endian>* targ,
4708 Output_section* output_section,
590b87ff
AM
4709 const Output_section::Input_section* owner,
4710 uint32_t id)
a3e60ddb
AM
4711 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4712 owner->relobj()
4713 ->section_addralign(owner->shndx())),
ec661b9d 4714 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4715 orig_data_size_(owner->current_data_size()),
4716 plt_size_(0), last_plt_size_(0),
6395d38b 4717 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4718 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4719 uniq_(id)
a3e60ddb
AM
4720 {
4721 this->set_output_section(output_section);
ec661b9d 4722
a3e60ddb
AM
4723 std::vector<Output_relaxed_input_section*> new_relaxed;
4724 new_relaxed.push_back(this);
4725 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4726 }
ec661b9d
AM
4727
4728 // Add a plt call stub.
a3e60ddb
AM
4729 bool
4730 add_plt_call_entry(Address,
4731 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4732 const Symbol*,
4733 unsigned int,
7e57d19e
AM
4734 Address,
4735 bool);
ec661b9d 4736
a3e60ddb
AM
4737 bool
4738 add_plt_call_entry(Address,
4739 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4740 unsigned int,
4741 unsigned int,
7e57d19e
AM
4742 Address,
4743 bool);
ec661b9d
AM
4744
4745 // Find a given plt call stub.
7e57d19e 4746 const Plt_stub_ent*
ec661b9d
AM
4747 find_plt_call_entry(const Symbol*) const;
4748
7e57d19e 4749 const Plt_stub_ent*
ec661b9d
AM
4750 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4751 unsigned int) const;
4752
7e57d19e 4753 const Plt_stub_ent*
ec661b9d
AM
4754 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4755 const Symbol*,
4756 unsigned int,
4757 Address) const;
4758
7e57d19e 4759 const Plt_stub_ent*
ec661b9d
AM
4760 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4761 unsigned int,
4762 unsigned int,
4763 Address) const;
4764
4765 // Add a long branch stub.
a3e60ddb
AM
4766 bool
4767 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
fa40fbe4 4768 unsigned int, Address, Address, unsigned int, bool);
ec661b9d 4769
32f59844 4770 const Branch_stub_ent*
9d5781f8
AM
4771 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4772 Address) const;
ec661b9d 4773
a3e60ddb
AM
4774 bool
4775 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4776 {
32f59844 4777 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4778 if (max_branch_offset == 0)
4779 return true;
4780 gold_assert(from != invalid_address);
4781 Address loc = off + this->stub_address();
4782 return loc - from + max_branch_offset < 2 * max_branch_offset;
4783 }
4784
ec661b9d 4785 void
a3e60ddb 4786 clear_stubs(bool all)
cf43a2fe 4787 {
9e69ed50
AM
4788 this->plt_call_stubs_.clear();
4789 this->plt_size_ = 0;
ec661b9d
AM
4790 this->long_branch_stubs_.clear();
4791 this->branch_size_ = 0;
d49044c7 4792 this->need_save_res_ = false;
a3e60ddb
AM
4793 if (all)
4794 {
4795 this->last_plt_size_ = 0;
4796 this->last_branch_size_ = 0;
4797 }
cf43a2fe
AM
4798 }
4799
32f59844
AM
4800 bool
4801 need_resize() const
4802 { return need_resize_; }
4803
4804 void
4805 set_resizing(bool val)
4806 {
4807 this->resizing_ = val;
4808 if (val)
4809 {
4810 this->need_resize_ = false;
4811 this->plt_size_ = 0;
4812 this->branch_size_ = 0;
4813 this->need_save_res_ = false;
4814 }
4815 }
4816
ec661b9d
AM
4817 Address
4818 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4819 {
ec661b9d
AM
4820 Address start_off = off;
4821 off += this->orig_data_size_;
4822 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4823 if (this->need_save_res_)
4824 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4825 if (my_size != 0)
4826 off = align_address(off, this->stub_align());
4827 // Include original section size and alignment padding in size
4828 my_size += off - start_off;
6395d38b
HS
4829 // Ensure new size is always larger than min size
4830 // threshold. Alignment requirement is included in "my_size", so
4831 // increase "my_size" does not invalidate alignment.
4832 if (my_size < this->min_size_threshold_)
4833 my_size = this->min_size_threshold_;
ec661b9d
AM
4834 this->reset_address_and_file_offset();
4835 this->set_current_data_size(my_size);
4836 this->set_address_and_file_offset(os->address() + start_off,
4837 os->offset() + start_off);
4838 return my_size;
cf43a2fe
AM
4839 }
4840
ec661b9d 4841 Address
9d5781f8 4842 stub_address() const
ec661b9d
AM
4843 {
4844 return align_address(this->address() + this->orig_data_size_,
4845 this->stub_align());
4846 }
4847
4848 Address
9d5781f8 4849 stub_offset() const
ec661b9d
AM
4850 {
4851 return align_address(this->offset() + this->orig_data_size_,
4852 this->stub_align());
4853 }
4854
4855 section_size_type
4856 plt_size() const
4857 { return this->plt_size_; }
4858
32f59844
AM
4859 section_size_type
4860 branch_size() const
4861 { return this->branch_size_; }
4862
590b87ff
AM
4863 void
4864 set_min_size_threshold(Address min_size)
6395d38b
HS
4865 { this->min_size_threshold_ = min_size; }
4866
590b87ff
AM
4867 void
4868 define_stub_syms(Symbol_table*);
4869
ec661b9d
AM
4870 bool
4871 size_update()
4872 {
4873 Output_section* os = this->output_section();
4874 if (os->addralign() < this->stub_align())
4875 {
4876 os->set_addralign(this->stub_align());
4877 // FIXME: get rid of the insane checkpointing.
4878 // We can't increase alignment of the input section to which
4879 // stubs are attached; The input section may be .init which
4880 // is pasted together with other .init sections to form a
4881 // function. Aligning might insert zero padding resulting in
4882 // sigill. However we do need to increase alignment of the
4883 // output section so that the align_address() on offset in
4884 // set_address_and_size() adds the same padding as the
4885 // align_address() on address in stub_address().
4886 // What's more, we need this alignment for the layout done in
4887 // relaxation_loop_body() so that the output section starts at
4888 // a suitably aligned address.
4889 os->checkpoint_set_addralign(this->stub_align());
4890 }
9e69ed50
AM
4891 if (this->last_plt_size_ != this->plt_size_
4892 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4893 {
9e69ed50
AM
4894 this->last_plt_size_ = this->plt_size_;
4895 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4896 return true;
4897 }
4898 return false;
4899 }
4900
34e0882b
AM
4901 // Add .eh_frame info for this stub section.
4902 void
4903 add_eh_frame(Layout* layout);
be897fb7 4904
34e0882b 4905 // Remove .eh_frame info for this stub section.
be897fb7 4906 void
34e0882b 4907 remove_eh_frame(Layout* layout);
9d5781f8 4908
ec661b9d
AM
4909 Target_powerpc<size, big_endian>*
4910 targ() const
4911 { return targ_; }
6ce78956 4912
cf43a2fe 4913 private:
bdab445c
AM
4914 class Plt_stub_key;
4915 class Plt_stub_key_hash;
bdab445c
AM
4916 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4917 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
4918 class Branch_stub_key;
4919 class Branch_stub_key_hash;
4920 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4921 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
4922
4923 // Alignment of stub section.
ec661b9d 4924 unsigned int
9e69ed50
AM
4925 stub_align() const
4926 {
691d2e9a 4927 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
4928 unsigned int user_align = 1 << parameters->options().plt_align();
4929 return std::max(user_align, min_align);
4930 }
cf43a2fe 4931
91c2b899
AM
4932 // Return the plt offset for the given call stub.
4933 Address
08be3224
AM
4934 plt_off(typename Plt_stub_entries::const_iterator p,
4935 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
4936 {
4937 const Symbol* gsym = p->first.sym_;
4938 if (gsym != NULL)
08be3224 4939 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
4940 else
4941 {
91c2b899
AM
4942 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4943 unsigned int local_sym_index = p->first.locsym_;
08be3224 4944 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
4945 }
4946 }
4947
9e69ed50 4948 // Size of a given plt call stub.
ec661b9d 4949 unsigned int
afd2ea23 4950 plt_call_size(typename Plt_stub_entries::iterator p) const;
34e0882b
AM
4951
4952 unsigned int
4953 plt_call_align(unsigned int bytes) const
4954 {
691d2e9a 4955 unsigned int align = param_plt_align<size>();
9e390558 4956 return (bytes + align - 1) & -align;
9e69ed50 4957 }
ec661b9d
AM
4958
4959 // Return long branch stub size.
4960 unsigned int
afd2ea23 4961 branch_stub_size(typename Branch_stub_entries::iterator p,
32f59844
AM
4962 bool* need_lt);
4963
afd2ea23
AM
4964 void
4965 build_tls_opt_head(unsigned char** pp, bool save_lr);
32f59844 4966
afd2ea23
AM
4967 void
4968 build_tls_opt_tail(unsigned char* p);
ec661b9d 4969
f073a3e8
AM
4970 void
4971 plt_error(const Plt_stub_key& p);
4972
ec661b9d 4973 // Write out stubs.
cf43a2fe
AM
4974 void
4975 do_write(Output_file*);
4976
ec661b9d 4977 // Plt call stub keys.
bdab445c 4978 class Plt_stub_key
cf43a2fe 4979 {
d1a8cabd 4980 public:
bdab445c 4981 Plt_stub_key(const Symbol* sym)
c9824451
AM
4982 : sym_(sym), object_(0), addend_(0), locsym_(0)
4983 { }
4984
bdab445c 4985 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4986 unsigned int locsym_index)
c9824451
AM
4987 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4988 { }
4989
bdab445c 4990 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4991 const Symbol* sym,
4992 unsigned int r_type,
4993 Address addend)
e5d5f5ed 4994 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4995 {
4996 if (size != 32)
ec661b9d 4997 this->addend_ = addend;
d1a8cabd 4998 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4999 && (r_type == elfcpp::R_PPC_PLTREL24
5000 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 5001 {
ec661b9d 5002 this->addend_ = addend;
e5d5f5ed 5003 if (this->addend_ >= 32768)
d1a8cabd 5004 this->object_ = object;
cf43a2fe
AM
5005 }
5006 }
5007
bdab445c 5008 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5009 unsigned int locsym_index,
5010 unsigned int r_type,
5011 Address addend)
e5d5f5ed
AM
5012 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5013 {
5014 if (size != 32)
ec661b9d 5015 this->addend_ = addend;
e5d5f5ed 5016 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5017 && (r_type == elfcpp::R_PPC_PLTREL24
5018 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 5019 this->addend_ = addend;
e5d5f5ed
AM
5020 }
5021
bdab445c 5022 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
5023 {
5024 return (this->sym_ == that.sym_
5025 && this->object_ == that.object_
e5d5f5ed
AM
5026 && this->addend_ == that.addend_
5027 && this->locsym_ == that.locsym_);
cf43a2fe 5028 }
c9269dff
AM
5029
5030 const Symbol* sym_;
e5d5f5ed
AM
5031 const Sized_relobj_file<size, big_endian>* object_;
5032 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5033 unsigned int locsym_;
cf43a2fe
AM
5034 };
5035
bdab445c 5036 class Plt_stub_key_hash
cf43a2fe 5037 {
d1a8cabd 5038 public:
bdab445c 5039 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
5040 {
5041 return (reinterpret_cast<uintptr_t>(ent.sym_)
5042 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
5043 ^ ent.addend_
5044 ^ ent.locsym_);
cf43a2fe 5045 }
ec661b9d
AM
5046 };
5047
5048 // Long branch stub keys.
32f59844 5049 class Branch_stub_key
ec661b9d
AM
5050 {
5051 public:
32f59844
AM
5052 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
5053 : dest_(to), toc_base_off_(0)
ec661b9d
AM
5054 {
5055 if (size == 64)
5056 toc_base_off_ = obj->toc_base_offset();
5057 }
5058
32f59844 5059 bool operator==(const Branch_stub_key& that) const
ec661b9d
AM
5060 {
5061 return (this->dest_ == that.dest_
5062 && (size == 32
5063 || this->toc_base_off_ == that.toc_base_off_));
5064 }
cf43a2fe 5065
ec661b9d
AM
5066 Address dest_;
5067 unsigned int toc_base_off_;
5068 };
cf43a2fe 5069
32f59844 5070 class Branch_stub_key_hash
ec661b9d
AM
5071 {
5072 public:
32f59844
AM
5073 size_t operator()(const Branch_stub_key& key) const
5074 { return key.dest_ ^ key.toc_base_off_; }
ec661b9d 5075 };
cf43a2fe 5076
ec661b9d 5077 // In a sane world this would be a global.
cf43a2fe 5078 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5079 // Map sym/object/addend to stub offset.
ec661b9d
AM
5080 Plt_stub_entries plt_call_stubs_;
5081 // Map destination address to stub offset.
ec661b9d
AM
5082 Branch_stub_entries long_branch_stubs_;
5083 // size of input section
5084 section_size_type orig_data_size_;
5085 // size of stubs
9e69ed50 5086 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5087 // Some rare cases cause (PR/20529) fluctuation in stub table
5088 // size, which leads to an endless relax loop. This is to be fixed
5089 // by, after the first few iterations, allowing only increase of
5090 // stub table size. This variable sets the minimal possible size of
5091 // a stub table, it is zero for the first few iterations, then
5092 // increases monotonically.
5093 Address min_size_threshold_;
d49044c7
AM
5094 // Set if this stub group needs a copy of out-of-line register
5095 // save/restore functions.
5096 bool need_save_res_;
32f59844
AM
5097 // Set when notoc_/r2save_ changes after sizing a stub
5098 bool need_resize_;
5099 // Set when resizing stubs
5100 bool resizing_;
590b87ff
AM
5101 // Per stub table unique identifier.
5102 uint32_t uniq_;
cf43a2fe
AM
5103};
5104
ec661b9d 5105// Add a plt call stub, if we do not already have one for this
d1a8cabd 5106// sym/object/addend combo.
cf43a2fe
AM
5107
5108template<int size, bool big_endian>
a3e60ddb 5109bool
ec661b9d 5110Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5111 Address from,
c9824451 5112 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5113 const Symbol* gsym,
ec661b9d 5114 unsigned int r_type,
7e57d19e
AM
5115 Address addend,
5116 bool tocsave)
cf43a2fe 5117{
bdab445c
AM
5118 Plt_stub_key key(object, gsym, r_type, addend);
5119 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5120 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5121 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5122 if (size == 64)
7ee7ff70 5123 {
32f59844 5124 if (p.second
7ee7ff70
AM
5125 && this->targ_->is_elfv2_localentry0(gsym))
5126 {
5127 p.first->second.localentry0_ = 1;
5128 this->targ_->set_has_localentry0();
5129 }
32f59844
AM
5130 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5131 {
e4dff765 5132 if (!p.second && !p.first->second.notoc_
afd2ea23
AM
5133 && (!this->targ_->power10_stubs()
5134 || this->targ_->power10_stubs_auto()))
32f59844
AM
5135 this->need_resize_ = true;
5136 p.first->second.notoc_ = 1;
5137 }
afd2ea23 5138 else
32f59844 5139 {
afd2ea23 5140 if (!p.second && !p.first->second.toc_)
32f59844 5141 this->need_resize_ = true;
afd2ea23
AM
5142 p.first->second.toc_ = 1;
5143 if (!tocsave && !p.first->second.localentry0_)
5144 {
5145 if (!p.second && !p.first->second.r2save_)
5146 this->need_resize_ = true;
5147 p.first->second.r2save_ = 1;
5148 }
32f59844
AM
5149 }
5150 }
5151 if (p.second || (this->resizing_ && !p.first->second.iter_))
5152 {
5153 if (this->resizing_)
5154 {
5155 p.first->second.iter_ = 1;
5156 p.first->second.off_ = this->plt_size_;
5157 }
5158 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5159 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5160 this->targ_->set_has_tls_get_addr_opt();
34e0882b 5161 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70 5162 }
32f59844 5163 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5164}
5165
e5d5f5ed 5166template<int size, bool big_endian>
a3e60ddb 5167bool
ec661b9d 5168Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5169 Address from,
c9824451 5170 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5171 unsigned int locsym_index,
ec661b9d 5172 unsigned int r_type,
7e57d19e
AM
5173 Address addend,
5174 bool tocsave)
e5d5f5ed 5175{
bdab445c
AM
5176 Plt_stub_key key(object, locsym_index, r_type, addend);
5177 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5178 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5179 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5180 if (size == 64)
7ee7ff70 5181 {
32f59844 5182 if (p.second
7ee7ff70
AM
5183 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5184 {
5185 p.first->second.localentry0_ = 1;
5186 this->targ_->set_has_localentry0();
5187 }
32f59844
AM
5188 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5189 {
e4dff765 5190 if (!p.second && !p.first->second.notoc_
afd2ea23
AM
5191 && (!this->targ_->power10_stubs()
5192 || this->targ_->power10_stubs_auto()))
32f59844
AM
5193 this->need_resize_ = true;
5194 p.first->second.notoc_ = 1;
5195 }
afd2ea23 5196 else
32f59844 5197 {
afd2ea23 5198 if (!p.second && !p.first->second.toc_)
32f59844 5199 this->need_resize_ = true;
afd2ea23
AM
5200 p.first->second.toc_ = 1;
5201 if (!tocsave && !p.first->second.localentry0_)
5202 {
5203 if (!p.second && !p.first->second.r2save_)
5204 this->need_resize_ = true;
5205 p.first->second.r2save_ = 1;
5206 }
32f59844 5207 }
7ee7ff70 5208 }
32f59844
AM
5209 if (p.second || (this->resizing_ && !p.first->second.iter_))
5210 {
5211 if (this->resizing_)
5212 {
5213 p.first->second.iter_ = 1;
5214 p.first->second.off_ = this->plt_size_;
5215 }
5216 this->plt_size_ += this->plt_call_size(p.first);
5217 this->plt_size_ = this->plt_call_align(this->plt_size_);
5218 }
5219 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5220}
5221
ec661b9d
AM
5222// Find a plt call stub.
5223
cf43a2fe 5224template<int size, bool big_endian>
7e57d19e 5225const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5226Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5227 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5228 const Symbol* gsym,
ec661b9d
AM
5229 unsigned int r_type,
5230 Address addend) const
c9824451 5231{
bdab445c
AM
5232 Plt_stub_key key(object, gsym, r_type, addend);
5233 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5234 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5235 return NULL;
5236 return &p->second;
c9824451
AM
5237}
5238
5239template<int size, bool big_endian>
7e57d19e 5240const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5241Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5242{
bdab445c
AM
5243 Plt_stub_key key(gsym);
5244 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5245 if (p == this->plt_call_stubs_.end())
5246 return NULL;
5247 return &p->second;
cf43a2fe
AM
5248}
5249
e5d5f5ed 5250template<int size, bool big_endian>
7e57d19e 5251const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5252Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5253 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5254 unsigned int locsym_index,
ec661b9d
AM
5255 unsigned int r_type,
5256 Address addend) const
e5d5f5ed 5257{
bdab445c
AM
5258 Plt_stub_key key(object, locsym_index, r_type, addend);
5259 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5260 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5261 return NULL;
5262 return &p->second;
c9824451
AM
5263}
5264
5265template<int size, bool big_endian>
7e57d19e 5266const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5267Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5268 const Sized_relobj_file<size, big_endian>* object,
5269 unsigned int locsym_index) const
5270{
bdab445c
AM
5271 Plt_stub_key key(object, locsym_index);
5272 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5273 if (p == this->plt_call_stubs_.end())
5274 return NULL;
5275 return &p->second;
ec661b9d
AM
5276}
5277
5278// Add a long branch stub if we don't already have one to given
5279// destination.
5280
5281template<int size, bool big_endian>
a3e60ddb 5282bool
ec661b9d
AM
5283Stub_table<size, big_endian>::add_long_branch_entry(
5284 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
5285 unsigned int r_type,
5286 Address from,
d49044c7 5287 Address to,
fa40fbe4 5288 unsigned int other,
d49044c7 5289 bool save_res)
ec661b9d 5290{
32f59844
AM
5291 Branch_stub_key key(object, to);
5292 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5293 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
590b87ff 5294 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844 5295 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
afd2ea23 5296 if (notoc)
ec661b9d 5297 {
afd2ea23
AM
5298 if (!p.second && !p.first->second.notoc_)
5299 this->need_resize_ = true;
32f59844
AM
5300 p.first->second.notoc_ = true;
5301 }
afd2ea23
AM
5302 else
5303 {
5304 if (!p.second && !p.first->second.toc_)
5305 this->need_resize_ = true;
5306 p.first->second.toc_ = true;
5307 }
e3b53295 5308 if (size == 64 && p.first->second.other_ == 0)
fa40fbe4 5309 p.first->second.other_ = other;
32f59844
AM
5310 gold_assert(save_res == p.first->second.save_res_);
5311 if (p.second || (this->resizing_ && !p.first->second.iter_))
5312 {
5313 if (this->resizing_)
5314 {
5315 p.first->second.iter_ = 1;
5316 p.first->second.off_ = this->branch_size_;
5317 }
d49044c7
AM
5318 if (save_res)
5319 this->need_save_res_ = true;
5320 else
5321 {
32f59844
AM
5322 bool need_lt = false;
5323 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5324 this->branch_size_ += stub_size;
5325 if (size == 64 && need_lt)
d49044c7
AM
5326 this->targ_->add_branch_lookup_table(to);
5327 }
ec661b9d 5328 }
32f59844 5329 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5330}
5331
d49044c7 5332// Find long branch stub offset.
ec661b9d
AM
5333
5334template<int size, bool big_endian>
32f59844 5335const typename Stub_table<size, big_endian>::Branch_stub_ent*
ec661b9d
AM
5336Stub_table<size, big_endian>::find_long_branch_entry(
5337 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 5338 Address to) const
ec661b9d 5339{
32f59844 5340 Branch_stub_key key(object, to);
ec661b9d 5341 typename Branch_stub_entries::const_iterator p
32f59844 5342 = this->long_branch_stubs_.find(key);
d49044c7 5343 if (p == this->long_branch_stubs_.end())
32f59844
AM
5344 return NULL;
5345 return &p->second;
e5d5f5ed
AM
5346}
5347
220f9906
AM
5348template<bool big_endian>
5349static void
5350eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5351{
220f9906
AM
5352 delta /= 4;
5353 if (delta < 64)
5354 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5355 else if (delta < 256)
34e0882b 5356 {
220f9906
AM
5357 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5358 fde.push_back(delta);
5359 }
5360 else if (delta < 65536)
5361 {
5362 fde.resize(fde.size() + 3);
5363 unsigned char *p = &*fde.end() - 3;
5364 *p++ = elfcpp::DW_CFA_advance_loc2;
5365 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5366 }
5367 else
5368 {
5369 fde.resize(fde.size() + 5);
5370 unsigned char *p = &*fde.end() - 5;
5371 *p++ = elfcpp::DW_CFA_advance_loc4;
5372 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5373 }
220f9906
AM
5374}
5375
5376template<typename T>
5377static bool
5378stub_sort(T s1, T s2)
5379{
5380 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5381}
5382
5383// Add .eh_frame info for this stub section. Unlike other linker
5384// generated .eh_frame this is added late in the link, because we
5385// only want the .eh_frame info if this particular stub section is
5386// non-empty.
5387
5388template<int size, bool big_endian>
5389void
5390Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5391{
220f9906
AM
5392 if (size != 64
5393 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5394 return;
5395
5396 // Since we add stub .eh_frame info late, it must be placed
5397 // after all other linker generated .eh_frame info so that
5398 // merge mapping need not be updated for input sections.
5399 // There is no provision to use a different CIE to that used
5400 // by .glink.
5401 if (!this->targ_->has_glink())
5402 return;
5403
afd2ea23 5404 typedef typename Plt_stub_entries::iterator plt_iter;
220f9906
AM
5405 std::vector<plt_iter> calls;
5406 if (!this->plt_call_stubs_.empty())
5407 for (plt_iter cs = this->plt_call_stubs_.begin();
5408 cs != this->plt_call_stubs_.end();
5409 ++cs)
5410 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5411 && cs->second.r2save_
5412 && !cs->second.localentry0_)
e4dff765 5413 || (cs->second.notoc_
7c1f4227 5414 && !this->targ_->power10_stubs()))
220f9906
AM
5415 calls.push_back(cs);
5416 if (calls.size() > 1)
5417 std::stable_sort(calls.begin(), calls.end(),
5418 stub_sort<plt_iter>);
5419
5420 typedef typename Branch_stub_entries::const_iterator branch_iter;
5421 std::vector<branch_iter> branches;
e4dff765 5422 if (!this->long_branch_stubs_.empty()
7c1f4227 5423 && !this->targ_->power10_stubs())
220f9906
AM
5424 for (branch_iter bs = this->long_branch_stubs_.begin();
5425 bs != this->long_branch_stubs_.end();
5426 ++bs)
5427 if (bs->second.notoc_)
5428 branches.push_back(bs);
5429 if (branches.size() > 1)
5430 std::stable_sort(branches.begin(), branches.end(),
5431 stub_sort<branch_iter>);
5432
5433 if (calls.empty() && branches.empty())
34e0882b
AM
5434 return;
5435
220f9906
AM
5436 unsigned int last_eh_loc = 0;
5437 // offset pcrel sdata4, size udata4, and augmentation size byte.
5438 std::vector<unsigned char> fde(9, 0);
5439
5440 for (unsigned int i = 0; i < calls.size(); i++)
5441 {
5442 plt_iter cs = calls[i];
5443 unsigned int off = cs->second.off_;
5444 // The __tls_get_addr_opt call stub needs to describe where
5445 // it saves LR, to support exceptions that might be thrown
5446 // from __tls_get_addr, and to support asynchronous exceptions.
5447 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5448 {
5449 off += 7 * 4;
5450 if (cs->second.r2save_
5451 && !cs->second.localentry0_)
5452 {
5453 off += 2 * 4;
5454 eh_advance<big_endian>(fde, off - last_eh_loc);
5455 fde.resize(fde.size() + 6);
5456 unsigned char* p = &*fde.end() - 6;
5457 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5458 *p++ = 65;
5459 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5460 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5461 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5462 *p++ = elfcpp::DW_CFA_restore_extended;
5463 *p++ = 65;
5464 last_eh_loc = off + delta;
5465 continue;
5466 }
5467 }
5468 // notoc stubs also should describe LR changes, to support
5469 // asynchronous exceptions.
5470 off += (cs->second.r2save_ ? 4 : 0) + 8;
5471 eh_advance<big_endian>(fde, off - last_eh_loc);
5472 fde.resize(fde.size() + 6);
5473 unsigned char* p = &*fde.end() - 6;
5474 *p++ = elfcpp::DW_CFA_register;
5475 *p++ = 65;
5476 *p++ = 12;
5477 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5478 *p++ = elfcpp::DW_CFA_restore_extended;
5479 *p++ = 65;
5480 last_eh_loc = off + 8;
5481 }
5482
5483 for (unsigned int i = 0; i < branches.size(); i++)
5484 {
5485 branch_iter bs = branches[i];
5486 unsigned int off = bs->second.off_ + 8;
5487 eh_advance<big_endian>(fde, off - last_eh_loc);
5488 fde.resize(fde.size() + 6);
5489 unsigned char* p = &*fde.end() - 6;
5490 *p++ = elfcpp::DW_CFA_register;
5491 *p++ = 65;
5492 *p++ = 12;
5493 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5494 *p++ = elfcpp::DW_CFA_restore_extended;
5495 *p++ = 65;
5496 last_eh_loc = off + 8;
5497 }
5498
34e0882b
AM
5499 layout->add_eh_frame_for_plt(this,
5500 Eh_cie<size>::eh_frame_cie,
5501 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5502 &*fde.begin(), fde.size());
34e0882b
AM
5503}
5504
5505template<int size, bool big_endian>
5506void
5507Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5508{
220f9906
AM
5509 if (size == 64
5510 && parameters->options().ld_generated_unwind_info()
5511 && this->targ_->has_glink())
5512 layout->remove_eh_frame_for_plt(this,
5513 Eh_cie<size>::eh_frame_cie,
5514 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5515}
5516
ec661b9d
AM
5517// A class to handle .glink.
5518
5519template<int size, bool big_endian>
5520class Output_data_glink : public Output_section_data
5521{
5522 public:
9055360d
AM
5523 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5524 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5525
5526 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5527 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5528 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5529 { }
5530
9d5781f8 5531 void
9055360d 5532 add_eh_frame(Layout* layout);
9d5781f8 5533
9055360d
AM
5534 void
5535 add_global_entry(const Symbol*);
5536
5537 Address
5538 find_global_entry(const Symbol*) const;
5539
9e390558
AM
5540 unsigned int
5541 global_entry_align(unsigned int off) const
5542 {
691d2e9a 5543 unsigned int align = param_plt_align<size>();
9e390558
AM
5544 return (off + align - 1) & -align;
5545 }
5546
5547 unsigned int
5548 global_entry_off() const
5549 {
5550 return this->global_entry_align(this->end_branch_table_);
5551 }
5552
9055360d
AM
5553 Address
5554 global_entry_address() const
5555 {
5556 gold_assert(this->is_data_size_valid());
9e390558
AM
5557 return this->address() + this->global_entry_off();
5558 }
5559
5560 int
5561 pltresolve_size() const
5562 {
5563 if (size == 64)
5564 return (8
63e5eea2
AM
5565 + (this->targ_->abiversion() < 2 ? 11 * 4
5566 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
9e390558 5567 return 16 * 4;
9d5781f8
AM
5568 }
5569
ec661b9d
AM
5570 protected:
5571 // Write to a map file.
5572 void
5573 do_print_to_mapfile(Mapfile* mapfile) const
5574 { mapfile->print_output_data(this, _("** glink")); }
5575
5576 private:
5577 void
5578 set_final_data_size();
5579
5580 // Write out .glink
5581 void
5582 do_write(Output_file*);
5583
5584 // Allows access to .got and .plt for do_write.
5585 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5586
5587 // Map sym to stub offset.
5588 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5589 Global_entry_stub_entries global_entry_stubs_;
5590
5591 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5592};
5593
9055360d
AM
5594template<int size, bool big_endian>
5595void
5596Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5597{
5598 if (!parameters->options().ld_generated_unwind_info())
5599 return;
5600
5601 if (size == 64)
5602 {
5603 if (this->targ_->abiversion() < 2)
5604 layout->add_eh_frame_for_plt(this,
5605 Eh_cie<64>::eh_frame_cie,
5606 sizeof (Eh_cie<64>::eh_frame_cie),
5607 glink_eh_frame_fde_64v1,
5608 sizeof (glink_eh_frame_fde_64v1));
63e5eea2
AM
5609 else if (this->targ_->has_localentry0())
5610 layout->add_eh_frame_for_plt(this,
5611 Eh_cie<64>::eh_frame_cie,
5612 sizeof (Eh_cie<64>::eh_frame_cie),
5613 glink_eh_frame_fde_64v2_localentry0,
5614 sizeof (glink_eh_frame_fde_64v2));
9055360d
AM
5615 else
5616 layout->add_eh_frame_for_plt(this,
5617 Eh_cie<64>::eh_frame_cie,
5618 sizeof (Eh_cie<64>::eh_frame_cie),
5619 glink_eh_frame_fde_64v2,
5620 sizeof (glink_eh_frame_fde_64v2));
5621 }
5622 else
5623 {
5624 // 32-bit .glink can use the default since the CIE return
5625 // address reg, LR, is valid.
5626 layout->add_eh_frame_for_plt(this,
5627 Eh_cie<32>::eh_frame_cie,
5628 sizeof (Eh_cie<32>::eh_frame_cie),
5629 default_fde,
5630 sizeof (default_fde));
5631 // Except where LR is used in a PIC __glink_PLTresolve.
5632 if (parameters->options().output_is_position_independent())
5633 layout->add_eh_frame_for_plt(this,
5634 Eh_cie<32>::eh_frame_cie,
5635 sizeof (Eh_cie<32>::eh_frame_cie),
5636 glink_eh_frame_fde_32,
5637 sizeof (glink_eh_frame_fde_32));
5638 }
5639}
5640
5641template<int size, bool big_endian>
5642void
5643Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5644{
9e390558 5645 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5646 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5647 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5648 if (p.second)
407aa07c 5649 this->ge_size_ = off + 16;
9055360d
AM
5650}
5651
5652template<int size, bool big_endian>
5653typename Output_data_glink<size, big_endian>::Address
5654Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5655{
5656 typename Global_entry_stub_entries::const_iterator p
5657 = this->global_entry_stubs_.find(gsym);
5658 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5659}
5660
cf43a2fe
AM
5661template<int size, bool big_endian>
5662void
5663Output_data_glink<size, big_endian>::set_final_data_size()
5664{
ec661b9d
AM
5665 unsigned int count = this->targ_->plt_entry_count();
5666 section_size_type total = 0;
cf43a2fe
AM
5667
5668 if (count != 0)
5669 {
5670 if (size == 32)
5671 {
cf43a2fe
AM
5672 // space for branch table
5673 total += 4 * (count - 1);
5674
5675 total += -total & 15;
9e390558 5676 total += this->pltresolve_size();
cf43a2fe
AM
5677 }
5678 else
5679 {
9e390558 5680 total += this->pltresolve_size();
cf43a2fe
AM
5681
5682 // space for branch table
b4f7960d
AM
5683 total += 4 * count;
5684 if (this->targ_->abiversion() < 2)
5685 {
5686 total += 4 * count;
5687 if (count > 0x8000)
5688 total += 4 * (count - 0x8000);
5689 }
cf43a2fe
AM
5690 }
5691 }
9055360d 5692 this->end_branch_table_ = total;
9e390558 5693 total = this->global_entry_align(total);
9055360d 5694 total += this->ge_size_;
cf43a2fe
AM
5695
5696 this->set_data_size(total);
5697}
5698
590b87ff
AM
5699// Define symbols on stubs, identifying the stub.
5700
5701template<int size, bool big_endian>
5702void
5703Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5704{
5705 if (!this->plt_call_stubs_.empty())
5706 {
5707 // The key for the plt call stub hash table includes addresses,
5708 // therefore traversal order depends on those addresses, which
5709 // can change between runs if gold is a PIE. Unfortunately the
5710 // output .symtab ordering depends on the order in which symbols
5711 // are added to the linker symtab. We want reproducible output
5712 // so must sort the call stub symbols.
afd2ea23 5713 typedef typename Plt_stub_entries::iterator plt_iter;
590b87ff
AM
5714 std::vector<plt_iter> sorted;
5715 sorted.resize(this->plt_call_stubs_.size());
5716
5717 for (plt_iter cs = this->plt_call_stubs_.begin();
5718 cs != this->plt_call_stubs_.end();
5719 ++cs)
bdab445c 5720 sorted[cs->second.indx_] = cs;
590b87ff
AM
5721
5722 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5723 {
5724 plt_iter cs = sorted[i];
5725 char add[10];
5726 add[0] = 0;
5727 if (cs->first.addend_ != 0)
5728 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5729 char obj[10];
5730 obj[0] = 0;
5731 if (cs->first.object_)
590b87ff
AM
5732 {
5733 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5734 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5735 sprintf(obj, "%x:", ppcobj->uniq());
5736 }
5737 char localname[9];
5738 const char *symname;
5739 if (cs->first.sym_ == NULL)
5740 {
5741 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5742 symname = localname;
5743 }
34e0882b
AM
5744 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5745 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5746 else
5747 symname = cs->first.sym_->name();
94de2a2c
JC
5748 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5749 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5750 Address value
5751 = this->stub_address() - this->address() + cs->second.off_;
34e0882b 5752 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
590b87ff
AM
5753 this->targ_->define_local(symtab, name, this, value, stub_size);
5754 }
5755 }
5756
afd2ea23 5757 typedef typename Branch_stub_entries::iterator branch_iter;
590b87ff
AM
5758 for (branch_iter bs = this->long_branch_stubs_.begin();
5759 bs != this->long_branch_stubs_.end();
5760 ++bs)
5761 {
32f59844 5762 if (bs->second.save_res_)
590b87ff
AM
5763 continue;
5764
5765 char* name = new char[8 + 13 + 16 + 1];
5766 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5767 static_cast<unsigned long long>(bs->first.dest_));
5768 Address value = (this->stub_address() - this->address()
32f59844
AM
5769 + this->plt_size_ + bs->second.off_);
5770 bool need_lt = false;
5771 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5772 this->targ_->define_local(symtab, name, this, value, stub_size);
5773 }
5774}
5775
32f59844
AM
5776// Emit the start of a __tls_get_addr_opt plt call stub.
5777
5778template<int size, bool big_endian>
afd2ea23
AM
5779void
5780Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5781 bool save_lr)
32f59844 5782{
afd2ea23
AM
5783 unsigned char* p = *pp;
5784 if (size == 64)
32f59844 5785 {
afd2ea23
AM
5786 write_insn<big_endian>(p, ld_11_3 + 0);
5787 p += 4;
5788 write_insn<big_endian>(p, ld_12_3 + 8);
5789 p += 4;
5790 write_insn<big_endian>(p, mr_0_3);
5791 p += 4;
5792 write_insn<big_endian>(p, cmpdi_11_0);
5793 p += 4;
5794 write_insn<big_endian>(p, add_3_12_13);
5795 p += 4;
5796 write_insn<big_endian>(p, beqlr);
5797 p += 4;
5798 write_insn<big_endian>(p, mr_3_0);
5799 p += 4;
5800 if (save_lr)
32f59844 5801 {
afd2ea23 5802 write_insn<big_endian>(p, mflr_11);
32f59844 5803 p += 4;
afd2ea23 5804 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
32f59844
AM
5805 p += 4;
5806 }
32f59844 5807 }
afd2ea23 5808 else
32f59844 5809 {
afd2ea23 5810 write_insn<big_endian>(p, lwz_11_3 + 0);
32f59844 5811 p += 4;
afd2ea23 5812 write_insn<big_endian>(p, lwz_12_3 + 4);
32f59844 5813 p += 4;
afd2ea23 5814 write_insn<big_endian>(p, mr_0_3);
32f59844 5815 p += 4;
afd2ea23
AM
5816 write_insn<big_endian>(p, cmpwi_11_0);
5817 p += 4;
5818 write_insn<big_endian>(p, add_3_12_2);
5819 p += 4;
5820 write_insn<big_endian>(p, beqlr);
5821 p += 4;
5822 write_insn<big_endian>(p, mr_3_0);
5823 p += 4;
5824 write_insn<big_endian>(p, nop);
32f59844 5825 p += 4;
32f59844 5826 }
afd2ea23
AM
5827 *pp = p;
5828}
5829
5830// Emit the tail of a __tls_get_addr_opt plt call stub.
5831
5832template<int size, bool big_endian>
5833void
5834Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5835{
5836 write_insn<big_endian>(p, bctrl);
5837 p += 4;
5838 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5839 p += 4;
5840 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5841 p += 4;
5842 write_insn<big_endian>(p, mtlr_11);
5843 p += 4;
5844 write_insn<big_endian>(p, blr);
32f59844
AM
5845}
5846
e4dff765
AM
5847// Emit pc-relative plt call stub code.
5848
5849template<bool big_endian>
5850static unsigned char*
7c1f4227 5851build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
e4dff765
AM
5852{
5853 uint64_t insn;
5854 if (off - odd + (1ULL << 33) < 1ULL << 34)
5855 {
5856 off -= odd;
5857 if (odd)
5858 {
5859 write_insn<big_endian>(p, nop);
5860 p += 4;
5861 }
5862 if (load)
5863 insn = pld_12_pc;
5864 else
5865 insn = paddi_12_pc;
5866 insn |= d34(off);
5867 write_insn<big_endian>(p, insn >> 32);
5868 p += 4;
5869 write_insn<big_endian>(p, insn & 0xffffffff);
5870 }
5871 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5872 {
5873 off -= 8 - odd;
5874 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5875 p += 4;
5876 if (!odd)
5877 {
5878 write_insn<big_endian>(p, sldi_11_11_34);
5879 p += 4;
5880 }
5881 insn = paddi_12_pc | d34(off);
5882 write_insn<big_endian>(p, insn >> 32);
5883 p += 4;
5884 write_insn<big_endian>(p, insn & 0xffffffff);
5885 p += 4;
5886 if (odd)
5887 {
5888 write_insn<big_endian>(p, sldi_11_11_34);
5889 p += 4;
5890 }
5891 if (load)
5892 write_insn<big_endian>(p, ldx_12_11_12);
5893 else
5894 write_insn<big_endian>(p, add_12_11_12);
5895 }
5896 else
5897 {
5898 off -= odd + 8;
5899 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5900 p += 4;
5901 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5902 p += 4;
5903 if (odd)
5904 {
5905 write_insn<big_endian>(p, sldi_11_11_34);
5906 p += 4;
5907 }
5908 insn = paddi_12_pc | d34(off);
5909 write_insn<big_endian>(p, insn >> 32);
5910 p += 4;
5911 write_insn<big_endian>(p, insn & 0xffffffff);
5912 p += 4;
5913 if (!odd)
5914 {
5915 write_insn<big_endian>(p, sldi_11_11_34);
5916 p += 4;
5917 }
5918 if (load)
5919 write_insn<big_endian>(p, ldx_12_11_12);
5920 else
5921 write_insn<big_endian>(p, add_12_11_12);
5922 }
5923 p += 4;
5924 return p;
5925}
5926
32f59844
AM
5927// Gets the address of a label (1:) in r11 and builds an offset in r12,
5928// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5929// mflr %r12
5930// bcl 20,31,1f
5931// 1: mflr %r11
5932// mtlr %r12
5933// lis %r12,xxx-1b@highest
5934// ori %r12,%r12,xxx-1b@higher
5935// sldi %r12,%r12,32
5936// oris %r12,%r12,xxx-1b@high
5937// ori %r12,%r12,xxx-1b@l
5938// add/ldx %r12,%r11,%r12
5939
5940template<bool big_endian>
5941static unsigned char*
5942build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5943{
5944 write_insn<big_endian>(p, mflr_12);
5945 p += 4;
5946 write_insn<big_endian>(p, bcl_20_31);
5947 p += 4;
5948 write_insn<big_endian>(p, mflr_11);
5949 p += 4;
5950 write_insn<big_endian>(p, mtlr_12);
5951 p += 4;
5952 if (off + 0x8000 < 0x10000)
5953 {
5954 if (load)
5955 write_insn<big_endian>(p, ld_12_11 + l(off));
5956 else
5957 write_insn<big_endian>(p, addi_12_11 + l(off));
5958 }
5959 else if (off + 0x80008000ULL < 0x100000000ULL)
5960 {
5961 write_insn<big_endian>(p, addis_12_11 + ha(off));
5962 p += 4;
5963 if (load)
5964 write_insn<big_endian>(p, ld_12_12 + l(off));
5965 else
5966 write_insn<big_endian>(p, addi_12_12 + l(off));
5967 }
5968 else
5969 {
5970 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5971 {
5972 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5973 p += 4;
5974 }
5975 else
5976 {
5977 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5978 p += 4;
5979 if (((off >> 32) & 0xffff) != 0)
5980 {
5981 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5982 p += 4;
5983 }
5984 }
5985 if (((off >> 32) & 0xffffffffULL) != 0)
5986 {
5987 write_insn<big_endian>(p, sldi_12_12_32);
5988 p += 4;
5989 }
5990 if (hi(off) != 0)
5991 {
5992 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5993 p += 4;
5994 }
5995 if (l(off) != 0)
5996 {
5997 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5998 p += 4;
5999 }
6000 if (load)
6001 write_insn<big_endian>(p, ldx_12_11_12);
6002 else
6003 write_insn<big_endian>(p, add_12_11_12);
6004 }
6005 p += 4;
6006 return p;
6007}
6008
6009// Size of a given plt call stub.
6010
6011template<int size, bool big_endian>
6012unsigned int
6013Stub_table<size, big_endian>::plt_call_size(
afd2ea23 6014 typename Plt_stub_entries::iterator p) const
32f59844
AM
6015{
6016 if (size == 32)
6017 {
6018 const Symbol* gsym = p->first.sym_;
6019 return (4 * 4
6020 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
6021 }
6022
6023 const Output_data_plt_powerpc<size, big_endian>* plt;
6024 uint64_t plt_addr = this->plt_off(p, &plt);
6025 plt_addr += plt->address();
afd2ea23
AM
6026 if (this->targ_->power10_stubs()
6027 && this->targ_->power10_stubs_auto())
32f59844 6028 {
afd2ea23
AM
6029 unsigned int bytes = 0;
6030 if (p->second.notoc_)
6031 {
6032 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6033 bytes = 7 * 4;
6034 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6035 uint64_t odd = from & 4;
6036 uint64_t off = plt_addr - from;
6037 if (off - odd + (1ULL << 33) < 1ULL << 34)
6038 bytes += odd + 4 * 4;
6039 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6040 bytes += 7 * 4;
6041 else
6042 bytes += 8 * 4;
6043 bytes = this->plt_call_align(bytes);
6044 }
6045 unsigned int tail = 0;
6046 if (p->second.toc_)
6047 {
6048 p->second.tocoff_ = bytes;
6049 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6050 {
6051 bytes += 7 * 4;
6052 if (p->second.r2save_ && !p->second.localentry0_)
6053 {
6054 bytes += 2 * 4;
6055 tail = 4 * 4;
6056 }
6057 }
6058 if (p->second.r2save_)
6059 bytes += 4;
6060 uint64_t got_addr
6061 = this->targ_->got_section()->output_section()->address();
6062 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6063 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6064 got_addr += ppcobj->toc_base_offset();
6065 uint64_t off = plt_addr - got_addr;
6066 bytes += 3 * 4 + 4 * (ha(off) != 0);
6067 }
6068 return bytes + tail;
32f59844 6069 }
afd2ea23
AM
6070 else
6071 {
6072 unsigned int bytes = 0;
6073 unsigned int tail = 0;
6074 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6075 {
6076 bytes = 7 * 4;
6077 if (p->second.r2save_ && !p->second.localentry0_)
6078 {
6079 bytes = 9 * 4;
6080 tail = 4 * 4;
6081 }
6082 }
32f59844 6083
afd2ea23
AM
6084 if (p->second.r2save_)
6085 bytes += 4;
32f59844 6086
afd2ea23
AM
6087 if (this->targ_->power10_stubs())
6088 {
6089 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6090 uint64_t odd = from & 4;
6091 uint64_t off = plt_addr - from;
6092 if (off - odd + (1ULL << 33) < 1ULL << 34)
6093 bytes += odd + 4 * 4;
6094 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6095 bytes += 7 * 4;
6096 else
6097 bytes += 8 * 4;
6098 return bytes + tail;
6099 }
e4dff765 6100
afd2ea23 6101 if (p->second.notoc_)
32f59844 6102 {
afd2ea23
AM
6103 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6104 uint64_t off = plt_addr - from;
6105 if (off + 0x8000 < 0x10000)
6106 bytes += 7 * 4;
6107 else if (off + 0x80008000ULL < 0x100000000ULL)
6108 bytes += 8 * 4;
6109 else
6110 {
6111 bytes += 8 * 4;
6112 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6113 && ((off >> 32) & 0xffff) != 0)
6114 bytes += 4;
6115 if (((off >> 32) & 0xffffffffULL) != 0)
6116 bytes += 4;
6117 if (hi(off) != 0)
6118 bytes += 4;
6119 if (l(off) != 0)
6120 bytes += 4;
6121 }
6122 return bytes + tail;
32f59844 6123 }
32f59844 6124
afd2ea23
AM
6125 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6126 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6127 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6128 got_addr += ppcobj->toc_base_offset();
6129 uint64_t off = plt_addr - got_addr;
6130 bytes += 3 * 4 + 4 * (ha(off) != 0);
6131 if (this->targ_->abiversion() < 2)
6132 {
6133 bool static_chain = parameters->options().plt_static_chain();
6134 bool thread_safe = this->targ_->plt_thread_safe();
6135 bytes += (4
6136 + 4 * static_chain
6137 + 8 * thread_safe
6138 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6139 }
6140 return bytes + tail;
32f59844 6141 }
32f59844
AM
6142}
6143
6144// Return long branch stub size.
6145
6146template<int size, bool big_endian>
6147unsigned int
6148Stub_table<size, big_endian>::branch_stub_size(
afd2ea23 6149 typename Branch_stub_entries::iterator p,
32f59844
AM
6150 bool* need_lt)
6151{
6152 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6153 if (size == 32)
6154 {
6155 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6156 return 4;
6157 if (parameters->options().output_is_position_independent())
6158 return 32;
6159 return 16;
6160 }
6161
6162 uint64_t off = p->first.dest_ - loc;
afd2ea23 6163 unsigned int bytes = 0;
32f59844
AM
6164 if (p->second.notoc_)
6165 {
7c1f4227 6166 if (this->targ_->power10_stubs())
e4dff765
AM
6167 {
6168 Address odd = loc & 4;
6169 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6170 bytes = odd + 12;
6171 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6172 bytes = odd + 16;
6173 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6174 bytes = 28;
6175 else
6176 bytes = 32;
6177 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6178 return bytes;
6179 p->second.tocoff_ = bytes;
6180 }
6181 else
6182 {
6183 off -= 8;
6184 if (off + 0x8000 < 0x10000)
6185 return 24;
6186 if (off + 0x80008000ULL < 0x100000000ULL)
6187 {
6188 if (off + 24 + (1 << 25) < 2 << 25)
6189 return 28;
6190 return 32;
6191 }
6192
6193 bytes = 32;
6194 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6195 && ((off >> 32) & 0xffff) != 0)
6196 bytes += 4;
6197 if (((off >> 32) & 0xffffffffULL) != 0)
6198 bytes += 4;
6199 if (hi(off) != 0)
6200 bytes += 4;
6201 if (l(off) != 0)
6202 bytes += 4;
6203 return bytes;
6204 }
32f59844
AM
6205 }
6206
fa40fbe4 6207 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
32f59844 6208 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6209 return bytes + 4;
6210 if (!this->targ_->power10_stubs()
6211 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
e4dff765 6212 *need_lt = true;
afd2ea23 6213 return bytes + 16;
32f59844
AM
6214}
6215
f073a3e8
AM
6216template<int size, bool big_endian>
6217void
6218Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6219{
6220 if (p.sym_)
6221 gold_error(_("linkage table error against `%s'"),
6222 p.sym_->demangled_name().c_str());
6223 else
6224 gold_error(_("linkage table error against `%s:[local %u]'"),
6225 p.object_->name().c_str(),
6226 p.locsym_);
6227}
6228
ec661b9d 6229// Write out plt and long branch stub code.
cf43a2fe
AM
6230
6231template<int size, bool big_endian>
6232void
ec661b9d 6233Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6234{
ec661b9d
AM
6235 if (this->plt_call_stubs_.empty()
6236 && this->long_branch_stubs_.empty())
6237 return;
6238
6239 const section_size_type start_off = this->offset();
6240 const section_size_type off = this->stub_offset();
42cacb20 6241 const section_size_type oview_size =
ec661b9d 6242 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6243 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6244 unsigned char* p;
42cacb20 6245
e4dff765 6246 if (size == 64
7c1f4227 6247 && this->targ_->power10_stubs())
e4dff765 6248 {
afd2ea23
AM
6249 const Output_data_got_powerpc<size, big_endian>* got
6250 = this->targ_->got_section();
6251 Address got_os_addr = got->output_section()->address();
6252
e4dff765
AM
6253 if (!this->plt_call_stubs_.empty())
6254 {
6255 // Write out plt call stubs.
6256 typename Plt_stub_entries::const_iterator cs;
6257 for (cs = this->plt_call_stubs_.begin();
6258 cs != this->plt_call_stubs_.end();
6259 ++cs)
6260 {
6261 p = oview + cs->second.off_;
e4dff765
AM
6262 const Output_data_plt_powerpc<size, big_endian>* plt;
6263 Address pltoff = this->plt_off(cs, &plt);
6264 Address plt_addr = pltoff + plt->address();
afd2ea23
AM
6265 if (this->targ_->power10_stubs_auto())
6266 {
6267 if (cs->second.notoc_)
6268 {
6269 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6270 this->build_tls_opt_head(&p, false);
6271 Address from = this->stub_address() + (p - oview);
6272 Address delta = plt_addr - from;
6273 p = build_power10_offset<big_endian>(p, delta, from & 4,
6274 true);
6275 write_insn<big_endian>(p, mtctr_12);
6276 p += 4;
6277 write_insn<big_endian>(p, bctr);
6278 p += 4;
6279 p = oview + this->plt_call_align(p - oview);
6280 }
6281 if (cs->second.toc_)
6282 {
6283 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6284 {
6285 bool save_lr
6286 = cs->second.r2save_ && !cs->second.localentry0_;
6287 this->build_tls_opt_head(&p, save_lr);
6288 }
6289 const Powerpc_relobj<size, big_endian>* ppcobj
6290 = static_cast<const Powerpc_relobj<size, big_endian>*>(
6291 cs->first.object_);
6292 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6293 Address off = plt_addr - got_addr;
6294
6295 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6296 this->plt_error(cs->first);
6297
6298 if (cs->second.r2save_)
6299 {
6300 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6301 p += 4;
6302 }
6303 if (ha(off) != 0)
6304 {
6305 write_insn<big_endian>(p, addis_12_2 + ha(off));
6306 p += 4;
6307 write_insn<big_endian>(p, ld_12_12 + l(off));
6308 p += 4;
6309 }
6310 else
6311 {
6312 write_insn<big_endian>(p, ld_12_2 + l(off));
6313 p += 4;
6314 }
6315 write_insn<big_endian>(p, mtctr_12);
6316 p += 4;
6317 if (cs->second.r2save_
6318 && !cs->second.localentry0_
6319 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6320 this->build_tls_opt_tail(p);
6321 else
6322 write_insn<big_endian>(p, bctr);
6323 }
6324 }
6325 else
6326 {
6327 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6328 {
6329 bool save_lr
6330 = cs->second.r2save_ && !cs->second.localentry0_;
6331 this->build_tls_opt_head(&p, save_lr);
6332 }
6333 if (cs->second.r2save_)
6334 {
6335 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6336 p += 4;
6337 }
6338 Address from = this->stub_address() + (p - oview);
6339 Address delta = plt_addr - from;
6340 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6341 write_insn<big_endian>(p, mtctr_12);
6342 p += 4;
6343 if (cs->second.r2save_
6344 && !cs->second.localentry0_
6345 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6346 this->build_tls_opt_tail(p);
6347 else
6348 write_insn<big_endian>(p, bctr);
6349 }
e4dff765
AM
6350 }
6351 }
6352
6353 // Write out long branch stubs.
6354 typename Branch_stub_entries::const_iterator bs;
6355 for (bs = this->long_branch_stubs_.begin();
6356 bs != this->long_branch_stubs_.end();
6357 ++bs)
6358 {
6359 if (bs->second.save_res_)
6360 continue;
6361 Address off = this->plt_size_ + bs->second.off_;
6362 p = oview + off;
6363 Address loc = this->stub_address() + off;
6364 Address delta = bs->first.dest_ - loc;
afd2ea23 6365 if (this->targ_->power10_stubs_auto())
e4dff765 6366 {
afd2ea23
AM
6367 if (bs->second.notoc_)
6368 {
6369 unsigned char* startp = p;
6370 p = build_power10_offset<big_endian>(p, delta,
6371 loc & 4, false);
6372 delta -= p - startp;
6373 startp = p;
6374 if (delta + (1 << 25) < 2 << 25)
6375 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6376 else
6377 {
6378 write_insn<big_endian>(p, mtctr_12);
6379 p += 4;
6380 write_insn<big_endian>(p, bctr);
6381 }
6382 p += 4;
6383 delta -= p - startp;
6384 }
6385 if (bs->second.toc_)
6386 {
fa40fbe4 6387 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6388 if (delta + (1 << 25) >= 2 << 25)
6389 {
6390 Address brlt_addr
6391 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6392 gold_assert(brlt_addr != invalid_address);
6393 brlt_addr += this->targ_->brlt_section()->address();
6394 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6395 Address brltoff = brlt_addr - got_addr;
6396 if (ha(brltoff) == 0)
6397 {
6398 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6399 p += 4;
6400 }
6401 else
6402 {
6403 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6404 p += 4;
6405 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6406 p += 4;
6407 }
6408 }
6409 if (delta + (1 << 25) < 2 << 25)
6410 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6411 else
6412 {
6413 write_insn<big_endian>(p, mtctr_12);
6414 p += 4;
6415 write_insn<big_endian>(p, bctr);
6416 }
6417 }
e4dff765 6418 }
e4dff765
AM
6419 else
6420 {
fa40fbe4
AM
6421 if (!bs->second.notoc_)
6422 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6423 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6424 {
6425 unsigned char* startp = p;
6426 p = build_power10_offset<big_endian>(p, delta,
6427 loc & 4, false);
6428 delta -= p - startp;
6429 }
6430 if (delta + (1 << 25) < 2 << 25)
6431 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6432 else
6433 {
6434 write_insn<big_endian>(p, mtctr_12);
6435 p += 4;
6436 write_insn<big_endian>(p, bctr);
6437 }
e4dff765
AM
6438 }
6439 }
6440 }
6441 else if (size == 64)
cf43a2fe 6442 {
ec661b9d
AM
6443 const Output_data_got_powerpc<size, big_endian>* got
6444 = this->targ_->got_section();
dd93cd0a 6445 Address got_os_addr = got->output_section()->address();
c9269dff 6446
32f59844
AM
6447 if (!this->plt_call_stubs_.empty()
6448 && this->targ_->abiversion() >= 2)
cf43a2fe 6449 {
32f59844
AM
6450 // Write out plt call stubs for ELFv2.
6451 typename Plt_stub_entries::const_iterator cs;
6452 for (cs = this->plt_call_stubs_.begin();
6453 cs != this->plt_call_stubs_.end();
6454 ++cs)
6455 {
6456 const Output_data_plt_powerpc<size, big_endian>* plt;
6457 Address pltoff = this->plt_off(cs, &plt);
6458 Address plt_addr = pltoff + plt->address();
6459
6460 p = oview + cs->second.off_;
afd2ea23
AM
6461 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6462 {
6463 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6464 this->build_tls_opt_head(&p, save_lr);
6465 }
32f59844
AM
6466 if (cs->second.r2save_)
6467 {
6468 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6469 p += 4;
6470 }
6471 if (cs->second.notoc_)
6472 {
6473 Address from = this->stub_address() + (p - oview) + 8;
6474 Address off = plt_addr - from;
6475 p = build_notoc_offset<big_endian>(p, off, true);
6476 }
6477 else
6478 {
6479 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6480 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6481 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6482 Address off = plt_addr - got_addr;
6483
6484 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6485 this->plt_error(cs->first);
6486
6487 if (ha(off) != 0)
6488 {
6489 write_insn<big_endian>(p, addis_12_2 + ha(off));
6490 p += 4;
6491 write_insn<big_endian>(p, ld_12_12 + l(off));
6492 p += 4;
6493 }
6494 else
6495 {
6496 write_insn<big_endian>(p, ld_12_2 + l(off));
6497 p += 4;
6498 }
6499 }
6500 write_insn<big_endian>(p, mtctr_12);
6501 p += 4;
afd2ea23
AM
6502 if (cs->second.r2save_
6503 && !cs->second.localentry0_
6504 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6505 this->build_tls_opt_tail(p);
6506 else
32f59844
AM
6507 write_insn<big_endian>(p, bctr);
6508 }
6509 }
6510 else if (!this->plt_call_stubs_.empty())
6511 {
6512 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6513 typename Plt_stub_entries::const_iterator cs;
6514 for (cs = this->plt_call_stubs_.begin();
6515 cs != this->plt_call_stubs_.end();
6516 ++cs)
e5d5f5ed 6517 {
08be3224
AM
6518 const Output_data_plt_powerpc<size, big_endian>* plt;
6519 Address pltoff = this->plt_off(cs, &plt);
6520 Address plt_addr = pltoff + plt->address();
ec661b9d
AM
6521 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6522 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6523 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 6524 Address off = plt_addr - got_addr;
ec661b9d 6525
32f59844
AM
6526 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6527 || cs->second.notoc_)
f073a3e8 6528 this->plt_error(cs->first);
ec661b9d 6529
32f59844
AM
6530 bool static_chain = parameters->options().plt_static_chain();
6531 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6532 bool use_fake_dep = false;
6533 Address cmp_branch_off = 0;
407aa07c 6534 if (thread_safe)
9e69ed50
AM
6535 {
6536 unsigned int pltindex
6537 = ((pltoff - this->targ_->first_plt_entry_offset())
6538 / this->targ_->plt_entry_size());
6539 Address glinkoff
9e390558 6540 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6541 + pltindex * 8);
6542 if (pltindex > 32768)
6543 glinkoff += (pltindex - 32768) * 4;
6544 Address to
6545 = this->targ_->glink_section()->address() + glinkoff;
6546 Address from
7e57d19e
AM
6547 = (this->stub_address() + cs->second.off_ + 20
6548 + 4 * cs->second.r2save_
9e69ed50
AM
6549 + 4 * (ha(off) != 0)
6550 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6551 + 4 * static_chain);
6552 cmp_branch_off = to - from;
6553 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6554 }
6555
bdab445c 6556 p = oview + cs->second.off_;
afd2ea23
AM
6557 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6558 {
6559 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6560 this->build_tls_opt_head(&p, save_lr);
6561 use_fake_dep = thread_safe;
6562 }
32f59844 6563 if (cs->second.r2save_)
34e0882b 6564 {
32f59844 6565 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6566 p += 4;
34e0882b 6567 }
9e69ed50 6568 if (ha(off) != 0)
ec661b9d 6569 {
32f59844
AM
6570 write_insn<big_endian>(p, addis_11_2 + ha(off));
6571 p += 4;
6572 write_insn<big_endian>(p, ld_12_11 + l(off));
6573 p += 4;
6574 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6575 {
32f59844 6576 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6577 p += 4;
32f59844 6578 off = 0;
397998fc 6579 }
32f59844
AM
6580 write_insn<big_endian>(p, mtctr_12);
6581 p += 4;
6582 if (use_fake_dep)
397998fc 6583 {
32f59844 6584 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6585 p += 4;
32f59844 6586 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6587 p += 4;
6588 }
32f59844 6589 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6590 p += 4;
32f59844 6591 if (static_chain)
9e69ed50 6592 {
32f59844 6593 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6594 p += 4;
9e69ed50 6595 }
ec661b9d
AM
6596 }
6597 else
6598 {
b4f7960d
AM
6599 write_insn<big_endian>(p, ld_12_2 + l(off));
6600 p += 4;
32f59844 6601 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6602 {
b4f7960d
AM
6603 write_insn<big_endian>(p, addi_2_2 + l(off));
6604 p += 4;
9e69ed50 6605 off = 0;
ec661b9d 6606 }
b4f7960d
AM
6607 write_insn<big_endian>(p, mtctr_12);
6608 p += 4;
32f59844 6609 if (use_fake_dep)
9e69ed50 6610 {
32f59844
AM
6611 write_insn<big_endian>(p, xor_11_12_12);
6612 p += 4;
6613 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6614 p += 4;
9e69ed50 6615 }
32f59844
AM
6616 if (static_chain)
6617 {
6618 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6619 p += 4;
6620 }
6621 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6622 p += 4;
34e0882b 6623 }
afd2ea23
AM
6624 if (cs->second.r2save_
6625 && !cs->second.localentry0_
6626 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6627 this->build_tls_opt_tail(p);
34e0882b 6628 else if (thread_safe && !use_fake_dep)
9e69ed50 6629 {
b4f7960d
AM
6630 write_insn<big_endian>(p, cmpldi_2_0);
6631 p += 4;
6632 write_insn<big_endian>(p, bnectr_p4);
6633 p += 4;
9e69ed50
AM
6634 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6635 }
6636 else
407aa07c 6637 write_insn<big_endian>(p, bctr);
e5d5f5ed 6638 }
ec661b9d
AM
6639 }
6640
6641 // Write out long branch stubs.
6642 typename Branch_stub_entries::const_iterator bs;
6643 for (bs = this->long_branch_stubs_.begin();
6644 bs != this->long_branch_stubs_.end();
6645 ++bs)
6646 {
32f59844 6647 if (bs->second.save_res_)
d49044c7 6648 continue;
32f59844
AM
6649 Address off = this->plt_size_ + bs->second.off_;
6650 p = oview + off;
6651 Address loc = this->stub_address() + off;
ec661b9d 6652 Address delta = bs->first.dest_ - loc;
fa40fbe4
AM
6653 if (!bs->second.notoc_)
6654 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
32f59844
AM
6655 if (bs->second.notoc_)
6656 {
6657 unsigned char* startp = p;
6658 p = build_notoc_offset<big_endian>(p, off, false);
6659 delta -= p - startp;
6660 }
6661 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6662 {
ec661b9d
AM
6663 Address brlt_addr
6664 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6665 gold_assert(brlt_addr != invalid_address);
6666 brlt_addr += this->targ_->brlt_section()->address();
6667 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6668 Address brltoff = brlt_addr - got_addr;
6669 if (ha(brltoff) == 0)
6670 {
32f59844
AM
6671 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6672 p += 4;
ec661b9d
AM
6673 }
6674 else
cf43a2fe 6675 {
32f59844
AM
6676 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6677 p += 4;
6678 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6679 p += 4;
cf43a2fe 6680 }
32f59844
AM
6681 }
6682 if (delta + (1 << 25) < 2 << 25)
6683 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6684 else
6685 {
6686 write_insn<big_endian>(p, mtctr_12);
6687 p += 4;
407aa07c 6688 write_insn<big_endian>(p, bctr);
cf43a2fe 6689 }
ec661b9d
AM
6690 }
6691 }
32f59844 6692 else // size == 32
ec661b9d
AM
6693 {
6694 if (!this->plt_call_stubs_.empty())
6695 {
ec661b9d
AM
6696 // The address of _GLOBAL_OFFSET_TABLE_.
6697 Address g_o_t = invalid_address;
6698
6699 // Write out plt call stubs.
6700 typename Plt_stub_entries::const_iterator cs;
6701 for (cs = this->plt_call_stubs_.begin();
6702 cs != this->plt_call_stubs_.end();
6703 ++cs)
cf43a2fe 6704 {
08be3224
AM
6705 const Output_data_plt_powerpc<size, big_endian>* plt;
6706 Address plt_addr = this->plt_off(cs, &plt);
6707 plt_addr += plt->address();
ec661b9d 6708
bdab445c 6709 p = oview + cs->second.off_;
afd2ea23
AM
6710 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6711 this->build_tls_opt_head(&p, false);
ec661b9d
AM
6712 if (parameters->options().output_is_position_independent())
6713 {
6714 Address got_addr;
6715 const Powerpc_relobj<size, big_endian>* ppcobj
6716 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6717 (cs->first.object_));
6718 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6719 {
6720 unsigned int got2 = ppcobj->got2_shndx();
6721 got_addr = ppcobj->get_output_section_offset(got2);
6722 gold_assert(got_addr != invalid_address);
6723 got_addr += (ppcobj->output_section(got2)->address()
6724 + cs->first.addend_);
6725 }
6726 else
6727 {
6728 if (g_o_t == invalid_address)
6729 {
6730 const Output_data_got_powerpc<size, big_endian>* got
6731 = this->targ_->got_section();
6732 g_o_t = got->address() + got->g_o_t();
6733 }
6734 got_addr = g_o_t;
6735 }
6736
9e69ed50
AM
6737 Address off = plt_addr - got_addr;
6738 if (ha(off) == 0)
9e390558 6739 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6740 else
6741 {
9e390558
AM
6742 write_insn<big_endian>(p, addis_11_30 + ha(off));
6743 p += 4;
6744 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6745 }
6746 }
6747 else
6748 {
9e390558
AM
6749 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6750 p += 4;
6751 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6752 }
9e390558
AM
6753 p += 4;
6754 write_insn<big_endian>(p, mtctr_11);
6755 p += 4;
407aa07c 6756 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6757 }
6758 }
6759
6760 // Write out long branch stubs.
6761 typename Branch_stub_entries::const_iterator bs;
6762 for (bs = this->long_branch_stubs_.begin();
6763 bs != this->long_branch_stubs_.end();
6764 ++bs)
6765 {
32f59844 6766 if (bs->second.save_res_)
d49044c7 6767 continue;
32f59844
AM
6768 Address off = this->plt_size_ + bs->second.off_;
6769 p = oview + off;
6770 Address loc = this->stub_address() + off;
ec661b9d
AM
6771 Address delta = bs->first.dest_ - loc;
6772 if (delta + (1 << 25) < 2 << 25)
6773 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6774 else if (!parameters->options().output_is_position_independent())
6775 {
9e390558
AM
6776 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6777 p += 4;
6778 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6779 }
6780 else
6781 {
6782 delta -= 8;
9e390558
AM
6783 write_insn<big_endian>(p, mflr_0);
6784 p += 4;
6785 write_insn<big_endian>(p, bcl_20_31);
6786 p += 4;
6787 write_insn<big_endian>(p, mflr_12);
6788 p += 4;
6789 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6790 p += 4;
6791 write_insn<big_endian>(p, addi_12_12 + l(delta));
6792 p += 4;
6793 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6794 }
9e390558
AM
6795 p += 4;
6796 write_insn<big_endian>(p, mtctr_12);
6797 p += 4;
407aa07c 6798 write_insn<big_endian>(p, bctr);
cf43a2fe 6799 }
ec661b9d 6800 }
d49044c7
AM
6801 if (this->need_save_res_)
6802 {
6803 p = oview + this->plt_size_ + this->branch_size_;
6804 memcpy (p, this->targ_->savres_section()->contents(),
6805 this->targ_->savres_section()->data_size());
6806 }
ec661b9d
AM
6807}
6808
6809// Write out .glink.
6810
6811template<int size, bool big_endian>
6812void
6813Output_data_glink<size, big_endian>::do_write(Output_file* of)
6814{
6815 const section_size_type off = this->offset();
6816 const section_size_type oview_size =
6817 convert_to_section_size_type(this->data_size());
6818 unsigned char* const oview = of->get_output_view(off, oview_size);
6819 unsigned char* p;
6820
6821 // The base address of the .plt section.
6822 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6823 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 6824
ec661b9d
AM
6825 if (size == 64)
6826 {
9055360d 6827 if (this->end_branch_table_ != 0)
b4f7960d 6828 {
9055360d
AM
6829 // Write pltresolve stub.
6830 p = oview;
6831 Address after_bcl = this->address() + 16;
6832 Address pltoff = plt_base - after_bcl;
6833
6834 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 6835
b4f7960d 6836 if (this->targ_->abiversion() < 2)
cf43a2fe 6837 {
9055360d
AM
6838 write_insn<big_endian>(p, mflr_12), p += 4;
6839 write_insn<big_endian>(p, bcl_20_31), p += 4;
6840 write_insn<big_endian>(p, mflr_11), p += 4;
6841 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6842 write_insn<big_endian>(p, mtlr_12), p += 4;
6843 write_insn<big_endian>(p, add_11_2_11), p += 4;
6844 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6845 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6846 write_insn<big_endian>(p, mtctr_12), p += 4;
6847 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6848 }
6849 else
6850 {
63e5eea2
AM
6851 if (this->targ_->has_localentry0())
6852 {
6853 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6854 }
9055360d
AM
6855 write_insn<big_endian>(p, mflr_0), p += 4;
6856 write_insn<big_endian>(p, bcl_20_31), p += 4;
6857 write_insn<big_endian>(p, mflr_11), p += 4;
9055360d 6858 write_insn<big_endian>(p, mtlr_0), p += 4;
63e5eea2
AM
6859 if (this->targ_->has_localentry0())
6860 {
6861 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
6862 }
6863 else
6864 {
6865 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
6866 }
9055360d 6867 write_insn<big_endian>(p, sub_12_12_11), p += 4;
63e5eea2
AM
6868 write_insn<big_endian>(p, add_11_0_11), p += 4;
6869 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
9055360d
AM
6870 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6871 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6872 write_insn<big_endian>(p, mtctr_12), p += 4;
6873 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6874 }
407aa07c 6875 write_insn<big_endian>(p, bctr), p += 4;
9e390558 6876 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
6877
6878 // Write lazy link call stubs.
6879 uint32_t indx = 0;
6880 while (p < oview + this->end_branch_table_)
6881 {
6882 if (this->targ_->abiversion() < 2)
b4f7960d 6883 {
9055360d
AM
6884 if (indx < 0x8000)
6885 {
6886 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6887 }
6888 else
6889 {
bbec1a5d 6890 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
6891 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6892 }
b4f7960d 6893 }
9055360d
AM
6894 uint32_t branch_off = 8 - (p - oview);
6895 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6896 indx++;
cf43a2fe 6897 }
9055360d
AM
6898 }
6899
6900 Address plt_base = this->targ_->plt_section()->address();
6901 Address iplt_base = invalid_address;
9e390558 6902 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
6903 Address global_entry_base = this->address() + global_entry_off;
6904 typename Global_entry_stub_entries::const_iterator ge;
6905 for (ge = this->global_entry_stubs_.begin();
6906 ge != this->global_entry_stubs_.end();
6907 ++ge)
6908 {
6909 p = oview + global_entry_off + ge->second;
6910 Address plt_addr = ge->first->plt_offset();
6911 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6912 && ge->first->can_use_relative_reloc(false))
6913 {
6914 if (iplt_base == invalid_address)
6915 iplt_base = this->targ_->iplt_section()->address();
6916 plt_addr += iplt_base;
6917 }
6918 else
6919 plt_addr += plt_base;
6920 Address my_addr = global_entry_base + ge->second;
6921 Address off = plt_addr - my_addr;
6922
6923 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 6924 gold_error(_("linkage table error against `%s'"),
9055360d
AM
6925 ge->first->demangled_name().c_str());
6926
6927 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6928 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6929 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 6930 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
6931 }
6932 }
6933 else
6934 {
ec661b9d
AM
6935 const Output_data_got_powerpc<size, big_endian>* got
6936 = this->targ_->got_section();
dd93cd0a
AM
6937 // The address of _GLOBAL_OFFSET_TABLE_.
6938 Address g_o_t = got->address() + got->g_o_t();
c9269dff 6939
cf43a2fe 6940 // Write out pltresolve branch table.
ec661b9d 6941 p = oview;
9e390558 6942 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 6943 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
6944 while (p < end_p - 8 * 4)
6945 write_insn<big_endian>(p, b + end_p - p), p += 4;
6946 while (p < end_p)
6947 write_insn<big_endian>(p, nop), p += 4;
42cacb20 6948
cf43a2fe 6949 // Write out pltresolve call stub.
9e390558 6950 end_p = oview + oview_size;
cf43a2fe 6951 if (parameters->options().output_is_position_independent())
42cacb20 6952 {
ec661b9d 6953 Address res0_off = 0;
dd93cd0a
AM
6954 Address after_bcl_off = the_end + 12;
6955 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 6956
9e390558
AM
6957 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6958 p += 4;
6959 write_insn<big_endian>(p, mflr_0);
6960 p += 4;
6961 write_insn<big_endian>(p, bcl_20_31);
6962 p += 4;
6963 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6964 p += 4;
6965 write_insn<big_endian>(p, mflr_12);
6966 p += 4;
6967 write_insn<big_endian>(p, mtlr_0);
6968 p += 4;
6969 write_insn<big_endian>(p, sub_11_11_12);
6970 p += 4;
cf43a2fe 6971
dd93cd0a 6972 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 6973
9e390558
AM
6974 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6975 p += 4;
cf43a2fe
AM
6976 if (ha(got_bcl) == ha(got_bcl + 4))
6977 {
9e390558
AM
6978 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6979 p += 4;
6980 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
6981 }
6982 else
6983 {
9e390558
AM
6984 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6985 p += 4;
6986 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 6987 }
9e390558
AM
6988 p += 4;
6989 write_insn<big_endian>(p, mtctr_0);
6990 p += 4;
6991 write_insn<big_endian>(p, add_0_11_11);
6992 p += 4;
6993 write_insn<big_endian>(p, add_11_0_11);
42cacb20 6994 }
cf43a2fe 6995 else
42cacb20 6996 {
ec661b9d 6997 Address res0 = this->address();
cf43a2fe 6998
9e390558
AM
6999 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7000 p += 4;
7001 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7002 p += 4;
cf43a2fe 7003 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7004 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 7005 else
9e390558
AM
7006 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7007 p += 4;
7008 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7009 p += 4;
7010 write_insn<big_endian>(p, mtctr_0);
7011 p += 4;
7012 write_insn<big_endian>(p, add_0_11_11);
7013 p += 4;
cf43a2fe 7014 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7015 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 7016 else
9e390558
AM
7017 write_insn<big_endian>(p, lwz_12_12 + 4);
7018 p += 4;
7019 write_insn<big_endian>(p, add_11_0_11);
7020 }
7021 p += 4;
407aa07c
AM
7022 write_insn<big_endian>(p, bctr);
7023 p += 4;
9e390558
AM
7024 while (p < end_p)
7025 {
7026 write_insn<big_endian>(p, nop);
7027 p += 4;
42cacb20
DE
7028 }
7029 }
7030
cf43a2fe
AM
7031 of->write_output_view(off, oview_size, oview);
7032}
7033
f3a0ed29
AM
7034
7035// A class to handle linker generated save/restore functions.
7036
7037template<int size, bool big_endian>
7038class Output_data_save_res : public Output_section_data_build
7039{
7040 public:
7041 Output_data_save_res(Symbol_table* symtab);
7042
d49044c7
AM
7043 const unsigned char*
7044 contents() const
7045 {
7046 return contents_;
7047 }
7048
f3a0ed29
AM
7049 protected:
7050 // Write to a map file.
7051 void
7052 do_print_to_mapfile(Mapfile* mapfile) const
7053 { mapfile->print_output_data(this, _("** save/restore")); }
7054
7055 void
7056 do_write(Output_file*);
7057
7058 private:
7059 // The maximum size of save/restore contents.
7060 static const unsigned int savres_max = 218*4;
7061
7062 void
7063 savres_define(Symbol_table* symtab,
7064 const char *name,
7065 unsigned int lo, unsigned int hi,
7066 unsigned char* write_ent(unsigned char*, int),
7067 unsigned char* write_tail(unsigned char*, int));
7068
7069 unsigned char *contents_;
7070};
7071
7072template<bool big_endian>
7073static unsigned char*
7074savegpr0(unsigned char* p, int r)
7075{
7076 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7077 write_insn<big_endian>(p, insn);
7078 return p + 4;
7079}
7080
7081template<bool big_endian>
7082static unsigned char*
7083savegpr0_tail(unsigned char* p, int r)
7084{
7085 p = savegpr0<big_endian>(p, r);
7086 uint32_t insn = std_0_1 + 16;
7087 write_insn<big_endian>(p, insn);
7088 p = p + 4;
7089 write_insn<big_endian>(p, blr);
7090 return p + 4;
7091}
7092
7093template<bool big_endian>
62fe925a 7094static unsigned char*
f3a0ed29
AM
7095restgpr0(unsigned char* p, int r)
7096{
7097 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7098 write_insn<big_endian>(p, insn);
7099 return p + 4;
7100}
7101
7102template<bool big_endian>
62fe925a 7103static unsigned char*
f3a0ed29
AM
7104restgpr0_tail(unsigned char* p, int r)
7105{
7106 uint32_t insn = ld_0_1 + 16;
7107 write_insn<big_endian>(p, insn);
7108 p = p + 4;
7109 p = restgpr0<big_endian>(p, r);
7110 write_insn<big_endian>(p, mtlr_0);
7111 p = p + 4;
7112 if (r == 29)
7113 {
7114 p = restgpr0<big_endian>(p, 30);
7115 p = restgpr0<big_endian>(p, 31);
7116 }
7117 write_insn<big_endian>(p, blr);
7118 return p + 4;
7119}
7120
7121template<bool big_endian>
62fe925a 7122static unsigned char*
f3a0ed29
AM
7123savegpr1(unsigned char* p, int r)
7124{
7125 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7126 write_insn<big_endian>(p, insn);
7127 return p + 4;
7128}
7129
7130template<bool big_endian>
62fe925a 7131static unsigned char*
f3a0ed29
AM
7132savegpr1_tail(unsigned char* p, int r)
7133{
7134 p = savegpr1<big_endian>(p, r);
7135 write_insn<big_endian>(p, blr);
7136 return p + 4;
7137}
7138
7139template<bool big_endian>
62fe925a 7140static unsigned char*
f3a0ed29
AM
7141restgpr1(unsigned char* p, int r)
7142{
7143 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7144 write_insn<big_endian>(p, insn);
7145 return p + 4;
7146}
7147
7148template<bool big_endian>
62fe925a 7149static unsigned char*
f3a0ed29
AM
7150restgpr1_tail(unsigned char* p, int r)
7151{
7152 p = restgpr1<big_endian>(p, r);
7153 write_insn<big_endian>(p, blr);
7154 return p + 4;
7155}
7156
7157template<bool big_endian>
62fe925a 7158static unsigned char*
f3a0ed29
AM
7159savefpr(unsigned char* p, int r)
7160{
7161 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7162 write_insn<big_endian>(p, insn);
7163 return p + 4;
7164}
7165
7166template<bool big_endian>
62fe925a 7167static unsigned char*
f3a0ed29
AM
7168savefpr0_tail(unsigned char* p, int r)
7169{
7170 p = savefpr<big_endian>(p, r);
7171 write_insn<big_endian>(p, std_0_1 + 16);
7172 p = p + 4;
7173 write_insn<big_endian>(p, blr);
7174 return p + 4;
7175}
7176
7177template<bool big_endian>
62fe925a 7178static unsigned char*
f3a0ed29
AM
7179restfpr(unsigned char* p, int r)
7180{
7181 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7182 write_insn<big_endian>(p, insn);
7183 return p + 4;
7184}
7185
7186template<bool big_endian>
62fe925a 7187static unsigned char*
f3a0ed29
AM
7188restfpr0_tail(unsigned char* p, int r)
7189{
7190 write_insn<big_endian>(p, ld_0_1 + 16);
7191 p = p + 4;
7192 p = restfpr<big_endian>(p, r);
7193 write_insn<big_endian>(p, mtlr_0);
7194 p = p + 4;
7195 if (r == 29)
7196 {
7197 p = restfpr<big_endian>(p, 30);
7198 p = restfpr<big_endian>(p, 31);
7199 }
7200 write_insn<big_endian>(p, blr);
7201 return p + 4;
7202}
7203
7204template<bool big_endian>
62fe925a 7205static unsigned char*
f3a0ed29
AM
7206savefpr1_tail(unsigned char* p, int r)
7207{
7208 p = savefpr<big_endian>(p, r);
7209 write_insn<big_endian>(p, blr);
7210 return p + 4;
7211}
7212
7213template<bool big_endian>
62fe925a 7214static unsigned char*
f3a0ed29
AM
7215restfpr1_tail(unsigned char* p, int r)
7216{
7217 p = restfpr<big_endian>(p, r);
7218 write_insn<big_endian>(p, blr);
7219 return p + 4;
7220}
7221
7222template<bool big_endian>
62fe925a 7223static unsigned char*
f3a0ed29
AM
7224savevr(unsigned char* p, int r)
7225{
7226 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7227 write_insn<big_endian>(p, insn);
7228 p = p + 4;
7229 insn = stvx_0_12_0 + (r << 21);
7230 write_insn<big_endian>(p, insn);
7231 return p + 4;
7232}
7233
7234template<bool big_endian>
62fe925a 7235static unsigned char*
f3a0ed29
AM
7236savevr_tail(unsigned char* p, int r)
7237{
7238 p = savevr<big_endian>(p, r);
7239 write_insn<big_endian>(p, blr);
7240 return p + 4;
7241}
7242
7243template<bool big_endian>
62fe925a 7244static unsigned char*
f3a0ed29
AM
7245restvr(unsigned char* p, int r)
7246{
7247 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7248 write_insn<big_endian>(p, insn);
7249 p = p + 4;
7250 insn = lvx_0_12_0 + (r << 21);
7251 write_insn<big_endian>(p, insn);
7252 return p + 4;
7253}
7254
7255template<bool big_endian>
62fe925a 7256static unsigned char*
f3a0ed29
AM
7257restvr_tail(unsigned char* p, int r)
7258{
7259 p = restvr<big_endian>(p, r);
7260 write_insn<big_endian>(p, blr);
7261 return p + 4;
7262}
7263
7264
7265template<int size, bool big_endian>
7266Output_data_save_res<size, big_endian>::Output_data_save_res(
7267 Symbol_table* symtab)
7268 : Output_section_data_build(4),
7269 contents_(NULL)
7270{
7271 this->savres_define(symtab,
7272 "_savegpr0_", 14, 31,
7273 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7274 this->savres_define(symtab,
7275 "_restgpr0_", 14, 29,
7276 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7277 this->savres_define(symtab,
7278 "_restgpr0_", 30, 31,
7279 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7280 this->savres_define(symtab,
7281 "_savegpr1_", 14, 31,
7282 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7283 this->savres_define(symtab,
7284 "_restgpr1_", 14, 31,
7285 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7286 this->savres_define(symtab,
7287 "_savefpr_", 14, 31,
7288 savefpr<big_endian>, savefpr0_tail<big_endian>);
7289 this->savres_define(symtab,
7290 "_restfpr_", 14, 29,
7291 restfpr<big_endian>, restfpr0_tail<big_endian>);
7292 this->savres_define(symtab,
7293 "_restfpr_", 30, 31,
7294 restfpr<big_endian>, restfpr0_tail<big_endian>);
7295 this->savres_define(symtab,
7296 "._savef", 14, 31,
7297 savefpr<big_endian>, savefpr1_tail<big_endian>);
7298 this->savres_define(symtab,
7299 "._restf", 14, 31,
7300 restfpr<big_endian>, restfpr1_tail<big_endian>);
7301 this->savres_define(symtab,
7302 "_savevr_", 20, 31,
7303 savevr<big_endian>, savevr_tail<big_endian>);
7304 this->savres_define(symtab,
7305 "_restvr_", 20, 31,
7306 restvr<big_endian>, restvr_tail<big_endian>);
7307}
7308
7309template<int size, bool big_endian>
7310void
7311Output_data_save_res<size, big_endian>::savres_define(
7312 Symbol_table* symtab,
7313 const char *name,
7314 unsigned int lo, unsigned int hi,
7315 unsigned char* write_ent(unsigned char*, int),
7316 unsigned char* write_tail(unsigned char*, int))
7317{
7318 size_t len = strlen(name);
7319 bool writing = false;
7320 char sym[16];
7321
7322 memcpy(sym, name, len);
7323 sym[len + 2] = 0;
7324
7325 for (unsigned int i = lo; i <= hi; i++)
7326 {
7327 sym[len + 0] = i / 10 + '0';
7328 sym[len + 1] = i % 10 + '0';
7329 Symbol* gsym = symtab->lookup(sym);
7330 bool refd = gsym != NULL && gsym->is_undefined();
7331 writing = writing || refd;
7332 if (writing)
7333 {
7334 if (this->contents_ == NULL)
7335 this->contents_ = new unsigned char[this->savres_max];
7336
ec661b9d 7337 section_size_type value = this->current_data_size();
f3a0ed29
AM
7338 unsigned char* p = this->contents_ + value;
7339 if (i != hi)
7340 p = write_ent(p, i);
7341 else
7342 p = write_tail(p, i);
ec661b9d 7343 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7344 this->set_current_data_size(cur_size);
7345 if (refd)
7346 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7347 this, value, cur_size - value,
7348 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7349 elfcpp::STV_HIDDEN, 0, false, false);
7350 }
7351 }
7352}
7353
7354// Write out save/restore.
7355
7356template<int size, bool big_endian>
7357void
7358Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7359{
ec661b9d 7360 const section_size_type off = this->offset();
f3a0ed29
AM
7361 const section_size_type oview_size =
7362 convert_to_section_size_type(this->data_size());
7363 unsigned char* const oview = of->get_output_view(off, oview_size);
7364 memcpy(oview, this->contents_, oview_size);
7365 of->write_output_view(off, oview_size, oview);
7366}
7367
7368
cf43a2fe 7369// Create the glink section.
42cacb20 7370
cf43a2fe
AM
7371template<int size, bool big_endian>
7372void
7373Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7374{
7375 if (this->glink_ == NULL)
7376 {
7377 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7378 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7379 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7380 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7381 this->glink_, ORDER_TEXT, false);
7382 }
42cacb20
DE
7383}
7384
7385// Create a PLT entry for a global symbol.
7386
7387template<int size, bool big_endian>
7388void
ec661b9d
AM
7389Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7390 Layout* layout,
7391 Symbol* gsym)
42cacb20 7392{
e5d5f5ed
AM
7393 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7394 && gsym->can_use_relative_reloc(false))
7395 {
7396 if (this->iplt_ == NULL)
40b469d7 7397 this->make_iplt_section(symtab, layout);
03e25981 7398 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7399 }
7400 else
7401 {
7402 if (this->plt_ == NULL)
40b469d7 7403 this->make_plt_section(symtab, layout);
03e25981 7404 this->plt_->add_entry(gsym);
e5d5f5ed 7405 }
e5d5f5ed 7406}
42cacb20 7407
2d7ad24e
AM
7408// Make a PLT entry for a local symbol.
7409
7410template<int size, bool big_endian>
7411void
7412Target_powerpc<size, big_endian>::make_local_plt_entry(
7413 Layout* layout,
7414 Sized_relobj_file<size, big_endian>* relobj,
7415 unsigned int r_sym)
7416{
7417 if (this->lplt_ == NULL)
7418 this->make_lplt_section(layout);
7419 this->lplt_->add_local_entry(relobj, r_sym);
7420}
7421
e5d5f5ed 7422// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7423
e5d5f5ed
AM
7424template<int size, bool big_endian>
7425void
7426Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7427 Symbol_table* symtab,
e5d5f5ed 7428 Layout* layout,
ec661b9d
AM
7429 Sized_relobj_file<size, big_endian>* relobj,
7430 unsigned int r_sym)
e5d5f5ed
AM
7431{
7432 if (this->iplt_ == NULL)
40b469d7 7433 this->make_iplt_section(symtab, layout);
03e25981 7434 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7435}
7436
0e70b911
CC
7437// Return the number of entries in the PLT.
7438
7439template<int size, bool big_endian>
7440unsigned int
7441Target_powerpc<size, big_endian>::plt_entry_count() const
7442{
7443 if (this->plt_ == NULL)
7444 return 0;
b3ccdeb5 7445 return this->plt_->entry_count();
0e70b911
CC
7446}
7447
dd93cd0a 7448// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7449
7450template<int size, bool big_endian>
7451unsigned int
dd93cd0a 7452Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7453 Symbol_table* symtab,
7454 Layout* layout,
7455 Sized_relobj_file<size, big_endian>* object)
42cacb20 7456{
dd93cd0a 7457 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7458 {
7459 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7460 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
7461 Output_data_got_powerpc<size, big_endian>* got
7462 = this->got_section(symtab, layout);
7463 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7464 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7465 got_offset, 0);
dd93cd0a 7466 this->tlsld_got_offset_ = got_offset;
42cacb20 7467 }
dd93cd0a 7468 return this->tlsld_got_offset_;
42cacb20
DE
7469}
7470
95a2c8d6
RS
7471// Get the Reference_flags for a particular relocation.
7472
7473template<int size, bool big_endian>
7474int
88b8e639
AM
7475Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7476 unsigned int r_type,
7477 const Target_powerpc* target)
95a2c8d6 7478{
88b8e639
AM
7479 int ref = 0;
7480
95a2c8d6
RS
7481 switch (r_type)
7482 {
7483 case elfcpp::R_POWERPC_NONE:
7484 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7485 case elfcpp::R_POWERPC_GNU_VTENTRY:
7486 case elfcpp::R_PPC64_TOC:
7487 // No symbol reference.
88b8e639 7488 break;
95a2c8d6 7489
dd93cd0a
AM
7490 case elfcpp::R_PPC64_ADDR64:
7491 case elfcpp::R_PPC64_UADDR64:
7492 case elfcpp::R_POWERPC_ADDR32:
7493 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 7494 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 7495 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
7496 case elfcpp::R_POWERPC_ADDR16_LO:
7497 case elfcpp::R_POWERPC_ADDR16_HI:
7498 case elfcpp::R_POWERPC_ADDR16_HA:
89c52ae3
AM
7499 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7500 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7501 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7502 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7503 case elfcpp::R_PPC64_D34:
7504 case elfcpp::R_PPC64_D34_LO:
7505 case elfcpp::R_PPC64_D34_HI30:
7506 case elfcpp::R_PPC64_D34_HA30:
7507 case elfcpp::R_PPC64_D28:
88b8e639
AM
7508 ref = Symbol::ABSOLUTE_REF;
7509 break;
95a2c8d6 7510
dd93cd0a
AM
7511 case elfcpp::R_POWERPC_ADDR24:
7512 case elfcpp::R_POWERPC_ADDR14:
7513 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7514 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7515 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7516 break;
dd93cd0a 7517
e5d5f5ed 7518 case elfcpp::R_PPC64_REL64:
dd93cd0a 7519 case elfcpp::R_POWERPC_REL32:
95a2c8d6 7520 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
7521 case elfcpp::R_POWERPC_REL16:
7522 case elfcpp::R_POWERPC_REL16_LO:
7523 case elfcpp::R_POWERPC_REL16_HI:
7524 case elfcpp::R_POWERPC_REL16_HA:
c432bbba
AM
7525 case elfcpp::R_PPC64_REL16_HIGH:
7526 case elfcpp::R_PPC64_REL16_HIGHA:
7527 case elfcpp::R_PPC64_REL16_HIGHER:
7528 case elfcpp::R_PPC64_REL16_HIGHERA:
7529 case elfcpp::R_PPC64_REL16_HIGHEST:
7530 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7531 case elfcpp::R_PPC64_PCREL34:
7532 case elfcpp::R_PPC64_REL16_HIGHER34:
7533 case elfcpp::R_PPC64_REL16_HIGHERA34:
7534 case elfcpp::R_PPC64_REL16_HIGHEST34:
7535 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7536 case elfcpp::R_PPC64_PCREL28:
88b8e639
AM
7537 ref = Symbol::RELATIVE_REF;
7538 break;
95a2c8d6 7539
32f59844
AM
7540 case elfcpp::R_PPC64_REL24_NOTOC:
7541 if (size == 32)
7542 break;
7543 // Fall through.
dd93cd0a 7544 case elfcpp::R_POWERPC_REL24:
95a2c8d6 7545 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
7546 case elfcpp::R_POWERPC_REL14:
7547 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7548 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
7549 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7550 break;
95a2c8d6
RS
7551
7552 case elfcpp::R_POWERPC_GOT16:
7553 case elfcpp::R_POWERPC_GOT16_LO:
7554 case elfcpp::R_POWERPC_GOT16_HI:
7555 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
7556 case elfcpp::R_PPC64_GOT16_DS:
7557 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7558 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7559 case elfcpp::R_PPC64_TOC16:
7560 case elfcpp::R_PPC64_TOC16_LO:
7561 case elfcpp::R_PPC64_TOC16_HI:
7562 case elfcpp::R_PPC64_TOC16_HA:
7563 case elfcpp::R_PPC64_TOC16_DS:
7564 case elfcpp::R_PPC64_TOC16_LO_DS:
08be3224
AM
7565 case elfcpp::R_POWERPC_PLT16_LO:
7566 case elfcpp::R_POWERPC_PLT16_HI:
7567 case elfcpp::R_POWERPC_PLT16_HA:
7568 case elfcpp::R_PPC64_PLT16_LO_DS:
e4dff765
AM
7569 case elfcpp::R_PPC64_PLT_PCREL34:
7570 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
32d849b3 7571 ref = Symbol::RELATIVE_REF;
88b8e639 7572 break;
95a2c8d6
RS
7573
7574 case elfcpp::R_POWERPC_GOT_TPREL16:
7575 case elfcpp::R_POWERPC_TLS:
89c52ae3
AM
7576 case elfcpp::R_PPC64_TLSGD:
7577 case elfcpp::R_PPC64_TLSLD:
7578 case elfcpp::R_PPC64_TPREL34:
7579 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
7580 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7581 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7582 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7583 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
88b8e639
AM
7584 ref = Symbol::TLS_REF;
7585 break;
95a2c8d6
RS
7586
7587 case elfcpp::R_POWERPC_COPY:
7588 case elfcpp::R_POWERPC_GLOB_DAT:
7589 case elfcpp::R_POWERPC_JMP_SLOT:
7590 case elfcpp::R_POWERPC_RELATIVE:
7591 case elfcpp::R_POWERPC_DTPMOD:
7592 default:
7593 // Not expected. We will give an error later.
88b8e639 7594 break;
95a2c8d6 7595 }
88b8e639
AM
7596
7597 if (size == 64 && target->abiversion() < 2)
7598 ref |= Symbol::FUNC_DESC_ABI;
7599 return ref;
95a2c8d6
RS
7600}
7601
42cacb20
DE
7602// Report an unsupported relocation against a local symbol.
7603
7604template<int size, bool big_endian>
7605void
7606Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7607 Sized_relobj_file<size, big_endian>* object,
7608 unsigned int r_type)
42cacb20
DE
7609{
7610 gold_error(_("%s: unsupported reloc %u against local symbol"),
7611 object->name().c_str(), r_type);
7612}
7613
7614// We are about to emit a dynamic relocation of type R_TYPE. If the
7615// dynamic linker does not support it, issue an error.
7616
7617template<int size, bool big_endian>
7618void
7619Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7620 unsigned int r_type)
7621{
7622 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7623
7624 // These are the relocation types supported by glibc for both 32-bit
7625 // and 64-bit powerpc.
7626 switch (r_type)
7627 {
3ea0a085 7628 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7629 case elfcpp::R_POWERPC_RELATIVE:
7630 case elfcpp::R_POWERPC_GLOB_DAT:
7631 case elfcpp::R_POWERPC_DTPMOD:
7632 case elfcpp::R_POWERPC_DTPREL:
7633 case elfcpp::R_POWERPC_TPREL:
7634 case elfcpp::R_POWERPC_JMP_SLOT:
7635 case elfcpp::R_POWERPC_COPY:
3ea0a085 7636 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7637 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7638 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7639 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7640 case elfcpp::R_POWERPC_ADDR16:
7641 case elfcpp::R_POWERPC_UADDR16:
7642 case elfcpp::R_POWERPC_ADDR16_LO:
7643 case elfcpp::R_POWERPC_ADDR16_HI:
7644 case elfcpp::R_POWERPC_ADDR16_HA:
7645 case elfcpp::R_POWERPC_ADDR14:
7646 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7647 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7648 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7649 case elfcpp::R_POWERPC_TPREL16:
7650 case elfcpp::R_POWERPC_TPREL16_LO:
7651 case elfcpp::R_POWERPC_TPREL16_HI:
7652 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7653 return;
7654
7655 default:
7656 break;
7657 }
7658
7659 if (size == 64)
7660 {
7661 switch (r_type)
7662 {
7663 // These are the relocation types supported only on 64-bit.
7664 case elfcpp::R_PPC64_ADDR64:
42cacb20 7665 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7666 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7667 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7668 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7669 case elfcpp::R_PPC64_ADDR16_HIGH:
7670 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7671 case elfcpp::R_PPC64_ADDR16_HIGHER:
7672 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7673 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7674 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7675 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7676 case elfcpp::R_POWERPC_ADDR30:
7677 case elfcpp::R_PPC64_TPREL16_DS:
7678 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7679 case elfcpp::R_PPC64_TPREL16_HIGH:
7680 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7681 case elfcpp::R_PPC64_TPREL16_HIGHER:
7682 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7683 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7684 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7685 return;
7686
7687 default:
7688 break;
7689 }
7690 }
7691 else
7692 {
7693 switch (r_type)
7694 {
7695 // These are the relocation types supported only on 32-bit.
3ea0a085 7696 // ??? glibc ld.so doesn't need to support these.
e59a1001 7697 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7698 case elfcpp::R_POWERPC_DTPREL16:
7699 case elfcpp::R_POWERPC_DTPREL16_LO:
7700 case elfcpp::R_POWERPC_DTPREL16_HI:
7701 case elfcpp::R_POWERPC_DTPREL16_HA:
7702 return;
42cacb20
DE
7703
7704 default:
7705 break;
7706 }
7707 }
7708
7709 // This prevents us from issuing more than one error per reloc
7710 // section. But we can still wind up issuing more than one
7711 // error per object file.
7712 if (this->issued_non_pic_error_)
7713 return;
33aea2fd 7714 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7715 object->error(_("requires unsupported dynamic reloc; "
7716 "recompile with -fPIC"));
7717 this->issued_non_pic_error_ = true;
7718 return;
7719}
7720
e5d5f5ed
AM
7721// Return whether we need to make a PLT entry for a relocation of the
7722// given type against a STT_GNU_IFUNC symbol.
7723
7724template<int size, bool big_endian>
7725bool
7726Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7727 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7728 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7729 unsigned int r_type,
7730 bool report_err)
e5d5f5ed 7731{
c9824451
AM
7732 // In non-pic code any reference will resolve to the plt call stub
7733 // for the ifunc symbol.
9055360d
AM
7734 if ((size == 32 || target->abiversion() >= 2)
7735 && !parameters->options().output_is_position_independent())
c9824451
AM
7736 return true;
7737
e5d5f5ed
AM
7738 switch (r_type)
7739 {
b3ccdeb5 7740 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7741 case elfcpp::R_POWERPC_ADDR32:
7742 case elfcpp::R_POWERPC_UADDR32:
7743 if (size == 32)
b3ccdeb5 7744 return false;
e5d5f5ed
AM
7745 break;
7746
7747 case elfcpp::R_PPC64_ADDR64:
7748 case elfcpp::R_PPC64_UADDR64:
7749 if (size == 64)
b3ccdeb5 7750 return false;
e5d5f5ed
AM
7751 break;
7752
b3ccdeb5 7753 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7754 case elfcpp::R_POWERPC_GOT16:
7755 case elfcpp::R_POWERPC_GOT16_LO:
7756 case elfcpp::R_POWERPC_GOT16_HI:
7757 case elfcpp::R_POWERPC_GOT16_HA:
7758 case elfcpp::R_PPC64_GOT16_DS:
7759 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7760 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7761 return false;
e5d5f5ed 7762
08be3224
AM
7763 // PLT relocs are OK and need a PLT entry.
7764 case elfcpp::R_POWERPC_PLT16_LO:
7765 case elfcpp::R_POWERPC_PLT16_HI:
7766 case elfcpp::R_POWERPC_PLT16_HA:
7767 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
7768 case elfcpp::R_POWERPC_PLTSEQ:
7769 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7770 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7771 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7772 case elfcpp::R_PPC64_PLT_PCREL34:
7773 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
7774 return true;
7775 break;
7776
b3ccdeb5 7777 // Function calls are good, and these do need a PLT entry.
32f59844
AM
7778 case elfcpp::R_PPC64_REL24_NOTOC:
7779 if (size == 32)
7780 break;
7781 // Fall through.
e5d5f5ed
AM
7782 case elfcpp::R_POWERPC_ADDR24:
7783 case elfcpp::R_POWERPC_ADDR14:
7784 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7785 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7786 case elfcpp::R_POWERPC_REL24:
7787 case elfcpp::R_PPC_PLTREL24:
7788 case elfcpp::R_POWERPC_REL14:
7789 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7790 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7791 return true;
7792
7793 default:
7794 break;
7795 }
7796
7797 // Anything else is a problem.
7798 // If we are building a static executable, the libc startup function
7799 // responsible for applying indirect function relocations is going
7800 // to complain about the reloc type.
7801 // If we are building a dynamic executable, we will have a text
7802 // relocation. The dynamic loader will set the text segment
7803 // writable and non-executable to apply text relocations. So we'll
7804 // segfault when trying to run the indirection function to resolve
7805 // the reloc.
b3ccdeb5
AM
7806 if (report_err)
7807 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
7808 object->name().c_str(), r_type);
7809 return false;
7810}
7811
5edad15d
AM
7812// Return TRUE iff INSN is one we expect on a _LO variety toc/got
7813// reloc.
7814
7815static bool
7816ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7817{
7818 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7819 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7820 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7821 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7822 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7823 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7824 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7825 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7826 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7827 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7828 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7829 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7830 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7831 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7832 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7833 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7834 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7835 /* Exclude lfqu by testing reloc. If relocs are ever
7836 defined for the reduced D field in psq_lu then those
7837 will need testing too. */
7838 && r_type != elfcpp::R_PPC64_TOC16_LO
7839 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7840 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7841 && (insn & 1) == 0)
7842 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7843 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7844 /* Exclude stfqu. psq_stu as above for psq_lu. */
7845 && r_type != elfcpp::R_PPC64_TOC16_LO
7846 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7847 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7848 && (insn & 1) == 0));
7849}
7850
42cacb20
DE
7851// Scan a relocation for a local symbol.
7852
7853template<int size, bool big_endian>
7854inline void
7855Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
7856 Symbol_table* symtab,
7857 Layout* layout,
7858 Target_powerpc<size, big_endian>* target,
7859 Sized_relobj_file<size, big_endian>* object,
7860 unsigned int data_shndx,
7861 Output_section* output_section,
7862 const elfcpp::Rela<size, big_endian>& reloc,
7863 unsigned int r_type,
e5d5f5ed 7864 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 7865 bool is_discarded)
42cacb20 7866{
34e0882b 7867 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
7868
7869 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7870 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7871 {
7872 this->expect_tls_get_addr_call();
7873 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7874 if (tls_type != tls::TLSOPT_NONE)
7875 this->skip_next_tls_get_addr_call();
7876 }
7877 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7878 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7879 {
7880 this->expect_tls_get_addr_call();
7881 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7882 if (tls_type != tls::TLSOPT_NONE)
7883 this->skip_next_tls_get_addr_call();
7884 }
7885
dd93cd0a
AM
7886 Powerpc_relobj<size, big_endian>* ppc_object
7887 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7888
bfdfa4cd
AM
7889 if (is_discarded)
7890 {
7891 if (size == 64
7892 && data_shndx == ppc_object->opd_shndx()
7893 && r_type == elfcpp::R_PPC64_ADDR64)
7894 ppc_object->set_opd_discard(reloc.get_r_offset());
7895 return;
7896 }
7897
e5d5f5ed
AM
7898 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7899 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 7900 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 7901 {
ec661b9d
AM
7902 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7903 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7904 r_type, r_sym, reloc.get_r_addend());
7905 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 7906 }
e5d5f5ed 7907
42cacb20
DE
7908 switch (r_type)
7909 {
7910 case elfcpp::R_POWERPC_NONE:
7911 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7912 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 7913 case elfcpp::R_POWERPC_TLS:
549dba71 7914 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
7915 case elfcpp::R_POWERPC_PLTSEQ:
7916 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7917 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7918 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7919 case elfcpp::R_PPC64_PCREL_OPT:
7920 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7921 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7922 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7923 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7924 case elfcpp::R_PPC64_REL16_HIGHER34:
7925 case elfcpp::R_PPC64_REL16_HIGHERA34:
7926 case elfcpp::R_PPC64_REL16_HIGHEST34:
7927 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
7928 case elfcpp::R_PPC64_D34:
7929 case elfcpp::R_PPC64_D34_LO:
7930 case elfcpp::R_PPC64_D34_HI30:
7931 case elfcpp::R_PPC64_D34_HA30:
7932 case elfcpp::R_PPC64_D28:
7933 case elfcpp::R_PPC64_PCREL34:
7934 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
7935 case elfcpp::R_PPC64_TPREL34:
7936 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
7937 break;
7938
7939 case elfcpp::R_PPC64_TOC:
7940 {
7941 Output_data_got_powerpc<size, big_endian>* got
7942 = target->got_section(symtab, layout);
7943 if (parameters->options().output_is_position_independent())
7944 {
bfdfa4cd
AM
7945 Address off = reloc.get_r_offset();
7946 if (size == 64
9055360d 7947 && target->abiversion() < 2
bfdfa4cd
AM
7948 && data_shndx == ppc_object->opd_shndx()
7949 && ppc_object->get_opd_discard(off - 8))
7950 break;
7951
dd93cd0a 7952 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 7953 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
7954 rela_dyn->add_output_section_relative(got->output_section(),
7955 elfcpp::R_POWERPC_RELATIVE,
7956 output_section,
bfdfa4cd
AM
7957 object, data_shndx, off,
7958 symobj->toc_base_offset());
dd93cd0a
AM
7959 }
7960 }
42cacb20
DE
7961 break;
7962
7963 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 7964 case elfcpp::R_PPC64_UADDR64:
42cacb20 7965 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
7966 case elfcpp::R_POWERPC_UADDR32:
7967 case elfcpp::R_POWERPC_ADDR24:
c9269dff 7968 case elfcpp::R_POWERPC_ADDR16:
42cacb20 7969 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
7970 case elfcpp::R_POWERPC_ADDR16_HI:
7971 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 7972 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
7973 case elfcpp::R_PPC64_ADDR16_HIGH:
7974 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
7975 case elfcpp::R_PPC64_ADDR16_HIGHER:
7976 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7977 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7978 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7979 case elfcpp::R_PPC64_ADDR16_DS:
7980 case elfcpp::R_PPC64_ADDR16_LO_DS:
7981 case elfcpp::R_POWERPC_ADDR14:
7982 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7983 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
7984 // If building a shared library (or a position-independent
7985 // executable), we need to create a dynamic relocation for
7986 // this location.
c9824451 7987 if (parameters->options().output_is_position_independent()
9055360d 7988 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 7989 {
b3ccdeb5
AM
7990 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7991 is_ifunc);
1f98a074 7992 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
7993 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7994 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 7995 {
b3ccdeb5
AM
7996 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7997 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7998 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
7999 output_section, data_shndx,
8000 reloc.get_r_offset(),
c9824451 8001 reloc.get_r_addend(), false);
2e702c99 8002 }
1f98a074 8003 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 8004 {
dd93cd0a 8005 check_non_pic(object, r_type);
dd93cd0a
AM
8006 rela_dyn->add_local(object, r_sym, r_type, output_section,
8007 data_shndx, reloc.get_r_offset(),
8008 reloc.get_r_addend());
2e702c99 8009 }
1f98a074
AM
8010 else
8011 {
8012 gold_assert(lsym.get_st_value() == 0);
8013 unsigned int shndx = lsym.get_st_shndx();
8014 bool is_ordinary;
8015 shndx = object->adjust_sym_shndx(r_sym, shndx,
8016 &is_ordinary);
8017 if (!is_ordinary)
8018 object->error(_("section symbol %u has bad shndx %u"),
8019 r_sym, shndx);
8020 else
8021 rela_dyn->add_local_section(object, shndx, r_type,
8022 output_section, data_shndx,
8023 reloc.get_r_offset());
8024 }
2e702c99 8025 }
42cacb20
DE
8026 break;
8027
e4dff765
AM
8028 case elfcpp::R_PPC64_PLT_PCREL34:
8029 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
8030 case elfcpp::R_POWERPC_PLT16_LO:
8031 case elfcpp::R_POWERPC_PLT16_HI:
8032 case elfcpp::R_POWERPC_PLT16_HA:
8033 case elfcpp::R_PPC64_PLT16_LO_DS:
8034 if (!is_ifunc)
8035 {
8036 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8037 target->make_local_plt_entry(layout, object, r_sym);
8038 }
8039 break;
8040
32f59844
AM
8041 case elfcpp::R_PPC64_REL24_NOTOC:
8042 if (size == 32)
8043 break;
8044 // Fall through.
42cacb20 8045 case elfcpp::R_POWERPC_REL24:
c9824451 8046 case elfcpp::R_PPC_PLTREL24:
42cacb20 8047 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
8048 case elfcpp::R_POWERPC_REL14:
8049 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8050 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8051 if (!is_ifunc)
0e123f69
AM
8052 {
8053 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8054 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8055 r_type, r_sym, reloc.get_r_addend());
8056 }
ec661b9d
AM
8057 break;
8058
7e57d19e
AM
8059 case elfcpp::R_PPC64_TOCSAVE:
8060 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8061 // caller has already saved r2 and thus a plt call stub need not
8062 // save r2.
8063 if (size == 64
8064 && target->mark_pltcall(ppc_object, data_shndx,
8065 reloc.get_r_offset() - 4, symtab))
8066 {
8067 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8068 unsigned int shndx = lsym.get_st_shndx();
8069 bool is_ordinary;
8070 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8071 if (!is_ordinary)
8072 object->error(_("tocsave symbol %u has bad shndx %u"),
8073 r_sym, shndx);
8074 else
8075 target->add_tocsave(ppc_object, shndx,
8076 lsym.get_st_value() + reloc.get_r_addend());
8077 }
8078 break;
8079
ec661b9d
AM
8080 case elfcpp::R_PPC64_REL64:
8081 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8082 case elfcpp::R_POWERPC_REL16:
6ce78956 8083 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 8084 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 8085 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8086 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8087 case elfcpp::R_PPC64_REL16_HIGH:
8088 case elfcpp::R_PPC64_REL16_HIGHA:
8089 case elfcpp::R_PPC64_REL16_HIGHER:
8090 case elfcpp::R_PPC64_REL16_HIGHERA:
8091 case elfcpp::R_PPC64_REL16_HIGHEST:
8092 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8093 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8094 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8095 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8096 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8097 case elfcpp::R_PPC64_SECTOFF_DS:
8098 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8099 case elfcpp::R_POWERPC_TPREL16:
8100 case elfcpp::R_POWERPC_TPREL16_LO:
8101 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8102 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8103 case elfcpp::R_PPC64_TPREL16_DS:
8104 case elfcpp::R_PPC64_TPREL16_LO_DS:
8105 case elfcpp::R_PPC64_TPREL16_HIGH:
8106 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8107 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8108 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8109 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8110 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8111 case elfcpp::R_POWERPC_DTPREL16:
8112 case elfcpp::R_POWERPC_DTPREL16_LO:
8113 case elfcpp::R_POWERPC_DTPREL16_HI:
8114 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8115 case elfcpp::R_PPC64_DTPREL16_DS:
8116 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8117 case elfcpp::R_PPC64_DTPREL16_HIGH:
8118 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8119 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8120 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8121 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8122 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8123 case elfcpp::R_PPC64_TLSGD:
8124 case elfcpp::R_PPC64_TLSLD:
45965137 8125 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
8126 break;
8127
e4dff765 8128 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8129 case elfcpp::R_POWERPC_GOT16:
8130 case elfcpp::R_POWERPC_GOT16_LO:
8131 case elfcpp::R_POWERPC_GOT16_HI:
8132 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8133 case elfcpp::R_PPC64_GOT16_DS:
8134 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8135 {
c9269dff 8136 // The symbol requires a GOT entry.
dd93cd0a
AM
8137 Output_data_got_powerpc<size, big_endian>* got
8138 = target->got_section(symtab, layout);
8139 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 8140
e5d5f5ed 8141 if (!parameters->options().output_is_position_independent())
42cacb20 8142 {
b01a4b04
AM
8143 if (is_ifunc
8144 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8145 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
8146 else
8147 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
8148 }
8149 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
8150 {
8151 // If we are generating a shared object or a pie, this
8152 // symbol's GOT entry will be set by a dynamic relocation.
8153 unsigned int off;
8154 off = got->add_constant(0);
8155 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 8156
b3ccdeb5
AM
8157 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8158 is_ifunc);
8159 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8160 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8161 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 8162 got, off, 0, false);
2e702c99 8163 }
42cacb20
DE
8164 }
8165 break;
8166
cf43a2fe
AM
8167 case elfcpp::R_PPC64_TOC16:
8168 case elfcpp::R_PPC64_TOC16_LO:
8169 case elfcpp::R_PPC64_TOC16_HI:
8170 case elfcpp::R_PPC64_TOC16_HA:
8171 case elfcpp::R_PPC64_TOC16_DS:
8172 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8173 // We need a GOT section.
8174 target->got_section(symtab, layout);
8175 break;
8176
87c69f97 8177 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8178 case elfcpp::R_POWERPC_GOT_TLSGD16:
8179 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8180 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8181 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8182 {
8183 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8184 if (tls_type == tls::TLSOPT_NONE)
8185 {
8186 Output_data_got_powerpc<size, big_endian>* got
8187 = target->got_section(symtab, layout);
8188 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
8189 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8190 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
8191 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
8192 }
8193 else if (tls_type == tls::TLSOPT_TO_LE)
8194 {
8195 // no GOT relocs needed for Local Exec.
8196 }
8197 else
8198 gold_unreachable();
8199 }
42cacb20
DE
8200 break;
8201
87c69f97 8202 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8203 case elfcpp::R_POWERPC_GOT_TLSLD16:
8204 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8205 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8206 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8207 {
8208 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8209 if (tls_type == tls::TLSOPT_NONE)
8210 target->tlsld_got_offset(symtab, layout, object);
8211 else if (tls_type == tls::TLSOPT_TO_LE)
8212 {
8213 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8214 if (parameters->options().emit_relocs())
8215 {
8216 Output_section* os = layout->tls_segment()->first_section();
8217 gold_assert(os != NULL);
8218 os->set_needs_symtab_index();
8219 }
dd93cd0a
AM
8220 }
8221 else
8222 gold_unreachable();
8223 }
42cacb20 8224 break;
42cacb20 8225
87c69f97 8226 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
8227 case elfcpp::R_POWERPC_GOT_DTPREL16:
8228 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8229 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8230 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8231 {
8232 Output_data_got_powerpc<size, big_endian>* got
8233 = target->got_section(symtab, layout);
8234 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 8235 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
8236 }
8237 break;
42cacb20 8238
87c69f97 8239 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
8240 case elfcpp::R_POWERPC_GOT_TPREL16:
8241 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8242 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8243 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8244 {
8245 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8246 if (tls_type == tls::TLSOPT_NONE)
8247 {
dd93cd0a 8248 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
8249 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
8250 {
8251 Output_data_got_powerpc<size, big_endian>* got
8252 = target->got_section(symtab, layout);
8253 unsigned int off = got->add_constant(0);
8254 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
8255
8256 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8257 rela_dyn->add_symbolless_local_addend(object, r_sym,
8258 elfcpp::R_POWERPC_TPREL,
8259 got, off, 0);
8260 }
dd93cd0a
AM
8261 }
8262 else if (tls_type == tls::TLSOPT_TO_LE)
8263 {
8264 // no GOT relocs needed for Local Exec.
8265 }
8266 else
8267 gold_unreachable();
8268 }
8269 break;
8270
8271 default:
8272 unsupported_reloc_local(object, r_type);
8273 break;
8274 }
d8f5a274 8275
5edad15d
AM
8276 if (size == 64
8277 && parameters->options().toc_optimize())
8278 {
8279 if (data_shndx == ppc_object->toc_shndx())
8280 {
8281 bool ok = true;
8282 if (r_type != elfcpp::R_PPC64_ADDR64
8283 || (is_ifunc && target->abiversion() < 2))
8284 ok = false;
8285 else if (parameters->options().output_is_position_independent())
8286 {
8287 if (is_ifunc)
8288 ok = false;
8289 else
8290 {
8291 unsigned int shndx = lsym.get_st_shndx();
8292 if (shndx >= elfcpp::SHN_LORESERVE
8293 && shndx != elfcpp::SHN_XINDEX)
8294 ok = false;
8295 }
8296 }
8297 if (!ok)
8298 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8299 }
8300
8301 enum {no_check, check_lo, check_ha} insn_check;
8302 switch (r_type)
8303 {
8304 default:
8305 insn_check = no_check;
8306 break;
8307
8308 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8309 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8310 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8311 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8312 case elfcpp::R_POWERPC_GOT16_HA:
8313 case elfcpp::R_PPC64_TOC16_HA:
8314 insn_check = check_ha;
8315 break;
8316
8317 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8318 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8319 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8320 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8321 case elfcpp::R_POWERPC_GOT16_LO:
8322 case elfcpp::R_PPC64_GOT16_LO_DS:
8323 case elfcpp::R_PPC64_TOC16_LO:
8324 case elfcpp::R_PPC64_TOC16_LO_DS:
8325 insn_check = check_lo;
8326 break;
8327 }
8328
8329 section_size_type slen;
8330 const unsigned char* view = NULL;
8331 if (insn_check != no_check)
8332 {
8333 view = ppc_object->section_contents(data_shndx, &slen, false);
8334 section_size_type off =
8335 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8336 if (off < slen)
8337 {
8338 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8339 if (insn_check == check_lo
8340 ? !ok_lo_toc_insn(insn, r_type)
8341 : ((insn & ((0x3f << 26) | 0x1f << 16))
8342 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8343 {
8344 ppc_object->set_no_toc_opt();
8345 gold_warning(_("%s: toc optimization is not supported "
8346 "for %#08x instruction"),
8347 ppc_object->name().c_str(), insn);
8348 }
8349 }
8350 }
8351
8352 switch (r_type)
8353 {
8354 default:
8355 break;
8356 case elfcpp::R_PPC64_TOC16:
8357 case elfcpp::R_PPC64_TOC16_LO:
8358 case elfcpp::R_PPC64_TOC16_HI:
8359 case elfcpp::R_PPC64_TOC16_HA:
8360 case elfcpp::R_PPC64_TOC16_DS:
8361 case elfcpp::R_PPC64_TOC16_LO_DS:
8362 unsigned int shndx = lsym.get_st_shndx();
8363 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8364 bool is_ordinary;
8365 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8366 if (is_ordinary && shndx == ppc_object->toc_shndx())
8367 {
412294da 8368 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8369 if (dst_off < ppc_object->section_size(shndx))
8370 {
8371 bool ok = false;
8372 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8373 ok = true;
8374 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8375 {
8376 // Need to check that the insn is a ld
8377 if (!view)
8378 view = ppc_object->section_contents(data_shndx,
8379 &slen,
8380 false);
8381 section_size_type off =
8382 (convert_to_section_size_type(reloc.get_r_offset())
8383 + (big_endian ? -2 : 3));
8384 if (off < slen
8385 && (view[off] & (0x3f << 2)) == 58u << 2)
8386 ok = true;
8387 }
8388 if (!ok)
8389 ppc_object->set_no_toc_opt(dst_off);
8390 }
8391 }
8392 break;
8393 }
8394 }
8395
f159cdb6
AM
8396 if (size == 32)
8397 {
8398 switch (r_type)
8399 {
8400 case elfcpp::R_POWERPC_REL32:
8401 if (ppc_object->got2_shndx() != 0
8402 && parameters->options().output_is_position_independent())
8403 {
8404 unsigned int shndx = lsym.get_st_shndx();
8405 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8406 bool is_ordinary;
8407 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8408 if (is_ordinary && shndx == ppc_object->got2_shndx()
8409 && (ppc_object->section_flags(data_shndx)
8410 & elfcpp::SHF_EXECINSTR) != 0)
8411 gold_error(_("%s: unsupported -mbss-plt code"),
8412 ppc_object->name().c_str());
8413 }
8414 break;
8415 default:
8416 break;
8417 }
8418 }
8419
d8f5a274
AM
8420 switch (r_type)
8421 {
8422 case elfcpp::R_POWERPC_GOT_TLSLD16:
8423 case elfcpp::R_POWERPC_GOT_TLSGD16:
8424 case elfcpp::R_POWERPC_GOT_TPREL16:
8425 case elfcpp::R_POWERPC_GOT_DTPREL16:
8426 case elfcpp::R_POWERPC_GOT16:
8427 case elfcpp::R_PPC64_GOT16_DS:
8428 case elfcpp::R_PPC64_TOC16:
8429 case elfcpp::R_PPC64_TOC16_DS:
8430 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8431 break;
8432 default:
8433 break;
8434 }
8435
8436 switch (r_type)
8437 {
89c52ae3
AM
8438 case elfcpp::R_PPC64_TPREL16_DS:
8439 case elfcpp::R_PPC64_TPREL16_LO_DS:
8440 case elfcpp::R_PPC64_TPREL16_HIGH:
8441 case elfcpp::R_PPC64_TPREL16_HIGHA:
8442 case elfcpp::R_PPC64_TPREL16_HIGHER:
8443 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8444 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8445 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8446 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
8447 if (size != 64)
8448 break;
8449 // Fall through.
8450 case elfcpp::R_POWERPC_TPREL16:
8451 case elfcpp::R_POWERPC_TPREL16_LO:
8452 case elfcpp::R_POWERPC_TPREL16_HI:
8453 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8454 layout->set_has_static_tls();
8455 break;
8456 default:
8457 break;
8458 }
8459
252dcdf4
AM
8460 switch (r_type)
8461 {
8462 case elfcpp::R_POWERPC_TPREL16_HA:
8463 if (target->tprel_opt())
8464 {
8465 section_size_type slen;
8466 const unsigned char* view = NULL;
8467 view = ppc_object->section_contents(data_shndx, &slen, false);
8468 section_size_type off
8469 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8470 if (off < slen)
8471 {
8472 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8473 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8474 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 8475 target->set_no_tprel_opt();
252dcdf4
AM
8476 }
8477 }
8478 break;
8479
8480 case elfcpp::R_PPC64_TPREL16_HIGH:
8481 case elfcpp::R_PPC64_TPREL16_HIGHA:
8482 case elfcpp::R_PPC64_TPREL16_HIGHER:
8483 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8484 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8485 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8486 if (size != 64)
8487 break;
8488 // Fall through.
8489 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 8490 target->set_no_tprel_opt();
252dcdf4
AM
8491 break;
8492 default:
8493 break;
8494 }
8495
89c52ae3
AM
8496 switch (r_type)
8497 {
8498 case elfcpp::R_PPC64_D34:
8499 case elfcpp::R_PPC64_D34_LO:
8500 case elfcpp::R_PPC64_D34_HI30:
8501 case elfcpp::R_PPC64_D34_HA30:
8502 case elfcpp::R_PPC64_D28:
8503 case elfcpp::R_PPC64_PCREL34:
8504 case elfcpp::R_PPC64_PCREL28:
8505 case elfcpp::R_PPC64_TPREL34:
8506 case elfcpp::R_PPC64_DTPREL34:
8507 case elfcpp::R_PPC64_PLT_PCREL34:
8508 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8509 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
8510 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8511 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8512 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8513 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 8514 target->set_power10_relocs();
89c52ae3 8515 break;
d8f5a274
AM
8516 default:
8517 break;
8518 }
dd93cd0a
AM
8519}
8520
8521// Report an unsupported relocation against a global symbol.
8522
8523template<int size, bool big_endian>
8524void
8525Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8526 Sized_relobj_file<size, big_endian>* object,
8527 unsigned int r_type,
8528 Symbol* gsym)
8529{
42cacb20
DE
8530 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8531 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8532}
8533
8534// Scan a relocation for a global symbol.
8535
8536template<int size, bool big_endian>
8537inline void
8538Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8539 Symbol_table* symtab,
8540 Layout* layout,
8541 Target_powerpc<size, big_endian>* target,
8542 Sized_relobj_file<size, big_endian>* object,
8543 unsigned int data_shndx,
8544 Output_section* output_section,
8545 const elfcpp::Rela<size, big_endian>& reloc,
8546 unsigned int r_type,
8547 Symbol* gsym)
42cacb20 8548{
34e0882b
AM
8549 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8550 == Track_tls::SKIP)
e3deeb9c
AM
8551 return;
8552
34e0882b
AM
8553 if (target->replace_tls_get_addr(gsym))
8554 // Change a __tls_get_addr reference to __tls_get_addr_opt
8555 // so dynamic relocs are emitted against the latter symbol.
8556 gsym = target->tls_get_addr_opt();
8557
e3deeb9c
AM
8558 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8559 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8560 {
8561 this->expect_tls_get_addr_call();
8562 const bool final = gsym->final_value_is_known();
8563 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8564 if (tls_type != tls::TLSOPT_NONE)
8565 this->skip_next_tls_get_addr_call();
8566 }
8567 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8568 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8569 {
8570 this->expect_tls_get_addr_call();
8571 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8572 if (tls_type != tls::TLSOPT_NONE)
8573 this->skip_next_tls_get_addr_call();
8574 }
8575
dd93cd0a
AM
8576 Powerpc_relobj<size, big_endian>* ppc_object
8577 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8578
e5d5f5ed 8579 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8580 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8581 bool pushed_ifunc = false;
8582 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8583 {
0e123f69 8584 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8585 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8586 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8587 target->make_plt_entry(symtab, layout, gsym);
9055360d 8588 pushed_ifunc = true;
ec661b9d 8589 }
e5d5f5ed 8590
42cacb20
DE
8591 switch (r_type)
8592 {
8593 case elfcpp::R_POWERPC_NONE:
8594 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8595 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8596 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8597 case elfcpp::R_POWERPC_TLS:
549dba71 8598 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8599 case elfcpp::R_POWERPC_PLTSEQ:
8600 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8601 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8602 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8603 case elfcpp::R_PPC64_PCREL_OPT:
8604 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8605 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8606 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8607 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8608 case elfcpp::R_PPC64_REL16_HIGHER34:
8609 case elfcpp::R_PPC64_REL16_HIGHERA34:
8610 case elfcpp::R_PPC64_REL16_HIGHEST34:
8611 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8612 case elfcpp::R_PPC64_D34:
8613 case elfcpp::R_PPC64_D34_LO:
8614 case elfcpp::R_PPC64_D34_HI30:
8615 case elfcpp::R_PPC64_D34_HA30:
8616 case elfcpp::R_PPC64_D28:
8617 case elfcpp::R_PPC64_PCREL34:
8618 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8619 case elfcpp::R_PPC64_TPREL34:
8620 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8621 break;
8622
8623 case elfcpp::R_PPC64_TOC:
8624 {
8625 Output_data_got_powerpc<size, big_endian>* got
8626 = target->got_section(symtab, layout);
8627 if (parameters->options().output_is_position_independent())
8628 {
bfdfa4cd
AM
8629 Address off = reloc.get_r_offset();
8630 if (size == 64
8631 && data_shndx == ppc_object->opd_shndx()
8632 && ppc_object->get_opd_discard(off - 8))
8633 break;
8634
dd93cd0a
AM
8635 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8636 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8637 if (data_shndx != ppc_object->opd_shndx())
8638 symobj = static_cast
8639 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8640 rela_dyn->add_output_section_relative(got->output_section(),
8641 elfcpp::R_POWERPC_RELATIVE,
8642 output_section,
bfdfa4cd 8643 object, data_shndx, off,
dd93cd0a
AM
8644 symobj->toc_base_offset());
8645 }
8646 }
42cacb20
DE
8647 break;
8648
c9269dff 8649 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8650 if (size == 64
9055360d 8651 && target->abiversion() < 2
bfdfa4cd
AM
8652 && data_shndx == ppc_object->opd_shndx()
8653 && (gsym->is_defined_in_discarded_section()
8654 || gsym->object() != object))
8655 {
8656 ppc_object->set_opd_discard(reloc.get_r_offset());
8657 break;
8658 }
d8e90251 8659 // Fall through.
dd93cd0a 8660 case elfcpp::R_PPC64_UADDR64:
c9269dff 8661 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8662 case elfcpp::R_POWERPC_UADDR32:
8663 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8664 case elfcpp::R_POWERPC_ADDR16:
8665 case elfcpp::R_POWERPC_ADDR16_LO:
8666 case elfcpp::R_POWERPC_ADDR16_HI:
8667 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8668 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8669 case elfcpp::R_PPC64_ADDR16_HIGH:
8670 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8671 case elfcpp::R_PPC64_ADDR16_HIGHER:
8672 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8673 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8674 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8675 case elfcpp::R_PPC64_ADDR16_DS:
8676 case elfcpp::R_PPC64_ADDR16_LO_DS:
8677 case elfcpp::R_POWERPC_ADDR14:
8678 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8679 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8680 {
c9269dff
AM
8681 // Make a PLT entry if necessary.
8682 if (gsym->needs_plt_entry())
8683 {
9055360d
AM
8684 // Since this is not a PC-relative relocation, we may be
8685 // taking the address of a function. In that case we need to
8686 // set the entry in the dynamic symbol table to the address of
8687 // the PLT call stub.
8688 bool need_ifunc_plt = false;
8689 if ((size == 32 || target->abiversion() >= 2)
8690 && gsym->is_from_dynobj()
8691 && !parameters->options().output_is_position_independent())
8692 {
8693 gsym->set_needs_dynsym_value();
8694 need_ifunc_plt = true;
8695 }
8696 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8697 {
0e123f69 8698 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8699 target->push_branch(ppc_object, data_shndx,
0e123f69 8700 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8701 reloc.get_r_addend());
8702 target->make_plt_entry(symtab, layout, gsym);
8703 }
c9269dff
AM
8704 }
8705 // Make a dynamic relocation if necessary.
88b8e639 8706 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8707 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8708 {
a82bef93
ST
8709 if (!parameters->options().output_is_position_independent()
8710 && gsym->may_need_copy_reloc())
c9269dff
AM
8711 {
8712 target->copy_reloc(symtab, layout, object,
8713 data_shndx, output_section, gsym, reloc);
8714 }
9055360d
AM
8715 else if ((((size == 32
8716 && r_type == elfcpp::R_POWERPC_ADDR32)
8717 || (size == 64
8718 && r_type == elfcpp::R_PPC64_ADDR64
8719 && target->abiversion() >= 2))
627b30b7
AM
8720 && gsym->can_use_relative_reloc(false)
8721 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8722 && parameters->options().shared()))
8723 || (size == 64
8724 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8725 && target->abiversion() < 2
627b30b7
AM
8726 && (gsym->can_use_relative_reloc(false)
8727 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8728 {
b3ccdeb5
AM
8729 Reloc_section* rela_dyn
8730 = target->rela_dyn_section(symtab, layout, is_ifunc);
8731 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8732 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8733 rela_dyn->add_symbolless_global_addend(
8734 gsym, dynrel, output_section, object, data_shndx,
8735 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
8736 }
8737 else
8738 {
b3ccdeb5
AM
8739 Reloc_section* rela_dyn
8740 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 8741 check_non_pic(object, r_type);
dd93cd0a
AM
8742 rela_dyn->add_global(gsym, r_type, output_section,
8743 object, data_shndx,
8744 reloc.get_r_offset(),
8745 reloc.get_r_addend());
5edad15d
AM
8746
8747 if (size == 64
8748 && parameters->options().toc_optimize()
8749 && data_shndx == ppc_object->toc_shndx())
8750 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
8751 }
8752 }
42cacb20
DE
8753 }
8754 break;
8755
e4dff765
AM
8756 case elfcpp::R_PPC64_PLT_PCREL34:
8757 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8758 case elfcpp::R_POWERPC_PLT16_LO:
8759 case elfcpp::R_POWERPC_PLT16_HI:
8760 case elfcpp::R_POWERPC_PLT16_HA:
8761 case elfcpp::R_PPC64_PLT16_LO_DS:
8762 if (!pushed_ifunc)
8763 target->make_plt_entry(symtab, layout, gsym);
8764 break;
8765
32f59844
AM
8766 case elfcpp::R_PPC64_REL24_NOTOC:
8767 if (size == 32)
8768 break;
8769 // Fall through.
cf43a2fe 8770 case elfcpp::R_PPC_PLTREL24:
42cacb20 8771 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
8772 if (!is_ifunc)
8773 {
0e123f69 8774 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8775 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8776 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
8777 if (gsym->needs_plt_entry()
8778 || (!gsym->final_value_is_known()
8779 && (gsym->is_undefined()
8780 || gsym->is_from_dynobj()
8781 || gsym->is_preemptible())))
8782 target->make_plt_entry(symtab, layout, gsym);
8783 }
d8e90251 8784 // Fall through.
42cacb20 8785
3ea0a085 8786 case elfcpp::R_PPC64_REL64:
dd93cd0a 8787 case elfcpp::R_POWERPC_REL32:
3ea0a085 8788 // Make a dynamic relocation if necessary.
88b8e639 8789 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 8790 {
a82bef93
ST
8791 if (!parameters->options().output_is_position_independent()
8792 && gsym->may_need_copy_reloc())
3ea0a085
AM
8793 {
8794 target->copy_reloc(symtab, layout, object,
8795 data_shndx, output_section, gsym,
8796 reloc);
8797 }
8798 else
8799 {
b3ccdeb5
AM
8800 Reloc_section* rela_dyn
8801 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
8802 check_non_pic(object, r_type);
8803 rela_dyn->add_global(gsym, r_type, output_section, object,
8804 data_shndx, reloc.get_r_offset(),
8805 reloc.get_r_addend());
8806 }
8807 }
8808 break;
8809
ec661b9d
AM
8810 case elfcpp::R_POWERPC_REL14:
8811 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8812 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8813 if (!is_ifunc)
0e123f69
AM
8814 {
8815 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8816 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8817 r_type, r_sym, reloc.get_r_addend());
8818 }
ec661b9d
AM
8819 break;
8820
7e57d19e
AM
8821 case elfcpp::R_PPC64_TOCSAVE:
8822 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8823 // caller has already saved r2 and thus a plt call stub need not
8824 // save r2.
8825 if (size == 64
8826 && target->mark_pltcall(ppc_object, data_shndx,
8827 reloc.get_r_offset() - 4, symtab))
8828 {
8829 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8830 bool is_ordinary;
8831 unsigned int shndx = gsym->shndx(&is_ordinary);
8832 if (!is_ordinary)
8833 object->error(_("tocsave symbol %u has bad shndx %u"),
8834 r_sym, shndx);
8835 else
8836 {
8837 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8838 target->add_tocsave(ppc_object, shndx,
8839 sym->value() + reloc.get_r_addend());
8840 }
8841 }
8842 break;
8843
6ce78956
AM
8844 case elfcpp::R_POWERPC_REL16:
8845 case elfcpp::R_POWERPC_REL16_LO:
8846 case elfcpp::R_POWERPC_REL16_HI:
8847 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8848 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8849 case elfcpp::R_PPC64_REL16_HIGH:
8850 case elfcpp::R_PPC64_REL16_HIGHA:
8851 case elfcpp::R_PPC64_REL16_HIGHER:
8852 case elfcpp::R_PPC64_REL16_HIGHERA:
8853 case elfcpp::R_PPC64_REL16_HIGHEST:
8854 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8855 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8856 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8857 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8858 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8859 case elfcpp::R_PPC64_SECTOFF_DS:
8860 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8861 case elfcpp::R_POWERPC_TPREL16:
8862 case elfcpp::R_POWERPC_TPREL16_LO:
8863 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8864 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8865 case elfcpp::R_PPC64_TPREL16_DS:
8866 case elfcpp::R_PPC64_TPREL16_LO_DS:
8867 case elfcpp::R_PPC64_TPREL16_HIGH:
8868 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8869 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8870 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8871 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8872 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8873 case elfcpp::R_POWERPC_DTPREL16:
8874 case elfcpp::R_POWERPC_DTPREL16_LO:
8875 case elfcpp::R_POWERPC_DTPREL16_HI:
8876 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8877 case elfcpp::R_PPC64_DTPREL16_DS:
8878 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8879 case elfcpp::R_PPC64_DTPREL16_HIGH:
8880 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8881 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8882 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8883 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8884 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8885 case elfcpp::R_PPC64_TLSGD:
8886 case elfcpp::R_PPC64_TLSLD:
45965137 8887 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
8888 break;
8889
e4dff765 8890 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8891 case elfcpp::R_POWERPC_GOT16:
8892 case elfcpp::R_POWERPC_GOT16_LO:
8893 case elfcpp::R_POWERPC_GOT16_HI:
8894 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8895 case elfcpp::R_PPC64_GOT16_DS:
8896 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8897 {
c9269dff
AM
8898 // The symbol requires a GOT entry.
8899 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
8900
8901 got = target->got_section(symtab, layout);
2e702c99 8902 if (gsym->final_value_is_known())
2e702c99 8903 {
b01a4b04
AM
8904 if (is_ifunc
8905 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8906 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8907 else
8908 got->add_global(gsym, GOT_TYPE_STANDARD);
8909 }
8910 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8911 {
8912 // If we are generating a shared object or a pie, this
8913 // symbol's GOT entry will be set by a dynamic relocation.
8914 unsigned int off = got->add_constant(0);
8915 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8916
b3ccdeb5
AM
8917 Reloc_section* rela_dyn
8918 = target->rela_dyn_section(symtab, layout, is_ifunc);
8919
e5d5f5ed 8920 if (gsym->can_use_relative_reloc(false)
9055360d
AM
8921 && !((size == 32
8922 || target->abiversion() >= 2)
e5d5f5ed
AM
8923 && gsym->visibility() == elfcpp::STV_PROTECTED
8924 && parameters->options().shared()))
2e702c99 8925 {
b3ccdeb5
AM
8926 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8927 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8928 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8929 }
8930 else
8931 {
8932 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8933 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 8934 }
2e702c99 8935 }
42cacb20
DE
8936 }
8937 break;
8938
cf43a2fe
AM
8939 case elfcpp::R_PPC64_TOC16:
8940 case elfcpp::R_PPC64_TOC16_LO:
8941 case elfcpp::R_PPC64_TOC16_HI:
8942 case elfcpp::R_PPC64_TOC16_HA:
8943 case elfcpp::R_PPC64_TOC16_DS:
8944 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8945 // We need a GOT section.
8946 target->got_section(symtab, layout);
8947 break;
8948
87c69f97 8949 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8950 case elfcpp::R_POWERPC_GOT_TLSGD16:
8951 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8952 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8953 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8954 {
8955 const bool final = gsym->final_value_is_known();
8956 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8957 if (tls_type == tls::TLSOPT_NONE)
8958 {
8959 Output_data_got_powerpc<size, big_endian>* got
8960 = target->got_section(symtab, layout);
b3ccdeb5
AM
8961 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8962 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
8963 elfcpp::R_POWERPC_DTPMOD,
8964 elfcpp::R_POWERPC_DTPREL);
8965 }
8966 else if (tls_type == tls::TLSOPT_TO_IE)
8967 {
acc276d8
AM
8968 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8969 {
8970 Output_data_got_powerpc<size, big_endian>* got
8971 = target->got_section(symtab, layout);
8972 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8973 if (gsym->is_undefined()
8974 || gsym->is_from_dynobj())
8975 {
8976 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8977 elfcpp::R_POWERPC_TPREL);
8978 }
8979 else
8980 {
8981 unsigned int off = got->add_constant(0);
8982 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8983 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8984 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8985 got, off, 0);
8986 }
8987 }
dd93cd0a
AM
8988 }
8989 else if (tls_type == tls::TLSOPT_TO_LE)
8990 {
8991 // no GOT relocs needed for Local Exec.
8992 }
8993 else
8994 gold_unreachable();
8995 }
42cacb20
DE
8996 break;
8997
87c69f97 8998 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8999 case elfcpp::R_POWERPC_GOT_TLSLD16:
9000 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9001 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9002 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9003 {
9004 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
9005 if (tls_type == tls::TLSOPT_NONE)
9006 target->tlsld_got_offset(symtab, layout, object);
9007 else if (tls_type == tls::TLSOPT_TO_LE)
9008 {
9009 // no GOT relocs needed for Local Exec.
7404fe1b
AM
9010 if (parameters->options().emit_relocs())
9011 {
9012 Output_section* os = layout->tls_segment()->first_section();
9013 gold_assert(os != NULL);
9014 os->set_needs_symtab_index();
9015 }
dd93cd0a
AM
9016 }
9017 else
9018 gold_unreachable();
9019 }
9020 break;
9021
87c69f97 9022 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
9023 case elfcpp::R_POWERPC_GOT_DTPREL16:
9024 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9025 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9026 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9027 {
9028 Output_data_got_powerpc<size, big_endian>* got
9029 = target->got_section(symtab, layout);
bd73a62d
AM
9030 if (!gsym->final_value_is_known()
9031 && (gsym->is_from_dynobj()
9032 || gsym->is_undefined()
9033 || gsym->is_preemptible()))
9034 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
9035 target->rela_dyn_section(layout),
9036 elfcpp::R_POWERPC_DTPREL);
9037 else
9038 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
9039 }
9040 break;
9041
87c69f97 9042 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
9043 case elfcpp::R_POWERPC_GOT_TPREL16:
9044 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9045 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9046 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9047 {
9048 const bool final = gsym->final_value_is_known();
9049 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9050 if (tls_type == tls::TLSOPT_NONE)
9051 {
acc276d8
AM
9052 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
9053 {
9054 Output_data_got_powerpc<size, big_endian>* got
9055 = target->got_section(symtab, layout);
9056 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9057 if (gsym->is_undefined()
9058 || gsym->is_from_dynobj())
9059 {
9060 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
9061 elfcpp::R_POWERPC_TPREL);
9062 }
9063 else
9064 {
9065 unsigned int off = got->add_constant(0);
9066 gsym->set_got_offset(GOT_TYPE_TPREL, off);
9067 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9068 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9069 got, off, 0);
9070 }
9071 }
dd93cd0a
AM
9072 }
9073 else if (tls_type == tls::TLSOPT_TO_LE)
9074 {
9075 // no GOT relocs needed for Local Exec.
9076 }
9077 else
9078 gold_unreachable();
9079 }
42cacb20
DE
9080 break;
9081
9082 default:
9083 unsupported_reloc_global(object, r_type, gsym);
9084 break;
9085 }
d8f5a274 9086
5edad15d
AM
9087 if (size == 64
9088 && parameters->options().toc_optimize())
9089 {
9090 if (data_shndx == ppc_object->toc_shndx())
9091 {
9092 bool ok = true;
9093 if (r_type != elfcpp::R_PPC64_ADDR64
9094 || (is_ifunc && target->abiversion() < 2))
9095 ok = false;
9096 else if (parameters->options().output_is_position_independent()
9097 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9098 ok = false;
9099 if (!ok)
9100 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9101 }
9102
9103 enum {no_check, check_lo, check_ha} insn_check;
9104 switch (r_type)
9105 {
9106 default:
9107 insn_check = no_check;
9108 break;
9109
9110 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9111 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9112 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9113 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9114 case elfcpp::R_POWERPC_GOT16_HA:
9115 case elfcpp::R_PPC64_TOC16_HA:
9116 insn_check = check_ha;
9117 break;
9118
9119 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9120 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9121 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9122 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9123 case elfcpp::R_POWERPC_GOT16_LO:
9124 case elfcpp::R_PPC64_GOT16_LO_DS:
9125 case elfcpp::R_PPC64_TOC16_LO:
9126 case elfcpp::R_PPC64_TOC16_LO_DS:
9127 insn_check = check_lo;
9128 break;
9129 }
9130
9131 section_size_type slen;
9132 const unsigned char* view = NULL;
9133 if (insn_check != no_check)
9134 {
9135 view = ppc_object->section_contents(data_shndx, &slen, false);
9136 section_size_type off =
9137 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9138 if (off < slen)
9139 {
9140 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9141 if (insn_check == check_lo
9142 ? !ok_lo_toc_insn(insn, r_type)
9143 : ((insn & ((0x3f << 26) | 0x1f << 16))
9144 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9145 {
9146 ppc_object->set_no_toc_opt();
9147 gold_warning(_("%s: toc optimization is not supported "
9148 "for %#08x instruction"),
9149 ppc_object->name().c_str(), insn);
9150 }
9151 }
9152 }
9153
9154 switch (r_type)
9155 {
9156 default:
9157 break;
9158 case elfcpp::R_PPC64_TOC16:
9159 case elfcpp::R_PPC64_TOC16_LO:
9160 case elfcpp::R_PPC64_TOC16_HI:
9161 case elfcpp::R_PPC64_TOC16_HA:
9162 case elfcpp::R_PPC64_TOC16_DS:
9163 case elfcpp::R_PPC64_TOC16_LO_DS:
9164 if (gsym->source() == Symbol::FROM_OBJECT
9165 && !gsym->object()->is_dynamic())
9166 {
9167 Powerpc_relobj<size, big_endian>* sym_object
9168 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9169 bool is_ordinary;
9170 unsigned int shndx = gsym->shndx(&is_ordinary);
9171 if (shndx == sym_object->toc_shndx())
9172 {
9173 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 9174 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
9175 if (dst_off < sym_object->section_size(shndx))
9176 {
9177 bool ok = false;
9178 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9179 ok = true;
9180 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9181 {
9182 // Need to check that the insn is a ld
9183 if (!view)
9184 view = ppc_object->section_contents(data_shndx,
9185 &slen,
9186 false);
9187 section_size_type off =
9188 (convert_to_section_size_type(reloc.get_r_offset())
9189 + (big_endian ? -2 : 3));
9190 if (off < slen
9191 && (view[off] & (0x3f << 2)) == (58u << 2))
9192 ok = true;
9193 }
9194 if (!ok)
9195 sym_object->set_no_toc_opt(dst_off);
9196 }
9197 }
9198 }
9199 break;
9200 }
9201 }
9202
f159cdb6
AM
9203 if (size == 32)
9204 {
9205 switch (r_type)
9206 {
9207 case elfcpp::R_PPC_LOCAL24PC:
9208 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9209 gold_error(_("%s: unsupported -mbss-plt code"),
9210 ppc_object->name().c_str());
9211 break;
9212 default:
9213 break;
9214 }
9215 }
9216
d8f5a274
AM
9217 switch (r_type)
9218 {
9219 case elfcpp::R_POWERPC_GOT_TLSLD16:
9220 case elfcpp::R_POWERPC_GOT_TLSGD16:
9221 case elfcpp::R_POWERPC_GOT_TPREL16:
9222 case elfcpp::R_POWERPC_GOT_DTPREL16:
9223 case elfcpp::R_POWERPC_GOT16:
9224 case elfcpp::R_PPC64_GOT16_DS:
9225 case elfcpp::R_PPC64_TOC16:
9226 case elfcpp::R_PPC64_TOC16_DS:
9227 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
9228 break;
9229 default:
9230 break;
9231 }
9232
9233 switch (r_type)
9234 {
89c52ae3
AM
9235 case elfcpp::R_PPC64_TPREL16_DS:
9236 case elfcpp::R_PPC64_TPREL16_LO_DS:
9237 case elfcpp::R_PPC64_TPREL16_HIGH:
9238 case elfcpp::R_PPC64_TPREL16_HIGHA:
9239 case elfcpp::R_PPC64_TPREL16_HIGHER:
9240 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9241 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9242 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9243 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
9244 if (size != 64)
9245 break;
9246 // Fall through.
9247 case elfcpp::R_POWERPC_TPREL16:
9248 case elfcpp::R_POWERPC_TPREL16_LO:
9249 case elfcpp::R_POWERPC_TPREL16_HI:
9250 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
9251 layout->set_has_static_tls();
9252 break;
9253 default:
9254 break;
9255 }
9256
252dcdf4
AM
9257 switch (r_type)
9258 {
9259 case elfcpp::R_POWERPC_TPREL16_HA:
9260 if (target->tprel_opt())
9261 {
9262 section_size_type slen;
9263 const unsigned char* view = NULL;
9264 view = ppc_object->section_contents(data_shndx, &slen, false);
9265 section_size_type off
9266 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9267 if (off < slen)
9268 {
9269 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9270 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9271 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 9272 target->set_no_tprel_opt();
252dcdf4
AM
9273 }
9274 }
9275 break;
9276
9277 case elfcpp::R_PPC64_TPREL16_HIGH:
9278 case elfcpp::R_PPC64_TPREL16_HIGHA:
9279 case elfcpp::R_PPC64_TPREL16_HIGHER:
9280 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9281 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9282 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9283 if (size != 64)
9284 break;
9285 // Fall through.
9286 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 9287 target->set_no_tprel_opt();
252dcdf4
AM
9288 break;
9289 default:
9290 break;
9291 }
9292
89c52ae3
AM
9293 switch (r_type)
9294 {
9295 case elfcpp::R_PPC64_D34:
9296 case elfcpp::R_PPC64_D34_LO:
9297 case elfcpp::R_PPC64_D34_HI30:
9298 case elfcpp::R_PPC64_D34_HA30:
9299 case elfcpp::R_PPC64_D28:
9300 case elfcpp::R_PPC64_PCREL34:
9301 case elfcpp::R_PPC64_PCREL28:
9302 case elfcpp::R_PPC64_TPREL34:
9303 case elfcpp::R_PPC64_DTPREL34:
9304 case elfcpp::R_PPC64_PLT_PCREL34:
9305 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9306 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
9307 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9308 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9309 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9310 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 9311 target->set_power10_relocs();
89c52ae3 9312 break;
d8f5a274
AM
9313 default:
9314 break;
9315 }
42cacb20
DE
9316}
9317
6d03d481
ST
9318// Process relocations for gc.
9319
9320template<int size, bool big_endian>
9321void
9322Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
9323 Symbol_table* symtab,
9324 Layout* layout,
9325 Sized_relobj_file<size, big_endian>* object,
9326 unsigned int data_shndx,
9327 unsigned int,
9328 const unsigned char* prelocs,
9329 size_t reloc_count,
9330 Output_section* output_section,
9331 bool needs_special_offset_handling,
9332 size_t local_symbol_count,
9333 const unsigned char* plocal_symbols)
6d03d481
ST
9334{
9335 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9336 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9337 Classify_reloc;
9338
e81fea4d
AM
9339 Powerpc_relobj<size, big_endian>* ppc_object
9340 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9341 if (size == 64)
9342 ppc_object->set_opd_valid();
9343 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9344 {
9345 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9346 for (p = ppc_object->access_from_map()->begin();
9347 p != ppc_object->access_from_map()->end();
9348 ++p)
9349 {
9350 Address dst_off = p->first;
9351 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9352 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9353 for (s = p->second.begin(); s != p->second.end(); ++s)
9354 {
efc6fa12 9355 Relobj* src_obj = s->first;
e81fea4d
AM
9356 unsigned int src_indx = s->second;
9357 symtab->gc()->add_reference(src_obj, src_indx,
9358 ppc_object, dst_indx);
9359 }
9360 p->second.clear();
9361 }
9362 ppc_object->access_from_map()->clear();
c6de8ed4 9363 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
9364 // Don't look at .opd relocs as .opd will reference everything.
9365 return;
9366 }
6d03d481 9367
4d625b70 9368 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9369 symtab,
9370 layout,
9371 this,
9372 object,
9373 data_shndx,
9374 prelocs,
9375 reloc_count,
9376 output_section,
9377 needs_special_offset_handling,
9378 local_symbol_count,
9379 plocal_symbols);
9380}
9381
e81fea4d
AM
9382// Handle target specific gc actions when adding a gc reference from
9383// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9384// and DST_OFF. For powerpc64, this adds a referenc to the code
9385// section of a function descriptor.
9386
9387template<int size, bool big_endian>
9388void
9389Target_powerpc<size, big_endian>::do_gc_add_reference(
9390 Symbol_table* symtab,
efc6fa12 9391 Relobj* src_obj,
e81fea4d 9392 unsigned int src_shndx,
efc6fa12 9393 Relobj* dst_obj,
e81fea4d
AM
9394 unsigned int dst_shndx,
9395 Address dst_off) const
9396{
6c77229c
AM
9397 if (size != 64 || dst_obj->is_dynamic())
9398 return;
9399
e81fea4d
AM
9400 Powerpc_relobj<size, big_endian>* ppc_object
9401 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9402 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9403 {
9404 if (ppc_object->opd_valid())
9405 {
9406 dst_shndx = ppc_object->get_opd_ent(dst_off);
9407 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9408 }
9409 else
9410 {
9411 // If we haven't run scan_opd_relocs, we must delay
9412 // processing this function descriptor reference.
9413 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9414 }
9415 }
9416}
9417
9418// Add any special sections for this symbol to the gc work list.
9419// For powerpc64, this adds the code section of a function
9420// descriptor.
9421
9422template<int size, bool big_endian>
9423void
9424Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9425 Symbol_table* symtab,
9426 Symbol* sym) const
9427{
6a31512f 9428 if (size == 64 && sym->object()->pluginobj() == NULL)
e81fea4d
AM
9429 {
9430 Powerpc_relobj<size, big_endian>* ppc_object
9431 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9432 bool is_ordinary;
9433 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9434 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9435 {
9436 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9437 Address dst_off = gsym->value();
c6de8ed4
AM
9438 if (ppc_object->opd_valid())
9439 {
9440 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9441 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9442 dst_indx));
c6de8ed4
AM
9443 }
9444 else
9445 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9446 }
9447 }
9448}
9449
dc3714f3
AM
9450// For a symbol location in .opd, set LOC to the location of the
9451// function entry.
9452
9453template<int size, bool big_endian>
9454void
9455Target_powerpc<size, big_endian>::do_function_location(
9456 Symbol_location* loc) const
9457{
a2d7bf59 9458 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9459 {
9460 if (loc->object->is_dynamic())
9461 {
9462 Powerpc_dynobj<size, big_endian>* ppc_object
9463 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9464 if (loc->shndx == ppc_object->opd_shndx())
9465 {
9466 Address dest_off;
9467 Address off = loc->offset - ppc_object->opd_address();
9468 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9469 loc->offset = dest_off;
9470 }
9471 }
9472 else
9473 {
9474 const Powerpc_relobj<size, big_endian>* ppc_object
9475 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9476 if (loc->shndx == ppc_object->opd_shndx())
9477 {
9478 Address dest_off;
9479 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9480 loc->offset = dest_off;
9481 }
9482 }
9483 }
9484}
9485
bbec1a5d
AM
9486// FNOFFSET in section SHNDX in OBJECT is the start of a function
9487// compiled with -fsplit-stack. The function calls non-split-stack
9488// code. Change the function to ensure it has enough stack space to
9489// call some random function.
9490
9491template<int size, bool big_endian>
9492void
9493Target_powerpc<size, big_endian>::do_calls_non_split(
9494 Relobj* object,
9495 unsigned int shndx,
9496 section_offset_type fnoffset,
9497 section_size_type fnsize,
6e0813d3
CC
9498 const unsigned char* prelocs,
9499 size_t reloc_count,
bbec1a5d
AM
9500 unsigned char* view,
9501 section_size_type view_size,
9502 std::string* from,
9503 std::string* to) const
9504{
9505 // 32-bit not supported.
9506 if (size == 32)
9507 {
9508 // warn
9509 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9510 prelocs, reloc_count, view, view_size,
9511 from, to);
bbec1a5d
AM
9512 return;
9513 }
9514
9515 // The function always starts with
9516 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9517 // addis %r12,%r1,-allocate@ha
9518 // addi %r12,%r12,-allocate@l
9519 // cmpld %r12,%r0
9520 // but note that the addis or addi may be replaced with a nop
9521
9522 unsigned char *entry = view + fnoffset;
9523 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9524
9525 if ((insn & 0xffff0000) == addis_2_12)
9526 {
9527 /* Skip ELFv2 global entry code. */
9528 entry += 8;
9529 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9530 }
9531
9532 unsigned char *pinsn = entry;
9533 bool ok = false;
9534 const uint32_t ld_private_ss = 0xe80d8fc0;
9535 if (insn == ld_private_ss)
9536 {
9537 int32_t allocate = 0;
9538 while (1)
9539 {
9540 pinsn += 4;
9541 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9542 if ((insn & 0xffff0000) == addis_12_1)
9543 allocate += (insn & 0xffff) << 16;
9544 else if ((insn & 0xffff0000) == addi_12_1
9545 || (insn & 0xffff0000) == addi_12_12)
9546 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9547 else if (insn != nop)
9548 break;
9549 }
9550 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9551 {
9552 int extra = parameters->options().split_stack_adjust_size();
9553 allocate -= extra;
9554 if (allocate >= 0 || extra < 0)
9555 {
9556 object->error(_("split-stack stack size overflow at "
9557 "section %u offset %0zx"),
9558 shndx, static_cast<size_t>(fnoffset));
9559 return;
9560 }
9561 pinsn = entry + 4;
9562 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9563 if (insn != addis_12_1)
9564 {
9565 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9566 pinsn += 4;
9567 insn = addi_12_12 | (allocate & 0xffff);
9568 if (insn != addi_12_12)
9569 {
9570 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9571 pinsn += 4;
9572 }
9573 }
9574 else
9575 {
9576 insn = addi_12_1 | (allocate & 0xffff);
9577 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9578 pinsn += 4;
9579 }
9580 if (pinsn != entry + 12)
9581 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9582
9583 ok = true;
9584 }
9585 }
9586
9587 if (!ok)
9588 {
9589 if (!object->has_no_split_stack())
9590 object->error(_("failed to match split-stack sequence at "
9591 "section %u offset %0zx"),
9592 shndx, static_cast<size_t>(fnoffset));
9593 }
9594}
9595
42cacb20
DE
9596// Scan relocations for a section.
9597
9598template<int size, bool big_endian>
9599void
9600Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9601 Symbol_table* symtab,
9602 Layout* layout,
9603 Sized_relobj_file<size, big_endian>* object,
9604 unsigned int data_shndx,
9605 unsigned int sh_type,
9606 const unsigned char* prelocs,
9607 size_t reloc_count,
9608 Output_section* output_section,
9609 bool needs_special_offset_handling,
9610 size_t local_symbol_count,
9611 const unsigned char* plocal_symbols)
42cacb20
DE
9612{
9613 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9614 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9615 Classify_reloc;
42cacb20 9616
7ee7ff70
AM
9617 if (!this->plt_localentry0_init_)
9618 {
9619 bool plt_localentry0 = false;
9620 if (size == 64
9621 && this->abiversion() >= 2)
9622 {
9623 if (parameters->options().user_set_plt_localentry())
9624 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9625 if (plt_localentry0
9626 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9627 gold_warning(_("--plt-localentry is especially dangerous without "
9628 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9629 }
9630 this->plt_localentry0_ = plt_localentry0;
9631 this->plt_localentry0_init_ = true;
9632 }
9633
42cacb20
DE
9634 if (sh_type == elfcpp::SHT_REL)
9635 {
9636 gold_error(_("%s: unsupported REL reloc section"),
9637 object->name().c_str());
9638 return;
9639 }
9640
4d625b70 9641 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9642 symtab,
9643 layout,
9644 this,
9645 object,
9646 data_shndx,
9647 prelocs,
9648 reloc_count,
9649 output_section,
9650 needs_special_offset_handling,
9651 local_symbol_count,
9652 plocal_symbols);
63e5eea2
AM
9653
9654 if (this->plt_localentry0_ && this->power10_relocs_)
9655 {
9656 gold_warning(_("--plt-localentry is incompatible with "
9657 "power10 pc-relative code"));
9658 this->plt_localentry0_ = false;
9659 }
42cacb20
DE
9660}
9661
ec4dbad3
AM
9662// Functor class for processing the global symbol table.
9663// Removes symbols defined on discarded opd entries.
9664
9665template<bool big_endian>
9666class Global_symbol_visitor_opd
9667{
9668 public:
9669 Global_symbol_visitor_opd()
9670 { }
9671
9672 void
9673 operator()(Sized_symbol<64>* sym)
9674 {
9675 if (sym->has_symtab_index()
9676 || sym->source() != Symbol::FROM_OBJECT
9677 || !sym->in_real_elf())
9678 return;
9679
6c77229c
AM
9680 if (sym->object()->is_dynamic())
9681 return;
9682
ec4dbad3
AM
9683 Powerpc_relobj<64, big_endian>* symobj
9684 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9685 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9686 return;
9687
9688 bool is_ordinary;
9689 unsigned int shndx = sym->shndx(&is_ordinary);
9690 if (shndx == symobj->opd_shndx()
9691 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9692 {
9693 sym->set_undefined();
e3ee8ed4 9694 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9695 sym->set_is_defined_in_discarded_section();
9696 sym->set_symtab_index(-1U);
9697 }
ec4dbad3
AM
9698 }
9699};
9700
f3a0ed29
AM
9701template<int size, bool big_endian>
9702void
9703Target_powerpc<size, big_endian>::define_save_restore_funcs(
9704 Layout* layout,
9705 Symbol_table* symtab)
9706{
9707 if (size == 64)
9708 {
d49044c7
AM
9709 Output_data_save_res<size, big_endian>* savres
9710 = new Output_data_save_res<size, big_endian>(symtab);
9711 this->savres_section_ = savres;
f3a0ed29
AM
9712 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9713 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9714 savres, ORDER_TEXT, false);
9715 }
9716}
9717
d8f5a274
AM
9718// Sort linker created .got section first (for the header), then input
9719// sections belonging to files using small model code.
9720
9721template<bool big_endian>
9722class Sort_toc_sections
9723{
9724 public:
9725 bool
9726 operator()(const Output_section::Input_section& is1,
9727 const Output_section::Input_section& is2) const
9728 {
9729 if (!is1.is_input_section() && is2.is_input_section())
9730 return true;
9731 bool small1
9732 = (is1.is_input_section()
9733 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9734 ->has_small_toc_reloc()));
9735 bool small2
9736 = (is2.is_input_section()
9737 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9738 ->has_small_toc_reloc()));
9739 return small1 && !small2;
9740 }
9741};
9742
42cacb20
DE
9743// Finalize the sections.
9744
9745template<int size, bool big_endian>
9746void
d5b40221
DK
9747Target_powerpc<size, big_endian>::do_finalize_sections(
9748 Layout* layout,
724436fc 9749 const Input_objects* input_objects,
ec4dbad3 9750 Symbol_table* symtab)
42cacb20 9751{
c9824451
AM
9752 if (parameters->doing_static_link())
9753 {
9754 // At least some versions of glibc elf-init.o have a strong
9755 // reference to __rela_iplt marker syms. A weak ref would be
9756 // better..
9757 if (this->iplt_ != NULL)
9758 {
9759 Reloc_section* rel = this->iplt_->rel_plt();
9760 symtab->define_in_output_data("__rela_iplt_start", NULL,
9761 Symbol_table::PREDEFINED, rel, 0, 0,
9762 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9763 elfcpp::STV_HIDDEN, 0, false, true);
9764 symtab->define_in_output_data("__rela_iplt_end", NULL,
9765 Symbol_table::PREDEFINED, rel, 0, 0,
9766 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9767 elfcpp::STV_HIDDEN, 0, true, true);
9768 }
9769 else
9770 {
9771 symtab->define_as_constant("__rela_iplt_start", NULL,
9772 Symbol_table::PREDEFINED, 0, 0,
9773 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9774 elfcpp::STV_HIDDEN, 0, true, false);
9775 symtab->define_as_constant("__rela_iplt_end", NULL,
9776 Symbol_table::PREDEFINED, 0, 0,
9777 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9778 elfcpp::STV_HIDDEN, 0, true, false);
9779 }
9780 }
9781
ec4dbad3
AM
9782 if (size == 64)
9783 {
9784 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9785 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
9786
9787 if (!parameters->options().relocatable())
9788 {
9789 this->define_save_restore_funcs(layout, symtab);
9790
9791 // Annoyingly, we need to make these sections now whether or
9792 // not we need them. If we delay until do_relax then we
9793 // need to mess with the relaxation machinery checkpointing.
9794 this->got_section(symtab, layout);
9795 this->make_brlt_section(layout);
d8f5a274
AM
9796
9797 if (parameters->options().toc_sort())
9798 {
9799 Output_section* os = this->got_->output_section();
9800 if (os != NULL && os->input_sections().size() > 1)
9801 std::stable_sort(os->input_sections().begin(),
9802 os->input_sections().end(),
9803 Sort_toc_sections<big_endian>());
9804 }
ec661b9d 9805 }
ec4dbad3
AM
9806 }
9807
42cacb20 9808 // Fill in some more dynamic tags.
c9269dff 9809 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 9810 if (odyn != NULL)
cf43a2fe 9811 {
c9824451
AM
9812 const Reloc_section* rel_plt = (this->plt_ == NULL
9813 ? NULL
9814 : this->plt_->rel_plt());
9815 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9816 this->rela_dyn_, true, size == 32);
9817
9818 if (size == 32)
dd93cd0a 9819 {
c9824451
AM
9820 if (this->got_ != NULL)
9821 {
9822 this->got_->finalize_data_size();
9823 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9824 this->got_, this->got_->g_o_t());
9825 }
34e0882b
AM
9826 if (this->has_tls_get_addr_opt_)
9827 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 9828 }
c9824451 9829 else
dd93cd0a 9830 {
c9824451
AM
9831 if (this->glink_ != NULL)
9832 {
9833 this->glink_->finalize_data_size();
9834 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9835 this->glink_,
9e390558 9836 (this->glink_->pltresolve_size()
c9824451
AM
9837 - 32));
9838 }
34e0882b 9839 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 9840 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
9841 ((this->has_localentry0_
9842 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9843 | (this->has_tls_get_addr_opt_
9844 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 9845 }
c9269dff 9846 }
cf43a2fe 9847
42cacb20
DE
9848 // Emit any relocs we saved in an attempt to avoid generating COPY
9849 // relocs.
9850 if (this->copy_relocs_.any_saved_relocs())
9851 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
9852
9853 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9854 p != input_objects->relobj_end();
9855 ++p)
9856 {
9857 Powerpc_relobj<size, big_endian>* ppc_relobj
9858 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9859 if (ppc_relobj->attributes_section_data())
6f3fe02b 9860 this->merge_object_attributes(ppc_relobj,
724436fc
AM
9861 ppc_relobj->attributes_section_data());
9862 }
9863 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9864 p != input_objects->dynobj_end();
9865 ++p)
9866 {
9867 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9868 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9869 if (ppc_dynobj->attributes_section_data())
6f3fe02b 9870 this->merge_object_attributes(ppc_dynobj,
724436fc
AM
9871 ppc_dynobj->attributes_section_data());
9872 }
9873
9874 // Create a .gnu.attributes section if we have merged any attributes
9875 // from inputs.
9876 if (this->attributes_section_data_ != NULL
9877 && this->attributes_section_data_->size() != 0)
9878 {
9879 Output_attributes_section_data* attributes_section
9880 = new Output_attributes_section_data(*this->attributes_section_data_);
9881 layout->add_output_section_data(".gnu.attributes",
9882 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9883 attributes_section, ORDER_INVALID, false);
9884 }
9885}
9886
9887// Merge object attributes from input file called NAME with those of the
9888// output. The input object attributes are in the object pointed by PASD.
9889
9890template<int size, bool big_endian>
9891void
9892Target_powerpc<size, big_endian>::merge_object_attributes(
6f3fe02b 9893 const Object* obj,
724436fc
AM
9894 const Attributes_section_data* pasd)
9895{
9896 // Return if there is no attributes section data.
9897 if (pasd == NULL)
9898 return;
9899
9900 // Create output object attributes.
9901 if (this->attributes_section_data_ == NULL)
9902 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9903
9904 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9905 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9906 Object_attribute* out_attr
9907 = this->attributes_section_data_->known_attributes(vendor);
9908
6f3fe02b 9909 const char* name = obj->name().c_str();
724436fc
AM
9910 const char* err;
9911 const char* first;
9912 const char* second;
9913 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9914 int in_fp = in_attr[tag].int_value() & 0xf;
9915 int out_fp = out_attr[tag].int_value() & 0xf;
6f3fe02b 9916 bool warn_only = obj->is_dynamic();
724436fc
AM
9917 if (in_fp != out_fp)
9918 {
9919 err = NULL;
9920 if ((in_fp & 3) == 0)
9921 ;
9922 else if ((out_fp & 3) == 0)
9923 {
6f3fe02b
AM
9924 if (!warn_only)
9925 {
9926 out_fp |= in_fp & 3;
9927 out_attr[tag].set_int_value(out_fp);
9928 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9929 this->last_fp_ = name;
9930 }
724436fc
AM
9931 }
9932 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9933 {
9934 err = N_("%s uses hard float, %s uses soft float");
9935 first = this->last_fp_;
9936 second = name;
9937 }
9938 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9939 {
9940 err = N_("%s uses hard float, %s uses soft float");
9941 first = name;
9942 second = this->last_fp_;
9943 }
9944 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9945 {
9946 err = N_("%s uses double-precision hard float, "
9947 "%s uses single-precision hard float");
9948 first = this->last_fp_;
9949 second = name;
9950 }
9951 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9952 {
9953 err = N_("%s uses double-precision hard float, "
9954 "%s uses single-precision hard float");
9955 first = name;
9956 second = this->last_fp_;
9957 }
9958
9959 if (err || (in_fp & 0xc) == 0)
9960 ;
9961 else if ((out_fp & 0xc) == 0)
9962 {
6f3fe02b
AM
9963 if (!warn_only)
9964 {
9965 out_fp |= in_fp & 0xc;
9966 out_attr[tag].set_int_value(out_fp);
9967 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9968 this->last_ld_ = name;
9969 }
724436fc
AM
9970 }
9971 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9972 {
9973 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9974 first = name;
9975 second = this->last_ld_;
9976 }
9977 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9978 {
9979 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9980 first = this->last_ld_;
9981 second = name;
9982 }
9983 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9984 {
9985 err = N_("%s uses IBM long double, %s uses IEEE long double");
9986 first = this->last_ld_;
9987 second = name;
9988 }
9989 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9990 {
9991 err = N_("%s uses IBM long double, %s uses IEEE long double");
9992 first = name;
9993 second = this->last_ld_;
9994 }
9995
9996 if (err)
9997 {
9998 if (parameters->options().warn_mismatch())
6f3fe02b
AM
9999 {
10000 if (warn_only)
10001 gold_warning(_(err), first, second);
10002 else
10003 gold_error(_(err), first, second);
10004 }
724436fc
AM
10005 // Arrange for this attribute to be deleted. It's better to
10006 // say "don't know" about a file than to wrongly claim compliance.
6f3fe02b
AM
10007 if (!warn_only)
10008 out_attr[tag].set_type(0);
724436fc
AM
10009 }
10010 }
10011
10012 if (size == 32)
10013 {
10014 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10015 int in_vec = in_attr[tag].int_value() & 3;
10016 int out_vec = out_attr[tag].int_value() & 3;
10017 if (in_vec != out_vec)
10018 {
10019 err = NULL;
10020 if (in_vec == 0)
10021 ;
10022 else if (out_vec == 0)
10023 {
10024 out_vec = in_vec;
10025 out_attr[tag].set_int_value(out_vec);
10026 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10027 this->last_vec_ = name;
10028 }
10029 // For now, allow generic to transition to AltiVec or SPE
10030 // without a warning. If GCC marked files with their stack
10031 // alignment and used don't-care markings for files which are
10032 // not affected by the vector ABI, we could warn about this
10033 // case too. */
10034 else if (in_vec == 1)
10035 ;
10036 else if (out_vec == 1)
10037 {
10038 out_vec = in_vec;
10039 out_attr[tag].set_int_value(out_vec);
10040 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10041 this->last_vec_ = name;
10042 }
10043 else if (out_vec < in_vec)
10044 {
10045 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10046 first = this->last_vec_;
10047 second = name;
10048 }
10049 else if (out_vec > in_vec)
10050 {
10051 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10052 first = name;
10053 second = this->last_vec_;
10054 }
10055 if (err)
10056 {
10057 if (parameters->options().warn_mismatch())
10058 gold_error(_(err), first, second);
10059 out_attr[tag].set_type(0);
10060 }
10061 }
10062
10063 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10064 int in_struct = in_attr[tag].int_value() & 3;
10065 int out_struct = out_attr[tag].int_value() & 3;
10066 if (in_struct != out_struct)
10067 {
10068 err = NULL;
10069 if (in_struct == 0 || in_struct == 3)
10070 ;
10071 else if (out_struct == 0)
10072 {
10073 out_struct = in_struct;
10074 out_attr[tag].set_int_value(out_struct);
10075 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10076 this->last_struct_ = name;
10077 }
10078 else if (out_struct < in_struct)
10079 {
10080 err = N_("%s uses r3/r4 for small structure returns, "
10081 "%s uses memory");
10082 first = this->last_struct_;
10083 second = name;
10084 }
10085 else if (out_struct > in_struct)
10086 {
10087 err = N_("%s uses r3/r4 for small structure returns, "
10088 "%s uses memory");
10089 first = name;
10090 second = this->last_struct_;
10091 }
10092 if (err)
10093 {
10094 if (parameters->options().warn_mismatch())
10095 gold_error(_(err), first, second);
10096 out_attr[tag].set_type(0);
10097 }
10098 }
10099 }
10100
10101 // Merge Tag_compatibility attributes and any common GNU ones.
10102 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
10103}
10104
5edad15d
AM
10105// Emit any saved relocs, and mark toc entries using any of these
10106// relocs as not optimizable.
aba6bc71 10107
5edad15d
AM
10108template<int sh_type, int size, bool big_endian>
10109void
10110Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10111 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 10112{
5edad15d
AM
10113 if (size == 64
10114 && parameters->options().toc_optimize())
10115 {
10116 for (typename Copy_relocs<sh_type, size, big_endian>::
10117 Copy_reloc_entries::iterator p = this->entries_.begin();
10118 p != this->entries_.end();
10119 ++p)
10120 {
10121 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10122 entry = *p;
10123
10124 // If the symbol is no longer defined in a dynamic object,
10125 // then we emitted a COPY relocation. If it is still
10126 // dynamic then we'll need dynamic relocations and thus
10127 // can't optimize toc entries.
10128 if (entry.sym_->is_from_dynobj())
10129 {
10130 Powerpc_relobj<size, big_endian>* ppc_object
10131 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10132 if (entry.shndx_ == ppc_object->toc_shndx())
10133 ppc_object->set_no_toc_opt(entry.address_);
10134 }
10135 }
10136 }
10137
10138 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
10139}
10140
3ea0a085
AM
10141// Return the value to use for a branch relocation.
10142
10143template<int size, bool big_endian>
1611bc4a 10144bool
3ea0a085 10145Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 10146 const Symbol_table* symtab,
3ea0a085
AM
10147 const Sized_symbol<size>* gsym,
10148 Powerpc_relobj<size, big_endian>* object,
1611bc4a 10149 Address *value,
3ea0a085
AM
10150 unsigned int *dest_shndx)
10151{
9055360d
AM
10152 if (size == 32 || this->abiversion() >= 2)
10153 gold_unreachable();
3ea0a085 10154 *dest_shndx = 0;
3ea0a085
AM
10155
10156 // If the symbol is defined in an opd section, ie. is a function
10157 // descriptor, use the function descriptor code entry address
10158 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 10159 if (gsym != NULL
0e123f69
AM
10160 && (gsym->source() != Symbol::FROM_OBJECT
10161 || gsym->object()->is_dynamic()))
1611bc4a 10162 return true;
3ea0a085
AM
10163 if (gsym != NULL)
10164 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10165 unsigned int shndx = symobj->opd_shndx();
10166 if (shndx == 0)
1611bc4a 10167 return true;
3ea0a085 10168 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 10169 if (opd_addr == invalid_address)
1611bc4a 10170 return true;
c6905c28 10171 opd_addr += symobj->output_section_address(shndx);
1611bc4a 10172 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
10173 {
10174 Address sec_off;
1611bc4a 10175 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
10176 if (symtab->is_section_folded(symobj, *dest_shndx))
10177 {
10178 Section_id folded
10179 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10180 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10181 *dest_shndx = folded.second;
10182 }
3ea0a085 10183 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
10184 if (sec_addr == invalid_address)
10185 return false;
10186
3ea0a085 10187 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 10188 *value = sec_addr + sec_off;
3ea0a085 10189 }
1611bc4a 10190 return true;
3ea0a085
AM
10191}
10192
c9b8abb7
AM
10193template<int size>
10194static bool
10195relative_value_is_known(const Sized_symbol<size>* gsym)
10196{
10197 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10198 return false;
10199
10200 if (gsym->is_from_dynobj()
10201 || gsym->is_undefined()
10202 || gsym->is_preemptible())
10203 return false;
10204
10205 if (gsym->is_absolute())
10206 return !parameters->options().output_is_position_independent();
10207
10208 return true;
10209}
10210
10211template<int size>
10212static bool
10213relative_value_is_known(const Symbol_value<size>* psymval)
10214{
10215 if (psymval->is_ifunc_symbol())
10216 return false;
10217
10218 bool is_ordinary;
10219 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10220
10221 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10222}
10223
0c951c25
AM
10224// PCREL_OPT in one instance flags to the linker that a pair of insns:
10225// pld ra,symbol@got@pcrel
10226// load/store rt,0(ra)
10227// or
10228// pla ra,symbol@pcrel
10229// load/store rt,0(ra)
10230// may be translated to
10231// pload/pstore rt,symbol@pcrel
10232// nop.
10233// This function returns true if the optimization is possible, placing
10234// the prefix insn in *PINSN1 and a NOP in *PINSN2.
10235//
10236// On entry to this function, the linker has already determined that
10237// the pld can be replaced with pla: *PINSN1 is that pla insn,
10238// while *PINSN2 is the second instruction.
10239
10240inline bool
10241xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10242{
10243 uint32_t insn2 = *pinsn2 >> 32;
10244 uint64_t i1new;
10245
10246 // Check that regs match.
10247 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10248 return false;
10249
10250 switch ((insn2 >> 26) & 63)
10251 {
10252 default:
10253 return false;
10254
10255 case 32: // lwz
10256 case 34: // lbz
10257 case 36: // stw
10258 case 38: // stb
10259 case 40: // lhz
10260 case 42: // lha
10261 case 44: // sth
10262 case 48: // lfs
10263 case 50: // lfd
10264 case 52: // stfs
10265 case 54: // stfd
10266 // These are the PMLS cases, where we just need to tack a prefix
10267 // on the insn. Check that the D field is zero.
10268 if ((insn2 & 0xffff) != 0)
10269 return false;
10270 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10271 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10272 break;
10273
10274 case 58: // lwa, ld
10275 if ((insn2 & 0xfffd) != 0)
10276 return false;
10277 i1new = ((1ULL << 58) | (1ULL << 52)
10278 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10279 | (insn2 & (31ULL << 21)));
10280 break;
10281
10282 case 57: // lxsd, lxssp
10283 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10284 return false;
10285 i1new = ((1ULL << 58) | (1ULL << 52)
10286 | ((40ULL | (insn2 & 3)) << 26)
10287 | (insn2 & (31ULL << 21)));
10288 break;
10289
10290 case 61: // stxsd, stxssp, lxv, stxv
10291 if ((insn2 & 3) == 0)
10292 return false;
10293 else if ((insn2 & 3) >= 2)
10294 {
10295 if ((insn2 & 0xfffc) != 0)
10296 return false;
10297 i1new = ((1ULL << 58) | (1ULL << 52)
10298 | ((44ULL | (insn2 & 3)) << 26)
10299 | (insn2 & (31ULL << 21)));
10300 }
10301 else
10302 {
10303 if ((insn2 & 0xfff0) != 0)
10304 return false;
10305 i1new = ((1ULL << 58) | (1ULL << 52)
10306 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10307 | (insn2 & (31ULL << 21)));
10308 }
10309 break;
10310
10311 case 56: // lq
10312 if ((insn2 & 0xffff) != 0)
10313 return false;
10314 i1new = ((1ULL << 58) | (1ULL << 52)
10315 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10316 break;
10317
10318 case 62: // std, stq
10319 if ((insn2 & 0xfffd) != 0)
10320 return false;
10321 i1new = ((1ULL << 58) | (1ULL << 52)
10322 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10323 | (insn2 & (31ULL << 21)));
10324 break;
10325 }
10326
10327 *pinsn1 = i1new;
10328 *pinsn2 = (uint64_t) nop << 32;
10329 return true;
10330}
10331
42cacb20
DE
10332// Perform a relocation.
10333
10334template<int size, bool big_endian>
10335inline bool
10336Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 10337 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 10338 unsigned int,
d83ce4e3
AM
10339 Target_powerpc* target,
10340 Output_section* os,
10341 size_t relnum,
91a65d2f 10342 const unsigned char* preloc,
d83ce4e3
AM
10343 const Sized_symbol<size>* gsym,
10344 const Symbol_value<size>* psymval,
10345 unsigned char* view,
c9269dff
AM
10346 Address address,
10347 section_size_type view_size)
42cacb20 10348{
23cedd1d
AM
10349 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10350 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10351 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10352
0e804863
ILT
10353 if (view == NULL)
10354 return true;
10355
34e0882b
AM
10356 if (target->replace_tls_get_addr(gsym))
10357 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10358
91a65d2f
AM
10359 const elfcpp::Rela<size, big_endian> rela(preloc);
10360 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
34e0882b 10361 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 10362 {
e3deeb9c
AM
10363 case Track_tls::NOT_EXPECTED:
10364 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10365 _("__tls_get_addr call lacks marker reloc"));
10366 break;
10367 case Track_tls::EXPECTED:
10368 // We have already complained.
10369 break;
10370 case Track_tls::SKIP:
23cedd1d 10371 if (is_plt16_reloc<size>(r_type)
32f59844
AM
10372 || r_type == elfcpp::R_POWERPC_PLTSEQ
10373 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
10374 {
10375 Insn* iview = reinterpret_cast<Insn*>(view);
10376 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10377 }
10378 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10379 {
10380 Insn* iview = reinterpret_cast<Insn*>(view);
10381 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10382 }
e4dff765
AM
10383 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10384 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10385 {
10386 Insn* iview = reinterpret_cast<Insn*>(view);
10387 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10388 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10389 }
e3deeb9c
AM
10390 return true;
10391 case Track_tls::NORMAL:
10392 break;
dd93cd0a 10393 }
dd93cd0a 10394
dcfc7dd4
AM
10395 // Offset from start of insn to d-field reloc.
10396 const int d_offset = big_endian ? 2 : 0;
10397
3ea0a085
AM
10398 Powerpc_relobj<size, big_endian>* const object
10399 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 10400 Address value = 0;
0cfb0717 10401 bool has_stub_value = false;
7ee7ff70 10402 bool localentry0 = false;
e5d5f5ed 10403 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
08be3224
AM
10404 bool has_plt_offset
10405 = (gsym != NULL
88b8e639 10406 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
08be3224
AM
10407 : object->local_has_plt_offset(r_sym));
10408 if (has_plt_offset
4290b0ab 10409 && !is_got_reloc(r_type)
08be3224 10410 && !is_plt16_reloc<size>(r_type)
e4dff765
AM
10411 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10412 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
23cedd1d
AM
10413 && r_type != elfcpp::R_POWERPC_PLTSEQ
10414 && r_type != elfcpp::R_POWERPC_PLTCALL
32f59844
AM
10415 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10416 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
b3ccdeb5 10417 && (!psymval->is_ifunc_symbol()
9055360d 10418 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 10419 {
9055360d
AM
10420 if (size == 64
10421 && gsym != NULL
10422 && target->abiversion() >= 2
10423 && !parameters->options().output_is_position_independent()
32f59844 10424 && !is_branch_reloc<size>(r_type))
ec661b9d 10425 {
faa2211d
AM
10426 Address off = target->glink_section()->find_global_entry(gsym);
10427 if (off != invalid_address)
6ec65f28
AM
10428 {
10429 value = target->glink_section()->global_entry_address() + off;
10430 has_stub_value = true;
10431 }
ec661b9d 10432 }
c9824451 10433 else
9055360d 10434 {
64b5d6d7
AM
10435 Stub_table<size, big_endian>* stub_table = NULL;
10436 if (target->stub_tables().size() == 1)
10437 stub_table = target->stub_tables()[0];
10438 if (stub_table == NULL
10439 && !(size == 32
10440 && gsym != NULL
10441 && !parameters->options().output_is_position_independent()
32f59844 10442 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10443 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10444 if (stub_table == NULL)
10445 {
64b5d6d7
AM
10446 // This is a ref from a data section to an ifunc symbol,
10447 // or a non-branch reloc for which we always want to use
10448 // one set of stubs for resolving function addresses.
9055360d
AM
10449 if (target->stub_tables().size() != 0)
10450 stub_table = target->stub_tables()[0];
10451 }
faa2211d
AM
10452 if (stub_table != NULL)
10453 {
7e57d19e 10454 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10455 if (gsym != NULL)
7e57d19e 10456 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10457 rela.get_r_addend());
10458 else
7e57d19e 10459 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10460 rela.get_r_addend());
7e57d19e 10461 if (ent != NULL)
faa2211d 10462 {
7e57d19e
AM
10463 value = stub_table->stub_address() + ent->off_;
10464 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10465 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10466 size_t reloc_count = shdr.get_sh_size() / reloc_size;
afd2ea23
AM
10467 if (size == 64
10468 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
10469 value += ent->tocoff_;
7e57d19e
AM
10470 if (size == 64
10471 && ent->r2save_
a993d270
AM
10472 && !(gsym != NULL
10473 && target->is_tls_get_addr_opt(gsym)))
afd2ea23 10474 {
a993d270
AM
10475 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10476 {
10477 if (!(target->power10_stubs()
10478 && target->power10_stubs_auto()))
10479 value += 4;
10480 }
10481 else if (relnum < reloc_count - 1)
10482 {
10483 Reltype next_rela(preloc + reloc_size);
10484 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10485 == elfcpp::R_PPC64_TOCSAVE
10486 && (next_rela.get_r_offset()
10487 == rela.get_r_offset() + 4))
10488 value += 4;
10489 }
7e57d19e 10490 }
7ee7ff70 10491 localentry0 = ent->localentry0_;
faa2211d
AM
10492 has_stub_value = true;
10493 }
10494 }
9055360d 10495 }
faa2211d
AM
10496 // We don't care too much about bogus debug references to
10497 // non-local functions, but otherwise there had better be a plt
10498 // call stub or global entry stub as appropriate.
10499 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10500 }
cf43a2fe 10501
e4dff765
AM
10502 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10503 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10504 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10505 {
10506 const Output_data_plt_powerpc<size, big_endian>* plt;
10507 if (gsym)
10508 value = target->plt_off(gsym, &plt);
10509 else
10510 value = target->plt_off(object, r_sym, &plt);
10511 value += plt->address();
10512
10513 if (size == 64)
e4dff765
AM
10514 {
10515 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10516 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10517 value -= (target->got_section()->output_section()->address()
10518 + object->toc_base_offset());
10519 }
08be3224
AM
10520 else if (parameters->options().output_is_position_independent())
10521 {
10522 if (rela.get_r_addend() >= 32768)
10523 {
10524 unsigned int got2 = object->got2_shndx();
10525 value -= (object->get_output_section_offset(got2)
10526 + object->output_section(got2)->address()
10527 + rela.get_r_addend());
10528 }
10529 else
10530 value -= (target->got_section()->address()
10531 + target->got_section()->g_o_t());
10532 }
10533 }
23cedd1d
AM
10534 else if (!has_plt_offset
10535 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10536 || r_type == elfcpp::R_POWERPC_PLTSEQ
10537 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10538 {
10539 Insn* iview = reinterpret_cast<Insn*>(view);
10540 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10541 r_type = elfcpp::R_POWERPC_NONE;
10542 }
e4dff765
AM
10543 else if (!has_plt_offset
10544 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10545 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10546 {
10547 Insn* iview = reinterpret_cast<Insn*>(view);
10548 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10549 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10550 r_type = elfcpp::R_POWERPC_NONE;
10551 }
4290b0ab 10552 else if (is_got_reloc(r_type))
42cacb20 10553 {
cf43a2fe
AM
10554 if (gsym != NULL)
10555 {
10556 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10557 value = gsym->got_offset(GOT_TYPE_STANDARD);
10558 }
10559 else
10560 {
cf43a2fe
AM
10561 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10562 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10563 }
e4dff765
AM
10564 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10565 value += target->got_section()->address();
10566 else
10567 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
10568 }
10569 else if (r_type == elfcpp::R_PPC64_TOC)
10570 {
c9269dff 10571 value = (target->got_section()->output_section()->address()
dd93cd0a 10572 + object->toc_base_offset());
cf43a2fe
AM
10573 }
10574 else if (gsym != NULL
10575 && (r_type == elfcpp::R_POWERPC_REL24
10576 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10577 && has_stub_value)
cf43a2fe 10578 {
c9269dff
AM
10579 if (size == 64)
10580 {
10581 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10582 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10583 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10584 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10585 {
3ea0a085 10586 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10587 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10588 if ((insn & 1) != 0
10589 && (insn2 == nop
10590 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10591 {
b4f7960d
AM
10592 elfcpp::Swap<32, big_endian>::
10593 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10594 can_plt_call = true;
10595 }
10596 }
10597 if (!can_plt_call)
3ea0a085
AM
10598 {
10599 // If we don't have a branch and link followed by a nop,
10600 // we can't go via the plt because there is no place to
10601 // put a toc restoring instruction.
10602 // Unless we know we won't be returning.
10603 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10604 can_plt_call = true;
10605 }
10606 if (!can_plt_call)
10607 {
ba8ca3e7
AM
10608 // g++ as of 20130507 emits self-calls without a
10609 // following nop. This is arguably wrong since we have
10610 // conflicting information. On the one hand a global
10611 // symbol and on the other a local call sequence, but
10612 // don't error for this special case.
10613 // It isn't possible to cheaply verify we have exactly
10614 // such a call. Allow all calls to the same section.
3ea0a085 10615 bool ok = false;
c9824451 10616 Address code = value;
3ea0a085
AM
10617 if (gsym->source() == Symbol::FROM_OBJECT
10618 && gsym->object() == object)
10619 {
9055360d
AM
10620 unsigned int dest_shndx = 0;
10621 if (target->abiversion() < 2)
10622 {
10623 Address addend = rela.get_r_addend();
1611bc4a
AM
10624 code = psymval->value(object, addend);
10625 target->symval_for_branch(relinfo->symtab, gsym, object,
10626 &code, &dest_shndx);
9055360d 10627 }
3ea0a085
AM
10628 bool is_ordinary;
10629 if (dest_shndx == 0)
10630 dest_shndx = gsym->shndx(&is_ordinary);
10631 ok = dest_shndx == relinfo->data_shndx;
10632 }
10633 if (!ok)
c9824451
AM
10634 {
10635 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10636 _("call lacks nop, can't restore toc; "
10637 "recompile with -fPIC"));
10638 value = code;
10639 }
3ea0a085 10640 }
c9269dff 10641 }
cf43a2fe 10642 }
dd93cd0a
AM
10643 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10644 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10645 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3 10646 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
87c69f97 10647 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a
AM
10648 {
10649 // First instruction of a global dynamic sequence, arg setup insn.
10650 const bool final = gsym == NULL || gsym->final_value_is_known();
10651 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10652 enum Got_type got_type = GOT_TYPE_STANDARD;
10653 if (tls_type == tls::TLSOPT_NONE)
10654 got_type = GOT_TYPE_TLSGD;
10655 else if (tls_type == tls::TLSOPT_TO_IE)
10656 got_type = GOT_TYPE_TPREL;
10657 if (got_type != GOT_TYPE_STANDARD)
10658 {
10659 if (gsym != NULL)
10660 {
10661 gold_assert(gsym->has_got_offset(got_type));
10662 value = gsym->got_offset(got_type);
10663 }
10664 else
10665 {
dd93cd0a
AM
10666 gold_assert(object->local_has_got_offset(r_sym, got_type));
10667 value = object->local_got_offset(r_sym, got_type);
10668 }
87c69f97 10669 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
89c52ae3
AM
10670 value += target->got_section()->address();
10671 else
10672 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10673 }
10674 if (tls_type == tls::TLSOPT_TO_IE)
10675 {
87c69f97 10676 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 10677 {
89c52ae3
AM
10678 Insn* iview = reinterpret_cast<Insn*>(view);
10679 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10680 pinsn <<= 32;
10681 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10682 // pla -> pld
10683 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10684 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10685 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10686 pinsn & 0xffffffff);
87c69f97 10687 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
89c52ae3
AM
10688 }
10689 else
10690 {
10691 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10692 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10693 {
10694 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10695 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10696 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10697 if (size == 32)
10698 insn |= 32 << 26; // lwz
10699 else
10700 insn |= 58 << 26; // ld
10701 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10702 }
10703 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10704 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 10705 }
dd93cd0a
AM
10706 }
10707 else if (tls_type == tls::TLSOPT_TO_LE)
10708 {
87c69f97 10709 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 10710 {
89c52ae3
AM
10711 Insn* iview = reinterpret_cast<Insn*>(view);
10712 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10713 pinsn <<= 32;
10714 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10715 // pla pcrel -> paddi r13
10716 pinsn += (-1ULL << 52) + (13ULL << 16);
10717 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10718 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10719 pinsn & 0xffffffff);
10720 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
10721 value = psymval->value(object, rela.get_r_addend());
10722 }
10723 else
10724 {
89c52ae3
AM
10725 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10726 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10727 {
10728 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10729 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10730 insn &= (1 << 26) - (1 << 21); // extract rt
10731 if (size == 32)
10732 insn |= addis_0_2;
10733 else
10734 insn |= addis_0_13;
10735 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10736 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10737 value = psymval->value(object, rela.get_r_addend());
10738 }
10739 else
10740 {
10741 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10742 Insn insn = nop;
10743 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10744 r_type = elfcpp::R_POWERPC_NONE;
10745 }
dd93cd0a
AM
10746 }
10747 }
10748 }
10749 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10750 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10751 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3 10752 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
87c69f97 10753 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
dd93cd0a
AM
10754 {
10755 // First instruction of a local dynamic sequence, arg setup insn.
10756 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10757 if (tls_type == tls::TLSOPT_NONE)
10758 {
10759 value = target->tlsld_got_offset();
87c69f97 10760 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
10761 value += target->got_section()->address();
10762 else
10763 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10764 }
10765 else
10766 {
10767 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 10768 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
10769 {
10770 Insn* iview = reinterpret_cast<Insn*>(view);
10771 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10772 pinsn <<= 32;
10773 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10774 // pla pcrel -> paddi r13
10775 pinsn += (-1ULL << 52) + (13ULL << 16);
10776 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10777 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10778 pinsn & 0xffffffff);
10779 r_type = elfcpp::R_PPC64_TPREL34;
10780 value = dtp_offset;
10781 }
10782 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10783 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 10784 {
dcfc7dd4 10785 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
10786 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10787 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 10788 if (size == 32)
0f81d3f0
AM
10789 insn |= addis_0_2;
10790 else
10791 insn |= addis_0_13;
dd93cd0a
AM
10792 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10793 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 10794 value = dtp_offset;
dd93cd0a
AM
10795 }
10796 else
10797 {
dcfc7dd4 10798 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10799 Insn insn = nop;
10800 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10801 r_type = elfcpp::R_POWERPC_NONE;
10802 }
10803 }
10804 }
10805 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10806 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10807 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3 10808 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
87c69f97 10809 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
dd93cd0a
AM
10810 {
10811 // Accesses relative to a local dynamic sequence address,
10812 // no optimisation here.
10813 if (gsym != NULL)
10814 {
10815 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10816 value = gsym->got_offset(GOT_TYPE_DTPREL);
10817 }
10818 else
10819 {
dd93cd0a
AM
10820 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10821 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10822 }
87c69f97 10823 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
89c52ae3
AM
10824 value += target->got_section()->address();
10825 else
10826 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10827 }
10828 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10829 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10830 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3 10831 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
87c69f97 10832 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
dd93cd0a
AM
10833 {
10834 // First instruction of initial exec sequence.
10835 const bool final = gsym == NULL || gsym->final_value_is_known();
10836 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10837 if (tls_type == tls::TLSOPT_NONE)
10838 {
10839 if (gsym != NULL)
10840 {
10841 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10842 value = gsym->got_offset(GOT_TYPE_TPREL);
10843 }
10844 else
10845 {
dd93cd0a
AM
10846 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10847 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10848 }
87c69f97 10849 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
10850 value += target->got_section()->address();
10851 else
10852 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10853 }
10854 else
10855 {
10856 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 10857 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
10858 {
10859 Insn* iview = reinterpret_cast<Insn*>(view);
10860 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10861 pinsn <<= 32;
10862 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10863 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10864 pinsn += ((2ULL << 56) + (-1ULL << 52)
10865 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10866 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10867 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10868 pinsn & 0xffffffff);
10869 r_type = elfcpp::R_PPC64_TPREL34;
10870 value = psymval->value(object, rela.get_r_addend());
10871 }
10872 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10873 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 10874 {
dcfc7dd4 10875 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10876 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10877 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10878 if (size == 32)
10879 insn |= addis_0_2;
10880 else
10881 insn |= addis_0_13;
10882 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10883 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10884 value = psymval->value(object, rela.get_r_addend());
10885 }
10886 else
10887 {
dcfc7dd4 10888 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10889 Insn insn = nop;
10890 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10891 r_type = elfcpp::R_POWERPC_NONE;
10892 }
10893 }
10894 }
10895 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10896 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10897 {
10898 // Second instruction of a global dynamic sequence,
10899 // the __tls_get_addr call
e3deeb9c 10900 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10901 const bool final = gsym == NULL || gsym->final_value_is_known();
10902 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10903 if (tls_type != tls::TLSOPT_NONE)
10904 {
10905 if (tls_type == tls::TLSOPT_TO_IE)
10906 {
10907 Insn* iview = reinterpret_cast<Insn*>(view);
10908 Insn insn = add_3_3_13;
10909 if (size == 32)
10910 insn = add_3_3_2;
10911 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10912 r_type = elfcpp::R_POWERPC_NONE;
10913 }
10914 else
10915 {
89c52ae3
AM
10916 bool is_pcrel = false;
10917 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10918 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10919 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10920 if (relnum < reloc_count - 1)
10921 {
10922 Reltype next_rela(preloc + reloc_size);
10923 unsigned int r_type2
10924 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10925 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10926 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10927 && next_rela.get_r_offset() == rela.get_r_offset())
10928 is_pcrel = true;
10929 }
dd93cd0a 10930 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10931 if (is_pcrel)
10932 {
10933 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10934 r_type = elfcpp::R_POWERPC_NONE;
10935 }
10936 else
10937 {
10938 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10939 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10940 view += d_offset;
10941 value = psymval->value(object, rela.get_r_addend());
10942 }
dd93cd0a 10943 }
e3deeb9c 10944 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10945 }
10946 }
10947 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10948 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10949 {
10950 // Second instruction of a local dynamic sequence,
10951 // the __tls_get_addr call
e3deeb9c 10952 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10953 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10954 if (tls_type == tls::TLSOPT_TO_LE)
10955 {
89c52ae3
AM
10956 bool is_pcrel = false;
10957 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10958 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10959 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10960 if (relnum < reloc_count - 1)
10961 {
10962 Reltype next_rela(preloc + reloc_size);
10963 unsigned int r_type2
10964 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10965 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10966 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10967 && next_rela.get_r_offset() == rela.get_r_offset())
10968 is_pcrel = true;
10969 }
dd93cd0a 10970 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10971 if (is_pcrel)
10972 {
10973 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10974 r_type = elfcpp::R_POWERPC_NONE;
10975 }
10976 else
10977 {
10978 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10979 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10980 view += d_offset;
10981 value = dtp_offset;
10982 }
e3deeb9c 10983 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10984 }
10985 }
10986 else if (r_type == elfcpp::R_POWERPC_TLS)
10987 {
10988 // Second instruction of an initial exec sequence
10989 const bool final = gsym == NULL || gsym->final_value_is_known();
10990 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10991 if (tls_type == tls::TLSOPT_TO_LE)
10992 {
89c52ae3
AM
10993 Address roff = rela.get_r_offset() & 3;
10994 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
10995 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10996 unsigned int reg = size == 32 ? 2 : 13;
10997 insn = at_tls_transform(insn, reg);
10998 gold_assert(insn != 0);
89c52ae3
AM
10999 if (roff == 0)
11000 {
11001 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11002 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11003 view += d_offset;
11004 value = psymval->value(object, rela.get_r_addend());
11005 }
11006 else if (roff == 1)
11007 {
11008 // For pcrel IE to LE we already have the full offset
11009 // and thus don't need an addi here. A nop or mr will do.
11010 if ((insn & (0x3f << 26)) == 14 << 26)
11011 {
11012 // Extract regs from addi rt,ra,si.
11013 unsigned int rt = (insn >> 21) & 0x1f;
11014 unsigned int ra = (insn >> 16) & 0x1f;
11015 if (rt == ra)
11016 insn = nop;
11017 else
11018 {
11019 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11020 insn = (rt << 16) | (ra << 21) | (ra << 11);
11021 insn |= (31u << 26) | (444u << 1);
11022 }
11023 }
11024 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11025 r_type = elfcpp::R_POWERPC_NONE;
11026 }
dd93cd0a
AM
11027 }
11028 }
0cfb0717 11029 else if (!has_stub_value)
cf43a2fe 11030 {
32f59844
AM
11031 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11032 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
11033 {
11034 // PLTCALL without plt entry => convert to direct call
11035 Insn* iview = reinterpret_cast<Insn*>(view);
11036 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11037 insn = (insn & 1) | b;
11038 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11039 if (size == 32)
11040 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
11041 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11042 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
11043 else
11044 r_type = elfcpp::R_POWERPC_REL24;
11045 }
dd93cd0a 11046 Address addend = 0;
08be3224
AM
11047 if (!(size == 32
11048 && (r_type == elfcpp::R_PPC_PLTREL24
11049 || r_type == elfcpp::R_POWERPC_PLT16_LO
11050 || r_type == elfcpp::R_POWERPC_PLT16_HI
11051 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 11052 addend = rela.get_r_addend();
c9824451 11053 value = psymval->value(object, addend);
fa40fbe4 11054 unsigned int local_ent = 0;
32f59844 11055 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
11056 {
11057 if (target->abiversion() >= 2)
11058 {
11059 if (gsym != NULL)
fa40fbe4 11060 local_ent = object->ppc64_local_entry_offset(gsym);
9055360d 11061 else
fa40fbe4 11062 local_ent = object->ppc64_local_entry_offset(r_sym);
9055360d
AM
11063 }
11064 else
1611bc4a
AM
11065 {
11066 unsigned int dest_shndx;
11067 target->symval_for_branch(relinfo->symtab, gsym, object,
11068 &value, &dest_shndx);
11069 }
9055360d 11070 }
fa40fbe4
AM
11071 Address max_branch = max_branch_delta<size>(r_type);
11072 if (max_branch != 0
11073 && (value + local_ent - address + max_branch >= 2 * max_branch
32f59844
AM
11074 || (size == 64
11075 && r_type == elfcpp::R_PPC64_REL24_NOTOC
11076 && (gsym != NULL
11077 ? object->ppc64_needs_toc(gsym)
11078 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
11079 {
11080 Stub_table<size, big_endian>* stub_table
11081 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
11082 if (stub_table != NULL)
11083 {
32f59844
AM
11084 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11085 = stub_table->find_long_branch_entry(object, value);
11086 if (ent != NULL)
0cfb0717 11087 {
32f59844
AM
11088 if (ent->save_res_)
11089 value = (value - target->savres_section()->address()
11090 + stub_table->branch_size());
11091 else
afd2ea23
AM
11092 {
11093 value = (stub_table->stub_address()
11094 + stub_table->plt_size()
11095 + ent->off_);
11096 if (size == 64
11097 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
f1e05b19 11098 value += ent->tocoff_;
afd2ea23 11099 }
0cfb0717
AM
11100 has_stub_value = true;
11101 }
0cfdc767 11102 }
ec661b9d 11103 }
fa40fbe4
AM
11104 if (!has_stub_value)
11105 value += local_ent;
42cacb20
DE
11106 }
11107
42cacb20
DE
11108 switch (r_type)
11109 {
32f59844
AM
11110 case elfcpp::R_PPC64_REL24_NOTOC:
11111 if (size == 32)
11112 break;
11113 // Fall through.
dd93cd0a
AM
11114 case elfcpp::R_PPC64_REL64:
11115 case elfcpp::R_POWERPC_REL32:
11116 case elfcpp::R_POWERPC_REL24:
11117 case elfcpp::R_PPC_PLTREL24:
11118 case elfcpp::R_PPC_LOCAL24PC:
11119 case elfcpp::R_POWERPC_REL16:
11120 case elfcpp::R_POWERPC_REL16_LO:
11121 case elfcpp::R_POWERPC_REL16_HI:
11122 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 11123 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
11124 case elfcpp::R_PPC64_REL16_HIGH:
11125 case elfcpp::R_PPC64_REL16_HIGHA:
11126 case elfcpp::R_PPC64_REL16_HIGHER:
11127 case elfcpp::R_PPC64_REL16_HIGHERA:
11128 case elfcpp::R_PPC64_REL16_HIGHEST:
11129 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11130 case elfcpp::R_POWERPC_REL14:
11131 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11132 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11133 case elfcpp::R_PPC64_PCREL34:
11134 case elfcpp::R_PPC64_GOT_PCREL34:
11135 case elfcpp::R_PPC64_PLT_PCREL34:
11136 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11137 case elfcpp::R_PPC64_PCREL28:
87c69f97
AM
11138 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11139 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11140 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11141 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11142 case elfcpp::R_PPC64_REL16_HIGHER34:
11143 case elfcpp::R_PPC64_REL16_HIGHERA34:
11144 case elfcpp::R_PPC64_REL16_HIGHEST34:
11145 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
11146 value -= address;
11147 break;
11148
42cacb20
DE
11149 case elfcpp::R_PPC64_TOC16:
11150 case elfcpp::R_PPC64_TOC16_LO:
11151 case elfcpp::R_PPC64_TOC16_HI:
11152 case elfcpp::R_PPC64_TOC16_HA:
11153 case elfcpp::R_PPC64_TOC16_DS:
11154 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 11155 // Subtract the TOC base address.
c9269dff 11156 value -= (target->got_section()->output_section()->address()
dd93cd0a 11157 + object->toc_base_offset());
42cacb20
DE
11158 break;
11159
cf43a2fe
AM
11160 case elfcpp::R_POWERPC_SECTOFF:
11161 case elfcpp::R_POWERPC_SECTOFF_LO:
11162 case elfcpp::R_POWERPC_SECTOFF_HI:
11163 case elfcpp::R_POWERPC_SECTOFF_HA:
11164 case elfcpp::R_PPC64_SECTOFF_DS:
11165 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11166 if (os != NULL)
11167 value -= os->address();
42cacb20
DE
11168 break;
11169
dd93cd0a
AM
11170 case elfcpp::R_PPC64_TPREL16_DS:
11171 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
11172 case elfcpp::R_PPC64_TPREL16_HIGH:
11173 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 11174 if (size != 64)
f9c6b907 11175 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 11176 break;
d8e90251 11177 // Fall through.
dd93cd0a
AM
11178 case elfcpp::R_POWERPC_TPREL16:
11179 case elfcpp::R_POWERPC_TPREL16_LO:
11180 case elfcpp::R_POWERPC_TPREL16_HI:
11181 case elfcpp::R_POWERPC_TPREL16_HA:
11182 case elfcpp::R_POWERPC_TPREL:
11183 case elfcpp::R_PPC64_TPREL16_HIGHER:
11184 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11185 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11186 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 11187 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
11188 // tls symbol values are relative to tls_segment()->vaddr()
11189 value -= tp_offset;
11190 break;
11191
11192 case elfcpp::R_PPC64_DTPREL16_DS:
11193 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11194 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11195 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11196 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11197 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11198 if (size != 64)
11199 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11200 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11201 break;
d8e90251 11202 // Fall through.
dd93cd0a
AM
11203 case elfcpp::R_POWERPC_DTPREL16:
11204 case elfcpp::R_POWERPC_DTPREL16_LO:
11205 case elfcpp::R_POWERPC_DTPREL16_HI:
11206 case elfcpp::R_POWERPC_DTPREL16_HA:
11207 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
11208 case elfcpp::R_PPC64_DTPREL16_HIGH:
11209 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 11210 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
11211 // tls symbol values are relative to tls_segment()->vaddr()
11212 value -= dtp_offset;
11213 break;
11214
45965137
AM
11215 case elfcpp::R_PPC64_ADDR64_LOCAL:
11216 if (gsym != NULL)
11217 value += object->ppc64_local_entry_offset(gsym);
11218 else
11219 value += object->ppc64_local_entry_offset(r_sym);
11220 break;
11221
42cacb20
DE
11222 default:
11223 break;
11224 }
11225
dd93cd0a 11226 Insn branch_bit = 0;
42cacb20
DE
11227 switch (r_type)
11228 {
dd93cd0a
AM
11229 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11230 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11231 branch_bit = 1 << 21;
d8e90251 11232 // Fall through.
dd93cd0a
AM
11233 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11234 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11235 {
11236 Insn* iview = reinterpret_cast<Insn*>(view);
11237 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11238 insn &= ~(1 << 21);
11239 insn |= branch_bit;
11240 if (this->is_isa_v2)
11241 {
11242 // Set 'a' bit. This is 0b00010 in BO field for branch
11243 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11244 // for branch on CTR insns (BO == 1a00t or 1a01t).
11245 if ((insn & (0x14 << 21)) == (0x04 << 21))
11246 insn |= 0x02 << 21;
11247 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11248 insn |= 0x08 << 21;
11249 else
11250 break;
11251 }
11252 else
11253 {
11254 // Invert 'y' bit if not the default.
11255 if (static_cast<Signed_address>(value) < 0)
11256 insn ^= 1 << 21;
11257 }
11258 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11259 }
11260 break;
11261
08be3224
AM
11262 case elfcpp::R_POWERPC_PLT16_HA:
11263 if (size == 32
11264 && !parameters->options().output_is_position_independent())
11265 {
11266 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11267 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11268
11269 // Convert addis to lis.
11270 if ((insn & (0x3f << 26)) == 15u << 26
11271 && (insn & (0x1f << 16)) != 0)
11272 {
11273 insn &= ~(0x1f << 16);
11274 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11275 }
11276 }
11277 break;
11278
dd93cd0a
AM
11279 default:
11280 break;
11281 }
11282
252dcdf4
AM
11283 if (gsym
11284 ? relative_value_is_known(gsym)
11285 : relative_value_is_known(psymval))
aba6bc71 11286 {
0c951c25
AM
11287 Insn* iview;
11288 Insn* iview2;
11289 Insn insn;
11290 uint64_t pinsn, pinsn2;
11291
aba6bc71
AM
11292 switch (r_type)
11293 {
11294 default:
11295 break;
11296
5edad15d
AM
11297 // Multi-instruction sequences that access the GOT/TOC can
11298 // be optimized, eg.
11299 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11300 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11301 // and
11302 // addis ra,r2,0; addi rb,ra,x@toc@l;
11303 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
11304 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11305 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11306 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11307 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11308 case elfcpp::R_POWERPC_GOT16_HA:
11309 case elfcpp::R_PPC64_TOC16_HA:
252dcdf4 11310 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11311 {
0c951c25
AM
11312 iview = reinterpret_cast<Insn*>(view - d_offset);
11313 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
11314 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11315 && object->make_toc_relative(target, &value))
11316 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11317 && object->make_got_relative(target, psymval,
11318 rela.get_r_addend(),
11319 &value)))
5edad15d
AM
11320 {
11321 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11322 == ((15u << 26) | (2 << 16)));
11323 }
11324 if (((insn & ((0x3f << 26) | 0x1f << 16))
11325 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11326 && value + 0x8000 < 0x10000)
aba6bc71
AM
11327 {
11328 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11329 return true;
11330 }
11331 }
11332 break;
11333
11334 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11335 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11336 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11337 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11338 case elfcpp::R_POWERPC_GOT16_LO:
11339 case elfcpp::R_PPC64_GOT16_LO_DS:
11340 case elfcpp::R_PPC64_TOC16_LO:
11341 case elfcpp::R_PPC64_TOC16_LO_DS:
252dcdf4 11342 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11343 {
0c951c25
AM
11344 iview = reinterpret_cast<Insn*>(view - d_offset);
11345 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 11346 bool changed = false;
c9b8abb7
AM
11347 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11348 && object->make_toc_relative(target, &value))
11349 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11350 && object->make_got_relative(target, psymval,
11351 rela.get_r_addend(),
11352 &value)))
5edad15d
AM
11353 {
11354 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11355 insn ^= (14u << 26) ^ (58u << 26);
11356 r_type = elfcpp::R_PPC64_TOC16_LO;
11357 changed = true;
11358 }
11359 if (ok_lo_toc_insn(insn, r_type)
11360 && value + 0x8000 < 0x10000)
aba6bc71
AM
11361 {
11362 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11363 {
11364 // Transform addic to addi when we change reg.
11365 insn &= ~((0x3f << 26) | (0x1f << 16));
11366 insn |= (14u << 26) | (2 << 16);
11367 }
11368 else
11369 {
11370 insn &= ~(0x1f << 16);
11371 insn |= 2 << 16;
11372 }
5edad15d 11373 changed = true;
aba6bc71 11374 }
5edad15d
AM
11375 if (changed)
11376 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
11377 }
11378 break;
549dba71 11379
c9b8abb7 11380 case elfcpp::R_PPC64_GOT_PCREL34:
252dcdf4 11381 if (size == 64 && parameters->options().toc_optimize())
c9b8abb7 11382 {
0c951c25
AM
11383 iview = reinterpret_cast<Insn*>(view);
11384 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11385 pinsn <<= 32;
11386 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11387 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
11388 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11389 break;
11390
11391 Address relval = psymval->value(object, rela.get_r_addend());
11392 relval -= address;
11393 if (relval + (1ULL << 33) < 1ULL << 34)
11394 {
11395 value = relval;
11396 // Replace with paddi
0c951c25
AM
11397 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11398 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11399 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11400 pinsn & 0xffffffff);
11401 goto pcrelopt;
c9b8abb7
AM
11402 }
11403 }
11404 break;
11405
0c951c25 11406 case elfcpp::R_PPC64_PCREL34:
252dcdf4
AM
11407 if (size == 64)
11408 {
11409 iview = reinterpret_cast<Insn*>(view);
11410 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11411 pinsn <<= 32;
11412 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11413 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11414 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11415 | (14ULL << 26) /* paddi */))
11416 break;
0c951c25 11417
252dcdf4
AM
11418 pcrelopt:
11419 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11420 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11421 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11422 if (relnum >= reloc_count - 1)
11423 break;
0c951c25 11424
252dcdf4
AM
11425 Reltype next_rela(preloc + reloc_size);
11426 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11427 != elfcpp::R_PPC64_PCREL_OPT)
11428 || next_rela.get_r_offset() != rela.get_r_offset())
11429 break;
0c951c25 11430
252dcdf4
AM
11431 Address off = next_rela.get_r_addend();
11432 if (off == 0)
11433 off = 8; // zero means next insn.
11434 if (off + rela.get_r_offset() + 4 > view_size)
11435 break;
0c951c25 11436
252dcdf4
AM
11437 iview2 = reinterpret_cast<Insn*>(view + off);
11438 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11439 pinsn2 <<= 32;
11440 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11441 break;
11442 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11443 {
11444 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11445 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11446 pinsn & 0xffffffff);
11447 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11448 }
11449 }
0c951c25
AM
11450 break;
11451
9a23f96e 11452 case elfcpp::R_POWERPC_TPREL16_HA:
252dcdf4 11453 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11454 {
11455 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
252dcdf4
AM
11456 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11457 return true;
9a23f96e
AM
11458 }
11459 break;
11460
11461 case elfcpp::R_PPC64_TPREL16_LO_DS:
11462 if (size == 32)
11463 // R_PPC_TLSGD, R_PPC_TLSLD
11464 break;
11465 // Fall through.
11466 case elfcpp::R_POWERPC_TPREL16_LO:
252dcdf4 11467 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11468 {
11469 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11470 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11471 insn &= ~(0x1f << 16);
11472 insn |= (size == 32 ? 2 : 13) << 16;
11473 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11474 }
11475 break;
11476
549dba71 11477 case elfcpp::R_PPC64_ENTRY:
252dcdf4 11478 if (size == 64)
549dba71 11479 {
252dcdf4
AM
11480 value = (target->got_section()->output_section()->address()
11481 + object->toc_base_offset());
11482 if (value + 0x80008000 <= 0xffffffff
11483 && !parameters->options().output_is_position_independent())
549dba71
AM
11484 {
11485 Insn* iview = reinterpret_cast<Insn*>(view);
11486 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11487 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11488
11489 if ((insn1 & ~0xfffc) == ld_2_12
11490 && insn2 == add_2_2_12)
11491 {
252dcdf4 11492 insn1 = lis_2 + ha(value);
549dba71
AM
11493 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11494 insn2 = addi_2_2 + l(value);
11495 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11496 return true;
11497 }
11498 }
252dcdf4
AM
11499 else
11500 {
11501 value -= address;
11502 if (value + 0x80008000 <= 0xffffffff)
11503 {
11504 Insn* iview = reinterpret_cast<Insn*>(view);
11505 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11506 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11507
11508 if ((insn1 & ~0xfffc) == ld_2_12
11509 && insn2 == add_2_2_12)
11510 {
11511 insn1 = addis_2_12 + ha(value);
11512 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11513 insn2 = addi_2_2 + l(value);
11514 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11515 return true;
11516 }
11517 }
11518 }
549dba71
AM
11519 }
11520 break;
e3a7574e
AM
11521
11522 case elfcpp::R_POWERPC_REL16_LO:
11523 // If we are generating a non-PIC executable, edit
11524 // 0: addis 2,12,.TOC.-0b@ha
11525 // addi 2,2,.TOC.-0b@l
11526 // used by ELFv2 global entry points to set up r2, to
11527 // lis 2,.TOC.@ha
11528 // addi 2,2,.TOC.@l
11529 // if .TOC. is in range. */
252dcdf4
AM
11530 if (size == 64
11531 && value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11532 && relnum + 1 > 1
e3a7574e
AM
11533 && preloc != NULL
11534 && target->abiversion() >= 2
11535 && !parameters->options().output_is_position_independent()
4f038ee5 11536 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11537 && gsym != NULL
11538 && strcmp(gsym->name(), ".TOC.") == 0)
11539 {
0e123f69 11540 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11541 Reltype prev_rela(preloc - reloc_size);
11542 if ((prev_rela.get_r_info()
11543 == elfcpp::elf_r_info<size>(r_sym,
11544 elfcpp::R_POWERPC_REL16_HA))
11545 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11546 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11547 {
dcfc7dd4 11548 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11549 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11550 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11551
11552 if ((insn1 & 0xffff0000) == addis_2_12
11553 && (insn2 & 0xffff0000) == addi_2_2)
11554 {
11555 insn1 = lis_2 + ha(value + address - 4);
11556 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11557 insn2 = addi_2_2 + l(value + address - 4);
11558 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11559 if (relinfo->rr)
11560 {
11561 relinfo->rr->set_strategy(relnum - 1,
11562 Relocatable_relocs::RELOC_SPECIAL);
11563 relinfo->rr->set_strategy(relnum,
11564 Relocatable_relocs::RELOC_SPECIAL);
11565 }
11566 return true;
11567 }
11568 }
11569 }
11570 break;
aba6bc71
AM
11571 }
11572 }
11573
f4baf0d4 11574 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11575 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11576 switch (r_type)
11577 {
11578 case elfcpp::R_POWERPC_ADDR32:
11579 case elfcpp::R_POWERPC_UADDR32:
11580 if (size == 64)
f4baf0d4 11581 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11582 break;
11583
11584 case elfcpp::R_POWERPC_REL32:
a680de9a 11585 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11586 if (size == 64)
f4baf0d4 11587 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11588 break;
11589
dd93cd0a 11590 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11591 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11592 break;
11593
b80eed39
AM
11594 case elfcpp::R_POWERPC_ADDR16:
11595 // We really should have three separate relocations,
11596 // one for 16-bit data, one for insns with 16-bit signed fields,
11597 // and one for insns with 16-bit unsigned fields.
11598 overflow = Reloc::CHECK_BITFIELD;
11599 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11600 overflow = Reloc::CHECK_LOW_INSN;
11601 break;
11602
f9c6b907
AM
11603 case elfcpp::R_POWERPC_ADDR16_HI:
11604 case elfcpp::R_POWERPC_ADDR16_HA:
11605 case elfcpp::R_POWERPC_GOT16_HI:
11606 case elfcpp::R_POWERPC_GOT16_HA:
11607 case elfcpp::R_POWERPC_PLT16_HI:
11608 case elfcpp::R_POWERPC_PLT16_HA:
11609 case elfcpp::R_POWERPC_SECTOFF_HI:
11610 case elfcpp::R_POWERPC_SECTOFF_HA:
11611 case elfcpp::R_PPC64_TOC16_HI:
11612 case elfcpp::R_PPC64_TOC16_HA:
11613 case elfcpp::R_PPC64_PLTGOT16_HI:
11614 case elfcpp::R_PPC64_PLTGOT16_HA:
11615 case elfcpp::R_POWERPC_TPREL16_HI:
11616 case elfcpp::R_POWERPC_TPREL16_HA:
11617 case elfcpp::R_POWERPC_DTPREL16_HI:
11618 case elfcpp::R_POWERPC_DTPREL16_HA:
11619 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11620 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11621 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11622 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11623 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11624 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11625 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11626 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11627 case elfcpp::R_POWERPC_REL16_HI:
11628 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11629 if (size != 32)
11630 overflow = Reloc::CHECK_HIGH_INSN;
11631 break;
11632
dd93cd0a
AM
11633 case elfcpp::R_POWERPC_REL16:
11634 case elfcpp::R_PPC64_TOC16:
11635 case elfcpp::R_POWERPC_GOT16:
11636 case elfcpp::R_POWERPC_SECTOFF:
11637 case elfcpp::R_POWERPC_TPREL16:
11638 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11639 case elfcpp::R_POWERPC_GOT_TLSGD16:
11640 case elfcpp::R_POWERPC_GOT_TLSLD16:
11641 case elfcpp::R_POWERPC_GOT_TPREL16:
11642 case elfcpp::R_POWERPC_GOT_DTPREL16:
11643 overflow = Reloc::CHECK_LOW_INSN;
11644 break;
11645
32f59844
AM
11646 case elfcpp::R_PPC64_REL24_NOTOC:
11647 if (size == 32)
11648 break;
11649 // Fall through.
b80eed39
AM
11650 case elfcpp::R_POWERPC_ADDR24:
11651 case elfcpp::R_POWERPC_ADDR14:
11652 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11653 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11654 case elfcpp::R_PPC64_ADDR16_DS:
11655 case elfcpp::R_POWERPC_REL24:
11656 case elfcpp::R_PPC_PLTREL24:
11657 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
11658 case elfcpp::R_PPC64_TPREL16_DS:
11659 case elfcpp::R_PPC64_DTPREL16_DS:
11660 case elfcpp::R_PPC64_TOC16_DS:
11661 case elfcpp::R_PPC64_GOT16_DS:
11662 case elfcpp::R_PPC64_SECTOFF_DS:
11663 case elfcpp::R_POWERPC_REL14:
11664 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11665 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11666 case elfcpp::R_PPC64_D34:
11667 case elfcpp::R_PPC64_PCREL34:
11668 case elfcpp::R_PPC64_GOT_PCREL34:
11669 case elfcpp::R_PPC64_PLT_PCREL34:
11670 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11671 case elfcpp::R_PPC64_D28:
11672 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
11673 case elfcpp::R_PPC64_TPREL34:
11674 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
11675 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11676 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11677 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11678 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
f4baf0d4 11679 overflow = Reloc::CHECK_SIGNED;
42cacb20 11680 break;
dd93cd0a 11681 }
42cacb20 11682
dcfc7dd4 11683 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
11684 Insn insn = 0;
11685
b80eed39
AM
11686 if (overflow == Reloc::CHECK_LOW_INSN
11687 || overflow == Reloc::CHECK_HIGH_INSN)
11688 {
a680de9a 11689 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 11690
a47622ac
AM
11691 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11692 overflow = Reloc::CHECK_BITFIELD;
11693 else if (overflow == Reloc::CHECK_LOW_INSN
11694 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11695 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11696 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11697 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11698 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11699 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 11700 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
11701 else
11702 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
11703 }
11704
a680de9a 11705 bool maybe_dq_reloc = false;
3ea0a085 11706 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 11707 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
11708 switch (r_type)
11709 {
11710 case elfcpp::R_POWERPC_NONE:
11711 case elfcpp::R_POWERPC_TLS:
11712 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11713 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
11714 case elfcpp::R_POWERPC_PLTSEQ:
11715 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
11716 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11717 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 11718 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
11719 break;
11720
11721 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 11722 case elfcpp::R_PPC64_REL64:
cf43a2fe 11723 case elfcpp::R_PPC64_TOC:
45965137 11724 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
11725 Reloc::addr64(view, value);
11726 break;
11727
11728 case elfcpp::R_POWERPC_TPREL:
11729 case elfcpp::R_POWERPC_DTPREL:
11730 if (size == 64)
11731 Reloc::addr64(view, value);
11732 else
3ea0a085 11733 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11734 break;
11735
11736 case elfcpp::R_PPC64_UADDR64:
11737 Reloc::addr64_u(view, value);
42cacb20
DE
11738 break;
11739
11740 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 11741 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11742 break;
11743
acc276d8 11744 case elfcpp::R_POWERPC_REL32:
dd93cd0a 11745 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 11746 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
11747 break;
11748
32f59844
AM
11749 case elfcpp::R_PPC64_REL24_NOTOC:
11750 if (size == 32)
11751 goto unsupp; // R_PPC_EMB_RELSDA
11752 // Fall through.
dd93cd0a
AM
11753 case elfcpp::R_POWERPC_ADDR24:
11754 case elfcpp::R_POWERPC_REL24:
11755 case elfcpp::R_PPC_PLTREL24:
11756 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 11757 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
11758 break;
11759
dd93cd0a
AM
11760 case elfcpp::R_POWERPC_GOT_DTPREL16:
11761 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
11762 case elfcpp::R_POWERPC_GOT_TPREL16:
11763 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
11764 if (size == 64)
11765 {
ec86f434 11766 // On ppc64 these are all ds form
a680de9a 11767 maybe_dq_reloc = true;
dd93cd0a
AM
11768 break;
11769 }
c25aa1e1 11770 // Fall through.
cf43a2fe 11771 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 11772 case elfcpp::R_POWERPC_REL16:
cf43a2fe 11773 case elfcpp::R_PPC64_TOC16:
42cacb20 11774 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 11775 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
11776 case elfcpp::R_POWERPC_TPREL16:
11777 case elfcpp::R_POWERPC_DTPREL16:
11778 case elfcpp::R_POWERPC_GOT_TLSGD16:
11779 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 11780 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 11781 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 11782 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 11783 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 11784 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 11785 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
11786 case elfcpp::R_POWERPC_TPREL16_LO:
11787 case elfcpp::R_POWERPC_DTPREL16_LO:
11788 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11789 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
11790 if (size == 64)
11791 status = Reloc::addr16(view, value, overflow);
11792 else
11793 maybe_dq_reloc = true;
dd93cd0a
AM
11794 break;
11795
11796 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 11797 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
11798 break;
11799
f9c6b907
AM
11800 case elfcpp::R_PPC64_ADDR16_HIGH:
11801 case elfcpp::R_PPC64_TPREL16_HIGH:
11802 case elfcpp::R_PPC64_DTPREL16_HIGH:
11803 if (size == 32)
11804 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11805 goto unsupp;
d8e90251 11806 // Fall through.
cf43a2fe 11807 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 11808 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 11809 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 11810 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 11811 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 11812 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 11813 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
11814 case elfcpp::R_POWERPC_TPREL16_HI:
11815 case elfcpp::R_POWERPC_DTPREL16_HI:
11816 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11817 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11818 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11819 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11820 Reloc::addr16_hi(view, value);
42cacb20
DE
11821 break;
11822
f9c6b907
AM
11823 case elfcpp::R_PPC64_ADDR16_HIGHA:
11824 case elfcpp::R_PPC64_TPREL16_HIGHA:
11825 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11826 if (size == 32)
11827 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11828 goto unsupp;
d8e90251 11829 // Fall through.
cf43a2fe 11830 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 11831 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 11832 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 11833 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 11834 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 11835 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 11836 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
11837 case elfcpp::R_POWERPC_TPREL16_HA:
11838 case elfcpp::R_POWERPC_DTPREL16_HA:
11839 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11840 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11841 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11842 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11843 Reloc::addr16_ha(view, value);
42cacb20
DE
11844 break;
11845
a680de9a
PB
11846 case elfcpp::R_POWERPC_REL16DX_HA:
11847 status = Reloc::addr16dx_ha(view, value, overflow);
11848 break;
11849
dd93cd0a
AM
11850 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11851 if (size == 32)
11852 // R_PPC_EMB_NADDR16_LO
11853 goto unsupp;
d8e90251 11854 // Fall through.
dd93cd0a 11855 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 11856 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
11857 case elfcpp::R_PPC64_TPREL16_HIGHER:
11858 Reloc::addr16_hi2(view, value);
42cacb20
DE
11859 break;
11860
dd93cd0a
AM
11861 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11862 if (size == 32)
11863 // R_PPC_EMB_NADDR16_HI
11864 goto unsupp;
d8e90251 11865 // Fall through.
dd93cd0a 11866 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 11867 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
11868 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11869 Reloc::addr16_ha2(view, value);
42cacb20
DE
11870 break;
11871
dd93cd0a
AM
11872 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11873 if (size == 32)
11874 // R_PPC_EMB_NADDR16_HA
11875 goto unsupp;
d8e90251 11876 // Fall through.
dd93cd0a 11877 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 11878 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
11879 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11880 Reloc::addr16_hi3(view, value);
42cacb20
DE
11881 break;
11882
dd93cd0a
AM
11883 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11884 if (size == 32)
11885 // R_PPC_EMB_SDAI16
11886 goto unsupp;
d8e90251 11887 // Fall through.
dd93cd0a 11888 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 11889 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11890 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11891 Reloc::addr16_ha3(view, value);
11892 break;
11893
11894 case elfcpp::R_PPC64_DTPREL16_DS:
11895 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11896 if (size == 32)
11897 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11898 goto unsupp;
d8e90251 11899 // Fall through.
dd93cd0a
AM
11900 case elfcpp::R_PPC64_TPREL16_DS:
11901 case elfcpp::R_PPC64_TPREL16_LO_DS:
11902 if (size == 32)
11903 // R_PPC_TLSGD, R_PPC_TLSLD
11904 break;
d8e90251 11905 // Fall through.
cf43a2fe
AM
11906 case elfcpp::R_PPC64_ADDR16_DS:
11907 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
11908 case elfcpp::R_PPC64_TOC16_DS:
11909 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
11910 case elfcpp::R_PPC64_GOT16_DS:
11911 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 11912 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
11913 case elfcpp::R_PPC64_SECTOFF_DS:
11914 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 11915 maybe_dq_reloc = true;
dd93cd0a
AM
11916 break;
11917
11918 case elfcpp::R_POWERPC_ADDR14:
11919 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11920 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11921 case elfcpp::R_POWERPC_REL14:
11922 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11923 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 11924 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
11925 break;
11926
11927 case elfcpp::R_POWERPC_COPY:
11928 case elfcpp::R_POWERPC_GLOB_DAT:
11929 case elfcpp::R_POWERPC_JMP_SLOT:
11930 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 11931 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
11932 case elfcpp::R_PPC64_JMP_IREL:
11933 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
11934 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11935 _("unexpected reloc %u in object file"),
11936 r_type);
11937 break;
11938
7e57d19e 11939 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 11940 if (size == 32)
7e57d19e 11941 // R_PPC_EMB_SDA21
dd93cd0a
AM
11942 goto unsupp;
11943 else
11944 {
7e57d19e
AM
11945 Symbol_location loc;
11946 loc.object = relinfo->object;
11947 loc.shndx = relinfo->data_shndx;
11948 loc.offset = rela.get_r_offset();
b0d0d02b
AM
11949 const Tocsave_loc *tocsave = target->tocsave_loc();
11950 if (tocsave->find(loc) != tocsave->end())
7e57d19e
AM
11951 {
11952 // If we've generated plt calls using this tocsave, then
11953 // the nop needs to be changed to save r2.
11954 Insn* iview = reinterpret_cast<Insn*>(view);
11955 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11956 elfcpp::Swap<32, big_endian>::
11957 writeval(iview, std_2_1 + target->stk_toc());
11958 }
dd93cd0a
AM
11959 }
11960 break;
11961
11962 case elfcpp::R_PPC_EMB_SDA2I16:
11963 case elfcpp::R_PPC_EMB_SDA2REL:
11964 if (size == 32)
11965 goto unsupp;
11966 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
11967 break;
11968
e4dff765
AM
11969 case elfcpp::R_PPC64_D34:
11970 case elfcpp::R_PPC64_D34_LO:
11971 case elfcpp::R_PPC64_PCREL34:
11972 case elfcpp::R_PPC64_GOT_PCREL34:
11973 case elfcpp::R_PPC64_PLT_PCREL34:
11974 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
11975 case elfcpp::R_PPC64_TPREL34:
11976 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
11977 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11978 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11979 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11980 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11981 if (size == 32)
11982 goto unsupp;
11983 status = Reloc::addr34(view, value, overflow);
11984 break;
11985
11986 case elfcpp::R_PPC64_D34_HI30:
11987 if (size == 32)
11988 goto unsupp;
11989 Reloc::addr34_hi(view, value);
11990 break;
11991
11992 case elfcpp::R_PPC64_D34_HA30:
11993 if (size == 32)
11994 goto unsupp;
11995 Reloc::addr34_ha(view, value);
11996 break;
11997
11998 case elfcpp::R_PPC64_D28:
11999 case elfcpp::R_PPC64_PCREL28:
12000 if (size == 32)
12001 goto unsupp;
12002 status = Reloc::addr28(view, value, overflow);
12003 break;
12004
12005 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12006 case elfcpp::R_PPC64_REL16_HIGHER34:
12007 if (size == 32)
12008 goto unsupp;
12009 Reloc::addr16_higher34(view, value);
12010 break;
12011
12012 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12013 case elfcpp::R_PPC64_REL16_HIGHERA34:
12014 if (size == 32)
12015 goto unsupp;
12016 Reloc::addr16_highera34(view, value);
12017 break;
12018
12019 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12020 case elfcpp::R_PPC64_REL16_HIGHEST34:
12021 if (size == 32)
12022 goto unsupp;
12023 Reloc::addr16_highest34(view, value);
12024 break;
12025
12026 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12027 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12028 if (size == 32)
12029 goto unsupp;
12030 Reloc::addr16_highesta34(view, value);
12031 break;
12032
dd93cd0a
AM
12033 case elfcpp::R_POWERPC_PLT32:
12034 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
12035 case elfcpp::R_PPC_SDAREL16:
12036 case elfcpp::R_POWERPC_ADDR30:
12037 case elfcpp::R_PPC64_PLT64:
12038 case elfcpp::R_PPC64_PLTREL64:
12039 case elfcpp::R_PPC64_PLTGOT16:
12040 case elfcpp::R_PPC64_PLTGOT16_LO:
12041 case elfcpp::R_PPC64_PLTGOT16_HI:
12042 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
12043 case elfcpp::R_PPC64_PLTGOT16_DS:
12044 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 12045 case elfcpp::R_PPC_TOC16:
42cacb20 12046 default:
dd93cd0a 12047 unsupp:
42cacb20
DE
12048 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12049 _("unsupported reloc %u"),
12050 r_type);
12051 break;
12052 }
a680de9a
PB
12053
12054 if (maybe_dq_reloc)
12055 {
12056 if (insn == 0)
12057 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12058
12059 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12060 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12061 && (insn & 3) == 1))
12062 status = Reloc::addr16_dq(view, value, overflow);
12063 else if (size == 64
12064 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12065 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12066 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12067 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12068 status = Reloc::addr16_ds(view, value, overflow);
12069 else
12070 status = Reloc::addr16(view, value, overflow);
12071 }
12072
0cfb0717 12073 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
12074 && (has_stub_value
12075 || !(gsym != NULL
282c9750 12076 && gsym->is_undefined()
32f59844 12077 && is_branch_reloc<size>(r_type))))
0cfb0717
AM
12078 {
12079 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12080 _("relocation overflow"));
12081 if (has_stub_value)
12082 gold_info(_("try relinking with a smaller --stub-group-size"));
12083 }
42cacb20
DE
12084
12085 return true;
12086}
12087
42cacb20
DE
12088// Relocate section data.
12089
12090template<int size, bool big_endian>
12091void
12092Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
12093 const Relocate_info<size, big_endian>* relinfo,
12094 unsigned int sh_type,
12095 const unsigned char* prelocs,
12096 size_t reloc_count,
12097 Output_section* output_section,
12098 bool needs_special_offset_handling,
12099 unsigned char* view,
c9269dff 12100 Address address,
d83ce4e3
AM
12101 section_size_type view_size,
12102 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
12103{
12104 typedef Target_powerpc<size, big_endian> Powerpc;
12105 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
12106 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12107 Powerpc_comdat_behavior;
4d625b70
CC
12108 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12109 Classify_reloc;
42cacb20
DE
12110
12111 gold_assert(sh_type == elfcpp::SHT_RELA);
12112
4d625b70
CC
12113 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12114 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
12115 relinfo,
12116 this,
12117 prelocs,
12118 reloc_count,
12119 output_section,
12120 needs_special_offset_handling,
12121 view,
12122 address,
364c7fa5
ILT
12123 view_size,
12124 reloc_symbol_changes);
42cacb20
DE
12125}
12126
4d625b70 12127template<int size, bool big_endian>
cf43a2fe 12128class Powerpc_scan_relocatable_reloc
42cacb20 12129{
cf43a2fe 12130public:
0e123f69
AM
12131 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12132 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
12133 static const int sh_type = elfcpp::SHT_RELA;
12134
12135 // Return the symbol referred to by the relocation.
12136 static inline unsigned int
12137 get_r_sym(const Reltype* reloc)
12138 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12139
12140 // Return the type of the relocation.
12141 static inline unsigned int
12142 get_r_type(const Reltype* reloc)
12143 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12144
cf43a2fe
AM
12145 // Return the strategy to use for a local symbol which is not a
12146 // section symbol, given the relocation type.
12147 inline Relocatable_relocs::Reloc_strategy
12148 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12149 {
12150 if (r_type == 0 && r_sym == 0)
12151 return Relocatable_relocs::RELOC_DISCARD;
12152 return Relocatable_relocs::RELOC_COPY;
12153 }
12154
12155 // Return the strategy to use for a local symbol which is a section
12156 // symbol, given the relocation type.
12157 inline Relocatable_relocs::Reloc_strategy
12158 local_section_strategy(unsigned int, Relobj*)
12159 {
12160 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12161 }
12162
12163 // Return the strategy to use for a global symbol, given the
12164 // relocation type, the object, and the symbol index.
12165 inline Relocatable_relocs::Reloc_strategy
12166 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12167 {
08be3224
AM
12168 if (size == 32
12169 && (r_type == elfcpp::R_PPC_PLTREL24
12170 || r_type == elfcpp::R_POWERPC_PLT16_LO
12171 || r_type == elfcpp::R_POWERPC_PLT16_HI
12172 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
12173 return Relocatable_relocs::RELOC_SPECIAL;
12174 return Relocatable_relocs::RELOC_COPY;
12175 }
12176};
42cacb20
DE
12177
12178// Scan the relocs during a relocatable link.
12179
12180template<int size, bool big_endian>
12181void
12182Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
12183 Symbol_table* symtab,
12184 Layout* layout,
12185 Sized_relobj_file<size, big_endian>* object,
12186 unsigned int data_shndx,
12187 unsigned int sh_type,
12188 const unsigned char* prelocs,
12189 size_t reloc_count,
12190 Output_section* output_section,
12191 bool needs_special_offset_handling,
12192 size_t local_symbol_count,
12193 const unsigned char* plocal_symbols,
12194 Relocatable_relocs* rr)
42cacb20 12195{
4d625b70
CC
12196 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12197
42cacb20
DE
12198 gold_assert(sh_type == elfcpp::SHT_RELA);
12199
4d625b70 12200 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
12201 symtab,
12202 layout,
12203 object,
12204 data_shndx,
12205 prelocs,
12206 reloc_count,
12207 output_section,
12208 needs_special_offset_handling,
12209 local_symbol_count,
12210 plocal_symbols,
12211 rr);
12212}
12213
4d625b70
CC
12214// Scan the relocs for --emit-relocs.
12215
12216template<int size, bool big_endian>
12217void
12218Target_powerpc<size, big_endian>::emit_relocs_scan(
12219 Symbol_table* symtab,
12220 Layout* layout,
12221 Sized_relobj_file<size, big_endian>* object,
12222 unsigned int data_shndx,
12223 unsigned int sh_type,
12224 const unsigned char* prelocs,
12225 size_t reloc_count,
12226 Output_section* output_section,
12227 bool needs_special_offset_handling,
12228 size_t local_symbol_count,
12229 const unsigned char* plocal_syms,
12230 Relocatable_relocs* rr)
12231{
12232 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12233 Classify_reloc;
12234 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12235 Emit_relocs_strategy;
12236
12237 gold_assert(sh_type == elfcpp::SHT_RELA);
12238
12239 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12240 symtab,
12241 layout,
12242 object,
12243 data_shndx,
12244 prelocs,
12245 reloc_count,
12246 output_section,
12247 needs_special_offset_handling,
12248 local_symbol_count,
12249 plocal_syms,
12250 rr);
12251}
12252
7404fe1b 12253// Emit relocations for a section.
dd93cd0a
AM
12254// This is a modified version of the function by the same name in
12255// target-reloc.h. Using relocate_special_relocatable for
12256// R_PPC_PLTREL24 would require duplication of the entire body of the
12257// loop, so we may as well duplicate the whole thing.
42cacb20
DE
12258
12259template<int size, bool big_endian>
12260void
7404fe1b 12261Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
12262 const Relocate_info<size, big_endian>* relinfo,
12263 unsigned int sh_type,
12264 const unsigned char* prelocs,
12265 size_t reloc_count,
12266 Output_section* output_section,
62fe925a 12267 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 12268 unsigned char*,
dd93cd0a 12269 Address view_address,
cf43a2fe 12270 section_size_type,
42cacb20
DE
12271 unsigned char* reloc_view,
12272 section_size_type reloc_view_size)
12273{
12274 gold_assert(sh_type == elfcpp::SHT_RELA);
12275
0e123f69
AM
12276 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12277 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12278 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
12279 // Offset from start of insn to d-field reloc.
12280 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
12281
12282 Powerpc_relobj<size, big_endian>* const object
12283 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12284 const unsigned int local_count = object->local_symbol_count();
12285 unsigned int got2_shndx = object->got2_shndx();
c9269dff 12286 Address got2_addend = 0;
cf43a2fe 12287 if (got2_shndx != 0)
c9269dff
AM
12288 {
12289 got2_addend = object->get_output_section_offset(got2_shndx);
12290 gold_assert(got2_addend != invalid_address);
12291 }
cf43a2fe 12292
033bfb73
CC
12293 const bool relocatable = parameters->options().relocatable();
12294
cf43a2fe 12295 unsigned char* pwrite = reloc_view;
7404fe1b 12296 bool zap_next = false;
cf43a2fe
AM
12297 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12298 {
91a65d2f 12299 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
12300 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12301 continue;
12302
12303 Reltype reloc(prelocs);
12304 Reltype_write reloc_write(pwrite);
12305
7404fe1b 12306 Address offset = reloc.get_r_offset();
cf43a2fe 12307 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
12308 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12309 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12310 const unsigned int orig_r_sym = r_sym;
12311 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12312 = reloc.get_r_addend();
12313 const Symbol* gsym = NULL;
12314
12315 if (zap_next)
12316 {
12317 // We could arrange to discard these and other relocs for
12318 // tls optimised sequences in the strategy methods, but for
12319 // now do as BFD ld does.
12320 r_type = elfcpp::R_POWERPC_NONE;
12321 zap_next = false;
12322 }
cf43a2fe
AM
12323
12324 // Get the new symbol index.
9215b98b 12325 Output_section* os = NULL;
cf43a2fe
AM
12326 if (r_sym < local_count)
12327 {
12328 switch (strategy)
12329 {
12330 case Relocatable_relocs::RELOC_COPY:
12331 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 12332 if (r_sym != 0)
dd93cd0a 12333 {
7404fe1b
AM
12334 r_sym = object->symtab_index(r_sym);
12335 gold_assert(r_sym != -1U);
dd93cd0a 12336 }
cf43a2fe
AM
12337 break;
12338
12339 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12340 {
12341 // We are adjusting a section symbol. We need to find
12342 // the symbol table index of the section symbol for
12343 // the output section corresponding to input section
12344 // in which this symbol is defined.
12345 gold_assert(r_sym < local_count);
12346 bool is_ordinary;
12347 unsigned int shndx =
12348 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12349 gold_assert(is_ordinary);
9215b98b 12350 os = object->output_section(shndx);
cf43a2fe
AM
12351 gold_assert(os != NULL);
12352 gold_assert(os->needs_symtab_index());
7404fe1b 12353 r_sym = os->symtab_index();
cf43a2fe
AM
12354 }
12355 break;
12356
12357 default:
12358 gold_unreachable();
12359 }
12360 }
12361 else
12362 {
7404fe1b 12363 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
12364 gold_assert(gsym != NULL);
12365 if (gsym->is_forwarder())
12366 gsym = relinfo->symtab->resolve_forwards(gsym);
12367
12368 gold_assert(gsym->has_symtab_index());
7404fe1b 12369 r_sym = gsym->symtab_index();
cf43a2fe
AM
12370 }
12371
12372 // Get the new offset--the location in the output section where
12373 // this relocation should be applied.
cf43a2fe 12374 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12375 offset += offset_in_output_section;
cf43a2fe
AM
12376 else
12377 {
c9269dff
AM
12378 section_offset_type sot_offset =
12379 convert_types<section_offset_type, Address>(offset);
cf43a2fe 12380 section_offset_type new_sot_offset =
c9269dff
AM
12381 output_section->output_offset(object, relinfo->data_shndx,
12382 sot_offset);
cf43a2fe 12383 gold_assert(new_sot_offset != -1);
7404fe1b 12384 offset = new_sot_offset;
cf43a2fe
AM
12385 }
12386
dd93cd0a
AM
12387 // In an object file, r_offset is an offset within the section.
12388 // In an executable or dynamic object, generated by
12389 // --emit-relocs, r_offset is an absolute address.
033bfb73 12390 if (!relocatable)
dd93cd0a 12391 {
7404fe1b 12392 offset += view_address;
dd93cd0a 12393 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12394 offset -= offset_in_output_section;
dd93cd0a
AM
12395 }
12396
cf43a2fe 12397 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12398 if (strategy == Relocatable_relocs::RELOC_COPY)
12399 ;
12400 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12401 {
7404fe1b 12402 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12403 addend = psymval->value(object, addend);
12404 // In a relocatable link, the symbol value is relative to
12405 // the start of the output section. For a non-relocatable
12406 // link, we need to adjust the addend.
12407 if (!relocatable)
12408 {
12409 gold_assert(os != NULL);
12410 addend -= os->address();
12411 }
cf43a2fe
AM
12412 }
12413 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12414 {
e3a7574e
AM
12415 if (size == 32)
12416 {
12417 if (addend >= 32768)
12418 addend += got2_addend;
12419 }
12420 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12421 {
12422 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12423 addend -= d_offset;
e3a7574e
AM
12424 }
12425 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12426 {
12427 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12428 addend -= d_offset + 4;
e3a7574e 12429 }
cf43a2fe
AM
12430 }
12431 else
12432 gold_unreachable();
12433
033bfb73 12434 if (!relocatable)
7404fe1b
AM
12435 {
12436 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12437 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12438 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12439 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12440 {
12441 // First instruction of a global dynamic sequence,
12442 // arg setup insn.
12443 const bool final = gsym == NULL || gsym->final_value_is_known();
12444 switch (this->optimize_tls_gd(final))
12445 {
12446 case tls::TLSOPT_TO_IE:
12447 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12448 - elfcpp::R_POWERPC_GOT_TLSGD16);
12449 break;
12450 case tls::TLSOPT_TO_LE:
12451 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12452 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12453 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12454 else
12455 {
12456 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12457 offset -= d_offset;
7404fe1b
AM
12458 }
12459 break;
12460 default:
12461 break;
12462 }
12463 }
12464 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12465 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12466 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12467 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12468 {
12469 // First instruction of a local dynamic sequence,
12470 // arg setup insn.
12471 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12472 {
12473 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12474 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12475 {
12476 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12477 const Output_section* os = relinfo->layout->tls_segment()
12478 ->first_section();
12479 gold_assert(os != NULL);
12480 gold_assert(os->needs_symtab_index());
12481 r_sym = os->symtab_index();
12482 addend = dtp_offset;
12483 }
12484 else
12485 {
12486 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12487 offset -= d_offset;
7404fe1b
AM
12488 }
12489 }
12490 }
12491 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12492 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12493 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12494 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12495 {
12496 // First instruction of initial exec sequence.
12497 const bool final = gsym == NULL || gsym->final_value_is_known();
12498 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12499 {
12500 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12501 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12502 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12503 else
12504 {
12505 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12506 offset -= d_offset;
7404fe1b
AM
12507 }
12508 }
12509 }
12510 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12511 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12512 {
12513 // Second instruction of a global dynamic sequence,
12514 // the __tls_get_addr call
12515 const bool final = gsym == NULL || gsym->final_value_is_known();
12516 switch (this->optimize_tls_gd(final))
12517 {
12518 case tls::TLSOPT_TO_IE:
12519 r_type = elfcpp::R_POWERPC_NONE;
12520 zap_next = true;
12521 break;
12522 case tls::TLSOPT_TO_LE:
12523 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12524 offset += d_offset;
7404fe1b
AM
12525 zap_next = true;
12526 break;
12527 default:
12528 break;
12529 }
12530 }
12531 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12532 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12533 {
12534 // Second instruction of a local dynamic sequence,
12535 // the __tls_get_addr call
12536 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12537 {
12538 const Output_section* os = relinfo->layout->tls_segment()
12539 ->first_section();
12540 gold_assert(os != NULL);
12541 gold_assert(os->needs_symtab_index());
12542 r_sym = os->symtab_index();
12543 addend = dtp_offset;
12544 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12545 offset += d_offset;
7404fe1b
AM
12546 zap_next = true;
12547 }
12548 }
12549 else if (r_type == elfcpp::R_POWERPC_TLS)
12550 {
12551 // Second instruction of an initial exec sequence
12552 const bool final = gsym == NULL || gsym->final_value_is_known();
12553 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12554 {
12555 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12556 offset += d_offset;
7404fe1b
AM
12557 }
12558 }
12559 }
12560
12561 reloc_write.put_r_offset(offset);
12562 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12563 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12564
12565 pwrite += reloc_size;
12566 }
12567
12568 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12569 == reloc_view_size);
42cacb20
DE
12570}
12571
ec661b9d 12572// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12573// treatment. This is how we support equality comparisons of function
12574// pointers across shared library boundaries, as described in the
12575// processor specific ABI supplement.
12576
12577template<int size, bool big_endian>
12578uint64_t
12579Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12580{
cf43a2fe
AM
12581 if (size == 32)
12582 {
12583 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12584 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12585 p != this->stub_tables_.end();
12586 ++p)
12587 {
7e57d19e
AM
12588 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12589 = (*p)->find_plt_call_entry(gsym);
12590 if (ent != NULL)
12591 return (*p)->stub_address() + ent->off_;
ec661b9d 12592 }
c9824451 12593 }
9055360d
AM
12594 else if (this->abiversion() >= 2)
12595 {
faa2211d
AM
12596 Address off = this->glink_section()->find_global_entry(gsym);
12597 if (off != invalid_address)
9055360d
AM
12598 return this->glink_section()->global_entry_address() + off;
12599 }
ec661b9d 12600 gold_unreachable();
c9824451
AM
12601}
12602
12603// Return the PLT address to use for a local symbol.
12604template<int size, bool big_endian>
12605uint64_t
12606Target_powerpc<size, big_endian>::do_plt_address_for_local(
12607 const Relobj* object,
12608 unsigned int symndx) const
12609{
12610 if (size == 32)
12611 {
12612 const Sized_relobj<size, big_endian>* relobj
12613 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12614 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12615 p != this->stub_tables_.end();
12616 ++p)
12617 {
7e57d19e
AM
12618 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12619 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12620 if (ent != NULL)
12621 return (*p)->stub_address() + ent->off_;
ec661b9d 12622 }
c9824451 12623 }
ec661b9d 12624 gold_unreachable();
c9824451
AM
12625}
12626
12627// Return the PLT address to use for a global symbol.
12628template<int size, bool big_endian>
12629uint64_t
12630Target_powerpc<size, big_endian>::do_plt_address_for_global(
12631 const Symbol* gsym) const
12632{
12633 if (size == 32)
12634 {
ec661b9d
AM
12635 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12636 p != this->stub_tables_.end();
12637 ++p)
12638 {
7e57d19e
AM
12639 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12640 = (*p)->find_plt_call_entry(gsym);
12641 if (ent != NULL)
12642 return (*p)->stub_address() + ent->off_;
ec661b9d 12643 }
cf43a2fe 12644 }
9055360d
AM
12645 else if (this->abiversion() >= 2)
12646 {
faa2211d
AM
12647 Address off = this->glink_section()->find_global_entry(gsym);
12648 if (off != invalid_address)
9055360d
AM
12649 return this->glink_section()->global_entry_address() + off;
12650 }
ec661b9d 12651 gold_unreachable();
42cacb20
DE
12652}
12653
bd73a62d
AM
12654// Return the offset to use for the GOT_INDX'th got entry which is
12655// for a local tls symbol specified by OBJECT, SYMNDX.
12656template<int size, bool big_endian>
12657int64_t
12658Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12659 const Relobj* object,
12660 unsigned int symndx,
12661 unsigned int got_indx) const
12662{
12663 const Powerpc_relobj<size, big_endian>* ppc_object
12664 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12665 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12666 {
12667 for (Got_type got_type = GOT_TYPE_TLSGD;
12668 got_type <= GOT_TYPE_TPREL;
12669 got_type = Got_type(got_type + 1))
12670 if (ppc_object->local_has_got_offset(symndx, got_type))
12671 {
12672 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12673 if (got_type == GOT_TYPE_TLSGD)
12674 off += size / 8;
12675 if (off == got_indx * (size / 8))
12676 {
12677 if (got_type == GOT_TYPE_TPREL)
12678 return -tp_offset;
12679 else
12680 return -dtp_offset;
12681 }
12682 }
12683 }
12684 gold_unreachable();
12685}
12686
12687// Return the offset to use for the GOT_INDX'th got entry which is
12688// for global tls symbol GSYM.
12689template<int size, bool big_endian>
12690int64_t
12691Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12692 Symbol* gsym,
12693 unsigned int got_indx) const
12694{
12695 if (gsym->type() == elfcpp::STT_TLS)
12696 {
12697 for (Got_type got_type = GOT_TYPE_TLSGD;
12698 got_type <= GOT_TYPE_TPREL;
12699 got_type = Got_type(got_type + 1))
12700 if (gsym->has_got_offset(got_type))
12701 {
12702 unsigned int off = gsym->got_offset(got_type);
12703 if (got_type == GOT_TYPE_TLSGD)
12704 off += size / 8;
12705 if (off == got_indx * (size / 8))
12706 {
12707 if (got_type == GOT_TYPE_TPREL)
12708 return -tp_offset;
12709 else
12710 return -dtp_offset;
12711 }
12712 }
12713 }
12714 gold_unreachable();
12715}
12716
42cacb20
DE
12717// The selector for powerpc object files.
12718
12719template<int size, bool big_endian>
12720class Target_selector_powerpc : public Target_selector
12721{
12722public:
12723 Target_selector_powerpc()
edc27beb
AM
12724 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12725 size, big_endian,
03ef7571
ILT
12726 (size == 64
12727 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12728 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12729 (size == 64
12730 ? (big_endian ? "elf64ppc" : "elf64lppc")
12731 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
12732 { }
12733
2e702c99
RM
12734 virtual Target*
12735 do_instantiate_target()
7f055c20 12736 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
12737};
12738
12739Target_selector_powerpc<32, true> target_selector_ppc32;
12740Target_selector_powerpc<32, false> target_selector_ppc32le;
12741Target_selector_powerpc<64, true> target_selector_ppc64;
12742Target_selector_powerpc<64, false> target_selector_ppc64le;
12743
decdd3bc
AM
12744// Instantiate these constants for -O0
12745template<int size, bool big_endian>
9055360d
AM
12746const typename Output_data_glink<size, big_endian>::Address
12747 Output_data_glink<size, big_endian>::invalid_address;
12748template<int size, bool big_endian>
decdd3bc
AM
12749const typename Stub_table<size, big_endian>::Address
12750 Stub_table<size, big_endian>::invalid_address;
12751template<int size, bool big_endian>
12752const typename Target_powerpc<size, big_endian>::Address
12753 Target_powerpc<size, big_endian>::invalid_address;
12754
42cacb20 12755} // End anonymous namespace.
This page took 1.449549 seconds and 4 git commands to generate.