]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/powerpc.cc
Updated Spanish translation for the bfd/ directory
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
fd67aa11 3// Copyright (C) 2008-2024 Free Software Foundation, Inc.
42cacb20
DE
4// Written by David S. Miller <davem@davemloft.net>
5// and David Edelsohn <edelsohn@gnu.org>
6
7// This file is part of gold.
8
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 3 of the License, or
12// (at your option) any later version.
13
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22// MA 02110-1301, USA.
23
24#include "gold.h"
25
dc3714f3 26#include <set>
ec661b9d 27#include <algorithm>
42cacb20 28#include "elfcpp.h"
9d5781f8 29#include "dwarf.h"
42cacb20
DE
30#include "parameters.h"
31#include "reloc.h"
32#include "powerpc.h"
33#include "object.h"
34#include "symtab.h"
35#include "layout.h"
36#include "output.h"
37#include "copy-relocs.h"
38#include "target.h"
39#include "target-reloc.h"
40#include "target-select.h"
41#include "tls.h"
42#include "errors.h"
f345227a 43#include "gc.h"
724436fc 44#include "attributes.h"
42cacb20
DE
45
46namespace
47{
48
49using namespace gold;
50
51template<int size, bool big_endian>
52class Output_data_plt_powerpc;
53
ec661b9d
AM
54template<int size, bool big_endian>
55class Output_data_brlt_powerpc;
56
cf43a2fe
AM
57template<int size, bool big_endian>
58class Output_data_got_powerpc;
59
60template<int size, bool big_endian>
61class Output_data_glink;
62
ec661b9d
AM
63template<int size, bool big_endian>
64class Stub_table;
65
d49044c7
AM
66template<int size, bool big_endian>
67class Output_data_save_res;
68
a3e60ddb
AM
69template<int size, bool big_endian>
70class Target_powerpc;
71
72struct Stub_table_owner
73{
dc60b26d
AM
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
a3e60ddb
AM
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80};
81
32f59844 82template<int size>
23cedd1d
AM
83inline bool is_branch_reloc(unsigned int);
84
85template<int size>
86inline bool is_plt16_reloc(unsigned int);
4d9aa155 87
590b87ff
AM
88// Counter incremented on every Powerpc_relobj constructed.
89static uint32_t object_id = 0;
90
cf43a2fe
AM
91template<int size, bool big_endian>
92class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93{
94public:
dd93cd0a 95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 98
cf43a2fe
AM
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
590b87ff
AM
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
724436fc
AM
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
b4f7960d
AM
107 {
108 this->set_abiversion(0);
109 }
cf43a2fe
AM
110
111 ~Powerpc_relobj()
724436fc 112 { delete this->attributes_section_data_; }
cf43a2fe 113
b4f7960d
AM
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
5edad15d
AM
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
c9269dff 164 // The .got2 section shndx.
cf43a2fe
AM
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
c9269dff 169 return this->special_;
cf43a2fe
AM
170 else
171 return 0;
172 }
173
c9269dff
AM
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 189 this->opd_ent_.resize(count);
c9269dff
AM
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 199 if (value != NULL)
bfdfa4cd
AM
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
c9269dff
AM
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
dd93cd0a 206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
c9269dff
AM
230 }
231
e81fea4d
AM
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
c9269dff
AM
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
5edad15d
AM
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
c9b8abb7
AM
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
257
26a4e9cb
AM
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
c9269dff
AM
260 void
261 do_read_relocs(Read_relocs_data*);
262
cf43a2fe
AM
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
265
ec4dbad3
AM
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
270 {
271 if (size == 64 && this->opd_shndx() != 0)
272 {
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
278 }
279 return true;
280 }
281
6c77229c
AM
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
285
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
efc6fa12 289 add_reference(Relobj* src_obj,
6c77229c
AM
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292 {
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
295 }
296
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301 {
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
306 }
307
308 void
309 process_gc_mark(Symbol_table* symtab)
310 {
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
313 {
314 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
316 }
317 }
318
d8f5a274
AM
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
322
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
326
ec661b9d
AM
327 void
328 set_has_14bit_branch(unsigned int shndx)
329 {
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
333 }
334
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
338
339 void
a3e60ddb 340 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 341 {
a3e60ddb 342 if (shndx >= this->stub_table_index_.size())
dc60b26d 343 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 344 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
345 }
346
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
349 {
a3e60ddb
AM
350 if (shndx < this->stub_table_index_.size())
351 {
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
a3e60ddb 358 }
ec661b9d
AM
359 return NULL;
360 }
361
a3e60ddb
AM
362 void
363 clear_stub_table()
364 {
365 this->stub_table_index_.clear();
366 }
367
590b87ff
AM
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
371
b4f7960d
AM
372 int
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
379
7ee7ff70
AM
380 unsigned int
381 st_other (unsigned int symndx) const
382 {
383 return this->st_other_[symndx];
384 }
385
b4f7960d
AM
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393
32f59844
AM
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
397
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
401
724436fc
AM
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
406
cf43a2fe 407private:
bfdfa4cd
AM
408 struct Opd_ent
409 {
410 unsigned int shndx;
c6de8ed4
AM
411 bool discard : 1;
412 bool gc_mark : 1;
26a4e9cb 413 Address off;
bfdfa4cd
AM
414 };
415
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
c9269dff
AM
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
429
590b87ff
AM
430 // Per object unique identifier
431 uint32_t uniq_;
432
c9269dff
AM
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
bfdfa4cd 435
5edad15d
AM
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
439
d8f5a274
AM
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
443
bfdfa4cd
AM
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
450
590b87ff
AM
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
453
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
457
c9269dff
AM
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
bfdfa4cd 460 // relocation on this word. The following vector records the
c9269dff 461 // section and offset specified by these relocations.
bfdfa4cd
AM
462 std::vector<Opd_ent> opd_ent_;
463
e81fea4d 464 // References made to this object's .opd section when running
bfdfa4cd
AM
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
e81fea4d 467 Access_from access_from_map_;
ec661b9d
AM
468
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
471
472 // The stub table to use for a given input section.
a3e60ddb 473 std::vector<unsigned int> stub_table_index_;
b4f7960d 474
b4f7960d
AM
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
724436fc
AM
477
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
cf43a2fe
AM
480};
481
dc3714f3
AM
482template<int size, bool big_endian>
483class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484{
485public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
724436fc
AM
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
b4f7960d
AM
493 {
494 this->set_abiversion(0);
495 }
dc3714f3
AM
496
497 ~Powerpc_dynobj()
724436fc 498 { delete this->attributes_section_data_; }
dc3714f3
AM
499
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
504
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
508 {
509 return this->opd_shndx_;
510 }
511
512 // The .opd section address.
513 Address
514 opd_address() const
515 {
516 return this->opd_address_;
517 }
518
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
522 {
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
525 }
526
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
530 {
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
537 }
538
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
542 {
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
547 }
548
b4f7960d
AM
549 int
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
556
724436fc
AM
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
561
dc3714f3
AM
562private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
565 {
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
568 { }
569
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
573
574 Address start;
575 Address len;
576 unsigned int shndx;
577 };
578
579 struct Opd_ent
580 {
581 unsigned int shndx;
582 Address off;
583 };
584
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
589
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
593
590b87ff
AM
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
596
dc3714f3
AM
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
724436fc
AM
602
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
dc3714f3
AM
605};
606
5edad15d
AM
607// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608// base class will emit.
609
610template<int sh_type, int size, bool big_endian>
611class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612{
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616 { }
617
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622};
623
f19c3684
AM
624// The types of GOT entries needed for this platform.
625// These values are exposed to the ABI in an incremental link, but
626// powerpc does not support incremental linking as yet.
627enum Got_type
628 {
629 GOT_TYPE_STANDARD = 0,
630 GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
631 GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
632 GOT_TYPE_TPREL = 3, // entry for @got@tprel
633 GOT_TYPE_SMALL = 4,
634 GOT_TYPE_SMALL_TLSGD = 5,
635 GOT_TYPE_SMALL_DTPREL = 6,
636 GOT_TYPE_SMALL_TPREL = 7
637 };
638
0961e631
AM
639// gsym->needs_plt_entry purpose is to decide whether a non-branch
640// reloc should reference a plt entry. It can't be used to decide
641// whether branches need a plt entry. In fact the call to
642// needs_plt_entry here is not needed; All cases where it might
643// return true ought to be covered already. However, since this
644// function is used to decide between plt_ and lplt_ sections in
645// plt_off, make certain that every case where make_plt_entry puts
646// entries in plt_ is covered here.
647static bool
648branch_needs_plt_entry(const Symbol* gsym)
649{
650 return (((!gsym->is_defined()
651 || gsym->is_from_dynobj()
652 || gsym->is_preemptible())
653 && !gsym->final_value_is_known())
654 || gsym->needs_plt_entry());
655}
656
42cacb20
DE
657template<int size, bool big_endian>
658class Target_powerpc : public Sized_target<size, big_endian>
659{
660 public:
d83ce4e3
AM
661 typedef
662 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 664 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 665 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 666 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
667 // Offset of tp and dtp pointers from start of TLS block.
668 static const Address tp_offset = 0x7000;
669 static const Address dtp_offset = 0x8000;
42cacb20
DE
670
671 Target_powerpc()
672 : Sized_target<size, big_endian>(&powerpc_info),
f19c3684
AM
673 got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
674 brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 675 tlsld_got_offset_(-1U),
7e57d19e 676 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
63e5eea2 677 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
7ee7ff70 678 plt_localentry0_init_(false), has_localentry0_(false),
4e0e019f 679 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
7ee7ff70 680 relax_failed_(false), relax_fail_count_(0),
34e0882b 681 stub_group_size_(0), savres_section_(0),
724436fc
AM
682 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
683 attributes_section_data_(NULL),
684 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
42cacb20
DE
685 {
686 }
687
2e702c99 688 // Process the relocations to determine unreferenced sections for
6d03d481
ST
689 // garbage collection.
690 void
ad0f2072 691 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
692 Layout* layout,
693 Sized_relobj_file<size, big_endian>* object,
694 unsigned int data_shndx,
695 unsigned int sh_type,
696 const unsigned char* prelocs,
697 size_t reloc_count,
698 Output_section* output_section,
699 bool needs_special_offset_handling,
700 size_t local_symbol_count,
701 const unsigned char* plocal_symbols);
6d03d481 702
42cacb20
DE
703 // Scan the relocations to look for symbol adjustments.
704 void
ad0f2072 705 scan_relocs(Symbol_table* symtab,
42cacb20 706 Layout* layout,
6fa2a40b 707 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
708 unsigned int data_shndx,
709 unsigned int sh_type,
710 const unsigned char* prelocs,
711 size_t reloc_count,
712 Output_section* output_section,
713 bool needs_special_offset_handling,
714 size_t local_symbol_count,
715 const unsigned char* plocal_symbols);
921b5322
AM
716
717 // Map input .toc section to output .got section.
718 const char*
719 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
720 {
721 if (size == 64 && strcmp(name, ".toc") == 0)
722 {
723 *plen = 4;
724 return ".got";
725 }
726 return NULL;
727 }
728
f3a0ed29
AM
729 // Provide linker defined save/restore functions.
730 void
731 define_save_restore_funcs(Layout*, Symbol_table*);
732
ec661b9d
AM
733 // No stubs unless a final link.
734 bool
735 do_may_relax() const
736 { return !parameters->options().relocatable(); }
737
738 bool
739 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
740
9d5781f8
AM
741 void
742 do_plt_fde_location(const Output_data*, unsigned char*,
743 uint64_t*, off_t*) const;
744
ec661b9d
AM
745 // Stash info about branches, for stub generation.
746 void
747 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
748 unsigned int data_shndx, Address r_offset,
749 unsigned int r_type, unsigned int r_sym, Address addend)
750 {
751 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
752 this->branch_info_.push_back(info);
753 if (r_type == elfcpp::R_POWERPC_REL14
754 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
755 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
756 ppc_object->set_has_14bit_branch(data_shndx);
757 }
758
7e57d19e
AM
759 // Return whether the last branch is a plt call, and if so, mark the
760 // branch as having an R_PPC64_TOCSAVE.
761 bool
762 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
763 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
764 {
765 return (size == 64
766 && !this->branch_info_.empty()
767 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
768 r_offset, this, symtab));
769 }
770
771 // Say the given location, that of a nop in a function prologue with
772 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
773 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
774 void
775 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
776 unsigned int shndx, Address offset)
777 {
778 Symbol_location loc;
779 loc.object = ppc_object;
780 loc.shndx = shndx;
781 loc.offset = offset;
782 this->tocsave_loc_.insert(loc);
783 }
784
785 // Accessor
b0d0d02b 786 const Tocsave_loc*
7e57d19e
AM
787 tocsave_loc() const
788 {
b0d0d02b 789 return &this->tocsave_loc_;
7e57d19e
AM
790 }
791
f43ba157
AM
792 void
793 do_define_standard_symbols(Symbol_table*, Layout*);
794
42cacb20
DE
795 // Finalize the sections.
796 void
f59f41f3 797 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20 798
bdad2ad5
AM
799 // Get the custom dynamic tag value.
800 unsigned int
801 do_dynamic_tag_custom_value(elfcpp::DT) const;
802
42cacb20
DE
803 // Return the value to use for a dynamic which requires special
804 // treatment.
805 uint64_t
806 do_dynsym_value(const Symbol*) const;
807
c9824451
AM
808 // Return the PLT address to use for a local symbol.
809 uint64_t
810 do_plt_address_for_local(const Relobj*, unsigned int) const;
811
812 // Return the PLT address to use for a global symbol.
813 uint64_t
814 do_plt_address_for_global(const Symbol*) const;
815
bd73a62d
AM
816 // Return the offset to use for the GOT_INDX'th got entry which is
817 // for a local tls symbol specified by OBJECT, SYMNDX.
818 int64_t
819 do_tls_offset_for_local(const Relobj* object,
820 unsigned int symndx,
f19c3684 821 Output_data_got_base* got,
e4d49a0f
AM
822 unsigned int got_indx,
823 uint64_t addend) const;
bd73a62d
AM
824
825 // Return the offset to use for the GOT_INDX'th got entry which is
826 // for global tls symbol GSYM.
827 int64_t
f19c3684
AM
828 do_tls_offset_for_global(Symbol* gsym,
829 Output_data_got_base* got, unsigned int got_indx,
e4d49a0f 830 uint64_t addend) const;
bd73a62d 831
dc3714f3
AM
832 void
833 do_function_location(Symbol_location*) const;
834
4d9aa155
AM
835 bool
836 do_can_check_for_function_pointers() const
837 { return true; }
838
bbec1a5d
AM
839 // Adjust -fsplit-stack code which calls non-split-stack code.
840 void
841 do_calls_non_split(Relobj* object, unsigned int shndx,
842 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 843 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
844 unsigned char* view, section_size_type view_size,
845 std::string* from, std::string* to) const;
846
42cacb20
DE
847 // Relocate a section.
848 void
849 relocate_section(const Relocate_info<size, big_endian>*,
850 unsigned int sh_type,
851 const unsigned char* prelocs,
852 size_t reloc_count,
853 Output_section* output_section,
854 bool needs_special_offset_handling,
855 unsigned char* view,
c9269dff 856 Address view_address,
364c7fa5
ILT
857 section_size_type view_size,
858 const Reloc_symbol_changes*);
42cacb20
DE
859
860 // Scan the relocs during a relocatable link.
861 void
ad0f2072 862 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 863 Layout* layout,
6fa2a40b 864 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
865 unsigned int data_shndx,
866 unsigned int sh_type,
867 const unsigned char* prelocs,
868 size_t reloc_count,
869 Output_section* output_section,
870 bool needs_special_offset_handling,
871 size_t local_symbol_count,
872 const unsigned char* plocal_symbols,
873 Relocatable_relocs*);
874
4d625b70
CC
875 // Scan the relocs for --emit-relocs.
876 void
877 emit_relocs_scan(Symbol_table* symtab,
878 Layout* layout,
879 Sized_relobj_file<size, big_endian>* object,
880 unsigned int data_shndx,
881 unsigned int sh_type,
882 const unsigned char* prelocs,
883 size_t reloc_count,
884 Output_section* output_section,
885 bool needs_special_offset_handling,
886 size_t local_symbol_count,
887 const unsigned char* plocal_syms,
888 Relocatable_relocs* rr);
889
7404fe1b 890 // Emit relocations for a section.
42cacb20 891 void
7404fe1b
AM
892 relocate_relocs(const Relocate_info<size, big_endian>*,
893 unsigned int sh_type,
894 const unsigned char* prelocs,
895 size_t reloc_count,
896 Output_section* output_section,
62fe925a
RM
897 typename elfcpp::Elf_types<size>::Elf_Off
898 offset_in_output_section,
7404fe1b
AM
899 unsigned char*,
900 Address view_address,
901 section_size_type,
902 unsigned char* reloc_view,
903 section_size_type reloc_view_size);
42cacb20
DE
904
905 // Return whether SYM is defined by the ABI.
906 bool
9c2d0ef9 907 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 908 {
cf43a2fe 909 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
910 }
911
f19c3684 912 // Return the size of the GOT section, for incremental linking
42cacb20 913 section_size_type
0e70b911 914 got_size() const
42cacb20
DE
915 {
916 gold_assert(this->got_ != NULL);
f19c3684
AM
917 return this->got_->data_size() + (this->biggot_
918 ? this->biggot_->data_size() : 0);
42cacb20
DE
919 }
920
cf43a2fe
AM
921 // Get the PLT section.
922 const Output_data_plt_powerpc<size, big_endian>*
923 plt_section() const
924 {
925 gold_assert(this->plt_ != NULL);
926 return this->plt_;
927 }
928
e5d5f5ed
AM
929 // Get the IPLT section.
930 const Output_data_plt_powerpc<size, big_endian>*
931 iplt_section() const
932 {
933 gold_assert(this->iplt_ != NULL);
934 return this->iplt_;
935 }
936
2d7ad24e
AM
937 // Get the LPLT section.
938 const Output_data_plt_powerpc<size, big_endian>*
939 lplt_section() const
940 {
941 return this->lplt_;
942 }
943
08be3224
AM
944 // Return the plt offset and section for the given global sym.
945 Address
946 plt_off(const Symbol* gsym,
947 const Output_data_plt_powerpc<size, big_endian>** sec) const
948 {
949 if (gsym->type() == elfcpp::STT_GNU_IFUNC
950 && gsym->can_use_relative_reloc(false))
951 *sec = this->iplt_section();
0961e631 952 else if (branch_needs_plt_entry(gsym))
08be3224 953 *sec = this->plt_section();
f21dbd7c
AM
954 else
955 *sec = this->lplt_section();
08be3224
AM
956 return gsym->plt_offset();
957 }
958
959 // Return the plt offset and section for the given local sym.
960 Address
961 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
962 unsigned int local_sym_index,
963 const Output_data_plt_powerpc<size, big_endian>** sec) const
964 {
2d7ad24e
AM
965 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
966 if (lsym->is_ifunc_symbol())
967 *sec = this->iplt_section();
968 else
969 *sec = this->lplt_section();
08be3224
AM
970 return relobj->local_plt_offset(local_sym_index);
971 }
972
cf43a2fe
AM
973 // Get the .glink section.
974 const Output_data_glink<size, big_endian>*
975 glink_section() const
976 {
977 gold_assert(this->glink_ != NULL);
978 return this->glink_;
979 }
980
9055360d
AM
981 Output_data_glink<size, big_endian>*
982 glink_section()
983 {
984 gold_assert(this->glink_ != NULL);
985 return this->glink_;
986 }
987
9d5781f8
AM
988 bool has_glink() const
989 { return this->glink_ != NULL; }
990
cf43a2fe
AM
991 // Get the GOT section.
992 const Output_data_got_powerpc<size, big_endian>*
f19c3684 993 got_section(Got_type got_type) const
cf43a2fe
AM
994 {
995 gold_assert(this->got_ != NULL);
f19c3684
AM
996 if (size == 32 || (got_type & GOT_TYPE_SMALL))
997 return this->got_;
998 gold_assert(this->biggot_ != NULL);
999 return this->biggot_;
cf43a2fe
AM
1000 }
1001
26a4e9cb
AM
1002 // Get the GOT section, creating it if necessary.
1003 Output_data_got_powerpc<size, big_endian>*
f19c3684 1004 got_section(Symbol_table*, Layout*, Got_type);
26a4e9cb 1005
a19da04b
AM
1006 // The toc/got pointer reg will be set to this value.
1007 Address
1008 toc_pointer() const
1009 {
1010 return this->got_->address() + this->got_->g_o_t();
1011 }
1012
1013 // Offset of base used to access the GOT/TOC relative to the GOT section.
1014 Address
f19c3684 1015 got_base_offset(Got_type got_type) const
a19da04b 1016 {
f19c3684
AM
1017 if (size == 32 || (got_type & GOT_TYPE_SMALL))
1018 return this->got_->g_o_t();
1019 return this->toc_pointer() - this->biggot_->address();
a19da04b
AM
1020 }
1021
cf43a2fe
AM
1022 Object*
1023 do_make_elf_object(const std::string&, Input_file*, off_t,
1024 const elfcpp::Ehdr<size, big_endian>&);
1025
0e70b911
CC
1026 // Return the number of entries in the GOT.
1027 unsigned int
1028 got_entry_count() const
1029 {
1030 if (this->got_ == NULL)
1031 return 0;
1032 return this->got_size() / (size / 8);
1033 }
1034
1035 // Return the number of entries in the PLT.
1036 unsigned int
1037 plt_entry_count() const;
1038
1039 // Return the offset of the first non-reserved PLT entry.
1040 unsigned int
b4f7960d
AM
1041 first_plt_entry_offset() const
1042 {
1043 if (size == 32)
1044 return 0;
1045 if (this->abiversion() >= 2)
1046 return 16;
1047 return 24;
1048 }
0e70b911
CC
1049
1050 // Return the size of each PLT entry.
1051 unsigned int
b4f7960d
AM
1052 plt_entry_size() const
1053 {
1054 if (size == 32)
1055 return 4;
1056 if (this->abiversion() >= 2)
1057 return 8;
1058 return 24;
1059 }
0e70b911 1060
d49044c7
AM
1061 Output_data_save_res<size, big_endian>*
1062 savres_section() const
1063 {
1064 return this->savres_section_;
1065 }
1066
e81fea4d
AM
1067 // Add any special sections for this symbol to the gc work list.
1068 // For powerpc64, this adds the code section of a function
1069 // descriptor.
1070 void
1071 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1072
1073 // Handle target specific gc actions when adding a gc reference from
1074 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1075 // and DST_OFF. For powerpc64, this adds a referenc to the code
1076 // section of a function descriptor.
1077 void
1078 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 1079 Relobj* src_obj,
e81fea4d 1080 unsigned int src_shndx,
efc6fa12 1081 Relobj* dst_obj,
e81fea4d
AM
1082 unsigned int dst_shndx,
1083 Address dst_off) const;
1084
ec661b9d
AM
1085 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1086 const Stub_tables&
1087 stub_tables() const
1088 { return this->stub_tables_; }
1089
1090 const Output_data_brlt_powerpc<size, big_endian>*
1091 brlt_section() const
1092 { return this->brlt_section_; }
1093
1094 void
1095 add_branch_lookup_table(Address to)
1096 {
1097 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1098 this->branch_lookup_table_.insert(std::make_pair(to, off));
1099 }
1100
1101 Address
1102 find_branch_lookup_table(Address to)
1103 {
1104 typename Branch_lookup_table::const_iterator p
1105 = this->branch_lookup_table_.find(to);
1106 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1107 }
1108
1109 void
1110 write_branch_lookup_table(unsigned char *oview)
1111 {
1112 for (typename Branch_lookup_table::const_iterator p
1113 = this->branch_lookup_table_.begin();
1114 p != this->branch_lookup_table_.end();
1115 ++p)
1116 {
4d5effb9 1117 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
1118 }
1119 }
1120
590b87ff
AM
1121 // Wrapper used after relax to define a local symbol in output data,
1122 // from the end if value < 0.
1123 void
1124 define_local(Symbol_table* symtab, const char* name,
1125 Output_data* od, Address value, unsigned int symsize)
1126 {
1127 Symbol* sym
1128 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1129 od, value, symsize, elfcpp::STT_NOTYPE,
1130 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1131 static_cast<Signed_address>(value) < 0,
1132 false);
1133 // We are creating this symbol late, so need to fix up things
1134 // done early in Layout::finalize.
1135 sym->set_dynsym_index(-1U);
1136 }
1137
63e5eea2
AM
1138 void
1139 set_power10_relocs()
1140 {
1141 this->power10_relocs_ = true;
1142 }
1143
e4dff765 1144 bool
7c1f4227 1145 power10_stubs() const
e4dff765 1146 {
63e5eea2
AM
1147 return (this->power10_relocs_
1148 && (parameters->options().power10_stubs_enum()
1149 != General_options::POWER10_STUBS_NO));
afd2ea23
AM
1150 }
1151
1152 bool
1153 power10_stubs_auto() const
1154 {
1155 return (parameters->options().power10_stubs_enum()
1156 == General_options::POWER10_STUBS_AUTO);
e4dff765
AM
1157 }
1158
9e69ed50
AM
1159 bool
1160 plt_thread_safe() const
1161 { return this->plt_thread_safe_; }
1162
7ee7ff70
AM
1163 bool
1164 plt_localentry0() const
1165 { return this->plt_localentry0_; }
1166
63e5eea2
AM
1167 bool
1168 has_localentry0() const
1169 { return this->has_localentry0_; }
1170
7ee7ff70
AM
1171 void
1172 set_has_localentry0()
1173 {
1174 this->has_localentry0_ = true;
1175 }
1176
1177 bool
1178 is_elfv2_localentry0(const Symbol* gsym) const
1179 {
1180 return (size == 64
1181 && this->abiversion() >= 2
1182 && this->plt_localentry0()
1183 && gsym->type() == elfcpp::STT_FUNC
1184 && gsym->is_defined()
565ed01a
AM
1185 && gsym->nonvis() >> 3 == 0
1186 && !gsym->non_zero_localentry());
7ee7ff70
AM
1187 }
1188
1189 bool
1190 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1191 unsigned int r_sym) const
1192 {
1193 const Powerpc_relobj<size, big_endian>* ppc_object
1194 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1195
1196 if (size == 64
1197 && this->abiversion() >= 2
1198 && this->plt_localentry0()
1199 && ppc_object->st_other(r_sym) >> 5 == 0)
1200 {
1201 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1202 bool is_ordinary;
1203 if (!psymval->is_ifunc_symbol()
1204 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1205 && is_ordinary)
1206 return true;
1207 }
1208 return false;
1209 }
1210
252dcdf4
AM
1211 bool
1212 tprel_opt() const
4e0e019f 1213 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
252dcdf4
AM
1214
1215 void
4e0e019f
AM
1216 set_no_tprel_opt()
1217 { this->no_tprel_opt_ = true; }
252dcdf4 1218
565ed01a
AM
1219 // Remember any symbols seen with non-zero localentry, even those
1220 // not providing a definition
1221 bool
1222 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1223 const char*)
1224 {
1225 if (size == 64)
1226 {
1227 unsigned char st_other = sym.get_st_other();
1228 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1229 to->set_non_zero_localentry();
1230 }
1231 // We haven't resolved anything, continue normal processing.
1232 return false;
1233 }
1234
b4f7960d 1235 int
aacb3b6d 1236 abiversion() const
b4f7960d
AM
1237 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1238
1239 void
aacb3b6d 1240 set_abiversion(int ver)
b4f7960d
AM
1241 {
1242 elfcpp::Elf_Word flags = this->processor_specific_flags();
1243 flags &= ~elfcpp::EF_PPC64_ABI;
1244 flags |= ver & elfcpp::EF_PPC64_ABI;
1245 this->set_processor_specific_flags(flags);
1246 }
1247
34e0882b
AM
1248 Symbol*
1249 tls_get_addr_opt() const
1250 { return this->tls_get_addr_opt_; }
1251
1252 Symbol*
1253 tls_get_addr() const
1254 { return this->tls_get_addr_; }
1255
1256 // If optimizing __tls_get_addr calls, whether this is the
1257 // "__tls_get_addr" symbol.
1258 bool
1259 is_tls_get_addr_opt(const Symbol* gsym) const
1260 {
1261 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1262 || gsym == this->tls_get_addr_opt_);
1263 }
1264
1265 bool
1266 replace_tls_get_addr(const Symbol* gsym) const
1267 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1268
1269 void
1270 set_has_tls_get_addr_opt()
1271 { this->has_tls_get_addr_opt_ = true; }
1272
aacb3b6d 1273 // Offset to toc save stack slot
b4f7960d 1274 int
aacb3b6d 1275 stk_toc() const
b4f7960d
AM
1276 { return this->abiversion() < 2 ? 40 : 24; }
1277
34e0882b
AM
1278 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1279 // so use the CR save slot. Used only by __tls_get_addr call stub,
1280 // relying on __tls_get_addr not saving CR itself.
1281 int
1282 stk_linker() const
1283 { return this->abiversion() < 2 ? 32 : 8; }
1284
724436fc
AM
1285 // Merge object attributes from input object with those in the output.
1286 void
6f3fe02b 1287 merge_object_attributes(const Object*, const Attributes_section_data*);
724436fc 1288
f21dbd7c
AM
1289 bool
1290 symval_for_branch(const Symbol_table* symtab,
1291 const Sized_symbol<size>* gsym,
1292 Powerpc_relobj<size, big_endian>* object,
1293 Address *value, unsigned int *dest_shndx);
1294
42cacb20
DE
1295 private:
1296
e3deeb9c
AM
1297 class Track_tls
1298 {
1299 public:
1300 enum Tls_get_addr
1301 {
1302 NOT_EXPECTED = 0,
1303 EXPECTED = 1,
1304 SKIP = 2,
1305 NORMAL = 3
1306 };
1307
1308 Track_tls()
aacb3b6d 1309 : tls_get_addr_state_(NOT_EXPECTED),
e3deeb9c
AM
1310 relinfo_(NULL), relnum_(0), r_offset_(0)
1311 { }
1312
1313 ~Track_tls()
1314 {
aacb3b6d 1315 if (this->tls_get_addr_state_ != NOT_EXPECTED)
e3deeb9c
AM
1316 this->missing();
1317 }
1318
1319 void
1320 missing(void)
1321 {
1322 if (this->relinfo_ != NULL)
1323 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1324 _("missing expected __tls_get_addr call"));
1325 }
1326
1327 void
1328 expect_tls_get_addr_call(
1329 const Relocate_info<size, big_endian>* relinfo,
1330 size_t relnum,
1331 Address r_offset)
1332 {
aacb3b6d 1333 this->tls_get_addr_state_ = EXPECTED;
e3deeb9c
AM
1334 this->relinfo_ = relinfo;
1335 this->relnum_ = relnum;
1336 this->r_offset_ = r_offset;
1337 }
1338
1339 void
1340 expect_tls_get_addr_call()
aacb3b6d 1341 { this->tls_get_addr_state_ = EXPECTED; }
e3deeb9c
AM
1342
1343 void
1344 skip_next_tls_get_addr_call()
aacb3b6d 1345 {this->tls_get_addr_state_ = SKIP; }
e3deeb9c
AM
1346
1347 Tls_get_addr
34e0882b
AM
1348 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1349 unsigned int r_type, const Symbol* gsym)
e3deeb9c 1350 {
32f59844
AM
1351 bool is_tls_call
1352 = ((r_type == elfcpp::R_POWERPC_REL24
1353 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 1354 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
32f59844
AM
1355 || r_type == elfcpp::R_PPC_PLTREL24
1356 || is_plt16_reloc<size>(r_type)
e4dff765
AM
1357 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1358 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
32f59844
AM
1359 || r_type == elfcpp::R_POWERPC_PLTSEQ
1360 || r_type == elfcpp::R_POWERPC_PLTCALL
1361 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1362 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1363 && gsym != NULL
1364 && (gsym == target->tls_get_addr()
1365 || gsym == target->tls_get_addr_opt()));
aacb3b6d
AM
1366 Tls_get_addr last_tls = this->tls_get_addr_state_;
1367 this->tls_get_addr_state_ = NOT_EXPECTED;
e3deeb9c
AM
1368 if (is_tls_call && last_tls != EXPECTED)
1369 return last_tls;
1370 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1371 {
1372 this->missing();
1373 return EXPECTED;
1374 }
1375 return NORMAL;
1376 }
1377
1378 private:
1379 // What we're up to regarding calls to __tls_get_addr.
1380 // On powerpc, the branch and link insn making a call to
1381 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1382 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
32f59844 1383 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
e3deeb9c
AM
1384 // The marker relocation always comes first, and has the same
1385 // symbol as the reloc on the insn setting up the __tls_get_addr
1386 // argument. This ties the arg setup insn with the call insn,
1387 // allowing ld to safely optimize away the call. We check that
1388 // every call to __tls_get_addr has a marker relocation, and that
1389 // every marker relocation is on a call to __tls_get_addr.
aacb3b6d 1390 Tls_get_addr tls_get_addr_state_;
e3deeb9c
AM
1391 // Info about the last reloc for error message.
1392 const Relocate_info<size, big_endian>* relinfo_;
1393 size_t relnum_;
1394 Address r_offset_;
1395 };
1396
42cacb20 1397 // The class which scans relocations.
e3deeb9c 1398 class Scan : protected Track_tls
42cacb20
DE
1399 {
1400 public:
bfdfa4cd
AM
1401 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1402
42cacb20 1403 Scan()
e3deeb9c 1404 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1405 { }
1406
95a2c8d6 1407 static inline int
88b8e639 1408 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1409
42cacb20 1410 inline void
ad0f2072 1411 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1412 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1413 unsigned int data_shndx,
1414 Output_section* output_section,
1415 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1416 const elfcpp::Sym<size, big_endian>& lsym,
1417 bool is_discarded);
42cacb20
DE
1418
1419 inline void
ad0f2072 1420 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1421 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1422 unsigned int data_shndx,
1423 Output_section* output_section,
1424 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1425 Symbol* gsym);
1426
21bb3914
ST
1427 inline bool
1428 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1429 Target_powerpc* ,
f6971787 1430 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1431 unsigned int ,
2e702c99
RM
1432 Output_section* ,
1433 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1434 unsigned int r_type,
2e702c99 1435 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1436 {
1437 // PowerPC64 .opd is not folded, so any identical function text
1438 // may be folded and we'll still keep function addresses distinct.
1439 // That means no reloc is of concern here.
1440 if (size == 64)
f6971787
AM
1441 {
1442 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1443 <Powerpc_relobj<size, big_endian>*>(relobj);
1444 if (ppcobj->abiversion() == 1)
1445 return false;
1446 }
1447 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155 1448 // function code might be taking the address of the function.
32f59844 1449 return !is_branch_reloc<size>(r_type);
4d9aa155 1450 }
21bb3914
ST
1451
1452 inline bool
1453 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1454 Target_powerpc* ,
f6971787 1455 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1456 unsigned int ,
1457 Output_section* ,
4d9aa155
AM
1458 const elfcpp::Rela<size, big_endian>& ,
1459 unsigned int r_type,
1460 Symbol*)
1461 {
1462 // As above.
1463 if (size == 64)
f6971787
AM
1464 {
1465 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1466 <Powerpc_relobj<size, big_endian>*>(relobj);
1467 if (ppcobj->abiversion() == 1)
1468 return false;
1469 }
32f59844 1470 return !is_branch_reloc<size>(r_type);
4d9aa155 1471 }
21bb3914 1472
b3ccdeb5 1473 static bool
9055360d
AM
1474 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1475 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1476 unsigned int r_type, bool report_err);
1477
42cacb20
DE
1478 private:
1479 static void
6fa2a40b 1480 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1481 unsigned int r_type);
1482
1483 static void
6fa2a40b 1484 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1485 unsigned int r_type, Symbol*);
1486
1487 static void
1488 generate_tls_call(Symbol_table* symtab, Layout* layout,
1489 Target_powerpc* target);
1490
1491 void
1492 check_non_pic(Relobj*, unsigned int r_type);
1493
1494 // Whether we have issued an error about a non-PIC compilation.
1495 bool issued_non_pic_error_;
1496 };
1497
1498 // The class which implements relocation.
e3deeb9c 1499 class Relocate : protected Track_tls
42cacb20
DE
1500 {
1501 public:
dd93cd0a
AM
1502 // Use 'at' branch hints when true, 'y' when false.
1503 // FIXME maybe: set this with an option.
1504 static const bool is_isa_v2 = true;
1505
dd93cd0a 1506 Relocate()
e3deeb9c 1507 : Track_tls()
dd93cd0a
AM
1508 { }
1509
42cacb20
DE
1510 // Do a relocation. Return false if the caller should not issue
1511 // any warnings about this relocation.
1512 inline bool
91a65d2f
AM
1513 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1514 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1515 const Sized_symbol<size>*, const Symbol_value<size>*,
1516 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1517 section_size_type);
42cacb20
DE
1518 };
1519
168a4726
AM
1520 class Relocate_comdat_behavior
1521 {
1522 public:
1523 // Decide what the linker should do for relocations that refer to
1524 // discarded comdat sections.
1525 inline Comdat_behavior
1526 get(const char* name)
1527 {
1528 gold::Default_comdat_behavior default_behavior;
1529 Comdat_behavior ret = default_behavior.get(name);
43193fe9 1530 if (ret == CB_ERROR)
168a4726
AM
1531 {
1532 if (size == 32
1533 && (strcmp(name, ".fixup") == 0
1534 || strcmp(name, ".got2") == 0))
1535 ret = CB_IGNORE;
1536 if (size == 64
1537 && (strcmp(name, ".opd") == 0
1538 || strcmp(name, ".toc") == 0
1539 || strcmp(name, ".toc1") == 0))
1540 ret = CB_IGNORE;
1541 }
1542 return ret;
1543 }
1544 };
1545
dd93cd0a
AM
1546 // Optimize the TLS relocation type based on what we know about the
1547 // symbol. IS_FINAL is true if the final address of this symbol is
1548 // known at link time.
1549
1550 tls::Tls_optimization
1551 optimize_tls_gd(bool is_final)
1552 {
1553 // If we are generating a shared library, then we can't do anything
1554 // in the linker.
aacb3b6d
AM
1555 if (parameters->options().shared()
1556 || !parameters->options().tls_optimize())
dd93cd0a
AM
1557 return tls::TLSOPT_NONE;
1558
1559 if (!is_final)
1560 return tls::TLSOPT_TO_IE;
1561 return tls::TLSOPT_TO_LE;
1562 }
1563
1564 tls::Tls_optimization
1565 optimize_tls_ld()
1566 {
aacb3b6d
AM
1567 if (parameters->options().shared()
1568 || !parameters->options().tls_optimize())
dd93cd0a
AM
1569 return tls::TLSOPT_NONE;
1570
1571 return tls::TLSOPT_TO_LE;
1572 }
1573
1574 tls::Tls_optimization
1575 optimize_tls_ie(bool is_final)
1576 {
aacb3b6d
AM
1577 if (!is_final
1578 || parameters->options().shared()
1579 || !parameters->options().tls_optimize())
dd93cd0a
AM
1580 return tls::TLSOPT_NONE;
1581
1582 return tls::TLSOPT_TO_LE;
1583 }
cf43a2fe 1584
cf43a2fe
AM
1585 // Create glink.
1586 void
1587 make_glink_section(Layout*);
42cacb20 1588
cf43a2fe
AM
1589 // Create the PLT section.
1590 void
40b469d7 1591 make_plt_section(Symbol_table*, Layout*);
42cacb20 1592
e5d5f5ed 1593 void
40b469d7 1594 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1595
2d7ad24e 1596 void
f21dbd7c 1597 make_lplt_section(Symbol_table*, Layout*);
2d7ad24e 1598
ec661b9d
AM
1599 void
1600 make_brlt_section(Layout*);
1601
42cacb20
DE
1602 // Create a PLT entry for a global symbol.
1603 void
ec661b9d 1604 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1605
1606 // Create a PLT entry for a local IFUNC symbol.
1607 void
40b469d7 1608 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1609 Sized_relobj_file<size, big_endian>*,
1610 unsigned int);
1611
2d7ad24e
AM
1612 // Create a PLT entry for a local non-IFUNC symbol.
1613 void
f21dbd7c 1614 make_local_plt_entry(Symbol_table*, Layout*,
2d7ad24e
AM
1615 Sized_relobj_file<size, big_endian>*,
1616 unsigned int);
1617
f21dbd7c
AM
1618 void
1619 make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
42cacb20 1620
dd93cd0a
AM
1621 // Create a GOT entry for local dynamic __tls_get_addr.
1622 unsigned int
1623 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1624 Sized_relobj_file<size, big_endian>* object);
1625
42cacb20 1626 unsigned int
dd93cd0a
AM
1627 tlsld_got_offset() const
1628 {
1629 return this->tlsld_got_offset_;
1630 }
42cacb20 1631
42cacb20
DE
1632 // Get the dynamic reloc section, creating it if necessary.
1633 Reloc_section*
1634 rela_dyn_section(Layout*);
1635
b3ccdeb5
AM
1636 // Similarly, but for ifunc symbols get the one for ifunc.
1637 Reloc_section*
1638 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1639
42cacb20
DE
1640 // Copy a relocation against a global symbol.
1641 void
ef9beddf 1642 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1643 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1644 unsigned int shndx, Output_section* output_section,
1645 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1646 {
859d7987 1647 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1648 this->copy_relocs_.copy_reloc(symtab, layout,
1649 symtab->get_sized_symbol<size>(sym),
1650 object, shndx, output_section,
859d7987
CC
1651 r_type, reloc.get_r_offset(),
1652 reloc.get_r_addend(),
1653 this->rela_dyn_section(layout));
42cacb20
DE
1654 }
1655
0cfdc767 1656 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1657 void
a3e60ddb 1658 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1659
1660 // Sort output sections by address.
1661 struct Sort_sections
1662 {
1663 bool
1664 operator()(const Output_section* sec1, const Output_section* sec2)
1665 { return sec1->address() < sec2->address(); }
1666 };
1667
1668 class Branch_info
1669 {
1670 public:
1671 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1672 unsigned int data_shndx,
1673 Address r_offset,
1674 unsigned int r_type,
1675 unsigned int r_sym,
1676 Address addend)
1677 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1678 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1679 { }
1680
1681 ~Branch_info()
1682 { }
1683
7e57d19e
AM
1684 // Return whether this branch is going via a plt call stub, and if
1685 // so, mark it as having an R_PPC64_TOCSAVE.
1686 bool
1687 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1688 unsigned int shndx, Address offset,
1689 Target_powerpc* target, Symbol_table* symtab);
1690
ec661b9d 1691 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1692 bool
ec661b9d
AM
1693 make_stub(Stub_table<size, big_endian>*,
1694 Stub_table<size, big_endian>*,
1695 Symbol_table*) const;
1696
1697 private:
1698 // The branch location..
1699 Powerpc_relobj<size, big_endian>* object_;
1700 unsigned int shndx_;
1701 Address offset_;
1702 // ..and the branch type and destination.
7e57d19e
AM
1703 unsigned int r_type_ : 31;
1704 unsigned int tocsave_ : 1;
ec661b9d
AM
1705 unsigned int r_sym_;
1706 Address addend_;
1707 };
1708
42cacb20
DE
1709 // Information about this specific target which we pass to the
1710 // general Target structure.
1711 static Target::Target_info powerpc_info;
1712
f19c3684
AM
1713 // The small GOT section used by ppc32, and by ppc64 for entries that
1714 // must be addresseed +/-32k from the got pointer.
cf43a2fe 1715 Output_data_got_powerpc<size, big_endian>* got_;
f19c3684
AM
1716 // Another GOT section used for entries that can be addressed +/- 2G
1717 // from the got pointer.
1718 Output_data_got_powerpc<size, big_endian>* biggot_;
1719
b3ccdeb5
AM
1720 // The PLT section. This is a container for a table of addresses,
1721 // and their relocations. Each address in the PLT has a dynamic
1722 // relocation (R_*_JMP_SLOT) and each address will have a
1723 // corresponding entry in .glink for lazy resolution of the PLT.
1724 // ppc32 initialises the PLT to point at the .glink entry, while
1725 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1726 // linker adds a stub that loads the PLT entry into ctr then
1727 // branches to ctr. There may be more than one stub for each PLT
1728 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1729 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1730 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1731 // The IPLT section. Like plt_, this is a container for a table of
1732 // addresses and their relocations, specifically for STT_GNU_IFUNC
1733 // functions that resolve locally (STT_GNU_IFUNC functions that
1734 // don't resolve locally go in PLT). Unlike plt_, these have no
1735 // entry in .glink for lazy resolution, and the relocation section
1736 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1737 // the relocation section may contain relocations against
1738 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1739 // relocation section will appear at the end of other dynamic
1740 // relocations, so that ld.so applies these relocations after other
1741 // dynamic relocations. In a static executable, the relocation
1742 // section is emitted and marked with __rela_iplt_start and
1743 // __rela_iplt_end symbols.
e5d5f5ed 1744 Output_data_plt_powerpc<size, big_endian>* iplt_;
2d7ad24e
AM
1745 // A PLT style section for local, non-ifunc symbols
1746 Output_data_plt_powerpc<size, big_endian>* lplt_;
ec661b9d
AM
1747 // Section holding long branch destinations.
1748 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1749 // The .glink section.
cf43a2fe 1750 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1751 // The dynamic reloc section.
42cacb20
DE
1752 Reloc_section* rela_dyn_;
1753 // Relocs saved to avoid a COPY reloc.
5edad15d 1754 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1755 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1756 unsigned int tlsld_got_offset_;
ec661b9d
AM
1757
1758 Stub_tables stub_tables_;
1759 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1760 Branch_lookup_table branch_lookup_table_;
1761
1762 typedef std::vector<Branch_info> Branches;
1763 Branches branch_info_;
7e57d19e 1764 Tocsave_loc tocsave_loc_;
9e69ed50 1765
5a97377e
AM
1766 off_t rela_dyn_size_;
1767
63e5eea2 1768 bool power10_relocs_;
9e69ed50 1769 bool plt_thread_safe_;
7ee7ff70
AM
1770 bool plt_localentry0_;
1771 bool plt_localentry0_init_;
1772 bool has_localentry0_;
34e0882b 1773 bool has_tls_get_addr_opt_;
4e0e019f 1774 bool no_tprel_opt_;
a3e60ddb
AM
1775
1776 bool relax_failed_;
1777 int relax_fail_count_;
1778 int32_t stub_group_size_;
d49044c7
AM
1779
1780 Output_data_save_res<size, big_endian> *savres_section_;
34e0882b
AM
1781
1782 // The "__tls_get_addr" symbol, if present
1783 Symbol* tls_get_addr_;
1784 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1785 Symbol* tls_get_addr_opt_;
724436fc
AM
1786
1787 // Attributes in output.
1788 Attributes_section_data* attributes_section_data_;
1789
1790 // Last input file to change various attribute tags
1791 const char* last_fp_;
1792 const char* last_ld_;
1793 const char* last_vec_;
1794 const char* last_struct_;
42cacb20
DE
1795};
1796
1797template<>
1798Target::Target_info Target_powerpc<32, true>::powerpc_info =
1799{
1800 32, // size
1801 true, // is_big_endian
1802 elfcpp::EM_PPC, // machine_code
1803 false, // has_make_symbol
1804 false, // has_resolve
1805 false, // has_code_fill
1806 true, // is_default_stack_executable
b3ce541e 1807 false, // can_icf_inline_merge_sections
42cacb20
DE
1808 '\0', // wrap_char
1809 "/usr/lib/ld.so.1", // dynamic_linker
1810 0x10000000, // default_text_segment_address
1811 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1812 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1813 false, // isolate_execinstr
1814 0, // rosegment_gap
8a5e3e08
ILT
1815 elfcpp::SHN_UNDEF, // small_common_shndx
1816 elfcpp::SHN_UNDEF, // large_common_shndx
1817 0, // small_common_section_flags
05a352e6
DK
1818 0, // large_common_section_flags
1819 NULL, // attributes_section
a67858e0 1820 NULL, // attributes_vendor
8d9743bd
MK
1821 "_start", // entry_symbol_name
1822 32, // hash_entry_size
bce5a025 1823 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1824};
1825
1826template<>
1827Target::Target_info Target_powerpc<32, false>::powerpc_info =
1828{
1829 32, // size
1830 false, // is_big_endian
1831 elfcpp::EM_PPC, // machine_code
1832 false, // has_make_symbol
1833 false, // has_resolve
1834 false, // has_code_fill
1835 true, // is_default_stack_executable
b3ce541e 1836 false, // can_icf_inline_merge_sections
42cacb20
DE
1837 '\0', // wrap_char
1838 "/usr/lib/ld.so.1", // dynamic_linker
1839 0x10000000, // default_text_segment_address
1840 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1841 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1842 false, // isolate_execinstr
1843 0, // rosegment_gap
8a5e3e08
ILT
1844 elfcpp::SHN_UNDEF, // small_common_shndx
1845 elfcpp::SHN_UNDEF, // large_common_shndx
1846 0, // small_common_section_flags
05a352e6
DK
1847 0, // large_common_section_flags
1848 NULL, // attributes_section
a67858e0 1849 NULL, // attributes_vendor
8d9743bd
MK
1850 "_start", // entry_symbol_name
1851 32, // hash_entry_size
bce5a025 1852 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1853};
1854
1855template<>
1856Target::Target_info Target_powerpc<64, true>::powerpc_info =
1857{
1858 64, // size
1859 true, // is_big_endian
1860 elfcpp::EM_PPC64, // machine_code
1861 false, // has_make_symbol
565ed01a 1862 true, // has_resolve
42cacb20 1863 false, // has_code_fill
ec769010 1864 false, // is_default_stack_executable
b3ce541e 1865 false, // can_icf_inline_merge_sections
42cacb20
DE
1866 '\0', // wrap_char
1867 "/usr/lib/ld.so.1", // dynamic_linker
1868 0x10000000, // default_text_segment_address
1869 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1870 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1871 false, // isolate_execinstr
1872 0, // rosegment_gap
8a5e3e08
ILT
1873 elfcpp::SHN_UNDEF, // small_common_shndx
1874 elfcpp::SHN_UNDEF, // large_common_shndx
1875 0, // small_common_section_flags
05a352e6
DK
1876 0, // large_common_section_flags
1877 NULL, // attributes_section
a67858e0 1878 NULL, // attributes_vendor
8d9743bd
MK
1879 "_start", // entry_symbol_name
1880 32, // hash_entry_size
bce5a025 1881 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1882};
1883
1884template<>
1885Target::Target_info Target_powerpc<64, false>::powerpc_info =
1886{
1887 64, // size
1888 false, // is_big_endian
1889 elfcpp::EM_PPC64, // machine_code
1890 false, // has_make_symbol
565ed01a 1891 true, // has_resolve
42cacb20 1892 false, // has_code_fill
ec769010 1893 false, // is_default_stack_executable
b3ce541e 1894 false, // can_icf_inline_merge_sections
42cacb20
DE
1895 '\0', // wrap_char
1896 "/usr/lib/ld.so.1", // dynamic_linker
1897 0x10000000, // default_text_segment_address
1898 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1899 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1900 false, // isolate_execinstr
1901 0, // rosegment_gap
8a5e3e08
ILT
1902 elfcpp::SHN_UNDEF, // small_common_shndx
1903 elfcpp::SHN_UNDEF, // large_common_shndx
1904 0, // small_common_section_flags
05a352e6
DK
1905 0, // large_common_section_flags
1906 NULL, // attributes_section
a67858e0 1907 NULL, // attributes_vendor
8d9743bd
MK
1908 "_start", // entry_symbol_name
1909 32, // hash_entry_size
bce5a025 1910 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1911};
1912
32f59844 1913template<int size>
dd93cd0a
AM
1914inline bool
1915is_branch_reloc(unsigned int r_type)
1916{
1917 return (r_type == elfcpp::R_POWERPC_REL24
32f59844 1918 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 1919 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
dd93cd0a
AM
1920 || r_type == elfcpp::R_PPC_PLTREL24
1921 || r_type == elfcpp::R_PPC_LOCAL24PC
1922 || r_type == elfcpp::R_POWERPC_REL14
1923 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1924 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1925 || r_type == elfcpp::R_POWERPC_ADDR24
1926 || r_type == elfcpp::R_POWERPC_ADDR14
1927 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1928 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1929}
1930
08be3224
AM
1931// Reloc resolves to plt entry.
1932template<int size>
1933inline bool
1934is_plt16_reloc(unsigned int r_type)
1935{
1936 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1937 || r_type == elfcpp::R_POWERPC_PLT16_HI
1938 || r_type == elfcpp::R_POWERPC_PLT16_HA
1939 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1940}
1941
f19c3684 1942// GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
4290b0ab
AM
1943inline bool
1944is_got_reloc(unsigned int r_type)
1945{
1946 return (r_type == elfcpp::R_POWERPC_GOT16
1947 || r_type == elfcpp::R_POWERPC_GOT16_LO
1948 || r_type == elfcpp::R_POWERPC_GOT16_HI
1949 || r_type == elfcpp::R_POWERPC_GOT16_HA
1950 || r_type == elfcpp::R_PPC64_GOT16_DS
1951 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1952 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1953}
1954
dd93cd0a
AM
1955// If INSN is an opcode that may be used with an @tls operand, return
1956// the transformed insn for TLS optimisation, otherwise return 0. If
1957// REG is non-zero only match an insn with RB or RA equal to REG.
1958uint32_t
1959at_tls_transform(uint32_t insn, unsigned int reg)
1960{
1961 if ((insn & (0x3f << 26)) != 31 << 26)
1962 return 0;
1963
1964 unsigned int rtra;
1965 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1966 rtra = insn & ((1 << 26) - (1 << 16));
1967 else if (((insn >> 16) & 0x1f) == reg)
1968 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1969 else
1970 return 0;
1971
1972 if ((insn & (0x3ff << 1)) == 266 << 1)
1973 // add -> addi
1974 insn = 14 << 26;
1975 else if ((insn & (0x1f << 1)) == 23 << 1
1976 && ((insn & (0x1f << 6)) < 14 << 6
1977 || ((insn & (0x1f << 6)) >= 16 << 6
1978 && (insn & (0x1f << 6)) < 24 << 6)))
1979 // load and store indexed -> dform
1980 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1981 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1982 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1983 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1984 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1985 // lwax -> lwa
1986 insn = (58 << 26) | 2;
1987 else
1988 return 0;
1989 insn |= rtra;
1990 return insn;
1991}
1992
dd93cd0a 1993
42cacb20
DE
1994template<int size, bool big_endian>
1995class Powerpc_relocate_functions
1996{
dd93cd0a 1997public:
f4baf0d4 1998 enum Overflow_check
dd93cd0a 1999 {
f4baf0d4
AM
2000 CHECK_NONE,
2001 CHECK_SIGNED,
b80eed39
AM
2002 CHECK_UNSIGNED,
2003 CHECK_BITFIELD,
2004 CHECK_LOW_INSN,
2005 CHECK_HIGH_INSN
dd93cd0a
AM
2006 };
2007
f4baf0d4 2008 enum Status
dd93cd0a 2009 {
f4baf0d4
AM
2010 STATUS_OK,
2011 STATUS_OVERFLOW
2012 };
dd93cd0a 2013
42cacb20 2014private:
c9269dff 2015 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 2016 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 2017 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 2018
dd93cd0a
AM
2019 template<int valsize>
2020 static inline bool
2021 has_overflow_signed(Address value)
2022 {
2023 // limit = 1 << (valsize - 1) without shift count exceeding size of type
2024 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2025 limit <<= ((valsize - 1) >> 1);
2026 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2027 return value + limit > (limit << 1) - 1;
2028 }
2029
2030 template<int valsize>
2031 static inline bool
b80eed39 2032 has_overflow_unsigned(Address value)
dd93cd0a
AM
2033 {
2034 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2035 limit <<= ((valsize - 1) >> 1);
2036 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
2037 return value > (limit << 1) - 1;
2038 }
2039
2040 template<int valsize>
2041 static inline bool
2042 has_overflow_bitfield(Address value)
2043 {
2044 return (has_overflow_unsigned<valsize>(value)
2045 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
2046 }
2047
2048 template<int valsize>
f4baf0d4
AM
2049 static inline Status
2050 overflowed(Address value, Overflow_check overflow)
dd93cd0a 2051 {
f4baf0d4 2052 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
2053 {
2054 if (has_overflow_signed<valsize>(value))
f4baf0d4 2055 return STATUS_OVERFLOW;
dd93cd0a 2056 }
b80eed39
AM
2057 else if (overflow == CHECK_UNSIGNED)
2058 {
2059 if (has_overflow_unsigned<valsize>(value))
2060 return STATUS_OVERFLOW;
2061 }
f4baf0d4 2062 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
2063 {
2064 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 2065 return STATUS_OVERFLOW;
dd93cd0a 2066 }
f4baf0d4 2067 return STATUS_OK;
dd93cd0a
AM
2068 }
2069
cf43a2fe 2070 // Do a simple RELA relocation
0cfb0717 2071 template<int fieldsize, int valsize>
f4baf0d4
AM
2072 static inline Status
2073 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2074 {
0cfb0717 2075 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 2076 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2077 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
2078 return overflowed<valsize>(value, overflow);
2079 }
2080
0cfb0717 2081 template<int fieldsize, int valsize>
f4baf0d4 2082 static inline Status
42cacb20
DE
2083 rela(unsigned char* view,
2084 unsigned int right_shift,
0cfb0717 2085 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2086 Address value,
f4baf0d4 2087 Overflow_check overflow)
42cacb20 2088 {
0cfb0717 2089 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 2090 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2091 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
6a010cf6
AM
2092 if (overflow == CHECK_SIGNED)
2093 value = static_cast<SignedAddress>(value) >> right_shift;
2094 else
2095 value = value >> right_shift;
2096 Valtype reloc = value;
42cacb20
DE
2097 val &= ~dst_mask;
2098 reloc &= dst_mask;
0cfb0717 2099 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
6a010cf6 2100 return overflowed<valsize>(value, overflow);
42cacb20
DE
2101 }
2102
cf43a2fe 2103 // Do a simple RELA relocation, unaligned.
0cfb0717 2104 template<int fieldsize, int valsize>
f4baf0d4
AM
2105 static inline Status
2106 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2107 {
0cfb0717 2108 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
2109 return overflowed<valsize>(value, overflow);
2110 }
2111
0cfb0717 2112 template<int fieldsize, int valsize>
f4baf0d4 2113 static inline Status
cf43a2fe
AM
2114 rela_ua(unsigned char* view,
2115 unsigned int right_shift,
0cfb0717 2116 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2117 Address value,
f4baf0d4 2118 Overflow_check overflow)
42cacb20 2119 {
0cfb0717 2120 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 2121 Valtype;
0cfb0717 2122 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
6a010cf6
AM
2123 if (overflow == CHECK_SIGNED)
2124 value = static_cast<SignedAddress>(value) >> right_shift;
2125 else
2126 value = value >> right_shift;
2127 Valtype reloc = value;
42cacb20
DE
2128 val &= ~dst_mask;
2129 reloc &= dst_mask;
0cfb0717 2130 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
6a010cf6 2131 return overflowed<valsize>(value, overflow);
42cacb20
DE
2132 }
2133
42cacb20 2134public:
dd93cd0a 2135 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 2136 static inline void
dd93cd0a 2137 addr64(unsigned char* view, Address value)
0cfb0717 2138 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 2139
dd93cd0a 2140 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 2141 static inline void
dd93cd0a 2142 addr64_u(unsigned char* view, Address value)
0cfb0717 2143 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
2144
2145 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
2146 static inline Status
2147 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2148 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
2149
2150 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
2151 static inline Status
2152 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2153 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
2154
2155 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
2156 static inline Status
2157 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2158 {
0cfb0717
AM
2159 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2160 value, overflow);
f4baf0d4
AM
2161 if (overflow != CHECK_NONE && (value & 3) != 0)
2162 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2163 return stat;
2164 }
42cacb20
DE
2165
2166 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
2167 static inline Status
2168 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2169 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 2170
dd93cd0a 2171 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
2172 static inline Status
2173 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2174 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 2175
dd93cd0a 2176 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2177 static inline Status
2178 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2179 {
0cfb0717 2180 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 2181 if ((value & 3) != 0)
f4baf0d4 2182 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2183 return stat;
2184 }
42cacb20 2185
a680de9a
PB
2186 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2187 static inline Status
2188 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2189 {
2190 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2191 if ((value & 15) != 0)
2192 stat = STATUS_OVERFLOW;
2193 return stat;
2194 }
2195
42cacb20
DE
2196 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2197 static inline void
dd93cd0a 2198 addr16_hi(unsigned char* view, Address value)
0cfb0717 2199 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 2200
c9269dff 2201 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 2202 static inline void
dd93cd0a
AM
2203 addr16_ha(unsigned char* view, Address value)
2204 { This::addr16_hi(view, value + 0x8000); }
42cacb20 2205
dd93cd0a 2206 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 2207 static inline void
dd93cd0a 2208 addr16_hi2(unsigned char* view, Address value)
0cfb0717 2209 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 2210
dd93cd0a 2211 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 2212 static inline void
dd93cd0a
AM
2213 addr16_ha2(unsigned char* view, Address value)
2214 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 2215
dd93cd0a 2216 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 2217 static inline void
dd93cd0a 2218 addr16_hi3(unsigned char* view, Address value)
0cfb0717 2219 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 2220
dd93cd0a 2221 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 2222 static inline void
dd93cd0a
AM
2223 addr16_ha3(unsigned char* view, Address value)
2224 { This::addr16_hi3(view, value + 0x8000); }
2225
2226 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2227 static inline Status
2228 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2229 {
0cfb0717 2230 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
2231 if (overflow != CHECK_NONE && (value & 3) != 0)
2232 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2233 return stat;
2234 }
a680de9a
PB
2235
2236 // R_POWERPC_REL16DX_HA
2237 static inline Status
2238 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2239 {
2240 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2241 Valtype* wv = reinterpret_cast<Valtype*>(view);
2242 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2243 value += 0x8000;
2244 value = static_cast<SignedAddress>(value) >> 16;
2245 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2246 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2247 return overflowed<16>(value, overflow);
2248 }
e4dff765
AM
2249
2250 // R_PPC64_D34
2251 static inline Status
2252 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2253 {
2254 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2255 value, overflow);
2256 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2257 return stat;
2258 }
2259
2260 // R_PPC64_D34_HI30
2261 static inline void
2262 addr34_hi(unsigned char *view, uint64_t value)
2263 { This::addr34(view, value >> 34, CHECK_NONE);}
2264
2265 // R_PPC64_D34_HA30
2266 static inline void
2267 addr34_ha(unsigned char *view, uint64_t value)
2268 { This::addr34_hi(view, value + (1ULL << 33));}
2269
2270 // R_PPC64_D28
2271 static inline Status
2272 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2273 {
2274 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2275 value, overflow);
2276 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2277 return stat;
2278 }
2279
2280 // R_PPC64_ADDR16_HIGHER34
2281 static inline void
2282 addr16_higher34(unsigned char* view, uint64_t value)
2283 { This::addr16(view, value >> 34, CHECK_NONE); }
2284
2285 // R_PPC64_ADDR16_HIGHERA34
2286 static inline void
2287 addr16_highera34(unsigned char* view, uint64_t value)
2288 { This::addr16_higher34(view, value + (1ULL << 33)); }
2289
2290 // R_PPC64_ADDR16_HIGHEST34
2291 static inline void
2292 addr16_highest34(unsigned char* view, uint64_t value)
2293 { This::addr16(view, value >> 50, CHECK_NONE); }
2294
2295 // R_PPC64_ADDR16_HIGHESTA34
2296 static inline void
2297 addr16_highesta34(unsigned char* view, uint64_t value)
2298 { This::addr16_highest34(view, value + (1ULL << 33)); }
cf43a2fe
AM
2299};
2300
b4f7960d
AM
2301// Set ABI version for input and output.
2302
2303template<int size, bool big_endian>
2304void
2305Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2306{
2307 this->e_flags_ |= ver;
2308 if (this->abiversion() != 0)
2309 {
2310 Target_powerpc<size, big_endian>* target =
2311 static_cast<Target_powerpc<size, big_endian>*>(
2312 parameters->sized_target<size, big_endian>());
2313 if (target->abiversion() == 0)
2314 target->set_abiversion(this->abiversion());
2315 else if (target->abiversion() != this->abiversion())
2316 gold_error(_("%s: ABI version %d is not compatible "
2317 "with ABI version %d output"),
2318 this->name().c_str(),
2319 this->abiversion(), target->abiversion());
2320
2321 }
2322}
2323
5edad15d
AM
2324// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2325// relocatable object, if such sections exists.
cf43a2fe
AM
2326
2327template<int size, bool big_endian>
2328bool
2329Powerpc_relobj<size, big_endian>::do_find_special_sections(
2330 Read_symbols_data* sd)
2331{
c9269dff
AM
2332 const unsigned char* const pshdrs = sd->section_headers->data();
2333 const unsigned char* namesu = sd->section_names->data();
2334 const char* names = reinterpret_cast<const char*>(namesu);
2335 section_size_type names_size = sd->section_names_size;
2336 const unsigned char* s;
2337
dc3714f3
AM
2338 s = this->template find_shdr<size, big_endian>(pshdrs,
2339 size == 32 ? ".got2" : ".opd",
2340 names, names_size, NULL);
c9269dff
AM
2341 if (s != NULL)
2342 {
2343 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2344 this->special_ = ndx;
b4f7960d
AM
2345 if (size == 64)
2346 {
2347 if (this->abiversion() == 0)
2348 this->set_abiversion(1);
2349 else if (this->abiversion() > 1)
2350 gold_error(_("%s: .opd invalid in abiv%d"),
2351 this->name().c_str(), this->abiversion());
2352 }
c9269dff 2353 }
5edad15d
AM
2354 if (size == 64)
2355 {
2356 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2357 names, names_size, NULL);
2358 if (s != NULL)
2359 {
2360 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2361 this->relatoc_ = ndx;
2362 typename elfcpp::Shdr<size, big_endian> shdr(s);
2363 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2364 }
2365 }
c9269dff
AM
2366 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2367}
2368
2369// Examine .rela.opd to build info about function entry points.
2370
2371template<int size, bool big_endian>
2372void
2373Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2374 size_t reloc_count,
2375 const unsigned char* prelocs,
2376 const unsigned char* plocal_syms)
2377{
2378 if (size == 64)
cf43a2fe 2379 {
0e123f69
AM
2380 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2381 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2382 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2383 Address expected_off = 0;
2384 bool regular = true;
2385 unsigned int opd_ent_size = 0;
c9269dff
AM
2386
2387 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2388 {
c9269dff
AM
2389 Reltype reloc(prelocs);
2390 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2391 = reloc.get_r_info();
2392 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2393 if (r_type == elfcpp::R_PPC64_ADDR64)
2394 {
2395 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2396 typename elfcpp::Elf_types<size>::Elf_Addr value;
2397 bool is_ordinary;
2398 unsigned int shndx;
2399 if (r_sym < this->local_symbol_count())
2400 {
2401 typename elfcpp::Sym<size, big_endian>
2402 lsym(plocal_syms + r_sym * sym_size);
2403 shndx = lsym.get_st_shndx();
2404 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2405 value = lsym.get_st_value();
2406 }
2407 else
2408 shndx = this->symbol_section_and_value(r_sym, &value,
2409 &is_ordinary);
2410 this->set_opd_ent(reloc.get_r_offset(), shndx,
2411 value + reloc.get_r_addend());
ec4dbad3
AM
2412 if (i == 2)
2413 {
2414 expected_off = reloc.get_r_offset();
2415 opd_ent_size = expected_off;
2416 }
2417 else if (expected_off != reloc.get_r_offset())
2418 regular = false;
2419 expected_off += opd_ent_size;
2420 }
2421 else if (r_type == elfcpp::R_PPC64_TOC)
2422 {
2423 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2424 regular = false;
2425 }
2426 else
2427 {
2428 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2429 this->name().c_str(), r_type);
2430 regular = false;
c9269dff
AM
2431 }
2432 }
ec4dbad3
AM
2433 if (reloc_count <= 2)
2434 opd_ent_size = this->section_size(this->opd_shndx());
2435 if (opd_ent_size != 24 && opd_ent_size != 16)
2436 regular = false;
2437 if (!regular)
2438 {
2439 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2440 this->name().c_str());
2441 opd_ent_size = 0;
2442 }
c9269dff
AM
2443 }
2444}
2445
5edad15d
AM
2446// Returns true if a code sequence loading the TOC entry at VALUE
2447// relative to the TOC pointer can be converted into code calculating
2448// a TOC pointer relative offset.
2449// If so, the TOC pointer relative offset is stored to VALUE.
2450
2451template<int size, bool big_endian>
2452bool
2453Powerpc_relobj<size, big_endian>::make_toc_relative(
2454 Target_powerpc<size, big_endian>* target,
2455 Address* value)
2456{
2457 if (size != 64)
2458 return false;
2459
e666304e
AM
2460 // With -mcmodel=medium code it is quite possible to have
2461 // toc-relative relocs referring to objects outside the TOC.
2462 // Don't try to look at a non-existent TOC.
a19da04b
AM
2463 if (this->toc_shndx() == 0
2464 || this->output_section(this->toc_shndx()) == 0)
e666304e
AM
2465 return false;
2466
5edad15d
AM
2467 // Convert VALUE back to an address by adding got_base (see below),
2468 // then to an offset in the TOC by subtracting the TOC output
a19da04b
AM
2469 // section address and the TOC output offset.
2470 Address off = (*value + target->toc_pointer()
2471 - this->output_section(this->toc_shndx())->address()
5edad15d
AM
2472 - this->output_section_offset(this->toc_shndx()));
2473 // Is this offset in the TOC? -mcmodel=medium code may be using
2474 // TOC relative access to variables outside the TOC. Those of
2475 // course can't be optimized. We also don't try to optimize code
2476 // that is using a different object's TOC.
2477 if (off >= this->section_size(this->toc_shndx()))
2478 return false;
2479
2480 if (this->no_toc_opt(off))
2481 return false;
2482
2483 section_size_type vlen;
2484 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2485 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2486 // The TOC pointer
a19da04b 2487 Address got_base = target->toc_pointer();
5edad15d 2488 addr -= got_base;
857e829e 2489 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2490 return false;
2491
2492 *value = addr;
2493 return true;
2494}
2495
c9b8abb7
AM
2496template<int size, bool big_endian>
2497bool
2498Powerpc_relobj<size, big_endian>::make_got_relative(
2499 Target_powerpc<size, big_endian>* target,
2500 const Symbol_value<size>* psymval,
2501 Address addend,
2502 Address* value)
2503{
2504 Address addr = psymval->value(this, addend);
a19da04b 2505 Address got_base = target->toc_pointer();
c9b8abb7
AM
2506 addr -= got_base;
2507 if (addr + 0x80008000 > 0xffffffff)
2508 return false;
2509
2510 *value = addr;
2511 return true;
2512}
2513
5edad15d
AM
2514// Perform the Sized_relobj_file method, then set up opd info from
2515// .opd relocs.
2516
c9269dff
AM
2517template<int size, bool big_endian>
2518void
2519Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2520{
2521 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2522 if (size == 64)
2523 {
2524 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2525 p != rd->relocs.end();
2526 ++p)
2527 {
2528 if (p->data_shndx == this->opd_shndx())
2529 {
ec4dbad3
AM
2530 uint64_t opd_size = this->section_size(this->opd_shndx());
2531 gold_assert(opd_size == static_cast<size_t>(opd_size));
2532 if (opd_size != 0)
2533 {
2534 this->init_opd(opd_size);
2535 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2536 rd->local_symbols->data());
2537 }
c9269dff
AM
2538 break;
2539 }
cf43a2fe
AM
2540 }
2541 }
cf43a2fe
AM
2542}
2543
b4f7960d
AM
2544// Read the symbols then set up st_other vector.
2545
2546template<int size, bool big_endian>
2547void
2548Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2549{
f35c4853 2550 this->base_read_symbols(sd);
724436fc
AM
2551 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2552 return;
b4f7960d
AM
2553 if (size == 64)
2554 {
2555 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2556 const unsigned char* const pshdrs = sd->section_headers->data();
2557 const unsigned int loccount = this->do_local_symbol_count();
2558 if (loccount != 0)
2559 {
2560 this->st_other_.resize(loccount);
2561 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2562 off_t locsize = loccount * sym_size;
2563 const unsigned int symtab_shndx = this->symtab_shndx();
2564 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2565 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2566 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2567 locsize, true, false);
2568 psyms += sym_size;
2569 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2570 {
2571 elfcpp::Sym<size, big_endian> sym(psyms);
2572 unsigned char st_other = sym.get_st_other();
2573 this->st_other_[i] = st_other;
2574 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2575 {
2576 if (this->abiversion() == 0)
2577 this->set_abiversion(2);
2578 else if (this->abiversion() < 2)
2579 gold_error(_("%s: local symbol %d has invalid st_other"
2580 " for ABI version 1"),
2581 this->name().c_str(), i);
2582 }
2583 }
2584 }
2585 }
724436fc
AM
2586
2587 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2588 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2589 bool merge_attributes = false;
2590 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2591 {
2592 elfcpp::Shdr<size, big_endian> shdr(ps);
2593 switch (shdr.get_sh_type())
2594 {
2595 case elfcpp::SHT_GNU_ATTRIBUTES:
2596 {
2597 gold_assert(this->attributes_section_data_ == NULL);
2598 section_offset_type section_offset = shdr.get_sh_offset();
2599 section_size_type section_size =
2600 convert_to_section_size_type(shdr.get_sh_size());
2601 const unsigned char* view =
2602 this->get_view(section_offset, section_size, true, false);
2603 this->attributes_section_data_ =
2604 new Attributes_section_data(view, section_size);
2605 }
2606 break;
2607
2608 case elfcpp::SHT_SYMTAB:
2609 {
2610 // Sometimes an object has no contents except the section
2611 // name string table and an empty symbol table with the
2612 // undefined symbol. We don't want to merge
2613 // processor-specific flags from such an object.
2614 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2615 elfcpp::Elf_sizes<size>::sym_size;
2616 if (shdr.get_sh_size() > sym_size)
2617 merge_attributes = true;
2618 }
2619 break;
2620
2621 case elfcpp::SHT_STRTAB:
2622 break;
2623
2624 default:
2625 merge_attributes = true;
2626 break;
2627 }
2628 }
2629
2630 if (!merge_attributes)
2631 {
2632 // Should rarely happen.
2633 delete this->attributes_section_data_;
2634 this->attributes_section_data_ = NULL;
2635 }
b4f7960d
AM
2636}
2637
2638template<int size, bool big_endian>
2639void
2640Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2641{
2642 this->e_flags_ |= ver;
2643 if (this->abiversion() != 0)
2644 {
2645 Target_powerpc<size, big_endian>* target =
2646 static_cast<Target_powerpc<size, big_endian>*>(
2647 parameters->sized_target<size, big_endian>());
2648 if (target->abiversion() == 0)
2649 target->set_abiversion(this->abiversion());
2650 else if (target->abiversion() != this->abiversion())
2651 gold_error(_("%s: ABI version %d is not compatible "
2652 "with ABI version %d output"),
2653 this->name().c_str(),
2654 this->abiversion(), target->abiversion());
2655
2656 }
2657}
2658
f35c4853 2659// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2660// read .opd from a dynamic object, filling in opd_ent_ vector,
2661
2662template<int size, bool big_endian>
2663void
2664Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2665{
f35c4853 2666 this->base_read_symbols(sd);
724436fc
AM
2667 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2668 const unsigned char* ps =
2669 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2670 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2671 {
2672 elfcpp::Shdr<size, big_endian> shdr(ps);
2673 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2674 {
2675 section_offset_type section_offset = shdr.get_sh_offset();
2676 section_size_type section_size =
2677 convert_to_section_size_type(shdr.get_sh_size());
2678 const unsigned char* view =
2679 this->get_view(section_offset, section_size, true, false);
2680 this->attributes_section_data_ =
2681 new Attributes_section_data(view, section_size);
2682 break;
2683 }
2684 }
dc3714f3
AM
2685 if (size == 64)
2686 {
dc3714f3
AM
2687 const unsigned char* const pshdrs = sd->section_headers->data();
2688 const unsigned char* namesu = sd->section_names->data();
2689 const char* names = reinterpret_cast<const char*>(namesu);
2690 const unsigned char* s = NULL;
2691 const unsigned char* opd;
2692 section_size_type opd_size;
2693
2694 // Find and read .opd section.
2695 while (1)
2696 {
2697 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2698 sd->section_names_size,
2699 s);
2700 if (s == NULL)
2701 return;
2702
2703 typename elfcpp::Shdr<size, big_endian> shdr(s);
2704 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2705 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2706 {
b4f7960d
AM
2707 if (this->abiversion() == 0)
2708 this->set_abiversion(1);
2709 else if (this->abiversion() > 1)
2710 gold_error(_("%s: .opd invalid in abiv%d"),
2711 this->name().c_str(), this->abiversion());
2712
dc3714f3
AM
2713 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2714 this->opd_address_ = shdr.get_sh_addr();
2715 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2716 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2717 true, false);
2718 break;
2719 }
2720 }
2721
2722 // Build set of executable sections.
2723 // Using a set is probably overkill. There is likely to be only
2724 // a few executable sections, typically .init, .text and .fini,
2725 // and they are generally grouped together.
2726 typedef std::set<Sec_info> Exec_sections;
2727 Exec_sections exec_sections;
2728 s = pshdrs;
2729 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2730 {
2731 typename elfcpp::Shdr<size, big_endian> shdr(s);
2732 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2733 && ((shdr.get_sh_flags()
2734 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2735 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2736 && shdr.get_sh_size() != 0)
2737 {
2738 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2739 shdr.get_sh_size(), i));
2740 }
2741 }
2742 if (exec_sections.empty())
2743 return;
2744
2745 // Look over the OPD entries. This is complicated by the fact
2746 // that some binaries will use two-word entries while others
2747 // will use the standard three-word entries. In most cases
2748 // the third word (the environment pointer for languages like
2749 // Pascal) is unused and will be zero. If the third word is
2750 // used it should not be pointing into executable sections,
2751 // I think.
2752 this->init_opd(opd_size);
2753 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2754 {
2755 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2756 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2757 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2758 if (val == 0)
2759 // Chances are that this is the third word of an OPD entry.
2760 continue;
2761 typename Exec_sections::const_iterator e
2762 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2763 if (e != exec_sections.begin())
2764 {
2765 --e;
2766 if (e->start <= val && val < e->start + e->len)
2767 {
2768 // We have an address in an executable section.
2769 // VAL ought to be the function entry, set it up.
2770 this->set_opd_ent(p - opd, e->shndx, val);
2771 // Skip second word of OPD entry, the TOC pointer.
2772 p += 8;
2773 }
2774 }
2775 // If we didn't match any executable sections, we likely
2776 // have a non-zero third word in the OPD entry.
2777 }
2778 }
2779}
2780
5edad15d
AM
2781// Relocate sections.
2782
2783template<int size, bool big_endian>
2784void
2785Powerpc_relobj<size, big_endian>::do_relocate_sections(
2786 const Symbol_table* symtab, const Layout* layout,
2787 const unsigned char* pshdrs, Output_file* of,
2788 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2789{
2790 unsigned int start = 1;
2791 if (size == 64
2792 && this->relatoc_ != 0
2793 && !parameters->options().relocatable())
2794 {
2795 // Relocate .toc first.
2796 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2797 this->relatoc_, this->relatoc_);
2798 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2799 1, this->relatoc_ - 1);
2800 start = this->relatoc_ + 1;
2801 }
2802 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2803 start, this->shnum() - 1);
2804}
2805
f43ba157 2806// Set up some symbols.
26a4e9cb
AM
2807
2808template<int size, bool big_endian>
2809void
f43ba157
AM
2810Target_powerpc<size, big_endian>::do_define_standard_symbols(
2811 Symbol_table* symtab,
2812 Layout* layout)
26a4e9cb
AM
2813{
2814 if (size == 32)
2815 {
bb66a627
AM
2816 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2817 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2818 // non-relative dynamic relocs). The proper value will be
2819 // updated later.
2820 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2821 if (gotsym != NULL && gotsym->is_undefined())
2822 {
2823 Target_powerpc<size, big_endian>* target =
2824 static_cast<Target_powerpc<size, big_endian>*>(
2825 parameters->sized_target<size, big_endian>());
2826 Output_data_got_powerpc<size, big_endian>* got
f19c3684 2827 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
26a4e9cb
AM
2828 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2829 Symbol_table::PREDEFINED,
2830 got, 0, 0,
2831 elfcpp::STT_OBJECT,
bb66a627 2832 elfcpp::STB_LOCAL,
26a4e9cb
AM
2833 elfcpp::STV_HIDDEN, 0,
2834 false, false);
2835 }
2836
2837 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2838 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2839 if (sdasym != NULL && sdasym->is_undefined())
2840 {
2841 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2842 Output_section* os
2843 = layout->add_output_section_data(".sdata", 0,
2844 elfcpp::SHF_ALLOC
2845 | elfcpp::SHF_WRITE,
2846 sdata, ORDER_SMALL_DATA, false);
2847 symtab->define_in_output_data("_SDA_BASE_", NULL,
2848 Symbol_table::PREDEFINED,
2849 os, 32768, 0, elfcpp::STT_OBJECT,
2850 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2851 0, false, false);
2852 }
2853 }
b4f7960d
AM
2854 else
2855 {
2856 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2857 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2858 if (gotsym != NULL && gotsym->is_undefined())
2859 {
2860 Target_powerpc<size, big_endian>* target =
2861 static_cast<Target_powerpc<size, big_endian>*>(
2862 parameters->sized_target<size, big_endian>());
2863 Output_data_got_powerpc<size, big_endian>* got
f19c3684 2864 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
b4f7960d
AM
2865 symtab->define_in_output_data(".TOC.", NULL,
2866 Symbol_table::PREDEFINED,
2867 got, 0x8000, 0,
2868 elfcpp::STT_OBJECT,
2869 elfcpp::STB_LOCAL,
2870 elfcpp::STV_HIDDEN, 0,
2871 false, false);
2872 }
2873 }
34e0882b
AM
2874
2875 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2876 if (parameters->options().tls_get_addr_optimize()
2877 && this->tls_get_addr_ != NULL
2878 && this->tls_get_addr_->in_reg())
2879 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2880 if (this->tls_get_addr_opt_ != NULL)
2881 {
2882 if (this->tls_get_addr_->is_undefined()
2883 || this->tls_get_addr_->is_from_dynobj())
2884 {
2885 // Make it seem as if references to __tls_get_addr are
2886 // really to __tls_get_addr_opt, so the latter symbol is
2887 // made dynamic, not the former.
2888 this->tls_get_addr_->clear_in_reg();
2889 this->tls_get_addr_opt_->set_in_reg();
2890 }
2891 // We have a non-dynamic definition for __tls_get_addr.
2892 // Make __tls_get_addr_opt the same, if it does not already have
2893 // a non-dynamic definition.
2894 else if (this->tls_get_addr_opt_->is_undefined()
2895 || this->tls_get_addr_opt_->is_from_dynobj())
2896 {
2897 Sized_symbol<size>* from
2898 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2899 Sized_symbol<size>* to
2900 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2901 symtab->clone<size>(to, from);
2902 }
2903 }
26a4e9cb
AM
2904}
2905
cf43a2fe
AM
2906// Set up PowerPC target specific relobj.
2907
2908template<int size, bool big_endian>
2909Object*
2910Target_powerpc<size, big_endian>::do_make_elf_object(
2911 const std::string& name,
2912 Input_file* input_file,
2913 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2914{
2915 int et = ehdr.get_e_type();
957564c9
AS
2916 // ET_EXEC files are valid input for --just-symbols/-R,
2917 // and we treat them as relocatable objects.
2918 if (et == elfcpp::ET_REL
2919 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2920 {
2921 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2922 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2923 obj->setup();
2924 return obj;
2925 }
2926 else if (et == elfcpp::ET_DYN)
2927 {
dc3714f3
AM
2928 Powerpc_dynobj<size, big_endian>* obj =
2929 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2930 obj->setup();
2931 return obj;
2932 }
2933 else
2934 {
c9269dff 2935 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2936 return NULL;
2937 }
2938}
2939
2940template<int size, bool big_endian>
2941class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2942{
2943public:
2944 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2945 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2946
f19c3684
AM
2947 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2948 Got_type got_type)
cf43a2fe
AM
2949 : Output_data_got<size, big_endian>(),
2950 symtab_(symtab), layout_(layout),
2951 header_ent_cnt_(size == 32 ? 3 : 1),
a19da04b 2952 header_index_(size == 32 ? 0x2000 : -1u)
751e4d66
AM
2953 {
2954 if (size == 64)
2955 this->set_addralign(256);
f19c3684 2956 if (size == 64 && (got_type & GOT_TYPE_SMALL))
a19da04b 2957 this->make_header();
751e4d66 2958 }
cf43a2fe 2959
e84fe78f
AM
2960 // Override all the Output_data_got methods we use so as to first call
2961 // reserve_ent().
2962 bool
5cdb4f14 2963 add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
e84fe78f
AM
2964 {
2965 this->reserve_ent();
5cdb4f14
AM
2966 return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2967 addend);
e84fe78f
AM
2968 }
2969
2970 bool
5cdb4f14 2971 add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
e84fe78f
AM
2972 {
2973 this->reserve_ent();
5cdb4f14
AM
2974 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2975 addend);
e84fe78f
AM
2976 }
2977
2978 bool
5cdb4f14
AM
2979 add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2980 { return this->add_global_plt(gsym, got_type, addend); }
e84fe78f
AM
2981
2982 void
2983 add_global_with_rel(Symbol* gsym, unsigned int got_type,
5cdb4f14
AM
2984 Output_data_reloc_generic* rel_dyn,
2985 unsigned int r_type, uint64_t addend)
e84fe78f
AM
2986 {
2987 this->reserve_ent();
2988 Output_data_got<size, big_endian>::
5cdb4f14 2989 add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
e84fe78f
AM
2990 }
2991
2992 void
2993 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2994 Output_data_reloc_generic* rel_dyn,
5cdb4f14
AM
2995 unsigned int r_type_1, unsigned int r_type_2,
2996 uint64_t addend)
e84fe78f 2997 {
aacb3b6d
AM
2998 if (gsym->has_got_offset(got_type))
2999 return;
3000
e84fe78f
AM
3001 this->reserve_ent(2);
3002 Output_data_got<size, big_endian>::
5cdb4f14
AM
3003 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
3004 addend);
e84fe78f
AM
3005 }
3006
3007 bool
5cdb4f14
AM
3008 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
3009 uint64_t addend)
e84fe78f
AM
3010 {
3011 this->reserve_ent();
3012 return Output_data_got<size, big_endian>::add_local(object, sym_index,
5cdb4f14 3013 got_type, addend);
e84fe78f
AM
3014 }
3015
3016 bool
5cdb4f14
AM
3017 add_local_plt(Relobj* object, unsigned int sym_index,
3018 unsigned int got_type, uint64_t addend)
e84fe78f
AM
3019 {
3020 this->reserve_ent();
3021 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
5cdb4f14 3022 got_type, addend);
e84fe78f
AM
3023 }
3024
3025 bool
5cdb4f14
AM
3026 add_local_tls(Relobj* object, unsigned int sym_index,
3027 unsigned int got_type, uint64_t addend)
3028 { return this->add_local_plt(object, sym_index, got_type, addend); }
e84fe78f
AM
3029
3030 void
3031 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3032 unsigned int got_type,
3033 Output_data_reloc_generic* rel_dyn,
5cdb4f14 3034 unsigned int r_type, uint64_t addend)
e84fe78f 3035 {
5cdb4f14 3036 if (object->local_has_got_offset(sym_index, got_type, addend))
aacb3b6d
AM
3037 return;
3038
e84fe78f
AM
3039 this->reserve_ent(2);
3040 Output_data_got<size, big_endian>::
5cdb4f14 3041 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
e84fe78f
AM
3042 }
3043
3044 unsigned int
3045 add_constant(Valtype constant)
3046 {
3047 this->reserve_ent();
3048 return Output_data_got<size, big_endian>::add_constant(constant);
3049 }
3050
dd93cd0a
AM
3051 unsigned int
3052 add_constant_pair(Valtype c1, Valtype c2)
3053 {
3054 this->reserve_ent(2);
e84fe78f 3055 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
3056 }
3057
a19da04b 3058 // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
cf43a2fe
AM
3059 unsigned int
3060 g_o_t() const
dd93cd0a
AM
3061 {
3062 if (size == 32)
a19da04b 3063 return this->got_offset(this->header_index_);
f19c3684 3064 else if (this->header_index_ != -1u)
a19da04b 3065 return this->got_offset(this->header_index_) + 0x8000;
f19c3684
AM
3066 else
3067 gold_unreachable();
dd93cd0a
AM
3068 }
3069
cf43a2fe
AM
3070 // Ensure our GOT has a header.
3071 void
3072 set_final_data_size()
3073 {
a19da04b 3074 if (size == 32 && this->header_ent_cnt_ != 0)
cf43a2fe
AM
3075 this->make_header();
3076 Output_data_got<size, big_endian>::set_final_data_size();
3077 }
3078
3079 // First word of GOT header needs some values that are not
3080 // handled by Output_data_got so poke them in here.
3081 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3082 void
3083 do_write(Output_file* of)
3084 {
f19c3684
AM
3085 if (this->header_index_ != -1u)
3086 {
3087 Valtype val = 0;
3088 if (size == 32 && this->layout_->dynamic_data() != NULL)
3089 val = this->layout_->dynamic_section()->address();
3090 if (size == 64)
3091 val = this->address() + this->g_o_t();
3092 this->replace_constant(this->header_index_, val);
3093 }
cf43a2fe
AM
3094 Output_data_got<size, big_endian>::do_write(of);
3095 }
3096
3097private:
3098 void
3099 reserve_ent(unsigned int cnt = 1)
3100 {
a19da04b 3101 if (size != 32 || this->header_ent_cnt_ == 0)
cf43a2fe
AM
3102 return;
3103 if (this->num_entries() + cnt > this->header_index_)
3104 this->make_header();
3105 }
3106
3107 void
3108 make_header()
3109 {
3110 this->header_ent_cnt_ = 0;
3111 this->header_index_ = this->num_entries();
3112 if (size == 32)
3113 {
3114 Output_data_got<size, big_endian>::add_constant(0);
3115 Output_data_got<size, big_endian>::add_constant(0);
3116 Output_data_got<size, big_endian>::add_constant(0);
3117
3118 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
3119 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3120 if (gotsym != NULL)
3121 {
3122 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3123 sym->set_value(this->g_o_t());
3124 }
3125 else
3126 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3127 Symbol_table::PREDEFINED,
3128 this, this->g_o_t(), 0,
3129 elfcpp::STT_OBJECT,
3130 elfcpp::STB_LOCAL,
3131 elfcpp::STV_HIDDEN, 0,
3132 false, false);
cf43a2fe
AM
3133 }
3134 else
3135 Output_data_got<size, big_endian>::add_constant(0);
3136 }
3137
3138 // Stashed pointers.
3139 Symbol_table* symtab_;
3140 Layout* layout_;
3141
3142 // GOT header size.
3143 unsigned int header_ent_cnt_;
3144 // GOT header index.
3145 unsigned int header_index_;
42cacb20
DE
3146};
3147
3148// Get the GOT section, creating it if necessary.
3149
3150template<int size, bool big_endian>
cf43a2fe 3151Output_data_got_powerpc<size, big_endian>*
42cacb20 3152Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
f19c3684
AM
3153 Layout* layout,
3154 Got_type got_type)
42cacb20
DE
3155{
3156 if (this->got_ == NULL)
3157 {
3158 gold_assert(symtab != NULL && layout != NULL);
3159
cf43a2fe 3160 this->got_
f19c3684
AM
3161 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3162 GOT_TYPE_SMALL);
42cacb20
DE
3163
3164 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3165 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 3166 this->got_, ORDER_DATA, false);
42cacb20
DE
3167 }
3168
f19c3684
AM
3169 if (size == 32 || (got_type & GOT_TYPE_SMALL))
3170 return this->got_;
3171
3172 if (this->biggot_ == NULL)
3173 {
3174 this->biggot_
3175 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3176 GOT_TYPE_STANDARD);
3177
3178 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3179 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3180 this->biggot_, ORDER_DATA, false);
3181 }
3182
3183 return this->biggot_;
42cacb20
DE
3184}
3185
3186// Get the dynamic reloc section, creating it if necessary.
3187
3188template<int size, bool big_endian>
3189typename Target_powerpc<size, big_endian>::Reloc_section*
3190Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3191{
3192 if (this->rela_dyn_ == NULL)
3193 {
3194 gold_assert(layout != NULL);
3195 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3196 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
3197 elfcpp::SHF_ALLOC, this->rela_dyn_,
3198 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
3199 }
3200 return this->rela_dyn_;
3201}
3202
b3ccdeb5
AM
3203// Similarly, but for ifunc symbols get the one for ifunc.
3204
3205template<int size, bool big_endian>
3206typename Target_powerpc<size, big_endian>::Reloc_section*
3207Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3208 Layout* layout,
3209 bool for_ifunc)
3210{
3211 if (!for_ifunc)
3212 return this->rela_dyn_section(layout);
3213
3214 if (this->iplt_ == NULL)
3215 this->make_iplt_section(symtab, layout);
3216 return this->iplt_->rel_plt();
3217}
3218
ec661b9d
AM
3219class Stub_control
3220{
3221 public:
3222 // Determine the stub group size. The group size is the absolute
3223 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 3224 // is passed a negative value, we restrict stubs to be always after
ec661b9d 3225 // the stubbed branches.
1c3a5fbe
AM
3226 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3227 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3228 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3229 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3230 owner_(NULL), output_section_(NULL)
ec661b9d 3231 {
ec661b9d
AM
3232 }
3233
3234 // Return true iff input section can be handled by current stub
3235 // group.
3236 bool
3237 can_add_to_stub_group(Output_section* o,
3238 const Output_section::Input_section* i,
3239 bool has14);
3240
3241 const Output_section::Input_section*
3242 owner()
3243 { return owner_; }
3244
3245 Output_section*
3246 output_section()
3247 { return output_section_; }
3248
a20605cf
AM
3249 void
3250 set_output_and_owner(Output_section* o,
3251 const Output_section::Input_section* i)
3252 {
3253 this->output_section_ = o;
3254 this->owner_ = i;
3255 }
3256
ec661b9d
AM
3257 private:
3258 typedef enum
3259 {
1c3a5fbe 3260 // Initial state.
ec661b9d 3261 NO_GROUP,
1c3a5fbe 3262 // Adding group sections before the stubs.
ec661b9d 3263 FINDING_STUB_SECTION,
1c3a5fbe 3264 // Adding group sections after the stubs.
ec661b9d
AM
3265 HAS_STUB_SECTION
3266 } State;
3267
ec661b9d 3268 uint32_t stub_group_size_;
a5018ae5 3269 bool stubs_always_after_branch_;
ec661b9d 3270 bool suppress_size_errors_;
1c3a5fbe
AM
3271 // True if a stub group can serve multiple output sections.
3272 bool multi_os_;
3273 State state_;
8a37735f
AM
3274 // Current max size of group. Starts at stub_group_size_ but is
3275 // reduced to stub_group_size_/1024 on seeing a section with
3276 // external conditional branches.
3277 uint32_t group_size_;
a5018ae5 3278 uint64_t group_start_addr_;
57f6d32d
AM
3279 // owner_ and output_section_ specify the section to which stubs are
3280 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
3281 const Output_section::Input_section* owner_;
3282 Output_section* output_section_;
3283};
3284
0cfdc767 3285// Return true iff input section can be handled by current stub
a5018ae5
AM
3286// group. Sections are presented to this function in order,
3287// so the first section is the head of the group.
ec661b9d
AM
3288
3289bool
3290Stub_control::can_add_to_stub_group(Output_section* o,
3291 const Output_section::Input_section* i,
3292 bool has14)
3293{
ec661b9d
AM
3294 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3295 uint64_t this_size;
3296 uint64_t start_addr = o->address();
3297
3298 if (whole_sec)
3299 // .init and .fini sections are pasted together to form a single
3300 // function. We can't be adding stubs in the middle of the function.
3301 this_size = o->data_size();
3302 else
3303 {
3304 start_addr += i->relobj()->output_section_offset(i->shndx());
3305 this_size = i->data_size();
3306 }
57f6d32d 3307
a5018ae5 3308 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
3309 uint32_t group_size = this->stub_group_size_;
3310 if (has14)
3311 this->group_size_ = group_size = group_size >> 10;
ec661b9d 3312
57f6d32d 3313 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
3314 gold_warning(_("%s:%s exceeds group size"),
3315 i->relobj()->name().c_str(),
3316 i->relobj()->section_name(i->shndx()).c_str());
3317
afe002dd
AM
3318 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3319 has14 ? " 14bit" : "",
3320 i->relobj()->name().c_str(),
3321 i->relobj()->section_name(i->shndx()).c_str(),
3322 (long long) this_size,
a5018ae5
AM
3323 (this->state_ == NO_GROUP
3324 ? this_size
3325 : (long long) end_addr - this->group_start_addr_));
afe002dd 3326
1c3a5fbe
AM
3327 if (this->state_ == NO_GROUP)
3328 {
3329 // Only here on very first use of Stub_control
3330 this->owner_ = i;
3331 this->output_section_ = o;
3332 this->state_ = FINDING_STUB_SECTION;
3333 this->group_size_ = group_size;
3334 this->group_start_addr_ = start_addr;
3335 return true;
3336 }
3337 else if (!this->multi_os_ && this->output_section_ != o)
3338 ;
3339 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 3340 {
a5018ae5 3341 // Can we add this section, which is after the stubs, to the
57f6d32d 3342 // group?
a5018ae5 3343 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3344 return true;
ec661b9d 3345 }
a5018ae5 3346 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 3347 {
a5018ae5
AM
3348 if ((whole_sec && this->output_section_ == o)
3349 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3350 {
a5018ae5 3351 // Stubs are added at the end of "owner_".
57f6d32d
AM
3352 this->owner_ = i;
3353 this->output_section_ = o;
a5018ae5 3354 return true;
57f6d32d 3355 }
a5018ae5
AM
3356 // The group before the stubs has reached maximum size.
3357 // Now see about adding sections after the stubs to the
3358 // group. If the current section has a 14-bit branch and
3359 // the group before the stubs exceeds group_size_ (because
3360 // they didn't have 14-bit branches), don't add sections
3361 // after the stubs: The size of stubs for such a large
3362 // group may exceed the reach of a 14-bit branch.
3363 if (!this->stubs_always_after_branch_
3364 && this_size <= this->group_size_
3365 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3366 {
a5018ae5
AM
3367 gold_debug(DEBUG_TARGET, "adding after stubs");
3368 this->state_ = HAS_STUB_SECTION;
3369 this->group_start_addr_ = start_addr;
57f6d32d
AM
3370 return true;
3371 }
ec661b9d 3372 }
a5018ae5
AM
3373 else
3374 gold_unreachable();
57f6d32d 3375
1c3a5fbe
AM
3376 gold_debug(DEBUG_TARGET,
3377 !this->multi_os_ && this->output_section_ != o
3378 ? "nope, new output section\n"
3379 : "nope, didn't fit\n");
afe002dd 3380
57f6d32d
AM
3381 // The section fails to fit in the current group. Set up a few
3382 // things for the next group. owner_ and output_section_ will be
3383 // set later after we've retrieved those values for the current
3384 // group.
3385 this->state_ = FINDING_STUB_SECTION;
8a37735f 3386 this->group_size_ = group_size;
a5018ae5 3387 this->group_start_addr_ = start_addr;
57f6d32d 3388 return false;
ec661b9d
AM
3389}
3390
3391// Look over all the input sections, deciding where to place stubs.
3392
3393template<int size, bool big_endian>
3394void
3395Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
3396 const Task*,
3397 bool no_size_errors)
ec661b9d 3398{
1c3a5fbe
AM
3399 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3400 parameters->options().stub_group_multi());
ec661b9d
AM
3401
3402 // Group input sections and insert stub table
a3e60ddb
AM
3403 Stub_table_owner* table_owner = NULL;
3404 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
3405 Layout::Section_list section_list;
3406 layout->get_executable_sections(&section_list);
3407 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
3408 for (Layout::Section_list::iterator o = section_list.begin();
3409 o != section_list.end();
ec661b9d
AM
3410 ++o)
3411 {
3412 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
3413 for (Input_section_list::const_iterator i
3414 = (*o)->input_sections().begin();
3415 i != (*o)->input_sections().end();
ec661b9d
AM
3416 ++i)
3417 {
a3e60ddb
AM
3418 if (i->is_input_section()
3419 || i->is_relaxed_input_section())
ec661b9d
AM
3420 {
3421 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3422 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3423 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3424 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3425 {
a3e60ddb
AM
3426 table_owner->output_section = stub_control.output_section();
3427 table_owner->owner = stub_control.owner();
a20605cf 3428 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 3429 table_owner = NULL;
ec661b9d 3430 }
a3e60ddb
AM
3431 if (table_owner == NULL)
3432 {
3433 table_owner = new Stub_table_owner;
3434 tables.push_back(table_owner);
3435 }
3436 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
3437 }
3438 }
3439 }
a3e60ddb 3440 if (table_owner != NULL)
0cfdc767 3441 {
a5018ae5
AM
3442 table_owner->output_section = stub_control.output_section();
3443 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
3444 }
3445 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3446 t != tables.end();
3447 ++t)
3448 {
3449 Stub_table<size, big_endian>* stub_table;
3450
3451 if ((*t)->owner->is_input_section())
3452 stub_table = new Stub_table<size, big_endian>(this,
3453 (*t)->output_section,
590b87ff
AM
3454 (*t)->owner,
3455 this->stub_tables_.size());
a3e60ddb
AM
3456 else if ((*t)->owner->is_relaxed_input_section())
3457 stub_table = static_cast<Stub_table<size, big_endian>*>(
3458 (*t)->owner->relaxed_input_section());
0cfdc767 3459 else
a3e60ddb
AM
3460 gold_unreachable();
3461 this->stub_tables_.push_back(stub_table);
3462 delete *t;
0cfdc767 3463 }
ec661b9d
AM
3464}
3465
32f59844 3466template<int size>
a3e60ddb
AM
3467static unsigned long
3468max_branch_delta (unsigned int r_type)
3469{
3470 if (r_type == elfcpp::R_POWERPC_REL14
3471 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3472 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3473 return 1L << 15;
3474 if (r_type == elfcpp::R_POWERPC_REL24
32f59844 3475 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
eb97d024 3476 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
a3e60ddb
AM
3477 || r_type == elfcpp::R_PPC_PLTREL24
3478 || r_type == elfcpp::R_PPC_LOCAL24PC)
3479 return 1L << 25;
3480 return 0;
3481}
3482
7e57d19e
AM
3483// Return whether this branch is going via a plt call stub.
3484
3485template<int size, bool big_endian>
3486bool
3487Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3488 Powerpc_relobj<size, big_endian>* ppc_object,
3489 unsigned int shndx,
3490 Address offset,
3491 Target_powerpc* target,
3492 Symbol_table* symtab)
3493{
3494 if (this->object_ != ppc_object
3495 || this->shndx_ != shndx
3496 || this->offset_ != offset)
3497 return false;
3498
3499 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3500 if (sym != NULL && sym->is_forwarder())
3501 sym = symtab->resolve_forwards(sym);
2778747c
AM
3502 if (target->replace_tls_get_addr(sym))
3503 sym = target->tls_get_addr_opt();
7e57d19e
AM
3504 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3505 if (gsym != NULL
7ee7ff70
AM
3506 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3507 && !target->is_elfv2_localentry0(gsym))
3508 : (this->object_->local_has_plt_offset(this->r_sym_)
3509 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3510 {
3511 this->tocsave_ = 1;
3512 return true;
3513 }
3514 return false;
3515}
3516
ec661b9d
AM
3517// If this branch needs a plt call stub, or a long branch stub, make one.
3518
3519template<int size, bool big_endian>
a3e60ddb 3520bool
ec661b9d
AM
3521Target_powerpc<size, big_endian>::Branch_info::make_stub(
3522 Stub_table<size, big_endian>* stub_table,
3523 Stub_table<size, big_endian>* ifunc_stub_table,
3524 Symbol_table* symtab) const
3525{
3526 Symbol* sym = this->object_->global_symbol(this->r_sym_);
88b8e639
AM
3527 Target_powerpc<size, big_endian>* target =
3528 static_cast<Target_powerpc<size, big_endian>*>(
3529 parameters->sized_target<size, big_endian>());
34e0882b
AM
3530 if (sym != NULL && sym->is_forwarder())
3531 sym = symtab->resolve_forwards(sym);
2778747c
AM
3532 if (target->replace_tls_get_addr(sym))
3533 sym = target->tls_get_addr_opt();
34e0882b 3534 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
dc60b26d
AM
3535 bool ok = true;
3536
ec661b9d 3537 if (gsym != NULL
88b8e639 3538 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3539 : this->object_->local_has_plt_offset(this->r_sym_))
3540 {
9055360d
AM
3541 if (size == 64
3542 && gsym != NULL
3543 && target->abiversion() >= 2
3544 && !parameters->options().output_is_position_independent()
32f59844 3545 && !is_branch_reloc<size>(this->r_type_))
9055360d
AM
3546 target->glink_section()->add_global_entry(gsym);
3547 else
ec661b9d 3548 {
64b5d6d7
AM
3549 if (stub_table == NULL
3550 && !(size == 32
3551 && gsym != NULL
3552 && !parameters->options().output_is_position_independent()
32f59844 3553 && !is_branch_reloc<size>(this->r_type_)))
9055360d
AM
3554 stub_table = this->object_->stub_table(this->shndx_);
3555 if (stub_table == NULL)
3556 {
64b5d6d7
AM
3557 // This is a ref from a data section to an ifunc symbol,
3558 // or a non-branch reloc for which we always want to use
3559 // one set of stubs for resolving function addresses.
9055360d
AM
3560 stub_table = ifunc_stub_table;
3561 }
3562 gold_assert(stub_table != NULL);
a3e60ddb
AM
3563 Address from = this->object_->get_output_section_offset(this->shndx_);
3564 if (from != invalid_address)
3565 from += (this->object_->output_section(this->shndx_)->address()
3566 + this->offset_);
9055360d 3567 if (gsym != NULL)
dc60b26d
AM
3568 ok = stub_table->add_plt_call_entry(from,
3569 this->object_, gsym,
7e57d19e
AM
3570 this->r_type_, this->addend_,
3571 this->tocsave_);
9055360d 3572 else
dc60b26d
AM
3573 ok = stub_table->add_plt_call_entry(from,
3574 this->object_, this->r_sym_,
7e57d19e
AM
3575 this->r_type_, this->addend_,
3576 this->tocsave_);
ec661b9d 3577 }
ec661b9d
AM
3578 }
3579 else
3580 {
32f59844 3581 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
a3e60ddb
AM
3582 if (max_branch_offset == 0)
3583 return true;
ec661b9d
AM
3584 Address from = this->object_->get_output_section_offset(this->shndx_);
3585 gold_assert(from != invalid_address);
3586 from += (this->object_->output_section(this->shndx_)->address()
3587 + this->offset_);
3588 Address to;
e3b53295 3589 unsigned int other = 0;
ec661b9d
AM
3590 if (gsym != NULL)
3591 {
3592 switch (gsym->source())
3593 {
3594 case Symbol::FROM_OBJECT:
3595 {
3596 Object* symobj = gsym->object();
3597 if (symobj->is_dynamic()
3598 || symobj->pluginobj() != NULL)
a3e60ddb 3599 return true;
ec661b9d
AM
3600 bool is_ordinary;
3601 unsigned int shndx = gsym->shndx(&is_ordinary);
3602 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3603 return true;
ec661b9d
AM
3604 }
3605 break;
3606
3607 case Symbol::IS_UNDEFINED:
a3e60ddb 3608 return true;
ec661b9d
AM
3609
3610 default:
3611 break;
3612 }
3613 Symbol_table::Compute_final_value_status status;
3614 to = symtab->compute_final_value<size>(gsym, &status);
3615 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3616 return true;
e3b53295
AM
3617 if (size == 64)
3618 other = gsym->nonvis() >> 3;
ec661b9d
AM
3619 }
3620 else
3621 {
3622 const Symbol_value<size>* psymval
3623 = this->object_->local_symbol(this->r_sym_);
3624 Symbol_value<size> symval;
0f125432
CC
3625 if (psymval->is_section_symbol())
3626 symval.set_is_section_symbol();
ec661b9d
AM
3627 typedef Sized_relobj_file<size, big_endian> ObjType;
3628 typename ObjType::Compute_final_local_value_status status
3629 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3630 &symval, symtab);
3631 if (status != ObjType::CFLV_OK
3632 || !symval.has_output_value())
a3e60ddb 3633 return true;
ec661b9d 3634 to = symval.value(this->object_, 0);
e3b53295
AM
3635 if (size == 64)
3636 other = this->object_->st_other(this->r_sym_) >> 5;
ec661b9d 3637 }
cbcb23fa
AM
3638 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3639 to += this->addend_;
ec661b9d
AM
3640 if (stub_table == NULL)
3641 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3642 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3643 {
3644 unsigned int dest_shndx;
1611bc4a
AM
3645 if (!target->symval_for_branch(symtab, gsym, this->object_,
3646 &to, &dest_shndx))
3647 return true;
ec661b9d 3648 }
fa40fbe4
AM
3649 unsigned int local_ent = 0;
3650 if (size == 64
eb97d024
AM
3651 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
3652 && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
fa40fbe4
AM
3653 local_ent = elfcpp::ppc64_decode_local_entry(other);
3654 Address delta = to + local_ent - from;
32f59844
AM
3655 if (delta + max_branch_offset >= 2 * max_branch_offset
3656 || (size == 64
eb97d024
AM
3657 && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3658 || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844
AM
3659 && (gsym != NULL
3660 ? this->object_->ppc64_needs_toc(gsym)
3661 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3662 {
0cfdc767
AM
3663 if (stub_table == NULL)
3664 {
3665 gold_warning(_("%s:%s: branch in non-executable section,"
3666 " no long branch stub for you"),
3667 this->object_->name().c_str(),
3668 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3669 return true;
0cfdc767 3670 }
d49044c7
AM
3671 bool save_res = (size == 64
3672 && gsym != NULL
3673 && gsym->source() == Symbol::IN_OUTPUT_DATA
3674 && gsym->output_data() == target->savres_section());
a19da04b 3675 ok = stub_table->add_long_branch_entry(this->r_type_,
fa40fbe4 3676 from, to, other, save_res);
ec661b9d
AM
3677 }
3678 }
dc60b26d
AM
3679 if (!ok)
3680 gold_debug(DEBUG_TARGET,
3681 "branch at %s:%s+%#lx\n"
3682 "can't reach stub attached to %s:%s",
3683 this->object_->name().c_str(),
3684 this->object_->section_name(this->shndx_).c_str(),
3685 (unsigned long) this->offset_,
3686 stub_table->relobj()->name().c_str(),
3687 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3688
3689 return ok;
ec661b9d
AM
3690}
3691
5a97377e
AM
3692// Helper for do_relax, avoiding checks that size, address and offset
3693// are not set more than once.
3694
3695static inline void
3696update_current_size(Output_section_data_build* od, off_t cur_size)
3697{
3698 od->reset_address_and_file_offset();
3699 od->set_current_data_size(cur_size);
3700 od->finalize_data_size();
3701 od->output_section()->set_section_offsets_need_adjustment();
3702}
3703
ec661b9d
AM
3704// Relaxation hook. This is where we do stub generation.
3705
3706template<int size, bool big_endian>
3707bool
3708Target_powerpc<size, big_endian>::do_relax(int pass,
3709 const Input_objects*,
3710 Symbol_table* symtab,
3711 Layout* layout,
3712 const Task* task)
3713{
3714 unsigned int prev_brlt_size = 0;
3715 if (pass == 1)
ec661b9d 3716 {
d537f77e 3717 if (size == 64 && this->abiversion() < 2)
ec661b9d 3718 {
e2458743 3719 static const char* const thread_starter[] =
9e69ed50
AM
3720 {
3721 "pthread_create",
3722 /* libstdc++ */
3723 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3724 /* librt */
3725 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3726 "mq_notify", "create_timer",
3727 /* libanl */
3728 "getaddrinfo_a",
3729 /* libgomp */
80272b8c 3730 "GOMP_parallel",
9e69ed50 3731 "GOMP_parallel_start",
80272b8c 3732 "GOMP_parallel_loop_static",
9e69ed50 3733 "GOMP_parallel_loop_static_start",
80272b8c 3734 "GOMP_parallel_loop_dynamic",
9e69ed50 3735 "GOMP_parallel_loop_dynamic_start",
80272b8c 3736 "GOMP_parallel_loop_guided",
9e69ed50 3737 "GOMP_parallel_loop_guided_start",
80272b8c 3738 "GOMP_parallel_loop_runtime",
9e69ed50 3739 "GOMP_parallel_loop_runtime_start",
80272b8c 3740 "GOMP_parallel_sections",
43819297 3741 "GOMP_parallel_sections_start",
f9dffbf0
AM
3742 /* libgo */
3743 "__go_go",
9e69ed50 3744 };
d537f77e 3745 bool thread_safe = parameters->options().plt_thread_safe();
9e69ed50 3746
d537f77e
AM
3747 if (!thread_safe
3748 && !parameters->options().user_set_plt_thread_safe())
9e69ed50 3749 {
d537f77e
AM
3750 if (parameters->options().shared())
3751 thread_safe = true;
3752 else
e2458743 3753 {
d537f77e
AM
3754 for (unsigned int i = 0;
3755 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3756 i++)
3757 {
3758 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3759 thread_safe = (sym != NULL
3760 && sym->in_reg()
3761 && sym->in_real_elf());
3762 if (thread_safe)
3763 break;
3764 }
e2458743 3765 }
9e69ed50 3766 }
d537f77e 3767 this->plt_thread_safe_ = thread_safe;
ec661b9d 3768 }
a3e60ddb 3769
d537f77e
AM
3770 if (size == 64
3771 && parameters->options().output_is_position_independent())
3772 {
3773 gold_assert (this->rela_dyn_);
3774 this->rela_dyn_size_ = this->rela_dyn_->current_data_size();
3775 }
5a97377e 3776
a3e60ddb
AM
3777 this->stub_group_size_ = parameters->options().stub_group_size();
3778 bool no_size_errors = true;
3779 if (this->stub_group_size_ == 1)
3780 this->stub_group_size_ = 0x1c00000;
3781 else if (this->stub_group_size_ == -1)
3782 this->stub_group_size_ = -0x1e00000;
3783 else
3784 no_size_errors = false;
3785 this->group_sections(layout, task, no_size_errors);
3786 }
3787 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3788 {
3789 this->branch_lookup_table_.clear();
3790 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3791 p != this->stub_tables_.end();
3792 ++p)
3793 {
3794 (*p)->clear_stubs(true);
3795 }
3796 this->stub_tables_.clear();
3797 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3798 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3799 program_name, this->stub_group_size_);
3800 this->group_sections(layout, task, true);
ec661b9d
AM
3801 }
3802
3803 // We need address of stub tables valid for make_stub.
3804 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3805 p != this->stub_tables_.end();
3806 ++p)
3807 {
3808 const Powerpc_relobj<size, big_endian>* object
3809 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3810 Address off = object->get_output_section_offset((*p)->shndx());
3811 gold_assert(off != invalid_address);
3812 Output_section* os = (*p)->output_section();
3813 (*p)->set_address_and_size(os, off);
3814 }
3815
9e69ed50
AM
3816 if (pass != 1)
3817 {
3818 // Clear plt call stubs, long branch stubs and branch lookup table.
3819 prev_brlt_size = this->branch_lookup_table_.size();
3820 this->branch_lookup_table_.clear();
3821 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3822 p != this->stub_tables_.end();
3823 ++p)
3824 {
a3e60ddb 3825 (*p)->clear_stubs(false);
9e69ed50
AM
3826 }
3827 }
3828
3829 // Build all the stubs.
a3e60ddb 3830 this->relax_failed_ = false;
ec661b9d
AM
3831 Stub_table<size, big_endian>* ifunc_stub_table
3832 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3833 Stub_table<size, big_endian>* one_stub_table
3834 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3835 for (typename Branches::const_iterator b = this->branch_info_.begin();
3836 b != this->branch_info_.end();
3837 b++)
3838 {
a3e60ddb
AM
3839 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3840 && !this->relax_failed_)
3841 {
3842 this->relax_failed_ = true;
3843 this->relax_fail_count_++;
3844 if (this->relax_fail_count_ < 3)
3845 return true;
3846 }
ec661b9d 3847 }
32f59844
AM
3848 bool do_resize = false;
3849 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3850 p != this->stub_tables_.end();
3851 ++p)
3852 if ((*p)->need_resize())
3853 {
3854 do_resize = true;
3855 break;
3856 }
3857 if (do_resize)
3858 {
3859 this->branch_lookup_table_.clear();
3860 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3861 p != this->stub_tables_.end();
3862 ++p)
3863 (*p)->set_resizing(true);
3864 for (typename Branches::const_iterator b = this->branch_info_.begin();
3865 b != this->branch_info_.end();
3866 b++)
3867 {
3868 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3869 && !this->relax_failed_)
3870 {
3871 this->relax_failed_ = true;
3872 this->relax_fail_count_++;
3873 if (this->relax_fail_count_ < 3)
3874 return true;
3875 }
3876 }
3877 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3878 p != this->stub_tables_.end();
3879 ++p)
3880 (*p)->set_resizing(false);
3881 }
ec661b9d 3882
9e69ed50 3883 // Did anything change size?
ec661b9d
AM
3884 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3885 bool again = num_huge_branches != prev_brlt_size;
3886 if (size == 64 && num_huge_branches != 0)
3887 this->make_brlt_section(layout);
3888 if (size == 64 && again)
5a97377e
AM
3889 {
3890 update_current_size(this->brlt_section_, num_huge_branches * 16);
3891 if (parameters->options().output_is_position_independent())
3892 {
3893 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3894 off_t cur = this->rela_dyn_size_ + num_huge_branches * reloc_size;
3895 update_current_size(this->rela_dyn_, cur);
3896 }
3897 }
ec661b9d 3898
be897fb7
AM
3899 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3900 p != this->stub_tables_.rend();
3901 ++p)
3902 (*p)->remove_eh_frame(layout);
3903
3904 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3905 p != this->stub_tables_.end();
3906 ++p)
3907 (*p)->add_eh_frame(layout);
3908
ec661b9d
AM
3909 typedef Unordered_set<Output_section*> Output_sections;
3910 Output_sections os_need_update;
3911 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3912 p != this->stub_tables_.end();
3913 ++p)
3914 {
3915 if ((*p)->size_update())
3916 {
3917 again = true;
3918 os_need_update.insert((*p)->output_section());
3919 }
3920 }
3921
9e69ed50
AM
3922 // Set output section offsets for all input sections in an output
3923 // section that just changed size. Anything past the stubs will
3924 // need updating.
ec661b9d
AM
3925 for (typename Output_sections::iterator p = os_need_update.begin();
3926 p != os_need_update.end();
3927 p++)
3928 {
3929 Output_section* os = *p;
3930 Address off = 0;
3931 typedef Output_section::Input_section_list Input_section_list;
3932 for (Input_section_list::const_iterator i = os->input_sections().begin();
3933 i != os->input_sections().end();
3934 ++i)
3935 {
3936 off = align_address(off, i->addralign());
3937 if (i->is_input_section() || i->is_relaxed_input_section())
3938 i->relobj()->set_section_offset(i->shndx(), off);
3939 if (i->is_relaxed_input_section())
3940 {
3941 Stub_table<size, big_endian>* stub_table
3942 = static_cast<Stub_table<size, big_endian>*>(
3943 i->relaxed_input_section());
6395d38b
HS
3944 Address stub_table_size = stub_table->set_address_and_size(os, off);
3945 off += stub_table_size;
3946 // After a few iterations, set current stub table size
3947 // as min size threshold, so later stub tables can only
3948 // grow in size.
3949 if (pass >= 4)
3950 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3951 }
3952 else
3953 off += i->data_size();
3954 }
6830ee24
AM
3955 // If .branch_lt is part of this output section, then we have
3956 // just done the offset adjustment.
ec661b9d
AM
3957 os->clear_section_offsets_need_adjustment();
3958 }
3959
3960 if (size == 64
3961 && !again
3962 && num_huge_branches != 0
3963 && parameters->options().output_is_position_independent())
3964 {
3965 // Fill in the BRLT relocs.
5a97377e
AM
3966 this->rela_dyn_->reset_data_size();
3967 this->rela_dyn_->set_current_data_size(this->rela_dyn_size_);
ec661b9d
AM
3968 for (typename Branch_lookup_table::const_iterator p
3969 = this->branch_lookup_table_.begin();
3970 p != this->branch_lookup_table_.end();
3971 ++p)
3972 {
5a97377e
AM
3973 this->rela_dyn_->add_relative(elfcpp::R_POWERPC_RELATIVE,
3974 this->brlt_section_, p->second,
3975 p->first);
ec661b9d 3976 }
5a97377e
AM
3977 this->rela_dyn_->finalize_data_size();
3978 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3979 gold_assert(this->rela_dyn_->data_size()
3980 == this->rela_dyn_size_ + num_huge_branches * reloc_size);
ec661b9d 3981 }
590b87ff
AM
3982
3983 if (!again
3984 && (parameters->options().user_set_emit_stub_syms()
3985 ? parameters->options().emit_stub_syms()
3986 : (size == 64
3987 || parameters->options().output_is_position_independent()
3988 || parameters->options().emit_relocs())))
3989 {
3990 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3991 p != this->stub_tables_.end();
3992 ++p)
3993 (*p)->define_stub_syms(symtab);
3994
3995 if (this->glink_ != NULL)
3996 {
9e390558 3997 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3998 Address value = -stub_size;
3999 if (size == 64)
4000 {
4001 value = 8;
4002 stub_size -= 8;
4003 }
4004 this->define_local(symtab, "__glink_PLTresolve",
4005 this->glink_, value, stub_size);
4006
4007 if (size != 64)
4008 this->define_local(symtab, "__glink", this->glink_, 0, 0);
4009 }
4010 }
4011
ec661b9d
AM
4012 return again;
4013}
4014
9d5781f8
AM
4015template<int size, bool big_endian>
4016void
4017Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
4018 unsigned char* oview,
4019 uint64_t* paddress,
4020 off_t* plen) const
4021{
4022 uint64_t address = plt->address();
4023 off_t len = plt->data_size();
4024
4025 if (plt == this->glink_)
4026 {
4027 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
4028 if (len == 0)
4029 {
5fe7ffdc
AM
4030 // Static linking may need stubs, to support ifunc and long
4031 // branches. We need to create an output section for
4032 // .eh_frame early in the link process, to have a place to
4033 // attach stub .eh_frame info. We also need to have
4034 // registered a CIE that matches the stub CIE. Both of
4035 // these requirements are satisfied by creating an FDE and
4036 // CIE for .glink, even though static linking will leave
4037 // .glink zero length.
4038 // ??? Hopefully generating an FDE with a zero address range
4039 // won't confuse anything that consumes .eh_frame info.
4040 }
4041 else if (size == 64)
9d5781f8
AM
4042 {
4043 // There is one word before __glink_PLTresolve
4044 address += 8;
4045 len -= 8;
4046 }
4047 else if (parameters->options().output_is_position_independent())
4048 {
4049 // There are two FDEs for a position independent glink.
4050 // The first covers the branch table, the second
4051 // __glink_PLTresolve at the end of glink.
9e390558 4052 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 4053 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
4054 len -= resolve_size;
4055 else
4056 {
4057 address += len - resolve_size;
4058 len = resolve_size;
4059 }
4060 }
4061 }
4062 else
4063 {
4064 // Must be a stub table.
4065 const Stub_table<size, big_endian>* stub_table
4066 = static_cast<const Stub_table<size, big_endian>*>(plt);
4067 uint64_t stub_address = stub_table->stub_address();
4068 len -= stub_address - address;
4069 address = stub_address;
4070 }
4071
4072 *paddress = address;
4073 *plen = len;
4074}
4075
42cacb20
DE
4076// A class to handle the PLT data.
4077
4078template<int size, bool big_endian>
cf43a2fe 4079class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
4080{
4081 public:
4082 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4083 size, big_endian> Reloc_section;
4084
e5d5f5ed 4085 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
f21dbd7c 4086 Symbol_table* symtab,
e5d5f5ed 4087 Reloc_section* plt_rel,
e5d5f5ed
AM
4088 const char* name)
4089 : Output_section_data_build(size == 32 ? 4 : 8),
f21dbd7c 4090 rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
e5d5f5ed 4091 { }
42cacb20
DE
4092
4093 // Add an entry to the PLT.
03e25981 4094 void
f21dbd7c 4095 add_entry(Symbol*, bool = false);
42cacb20 4096
03e25981 4097 void
e5d5f5ed
AM
4098 add_ifunc_entry(Symbol*);
4099
2d7ad24e
AM
4100 void
4101 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4102
03e25981 4103 void
e5d5f5ed
AM
4104 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4105
42cacb20 4106 // Return the .rela.plt section data.
e5d5f5ed 4107 Reloc_section*
cf43a2fe
AM
4108 rel_plt() const
4109 {
42cacb20
DE
4110 return this->rel_;
4111 }
4112
0e70b911
CC
4113 // Return the number of PLT entries.
4114 unsigned int
4115 entry_count() const
d83ce4e3 4116 {
b3ccdeb5
AM
4117 if (this->current_data_size() == 0)
4118 return 0;
b4f7960d
AM
4119 return ((this->current_data_size() - this->first_plt_entry_offset())
4120 / this->plt_entry_size());
d83ce4e3 4121 }
0e70b911 4122
42cacb20 4123 protected:
42cacb20 4124 void
cf43a2fe 4125 do_adjust_output_section(Output_section* os)
42cacb20 4126 {
cf43a2fe 4127 os->set_entsize(0);
42cacb20
DE
4128 }
4129
6ce78956
AM
4130 // Write to a map file.
4131 void
4132 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4133 { mapfile->print_output_data(this, this->name_); }
6ce78956 4134
cf43a2fe 4135 private:
f21dbd7c
AM
4136 struct Local_plt_ent
4137 {
4138 Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4139 { rsym_ = rsym; u.obj_ = obj; }
4140 Local_plt_ent(Symbol* sym)
4141 { rsym_ = -1u; u.gsym_ = sym; }
4142 ~Local_plt_ent()
4143 { }
4144
4145 unsigned int rsym_;
4146 union
4147 {
4148 Sized_relobj_file<size, big_endian>* obj_;
4149 Symbol* gsym_;
4150 } u;
4151 };
4152
b4f7960d
AM
4153 // Return the offset of the first non-reserved PLT entry.
4154 unsigned int
4155 first_plt_entry_offset() const
4156 {
2d7ad24e
AM
4157 // IPLT and LPLT have no reserved entry.
4158 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4159 return 0;
4160 return this->targ_->first_plt_entry_offset();
4161 }
4162
4163 // Return the size of each PLT entry.
4164 unsigned int
4165 plt_entry_size() const
4166 {
4167 return this->targ_->plt_entry_size();
4168 }
cf43a2fe 4169
42cacb20
DE
4170 // Write out the PLT data.
4171 void
4172 do_write(Output_file*);
4173
4174 // The reloc section.
4175 Reloc_section* rel_;
cf43a2fe
AM
4176 // Allows access to .glink for do_write.
4177 Target_powerpc<size, big_endian>* targ_;
f21dbd7c 4178 Symbol_table* symtab_;
e5d5f5ed
AM
4179 // What to report in map file.
4180 const char *name_;
f21dbd7c
AM
4181
4182 std::vector<Local_plt_ent> sym_ents_;
42cacb20
DE
4183};
4184
e5d5f5ed 4185// Add an entry to the PLT.
42cacb20
DE
4186
4187template<int size, bool big_endian>
03e25981 4188void
f21dbd7c 4189Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
0961e631 4190 bool is_local)
42cacb20 4191{
e5d5f5ed
AM
4192 if (!gsym->has_plt_offset())
4193 {
ec661b9d 4194 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4195 if (off == 0)
4196 off += this->first_plt_entry_offset();
4197 gsym->set_plt_offset(off);
f21dbd7c
AM
4198 if (this->rel_)
4199 {
0961e631
AM
4200 if (is_local)
4201 {
4202 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4203 if (size == 64 && this->targ_->abiversion() < 2)
4204 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4205 this->rel_->add_symbolless_global_addend(gsym, dynrel,
4206 this, off, 0);
4207 }
4208 else
4209 {
4210 gsym->set_needs_dynsym_entry();
4211 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4212 this->rel_->add_global(gsym, dynrel, this, off, 0);
4213 }
f21dbd7c 4214 }
b4f7960d 4215 off += this->plt_entry_size();
e5d5f5ed 4216 this->set_current_data_size(off);
0961e631 4217 if (is_local)
f21dbd7c
AM
4218 {
4219 Local_plt_ent sym(gsym);
4220 this->sym_ents_.push_back(sym);
4221 }
e5d5f5ed 4222 }
42cacb20
DE
4223}
4224
e5d5f5ed 4225// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4226
4227template<int size, bool big_endian>
03e25981 4228void
e5d5f5ed 4229Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4230{
cf43a2fe
AM
4231 if (!gsym->has_plt_offset())
4232 {
ec661b9d 4233 section_size_type off = this->current_data_size();
cf43a2fe 4234 gsym->set_plt_offset(off);
e5d5f5ed 4235 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4236 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4237 dynrel = elfcpp::R_PPC64_JMP_IREL;
4238 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4239 off += this->plt_entry_size();
e5d5f5ed
AM
4240 this->set_current_data_size(off);
4241 }
4242}
4243
2d7ad24e
AM
4244// Add an entry for a local symbol to the PLT.
4245
4246template<int size, bool big_endian>
4247void
4248Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4249 Sized_relobj_file<size, big_endian>* relobj,
4250 unsigned int local_sym_index)
4251{
4252 if (!relobj->local_has_plt_offset(local_sym_index))
4253 {
4254 section_size_type off = this->current_data_size();
4255 relobj->set_local_plt_offset(local_sym_index, off);
4256 if (this->rel_)
4257 {
4258 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4259 if (size == 64 && this->targ_->abiversion() < 2)
4260 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4261 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4262 dynrel, this, off, 0);
4263 }
4264 off += this->plt_entry_size();
4265 this->set_current_data_size(off);
f21dbd7c
AM
4266 Local_plt_ent sym(relobj, local_sym_index);
4267 this->sym_ents_.push_back(sym);
2d7ad24e
AM
4268 }
4269}
4270
e5d5f5ed
AM
4271// Add an entry for a local ifunc symbol to the IPLT.
4272
4273template<int size, bool big_endian>
03e25981 4274void
e5d5f5ed
AM
4275Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4276 Sized_relobj_file<size, big_endian>* relobj,
4277 unsigned int local_sym_index)
4278{
4279 if (!relobj->local_has_plt_offset(local_sym_index))
4280 {
ec661b9d 4281 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4282 relobj->set_local_plt_offset(local_sym_index, off);
4283 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4284 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4285 dynrel = elfcpp::R_PPC64_JMP_IREL;
4286 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4287 this, off, 0);
b4f7960d 4288 off += this->plt_entry_size();
cf43a2fe
AM
4289 this->set_current_data_size(off);
4290 }
42cacb20
DE
4291}
4292
dd93cd0a 4293static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4294static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4295static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4296static const uint32_t add_3_3_2 = 0x7c631214;
4297static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4298static const uint32_t add_3_12_2 = 0x7c6c1214;
4299static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4300static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4301static const uint32_t add_11_2_11 = 0x7d625a14;
4302static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4303static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4304static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4305static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4306static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4307static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4308static const uint32_t addi_12_1 = 0x39810000;
32f59844 4309static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4310static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4311static const uint32_t addis_0_2 = 0x3c020000;
4312static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4313static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4314static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4315static const uint32_t addis_11_11 = 0x3d6b0000;
4316static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4317static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4318static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4319static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4320static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4321static const uint32_t b = 0x48000000;
4322static const uint32_t bcl_20_31 = 0x429f0005;
4323static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4324static const uint32_t bctrl = 0x4e800421;
4325static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4326static const uint32_t blr = 0x4e800020;
9e69ed50 4327static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4328static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4329static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4330static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4331static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4332static const uint32_t cror_15_15_15 = 0x4def7b82;
4333static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29 4334static const uint32_t ld_0_1 = 0xe8010000;
63e5eea2 4335static const uint32_t ld_0_11 = 0xe80b0000;
f3a0ed29 4336static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4337static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4338static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4339static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4340static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4341static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4342static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4343static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4344static const uint32_t ld_11_11 = 0xe96b0000;
4345static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4346static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4347static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4348static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4349static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4350static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4351static const uint32_t li_0_0 = 0x38000000;
e4dff765 4352static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4353static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4354static const uint32_t lis_0 = 0x3c000000;
549dba71 4355static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4356static const uint32_t lis_11 = 0x3d600000;
4357static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4358static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4359static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4360static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4361static const uint32_t lwz_11_11 = 0x816b0000;
4362static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4363static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4364static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4365static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4366static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4367static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4368static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4369static const uint32_t mr_0_3 = 0x7c601b78;
4370static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4371static const uint32_t mtctr_0 = 0x7c0903a6;
4372static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4373static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4374static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4375static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4376static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4377static const uint32_t nop = 0x60000000;
c9269dff 4378static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4379static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4380static const uint32_t ori_12_12_0 = 0x618c0000;
4381static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4382static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4383static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4384static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4385static const uint32_t std_0_1 = 0xf8010000;
4386static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4387static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4388static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4389static const uint32_t stfd_0_1 = 0xd8010000;
4390static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4391static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4392static const uint32_t sub_12_12_11 = 0x7d8b6050;
4393static const uint32_t xor_2_12_12 = 0x7d826278;
4394static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4395
e4dff765
AM
4396static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4397static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4398static const uint64_t pnop = 0x0700000000000000ULL;
4399
42cacb20
DE
4400// Write out the PLT.
4401
4402template<int size, bool big_endian>
4403void
4404Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4405{
c21736ae
AM
4406 if (!this->sym_ents_.empty()
4407 && !parameters->options().output_is_position_independent())
f21dbd7c
AM
4408 {
4409 const section_size_type offset = this->offset();
4410 const section_size_type oview_size
4411 = convert_to_section_size_type(this->data_size());
4412 unsigned char* const oview = of->get_output_view(offset, oview_size);
4413 unsigned char* pov = oview;
4414 unsigned char* endpov = oview + oview_size;
4415
4416 for (typename std::vector<Local_plt_ent>::iterator e
4417 = this->sym_ents_.begin();
4418 e != this->sym_ents_.end();
4419 e++)
4420 {
4421 typename elfcpp::Elf_types<size>::Elf_Addr val;
4422 Sized_symbol<size>* gsym = NULL;
4423 Powerpc_relobj<size, big_endian>* obj = NULL;
4424 if (e->rsym_ == -1u)
4425 {
4426 gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4427 val = gsym->value();
4428 }
4429 else
4430 {
4431 obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4432 val = obj->local_symbol(e->rsym_)->value(obj, 0);
4433 }
4434 if (this->targ_->abiversion() >= 2)
4435 {
4436 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4437 pov += size / 8;
4438 }
4439 else
4440 {
4441 unsigned int shndx;
4442 this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4443 &val, &shndx);
4444 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4445 pov += size / 8;
4446 val = this->targ_->toc_pointer();
4447 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4448 pov += size / 8;
4449 if (this->plt_entry_size() > 16)
4450 {
4451 elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4452 pov += size / 8;
4453 }
4454 }
4455 }
4456 gold_assert(pov == endpov);
4457 }
4458
2d7ad24e 4459 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4460 {
ec661b9d 4461 const section_size_type offset = this->offset();
cf43a2fe
AM
4462 const section_size_type oview_size
4463 = convert_to_section_size_type(this->data_size());
4464 unsigned char* const oview = of->get_output_view(offset, oview_size);
4465 unsigned char* pov = oview;
4466 unsigned char* endpov = oview + oview_size;
4467
e5d5f5ed 4468 // The address of the .glink branch table
cf43a2fe
AM
4469 const Output_data_glink<size, big_endian>* glink
4470 = this->targ_->glink_section();
ec661b9d 4471 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4472
4473 while (pov < endpov)
4474 {
4475 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4476 pov += 4;
4477 branch_tab += 4;
4478 }
4479
4480 of->write_output_view(offset, oview_size, oview);
4481 }
4482}
4483
4484// Create the PLT section.
4485
4486template<int size, bool big_endian>
4487void
40b469d7
AM
4488Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4489 Layout* layout)
cf43a2fe
AM
4490{
4491 if (this->plt_ == NULL)
4492 {
40b469d7 4493 if (this->got_ == NULL)
f19c3684 4494 this->got_section(symtab, layout, GOT_TYPE_SMALL);
40b469d7 4495
cf43a2fe
AM
4496 if (this->glink_ == NULL)
4497 make_glink_section(layout);
4498
4499 // Ensure that .rela.dyn always appears before .rela.plt This is
4500 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4501 // needs to include .rela.plt in its range.
cf43a2fe
AM
4502 this->rela_dyn_section(layout);
4503
e5d5f5ed
AM
4504 Reloc_section* plt_rel = new Reloc_section(false);
4505 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4506 elfcpp::SHF_ALLOC, plt_rel,
4507 ORDER_DYNAMIC_PLT_RELOCS, false);
4508 this->plt_
f21dbd7c 4509 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
e5d5f5ed 4510 "** PLT");
cf43a2fe
AM
4511 layout->add_output_section_data(".plt",
4512 (size == 32
4513 ? elfcpp::SHT_PROGBITS
4514 : elfcpp::SHT_NOBITS),
4515 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4516 this->plt_,
4517 (size == 32
4518 ? ORDER_SMALL_DATA
4519 : ORDER_SMALL_BSS),
4520 false);
3254d32c
AM
4521
4522 Output_section* rela_plt_os = plt_rel->output_section();
4523 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4524 }
4525}
4526
e5d5f5ed
AM
4527// Create the IPLT section.
4528
4529template<int size, bool big_endian>
4530void
40b469d7
AM
4531Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4532 Layout* layout)
e5d5f5ed
AM
4533{
4534 if (this->iplt_ == NULL)
4535 {
40b469d7 4536 this->make_plt_section(symtab, layout);
f21dbd7c 4537 this->make_lplt_section(symtab, layout);
e5d5f5ed
AM
4538
4539 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4540 if (this->rela_dyn_->output_section())
4541 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed 4542 this->iplt_
f21dbd7c 4543 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
b4f7960d 4544 "** IPLT");
6528b6eb
AM
4545 if (this->plt_->output_section())
4546 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4547 }
4548}
4549
2d7ad24e
AM
4550// Create the LPLT section.
4551
4552template<int size, bool big_endian>
4553void
f21dbd7c
AM
4554Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4555 Layout* layout)
2d7ad24e
AM
4556{
4557 if (this->lplt_ == NULL)
4558 {
4559 Reloc_section* lplt_rel = NULL;
4560 if (parameters->options().output_is_position_independent())
0961e631 4561 lplt_rel = this->rela_dyn_section(layout);
2d7ad24e 4562 this->lplt_
f21dbd7c 4563 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
2d7ad24e
AM
4564 "** LPLT");
4565 this->make_brlt_section(layout);
4566 if (this->brlt_section_ && this->brlt_section_->output_section())
4567 this->brlt_section_->output_section()
4568 ->add_output_section_data(this->lplt_);
4569 else
4570 layout->add_output_section_data(".branch_lt",
4571 elfcpp::SHT_PROGBITS,
4572 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4573 this->lplt_,
4574 ORDER_RELRO,
4575 true);
4576 }
4577}
4578
ec661b9d 4579// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4580
4581template<int size, bool big_endian>
ec661b9d 4582class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4583{
4584 public:
ec661b9d
AM
4585 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4586 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4587 size, big_endian> Reloc_section;
c9269dff 4588
5a97377e 4589 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ)
ec661b9d 4590 : Output_section_data_build(size == 32 ? 4 : 8),
ec661b9d
AM
4591 targ_(targ)
4592 { }
cf43a2fe 4593
ec661b9d
AM
4594 protected:
4595 void
4596 do_adjust_output_section(Output_section* os)
4597 {
4598 os->set_entsize(0);
4599 }
e5d5f5ed 4600
ec661b9d
AM
4601 // Write to a map file.
4602 void
4603 do_print_to_mapfile(Mapfile* mapfile) const
4604 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4605
ec661b9d
AM
4606 private:
4607 // Write out the BRLT data.
4608 void
4609 do_write(Output_file*);
c9824451 4610
ec661b9d
AM
4611 Target_powerpc<size, big_endian>* targ_;
4612};
cf43a2fe 4613
ec661b9d
AM
4614// Make the branch lookup table section.
4615
4616template<int size, bool big_endian>
4617void
4618Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4619{
4620 if (size == 64 && this->brlt_section_ == NULL)
4621 {
ec661b9d
AM
4622 bool is_pic = parameters->options().output_is_position_independent();
4623 if (is_pic)
4624 {
54483898
AM
4625 // When PIC we can't fill in .branch_lt but must initialise at
4626 // runtime via dynamic relocations.
dc808a2e 4627 this->rela_dyn_section(layout);
ec661b9d
AM
4628 }
4629 this->brlt_section_
5a97377e 4630 = new Output_data_brlt_powerpc<size, big_endian>(this);
6528b6eb 4631 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4632 this->plt_->output_section()
4633 ->add_output_section_data(this->brlt_section_);
4634 else
6830ee24 4635 layout->add_output_section_data(".branch_lt",
54483898 4636 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4637 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4638 this->brlt_section_,
54483898
AM
4639 ORDER_RELRO,
4640 true);
ec661b9d
AM
4641 }
4642}
4643
6830ee24 4644// Write out .branch_lt when non-PIC.
ec661b9d
AM
4645
4646template<int size, bool big_endian>
4647void
4648Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4649{
4650 if (size == 64 && !parameters->options().output_is_position_independent())
4651 {
4652 const section_size_type offset = this->offset();
4653 const section_size_type oview_size
4654 = convert_to_section_size_type(this->data_size());
4655 unsigned char* const oview = of->get_output_view(offset, oview_size);
4656
4657 this->targ_->write_branch_lookup_table(oview);
4658 of->write_output_view(offset, oview_size, oview);
4659 }
4660}
4661
9e69ed50
AM
4662static inline uint32_t
4663l(uint32_t a)
4664{
4665 return a & 0xffff;
4666}
4667
4668static inline uint32_t
4669hi(uint32_t a)
4670{
4671 return l(a >> 16);
4672}
4673
4674static inline uint32_t
4675ha(uint32_t a)
4676{
4677 return hi(a + 0x8000);
4678}
4679
e4dff765
AM
4680static inline uint64_t
4681d34(uint64_t v)
4682{
4683 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4684}
4685
4686static inline uint64_t
4687ha34(uint64_t v)
4688{
4689 return (v + (1ULL << 33)) >> 34;
4690}
4691
9d5781f8
AM
4692template<int size>
4693struct Eh_cie
4694{
4695 static const unsigned char eh_frame_cie[12];
4696};
4697
4698template<int size>
4699const unsigned char Eh_cie<size>::eh_frame_cie[] =
4700{
4701 1, // CIE version.
4702 'z', 'R', 0, // Augmentation string.
4703 4, // Code alignment.
4704 0x80 - size / 8 , // Data alignment.
4705 65, // RA reg.
4706 1, // Augmentation size.
4707 (elfcpp::DW_EH_PE_pcrel
4708 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4709 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4710};
4711
b4f7960d
AM
4712// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4713static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4714{
4715 0, 0, 0, 0, // Replaced with offset to .glink.
4716 0, 0, 0, 0, // Replaced with size of .glink.
4717 0, // Augmentation size.
63e5eea2 4718 elfcpp::DW_CFA_advance_loc + 2,
9d5781f8 4719 elfcpp::DW_CFA_register, 65, 12,
63e5eea2 4720 elfcpp::DW_CFA_advance_loc + 4,
9d5781f8
AM
4721 elfcpp::DW_CFA_restore_extended, 65
4722};
4723
b4f7960d
AM
4724// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4725static const unsigned char glink_eh_frame_fde_64v2[] =
4726{
4727 0, 0, 0, 0, // Replaced with offset to .glink.
4728 0, 0, 0, 0, // Replaced with size of .glink.
4729 0, // Augmentation size.
63e5eea2 4730 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d 4731 elfcpp::DW_CFA_register, 65, 0,
63e5eea2
AM
4732 elfcpp::DW_CFA_advance_loc + 2,
4733 elfcpp::DW_CFA_restore_extended, 65
4734};
4735
4736static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4737{
4738 0, 0, 0, 0, // Replaced with offset to .glink.
4739 0, 0, 0, 0, // Replaced with size of .glink.
4740 0, // Augmentation size.
4741 elfcpp::DW_CFA_advance_loc + 3,
4742 elfcpp::DW_CFA_register, 65, 0,
4743 elfcpp::DW_CFA_advance_loc + 2,
b4f7960d
AM
4744 elfcpp::DW_CFA_restore_extended, 65
4745};
4746
9d5781f8
AM
4747// Describe __glink_PLTresolve use of LR, 32-bit version.
4748static const unsigned char glink_eh_frame_fde_32[] =
4749{
4750 0, 0, 0, 0, // Replaced with offset to .glink.
4751 0, 0, 0, 0, // Replaced with size of .glink.
4752 0, // Augmentation size.
4753 elfcpp::DW_CFA_advance_loc + 2,
4754 elfcpp::DW_CFA_register, 65, 0,
4755 elfcpp::DW_CFA_advance_loc + 4,
4756 elfcpp::DW_CFA_restore_extended, 65
4757};
4758
4759static const unsigned char default_fde[] =
4760{
4761 0, 0, 0, 0, // Replaced with offset to stubs.
4762 0, 0, 0, 0, // Replaced with size of stubs.
4763 0, // Augmentation size.
4764 elfcpp::DW_CFA_nop, // Pad.
4765 elfcpp::DW_CFA_nop,
4766 elfcpp::DW_CFA_nop
4767};
4768
9e69ed50
AM
4769template<bool big_endian>
4770static inline void
4771write_insn(unsigned char* p, uint32_t v)
4772{
4773 elfcpp::Swap<32, big_endian>::writeval(p, v);
4774}
4775
691d2e9a
AM
4776template<int size>
4777static inline unsigned int
4778param_plt_align()
4779{
4780 if (!parameters->options().user_set_plt_align())
4781 return size == 64 ? 32 : 8;
4782 return 1 << parameters->options().plt_align();
4783}
4784
ec661b9d
AM
4785// Stub_table holds information about plt and long branch stubs.
4786// Stubs are built in an area following some input section determined
4787// by group_sections(). This input section is converted to a relaxed
4788// input section allowing it to be resized to accommodate the stubs
4789
4790template<int size, bool big_endian>
4791class Stub_table : public Output_relaxed_input_section
4792{
4793 public:
7e57d19e
AM
4794 struct Plt_stub_ent
4795 {
4796 Plt_stub_ent(unsigned int off, unsigned int indx)
eb97d024
AM
4797 : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4798 toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
7e57d19e
AM
4799 { }
4800
4801 unsigned int off_;
afd2ea23 4802 unsigned int indx_;
eb97d024
AM
4803 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4804 // toc stub, p9off_ is offset to p9notoc stub
4805 unsigned int tocoff_ : 8;
4806 unsigned int p9off_ : 8;
4807 // The size of the toc stub, used to locate blr on tls_get_addr stub.
4808 unsigned int tsize_ : 8;
4809 // Stub revision management
32f59844 4810 unsigned int iter_ : 1;
eb97d024 4811 // The three types of stubs.
afd2ea23 4812 unsigned int toc_ : 1;
eb97d024
AM
4813 unsigned int notoc_ : 1;
4814 unsigned int p9notoc_ : 1;
4815 // Each with a possible variant saving r2 first
7e57d19e 4816 unsigned int r2save_ : 1;
eb97d024 4817 // Handy cached info from symbol
7ee7ff70 4818 unsigned int localentry0_ : 1;
7e57d19e 4819 };
32f59844
AM
4820 struct Branch_stub_ent
4821 {
eb97d024
AM
4822 Branch_stub_ent(unsigned int off)
4823 : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4824 p9notoc_(0), save_res_(0), other_(0)
32f59844
AM
4825 { }
4826
4827 unsigned int off_;
eb97d024
AM
4828 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4829 // toc stub, p9off_ is offset to p9notoc stub
4830 unsigned int tocoff_ : 8;
4831 unsigned int p9off_ : 8;
4832 // Stub revision management
afd2ea23 4833 unsigned int iter_ : 1;
eb97d024 4834 // Four types of stubs.
afd2ea23 4835 unsigned int toc_ : 1;
eb97d024
AM
4836 unsigned int notoc_ : 1;
4837 unsigned int p9notoc_ : 1;
afd2ea23 4838 unsigned int save_res_ : 1;
eb97d024 4839 // Handy cached info from symbol
fa40fbe4 4840 unsigned int other_ : 3;
32f59844 4841 };
ec661b9d
AM
4842 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4843 static const Address invalid_address = static_cast<Address>(0) - 1;
4844
a3e60ddb
AM
4845 Stub_table(Target_powerpc<size, big_endian>* targ,
4846 Output_section* output_section,
590b87ff
AM
4847 const Output_section::Input_section* owner,
4848 uint32_t id)
a3e60ddb
AM
4849 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4850 owner->relobj()
4851 ->section_addralign(owner->shndx())),
ec661b9d 4852 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4853 orig_data_size_(owner->current_data_size()),
4854 plt_size_(0), last_plt_size_(0),
6395d38b 4855 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4856 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4857 uniq_(id)
a3e60ddb
AM
4858 {
4859 this->set_output_section(output_section);
ec661b9d 4860
a3e60ddb
AM
4861 std::vector<Output_relaxed_input_section*> new_relaxed;
4862 new_relaxed.push_back(this);
4863 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4864 }
ec661b9d
AM
4865
4866 // Add a plt call stub.
a3e60ddb
AM
4867 bool
4868 add_plt_call_entry(Address,
4869 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4870 const Symbol*,
4871 unsigned int,
7e57d19e
AM
4872 Address,
4873 bool);
ec661b9d 4874
a3e60ddb
AM
4875 bool
4876 add_plt_call_entry(Address,
4877 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4878 unsigned int,
4879 unsigned int,
7e57d19e
AM
4880 Address,
4881 bool);
ec661b9d
AM
4882
4883 // Find a given plt call stub.
7e57d19e 4884 const Plt_stub_ent*
ec661b9d
AM
4885 find_plt_call_entry(const Symbol*) const;
4886
7e57d19e 4887 const Plt_stub_ent*
ec661b9d
AM
4888 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4889 unsigned int) const;
4890
7e57d19e 4891 const Plt_stub_ent*
ec661b9d
AM
4892 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4893 const Symbol*,
4894 unsigned int,
4895 Address) const;
4896
7e57d19e 4897 const Plt_stub_ent*
ec661b9d
AM
4898 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4899 unsigned int,
4900 unsigned int,
4901 Address) const;
4902
4903 // Add a long branch stub.
a3e60ddb 4904 bool
a19da04b 4905 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
ec661b9d 4906
32f59844 4907 const Branch_stub_ent*
a19da04b 4908 find_long_branch_entry(Address) const;
ec661b9d 4909
a3e60ddb
AM
4910 bool
4911 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4912 {
32f59844 4913 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4914 if (max_branch_offset == 0)
4915 return true;
4916 gold_assert(from != invalid_address);
4917 Address loc = off + this->stub_address();
4918 return loc - from + max_branch_offset < 2 * max_branch_offset;
4919 }
4920
ec661b9d 4921 void
a3e60ddb 4922 clear_stubs(bool all)
cf43a2fe 4923 {
9e69ed50
AM
4924 this->plt_call_stubs_.clear();
4925 this->plt_size_ = 0;
ec661b9d
AM
4926 this->long_branch_stubs_.clear();
4927 this->branch_size_ = 0;
d49044c7 4928 this->need_save_res_ = false;
a3e60ddb
AM
4929 if (all)
4930 {
4931 this->last_plt_size_ = 0;
4932 this->last_branch_size_ = 0;
4933 }
cf43a2fe
AM
4934 }
4935
32f59844
AM
4936 bool
4937 need_resize() const
4938 { return need_resize_; }
4939
4940 void
4941 set_resizing(bool val)
4942 {
4943 this->resizing_ = val;
4944 if (val)
4945 {
4946 this->need_resize_ = false;
4947 this->plt_size_ = 0;
4948 this->branch_size_ = 0;
4949 this->need_save_res_ = false;
4950 }
4951 }
4952
ec661b9d
AM
4953 Address
4954 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4955 {
ec661b9d
AM
4956 Address start_off = off;
4957 off += this->orig_data_size_;
4958 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4959 if (this->need_save_res_)
4960 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4961 if (my_size != 0)
4962 off = align_address(off, this->stub_align());
4963 // Include original section size and alignment padding in size
4964 my_size += off - start_off;
6395d38b
HS
4965 // Ensure new size is always larger than min size
4966 // threshold. Alignment requirement is included in "my_size", so
4967 // increase "my_size" does not invalidate alignment.
4968 if (my_size < this->min_size_threshold_)
4969 my_size = this->min_size_threshold_;
ec661b9d
AM
4970 this->reset_address_and_file_offset();
4971 this->set_current_data_size(my_size);
4972 this->set_address_and_file_offset(os->address() + start_off,
4973 os->offset() + start_off);
4974 return my_size;
cf43a2fe
AM
4975 }
4976
ec661b9d 4977 Address
9d5781f8 4978 stub_address() const
ec661b9d
AM
4979 {
4980 return align_address(this->address() + this->orig_data_size_,
4981 this->stub_align());
4982 }
4983
4984 Address
9d5781f8 4985 stub_offset() const
ec661b9d
AM
4986 {
4987 return align_address(this->offset() + this->orig_data_size_,
4988 this->stub_align());
4989 }
4990
4991 section_size_type
4992 plt_size() const
4993 { return this->plt_size_; }
4994
32f59844
AM
4995 section_size_type
4996 branch_size() const
4997 { return this->branch_size_; }
4998
590b87ff
AM
4999 void
5000 set_min_size_threshold(Address min_size)
6395d38b
HS
5001 { this->min_size_threshold_ = min_size; }
5002
590b87ff
AM
5003 void
5004 define_stub_syms(Symbol_table*);
5005
ec661b9d
AM
5006 bool
5007 size_update()
5008 {
5009 Output_section* os = this->output_section();
5010 if (os->addralign() < this->stub_align())
5011 {
5012 os->set_addralign(this->stub_align());
5013 // FIXME: get rid of the insane checkpointing.
5014 // We can't increase alignment of the input section to which
5015 // stubs are attached; The input section may be .init which
5016 // is pasted together with other .init sections to form a
5017 // function. Aligning might insert zero padding resulting in
5018 // sigill. However we do need to increase alignment of the
5019 // output section so that the align_address() on offset in
5020 // set_address_and_size() adds the same padding as the
5021 // align_address() on address in stub_address().
5022 // What's more, we need this alignment for the layout done in
5023 // relaxation_loop_body() so that the output section starts at
5024 // a suitably aligned address.
5025 os->checkpoint_set_addralign(this->stub_align());
5026 }
9e69ed50
AM
5027 if (this->last_plt_size_ != this->plt_size_
5028 || this->last_branch_size_ != this->branch_size_)
ec661b9d 5029 {
9e69ed50
AM
5030 this->last_plt_size_ = this->plt_size_;
5031 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
5032 return true;
5033 }
5034 return false;
5035 }
5036
34e0882b
AM
5037 // Add .eh_frame info for this stub section.
5038 void
5039 add_eh_frame(Layout* layout);
be897fb7 5040
34e0882b 5041 // Remove .eh_frame info for this stub section.
be897fb7 5042 void
34e0882b 5043 remove_eh_frame(Layout* layout);
9d5781f8 5044
ec661b9d
AM
5045 Target_powerpc<size, big_endian>*
5046 targ() const
5047 { return targ_; }
6ce78956 5048
cf43a2fe 5049 private:
bdab445c
AM
5050 class Plt_stub_key;
5051 class Plt_stub_key_hash;
bdab445c
AM
5052 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5053 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
5054 class Branch_stub_key;
5055 class Branch_stub_key_hash;
5056 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5057 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
5058
5059 // Alignment of stub section.
ec661b9d 5060 unsigned int
9e69ed50
AM
5061 stub_align() const
5062 {
691d2e9a 5063 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
5064 unsigned int user_align = 1 << parameters->options().plt_align();
5065 return std::max(user_align, min_align);
5066 }
cf43a2fe 5067
91c2b899
AM
5068 // Return the plt offset for the given call stub.
5069 Address
08be3224
AM
5070 plt_off(typename Plt_stub_entries::const_iterator p,
5071 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
5072 {
5073 const Symbol* gsym = p->first.sym_;
5074 if (gsym != NULL)
08be3224 5075 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
5076 else
5077 {
91c2b899
AM
5078 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5079 unsigned int local_sym_index = p->first.locsym_;
08be3224 5080 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
5081 }
5082 }
5083
9e69ed50 5084 // Size of a given plt call stub.
ec661b9d 5085 unsigned int
afd2ea23 5086 plt_call_size(typename Plt_stub_entries::iterator p) const;
34e0882b
AM
5087
5088 unsigned int
5089 plt_call_align(unsigned int bytes) const
5090 {
691d2e9a 5091 unsigned int align = param_plt_align<size>();
9e390558 5092 return (bytes + align - 1) & -align;
9e69ed50 5093 }
ec661b9d
AM
5094
5095 // Return long branch stub size.
5096 unsigned int
afd2ea23 5097 branch_stub_size(typename Branch_stub_entries::iterator p,
32f59844
AM
5098 bool* need_lt);
5099
afd2ea23
AM
5100 void
5101 build_tls_opt_head(unsigned char** pp, bool save_lr);
32f59844 5102
afd2ea23
AM
5103 void
5104 build_tls_opt_tail(unsigned char* p);
ec661b9d 5105
f073a3e8
AM
5106 void
5107 plt_error(const Plt_stub_key& p);
5108
ec661b9d 5109 // Write out stubs.
cf43a2fe
AM
5110 void
5111 do_write(Output_file*);
5112
ec661b9d 5113 // Plt call stub keys.
bdab445c 5114 class Plt_stub_key
cf43a2fe 5115 {
d1a8cabd 5116 public:
bdab445c 5117 Plt_stub_key(const Symbol* sym)
c9824451
AM
5118 : sym_(sym), object_(0), addend_(0), locsym_(0)
5119 { }
5120
bdab445c 5121 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 5122 unsigned int locsym_index)
c9824451
AM
5123 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5124 { }
5125
bdab445c 5126 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5127 const Symbol* sym,
5128 unsigned int r_type,
5129 Address addend)
e5d5f5ed 5130 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
5131 {
5132 if (size != 32)
ec661b9d 5133 this->addend_ = addend;
d1a8cabd 5134 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5135 && (r_type == elfcpp::R_PPC_PLTREL24
5136 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 5137 {
ec661b9d 5138 this->addend_ = addend;
e5d5f5ed 5139 if (this->addend_ >= 32768)
d1a8cabd 5140 this->object_ = object;
cf43a2fe
AM
5141 }
5142 }
5143
bdab445c 5144 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
5145 unsigned int locsym_index,
5146 unsigned int r_type,
5147 Address addend)
e5d5f5ed
AM
5148 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5149 {
5150 if (size != 32)
ec661b9d 5151 this->addend_ = addend;
e5d5f5ed 5152 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
5153 && (r_type == elfcpp::R_PPC_PLTREL24
5154 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 5155 this->addend_ = addend;
e5d5f5ed
AM
5156 }
5157
bdab445c 5158 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
5159 {
5160 return (this->sym_ == that.sym_
5161 && this->object_ == that.object_
e5d5f5ed
AM
5162 && this->addend_ == that.addend_
5163 && this->locsym_ == that.locsym_);
cf43a2fe 5164 }
c9269dff
AM
5165
5166 const Symbol* sym_;
e5d5f5ed
AM
5167 const Sized_relobj_file<size, big_endian>* object_;
5168 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5169 unsigned int locsym_;
cf43a2fe
AM
5170 };
5171
bdab445c 5172 class Plt_stub_key_hash
cf43a2fe 5173 {
d1a8cabd 5174 public:
bdab445c 5175 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
5176 {
5177 return (reinterpret_cast<uintptr_t>(ent.sym_)
5178 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
5179 ^ ent.addend_
5180 ^ ent.locsym_);
cf43a2fe 5181 }
ec661b9d
AM
5182 };
5183
5184 // Long branch stub keys.
32f59844 5185 class Branch_stub_key
ec661b9d
AM
5186 {
5187 public:
a19da04b
AM
5188 Branch_stub_key(Address to)
5189 : dest_(to)
5190 { }
ec661b9d 5191
32f59844 5192 bool operator==(const Branch_stub_key& that) const
ec661b9d 5193 {
a19da04b 5194 return this->dest_ == that.dest_;
ec661b9d 5195 }
cf43a2fe 5196
ec661b9d 5197 Address dest_;
ec661b9d 5198 };
cf43a2fe 5199
32f59844 5200 class Branch_stub_key_hash
ec661b9d
AM
5201 {
5202 public:
32f59844 5203 size_t operator()(const Branch_stub_key& key) const
a19da04b 5204 { return key.dest_; }
ec661b9d 5205 };
cf43a2fe 5206
ec661b9d 5207 // In a sane world this would be a global.
cf43a2fe 5208 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5209 // Map sym/object/addend to stub offset.
ec661b9d
AM
5210 Plt_stub_entries plt_call_stubs_;
5211 // Map destination address to stub offset.
ec661b9d
AM
5212 Branch_stub_entries long_branch_stubs_;
5213 // size of input section
5214 section_size_type orig_data_size_;
5215 // size of stubs
9e69ed50 5216 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5217 // Some rare cases cause (PR/20529) fluctuation in stub table
5218 // size, which leads to an endless relax loop. This is to be fixed
5219 // by, after the first few iterations, allowing only increase of
5220 // stub table size. This variable sets the minimal possible size of
5221 // a stub table, it is zero for the first few iterations, then
5222 // increases monotonically.
5223 Address min_size_threshold_;
d49044c7
AM
5224 // Set if this stub group needs a copy of out-of-line register
5225 // save/restore functions.
5226 bool need_save_res_;
32f59844
AM
5227 // Set when notoc_/r2save_ changes after sizing a stub
5228 bool need_resize_;
5229 // Set when resizing stubs
5230 bool resizing_;
590b87ff
AM
5231 // Per stub table unique identifier.
5232 uint32_t uniq_;
cf43a2fe
AM
5233};
5234
ec661b9d 5235// Add a plt call stub, if we do not already have one for this
d1a8cabd 5236// sym/object/addend combo.
cf43a2fe
AM
5237
5238template<int size, bool big_endian>
a3e60ddb 5239bool
ec661b9d 5240Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5241 Address from,
c9824451 5242 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5243 const Symbol* gsym,
ec661b9d 5244 unsigned int r_type,
7e57d19e
AM
5245 Address addend,
5246 bool tocsave)
cf43a2fe 5247{
bdab445c
AM
5248 Plt_stub_key key(object, gsym, r_type, addend);
5249 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5250 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5251 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5252 if (size == 64)
7ee7ff70 5253 {
32f59844 5254 if (p.second
7ee7ff70
AM
5255 && this->targ_->is_elfv2_localentry0(gsym))
5256 {
5257 p.first->second.localentry0_ = 1;
5258 this->targ_->set_has_localentry0();
5259 }
eb97d024
AM
5260 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5261 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844 5262 {
eb97d024
AM
5263 if (this->targ_->power10_stubs()
5264 && (!this->targ_->power10_stubs_auto()
5265 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5266 {
5267 if (!p.second && !p.first->second.notoc_)
5268 this->need_resize_ = true;
5269 p.first->second.notoc_ = 1;
5270 }
5271 else
5272 {
5273 if (!p.second && !p.first->second.p9notoc_)
5274 this->need_resize_ = true;
5275 p.first->second.p9notoc_ = 1;
5276 }
32f59844 5277 }
afd2ea23 5278 else
32f59844 5279 {
afd2ea23 5280 if (!p.second && !p.first->second.toc_)
32f59844 5281 this->need_resize_ = true;
afd2ea23
AM
5282 p.first->second.toc_ = 1;
5283 if (!tocsave && !p.first->second.localentry0_)
5284 {
5285 if (!p.second && !p.first->second.r2save_)
5286 this->need_resize_ = true;
5287 p.first->second.r2save_ = 1;
5288 }
32f59844
AM
5289 }
5290 }
5291 if (p.second || (this->resizing_ && !p.first->second.iter_))
5292 {
5293 if (this->resizing_)
5294 {
5295 p.first->second.iter_ = 1;
5296 p.first->second.off_ = this->plt_size_;
5297 }
5298 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5299 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5300 this->targ_->set_has_tls_get_addr_opt();
7ee7ff70 5301 }
32f59844 5302 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5303}
5304
e5d5f5ed 5305template<int size, bool big_endian>
a3e60ddb 5306bool
ec661b9d 5307Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5308 Address from,
c9824451 5309 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5310 unsigned int locsym_index,
ec661b9d 5311 unsigned int r_type,
7e57d19e
AM
5312 Address addend,
5313 bool tocsave)
e5d5f5ed 5314{
bdab445c
AM
5315 Plt_stub_key key(object, locsym_index, r_type, addend);
5316 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5317 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5318 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5319 if (size == 64)
7ee7ff70 5320 {
32f59844 5321 if (p.second
7ee7ff70
AM
5322 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5323 {
5324 p.first->second.localentry0_ = 1;
5325 this->targ_->set_has_localentry0();
5326 }
eb97d024
AM
5327 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5328 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
32f59844 5329 {
eb97d024
AM
5330 if (this->targ_->power10_stubs()
5331 && (!this->targ_->power10_stubs_auto()
5332 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5333 {
5334 if (!p.second && !p.first->second.notoc_)
5335 this->need_resize_ = true;
5336 p.first->second.notoc_ = 1;
5337 }
5338 else
5339 {
5340 if (!p.second && !p.first->second.p9notoc_)
5341 this->need_resize_ = true;
5342 p.first->second.p9notoc_ = 1;
5343 }
32f59844 5344 }
afd2ea23 5345 else
32f59844 5346 {
afd2ea23 5347 if (!p.second && !p.first->second.toc_)
32f59844 5348 this->need_resize_ = true;
afd2ea23
AM
5349 p.first->second.toc_ = 1;
5350 if (!tocsave && !p.first->second.localentry0_)
5351 {
5352 if (!p.second && !p.first->second.r2save_)
5353 this->need_resize_ = true;
5354 p.first->second.r2save_ = 1;
5355 }
32f59844 5356 }
7ee7ff70 5357 }
32f59844
AM
5358 if (p.second || (this->resizing_ && !p.first->second.iter_))
5359 {
5360 if (this->resizing_)
5361 {
5362 p.first->second.iter_ = 1;
5363 p.first->second.off_ = this->plt_size_;
5364 }
5365 this->plt_size_ += this->plt_call_size(p.first);
32f59844
AM
5366 }
5367 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5368}
5369
ec661b9d
AM
5370// Find a plt call stub.
5371
cf43a2fe 5372template<int size, bool big_endian>
7e57d19e 5373const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5374Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5375 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5376 const Symbol* gsym,
ec661b9d
AM
5377 unsigned int r_type,
5378 Address addend) const
c9824451 5379{
bdab445c
AM
5380 Plt_stub_key key(object, gsym, r_type, addend);
5381 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5382 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5383 return NULL;
5384 return &p->second;
c9824451
AM
5385}
5386
5387template<int size, bool big_endian>
7e57d19e 5388const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5389Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5390{
bdab445c
AM
5391 Plt_stub_key key(gsym);
5392 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5393 if (p == this->plt_call_stubs_.end())
5394 return NULL;
5395 return &p->second;
cf43a2fe
AM
5396}
5397
e5d5f5ed 5398template<int size, bool big_endian>
7e57d19e 5399const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5400Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5401 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5402 unsigned int locsym_index,
ec661b9d
AM
5403 unsigned int r_type,
5404 Address addend) const
e5d5f5ed 5405{
bdab445c
AM
5406 Plt_stub_key key(object, locsym_index, r_type, addend);
5407 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5408 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5409 return NULL;
5410 return &p->second;
c9824451
AM
5411}
5412
5413template<int size, bool big_endian>
7e57d19e 5414const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5415Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5416 const Sized_relobj_file<size, big_endian>* object,
5417 unsigned int locsym_index) const
5418{
bdab445c
AM
5419 Plt_stub_key key(object, locsym_index);
5420 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5421 if (p == this->plt_call_stubs_.end())
5422 return NULL;
5423 return &p->second;
ec661b9d
AM
5424}
5425
5426// Add a long branch stub if we don't already have one to given
5427// destination.
5428
5429template<int size, bool big_endian>
a3e60ddb 5430bool
ec661b9d 5431Stub_table<size, big_endian>::add_long_branch_entry(
a3e60ddb
AM
5432 unsigned int r_type,
5433 Address from,
d49044c7 5434 Address to,
fa40fbe4 5435 unsigned int other,
d49044c7 5436 bool save_res)
ec661b9d 5437{
a19da04b 5438 Branch_stub_key key(to);
eb97d024 5439 Branch_stub_ent ent(this->branch_size_);
590b87ff 5440 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844 5441 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
eb97d024 5442 if (save_res)
ec661b9d 5443 {
eb97d024 5444 if (!p.second && !p.first->second.save_res_)
afd2ea23 5445 this->need_resize_ = true;
eb97d024
AM
5446 p.first->second.save_res_ = true;
5447 }
5448 else if (size == 64
5449 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5450 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5451 {
5452 if (this->targ_->power10_stubs()
5453 && (!this->targ_->power10_stubs_auto()
5454 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5455 {
5456 if (!p.second && !p.first->second.notoc_)
5457 this->need_resize_ = true;
5458 p.first->second.notoc_ = true;
5459 }
5460 else
5461 {
5462 if (!p.second && !p.first->second.p9notoc_)
5463 this->need_resize_ = true;
5464 p.first->second.p9notoc_ = true;
5465 }
32f59844 5466 }
afd2ea23
AM
5467 else
5468 {
5469 if (!p.second && !p.first->second.toc_)
5470 this->need_resize_ = true;
5471 p.first->second.toc_ = true;
5472 }
e3b53295 5473 if (size == 64 && p.first->second.other_ == 0)
fa40fbe4 5474 p.first->second.other_ = other;
32f59844
AM
5475 if (p.second || (this->resizing_ && !p.first->second.iter_))
5476 {
5477 if (this->resizing_)
5478 {
5479 p.first->second.iter_ = 1;
5480 p.first->second.off_ = this->branch_size_;
5481 }
d49044c7
AM
5482 if (save_res)
5483 this->need_save_res_ = true;
5484 else
5485 {
32f59844
AM
5486 bool need_lt = false;
5487 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5488 this->branch_size_ += stub_size;
5489 if (size == 64 && need_lt)
d49044c7
AM
5490 this->targ_->add_branch_lookup_table(to);
5491 }
ec661b9d 5492 }
32f59844 5493 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5494}
5495
d49044c7 5496// Find long branch stub offset.
ec661b9d
AM
5497
5498template<int size, bool big_endian>
32f59844 5499const typename Stub_table<size, big_endian>::Branch_stub_ent*
a19da04b 5500Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
ec661b9d 5501{
a19da04b 5502 Branch_stub_key key(to);
ec661b9d 5503 typename Branch_stub_entries::const_iterator p
32f59844 5504 = this->long_branch_stubs_.find(key);
d49044c7 5505 if (p == this->long_branch_stubs_.end())
32f59844
AM
5506 return NULL;
5507 return &p->second;
e5d5f5ed
AM
5508}
5509
220f9906
AM
5510template<bool big_endian>
5511static void
5512eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5513{
220f9906
AM
5514 delta /= 4;
5515 if (delta < 64)
5516 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5517 else if (delta < 256)
34e0882b 5518 {
220f9906
AM
5519 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5520 fde.push_back(delta);
5521 }
5522 else if (delta < 65536)
5523 {
5524 fde.resize(fde.size() + 3);
5525 unsigned char *p = &*fde.end() - 3;
5526 *p++ = elfcpp::DW_CFA_advance_loc2;
5527 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5528 }
5529 else
5530 {
5531 fde.resize(fde.size() + 5);
5532 unsigned char *p = &*fde.end() - 5;
5533 *p++ = elfcpp::DW_CFA_advance_loc4;
5534 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5535 }
220f9906
AM
5536}
5537
5538template<typename T>
5539static bool
5540stub_sort(T s1, T s2)
5541{
5542 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5543}
5544
5545// Add .eh_frame info for this stub section. Unlike other linker
5546// generated .eh_frame this is added late in the link, because we
5547// only want the .eh_frame info if this particular stub section is
5548// non-empty.
5549
5550template<int size, bool big_endian>
5551void
5552Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5553{
220f9906
AM
5554 if (size != 64
5555 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5556 return;
5557
5558 // Since we add stub .eh_frame info late, it must be placed
5559 // after all other linker generated .eh_frame info so that
5560 // merge mapping need not be updated for input sections.
5561 // There is no provision to use a different CIE to that used
5562 // by .glink.
5563 if (!this->targ_->has_glink())
5564 return;
5565
afd2ea23 5566 typedef typename Plt_stub_entries::iterator plt_iter;
220f9906
AM
5567 std::vector<plt_iter> calls;
5568 if (!this->plt_call_stubs_.empty())
5569 for (plt_iter cs = this->plt_call_stubs_.begin();
5570 cs != this->plt_call_stubs_.end();
5571 ++cs)
eb97d024
AM
5572 if (cs->second.p9notoc_
5573 || (cs->second.toc_
5574 && cs->second.r2save_
5575 && !cs->second.localentry0_
5576 && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
220f9906
AM
5577 calls.push_back(cs);
5578 if (calls.size() > 1)
5579 std::stable_sort(calls.begin(), calls.end(),
5580 stub_sort<plt_iter>);
5581
5582 typedef typename Branch_stub_entries::const_iterator branch_iter;
5583 std::vector<branch_iter> branches;
e4dff765 5584 if (!this->long_branch_stubs_.empty()
7c1f4227 5585 && !this->targ_->power10_stubs())
220f9906
AM
5586 for (branch_iter bs = this->long_branch_stubs_.begin();
5587 bs != this->long_branch_stubs_.end();
5588 ++bs)
5589 if (bs->second.notoc_)
5590 branches.push_back(bs);
5591 if (branches.size() > 1)
5592 std::stable_sort(branches.begin(), branches.end(),
5593 stub_sort<branch_iter>);
5594
5595 if (calls.empty() && branches.empty())
34e0882b
AM
5596 return;
5597
220f9906
AM
5598 unsigned int last_eh_loc = 0;
5599 // offset pcrel sdata4, size udata4, and augmentation size byte.
5600 std::vector<unsigned char> fde(9, 0);
5601
5602 for (unsigned int i = 0; i < calls.size(); i++)
5603 {
5604 plt_iter cs = calls[i];
5605 unsigned int off = cs->second.off_;
5606 // The __tls_get_addr_opt call stub needs to describe where
5607 // it saves LR, to support exceptions that might be thrown
5608 // from __tls_get_addr, and to support asynchronous exceptions.
5609 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5610 {
5611 off += 7 * 4;
eb97d024
AM
5612 if (cs->second.toc_
5613 && cs->second.r2save_
220f9906
AM
5614 && !cs->second.localentry0_)
5615 {
eb97d024 5616 off += cs->second.tocoff_ + 2 * 4;
220f9906
AM
5617 eh_advance<big_endian>(fde, off - last_eh_loc);
5618 fde.resize(fde.size() + 6);
5619 unsigned char* p = &*fde.end() - 6;
5620 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5621 *p++ = 65;
5622 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
eb97d024 5623 unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
220f9906
AM
5624 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5625 *p++ = elfcpp::DW_CFA_restore_extended;
5626 *p++ = 65;
5627 last_eh_loc = off + delta;
eb97d024 5628 off = cs->second.off_ + 7 * 4;
220f9906
AM
5629 }
5630 }
5631 // notoc stubs also should describe LR changes, to support
5632 // asynchronous exceptions.
eb97d024
AM
5633 if (cs->second.p9notoc_)
5634 {
5635 off += cs->second.p9off_;
5636 off += (cs->second.r2save_ ? 4 : 0) + 8;
5637 eh_advance<big_endian>(fde, off - last_eh_loc);
5638 fde.resize(fde.size() + 6);
5639 unsigned char* p = &*fde.end() - 6;
5640 *p++ = elfcpp::DW_CFA_register;
5641 *p++ = 65;
5642 *p++ = 12;
5643 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5644 *p++ = elfcpp::DW_CFA_restore_extended;
5645 *p++ = 65;
5646 last_eh_loc = off + 8;
5647 }
220f9906
AM
5648 }
5649
5650 for (unsigned int i = 0; i < branches.size(); i++)
5651 {
5652 branch_iter bs = branches[i];
5653 unsigned int off = bs->second.off_ + 8;
5654 eh_advance<big_endian>(fde, off - last_eh_loc);
5655 fde.resize(fde.size() + 6);
5656 unsigned char* p = &*fde.end() - 6;
5657 *p++ = elfcpp::DW_CFA_register;
5658 *p++ = 65;
5659 *p++ = 12;
5660 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5661 *p++ = elfcpp::DW_CFA_restore_extended;
5662 *p++ = 65;
5663 last_eh_loc = off + 8;
5664 }
5665
34e0882b
AM
5666 layout->add_eh_frame_for_plt(this,
5667 Eh_cie<size>::eh_frame_cie,
5668 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5669 &*fde.begin(), fde.size());
34e0882b
AM
5670}
5671
5672template<int size, bool big_endian>
5673void
5674Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5675{
220f9906
AM
5676 if (size == 64
5677 && parameters->options().ld_generated_unwind_info()
5678 && this->targ_->has_glink())
5679 layout->remove_eh_frame_for_plt(this,
5680 Eh_cie<size>::eh_frame_cie,
5681 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5682}
5683
ec661b9d
AM
5684// A class to handle .glink.
5685
5686template<int size, bool big_endian>
5687class Output_data_glink : public Output_section_data
5688{
5689 public:
9055360d
AM
5690 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5691 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5692
5693 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5694 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5695 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5696 { }
5697
9d5781f8 5698 void
9055360d 5699 add_eh_frame(Layout* layout);
9d5781f8 5700
9055360d
AM
5701 void
5702 add_global_entry(const Symbol*);
5703
5704 Address
5705 find_global_entry(const Symbol*) const;
5706
9e390558
AM
5707 unsigned int
5708 global_entry_align(unsigned int off) const
5709 {
691d2e9a 5710 unsigned int align = param_plt_align<size>();
9e390558
AM
5711 return (off + align - 1) & -align;
5712 }
5713
5714 unsigned int
5715 global_entry_off() const
5716 {
5717 return this->global_entry_align(this->end_branch_table_);
5718 }
5719
9055360d
AM
5720 Address
5721 global_entry_address() const
5722 {
5723 gold_assert(this->is_data_size_valid());
9e390558
AM
5724 return this->address() + this->global_entry_off();
5725 }
5726
5727 int
5728 pltresolve_size() const
5729 {
5730 if (size == 64)
5731 return (8
63e5eea2
AM
5732 + (this->targ_->abiversion() < 2 ? 11 * 4
5733 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
9e390558 5734 return 16 * 4;
9d5781f8
AM
5735 }
5736
ec661b9d
AM
5737 protected:
5738 // Write to a map file.
5739 void
5740 do_print_to_mapfile(Mapfile* mapfile) const
5741 { mapfile->print_output_data(this, _("** glink")); }
5742
5743 private:
5744 void
5745 set_final_data_size();
5746
5747 // Write out .glink
5748 void
5749 do_write(Output_file*);
5750
5751 // Allows access to .got and .plt for do_write.
5752 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5753
5754 // Map sym to stub offset.
5755 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5756 Global_entry_stub_entries global_entry_stubs_;
5757
5758 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5759};
5760
9055360d
AM
5761template<int size, bool big_endian>
5762void
5763Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5764{
5765 if (!parameters->options().ld_generated_unwind_info())
5766 return;
5767
5768 if (size == 64)
5769 {
5770 if (this->targ_->abiversion() < 2)
5771 layout->add_eh_frame_for_plt(this,
5772 Eh_cie<64>::eh_frame_cie,
5773 sizeof (Eh_cie<64>::eh_frame_cie),
5774 glink_eh_frame_fde_64v1,
5775 sizeof (glink_eh_frame_fde_64v1));
63e5eea2
AM
5776 else if (this->targ_->has_localentry0())
5777 layout->add_eh_frame_for_plt(this,
5778 Eh_cie<64>::eh_frame_cie,
5779 sizeof (Eh_cie<64>::eh_frame_cie),
5780 glink_eh_frame_fde_64v2_localentry0,
5781 sizeof (glink_eh_frame_fde_64v2));
9055360d
AM
5782 else
5783 layout->add_eh_frame_for_plt(this,
5784 Eh_cie<64>::eh_frame_cie,
5785 sizeof (Eh_cie<64>::eh_frame_cie),
5786 glink_eh_frame_fde_64v2,
5787 sizeof (glink_eh_frame_fde_64v2));
5788 }
5789 else
5790 {
5791 // 32-bit .glink can use the default since the CIE return
5792 // address reg, LR, is valid.
5793 layout->add_eh_frame_for_plt(this,
5794 Eh_cie<32>::eh_frame_cie,
5795 sizeof (Eh_cie<32>::eh_frame_cie),
5796 default_fde,
5797 sizeof (default_fde));
5798 // Except where LR is used in a PIC __glink_PLTresolve.
5799 if (parameters->options().output_is_position_independent())
5800 layout->add_eh_frame_for_plt(this,
5801 Eh_cie<32>::eh_frame_cie,
5802 sizeof (Eh_cie<32>::eh_frame_cie),
5803 glink_eh_frame_fde_32,
5804 sizeof (glink_eh_frame_fde_32));
5805 }
5806}
5807
5808template<int size, bool big_endian>
5809void
5810Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5811{
9e390558 5812 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5813 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5814 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5815 if (p.second)
407aa07c 5816 this->ge_size_ = off + 16;
9055360d
AM
5817}
5818
5819template<int size, bool big_endian>
5820typename Output_data_glink<size, big_endian>::Address
5821Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5822{
5823 typename Global_entry_stub_entries::const_iterator p
5824 = this->global_entry_stubs_.find(gsym);
5825 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5826}
5827
cf43a2fe
AM
5828template<int size, bool big_endian>
5829void
5830Output_data_glink<size, big_endian>::set_final_data_size()
5831{
ec661b9d
AM
5832 unsigned int count = this->targ_->plt_entry_count();
5833 section_size_type total = 0;
cf43a2fe
AM
5834
5835 if (count != 0)
5836 {
5837 if (size == 32)
5838 {
cf43a2fe
AM
5839 // space for branch table
5840 total += 4 * (count - 1);
5841
5842 total += -total & 15;
9e390558 5843 total += this->pltresolve_size();
cf43a2fe
AM
5844 }
5845 else
5846 {
9e390558 5847 total += this->pltresolve_size();
cf43a2fe
AM
5848
5849 // space for branch table
b4f7960d
AM
5850 total += 4 * count;
5851 if (this->targ_->abiversion() < 2)
5852 {
5853 total += 4 * count;
5854 if (count > 0x8000)
5855 total += 4 * (count - 0x8000);
5856 }
cf43a2fe
AM
5857 }
5858 }
9055360d 5859 this->end_branch_table_ = total;
9e390558 5860 total = this->global_entry_align(total);
9055360d 5861 total += this->ge_size_;
cf43a2fe
AM
5862
5863 this->set_data_size(total);
5864}
5865
590b87ff
AM
5866// Define symbols on stubs, identifying the stub.
5867
5868template<int size, bool big_endian>
5869void
5870Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5871{
5872 if (!this->plt_call_stubs_.empty())
5873 {
5874 // The key for the plt call stub hash table includes addresses,
5875 // therefore traversal order depends on those addresses, which
5876 // can change between runs if gold is a PIE. Unfortunately the
5877 // output .symtab ordering depends on the order in which symbols
5878 // are added to the linker symtab. We want reproducible output
5879 // so must sort the call stub symbols.
afd2ea23 5880 typedef typename Plt_stub_entries::iterator plt_iter;
590b87ff
AM
5881 std::vector<plt_iter> sorted;
5882 sorted.resize(this->plt_call_stubs_.size());
5883
5884 for (plt_iter cs = this->plt_call_stubs_.begin();
5885 cs != this->plt_call_stubs_.end();
5886 ++cs)
bdab445c 5887 sorted[cs->second.indx_] = cs;
590b87ff
AM
5888
5889 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5890 {
5891 plt_iter cs = sorted[i];
5892 char add[10];
5893 add[0] = 0;
5894 if (cs->first.addend_ != 0)
5895 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5896 char obj[10];
5897 obj[0] = 0;
5898 if (cs->first.object_)
590b87ff
AM
5899 {
5900 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5901 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5902 sprintf(obj, "%x:", ppcobj->uniq());
5903 }
5904 char localname[9];
5905 const char *symname;
5906 if (cs->first.sym_ == NULL)
5907 {
5908 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5909 symname = localname;
5910 }
34e0882b
AM
5911 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5912 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5913 else
5914 symname = cs->first.sym_->name();
94de2a2c
JC
5915 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5916 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5917 Address value
5918 = this->stub_address() - this->address() + cs->second.off_;
eb97d024 5919 unsigned int stub_size = this->plt_call_size(cs);
590b87ff
AM
5920 this->targ_->define_local(symtab, name, this, value, stub_size);
5921 }
5922 }
5923
afd2ea23 5924 typedef typename Branch_stub_entries::iterator branch_iter;
590b87ff
AM
5925 for (branch_iter bs = this->long_branch_stubs_.begin();
5926 bs != this->long_branch_stubs_.end();
5927 ++bs)
5928 {
32f59844 5929 if (bs->second.save_res_)
590b87ff
AM
5930 continue;
5931
5932 char* name = new char[8 + 13 + 16 + 1];
5933 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5934 static_cast<unsigned long long>(bs->first.dest_));
5935 Address value = (this->stub_address() - this->address()
32f59844
AM
5936 + this->plt_size_ + bs->second.off_);
5937 bool need_lt = false;
5938 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5939 this->targ_->define_local(symtab, name, this, value, stub_size);
5940 }
5941}
5942
32f59844
AM
5943// Emit the start of a __tls_get_addr_opt plt call stub.
5944
5945template<int size, bool big_endian>
afd2ea23
AM
5946void
5947Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5948 bool save_lr)
32f59844 5949{
afd2ea23
AM
5950 unsigned char* p = *pp;
5951 if (size == 64)
32f59844 5952 {
afd2ea23
AM
5953 write_insn<big_endian>(p, ld_11_3 + 0);
5954 p += 4;
5955 write_insn<big_endian>(p, ld_12_3 + 8);
5956 p += 4;
5957 write_insn<big_endian>(p, mr_0_3);
5958 p += 4;
5959 write_insn<big_endian>(p, cmpdi_11_0);
5960 p += 4;
5961 write_insn<big_endian>(p, add_3_12_13);
5962 p += 4;
5963 write_insn<big_endian>(p, beqlr);
5964 p += 4;
5965 write_insn<big_endian>(p, mr_3_0);
5966 p += 4;
5967 if (save_lr)
32f59844 5968 {
afd2ea23 5969 write_insn<big_endian>(p, mflr_11);
32f59844 5970 p += 4;
afd2ea23 5971 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
32f59844
AM
5972 p += 4;
5973 }
32f59844 5974 }
afd2ea23 5975 else
32f59844 5976 {
afd2ea23 5977 write_insn<big_endian>(p, lwz_11_3 + 0);
32f59844 5978 p += 4;
afd2ea23 5979 write_insn<big_endian>(p, lwz_12_3 + 4);
32f59844 5980 p += 4;
afd2ea23 5981 write_insn<big_endian>(p, mr_0_3);
32f59844 5982 p += 4;
afd2ea23
AM
5983 write_insn<big_endian>(p, cmpwi_11_0);
5984 p += 4;
5985 write_insn<big_endian>(p, add_3_12_2);
5986 p += 4;
5987 write_insn<big_endian>(p, beqlr);
5988 p += 4;
5989 write_insn<big_endian>(p, mr_3_0);
5990 p += 4;
5991 write_insn<big_endian>(p, nop);
32f59844 5992 p += 4;
32f59844 5993 }
afd2ea23
AM
5994 *pp = p;
5995}
5996
5997// Emit the tail of a __tls_get_addr_opt plt call stub.
5998
5999template<int size, bool big_endian>
6000void
6001Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
6002{
6003 write_insn<big_endian>(p, bctrl);
6004 p += 4;
6005 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
6006 p += 4;
6007 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
6008 p += 4;
6009 write_insn<big_endian>(p, mtlr_11);
6010 p += 4;
6011 write_insn<big_endian>(p, blr);
32f59844
AM
6012}
6013
e4dff765
AM
6014// Emit pc-relative plt call stub code.
6015
6016template<bool big_endian>
6017static unsigned char*
7c1f4227 6018build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
e4dff765
AM
6019{
6020 uint64_t insn;
6021 if (off - odd + (1ULL << 33) < 1ULL << 34)
6022 {
6023 off -= odd;
6024 if (odd)
6025 {
6026 write_insn<big_endian>(p, nop);
6027 p += 4;
6028 }
6029 if (load)
6030 insn = pld_12_pc;
6031 else
6032 insn = paddi_12_pc;
6033 insn |= d34(off);
6034 write_insn<big_endian>(p, insn >> 32);
6035 p += 4;
6036 write_insn<big_endian>(p, insn & 0xffffffff);
6037 }
6038 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6039 {
6040 off -= 8 - odd;
6041 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6042 p += 4;
6043 if (!odd)
6044 {
6045 write_insn<big_endian>(p, sldi_11_11_34);
6046 p += 4;
6047 }
6048 insn = paddi_12_pc | d34(off);
6049 write_insn<big_endian>(p, insn >> 32);
6050 p += 4;
6051 write_insn<big_endian>(p, insn & 0xffffffff);
6052 p += 4;
6053 if (odd)
6054 {
6055 write_insn<big_endian>(p, sldi_11_11_34);
6056 p += 4;
6057 }
6058 if (load)
6059 write_insn<big_endian>(p, ldx_12_11_12);
6060 else
6061 write_insn<big_endian>(p, add_12_11_12);
6062 }
6063 else
6064 {
6065 off -= odd + 8;
6066 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6067 p += 4;
6068 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6069 p += 4;
6070 if (odd)
6071 {
6072 write_insn<big_endian>(p, sldi_11_11_34);
6073 p += 4;
6074 }
6075 insn = paddi_12_pc | d34(off);
6076 write_insn<big_endian>(p, insn >> 32);
6077 p += 4;
6078 write_insn<big_endian>(p, insn & 0xffffffff);
6079 p += 4;
6080 if (!odd)
6081 {
6082 write_insn<big_endian>(p, sldi_11_11_34);
6083 p += 4;
6084 }
6085 if (load)
6086 write_insn<big_endian>(p, ldx_12_11_12);
6087 else
6088 write_insn<big_endian>(p, add_12_11_12);
6089 }
6090 p += 4;
6091 return p;
6092}
6093
32f59844
AM
6094// Gets the address of a label (1:) in r11 and builds an offset in r12,
6095// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6096// mflr %r12
6097// bcl 20,31,1f
6098// 1: mflr %r11
6099// mtlr %r12
6100// lis %r12,xxx-1b@highest
6101// ori %r12,%r12,xxx-1b@higher
6102// sldi %r12,%r12,32
6103// oris %r12,%r12,xxx-1b@high
6104// ori %r12,%r12,xxx-1b@l
6105// add/ldx %r12,%r11,%r12
6106
6107template<bool big_endian>
6108static unsigned char*
6109build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6110{
6111 write_insn<big_endian>(p, mflr_12);
6112 p += 4;
6113 write_insn<big_endian>(p, bcl_20_31);
6114 p += 4;
6115 write_insn<big_endian>(p, mflr_11);
6116 p += 4;
6117 write_insn<big_endian>(p, mtlr_12);
6118 p += 4;
6119 if (off + 0x8000 < 0x10000)
6120 {
6121 if (load)
6122 write_insn<big_endian>(p, ld_12_11 + l(off));
6123 else
6124 write_insn<big_endian>(p, addi_12_11 + l(off));
6125 }
6126 else if (off + 0x80008000ULL < 0x100000000ULL)
6127 {
6128 write_insn<big_endian>(p, addis_12_11 + ha(off));
6129 p += 4;
6130 if (load)
6131 write_insn<big_endian>(p, ld_12_12 + l(off));
6132 else
6133 write_insn<big_endian>(p, addi_12_12 + l(off));
6134 }
6135 else
6136 {
6137 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6138 {
6139 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6140 p += 4;
6141 }
6142 else
6143 {
6144 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6145 p += 4;
6146 if (((off >> 32) & 0xffff) != 0)
6147 {
6148 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6149 p += 4;
6150 }
6151 }
6152 if (((off >> 32) & 0xffffffffULL) != 0)
6153 {
6154 write_insn<big_endian>(p, sldi_12_12_32);
6155 p += 4;
6156 }
6157 if (hi(off) != 0)
6158 {
6159 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6160 p += 4;
6161 }
6162 if (l(off) != 0)
6163 {
6164 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6165 p += 4;
6166 }
6167 if (load)
6168 write_insn<big_endian>(p, ldx_12_11_12);
6169 else
6170 write_insn<big_endian>(p, add_12_11_12);
6171 }
6172 p += 4;
6173 return p;
6174}
6175
6176// Size of a given plt call stub.
6177
6178template<int size, bool big_endian>
6179unsigned int
6180Stub_table<size, big_endian>::plt_call_size(
afd2ea23 6181 typename Plt_stub_entries::iterator p) const
32f59844
AM
6182{
6183 if (size == 32)
6184 {
eb97d024
AM
6185 unsigned int bytes = 4 * 4;
6186 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6187 bytes = 12 * 4;
6188 return this->plt_call_align(bytes);
32f59844
AM
6189 }
6190
6191 const Output_data_plt_powerpc<size, big_endian>* plt;
6192 uint64_t plt_addr = this->plt_off(p, &plt);
6193 plt_addr += plt->address();
afd2ea23
AM
6194 if (this->targ_->power10_stubs()
6195 && this->targ_->power10_stubs_auto())
32f59844 6196 {
afd2ea23
AM
6197 unsigned int bytes = 0;
6198 if (p->second.notoc_)
6199 {
6200 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6201 bytes = 7 * 4;
6202 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6203 uint64_t odd = from & 4;
6204 uint64_t off = plt_addr - from;
6205 if (off - odd + (1ULL << 33) < 1ULL << 34)
6206 bytes += odd + 4 * 4;
6207 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6208 bytes += 7 * 4;
6209 else
6210 bytes += 8 * 4;
6211 bytes = this->plt_call_align(bytes);
6212 }
afd2ea23
AM
6213 if (p->second.toc_)
6214 {
6215 p->second.tocoff_ = bytes;
6216 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6217 {
6218 bytes += 7 * 4;
6219 if (p->second.r2save_ && !p->second.localentry0_)
eb97d024 6220 bytes += 2 * 4 + 4 * 4;
afd2ea23
AM
6221 }
6222 if (p->second.r2save_)
6223 bytes += 4;
a19da04b 6224 uint64_t got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6225 uint64_t off = plt_addr - got_addr;
6226 bytes += 3 * 4 + 4 * (ha(off) != 0);
eb97d024
AM
6227 p->second.tsize_ = bytes - p->second.tocoff_;
6228 bytes = this->plt_call_align(bytes);
afd2ea23 6229 }
eb97d024
AM
6230 if (p->second.p9notoc_)
6231 {
6232 p->second.p9off_ = bytes;
6233 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6234 bytes += 7 * 4;
6235 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6236 uint64_t off = plt_addr - from;
6237 if (off + 0x8000 < 0x10000)
6238 bytes += 7 * 4;
6239 else if (off + 0x80008000ULL < 0x100000000ULL)
6240 bytes += 8 * 4;
6241 else
6242 {
6243 bytes += 8 * 4;
6244 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6245 && ((off >> 32) & 0xffff) != 0)
6246 bytes += 4;
6247 if (((off >> 32) & 0xffffffffULL) != 0)
6248 bytes += 4;
6249 if (hi(off) != 0)
6250 bytes += 4;
6251 if (l(off) != 0)
6252 bytes += 4;
6253 }
6254 bytes = this->plt_call_align(bytes);
6255 }
6256 return bytes;
32f59844 6257 }
afd2ea23
AM
6258 else
6259 {
6260 unsigned int bytes = 0;
6261 unsigned int tail = 0;
6262 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6263 {
6264 bytes = 7 * 4;
6265 if (p->second.r2save_ && !p->second.localentry0_)
6266 {
6267 bytes = 9 * 4;
6268 tail = 4 * 4;
6269 }
6270 }
32f59844 6271
afd2ea23
AM
6272 if (p->second.r2save_)
6273 bytes += 4;
32f59844 6274
afd2ea23
AM
6275 if (this->targ_->power10_stubs())
6276 {
6277 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6278 uint64_t odd = from & 4;
6279 uint64_t off = plt_addr - from;
6280 if (off - odd + (1ULL << 33) < 1ULL << 34)
6281 bytes += odd + 4 * 4;
6282 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6283 bytes += 7 * 4;
6284 else
6285 bytes += 8 * 4;
eb97d024 6286 return this->plt_call_align(bytes + tail);
afd2ea23 6287 }
e4dff765 6288
eb97d024 6289 if (p->second.p9notoc_)
32f59844 6290 {
afd2ea23
AM
6291 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6292 uint64_t off = plt_addr - from;
6293 if (off + 0x8000 < 0x10000)
6294 bytes += 7 * 4;
6295 else if (off + 0x80008000ULL < 0x100000000ULL)
6296 bytes += 8 * 4;
6297 else
6298 {
6299 bytes += 8 * 4;
6300 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6301 && ((off >> 32) & 0xffff) != 0)
6302 bytes += 4;
6303 if (((off >> 32) & 0xffffffffULL) != 0)
6304 bytes += 4;
6305 if (hi(off) != 0)
6306 bytes += 4;
6307 if (l(off) != 0)
6308 bytes += 4;
6309 }
eb97d024 6310 return this->plt_call_align(bytes + tail);
32f59844 6311 }
32f59844 6312
a19da04b 6313 uint64_t got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6314 uint64_t off = plt_addr - got_addr;
6315 bytes += 3 * 4 + 4 * (ha(off) != 0);
6316 if (this->targ_->abiversion() < 2)
6317 {
6318 bool static_chain = parameters->options().plt_static_chain();
6319 bool thread_safe = this->targ_->plt_thread_safe();
6320 bytes += (4
6321 + 4 * static_chain
6322 + 8 * thread_safe
6323 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6324 }
eb97d024 6325 return this->plt_call_align(bytes + tail);
32f59844 6326 }
32f59844
AM
6327}
6328
6329// Return long branch stub size.
6330
6331template<int size, bool big_endian>
6332unsigned int
6333Stub_table<size, big_endian>::branch_stub_size(
afd2ea23 6334 typename Branch_stub_entries::iterator p,
32f59844
AM
6335 bool* need_lt)
6336{
6337 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6338 if (size == 32)
6339 {
6340 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6341 return 4;
6342 if (parameters->options().output_is_position_independent())
6343 return 32;
6344 return 16;
6345 }
6346
6347 uint64_t off = p->first.dest_ - loc;
afd2ea23 6348 unsigned int bytes = 0;
32f59844
AM
6349 if (p->second.notoc_)
6350 {
7c1f4227 6351 if (this->targ_->power10_stubs())
e4dff765
AM
6352 {
6353 Address odd = loc & 4;
6354 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6355 bytes = odd + 12;
6356 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6357 bytes = odd + 16;
6358 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6359 bytes = 28;
6360 else
6361 bytes = 32;
6362 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6363 return bytes;
6364 p->second.tocoff_ = bytes;
6365 }
6366 else
6367 {
6368 off -= 8;
6369 if (off + 0x8000 < 0x10000)
6370 return 24;
6371 if (off + 0x80008000ULL < 0x100000000ULL)
6372 {
6373 if (off + 24 + (1 << 25) < 2 << 25)
6374 return 28;
6375 return 32;
6376 }
6377
6378 bytes = 32;
6379 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6380 && ((off >> 32) & 0xffff) != 0)
6381 bytes += 4;
6382 if (((off >> 32) & 0xffffffffULL) != 0)
6383 bytes += 4;
6384 if (hi(off) != 0)
6385 bytes += 4;
6386 if (l(off) != 0)
6387 bytes += 4;
6388 return bytes;
6389 }
32f59844
AM
6390 }
6391
fa40fbe4 6392 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
32f59844 6393 if (off + (1 << 25) < 2 << 25)
afd2ea23
AM
6394 return bytes + 4;
6395 if (!this->targ_->power10_stubs()
6396 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
e4dff765 6397 *need_lt = true;
afd2ea23 6398 return bytes + 16;
32f59844
AM
6399}
6400
f073a3e8
AM
6401template<int size, bool big_endian>
6402void
6403Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6404{
6405 if (p.sym_)
6406 gold_error(_("linkage table error against `%s'"),
6407 p.sym_->demangled_name().c_str());
6408 else
6409 gold_error(_("linkage table error against `%s:[local %u]'"),
6410 p.object_->name().c_str(),
6411 p.locsym_);
6412}
6413
ec661b9d 6414// Write out plt and long branch stub code.
cf43a2fe
AM
6415
6416template<int size, bool big_endian>
6417void
ec661b9d 6418Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6419{
ec661b9d
AM
6420 if (this->plt_call_stubs_.empty()
6421 && this->long_branch_stubs_.empty())
6422 return;
6423
6424 const section_size_type start_off = this->offset();
6425 const section_size_type off = this->stub_offset();
42cacb20 6426 const section_size_type oview_size =
ec661b9d 6427 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6428 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6429 unsigned char* p;
42cacb20 6430
e4dff765 6431 if (size == 64
7c1f4227 6432 && this->targ_->power10_stubs())
e4dff765
AM
6433 {
6434 if (!this->plt_call_stubs_.empty())
6435 {
6436 // Write out plt call stubs.
6437 typename Plt_stub_entries::const_iterator cs;
6438 for (cs = this->plt_call_stubs_.begin();
6439 cs != this->plt_call_stubs_.end();
6440 ++cs)
6441 {
6442 p = oview + cs->second.off_;
e4dff765
AM
6443 const Output_data_plt_powerpc<size, big_endian>* plt;
6444 Address pltoff = this->plt_off(cs, &plt);
6445 Address plt_addr = pltoff + plt->address();
afd2ea23
AM
6446 if (this->targ_->power10_stubs_auto())
6447 {
6448 if (cs->second.notoc_)
6449 {
6450 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6451 this->build_tls_opt_head(&p, false);
6452 Address from = this->stub_address() + (p - oview);
6453 Address delta = plt_addr - from;
6454 p = build_power10_offset<big_endian>(p, delta, from & 4,
6455 true);
6456 write_insn<big_endian>(p, mtctr_12);
6457 p += 4;
6458 write_insn<big_endian>(p, bctr);
6459 p += 4;
6460 p = oview + this->plt_call_align(p - oview);
6461 }
6462 if (cs->second.toc_)
6463 {
6464 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6465 {
6466 bool save_lr
6467 = cs->second.r2save_ && !cs->second.localentry0_;
6468 this->build_tls_opt_head(&p, save_lr);
6469 }
a19da04b 6470 Address got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6471 Address off = plt_addr - got_addr;
6472
6473 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6474 this->plt_error(cs->first);
6475
6476 if (cs->second.r2save_)
6477 {
6478 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6479 p += 4;
6480 }
6481 if (ha(off) != 0)
6482 {
6483 write_insn<big_endian>(p, addis_12_2 + ha(off));
6484 p += 4;
6485 write_insn<big_endian>(p, ld_12_12 + l(off));
6486 p += 4;
6487 }
6488 else
6489 {
6490 write_insn<big_endian>(p, ld_12_2 + l(off));
6491 p += 4;
6492 }
6493 write_insn<big_endian>(p, mtctr_12);
6494 p += 4;
6495 if (cs->second.r2save_
6496 && !cs->second.localentry0_
6497 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6498 this->build_tls_opt_tail(p);
6499 else
6500 write_insn<big_endian>(p, bctr);
6501 }
eb97d024
AM
6502 if (cs->second.p9notoc_)
6503 {
6504 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6505 this->build_tls_opt_head(&p, false);
6506 Address from = this->stub_address() + (p - oview);
6507 Address delta = plt_addr - from;
6508 p = build_notoc_offset<big_endian>(p, delta, true);
6509 write_insn<big_endian>(p, mtctr_12);
6510 p += 4;
6511 write_insn<big_endian>(p, bctr);
6512 p += 4;
6513 p = oview + this->plt_call_align(p - oview);
6514 }
afd2ea23
AM
6515 }
6516 else
6517 {
6518 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6519 {
6520 bool save_lr
6521 = cs->second.r2save_ && !cs->second.localentry0_;
6522 this->build_tls_opt_head(&p, save_lr);
6523 }
6524 if (cs->second.r2save_)
6525 {
6526 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6527 p += 4;
6528 }
6529 Address from = this->stub_address() + (p - oview);
6530 Address delta = plt_addr - from;
6531 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6532 write_insn<big_endian>(p, mtctr_12);
6533 p += 4;
6534 if (cs->second.r2save_
6535 && !cs->second.localentry0_
6536 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6537 this->build_tls_opt_tail(p);
6538 else
6539 write_insn<big_endian>(p, bctr);
6540 }
e4dff765
AM
6541 }
6542 }
6543
6544 // Write out long branch stubs.
6545 typename Branch_stub_entries::const_iterator bs;
6546 for (bs = this->long_branch_stubs_.begin();
6547 bs != this->long_branch_stubs_.end();
6548 ++bs)
6549 {
6550 if (bs->second.save_res_)
6551 continue;
6552 Address off = this->plt_size_ + bs->second.off_;
6553 p = oview + off;
6554 Address loc = this->stub_address() + off;
6555 Address delta = bs->first.dest_ - loc;
afd2ea23 6556 if (this->targ_->power10_stubs_auto())
e4dff765 6557 {
afd2ea23
AM
6558 if (bs->second.notoc_)
6559 {
6560 unsigned char* startp = p;
6561 p = build_power10_offset<big_endian>(p, delta,
6562 loc & 4, false);
6563 delta -= p - startp;
6564 startp = p;
6565 if (delta + (1 << 25) < 2 << 25)
6566 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6567 else
6568 {
6569 write_insn<big_endian>(p, mtctr_12);
6570 p += 4;
6571 write_insn<big_endian>(p, bctr);
6572 }
6573 p += 4;
6574 delta -= p - startp;
6575 }
6576 if (bs->second.toc_)
6577 {
fa40fbe4 6578 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6579 if (delta + (1 << 25) >= 2 << 25)
6580 {
6581 Address brlt_addr
6582 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6583 gold_assert(brlt_addr != invalid_address);
6584 brlt_addr += this->targ_->brlt_section()->address();
a19da04b 6585 Address got_addr = this->targ_->toc_pointer();
afd2ea23
AM
6586 Address brltoff = brlt_addr - got_addr;
6587 if (ha(brltoff) == 0)
6588 {
6589 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6590 p += 4;
6591 }
6592 else
6593 {
6594 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6595 p += 4;
6596 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6597 p += 4;
6598 }
6599 }
6600 if (delta + (1 << 25) < 2 << 25)
6601 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6602 else
6603 {
6604 write_insn<big_endian>(p, mtctr_12);
6605 p += 4;
6606 write_insn<big_endian>(p, bctr);
6607 }
6608 }
eb97d024
AM
6609 if (bs->second.p9notoc_)
6610 {
6611 unsigned char* startp = p;
6612 p = build_notoc_offset<big_endian>(p, delta, false);
6613 delta -= p - startp;
6614 startp = p;
6615 if (delta + (1 << 25) < 2 << 25)
6616 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6617 else
6618 {
6619 write_insn<big_endian>(p, mtctr_12);
6620 p += 4;
6621 write_insn<big_endian>(p, bctr);
6622 }
6623 p += 4;
6624 delta -= p - startp;
6625 }
e4dff765 6626 }
e4dff765
AM
6627 else
6628 {
fa40fbe4
AM
6629 if (!bs->second.notoc_)
6630 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
afd2ea23
AM
6631 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6632 {
6633 unsigned char* startp = p;
6634 p = build_power10_offset<big_endian>(p, delta,
6635 loc & 4, false);
6636 delta -= p - startp;
6637 }
6638 if (delta + (1 << 25) < 2 << 25)
6639 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6640 else
6641 {
6642 write_insn<big_endian>(p, mtctr_12);
6643 p += 4;
6644 write_insn<big_endian>(p, bctr);
6645 }
e4dff765
AM
6646 }
6647 }
6648 }
6649 else if (size == 64)
cf43a2fe 6650 {
c9269dff 6651
32f59844
AM
6652 if (!this->plt_call_stubs_.empty()
6653 && this->targ_->abiversion() >= 2)
cf43a2fe 6654 {
32f59844
AM
6655 // Write out plt call stubs for ELFv2.
6656 typename Plt_stub_entries::const_iterator cs;
6657 for (cs = this->plt_call_stubs_.begin();
6658 cs != this->plt_call_stubs_.end();
6659 ++cs)
6660 {
6661 const Output_data_plt_powerpc<size, big_endian>* plt;
6662 Address pltoff = this->plt_off(cs, &plt);
6663 Address plt_addr = pltoff + plt->address();
6664
6665 p = oview + cs->second.off_;
afd2ea23
AM
6666 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6667 {
6668 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6669 this->build_tls_opt_head(&p, save_lr);
6670 }
32f59844
AM
6671 if (cs->second.r2save_)
6672 {
6673 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6674 p += 4;
6675 }
eb97d024 6676 if (cs->second.p9notoc_)
32f59844
AM
6677 {
6678 Address from = this->stub_address() + (p - oview) + 8;
6679 Address off = plt_addr - from;
6680 p = build_notoc_offset<big_endian>(p, off, true);
6681 }
6682 else
6683 {
a19da04b 6684 Address got_addr = this->targ_->toc_pointer();
32f59844
AM
6685 Address off = plt_addr - got_addr;
6686
6687 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6688 this->plt_error(cs->first);
6689
6690 if (ha(off) != 0)
6691 {
6692 write_insn<big_endian>(p, addis_12_2 + ha(off));
6693 p += 4;
6694 write_insn<big_endian>(p, ld_12_12 + l(off));
6695 p += 4;
6696 }
6697 else
6698 {
6699 write_insn<big_endian>(p, ld_12_2 + l(off));
6700 p += 4;
6701 }
6702 }
6703 write_insn<big_endian>(p, mtctr_12);
6704 p += 4;
afd2ea23
AM
6705 if (cs->second.r2save_
6706 && !cs->second.localentry0_
6707 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6708 this->build_tls_opt_tail(p);
6709 else
32f59844
AM
6710 write_insn<big_endian>(p, bctr);
6711 }
6712 }
6713 else if (!this->plt_call_stubs_.empty())
6714 {
6715 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6716 typename Plt_stub_entries::const_iterator cs;
6717 for (cs = this->plt_call_stubs_.begin();
6718 cs != this->plt_call_stubs_.end();
6719 ++cs)
e5d5f5ed 6720 {
08be3224
AM
6721 const Output_data_plt_powerpc<size, big_endian>* plt;
6722 Address pltoff = this->plt_off(cs, &plt);
6723 Address plt_addr = pltoff + plt->address();
a19da04b 6724 Address got_addr = this->targ_->toc_pointer();
9e69ed50 6725 Address off = plt_addr - got_addr;
ec661b9d 6726
32f59844
AM
6727 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6728 || cs->second.notoc_)
f073a3e8 6729 this->plt_error(cs->first);
ec661b9d 6730
32f59844
AM
6731 bool static_chain = parameters->options().plt_static_chain();
6732 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6733 bool use_fake_dep = false;
6734 Address cmp_branch_off = 0;
407aa07c 6735 if (thread_safe)
9e69ed50
AM
6736 {
6737 unsigned int pltindex
6738 = ((pltoff - this->targ_->first_plt_entry_offset())
6739 / this->targ_->plt_entry_size());
6740 Address glinkoff
9e390558 6741 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6742 + pltindex * 8);
6743 if (pltindex > 32768)
6744 glinkoff += (pltindex - 32768) * 4;
6745 Address to
6746 = this->targ_->glink_section()->address() + glinkoff;
6747 Address from
7e57d19e
AM
6748 = (this->stub_address() + cs->second.off_ + 20
6749 + 4 * cs->second.r2save_
9e69ed50
AM
6750 + 4 * (ha(off) != 0)
6751 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6752 + 4 * static_chain);
6753 cmp_branch_off = to - from;
6754 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6755 }
6756
bdab445c 6757 p = oview + cs->second.off_;
afd2ea23
AM
6758 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6759 {
6760 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6761 this->build_tls_opt_head(&p, save_lr);
6762 use_fake_dep = thread_safe;
6763 }
32f59844 6764 if (cs->second.r2save_)
34e0882b 6765 {
32f59844 6766 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6767 p += 4;
34e0882b 6768 }
9e69ed50 6769 if (ha(off) != 0)
ec661b9d 6770 {
32f59844
AM
6771 write_insn<big_endian>(p, addis_11_2 + ha(off));
6772 p += 4;
6773 write_insn<big_endian>(p, ld_12_11 + l(off));
6774 p += 4;
6775 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6776 {
32f59844 6777 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6778 p += 4;
32f59844 6779 off = 0;
397998fc 6780 }
32f59844
AM
6781 write_insn<big_endian>(p, mtctr_12);
6782 p += 4;
6783 if (use_fake_dep)
397998fc 6784 {
32f59844 6785 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6786 p += 4;
32f59844 6787 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6788 p += 4;
6789 }
32f59844 6790 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6791 p += 4;
32f59844 6792 if (static_chain)
9e69ed50 6793 {
32f59844 6794 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6795 p += 4;
9e69ed50 6796 }
ec661b9d
AM
6797 }
6798 else
6799 {
b4f7960d
AM
6800 write_insn<big_endian>(p, ld_12_2 + l(off));
6801 p += 4;
32f59844 6802 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6803 {
b4f7960d
AM
6804 write_insn<big_endian>(p, addi_2_2 + l(off));
6805 p += 4;
9e69ed50 6806 off = 0;
ec661b9d 6807 }
b4f7960d
AM
6808 write_insn<big_endian>(p, mtctr_12);
6809 p += 4;
32f59844 6810 if (use_fake_dep)
9e69ed50 6811 {
32f59844
AM
6812 write_insn<big_endian>(p, xor_11_12_12);
6813 p += 4;
6814 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6815 p += 4;
9e69ed50 6816 }
32f59844
AM
6817 if (static_chain)
6818 {
6819 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6820 p += 4;
6821 }
6822 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6823 p += 4;
34e0882b 6824 }
afd2ea23
AM
6825 if (cs->second.r2save_
6826 && !cs->second.localentry0_
6827 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6828 this->build_tls_opt_tail(p);
34e0882b 6829 else if (thread_safe && !use_fake_dep)
9e69ed50 6830 {
b4f7960d
AM
6831 write_insn<big_endian>(p, cmpldi_2_0);
6832 p += 4;
6833 write_insn<big_endian>(p, bnectr_p4);
6834 p += 4;
9e69ed50
AM
6835 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6836 }
6837 else
407aa07c 6838 write_insn<big_endian>(p, bctr);
e5d5f5ed 6839 }
ec661b9d
AM
6840 }
6841
6842 // Write out long branch stubs.
6843 typename Branch_stub_entries::const_iterator bs;
6844 for (bs = this->long_branch_stubs_.begin();
6845 bs != this->long_branch_stubs_.end();
6846 ++bs)
6847 {
32f59844 6848 if (bs->second.save_res_)
d49044c7 6849 continue;
32f59844
AM
6850 Address off = this->plt_size_ + bs->second.off_;
6851 p = oview + off;
6852 Address loc = this->stub_address() + off;
ec661b9d 6853 Address delta = bs->first.dest_ - loc;
eb97d024 6854 if (!bs->second.p9notoc_)
fa40fbe4 6855 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
eb97d024 6856 if (bs->second.p9notoc_)
32f59844
AM
6857 {
6858 unsigned char* startp = p;
6859 p = build_notoc_offset<big_endian>(p, off, false);
6860 delta -= p - startp;
6861 }
6862 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6863 {
ec661b9d
AM
6864 Address brlt_addr
6865 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6866 gold_assert(brlt_addr != invalid_address);
6867 brlt_addr += this->targ_->brlt_section()->address();
a19da04b 6868 Address got_addr = this->targ_->toc_pointer();
ec661b9d
AM
6869 Address brltoff = brlt_addr - got_addr;
6870 if (ha(brltoff) == 0)
6871 {
32f59844
AM
6872 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6873 p += 4;
ec661b9d
AM
6874 }
6875 else
cf43a2fe 6876 {
32f59844
AM
6877 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6878 p += 4;
6879 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6880 p += 4;
cf43a2fe 6881 }
32f59844
AM
6882 }
6883 if (delta + (1 << 25) < 2 << 25)
6884 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6885 else
6886 {
6887 write_insn<big_endian>(p, mtctr_12);
6888 p += 4;
407aa07c 6889 write_insn<big_endian>(p, bctr);
cf43a2fe 6890 }
ec661b9d
AM
6891 }
6892 }
32f59844 6893 else // size == 32
ec661b9d
AM
6894 {
6895 if (!this->plt_call_stubs_.empty())
6896 {
ec661b9d
AM
6897 // The address of _GLOBAL_OFFSET_TABLE_.
6898 Address g_o_t = invalid_address;
6899
6900 // Write out plt call stubs.
6901 typename Plt_stub_entries::const_iterator cs;
6902 for (cs = this->plt_call_stubs_.begin();
6903 cs != this->plt_call_stubs_.end();
6904 ++cs)
cf43a2fe 6905 {
08be3224
AM
6906 const Output_data_plt_powerpc<size, big_endian>* plt;
6907 Address plt_addr = this->plt_off(cs, &plt);
6908 plt_addr += plt->address();
ec661b9d 6909
bdab445c 6910 p = oview + cs->second.off_;
afd2ea23
AM
6911 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6912 this->build_tls_opt_head(&p, false);
ec661b9d
AM
6913 if (parameters->options().output_is_position_independent())
6914 {
6915 Address got_addr;
6916 const Powerpc_relobj<size, big_endian>* ppcobj
6917 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6918 (cs->first.object_));
6919 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6920 {
6921 unsigned int got2 = ppcobj->got2_shndx();
6922 got_addr = ppcobj->get_output_section_offset(got2);
6923 gold_assert(got_addr != invalid_address);
6924 got_addr += (ppcobj->output_section(got2)->address()
6925 + cs->first.addend_);
6926 }
6927 else
6928 {
6929 if (g_o_t == invalid_address)
a19da04b 6930 g_o_t = this->targ_->toc_pointer();
ec661b9d
AM
6931 got_addr = g_o_t;
6932 }
6933
9e69ed50
AM
6934 Address off = plt_addr - got_addr;
6935 if (ha(off) == 0)
9e390558 6936 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6937 else
6938 {
9e390558
AM
6939 write_insn<big_endian>(p, addis_11_30 + ha(off));
6940 p += 4;
6941 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6942 }
6943 }
6944 else
6945 {
9e390558
AM
6946 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6947 p += 4;
6948 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6949 }
9e390558
AM
6950 p += 4;
6951 write_insn<big_endian>(p, mtctr_11);
6952 p += 4;
407aa07c 6953 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6954 }
6955 }
6956
6957 // Write out long branch stubs.
6958 typename Branch_stub_entries::const_iterator bs;
6959 for (bs = this->long_branch_stubs_.begin();
6960 bs != this->long_branch_stubs_.end();
6961 ++bs)
6962 {
32f59844 6963 if (bs->second.save_res_)
d49044c7 6964 continue;
32f59844
AM
6965 Address off = this->plt_size_ + bs->second.off_;
6966 p = oview + off;
6967 Address loc = this->stub_address() + off;
ec661b9d
AM
6968 Address delta = bs->first.dest_ - loc;
6969 if (delta + (1 << 25) < 2 << 25)
6970 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6971 else if (!parameters->options().output_is_position_independent())
6972 {
9e390558
AM
6973 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6974 p += 4;
6975 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6976 }
6977 else
6978 {
6979 delta -= 8;
9e390558
AM
6980 write_insn<big_endian>(p, mflr_0);
6981 p += 4;
6982 write_insn<big_endian>(p, bcl_20_31);
6983 p += 4;
6984 write_insn<big_endian>(p, mflr_12);
6985 p += 4;
6986 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6987 p += 4;
6988 write_insn<big_endian>(p, addi_12_12 + l(delta));
6989 p += 4;
6990 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6991 }
9e390558
AM
6992 p += 4;
6993 write_insn<big_endian>(p, mtctr_12);
6994 p += 4;
407aa07c 6995 write_insn<big_endian>(p, bctr);
cf43a2fe 6996 }
ec661b9d 6997 }
d49044c7
AM
6998 if (this->need_save_res_)
6999 {
7000 p = oview + this->plt_size_ + this->branch_size_;
7001 memcpy (p, this->targ_->savres_section()->contents(),
7002 this->targ_->savres_section()->data_size());
7003 }
ec661b9d
AM
7004}
7005
7006// Write out .glink.
7007
7008template<int size, bool big_endian>
7009void
7010Output_data_glink<size, big_endian>::do_write(Output_file* of)
7011{
7012 const section_size_type off = this->offset();
7013 const section_size_type oview_size =
7014 convert_to_section_size_type(this->data_size());
7015 unsigned char* const oview = of->get_output_view(off, oview_size);
7016 unsigned char* p;
7017
7018 // The base address of the .plt section.
7019 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7020 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 7021
ec661b9d
AM
7022 if (size == 64)
7023 {
9055360d 7024 if (this->end_branch_table_ != 0)
b4f7960d 7025 {
9055360d
AM
7026 // Write pltresolve stub.
7027 p = oview;
7028 Address after_bcl = this->address() + 16;
7029 Address pltoff = plt_base - after_bcl;
7030
7031 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 7032
b4f7960d 7033 if (this->targ_->abiversion() < 2)
cf43a2fe 7034 {
9055360d
AM
7035 write_insn<big_endian>(p, mflr_12), p += 4;
7036 write_insn<big_endian>(p, bcl_20_31), p += 4;
7037 write_insn<big_endian>(p, mflr_11), p += 4;
7038 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
7039 write_insn<big_endian>(p, mtlr_12), p += 4;
7040 write_insn<big_endian>(p, add_11_2_11), p += 4;
7041 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7042 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
7043 write_insn<big_endian>(p, mtctr_12), p += 4;
7044 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
7045 }
7046 else
7047 {
63e5eea2
AM
7048 if (this->targ_->has_localentry0())
7049 {
7050 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
7051 }
9055360d
AM
7052 write_insn<big_endian>(p, mflr_0), p += 4;
7053 write_insn<big_endian>(p, bcl_20_31), p += 4;
7054 write_insn<big_endian>(p, mflr_11), p += 4;
9055360d 7055 write_insn<big_endian>(p, mtlr_0), p += 4;
63e5eea2
AM
7056 if (this->targ_->has_localentry0())
7057 {
7058 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
7059 }
7060 else
7061 {
7062 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
7063 }
9055360d 7064 write_insn<big_endian>(p, sub_12_12_11), p += 4;
63e5eea2
AM
7065 write_insn<big_endian>(p, add_11_0_11), p += 4;
7066 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
9055360d
AM
7067 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7068 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
7069 write_insn<big_endian>(p, mtctr_12), p += 4;
7070 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
7071 }
407aa07c 7072 write_insn<big_endian>(p, bctr), p += 4;
9e390558 7073 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
7074
7075 // Write lazy link call stubs.
7076 uint32_t indx = 0;
7077 while (p < oview + this->end_branch_table_)
7078 {
7079 if (this->targ_->abiversion() < 2)
b4f7960d 7080 {
9055360d
AM
7081 if (indx < 0x8000)
7082 {
7083 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
7084 }
7085 else
7086 {
bbec1a5d 7087 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
7088 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
7089 }
b4f7960d 7090 }
9055360d
AM
7091 uint32_t branch_off = 8 - (p - oview);
7092 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
7093 indx++;
cf43a2fe 7094 }
9055360d
AM
7095 }
7096
7097 Address plt_base = this->targ_->plt_section()->address();
7098 Address iplt_base = invalid_address;
9e390558 7099 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
7100 Address global_entry_base = this->address() + global_entry_off;
7101 typename Global_entry_stub_entries::const_iterator ge;
7102 for (ge = this->global_entry_stubs_.begin();
7103 ge != this->global_entry_stubs_.end();
7104 ++ge)
7105 {
7106 p = oview + global_entry_off + ge->second;
7107 Address plt_addr = ge->first->plt_offset();
7108 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7109 && ge->first->can_use_relative_reloc(false))
7110 {
7111 if (iplt_base == invalid_address)
7112 iplt_base = this->targ_->iplt_section()->address();
7113 plt_addr += iplt_base;
7114 }
7115 else
7116 plt_addr += plt_base;
7117 Address my_addr = global_entry_base + ge->second;
7118 Address off = plt_addr - my_addr;
7119
7120 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 7121 gold_error(_("linkage table error against `%s'"),
9055360d
AM
7122 ge->first->demangled_name().c_str());
7123
7124 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
7125 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
7126 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 7127 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
7128 }
7129 }
7130 else
7131 {
dd93cd0a 7132 // The address of _GLOBAL_OFFSET_TABLE_.
a19da04b 7133 Address g_o_t = this->targ_->toc_pointer();
c9269dff 7134
cf43a2fe 7135 // Write out pltresolve branch table.
ec661b9d 7136 p = oview;
9e390558 7137 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 7138 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
7139 while (p < end_p - 8 * 4)
7140 write_insn<big_endian>(p, b + end_p - p), p += 4;
7141 while (p < end_p)
7142 write_insn<big_endian>(p, nop), p += 4;
42cacb20 7143
cf43a2fe 7144 // Write out pltresolve call stub.
9e390558 7145 end_p = oview + oview_size;
cf43a2fe 7146 if (parameters->options().output_is_position_independent())
42cacb20 7147 {
ec661b9d 7148 Address res0_off = 0;
dd93cd0a
AM
7149 Address after_bcl_off = the_end + 12;
7150 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 7151
9e390558
AM
7152 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7153 p += 4;
7154 write_insn<big_endian>(p, mflr_0);
7155 p += 4;
7156 write_insn<big_endian>(p, bcl_20_31);
7157 p += 4;
7158 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7159 p += 4;
7160 write_insn<big_endian>(p, mflr_12);
7161 p += 4;
7162 write_insn<big_endian>(p, mtlr_0);
7163 p += 4;
7164 write_insn<big_endian>(p, sub_11_11_12);
7165 p += 4;
cf43a2fe 7166
dd93cd0a 7167 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 7168
9e390558
AM
7169 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7170 p += 4;
cf43a2fe
AM
7171 if (ha(got_bcl) == ha(got_bcl + 4))
7172 {
9e390558
AM
7173 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7174 p += 4;
7175 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
7176 }
7177 else
7178 {
9e390558
AM
7179 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7180 p += 4;
7181 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 7182 }
9e390558
AM
7183 p += 4;
7184 write_insn<big_endian>(p, mtctr_0);
7185 p += 4;
7186 write_insn<big_endian>(p, add_0_11_11);
7187 p += 4;
7188 write_insn<big_endian>(p, add_11_0_11);
42cacb20 7189 }
cf43a2fe 7190 else
42cacb20 7191 {
ec661b9d 7192 Address res0 = this->address();
cf43a2fe 7193
9e390558
AM
7194 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7195 p += 4;
7196 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7197 p += 4;
cf43a2fe 7198 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7199 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 7200 else
9e390558
AM
7201 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7202 p += 4;
7203 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7204 p += 4;
7205 write_insn<big_endian>(p, mtctr_0);
7206 p += 4;
7207 write_insn<big_endian>(p, add_0_11_11);
7208 p += 4;
cf43a2fe 7209 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 7210 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 7211 else
9e390558
AM
7212 write_insn<big_endian>(p, lwz_12_12 + 4);
7213 p += 4;
7214 write_insn<big_endian>(p, add_11_0_11);
7215 }
7216 p += 4;
407aa07c
AM
7217 write_insn<big_endian>(p, bctr);
7218 p += 4;
9e390558
AM
7219 while (p < end_p)
7220 {
7221 write_insn<big_endian>(p, nop);
7222 p += 4;
42cacb20
DE
7223 }
7224 }
7225
cf43a2fe
AM
7226 of->write_output_view(off, oview_size, oview);
7227}
7228
f3a0ed29
AM
7229
7230// A class to handle linker generated save/restore functions.
7231
7232template<int size, bool big_endian>
7233class Output_data_save_res : public Output_section_data_build
7234{
7235 public:
7236 Output_data_save_res(Symbol_table* symtab);
7237
d49044c7
AM
7238 const unsigned char*
7239 contents() const
7240 {
7241 return contents_;
7242 }
7243
f3a0ed29
AM
7244 protected:
7245 // Write to a map file.
7246 void
7247 do_print_to_mapfile(Mapfile* mapfile) const
7248 { mapfile->print_output_data(this, _("** save/restore")); }
7249
7250 void
7251 do_write(Output_file*);
7252
7253 private:
7254 // The maximum size of save/restore contents.
7255 static const unsigned int savres_max = 218*4;
7256
7257 void
7258 savres_define(Symbol_table* symtab,
7259 const char *name,
7260 unsigned int lo, unsigned int hi,
7261 unsigned char* write_ent(unsigned char*, int),
7262 unsigned char* write_tail(unsigned char*, int));
7263
7264 unsigned char *contents_;
7265};
7266
7267template<bool big_endian>
7268static unsigned char*
7269savegpr0(unsigned char* p, int r)
7270{
7271 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7272 write_insn<big_endian>(p, insn);
7273 return p + 4;
7274}
7275
7276template<bool big_endian>
7277static unsigned char*
7278savegpr0_tail(unsigned char* p, int r)
7279{
7280 p = savegpr0<big_endian>(p, r);
7281 uint32_t insn = std_0_1 + 16;
7282 write_insn<big_endian>(p, insn);
7283 p = p + 4;
7284 write_insn<big_endian>(p, blr);
7285 return p + 4;
7286}
7287
7288template<bool big_endian>
62fe925a 7289static unsigned char*
f3a0ed29
AM
7290restgpr0(unsigned char* p, int r)
7291{
7292 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7293 write_insn<big_endian>(p, insn);
7294 return p + 4;
7295}
7296
7297template<bool big_endian>
62fe925a 7298static unsigned char*
f3a0ed29
AM
7299restgpr0_tail(unsigned char* p, int r)
7300{
7301 uint32_t insn = ld_0_1 + 16;
7302 write_insn<big_endian>(p, insn);
7303 p = p + 4;
7304 p = restgpr0<big_endian>(p, r);
7305 write_insn<big_endian>(p, mtlr_0);
7306 p = p + 4;
7307 if (r == 29)
7308 {
7309 p = restgpr0<big_endian>(p, 30);
7310 p = restgpr0<big_endian>(p, 31);
7311 }
7312 write_insn<big_endian>(p, blr);
7313 return p + 4;
7314}
7315
7316template<bool big_endian>
62fe925a 7317static unsigned char*
f3a0ed29
AM
7318savegpr1(unsigned char* p, int r)
7319{
7320 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7321 write_insn<big_endian>(p, insn);
7322 return p + 4;
7323}
7324
7325template<bool big_endian>
62fe925a 7326static unsigned char*
f3a0ed29
AM
7327savegpr1_tail(unsigned char* p, int r)
7328{
7329 p = savegpr1<big_endian>(p, r);
7330 write_insn<big_endian>(p, blr);
7331 return p + 4;
7332}
7333
7334template<bool big_endian>
62fe925a 7335static unsigned char*
f3a0ed29
AM
7336restgpr1(unsigned char* p, int r)
7337{
7338 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7339 write_insn<big_endian>(p, insn);
7340 return p + 4;
7341}
7342
7343template<bool big_endian>
62fe925a 7344static unsigned char*
f3a0ed29
AM
7345restgpr1_tail(unsigned char* p, int r)
7346{
7347 p = restgpr1<big_endian>(p, r);
7348 write_insn<big_endian>(p, blr);
7349 return p + 4;
7350}
7351
7352template<bool big_endian>
62fe925a 7353static unsigned char*
f3a0ed29
AM
7354savefpr(unsigned char* p, int r)
7355{
7356 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7357 write_insn<big_endian>(p, insn);
7358 return p + 4;
7359}
7360
7361template<bool big_endian>
62fe925a 7362static unsigned char*
f3a0ed29
AM
7363savefpr0_tail(unsigned char* p, int r)
7364{
7365 p = savefpr<big_endian>(p, r);
7366 write_insn<big_endian>(p, std_0_1 + 16);
7367 p = p + 4;
7368 write_insn<big_endian>(p, blr);
7369 return p + 4;
7370}
7371
7372template<bool big_endian>
62fe925a 7373static unsigned char*
f3a0ed29
AM
7374restfpr(unsigned char* p, int r)
7375{
7376 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7377 write_insn<big_endian>(p, insn);
7378 return p + 4;
7379}
7380
7381template<bool big_endian>
62fe925a 7382static unsigned char*
f3a0ed29
AM
7383restfpr0_tail(unsigned char* p, int r)
7384{
7385 write_insn<big_endian>(p, ld_0_1 + 16);
7386 p = p + 4;
7387 p = restfpr<big_endian>(p, r);
7388 write_insn<big_endian>(p, mtlr_0);
7389 p = p + 4;
7390 if (r == 29)
7391 {
7392 p = restfpr<big_endian>(p, 30);
7393 p = restfpr<big_endian>(p, 31);
7394 }
7395 write_insn<big_endian>(p, blr);
7396 return p + 4;
7397}
7398
7399template<bool big_endian>
62fe925a 7400static unsigned char*
f3a0ed29
AM
7401savefpr1_tail(unsigned char* p, int r)
7402{
7403 p = savefpr<big_endian>(p, r);
7404 write_insn<big_endian>(p, blr);
7405 return p + 4;
7406}
7407
7408template<bool big_endian>
62fe925a 7409static unsigned char*
f3a0ed29
AM
7410restfpr1_tail(unsigned char* p, int r)
7411{
7412 p = restfpr<big_endian>(p, r);
7413 write_insn<big_endian>(p, blr);
7414 return p + 4;
7415}
7416
7417template<bool big_endian>
62fe925a 7418static unsigned char*
f3a0ed29
AM
7419savevr(unsigned char* p, int r)
7420{
7421 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7422 write_insn<big_endian>(p, insn);
7423 p = p + 4;
7424 insn = stvx_0_12_0 + (r << 21);
7425 write_insn<big_endian>(p, insn);
7426 return p + 4;
7427}
7428
7429template<bool big_endian>
62fe925a 7430static unsigned char*
f3a0ed29
AM
7431savevr_tail(unsigned char* p, int r)
7432{
7433 p = savevr<big_endian>(p, r);
7434 write_insn<big_endian>(p, blr);
7435 return p + 4;
7436}
7437
7438template<bool big_endian>
62fe925a 7439static unsigned char*
f3a0ed29
AM
7440restvr(unsigned char* p, int r)
7441{
7442 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7443 write_insn<big_endian>(p, insn);
7444 p = p + 4;
7445 insn = lvx_0_12_0 + (r << 21);
7446 write_insn<big_endian>(p, insn);
7447 return p + 4;
7448}
7449
7450template<bool big_endian>
62fe925a 7451static unsigned char*
f3a0ed29
AM
7452restvr_tail(unsigned char* p, int r)
7453{
7454 p = restvr<big_endian>(p, r);
7455 write_insn<big_endian>(p, blr);
7456 return p + 4;
7457}
7458
7459
7460template<int size, bool big_endian>
7461Output_data_save_res<size, big_endian>::Output_data_save_res(
7462 Symbol_table* symtab)
7463 : Output_section_data_build(4),
7464 contents_(NULL)
7465{
7466 this->savres_define(symtab,
7467 "_savegpr0_", 14, 31,
7468 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7469 this->savres_define(symtab,
7470 "_restgpr0_", 14, 29,
7471 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7472 this->savres_define(symtab,
7473 "_restgpr0_", 30, 31,
7474 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7475 this->savres_define(symtab,
7476 "_savegpr1_", 14, 31,
7477 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7478 this->savres_define(symtab,
7479 "_restgpr1_", 14, 31,
7480 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7481 this->savres_define(symtab,
7482 "_savefpr_", 14, 31,
7483 savefpr<big_endian>, savefpr0_tail<big_endian>);
7484 this->savres_define(symtab,
7485 "_restfpr_", 14, 29,
7486 restfpr<big_endian>, restfpr0_tail<big_endian>);
7487 this->savres_define(symtab,
7488 "_restfpr_", 30, 31,
7489 restfpr<big_endian>, restfpr0_tail<big_endian>);
7490 this->savres_define(symtab,
7491 "._savef", 14, 31,
7492 savefpr<big_endian>, savefpr1_tail<big_endian>);
7493 this->savres_define(symtab,
7494 "._restf", 14, 31,
7495 restfpr<big_endian>, restfpr1_tail<big_endian>);
7496 this->savres_define(symtab,
7497 "_savevr_", 20, 31,
7498 savevr<big_endian>, savevr_tail<big_endian>);
7499 this->savres_define(symtab,
7500 "_restvr_", 20, 31,
7501 restvr<big_endian>, restvr_tail<big_endian>);
7502}
7503
7504template<int size, bool big_endian>
7505void
7506Output_data_save_res<size, big_endian>::savres_define(
7507 Symbol_table* symtab,
7508 const char *name,
7509 unsigned int lo, unsigned int hi,
7510 unsigned char* write_ent(unsigned char*, int),
7511 unsigned char* write_tail(unsigned char*, int))
7512{
7513 size_t len = strlen(name);
7514 bool writing = false;
7515 char sym[16];
7516
7517 memcpy(sym, name, len);
7518 sym[len + 2] = 0;
7519
7520 for (unsigned int i = lo; i <= hi; i++)
7521 {
7522 sym[len + 0] = i / 10 + '0';
7523 sym[len + 1] = i % 10 + '0';
7524 Symbol* gsym = symtab->lookup(sym);
7525 bool refd = gsym != NULL && gsym->is_undefined();
7526 writing = writing || refd;
7527 if (writing)
7528 {
7529 if (this->contents_ == NULL)
7530 this->contents_ = new unsigned char[this->savres_max];
7531
ec661b9d 7532 section_size_type value = this->current_data_size();
f3a0ed29
AM
7533 unsigned char* p = this->contents_ + value;
7534 if (i != hi)
7535 p = write_ent(p, i);
7536 else
7537 p = write_tail(p, i);
ec661b9d 7538 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7539 this->set_current_data_size(cur_size);
7540 if (refd)
7541 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7542 this, value, cur_size - value,
7543 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7544 elfcpp::STV_HIDDEN, 0, false, false);
7545 }
7546 }
7547}
7548
7549// Write out save/restore.
7550
7551template<int size, bool big_endian>
7552void
7553Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7554{
ec661b9d 7555 const section_size_type off = this->offset();
f3a0ed29
AM
7556 const section_size_type oview_size =
7557 convert_to_section_size_type(this->data_size());
7558 unsigned char* const oview = of->get_output_view(off, oview_size);
7559 memcpy(oview, this->contents_, oview_size);
7560 of->write_output_view(off, oview_size, oview);
7561}
7562
7563
cf43a2fe 7564// Create the glink section.
42cacb20 7565
cf43a2fe
AM
7566template<int size, bool big_endian>
7567void
7568Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7569{
7570 if (this->glink_ == NULL)
7571 {
7572 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7573 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7574 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7575 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7576 this->glink_, ORDER_TEXT, false);
7577 }
42cacb20
DE
7578}
7579
7580// Create a PLT entry for a global symbol.
7581
7582template<int size, bool big_endian>
7583void
ec661b9d
AM
7584Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7585 Layout* layout,
7586 Symbol* gsym)
42cacb20 7587{
e5d5f5ed
AM
7588 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7589 && gsym->can_use_relative_reloc(false))
7590 {
7591 if (this->iplt_ == NULL)
40b469d7 7592 this->make_iplt_section(symtab, layout);
03e25981 7593 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7594 }
7595 else
7596 {
7597 if (this->plt_ == NULL)
40b469d7 7598 this->make_plt_section(symtab, layout);
03e25981 7599 this->plt_->add_entry(gsym);
e5d5f5ed 7600 }
e5d5f5ed 7601}
42cacb20 7602
2d7ad24e
AM
7603// Make a PLT entry for a local symbol.
7604
7605template<int size, bool big_endian>
7606void
7607Target_powerpc<size, big_endian>::make_local_plt_entry(
f21dbd7c 7608 Symbol_table* symtab,
2d7ad24e
AM
7609 Layout* layout,
7610 Sized_relobj_file<size, big_endian>* relobj,
7611 unsigned int r_sym)
7612{
7613 if (this->lplt_ == NULL)
f21dbd7c 7614 this->make_lplt_section(symtab, layout);
2d7ad24e
AM
7615 this->lplt_->add_local_entry(relobj, r_sym);
7616}
7617
f21dbd7c
AM
7618template<int size, bool big_endian>
7619void
7620Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7621 Layout* layout,
7622 Symbol* gsym)
7623{
7624 if (this->lplt_ == NULL)
7625 this->make_lplt_section(symtab, layout);
7626 this->lplt_->add_entry(gsym, true);
7627}
7628
e5d5f5ed 7629// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7630
e5d5f5ed
AM
7631template<int size, bool big_endian>
7632void
7633Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7634 Symbol_table* symtab,
e5d5f5ed 7635 Layout* layout,
ec661b9d
AM
7636 Sized_relobj_file<size, big_endian>* relobj,
7637 unsigned int r_sym)
e5d5f5ed
AM
7638{
7639 if (this->iplt_ == NULL)
40b469d7 7640 this->make_iplt_section(symtab, layout);
03e25981 7641 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7642}
7643
0e70b911
CC
7644// Return the number of entries in the PLT.
7645
7646template<int size, bool big_endian>
7647unsigned int
7648Target_powerpc<size, big_endian>::plt_entry_count() const
7649{
7650 if (this->plt_ == NULL)
7651 return 0;
b3ccdeb5 7652 return this->plt_->entry_count();
0e70b911
CC
7653}
7654
dd93cd0a 7655// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7656
7657template<int size, bool big_endian>
7658unsigned int
dd93cd0a 7659Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7660 Symbol_table* symtab,
7661 Layout* layout,
7662 Sized_relobj_file<size, big_endian>* object)
42cacb20 7663{
dd93cd0a 7664 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7665 {
7666 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7667 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a 7668 Output_data_got_powerpc<size, big_endian>* got
f19c3684 7669 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a 7670 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7671 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7672 got_offset, 0);
dd93cd0a 7673 this->tlsld_got_offset_ = got_offset;
42cacb20 7674 }
dd93cd0a 7675 return this->tlsld_got_offset_;
42cacb20
DE
7676}
7677
95a2c8d6
RS
7678// Get the Reference_flags for a particular relocation.
7679
7680template<int size, bool big_endian>
7681int
88b8e639
AM
7682Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7683 unsigned int r_type,
7684 const Target_powerpc* target)
95a2c8d6 7685{
88b8e639
AM
7686 int ref = 0;
7687
95a2c8d6
RS
7688 switch (r_type)
7689 {
6158b25f
AM
7690 case elfcpp::R_PPC64_TOC:
7691 if (size != 64)
7692 break;
7693 // Fall through.
95a2c8d6
RS
7694 case elfcpp::R_POWERPC_NONE:
7695 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7696 case elfcpp::R_POWERPC_GNU_VTENTRY:
95a2c8d6 7697 // No symbol reference.
88b8e639 7698 break;
95a2c8d6 7699
dd93cd0a
AM
7700 case elfcpp::R_PPC64_ADDR64:
7701 case elfcpp::R_PPC64_UADDR64:
89c52ae3
AM
7702 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7703 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7704 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7705 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7706 case elfcpp::R_PPC64_D34:
7707 case elfcpp::R_PPC64_D34_LO:
7708 case elfcpp::R_PPC64_D34_HI30:
7709 case elfcpp::R_PPC64_D34_HA30:
7710 case elfcpp::R_PPC64_D28:
6158b25f
AM
7711 if (size != 64)
7712 break;
7713 // Fall through.
7714 case elfcpp::R_POWERPC_ADDR32:
7715 case elfcpp::R_POWERPC_UADDR32:
7716 case elfcpp::R_POWERPC_ADDR16:
7717 case elfcpp::R_POWERPC_UADDR16:
7718 case elfcpp::R_POWERPC_ADDR16_LO:
7719 case elfcpp::R_POWERPC_ADDR16_HI:
7720 case elfcpp::R_POWERPC_ADDR16_HA:
88b8e639
AM
7721 ref = Symbol::ABSOLUTE_REF;
7722 break;
95a2c8d6 7723
dd93cd0a
AM
7724 case elfcpp::R_POWERPC_ADDR24:
7725 case elfcpp::R_POWERPC_ADDR14:
7726 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7727 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7728 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7729 break;
dd93cd0a 7730
95a2c8d6 7731 case elfcpp::R_PPC_LOCAL24PC:
6158b25f
AM
7732 if (size != 32)
7733 break;
7734 // Fall through.
7735 ref = Symbol::RELATIVE_REF;
7736 break;
7737
7738 case elfcpp::R_PPC64_REL64:
c432bbba
AM
7739 case elfcpp::R_PPC64_REL16_HIGH:
7740 case elfcpp::R_PPC64_REL16_HIGHA:
7741 case elfcpp::R_PPC64_REL16_HIGHER:
7742 case elfcpp::R_PPC64_REL16_HIGHERA:
7743 case elfcpp::R_PPC64_REL16_HIGHEST:
7744 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7745 case elfcpp::R_PPC64_PCREL34:
7746 case elfcpp::R_PPC64_REL16_HIGHER34:
7747 case elfcpp::R_PPC64_REL16_HIGHERA34:
7748 case elfcpp::R_PPC64_REL16_HIGHEST34:
7749 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7750 case elfcpp::R_PPC64_PCREL28:
6158b25f
AM
7751 if (size != 64)
7752 break;
7753 // Fall through.
7754 case elfcpp::R_POWERPC_REL32:
7755 case elfcpp::R_POWERPC_REL16:
7756 case elfcpp::R_POWERPC_REL16_LO:
7757 case elfcpp::R_POWERPC_REL16_HI:
7758 case elfcpp::R_POWERPC_REL16_HA:
88b8e639
AM
7759 ref = Symbol::RELATIVE_REF;
7760 break;
95a2c8d6 7761
6158b25f
AM
7762 case elfcpp::R_PPC_PLTREL24:
7763 if (size != 32)
7764 break;
7765 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7766 break;
7767
32f59844 7768 case elfcpp::R_PPC64_REL24_NOTOC:
6158b25f
AM
7769 case elfcpp::R_PPC64_REL24_P9NOTOC:
7770 case elfcpp::R_PPC64_PLT16_LO_DS:
7771 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7772 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7773 case elfcpp::R_PPC64_PLT_PCREL34:
7774 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7775 if (size != 64)
32f59844
AM
7776 break;
7777 // Fall through.
dd93cd0a 7778 case elfcpp::R_POWERPC_REL24:
dd93cd0a
AM
7779 case elfcpp::R_POWERPC_REL14:
7780 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7781 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
f21dbd7c
AM
7782 case elfcpp::R_POWERPC_PLT16_LO:
7783 case elfcpp::R_POWERPC_PLT16_HI:
7784 case elfcpp::R_POWERPC_PLT16_HA:
f21dbd7c 7785 case elfcpp::R_POWERPC_PLTSEQ:
f21dbd7c 7786 case elfcpp::R_POWERPC_PLTCALL:
88b8e639
AM
7787 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7788 break;
95a2c8d6 7789
e5d5f5ed
AM
7790 case elfcpp::R_PPC64_GOT16_DS:
7791 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7792 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7793 case elfcpp::R_PPC64_TOC16:
7794 case elfcpp::R_PPC64_TOC16_LO:
7795 case elfcpp::R_PPC64_TOC16_HI:
7796 case elfcpp::R_PPC64_TOC16_HA:
7797 case elfcpp::R_PPC64_TOC16_DS:
7798 case elfcpp::R_PPC64_TOC16_LO_DS:
6158b25f
AM
7799 if (size != 64)
7800 break;
7801 // Fall through.
7802 case elfcpp::R_POWERPC_GOT16:
7803 case elfcpp::R_POWERPC_GOT16_LO:
7804 case elfcpp::R_POWERPC_GOT16_HI:
7805 case elfcpp::R_POWERPC_GOT16_HA:
32d849b3 7806 ref = Symbol::RELATIVE_REF;
88b8e639 7807 break;
95a2c8d6 7808
89c52ae3
AM
7809 case elfcpp::R_PPC64_TLSGD:
7810 case elfcpp::R_PPC64_TLSLD:
7811 case elfcpp::R_PPC64_TPREL34:
7812 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
7813 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7814 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7815 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7816 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
6158b25f
AM
7817 if (size != 64)
7818 break;
7819 // Fall through.
7820 case elfcpp::R_POWERPC_GOT_TPREL16:
7821 case elfcpp::R_POWERPC_TLS:
88b8e639
AM
7822 ref = Symbol::TLS_REF;
7823 break;
95a2c8d6
RS
7824
7825 case elfcpp::R_POWERPC_COPY:
7826 case elfcpp::R_POWERPC_GLOB_DAT:
7827 case elfcpp::R_POWERPC_JMP_SLOT:
7828 case elfcpp::R_POWERPC_RELATIVE:
7829 case elfcpp::R_POWERPC_DTPMOD:
7830 default:
7831 // Not expected. We will give an error later.
88b8e639 7832 break;
95a2c8d6 7833 }
88b8e639
AM
7834
7835 if (size == 64 && target->abiversion() < 2)
7836 ref |= Symbol::FUNC_DESC_ABI;
7837 return ref;
95a2c8d6
RS
7838}
7839
42cacb20
DE
7840// Report an unsupported relocation against a local symbol.
7841
7842template<int size, bool big_endian>
7843void
7844Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7845 Sized_relobj_file<size, big_endian>* object,
7846 unsigned int r_type)
42cacb20
DE
7847{
7848 gold_error(_("%s: unsupported reloc %u against local symbol"),
7849 object->name().c_str(), r_type);
7850}
7851
7852// We are about to emit a dynamic relocation of type R_TYPE. If the
7853// dynamic linker does not support it, issue an error.
7854
7855template<int size, bool big_endian>
7856void
7857Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7858 unsigned int r_type)
7859{
7860 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7861
7862 // These are the relocation types supported by glibc for both 32-bit
7863 // and 64-bit powerpc.
7864 switch (r_type)
7865 {
3ea0a085 7866 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7867 case elfcpp::R_POWERPC_RELATIVE:
7868 case elfcpp::R_POWERPC_GLOB_DAT:
7869 case elfcpp::R_POWERPC_DTPMOD:
7870 case elfcpp::R_POWERPC_DTPREL:
7871 case elfcpp::R_POWERPC_TPREL:
7872 case elfcpp::R_POWERPC_JMP_SLOT:
7873 case elfcpp::R_POWERPC_COPY:
3ea0a085 7874 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7875 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7876 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7877 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7878 case elfcpp::R_POWERPC_ADDR16:
7879 case elfcpp::R_POWERPC_UADDR16:
7880 case elfcpp::R_POWERPC_ADDR16_LO:
7881 case elfcpp::R_POWERPC_ADDR16_HI:
7882 case elfcpp::R_POWERPC_ADDR16_HA:
7883 case elfcpp::R_POWERPC_ADDR14:
7884 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7885 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7886 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7887 case elfcpp::R_POWERPC_TPREL16:
7888 case elfcpp::R_POWERPC_TPREL16_LO:
7889 case elfcpp::R_POWERPC_TPREL16_HI:
7890 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7891 return;
7892
7893 default:
7894 break;
7895 }
7896
7897 if (size == 64)
7898 {
7899 switch (r_type)
7900 {
7901 // These are the relocation types supported only on 64-bit.
7902 case elfcpp::R_PPC64_ADDR64:
42cacb20 7903 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7904 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7905 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7906 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7907 case elfcpp::R_PPC64_ADDR16_HIGH:
7908 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7909 case elfcpp::R_PPC64_ADDR16_HIGHER:
7910 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7911 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7912 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7913 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7914 case elfcpp::R_POWERPC_ADDR30:
7915 case elfcpp::R_PPC64_TPREL16_DS:
7916 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7917 case elfcpp::R_PPC64_TPREL16_HIGH:
7918 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7919 case elfcpp::R_PPC64_TPREL16_HIGHER:
7920 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7921 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7922 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7923 return;
7924
7925 default:
7926 break;
7927 }
7928 }
7929 else
7930 {
7931 switch (r_type)
7932 {
7933 // These are the relocation types supported only on 32-bit.
3ea0a085 7934 // ??? glibc ld.so doesn't need to support these.
e59a1001 7935 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7936 case elfcpp::R_POWERPC_DTPREL16:
7937 case elfcpp::R_POWERPC_DTPREL16_LO:
7938 case elfcpp::R_POWERPC_DTPREL16_HI:
7939 case elfcpp::R_POWERPC_DTPREL16_HA:
7940 return;
42cacb20
DE
7941
7942 default:
7943 break;
7944 }
7945 }
7946
7947 // This prevents us from issuing more than one error per reloc
7948 // section. But we can still wind up issuing more than one
7949 // error per object file.
7950 if (this->issued_non_pic_error_)
7951 return;
33aea2fd 7952 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7953 object->error(_("requires unsupported dynamic reloc; "
7954 "recompile with -fPIC"));
7955 this->issued_non_pic_error_ = true;
7956 return;
7957}
7958
e5d5f5ed
AM
7959// Return whether we need to make a PLT entry for a relocation of the
7960// given type against a STT_GNU_IFUNC symbol.
7961
7962template<int size, bool big_endian>
7963bool
7964Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7965 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7966 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7967 unsigned int r_type,
7968 bool report_err)
e5d5f5ed 7969{
c9824451
AM
7970 // In non-pic code any reference will resolve to the plt call stub
7971 // for the ifunc symbol.
9055360d
AM
7972 if ((size == 32 || target->abiversion() >= 2)
7973 && !parameters->options().output_is_position_independent())
c9824451
AM
7974 return true;
7975
e5d5f5ed
AM
7976 switch (r_type)
7977 {
b3ccdeb5 7978 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7979 case elfcpp::R_POWERPC_ADDR32:
7980 case elfcpp::R_POWERPC_UADDR32:
7981 if (size == 32)
b3ccdeb5 7982 return false;
e5d5f5ed
AM
7983 break;
7984
7985 case elfcpp::R_PPC64_ADDR64:
7986 case elfcpp::R_PPC64_UADDR64:
7987 if (size == 64)
b3ccdeb5 7988 return false;
e5d5f5ed
AM
7989 break;
7990
b3ccdeb5 7991 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7992 case elfcpp::R_POWERPC_GOT16:
7993 case elfcpp::R_POWERPC_GOT16_LO:
7994 case elfcpp::R_POWERPC_GOT16_HI:
7995 case elfcpp::R_POWERPC_GOT16_HA:
7996 case elfcpp::R_PPC64_GOT16_DS:
7997 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7998 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7999 return false;
e5d5f5ed 8000
08be3224
AM
8001 // PLT relocs are OK and need a PLT entry.
8002 case elfcpp::R_POWERPC_PLT16_LO:
8003 case elfcpp::R_POWERPC_PLT16_HI:
8004 case elfcpp::R_POWERPC_PLT16_HA:
8005 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
8006 case elfcpp::R_POWERPC_PLTSEQ:
8007 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8008 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8009 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8010 case elfcpp::R_PPC64_PLT_PCREL34:
8011 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8012 return true;
8013 break;
8014
b3ccdeb5 8015 // Function calls are good, and these do need a PLT entry.
32f59844
AM
8016 case elfcpp::R_PPC64_REL24_NOTOC:
8017 if (size == 32)
8018 break;
8019 // Fall through.
eb97d024 8020 case elfcpp::R_PPC64_REL24_P9NOTOC:
e5d5f5ed
AM
8021 case elfcpp::R_POWERPC_ADDR24:
8022 case elfcpp::R_POWERPC_ADDR14:
8023 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8024 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8025 case elfcpp::R_POWERPC_REL24:
8026 case elfcpp::R_PPC_PLTREL24:
8027 case elfcpp::R_POWERPC_REL14:
8028 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8029 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8030 return true;
8031
8032 default:
8033 break;
8034 }
8035
8036 // Anything else is a problem.
8037 // If we are building a static executable, the libc startup function
8038 // responsible for applying indirect function relocations is going
8039 // to complain about the reloc type.
8040 // If we are building a dynamic executable, we will have a text
8041 // relocation. The dynamic loader will set the text segment
8042 // writable and non-executable to apply text relocations. So we'll
8043 // segfault when trying to run the indirection function to resolve
8044 // the reloc.
b3ccdeb5
AM
8045 if (report_err)
8046 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
8047 object->name().c_str(), r_type);
8048 return false;
8049}
8050
5edad15d
AM
8051// Return TRUE iff INSN is one we expect on a _LO variety toc/got
8052// reloc.
8053
8054static bool
8055ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8056{
8057 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8058 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8059 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8060 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8061 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8062 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8063 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8064 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8065 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8066 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8067 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8068 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8069 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8070 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8071 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8072 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8073 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8074 /* Exclude lfqu by testing reloc. If relocs are ever
8075 defined for the reduced D field in psq_lu then those
8076 will need testing too. */
8077 && r_type != elfcpp::R_PPC64_TOC16_LO
8078 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8079 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8080 && (insn & 1) == 0)
8081 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8082 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8083 /* Exclude stfqu. psq_stu as above for psq_lu. */
8084 && r_type != elfcpp::R_PPC64_TOC16_LO
8085 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8086 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8087 && (insn & 1) == 0));
8088}
8089
42cacb20
DE
8090// Scan a relocation for a local symbol.
8091
8092template<int size, bool big_endian>
8093inline void
8094Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
8095 Symbol_table* symtab,
8096 Layout* layout,
8097 Target_powerpc<size, big_endian>* target,
8098 Sized_relobj_file<size, big_endian>* object,
8099 unsigned int data_shndx,
8100 Output_section* output_section,
8101 const elfcpp::Rela<size, big_endian>& reloc,
8102 unsigned int r_type,
e5d5f5ed 8103 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 8104 bool is_discarded)
42cacb20 8105{
0af4fcc2
AM
8106 Powerpc_relobj<size, big_endian>* ppc_object
8107 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8108
c7fdac09 8109 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
8110
8111 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8112 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8113 {
8114 this->expect_tls_get_addr_call();
c7fdac09
AM
8115 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8116 if (tls_type != tls::TLSOPT_NONE)
8117 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8118 }
8119 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8120 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8121 {
8122 this->expect_tls_get_addr_call();
c7fdac09
AM
8123 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8124 if (tls_type != tls::TLSOPT_NONE)
8125 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8126 }
8127
bfdfa4cd
AM
8128 if (is_discarded)
8129 {
8130 if (size == 64
8131 && data_shndx == ppc_object->opd_shndx()
8132 && r_type == elfcpp::R_PPC64_ADDR64)
8133 ppc_object->set_opd_discard(reloc.get_r_offset());
8134 return;
8135 }
8136
e5d5f5ed
AM
8137 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8138 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 8139 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 8140 {
ec661b9d
AM
8141 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8142 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8143 r_type, r_sym, reloc.get_r_addend());
8144 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 8145 }
e5d5f5ed 8146
42cacb20
DE
8147 switch (r_type)
8148 {
8149 case elfcpp::R_POWERPC_NONE:
8150 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8151 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 8152 case elfcpp::R_POWERPC_TLS:
549dba71 8153 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8154 case elfcpp::R_POWERPC_PLTSEQ:
8155 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8156 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8157 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8158 case elfcpp::R_PPC64_PCREL_OPT:
8159 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8160 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8161 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8162 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8163 case elfcpp::R_PPC64_REL16_HIGHER34:
8164 case elfcpp::R_PPC64_REL16_HIGHERA34:
8165 case elfcpp::R_PPC64_REL16_HIGHEST34:
8166 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8167 case elfcpp::R_PPC64_D34:
8168 case elfcpp::R_PPC64_D34_LO:
8169 case elfcpp::R_PPC64_D34_HI30:
8170 case elfcpp::R_PPC64_D34_HA30:
8171 case elfcpp::R_PPC64_D28:
8172 case elfcpp::R_PPC64_PCREL34:
8173 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8174 case elfcpp::R_PPC64_TPREL34:
8175 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8176 break;
8177
8178 case elfcpp::R_PPC64_TOC:
8179 {
8180 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8181 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a
AM
8182 if (parameters->options().output_is_position_independent())
8183 {
bfdfa4cd
AM
8184 Address off = reloc.get_r_offset();
8185 if (size == 64
9055360d 8186 && target->abiversion() < 2
bfdfa4cd
AM
8187 && data_shndx == ppc_object->opd_shndx()
8188 && ppc_object->get_opd_discard(off - 8))
8189 break;
8190
dd93cd0a 8191 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
a1251fdc 8192 Address got_off = got->g_o_t();
dd93cd0a
AM
8193 rela_dyn->add_output_section_relative(got->output_section(),
8194 elfcpp::R_POWERPC_RELATIVE,
8195 output_section,
bfdfa4cd 8196 object, data_shndx, off,
a19da04b 8197 got_off);
dd93cd0a
AM
8198 }
8199 }
42cacb20
DE
8200 break;
8201
8202 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 8203 case elfcpp::R_PPC64_UADDR64:
42cacb20 8204 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8205 case elfcpp::R_POWERPC_UADDR32:
8206 case elfcpp::R_POWERPC_ADDR24:
c9269dff 8207 case elfcpp::R_POWERPC_ADDR16:
42cacb20 8208 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
8209 case elfcpp::R_POWERPC_ADDR16_HI:
8210 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8211 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8212 case elfcpp::R_PPC64_ADDR16_HIGH:
8213 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8214 case elfcpp::R_PPC64_ADDR16_HIGHER:
8215 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8216 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8217 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8218 case elfcpp::R_PPC64_ADDR16_DS:
8219 case elfcpp::R_PPC64_ADDR16_LO_DS:
8220 case elfcpp::R_POWERPC_ADDR14:
8221 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8222 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
8223 // If building a shared library (or a position-independent
8224 // executable), we need to create a dynamic relocation for
8225 // this location.
c9824451 8226 if (parameters->options().output_is_position_independent()
9055360d 8227 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 8228 {
b3ccdeb5
AM
8229 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8230 is_ifunc);
1f98a074 8231 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
8232 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8233 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 8234 {
b3ccdeb5
AM
8235 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8236 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8237 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
8238 output_section, data_shndx,
8239 reloc.get_r_offset(),
c9824451 8240 reloc.get_r_addend(), false);
2e702c99 8241 }
1f98a074 8242 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 8243 {
dd93cd0a 8244 check_non_pic(object, r_type);
dd93cd0a
AM
8245 rela_dyn->add_local(object, r_sym, r_type, output_section,
8246 data_shndx, reloc.get_r_offset(),
8247 reloc.get_r_addend());
2e702c99 8248 }
1f98a074
AM
8249 else
8250 {
8251 gold_assert(lsym.get_st_value() == 0);
8252 unsigned int shndx = lsym.get_st_shndx();
8253 bool is_ordinary;
8254 shndx = object->adjust_sym_shndx(r_sym, shndx,
8255 &is_ordinary);
8256 if (!is_ordinary)
8257 object->error(_("section symbol %u has bad shndx %u"),
8258 r_sym, shndx);
8259 else
8260 rela_dyn->add_local_section(object, shndx, r_type,
8261 output_section, data_shndx,
8262 reloc.get_r_offset());
8263 }
2e702c99 8264 }
42cacb20
DE
8265 break;
8266
e4dff765
AM
8267 case elfcpp::R_PPC64_PLT_PCREL34:
8268 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
8269 case elfcpp::R_POWERPC_PLT16_LO:
8270 case elfcpp::R_POWERPC_PLT16_HI:
8271 case elfcpp::R_POWERPC_PLT16_HA:
8272 case elfcpp::R_PPC64_PLT16_LO_DS:
8273 if (!is_ifunc)
8274 {
8275 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
f21dbd7c 8276 target->make_local_plt_entry(symtab, layout, object, r_sym);
2d7ad24e
AM
8277 }
8278 break;
8279
32f59844
AM
8280 case elfcpp::R_PPC64_REL24_NOTOC:
8281 if (size == 32)
8282 break;
8283 // Fall through.
eb97d024 8284 case elfcpp::R_PPC64_REL24_P9NOTOC:
42cacb20 8285 case elfcpp::R_POWERPC_REL24:
c9824451 8286 case elfcpp::R_PPC_PLTREL24:
42cacb20 8287 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
8288 case elfcpp::R_POWERPC_REL14:
8289 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8290 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8291 if (!is_ifunc)
0e123f69
AM
8292 {
8293 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8294 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8295 r_type, r_sym, reloc.get_r_addend());
8296 }
ec661b9d
AM
8297 break;
8298
7e57d19e
AM
8299 case elfcpp::R_PPC64_TOCSAVE:
8300 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8301 // caller has already saved r2 and thus a plt call stub need not
8302 // save r2.
8303 if (size == 64
8304 && target->mark_pltcall(ppc_object, data_shndx,
8305 reloc.get_r_offset() - 4, symtab))
8306 {
8307 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8308 unsigned int shndx = lsym.get_st_shndx();
8309 bool is_ordinary;
8310 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8311 if (!is_ordinary)
8312 object->error(_("tocsave symbol %u has bad shndx %u"),
8313 r_sym, shndx);
8314 else
8315 target->add_tocsave(ppc_object, shndx,
8316 lsym.get_st_value() + reloc.get_r_addend());
8317 }
8318 break;
8319
ec661b9d
AM
8320 case elfcpp::R_PPC64_REL64:
8321 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8322 case elfcpp::R_POWERPC_REL16:
6ce78956 8323 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 8324 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 8325 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8326 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8327 case elfcpp::R_PPC64_REL16_HIGH:
8328 case elfcpp::R_PPC64_REL16_HIGHA:
8329 case elfcpp::R_PPC64_REL16_HIGHER:
8330 case elfcpp::R_PPC64_REL16_HIGHERA:
8331 case elfcpp::R_PPC64_REL16_HIGHEST:
8332 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8333 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8334 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8335 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8336 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8337 case elfcpp::R_PPC64_SECTOFF_DS:
8338 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8339 case elfcpp::R_POWERPC_TPREL16:
8340 case elfcpp::R_POWERPC_TPREL16_LO:
8341 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8342 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8343 case elfcpp::R_PPC64_TPREL16_DS:
8344 case elfcpp::R_PPC64_TPREL16_LO_DS:
8345 case elfcpp::R_PPC64_TPREL16_HIGH:
8346 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8347 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8348 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8349 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8350 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8351 case elfcpp::R_POWERPC_DTPREL16:
8352 case elfcpp::R_POWERPC_DTPREL16_LO:
8353 case elfcpp::R_POWERPC_DTPREL16_HI:
8354 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8355 case elfcpp::R_PPC64_DTPREL16_DS:
8356 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8357 case elfcpp::R_PPC64_DTPREL16_HIGH:
8358 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8359 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8360 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8361 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8362 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8363 case elfcpp::R_PPC64_TLSGD:
8364 case elfcpp::R_PPC64_TLSLD:
45965137 8365 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
8366 break;
8367
e4dff765 8368 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8369 case elfcpp::R_POWERPC_GOT16:
8370 case elfcpp::R_POWERPC_GOT16_LO:
8371 case elfcpp::R_POWERPC_GOT16_HI:
8372 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8373 case elfcpp::R_PPC64_GOT16_DS:
8374 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8375 {
c9269dff 8376 // The symbol requires a GOT entry.
f19c3684
AM
8377 Got_type got_type = ((size == 32
8378 || r_type == elfcpp::R_POWERPC_GOT16
8379 || r_type == elfcpp::R_PPC64_GOT16_DS)
8380 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
dd93cd0a 8381 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8382 = target->got_section(symtab, layout, got_type);
dd93cd0a 8383 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8384 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
42cacb20 8385
e5d5f5ed 8386 if (!parameters->options().output_is_position_independent())
42cacb20 8387 {
b01a4b04
AM
8388 if (is_ifunc
8389 && (size == 32 || target->abiversion() >= 2))
f19c3684 8390 got->add_local_plt(object, r_sym, got_type, addend);
e5d5f5ed 8391 else
f19c3684 8392 got->add_local(object, r_sym, got_type, addend);
e5d5f5ed 8393 }
f19c3684 8394 else if (!object->local_has_got_offset(r_sym, got_type, addend))
e5d5f5ed
AM
8395 {
8396 // If we are generating a shared object or a pie, this
8397 // symbol's GOT entry will be set by a dynamic relocation.
8398 unsigned int off;
8399 off = got->add_constant(0);
f19c3684 8400 object->set_local_got_offset(r_sym, got_type, off, addend);
42cacb20 8401
b3ccdeb5
AM
8402 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8403 is_ifunc);
8404 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8405 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 8406 rela_dyn->add_local_relative(object, r_sym, dynrel,
5cdb4f14 8407 got, off, addend, false);
2e702c99 8408 }
42cacb20
DE
8409 }
8410 break;
8411
cf43a2fe
AM
8412 case elfcpp::R_PPC64_TOC16:
8413 case elfcpp::R_PPC64_TOC16_LO:
8414 case elfcpp::R_PPC64_TOC16_HI:
8415 case elfcpp::R_PPC64_TOC16_HA:
8416 case elfcpp::R_PPC64_TOC16_DS:
8417 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20 8418 // We need a GOT section.
f19c3684 8419 target->got_section(symtab, layout, GOT_TYPE_SMALL);
42cacb20
DE
8420 break;
8421
87c69f97 8422 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
8423 case elfcpp::R_POWERPC_GOT_TLSGD16:
8424 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8425 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8426 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8427 {
c7fdac09 8428 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
dd93cd0a
AM
8429 if (tls_type == tls::TLSOPT_NONE)
8430 {
f19c3684
AM
8431 Got_type got_type = ((size == 32
8432 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8433 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
dd93cd0a 8434 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8435 = target->got_section(symtab, layout, got_type);
dd93cd0a 8436 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8437 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
bd73a62d 8438 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
f19c3684 8439 got->add_local_tls_pair(object, r_sym, got_type,
5cdb4f14
AM
8440 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8441 addend);
dd93cd0a
AM
8442 }
8443 else if (tls_type == tls::TLSOPT_TO_LE)
8444 {
8445 // no GOT relocs needed for Local Exec.
8446 }
8447 else
8448 gold_unreachable();
8449 }
42cacb20
DE
8450 break;
8451
87c69f97 8452 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
8453 case elfcpp::R_POWERPC_GOT_TLSLD16:
8454 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8455 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8456 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8457 {
c7fdac09 8458 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
8459 if (tls_type == tls::TLSOPT_NONE)
8460 target->tlsld_got_offset(symtab, layout, object);
8461 else if (tls_type == tls::TLSOPT_TO_LE)
8462 {
8463 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8464 if (parameters->options().emit_relocs())
8465 {
8466 Output_section* os = layout->tls_segment()->first_section();
8467 gold_assert(os != NULL);
8468 os->set_needs_symtab_index();
8469 }
dd93cd0a
AM
8470 }
8471 else
8472 gold_unreachable();
8473 }
42cacb20 8474 break;
42cacb20 8475
87c69f97 8476 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
8477 case elfcpp::R_POWERPC_GOT_DTPREL16:
8478 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8479 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8480 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8481 {
f19c3684
AM
8482 Got_type got_type = ((size == 32
8483 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8484 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 8485 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8486 = target->got_section(symtab, layout, got_type);
dd93cd0a 8487 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8488 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684 8489 got->add_local_tls(object, r_sym, got_type, addend);
dd93cd0a
AM
8490 }
8491 break;
42cacb20 8492
87c69f97 8493 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
8494 case elfcpp::R_POWERPC_GOT_TPREL16:
8495 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8496 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8497 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8498 {
0af4fcc2 8499 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
dd93cd0a
AM
8500 if (tls_type == tls::TLSOPT_NONE)
8501 {
dd93cd0a 8502 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5cdb4f14 8503 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684
AM
8504 Got_type got_type = ((size == 32
8505 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8506 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8507 if (!object->local_has_got_offset(r_sym, got_type, addend))
acc276d8
AM
8508 {
8509 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8510 = target->got_section(symtab, layout, got_type);
acc276d8 8511 unsigned int off = got->add_constant(0);
f19c3684 8512 object->set_local_got_offset(r_sym, got_type, off, addend);
acc276d8
AM
8513
8514 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8515 rela_dyn->add_symbolless_local_addend(object, r_sym,
8516 elfcpp::R_POWERPC_TPREL,
5cdb4f14 8517 got, off, addend);
acc276d8 8518 }
dd93cd0a
AM
8519 }
8520 else if (tls_type == tls::TLSOPT_TO_LE)
8521 {
8522 // no GOT relocs needed for Local Exec.
8523 }
8524 else
8525 gold_unreachable();
8526 }
8527 break;
8528
8529 default:
8530 unsupported_reloc_local(object, r_type);
8531 break;
8532 }
d8f5a274 8533
5edad15d
AM
8534 if (size == 64
8535 && parameters->options().toc_optimize())
8536 {
8537 if (data_shndx == ppc_object->toc_shndx())
8538 {
8539 bool ok = true;
8540 if (r_type != elfcpp::R_PPC64_ADDR64
8541 || (is_ifunc && target->abiversion() < 2))
8542 ok = false;
8543 else if (parameters->options().output_is_position_independent())
8544 {
8545 if (is_ifunc)
8546 ok = false;
8547 else
8548 {
8549 unsigned int shndx = lsym.get_st_shndx();
8550 if (shndx >= elfcpp::SHN_LORESERVE
8551 && shndx != elfcpp::SHN_XINDEX)
8552 ok = false;
8553 }
8554 }
8555 if (!ok)
8556 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8557 }
8558
8559 enum {no_check, check_lo, check_ha} insn_check;
8560 switch (r_type)
8561 {
8562 default:
8563 insn_check = no_check;
8564 break;
8565
8566 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8567 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8568 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8569 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8570 case elfcpp::R_POWERPC_GOT16_HA:
8571 case elfcpp::R_PPC64_TOC16_HA:
8572 insn_check = check_ha;
8573 break;
8574
8575 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8576 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8577 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8578 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8579 case elfcpp::R_POWERPC_GOT16_LO:
8580 case elfcpp::R_PPC64_GOT16_LO_DS:
8581 case elfcpp::R_PPC64_TOC16_LO:
8582 case elfcpp::R_PPC64_TOC16_LO_DS:
8583 insn_check = check_lo;
8584 break;
8585 }
8586
8587 section_size_type slen;
8588 const unsigned char* view = NULL;
8589 if (insn_check != no_check)
8590 {
8591 view = ppc_object->section_contents(data_shndx, &slen, false);
8592 section_size_type off =
8593 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8594 if (off < slen)
8595 {
8596 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8597 if (insn_check == check_lo
8598 ? !ok_lo_toc_insn(insn, r_type)
8599 : ((insn & ((0x3f << 26) | 0x1f << 16))
8600 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8601 {
8602 ppc_object->set_no_toc_opt();
8603 gold_warning(_("%s: toc optimization is not supported "
8604 "for %#08x instruction"),
8605 ppc_object->name().c_str(), insn);
8606 }
8607 }
8608 }
8609
8610 switch (r_type)
8611 {
8612 default:
8613 break;
8614 case elfcpp::R_PPC64_TOC16:
8615 case elfcpp::R_PPC64_TOC16_LO:
8616 case elfcpp::R_PPC64_TOC16_HI:
8617 case elfcpp::R_PPC64_TOC16_HA:
8618 case elfcpp::R_PPC64_TOC16_DS:
8619 case elfcpp::R_PPC64_TOC16_LO_DS:
8620 unsigned int shndx = lsym.get_st_shndx();
8621 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8622 bool is_ordinary;
8623 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8624 if (is_ordinary && shndx == ppc_object->toc_shndx())
8625 {
412294da 8626 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8627 if (dst_off < ppc_object->section_size(shndx))
8628 {
8629 bool ok = false;
8630 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8631 ok = true;
8632 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8633 {
8634 // Need to check that the insn is a ld
8635 if (!view)
8636 view = ppc_object->section_contents(data_shndx,
8637 &slen,
8638 false);
8639 section_size_type off =
8640 (convert_to_section_size_type(reloc.get_r_offset())
8641 + (big_endian ? -2 : 3));
8642 if (off < slen
8643 && (view[off] & (0x3f << 2)) == 58u << 2)
8644 ok = true;
8645 }
8646 if (!ok)
8647 ppc_object->set_no_toc_opt(dst_off);
8648 }
8649 }
8650 break;
8651 }
8652 }
8653
f159cdb6
AM
8654 if (size == 32)
8655 {
8656 switch (r_type)
8657 {
8658 case elfcpp::R_POWERPC_REL32:
8659 if (ppc_object->got2_shndx() != 0
8660 && parameters->options().output_is_position_independent())
8661 {
8662 unsigned int shndx = lsym.get_st_shndx();
8663 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8664 bool is_ordinary;
8665 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8666 if (is_ordinary && shndx == ppc_object->got2_shndx()
8667 && (ppc_object->section_flags(data_shndx)
8668 & elfcpp::SHF_EXECINSTR) != 0)
8669 gold_error(_("%s: unsupported -mbss-plt code"),
8670 ppc_object->name().c_str());
8671 }
8672 break;
8673 default:
8674 break;
8675 }
8676 }
8677
d8f5a274
AM
8678 switch (r_type)
8679 {
8680 case elfcpp::R_POWERPC_GOT_TLSLD16:
8681 case elfcpp::R_POWERPC_GOT_TLSGD16:
8682 case elfcpp::R_POWERPC_GOT_TPREL16:
8683 case elfcpp::R_POWERPC_GOT_DTPREL16:
8684 case elfcpp::R_POWERPC_GOT16:
8685 case elfcpp::R_PPC64_GOT16_DS:
8686 case elfcpp::R_PPC64_TOC16:
8687 case elfcpp::R_PPC64_TOC16_DS:
8688 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8689 break;
8690 default:
8691 break;
8692 }
8693
8694 switch (r_type)
8695 {
89c52ae3
AM
8696 case elfcpp::R_PPC64_TPREL16_DS:
8697 case elfcpp::R_PPC64_TPREL16_LO_DS:
8698 case elfcpp::R_PPC64_TPREL16_HIGH:
8699 case elfcpp::R_PPC64_TPREL16_HIGHA:
8700 case elfcpp::R_PPC64_TPREL16_HIGHER:
8701 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8702 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8703 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8704 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
8705 if (size != 64)
8706 break;
8707 // Fall through.
8708 case elfcpp::R_POWERPC_TPREL16:
8709 case elfcpp::R_POWERPC_TPREL16_LO:
8710 case elfcpp::R_POWERPC_TPREL16_HI:
8711 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8712 layout->set_has_static_tls();
8713 break;
8714 default:
8715 break;
8716 }
8717
252dcdf4
AM
8718 switch (r_type)
8719 {
8720 case elfcpp::R_POWERPC_TPREL16_HA:
8721 if (target->tprel_opt())
8722 {
8723 section_size_type slen;
8724 const unsigned char* view = NULL;
8725 view = ppc_object->section_contents(data_shndx, &slen, false);
8726 section_size_type off
8727 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8728 if (off < slen)
8729 {
8730 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8731 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8732 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 8733 target->set_no_tprel_opt();
252dcdf4
AM
8734 }
8735 }
8736 break;
8737
8738 case elfcpp::R_PPC64_TPREL16_HIGH:
8739 case elfcpp::R_PPC64_TPREL16_HIGHA:
8740 case elfcpp::R_PPC64_TPREL16_HIGHER:
8741 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8742 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8743 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8744 if (size != 64)
8745 break;
8746 // Fall through.
8747 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 8748 target->set_no_tprel_opt();
252dcdf4
AM
8749 break;
8750 default:
8751 break;
8752 }
8753
89c52ae3
AM
8754 switch (r_type)
8755 {
8756 case elfcpp::R_PPC64_D34:
8757 case elfcpp::R_PPC64_D34_LO:
8758 case elfcpp::R_PPC64_D34_HI30:
8759 case elfcpp::R_PPC64_D34_HA30:
8760 case elfcpp::R_PPC64_D28:
8761 case elfcpp::R_PPC64_PCREL34:
8762 case elfcpp::R_PPC64_PCREL28:
8763 case elfcpp::R_PPC64_TPREL34:
8764 case elfcpp::R_PPC64_DTPREL34:
8765 case elfcpp::R_PPC64_PLT_PCREL34:
8766 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8767 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
8768 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8769 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8770 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8771 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 8772 target->set_power10_relocs();
89c52ae3 8773 break;
d8f5a274
AM
8774 default:
8775 break;
8776 }
dd93cd0a
AM
8777}
8778
8779// Report an unsupported relocation against a global symbol.
8780
8781template<int size, bool big_endian>
8782void
8783Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8784 Sized_relobj_file<size, big_endian>* object,
8785 unsigned int r_type,
8786 Symbol* gsym)
8787{
42cacb20
DE
8788 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8789 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8790}
8791
8792// Scan a relocation for a global symbol.
8793
8794template<int size, bool big_endian>
8795inline void
8796Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8797 Symbol_table* symtab,
8798 Layout* layout,
8799 Target_powerpc<size, big_endian>* target,
8800 Sized_relobj_file<size, big_endian>* object,
8801 unsigned int data_shndx,
8802 Output_section* output_section,
8803 const elfcpp::Rela<size, big_endian>& reloc,
8804 unsigned int r_type,
8805 Symbol* gsym)
42cacb20 8806{
0af4fcc2
AM
8807 Powerpc_relobj<size, big_endian>* ppc_object
8808 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8809
8810 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8811 {
0af4fcc2
AM
8812 case Track_tls::SKIP:
8813 return;
8814 default:
8815 break;
8816 }
e3deeb9c 8817
34e0882b
AM
8818 if (target->replace_tls_get_addr(gsym))
8819 // Change a __tls_get_addr reference to __tls_get_addr_opt
8820 // so dynamic relocs are emitted against the latter symbol.
8821 gsym = target->tls_get_addr_opt();
8822
e3deeb9c
AM
8823 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8824 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8825 {
8826 this->expect_tls_get_addr_call();
c7fdac09
AM
8827 bool final = gsym->final_value_is_known();
8828 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8829 if (tls_type != tls::TLSOPT_NONE)
8830 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8831 }
8832 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8833 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8834 {
8835 this->expect_tls_get_addr_call();
c7fdac09
AM
8836 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8837 if (tls_type != tls::TLSOPT_NONE)
8838 this->skip_next_tls_get_addr_call();
e3deeb9c
AM
8839 }
8840
e5d5f5ed 8841 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8842 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8843 bool pushed_ifunc = false;
8844 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8845 {
0e123f69 8846 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8847 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8848 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8849 target->make_plt_entry(symtab, layout, gsym);
9055360d 8850 pushed_ifunc = true;
ec661b9d 8851 }
e5d5f5ed 8852
42cacb20
DE
8853 switch (r_type)
8854 {
8855 case elfcpp::R_POWERPC_NONE:
8856 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8857 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8858 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8859 case elfcpp::R_POWERPC_TLS:
549dba71 8860 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8861 case elfcpp::R_POWERPC_PLTSEQ:
8862 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8863 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8864 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8865 case elfcpp::R_PPC64_PCREL_OPT:
8866 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8867 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8868 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8869 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8870 case elfcpp::R_PPC64_REL16_HIGHER34:
8871 case elfcpp::R_PPC64_REL16_HIGHERA34:
8872 case elfcpp::R_PPC64_REL16_HIGHEST34:
8873 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8874 case elfcpp::R_PPC64_D34:
8875 case elfcpp::R_PPC64_D34_LO:
8876 case elfcpp::R_PPC64_D34_HI30:
8877 case elfcpp::R_PPC64_D34_HA30:
8878 case elfcpp::R_PPC64_D28:
8879 case elfcpp::R_PPC64_PCREL34:
8880 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8881 case elfcpp::R_PPC64_TPREL34:
8882 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8883 break;
8884
8885 case elfcpp::R_PPC64_TOC:
8886 {
8887 Output_data_got_powerpc<size, big_endian>* got
f19c3684 8888 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
dd93cd0a
AM
8889 if (parameters->options().output_is_position_independent())
8890 {
bfdfa4cd
AM
8891 Address off = reloc.get_r_offset();
8892 if (size == 64
8893 && data_shndx == ppc_object->opd_shndx()
8894 && ppc_object->get_opd_discard(off - 8))
8895 break;
8896
dd93cd0a 8897 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
a1251fdc 8898 Address got_off = got->g_o_t();
dd93cd0a
AM
8899 rela_dyn->add_output_section_relative(got->output_section(),
8900 elfcpp::R_POWERPC_RELATIVE,
8901 output_section,
bfdfa4cd 8902 object, data_shndx, off,
a19da04b 8903 got_off);
dd93cd0a
AM
8904 }
8905 }
42cacb20
DE
8906 break;
8907
c9269dff 8908 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8909 if (size == 64
9055360d 8910 && target->abiversion() < 2
bfdfa4cd
AM
8911 && data_shndx == ppc_object->opd_shndx()
8912 && (gsym->is_defined_in_discarded_section()
8913 || gsym->object() != object))
8914 {
8915 ppc_object->set_opd_discard(reloc.get_r_offset());
8916 break;
8917 }
d8e90251 8918 // Fall through.
dd93cd0a 8919 case elfcpp::R_PPC64_UADDR64:
c9269dff 8920 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8921 case elfcpp::R_POWERPC_UADDR32:
8922 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8923 case elfcpp::R_POWERPC_ADDR16:
8924 case elfcpp::R_POWERPC_ADDR16_LO:
8925 case elfcpp::R_POWERPC_ADDR16_HI:
8926 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8927 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8928 case elfcpp::R_PPC64_ADDR16_HIGH:
8929 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8930 case elfcpp::R_PPC64_ADDR16_HIGHER:
8931 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8932 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8933 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8934 case elfcpp::R_PPC64_ADDR16_DS:
8935 case elfcpp::R_PPC64_ADDR16_LO_DS:
8936 case elfcpp::R_POWERPC_ADDR14:
8937 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8938 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8939 {
c9269dff
AM
8940 // Make a PLT entry if necessary.
8941 if (gsym->needs_plt_entry())
8942 {
9055360d
AM
8943 // Since this is not a PC-relative relocation, we may be
8944 // taking the address of a function. In that case we need to
8945 // set the entry in the dynamic symbol table to the address of
8946 // the PLT call stub.
8947 bool need_ifunc_plt = false;
8948 if ((size == 32 || target->abiversion() >= 2)
8949 && gsym->is_from_dynobj()
8950 && !parameters->options().output_is_position_independent())
8951 {
8952 gsym->set_needs_dynsym_value();
8953 need_ifunc_plt = true;
8954 }
8955 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8956 {
0e123f69 8957 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8958 target->push_branch(ppc_object, data_shndx,
0e123f69 8959 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8960 reloc.get_r_addend());
8961 target->make_plt_entry(symtab, layout, gsym);
8962 }
c9269dff
AM
8963 }
8964 // Make a dynamic relocation if necessary.
88b8e639 8965 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8966 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8967 {
a82bef93
ST
8968 if (!parameters->options().output_is_position_independent()
8969 && gsym->may_need_copy_reloc())
c9269dff
AM
8970 {
8971 target->copy_reloc(symtab, layout, object,
8972 data_shndx, output_section, gsym, reloc);
8973 }
9055360d
AM
8974 else if ((((size == 32
8975 && r_type == elfcpp::R_POWERPC_ADDR32)
8976 || (size == 64
8977 && r_type == elfcpp::R_PPC64_ADDR64
8978 && target->abiversion() >= 2))
627b30b7
AM
8979 && gsym->can_use_relative_reloc(false)
8980 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8981 && parameters->options().shared()))
8982 || (size == 64
8983 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8984 && target->abiversion() < 2
627b30b7
AM
8985 && (gsym->can_use_relative_reloc(false)
8986 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8987 {
b3ccdeb5
AM
8988 Reloc_section* rela_dyn
8989 = target->rela_dyn_section(symtab, layout, is_ifunc);
8990 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8991 : elfcpp::R_POWERPC_RELATIVE);
027614ab
AM
8992 // Use the "add" method that marks the reloc as being
8993 // relative. This is proper here and in other places
8994 // that add IRELATIVE relocs because those relocs go
8995 // into a separate section that isn't sorted, so it
8996 // doesn't matter that they are marked is_relative.
8997 rela_dyn->add_global_relative(
e5d5f5ed 8998 gsym, dynrel, output_section, object, data_shndx,
027614ab 8999 reloc.get_r_offset(), reloc.get_r_addend(), false);
2e702c99
RM
9000 }
9001 else
9002 {
b3ccdeb5
AM
9003 Reloc_section* rela_dyn
9004 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 9005 check_non_pic(object, r_type);
dd93cd0a
AM
9006 rela_dyn->add_global(gsym, r_type, output_section,
9007 object, data_shndx,
9008 reloc.get_r_offset(),
9009 reloc.get_r_addend());
5edad15d
AM
9010
9011 if (size == 64
9012 && parameters->options().toc_optimize()
9013 && data_shndx == ppc_object->toc_shndx())
9014 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
9015 }
9016 }
42cacb20
DE
9017 }
9018 break;
9019
e4dff765
AM
9020 case elfcpp::R_PPC64_PLT_PCREL34:
9021 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
9022 case elfcpp::R_POWERPC_PLT16_LO:
9023 case elfcpp::R_POWERPC_PLT16_HI:
9024 case elfcpp::R_POWERPC_PLT16_HA:
9025 case elfcpp::R_PPC64_PLT16_LO_DS:
9026 if (!pushed_ifunc)
f21dbd7c 9027 {
0961e631 9028 if (branch_needs_plt_entry(gsym))
f21dbd7c
AM
9029 target->make_plt_entry(symtab, layout, gsym);
9030 else
9031 target->make_local_plt_entry(symtab, layout, gsym);
9032 }
08be3224
AM
9033 break;
9034
32f59844
AM
9035 case elfcpp::R_PPC64_REL24_NOTOC:
9036 if (size == 32)
9037 break;
9038 // Fall through.
eb97d024 9039 case elfcpp::R_PPC64_REL24_P9NOTOC:
cf43a2fe 9040 case elfcpp::R_PPC_PLTREL24:
42cacb20 9041 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
9042 if (!is_ifunc)
9043 {
0e123f69 9044 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 9045 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 9046 r_type, r_sym, reloc.get_r_addend());
0961e631 9047 if (branch_needs_plt_entry(gsym))
b3ccdeb5
AM
9048 target->make_plt_entry(symtab, layout, gsym);
9049 }
d8e90251 9050 // Fall through.
42cacb20 9051
3ea0a085 9052 case elfcpp::R_PPC64_REL64:
dd93cd0a 9053 case elfcpp::R_POWERPC_REL32:
3ea0a085 9054 // Make a dynamic relocation if necessary.
88b8e639 9055 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 9056 {
a82bef93
ST
9057 if (!parameters->options().output_is_position_independent()
9058 && gsym->may_need_copy_reloc())
3ea0a085
AM
9059 {
9060 target->copy_reloc(symtab, layout, object,
9061 data_shndx, output_section, gsym,
9062 reloc);
9063 }
9064 else
9065 {
b3ccdeb5
AM
9066 Reloc_section* rela_dyn
9067 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
9068 check_non_pic(object, r_type);
9069 rela_dyn->add_global(gsym, r_type, output_section, object,
9070 data_shndx, reloc.get_r_offset(),
9071 reloc.get_r_addend());
9072 }
9073 }
9074 break;
9075
ec661b9d
AM
9076 case elfcpp::R_POWERPC_REL14:
9077 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9078 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 9079 if (!is_ifunc)
0e123f69
AM
9080 {
9081 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9082 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9083 r_type, r_sym, reloc.get_r_addend());
9084 }
ec661b9d
AM
9085 break;
9086
7e57d19e
AM
9087 case elfcpp::R_PPC64_TOCSAVE:
9088 // R_PPC64_TOCSAVE follows a call instruction to indicate the
9089 // caller has already saved r2 and thus a plt call stub need not
9090 // save r2.
9091 if (size == 64
9092 && target->mark_pltcall(ppc_object, data_shndx,
9093 reloc.get_r_offset() - 4, symtab))
9094 {
9095 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9096 bool is_ordinary;
9097 unsigned int shndx = gsym->shndx(&is_ordinary);
9098 if (!is_ordinary)
9099 object->error(_("tocsave symbol %u has bad shndx %u"),
9100 r_sym, shndx);
9101 else
9102 {
9103 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9104 target->add_tocsave(ppc_object, shndx,
9105 sym->value() + reloc.get_r_addend());
9106 }
9107 }
9108 break;
9109
6ce78956
AM
9110 case elfcpp::R_POWERPC_REL16:
9111 case elfcpp::R_POWERPC_REL16_LO:
9112 case elfcpp::R_POWERPC_REL16_HI:
9113 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 9114 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
9115 case elfcpp::R_PPC64_REL16_HIGH:
9116 case elfcpp::R_PPC64_REL16_HIGHA:
9117 case elfcpp::R_PPC64_REL16_HIGHER:
9118 case elfcpp::R_PPC64_REL16_HIGHERA:
9119 case elfcpp::R_PPC64_REL16_HIGHEST:
9120 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 9121 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 9122 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 9123 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 9124 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
9125 case elfcpp::R_PPC64_SECTOFF_DS:
9126 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9127 case elfcpp::R_POWERPC_TPREL16:
9128 case elfcpp::R_POWERPC_TPREL16_LO:
9129 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 9130 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
9131 case elfcpp::R_PPC64_TPREL16_DS:
9132 case elfcpp::R_PPC64_TPREL16_LO_DS:
9133 case elfcpp::R_PPC64_TPREL16_HIGH:
9134 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 9135 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 9136 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 9137 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 9138 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
9139 case elfcpp::R_POWERPC_DTPREL16:
9140 case elfcpp::R_POWERPC_DTPREL16_LO:
9141 case elfcpp::R_POWERPC_DTPREL16_HI:
9142 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
9143 case elfcpp::R_PPC64_DTPREL16_DS:
9144 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
9145 case elfcpp::R_PPC64_DTPREL16_HIGH:
9146 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9147 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9148 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9149 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9150 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
9151 case elfcpp::R_PPC64_TLSGD:
9152 case elfcpp::R_PPC64_TLSLD:
45965137 9153 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
9154 break;
9155
e4dff765 9156 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
9157 case elfcpp::R_POWERPC_GOT16:
9158 case elfcpp::R_POWERPC_GOT16_LO:
9159 case elfcpp::R_POWERPC_GOT16_HI:
9160 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
9161 case elfcpp::R_PPC64_GOT16_DS:
9162 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 9163 {
c9269dff
AM
9164 // The symbol requires a GOT entry.
9165 Output_data_got_powerpc<size, big_endian>* got;
5cdb4f14 9166 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684
AM
9167 Got_type got_type = ((size == 32
9168 || r_type == elfcpp::R_POWERPC_GOT16
9169 || r_type == elfcpp::R_PPC64_GOT16_DS)
9170 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
42cacb20 9171
f19c3684 9172 got = target->got_section(symtab, layout, got_type);
2e702c99 9173 if (gsym->final_value_is_known())
2e702c99 9174 {
b01a4b04
AM
9175 if (is_ifunc
9176 && (size == 32 || target->abiversion() >= 2))
f19c3684 9177 got->add_global_plt(gsym, got_type, addend);
e5d5f5ed 9178 else
f19c3684 9179 got->add_global(gsym, got_type, addend);
e5d5f5ed 9180 }
f19c3684 9181 else if (!gsym->has_got_offset(got_type, addend))
e5d5f5ed
AM
9182 {
9183 // If we are generating a shared object or a pie, this
9184 // symbol's GOT entry will be set by a dynamic relocation.
9185 unsigned int off = got->add_constant(0);
f19c3684 9186 gsym->set_got_offset(got_type, off, addend);
e5d5f5ed 9187
b3ccdeb5
AM
9188 Reloc_section* rela_dyn
9189 = target->rela_dyn_section(symtab, layout, is_ifunc);
9190
e5d5f5ed 9191 if (gsym->can_use_relative_reloc(false)
9055360d
AM
9192 && !((size == 32
9193 || target->abiversion() >= 2)
e5d5f5ed
AM
9194 && gsym->visibility() == elfcpp::STV_PROTECTED
9195 && parameters->options().shared()))
2e702c99 9196 {
b3ccdeb5
AM
9197 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9198 : elfcpp::R_POWERPC_RELATIVE);
5cdb4f14
AM
9199 rela_dyn->add_global_relative(gsym, dynrel, got, off,
9200 addend, false);
e5d5f5ed
AM
9201 }
9202 else
9203 {
9204 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5cdb4f14 9205 rela_dyn->add_global(gsym, dynrel, got, off, addend);
42cacb20 9206 }
2e702c99 9207 }
42cacb20
DE
9208 }
9209 break;
9210
cf43a2fe
AM
9211 case elfcpp::R_PPC64_TOC16:
9212 case elfcpp::R_PPC64_TOC16_LO:
9213 case elfcpp::R_PPC64_TOC16_HI:
9214 case elfcpp::R_PPC64_TOC16_HA:
9215 case elfcpp::R_PPC64_TOC16_DS:
9216 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20 9217 // We need a GOT section.
f19c3684 9218 target->got_section(symtab, layout, GOT_TYPE_SMALL);
42cacb20
DE
9219 break;
9220
87c69f97 9221 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
dd93cd0a
AM
9222 case elfcpp::R_POWERPC_GOT_TLSGD16:
9223 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9224 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9225 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9226 {
c7fdac09
AM
9227 bool final = gsym->final_value_is_known();
9228 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
dd93cd0a
AM
9229 if (tls_type == tls::TLSOPT_NONE)
9230 {
f19c3684
AM
9231 Got_type got_type = ((size == 32
9232 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9233 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
dd93cd0a 9234 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9235 = target->got_section(symtab, layout, got_type);
b3ccdeb5 9236 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9237 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
f19c3684 9238 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
dd93cd0a 9239 elfcpp::R_POWERPC_DTPMOD,
5cdb4f14
AM
9240 elfcpp::R_POWERPC_DTPREL,
9241 addend);
dd93cd0a
AM
9242 }
9243 else if (tls_type == tls::TLSOPT_TO_IE)
9244 {
f19c3684
AM
9245 Got_type got_type = ((size == 32
9246 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9247 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9248 if (!gsym->has_got_offset(got_type))
acc276d8
AM
9249 {
9250 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9251 = target->got_section(symtab, layout, got_type);
acc276d8 9252 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9253 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
acc276d8
AM
9254 if (gsym->is_undefined()
9255 || gsym->is_from_dynobj())
9256 {
f19c3684 9257 got->add_global_with_rel(gsym, got_type, rela_dyn,
5cdb4f14 9258 elfcpp::R_POWERPC_TPREL, addend);
acc276d8
AM
9259 }
9260 else
9261 {
9262 unsigned int off = got->add_constant(0);
f19c3684 9263 gsym->set_got_offset(got_type, off);
acc276d8
AM
9264 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9265 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5cdb4f14 9266 got, off, addend);
acc276d8
AM
9267 }
9268 }
dd93cd0a
AM
9269 }
9270 else if (tls_type == tls::TLSOPT_TO_LE)
9271 {
9272 // no GOT relocs needed for Local Exec.
9273 }
9274 else
9275 gold_unreachable();
9276 }
42cacb20
DE
9277 break;
9278
87c69f97 9279 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
dd93cd0a
AM
9280 case elfcpp::R_POWERPC_GOT_TLSLD16:
9281 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9282 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9283 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9284 {
c7fdac09 9285 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
9286 if (tls_type == tls::TLSOPT_NONE)
9287 target->tlsld_got_offset(symtab, layout, object);
9288 else if (tls_type == tls::TLSOPT_TO_LE)
9289 {
9290 // no GOT relocs needed for Local Exec.
7404fe1b
AM
9291 if (parameters->options().emit_relocs())
9292 {
9293 Output_section* os = layout->tls_segment()->first_section();
9294 gold_assert(os != NULL);
9295 os->set_needs_symtab_index();
9296 }
dd93cd0a
AM
9297 }
9298 else
9299 gold_unreachable();
9300 }
9301 break;
9302
87c69f97 9303 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
dd93cd0a
AM
9304 case elfcpp::R_POWERPC_GOT_DTPREL16:
9305 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9306 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9307 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9308 {
f19c3684
AM
9309 Got_type got_type = ((size == 32
9310 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9311 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 9312 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9313 = target->got_section(symtab, layout, got_type);
5cdb4f14 9314 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
bd73a62d
AM
9315 if (!gsym->final_value_is_known()
9316 && (gsym->is_from_dynobj()
9317 || gsym->is_undefined()
9318 || gsym->is_preemptible()))
f19c3684 9319 got->add_global_with_rel(gsym, got_type,
bd73a62d 9320 target->rela_dyn_section(layout),
5cdb4f14 9321 elfcpp::R_POWERPC_DTPREL, addend);
bd73a62d 9322 else
f19c3684 9323 got->add_global_tls(gsym, got_type, addend);
dd93cd0a
AM
9324 }
9325 break;
9326
87c69f97 9327 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
dd93cd0a
AM
9328 case elfcpp::R_POWERPC_GOT_TPREL16:
9329 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9330 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9331 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9332 {
0af4fcc2
AM
9333 bool final = gsym->final_value_is_known();
9334 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
9335 if (tls_type == tls::TLSOPT_NONE)
9336 {
f19c3684
AM
9337 Got_type got_type = ((size == 32
9338 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9339 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9340 if (!gsym->has_got_offset(got_type))
acc276d8
AM
9341 {
9342 Output_data_got_powerpc<size, big_endian>* got
f19c3684 9343 = target->got_section(symtab, layout, got_type);
acc276d8 9344 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5cdb4f14 9345 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
acc276d8
AM
9346 if (gsym->is_undefined()
9347 || gsym->is_from_dynobj())
9348 {
f19c3684 9349 got->add_global_with_rel(gsym, got_type, rela_dyn,
5cdb4f14 9350 elfcpp::R_POWERPC_TPREL, addend);
acc276d8
AM
9351 }
9352 else
9353 {
9354 unsigned int off = got->add_constant(0);
f19c3684 9355 gsym->set_got_offset(got_type, off);
acc276d8
AM
9356 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9357 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5cdb4f14 9358 got, off, addend);
acc276d8
AM
9359 }
9360 }
dd93cd0a
AM
9361 }
9362 else if (tls_type == tls::TLSOPT_TO_LE)
9363 {
9364 // no GOT relocs needed for Local Exec.
9365 }
9366 else
9367 gold_unreachable();
9368 }
42cacb20
DE
9369 break;
9370
9371 default:
9372 unsupported_reloc_global(object, r_type, gsym);
9373 break;
9374 }
d8f5a274 9375
5edad15d
AM
9376 if (size == 64
9377 && parameters->options().toc_optimize())
9378 {
9379 if (data_shndx == ppc_object->toc_shndx())
9380 {
9381 bool ok = true;
9382 if (r_type != elfcpp::R_PPC64_ADDR64
9383 || (is_ifunc && target->abiversion() < 2))
9384 ok = false;
9385 else if (parameters->options().output_is_position_independent()
9386 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9387 ok = false;
9388 if (!ok)
9389 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9390 }
9391
9392 enum {no_check, check_lo, check_ha} insn_check;
9393 switch (r_type)
9394 {
9395 default:
9396 insn_check = no_check;
9397 break;
9398
9399 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9400 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9401 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9402 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9403 case elfcpp::R_POWERPC_GOT16_HA:
9404 case elfcpp::R_PPC64_TOC16_HA:
9405 insn_check = check_ha;
9406 break;
9407
9408 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9409 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9410 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9411 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9412 case elfcpp::R_POWERPC_GOT16_LO:
9413 case elfcpp::R_PPC64_GOT16_LO_DS:
9414 case elfcpp::R_PPC64_TOC16_LO:
9415 case elfcpp::R_PPC64_TOC16_LO_DS:
9416 insn_check = check_lo;
9417 break;
9418 }
9419
9420 section_size_type slen;
9421 const unsigned char* view = NULL;
9422 if (insn_check != no_check)
9423 {
9424 view = ppc_object->section_contents(data_shndx, &slen, false);
9425 section_size_type off =
9426 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9427 if (off < slen)
9428 {
9429 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9430 if (insn_check == check_lo
9431 ? !ok_lo_toc_insn(insn, r_type)
9432 : ((insn & ((0x3f << 26) | 0x1f << 16))
9433 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9434 {
9435 ppc_object->set_no_toc_opt();
9436 gold_warning(_("%s: toc optimization is not supported "
9437 "for %#08x instruction"),
9438 ppc_object->name().c_str(), insn);
9439 }
9440 }
9441 }
9442
9443 switch (r_type)
9444 {
9445 default:
9446 break;
9447 case elfcpp::R_PPC64_TOC16:
9448 case elfcpp::R_PPC64_TOC16_LO:
9449 case elfcpp::R_PPC64_TOC16_HI:
9450 case elfcpp::R_PPC64_TOC16_HA:
9451 case elfcpp::R_PPC64_TOC16_DS:
9452 case elfcpp::R_PPC64_TOC16_LO_DS:
9453 if (gsym->source() == Symbol::FROM_OBJECT
9454 && !gsym->object()->is_dynamic())
9455 {
9456 Powerpc_relobj<size, big_endian>* sym_object
9457 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9458 bool is_ordinary;
9459 unsigned int shndx = gsym->shndx(&is_ordinary);
9460 if (shndx == sym_object->toc_shndx())
9461 {
9462 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 9463 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
9464 if (dst_off < sym_object->section_size(shndx))
9465 {
9466 bool ok = false;
9467 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9468 ok = true;
9469 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9470 {
9471 // Need to check that the insn is a ld
9472 if (!view)
9473 view = ppc_object->section_contents(data_shndx,
9474 &slen,
9475 false);
9476 section_size_type off =
9477 (convert_to_section_size_type(reloc.get_r_offset())
9478 + (big_endian ? -2 : 3));
9479 if (off < slen
9480 && (view[off] & (0x3f << 2)) == (58u << 2))
9481 ok = true;
9482 }
9483 if (!ok)
9484 sym_object->set_no_toc_opt(dst_off);
9485 }
9486 }
9487 }
9488 break;
9489 }
9490 }
9491
f159cdb6
AM
9492 if (size == 32)
9493 {
9494 switch (r_type)
9495 {
9496 case elfcpp::R_PPC_LOCAL24PC:
9497 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9498 gold_error(_("%s: unsupported -mbss-plt code"),
9499 ppc_object->name().c_str());
9500 break;
9501 default:
9502 break;
9503 }
9504 }
9505
d8f5a274
AM
9506 switch (r_type)
9507 {
9508 case elfcpp::R_POWERPC_GOT_TLSLD16:
9509 case elfcpp::R_POWERPC_GOT_TLSGD16:
9510 case elfcpp::R_POWERPC_GOT_TPREL16:
9511 case elfcpp::R_POWERPC_GOT_DTPREL16:
9512 case elfcpp::R_POWERPC_GOT16:
9513 case elfcpp::R_PPC64_GOT16_DS:
9514 case elfcpp::R_PPC64_TOC16:
9515 case elfcpp::R_PPC64_TOC16_DS:
9516 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
9517 break;
9518 default:
9519 break;
9520 }
9521
9522 switch (r_type)
9523 {
89c52ae3
AM
9524 case elfcpp::R_PPC64_TPREL16_DS:
9525 case elfcpp::R_PPC64_TPREL16_LO_DS:
9526 case elfcpp::R_PPC64_TPREL16_HIGH:
9527 case elfcpp::R_PPC64_TPREL16_HIGHA:
9528 case elfcpp::R_PPC64_TPREL16_HIGHER:
9529 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9530 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9531 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9532 case elfcpp::R_PPC64_TPREL34:
252dcdf4
AM
9533 if (size != 64)
9534 break;
9535 // Fall through.
9536 case elfcpp::R_POWERPC_TPREL16:
9537 case elfcpp::R_POWERPC_TPREL16_LO:
9538 case elfcpp::R_POWERPC_TPREL16_HI:
9539 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
9540 layout->set_has_static_tls();
9541 break;
9542 default:
9543 break;
9544 }
9545
252dcdf4
AM
9546 switch (r_type)
9547 {
9548 case elfcpp::R_POWERPC_TPREL16_HA:
9549 if (target->tprel_opt())
9550 {
9551 section_size_type slen;
9552 const unsigned char* view = NULL;
9553 view = ppc_object->section_contents(data_shndx, &slen, false);
9554 section_size_type off
9555 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9556 if (off < slen)
9557 {
9558 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9559 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9560 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
4e0e019f 9561 target->set_no_tprel_opt();
252dcdf4
AM
9562 }
9563 }
9564 break;
9565
9566 case elfcpp::R_PPC64_TPREL16_HIGH:
9567 case elfcpp::R_PPC64_TPREL16_HIGHA:
9568 case elfcpp::R_PPC64_TPREL16_HIGHER:
9569 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9570 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9571 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9572 if (size != 64)
9573 break;
9574 // Fall through.
9575 case elfcpp::R_POWERPC_TPREL16_HI:
4e0e019f 9576 target->set_no_tprel_opt();
252dcdf4
AM
9577 break;
9578 default:
9579 break;
9580 }
9581
89c52ae3
AM
9582 switch (r_type)
9583 {
9584 case elfcpp::R_PPC64_D34:
9585 case elfcpp::R_PPC64_D34_LO:
9586 case elfcpp::R_PPC64_D34_HI30:
9587 case elfcpp::R_PPC64_D34_HA30:
9588 case elfcpp::R_PPC64_D28:
9589 case elfcpp::R_PPC64_PCREL34:
9590 case elfcpp::R_PPC64_PCREL28:
9591 case elfcpp::R_PPC64_TPREL34:
9592 case elfcpp::R_PPC64_DTPREL34:
9593 case elfcpp::R_PPC64_PLT_PCREL34:
9594 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9595 case elfcpp::R_PPC64_GOT_PCREL34:
87c69f97
AM
9596 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9597 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9598 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9599 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
63e5eea2 9600 target->set_power10_relocs();
89c52ae3 9601 break;
d8f5a274
AM
9602 default:
9603 break;
9604 }
42cacb20
DE
9605}
9606
6d03d481
ST
9607// Process relocations for gc.
9608
9609template<int size, bool big_endian>
9610void
9611Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
9612 Symbol_table* symtab,
9613 Layout* layout,
9614 Sized_relobj_file<size, big_endian>* object,
9615 unsigned int data_shndx,
9616 unsigned int,
9617 const unsigned char* prelocs,
9618 size_t reloc_count,
9619 Output_section* output_section,
9620 bool needs_special_offset_handling,
9621 size_t local_symbol_count,
9622 const unsigned char* plocal_symbols)
6d03d481
ST
9623{
9624 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9625 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9626 Classify_reloc;
9627
e81fea4d
AM
9628 Powerpc_relobj<size, big_endian>* ppc_object
9629 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9630 if (size == 64)
9631 ppc_object->set_opd_valid();
9632 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9633 {
9634 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9635 for (p = ppc_object->access_from_map()->begin();
9636 p != ppc_object->access_from_map()->end();
9637 ++p)
9638 {
9639 Address dst_off = p->first;
9640 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9641 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9642 for (s = p->second.begin(); s != p->second.end(); ++s)
9643 {
efc6fa12 9644 Relobj* src_obj = s->first;
e81fea4d
AM
9645 unsigned int src_indx = s->second;
9646 symtab->gc()->add_reference(src_obj, src_indx,
9647 ppc_object, dst_indx);
9648 }
9649 p->second.clear();
9650 }
9651 ppc_object->access_from_map()->clear();
c6de8ed4 9652 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
9653 // Don't look at .opd relocs as .opd will reference everything.
9654 return;
9655 }
6d03d481 9656
4d625b70 9657 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9658 symtab,
9659 layout,
9660 this,
9661 object,
9662 data_shndx,
9663 prelocs,
9664 reloc_count,
9665 output_section,
9666 needs_special_offset_handling,
9667 local_symbol_count,
9668 plocal_symbols);
9669}
9670
e81fea4d
AM
9671// Handle target specific gc actions when adding a gc reference from
9672// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9673// and DST_OFF. For powerpc64, this adds a referenc to the code
9674// section of a function descriptor.
9675
9676template<int size, bool big_endian>
9677void
9678Target_powerpc<size, big_endian>::do_gc_add_reference(
9679 Symbol_table* symtab,
efc6fa12 9680 Relobj* src_obj,
e81fea4d 9681 unsigned int src_shndx,
efc6fa12 9682 Relobj* dst_obj,
e81fea4d
AM
9683 unsigned int dst_shndx,
9684 Address dst_off) const
9685{
6c77229c
AM
9686 if (size != 64 || dst_obj->is_dynamic())
9687 return;
9688
e81fea4d
AM
9689 Powerpc_relobj<size, big_endian>* ppc_object
9690 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9691 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9692 {
9693 if (ppc_object->opd_valid())
9694 {
9695 dst_shndx = ppc_object->get_opd_ent(dst_off);
9696 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9697 }
9698 else
9699 {
9700 // If we haven't run scan_opd_relocs, we must delay
9701 // processing this function descriptor reference.
9702 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9703 }
9704 }
9705}
9706
9707// Add any special sections for this symbol to the gc work list.
9708// For powerpc64, this adds the code section of a function
9709// descriptor.
9710
9711template<int size, bool big_endian>
9712void
9713Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9714 Symbol_table* symtab,
9715 Symbol* sym) const
9716{
6a31512f 9717 if (size == 64 && sym->object()->pluginobj() == NULL)
e81fea4d
AM
9718 {
9719 Powerpc_relobj<size, big_endian>* ppc_object
9720 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9721 bool is_ordinary;
9722 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9723 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9724 {
9725 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9726 Address dst_off = gsym->value();
c6de8ed4
AM
9727 if (ppc_object->opd_valid())
9728 {
9729 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9730 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9731 dst_indx));
c6de8ed4
AM
9732 }
9733 else
9734 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9735 }
9736 }
9737}
9738
dc3714f3
AM
9739// For a symbol location in .opd, set LOC to the location of the
9740// function entry.
9741
9742template<int size, bool big_endian>
9743void
9744Target_powerpc<size, big_endian>::do_function_location(
9745 Symbol_location* loc) const
9746{
a2d7bf59 9747 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9748 {
9749 if (loc->object->is_dynamic())
9750 {
9751 Powerpc_dynobj<size, big_endian>* ppc_object
9752 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9753 if (loc->shndx == ppc_object->opd_shndx())
9754 {
9755 Address dest_off;
9756 Address off = loc->offset - ppc_object->opd_address();
9757 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9758 loc->offset = dest_off;
9759 }
9760 }
9761 else
9762 {
9763 const Powerpc_relobj<size, big_endian>* ppc_object
9764 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9765 if (loc->shndx == ppc_object->opd_shndx())
9766 {
9767 Address dest_off;
9768 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9769 loc->offset = dest_off;
9770 }
9771 }
9772 }
9773}
9774
bbec1a5d
AM
9775// FNOFFSET in section SHNDX in OBJECT is the start of a function
9776// compiled with -fsplit-stack. The function calls non-split-stack
9777// code. Change the function to ensure it has enough stack space to
9778// call some random function.
9779
9780template<int size, bool big_endian>
9781void
9782Target_powerpc<size, big_endian>::do_calls_non_split(
9783 Relobj* object,
9784 unsigned int shndx,
9785 section_offset_type fnoffset,
9786 section_size_type fnsize,
6e0813d3
CC
9787 const unsigned char* prelocs,
9788 size_t reloc_count,
bbec1a5d
AM
9789 unsigned char* view,
9790 section_size_type view_size,
9791 std::string* from,
9792 std::string* to) const
9793{
9794 // 32-bit not supported.
9795 if (size == 32)
9796 {
9797 // warn
9798 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9799 prelocs, reloc_count, view, view_size,
9800 from, to);
bbec1a5d
AM
9801 return;
9802 }
9803
9804 // The function always starts with
9805 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9806 // addis %r12,%r1,-allocate@ha
9807 // addi %r12,%r12,-allocate@l
9808 // cmpld %r12,%r0
9809 // but note that the addis or addi may be replaced with a nop
9810
9811 unsigned char *entry = view + fnoffset;
9812 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9813
9814 if ((insn & 0xffff0000) == addis_2_12)
9815 {
9816 /* Skip ELFv2 global entry code. */
9817 entry += 8;
9818 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9819 }
9820
9821 unsigned char *pinsn = entry;
9822 bool ok = false;
9823 const uint32_t ld_private_ss = 0xe80d8fc0;
9824 if (insn == ld_private_ss)
9825 {
9826 int32_t allocate = 0;
9827 while (1)
9828 {
9829 pinsn += 4;
9830 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9831 if ((insn & 0xffff0000) == addis_12_1)
9832 allocate += (insn & 0xffff) << 16;
9833 else if ((insn & 0xffff0000) == addi_12_1
9834 || (insn & 0xffff0000) == addi_12_12)
9835 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9836 else if (insn != nop)
9837 break;
9838 }
9839 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9840 {
9841 int extra = parameters->options().split_stack_adjust_size();
9842 allocate -= extra;
9843 if (allocate >= 0 || extra < 0)
9844 {
9845 object->error(_("split-stack stack size overflow at "
9846 "section %u offset %0zx"),
9847 shndx, static_cast<size_t>(fnoffset));
9848 return;
9849 }
9850 pinsn = entry + 4;
9851 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9852 if (insn != addis_12_1)
9853 {
9854 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9855 pinsn += 4;
9856 insn = addi_12_12 | (allocate & 0xffff);
9857 if (insn != addi_12_12)
9858 {
9859 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9860 pinsn += 4;
9861 }
9862 }
9863 else
9864 {
9865 insn = addi_12_1 | (allocate & 0xffff);
9866 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9867 pinsn += 4;
9868 }
9869 if (pinsn != entry + 12)
9870 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9871
9872 ok = true;
9873 }
9874 }
9875
9876 if (!ok)
9877 {
9878 if (!object->has_no_split_stack())
9879 object->error(_("failed to match split-stack sequence at "
9880 "section %u offset %0zx"),
9881 shndx, static_cast<size_t>(fnoffset));
9882 }
9883}
9884
42cacb20
DE
9885// Scan relocations for a section.
9886
9887template<int size, bool big_endian>
9888void
9889Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9890 Symbol_table* symtab,
9891 Layout* layout,
9892 Sized_relobj_file<size, big_endian>* object,
9893 unsigned int data_shndx,
9894 unsigned int sh_type,
9895 const unsigned char* prelocs,
9896 size_t reloc_count,
9897 Output_section* output_section,
9898 bool needs_special_offset_handling,
9899 size_t local_symbol_count,
9900 const unsigned char* plocal_symbols)
42cacb20
DE
9901{
9902 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9903 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9904 Classify_reloc;
42cacb20 9905
7ee7ff70
AM
9906 if (!this->plt_localentry0_init_)
9907 {
9908 bool plt_localentry0 = false;
9909 if (size == 64
9910 && this->abiversion() >= 2)
9911 {
9912 if (parameters->options().user_set_plt_localentry())
9913 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9914 if (plt_localentry0
9915 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9916 gold_warning(_("--plt-localentry is especially dangerous without "
9917 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9918 }
9919 this->plt_localentry0_ = plt_localentry0;
9920 this->plt_localentry0_init_ = true;
9921 }
9922
42cacb20
DE
9923 if (sh_type == elfcpp::SHT_REL)
9924 {
9925 gold_error(_("%s: unsupported REL reloc section"),
9926 object->name().c_str());
9927 return;
9928 }
9929
4d625b70 9930 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9931 symtab,
9932 layout,
9933 this,
9934 object,
9935 data_shndx,
9936 prelocs,
9937 reloc_count,
9938 output_section,
9939 needs_special_offset_handling,
9940 local_symbol_count,
9941 plocal_symbols);
63e5eea2
AM
9942
9943 if (this->plt_localentry0_ && this->power10_relocs_)
9944 {
9945 gold_warning(_("--plt-localentry is incompatible with "
9946 "power10 pc-relative code"));
9947 this->plt_localentry0_ = false;
9948 }
42cacb20
DE
9949}
9950
ec4dbad3
AM
9951// Functor class for processing the global symbol table.
9952// Removes symbols defined on discarded opd entries.
9953
9954template<bool big_endian>
9955class Global_symbol_visitor_opd
9956{
9957 public:
9958 Global_symbol_visitor_opd()
9959 { }
9960
9961 void
9962 operator()(Sized_symbol<64>* sym)
9963 {
9964 if (sym->has_symtab_index()
9965 || sym->source() != Symbol::FROM_OBJECT
9966 || !sym->in_real_elf())
9967 return;
9968
6c77229c
AM
9969 if (sym->object()->is_dynamic())
9970 return;
9971
ec4dbad3
AM
9972 Powerpc_relobj<64, big_endian>* symobj
9973 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9974 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9975 return;
9976
9977 bool is_ordinary;
9978 unsigned int shndx = sym->shndx(&is_ordinary);
9979 if (shndx == symobj->opd_shndx()
9980 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9981 {
9982 sym->set_undefined();
e3ee8ed4 9983 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9984 sym->set_is_defined_in_discarded_section();
9985 sym->set_symtab_index(-1U);
9986 }
ec4dbad3
AM
9987 }
9988};
9989
f3a0ed29
AM
9990template<int size, bool big_endian>
9991void
9992Target_powerpc<size, big_endian>::define_save_restore_funcs(
9993 Layout* layout,
9994 Symbol_table* symtab)
9995{
9996 if (size == 64)
9997 {
d49044c7
AM
9998 Output_data_save_res<size, big_endian>* savres
9999 = new Output_data_save_res<size, big_endian>(symtab);
10000 this->savres_section_ = savres;
f3a0ed29
AM
10001 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
10002 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
10003 savres, ORDER_TEXT, false);
10004 }
10005}
10006
d8f5a274
AM
10007// Sort linker created .got section first (for the header), then input
10008// sections belonging to files using small model code.
10009
10010template<bool big_endian>
10011class Sort_toc_sections
10012{
f19c3684
AM
10013 const Output_section_data*
10014 small_got_section() const
10015 {
10016 return (static_cast<Target_powerpc<64, big_endian>*>(
10017 parameters->sized_target<64, big_endian>())
10018 ->got_section(GOT_TYPE_SMALL));
10019 }
10020
10021 int
10022 rank(const Output_section::Input_section& isec) const
10023 {
10024 if (!isec.is_input_section())
10025 {
10026 if (isec.output_section_data() == this->small_got_section())
10027 return 0;
10028 return 2;
10029 }
10030 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
10031 ->has_small_toc_reloc())
10032 return 1;
10033 return 3;
10034 }
10035
d8f5a274
AM
10036 public:
10037 bool
10038 operator()(const Output_section::Input_section& is1,
10039 const Output_section::Input_section& is2) const
10040 {
f19c3684 10041 return rank(is1) < rank(is2);
d8f5a274
AM
10042 }
10043};
10044
42cacb20
DE
10045// Finalize the sections.
10046
10047template<int size, bool big_endian>
10048void
d5b40221
DK
10049Target_powerpc<size, big_endian>::do_finalize_sections(
10050 Layout* layout,
724436fc 10051 const Input_objects* input_objects,
ec4dbad3 10052 Symbol_table* symtab)
42cacb20 10053{
c9824451
AM
10054 if (parameters->doing_static_link())
10055 {
10056 // At least some versions of glibc elf-init.o have a strong
10057 // reference to __rela_iplt marker syms. A weak ref would be
10058 // better..
10059 if (this->iplt_ != NULL)
10060 {
10061 Reloc_section* rel = this->iplt_->rel_plt();
10062 symtab->define_in_output_data("__rela_iplt_start", NULL,
10063 Symbol_table::PREDEFINED, rel, 0, 0,
10064 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10065 elfcpp::STV_HIDDEN, 0, false, true);
10066 symtab->define_in_output_data("__rela_iplt_end", NULL,
10067 Symbol_table::PREDEFINED, rel, 0, 0,
10068 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10069 elfcpp::STV_HIDDEN, 0, true, true);
10070 }
10071 else
10072 {
10073 symtab->define_as_constant("__rela_iplt_start", NULL,
10074 Symbol_table::PREDEFINED, 0, 0,
10075 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10076 elfcpp::STV_HIDDEN, 0, true, false);
10077 symtab->define_as_constant("__rela_iplt_end", NULL,
10078 Symbol_table::PREDEFINED, 0, 0,
10079 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10080 elfcpp::STV_HIDDEN, 0, true, false);
10081 }
10082 }
10083
ec4dbad3
AM
10084 if (size == 64)
10085 {
10086 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10087 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
10088
10089 if (!parameters->options().relocatable())
10090 {
10091 this->define_save_restore_funcs(layout, symtab);
10092
10093 // Annoyingly, we need to make these sections now whether or
10094 // not we need them. If we delay until do_relax then we
10095 // need to mess with the relaxation machinery checkpointing.
f19c3684 10096 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
ec661b9d 10097 this->make_brlt_section(layout);
d8f5a274 10098
f19c3684
AM
10099 // FIXME, maybe. Here we could run through all the got
10100 // entries in the small got section, removing any duplicates
10101 // found in the big got section and renumbering offsets.
10102
d8f5a274
AM
10103 if (parameters->options().toc_sort())
10104 {
10105 Output_section* os = this->got_->output_section();
10106 if (os != NULL && os->input_sections().size() > 1)
10107 std::stable_sort(os->input_sections().begin(),
10108 os->input_sections().end(),
10109 Sort_toc_sections<big_endian>());
10110 }
ec661b9d 10111 }
ec4dbad3
AM
10112 }
10113
42cacb20 10114 // Fill in some more dynamic tags.
c9269dff 10115 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 10116 if (odyn != NULL)
cf43a2fe 10117 {
c9824451
AM
10118 const Reloc_section* rel_plt = (this->plt_ == NULL
10119 ? NULL
10120 : this->plt_->rel_plt());
10121 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
bdad2ad5 10122 this->rela_dyn_, true, size == 32, true);
c9824451
AM
10123
10124 if (size == 32)
dd93cd0a 10125 {
c9824451
AM
10126 if (this->got_ != NULL)
10127 {
10128 this->got_->finalize_data_size();
10129 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10130 this->got_, this->got_->g_o_t());
10131 }
34e0882b
AM
10132 if (this->has_tls_get_addr_opt_)
10133 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 10134 }
c9824451 10135 else
dd93cd0a 10136 {
c9824451
AM
10137 if (this->glink_ != NULL)
10138 {
10139 this->glink_->finalize_data_size();
10140 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10141 this->glink_,
9e390558 10142 (this->glink_->pltresolve_size()
c9824451
AM
10143 - 32));
10144 }
34e0882b 10145 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 10146 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
10147 ((this->has_localentry0_
10148 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10149 | (this->has_tls_get_addr_opt_
10150 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 10151 }
c9269dff 10152 }
cf43a2fe 10153
42cacb20
DE
10154 // Emit any relocs we saved in an attempt to avoid generating COPY
10155 // relocs.
10156 if (this->copy_relocs_.any_saved_relocs())
10157 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
10158
10159 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10160 p != input_objects->relobj_end();
10161 ++p)
10162 {
10163 Powerpc_relobj<size, big_endian>* ppc_relobj
10164 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10165 if (ppc_relobj->attributes_section_data())
6f3fe02b 10166 this->merge_object_attributes(ppc_relobj,
724436fc
AM
10167 ppc_relobj->attributes_section_data());
10168 }
10169 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10170 p != input_objects->dynobj_end();
10171 ++p)
10172 {
10173 Powerpc_dynobj<size, big_endian>* ppc_dynobj
10174 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10175 if (ppc_dynobj->attributes_section_data())
6f3fe02b 10176 this->merge_object_attributes(ppc_dynobj,
724436fc
AM
10177 ppc_dynobj->attributes_section_data());
10178 }
10179
10180 // Create a .gnu.attributes section if we have merged any attributes
10181 // from inputs.
10182 if (this->attributes_section_data_ != NULL
10183 && this->attributes_section_data_->size() != 0)
10184 {
10185 Output_attributes_section_data* attributes_section
10186 = new Output_attributes_section_data(*this->attributes_section_data_);
10187 layout->add_output_section_data(".gnu.attributes",
10188 elfcpp::SHT_GNU_ATTRIBUTES, 0,
10189 attributes_section, ORDER_INVALID, false);
10190 }
10191}
10192
bdad2ad5
AM
10193// Get the custom dynamic tag value.
10194
10195template<int size, bool big_endian>
10196unsigned int
10197Target_powerpc<size, big_endian>::do_dynamic_tag_custom_value(
10198 elfcpp::DT tag) const
10199{
10200 if (tag != elfcpp::DT_RELACOUNT)
10201 gold_unreachable();
10202 return this->rela_dyn_->relative_reloc_count();
10203}
10204
724436fc
AM
10205// Merge object attributes from input file called NAME with those of the
10206// output. The input object attributes are in the object pointed by PASD.
10207
10208template<int size, bool big_endian>
10209void
10210Target_powerpc<size, big_endian>::merge_object_attributes(
6f3fe02b 10211 const Object* obj,
724436fc
AM
10212 const Attributes_section_data* pasd)
10213{
10214 // Return if there is no attributes section data.
10215 if (pasd == NULL)
10216 return;
10217
10218 // Create output object attributes.
10219 if (this->attributes_section_data_ == NULL)
10220 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10221
10222 const int vendor = Object_attribute::OBJ_ATTR_GNU;
10223 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10224 Object_attribute* out_attr
10225 = this->attributes_section_data_->known_attributes(vendor);
10226
6f3fe02b 10227 const char* name = obj->name().c_str();
724436fc
AM
10228 const char* err;
10229 const char* first;
10230 const char* second;
10231 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10232 int in_fp = in_attr[tag].int_value() & 0xf;
10233 int out_fp = out_attr[tag].int_value() & 0xf;
6f3fe02b 10234 bool warn_only = obj->is_dynamic();
724436fc
AM
10235 if (in_fp != out_fp)
10236 {
10237 err = NULL;
10238 if ((in_fp & 3) == 0)
10239 ;
10240 else if ((out_fp & 3) == 0)
10241 {
6f3fe02b
AM
10242 if (!warn_only)
10243 {
10244 out_fp |= in_fp & 3;
10245 out_attr[tag].set_int_value(out_fp);
10246 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10247 this->last_fp_ = name;
10248 }
724436fc
AM
10249 }
10250 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10251 {
10252 err = N_("%s uses hard float, %s uses soft float");
10253 first = this->last_fp_;
10254 second = name;
10255 }
10256 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10257 {
10258 err = N_("%s uses hard float, %s uses soft float");
10259 first = name;
10260 second = this->last_fp_;
10261 }
10262 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10263 {
10264 err = N_("%s uses double-precision hard float, "
10265 "%s uses single-precision hard float");
10266 first = this->last_fp_;
10267 second = name;
10268 }
10269 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10270 {
10271 err = N_("%s uses double-precision hard float, "
10272 "%s uses single-precision hard float");
10273 first = name;
10274 second = this->last_fp_;
10275 }
10276
10277 if (err || (in_fp & 0xc) == 0)
10278 ;
10279 else if ((out_fp & 0xc) == 0)
10280 {
6f3fe02b
AM
10281 if (!warn_only)
10282 {
10283 out_fp |= in_fp & 0xc;
10284 out_attr[tag].set_int_value(out_fp);
10285 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10286 this->last_ld_ = name;
10287 }
724436fc
AM
10288 }
10289 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10290 {
10291 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10292 first = name;
10293 second = this->last_ld_;
10294 }
10295 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10296 {
10297 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10298 first = this->last_ld_;
10299 second = name;
10300 }
10301 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10302 {
10303 err = N_("%s uses IBM long double, %s uses IEEE long double");
10304 first = this->last_ld_;
10305 second = name;
10306 }
10307 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10308 {
10309 err = N_("%s uses IBM long double, %s uses IEEE long double");
10310 first = name;
10311 second = this->last_ld_;
10312 }
10313
10314 if (err)
10315 {
10316 if (parameters->options().warn_mismatch())
6f3fe02b
AM
10317 {
10318 if (warn_only)
10319 gold_warning(_(err), first, second);
10320 else
10321 gold_error(_(err), first, second);
10322 }
724436fc
AM
10323 // Arrange for this attribute to be deleted. It's better to
10324 // say "don't know" about a file than to wrongly claim compliance.
6f3fe02b
AM
10325 if (!warn_only)
10326 out_attr[tag].set_type(0);
724436fc
AM
10327 }
10328 }
10329
10330 if (size == 32)
10331 {
10332 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10333 int in_vec = in_attr[tag].int_value() & 3;
10334 int out_vec = out_attr[tag].int_value() & 3;
10335 if (in_vec != out_vec)
10336 {
10337 err = NULL;
10338 if (in_vec == 0)
10339 ;
10340 else if (out_vec == 0)
10341 {
10342 out_vec = in_vec;
10343 out_attr[tag].set_int_value(out_vec);
10344 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10345 this->last_vec_ = name;
10346 }
10347 // For now, allow generic to transition to AltiVec or SPE
10348 // without a warning. If GCC marked files with their stack
10349 // alignment and used don't-care markings for files which are
10350 // not affected by the vector ABI, we could warn about this
10351 // case too. */
10352 else if (in_vec == 1)
10353 ;
10354 else if (out_vec == 1)
10355 {
10356 out_vec = in_vec;
10357 out_attr[tag].set_int_value(out_vec);
10358 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10359 this->last_vec_ = name;
10360 }
10361 else if (out_vec < in_vec)
10362 {
10363 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10364 first = this->last_vec_;
10365 second = name;
10366 }
10367 else if (out_vec > in_vec)
10368 {
10369 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10370 first = name;
10371 second = this->last_vec_;
10372 }
10373 if (err)
10374 {
10375 if (parameters->options().warn_mismatch())
10376 gold_error(_(err), first, second);
10377 out_attr[tag].set_type(0);
10378 }
10379 }
10380
10381 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10382 int in_struct = in_attr[tag].int_value() & 3;
10383 int out_struct = out_attr[tag].int_value() & 3;
10384 if (in_struct != out_struct)
10385 {
10386 err = NULL;
10387 if (in_struct == 0 || in_struct == 3)
10388 ;
10389 else if (out_struct == 0)
10390 {
10391 out_struct = in_struct;
10392 out_attr[tag].set_int_value(out_struct);
10393 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10394 this->last_struct_ = name;
10395 }
10396 else if (out_struct < in_struct)
10397 {
10398 err = N_("%s uses r3/r4 for small structure returns, "
10399 "%s uses memory");
10400 first = this->last_struct_;
10401 second = name;
10402 }
10403 else if (out_struct > in_struct)
10404 {
10405 err = N_("%s uses r3/r4 for small structure returns, "
10406 "%s uses memory");
10407 first = name;
10408 second = this->last_struct_;
10409 }
10410 if (err)
10411 {
10412 if (parameters->options().warn_mismatch())
10413 gold_error(_(err), first, second);
10414 out_attr[tag].set_type(0);
10415 }
10416 }
10417 }
10418
10419 // Merge Tag_compatibility attributes and any common GNU ones.
10420 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
10421}
10422
5edad15d
AM
10423// Emit any saved relocs, and mark toc entries using any of these
10424// relocs as not optimizable.
aba6bc71 10425
5edad15d
AM
10426template<int sh_type, int size, bool big_endian>
10427void
10428Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10429 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 10430{
5edad15d
AM
10431 if (size == 64
10432 && parameters->options().toc_optimize())
10433 {
10434 for (typename Copy_relocs<sh_type, size, big_endian>::
10435 Copy_reloc_entries::iterator p = this->entries_.begin();
10436 p != this->entries_.end();
10437 ++p)
10438 {
10439 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10440 entry = *p;
10441
10442 // If the symbol is no longer defined in a dynamic object,
10443 // then we emitted a COPY relocation. If it is still
10444 // dynamic then we'll need dynamic relocations and thus
10445 // can't optimize toc entries.
10446 if (entry.sym_->is_from_dynobj())
10447 {
10448 Powerpc_relobj<size, big_endian>* ppc_object
10449 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10450 if (entry.shndx_ == ppc_object->toc_shndx())
10451 ppc_object->set_no_toc_opt(entry.address_);
10452 }
10453 }
10454 }
10455
10456 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
10457}
10458
3ea0a085
AM
10459// Return the value to use for a branch relocation.
10460
10461template<int size, bool big_endian>
1611bc4a 10462bool
3ea0a085 10463Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 10464 const Symbol_table* symtab,
3ea0a085
AM
10465 const Sized_symbol<size>* gsym,
10466 Powerpc_relobj<size, big_endian>* object,
1611bc4a 10467 Address *value,
3ea0a085
AM
10468 unsigned int *dest_shndx)
10469{
9055360d
AM
10470 if (size == 32 || this->abiversion() >= 2)
10471 gold_unreachable();
3ea0a085 10472 *dest_shndx = 0;
3ea0a085
AM
10473
10474 // If the symbol is defined in an opd section, ie. is a function
10475 // descriptor, use the function descriptor code entry address
10476 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 10477 if (gsym != NULL
0e123f69
AM
10478 && (gsym->source() != Symbol::FROM_OBJECT
10479 || gsym->object()->is_dynamic()))
1611bc4a 10480 return true;
3ea0a085
AM
10481 if (gsym != NULL)
10482 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10483 unsigned int shndx = symobj->opd_shndx();
10484 if (shndx == 0)
1611bc4a 10485 return true;
3ea0a085 10486 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 10487 if (opd_addr == invalid_address)
1611bc4a 10488 return true;
c6905c28 10489 opd_addr += symobj->output_section_address(shndx);
1611bc4a 10490 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
10491 {
10492 Address sec_off;
1611bc4a 10493 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
10494 if (symtab->is_section_folded(symobj, *dest_shndx))
10495 {
10496 Section_id folded
10497 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10498 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10499 *dest_shndx = folded.second;
10500 }
3ea0a085 10501 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
10502 if (sec_addr == invalid_address)
10503 return false;
10504
3ea0a085 10505 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 10506 *value = sec_addr + sec_off;
3ea0a085 10507 }
1611bc4a 10508 return true;
3ea0a085
AM
10509}
10510
c9b8abb7
AM
10511template<int size>
10512static bool
10513relative_value_is_known(const Sized_symbol<size>* gsym)
10514{
10515 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10516 return false;
10517
10518 if (gsym->is_from_dynobj()
10519 || gsym->is_undefined()
10520 || gsym->is_preemptible())
10521 return false;
10522
10523 if (gsym->is_absolute())
10524 return !parameters->options().output_is_position_independent();
10525
10526 return true;
10527}
10528
10529template<int size>
10530static bool
10531relative_value_is_known(const Symbol_value<size>* psymval)
10532{
10533 if (psymval->is_ifunc_symbol())
10534 return false;
10535
10536 bool is_ordinary;
10537 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10538
10539 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10540}
10541
0c951c25
AM
10542// PCREL_OPT in one instance flags to the linker that a pair of insns:
10543// pld ra,symbol@got@pcrel
10544// load/store rt,0(ra)
10545// or
10546// pla ra,symbol@pcrel
10547// load/store rt,0(ra)
10548// may be translated to
10549// pload/pstore rt,symbol@pcrel
10550// nop.
10551// This function returns true if the optimization is possible, placing
10552// the prefix insn in *PINSN1 and a NOP in *PINSN2.
10553//
10554// On entry to this function, the linker has already determined that
10555// the pld can be replaced with pla: *PINSN1 is that pla insn,
10556// while *PINSN2 is the second instruction.
10557
10558inline bool
10559xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10560{
10561 uint32_t insn2 = *pinsn2 >> 32;
10562 uint64_t i1new;
10563
10564 // Check that regs match.
10565 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10566 return false;
10567
10568 switch ((insn2 >> 26) & 63)
10569 {
10570 default:
10571 return false;
10572
10573 case 32: // lwz
10574 case 34: // lbz
10575 case 36: // stw
10576 case 38: // stb
10577 case 40: // lhz
10578 case 42: // lha
10579 case 44: // sth
10580 case 48: // lfs
10581 case 50: // lfd
10582 case 52: // stfs
10583 case 54: // stfd
10584 // These are the PMLS cases, where we just need to tack a prefix
10585 // on the insn. Check that the D field is zero.
10586 if ((insn2 & 0xffff) != 0)
10587 return false;
10588 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10589 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10590 break;
10591
10592 case 58: // lwa, ld
10593 if ((insn2 & 0xfffd) != 0)
10594 return false;
10595 i1new = ((1ULL << 58) | (1ULL << 52)
10596 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10597 | (insn2 & (31ULL << 21)));
10598 break;
10599
10600 case 57: // lxsd, lxssp
10601 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10602 return false;
10603 i1new = ((1ULL << 58) | (1ULL << 52)
10604 | ((40ULL | (insn2 & 3)) << 26)
10605 | (insn2 & (31ULL << 21)));
10606 break;
10607
10608 case 61: // stxsd, stxssp, lxv, stxv
10609 if ((insn2 & 3) == 0)
10610 return false;
10611 else if ((insn2 & 3) >= 2)
10612 {
10613 if ((insn2 & 0xfffc) != 0)
10614 return false;
10615 i1new = ((1ULL << 58) | (1ULL << 52)
10616 | ((44ULL | (insn2 & 3)) << 26)
10617 | (insn2 & (31ULL << 21)));
10618 }
10619 else
10620 {
10621 if ((insn2 & 0xfff0) != 0)
10622 return false;
10623 i1new = ((1ULL << 58) | (1ULL << 52)
10624 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10625 | (insn2 & (31ULL << 21)));
10626 }
10627 break;
10628
10629 case 56: // lq
10630 if ((insn2 & 0xffff) != 0)
10631 return false;
10632 i1new = ((1ULL << 58) | (1ULL << 52)
10633 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10634 break;
10635
10636 case 62: // std, stq
10637 if ((insn2 & 0xfffd) != 0)
10638 return false;
10639 i1new = ((1ULL << 58) | (1ULL << 52)
10640 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10641 | (insn2 & (31ULL << 21)));
10642 break;
10643 }
10644
10645 *pinsn1 = i1new;
10646 *pinsn2 = (uint64_t) nop << 32;
10647 return true;
10648}
10649
42cacb20
DE
10650// Perform a relocation.
10651
10652template<int size, bool big_endian>
10653inline bool
10654Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 10655 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 10656 unsigned int,
d83ce4e3
AM
10657 Target_powerpc* target,
10658 Output_section* os,
10659 size_t relnum,
91a65d2f 10660 const unsigned char* preloc,
d83ce4e3
AM
10661 const Sized_symbol<size>* gsym,
10662 const Symbol_value<size>* psymval,
10663 unsigned char* view,
c9269dff
AM
10664 Address address,
10665 section_size_type view_size)
42cacb20 10666{
23cedd1d
AM
10667 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10668 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10669 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10670
0e804863
ILT
10671 if (view == NULL)
10672 return true;
10673
34e0882b
AM
10674 if (target->replace_tls_get_addr(gsym))
10675 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10676
91a65d2f
AM
10677 const elfcpp::Rela<size, big_endian> rela(preloc);
10678 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
0af4fcc2
AM
10679 Powerpc_relobj<size, big_endian>* const object
10680 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
34e0882b 10681 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 10682 {
e3deeb9c 10683 case Track_tls::NOT_EXPECTED:
c7fdac09
AM
10684 // No warning. This will result in really old code without tls
10685 // marker relocs being mis-optimised, but there shouldn't be too
10686 // much of that code around. The problem with warning is that
10687 // glibc and libphobos both construct direct calls to
10688 // __tls_get_addr in a way that is harmless.
e3deeb9c
AM
10689 break;
10690 case Track_tls::EXPECTED:
10691 // We have already complained.
10692 break;
10693 case Track_tls::SKIP:
23cedd1d 10694 if (is_plt16_reloc<size>(r_type)
32f59844
AM
10695 || r_type == elfcpp::R_POWERPC_PLTSEQ
10696 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
10697 {
10698 Insn* iview = reinterpret_cast<Insn*>(view);
10699 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10700 }
10701 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10702 {
10703 Insn* iview = reinterpret_cast<Insn*>(view);
10704 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10705 }
e4dff765
AM
10706 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10707 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10708 {
10709 Insn* iview = reinterpret_cast<Insn*>(view);
10710 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10711 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10712 }
e3deeb9c
AM
10713 return true;
10714 case Track_tls::NORMAL:
10715 break;
dd93cd0a 10716 }
dd93cd0a 10717
dcfc7dd4
AM
10718 // Offset from start of insn to d-field reloc.
10719 const int d_offset = big_endian ? 2 : 0;
10720
dd93cd0a 10721 Address value = 0;
0cfb0717 10722 bool has_stub_value = false;
7ee7ff70 10723 bool localentry0 = false;
e5d5f5ed 10724 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6158b25f
AM
10725 bool pltcall_to_direct = false;
10726
f21dbd7c
AM
10727 if (is_plt16_reloc<size>(r_type)
10728 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10729 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10730 || r_type == elfcpp::R_POWERPC_PLTSEQ
10731 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10732 || r_type == elfcpp::R_POWERPC_PLTCALL
10733 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10734 {
10735 // It would be possible to replace inline plt calls with direct
10736 // calls if the PLTCALL is in range. The only difficulty is
10737 // that the decision depends on the PLTCALL reloc, and we don't
10738 // know the address of that instruction when processing others
10739 // in the sequence. So the decision needs to be made in
6158b25f
AM
10740 // do_relax().
10741 pltcall_to_direct = !(gsym != NULL
10742 ? gsym->has_plt_offset()
10743 : object->local_has_plt_offset(r_sym));
10744 }
10745 else if ((gsym != NULL
10746 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10747 : psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
10748 && !is_got_reloc(r_type)
10749 && (!psymval->is_ifunc_symbol()
10750 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type,
10751 false)))
dd93cd0a 10752 {
9055360d
AM
10753 if (size == 64
10754 && gsym != NULL
10755 && target->abiversion() >= 2
10756 && !parameters->options().output_is_position_independent()
32f59844 10757 && !is_branch_reloc<size>(r_type))
ec661b9d 10758 {
faa2211d
AM
10759 Address off = target->glink_section()->find_global_entry(gsym);
10760 if (off != invalid_address)
6ec65f28
AM
10761 {
10762 value = target->glink_section()->global_entry_address() + off;
10763 has_stub_value = true;
10764 }
ec661b9d 10765 }
c9824451 10766 else
9055360d 10767 {
64b5d6d7
AM
10768 Stub_table<size, big_endian>* stub_table = NULL;
10769 if (target->stub_tables().size() == 1)
10770 stub_table = target->stub_tables()[0];
10771 if (stub_table == NULL
10772 && !(size == 32
10773 && gsym != NULL
10774 && !parameters->options().output_is_position_independent()
32f59844 10775 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10776 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10777 if (stub_table == NULL)
10778 {
64b5d6d7
AM
10779 // This is a ref from a data section to an ifunc symbol,
10780 // or a non-branch reloc for which we always want to use
10781 // one set of stubs for resolving function addresses.
9055360d
AM
10782 if (target->stub_tables().size() != 0)
10783 stub_table = target->stub_tables()[0];
10784 }
faa2211d
AM
10785 if (stub_table != NULL)
10786 {
7e57d19e 10787 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10788 if (gsym != NULL)
7e57d19e 10789 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10790 rela.get_r_addend());
10791 else
7e57d19e 10792 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10793 rela.get_r_addend());
7e57d19e 10794 if (ent != NULL)
faa2211d 10795 {
7e57d19e
AM
10796 value = stub_table->stub_address() + ent->off_;
10797 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10798 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10799 size_t reloc_count = shdr.get_sh_size() / reloc_size;
eb97d024
AM
10800 if (size == 64)
10801 {
10802 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10803 {
10804 if (!ent->notoc_)
10805 value += ent->p9off_;
10806 }
10807 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10808 value += ent->p9off_;
10809 else
10810 value += ent->tocoff_;
10811 }
7e57d19e
AM
10812 if (size == 64
10813 && ent->r2save_
a993d270
AM
10814 && !(gsym != NULL
10815 && target->is_tls_get_addr_opt(gsym)))
afd2ea23 10816 {
eb97d024
AM
10817 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10818 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
a993d270
AM
10819 {
10820 if (!(target->power10_stubs()
10821 && target->power10_stubs_auto()))
10822 value += 4;
10823 }
10824 else if (relnum < reloc_count - 1)
10825 {
10826 Reltype next_rela(preloc + reloc_size);
10827 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10828 == elfcpp::R_PPC64_TOCSAVE
10829 && (next_rela.get_r_offset()
10830 == rela.get_r_offset() + 4))
10831 value += 4;
10832 }
7e57d19e 10833 }
7ee7ff70 10834 localentry0 = ent->localentry0_;
faa2211d
AM
10835 has_stub_value = true;
10836 }
10837 }
9055360d 10838 }
faa2211d
AM
10839 // We don't care too much about bogus debug references to
10840 // non-local functions, but otherwise there had better be a plt
10841 // call stub or global entry stub as appropriate.
10842 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10843 }
cf43a2fe 10844
6158b25f
AM
10845 if (!pltcall_to_direct && (is_plt16_reloc<size>(r_type)
10846 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10847 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10848 {
10849 const Output_data_plt_powerpc<size, big_endian>* plt;
10850 if (gsym)
10851 value = target->plt_off(gsym, &plt);
10852 else
10853 value = target->plt_off(object, r_sym, &plt);
10854 value += plt->address();
10855
10856 if (size == 64)
e4dff765
AM
10857 {
10858 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10859 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
a19da04b 10860 value -= target->toc_pointer();
e4dff765 10861 }
08be3224
AM
10862 else if (parameters->options().output_is_position_independent())
10863 {
10864 if (rela.get_r_addend() >= 32768)
10865 {
10866 unsigned int got2 = object->got2_shndx();
10867 value -= (object->get_output_section_offset(got2)
10868 + object->output_section(got2)->address()
10869 + rela.get_r_addend());
10870 }
10871 else
a19da04b 10872 value -= target->toc_pointer();
08be3224
AM
10873 }
10874 }
6158b25f 10875 else if (pltcall_to_direct
23cedd1d 10876 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10877 || r_type == elfcpp::R_POWERPC_PLTSEQ
10878 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10879 {
10880 Insn* iview = reinterpret_cast<Insn*>(view);
10881 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10882 r_type = elfcpp::R_POWERPC_NONE;
10883 }
6158b25f 10884 else if (pltcall_to_direct
e4dff765
AM
10885 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10886 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10887 {
10888 Insn* iview = reinterpret_cast<Insn*>(view);
10889 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10890 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10891 r_type = elfcpp::R_POWERPC_NONE;
10892 }
4290b0ab 10893 else if (is_got_reloc(r_type))
42cacb20 10894 {
5cdb4f14 10895 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
10896 Got_type got_type = ((size == 32
10897 || r_type == elfcpp::R_POWERPC_GOT16
10898 || r_type == elfcpp::R_PPC64_GOT16_DS)
10899 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
cf43a2fe 10900 if (gsym != NULL)
f19c3684 10901 value = gsym->got_offset(got_type, addend);
cf43a2fe 10902 else
f19c3684 10903 value = object->local_got_offset(r_sym, got_type, addend);
e4dff765 10904 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
f19c3684 10905 value += target->got_section(got_type)->address();
e4dff765 10906 else
f19c3684 10907 value -= target->got_base_offset(got_type);
cf43a2fe
AM
10908 }
10909 else if (r_type == elfcpp::R_PPC64_TOC)
10910 {
a19da04b 10911 value = target->toc_pointer();
cf43a2fe
AM
10912 }
10913 else if (gsym != NULL
10914 && (r_type == elfcpp::R_POWERPC_REL24
10915 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10916 && has_stub_value)
cf43a2fe 10917 {
c9269dff
AM
10918 if (size == 64)
10919 {
10920 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10921 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10922 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10923 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10924 {
3ea0a085 10925 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10926 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10927 if ((insn & 1) != 0
10928 && (insn2 == nop
10929 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10930 {
b4f7960d
AM
10931 elfcpp::Swap<32, big_endian>::
10932 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10933 can_plt_call = true;
10934 }
10935 }
10936 if (!can_plt_call)
3ea0a085
AM
10937 {
10938 // If we don't have a branch and link followed by a nop,
10939 // we can't go via the plt because there is no place to
10940 // put a toc restoring instruction.
10941 // Unless we know we won't be returning.
10942 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10943 can_plt_call = true;
10944 }
10945 if (!can_plt_call)
10946 {
ba8ca3e7
AM
10947 // g++ as of 20130507 emits self-calls without a
10948 // following nop. This is arguably wrong since we have
10949 // conflicting information. On the one hand a global
10950 // symbol and on the other a local call sequence, but
10951 // don't error for this special case.
10952 // It isn't possible to cheaply verify we have exactly
10953 // such a call. Allow all calls to the same section.
3ea0a085 10954 bool ok = false;
c9824451 10955 Address code = value;
3ea0a085
AM
10956 if (gsym->source() == Symbol::FROM_OBJECT
10957 && gsym->object() == object)
10958 {
9055360d
AM
10959 unsigned int dest_shndx = 0;
10960 if (target->abiversion() < 2)
10961 {
10962 Address addend = rela.get_r_addend();
1611bc4a
AM
10963 code = psymval->value(object, addend);
10964 target->symval_for_branch(relinfo->symtab, gsym, object,
10965 &code, &dest_shndx);
9055360d 10966 }
3ea0a085
AM
10967 bool is_ordinary;
10968 if (dest_shndx == 0)
10969 dest_shndx = gsym->shndx(&is_ordinary);
10970 ok = dest_shndx == relinfo->data_shndx;
10971 }
10972 if (!ok)
c9824451
AM
10973 {
10974 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10975 _("call lacks nop, can't restore toc; "
10976 "recompile with -fPIC"));
10977 value = code;
10978 }
3ea0a085 10979 }
c9269dff 10980 }
cf43a2fe 10981 }
dd93cd0a
AM
10982 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10983 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10984 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3 10985 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
87c69f97 10986 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a
AM
10987 {
10988 // First instruction of a global dynamic sequence, arg setup insn.
c7fdac09
AM
10989 bool final = gsym == NULL || gsym->final_value_is_known();
10990 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
f19c3684
AM
10991 Got_type got_type = ((size == 32
10992 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10993 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
dd93cd0a 10994 if (tls_type == tls::TLSOPT_NONE)
f19c3684 10995 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
dd93cd0a 10996 else if (tls_type == tls::TLSOPT_TO_IE)
f19c3684
AM
10997 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10998 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
dd93cd0a 10999 {
5cdb4f14 11000 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
dd93cd0a 11001 if (gsym != NULL)
5cdb4f14 11002 value = gsym->got_offset(got_type, addend);
dd93cd0a 11003 else
5cdb4f14 11004 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 11005 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
f19c3684 11006 value += target->got_section(got_type)->address();
89c52ae3 11007 else
f19c3684 11008 value -= target->got_base_offset(got_type);
dd93cd0a
AM
11009 }
11010 if (tls_type == tls::TLSOPT_TO_IE)
11011 {
87c69f97 11012 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 11013 {
89c52ae3
AM
11014 Insn* iview = reinterpret_cast<Insn*>(view);
11015 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11016 pinsn <<= 32;
11017 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11018 // pla -> pld
11019 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
11020 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11021 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11022 pinsn & 0xffffffff);
87c69f97 11023 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
89c52ae3
AM
11024 }
11025 else
11026 {
11027 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11028 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11029 {
11030 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11031 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11032 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
11033 if (size == 32)
11034 insn |= 32 << 26; // lwz
11035 else
11036 insn |= 58 << 26; // ld
11037 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11038 }
11039 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11040 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 11041 }
dd93cd0a
AM
11042 }
11043 else if (tls_type == tls::TLSOPT_TO_LE)
11044 {
87c69f97 11045 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
dd93cd0a 11046 {
89c52ae3
AM
11047 Insn* iview = reinterpret_cast<Insn*>(view);
11048 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11049 pinsn <<= 32;
11050 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11051 // pla pcrel -> paddi r13
11052 pinsn += (-1ULL << 52) + (13ULL << 16);
11053 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11054 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11055 pinsn & 0xffffffff);
11056 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
11057 value = psymval->value(object, rela.get_r_addend());
11058 }
11059 else
11060 {
89c52ae3
AM
11061 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11062 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11063 {
11064 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11065 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11066 insn &= (1 << 26) - (1 << 21); // extract rt
11067 if (size == 32)
11068 insn |= addis_0_2;
11069 else
11070 insn |= addis_0_13;
11071 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11072 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11073 value = psymval->value(object, rela.get_r_addend());
11074 }
11075 else
11076 {
11077 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11078 Insn insn = nop;
11079 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11080 r_type = elfcpp::R_POWERPC_NONE;
11081 }
dd93cd0a
AM
11082 }
11083 }
11084 }
11085 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11086 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11087 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3 11088 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
87c69f97 11089 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
dd93cd0a
AM
11090 {
11091 // First instruction of a local dynamic sequence, arg setup insn.
c7fdac09 11092 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
11093 if (tls_type == tls::TLSOPT_NONE)
11094 {
11095 value = target->tlsld_got_offset();
87c69f97 11096 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
f19c3684 11097 value += target->got_section(GOT_TYPE_SMALL)->address();
89c52ae3 11098 else
f19c3684 11099 value -= target->got_base_offset(GOT_TYPE_SMALL);
dd93cd0a
AM
11100 }
11101 else
11102 {
11103 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 11104 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
89c52ae3
AM
11105 {
11106 Insn* iview = reinterpret_cast<Insn*>(view);
11107 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11108 pinsn <<= 32;
11109 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11110 // pla pcrel -> paddi r13
11111 pinsn += (-1ULL << 52) + (13ULL << 16);
11112 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11113 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11114 pinsn & 0xffffffff);
11115 r_type = elfcpp::R_PPC64_TPREL34;
11116 value = dtp_offset;
11117 }
11118 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11119 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 11120 {
dcfc7dd4 11121 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
11122 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11123 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 11124 if (size == 32)
0f81d3f0
AM
11125 insn |= addis_0_2;
11126 else
11127 insn |= addis_0_13;
dd93cd0a
AM
11128 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11129 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 11130 value = dtp_offset;
dd93cd0a
AM
11131 }
11132 else
11133 {
dcfc7dd4 11134 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11135 Insn insn = nop;
11136 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11137 r_type = elfcpp::R_POWERPC_NONE;
11138 }
11139 }
11140 }
11141 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11142 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11143 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3 11144 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
87c69f97 11145 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
dd93cd0a
AM
11146 {
11147 // Accesses relative to a local dynamic sequence address,
11148 // no optimisation here.
5cdb4f14 11149 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
11150 Got_type got_type = ((size == 32
11151 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11152 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
dd93cd0a 11153 if (gsym != NULL)
f19c3684 11154 value = gsym->got_offset(got_type, addend);
dd93cd0a 11155 else
f19c3684 11156 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 11157 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
f19c3684 11158 value += target->got_section(got_type)->address();
89c52ae3 11159 else
f19c3684 11160 value -= target->got_base_offset(got_type);
dd93cd0a
AM
11161 }
11162 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11163 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11164 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3 11165 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
87c69f97 11166 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
dd93cd0a
AM
11167 {
11168 // First instruction of initial exec sequence.
0af4fcc2
AM
11169 bool final = gsym == NULL || gsym->final_value_is_known();
11170 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
11171 if (tls_type == tls::TLSOPT_NONE)
11172 {
5cdb4f14 11173 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
f19c3684
AM
11174 Got_type got_type = ((size == 32
11175 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11176 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
dd93cd0a 11177 if (gsym != NULL)
f19c3684 11178 value = gsym->got_offset(got_type, addend);
dd93cd0a 11179 else
f19c3684 11180 value = object->local_got_offset(r_sym, got_type, addend);
87c69f97 11181 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
f19c3684 11182 value += target->got_section(got_type)->address();
89c52ae3 11183 else
f19c3684 11184 value -= target->got_base_offset(got_type);
dd93cd0a
AM
11185 }
11186 else
11187 {
11188 gold_assert(tls_type == tls::TLSOPT_TO_LE);
87c69f97 11189 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
89c52ae3
AM
11190 {
11191 Insn* iview = reinterpret_cast<Insn*>(view);
11192 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11193 pinsn <<= 32;
11194 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11195 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11196 pinsn += ((2ULL << 56) + (-1ULL << 52)
11197 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11198 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11199 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11200 pinsn & 0xffffffff);
11201 r_type = elfcpp::R_PPC64_TPREL34;
11202 value = psymval->value(object, rela.get_r_addend());
11203 }
11204 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11205 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 11206 {
dcfc7dd4 11207 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11208 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11209 insn &= (1 << 26) - (1 << 21); // extract rt from ld
11210 if (size == 32)
11211 insn |= addis_0_2;
11212 else
11213 insn |= addis_0_13;
11214 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11215 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11216 value = psymval->value(object, rela.get_r_addend());
11217 }
11218 else
11219 {
dcfc7dd4 11220 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
11221 Insn insn = nop;
11222 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11223 r_type = elfcpp::R_POWERPC_NONE;
11224 }
11225 }
11226 }
11227 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11228 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11229 {
11230 // Second instruction of a global dynamic sequence,
11231 // the __tls_get_addr call
e3deeb9c 11232 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
c7fdac09
AM
11233 bool final = gsym == NULL || gsym->final_value_is_known();
11234 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
dd93cd0a
AM
11235 if (tls_type != tls::TLSOPT_NONE)
11236 {
11237 if (tls_type == tls::TLSOPT_TO_IE)
11238 {
11239 Insn* iview = reinterpret_cast<Insn*>(view);
11240 Insn insn = add_3_3_13;
11241 if (size == 32)
11242 insn = add_3_3_2;
11243 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11244 r_type = elfcpp::R_POWERPC_NONE;
11245 }
11246 else
11247 {
89c52ae3
AM
11248 bool is_pcrel = false;
11249 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11250 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11251 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11252 if (relnum < reloc_count - 1)
11253 {
11254 Reltype next_rela(preloc + reloc_size);
11255 unsigned int r_type2
11256 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11257 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
eb97d024 11258 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
89c52ae3
AM
11259 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11260 && next_rela.get_r_offset() == rela.get_r_offset())
11261 is_pcrel = true;
11262 }
dd93cd0a 11263 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
11264 if (is_pcrel)
11265 {
11266 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11267 r_type = elfcpp::R_POWERPC_NONE;
11268 }
11269 else
11270 {
11271 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11272 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11273 view += d_offset;
11274 value = psymval->value(object, rela.get_r_addend());
11275 }
dd93cd0a 11276 }
e3deeb9c 11277 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
11278 }
11279 }
11280 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11281 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11282 {
11283 // Second instruction of a local dynamic sequence,
11284 // the __tls_get_addr call
e3deeb9c 11285 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
c7fdac09 11286 tls::Tls_optimization tls_type = target->optimize_tls_ld();
dd93cd0a
AM
11287 if (tls_type == tls::TLSOPT_TO_LE)
11288 {
89c52ae3
AM
11289 bool is_pcrel = false;
11290 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11291 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11292 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11293 if (relnum < reloc_count - 1)
11294 {
11295 Reltype next_rela(preloc + reloc_size);
11296 unsigned int r_type2
11297 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11298 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
eb97d024 11299 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
89c52ae3
AM
11300 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11301 && next_rela.get_r_offset() == rela.get_r_offset())
11302 is_pcrel = true;
11303 }
dd93cd0a 11304 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
11305 if (is_pcrel)
11306 {
11307 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11308 r_type = elfcpp::R_POWERPC_NONE;
11309 }
11310 else
11311 {
11312 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11313 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11314 view += d_offset;
11315 value = dtp_offset;
11316 }
e3deeb9c 11317 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
11318 }
11319 }
11320 else if (r_type == elfcpp::R_POWERPC_TLS)
11321 {
11322 // Second instruction of an initial exec sequence
0af4fcc2
AM
11323 bool final = gsym == NULL || gsym->final_value_is_known();
11324 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
dd93cd0a
AM
11325 if (tls_type == tls::TLSOPT_TO_LE)
11326 {
89c52ae3
AM
11327 Address roff = rela.get_r_offset() & 3;
11328 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
11329 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11330 unsigned int reg = size == 32 ? 2 : 13;
11331 insn = at_tls_transform(insn, reg);
11332 gold_assert(insn != 0);
89c52ae3
AM
11333 if (roff == 0)
11334 {
11335 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11336 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11337 view += d_offset;
11338 value = psymval->value(object, rela.get_r_addend());
11339 }
11340 else if (roff == 1)
11341 {
11342 // For pcrel IE to LE we already have the full offset
11343 // and thus don't need an addi here. A nop or mr will do.
11344 if ((insn & (0x3f << 26)) == 14 << 26)
11345 {
11346 // Extract regs from addi rt,ra,si.
11347 unsigned int rt = (insn >> 21) & 0x1f;
11348 unsigned int ra = (insn >> 16) & 0x1f;
11349 if (rt == ra)
11350 insn = nop;
11351 else
11352 {
11353 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11354 insn = (rt << 16) | (ra << 21) | (ra << 11);
11355 insn |= (31u << 26) | (444u << 1);
11356 }
11357 }
11358 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11359 r_type = elfcpp::R_POWERPC_NONE;
11360 }
dd93cd0a
AM
11361 }
11362 }
0cfb0717 11363 else if (!has_stub_value)
cf43a2fe 11364 {
6158b25f
AM
11365 if (pltcall_to_direct && (r_type == elfcpp::R_POWERPC_PLTCALL
11366 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
11367 {
11368 // PLTCALL without plt entry => convert to direct call
11369 Insn* iview = reinterpret_cast<Insn*>(view);
11370 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11371 insn = (insn & 1) | b;
11372 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11373 if (size == 32)
11374 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
11375 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11376 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
11377 else
11378 r_type = elfcpp::R_POWERPC_REL24;
11379 }
dd93cd0a 11380 Address addend = 0;
08be3224
AM
11381 if (!(size == 32
11382 && (r_type == elfcpp::R_PPC_PLTREL24
11383 || r_type == elfcpp::R_POWERPC_PLT16_LO
11384 || r_type == elfcpp::R_POWERPC_PLT16_HI
11385 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 11386 addend = rela.get_r_addend();
c9824451 11387 value = psymval->value(object, addend);
fa40fbe4 11388 unsigned int local_ent = 0;
32f59844 11389 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
11390 {
11391 if (target->abiversion() >= 2)
11392 {
11393 if (gsym != NULL)
fa40fbe4 11394 local_ent = object->ppc64_local_entry_offset(gsym);
9055360d 11395 else
fa40fbe4 11396 local_ent = object->ppc64_local_entry_offset(r_sym);
9055360d
AM
11397 }
11398 else
1611bc4a
AM
11399 {
11400 unsigned int dest_shndx;
11401 target->symval_for_branch(relinfo->symtab, gsym, object,
11402 &value, &dest_shndx);
11403 }
9055360d 11404 }
fa40fbe4
AM
11405 Address max_branch = max_branch_delta<size>(r_type);
11406 if (max_branch != 0
11407 && (value + local_ent - address + max_branch >= 2 * max_branch
32f59844 11408 || (size == 64
eb97d024
AM
11409 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11410 || r_type == elfcpp::R_PPC64_REL24_NOTOC)
32f59844
AM
11411 && (gsym != NULL
11412 ? object->ppc64_needs_toc(gsym)
11413 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
11414 {
11415 Stub_table<size, big_endian>* stub_table
11416 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
11417 if (stub_table != NULL)
11418 {
32f59844 11419 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
a19da04b 11420 = stub_table->find_long_branch_entry(value);
32f59844 11421 if (ent != NULL)
0cfb0717 11422 {
32f59844
AM
11423 if (ent->save_res_)
11424 value = (value - target->savres_section()->address()
89c905a3
AM
11425 + stub_table->stub_address()
11426 + stub_table->plt_size()
32f59844
AM
11427 + stub_table->branch_size());
11428 else
afd2ea23
AM
11429 {
11430 value = (stub_table->stub_address()
11431 + stub_table->plt_size()
11432 + ent->off_);
eb97d024
AM
11433 if (size == 64)
11434 {
11435 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11436 {
11437 if (!ent->notoc_)
11438 value += ent->p9off_;
11439 }
11440 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11441 value += ent->p9off_;
11442 else
11443 value += ent->tocoff_;
11444 }
afd2ea23 11445 }
0cfb0717
AM
11446 has_stub_value = true;
11447 }
0cfdc767 11448 }
ec661b9d 11449 }
fa40fbe4
AM
11450 if (!has_stub_value)
11451 value += local_ent;
42cacb20
DE
11452 }
11453
42cacb20
DE
11454 switch (r_type)
11455 {
32f59844
AM
11456 case elfcpp::R_PPC64_REL24_NOTOC:
11457 if (size == 32)
11458 break;
11459 // Fall through.
eb97d024 11460 case elfcpp::R_PPC64_REL24_P9NOTOC:
dd93cd0a
AM
11461 case elfcpp::R_PPC64_REL64:
11462 case elfcpp::R_POWERPC_REL32:
11463 case elfcpp::R_POWERPC_REL24:
11464 case elfcpp::R_PPC_PLTREL24:
11465 case elfcpp::R_PPC_LOCAL24PC:
11466 case elfcpp::R_POWERPC_REL16:
11467 case elfcpp::R_POWERPC_REL16_LO:
11468 case elfcpp::R_POWERPC_REL16_HI:
11469 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 11470 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
11471 case elfcpp::R_PPC64_REL16_HIGH:
11472 case elfcpp::R_PPC64_REL16_HIGHA:
11473 case elfcpp::R_PPC64_REL16_HIGHER:
11474 case elfcpp::R_PPC64_REL16_HIGHERA:
11475 case elfcpp::R_PPC64_REL16_HIGHEST:
11476 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11477 case elfcpp::R_POWERPC_REL14:
11478 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11479 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11480 case elfcpp::R_PPC64_PCREL34:
11481 case elfcpp::R_PPC64_GOT_PCREL34:
11482 case elfcpp::R_PPC64_PLT_PCREL34:
11483 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11484 case elfcpp::R_PPC64_PCREL28:
87c69f97
AM
11485 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11486 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11487 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11488 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
11489 case elfcpp::R_PPC64_REL16_HIGHER34:
11490 case elfcpp::R_PPC64_REL16_HIGHERA34:
11491 case elfcpp::R_PPC64_REL16_HIGHEST34:
11492 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
11493 value -= address;
11494 break;
11495
42cacb20
DE
11496 case elfcpp::R_PPC64_TOC16:
11497 case elfcpp::R_PPC64_TOC16_LO:
11498 case elfcpp::R_PPC64_TOC16_HI:
11499 case elfcpp::R_PPC64_TOC16_HA:
11500 case elfcpp::R_PPC64_TOC16_DS:
11501 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 11502 // Subtract the TOC base address.
a19da04b 11503 value -= target->toc_pointer();
42cacb20
DE
11504 break;
11505
cf43a2fe
AM
11506 case elfcpp::R_POWERPC_SECTOFF:
11507 case elfcpp::R_POWERPC_SECTOFF_LO:
11508 case elfcpp::R_POWERPC_SECTOFF_HI:
11509 case elfcpp::R_POWERPC_SECTOFF_HA:
11510 case elfcpp::R_PPC64_SECTOFF_DS:
11511 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11512 if (os != NULL)
11513 value -= os->address();
42cacb20
DE
11514 break;
11515
dd93cd0a
AM
11516 case elfcpp::R_PPC64_TPREL16_DS:
11517 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
11518 case elfcpp::R_PPC64_TPREL16_HIGH:
11519 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 11520 if (size != 64)
f9c6b907 11521 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 11522 break;
d8e90251 11523 // Fall through.
dd93cd0a
AM
11524 case elfcpp::R_POWERPC_TPREL16:
11525 case elfcpp::R_POWERPC_TPREL16_LO:
11526 case elfcpp::R_POWERPC_TPREL16_HI:
11527 case elfcpp::R_POWERPC_TPREL16_HA:
11528 case elfcpp::R_POWERPC_TPREL:
11529 case elfcpp::R_PPC64_TPREL16_HIGHER:
11530 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11531 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11532 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 11533 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
11534 // tls symbol values are relative to tls_segment()->vaddr()
11535 value -= tp_offset;
11536 break;
11537
11538 case elfcpp::R_PPC64_DTPREL16_DS:
11539 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11540 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11541 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11542 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11543 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11544 if (size != 64)
11545 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11546 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11547 break;
d8e90251 11548 // Fall through.
dd93cd0a
AM
11549 case elfcpp::R_POWERPC_DTPREL16:
11550 case elfcpp::R_POWERPC_DTPREL16_LO:
11551 case elfcpp::R_POWERPC_DTPREL16_HI:
11552 case elfcpp::R_POWERPC_DTPREL16_HA:
11553 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
11554 case elfcpp::R_PPC64_DTPREL16_HIGH:
11555 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 11556 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
11557 // tls symbol values are relative to tls_segment()->vaddr()
11558 value -= dtp_offset;
11559 break;
11560
45965137
AM
11561 case elfcpp::R_PPC64_ADDR64_LOCAL:
11562 if (gsym != NULL)
11563 value += object->ppc64_local_entry_offset(gsym);
11564 else
11565 value += object->ppc64_local_entry_offset(r_sym);
11566 break;
11567
42cacb20
DE
11568 default:
11569 break;
11570 }
11571
dd93cd0a 11572 Insn branch_bit = 0;
42cacb20
DE
11573 switch (r_type)
11574 {
dd93cd0a
AM
11575 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11576 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11577 branch_bit = 1 << 21;
d8e90251 11578 // Fall through.
dd93cd0a
AM
11579 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11580 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11581 {
11582 Insn* iview = reinterpret_cast<Insn*>(view);
11583 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11584 insn &= ~(1 << 21);
11585 insn |= branch_bit;
11586 if (this->is_isa_v2)
11587 {
11588 // Set 'a' bit. This is 0b00010 in BO field for branch
11589 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11590 // for branch on CTR insns (BO == 1a00t or 1a01t).
11591 if ((insn & (0x14 << 21)) == (0x04 << 21))
11592 insn |= 0x02 << 21;
11593 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11594 insn |= 0x08 << 21;
11595 else
11596 break;
11597 }
11598 else
11599 {
11600 // Invert 'y' bit if not the default.
11601 if (static_cast<Signed_address>(value) < 0)
11602 insn ^= 1 << 21;
11603 }
11604 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11605 }
11606 break;
11607
08be3224
AM
11608 case elfcpp::R_POWERPC_PLT16_HA:
11609 if (size == 32
11610 && !parameters->options().output_is_position_independent())
11611 {
11612 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11613 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11614
11615 // Convert addis to lis.
11616 if ((insn & (0x3f << 26)) == 15u << 26
11617 && (insn & (0x1f << 16)) != 0)
11618 {
11619 insn &= ~(0x1f << 16);
11620 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11621 }
11622 }
11623 break;
11624
dd93cd0a
AM
11625 default:
11626 break;
11627 }
11628
252dcdf4
AM
11629 if (gsym
11630 ? relative_value_is_known(gsym)
11631 : relative_value_is_known(psymval))
aba6bc71 11632 {
0c951c25
AM
11633 Insn* iview;
11634 Insn* iview2;
11635 Insn insn;
11636 uint64_t pinsn, pinsn2;
11637
aba6bc71
AM
11638 switch (r_type)
11639 {
11640 default:
11641 break;
11642
5edad15d
AM
11643 // Multi-instruction sequences that access the GOT/TOC can
11644 // be optimized, eg.
11645 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11646 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11647 // and
11648 // addis ra,r2,0; addi rb,ra,x@toc@l;
11649 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
11650 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11651 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11652 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11653 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11654 case elfcpp::R_POWERPC_GOT16_HA:
11655 case elfcpp::R_PPC64_TOC16_HA:
252dcdf4 11656 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11657 {
0c951c25
AM
11658 iview = reinterpret_cast<Insn*>(view - d_offset);
11659 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
11660 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11661 && object->make_toc_relative(target, &value))
11662 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11663 && object->make_got_relative(target, psymval,
11664 rela.get_r_addend(),
11665 &value)))
5edad15d
AM
11666 {
11667 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11668 == ((15u << 26) | (2 << 16)));
11669 }
11670 if (((insn & ((0x3f << 26) | 0x1f << 16))
11671 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11672 && value + 0x8000 < 0x10000)
aba6bc71
AM
11673 {
11674 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11675 return true;
11676 }
11677 }
11678 break;
11679
11680 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11681 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11682 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11683 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11684 case elfcpp::R_POWERPC_GOT16_LO:
11685 case elfcpp::R_PPC64_GOT16_LO_DS:
11686 case elfcpp::R_PPC64_TOC16_LO:
11687 case elfcpp::R_PPC64_TOC16_LO_DS:
252dcdf4 11688 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11689 {
0c951c25
AM
11690 iview = reinterpret_cast<Insn*>(view - d_offset);
11691 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 11692 bool changed = false;
c9b8abb7
AM
11693 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11694 && object->make_toc_relative(target, &value))
11695 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11696 && object->make_got_relative(target, psymval,
11697 rela.get_r_addend(),
11698 &value)))
5edad15d
AM
11699 {
11700 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11701 insn ^= (14u << 26) ^ (58u << 26);
11702 r_type = elfcpp::R_PPC64_TOC16_LO;
11703 changed = true;
11704 }
11705 if (ok_lo_toc_insn(insn, r_type)
11706 && value + 0x8000 < 0x10000)
aba6bc71
AM
11707 {
11708 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11709 {
11710 // Transform addic to addi when we change reg.
11711 insn &= ~((0x3f << 26) | (0x1f << 16));
11712 insn |= (14u << 26) | (2 << 16);
11713 }
11714 else
11715 {
11716 insn &= ~(0x1f << 16);
11717 insn |= 2 << 16;
11718 }
5edad15d 11719 changed = true;
aba6bc71 11720 }
5edad15d
AM
11721 if (changed)
11722 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
11723 }
11724 break;
549dba71 11725
c9b8abb7 11726 case elfcpp::R_PPC64_GOT_PCREL34:
252dcdf4 11727 if (size == 64 && parameters->options().toc_optimize())
c9b8abb7 11728 {
0c951c25
AM
11729 iview = reinterpret_cast<Insn*>(view);
11730 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11731 pinsn <<= 32;
11732 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11733 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
11734 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11735 break;
11736
11737 Address relval = psymval->value(object, rela.get_r_addend());
11738 relval -= address;
11739 if (relval + (1ULL << 33) < 1ULL << 34)
11740 {
11741 value = relval;
11742 // Replace with paddi
0c951c25
AM
11743 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11744 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11745 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11746 pinsn & 0xffffffff);
11747 goto pcrelopt;
c9b8abb7
AM
11748 }
11749 }
11750 break;
11751
0c951c25 11752 case elfcpp::R_PPC64_PCREL34:
252dcdf4
AM
11753 if (size == 64)
11754 {
11755 iview = reinterpret_cast<Insn*>(view);
11756 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11757 pinsn <<= 32;
11758 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11759 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11760 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11761 | (14ULL << 26) /* paddi */))
11762 break;
0c951c25 11763
252dcdf4
AM
11764 pcrelopt:
11765 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11766 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11767 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11768 if (relnum >= reloc_count - 1)
11769 break;
0c951c25 11770
252dcdf4
AM
11771 Reltype next_rela(preloc + reloc_size);
11772 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11773 != elfcpp::R_PPC64_PCREL_OPT)
11774 || next_rela.get_r_offset() != rela.get_r_offset())
11775 break;
0c951c25 11776
252dcdf4
AM
11777 Address off = next_rela.get_r_addend();
11778 if (off == 0)
11779 off = 8; // zero means next insn.
11780 if (off + rela.get_r_offset() + 4 > view_size)
11781 break;
0c951c25 11782
252dcdf4
AM
11783 iview2 = reinterpret_cast<Insn*>(view + off);
11784 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11785 pinsn2 <<= 32;
11786 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11787 break;
11788 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11789 {
11790 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11791 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11792 pinsn & 0xffffffff);
11793 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11794 }
11795 }
0c951c25
AM
11796 break;
11797
9a23f96e 11798 case elfcpp::R_POWERPC_TPREL16_HA:
252dcdf4 11799 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11800 {
11801 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
252dcdf4
AM
11802 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11803 return true;
9a23f96e
AM
11804 }
11805 break;
11806
11807 case elfcpp::R_PPC64_TPREL16_LO_DS:
11808 if (size == 32)
11809 // R_PPC_TLSGD, R_PPC_TLSLD
11810 break;
11811 // Fall through.
11812 case elfcpp::R_POWERPC_TPREL16_LO:
252dcdf4 11813 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11814 {
11815 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11816 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11817 insn &= ~(0x1f << 16);
11818 insn |= (size == 32 ? 2 : 13) << 16;
11819 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11820 }
11821 break;
11822
549dba71 11823 case elfcpp::R_PPC64_ENTRY:
252dcdf4 11824 if (size == 64)
549dba71 11825 {
a19da04b 11826 value = target->toc_pointer();
252dcdf4
AM
11827 if (value + 0x80008000 <= 0xffffffff
11828 && !parameters->options().output_is_position_independent())
549dba71
AM
11829 {
11830 Insn* iview = reinterpret_cast<Insn*>(view);
11831 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11832 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11833
11834 if ((insn1 & ~0xfffc) == ld_2_12
11835 && insn2 == add_2_2_12)
11836 {
252dcdf4 11837 insn1 = lis_2 + ha(value);
549dba71
AM
11838 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11839 insn2 = addi_2_2 + l(value);
11840 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11841 return true;
11842 }
11843 }
252dcdf4
AM
11844 else
11845 {
11846 value -= address;
11847 if (value + 0x80008000 <= 0xffffffff)
11848 {
11849 Insn* iview = reinterpret_cast<Insn*>(view);
11850 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11851 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11852
11853 if ((insn1 & ~0xfffc) == ld_2_12
11854 && insn2 == add_2_2_12)
11855 {
11856 insn1 = addis_2_12 + ha(value);
11857 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11858 insn2 = addi_2_2 + l(value);
11859 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11860 return true;
11861 }
11862 }
11863 }
549dba71
AM
11864 }
11865 break;
e3a7574e
AM
11866
11867 case elfcpp::R_POWERPC_REL16_LO:
11868 // If we are generating a non-PIC executable, edit
11869 // 0: addis 2,12,.TOC.-0b@ha
11870 // addi 2,2,.TOC.-0b@l
11871 // used by ELFv2 global entry points to set up r2, to
11872 // lis 2,.TOC.@ha
11873 // addi 2,2,.TOC.@l
11874 // if .TOC. is in range. */
252dcdf4
AM
11875 if (size == 64
11876 && value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11877 && relnum + 1 > 1
e3a7574e
AM
11878 && preloc != NULL
11879 && target->abiversion() >= 2
11880 && !parameters->options().output_is_position_independent()
4f038ee5 11881 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11882 && gsym != NULL
11883 && strcmp(gsym->name(), ".TOC.") == 0)
11884 {
0e123f69 11885 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11886 Reltype prev_rela(preloc - reloc_size);
11887 if ((prev_rela.get_r_info()
11888 == elfcpp::elf_r_info<size>(r_sym,
11889 elfcpp::R_POWERPC_REL16_HA))
11890 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11891 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11892 {
dcfc7dd4 11893 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11894 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11895 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11896
11897 if ((insn1 & 0xffff0000) == addis_2_12
11898 && (insn2 & 0xffff0000) == addi_2_2)
11899 {
11900 insn1 = lis_2 + ha(value + address - 4);
11901 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11902 insn2 = addi_2_2 + l(value + address - 4);
11903 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11904 if (relinfo->rr)
11905 {
11906 relinfo->rr->set_strategy(relnum - 1,
11907 Relocatable_relocs::RELOC_SPECIAL);
11908 relinfo->rr->set_strategy(relnum,
11909 Relocatable_relocs::RELOC_SPECIAL);
11910 }
11911 return true;
11912 }
11913 }
11914 }
11915 break;
aba6bc71
AM
11916 }
11917 }
11918
f4baf0d4 11919 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11920 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11921 switch (r_type)
11922 {
11923 case elfcpp::R_POWERPC_ADDR32:
11924 case elfcpp::R_POWERPC_UADDR32:
11925 if (size == 64)
f4baf0d4 11926 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11927 break;
11928
11929 case elfcpp::R_POWERPC_REL32:
a680de9a 11930 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11931 if (size == 64)
f4baf0d4 11932 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11933 break;
11934
dd93cd0a 11935 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11936 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11937 break;
11938
b80eed39
AM
11939 case elfcpp::R_POWERPC_ADDR16:
11940 // We really should have three separate relocations,
11941 // one for 16-bit data, one for insns with 16-bit signed fields,
11942 // and one for insns with 16-bit unsigned fields.
11943 overflow = Reloc::CHECK_BITFIELD;
11944 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11945 overflow = Reloc::CHECK_LOW_INSN;
11946 break;
11947
f9c6b907
AM
11948 case elfcpp::R_POWERPC_ADDR16_HI:
11949 case elfcpp::R_POWERPC_ADDR16_HA:
11950 case elfcpp::R_POWERPC_GOT16_HI:
11951 case elfcpp::R_POWERPC_GOT16_HA:
11952 case elfcpp::R_POWERPC_PLT16_HI:
11953 case elfcpp::R_POWERPC_PLT16_HA:
11954 case elfcpp::R_POWERPC_SECTOFF_HI:
11955 case elfcpp::R_POWERPC_SECTOFF_HA:
11956 case elfcpp::R_PPC64_TOC16_HI:
11957 case elfcpp::R_PPC64_TOC16_HA:
11958 case elfcpp::R_PPC64_PLTGOT16_HI:
11959 case elfcpp::R_PPC64_PLTGOT16_HA:
11960 case elfcpp::R_POWERPC_TPREL16_HI:
11961 case elfcpp::R_POWERPC_TPREL16_HA:
11962 case elfcpp::R_POWERPC_DTPREL16_HI:
11963 case elfcpp::R_POWERPC_DTPREL16_HA:
11964 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11965 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11966 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11967 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11968 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11969 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11970 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11971 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11972 case elfcpp::R_POWERPC_REL16_HI:
11973 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11974 if (size != 32)
11975 overflow = Reloc::CHECK_HIGH_INSN;
11976 break;
11977
dd93cd0a
AM
11978 case elfcpp::R_POWERPC_REL16:
11979 case elfcpp::R_PPC64_TOC16:
11980 case elfcpp::R_POWERPC_GOT16:
11981 case elfcpp::R_POWERPC_SECTOFF:
11982 case elfcpp::R_POWERPC_TPREL16:
11983 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11984 case elfcpp::R_POWERPC_GOT_TLSGD16:
11985 case elfcpp::R_POWERPC_GOT_TLSLD16:
11986 case elfcpp::R_POWERPC_GOT_TPREL16:
11987 case elfcpp::R_POWERPC_GOT_DTPREL16:
11988 overflow = Reloc::CHECK_LOW_INSN;
11989 break;
11990
32f59844
AM
11991 case elfcpp::R_PPC64_REL24_NOTOC:
11992 if (size == 32)
11993 break;
11994 // Fall through.
eb97d024 11995 case elfcpp::R_PPC64_REL24_P9NOTOC:
b80eed39
AM
11996 case elfcpp::R_POWERPC_ADDR24:
11997 case elfcpp::R_POWERPC_ADDR14:
11998 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11999 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12000 case elfcpp::R_PPC64_ADDR16_DS:
12001 case elfcpp::R_POWERPC_REL24:
12002 case elfcpp::R_PPC_PLTREL24:
12003 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
12004 case elfcpp::R_PPC64_TPREL16_DS:
12005 case elfcpp::R_PPC64_DTPREL16_DS:
12006 case elfcpp::R_PPC64_TOC16_DS:
12007 case elfcpp::R_PPC64_GOT16_DS:
12008 case elfcpp::R_PPC64_SECTOFF_DS:
12009 case elfcpp::R_POWERPC_REL14:
12010 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12011 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
12012 case elfcpp::R_PPC64_D34:
12013 case elfcpp::R_PPC64_PCREL34:
12014 case elfcpp::R_PPC64_GOT_PCREL34:
12015 case elfcpp::R_PPC64_PLT_PCREL34:
12016 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12017 case elfcpp::R_PPC64_D28:
12018 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
12019 case elfcpp::R_PPC64_TPREL34:
12020 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
12021 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12022 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12023 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12024 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
f4baf0d4 12025 overflow = Reloc::CHECK_SIGNED;
42cacb20 12026 break;
dd93cd0a 12027 }
42cacb20 12028
dcfc7dd4 12029 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
12030 Insn insn = 0;
12031
b80eed39
AM
12032 if (overflow == Reloc::CHECK_LOW_INSN
12033 || overflow == Reloc::CHECK_HIGH_INSN)
12034 {
a680de9a 12035 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 12036
a47622ac
AM
12037 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
12038 overflow = Reloc::CHECK_BITFIELD;
12039 else if (overflow == Reloc::CHECK_LOW_INSN
12040 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
12041 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
12042 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
12043 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
12044 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
12045 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 12046 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
12047 else
12048 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
12049 }
12050
a680de9a 12051 bool maybe_dq_reloc = false;
3ea0a085 12052 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 12053 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
12054 switch (r_type)
12055 {
12056 case elfcpp::R_POWERPC_NONE:
12057 case elfcpp::R_POWERPC_TLS:
12058 case elfcpp::R_POWERPC_GNU_VTINHERIT:
12059 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
12060 case elfcpp::R_POWERPC_PLTSEQ:
12061 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
12062 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12063 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 12064 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
12065 break;
12066
12067 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 12068 case elfcpp::R_PPC64_REL64:
cf43a2fe 12069 case elfcpp::R_PPC64_TOC:
45965137 12070 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
12071 Reloc::addr64(view, value);
12072 break;
12073
12074 case elfcpp::R_POWERPC_TPREL:
12075 case elfcpp::R_POWERPC_DTPREL:
12076 if (size == 64)
12077 Reloc::addr64(view, value);
12078 else
3ea0a085 12079 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
12080 break;
12081
12082 case elfcpp::R_PPC64_UADDR64:
12083 Reloc::addr64_u(view, value);
42cacb20
DE
12084 break;
12085
12086 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 12087 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
12088 break;
12089
acc276d8 12090 case elfcpp::R_POWERPC_REL32:
dd93cd0a 12091 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 12092 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
12093 break;
12094
32f59844
AM
12095 case elfcpp::R_PPC64_REL24_NOTOC:
12096 if (size == 32)
12097 goto unsupp; // R_PPC_EMB_RELSDA
12098 // Fall through.
eb97d024 12099 case elfcpp::R_PPC64_REL24_P9NOTOC:
dd93cd0a
AM
12100 case elfcpp::R_POWERPC_ADDR24:
12101 case elfcpp::R_POWERPC_REL24:
12102 case elfcpp::R_PPC_PLTREL24:
12103 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 12104 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
12105 break;
12106
dd93cd0a
AM
12107 case elfcpp::R_POWERPC_GOT_DTPREL16:
12108 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
12109 case elfcpp::R_POWERPC_GOT_TPREL16:
12110 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
12111 if (size == 64)
12112 {
ec86f434 12113 // On ppc64 these are all ds form
a680de9a 12114 maybe_dq_reloc = true;
dd93cd0a
AM
12115 break;
12116 }
c25aa1e1 12117 // Fall through.
cf43a2fe 12118 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 12119 case elfcpp::R_POWERPC_REL16:
cf43a2fe 12120 case elfcpp::R_PPC64_TOC16:
42cacb20 12121 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 12122 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
12123 case elfcpp::R_POWERPC_TPREL16:
12124 case elfcpp::R_POWERPC_DTPREL16:
12125 case elfcpp::R_POWERPC_GOT_TLSGD16:
12126 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 12127 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 12128 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 12129 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 12130 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 12131 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 12132 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
12133 case elfcpp::R_POWERPC_TPREL16_LO:
12134 case elfcpp::R_POWERPC_DTPREL16_LO:
12135 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12136 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
12137 if (size == 64)
12138 status = Reloc::addr16(view, value, overflow);
12139 else
12140 maybe_dq_reloc = true;
dd93cd0a
AM
12141 break;
12142
12143 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 12144 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
12145 break;
12146
f9c6b907
AM
12147 case elfcpp::R_PPC64_ADDR16_HIGH:
12148 case elfcpp::R_PPC64_TPREL16_HIGH:
12149 case elfcpp::R_PPC64_DTPREL16_HIGH:
12150 if (size == 32)
12151 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12152 goto unsupp;
d8e90251 12153 // Fall through.
cf43a2fe 12154 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 12155 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 12156 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 12157 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 12158 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 12159 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 12160 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
12161 case elfcpp::R_POWERPC_TPREL16_HI:
12162 case elfcpp::R_POWERPC_DTPREL16_HI:
12163 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12164 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12165 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12166 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12167 Reloc::addr16_hi(view, value);
42cacb20
DE
12168 break;
12169
f9c6b907
AM
12170 case elfcpp::R_PPC64_ADDR16_HIGHA:
12171 case elfcpp::R_PPC64_TPREL16_HIGHA:
12172 case elfcpp::R_PPC64_DTPREL16_HIGHA:
12173 if (size == 32)
12174 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12175 goto unsupp;
d8e90251 12176 // Fall through.
cf43a2fe 12177 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 12178 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 12179 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 12180 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 12181 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 12182 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 12183 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
12184 case elfcpp::R_POWERPC_TPREL16_HA:
12185 case elfcpp::R_POWERPC_DTPREL16_HA:
12186 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12187 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12188 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12189 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12190 Reloc::addr16_ha(view, value);
42cacb20
DE
12191 break;
12192
a680de9a
PB
12193 case elfcpp::R_POWERPC_REL16DX_HA:
12194 status = Reloc::addr16dx_ha(view, value, overflow);
12195 break;
12196
dd93cd0a
AM
12197 case elfcpp::R_PPC64_DTPREL16_HIGHER:
12198 if (size == 32)
12199 // R_PPC_EMB_NADDR16_LO
12200 goto unsupp;
d8e90251 12201 // Fall through.
dd93cd0a 12202 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 12203 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
12204 case elfcpp::R_PPC64_TPREL16_HIGHER:
12205 Reloc::addr16_hi2(view, value);
42cacb20
DE
12206 break;
12207
dd93cd0a
AM
12208 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12209 if (size == 32)
12210 // R_PPC_EMB_NADDR16_HI
12211 goto unsupp;
d8e90251 12212 // Fall through.
dd93cd0a 12213 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 12214 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
12215 case elfcpp::R_PPC64_TPREL16_HIGHERA:
12216 Reloc::addr16_ha2(view, value);
42cacb20
DE
12217 break;
12218
dd93cd0a
AM
12219 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12220 if (size == 32)
12221 // R_PPC_EMB_NADDR16_HA
12222 goto unsupp;
d8e90251 12223 // Fall through.
dd93cd0a 12224 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 12225 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
12226 case elfcpp::R_PPC64_TPREL16_HIGHEST:
12227 Reloc::addr16_hi3(view, value);
42cacb20
DE
12228 break;
12229
dd93cd0a
AM
12230 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12231 if (size == 32)
12232 // R_PPC_EMB_SDAI16
12233 goto unsupp;
d8e90251 12234 // Fall through.
dd93cd0a 12235 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 12236 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
12237 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12238 Reloc::addr16_ha3(view, value);
12239 break;
12240
12241 case elfcpp::R_PPC64_DTPREL16_DS:
12242 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12243 if (size == 32)
12244 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12245 goto unsupp;
d8e90251 12246 // Fall through.
dd93cd0a
AM
12247 case elfcpp::R_PPC64_TPREL16_DS:
12248 case elfcpp::R_PPC64_TPREL16_LO_DS:
12249 if (size == 32)
12250 // R_PPC_TLSGD, R_PPC_TLSLD
12251 break;
d8e90251 12252 // Fall through.
cf43a2fe
AM
12253 case elfcpp::R_PPC64_ADDR16_DS:
12254 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
12255 case elfcpp::R_PPC64_TOC16_DS:
12256 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
12257 case elfcpp::R_PPC64_GOT16_DS:
12258 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 12259 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
12260 case elfcpp::R_PPC64_SECTOFF_DS:
12261 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 12262 maybe_dq_reloc = true;
dd93cd0a
AM
12263 break;
12264
12265 case elfcpp::R_POWERPC_ADDR14:
12266 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12267 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12268 case elfcpp::R_POWERPC_REL14:
12269 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12270 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 12271 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
12272 break;
12273
12274 case elfcpp::R_POWERPC_COPY:
12275 case elfcpp::R_POWERPC_GLOB_DAT:
12276 case elfcpp::R_POWERPC_JMP_SLOT:
12277 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 12278 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
12279 case elfcpp::R_PPC64_JMP_IREL:
12280 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
12281 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12282 _("unexpected reloc %u in object file"),
12283 r_type);
12284 break;
12285
7e57d19e 12286 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 12287 if (size == 32)
7e57d19e 12288 // R_PPC_EMB_SDA21
dd93cd0a
AM
12289 goto unsupp;
12290 else
12291 {
7e57d19e
AM
12292 Symbol_location loc;
12293 loc.object = relinfo->object;
12294 loc.shndx = relinfo->data_shndx;
12295 loc.offset = rela.get_r_offset();
b0d0d02b
AM
12296 const Tocsave_loc *tocsave = target->tocsave_loc();
12297 if (tocsave->find(loc) != tocsave->end())
7e57d19e
AM
12298 {
12299 // If we've generated plt calls using this tocsave, then
12300 // the nop needs to be changed to save r2.
12301 Insn* iview = reinterpret_cast<Insn*>(view);
12302 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12303 elfcpp::Swap<32, big_endian>::
12304 writeval(iview, std_2_1 + target->stk_toc());
12305 }
dd93cd0a
AM
12306 }
12307 break;
12308
12309 case elfcpp::R_PPC_EMB_SDA2I16:
12310 case elfcpp::R_PPC_EMB_SDA2REL:
12311 if (size == 32)
12312 goto unsupp;
12313 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
12314 break;
12315
e4dff765
AM
12316 case elfcpp::R_PPC64_D34:
12317 case elfcpp::R_PPC64_D34_LO:
12318 case elfcpp::R_PPC64_PCREL34:
12319 case elfcpp::R_PPC64_GOT_PCREL34:
12320 case elfcpp::R_PPC64_PLT_PCREL34:
12321 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
12322 case elfcpp::R_PPC64_TPREL34:
12323 case elfcpp::R_PPC64_DTPREL34:
87c69f97
AM
12324 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12325 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12326 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12327 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
e4dff765
AM
12328 if (size == 32)
12329 goto unsupp;
12330 status = Reloc::addr34(view, value, overflow);
12331 break;
12332
12333 case elfcpp::R_PPC64_D34_HI30:
12334 if (size == 32)
12335 goto unsupp;
12336 Reloc::addr34_hi(view, value);
12337 break;
12338
12339 case elfcpp::R_PPC64_D34_HA30:
12340 if (size == 32)
12341 goto unsupp;
12342 Reloc::addr34_ha(view, value);
12343 break;
12344
12345 case elfcpp::R_PPC64_D28:
12346 case elfcpp::R_PPC64_PCREL28:
12347 if (size == 32)
12348 goto unsupp;
12349 status = Reloc::addr28(view, value, overflow);
12350 break;
12351
12352 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12353 case elfcpp::R_PPC64_REL16_HIGHER34:
12354 if (size == 32)
12355 goto unsupp;
12356 Reloc::addr16_higher34(view, value);
12357 break;
12358
12359 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12360 case elfcpp::R_PPC64_REL16_HIGHERA34:
12361 if (size == 32)
12362 goto unsupp;
12363 Reloc::addr16_highera34(view, value);
12364 break;
12365
12366 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12367 case elfcpp::R_PPC64_REL16_HIGHEST34:
12368 if (size == 32)
12369 goto unsupp;
12370 Reloc::addr16_highest34(view, value);
12371 break;
12372
12373 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12374 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12375 if (size == 32)
12376 goto unsupp;
12377 Reloc::addr16_highesta34(view, value);
12378 break;
12379
dd93cd0a
AM
12380 case elfcpp::R_POWERPC_PLT32:
12381 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
12382 case elfcpp::R_PPC_SDAREL16:
12383 case elfcpp::R_POWERPC_ADDR30:
12384 case elfcpp::R_PPC64_PLT64:
12385 case elfcpp::R_PPC64_PLTREL64:
12386 case elfcpp::R_PPC64_PLTGOT16:
12387 case elfcpp::R_PPC64_PLTGOT16_LO:
12388 case elfcpp::R_PPC64_PLTGOT16_HI:
12389 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
12390 case elfcpp::R_PPC64_PLTGOT16_DS:
12391 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 12392 case elfcpp::R_PPC_TOC16:
42cacb20 12393 default:
dd93cd0a 12394 unsupp:
42cacb20
DE
12395 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12396 _("unsupported reloc %u"),
12397 r_type);
12398 break;
12399 }
a680de9a
PB
12400
12401 if (maybe_dq_reloc)
12402 {
12403 if (insn == 0)
12404 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12405
12406 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12407 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12408 && (insn & 3) == 1))
12409 status = Reloc::addr16_dq(view, value, overflow);
12410 else if (size == 64
12411 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12412 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12413 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12414 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12415 status = Reloc::addr16_ds(view, value, overflow);
12416 else
12417 status = Reloc::addr16(view, value, overflow);
12418 }
12419
0cfb0717 12420 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
12421 && (has_stub_value
12422 || !(gsym != NULL
282c9750 12423 && gsym->is_undefined()
32f59844 12424 && is_branch_reloc<size>(r_type))))
0cfb0717 12425 {
b5c37946
SJ
12426 std::string name;
12427 if (gsym)
12428 name = gsym->demangled_name();
12429 else
12430 name = relinfo->object->get_symbol_name(r_sym);
03c02b69
AM
12431 if (os->flags() & elfcpp::SHF_ALLOC)
12432 {
12433 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
b5c37946
SJ
12434 _("reloc type %u overflow against '%s'"),
12435 r_type, name.c_str());
03c02b69
AM
12436 if (has_stub_value)
12437 gold_info(_("try relinking with a smaller --stub-group-size"));
12438 }
12439 else
12440 {
12441 gold_warning_at_location(relinfo, relnum, rela.get_r_offset(),
b5c37946
SJ
12442 _("reloc type %u overflow against '%s'"),
12443 r_type, name.c_str());
03c02b69
AM
12444 gold_info(_("debug info may be unreliable, compile with -gdwarf64"));
12445 }
0cfb0717 12446 }
42cacb20
DE
12447
12448 return true;
12449}
12450
42cacb20
DE
12451// Relocate section data.
12452
12453template<int size, bool big_endian>
12454void
12455Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
12456 const Relocate_info<size, big_endian>* relinfo,
12457 unsigned int sh_type,
12458 const unsigned char* prelocs,
12459 size_t reloc_count,
12460 Output_section* output_section,
12461 bool needs_special_offset_handling,
12462 unsigned char* view,
c9269dff 12463 Address address,
d83ce4e3
AM
12464 section_size_type view_size,
12465 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
12466{
12467 typedef Target_powerpc<size, big_endian> Powerpc;
12468 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
12469 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12470 Powerpc_comdat_behavior;
4d625b70
CC
12471 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12472 Classify_reloc;
42cacb20
DE
12473
12474 gold_assert(sh_type == elfcpp::SHT_RELA);
12475
4d625b70
CC
12476 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12477 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
12478 relinfo,
12479 this,
12480 prelocs,
12481 reloc_count,
12482 output_section,
12483 needs_special_offset_handling,
12484 view,
12485 address,
364c7fa5
ILT
12486 view_size,
12487 reloc_symbol_changes);
42cacb20
DE
12488}
12489
4d625b70 12490template<int size, bool big_endian>
cf43a2fe 12491class Powerpc_scan_relocatable_reloc
42cacb20 12492{
cf43a2fe 12493public:
0e123f69
AM
12494 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12495 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
12496 static const int sh_type = elfcpp::SHT_RELA;
12497
12498 // Return the symbol referred to by the relocation.
12499 static inline unsigned int
12500 get_r_sym(const Reltype* reloc)
12501 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12502
12503 // Return the type of the relocation.
12504 static inline unsigned int
12505 get_r_type(const Reltype* reloc)
12506 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12507
cf43a2fe
AM
12508 // Return the strategy to use for a local symbol which is not a
12509 // section symbol, given the relocation type.
12510 inline Relocatable_relocs::Reloc_strategy
12511 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12512 {
12513 if (r_type == 0 && r_sym == 0)
12514 return Relocatable_relocs::RELOC_DISCARD;
12515 return Relocatable_relocs::RELOC_COPY;
12516 }
12517
12518 // Return the strategy to use for a local symbol which is a section
12519 // symbol, given the relocation type.
12520 inline Relocatable_relocs::Reloc_strategy
12521 local_section_strategy(unsigned int, Relobj*)
12522 {
12523 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12524 }
12525
12526 // Return the strategy to use for a global symbol, given the
12527 // relocation type, the object, and the symbol index.
12528 inline Relocatable_relocs::Reloc_strategy
12529 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12530 {
08be3224
AM
12531 if (size == 32
12532 && (r_type == elfcpp::R_PPC_PLTREL24
12533 || r_type == elfcpp::R_POWERPC_PLT16_LO
12534 || r_type == elfcpp::R_POWERPC_PLT16_HI
12535 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
12536 return Relocatable_relocs::RELOC_SPECIAL;
12537 return Relocatable_relocs::RELOC_COPY;
12538 }
12539};
42cacb20
DE
12540
12541// Scan the relocs during a relocatable link.
12542
12543template<int size, bool big_endian>
12544void
12545Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
12546 Symbol_table* symtab,
12547 Layout* layout,
12548 Sized_relobj_file<size, big_endian>* object,
12549 unsigned int data_shndx,
12550 unsigned int sh_type,
12551 const unsigned char* prelocs,
12552 size_t reloc_count,
12553 Output_section* output_section,
12554 bool needs_special_offset_handling,
12555 size_t local_symbol_count,
12556 const unsigned char* plocal_symbols,
12557 Relocatable_relocs* rr)
42cacb20 12558{
4d625b70
CC
12559 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12560
42cacb20
DE
12561 gold_assert(sh_type == elfcpp::SHT_RELA);
12562
4d625b70 12563 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
12564 symtab,
12565 layout,
12566 object,
12567 data_shndx,
12568 prelocs,
12569 reloc_count,
12570 output_section,
12571 needs_special_offset_handling,
12572 local_symbol_count,
12573 plocal_symbols,
12574 rr);
12575}
12576
4d625b70
CC
12577// Scan the relocs for --emit-relocs.
12578
12579template<int size, bool big_endian>
12580void
12581Target_powerpc<size, big_endian>::emit_relocs_scan(
12582 Symbol_table* symtab,
12583 Layout* layout,
12584 Sized_relobj_file<size, big_endian>* object,
12585 unsigned int data_shndx,
12586 unsigned int sh_type,
12587 const unsigned char* prelocs,
12588 size_t reloc_count,
12589 Output_section* output_section,
12590 bool needs_special_offset_handling,
12591 size_t local_symbol_count,
12592 const unsigned char* plocal_syms,
12593 Relocatable_relocs* rr)
12594{
12595 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12596 Classify_reloc;
12597 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12598 Emit_relocs_strategy;
12599
12600 gold_assert(sh_type == elfcpp::SHT_RELA);
12601
12602 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12603 symtab,
12604 layout,
12605 object,
12606 data_shndx,
12607 prelocs,
12608 reloc_count,
12609 output_section,
12610 needs_special_offset_handling,
12611 local_symbol_count,
12612 plocal_syms,
12613 rr);
12614}
12615
7404fe1b 12616// Emit relocations for a section.
dd93cd0a
AM
12617// This is a modified version of the function by the same name in
12618// target-reloc.h. Using relocate_special_relocatable for
12619// R_PPC_PLTREL24 would require duplication of the entire body of the
12620// loop, so we may as well duplicate the whole thing.
42cacb20
DE
12621
12622template<int size, bool big_endian>
12623void
7404fe1b 12624Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
12625 const Relocate_info<size, big_endian>* relinfo,
12626 unsigned int sh_type,
12627 const unsigned char* prelocs,
12628 size_t reloc_count,
12629 Output_section* output_section,
62fe925a 12630 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 12631 unsigned char*,
dd93cd0a 12632 Address view_address,
cf43a2fe 12633 section_size_type,
42cacb20
DE
12634 unsigned char* reloc_view,
12635 section_size_type reloc_view_size)
12636{
12637 gold_assert(sh_type == elfcpp::SHT_RELA);
12638
0e123f69
AM
12639 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12640 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12641 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
12642 // Offset from start of insn to d-field reloc.
12643 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
12644
12645 Powerpc_relobj<size, big_endian>* const object
12646 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12647 const unsigned int local_count = object->local_symbol_count();
12648 unsigned int got2_shndx = object->got2_shndx();
c9269dff 12649 Address got2_addend = 0;
cf43a2fe 12650 if (got2_shndx != 0)
c9269dff
AM
12651 {
12652 got2_addend = object->get_output_section_offset(got2_shndx);
12653 gold_assert(got2_addend != invalid_address);
12654 }
cf43a2fe 12655
033bfb73
CC
12656 const bool relocatable = parameters->options().relocatable();
12657
cf43a2fe 12658 unsigned char* pwrite = reloc_view;
7404fe1b 12659 bool zap_next = false;
cf43a2fe
AM
12660 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12661 {
91a65d2f 12662 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
12663 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12664 continue;
12665
12666 Reltype reloc(prelocs);
12667 Reltype_write reloc_write(pwrite);
12668
7404fe1b 12669 Address offset = reloc.get_r_offset();
cf43a2fe 12670 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
12671 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12672 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12673 const unsigned int orig_r_sym = r_sym;
12674 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12675 = reloc.get_r_addend();
12676 const Symbol* gsym = NULL;
12677
12678 if (zap_next)
12679 {
12680 // We could arrange to discard these and other relocs for
12681 // tls optimised sequences in the strategy methods, but for
12682 // now do as BFD ld does.
12683 r_type = elfcpp::R_POWERPC_NONE;
12684 zap_next = false;
12685 }
cf43a2fe
AM
12686
12687 // Get the new symbol index.
9215b98b 12688 Output_section* os = NULL;
cf43a2fe
AM
12689 if (r_sym < local_count)
12690 {
12691 switch (strategy)
12692 {
12693 case Relocatable_relocs::RELOC_COPY:
12694 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 12695 if (r_sym != 0)
dd93cd0a 12696 {
7404fe1b
AM
12697 r_sym = object->symtab_index(r_sym);
12698 gold_assert(r_sym != -1U);
dd93cd0a 12699 }
cf43a2fe
AM
12700 break;
12701
12702 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12703 {
12704 // We are adjusting a section symbol. We need to find
12705 // the symbol table index of the section symbol for
12706 // the output section corresponding to input section
12707 // in which this symbol is defined.
12708 gold_assert(r_sym < local_count);
12709 bool is_ordinary;
12710 unsigned int shndx =
12711 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12712 gold_assert(is_ordinary);
9215b98b 12713 os = object->output_section(shndx);
cf43a2fe
AM
12714 gold_assert(os != NULL);
12715 gold_assert(os->needs_symtab_index());
7404fe1b 12716 r_sym = os->symtab_index();
cf43a2fe
AM
12717 }
12718 break;
12719
12720 default:
12721 gold_unreachable();
12722 }
12723 }
12724 else
12725 {
7404fe1b 12726 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
12727 gold_assert(gsym != NULL);
12728 if (gsym->is_forwarder())
12729 gsym = relinfo->symtab->resolve_forwards(gsym);
12730
12731 gold_assert(gsym->has_symtab_index());
7404fe1b 12732 r_sym = gsym->symtab_index();
cf43a2fe
AM
12733 }
12734
12735 // Get the new offset--the location in the output section where
12736 // this relocation should be applied.
cf43a2fe 12737 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12738 offset += offset_in_output_section;
cf43a2fe
AM
12739 else
12740 {
c9269dff
AM
12741 section_offset_type sot_offset =
12742 convert_types<section_offset_type, Address>(offset);
cf43a2fe 12743 section_offset_type new_sot_offset =
c9269dff
AM
12744 output_section->output_offset(object, relinfo->data_shndx,
12745 sot_offset);
cf43a2fe 12746 gold_assert(new_sot_offset != -1);
7404fe1b 12747 offset = new_sot_offset;
cf43a2fe
AM
12748 }
12749
dd93cd0a
AM
12750 // In an object file, r_offset is an offset within the section.
12751 // In an executable or dynamic object, generated by
12752 // --emit-relocs, r_offset is an absolute address.
033bfb73 12753 if (!relocatable)
dd93cd0a 12754 {
7404fe1b 12755 offset += view_address;
dd93cd0a 12756 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12757 offset -= offset_in_output_section;
dd93cd0a
AM
12758 }
12759
cf43a2fe 12760 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12761 if (strategy == Relocatable_relocs::RELOC_COPY)
12762 ;
12763 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12764 {
7404fe1b 12765 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12766 addend = psymval->value(object, addend);
12767 // In a relocatable link, the symbol value is relative to
12768 // the start of the output section. For a non-relocatable
12769 // link, we need to adjust the addend.
12770 if (!relocatable)
12771 {
12772 gold_assert(os != NULL);
12773 addend -= os->address();
12774 }
cf43a2fe
AM
12775 }
12776 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12777 {
e3a7574e
AM
12778 if (size == 32)
12779 {
12780 if (addend >= 32768)
12781 addend += got2_addend;
12782 }
12783 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12784 {
12785 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12786 addend -= d_offset;
e3a7574e
AM
12787 }
12788 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12789 {
12790 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12791 addend -= d_offset + 4;
e3a7574e 12792 }
cf43a2fe
AM
12793 }
12794 else
12795 gold_unreachable();
12796
033bfb73 12797 if (!relocatable)
7404fe1b
AM
12798 {
12799 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12800 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12801 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12802 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12803 {
12804 // First instruction of a global dynamic sequence,
12805 // arg setup insn.
c7fdac09
AM
12806 bool final = gsym == NULL || gsym->final_value_is_known();
12807 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
0af4fcc2 12808 switch (tls_type)
7404fe1b
AM
12809 {
12810 case tls::TLSOPT_TO_IE:
12811 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12812 - elfcpp::R_POWERPC_GOT_TLSGD16);
12813 break;
12814 case tls::TLSOPT_TO_LE:
12815 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12816 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12817 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12818 else
12819 {
12820 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12821 offset -= d_offset;
7404fe1b
AM
12822 }
12823 break;
12824 default:
12825 break;
12826 }
12827 }
12828 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12829 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12830 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12831 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12832 {
12833 // First instruction of a local dynamic sequence,
12834 // arg setup insn.
c7fdac09 12835 tls::Tls_optimization tls_type = this->optimize_tls_ld();
0af4fcc2 12836 if (tls_type == tls::TLSOPT_TO_LE)
7404fe1b
AM
12837 {
12838 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12839 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12840 {
12841 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12842 const Output_section* os = relinfo->layout->tls_segment()
12843 ->first_section();
12844 gold_assert(os != NULL);
12845 gold_assert(os->needs_symtab_index());
12846 r_sym = os->symtab_index();
12847 addend = dtp_offset;
12848 }
12849 else
12850 {
12851 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12852 offset -= d_offset;
7404fe1b
AM
12853 }
12854 }
12855 }
12856 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12857 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12858 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12859 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12860 {
12861 // First instruction of initial exec sequence.
0af4fcc2 12862 bool final = gsym == NULL || gsym->final_value_is_known();
7404fe1b
AM
12863 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12864 {
12865 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12866 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12867 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12868 else
12869 {
12870 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12871 offset -= d_offset;
7404fe1b
AM
12872 }
12873 }
12874 }
12875 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12876 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12877 {
12878 // Second instruction of a global dynamic sequence,
12879 // the __tls_get_addr call
c7fdac09
AM
12880 bool final = gsym == NULL || gsym->final_value_is_known();
12881 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
0af4fcc2 12882 switch (tls_type)
7404fe1b
AM
12883 {
12884 case tls::TLSOPT_TO_IE:
12885 r_type = elfcpp::R_POWERPC_NONE;
12886 zap_next = true;
12887 break;
12888 case tls::TLSOPT_TO_LE:
12889 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12890 offset += d_offset;
7404fe1b
AM
12891 zap_next = true;
12892 break;
12893 default:
12894 break;
12895 }
12896 }
12897 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12898 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12899 {
12900 // Second instruction of a local dynamic sequence,
12901 // the __tls_get_addr call
c7fdac09 12902 tls::Tls_optimization tls_type = this->optimize_tls_ld();
0af4fcc2 12903 if (tls_type == tls::TLSOPT_TO_LE)
7404fe1b
AM
12904 {
12905 const Output_section* os = relinfo->layout->tls_segment()
12906 ->first_section();
12907 gold_assert(os != NULL);
12908 gold_assert(os->needs_symtab_index());
12909 r_sym = os->symtab_index();
12910 addend = dtp_offset;
12911 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12912 offset += d_offset;
7404fe1b
AM
12913 zap_next = true;
12914 }
12915 }
12916 else if (r_type == elfcpp::R_POWERPC_TLS)
12917 {
12918 // Second instruction of an initial exec sequence
0af4fcc2 12919 bool final = gsym == NULL || gsym->final_value_is_known();
7404fe1b
AM
12920 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12921 {
12922 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12923 offset += d_offset;
7404fe1b
AM
12924 }
12925 }
12926 }
12927
12928 reloc_write.put_r_offset(offset);
12929 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12930 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12931
12932 pwrite += reloc_size;
12933 }
12934
12935 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12936 == reloc_view_size);
42cacb20
DE
12937}
12938
ec661b9d 12939// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12940// treatment. This is how we support equality comparisons of function
12941// pointers across shared library boundaries, as described in the
12942// processor specific ABI supplement.
12943
12944template<int size, bool big_endian>
12945uint64_t
12946Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12947{
cf43a2fe
AM
12948 if (size == 32)
12949 {
12950 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12951 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12952 p != this->stub_tables_.end();
12953 ++p)
12954 {
7e57d19e
AM
12955 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12956 = (*p)->find_plt_call_entry(gsym);
12957 if (ent != NULL)
12958 return (*p)->stub_address() + ent->off_;
ec661b9d 12959 }
c9824451 12960 }
9055360d
AM
12961 else if (this->abiversion() >= 2)
12962 {
faa2211d
AM
12963 Address off = this->glink_section()->find_global_entry(gsym);
12964 if (off != invalid_address)
9055360d
AM
12965 return this->glink_section()->global_entry_address() + off;
12966 }
ec661b9d 12967 gold_unreachable();
c9824451
AM
12968}
12969
12970// Return the PLT address to use for a local symbol.
12971template<int size, bool big_endian>
12972uint64_t
12973Target_powerpc<size, big_endian>::do_plt_address_for_local(
12974 const Relobj* object,
12975 unsigned int symndx) const
12976{
12977 if (size == 32)
12978 {
12979 const Sized_relobj<size, big_endian>* relobj
12980 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12981 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12982 p != this->stub_tables_.end();
12983 ++p)
12984 {
7e57d19e
AM
12985 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12986 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12987 if (ent != NULL)
12988 return (*p)->stub_address() + ent->off_;
ec661b9d 12989 }
c9824451 12990 }
ec661b9d 12991 gold_unreachable();
c9824451
AM
12992}
12993
12994// Return the PLT address to use for a global symbol.
12995template<int size, bool big_endian>
12996uint64_t
12997Target_powerpc<size, big_endian>::do_plt_address_for_global(
12998 const Symbol* gsym) const
12999{
13000 if (size == 32)
13001 {
ec661b9d
AM
13002 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
13003 p != this->stub_tables_.end();
13004 ++p)
13005 {
7e57d19e
AM
13006 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
13007 = (*p)->find_plt_call_entry(gsym);
13008 if (ent != NULL)
13009 return (*p)->stub_address() + ent->off_;
ec661b9d 13010 }
cf43a2fe 13011 }
9055360d
AM
13012 else if (this->abiversion() >= 2)
13013 {
faa2211d
AM
13014 Address off = this->glink_section()->find_global_entry(gsym);
13015 if (off != invalid_address)
9055360d
AM
13016 return this->glink_section()->global_entry_address() + off;
13017 }
ec661b9d 13018 gold_unreachable();
42cacb20
DE
13019}
13020
bd73a62d
AM
13021// Return the offset to use for the GOT_INDX'th got entry which is
13022// for a local tls symbol specified by OBJECT, SYMNDX.
13023template<int size, bool big_endian>
13024int64_t
13025Target_powerpc<size, big_endian>::do_tls_offset_for_local(
13026 const Relobj* object,
13027 unsigned int symndx,
f19c3684 13028 Output_data_got_base* got,
e4d49a0f
AM
13029 unsigned int got_indx,
13030 uint64_t addend) const
bd73a62d
AM
13031{
13032 const Powerpc_relobj<size, big_endian>* ppc_object
13033 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
13034 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
13035 {
f19c3684
AM
13036 for (Got_type got_type = (size == 32
13037 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13038 got_type <= GOT_TYPE_SMALL_TPREL;
bd73a62d 13039 got_type = Got_type(got_type + 1))
f19c3684
AM
13040 if (got_type != GOT_TYPE_SMALL
13041 && ppc_object->local_has_got_offset(symndx, got_type, addend))
bd73a62d 13042 {
e4d49a0f
AM
13043 unsigned int off
13044 = ppc_object->local_got_offset(symndx, got_type, addend);
f19c3684 13045 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
bd73a62d 13046 off += size / 8;
f19c3684
AM
13047 if (off == got_indx * (size / 8)
13048 && (size == 32 || got == this->got_section(got_type)))
bd73a62d 13049 {
f19c3684 13050 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
bd73a62d
AM
13051 return -tp_offset;
13052 else
13053 return -dtp_offset;
13054 }
13055 }
13056 }
13057 gold_unreachable();
13058}
13059
13060// Return the offset to use for the GOT_INDX'th got entry which is
13061// for global tls symbol GSYM.
13062template<int size, bool big_endian>
13063int64_t
13064Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13065 Symbol* gsym,
f19c3684 13066 Output_data_got_base* got,
e4d49a0f
AM
13067 unsigned int got_indx,
13068 uint64_t addend) const
bd73a62d
AM
13069{
13070 if (gsym->type() == elfcpp::STT_TLS)
13071 {
f19c3684
AM
13072 for (Got_type got_type = (size == 32
13073 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13074 got_type <= GOT_TYPE_SMALL_TPREL;
bd73a62d 13075 got_type = Got_type(got_type + 1))
f19c3684
AM
13076 if (got_type != GOT_TYPE_SMALL
13077 && gsym->has_got_offset(got_type, addend))
bd73a62d 13078 {
e4d49a0f 13079 unsigned int off = gsym->got_offset(got_type, addend);
f19c3684 13080 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
bd73a62d 13081 off += size / 8;
f19c3684
AM
13082 if (off == got_indx * (size / 8)
13083 && (size == 32 || got == this->got_section(got_type)))
bd73a62d 13084 {
f19c3684 13085 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
bd73a62d
AM
13086 return -tp_offset;
13087 else
13088 return -dtp_offset;
13089 }
13090 }
13091 }
13092 gold_unreachable();
13093}
13094
42cacb20
DE
13095// The selector for powerpc object files.
13096
13097template<int size, bool big_endian>
13098class Target_selector_powerpc : public Target_selector
13099{
13100public:
13101 Target_selector_powerpc()
edc27beb
AM
13102 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13103 size, big_endian,
03ef7571
ILT
13104 (size == 64
13105 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13106 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13107 (size == 64
13108 ? (big_endian ? "elf64ppc" : "elf64lppc")
13109 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
13110 { }
13111
2e702c99
RM
13112 virtual Target*
13113 do_instantiate_target()
7f055c20 13114 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
13115};
13116
13117Target_selector_powerpc<32, true> target_selector_ppc32;
13118Target_selector_powerpc<32, false> target_selector_ppc32le;
13119Target_selector_powerpc<64, true> target_selector_ppc64;
13120Target_selector_powerpc<64, false> target_selector_ppc64le;
13121
decdd3bc
AM
13122// Instantiate these constants for -O0
13123template<int size, bool big_endian>
9055360d
AM
13124const typename Output_data_glink<size, big_endian>::Address
13125 Output_data_glink<size, big_endian>::invalid_address;
13126template<int size, bool big_endian>
decdd3bc
AM
13127const typename Stub_table<size, big_endian>::Address
13128 Stub_table<size, big_endian>::invalid_address;
13129template<int size, bool big_endian>
13130const typename Target_powerpc<size, big_endian>::Address
13131 Target_powerpc<size, big_endian>::invalid_address;
13132
42cacb20 13133} // End anonymous namespace.