gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
b3adc24a 3// Copyright (C) 2008-2020 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_(),
7c1f4227 650 power10_stubs_(false), plt_thread_safe_(false), plt_localentry0_(false),
7ee7ff70 651 plt_localentry0_init_(false), has_localentry0_(false),
34e0882b 652 has_tls_get_addr_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
759 const Tocsave_loc
760 tocsave_loc() const
761 {
762 return this->tocsave_loc_;
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
e4dff765 1081 bool
7c1f4227
AM
1082 power10_stubs() const
1083 { return this->power10_stubs_; }
e4dff765
AM
1084
1085 void
7c1f4227 1086 set_power10_stubs()
e4dff765 1087 {
7c1f4227 1088 this->power10_stubs_ = true;
e4dff765
AM
1089 }
1090
9e69ed50
AM
1091 bool
1092 plt_thread_safe() const
1093 { return this->plt_thread_safe_; }
1094
7ee7ff70
AM
1095 bool
1096 plt_localentry0() const
1097 { return this->plt_localentry0_; }
1098
1099 void
1100 set_has_localentry0()
1101 {
1102 this->has_localentry0_ = true;
1103 }
1104
1105 bool
1106 is_elfv2_localentry0(const Symbol* gsym) const
1107 {
1108 return (size == 64
1109 && this->abiversion() >= 2
1110 && this->plt_localentry0()
1111 && gsym->type() == elfcpp::STT_FUNC
1112 && gsym->is_defined()
565ed01a
AM
1113 && gsym->nonvis() >> 3 == 0
1114 && !gsym->non_zero_localentry());
7ee7ff70
AM
1115 }
1116
1117 bool
1118 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1119 unsigned int r_sym) const
1120 {
1121 const Powerpc_relobj<size, big_endian>* ppc_object
1122 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1123
1124 if (size == 64
1125 && this->abiversion() >= 2
1126 && this->plt_localentry0()
1127 && ppc_object->st_other(r_sym) >> 5 == 0)
1128 {
1129 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1130 bool is_ordinary;
1131 if (!psymval->is_ifunc_symbol()
1132 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1133 && is_ordinary)
1134 return true;
1135 }
1136 return false;
1137 }
1138
565ed01a
AM
1139 // Remember any symbols seen with non-zero localentry, even those
1140 // not providing a definition
1141 bool
1142 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1143 const char*)
1144 {
1145 if (size == 64)
1146 {
1147 unsigned char st_other = sym.get_st_other();
1148 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1149 to->set_non_zero_localentry();
1150 }
1151 // We haven't resolved anything, continue normal processing.
1152 return false;
1153 }
1154
b4f7960d 1155 int
aacb3b6d 1156 abiversion() const
b4f7960d
AM
1157 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1158
1159 void
aacb3b6d 1160 set_abiversion(int ver)
b4f7960d
AM
1161 {
1162 elfcpp::Elf_Word flags = this->processor_specific_flags();
1163 flags &= ~elfcpp::EF_PPC64_ABI;
1164 flags |= ver & elfcpp::EF_PPC64_ABI;
1165 this->set_processor_specific_flags(flags);
1166 }
1167
34e0882b
AM
1168 Symbol*
1169 tls_get_addr_opt() const
1170 { return this->tls_get_addr_opt_; }
1171
1172 Symbol*
1173 tls_get_addr() const
1174 { return this->tls_get_addr_; }
1175
1176 // If optimizing __tls_get_addr calls, whether this is the
1177 // "__tls_get_addr" symbol.
1178 bool
1179 is_tls_get_addr_opt(const Symbol* gsym) const
1180 {
1181 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1182 || gsym == this->tls_get_addr_opt_);
1183 }
1184
1185 bool
1186 replace_tls_get_addr(const Symbol* gsym) const
1187 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1188
1189 void
1190 set_has_tls_get_addr_opt()
1191 { this->has_tls_get_addr_opt_ = true; }
1192
aacb3b6d 1193 // Offset to toc save stack slot
b4f7960d 1194 int
aacb3b6d 1195 stk_toc() const
b4f7960d
AM
1196 { return this->abiversion() < 2 ? 40 : 24; }
1197
34e0882b
AM
1198 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1199 // so use the CR save slot. Used only by __tls_get_addr call stub,
1200 // relying on __tls_get_addr not saving CR itself.
1201 int
1202 stk_linker() const
1203 { return this->abiversion() < 2 ? 32 : 8; }
1204
724436fc
AM
1205 // Merge object attributes from input object with those in the output.
1206 void
6f3fe02b 1207 merge_object_attributes(const Object*, const Attributes_section_data*);
724436fc 1208
42cacb20
DE
1209 private:
1210
e3deeb9c
AM
1211 class Track_tls
1212 {
1213 public:
1214 enum Tls_get_addr
1215 {
1216 NOT_EXPECTED = 0,
1217 EXPECTED = 1,
1218 SKIP = 2,
1219 NORMAL = 3
1220 };
1221
1222 Track_tls()
aacb3b6d 1223 : tls_get_addr_state_(NOT_EXPECTED),
e3deeb9c
AM
1224 relinfo_(NULL), relnum_(0), r_offset_(0)
1225 { }
1226
1227 ~Track_tls()
1228 {
aacb3b6d 1229 if (this->tls_get_addr_state_ != NOT_EXPECTED)
e3deeb9c
AM
1230 this->missing();
1231 }
1232
1233 void
1234 missing(void)
1235 {
1236 if (this->relinfo_ != NULL)
1237 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1238 _("missing expected __tls_get_addr call"));
1239 }
1240
1241 void
1242 expect_tls_get_addr_call(
1243 const Relocate_info<size, big_endian>* relinfo,
1244 size_t relnum,
1245 Address r_offset)
1246 {
aacb3b6d 1247 this->tls_get_addr_state_ = EXPECTED;
e3deeb9c
AM
1248 this->relinfo_ = relinfo;
1249 this->relnum_ = relnum;
1250 this->r_offset_ = r_offset;
1251 }
1252
1253 void
1254 expect_tls_get_addr_call()
aacb3b6d 1255 { this->tls_get_addr_state_ = EXPECTED; }
e3deeb9c
AM
1256
1257 void
1258 skip_next_tls_get_addr_call()
aacb3b6d 1259 {this->tls_get_addr_state_ = SKIP; }
e3deeb9c
AM
1260
1261 Tls_get_addr
34e0882b
AM
1262 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1263 unsigned int r_type, const Symbol* gsym)
e3deeb9c 1264 {
32f59844
AM
1265 bool is_tls_call
1266 = ((r_type == elfcpp::R_POWERPC_REL24
1267 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1268 || r_type == elfcpp::R_PPC_PLTREL24
1269 || is_plt16_reloc<size>(r_type)
e4dff765
AM
1270 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1271 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
32f59844
AM
1272 || r_type == elfcpp::R_POWERPC_PLTSEQ
1273 || r_type == elfcpp::R_POWERPC_PLTCALL
1274 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1275 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1276 && gsym != NULL
1277 && (gsym == target->tls_get_addr()
1278 || gsym == target->tls_get_addr_opt()));
aacb3b6d
AM
1279 Tls_get_addr last_tls = this->tls_get_addr_state_;
1280 this->tls_get_addr_state_ = NOT_EXPECTED;
e3deeb9c
AM
1281 if (is_tls_call && last_tls != EXPECTED)
1282 return last_tls;
1283 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1284 {
1285 this->missing();
1286 return EXPECTED;
1287 }
1288 return NORMAL;
1289 }
1290
1291 private:
1292 // What we're up to regarding calls to __tls_get_addr.
1293 // On powerpc, the branch and link insn making a call to
1294 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1295 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
32f59844 1296 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
e3deeb9c
AM
1297 // The marker relocation always comes first, and has the same
1298 // symbol as the reloc on the insn setting up the __tls_get_addr
1299 // argument. This ties the arg setup insn with the call insn,
1300 // allowing ld to safely optimize away the call. We check that
1301 // every call to __tls_get_addr has a marker relocation, and that
1302 // every marker relocation is on a call to __tls_get_addr.
aacb3b6d 1303 Tls_get_addr tls_get_addr_state_;
e3deeb9c
AM
1304 // Info about the last reloc for error message.
1305 const Relocate_info<size, big_endian>* relinfo_;
1306 size_t relnum_;
1307 Address r_offset_;
1308 };
1309
42cacb20 1310 // The class which scans relocations.
e3deeb9c 1311 class Scan : protected Track_tls
42cacb20
DE
1312 {
1313 public:
bfdfa4cd
AM
1314 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1315
42cacb20 1316 Scan()
e3deeb9c 1317 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1318 { }
1319
95a2c8d6 1320 static inline int
88b8e639 1321 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1322
42cacb20 1323 inline void
ad0f2072 1324 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1325 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1326 unsigned int data_shndx,
1327 Output_section* output_section,
1328 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1329 const elfcpp::Sym<size, big_endian>& lsym,
1330 bool is_discarded);
42cacb20
DE
1331
1332 inline void
ad0f2072 1333 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1334 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1335 unsigned int data_shndx,
1336 Output_section* output_section,
1337 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1338 Symbol* gsym);
1339
21bb3914
ST
1340 inline bool
1341 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1342 Target_powerpc* ,
f6971787 1343 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1344 unsigned int ,
2e702c99
RM
1345 Output_section* ,
1346 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1347 unsigned int r_type,
2e702c99 1348 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1349 {
1350 // PowerPC64 .opd is not folded, so any identical function text
1351 // may be folded and we'll still keep function addresses distinct.
1352 // That means no reloc is of concern here.
1353 if (size == 64)
f6971787
AM
1354 {
1355 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1356 <Powerpc_relobj<size, big_endian>*>(relobj);
1357 if (ppcobj->abiversion() == 1)
1358 return false;
1359 }
1360 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155 1361 // function code might be taking the address of the function.
32f59844 1362 return !is_branch_reloc<size>(r_type);
4d9aa155 1363 }
21bb3914
ST
1364
1365 inline bool
1366 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1367 Target_powerpc* ,
f6971787 1368 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1369 unsigned int ,
1370 Output_section* ,
4d9aa155
AM
1371 const elfcpp::Rela<size, big_endian>& ,
1372 unsigned int r_type,
1373 Symbol*)
1374 {
1375 // As above.
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 }
32f59844 1383 return !is_branch_reloc<size>(r_type);
4d9aa155 1384 }
21bb3914 1385
b3ccdeb5 1386 static bool
9055360d
AM
1387 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1388 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1389 unsigned int r_type, bool report_err);
1390
42cacb20
DE
1391 private:
1392 static void
6fa2a40b 1393 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1394 unsigned int r_type);
1395
1396 static void
6fa2a40b 1397 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1398 unsigned int r_type, Symbol*);
1399
1400 static void
1401 generate_tls_call(Symbol_table* symtab, Layout* layout,
1402 Target_powerpc* target);
1403
1404 void
1405 check_non_pic(Relobj*, unsigned int r_type);
1406
1407 // Whether we have issued an error about a non-PIC compilation.
1408 bool issued_non_pic_error_;
1409 };
1410
1611bc4a
AM
1411 bool
1412 symval_for_branch(const Symbol_table* symtab,
6c77229c 1413 const Sized_symbol<size>* gsym,
3ea0a085 1414 Powerpc_relobj<size, big_endian>* object,
1611bc4a 1415 Address *value, unsigned int *dest_shndx);
3ea0a085 1416
42cacb20 1417 // The class which implements relocation.
e3deeb9c 1418 class Relocate : protected Track_tls
42cacb20
DE
1419 {
1420 public:
dd93cd0a
AM
1421 // Use 'at' branch hints when true, 'y' when false.
1422 // FIXME maybe: set this with an option.
1423 static const bool is_isa_v2 = true;
1424
dd93cd0a 1425 Relocate()
e3deeb9c 1426 : Track_tls()
dd93cd0a
AM
1427 { }
1428
42cacb20
DE
1429 // Do a relocation. Return false if the caller should not issue
1430 // any warnings about this relocation.
1431 inline bool
91a65d2f
AM
1432 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1433 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1434 const Sized_symbol<size>*, const Symbol_value<size>*,
1435 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1436 section_size_type);
42cacb20
DE
1437 };
1438
168a4726
AM
1439 class Relocate_comdat_behavior
1440 {
1441 public:
1442 // Decide what the linker should do for relocations that refer to
1443 // discarded comdat sections.
1444 inline Comdat_behavior
1445 get(const char* name)
1446 {
1447 gold::Default_comdat_behavior default_behavior;
1448 Comdat_behavior ret = default_behavior.get(name);
43193fe9 1449 if (ret == CB_ERROR)
168a4726
AM
1450 {
1451 if (size == 32
1452 && (strcmp(name, ".fixup") == 0
1453 || strcmp(name, ".got2") == 0))
1454 ret = CB_IGNORE;
1455 if (size == 64
1456 && (strcmp(name, ".opd") == 0
1457 || strcmp(name, ".toc") == 0
1458 || strcmp(name, ".toc1") == 0))
1459 ret = CB_IGNORE;
1460 }
1461 return ret;
1462 }
1463 };
1464
dd93cd0a
AM
1465 // Optimize the TLS relocation type based on what we know about the
1466 // symbol. IS_FINAL is true if the final address of this symbol is
1467 // known at link time.
1468
1469 tls::Tls_optimization
1470 optimize_tls_gd(bool is_final)
1471 {
1472 // If we are generating a shared library, then we can't do anything
1473 // in the linker.
aacb3b6d
AM
1474 if (parameters->options().shared()
1475 || !parameters->options().tls_optimize())
dd93cd0a
AM
1476 return tls::TLSOPT_NONE;
1477
1478 if (!is_final)
1479 return tls::TLSOPT_TO_IE;
1480 return tls::TLSOPT_TO_LE;
1481 }
1482
1483 tls::Tls_optimization
1484 optimize_tls_ld()
1485 {
aacb3b6d
AM
1486 if (parameters->options().shared()
1487 || !parameters->options().tls_optimize())
dd93cd0a
AM
1488 return tls::TLSOPT_NONE;
1489
1490 return tls::TLSOPT_TO_LE;
1491 }
1492
1493 tls::Tls_optimization
1494 optimize_tls_ie(bool is_final)
1495 {
aacb3b6d
AM
1496 if (!is_final
1497 || parameters->options().shared()
1498 || !parameters->options().tls_optimize())
dd93cd0a
AM
1499 return tls::TLSOPT_NONE;
1500
1501 return tls::TLSOPT_TO_LE;
1502 }
cf43a2fe 1503
cf43a2fe
AM
1504 // Create glink.
1505 void
1506 make_glink_section(Layout*);
42cacb20 1507
cf43a2fe
AM
1508 // Create the PLT section.
1509 void
40b469d7 1510 make_plt_section(Symbol_table*, Layout*);
42cacb20 1511
e5d5f5ed 1512 void
40b469d7 1513 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1514
2d7ad24e
AM
1515 void
1516 make_lplt_section(Layout*);
1517
ec661b9d
AM
1518 void
1519 make_brlt_section(Layout*);
1520
42cacb20
DE
1521 // Create a PLT entry for a global symbol.
1522 void
ec661b9d 1523 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1524
1525 // Create a PLT entry for a local IFUNC symbol.
1526 void
40b469d7 1527 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1528 Sized_relobj_file<size, big_endian>*,
1529 unsigned int);
1530
2d7ad24e
AM
1531 // Create a PLT entry for a local non-IFUNC symbol.
1532 void
1533 make_local_plt_entry(Layout*,
1534 Sized_relobj_file<size, big_endian>*,
1535 unsigned int);
1536
42cacb20 1537
dd93cd0a
AM
1538 // Create a GOT entry for local dynamic __tls_get_addr.
1539 unsigned int
1540 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1541 Sized_relobj_file<size, big_endian>* object);
1542
42cacb20 1543 unsigned int
dd93cd0a
AM
1544 tlsld_got_offset() const
1545 {
1546 return this->tlsld_got_offset_;
1547 }
42cacb20 1548
42cacb20
DE
1549 // Get the dynamic reloc section, creating it if necessary.
1550 Reloc_section*
1551 rela_dyn_section(Layout*);
1552
b3ccdeb5
AM
1553 // Similarly, but for ifunc symbols get the one for ifunc.
1554 Reloc_section*
1555 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1556
42cacb20
DE
1557 // Copy a relocation against a global symbol.
1558 void
ef9beddf 1559 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1560 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1561 unsigned int shndx, Output_section* output_section,
1562 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1563 {
859d7987 1564 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1565 this->copy_relocs_.copy_reloc(symtab, layout,
1566 symtab->get_sized_symbol<size>(sym),
1567 object, shndx, output_section,
859d7987
CC
1568 r_type, reloc.get_r_offset(),
1569 reloc.get_r_addend(),
1570 this->rela_dyn_section(layout));
42cacb20
DE
1571 }
1572
0cfdc767 1573 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1574 void
a3e60ddb 1575 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1576
1577 // Sort output sections by address.
1578 struct Sort_sections
1579 {
1580 bool
1581 operator()(const Output_section* sec1, const Output_section* sec2)
1582 { return sec1->address() < sec2->address(); }
1583 };
1584
1585 class Branch_info
1586 {
1587 public:
1588 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1589 unsigned int data_shndx,
1590 Address r_offset,
1591 unsigned int r_type,
1592 unsigned int r_sym,
1593 Address addend)
1594 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1595 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1596 { }
1597
1598 ~Branch_info()
1599 { }
1600
7e57d19e
AM
1601 // Return whether this branch is going via a plt call stub, and if
1602 // so, mark it as having an R_PPC64_TOCSAVE.
1603 bool
1604 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1605 unsigned int shndx, Address offset,
1606 Target_powerpc* target, Symbol_table* symtab);
1607
ec661b9d 1608 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1609 bool
ec661b9d
AM
1610 make_stub(Stub_table<size, big_endian>*,
1611 Stub_table<size, big_endian>*,
1612 Symbol_table*) const;
1613
1614 private:
1615 // The branch location..
1616 Powerpc_relobj<size, big_endian>* object_;
1617 unsigned int shndx_;
1618 Address offset_;
1619 // ..and the branch type and destination.
7e57d19e
AM
1620 unsigned int r_type_ : 31;
1621 unsigned int tocsave_ : 1;
ec661b9d
AM
1622 unsigned int r_sym_;
1623 Address addend_;
1624 };
1625
42cacb20
DE
1626 // Information about this specific target which we pass to the
1627 // general Target structure.
1628 static Target::Target_info powerpc_info;
1629
1630 // The types of GOT entries needed for this platform.
0e70b911
CC
1631 // These values are exposed to the ABI in an incremental link.
1632 // Do not renumber existing values without changing the version
1633 // number of the .gnu_incremental_inputs section.
42cacb20
DE
1634 enum Got_type
1635 {
dd93cd0a
AM
1636 GOT_TYPE_STANDARD,
1637 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1638 GOT_TYPE_DTPREL, // entry for @got@dtprel
1639 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
1640 };
1641
ec661b9d 1642 // The GOT section.
cf43a2fe 1643 Output_data_got_powerpc<size, big_endian>* got_;
b3ccdeb5
AM
1644 // The PLT section. This is a container for a table of addresses,
1645 // and their relocations. Each address in the PLT has a dynamic
1646 // relocation (R_*_JMP_SLOT) and each address will have a
1647 // corresponding entry in .glink for lazy resolution of the PLT.
1648 // ppc32 initialises the PLT to point at the .glink entry, while
1649 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1650 // linker adds a stub that loads the PLT entry into ctr then
1651 // branches to ctr. There may be more than one stub for each PLT
1652 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1653 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1654 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1655 // The IPLT section. Like plt_, this is a container for a table of
1656 // addresses and their relocations, specifically for STT_GNU_IFUNC
1657 // functions that resolve locally (STT_GNU_IFUNC functions that
1658 // don't resolve locally go in PLT). Unlike plt_, these have no
1659 // entry in .glink for lazy resolution, and the relocation section
1660 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1661 // the relocation section may contain relocations against
1662 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1663 // relocation section will appear at the end of other dynamic
1664 // relocations, so that ld.so applies these relocations after other
1665 // dynamic relocations. In a static executable, the relocation
1666 // section is emitted and marked with __rela_iplt_start and
1667 // __rela_iplt_end symbols.
e5d5f5ed 1668 Output_data_plt_powerpc<size, big_endian>* iplt_;
2d7ad24e
AM
1669 // A PLT style section for local, non-ifunc symbols
1670 Output_data_plt_powerpc<size, big_endian>* lplt_;
ec661b9d
AM
1671 // Section holding long branch destinations.
1672 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1673 // The .glink section.
cf43a2fe 1674 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1675 // The dynamic reloc section.
42cacb20
DE
1676 Reloc_section* rela_dyn_;
1677 // Relocs saved to avoid a COPY reloc.
5edad15d 1678 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1679 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1680 unsigned int tlsld_got_offset_;
ec661b9d
AM
1681
1682 Stub_tables stub_tables_;
1683 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1684 Branch_lookup_table branch_lookup_table_;
1685
1686 typedef std::vector<Branch_info> Branches;
1687 Branches branch_info_;
7e57d19e 1688 Tocsave_loc tocsave_loc_;
9e69ed50 1689
7c1f4227 1690 bool power10_stubs_;
9e69ed50 1691 bool plt_thread_safe_;
7ee7ff70
AM
1692 bool plt_localentry0_;
1693 bool plt_localentry0_init_;
1694 bool has_localentry0_;
34e0882b 1695 bool has_tls_get_addr_opt_;
a3e60ddb
AM
1696
1697 bool relax_failed_;
1698 int relax_fail_count_;
1699 int32_t stub_group_size_;
d49044c7
AM
1700
1701 Output_data_save_res<size, big_endian> *savres_section_;
34e0882b
AM
1702
1703 // The "__tls_get_addr" symbol, if present
1704 Symbol* tls_get_addr_;
1705 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1706 Symbol* tls_get_addr_opt_;
724436fc
AM
1707
1708 // Attributes in output.
1709 Attributes_section_data* attributes_section_data_;
1710
1711 // Last input file to change various attribute tags
1712 const char* last_fp_;
1713 const char* last_ld_;
1714 const char* last_vec_;
1715 const char* last_struct_;
42cacb20
DE
1716};
1717
1718template<>
1719Target::Target_info Target_powerpc<32, true>::powerpc_info =
1720{
1721 32, // size
1722 true, // is_big_endian
1723 elfcpp::EM_PPC, // machine_code
1724 false, // has_make_symbol
1725 false, // has_resolve
1726 false, // has_code_fill
1727 true, // is_default_stack_executable
b3ce541e 1728 false, // can_icf_inline_merge_sections
42cacb20
DE
1729 '\0', // wrap_char
1730 "/usr/lib/ld.so.1", // dynamic_linker
1731 0x10000000, // default_text_segment_address
1732 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1733 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1734 false, // isolate_execinstr
1735 0, // rosegment_gap
8a5e3e08
ILT
1736 elfcpp::SHN_UNDEF, // small_common_shndx
1737 elfcpp::SHN_UNDEF, // large_common_shndx
1738 0, // small_common_section_flags
05a352e6
DK
1739 0, // large_common_section_flags
1740 NULL, // attributes_section
a67858e0 1741 NULL, // attributes_vendor
8d9743bd
MK
1742 "_start", // entry_symbol_name
1743 32, // hash_entry_size
bce5a025 1744 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1745};
1746
1747template<>
1748Target::Target_info Target_powerpc<32, false>::powerpc_info =
1749{
1750 32, // size
1751 false, // is_big_endian
1752 elfcpp::EM_PPC, // machine_code
1753 false, // has_make_symbol
1754 false, // has_resolve
1755 false, // has_code_fill
1756 true, // is_default_stack_executable
b3ce541e 1757 false, // can_icf_inline_merge_sections
42cacb20
DE
1758 '\0', // wrap_char
1759 "/usr/lib/ld.so.1", // dynamic_linker
1760 0x10000000, // default_text_segment_address
1761 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1762 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1763 false, // isolate_execinstr
1764 0, // rosegment_gap
8a5e3e08
ILT
1765 elfcpp::SHN_UNDEF, // small_common_shndx
1766 elfcpp::SHN_UNDEF, // large_common_shndx
1767 0, // small_common_section_flags
05a352e6
DK
1768 0, // large_common_section_flags
1769 NULL, // attributes_section
a67858e0 1770 NULL, // attributes_vendor
8d9743bd
MK
1771 "_start", // entry_symbol_name
1772 32, // hash_entry_size
bce5a025 1773 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1774};
1775
1776template<>
1777Target::Target_info Target_powerpc<64, true>::powerpc_info =
1778{
1779 64, // size
1780 true, // is_big_endian
1781 elfcpp::EM_PPC64, // machine_code
1782 false, // has_make_symbol
565ed01a 1783 true, // has_resolve
42cacb20 1784 false, // has_code_fill
ec769010 1785 false, // is_default_stack_executable
b3ce541e 1786 false, // can_icf_inline_merge_sections
42cacb20
DE
1787 '\0', // wrap_char
1788 "/usr/lib/ld.so.1", // dynamic_linker
1789 0x10000000, // default_text_segment_address
1790 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1791 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1792 false, // isolate_execinstr
1793 0, // rosegment_gap
8a5e3e08
ILT
1794 elfcpp::SHN_UNDEF, // small_common_shndx
1795 elfcpp::SHN_UNDEF, // large_common_shndx
1796 0, // small_common_section_flags
05a352e6
DK
1797 0, // large_common_section_flags
1798 NULL, // attributes_section
a67858e0 1799 NULL, // attributes_vendor
8d9743bd
MK
1800 "_start", // entry_symbol_name
1801 32, // hash_entry_size
bce5a025 1802 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1803};
1804
1805template<>
1806Target::Target_info Target_powerpc<64, false>::powerpc_info =
1807{
1808 64, // size
1809 false, // is_big_endian
1810 elfcpp::EM_PPC64, // machine_code
1811 false, // has_make_symbol
565ed01a 1812 true, // has_resolve
42cacb20 1813 false, // has_code_fill
ec769010 1814 false, // is_default_stack_executable
b3ce541e 1815 false, // can_icf_inline_merge_sections
42cacb20
DE
1816 '\0', // wrap_char
1817 "/usr/lib/ld.so.1", // dynamic_linker
1818 0x10000000, // default_text_segment_address
1819 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1820 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1821 false, // isolate_execinstr
1822 0, // rosegment_gap
8a5e3e08
ILT
1823 elfcpp::SHN_UNDEF, // small_common_shndx
1824 elfcpp::SHN_UNDEF, // large_common_shndx
1825 0, // small_common_section_flags
05a352e6
DK
1826 0, // large_common_section_flags
1827 NULL, // attributes_section
a67858e0 1828 NULL, // attributes_vendor
8d9743bd
MK
1829 "_start", // entry_symbol_name
1830 32, // hash_entry_size
bce5a025 1831 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1832};
1833
32f59844 1834template<int size>
dd93cd0a
AM
1835inline bool
1836is_branch_reloc(unsigned int r_type)
1837{
1838 return (r_type == elfcpp::R_POWERPC_REL24
32f59844 1839 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
dd93cd0a
AM
1840 || r_type == elfcpp::R_PPC_PLTREL24
1841 || r_type == elfcpp::R_PPC_LOCAL24PC
1842 || r_type == elfcpp::R_POWERPC_REL14
1843 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1844 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1845 || r_type == elfcpp::R_POWERPC_ADDR24
1846 || r_type == elfcpp::R_POWERPC_ADDR14
1847 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1848 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1849}
1850
08be3224
AM
1851// Reloc resolves to plt entry.
1852template<int size>
1853inline bool
1854is_plt16_reloc(unsigned int r_type)
1855{
1856 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1857 || r_type == elfcpp::R_POWERPC_PLT16_HI
1858 || r_type == elfcpp::R_POWERPC_PLT16_HA
1859 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1860}
1861
dd93cd0a
AM
1862// If INSN is an opcode that may be used with an @tls operand, return
1863// the transformed insn for TLS optimisation, otherwise return 0. If
1864// REG is non-zero only match an insn with RB or RA equal to REG.
1865uint32_t
1866at_tls_transform(uint32_t insn, unsigned int reg)
1867{
1868 if ((insn & (0x3f << 26)) != 31 << 26)
1869 return 0;
1870
1871 unsigned int rtra;
1872 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1873 rtra = insn & ((1 << 26) - (1 << 16));
1874 else if (((insn >> 16) & 0x1f) == reg)
1875 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1876 else
1877 return 0;
1878
1879 if ((insn & (0x3ff << 1)) == 266 << 1)
1880 // add -> addi
1881 insn = 14 << 26;
1882 else if ((insn & (0x1f << 1)) == 23 << 1
1883 && ((insn & (0x1f << 6)) < 14 << 6
1884 || ((insn & (0x1f << 6)) >= 16 << 6
1885 && (insn & (0x1f << 6)) < 24 << 6)))
1886 // load and store indexed -> dform
1887 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1888 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1889 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1890 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1891 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1892 // lwax -> lwa
1893 insn = (58 << 26) | 2;
1894 else
1895 return 0;
1896 insn |= rtra;
1897 return insn;
1898}
1899
dd93cd0a 1900
42cacb20
DE
1901template<int size, bool big_endian>
1902class Powerpc_relocate_functions
1903{
dd93cd0a 1904public:
f4baf0d4 1905 enum Overflow_check
dd93cd0a 1906 {
f4baf0d4
AM
1907 CHECK_NONE,
1908 CHECK_SIGNED,
b80eed39
AM
1909 CHECK_UNSIGNED,
1910 CHECK_BITFIELD,
1911 CHECK_LOW_INSN,
1912 CHECK_HIGH_INSN
dd93cd0a
AM
1913 };
1914
f4baf0d4 1915 enum Status
dd93cd0a 1916 {
f4baf0d4
AM
1917 STATUS_OK,
1918 STATUS_OVERFLOW
1919 };
dd93cd0a 1920
42cacb20 1921private:
c9269dff 1922 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1923 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1924 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1925
dd93cd0a
AM
1926 template<int valsize>
1927 static inline bool
1928 has_overflow_signed(Address value)
1929 {
1930 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1931 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1932 limit <<= ((valsize - 1) >> 1);
1933 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1934 return value + limit > (limit << 1) - 1;
1935 }
1936
1937 template<int valsize>
1938 static inline bool
b80eed39 1939 has_overflow_unsigned(Address value)
dd93cd0a
AM
1940 {
1941 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1942 limit <<= ((valsize - 1) >> 1);
1943 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
1944 return value > (limit << 1) - 1;
1945 }
1946
1947 template<int valsize>
1948 static inline bool
1949 has_overflow_bitfield(Address value)
1950 {
1951 return (has_overflow_unsigned<valsize>(value)
1952 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
1953 }
1954
1955 template<int valsize>
f4baf0d4
AM
1956 static inline Status
1957 overflowed(Address value, Overflow_check overflow)
dd93cd0a 1958 {
f4baf0d4 1959 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
1960 {
1961 if (has_overflow_signed<valsize>(value))
f4baf0d4 1962 return STATUS_OVERFLOW;
dd93cd0a 1963 }
b80eed39
AM
1964 else if (overflow == CHECK_UNSIGNED)
1965 {
1966 if (has_overflow_unsigned<valsize>(value))
1967 return STATUS_OVERFLOW;
1968 }
f4baf0d4 1969 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
1970 {
1971 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 1972 return STATUS_OVERFLOW;
dd93cd0a 1973 }
f4baf0d4 1974 return STATUS_OK;
dd93cd0a
AM
1975 }
1976
cf43a2fe 1977 // Do a simple RELA relocation
0cfb0717 1978 template<int fieldsize, int valsize>
f4baf0d4
AM
1979 static inline Status
1980 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1981 {
0cfb0717 1982 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 1983 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1984 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
1985 return overflowed<valsize>(value, overflow);
1986 }
1987
0cfb0717 1988 template<int fieldsize, int valsize>
f4baf0d4 1989 static inline Status
42cacb20
DE
1990 rela(unsigned char* view,
1991 unsigned int right_shift,
0cfb0717 1992 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 1993 Address value,
f4baf0d4 1994 Overflow_check overflow)
42cacb20 1995 {
0cfb0717 1996 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 1997 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1998 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
6a010cf6
AM
1999 if (overflow == CHECK_SIGNED)
2000 value = static_cast<SignedAddress>(value) >> right_shift;
2001 else
2002 value = value >> right_shift;
2003 Valtype reloc = value;
42cacb20
DE
2004 val &= ~dst_mask;
2005 reloc &= dst_mask;
0cfb0717 2006 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
6a010cf6 2007 return overflowed<valsize>(value, overflow);
42cacb20
DE
2008 }
2009
cf43a2fe 2010 // Do a simple RELA relocation, unaligned.
0cfb0717 2011 template<int fieldsize, int valsize>
f4baf0d4
AM
2012 static inline Status
2013 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2014 {
0cfb0717 2015 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
2016 return overflowed<valsize>(value, overflow);
2017 }
2018
0cfb0717 2019 template<int fieldsize, int valsize>
f4baf0d4 2020 static inline Status
cf43a2fe
AM
2021 rela_ua(unsigned char* view,
2022 unsigned int right_shift,
0cfb0717 2023 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2024 Address value,
f4baf0d4 2025 Overflow_check overflow)
42cacb20 2026 {
0cfb0717 2027 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 2028 Valtype;
0cfb0717 2029 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
6a010cf6
AM
2030 if (overflow == CHECK_SIGNED)
2031 value = static_cast<SignedAddress>(value) >> right_shift;
2032 else
2033 value = value >> right_shift;
2034 Valtype reloc = value;
42cacb20
DE
2035 val &= ~dst_mask;
2036 reloc &= dst_mask;
0cfb0717 2037 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
6a010cf6 2038 return overflowed<valsize>(value, overflow);
42cacb20
DE
2039 }
2040
42cacb20 2041public:
dd93cd0a 2042 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 2043 static inline void
dd93cd0a 2044 addr64(unsigned char* view, Address value)
0cfb0717 2045 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 2046
dd93cd0a 2047 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 2048 static inline void
dd93cd0a 2049 addr64_u(unsigned char* view, Address value)
0cfb0717 2050 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
2051
2052 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
2053 static inline Status
2054 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2055 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
2056
2057 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
2058 static inline Status
2059 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2060 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
2061
2062 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
2063 static inline Status
2064 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2065 {
0cfb0717
AM
2066 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2067 value, overflow);
f4baf0d4
AM
2068 if (overflow != CHECK_NONE && (value & 3) != 0)
2069 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2070 return stat;
2071 }
42cacb20
DE
2072
2073 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
2074 static inline Status
2075 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2076 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 2077
dd93cd0a 2078 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
2079 static inline Status
2080 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2081 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 2082
dd93cd0a 2083 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2084 static inline Status
2085 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2086 {
0cfb0717 2087 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 2088 if ((value & 3) != 0)
f4baf0d4 2089 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2090 return stat;
2091 }
42cacb20 2092
a680de9a
PB
2093 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2094 static inline Status
2095 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2096 {
2097 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2098 if ((value & 15) != 0)
2099 stat = STATUS_OVERFLOW;
2100 return stat;
2101 }
2102
42cacb20
DE
2103 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2104 static inline void
dd93cd0a 2105 addr16_hi(unsigned char* view, Address value)
0cfb0717 2106 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 2107
c9269dff 2108 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 2109 static inline void
dd93cd0a
AM
2110 addr16_ha(unsigned char* view, Address value)
2111 { This::addr16_hi(view, value + 0x8000); }
42cacb20 2112
dd93cd0a 2113 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 2114 static inline void
dd93cd0a 2115 addr16_hi2(unsigned char* view, Address value)
0cfb0717 2116 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 2117
dd93cd0a 2118 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 2119 static inline void
dd93cd0a
AM
2120 addr16_ha2(unsigned char* view, Address value)
2121 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 2122
dd93cd0a 2123 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 2124 static inline void
dd93cd0a 2125 addr16_hi3(unsigned char* view, Address value)
0cfb0717 2126 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 2127
dd93cd0a 2128 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 2129 static inline void
dd93cd0a
AM
2130 addr16_ha3(unsigned char* view, Address value)
2131 { This::addr16_hi3(view, value + 0x8000); }
2132
2133 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2134 static inline Status
2135 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2136 {
0cfb0717 2137 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
2138 if (overflow != CHECK_NONE && (value & 3) != 0)
2139 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2140 return stat;
2141 }
a680de9a
PB
2142
2143 // R_POWERPC_REL16DX_HA
2144 static inline Status
2145 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2146 {
2147 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2148 Valtype* wv = reinterpret_cast<Valtype*>(view);
2149 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2150 value += 0x8000;
2151 value = static_cast<SignedAddress>(value) >> 16;
2152 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2153 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2154 return overflowed<16>(value, overflow);
2155 }
e4dff765
AM
2156
2157 // R_PPC64_D34
2158 static inline Status
2159 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2160 {
2161 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2162 value, overflow);
2163 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2164 return stat;
2165 }
2166
2167 // R_PPC64_D34_HI30
2168 static inline void
2169 addr34_hi(unsigned char *view, uint64_t value)
2170 { This::addr34(view, value >> 34, CHECK_NONE);}
2171
2172 // R_PPC64_D34_HA30
2173 static inline void
2174 addr34_ha(unsigned char *view, uint64_t value)
2175 { This::addr34_hi(view, value + (1ULL << 33));}
2176
2177 // R_PPC64_D28
2178 static inline Status
2179 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2180 {
2181 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2182 value, overflow);
2183 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2184 return stat;
2185 }
2186
2187 // R_PPC64_ADDR16_HIGHER34
2188 static inline void
2189 addr16_higher34(unsigned char* view, uint64_t value)
2190 { This::addr16(view, value >> 34, CHECK_NONE); }
2191
2192 // R_PPC64_ADDR16_HIGHERA34
2193 static inline void
2194 addr16_highera34(unsigned char* view, uint64_t value)
2195 { This::addr16_higher34(view, value + (1ULL << 33)); }
2196
2197 // R_PPC64_ADDR16_HIGHEST34
2198 static inline void
2199 addr16_highest34(unsigned char* view, uint64_t value)
2200 { This::addr16(view, value >> 50, CHECK_NONE); }
2201
2202 // R_PPC64_ADDR16_HIGHESTA34
2203 static inline void
2204 addr16_highesta34(unsigned char* view, uint64_t value)
2205 { This::addr16_highest34(view, value + (1ULL << 33)); }
cf43a2fe
AM
2206};
2207
b4f7960d
AM
2208// Set ABI version for input and output.
2209
2210template<int size, bool big_endian>
2211void
2212Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2213{
2214 this->e_flags_ |= ver;
2215 if (this->abiversion() != 0)
2216 {
2217 Target_powerpc<size, big_endian>* target =
2218 static_cast<Target_powerpc<size, big_endian>*>(
2219 parameters->sized_target<size, big_endian>());
2220 if (target->abiversion() == 0)
2221 target->set_abiversion(this->abiversion());
2222 else if (target->abiversion() != this->abiversion())
2223 gold_error(_("%s: ABI version %d is not compatible "
2224 "with ABI version %d output"),
2225 this->name().c_str(),
2226 this->abiversion(), target->abiversion());
2227
2228 }
2229}
2230
5edad15d
AM
2231// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2232// relocatable object, if such sections exists.
cf43a2fe
AM
2233
2234template<int size, bool big_endian>
2235bool
2236Powerpc_relobj<size, big_endian>::do_find_special_sections(
2237 Read_symbols_data* sd)
2238{
c9269dff
AM
2239 const unsigned char* const pshdrs = sd->section_headers->data();
2240 const unsigned char* namesu = sd->section_names->data();
2241 const char* names = reinterpret_cast<const char*>(namesu);
2242 section_size_type names_size = sd->section_names_size;
2243 const unsigned char* s;
2244
dc3714f3
AM
2245 s = this->template find_shdr<size, big_endian>(pshdrs,
2246 size == 32 ? ".got2" : ".opd",
2247 names, names_size, NULL);
c9269dff
AM
2248 if (s != NULL)
2249 {
2250 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2251 this->special_ = ndx;
b4f7960d
AM
2252 if (size == 64)
2253 {
2254 if (this->abiversion() == 0)
2255 this->set_abiversion(1);
2256 else if (this->abiversion() > 1)
2257 gold_error(_("%s: .opd invalid in abiv%d"),
2258 this->name().c_str(), this->abiversion());
2259 }
c9269dff 2260 }
5edad15d
AM
2261 if (size == 64)
2262 {
2263 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2264 names, names_size, NULL);
2265 if (s != NULL)
2266 {
2267 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2268 this->relatoc_ = ndx;
2269 typename elfcpp::Shdr<size, big_endian> shdr(s);
2270 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2271 }
2272 }
c9269dff
AM
2273 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2274}
2275
2276// Examine .rela.opd to build info about function entry points.
2277
2278template<int size, bool big_endian>
2279void
2280Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2281 size_t reloc_count,
2282 const unsigned char* prelocs,
2283 const unsigned char* plocal_syms)
2284{
2285 if (size == 64)
cf43a2fe 2286 {
0e123f69
AM
2287 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2288 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2289 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2290 Address expected_off = 0;
2291 bool regular = true;
2292 unsigned int opd_ent_size = 0;
c9269dff
AM
2293
2294 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2295 {
c9269dff
AM
2296 Reltype reloc(prelocs);
2297 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2298 = reloc.get_r_info();
2299 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2300 if (r_type == elfcpp::R_PPC64_ADDR64)
2301 {
2302 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2303 typename elfcpp::Elf_types<size>::Elf_Addr value;
2304 bool is_ordinary;
2305 unsigned int shndx;
2306 if (r_sym < this->local_symbol_count())
2307 {
2308 typename elfcpp::Sym<size, big_endian>
2309 lsym(plocal_syms + r_sym * sym_size);
2310 shndx = lsym.get_st_shndx();
2311 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2312 value = lsym.get_st_value();
2313 }
2314 else
2315 shndx = this->symbol_section_and_value(r_sym, &value,
2316 &is_ordinary);
2317 this->set_opd_ent(reloc.get_r_offset(), shndx,
2318 value + reloc.get_r_addend());
ec4dbad3
AM
2319 if (i == 2)
2320 {
2321 expected_off = reloc.get_r_offset();
2322 opd_ent_size = expected_off;
2323 }
2324 else if (expected_off != reloc.get_r_offset())
2325 regular = false;
2326 expected_off += opd_ent_size;
2327 }
2328 else if (r_type == elfcpp::R_PPC64_TOC)
2329 {
2330 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2331 regular = false;
2332 }
2333 else
2334 {
2335 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2336 this->name().c_str(), r_type);
2337 regular = false;
c9269dff
AM
2338 }
2339 }
ec4dbad3
AM
2340 if (reloc_count <= 2)
2341 opd_ent_size = this->section_size(this->opd_shndx());
2342 if (opd_ent_size != 24 && opd_ent_size != 16)
2343 regular = false;
2344 if (!regular)
2345 {
2346 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2347 this->name().c_str());
2348 opd_ent_size = 0;
2349 }
c9269dff
AM
2350 }
2351}
2352
5edad15d
AM
2353// Returns true if a code sequence loading the TOC entry at VALUE
2354// relative to the TOC pointer can be converted into code calculating
2355// a TOC pointer relative offset.
2356// If so, the TOC pointer relative offset is stored to VALUE.
2357
2358template<int size, bool big_endian>
2359bool
2360Powerpc_relobj<size, big_endian>::make_toc_relative(
2361 Target_powerpc<size, big_endian>* target,
2362 Address* value)
2363{
2364 if (size != 64)
2365 return false;
2366
e666304e
AM
2367 // With -mcmodel=medium code it is quite possible to have
2368 // toc-relative relocs referring to objects outside the TOC.
2369 // Don't try to look at a non-existent TOC.
2370 if (this->toc_shndx() == 0)
2371 return false;
2372
5edad15d
AM
2373 // Convert VALUE back to an address by adding got_base (see below),
2374 // then to an offset in the TOC by subtracting the TOC output
2375 // section address and the TOC output offset. Since this TOC output
2376 // section and the got output section are one and the same, we can
2377 // omit adding and subtracting the output section address.
2378 Address off = (*value + this->toc_base_offset()
2379 - this->output_section_offset(this->toc_shndx()));
2380 // Is this offset in the TOC? -mcmodel=medium code may be using
2381 // TOC relative access to variables outside the TOC. Those of
2382 // course can't be optimized. We also don't try to optimize code
2383 // that is using a different object's TOC.
2384 if (off >= this->section_size(this->toc_shndx()))
2385 return false;
2386
2387 if (this->no_toc_opt(off))
2388 return false;
2389
2390 section_size_type vlen;
2391 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2392 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2393 // The TOC pointer
2394 Address got_base = (target->got_section()->output_section()->address()
2395 + this->toc_base_offset());
2396 addr -= got_base;
857e829e 2397 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2398 return false;
2399
2400 *value = addr;
2401 return true;
2402}
2403
c9b8abb7
AM
2404template<int size, bool big_endian>
2405bool
2406Powerpc_relobj<size, big_endian>::make_got_relative(
2407 Target_powerpc<size, big_endian>* target,
2408 const Symbol_value<size>* psymval,
2409 Address addend,
2410 Address* value)
2411{
2412 Address addr = psymval->value(this, addend);
2413 Address got_base = (target->got_section()->output_section()->address()
2414 + this->toc_base_offset());
2415 addr -= got_base;
2416 if (addr + 0x80008000 > 0xffffffff)
2417 return false;
2418
2419 *value = addr;
2420 return true;
2421}
2422
5edad15d
AM
2423// Perform the Sized_relobj_file method, then set up opd info from
2424// .opd relocs.
2425
c9269dff
AM
2426template<int size, bool big_endian>
2427void
2428Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2429{
2430 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2431 if (size == 64)
2432 {
2433 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2434 p != rd->relocs.end();
2435 ++p)
2436 {
2437 if (p->data_shndx == this->opd_shndx())
2438 {
ec4dbad3
AM
2439 uint64_t opd_size = this->section_size(this->opd_shndx());
2440 gold_assert(opd_size == static_cast<size_t>(opd_size));
2441 if (opd_size != 0)
2442 {
2443 this->init_opd(opd_size);
2444 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2445 rd->local_symbols->data());
2446 }
c9269dff
AM
2447 break;
2448 }
cf43a2fe
AM
2449 }
2450 }
cf43a2fe
AM
2451}
2452
b4f7960d
AM
2453// Read the symbols then set up st_other vector.
2454
2455template<int size, bool big_endian>
2456void
2457Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2458{
f35c4853 2459 this->base_read_symbols(sd);
724436fc
AM
2460 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2461 return;
b4f7960d
AM
2462 if (size == 64)
2463 {
2464 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2465 const unsigned char* const pshdrs = sd->section_headers->data();
2466 const unsigned int loccount = this->do_local_symbol_count();
2467 if (loccount != 0)
2468 {
2469 this->st_other_.resize(loccount);
2470 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2471 off_t locsize = loccount * sym_size;
2472 const unsigned int symtab_shndx = this->symtab_shndx();
2473 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2474 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2475 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2476 locsize, true, false);
2477 psyms += sym_size;
2478 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2479 {
2480 elfcpp::Sym<size, big_endian> sym(psyms);
2481 unsigned char st_other = sym.get_st_other();
2482 this->st_other_[i] = st_other;
2483 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2484 {
2485 if (this->abiversion() == 0)
2486 this->set_abiversion(2);
2487 else if (this->abiversion() < 2)
2488 gold_error(_("%s: local symbol %d has invalid st_other"
2489 " for ABI version 1"),
2490 this->name().c_str(), i);
2491 }
2492 }
2493 }
2494 }
724436fc
AM
2495
2496 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2497 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2498 bool merge_attributes = false;
2499 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2500 {
2501 elfcpp::Shdr<size, big_endian> shdr(ps);
2502 switch (shdr.get_sh_type())
2503 {
2504 case elfcpp::SHT_GNU_ATTRIBUTES:
2505 {
2506 gold_assert(this->attributes_section_data_ == NULL);
2507 section_offset_type section_offset = shdr.get_sh_offset();
2508 section_size_type section_size =
2509 convert_to_section_size_type(shdr.get_sh_size());
2510 const unsigned char* view =
2511 this->get_view(section_offset, section_size, true, false);
2512 this->attributes_section_data_ =
2513 new Attributes_section_data(view, section_size);
2514 }
2515 break;
2516
2517 case elfcpp::SHT_SYMTAB:
2518 {
2519 // Sometimes an object has no contents except the section
2520 // name string table and an empty symbol table with the
2521 // undefined symbol. We don't want to merge
2522 // processor-specific flags from such an object.
2523 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2524 elfcpp::Elf_sizes<size>::sym_size;
2525 if (shdr.get_sh_size() > sym_size)
2526 merge_attributes = true;
2527 }
2528 break;
2529
2530 case elfcpp::SHT_STRTAB:
2531 break;
2532
2533 default:
2534 merge_attributes = true;
2535 break;
2536 }
2537 }
2538
2539 if (!merge_attributes)
2540 {
2541 // Should rarely happen.
2542 delete this->attributes_section_data_;
2543 this->attributes_section_data_ = NULL;
2544 }
b4f7960d
AM
2545}
2546
2547template<int size, bool big_endian>
2548void
2549Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2550{
2551 this->e_flags_ |= ver;
2552 if (this->abiversion() != 0)
2553 {
2554 Target_powerpc<size, big_endian>* target =
2555 static_cast<Target_powerpc<size, big_endian>*>(
2556 parameters->sized_target<size, big_endian>());
2557 if (target->abiversion() == 0)
2558 target->set_abiversion(this->abiversion());
2559 else if (target->abiversion() != this->abiversion())
2560 gold_error(_("%s: ABI version %d is not compatible "
2561 "with ABI version %d output"),
2562 this->name().c_str(),
2563 this->abiversion(), target->abiversion());
2564
2565 }
2566}
2567
f35c4853 2568// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2569// read .opd from a dynamic object, filling in opd_ent_ vector,
2570
2571template<int size, bool big_endian>
2572void
2573Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2574{
f35c4853 2575 this->base_read_symbols(sd);
724436fc
AM
2576 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2577 const unsigned char* ps =
2578 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2579 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2580 {
2581 elfcpp::Shdr<size, big_endian> shdr(ps);
2582 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2583 {
2584 section_offset_type section_offset = shdr.get_sh_offset();
2585 section_size_type section_size =
2586 convert_to_section_size_type(shdr.get_sh_size());
2587 const unsigned char* view =
2588 this->get_view(section_offset, section_size, true, false);
2589 this->attributes_section_data_ =
2590 new Attributes_section_data(view, section_size);
2591 break;
2592 }
2593 }
dc3714f3
AM
2594 if (size == 64)
2595 {
dc3714f3
AM
2596 const unsigned char* const pshdrs = sd->section_headers->data();
2597 const unsigned char* namesu = sd->section_names->data();
2598 const char* names = reinterpret_cast<const char*>(namesu);
2599 const unsigned char* s = NULL;
2600 const unsigned char* opd;
2601 section_size_type opd_size;
2602
2603 // Find and read .opd section.
2604 while (1)
2605 {
2606 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2607 sd->section_names_size,
2608 s);
2609 if (s == NULL)
2610 return;
2611
2612 typename elfcpp::Shdr<size, big_endian> shdr(s);
2613 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2614 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2615 {
b4f7960d
AM
2616 if (this->abiversion() == 0)
2617 this->set_abiversion(1);
2618 else if (this->abiversion() > 1)
2619 gold_error(_("%s: .opd invalid in abiv%d"),
2620 this->name().c_str(), this->abiversion());
2621
dc3714f3
AM
2622 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2623 this->opd_address_ = shdr.get_sh_addr();
2624 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2625 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2626 true, false);
2627 break;
2628 }
2629 }
2630
2631 // Build set of executable sections.
2632 // Using a set is probably overkill. There is likely to be only
2633 // a few executable sections, typically .init, .text and .fini,
2634 // and they are generally grouped together.
2635 typedef std::set<Sec_info> Exec_sections;
2636 Exec_sections exec_sections;
2637 s = pshdrs;
2638 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2639 {
2640 typename elfcpp::Shdr<size, big_endian> shdr(s);
2641 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2642 && ((shdr.get_sh_flags()
2643 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2644 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2645 && shdr.get_sh_size() != 0)
2646 {
2647 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2648 shdr.get_sh_size(), i));
2649 }
2650 }
2651 if (exec_sections.empty())
2652 return;
2653
2654 // Look over the OPD entries. This is complicated by the fact
2655 // that some binaries will use two-word entries while others
2656 // will use the standard three-word entries. In most cases
2657 // the third word (the environment pointer for languages like
2658 // Pascal) is unused and will be zero. If the third word is
2659 // used it should not be pointing into executable sections,
2660 // I think.
2661 this->init_opd(opd_size);
2662 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2663 {
2664 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2665 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2666 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2667 if (val == 0)
2668 // Chances are that this is the third word of an OPD entry.
2669 continue;
2670 typename Exec_sections::const_iterator e
2671 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2672 if (e != exec_sections.begin())
2673 {
2674 --e;
2675 if (e->start <= val && val < e->start + e->len)
2676 {
2677 // We have an address in an executable section.
2678 // VAL ought to be the function entry, set it up.
2679 this->set_opd_ent(p - opd, e->shndx, val);
2680 // Skip second word of OPD entry, the TOC pointer.
2681 p += 8;
2682 }
2683 }
2684 // If we didn't match any executable sections, we likely
2685 // have a non-zero third word in the OPD entry.
2686 }
2687 }
2688}
2689
5edad15d
AM
2690// Relocate sections.
2691
2692template<int size, bool big_endian>
2693void
2694Powerpc_relobj<size, big_endian>::do_relocate_sections(
2695 const Symbol_table* symtab, const Layout* layout,
2696 const unsigned char* pshdrs, Output_file* of,
2697 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2698{
2699 unsigned int start = 1;
2700 if (size == 64
2701 && this->relatoc_ != 0
2702 && !parameters->options().relocatable())
2703 {
2704 // Relocate .toc first.
2705 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2706 this->relatoc_, this->relatoc_);
2707 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2708 1, this->relatoc_ - 1);
2709 start = this->relatoc_ + 1;
2710 }
2711 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2712 start, this->shnum() - 1);
2d7ad24e
AM
2713
2714 if (!parameters->options().output_is_position_independent())
2715 {
2716 Target_powerpc<size, big_endian>* target
2717 = static_cast<Target_powerpc<size, big_endian>*>(
2718 parameters->sized_target<size, big_endian>());
2719 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2720 {
2721 const section_size_type offset = target->lplt_section()->offset();
2722 const section_size_type oview_size
2723 = convert_to_section_size_type(target->lplt_section()->data_size());
2724 unsigned char* const oview = of->get_output_view(offset, oview_size);
2725
2726 bool modified = false;
2727 unsigned int nsyms = this->local_symbol_count();
2728 for (unsigned int i = 0; i < nsyms; i++)
2729 if (this->local_has_plt_offset(i))
2730 {
2731 Address value = this->local_symbol_value(i, 0);
2732 if (size == 64)
2733 value += ppc64_local_entry_offset(i);
2734 size_t off = this->local_plt_offset(i);
2735 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2736 modified = true;
2737 }
2738 if (modified)
2739 of->write_output_view(offset, oview_size, oview);
2740 }
2741 }
5edad15d
AM
2742}
2743
f43ba157 2744// Set up some symbols.
26a4e9cb
AM
2745
2746template<int size, bool big_endian>
2747void
f43ba157
AM
2748Target_powerpc<size, big_endian>::do_define_standard_symbols(
2749 Symbol_table* symtab,
2750 Layout* layout)
26a4e9cb
AM
2751{
2752 if (size == 32)
2753 {
bb66a627
AM
2754 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2755 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2756 // non-relative dynamic relocs). The proper value will be
2757 // updated later.
2758 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2759 if (gotsym != NULL && gotsym->is_undefined())
2760 {
2761 Target_powerpc<size, big_endian>* target =
2762 static_cast<Target_powerpc<size, big_endian>*>(
2763 parameters->sized_target<size, big_endian>());
2764 Output_data_got_powerpc<size, big_endian>* got
2765 = target->got_section(symtab, layout);
2766 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2767 Symbol_table::PREDEFINED,
2768 got, 0, 0,
2769 elfcpp::STT_OBJECT,
bb66a627 2770 elfcpp::STB_LOCAL,
26a4e9cb
AM
2771 elfcpp::STV_HIDDEN, 0,
2772 false, false);
2773 }
2774
2775 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2776 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2777 if (sdasym != NULL && sdasym->is_undefined())
2778 {
2779 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2780 Output_section* os
2781 = layout->add_output_section_data(".sdata", 0,
2782 elfcpp::SHF_ALLOC
2783 | elfcpp::SHF_WRITE,
2784 sdata, ORDER_SMALL_DATA, false);
2785 symtab->define_in_output_data("_SDA_BASE_", NULL,
2786 Symbol_table::PREDEFINED,
2787 os, 32768, 0, elfcpp::STT_OBJECT,
2788 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2789 0, false, false);
2790 }
2791 }
b4f7960d
AM
2792 else
2793 {
2794 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2795 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2796 if (gotsym != NULL && gotsym->is_undefined())
2797 {
2798 Target_powerpc<size, big_endian>* target =
2799 static_cast<Target_powerpc<size, big_endian>*>(
2800 parameters->sized_target<size, big_endian>());
2801 Output_data_got_powerpc<size, big_endian>* got
2802 = target->got_section(symtab, layout);
2803 symtab->define_in_output_data(".TOC.", NULL,
2804 Symbol_table::PREDEFINED,
2805 got, 0x8000, 0,
2806 elfcpp::STT_OBJECT,
2807 elfcpp::STB_LOCAL,
2808 elfcpp::STV_HIDDEN, 0,
2809 false, false);
2810 }
2811 }
34e0882b
AM
2812
2813 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2814 if (parameters->options().tls_get_addr_optimize()
2815 && this->tls_get_addr_ != NULL
2816 && this->tls_get_addr_->in_reg())
2817 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2818 if (this->tls_get_addr_opt_ != NULL)
2819 {
2820 if (this->tls_get_addr_->is_undefined()
2821 || this->tls_get_addr_->is_from_dynobj())
2822 {
2823 // Make it seem as if references to __tls_get_addr are
2824 // really to __tls_get_addr_opt, so the latter symbol is
2825 // made dynamic, not the former.
2826 this->tls_get_addr_->clear_in_reg();
2827 this->tls_get_addr_opt_->set_in_reg();
2828 }
2829 // We have a non-dynamic definition for __tls_get_addr.
2830 // Make __tls_get_addr_opt the same, if it does not already have
2831 // a non-dynamic definition.
2832 else if (this->tls_get_addr_opt_->is_undefined()
2833 || this->tls_get_addr_opt_->is_from_dynobj())
2834 {
2835 Sized_symbol<size>* from
2836 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2837 Sized_symbol<size>* to
2838 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2839 symtab->clone<size>(to, from);
2840 }
2841 }
26a4e9cb
AM
2842}
2843
cf43a2fe
AM
2844// Set up PowerPC target specific relobj.
2845
2846template<int size, bool big_endian>
2847Object*
2848Target_powerpc<size, big_endian>::do_make_elf_object(
2849 const std::string& name,
2850 Input_file* input_file,
2851 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2852{
2853 int et = ehdr.get_e_type();
957564c9
AS
2854 // ET_EXEC files are valid input for --just-symbols/-R,
2855 // and we treat them as relocatable objects.
2856 if (et == elfcpp::ET_REL
2857 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2858 {
2859 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2860 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2861 obj->setup();
2862 return obj;
2863 }
2864 else if (et == elfcpp::ET_DYN)
2865 {
dc3714f3
AM
2866 Powerpc_dynobj<size, big_endian>* obj =
2867 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2868 obj->setup();
2869 return obj;
2870 }
2871 else
2872 {
c9269dff 2873 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2874 return NULL;
2875 }
2876}
2877
2878template<int size, bool big_endian>
2879class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2880{
2881public:
2882 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2883 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2884
2885 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2886 : Output_data_got<size, big_endian>(),
2887 symtab_(symtab), layout_(layout),
2888 header_ent_cnt_(size == 32 ? 3 : 1),
2889 header_index_(size == 32 ? 0x2000 : 0)
751e4d66
AM
2890 {
2891 if (size == 64)
2892 this->set_addralign(256);
2893 }
cf43a2fe 2894
e84fe78f
AM
2895 // Override all the Output_data_got methods we use so as to first call
2896 // reserve_ent().
2897 bool
2898 add_global(Symbol* gsym, unsigned int got_type)
2899 {
2900 this->reserve_ent();
2901 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2902 }
2903
2904 bool
2905 add_global_plt(Symbol* gsym, unsigned int got_type)
2906 {
2907 this->reserve_ent();
2908 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2909 }
2910
2911 bool
2912 add_global_tls(Symbol* gsym, unsigned int got_type)
2913 { return this->add_global_plt(gsym, got_type); }
2914
2915 void
2916 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2917 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2918 {
2919 this->reserve_ent();
2920 Output_data_got<size, big_endian>::
2921 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2922 }
2923
2924 void
2925 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2926 Output_data_reloc_generic* rel_dyn,
2927 unsigned int r_type_1, unsigned int r_type_2)
2928 {
aacb3b6d
AM
2929 if (gsym->has_got_offset(got_type))
2930 return;
2931
e84fe78f
AM
2932 this->reserve_ent(2);
2933 Output_data_got<size, big_endian>::
2934 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2935 }
2936
2937 bool
2938 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2939 {
2940 this->reserve_ent();
2941 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2942 got_type);
2943 }
2944
2945 bool
2946 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2947 {
2948 this->reserve_ent();
2949 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2950 got_type);
2951 }
2952
2953 bool
2954 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2955 { return this->add_local_plt(object, sym_index, got_type); }
2956
2957 void
2958 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2959 unsigned int got_type,
2960 Output_data_reloc_generic* rel_dyn,
2961 unsigned int r_type)
2962 {
aacb3b6d
AM
2963 if (object->local_has_got_offset(sym_index, got_type))
2964 return;
2965
e84fe78f
AM
2966 this->reserve_ent(2);
2967 Output_data_got<size, big_endian>::
2968 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2969 }
2970
2971 unsigned int
2972 add_constant(Valtype constant)
2973 {
2974 this->reserve_ent();
2975 return Output_data_got<size, big_endian>::add_constant(constant);
2976 }
2977
dd93cd0a
AM
2978 unsigned int
2979 add_constant_pair(Valtype c1, Valtype c2)
2980 {
2981 this->reserve_ent(2);
e84fe78f 2982 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
2983 }
2984
2985 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
2986 unsigned int
2987 g_o_t() const
2988 {
2989 return this->got_offset(this->header_index_);
42cacb20 2990 }
cf43a2fe 2991
dd93cd0a
AM
2992 // Offset of base used to access the GOT/TOC.
2993 // The got/toc pointer reg will be set to this value.
26a4e9cb 2994 Valtype
dd93cd0a
AM
2995 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2996 {
2997 if (size == 32)
2998 return this->g_o_t();
2999 else
3000 return (this->output_section()->address()
3001 + object->toc_base_offset()
3002 - this->address());
3003 }
3004
cf43a2fe
AM
3005 // Ensure our GOT has a header.
3006 void
3007 set_final_data_size()
3008 {
3009 if (this->header_ent_cnt_ != 0)
3010 this->make_header();
3011 Output_data_got<size, big_endian>::set_final_data_size();
3012 }
3013
3014 // First word of GOT header needs some values that are not
3015 // handled by Output_data_got so poke them in here.
3016 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3017 void
3018 do_write(Output_file* of)
3019 {
c9824451
AM
3020 Valtype val = 0;
3021 if (size == 32 && this->layout_->dynamic_data() != NULL)
3022 val = this->layout_->dynamic_section()->address();
3023 if (size == 64)
3024 val = this->output_section()->address() + 0x8000;
3025 this->replace_constant(this->header_index_, val);
cf43a2fe
AM
3026 Output_data_got<size, big_endian>::do_write(of);
3027 }
3028
3029private:
3030 void
3031 reserve_ent(unsigned int cnt = 1)
3032 {
3033 if (this->header_ent_cnt_ == 0)
3034 return;
3035 if (this->num_entries() + cnt > this->header_index_)
3036 this->make_header();
3037 }
3038
3039 void
3040 make_header()
3041 {
3042 this->header_ent_cnt_ = 0;
3043 this->header_index_ = this->num_entries();
3044 if (size == 32)
3045 {
3046 Output_data_got<size, big_endian>::add_constant(0);
3047 Output_data_got<size, big_endian>::add_constant(0);
3048 Output_data_got<size, big_endian>::add_constant(0);
3049
3050 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
3051 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3052 if (gotsym != NULL)
3053 {
3054 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3055 sym->set_value(this->g_o_t());
3056 }
3057 else
3058 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3059 Symbol_table::PREDEFINED,
3060 this, this->g_o_t(), 0,
3061 elfcpp::STT_OBJECT,
3062 elfcpp::STB_LOCAL,
3063 elfcpp::STV_HIDDEN, 0,
3064 false, false);
cf43a2fe
AM
3065 }
3066 else
3067 Output_data_got<size, big_endian>::add_constant(0);
3068 }
3069
3070 // Stashed pointers.
3071 Symbol_table* symtab_;
3072 Layout* layout_;
3073
3074 // GOT header size.
3075 unsigned int header_ent_cnt_;
3076 // GOT header index.
3077 unsigned int header_index_;
42cacb20
DE
3078};
3079
3080// Get the GOT section, creating it if necessary.
3081
3082template<int size, bool big_endian>
cf43a2fe 3083Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
3084Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3085 Layout* layout)
3086{
3087 if (this->got_ == NULL)
3088 {
3089 gold_assert(symtab != NULL && layout != NULL);
3090
cf43a2fe
AM
3091 this->got_
3092 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
3093
3094 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3095 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 3096 this->got_, ORDER_DATA, false);
42cacb20
DE
3097 }
3098
3099 return this->got_;
3100}
3101
3102// Get the dynamic reloc section, creating it if necessary.
3103
3104template<int size, bool big_endian>
3105typename Target_powerpc<size, big_endian>::Reloc_section*
3106Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3107{
3108 if (this->rela_dyn_ == NULL)
3109 {
3110 gold_assert(layout != NULL);
3111 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3112 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
3113 elfcpp::SHF_ALLOC, this->rela_dyn_,
3114 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
3115 }
3116 return this->rela_dyn_;
3117}
3118
b3ccdeb5
AM
3119// Similarly, but for ifunc symbols get the one for ifunc.
3120
3121template<int size, bool big_endian>
3122typename Target_powerpc<size, big_endian>::Reloc_section*
3123Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3124 Layout* layout,
3125 bool for_ifunc)
3126{
3127 if (!for_ifunc)
3128 return this->rela_dyn_section(layout);
3129
3130 if (this->iplt_ == NULL)
3131 this->make_iplt_section(symtab, layout);
3132 return this->iplt_->rel_plt();
3133}
3134
ec661b9d
AM
3135class Stub_control
3136{
3137 public:
3138 // Determine the stub group size. The group size is the absolute
3139 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 3140 // is passed a negative value, we restrict stubs to be always after
ec661b9d 3141 // the stubbed branches.
1c3a5fbe
AM
3142 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3143 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3144 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3145 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3146 owner_(NULL), output_section_(NULL)
ec661b9d 3147 {
ec661b9d
AM
3148 }
3149
3150 // Return true iff input section can be handled by current stub
3151 // group.
3152 bool
3153 can_add_to_stub_group(Output_section* o,
3154 const Output_section::Input_section* i,
3155 bool has14);
3156
3157 const Output_section::Input_section*
3158 owner()
3159 { return owner_; }
3160
3161 Output_section*
3162 output_section()
3163 { return output_section_; }
3164
a20605cf
AM
3165 void
3166 set_output_and_owner(Output_section* o,
3167 const Output_section::Input_section* i)
3168 {
3169 this->output_section_ = o;
3170 this->owner_ = i;
3171 }
3172
ec661b9d
AM
3173 private:
3174 typedef enum
3175 {
1c3a5fbe 3176 // Initial state.
ec661b9d 3177 NO_GROUP,
1c3a5fbe 3178 // Adding group sections before the stubs.
ec661b9d 3179 FINDING_STUB_SECTION,
1c3a5fbe 3180 // Adding group sections after the stubs.
ec661b9d
AM
3181 HAS_STUB_SECTION
3182 } State;
3183
ec661b9d 3184 uint32_t stub_group_size_;
a5018ae5 3185 bool stubs_always_after_branch_;
ec661b9d 3186 bool suppress_size_errors_;
1c3a5fbe
AM
3187 // True if a stub group can serve multiple output sections.
3188 bool multi_os_;
3189 State state_;
8a37735f
AM
3190 // Current max size of group. Starts at stub_group_size_ but is
3191 // reduced to stub_group_size_/1024 on seeing a section with
3192 // external conditional branches.
3193 uint32_t group_size_;
a5018ae5 3194 uint64_t group_start_addr_;
57f6d32d
AM
3195 // owner_ and output_section_ specify the section to which stubs are
3196 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
3197 const Output_section::Input_section* owner_;
3198 Output_section* output_section_;
3199};
3200
0cfdc767 3201// Return true iff input section can be handled by current stub
a5018ae5
AM
3202// group. Sections are presented to this function in order,
3203// so the first section is the head of the group.
ec661b9d
AM
3204
3205bool
3206Stub_control::can_add_to_stub_group(Output_section* o,
3207 const Output_section::Input_section* i,
3208 bool has14)
3209{
ec661b9d
AM
3210 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3211 uint64_t this_size;
3212 uint64_t start_addr = o->address();
3213
3214 if (whole_sec)
3215 // .init and .fini sections are pasted together to form a single
3216 // function. We can't be adding stubs in the middle of the function.
3217 this_size = o->data_size();
3218 else
3219 {
3220 start_addr += i->relobj()->output_section_offset(i->shndx());
3221 this_size = i->data_size();
3222 }
57f6d32d 3223
a5018ae5 3224 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
3225 uint32_t group_size = this->stub_group_size_;
3226 if (has14)
3227 this->group_size_ = group_size = group_size >> 10;
ec661b9d 3228
57f6d32d 3229 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
3230 gold_warning(_("%s:%s exceeds group size"),
3231 i->relobj()->name().c_str(),
3232 i->relobj()->section_name(i->shndx()).c_str());
3233
afe002dd
AM
3234 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3235 has14 ? " 14bit" : "",
3236 i->relobj()->name().c_str(),
3237 i->relobj()->section_name(i->shndx()).c_str(),
3238 (long long) this_size,
a5018ae5
AM
3239 (this->state_ == NO_GROUP
3240 ? this_size
3241 : (long long) end_addr - this->group_start_addr_));
afe002dd 3242
1c3a5fbe
AM
3243 if (this->state_ == NO_GROUP)
3244 {
3245 // Only here on very first use of Stub_control
3246 this->owner_ = i;
3247 this->output_section_ = o;
3248 this->state_ = FINDING_STUB_SECTION;
3249 this->group_size_ = group_size;
3250 this->group_start_addr_ = start_addr;
3251 return true;
3252 }
3253 else if (!this->multi_os_ && this->output_section_ != o)
3254 ;
3255 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 3256 {
a5018ae5 3257 // Can we add this section, which is after the stubs, to the
57f6d32d 3258 // group?
a5018ae5 3259 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3260 return true;
ec661b9d 3261 }
a5018ae5 3262 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 3263 {
a5018ae5
AM
3264 if ((whole_sec && this->output_section_ == o)
3265 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3266 {
a5018ae5 3267 // Stubs are added at the end of "owner_".
57f6d32d
AM
3268 this->owner_ = i;
3269 this->output_section_ = o;
a5018ae5 3270 return true;
57f6d32d 3271 }
a5018ae5
AM
3272 // The group before the stubs has reached maximum size.
3273 // Now see about adding sections after the stubs to the
3274 // group. If the current section has a 14-bit branch and
3275 // the group before the stubs exceeds group_size_ (because
3276 // they didn't have 14-bit branches), don't add sections
3277 // after the stubs: The size of stubs for such a large
3278 // group may exceed the reach of a 14-bit branch.
3279 if (!this->stubs_always_after_branch_
3280 && this_size <= this->group_size_
3281 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3282 {
a5018ae5
AM
3283 gold_debug(DEBUG_TARGET, "adding after stubs");
3284 this->state_ = HAS_STUB_SECTION;
3285 this->group_start_addr_ = start_addr;
57f6d32d
AM
3286 return true;
3287 }
ec661b9d 3288 }
a5018ae5
AM
3289 else
3290 gold_unreachable();
57f6d32d 3291
1c3a5fbe
AM
3292 gold_debug(DEBUG_TARGET,
3293 !this->multi_os_ && this->output_section_ != o
3294 ? "nope, new output section\n"
3295 : "nope, didn't fit\n");
afe002dd 3296
57f6d32d
AM
3297 // The section fails to fit in the current group. Set up a few
3298 // things for the next group. owner_ and output_section_ will be
3299 // set later after we've retrieved those values for the current
3300 // group.
3301 this->state_ = FINDING_STUB_SECTION;
8a37735f 3302 this->group_size_ = group_size;
a5018ae5 3303 this->group_start_addr_ = start_addr;
57f6d32d 3304 return false;
ec661b9d
AM
3305}
3306
3307// Look over all the input sections, deciding where to place stubs.
3308
3309template<int size, bool big_endian>
3310void
3311Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
3312 const Task*,
3313 bool no_size_errors)
ec661b9d 3314{
1c3a5fbe
AM
3315 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3316 parameters->options().stub_group_multi());
ec661b9d
AM
3317
3318 // Group input sections and insert stub table
a3e60ddb
AM
3319 Stub_table_owner* table_owner = NULL;
3320 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
3321 Layout::Section_list section_list;
3322 layout->get_executable_sections(&section_list);
3323 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
3324 for (Layout::Section_list::iterator o = section_list.begin();
3325 o != section_list.end();
ec661b9d
AM
3326 ++o)
3327 {
3328 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
3329 for (Input_section_list::const_iterator i
3330 = (*o)->input_sections().begin();
3331 i != (*o)->input_sections().end();
ec661b9d
AM
3332 ++i)
3333 {
a3e60ddb
AM
3334 if (i->is_input_section()
3335 || i->is_relaxed_input_section())
ec661b9d
AM
3336 {
3337 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3338 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3339 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3340 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3341 {
a3e60ddb
AM
3342 table_owner->output_section = stub_control.output_section();
3343 table_owner->owner = stub_control.owner();
a20605cf 3344 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 3345 table_owner = NULL;
ec661b9d 3346 }
a3e60ddb
AM
3347 if (table_owner == NULL)
3348 {
3349 table_owner = new Stub_table_owner;
3350 tables.push_back(table_owner);
3351 }
3352 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
3353 }
3354 }
3355 }
a3e60ddb 3356 if (table_owner != NULL)
0cfdc767 3357 {
a5018ae5
AM
3358 table_owner->output_section = stub_control.output_section();
3359 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
3360 }
3361 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3362 t != tables.end();
3363 ++t)
3364 {
3365 Stub_table<size, big_endian>* stub_table;
3366
3367 if ((*t)->owner->is_input_section())
3368 stub_table = new Stub_table<size, big_endian>(this,
3369 (*t)->output_section,
590b87ff
AM
3370 (*t)->owner,
3371 this->stub_tables_.size());
a3e60ddb
AM
3372 else if ((*t)->owner->is_relaxed_input_section())
3373 stub_table = static_cast<Stub_table<size, big_endian>*>(
3374 (*t)->owner->relaxed_input_section());
0cfdc767 3375 else
a3e60ddb
AM
3376 gold_unreachable();
3377 this->stub_tables_.push_back(stub_table);
3378 delete *t;
0cfdc767 3379 }
ec661b9d
AM
3380}
3381
32f59844 3382template<int size>
a3e60ddb
AM
3383static unsigned long
3384max_branch_delta (unsigned int r_type)
3385{
3386 if (r_type == elfcpp::R_POWERPC_REL14
3387 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3388 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3389 return 1L << 15;
3390 if (r_type == elfcpp::R_POWERPC_REL24
32f59844 3391 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
a3e60ddb
AM
3392 || r_type == elfcpp::R_PPC_PLTREL24
3393 || r_type == elfcpp::R_PPC_LOCAL24PC)
3394 return 1L << 25;
3395 return 0;
3396}
3397
7e57d19e
AM
3398// Return whether this branch is going via a plt call stub.
3399
3400template<int size, bool big_endian>
3401bool
3402Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3403 Powerpc_relobj<size, big_endian>* ppc_object,
3404 unsigned int shndx,
3405 Address offset,
3406 Target_powerpc* target,
3407 Symbol_table* symtab)
3408{
3409 if (this->object_ != ppc_object
3410 || this->shndx_ != shndx
3411 || this->offset_ != offset)
3412 return false;
3413
3414 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3415 if (sym != NULL && sym->is_forwarder())
3416 sym = symtab->resolve_forwards(sym);
2778747c
AM
3417 if (target->replace_tls_get_addr(sym))
3418 sym = target->tls_get_addr_opt();
7e57d19e
AM
3419 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3420 if (gsym != NULL
7ee7ff70
AM
3421 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3422 && !target->is_elfv2_localentry0(gsym))
3423 : (this->object_->local_has_plt_offset(this->r_sym_)
3424 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3425 {
3426 this->tocsave_ = 1;
3427 return true;
3428 }
3429 return false;
3430}
3431
ec661b9d
AM
3432// If this branch needs a plt call stub, or a long branch stub, make one.
3433
3434template<int size, bool big_endian>
a3e60ddb 3435bool
ec661b9d
AM
3436Target_powerpc<size, big_endian>::Branch_info::make_stub(
3437 Stub_table<size, big_endian>* stub_table,
3438 Stub_table<size, big_endian>* ifunc_stub_table,
3439 Symbol_table* symtab) const
3440{
3441 Symbol* sym = this->object_->global_symbol(this->r_sym_);
88b8e639
AM
3442 Target_powerpc<size, big_endian>* target =
3443 static_cast<Target_powerpc<size, big_endian>*>(
3444 parameters->sized_target<size, big_endian>());
34e0882b
AM
3445 if (sym != NULL && sym->is_forwarder())
3446 sym = symtab->resolve_forwards(sym);
2778747c
AM
3447 if (target->replace_tls_get_addr(sym))
3448 sym = target->tls_get_addr_opt();
34e0882b 3449 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
dc60b26d
AM
3450 bool ok = true;
3451
ec661b9d 3452 if (gsym != NULL
88b8e639 3453 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3454 : this->object_->local_has_plt_offset(this->r_sym_))
3455 {
9055360d
AM
3456 if (size == 64
3457 && gsym != NULL
3458 && target->abiversion() >= 2
3459 && !parameters->options().output_is_position_independent()
32f59844 3460 && !is_branch_reloc<size>(this->r_type_))
9055360d
AM
3461 target->glink_section()->add_global_entry(gsym);
3462 else
ec661b9d 3463 {
64b5d6d7
AM
3464 if (stub_table == NULL
3465 && !(size == 32
3466 && gsym != NULL
3467 && !parameters->options().output_is_position_independent()
32f59844 3468 && !is_branch_reloc<size>(this->r_type_)))
9055360d
AM
3469 stub_table = this->object_->stub_table(this->shndx_);
3470 if (stub_table == NULL)
3471 {
64b5d6d7
AM
3472 // This is a ref from a data section to an ifunc symbol,
3473 // or a non-branch reloc for which we always want to use
3474 // one set of stubs for resolving function addresses.
9055360d
AM
3475 stub_table = ifunc_stub_table;
3476 }
3477 gold_assert(stub_table != NULL);
a3e60ddb
AM
3478 Address from = this->object_->get_output_section_offset(this->shndx_);
3479 if (from != invalid_address)
3480 from += (this->object_->output_section(this->shndx_)->address()
3481 + this->offset_);
9055360d 3482 if (gsym != NULL)
dc60b26d
AM
3483 ok = stub_table->add_plt_call_entry(from,
3484 this->object_, gsym,
7e57d19e
AM
3485 this->r_type_, this->addend_,
3486 this->tocsave_);
9055360d 3487 else
dc60b26d
AM
3488 ok = stub_table->add_plt_call_entry(from,
3489 this->object_, this->r_sym_,
7e57d19e
AM
3490 this->r_type_, this->addend_,
3491 this->tocsave_);
ec661b9d 3492 }
ec661b9d
AM
3493 }
3494 else
3495 {
32f59844 3496 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
a3e60ddb
AM
3497 if (max_branch_offset == 0)
3498 return true;
ec661b9d
AM
3499 Address from = this->object_->get_output_section_offset(this->shndx_);
3500 gold_assert(from != invalid_address);
3501 from += (this->object_->output_section(this->shndx_)->address()
3502 + this->offset_);
3503 Address to;
3504 if (gsym != NULL)
3505 {
3506 switch (gsym->source())
3507 {
3508 case Symbol::FROM_OBJECT:
3509 {
3510 Object* symobj = gsym->object();
3511 if (symobj->is_dynamic()
3512 || symobj->pluginobj() != NULL)
a3e60ddb 3513 return true;
ec661b9d
AM
3514 bool is_ordinary;
3515 unsigned int shndx = gsym->shndx(&is_ordinary);
3516 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3517 return true;
ec661b9d
AM
3518 }
3519 break;
3520
3521 case Symbol::IS_UNDEFINED:
a3e60ddb 3522 return true;
ec661b9d
AM
3523
3524 default:
3525 break;
3526 }
3527 Symbol_table::Compute_final_value_status status;
3528 to = symtab->compute_final_value<size>(gsym, &status);
3529 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3530 return true;
9055360d
AM
3531 if (size == 64)
3532 to += this->object_->ppc64_local_entry_offset(gsym);
ec661b9d
AM
3533 }
3534 else
3535 {
3536 const Symbol_value<size>* psymval
3537 = this->object_->local_symbol(this->r_sym_);
3538 Symbol_value<size> symval;
0f125432
CC
3539 if (psymval->is_section_symbol())
3540 symval.set_is_section_symbol();
ec661b9d
AM
3541 typedef Sized_relobj_file<size, big_endian> ObjType;
3542 typename ObjType::Compute_final_local_value_status status
3543 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3544 &symval, symtab);
3545 if (status != ObjType::CFLV_OK
3546 || !symval.has_output_value())
a3e60ddb 3547 return true;
ec661b9d 3548 to = symval.value(this->object_, 0);
9055360d
AM
3549 if (size == 64)
3550 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
ec661b9d 3551 }
cbcb23fa
AM
3552 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3553 to += this->addend_;
ec661b9d
AM
3554 if (stub_table == NULL)
3555 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3556 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3557 {
3558 unsigned int dest_shndx;
1611bc4a
AM
3559 if (!target->symval_for_branch(symtab, gsym, this->object_,
3560 &to, &dest_shndx))
3561 return true;
ec661b9d
AM
3562 }
3563 Address delta = to - from;
32f59844
AM
3564 if (delta + max_branch_offset >= 2 * max_branch_offset
3565 || (size == 64
3566 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3567 && (gsym != NULL
3568 ? this->object_->ppc64_needs_toc(gsym)
3569 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3570 {
0cfdc767
AM
3571 if (stub_table == NULL)
3572 {
3573 gold_warning(_("%s:%s: branch in non-executable section,"
3574 " no long branch stub for you"),
3575 this->object_->name().c_str(),
3576 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3577 return true;
0cfdc767 3578 }
d49044c7
AM
3579 bool save_res = (size == 64
3580 && gsym != NULL
3581 && gsym->source() == Symbol::IN_OUTPUT_DATA
3582 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3583 ok = stub_table->add_long_branch_entry(this->object_,
3584 this->r_type_,
3585 from, to, save_res);
ec661b9d
AM
3586 }
3587 }
dc60b26d
AM
3588 if (!ok)
3589 gold_debug(DEBUG_TARGET,
3590 "branch at %s:%s+%#lx\n"
3591 "can't reach stub attached to %s:%s",
3592 this->object_->name().c_str(),
3593 this->object_->section_name(this->shndx_).c_str(),
3594 (unsigned long) this->offset_,
3595 stub_table->relobj()->name().c_str(),
3596 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3597
3598 return ok;
ec661b9d
AM
3599}
3600
3601// Relaxation hook. This is where we do stub generation.
3602
3603template<int size, bool big_endian>
3604bool
3605Target_powerpc<size, big_endian>::do_relax(int pass,
3606 const Input_objects*,
3607 Symbol_table* symtab,
3608 Layout* layout,
3609 const Task* task)
3610{
3611 unsigned int prev_brlt_size = 0;
3612 if (pass == 1)
ec661b9d 3613 {
b4f7960d
AM
3614 bool thread_safe
3615 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3616 if (size == 64
3617 && this->abiversion() < 2
3618 && !thread_safe
3619 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3620 {
e2458743 3621 static const char* const thread_starter[] =
9e69ed50
AM
3622 {
3623 "pthread_create",
3624 /* libstdc++ */
3625 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3626 /* librt */
3627 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3628 "mq_notify", "create_timer",
3629 /* libanl */
3630 "getaddrinfo_a",
3631 /* libgomp */
80272b8c 3632 "GOMP_parallel",
9e69ed50 3633 "GOMP_parallel_start",
80272b8c 3634 "GOMP_parallel_loop_static",
9e69ed50 3635 "GOMP_parallel_loop_static_start",
80272b8c 3636 "GOMP_parallel_loop_dynamic",
9e69ed50 3637 "GOMP_parallel_loop_dynamic_start",
80272b8c 3638 "GOMP_parallel_loop_guided",
9e69ed50 3639 "GOMP_parallel_loop_guided_start",
80272b8c 3640 "GOMP_parallel_loop_runtime",
9e69ed50 3641 "GOMP_parallel_loop_runtime_start",
80272b8c 3642 "GOMP_parallel_sections",
43819297 3643 "GOMP_parallel_sections_start",
f9dffbf0
AM
3644 /* libgo */
3645 "__go_go",
9e69ed50
AM
3646 };
3647
e2458743
AM
3648 if (parameters->options().shared())
3649 thread_safe = true;
3650 else
9e69ed50 3651 {
e2458743
AM
3652 for (unsigned int i = 0;
3653 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3654 i++)
3655 {
3656 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3657 thread_safe = (sym != NULL
3658 && sym->in_reg()
3659 && sym->in_real_elf());
3660 if (thread_safe)
3661 break;
3662 }
9e69ed50 3663 }
ec661b9d 3664 }
9e69ed50 3665 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3666 }
3667
3668 if (pass == 1)
3669 {
3670 this->stub_group_size_ = parameters->options().stub_group_size();
3671 bool no_size_errors = true;
3672 if (this->stub_group_size_ == 1)
3673 this->stub_group_size_ = 0x1c00000;
3674 else if (this->stub_group_size_ == -1)
3675 this->stub_group_size_ = -0x1e00000;
3676 else
3677 no_size_errors = false;
3678 this->group_sections(layout, task, no_size_errors);
3679 }
3680 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3681 {
3682 this->branch_lookup_table_.clear();
3683 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3684 p != this->stub_tables_.end();
3685 ++p)
3686 {
3687 (*p)->clear_stubs(true);
3688 }
3689 this->stub_tables_.clear();
3690 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3691 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3692 program_name, this->stub_group_size_);
3693 this->group_sections(layout, task, true);
ec661b9d
AM
3694 }
3695
3696 // We need address of stub tables valid for make_stub.
3697 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3698 p != this->stub_tables_.end();
3699 ++p)
3700 {
3701 const Powerpc_relobj<size, big_endian>* object
3702 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3703 Address off = object->get_output_section_offset((*p)->shndx());
3704 gold_assert(off != invalid_address);
3705 Output_section* os = (*p)->output_section();
3706 (*p)->set_address_and_size(os, off);
3707 }
3708
9e69ed50
AM
3709 if (pass != 1)
3710 {
3711 // Clear plt call stubs, long branch stubs and branch lookup table.
3712 prev_brlt_size = this->branch_lookup_table_.size();
3713 this->branch_lookup_table_.clear();
3714 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3715 p != this->stub_tables_.end();
3716 ++p)
3717 {
a3e60ddb 3718 (*p)->clear_stubs(false);
9e69ed50
AM
3719 }
3720 }
3721
3722 // Build all the stubs.
a3e60ddb 3723 this->relax_failed_ = false;
ec661b9d
AM
3724 Stub_table<size, big_endian>* ifunc_stub_table
3725 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3726 Stub_table<size, big_endian>* one_stub_table
3727 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3728 for (typename Branches::const_iterator b = this->branch_info_.begin();
3729 b != this->branch_info_.end();
3730 b++)
3731 {
a3e60ddb
AM
3732 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3733 && !this->relax_failed_)
3734 {
3735 this->relax_failed_ = true;
3736 this->relax_fail_count_++;
3737 if (this->relax_fail_count_ < 3)
3738 return true;
3739 }
ec661b9d 3740 }
32f59844
AM
3741 bool do_resize = false;
3742 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3743 p != this->stub_tables_.end();
3744 ++p)
3745 if ((*p)->need_resize())
3746 {
3747 do_resize = true;
3748 break;
3749 }
3750 if (do_resize)
3751 {
3752 this->branch_lookup_table_.clear();
3753 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3754 p != this->stub_tables_.end();
3755 ++p)
3756 (*p)->set_resizing(true);
3757 for (typename Branches::const_iterator b = this->branch_info_.begin();
3758 b != this->branch_info_.end();
3759 b++)
3760 {
3761 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3762 && !this->relax_failed_)
3763 {
3764 this->relax_failed_ = true;
3765 this->relax_fail_count_++;
3766 if (this->relax_fail_count_ < 3)
3767 return true;
3768 }
3769 }
3770 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3771 p != this->stub_tables_.end();
3772 ++p)
3773 (*p)->set_resizing(false);
3774 }
ec661b9d 3775
9e69ed50 3776 // Did anything change size?
ec661b9d
AM
3777 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3778 bool again = num_huge_branches != prev_brlt_size;
3779 if (size == 64 && num_huge_branches != 0)
3780 this->make_brlt_section(layout);
3781 if (size == 64 && again)
3782 this->brlt_section_->set_current_size(num_huge_branches);
3783
be897fb7
AM
3784 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3785 p != this->stub_tables_.rend();
3786 ++p)
3787 (*p)->remove_eh_frame(layout);
3788
3789 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3790 p != this->stub_tables_.end();
3791 ++p)
3792 (*p)->add_eh_frame(layout);
3793
ec661b9d
AM
3794 typedef Unordered_set<Output_section*> Output_sections;
3795 Output_sections os_need_update;
3796 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3797 p != this->stub_tables_.end();
3798 ++p)
3799 {
3800 if ((*p)->size_update())
3801 {
3802 again = true;
3803 os_need_update.insert((*p)->output_section());
3804 }
3805 }
3806
9e69ed50
AM
3807 // Set output section offsets for all input sections in an output
3808 // section that just changed size. Anything past the stubs will
3809 // need updating.
ec661b9d
AM
3810 for (typename Output_sections::iterator p = os_need_update.begin();
3811 p != os_need_update.end();
3812 p++)
3813 {
3814 Output_section* os = *p;
3815 Address off = 0;
3816 typedef Output_section::Input_section_list Input_section_list;
3817 for (Input_section_list::const_iterator i = os->input_sections().begin();
3818 i != os->input_sections().end();
3819 ++i)
3820 {
3821 off = align_address(off, i->addralign());
3822 if (i->is_input_section() || i->is_relaxed_input_section())
3823 i->relobj()->set_section_offset(i->shndx(), off);
3824 if (i->is_relaxed_input_section())
3825 {
3826 Stub_table<size, big_endian>* stub_table
3827 = static_cast<Stub_table<size, big_endian>*>(
3828 i->relaxed_input_section());
6395d38b
HS
3829 Address stub_table_size = stub_table->set_address_and_size(os, off);
3830 off += stub_table_size;
3831 // After a few iterations, set current stub table size
3832 // as min size threshold, so later stub tables can only
3833 // grow in size.
3834 if (pass >= 4)
3835 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3836 }
3837 else
3838 off += i->data_size();
3839 }
6830ee24
AM
3840 // If .branch_lt is part of this output section, then we have
3841 // just done the offset adjustment.
ec661b9d
AM
3842 os->clear_section_offsets_need_adjustment();
3843 }
3844
3845 if (size == 64
3846 && !again
3847 && num_huge_branches != 0
3848 && parameters->options().output_is_position_independent())
3849 {
3850 // Fill in the BRLT relocs.
06f30c9d 3851 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3852 for (typename Branch_lookup_table::const_iterator p
3853 = this->branch_lookup_table_.begin();
3854 p != this->branch_lookup_table_.end();
3855 ++p)
3856 {
3857 this->brlt_section_->add_reloc(p->first, p->second);
3858 }
06f30c9d 3859 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3860 }
590b87ff
AM
3861
3862 if (!again
3863 && (parameters->options().user_set_emit_stub_syms()
3864 ? parameters->options().emit_stub_syms()
3865 : (size == 64
3866 || parameters->options().output_is_position_independent()
3867 || parameters->options().emit_relocs())))
3868 {
3869 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3870 p != this->stub_tables_.end();
3871 ++p)
3872 (*p)->define_stub_syms(symtab);
3873
3874 if (this->glink_ != NULL)
3875 {
9e390558 3876 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3877 Address value = -stub_size;
3878 if (size == 64)
3879 {
3880 value = 8;
3881 stub_size -= 8;
3882 }
3883 this->define_local(symtab, "__glink_PLTresolve",
3884 this->glink_, value, stub_size);
3885
3886 if (size != 64)
3887 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3888 }
3889 }
3890
ec661b9d
AM
3891 return again;
3892}
3893
9d5781f8
AM
3894template<int size, bool big_endian>
3895void
3896Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3897 unsigned char* oview,
3898 uint64_t* paddress,
3899 off_t* plen) const
3900{
3901 uint64_t address = plt->address();
3902 off_t len = plt->data_size();
3903
3904 if (plt == this->glink_)
3905 {
3906 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3907 if (len == 0)
3908 {
3909 gold_assert(parameters->doing_static_link());
3910 // Static linking may need stubs, to support ifunc and long
3911 // branches. We need to create an output section for
3912 // .eh_frame early in the link process, to have a place to
3913 // attach stub .eh_frame info. We also need to have
3914 // registered a CIE that matches the stub CIE. Both of
3915 // these requirements are satisfied by creating an FDE and
3916 // CIE for .glink, even though static linking will leave
3917 // .glink zero length.
3918 // ??? Hopefully generating an FDE with a zero address range
3919 // won't confuse anything that consumes .eh_frame info.
3920 }
3921 else if (size == 64)
9d5781f8
AM
3922 {
3923 // There is one word before __glink_PLTresolve
3924 address += 8;
3925 len -= 8;
3926 }
3927 else if (parameters->options().output_is_position_independent())
3928 {
3929 // There are two FDEs for a position independent glink.
3930 // The first covers the branch table, the second
3931 // __glink_PLTresolve at the end of glink.
9e390558 3932 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 3933 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3934 len -= resolve_size;
3935 else
3936 {
3937 address += len - resolve_size;
3938 len = resolve_size;
3939 }
3940 }
3941 }
3942 else
3943 {
3944 // Must be a stub table.
3945 const Stub_table<size, big_endian>* stub_table
3946 = static_cast<const Stub_table<size, big_endian>*>(plt);
3947 uint64_t stub_address = stub_table->stub_address();
3948 len -= stub_address - address;
3949 address = stub_address;
3950 }
3951
3952 *paddress = address;
3953 *plen = len;
3954}
3955
42cacb20
DE
3956// A class to handle the PLT data.
3957
3958template<int size, bool big_endian>
cf43a2fe 3959class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3960{
3961 public:
3962 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3963 size, big_endian> Reloc_section;
3964
e5d5f5ed
AM
3965 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3966 Reloc_section* plt_rel,
e5d5f5ed
AM
3967 const char* name)
3968 : Output_section_data_build(size == 32 ? 4 : 8),
3969 rel_(plt_rel),
3970 targ_(targ),
e5d5f5ed
AM
3971 name_(name)
3972 { }
42cacb20
DE
3973
3974 // Add an entry to the PLT.
03e25981 3975 void
cf43a2fe 3976 add_entry(Symbol*);
42cacb20 3977
03e25981 3978 void
e5d5f5ed
AM
3979 add_ifunc_entry(Symbol*);
3980
2d7ad24e
AM
3981 void
3982 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3983
03e25981 3984 void
e5d5f5ed
AM
3985 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3986
42cacb20 3987 // Return the .rela.plt section data.
e5d5f5ed 3988 Reloc_section*
cf43a2fe
AM
3989 rel_plt() const
3990 {
42cacb20
DE
3991 return this->rel_;
3992 }
3993
0e70b911
CC
3994 // Return the number of PLT entries.
3995 unsigned int
3996 entry_count() const
d83ce4e3 3997 {
b3ccdeb5
AM
3998 if (this->current_data_size() == 0)
3999 return 0;
b4f7960d
AM
4000 return ((this->current_data_size() - this->first_plt_entry_offset())
4001 / this->plt_entry_size());
d83ce4e3 4002 }
0e70b911 4003
42cacb20 4004 protected:
42cacb20 4005 void
cf43a2fe 4006 do_adjust_output_section(Output_section* os)
42cacb20 4007 {
cf43a2fe 4008 os->set_entsize(0);
42cacb20
DE
4009 }
4010
6ce78956
AM
4011 // Write to a map file.
4012 void
4013 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4014 { mapfile->print_output_data(this, this->name_); }
6ce78956 4015
cf43a2fe 4016 private:
b4f7960d
AM
4017 // Return the offset of the first non-reserved PLT entry.
4018 unsigned int
4019 first_plt_entry_offset() const
4020 {
2d7ad24e
AM
4021 // IPLT and LPLT have no reserved entry.
4022 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4023 return 0;
4024 return this->targ_->first_plt_entry_offset();
4025 }
4026
4027 // Return the size of each PLT entry.
4028 unsigned int
4029 plt_entry_size() const
4030 {
4031 return this->targ_->plt_entry_size();
4032 }
cf43a2fe 4033
42cacb20
DE
4034 // Write out the PLT data.
4035 void
4036 do_write(Output_file*);
4037
4038 // The reloc section.
4039 Reloc_section* rel_;
cf43a2fe
AM
4040 // Allows access to .glink for do_write.
4041 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
4042 // What to report in map file.
4043 const char *name_;
42cacb20
DE
4044};
4045
e5d5f5ed 4046// Add an entry to the PLT.
42cacb20
DE
4047
4048template<int size, bool big_endian>
03e25981 4049void
e5d5f5ed 4050Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 4051{
e5d5f5ed
AM
4052 if (!gsym->has_plt_offset())
4053 {
ec661b9d 4054 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4055 if (off == 0)
4056 off += this->first_plt_entry_offset();
4057 gsym->set_plt_offset(off);
4058 gsym->set_needs_dynsym_entry();
4059 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4060 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 4061 off += this->plt_entry_size();
e5d5f5ed
AM
4062 this->set_current_data_size(off);
4063 }
42cacb20
DE
4064}
4065
e5d5f5ed 4066// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4067
4068template<int size, bool big_endian>
03e25981 4069void
e5d5f5ed 4070Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4071{
cf43a2fe
AM
4072 if (!gsym->has_plt_offset())
4073 {
ec661b9d 4074 section_size_type off = this->current_data_size();
cf43a2fe 4075 gsym->set_plt_offset(off);
e5d5f5ed 4076 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4077 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4078 dynrel = elfcpp::R_PPC64_JMP_IREL;
4079 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4080 off += this->plt_entry_size();
e5d5f5ed
AM
4081 this->set_current_data_size(off);
4082 }
4083}
4084
2d7ad24e
AM
4085// Add an entry for a local symbol to the PLT.
4086
4087template<int size, bool big_endian>
4088void
4089Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4090 Sized_relobj_file<size, big_endian>* relobj,
4091 unsigned int local_sym_index)
4092{
4093 if (!relobj->local_has_plt_offset(local_sym_index))
4094 {
4095 section_size_type off = this->current_data_size();
4096 relobj->set_local_plt_offset(local_sym_index, off);
4097 if (this->rel_)
4098 {
4099 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4100 if (size == 64 && this->targ_->abiversion() < 2)
4101 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4102 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4103 dynrel, this, off, 0);
4104 }
4105 off += this->plt_entry_size();
4106 this->set_current_data_size(off);
4107 }
4108}
4109
e5d5f5ed
AM
4110// Add an entry for a local ifunc symbol to the IPLT.
4111
4112template<int size, bool big_endian>
03e25981 4113void
e5d5f5ed
AM
4114Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4115 Sized_relobj_file<size, big_endian>* relobj,
4116 unsigned int local_sym_index)
4117{
4118 if (!relobj->local_has_plt_offset(local_sym_index))
4119 {
ec661b9d 4120 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4121 relobj->set_local_plt_offset(local_sym_index, off);
4122 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4123 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4124 dynrel = elfcpp::R_PPC64_JMP_IREL;
4125 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4126 this, off, 0);
b4f7960d 4127 off += this->plt_entry_size();
cf43a2fe
AM
4128 this->set_current_data_size(off);
4129 }
42cacb20
DE
4130}
4131
dd93cd0a 4132static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4133static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4134static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4135static const uint32_t add_3_3_2 = 0x7c631214;
4136static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4137static const uint32_t add_3_12_2 = 0x7c6c1214;
4138static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4139static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4140static const uint32_t add_11_2_11 = 0x7d625a14;
4141static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4142static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4143static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4144static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4145static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4146static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4147static const uint32_t addi_12_1 = 0x39810000;
32f59844 4148static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4149static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4150static const uint32_t addis_0_2 = 0x3c020000;
4151static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4152static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4153static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4154static const uint32_t addis_11_11 = 0x3d6b0000;
4155static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4156static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4157static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4158static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4159static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4160static const uint32_t b = 0x48000000;
4161static const uint32_t bcl_20_31 = 0x429f0005;
4162static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4163static const uint32_t bctrl = 0x4e800421;
4164static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4165static const uint32_t blr = 0x4e800020;
9e69ed50 4166static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4167static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4168static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4169static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4170static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4171static const uint32_t cror_15_15_15 = 0x4def7b82;
4172static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29
AM
4173static const uint32_t ld_0_1 = 0xe8010000;
4174static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4175static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4176static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4177static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4178static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4179static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4180static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4181static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4182static const uint32_t ld_11_11 = 0xe96b0000;
4183static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4184static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4185static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4186static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4187static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4188static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4189static const uint32_t li_0_0 = 0x38000000;
e4dff765 4190static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4191static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4192static const uint32_t lis_0 = 0x3c000000;
549dba71 4193static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4194static const uint32_t lis_11 = 0x3d600000;
4195static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4196static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4197static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4198static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4199static const uint32_t lwz_11_11 = 0x816b0000;
4200static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4201static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4202static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4203static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4204static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4205static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4206static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4207static const uint32_t mr_0_3 = 0x7c601b78;
4208static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4209static const uint32_t mtctr_0 = 0x7c0903a6;
4210static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4211static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4212static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4213static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4214static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4215static const uint32_t nop = 0x60000000;
c9269dff 4216static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4217static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4218static const uint32_t ori_12_12_0 = 0x618c0000;
4219static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4220static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4221static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4222static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4223static const uint32_t std_0_1 = 0xf8010000;
4224static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4225static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4226static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4227static const uint32_t stfd_0_1 = 0xd8010000;
4228static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4229static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4230static const uint32_t sub_12_12_11 = 0x7d8b6050;
4231static const uint32_t xor_2_12_12 = 0x7d826278;
4232static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4233
e4dff765
AM
4234static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4235static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4236static const uint64_t pnop = 0x0700000000000000ULL;
4237
42cacb20
DE
4238// Write out the PLT.
4239
4240template<int size, bool big_endian>
4241void
4242Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4243{
2d7ad24e 4244 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4245 {
ec661b9d 4246 const section_size_type offset = this->offset();
cf43a2fe
AM
4247 const section_size_type oview_size
4248 = convert_to_section_size_type(this->data_size());
4249 unsigned char* const oview = of->get_output_view(offset, oview_size);
4250 unsigned char* pov = oview;
4251 unsigned char* endpov = oview + oview_size;
4252
e5d5f5ed 4253 // The address of the .glink branch table
cf43a2fe
AM
4254 const Output_data_glink<size, big_endian>* glink
4255 = this->targ_->glink_section();
ec661b9d 4256 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4257
4258 while (pov < endpov)
4259 {
4260 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4261 pov += 4;
4262 branch_tab += 4;
4263 }
4264
4265 of->write_output_view(offset, oview_size, oview);
4266 }
4267}
4268
4269// Create the PLT section.
4270
4271template<int size, bool big_endian>
4272void
40b469d7
AM
4273Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4274 Layout* layout)
cf43a2fe
AM
4275{
4276 if (this->plt_ == NULL)
4277 {
40b469d7
AM
4278 if (this->got_ == NULL)
4279 this->got_section(symtab, layout);
4280
cf43a2fe
AM
4281 if (this->glink_ == NULL)
4282 make_glink_section(layout);
4283
4284 // Ensure that .rela.dyn always appears before .rela.plt This is
4285 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4286 // needs to include .rela.plt in its range.
cf43a2fe
AM
4287 this->rela_dyn_section(layout);
4288
e5d5f5ed
AM
4289 Reloc_section* plt_rel = new Reloc_section(false);
4290 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4291 elfcpp::SHF_ALLOC, plt_rel,
4292 ORDER_DYNAMIC_PLT_RELOCS, false);
4293 this->plt_
4294 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 4295 "** PLT");
cf43a2fe
AM
4296 layout->add_output_section_data(".plt",
4297 (size == 32
4298 ? elfcpp::SHT_PROGBITS
4299 : elfcpp::SHT_NOBITS),
4300 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4301 this->plt_,
4302 (size == 32
4303 ? ORDER_SMALL_DATA
4304 : ORDER_SMALL_BSS),
4305 false);
3254d32c
AM
4306
4307 Output_section* rela_plt_os = plt_rel->output_section();
4308 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4309 }
4310}
4311
e5d5f5ed
AM
4312// Create the IPLT section.
4313
4314template<int size, bool big_endian>
4315void
40b469d7
AM
4316Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4317 Layout* layout)
e5d5f5ed
AM
4318{
4319 if (this->iplt_ == NULL)
4320 {
40b469d7 4321 this->make_plt_section(symtab, layout);
2d7ad24e 4322 this->make_lplt_section(layout);
e5d5f5ed
AM
4323
4324 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4325 if (this->rela_dyn_->output_section())
4326 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
4327 this->iplt_
4328 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 4329 "** IPLT");
6528b6eb
AM
4330 if (this->plt_->output_section())
4331 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4332 }
4333}
4334
2d7ad24e
AM
4335// Create the LPLT section.
4336
4337template<int size, bool big_endian>
4338void
4339Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4340{
4341 if (this->lplt_ == NULL)
4342 {
4343 Reloc_section* lplt_rel = NULL;
4344 if (parameters->options().output_is_position_independent())
4345 {
4346 lplt_rel = new Reloc_section(false);
4347 this->rela_dyn_section(layout);
4348 if (this->rela_dyn_->output_section())
4349 this->rela_dyn_->output_section()
4350 ->add_output_section_data(lplt_rel);
4351 }
4352 this->lplt_
4353 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4354 "** LPLT");
4355 this->make_brlt_section(layout);
4356 if (this->brlt_section_ && this->brlt_section_->output_section())
4357 this->brlt_section_->output_section()
4358 ->add_output_section_data(this->lplt_);
4359 else
4360 layout->add_output_section_data(".branch_lt",
4361 elfcpp::SHT_PROGBITS,
4362 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4363 this->lplt_,
4364 ORDER_RELRO,
4365 true);
4366 }
4367}
4368
ec661b9d 4369// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4370
4371template<int size, bool big_endian>
ec661b9d 4372class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4373{
4374 public:
ec661b9d
AM
4375 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4376 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4377 size, big_endian> Reloc_section;
c9269dff 4378
ec661b9d
AM
4379 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4380 Reloc_section* brlt_rel)
4381 : Output_section_data_build(size == 32 ? 4 : 8),
4382 rel_(brlt_rel),
4383 targ_(targ)
4384 { }
cf43a2fe 4385
06f30c9d
CC
4386 void
4387 reset_brlt_sizes()
4388 {
4389 this->reset_data_size();
4390 this->rel_->reset_data_size();
4391 }
4392
4393 void
4394 finalize_brlt_sizes()
4395 {
4396 this->finalize_data_size();
4397 this->rel_->finalize_data_size();
4398 }
4399
ec661b9d 4400 // Add a reloc for an entry in the BRLT.
cf43a2fe 4401 void
ec661b9d
AM
4402 add_reloc(Address to, unsigned int off)
4403 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 4404
ec661b9d 4405 // Update section and reloc section size.
e5d5f5ed 4406 void
ec661b9d
AM
4407 set_current_size(unsigned int num_branches)
4408 {
4409 this->reset_address_and_file_offset();
4410 this->set_current_data_size(num_branches * 16);
4411 this->finalize_data_size();
4412 Output_section* os = this->output_section();
4413 os->set_section_offsets_need_adjustment();
4414 if (this->rel_ != NULL)
4415 {
0e123f69 4416 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
4417 this->rel_->reset_address_and_file_offset();
4418 this->rel_->set_current_data_size(num_branches * reloc_size);
4419 this->rel_->finalize_data_size();
4420 Output_section* os = this->rel_->output_section();
4421 os->set_section_offsets_need_adjustment();
4422 }
4423 }
cf43a2fe 4424
ec661b9d
AM
4425 protected:
4426 void
4427 do_adjust_output_section(Output_section* os)
4428 {
4429 os->set_entsize(0);
4430 }
e5d5f5ed 4431
ec661b9d
AM
4432 // Write to a map file.
4433 void
4434 do_print_to_mapfile(Mapfile* mapfile) const
4435 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4436
ec661b9d
AM
4437 private:
4438 // Write out the BRLT data.
4439 void
4440 do_write(Output_file*);
c9824451 4441
ec661b9d
AM
4442 // The reloc section.
4443 Reloc_section* rel_;
4444 Target_powerpc<size, big_endian>* targ_;
4445};
cf43a2fe 4446
ec661b9d
AM
4447// Make the branch lookup table section.
4448
4449template<int size, bool big_endian>
4450void
4451Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4452{
4453 if (size == 64 && this->brlt_section_ == NULL)
4454 {
4455 Reloc_section* brlt_rel = NULL;
4456 bool is_pic = parameters->options().output_is_position_independent();
4457 if (is_pic)
4458 {
54483898
AM
4459 // When PIC we can't fill in .branch_lt but must initialise at
4460 // runtime via dynamic relocations.
ec661b9d
AM
4461 this->rela_dyn_section(layout);
4462 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4463 if (this->rela_dyn_->output_section())
4464 this->rela_dyn_->output_section()
4465 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4466 }
4467 this->brlt_section_
4468 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4469 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4470 this->plt_->output_section()
4471 ->add_output_section_data(this->brlt_section_);
4472 else
6830ee24 4473 layout->add_output_section_data(".branch_lt",
54483898 4474 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4475 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4476 this->brlt_section_,
54483898
AM
4477 ORDER_RELRO,
4478 true);
ec661b9d
AM
4479 }
4480}
4481
6830ee24 4482// Write out .branch_lt when non-PIC.
ec661b9d
AM
4483
4484template<int size, bool big_endian>
4485void
4486Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4487{
4488 if (size == 64 && !parameters->options().output_is_position_independent())
4489 {
4490 const section_size_type offset = this->offset();
4491 const section_size_type oview_size
4492 = convert_to_section_size_type(this->data_size());
4493 unsigned char* const oview = of->get_output_view(offset, oview_size);
4494
4495 this->targ_->write_branch_lookup_table(oview);
4496 of->write_output_view(offset, oview_size, oview);
4497 }
4498}
4499
9e69ed50
AM
4500static inline uint32_t
4501l(uint32_t a)
4502{
4503 return a & 0xffff;
4504}
4505
4506static inline uint32_t
4507hi(uint32_t a)
4508{
4509 return l(a >> 16);
4510}
4511
4512static inline uint32_t
4513ha(uint32_t a)
4514{
4515 return hi(a + 0x8000);
4516}
4517
e4dff765
AM
4518static inline uint64_t
4519d34(uint64_t v)
4520{
4521 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4522}
4523
4524static inline uint64_t
4525ha34(uint64_t v)
4526{
4527 return (v + (1ULL << 33)) >> 34;
4528}
4529
9d5781f8
AM
4530template<int size>
4531struct Eh_cie
4532{
4533 static const unsigned char eh_frame_cie[12];
4534};
4535
4536template<int size>
4537const unsigned char Eh_cie<size>::eh_frame_cie[] =
4538{
4539 1, // CIE version.
4540 'z', 'R', 0, // Augmentation string.
4541 4, // Code alignment.
4542 0x80 - size / 8 , // Data alignment.
4543 65, // RA reg.
4544 1, // Augmentation size.
4545 (elfcpp::DW_EH_PE_pcrel
4546 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4547 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4548};
4549
b4f7960d
AM
4550// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4551static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4552{
4553 0, 0, 0, 0, // Replaced with offset to .glink.
4554 0, 0, 0, 0, // Replaced with size of .glink.
4555 0, // Augmentation size.
4556 elfcpp::DW_CFA_advance_loc + 1,
4557 elfcpp::DW_CFA_register, 65, 12,
15a3a14f 4558 elfcpp::DW_CFA_advance_loc + 5,
9d5781f8
AM
4559 elfcpp::DW_CFA_restore_extended, 65
4560};
4561
b4f7960d
AM
4562// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4563static const unsigned char glink_eh_frame_fde_64v2[] =
4564{
4565 0, 0, 0, 0, // Replaced with offset to .glink.
4566 0, 0, 0, 0, // Replaced with size of .glink.
4567 0, // Augmentation size.
4568 elfcpp::DW_CFA_advance_loc + 1,
4569 elfcpp::DW_CFA_register, 65, 0,
15a3a14f 4570 elfcpp::DW_CFA_advance_loc + 7,
b4f7960d
AM
4571 elfcpp::DW_CFA_restore_extended, 65
4572};
4573
9d5781f8
AM
4574// Describe __glink_PLTresolve use of LR, 32-bit version.
4575static const unsigned char glink_eh_frame_fde_32[] =
4576{
4577 0, 0, 0, 0, // Replaced with offset to .glink.
4578 0, 0, 0, 0, // Replaced with size of .glink.
4579 0, // Augmentation size.
4580 elfcpp::DW_CFA_advance_loc + 2,
4581 elfcpp::DW_CFA_register, 65, 0,
4582 elfcpp::DW_CFA_advance_loc + 4,
4583 elfcpp::DW_CFA_restore_extended, 65
4584};
4585
4586static const unsigned char default_fde[] =
4587{
4588 0, 0, 0, 0, // Replaced with offset to stubs.
4589 0, 0, 0, 0, // Replaced with size of stubs.
4590 0, // Augmentation size.
4591 elfcpp::DW_CFA_nop, // Pad.
4592 elfcpp::DW_CFA_nop,
4593 elfcpp::DW_CFA_nop
4594};
4595
9e69ed50
AM
4596template<bool big_endian>
4597static inline void
4598write_insn(unsigned char* p, uint32_t v)
4599{
4600 elfcpp::Swap<32, big_endian>::writeval(p, v);
4601}
4602
691d2e9a
AM
4603template<int size>
4604static inline unsigned int
4605param_plt_align()
4606{
4607 if (!parameters->options().user_set_plt_align())
4608 return size == 64 ? 32 : 8;
4609 return 1 << parameters->options().plt_align();
4610}
4611
ec661b9d
AM
4612// Stub_table holds information about plt and long branch stubs.
4613// Stubs are built in an area following some input section determined
4614// by group_sections(). This input section is converted to a relaxed
4615// input section allowing it to be resized to accommodate the stubs
4616
4617template<int size, bool big_endian>
4618class Stub_table : public Output_relaxed_input_section
4619{
4620 public:
7e57d19e
AM
4621 struct Plt_stub_ent
4622 {
4623 Plt_stub_ent(unsigned int off, unsigned int indx)
32f59844 4624 : off_(off), indx_(indx), iter_(0), notoc_(0), r2save_(0), localentry0_(0)
7e57d19e
AM
4625 { }
4626
4627 unsigned int off_;
32f59844
AM
4628 unsigned int indx_ : 28;
4629 unsigned int iter_ : 1;
4630 unsigned int notoc_ : 1;
7e57d19e 4631 unsigned int r2save_ : 1;
7ee7ff70 4632 unsigned int localentry0_ : 1;
7e57d19e 4633 };
32f59844
AM
4634 struct Branch_stub_ent
4635 {
4636 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4637 : off_(off), iter_(false), notoc_(notoc), save_res_(save_res)
4638 { }
4639
4640 unsigned int off_;
4641 bool iter_;
4642 bool notoc_;
4643 bool save_res_;
4644 };
ec661b9d
AM
4645 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4646 static const Address invalid_address = static_cast<Address>(0) - 1;
4647
a3e60ddb
AM
4648 Stub_table(Target_powerpc<size, big_endian>* targ,
4649 Output_section* output_section,
590b87ff
AM
4650 const Output_section::Input_section* owner,
4651 uint32_t id)
a3e60ddb
AM
4652 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4653 owner->relobj()
4654 ->section_addralign(owner->shndx())),
ec661b9d 4655 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4656 orig_data_size_(owner->current_data_size()),
4657 plt_size_(0), last_plt_size_(0),
6395d38b 4658 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4659 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4660 uniq_(id)
a3e60ddb
AM
4661 {
4662 this->set_output_section(output_section);
ec661b9d 4663
a3e60ddb
AM
4664 std::vector<Output_relaxed_input_section*> new_relaxed;
4665 new_relaxed.push_back(this);
4666 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4667 }
ec661b9d
AM
4668
4669 // Add a plt call stub.
a3e60ddb
AM
4670 bool
4671 add_plt_call_entry(Address,
4672 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4673 const Symbol*,
4674 unsigned int,
7e57d19e
AM
4675 Address,
4676 bool);
ec661b9d 4677
a3e60ddb
AM
4678 bool
4679 add_plt_call_entry(Address,
4680 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4681 unsigned int,
4682 unsigned int,
7e57d19e
AM
4683 Address,
4684 bool);
ec661b9d
AM
4685
4686 // Find a given plt call stub.
7e57d19e 4687 const Plt_stub_ent*
ec661b9d
AM
4688 find_plt_call_entry(const Symbol*) const;
4689
7e57d19e 4690 const Plt_stub_ent*
ec661b9d
AM
4691 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4692 unsigned int) const;
4693
7e57d19e 4694 const Plt_stub_ent*
ec661b9d
AM
4695 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4696 const Symbol*,
4697 unsigned int,
4698 Address) const;
4699
7e57d19e 4700 const Plt_stub_ent*
ec661b9d
AM
4701 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4702 unsigned int,
4703 unsigned int,
4704 Address) const;
4705
4706 // Add a long branch stub.
a3e60ddb
AM
4707 bool
4708 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
d49044c7 4709 unsigned int, Address, Address, bool);
ec661b9d 4710
32f59844 4711 const Branch_stub_ent*
9d5781f8
AM
4712 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4713 Address) const;
ec661b9d 4714
a3e60ddb
AM
4715 bool
4716 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4717 {
32f59844 4718 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4719 if (max_branch_offset == 0)
4720 return true;
4721 gold_assert(from != invalid_address);
4722 Address loc = off + this->stub_address();
4723 return loc - from + max_branch_offset < 2 * max_branch_offset;
4724 }
4725
ec661b9d 4726 void
a3e60ddb 4727 clear_stubs(bool all)
cf43a2fe 4728 {
9e69ed50
AM
4729 this->plt_call_stubs_.clear();
4730 this->plt_size_ = 0;
ec661b9d
AM
4731 this->long_branch_stubs_.clear();
4732 this->branch_size_ = 0;
d49044c7 4733 this->need_save_res_ = false;
a3e60ddb
AM
4734 if (all)
4735 {
4736 this->last_plt_size_ = 0;
4737 this->last_branch_size_ = 0;
4738 }
cf43a2fe
AM
4739 }
4740
32f59844
AM
4741 bool
4742 need_resize() const
4743 { return need_resize_; }
4744
4745 void
4746 set_resizing(bool val)
4747 {
4748 this->resizing_ = val;
4749 if (val)
4750 {
4751 this->need_resize_ = false;
4752 this->plt_size_ = 0;
4753 this->branch_size_ = 0;
4754 this->need_save_res_ = false;
4755 }
4756 }
4757
ec661b9d
AM
4758 Address
4759 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4760 {
ec661b9d
AM
4761 Address start_off = off;
4762 off += this->orig_data_size_;
4763 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4764 if (this->need_save_res_)
4765 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4766 if (my_size != 0)
4767 off = align_address(off, this->stub_align());
4768 // Include original section size and alignment padding in size
4769 my_size += off - start_off;
6395d38b
HS
4770 // Ensure new size is always larger than min size
4771 // threshold. Alignment requirement is included in "my_size", so
4772 // increase "my_size" does not invalidate alignment.
4773 if (my_size < this->min_size_threshold_)
4774 my_size = this->min_size_threshold_;
ec661b9d
AM
4775 this->reset_address_and_file_offset();
4776 this->set_current_data_size(my_size);
4777 this->set_address_and_file_offset(os->address() + start_off,
4778 os->offset() + start_off);
4779 return my_size;
cf43a2fe
AM
4780 }
4781
ec661b9d 4782 Address
9d5781f8 4783 stub_address() const
ec661b9d
AM
4784 {
4785 return align_address(this->address() + this->orig_data_size_,
4786 this->stub_align());
4787 }
4788
4789 Address
9d5781f8 4790 stub_offset() const
ec661b9d
AM
4791 {
4792 return align_address(this->offset() + this->orig_data_size_,
4793 this->stub_align());
4794 }
4795
4796 section_size_type
4797 plt_size() const
4798 { return this->plt_size_; }
4799
32f59844
AM
4800 section_size_type
4801 branch_size() const
4802 { return this->branch_size_; }
4803
590b87ff
AM
4804 void
4805 set_min_size_threshold(Address min_size)
6395d38b
HS
4806 { this->min_size_threshold_ = min_size; }
4807
590b87ff
AM
4808 void
4809 define_stub_syms(Symbol_table*);
4810
ec661b9d
AM
4811 bool
4812 size_update()
4813 {
4814 Output_section* os = this->output_section();
4815 if (os->addralign() < this->stub_align())
4816 {
4817 os->set_addralign(this->stub_align());
4818 // FIXME: get rid of the insane checkpointing.
4819 // We can't increase alignment of the input section to which
4820 // stubs are attached; The input section may be .init which
4821 // is pasted together with other .init sections to form a
4822 // function. Aligning might insert zero padding resulting in
4823 // sigill. However we do need to increase alignment of the
4824 // output section so that the align_address() on offset in
4825 // set_address_and_size() adds the same padding as the
4826 // align_address() on address in stub_address().
4827 // What's more, we need this alignment for the layout done in
4828 // relaxation_loop_body() so that the output section starts at
4829 // a suitably aligned address.
4830 os->checkpoint_set_addralign(this->stub_align());
4831 }
9e69ed50
AM
4832 if (this->last_plt_size_ != this->plt_size_
4833 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4834 {
9e69ed50
AM
4835 this->last_plt_size_ = this->plt_size_;
4836 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4837 return true;
4838 }
4839 return false;
4840 }
4841
34e0882b
AM
4842 // Add .eh_frame info for this stub section.
4843 void
4844 add_eh_frame(Layout* layout);
be897fb7 4845
34e0882b 4846 // Remove .eh_frame info for this stub section.
be897fb7 4847 void
34e0882b 4848 remove_eh_frame(Layout* layout);
9d5781f8 4849
ec661b9d
AM
4850 Target_powerpc<size, big_endian>*
4851 targ() const
4852 { return targ_; }
6ce78956 4853
cf43a2fe 4854 private:
bdab445c
AM
4855 class Plt_stub_key;
4856 class Plt_stub_key_hash;
bdab445c
AM
4857 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4858 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
4859 class Branch_stub_key;
4860 class Branch_stub_key_hash;
4861 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4862 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
4863
4864 // Alignment of stub section.
ec661b9d 4865 unsigned int
9e69ed50
AM
4866 stub_align() const
4867 {
691d2e9a 4868 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
4869 unsigned int user_align = 1 << parameters->options().plt_align();
4870 return std::max(user_align, min_align);
4871 }
cf43a2fe 4872
91c2b899
AM
4873 // Return the plt offset for the given call stub.
4874 Address
08be3224
AM
4875 plt_off(typename Plt_stub_entries::const_iterator p,
4876 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
4877 {
4878 const Symbol* gsym = p->first.sym_;
4879 if (gsym != NULL)
08be3224 4880 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
4881 else
4882 {
91c2b899
AM
4883 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4884 unsigned int local_sym_index = p->first.locsym_;
08be3224 4885 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
4886 }
4887 }
4888
9e69ed50 4889 // Size of a given plt call stub.
ec661b9d 4890 unsigned int
32f59844 4891 plt_call_size(typename Plt_stub_entries::const_iterator p) const;
34e0882b
AM
4892
4893 unsigned int
4894 plt_call_align(unsigned int bytes) const
4895 {
691d2e9a 4896 unsigned int align = param_plt_align<size>();
9e390558 4897 return (bytes + align - 1) & -align;
9e69ed50 4898 }
ec661b9d
AM
4899
4900 // Return long branch stub size.
4901 unsigned int
32f59844
AM
4902 branch_stub_size(typename Branch_stub_entries::const_iterator p,
4903 bool* need_lt);
4904
4905 bool
4906 build_tls_opt_head(unsigned char** pp,
4907 typename Plt_stub_entries::const_iterator cs);
4908
4909 bool
4910 build_tls_opt_tail(unsigned char* p,
4911 typename Plt_stub_entries::const_iterator cs);
ec661b9d 4912
f073a3e8
AM
4913 void
4914 plt_error(const Plt_stub_key& p);
4915
ec661b9d 4916 // Write out stubs.
cf43a2fe
AM
4917 void
4918 do_write(Output_file*);
4919
ec661b9d 4920 // Plt call stub keys.
bdab445c 4921 class Plt_stub_key
cf43a2fe 4922 {
d1a8cabd 4923 public:
bdab445c 4924 Plt_stub_key(const Symbol* sym)
c9824451
AM
4925 : sym_(sym), object_(0), addend_(0), locsym_(0)
4926 { }
4927
bdab445c 4928 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4929 unsigned int locsym_index)
c9824451
AM
4930 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4931 { }
4932
bdab445c 4933 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4934 const Symbol* sym,
4935 unsigned int r_type,
4936 Address addend)
e5d5f5ed 4937 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4938 {
4939 if (size != 32)
ec661b9d 4940 this->addend_ = addend;
d1a8cabd 4941 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4942 && (r_type == elfcpp::R_PPC_PLTREL24
4943 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 4944 {
ec661b9d 4945 this->addend_ = addend;
e5d5f5ed 4946 if (this->addend_ >= 32768)
d1a8cabd 4947 this->object_ = object;
cf43a2fe
AM
4948 }
4949 }
4950
bdab445c 4951 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4952 unsigned int locsym_index,
4953 unsigned int r_type,
4954 Address addend)
e5d5f5ed
AM
4955 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4956 {
4957 if (size != 32)
ec661b9d 4958 this->addend_ = addend;
e5d5f5ed 4959 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4960 && (r_type == elfcpp::R_PPC_PLTREL24
4961 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 4962 this->addend_ = addend;
e5d5f5ed
AM
4963 }
4964
bdab445c 4965 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
4966 {
4967 return (this->sym_ == that.sym_
4968 && this->object_ == that.object_
e5d5f5ed
AM
4969 && this->addend_ == that.addend_
4970 && this->locsym_ == that.locsym_);
cf43a2fe 4971 }
c9269dff
AM
4972
4973 const Symbol* sym_;
e5d5f5ed
AM
4974 const Sized_relobj_file<size, big_endian>* object_;
4975 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4976 unsigned int locsym_;
cf43a2fe
AM
4977 };
4978
bdab445c 4979 class Plt_stub_key_hash
cf43a2fe 4980 {
d1a8cabd 4981 public:
bdab445c 4982 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
4983 {
4984 return (reinterpret_cast<uintptr_t>(ent.sym_)
4985 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
4986 ^ ent.addend_
4987 ^ ent.locsym_);
cf43a2fe 4988 }
ec661b9d
AM
4989 };
4990
4991 // Long branch stub keys.
32f59844 4992 class Branch_stub_key
ec661b9d
AM
4993 {
4994 public:
32f59844
AM
4995 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
4996 : dest_(to), toc_base_off_(0)
ec661b9d
AM
4997 {
4998 if (size == 64)
4999 toc_base_off_ = obj->toc_base_offset();
5000 }
5001
32f59844 5002 bool operator==(const Branch_stub_key& that) const
ec661b9d
AM
5003 {
5004 return (this->dest_ == that.dest_
5005 && (size == 32
5006 || this->toc_base_off_ == that.toc_base_off_));
5007 }
cf43a2fe 5008
ec661b9d
AM
5009 Address dest_;
5010 unsigned int toc_base_off_;
5011 };
cf43a2fe 5012
32f59844 5013 class Branch_stub_key_hash
ec661b9d
AM
5014 {
5015 public:
32f59844
AM
5016 size_t operator()(const Branch_stub_key& key) const
5017 { return key.dest_ ^ key.toc_base_off_; }
ec661b9d 5018 };
cf43a2fe 5019
ec661b9d 5020 // In a sane world this would be a global.
cf43a2fe 5021 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5022 // Map sym/object/addend to stub offset.
ec661b9d
AM
5023 Plt_stub_entries plt_call_stubs_;
5024 // Map destination address to stub offset.
ec661b9d
AM
5025 Branch_stub_entries long_branch_stubs_;
5026 // size of input section
5027 section_size_type orig_data_size_;
5028 // size of stubs
9e69ed50 5029 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5030 // Some rare cases cause (PR/20529) fluctuation in stub table
5031 // size, which leads to an endless relax loop. This is to be fixed
5032 // by, after the first few iterations, allowing only increase of
5033 // stub table size. This variable sets the minimal possible size of
5034 // a stub table, it is zero for the first few iterations, then
5035 // increases monotonically.
5036 Address min_size_threshold_;
d49044c7
AM
5037 // Set if this stub group needs a copy of out-of-line register
5038 // save/restore functions.
5039 bool need_save_res_;
32f59844
AM
5040 // Set when notoc_/r2save_ changes after sizing a stub
5041 bool need_resize_;
5042 // Set when resizing stubs
5043 bool resizing_;
590b87ff
AM
5044 // Per stub table unique identifier.
5045 uint32_t uniq_;
cf43a2fe
AM
5046};
5047
ec661b9d 5048// Add a plt call stub, if we do not already have one for this
d1a8cabd 5049// sym/object/addend combo.
cf43a2fe
AM
5050
5051template<int size, bool big_endian>
a3e60ddb 5052bool
ec661b9d 5053Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5054 Address from,
c9824451 5055 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5056 const Symbol* gsym,
ec661b9d 5057 unsigned int r_type,
7e57d19e
AM
5058 Address addend,
5059 bool tocsave)
cf43a2fe 5060{
bdab445c
AM
5061 Plt_stub_key key(object, gsym, r_type, addend);
5062 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5063 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5064 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5065 if (size == 64)
7ee7ff70 5066 {
32f59844 5067 if (p.second
7ee7ff70
AM
5068 && this->targ_->is_elfv2_localentry0(gsym))
5069 {
5070 p.first->second.localentry0_ = 1;
5071 this->targ_->set_has_localentry0();
5072 }
32f59844
AM
5073 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5074 {
e4dff765 5075 if (!p.second && !p.first->second.notoc_
7c1f4227 5076 && !this->targ_->power10_stubs())
32f59844
AM
5077 this->need_resize_ = true;
5078 p.first->second.notoc_ = 1;
5079 }
5080 else if (!tocsave && !p.first->second.localentry0_)
5081 {
5082 if (!p.second && !p.first->second.r2save_)
5083 this->need_resize_ = true;
5084 p.first->second.r2save_ = 1;
5085 }
5086 }
5087 if (p.second || (this->resizing_ && !p.first->second.iter_))
5088 {
5089 if (this->resizing_)
5090 {
5091 p.first->second.iter_ = 1;
5092 p.first->second.off_ = this->plt_size_;
5093 }
5094 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5095 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5096 this->targ_->set_has_tls_get_addr_opt();
34e0882b 5097 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70 5098 }
32f59844 5099 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5100}
5101
e5d5f5ed 5102template<int size, bool big_endian>
a3e60ddb 5103bool
ec661b9d 5104Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5105 Address from,
c9824451 5106 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5107 unsigned int locsym_index,
ec661b9d 5108 unsigned int r_type,
7e57d19e
AM
5109 Address addend,
5110 bool tocsave)
e5d5f5ed 5111{
bdab445c
AM
5112 Plt_stub_key key(object, locsym_index, r_type, addend);
5113 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5114 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5115 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5116 if (size == 64)
7ee7ff70 5117 {
32f59844 5118 if (p.second
7ee7ff70
AM
5119 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5120 {
5121 p.first->second.localentry0_ = 1;
5122 this->targ_->set_has_localentry0();
5123 }
32f59844
AM
5124 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5125 {
e4dff765 5126 if (!p.second && !p.first->second.notoc_
7c1f4227 5127 && !this->targ_->power10_stubs())
32f59844
AM
5128 this->need_resize_ = true;
5129 p.first->second.notoc_ = 1;
5130 }
5131 else if (!tocsave && !p.first->second.localentry0_)
5132 {
5133 if (!p.second && !p.first->second.r2save_)
5134 this->need_resize_ = true;
5135 p.first->second.r2save_ = 1;
5136 }
7ee7ff70 5137 }
32f59844
AM
5138 if (p.second || (this->resizing_ && !p.first->second.iter_))
5139 {
5140 if (this->resizing_)
5141 {
5142 p.first->second.iter_ = 1;
5143 p.first->second.off_ = this->plt_size_;
5144 }
5145 this->plt_size_ += this->plt_call_size(p.first);
5146 this->plt_size_ = this->plt_call_align(this->plt_size_);
5147 }
5148 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5149}
5150
ec661b9d
AM
5151// Find a plt call stub.
5152
cf43a2fe 5153template<int size, bool big_endian>
7e57d19e 5154const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5155Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5156 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5157 const Symbol* gsym,
ec661b9d
AM
5158 unsigned int r_type,
5159 Address addend) const
c9824451 5160{
bdab445c
AM
5161 Plt_stub_key key(object, gsym, r_type, addend);
5162 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5163 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5164 return NULL;
5165 return &p->second;
c9824451
AM
5166}
5167
5168template<int size, bool big_endian>
7e57d19e 5169const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5170Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5171{
bdab445c
AM
5172 Plt_stub_key key(gsym);
5173 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5174 if (p == this->plt_call_stubs_.end())
5175 return NULL;
5176 return &p->second;
cf43a2fe
AM
5177}
5178
e5d5f5ed 5179template<int size, bool big_endian>
7e57d19e 5180const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5181Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5182 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5183 unsigned int locsym_index,
ec661b9d
AM
5184 unsigned int r_type,
5185 Address addend) const
e5d5f5ed 5186{
bdab445c
AM
5187 Plt_stub_key key(object, locsym_index, r_type, addend);
5188 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5189 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5190 return NULL;
5191 return &p->second;
c9824451
AM
5192}
5193
5194template<int size, bool big_endian>
7e57d19e 5195const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5196Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5197 const Sized_relobj_file<size, big_endian>* object,
5198 unsigned int locsym_index) const
5199{
bdab445c
AM
5200 Plt_stub_key key(object, locsym_index);
5201 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5202 if (p == this->plt_call_stubs_.end())
5203 return NULL;
5204 return &p->second;
ec661b9d
AM
5205}
5206
5207// Add a long branch stub if we don't already have one to given
5208// destination.
5209
5210template<int size, bool big_endian>
a3e60ddb 5211bool
ec661b9d
AM
5212Stub_table<size, big_endian>::add_long_branch_entry(
5213 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
5214 unsigned int r_type,
5215 Address from,
d49044c7
AM
5216 Address to,
5217 bool save_res)
ec661b9d 5218{
32f59844
AM
5219 Branch_stub_key key(object, to);
5220 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5221 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
590b87ff 5222 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844
AM
5223 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5224 if (notoc && !p.first->second.notoc_)
ec661b9d 5225 {
32f59844
AM
5226 this->need_resize_ = true;
5227 p.first->second.notoc_ = true;
5228 }
5229 gold_assert(save_res == p.first->second.save_res_);
5230 if (p.second || (this->resizing_ && !p.first->second.iter_))
5231 {
5232 if (this->resizing_)
5233 {
5234 p.first->second.iter_ = 1;
5235 p.first->second.off_ = this->branch_size_;
5236 }
d49044c7
AM
5237 if (save_res)
5238 this->need_save_res_ = true;
5239 else
5240 {
32f59844
AM
5241 bool need_lt = false;
5242 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5243 this->branch_size_ += stub_size;
5244 if (size == 64 && need_lt)
d49044c7
AM
5245 this->targ_->add_branch_lookup_table(to);
5246 }
ec661b9d 5247 }
32f59844 5248 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5249}
5250
d49044c7 5251// Find long branch stub offset.
ec661b9d
AM
5252
5253template<int size, bool big_endian>
32f59844 5254const typename Stub_table<size, big_endian>::Branch_stub_ent*
ec661b9d
AM
5255Stub_table<size, big_endian>::find_long_branch_entry(
5256 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 5257 Address to) const
ec661b9d 5258{
32f59844 5259 Branch_stub_key key(object, to);
ec661b9d 5260 typename Branch_stub_entries::const_iterator p
32f59844 5261 = this->long_branch_stubs_.find(key);
d49044c7 5262 if (p == this->long_branch_stubs_.end())
32f59844
AM
5263 return NULL;
5264 return &p->second;
e5d5f5ed
AM
5265}
5266
220f9906
AM
5267template<bool big_endian>
5268static void
5269eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5270{
220f9906
AM
5271 delta /= 4;
5272 if (delta < 64)
5273 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5274 else if (delta < 256)
34e0882b 5275 {
220f9906
AM
5276 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5277 fde.push_back(delta);
5278 }
5279 else if (delta < 65536)
5280 {
5281 fde.resize(fde.size() + 3);
5282 unsigned char *p = &*fde.end() - 3;
5283 *p++ = elfcpp::DW_CFA_advance_loc2;
5284 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5285 }
5286 else
5287 {
5288 fde.resize(fde.size() + 5);
5289 unsigned char *p = &*fde.end() - 5;
5290 *p++ = elfcpp::DW_CFA_advance_loc4;
5291 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5292 }
220f9906
AM
5293}
5294
5295template<typename T>
5296static bool
5297stub_sort(T s1, T s2)
5298{
5299 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5300}
5301
5302// Add .eh_frame info for this stub section. Unlike other linker
5303// generated .eh_frame this is added late in the link, because we
5304// only want the .eh_frame info if this particular stub section is
5305// non-empty.
5306
5307template<int size, bool big_endian>
5308void
5309Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5310{
220f9906
AM
5311 if (size != 64
5312 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5313 return;
5314
5315 // Since we add stub .eh_frame info late, it must be placed
5316 // after all other linker generated .eh_frame info so that
5317 // merge mapping need not be updated for input sections.
5318 // There is no provision to use a different CIE to that used
5319 // by .glink.
5320 if (!this->targ_->has_glink())
5321 return;
5322
220f9906
AM
5323 typedef typename Plt_stub_entries::const_iterator plt_iter;
5324 std::vector<plt_iter> calls;
5325 if (!this->plt_call_stubs_.empty())
5326 for (plt_iter cs = this->plt_call_stubs_.begin();
5327 cs != this->plt_call_stubs_.end();
5328 ++cs)
5329 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5330 && cs->second.r2save_
5331 && !cs->second.localentry0_)
e4dff765 5332 || (cs->second.notoc_
7c1f4227 5333 && !this->targ_->power10_stubs()))
220f9906
AM
5334 calls.push_back(cs);
5335 if (calls.size() > 1)
5336 std::stable_sort(calls.begin(), calls.end(),
5337 stub_sort<plt_iter>);
5338
5339 typedef typename Branch_stub_entries::const_iterator branch_iter;
5340 std::vector<branch_iter> branches;
e4dff765 5341 if (!this->long_branch_stubs_.empty()
7c1f4227 5342 && !this->targ_->power10_stubs())
220f9906
AM
5343 for (branch_iter bs = this->long_branch_stubs_.begin();
5344 bs != this->long_branch_stubs_.end();
5345 ++bs)
5346 if (bs->second.notoc_)
5347 branches.push_back(bs);
5348 if (branches.size() > 1)
5349 std::stable_sort(branches.begin(), branches.end(),
5350 stub_sort<branch_iter>);
5351
5352 if (calls.empty() && branches.empty())
34e0882b
AM
5353 return;
5354
220f9906
AM
5355 unsigned int last_eh_loc = 0;
5356 // offset pcrel sdata4, size udata4, and augmentation size byte.
5357 std::vector<unsigned char> fde(9, 0);
5358
5359 for (unsigned int i = 0; i < calls.size(); i++)
5360 {
5361 plt_iter cs = calls[i];
5362 unsigned int off = cs->second.off_;
5363 // The __tls_get_addr_opt call stub needs to describe where
5364 // it saves LR, to support exceptions that might be thrown
5365 // from __tls_get_addr, and to support asynchronous exceptions.
5366 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5367 {
5368 off += 7 * 4;
5369 if (cs->second.r2save_
5370 && !cs->second.localentry0_)
5371 {
5372 off += 2 * 4;
5373 eh_advance<big_endian>(fde, off - last_eh_loc);
5374 fde.resize(fde.size() + 6);
5375 unsigned char* p = &*fde.end() - 6;
5376 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5377 *p++ = 65;
5378 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5379 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5380 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5381 *p++ = elfcpp::DW_CFA_restore_extended;
5382 *p++ = 65;
5383 last_eh_loc = off + delta;
5384 continue;
5385 }
5386 }
5387 // notoc stubs also should describe LR changes, to support
5388 // asynchronous exceptions.
5389 off += (cs->second.r2save_ ? 4 : 0) + 8;
5390 eh_advance<big_endian>(fde, off - last_eh_loc);
5391 fde.resize(fde.size() + 6);
5392 unsigned char* p = &*fde.end() - 6;
5393 *p++ = elfcpp::DW_CFA_register;
5394 *p++ = 65;
5395 *p++ = 12;
5396 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5397 *p++ = elfcpp::DW_CFA_restore_extended;
5398 *p++ = 65;
5399 last_eh_loc = off + 8;
5400 }
5401
5402 for (unsigned int i = 0; i < branches.size(); i++)
5403 {
5404 branch_iter bs = branches[i];
5405 unsigned int off = bs->second.off_ + 8;
5406 eh_advance<big_endian>(fde, off - last_eh_loc);
5407 fde.resize(fde.size() + 6);
5408 unsigned char* p = &*fde.end() - 6;
5409 *p++ = elfcpp::DW_CFA_register;
5410 *p++ = 65;
5411 *p++ = 12;
5412 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5413 *p++ = elfcpp::DW_CFA_restore_extended;
5414 *p++ = 65;
5415 last_eh_loc = off + 8;
5416 }
5417
34e0882b
AM
5418 layout->add_eh_frame_for_plt(this,
5419 Eh_cie<size>::eh_frame_cie,
5420 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5421 &*fde.begin(), fde.size());
34e0882b
AM
5422}
5423
5424template<int size, bool big_endian>
5425void
5426Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5427{
220f9906
AM
5428 if (size == 64
5429 && parameters->options().ld_generated_unwind_info()
5430 && this->targ_->has_glink())
5431 layout->remove_eh_frame_for_plt(this,
5432 Eh_cie<size>::eh_frame_cie,
5433 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5434}
5435
ec661b9d
AM
5436// A class to handle .glink.
5437
5438template<int size, bool big_endian>
5439class Output_data_glink : public Output_section_data
5440{
5441 public:
9055360d
AM
5442 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5443 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5444
5445 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5446 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5447 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5448 { }
5449
9d5781f8 5450 void
9055360d 5451 add_eh_frame(Layout* layout);
9d5781f8 5452
9055360d
AM
5453 void
5454 add_global_entry(const Symbol*);
5455
5456 Address
5457 find_global_entry(const Symbol*) const;
5458
9e390558
AM
5459 unsigned int
5460 global_entry_align(unsigned int off) const
5461 {
691d2e9a 5462 unsigned int align = param_plt_align<size>();
9e390558
AM
5463 return (off + align - 1) & -align;
5464 }
5465
5466 unsigned int
5467 global_entry_off() const
5468 {
5469 return this->global_entry_align(this->end_branch_table_);
5470 }
5471
9055360d
AM
5472 Address
5473 global_entry_address() const
5474 {
5475 gold_assert(this->is_data_size_valid());
9e390558
AM
5476 return this->address() + this->global_entry_off();
5477 }
5478
5479 int
5480 pltresolve_size() const
5481 {
5482 if (size == 64)
5483 return (8
407aa07c 5484 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
9e390558 5485 return 16 * 4;
9d5781f8
AM
5486 }
5487
ec661b9d
AM
5488 protected:
5489 // Write to a map file.
5490 void
5491 do_print_to_mapfile(Mapfile* mapfile) const
5492 { mapfile->print_output_data(this, _("** glink")); }
5493
5494 private:
5495 void
5496 set_final_data_size();
5497
5498 // Write out .glink
5499 void
5500 do_write(Output_file*);
5501
5502 // Allows access to .got and .plt for do_write.
5503 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5504
5505 // Map sym to stub offset.
5506 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5507 Global_entry_stub_entries global_entry_stubs_;
5508
5509 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5510};
5511
9055360d
AM
5512template<int size, bool big_endian>
5513void
5514Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5515{
5516 if (!parameters->options().ld_generated_unwind_info())
5517 return;
5518
5519 if (size == 64)
5520 {
5521 if (this->targ_->abiversion() < 2)
5522 layout->add_eh_frame_for_plt(this,
5523 Eh_cie<64>::eh_frame_cie,
5524 sizeof (Eh_cie<64>::eh_frame_cie),
5525 glink_eh_frame_fde_64v1,
5526 sizeof (glink_eh_frame_fde_64v1));
5527 else
5528 layout->add_eh_frame_for_plt(this,
5529 Eh_cie<64>::eh_frame_cie,
5530 sizeof (Eh_cie<64>::eh_frame_cie),
5531 glink_eh_frame_fde_64v2,
5532 sizeof (glink_eh_frame_fde_64v2));
5533 }
5534 else
5535 {
5536 // 32-bit .glink can use the default since the CIE return
5537 // address reg, LR, is valid.
5538 layout->add_eh_frame_for_plt(this,
5539 Eh_cie<32>::eh_frame_cie,
5540 sizeof (Eh_cie<32>::eh_frame_cie),
5541 default_fde,
5542 sizeof (default_fde));
5543 // Except where LR is used in a PIC __glink_PLTresolve.
5544 if (parameters->options().output_is_position_independent())
5545 layout->add_eh_frame_for_plt(this,
5546 Eh_cie<32>::eh_frame_cie,
5547 sizeof (Eh_cie<32>::eh_frame_cie),
5548 glink_eh_frame_fde_32,
5549 sizeof (glink_eh_frame_fde_32));
5550 }
5551}
5552
5553template<int size, bool big_endian>
5554void
5555Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5556{
9e390558 5557 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5558 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5559 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5560 if (p.second)
407aa07c 5561 this->ge_size_ = off + 16;
9055360d
AM
5562}
5563
5564template<int size, bool big_endian>
5565typename Output_data_glink<size, big_endian>::Address
5566Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5567{
5568 typename Global_entry_stub_entries::const_iterator p
5569 = this->global_entry_stubs_.find(gsym);
5570 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5571}
5572
cf43a2fe
AM
5573template<int size, bool big_endian>
5574void
5575Output_data_glink<size, big_endian>::set_final_data_size()
5576{
ec661b9d
AM
5577 unsigned int count = this->targ_->plt_entry_count();
5578 section_size_type total = 0;
cf43a2fe
AM
5579
5580 if (count != 0)
5581 {
5582 if (size == 32)
5583 {
cf43a2fe
AM
5584 // space for branch table
5585 total += 4 * (count - 1);
5586
5587 total += -total & 15;
9e390558 5588 total += this->pltresolve_size();
cf43a2fe
AM
5589 }
5590 else
5591 {
9e390558 5592 total += this->pltresolve_size();
cf43a2fe
AM
5593
5594 // space for branch table
b4f7960d
AM
5595 total += 4 * count;
5596 if (this->targ_->abiversion() < 2)
5597 {
5598 total += 4 * count;
5599 if (count > 0x8000)
5600 total += 4 * (count - 0x8000);
5601 }
cf43a2fe
AM
5602 }
5603 }
9055360d 5604 this->end_branch_table_ = total;
9e390558 5605 total = this->global_entry_align(total);
9055360d 5606 total += this->ge_size_;
cf43a2fe
AM
5607
5608 this->set_data_size(total);
5609}
5610
590b87ff
AM
5611// Define symbols on stubs, identifying the stub.
5612
5613template<int size, bool big_endian>
5614void
5615Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5616{
5617 if (!this->plt_call_stubs_.empty())
5618 {
5619 // The key for the plt call stub hash table includes addresses,
5620 // therefore traversal order depends on those addresses, which
5621 // can change between runs if gold is a PIE. Unfortunately the
5622 // output .symtab ordering depends on the order in which symbols
5623 // are added to the linker symtab. We want reproducible output
5624 // so must sort the call stub symbols.
5625 typedef typename Plt_stub_entries::const_iterator plt_iter;
5626 std::vector<plt_iter> sorted;
5627 sorted.resize(this->plt_call_stubs_.size());
5628
5629 for (plt_iter cs = this->plt_call_stubs_.begin();
5630 cs != this->plt_call_stubs_.end();
5631 ++cs)
bdab445c 5632 sorted[cs->second.indx_] = cs;
590b87ff
AM
5633
5634 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5635 {
5636 plt_iter cs = sorted[i];
5637 char add[10];
5638 add[0] = 0;
5639 if (cs->first.addend_ != 0)
5640 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5641 char obj[10];
5642 obj[0] = 0;
5643 if (cs->first.object_)
590b87ff
AM
5644 {
5645 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5646 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5647 sprintf(obj, "%x:", ppcobj->uniq());
5648 }
5649 char localname[9];
5650 const char *symname;
5651 if (cs->first.sym_ == NULL)
5652 {
5653 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5654 symname = localname;
5655 }
34e0882b
AM
5656 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5657 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5658 else
5659 symname = cs->first.sym_->name();
94de2a2c
JC
5660 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5661 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5662 Address value
5663 = this->stub_address() - this->address() + cs->second.off_;
34e0882b 5664 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
590b87ff
AM
5665 this->targ_->define_local(symtab, name, this, value, stub_size);
5666 }
5667 }
5668
5669 typedef typename Branch_stub_entries::const_iterator branch_iter;
5670 for (branch_iter bs = this->long_branch_stubs_.begin();
5671 bs != this->long_branch_stubs_.end();
5672 ++bs)
5673 {
32f59844 5674 if (bs->second.save_res_)
590b87ff
AM
5675 continue;
5676
5677 char* name = new char[8 + 13 + 16 + 1];
5678 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5679 static_cast<unsigned long long>(bs->first.dest_));
5680 Address value = (this->stub_address() - this->address()
32f59844
AM
5681 + this->plt_size_ + bs->second.off_);
5682 bool need_lt = false;
5683 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5684 this->targ_->define_local(symtab, name, this, value, stub_size);
5685 }
5686}
5687
32f59844
AM
5688// Emit the start of a __tls_get_addr_opt plt call stub.
5689
5690template<int size, bool big_endian>
5691bool
5692Stub_table<size, big_endian>::build_tls_opt_head(
5693 unsigned char** pp,
5694 typename Plt_stub_entries::const_iterator cs)
5695{
5696 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5697 {
5698 unsigned char* p = *pp;
5699 if (size == 64)
5700 {
5701 write_insn<big_endian>(p, ld_11_3 + 0);
5702 p += 4;
5703 write_insn<big_endian>(p, ld_12_3 + 8);
5704 p += 4;
5705 write_insn<big_endian>(p, mr_0_3);
5706 p += 4;
5707 write_insn<big_endian>(p, cmpdi_11_0);
5708 p += 4;
5709 write_insn<big_endian>(p, add_3_12_13);
5710 p += 4;
5711 write_insn<big_endian>(p, beqlr);
5712 p += 4;
5713 write_insn<big_endian>(p, mr_3_0);
5714 p += 4;
5715 if (cs->second.r2save_ && !cs->second.localentry0_)
5716 {
5717 write_insn<big_endian>(p, mflr_11);
5718 p += 4;
5719 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5720 p += 4;
5721 }
5722 }
5723 else
5724 {
5725 write_insn<big_endian>(p, lwz_11_3 + 0);
5726 p += 4;
5727 write_insn<big_endian>(p, lwz_12_3 + 4);
5728 p += 4;
5729 write_insn<big_endian>(p, mr_0_3);
5730 p += 4;
5731 write_insn<big_endian>(p, cmpwi_11_0);
5732 p += 4;
5733 write_insn<big_endian>(p, add_3_12_2);
5734 p += 4;
5735 write_insn<big_endian>(p, beqlr);
5736 p += 4;
5737 write_insn<big_endian>(p, mr_3_0);
5738 p += 4;
5739 write_insn<big_endian>(p, nop);
5740 p += 4;
5741 }
5742 *pp = p;
5743 return true;
5744 }
5745 return false;
5746}
5747
5748// Emit the tail of a __tls_get_addr_opt plt call stub.
5749
5750template<int size, bool big_endian>
5751bool
5752Stub_table<size, big_endian>::build_tls_opt_tail(
5753 unsigned char* p,
5754 typename Plt_stub_entries::const_iterator cs)
5755{
5756 if (size == 64
5757 && cs->second.r2save_
5758 && !cs->second.localentry0_
5759 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5760 {
5761 write_insn<big_endian>(p, bctrl);
5762 p += 4;
5763 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5764 p += 4;
5765 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5766 p += 4;
5767 write_insn<big_endian>(p, mtlr_11);
5768 p += 4;
5769 write_insn<big_endian>(p, blr);
5770 return true;
5771 }
5772 return false;
5773}
5774
e4dff765
AM
5775// Emit pc-relative plt call stub code.
5776
5777template<bool big_endian>
5778static unsigned char*
7c1f4227 5779build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
e4dff765
AM
5780{
5781 uint64_t insn;
5782 if (off - odd + (1ULL << 33) < 1ULL << 34)
5783 {
5784 off -= odd;
5785 if (odd)
5786 {
5787 write_insn<big_endian>(p, nop);
5788 p += 4;
5789 }
5790 if (load)
5791 insn = pld_12_pc;
5792 else
5793 insn = paddi_12_pc;
5794 insn |= d34(off);
5795 write_insn<big_endian>(p, insn >> 32);
5796 p += 4;
5797 write_insn<big_endian>(p, insn & 0xffffffff);
5798 }
5799 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5800 {
5801 off -= 8 - odd;
5802 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5803 p += 4;
5804 if (!odd)
5805 {
5806 write_insn<big_endian>(p, sldi_11_11_34);
5807 p += 4;
5808 }
5809 insn = paddi_12_pc | d34(off);
5810 write_insn<big_endian>(p, insn >> 32);
5811 p += 4;
5812 write_insn<big_endian>(p, insn & 0xffffffff);
5813 p += 4;
5814 if (odd)
5815 {
5816 write_insn<big_endian>(p, sldi_11_11_34);
5817 p += 4;
5818 }
5819 if (load)
5820 write_insn<big_endian>(p, ldx_12_11_12);
5821 else
5822 write_insn<big_endian>(p, add_12_11_12);
5823 }
5824 else
5825 {
5826 off -= odd + 8;
5827 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5828 p += 4;
5829 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5830 p += 4;
5831 if (odd)
5832 {
5833 write_insn<big_endian>(p, sldi_11_11_34);
5834 p += 4;
5835 }
5836 insn = paddi_12_pc | d34(off);
5837 write_insn<big_endian>(p, insn >> 32);
5838 p += 4;
5839 write_insn<big_endian>(p, insn & 0xffffffff);
5840 p += 4;
5841 if (!odd)
5842 {
5843 write_insn<big_endian>(p, sldi_11_11_34);
5844 p += 4;
5845 }
5846 if (load)
5847 write_insn<big_endian>(p, ldx_12_11_12);
5848 else
5849 write_insn<big_endian>(p, add_12_11_12);
5850 }
5851 p += 4;
5852 return p;
5853}
5854
32f59844
AM
5855// Gets the address of a label (1:) in r11 and builds an offset in r12,
5856// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5857// mflr %r12
5858// bcl 20,31,1f
5859// 1: mflr %r11
5860// mtlr %r12
5861// lis %r12,xxx-1b@highest
5862// ori %r12,%r12,xxx-1b@higher
5863// sldi %r12,%r12,32
5864// oris %r12,%r12,xxx-1b@high
5865// ori %r12,%r12,xxx-1b@l
5866// add/ldx %r12,%r11,%r12
5867
5868template<bool big_endian>
5869static unsigned char*
5870build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5871{
5872 write_insn<big_endian>(p, mflr_12);
5873 p += 4;
5874 write_insn<big_endian>(p, bcl_20_31);
5875 p += 4;
5876 write_insn<big_endian>(p, mflr_11);
5877 p += 4;
5878 write_insn<big_endian>(p, mtlr_12);
5879 p += 4;
5880 if (off + 0x8000 < 0x10000)
5881 {
5882 if (load)
5883 write_insn<big_endian>(p, ld_12_11 + l(off));
5884 else
5885 write_insn<big_endian>(p, addi_12_11 + l(off));
5886 }
5887 else if (off + 0x80008000ULL < 0x100000000ULL)
5888 {
5889 write_insn<big_endian>(p, addis_12_11 + ha(off));
5890 p += 4;
5891 if (load)
5892 write_insn<big_endian>(p, ld_12_12 + l(off));
5893 else
5894 write_insn<big_endian>(p, addi_12_12 + l(off));
5895 }
5896 else
5897 {
5898 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5899 {
5900 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5901 p += 4;
5902 }
5903 else
5904 {
5905 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5906 p += 4;
5907 if (((off >> 32) & 0xffff) != 0)
5908 {
5909 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5910 p += 4;
5911 }
5912 }
5913 if (((off >> 32) & 0xffffffffULL) != 0)
5914 {
5915 write_insn<big_endian>(p, sldi_12_12_32);
5916 p += 4;
5917 }
5918 if (hi(off) != 0)
5919 {
5920 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5921 p += 4;
5922 }
5923 if (l(off) != 0)
5924 {
5925 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5926 p += 4;
5927 }
5928 if (load)
5929 write_insn<big_endian>(p, ldx_12_11_12);
5930 else
5931 write_insn<big_endian>(p, add_12_11_12);
5932 }
5933 p += 4;
5934 return p;
5935}
5936
5937// Size of a given plt call stub.
5938
5939template<int size, bool big_endian>
5940unsigned int
5941Stub_table<size, big_endian>::plt_call_size(
5942 typename Plt_stub_entries::const_iterator p) const
5943{
5944 if (size == 32)
5945 {
5946 const Symbol* gsym = p->first.sym_;
5947 return (4 * 4
5948 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
5949 }
5950
5951 const Output_data_plt_powerpc<size, big_endian>* plt;
5952 uint64_t plt_addr = this->plt_off(p, &plt);
5953 plt_addr += plt->address();
5954 unsigned int bytes = 0;
5955 const Symbol* gsym = p->first.sym_;
5956 if (this->targ_->is_tls_get_addr_opt(gsym))
5957 {
5958 if (p->second.r2save_ && !p->second.localentry0_)
5959 bytes = 13 * 4;
5960 else
5961 bytes = 7 * 4;
5962 }
5963
5964 if (p->second.r2save_)
5965 bytes += 4;
5966
7c1f4227 5967 if (this->targ_->power10_stubs())
e4dff765
AM
5968 {
5969 uint64_t from = this->stub_address() + p->second.off_ + bytes;
5970 if (bytes > 8 * 4)
5971 from -= 4 * 4;
5972 uint64_t odd = from & 4;
5973 uint64_t off = plt_addr - from;
5974 if (off - odd + (1ULL << 33) < 1ULL << 34)
5975 bytes += odd + 4 * 4;
5976 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5977 bytes += 7 * 4;
5978 else
5979 bytes += 8 * 4;
5980 return bytes;
5981 }
5982
32f59844
AM
5983 if (p->second.notoc_)
5984 {
5985 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
5986 if (bytes > 32)
5987 from -= 4 * 4;
5988 uint64_t off = plt_addr - from;
5989 if (off + 0x8000 < 0x10000)
5990 bytes += 7 * 4;
5991 else if (off + 0x80008000ULL < 0x100000000ULL)
5992 bytes += 8 * 4;
5993 else
5994 {
5995 bytes += 8 * 4;
5996 if (off + 0x800000000000ULL >= 0x1000000000000ULL
5997 && ((off >> 32) & 0xffff) != 0)
5998 bytes += 4;
5999 if (((off >> 32) & 0xffffffffULL) != 0)
6000 bytes += 4;
6001 if (hi(off) != 0)
6002 bytes += 4;
6003 if (l(off) != 0)
6004 bytes += 4;
6005 }
6006 return bytes;
6007 }
6008
6009 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6010 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6011 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6012 got_addr += ppcobj->toc_base_offset();
6013 uint64_t off = plt_addr - got_addr;
6014 bytes += 3 * 4 + 4 * (ha(off) != 0);
6015 if (this->targ_->abiversion() < 2)
6016 {
6017 bool static_chain = parameters->options().plt_static_chain();
6018 bool thread_safe = this->targ_->plt_thread_safe();
6019 bytes += (4
6020 + 4 * static_chain
6021 + 8 * thread_safe
6022 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6023 }
6024 return bytes;
6025}
6026
6027// Return long branch stub size.
6028
6029template<int size, bool big_endian>
6030unsigned int
6031Stub_table<size, big_endian>::branch_stub_size(
6032 typename Branch_stub_entries::const_iterator p,
6033 bool* need_lt)
6034{
6035 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6036 if (size == 32)
6037 {
6038 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6039 return 4;
6040 if (parameters->options().output_is_position_independent())
6041 return 32;
6042 return 16;
6043 }
6044
6045 uint64_t off = p->first.dest_ - loc;
6046 if (p->second.notoc_)
6047 {
7c1f4227 6048 if (this->targ_->power10_stubs())
e4dff765
AM
6049 {
6050 Address odd = loc & 4;
6051 if (off + (1 << 25) < 2 << 25)
6052 return odd + 12;
6053 if (off - odd + (1ULL << 33) < 1ULL << 34)
6054 return odd + 16;
6055 if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6056 return 28;
6057 return 32;
6058 }
32f59844
AM
6059 off -= 8;
6060 if (off + 0x8000 < 0x10000)
6061 return 24;
6062 if (off + 0x80008000ULL < 0x100000000ULL)
6063 {
6064 if (off + 24 + (1 << 25) < 2 << 25)
6065 return 28;
6066 return 32;
6067 }
6068 unsigned int bytes = 32;
6069 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6070 && ((off >> 32) & 0xffff) != 0)
6071 bytes += 4;
6072 if (((off >> 32) & 0xffffffffULL) != 0)
6073 bytes += 4;
6074 if (hi(off) != 0)
6075 bytes += 4;
6076 if (l(off) != 0)
6077 bytes += 4;
6078 return bytes;
6079 }
6080
6081 if (off + (1 << 25) < 2 << 25)
6082 return 4;
7c1f4227 6083 if (!this->targ_->power10_stubs())
e4dff765 6084 *need_lt = true;
32f59844
AM
6085 return 16;
6086}
6087
f073a3e8
AM
6088template<int size, bool big_endian>
6089void
6090Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6091{
6092 if (p.sym_)
6093 gold_error(_("linkage table error against `%s'"),
6094 p.sym_->demangled_name().c_str());
6095 else
6096 gold_error(_("linkage table error against `%s:[local %u]'"),
6097 p.object_->name().c_str(),
6098 p.locsym_);
6099}
6100
ec661b9d 6101// Write out plt and long branch stub code.
cf43a2fe
AM
6102
6103template<int size, bool big_endian>
6104void
ec661b9d 6105Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6106{
ec661b9d
AM
6107 if (this->plt_call_stubs_.empty()
6108 && this->long_branch_stubs_.empty())
6109 return;
6110
6111 const section_size_type start_off = this->offset();
6112 const section_size_type off = this->stub_offset();
42cacb20 6113 const section_size_type oview_size =
ec661b9d 6114 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6115 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6116 unsigned char* p;
42cacb20 6117
e4dff765 6118 if (size == 64
7c1f4227 6119 && this->targ_->power10_stubs())
e4dff765
AM
6120 {
6121 if (!this->plt_call_stubs_.empty())
6122 {
6123 // Write out plt call stubs.
6124 typename Plt_stub_entries::const_iterator cs;
6125 for (cs = this->plt_call_stubs_.begin();
6126 cs != this->plt_call_stubs_.end();
6127 ++cs)
6128 {
6129 p = oview + cs->second.off_;
6130 this->build_tls_opt_head(&p, cs);
6131 if (cs->second.r2save_)
6132 {
6133 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6134 p += 4;
6135 }
6136 const Output_data_plt_powerpc<size, big_endian>* plt;
6137 Address pltoff = this->plt_off(cs, &plt);
6138 Address plt_addr = pltoff + plt->address();
6139 Address from = this->stub_address() + (p - oview);
6140 Address delta = plt_addr - from;
7c1f4227 6141 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
e4dff765
AM
6142 write_insn<big_endian>(p, mtctr_12);
6143 p += 4;
6144 if (!this->build_tls_opt_tail(p, cs))
6145 write_insn<big_endian>(p, bctr);
6146 }
6147 }
6148
6149 // Write out long branch stubs.
6150 typename Branch_stub_entries::const_iterator bs;
6151 for (bs = this->long_branch_stubs_.begin();
6152 bs != this->long_branch_stubs_.end();
6153 ++bs)
6154 {
6155 if (bs->second.save_res_)
6156 continue;
6157 Address off = this->plt_size_ + bs->second.off_;
6158 p = oview + off;
6159 Address loc = this->stub_address() + off;
6160 Address delta = bs->first.dest_ - loc;
6161 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6162 {
6163 unsigned char* startp = p;
7c1f4227 6164 p = build_power10_offset<big_endian>(p, delta, loc & 4, false);
e4dff765
AM
6165 delta -= p - startp;
6166 }
6167 if (delta + (1 << 25) < 2 << 25)
6168 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6169 else
6170 {
6171 write_insn<big_endian>(p, mtctr_12);
6172 p += 4;
6173 write_insn<big_endian>(p, bctr);
6174 }
6175 }
6176 }
6177 else if (size == 64)
cf43a2fe 6178 {
ec661b9d
AM
6179 const Output_data_got_powerpc<size, big_endian>* got
6180 = this->targ_->got_section();
dd93cd0a 6181 Address got_os_addr = got->output_section()->address();
c9269dff 6182
32f59844
AM
6183 if (!this->plt_call_stubs_.empty()
6184 && this->targ_->abiversion() >= 2)
cf43a2fe 6185 {
32f59844
AM
6186 // Write out plt call stubs for ELFv2.
6187 typename Plt_stub_entries::const_iterator cs;
6188 for (cs = this->plt_call_stubs_.begin();
6189 cs != this->plt_call_stubs_.end();
6190 ++cs)
6191 {
6192 const Output_data_plt_powerpc<size, big_endian>* plt;
6193 Address pltoff = this->plt_off(cs, &plt);
6194 Address plt_addr = pltoff + plt->address();
6195
6196 p = oview + cs->second.off_;
6197 this->build_tls_opt_head(&p, cs);
6198 if (cs->second.r2save_)
6199 {
6200 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6201 p += 4;
6202 }
6203 if (cs->second.notoc_)
6204 {
6205 Address from = this->stub_address() + (p - oview) + 8;
6206 Address off = plt_addr - from;
6207 p = build_notoc_offset<big_endian>(p, off, true);
6208 }
6209 else
6210 {
6211 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6212 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6213 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6214 Address off = plt_addr - got_addr;
6215
6216 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6217 this->plt_error(cs->first);
6218
6219 if (ha(off) != 0)
6220 {
6221 write_insn<big_endian>(p, addis_12_2 + ha(off));
6222 p += 4;
6223 write_insn<big_endian>(p, ld_12_12 + l(off));
6224 p += 4;
6225 }
6226 else
6227 {
6228 write_insn<big_endian>(p, ld_12_2 + l(off));
6229 p += 4;
6230 }
6231 }
6232 write_insn<big_endian>(p, mtctr_12);
6233 p += 4;
6234 if (!this->build_tls_opt_tail(p, cs))
6235 write_insn<big_endian>(p, bctr);
6236 }
6237 }
6238 else if (!this->plt_call_stubs_.empty())
6239 {
6240 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6241 typename Plt_stub_entries::const_iterator cs;
6242 for (cs = this->plt_call_stubs_.begin();
6243 cs != this->plt_call_stubs_.end();
6244 ++cs)
e5d5f5ed 6245 {
08be3224
AM
6246 const Output_data_plt_powerpc<size, big_endian>* plt;
6247 Address pltoff = this->plt_off(cs, &plt);
6248 Address plt_addr = pltoff + plt->address();
ec661b9d
AM
6249 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6250 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6251 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 6252 Address off = plt_addr - got_addr;
ec661b9d 6253
32f59844
AM
6254 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6255 || cs->second.notoc_)
f073a3e8 6256 this->plt_error(cs->first);
ec661b9d 6257
32f59844
AM
6258 bool static_chain = parameters->options().plt_static_chain();
6259 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6260 bool use_fake_dep = false;
6261 Address cmp_branch_off = 0;
407aa07c 6262 if (thread_safe)
9e69ed50
AM
6263 {
6264 unsigned int pltindex
6265 = ((pltoff - this->targ_->first_plt_entry_offset())
6266 / this->targ_->plt_entry_size());
6267 Address glinkoff
9e390558 6268 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6269 + pltindex * 8);
6270 if (pltindex > 32768)
6271 glinkoff += (pltindex - 32768) * 4;
6272 Address to
6273 = this->targ_->glink_section()->address() + glinkoff;
6274 Address from
7e57d19e
AM
6275 = (this->stub_address() + cs->second.off_ + 20
6276 + 4 * cs->second.r2save_
9e69ed50
AM
6277 + 4 * (ha(off) != 0)
6278 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6279 + 4 * static_chain);
6280 cmp_branch_off = to - from;
6281 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6282 }
6283
bdab445c 6284 p = oview + cs->second.off_;
32f59844
AM
6285 if (this->build_tls_opt_head(&p, cs))
6286 use_fake_dep = thread_safe;
6287 if (cs->second.r2save_)
34e0882b 6288 {
32f59844 6289 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6290 p += 4;
34e0882b 6291 }
9e69ed50 6292 if (ha(off) != 0)
ec661b9d 6293 {
32f59844
AM
6294 write_insn<big_endian>(p, addis_11_2 + ha(off));
6295 p += 4;
6296 write_insn<big_endian>(p, ld_12_11 + l(off));
6297 p += 4;
6298 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6299 {
32f59844 6300 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6301 p += 4;
32f59844 6302 off = 0;
397998fc 6303 }
32f59844
AM
6304 write_insn<big_endian>(p, mtctr_12);
6305 p += 4;
6306 if (use_fake_dep)
397998fc 6307 {
32f59844 6308 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6309 p += 4;
32f59844 6310 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6311 p += 4;
6312 }
32f59844 6313 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6314 p += 4;
32f59844 6315 if (static_chain)
9e69ed50 6316 {
32f59844 6317 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6318 p += 4;
9e69ed50 6319 }
ec661b9d
AM
6320 }
6321 else
6322 {
b4f7960d
AM
6323 write_insn<big_endian>(p, ld_12_2 + l(off));
6324 p += 4;
32f59844 6325 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6326 {
b4f7960d
AM
6327 write_insn<big_endian>(p, addi_2_2 + l(off));
6328 p += 4;
9e69ed50 6329 off = 0;
ec661b9d 6330 }
b4f7960d
AM
6331 write_insn<big_endian>(p, mtctr_12);
6332 p += 4;
32f59844 6333 if (use_fake_dep)
9e69ed50 6334 {
32f59844
AM
6335 write_insn<big_endian>(p, xor_11_12_12);
6336 p += 4;
6337 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6338 p += 4;
9e69ed50 6339 }
32f59844
AM
6340 if (static_chain)
6341 {
6342 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6343 p += 4;
6344 }
6345 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6346 p += 4;
34e0882b 6347 }
32f59844
AM
6348 if (this->build_tls_opt_tail(p, cs))
6349 ;
34e0882b 6350 else if (thread_safe && !use_fake_dep)
9e69ed50 6351 {
b4f7960d
AM
6352 write_insn<big_endian>(p, cmpldi_2_0);
6353 p += 4;
6354 write_insn<big_endian>(p, bnectr_p4);
6355 p += 4;
9e69ed50
AM
6356 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6357 }
6358 else
407aa07c 6359 write_insn<big_endian>(p, bctr);
e5d5f5ed 6360 }
ec661b9d
AM
6361 }
6362
6363 // Write out long branch stubs.
6364 typename Branch_stub_entries::const_iterator bs;
6365 for (bs = this->long_branch_stubs_.begin();
6366 bs != this->long_branch_stubs_.end();
6367 ++bs)
6368 {
32f59844 6369 if (bs->second.save_res_)
d49044c7 6370 continue;
32f59844
AM
6371 Address off = this->plt_size_ + bs->second.off_;
6372 p = oview + off;
6373 Address loc = this->stub_address() + off;
ec661b9d 6374 Address delta = bs->first.dest_ - loc;
32f59844
AM
6375 if (bs->second.notoc_)
6376 {
6377 unsigned char* startp = p;
6378 p = build_notoc_offset<big_endian>(p, off, false);
6379 delta -= p - startp;
6380 }
6381 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6382 {
ec661b9d
AM
6383 Address brlt_addr
6384 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6385 gold_assert(brlt_addr != invalid_address);
6386 brlt_addr += this->targ_->brlt_section()->address();
6387 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6388 Address brltoff = brlt_addr - got_addr;
6389 if (ha(brltoff) == 0)
6390 {
32f59844
AM
6391 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6392 p += 4;
ec661b9d
AM
6393 }
6394 else
cf43a2fe 6395 {
32f59844
AM
6396 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6397 p += 4;
6398 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6399 p += 4;
cf43a2fe 6400 }
32f59844
AM
6401 }
6402 if (delta + (1 << 25) < 2 << 25)
6403 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6404 else
6405 {
6406 write_insn<big_endian>(p, mtctr_12);
6407 p += 4;
407aa07c 6408 write_insn<big_endian>(p, bctr);
cf43a2fe 6409 }
ec661b9d
AM
6410 }
6411 }
32f59844 6412 else // size == 32
ec661b9d
AM
6413 {
6414 if (!this->plt_call_stubs_.empty())
6415 {
ec661b9d
AM
6416 // The address of _GLOBAL_OFFSET_TABLE_.
6417 Address g_o_t = invalid_address;
6418
6419 // Write out plt call stubs.
6420 typename Plt_stub_entries::const_iterator cs;
6421 for (cs = this->plt_call_stubs_.begin();
6422 cs != this->plt_call_stubs_.end();
6423 ++cs)
cf43a2fe 6424 {
08be3224
AM
6425 const Output_data_plt_powerpc<size, big_endian>* plt;
6426 Address plt_addr = this->plt_off(cs, &plt);
6427 plt_addr += plt->address();
ec661b9d 6428
bdab445c 6429 p = oview + cs->second.off_;
32f59844 6430 this->build_tls_opt_head(&p, cs);
ec661b9d
AM
6431 if (parameters->options().output_is_position_independent())
6432 {
6433 Address got_addr;
6434 const Powerpc_relobj<size, big_endian>* ppcobj
6435 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6436 (cs->first.object_));
6437 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6438 {
6439 unsigned int got2 = ppcobj->got2_shndx();
6440 got_addr = ppcobj->get_output_section_offset(got2);
6441 gold_assert(got_addr != invalid_address);
6442 got_addr += (ppcobj->output_section(got2)->address()
6443 + cs->first.addend_);
6444 }
6445 else
6446 {
6447 if (g_o_t == invalid_address)
6448 {
6449 const Output_data_got_powerpc<size, big_endian>* got
6450 = this->targ_->got_section();
6451 g_o_t = got->address() + got->g_o_t();
6452 }
6453 got_addr = g_o_t;
6454 }
6455
9e69ed50
AM
6456 Address off = plt_addr - got_addr;
6457 if (ha(off) == 0)
9e390558 6458 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6459 else
6460 {
9e390558
AM
6461 write_insn<big_endian>(p, addis_11_30 + ha(off));
6462 p += 4;
6463 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6464 }
6465 }
6466 else
6467 {
9e390558
AM
6468 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6469 p += 4;
6470 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6471 }
9e390558
AM
6472 p += 4;
6473 write_insn<big_endian>(p, mtctr_11);
6474 p += 4;
407aa07c 6475 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6476 }
6477 }
6478
6479 // Write out long branch stubs.
6480 typename Branch_stub_entries::const_iterator bs;
6481 for (bs = this->long_branch_stubs_.begin();
6482 bs != this->long_branch_stubs_.end();
6483 ++bs)
6484 {
32f59844 6485 if (bs->second.save_res_)
d49044c7 6486 continue;
32f59844
AM
6487 Address off = this->plt_size_ + bs->second.off_;
6488 p = oview + off;
6489 Address loc = this->stub_address() + off;
ec661b9d
AM
6490 Address delta = bs->first.dest_ - loc;
6491 if (delta + (1 << 25) < 2 << 25)
6492 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6493 else if (!parameters->options().output_is_position_independent())
6494 {
9e390558
AM
6495 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6496 p += 4;
6497 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6498 }
6499 else
6500 {
6501 delta -= 8;
9e390558
AM
6502 write_insn<big_endian>(p, mflr_0);
6503 p += 4;
6504 write_insn<big_endian>(p, bcl_20_31);
6505 p += 4;
6506 write_insn<big_endian>(p, mflr_12);
6507 p += 4;
6508 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6509 p += 4;
6510 write_insn<big_endian>(p, addi_12_12 + l(delta));
6511 p += 4;
6512 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6513 }
9e390558
AM
6514 p += 4;
6515 write_insn<big_endian>(p, mtctr_12);
6516 p += 4;
407aa07c 6517 write_insn<big_endian>(p, bctr);
cf43a2fe 6518 }
ec661b9d 6519 }
d49044c7
AM
6520 if (this->need_save_res_)
6521 {
6522 p = oview + this->plt_size_ + this->branch_size_;
6523 memcpy (p, this->targ_->savres_section()->contents(),
6524 this->targ_->savres_section()->data_size());
6525 }
ec661b9d
AM
6526}
6527
6528// Write out .glink.
6529
6530template<int size, bool big_endian>
6531void
6532Output_data_glink<size, big_endian>::do_write(Output_file* of)
6533{
6534 const section_size_type off = this->offset();
6535 const section_size_type oview_size =
6536 convert_to_section_size_type(this->data_size());
6537 unsigned char* const oview = of->get_output_view(off, oview_size);
6538 unsigned char* p;
6539
6540 // The base address of the .plt section.
6541 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6542 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 6543
ec661b9d
AM
6544 if (size == 64)
6545 {
9055360d 6546 if (this->end_branch_table_ != 0)
b4f7960d 6547 {
9055360d
AM
6548 // Write pltresolve stub.
6549 p = oview;
6550 Address after_bcl = this->address() + 16;
6551 Address pltoff = plt_base - after_bcl;
6552
6553 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 6554
b4f7960d 6555 if (this->targ_->abiversion() < 2)
cf43a2fe 6556 {
9055360d
AM
6557 write_insn<big_endian>(p, mflr_12), p += 4;
6558 write_insn<big_endian>(p, bcl_20_31), p += 4;
6559 write_insn<big_endian>(p, mflr_11), p += 4;
6560 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6561 write_insn<big_endian>(p, mtlr_12), p += 4;
6562 write_insn<big_endian>(p, add_11_2_11), p += 4;
6563 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6564 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6565 write_insn<big_endian>(p, mtctr_12), p += 4;
6566 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6567 }
6568 else
6569 {
6570 write_insn<big_endian>(p, mflr_0), p += 4;
6571 write_insn<big_endian>(p, bcl_20_31), p += 4;
6572 write_insn<big_endian>(p, mflr_11), p += 4;
7ee7ff70 6573 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
9055360d
AM
6574 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6575 write_insn<big_endian>(p, mtlr_0), p += 4;
6576 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6577 write_insn<big_endian>(p, add_11_2_11), p += 4;
6578 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
6579 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6580 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6581 write_insn<big_endian>(p, mtctr_12), p += 4;
6582 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6583 }
407aa07c 6584 write_insn<big_endian>(p, bctr), p += 4;
9e390558 6585 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
6586
6587 // Write lazy link call stubs.
6588 uint32_t indx = 0;
6589 while (p < oview + this->end_branch_table_)
6590 {
6591 if (this->targ_->abiversion() < 2)
b4f7960d 6592 {
9055360d
AM
6593 if (indx < 0x8000)
6594 {
6595 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6596 }
6597 else
6598 {
bbec1a5d 6599 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
6600 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6601 }
b4f7960d 6602 }
9055360d
AM
6603 uint32_t branch_off = 8 - (p - oview);
6604 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6605 indx++;
cf43a2fe 6606 }
9055360d
AM
6607 }
6608
6609 Address plt_base = this->targ_->plt_section()->address();
6610 Address iplt_base = invalid_address;
9e390558 6611 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
6612 Address global_entry_base = this->address() + global_entry_off;
6613 typename Global_entry_stub_entries::const_iterator ge;
6614 for (ge = this->global_entry_stubs_.begin();
6615 ge != this->global_entry_stubs_.end();
6616 ++ge)
6617 {
6618 p = oview + global_entry_off + ge->second;
6619 Address plt_addr = ge->first->plt_offset();
6620 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6621 && ge->first->can_use_relative_reloc(false))
6622 {
6623 if (iplt_base == invalid_address)
6624 iplt_base = this->targ_->iplt_section()->address();
6625 plt_addr += iplt_base;
6626 }
6627 else
6628 plt_addr += plt_base;
6629 Address my_addr = global_entry_base + ge->second;
6630 Address off = plt_addr - my_addr;
6631
6632 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 6633 gold_error(_("linkage table error against `%s'"),
9055360d
AM
6634 ge->first->demangled_name().c_str());
6635
6636 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6637 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6638 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 6639 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
6640 }
6641 }
6642 else
6643 {
ec661b9d
AM
6644 const Output_data_got_powerpc<size, big_endian>* got
6645 = this->targ_->got_section();
dd93cd0a
AM
6646 // The address of _GLOBAL_OFFSET_TABLE_.
6647 Address g_o_t = got->address() + got->g_o_t();
c9269dff 6648
cf43a2fe 6649 // Write out pltresolve branch table.
ec661b9d 6650 p = oview;
9e390558 6651 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 6652 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
6653 while (p < end_p - 8 * 4)
6654 write_insn<big_endian>(p, b + end_p - p), p += 4;
6655 while (p < end_p)
6656 write_insn<big_endian>(p, nop), p += 4;
42cacb20 6657
cf43a2fe 6658 // Write out pltresolve call stub.
9e390558 6659 end_p = oview + oview_size;
cf43a2fe 6660 if (parameters->options().output_is_position_independent())
42cacb20 6661 {
ec661b9d 6662 Address res0_off = 0;
dd93cd0a
AM
6663 Address after_bcl_off = the_end + 12;
6664 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 6665
9e390558
AM
6666 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6667 p += 4;
6668 write_insn<big_endian>(p, mflr_0);
6669 p += 4;
6670 write_insn<big_endian>(p, bcl_20_31);
6671 p += 4;
6672 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6673 p += 4;
6674 write_insn<big_endian>(p, mflr_12);
6675 p += 4;
6676 write_insn<big_endian>(p, mtlr_0);
6677 p += 4;
6678 write_insn<big_endian>(p, sub_11_11_12);
6679 p += 4;
cf43a2fe 6680
dd93cd0a 6681 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 6682
9e390558
AM
6683 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6684 p += 4;
cf43a2fe
AM
6685 if (ha(got_bcl) == ha(got_bcl + 4))
6686 {
9e390558
AM
6687 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6688 p += 4;
6689 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
6690 }
6691 else
6692 {
9e390558
AM
6693 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6694 p += 4;
6695 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 6696 }
9e390558
AM
6697 p += 4;
6698 write_insn<big_endian>(p, mtctr_0);
6699 p += 4;
6700 write_insn<big_endian>(p, add_0_11_11);
6701 p += 4;
6702 write_insn<big_endian>(p, add_11_0_11);
42cacb20 6703 }
cf43a2fe 6704 else
42cacb20 6705 {
ec661b9d 6706 Address res0 = this->address();
cf43a2fe 6707
9e390558
AM
6708 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6709 p += 4;
6710 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
6711 p += 4;
cf43a2fe 6712 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 6713 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 6714 else
9e390558
AM
6715 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
6716 p += 4;
6717 write_insn<big_endian>(p, addi_11_11 + l(-res0));
6718 p += 4;
6719 write_insn<big_endian>(p, mtctr_0);
6720 p += 4;
6721 write_insn<big_endian>(p, add_0_11_11);
6722 p += 4;
cf43a2fe 6723 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 6724 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 6725 else
9e390558
AM
6726 write_insn<big_endian>(p, lwz_12_12 + 4);
6727 p += 4;
6728 write_insn<big_endian>(p, add_11_0_11);
6729 }
6730 p += 4;
407aa07c
AM
6731 write_insn<big_endian>(p, bctr);
6732 p += 4;
9e390558
AM
6733 while (p < end_p)
6734 {
6735 write_insn<big_endian>(p, nop);
6736 p += 4;
42cacb20
DE
6737 }
6738 }
6739
cf43a2fe
AM
6740 of->write_output_view(off, oview_size, oview);
6741}
6742
f3a0ed29
AM
6743
6744// A class to handle linker generated save/restore functions.
6745
6746template<int size, bool big_endian>
6747class Output_data_save_res : public Output_section_data_build
6748{
6749 public:
6750 Output_data_save_res(Symbol_table* symtab);
6751
d49044c7
AM
6752 const unsigned char*
6753 contents() const
6754 {
6755 return contents_;
6756 }
6757
f3a0ed29
AM
6758 protected:
6759 // Write to a map file.
6760 void
6761 do_print_to_mapfile(Mapfile* mapfile) const
6762 { mapfile->print_output_data(this, _("** save/restore")); }
6763
6764 void
6765 do_write(Output_file*);
6766
6767 private:
6768 // The maximum size of save/restore contents.
6769 static const unsigned int savres_max = 218*4;
6770
6771 void
6772 savres_define(Symbol_table* symtab,
6773 const char *name,
6774 unsigned int lo, unsigned int hi,
6775 unsigned char* write_ent(unsigned char*, int),
6776 unsigned char* write_tail(unsigned char*, int));
6777
6778 unsigned char *contents_;
6779};
6780
6781template<bool big_endian>
6782static unsigned char*
6783savegpr0(unsigned char* p, int r)
6784{
6785 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6786 write_insn<big_endian>(p, insn);
6787 return p + 4;
6788}
6789
6790template<bool big_endian>
6791static unsigned char*
6792savegpr0_tail(unsigned char* p, int r)
6793{
6794 p = savegpr0<big_endian>(p, r);
6795 uint32_t insn = std_0_1 + 16;
6796 write_insn<big_endian>(p, insn);
6797 p = p + 4;
6798 write_insn<big_endian>(p, blr);
6799 return p + 4;
6800}
6801
6802template<bool big_endian>
62fe925a 6803static unsigned char*
f3a0ed29
AM
6804restgpr0(unsigned char* p, int r)
6805{
6806 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6807 write_insn<big_endian>(p, insn);
6808 return p + 4;
6809}
6810
6811template<bool big_endian>
62fe925a 6812static unsigned char*
f3a0ed29
AM
6813restgpr0_tail(unsigned char* p, int r)
6814{
6815 uint32_t insn = ld_0_1 + 16;
6816 write_insn<big_endian>(p, insn);
6817 p = p + 4;
6818 p = restgpr0<big_endian>(p, r);
6819 write_insn<big_endian>(p, mtlr_0);
6820 p = p + 4;
6821 if (r == 29)
6822 {
6823 p = restgpr0<big_endian>(p, 30);
6824 p = restgpr0<big_endian>(p, 31);
6825 }
6826 write_insn<big_endian>(p, blr);
6827 return p + 4;
6828}
6829
6830template<bool big_endian>
62fe925a 6831static unsigned char*
f3a0ed29
AM
6832savegpr1(unsigned char* p, int r)
6833{
6834 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6835 write_insn<big_endian>(p, insn);
6836 return p + 4;
6837}
6838
6839template<bool big_endian>
62fe925a 6840static unsigned char*
f3a0ed29
AM
6841savegpr1_tail(unsigned char* p, int r)
6842{
6843 p = savegpr1<big_endian>(p, r);
6844 write_insn<big_endian>(p, blr);
6845 return p + 4;
6846}
6847
6848template<bool big_endian>
62fe925a 6849static unsigned char*
f3a0ed29
AM
6850restgpr1(unsigned char* p, int r)
6851{
6852 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6853 write_insn<big_endian>(p, insn);
6854 return p + 4;
6855}
6856
6857template<bool big_endian>
62fe925a 6858static unsigned char*
f3a0ed29
AM
6859restgpr1_tail(unsigned char* p, int r)
6860{
6861 p = restgpr1<big_endian>(p, r);
6862 write_insn<big_endian>(p, blr);
6863 return p + 4;
6864}
6865
6866template<bool big_endian>
62fe925a 6867static unsigned char*
f3a0ed29
AM
6868savefpr(unsigned char* p, int r)
6869{
6870 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6871 write_insn<big_endian>(p, insn);
6872 return p + 4;
6873}
6874
6875template<bool big_endian>
62fe925a 6876static unsigned char*
f3a0ed29
AM
6877savefpr0_tail(unsigned char* p, int r)
6878{
6879 p = savefpr<big_endian>(p, r);
6880 write_insn<big_endian>(p, std_0_1 + 16);
6881 p = p + 4;
6882 write_insn<big_endian>(p, blr);
6883 return p + 4;
6884}
6885
6886template<bool big_endian>
62fe925a 6887static unsigned char*
f3a0ed29
AM
6888restfpr(unsigned char* p, int r)
6889{
6890 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6891 write_insn<big_endian>(p, insn);
6892 return p + 4;
6893}
6894
6895template<bool big_endian>
62fe925a 6896static unsigned char*
f3a0ed29
AM
6897restfpr0_tail(unsigned char* p, int r)
6898{
6899 write_insn<big_endian>(p, ld_0_1 + 16);
6900 p = p + 4;
6901 p = restfpr<big_endian>(p, r);
6902 write_insn<big_endian>(p, mtlr_0);
6903 p = p + 4;
6904 if (r == 29)
6905 {
6906 p = restfpr<big_endian>(p, 30);
6907 p = restfpr<big_endian>(p, 31);
6908 }
6909 write_insn<big_endian>(p, blr);
6910 return p + 4;
6911}
6912
6913template<bool big_endian>
62fe925a 6914static unsigned char*
f3a0ed29
AM
6915savefpr1_tail(unsigned char* p, int r)
6916{
6917 p = savefpr<big_endian>(p, r);
6918 write_insn<big_endian>(p, blr);
6919 return p + 4;
6920}
6921
6922template<bool big_endian>
62fe925a 6923static unsigned char*
f3a0ed29
AM
6924restfpr1_tail(unsigned char* p, int r)
6925{
6926 p = restfpr<big_endian>(p, r);
6927 write_insn<big_endian>(p, blr);
6928 return p + 4;
6929}
6930
6931template<bool big_endian>
62fe925a 6932static unsigned char*
f3a0ed29
AM
6933savevr(unsigned char* p, int r)
6934{
6935 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6936 write_insn<big_endian>(p, insn);
6937 p = p + 4;
6938 insn = stvx_0_12_0 + (r << 21);
6939 write_insn<big_endian>(p, insn);
6940 return p + 4;
6941}
6942
6943template<bool big_endian>
62fe925a 6944static unsigned char*
f3a0ed29
AM
6945savevr_tail(unsigned char* p, int r)
6946{
6947 p = savevr<big_endian>(p, r);
6948 write_insn<big_endian>(p, blr);
6949 return p + 4;
6950}
6951
6952template<bool big_endian>
62fe925a 6953static unsigned char*
f3a0ed29
AM
6954restvr(unsigned char* p, int r)
6955{
6956 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6957 write_insn<big_endian>(p, insn);
6958 p = p + 4;
6959 insn = lvx_0_12_0 + (r << 21);
6960 write_insn<big_endian>(p, insn);
6961 return p + 4;
6962}
6963
6964template<bool big_endian>
62fe925a 6965static unsigned char*
f3a0ed29
AM
6966restvr_tail(unsigned char* p, int r)
6967{
6968 p = restvr<big_endian>(p, r);
6969 write_insn<big_endian>(p, blr);
6970 return p + 4;
6971}
6972
6973
6974template<int size, bool big_endian>
6975Output_data_save_res<size, big_endian>::Output_data_save_res(
6976 Symbol_table* symtab)
6977 : Output_section_data_build(4),
6978 contents_(NULL)
6979{
6980 this->savres_define(symtab,
6981 "_savegpr0_", 14, 31,
6982 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6983 this->savres_define(symtab,
6984 "_restgpr0_", 14, 29,
6985 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6986 this->savres_define(symtab,
6987 "_restgpr0_", 30, 31,
6988 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6989 this->savres_define(symtab,
6990 "_savegpr1_", 14, 31,
6991 savegpr1<big_endian>, savegpr1_tail<big_endian>);
6992 this->savres_define(symtab,
6993 "_restgpr1_", 14, 31,
6994 restgpr1<big_endian>, restgpr1_tail<big_endian>);
6995 this->savres_define(symtab,
6996 "_savefpr_", 14, 31,
6997 savefpr<big_endian>, savefpr0_tail<big_endian>);
6998 this->savres_define(symtab,
6999 "_restfpr_", 14, 29,
7000 restfpr<big_endian>, restfpr0_tail<big_endian>);
7001 this->savres_define(symtab,
7002 "_restfpr_", 30, 31,
7003 restfpr<big_endian>, restfpr0_tail<big_endian>);
7004 this->savres_define(symtab,
7005 "._savef", 14, 31,
7006 savefpr<big_endian>, savefpr1_tail<big_endian>);
7007 this->savres_define(symtab,
7008 "._restf", 14, 31,
7009 restfpr<big_endian>, restfpr1_tail<big_endian>);
7010 this->savres_define(symtab,
7011 "_savevr_", 20, 31,
7012 savevr<big_endian>, savevr_tail<big_endian>);
7013 this->savres_define(symtab,
7014 "_restvr_", 20, 31,
7015 restvr<big_endian>, restvr_tail<big_endian>);
7016}
7017
7018template<int size, bool big_endian>
7019void
7020Output_data_save_res<size, big_endian>::savres_define(
7021 Symbol_table* symtab,
7022 const char *name,
7023 unsigned int lo, unsigned int hi,
7024 unsigned char* write_ent(unsigned char*, int),
7025 unsigned char* write_tail(unsigned char*, int))
7026{
7027 size_t len = strlen(name);
7028 bool writing = false;
7029 char sym[16];
7030
7031 memcpy(sym, name, len);
7032 sym[len + 2] = 0;
7033
7034 for (unsigned int i = lo; i <= hi; i++)
7035 {
7036 sym[len + 0] = i / 10 + '0';
7037 sym[len + 1] = i % 10 + '0';
7038 Symbol* gsym = symtab->lookup(sym);
7039 bool refd = gsym != NULL && gsym->is_undefined();
7040 writing = writing || refd;
7041 if (writing)
7042 {
7043 if (this->contents_ == NULL)
7044 this->contents_ = new unsigned char[this->savres_max];
7045
ec661b9d 7046 section_size_type value = this->current_data_size();
f3a0ed29
AM
7047 unsigned char* p = this->contents_ + value;
7048 if (i != hi)
7049 p = write_ent(p, i);
7050 else
7051 p = write_tail(p, i);
ec661b9d 7052 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7053 this->set_current_data_size(cur_size);
7054 if (refd)
7055 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7056 this, value, cur_size - value,
7057 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7058 elfcpp::STV_HIDDEN, 0, false, false);
7059 }
7060 }
7061}
7062
7063// Write out save/restore.
7064
7065template<int size, bool big_endian>
7066void
7067Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7068{
ec661b9d 7069 const section_size_type off = this->offset();
f3a0ed29
AM
7070 const section_size_type oview_size =
7071 convert_to_section_size_type(this->data_size());
7072 unsigned char* const oview = of->get_output_view(off, oview_size);
7073 memcpy(oview, this->contents_, oview_size);
7074 of->write_output_view(off, oview_size, oview);
7075}
7076
7077
cf43a2fe 7078// Create the glink section.
42cacb20 7079
cf43a2fe
AM
7080template<int size, bool big_endian>
7081void
7082Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7083{
7084 if (this->glink_ == NULL)
7085 {
7086 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7087 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7088 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7089 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7090 this->glink_, ORDER_TEXT, false);
7091 }
42cacb20
DE
7092}
7093
7094// Create a PLT entry for a global symbol.
7095
7096template<int size, bool big_endian>
7097void
ec661b9d
AM
7098Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7099 Layout* layout,
7100 Symbol* gsym)
42cacb20 7101{
e5d5f5ed
AM
7102 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7103 && gsym->can_use_relative_reloc(false))
7104 {
7105 if (this->iplt_ == NULL)
40b469d7 7106 this->make_iplt_section(symtab, layout);
03e25981 7107 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7108 }
7109 else
7110 {
7111 if (this->plt_ == NULL)
40b469d7 7112 this->make_plt_section(symtab, layout);
03e25981 7113 this->plt_->add_entry(gsym);
e5d5f5ed 7114 }
e5d5f5ed 7115}
42cacb20 7116
2d7ad24e
AM
7117// Make a PLT entry for a local symbol.
7118
7119template<int size, bool big_endian>
7120void
7121Target_powerpc<size, big_endian>::make_local_plt_entry(
7122 Layout* layout,
7123 Sized_relobj_file<size, big_endian>* relobj,
7124 unsigned int r_sym)
7125{
7126 if (this->lplt_ == NULL)
7127 this->make_lplt_section(layout);
7128 this->lplt_->add_local_entry(relobj, r_sym);
7129}
7130
e5d5f5ed 7131// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7132
e5d5f5ed
AM
7133template<int size, bool big_endian>
7134void
7135Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7136 Symbol_table* symtab,
e5d5f5ed 7137 Layout* layout,
ec661b9d
AM
7138 Sized_relobj_file<size, big_endian>* relobj,
7139 unsigned int r_sym)
e5d5f5ed
AM
7140{
7141 if (this->iplt_ == NULL)
40b469d7 7142 this->make_iplt_section(symtab, layout);
03e25981 7143 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7144}
7145
0e70b911
CC
7146// Return the number of entries in the PLT.
7147
7148template<int size, bool big_endian>
7149unsigned int
7150Target_powerpc<size, big_endian>::plt_entry_count() const
7151{
7152 if (this->plt_ == NULL)
7153 return 0;
b3ccdeb5 7154 return this->plt_->entry_count();
0e70b911
CC
7155}
7156
dd93cd0a 7157// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7158
7159template<int size, bool big_endian>
7160unsigned int
dd93cd0a 7161Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7162 Symbol_table* symtab,
7163 Layout* layout,
7164 Sized_relobj_file<size, big_endian>* object)
42cacb20 7165{
dd93cd0a 7166 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7167 {
7168 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7169 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
7170 Output_data_got_powerpc<size, big_endian>* got
7171 = this->got_section(symtab, layout);
7172 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7173 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7174 got_offset, 0);
dd93cd0a 7175 this->tlsld_got_offset_ = got_offset;
42cacb20 7176 }
dd93cd0a 7177 return this->tlsld_got_offset_;
42cacb20
DE
7178}
7179
95a2c8d6
RS
7180// Get the Reference_flags for a particular relocation.
7181
7182template<int size, bool big_endian>
7183int
88b8e639
AM
7184Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7185 unsigned int r_type,
7186 const Target_powerpc* target)
95a2c8d6 7187{
88b8e639
AM
7188 int ref = 0;
7189
95a2c8d6
RS
7190 switch (r_type)
7191 {
7192 case elfcpp::R_POWERPC_NONE:
7193 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7194 case elfcpp::R_POWERPC_GNU_VTENTRY:
7195 case elfcpp::R_PPC64_TOC:
7196 // No symbol reference.
88b8e639 7197 break;
95a2c8d6 7198
dd93cd0a
AM
7199 case elfcpp::R_PPC64_ADDR64:
7200 case elfcpp::R_PPC64_UADDR64:
7201 case elfcpp::R_POWERPC_ADDR32:
7202 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 7203 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 7204 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
7205 case elfcpp::R_POWERPC_ADDR16_LO:
7206 case elfcpp::R_POWERPC_ADDR16_HI:
7207 case elfcpp::R_POWERPC_ADDR16_HA:
89c52ae3
AM
7208 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7209 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7210 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7211 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7212 case elfcpp::R_PPC64_D34:
7213 case elfcpp::R_PPC64_D34_LO:
7214 case elfcpp::R_PPC64_D34_HI30:
7215 case elfcpp::R_PPC64_D34_HA30:
7216 case elfcpp::R_PPC64_D28:
88b8e639
AM
7217 ref = Symbol::ABSOLUTE_REF;
7218 break;
95a2c8d6 7219
dd93cd0a
AM
7220 case elfcpp::R_POWERPC_ADDR24:
7221 case elfcpp::R_POWERPC_ADDR14:
7222 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7223 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7224 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7225 break;
dd93cd0a 7226
e5d5f5ed 7227 case elfcpp::R_PPC64_REL64:
dd93cd0a 7228 case elfcpp::R_POWERPC_REL32:
95a2c8d6 7229 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
7230 case elfcpp::R_POWERPC_REL16:
7231 case elfcpp::R_POWERPC_REL16_LO:
7232 case elfcpp::R_POWERPC_REL16_HI:
7233 case elfcpp::R_POWERPC_REL16_HA:
c432bbba
AM
7234 case elfcpp::R_PPC64_REL16_HIGH:
7235 case elfcpp::R_PPC64_REL16_HIGHA:
7236 case elfcpp::R_PPC64_REL16_HIGHER:
7237 case elfcpp::R_PPC64_REL16_HIGHERA:
7238 case elfcpp::R_PPC64_REL16_HIGHEST:
7239 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7240 case elfcpp::R_PPC64_PCREL34:
7241 case elfcpp::R_PPC64_REL16_HIGHER34:
7242 case elfcpp::R_PPC64_REL16_HIGHERA34:
7243 case elfcpp::R_PPC64_REL16_HIGHEST34:
7244 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7245 case elfcpp::R_PPC64_PCREL28:
88b8e639
AM
7246 ref = Symbol::RELATIVE_REF;
7247 break;
95a2c8d6 7248
32f59844
AM
7249 case elfcpp::R_PPC64_REL24_NOTOC:
7250 if (size == 32)
7251 break;
7252 // Fall through.
dd93cd0a 7253 case elfcpp::R_POWERPC_REL24:
95a2c8d6 7254 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
7255 case elfcpp::R_POWERPC_REL14:
7256 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7257 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
7258 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7259 break;
95a2c8d6
RS
7260
7261 case elfcpp::R_POWERPC_GOT16:
7262 case elfcpp::R_POWERPC_GOT16_LO:
7263 case elfcpp::R_POWERPC_GOT16_HI:
7264 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
7265 case elfcpp::R_PPC64_GOT16_DS:
7266 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7267 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7268 case elfcpp::R_PPC64_TOC16:
7269 case elfcpp::R_PPC64_TOC16_LO:
7270 case elfcpp::R_PPC64_TOC16_HI:
7271 case elfcpp::R_PPC64_TOC16_HA:
7272 case elfcpp::R_PPC64_TOC16_DS:
7273 case elfcpp::R_PPC64_TOC16_LO_DS:
08be3224
AM
7274 case elfcpp::R_POWERPC_PLT16_LO:
7275 case elfcpp::R_POWERPC_PLT16_HI:
7276 case elfcpp::R_POWERPC_PLT16_HA:
7277 case elfcpp::R_PPC64_PLT16_LO_DS:
e4dff765
AM
7278 case elfcpp::R_PPC64_PLT_PCREL34:
7279 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
32d849b3 7280 ref = Symbol::RELATIVE_REF;
88b8e639 7281 break;
95a2c8d6
RS
7282
7283 case elfcpp::R_POWERPC_GOT_TPREL16:
7284 case elfcpp::R_POWERPC_TLS:
89c52ae3
AM
7285 case elfcpp::R_PPC64_TLSGD:
7286 case elfcpp::R_PPC64_TLSLD:
7287 case elfcpp::R_PPC64_TPREL34:
7288 case elfcpp::R_PPC64_DTPREL34:
7289 case elfcpp::R_PPC64_GOT_TLSGD34:
7290 case elfcpp::R_PPC64_GOT_TLSLD34:
7291 case elfcpp::R_PPC64_GOT_TPREL34:
7292 case elfcpp::R_PPC64_GOT_DTPREL34:
88b8e639
AM
7293 ref = Symbol::TLS_REF;
7294 break;
95a2c8d6
RS
7295
7296 case elfcpp::R_POWERPC_COPY:
7297 case elfcpp::R_POWERPC_GLOB_DAT:
7298 case elfcpp::R_POWERPC_JMP_SLOT:
7299 case elfcpp::R_POWERPC_RELATIVE:
7300 case elfcpp::R_POWERPC_DTPMOD:
7301 default:
7302 // Not expected. We will give an error later.
88b8e639 7303 break;
95a2c8d6 7304 }
88b8e639
AM
7305
7306 if (size == 64 && target->abiversion() < 2)
7307 ref |= Symbol::FUNC_DESC_ABI;
7308 return ref;
95a2c8d6
RS
7309}
7310
42cacb20
DE
7311// Report an unsupported relocation against a local symbol.
7312
7313template<int size, bool big_endian>
7314void
7315Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7316 Sized_relobj_file<size, big_endian>* object,
7317 unsigned int r_type)
42cacb20
DE
7318{
7319 gold_error(_("%s: unsupported reloc %u against local symbol"),
7320 object->name().c_str(), r_type);
7321}
7322
7323// We are about to emit a dynamic relocation of type R_TYPE. If the
7324// dynamic linker does not support it, issue an error.
7325
7326template<int size, bool big_endian>
7327void
7328Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7329 unsigned int r_type)
7330{
7331 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7332
7333 // These are the relocation types supported by glibc for both 32-bit
7334 // and 64-bit powerpc.
7335 switch (r_type)
7336 {
3ea0a085 7337 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7338 case elfcpp::R_POWERPC_RELATIVE:
7339 case elfcpp::R_POWERPC_GLOB_DAT:
7340 case elfcpp::R_POWERPC_DTPMOD:
7341 case elfcpp::R_POWERPC_DTPREL:
7342 case elfcpp::R_POWERPC_TPREL:
7343 case elfcpp::R_POWERPC_JMP_SLOT:
7344 case elfcpp::R_POWERPC_COPY:
3ea0a085 7345 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7346 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7347 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7348 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7349 case elfcpp::R_POWERPC_ADDR16:
7350 case elfcpp::R_POWERPC_UADDR16:
7351 case elfcpp::R_POWERPC_ADDR16_LO:
7352 case elfcpp::R_POWERPC_ADDR16_HI:
7353 case elfcpp::R_POWERPC_ADDR16_HA:
7354 case elfcpp::R_POWERPC_ADDR14:
7355 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7356 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7357 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7358 case elfcpp::R_POWERPC_TPREL16:
7359 case elfcpp::R_POWERPC_TPREL16_LO:
7360 case elfcpp::R_POWERPC_TPREL16_HI:
7361 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7362 return;
7363
7364 default:
7365 break;
7366 }
7367
7368 if (size == 64)
7369 {
7370 switch (r_type)
7371 {
7372 // These are the relocation types supported only on 64-bit.
7373 case elfcpp::R_PPC64_ADDR64:
42cacb20 7374 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7375 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7376 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7377 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7378 case elfcpp::R_PPC64_ADDR16_HIGH:
7379 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7380 case elfcpp::R_PPC64_ADDR16_HIGHER:
7381 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7382 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7383 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7384 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7385 case elfcpp::R_POWERPC_ADDR30:
7386 case elfcpp::R_PPC64_TPREL16_DS:
7387 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7388 case elfcpp::R_PPC64_TPREL16_HIGH:
7389 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7390 case elfcpp::R_PPC64_TPREL16_HIGHER:
7391 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7392 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7393 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7394 return;
7395
7396 default:
7397 break;
7398 }
7399 }
7400 else
7401 {
7402 switch (r_type)
7403 {
7404 // These are the relocation types supported only on 32-bit.
3ea0a085 7405 // ??? glibc ld.so doesn't need to support these.
e59a1001 7406 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7407 case elfcpp::R_POWERPC_DTPREL16:
7408 case elfcpp::R_POWERPC_DTPREL16_LO:
7409 case elfcpp::R_POWERPC_DTPREL16_HI:
7410 case elfcpp::R_POWERPC_DTPREL16_HA:
7411 return;
42cacb20
DE
7412
7413 default:
7414 break;
7415 }
7416 }
7417
7418 // This prevents us from issuing more than one error per reloc
7419 // section. But we can still wind up issuing more than one
7420 // error per object file.
7421 if (this->issued_non_pic_error_)
7422 return;
33aea2fd 7423 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7424 object->error(_("requires unsupported dynamic reloc; "
7425 "recompile with -fPIC"));
7426 this->issued_non_pic_error_ = true;
7427 return;
7428}
7429
e5d5f5ed
AM
7430// Return whether we need to make a PLT entry for a relocation of the
7431// given type against a STT_GNU_IFUNC symbol.
7432
7433template<int size, bool big_endian>
7434bool
7435Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7436 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7437 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7438 unsigned int r_type,
7439 bool report_err)
e5d5f5ed 7440{
c9824451
AM
7441 // In non-pic code any reference will resolve to the plt call stub
7442 // for the ifunc symbol.
9055360d
AM
7443 if ((size == 32 || target->abiversion() >= 2)
7444 && !parameters->options().output_is_position_independent())
c9824451
AM
7445 return true;
7446
e5d5f5ed
AM
7447 switch (r_type)
7448 {
b3ccdeb5 7449 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7450 case elfcpp::R_POWERPC_ADDR32:
7451 case elfcpp::R_POWERPC_UADDR32:
7452 if (size == 32)
b3ccdeb5 7453 return false;
e5d5f5ed
AM
7454 break;
7455
7456 case elfcpp::R_PPC64_ADDR64:
7457 case elfcpp::R_PPC64_UADDR64:
7458 if (size == 64)
b3ccdeb5 7459 return false;
e5d5f5ed
AM
7460 break;
7461
b3ccdeb5 7462 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7463 case elfcpp::R_POWERPC_GOT16:
7464 case elfcpp::R_POWERPC_GOT16_LO:
7465 case elfcpp::R_POWERPC_GOT16_HI:
7466 case elfcpp::R_POWERPC_GOT16_HA:
7467 case elfcpp::R_PPC64_GOT16_DS:
7468 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7469 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7470 return false;
e5d5f5ed 7471
08be3224
AM
7472 // PLT relocs are OK and need a PLT entry.
7473 case elfcpp::R_POWERPC_PLT16_LO:
7474 case elfcpp::R_POWERPC_PLT16_HI:
7475 case elfcpp::R_POWERPC_PLT16_HA:
7476 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
7477 case elfcpp::R_POWERPC_PLTSEQ:
7478 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7479 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7480 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7481 case elfcpp::R_PPC64_PLT_PCREL34:
7482 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
7483 return true;
7484 break;
7485
b3ccdeb5 7486 // Function calls are good, and these do need a PLT entry.
32f59844
AM
7487 case elfcpp::R_PPC64_REL24_NOTOC:
7488 if (size == 32)
7489 break;
7490 // Fall through.
e5d5f5ed
AM
7491 case elfcpp::R_POWERPC_ADDR24:
7492 case elfcpp::R_POWERPC_ADDR14:
7493 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7494 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7495 case elfcpp::R_POWERPC_REL24:
7496 case elfcpp::R_PPC_PLTREL24:
7497 case elfcpp::R_POWERPC_REL14:
7498 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7499 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7500 return true;
7501
7502 default:
7503 break;
7504 }
7505
7506 // Anything else is a problem.
7507 // If we are building a static executable, the libc startup function
7508 // responsible for applying indirect function relocations is going
7509 // to complain about the reloc type.
7510 // If we are building a dynamic executable, we will have a text
7511 // relocation. The dynamic loader will set the text segment
7512 // writable and non-executable to apply text relocations. So we'll
7513 // segfault when trying to run the indirection function to resolve
7514 // the reloc.
b3ccdeb5
AM
7515 if (report_err)
7516 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
7517 object->name().c_str(), r_type);
7518 return false;
7519}
7520
5edad15d
AM
7521// Return TRUE iff INSN is one we expect on a _LO variety toc/got
7522// reloc.
7523
7524static bool
7525ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7526{
7527 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7528 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7529 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7530 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7531 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7532 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7533 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7534 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7535 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7536 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7537 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7538 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7539 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7540 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7541 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7542 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7543 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7544 /* Exclude lfqu by testing reloc. If relocs are ever
7545 defined for the reduced D field in psq_lu then those
7546 will need testing too. */
7547 && r_type != elfcpp::R_PPC64_TOC16_LO
7548 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7549 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7550 && (insn & 1) == 0)
7551 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7552 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7553 /* Exclude stfqu. psq_stu as above for psq_lu. */
7554 && r_type != elfcpp::R_PPC64_TOC16_LO
7555 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7556 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7557 && (insn & 1) == 0));
7558}
7559
42cacb20
DE
7560// Scan a relocation for a local symbol.
7561
7562template<int size, bool big_endian>
7563inline void
7564Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
7565 Symbol_table* symtab,
7566 Layout* layout,
7567 Target_powerpc<size, big_endian>* target,
7568 Sized_relobj_file<size, big_endian>* object,
7569 unsigned int data_shndx,
7570 Output_section* output_section,
7571 const elfcpp::Rela<size, big_endian>& reloc,
7572 unsigned int r_type,
e5d5f5ed 7573 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 7574 bool is_discarded)
42cacb20 7575{
34e0882b 7576 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
7577
7578 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7579 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7580 {
7581 this->expect_tls_get_addr_call();
7582 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7583 if (tls_type != tls::TLSOPT_NONE)
7584 this->skip_next_tls_get_addr_call();
7585 }
7586 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7587 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7588 {
7589 this->expect_tls_get_addr_call();
7590 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7591 if (tls_type != tls::TLSOPT_NONE)
7592 this->skip_next_tls_get_addr_call();
7593 }
7594
dd93cd0a
AM
7595 Powerpc_relobj<size, big_endian>* ppc_object
7596 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7597
bfdfa4cd
AM
7598 if (is_discarded)
7599 {
7600 if (size == 64
7601 && data_shndx == ppc_object->opd_shndx()
7602 && r_type == elfcpp::R_PPC64_ADDR64)
7603 ppc_object->set_opd_discard(reloc.get_r_offset());
7604 return;
7605 }
7606
e5d5f5ed
AM
7607 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7608 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 7609 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 7610 {
ec661b9d
AM
7611 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7612 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7613 r_type, r_sym, reloc.get_r_addend());
7614 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 7615 }
e5d5f5ed 7616
42cacb20
DE
7617 switch (r_type)
7618 {
7619 case elfcpp::R_POWERPC_NONE:
7620 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7621 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 7622 case elfcpp::R_POWERPC_TLS:
549dba71 7623 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
7624 case elfcpp::R_POWERPC_PLTSEQ:
7625 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7626 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7627 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7628 case elfcpp::R_PPC64_PCREL_OPT:
7629 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7630 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7631 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7632 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7633 case elfcpp::R_PPC64_REL16_HIGHER34:
7634 case elfcpp::R_PPC64_REL16_HIGHERA34:
7635 case elfcpp::R_PPC64_REL16_HIGHEST34:
7636 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
7637 case elfcpp::R_PPC64_D34:
7638 case elfcpp::R_PPC64_D34_LO:
7639 case elfcpp::R_PPC64_D34_HI30:
7640 case elfcpp::R_PPC64_D34_HA30:
7641 case elfcpp::R_PPC64_D28:
7642 case elfcpp::R_PPC64_PCREL34:
7643 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
7644 case elfcpp::R_PPC64_TPREL34:
7645 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
7646 break;
7647
7648 case elfcpp::R_PPC64_TOC:
7649 {
7650 Output_data_got_powerpc<size, big_endian>* got
7651 = target->got_section(symtab, layout);
7652 if (parameters->options().output_is_position_independent())
7653 {
bfdfa4cd
AM
7654 Address off = reloc.get_r_offset();
7655 if (size == 64
9055360d 7656 && target->abiversion() < 2
bfdfa4cd
AM
7657 && data_shndx == ppc_object->opd_shndx()
7658 && ppc_object->get_opd_discard(off - 8))
7659 break;
7660
dd93cd0a 7661 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 7662 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
7663 rela_dyn->add_output_section_relative(got->output_section(),
7664 elfcpp::R_POWERPC_RELATIVE,
7665 output_section,
bfdfa4cd
AM
7666 object, data_shndx, off,
7667 symobj->toc_base_offset());
dd93cd0a
AM
7668 }
7669 }
42cacb20
DE
7670 break;
7671
7672 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 7673 case elfcpp::R_PPC64_UADDR64:
42cacb20 7674 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
7675 case elfcpp::R_POWERPC_UADDR32:
7676 case elfcpp::R_POWERPC_ADDR24:
c9269dff 7677 case elfcpp::R_POWERPC_ADDR16:
42cacb20 7678 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
7679 case elfcpp::R_POWERPC_ADDR16_HI:
7680 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 7681 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
7682 case elfcpp::R_PPC64_ADDR16_HIGH:
7683 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
7684 case elfcpp::R_PPC64_ADDR16_HIGHER:
7685 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7686 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7687 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7688 case elfcpp::R_PPC64_ADDR16_DS:
7689 case elfcpp::R_PPC64_ADDR16_LO_DS:
7690 case elfcpp::R_POWERPC_ADDR14:
7691 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7692 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
7693 // If building a shared library (or a position-independent
7694 // executable), we need to create a dynamic relocation for
7695 // this location.
c9824451 7696 if (parameters->options().output_is_position_independent()
9055360d 7697 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 7698 {
b3ccdeb5
AM
7699 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7700 is_ifunc);
1f98a074 7701 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
7702 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7703 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 7704 {
b3ccdeb5
AM
7705 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7706 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7707 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
7708 output_section, data_shndx,
7709 reloc.get_r_offset(),
c9824451 7710 reloc.get_r_addend(), false);
2e702c99 7711 }
1f98a074 7712 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 7713 {
dd93cd0a 7714 check_non_pic(object, r_type);
dd93cd0a
AM
7715 rela_dyn->add_local(object, r_sym, r_type, output_section,
7716 data_shndx, reloc.get_r_offset(),
7717 reloc.get_r_addend());
2e702c99 7718 }
1f98a074
AM
7719 else
7720 {
7721 gold_assert(lsym.get_st_value() == 0);
7722 unsigned int shndx = lsym.get_st_shndx();
7723 bool is_ordinary;
7724 shndx = object->adjust_sym_shndx(r_sym, shndx,
7725 &is_ordinary);
7726 if (!is_ordinary)
7727 object->error(_("section symbol %u has bad shndx %u"),
7728 r_sym, shndx);
7729 else
7730 rela_dyn->add_local_section(object, shndx, r_type,
7731 output_section, data_shndx,
7732 reloc.get_r_offset());
7733 }
2e702c99 7734 }
42cacb20
DE
7735 break;
7736
e4dff765
AM
7737 case elfcpp::R_PPC64_PLT_PCREL34:
7738 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
7739 case elfcpp::R_POWERPC_PLT16_LO:
7740 case elfcpp::R_POWERPC_PLT16_HI:
7741 case elfcpp::R_POWERPC_PLT16_HA:
7742 case elfcpp::R_PPC64_PLT16_LO_DS:
7743 if (!is_ifunc)
7744 {
7745 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7746 target->make_local_plt_entry(layout, object, r_sym);
7747 }
7748 break;
7749
32f59844
AM
7750 case elfcpp::R_PPC64_REL24_NOTOC:
7751 if (size == 32)
7752 break;
7753 // Fall through.
42cacb20 7754 case elfcpp::R_POWERPC_REL24:
c9824451 7755 case elfcpp::R_PPC_PLTREL24:
42cacb20 7756 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
7757 case elfcpp::R_POWERPC_REL14:
7758 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7759 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 7760 if (!is_ifunc)
0e123f69
AM
7761 {
7762 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7763 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7764 r_type, r_sym, reloc.get_r_addend());
7765 }
ec661b9d
AM
7766 break;
7767
7e57d19e
AM
7768 case elfcpp::R_PPC64_TOCSAVE:
7769 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7770 // caller has already saved r2 and thus a plt call stub need not
7771 // save r2.
7772 if (size == 64
7773 && target->mark_pltcall(ppc_object, data_shndx,
7774 reloc.get_r_offset() - 4, symtab))
7775 {
7776 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7777 unsigned int shndx = lsym.get_st_shndx();
7778 bool is_ordinary;
7779 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7780 if (!is_ordinary)
7781 object->error(_("tocsave symbol %u has bad shndx %u"),
7782 r_sym, shndx);
7783 else
7784 target->add_tocsave(ppc_object, shndx,
7785 lsym.get_st_value() + reloc.get_r_addend());
7786 }
7787 break;
7788
ec661b9d
AM
7789 case elfcpp::R_PPC64_REL64:
7790 case elfcpp::R_POWERPC_REL32:
dd93cd0a 7791 case elfcpp::R_POWERPC_REL16:
6ce78956 7792 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 7793 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 7794 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 7795 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
7796 case elfcpp::R_PPC64_REL16_HIGH:
7797 case elfcpp::R_PPC64_REL16_HIGHA:
7798 case elfcpp::R_PPC64_REL16_HIGHER:
7799 case elfcpp::R_PPC64_REL16_HIGHERA:
7800 case elfcpp::R_PPC64_REL16_HIGHEST:
7801 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 7802 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 7803 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 7804 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 7805 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
7806 case elfcpp::R_PPC64_SECTOFF_DS:
7807 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7808 case elfcpp::R_POWERPC_TPREL16:
7809 case elfcpp::R_POWERPC_TPREL16_LO:
7810 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 7811 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
7812 case elfcpp::R_PPC64_TPREL16_DS:
7813 case elfcpp::R_PPC64_TPREL16_LO_DS:
7814 case elfcpp::R_PPC64_TPREL16_HIGH:
7815 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 7816 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 7817 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 7818 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 7819 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
7820 case elfcpp::R_POWERPC_DTPREL16:
7821 case elfcpp::R_POWERPC_DTPREL16_LO:
7822 case elfcpp::R_POWERPC_DTPREL16_HI:
7823 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
7824 case elfcpp::R_PPC64_DTPREL16_DS:
7825 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
7826 case elfcpp::R_PPC64_DTPREL16_HIGH:
7827 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7828 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7829 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7830 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7831 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
7832 case elfcpp::R_PPC64_TLSGD:
7833 case elfcpp::R_PPC64_TLSLD:
45965137 7834 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
7835 break;
7836
e4dff765 7837 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
7838 case elfcpp::R_POWERPC_GOT16:
7839 case elfcpp::R_POWERPC_GOT16_LO:
7840 case elfcpp::R_POWERPC_GOT16_HI:
7841 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
7842 case elfcpp::R_PPC64_GOT16_DS:
7843 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 7844 {
c9269dff 7845 // The symbol requires a GOT entry.
dd93cd0a
AM
7846 Output_data_got_powerpc<size, big_endian>* got
7847 = target->got_section(symtab, layout);
7848 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 7849
e5d5f5ed 7850 if (!parameters->options().output_is_position_independent())
42cacb20 7851 {
b01a4b04
AM
7852 if (is_ifunc
7853 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
7854 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
7855 else
7856 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
7857 }
7858 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
7859 {
7860 // If we are generating a shared object or a pie, this
7861 // symbol's GOT entry will be set by a dynamic relocation.
7862 unsigned int off;
7863 off = got->add_constant(0);
7864 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 7865
b3ccdeb5
AM
7866 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7867 is_ifunc);
7868 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7869 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7870 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 7871 got, off, 0, false);
2e702c99 7872 }
42cacb20
DE
7873 }
7874 break;
7875
cf43a2fe
AM
7876 case elfcpp::R_PPC64_TOC16:
7877 case elfcpp::R_PPC64_TOC16_LO:
7878 case elfcpp::R_PPC64_TOC16_HI:
7879 case elfcpp::R_PPC64_TOC16_HA:
7880 case elfcpp::R_PPC64_TOC16_DS:
7881 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
7882 // We need a GOT section.
7883 target->got_section(symtab, layout);
7884 break;
7885
89c52ae3 7886 case elfcpp::R_PPC64_GOT_TLSGD34:
dd93cd0a
AM
7887 case elfcpp::R_POWERPC_GOT_TLSGD16:
7888 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7889 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7890 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7891 {
7892 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7893 if (tls_type == tls::TLSOPT_NONE)
7894 {
7895 Output_data_got_powerpc<size, big_endian>* got
7896 = target->got_section(symtab, layout);
7897 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
7898 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7899 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
7900 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
7901 }
7902 else if (tls_type == tls::TLSOPT_TO_LE)
7903 {
7904 // no GOT relocs needed for Local Exec.
7905 }
7906 else
7907 gold_unreachable();
7908 }
42cacb20
DE
7909 break;
7910
89c52ae3 7911 case elfcpp::R_PPC64_GOT_TLSLD34:
dd93cd0a
AM
7912 case elfcpp::R_POWERPC_GOT_TLSLD16:
7913 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7914 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7915 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7916 {
7917 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7918 if (tls_type == tls::TLSOPT_NONE)
7919 target->tlsld_got_offset(symtab, layout, object);
7920 else if (tls_type == tls::TLSOPT_TO_LE)
7921 {
7922 // no GOT relocs needed for Local Exec.
7404fe1b
AM
7923 if (parameters->options().emit_relocs())
7924 {
7925 Output_section* os = layout->tls_segment()->first_section();
7926 gold_assert(os != NULL);
7927 os->set_needs_symtab_index();
7928 }
dd93cd0a
AM
7929 }
7930 else
7931 gold_unreachable();
7932 }
42cacb20 7933 break;
42cacb20 7934
89c52ae3 7935 case elfcpp::R_PPC64_GOT_DTPREL34:
dd93cd0a
AM
7936 case elfcpp::R_POWERPC_GOT_DTPREL16:
7937 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7938 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7939 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7940 {
7941 Output_data_got_powerpc<size, big_endian>* got
7942 = target->got_section(symtab, layout);
7943 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 7944 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
7945 }
7946 break;
42cacb20 7947
89c52ae3 7948 case elfcpp::R_PPC64_GOT_TPREL34:
dd93cd0a
AM
7949 case elfcpp::R_POWERPC_GOT_TPREL16:
7950 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7951 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7952 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7953 {
7954 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7955 if (tls_type == tls::TLSOPT_NONE)
7956 {
dd93cd0a 7957 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
7958 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7959 {
7960 Output_data_got_powerpc<size, big_endian>* got
7961 = target->got_section(symtab, layout);
7962 unsigned int off = got->add_constant(0);
7963 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7964
7965 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7966 rela_dyn->add_symbolless_local_addend(object, r_sym,
7967 elfcpp::R_POWERPC_TPREL,
7968 got, off, 0);
7969 }
dd93cd0a
AM
7970 }
7971 else if (tls_type == tls::TLSOPT_TO_LE)
7972 {
7973 // no GOT relocs needed for Local Exec.
7974 }
7975 else
7976 gold_unreachable();
7977 }
7978 break;
7979
7980 default:
7981 unsupported_reloc_local(object, r_type);
7982 break;
7983 }
d8f5a274 7984
5edad15d
AM
7985 if (size == 64
7986 && parameters->options().toc_optimize())
7987 {
7988 if (data_shndx == ppc_object->toc_shndx())
7989 {
7990 bool ok = true;
7991 if (r_type != elfcpp::R_PPC64_ADDR64
7992 || (is_ifunc && target->abiversion() < 2))
7993 ok = false;
7994 else if (parameters->options().output_is_position_independent())
7995 {
7996 if (is_ifunc)
7997 ok = false;
7998 else
7999 {
8000 unsigned int shndx = lsym.get_st_shndx();
8001 if (shndx >= elfcpp::SHN_LORESERVE
8002 && shndx != elfcpp::SHN_XINDEX)
8003 ok = false;
8004 }
8005 }
8006 if (!ok)
8007 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8008 }
8009
8010 enum {no_check, check_lo, check_ha} insn_check;
8011 switch (r_type)
8012 {
8013 default:
8014 insn_check = no_check;
8015 break;
8016
8017 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8018 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8019 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8020 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8021 case elfcpp::R_POWERPC_GOT16_HA:
8022 case elfcpp::R_PPC64_TOC16_HA:
8023 insn_check = check_ha;
8024 break;
8025
8026 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8027 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8028 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8029 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8030 case elfcpp::R_POWERPC_GOT16_LO:
8031 case elfcpp::R_PPC64_GOT16_LO_DS:
8032 case elfcpp::R_PPC64_TOC16_LO:
8033 case elfcpp::R_PPC64_TOC16_LO_DS:
8034 insn_check = check_lo;
8035 break;
8036 }
8037
8038 section_size_type slen;
8039 const unsigned char* view = NULL;
8040 if (insn_check != no_check)
8041 {
8042 view = ppc_object->section_contents(data_shndx, &slen, false);
8043 section_size_type off =
8044 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8045 if (off < slen)
8046 {
8047 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8048 if (insn_check == check_lo
8049 ? !ok_lo_toc_insn(insn, r_type)
8050 : ((insn & ((0x3f << 26) | 0x1f << 16))
8051 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8052 {
8053 ppc_object->set_no_toc_opt();
8054 gold_warning(_("%s: toc optimization is not supported "
8055 "for %#08x instruction"),
8056 ppc_object->name().c_str(), insn);
8057 }
8058 }
8059 }
8060
8061 switch (r_type)
8062 {
8063 default:
8064 break;
8065 case elfcpp::R_PPC64_TOC16:
8066 case elfcpp::R_PPC64_TOC16_LO:
8067 case elfcpp::R_PPC64_TOC16_HI:
8068 case elfcpp::R_PPC64_TOC16_HA:
8069 case elfcpp::R_PPC64_TOC16_DS:
8070 case elfcpp::R_PPC64_TOC16_LO_DS:
8071 unsigned int shndx = lsym.get_st_shndx();
8072 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8073 bool is_ordinary;
8074 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8075 if (is_ordinary && shndx == ppc_object->toc_shndx())
8076 {
412294da 8077 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8078 if (dst_off < ppc_object->section_size(shndx))
8079 {
8080 bool ok = false;
8081 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8082 ok = true;
8083 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8084 {
8085 // Need to check that the insn is a ld
8086 if (!view)
8087 view = ppc_object->section_contents(data_shndx,
8088 &slen,
8089 false);
8090 section_size_type off =
8091 (convert_to_section_size_type(reloc.get_r_offset())
8092 + (big_endian ? -2 : 3));
8093 if (off < slen
8094 && (view[off] & (0x3f << 2)) == 58u << 2)
8095 ok = true;
8096 }
8097 if (!ok)
8098 ppc_object->set_no_toc_opt(dst_off);
8099 }
8100 }
8101 break;
8102 }
8103 }
8104
f159cdb6
AM
8105 if (size == 32)
8106 {
8107 switch (r_type)
8108 {
8109 case elfcpp::R_POWERPC_REL32:
8110 if (ppc_object->got2_shndx() != 0
8111 && parameters->options().output_is_position_independent())
8112 {
8113 unsigned int shndx = lsym.get_st_shndx();
8114 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8115 bool is_ordinary;
8116 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8117 if (is_ordinary && shndx == ppc_object->got2_shndx()
8118 && (ppc_object->section_flags(data_shndx)
8119 & elfcpp::SHF_EXECINSTR) != 0)
8120 gold_error(_("%s: unsupported -mbss-plt code"),
8121 ppc_object->name().c_str());
8122 }
8123 break;
8124 default:
8125 break;
8126 }
8127 }
8128
d8f5a274
AM
8129 switch (r_type)
8130 {
8131 case elfcpp::R_POWERPC_GOT_TLSLD16:
8132 case elfcpp::R_POWERPC_GOT_TLSGD16:
8133 case elfcpp::R_POWERPC_GOT_TPREL16:
8134 case elfcpp::R_POWERPC_GOT_DTPREL16:
8135 case elfcpp::R_POWERPC_GOT16:
8136 case elfcpp::R_PPC64_GOT16_DS:
8137 case elfcpp::R_PPC64_TOC16:
8138 case elfcpp::R_PPC64_TOC16_DS:
8139 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8140 break;
8141 default:
8142 break;
8143 }
8144
8145 switch (r_type)
8146 {
8147 case elfcpp::R_POWERPC_TPREL16:
8148 case elfcpp::R_POWERPC_TPREL16_LO:
8149 case elfcpp::R_POWERPC_TPREL16_HI:
8150 case elfcpp::R_POWERPC_TPREL16_HA:
8151 case elfcpp::R_PPC64_TPREL16_DS:
8152 case elfcpp::R_PPC64_TPREL16_LO_DS:
8153 case elfcpp::R_PPC64_TPREL16_HIGH:
8154 case elfcpp::R_PPC64_TPREL16_HIGHA:
8155 case elfcpp::R_PPC64_TPREL16_HIGHER:
8156 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8157 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8158 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8159 case elfcpp::R_PPC64_TPREL34:
8160 layout->set_has_static_tls();
8161 break;
8162 default:
8163 break;
8164 }
8165
8166 switch (r_type)
8167 {
8168 case elfcpp::R_PPC64_D34:
8169 case elfcpp::R_PPC64_D34_LO:
8170 case elfcpp::R_PPC64_D34_HI30:
8171 case elfcpp::R_PPC64_D34_HA30:
8172 case elfcpp::R_PPC64_D28:
8173 case elfcpp::R_PPC64_PCREL34:
8174 case elfcpp::R_PPC64_PCREL28:
8175 case elfcpp::R_PPC64_TPREL34:
8176 case elfcpp::R_PPC64_DTPREL34:
8177 case elfcpp::R_PPC64_PLT_PCREL34:
8178 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8179 case elfcpp::R_PPC64_GOT_PCREL34:
8180 case elfcpp::R_PPC64_GOT_TLSGD34:
8181 case elfcpp::R_PPC64_GOT_TLSLD34:
8182 case elfcpp::R_PPC64_GOT_DTPREL34:
8183 case elfcpp::R_PPC64_GOT_TPREL34:
7c1f4227 8184 target->set_power10_stubs();
89c52ae3 8185 break;
d8f5a274
AM
8186 default:
8187 break;
8188 }
dd93cd0a
AM
8189}
8190
8191// Report an unsupported relocation against a global symbol.
8192
8193template<int size, bool big_endian>
8194void
8195Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8196 Sized_relobj_file<size, big_endian>* object,
8197 unsigned int r_type,
8198 Symbol* gsym)
8199{
42cacb20
DE
8200 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8201 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8202}
8203
8204// Scan a relocation for a global symbol.
8205
8206template<int size, bool big_endian>
8207inline void
8208Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8209 Symbol_table* symtab,
8210 Layout* layout,
8211 Target_powerpc<size, big_endian>* target,
8212 Sized_relobj_file<size, big_endian>* object,
8213 unsigned int data_shndx,
8214 Output_section* output_section,
8215 const elfcpp::Rela<size, big_endian>& reloc,
8216 unsigned int r_type,
8217 Symbol* gsym)
42cacb20 8218{
34e0882b
AM
8219 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8220 == Track_tls::SKIP)
e3deeb9c
AM
8221 return;
8222
34e0882b
AM
8223 if (target->replace_tls_get_addr(gsym))
8224 // Change a __tls_get_addr reference to __tls_get_addr_opt
8225 // so dynamic relocs are emitted against the latter symbol.
8226 gsym = target->tls_get_addr_opt();
8227
e3deeb9c
AM
8228 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8229 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8230 {
8231 this->expect_tls_get_addr_call();
8232 const bool final = gsym->final_value_is_known();
8233 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8234 if (tls_type != tls::TLSOPT_NONE)
8235 this->skip_next_tls_get_addr_call();
8236 }
8237 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8238 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8239 {
8240 this->expect_tls_get_addr_call();
8241 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8242 if (tls_type != tls::TLSOPT_NONE)
8243 this->skip_next_tls_get_addr_call();
8244 }
8245
dd93cd0a
AM
8246 Powerpc_relobj<size, big_endian>* ppc_object
8247 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8248
e5d5f5ed 8249 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8250 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8251 bool pushed_ifunc = false;
8252 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8253 {
0e123f69 8254 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8255 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8256 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8257 target->make_plt_entry(symtab, layout, gsym);
9055360d 8258 pushed_ifunc = true;
ec661b9d 8259 }
e5d5f5ed 8260
42cacb20
DE
8261 switch (r_type)
8262 {
8263 case elfcpp::R_POWERPC_NONE:
8264 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8265 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8266 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8267 case elfcpp::R_POWERPC_TLS:
549dba71 8268 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8269 case elfcpp::R_POWERPC_PLTSEQ:
8270 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8271 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8272 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8273 case elfcpp::R_PPC64_PCREL_OPT:
8274 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8275 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8276 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8277 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8278 case elfcpp::R_PPC64_REL16_HIGHER34:
8279 case elfcpp::R_PPC64_REL16_HIGHERA34:
8280 case elfcpp::R_PPC64_REL16_HIGHEST34:
8281 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8282 case elfcpp::R_PPC64_D34:
8283 case elfcpp::R_PPC64_D34_LO:
8284 case elfcpp::R_PPC64_D34_HI30:
8285 case elfcpp::R_PPC64_D34_HA30:
8286 case elfcpp::R_PPC64_D28:
8287 case elfcpp::R_PPC64_PCREL34:
8288 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8289 case elfcpp::R_PPC64_TPREL34:
8290 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8291 break;
8292
8293 case elfcpp::R_PPC64_TOC:
8294 {
8295 Output_data_got_powerpc<size, big_endian>* got
8296 = target->got_section(symtab, layout);
8297 if (parameters->options().output_is_position_independent())
8298 {
bfdfa4cd
AM
8299 Address off = reloc.get_r_offset();
8300 if (size == 64
8301 && data_shndx == ppc_object->opd_shndx()
8302 && ppc_object->get_opd_discard(off - 8))
8303 break;
8304
dd93cd0a
AM
8305 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8306 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8307 if (data_shndx != ppc_object->opd_shndx())
8308 symobj = static_cast
8309 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8310 rela_dyn->add_output_section_relative(got->output_section(),
8311 elfcpp::R_POWERPC_RELATIVE,
8312 output_section,
bfdfa4cd 8313 object, data_shndx, off,
dd93cd0a
AM
8314 symobj->toc_base_offset());
8315 }
8316 }
42cacb20
DE
8317 break;
8318
c9269dff 8319 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8320 if (size == 64
9055360d 8321 && target->abiversion() < 2
bfdfa4cd
AM
8322 && data_shndx == ppc_object->opd_shndx()
8323 && (gsym->is_defined_in_discarded_section()
8324 || gsym->object() != object))
8325 {
8326 ppc_object->set_opd_discard(reloc.get_r_offset());
8327 break;
8328 }
d8e90251 8329 // Fall through.
dd93cd0a 8330 case elfcpp::R_PPC64_UADDR64:
c9269dff 8331 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8332 case elfcpp::R_POWERPC_UADDR32:
8333 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8334 case elfcpp::R_POWERPC_ADDR16:
8335 case elfcpp::R_POWERPC_ADDR16_LO:
8336 case elfcpp::R_POWERPC_ADDR16_HI:
8337 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8338 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8339 case elfcpp::R_PPC64_ADDR16_HIGH:
8340 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8341 case elfcpp::R_PPC64_ADDR16_HIGHER:
8342 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8343 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8344 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8345 case elfcpp::R_PPC64_ADDR16_DS:
8346 case elfcpp::R_PPC64_ADDR16_LO_DS:
8347 case elfcpp::R_POWERPC_ADDR14:
8348 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8349 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8350 {
c9269dff
AM
8351 // Make a PLT entry if necessary.
8352 if (gsym->needs_plt_entry())
8353 {
9055360d
AM
8354 // Since this is not a PC-relative relocation, we may be
8355 // taking the address of a function. In that case we need to
8356 // set the entry in the dynamic symbol table to the address of
8357 // the PLT call stub.
8358 bool need_ifunc_plt = false;
8359 if ((size == 32 || target->abiversion() >= 2)
8360 && gsym->is_from_dynobj()
8361 && !parameters->options().output_is_position_independent())
8362 {
8363 gsym->set_needs_dynsym_value();
8364 need_ifunc_plt = true;
8365 }
8366 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8367 {
0e123f69 8368 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8369 target->push_branch(ppc_object, data_shndx,
0e123f69 8370 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8371 reloc.get_r_addend());
8372 target->make_plt_entry(symtab, layout, gsym);
8373 }
c9269dff
AM
8374 }
8375 // Make a dynamic relocation if necessary.
88b8e639 8376 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8377 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8378 {
a82bef93
ST
8379 if (!parameters->options().output_is_position_independent()
8380 && gsym->may_need_copy_reloc())
c9269dff
AM
8381 {
8382 target->copy_reloc(symtab, layout, object,
8383 data_shndx, output_section, gsym, reloc);
8384 }
9055360d
AM
8385 else if ((((size == 32
8386 && r_type == elfcpp::R_POWERPC_ADDR32)
8387 || (size == 64
8388 && r_type == elfcpp::R_PPC64_ADDR64
8389 && target->abiversion() >= 2))
627b30b7
AM
8390 && gsym->can_use_relative_reloc(false)
8391 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8392 && parameters->options().shared()))
8393 || (size == 64
8394 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8395 && target->abiversion() < 2
627b30b7
AM
8396 && (gsym->can_use_relative_reloc(false)
8397 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8398 {
b3ccdeb5
AM
8399 Reloc_section* rela_dyn
8400 = target->rela_dyn_section(symtab, layout, is_ifunc);
8401 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8402 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8403 rela_dyn->add_symbolless_global_addend(
8404 gsym, dynrel, output_section, object, data_shndx,
8405 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
8406 }
8407 else
8408 {
b3ccdeb5
AM
8409 Reloc_section* rela_dyn
8410 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 8411 check_non_pic(object, r_type);
dd93cd0a
AM
8412 rela_dyn->add_global(gsym, r_type, output_section,
8413 object, data_shndx,
8414 reloc.get_r_offset(),
8415 reloc.get_r_addend());
5edad15d
AM
8416
8417 if (size == 64
8418 && parameters->options().toc_optimize()
8419 && data_shndx == ppc_object->toc_shndx())
8420 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
8421 }
8422 }
42cacb20
DE
8423 }
8424 break;
8425
e4dff765
AM
8426 case elfcpp::R_PPC64_PLT_PCREL34:
8427 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8428 case elfcpp::R_POWERPC_PLT16_LO:
8429 case elfcpp::R_POWERPC_PLT16_HI:
8430 case elfcpp::R_POWERPC_PLT16_HA:
8431 case elfcpp::R_PPC64_PLT16_LO_DS:
8432 if (!pushed_ifunc)
8433 target->make_plt_entry(symtab, layout, gsym);
8434 break;
8435
32f59844
AM
8436 case elfcpp::R_PPC64_REL24_NOTOC:
8437 if (size == 32)
8438 break;
8439 // Fall through.
cf43a2fe 8440 case elfcpp::R_PPC_PLTREL24:
42cacb20 8441 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
8442 if (!is_ifunc)
8443 {
0e123f69 8444 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8445 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8446 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
8447 if (gsym->needs_plt_entry()
8448 || (!gsym->final_value_is_known()
8449 && (gsym->is_undefined()
8450 || gsym->is_from_dynobj()
8451 || gsym->is_preemptible())))
8452 target->make_plt_entry(symtab, layout, gsym);
8453 }
d8e90251 8454 // Fall through.
42cacb20 8455
3ea0a085 8456 case elfcpp::R_PPC64_REL64:
dd93cd0a 8457 case elfcpp::R_POWERPC_REL32:
3ea0a085 8458 // Make a dynamic relocation if necessary.
88b8e639 8459 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 8460 {
a82bef93
ST
8461 if (!parameters->options().output_is_position_independent()
8462 && gsym->may_need_copy_reloc())
3ea0a085
AM
8463 {
8464 target->copy_reloc(symtab, layout, object,
8465 data_shndx, output_section, gsym,
8466 reloc);
8467 }
8468 else
8469 {
b3ccdeb5
AM
8470 Reloc_section* rela_dyn
8471 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
8472 check_non_pic(object, r_type);
8473 rela_dyn->add_global(gsym, r_type, output_section, object,
8474 data_shndx, reloc.get_r_offset(),
8475 reloc.get_r_addend());
8476 }
8477 }
8478 break;
8479
ec661b9d
AM
8480 case elfcpp::R_POWERPC_REL14:
8481 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8482 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8483 if (!is_ifunc)
0e123f69
AM
8484 {
8485 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8486 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8487 r_type, r_sym, reloc.get_r_addend());
8488 }
ec661b9d
AM
8489 break;
8490
7e57d19e
AM
8491 case elfcpp::R_PPC64_TOCSAVE:
8492 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8493 // caller has already saved r2 and thus a plt call stub need not
8494 // save r2.
8495 if (size == 64
8496 && target->mark_pltcall(ppc_object, data_shndx,
8497 reloc.get_r_offset() - 4, symtab))
8498 {
8499 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8500 bool is_ordinary;
8501 unsigned int shndx = gsym->shndx(&is_ordinary);
8502 if (!is_ordinary)
8503 object->error(_("tocsave symbol %u has bad shndx %u"),
8504 r_sym, shndx);
8505 else
8506 {
8507 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8508 target->add_tocsave(ppc_object, shndx,
8509 sym->value() + reloc.get_r_addend());
8510 }
8511 }
8512 break;
8513
6ce78956
AM
8514 case elfcpp::R_POWERPC_REL16:
8515 case elfcpp::R_POWERPC_REL16_LO:
8516 case elfcpp::R_POWERPC_REL16_HI:
8517 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8518 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8519 case elfcpp::R_PPC64_REL16_HIGH:
8520 case elfcpp::R_PPC64_REL16_HIGHA:
8521 case elfcpp::R_PPC64_REL16_HIGHER:
8522 case elfcpp::R_PPC64_REL16_HIGHERA:
8523 case elfcpp::R_PPC64_REL16_HIGHEST:
8524 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8525 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8526 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8527 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8528 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8529 case elfcpp::R_PPC64_SECTOFF_DS:
8530 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8531 case elfcpp::R_POWERPC_TPREL16:
8532 case elfcpp::R_POWERPC_TPREL16_LO:
8533 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8534 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8535 case elfcpp::R_PPC64_TPREL16_DS:
8536 case elfcpp::R_PPC64_TPREL16_LO_DS:
8537 case elfcpp::R_PPC64_TPREL16_HIGH:
8538 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8539 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8540 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8541 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8542 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8543 case elfcpp::R_POWERPC_DTPREL16:
8544 case elfcpp::R_POWERPC_DTPREL16_LO:
8545 case elfcpp::R_POWERPC_DTPREL16_HI:
8546 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8547 case elfcpp::R_PPC64_DTPREL16_DS:
8548 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8549 case elfcpp::R_PPC64_DTPREL16_HIGH:
8550 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8551 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8552 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8553 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8554 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8555 case elfcpp::R_PPC64_TLSGD:
8556 case elfcpp::R_PPC64_TLSLD:
45965137 8557 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
8558 break;
8559
e4dff765 8560 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8561 case elfcpp::R_POWERPC_GOT16:
8562 case elfcpp::R_POWERPC_GOT16_LO:
8563 case elfcpp::R_POWERPC_GOT16_HI:
8564 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8565 case elfcpp::R_PPC64_GOT16_DS:
8566 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8567 {
c9269dff
AM
8568 // The symbol requires a GOT entry.
8569 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
8570
8571 got = target->got_section(symtab, layout);
2e702c99 8572 if (gsym->final_value_is_known())
2e702c99 8573 {
b01a4b04
AM
8574 if (is_ifunc
8575 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8576 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8577 else
8578 got->add_global(gsym, GOT_TYPE_STANDARD);
8579 }
8580 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8581 {
8582 // If we are generating a shared object or a pie, this
8583 // symbol's GOT entry will be set by a dynamic relocation.
8584 unsigned int off = got->add_constant(0);
8585 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8586
b3ccdeb5
AM
8587 Reloc_section* rela_dyn
8588 = target->rela_dyn_section(symtab, layout, is_ifunc);
8589
e5d5f5ed 8590 if (gsym->can_use_relative_reloc(false)
9055360d
AM
8591 && !((size == 32
8592 || target->abiversion() >= 2)
e5d5f5ed
AM
8593 && gsym->visibility() == elfcpp::STV_PROTECTED
8594 && parameters->options().shared()))
2e702c99 8595 {
b3ccdeb5
AM
8596 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8597 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8598 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8599 }
8600 else
8601 {
8602 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8603 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 8604 }
2e702c99 8605 }
42cacb20
DE
8606 }
8607 break;
8608
cf43a2fe
AM
8609 case elfcpp::R_PPC64_TOC16:
8610 case elfcpp::R_PPC64_TOC16_LO:
8611 case elfcpp::R_PPC64_TOC16_HI:
8612 case elfcpp::R_PPC64_TOC16_HA:
8613 case elfcpp::R_PPC64_TOC16_DS:
8614 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8615 // We need a GOT section.
8616 target->got_section(symtab, layout);
8617 break;
8618
89c52ae3 8619 case elfcpp::R_PPC64_GOT_TLSGD34:
dd93cd0a
AM
8620 case elfcpp::R_POWERPC_GOT_TLSGD16:
8621 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8622 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8623 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8624 {
8625 const bool final = gsym->final_value_is_known();
8626 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8627 if (tls_type == tls::TLSOPT_NONE)
8628 {
8629 Output_data_got_powerpc<size, big_endian>* got
8630 = target->got_section(symtab, layout);
b3ccdeb5
AM
8631 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8632 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
8633 elfcpp::R_POWERPC_DTPMOD,
8634 elfcpp::R_POWERPC_DTPREL);
8635 }
8636 else if (tls_type == tls::TLSOPT_TO_IE)
8637 {
acc276d8
AM
8638 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8639 {
8640 Output_data_got_powerpc<size, big_endian>* got
8641 = target->got_section(symtab, layout);
8642 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8643 if (gsym->is_undefined()
8644 || gsym->is_from_dynobj())
8645 {
8646 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8647 elfcpp::R_POWERPC_TPREL);
8648 }
8649 else
8650 {
8651 unsigned int off = got->add_constant(0);
8652 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8653 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8654 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8655 got, off, 0);
8656 }
8657 }
dd93cd0a
AM
8658 }
8659 else if (tls_type == tls::TLSOPT_TO_LE)
8660 {
8661 // no GOT relocs needed for Local Exec.
8662 }
8663 else
8664 gold_unreachable();
8665 }
42cacb20
DE
8666 break;
8667
89c52ae3 8668 case elfcpp::R_PPC64_GOT_TLSLD34:
dd93cd0a
AM
8669 case elfcpp::R_POWERPC_GOT_TLSLD16:
8670 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8671 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8672 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8673 {
8674 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8675 if (tls_type == tls::TLSOPT_NONE)
8676 target->tlsld_got_offset(symtab, layout, object);
8677 else if (tls_type == tls::TLSOPT_TO_LE)
8678 {
8679 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8680 if (parameters->options().emit_relocs())
8681 {
8682 Output_section* os = layout->tls_segment()->first_section();
8683 gold_assert(os != NULL);
8684 os->set_needs_symtab_index();
8685 }
dd93cd0a
AM
8686 }
8687 else
8688 gold_unreachable();
8689 }
8690 break;
8691
89c52ae3 8692 case elfcpp::R_PPC64_GOT_DTPREL34:
dd93cd0a
AM
8693 case elfcpp::R_POWERPC_GOT_DTPREL16:
8694 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8695 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8696 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8697 {
8698 Output_data_got_powerpc<size, big_endian>* got
8699 = target->got_section(symtab, layout);
bd73a62d
AM
8700 if (!gsym->final_value_is_known()
8701 && (gsym->is_from_dynobj()
8702 || gsym->is_undefined()
8703 || gsym->is_preemptible()))
8704 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
8705 target->rela_dyn_section(layout),
8706 elfcpp::R_POWERPC_DTPREL);
8707 else
8708 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
8709 }
8710 break;
8711
89c52ae3 8712 case elfcpp::R_PPC64_GOT_TPREL34:
dd93cd0a
AM
8713 case elfcpp::R_POWERPC_GOT_TPREL16:
8714 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8715 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8716 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8717 {
8718 const bool final = gsym->final_value_is_known();
8719 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8720 if (tls_type == tls::TLSOPT_NONE)
8721 {
acc276d8
AM
8722 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8723 {
8724 Output_data_got_powerpc<size, big_endian>* got
8725 = target->got_section(symtab, layout);
8726 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8727 if (gsym->is_undefined()
8728 || gsym->is_from_dynobj())
8729 {
8730 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8731 elfcpp::R_POWERPC_TPREL);
8732 }
8733 else
8734 {
8735 unsigned int off = got->add_constant(0);
8736 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8737 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8738 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8739 got, off, 0);
8740 }
8741 }
dd93cd0a
AM
8742 }
8743 else if (tls_type == tls::TLSOPT_TO_LE)
8744 {
8745 // no GOT relocs needed for Local Exec.
8746 }
8747 else
8748 gold_unreachable();
8749 }
42cacb20
DE
8750 break;
8751
8752 default:
8753 unsupported_reloc_global(object, r_type, gsym);
8754 break;
8755 }
d8f5a274 8756
5edad15d
AM
8757 if (size == 64
8758 && parameters->options().toc_optimize())
8759 {
8760 if (data_shndx == ppc_object->toc_shndx())
8761 {
8762 bool ok = true;
8763 if (r_type != elfcpp::R_PPC64_ADDR64
8764 || (is_ifunc && target->abiversion() < 2))
8765 ok = false;
8766 else if (parameters->options().output_is_position_independent()
8767 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
8768 ok = false;
8769 if (!ok)
8770 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8771 }
8772
8773 enum {no_check, check_lo, check_ha} insn_check;
8774 switch (r_type)
8775 {
8776 default:
8777 insn_check = no_check;
8778 break;
8779
8780 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8781 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8782 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8783 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8784 case elfcpp::R_POWERPC_GOT16_HA:
8785 case elfcpp::R_PPC64_TOC16_HA:
8786 insn_check = check_ha;
8787 break;
8788
8789 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8790 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8791 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8792 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8793 case elfcpp::R_POWERPC_GOT16_LO:
8794 case elfcpp::R_PPC64_GOT16_LO_DS:
8795 case elfcpp::R_PPC64_TOC16_LO:
8796 case elfcpp::R_PPC64_TOC16_LO_DS:
8797 insn_check = check_lo;
8798 break;
8799 }
8800
8801 section_size_type slen;
8802 const unsigned char* view = NULL;
8803 if (insn_check != no_check)
8804 {
8805 view = ppc_object->section_contents(data_shndx, &slen, false);
8806 section_size_type off =
8807 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8808 if (off < slen)
8809 {
8810 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8811 if (insn_check == check_lo
8812 ? !ok_lo_toc_insn(insn, r_type)
8813 : ((insn & ((0x3f << 26) | 0x1f << 16))
8814 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8815 {
8816 ppc_object->set_no_toc_opt();
8817 gold_warning(_("%s: toc optimization is not supported "
8818 "for %#08x instruction"),
8819 ppc_object->name().c_str(), insn);
8820 }
8821 }
8822 }
8823
8824 switch (r_type)
8825 {
8826 default:
8827 break;
8828 case elfcpp::R_PPC64_TOC16:
8829 case elfcpp::R_PPC64_TOC16_LO:
8830 case elfcpp::R_PPC64_TOC16_HI:
8831 case elfcpp::R_PPC64_TOC16_HA:
8832 case elfcpp::R_PPC64_TOC16_DS:
8833 case elfcpp::R_PPC64_TOC16_LO_DS:
8834 if (gsym->source() == Symbol::FROM_OBJECT
8835 && !gsym->object()->is_dynamic())
8836 {
8837 Powerpc_relobj<size, big_endian>* sym_object
8838 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8839 bool is_ordinary;
8840 unsigned int shndx = gsym->shndx(&is_ordinary);
8841 if (shndx == sym_object->toc_shndx())
8842 {
8843 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 8844 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
8845 if (dst_off < sym_object->section_size(shndx))
8846 {
8847 bool ok = false;
8848 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8849 ok = true;
8850 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8851 {
8852 // Need to check that the insn is a ld
8853 if (!view)
8854 view = ppc_object->section_contents(data_shndx,
8855 &slen,
8856 false);
8857 section_size_type off =
8858 (convert_to_section_size_type(reloc.get_r_offset())
8859 + (big_endian ? -2 : 3));
8860 if (off < slen
8861 && (view[off] & (0x3f << 2)) == (58u << 2))
8862 ok = true;
8863 }
8864 if (!ok)
8865 sym_object->set_no_toc_opt(dst_off);
8866 }
8867 }
8868 }
8869 break;
8870 }
8871 }
8872
f159cdb6
AM
8873 if (size == 32)
8874 {
8875 switch (r_type)
8876 {
8877 case elfcpp::R_PPC_LOCAL24PC:
8878 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8879 gold_error(_("%s: unsupported -mbss-plt code"),
8880 ppc_object->name().c_str());
8881 break;
8882 default:
8883 break;
8884 }
8885 }
8886
d8f5a274
AM
8887 switch (r_type)
8888 {
8889 case elfcpp::R_POWERPC_GOT_TLSLD16:
8890 case elfcpp::R_POWERPC_GOT_TLSGD16:
8891 case elfcpp::R_POWERPC_GOT_TPREL16:
8892 case elfcpp::R_POWERPC_GOT_DTPREL16:
8893 case elfcpp::R_POWERPC_GOT16:
8894 case elfcpp::R_PPC64_GOT16_DS:
8895 case elfcpp::R_PPC64_TOC16:
8896 case elfcpp::R_PPC64_TOC16_DS:
8897 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8898 break;
8899 default:
8900 break;
8901 }
8902
8903 switch (r_type)
8904 {
8905 case elfcpp::R_POWERPC_TPREL16:
8906 case elfcpp::R_POWERPC_TPREL16_LO:
8907 case elfcpp::R_POWERPC_TPREL16_HI:
8908 case elfcpp::R_POWERPC_TPREL16_HA:
8909 case elfcpp::R_PPC64_TPREL16_DS:
8910 case elfcpp::R_PPC64_TPREL16_LO_DS:
8911 case elfcpp::R_PPC64_TPREL16_HIGH:
8912 case elfcpp::R_PPC64_TPREL16_HIGHA:
8913 case elfcpp::R_PPC64_TPREL16_HIGHER:
8914 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8915 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8916 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8917 case elfcpp::R_PPC64_TPREL34:
8918 layout->set_has_static_tls();
8919 break;
8920 default:
8921 break;
8922 }
8923
8924 switch (r_type)
8925 {
8926 case elfcpp::R_PPC64_D34:
8927 case elfcpp::R_PPC64_D34_LO:
8928 case elfcpp::R_PPC64_D34_HI30:
8929 case elfcpp::R_PPC64_D34_HA30:
8930 case elfcpp::R_PPC64_D28:
8931 case elfcpp::R_PPC64_PCREL34:
8932 case elfcpp::R_PPC64_PCREL28:
8933 case elfcpp::R_PPC64_TPREL34:
8934 case elfcpp::R_PPC64_DTPREL34:
8935 case elfcpp::R_PPC64_PLT_PCREL34:
8936 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8937 case elfcpp::R_PPC64_GOT_PCREL34:
8938 case elfcpp::R_PPC64_GOT_TLSGD34:
8939 case elfcpp::R_PPC64_GOT_TLSLD34:
8940 case elfcpp::R_PPC64_GOT_DTPREL34:
8941 case elfcpp::R_PPC64_GOT_TPREL34:
7c1f4227 8942 target->set_power10_stubs();
89c52ae3 8943 break;
d8f5a274
AM
8944 default:
8945 break;
8946 }
42cacb20
DE
8947}
8948
6d03d481
ST
8949// Process relocations for gc.
8950
8951template<int size, bool big_endian>
8952void
8953Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
8954 Symbol_table* symtab,
8955 Layout* layout,
8956 Sized_relobj_file<size, big_endian>* object,
8957 unsigned int data_shndx,
8958 unsigned int,
8959 const unsigned char* prelocs,
8960 size_t reloc_count,
8961 Output_section* output_section,
8962 bool needs_special_offset_handling,
8963 size_t local_symbol_count,
8964 const unsigned char* plocal_symbols)
6d03d481
ST
8965{
8966 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
8967 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8968 Classify_reloc;
8969
e81fea4d
AM
8970 Powerpc_relobj<size, big_endian>* ppc_object
8971 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8972 if (size == 64)
8973 ppc_object->set_opd_valid();
8974 if (size == 64 && data_shndx == ppc_object->opd_shndx())
8975 {
8976 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
8977 for (p = ppc_object->access_from_map()->begin();
8978 p != ppc_object->access_from_map()->end();
8979 ++p)
8980 {
8981 Address dst_off = p->first;
8982 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8983 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
8984 for (s = p->second.begin(); s != p->second.end(); ++s)
8985 {
efc6fa12 8986 Relobj* src_obj = s->first;
e81fea4d
AM
8987 unsigned int src_indx = s->second;
8988 symtab->gc()->add_reference(src_obj, src_indx,
8989 ppc_object, dst_indx);
8990 }
8991 p->second.clear();
8992 }
8993 ppc_object->access_from_map()->clear();
c6de8ed4 8994 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
8995 // Don't look at .opd relocs as .opd will reference everything.
8996 return;
8997 }
6d03d481 8998
4d625b70 8999 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9000 symtab,
9001 layout,
9002 this,
9003 object,
9004 data_shndx,
9005 prelocs,
9006 reloc_count,
9007 output_section,
9008 needs_special_offset_handling,
9009 local_symbol_count,
9010 plocal_symbols);
9011}
9012
e81fea4d
AM
9013// Handle target specific gc actions when adding a gc reference from
9014// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9015// and DST_OFF. For powerpc64, this adds a referenc to the code
9016// section of a function descriptor.
9017
9018template<int size, bool big_endian>
9019void
9020Target_powerpc<size, big_endian>::do_gc_add_reference(
9021 Symbol_table* symtab,
efc6fa12 9022 Relobj* src_obj,
e81fea4d 9023 unsigned int src_shndx,
efc6fa12 9024 Relobj* dst_obj,
e81fea4d
AM
9025 unsigned int dst_shndx,
9026 Address dst_off) const
9027{
6c77229c
AM
9028 if (size != 64 || dst_obj->is_dynamic())
9029 return;
9030
e81fea4d
AM
9031 Powerpc_relobj<size, big_endian>* ppc_object
9032 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9033 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9034 {
9035 if (ppc_object->opd_valid())
9036 {
9037 dst_shndx = ppc_object->get_opd_ent(dst_off);
9038 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9039 }
9040 else
9041 {
9042 // If we haven't run scan_opd_relocs, we must delay
9043 // processing this function descriptor reference.
9044 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9045 }
9046 }
9047}
9048
9049// Add any special sections for this symbol to the gc work list.
9050// For powerpc64, this adds the code section of a function
9051// descriptor.
9052
9053template<int size, bool big_endian>
9054void
9055Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9056 Symbol_table* symtab,
9057 Symbol* sym) const
9058{
6a31512f 9059 if (size == 64 && sym->object()->pluginobj() == NULL)
e81fea4d
AM
9060 {
9061 Powerpc_relobj<size, big_endian>* ppc_object
9062 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9063 bool is_ordinary;
9064 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9065 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9066 {
9067 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9068 Address dst_off = gsym->value();
c6de8ed4
AM
9069 if (ppc_object->opd_valid())
9070 {
9071 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9072 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9073 dst_indx));
c6de8ed4
AM
9074 }
9075 else
9076 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9077 }
9078 }
9079}
9080
dc3714f3
AM
9081// For a symbol location in .opd, set LOC to the location of the
9082// function entry.
9083
9084template<int size, bool big_endian>
9085void
9086Target_powerpc<size, big_endian>::do_function_location(
9087 Symbol_location* loc) const
9088{
a2d7bf59 9089 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9090 {
9091 if (loc->object->is_dynamic())
9092 {
9093 Powerpc_dynobj<size, big_endian>* ppc_object
9094 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9095 if (loc->shndx == ppc_object->opd_shndx())
9096 {
9097 Address dest_off;
9098 Address off = loc->offset - ppc_object->opd_address();
9099 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9100 loc->offset = dest_off;
9101 }
9102 }
9103 else
9104 {
9105 const Powerpc_relobj<size, big_endian>* ppc_object
9106 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9107 if (loc->shndx == ppc_object->opd_shndx())
9108 {
9109 Address dest_off;
9110 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9111 loc->offset = dest_off;
9112 }
9113 }
9114 }
9115}
9116
bbec1a5d
AM
9117// FNOFFSET in section SHNDX in OBJECT is the start of a function
9118// compiled with -fsplit-stack. The function calls non-split-stack
9119// code. Change the function to ensure it has enough stack space to
9120// call some random function.
9121
9122template<int size, bool big_endian>
9123void
9124Target_powerpc<size, big_endian>::do_calls_non_split(
9125 Relobj* object,
9126 unsigned int shndx,
9127 section_offset_type fnoffset,
9128 section_size_type fnsize,
6e0813d3
CC
9129 const unsigned char* prelocs,
9130 size_t reloc_count,
bbec1a5d
AM
9131 unsigned char* view,
9132 section_size_type view_size,
9133 std::string* from,
9134 std::string* to) const
9135{
9136 // 32-bit not supported.
9137 if (size == 32)
9138 {
9139 // warn
9140 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9141 prelocs, reloc_count, view, view_size,
9142 from, to);
bbec1a5d
AM
9143 return;
9144 }
9145
9146 // The function always starts with
9147 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9148 // addis %r12,%r1,-allocate@ha
9149 // addi %r12,%r12,-allocate@l
9150 // cmpld %r12,%r0
9151 // but note that the addis or addi may be replaced with a nop
9152
9153 unsigned char *entry = view + fnoffset;
9154 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9155
9156 if ((insn & 0xffff0000) == addis_2_12)
9157 {
9158 /* Skip ELFv2 global entry code. */
9159 entry += 8;
9160 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9161 }
9162
9163 unsigned char *pinsn = entry;
9164 bool ok = false;
9165 const uint32_t ld_private_ss = 0xe80d8fc0;
9166 if (insn == ld_private_ss)
9167 {
9168 int32_t allocate = 0;
9169 while (1)
9170 {
9171 pinsn += 4;
9172 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9173 if ((insn & 0xffff0000) == addis_12_1)
9174 allocate += (insn & 0xffff) << 16;
9175 else if ((insn & 0xffff0000) == addi_12_1
9176 || (insn & 0xffff0000) == addi_12_12)
9177 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9178 else if (insn != nop)
9179 break;
9180 }
9181 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9182 {
9183 int extra = parameters->options().split_stack_adjust_size();
9184 allocate -= extra;
9185 if (allocate >= 0 || extra < 0)
9186 {
9187 object->error(_("split-stack stack size overflow at "
9188 "section %u offset %0zx"),
9189 shndx, static_cast<size_t>(fnoffset));
9190 return;
9191 }
9192 pinsn = entry + 4;
9193 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9194 if (insn != addis_12_1)
9195 {
9196 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9197 pinsn += 4;
9198 insn = addi_12_12 | (allocate & 0xffff);
9199 if (insn != addi_12_12)
9200 {
9201 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9202 pinsn += 4;
9203 }
9204 }
9205 else
9206 {
9207 insn = addi_12_1 | (allocate & 0xffff);
9208 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9209 pinsn += 4;
9210 }
9211 if (pinsn != entry + 12)
9212 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9213
9214 ok = true;
9215 }
9216 }
9217
9218 if (!ok)
9219 {
9220 if (!object->has_no_split_stack())
9221 object->error(_("failed to match split-stack sequence at "
9222 "section %u offset %0zx"),
9223 shndx, static_cast<size_t>(fnoffset));
9224 }
9225}
9226
42cacb20
DE
9227// Scan relocations for a section.
9228
9229template<int size, bool big_endian>
9230void
9231Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9232 Symbol_table* symtab,
9233 Layout* layout,
9234 Sized_relobj_file<size, big_endian>* object,
9235 unsigned int data_shndx,
9236 unsigned int sh_type,
9237 const unsigned char* prelocs,
9238 size_t reloc_count,
9239 Output_section* output_section,
9240 bool needs_special_offset_handling,
9241 size_t local_symbol_count,
9242 const unsigned char* plocal_symbols)
42cacb20
DE
9243{
9244 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9245 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9246 Classify_reloc;
42cacb20 9247
7ee7ff70
AM
9248 if (!this->plt_localentry0_init_)
9249 {
9250 bool plt_localentry0 = false;
9251 if (size == 64
9252 && this->abiversion() >= 2)
9253 {
9254 if (parameters->options().user_set_plt_localentry())
9255 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9256 if (plt_localentry0
9257 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9258 gold_warning(_("--plt-localentry is especially dangerous without "
9259 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9260 }
9261 this->plt_localentry0_ = plt_localentry0;
9262 this->plt_localentry0_init_ = true;
9263 }
9264
42cacb20
DE
9265 if (sh_type == elfcpp::SHT_REL)
9266 {
9267 gold_error(_("%s: unsupported REL reloc section"),
9268 object->name().c_str());
9269 return;
9270 }
9271
4d625b70 9272 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9273 symtab,
9274 layout,
9275 this,
9276 object,
9277 data_shndx,
9278 prelocs,
9279 reloc_count,
9280 output_section,
9281 needs_special_offset_handling,
9282 local_symbol_count,
9283 plocal_symbols);
9284}
9285
ec4dbad3
AM
9286// Functor class for processing the global symbol table.
9287// Removes symbols defined on discarded opd entries.
9288
9289template<bool big_endian>
9290class Global_symbol_visitor_opd
9291{
9292 public:
9293 Global_symbol_visitor_opd()
9294 { }
9295
9296 void
9297 operator()(Sized_symbol<64>* sym)
9298 {
9299 if (sym->has_symtab_index()
9300 || sym->source() != Symbol::FROM_OBJECT
9301 || !sym->in_real_elf())
9302 return;
9303
6c77229c
AM
9304 if (sym->object()->is_dynamic())
9305 return;
9306
ec4dbad3
AM
9307 Powerpc_relobj<64, big_endian>* symobj
9308 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9309 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9310 return;
9311
9312 bool is_ordinary;
9313 unsigned int shndx = sym->shndx(&is_ordinary);
9314 if (shndx == symobj->opd_shndx()
9315 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9316 {
9317 sym->set_undefined();
e3ee8ed4 9318 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9319 sym->set_is_defined_in_discarded_section();
9320 sym->set_symtab_index(-1U);
9321 }
ec4dbad3
AM
9322 }
9323};
9324
f3a0ed29
AM
9325template<int size, bool big_endian>
9326void
9327Target_powerpc<size, big_endian>::define_save_restore_funcs(
9328 Layout* layout,
9329 Symbol_table* symtab)
9330{
9331 if (size == 64)
9332 {
d49044c7
AM
9333 Output_data_save_res<size, big_endian>* savres
9334 = new Output_data_save_res<size, big_endian>(symtab);
9335 this->savres_section_ = savres;
f3a0ed29
AM
9336 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9337 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9338 savres, ORDER_TEXT, false);
9339 }
9340}
9341
d8f5a274
AM
9342// Sort linker created .got section first (for the header), then input
9343// sections belonging to files using small model code.
9344
9345template<bool big_endian>
9346class Sort_toc_sections
9347{
9348 public:
9349 bool
9350 operator()(const Output_section::Input_section& is1,
9351 const Output_section::Input_section& is2) const
9352 {
9353 if (!is1.is_input_section() && is2.is_input_section())
9354 return true;
9355 bool small1
9356 = (is1.is_input_section()
9357 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9358 ->has_small_toc_reloc()));
9359 bool small2
9360 = (is2.is_input_section()
9361 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9362 ->has_small_toc_reloc()));
9363 return small1 && !small2;
9364 }
9365};
9366
42cacb20
DE
9367// Finalize the sections.
9368
9369template<int size, bool big_endian>
9370void
d5b40221
DK
9371Target_powerpc<size, big_endian>::do_finalize_sections(
9372 Layout* layout,
724436fc 9373 const Input_objects* input_objects,
ec4dbad3 9374 Symbol_table* symtab)
42cacb20 9375{
c9824451
AM
9376 if (parameters->doing_static_link())
9377 {
9378 // At least some versions of glibc elf-init.o have a strong
9379 // reference to __rela_iplt marker syms. A weak ref would be
9380 // better..
9381 if (this->iplt_ != NULL)
9382 {
9383 Reloc_section* rel = this->iplt_->rel_plt();
9384 symtab->define_in_output_data("__rela_iplt_start", NULL,
9385 Symbol_table::PREDEFINED, rel, 0, 0,
9386 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9387 elfcpp::STV_HIDDEN, 0, false, true);
9388 symtab->define_in_output_data("__rela_iplt_end", NULL,
9389 Symbol_table::PREDEFINED, rel, 0, 0,
9390 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9391 elfcpp::STV_HIDDEN, 0, true, true);
9392 }
9393 else
9394 {
9395 symtab->define_as_constant("__rela_iplt_start", NULL,
9396 Symbol_table::PREDEFINED, 0, 0,
9397 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9398 elfcpp::STV_HIDDEN, 0, true, false);
9399 symtab->define_as_constant("__rela_iplt_end", NULL,
9400 Symbol_table::PREDEFINED, 0, 0,
9401 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9402 elfcpp::STV_HIDDEN, 0, true, false);
9403 }
9404 }
9405
ec4dbad3
AM
9406 if (size == 64)
9407 {
9408 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9409 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
9410
9411 if (!parameters->options().relocatable())
9412 {
9413 this->define_save_restore_funcs(layout, symtab);
9414
9415 // Annoyingly, we need to make these sections now whether or
9416 // not we need them. If we delay until do_relax then we
9417 // need to mess with the relaxation machinery checkpointing.
9418 this->got_section(symtab, layout);
9419 this->make_brlt_section(layout);
d8f5a274
AM
9420
9421 if (parameters->options().toc_sort())
9422 {
9423 Output_section* os = this->got_->output_section();
9424 if (os != NULL && os->input_sections().size() > 1)
9425 std::stable_sort(os->input_sections().begin(),
9426 os->input_sections().end(),
9427 Sort_toc_sections<big_endian>());
9428 }
ec661b9d 9429 }
ec4dbad3
AM
9430 }
9431
42cacb20 9432 // Fill in some more dynamic tags.
c9269dff 9433 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 9434 if (odyn != NULL)
cf43a2fe 9435 {
c9824451
AM
9436 const Reloc_section* rel_plt = (this->plt_ == NULL
9437 ? NULL
9438 : this->plt_->rel_plt());
9439 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9440 this->rela_dyn_, true, size == 32);
9441
9442 if (size == 32)
dd93cd0a 9443 {
c9824451
AM
9444 if (this->got_ != NULL)
9445 {
9446 this->got_->finalize_data_size();
9447 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9448 this->got_, this->got_->g_o_t());
9449 }
34e0882b
AM
9450 if (this->has_tls_get_addr_opt_)
9451 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 9452 }
c9824451 9453 else
dd93cd0a 9454 {
c9824451
AM
9455 if (this->glink_ != NULL)
9456 {
9457 this->glink_->finalize_data_size();
9458 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9459 this->glink_,
9e390558 9460 (this->glink_->pltresolve_size()
c9824451
AM
9461 - 32));
9462 }
34e0882b 9463 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 9464 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
9465 ((this->has_localentry0_
9466 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9467 | (this->has_tls_get_addr_opt_
9468 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 9469 }
c9269dff 9470 }
cf43a2fe 9471
42cacb20
DE
9472 // Emit any relocs we saved in an attempt to avoid generating COPY
9473 // relocs.
9474 if (this->copy_relocs_.any_saved_relocs())
9475 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
9476
9477 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9478 p != input_objects->relobj_end();
9479 ++p)
9480 {
9481 Powerpc_relobj<size, big_endian>* ppc_relobj
9482 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9483 if (ppc_relobj->attributes_section_data())
6f3fe02b 9484 this->merge_object_attributes(ppc_relobj,
724436fc
AM
9485 ppc_relobj->attributes_section_data());
9486 }
9487 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9488 p != input_objects->dynobj_end();
9489 ++p)
9490 {
9491 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9492 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9493 if (ppc_dynobj->attributes_section_data())
6f3fe02b 9494 this->merge_object_attributes(ppc_dynobj,
724436fc
AM
9495 ppc_dynobj->attributes_section_data());
9496 }
9497
9498 // Create a .gnu.attributes section if we have merged any attributes
9499 // from inputs.
9500 if (this->attributes_section_data_ != NULL
9501 && this->attributes_section_data_->size() != 0)
9502 {
9503 Output_attributes_section_data* attributes_section
9504 = new Output_attributes_section_data(*this->attributes_section_data_);
9505 layout->add_output_section_data(".gnu.attributes",
9506 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9507 attributes_section, ORDER_INVALID, false);
9508 }
9509}
9510
9511// Merge object attributes from input file called NAME with those of the
9512// output. The input object attributes are in the object pointed by PASD.
9513
9514template<int size, bool big_endian>
9515void
9516Target_powerpc<size, big_endian>::merge_object_attributes(
6f3fe02b 9517 const Object* obj,
724436fc
AM
9518 const Attributes_section_data* pasd)
9519{
9520 // Return if there is no attributes section data.
9521 if (pasd == NULL)
9522 return;
9523
9524 // Create output object attributes.
9525 if (this->attributes_section_data_ == NULL)
9526 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9527
9528 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9529 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9530 Object_attribute* out_attr
9531 = this->attributes_section_data_->known_attributes(vendor);
9532
6f3fe02b 9533 const char* name = obj->name().c_str();
724436fc
AM
9534 const char* err;
9535 const char* first;
9536 const char* second;
9537 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9538 int in_fp = in_attr[tag].int_value() & 0xf;
9539 int out_fp = out_attr[tag].int_value() & 0xf;
6f3fe02b 9540 bool warn_only = obj->is_dynamic();
724436fc
AM
9541 if (in_fp != out_fp)
9542 {
9543 err = NULL;
9544 if ((in_fp & 3) == 0)
9545 ;
9546 else if ((out_fp & 3) == 0)
9547 {
6f3fe02b
AM
9548 if (!warn_only)
9549 {
9550 out_fp |= in_fp & 3;
9551 out_attr[tag].set_int_value(out_fp);
9552 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9553 this->last_fp_ = name;
9554 }
724436fc
AM
9555 }
9556 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9557 {
9558 err = N_("%s uses hard float, %s uses soft float");
9559 first = this->last_fp_;
9560 second = name;
9561 }
9562 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9563 {
9564 err = N_("%s uses hard float, %s uses soft float");
9565 first = name;
9566 second = this->last_fp_;
9567 }
9568 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9569 {
9570 err = N_("%s uses double-precision hard float, "
9571 "%s uses single-precision hard float");
9572 first = this->last_fp_;
9573 second = name;
9574 }
9575 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9576 {
9577 err = N_("%s uses double-precision hard float, "
9578 "%s uses single-precision hard float");
9579 first = name;
9580 second = this->last_fp_;
9581 }
9582
9583 if (err || (in_fp & 0xc) == 0)
9584 ;
9585 else if ((out_fp & 0xc) == 0)
9586 {
6f3fe02b
AM
9587 if (!warn_only)
9588 {
9589 out_fp |= in_fp & 0xc;
9590 out_attr[tag].set_int_value(out_fp);
9591 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9592 this->last_ld_ = name;
9593 }
724436fc
AM
9594 }
9595 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9596 {
9597 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9598 first = name;
9599 second = this->last_ld_;
9600 }
9601 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9602 {
9603 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9604 first = this->last_ld_;
9605 second = name;
9606 }
9607 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9608 {
9609 err = N_("%s uses IBM long double, %s uses IEEE long double");
9610 first = this->last_ld_;
9611 second = name;
9612 }
9613 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9614 {
9615 err = N_("%s uses IBM long double, %s uses IEEE long double");
9616 first = name;
9617 second = this->last_ld_;
9618 }
9619
9620 if (err)
9621 {
9622 if (parameters->options().warn_mismatch())
6f3fe02b
AM
9623 {
9624 if (warn_only)
9625 gold_warning(_(err), first, second);
9626 else
9627 gold_error(_(err), first, second);
9628 }
724436fc
AM
9629 // Arrange for this attribute to be deleted. It's better to
9630 // say "don't know" about a file than to wrongly claim compliance.
6f3fe02b
AM
9631 if (!warn_only)
9632 out_attr[tag].set_type(0);
724436fc
AM
9633 }
9634 }
9635
9636 if (size == 32)
9637 {
9638 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
9639 int in_vec = in_attr[tag].int_value() & 3;
9640 int out_vec = out_attr[tag].int_value() & 3;
9641 if (in_vec != out_vec)
9642 {
9643 err = NULL;
9644 if (in_vec == 0)
9645 ;
9646 else if (out_vec == 0)
9647 {
9648 out_vec = in_vec;
9649 out_attr[tag].set_int_value(out_vec);
9650 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9651 this->last_vec_ = name;
9652 }
9653 // For now, allow generic to transition to AltiVec or SPE
9654 // without a warning. If GCC marked files with their stack
9655 // alignment and used don't-care markings for files which are
9656 // not affected by the vector ABI, we could warn about this
9657 // case too. */
9658 else if (in_vec == 1)
9659 ;
9660 else if (out_vec == 1)
9661 {
9662 out_vec = in_vec;
9663 out_attr[tag].set_int_value(out_vec);
9664 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9665 this->last_vec_ = name;
9666 }
9667 else if (out_vec < in_vec)
9668 {
9669 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9670 first = this->last_vec_;
9671 second = name;
9672 }
9673 else if (out_vec > in_vec)
9674 {
9675 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9676 first = name;
9677 second = this->last_vec_;
9678 }
9679 if (err)
9680 {
9681 if (parameters->options().warn_mismatch())
9682 gold_error(_(err), first, second);
9683 out_attr[tag].set_type(0);
9684 }
9685 }
9686
9687 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
9688 int in_struct = in_attr[tag].int_value() & 3;
9689 int out_struct = out_attr[tag].int_value() & 3;
9690 if (in_struct != out_struct)
9691 {
9692 err = NULL;
9693 if (in_struct == 0 || in_struct == 3)
9694 ;
9695 else if (out_struct == 0)
9696 {
9697 out_struct = in_struct;
9698 out_attr[tag].set_int_value(out_struct);
9699 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9700 this->last_struct_ = name;
9701 }
9702 else if (out_struct < in_struct)
9703 {
9704 err = N_("%s uses r3/r4 for small structure returns, "
9705 "%s uses memory");
9706 first = this->last_struct_;
9707 second = name;
9708 }
9709 else if (out_struct > in_struct)
9710 {
9711 err = N_("%s uses r3/r4 for small structure returns, "
9712 "%s uses memory");
9713 first = name;
9714 second = this->last_struct_;
9715 }
9716 if (err)
9717 {
9718 if (parameters->options().warn_mismatch())
9719 gold_error(_(err), first, second);
9720 out_attr[tag].set_type(0);
9721 }
9722 }
9723 }
9724
9725 // Merge Tag_compatibility attributes and any common GNU ones.
9726 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
9727}
9728
5edad15d
AM
9729// Emit any saved relocs, and mark toc entries using any of these
9730// relocs as not optimizable.
aba6bc71 9731
5edad15d
AM
9732template<int sh_type, int size, bool big_endian>
9733void
9734Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
9735 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 9736{
5edad15d
AM
9737 if (size == 64
9738 && parameters->options().toc_optimize())
9739 {
9740 for (typename Copy_relocs<sh_type, size, big_endian>::
9741 Copy_reloc_entries::iterator p = this->entries_.begin();
9742 p != this->entries_.end();
9743 ++p)
9744 {
9745 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
9746 entry = *p;
9747
9748 // If the symbol is no longer defined in a dynamic object,
9749 // then we emitted a COPY relocation. If it is still
9750 // dynamic then we'll need dynamic relocations and thus
9751 // can't optimize toc entries.
9752 if (entry.sym_->is_from_dynobj())
9753 {
9754 Powerpc_relobj<size, big_endian>* ppc_object
9755 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
9756 if (entry.shndx_ == ppc_object->toc_shndx())
9757 ppc_object->set_no_toc_opt(entry.address_);
9758 }
9759 }
9760 }
9761
9762 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
9763}
9764
3ea0a085
AM
9765// Return the value to use for a branch relocation.
9766
9767template<int size, bool big_endian>
1611bc4a 9768bool
3ea0a085 9769Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 9770 const Symbol_table* symtab,
3ea0a085
AM
9771 const Sized_symbol<size>* gsym,
9772 Powerpc_relobj<size, big_endian>* object,
1611bc4a 9773 Address *value,
3ea0a085
AM
9774 unsigned int *dest_shndx)
9775{
9055360d
AM
9776 if (size == 32 || this->abiversion() >= 2)
9777 gold_unreachable();
3ea0a085 9778 *dest_shndx = 0;
3ea0a085
AM
9779
9780 // If the symbol is defined in an opd section, ie. is a function
9781 // descriptor, use the function descriptor code entry address
9782 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 9783 if (gsym != NULL
0e123f69
AM
9784 && (gsym->source() != Symbol::FROM_OBJECT
9785 || gsym->object()->is_dynamic()))
1611bc4a 9786 return true;
3ea0a085
AM
9787 if (gsym != NULL)
9788 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9789 unsigned int shndx = symobj->opd_shndx();
9790 if (shndx == 0)
1611bc4a 9791 return true;
3ea0a085 9792 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 9793 if (opd_addr == invalid_address)
1611bc4a 9794 return true;
c6905c28 9795 opd_addr += symobj->output_section_address(shndx);
1611bc4a 9796 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
9797 {
9798 Address sec_off;
1611bc4a 9799 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
9800 if (symtab->is_section_folded(symobj, *dest_shndx))
9801 {
9802 Section_id folded
9803 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
9804 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
9805 *dest_shndx = folded.second;
9806 }
3ea0a085 9807 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
9808 if (sec_addr == invalid_address)
9809 return false;
9810
3ea0a085 9811 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 9812 *value = sec_addr + sec_off;
3ea0a085 9813 }
1611bc4a 9814 return true;
3ea0a085
AM
9815}
9816
c9b8abb7
AM
9817template<int size>
9818static bool
9819relative_value_is_known(const Sized_symbol<size>* gsym)
9820{
9821 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9822 return false;
9823
9824 if (gsym->is_from_dynobj()
9825 || gsym->is_undefined()
9826 || gsym->is_preemptible())
9827 return false;
9828
9829 if (gsym->is_absolute())
9830 return !parameters->options().output_is_position_independent();
9831
9832 return true;
9833}
9834
9835template<int size>
9836static bool
9837relative_value_is_known(const Symbol_value<size>* psymval)
9838{
9839 if (psymval->is_ifunc_symbol())
9840 return false;
9841
9842 bool is_ordinary;
9843 unsigned int shndx = psymval->input_shndx(&is_ordinary);
9844
9845 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
9846}
9847
0c951c25
AM
9848// PCREL_OPT in one instance flags to the linker that a pair of insns:
9849// pld ra,symbol@got@pcrel
9850// load/store rt,0(ra)
9851// or
9852// pla ra,symbol@pcrel
9853// load/store rt,0(ra)
9854// may be translated to
9855// pload/pstore rt,symbol@pcrel
9856// nop.
9857// This function returns true if the optimization is possible, placing
9858// the prefix insn in *PINSN1 and a NOP in *PINSN2.
9859//
9860// On entry to this function, the linker has already determined that
9861// the pld can be replaced with pla: *PINSN1 is that pla insn,
9862// while *PINSN2 is the second instruction.
9863
9864inline bool
9865xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
9866{
9867 uint32_t insn2 = *pinsn2 >> 32;
9868 uint64_t i1new;
9869
9870 // Check that regs match.
9871 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
9872 return false;
9873
9874 switch ((insn2 >> 26) & 63)
9875 {
9876 default:
9877 return false;
9878
9879 case 32: // lwz
9880 case 34: // lbz
9881 case 36: // stw
9882 case 38: // stb
9883 case 40: // lhz
9884 case 42: // lha
9885 case 44: // sth
9886 case 48: // lfs
9887 case 50: // lfd
9888 case 52: // stfs
9889 case 54: // stfd
9890 // These are the PMLS cases, where we just need to tack a prefix
9891 // on the insn. Check that the D field is zero.
9892 if ((insn2 & 0xffff) != 0)
9893 return false;
9894 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
9895 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9896 break;
9897
9898 case 58: // lwa, ld
9899 if ((insn2 & 0xfffd) != 0)
9900 return false;
9901 i1new = ((1ULL << 58) | (1ULL << 52)
9902 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
9903 | (insn2 & (31ULL << 21)));
9904 break;
9905
9906 case 57: // lxsd, lxssp
9907 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
9908 return false;
9909 i1new = ((1ULL << 58) | (1ULL << 52)
9910 | ((40ULL | (insn2 & 3)) << 26)
9911 | (insn2 & (31ULL << 21)));
9912 break;
9913
9914 case 61: // stxsd, stxssp, lxv, stxv
9915 if ((insn2 & 3) == 0)
9916 return false;
9917 else if ((insn2 & 3) >= 2)
9918 {
9919 if ((insn2 & 0xfffc) != 0)
9920 return false;
9921 i1new = ((1ULL << 58) | (1ULL << 52)
9922 | ((44ULL | (insn2 & 3)) << 26)
9923 | (insn2 & (31ULL << 21)));
9924 }
9925 else
9926 {
9927 if ((insn2 & 0xfff0) != 0)
9928 return false;
9929 i1new = ((1ULL << 58) | (1ULL << 52)
9930 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
9931 | (insn2 & (31ULL << 21)));
9932 }
9933 break;
9934
9935 case 56: // lq
9936 if ((insn2 & 0xffff) != 0)
9937 return false;
9938 i1new = ((1ULL << 58) | (1ULL << 52)
9939 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9940 break;
9941
9942 case 62: // std, stq
9943 if ((insn2 & 0xfffd) != 0)
9944 return false;
9945 i1new = ((1ULL << 58) | (1ULL << 52)
9946 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
9947 | (insn2 & (31ULL << 21)));
9948 break;
9949 }
9950
9951 *pinsn1 = i1new;
9952 *pinsn2 = (uint64_t) nop << 32;
9953 return true;
9954}
9955
42cacb20
DE
9956// Perform a relocation.
9957
9958template<int size, bool big_endian>
9959inline bool
9960Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 9961 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 9962 unsigned int,
d83ce4e3
AM
9963 Target_powerpc* target,
9964 Output_section* os,
9965 size_t relnum,
91a65d2f 9966 const unsigned char* preloc,
d83ce4e3
AM
9967 const Sized_symbol<size>* gsym,
9968 const Symbol_value<size>* psymval,
9969 unsigned char* view,
c9269dff
AM
9970 Address address,
9971 section_size_type view_size)
42cacb20 9972{
23cedd1d
AM
9973 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
9974 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
9975 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9976
0e804863
ILT
9977 if (view == NULL)
9978 return true;
9979
34e0882b
AM
9980 if (target->replace_tls_get_addr(gsym))
9981 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
9982
91a65d2f
AM
9983 const elfcpp::Rela<size, big_endian> rela(preloc);
9984 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
34e0882b 9985 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 9986 {
e3deeb9c
AM
9987 case Track_tls::NOT_EXPECTED:
9988 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9989 _("__tls_get_addr call lacks marker reloc"));
9990 break;
9991 case Track_tls::EXPECTED:
9992 // We have already complained.
9993 break;
9994 case Track_tls::SKIP:
23cedd1d 9995 if (is_plt16_reloc<size>(r_type)
32f59844
AM
9996 || r_type == elfcpp::R_POWERPC_PLTSEQ
9997 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
9998 {
9999 Insn* iview = reinterpret_cast<Insn*>(view);
10000 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10001 }
10002 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10003 {
10004 Insn* iview = reinterpret_cast<Insn*>(view);
10005 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10006 }
e4dff765
AM
10007 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10008 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10009 {
10010 Insn* iview = reinterpret_cast<Insn*>(view);
10011 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10012 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10013 }
e3deeb9c
AM
10014 return true;
10015 case Track_tls::NORMAL:
10016 break;
dd93cd0a 10017 }
dd93cd0a 10018
dcfc7dd4
AM
10019 // Offset from start of insn to d-field reloc.
10020 const int d_offset = big_endian ? 2 : 0;
10021
3ea0a085
AM
10022 Powerpc_relobj<size, big_endian>* const object
10023 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 10024 Address value = 0;
0cfb0717 10025 bool has_stub_value = false;
7ee7ff70 10026 bool localentry0 = false;
e5d5f5ed 10027 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
08be3224
AM
10028 bool has_plt_offset
10029 = (gsym != NULL
88b8e639 10030 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
08be3224
AM
10031 : object->local_has_plt_offset(r_sym));
10032 if (has_plt_offset
10033 && !is_plt16_reloc<size>(r_type)
e4dff765
AM
10034 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10035 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
23cedd1d
AM
10036 && r_type != elfcpp::R_POWERPC_PLTSEQ
10037 && r_type != elfcpp::R_POWERPC_PLTCALL
32f59844
AM
10038 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10039 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
b3ccdeb5 10040 && (!psymval->is_ifunc_symbol()
9055360d 10041 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 10042 {
9055360d
AM
10043 if (size == 64
10044 && gsym != NULL
10045 && target->abiversion() >= 2
10046 && !parameters->options().output_is_position_independent()
32f59844 10047 && !is_branch_reloc<size>(r_type))
ec661b9d 10048 {
faa2211d
AM
10049 Address off = target->glink_section()->find_global_entry(gsym);
10050 if (off != invalid_address)
6ec65f28
AM
10051 {
10052 value = target->glink_section()->global_entry_address() + off;
10053 has_stub_value = true;
10054 }
ec661b9d 10055 }
c9824451 10056 else
9055360d 10057 {
64b5d6d7
AM
10058 Stub_table<size, big_endian>* stub_table = NULL;
10059 if (target->stub_tables().size() == 1)
10060 stub_table = target->stub_tables()[0];
10061 if (stub_table == NULL
10062 && !(size == 32
10063 && gsym != NULL
10064 && !parameters->options().output_is_position_independent()
32f59844 10065 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10066 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10067 if (stub_table == NULL)
10068 {
64b5d6d7
AM
10069 // This is a ref from a data section to an ifunc symbol,
10070 // or a non-branch reloc for which we always want to use
10071 // one set of stubs for resolving function addresses.
9055360d
AM
10072 if (target->stub_tables().size() != 0)
10073 stub_table = target->stub_tables()[0];
10074 }
faa2211d
AM
10075 if (stub_table != NULL)
10076 {
7e57d19e 10077 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10078 if (gsym != NULL)
7e57d19e 10079 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10080 rela.get_r_addend());
10081 else
7e57d19e 10082 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10083 rela.get_r_addend());
7e57d19e 10084 if (ent != NULL)
faa2211d 10085 {
7e57d19e
AM
10086 value = stub_table->stub_address() + ent->off_;
10087 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10088 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10089 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10090 if (size == 64
10091 && ent->r2save_
32f59844
AM
10092 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
10093 value += 4;
10094 else if (size == 64
10095 && ent->r2save_
10096 && relnum < reloc_count - 1)
7e57d19e
AM
10097 {
10098 Reltype next_rela(preloc + reloc_size);
10099 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10100 == elfcpp::R_PPC64_TOCSAVE
10101 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
10102 value += 4;
10103 }
7ee7ff70 10104 localentry0 = ent->localentry0_;
faa2211d
AM
10105 has_stub_value = true;
10106 }
10107 }
9055360d 10108 }
faa2211d
AM
10109 // We don't care too much about bogus debug references to
10110 // non-local functions, but otherwise there had better be a plt
10111 // call stub or global entry stub as appropriate.
10112 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10113 }
cf43a2fe 10114
e4dff765
AM
10115 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10116 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10117 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10118 {
10119 const Output_data_plt_powerpc<size, big_endian>* plt;
10120 if (gsym)
10121 value = target->plt_off(gsym, &plt);
10122 else
10123 value = target->plt_off(object, r_sym, &plt);
10124 value += plt->address();
10125
10126 if (size == 64)
e4dff765
AM
10127 {
10128 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10129 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10130 value -= (target->got_section()->output_section()->address()
10131 + object->toc_base_offset());
10132 }
08be3224
AM
10133 else if (parameters->options().output_is_position_independent())
10134 {
10135 if (rela.get_r_addend() >= 32768)
10136 {
10137 unsigned int got2 = object->got2_shndx();
10138 value -= (object->get_output_section_offset(got2)
10139 + object->output_section(got2)->address()
10140 + rela.get_r_addend());
10141 }
10142 else
10143 value -= (target->got_section()->address()
10144 + target->got_section()->g_o_t());
10145 }
10146 }
23cedd1d
AM
10147 else if (!has_plt_offset
10148 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10149 || r_type == elfcpp::R_POWERPC_PLTSEQ
10150 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10151 {
10152 Insn* iview = reinterpret_cast<Insn*>(view);
10153 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10154 r_type = elfcpp::R_POWERPC_NONE;
10155 }
e4dff765
AM
10156 else if (!has_plt_offset
10157 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10158 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10159 {
10160 Insn* iview = reinterpret_cast<Insn*>(view);
10161 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10162 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10163 r_type = elfcpp::R_POWERPC_NONE;
10164 }
08be3224
AM
10165 else if (r_type == elfcpp::R_POWERPC_GOT16
10166 || r_type == elfcpp::R_POWERPC_GOT16_LO
10167 || r_type == elfcpp::R_POWERPC_GOT16_HI
10168 || r_type == elfcpp::R_POWERPC_GOT16_HA
10169 || r_type == elfcpp::R_PPC64_GOT16_DS
e4dff765
AM
10170 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
10171 || r_type == elfcpp::R_PPC64_GOT_PCREL34)
42cacb20 10172 {
cf43a2fe
AM
10173 if (gsym != NULL)
10174 {
10175 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10176 value = gsym->got_offset(GOT_TYPE_STANDARD);
10177 }
10178 else
10179 {
cf43a2fe
AM
10180 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10181 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10182 }
e4dff765
AM
10183 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10184 value += target->got_section()->address();
10185 else
10186 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
10187 }
10188 else if (r_type == elfcpp::R_PPC64_TOC)
10189 {
c9269dff 10190 value = (target->got_section()->output_section()->address()
dd93cd0a 10191 + object->toc_base_offset());
cf43a2fe
AM
10192 }
10193 else if (gsym != NULL
10194 && (r_type == elfcpp::R_POWERPC_REL24
10195 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10196 && has_stub_value)
cf43a2fe 10197 {
c9269dff
AM
10198 if (size == 64)
10199 {
10200 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10201 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10202 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10203 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10204 {
3ea0a085 10205 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10206 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10207 if ((insn & 1) != 0
10208 && (insn2 == nop
10209 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10210 {
b4f7960d
AM
10211 elfcpp::Swap<32, big_endian>::
10212 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10213 can_plt_call = true;
10214 }
10215 }
10216 if (!can_plt_call)
3ea0a085
AM
10217 {
10218 // If we don't have a branch and link followed by a nop,
10219 // we can't go via the plt because there is no place to
10220 // put a toc restoring instruction.
10221 // Unless we know we won't be returning.
10222 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10223 can_plt_call = true;
10224 }
10225 if (!can_plt_call)
10226 {
ba8ca3e7
AM
10227 // g++ as of 20130507 emits self-calls without a
10228 // following nop. This is arguably wrong since we have
10229 // conflicting information. On the one hand a global
10230 // symbol and on the other a local call sequence, but
10231 // don't error for this special case.
10232 // It isn't possible to cheaply verify we have exactly
10233 // such a call. Allow all calls to the same section.
3ea0a085 10234 bool ok = false;
c9824451 10235 Address code = value;
3ea0a085
AM
10236 if (gsym->source() == Symbol::FROM_OBJECT
10237 && gsym->object() == object)
10238 {
9055360d
AM
10239 unsigned int dest_shndx = 0;
10240 if (target->abiversion() < 2)
10241 {
10242 Address addend = rela.get_r_addend();
1611bc4a
AM
10243 code = psymval->value(object, addend);
10244 target->symval_for_branch(relinfo->symtab, gsym, object,
10245 &code, &dest_shndx);
9055360d 10246 }
3ea0a085
AM
10247 bool is_ordinary;
10248 if (dest_shndx == 0)
10249 dest_shndx = gsym->shndx(&is_ordinary);
10250 ok = dest_shndx == relinfo->data_shndx;
10251 }
10252 if (!ok)
c9824451
AM
10253 {
10254 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10255 _("call lacks nop, can't restore toc; "
10256 "recompile with -fPIC"));
10257 value = code;
10258 }
3ea0a085 10259 }
c9269dff 10260 }
cf43a2fe 10261 }
dd93cd0a
AM
10262 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10263 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10264 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3
AM
10265 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10266 || r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a
AM
10267 {
10268 // First instruction of a global dynamic sequence, arg setup insn.
10269 const bool final = gsym == NULL || gsym->final_value_is_known();
10270 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10271 enum Got_type got_type = GOT_TYPE_STANDARD;
10272 if (tls_type == tls::TLSOPT_NONE)
10273 got_type = GOT_TYPE_TLSGD;
10274 else if (tls_type == tls::TLSOPT_TO_IE)
10275 got_type = GOT_TYPE_TPREL;
10276 if (got_type != GOT_TYPE_STANDARD)
10277 {
10278 if (gsym != NULL)
10279 {
10280 gold_assert(gsym->has_got_offset(got_type));
10281 value = gsym->got_offset(got_type);
10282 }
10283 else
10284 {
dd93cd0a
AM
10285 gold_assert(object->local_has_got_offset(r_sym, got_type));
10286 value = object->local_got_offset(r_sym, got_type);
10287 }
89c52ae3
AM
10288 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10289 value += target->got_section()->address();
10290 else
10291 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10292 }
10293 if (tls_type == tls::TLSOPT_TO_IE)
10294 {
89c52ae3 10295 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a 10296 {
89c52ae3
AM
10297 Insn* iview = reinterpret_cast<Insn*>(view);
10298 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10299 pinsn <<= 32;
10300 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10301 // pla -> pld
10302 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10303 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10304 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10305 pinsn & 0xffffffff);
10306 r_type = elfcpp::R_PPC64_GOT_TPREL34;
10307 }
10308 else
10309 {
10310 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10311 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10312 {
10313 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10314 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10315 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10316 if (size == 32)
10317 insn |= 32 << 26; // lwz
10318 else
10319 insn |= 58 << 26; // ld
10320 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10321 }
10322 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10323 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 10324 }
dd93cd0a
AM
10325 }
10326 else if (tls_type == tls::TLSOPT_TO_LE)
10327 {
89c52ae3 10328 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a 10329 {
89c52ae3
AM
10330 Insn* iview = reinterpret_cast<Insn*>(view);
10331 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10332 pinsn <<= 32;
10333 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10334 // pla pcrel -> paddi r13
10335 pinsn += (-1ULL << 52) + (13ULL << 16);
10336 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10337 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10338 pinsn & 0xffffffff);
10339 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
10340 value = psymval->value(object, rela.get_r_addend());
10341 }
10342 else
10343 {
89c52ae3
AM
10344 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10345 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10346 {
10347 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10348 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10349 insn &= (1 << 26) - (1 << 21); // extract rt
10350 if (size == 32)
10351 insn |= addis_0_2;
10352 else
10353 insn |= addis_0_13;
10354 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10355 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10356 value = psymval->value(object, rela.get_r_addend());
10357 }
10358 else
10359 {
10360 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10361 Insn insn = nop;
10362 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10363 r_type = elfcpp::R_POWERPC_NONE;
10364 }
dd93cd0a
AM
10365 }
10366 }
10367 }
10368 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10369 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10370 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3
AM
10371 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10372 || r_type == elfcpp::R_PPC64_GOT_TLSLD34)
dd93cd0a
AM
10373 {
10374 // First instruction of a local dynamic sequence, arg setup insn.
10375 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10376 if (tls_type == tls::TLSOPT_NONE)
10377 {
10378 value = target->tlsld_got_offset();
89c52ae3
AM
10379 if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10380 value += target->got_section()->address();
10381 else
10382 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10383 }
10384 else
10385 {
10386 gold_assert(tls_type == tls::TLSOPT_TO_LE);
89c52ae3
AM
10387 if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10388 {
10389 Insn* iview = reinterpret_cast<Insn*>(view);
10390 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10391 pinsn <<= 32;
10392 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10393 // pla pcrel -> paddi r13
10394 pinsn += (-1ULL << 52) + (13ULL << 16);
10395 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10396 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10397 pinsn & 0xffffffff);
10398 r_type = elfcpp::R_PPC64_TPREL34;
10399 value = dtp_offset;
10400 }
10401 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10402 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 10403 {
dcfc7dd4 10404 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
10405 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10406 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 10407 if (size == 32)
0f81d3f0
AM
10408 insn |= addis_0_2;
10409 else
10410 insn |= addis_0_13;
dd93cd0a
AM
10411 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10412 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 10413 value = dtp_offset;
dd93cd0a
AM
10414 }
10415 else
10416 {
dcfc7dd4 10417 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10418 Insn insn = nop;
10419 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10420 r_type = elfcpp::R_POWERPC_NONE;
10421 }
10422 }
10423 }
10424 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10425 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10426 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3
AM
10427 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10428 || r_type == elfcpp::R_PPC64_GOT_DTPREL34)
dd93cd0a
AM
10429 {
10430 // Accesses relative to a local dynamic sequence address,
10431 // no optimisation here.
10432 if (gsym != NULL)
10433 {
10434 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10435 value = gsym->got_offset(GOT_TYPE_DTPREL);
10436 }
10437 else
10438 {
dd93cd0a
AM
10439 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10440 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10441 }
89c52ae3
AM
10442 if (r_type == elfcpp::R_PPC64_GOT_DTPREL34)
10443 value += target->got_section()->address();
10444 else
10445 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10446 }
10447 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10448 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10449 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3
AM
10450 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10451 || r_type == elfcpp::R_PPC64_GOT_TPREL34)
dd93cd0a
AM
10452 {
10453 // First instruction of initial exec sequence.
10454 const bool final = gsym == NULL || gsym->final_value_is_known();
10455 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10456 if (tls_type == tls::TLSOPT_NONE)
10457 {
10458 if (gsym != NULL)
10459 {
10460 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10461 value = gsym->got_offset(GOT_TYPE_TPREL);
10462 }
10463 else
10464 {
dd93cd0a
AM
10465 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10466 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10467 }
89c52ae3
AM
10468 if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10469 value += target->got_section()->address();
10470 else
10471 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10472 }
10473 else
10474 {
10475 gold_assert(tls_type == tls::TLSOPT_TO_LE);
89c52ae3
AM
10476 if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10477 {
10478 Insn* iview = reinterpret_cast<Insn*>(view);
10479 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10480 pinsn <<= 32;
10481 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10482 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10483 pinsn += ((2ULL << 56) + (-1ULL << 52)
10484 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10485 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10486 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10487 pinsn & 0xffffffff);
10488 r_type = elfcpp::R_PPC64_TPREL34;
10489 value = psymval->value(object, rela.get_r_addend());
10490 }
10491 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10492 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 10493 {
dcfc7dd4 10494 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10495 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10496 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10497 if (size == 32)
10498 insn |= addis_0_2;
10499 else
10500 insn |= addis_0_13;
10501 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10502 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10503 value = psymval->value(object, rela.get_r_addend());
10504 }
10505 else
10506 {
dcfc7dd4 10507 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10508 Insn insn = nop;
10509 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10510 r_type = elfcpp::R_POWERPC_NONE;
10511 }
10512 }
10513 }
10514 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10515 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10516 {
10517 // Second instruction of a global dynamic sequence,
10518 // the __tls_get_addr call
e3deeb9c 10519 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10520 const bool final = gsym == NULL || gsym->final_value_is_known();
10521 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10522 if (tls_type != tls::TLSOPT_NONE)
10523 {
10524 if (tls_type == tls::TLSOPT_TO_IE)
10525 {
10526 Insn* iview = reinterpret_cast<Insn*>(view);
10527 Insn insn = add_3_3_13;
10528 if (size == 32)
10529 insn = add_3_3_2;
10530 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10531 r_type = elfcpp::R_POWERPC_NONE;
10532 }
10533 else
10534 {
89c52ae3
AM
10535 bool is_pcrel = false;
10536 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10537 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10538 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10539 if (relnum < reloc_count - 1)
10540 {
10541 Reltype next_rela(preloc + reloc_size);
10542 unsigned int r_type2
10543 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10544 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10545 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10546 && next_rela.get_r_offset() == rela.get_r_offset())
10547 is_pcrel = true;
10548 }
dd93cd0a 10549 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10550 if (is_pcrel)
10551 {
10552 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10553 r_type = elfcpp::R_POWERPC_NONE;
10554 }
10555 else
10556 {
10557 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10558 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10559 view += d_offset;
10560 value = psymval->value(object, rela.get_r_addend());
10561 }
dd93cd0a 10562 }
e3deeb9c 10563 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10564 }
10565 }
10566 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10567 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10568 {
10569 // Second instruction of a local dynamic sequence,
10570 // the __tls_get_addr call
e3deeb9c 10571 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10572 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10573 if (tls_type == tls::TLSOPT_TO_LE)
10574 {
89c52ae3
AM
10575 bool is_pcrel = false;
10576 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10577 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10578 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10579 if (relnum < reloc_count - 1)
10580 {
10581 Reltype next_rela(preloc + reloc_size);
10582 unsigned int r_type2
10583 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10584 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10585 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10586 && next_rela.get_r_offset() == rela.get_r_offset())
10587 is_pcrel = true;
10588 }
dd93cd0a 10589 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10590 if (is_pcrel)
10591 {
10592 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10593 r_type = elfcpp::R_POWERPC_NONE;
10594 }
10595 else
10596 {
10597 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10598 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10599 view += d_offset;
10600 value = dtp_offset;
10601 }
e3deeb9c 10602 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10603 }
10604 }
10605 else if (r_type == elfcpp::R_POWERPC_TLS)
10606 {
10607 // Second instruction of an initial exec sequence
10608 const bool final = gsym == NULL || gsym->final_value_is_known();
10609 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10610 if (tls_type == tls::TLSOPT_TO_LE)
10611 {
89c52ae3
AM
10612 Address roff = rela.get_r_offset() & 3;
10613 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
10614 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10615 unsigned int reg = size == 32 ? 2 : 13;
10616 insn = at_tls_transform(insn, reg);
10617 gold_assert(insn != 0);
89c52ae3
AM
10618 if (roff == 0)
10619 {
10620 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10621 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10622 view += d_offset;
10623 value = psymval->value(object, rela.get_r_addend());
10624 }
10625 else if (roff == 1)
10626 {
10627 // For pcrel IE to LE we already have the full offset
10628 // and thus don't need an addi here. A nop or mr will do.
10629 if ((insn & (0x3f << 26)) == 14 << 26)
10630 {
10631 // Extract regs from addi rt,ra,si.
10632 unsigned int rt = (insn >> 21) & 0x1f;
10633 unsigned int ra = (insn >> 16) & 0x1f;
10634 if (rt == ra)
10635 insn = nop;
10636 else
10637 {
10638 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
10639 insn = (rt << 16) | (ra << 21) | (ra << 11);
10640 insn |= (31u << 26) | (444u << 1);
10641 }
10642 }
10643 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10644 r_type = elfcpp::R_POWERPC_NONE;
10645 }
dd93cd0a
AM
10646 }
10647 }
0cfb0717 10648 else if (!has_stub_value)
cf43a2fe 10649 {
32f59844
AM
10650 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
10651 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
10652 {
10653 // PLTCALL without plt entry => convert to direct call
10654 Insn* iview = reinterpret_cast<Insn*>(view);
10655 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10656 insn = (insn & 1) | b;
10657 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10658 if (size == 32)
10659 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
10660 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10661 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
10662 else
10663 r_type = elfcpp::R_POWERPC_REL24;
10664 }
dd93cd0a 10665 Address addend = 0;
08be3224
AM
10666 if (!(size == 32
10667 && (r_type == elfcpp::R_PPC_PLTREL24
10668 || r_type == elfcpp::R_POWERPC_PLT16_LO
10669 || r_type == elfcpp::R_POWERPC_PLT16_HI
10670 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 10671 addend = rela.get_r_addend();
c9824451 10672 value = psymval->value(object, addend);
32f59844 10673 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
10674 {
10675 if (target->abiversion() >= 2)
10676 {
10677 if (gsym != NULL)
10678 value += object->ppc64_local_entry_offset(gsym);
10679 else
10680 value += object->ppc64_local_entry_offset(r_sym);
10681 }
10682 else
1611bc4a
AM
10683 {
10684 unsigned int dest_shndx;
10685 target->symval_for_branch(relinfo->symtab, gsym, object,
10686 &value, &dest_shndx);
10687 }
9055360d 10688 }
32f59844 10689 Address max_branch_offset = max_branch_delta<size>(r_type);
ec661b9d 10690 if (max_branch_offset != 0
32f59844
AM
10691 && (value - address + max_branch_offset >= 2 * max_branch_offset
10692 || (size == 64
10693 && r_type == elfcpp::R_PPC64_REL24_NOTOC
10694 && (gsym != NULL
10695 ? object->ppc64_needs_toc(gsym)
10696 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
10697 {
10698 Stub_table<size, big_endian>* stub_table
10699 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
10700 if (stub_table != NULL)
10701 {
32f59844
AM
10702 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
10703 = stub_table->find_long_branch_entry(object, value);
10704 if (ent != NULL)
0cfb0717 10705 {
32f59844
AM
10706 if (ent->save_res_)
10707 value = (value - target->savres_section()->address()
10708 + stub_table->branch_size());
10709 else
10710 value = (stub_table->stub_address() + stub_table->plt_size()
10711 + ent->off_);
0cfb0717
AM
10712 has_stub_value = true;
10713 }
0cfdc767 10714 }
ec661b9d 10715 }
42cacb20
DE
10716 }
10717
42cacb20
DE
10718 switch (r_type)
10719 {
32f59844
AM
10720 case elfcpp::R_PPC64_REL24_NOTOC:
10721 if (size == 32)
10722 break;
10723 // Fall through.
dd93cd0a
AM
10724 case elfcpp::R_PPC64_REL64:
10725 case elfcpp::R_POWERPC_REL32:
10726 case elfcpp::R_POWERPC_REL24:
10727 case elfcpp::R_PPC_PLTREL24:
10728 case elfcpp::R_PPC_LOCAL24PC:
10729 case elfcpp::R_POWERPC_REL16:
10730 case elfcpp::R_POWERPC_REL16_LO:
10731 case elfcpp::R_POWERPC_REL16_HI:
10732 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 10733 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
10734 case elfcpp::R_PPC64_REL16_HIGH:
10735 case elfcpp::R_PPC64_REL16_HIGHA:
10736 case elfcpp::R_PPC64_REL16_HIGHER:
10737 case elfcpp::R_PPC64_REL16_HIGHERA:
10738 case elfcpp::R_PPC64_REL16_HIGHEST:
10739 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
10740 case elfcpp::R_POWERPC_REL14:
10741 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10742 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
10743 case elfcpp::R_PPC64_PCREL34:
10744 case elfcpp::R_PPC64_GOT_PCREL34:
10745 case elfcpp::R_PPC64_PLT_PCREL34:
10746 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
10747 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
10748 case elfcpp::R_PPC64_GOT_TLSGD34:
10749 case elfcpp::R_PPC64_GOT_TLSLD34:
10750 case elfcpp::R_PPC64_GOT_TPREL34:
10751 case elfcpp::R_PPC64_GOT_DTPREL34:
e4dff765
AM
10752 case elfcpp::R_PPC64_REL16_HIGHER34:
10753 case elfcpp::R_PPC64_REL16_HIGHERA34:
10754 case elfcpp::R_PPC64_REL16_HIGHEST34:
10755 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
10756 value -= address;
10757 break;
10758
42cacb20
DE
10759 case elfcpp::R_PPC64_TOC16:
10760 case elfcpp::R_PPC64_TOC16_LO:
10761 case elfcpp::R_PPC64_TOC16_HI:
10762 case elfcpp::R_PPC64_TOC16_HA:
10763 case elfcpp::R_PPC64_TOC16_DS:
10764 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 10765 // Subtract the TOC base address.
c9269dff 10766 value -= (target->got_section()->output_section()->address()
dd93cd0a 10767 + object->toc_base_offset());
42cacb20
DE
10768 break;
10769
cf43a2fe
AM
10770 case elfcpp::R_POWERPC_SECTOFF:
10771 case elfcpp::R_POWERPC_SECTOFF_LO:
10772 case elfcpp::R_POWERPC_SECTOFF_HI:
10773 case elfcpp::R_POWERPC_SECTOFF_HA:
10774 case elfcpp::R_PPC64_SECTOFF_DS:
10775 case elfcpp::R_PPC64_SECTOFF_LO_DS:
10776 if (os != NULL)
10777 value -= os->address();
42cacb20
DE
10778 break;
10779
dd93cd0a
AM
10780 case elfcpp::R_PPC64_TPREL16_DS:
10781 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
10782 case elfcpp::R_PPC64_TPREL16_HIGH:
10783 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 10784 if (size != 64)
f9c6b907 10785 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 10786 break;
d8e90251 10787 // Fall through.
dd93cd0a
AM
10788 case elfcpp::R_POWERPC_TPREL16:
10789 case elfcpp::R_POWERPC_TPREL16_LO:
10790 case elfcpp::R_POWERPC_TPREL16_HI:
10791 case elfcpp::R_POWERPC_TPREL16_HA:
10792 case elfcpp::R_POWERPC_TPREL:
10793 case elfcpp::R_PPC64_TPREL16_HIGHER:
10794 case elfcpp::R_PPC64_TPREL16_HIGHERA:
10795 case elfcpp::R_PPC64_TPREL16_HIGHEST:
10796 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 10797 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
10798 // tls symbol values are relative to tls_segment()->vaddr()
10799 value -= tp_offset;
10800 break;
10801
10802 case elfcpp::R_PPC64_DTPREL16_DS:
10803 case elfcpp::R_PPC64_DTPREL16_LO_DS:
10804 case elfcpp::R_PPC64_DTPREL16_HIGHER:
10805 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10806 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10807 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10808 if (size != 64)
10809 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
10810 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
10811 break;
d8e90251 10812 // Fall through.
dd93cd0a
AM
10813 case elfcpp::R_POWERPC_DTPREL16:
10814 case elfcpp::R_POWERPC_DTPREL16_LO:
10815 case elfcpp::R_POWERPC_DTPREL16_HI:
10816 case elfcpp::R_POWERPC_DTPREL16_HA:
10817 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
10818 case elfcpp::R_PPC64_DTPREL16_HIGH:
10819 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 10820 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
10821 // tls symbol values are relative to tls_segment()->vaddr()
10822 value -= dtp_offset;
10823 break;
10824
45965137
AM
10825 case elfcpp::R_PPC64_ADDR64_LOCAL:
10826 if (gsym != NULL)
10827 value += object->ppc64_local_entry_offset(gsym);
10828 else
10829 value += object->ppc64_local_entry_offset(r_sym);
10830 break;
10831
42cacb20
DE
10832 default:
10833 break;
10834 }
10835
dd93cd0a 10836 Insn branch_bit = 0;
42cacb20
DE
10837 switch (r_type)
10838 {
dd93cd0a
AM
10839 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10840 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10841 branch_bit = 1 << 21;
d8e90251 10842 // Fall through.
dd93cd0a
AM
10843 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10844 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10845 {
10846 Insn* iview = reinterpret_cast<Insn*>(view);
10847 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10848 insn &= ~(1 << 21);
10849 insn |= branch_bit;
10850 if (this->is_isa_v2)
10851 {
10852 // Set 'a' bit. This is 0b00010 in BO field for branch
10853 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
10854 // for branch on CTR insns (BO == 1a00t or 1a01t).
10855 if ((insn & (0x14 << 21)) == (0x04 << 21))
10856 insn |= 0x02 << 21;
10857 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10858 insn |= 0x08 << 21;
10859 else
10860 break;
10861 }
10862 else
10863 {
10864 // Invert 'y' bit if not the default.
10865 if (static_cast<Signed_address>(value) < 0)
10866 insn ^= 1 << 21;
10867 }
10868 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10869 }
10870 break;
10871
08be3224
AM
10872 case elfcpp::R_POWERPC_PLT16_HA:
10873 if (size == 32
10874 && !parameters->options().output_is_position_independent())
10875 {
10876 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10877 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10878
10879 // Convert addis to lis.
10880 if ((insn & (0x3f << 26)) == 15u << 26
10881 && (insn & (0x1f << 16)) != 0)
10882 {
10883 insn &= ~(0x1f << 16);
10884 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10885 }
10886 }
10887 break;
10888
dd93cd0a
AM
10889 default:
10890 break;
10891 }
10892
c9b8abb7
AM
10893 if (size == 64
10894 && (gsym
10895 ? relative_value_is_known(gsym)
10896 : relative_value_is_known(psymval)))
aba6bc71 10897 {
0c951c25
AM
10898 Insn* iview;
10899 Insn* iview2;
10900 Insn insn;
10901 uint64_t pinsn, pinsn2;
10902
aba6bc71
AM
10903 switch (r_type)
10904 {
10905 default:
10906 break;
10907
5edad15d
AM
10908 // Multi-instruction sequences that access the GOT/TOC can
10909 // be optimized, eg.
10910 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
10911 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
10912 // and
10913 // addis ra,r2,0; addi rb,ra,x@toc@l;
10914 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
10915 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10916 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10917 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10918 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10919 case elfcpp::R_POWERPC_GOT16_HA:
10920 case elfcpp::R_PPC64_TOC16_HA:
d8f5a274 10921 if (parameters->options().toc_optimize())
aba6bc71 10922 {
0c951c25
AM
10923 iview = reinterpret_cast<Insn*>(view - d_offset);
10924 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
10925 if ((r_type == elfcpp::R_PPC64_TOC16_HA
10926 && object->make_toc_relative(target, &value))
10927 || (r_type == elfcpp::R_POWERPC_GOT16_HA
10928 && object->make_got_relative(target, psymval,
10929 rela.get_r_addend(),
10930 &value)))
5edad15d
AM
10931 {
10932 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
10933 == ((15u << 26) | (2 << 16)));
10934 }
10935 if (((insn & ((0x3f << 26) | 0x1f << 16))
10936 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
10937 && value + 0x8000 < 0x10000)
aba6bc71
AM
10938 {
10939 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10940 return true;
10941 }
10942 }
10943 break;
10944
10945 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
10946 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
10947 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
10948 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
10949 case elfcpp::R_POWERPC_GOT16_LO:
10950 case elfcpp::R_PPC64_GOT16_LO_DS:
10951 case elfcpp::R_PPC64_TOC16_LO:
10952 case elfcpp::R_PPC64_TOC16_LO_DS:
d8f5a274 10953 if (parameters->options().toc_optimize())
aba6bc71 10954 {
0c951c25
AM
10955 iview = reinterpret_cast<Insn*>(view - d_offset);
10956 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 10957 bool changed = false;
c9b8abb7
AM
10958 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
10959 && object->make_toc_relative(target, &value))
10960 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
10961 && object->make_got_relative(target, psymval,
10962 rela.get_r_addend(),
10963 &value)))
5edad15d
AM
10964 {
10965 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
10966 insn ^= (14u << 26) ^ (58u << 26);
10967 r_type = elfcpp::R_PPC64_TOC16_LO;
10968 changed = true;
10969 }
10970 if (ok_lo_toc_insn(insn, r_type)
10971 && value + 0x8000 < 0x10000)
aba6bc71
AM
10972 {
10973 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
10974 {
10975 // Transform addic to addi when we change reg.
10976 insn &= ~((0x3f << 26) | (0x1f << 16));
10977 insn |= (14u << 26) | (2 << 16);
10978 }
10979 else
10980 {
10981 insn &= ~(0x1f << 16);
10982 insn |= 2 << 16;
10983 }
5edad15d 10984 changed = true;
aba6bc71 10985 }
5edad15d
AM
10986 if (changed)
10987 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
10988 }
10989 break;
549dba71 10990
c9b8abb7
AM
10991 case elfcpp::R_PPC64_GOT_PCREL34:
10992 if (parameters->options().toc_optimize())
10993 {
0c951c25
AM
10994 iview = reinterpret_cast<Insn*>(view);
10995 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10996 pinsn <<= 32;
10997 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10998 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
10999 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11000 break;
11001
11002 Address relval = psymval->value(object, rela.get_r_addend());
11003 relval -= address;
11004 if (relval + (1ULL << 33) < 1ULL << 34)
11005 {
11006 value = relval;
11007 // Replace with paddi
0c951c25
AM
11008 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11009 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11010 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11011 pinsn & 0xffffffff);
11012 goto pcrelopt;
c9b8abb7
AM
11013 }
11014 }
11015 break;
11016
0c951c25
AM
11017 case elfcpp::R_PPC64_PCREL34:
11018 {
11019 iview = reinterpret_cast<Insn*>(view);
11020 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11021 pinsn <<= 32;
11022 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11023 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11024 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11025 | (14ULL << 26) /* paddi */))
11026 break;
11027
11028 pcrelopt:
11029 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11030 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11031 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11032 if (relnum >= reloc_count - 1)
11033 break;
11034
11035 Reltype next_rela(preloc + reloc_size);
11036 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11037 != elfcpp::R_PPC64_PCREL_OPT)
11038 || next_rela.get_r_offset() != rela.get_r_offset())
11039 break;
11040
11041 Address off = next_rela.get_r_addend();
11042 if (off == 0)
11043 off = 8; // zero means next insn.
11044 if (off + rela.get_r_offset() + 4 > view_size)
11045 break;
11046
11047 iview2 = reinterpret_cast<Insn*>(view + off);
11048 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11049 pinsn2 <<= 32;
11050 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11051 break;
11052 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11053 {
11054 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11055 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11056 pinsn & 0xffffffff);
11057 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11058 }
11059 }
11060 break;
11061
9a23f96e
AM
11062 case elfcpp::R_POWERPC_TPREL16_HA:
11063 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
11064 {
11065 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11066 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11067 if ((insn & ((0x3f << 26) | 0x1f << 16))
11068 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
11069 ;
11070 else
11071 {
11072 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11073 return true;
11074 }
11075 }
11076 break;
11077
11078 case elfcpp::R_PPC64_TPREL16_LO_DS:
11079 if (size == 32)
11080 // R_PPC_TLSGD, R_PPC_TLSLD
11081 break;
11082 // Fall through.
11083 case elfcpp::R_POWERPC_TPREL16_LO:
11084 if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
11085 {
11086 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11087 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11088 insn &= ~(0x1f << 16);
11089 insn |= (size == 32 ? 2 : 13) << 16;
11090 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11091 }
11092 break;
11093
549dba71
AM
11094 case elfcpp::R_PPC64_ENTRY:
11095 value = (target->got_section()->output_section()->address()
11096 + object->toc_base_offset());
11097 if (value + 0x80008000 <= 0xffffffff
11098 && !parameters->options().output_is_position_independent())
11099 {
11100 Insn* iview = reinterpret_cast<Insn*>(view);
11101 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11102 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11103
11104 if ((insn1 & ~0xfffc) == ld_2_12
11105 && insn2 == add_2_2_12)
11106 {
11107 insn1 = lis_2 + ha(value);
11108 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11109 insn2 = addi_2_2 + l(value);
11110 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11111 return true;
11112 }
11113 }
11114 else
11115 {
11116 value -= address;
11117 if (value + 0x80008000 <= 0xffffffff)
11118 {
11119 Insn* iview = reinterpret_cast<Insn*>(view);
11120 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11121 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11122
11123 if ((insn1 & ~0xfffc) == ld_2_12
11124 && insn2 == add_2_2_12)
11125 {
11126 insn1 = addis_2_12 + ha(value);
11127 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11128 insn2 = addi_2_2 + l(value);
11129 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11130 return true;
11131 }
11132 }
11133 }
11134 break;
e3a7574e
AM
11135
11136 case elfcpp::R_POWERPC_REL16_LO:
11137 // If we are generating a non-PIC executable, edit
11138 // 0: addis 2,12,.TOC.-0b@ha
11139 // addi 2,2,.TOC.-0b@l
11140 // used by ELFv2 global entry points to set up r2, to
11141 // lis 2,.TOC.@ha
11142 // addi 2,2,.TOC.@l
11143 // if .TOC. is in range. */
11144 if (value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11145 && relnum + 1 > 1
e3a7574e
AM
11146 && preloc != NULL
11147 && target->abiversion() >= 2
11148 && !parameters->options().output_is_position_independent()
4f038ee5 11149 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11150 && gsym != NULL
11151 && strcmp(gsym->name(), ".TOC.") == 0)
11152 {
0e123f69 11153 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11154 Reltype prev_rela(preloc - reloc_size);
11155 if ((prev_rela.get_r_info()
11156 == elfcpp::elf_r_info<size>(r_sym,
11157 elfcpp::R_POWERPC_REL16_HA))
11158 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11159 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11160 {
dcfc7dd4 11161 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11162 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11163 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11164
11165 if ((insn1 & 0xffff0000) == addis_2_12
11166 && (insn2 & 0xffff0000) == addi_2_2)
11167 {
11168 insn1 = lis_2 + ha(value + address - 4);
11169 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11170 insn2 = addi_2_2 + l(value + address - 4);
11171 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11172 if (relinfo->rr)
11173 {
11174 relinfo->rr->set_strategy(relnum - 1,
11175 Relocatable_relocs::RELOC_SPECIAL);
11176 relinfo->rr->set_strategy(relnum,
11177 Relocatable_relocs::RELOC_SPECIAL);
11178 }
11179 return true;
11180 }
11181 }
11182 }
11183 break;
aba6bc71
AM
11184 }
11185 }
11186
f4baf0d4 11187 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11188 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11189 switch (r_type)
11190 {
11191 case elfcpp::R_POWERPC_ADDR32:
11192 case elfcpp::R_POWERPC_UADDR32:
11193 if (size == 64)
f4baf0d4 11194 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11195 break;
11196
11197 case elfcpp::R_POWERPC_REL32:
a680de9a 11198 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11199 if (size == 64)
f4baf0d4 11200 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11201 break;
11202
dd93cd0a 11203 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11204 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11205 break;
11206
b80eed39
AM
11207 case elfcpp::R_POWERPC_ADDR16:
11208 // We really should have three separate relocations,
11209 // one for 16-bit data, one for insns with 16-bit signed fields,
11210 // and one for insns with 16-bit unsigned fields.
11211 overflow = Reloc::CHECK_BITFIELD;
11212 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11213 overflow = Reloc::CHECK_LOW_INSN;
11214 break;
11215
f9c6b907
AM
11216 case elfcpp::R_POWERPC_ADDR16_HI:
11217 case elfcpp::R_POWERPC_ADDR16_HA:
11218 case elfcpp::R_POWERPC_GOT16_HI:
11219 case elfcpp::R_POWERPC_GOT16_HA:
11220 case elfcpp::R_POWERPC_PLT16_HI:
11221 case elfcpp::R_POWERPC_PLT16_HA:
11222 case elfcpp::R_POWERPC_SECTOFF_HI:
11223 case elfcpp::R_POWERPC_SECTOFF_HA:
11224 case elfcpp::R_PPC64_TOC16_HI:
11225 case elfcpp::R_PPC64_TOC16_HA:
11226 case elfcpp::R_PPC64_PLTGOT16_HI:
11227 case elfcpp::R_PPC64_PLTGOT16_HA:
11228 case elfcpp::R_POWERPC_TPREL16_HI:
11229 case elfcpp::R_POWERPC_TPREL16_HA:
11230 case elfcpp::R_POWERPC_DTPREL16_HI:
11231 case elfcpp::R_POWERPC_DTPREL16_HA:
11232 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11233 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11234 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11235 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11236 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11237 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11238 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11239 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11240 case elfcpp::R_POWERPC_REL16_HI:
11241 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11242 if (size != 32)
11243 overflow = Reloc::CHECK_HIGH_INSN;
11244 break;
11245
dd93cd0a
AM
11246 case elfcpp::R_POWERPC_REL16:
11247 case elfcpp::R_PPC64_TOC16:
11248 case elfcpp::R_POWERPC_GOT16:
11249 case elfcpp::R_POWERPC_SECTOFF:
11250 case elfcpp::R_POWERPC_TPREL16:
11251 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11252 case elfcpp::R_POWERPC_GOT_TLSGD16:
11253 case elfcpp::R_POWERPC_GOT_TLSLD16:
11254 case elfcpp::R_POWERPC_GOT_TPREL16:
11255 case elfcpp::R_POWERPC_GOT_DTPREL16:
11256 overflow = Reloc::CHECK_LOW_INSN;
11257 break;
11258
32f59844
AM
11259 case elfcpp::R_PPC64_REL24_NOTOC:
11260 if (size == 32)
11261 break;
11262 // Fall through.
b80eed39
AM
11263 case elfcpp::R_POWERPC_ADDR24:
11264 case elfcpp::R_POWERPC_ADDR14:
11265 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11266 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11267 case elfcpp::R_PPC64_ADDR16_DS:
11268 case elfcpp::R_POWERPC_REL24:
11269 case elfcpp::R_PPC_PLTREL24:
11270 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
11271 case elfcpp::R_PPC64_TPREL16_DS:
11272 case elfcpp::R_PPC64_DTPREL16_DS:
11273 case elfcpp::R_PPC64_TOC16_DS:
11274 case elfcpp::R_PPC64_GOT16_DS:
11275 case elfcpp::R_PPC64_SECTOFF_DS:
11276 case elfcpp::R_POWERPC_REL14:
11277 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11278 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11279 case elfcpp::R_PPC64_D34:
11280 case elfcpp::R_PPC64_PCREL34:
11281 case elfcpp::R_PPC64_GOT_PCREL34:
11282 case elfcpp::R_PPC64_PLT_PCREL34:
11283 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11284 case elfcpp::R_PPC64_D28:
11285 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
11286 case elfcpp::R_PPC64_TPREL34:
11287 case elfcpp::R_PPC64_DTPREL34:
11288 case elfcpp::R_PPC64_GOT_TLSGD34:
11289 case elfcpp::R_PPC64_GOT_TLSLD34:
11290 case elfcpp::R_PPC64_GOT_TPREL34:
11291 case elfcpp::R_PPC64_GOT_DTPREL34:
f4baf0d4 11292 overflow = Reloc::CHECK_SIGNED;
42cacb20 11293 break;
dd93cd0a 11294 }
42cacb20 11295
dcfc7dd4 11296 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
11297 Insn insn = 0;
11298
b80eed39
AM
11299 if (overflow == Reloc::CHECK_LOW_INSN
11300 || overflow == Reloc::CHECK_HIGH_INSN)
11301 {
a680de9a 11302 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 11303
a47622ac
AM
11304 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11305 overflow = Reloc::CHECK_BITFIELD;
11306 else if (overflow == Reloc::CHECK_LOW_INSN
11307 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11308 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11309 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11310 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11311 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11312 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 11313 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
11314 else
11315 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
11316 }
11317
a680de9a 11318 bool maybe_dq_reloc = false;
3ea0a085 11319 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 11320 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
11321 switch (r_type)
11322 {
11323 case elfcpp::R_POWERPC_NONE:
11324 case elfcpp::R_POWERPC_TLS:
11325 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11326 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
11327 case elfcpp::R_POWERPC_PLTSEQ:
11328 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
11329 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11330 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 11331 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
11332 break;
11333
11334 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 11335 case elfcpp::R_PPC64_REL64:
cf43a2fe 11336 case elfcpp::R_PPC64_TOC:
45965137 11337 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
11338 Reloc::addr64(view, value);
11339 break;
11340
11341 case elfcpp::R_POWERPC_TPREL:
11342 case elfcpp::R_POWERPC_DTPREL:
11343 if (size == 64)
11344 Reloc::addr64(view, value);
11345 else
3ea0a085 11346 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11347 break;
11348
11349 case elfcpp::R_PPC64_UADDR64:
11350 Reloc::addr64_u(view, value);
42cacb20
DE
11351 break;
11352
11353 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 11354 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11355 break;
11356
acc276d8 11357 case elfcpp::R_POWERPC_REL32:
dd93cd0a 11358 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 11359 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
11360 break;
11361
32f59844
AM
11362 case elfcpp::R_PPC64_REL24_NOTOC:
11363 if (size == 32)
11364 goto unsupp; // R_PPC_EMB_RELSDA
11365 // Fall through.
dd93cd0a
AM
11366 case elfcpp::R_POWERPC_ADDR24:
11367 case elfcpp::R_POWERPC_REL24:
11368 case elfcpp::R_PPC_PLTREL24:
11369 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 11370 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
11371 break;
11372
dd93cd0a
AM
11373 case elfcpp::R_POWERPC_GOT_DTPREL16:
11374 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
11375 case elfcpp::R_POWERPC_GOT_TPREL16:
11376 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
11377 if (size == 64)
11378 {
ec86f434 11379 // On ppc64 these are all ds form
a680de9a 11380 maybe_dq_reloc = true;
dd93cd0a
AM
11381 break;
11382 }
c25aa1e1 11383 // Fall through.
cf43a2fe 11384 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 11385 case elfcpp::R_POWERPC_REL16:
cf43a2fe 11386 case elfcpp::R_PPC64_TOC16:
42cacb20 11387 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 11388 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
11389 case elfcpp::R_POWERPC_TPREL16:
11390 case elfcpp::R_POWERPC_DTPREL16:
11391 case elfcpp::R_POWERPC_GOT_TLSGD16:
11392 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 11393 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 11394 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 11395 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 11396 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 11397 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 11398 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
11399 case elfcpp::R_POWERPC_TPREL16_LO:
11400 case elfcpp::R_POWERPC_DTPREL16_LO:
11401 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11402 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
11403 if (size == 64)
11404 status = Reloc::addr16(view, value, overflow);
11405 else
11406 maybe_dq_reloc = true;
dd93cd0a
AM
11407 break;
11408
11409 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 11410 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
11411 break;
11412
f9c6b907
AM
11413 case elfcpp::R_PPC64_ADDR16_HIGH:
11414 case elfcpp::R_PPC64_TPREL16_HIGH:
11415 case elfcpp::R_PPC64_DTPREL16_HIGH:
11416 if (size == 32)
11417 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11418 goto unsupp;
d8e90251 11419 // Fall through.
cf43a2fe 11420 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 11421 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 11422 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 11423 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 11424 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 11425 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 11426 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
11427 case elfcpp::R_POWERPC_TPREL16_HI:
11428 case elfcpp::R_POWERPC_DTPREL16_HI:
11429 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11430 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11431 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11432 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11433 Reloc::addr16_hi(view, value);
42cacb20
DE
11434 break;
11435
f9c6b907
AM
11436 case elfcpp::R_PPC64_ADDR16_HIGHA:
11437 case elfcpp::R_PPC64_TPREL16_HIGHA:
11438 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11439 if (size == 32)
11440 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11441 goto unsupp;
d8e90251 11442 // Fall through.
cf43a2fe 11443 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 11444 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 11445 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 11446 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 11447 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 11448 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 11449 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
11450 case elfcpp::R_POWERPC_TPREL16_HA:
11451 case elfcpp::R_POWERPC_DTPREL16_HA:
11452 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11453 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11454 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11455 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11456 Reloc::addr16_ha(view, value);
42cacb20
DE
11457 break;
11458
a680de9a
PB
11459 case elfcpp::R_POWERPC_REL16DX_HA:
11460 status = Reloc::addr16dx_ha(view, value, overflow);
11461 break;
11462
dd93cd0a
AM
11463 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11464 if (size == 32)
11465 // R_PPC_EMB_NADDR16_LO
11466 goto unsupp;
d8e90251 11467 // Fall through.
dd93cd0a 11468 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 11469 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
11470 case elfcpp::R_PPC64_TPREL16_HIGHER:
11471 Reloc::addr16_hi2(view, value);
42cacb20
DE
11472 break;
11473
dd93cd0a
AM
11474 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11475 if (size == 32)
11476 // R_PPC_EMB_NADDR16_HI
11477 goto unsupp;
d8e90251 11478 // Fall through.
dd93cd0a 11479 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 11480 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
11481 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11482 Reloc::addr16_ha2(view, value);
42cacb20
DE
11483 break;
11484
dd93cd0a
AM
11485 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11486 if (size == 32)
11487 // R_PPC_EMB_NADDR16_HA
11488 goto unsupp;
d8e90251 11489 // Fall through.
dd93cd0a 11490 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 11491 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
11492 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11493 Reloc::addr16_hi3(view, value);
42cacb20
DE
11494 break;
11495
dd93cd0a
AM
11496 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11497 if (size == 32)
11498 // R_PPC_EMB_SDAI16
11499 goto unsupp;
d8e90251 11500 // Fall through.
dd93cd0a 11501 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 11502 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11503 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11504 Reloc::addr16_ha3(view, value);
11505 break;
11506
11507 case elfcpp::R_PPC64_DTPREL16_DS:
11508 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11509 if (size == 32)
11510 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11511 goto unsupp;
d8e90251 11512 // Fall through.
dd93cd0a
AM
11513 case elfcpp::R_PPC64_TPREL16_DS:
11514 case elfcpp::R_PPC64_TPREL16_LO_DS:
11515 if (size == 32)
11516 // R_PPC_TLSGD, R_PPC_TLSLD
11517 break;
d8e90251 11518 // Fall through.
cf43a2fe
AM
11519 case elfcpp::R_PPC64_ADDR16_DS:
11520 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
11521 case elfcpp::R_PPC64_TOC16_DS:
11522 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
11523 case elfcpp::R_PPC64_GOT16_DS:
11524 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 11525 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
11526 case elfcpp::R_PPC64_SECTOFF_DS:
11527 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 11528 maybe_dq_reloc = true;
dd93cd0a
AM
11529 break;
11530
11531 case elfcpp::R_POWERPC_ADDR14:
11532 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11533 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11534 case elfcpp::R_POWERPC_REL14:
11535 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11536 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 11537 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
11538 break;
11539
11540 case elfcpp::R_POWERPC_COPY:
11541 case elfcpp::R_POWERPC_GLOB_DAT:
11542 case elfcpp::R_POWERPC_JMP_SLOT:
11543 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 11544 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
11545 case elfcpp::R_PPC64_JMP_IREL:
11546 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
11547 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11548 _("unexpected reloc %u in object file"),
11549 r_type);
11550 break;
11551
7e57d19e 11552 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 11553 if (size == 32)
7e57d19e 11554 // R_PPC_EMB_SDA21
dd93cd0a
AM
11555 goto unsupp;
11556 else
11557 {
7e57d19e
AM
11558 Symbol_location loc;
11559 loc.object = relinfo->object;
11560 loc.shndx = relinfo->data_shndx;
11561 loc.offset = rela.get_r_offset();
11562 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
11563 if (p != target->tocsave_loc().end())
11564 {
11565 // If we've generated plt calls using this tocsave, then
11566 // the nop needs to be changed to save r2.
11567 Insn* iview = reinterpret_cast<Insn*>(view);
11568 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11569 elfcpp::Swap<32, big_endian>::
11570 writeval(iview, std_2_1 + target->stk_toc());
11571 }
dd93cd0a
AM
11572 }
11573 break;
11574
11575 case elfcpp::R_PPC_EMB_SDA2I16:
11576 case elfcpp::R_PPC_EMB_SDA2REL:
11577 if (size == 32)
11578 goto unsupp;
11579 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
11580 break;
11581
e4dff765
AM
11582 case elfcpp::R_PPC64_D34:
11583 case elfcpp::R_PPC64_D34_LO:
11584 case elfcpp::R_PPC64_PCREL34:
11585 case elfcpp::R_PPC64_GOT_PCREL34:
11586 case elfcpp::R_PPC64_PLT_PCREL34:
11587 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
11588 case elfcpp::R_PPC64_TPREL34:
11589 case elfcpp::R_PPC64_DTPREL34:
11590 case elfcpp::R_PPC64_GOT_TLSGD34:
11591 case elfcpp::R_PPC64_GOT_TLSLD34:
11592 case elfcpp::R_PPC64_GOT_TPREL34:
11593 case elfcpp::R_PPC64_GOT_DTPREL34:
e4dff765
AM
11594 if (size == 32)
11595 goto unsupp;
11596 status = Reloc::addr34(view, value, overflow);
11597 break;
11598
11599 case elfcpp::R_PPC64_D34_HI30:
11600 if (size == 32)
11601 goto unsupp;
11602 Reloc::addr34_hi(view, value);
11603 break;
11604
11605 case elfcpp::R_PPC64_D34_HA30:
11606 if (size == 32)
11607 goto unsupp;
11608 Reloc::addr34_ha(view, value);
11609 break;
11610
11611 case elfcpp::R_PPC64_D28:
11612 case elfcpp::R_PPC64_PCREL28:
11613 if (size == 32)
11614 goto unsupp;
11615 status = Reloc::addr28(view, value, overflow);
11616 break;
11617
11618 case elfcpp::R_PPC64_ADDR16_HIGHER34:
11619 case elfcpp::R_PPC64_REL16_HIGHER34:
11620 if (size == 32)
11621 goto unsupp;
11622 Reloc::addr16_higher34(view, value);
11623 break;
11624
11625 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
11626 case elfcpp::R_PPC64_REL16_HIGHERA34:
11627 if (size == 32)
11628 goto unsupp;
11629 Reloc::addr16_highera34(view, value);
11630 break;
11631
11632 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
11633 case elfcpp::R_PPC64_REL16_HIGHEST34:
11634 if (size == 32)
11635 goto unsupp;
11636 Reloc::addr16_highest34(view, value);
11637 break;
11638
11639 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
11640 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11641 if (size == 32)
11642 goto unsupp;
11643 Reloc::addr16_highesta34(view, value);
11644 break;
11645
dd93cd0a
AM
11646 case elfcpp::R_POWERPC_PLT32:
11647 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
11648 case elfcpp::R_PPC_SDAREL16:
11649 case elfcpp::R_POWERPC_ADDR30:
11650 case elfcpp::R_PPC64_PLT64:
11651 case elfcpp::R_PPC64_PLTREL64:
11652 case elfcpp::R_PPC64_PLTGOT16:
11653 case elfcpp::R_PPC64_PLTGOT16_LO:
11654 case elfcpp::R_PPC64_PLTGOT16_HI:
11655 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
11656 case elfcpp::R_PPC64_PLTGOT16_DS:
11657 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 11658 case elfcpp::R_PPC_TOC16:
42cacb20 11659 default:
dd93cd0a 11660 unsupp:
42cacb20
DE
11661 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11662 _("unsupported reloc %u"),
11663 r_type);
11664 break;
11665 }
a680de9a
PB
11666
11667 if (maybe_dq_reloc)
11668 {
11669 if (insn == 0)
11670 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11671
11672 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
11673 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
11674 && (insn & 3) == 1))
11675 status = Reloc::addr16_dq(view, value, overflow);
11676 else if (size == 64
11677 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
11678 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
11679 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
11680 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
11681 status = Reloc::addr16_ds(view, value, overflow);
11682 else
11683 status = Reloc::addr16(view, value, overflow);
11684 }
11685
0cfb0717 11686 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
11687 && (has_stub_value
11688 || !(gsym != NULL
282c9750 11689 && gsym->is_undefined()
32f59844 11690 && is_branch_reloc<size>(r_type))))
0cfb0717
AM
11691 {
11692 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11693 _("relocation overflow"));
11694 if (has_stub_value)
11695 gold_info(_("try relinking with a smaller --stub-group-size"));
11696 }
42cacb20
DE
11697
11698 return true;
11699}
11700
42cacb20
DE
11701// Relocate section data.
11702
11703template<int size, bool big_endian>
11704void
11705Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
11706 const Relocate_info<size, big_endian>* relinfo,
11707 unsigned int sh_type,
11708 const unsigned char* prelocs,
11709 size_t reloc_count,
11710 Output_section* output_section,
11711 bool needs_special_offset_handling,
11712 unsigned char* view,
c9269dff 11713 Address address,
d83ce4e3
AM
11714 section_size_type view_size,
11715 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
11716{
11717 typedef Target_powerpc<size, big_endian> Powerpc;
11718 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
11719 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
11720 Powerpc_comdat_behavior;
4d625b70
CC
11721 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11722 Classify_reloc;
42cacb20
DE
11723
11724 gold_assert(sh_type == elfcpp::SHT_RELA);
11725
4d625b70
CC
11726 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
11727 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
11728 relinfo,
11729 this,
11730 prelocs,
11731 reloc_count,
11732 output_section,
11733 needs_special_offset_handling,
11734 view,
11735 address,
364c7fa5
ILT
11736 view_size,
11737 reloc_symbol_changes);
42cacb20
DE
11738}
11739
4d625b70 11740template<int size, bool big_endian>
cf43a2fe 11741class Powerpc_scan_relocatable_reloc
42cacb20 11742{
cf43a2fe 11743public:
0e123f69
AM
11744 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11745 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
11746 static const int sh_type = elfcpp::SHT_RELA;
11747
11748 // Return the symbol referred to by the relocation.
11749 static inline unsigned int
11750 get_r_sym(const Reltype* reloc)
11751 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
11752
11753 // Return the type of the relocation.
11754 static inline unsigned int
11755 get_r_type(const Reltype* reloc)
11756 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
11757
cf43a2fe
AM
11758 // Return the strategy to use for a local symbol which is not a
11759 // section symbol, given the relocation type.
11760 inline Relocatable_relocs::Reloc_strategy
11761 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
11762 {
11763 if (r_type == 0 && r_sym == 0)
11764 return Relocatable_relocs::RELOC_DISCARD;
11765 return Relocatable_relocs::RELOC_COPY;
11766 }
11767
11768 // Return the strategy to use for a local symbol which is a section
11769 // symbol, given the relocation type.
11770 inline Relocatable_relocs::Reloc_strategy
11771 local_section_strategy(unsigned int, Relobj*)
11772 {
11773 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
11774 }
11775
11776 // Return the strategy to use for a global symbol, given the
11777 // relocation type, the object, and the symbol index.
11778 inline Relocatable_relocs::Reloc_strategy
11779 global_strategy(unsigned int r_type, Relobj*, unsigned int)
11780 {
08be3224
AM
11781 if (size == 32
11782 && (r_type == elfcpp::R_PPC_PLTREL24
11783 || r_type == elfcpp::R_POWERPC_PLT16_LO
11784 || r_type == elfcpp::R_POWERPC_PLT16_HI
11785 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
11786 return Relocatable_relocs::RELOC_SPECIAL;
11787 return Relocatable_relocs::RELOC_COPY;
11788 }
11789};
42cacb20
DE
11790
11791// Scan the relocs during a relocatable link.
11792
11793template<int size, bool big_endian>
11794void
11795Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
11796 Symbol_table* symtab,
11797 Layout* layout,
11798 Sized_relobj_file<size, big_endian>* object,
11799 unsigned int data_shndx,
11800 unsigned int sh_type,
11801 const unsigned char* prelocs,
11802 size_t reloc_count,
11803 Output_section* output_section,
11804 bool needs_special_offset_handling,
11805 size_t local_symbol_count,
11806 const unsigned char* plocal_symbols,
11807 Relocatable_relocs* rr)
42cacb20 11808{
4d625b70
CC
11809 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
11810
42cacb20
DE
11811 gold_assert(sh_type == elfcpp::SHT_RELA);
11812
4d625b70 11813 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
11814 symtab,
11815 layout,
11816 object,
11817 data_shndx,
11818 prelocs,
11819 reloc_count,
11820 output_section,
11821 needs_special_offset_handling,
11822 local_symbol_count,
11823 plocal_symbols,
11824 rr);
11825}
11826
4d625b70
CC
11827// Scan the relocs for --emit-relocs.
11828
11829template<int size, bool big_endian>
11830void
11831Target_powerpc<size, big_endian>::emit_relocs_scan(
11832 Symbol_table* symtab,
11833 Layout* layout,
11834 Sized_relobj_file<size, big_endian>* object,
11835 unsigned int data_shndx,
11836 unsigned int sh_type,
11837 const unsigned char* prelocs,
11838 size_t reloc_count,
11839 Output_section* output_section,
11840 bool needs_special_offset_handling,
11841 size_t local_symbol_count,
11842 const unsigned char* plocal_syms,
11843 Relocatable_relocs* rr)
11844{
11845 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11846 Classify_reloc;
11847 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
11848 Emit_relocs_strategy;
11849
11850 gold_assert(sh_type == elfcpp::SHT_RELA);
11851
11852 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
11853 symtab,
11854 layout,
11855 object,
11856 data_shndx,
11857 prelocs,
11858 reloc_count,
11859 output_section,
11860 needs_special_offset_handling,
11861 local_symbol_count,
11862 plocal_syms,
11863 rr);
11864}
11865
7404fe1b 11866// Emit relocations for a section.
dd93cd0a
AM
11867// This is a modified version of the function by the same name in
11868// target-reloc.h. Using relocate_special_relocatable for
11869// R_PPC_PLTREL24 would require duplication of the entire body of the
11870// loop, so we may as well duplicate the whole thing.
42cacb20
DE
11871
11872template<int size, bool big_endian>
11873void
7404fe1b 11874Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
11875 const Relocate_info<size, big_endian>* relinfo,
11876 unsigned int sh_type,
11877 const unsigned char* prelocs,
11878 size_t reloc_count,
11879 Output_section* output_section,
62fe925a 11880 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 11881 unsigned char*,
dd93cd0a 11882 Address view_address,
cf43a2fe 11883 section_size_type,
42cacb20
DE
11884 unsigned char* reloc_view,
11885 section_size_type reloc_view_size)
11886{
11887 gold_assert(sh_type == elfcpp::SHT_RELA);
11888
0e123f69
AM
11889 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11890 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
11891 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
11892 // Offset from start of insn to d-field reloc.
11893 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
11894
11895 Powerpc_relobj<size, big_endian>* const object
11896 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
11897 const unsigned int local_count = object->local_symbol_count();
11898 unsigned int got2_shndx = object->got2_shndx();
c9269dff 11899 Address got2_addend = 0;
cf43a2fe 11900 if (got2_shndx != 0)
c9269dff
AM
11901 {
11902 got2_addend = object->get_output_section_offset(got2_shndx);
11903 gold_assert(got2_addend != invalid_address);
11904 }
cf43a2fe 11905
033bfb73
CC
11906 const bool relocatable = parameters->options().relocatable();
11907
cf43a2fe 11908 unsigned char* pwrite = reloc_view;
7404fe1b 11909 bool zap_next = false;
cf43a2fe
AM
11910 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11911 {
91a65d2f 11912 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
11913 if (strategy == Relocatable_relocs::RELOC_DISCARD)
11914 continue;
11915
11916 Reltype reloc(prelocs);
11917 Reltype_write reloc_write(pwrite);
11918
7404fe1b 11919 Address offset = reloc.get_r_offset();
cf43a2fe 11920 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
11921 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
11922 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
11923 const unsigned int orig_r_sym = r_sym;
11924 typename elfcpp::Elf_types<size>::Elf_Swxword addend
11925 = reloc.get_r_addend();
11926 const Symbol* gsym = NULL;
11927
11928 if (zap_next)
11929 {
11930 // We could arrange to discard these and other relocs for
11931 // tls optimised sequences in the strategy methods, but for
11932 // now do as BFD ld does.
11933 r_type = elfcpp::R_POWERPC_NONE;
11934 zap_next = false;
11935 }
cf43a2fe
AM
11936
11937 // Get the new symbol index.
9215b98b 11938 Output_section* os = NULL;
cf43a2fe
AM
11939 if (r_sym < local_count)
11940 {
11941 switch (strategy)
11942 {
11943 case Relocatable_relocs::RELOC_COPY:
11944 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 11945 if (r_sym != 0)
dd93cd0a 11946 {
7404fe1b
AM
11947 r_sym = object->symtab_index(r_sym);
11948 gold_assert(r_sym != -1U);
dd93cd0a 11949 }
cf43a2fe
AM
11950 break;
11951
11952 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
11953 {
11954 // We are adjusting a section symbol. We need to find
11955 // the symbol table index of the section symbol for
11956 // the output section corresponding to input section
11957 // in which this symbol is defined.
11958 gold_assert(r_sym < local_count);
11959 bool is_ordinary;
11960 unsigned int shndx =
11961 object->local_symbol_input_shndx(r_sym, &is_ordinary);
11962 gold_assert(is_ordinary);
9215b98b 11963 os = object->output_section(shndx);
cf43a2fe
AM
11964 gold_assert(os != NULL);
11965 gold_assert(os->needs_symtab_index());
7404fe1b 11966 r_sym = os->symtab_index();
cf43a2fe
AM
11967 }
11968 break;
11969
11970 default:
11971 gold_unreachable();
11972 }
11973 }
11974 else
11975 {
7404fe1b 11976 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
11977 gold_assert(gsym != NULL);
11978 if (gsym->is_forwarder())
11979 gsym = relinfo->symtab->resolve_forwards(gsym);
11980
11981 gold_assert(gsym->has_symtab_index());
7404fe1b 11982 r_sym = gsym->symtab_index();
cf43a2fe
AM
11983 }
11984
11985 // Get the new offset--the location in the output section where
11986 // this relocation should be applied.
cf43a2fe 11987 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 11988 offset += offset_in_output_section;
cf43a2fe
AM
11989 else
11990 {
c9269dff
AM
11991 section_offset_type sot_offset =
11992 convert_types<section_offset_type, Address>(offset);
cf43a2fe 11993 section_offset_type new_sot_offset =
c9269dff
AM
11994 output_section->output_offset(object, relinfo->data_shndx,
11995 sot_offset);
cf43a2fe 11996 gold_assert(new_sot_offset != -1);
7404fe1b 11997 offset = new_sot_offset;
cf43a2fe
AM
11998 }
11999
dd93cd0a
AM
12000 // In an object file, r_offset is an offset within the section.
12001 // In an executable or dynamic object, generated by
12002 // --emit-relocs, r_offset is an absolute address.
033bfb73 12003 if (!relocatable)
dd93cd0a 12004 {
7404fe1b 12005 offset += view_address;
dd93cd0a 12006 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12007 offset -= offset_in_output_section;
dd93cd0a
AM
12008 }
12009
cf43a2fe 12010 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12011 if (strategy == Relocatable_relocs::RELOC_COPY)
12012 ;
12013 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12014 {
7404fe1b 12015 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12016 addend = psymval->value(object, addend);
12017 // In a relocatable link, the symbol value is relative to
12018 // the start of the output section. For a non-relocatable
12019 // link, we need to adjust the addend.
12020 if (!relocatable)
12021 {
12022 gold_assert(os != NULL);
12023 addend -= os->address();
12024 }
cf43a2fe
AM
12025 }
12026 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12027 {
e3a7574e
AM
12028 if (size == 32)
12029 {
12030 if (addend >= 32768)
12031 addend += got2_addend;
12032 }
12033 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12034 {
12035 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12036 addend -= d_offset;
e3a7574e
AM
12037 }
12038 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12039 {
12040 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12041 addend -= d_offset + 4;
e3a7574e 12042 }
cf43a2fe
AM
12043 }
12044 else
12045 gold_unreachable();
12046
033bfb73 12047 if (!relocatable)
7404fe1b
AM
12048 {
12049 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12050 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12051 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12052 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12053 {
12054 // First instruction of a global dynamic sequence,
12055 // arg setup insn.
12056 const bool final = gsym == NULL || gsym->final_value_is_known();
12057 switch (this->optimize_tls_gd(final))
12058 {
12059 case tls::TLSOPT_TO_IE:
12060 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12061 - elfcpp::R_POWERPC_GOT_TLSGD16);
12062 break;
12063 case tls::TLSOPT_TO_LE:
12064 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12065 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12066 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12067 else
12068 {
12069 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12070 offset -= d_offset;
7404fe1b
AM
12071 }
12072 break;
12073 default:
12074 break;
12075 }
12076 }
12077 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12078 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12079 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12080 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12081 {
12082 // First instruction of a local dynamic sequence,
12083 // arg setup insn.
12084 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12085 {
12086 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12087 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12088 {
12089 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12090 const Output_section* os = relinfo->layout->tls_segment()
12091 ->first_section();
12092 gold_assert(os != NULL);
12093 gold_assert(os->needs_symtab_index());
12094 r_sym = os->symtab_index();
12095 addend = dtp_offset;
12096 }
12097 else
12098 {
12099 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12100 offset -= d_offset;
7404fe1b
AM
12101 }
12102 }
12103 }
12104 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12105 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12106 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12107 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12108 {
12109 // First instruction of initial exec sequence.
12110 const bool final = gsym == NULL || gsym->final_value_is_known();
12111 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12112 {
12113 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12114 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12115 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12116 else
12117 {
12118 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12119 offset -= d_offset;
7404fe1b
AM
12120 }
12121 }
12122 }
12123 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12124 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12125 {
12126 // Second instruction of a global dynamic sequence,
12127 // the __tls_get_addr call
12128 const bool final = gsym == NULL || gsym->final_value_is_known();
12129 switch (this->optimize_tls_gd(final))
12130 {
12131 case tls::TLSOPT_TO_IE:
12132 r_type = elfcpp::R_POWERPC_NONE;
12133 zap_next = true;
12134 break;
12135 case tls::TLSOPT_TO_LE:
12136 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12137 offset += d_offset;
7404fe1b
AM
12138 zap_next = true;
12139 break;
12140 default:
12141 break;
12142 }
12143 }
12144 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12145 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12146 {
12147 // Second instruction of a local dynamic sequence,
12148 // the __tls_get_addr call
12149 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12150 {
12151 const Output_section* os = relinfo->layout->tls_segment()
12152 ->first_section();
12153 gold_assert(os != NULL);
12154 gold_assert(os->needs_symtab_index());
12155 r_sym = os->symtab_index();
12156 addend = dtp_offset;
12157 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12158 offset += d_offset;
7404fe1b
AM
12159 zap_next = true;
12160 }
12161 }
12162 else if (r_type == elfcpp::R_POWERPC_TLS)
12163 {
12164 // Second instruction of an initial exec sequence
12165 const bool final = gsym == NULL || gsym->final_value_is_known();
12166 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12167 {
12168 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12169 offset += d_offset;
7404fe1b
AM
12170 }
12171 }
12172 }
12173
12174 reloc_write.put_r_offset(offset);
12175 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12176 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12177
12178 pwrite += reloc_size;
12179 }
12180
12181 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12182 == reloc_view_size);
42cacb20
DE
12183}
12184
ec661b9d 12185// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12186// treatment. This is how we support equality comparisons of function
12187// pointers across shared library boundaries, as described in the
12188// processor specific ABI supplement.
12189
12190template<int size, bool big_endian>
12191uint64_t
12192Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12193{
cf43a2fe
AM
12194 if (size == 32)
12195 {
12196 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12197 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12198 p != this->stub_tables_.end();
12199 ++p)
12200 {
7e57d19e
AM
12201 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12202 = (*p)->find_plt_call_entry(gsym);
12203 if (ent != NULL)
12204 return (*p)->stub_address() + ent->off_;
ec661b9d 12205 }
c9824451 12206 }
9055360d
AM
12207 else if (this->abiversion() >= 2)
12208 {
faa2211d
AM
12209 Address off = this->glink_section()->find_global_entry(gsym);
12210 if (off != invalid_address)
9055360d
AM
12211 return this->glink_section()->global_entry_address() + off;
12212 }
ec661b9d 12213 gold_unreachable();
c9824451
AM
12214}
12215
12216// Return the PLT address to use for a local symbol.
12217template<int size, bool big_endian>
12218uint64_t
12219Target_powerpc<size, big_endian>::do_plt_address_for_local(
12220 const Relobj* object,
12221 unsigned int symndx) const
12222{
12223 if (size == 32)
12224 {
12225 const Sized_relobj<size, big_endian>* relobj
12226 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12227 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12228 p != this->stub_tables_.end();
12229 ++p)
12230 {
7e57d19e
AM
12231 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12232 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12233 if (ent != NULL)
12234 return (*p)->stub_address() + ent->off_;
ec661b9d 12235 }
c9824451 12236 }
ec661b9d 12237 gold_unreachable();
c9824451
AM
12238}
12239
12240// Return the PLT address to use for a global symbol.
12241template<int size, bool big_endian>
12242uint64_t
12243Target_powerpc<size, big_endian>::do_plt_address_for_global(
12244 const Symbol* gsym) const
12245{
12246 if (size == 32)
12247 {
ec661b9d
AM
12248 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12249 p != this->stub_tables_.end();
12250 ++p)
12251 {
7e57d19e
AM
12252 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12253 = (*p)->find_plt_call_entry(gsym);
12254 if (ent != NULL)
12255 return (*p)->stub_address() + ent->off_;
ec661b9d 12256 }
cf43a2fe 12257 }
9055360d
AM
12258 else if (this->abiversion() >= 2)
12259 {
faa2211d
AM
12260 Address off = this->glink_section()->find_global_entry(gsym);
12261 if (off != invalid_address)
9055360d
AM
12262 return this->glink_section()->global_entry_address() + off;
12263 }
ec661b9d 12264 gold_unreachable();
42cacb20
DE
12265}
12266
bd73a62d
AM
12267// Return the offset to use for the GOT_INDX'th got entry which is
12268// for a local tls symbol specified by OBJECT, SYMNDX.
12269template<int size, bool big_endian>
12270int64_t
12271Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12272 const Relobj* object,
12273 unsigned int symndx,
12274 unsigned int got_indx) const
12275{
12276 const Powerpc_relobj<size, big_endian>* ppc_object
12277 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12278 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12279 {
12280 for (Got_type got_type = GOT_TYPE_TLSGD;
12281 got_type <= GOT_TYPE_TPREL;
12282 got_type = Got_type(got_type + 1))
12283 if (ppc_object->local_has_got_offset(symndx, got_type))
12284 {
12285 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12286 if (got_type == GOT_TYPE_TLSGD)
12287 off += size / 8;
12288 if (off == got_indx * (size / 8))
12289 {
12290 if (got_type == GOT_TYPE_TPREL)
12291 return -tp_offset;
12292 else
12293 return -dtp_offset;
12294 }
12295 }
12296 }
12297 gold_unreachable();
12298}
12299
12300// Return the offset to use for the GOT_INDX'th got entry which is
12301// for global tls symbol GSYM.
12302template<int size, bool big_endian>
12303int64_t
12304Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12305 Symbol* gsym,
12306 unsigned int got_indx) const
12307{
12308 if (gsym->type() == elfcpp::STT_TLS)
12309 {
12310 for (Got_type got_type = GOT_TYPE_TLSGD;
12311 got_type <= GOT_TYPE_TPREL;
12312 got_type = Got_type(got_type + 1))
12313 if (gsym->has_got_offset(got_type))
12314 {
12315 unsigned int off = gsym->got_offset(got_type);
12316 if (got_type == GOT_TYPE_TLSGD)
12317 off += size / 8;
12318 if (off == got_indx * (size / 8))
12319 {
12320 if (got_type == GOT_TYPE_TPREL)
12321 return -tp_offset;
12322 else
12323 return -dtp_offset;
12324 }
12325 }
12326 }
12327 gold_unreachable();
12328}
12329
42cacb20
DE
12330// The selector for powerpc object files.
12331
12332template<int size, bool big_endian>
12333class Target_selector_powerpc : public Target_selector
12334{
12335public:
12336 Target_selector_powerpc()
edc27beb
AM
12337 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12338 size, big_endian,
03ef7571
ILT
12339 (size == 64
12340 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12341 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12342 (size == 64
12343 ? (big_endian ? "elf64ppc" : "elf64lppc")
12344 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
12345 { }
12346
2e702c99
RM
12347 virtual Target*
12348 do_instantiate_target()
7f055c20 12349 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
12350};
12351
12352Target_selector_powerpc<32, true> target_selector_ppc32;
12353Target_selector_powerpc<32, false> target_selector_ppc32le;
12354Target_selector_powerpc<64, true> target_selector_ppc64;
12355Target_selector_powerpc<64, false> target_selector_ppc64le;
12356
decdd3bc
AM
12357// Instantiate these constants for -O0
12358template<int size, bool big_endian>
9055360d
AM
12359const typename Output_data_glink<size, big_endian>::Address
12360 Output_data_glink<size, big_endian>::invalid_address;
12361template<int size, bool big_endian>
decdd3bc
AM
12362const typename Stub_table<size, big_endian>::Address
12363 Stub_table<size, big_endian>::invalid_address;
12364template<int size, bool big_endian>
12365const typename Target_powerpc<size, big_endian>::Address
12366 Target_powerpc<size, big_endian>::invalid_address;
12367
42cacb20 12368} // End anonymous namespace.
This page took 1.219815 seconds and 4 git commands to generate.