]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/powerpc.cc
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
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;
fa40fbe4 3539 unsigned int other;
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;
fa40fbe4 3567 other = gsym->nonvis() >> 3;
ec661b9d
AM
3568 }
3569 else
3570 {
3571 const Symbol_value<size>* psymval
3572 = this->object_->local_symbol(this->r_sym_);
3573 Symbol_value<size> symval;
0f125432
CC
3574 if (psymval->is_section_symbol())
3575 symval.set_is_section_symbol();
ec661b9d
AM
3576 typedef Sized_relobj_file<size, big_endian> ObjType;
3577 typename ObjType::Compute_final_local_value_status status
3578 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3579 &symval, symtab);
3580 if (status != ObjType::CFLV_OK
3581 || !symval.has_output_value())
a3e60ddb 3582 return true;
ec661b9d 3583 to = symval.value(this->object_, 0);
fa40fbe4 3584 other = this->object_->st_other(this->r_sym_) >> 5;
ec661b9d 3585 }
cbcb23fa
AM
3586 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3587 to += this->addend_;
ec661b9d
AM
3588 if (stub_table == NULL)
3589 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3590 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3591 {
3592 unsigned int dest_shndx;
1611bc4a
AM
3593 if (!target->symval_for_branch(symtab, gsym, this->object_,
3594 &to, &dest_shndx))
3595 return true;
ec661b9d 3596 }
fa40fbe4
AM
3597 unsigned int local_ent = 0;
3598 if (size == 64
3599 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC)
3600 local_ent = elfcpp::ppc64_decode_local_entry(other);
3601 Address delta = to + local_ent - from;
32f59844
AM
3602 if (delta + max_branch_offset >= 2 * max_branch_offset
3603 || (size == 64
3604 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3605 && (gsym != NULL
3606 ? this->object_->ppc64_needs_toc(gsym)
3607 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3608 {
0cfdc767
AM
3609 if (stub_table == NULL)
3610 {
3611 gold_warning(_("%s:%s: branch in non-executable section,"
3612 " no long branch stub for you"),
3613 this->object_->name().c_str(),
3614 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3615 return true;
0cfdc767 3616 }
d49044c7
AM
3617 bool save_res = (size == 64
3618 && gsym != NULL
3619 && gsym->source() == Symbol::IN_OUTPUT_DATA
3620 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3621 ok = stub_table->add_long_branch_entry(this->object_,
3622 this->r_type_,
fa40fbe4 3623 from, to, other, save_res);
ec661b9d
AM
3624 }
3625 }
dc60b26d
AM
3626 if (!ok)
3627 gold_debug(DEBUG_TARGET,
3628 "branch at %s:%s+%#lx\n"
3629 "can't reach stub attached to %s:%s",
3630 this->object_->name().c_str(),
3631 this->object_->section_name(this->shndx_).c_str(),
3632 (unsigned long) this->offset_,
3633 stub_table->relobj()->name().c_str(),
3634 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3635
3636 return ok;
ec661b9d
AM
3637}
3638
3639// Relaxation hook. This is where we do stub generation.
3640
3641template<int size, bool big_endian>
3642bool
3643Target_powerpc<size, big_endian>::do_relax(int pass,
3644 const Input_objects*,
3645 Symbol_table* symtab,
3646 Layout* layout,
3647 const Task* task)
3648{
3649 unsigned int prev_brlt_size = 0;
3650 if (pass == 1)
ec661b9d 3651 {
b4f7960d
AM
3652 bool thread_safe
3653 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3654 if (size == 64
3655 && this->abiversion() < 2
3656 && !thread_safe
3657 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3658 {
e2458743 3659 static const char* const thread_starter[] =
9e69ed50
AM
3660 {
3661 "pthread_create",
3662 /* libstdc++ */
3663 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3664 /* librt */
3665 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3666 "mq_notify", "create_timer",
3667 /* libanl */
3668 "getaddrinfo_a",
3669 /* libgomp */
80272b8c 3670 "GOMP_parallel",
9e69ed50 3671 "GOMP_parallel_start",
80272b8c 3672 "GOMP_parallel_loop_static",
9e69ed50 3673 "GOMP_parallel_loop_static_start",
80272b8c 3674 "GOMP_parallel_loop_dynamic",
9e69ed50 3675 "GOMP_parallel_loop_dynamic_start",
80272b8c 3676 "GOMP_parallel_loop_guided",
9e69ed50 3677 "GOMP_parallel_loop_guided_start",
80272b8c 3678 "GOMP_parallel_loop_runtime",
9e69ed50 3679 "GOMP_parallel_loop_runtime_start",
80272b8c 3680 "GOMP_parallel_sections",
43819297 3681 "GOMP_parallel_sections_start",
f9dffbf0
AM
3682 /* libgo */
3683 "__go_go",
9e69ed50
AM
3684 };
3685
e2458743
AM
3686 if (parameters->options().shared())
3687 thread_safe = true;
3688 else
9e69ed50 3689 {
e2458743
AM
3690 for (unsigned int i = 0;
3691 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3692 i++)
3693 {
3694 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3695 thread_safe = (sym != NULL
3696 && sym->in_reg()
3697 && sym->in_real_elf());
3698 if (thread_safe)
3699 break;
3700 }
9e69ed50 3701 }
ec661b9d 3702 }
9e69ed50 3703 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3704 }
3705
3706 if (pass == 1)
3707 {
3708 this->stub_group_size_ = parameters->options().stub_group_size();
3709 bool no_size_errors = true;
3710 if (this->stub_group_size_ == 1)
3711 this->stub_group_size_ = 0x1c00000;
3712 else if (this->stub_group_size_ == -1)
3713 this->stub_group_size_ = -0x1e00000;
3714 else
3715 no_size_errors = false;
3716 this->group_sections(layout, task, no_size_errors);
3717 }
3718 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3719 {
3720 this->branch_lookup_table_.clear();
3721 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3722 p != this->stub_tables_.end();
3723 ++p)
3724 {
3725 (*p)->clear_stubs(true);
3726 }
3727 this->stub_tables_.clear();
3728 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3729 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3730 program_name, this->stub_group_size_);
3731 this->group_sections(layout, task, true);
ec661b9d
AM
3732 }
3733
3734 // We need address of stub tables valid for make_stub.
3735 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3736 p != this->stub_tables_.end();
3737 ++p)
3738 {
3739 const Powerpc_relobj<size, big_endian>* object
3740 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3741 Address off = object->get_output_section_offset((*p)->shndx());
3742 gold_assert(off != invalid_address);
3743 Output_section* os = (*p)->output_section();
3744 (*p)->set_address_and_size(os, off);
3745 }
3746
9e69ed50
AM
3747 if (pass != 1)
3748 {
3749 // Clear plt call stubs, long branch stubs and branch lookup table.
3750 prev_brlt_size = this->branch_lookup_table_.size();
3751 this->branch_lookup_table_.clear();
3752 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3753 p != this->stub_tables_.end();
3754 ++p)
3755 {
a3e60ddb 3756 (*p)->clear_stubs(false);
9e69ed50
AM
3757 }
3758 }
3759
3760 // Build all the stubs.
a3e60ddb 3761 this->relax_failed_ = false;
ec661b9d
AM
3762 Stub_table<size, big_endian>* ifunc_stub_table
3763 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3764 Stub_table<size, big_endian>* one_stub_table
3765 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3766 for (typename Branches::const_iterator b = this->branch_info_.begin();
3767 b != this->branch_info_.end();
3768 b++)
3769 {
a3e60ddb
AM
3770 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3771 && !this->relax_failed_)
3772 {
3773 this->relax_failed_ = true;
3774 this->relax_fail_count_++;
3775 if (this->relax_fail_count_ < 3)
3776 return true;
3777 }
ec661b9d 3778 }
32f59844
AM
3779 bool do_resize = false;
3780 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3781 p != this->stub_tables_.end();
3782 ++p)
3783 if ((*p)->need_resize())
3784 {
3785 do_resize = true;
3786 break;
3787 }
3788 if (do_resize)
3789 {
3790 this->branch_lookup_table_.clear();
3791 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3792 p != this->stub_tables_.end();
3793 ++p)
3794 (*p)->set_resizing(true);
3795 for (typename Branches::const_iterator b = this->branch_info_.begin();
3796 b != this->branch_info_.end();
3797 b++)
3798 {
3799 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3800 && !this->relax_failed_)
3801 {
3802 this->relax_failed_ = true;
3803 this->relax_fail_count_++;
3804 if (this->relax_fail_count_ < 3)
3805 return true;
3806 }
3807 }
3808 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3809 p != this->stub_tables_.end();
3810 ++p)
3811 (*p)->set_resizing(false);
3812 }
ec661b9d 3813
9e69ed50 3814 // Did anything change size?
ec661b9d
AM
3815 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3816 bool again = num_huge_branches != prev_brlt_size;
3817 if (size == 64 && num_huge_branches != 0)
3818 this->make_brlt_section(layout);
3819 if (size == 64 && again)
3820 this->brlt_section_->set_current_size(num_huge_branches);
3821
be897fb7
AM
3822 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3823 p != this->stub_tables_.rend();
3824 ++p)
3825 (*p)->remove_eh_frame(layout);
3826
3827 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3828 p != this->stub_tables_.end();
3829 ++p)
3830 (*p)->add_eh_frame(layout);
3831
ec661b9d
AM
3832 typedef Unordered_set<Output_section*> Output_sections;
3833 Output_sections os_need_update;
3834 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3835 p != this->stub_tables_.end();
3836 ++p)
3837 {
3838 if ((*p)->size_update())
3839 {
3840 again = true;
3841 os_need_update.insert((*p)->output_section());
3842 }
3843 }
3844
9e69ed50
AM
3845 // Set output section offsets for all input sections in an output
3846 // section that just changed size. Anything past the stubs will
3847 // need updating.
ec661b9d
AM
3848 for (typename Output_sections::iterator p = os_need_update.begin();
3849 p != os_need_update.end();
3850 p++)
3851 {
3852 Output_section* os = *p;
3853 Address off = 0;
3854 typedef Output_section::Input_section_list Input_section_list;
3855 for (Input_section_list::const_iterator i = os->input_sections().begin();
3856 i != os->input_sections().end();
3857 ++i)
3858 {
3859 off = align_address(off, i->addralign());
3860 if (i->is_input_section() || i->is_relaxed_input_section())
3861 i->relobj()->set_section_offset(i->shndx(), off);
3862 if (i->is_relaxed_input_section())
3863 {
3864 Stub_table<size, big_endian>* stub_table
3865 = static_cast<Stub_table<size, big_endian>*>(
3866 i->relaxed_input_section());
6395d38b
HS
3867 Address stub_table_size = stub_table->set_address_and_size(os, off);
3868 off += stub_table_size;
3869 // After a few iterations, set current stub table size
3870 // as min size threshold, so later stub tables can only
3871 // grow in size.
3872 if (pass >= 4)
3873 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3874 }
3875 else
3876 off += i->data_size();
3877 }
6830ee24
AM
3878 // If .branch_lt is part of this output section, then we have
3879 // just done the offset adjustment.
ec661b9d
AM
3880 os->clear_section_offsets_need_adjustment();
3881 }
3882
3883 if (size == 64
3884 && !again
3885 && num_huge_branches != 0
3886 && parameters->options().output_is_position_independent())
3887 {
3888 // Fill in the BRLT relocs.
06f30c9d 3889 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3890 for (typename Branch_lookup_table::const_iterator p
3891 = this->branch_lookup_table_.begin();
3892 p != this->branch_lookup_table_.end();
3893 ++p)
3894 {
3895 this->brlt_section_->add_reloc(p->first, p->second);
3896 }
06f30c9d 3897 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3898 }
590b87ff
AM
3899
3900 if (!again
3901 && (parameters->options().user_set_emit_stub_syms()
3902 ? parameters->options().emit_stub_syms()
3903 : (size == 64
3904 || parameters->options().output_is_position_independent()
3905 || parameters->options().emit_relocs())))
3906 {
3907 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3908 p != this->stub_tables_.end();
3909 ++p)
3910 (*p)->define_stub_syms(symtab);
3911
3912 if (this->glink_ != NULL)
3913 {
9e390558 3914 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3915 Address value = -stub_size;
3916 if (size == 64)
3917 {
3918 value = 8;
3919 stub_size -= 8;
3920 }
3921 this->define_local(symtab, "__glink_PLTresolve",
3922 this->glink_, value, stub_size);
3923
3924 if (size != 64)
3925 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3926 }
3927 }
3928
ec661b9d
AM
3929 return again;
3930}
3931
9d5781f8
AM
3932template<int size, bool big_endian>
3933void
3934Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3935 unsigned char* oview,
3936 uint64_t* paddress,
3937 off_t* plen) const
3938{
3939 uint64_t address = plt->address();
3940 off_t len = plt->data_size();
3941
3942 if (plt == this->glink_)
3943 {
3944 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3945 if (len == 0)
3946 {
3947 gold_assert(parameters->doing_static_link());
3948 // Static linking may need stubs, to support ifunc and long
3949 // branches. We need to create an output section for
3950 // .eh_frame early in the link process, to have a place to
3951 // attach stub .eh_frame info. We also need to have
3952 // registered a CIE that matches the stub CIE. Both of
3953 // these requirements are satisfied by creating an FDE and
3954 // CIE for .glink, even though static linking will leave
3955 // .glink zero length.
3956 // ??? Hopefully generating an FDE with a zero address range
3957 // won't confuse anything that consumes .eh_frame info.
3958 }
3959 else if (size == 64)
9d5781f8
AM
3960 {
3961 // There is one word before __glink_PLTresolve
3962 address += 8;
3963 len -= 8;
3964 }
3965 else if (parameters->options().output_is_position_independent())
3966 {
3967 // There are two FDEs for a position independent glink.
3968 // The first covers the branch table, the second
3969 // __glink_PLTresolve at the end of glink.
9e390558 3970 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 3971 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3972 len -= resolve_size;
3973 else
3974 {
3975 address += len - resolve_size;
3976 len = resolve_size;
3977 }
3978 }
3979 }
3980 else
3981 {
3982 // Must be a stub table.
3983 const Stub_table<size, big_endian>* stub_table
3984 = static_cast<const Stub_table<size, big_endian>*>(plt);
3985 uint64_t stub_address = stub_table->stub_address();
3986 len -= stub_address - address;
3987 address = stub_address;
3988 }
3989
3990 *paddress = address;
3991 *plen = len;
3992}
3993
42cacb20
DE
3994// A class to handle the PLT data.
3995
3996template<int size, bool big_endian>
cf43a2fe 3997class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3998{
3999 public:
4000 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4001 size, big_endian> Reloc_section;
4002
e5d5f5ed
AM
4003 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4004 Reloc_section* plt_rel,
e5d5f5ed
AM
4005 const char* name)
4006 : Output_section_data_build(size == 32 ? 4 : 8),
4007 rel_(plt_rel),
4008 targ_(targ),
e5d5f5ed
AM
4009 name_(name)
4010 { }
42cacb20
DE
4011
4012 // Add an entry to the PLT.
03e25981 4013 void
cf43a2fe 4014 add_entry(Symbol*);
42cacb20 4015
03e25981 4016 void
e5d5f5ed
AM
4017 add_ifunc_entry(Symbol*);
4018
2d7ad24e
AM
4019 void
4020 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4021
03e25981 4022 void
e5d5f5ed
AM
4023 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4024
42cacb20 4025 // Return the .rela.plt section data.
e5d5f5ed 4026 Reloc_section*
cf43a2fe
AM
4027 rel_plt() const
4028 {
42cacb20
DE
4029 return this->rel_;
4030 }
4031
0e70b911
CC
4032 // Return the number of PLT entries.
4033 unsigned int
4034 entry_count() const
d83ce4e3 4035 {
b3ccdeb5
AM
4036 if (this->current_data_size() == 0)
4037 return 0;
b4f7960d
AM
4038 return ((this->current_data_size() - this->first_plt_entry_offset())
4039 / this->plt_entry_size());
d83ce4e3 4040 }
0e70b911 4041
42cacb20 4042 protected:
42cacb20 4043 void
cf43a2fe 4044 do_adjust_output_section(Output_section* os)
42cacb20 4045 {
cf43a2fe 4046 os->set_entsize(0);
42cacb20
DE
4047 }
4048
6ce78956
AM
4049 // Write to a map file.
4050 void
4051 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4052 { mapfile->print_output_data(this, this->name_); }
6ce78956 4053
cf43a2fe 4054 private:
b4f7960d
AM
4055 // Return the offset of the first non-reserved PLT entry.
4056 unsigned int
4057 first_plt_entry_offset() const
4058 {
2d7ad24e
AM
4059 // IPLT and LPLT have no reserved entry.
4060 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4061 return 0;
4062 return this->targ_->first_plt_entry_offset();
4063 }
4064
4065 // Return the size of each PLT entry.
4066 unsigned int
4067 plt_entry_size() const
4068 {
4069 return this->targ_->plt_entry_size();
4070 }
cf43a2fe 4071
42cacb20
DE
4072 // Write out the PLT data.
4073 void
4074 do_write(Output_file*);
4075
4076 // The reloc section.
4077 Reloc_section* rel_;
cf43a2fe
AM
4078 // Allows access to .glink for do_write.
4079 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
4080 // What to report in map file.
4081 const char *name_;
42cacb20
DE
4082};
4083
e5d5f5ed 4084// Add an entry to the PLT.
42cacb20
DE
4085
4086template<int size, bool big_endian>
03e25981 4087void
e5d5f5ed 4088Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 4089{
e5d5f5ed
AM
4090 if (!gsym->has_plt_offset())
4091 {
ec661b9d 4092 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4093 if (off == 0)
4094 off += this->first_plt_entry_offset();
4095 gsym->set_plt_offset(off);
4096 gsym->set_needs_dynsym_entry();
4097 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4098 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 4099 off += this->plt_entry_size();
e5d5f5ed
AM
4100 this->set_current_data_size(off);
4101 }
42cacb20
DE
4102}
4103
e5d5f5ed 4104// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4105
4106template<int size, bool big_endian>
03e25981 4107void
e5d5f5ed 4108Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4109{
cf43a2fe
AM
4110 if (!gsym->has_plt_offset())
4111 {
ec661b9d 4112 section_size_type off = this->current_data_size();
cf43a2fe 4113 gsym->set_plt_offset(off);
e5d5f5ed 4114 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4115 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4116 dynrel = elfcpp::R_PPC64_JMP_IREL;
4117 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4118 off += this->plt_entry_size();
e5d5f5ed
AM
4119 this->set_current_data_size(off);
4120 }
4121}
4122
2d7ad24e
AM
4123// Add an entry for a local symbol to the PLT.
4124
4125template<int size, bool big_endian>
4126void
4127Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4128 Sized_relobj_file<size, big_endian>* relobj,
4129 unsigned int local_sym_index)
4130{
4131 if (!relobj->local_has_plt_offset(local_sym_index))
4132 {
4133 section_size_type off = this->current_data_size();
4134 relobj->set_local_plt_offset(local_sym_index, off);
4135 if (this->rel_)
4136 {
4137 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4138 if (size == 64 && this->targ_->abiversion() < 2)
4139 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4140 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4141 dynrel, this, off, 0);
4142 }
4143 off += this->plt_entry_size();
4144 this->set_current_data_size(off);
4145 }
4146}
4147
e5d5f5ed
AM
4148// Add an entry for a local ifunc symbol to the IPLT.
4149
4150template<int size, bool big_endian>
03e25981 4151void
e5d5f5ed
AM
4152Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4153 Sized_relobj_file<size, big_endian>* relobj,
4154 unsigned int local_sym_index)
4155{
4156 if (!relobj->local_has_plt_offset(local_sym_index))
4157 {
ec661b9d 4158 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4159 relobj->set_local_plt_offset(local_sym_index, off);
4160 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4161 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4162 dynrel = elfcpp::R_PPC64_JMP_IREL;
4163 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4164 this, off, 0);
b4f7960d 4165 off += this->plt_entry_size();
cf43a2fe
AM
4166 this->set_current_data_size(off);
4167 }
42cacb20
DE
4168}
4169
dd93cd0a 4170static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4171static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4172static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4173static const uint32_t add_3_3_2 = 0x7c631214;
4174static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4175static const uint32_t add_3_12_2 = 0x7c6c1214;
4176static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4177static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4178static const uint32_t add_11_2_11 = 0x7d625a14;
4179static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4180static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4181static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4182static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4183static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4184static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4185static const uint32_t addi_12_1 = 0x39810000;
32f59844 4186static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4187static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4188static const uint32_t addis_0_2 = 0x3c020000;
4189static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4190static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4191static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4192static const uint32_t addis_11_11 = 0x3d6b0000;
4193static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4194static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4195static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4196static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4197static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4198static const uint32_t b = 0x48000000;
4199static const uint32_t bcl_20_31 = 0x429f0005;
4200static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4201static const uint32_t bctrl = 0x4e800421;
4202static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4203static const uint32_t blr = 0x4e800020;
9e69ed50 4204static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4205static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4206static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4207static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4208static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4209static const uint32_t cror_15_15_15 = 0x4def7b82;
4210static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29 4211static const uint32_t ld_0_1 = 0xe8010000;
63e5eea2 4212static const uint32_t ld_0_11 = 0xe80b0000;
f3a0ed29 4213static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4214static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4215static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4216static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4217static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4218static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4219static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4220static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4221static const uint32_t ld_11_11 = 0xe96b0000;
4222static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4223static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4224static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4225static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4226static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4227static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4228static const uint32_t li_0_0 = 0x38000000;
e4dff765 4229static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4230static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4231static const uint32_t lis_0 = 0x3c000000;
549dba71 4232static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4233static const uint32_t lis_11 = 0x3d600000;
4234static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4235static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4236static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4237static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4238static const uint32_t lwz_11_11 = 0x816b0000;
4239static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4240static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4241static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4242static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4243static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4244static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4245static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4246static const uint32_t mr_0_3 = 0x7c601b78;
4247static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4248static const uint32_t mtctr_0 = 0x7c0903a6;
4249static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4250static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4251static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4252static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4253static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4254static const uint32_t nop = 0x60000000;
c9269dff 4255static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4256static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4257static const uint32_t ori_12_12_0 = 0x618c0000;
4258static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4259static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4260static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4261static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4262static const uint32_t std_0_1 = 0xf8010000;
4263static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4264static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4265static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4266static const uint32_t stfd_0_1 = 0xd8010000;
4267static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4268static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4269static const uint32_t sub_12_12_11 = 0x7d8b6050;
4270static const uint32_t xor_2_12_12 = 0x7d826278;
4271static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4272
e4dff765
AM
4273static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4274static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4275static const uint64_t pnop = 0x0700000000000000ULL;
4276
42cacb20
DE
4277// Write out the PLT.
4278
4279template<int size, bool big_endian>
4280void
4281Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4282{
2d7ad24e 4283 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4284 {
ec661b9d 4285 const section_size_type offset = this->offset();
cf43a2fe
AM
4286 const section_size_type oview_size
4287 = convert_to_section_size_type(this->data_size());
4288 unsigned char* const oview = of->get_output_view(offset, oview_size);
4289 unsigned char* pov = oview;
4290 unsigned char* endpov = oview + oview_size;
4291
e5d5f5ed 4292 // The address of the .glink branch table
cf43a2fe
AM
4293 const Output_data_glink<size, big_endian>* glink
4294 = this->targ_->glink_section();
ec661b9d 4295 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4296
4297 while (pov < endpov)
4298 {
4299 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4300 pov += 4;
4301 branch_tab += 4;
4302 }
4303
4304 of->write_output_view(offset, oview_size, oview);
4305 }
4306}
4307
4308// Create the PLT section.
4309
4310template<int size, bool big_endian>
4311void
40b469d7
AM
4312Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4313 Layout* layout)
cf43a2fe
AM
4314{
4315 if (this->plt_ == NULL)
4316 {
40b469d7
AM
4317 if (this->got_ == NULL)
4318 this->got_section(symtab, layout);
4319
cf43a2fe
AM
4320 if (this->glink_ == NULL)
4321 make_glink_section(layout);
4322
4323 // Ensure that .rela.dyn always appears before .rela.plt This is
4324 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4325 // needs to include .rela.plt in its range.
cf43a2fe
AM
4326 this->rela_dyn_section(layout);
4327
e5d5f5ed
AM
4328 Reloc_section* plt_rel = new Reloc_section(false);
4329 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4330 elfcpp::SHF_ALLOC, plt_rel,
4331 ORDER_DYNAMIC_PLT_RELOCS, false);
4332 this->plt_
4333 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 4334 "** PLT");
cf43a2fe
AM
4335 layout->add_output_section_data(".plt",
4336 (size == 32
4337 ? elfcpp::SHT_PROGBITS
4338 : elfcpp::SHT_NOBITS),
4339 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4340 this->plt_,
4341 (size == 32
4342 ? ORDER_SMALL_DATA
4343 : ORDER_SMALL_BSS),
4344 false);
3254d32c
AM
4345
4346 Output_section* rela_plt_os = plt_rel->output_section();
4347 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4348 }
4349}
4350
e5d5f5ed
AM
4351// Create the IPLT section.
4352
4353template<int size, bool big_endian>
4354void
40b469d7
AM
4355Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4356 Layout* layout)
e5d5f5ed
AM
4357{
4358 if (this->iplt_ == NULL)
4359 {
40b469d7 4360 this->make_plt_section(symtab, layout);
2d7ad24e 4361 this->make_lplt_section(layout);
e5d5f5ed
AM
4362
4363 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4364 if (this->rela_dyn_->output_section())
4365 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
4366 this->iplt_
4367 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 4368 "** IPLT");
6528b6eb
AM
4369 if (this->plt_->output_section())
4370 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4371 }
4372}
4373
2d7ad24e
AM
4374// Create the LPLT section.
4375
4376template<int size, bool big_endian>
4377void
4378Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4379{
4380 if (this->lplt_ == NULL)
4381 {
4382 Reloc_section* lplt_rel = NULL;
4383 if (parameters->options().output_is_position_independent())
4384 {
4385 lplt_rel = new Reloc_section(false);
4386 this->rela_dyn_section(layout);
4387 if (this->rela_dyn_->output_section())
4388 this->rela_dyn_->output_section()
4389 ->add_output_section_data(lplt_rel);
4390 }
4391 this->lplt_
4392 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4393 "** LPLT");
4394 this->make_brlt_section(layout);
4395 if (this->brlt_section_ && this->brlt_section_->output_section())
4396 this->brlt_section_->output_section()
4397 ->add_output_section_data(this->lplt_);
4398 else
4399 layout->add_output_section_data(".branch_lt",
4400 elfcpp::SHT_PROGBITS,
4401 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4402 this->lplt_,
4403 ORDER_RELRO,
4404 true);
4405 }
4406}
4407
ec661b9d 4408// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4409
4410template<int size, bool big_endian>
ec661b9d 4411class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4412{
4413 public:
ec661b9d
AM
4414 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4415 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4416 size, big_endian> Reloc_section;
c9269dff 4417
ec661b9d
AM
4418 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4419 Reloc_section* brlt_rel)
4420 : Output_section_data_build(size == 32 ? 4 : 8),
4421 rel_(brlt_rel),
4422 targ_(targ)
4423 { }
cf43a2fe 4424
06f30c9d
CC
4425 void
4426 reset_brlt_sizes()
4427 {
4428 this->reset_data_size();
4429 this->rel_->reset_data_size();
4430 }
4431
4432 void
4433 finalize_brlt_sizes()
4434 {
4435 this->finalize_data_size();
4436 this->rel_->finalize_data_size();
4437 }
4438
ec661b9d 4439 // Add a reloc for an entry in the BRLT.
cf43a2fe 4440 void
ec661b9d
AM
4441 add_reloc(Address to, unsigned int off)
4442 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 4443
ec661b9d 4444 // Update section and reloc section size.
e5d5f5ed 4445 void
ec661b9d
AM
4446 set_current_size(unsigned int num_branches)
4447 {
4448 this->reset_address_and_file_offset();
4449 this->set_current_data_size(num_branches * 16);
4450 this->finalize_data_size();
4451 Output_section* os = this->output_section();
4452 os->set_section_offsets_need_adjustment();
4453 if (this->rel_ != NULL)
4454 {
0e123f69 4455 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
4456 this->rel_->reset_address_and_file_offset();
4457 this->rel_->set_current_data_size(num_branches * reloc_size);
4458 this->rel_->finalize_data_size();
4459 Output_section* os = this->rel_->output_section();
4460 os->set_section_offsets_need_adjustment();
4461 }
4462 }
cf43a2fe 4463
ec661b9d
AM
4464 protected:
4465 void
4466 do_adjust_output_section(Output_section* os)
4467 {
4468 os->set_entsize(0);
4469 }
e5d5f5ed 4470
ec661b9d
AM
4471 // Write to a map file.
4472 void
4473 do_print_to_mapfile(Mapfile* mapfile) const
4474 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4475
ec661b9d
AM
4476 private:
4477 // Write out the BRLT data.
4478 void
4479 do_write(Output_file*);
c9824451 4480
ec661b9d
AM
4481 // The reloc section.
4482 Reloc_section* rel_;
4483 Target_powerpc<size, big_endian>* targ_;
4484};
cf43a2fe 4485
ec661b9d
AM
4486// Make the branch lookup table section.
4487
4488template<int size, bool big_endian>
4489void
4490Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4491{
4492 if (size == 64 && this->brlt_section_ == NULL)
4493 {
4494 Reloc_section* brlt_rel = NULL;
4495 bool is_pic = parameters->options().output_is_position_independent();
4496 if (is_pic)
4497 {
54483898
AM
4498 // When PIC we can't fill in .branch_lt but must initialise at
4499 // runtime via dynamic relocations.
ec661b9d
AM
4500 this->rela_dyn_section(layout);
4501 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4502 if (this->rela_dyn_->output_section())
4503 this->rela_dyn_->output_section()
4504 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4505 }
4506 this->brlt_section_
4507 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4508 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4509 this->plt_->output_section()
4510 ->add_output_section_data(this->brlt_section_);
4511 else
6830ee24 4512 layout->add_output_section_data(".branch_lt",
54483898 4513 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4514 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4515 this->brlt_section_,
54483898
AM
4516 ORDER_RELRO,
4517 true);
ec661b9d
AM
4518 }
4519}
4520
6830ee24 4521// Write out .branch_lt when non-PIC.
ec661b9d
AM
4522
4523template<int size, bool big_endian>
4524void
4525Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4526{
4527 if (size == 64 && !parameters->options().output_is_position_independent())
4528 {
4529 const section_size_type offset = this->offset();
4530 const section_size_type oview_size
4531 = convert_to_section_size_type(this->data_size());
4532 unsigned char* const oview = of->get_output_view(offset, oview_size);
4533
4534 this->targ_->write_branch_lookup_table(oview);
4535 of->write_output_view(offset, oview_size, oview);
4536 }
4537}
4538
9e69ed50
AM
4539static inline uint32_t
4540l(uint32_t a)
4541{
4542 return a & 0xffff;
4543}
4544
4545static inline uint32_t
4546hi(uint32_t a)
4547{
4548 return l(a >> 16);
4549}
4550
4551static inline uint32_t
4552ha(uint32_t a)
4553{
4554 return hi(a + 0x8000);
4555}
4556
e4dff765
AM
4557static inline uint64_t
4558d34(uint64_t v)
4559{
4560 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4561}
4562
4563static inline uint64_t
4564ha34(uint64_t v)
4565{
4566 return (v + (1ULL << 33)) >> 34;
4567}
4568
9d5781f8
AM
4569template<int size>
4570struct Eh_cie
4571{
4572 static const unsigned char eh_frame_cie[12];
4573};
4574
4575template<int size>
4576const unsigned char Eh_cie<size>::eh_frame_cie[] =
4577{
4578 1, // CIE version.
4579 'z', 'R', 0, // Augmentation string.
4580 4, // Code alignment.
4581 0x80 - size / 8 , // Data alignment.
4582 65, // RA reg.
4583 1, // Augmentation size.
4584 (elfcpp::DW_EH_PE_pcrel
4585 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4586 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4587};
4588
b4f7960d
AM
4589// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4590static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4591{
4592 0, 0, 0, 0, // Replaced with offset to .glink.
4593 0, 0, 0, 0, // Replaced with size of .glink.
4594 0, // Augmentation size.
63e5eea2 4595 elfcpp::DW_CFA_advance_loc + 2,
9d5781f8 4596 elfcpp::DW_CFA_register, 65, 12,
63e5eea2 4597 elfcpp::DW_CFA_advance_loc + 4,
9d5781f8
AM
4598 elfcpp::DW_CFA_restore_extended, 65
4599};
4600
b4f7960d
AM
4601// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4602static const unsigned char glink_eh_frame_fde_64v2[] =
4603{
4604 0, 0, 0, 0, // Replaced with offset to .glink.
4605 0, 0, 0, 0, // Replaced with size of .glink.
4606 0, // Augmentation size.
63e5eea2 4607 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d 4608 elfcpp::DW_CFA_register, 65, 0,
63e5eea2
AM
4609 elfcpp::DW_CFA_advance_loc + 2,
4610 elfcpp::DW_CFA_restore_extended, 65
4611};
4612
4613static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4614{
4615 0, 0, 0, 0, // Replaced with offset to .glink.
4616 0, 0, 0, 0, // Replaced with size of .glink.
4617 0, // Augmentation size.
4618 elfcpp::DW_CFA_advance_loc + 3,
4619 elfcpp::DW_CFA_register, 65, 0,
4620 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d
AM
4621 elfcpp::DW_CFA_restore_extended, 65
4622};
4623
9d5781f8
AM
4624// Describe __glink_PLTresolve use of LR, 32-bit version.
4625static const unsigned char glink_eh_frame_fde_32[] =
4626{
4627 0, 0, 0, 0, // Replaced with offset to .glink.
4628 0, 0, 0, 0, // Replaced with size of .glink.
4629 0, // Augmentation size.
4630 elfcpp::DW_CFA_advance_loc + 2,
4631 elfcpp::DW_CFA_register, 65, 0,
4632 elfcpp::DW_CFA_advance_loc + 4,
4633 elfcpp::DW_CFA_restore_extended, 65
4634};
4635
4636static const unsigned char default_fde[] =
4637{
4638 0, 0, 0, 0, // Replaced with offset to stubs.
4639 0, 0, 0, 0, // Replaced with size of stubs.
4640 0, // Augmentation size.
4641 elfcpp::DW_CFA_nop, // Pad.
4642 elfcpp::DW_CFA_nop,
4643 elfcpp::DW_CFA_nop
4644};
4645
9e69ed50
AM
4646template<bool big_endian>
4647static inline void
4648write_insn(unsigned char* p, uint32_t v)
4649{
4650 elfcpp::Swap<32, big_endian>::writeval(p, v);
4651}
4652
691d2e9a
AM
4653template<int size>
4654static inline unsigned int
4655param_plt_align()
4656{
4657 if (!parameters->options().user_set_plt_align())
4658 return size == 64 ? 32 : 8;
4659 return 1 << parameters->options().plt_align();
4660}
4661
ec661b9d
AM
4662// Stub_table holds information about plt and long branch stubs.
4663// Stubs are built in an area following some input section determined
4664// by group_sections(). This input section is converted to a relaxed
4665// input section allowing it to be resized to accommodate the stubs
4666
4667template<int size, bool big_endian>
4668class Stub_table : public Output_relaxed_input_section
4669{
4670 public:
7e57d19e
AM
4671 struct Plt_stub_ent
4672 {
4673 Plt_stub_ent(unsigned int off, unsigned int indx)
afd2ea23
AM
4674 : off_(off), indx_(indx), iter_(0), notoc_(0), toc_(0),
4675 r2save_(0), localentry0_(0), tocoff_(0)
7e57d19e
AM
4676 { }
4677
4678 unsigned int off_;
afd2ea23 4679 unsigned int indx_;
32f59844
AM
4680 unsigned int iter_ : 1;
4681 unsigned int notoc_ : 1;
afd2ea23 4682 unsigned int toc_ : 1;
7e57d19e 4683 unsigned int r2save_ : 1;
7ee7ff70 4684 unsigned int localentry0_ : 1;
afd2ea23 4685 unsigned int tocoff_ : 8;
7e57d19e 4686 };
32f59844
AM
4687 struct Branch_stub_ent
4688 {
4689 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
afd2ea23 4690 : off_(off), iter_(0), notoc_(notoc), toc_(0), save_res_(save_res),
fa40fbe4 4691 other_(0), tocoff_(0)
32f59844
AM
4692 { }
4693
4694 unsigned int off_;
afd2ea23
AM
4695 unsigned int iter_ : 1;
4696 unsigned int notoc_ : 1;
4697 unsigned int toc_ : 1;
4698 unsigned int save_res_ : 1;
fa40fbe4 4699 unsigned int other_ : 3;
afd2ea23 4700 unsigned int tocoff_ : 8;
32f59844 4701 };
ec661b9d
AM
4702 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4703 static const Address invalid_address = static_cast<Address>(0) - 1;
4704
a3e60ddb
AM
4705 Stub_table(Target_powerpc<size, big_endian>* targ,
4706 Output_section* output_section,
590b87ff
AM
4707 const Output_section::Input_section* owner,
4708 uint32_t id)
a3e60ddb
AM
4709 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4710 owner->relobj()
4711 ->section_addralign(owner->shndx())),
ec661b9d 4712 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4713 orig_data_size_(owner->current_data_size()),
4714 plt_size_(0), last_plt_size_(0),
6395d38b 4715 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4716 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4717 uniq_(id)
a3e60ddb
AM
4718 {
4719 this->set_output_section(output_section);
ec661b9d 4720
a3e60ddb
AM
4721 std::vector<Output_relaxed_input_section*> new_relaxed;
4722 new_relaxed.push_back(this);
4723 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4724 }
ec661b9d
AM
4725
4726 // Add a plt call stub.
a3e60ddb
AM
4727 bool
4728 add_plt_call_entry(Address,
4729 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4730 const Symbol*,
4731 unsigned int,
7e57d19e
AM
4732 Address,
4733 bool);
ec661b9d 4734
a3e60ddb
AM
4735 bool
4736 add_plt_call_entry(Address,
4737 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4738 unsigned int,
4739 unsigned int,
7e57d19e
AM
4740 Address,
4741 bool);
ec661b9d
AM
4742
4743 // Find a given plt call stub.
7e57d19e 4744 const Plt_stub_ent*
ec661b9d
AM
4745 find_plt_call_entry(const Symbol*) const;
4746
7e57d19e 4747 const Plt_stub_ent*
ec661b9d
AM
4748 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4749 unsigned int) const;
4750
7e57d19e 4751 const Plt_stub_ent*
ec661b9d
AM
4752 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4753 const Symbol*,
4754 unsigned int,
4755 Address) const;
4756
7e57d19e 4757 const Plt_stub_ent*
ec661b9d
AM
4758 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4759 unsigned int,
4760 unsigned int,
4761 Address) const;
4762
4763 // Add a long branch stub.
a3e60ddb
AM
4764 bool
4765 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
fa40fbe4 4766 unsigned int, Address, Address, unsigned int, bool);
ec661b9d 4767
32f59844 4768 const Branch_stub_ent*
9d5781f8
AM
4769 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4770 Address) const;
ec661b9d 4771
a3e60ddb
AM
4772 bool
4773 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4774 {
32f59844 4775 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4776 if (max_branch_offset == 0)
4777 return true;
4778 gold_assert(from != invalid_address);
4779 Address loc = off + this->stub_address();
4780 return loc - from + max_branch_offset < 2 * max_branch_offset;
4781 }
4782
ec661b9d 4783 void
a3e60ddb 4784 clear_stubs(bool all)
cf43a2fe 4785 {
9e69ed50
AM
4786 this->plt_call_stubs_.clear();
4787 this->plt_size_ = 0;
ec661b9d
AM
4788 this->long_branch_stubs_.clear();
4789 this->branch_size_ = 0;
d49044c7 4790 this->need_save_res_ = false;
a3e60ddb
AM
4791 if (all)
4792 {
4793 this->last_plt_size_ = 0;
4794 this->last_branch_size_ = 0;
4795 }
cf43a2fe
AM
4796 }
4797
32f59844
AM
4798 bool
4799 need_resize() const
4800 { return need_resize_; }
4801
4802 void
4803 set_resizing(bool val)
4804 {
4805 this->resizing_ = val;
4806 if (val)
4807 {
4808 this->need_resize_ = false;
4809 this->plt_size_ = 0;
4810 this->branch_size_ = 0;
4811 this->need_save_res_ = false;
4812 }
4813 }
4814
ec661b9d
AM
4815 Address
4816 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4817 {
ec661b9d
AM
4818 Address start_off = off;
4819 off += this->orig_data_size_;
4820 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4821 if (this->need_save_res_)
4822 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4823 if (my_size != 0)
4824 off = align_address(off, this->stub_align());
4825 // Include original section size and alignment padding in size
4826 my_size += off - start_off;
6395d38b
HS
4827 // Ensure new size is always larger than min size
4828 // threshold. Alignment requirement is included in "my_size", so
4829 // increase "my_size" does not invalidate alignment.
4830 if (my_size < this->min_size_threshold_)
4831 my_size = this->min_size_threshold_;
ec661b9d
AM
4832 this->reset_address_and_file_offset();
4833 this->set_current_data_size(my_size);
4834 this->set_address_and_file_offset(os->address() + start_off,
4835 os->offset() + start_off);
4836 return my_size;
cf43a2fe
AM
4837 }
4838
ec661b9d 4839 Address
9d5781f8 4840 stub_address() const
ec661b9d
AM
4841 {
4842 return align_address(this->address() + this->orig_data_size_,
4843 this->stub_align());
4844 }
4845
4846 Address
9d5781f8 4847 stub_offset() const
ec661b9d
AM
4848 {
4849 return align_address(this->offset() + this->orig_data_size_,
4850 this->stub_align());
4851 }
4852
4853 section_size_type
4854 plt_size() const
4855 { return this->plt_size_; }
4856
32f59844
AM
4857 section_size_type
4858 branch_size() const
4859 { return this->branch_size_; }
4860
590b87ff
AM
4861 void
4862 set_min_size_threshold(Address min_size)
6395d38b
HS
4863 { this->min_size_threshold_ = min_size; }
4864
590b87ff
AM
4865 void
4866 define_stub_syms(Symbol_table*);
4867
ec661b9d
AM
4868 bool
4869 size_update()
4870 {
4871 Output_section* os = this->output_section();
4872 if (os->addralign() < this->stub_align())
4873 {
4874 os->set_addralign(this->stub_align());
4875 // FIXME: get rid of the insane checkpointing.
4876 // We can't increase alignment of the input section to which
4877 // stubs are attached; The input section may be .init which
4878 // is pasted together with other .init sections to form a
4879 // function. Aligning might insert zero padding resulting in
4880 // sigill. However we do need to increase alignment of the
4881 // output section so that the align_address() on offset in
4882 // set_address_and_size() adds the same padding as the
4883 // align_address() on address in stub_address().
4884 // What's more, we need this alignment for the layout done in
4885 // relaxation_loop_body() so that the output section starts at
4886 // a suitably aligned address.
4887 os->checkpoint_set_addralign(this->stub_align());
4888 }
9e69ed50
AM
4889 if (this->last_plt_size_ != this->plt_size_
4890 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4891 {
9e69ed50
AM
4892 this->last_plt_size_ = this->plt_size_;
4893 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4894 return true;
4895 }
4896 return false;
4897 }
4898
34e0882b
AM
4899 // Add .eh_frame info for this stub section.
4900 void
4901 add_eh_frame(Layout* layout);
be897fb7 4902
34e0882b 4903 // Remove .eh_frame info for this stub section.
be897fb7 4904 void
34e0882b 4905 remove_eh_frame(Layout* layout);
9d5781f8 4906
ec661b9d
AM
4907 Target_powerpc<size, big_endian>*
4908 targ() const
4909 { return targ_; }
6ce78956 4910
cf43a2fe 4911 private:
bdab445c
AM
4912 class Plt_stub_key;
4913 class Plt_stub_key_hash;
bdab445c
AM
4914 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4915 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
4916 class Branch_stub_key;
4917 class Branch_stub_key_hash;
4918 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4919 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
4920
4921 // Alignment of stub section.
ec661b9d 4922 unsigned int
9e69ed50
AM
4923 stub_align() const
4924 {
691d2e9a 4925 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
4926 unsigned int user_align = 1 << parameters->options().plt_align();
4927 return std::max(user_align, min_align);
4928 }
cf43a2fe 4929
91c2b899
AM
4930 // Return the plt offset for the given call stub.
4931 Address
08be3224
AM
4932 plt_off(typename Plt_stub_entries::const_iterator p,
4933 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
4934 {
4935 const Symbol* gsym = p->first.sym_;
4936 if (gsym != NULL)
08be3224 4937 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
4938 else
4939 {
91c2b899
AM
4940 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4941 unsigned int local_sym_index = p->first.locsym_;
08be3224 4942 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
4943 }
4944 }
4945
9e69ed50 4946 // Size of a given plt call stub.
ec661b9d 4947 unsigned int
afd2ea23 4948 plt_call_size(typename Plt_stub_entries::iterator p) const;
34e0882b
AM
4949
4950 unsigned int
4951 plt_call_align(unsigned int bytes) const
4952 {
691d2e9a 4953 unsigned int align = param_plt_align<size>();
9e390558 4954 return (bytes + align - 1) & -align;
9e69ed50 4955 }
ec661b9d
AM
4956
4957 // Return long branch stub size.
4958 unsigned int
afd2ea23 4959 branch_stub_size(typename Branch_stub_entries::iterator p,
32f59844
AM
4960 bool* need_lt);
4961
afd2ea23
AM
4962 void
4963 build_tls_opt_head(unsigned char** pp, bool save_lr);
32f59844 4964
afd2ea23
AM
4965 void
4966 build_tls_opt_tail(unsigned char* p);
ec661b9d 4967
f073a3e8
AM
4968 void
4969 plt_error(const Plt_stub_key& p);
4970
ec661b9d 4971 // Write out stubs.
cf43a2fe
AM
4972 void
4973 do_write(Output_file*);
4974
ec661b9d 4975 // Plt call stub keys.
bdab445c 4976 class Plt_stub_key
cf43a2fe 4977 {
d1a8cabd 4978 public:
bdab445c 4979 Plt_stub_key(const Symbol* sym)
c9824451
AM
4980 : sym_(sym), object_(0), addend_(0), locsym_(0)
4981 { }
4982
bdab445c 4983 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4984 unsigned int locsym_index)
c9824451
AM
4985 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4986 { }
4987
bdab445c 4988 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4989 const Symbol* sym,
4990 unsigned int r_type,
4991 Address addend)
e5d5f5ed 4992 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4993 {
4994 if (size != 32)
ec661b9d 4995 this->addend_ = addend;
d1a8cabd 4996 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4997 && (r_type == elfcpp::R_PPC_PLTREL24
4998 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 4999 {
ec661b9d 5000 this->addend_ = addend;
e5d5f5ed 5001 if (this->addend_ >= 32768)
d1a8cabd 5002 this->object_ = object;
cf43a2fe
AM
5003 }
5004 }
5005
bdab445c 5006 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5007 unsigned int locsym_index,
5008 unsigned int r_type,
5009 Address addend)
e5d5f5ed
AM
5010 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5011 {
5012 if (size != 32)
ec661b9d 5013 this->addend_ = addend;
e5d5f5ed 5014 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5015 && (r_type == elfcpp::R_PPC_PLTREL24
5016 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 5017 this->addend_ = addend;
e5d5f5ed
AM
5018 }
5019
bdab445c 5020 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
5021 {
5022 return (this->sym_ == that.sym_
5023 && this->object_ == that.object_
e5d5f5ed
AM
5024 && this->addend_ == that.addend_
5025 && this->locsym_ == that.locsym_);
cf43a2fe 5026 }
c9269dff
AM
5027
5028 const Symbol* sym_;
e5d5f5ed
AM
5029 const Sized_relobj_file<size, big_endian>* object_;
5030 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5031 unsigned int locsym_;
cf43a2fe
AM
5032 };
5033
bdab445c 5034 class Plt_stub_key_hash
cf43a2fe 5035 {
d1a8cabd 5036 public:
bdab445c 5037 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
5038 {
5039 return (reinterpret_cast<uintptr_t>(ent.sym_)
5040 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
5041 ^ ent.addend_
5042 ^ ent.locsym_);
cf43a2fe 5043 }
ec661b9d
AM
5044 };
5045
5046 // Long branch stub keys.
32f59844 5047 class Branch_stub_key
ec661b9d
AM
5048 {
5049 public:
32f59844
AM
5050 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
5051 : dest_(to), toc_base_off_(0)
ec661b9d
AM
5052 {
5053 if (size == 64)
5054 toc_base_off_ = obj->toc_base_offset();
5055 }
5056
32f59844 5057 bool operator==(const Branch_stub_key& that) const
ec661b9d
AM
5058 {
5059 return (this->dest_ == that.dest_
5060 && (size == 32
5061 || this->toc_base_off_ == that.toc_base_off_));
5062 }
cf43a2fe 5063
ec661b9d
AM
5064 Address dest_;
5065 unsigned int toc_base_off_;
5066 };
cf43a2fe 5067
32f59844 5068 class Branch_stub_key_hash
ec661b9d
AM
5069 {
5070 public:
32f59844
AM
5071 size_t operator()(const Branch_stub_key& key) const
5072 { return key.dest_ ^ key.toc_base_off_; }
ec661b9d 5073 };
cf43a2fe 5074
ec661b9d 5075 // In a sane world this would be a global.
cf43a2fe 5076 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5077 // Map sym/object/addend to stub offset.
ec661b9d
AM
5078 Plt_stub_entries plt_call_stubs_;
5079 // Map destination address to stub offset.
ec661b9d
AM
5080 Branch_stub_entries long_branch_stubs_;
5081 // size of input section
5082 section_size_type orig_data_size_;
5083 // size of stubs
9e69ed50 5084 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5085 // Some rare cases cause (PR/20529) fluctuation in stub table
5086 // size, which leads to an endless relax loop. This is to be fixed
5087 // by, after the first few iterations, allowing only increase of
5088 // stub table size. This variable sets the minimal possible size of
5089 // a stub table, it is zero for the first few iterations, then
5090 // increases monotonically.
5091 Address min_size_threshold_;
d49044c7
AM
5092 // Set if this stub group needs a copy of out-of-line register
5093 // save/restore functions.
5094 bool need_save_res_;
32f59844
AM
5095 // Set when notoc_/r2save_ changes after sizing a stub
5096 bool need_resize_;
5097 // Set when resizing stubs
5098 bool resizing_;
590b87ff
AM
5099 // Per stub table unique identifier.
5100 uint32_t uniq_;
cf43a2fe
AM
5101};
5102
ec661b9d 5103// Add a plt call stub, if we do not already have one for this
d1a8cabd 5104// sym/object/addend combo.
cf43a2fe
AM
5105
5106template<int size, bool big_endian>
a3e60ddb 5107bool
ec661b9d 5108Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5109 Address from,
c9824451 5110 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5111 const Symbol* gsym,
ec661b9d 5112 unsigned int r_type,
7e57d19e
AM
5113 Address addend,
5114 bool tocsave)
cf43a2fe 5115{
bdab445c
AM
5116 Plt_stub_key key(object, gsym, r_type, addend);
5117 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5118 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5119 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5120 if (size == 64)
7ee7ff70 5121 {
32f59844 5122 if (p.second
7ee7ff70
AM
5123 && this->targ_->is_elfv2_localentry0(gsym))
5124 {
5125 p.first->second.localentry0_ = 1;
5126 this->targ_->set_has_localentry0();
5127 }
32f59844
AM
5128 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5129 {
e4dff765 5130 if (!p.second && !p.first->second.notoc_
afd2ea23
AM
5131 && (!this->targ_->power10_stubs()
5132 || this->targ_->power10_stubs_auto()))
32f59844
AM
5133 this->need_resize_ = true;
5134 p.first->second.notoc_ = 1;
5135 }
afd2ea23 5136 else
32f59844 5137 {
afd2ea23 5138 if (!p.second && !p.first->second.toc_)
32f59844 5139 this->need_resize_ = true;
afd2ea23
AM
5140 p.first->second.toc_ = 1;
5141 if (!tocsave && !p.first->second.localentry0_)
5142 {
5143 if (!p.second && !p.first->second.r2save_)
5144 this->need_resize_ = true;
5145 p.first->second.r2save_ = 1;
5146 }
32f59844
AM
5147 }
5148 }
5149 if (p.second || (this->resizing_ && !p.first->second.iter_))
5150 {
5151 if (this->resizing_)
5152 {
5153 p.first->second.iter_ = 1;
5154 p.first->second.off_ = this->plt_size_;
5155 }
5156 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5157 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5158 this->targ_->set_has_tls_get_addr_opt();
34e0882b 5159 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70 5160 }
32f59844 5161 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5162}
5163
e5d5f5ed 5164template<int size, bool big_endian>
a3e60ddb 5165bool
ec661b9d 5166Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5167 Address from,
c9824451 5168 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5169 unsigned int locsym_index,
ec661b9d 5170 unsigned int r_type,
7e57d19e
AM
5171 Address addend,
5172 bool tocsave)
e5d5f5ed 5173{
bdab445c
AM
5174 Plt_stub_key key(object, locsym_index, r_type, addend);
5175 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5176 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5177 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5178 if (size == 64)
7ee7ff70 5179 {
32f59844 5180 if (p.second
7ee7ff70
AM
5181 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5182 {
5183 p.first->second.localentry0_ = 1;
5184 this->targ_->set_has_localentry0();
5185 }
32f59844
AM
5186 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5187 {
e4dff765 5188 if (!p.second && !p.first->second.notoc_
afd2ea23
AM
5189 && (!this->targ_->power10_stubs()
5190 || this->targ_->power10_stubs_auto()))
32f59844
AM
5191 this->need_resize_ = true;
5192 p.first->second.notoc_ = 1;
5193 }
afd2ea23 5194 else
32f59844 5195 {
afd2ea23 5196 if (!p.second && !p.first->second.toc_)
32f59844 5197 this->need_resize_ = true;
afd2ea23
AM
5198 p.first->second.toc_ = 1;
5199 if (!tocsave && !p.first->second.localentry0_)
5200 {
5201 if (!p.second && !p.first->second.r2save_)
5202 this->need_resize_ = true;
5203 p.first->second.r2save_ = 1;
5204 }
32f59844 5205 }
7ee7ff70 5206 }
32f59844
AM
5207 if (p.second || (this->resizing_ && !p.first->second.iter_))
5208 {
5209 if (this->resizing_)
5210 {
5211 p.first->second.iter_ = 1;
5212 p.first->second.off_ = this->plt_size_;
5213 }
5214 this->plt_size_ += this->plt_call_size(p.first);
5215 this->plt_size_ = this->plt_call_align(this->plt_size_);
5216 }
5217 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5218}
5219
ec661b9d
AM
5220// Find a plt call stub.
5221
cf43a2fe 5222template<int size, bool big_endian>
7e57d19e 5223const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5224Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5225 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5226 const Symbol* gsym,
ec661b9d
AM
5227 unsigned int r_type,
5228 Address addend) const
c9824451 5229{
bdab445c
AM
5230 Plt_stub_key key(object, gsym, r_type, addend);
5231 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5232 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5233 return NULL;
5234 return &p->second;
c9824451
AM
5235}
5236
5237template<int size, bool big_endian>
7e57d19e 5238const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5239Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5240{
bdab445c
AM
5241 Plt_stub_key key(gsym);
5242 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5243 if (p == this->plt_call_stubs_.end())
5244 return NULL;
5245 return &p->second;
cf43a2fe
AM
5246}
5247
e5d5f5ed 5248template<int size, bool big_endian>
7e57d19e 5249const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5250Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5251 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5252 unsigned int locsym_index,
ec661b9d
AM
5253 unsigned int r_type,
5254 Address addend) const
e5d5f5ed 5255{
bdab445c
AM
5256 Plt_stub_key key(object, locsym_index, r_type, addend);
5257 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5258 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5259 return NULL;
5260 return &p->second;
c9824451
AM
5261}
5262
5263template<int size, bool big_endian>
7e57d19e 5264const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5265Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5266 const Sized_relobj_file<size, big_endian>* object,
5267 unsigned int locsym_index) const
5268{
bdab445c
AM
5269 Plt_stub_key key(object, locsym_index);
5270 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5271 if (p == this->plt_call_stubs_.end())
5272 return NULL;
5273 return &p->second;
ec661b9d
AM
5274}
5275
5276// Add a long branch stub if we don't already have one to given
5277// destination.
5278
5279template<int size, bool big_endian>
a3e60ddb 5280bool
ec661b9d
AM
5281Stub_table<size, big_endian>::add_long_branch_entry(
5282 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
5283 unsigned int r_type,
5284 Address from,
d49044c7 5285 Address to,
fa40fbe4 5286 unsigned int other,
d49044c7 5287 bool save_res)
ec661b9d 5288{
32f59844
AM
5289 Branch_stub_key key(object, to);
5290 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5291 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
590b87ff 5292 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844 5293 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
afd2ea23 5294 if (notoc)
ec661b9d 5295 {
afd2ea23
AM
5296 if (!p.second && !p.first->second.notoc_)
5297 this->need_resize_ = true;
32f59844
AM
5298 p.first->second.notoc_ = true;
5299 }
afd2ea23
AM
5300 else
5301 {
5302 if (!p.second && !p.first->second.toc_)
5303 this->need_resize_ = true;
5304 p.first->second.toc_ = true;
5305 }
fa40fbe4
AM
5306 if (p.first->second.other_ == 0)
5307 p.first->second.other_ = other;
32f59844
AM
5308 gold_assert(save_res == p.first->second.save_res_);
5309 if (p.second || (this->resizing_ && !p.first->second.iter_))
5310 {
5311 if (this->resizing_)
5312 {
5313 p.first->second.iter_ = 1;
5314 p.first->second.off_ = this->branch_size_;
5315 }
d49044c7
AM
5316 if (save_res)
5317 this->need_save_res_ = true;
5318 else
5319 {
32f59844
AM
5320 bool need_lt = false;
5321 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5322 this->branch_size_ += stub_size;
5323 if (size == 64 && need_lt)
d49044c7
AM
5324 this->targ_->add_branch_lookup_table(to);
5325 }
ec661b9d 5326 }
32f59844 5327 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5328}
5329
d49044c7 5330// Find long branch stub offset.
ec661b9d
AM
5331
5332template<int size, bool big_endian>
32f59844 5333const typename Stub_table<size, big_endian>::Branch_stub_ent*
ec661b9d
AM
5334Stub_table<size, big_endian>::find_long_branch_entry(
5335 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 5336 Address to) const
ec661b9d 5337{
32f59844 5338 Branch_stub_key key(object, to);
ec661b9d 5339 typename Branch_stub_entries::const_iterator p
32f59844 5340 = this->long_branch_stubs_.find(key);
d49044c7 5341 if (p == this->long_branch_stubs_.end())
32f59844
AM
5342 return NULL;
5343 return &p->second;
e5d5f5ed
AM
5344}
5345
220f9906
AM
5346template<bool big_endian>
5347static void
5348eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5349{
220f9906
AM
5350 delta /= 4;
5351 if (delta < 64)
5352 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5353 else if (delta < 256)
34e0882b 5354 {
220f9906
AM
5355 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5356 fde.push_back(delta);
5357 }
5358 else if (delta < 65536)
5359 {
5360 fde.resize(fde.size() + 3);
5361 unsigned char *p = &*fde.end() - 3;
5362 *p++ = elfcpp::DW_CFA_advance_loc2;
5363 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5364 }
5365 else
5366 {
5367 fde.resize(fde.size() + 5);
5368 unsigned char *p = &*fde.end() - 5;
5369 *p++ = elfcpp::DW_CFA_advance_loc4;
5370 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5371 }
220f9906
AM
5372}
5373
5374template<typename T>
5375static bool
5376stub_sort(T s1, T s2)
5377{
5378 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5379}
5380
5381// Add .eh_frame info for this stub section. Unlike other linker
5382// generated .eh_frame this is added late in the link, because we
5383// only want the .eh_frame info if this particular stub section is
5384// non-empty.
5385
5386template<int size, bool big_endian>
5387void
5388Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5389{
220f9906
AM
5390 if (size != 64
5391 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5392 return;
5393
5394 // Since we add stub .eh_frame info late, it must be placed
5395 // after all other linker generated .eh_frame info so that
5396 // merge mapping need not be updated for input sections.
5397 // There is no provision to use a different CIE to that used
5398 // by .glink.
5399 if (!this->targ_->has_glink())
5400 return;
5401
afd2ea23 5402 typedef typename Plt_stub_entries::iterator plt_iter;
220f9906
AM
5403 std::vector<plt_iter> calls;
5404 if (!this->plt_call_stubs_.empty())
5405 for (plt_iter cs = this->plt_call_stubs_.begin();
5406 cs != this->plt_call_stubs_.end();
5407 ++cs)
5408 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5409 && cs->second.r2save_
5410 && !cs->second.localentry0_)
e4dff765 5411 || (cs->second.notoc_
7c1f4227 5412 && !this->targ_->power10_stubs()))
220f9906
AM
5413 calls.push_back(cs);
5414 if (calls.size() > 1)
5415 std::stable_sort(calls.begin(), calls.end(),
5416 stub_sort<plt_iter>);
5417
5418 typedef typename Branch_stub_entries::const_iterator branch_iter;
5419 std::vector<branch_iter> branches;
e4dff765 5420 if (!this->long_branch_stubs_.empty()
7c1f4227 5421 && !this->targ_->power10_stubs())
220f9906
AM
5422 for (branch_iter bs = this->long_branch_stubs_.begin();
5423 bs != this->long_branch_stubs_.end();
5424 ++bs)
5425 if (bs->second.notoc_)
5426 branches.push_back(bs);
5427 if (branches.size() > 1)
5428 std::stable_sort(branches.begin(), branches.end(),
5429 stub_sort<branch_iter>);
5430
5431 if (calls.empty() && branches.empty())
34e0882b
AM
5432 return;
5433
220f9906
AM
5434 unsigned int last_eh_loc = 0;
5435 // offset pcrel sdata4, size udata4, and augmentation size byte.
5436 std::vector<unsigned char> fde(9, 0);
5437
5438 for (unsigned int i = 0; i < calls.size(); i++)
5439 {
5440 plt_iter cs = calls[i];
5441 unsigned int off = cs->second.off_;
5442 // The __tls_get_addr_opt call stub needs to describe where
5443 // it saves LR, to support exceptions that might be thrown
5444 // from __tls_get_addr, and to support asynchronous exceptions.
5445 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5446 {
5447 off += 7 * 4;
5448 if (cs->second.r2save_
5449 && !cs->second.localentry0_)
5450 {
5451 off += 2 * 4;
5452 eh_advance<big_endian>(fde, off - last_eh_loc);
5453 fde.resize(fde.size() + 6);
5454 unsigned char* p = &*fde.end() - 6;
5455 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5456 *p++ = 65;
5457 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5458 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5459 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5460 *p++ = elfcpp::DW_CFA_restore_extended;
5461 *p++ = 65;
5462 last_eh_loc = off + delta;
5463 continue;
5464 }
5465 }
5466 // notoc stubs also should describe LR changes, to support
5467 // asynchronous exceptions.
5468 off += (cs->second.r2save_ ? 4 : 0) + 8;
5469 eh_advance<big_endian>(fde, off - last_eh_loc);
5470 fde.resize(fde.size() + 6);
5471 unsigned char* p = &*fde.end() - 6;
5472 *p++ = elfcpp::DW_CFA_register;
5473 *p++ = 65;
5474 *p++ = 12;
5475 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5476 *p++ = elfcpp::DW_CFA_restore_extended;
5477 *p++ = 65;
5478 last_eh_loc = off + 8;
5479 }
5480
5481 for (unsigned int i = 0; i < branches.size(); i++)
5482 {
5483 branch_iter bs = branches[i];
5484 unsigned int off = bs->second.off_ + 8;
5485 eh_advance<big_endian>(fde, off - last_eh_loc);
5486 fde.resize(fde.size() + 6);
5487 unsigned char* p = &*fde.end() - 6;
5488 *p++ = elfcpp::DW_CFA_register;
5489 *p++ = 65;
5490 *p++ = 12;
5491 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5492 *p++ = elfcpp::DW_CFA_restore_extended;
5493 *p++ = 65;
5494 last_eh_loc = off + 8;
5495 }
5496
34e0882b
AM
5497 layout->add_eh_frame_for_plt(this,
5498 Eh_cie<size>::eh_frame_cie,
5499 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5500 &*fde.begin(), fde.size());
34e0882b
AM
5501}
5502
5503template<int size, bool big_endian>
5504void
5505Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5506{
220f9906
AM
5507 if (size == 64
5508 && parameters->options().ld_generated_unwind_info()
5509 && this->targ_->has_glink())
5510 layout->remove_eh_frame_for_plt(this,
5511 Eh_cie<size>::eh_frame_cie,
5512 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5513}
5514
ec661b9d
AM
5515// A class to handle .glink.
5516
5517template<int size, bool big_endian>
5518class Output_data_glink : public Output_section_data
5519{
5520 public:
9055360d
AM
5521 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5522 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5523
5524 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5525 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5526 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5527 { }
5528
9d5781f8 5529 void
9055360d 5530 add_eh_frame(Layout* layout);
9d5781f8 5531
9055360d
AM
5532 void
5533 add_global_entry(const Symbol*);
5534
5535 Address
5536 find_global_entry(const Symbol*) const;
5537
9e390558
AM
5538 unsigned int
5539 global_entry_align(unsigned int off) const
5540 {
691d2e9a 5541 unsigned int align = param_plt_align<size>();
9e390558
AM
5542 return (off + align - 1) & -align;
5543 }
5544
5545 unsigned int
5546 global_entry_off() const
5547 {
5548 return this->global_entry_align(this->end_branch_table_);
5549 }
5550
9055360d
AM
5551 Address
5552 global_entry_address() const
5553 {
5554 gold_assert(this->is_data_size_valid());
9e390558
AM
5555 return this->address() + this->global_entry_off();
5556 }
5557
5558 int
5559 pltresolve_size() const
5560 {
5561 if (size == 64)
5562 return (8
63e5eea2
AM
5563 + (this->targ_->abiversion() < 2 ? 11 * 4
5564 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
9e390558 5565 return 16 * 4;
9d5781f8
AM
5566 }
5567
ec661b9d
AM
5568 protected:
5569 // Write to a map file.
5570 void
5571 do_print_to_mapfile(Mapfile* mapfile) const
5572 { mapfile->print_output_data(this, _("** glink")); }
5573
5574 private:
5575 void
5576 set_final_data_size();
5577
5578 // Write out .glink
5579 void
5580 do_write(Output_file*);
5581
5582 // Allows access to .got and .plt for do_write.
5583 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5584
5585 // Map sym to stub offset.
5586 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5587 Global_entry_stub_entries global_entry_stubs_;
5588
5589 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5590};
5591
9055360d
AM
5592template<int size, bool big_endian>
5593void
5594Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5595{
5596 if (!parameters->options().ld_generated_unwind_info())
5597 return;
5598
5599 if (size == 64)
5600 {
5601 if (this->targ_->abiversion() < 2)
5602 layout->add_eh_frame_for_plt(this,
5603 Eh_cie<64>::eh_frame_cie,
5604 sizeof (Eh_cie<64>::eh_frame_cie),
5605 glink_eh_frame_fde_64v1,
5606 sizeof (glink_eh_frame_fde_64v1));
63e5eea2
AM
5607 else if (this->targ_->has_localentry0())
5608 layout->add_eh_frame_for_plt(this,
5609 Eh_cie<64>::eh_frame_cie,
5610 sizeof (Eh_cie<64>::eh_frame_cie),
5611 glink_eh_frame_fde_64v2_localentry0,
5612 sizeof (glink_eh_frame_fde_64v2));
9055360d
AM
5613 else
5614 layout->add_eh_frame_for_plt(this,
5615 Eh_cie<64>::eh_frame_cie,
5616 sizeof (Eh_cie<64>::eh_frame_cie),
5617 glink_eh_frame_fde_64v2,
5618 sizeof (glink_eh_frame_fde_64v2));
5619 }
5620 else
5621 {
5622 // 32-bit .glink can use the default since the CIE return
5623 // address reg, LR, is valid.
5624 layout->add_eh_frame_for_plt(this,
5625 Eh_cie<32>::eh_frame_cie,
5626 sizeof (Eh_cie<32>::eh_frame_cie),
5627 default_fde,
5628 sizeof (default_fde));
5629 // Except where LR is used in a PIC __glink_PLTresolve.
5630 if (parameters->options().output_is_position_independent())
5631 layout->add_eh_frame_for_plt(this,
5632 Eh_cie<32>::eh_frame_cie,
5633 sizeof (Eh_cie<32>::eh_frame_cie),
5634 glink_eh_frame_fde_32,
5635 sizeof (glink_eh_frame_fde_32));
5636 }
5637}
5638
5639template<int size, bool big_endian>
5640void
5641Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5642{
9e390558 5643 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5644 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5645 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5646 if (p.second)
407aa07c 5647 this->ge_size_ = off + 16;
9055360d
AM
5648}
5649
5650template<int size, bool big_endian>
5651typename Output_data_glink<size, big_endian>::Address
5652Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5653{
5654 typename Global_entry_stub_entries::const_iterator p
5655 = this->global_entry_stubs_.find(gsym);
5656 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5657}
5658
cf43a2fe
AM
5659template<int size, bool big_endian>
5660void
5661Output_data_glink<size, big_endian>::set_final_data_size()
5662{
ec661b9d
AM
5663 unsigned int count = this->targ_->plt_entry_count();
5664 section_size_type total = 0;
cf43a2fe
AM
5665
5666 if (count != 0)
5667 {
5668 if (size == 32)
5669 {
cf43a2fe
AM
5670 // space for branch table
5671 total += 4 * (count - 1);
5672
5673 total += -total & 15;
9e390558 5674 total += this->pltresolve_size();
cf43a2fe
AM
5675 }
5676 else
5677 {
9e390558 5678 total += this->pltresolve_size();
cf43a2fe
AM
5679
5680 // space for branch table
b4f7960d
AM
5681 total += 4 * count;
5682 if (this->targ_->abiversion() < 2)
5683 {
5684 total += 4 * count;
5685 if (count > 0x8000)
5686 total += 4 * (count - 0x8000);
5687 }
cf43a2fe
AM
5688 }
5689 }
9055360d 5690 this->end_branch_table_ = total;
9e390558 5691 total = this->global_entry_align(total);
9055360d 5692 total += this->ge_size_;
cf43a2fe
AM
5693
5694 this->set_data_size(total);
5695}
5696
590b87ff
AM
5697// Define symbols on stubs, identifying the stub.
5698
5699template<int size, bool big_endian>
5700void
5701Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5702{
5703 if (!this->plt_call_stubs_.empty())
5704 {
5705 // The key for the plt call stub hash table includes addresses,
5706 // therefore traversal order depends on those addresses, which
5707 // can change between runs if gold is a PIE. Unfortunately the
5708 // output .symtab ordering depends on the order in which symbols
5709 // are added to the linker symtab. We want reproducible output
5710 // so must sort the call stub symbols.
afd2ea23 5711 typedef typename Plt_stub_entries::iterator plt_iter;
590b87ff
AM
5712 std::vector<plt_iter> sorted;
5713 sorted.resize(this->plt_call_stubs_.size());
5714
5715 for (plt_iter cs = this->plt_call_stubs_.begin();
5716 cs != this->plt_call_stubs_.end();
5717 ++cs)
bdab445c 5718 sorted[cs->second.indx_] = cs;
590b87ff
AM
5719
5720 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5721 {
5722 plt_iter cs = sorted[i];
5723 char add[10];
5724 add[0] = 0;
5725 if (cs->first.addend_ != 0)
5726 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5727 char obj[10];
5728 obj[0] = 0;
5729 if (cs->first.object_)
590b87ff
AM
5730 {
5731 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5732 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5733 sprintf(obj, "%x:", ppcobj->uniq());
5734 }
5735 char localname[9];
5736 const char *symname;
5737 if (cs->first.sym_ == NULL)
5738 {
5739 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5740 symname = localname;
5741 }
34e0882b
AM
5742 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5743 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5744 else
5745 symname = cs->first.sym_->name();
94de2a2c
JC
5746 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5747 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5748 Address value
5749 = this->stub_address() - this->address() + cs->second.off_;
34e0882b 5750 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
590b87ff
AM
5751 this->targ_->define_local(symtab, name, this, value, stub_size);
5752 }
5753 }
5754
afd2ea23 5755 typedef typename Branch_stub_entries::iterator branch_iter;
590b87ff
AM
5756 for (branch_iter bs = this->long_branch_stubs_.begin();
5757 bs != this->long_branch_stubs_.end();
5758 ++bs)
5759 {
32f59844 5760 if (bs->second.save_res_)
590b87ff
AM
5761 continue;
5762
5763 char* name = new char[8 + 13 + 16 + 1];
5764 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5765 static_cast<unsigned long long>(bs->first.dest_));
5766 Address value = (this->stub_address() - this->address()
32f59844
AM
5767 + this->plt_size_ + bs->second.off_);
5768 bool need_lt = false;
5769 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5770 this->targ_->define_local(symtab, name, this, value, stub_size);
5771 }
5772}
5773
32f59844
AM
5774// Emit the start of a __tls_get_addr_opt plt call stub.
5775
5776template<int size, bool big_endian>
afd2ea23
AM
5777void
5778Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5779 bool save_lr)
32f59844 5780{
afd2ea23
AM
5781 unsigned char* p = *pp;
5782 if (size == 64)
32f59844 5783 {
afd2ea23
AM
5784 write_insn<big_endian>(p, ld_11_3 + 0);
5785 p += 4;
5786 write_insn<big_endian>(p, ld_12_3 + 8);
5787 p += 4;
5788 write_insn<big_endian>(p, mr_0_3);
5789 p += 4;
5790 write_insn<big_endian>(p, cmpdi_11_0);
5791 p += 4;
5792 write_insn<big_endian>(p, add_3_12_13);
5793 p += 4;
5794 write_insn<big_endian>(p, beqlr);
5795 p += 4;
5796 write_insn<big_endian>(p, mr_3_0);
5797 p += 4;
5798 if (save_lr)
32f59844 5799 {
afd2ea23 5800 write_insn<big_endian>(p, mflr_11);
32f59844 5801 p += 4;
afd2ea23 5802 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
32f59844
AM
5803 p += 4;
5804 }
32f59844 5805 }
afd2ea23 5806 else
32f59844 5807 {
afd2ea23 5808 write_insn<big_endian>(p, lwz_11_3 + 0);
32f59844 5809 p += 4;
afd2ea23 5810 write_insn<big_endian>(p, lwz_12_3 + 4);
32f59844 5811 p += 4;
afd2ea23 5812 write_insn<big_endian>(p, mr_0_3);
32f59844 5813 p += 4;
afd2ea23
AM
5814 write_insn<big_endian>(p, cmpwi_11_0);
5815 p += 4;
5816 write_insn<big_endian>(p, add_3_12_2);
5817 p += 4;
5818 write_insn<big_endian>(p, beqlr);
5819 p += 4;
5820 write_insn<big_endian>(p, mr_3_0);
5821 p += 4;
5822 write_insn<big_endian>(p, nop);
32f59844 5823 p += 4;
32f59844 5824 }
afd2ea23
AM
5825 *pp = p;
5826}
5827
5828// Emit the tail of a __tls_get_addr_opt plt call stub.
5829
5830template<int size, bool big_endian>
5831void
5832Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5833{
5834 write_insn<big_endian>(p, bctrl);
5835 p += 4;
5836 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5837 p += 4;
5838 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5839 p += 4;
5840 write_insn<big_endian>(p, mtlr_11);
5841 p += 4;
5842 write_insn<big_endian>(p, blr);
32f59844
AM
5843}
5844
e4dff765
AM
5845// Emit pc-relative plt call stub code.
5846
5847template<bool big_endian>
5848static unsigned char*
7c1f4227 5849build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
e4dff765
AM
5850{
5851 uint64_t insn;
5852 if (off - odd + (1ULL << 33) < 1ULL << 34)
5853 {
5854 off -= odd;
5855 if (odd)
5856 {
5857 write_insn<big_endian>(p, nop);
5858 p += 4;
5859 }
5860 if (load)
5861 insn = pld_12_pc;
5862 else
5863 insn = paddi_12_pc;
5864 insn |= d34(off);
5865 write_insn<big_endian>(p, insn >> 32);
5866 p += 4;
5867 write_insn<big_endian>(p, insn & 0xffffffff);
5868 }
5869 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5870 {
5871 off -= 8 - odd;
5872 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5873 p += 4;
5874 if (!odd)
5875 {
5876 write_insn<big_endian>(p, sldi_11_11_34);
5877 p += 4;
5878 }
5879 insn = paddi_12_pc | d34(off);
5880 write_insn<big_endian>(p, insn >> 32);
5881 p += 4;
5882 write_insn<big_endian>(p, insn & 0xffffffff);
5883 p += 4;
5884 if (odd)
5885 {
5886 write_insn<big_endian>(p, sldi_11_11_34);
5887 p += 4;
5888 }
5889 if (load)
5890 write_insn<big_endian>(p, ldx_12_11_12);
5891 else
5892 write_insn<big_endian>(p, add_12_11_12);
5893 }
5894 else
5895 {
5896 off -= odd + 8;
5897 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5898 p += 4;
5899 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5900 p += 4;
5901 if (odd)
5902 {
5903 write_insn<big_endian>(p, sldi_11_11_34);
5904 p += 4;
5905 }
5906 insn = paddi_12_pc | d34(off);
5907 write_insn<big_endian>(p, insn >> 32);
5908 p += 4;
5909 write_insn<big_endian>(p, insn & 0xffffffff);
5910 p += 4;
5911 if (!odd)
5912 {
5913 write_insn<big_endian>(p, sldi_11_11_34);
5914 p += 4;
5915 }
5916 if (load)
5917 write_insn<big_endian>(p, ldx_12_11_12);
5918 else
5919 write_insn<big_endian>(p, add_12_11_12);
5920 }
5921 p += 4;
5922 return p;
5923}
5924
32f59844
AM
5925// Gets the address of a label (1:) in r11 and builds an offset in r12,
5926// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5927// mflr %r12
5928// bcl 20,31,1f
5929// 1: mflr %r11
5930// mtlr %r12
5931// lis %r12,xxx-1b@highest
5932// ori %r12,%r12,xxx-1b@higher
5933// sldi %r12,%r12,32
5934// oris %r12,%r12,xxx-1b@high
5935// ori %r12,%r12,xxx-1b@l
5936// add/ldx %r12,%r11,%r12
5937
5938template<bool big_endian>
5939static unsigned char*
5940build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5941{
5942 write_insn<big_endian>(p, mflr_12);
5943 p += 4;
5944 write_insn<big_endian>(p, bcl_20_31);
5945 p += 4;
5946 write_insn<big_endian>(p, mflr_11);
5947 p += 4;
5948 write_insn<big_endian>(p, mtlr_12);
5949 p += 4;
5950 if (off + 0x8000 < 0x10000)
5951 {
5952 if (load)
5953 write_insn<big_endian>(p, ld_12_11 + l(off));
5954 else
5955 write_insn<big_endian>(p, addi_12_11 + l(off));
5956 }
5957 else if (off + 0x80008000ULL < 0x100000000ULL)
5958 {
5959 write_insn<big_endian>(p, addis_12_11 + ha(off));
5960 p += 4;
5961 if (load)
5962 write_insn<big_endian>(p, ld_12_12 + l(off));
5963 else
5964 write_insn<big_endian>(p, addi_12_12 + l(off));
5965 }
5966 else
5967 {
5968 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5969 {
5970 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5971 p += 4;
5972 }
5973 else
5974 {
5975 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5976 p += 4;
5977 if (((off >> 32) & 0xffff) != 0)
5978 {
5979 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5980 p += 4;
5981 }
5982 }
5983 if (((off >> 32) & 0xffffffffULL) != 0)
5984 {
5985 write_insn<big_endian>(p, sldi_12_12_32);
5986 p += 4;
5987 }
5988 if (hi(off) != 0)
5989 {
5990 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5991 p += 4;
5992 }
5993 if (l(off) != 0)
5994 {
5995 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5996 p += 4;
5997 }
5998 if (load)
5999 write_insn<big_endian>(p, ldx_12_11_12);
6000 else
6001 write_insn<big_endian>(p, add_12_11_12);
6002 }
6003 p += 4;
6004 return p;
6005}
6006
6007// Size of a given plt call stub.
6008
6009template<int size, bool big_endian>
6010unsigned int
6011Stub_table<size, big_endian>::plt_call_size(
afd2ea23 6012 typename Plt_stub_entries::iterator p) const
32f59844
AM
6013{
6014 if (size == 32)
6015 {
6016 const Symbol* gsym = p->first.sym_;
6017 return (4 * 4
6018 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
6019 }
6020
6021 const Output_data_plt_powerpc<size, big_endian>* plt;
6022 uint64_t plt_addr = this->plt_off(p, &plt);
6023 plt_addr += plt->address();
afd2ea23
AM
6024 if (this->targ_->power10_stubs()
6025 && this->targ_->power10_stubs_auto())
32f59844 6026 {
afd2ea23
AM
6027 unsigned int bytes = 0;
6028 if (p->second.notoc_)
6029 {
6030 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6031 bytes = 7 * 4;
6032 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6033 uint64_t odd = from & 4;
6034 uint64_t off = plt_addr - from;
6035 if (off - odd + (1ULL << 33) < 1ULL << 34)
6036 bytes += odd + 4 * 4;
6037 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6038 bytes += 7 * 4;
6039 else
6040 bytes += 8 * 4;
6041 bytes = this->plt_call_align(bytes);
6042 }
6043 unsigned int tail = 0;
6044 if (p->second.toc_)
6045 {
6046 p->second.tocoff_ = bytes;
6047 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6048 {
6049 bytes += 7 * 4;
6050 if (p->second.r2save_ && !p->second.localentry0_)
6051 {
6052 bytes += 2 * 4;
6053 tail = 4 * 4;
6054 }
6055 }
6056 if (p->second.r2save_)
6057 bytes += 4;
6058 uint64_t got_addr
6059 = this->targ_->got_section()->output_section()->address();
6060 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6061 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6062 got_addr += ppcobj->toc_base_offset();
6063 uint64_t off = plt_addr - got_addr;
6064 bytes += 3 * 4 + 4 * (ha(off) != 0);
6065 }
6066 return bytes + tail;
32f59844 6067 }
afd2ea23
AM
6068 else
6069 {
6070 unsigned int bytes = 0;
6071 unsigned int tail = 0;
6072 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6073 {
6074 bytes = 7 * 4;
6075 if (p->second.r2save_ && !p->second.localentry0_)
6076 {
6077 bytes = 9 * 4;
6078 tail = 4 * 4;
6079 }
6080 }
32f59844 6081
afd2ea23
AM
6082 if (p->second.r2save_)
6083 bytes += 4;
32f59844 6084
afd2ea23
AM
6085 if (this->targ_->power10_stubs())
6086 {
6087 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6088 uint64_t odd = from & 4;
6089 uint64_t off = plt_addr - from;
6090 if (off - odd + (1ULL << 33) < 1ULL << 34)
6091 bytes += odd + 4 * 4;
6092 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6093 bytes += 7 * 4;
6094 else
6095 bytes += 8 * 4;
6096 return bytes + tail;
6097 }
e4dff765 6098
afd2ea23 6099 if (p->second.notoc_)
32f59844 6100 {
afd2ea23
AM
6101 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6102 uint64_t off = plt_addr - from;
6103 if (off + 0x8000 < 0x10000)
6104 bytes += 7 * 4;
6105 else if (off + 0x80008000ULL < 0x100000000ULL)
6106 bytes += 8 * 4;
6107 else
6108 {
6109 bytes += 8 * 4;
6110 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6111 && ((off >> 32) & 0xffff) != 0)
6112 bytes += 4;
6113 if (((off >> 32) & 0xffffffffULL) != 0)
6114 bytes += 4;
6115 if (hi(off) != 0)
6116 bytes += 4;
6117 if (l(off) != 0)
6118 bytes += 4;
6119 }
6120 return bytes + tail;
32f59844 6121 }
32f59844 6122
afd2ea23
AM
6123 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6124 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6125 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6126 got_addr += ppcobj->toc_base_offset();
6127 uint64_t off = plt_addr - got_addr;
6128 bytes += 3 * 4 + 4 * (ha(off) != 0);
6129 if (this->targ_->abiversion() < 2)
6130 {
6131 bool static_chain = parameters->options().plt_static_chain();
6132 bool thread_safe = this->targ_->plt_thread_safe();
6133 bytes += (4
6134 + 4 * static_chain
6135 + 8 * thread_safe
6136 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6137 }
6138 return bytes + tail;
32f59844 6139 }
32f59844
AM
6140}
6141
6142// Return long branch stub size.
6143
6144template<int size, bool big_endian>
6145unsigned int
6146Stub_table<size, big_endian>::branch_stub_size(
afd2ea23 6147 typename Branch_stub_entries::iterator p,
32f59844
AM
6148 bool* need_lt)
6149{
6150 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6151 if (size == 32)
6152 {
6153 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6154 return 4;
6155 if (parameters->options().output_is_position_independent())
6156 return 32;
6157 return 16;
6158 }
6159
6160 uint64_t off = p->first.dest_ - loc;
afd2ea23 6161 unsigned int bytes = 0;
32f59844
AM
6162 if (p->second.notoc_)
6163 {
7c1f4227 6164 if (this->targ_->power10_stubs())
e4dff765
AM
6165 {
6166 Address odd = loc & 4;
6167 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6168 bytes = odd + 12;
6169 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6170 bytes = odd + 16;
6171 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6172 bytes = 28;
6173 else
6174 bytes = 32;
6175 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6176 return bytes;
6177 p->second.tocoff_ = bytes;
6178 }
6179 else
6180 {
6181 off -= 8;
6182 if (off + 0x8000 < 0x10000)
6183 return 24;
6184 if (off + 0x80008000ULL < 0x100000000ULL)
6185 {
6186 if (off + 24 + (1 << 25) < 2 << 25)
6187 return 28;
6188 return 32;
6189 }
6190
6191 bytes = 32;
6192 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6193 && ((off >> 32) & 0xffff) != 0)
6194 bytes += 4;
6195 if (((off >> 32) & 0xffffffffULL) != 0)
6196 bytes += 4;
6197 if (hi(off) != 0)
6198 bytes += 4;
6199 if (l(off) != 0)
6200 bytes += 4;
6201 return bytes;
6202 }
32f59844
AM
6203 }
6204
fa40fbe4 6205 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
32f59844 6206 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6207 return bytes + 4;
6208 if (!this->targ_->power10_stubs()
6209 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
e4dff765 6210 *need_lt = true;
afd2ea23 6211 return bytes + 16;
32f59844
AM
6212}
6213
f073a3e8
AM
6214template<int size, bool big_endian>
6215void
6216Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6217{
6218 if (p.sym_)
6219 gold_error(_("linkage table error against `%s'"),
6220 p.sym_->demangled_name().c_str());
6221 else
6222 gold_error(_("linkage table error against `%s:[local %u]'"),
6223 p.object_->name().c_str(),
6224 p.locsym_);
6225}
6226
ec661b9d 6227// Write out plt and long branch stub code.
cf43a2fe
AM
6228
6229template<int size, bool big_endian>
6230void
ec661b9d 6231Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6232{
ec661b9d
AM
6233 if (this->plt_call_stubs_.empty()
6234 && this->long_branch_stubs_.empty())
6235 return;
6236
6237 const section_size_type start_off = this->offset();
6238 const section_size_type off = this->stub_offset();
42cacb20 6239 const section_size_type oview_size =
ec661b9d 6240 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6241 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6242 unsigned char* p;
42cacb20 6243
e4dff765 6244 if (size == 64
7c1f4227 6245 && this->targ_->power10_stubs())
e4dff765 6246 {
afd2ea23
AM
6247 const Output_data_got_powerpc<size, big_endian>* got
6248 = this->targ_->got_section();
6249 Address got_os_addr = got->output_section()->address();
6250
e4dff765
AM
6251 if (!this->plt_call_stubs_.empty())
6252 {
6253 // Write out plt call stubs.
6254 typename Plt_stub_entries::const_iterator cs;
6255 for (cs = this->plt_call_stubs_.begin();
6256 cs != this->plt_call_stubs_.end();
6257 ++cs)
6258 {
6259 p = oview + cs->second.off_;
e4dff765
AM
6260 const Output_data_plt_powerpc<size, big_endian>* plt;
6261 Address pltoff = this->plt_off(cs, &plt);
6262 Address plt_addr = pltoff + plt->address();
afd2ea23
AM
6263 if (this->targ_->power10_stubs_auto())
6264 {
6265 if (cs->second.notoc_)
6266 {
6267 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6268 this->build_tls_opt_head(&p, false);
6269 Address from = this->stub_address() + (p - oview);
6270 Address delta = plt_addr - from;
6271 p = build_power10_offset<big_endian>(p, delta, from & 4,
6272 true);
6273 write_insn<big_endian>(p, mtctr_12);
6274 p += 4;
6275 write_insn<big_endian>(p, bctr);
6276 p += 4;
6277 p = oview + this->plt_call_align(p - oview);
6278 }
6279 if (cs->second.toc_)
6280 {
6281 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6282 {
6283 bool save_lr
6284 = cs->second.r2save_ && !cs->second.localentry0_;
6285 this->build_tls_opt_head(&p, save_lr);
6286 }
6287 const Powerpc_relobj<size, big_endian>* ppcobj
6288 = static_cast<const Powerpc_relobj<size, big_endian>*>(
6289 cs->first.object_);
6290 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6291 Address off = plt_addr - got_addr;
6292
6293 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6294 this->plt_error(cs->first);
6295
6296 if (cs->second.r2save_)
6297 {
6298 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6299 p += 4;
6300 }
6301 if (ha(off) != 0)
6302 {
6303 write_insn<big_endian>(p, addis_12_2 + ha(off));
6304 p += 4;
6305 write_insn<big_endian>(p, ld_12_12 + l(off));
6306 p += 4;
6307 }
6308 else
6309 {
6310 write_insn<big_endian>(p, ld_12_2 + l(off));
6311 p += 4;
6312 }
6313 write_insn<big_endian>(p, mtctr_12);
6314 p += 4;
6315 if (cs->second.r2save_
6316 && !cs->second.localentry0_
6317 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6318 this->build_tls_opt_tail(p);
6319 else
6320 write_insn<big_endian>(p, bctr);
6321 }
6322 }
6323 else
6324 {
6325 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6326 {
6327 bool save_lr
6328 = cs->second.r2save_ && !cs->second.localentry0_;
6329 this->build_tls_opt_head(&p, save_lr);
6330 }
6331 if (cs->second.r2save_)
6332 {
6333 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6334 p += 4;
6335 }
6336 Address from = this->stub_address() + (p - oview);
6337 Address delta = plt_addr - from;
6338 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6339 write_insn<big_endian>(p, mtctr_12);
6340 p += 4;
6341 if (cs->second.r2save_
6342 && !cs->second.localentry0_
6343 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6344 this->build_tls_opt_tail(p);
6345 else
6346 write_insn<big_endian>(p, bctr);
6347 }
e4dff765
AM
6348 }
6349 }
6350
6351 // Write out long branch stubs.
6352 typename Branch_stub_entries::const_iterator bs;
6353 for (bs = this->long_branch_stubs_.begin();
6354 bs != this->long_branch_stubs_.end();
6355 ++bs)
6356 {
6357 if (bs->second.save_res_)
6358 continue;
6359 Address off = this->plt_size_ + bs->second.off_;
6360 p = oview + off;
6361 Address loc = this->stub_address() + off;
6362 Address delta = bs->first.dest_ - loc;
afd2ea23 6363 if (this->targ_->power10_stubs_auto())
e4dff765 6364 {
afd2ea23
AM
6365 if (bs->second.notoc_)
6366 {
6367 unsigned char* startp = p;
6368 p = build_power10_offset<big_endian>(p, delta,
6369 loc & 4, false);
6370 delta -= p - startp;
6371 startp = p;
6372 if (delta + (1 << 25) < 2 << 25)
6373 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6374 else
6375 {
6376 write_insn<big_endian>(p, mtctr_12);
6377 p += 4;
6378 write_insn<big_endian>(p, bctr);
6379 }
6380 p += 4;
6381 delta -= p - startp;
6382 }
6383 if (bs->second.toc_)
6384 {
fa40fbe4 6385 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6386 if (delta + (1 << 25) >= 2 << 25)
6387 {
6388 Address brlt_addr
6389 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6390 gold_assert(brlt_addr != invalid_address);
6391 brlt_addr += this->targ_->brlt_section()->address();
6392 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6393 Address brltoff = brlt_addr - got_addr;
6394 if (ha(brltoff) == 0)
6395 {
6396 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6397 p += 4;
6398 }
6399 else
6400 {
6401 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6402 p += 4;
6403 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6404 p += 4;
6405 }
6406 }
6407 if (delta + (1 << 25) < 2 << 25)
6408 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6409 else
6410 {
6411 write_insn<big_endian>(p, mtctr_12);
6412 p += 4;
6413 write_insn<big_endian>(p, bctr);
6414 }
6415 }
e4dff765 6416 }
e4dff765
AM
6417 else
6418 {
fa40fbe4
AM
6419 if (!bs->second.notoc_)
6420 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6421 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6422 {
6423 unsigned char* startp = p;
6424 p = build_power10_offset<big_endian>(p, delta,
6425 loc & 4, false);
6426 delta -= p - startp;
6427 }
6428 if (delta + (1 << 25) < 2 << 25)
6429 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6430 else
6431 {
6432 write_insn<big_endian>(p, mtctr_12);
6433 p += 4;
6434 write_insn<big_endian>(p, bctr);
6435 }
e4dff765
AM
6436 }
6437 }
6438 }
6439 else if (size == 64)
cf43a2fe 6440 {
ec661b9d
AM
6441 const Output_data_got_powerpc<size, big_endian>* got
6442 = this->targ_->got_section();
dd93cd0a 6443 Address got_os_addr = got->output_section()->address();
c9269dff 6444
32f59844
AM
6445 if (!this->plt_call_stubs_.empty()
6446 && this->targ_->abiversion() >= 2)
cf43a2fe 6447 {
32f59844
AM
6448 // Write out plt call stubs for ELFv2.
6449 typename Plt_stub_entries::const_iterator cs;
6450 for (cs = this->plt_call_stubs_.begin();
6451 cs != this->plt_call_stubs_.end();
6452 ++cs)
6453 {
6454 const Output_data_plt_powerpc<size, big_endian>* plt;
6455 Address pltoff = this->plt_off(cs, &plt);
6456 Address plt_addr = pltoff + plt->address();
6457
6458 p = oview + cs->second.off_;
afd2ea23
AM
6459 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6460 {
6461 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6462 this->build_tls_opt_head(&p, save_lr);
6463 }
32f59844
AM
6464 if (cs->second.r2save_)
6465 {
6466 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6467 p += 4;
6468 }
6469 if (cs->second.notoc_)
6470 {
6471 Address from = this->stub_address() + (p - oview) + 8;
6472 Address off = plt_addr - from;
6473 p = build_notoc_offset<big_endian>(p, off, true);
6474 }
6475 else
6476 {
6477 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6478 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6479 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6480 Address off = plt_addr - got_addr;
6481
6482 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6483 this->plt_error(cs->first);
6484
6485 if (ha(off) != 0)
6486 {
6487 write_insn<big_endian>(p, addis_12_2 + ha(off));
6488 p += 4;
6489 write_insn<big_endian>(p, ld_12_12 + l(off));
6490 p += 4;
6491 }
6492 else
6493 {
6494 write_insn<big_endian>(p, ld_12_2 + l(off));
6495 p += 4;
6496 }
6497 }
6498 write_insn<big_endian>(p, mtctr_12);
6499 p += 4;
afd2ea23
AM
6500 if (cs->second.r2save_
6501 && !cs->second.localentry0_
6502 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6503 this->build_tls_opt_tail(p);
6504 else
32f59844
AM
6505 write_insn<big_endian>(p, bctr);
6506 }
6507 }
6508 else if (!this->plt_call_stubs_.empty())
6509 {
6510 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6511 typename Plt_stub_entries::const_iterator cs;
6512 for (cs = this->plt_call_stubs_.begin();
6513 cs != this->plt_call_stubs_.end();
6514 ++cs)
e5d5f5ed 6515 {
08be3224
AM
6516 const Output_data_plt_powerpc<size, big_endian>* plt;
6517 Address pltoff = this->plt_off(cs, &plt);
6518 Address plt_addr = pltoff + plt->address();
ec661b9d
AM
6519 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6520 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6521 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 6522 Address off = plt_addr - got_addr;
ec661b9d 6523
32f59844
AM
6524 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6525 || cs->second.notoc_)
f073a3e8 6526 this->plt_error(cs->first);
ec661b9d 6527
32f59844
AM
6528 bool static_chain = parameters->options().plt_static_chain();
6529 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6530 bool use_fake_dep = false;
6531 Address cmp_branch_off = 0;
407aa07c 6532 if (thread_safe)
9e69ed50
AM
6533 {
6534 unsigned int pltindex
6535 = ((pltoff - this->targ_->first_plt_entry_offset())
6536 / this->targ_->plt_entry_size());
6537 Address glinkoff
9e390558 6538 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6539 + pltindex * 8);
6540 if (pltindex > 32768)
6541 glinkoff += (pltindex - 32768) * 4;
6542 Address to
6543 = this->targ_->glink_section()->address() + glinkoff;
6544 Address from
7e57d19e
AM
6545 = (this->stub_address() + cs->second.off_ + 20
6546 + 4 * cs->second.r2save_
9e69ed50
AM
6547 + 4 * (ha(off) != 0)
6548 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6549 + 4 * static_chain);
6550 cmp_branch_off = to - from;
6551 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6552 }
6553
bdab445c 6554 p = oview + cs->second.off_;
afd2ea23
AM
6555 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6556 {
6557 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6558 this->build_tls_opt_head(&p, save_lr);
6559 use_fake_dep = thread_safe;
6560 }
32f59844 6561 if (cs->second.r2save_)
34e0882b 6562 {
32f59844 6563 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6564 p += 4;
34e0882b 6565 }
9e69ed50 6566 if (ha(off) != 0)
ec661b9d 6567 {
32f59844
AM
6568 write_insn<big_endian>(p, addis_11_2 + ha(off));
6569 p += 4;
6570 write_insn<big_endian>(p, ld_12_11 + l(off));
6571 p += 4;
6572 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6573 {
32f59844 6574 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6575 p += 4;
32f59844 6576 off = 0;
397998fc 6577 }
32f59844
AM
6578 write_insn<big_endian>(p, mtctr_12);
6579 p += 4;
6580 if (use_fake_dep)
397998fc 6581 {
32f59844 6582 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6583 p += 4;
32f59844 6584 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6585 p += 4;
6586 }
32f59844 6587 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6588 p += 4;
32f59844 6589 if (static_chain)
9e69ed50 6590 {
32f59844 6591 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6592 p += 4;
9e69ed50 6593 }
ec661b9d
AM
6594 }
6595 else
6596 {
b4f7960d
AM
6597 write_insn<big_endian>(p, ld_12_2 + l(off));
6598 p += 4;
32f59844 6599 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6600 {
b4f7960d
AM
6601 write_insn<big_endian>(p, addi_2_2 + l(off));
6602 p += 4;
9e69ed50 6603 off = 0;
ec661b9d 6604 }
b4f7960d
AM
6605 write_insn<big_endian>(p, mtctr_12);
6606 p += 4;
32f59844 6607 if (use_fake_dep)
9e69ed50 6608 {
32f59844
AM
6609 write_insn<big_endian>(p, xor_11_12_12);
6610 p += 4;
6611 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6612 p += 4;
9e69ed50 6613 }
32f59844
AM
6614 if (static_chain)
6615 {
6616 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6617 p += 4;
6618 }
6619 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6620 p += 4;
34e0882b 6621 }
afd2ea23
AM
6622 if (cs->second.r2save_
6623 && !cs->second.localentry0_
6624 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6625 this->build_tls_opt_tail(p);
34e0882b 6626 else if (thread_safe && !use_fake_dep)
9e69ed50 6627 {
b4f7960d
AM
6628 write_insn<big_endian>(p, cmpldi_2_0);
6629 p += 4;
6630 write_insn<big_endian>(p, bnectr_p4);
6631 p += 4;
9e69ed50
AM
6632 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6633 }
6634 else
407aa07c 6635 write_insn<big_endian>(p, bctr);
e5d5f5ed 6636 }
ec661b9d
AM
6637 }
6638
6639 // Write out long branch stubs.
6640 typename Branch_stub_entries::const_iterator bs;
6641 for (bs = this->long_branch_stubs_.begin();
6642 bs != this->long_branch_stubs_.end();
6643 ++bs)
6644 {
32f59844 6645 if (bs->second.save_res_)
d49044c7 6646 continue;
32f59844
AM
6647 Address off = this->plt_size_ + bs->second.off_;
6648 p = oview + off;
6649 Address loc = this->stub_address() + off;
ec661b9d 6650 Address delta = bs->first.dest_ - loc;
fa40fbe4
AM
6651 if (!bs->second.notoc_)
6652 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
32f59844
AM
6653 if (bs->second.notoc_)
6654 {
6655 unsigned char* startp = p;
6656 p = build_notoc_offset<big_endian>(p, off, false);
6657 delta -= p - startp;
6658 }
6659 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6660 {
ec661b9d
AM
6661 Address brlt_addr
6662 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6663 gold_assert(brlt_addr != invalid_address);
6664 brlt_addr += this->targ_->brlt_section()->address();
6665 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6666 Address brltoff = brlt_addr - got_addr;
6667 if (ha(brltoff) == 0)
6668 {
32f59844
AM
6669 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6670 p += 4;
ec661b9d
AM
6671 }
6672 else
cf43a2fe 6673 {
32f59844
AM
6674 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6675 p += 4;
6676 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6677 p += 4;
cf43a2fe 6678 }
32f59844
AM
6679 }
6680 if (delta + (1 << 25) < 2 << 25)
6681 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6682 else
6683 {
6684 write_insn<big_endian>(p, mtctr_12);
6685 p += 4;
407aa07c 6686 write_insn<big_endian>(p, bctr);
cf43a2fe 6687 }
ec661b9d
AM
6688 }
6689 }
32f59844 6690 else // size == 32
ec661b9d
AM
6691 {
6692 if (!this->plt_call_stubs_.empty())
6693 {
ec661b9d
AM
6694 // The address of _GLOBAL_OFFSET_TABLE_.
6695 Address g_o_t = invalid_address;
6696
6697 // Write out plt call stubs.
6698 typename Plt_stub_entries::const_iterator cs;
6699 for (cs = this->plt_call_stubs_.begin();
6700 cs != this->plt_call_stubs_.end();
6701 ++cs)
cf43a2fe 6702 {
08be3224
AM
6703 const Output_data_plt_powerpc<size, big_endian>* plt;
6704 Address plt_addr = this->plt_off(cs, &plt);
6705 plt_addr += plt->address();
ec661b9d 6706
bdab445c 6707 p = oview + cs->second.off_;
afd2ea23
AM
6708 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6709 this->build_tls_opt_head(&p, false);
ec661b9d
AM
6710 if (parameters->options().output_is_position_independent())
6711 {
6712 Address got_addr;
6713 const Powerpc_relobj<size, big_endian>* ppcobj
6714 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6715 (cs->first.object_));
6716 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6717 {
6718 unsigned int got2 = ppcobj->got2_shndx();
6719 got_addr = ppcobj->get_output_section_offset(got2);
6720 gold_assert(got_addr != invalid_address);
6721 got_addr += (ppcobj->output_section(got2)->address()
6722 + cs->first.addend_);
6723 }
6724 else
6725 {
6726 if (g_o_t == invalid_address)
6727 {
6728 const Output_data_got_powerpc<size, big_endian>* got
6729 = this->targ_->got_section();
6730 g_o_t = got->address() + got->g_o_t();
6731 }
6732 got_addr = g_o_t;
6733 }
6734
9e69ed50
AM
6735 Address off = plt_addr - got_addr;
6736 if (ha(off) == 0)
9e390558 6737 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6738 else
6739 {
9e390558
AM
6740 write_insn<big_endian>(p, addis_11_30 + ha(off));
6741 p += 4;
6742 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6743 }
6744 }
6745 else
6746 {
9e390558
AM
6747 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6748 p += 4;
6749 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6750 }
9e390558
AM
6751 p += 4;
6752 write_insn<big_endian>(p, mtctr_11);
6753 p += 4;
407aa07c 6754 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6755 }
6756 }
6757
6758 // Write out long branch stubs.
6759 typename Branch_stub_entries::const_iterator bs;
6760 for (bs = this->long_branch_stubs_.begin();
6761 bs != this->long_branch_stubs_.end();
6762 ++bs)
6763 {
32f59844 6764 if (bs->second.save_res_)
d49044c7 6765 continue;
32f59844
AM
6766 Address off = this->plt_size_ + bs->second.off_;
6767 p = oview + off;
6768 Address loc = this->stub_address() + off;
ec661b9d
AM
6769 Address delta = bs->first.dest_ - loc;
6770 if (delta + (1 << 25) < 2 << 25)
6771 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6772 else if (!parameters->options().output_is_position_independent())
6773 {
9e390558
AM
6774 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6775 p += 4;
6776 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6777 }
6778 else
6779 {
6780 delta -= 8;
9e390558
AM
6781 write_insn<big_endian>(p, mflr_0);
6782 p += 4;
6783 write_insn<big_endian>(p, bcl_20_31);
6784 p += 4;
6785 write_insn<big_endian>(p, mflr_12);
6786 p += 4;
6787 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6788 p += 4;
6789 write_insn<big_endian>(p, addi_12_12 + l(delta));
6790 p += 4;
6791 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6792 }
9e390558
AM
6793 p += 4;
6794 write_insn<big_endian>(p, mtctr_12);
6795 p += 4;
407aa07c 6796 write_insn<big_endian>(p, bctr);
cf43a2fe 6797 }
ec661b9d 6798 }
d49044c7
AM
6799 if (this->need_save_res_)
6800 {
6801 p = oview + this->plt_size_ + this->branch_size_;
6802 memcpy (p, this->targ_->savres_section()->contents(),
6803 this->targ_->savres_section()->data_size());
6804 }
ec661b9d
AM
6805}
6806
6807// Write out .glink.
6808
6809template<int size, bool big_endian>
6810void
6811Output_data_glink<size, big_endian>::do_write(Output_file* of)
6812{
6813 const section_size_type off = this->offset();
6814 const section_size_type oview_size =
6815 convert_to_section_size_type(this->data_size());
6816 unsigned char* const oview = of->get_output_view(off, oview_size);
6817 unsigned char* p;
6818
6819 // The base address of the .plt section.
6820 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6821 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 6822
ec661b9d
AM
6823 if (size == 64)
6824 {
9055360d 6825 if (this->end_branch_table_ != 0)
b4f7960d 6826 {
9055360d
AM
6827 // Write pltresolve stub.
6828 p = oview;
6829 Address after_bcl = this->address() + 16;
6830 Address pltoff = plt_base - after_bcl;
6831
6832 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 6833
b4f7960d 6834 if (this->targ_->abiversion() < 2)
cf43a2fe 6835 {
9055360d
AM
6836 write_insn<big_endian>(p, mflr_12), p += 4;
6837 write_insn<big_endian>(p, bcl_20_31), p += 4;
6838 write_insn<big_endian>(p, mflr_11), p += 4;
6839 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6840 write_insn<big_endian>(p, mtlr_12), p += 4;
6841 write_insn<big_endian>(p, add_11_2_11), p += 4;
6842 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6843 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6844 write_insn<big_endian>(p, mtctr_12), p += 4;
6845 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6846 }
6847 else
6848 {
63e5eea2
AM
6849 if (this->targ_->has_localentry0())
6850 {
6851 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6852 }
9055360d
AM
6853 write_insn<big_endian>(p, mflr_0), p += 4;
6854 write_insn<big_endian>(p, bcl_20_31), p += 4;
6855 write_insn<big_endian>(p, mflr_11), p += 4;
9055360d 6856 write_insn<big_endian>(p, mtlr_0), p += 4;
63e5eea2
AM
6857 if (this->targ_->has_localentry0())
6858 {
6859 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
6860 }
6861 else
6862 {
6863 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
6864 }
9055360d 6865 write_insn<big_endian>(p, sub_12_12_11), p += 4;
63e5eea2
AM
6866 write_insn<big_endian>(p, add_11_0_11), p += 4;
6867 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
9055360d
AM
6868 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6869 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6870 write_insn<big_endian>(p, mtctr_12), p += 4;
6871 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6872 }
407aa07c 6873 write_insn<big_endian>(p, bctr), p += 4;
9e390558 6874 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
6875
6876 // Write lazy link call stubs.
6877 uint32_t indx = 0;
6878 while (p < oview + this->end_branch_table_)
6879 {
6880 if (this->targ_->abiversion() < 2)
b4f7960d 6881 {
9055360d
AM
6882 if (indx < 0x8000)
6883 {
6884 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6885 }
6886 else
6887 {
bbec1a5d 6888 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
6889 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6890 }
b4f7960d 6891 }
9055360d
AM
6892 uint32_t branch_off = 8 - (p - oview);
6893 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6894 indx++;
cf43a2fe 6895 }
9055360d
AM
6896 }
6897
6898 Address plt_base = this->targ_->plt_section()->address();
6899 Address iplt_base = invalid_address;
9e390558 6900 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
6901 Address global_entry_base = this->address() + global_entry_off;
6902 typename Global_entry_stub_entries::const_iterator ge;
6903 for (ge = this->global_entry_stubs_.begin();
6904 ge != this->global_entry_stubs_.end();
6905 ++ge)
6906 {
6907 p = oview + global_entry_off + ge->second;
6908 Address plt_addr = ge->first->plt_offset();
6909 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6910 && ge->first->can_use_relative_reloc(false))
6911 {
6912 if (iplt_base == invalid_address)
6913 iplt_base = this->targ_->iplt_section()->address();
6914 plt_addr += iplt_base;
6915 }
6916 else
6917 plt_addr += plt_base;
6918 Address my_addr = global_entry_base + ge->second;
6919 Address off = plt_addr - my_addr;
6920
6921 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 6922 gold_error(_("linkage table error against `%s'"),
9055360d
AM
6923 ge->first->demangled_name().c_str());
6924
6925 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6926 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6927 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 6928 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
6929 }
6930 }
6931 else
6932 {
ec661b9d
AM
6933 const Output_data_got_powerpc<size, big_endian>* got
6934 = this->targ_->got_section();
dd93cd0a
AM
6935 // The address of _GLOBAL_OFFSET_TABLE_.
6936 Address g_o_t = got->address() + got->g_o_t();
c9269dff 6937
cf43a2fe 6938 // Write out pltresolve branch table.
ec661b9d 6939 p = oview;
9e390558 6940 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 6941 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
6942 while (p < end_p - 8 * 4)
6943 write_insn<big_endian>(p, b + end_p - p), p += 4;
6944 while (p < end_p)
6945 write_insn<big_endian>(p, nop), p += 4;
42cacb20 6946
cf43a2fe 6947 // Write out pltresolve call stub.
9e390558 6948 end_p = oview + oview_size;
cf43a2fe 6949 if (parameters->options().output_is_position_independent())
42cacb20 6950 {
ec661b9d 6951 Address res0_off = 0;
dd93cd0a
AM
6952 Address after_bcl_off = the_end + 12;
6953 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 6954
9e390558
AM
6955 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6956 p += 4;
6957 write_insn<big_endian>(p, mflr_0);
6958 p += 4;
6959 write_insn<big_endian>(p, bcl_20_31);
6960 p += 4;
6961 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6962 p += 4;
6963 write_insn<big_endian>(p, mflr_12);
6964 p += 4;
6965 write_insn<big_endian>(p, mtlr_0);
6966 p += 4;
6967 write_insn<big_endian>(p, sub_11_11_12);
6968 p += 4;
cf43a2fe 6969
dd93cd0a 6970 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 6971
9e390558
AM
6972 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6973 p += 4;
cf43a2fe
AM
6974 if (ha(got_bcl) == ha(got_bcl + 4))
6975 {
9e390558
AM
6976 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6977 p += 4;
6978 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
6979 }
6980 else
6981 {
9e390558
AM
6982 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6983 p += 4;
6984 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 6985 }
9e390558
AM
6986 p += 4;
6987 write_insn<big_endian>(p, mtctr_0);
6988 p += 4;
6989 write_insn<big_endian>(p, add_0_11_11);
6990 p += 4;
6991 write_insn<big_endian>(p, add_11_0_11);
42cacb20 6992 }
cf43a2fe 6993 else
42cacb20 6994 {
ec661b9d 6995 Address res0 = this->address();
cf43a2fe 6996
9e390558
AM
6997 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6998 p += 4;
6999 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7000 p += 4;
cf43a2fe 7001 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7002 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 7003 else
9e390558
AM
7004 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7005 p += 4;
7006 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7007 p += 4;
7008 write_insn<big_endian>(p, mtctr_0);
7009 p += 4;
7010 write_insn<big_endian>(p, add_0_11_11);
7011 p += 4;
cf43a2fe 7012 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7013 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 7014 else
9e390558
AM
7015 write_insn<big_endian>(p, lwz_12_12 + 4);
7016 p += 4;
7017 write_insn<big_endian>(p, add_11_0_11);
7018 }
7019 p += 4;
407aa07c
AM
7020 write_insn<big_endian>(p, bctr);
7021 p += 4;
9e390558
AM
7022 while (p < end_p)
7023 {
7024 write_insn<big_endian>(p, nop);
7025 p += 4;
42cacb20
DE
7026 }
7027 }
7028
cf43a2fe
AM
7029 of->write_output_view(off, oview_size, oview);
7030}
7031
f3a0ed29
AM
7032
7033// A class to handle linker generated save/restore functions.
7034
7035template<int size, bool big_endian>
7036class Output_data_save_res : public Output_section_data_build
7037{
7038 public:
7039 Output_data_save_res(Symbol_table* symtab);
7040
d49044c7
AM
7041 const unsigned char*
7042 contents() const
7043 {
7044 return contents_;
7045 }
7046
f3a0ed29
AM
7047 protected:
7048 // Write to a map file.
7049 void
7050 do_print_to_mapfile(Mapfile* mapfile) const
7051 { mapfile->print_output_data(this, _("** save/restore")); }
7052
7053 void
7054 do_write(Output_file*);
7055
7056 private:
7057 // The maximum size of save/restore contents.
7058 static const unsigned int savres_max = 218*4;
7059
7060 void
7061 savres_define(Symbol_table* symtab,
7062 const char *name,
7063 unsigned int lo, unsigned int hi,
7064 unsigned char* write_ent(unsigned char*, int),
7065 unsigned char* write_tail(unsigned char*, int));
7066
7067 unsigned char *contents_;
7068};
7069
7070template<bool big_endian>
7071static unsigned char*
7072savegpr0(unsigned char* p, int r)
7073{
7074 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7075 write_insn<big_endian>(p, insn);
7076 return p + 4;
7077}
7078
7079template<bool big_endian>
7080static unsigned char*
7081savegpr0_tail(unsigned char* p, int r)
7082{
7083 p = savegpr0<big_endian>(p, r);
7084 uint32_t insn = std_0_1 + 16;
7085 write_insn<big_endian>(p, insn);
7086 p = p + 4;
7087 write_insn<big_endian>(p, blr);
7088 return p + 4;
7089}
7090
7091template<bool big_endian>
62fe925a 7092static unsigned char*
f3a0ed29
AM
7093restgpr0(unsigned char* p, int r)
7094{
7095 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7096 write_insn<big_endian>(p, insn);
7097 return p + 4;
7098}
7099
7100template<bool big_endian>
62fe925a 7101static unsigned char*
f3a0ed29
AM
7102restgpr0_tail(unsigned char* p, int r)
7103{
7104 uint32_t insn = ld_0_1 + 16;
7105 write_insn<big_endian>(p, insn);
7106 p = p + 4;
7107 p = restgpr0<big_endian>(p, r);
7108 write_insn<big_endian>(p, mtlr_0);
7109 p = p + 4;
7110 if (r == 29)
7111 {
7112 p = restgpr0<big_endian>(p, 30);
7113 p = restgpr0<big_endian>(p, 31);
7114 }
7115 write_insn<big_endian>(p, blr);
7116 return p + 4;
7117}
7118
7119template<bool big_endian>
62fe925a 7120static unsigned char*
f3a0ed29
AM
7121savegpr1(unsigned char* p, int r)
7122{
7123 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7124 write_insn<big_endian>(p, insn);
7125 return p + 4;
7126}
7127
7128template<bool big_endian>
62fe925a 7129static unsigned char*
f3a0ed29
AM
7130savegpr1_tail(unsigned char* p, int r)
7131{
7132 p = savegpr1<big_endian>(p, r);
7133 write_insn<big_endian>(p, blr);
7134 return p + 4;
7135}
7136
7137template<bool big_endian>
62fe925a 7138static unsigned char*
f3a0ed29
AM
7139restgpr1(unsigned char* p, int r)
7140{
7141 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7142 write_insn<big_endian>(p, insn);
7143 return p + 4;
7144}
7145
7146template<bool big_endian>
62fe925a 7147static unsigned char*
f3a0ed29
AM
7148restgpr1_tail(unsigned char* p, int r)
7149{
7150 p = restgpr1<big_endian>(p, r);
7151 write_insn<big_endian>(p, blr);
7152 return p + 4;
7153}
7154
7155template<bool big_endian>
62fe925a 7156static unsigned char*
f3a0ed29
AM
7157savefpr(unsigned char* p, int r)
7158{
7159 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7160 write_insn<big_endian>(p, insn);
7161 return p + 4;
7162}
7163
7164template<bool big_endian>
62fe925a 7165static unsigned char*
f3a0ed29
AM
7166savefpr0_tail(unsigned char* p, int r)
7167{
7168 p = savefpr<big_endian>(p, r);
7169 write_insn<big_endian>(p, std_0_1 + 16);
7170 p = p + 4;
7171 write_insn<big_endian>(p, blr);
7172 return p + 4;
7173}
7174
7175template<bool big_endian>
62fe925a 7176static unsigned char*
f3a0ed29
AM
7177restfpr(unsigned char* p, int r)
7178{
7179 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7180 write_insn<big_endian>(p, insn);
7181 return p + 4;
7182}
7183
7184template<bool big_endian>
62fe925a 7185static unsigned char*
f3a0ed29
AM
7186restfpr0_tail(unsigned char* p, int r)
7187{
7188 write_insn<big_endian>(p, ld_0_1 + 16);
7189 p = p + 4;
7190 p = restfpr<big_endian>(p, r);
7191 write_insn<big_endian>(p, mtlr_0);
7192 p = p + 4;
7193 if (r == 29)
7194 {
7195 p = restfpr<big_endian>(p, 30);
7196 p = restfpr<big_endian>(p, 31);
7197 }
7198 write_insn<big_endian>(p, blr);
7199 return p + 4;
7200}
7201
7202template<bool big_endian>
62fe925a 7203static unsigned char*
f3a0ed29
AM
7204savefpr1_tail(unsigned char* p, int r)
7205{
7206 p = savefpr<big_endian>(p, r);
7207 write_insn<big_endian>(p, blr);
7208 return p + 4;
7209}
7210
7211template<bool big_endian>
62fe925a 7212static unsigned char*
f3a0ed29
AM
7213restfpr1_tail(unsigned char* p, int r)
7214{
7215 p = restfpr<big_endian>(p, r);
7216 write_insn<big_endian>(p, blr);
7217 return p + 4;
7218}
7219
7220template<bool big_endian>
62fe925a 7221static unsigned char*
f3a0ed29
AM
7222savevr(unsigned char* p, int r)
7223{
7224 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7225 write_insn<big_endian>(p, insn);
7226 p = p + 4;
7227 insn = stvx_0_12_0 + (r << 21);
7228 write_insn<big_endian>(p, insn);
7229 return p + 4;
7230}
7231
7232template<bool big_endian>
62fe925a 7233static unsigned char*
f3a0ed29
AM
7234savevr_tail(unsigned char* p, int r)
7235{
7236 p = savevr<big_endian>(p, r);
7237 write_insn<big_endian>(p, blr);
7238 return p + 4;
7239}
7240
7241template<bool big_endian>
62fe925a 7242static unsigned char*
f3a0ed29
AM
7243restvr(unsigned char* p, int r)
7244{
7245 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7246 write_insn<big_endian>(p, insn);
7247 p = p + 4;
7248 insn = lvx_0_12_0 + (r << 21);
7249 write_insn<big_endian>(p, insn);
7250 return p + 4;
7251}
7252
7253template<bool big_endian>
62fe925a 7254static unsigned char*
f3a0ed29
AM
7255restvr_tail(unsigned char* p, int r)
7256{
7257 p = restvr<big_endian>(p, r);
7258 write_insn<big_endian>(p, blr);
7259 return p + 4;
7260}
7261
7262
7263template<int size, bool big_endian>
7264Output_data_save_res<size, big_endian>::Output_data_save_res(
7265 Symbol_table* symtab)
7266 : Output_section_data_build(4),
7267 contents_(NULL)
7268{
7269 this->savres_define(symtab,
7270 "_savegpr0_", 14, 31,
7271 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7272 this->savres_define(symtab,
7273 "_restgpr0_", 14, 29,
7274 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7275 this->savres_define(symtab,
7276 "_restgpr0_", 30, 31,
7277 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7278 this->savres_define(symtab,
7279 "_savegpr1_", 14, 31,
7280 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7281 this->savres_define(symtab,
7282 "_restgpr1_", 14, 31,
7283 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7284 this->savres_define(symtab,
7285 "_savefpr_", 14, 31,
7286 savefpr<big_endian>, savefpr0_tail<big_endian>);
7287 this->savres_define(symtab,
7288 "_restfpr_", 14, 29,
7289 restfpr<big_endian>, restfpr0_tail<big_endian>);
7290 this->savres_define(symtab,
7291 "_restfpr_", 30, 31,
7292 restfpr<big_endian>, restfpr0_tail<big_endian>);
7293 this->savres_define(symtab,
7294 "._savef", 14, 31,
7295 savefpr<big_endian>, savefpr1_tail<big_endian>);
7296 this->savres_define(symtab,
7297 "._restf", 14, 31,
7298 restfpr<big_endian>, restfpr1_tail<big_endian>);
7299 this->savres_define(symtab,
7300 "_savevr_", 20, 31,
7301 savevr<big_endian>, savevr_tail<big_endian>);
7302 this->savres_define(symtab,
7303 "_restvr_", 20, 31,
7304 restvr<big_endian>, restvr_tail<big_endian>);
7305}
7306
7307template<int size, bool big_endian>
7308void
7309Output_data_save_res<size, big_endian>::savres_define(
7310 Symbol_table* symtab,
7311 const char *name,
7312 unsigned int lo, unsigned int hi,
7313 unsigned char* write_ent(unsigned char*, int),
7314 unsigned char* write_tail(unsigned char*, int))
7315{
7316 size_t len = strlen(name);
7317 bool writing = false;
7318 char sym[16];
7319
7320 memcpy(sym, name, len);
7321 sym[len + 2] = 0;
7322
7323 for (unsigned int i = lo; i <= hi; i++)
7324 {
7325 sym[len + 0] = i / 10 + '0';
7326 sym[len + 1] = i % 10 + '0';
7327 Symbol* gsym = symtab->lookup(sym);
7328 bool refd = gsym != NULL && gsym->is_undefined();
7329 writing = writing || refd;
7330 if (writing)
7331 {
7332 if (this->contents_ == NULL)
7333 this->contents_ = new unsigned char[this->savres_max];
7334
ec661b9d 7335 section_size_type value = this->current_data_size();
f3a0ed29
AM
7336 unsigned char* p = this->contents_ + value;
7337 if (i != hi)
7338 p = write_ent(p, i);
7339 else
7340 p = write_tail(p, i);
ec661b9d 7341 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7342 this->set_current_data_size(cur_size);
7343 if (refd)
7344 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7345 this, value, cur_size - value,
7346 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7347 elfcpp::STV_HIDDEN, 0, false, false);
7348 }
7349 }
7350}
7351
7352// Write out save/restore.
7353
7354template<int size, bool big_endian>
7355void
7356Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7357{
ec661b9d 7358 const section_size_type off = this->offset();
f3a0ed29
AM
7359 const section_size_type oview_size =
7360 convert_to_section_size_type(this->data_size());
7361 unsigned char* const oview = of->get_output_view(off, oview_size);
7362 memcpy(oview, this->contents_, oview_size);
7363 of->write_output_view(off, oview_size, oview);
7364}
7365
7366
cf43a2fe 7367// Create the glink section.
42cacb20 7368
cf43a2fe
AM
7369template<int size, bool big_endian>
7370void
7371Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7372{
7373 if (this->glink_ == NULL)
7374 {
7375 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7376 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7377 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7378 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7379 this->glink_, ORDER_TEXT, false);
7380 }
42cacb20
DE
7381}
7382
7383// Create a PLT entry for a global symbol.
7384
7385template<int size, bool big_endian>
7386void
ec661b9d
AM
7387Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7388 Layout* layout,
7389 Symbol* gsym)
42cacb20 7390{
e5d5f5ed
AM
7391 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7392 && gsym->can_use_relative_reloc(false))
7393 {
7394 if (this->iplt_ == NULL)
40b469d7 7395 this->make_iplt_section(symtab, layout);
03e25981 7396 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7397 }
7398 else
7399 {
7400 if (this->plt_ == NULL)
40b469d7 7401 this->make_plt_section(symtab, layout);
03e25981 7402 this->plt_->add_entry(gsym);
e5d5f5ed 7403 }
e5d5f5ed 7404}
42cacb20 7405
2d7ad24e
AM
7406// Make a PLT entry for a local symbol.
7407
7408template<int size, bool big_endian>
7409void
7410Target_powerpc<size, big_endian>::make_local_plt_entry(
7411 Layout* layout,
7412 Sized_relobj_file<size, big_endian>* relobj,
7413 unsigned int r_sym)
7414{
7415 if (this->lplt_ == NULL)
7416 this->make_lplt_section(layout);
7417 this->lplt_->add_local_entry(relobj, r_sym);
7418}
7419
e5d5f5ed 7420// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7421
e5d5f5ed
AM
7422template<int size, bool big_endian>
7423void
7424Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7425 Symbol_table* symtab,
e5d5f5ed 7426 Layout* layout,
ec661b9d
AM
7427 Sized_relobj_file<size, big_endian>* relobj,
7428 unsigned int r_sym)
e5d5f5ed
AM
7429{
7430 if (this->iplt_ == NULL)
40b469d7 7431 this->make_iplt_section(symtab, layout);
03e25981 7432 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7433}
7434
0e70b911
CC
7435// Return the number of entries in the PLT.
7436
7437template<int size, bool big_endian>
7438unsigned int
7439Target_powerpc<size, big_endian>::plt_entry_count() const
7440{
7441 if (this->plt_ == NULL)
7442 return 0;
b3ccdeb5 7443 return this->plt_->entry_count();
0e70b911
CC
7444}
7445
dd93cd0a 7446// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7447
7448template<int size, bool big_endian>
7449unsigned int
dd93cd0a 7450Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7451 Symbol_table* symtab,
7452 Layout* layout,
7453 Sized_relobj_file<size, big_endian>* object)
42cacb20 7454{
dd93cd0a 7455 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7456 {
7457 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7458 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
7459 Output_data_got_powerpc<size, big_endian>* got
7460 = this->got_section(symtab, layout);
7461 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7462 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7463 got_offset, 0);
dd93cd0a 7464 this->tlsld_got_offset_ = got_offset;
42cacb20 7465 }
dd93cd0a 7466 return this->tlsld_got_offset_;
42cacb20
DE
7467}
7468
95a2c8d6
RS
7469// Get the Reference_flags for a particular relocation.
7470
7471template<int size, bool big_endian>
7472int
88b8e639
AM
7473Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7474 unsigned int r_type,
7475 const Target_powerpc* target)
95a2c8d6 7476{
88b8e639
AM
7477 int ref = 0;
7478
95a2c8d6
RS
7479 switch (r_type)
7480 {
7481 case elfcpp::R_POWERPC_NONE:
7482 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7483 case elfcpp::R_POWERPC_GNU_VTENTRY:
7484 case elfcpp::R_PPC64_TOC:
7485 // No symbol reference.
88b8e639 7486 break;
95a2c8d6 7487
dd93cd0a
AM
7488 case elfcpp::R_PPC64_ADDR64:
7489 case elfcpp::R_PPC64_UADDR64:
7490 case elfcpp::R_POWERPC_ADDR32:
7491 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 7492 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 7493 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
7494 case elfcpp::R_POWERPC_ADDR16_LO:
7495 case elfcpp::R_POWERPC_ADDR16_HI:
7496 case elfcpp::R_POWERPC_ADDR16_HA:
89c52ae3
AM
7497 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7498 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7499 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7500 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7501 case elfcpp::R_PPC64_D34:
7502 case elfcpp::R_PPC64_D34_LO:
7503 case elfcpp::R_PPC64_D34_HI30:
7504 case elfcpp::R_PPC64_D34_HA30:
7505 case elfcpp::R_PPC64_D28:
88b8e639
AM
7506 ref = Symbol::ABSOLUTE_REF;
7507 break;
95a2c8d6 7508
dd93cd0a
AM
7509 case elfcpp::R_POWERPC_ADDR24:
7510 case elfcpp::R_POWERPC_ADDR14:
7511 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7512 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7513 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7514 break;
dd93cd0a 7515
e5d5f5ed 7516 case elfcpp::R_PPC64_REL64:
dd93cd0a 7517 case elfcpp::R_POWERPC_REL32:
95a2c8d6 7518 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
7519 case elfcpp::R_POWERPC_REL16:
7520 case elfcpp::R_POWERPC_REL16_LO:
7521 case elfcpp::R_POWERPC_REL16_HI:
7522 case elfcpp::R_POWERPC_REL16_HA:
c432bbba
AM
7523 case elfcpp::R_PPC64_REL16_HIGH:
7524 case elfcpp::R_PPC64_REL16_HIGHA:
7525 case elfcpp::R_PPC64_REL16_HIGHER:
7526 case elfcpp::R_PPC64_REL16_HIGHERA:
7527 case elfcpp::R_PPC64_REL16_HIGHEST:
7528 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7529 case elfcpp::R_PPC64_PCREL34:
7530 case elfcpp::R_PPC64_REL16_HIGHER34:
7531 case elfcpp::R_PPC64_REL16_HIGHERA34:
7532 case elfcpp::R_PPC64_REL16_HIGHEST34:
7533 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7534 case elfcpp::R_PPC64_PCREL28:
88b8e639
AM
7535 ref = Symbol::RELATIVE_REF;
7536 break;
95a2c8d6 7537
32f59844
AM
7538 case elfcpp::R_PPC64_REL24_NOTOC:
7539 if (size == 32)
7540 break;
7541 // Fall through.
dd93cd0a 7542 case elfcpp::R_POWERPC_REL24:
95a2c8d6 7543 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
7544 case elfcpp::R_POWERPC_REL14:
7545 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7546 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
7547 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7548 break;
95a2c8d6
RS
7549
7550 case elfcpp::R_POWERPC_GOT16:
7551 case elfcpp::R_POWERPC_GOT16_LO:
7552 case elfcpp::R_POWERPC_GOT16_HI:
7553 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
7554 case elfcpp::R_PPC64_GOT16_DS:
7555 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7556 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7557 case elfcpp::R_PPC64_TOC16:
7558 case elfcpp::R_PPC64_TOC16_LO:
7559 case elfcpp::R_PPC64_TOC16_HI:
7560 case elfcpp::R_PPC64_TOC16_HA:
7561 case elfcpp::R_PPC64_TOC16_DS:
7562 case elfcpp::R_PPC64_TOC16_LO_DS:
08be3224
AM
7563 case elfcpp::R_POWERPC_PLT16_LO:
7564 case elfcpp::R_POWERPC_PLT16_HI:
7565 case elfcpp::R_POWERPC_PLT16_HA:
7566 case elfcpp::R_PPC64_PLT16_LO_DS:
e4dff765
AM
7567 case elfcpp::R_PPC64_PLT_PCREL34:
7568 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
32d849b3 7569 ref = Symbol::RELATIVE_REF;
88b8e639 7570 break;
95a2c8d6
RS
7571
7572 case elfcpp::R_POWERPC_GOT_TPREL16:
7573 case elfcpp::R_POWERPC_TLS:
89c52ae3
AM
7574 case elfcpp::R_PPC64_TLSGD:
7575 case elfcpp::R_PPC64_TLSLD:
7576 case elfcpp::R_PPC64_TPREL34:
7577 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
7578 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7579 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7580 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7581 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
88b8e639
AM
7582 ref = Symbol::TLS_REF;
7583 break;
95a2c8d6
RS
7584
7585 case elfcpp::R_POWERPC_COPY:
7586 case elfcpp::R_POWERPC_GLOB_DAT:
7587 case elfcpp::R_POWERPC_JMP_SLOT:
7588 case elfcpp::R_POWERPC_RELATIVE:
7589 case elfcpp::R_POWERPC_DTPMOD:
7590 default:
7591 // Not expected. We will give an error later.
88b8e639 7592 break;
95a2c8d6 7593 }
88b8e639
AM
7594
7595 if (size == 64 && target->abiversion() < 2)
7596 ref |= Symbol::FUNC_DESC_ABI;
7597 return ref;
95a2c8d6
RS
7598}
7599
42cacb20
DE
7600// Report an unsupported relocation against a local symbol.
7601
7602template<int size, bool big_endian>
7603void
7604Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7605 Sized_relobj_file<size, big_endian>* object,
7606 unsigned int r_type)
42cacb20
DE
7607{
7608 gold_error(_("%s: unsupported reloc %u against local symbol"),
7609 object->name().c_str(), r_type);
7610}
7611
7612// We are about to emit a dynamic relocation of type R_TYPE. If the
7613// dynamic linker does not support it, issue an error.
7614
7615template<int size, bool big_endian>
7616void
7617Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7618 unsigned int r_type)
7619{
7620 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7621
7622 // These are the relocation types supported by glibc for both 32-bit
7623 // and 64-bit powerpc.
7624 switch (r_type)
7625 {
3ea0a085 7626 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7627 case elfcpp::R_POWERPC_RELATIVE:
7628 case elfcpp::R_POWERPC_GLOB_DAT:
7629 case elfcpp::R_POWERPC_DTPMOD:
7630 case elfcpp::R_POWERPC_DTPREL:
7631 case elfcpp::R_POWERPC_TPREL:
7632 case elfcpp::R_POWERPC_JMP_SLOT:
7633 case elfcpp::R_POWERPC_COPY:
3ea0a085 7634 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7635 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7636 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7637 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7638 case elfcpp::R_POWERPC_ADDR16:
7639 case elfcpp::R_POWERPC_UADDR16:
7640 case elfcpp::R_POWERPC_ADDR16_LO:
7641 case elfcpp::R_POWERPC_ADDR16_HI:
7642 case elfcpp::R_POWERPC_ADDR16_HA:
7643 case elfcpp::R_POWERPC_ADDR14:
7644 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7645 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7646 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7647 case elfcpp::R_POWERPC_TPREL16:
7648 case elfcpp::R_POWERPC_TPREL16_LO:
7649 case elfcpp::R_POWERPC_TPREL16_HI:
7650 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7651 return;
7652
7653 default:
7654 break;
7655 }
7656
7657 if (size == 64)
7658 {
7659 switch (r_type)
7660 {
7661 // These are the relocation types supported only on 64-bit.
7662 case elfcpp::R_PPC64_ADDR64:
42cacb20 7663 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7664 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7665 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7666 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7667 case elfcpp::R_PPC64_ADDR16_HIGH:
7668 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7669 case elfcpp::R_PPC64_ADDR16_HIGHER:
7670 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7671 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7672 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7673 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7674 case elfcpp::R_POWERPC_ADDR30:
7675 case elfcpp::R_PPC64_TPREL16_DS:
7676 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7677 case elfcpp::R_PPC64_TPREL16_HIGH:
7678 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7679 case elfcpp::R_PPC64_TPREL16_HIGHER:
7680 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7681 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7682 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7683 return;
7684
7685 default:
7686 break;
7687 }
7688 }
7689 else
7690 {
7691 switch (r_type)
7692 {
7693 // These are the relocation types supported only on 32-bit.
3ea0a085 7694 // ??? glibc ld.so doesn't need to support these.
e59a1001 7695 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7696 case elfcpp::R_POWERPC_DTPREL16:
7697 case elfcpp::R_POWERPC_DTPREL16_LO:
7698 case elfcpp::R_POWERPC_DTPREL16_HI:
7699 case elfcpp::R_POWERPC_DTPREL16_HA:
7700 return;
42cacb20
DE
7701
7702 default:
7703 break;
7704 }
7705 }
7706
7707 // This prevents us from issuing more than one error per reloc
7708 // section. But we can still wind up issuing more than one
7709 // error per object file.
7710 if (this->issued_non_pic_error_)
7711 return;
33aea2fd 7712 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7713 object->error(_("requires unsupported dynamic reloc; "
7714 "recompile with -fPIC"));
7715 this->issued_non_pic_error_ = true;
7716 return;
7717}
7718
e5d5f5ed
AM
7719// Return whether we need to make a PLT entry for a relocation of the
7720// given type against a STT_GNU_IFUNC symbol.
7721
7722template<int size, bool big_endian>
7723bool
7724Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7725 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7726 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7727 unsigned int r_type,
7728 bool report_err)
e5d5f5ed 7729{
c9824451
AM
7730 // In non-pic code any reference will resolve to the plt call stub
7731 // for the ifunc symbol.
9055360d
AM
7732 if ((size == 32 || target->abiversion() >= 2)
7733 && !parameters->options().output_is_position_independent())
c9824451
AM
7734 return true;
7735
e5d5f5ed
AM
7736 switch (r_type)
7737 {
b3ccdeb5 7738 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7739 case elfcpp::R_POWERPC_ADDR32:
7740 case elfcpp::R_POWERPC_UADDR32:
7741 if (size == 32)
b3ccdeb5 7742 return false;
e5d5f5ed
AM
7743 break;
7744
7745 case elfcpp::R_PPC64_ADDR64:
7746 case elfcpp::R_PPC64_UADDR64:
7747 if (size == 64)
b3ccdeb5 7748 return false;
e5d5f5ed
AM
7749 break;
7750
b3ccdeb5 7751 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7752 case elfcpp::R_POWERPC_GOT16:
7753 case elfcpp::R_POWERPC_GOT16_LO:
7754 case elfcpp::R_POWERPC_GOT16_HI:
7755 case elfcpp::R_POWERPC_GOT16_HA:
7756 case elfcpp::R_PPC64_GOT16_DS:
7757 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7758 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7759 return false;
e5d5f5ed 7760
08be3224
AM
7761 // PLT relocs are OK and need a PLT entry.
7762 case elfcpp::R_POWERPC_PLT16_LO:
7763 case elfcpp::R_POWERPC_PLT16_HI:
7764 case elfcpp::R_POWERPC_PLT16_HA:
7765 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
7766 case elfcpp::R_POWERPC_PLTSEQ:
7767 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7768 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7769 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7770 case elfcpp::R_PPC64_PLT_PCREL34:
7771 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
7772 return true;
7773 break;
7774
b3ccdeb5 7775 // Function calls are good, and these do need a PLT entry.
32f59844
AM
7776 case elfcpp::R_PPC64_REL24_NOTOC:
7777 if (size == 32)
7778 break;
7779 // Fall through.
e5d5f5ed
AM
7780 case elfcpp::R_POWERPC_ADDR24:
7781 case elfcpp::R_POWERPC_ADDR14:
7782 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7783 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7784 case elfcpp::R_POWERPC_REL24:
7785 case elfcpp::R_PPC_PLTREL24:
7786 case elfcpp::R_POWERPC_REL14:
7787 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7788 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7789 return true;
7790
7791 default:
7792 break;
7793 }
7794
7795 // Anything else is a problem.
7796 // If we are building a static executable, the libc startup function
7797 // responsible for applying indirect function relocations is going
7798 // to complain about the reloc type.
7799 // If we are building a dynamic executable, we will have a text
7800 // relocation. The dynamic loader will set the text segment
7801 // writable and non-executable to apply text relocations. So we'll
7802 // segfault when trying to run the indirection function to resolve
7803 // the reloc.
b3ccdeb5
AM
7804 if (report_err)
7805 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
7806 object->name().c_str(), r_type);
7807 return false;
7808}
7809
5edad15d
AM
7810// Return TRUE iff INSN is one we expect on a _LO variety toc/got
7811// reloc.
7812
7813static bool
7814ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7815{
7816 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7817 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7818 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7819 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7820 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7821 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7822 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7823 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7824 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7825 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7826 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7827 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7828 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7829 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7830 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7831 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7832 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7833 /* Exclude lfqu by testing reloc. If relocs are ever
7834 defined for the reduced D field in psq_lu then those
7835 will need testing too. */
7836 && r_type != elfcpp::R_PPC64_TOC16_LO
7837 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7838 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7839 && (insn & 1) == 0)
7840 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7841 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7842 /* Exclude stfqu. psq_stu as above for psq_lu. */
7843 && r_type != elfcpp::R_PPC64_TOC16_LO
7844 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7845 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7846 && (insn & 1) == 0));
7847}
7848
42cacb20
DE
7849// Scan a relocation for a local symbol.
7850
7851template<int size, bool big_endian>
7852inline void
7853Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
7854 Symbol_table* symtab,
7855 Layout* layout,
7856 Target_powerpc<size, big_endian>* target,
7857 Sized_relobj_file<size, big_endian>* object,
7858 unsigned int data_shndx,
7859 Output_section* output_section,
7860 const elfcpp::Rela<size, big_endian>& reloc,
7861 unsigned int r_type,
e5d5f5ed 7862 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 7863 bool is_discarded)
42cacb20 7864{
34e0882b 7865 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
7866
7867 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7868 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7869 {
7870 this->expect_tls_get_addr_call();
7871 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7872 if (tls_type != tls::TLSOPT_NONE)
7873 this->skip_next_tls_get_addr_call();
7874 }
7875 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7876 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7877 {
7878 this->expect_tls_get_addr_call();
7879 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7880 if (tls_type != tls::TLSOPT_NONE)
7881 this->skip_next_tls_get_addr_call();
7882 }
7883
dd93cd0a
AM
7884 Powerpc_relobj<size, big_endian>* ppc_object
7885 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7886
bfdfa4cd
AM
7887 if (is_discarded)
7888 {
7889 if (size == 64
7890 && data_shndx == ppc_object->opd_shndx()
7891 && r_type == elfcpp::R_PPC64_ADDR64)
7892 ppc_object->set_opd_discard(reloc.get_r_offset());
7893 return;
7894 }
7895
e5d5f5ed
AM
7896 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7897 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 7898 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 7899 {
ec661b9d
AM
7900 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7901 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7902 r_type, r_sym, reloc.get_r_addend());
7903 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 7904 }
e5d5f5ed 7905
42cacb20
DE
7906 switch (r_type)
7907 {
7908 case elfcpp::R_POWERPC_NONE:
7909 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7910 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 7911 case elfcpp::R_POWERPC_TLS:
549dba71 7912 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
7913 case elfcpp::R_POWERPC_PLTSEQ:
7914 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7915 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7916 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7917 case elfcpp::R_PPC64_PCREL_OPT:
7918 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7919 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7920 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7921 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7922 case elfcpp::R_PPC64_REL16_HIGHER34:
7923 case elfcpp::R_PPC64_REL16_HIGHERA34:
7924 case elfcpp::R_PPC64_REL16_HIGHEST34:
7925 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
7926 case elfcpp::R_PPC64_D34:
7927 case elfcpp::R_PPC64_D34_LO:
7928 case elfcpp::R_PPC64_D34_HI30:
7929 case elfcpp::R_PPC64_D34_HA30:
7930 case elfcpp::R_PPC64_D28:
7931 case elfcpp::R_PPC64_PCREL34:
7932 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
7933 case elfcpp::R_PPC64_TPREL34:
7934 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
7935 break;
7936
7937 case elfcpp::R_PPC64_TOC:
7938 {
7939 Output_data_got_powerpc<size, big_endian>* got
7940 = target->got_section(symtab, layout);
7941 if (parameters->options().output_is_position_independent())
7942 {
bfdfa4cd
AM
7943 Address off = reloc.get_r_offset();
7944 if (size == 64
9055360d 7945 && target->abiversion() < 2
bfdfa4cd
AM
7946 && data_shndx == ppc_object->opd_shndx()
7947 && ppc_object->get_opd_discard(off - 8))
7948 break;
7949
dd93cd0a 7950 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 7951 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
7952 rela_dyn->add_output_section_relative(got->output_section(),
7953 elfcpp::R_POWERPC_RELATIVE,
7954 output_section,
bfdfa4cd
AM
7955 object, data_shndx, off,
7956 symobj->toc_base_offset());
dd93cd0a
AM
7957 }
7958 }
42cacb20
DE
7959 break;
7960
7961 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 7962 case elfcpp::R_PPC64_UADDR64:
42cacb20 7963 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
7964 case elfcpp::R_POWERPC_UADDR32:
7965 case elfcpp::R_POWERPC_ADDR24:
c9269dff 7966 case elfcpp::R_POWERPC_ADDR16:
42cacb20 7967 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
7968 case elfcpp::R_POWERPC_ADDR16_HI:
7969 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 7970 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
7971 case elfcpp::R_PPC64_ADDR16_HIGH:
7972 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
7973 case elfcpp::R_PPC64_ADDR16_HIGHER:
7974 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7975 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7976 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7977 case elfcpp::R_PPC64_ADDR16_DS:
7978 case elfcpp::R_PPC64_ADDR16_LO_DS:
7979 case elfcpp::R_POWERPC_ADDR14:
7980 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7981 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
7982 // If building a shared library (or a position-independent
7983 // executable), we need to create a dynamic relocation for
7984 // this location.
c9824451 7985 if (parameters->options().output_is_position_independent()
9055360d 7986 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 7987 {
b3ccdeb5
AM
7988 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7989 is_ifunc);
1f98a074 7990 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
7991 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7992 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 7993 {
b3ccdeb5
AM
7994 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7995 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7996 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
7997 output_section, data_shndx,
7998 reloc.get_r_offset(),
c9824451 7999 reloc.get_r_addend(), false);
2e702c99 8000 }
1f98a074 8001 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 8002 {
dd93cd0a 8003 check_non_pic(object, r_type);
dd93cd0a
AM
8004 rela_dyn->add_local(object, r_sym, r_type, output_section,
8005 data_shndx, reloc.get_r_offset(),
8006 reloc.get_r_addend());
2e702c99 8007 }
1f98a074
AM
8008 else
8009 {
8010 gold_assert(lsym.get_st_value() == 0);
8011 unsigned int shndx = lsym.get_st_shndx();
8012 bool is_ordinary;
8013 shndx = object->adjust_sym_shndx(r_sym, shndx,
8014 &is_ordinary);
8015 if (!is_ordinary)
8016 object->error(_("section symbol %u has bad shndx %u"),
8017 r_sym, shndx);
8018 else
8019 rela_dyn->add_local_section(object, shndx, r_type,
8020 output_section, data_shndx,
8021 reloc.get_r_offset());
8022 }
2e702c99 8023 }
42cacb20
DE
8024 break;
8025
e4dff765
AM
8026 case elfcpp::R_PPC64_PLT_PCREL34:
8027 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
8028 case elfcpp::R_POWERPC_PLT16_LO:
8029 case elfcpp::R_POWERPC_PLT16_HI:
8030 case elfcpp::R_POWERPC_PLT16_HA:
8031 case elfcpp::R_PPC64_PLT16_LO_DS:
8032 if (!is_ifunc)
8033 {
8034 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8035 target->make_local_plt_entry(layout, object, r_sym);
8036 }
8037 break;
8038
32f59844
AM
8039 case elfcpp::R_PPC64_REL24_NOTOC:
8040 if (size == 32)
8041 break;
8042 // Fall through.
42cacb20 8043 case elfcpp::R_POWERPC_REL24:
c9824451 8044 case elfcpp::R_PPC_PLTREL24:
42cacb20 8045 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
8046 case elfcpp::R_POWERPC_REL14:
8047 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8048 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8049 if (!is_ifunc)
0e123f69
AM
8050 {
8051 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8052 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8053 r_type, r_sym, reloc.get_r_addend());
8054 }
ec661b9d
AM
8055 break;
8056
7e57d19e
AM
8057 case elfcpp::R_PPC64_TOCSAVE:
8058 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8059 // caller has already saved r2 and thus a plt call stub need not
8060 // save r2.
8061 if (size == 64
8062 && target->mark_pltcall(ppc_object, data_shndx,
8063 reloc.get_r_offset() - 4, symtab))
8064 {
8065 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8066 unsigned int shndx = lsym.get_st_shndx();
8067 bool is_ordinary;
8068 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8069 if (!is_ordinary)
8070 object->error(_("tocsave symbol %u has bad shndx %u"),
8071 r_sym, shndx);
8072 else
8073 target->add_tocsave(ppc_object, shndx,
8074 lsym.get_st_value() + reloc.get_r_addend());
8075 }
8076 break;
8077
ec661b9d
AM
8078 case elfcpp::R_PPC64_REL64:
8079 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8080 case elfcpp::R_POWERPC_REL16:
6ce78956 8081 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 8082 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 8083 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8084 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8085 case elfcpp::R_PPC64_REL16_HIGH:
8086 case elfcpp::R_PPC64_REL16_HIGHA:
8087 case elfcpp::R_PPC64_REL16_HIGHER:
8088 case elfcpp::R_PPC64_REL16_HIGHERA:
8089 case elfcpp::R_PPC64_REL16_HIGHEST:
8090 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8091 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8092 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8093 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8094 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8095 case elfcpp::R_PPC64_SECTOFF_DS:
8096 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8097 case elfcpp::R_POWERPC_TPREL16:
8098 case elfcpp::R_POWERPC_TPREL16_LO:
8099 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8100 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8101 case elfcpp::R_PPC64_TPREL16_DS:
8102 case elfcpp::R_PPC64_TPREL16_LO_DS:
8103 case elfcpp::R_PPC64_TPREL16_HIGH:
8104 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8105 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8106 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8107 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8108 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8109 case elfcpp::R_POWERPC_DTPREL16:
8110 case elfcpp::R_POWERPC_DTPREL16_LO:
8111 case elfcpp::R_POWERPC_DTPREL16_HI:
8112 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8113 case elfcpp::R_PPC64_DTPREL16_DS:
8114 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8115 case elfcpp::R_PPC64_DTPREL16_HIGH:
8116 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8117 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8118 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8119 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8120 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8121 case elfcpp::R_PPC64_TLSGD:
8122 case elfcpp::R_PPC64_TLSLD:
45965137 8123 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
8124 break;
8125
e4dff765 8126 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8127 case elfcpp::R_POWERPC_GOT16:
8128 case elfcpp::R_POWERPC_GOT16_LO:
8129 case elfcpp::R_POWERPC_GOT16_HI:
8130 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8131 case elfcpp::R_PPC64_GOT16_DS:
8132 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8133 {
c9269dff 8134 // The symbol requires a GOT entry.
dd93cd0a
AM
8135 Output_data_got_powerpc<size, big_endian>* got
8136 = target->got_section(symtab, layout);
8137 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 8138
e5d5f5ed 8139 if (!parameters->options().output_is_position_independent())
42cacb20 8140 {
b01a4b04
AM
8141 if (is_ifunc
8142 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8143 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
8144 else
8145 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
8146 }
8147 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
8148 {
8149 // If we are generating a shared object or a pie, this
8150 // symbol's GOT entry will be set by a dynamic relocation.
8151 unsigned int off;
8152 off = got->add_constant(0);
8153 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 8154
b3ccdeb5
AM
8155 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8156 is_ifunc);
8157 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8158 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8159 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 8160 got, off, 0, false);
2e702c99 8161 }
42cacb20
DE
8162 }
8163 break;
8164
cf43a2fe
AM
8165 case elfcpp::R_PPC64_TOC16:
8166 case elfcpp::R_PPC64_TOC16_LO:
8167 case elfcpp::R_PPC64_TOC16_HI:
8168 case elfcpp::R_PPC64_TOC16_HA:
8169 case elfcpp::R_PPC64_TOC16_DS:
8170 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8171 // We need a GOT section.
8172 target->got_section(symtab, layout);
8173 break;
8174
87c69f97 8175 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8176 case elfcpp::R_POWERPC_GOT_TLSGD16:
8177 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8178 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8179 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8180 {
8181 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8182 if (tls_type == tls::TLSOPT_NONE)
8183 {
8184 Output_data_got_powerpc<size, big_endian>* got
8185 = target->got_section(symtab, layout);
8186 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
8187 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8188 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
8189 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
8190 }
8191 else if (tls_type == tls::TLSOPT_TO_LE)
8192 {
8193 // no GOT relocs needed for Local Exec.
8194 }
8195 else
8196 gold_unreachable();
8197 }
42cacb20
DE
8198 break;
8199
87c69f97 8200 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8201 case elfcpp::R_POWERPC_GOT_TLSLD16:
8202 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8203 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8204 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8205 {
8206 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8207 if (tls_type == tls::TLSOPT_NONE)
8208 target->tlsld_got_offset(symtab, layout, object);
8209 else if (tls_type == tls::TLSOPT_TO_LE)
8210 {
8211 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8212 if (parameters->options().emit_relocs())
8213 {
8214 Output_section* os = layout->tls_segment()->first_section();
8215 gold_assert(os != NULL);
8216 os->set_needs_symtab_index();
8217 }
dd93cd0a
AM
8218 }
8219 else
8220 gold_unreachable();
8221 }
42cacb20 8222 break;
42cacb20 8223
87c69f97 8224 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
8225 case elfcpp::R_POWERPC_GOT_DTPREL16:
8226 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8227 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8228 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8229 {
8230 Output_data_got_powerpc<size, big_endian>* got
8231 = target->got_section(symtab, layout);
8232 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 8233 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
8234 }
8235 break;
42cacb20 8236
87c69f97 8237 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
8238 case elfcpp::R_POWERPC_GOT_TPREL16:
8239 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8240 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8241 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8242 {
8243 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8244 if (tls_type == tls::TLSOPT_NONE)
8245 {
dd93cd0a 8246 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
8247 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
8248 {
8249 Output_data_got_powerpc<size, big_endian>* got
8250 = target->got_section(symtab, layout);
8251 unsigned int off = got->add_constant(0);
8252 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
8253
8254 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8255 rela_dyn->add_symbolless_local_addend(object, r_sym,
8256 elfcpp::R_POWERPC_TPREL,
8257 got, off, 0);
8258 }
dd93cd0a
AM
8259 }
8260 else if (tls_type == tls::TLSOPT_TO_LE)
8261 {
8262 // no GOT relocs needed for Local Exec.
8263 }
8264 else
8265 gold_unreachable();
8266 }
8267 break;
8268
8269 default:
8270 unsupported_reloc_local(object, r_type);
8271 break;
8272 }
d8f5a274 8273
5edad15d
AM
8274 if (size == 64
8275 && parameters->options().toc_optimize())
8276 {
8277 if (data_shndx == ppc_object->toc_shndx())
8278 {
8279 bool ok = true;
8280 if (r_type != elfcpp::R_PPC64_ADDR64
8281 || (is_ifunc && target->abiversion() < 2))
8282 ok = false;
8283 else if (parameters->options().output_is_position_independent())
8284 {
8285 if (is_ifunc)
8286 ok = false;
8287 else
8288 {
8289 unsigned int shndx = lsym.get_st_shndx();
8290 if (shndx >= elfcpp::SHN_LORESERVE
8291 && shndx != elfcpp::SHN_XINDEX)
8292 ok = false;
8293 }
8294 }
8295 if (!ok)
8296 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8297 }
8298
8299 enum {no_check, check_lo, check_ha} insn_check;
8300 switch (r_type)
8301 {
8302 default:
8303 insn_check = no_check;
8304 break;
8305
8306 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8307 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8308 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8309 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8310 case elfcpp::R_POWERPC_GOT16_HA:
8311 case elfcpp::R_PPC64_TOC16_HA:
8312 insn_check = check_ha;
8313 break;
8314
8315 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8316 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8317 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8318 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8319 case elfcpp::R_POWERPC_GOT16_LO:
8320 case elfcpp::R_PPC64_GOT16_LO_DS:
8321 case elfcpp::R_PPC64_TOC16_LO:
8322 case elfcpp::R_PPC64_TOC16_LO_DS:
8323 insn_check = check_lo;
8324 break;
8325 }
8326
8327 section_size_type slen;
8328 const unsigned char* view = NULL;
8329 if (insn_check != no_check)
8330 {
8331 view = ppc_object->section_contents(data_shndx, &slen, false);
8332 section_size_type off =
8333 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8334 if (off < slen)
8335 {
8336 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8337 if (insn_check == check_lo
8338 ? !ok_lo_toc_insn(insn, r_type)
8339 : ((insn & ((0x3f << 26) | 0x1f << 16))
8340 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8341 {
8342 ppc_object->set_no_toc_opt();
8343 gold_warning(_("%s: toc optimization is not supported "
8344 "for %#08x instruction"),
8345 ppc_object->name().c_str(), insn);
8346 }
8347 }
8348 }
8349
8350 switch (r_type)
8351 {
8352 default:
8353 break;
8354 case elfcpp::R_PPC64_TOC16:
8355 case elfcpp::R_PPC64_TOC16_LO:
8356 case elfcpp::R_PPC64_TOC16_HI:
8357 case elfcpp::R_PPC64_TOC16_HA:
8358 case elfcpp::R_PPC64_TOC16_DS:
8359 case elfcpp::R_PPC64_TOC16_LO_DS:
8360 unsigned int shndx = lsym.get_st_shndx();
8361 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8362 bool is_ordinary;
8363 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8364 if (is_ordinary && shndx == ppc_object->toc_shndx())
8365 {
412294da 8366 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8367 if (dst_off < ppc_object->section_size(shndx))
8368 {
8369 bool ok = false;
8370 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8371 ok = true;
8372 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8373 {
8374 // Need to check that the insn is a ld
8375 if (!view)
8376 view = ppc_object->section_contents(data_shndx,
8377 &slen,
8378 false);
8379 section_size_type off =
8380 (convert_to_section_size_type(reloc.get_r_offset())
8381 + (big_endian ? -2 : 3));
8382 if (off < slen
8383 && (view[off] & (0x3f << 2)) == 58u << 2)
8384 ok = true;
8385 }
8386 if (!ok)
8387 ppc_object->set_no_toc_opt(dst_off);
8388 }
8389 }
8390 break;
8391 }
8392 }
8393
f159cdb6
AM
8394 if (size == 32)
8395 {
8396 switch (r_type)
8397 {
8398 case elfcpp::R_POWERPC_REL32:
8399 if (ppc_object->got2_shndx() != 0
8400 && parameters->options().output_is_position_independent())
8401 {
8402 unsigned int shndx = lsym.get_st_shndx();
8403 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8404 bool is_ordinary;
8405 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8406 if (is_ordinary && shndx == ppc_object->got2_shndx()
8407 && (ppc_object->section_flags(data_shndx)
8408 & elfcpp::SHF_EXECINSTR) != 0)
8409 gold_error(_("%s: unsupported -mbss-plt code"),
8410 ppc_object->name().c_str());
8411 }
8412 break;
8413 default:
8414 break;
8415 }
8416 }
8417
d8f5a274
AM
8418 switch (r_type)
8419 {
8420 case elfcpp::R_POWERPC_GOT_TLSLD16:
8421 case elfcpp::R_POWERPC_GOT_TLSGD16:
8422 case elfcpp::R_POWERPC_GOT_TPREL16:
8423 case elfcpp::R_POWERPC_GOT_DTPREL16:
8424 case elfcpp::R_POWERPC_GOT16:
8425 case elfcpp::R_PPC64_GOT16_DS:
8426 case elfcpp::R_PPC64_TOC16:
8427 case elfcpp::R_PPC64_TOC16_DS:
8428 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8429 break;
8430 default:
8431 break;
8432 }
8433
8434 switch (r_type)
8435 {
89c52ae3
AM
8436 case elfcpp::R_PPC64_TPREL16_DS:
8437 case elfcpp::R_PPC64_TPREL16_LO_DS:
8438 case elfcpp::R_PPC64_TPREL16_HIGH:
8439 case elfcpp::R_PPC64_TPREL16_HIGHA:
8440 case elfcpp::R_PPC64_TPREL16_HIGHER:
8441 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8442 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8443 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8444 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
8445 if (size != 64)
8446 break;
8447 // Fall through.
8448 case elfcpp::R_POWERPC_TPREL16:
8449 case elfcpp::R_POWERPC_TPREL16_LO:
8450 case elfcpp::R_POWERPC_TPREL16_HI:
8451 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8452 layout->set_has_static_tls();
8453 break;
8454 default:
8455 break;
8456 }
8457
252dcdf4
AM
8458 switch (r_type)
8459 {
8460 case elfcpp::R_POWERPC_TPREL16_HA:
8461 if (target->tprel_opt())
8462 {
8463 section_size_type slen;
8464 const unsigned char* view = NULL;
8465 view = ppc_object->section_contents(data_shndx, &slen, false);
8466 section_size_type off
8467 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8468 if (off < slen)
8469 {
8470 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8471 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8472 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 8473 target->set_no_tprel_opt();
252dcdf4
AM
8474 }
8475 }
8476 break;
8477
8478 case elfcpp::R_PPC64_TPREL16_HIGH:
8479 case elfcpp::R_PPC64_TPREL16_HIGHA:
8480 case elfcpp::R_PPC64_TPREL16_HIGHER:
8481 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8482 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8483 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8484 if (size != 64)
8485 break;
8486 // Fall through.
8487 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 8488 target->set_no_tprel_opt();
252dcdf4
AM
8489 break;
8490 default:
8491 break;
8492 }
8493
89c52ae3
AM
8494 switch (r_type)
8495 {
8496 case elfcpp::R_PPC64_D34:
8497 case elfcpp::R_PPC64_D34_LO:
8498 case elfcpp::R_PPC64_D34_HI30:
8499 case elfcpp::R_PPC64_D34_HA30:
8500 case elfcpp::R_PPC64_D28:
8501 case elfcpp::R_PPC64_PCREL34:
8502 case elfcpp::R_PPC64_PCREL28:
8503 case elfcpp::R_PPC64_TPREL34:
8504 case elfcpp::R_PPC64_DTPREL34:
8505 case elfcpp::R_PPC64_PLT_PCREL34:
8506 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8507 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
8508 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8509 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8510 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8511 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 8512 target->set_power10_relocs();
89c52ae3 8513 break;
d8f5a274
AM
8514 default:
8515 break;
8516 }
dd93cd0a
AM
8517}
8518
8519// Report an unsupported relocation against a global symbol.
8520
8521template<int size, bool big_endian>
8522void
8523Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8524 Sized_relobj_file<size, big_endian>* object,
8525 unsigned int r_type,
8526 Symbol* gsym)
8527{
42cacb20
DE
8528 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8529 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8530}
8531
8532// Scan a relocation for a global symbol.
8533
8534template<int size, bool big_endian>
8535inline void
8536Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8537 Symbol_table* symtab,
8538 Layout* layout,
8539 Target_powerpc<size, big_endian>* target,
8540 Sized_relobj_file<size, big_endian>* object,
8541 unsigned int data_shndx,
8542 Output_section* output_section,
8543 const elfcpp::Rela<size, big_endian>& reloc,
8544 unsigned int r_type,
8545 Symbol* gsym)
42cacb20 8546{
34e0882b
AM
8547 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8548 == Track_tls::SKIP)
e3deeb9c
AM
8549 return;
8550
34e0882b
AM
8551 if (target->replace_tls_get_addr(gsym))
8552 // Change a __tls_get_addr reference to __tls_get_addr_opt
8553 // so dynamic relocs are emitted against the latter symbol.
8554 gsym = target->tls_get_addr_opt();
8555
e3deeb9c
AM
8556 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8557 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8558 {
8559 this->expect_tls_get_addr_call();
8560 const bool final = gsym->final_value_is_known();
8561 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8562 if (tls_type != tls::TLSOPT_NONE)
8563 this->skip_next_tls_get_addr_call();
8564 }
8565 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8566 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8567 {
8568 this->expect_tls_get_addr_call();
8569 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8570 if (tls_type != tls::TLSOPT_NONE)
8571 this->skip_next_tls_get_addr_call();
8572 }
8573
dd93cd0a
AM
8574 Powerpc_relobj<size, big_endian>* ppc_object
8575 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8576
e5d5f5ed 8577 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8578 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8579 bool pushed_ifunc = false;
8580 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8581 {
0e123f69 8582 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8583 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8584 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8585 target->make_plt_entry(symtab, layout, gsym);
9055360d 8586 pushed_ifunc = true;
ec661b9d 8587 }
e5d5f5ed 8588
42cacb20
DE
8589 switch (r_type)
8590 {
8591 case elfcpp::R_POWERPC_NONE:
8592 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8593 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8594 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8595 case elfcpp::R_POWERPC_TLS:
549dba71 8596 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8597 case elfcpp::R_POWERPC_PLTSEQ:
8598 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8599 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8600 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8601 case elfcpp::R_PPC64_PCREL_OPT:
8602 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8603 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8604 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8605 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8606 case elfcpp::R_PPC64_REL16_HIGHER34:
8607 case elfcpp::R_PPC64_REL16_HIGHERA34:
8608 case elfcpp::R_PPC64_REL16_HIGHEST34:
8609 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8610 case elfcpp::R_PPC64_D34:
8611 case elfcpp::R_PPC64_D34_LO:
8612 case elfcpp::R_PPC64_D34_HI30:
8613 case elfcpp::R_PPC64_D34_HA30:
8614 case elfcpp::R_PPC64_D28:
8615 case elfcpp::R_PPC64_PCREL34:
8616 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8617 case elfcpp::R_PPC64_TPREL34:
8618 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8619 break;
8620
8621 case elfcpp::R_PPC64_TOC:
8622 {
8623 Output_data_got_powerpc<size, big_endian>* got
8624 = target->got_section(symtab, layout);
8625 if (parameters->options().output_is_position_independent())
8626 {
bfdfa4cd
AM
8627 Address off = reloc.get_r_offset();
8628 if (size == 64
8629 && data_shndx == ppc_object->opd_shndx()
8630 && ppc_object->get_opd_discard(off - 8))
8631 break;
8632
dd93cd0a
AM
8633 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8634 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8635 if (data_shndx != ppc_object->opd_shndx())
8636 symobj = static_cast
8637 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8638 rela_dyn->add_output_section_relative(got->output_section(),
8639 elfcpp::R_POWERPC_RELATIVE,
8640 output_section,
bfdfa4cd 8641 object, data_shndx, off,
dd93cd0a
AM
8642 symobj->toc_base_offset());
8643 }
8644 }
42cacb20
DE
8645 break;
8646
c9269dff 8647 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8648 if (size == 64
9055360d 8649 && target->abiversion() < 2
bfdfa4cd
AM
8650 && data_shndx == ppc_object->opd_shndx()
8651 && (gsym->is_defined_in_discarded_section()
8652 || gsym->object() != object))
8653 {
8654 ppc_object->set_opd_discard(reloc.get_r_offset());
8655 break;
8656 }
d8e90251 8657 // Fall through.
dd93cd0a 8658 case elfcpp::R_PPC64_UADDR64:
c9269dff 8659 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8660 case elfcpp::R_POWERPC_UADDR32:
8661 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8662 case elfcpp::R_POWERPC_ADDR16:
8663 case elfcpp::R_POWERPC_ADDR16_LO:
8664 case elfcpp::R_POWERPC_ADDR16_HI:
8665 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8666 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8667 case elfcpp::R_PPC64_ADDR16_HIGH:
8668 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8669 case elfcpp::R_PPC64_ADDR16_HIGHER:
8670 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8671 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8672 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8673 case elfcpp::R_PPC64_ADDR16_DS:
8674 case elfcpp::R_PPC64_ADDR16_LO_DS:
8675 case elfcpp::R_POWERPC_ADDR14:
8676 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8677 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8678 {
c9269dff
AM
8679 // Make a PLT entry if necessary.
8680 if (gsym->needs_plt_entry())
8681 {
9055360d
AM
8682 // Since this is not a PC-relative relocation, we may be
8683 // taking the address of a function. In that case we need to
8684 // set the entry in the dynamic symbol table to the address of
8685 // the PLT call stub.
8686 bool need_ifunc_plt = false;
8687 if ((size == 32 || target->abiversion() >= 2)
8688 && gsym->is_from_dynobj()
8689 && !parameters->options().output_is_position_independent())
8690 {
8691 gsym->set_needs_dynsym_value();
8692 need_ifunc_plt = true;
8693 }
8694 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8695 {
0e123f69 8696 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8697 target->push_branch(ppc_object, data_shndx,
0e123f69 8698 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8699 reloc.get_r_addend());
8700 target->make_plt_entry(symtab, layout, gsym);
8701 }
c9269dff
AM
8702 }
8703 // Make a dynamic relocation if necessary.
88b8e639 8704 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8705 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8706 {
a82bef93
ST
8707 if (!parameters->options().output_is_position_independent()
8708 && gsym->may_need_copy_reloc())
c9269dff
AM
8709 {
8710 target->copy_reloc(symtab, layout, object,
8711 data_shndx, output_section, gsym, reloc);
8712 }
9055360d
AM
8713 else if ((((size == 32
8714 && r_type == elfcpp::R_POWERPC_ADDR32)
8715 || (size == 64
8716 && r_type == elfcpp::R_PPC64_ADDR64
8717 && target->abiversion() >= 2))
627b30b7
AM
8718 && gsym->can_use_relative_reloc(false)
8719 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8720 && parameters->options().shared()))
8721 || (size == 64
8722 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8723 && target->abiversion() < 2
627b30b7
AM
8724 && (gsym->can_use_relative_reloc(false)
8725 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8726 {
b3ccdeb5
AM
8727 Reloc_section* rela_dyn
8728 = target->rela_dyn_section(symtab, layout, is_ifunc);
8729 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8730 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8731 rela_dyn->add_symbolless_global_addend(
8732 gsym, dynrel, output_section, object, data_shndx,
8733 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
8734 }
8735 else
8736 {
b3ccdeb5
AM
8737 Reloc_section* rela_dyn
8738 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 8739 check_non_pic(object, r_type);
dd93cd0a
AM
8740 rela_dyn->add_global(gsym, r_type, output_section,
8741 object, data_shndx,
8742 reloc.get_r_offset(),
8743 reloc.get_r_addend());
5edad15d
AM
8744
8745 if (size == 64
8746 && parameters->options().toc_optimize()
8747 && data_shndx == ppc_object->toc_shndx())
8748 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
8749 }
8750 }
42cacb20
DE
8751 }
8752 break;
8753
e4dff765
AM
8754 case elfcpp::R_PPC64_PLT_PCREL34:
8755 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8756 case elfcpp::R_POWERPC_PLT16_LO:
8757 case elfcpp::R_POWERPC_PLT16_HI:
8758 case elfcpp::R_POWERPC_PLT16_HA:
8759 case elfcpp::R_PPC64_PLT16_LO_DS:
8760 if (!pushed_ifunc)
8761 target->make_plt_entry(symtab, layout, gsym);
8762 break;
8763
32f59844
AM
8764 case elfcpp::R_PPC64_REL24_NOTOC:
8765 if (size == 32)
8766 break;
8767 // Fall through.
cf43a2fe 8768 case elfcpp::R_PPC_PLTREL24:
42cacb20 8769 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
8770 if (!is_ifunc)
8771 {
0e123f69 8772 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8773 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8774 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
8775 if (gsym->needs_plt_entry()
8776 || (!gsym->final_value_is_known()
8777 && (gsym->is_undefined()
8778 || gsym->is_from_dynobj()
8779 || gsym->is_preemptible())))
8780 target->make_plt_entry(symtab, layout, gsym);
8781 }
d8e90251 8782 // Fall through.
42cacb20 8783
3ea0a085 8784 case elfcpp::R_PPC64_REL64:
dd93cd0a 8785 case elfcpp::R_POWERPC_REL32:
3ea0a085 8786 // Make a dynamic relocation if necessary.
88b8e639 8787 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 8788 {
a82bef93
ST
8789 if (!parameters->options().output_is_position_independent()
8790 && gsym->may_need_copy_reloc())
3ea0a085
AM
8791 {
8792 target->copy_reloc(symtab, layout, object,
8793 data_shndx, output_section, gsym,
8794 reloc);
8795 }
8796 else
8797 {
b3ccdeb5
AM
8798 Reloc_section* rela_dyn
8799 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
8800 check_non_pic(object, r_type);
8801 rela_dyn->add_global(gsym, r_type, output_section, object,
8802 data_shndx, reloc.get_r_offset(),
8803 reloc.get_r_addend());
8804 }
8805 }
8806 break;
8807
ec661b9d
AM
8808 case elfcpp::R_POWERPC_REL14:
8809 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8810 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8811 if (!is_ifunc)
0e123f69
AM
8812 {
8813 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8814 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8815 r_type, r_sym, reloc.get_r_addend());
8816 }
ec661b9d
AM
8817 break;
8818
7e57d19e
AM
8819 case elfcpp::R_PPC64_TOCSAVE:
8820 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8821 // caller has already saved r2 and thus a plt call stub need not
8822 // save r2.
8823 if (size == 64
8824 && target->mark_pltcall(ppc_object, data_shndx,
8825 reloc.get_r_offset() - 4, symtab))
8826 {
8827 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8828 bool is_ordinary;
8829 unsigned int shndx = gsym->shndx(&is_ordinary);
8830 if (!is_ordinary)
8831 object->error(_("tocsave symbol %u has bad shndx %u"),
8832 r_sym, shndx);
8833 else
8834 {
8835 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8836 target->add_tocsave(ppc_object, shndx,
8837 sym->value() + reloc.get_r_addend());
8838 }
8839 }
8840 break;
8841
6ce78956
AM
8842 case elfcpp::R_POWERPC_REL16:
8843 case elfcpp::R_POWERPC_REL16_LO:
8844 case elfcpp::R_POWERPC_REL16_HI:
8845 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8846 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8847 case elfcpp::R_PPC64_REL16_HIGH:
8848 case elfcpp::R_PPC64_REL16_HIGHA:
8849 case elfcpp::R_PPC64_REL16_HIGHER:
8850 case elfcpp::R_PPC64_REL16_HIGHERA:
8851 case elfcpp::R_PPC64_REL16_HIGHEST:
8852 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8853 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8854 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8855 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8856 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8857 case elfcpp::R_PPC64_SECTOFF_DS:
8858 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8859 case elfcpp::R_POWERPC_TPREL16:
8860 case elfcpp::R_POWERPC_TPREL16_LO:
8861 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8862 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8863 case elfcpp::R_PPC64_TPREL16_DS:
8864 case elfcpp::R_PPC64_TPREL16_LO_DS:
8865 case elfcpp::R_PPC64_TPREL16_HIGH:
8866 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8867 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8868 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8869 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8870 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8871 case elfcpp::R_POWERPC_DTPREL16:
8872 case elfcpp::R_POWERPC_DTPREL16_LO:
8873 case elfcpp::R_POWERPC_DTPREL16_HI:
8874 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8875 case elfcpp::R_PPC64_DTPREL16_DS:
8876 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8877 case elfcpp::R_PPC64_DTPREL16_HIGH:
8878 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8879 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8880 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8881 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8882 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8883 case elfcpp::R_PPC64_TLSGD:
8884 case elfcpp::R_PPC64_TLSLD:
45965137 8885 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
8886 break;
8887
e4dff765 8888 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8889 case elfcpp::R_POWERPC_GOT16:
8890 case elfcpp::R_POWERPC_GOT16_LO:
8891 case elfcpp::R_POWERPC_GOT16_HI:
8892 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8893 case elfcpp::R_PPC64_GOT16_DS:
8894 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8895 {
c9269dff
AM
8896 // The symbol requires a GOT entry.
8897 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
8898
8899 got = target->got_section(symtab, layout);
2e702c99 8900 if (gsym->final_value_is_known())
2e702c99 8901 {
b01a4b04
AM
8902 if (is_ifunc
8903 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8904 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8905 else
8906 got->add_global(gsym, GOT_TYPE_STANDARD);
8907 }
8908 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8909 {
8910 // If we are generating a shared object or a pie, this
8911 // symbol's GOT entry will be set by a dynamic relocation.
8912 unsigned int off = got->add_constant(0);
8913 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8914
b3ccdeb5
AM
8915 Reloc_section* rela_dyn
8916 = target->rela_dyn_section(symtab, layout, is_ifunc);
8917
e5d5f5ed 8918 if (gsym->can_use_relative_reloc(false)
9055360d
AM
8919 && !((size == 32
8920 || target->abiversion() >= 2)
e5d5f5ed
AM
8921 && gsym->visibility() == elfcpp::STV_PROTECTED
8922 && parameters->options().shared()))
2e702c99 8923 {
b3ccdeb5
AM
8924 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8925 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8926 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8927 }
8928 else
8929 {
8930 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8931 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 8932 }
2e702c99 8933 }
42cacb20
DE
8934 }
8935 break;
8936
cf43a2fe
AM
8937 case elfcpp::R_PPC64_TOC16:
8938 case elfcpp::R_PPC64_TOC16_LO:
8939 case elfcpp::R_PPC64_TOC16_HI:
8940 case elfcpp::R_PPC64_TOC16_HA:
8941 case elfcpp::R_PPC64_TOC16_DS:
8942 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8943 // We need a GOT section.
8944 target->got_section(symtab, layout);
8945 break;
8946
87c69f97 8947 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8948 case elfcpp::R_POWERPC_GOT_TLSGD16:
8949 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8950 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8951 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8952 {
8953 const bool final = gsym->final_value_is_known();
8954 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8955 if (tls_type == tls::TLSOPT_NONE)
8956 {
8957 Output_data_got_powerpc<size, big_endian>* got
8958 = target->got_section(symtab, layout);
b3ccdeb5
AM
8959 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8960 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
8961 elfcpp::R_POWERPC_DTPMOD,
8962 elfcpp::R_POWERPC_DTPREL);
8963 }
8964 else if (tls_type == tls::TLSOPT_TO_IE)
8965 {
acc276d8
AM
8966 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8967 {
8968 Output_data_got_powerpc<size, big_endian>* got
8969 = target->got_section(symtab, layout);
8970 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8971 if (gsym->is_undefined()
8972 || gsym->is_from_dynobj())
8973 {
8974 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8975 elfcpp::R_POWERPC_TPREL);
8976 }
8977 else
8978 {
8979 unsigned int off = got->add_constant(0);
8980 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8981 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8982 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8983 got, off, 0);
8984 }
8985 }
dd93cd0a
AM
8986 }
8987 else if (tls_type == tls::TLSOPT_TO_LE)
8988 {
8989 // no GOT relocs needed for Local Exec.
8990 }
8991 else
8992 gold_unreachable();
8993 }
42cacb20
DE
8994 break;
8995
87c69f97 8996 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8997 case elfcpp::R_POWERPC_GOT_TLSLD16:
8998 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8999 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9000 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9001 {
9002 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
9003 if (tls_type == tls::TLSOPT_NONE)
9004 target->tlsld_got_offset(symtab, layout, object);
9005 else if (tls_type == tls::TLSOPT_TO_LE)
9006 {
9007 // no GOT relocs needed for Local Exec.
7404fe1b
AM
9008 if (parameters->options().emit_relocs())
9009 {
9010 Output_section* os = layout->tls_segment()->first_section();
9011 gold_assert(os != NULL);
9012 os->set_needs_symtab_index();
9013 }
dd93cd0a
AM
9014 }
9015 else
9016 gold_unreachable();
9017 }
9018 break;
9019
87c69f97 9020 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
9021 case elfcpp::R_POWERPC_GOT_DTPREL16:
9022 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9023 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9024 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9025 {
9026 Output_data_got_powerpc<size, big_endian>* got
9027 = target->got_section(symtab, layout);
bd73a62d
AM
9028 if (!gsym->final_value_is_known()
9029 && (gsym->is_from_dynobj()
9030 || gsym->is_undefined()
9031 || gsym->is_preemptible()))
9032 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
9033 target->rela_dyn_section(layout),
9034 elfcpp::R_POWERPC_DTPREL);
9035 else
9036 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
9037 }
9038 break;
9039
87c69f97 9040 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
9041 case elfcpp::R_POWERPC_GOT_TPREL16:
9042 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9043 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9044 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9045 {
9046 const bool final = gsym->final_value_is_known();
9047 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9048 if (tls_type == tls::TLSOPT_NONE)
9049 {
acc276d8
AM
9050 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
9051 {
9052 Output_data_got_powerpc<size, big_endian>* got
9053 = target->got_section(symtab, layout);
9054 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9055 if (gsym->is_undefined()
9056 || gsym->is_from_dynobj())
9057 {
9058 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
9059 elfcpp::R_POWERPC_TPREL);
9060 }
9061 else
9062 {
9063 unsigned int off = got->add_constant(0);
9064 gsym->set_got_offset(GOT_TYPE_TPREL, off);
9065 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9066 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9067 got, off, 0);
9068 }
9069 }
dd93cd0a
AM
9070 }
9071 else if (tls_type == tls::TLSOPT_TO_LE)
9072 {
9073 // no GOT relocs needed for Local Exec.
9074 }
9075 else
9076 gold_unreachable();
9077 }
42cacb20
DE
9078 break;
9079
9080 default:
9081 unsupported_reloc_global(object, r_type, gsym);
9082 break;
9083 }
d8f5a274 9084
5edad15d
AM
9085 if (size == 64
9086 && parameters->options().toc_optimize())
9087 {
9088 if (data_shndx == ppc_object->toc_shndx())
9089 {
9090 bool ok = true;
9091 if (r_type != elfcpp::R_PPC64_ADDR64
9092 || (is_ifunc && target->abiversion() < 2))
9093 ok = false;
9094 else if (parameters->options().output_is_position_independent()
9095 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9096 ok = false;
9097 if (!ok)
9098 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9099 }
9100
9101 enum {no_check, check_lo, check_ha} insn_check;
9102 switch (r_type)
9103 {
9104 default:
9105 insn_check = no_check;
9106 break;
9107
9108 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9109 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9110 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9111 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9112 case elfcpp::R_POWERPC_GOT16_HA:
9113 case elfcpp::R_PPC64_TOC16_HA:
9114 insn_check = check_ha;
9115 break;
9116
9117 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9118 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9119 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9120 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9121 case elfcpp::R_POWERPC_GOT16_LO:
9122 case elfcpp::R_PPC64_GOT16_LO_DS:
9123 case elfcpp::R_PPC64_TOC16_LO:
9124 case elfcpp::R_PPC64_TOC16_LO_DS:
9125 insn_check = check_lo;
9126 break;
9127 }
9128
9129 section_size_type slen;
9130 const unsigned char* view = NULL;
9131 if (insn_check != no_check)
9132 {
9133 view = ppc_object->section_contents(data_shndx, &slen, false);
9134 section_size_type off =
9135 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9136 if (off < slen)
9137 {
9138 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9139 if (insn_check == check_lo
9140 ? !ok_lo_toc_insn(insn, r_type)
9141 : ((insn & ((0x3f << 26) | 0x1f << 16))
9142 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9143 {
9144 ppc_object->set_no_toc_opt();
9145 gold_warning(_("%s: toc optimization is not supported "
9146 "for %#08x instruction"),
9147 ppc_object->name().c_str(), insn);
9148 }
9149 }
9150 }
9151
9152 switch (r_type)
9153 {
9154 default:
9155 break;
9156 case elfcpp::R_PPC64_TOC16:
9157 case elfcpp::R_PPC64_TOC16_LO:
9158 case elfcpp::R_PPC64_TOC16_HI:
9159 case elfcpp::R_PPC64_TOC16_HA:
9160 case elfcpp::R_PPC64_TOC16_DS:
9161 case elfcpp::R_PPC64_TOC16_LO_DS:
9162 if (gsym->source() == Symbol::FROM_OBJECT
9163 && !gsym->object()->is_dynamic())
9164 {
9165 Powerpc_relobj<size, big_endian>* sym_object
9166 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9167 bool is_ordinary;
9168 unsigned int shndx = gsym->shndx(&is_ordinary);
9169 if (shndx == sym_object->toc_shndx())
9170 {
9171 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 9172 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
9173 if (dst_off < sym_object->section_size(shndx))
9174 {
9175 bool ok = false;
9176 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9177 ok = true;
9178 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9179 {
9180 // Need to check that the insn is a ld
9181 if (!view)
9182 view = ppc_object->section_contents(data_shndx,
9183 &slen,
9184 false);
9185 section_size_type off =
9186 (convert_to_section_size_type(reloc.get_r_offset())
9187 + (big_endian ? -2 : 3));
9188 if (off < slen
9189 && (view[off] & (0x3f << 2)) == (58u << 2))
9190 ok = true;
9191 }
9192 if (!ok)
9193 sym_object->set_no_toc_opt(dst_off);
9194 }
9195 }
9196 }
9197 break;
9198 }
9199 }
9200
f159cdb6
AM
9201 if (size == 32)
9202 {
9203 switch (r_type)
9204 {
9205 case elfcpp::R_PPC_LOCAL24PC:
9206 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9207 gold_error(_("%s: unsupported -mbss-plt code"),
9208 ppc_object->name().c_str());
9209 break;
9210 default:
9211 break;
9212 }
9213 }
9214
d8f5a274
AM
9215 switch (r_type)
9216 {
9217 case elfcpp::R_POWERPC_GOT_TLSLD16:
9218 case elfcpp::R_POWERPC_GOT_TLSGD16:
9219 case elfcpp::R_POWERPC_GOT_TPREL16:
9220 case elfcpp::R_POWERPC_GOT_DTPREL16:
9221 case elfcpp::R_POWERPC_GOT16:
9222 case elfcpp::R_PPC64_GOT16_DS:
9223 case elfcpp::R_PPC64_TOC16:
9224 case elfcpp::R_PPC64_TOC16_DS:
9225 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
9226 break;
9227 default:
9228 break;
9229 }
9230
9231 switch (r_type)
9232 {
89c52ae3
AM
9233 case elfcpp::R_PPC64_TPREL16_DS:
9234 case elfcpp::R_PPC64_TPREL16_LO_DS:
9235 case elfcpp::R_PPC64_TPREL16_HIGH:
9236 case elfcpp::R_PPC64_TPREL16_HIGHA:
9237 case elfcpp::R_PPC64_TPREL16_HIGHER:
9238 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9239 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9240 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9241 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
9242 if (size != 64)
9243 break;
9244 // Fall through.
9245 case elfcpp::R_POWERPC_TPREL16:
9246 case elfcpp::R_POWERPC_TPREL16_LO:
9247 case elfcpp::R_POWERPC_TPREL16_HI:
9248 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
9249 layout->set_has_static_tls();
9250 break;
9251 default:
9252 break;
9253 }
9254
252dcdf4
AM
9255 switch (r_type)
9256 {
9257 case elfcpp::R_POWERPC_TPREL16_HA:
9258 if (target->tprel_opt())
9259 {
9260 section_size_type slen;
9261 const unsigned char* view = NULL;
9262 view = ppc_object->section_contents(data_shndx, &slen, false);
9263 section_size_type off
9264 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9265 if (off < slen)
9266 {
9267 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9268 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9269 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 9270 target->set_no_tprel_opt();
252dcdf4
AM
9271 }
9272 }
9273 break;
9274
9275 case elfcpp::R_PPC64_TPREL16_HIGH:
9276 case elfcpp::R_PPC64_TPREL16_HIGHA:
9277 case elfcpp::R_PPC64_TPREL16_HIGHER:
9278 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9279 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9280 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9281 if (size != 64)
9282 break;
9283 // Fall through.
9284 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 9285 target->set_no_tprel_opt();
252dcdf4
AM
9286 break;
9287 default:
9288 break;
9289 }
9290
89c52ae3
AM
9291 switch (r_type)
9292 {
9293 case elfcpp::R_PPC64_D34:
9294 case elfcpp::R_PPC64_D34_LO:
9295 case elfcpp::R_PPC64_D34_HI30:
9296 case elfcpp::R_PPC64_D34_HA30:
9297 case elfcpp::R_PPC64_D28:
9298 case elfcpp::R_PPC64_PCREL34:
9299 case elfcpp::R_PPC64_PCREL28:
9300 case elfcpp::R_PPC64_TPREL34:
9301 case elfcpp::R_PPC64_DTPREL34:
9302 case elfcpp::R_PPC64_PLT_PCREL34:
9303 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9304 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
9305 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9306 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9307 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9308 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 9309 target->set_power10_relocs();
89c52ae3 9310 break;
d8f5a274
AM
9311 default:
9312 break;
9313 }
42cacb20
DE
9314}
9315
6d03d481
ST
9316// Process relocations for gc.
9317
9318template<int size, bool big_endian>
9319void
9320Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
9321 Symbol_table* symtab,
9322 Layout* layout,
9323 Sized_relobj_file<size, big_endian>* object,
9324 unsigned int data_shndx,
9325 unsigned int,
9326 const unsigned char* prelocs,
9327 size_t reloc_count,
9328 Output_section* output_section,
9329 bool needs_special_offset_handling,
9330 size_t local_symbol_count,
9331 const unsigned char* plocal_symbols)
6d03d481
ST
9332{
9333 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9334 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9335 Classify_reloc;
9336
e81fea4d
AM
9337 Powerpc_relobj<size, big_endian>* ppc_object
9338 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9339 if (size == 64)
9340 ppc_object->set_opd_valid();
9341 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9342 {
9343 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9344 for (p = ppc_object->access_from_map()->begin();
9345 p != ppc_object->access_from_map()->end();
9346 ++p)
9347 {
9348 Address dst_off = p->first;
9349 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9350 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9351 for (s = p->second.begin(); s != p->second.end(); ++s)
9352 {
efc6fa12 9353 Relobj* src_obj = s->first;
e81fea4d
AM
9354 unsigned int src_indx = s->second;
9355 symtab->gc()->add_reference(src_obj, src_indx,
9356 ppc_object, dst_indx);
9357 }
9358 p->second.clear();
9359 }
9360 ppc_object->access_from_map()->clear();
c6de8ed4 9361 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
9362 // Don't look at .opd relocs as .opd will reference everything.
9363 return;
9364 }
6d03d481 9365
4d625b70 9366 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9367 symtab,
9368 layout,
9369 this,
9370 object,
9371 data_shndx,
9372 prelocs,
9373 reloc_count,
9374 output_section,
9375 needs_special_offset_handling,
9376 local_symbol_count,
9377 plocal_symbols);
9378}
9379
e81fea4d
AM
9380// Handle target specific gc actions when adding a gc reference from
9381// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9382// and DST_OFF. For powerpc64, this adds a referenc to the code
9383// section of a function descriptor.
9384
9385template<int size, bool big_endian>
9386void
9387Target_powerpc<size, big_endian>::do_gc_add_reference(
9388 Symbol_table* symtab,
efc6fa12 9389 Relobj* src_obj,
e81fea4d 9390 unsigned int src_shndx,
efc6fa12 9391 Relobj* dst_obj,
e81fea4d
AM
9392 unsigned int dst_shndx,
9393 Address dst_off) const
9394{
6c77229c
AM
9395 if (size != 64 || dst_obj->is_dynamic())
9396 return;
9397
e81fea4d
AM
9398 Powerpc_relobj<size, big_endian>* ppc_object
9399 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9400 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9401 {
9402 if (ppc_object->opd_valid())
9403 {
9404 dst_shndx = ppc_object->get_opd_ent(dst_off);
9405 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9406 }
9407 else
9408 {
9409 // If we haven't run scan_opd_relocs, we must delay
9410 // processing this function descriptor reference.
9411 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9412 }
9413 }
9414}
9415
9416// Add any special sections for this symbol to the gc work list.
9417// For powerpc64, this adds the code section of a function
9418// descriptor.
9419
9420template<int size, bool big_endian>
9421void
9422Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9423 Symbol_table* symtab,
9424 Symbol* sym) const
9425{
6a31512f 9426 if (size == 64 && sym->object()->pluginobj() == NULL)
e81fea4d
AM
9427 {
9428 Powerpc_relobj<size, big_endian>* ppc_object
9429 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9430 bool is_ordinary;
9431 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9432 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9433 {
9434 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9435 Address dst_off = gsym->value();
c6de8ed4
AM
9436 if (ppc_object->opd_valid())
9437 {
9438 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9439 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9440 dst_indx));
c6de8ed4
AM
9441 }
9442 else
9443 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9444 }
9445 }
9446}
9447
dc3714f3
AM
9448// For a symbol location in .opd, set LOC to the location of the
9449// function entry.
9450
9451template<int size, bool big_endian>
9452void
9453Target_powerpc<size, big_endian>::do_function_location(
9454 Symbol_location* loc) const
9455{
a2d7bf59 9456 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9457 {
9458 if (loc->object->is_dynamic())
9459 {
9460 Powerpc_dynobj<size, big_endian>* ppc_object
9461 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9462 if (loc->shndx == ppc_object->opd_shndx())
9463 {
9464 Address dest_off;
9465 Address off = loc->offset - ppc_object->opd_address();
9466 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9467 loc->offset = dest_off;
9468 }
9469 }
9470 else
9471 {
9472 const Powerpc_relobj<size, big_endian>* ppc_object
9473 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9474 if (loc->shndx == ppc_object->opd_shndx())
9475 {
9476 Address dest_off;
9477 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9478 loc->offset = dest_off;
9479 }
9480 }
9481 }
9482}
9483
bbec1a5d
AM
9484// FNOFFSET in section SHNDX in OBJECT is the start of a function
9485// compiled with -fsplit-stack. The function calls non-split-stack
9486// code. Change the function to ensure it has enough stack space to
9487// call some random function.
9488
9489template<int size, bool big_endian>
9490void
9491Target_powerpc<size, big_endian>::do_calls_non_split(
9492 Relobj* object,
9493 unsigned int shndx,
9494 section_offset_type fnoffset,
9495 section_size_type fnsize,
6e0813d3
CC
9496 const unsigned char* prelocs,
9497 size_t reloc_count,
bbec1a5d
AM
9498 unsigned char* view,
9499 section_size_type view_size,
9500 std::string* from,
9501 std::string* to) const
9502{
9503 // 32-bit not supported.
9504 if (size == 32)
9505 {
9506 // warn
9507 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9508 prelocs, reloc_count, view, view_size,
9509 from, to);
bbec1a5d
AM
9510 return;
9511 }
9512
9513 // The function always starts with
9514 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9515 // addis %r12,%r1,-allocate@ha
9516 // addi %r12,%r12,-allocate@l
9517 // cmpld %r12,%r0
9518 // but note that the addis or addi may be replaced with a nop
9519
9520 unsigned char *entry = view + fnoffset;
9521 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9522
9523 if ((insn & 0xffff0000) == addis_2_12)
9524 {
9525 /* Skip ELFv2 global entry code. */
9526 entry += 8;
9527 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9528 }
9529
9530 unsigned char *pinsn = entry;
9531 bool ok = false;
9532 const uint32_t ld_private_ss = 0xe80d8fc0;
9533 if (insn == ld_private_ss)
9534 {
9535 int32_t allocate = 0;
9536 while (1)
9537 {
9538 pinsn += 4;
9539 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9540 if ((insn & 0xffff0000) == addis_12_1)
9541 allocate += (insn & 0xffff) << 16;
9542 else if ((insn & 0xffff0000) == addi_12_1
9543 || (insn & 0xffff0000) == addi_12_12)
9544 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9545 else if (insn != nop)
9546 break;
9547 }
9548 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9549 {
9550 int extra = parameters->options().split_stack_adjust_size();
9551 allocate -= extra;
9552 if (allocate >= 0 || extra < 0)
9553 {
9554 object->error(_("split-stack stack size overflow at "
9555 "section %u offset %0zx"),
9556 shndx, static_cast<size_t>(fnoffset));
9557 return;
9558 }
9559 pinsn = entry + 4;
9560 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9561 if (insn != addis_12_1)
9562 {
9563 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9564 pinsn += 4;
9565 insn = addi_12_12 | (allocate & 0xffff);
9566 if (insn != addi_12_12)
9567 {
9568 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9569 pinsn += 4;
9570 }
9571 }
9572 else
9573 {
9574 insn = addi_12_1 | (allocate & 0xffff);
9575 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9576 pinsn += 4;
9577 }
9578 if (pinsn != entry + 12)
9579 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9580
9581 ok = true;
9582 }
9583 }
9584
9585 if (!ok)
9586 {
9587 if (!object->has_no_split_stack())
9588 object->error(_("failed to match split-stack sequence at "
9589 "section %u offset %0zx"),
9590 shndx, static_cast<size_t>(fnoffset));
9591 }
9592}
9593
42cacb20
DE
9594// Scan relocations for a section.
9595
9596template<int size, bool big_endian>
9597void
9598Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9599 Symbol_table* symtab,
9600 Layout* layout,
9601 Sized_relobj_file<size, big_endian>* object,
9602 unsigned int data_shndx,
9603 unsigned int sh_type,
9604 const unsigned char* prelocs,
9605 size_t reloc_count,
9606 Output_section* output_section,
9607 bool needs_special_offset_handling,
9608 size_t local_symbol_count,
9609 const unsigned char* plocal_symbols)
42cacb20
DE
9610{
9611 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9612 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9613 Classify_reloc;
42cacb20 9614
7ee7ff70
AM
9615 if (!this->plt_localentry0_init_)
9616 {
9617 bool plt_localentry0 = false;
9618 if (size == 64
9619 && this->abiversion() >= 2)
9620 {
9621 if (parameters->options().user_set_plt_localentry())
9622 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9623 if (plt_localentry0
9624 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9625 gold_warning(_("--plt-localentry is especially dangerous without "
9626 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9627 }
9628 this->plt_localentry0_ = plt_localentry0;
9629 this->plt_localentry0_init_ = true;
9630 }
9631
42cacb20
DE
9632 if (sh_type == elfcpp::SHT_REL)
9633 {
9634 gold_error(_("%s: unsupported REL reloc section"),
9635 object->name().c_str());
9636 return;
9637 }
9638
4d625b70 9639 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9640 symtab,
9641 layout,
9642 this,
9643 object,
9644 data_shndx,
9645 prelocs,
9646 reloc_count,
9647 output_section,
9648 needs_special_offset_handling,
9649 local_symbol_count,
9650 plocal_symbols);
63e5eea2
AM
9651
9652 if (this->plt_localentry0_ && this->power10_relocs_)
9653 {
9654 gold_warning(_("--plt-localentry is incompatible with "
9655 "power10 pc-relative code"));
9656 this->plt_localentry0_ = false;
9657 }
42cacb20
DE
9658}
9659
ec4dbad3
AM
9660// Functor class for processing the global symbol table.
9661// Removes symbols defined on discarded opd entries.
9662
9663template<bool big_endian>
9664class Global_symbol_visitor_opd
9665{
9666 public:
9667 Global_symbol_visitor_opd()
9668 { }
9669
9670 void
9671 operator()(Sized_symbol<64>* sym)
9672 {
9673 if (sym->has_symtab_index()
9674 || sym->source() != Symbol::FROM_OBJECT
9675 || !sym->in_real_elf())
9676 return;
9677
6c77229c
AM
9678 if (sym->object()->is_dynamic())
9679 return;
9680
ec4dbad3
AM
9681 Powerpc_relobj<64, big_endian>* symobj
9682 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9683 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9684 return;
9685
9686 bool is_ordinary;
9687 unsigned int shndx = sym->shndx(&is_ordinary);
9688 if (shndx == symobj->opd_shndx()
9689 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9690 {
9691 sym->set_undefined();
e3ee8ed4 9692 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9693 sym->set_is_defined_in_discarded_section();
9694 sym->set_symtab_index(-1U);
9695 }
ec4dbad3
AM
9696 }
9697};
9698
f3a0ed29
AM
9699template<int size, bool big_endian>
9700void
9701Target_powerpc<size, big_endian>::define_save_restore_funcs(
9702 Layout* layout,
9703 Symbol_table* symtab)
9704{
9705 if (size == 64)
9706 {
d49044c7
AM
9707 Output_data_save_res<size, big_endian>* savres
9708 = new Output_data_save_res<size, big_endian>(symtab);
9709 this->savres_section_ = savres;
f3a0ed29
AM
9710 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9711 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9712 savres, ORDER_TEXT, false);
9713 }
9714}
9715
d8f5a274
AM
9716// Sort linker created .got section first (for the header), then input
9717// sections belonging to files using small model code.
9718
9719template<bool big_endian>
9720class Sort_toc_sections
9721{
9722 public:
9723 bool
9724 operator()(const Output_section::Input_section& is1,
9725 const Output_section::Input_section& is2) const
9726 {
9727 if (!is1.is_input_section() && is2.is_input_section())
9728 return true;
9729 bool small1
9730 = (is1.is_input_section()
9731 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9732 ->has_small_toc_reloc()));
9733 bool small2
9734 = (is2.is_input_section()
9735 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9736 ->has_small_toc_reloc()));
9737 return small1 && !small2;
9738 }
9739};
9740
42cacb20
DE
9741// Finalize the sections.
9742
9743template<int size, bool big_endian>
9744void
d5b40221
DK
9745Target_powerpc<size, big_endian>::do_finalize_sections(
9746 Layout* layout,
724436fc 9747 const Input_objects* input_objects,
ec4dbad3 9748 Symbol_table* symtab)
42cacb20 9749{
c9824451
AM
9750 if (parameters->doing_static_link())
9751 {
9752 // At least some versions of glibc elf-init.o have a strong
9753 // reference to __rela_iplt marker syms. A weak ref would be
9754 // better..
9755 if (this->iplt_ != NULL)
9756 {
9757 Reloc_section* rel = this->iplt_->rel_plt();
9758 symtab->define_in_output_data("__rela_iplt_start", NULL,
9759 Symbol_table::PREDEFINED, rel, 0, 0,
9760 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9761 elfcpp::STV_HIDDEN, 0, false, true);
9762 symtab->define_in_output_data("__rela_iplt_end", NULL,
9763 Symbol_table::PREDEFINED, rel, 0, 0,
9764 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9765 elfcpp::STV_HIDDEN, 0, true, true);
9766 }
9767 else
9768 {
9769 symtab->define_as_constant("__rela_iplt_start", NULL,
9770 Symbol_table::PREDEFINED, 0, 0,
9771 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9772 elfcpp::STV_HIDDEN, 0, true, false);
9773 symtab->define_as_constant("__rela_iplt_end", NULL,
9774 Symbol_table::PREDEFINED, 0, 0,
9775 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9776 elfcpp::STV_HIDDEN, 0, true, false);
9777 }
9778 }
9779
ec4dbad3
AM
9780 if (size == 64)
9781 {
9782 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9783 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
9784
9785 if (!parameters->options().relocatable())
9786 {
9787 this->define_save_restore_funcs(layout, symtab);
9788
9789 // Annoyingly, we need to make these sections now whether or
9790 // not we need them. If we delay until do_relax then we
9791 // need to mess with the relaxation machinery checkpointing.
9792 this->got_section(symtab, layout);
9793 this->make_brlt_section(layout);
d8f5a274
AM
9794
9795 if (parameters->options().toc_sort())
9796 {
9797 Output_section* os = this->got_->output_section();
9798 if (os != NULL && os->input_sections().size() > 1)
9799 std::stable_sort(os->input_sections().begin(),
9800 os->input_sections().end(),
9801 Sort_toc_sections<big_endian>());
9802 }
ec661b9d 9803 }
ec4dbad3
AM
9804 }
9805
42cacb20 9806 // Fill in some more dynamic tags.
c9269dff 9807 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 9808 if (odyn != NULL)
cf43a2fe 9809 {
c9824451
AM
9810 const Reloc_section* rel_plt = (this->plt_ == NULL
9811 ? NULL
9812 : this->plt_->rel_plt());
9813 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9814 this->rela_dyn_, true, size == 32);
9815
9816 if (size == 32)
dd93cd0a 9817 {
c9824451
AM
9818 if (this->got_ != NULL)
9819 {
9820 this->got_->finalize_data_size();
9821 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9822 this->got_, this->got_->g_o_t());
9823 }
34e0882b
AM
9824 if (this->has_tls_get_addr_opt_)
9825 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 9826 }
c9824451 9827 else
dd93cd0a 9828 {
c9824451
AM
9829 if (this->glink_ != NULL)
9830 {
9831 this->glink_->finalize_data_size();
9832 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9833 this->glink_,
9e390558 9834 (this->glink_->pltresolve_size()
c9824451
AM
9835 - 32));
9836 }
34e0882b 9837 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 9838 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
9839 ((this->has_localentry0_
9840 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9841 | (this->has_tls_get_addr_opt_
9842 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 9843 }
c9269dff 9844 }
cf43a2fe 9845
42cacb20
DE
9846 // Emit any relocs we saved in an attempt to avoid generating COPY
9847 // relocs.
9848 if (this->copy_relocs_.any_saved_relocs())
9849 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
9850
9851 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9852 p != input_objects->relobj_end();
9853 ++p)
9854 {
9855 Powerpc_relobj<size, big_endian>* ppc_relobj
9856 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9857 if (ppc_relobj->attributes_section_data())
6f3fe02b 9858 this->merge_object_attributes(ppc_relobj,
724436fc
AM
9859 ppc_relobj->attributes_section_data());
9860 }
9861 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9862 p != input_objects->dynobj_end();
9863 ++p)
9864 {
9865 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9866 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9867 if (ppc_dynobj->attributes_section_data())
6f3fe02b 9868 this->merge_object_attributes(ppc_dynobj,
724436fc
AM
9869 ppc_dynobj->attributes_section_data());
9870 }
9871
9872 // Create a .gnu.attributes section if we have merged any attributes
9873 // from inputs.
9874 if (this->attributes_section_data_ != NULL
9875 && this->attributes_section_data_->size() != 0)
9876 {
9877 Output_attributes_section_data* attributes_section
9878 = new Output_attributes_section_data(*this->attributes_section_data_);
9879 layout->add_output_section_data(".gnu.attributes",
9880 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9881 attributes_section, ORDER_INVALID, false);
9882 }
9883}
9884
9885// Merge object attributes from input file called NAME with those of the
9886// output. The input object attributes are in the object pointed by PASD.
9887
9888template<int size, bool big_endian>
9889void
9890Target_powerpc<size, big_endian>::merge_object_attributes(
6f3fe02b 9891 const Object* obj,
724436fc
AM
9892 const Attributes_section_data* pasd)
9893{
9894 // Return if there is no attributes section data.
9895 if (pasd == NULL)
9896 return;
9897
9898 // Create output object attributes.
9899 if (this->attributes_section_data_ == NULL)
9900 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9901
9902 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9903 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9904 Object_attribute* out_attr
9905 = this->attributes_section_data_->known_attributes(vendor);
9906
6f3fe02b 9907 const char* name = obj->name().c_str();
724436fc
AM
9908 const char* err;
9909 const char* first;
9910 const char* second;
9911 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9912 int in_fp = in_attr[tag].int_value() & 0xf;
9913 int out_fp = out_attr[tag].int_value() & 0xf;
6f3fe02b 9914 bool warn_only = obj->is_dynamic();
724436fc
AM
9915 if (in_fp != out_fp)
9916 {
9917 err = NULL;
9918 if ((in_fp & 3) == 0)
9919 ;
9920 else if ((out_fp & 3) == 0)
9921 {
6f3fe02b
AM
9922 if (!warn_only)
9923 {
9924 out_fp |= in_fp & 3;
9925 out_attr[tag].set_int_value(out_fp);
9926 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9927 this->last_fp_ = name;
9928 }
724436fc
AM
9929 }
9930 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9931 {
9932 err = N_("%s uses hard float, %s uses soft float");
9933 first = this->last_fp_;
9934 second = name;
9935 }
9936 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9937 {
9938 err = N_("%s uses hard float, %s uses soft float");
9939 first = name;
9940 second = this->last_fp_;
9941 }
9942 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9943 {
9944 err = N_("%s uses double-precision hard float, "
9945 "%s uses single-precision hard float");
9946 first = this->last_fp_;
9947 second = name;
9948 }
9949 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9950 {
9951 err = N_("%s uses double-precision hard float, "
9952 "%s uses single-precision hard float");
9953 first = name;
9954 second = this->last_fp_;
9955 }
9956
9957 if (err || (in_fp & 0xc) == 0)
9958 ;
9959 else if ((out_fp & 0xc) == 0)
9960 {
6f3fe02b
AM
9961 if (!warn_only)
9962 {
9963 out_fp |= in_fp & 0xc;
9964 out_attr[tag].set_int_value(out_fp);
9965 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9966 this->last_ld_ = name;
9967 }
724436fc
AM
9968 }
9969 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9970 {
9971 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9972 first = name;
9973 second = this->last_ld_;
9974 }
9975 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9976 {
9977 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9978 first = this->last_ld_;
9979 second = name;
9980 }
9981 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9982 {
9983 err = N_("%s uses IBM long double, %s uses IEEE long double");
9984 first = this->last_ld_;
9985 second = name;
9986 }
9987 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9988 {
9989 err = N_("%s uses IBM long double, %s uses IEEE long double");
9990 first = name;
9991 second = this->last_ld_;
9992 }
9993
9994 if (err)
9995 {
9996 if (parameters->options().warn_mismatch())
6f3fe02b
AM
9997 {
9998 if (warn_only)
9999 gold_warning(_(err), first, second);
10000 else
10001 gold_error(_(err), first, second);
10002 }
724436fc
AM
10003 // Arrange for this attribute to be deleted. It's better to
10004 // say "don't know" about a file than to wrongly claim compliance.
6f3fe02b
AM
10005 if (!warn_only)
10006 out_attr[tag].set_type(0);
724436fc
AM
10007 }
10008 }
10009
10010 if (size == 32)
10011 {
10012 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10013 int in_vec = in_attr[tag].int_value() & 3;
10014 int out_vec = out_attr[tag].int_value() & 3;
10015 if (in_vec != out_vec)
10016 {
10017 err = NULL;
10018 if (in_vec == 0)
10019 ;
10020 else if (out_vec == 0)
10021 {
10022 out_vec = in_vec;
10023 out_attr[tag].set_int_value(out_vec);
10024 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10025 this->last_vec_ = name;
10026 }
10027 // For now, allow generic to transition to AltiVec or SPE
10028 // without a warning. If GCC marked files with their stack
10029 // alignment and used don't-care markings for files which are
10030 // not affected by the vector ABI, we could warn about this
10031 // case too. */
10032 else if (in_vec == 1)
10033 ;
10034 else if (out_vec == 1)
10035 {
10036 out_vec = in_vec;
10037 out_attr[tag].set_int_value(out_vec);
10038 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10039 this->last_vec_ = name;
10040 }
10041 else if (out_vec < in_vec)
10042 {
10043 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10044 first = this->last_vec_;
10045 second = name;
10046 }
10047 else if (out_vec > in_vec)
10048 {
10049 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10050 first = name;
10051 second = this->last_vec_;
10052 }
10053 if (err)
10054 {
10055 if (parameters->options().warn_mismatch())
10056 gold_error(_(err), first, second);
10057 out_attr[tag].set_type(0);
10058 }
10059 }
10060
10061 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10062 int in_struct = in_attr[tag].int_value() & 3;
10063 int out_struct = out_attr[tag].int_value() & 3;
10064 if (in_struct != out_struct)
10065 {
10066 err = NULL;
10067 if (in_struct == 0 || in_struct == 3)
10068 ;
10069 else if (out_struct == 0)
10070 {
10071 out_struct = in_struct;
10072 out_attr[tag].set_int_value(out_struct);
10073 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10074 this->last_struct_ = name;
10075 }
10076 else if (out_struct < in_struct)
10077 {
10078 err = N_("%s uses r3/r4 for small structure returns, "
10079 "%s uses memory");
10080 first = this->last_struct_;
10081 second = name;
10082 }
10083 else if (out_struct > in_struct)
10084 {
10085 err = N_("%s uses r3/r4 for small structure returns, "
10086 "%s uses memory");
10087 first = name;
10088 second = this->last_struct_;
10089 }
10090 if (err)
10091 {
10092 if (parameters->options().warn_mismatch())
10093 gold_error(_(err), first, second);
10094 out_attr[tag].set_type(0);
10095 }
10096 }
10097 }
10098
10099 // Merge Tag_compatibility attributes and any common GNU ones.
10100 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
10101}
10102
5edad15d
AM
10103// Emit any saved relocs, and mark toc entries using any of these
10104// relocs as not optimizable.
aba6bc71 10105
5edad15d
AM
10106template<int sh_type, int size, bool big_endian>
10107void
10108Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10109 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 10110{
5edad15d
AM
10111 if (size == 64
10112 && parameters->options().toc_optimize())
10113 {
10114 for (typename Copy_relocs<sh_type, size, big_endian>::
10115 Copy_reloc_entries::iterator p = this->entries_.begin();
10116 p != this->entries_.end();
10117 ++p)
10118 {
10119 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10120 entry = *p;
10121
10122 // If the symbol is no longer defined in a dynamic object,
10123 // then we emitted a COPY relocation. If it is still
10124 // dynamic then we'll need dynamic relocations and thus
10125 // can't optimize toc entries.
10126 if (entry.sym_->is_from_dynobj())
10127 {
10128 Powerpc_relobj<size, big_endian>* ppc_object
10129 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10130 if (entry.shndx_ == ppc_object->toc_shndx())
10131 ppc_object->set_no_toc_opt(entry.address_);
10132 }
10133 }
10134 }
10135
10136 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
10137}
10138
3ea0a085
AM
10139// Return the value to use for a branch relocation.
10140
10141template<int size, bool big_endian>
1611bc4a 10142bool
3ea0a085 10143Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 10144 const Symbol_table* symtab,
3ea0a085
AM
10145 const Sized_symbol<size>* gsym,
10146 Powerpc_relobj<size, big_endian>* object,
1611bc4a 10147 Address *value,
3ea0a085
AM
10148 unsigned int *dest_shndx)
10149{
9055360d
AM
10150 if (size == 32 || this->abiversion() >= 2)
10151 gold_unreachable();
3ea0a085 10152 *dest_shndx = 0;
3ea0a085
AM
10153
10154 // If the symbol is defined in an opd section, ie. is a function
10155 // descriptor, use the function descriptor code entry address
10156 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 10157 if (gsym != NULL
0e123f69
AM
10158 && (gsym->source() != Symbol::FROM_OBJECT
10159 || gsym->object()->is_dynamic()))
1611bc4a 10160 return true;
3ea0a085
AM
10161 if (gsym != NULL)
10162 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10163 unsigned int shndx = symobj->opd_shndx();
10164 if (shndx == 0)
1611bc4a 10165 return true;
3ea0a085 10166 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 10167 if (opd_addr == invalid_address)
1611bc4a 10168 return true;
c6905c28 10169 opd_addr += symobj->output_section_address(shndx);
1611bc4a 10170 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
10171 {
10172 Address sec_off;
1611bc4a 10173 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
10174 if (symtab->is_section_folded(symobj, *dest_shndx))
10175 {
10176 Section_id folded
10177 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10178 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10179 *dest_shndx = folded.second;
10180 }
3ea0a085 10181 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
10182 if (sec_addr == invalid_address)
10183 return false;
10184
3ea0a085 10185 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 10186 *value = sec_addr + sec_off;
3ea0a085 10187 }
1611bc4a 10188 return true;
3ea0a085
AM
10189}
10190
c9b8abb7
AM
10191template<int size>
10192static bool
10193relative_value_is_known(const Sized_symbol<size>* gsym)
10194{
10195 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10196 return false;
10197
10198 if (gsym->is_from_dynobj()
10199 || gsym->is_undefined()
10200 || gsym->is_preemptible())
10201 return false;
10202
10203 if (gsym->is_absolute())
10204 return !parameters->options().output_is_position_independent();
10205
10206 return true;
10207}
10208
10209template<int size>
10210static bool
10211relative_value_is_known(const Symbol_value<size>* psymval)
10212{
10213 if (psymval->is_ifunc_symbol())
10214 return false;
10215
10216 bool is_ordinary;
10217 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10218
10219 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10220}
10221
0c951c25
AM
10222// PCREL_OPT in one instance flags to the linker that a pair of insns:
10223// pld ra,symbol@got@pcrel
10224// load/store rt,0(ra)
10225// or
10226// pla ra,symbol@pcrel
10227// load/store rt,0(ra)
10228// may be translated to
10229// pload/pstore rt,symbol@pcrel
10230// nop.
10231// This function returns true if the optimization is possible, placing
10232// the prefix insn in *PINSN1 and a NOP in *PINSN2.
10233//
10234// On entry to this function, the linker has already determined that
10235// the pld can be replaced with pla: *PINSN1 is that pla insn,
10236// while *PINSN2 is the second instruction.
10237
10238inline bool
10239xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10240{
10241 uint32_t insn2 = *pinsn2 >> 32;
10242 uint64_t i1new;
10243
10244 // Check that regs match.
10245 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10246 return false;
10247
10248 switch ((insn2 >> 26) & 63)
10249 {
10250 default:
10251 return false;
10252
10253 case 32: // lwz
10254 case 34: // lbz
10255 case 36: // stw
10256 case 38: // stb
10257 case 40: // lhz
10258 case 42: // lha
10259 case 44: // sth
10260 case 48: // lfs
10261 case 50: // lfd
10262 case 52: // stfs
10263 case 54: // stfd
10264 // These are the PMLS cases, where we just need to tack a prefix
10265 // on the insn. Check that the D field is zero.
10266 if ((insn2 & 0xffff) != 0)
10267 return false;
10268 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10269 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10270 break;
10271
10272 case 58: // lwa, ld
10273 if ((insn2 & 0xfffd) != 0)
10274 return false;
10275 i1new = ((1ULL << 58) | (1ULL << 52)
10276 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10277 | (insn2 & (31ULL << 21)));
10278 break;
10279
10280 case 57: // lxsd, lxssp
10281 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10282 return false;
10283 i1new = ((1ULL << 58) | (1ULL << 52)
10284 | ((40ULL | (insn2 & 3)) << 26)
10285 | (insn2 & (31ULL << 21)));
10286 break;
10287
10288 case 61: // stxsd, stxssp, lxv, stxv
10289 if ((insn2 & 3) == 0)
10290 return false;
10291 else if ((insn2 & 3) >= 2)
10292 {
10293 if ((insn2 & 0xfffc) != 0)
10294 return false;
10295 i1new = ((1ULL << 58) | (1ULL << 52)
10296 | ((44ULL | (insn2 & 3)) << 26)
10297 | (insn2 & (31ULL << 21)));
10298 }
10299 else
10300 {
10301 if ((insn2 & 0xfff0) != 0)
10302 return false;
10303 i1new = ((1ULL << 58) | (1ULL << 52)
10304 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10305 | (insn2 & (31ULL << 21)));
10306 }
10307 break;
10308
10309 case 56: // lq
10310 if ((insn2 & 0xffff) != 0)
10311 return false;
10312 i1new = ((1ULL << 58) | (1ULL << 52)
10313 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10314 break;
10315
10316 case 62: // std, stq
10317 if ((insn2 & 0xfffd) != 0)
10318 return false;
10319 i1new = ((1ULL << 58) | (1ULL << 52)
10320 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10321 | (insn2 & (31ULL << 21)));
10322 break;
10323 }
10324
10325 *pinsn1 = i1new;
10326 *pinsn2 = (uint64_t) nop << 32;
10327 return true;
10328}
10329
42cacb20
DE
10330// Perform a relocation.
10331
10332template<int size, bool big_endian>
10333inline bool
10334Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 10335 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 10336 unsigned int,
d83ce4e3
AM
10337 Target_powerpc* target,
10338 Output_section* os,
10339 size_t relnum,
91a65d2f 10340 const unsigned char* preloc,
d83ce4e3
AM
10341 const Sized_symbol<size>* gsym,
10342 const Symbol_value<size>* psymval,
10343 unsigned char* view,
c9269dff
AM
10344 Address address,
10345 section_size_type view_size)
42cacb20 10346{
23cedd1d
AM
10347 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10348 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10349 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10350
0e804863
ILT
10351 if (view == NULL)
10352 return true;
10353
34e0882b
AM
10354 if (target->replace_tls_get_addr(gsym))
10355 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10356
91a65d2f
AM
10357 const elfcpp::Rela<size, big_endian> rela(preloc);
10358 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
34e0882b 10359 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 10360 {
e3deeb9c
AM
10361 case Track_tls::NOT_EXPECTED:
10362 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10363 _("__tls_get_addr call lacks marker reloc"));
10364 break;
10365 case Track_tls::EXPECTED:
10366 // We have already complained.
10367 break;
10368 case Track_tls::SKIP:
23cedd1d 10369 if (is_plt16_reloc<size>(r_type)
32f59844
AM
10370 || r_type == elfcpp::R_POWERPC_PLTSEQ
10371 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
10372 {
10373 Insn* iview = reinterpret_cast<Insn*>(view);
10374 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10375 }
10376 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10377 {
10378 Insn* iview = reinterpret_cast<Insn*>(view);
10379 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10380 }
e4dff765
AM
10381 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10382 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10383 {
10384 Insn* iview = reinterpret_cast<Insn*>(view);
10385 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10386 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10387 }
e3deeb9c
AM
10388 return true;
10389 case Track_tls::NORMAL:
10390 break;
dd93cd0a 10391 }
dd93cd0a 10392
dcfc7dd4
AM
10393 // Offset from start of insn to d-field reloc.
10394 const int d_offset = big_endian ? 2 : 0;
10395
3ea0a085
AM
10396 Powerpc_relobj<size, big_endian>* const object
10397 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 10398 Address value = 0;
0cfb0717 10399 bool has_stub_value = false;
7ee7ff70 10400 bool localentry0 = false;
e5d5f5ed 10401 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
08be3224
AM
10402 bool has_plt_offset
10403 = (gsym != NULL
88b8e639 10404 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
08be3224
AM
10405 : object->local_has_plt_offset(r_sym));
10406 if (has_plt_offset
4290b0ab 10407 && !is_got_reloc(r_type)
08be3224 10408 && !is_plt16_reloc<size>(r_type)
e4dff765
AM
10409 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10410 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
23cedd1d
AM
10411 && r_type != elfcpp::R_POWERPC_PLTSEQ
10412 && r_type != elfcpp::R_POWERPC_PLTCALL
32f59844
AM
10413 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10414 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
b3ccdeb5 10415 && (!psymval->is_ifunc_symbol()
9055360d 10416 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 10417 {
9055360d
AM
10418 if (size == 64
10419 && gsym != NULL
10420 && target->abiversion() >= 2
10421 && !parameters->options().output_is_position_independent()
32f59844 10422 && !is_branch_reloc<size>(r_type))
ec661b9d 10423 {
faa2211d
AM
10424 Address off = target->glink_section()->find_global_entry(gsym);
10425 if (off != invalid_address)
6ec65f28
AM
10426 {
10427 value = target->glink_section()->global_entry_address() + off;
10428 has_stub_value = true;
10429 }
ec661b9d 10430 }
c9824451 10431 else
9055360d 10432 {
64b5d6d7
AM
10433 Stub_table<size, big_endian>* stub_table = NULL;
10434 if (target->stub_tables().size() == 1)
10435 stub_table = target->stub_tables()[0];
10436 if (stub_table == NULL
10437 && !(size == 32
10438 && gsym != NULL
10439 && !parameters->options().output_is_position_independent()
32f59844 10440 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10441 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10442 if (stub_table == NULL)
10443 {
64b5d6d7
AM
10444 // This is a ref from a data section to an ifunc symbol,
10445 // or a non-branch reloc for which we always want to use
10446 // one set of stubs for resolving function addresses.
9055360d
AM
10447 if (target->stub_tables().size() != 0)
10448 stub_table = target->stub_tables()[0];
10449 }
faa2211d
AM
10450 if (stub_table != NULL)
10451 {
7e57d19e 10452 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10453 if (gsym != NULL)
7e57d19e 10454 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10455 rela.get_r_addend());
10456 else
7e57d19e 10457 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10458 rela.get_r_addend());
7e57d19e 10459 if (ent != NULL)
faa2211d 10460 {
7e57d19e
AM
10461 value = stub_table->stub_address() + ent->off_;
10462 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10463 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10464 size_t reloc_count = shdr.get_sh_size() / reloc_size;
afd2ea23
AM
10465 if (size == 64
10466 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
10467 value += ent->tocoff_;
7e57d19e
AM
10468 if (size == 64
10469 && ent->r2save_
a993d270
AM
10470 && !(gsym != NULL
10471 && target->is_tls_get_addr_opt(gsym)))
afd2ea23 10472 {
a993d270
AM
10473 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10474 {
10475 if (!(target->power10_stubs()
10476 && target->power10_stubs_auto()))
10477 value += 4;
10478 }
10479 else if (relnum < reloc_count - 1)
10480 {
10481 Reltype next_rela(preloc + reloc_size);
10482 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10483 == elfcpp::R_PPC64_TOCSAVE
10484 && (next_rela.get_r_offset()
10485 == rela.get_r_offset() + 4))
10486 value += 4;
10487 }
7e57d19e 10488 }
7ee7ff70 10489 localentry0 = ent->localentry0_;
faa2211d
AM
10490 has_stub_value = true;
10491 }
10492 }
9055360d 10493 }
faa2211d
AM
10494 // We don't care too much about bogus debug references to
10495 // non-local functions, but otherwise there had better be a plt
10496 // call stub or global entry stub as appropriate.
10497 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10498 }
cf43a2fe 10499
e4dff765
AM
10500 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10501 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10502 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10503 {
10504 const Output_data_plt_powerpc<size, big_endian>* plt;
10505 if (gsym)
10506 value = target->plt_off(gsym, &plt);
10507 else
10508 value = target->plt_off(object, r_sym, &plt);
10509 value += plt->address();
10510
10511 if (size == 64)
e4dff765
AM
10512 {
10513 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10514 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10515 value -= (target->got_section()->output_section()->address()
10516 + object->toc_base_offset());
10517 }
08be3224
AM
10518 else if (parameters->options().output_is_position_independent())
10519 {
10520 if (rela.get_r_addend() >= 32768)
10521 {
10522 unsigned int got2 = object->got2_shndx();
10523 value -= (object->get_output_section_offset(got2)
10524 + object->output_section(got2)->address()
10525 + rela.get_r_addend());
10526 }
10527 else
10528 value -= (target->got_section()->address()
10529 + target->got_section()->g_o_t());
10530 }
10531 }
23cedd1d
AM
10532 else if (!has_plt_offset
10533 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10534 || r_type == elfcpp::R_POWERPC_PLTSEQ
10535 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10536 {
10537 Insn* iview = reinterpret_cast<Insn*>(view);
10538 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10539 r_type = elfcpp::R_POWERPC_NONE;
10540 }
e4dff765
AM
10541 else if (!has_plt_offset
10542 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10543 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10544 {
10545 Insn* iview = reinterpret_cast<Insn*>(view);
10546 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10547 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10548 r_type = elfcpp::R_POWERPC_NONE;
10549 }
4290b0ab 10550 else if (is_got_reloc(r_type))
42cacb20 10551 {
cf43a2fe
AM
10552 if (gsym != NULL)
10553 {
10554 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10555 value = gsym->got_offset(GOT_TYPE_STANDARD);
10556 }
10557 else
10558 {
cf43a2fe
AM
10559 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10560 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10561 }
e4dff765
AM
10562 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10563 value += target->got_section()->address();
10564 else
10565 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
10566 }
10567 else if (r_type == elfcpp::R_PPC64_TOC)
10568 {
c9269dff 10569 value = (target->got_section()->output_section()->address()
dd93cd0a 10570 + object->toc_base_offset());
cf43a2fe
AM
10571 }
10572 else if (gsym != NULL
10573 && (r_type == elfcpp::R_POWERPC_REL24
10574 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10575 && has_stub_value)
cf43a2fe 10576 {
c9269dff
AM
10577 if (size == 64)
10578 {
10579 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10580 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10581 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10582 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10583 {
3ea0a085 10584 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10585 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10586 if ((insn & 1) != 0
10587 && (insn2 == nop
10588 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10589 {
b4f7960d
AM
10590 elfcpp::Swap<32, big_endian>::
10591 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10592 can_plt_call = true;
10593 }
10594 }
10595 if (!can_plt_call)
3ea0a085
AM
10596 {
10597 // If we don't have a branch and link followed by a nop,
10598 // we can't go via the plt because there is no place to
10599 // put a toc restoring instruction.
10600 // Unless we know we won't be returning.
10601 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10602 can_plt_call = true;
10603 }
10604 if (!can_plt_call)
10605 {
ba8ca3e7
AM
10606 // g++ as of 20130507 emits self-calls without a
10607 // following nop. This is arguably wrong since we have
10608 // conflicting information. On the one hand a global
10609 // symbol and on the other a local call sequence, but
10610 // don't error for this special case.
10611 // It isn't possible to cheaply verify we have exactly
10612 // such a call. Allow all calls to the same section.
3ea0a085 10613 bool ok = false;
c9824451 10614 Address code = value;
3ea0a085
AM
10615 if (gsym->source() == Symbol::FROM_OBJECT
10616 && gsym->object() == object)
10617 {
9055360d
AM
10618 unsigned int dest_shndx = 0;
10619 if (target->abiversion() < 2)
10620 {
10621 Address addend = rela.get_r_addend();
1611bc4a
AM
10622 code = psymval->value(object, addend);
10623 target->symval_for_branch(relinfo->symtab, gsym, object,
10624 &code, &dest_shndx);
9055360d 10625 }
3ea0a085
AM
10626 bool is_ordinary;
10627 if (dest_shndx == 0)
10628 dest_shndx = gsym->shndx(&is_ordinary);
10629 ok = dest_shndx == relinfo->data_shndx;
10630 }
10631 if (!ok)
c9824451
AM
10632 {
10633 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10634 _("call lacks nop, can't restore toc; "
10635 "recompile with -fPIC"));
10636 value = code;
10637 }
3ea0a085 10638 }
c9269dff 10639 }
cf43a2fe 10640 }
dd93cd0a
AM
10641 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10642 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10643 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3 10644 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
87c69f97 10645 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a
AM
10646 {
10647 // First instruction of a global dynamic sequence, arg setup insn.
10648 const bool final = gsym == NULL || gsym->final_value_is_known();
10649 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10650 enum Got_type got_type = GOT_TYPE_STANDARD;
10651 if (tls_type == tls::TLSOPT_NONE)
10652 got_type = GOT_TYPE_TLSGD;
10653 else if (tls_type == tls::TLSOPT_TO_IE)
10654 got_type = GOT_TYPE_TPREL;
10655 if (got_type != GOT_TYPE_STANDARD)
10656 {
10657 if (gsym != NULL)
10658 {
10659 gold_assert(gsym->has_got_offset(got_type));
10660 value = gsym->got_offset(got_type);
10661 }
10662 else
10663 {
dd93cd0a
AM
10664 gold_assert(object->local_has_got_offset(r_sym, got_type));
10665 value = object->local_got_offset(r_sym, got_type);
10666 }
87c69f97 10667 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
89c52ae3
AM
10668 value += target->got_section()->address();
10669 else
10670 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10671 }
10672 if (tls_type == tls::TLSOPT_TO_IE)
10673 {
87c69f97 10674 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 10675 {
89c52ae3
AM
10676 Insn* iview = reinterpret_cast<Insn*>(view);
10677 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10678 pinsn <<= 32;
10679 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10680 // pla -> pld
10681 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10682 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10683 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10684 pinsn & 0xffffffff);
87c69f97 10685 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
89c52ae3
AM
10686 }
10687 else
10688 {
10689 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10690 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10691 {
10692 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10693 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10694 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10695 if (size == 32)
10696 insn |= 32 << 26; // lwz
10697 else
10698 insn |= 58 << 26; // ld
10699 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10700 }
10701 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10702 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 10703 }
dd93cd0a
AM
10704 }
10705 else if (tls_type == tls::TLSOPT_TO_LE)
10706 {
87c69f97 10707 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 10708 {
89c52ae3
AM
10709 Insn* iview = reinterpret_cast<Insn*>(view);
10710 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10711 pinsn <<= 32;
10712 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10713 // pla pcrel -> paddi r13
10714 pinsn += (-1ULL << 52) + (13ULL << 16);
10715 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10716 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10717 pinsn & 0xffffffff);
10718 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
10719 value = psymval->value(object, rela.get_r_addend());
10720 }
10721 else
10722 {
89c52ae3
AM
10723 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10724 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10725 {
10726 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10727 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10728 insn &= (1 << 26) - (1 << 21); // extract rt
10729 if (size == 32)
10730 insn |= addis_0_2;
10731 else
10732 insn |= addis_0_13;
10733 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10734 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10735 value = psymval->value(object, rela.get_r_addend());
10736 }
10737 else
10738 {
10739 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10740 Insn insn = nop;
10741 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10742 r_type = elfcpp::R_POWERPC_NONE;
10743 }
dd93cd0a
AM
10744 }
10745 }
10746 }
10747 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10748 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10749 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3 10750 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
87c69f97 10751 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
dd93cd0a
AM
10752 {
10753 // First instruction of a local dynamic sequence, arg setup insn.
10754 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10755 if (tls_type == tls::TLSOPT_NONE)
10756 {
10757 value = target->tlsld_got_offset();
87c69f97 10758 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
10759 value += target->got_section()->address();
10760 else
10761 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10762 }
10763 else
10764 {
10765 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 10766 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
10767 {
10768 Insn* iview = reinterpret_cast<Insn*>(view);
10769 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10770 pinsn <<= 32;
10771 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10772 // pla pcrel -> paddi r13
10773 pinsn += (-1ULL << 52) + (13ULL << 16);
10774 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10775 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10776 pinsn & 0xffffffff);
10777 r_type = elfcpp::R_PPC64_TPREL34;
10778 value = dtp_offset;
10779 }
10780 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10781 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 10782 {
dcfc7dd4 10783 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
10784 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10785 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 10786 if (size == 32)
0f81d3f0
AM
10787 insn |= addis_0_2;
10788 else
10789 insn |= addis_0_13;
dd93cd0a
AM
10790 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10791 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 10792 value = dtp_offset;
dd93cd0a
AM
10793 }
10794 else
10795 {
dcfc7dd4 10796 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10797 Insn insn = nop;
10798 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10799 r_type = elfcpp::R_POWERPC_NONE;
10800 }
10801 }
10802 }
10803 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10804 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10805 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3 10806 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
87c69f97 10807 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
dd93cd0a
AM
10808 {
10809 // Accesses relative to a local dynamic sequence address,
10810 // no optimisation here.
10811 if (gsym != NULL)
10812 {
10813 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10814 value = gsym->got_offset(GOT_TYPE_DTPREL);
10815 }
10816 else
10817 {
dd93cd0a
AM
10818 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10819 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10820 }
87c69f97 10821 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
89c52ae3
AM
10822 value += target->got_section()->address();
10823 else
10824 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10825 }
10826 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10827 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10828 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3 10829 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
87c69f97 10830 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
dd93cd0a
AM
10831 {
10832 // First instruction of initial exec sequence.
10833 const bool final = gsym == NULL || gsym->final_value_is_known();
10834 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10835 if (tls_type == tls::TLSOPT_NONE)
10836 {
10837 if (gsym != NULL)
10838 {
10839 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10840 value = gsym->got_offset(GOT_TYPE_TPREL);
10841 }
10842 else
10843 {
dd93cd0a
AM
10844 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10845 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10846 }
87c69f97 10847 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
10848 value += target->got_section()->address();
10849 else
10850 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10851 }
10852 else
10853 {
10854 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 10855 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
10856 {
10857 Insn* iview = reinterpret_cast<Insn*>(view);
10858 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10859 pinsn <<= 32;
10860 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10861 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10862 pinsn += ((2ULL << 56) + (-1ULL << 52)
10863 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10864 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10865 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10866 pinsn & 0xffffffff);
10867 r_type = elfcpp::R_PPC64_TPREL34;
10868 value = psymval->value(object, rela.get_r_addend());
10869 }
10870 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10871 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 10872 {
dcfc7dd4 10873 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10874 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10875 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10876 if (size == 32)
10877 insn |= addis_0_2;
10878 else
10879 insn |= addis_0_13;
10880 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10881 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10882 value = psymval->value(object, rela.get_r_addend());
10883 }
10884 else
10885 {
dcfc7dd4 10886 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10887 Insn insn = nop;
10888 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10889 r_type = elfcpp::R_POWERPC_NONE;
10890 }
10891 }
10892 }
10893 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10894 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10895 {
10896 // Second instruction of a global dynamic sequence,
10897 // the __tls_get_addr call
e3deeb9c 10898 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10899 const bool final = gsym == NULL || gsym->final_value_is_known();
10900 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10901 if (tls_type != tls::TLSOPT_NONE)
10902 {
10903 if (tls_type == tls::TLSOPT_TO_IE)
10904 {
10905 Insn* iview = reinterpret_cast<Insn*>(view);
10906 Insn insn = add_3_3_13;
10907 if (size == 32)
10908 insn = add_3_3_2;
10909 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10910 r_type = elfcpp::R_POWERPC_NONE;
10911 }
10912 else
10913 {
89c52ae3
AM
10914 bool is_pcrel = false;
10915 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10916 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10917 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10918 if (relnum < reloc_count - 1)
10919 {
10920 Reltype next_rela(preloc + reloc_size);
10921 unsigned int r_type2
10922 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10923 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10924 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10925 && next_rela.get_r_offset() == rela.get_r_offset())
10926 is_pcrel = true;
10927 }
dd93cd0a 10928 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10929 if (is_pcrel)
10930 {
10931 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10932 r_type = elfcpp::R_POWERPC_NONE;
10933 }
10934 else
10935 {
10936 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10937 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10938 view += d_offset;
10939 value = psymval->value(object, rela.get_r_addend());
10940 }
dd93cd0a 10941 }
e3deeb9c 10942 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10943 }
10944 }
10945 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10946 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10947 {
10948 // Second instruction of a local dynamic sequence,
10949 // the __tls_get_addr call
e3deeb9c 10950 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10951 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10952 if (tls_type == tls::TLSOPT_TO_LE)
10953 {
89c52ae3
AM
10954 bool is_pcrel = false;
10955 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10956 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10957 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10958 if (relnum < reloc_count - 1)
10959 {
10960 Reltype next_rela(preloc + reloc_size);
10961 unsigned int r_type2
10962 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10963 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10964 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10965 && next_rela.get_r_offset() == rela.get_r_offset())
10966 is_pcrel = true;
10967 }
dd93cd0a 10968 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10969 if (is_pcrel)
10970 {
10971 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10972 r_type = elfcpp::R_POWERPC_NONE;
10973 }
10974 else
10975 {
10976 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10977 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10978 view += d_offset;
10979 value = dtp_offset;
10980 }
e3deeb9c 10981 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10982 }
10983 }
10984 else if (r_type == elfcpp::R_POWERPC_TLS)
10985 {
10986 // Second instruction of an initial exec sequence
10987 const bool final = gsym == NULL || gsym->final_value_is_known();
10988 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10989 if (tls_type == tls::TLSOPT_TO_LE)
10990 {
89c52ae3
AM
10991 Address roff = rela.get_r_offset() & 3;
10992 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
10993 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10994 unsigned int reg = size == 32 ? 2 : 13;
10995 insn = at_tls_transform(insn, reg);
10996 gold_assert(insn != 0);
89c52ae3
AM
10997 if (roff == 0)
10998 {
10999 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11000 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11001 view += d_offset;
11002 value = psymval->value(object, rela.get_r_addend());
11003 }
11004 else if (roff == 1)
11005 {
11006 // For pcrel IE to LE we already have the full offset
11007 // and thus don't need an addi here. A nop or mr will do.
11008 if ((insn & (0x3f << 26)) == 14 << 26)
11009 {
11010 // Extract regs from addi rt,ra,si.
11011 unsigned int rt = (insn >> 21) & 0x1f;
11012 unsigned int ra = (insn >> 16) & 0x1f;
11013 if (rt == ra)
11014 insn = nop;
11015 else
11016 {
11017 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11018 insn = (rt << 16) | (ra << 21) | (ra << 11);
11019 insn |= (31u << 26) | (444u << 1);
11020 }
11021 }
11022 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11023 r_type = elfcpp::R_POWERPC_NONE;
11024 }
dd93cd0a
AM
11025 }
11026 }
0cfb0717 11027 else if (!has_stub_value)
cf43a2fe 11028 {
32f59844
AM
11029 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11030 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
11031 {
11032 // PLTCALL without plt entry => convert to direct call
11033 Insn* iview = reinterpret_cast<Insn*>(view);
11034 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11035 insn = (insn & 1) | b;
11036 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11037 if (size == 32)
11038 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
11039 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11040 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
11041 else
11042 r_type = elfcpp::R_POWERPC_REL24;
11043 }
dd93cd0a 11044 Address addend = 0;
08be3224
AM
11045 if (!(size == 32
11046 && (r_type == elfcpp::R_PPC_PLTREL24
11047 || r_type == elfcpp::R_POWERPC_PLT16_LO
11048 || r_type == elfcpp::R_POWERPC_PLT16_HI
11049 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 11050 addend = rela.get_r_addend();
c9824451 11051 value = psymval->value(object, addend);
fa40fbe4 11052 unsigned int local_ent = 0;
32f59844 11053 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
11054 {
11055 if (target->abiversion() >= 2)
11056 {
11057 if (gsym != NULL)
fa40fbe4 11058 local_ent = object->ppc64_local_entry_offset(gsym);
9055360d 11059 else
fa40fbe4 11060 local_ent = object->ppc64_local_entry_offset(r_sym);
9055360d
AM
11061 }
11062 else
1611bc4a
AM
11063 {
11064 unsigned int dest_shndx;
11065 target->symval_for_branch(relinfo->symtab, gsym, object,
11066 &value, &dest_shndx);
11067 }
9055360d 11068 }
fa40fbe4
AM
11069 Address max_branch = max_branch_delta<size>(r_type);
11070 if (max_branch != 0
11071 && (value + local_ent - address + max_branch >= 2 * max_branch
32f59844
AM
11072 || (size == 64
11073 && r_type == elfcpp::R_PPC64_REL24_NOTOC
11074 && (gsym != NULL
11075 ? object->ppc64_needs_toc(gsym)
11076 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
11077 {
11078 Stub_table<size, big_endian>* stub_table
11079 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
11080 if (stub_table != NULL)
11081 {
32f59844
AM
11082 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11083 = stub_table->find_long_branch_entry(object, value);
11084 if (ent != NULL)
0cfb0717 11085 {
32f59844
AM
11086 if (ent->save_res_)
11087 value = (value - target->savres_section()->address()
11088 + stub_table->branch_size());
11089 else
afd2ea23
AM
11090 {
11091 value = (stub_table->stub_address()
11092 + stub_table->plt_size()
11093 + ent->off_);
11094 if (size == 64
11095 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
f1e05b19 11096 value += ent->tocoff_;
afd2ea23 11097 }
0cfb0717
AM
11098 has_stub_value = true;
11099 }
0cfdc767 11100 }
ec661b9d 11101 }
fa40fbe4
AM
11102 if (!has_stub_value)
11103 value += local_ent;
42cacb20
DE
11104 }
11105
42cacb20
DE
11106 switch (r_type)
11107 {
32f59844
AM
11108 case elfcpp::R_PPC64_REL24_NOTOC:
11109 if (size == 32)
11110 break;
11111 // Fall through.
dd93cd0a
AM
11112 case elfcpp::R_PPC64_REL64:
11113 case elfcpp::R_POWERPC_REL32:
11114 case elfcpp::R_POWERPC_REL24:
11115 case elfcpp::R_PPC_PLTREL24:
11116 case elfcpp::R_PPC_LOCAL24PC:
11117 case elfcpp::R_POWERPC_REL16:
11118 case elfcpp::R_POWERPC_REL16_LO:
11119 case elfcpp::R_POWERPC_REL16_HI:
11120 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 11121 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
11122 case elfcpp::R_PPC64_REL16_HIGH:
11123 case elfcpp::R_PPC64_REL16_HIGHA:
11124 case elfcpp::R_PPC64_REL16_HIGHER:
11125 case elfcpp::R_PPC64_REL16_HIGHERA:
11126 case elfcpp::R_PPC64_REL16_HIGHEST:
11127 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11128 case elfcpp::R_POWERPC_REL14:
11129 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11130 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11131 case elfcpp::R_PPC64_PCREL34:
11132 case elfcpp::R_PPC64_GOT_PCREL34:
11133 case elfcpp::R_PPC64_PLT_PCREL34:
11134 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11135 case elfcpp::R_PPC64_PCREL28:
87c69f97
AM
11136 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11137 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11138 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11139 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11140 case elfcpp::R_PPC64_REL16_HIGHER34:
11141 case elfcpp::R_PPC64_REL16_HIGHERA34:
11142 case elfcpp::R_PPC64_REL16_HIGHEST34:
11143 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
11144 value -= address;
11145 break;
11146
42cacb20
DE
11147 case elfcpp::R_PPC64_TOC16:
11148 case elfcpp::R_PPC64_TOC16_LO:
11149 case elfcpp::R_PPC64_TOC16_HI:
11150 case elfcpp::R_PPC64_TOC16_HA:
11151 case elfcpp::R_PPC64_TOC16_DS:
11152 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 11153 // Subtract the TOC base address.
c9269dff 11154 value -= (target->got_section()->output_section()->address()
dd93cd0a 11155 + object->toc_base_offset());
42cacb20
DE
11156 break;
11157
cf43a2fe
AM
11158 case elfcpp::R_POWERPC_SECTOFF:
11159 case elfcpp::R_POWERPC_SECTOFF_LO:
11160 case elfcpp::R_POWERPC_SECTOFF_HI:
11161 case elfcpp::R_POWERPC_SECTOFF_HA:
11162 case elfcpp::R_PPC64_SECTOFF_DS:
11163 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11164 if (os != NULL)
11165 value -= os->address();
42cacb20
DE
11166 break;
11167
dd93cd0a
AM
11168 case elfcpp::R_PPC64_TPREL16_DS:
11169 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
11170 case elfcpp::R_PPC64_TPREL16_HIGH:
11171 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 11172 if (size != 64)
f9c6b907 11173 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 11174 break;
d8e90251 11175 // Fall through.
dd93cd0a
AM
11176 case elfcpp::R_POWERPC_TPREL16:
11177 case elfcpp::R_POWERPC_TPREL16_LO:
11178 case elfcpp::R_POWERPC_TPREL16_HI:
11179 case elfcpp::R_POWERPC_TPREL16_HA:
11180 case elfcpp::R_POWERPC_TPREL:
11181 case elfcpp::R_PPC64_TPREL16_HIGHER:
11182 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11183 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11184 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 11185 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
11186 // tls symbol values are relative to tls_segment()->vaddr()
11187 value -= tp_offset;
11188 break;
11189
11190 case elfcpp::R_PPC64_DTPREL16_DS:
11191 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11192 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11193 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11194 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11195 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11196 if (size != 64)
11197 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11198 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11199 break;
d8e90251 11200 // Fall through.
dd93cd0a
AM
11201 case elfcpp::R_POWERPC_DTPREL16:
11202 case elfcpp::R_POWERPC_DTPREL16_LO:
11203 case elfcpp::R_POWERPC_DTPREL16_HI:
11204 case elfcpp::R_POWERPC_DTPREL16_HA:
11205 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
11206 case elfcpp::R_PPC64_DTPREL16_HIGH:
11207 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 11208 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
11209 // tls symbol values are relative to tls_segment()->vaddr()
11210 value -= dtp_offset;
11211 break;
11212
45965137
AM
11213 case elfcpp::R_PPC64_ADDR64_LOCAL:
11214 if (gsym != NULL)
11215 value += object->ppc64_local_entry_offset(gsym);
11216 else
11217 value += object->ppc64_local_entry_offset(r_sym);
11218 break;
11219
42cacb20
DE
11220 default:
11221 break;
11222 }
11223
dd93cd0a 11224 Insn branch_bit = 0;
42cacb20
DE
11225 switch (r_type)
11226 {
dd93cd0a
AM
11227 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11228 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11229 branch_bit = 1 << 21;
d8e90251 11230 // Fall through.
dd93cd0a
AM
11231 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11232 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11233 {
11234 Insn* iview = reinterpret_cast<Insn*>(view);
11235 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11236 insn &= ~(1 << 21);
11237 insn |= branch_bit;
11238 if (this->is_isa_v2)
11239 {
11240 // Set 'a' bit. This is 0b00010 in BO field for branch
11241 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11242 // for branch on CTR insns (BO == 1a00t or 1a01t).
11243 if ((insn & (0x14 << 21)) == (0x04 << 21))
11244 insn |= 0x02 << 21;
11245 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11246 insn |= 0x08 << 21;
11247 else
11248 break;
11249 }
11250 else
11251 {
11252 // Invert 'y' bit if not the default.
11253 if (static_cast<Signed_address>(value) < 0)
11254 insn ^= 1 << 21;
11255 }
11256 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11257 }
11258 break;
11259
08be3224
AM
11260 case elfcpp::R_POWERPC_PLT16_HA:
11261 if (size == 32
11262 && !parameters->options().output_is_position_independent())
11263 {
11264 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11265 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11266
11267 // Convert addis to lis.
11268 if ((insn & (0x3f << 26)) == 15u << 26
11269 && (insn & (0x1f << 16)) != 0)
11270 {
11271 insn &= ~(0x1f << 16);
11272 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11273 }
11274 }
11275 break;
11276
dd93cd0a
AM
11277 default:
11278 break;
11279 }
11280
252dcdf4
AM
11281 if (gsym
11282 ? relative_value_is_known(gsym)
11283 : relative_value_is_known(psymval))
aba6bc71 11284 {
0c951c25
AM
11285 Insn* iview;
11286 Insn* iview2;
11287 Insn insn;
11288 uint64_t pinsn, pinsn2;
11289
aba6bc71
AM
11290 switch (r_type)
11291 {
11292 default:
11293 break;
11294
5edad15d
AM
11295 // Multi-instruction sequences that access the GOT/TOC can
11296 // be optimized, eg.
11297 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11298 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11299 // and
11300 // addis ra,r2,0; addi rb,ra,x@toc@l;
11301 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
11302 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11303 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11304 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11305 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11306 case elfcpp::R_POWERPC_GOT16_HA:
11307 case elfcpp::R_PPC64_TOC16_HA:
252dcdf4 11308 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11309 {
0c951c25
AM
11310 iview = reinterpret_cast<Insn*>(view - d_offset);
11311 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
11312 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11313 && object->make_toc_relative(target, &value))
11314 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11315 && object->make_got_relative(target, psymval,
11316 rela.get_r_addend(),
11317 &value)))
5edad15d
AM
11318 {
11319 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11320 == ((15u << 26) | (2 << 16)));
11321 }
11322 if (((insn & ((0x3f << 26) | 0x1f << 16))
11323 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11324 && value + 0x8000 < 0x10000)
aba6bc71
AM
11325 {
11326 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11327 return true;
11328 }
11329 }
11330 break;
11331
11332 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11333 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11334 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11335 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11336 case elfcpp::R_POWERPC_GOT16_LO:
11337 case elfcpp::R_PPC64_GOT16_LO_DS:
11338 case elfcpp::R_PPC64_TOC16_LO:
11339 case elfcpp::R_PPC64_TOC16_LO_DS:
252dcdf4 11340 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11341 {
0c951c25
AM
11342 iview = reinterpret_cast<Insn*>(view - d_offset);
11343 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 11344 bool changed = false;
c9b8abb7
AM
11345 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11346 && object->make_toc_relative(target, &value))
11347 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11348 && object->make_got_relative(target, psymval,
11349 rela.get_r_addend(),
11350 &value)))
5edad15d
AM
11351 {
11352 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11353 insn ^= (14u << 26) ^ (58u << 26);
11354 r_type = elfcpp::R_PPC64_TOC16_LO;
11355 changed = true;
11356 }
11357 if (ok_lo_toc_insn(insn, r_type)
11358 && value + 0x8000 < 0x10000)
aba6bc71
AM
11359 {
11360 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11361 {
11362 // Transform addic to addi when we change reg.
11363 insn &= ~((0x3f << 26) | (0x1f << 16));
11364 insn |= (14u << 26) | (2 << 16);
11365 }
11366 else
11367 {
11368 insn &= ~(0x1f << 16);
11369 insn |= 2 << 16;
11370 }
5edad15d 11371 changed = true;
aba6bc71 11372 }
5edad15d
AM
11373 if (changed)
11374 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
11375 }
11376 break;
549dba71 11377
c9b8abb7 11378 case elfcpp::R_PPC64_GOT_PCREL34:
252dcdf4 11379 if (size == 64 && parameters->options().toc_optimize())
c9b8abb7 11380 {
0c951c25
AM
11381 iview = reinterpret_cast<Insn*>(view);
11382 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11383 pinsn <<= 32;
11384 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11385 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
11386 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11387 break;
11388
11389 Address relval = psymval->value(object, rela.get_r_addend());
11390 relval -= address;
11391 if (relval + (1ULL << 33) < 1ULL << 34)
11392 {
11393 value = relval;
11394 // Replace with paddi
0c951c25
AM
11395 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11396 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11397 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11398 pinsn & 0xffffffff);
11399 goto pcrelopt;
c9b8abb7
AM
11400 }
11401 }
11402 break;
11403
0c951c25 11404 case elfcpp::R_PPC64_PCREL34:
252dcdf4
AM
11405 if (size == 64)
11406 {
11407 iview = reinterpret_cast<Insn*>(view);
11408 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11409 pinsn <<= 32;
11410 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11411 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11412 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11413 | (14ULL << 26) /* paddi */))
11414 break;
0c951c25 11415
252dcdf4
AM
11416 pcrelopt:
11417 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11418 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11419 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11420 if (relnum >= reloc_count - 1)
11421 break;
0c951c25 11422
252dcdf4
AM
11423 Reltype next_rela(preloc + reloc_size);
11424 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11425 != elfcpp::R_PPC64_PCREL_OPT)
11426 || next_rela.get_r_offset() != rela.get_r_offset())
11427 break;
0c951c25 11428
252dcdf4
AM
11429 Address off = next_rela.get_r_addend();
11430 if (off == 0)
11431 off = 8; // zero means next insn.
11432 if (off + rela.get_r_offset() + 4 > view_size)
11433 break;
0c951c25 11434
252dcdf4
AM
11435 iview2 = reinterpret_cast<Insn*>(view + off);
11436 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11437 pinsn2 <<= 32;
11438 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11439 break;
11440 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11441 {
11442 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11443 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11444 pinsn & 0xffffffff);
11445 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11446 }
11447 }
0c951c25
AM
11448 break;
11449
9a23f96e 11450 case elfcpp::R_POWERPC_TPREL16_HA:
252dcdf4 11451 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11452 {
11453 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
252dcdf4
AM
11454 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11455 return true;
9a23f96e
AM
11456 }
11457 break;
11458
11459 case elfcpp::R_PPC64_TPREL16_LO_DS:
11460 if (size == 32)
11461 // R_PPC_TLSGD, R_PPC_TLSLD
11462 break;
11463 // Fall through.
11464 case elfcpp::R_POWERPC_TPREL16_LO:
252dcdf4 11465 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11466 {
11467 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11468 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11469 insn &= ~(0x1f << 16);
11470 insn |= (size == 32 ? 2 : 13) << 16;
11471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11472 }
11473 break;
11474
549dba71 11475 case elfcpp::R_PPC64_ENTRY:
252dcdf4 11476 if (size == 64)
549dba71 11477 {
252dcdf4
AM
11478 value = (target->got_section()->output_section()->address()
11479 + object->toc_base_offset());
11480 if (value + 0x80008000 <= 0xffffffff
11481 && !parameters->options().output_is_position_independent())
549dba71
AM
11482 {
11483 Insn* iview = reinterpret_cast<Insn*>(view);
11484 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11485 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11486
11487 if ((insn1 & ~0xfffc) == ld_2_12
11488 && insn2 == add_2_2_12)
11489 {
252dcdf4 11490 insn1 = lis_2 + ha(value);
549dba71
AM
11491 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11492 insn2 = addi_2_2 + l(value);
11493 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11494 return true;
11495 }
11496 }
252dcdf4
AM
11497 else
11498 {
11499 value -= address;
11500 if (value + 0x80008000 <= 0xffffffff)
11501 {
11502 Insn* iview = reinterpret_cast<Insn*>(view);
11503 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11504 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11505
11506 if ((insn1 & ~0xfffc) == ld_2_12
11507 && insn2 == add_2_2_12)
11508 {
11509 insn1 = addis_2_12 + ha(value);
11510 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11511 insn2 = addi_2_2 + l(value);
11512 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11513 return true;
11514 }
11515 }
11516 }
549dba71
AM
11517 }
11518 break;
e3a7574e
AM
11519
11520 case elfcpp::R_POWERPC_REL16_LO:
11521 // If we are generating a non-PIC executable, edit
11522 // 0: addis 2,12,.TOC.-0b@ha
11523 // addi 2,2,.TOC.-0b@l
11524 // used by ELFv2 global entry points to set up r2, to
11525 // lis 2,.TOC.@ha
11526 // addi 2,2,.TOC.@l
11527 // if .TOC. is in range. */
252dcdf4
AM
11528 if (size == 64
11529 && value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11530 && relnum + 1 > 1
e3a7574e
AM
11531 && preloc != NULL
11532 && target->abiversion() >= 2
11533 && !parameters->options().output_is_position_independent()
4f038ee5 11534 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11535 && gsym != NULL
11536 && strcmp(gsym->name(), ".TOC.") == 0)
11537 {
0e123f69 11538 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11539 Reltype prev_rela(preloc - reloc_size);
11540 if ((prev_rela.get_r_info()
11541 == elfcpp::elf_r_info<size>(r_sym,
11542 elfcpp::R_POWERPC_REL16_HA))
11543 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11544 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11545 {
dcfc7dd4 11546 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11547 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11548 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11549
11550 if ((insn1 & 0xffff0000) == addis_2_12
11551 && (insn2 & 0xffff0000) == addi_2_2)
11552 {
11553 insn1 = lis_2 + ha(value + address - 4);
11554 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11555 insn2 = addi_2_2 + l(value + address - 4);
11556 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11557 if (relinfo->rr)
11558 {
11559 relinfo->rr->set_strategy(relnum - 1,
11560 Relocatable_relocs::RELOC_SPECIAL);
11561 relinfo->rr->set_strategy(relnum,
11562 Relocatable_relocs::RELOC_SPECIAL);
11563 }
11564 return true;
11565 }
11566 }
11567 }
11568 break;
aba6bc71
AM
11569 }
11570 }
11571
f4baf0d4 11572 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11573 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11574 switch (r_type)
11575 {
11576 case elfcpp::R_POWERPC_ADDR32:
11577 case elfcpp::R_POWERPC_UADDR32:
11578 if (size == 64)
f4baf0d4 11579 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11580 break;
11581
11582 case elfcpp::R_POWERPC_REL32:
a680de9a 11583 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11584 if (size == 64)
f4baf0d4 11585 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11586 break;
11587
dd93cd0a 11588 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11589 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11590 break;
11591
b80eed39
AM
11592 case elfcpp::R_POWERPC_ADDR16:
11593 // We really should have three separate relocations,
11594 // one for 16-bit data, one for insns with 16-bit signed fields,
11595 // and one for insns with 16-bit unsigned fields.
11596 overflow = Reloc::CHECK_BITFIELD;
11597 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11598 overflow = Reloc::CHECK_LOW_INSN;
11599 break;
11600
f9c6b907
AM
11601 case elfcpp::R_POWERPC_ADDR16_HI:
11602 case elfcpp::R_POWERPC_ADDR16_HA:
11603 case elfcpp::R_POWERPC_GOT16_HI:
11604 case elfcpp::R_POWERPC_GOT16_HA:
11605 case elfcpp::R_POWERPC_PLT16_HI:
11606 case elfcpp::R_POWERPC_PLT16_HA:
11607 case elfcpp::R_POWERPC_SECTOFF_HI:
11608 case elfcpp::R_POWERPC_SECTOFF_HA:
11609 case elfcpp::R_PPC64_TOC16_HI:
11610 case elfcpp::R_PPC64_TOC16_HA:
11611 case elfcpp::R_PPC64_PLTGOT16_HI:
11612 case elfcpp::R_PPC64_PLTGOT16_HA:
11613 case elfcpp::R_POWERPC_TPREL16_HI:
11614 case elfcpp::R_POWERPC_TPREL16_HA:
11615 case elfcpp::R_POWERPC_DTPREL16_HI:
11616 case elfcpp::R_POWERPC_DTPREL16_HA:
11617 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11618 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11619 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11620 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11621 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11622 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11623 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11624 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11625 case elfcpp::R_POWERPC_REL16_HI:
11626 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11627 if (size != 32)
11628 overflow = Reloc::CHECK_HIGH_INSN;
11629 break;
11630
dd93cd0a
AM
11631 case elfcpp::R_POWERPC_REL16:
11632 case elfcpp::R_PPC64_TOC16:
11633 case elfcpp::R_POWERPC_GOT16:
11634 case elfcpp::R_POWERPC_SECTOFF:
11635 case elfcpp::R_POWERPC_TPREL16:
11636 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11637 case elfcpp::R_POWERPC_GOT_TLSGD16:
11638 case elfcpp::R_POWERPC_GOT_TLSLD16:
11639 case elfcpp::R_POWERPC_GOT_TPREL16:
11640 case elfcpp::R_POWERPC_GOT_DTPREL16:
11641 overflow = Reloc::CHECK_LOW_INSN;
11642 break;
11643
32f59844
AM
11644 case elfcpp::R_PPC64_REL24_NOTOC:
11645 if (size == 32)
11646 break;
11647 // Fall through.
b80eed39
AM
11648 case elfcpp::R_POWERPC_ADDR24:
11649 case elfcpp::R_POWERPC_ADDR14:
11650 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11651 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11652 case elfcpp::R_PPC64_ADDR16_DS:
11653 case elfcpp::R_POWERPC_REL24:
11654 case elfcpp::R_PPC_PLTREL24:
11655 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
11656 case elfcpp::R_PPC64_TPREL16_DS:
11657 case elfcpp::R_PPC64_DTPREL16_DS:
11658 case elfcpp::R_PPC64_TOC16_DS:
11659 case elfcpp::R_PPC64_GOT16_DS:
11660 case elfcpp::R_PPC64_SECTOFF_DS:
11661 case elfcpp::R_POWERPC_REL14:
11662 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11663 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11664 case elfcpp::R_PPC64_D34:
11665 case elfcpp::R_PPC64_PCREL34:
11666 case elfcpp::R_PPC64_GOT_PCREL34:
11667 case elfcpp::R_PPC64_PLT_PCREL34:
11668 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11669 case elfcpp::R_PPC64_D28:
11670 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
11671 case elfcpp::R_PPC64_TPREL34:
11672 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
11673 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11674 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11675 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11676 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
f4baf0d4 11677 overflow = Reloc::CHECK_SIGNED;
42cacb20 11678 break;
dd93cd0a 11679 }
42cacb20 11680
dcfc7dd4 11681 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
11682 Insn insn = 0;
11683
b80eed39
AM
11684 if (overflow == Reloc::CHECK_LOW_INSN
11685 || overflow == Reloc::CHECK_HIGH_INSN)
11686 {
a680de9a 11687 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 11688
a47622ac
AM
11689 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11690 overflow = Reloc::CHECK_BITFIELD;
11691 else if (overflow == Reloc::CHECK_LOW_INSN
11692 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11693 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11694 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11695 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11696 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11697 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 11698 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
11699 else
11700 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
11701 }
11702
a680de9a 11703 bool maybe_dq_reloc = false;
3ea0a085 11704 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 11705 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
11706 switch (r_type)
11707 {
11708 case elfcpp::R_POWERPC_NONE:
11709 case elfcpp::R_POWERPC_TLS:
11710 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11711 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
11712 case elfcpp::R_POWERPC_PLTSEQ:
11713 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
11714 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11715 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 11716 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
11717 break;
11718
11719 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 11720 case elfcpp::R_PPC64_REL64:
cf43a2fe 11721 case elfcpp::R_PPC64_TOC:
45965137 11722 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
11723 Reloc::addr64(view, value);
11724 break;
11725
11726 case elfcpp::R_POWERPC_TPREL:
11727 case elfcpp::R_POWERPC_DTPREL:
11728 if (size == 64)
11729 Reloc::addr64(view, value);
11730 else
3ea0a085 11731 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11732 break;
11733
11734 case elfcpp::R_PPC64_UADDR64:
11735 Reloc::addr64_u(view, value);
42cacb20
DE
11736 break;
11737
11738 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 11739 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11740 break;
11741
acc276d8 11742 case elfcpp::R_POWERPC_REL32:
dd93cd0a 11743 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 11744 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
11745 break;
11746
32f59844
AM
11747 case elfcpp::R_PPC64_REL24_NOTOC:
11748 if (size == 32)
11749 goto unsupp; // R_PPC_EMB_RELSDA
11750 // Fall through.
dd93cd0a
AM
11751 case elfcpp::R_POWERPC_ADDR24:
11752 case elfcpp::R_POWERPC_REL24:
11753 case elfcpp::R_PPC_PLTREL24:
11754 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 11755 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
11756 break;
11757
dd93cd0a
AM
11758 case elfcpp::R_POWERPC_GOT_DTPREL16:
11759 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
11760 case elfcpp::R_POWERPC_GOT_TPREL16:
11761 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
11762 if (size == 64)
11763 {
ec86f434 11764 // On ppc64 these are all ds form
a680de9a 11765 maybe_dq_reloc = true;
dd93cd0a
AM
11766 break;
11767 }
c25aa1e1 11768 // Fall through.
cf43a2fe 11769 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 11770 case elfcpp::R_POWERPC_REL16:
cf43a2fe 11771 case elfcpp::R_PPC64_TOC16:
42cacb20 11772 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 11773 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
11774 case elfcpp::R_POWERPC_TPREL16:
11775 case elfcpp::R_POWERPC_DTPREL16:
11776 case elfcpp::R_POWERPC_GOT_TLSGD16:
11777 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 11778 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 11779 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 11780 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 11781 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 11782 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 11783 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
11784 case elfcpp::R_POWERPC_TPREL16_LO:
11785 case elfcpp::R_POWERPC_DTPREL16_LO:
11786 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11787 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
11788 if (size == 64)
11789 status = Reloc::addr16(view, value, overflow);
11790 else
11791 maybe_dq_reloc = true;
dd93cd0a
AM
11792 break;
11793
11794 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 11795 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
11796 break;
11797
f9c6b907
AM
11798 case elfcpp::R_PPC64_ADDR16_HIGH:
11799 case elfcpp::R_PPC64_TPREL16_HIGH:
11800 case elfcpp::R_PPC64_DTPREL16_HIGH:
11801 if (size == 32)
11802 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11803 goto unsupp;
d8e90251 11804 // Fall through.
cf43a2fe 11805 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 11806 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 11807 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 11808 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 11809 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 11810 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 11811 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
11812 case elfcpp::R_POWERPC_TPREL16_HI:
11813 case elfcpp::R_POWERPC_DTPREL16_HI:
11814 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11815 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11816 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11817 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11818 Reloc::addr16_hi(view, value);
42cacb20
DE
11819 break;
11820
f9c6b907
AM
11821 case elfcpp::R_PPC64_ADDR16_HIGHA:
11822 case elfcpp::R_PPC64_TPREL16_HIGHA:
11823 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11824 if (size == 32)
11825 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11826 goto unsupp;
d8e90251 11827 // Fall through.
cf43a2fe 11828 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 11829 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 11830 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 11831 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 11832 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 11833 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 11834 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
11835 case elfcpp::R_POWERPC_TPREL16_HA:
11836 case elfcpp::R_POWERPC_DTPREL16_HA:
11837 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11838 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11839 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11840 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11841 Reloc::addr16_ha(view, value);
42cacb20
DE
11842 break;
11843
a680de9a
PB
11844 case elfcpp::R_POWERPC_REL16DX_HA:
11845 status = Reloc::addr16dx_ha(view, value, overflow);
11846 break;
11847
dd93cd0a
AM
11848 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11849 if (size == 32)
11850 // R_PPC_EMB_NADDR16_LO
11851 goto unsupp;
d8e90251 11852 // Fall through.
dd93cd0a 11853 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 11854 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
11855 case elfcpp::R_PPC64_TPREL16_HIGHER:
11856 Reloc::addr16_hi2(view, value);
42cacb20
DE
11857 break;
11858
dd93cd0a
AM
11859 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11860 if (size == 32)
11861 // R_PPC_EMB_NADDR16_HI
11862 goto unsupp;
d8e90251 11863 // Fall through.
dd93cd0a 11864 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 11865 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
11866 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11867 Reloc::addr16_ha2(view, value);
42cacb20
DE
11868 break;
11869
dd93cd0a
AM
11870 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11871 if (size == 32)
11872 // R_PPC_EMB_NADDR16_HA
11873 goto unsupp;
d8e90251 11874 // Fall through.
dd93cd0a 11875 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 11876 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
11877 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11878 Reloc::addr16_hi3(view, value);
42cacb20
DE
11879 break;
11880
dd93cd0a
AM
11881 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11882 if (size == 32)
11883 // R_PPC_EMB_SDAI16
11884 goto unsupp;
d8e90251 11885 // Fall through.
dd93cd0a 11886 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 11887 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11888 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11889 Reloc::addr16_ha3(view, value);
11890 break;
11891
11892 case elfcpp::R_PPC64_DTPREL16_DS:
11893 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11894 if (size == 32)
11895 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11896 goto unsupp;
d8e90251 11897 // Fall through.
dd93cd0a
AM
11898 case elfcpp::R_PPC64_TPREL16_DS:
11899 case elfcpp::R_PPC64_TPREL16_LO_DS:
11900 if (size == 32)
11901 // R_PPC_TLSGD, R_PPC_TLSLD
11902 break;
d8e90251 11903 // Fall through.
cf43a2fe
AM
11904 case elfcpp::R_PPC64_ADDR16_DS:
11905 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
11906 case elfcpp::R_PPC64_TOC16_DS:
11907 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
11908 case elfcpp::R_PPC64_GOT16_DS:
11909 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 11910 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
11911 case elfcpp::R_PPC64_SECTOFF_DS:
11912 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 11913 maybe_dq_reloc = true;
dd93cd0a
AM
11914 break;
11915
11916 case elfcpp::R_POWERPC_ADDR14:
11917 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11918 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11919 case elfcpp::R_POWERPC_REL14:
11920 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11921 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 11922 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
11923 break;
11924
11925 case elfcpp::R_POWERPC_COPY:
11926 case elfcpp::R_POWERPC_GLOB_DAT:
11927 case elfcpp::R_POWERPC_JMP_SLOT:
11928 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 11929 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
11930 case elfcpp::R_PPC64_JMP_IREL:
11931 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
11932 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11933 _("unexpected reloc %u in object file"),
11934 r_type);
11935 break;
11936
7e57d19e 11937 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 11938 if (size == 32)
7e57d19e 11939 // R_PPC_EMB_SDA21
dd93cd0a
AM
11940 goto unsupp;
11941 else
11942 {
7e57d19e
AM
11943 Symbol_location loc;
11944 loc.object = relinfo->object;
11945 loc.shndx = relinfo->data_shndx;
11946 loc.offset = rela.get_r_offset();
b0d0d02b
AM
11947 const Tocsave_loc *tocsave = target->tocsave_loc();
11948 if (tocsave->find(loc) != tocsave->end())
7e57d19e
AM
11949 {
11950 // If we've generated plt calls using this tocsave, then
11951 // the nop needs to be changed to save r2.
11952 Insn* iview = reinterpret_cast<Insn*>(view);
11953 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11954 elfcpp::Swap<32, big_endian>::
11955 writeval(iview, std_2_1 + target->stk_toc());
11956 }
dd93cd0a
AM
11957 }
11958 break;
11959
11960 case elfcpp::R_PPC_EMB_SDA2I16:
11961 case elfcpp::R_PPC_EMB_SDA2REL:
11962 if (size == 32)
11963 goto unsupp;
11964 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
11965 break;
11966
e4dff765
AM
11967 case elfcpp::R_PPC64_D34:
11968 case elfcpp::R_PPC64_D34_LO:
11969 case elfcpp::R_PPC64_PCREL34:
11970 case elfcpp::R_PPC64_GOT_PCREL34:
11971 case elfcpp::R_PPC64_PLT_PCREL34:
11972 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
11973 case elfcpp::R_PPC64_TPREL34:
11974 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
11975 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11976 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11977 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11978 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11979 if (size == 32)
11980 goto unsupp;
11981 status = Reloc::addr34(view, value, overflow);
11982 break;
11983
11984 case elfcpp::R_PPC64_D34_HI30:
11985 if (size == 32)
11986 goto unsupp;
11987 Reloc::addr34_hi(view, value);
11988 break;
11989
11990 case elfcpp::R_PPC64_D34_HA30:
11991 if (size == 32)
11992 goto unsupp;
11993 Reloc::addr34_ha(view, value);
11994 break;
11995
11996 case elfcpp::R_PPC64_D28:
11997 case elfcpp::R_PPC64_PCREL28:
11998 if (size == 32)
11999 goto unsupp;
12000 status = Reloc::addr28(view, value, overflow);
12001 break;
12002
12003 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12004 case elfcpp::R_PPC64_REL16_HIGHER34:
12005 if (size == 32)
12006 goto unsupp;
12007 Reloc::addr16_higher34(view, value);
12008 break;
12009
12010 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12011 case elfcpp::R_PPC64_REL16_HIGHERA34:
12012 if (size == 32)
12013 goto unsupp;
12014 Reloc::addr16_highera34(view, value);
12015 break;
12016
12017 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12018 case elfcpp::R_PPC64_REL16_HIGHEST34:
12019 if (size == 32)
12020 goto unsupp;
12021 Reloc::addr16_highest34(view, value);
12022 break;
12023
12024 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12025 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12026 if (size == 32)
12027 goto unsupp;
12028 Reloc::addr16_highesta34(view, value);
12029 break;
12030
dd93cd0a
AM
12031 case elfcpp::R_POWERPC_PLT32:
12032 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
12033 case elfcpp::R_PPC_SDAREL16:
12034 case elfcpp::R_POWERPC_ADDR30:
12035 case elfcpp::R_PPC64_PLT64:
12036 case elfcpp::R_PPC64_PLTREL64:
12037 case elfcpp::R_PPC64_PLTGOT16:
12038 case elfcpp::R_PPC64_PLTGOT16_LO:
12039 case elfcpp::R_PPC64_PLTGOT16_HI:
12040 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
12041 case elfcpp::R_PPC64_PLTGOT16_DS:
12042 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 12043 case elfcpp::R_PPC_TOC16:
42cacb20 12044 default:
dd93cd0a 12045 unsupp:
42cacb20
DE
12046 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12047 _("unsupported reloc %u"),
12048 r_type);
12049 break;
12050 }
a680de9a
PB
12051
12052 if (maybe_dq_reloc)
12053 {
12054 if (insn == 0)
12055 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12056
12057 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12058 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12059 && (insn & 3) == 1))
12060 status = Reloc::addr16_dq(view, value, overflow);
12061 else if (size == 64
12062 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12063 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12064 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12065 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12066 status = Reloc::addr16_ds(view, value, overflow);
12067 else
12068 status = Reloc::addr16(view, value, overflow);
12069 }
12070
0cfb0717 12071 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
12072 && (has_stub_value
12073 || !(gsym != NULL
282c9750 12074 && gsym->is_undefined()
32f59844 12075 && is_branch_reloc<size>(r_type))))
0cfb0717
AM
12076 {
12077 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12078 _("relocation overflow"));
12079 if (has_stub_value)
12080 gold_info(_("try relinking with a smaller --stub-group-size"));
12081 }
42cacb20
DE
12082
12083 return true;
12084}
12085
42cacb20
DE
12086// Relocate section data.
12087
12088template<int size, bool big_endian>
12089void
12090Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
12091 const Relocate_info<size, big_endian>* relinfo,
12092 unsigned int sh_type,
12093 const unsigned char* prelocs,
12094 size_t reloc_count,
12095 Output_section* output_section,
12096 bool needs_special_offset_handling,
12097 unsigned char* view,
c9269dff 12098 Address address,
d83ce4e3
AM
12099 section_size_type view_size,
12100 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
12101{
12102 typedef Target_powerpc<size, big_endian> Powerpc;
12103 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
12104 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12105 Powerpc_comdat_behavior;
4d625b70
CC
12106 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12107 Classify_reloc;
42cacb20
DE
12108
12109 gold_assert(sh_type == elfcpp::SHT_RELA);
12110
4d625b70
CC
12111 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12112 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
12113 relinfo,
12114 this,
12115 prelocs,
12116 reloc_count,
12117 output_section,
12118 needs_special_offset_handling,
12119 view,
12120 address,
364c7fa5
ILT
12121 view_size,
12122 reloc_symbol_changes);
42cacb20
DE
12123}
12124
4d625b70 12125template<int size, bool big_endian>
cf43a2fe 12126class Powerpc_scan_relocatable_reloc
42cacb20 12127{
cf43a2fe 12128public:
0e123f69
AM
12129 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12130 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
12131 static const int sh_type = elfcpp::SHT_RELA;
12132
12133 // Return the symbol referred to by the relocation.
12134 static inline unsigned int
12135 get_r_sym(const Reltype* reloc)
12136 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12137
12138 // Return the type of the relocation.
12139 static inline unsigned int
12140 get_r_type(const Reltype* reloc)
12141 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12142
cf43a2fe
AM
12143 // Return the strategy to use for a local symbol which is not a
12144 // section symbol, given the relocation type.
12145 inline Relocatable_relocs::Reloc_strategy
12146 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12147 {
12148 if (r_type == 0 && r_sym == 0)
12149 return Relocatable_relocs::RELOC_DISCARD;
12150 return Relocatable_relocs::RELOC_COPY;
12151 }
12152
12153 // Return the strategy to use for a local symbol which is a section
12154 // symbol, given the relocation type.
12155 inline Relocatable_relocs::Reloc_strategy
12156 local_section_strategy(unsigned int, Relobj*)
12157 {
12158 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12159 }
12160
12161 // Return the strategy to use for a global symbol, given the
12162 // relocation type, the object, and the symbol index.
12163 inline Relocatable_relocs::Reloc_strategy
12164 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12165 {
08be3224
AM
12166 if (size == 32
12167 && (r_type == elfcpp::R_PPC_PLTREL24
12168 || r_type == elfcpp::R_POWERPC_PLT16_LO
12169 || r_type == elfcpp::R_POWERPC_PLT16_HI
12170 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
12171 return Relocatable_relocs::RELOC_SPECIAL;
12172 return Relocatable_relocs::RELOC_COPY;
12173 }
12174};
42cacb20
DE
12175
12176// Scan the relocs during a relocatable link.
12177
12178template<int size, bool big_endian>
12179void
12180Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
12181 Symbol_table* symtab,
12182 Layout* layout,
12183 Sized_relobj_file<size, big_endian>* object,
12184 unsigned int data_shndx,
12185 unsigned int sh_type,
12186 const unsigned char* prelocs,
12187 size_t reloc_count,
12188 Output_section* output_section,
12189 bool needs_special_offset_handling,
12190 size_t local_symbol_count,
12191 const unsigned char* plocal_symbols,
12192 Relocatable_relocs* rr)
42cacb20 12193{
4d625b70
CC
12194 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12195
42cacb20
DE
12196 gold_assert(sh_type == elfcpp::SHT_RELA);
12197
4d625b70 12198 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
12199 symtab,
12200 layout,
12201 object,
12202 data_shndx,
12203 prelocs,
12204 reloc_count,
12205 output_section,
12206 needs_special_offset_handling,
12207 local_symbol_count,
12208 plocal_symbols,
12209 rr);
12210}
12211
4d625b70
CC
12212// Scan the relocs for --emit-relocs.
12213
12214template<int size, bool big_endian>
12215void
12216Target_powerpc<size, big_endian>::emit_relocs_scan(
12217 Symbol_table* symtab,
12218 Layout* layout,
12219 Sized_relobj_file<size, big_endian>* object,
12220 unsigned int data_shndx,
12221 unsigned int sh_type,
12222 const unsigned char* prelocs,
12223 size_t reloc_count,
12224 Output_section* output_section,
12225 bool needs_special_offset_handling,
12226 size_t local_symbol_count,
12227 const unsigned char* plocal_syms,
12228 Relocatable_relocs* rr)
12229{
12230 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12231 Classify_reloc;
12232 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12233 Emit_relocs_strategy;
12234
12235 gold_assert(sh_type == elfcpp::SHT_RELA);
12236
12237 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12238 symtab,
12239 layout,
12240 object,
12241 data_shndx,
12242 prelocs,
12243 reloc_count,
12244 output_section,
12245 needs_special_offset_handling,
12246 local_symbol_count,
12247 plocal_syms,
12248 rr);
12249}
12250
7404fe1b 12251// Emit relocations for a section.
dd93cd0a
AM
12252// This is a modified version of the function by the same name in
12253// target-reloc.h. Using relocate_special_relocatable for
12254// R_PPC_PLTREL24 would require duplication of the entire body of the
12255// loop, so we may as well duplicate the whole thing.
42cacb20
DE
12256
12257template<int size, bool big_endian>
12258void
7404fe1b 12259Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
12260 const Relocate_info<size, big_endian>* relinfo,
12261 unsigned int sh_type,
12262 const unsigned char* prelocs,
12263 size_t reloc_count,
12264 Output_section* output_section,
62fe925a 12265 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 12266 unsigned char*,
dd93cd0a 12267 Address view_address,
cf43a2fe 12268 section_size_type,
42cacb20
DE
12269 unsigned char* reloc_view,
12270 section_size_type reloc_view_size)
12271{
12272 gold_assert(sh_type == elfcpp::SHT_RELA);
12273
0e123f69
AM
12274 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12275 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12276 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
12277 // Offset from start of insn to d-field reloc.
12278 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
12279
12280 Powerpc_relobj<size, big_endian>* const object
12281 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12282 const unsigned int local_count = object->local_symbol_count();
12283 unsigned int got2_shndx = object->got2_shndx();
c9269dff 12284 Address got2_addend = 0;
cf43a2fe 12285 if (got2_shndx != 0)
c9269dff
AM
12286 {
12287 got2_addend = object->get_output_section_offset(got2_shndx);
12288 gold_assert(got2_addend != invalid_address);
12289 }
cf43a2fe 12290
033bfb73
CC
12291 const bool relocatable = parameters->options().relocatable();
12292
cf43a2fe 12293 unsigned char* pwrite = reloc_view;
7404fe1b 12294 bool zap_next = false;
cf43a2fe
AM
12295 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12296 {
91a65d2f 12297 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
12298 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12299 continue;
12300
12301 Reltype reloc(prelocs);
12302 Reltype_write reloc_write(pwrite);
12303
7404fe1b 12304 Address offset = reloc.get_r_offset();
cf43a2fe 12305 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
12306 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12307 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12308 const unsigned int orig_r_sym = r_sym;
12309 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12310 = reloc.get_r_addend();
12311 const Symbol* gsym = NULL;
12312
12313 if (zap_next)
12314 {
12315 // We could arrange to discard these and other relocs for
12316 // tls optimised sequences in the strategy methods, but for
12317 // now do as BFD ld does.
12318 r_type = elfcpp::R_POWERPC_NONE;
12319 zap_next = false;
12320 }
cf43a2fe
AM
12321
12322 // Get the new symbol index.
9215b98b 12323 Output_section* os = NULL;
cf43a2fe
AM
12324 if (r_sym < local_count)
12325 {
12326 switch (strategy)
12327 {
12328 case Relocatable_relocs::RELOC_COPY:
12329 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 12330 if (r_sym != 0)
dd93cd0a 12331 {
7404fe1b
AM
12332 r_sym = object->symtab_index(r_sym);
12333 gold_assert(r_sym != -1U);
dd93cd0a 12334 }
cf43a2fe
AM
12335 break;
12336
12337 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12338 {
12339 // We are adjusting a section symbol. We need to find
12340 // the symbol table index of the section symbol for
12341 // the output section corresponding to input section
12342 // in which this symbol is defined.
12343 gold_assert(r_sym < local_count);
12344 bool is_ordinary;
12345 unsigned int shndx =
12346 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12347 gold_assert(is_ordinary);
9215b98b 12348 os = object->output_section(shndx);
cf43a2fe
AM
12349 gold_assert(os != NULL);
12350 gold_assert(os->needs_symtab_index());
7404fe1b 12351 r_sym = os->symtab_index();
cf43a2fe
AM
12352 }
12353 break;
12354
12355 default:
12356 gold_unreachable();
12357 }
12358 }
12359 else
12360 {
7404fe1b 12361 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
12362 gold_assert(gsym != NULL);
12363 if (gsym->is_forwarder())
12364 gsym = relinfo->symtab->resolve_forwards(gsym);
12365
12366 gold_assert(gsym->has_symtab_index());
7404fe1b 12367 r_sym = gsym->symtab_index();
cf43a2fe
AM
12368 }
12369
12370 // Get the new offset--the location in the output section where
12371 // this relocation should be applied.
cf43a2fe 12372 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12373 offset += offset_in_output_section;
cf43a2fe
AM
12374 else
12375 {
c9269dff
AM
12376 section_offset_type sot_offset =
12377 convert_types<section_offset_type, Address>(offset);
cf43a2fe 12378 section_offset_type new_sot_offset =
c9269dff
AM
12379 output_section->output_offset(object, relinfo->data_shndx,
12380 sot_offset);
cf43a2fe 12381 gold_assert(new_sot_offset != -1);
7404fe1b 12382 offset = new_sot_offset;
cf43a2fe
AM
12383 }
12384
dd93cd0a
AM
12385 // In an object file, r_offset is an offset within the section.
12386 // In an executable or dynamic object, generated by
12387 // --emit-relocs, r_offset is an absolute address.
033bfb73 12388 if (!relocatable)
dd93cd0a 12389 {
7404fe1b 12390 offset += view_address;
dd93cd0a 12391 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12392 offset -= offset_in_output_section;
dd93cd0a
AM
12393 }
12394
cf43a2fe 12395 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12396 if (strategy == Relocatable_relocs::RELOC_COPY)
12397 ;
12398 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12399 {
7404fe1b 12400 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12401 addend = psymval->value(object, addend);
12402 // In a relocatable link, the symbol value is relative to
12403 // the start of the output section. For a non-relocatable
12404 // link, we need to adjust the addend.
12405 if (!relocatable)
12406 {
12407 gold_assert(os != NULL);
12408 addend -= os->address();
12409 }
cf43a2fe
AM
12410 }
12411 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12412 {
e3a7574e
AM
12413 if (size == 32)
12414 {
12415 if (addend >= 32768)
12416 addend += got2_addend;
12417 }
12418 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12419 {
12420 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12421 addend -= d_offset;
e3a7574e
AM
12422 }
12423 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12424 {
12425 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12426 addend -= d_offset + 4;
e3a7574e 12427 }
cf43a2fe
AM
12428 }
12429 else
12430 gold_unreachable();
12431
033bfb73 12432 if (!relocatable)
7404fe1b
AM
12433 {
12434 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12435 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12436 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12437 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12438 {
12439 // First instruction of a global dynamic sequence,
12440 // arg setup insn.
12441 const bool final = gsym == NULL || gsym->final_value_is_known();
12442 switch (this->optimize_tls_gd(final))
12443 {
12444 case tls::TLSOPT_TO_IE:
12445 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12446 - elfcpp::R_POWERPC_GOT_TLSGD16);
12447 break;
12448 case tls::TLSOPT_TO_LE:
12449 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12450 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12451 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12452 else
12453 {
12454 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12455 offset -= d_offset;
7404fe1b
AM
12456 }
12457 break;
12458 default:
12459 break;
12460 }
12461 }
12462 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12463 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12464 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12465 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12466 {
12467 // First instruction of a local dynamic sequence,
12468 // arg setup insn.
12469 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12470 {
12471 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12472 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12473 {
12474 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12475 const Output_section* os = relinfo->layout->tls_segment()
12476 ->first_section();
12477 gold_assert(os != NULL);
12478 gold_assert(os->needs_symtab_index());
12479 r_sym = os->symtab_index();
12480 addend = dtp_offset;
12481 }
12482 else
12483 {
12484 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12485 offset -= d_offset;
7404fe1b
AM
12486 }
12487 }
12488 }
12489 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12490 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12491 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12492 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12493 {
12494 // First instruction of initial exec sequence.
12495 const bool final = gsym == NULL || gsym->final_value_is_known();
12496 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12497 {
12498 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12499 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12500 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12501 else
12502 {
12503 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12504 offset -= d_offset;
7404fe1b
AM
12505 }
12506 }
12507 }
12508 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12509 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12510 {
12511 // Second instruction of a global dynamic sequence,
12512 // the __tls_get_addr call
12513 const bool final = gsym == NULL || gsym->final_value_is_known();
12514 switch (this->optimize_tls_gd(final))
12515 {
12516 case tls::TLSOPT_TO_IE:
12517 r_type = elfcpp::R_POWERPC_NONE;
12518 zap_next = true;
12519 break;
12520 case tls::TLSOPT_TO_LE:
12521 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12522 offset += d_offset;
7404fe1b
AM
12523 zap_next = true;
12524 break;
12525 default:
12526 break;
12527 }
12528 }
12529 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12530 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12531 {
12532 // Second instruction of a local dynamic sequence,
12533 // the __tls_get_addr call
12534 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12535 {
12536 const Output_section* os = relinfo->layout->tls_segment()
12537 ->first_section();
12538 gold_assert(os != NULL);
12539 gold_assert(os->needs_symtab_index());
12540 r_sym = os->symtab_index();
12541 addend = dtp_offset;
12542 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12543 offset += d_offset;
7404fe1b
AM
12544 zap_next = true;
12545 }
12546 }
12547 else if (r_type == elfcpp::R_POWERPC_TLS)
12548 {
12549 // Second instruction of an initial exec sequence
12550 const bool final = gsym == NULL || gsym->final_value_is_known();
12551 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12552 {
12553 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12554 offset += d_offset;
7404fe1b
AM
12555 }
12556 }
12557 }
12558
12559 reloc_write.put_r_offset(offset);
12560 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12561 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12562
12563 pwrite += reloc_size;
12564 }
12565
12566 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12567 == reloc_view_size);
42cacb20
DE
12568}
12569
ec661b9d 12570// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12571// treatment. This is how we support equality comparisons of function
12572// pointers across shared library boundaries, as described in the
12573// processor specific ABI supplement.
12574
12575template<int size, bool big_endian>
12576uint64_t
12577Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12578{
cf43a2fe
AM
12579 if (size == 32)
12580 {
12581 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12582 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12583 p != this->stub_tables_.end();
12584 ++p)
12585 {
7e57d19e
AM
12586 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12587 = (*p)->find_plt_call_entry(gsym);
12588 if (ent != NULL)
12589 return (*p)->stub_address() + ent->off_;
ec661b9d 12590 }
c9824451 12591 }
9055360d
AM
12592 else if (this->abiversion() >= 2)
12593 {
faa2211d
AM
12594 Address off = this->glink_section()->find_global_entry(gsym);
12595 if (off != invalid_address)
9055360d
AM
12596 return this->glink_section()->global_entry_address() + off;
12597 }
ec661b9d 12598 gold_unreachable();
c9824451
AM
12599}
12600
12601// Return the PLT address to use for a local symbol.
12602template<int size, bool big_endian>
12603uint64_t
12604Target_powerpc<size, big_endian>::do_plt_address_for_local(
12605 const Relobj* object,
12606 unsigned int symndx) const
12607{
12608 if (size == 32)
12609 {
12610 const Sized_relobj<size, big_endian>* relobj
12611 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12612 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12613 p != this->stub_tables_.end();
12614 ++p)
12615 {
7e57d19e
AM
12616 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12617 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12618 if (ent != NULL)
12619 return (*p)->stub_address() + ent->off_;
ec661b9d 12620 }
c9824451 12621 }
ec661b9d 12622 gold_unreachable();
c9824451
AM
12623}
12624
12625// Return the PLT address to use for a global symbol.
12626template<int size, bool big_endian>
12627uint64_t
12628Target_powerpc<size, big_endian>::do_plt_address_for_global(
12629 const Symbol* gsym) const
12630{
12631 if (size == 32)
12632 {
ec661b9d
AM
12633 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12634 p != this->stub_tables_.end();
12635 ++p)
12636 {
7e57d19e
AM
12637 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12638 = (*p)->find_plt_call_entry(gsym);
12639 if (ent != NULL)
12640 return (*p)->stub_address() + ent->off_;
ec661b9d 12641 }
cf43a2fe 12642 }
9055360d
AM
12643 else if (this->abiversion() >= 2)
12644 {
faa2211d
AM
12645 Address off = this->glink_section()->find_global_entry(gsym);
12646 if (off != invalid_address)
9055360d
AM
12647 return this->glink_section()->global_entry_address() + off;
12648 }
ec661b9d 12649 gold_unreachable();
42cacb20
DE
12650}
12651
bd73a62d
AM
12652// Return the offset to use for the GOT_INDX'th got entry which is
12653// for a local tls symbol specified by OBJECT, SYMNDX.
12654template<int size, bool big_endian>
12655int64_t
12656Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12657 const Relobj* object,
12658 unsigned int symndx,
12659 unsigned int got_indx) const
12660{
12661 const Powerpc_relobj<size, big_endian>* ppc_object
12662 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12663 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12664 {
12665 for (Got_type got_type = GOT_TYPE_TLSGD;
12666 got_type <= GOT_TYPE_TPREL;
12667 got_type = Got_type(got_type + 1))
12668 if (ppc_object->local_has_got_offset(symndx, got_type))
12669 {
12670 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12671 if (got_type == GOT_TYPE_TLSGD)
12672 off += size / 8;
12673 if (off == got_indx * (size / 8))
12674 {
12675 if (got_type == GOT_TYPE_TPREL)
12676 return -tp_offset;
12677 else
12678 return -dtp_offset;
12679 }
12680 }
12681 }
12682 gold_unreachable();
12683}
12684
12685// Return the offset to use for the GOT_INDX'th got entry which is
12686// for global tls symbol GSYM.
12687template<int size, bool big_endian>
12688int64_t
12689Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12690 Symbol* gsym,
12691 unsigned int got_indx) const
12692{
12693 if (gsym->type() == elfcpp::STT_TLS)
12694 {
12695 for (Got_type got_type = GOT_TYPE_TLSGD;
12696 got_type <= GOT_TYPE_TPREL;
12697 got_type = Got_type(got_type + 1))
12698 if (gsym->has_got_offset(got_type))
12699 {
12700 unsigned int off = gsym->got_offset(got_type);
12701 if (got_type == GOT_TYPE_TLSGD)
12702 off += size / 8;
12703 if (off == got_indx * (size / 8))
12704 {
12705 if (got_type == GOT_TYPE_TPREL)
12706 return -tp_offset;
12707 else
12708 return -dtp_offset;
12709 }
12710 }
12711 }
12712 gold_unreachable();
12713}
12714
42cacb20
DE
12715// The selector for powerpc object files.
12716
12717template<int size, bool big_endian>
12718class Target_selector_powerpc : public Target_selector
12719{
12720public:
12721 Target_selector_powerpc()
edc27beb
AM
12722 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12723 size, big_endian,
03ef7571
ILT
12724 (size == 64
12725 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12726 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12727 (size == 64
12728 ? (big_endian ? "elf64ppc" : "elf64lppc")
12729 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
12730 { }
12731
2e702c99
RM
12732 virtual Target*
12733 do_instantiate_target()
7f055c20 12734 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
12735};
12736
12737Target_selector_powerpc<32, true> target_selector_ppc32;
12738Target_selector_powerpc<32, false> target_selector_ppc32le;
12739Target_selector_powerpc<64, true> target_selector_ppc64;
12740Target_selector_powerpc<64, false> target_selector_ppc64le;
12741
decdd3bc
AM
12742// Instantiate these constants for -O0
12743template<int size, bool big_endian>
9055360d
AM
12744const typename Output_data_glink<size, big_endian>::Address
12745 Output_data_glink<size, big_endian>::invalid_address;
12746template<int size, bool big_endian>
decdd3bc
AM
12747const typename Stub_table<size, big_endian>::Address
12748 Stub_table<size, big_endian>::invalid_address;
12749template<int size, bool big_endian>
12750const typename Target_powerpc<size, big_endian>::Address
12751 Target_powerpc<size, big_endian>::invalid_address;
12752
42cacb20 12753} // End anonymous namespace.