]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/powerpc.cc
PowerPC TPREL_HA/LO optimisation
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
b3adc24a 3// Copyright (C) 2008-2020 Free Software Foundation, Inc.
42cacb20
DE
4// Written by David S. Miller <davem@davemloft.net>
5// and David Edelsohn <edelsohn@gnu.org>
6
7// This file is part of gold.
8
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 3 of the License, or
12// (at your option) any later version.
13
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22// MA 02110-1301, USA.
23
24#include "gold.h"
25
dc3714f3 26#include <set>
ec661b9d 27#include <algorithm>
42cacb20 28#include "elfcpp.h"
9d5781f8 29#include "dwarf.h"
42cacb20
DE
30#include "parameters.h"
31#include "reloc.h"
32#include "powerpc.h"
33#include "object.h"
34#include "symtab.h"
35#include "layout.h"
36#include "output.h"
37#include "copy-relocs.h"
38#include "target.h"
39#include "target-reloc.h"
40#include "target-select.h"
41#include "tls.h"
42#include "errors.h"
f345227a 43#include "gc.h"
724436fc 44#include "attributes.h"
42cacb20
DE
45
46namespace
47{
48
49using namespace gold;
50
51template<int size, bool big_endian>
52class Output_data_plt_powerpc;
53
ec661b9d
AM
54template<int size, bool big_endian>
55class Output_data_brlt_powerpc;
56
cf43a2fe
AM
57template<int size, bool big_endian>
58class Output_data_got_powerpc;
59
60template<int size, bool big_endian>
61class Output_data_glink;
62
ec661b9d
AM
63template<int size, bool big_endian>
64class Stub_table;
65
d49044c7
AM
66template<int size, bool big_endian>
67class Output_data_save_res;
68
a3e60ddb
AM
69template<int size, bool big_endian>
70class Target_powerpc;
71
72struct Stub_table_owner
73{
dc60b26d
AM
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
a3e60ddb
AM
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80};
81
32f59844 82template<int size>
23cedd1d
AM
83inline bool is_branch_reloc(unsigned int);
84
85template<int size>
86inline bool is_plt16_reloc(unsigned int);
4d9aa155 87
590b87ff
AM
88// Counter incremented on every Powerpc_relobj constructed.
89static uint32_t object_id = 0;
90
cf43a2fe
AM
91template<int size, bool big_endian>
92class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93{
94public:
dd93cd0a 95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 98
cf43a2fe
AM
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
590b87ff
AM
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
724436fc
AM
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
b4f7960d
AM
107 {
108 this->set_abiversion(0);
109 }
cf43a2fe
AM
110
111 ~Powerpc_relobj()
724436fc 112 { delete this->attributes_section_data_; }
cf43a2fe 113
b4f7960d
AM
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
5edad15d
AM
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
c9269dff 164 // The .got2 section shndx.
cf43a2fe
AM
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
c9269dff 169 return this->special_;
cf43a2fe
AM
170 else
171 return 0;
172 }
173
c9269dff
AM
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 189 this->opd_ent_.resize(count);
c9269dff
AM
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 199 if (value != NULL)
bfdfa4cd
AM
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
c9269dff
AM
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
dd93cd0a 206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
c9269dff
AM
230 }
231
e81fea4d
AM
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
c9269dff
AM
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
5edad15d
AM
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
c9b8abb7
AM
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
257
26a4e9cb
AM
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
c9269dff
AM
260 void
261 do_read_relocs(Read_relocs_data*);
262
cf43a2fe
AM
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
265
ec4dbad3
AM
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
270 {
271 if (size == 64 && this->opd_shndx() != 0)
272 {
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
278 }
279 return true;
280 }
281
6c77229c
AM
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
285
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
efc6fa12 289 add_reference(Relobj* src_obj,
6c77229c
AM
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292 {
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
295 }
296
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301 {
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
306 }
307
308 void
309 process_gc_mark(Symbol_table* symtab)
310 {
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
313 {
314 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
316 }
317 }
318
dd93cd0a
AM
319 // Return offset in output GOT section that this object will use
320 // as a TOC pointer. Won't be just a constant with multi-toc support.
321 Address
322 toc_base_offset() const
323 { return 0x8000; }
324
d8f5a274
AM
325 void
326 set_has_small_toc_reloc()
327 { has_small_toc_reloc_ = true; }
328
329 bool
330 has_small_toc_reloc() const
331 { return has_small_toc_reloc_; }
332
ec661b9d
AM
333 void
334 set_has_14bit_branch(unsigned int shndx)
335 {
336 if (shndx >= this->has14_.size())
337 this->has14_.resize(shndx + 1);
338 this->has14_[shndx] = true;
339 }
340
341 bool
342 has_14bit_branch(unsigned int shndx) const
343 { return shndx < this->has14_.size() && this->has14_[shndx]; }
344
345 void
a3e60ddb 346 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 347 {
a3e60ddb 348 if (shndx >= this->stub_table_index_.size())
dc60b26d 349 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 350 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
351 }
352
353 Stub_table<size, big_endian>*
354 stub_table(unsigned int shndx)
355 {
a3e60ddb
AM
356 if (shndx < this->stub_table_index_.size())
357 {
358 Target_powerpc<size, big_endian>* target
359 = static_cast<Target_powerpc<size, big_endian>*>(
360 parameters->sized_target<size, big_endian>());
361 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
362 if (indx < target->stub_tables().size())
363 return target->stub_tables()[indx];
a3e60ddb 364 }
ec661b9d
AM
365 return NULL;
366 }
367
a3e60ddb
AM
368 void
369 clear_stub_table()
370 {
371 this->stub_table_index_.clear();
372 }
373
590b87ff
AM
374 uint32_t
375 uniq() const
376 { return this->uniq_; }
377
b4f7960d
AM
378 int
379 abiversion() const
380 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
381
382 // Set ABI version for input and output
383 void
384 set_abiversion(int ver);
385
7ee7ff70
AM
386 unsigned int
387 st_other (unsigned int symndx) const
388 {
389 return this->st_other_[symndx];
390 }
391
b4f7960d
AM
392 unsigned int
393 ppc64_local_entry_offset(const Symbol* sym) const
394 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
395
396 unsigned int
397 ppc64_local_entry_offset(unsigned int symndx) const
398 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
399
32f59844
AM
400 bool
401 ppc64_needs_toc(const Symbol* sym) const
402 { return sym->nonvis() > 1 << 3; }
403
404 bool
405 ppc64_needs_toc(unsigned int symndx) const
406 { return this->st_other_[symndx] > 1 << 5; }
407
724436fc
AM
408 // The contents of the .gnu.attributes section if there is one.
409 const Attributes_section_data*
410 attributes_section_data() const
411 { return this->attributes_section_data_; }
412
cf43a2fe 413private:
bfdfa4cd
AM
414 struct Opd_ent
415 {
416 unsigned int shndx;
c6de8ed4
AM
417 bool discard : 1;
418 bool gc_mark : 1;
26a4e9cb 419 Address off;
bfdfa4cd
AM
420 };
421
422 // Return index into opd_ent_ array for .opd entry at OFF.
423 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
424 // apart when the language doesn't use the last 8-byte word, the
425 // environment pointer. Thus dividing the entry section offset by
426 // 16 will give an index into opd_ent_ that works for either layout
427 // of .opd. (It leaves some elements of the vector unused when .opd
428 // entries are spaced 24 bytes apart, but we don't know the spacing
429 // until relocations are processed, and in any case it is possible
430 // for an object to have some entries spaced 16 bytes apart and
431 // others 24 bytes apart.)
c9269dff
AM
432 size_t
433 opd_ent_ndx(size_t off) const
434 { return off >> 4;}
435
590b87ff
AM
436 // Per object unique identifier
437 uint32_t uniq_;
438
c9269dff
AM
439 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
440 unsigned int special_;
bfdfa4cd 441
5edad15d
AM
442 // For 64-bit the .rela.toc and .toc section shdnx.
443 unsigned int relatoc_;
444 unsigned int toc_;
445
d8f5a274
AM
446 // For 64-bit, whether this object uses small model relocs to access
447 // the toc.
448 bool has_small_toc_reloc_;
449
bfdfa4cd
AM
450 // Set at the start of gc_process_relocs, when we know opd_ent_
451 // vector is valid. The flag could be made atomic and set in
452 // do_read_relocs with memory_order_release and then tested with
453 // memory_order_acquire, potentially resulting in fewer entries in
454 // access_from_map_.
455 bool opd_valid_;
456
590b87ff
AM
457 // Header e_flags
458 elfcpp::Elf_Word e_flags_;
459
460 // For 64-bit, an array with one entry per 64-bit word in the .toc
461 // section, set if accesses using that word cannot be optimised.
462 std::vector<bool> no_toc_opt_;
463
c9269dff
AM
464 // The first 8-byte word of an OPD entry gives the address of the
465 // entry point of the function. Relocatable object files have a
bfdfa4cd 466 // relocation on this word. The following vector records the
c9269dff 467 // section and offset specified by these relocations.
bfdfa4cd
AM
468 std::vector<Opd_ent> opd_ent_;
469
e81fea4d 470 // References made to this object's .opd section when running
bfdfa4cd
AM
471 // gc_process_relocs for another object, before the opd_ent_ vector
472 // is valid for this object.
e81fea4d 473 Access_from access_from_map_;
ec661b9d
AM
474
475 // Whether input section has a 14-bit branch reloc.
476 std::vector<bool> has14_;
477
478 // The stub table to use for a given input section.
a3e60ddb 479 std::vector<unsigned int> stub_table_index_;
b4f7960d 480
b4f7960d
AM
481 // ELF st_other field for local symbols.
482 std::vector<unsigned char> st_other_;
724436fc
AM
483
484 // Object attributes if there is a .gnu.attributes section.
485 Attributes_section_data* attributes_section_data_;
cf43a2fe
AM
486};
487
dc3714f3
AM
488template<int size, bool big_endian>
489class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
490{
491public:
492 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
493
494 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
495 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
496 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
724436fc
AM
497 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
498 attributes_section_data_(NULL)
b4f7960d
AM
499 {
500 this->set_abiversion(0);
501 }
dc3714f3
AM
502
503 ~Powerpc_dynobj()
724436fc 504 { delete this->attributes_section_data_; }
dc3714f3
AM
505
506 // Call Sized_dynobj::do_read_symbols to read the symbols then
507 // read .opd from a dynamic object, filling in opd_ent_ vector,
508 void
509 do_read_symbols(Read_symbols_data*);
510
511 // The .opd section shndx.
512 unsigned int
513 opd_shndx() const
514 {
515 return this->opd_shndx_;
516 }
517
518 // The .opd section address.
519 Address
520 opd_address() const
521 {
522 return this->opd_address_;
523 }
524
525 // Init OPD entry arrays.
526 void
527 init_opd(size_t opd_size)
528 {
529 size_t count = this->opd_ent_ndx(opd_size);
530 this->opd_ent_.resize(count);
531 }
532
533 // Return section and offset of function entry for .opd + R_OFF.
534 unsigned int
535 get_opd_ent(Address r_off, Address* value = NULL) const
536 {
537 size_t ndx = this->opd_ent_ndx(r_off);
538 gold_assert(ndx < this->opd_ent_.size());
539 gold_assert(this->opd_ent_[ndx].shndx != 0);
540 if (value != NULL)
541 *value = this->opd_ent_[ndx].off;
542 return this->opd_ent_[ndx].shndx;
543 }
544
545 // Set section and offset of function entry for .opd + R_OFF.
546 void
547 set_opd_ent(Address r_off, unsigned int shndx, Address value)
548 {
549 size_t ndx = this->opd_ent_ndx(r_off);
550 gold_assert(ndx < this->opd_ent_.size());
551 this->opd_ent_[ndx].shndx = shndx;
552 this->opd_ent_[ndx].off = value;
553 }
554
b4f7960d
AM
555 int
556 abiversion() const
557 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
558
559 // Set ABI version for input and output.
560 void
561 set_abiversion(int ver);
562
724436fc
AM
563 // The contents of the .gnu.attributes section if there is one.
564 const Attributes_section_data*
565 attributes_section_data() const
566 { return this->attributes_section_data_; }
567
dc3714f3
AM
568private:
569 // Used to specify extent of executable sections.
570 struct Sec_info
571 {
572 Sec_info(Address start_, Address len_, unsigned int shndx_)
573 : start(start_), len(len_), shndx(shndx_)
574 { }
575
576 bool
577 operator<(const Sec_info& that) const
578 { return this->start < that.start; }
579
580 Address start;
581 Address len;
582 unsigned int shndx;
583 };
584
585 struct Opd_ent
586 {
587 unsigned int shndx;
588 Address off;
589 };
590
591 // Return index into opd_ent_ array for .opd entry at OFF.
592 size_t
593 opd_ent_ndx(size_t off) const
594 { return off >> 4;}
595
596 // For 64-bit the .opd section shndx and address.
597 unsigned int opd_shndx_;
598 Address opd_address_;
599
590b87ff
AM
600 // Header e_flags
601 elfcpp::Elf_Word e_flags_;
602
dc3714f3
AM
603 // The first 8-byte word of an OPD entry gives the address of the
604 // entry point of the function. Records the section and offset
605 // corresponding to the address. Note that in dynamic objects,
606 // offset is *not* relative to the section.
607 std::vector<Opd_ent> opd_ent_;
724436fc
AM
608
609 // Object attributes if there is a .gnu.attributes section.
610 Attributes_section_data* attributes_section_data_;
dc3714f3
AM
611};
612
5edad15d
AM
613// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
614// base class will emit.
615
616template<int sh_type, int size, bool big_endian>
617class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
618{
619 public:
620 Powerpc_copy_relocs()
621 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
622 { }
623
624 // Emit any saved relocations which turn out to be needed. This is
625 // called after all the relocs have been scanned.
626 void
627 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
628};
629
42cacb20
DE
630template<int size, bool big_endian>
631class Target_powerpc : public Sized_target<size, big_endian>
632{
633 public:
d83ce4e3
AM
634 typedef
635 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 636 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 637 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 638 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 639 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
640 // Offset of tp and dtp pointers from start of TLS block.
641 static const Address tp_offset = 0x7000;
642 static const Address dtp_offset = 0x8000;
42cacb20
DE
643
644 Target_powerpc()
645 : Sized_target<size, big_endian>(&powerpc_info),
2d7ad24e 646 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
5edad15d 647 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 648 tlsld_got_offset_(-1U),
7e57d19e 649 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
e4dff765 650 powerxx_stubs_(false), plt_thread_safe_(false), plt_localentry0_(false),
7ee7ff70 651 plt_localentry0_init_(false), has_localentry0_(false),
34e0882b 652 has_tls_get_addr_opt_(false),
93b9bf16 653 tprel_opt_(parameters->options().tls_optimize()),
7ee7ff70 654 relax_failed_(false), relax_fail_count_(0),
34e0882b 655 stub_group_size_(0), savres_section_(0),
724436fc
AM
656 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
657 attributes_section_data_(NULL),
658 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
42cacb20
DE
659 {
660 }
661
2e702c99 662 // Process the relocations to determine unreferenced sections for
6d03d481
ST
663 // garbage collection.
664 void
ad0f2072 665 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
666 Layout* layout,
667 Sized_relobj_file<size, big_endian>* object,
668 unsigned int data_shndx,
669 unsigned int sh_type,
670 const unsigned char* prelocs,
671 size_t reloc_count,
672 Output_section* output_section,
673 bool needs_special_offset_handling,
674 size_t local_symbol_count,
675 const unsigned char* plocal_symbols);
6d03d481 676
42cacb20
DE
677 // Scan the relocations to look for symbol adjustments.
678 void
ad0f2072 679 scan_relocs(Symbol_table* symtab,
42cacb20 680 Layout* layout,
6fa2a40b 681 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
682 unsigned int data_shndx,
683 unsigned int sh_type,
684 const unsigned char* prelocs,
685 size_t reloc_count,
686 Output_section* output_section,
687 bool needs_special_offset_handling,
688 size_t local_symbol_count,
689 const unsigned char* plocal_symbols);
921b5322
AM
690
691 // Map input .toc section to output .got section.
692 const char*
693 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
694 {
695 if (size == 64 && strcmp(name, ".toc") == 0)
696 {
697 *plen = 4;
698 return ".got";
699 }
700 return NULL;
701 }
702
f3a0ed29
AM
703 // Provide linker defined save/restore functions.
704 void
705 define_save_restore_funcs(Layout*, Symbol_table*);
706
ec661b9d
AM
707 // No stubs unless a final link.
708 bool
709 do_may_relax() const
710 { return !parameters->options().relocatable(); }
711
712 bool
713 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
714
9d5781f8
AM
715 void
716 do_plt_fde_location(const Output_data*, unsigned char*,
717 uint64_t*, off_t*) const;
718
ec661b9d
AM
719 // Stash info about branches, for stub generation.
720 void
721 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
722 unsigned int data_shndx, Address r_offset,
723 unsigned int r_type, unsigned int r_sym, Address addend)
724 {
725 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
726 this->branch_info_.push_back(info);
727 if (r_type == elfcpp::R_POWERPC_REL14
728 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
729 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
730 ppc_object->set_has_14bit_branch(data_shndx);
731 }
732
7e57d19e
AM
733 // Return whether the last branch is a plt call, and if so, mark the
734 // branch as having an R_PPC64_TOCSAVE.
735 bool
736 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
737 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
738 {
739 return (size == 64
740 && !this->branch_info_.empty()
741 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
742 r_offset, this, symtab));
743 }
744
745 // Say the given location, that of a nop in a function prologue with
746 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
747 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
748 void
749 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
750 unsigned int shndx, Address offset)
751 {
752 Symbol_location loc;
753 loc.object = ppc_object;
754 loc.shndx = shndx;
755 loc.offset = offset;
756 this->tocsave_loc_.insert(loc);
757 }
758
759 // Accessor
760 const Tocsave_loc
761 tocsave_loc() const
762 {
763 return this->tocsave_loc_;
764 }
765
f43ba157
AM
766 void
767 do_define_standard_symbols(Symbol_table*, Layout*);
768
42cacb20
DE
769 // Finalize the sections.
770 void
f59f41f3 771 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
772
773 // Return the value to use for a dynamic which requires special
774 // treatment.
775 uint64_t
776 do_dynsym_value(const Symbol*) const;
777
c9824451
AM
778 // Return the PLT address to use for a local symbol.
779 uint64_t
780 do_plt_address_for_local(const Relobj*, unsigned int) const;
781
782 // Return the PLT address to use for a global symbol.
783 uint64_t
784 do_plt_address_for_global(const Symbol*) const;
785
bd73a62d
AM
786 // Return the offset to use for the GOT_INDX'th got entry which is
787 // for a local tls symbol specified by OBJECT, SYMNDX.
788 int64_t
789 do_tls_offset_for_local(const Relobj* object,
790 unsigned int symndx,
791 unsigned int got_indx) const;
792
793 // Return the offset to use for the GOT_INDX'th got entry which is
794 // for global tls symbol GSYM.
795 int64_t
796 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
797
dc3714f3
AM
798 void
799 do_function_location(Symbol_location*) const;
800
4d9aa155
AM
801 bool
802 do_can_check_for_function_pointers() const
803 { return true; }
804
bbec1a5d
AM
805 // Adjust -fsplit-stack code which calls non-split-stack code.
806 void
807 do_calls_non_split(Relobj* object, unsigned int shndx,
808 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 809 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
810 unsigned char* view, section_size_type view_size,
811 std::string* from, std::string* to) const;
812
42cacb20
DE
813 // Relocate a section.
814 void
815 relocate_section(const Relocate_info<size, big_endian>*,
816 unsigned int sh_type,
817 const unsigned char* prelocs,
818 size_t reloc_count,
819 Output_section* output_section,
820 bool needs_special_offset_handling,
821 unsigned char* view,
c9269dff 822 Address view_address,
364c7fa5
ILT
823 section_size_type view_size,
824 const Reloc_symbol_changes*);
42cacb20
DE
825
826 // Scan the relocs during a relocatable link.
827 void
ad0f2072 828 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 829 Layout* layout,
6fa2a40b 830 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
831 unsigned int data_shndx,
832 unsigned int sh_type,
833 const unsigned char* prelocs,
834 size_t reloc_count,
835 Output_section* output_section,
836 bool needs_special_offset_handling,
837 size_t local_symbol_count,
838 const unsigned char* plocal_symbols,
839 Relocatable_relocs*);
840
4d625b70
CC
841 // Scan the relocs for --emit-relocs.
842 void
843 emit_relocs_scan(Symbol_table* symtab,
844 Layout* layout,
845 Sized_relobj_file<size, big_endian>* object,
846 unsigned int data_shndx,
847 unsigned int sh_type,
848 const unsigned char* prelocs,
849 size_t reloc_count,
850 Output_section* output_section,
851 bool needs_special_offset_handling,
852 size_t local_symbol_count,
853 const unsigned char* plocal_syms,
854 Relocatable_relocs* rr);
855
7404fe1b 856 // Emit relocations for a section.
42cacb20 857 void
7404fe1b
AM
858 relocate_relocs(const Relocate_info<size, big_endian>*,
859 unsigned int sh_type,
860 const unsigned char* prelocs,
861 size_t reloc_count,
862 Output_section* output_section,
62fe925a
RM
863 typename elfcpp::Elf_types<size>::Elf_Off
864 offset_in_output_section,
7404fe1b
AM
865 unsigned char*,
866 Address view_address,
867 section_size_type,
868 unsigned char* reloc_view,
869 section_size_type reloc_view_size);
42cacb20
DE
870
871 // Return whether SYM is defined by the ABI.
872 bool
9c2d0ef9 873 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 874 {
cf43a2fe 875 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
876 }
877
878 // Return the size of the GOT section.
879 section_size_type
0e70b911 880 got_size() const
42cacb20
DE
881 {
882 gold_assert(this->got_ != NULL);
883 return this->got_->data_size();
884 }
885
cf43a2fe
AM
886 // Get the PLT section.
887 const Output_data_plt_powerpc<size, big_endian>*
888 plt_section() const
889 {
890 gold_assert(this->plt_ != NULL);
891 return this->plt_;
892 }
893
e5d5f5ed
AM
894 // Get the IPLT section.
895 const Output_data_plt_powerpc<size, big_endian>*
896 iplt_section() const
897 {
898 gold_assert(this->iplt_ != NULL);
899 return this->iplt_;
900 }
901
2d7ad24e
AM
902 // Get the LPLT section.
903 const Output_data_plt_powerpc<size, big_endian>*
904 lplt_section() const
905 {
906 return this->lplt_;
907 }
908
08be3224
AM
909 // Return the plt offset and section for the given global sym.
910 Address
911 plt_off(const Symbol* gsym,
912 const Output_data_plt_powerpc<size, big_endian>** sec) const
913 {
914 if (gsym->type() == elfcpp::STT_GNU_IFUNC
915 && gsym->can_use_relative_reloc(false))
916 *sec = this->iplt_section();
917 else
918 *sec = this->plt_section();
919 return gsym->plt_offset();
920 }
921
922 // Return the plt offset and section for the given local sym.
923 Address
924 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
925 unsigned int local_sym_index,
926 const Output_data_plt_powerpc<size, big_endian>** sec) const
927 {
2d7ad24e
AM
928 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
929 if (lsym->is_ifunc_symbol())
930 *sec = this->iplt_section();
931 else
932 *sec = this->lplt_section();
08be3224
AM
933 return relobj->local_plt_offset(local_sym_index);
934 }
935
cf43a2fe
AM
936 // Get the .glink section.
937 const Output_data_glink<size, big_endian>*
938 glink_section() const
939 {
940 gold_assert(this->glink_ != NULL);
941 return this->glink_;
942 }
943
9055360d
AM
944 Output_data_glink<size, big_endian>*
945 glink_section()
946 {
947 gold_assert(this->glink_ != NULL);
948 return this->glink_;
949 }
950
9d5781f8
AM
951 bool has_glink() const
952 { return this->glink_ != NULL; }
953
cf43a2fe
AM
954 // Get the GOT section.
955 const Output_data_got_powerpc<size, big_endian>*
956 got_section() const
957 {
958 gold_assert(this->got_ != NULL);
959 return this->got_;
960 }
961
26a4e9cb
AM
962 // Get the GOT section, creating it if necessary.
963 Output_data_got_powerpc<size, big_endian>*
964 got_section(Symbol_table*, Layout*);
965
cf43a2fe
AM
966 Object*
967 do_make_elf_object(const std::string&, Input_file*, off_t,
968 const elfcpp::Ehdr<size, big_endian>&);
969
0e70b911
CC
970 // Return the number of entries in the GOT.
971 unsigned int
972 got_entry_count() const
973 {
974 if (this->got_ == NULL)
975 return 0;
976 return this->got_size() / (size / 8);
977 }
978
979 // Return the number of entries in the PLT.
980 unsigned int
981 plt_entry_count() const;
982
983 // Return the offset of the first non-reserved PLT entry.
984 unsigned int
b4f7960d
AM
985 first_plt_entry_offset() const
986 {
987 if (size == 32)
988 return 0;
989 if (this->abiversion() >= 2)
990 return 16;
991 return 24;
992 }
0e70b911
CC
993
994 // Return the size of each PLT entry.
995 unsigned int
b4f7960d
AM
996 plt_entry_size() const
997 {
998 if (size == 32)
999 return 4;
1000 if (this->abiversion() >= 2)
1001 return 8;
1002 return 24;
1003 }
0e70b911 1004
d49044c7
AM
1005 Output_data_save_res<size, big_endian>*
1006 savres_section() const
1007 {
1008 return this->savres_section_;
1009 }
1010
e81fea4d
AM
1011 // Add any special sections for this symbol to the gc work list.
1012 // For powerpc64, this adds the code section of a function
1013 // descriptor.
1014 void
1015 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1016
1017 // Handle target specific gc actions when adding a gc reference from
1018 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1019 // and DST_OFF. For powerpc64, this adds a referenc to the code
1020 // section of a function descriptor.
1021 void
1022 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 1023 Relobj* src_obj,
e81fea4d 1024 unsigned int src_shndx,
efc6fa12 1025 Relobj* dst_obj,
e81fea4d
AM
1026 unsigned int dst_shndx,
1027 Address dst_off) const;
1028
ec661b9d
AM
1029 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1030 const Stub_tables&
1031 stub_tables() const
1032 { return this->stub_tables_; }
1033
1034 const Output_data_brlt_powerpc<size, big_endian>*
1035 brlt_section() const
1036 { return this->brlt_section_; }
1037
1038 void
1039 add_branch_lookup_table(Address to)
1040 {
1041 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1042 this->branch_lookup_table_.insert(std::make_pair(to, off));
1043 }
1044
1045 Address
1046 find_branch_lookup_table(Address to)
1047 {
1048 typename Branch_lookup_table::const_iterator p
1049 = this->branch_lookup_table_.find(to);
1050 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1051 }
1052
1053 void
1054 write_branch_lookup_table(unsigned char *oview)
1055 {
1056 for (typename Branch_lookup_table::const_iterator p
1057 = this->branch_lookup_table_.begin();
1058 p != this->branch_lookup_table_.end();
1059 ++p)
1060 {
4d5effb9 1061 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
1062 }
1063 }
1064
590b87ff
AM
1065 // Wrapper used after relax to define a local symbol in output data,
1066 // from the end if value < 0.
1067 void
1068 define_local(Symbol_table* symtab, const char* name,
1069 Output_data* od, Address value, unsigned int symsize)
1070 {
1071 Symbol* sym
1072 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1073 od, value, symsize, elfcpp::STT_NOTYPE,
1074 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1075 static_cast<Signed_address>(value) < 0,
1076 false);
1077 // We are creating this symbol late, so need to fix up things
1078 // done early in Layout::finalize.
1079 sym->set_dynsym_index(-1U);
1080 }
1081
e4dff765
AM
1082 bool
1083 powerxx_stubs() const
1084 { return this->powerxx_stubs_; }
1085
1086 void
1087 set_powerxx_stubs()
1088 {
1089 this->powerxx_stubs_ = true;
1090 }
1091
9e69ed50
AM
1092 bool
1093 plt_thread_safe() const
1094 { return this->plt_thread_safe_; }
1095
7ee7ff70
AM
1096 bool
1097 plt_localentry0() const
1098 { return this->plt_localentry0_; }
1099
1100 void
1101 set_has_localentry0()
1102 {
1103 this->has_localentry0_ = true;
1104 }
1105
1106 bool
1107 is_elfv2_localentry0(const Symbol* gsym) const
1108 {
1109 return (size == 64
1110 && this->abiversion() >= 2
1111 && this->plt_localentry0()
1112 && gsym->type() == elfcpp::STT_FUNC
1113 && gsym->is_defined()
565ed01a
AM
1114 && gsym->nonvis() >> 3 == 0
1115 && !gsym->non_zero_localentry());
7ee7ff70
AM
1116 }
1117
1118 bool
1119 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1120 unsigned int r_sym) const
1121 {
1122 const Powerpc_relobj<size, big_endian>* ppc_object
1123 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1124
1125 if (size == 64
1126 && this->abiversion() >= 2
1127 && this->plt_localentry0()
1128 && ppc_object->st_other(r_sym) >> 5 == 0)
1129 {
1130 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1131 bool is_ordinary;
1132 if (!psymval->is_ifunc_symbol()
1133 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1134 && is_ordinary)
1135 return true;
1136 }
1137 return false;
1138 }
1139
93b9bf16
AM
1140 bool
1141 tprel_opt() const
1142 { return this->tprel_opt_; }
1143
1144 void
1145 set_tprel_opt(bool val)
1146 { this->tprel_opt_ = val; }
1147
565ed01a
AM
1148 // Remember any symbols seen with non-zero localentry, even those
1149 // not providing a definition
1150 bool
1151 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1152 const char*)
1153 {
1154 if (size == 64)
1155 {
1156 unsigned char st_other = sym.get_st_other();
1157 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1158 to->set_non_zero_localentry();
1159 }
1160 // We haven't resolved anything, continue normal processing.
1161 return false;
1162 }
1163
b4f7960d 1164 int
aacb3b6d 1165 abiversion() const
b4f7960d
AM
1166 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1167
1168 void
aacb3b6d 1169 set_abiversion(int ver)
b4f7960d
AM
1170 {
1171 elfcpp::Elf_Word flags = this->processor_specific_flags();
1172 flags &= ~elfcpp::EF_PPC64_ABI;
1173 flags |= ver & elfcpp::EF_PPC64_ABI;
1174 this->set_processor_specific_flags(flags);
1175 }
1176
34e0882b
AM
1177 Symbol*
1178 tls_get_addr_opt() const
1179 { return this->tls_get_addr_opt_; }
1180
1181 Symbol*
1182 tls_get_addr() const
1183 { return this->tls_get_addr_; }
1184
1185 // If optimizing __tls_get_addr calls, whether this is the
1186 // "__tls_get_addr" symbol.
1187 bool
1188 is_tls_get_addr_opt(const Symbol* gsym) const
1189 {
1190 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1191 || gsym == this->tls_get_addr_opt_);
1192 }
1193
1194 bool
1195 replace_tls_get_addr(const Symbol* gsym) const
1196 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1197
1198 void
1199 set_has_tls_get_addr_opt()
1200 { this->has_tls_get_addr_opt_ = true; }
1201
aacb3b6d 1202 // Offset to toc save stack slot
b4f7960d 1203 int
aacb3b6d 1204 stk_toc() const
b4f7960d
AM
1205 { return this->abiversion() < 2 ? 40 : 24; }
1206
34e0882b
AM
1207 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1208 // so use the CR save slot. Used only by __tls_get_addr call stub,
1209 // relying on __tls_get_addr not saving CR itself.
1210 int
1211 stk_linker() const
1212 { return this->abiversion() < 2 ? 32 : 8; }
1213
724436fc
AM
1214 // Merge object attributes from input object with those in the output.
1215 void
1216 merge_object_attributes(const char*, const Attributes_section_data*);
1217
42cacb20
DE
1218 private:
1219
e3deeb9c
AM
1220 class Track_tls
1221 {
1222 public:
1223 enum Tls_get_addr
1224 {
1225 NOT_EXPECTED = 0,
1226 EXPECTED = 1,
1227 SKIP = 2,
1228 NORMAL = 3
1229 };
1230
1231 Track_tls()
aacb3b6d 1232 : tls_get_addr_state_(NOT_EXPECTED),
e3deeb9c
AM
1233 relinfo_(NULL), relnum_(0), r_offset_(0)
1234 { }
1235
1236 ~Track_tls()
1237 {
aacb3b6d 1238 if (this->tls_get_addr_state_ != NOT_EXPECTED)
e3deeb9c
AM
1239 this->missing();
1240 }
1241
1242 void
1243 missing(void)
1244 {
1245 if (this->relinfo_ != NULL)
1246 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1247 _("missing expected __tls_get_addr call"));
1248 }
1249
1250 void
1251 expect_tls_get_addr_call(
1252 const Relocate_info<size, big_endian>* relinfo,
1253 size_t relnum,
1254 Address r_offset)
1255 {
aacb3b6d 1256 this->tls_get_addr_state_ = EXPECTED;
e3deeb9c
AM
1257 this->relinfo_ = relinfo;
1258 this->relnum_ = relnum;
1259 this->r_offset_ = r_offset;
1260 }
1261
1262 void
1263 expect_tls_get_addr_call()
aacb3b6d 1264 { this->tls_get_addr_state_ = EXPECTED; }
e3deeb9c
AM
1265
1266 void
1267 skip_next_tls_get_addr_call()
aacb3b6d 1268 {this->tls_get_addr_state_ = SKIP; }
e3deeb9c
AM
1269
1270 Tls_get_addr
34e0882b
AM
1271 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1272 unsigned int r_type, const Symbol* gsym)
e3deeb9c 1273 {
32f59844
AM
1274 bool is_tls_call
1275 = ((r_type == elfcpp::R_POWERPC_REL24
1276 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1277 || r_type == elfcpp::R_PPC_PLTREL24
1278 || is_plt16_reloc<size>(r_type)
e4dff765
AM
1279 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1280 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
32f59844
AM
1281 || r_type == elfcpp::R_POWERPC_PLTSEQ
1282 || r_type == elfcpp::R_POWERPC_PLTCALL
1283 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1284 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1285 && gsym != NULL
1286 && (gsym == target->tls_get_addr()
1287 || gsym == target->tls_get_addr_opt()));
aacb3b6d
AM
1288 Tls_get_addr last_tls = this->tls_get_addr_state_;
1289 this->tls_get_addr_state_ = NOT_EXPECTED;
e3deeb9c
AM
1290 if (is_tls_call && last_tls != EXPECTED)
1291 return last_tls;
1292 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1293 {
1294 this->missing();
1295 return EXPECTED;
1296 }
1297 return NORMAL;
1298 }
1299
1300 private:
1301 // What we're up to regarding calls to __tls_get_addr.
1302 // On powerpc, the branch and link insn making a call to
1303 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1304 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
32f59844 1305 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
e3deeb9c
AM
1306 // The marker relocation always comes first, and has the same
1307 // symbol as the reloc on the insn setting up the __tls_get_addr
1308 // argument. This ties the arg setup insn with the call insn,
1309 // allowing ld to safely optimize away the call. We check that
1310 // every call to __tls_get_addr has a marker relocation, and that
1311 // every marker relocation is on a call to __tls_get_addr.
aacb3b6d 1312 Tls_get_addr tls_get_addr_state_;
e3deeb9c
AM
1313 // Info about the last reloc for error message.
1314 const Relocate_info<size, big_endian>* relinfo_;
1315 size_t relnum_;
1316 Address r_offset_;
1317 };
1318
42cacb20 1319 // The class which scans relocations.
e3deeb9c 1320 class Scan : protected Track_tls
42cacb20
DE
1321 {
1322 public:
bfdfa4cd
AM
1323 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1324
42cacb20 1325 Scan()
e3deeb9c 1326 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1327 { }
1328
95a2c8d6 1329 static inline int
88b8e639 1330 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1331
42cacb20 1332 inline void
ad0f2072 1333 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1334 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1335 unsigned int data_shndx,
1336 Output_section* output_section,
1337 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1338 const elfcpp::Sym<size, big_endian>& lsym,
1339 bool is_discarded);
42cacb20
DE
1340
1341 inline void
ad0f2072 1342 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1343 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1344 unsigned int data_shndx,
1345 Output_section* output_section,
1346 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1347 Symbol* gsym);
1348
21bb3914
ST
1349 inline bool
1350 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1351 Target_powerpc* ,
f6971787 1352 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1353 unsigned int ,
2e702c99
RM
1354 Output_section* ,
1355 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1356 unsigned int r_type,
2e702c99 1357 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1358 {
1359 // PowerPC64 .opd is not folded, so any identical function text
1360 // may be folded and we'll still keep function addresses distinct.
1361 // That means no reloc is of concern here.
1362 if (size == 64)
f6971787
AM
1363 {
1364 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1365 <Powerpc_relobj<size, big_endian>*>(relobj);
1366 if (ppcobj->abiversion() == 1)
1367 return false;
1368 }
1369 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155 1370 // function code might be taking the address of the function.
32f59844 1371 return !is_branch_reloc<size>(r_type);
4d9aa155 1372 }
21bb3914
ST
1373
1374 inline bool
1375 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1376 Target_powerpc* ,
f6971787 1377 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1378 unsigned int ,
1379 Output_section* ,
4d9aa155
AM
1380 const elfcpp::Rela<size, big_endian>& ,
1381 unsigned int r_type,
1382 Symbol*)
1383 {
1384 // As above.
1385 if (size == 64)
f6971787
AM
1386 {
1387 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1388 <Powerpc_relobj<size, big_endian>*>(relobj);
1389 if (ppcobj->abiversion() == 1)
1390 return false;
1391 }
32f59844 1392 return !is_branch_reloc<size>(r_type);
4d9aa155 1393 }
21bb3914 1394
b3ccdeb5 1395 static bool
9055360d
AM
1396 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1397 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1398 unsigned int r_type, bool report_err);
1399
42cacb20
DE
1400 private:
1401 static void
6fa2a40b 1402 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1403 unsigned int r_type);
1404
1405 static void
6fa2a40b 1406 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1407 unsigned int r_type, Symbol*);
1408
1409 static void
1410 generate_tls_call(Symbol_table* symtab, Layout* layout,
1411 Target_powerpc* target);
1412
1413 void
1414 check_non_pic(Relobj*, unsigned int r_type);
1415
1416 // Whether we have issued an error about a non-PIC compilation.
1417 bool issued_non_pic_error_;
1418 };
1419
1611bc4a
AM
1420 bool
1421 symval_for_branch(const Symbol_table* symtab,
6c77229c 1422 const Sized_symbol<size>* gsym,
3ea0a085 1423 Powerpc_relobj<size, big_endian>* object,
1611bc4a 1424 Address *value, unsigned int *dest_shndx);
3ea0a085 1425
42cacb20 1426 // The class which implements relocation.
e3deeb9c 1427 class Relocate : protected Track_tls
42cacb20
DE
1428 {
1429 public:
dd93cd0a
AM
1430 // Use 'at' branch hints when true, 'y' when false.
1431 // FIXME maybe: set this with an option.
1432 static const bool is_isa_v2 = true;
1433
dd93cd0a 1434 Relocate()
e3deeb9c 1435 : Track_tls()
dd93cd0a
AM
1436 { }
1437
42cacb20
DE
1438 // Do a relocation. Return false if the caller should not issue
1439 // any warnings about this relocation.
1440 inline bool
91a65d2f
AM
1441 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1442 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1443 const Sized_symbol<size>*, const Symbol_value<size>*,
1444 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1445 section_size_type);
42cacb20
DE
1446 };
1447
168a4726
AM
1448 class Relocate_comdat_behavior
1449 {
1450 public:
1451 // Decide what the linker should do for relocations that refer to
1452 // discarded comdat sections.
1453 inline Comdat_behavior
1454 get(const char* name)
1455 {
1456 gold::Default_comdat_behavior default_behavior;
1457 Comdat_behavior ret = default_behavior.get(name);
43193fe9 1458 if (ret == CB_ERROR)
168a4726
AM
1459 {
1460 if (size == 32
1461 && (strcmp(name, ".fixup") == 0
1462 || strcmp(name, ".got2") == 0))
1463 ret = CB_IGNORE;
1464 if (size == 64
1465 && (strcmp(name, ".opd") == 0
1466 || strcmp(name, ".toc") == 0
1467 || strcmp(name, ".toc1") == 0))
1468 ret = CB_IGNORE;
1469 }
1470 return ret;
1471 }
1472 };
1473
dd93cd0a
AM
1474 // Optimize the TLS relocation type based on what we know about the
1475 // symbol. IS_FINAL is true if the final address of this symbol is
1476 // known at link time.
1477
1478 tls::Tls_optimization
1479 optimize_tls_gd(bool is_final)
1480 {
1481 // If we are generating a shared library, then we can't do anything
1482 // in the linker.
aacb3b6d
AM
1483 if (parameters->options().shared()
1484 || !parameters->options().tls_optimize())
dd93cd0a
AM
1485 return tls::TLSOPT_NONE;
1486
1487 if (!is_final)
1488 return tls::TLSOPT_TO_IE;
1489 return tls::TLSOPT_TO_LE;
1490 }
1491
1492 tls::Tls_optimization
1493 optimize_tls_ld()
1494 {
aacb3b6d
AM
1495 if (parameters->options().shared()
1496 || !parameters->options().tls_optimize())
dd93cd0a
AM
1497 return tls::TLSOPT_NONE;
1498
1499 return tls::TLSOPT_TO_LE;
1500 }
1501
1502 tls::Tls_optimization
1503 optimize_tls_ie(bool is_final)
1504 {
aacb3b6d
AM
1505 if (!is_final
1506 || parameters->options().shared()
1507 || !parameters->options().tls_optimize())
dd93cd0a
AM
1508 return tls::TLSOPT_NONE;
1509
1510 return tls::TLSOPT_TO_LE;
1511 }
cf43a2fe 1512
cf43a2fe
AM
1513 // Create glink.
1514 void
1515 make_glink_section(Layout*);
42cacb20 1516
cf43a2fe
AM
1517 // Create the PLT section.
1518 void
40b469d7 1519 make_plt_section(Symbol_table*, Layout*);
42cacb20 1520
e5d5f5ed 1521 void
40b469d7 1522 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1523
2d7ad24e
AM
1524 void
1525 make_lplt_section(Layout*);
1526
ec661b9d
AM
1527 void
1528 make_brlt_section(Layout*);
1529
42cacb20
DE
1530 // Create a PLT entry for a global symbol.
1531 void
ec661b9d 1532 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1533
1534 // Create a PLT entry for a local IFUNC symbol.
1535 void
40b469d7 1536 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1537 Sized_relobj_file<size, big_endian>*,
1538 unsigned int);
1539
2d7ad24e
AM
1540 // Create a PLT entry for a local non-IFUNC symbol.
1541 void
1542 make_local_plt_entry(Layout*,
1543 Sized_relobj_file<size, big_endian>*,
1544 unsigned int);
1545
42cacb20 1546
dd93cd0a
AM
1547 // Create a GOT entry for local dynamic __tls_get_addr.
1548 unsigned int
1549 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1550 Sized_relobj_file<size, big_endian>* object);
1551
42cacb20 1552 unsigned int
dd93cd0a
AM
1553 tlsld_got_offset() const
1554 {
1555 return this->tlsld_got_offset_;
1556 }
42cacb20 1557
42cacb20
DE
1558 // Get the dynamic reloc section, creating it if necessary.
1559 Reloc_section*
1560 rela_dyn_section(Layout*);
1561
b3ccdeb5
AM
1562 // Similarly, but for ifunc symbols get the one for ifunc.
1563 Reloc_section*
1564 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1565
42cacb20
DE
1566 // Copy a relocation against a global symbol.
1567 void
ef9beddf 1568 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1569 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1570 unsigned int shndx, Output_section* output_section,
1571 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1572 {
859d7987 1573 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1574 this->copy_relocs_.copy_reloc(symtab, layout,
1575 symtab->get_sized_symbol<size>(sym),
1576 object, shndx, output_section,
859d7987
CC
1577 r_type, reloc.get_r_offset(),
1578 reloc.get_r_addend(),
1579 this->rela_dyn_section(layout));
42cacb20
DE
1580 }
1581
0cfdc767 1582 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1583 void
a3e60ddb 1584 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1585
1586 // Sort output sections by address.
1587 struct Sort_sections
1588 {
1589 bool
1590 operator()(const Output_section* sec1, const Output_section* sec2)
1591 { return sec1->address() < sec2->address(); }
1592 };
1593
1594 class Branch_info
1595 {
1596 public:
1597 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1598 unsigned int data_shndx,
1599 Address r_offset,
1600 unsigned int r_type,
1601 unsigned int r_sym,
1602 Address addend)
1603 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1604 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1605 { }
1606
1607 ~Branch_info()
1608 { }
1609
7e57d19e
AM
1610 // Return whether this branch is going via a plt call stub, and if
1611 // so, mark it as having an R_PPC64_TOCSAVE.
1612 bool
1613 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1614 unsigned int shndx, Address offset,
1615 Target_powerpc* target, Symbol_table* symtab);
1616
ec661b9d 1617 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1618 bool
ec661b9d
AM
1619 make_stub(Stub_table<size, big_endian>*,
1620 Stub_table<size, big_endian>*,
1621 Symbol_table*) const;
1622
1623 private:
1624 // The branch location..
1625 Powerpc_relobj<size, big_endian>* object_;
1626 unsigned int shndx_;
1627 Address offset_;
1628 // ..and the branch type and destination.
7e57d19e
AM
1629 unsigned int r_type_ : 31;
1630 unsigned int tocsave_ : 1;
ec661b9d
AM
1631 unsigned int r_sym_;
1632 Address addend_;
1633 };
1634
42cacb20
DE
1635 // Information about this specific target which we pass to the
1636 // general Target structure.
1637 static Target::Target_info powerpc_info;
1638
1639 // The types of GOT entries needed for this platform.
0e70b911
CC
1640 // These values are exposed to the ABI in an incremental link.
1641 // Do not renumber existing values without changing the version
1642 // number of the .gnu_incremental_inputs section.
42cacb20
DE
1643 enum Got_type
1644 {
dd93cd0a
AM
1645 GOT_TYPE_STANDARD,
1646 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1647 GOT_TYPE_DTPREL, // entry for @got@dtprel
1648 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
1649 };
1650
ec661b9d 1651 // The GOT section.
cf43a2fe 1652 Output_data_got_powerpc<size, big_endian>* got_;
b3ccdeb5
AM
1653 // The PLT section. This is a container for a table of addresses,
1654 // and their relocations. Each address in the PLT has a dynamic
1655 // relocation (R_*_JMP_SLOT) and each address will have a
1656 // corresponding entry in .glink for lazy resolution of the PLT.
1657 // ppc32 initialises the PLT to point at the .glink entry, while
1658 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1659 // linker adds a stub that loads the PLT entry into ctr then
1660 // branches to ctr. There may be more than one stub for each PLT
1661 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1662 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1663 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1664 // The IPLT section. Like plt_, this is a container for a table of
1665 // addresses and their relocations, specifically for STT_GNU_IFUNC
1666 // functions that resolve locally (STT_GNU_IFUNC functions that
1667 // don't resolve locally go in PLT). Unlike plt_, these have no
1668 // entry in .glink for lazy resolution, and the relocation section
1669 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1670 // the relocation section may contain relocations against
1671 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1672 // relocation section will appear at the end of other dynamic
1673 // relocations, so that ld.so applies these relocations after other
1674 // dynamic relocations. In a static executable, the relocation
1675 // section is emitted and marked with __rela_iplt_start and
1676 // __rela_iplt_end symbols.
e5d5f5ed 1677 Output_data_plt_powerpc<size, big_endian>* iplt_;
2d7ad24e
AM
1678 // A PLT style section for local, non-ifunc symbols
1679 Output_data_plt_powerpc<size, big_endian>* lplt_;
ec661b9d
AM
1680 // Section holding long branch destinations.
1681 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1682 // The .glink section.
cf43a2fe 1683 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1684 // The dynamic reloc section.
42cacb20
DE
1685 Reloc_section* rela_dyn_;
1686 // Relocs saved to avoid a COPY reloc.
5edad15d 1687 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1688 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1689 unsigned int tlsld_got_offset_;
ec661b9d
AM
1690
1691 Stub_tables stub_tables_;
1692 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1693 Branch_lookup_table branch_lookup_table_;
1694
1695 typedef std::vector<Branch_info> Branches;
1696 Branches branch_info_;
7e57d19e 1697 Tocsave_loc tocsave_loc_;
9e69ed50 1698
e4dff765 1699 bool powerxx_stubs_;
9e69ed50 1700 bool plt_thread_safe_;
7ee7ff70
AM
1701 bool plt_localentry0_;
1702 bool plt_localentry0_init_;
1703 bool has_localentry0_;
34e0882b 1704 bool has_tls_get_addr_opt_;
93b9bf16 1705 bool tprel_opt_;
a3e60ddb
AM
1706
1707 bool relax_failed_;
1708 int relax_fail_count_;
1709 int32_t stub_group_size_;
d49044c7
AM
1710
1711 Output_data_save_res<size, big_endian> *savres_section_;
34e0882b
AM
1712
1713 // The "__tls_get_addr" symbol, if present
1714 Symbol* tls_get_addr_;
1715 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1716 Symbol* tls_get_addr_opt_;
724436fc
AM
1717
1718 // Attributes in output.
1719 Attributes_section_data* attributes_section_data_;
1720
1721 // Last input file to change various attribute tags
1722 const char* last_fp_;
1723 const char* last_ld_;
1724 const char* last_vec_;
1725 const char* last_struct_;
42cacb20
DE
1726};
1727
1728template<>
1729Target::Target_info Target_powerpc<32, true>::powerpc_info =
1730{
1731 32, // size
1732 true, // is_big_endian
1733 elfcpp::EM_PPC, // machine_code
1734 false, // has_make_symbol
1735 false, // has_resolve
1736 false, // has_code_fill
1737 true, // is_default_stack_executable
b3ce541e 1738 false, // can_icf_inline_merge_sections
42cacb20
DE
1739 '\0', // wrap_char
1740 "/usr/lib/ld.so.1", // dynamic_linker
1741 0x10000000, // default_text_segment_address
1742 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1743 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1744 false, // isolate_execinstr
1745 0, // rosegment_gap
8a5e3e08
ILT
1746 elfcpp::SHN_UNDEF, // small_common_shndx
1747 elfcpp::SHN_UNDEF, // large_common_shndx
1748 0, // small_common_section_flags
05a352e6
DK
1749 0, // large_common_section_flags
1750 NULL, // attributes_section
a67858e0 1751 NULL, // attributes_vendor
8d9743bd
MK
1752 "_start", // entry_symbol_name
1753 32, // hash_entry_size
bce5a025 1754 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1755};
1756
1757template<>
1758Target::Target_info Target_powerpc<32, false>::powerpc_info =
1759{
1760 32, // size
1761 false, // is_big_endian
1762 elfcpp::EM_PPC, // machine_code
1763 false, // has_make_symbol
1764 false, // has_resolve
1765 false, // has_code_fill
1766 true, // is_default_stack_executable
b3ce541e 1767 false, // can_icf_inline_merge_sections
42cacb20
DE
1768 '\0', // wrap_char
1769 "/usr/lib/ld.so.1", // dynamic_linker
1770 0x10000000, // default_text_segment_address
1771 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1772 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1773 false, // isolate_execinstr
1774 0, // rosegment_gap
8a5e3e08
ILT
1775 elfcpp::SHN_UNDEF, // small_common_shndx
1776 elfcpp::SHN_UNDEF, // large_common_shndx
1777 0, // small_common_section_flags
05a352e6
DK
1778 0, // large_common_section_flags
1779 NULL, // attributes_section
a67858e0 1780 NULL, // attributes_vendor
8d9743bd
MK
1781 "_start", // entry_symbol_name
1782 32, // hash_entry_size
bce5a025 1783 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1784};
1785
1786template<>
1787Target::Target_info Target_powerpc<64, true>::powerpc_info =
1788{
1789 64, // size
1790 true, // is_big_endian
1791 elfcpp::EM_PPC64, // machine_code
1792 false, // has_make_symbol
565ed01a 1793 true, // has_resolve
42cacb20 1794 false, // has_code_fill
ec769010 1795 false, // is_default_stack_executable
b3ce541e 1796 false, // can_icf_inline_merge_sections
42cacb20
DE
1797 '\0', // wrap_char
1798 "/usr/lib/ld.so.1", // dynamic_linker
1799 0x10000000, // default_text_segment_address
1800 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1801 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1802 false, // isolate_execinstr
1803 0, // rosegment_gap
8a5e3e08
ILT
1804 elfcpp::SHN_UNDEF, // small_common_shndx
1805 elfcpp::SHN_UNDEF, // large_common_shndx
1806 0, // small_common_section_flags
05a352e6
DK
1807 0, // large_common_section_flags
1808 NULL, // attributes_section
a67858e0 1809 NULL, // attributes_vendor
8d9743bd
MK
1810 "_start", // entry_symbol_name
1811 32, // hash_entry_size
bce5a025 1812 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1813};
1814
1815template<>
1816Target::Target_info Target_powerpc<64, false>::powerpc_info =
1817{
1818 64, // size
1819 false, // is_big_endian
1820 elfcpp::EM_PPC64, // machine_code
1821 false, // has_make_symbol
565ed01a 1822 true, // has_resolve
42cacb20 1823 false, // has_code_fill
ec769010 1824 false, // is_default_stack_executable
b3ce541e 1825 false, // can_icf_inline_merge_sections
42cacb20
DE
1826 '\0', // wrap_char
1827 "/usr/lib/ld.so.1", // dynamic_linker
1828 0x10000000, // default_text_segment_address
1829 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1830 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1831 false, // isolate_execinstr
1832 0, // rosegment_gap
8a5e3e08
ILT
1833 elfcpp::SHN_UNDEF, // small_common_shndx
1834 elfcpp::SHN_UNDEF, // large_common_shndx
1835 0, // small_common_section_flags
05a352e6
DK
1836 0, // large_common_section_flags
1837 NULL, // attributes_section
a67858e0 1838 NULL, // attributes_vendor
8d9743bd
MK
1839 "_start", // entry_symbol_name
1840 32, // hash_entry_size
bce5a025 1841 elfcpp::SHT_PROGBITS, // unwind_section_type
42cacb20
DE
1842};
1843
32f59844 1844template<int size>
dd93cd0a
AM
1845inline bool
1846is_branch_reloc(unsigned int r_type)
1847{
1848 return (r_type == elfcpp::R_POWERPC_REL24
32f59844 1849 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
dd93cd0a
AM
1850 || r_type == elfcpp::R_PPC_PLTREL24
1851 || r_type == elfcpp::R_PPC_LOCAL24PC
1852 || r_type == elfcpp::R_POWERPC_REL14
1853 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1854 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1855 || r_type == elfcpp::R_POWERPC_ADDR24
1856 || r_type == elfcpp::R_POWERPC_ADDR14
1857 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1858 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1859}
1860
08be3224
AM
1861// Reloc resolves to plt entry.
1862template<int size>
1863inline bool
1864is_plt16_reloc(unsigned int r_type)
1865{
1866 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1867 || r_type == elfcpp::R_POWERPC_PLT16_HI
1868 || r_type == elfcpp::R_POWERPC_PLT16_HA
1869 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1870}
1871
dd93cd0a
AM
1872// If INSN is an opcode that may be used with an @tls operand, return
1873// the transformed insn for TLS optimisation, otherwise return 0. If
1874// REG is non-zero only match an insn with RB or RA equal to REG.
1875uint32_t
1876at_tls_transform(uint32_t insn, unsigned int reg)
1877{
1878 if ((insn & (0x3f << 26)) != 31 << 26)
1879 return 0;
1880
1881 unsigned int rtra;
1882 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1883 rtra = insn & ((1 << 26) - (1 << 16));
1884 else if (((insn >> 16) & 0x1f) == reg)
1885 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1886 else
1887 return 0;
1888
1889 if ((insn & (0x3ff << 1)) == 266 << 1)
1890 // add -> addi
1891 insn = 14 << 26;
1892 else if ((insn & (0x1f << 1)) == 23 << 1
1893 && ((insn & (0x1f << 6)) < 14 << 6
1894 || ((insn & (0x1f << 6)) >= 16 << 6
1895 && (insn & (0x1f << 6)) < 24 << 6)))
1896 // load and store indexed -> dform
1897 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1898 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1899 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1900 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1901 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1902 // lwax -> lwa
1903 insn = (58 << 26) | 2;
1904 else
1905 return 0;
1906 insn |= rtra;
1907 return insn;
1908}
1909
dd93cd0a 1910
42cacb20
DE
1911template<int size, bool big_endian>
1912class Powerpc_relocate_functions
1913{
dd93cd0a 1914public:
f4baf0d4 1915 enum Overflow_check
dd93cd0a 1916 {
f4baf0d4
AM
1917 CHECK_NONE,
1918 CHECK_SIGNED,
b80eed39
AM
1919 CHECK_UNSIGNED,
1920 CHECK_BITFIELD,
1921 CHECK_LOW_INSN,
1922 CHECK_HIGH_INSN
dd93cd0a
AM
1923 };
1924
f4baf0d4 1925 enum Status
dd93cd0a 1926 {
f4baf0d4
AM
1927 STATUS_OK,
1928 STATUS_OVERFLOW
1929 };
dd93cd0a 1930
42cacb20 1931private:
c9269dff 1932 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1933 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1934 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1935
dd93cd0a
AM
1936 template<int valsize>
1937 static inline bool
1938 has_overflow_signed(Address value)
1939 {
1940 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1941 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1942 limit <<= ((valsize - 1) >> 1);
1943 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1944 return value + limit > (limit << 1) - 1;
1945 }
1946
1947 template<int valsize>
1948 static inline bool
b80eed39 1949 has_overflow_unsigned(Address value)
dd93cd0a
AM
1950 {
1951 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1952 limit <<= ((valsize - 1) >> 1);
1953 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
1954 return value > (limit << 1) - 1;
1955 }
1956
1957 template<int valsize>
1958 static inline bool
1959 has_overflow_bitfield(Address value)
1960 {
1961 return (has_overflow_unsigned<valsize>(value)
1962 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
1963 }
1964
1965 template<int valsize>
f4baf0d4
AM
1966 static inline Status
1967 overflowed(Address value, Overflow_check overflow)
dd93cd0a 1968 {
f4baf0d4 1969 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
1970 {
1971 if (has_overflow_signed<valsize>(value))
f4baf0d4 1972 return STATUS_OVERFLOW;
dd93cd0a 1973 }
b80eed39
AM
1974 else if (overflow == CHECK_UNSIGNED)
1975 {
1976 if (has_overflow_unsigned<valsize>(value))
1977 return STATUS_OVERFLOW;
1978 }
f4baf0d4 1979 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
1980 {
1981 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 1982 return STATUS_OVERFLOW;
dd93cd0a 1983 }
f4baf0d4 1984 return STATUS_OK;
dd93cd0a
AM
1985 }
1986
cf43a2fe 1987 // Do a simple RELA relocation
0cfb0717 1988 template<int fieldsize, int valsize>
f4baf0d4
AM
1989 static inline Status
1990 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1991 {
0cfb0717 1992 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 1993 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1994 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
1995 return overflowed<valsize>(value, overflow);
1996 }
1997
0cfb0717 1998 template<int fieldsize, int valsize>
f4baf0d4 1999 static inline Status
42cacb20
DE
2000 rela(unsigned char* view,
2001 unsigned int right_shift,
0cfb0717 2002 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2003 Address value,
f4baf0d4 2004 Overflow_check overflow)
42cacb20 2005 {
0cfb0717 2006 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 2007 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 2008 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
6a010cf6
AM
2009 if (overflow == CHECK_SIGNED)
2010 value = static_cast<SignedAddress>(value) >> right_shift;
2011 else
2012 value = value >> right_shift;
2013 Valtype reloc = value;
42cacb20
DE
2014 val &= ~dst_mask;
2015 reloc &= dst_mask;
0cfb0717 2016 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
6a010cf6 2017 return overflowed<valsize>(value, overflow);
42cacb20
DE
2018 }
2019
cf43a2fe 2020 // Do a simple RELA relocation, unaligned.
0cfb0717 2021 template<int fieldsize, int valsize>
f4baf0d4
AM
2022 static inline Status
2023 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2024 {
0cfb0717 2025 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
2026 return overflowed<valsize>(value, overflow);
2027 }
2028
0cfb0717 2029 template<int fieldsize, int valsize>
f4baf0d4 2030 static inline Status
cf43a2fe
AM
2031 rela_ua(unsigned char* view,
2032 unsigned int right_shift,
0cfb0717 2033 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 2034 Address value,
f4baf0d4 2035 Overflow_check overflow)
42cacb20 2036 {
0cfb0717 2037 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 2038 Valtype;
0cfb0717 2039 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
6a010cf6
AM
2040 if (overflow == CHECK_SIGNED)
2041 value = static_cast<SignedAddress>(value) >> right_shift;
2042 else
2043 value = value >> right_shift;
2044 Valtype reloc = value;
42cacb20
DE
2045 val &= ~dst_mask;
2046 reloc &= dst_mask;
0cfb0717 2047 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
6a010cf6 2048 return overflowed<valsize>(value, overflow);
42cacb20
DE
2049 }
2050
42cacb20 2051public:
dd93cd0a 2052 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 2053 static inline void
dd93cd0a 2054 addr64(unsigned char* view, Address value)
0cfb0717 2055 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 2056
dd93cd0a 2057 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 2058 static inline void
dd93cd0a 2059 addr64_u(unsigned char* view, Address value)
0cfb0717 2060 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
2061
2062 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
2063 static inline Status
2064 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2065 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
2066
2067 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
2068 static inline Status
2069 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2070 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
2071
2072 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
2073 static inline Status
2074 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2075 {
0cfb0717
AM
2076 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2077 value, overflow);
f4baf0d4
AM
2078 if (overflow != CHECK_NONE && (value & 3) != 0)
2079 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2080 return stat;
2081 }
42cacb20
DE
2082
2083 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
2084 static inline Status
2085 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2086 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 2087
dd93cd0a 2088 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
2089 static inline Status
2090 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 2091 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 2092
dd93cd0a 2093 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2094 static inline Status
2095 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2096 {
0cfb0717 2097 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 2098 if ((value & 3) != 0)
f4baf0d4 2099 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2100 return stat;
2101 }
42cacb20 2102
a680de9a
PB
2103 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2104 static inline Status
2105 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2106 {
2107 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2108 if ((value & 15) != 0)
2109 stat = STATUS_OVERFLOW;
2110 return stat;
2111 }
2112
42cacb20
DE
2113 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2114 static inline void
dd93cd0a 2115 addr16_hi(unsigned char* view, Address value)
0cfb0717 2116 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 2117
c9269dff 2118 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 2119 static inline void
dd93cd0a
AM
2120 addr16_ha(unsigned char* view, Address value)
2121 { This::addr16_hi(view, value + 0x8000); }
42cacb20 2122
dd93cd0a 2123 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 2124 static inline void
dd93cd0a 2125 addr16_hi2(unsigned char* view, Address value)
0cfb0717 2126 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 2127
dd93cd0a 2128 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 2129 static inline void
dd93cd0a
AM
2130 addr16_ha2(unsigned char* view, Address value)
2131 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 2132
dd93cd0a 2133 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 2134 static inline void
dd93cd0a 2135 addr16_hi3(unsigned char* view, Address value)
0cfb0717 2136 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 2137
dd93cd0a 2138 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 2139 static inline void
dd93cd0a
AM
2140 addr16_ha3(unsigned char* view, Address value)
2141 { This::addr16_hi3(view, value + 0x8000); }
2142
2143 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
2144 static inline Status
2145 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 2146 {
0cfb0717 2147 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
2148 if (overflow != CHECK_NONE && (value & 3) != 0)
2149 stat = STATUS_OVERFLOW;
dd93cd0a
AM
2150 return stat;
2151 }
a680de9a
PB
2152
2153 // R_POWERPC_REL16DX_HA
2154 static inline Status
2155 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2156 {
2157 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2158 Valtype* wv = reinterpret_cast<Valtype*>(view);
2159 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2160 value += 0x8000;
2161 value = static_cast<SignedAddress>(value) >> 16;
2162 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2163 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2164 return overflowed<16>(value, overflow);
2165 }
e4dff765
AM
2166
2167 // R_PPC64_D34
2168 static inline Status
2169 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2170 {
2171 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2172 value, overflow);
2173 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2174 return stat;
2175 }
2176
2177 // R_PPC64_D34_HI30
2178 static inline void
2179 addr34_hi(unsigned char *view, uint64_t value)
2180 { This::addr34(view, value >> 34, CHECK_NONE);}
2181
2182 // R_PPC64_D34_HA30
2183 static inline void
2184 addr34_ha(unsigned char *view, uint64_t value)
2185 { This::addr34_hi(view, value + (1ULL << 33));}
2186
2187 // R_PPC64_D28
2188 static inline Status
2189 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2190 {
2191 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2192 value, overflow);
2193 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2194 return stat;
2195 }
2196
2197 // R_PPC64_ADDR16_HIGHER34
2198 static inline void
2199 addr16_higher34(unsigned char* view, uint64_t value)
2200 { This::addr16(view, value >> 34, CHECK_NONE); }
2201
2202 // R_PPC64_ADDR16_HIGHERA34
2203 static inline void
2204 addr16_highera34(unsigned char* view, uint64_t value)
2205 { This::addr16_higher34(view, value + (1ULL << 33)); }
2206
2207 // R_PPC64_ADDR16_HIGHEST34
2208 static inline void
2209 addr16_highest34(unsigned char* view, uint64_t value)
2210 { This::addr16(view, value >> 50, CHECK_NONE); }
2211
2212 // R_PPC64_ADDR16_HIGHESTA34
2213 static inline void
2214 addr16_highesta34(unsigned char* view, uint64_t value)
2215 { This::addr16_highest34(view, value + (1ULL << 33)); }
cf43a2fe
AM
2216};
2217
b4f7960d
AM
2218// Set ABI version for input and output.
2219
2220template<int size, bool big_endian>
2221void
2222Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2223{
2224 this->e_flags_ |= ver;
2225 if (this->abiversion() != 0)
2226 {
2227 Target_powerpc<size, big_endian>* target =
2228 static_cast<Target_powerpc<size, big_endian>*>(
2229 parameters->sized_target<size, big_endian>());
2230 if (target->abiversion() == 0)
2231 target->set_abiversion(this->abiversion());
2232 else if (target->abiversion() != this->abiversion())
2233 gold_error(_("%s: ABI version %d is not compatible "
2234 "with ABI version %d output"),
2235 this->name().c_str(),
2236 this->abiversion(), target->abiversion());
2237
2238 }
2239}
2240
5edad15d
AM
2241// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2242// relocatable object, if such sections exists.
cf43a2fe
AM
2243
2244template<int size, bool big_endian>
2245bool
2246Powerpc_relobj<size, big_endian>::do_find_special_sections(
2247 Read_symbols_data* sd)
2248{
c9269dff
AM
2249 const unsigned char* const pshdrs = sd->section_headers->data();
2250 const unsigned char* namesu = sd->section_names->data();
2251 const char* names = reinterpret_cast<const char*>(namesu);
2252 section_size_type names_size = sd->section_names_size;
2253 const unsigned char* s;
2254
dc3714f3
AM
2255 s = this->template find_shdr<size, big_endian>(pshdrs,
2256 size == 32 ? ".got2" : ".opd",
2257 names, names_size, NULL);
c9269dff
AM
2258 if (s != NULL)
2259 {
2260 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2261 this->special_ = ndx;
b4f7960d
AM
2262 if (size == 64)
2263 {
2264 if (this->abiversion() == 0)
2265 this->set_abiversion(1);
2266 else if (this->abiversion() > 1)
2267 gold_error(_("%s: .opd invalid in abiv%d"),
2268 this->name().c_str(), this->abiversion());
2269 }
c9269dff 2270 }
5edad15d
AM
2271 if (size == 64)
2272 {
2273 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2274 names, names_size, NULL);
2275 if (s != NULL)
2276 {
2277 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2278 this->relatoc_ = ndx;
2279 typename elfcpp::Shdr<size, big_endian> shdr(s);
2280 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2281 }
2282 }
c9269dff
AM
2283 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2284}
2285
2286// Examine .rela.opd to build info about function entry points.
2287
2288template<int size, bool big_endian>
2289void
2290Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2291 size_t reloc_count,
2292 const unsigned char* prelocs,
2293 const unsigned char* plocal_syms)
2294{
2295 if (size == 64)
cf43a2fe 2296 {
0e123f69
AM
2297 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2298 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2299 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2300 Address expected_off = 0;
2301 bool regular = true;
2302 unsigned int opd_ent_size = 0;
c9269dff
AM
2303
2304 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2305 {
c9269dff
AM
2306 Reltype reloc(prelocs);
2307 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2308 = reloc.get_r_info();
2309 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2310 if (r_type == elfcpp::R_PPC64_ADDR64)
2311 {
2312 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2313 typename elfcpp::Elf_types<size>::Elf_Addr value;
2314 bool is_ordinary;
2315 unsigned int shndx;
2316 if (r_sym < this->local_symbol_count())
2317 {
2318 typename elfcpp::Sym<size, big_endian>
2319 lsym(plocal_syms + r_sym * sym_size);
2320 shndx = lsym.get_st_shndx();
2321 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2322 value = lsym.get_st_value();
2323 }
2324 else
2325 shndx = this->symbol_section_and_value(r_sym, &value,
2326 &is_ordinary);
2327 this->set_opd_ent(reloc.get_r_offset(), shndx,
2328 value + reloc.get_r_addend());
ec4dbad3
AM
2329 if (i == 2)
2330 {
2331 expected_off = reloc.get_r_offset();
2332 opd_ent_size = expected_off;
2333 }
2334 else if (expected_off != reloc.get_r_offset())
2335 regular = false;
2336 expected_off += opd_ent_size;
2337 }
2338 else if (r_type == elfcpp::R_PPC64_TOC)
2339 {
2340 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2341 regular = false;
2342 }
2343 else
2344 {
2345 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2346 this->name().c_str(), r_type);
2347 regular = false;
c9269dff
AM
2348 }
2349 }
ec4dbad3
AM
2350 if (reloc_count <= 2)
2351 opd_ent_size = this->section_size(this->opd_shndx());
2352 if (opd_ent_size != 24 && opd_ent_size != 16)
2353 regular = false;
2354 if (!regular)
2355 {
2356 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2357 this->name().c_str());
2358 opd_ent_size = 0;
2359 }
c9269dff
AM
2360 }
2361}
2362
5edad15d
AM
2363// Returns true if a code sequence loading the TOC entry at VALUE
2364// relative to the TOC pointer can be converted into code calculating
2365// a TOC pointer relative offset.
2366// If so, the TOC pointer relative offset is stored to VALUE.
2367
2368template<int size, bool big_endian>
2369bool
2370Powerpc_relobj<size, big_endian>::make_toc_relative(
2371 Target_powerpc<size, big_endian>* target,
2372 Address* value)
2373{
2374 if (size != 64)
2375 return false;
2376
e666304e
AM
2377 // With -mcmodel=medium code it is quite possible to have
2378 // toc-relative relocs referring to objects outside the TOC.
2379 // Don't try to look at a non-existent TOC.
2380 if (this->toc_shndx() == 0)
2381 return false;
2382
5edad15d
AM
2383 // Convert VALUE back to an address by adding got_base (see below),
2384 // then to an offset in the TOC by subtracting the TOC output
2385 // section address and the TOC output offset. Since this TOC output
2386 // section and the got output section are one and the same, we can
2387 // omit adding and subtracting the output section address.
2388 Address off = (*value + this->toc_base_offset()
2389 - this->output_section_offset(this->toc_shndx()));
2390 // Is this offset in the TOC? -mcmodel=medium code may be using
2391 // TOC relative access to variables outside the TOC. Those of
2392 // course can't be optimized. We also don't try to optimize code
2393 // that is using a different object's TOC.
2394 if (off >= this->section_size(this->toc_shndx()))
2395 return false;
2396
2397 if (this->no_toc_opt(off))
2398 return false;
2399
2400 section_size_type vlen;
2401 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2402 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2403 // The TOC pointer
2404 Address got_base = (target->got_section()->output_section()->address()
2405 + this->toc_base_offset());
2406 addr -= got_base;
857e829e 2407 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2408 return false;
2409
2410 *value = addr;
2411 return true;
2412}
2413
c9b8abb7
AM
2414template<int size, bool big_endian>
2415bool
2416Powerpc_relobj<size, big_endian>::make_got_relative(
2417 Target_powerpc<size, big_endian>* target,
2418 const Symbol_value<size>* psymval,
2419 Address addend,
2420 Address* value)
2421{
2422 Address addr = psymval->value(this, addend);
2423 Address got_base = (target->got_section()->output_section()->address()
2424 + this->toc_base_offset());
2425 addr -= got_base;
2426 if (addr + 0x80008000 > 0xffffffff)
2427 return false;
2428
2429 *value = addr;
2430 return true;
2431}
2432
5edad15d
AM
2433// Perform the Sized_relobj_file method, then set up opd info from
2434// .opd relocs.
2435
c9269dff
AM
2436template<int size, bool big_endian>
2437void
2438Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2439{
2440 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2441 if (size == 64)
2442 {
2443 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2444 p != rd->relocs.end();
2445 ++p)
2446 {
2447 if (p->data_shndx == this->opd_shndx())
2448 {
ec4dbad3
AM
2449 uint64_t opd_size = this->section_size(this->opd_shndx());
2450 gold_assert(opd_size == static_cast<size_t>(opd_size));
2451 if (opd_size != 0)
2452 {
2453 this->init_opd(opd_size);
2454 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2455 rd->local_symbols->data());
2456 }
c9269dff
AM
2457 break;
2458 }
cf43a2fe
AM
2459 }
2460 }
cf43a2fe
AM
2461}
2462
b4f7960d
AM
2463// Read the symbols then set up st_other vector.
2464
2465template<int size, bool big_endian>
2466void
2467Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2468{
f35c4853 2469 this->base_read_symbols(sd);
724436fc
AM
2470 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2471 return;
b4f7960d
AM
2472 if (size == 64)
2473 {
2474 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2475 const unsigned char* const pshdrs = sd->section_headers->data();
2476 const unsigned int loccount = this->do_local_symbol_count();
2477 if (loccount != 0)
2478 {
2479 this->st_other_.resize(loccount);
2480 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2481 off_t locsize = loccount * sym_size;
2482 const unsigned int symtab_shndx = this->symtab_shndx();
2483 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2484 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2485 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2486 locsize, true, false);
2487 psyms += sym_size;
2488 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2489 {
2490 elfcpp::Sym<size, big_endian> sym(psyms);
2491 unsigned char st_other = sym.get_st_other();
2492 this->st_other_[i] = st_other;
2493 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2494 {
2495 if (this->abiversion() == 0)
2496 this->set_abiversion(2);
2497 else if (this->abiversion() < 2)
2498 gold_error(_("%s: local symbol %d has invalid st_other"
2499 " for ABI version 1"),
2500 this->name().c_str(), i);
2501 }
2502 }
2503 }
2504 }
724436fc
AM
2505
2506 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2507 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2508 bool merge_attributes = false;
2509 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2510 {
2511 elfcpp::Shdr<size, big_endian> shdr(ps);
2512 switch (shdr.get_sh_type())
2513 {
2514 case elfcpp::SHT_GNU_ATTRIBUTES:
2515 {
2516 gold_assert(this->attributes_section_data_ == NULL);
2517 section_offset_type section_offset = shdr.get_sh_offset();
2518 section_size_type section_size =
2519 convert_to_section_size_type(shdr.get_sh_size());
2520 const unsigned char* view =
2521 this->get_view(section_offset, section_size, true, false);
2522 this->attributes_section_data_ =
2523 new Attributes_section_data(view, section_size);
2524 }
2525 break;
2526
2527 case elfcpp::SHT_SYMTAB:
2528 {
2529 // Sometimes an object has no contents except the section
2530 // name string table and an empty symbol table with the
2531 // undefined symbol. We don't want to merge
2532 // processor-specific flags from such an object.
2533 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2534 elfcpp::Elf_sizes<size>::sym_size;
2535 if (shdr.get_sh_size() > sym_size)
2536 merge_attributes = true;
2537 }
2538 break;
2539
2540 case elfcpp::SHT_STRTAB:
2541 break;
2542
2543 default:
2544 merge_attributes = true;
2545 break;
2546 }
2547 }
2548
2549 if (!merge_attributes)
2550 {
2551 // Should rarely happen.
2552 delete this->attributes_section_data_;
2553 this->attributes_section_data_ = NULL;
2554 }
b4f7960d
AM
2555}
2556
2557template<int size, bool big_endian>
2558void
2559Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2560{
2561 this->e_flags_ |= ver;
2562 if (this->abiversion() != 0)
2563 {
2564 Target_powerpc<size, big_endian>* target =
2565 static_cast<Target_powerpc<size, big_endian>*>(
2566 parameters->sized_target<size, big_endian>());
2567 if (target->abiversion() == 0)
2568 target->set_abiversion(this->abiversion());
2569 else if (target->abiversion() != this->abiversion())
2570 gold_error(_("%s: ABI version %d is not compatible "
2571 "with ABI version %d output"),
2572 this->name().c_str(),
2573 this->abiversion(), target->abiversion());
2574
2575 }
2576}
2577
f35c4853 2578// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2579// read .opd from a dynamic object, filling in opd_ent_ vector,
2580
2581template<int size, bool big_endian>
2582void
2583Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2584{
f35c4853 2585 this->base_read_symbols(sd);
724436fc
AM
2586 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2587 const unsigned char* ps =
2588 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2589 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2590 {
2591 elfcpp::Shdr<size, big_endian> shdr(ps);
2592 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2593 {
2594 section_offset_type section_offset = shdr.get_sh_offset();
2595 section_size_type section_size =
2596 convert_to_section_size_type(shdr.get_sh_size());
2597 const unsigned char* view =
2598 this->get_view(section_offset, section_size, true, false);
2599 this->attributes_section_data_ =
2600 new Attributes_section_data(view, section_size);
2601 break;
2602 }
2603 }
dc3714f3
AM
2604 if (size == 64)
2605 {
dc3714f3
AM
2606 const unsigned char* const pshdrs = sd->section_headers->data();
2607 const unsigned char* namesu = sd->section_names->data();
2608 const char* names = reinterpret_cast<const char*>(namesu);
2609 const unsigned char* s = NULL;
2610 const unsigned char* opd;
2611 section_size_type opd_size;
2612
2613 // Find and read .opd section.
2614 while (1)
2615 {
2616 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2617 sd->section_names_size,
2618 s);
2619 if (s == NULL)
2620 return;
2621
2622 typename elfcpp::Shdr<size, big_endian> shdr(s);
2623 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2624 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2625 {
b4f7960d
AM
2626 if (this->abiversion() == 0)
2627 this->set_abiversion(1);
2628 else if (this->abiversion() > 1)
2629 gold_error(_("%s: .opd invalid in abiv%d"),
2630 this->name().c_str(), this->abiversion());
2631
dc3714f3
AM
2632 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2633 this->opd_address_ = shdr.get_sh_addr();
2634 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2635 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2636 true, false);
2637 break;
2638 }
2639 }
2640
2641 // Build set of executable sections.
2642 // Using a set is probably overkill. There is likely to be only
2643 // a few executable sections, typically .init, .text and .fini,
2644 // and they are generally grouped together.
2645 typedef std::set<Sec_info> Exec_sections;
2646 Exec_sections exec_sections;
2647 s = pshdrs;
2648 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2649 {
2650 typename elfcpp::Shdr<size, big_endian> shdr(s);
2651 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2652 && ((shdr.get_sh_flags()
2653 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2654 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2655 && shdr.get_sh_size() != 0)
2656 {
2657 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2658 shdr.get_sh_size(), i));
2659 }
2660 }
2661 if (exec_sections.empty())
2662 return;
2663
2664 // Look over the OPD entries. This is complicated by the fact
2665 // that some binaries will use two-word entries while others
2666 // will use the standard three-word entries. In most cases
2667 // the third word (the environment pointer for languages like
2668 // Pascal) is unused and will be zero. If the third word is
2669 // used it should not be pointing into executable sections,
2670 // I think.
2671 this->init_opd(opd_size);
2672 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2673 {
2674 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2675 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2676 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2677 if (val == 0)
2678 // Chances are that this is the third word of an OPD entry.
2679 continue;
2680 typename Exec_sections::const_iterator e
2681 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2682 if (e != exec_sections.begin())
2683 {
2684 --e;
2685 if (e->start <= val && val < e->start + e->len)
2686 {
2687 // We have an address in an executable section.
2688 // VAL ought to be the function entry, set it up.
2689 this->set_opd_ent(p - opd, e->shndx, val);
2690 // Skip second word of OPD entry, the TOC pointer.
2691 p += 8;
2692 }
2693 }
2694 // If we didn't match any executable sections, we likely
2695 // have a non-zero third word in the OPD entry.
2696 }
2697 }
2698}
2699
5edad15d
AM
2700// Relocate sections.
2701
2702template<int size, bool big_endian>
2703void
2704Powerpc_relobj<size, big_endian>::do_relocate_sections(
2705 const Symbol_table* symtab, const Layout* layout,
2706 const unsigned char* pshdrs, Output_file* of,
2707 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2708{
2709 unsigned int start = 1;
2710 if (size == 64
2711 && this->relatoc_ != 0
2712 && !parameters->options().relocatable())
2713 {
2714 // Relocate .toc first.
2715 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2716 this->relatoc_, this->relatoc_);
2717 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2718 1, this->relatoc_ - 1);
2719 start = this->relatoc_ + 1;
2720 }
2721 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2722 start, this->shnum() - 1);
2d7ad24e
AM
2723
2724 if (!parameters->options().output_is_position_independent())
2725 {
2726 Target_powerpc<size, big_endian>* target
2727 = static_cast<Target_powerpc<size, big_endian>*>(
2728 parameters->sized_target<size, big_endian>());
2729 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2730 {
2731 const section_size_type offset = target->lplt_section()->offset();
2732 const section_size_type oview_size
2733 = convert_to_section_size_type(target->lplt_section()->data_size());
2734 unsigned char* const oview = of->get_output_view(offset, oview_size);
2735
2736 bool modified = false;
2737 unsigned int nsyms = this->local_symbol_count();
2738 for (unsigned int i = 0; i < nsyms; i++)
2739 if (this->local_has_plt_offset(i))
2740 {
2741 Address value = this->local_symbol_value(i, 0);
2742 if (size == 64)
2743 value += ppc64_local_entry_offset(i);
2744 size_t off = this->local_plt_offset(i);
2745 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2746 modified = true;
2747 }
2748 if (modified)
2749 of->write_output_view(offset, oview_size, oview);
2750 }
2751 }
5edad15d
AM
2752}
2753
f43ba157 2754// Set up some symbols.
26a4e9cb
AM
2755
2756template<int size, bool big_endian>
2757void
f43ba157
AM
2758Target_powerpc<size, big_endian>::do_define_standard_symbols(
2759 Symbol_table* symtab,
2760 Layout* layout)
26a4e9cb
AM
2761{
2762 if (size == 32)
2763 {
bb66a627
AM
2764 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2765 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2766 // non-relative dynamic relocs). The proper value will be
2767 // updated later.
2768 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2769 if (gotsym != NULL && gotsym->is_undefined())
2770 {
2771 Target_powerpc<size, big_endian>* target =
2772 static_cast<Target_powerpc<size, big_endian>*>(
2773 parameters->sized_target<size, big_endian>());
2774 Output_data_got_powerpc<size, big_endian>* got
2775 = target->got_section(symtab, layout);
2776 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2777 Symbol_table::PREDEFINED,
2778 got, 0, 0,
2779 elfcpp::STT_OBJECT,
bb66a627 2780 elfcpp::STB_LOCAL,
26a4e9cb
AM
2781 elfcpp::STV_HIDDEN, 0,
2782 false, false);
2783 }
2784
2785 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2786 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2787 if (sdasym != NULL && sdasym->is_undefined())
2788 {
2789 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2790 Output_section* os
2791 = layout->add_output_section_data(".sdata", 0,
2792 elfcpp::SHF_ALLOC
2793 | elfcpp::SHF_WRITE,
2794 sdata, ORDER_SMALL_DATA, false);
2795 symtab->define_in_output_data("_SDA_BASE_", NULL,
2796 Symbol_table::PREDEFINED,
2797 os, 32768, 0, elfcpp::STT_OBJECT,
2798 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2799 0, false, false);
2800 }
2801 }
b4f7960d
AM
2802 else
2803 {
2804 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2805 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2806 if (gotsym != NULL && gotsym->is_undefined())
2807 {
2808 Target_powerpc<size, big_endian>* target =
2809 static_cast<Target_powerpc<size, big_endian>*>(
2810 parameters->sized_target<size, big_endian>());
2811 Output_data_got_powerpc<size, big_endian>* got
2812 = target->got_section(symtab, layout);
2813 symtab->define_in_output_data(".TOC.", NULL,
2814 Symbol_table::PREDEFINED,
2815 got, 0x8000, 0,
2816 elfcpp::STT_OBJECT,
2817 elfcpp::STB_LOCAL,
2818 elfcpp::STV_HIDDEN, 0,
2819 false, false);
2820 }
2821 }
34e0882b
AM
2822
2823 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2824 if (parameters->options().tls_get_addr_optimize()
2825 && this->tls_get_addr_ != NULL
2826 && this->tls_get_addr_->in_reg())
2827 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2828 if (this->tls_get_addr_opt_ != NULL)
2829 {
2830 if (this->tls_get_addr_->is_undefined()
2831 || this->tls_get_addr_->is_from_dynobj())
2832 {
2833 // Make it seem as if references to __tls_get_addr are
2834 // really to __tls_get_addr_opt, so the latter symbol is
2835 // made dynamic, not the former.
2836 this->tls_get_addr_->clear_in_reg();
2837 this->tls_get_addr_opt_->set_in_reg();
2838 }
2839 // We have a non-dynamic definition for __tls_get_addr.
2840 // Make __tls_get_addr_opt the same, if it does not already have
2841 // a non-dynamic definition.
2842 else if (this->tls_get_addr_opt_->is_undefined()
2843 || this->tls_get_addr_opt_->is_from_dynobj())
2844 {
2845 Sized_symbol<size>* from
2846 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2847 Sized_symbol<size>* to
2848 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2849 symtab->clone<size>(to, from);
2850 }
2851 }
26a4e9cb
AM
2852}
2853
cf43a2fe
AM
2854// Set up PowerPC target specific relobj.
2855
2856template<int size, bool big_endian>
2857Object*
2858Target_powerpc<size, big_endian>::do_make_elf_object(
2859 const std::string& name,
2860 Input_file* input_file,
2861 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2862{
2863 int et = ehdr.get_e_type();
957564c9
AS
2864 // ET_EXEC files are valid input for --just-symbols/-R,
2865 // and we treat them as relocatable objects.
2866 if (et == elfcpp::ET_REL
2867 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2868 {
2869 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2870 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2871 obj->setup();
2872 return obj;
2873 }
2874 else if (et == elfcpp::ET_DYN)
2875 {
dc3714f3
AM
2876 Powerpc_dynobj<size, big_endian>* obj =
2877 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2878 obj->setup();
2879 return obj;
2880 }
2881 else
2882 {
c9269dff 2883 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2884 return NULL;
2885 }
2886}
2887
2888template<int size, bool big_endian>
2889class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2890{
2891public:
2892 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2893 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2894
2895 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2896 : Output_data_got<size, big_endian>(),
2897 symtab_(symtab), layout_(layout),
2898 header_ent_cnt_(size == 32 ? 3 : 1),
2899 header_index_(size == 32 ? 0x2000 : 0)
751e4d66
AM
2900 {
2901 if (size == 64)
2902 this->set_addralign(256);
2903 }
cf43a2fe 2904
e84fe78f
AM
2905 // Override all the Output_data_got methods we use so as to first call
2906 // reserve_ent().
2907 bool
2908 add_global(Symbol* gsym, unsigned int got_type)
2909 {
2910 this->reserve_ent();
2911 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2912 }
2913
2914 bool
2915 add_global_plt(Symbol* gsym, unsigned int got_type)
2916 {
2917 this->reserve_ent();
2918 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2919 }
2920
2921 bool
2922 add_global_tls(Symbol* gsym, unsigned int got_type)
2923 { return this->add_global_plt(gsym, got_type); }
2924
2925 void
2926 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2927 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2928 {
2929 this->reserve_ent();
2930 Output_data_got<size, big_endian>::
2931 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2932 }
2933
2934 void
2935 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2936 Output_data_reloc_generic* rel_dyn,
2937 unsigned int r_type_1, unsigned int r_type_2)
2938 {
aacb3b6d
AM
2939 if (gsym->has_got_offset(got_type))
2940 return;
2941
e84fe78f
AM
2942 this->reserve_ent(2);
2943 Output_data_got<size, big_endian>::
2944 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2945 }
2946
2947 bool
2948 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2949 {
2950 this->reserve_ent();
2951 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2952 got_type);
2953 }
2954
2955 bool
2956 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2957 {
2958 this->reserve_ent();
2959 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2960 got_type);
2961 }
2962
2963 bool
2964 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2965 { return this->add_local_plt(object, sym_index, got_type); }
2966
2967 void
2968 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2969 unsigned int got_type,
2970 Output_data_reloc_generic* rel_dyn,
2971 unsigned int r_type)
2972 {
aacb3b6d
AM
2973 if (object->local_has_got_offset(sym_index, got_type))
2974 return;
2975
e84fe78f
AM
2976 this->reserve_ent(2);
2977 Output_data_got<size, big_endian>::
2978 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2979 }
2980
2981 unsigned int
2982 add_constant(Valtype constant)
2983 {
2984 this->reserve_ent();
2985 return Output_data_got<size, big_endian>::add_constant(constant);
2986 }
2987
dd93cd0a
AM
2988 unsigned int
2989 add_constant_pair(Valtype c1, Valtype c2)
2990 {
2991 this->reserve_ent(2);
e84fe78f 2992 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
2993 }
2994
2995 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
2996 unsigned int
2997 g_o_t() const
2998 {
2999 return this->got_offset(this->header_index_);
42cacb20 3000 }
cf43a2fe 3001
dd93cd0a
AM
3002 // Offset of base used to access the GOT/TOC.
3003 // The got/toc pointer reg will be set to this value.
26a4e9cb 3004 Valtype
dd93cd0a
AM
3005 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
3006 {
3007 if (size == 32)
3008 return this->g_o_t();
3009 else
3010 return (this->output_section()->address()
3011 + object->toc_base_offset()
3012 - this->address());
3013 }
3014
cf43a2fe
AM
3015 // Ensure our GOT has a header.
3016 void
3017 set_final_data_size()
3018 {
3019 if (this->header_ent_cnt_ != 0)
3020 this->make_header();
3021 Output_data_got<size, big_endian>::set_final_data_size();
3022 }
3023
3024 // First word of GOT header needs some values that are not
3025 // handled by Output_data_got so poke them in here.
3026 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3027 void
3028 do_write(Output_file* of)
3029 {
c9824451
AM
3030 Valtype val = 0;
3031 if (size == 32 && this->layout_->dynamic_data() != NULL)
3032 val = this->layout_->dynamic_section()->address();
3033 if (size == 64)
3034 val = this->output_section()->address() + 0x8000;
3035 this->replace_constant(this->header_index_, val);
cf43a2fe
AM
3036 Output_data_got<size, big_endian>::do_write(of);
3037 }
3038
3039private:
3040 void
3041 reserve_ent(unsigned int cnt = 1)
3042 {
3043 if (this->header_ent_cnt_ == 0)
3044 return;
3045 if (this->num_entries() + cnt > this->header_index_)
3046 this->make_header();
3047 }
3048
3049 void
3050 make_header()
3051 {
3052 this->header_ent_cnt_ = 0;
3053 this->header_index_ = this->num_entries();
3054 if (size == 32)
3055 {
3056 Output_data_got<size, big_endian>::add_constant(0);
3057 Output_data_got<size, big_endian>::add_constant(0);
3058 Output_data_got<size, big_endian>::add_constant(0);
3059
3060 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
3061 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3062 if (gotsym != NULL)
3063 {
3064 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3065 sym->set_value(this->g_o_t());
3066 }
3067 else
3068 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3069 Symbol_table::PREDEFINED,
3070 this, this->g_o_t(), 0,
3071 elfcpp::STT_OBJECT,
3072 elfcpp::STB_LOCAL,
3073 elfcpp::STV_HIDDEN, 0,
3074 false, false);
cf43a2fe
AM
3075 }
3076 else
3077 Output_data_got<size, big_endian>::add_constant(0);
3078 }
3079
3080 // Stashed pointers.
3081 Symbol_table* symtab_;
3082 Layout* layout_;
3083
3084 // GOT header size.
3085 unsigned int header_ent_cnt_;
3086 // GOT header index.
3087 unsigned int header_index_;
42cacb20
DE
3088};
3089
3090// Get the GOT section, creating it if necessary.
3091
3092template<int size, bool big_endian>
cf43a2fe 3093Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
3094Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3095 Layout* layout)
3096{
3097 if (this->got_ == NULL)
3098 {
3099 gold_assert(symtab != NULL && layout != NULL);
3100
cf43a2fe
AM
3101 this->got_
3102 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
3103
3104 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3105 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 3106 this->got_, ORDER_DATA, false);
42cacb20
DE
3107 }
3108
3109 return this->got_;
3110}
3111
3112// Get the dynamic reloc section, creating it if necessary.
3113
3114template<int size, bool big_endian>
3115typename Target_powerpc<size, big_endian>::Reloc_section*
3116Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3117{
3118 if (this->rela_dyn_ == NULL)
3119 {
3120 gold_assert(layout != NULL);
3121 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3122 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
3123 elfcpp::SHF_ALLOC, this->rela_dyn_,
3124 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
3125 }
3126 return this->rela_dyn_;
3127}
3128
b3ccdeb5
AM
3129// Similarly, but for ifunc symbols get the one for ifunc.
3130
3131template<int size, bool big_endian>
3132typename Target_powerpc<size, big_endian>::Reloc_section*
3133Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3134 Layout* layout,
3135 bool for_ifunc)
3136{
3137 if (!for_ifunc)
3138 return this->rela_dyn_section(layout);
3139
3140 if (this->iplt_ == NULL)
3141 this->make_iplt_section(symtab, layout);
3142 return this->iplt_->rel_plt();
3143}
3144
ec661b9d
AM
3145class Stub_control
3146{
3147 public:
3148 // Determine the stub group size. The group size is the absolute
3149 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 3150 // is passed a negative value, we restrict stubs to be always after
ec661b9d 3151 // the stubbed branches.
1c3a5fbe
AM
3152 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3153 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3154 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3155 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3156 owner_(NULL), output_section_(NULL)
ec661b9d 3157 {
ec661b9d
AM
3158 }
3159
3160 // Return true iff input section can be handled by current stub
3161 // group.
3162 bool
3163 can_add_to_stub_group(Output_section* o,
3164 const Output_section::Input_section* i,
3165 bool has14);
3166
3167 const Output_section::Input_section*
3168 owner()
3169 { return owner_; }
3170
3171 Output_section*
3172 output_section()
3173 { return output_section_; }
3174
a20605cf
AM
3175 void
3176 set_output_and_owner(Output_section* o,
3177 const Output_section::Input_section* i)
3178 {
3179 this->output_section_ = o;
3180 this->owner_ = i;
3181 }
3182
ec661b9d
AM
3183 private:
3184 typedef enum
3185 {
1c3a5fbe 3186 // Initial state.
ec661b9d 3187 NO_GROUP,
1c3a5fbe 3188 // Adding group sections before the stubs.
ec661b9d 3189 FINDING_STUB_SECTION,
1c3a5fbe 3190 // Adding group sections after the stubs.
ec661b9d
AM
3191 HAS_STUB_SECTION
3192 } State;
3193
ec661b9d 3194 uint32_t stub_group_size_;
a5018ae5 3195 bool stubs_always_after_branch_;
ec661b9d 3196 bool suppress_size_errors_;
1c3a5fbe
AM
3197 // True if a stub group can serve multiple output sections.
3198 bool multi_os_;
3199 State state_;
8a37735f
AM
3200 // Current max size of group. Starts at stub_group_size_ but is
3201 // reduced to stub_group_size_/1024 on seeing a section with
3202 // external conditional branches.
3203 uint32_t group_size_;
a5018ae5 3204 uint64_t group_start_addr_;
57f6d32d
AM
3205 // owner_ and output_section_ specify the section to which stubs are
3206 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
3207 const Output_section::Input_section* owner_;
3208 Output_section* output_section_;
3209};
3210
0cfdc767 3211// Return true iff input section can be handled by current stub
a5018ae5
AM
3212// group. Sections are presented to this function in order,
3213// so the first section is the head of the group.
ec661b9d
AM
3214
3215bool
3216Stub_control::can_add_to_stub_group(Output_section* o,
3217 const Output_section::Input_section* i,
3218 bool has14)
3219{
ec661b9d
AM
3220 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3221 uint64_t this_size;
3222 uint64_t start_addr = o->address();
3223
3224 if (whole_sec)
3225 // .init and .fini sections are pasted together to form a single
3226 // function. We can't be adding stubs in the middle of the function.
3227 this_size = o->data_size();
3228 else
3229 {
3230 start_addr += i->relobj()->output_section_offset(i->shndx());
3231 this_size = i->data_size();
3232 }
57f6d32d 3233
a5018ae5 3234 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
3235 uint32_t group_size = this->stub_group_size_;
3236 if (has14)
3237 this->group_size_ = group_size = group_size >> 10;
ec661b9d 3238
57f6d32d 3239 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
3240 gold_warning(_("%s:%s exceeds group size"),
3241 i->relobj()->name().c_str(),
3242 i->relobj()->section_name(i->shndx()).c_str());
3243
afe002dd
AM
3244 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3245 has14 ? " 14bit" : "",
3246 i->relobj()->name().c_str(),
3247 i->relobj()->section_name(i->shndx()).c_str(),
3248 (long long) this_size,
a5018ae5
AM
3249 (this->state_ == NO_GROUP
3250 ? this_size
3251 : (long long) end_addr - this->group_start_addr_));
afe002dd 3252
1c3a5fbe
AM
3253 if (this->state_ == NO_GROUP)
3254 {
3255 // Only here on very first use of Stub_control
3256 this->owner_ = i;
3257 this->output_section_ = o;
3258 this->state_ = FINDING_STUB_SECTION;
3259 this->group_size_ = group_size;
3260 this->group_start_addr_ = start_addr;
3261 return true;
3262 }
3263 else if (!this->multi_os_ && this->output_section_ != o)
3264 ;
3265 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 3266 {
a5018ae5 3267 // Can we add this section, which is after the stubs, to the
57f6d32d 3268 // group?
a5018ae5 3269 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3270 return true;
ec661b9d 3271 }
a5018ae5 3272 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 3273 {
a5018ae5
AM
3274 if ((whole_sec && this->output_section_ == o)
3275 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3276 {
a5018ae5 3277 // Stubs are added at the end of "owner_".
57f6d32d
AM
3278 this->owner_ = i;
3279 this->output_section_ = o;
a5018ae5 3280 return true;
57f6d32d 3281 }
a5018ae5
AM
3282 // The group before the stubs has reached maximum size.
3283 // Now see about adding sections after the stubs to the
3284 // group. If the current section has a 14-bit branch and
3285 // the group before the stubs exceeds group_size_ (because
3286 // they didn't have 14-bit branches), don't add sections
3287 // after the stubs: The size of stubs for such a large
3288 // group may exceed the reach of a 14-bit branch.
3289 if (!this->stubs_always_after_branch_
3290 && this_size <= this->group_size_
3291 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 3292 {
a5018ae5
AM
3293 gold_debug(DEBUG_TARGET, "adding after stubs");
3294 this->state_ = HAS_STUB_SECTION;
3295 this->group_start_addr_ = start_addr;
57f6d32d
AM
3296 return true;
3297 }
ec661b9d 3298 }
a5018ae5
AM
3299 else
3300 gold_unreachable();
57f6d32d 3301
1c3a5fbe
AM
3302 gold_debug(DEBUG_TARGET,
3303 !this->multi_os_ && this->output_section_ != o
3304 ? "nope, new output section\n"
3305 : "nope, didn't fit\n");
afe002dd 3306
57f6d32d
AM
3307 // The section fails to fit in the current group. Set up a few
3308 // things for the next group. owner_ and output_section_ will be
3309 // set later after we've retrieved those values for the current
3310 // group.
3311 this->state_ = FINDING_STUB_SECTION;
8a37735f 3312 this->group_size_ = group_size;
a5018ae5 3313 this->group_start_addr_ = start_addr;
57f6d32d 3314 return false;
ec661b9d
AM
3315}
3316
3317// Look over all the input sections, deciding where to place stubs.
3318
3319template<int size, bool big_endian>
3320void
3321Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
3322 const Task*,
3323 bool no_size_errors)
ec661b9d 3324{
1c3a5fbe
AM
3325 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3326 parameters->options().stub_group_multi());
ec661b9d
AM
3327
3328 // Group input sections and insert stub table
a3e60ddb
AM
3329 Stub_table_owner* table_owner = NULL;
3330 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
3331 Layout::Section_list section_list;
3332 layout->get_executable_sections(&section_list);
3333 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
3334 for (Layout::Section_list::iterator o = section_list.begin();
3335 o != section_list.end();
ec661b9d
AM
3336 ++o)
3337 {
3338 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
3339 for (Input_section_list::const_iterator i
3340 = (*o)->input_sections().begin();
3341 i != (*o)->input_sections().end();
ec661b9d
AM
3342 ++i)
3343 {
a3e60ddb
AM
3344 if (i->is_input_section()
3345 || i->is_relaxed_input_section())
ec661b9d
AM
3346 {
3347 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3348 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3349 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3350 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3351 {
a3e60ddb
AM
3352 table_owner->output_section = stub_control.output_section();
3353 table_owner->owner = stub_control.owner();
a20605cf 3354 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 3355 table_owner = NULL;
ec661b9d 3356 }
a3e60ddb
AM
3357 if (table_owner == NULL)
3358 {
3359 table_owner = new Stub_table_owner;
3360 tables.push_back(table_owner);
3361 }
3362 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
3363 }
3364 }
3365 }
a3e60ddb 3366 if (table_owner != NULL)
0cfdc767 3367 {
a5018ae5
AM
3368 table_owner->output_section = stub_control.output_section();
3369 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
3370 }
3371 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3372 t != tables.end();
3373 ++t)
3374 {
3375 Stub_table<size, big_endian>* stub_table;
3376
3377 if ((*t)->owner->is_input_section())
3378 stub_table = new Stub_table<size, big_endian>(this,
3379 (*t)->output_section,
590b87ff
AM
3380 (*t)->owner,
3381 this->stub_tables_.size());
a3e60ddb
AM
3382 else if ((*t)->owner->is_relaxed_input_section())
3383 stub_table = static_cast<Stub_table<size, big_endian>*>(
3384 (*t)->owner->relaxed_input_section());
0cfdc767 3385 else
a3e60ddb
AM
3386 gold_unreachable();
3387 this->stub_tables_.push_back(stub_table);
3388 delete *t;
0cfdc767 3389 }
ec661b9d
AM
3390}
3391
32f59844 3392template<int size>
a3e60ddb
AM
3393static unsigned long
3394max_branch_delta (unsigned int r_type)
3395{
3396 if (r_type == elfcpp::R_POWERPC_REL14
3397 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3398 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3399 return 1L << 15;
3400 if (r_type == elfcpp::R_POWERPC_REL24
32f59844 3401 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
a3e60ddb
AM
3402 || r_type == elfcpp::R_PPC_PLTREL24
3403 || r_type == elfcpp::R_PPC_LOCAL24PC)
3404 return 1L << 25;
3405 return 0;
3406}
3407
7e57d19e
AM
3408// Return whether this branch is going via a plt call stub.
3409
3410template<int size, bool big_endian>
3411bool
3412Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3413 Powerpc_relobj<size, big_endian>* ppc_object,
3414 unsigned int shndx,
3415 Address offset,
3416 Target_powerpc* target,
3417 Symbol_table* symtab)
3418{
3419 if (this->object_ != ppc_object
3420 || this->shndx_ != shndx
3421 || this->offset_ != offset)
3422 return false;
3423
3424 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3425 if (sym != NULL && sym->is_forwarder())
3426 sym = symtab->resolve_forwards(sym);
2778747c
AM
3427 if (target->replace_tls_get_addr(sym))
3428 sym = target->tls_get_addr_opt();
7e57d19e
AM
3429 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3430 if (gsym != NULL
7ee7ff70
AM
3431 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3432 && !target->is_elfv2_localentry0(gsym))
3433 : (this->object_->local_has_plt_offset(this->r_sym_)
3434 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3435 {
3436 this->tocsave_ = 1;
3437 return true;
3438 }
3439 return false;
3440}
3441
ec661b9d
AM
3442// If this branch needs a plt call stub, or a long branch stub, make one.
3443
3444template<int size, bool big_endian>
a3e60ddb 3445bool
ec661b9d
AM
3446Target_powerpc<size, big_endian>::Branch_info::make_stub(
3447 Stub_table<size, big_endian>* stub_table,
3448 Stub_table<size, big_endian>* ifunc_stub_table,
3449 Symbol_table* symtab) const
3450{
3451 Symbol* sym = this->object_->global_symbol(this->r_sym_);
88b8e639
AM
3452 Target_powerpc<size, big_endian>* target =
3453 static_cast<Target_powerpc<size, big_endian>*>(
3454 parameters->sized_target<size, big_endian>());
34e0882b
AM
3455 if (sym != NULL && sym->is_forwarder())
3456 sym = symtab->resolve_forwards(sym);
2778747c
AM
3457 if (target->replace_tls_get_addr(sym))
3458 sym = target->tls_get_addr_opt();
34e0882b 3459 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
dc60b26d
AM
3460 bool ok = true;
3461
ec661b9d 3462 if (gsym != NULL
88b8e639 3463 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3464 : this->object_->local_has_plt_offset(this->r_sym_))
3465 {
9055360d
AM
3466 if (size == 64
3467 && gsym != NULL
3468 && target->abiversion() >= 2
3469 && !parameters->options().output_is_position_independent()
32f59844 3470 && !is_branch_reloc<size>(this->r_type_))
9055360d
AM
3471 target->glink_section()->add_global_entry(gsym);
3472 else
ec661b9d 3473 {
64b5d6d7
AM
3474 if (stub_table == NULL
3475 && !(size == 32
3476 && gsym != NULL
3477 && !parameters->options().output_is_position_independent()
32f59844 3478 && !is_branch_reloc<size>(this->r_type_)))
9055360d
AM
3479 stub_table = this->object_->stub_table(this->shndx_);
3480 if (stub_table == NULL)
3481 {
64b5d6d7
AM
3482 // This is a ref from a data section to an ifunc symbol,
3483 // or a non-branch reloc for which we always want to use
3484 // one set of stubs for resolving function addresses.
9055360d
AM
3485 stub_table = ifunc_stub_table;
3486 }
3487 gold_assert(stub_table != NULL);
a3e60ddb
AM
3488 Address from = this->object_->get_output_section_offset(this->shndx_);
3489 if (from != invalid_address)
3490 from += (this->object_->output_section(this->shndx_)->address()
3491 + this->offset_);
9055360d 3492 if (gsym != NULL)
dc60b26d
AM
3493 ok = stub_table->add_plt_call_entry(from,
3494 this->object_, gsym,
7e57d19e
AM
3495 this->r_type_, this->addend_,
3496 this->tocsave_);
9055360d 3497 else
dc60b26d
AM
3498 ok = stub_table->add_plt_call_entry(from,
3499 this->object_, this->r_sym_,
7e57d19e
AM
3500 this->r_type_, this->addend_,
3501 this->tocsave_);
ec661b9d 3502 }
ec661b9d
AM
3503 }
3504 else
3505 {
32f59844 3506 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
a3e60ddb
AM
3507 if (max_branch_offset == 0)
3508 return true;
ec661b9d
AM
3509 Address from = this->object_->get_output_section_offset(this->shndx_);
3510 gold_assert(from != invalid_address);
3511 from += (this->object_->output_section(this->shndx_)->address()
3512 + this->offset_);
3513 Address to;
3514 if (gsym != NULL)
3515 {
3516 switch (gsym->source())
3517 {
3518 case Symbol::FROM_OBJECT:
3519 {
3520 Object* symobj = gsym->object();
3521 if (symobj->is_dynamic()
3522 || symobj->pluginobj() != NULL)
a3e60ddb 3523 return true;
ec661b9d
AM
3524 bool is_ordinary;
3525 unsigned int shndx = gsym->shndx(&is_ordinary);
3526 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3527 return true;
ec661b9d
AM
3528 }
3529 break;
3530
3531 case Symbol::IS_UNDEFINED:
a3e60ddb 3532 return true;
ec661b9d
AM
3533
3534 default:
3535 break;
3536 }
3537 Symbol_table::Compute_final_value_status status;
3538 to = symtab->compute_final_value<size>(gsym, &status);
3539 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3540 return true;
9055360d
AM
3541 if (size == 64)
3542 to += this->object_->ppc64_local_entry_offset(gsym);
ec661b9d
AM
3543 }
3544 else
3545 {
3546 const Symbol_value<size>* psymval
3547 = this->object_->local_symbol(this->r_sym_);
3548 Symbol_value<size> symval;
0f125432
CC
3549 if (psymval->is_section_symbol())
3550 symval.set_is_section_symbol();
ec661b9d
AM
3551 typedef Sized_relobj_file<size, big_endian> ObjType;
3552 typename ObjType::Compute_final_local_value_status status
3553 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3554 &symval, symtab);
3555 if (status != ObjType::CFLV_OK
3556 || !symval.has_output_value())
a3e60ddb 3557 return true;
ec661b9d 3558 to = symval.value(this->object_, 0);
9055360d
AM
3559 if (size == 64)
3560 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
ec661b9d 3561 }
cbcb23fa
AM
3562 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3563 to += this->addend_;
ec661b9d
AM
3564 if (stub_table == NULL)
3565 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3566 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3567 {
3568 unsigned int dest_shndx;
1611bc4a
AM
3569 if (!target->symval_for_branch(symtab, gsym, this->object_,
3570 &to, &dest_shndx))
3571 return true;
ec661b9d
AM
3572 }
3573 Address delta = to - from;
32f59844
AM
3574 if (delta + max_branch_offset >= 2 * max_branch_offset
3575 || (size == 64
3576 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3577 && (gsym != NULL
3578 ? this->object_->ppc64_needs_toc(gsym)
3579 : this->object_->ppc64_needs_toc(this->r_sym_))))
ec661b9d 3580 {
0cfdc767
AM
3581 if (stub_table == NULL)
3582 {
3583 gold_warning(_("%s:%s: branch in non-executable section,"
3584 " no long branch stub for you"),
3585 this->object_->name().c_str(),
3586 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3587 return true;
0cfdc767 3588 }
d49044c7
AM
3589 bool save_res = (size == 64
3590 && gsym != NULL
3591 && gsym->source() == Symbol::IN_OUTPUT_DATA
3592 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3593 ok = stub_table->add_long_branch_entry(this->object_,
3594 this->r_type_,
3595 from, to, save_res);
ec661b9d
AM
3596 }
3597 }
dc60b26d
AM
3598 if (!ok)
3599 gold_debug(DEBUG_TARGET,
3600 "branch at %s:%s+%#lx\n"
3601 "can't reach stub attached to %s:%s",
3602 this->object_->name().c_str(),
3603 this->object_->section_name(this->shndx_).c_str(),
3604 (unsigned long) this->offset_,
3605 stub_table->relobj()->name().c_str(),
3606 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3607
3608 return ok;
ec661b9d
AM
3609}
3610
3611// Relaxation hook. This is where we do stub generation.
3612
3613template<int size, bool big_endian>
3614bool
3615Target_powerpc<size, big_endian>::do_relax(int pass,
3616 const Input_objects*,
3617 Symbol_table* symtab,
3618 Layout* layout,
3619 const Task* task)
3620{
3621 unsigned int prev_brlt_size = 0;
3622 if (pass == 1)
ec661b9d 3623 {
b4f7960d
AM
3624 bool thread_safe
3625 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3626 if (size == 64
3627 && this->abiversion() < 2
3628 && !thread_safe
3629 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3630 {
e2458743 3631 static const char* const thread_starter[] =
9e69ed50
AM
3632 {
3633 "pthread_create",
3634 /* libstdc++ */
3635 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3636 /* librt */
3637 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3638 "mq_notify", "create_timer",
3639 /* libanl */
3640 "getaddrinfo_a",
3641 /* libgomp */
80272b8c 3642 "GOMP_parallel",
9e69ed50 3643 "GOMP_parallel_start",
80272b8c 3644 "GOMP_parallel_loop_static",
9e69ed50 3645 "GOMP_parallel_loop_static_start",
80272b8c 3646 "GOMP_parallel_loop_dynamic",
9e69ed50 3647 "GOMP_parallel_loop_dynamic_start",
80272b8c 3648 "GOMP_parallel_loop_guided",
9e69ed50 3649 "GOMP_parallel_loop_guided_start",
80272b8c 3650 "GOMP_parallel_loop_runtime",
9e69ed50 3651 "GOMP_parallel_loop_runtime_start",
80272b8c 3652 "GOMP_parallel_sections",
43819297 3653 "GOMP_parallel_sections_start",
f9dffbf0
AM
3654 /* libgo */
3655 "__go_go",
9e69ed50
AM
3656 };
3657
e2458743
AM
3658 if (parameters->options().shared())
3659 thread_safe = true;
3660 else
9e69ed50 3661 {
e2458743
AM
3662 for (unsigned int i = 0;
3663 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3664 i++)
3665 {
3666 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3667 thread_safe = (sym != NULL
3668 && sym->in_reg()
3669 && sym->in_real_elf());
3670 if (thread_safe)
3671 break;
3672 }
9e69ed50 3673 }
ec661b9d 3674 }
9e69ed50 3675 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3676 }
3677
3678 if (pass == 1)
3679 {
3680 this->stub_group_size_ = parameters->options().stub_group_size();
3681 bool no_size_errors = true;
3682 if (this->stub_group_size_ == 1)
3683 this->stub_group_size_ = 0x1c00000;
3684 else if (this->stub_group_size_ == -1)
3685 this->stub_group_size_ = -0x1e00000;
3686 else
3687 no_size_errors = false;
3688 this->group_sections(layout, task, no_size_errors);
3689 }
3690 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3691 {
3692 this->branch_lookup_table_.clear();
3693 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3694 p != this->stub_tables_.end();
3695 ++p)
3696 {
3697 (*p)->clear_stubs(true);
3698 }
3699 this->stub_tables_.clear();
3700 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3701 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3702 program_name, this->stub_group_size_);
3703 this->group_sections(layout, task, true);
ec661b9d
AM
3704 }
3705
3706 // We need address of stub tables valid for make_stub.
3707 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3708 p != this->stub_tables_.end();
3709 ++p)
3710 {
3711 const Powerpc_relobj<size, big_endian>* object
3712 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3713 Address off = object->get_output_section_offset((*p)->shndx());
3714 gold_assert(off != invalid_address);
3715 Output_section* os = (*p)->output_section();
3716 (*p)->set_address_and_size(os, off);
3717 }
3718
9e69ed50
AM
3719 if (pass != 1)
3720 {
3721 // Clear plt call stubs, long branch stubs and branch lookup table.
3722 prev_brlt_size = this->branch_lookup_table_.size();
3723 this->branch_lookup_table_.clear();
3724 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3725 p != this->stub_tables_.end();
3726 ++p)
3727 {
a3e60ddb 3728 (*p)->clear_stubs(false);
9e69ed50
AM
3729 }
3730 }
3731
3732 // Build all the stubs.
a3e60ddb 3733 this->relax_failed_ = false;
ec661b9d
AM
3734 Stub_table<size, big_endian>* ifunc_stub_table
3735 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3736 Stub_table<size, big_endian>* one_stub_table
3737 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3738 for (typename Branches::const_iterator b = this->branch_info_.begin();
3739 b != this->branch_info_.end();
3740 b++)
3741 {
a3e60ddb
AM
3742 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3743 && !this->relax_failed_)
3744 {
3745 this->relax_failed_ = true;
3746 this->relax_fail_count_++;
3747 if (this->relax_fail_count_ < 3)
3748 return true;
3749 }
ec661b9d 3750 }
32f59844
AM
3751 bool do_resize = false;
3752 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3753 p != this->stub_tables_.end();
3754 ++p)
3755 if ((*p)->need_resize())
3756 {
3757 do_resize = true;
3758 break;
3759 }
3760 if (do_resize)
3761 {
3762 this->branch_lookup_table_.clear();
3763 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3764 p != this->stub_tables_.end();
3765 ++p)
3766 (*p)->set_resizing(true);
3767 for (typename Branches::const_iterator b = this->branch_info_.begin();
3768 b != this->branch_info_.end();
3769 b++)
3770 {
3771 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3772 && !this->relax_failed_)
3773 {
3774 this->relax_failed_ = true;
3775 this->relax_fail_count_++;
3776 if (this->relax_fail_count_ < 3)
3777 return true;
3778 }
3779 }
3780 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3781 p != this->stub_tables_.end();
3782 ++p)
3783 (*p)->set_resizing(false);
3784 }
ec661b9d 3785
9e69ed50 3786 // Did anything change size?
ec661b9d
AM
3787 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3788 bool again = num_huge_branches != prev_brlt_size;
3789 if (size == 64 && num_huge_branches != 0)
3790 this->make_brlt_section(layout);
3791 if (size == 64 && again)
3792 this->brlt_section_->set_current_size(num_huge_branches);
3793
be897fb7
AM
3794 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3795 p != this->stub_tables_.rend();
3796 ++p)
3797 (*p)->remove_eh_frame(layout);
3798
3799 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3800 p != this->stub_tables_.end();
3801 ++p)
3802 (*p)->add_eh_frame(layout);
3803
ec661b9d
AM
3804 typedef Unordered_set<Output_section*> Output_sections;
3805 Output_sections os_need_update;
3806 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3807 p != this->stub_tables_.end();
3808 ++p)
3809 {
3810 if ((*p)->size_update())
3811 {
3812 again = true;
3813 os_need_update.insert((*p)->output_section());
3814 }
3815 }
3816
9e69ed50
AM
3817 // Set output section offsets for all input sections in an output
3818 // section that just changed size. Anything past the stubs will
3819 // need updating.
ec661b9d
AM
3820 for (typename Output_sections::iterator p = os_need_update.begin();
3821 p != os_need_update.end();
3822 p++)
3823 {
3824 Output_section* os = *p;
3825 Address off = 0;
3826 typedef Output_section::Input_section_list Input_section_list;
3827 for (Input_section_list::const_iterator i = os->input_sections().begin();
3828 i != os->input_sections().end();
3829 ++i)
3830 {
3831 off = align_address(off, i->addralign());
3832 if (i->is_input_section() || i->is_relaxed_input_section())
3833 i->relobj()->set_section_offset(i->shndx(), off);
3834 if (i->is_relaxed_input_section())
3835 {
3836 Stub_table<size, big_endian>* stub_table
3837 = static_cast<Stub_table<size, big_endian>*>(
3838 i->relaxed_input_section());
6395d38b
HS
3839 Address stub_table_size = stub_table->set_address_and_size(os, off);
3840 off += stub_table_size;
3841 // After a few iterations, set current stub table size
3842 // as min size threshold, so later stub tables can only
3843 // grow in size.
3844 if (pass >= 4)
3845 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3846 }
3847 else
3848 off += i->data_size();
3849 }
6830ee24
AM
3850 // If .branch_lt is part of this output section, then we have
3851 // just done the offset adjustment.
ec661b9d
AM
3852 os->clear_section_offsets_need_adjustment();
3853 }
3854
3855 if (size == 64
3856 && !again
3857 && num_huge_branches != 0
3858 && parameters->options().output_is_position_independent())
3859 {
3860 // Fill in the BRLT relocs.
06f30c9d 3861 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3862 for (typename Branch_lookup_table::const_iterator p
3863 = this->branch_lookup_table_.begin();
3864 p != this->branch_lookup_table_.end();
3865 ++p)
3866 {
3867 this->brlt_section_->add_reloc(p->first, p->second);
3868 }
06f30c9d 3869 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3870 }
590b87ff
AM
3871
3872 if (!again
3873 && (parameters->options().user_set_emit_stub_syms()
3874 ? parameters->options().emit_stub_syms()
3875 : (size == 64
3876 || parameters->options().output_is_position_independent()
3877 || parameters->options().emit_relocs())))
3878 {
3879 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3880 p != this->stub_tables_.end();
3881 ++p)
3882 (*p)->define_stub_syms(symtab);
3883
3884 if (this->glink_ != NULL)
3885 {
9e390558 3886 int stub_size = this->glink_->pltresolve_size();
590b87ff
AM
3887 Address value = -stub_size;
3888 if (size == 64)
3889 {
3890 value = 8;
3891 stub_size -= 8;
3892 }
3893 this->define_local(symtab, "__glink_PLTresolve",
3894 this->glink_, value, stub_size);
3895
3896 if (size != 64)
3897 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3898 }
3899 }
3900
ec661b9d
AM
3901 return again;
3902}
3903
9d5781f8
AM
3904template<int size, bool big_endian>
3905void
3906Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3907 unsigned char* oview,
3908 uint64_t* paddress,
3909 off_t* plen) const
3910{
3911 uint64_t address = plt->address();
3912 off_t len = plt->data_size();
3913
3914 if (plt == this->glink_)
3915 {
3916 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3917 if (len == 0)
3918 {
3919 gold_assert(parameters->doing_static_link());
3920 // Static linking may need stubs, to support ifunc and long
3921 // branches. We need to create an output section for
3922 // .eh_frame early in the link process, to have a place to
3923 // attach stub .eh_frame info. We also need to have
3924 // registered a CIE that matches the stub CIE. Both of
3925 // these requirements are satisfied by creating an FDE and
3926 // CIE for .glink, even though static linking will leave
3927 // .glink zero length.
3928 // ??? Hopefully generating an FDE with a zero address range
3929 // won't confuse anything that consumes .eh_frame info.
3930 }
3931 else if (size == 64)
9d5781f8
AM
3932 {
3933 // There is one word before __glink_PLTresolve
3934 address += 8;
3935 len -= 8;
3936 }
3937 else if (parameters->options().output_is_position_independent())
3938 {
3939 // There are two FDEs for a position independent glink.
3940 // The first covers the branch table, the second
3941 // __glink_PLTresolve at the end of glink.
9e390558 3942 off_t resolve_size = this->glink_->pltresolve_size();
5fe7ffdc 3943 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3944 len -= resolve_size;
3945 else
3946 {
3947 address += len - resolve_size;
3948 len = resolve_size;
3949 }
3950 }
3951 }
3952 else
3953 {
3954 // Must be a stub table.
3955 const Stub_table<size, big_endian>* stub_table
3956 = static_cast<const Stub_table<size, big_endian>*>(plt);
3957 uint64_t stub_address = stub_table->stub_address();
3958 len -= stub_address - address;
3959 address = stub_address;
3960 }
3961
3962 *paddress = address;
3963 *plen = len;
3964}
3965
42cacb20
DE
3966// A class to handle the PLT data.
3967
3968template<int size, bool big_endian>
cf43a2fe 3969class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3970{
3971 public:
3972 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3973 size, big_endian> Reloc_section;
3974
e5d5f5ed
AM
3975 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3976 Reloc_section* plt_rel,
e5d5f5ed
AM
3977 const char* name)
3978 : Output_section_data_build(size == 32 ? 4 : 8),
3979 rel_(plt_rel),
3980 targ_(targ),
e5d5f5ed
AM
3981 name_(name)
3982 { }
42cacb20
DE
3983
3984 // Add an entry to the PLT.
03e25981 3985 void
cf43a2fe 3986 add_entry(Symbol*);
42cacb20 3987
03e25981 3988 void
e5d5f5ed
AM
3989 add_ifunc_entry(Symbol*);
3990
2d7ad24e
AM
3991 void
3992 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3993
03e25981 3994 void
e5d5f5ed
AM
3995 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3996
42cacb20 3997 // Return the .rela.plt section data.
e5d5f5ed 3998 Reloc_section*
cf43a2fe
AM
3999 rel_plt() const
4000 {
42cacb20
DE
4001 return this->rel_;
4002 }
4003
0e70b911
CC
4004 // Return the number of PLT entries.
4005 unsigned int
4006 entry_count() const
d83ce4e3 4007 {
b3ccdeb5
AM
4008 if (this->current_data_size() == 0)
4009 return 0;
b4f7960d
AM
4010 return ((this->current_data_size() - this->first_plt_entry_offset())
4011 / this->plt_entry_size());
d83ce4e3 4012 }
0e70b911 4013
42cacb20 4014 protected:
42cacb20 4015 void
cf43a2fe 4016 do_adjust_output_section(Output_section* os)
42cacb20 4017 {
cf43a2fe 4018 os->set_entsize(0);
42cacb20
DE
4019 }
4020
6ce78956
AM
4021 // Write to a map file.
4022 void
4023 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 4024 { mapfile->print_output_data(this, this->name_); }
6ce78956 4025
cf43a2fe 4026 private:
b4f7960d
AM
4027 // Return the offset of the first non-reserved PLT entry.
4028 unsigned int
4029 first_plt_entry_offset() const
4030 {
2d7ad24e
AM
4031 // IPLT and LPLT have no reserved entry.
4032 if (this->name_[3] == 'I' || this->name_[3] == 'L')
b4f7960d
AM
4033 return 0;
4034 return this->targ_->first_plt_entry_offset();
4035 }
4036
4037 // Return the size of each PLT entry.
4038 unsigned int
4039 plt_entry_size() const
4040 {
4041 return this->targ_->plt_entry_size();
4042 }
cf43a2fe 4043
42cacb20
DE
4044 // Write out the PLT data.
4045 void
4046 do_write(Output_file*);
4047
4048 // The reloc section.
4049 Reloc_section* rel_;
cf43a2fe
AM
4050 // Allows access to .glink for do_write.
4051 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
4052 // What to report in map file.
4053 const char *name_;
42cacb20
DE
4054};
4055
e5d5f5ed 4056// Add an entry to the PLT.
42cacb20
DE
4057
4058template<int size, bool big_endian>
03e25981 4059void
e5d5f5ed 4060Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 4061{
e5d5f5ed
AM
4062 if (!gsym->has_plt_offset())
4063 {
ec661b9d 4064 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4065 if (off == 0)
4066 off += this->first_plt_entry_offset();
4067 gsym->set_plt_offset(off);
4068 gsym->set_needs_dynsym_entry();
4069 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4070 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 4071 off += this->plt_entry_size();
e5d5f5ed
AM
4072 this->set_current_data_size(off);
4073 }
42cacb20
DE
4074}
4075
e5d5f5ed 4076// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
4077
4078template<int size, bool big_endian>
03e25981 4079void
e5d5f5ed 4080Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 4081{
cf43a2fe
AM
4082 if (!gsym->has_plt_offset())
4083 {
ec661b9d 4084 section_size_type off = this->current_data_size();
cf43a2fe 4085 gsym->set_plt_offset(off);
e5d5f5ed 4086 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4087 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4088 dynrel = elfcpp::R_PPC64_JMP_IREL;
4089 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 4090 off += this->plt_entry_size();
e5d5f5ed
AM
4091 this->set_current_data_size(off);
4092 }
4093}
4094
2d7ad24e
AM
4095// Add an entry for a local symbol to the PLT.
4096
4097template<int size, bool big_endian>
4098void
4099Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4100 Sized_relobj_file<size, big_endian>* relobj,
4101 unsigned int local_sym_index)
4102{
4103 if (!relobj->local_has_plt_offset(local_sym_index))
4104 {
4105 section_size_type off = this->current_data_size();
4106 relobj->set_local_plt_offset(local_sym_index, off);
4107 if (this->rel_)
4108 {
4109 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4110 if (size == 64 && this->targ_->abiversion() < 2)
4111 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4112 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4113 dynrel, this, off, 0);
4114 }
4115 off += this->plt_entry_size();
4116 this->set_current_data_size(off);
4117 }
4118}
4119
e5d5f5ed
AM
4120// Add an entry for a local ifunc symbol to the IPLT.
4121
4122template<int size, bool big_endian>
03e25981 4123void
e5d5f5ed
AM
4124Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4125 Sized_relobj_file<size, big_endian>* relobj,
4126 unsigned int local_sym_index)
4127{
4128 if (!relobj->local_has_plt_offset(local_sym_index))
4129 {
ec661b9d 4130 section_size_type off = this->current_data_size();
e5d5f5ed
AM
4131 relobj->set_local_plt_offset(local_sym_index, off);
4132 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 4133 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
4134 dynrel = elfcpp::R_PPC64_JMP_IREL;
4135 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4136 this, off, 0);
b4f7960d 4137 off += this->plt_entry_size();
cf43a2fe
AM
4138 this->set_current_data_size(off);
4139 }
42cacb20
DE
4140}
4141
dd93cd0a 4142static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 4143static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 4144static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
4145static const uint32_t add_3_3_2 = 0x7c631214;
4146static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
4147static const uint32_t add_3_12_2 = 0x7c6c1214;
4148static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 4149static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
4150static const uint32_t add_11_2_11 = 0x7d625a14;
4151static const uint32_t add_11_11_2 = 0x7d6b1214;
32f59844 4152static const uint32_t add_12_11_12 = 0x7d8b6214;
b4f7960d 4153static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 4154static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 4155static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 4156static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 4157static const uint32_t addi_12_1 = 0x39810000;
32f59844 4158static const uint32_t addi_12_11 = 0x398b0000;
b4f7960d 4159static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
4160static const uint32_t addis_0_2 = 0x3c020000;
4161static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 4162static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 4163static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
4164static const uint32_t addis_11_11 = 0x3d6b0000;
4165static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 4166static const uint32_t addis_12_1 = 0x3d810000;
397998fc 4167static const uint32_t addis_12_2 = 0x3d820000;
32f59844 4168static const uint32_t addis_12_11 = 0x3d8b0000;
c9269dff 4169static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
4170static const uint32_t b = 0x48000000;
4171static const uint32_t bcl_20_31 = 0x429f0005;
4172static const uint32_t bctr = 0x4e800420;
34e0882b
AM
4173static const uint32_t bctrl = 0x4e800421;
4174static const uint32_t beqlr = 0x4d820020;
f3a0ed29 4175static const uint32_t blr = 0x4e800020;
9e69ed50 4176static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 4177static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 4178static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
4179static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4180static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
4181static const uint32_t cror_15_15_15 = 0x4def7b82;
4182static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29
AM
4183static const uint32_t ld_0_1 = 0xe8010000;
4184static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 4185static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 4186static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 4187static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 4188static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 4189static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 4190static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 4191static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
4192static const uint32_t ld_11_11 = 0xe96b0000;
4193static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 4194static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 4195static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 4196static const uint32_t ld_12_12 = 0xe98c0000;
32f59844 4197static const uint32_t ldx_12_11_12 = 0x7d8b602a;
f3a0ed29 4198static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 4199static const uint32_t li_0_0 = 0x38000000;
e4dff765 4200static const uint32_t li_11_0 = 0x39600000;
f3a0ed29 4201static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 4202static const uint32_t lis_0 = 0x3c000000;
549dba71 4203static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
4204static const uint32_t lis_11 = 0x3d600000;
4205static const uint32_t lis_12 = 0x3d800000;
b4f7960d 4206static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 4207static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 4208static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
4209static const uint32_t lwz_11_11 = 0x816b0000;
4210static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 4211static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 4212static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 4213static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 4214static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 4215static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 4216static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
4217static const uint32_t mr_0_3 = 0x7c601b78;
4218static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
4219static const uint32_t mtctr_0 = 0x7c0903a6;
4220static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 4221static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 4222static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 4223static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 4224static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 4225static const uint32_t nop = 0x60000000;
c9269dff 4226static const uint32_t ori_0_0_0 = 0x60000000;
e4dff765 4227static const uint32_t ori_11_11_0 = 0x616b0000;
32f59844
AM
4228static const uint32_t ori_12_12_0 = 0x618c0000;
4229static const uint32_t oris_12_12_0 = 0x658c0000;
e4dff765 4230static const uint32_t sldi_11_11_34 = 0x796b1746;
32f59844 4231static const uint32_t sldi_12_12_32 = 0x799c07c6;
b4f7960d 4232static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
4233static const uint32_t std_0_1 = 0xf8010000;
4234static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 4235static const uint32_t std_2_1 = 0xf8410000;
34e0882b 4236static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
4237static const uint32_t stfd_0_1 = 0xd8010000;
4238static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 4239static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
4240static const uint32_t sub_12_12_11 = 0x7d8b6050;
4241static const uint32_t xor_2_12_12 = 0x7d826278;
4242static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20 4243
e4dff765
AM
4244static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4245static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4246static const uint64_t pnop = 0x0700000000000000ULL;
4247
42cacb20
DE
4248// Write out the PLT.
4249
4250template<int size, bool big_endian>
4251void
4252Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4253{
2d7ad24e 4254 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
cf43a2fe 4255 {
ec661b9d 4256 const section_size_type offset = this->offset();
cf43a2fe
AM
4257 const section_size_type oview_size
4258 = convert_to_section_size_type(this->data_size());
4259 unsigned char* const oview = of->get_output_view(offset, oview_size);
4260 unsigned char* pov = oview;
4261 unsigned char* endpov = oview + oview_size;
4262
e5d5f5ed 4263 // The address of the .glink branch table
cf43a2fe
AM
4264 const Output_data_glink<size, big_endian>* glink
4265 = this->targ_->glink_section();
ec661b9d 4266 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
4267
4268 while (pov < endpov)
4269 {
4270 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4271 pov += 4;
4272 branch_tab += 4;
4273 }
4274
4275 of->write_output_view(offset, oview_size, oview);
4276 }
4277}
4278
4279// Create the PLT section.
4280
4281template<int size, bool big_endian>
4282void
40b469d7
AM
4283Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4284 Layout* layout)
cf43a2fe
AM
4285{
4286 if (this->plt_ == NULL)
4287 {
40b469d7
AM
4288 if (this->got_ == NULL)
4289 this->got_section(symtab, layout);
4290
cf43a2fe
AM
4291 if (this->glink_ == NULL)
4292 make_glink_section(layout);
4293
4294 // Ensure that .rela.dyn always appears before .rela.plt This is
4295 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 4296 // needs to include .rela.plt in its range.
cf43a2fe
AM
4297 this->rela_dyn_section(layout);
4298
e5d5f5ed
AM
4299 Reloc_section* plt_rel = new Reloc_section(false);
4300 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4301 elfcpp::SHF_ALLOC, plt_rel,
4302 ORDER_DYNAMIC_PLT_RELOCS, false);
4303 this->plt_
4304 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 4305 "** PLT");
cf43a2fe
AM
4306 layout->add_output_section_data(".plt",
4307 (size == 32
4308 ? elfcpp::SHT_PROGBITS
4309 : elfcpp::SHT_NOBITS),
4310 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4311 this->plt_,
4312 (size == 32
4313 ? ORDER_SMALL_DATA
4314 : ORDER_SMALL_BSS),
4315 false);
3254d32c
AM
4316
4317 Output_section* rela_plt_os = plt_rel->output_section();
4318 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
4319 }
4320}
4321
e5d5f5ed
AM
4322// Create the IPLT section.
4323
4324template<int size, bool big_endian>
4325void
40b469d7
AM
4326Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4327 Layout* layout)
e5d5f5ed
AM
4328{
4329 if (this->iplt_ == NULL)
4330 {
40b469d7 4331 this->make_plt_section(symtab, layout);
2d7ad24e 4332 this->make_lplt_section(layout);
e5d5f5ed
AM
4333
4334 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
4335 if (this->rela_dyn_->output_section())
4336 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
4337 this->iplt_
4338 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 4339 "** IPLT");
6528b6eb
AM
4340 if (this->plt_->output_section())
4341 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
4342 }
4343}
4344
2d7ad24e
AM
4345// Create the LPLT section.
4346
4347template<int size, bool big_endian>
4348void
4349Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4350{
4351 if (this->lplt_ == NULL)
4352 {
4353 Reloc_section* lplt_rel = NULL;
4354 if (parameters->options().output_is_position_independent())
4355 {
4356 lplt_rel = new Reloc_section(false);
4357 this->rela_dyn_section(layout);
4358 if (this->rela_dyn_->output_section())
4359 this->rela_dyn_->output_section()
4360 ->add_output_section_data(lplt_rel);
4361 }
4362 this->lplt_
4363 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4364 "** LPLT");
4365 this->make_brlt_section(layout);
4366 if (this->brlt_section_ && this->brlt_section_->output_section())
4367 this->brlt_section_->output_section()
4368 ->add_output_section_data(this->lplt_);
4369 else
4370 layout->add_output_section_data(".branch_lt",
4371 elfcpp::SHT_PROGBITS,
4372 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4373 this->lplt_,
4374 ORDER_RELRO,
4375 true);
4376 }
4377}
4378
ec661b9d 4379// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
4380
4381template<int size, bool big_endian>
ec661b9d 4382class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
4383{
4384 public:
ec661b9d
AM
4385 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4386 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4387 size, big_endian> Reloc_section;
c9269dff 4388
ec661b9d
AM
4389 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4390 Reloc_section* brlt_rel)
4391 : Output_section_data_build(size == 32 ? 4 : 8),
4392 rel_(brlt_rel),
4393 targ_(targ)
4394 { }
cf43a2fe 4395
06f30c9d
CC
4396 void
4397 reset_brlt_sizes()
4398 {
4399 this->reset_data_size();
4400 this->rel_->reset_data_size();
4401 }
4402
4403 void
4404 finalize_brlt_sizes()
4405 {
4406 this->finalize_data_size();
4407 this->rel_->finalize_data_size();
4408 }
4409
ec661b9d 4410 // Add a reloc for an entry in the BRLT.
cf43a2fe 4411 void
ec661b9d
AM
4412 add_reloc(Address to, unsigned int off)
4413 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 4414
ec661b9d 4415 // Update section and reloc section size.
e5d5f5ed 4416 void
ec661b9d
AM
4417 set_current_size(unsigned int num_branches)
4418 {
4419 this->reset_address_and_file_offset();
4420 this->set_current_data_size(num_branches * 16);
4421 this->finalize_data_size();
4422 Output_section* os = this->output_section();
4423 os->set_section_offsets_need_adjustment();
4424 if (this->rel_ != NULL)
4425 {
0e123f69 4426 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
4427 this->rel_->reset_address_and_file_offset();
4428 this->rel_->set_current_data_size(num_branches * reloc_size);
4429 this->rel_->finalize_data_size();
4430 Output_section* os = this->rel_->output_section();
4431 os->set_section_offsets_need_adjustment();
4432 }
4433 }
cf43a2fe 4434
ec661b9d
AM
4435 protected:
4436 void
4437 do_adjust_output_section(Output_section* os)
4438 {
4439 os->set_entsize(0);
4440 }
e5d5f5ed 4441
ec661b9d
AM
4442 // Write to a map file.
4443 void
4444 do_print_to_mapfile(Mapfile* mapfile) const
4445 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4446
ec661b9d
AM
4447 private:
4448 // Write out the BRLT data.
4449 void
4450 do_write(Output_file*);
c9824451 4451
ec661b9d
AM
4452 // The reloc section.
4453 Reloc_section* rel_;
4454 Target_powerpc<size, big_endian>* targ_;
4455};
cf43a2fe 4456
ec661b9d
AM
4457// Make the branch lookup table section.
4458
4459template<int size, bool big_endian>
4460void
4461Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4462{
4463 if (size == 64 && this->brlt_section_ == NULL)
4464 {
4465 Reloc_section* brlt_rel = NULL;
4466 bool is_pic = parameters->options().output_is_position_independent();
4467 if (is_pic)
4468 {
54483898
AM
4469 // When PIC we can't fill in .branch_lt but must initialise at
4470 // runtime via dynamic relocations.
ec661b9d
AM
4471 this->rela_dyn_section(layout);
4472 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4473 if (this->rela_dyn_->output_section())
4474 this->rela_dyn_->output_section()
4475 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4476 }
4477 this->brlt_section_
4478 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4479 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4480 this->plt_->output_section()
4481 ->add_output_section_data(this->brlt_section_);
4482 else
6830ee24 4483 layout->add_output_section_data(".branch_lt",
54483898 4484 elfcpp::SHT_PROGBITS,
ec661b9d
AM
4485 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4486 this->brlt_section_,
54483898
AM
4487 ORDER_RELRO,
4488 true);
ec661b9d
AM
4489 }
4490}
4491
6830ee24 4492// Write out .branch_lt when non-PIC.
ec661b9d
AM
4493
4494template<int size, bool big_endian>
4495void
4496Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4497{
4498 if (size == 64 && !parameters->options().output_is_position_independent())
4499 {
4500 const section_size_type offset = this->offset();
4501 const section_size_type oview_size
4502 = convert_to_section_size_type(this->data_size());
4503 unsigned char* const oview = of->get_output_view(offset, oview_size);
4504
4505 this->targ_->write_branch_lookup_table(oview);
4506 of->write_output_view(offset, oview_size, oview);
4507 }
4508}
4509
9e69ed50
AM
4510static inline uint32_t
4511l(uint32_t a)
4512{
4513 return a & 0xffff;
4514}
4515
4516static inline uint32_t
4517hi(uint32_t a)
4518{
4519 return l(a >> 16);
4520}
4521
4522static inline uint32_t
4523ha(uint32_t a)
4524{
4525 return hi(a + 0x8000);
4526}
4527
e4dff765
AM
4528static inline uint64_t
4529d34(uint64_t v)
4530{
4531 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4532}
4533
4534static inline uint64_t
4535ha34(uint64_t v)
4536{
4537 return (v + (1ULL << 33)) >> 34;
4538}
4539
9d5781f8
AM
4540template<int size>
4541struct Eh_cie
4542{
4543 static const unsigned char eh_frame_cie[12];
4544};
4545
4546template<int size>
4547const unsigned char Eh_cie<size>::eh_frame_cie[] =
4548{
4549 1, // CIE version.
4550 'z', 'R', 0, // Augmentation string.
4551 4, // Code alignment.
4552 0x80 - size / 8 , // Data alignment.
4553 65, // RA reg.
4554 1, // Augmentation size.
4555 (elfcpp::DW_EH_PE_pcrel
4556 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4557 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4558};
4559
b4f7960d
AM
4560// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4561static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4562{
4563 0, 0, 0, 0, // Replaced with offset to .glink.
4564 0, 0, 0, 0, // Replaced with size of .glink.
4565 0, // Augmentation size.
4566 elfcpp::DW_CFA_advance_loc + 1,
4567 elfcpp::DW_CFA_register, 65, 12,
15a3a14f 4568 elfcpp::DW_CFA_advance_loc + 5,
9d5781f8
AM
4569 elfcpp::DW_CFA_restore_extended, 65
4570};
4571
b4f7960d
AM
4572// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4573static const unsigned char glink_eh_frame_fde_64v2[] =
4574{
4575 0, 0, 0, 0, // Replaced with offset to .glink.
4576 0, 0, 0, 0, // Replaced with size of .glink.
4577 0, // Augmentation size.
4578 elfcpp::DW_CFA_advance_loc + 1,
4579 elfcpp::DW_CFA_register, 65, 0,
15a3a14f 4580 elfcpp::DW_CFA_advance_loc + 7,
b4f7960d
AM
4581 elfcpp::DW_CFA_restore_extended, 65
4582};
4583
9d5781f8
AM
4584// Describe __glink_PLTresolve use of LR, 32-bit version.
4585static const unsigned char glink_eh_frame_fde_32[] =
4586{
4587 0, 0, 0, 0, // Replaced with offset to .glink.
4588 0, 0, 0, 0, // Replaced with size of .glink.
4589 0, // Augmentation size.
4590 elfcpp::DW_CFA_advance_loc + 2,
4591 elfcpp::DW_CFA_register, 65, 0,
4592 elfcpp::DW_CFA_advance_loc + 4,
4593 elfcpp::DW_CFA_restore_extended, 65
4594};
4595
4596static const unsigned char default_fde[] =
4597{
4598 0, 0, 0, 0, // Replaced with offset to stubs.
4599 0, 0, 0, 0, // Replaced with size of stubs.
4600 0, // Augmentation size.
4601 elfcpp::DW_CFA_nop, // Pad.
4602 elfcpp::DW_CFA_nop,
4603 elfcpp::DW_CFA_nop
4604};
4605
9e69ed50
AM
4606template<bool big_endian>
4607static inline void
4608write_insn(unsigned char* p, uint32_t v)
4609{
4610 elfcpp::Swap<32, big_endian>::writeval(p, v);
4611}
4612
691d2e9a
AM
4613template<int size>
4614static inline unsigned int
4615param_plt_align()
4616{
4617 if (!parameters->options().user_set_plt_align())
4618 return size == 64 ? 32 : 8;
4619 return 1 << parameters->options().plt_align();
4620}
4621
ec661b9d
AM
4622// Stub_table holds information about plt and long branch stubs.
4623// Stubs are built in an area following some input section determined
4624// by group_sections(). This input section is converted to a relaxed
4625// input section allowing it to be resized to accommodate the stubs
4626
4627template<int size, bool big_endian>
4628class Stub_table : public Output_relaxed_input_section
4629{
4630 public:
7e57d19e
AM
4631 struct Plt_stub_ent
4632 {
4633 Plt_stub_ent(unsigned int off, unsigned int indx)
32f59844 4634 : off_(off), indx_(indx), iter_(0), notoc_(0), r2save_(0), localentry0_(0)
7e57d19e
AM
4635 { }
4636
4637 unsigned int off_;
32f59844
AM
4638 unsigned int indx_ : 28;
4639 unsigned int iter_ : 1;
4640 unsigned int notoc_ : 1;
7e57d19e 4641 unsigned int r2save_ : 1;
7ee7ff70 4642 unsigned int localentry0_ : 1;
7e57d19e 4643 };
32f59844
AM
4644 struct Branch_stub_ent
4645 {
4646 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4647 : off_(off), iter_(false), notoc_(notoc), save_res_(save_res)
4648 { }
4649
4650 unsigned int off_;
4651 bool iter_;
4652 bool notoc_;
4653 bool save_res_;
4654 };
ec661b9d
AM
4655 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4656 static const Address invalid_address = static_cast<Address>(0) - 1;
4657
a3e60ddb
AM
4658 Stub_table(Target_powerpc<size, big_endian>* targ,
4659 Output_section* output_section,
590b87ff
AM
4660 const Output_section::Input_section* owner,
4661 uint32_t id)
a3e60ddb
AM
4662 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4663 owner->relobj()
4664 ->section_addralign(owner->shndx())),
ec661b9d 4665 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4666 orig_data_size_(owner->current_data_size()),
4667 plt_size_(0), last_plt_size_(0),
6395d38b 4668 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
32f59844 4669 need_save_res_(false), need_resize_(false), resizing_(false),
220f9906 4670 uniq_(id)
a3e60ddb
AM
4671 {
4672 this->set_output_section(output_section);
ec661b9d 4673
a3e60ddb
AM
4674 std::vector<Output_relaxed_input_section*> new_relaxed;
4675 new_relaxed.push_back(this);
4676 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4677 }
ec661b9d
AM
4678
4679 // Add a plt call stub.
a3e60ddb
AM
4680 bool
4681 add_plt_call_entry(Address,
4682 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4683 const Symbol*,
4684 unsigned int,
7e57d19e
AM
4685 Address,
4686 bool);
ec661b9d 4687
a3e60ddb
AM
4688 bool
4689 add_plt_call_entry(Address,
4690 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4691 unsigned int,
4692 unsigned int,
7e57d19e
AM
4693 Address,
4694 bool);
ec661b9d
AM
4695
4696 // Find a given plt call stub.
7e57d19e 4697 const Plt_stub_ent*
ec661b9d
AM
4698 find_plt_call_entry(const Symbol*) const;
4699
7e57d19e 4700 const Plt_stub_ent*
ec661b9d
AM
4701 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4702 unsigned int) const;
4703
7e57d19e 4704 const Plt_stub_ent*
ec661b9d
AM
4705 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4706 const Symbol*,
4707 unsigned int,
4708 Address) const;
4709
7e57d19e 4710 const Plt_stub_ent*
ec661b9d
AM
4711 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4712 unsigned int,
4713 unsigned int,
4714 Address) const;
4715
4716 // Add a long branch stub.
a3e60ddb
AM
4717 bool
4718 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
d49044c7 4719 unsigned int, Address, Address, bool);
ec661b9d 4720
32f59844 4721 const Branch_stub_ent*
9d5781f8
AM
4722 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4723 Address) const;
ec661b9d 4724
a3e60ddb
AM
4725 bool
4726 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4727 {
32f59844 4728 Address max_branch_offset = max_branch_delta<size>(r_type);
a3e60ddb
AM
4729 if (max_branch_offset == 0)
4730 return true;
4731 gold_assert(from != invalid_address);
4732 Address loc = off + this->stub_address();
4733 return loc - from + max_branch_offset < 2 * max_branch_offset;
4734 }
4735
ec661b9d 4736 void
a3e60ddb 4737 clear_stubs(bool all)
cf43a2fe 4738 {
9e69ed50
AM
4739 this->plt_call_stubs_.clear();
4740 this->plt_size_ = 0;
ec661b9d
AM
4741 this->long_branch_stubs_.clear();
4742 this->branch_size_ = 0;
d49044c7 4743 this->need_save_res_ = false;
a3e60ddb
AM
4744 if (all)
4745 {
4746 this->last_plt_size_ = 0;
4747 this->last_branch_size_ = 0;
4748 }
cf43a2fe
AM
4749 }
4750
32f59844
AM
4751 bool
4752 need_resize() const
4753 { return need_resize_; }
4754
4755 void
4756 set_resizing(bool val)
4757 {
4758 this->resizing_ = val;
4759 if (val)
4760 {
4761 this->need_resize_ = false;
4762 this->plt_size_ = 0;
4763 this->branch_size_ = 0;
4764 this->need_save_res_ = false;
4765 }
4766 }
4767
ec661b9d
AM
4768 Address
4769 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4770 {
ec661b9d
AM
4771 Address start_off = off;
4772 off += this->orig_data_size_;
4773 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4774 if (this->need_save_res_)
4775 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4776 if (my_size != 0)
4777 off = align_address(off, this->stub_align());
4778 // Include original section size and alignment padding in size
4779 my_size += off - start_off;
6395d38b
HS
4780 // Ensure new size is always larger than min size
4781 // threshold. Alignment requirement is included in "my_size", so
4782 // increase "my_size" does not invalidate alignment.
4783 if (my_size < this->min_size_threshold_)
4784 my_size = this->min_size_threshold_;
ec661b9d
AM
4785 this->reset_address_and_file_offset();
4786 this->set_current_data_size(my_size);
4787 this->set_address_and_file_offset(os->address() + start_off,
4788 os->offset() + start_off);
4789 return my_size;
cf43a2fe
AM
4790 }
4791
ec661b9d 4792 Address
9d5781f8 4793 stub_address() const
ec661b9d
AM
4794 {
4795 return align_address(this->address() + this->orig_data_size_,
4796 this->stub_align());
4797 }
4798
4799 Address
9d5781f8 4800 stub_offset() const
ec661b9d
AM
4801 {
4802 return align_address(this->offset() + this->orig_data_size_,
4803 this->stub_align());
4804 }
4805
4806 section_size_type
4807 plt_size() const
4808 { return this->plt_size_; }
4809
32f59844
AM
4810 section_size_type
4811 branch_size() const
4812 { return this->branch_size_; }
4813
590b87ff
AM
4814 void
4815 set_min_size_threshold(Address min_size)
6395d38b
HS
4816 { this->min_size_threshold_ = min_size; }
4817
590b87ff
AM
4818 void
4819 define_stub_syms(Symbol_table*);
4820
ec661b9d
AM
4821 bool
4822 size_update()
4823 {
4824 Output_section* os = this->output_section();
4825 if (os->addralign() < this->stub_align())
4826 {
4827 os->set_addralign(this->stub_align());
4828 // FIXME: get rid of the insane checkpointing.
4829 // We can't increase alignment of the input section to which
4830 // stubs are attached; The input section may be .init which
4831 // is pasted together with other .init sections to form a
4832 // function. Aligning might insert zero padding resulting in
4833 // sigill. However we do need to increase alignment of the
4834 // output section so that the align_address() on offset in
4835 // set_address_and_size() adds the same padding as the
4836 // align_address() on address in stub_address().
4837 // What's more, we need this alignment for the layout done in
4838 // relaxation_loop_body() so that the output section starts at
4839 // a suitably aligned address.
4840 os->checkpoint_set_addralign(this->stub_align());
4841 }
9e69ed50
AM
4842 if (this->last_plt_size_ != this->plt_size_
4843 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4844 {
9e69ed50
AM
4845 this->last_plt_size_ = this->plt_size_;
4846 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4847 return true;
4848 }
4849 return false;
4850 }
4851
34e0882b
AM
4852 // Add .eh_frame info for this stub section.
4853 void
4854 add_eh_frame(Layout* layout);
be897fb7 4855
34e0882b 4856 // Remove .eh_frame info for this stub section.
be897fb7 4857 void
34e0882b 4858 remove_eh_frame(Layout* layout);
9d5781f8 4859
ec661b9d
AM
4860 Target_powerpc<size, big_endian>*
4861 targ() const
4862 { return targ_; }
6ce78956 4863
cf43a2fe 4864 private:
bdab445c
AM
4865 class Plt_stub_key;
4866 class Plt_stub_key_hash;
bdab445c
AM
4867 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4868 Plt_stub_key_hash> Plt_stub_entries;
32f59844
AM
4869 class Branch_stub_key;
4870 class Branch_stub_key_hash;
4871 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4872 Branch_stub_key_hash> Branch_stub_entries;
9e69ed50
AM
4873
4874 // Alignment of stub section.
ec661b9d 4875 unsigned int
9e69ed50
AM
4876 stub_align() const
4877 {
691d2e9a 4878 unsigned int min_align = size == 64 ? 32 : 16;
9e69ed50
AM
4879 unsigned int user_align = 1 << parameters->options().plt_align();
4880 return std::max(user_align, min_align);
4881 }
cf43a2fe 4882
91c2b899
AM
4883 // Return the plt offset for the given call stub.
4884 Address
08be3224
AM
4885 plt_off(typename Plt_stub_entries::const_iterator p,
4886 const Output_data_plt_powerpc<size, big_endian>** sec) const
91c2b899
AM
4887 {
4888 const Symbol* gsym = p->first.sym_;
4889 if (gsym != NULL)
08be3224 4890 return this->targ_->plt_off(gsym, sec);
91c2b899
AM
4891 else
4892 {
91c2b899
AM
4893 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4894 unsigned int local_sym_index = p->first.locsym_;
08be3224 4895 return this->targ_->plt_off(relobj, local_sym_index, sec);
91c2b899
AM
4896 }
4897 }
4898
9e69ed50 4899 // Size of a given plt call stub.
ec661b9d 4900 unsigned int
32f59844 4901 plt_call_size(typename Plt_stub_entries::const_iterator p) const;
34e0882b
AM
4902
4903 unsigned int
4904 plt_call_align(unsigned int bytes) const
4905 {
691d2e9a 4906 unsigned int align = param_plt_align<size>();
9e390558 4907 return (bytes + align - 1) & -align;
9e69ed50 4908 }
ec661b9d
AM
4909
4910 // Return long branch stub size.
4911 unsigned int
32f59844
AM
4912 branch_stub_size(typename Branch_stub_entries::const_iterator p,
4913 bool* need_lt);
4914
4915 bool
4916 build_tls_opt_head(unsigned char** pp,
4917 typename Plt_stub_entries::const_iterator cs);
4918
4919 bool
4920 build_tls_opt_tail(unsigned char* p,
4921 typename Plt_stub_entries::const_iterator cs);
ec661b9d 4922
f073a3e8
AM
4923 void
4924 plt_error(const Plt_stub_key& p);
4925
ec661b9d 4926 // Write out stubs.
cf43a2fe
AM
4927 void
4928 do_write(Output_file*);
4929
ec661b9d 4930 // Plt call stub keys.
bdab445c 4931 class Plt_stub_key
cf43a2fe 4932 {
d1a8cabd 4933 public:
bdab445c 4934 Plt_stub_key(const Symbol* sym)
c9824451
AM
4935 : sym_(sym), object_(0), addend_(0), locsym_(0)
4936 { }
4937
bdab445c 4938 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4939 unsigned int locsym_index)
c9824451
AM
4940 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4941 { }
4942
bdab445c 4943 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4944 const Symbol* sym,
4945 unsigned int r_type,
4946 Address addend)
e5d5f5ed 4947 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4948 {
4949 if (size != 32)
ec661b9d 4950 this->addend_ = addend;
d1a8cabd 4951 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4952 && (r_type == elfcpp::R_PPC_PLTREL24
4953 || r_type == elfcpp::R_POWERPC_PLTCALL))
cf43a2fe 4954 {
ec661b9d 4955 this->addend_ = addend;
e5d5f5ed 4956 if (this->addend_ >= 32768)
d1a8cabd 4957 this->object_ = object;
cf43a2fe
AM
4958 }
4959 }
4960
bdab445c 4961 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4962 unsigned int locsym_index,
4963 unsigned int r_type,
4964 Address addend)
e5d5f5ed
AM
4965 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4966 {
4967 if (size != 32)
ec661b9d 4968 this->addend_ = addend;
e5d5f5ed 4969 else if (parameters->options().output_is_position_independent()
23cedd1d
AM
4970 && (r_type == elfcpp::R_PPC_PLTREL24
4971 || r_type == elfcpp::R_POWERPC_PLTCALL))
ec661b9d 4972 this->addend_ = addend;
e5d5f5ed
AM
4973 }
4974
bdab445c 4975 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
4976 {
4977 return (this->sym_ == that.sym_
4978 && this->object_ == that.object_
e5d5f5ed
AM
4979 && this->addend_ == that.addend_
4980 && this->locsym_ == that.locsym_);
cf43a2fe 4981 }
c9269dff
AM
4982
4983 const Symbol* sym_;
e5d5f5ed
AM
4984 const Sized_relobj_file<size, big_endian>* object_;
4985 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4986 unsigned int locsym_;
cf43a2fe
AM
4987 };
4988
bdab445c 4989 class Plt_stub_key_hash
cf43a2fe 4990 {
d1a8cabd 4991 public:
bdab445c 4992 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
4993 {
4994 return (reinterpret_cast<uintptr_t>(ent.sym_)
4995 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
4996 ^ ent.addend_
4997 ^ ent.locsym_);
cf43a2fe 4998 }
ec661b9d
AM
4999 };
5000
5001 // Long branch stub keys.
32f59844 5002 class Branch_stub_key
ec661b9d
AM
5003 {
5004 public:
32f59844
AM
5005 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
5006 : dest_(to), toc_base_off_(0)
ec661b9d
AM
5007 {
5008 if (size == 64)
5009 toc_base_off_ = obj->toc_base_offset();
5010 }
5011
32f59844 5012 bool operator==(const Branch_stub_key& that) const
ec661b9d
AM
5013 {
5014 return (this->dest_ == that.dest_
5015 && (size == 32
5016 || this->toc_base_off_ == that.toc_base_off_));
5017 }
cf43a2fe 5018
ec661b9d
AM
5019 Address dest_;
5020 unsigned int toc_base_off_;
5021 };
cf43a2fe 5022
32f59844 5023 class Branch_stub_key_hash
ec661b9d
AM
5024 {
5025 public:
32f59844
AM
5026 size_t operator()(const Branch_stub_key& key) const
5027 { return key.dest_ ^ key.toc_base_off_; }
ec661b9d 5028 };
cf43a2fe 5029
ec661b9d 5030 // In a sane world this would be a global.
cf43a2fe 5031 Target_powerpc<size, big_endian>* targ_;
ec661b9d 5032 // Map sym/object/addend to stub offset.
ec661b9d
AM
5033 Plt_stub_entries plt_call_stubs_;
5034 // Map destination address to stub offset.
ec661b9d
AM
5035 Branch_stub_entries long_branch_stubs_;
5036 // size of input section
5037 section_size_type orig_data_size_;
5038 // size of stubs
9e69ed50 5039 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
5040 // Some rare cases cause (PR/20529) fluctuation in stub table
5041 // size, which leads to an endless relax loop. This is to be fixed
5042 // by, after the first few iterations, allowing only increase of
5043 // stub table size. This variable sets the minimal possible size of
5044 // a stub table, it is zero for the first few iterations, then
5045 // increases monotonically.
5046 Address min_size_threshold_;
d49044c7
AM
5047 // Set if this stub group needs a copy of out-of-line register
5048 // save/restore functions.
5049 bool need_save_res_;
32f59844
AM
5050 // Set when notoc_/r2save_ changes after sizing a stub
5051 bool need_resize_;
5052 // Set when resizing stubs
5053 bool resizing_;
590b87ff
AM
5054 // Per stub table unique identifier.
5055 uint32_t uniq_;
cf43a2fe
AM
5056};
5057
ec661b9d 5058// Add a plt call stub, if we do not already have one for this
d1a8cabd 5059// sym/object/addend combo.
cf43a2fe
AM
5060
5061template<int size, bool big_endian>
a3e60ddb 5062bool
ec661b9d 5063Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5064 Address from,
c9824451 5065 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5066 const Symbol* gsym,
ec661b9d 5067 unsigned int r_type,
7e57d19e
AM
5068 Address addend,
5069 bool tocsave)
cf43a2fe 5070{
bdab445c
AM
5071 Plt_stub_key key(object, gsym, r_type, addend);
5072 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5073 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5074 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5075 if (size == 64)
7ee7ff70 5076 {
32f59844 5077 if (p.second
7ee7ff70
AM
5078 && this->targ_->is_elfv2_localentry0(gsym))
5079 {
5080 p.first->second.localentry0_ = 1;
5081 this->targ_->set_has_localentry0();
5082 }
32f59844
AM
5083 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5084 {
e4dff765
AM
5085 if (!p.second && !p.first->second.notoc_
5086 && !this->targ_->powerxx_stubs())
32f59844
AM
5087 this->need_resize_ = true;
5088 p.first->second.notoc_ = 1;
5089 }
5090 else if (!tocsave && !p.first->second.localentry0_)
5091 {
5092 if (!p.second && !p.first->second.r2save_)
5093 this->need_resize_ = true;
5094 p.first->second.r2save_ = 1;
5095 }
5096 }
5097 if (p.second || (this->resizing_ && !p.first->second.iter_))
5098 {
5099 if (this->resizing_)
5100 {
5101 p.first->second.iter_ = 1;
5102 p.first->second.off_ = this->plt_size_;
5103 }
5104 this->plt_size_ += this->plt_call_size(p.first);
34e0882b 5105 if (this->targ_->is_tls_get_addr_opt(gsym))
220f9906 5106 this->targ_->set_has_tls_get_addr_opt();
34e0882b 5107 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70 5108 }
32f59844 5109 return this->can_reach_stub(from, p.first->second.off_, r_type);
cf43a2fe
AM
5110}
5111
e5d5f5ed 5112template<int size, bool big_endian>
a3e60ddb 5113bool
ec661b9d 5114Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 5115 Address from,
c9824451 5116 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5117 unsigned int locsym_index,
ec661b9d 5118 unsigned int r_type,
7e57d19e
AM
5119 Address addend,
5120 bool tocsave)
e5d5f5ed 5121{
bdab445c
AM
5122 Plt_stub_key key(object, locsym_index, r_type, addend);
5123 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 5124 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 5125 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
32f59844 5126 if (size == 64)
7ee7ff70 5127 {
32f59844 5128 if (p.second
7ee7ff70
AM
5129 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5130 {
5131 p.first->second.localentry0_ = 1;
5132 this->targ_->set_has_localentry0();
5133 }
32f59844
AM
5134 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5135 {
e4dff765
AM
5136 if (!p.second && !p.first->second.notoc_
5137 && !this->targ_->powerxx_stubs())
32f59844
AM
5138 this->need_resize_ = true;
5139 p.first->second.notoc_ = 1;
5140 }
5141 else if (!tocsave && !p.first->second.localentry0_)
5142 {
5143 if (!p.second && !p.first->second.r2save_)
5144 this->need_resize_ = true;
5145 p.first->second.r2save_ = 1;
5146 }
7ee7ff70 5147 }
32f59844
AM
5148 if (p.second || (this->resizing_ && !p.first->second.iter_))
5149 {
5150 if (this->resizing_)
5151 {
5152 p.first->second.iter_ = 1;
5153 p.first->second.off_ = this->plt_size_;
5154 }
5155 this->plt_size_ += this->plt_call_size(p.first);
5156 this->plt_size_ = this->plt_call_align(this->plt_size_);
5157 }
5158 return this->can_reach_stub(from, p.first->second.off_, r_type);
e5d5f5ed
AM
5159}
5160
ec661b9d
AM
5161// Find a plt call stub.
5162
cf43a2fe 5163template<int size, bool big_endian>
7e57d19e 5164const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5165Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5166 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 5167 const Symbol* gsym,
ec661b9d
AM
5168 unsigned int r_type,
5169 Address addend) const
c9824451 5170{
bdab445c
AM
5171 Plt_stub_key key(object, gsym, r_type, addend);
5172 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5173 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5174 return NULL;
5175 return &p->second;
c9824451
AM
5176}
5177
5178template<int size, bool big_endian>
7e57d19e 5179const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5180Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 5181{
bdab445c
AM
5182 Plt_stub_key key(gsym);
5183 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5184 if (p == this->plt_call_stubs_.end())
5185 return NULL;
5186 return &p->second;
cf43a2fe
AM
5187}
5188
e5d5f5ed 5189template<int size, bool big_endian>
7e57d19e 5190const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5191Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 5192 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 5193 unsigned int locsym_index,
ec661b9d
AM
5194 unsigned int r_type,
5195 Address addend) const
e5d5f5ed 5196{
bdab445c
AM
5197 Plt_stub_key key(object, locsym_index, r_type, addend);
5198 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5199 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
5200 return NULL;
5201 return &p->second;
c9824451
AM
5202}
5203
5204template<int size, bool big_endian>
7e57d19e 5205const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 5206Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
5207 const Sized_relobj_file<size, big_endian>* object,
5208 unsigned int locsym_index) const
5209{
bdab445c
AM
5210 Plt_stub_key key(object, locsym_index);
5211 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
5212 if (p == this->plt_call_stubs_.end())
5213 return NULL;
5214 return &p->second;
ec661b9d
AM
5215}
5216
5217// Add a long branch stub if we don't already have one to given
5218// destination.
5219
5220template<int size, bool big_endian>
a3e60ddb 5221bool
ec661b9d
AM
5222Stub_table<size, big_endian>::add_long_branch_entry(
5223 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
5224 unsigned int r_type,
5225 Address from,
d49044c7
AM
5226 Address to,
5227 bool save_res)
ec661b9d 5228{
32f59844
AM
5229 Branch_stub_key key(object, to);
5230 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5231 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
590b87ff 5232 std::pair<typename Branch_stub_entries::iterator, bool> p
32f59844
AM
5233 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5234 if (notoc && !p.first->second.notoc_)
ec661b9d 5235 {
32f59844
AM
5236 this->need_resize_ = true;
5237 p.first->second.notoc_ = true;
5238 }
5239 gold_assert(save_res == p.first->second.save_res_);
5240 if (p.second || (this->resizing_ && !p.first->second.iter_))
5241 {
5242 if (this->resizing_)
5243 {
5244 p.first->second.iter_ = 1;
5245 p.first->second.off_ = this->branch_size_;
5246 }
d49044c7
AM
5247 if (save_res)
5248 this->need_save_res_ = true;
5249 else
5250 {
32f59844
AM
5251 bool need_lt = false;
5252 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5253 this->branch_size_ += stub_size;
5254 if (size == 64 && need_lt)
d49044c7
AM
5255 this->targ_->add_branch_lookup_table(to);
5256 }
ec661b9d 5257 }
32f59844 5258 return this->can_reach_stub(from, p.first->second.off_, r_type);
ec661b9d
AM
5259}
5260
d49044c7 5261// Find long branch stub offset.
ec661b9d
AM
5262
5263template<int size, bool big_endian>
32f59844 5264const typename Stub_table<size, big_endian>::Branch_stub_ent*
ec661b9d
AM
5265Stub_table<size, big_endian>::find_long_branch_entry(
5266 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 5267 Address to) const
ec661b9d 5268{
32f59844 5269 Branch_stub_key key(object, to);
ec661b9d 5270 typename Branch_stub_entries::const_iterator p
32f59844 5271 = this->long_branch_stubs_.find(key);
d49044c7 5272 if (p == this->long_branch_stubs_.end())
32f59844
AM
5273 return NULL;
5274 return &p->second;
e5d5f5ed
AM
5275}
5276
220f9906
AM
5277template<bool big_endian>
5278static void
5279eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
34e0882b 5280{
220f9906
AM
5281 delta /= 4;
5282 if (delta < 64)
5283 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5284 else if (delta < 256)
34e0882b 5285 {
220f9906
AM
5286 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5287 fde.push_back(delta);
5288 }
5289 else if (delta < 65536)
5290 {
5291 fde.resize(fde.size() + 3);
5292 unsigned char *p = &*fde.end() - 3;
5293 *p++ = elfcpp::DW_CFA_advance_loc2;
5294 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5295 }
5296 else
5297 {
5298 fde.resize(fde.size() + 5);
5299 unsigned char *p = &*fde.end() - 5;
5300 *p++ = elfcpp::DW_CFA_advance_loc4;
5301 elfcpp::Swap<32, big_endian>::writeval(p, delta);
34e0882b 5302 }
220f9906
AM
5303}
5304
5305template<typename T>
5306static bool
5307stub_sort(T s1, T s2)
5308{
5309 return s1->second.off_ < s2->second.off_;
34e0882b
AM
5310}
5311
5312// Add .eh_frame info for this stub section. Unlike other linker
5313// generated .eh_frame this is added late in the link, because we
5314// only want the .eh_frame info if this particular stub section is
5315// non-empty.
5316
5317template<int size, bool big_endian>
5318void
5319Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5320{
220f9906
AM
5321 if (size != 64
5322 || !parameters->options().ld_generated_unwind_info())
34e0882b
AM
5323 return;
5324
5325 // Since we add stub .eh_frame info late, it must be placed
5326 // after all other linker generated .eh_frame info so that
5327 // merge mapping need not be updated for input sections.
5328 // There is no provision to use a different CIE to that used
5329 // by .glink.
5330 if (!this->targ_->has_glink())
5331 return;
5332
220f9906
AM
5333 typedef typename Plt_stub_entries::const_iterator plt_iter;
5334 std::vector<plt_iter> calls;
5335 if (!this->plt_call_stubs_.empty())
5336 for (plt_iter cs = this->plt_call_stubs_.begin();
5337 cs != this->plt_call_stubs_.end();
5338 ++cs)
5339 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5340 && cs->second.r2save_
5341 && !cs->second.localentry0_)
e4dff765
AM
5342 || (cs->second.notoc_
5343 && !this->targ_->powerxx_stubs()))
220f9906
AM
5344 calls.push_back(cs);
5345 if (calls.size() > 1)
5346 std::stable_sort(calls.begin(), calls.end(),
5347 stub_sort<plt_iter>);
5348
5349 typedef typename Branch_stub_entries::const_iterator branch_iter;
5350 std::vector<branch_iter> branches;
e4dff765
AM
5351 if (!this->long_branch_stubs_.empty()
5352 && !this->targ_->powerxx_stubs())
220f9906
AM
5353 for (branch_iter bs = this->long_branch_stubs_.begin();
5354 bs != this->long_branch_stubs_.end();
5355 ++bs)
5356 if (bs->second.notoc_)
5357 branches.push_back(bs);
5358 if (branches.size() > 1)
5359 std::stable_sort(branches.begin(), branches.end(),
5360 stub_sort<branch_iter>);
5361
5362 if (calls.empty() && branches.empty())
34e0882b
AM
5363 return;
5364
220f9906
AM
5365 unsigned int last_eh_loc = 0;
5366 // offset pcrel sdata4, size udata4, and augmentation size byte.
5367 std::vector<unsigned char> fde(9, 0);
5368
5369 for (unsigned int i = 0; i < calls.size(); i++)
5370 {
5371 plt_iter cs = calls[i];
5372 unsigned int off = cs->second.off_;
5373 // The __tls_get_addr_opt call stub needs to describe where
5374 // it saves LR, to support exceptions that might be thrown
5375 // from __tls_get_addr, and to support asynchronous exceptions.
5376 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5377 {
5378 off += 7 * 4;
5379 if (cs->second.r2save_
5380 && !cs->second.localentry0_)
5381 {
5382 off += 2 * 4;
5383 eh_advance<big_endian>(fde, off - last_eh_loc);
5384 fde.resize(fde.size() + 6);
5385 unsigned char* p = &*fde.end() - 6;
5386 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5387 *p++ = 65;
5388 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5389 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5390 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5391 *p++ = elfcpp::DW_CFA_restore_extended;
5392 *p++ = 65;
5393 last_eh_loc = off + delta;
5394 continue;
5395 }
5396 }
5397 // notoc stubs also should describe LR changes, to support
5398 // asynchronous exceptions.
5399 off += (cs->second.r2save_ ? 4 : 0) + 8;
5400 eh_advance<big_endian>(fde, off - last_eh_loc);
5401 fde.resize(fde.size() + 6);
5402 unsigned char* p = &*fde.end() - 6;
5403 *p++ = elfcpp::DW_CFA_register;
5404 *p++ = 65;
5405 *p++ = 12;
5406 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5407 *p++ = elfcpp::DW_CFA_restore_extended;
5408 *p++ = 65;
5409 last_eh_loc = off + 8;
5410 }
5411
5412 for (unsigned int i = 0; i < branches.size(); i++)
5413 {
5414 branch_iter bs = branches[i];
5415 unsigned int off = bs->second.off_ + 8;
5416 eh_advance<big_endian>(fde, off - last_eh_loc);
5417 fde.resize(fde.size() + 6);
5418 unsigned char* p = &*fde.end() - 6;
5419 *p++ = elfcpp::DW_CFA_register;
5420 *p++ = 65;
5421 *p++ = 12;
5422 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5423 *p++ = elfcpp::DW_CFA_restore_extended;
5424 *p++ = 65;
5425 last_eh_loc = off + 8;
5426 }
5427
34e0882b
AM
5428 layout->add_eh_frame_for_plt(this,
5429 Eh_cie<size>::eh_frame_cie,
5430 sizeof (Eh_cie<size>::eh_frame_cie),
220f9906 5431 &*fde.begin(), fde.size());
34e0882b
AM
5432}
5433
5434template<int size, bool big_endian>
5435void
5436Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5437{
220f9906
AM
5438 if (size == 64
5439 && parameters->options().ld_generated_unwind_info()
5440 && this->targ_->has_glink())
5441 layout->remove_eh_frame_for_plt(this,
5442 Eh_cie<size>::eh_frame_cie,
5443 sizeof (Eh_cie<size>::eh_frame_cie));
34e0882b
AM
5444}
5445
ec661b9d
AM
5446// A class to handle .glink.
5447
5448template<int size, bool big_endian>
5449class Output_data_glink : public Output_section_data
5450{
5451 public:
9055360d
AM
5452 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5453 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
5454
5455 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
5456 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5457 end_branch_table_(), ge_size_(0)
ec661b9d
AM
5458 { }
5459
9d5781f8 5460 void
9055360d 5461 add_eh_frame(Layout* layout);
9d5781f8 5462
9055360d
AM
5463 void
5464 add_global_entry(const Symbol*);
5465
5466 Address
5467 find_global_entry(const Symbol*) const;
5468
9e390558
AM
5469 unsigned int
5470 global_entry_align(unsigned int off) const
5471 {
691d2e9a 5472 unsigned int align = param_plt_align<size>();
9e390558
AM
5473 return (off + align - 1) & -align;
5474 }
5475
5476 unsigned int
5477 global_entry_off() const
5478 {
5479 return this->global_entry_align(this->end_branch_table_);
5480 }
5481
9055360d
AM
5482 Address
5483 global_entry_address() const
5484 {
5485 gold_assert(this->is_data_size_valid());
9e390558
AM
5486 return this->address() + this->global_entry_off();
5487 }
5488
5489 int
5490 pltresolve_size() const
5491 {
5492 if (size == 64)
5493 return (8
407aa07c 5494 + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
9e390558 5495 return 16 * 4;
9d5781f8
AM
5496 }
5497
ec661b9d
AM
5498 protected:
5499 // Write to a map file.
5500 void
5501 do_print_to_mapfile(Mapfile* mapfile) const
5502 { mapfile->print_output_data(this, _("** glink")); }
5503
5504 private:
5505 void
5506 set_final_data_size();
5507
5508 // Write out .glink
5509 void
5510 do_write(Output_file*);
5511
5512 // Allows access to .got and .plt for do_write.
5513 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
5514
5515 // Map sym to stub offset.
5516 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5517 Global_entry_stub_entries global_entry_stubs_;
5518
5519 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
5520};
5521
9055360d
AM
5522template<int size, bool big_endian>
5523void
5524Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5525{
5526 if (!parameters->options().ld_generated_unwind_info())
5527 return;
5528
5529 if (size == 64)
5530 {
5531 if (this->targ_->abiversion() < 2)
5532 layout->add_eh_frame_for_plt(this,
5533 Eh_cie<64>::eh_frame_cie,
5534 sizeof (Eh_cie<64>::eh_frame_cie),
5535 glink_eh_frame_fde_64v1,
5536 sizeof (glink_eh_frame_fde_64v1));
5537 else
5538 layout->add_eh_frame_for_plt(this,
5539 Eh_cie<64>::eh_frame_cie,
5540 sizeof (Eh_cie<64>::eh_frame_cie),
5541 glink_eh_frame_fde_64v2,
5542 sizeof (glink_eh_frame_fde_64v2));
5543 }
5544 else
5545 {
5546 // 32-bit .glink can use the default since the CIE return
5547 // address reg, LR, is valid.
5548 layout->add_eh_frame_for_plt(this,
5549 Eh_cie<32>::eh_frame_cie,
5550 sizeof (Eh_cie<32>::eh_frame_cie),
5551 default_fde,
5552 sizeof (default_fde));
5553 // Except where LR is used in a PIC __glink_PLTresolve.
5554 if (parameters->options().output_is_position_independent())
5555 layout->add_eh_frame_for_plt(this,
5556 Eh_cie<32>::eh_frame_cie,
5557 sizeof (Eh_cie<32>::eh_frame_cie),
5558 glink_eh_frame_fde_32,
5559 sizeof (glink_eh_frame_fde_32));
5560 }
5561}
5562
5563template<int size, bool big_endian>
5564void
5565Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5566{
9e390558 5567 unsigned int off = this->global_entry_align(this->ge_size_);
9055360d 5568 std::pair<typename Global_entry_stub_entries::iterator, bool> p
9e390558 5569 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
9055360d 5570 if (p.second)
407aa07c 5571 this->ge_size_ = off + 16;
9055360d
AM
5572}
5573
5574template<int size, bool big_endian>
5575typename Output_data_glink<size, big_endian>::Address
5576Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5577{
5578 typename Global_entry_stub_entries::const_iterator p
5579 = this->global_entry_stubs_.find(gsym);
5580 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5581}
5582
cf43a2fe
AM
5583template<int size, bool big_endian>
5584void
5585Output_data_glink<size, big_endian>::set_final_data_size()
5586{
ec661b9d
AM
5587 unsigned int count = this->targ_->plt_entry_count();
5588 section_size_type total = 0;
cf43a2fe
AM
5589
5590 if (count != 0)
5591 {
5592 if (size == 32)
5593 {
cf43a2fe
AM
5594 // space for branch table
5595 total += 4 * (count - 1);
5596
5597 total += -total & 15;
9e390558 5598 total += this->pltresolve_size();
cf43a2fe
AM
5599 }
5600 else
5601 {
9e390558 5602 total += this->pltresolve_size();
cf43a2fe
AM
5603
5604 // space for branch table
b4f7960d
AM
5605 total += 4 * count;
5606 if (this->targ_->abiversion() < 2)
5607 {
5608 total += 4 * count;
5609 if (count > 0x8000)
5610 total += 4 * (count - 0x8000);
5611 }
cf43a2fe
AM
5612 }
5613 }
9055360d 5614 this->end_branch_table_ = total;
9e390558 5615 total = this->global_entry_align(total);
9055360d 5616 total += this->ge_size_;
cf43a2fe
AM
5617
5618 this->set_data_size(total);
5619}
5620
590b87ff
AM
5621// Define symbols on stubs, identifying the stub.
5622
5623template<int size, bool big_endian>
5624void
5625Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5626{
5627 if (!this->plt_call_stubs_.empty())
5628 {
5629 // The key for the plt call stub hash table includes addresses,
5630 // therefore traversal order depends on those addresses, which
5631 // can change between runs if gold is a PIE. Unfortunately the
5632 // output .symtab ordering depends on the order in which symbols
5633 // are added to the linker symtab. We want reproducible output
5634 // so must sort the call stub symbols.
5635 typedef typename Plt_stub_entries::const_iterator plt_iter;
5636 std::vector<plt_iter> sorted;
5637 sorted.resize(this->plt_call_stubs_.size());
5638
5639 for (plt_iter cs = this->plt_call_stubs_.begin();
5640 cs != this->plt_call_stubs_.end();
5641 ++cs)
bdab445c 5642 sorted[cs->second.indx_] = cs;
590b87ff
AM
5643
5644 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5645 {
5646 plt_iter cs = sorted[i];
5647 char add[10];
5648 add[0] = 0;
5649 if (cs->first.addend_ != 0)
5650 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5651 char obj[10];
5652 obj[0] = 0;
5653 if (cs->first.object_)
590b87ff
AM
5654 {
5655 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5656 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5657 sprintf(obj, "%x:", ppcobj->uniq());
5658 }
5659 char localname[9];
5660 const char *symname;
5661 if (cs->first.sym_ == NULL)
5662 {
5663 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5664 symname = localname;
5665 }
34e0882b
AM
5666 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5667 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5668 else
5669 symname = cs->first.sym_->name();
94de2a2c
JC
5670 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5671 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5672 Address value
5673 = this->stub_address() - this->address() + cs->second.off_;
34e0882b 5674 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
590b87ff
AM
5675 this->targ_->define_local(symtab, name, this, value, stub_size);
5676 }
5677 }
5678
5679 typedef typename Branch_stub_entries::const_iterator branch_iter;
5680 for (branch_iter bs = this->long_branch_stubs_.begin();
5681 bs != this->long_branch_stubs_.end();
5682 ++bs)
5683 {
32f59844 5684 if (bs->second.save_res_)
590b87ff
AM
5685 continue;
5686
5687 char* name = new char[8 + 13 + 16 + 1];
5688 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5689 static_cast<unsigned long long>(bs->first.dest_));
5690 Address value = (this->stub_address() - this->address()
32f59844
AM
5691 + this->plt_size_ + bs->second.off_);
5692 bool need_lt = false;
5693 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
590b87ff
AM
5694 this->targ_->define_local(symtab, name, this, value, stub_size);
5695 }
5696}
5697
32f59844
AM
5698// Emit the start of a __tls_get_addr_opt plt call stub.
5699
5700template<int size, bool big_endian>
5701bool
5702Stub_table<size, big_endian>::build_tls_opt_head(
5703 unsigned char** pp,
5704 typename Plt_stub_entries::const_iterator cs)
5705{
5706 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5707 {
5708 unsigned char* p = *pp;
5709 if (size == 64)
5710 {
5711 write_insn<big_endian>(p, ld_11_3 + 0);
5712 p += 4;
5713 write_insn<big_endian>(p, ld_12_3 + 8);
5714 p += 4;
5715 write_insn<big_endian>(p, mr_0_3);
5716 p += 4;
5717 write_insn<big_endian>(p, cmpdi_11_0);
5718 p += 4;
5719 write_insn<big_endian>(p, add_3_12_13);
5720 p += 4;
5721 write_insn<big_endian>(p, beqlr);
5722 p += 4;
5723 write_insn<big_endian>(p, mr_3_0);
5724 p += 4;
5725 if (cs->second.r2save_ && !cs->second.localentry0_)
5726 {
5727 write_insn<big_endian>(p, mflr_11);
5728 p += 4;
5729 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5730 p += 4;
5731 }
5732 }
5733 else
5734 {
5735 write_insn<big_endian>(p, lwz_11_3 + 0);
5736 p += 4;
5737 write_insn<big_endian>(p, lwz_12_3 + 4);
5738 p += 4;
5739 write_insn<big_endian>(p, mr_0_3);
5740 p += 4;
5741 write_insn<big_endian>(p, cmpwi_11_0);
5742 p += 4;
5743 write_insn<big_endian>(p, add_3_12_2);
5744 p += 4;
5745 write_insn<big_endian>(p, beqlr);
5746 p += 4;
5747 write_insn<big_endian>(p, mr_3_0);
5748 p += 4;
5749 write_insn<big_endian>(p, nop);
5750 p += 4;
5751 }
5752 *pp = p;
5753 return true;
5754 }
5755 return false;
5756}
5757
5758// Emit the tail of a __tls_get_addr_opt plt call stub.
5759
5760template<int size, bool big_endian>
5761bool
5762Stub_table<size, big_endian>::build_tls_opt_tail(
5763 unsigned char* p,
5764 typename Plt_stub_entries::const_iterator cs)
5765{
5766 if (size == 64
5767 && cs->second.r2save_
5768 && !cs->second.localentry0_
5769 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5770 {
5771 write_insn<big_endian>(p, bctrl);
5772 p += 4;
5773 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5774 p += 4;
5775 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5776 p += 4;
5777 write_insn<big_endian>(p, mtlr_11);
5778 p += 4;
5779 write_insn<big_endian>(p, blr);
5780 return true;
5781 }
5782 return false;
5783}
5784
e4dff765
AM
5785// Emit pc-relative plt call stub code.
5786
5787template<bool big_endian>
5788static unsigned char*
5789build_powerxx_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5790{
5791 uint64_t insn;
5792 if (off - odd + (1ULL << 33) < 1ULL << 34)
5793 {
5794 off -= odd;
5795 if (odd)
5796 {
5797 write_insn<big_endian>(p, nop);
5798 p += 4;
5799 }
5800 if (load)
5801 insn = pld_12_pc;
5802 else
5803 insn = paddi_12_pc;
5804 insn |= d34(off);
5805 write_insn<big_endian>(p, insn >> 32);
5806 p += 4;
5807 write_insn<big_endian>(p, insn & 0xffffffff);
5808 }
5809 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5810 {
5811 off -= 8 - odd;
5812 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5813 p += 4;
5814 if (!odd)
5815 {
5816 write_insn<big_endian>(p, sldi_11_11_34);
5817 p += 4;
5818 }
5819 insn = paddi_12_pc | d34(off);
5820 write_insn<big_endian>(p, insn >> 32);
5821 p += 4;
5822 write_insn<big_endian>(p, insn & 0xffffffff);
5823 p += 4;
5824 if (odd)
5825 {
5826 write_insn<big_endian>(p, sldi_11_11_34);
5827 p += 4;
5828 }
5829 if (load)
5830 write_insn<big_endian>(p, ldx_12_11_12);
5831 else
5832 write_insn<big_endian>(p, add_12_11_12);
5833 }
5834 else
5835 {
5836 off -= odd + 8;
5837 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5838 p += 4;
5839 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5840 p += 4;
5841 if (odd)
5842 {
5843 write_insn<big_endian>(p, sldi_11_11_34);
5844 p += 4;
5845 }
5846 insn = paddi_12_pc | d34(off);
5847 write_insn<big_endian>(p, insn >> 32);
5848 p += 4;
5849 write_insn<big_endian>(p, insn & 0xffffffff);
5850 p += 4;
5851 if (!odd)
5852 {
5853 write_insn<big_endian>(p, sldi_11_11_34);
5854 p += 4;
5855 }
5856 if (load)
5857 write_insn<big_endian>(p, ldx_12_11_12);
5858 else
5859 write_insn<big_endian>(p, add_12_11_12);
5860 }
5861 p += 4;
5862 return p;
5863}
5864
32f59844
AM
5865// Gets the address of a label (1:) in r11 and builds an offset in r12,
5866// then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5867// mflr %r12
5868// bcl 20,31,1f
5869// 1: mflr %r11
5870// mtlr %r12
5871// lis %r12,xxx-1b@highest
5872// ori %r12,%r12,xxx-1b@higher
5873// sldi %r12,%r12,32
5874// oris %r12,%r12,xxx-1b@high
5875// ori %r12,%r12,xxx-1b@l
5876// add/ldx %r12,%r11,%r12
5877
5878template<bool big_endian>
5879static unsigned char*
5880build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5881{
5882 write_insn<big_endian>(p, mflr_12);
5883 p += 4;
5884 write_insn<big_endian>(p, bcl_20_31);
5885 p += 4;
5886 write_insn<big_endian>(p, mflr_11);
5887 p += 4;
5888 write_insn<big_endian>(p, mtlr_12);
5889 p += 4;
5890 if (off + 0x8000 < 0x10000)
5891 {
5892 if (load)
5893 write_insn<big_endian>(p, ld_12_11 + l(off));
5894 else
5895 write_insn<big_endian>(p, addi_12_11 + l(off));
5896 }
5897 else if (off + 0x80008000ULL < 0x100000000ULL)
5898 {
5899 write_insn<big_endian>(p, addis_12_11 + ha(off));
5900 p += 4;
5901 if (load)
5902 write_insn<big_endian>(p, ld_12_12 + l(off));
5903 else
5904 write_insn<big_endian>(p, addi_12_12 + l(off));
5905 }
5906 else
5907 {
5908 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5909 {
5910 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5911 p += 4;
5912 }
5913 else
5914 {
5915 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5916 p += 4;
5917 if (((off >> 32) & 0xffff) != 0)
5918 {
5919 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5920 p += 4;
5921 }
5922 }
5923 if (((off >> 32) & 0xffffffffULL) != 0)
5924 {
5925 write_insn<big_endian>(p, sldi_12_12_32);
5926 p += 4;
5927 }
5928 if (hi(off) != 0)
5929 {
5930 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5931 p += 4;
5932 }
5933 if (l(off) != 0)
5934 {
5935 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5936 p += 4;
5937 }
5938 if (load)
5939 write_insn<big_endian>(p, ldx_12_11_12);
5940 else
5941 write_insn<big_endian>(p, add_12_11_12);
5942 }
5943 p += 4;
5944 return p;
5945}
5946
5947// Size of a given plt call stub.
5948
5949template<int size, bool big_endian>
5950unsigned int
5951Stub_table<size, big_endian>::plt_call_size(
5952 typename Plt_stub_entries::const_iterator p) const
5953{
5954 if (size == 32)
5955 {
5956 const Symbol* gsym = p->first.sym_;
5957 return (4 * 4
5958 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
5959 }
5960
5961 const Output_data_plt_powerpc<size, big_endian>* plt;
5962 uint64_t plt_addr = this->plt_off(p, &plt);
5963 plt_addr += plt->address();
5964 unsigned int bytes = 0;
5965 const Symbol* gsym = p->first.sym_;
5966 if (this->targ_->is_tls_get_addr_opt(gsym))
5967 {
5968 if (p->second.r2save_ && !p->second.localentry0_)
5969 bytes = 13 * 4;
5970 else
5971 bytes = 7 * 4;
5972 }
5973
5974 if (p->second.r2save_)
5975 bytes += 4;
5976
e4dff765
AM
5977 if (this->targ_->powerxx_stubs())
5978 {
5979 uint64_t from = this->stub_address() + p->second.off_ + bytes;
5980 if (bytes > 8 * 4)
5981 from -= 4 * 4;
5982 uint64_t odd = from & 4;
5983 uint64_t off = plt_addr - from;
5984 if (off - odd + (1ULL << 33) < 1ULL << 34)
5985 bytes += odd + 4 * 4;
5986 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5987 bytes += 7 * 4;
5988 else
5989 bytes += 8 * 4;
5990 return bytes;
5991 }
5992
32f59844
AM
5993 if (p->second.notoc_)
5994 {
5995 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
5996 if (bytes > 32)
5997 from -= 4 * 4;
5998 uint64_t off = plt_addr - from;
5999 if (off + 0x8000 < 0x10000)
6000 bytes += 7 * 4;
6001 else if (off + 0x80008000ULL < 0x100000000ULL)
6002 bytes += 8 * 4;
6003 else
6004 {
6005 bytes += 8 * 4;
6006 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6007 && ((off >> 32) & 0xffff) != 0)
6008 bytes += 4;
6009 if (((off >> 32) & 0xffffffffULL) != 0)
6010 bytes += 4;
6011 if (hi(off) != 0)
6012 bytes += 4;
6013 if (l(off) != 0)
6014 bytes += 4;
6015 }
6016 return bytes;
6017 }
6018
6019 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6020 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6021 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6022 got_addr += ppcobj->toc_base_offset();
6023 uint64_t off = plt_addr - got_addr;
6024 bytes += 3 * 4 + 4 * (ha(off) != 0);
6025 if (this->targ_->abiversion() < 2)
6026 {
6027 bool static_chain = parameters->options().plt_static_chain();
6028 bool thread_safe = this->targ_->plt_thread_safe();
6029 bytes += (4
6030 + 4 * static_chain
6031 + 8 * thread_safe
6032 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6033 }
6034 return bytes;
6035}
6036
6037// Return long branch stub size.
6038
6039template<int size, bool big_endian>
6040unsigned int
6041Stub_table<size, big_endian>::branch_stub_size(
6042 typename Branch_stub_entries::const_iterator p,
6043 bool* need_lt)
6044{
6045 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6046 if (size == 32)
6047 {
6048 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6049 return 4;
6050 if (parameters->options().output_is_position_independent())
6051 return 32;
6052 return 16;
6053 }
6054
6055 uint64_t off = p->first.dest_ - loc;
6056 if (p->second.notoc_)
6057 {
e4dff765
AM
6058 if (this->targ_->powerxx_stubs())
6059 {
6060 Address odd = loc & 4;
6061 if (off + (1 << 25) < 2 << 25)
6062 return odd + 12;
6063 if (off - odd + (1ULL << 33) < 1ULL << 34)
6064 return odd + 16;
6065 if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6066 return 28;
6067 return 32;
6068 }
32f59844
AM
6069 off -= 8;
6070 if (off + 0x8000 < 0x10000)
6071 return 24;
6072 if (off + 0x80008000ULL < 0x100000000ULL)
6073 {
6074 if (off + 24 + (1 << 25) < 2 << 25)
6075 return 28;
6076 return 32;
6077 }
6078 unsigned int bytes = 32;
6079 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6080 && ((off >> 32) & 0xffff) != 0)
6081 bytes += 4;
6082 if (((off >> 32) & 0xffffffffULL) != 0)
6083 bytes += 4;
6084 if (hi(off) != 0)
6085 bytes += 4;
6086 if (l(off) != 0)
6087 bytes += 4;
6088 return bytes;
6089 }
6090
6091 if (off + (1 << 25) < 2 << 25)
6092 return 4;
e4dff765
AM
6093 if (!this->targ_->powerxx_stubs())
6094 *need_lt = true;
32f59844
AM
6095 return 16;
6096}
6097
f073a3e8
AM
6098template<int size, bool big_endian>
6099void
6100Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6101{
6102 if (p.sym_)
6103 gold_error(_("linkage table error against `%s'"),
6104 p.sym_->demangled_name().c_str());
6105 else
6106 gold_error(_("linkage table error against `%s:[local %u]'"),
6107 p.object_->name().c_str(),
6108 p.locsym_);
6109}
6110
ec661b9d 6111// Write out plt and long branch stub code.
cf43a2fe
AM
6112
6113template<int size, bool big_endian>
6114void
ec661b9d 6115Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 6116{
ec661b9d
AM
6117 if (this->plt_call_stubs_.empty()
6118 && this->long_branch_stubs_.empty())
6119 return;
6120
6121 const section_size_type start_off = this->offset();
6122 const section_size_type off = this->stub_offset();
42cacb20 6123 const section_size_type oview_size =
ec661b9d 6124 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 6125 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 6126 unsigned char* p;
42cacb20 6127
e4dff765
AM
6128 if (size == 64
6129 && this->targ_->powerxx_stubs())
6130 {
6131 if (!this->plt_call_stubs_.empty())
6132 {
6133 // Write out plt call stubs.
6134 typename Plt_stub_entries::const_iterator cs;
6135 for (cs = this->plt_call_stubs_.begin();
6136 cs != this->plt_call_stubs_.end();
6137 ++cs)
6138 {
6139 p = oview + cs->second.off_;
6140 this->build_tls_opt_head(&p, cs);
6141 if (cs->second.r2save_)
6142 {
6143 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6144 p += 4;
6145 }
6146 const Output_data_plt_powerpc<size, big_endian>* plt;
6147 Address pltoff = this->plt_off(cs, &plt);
6148 Address plt_addr = pltoff + plt->address();
6149 Address from = this->stub_address() + (p - oview);
6150 Address delta = plt_addr - from;
6151 p = build_powerxx_offset<big_endian>(p, delta, from & 4, true);
6152 write_insn<big_endian>(p, mtctr_12);
6153 p += 4;
6154 if (!this->build_tls_opt_tail(p, cs))
6155 write_insn<big_endian>(p, bctr);
6156 }
6157 }
6158
6159 // Write out long branch stubs.
6160 typename Branch_stub_entries::const_iterator bs;
6161 for (bs = this->long_branch_stubs_.begin();
6162 bs != this->long_branch_stubs_.end();
6163 ++bs)
6164 {
6165 if (bs->second.save_res_)
6166 continue;
6167 Address off = this->plt_size_ + bs->second.off_;
6168 p = oview + off;
6169 Address loc = this->stub_address() + off;
6170 Address delta = bs->first.dest_ - loc;
6171 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6172 {
6173 unsigned char* startp = p;
6174 p = build_powerxx_offset<big_endian>(p, delta, loc & 4, false);
6175 delta -= p - startp;
6176 }
6177 if (delta + (1 << 25) < 2 << 25)
6178 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6179 else
6180 {
6181 write_insn<big_endian>(p, mtctr_12);
6182 p += 4;
6183 write_insn<big_endian>(p, bctr);
6184 }
6185 }
6186 }
6187 else if (size == 64)
cf43a2fe 6188 {
ec661b9d
AM
6189 const Output_data_got_powerpc<size, big_endian>* got
6190 = this->targ_->got_section();
dd93cd0a 6191 Address got_os_addr = got->output_section()->address();
c9269dff 6192
32f59844
AM
6193 if (!this->plt_call_stubs_.empty()
6194 && this->targ_->abiversion() >= 2)
cf43a2fe 6195 {
32f59844
AM
6196 // Write out plt call stubs for ELFv2.
6197 typename Plt_stub_entries::const_iterator cs;
6198 for (cs = this->plt_call_stubs_.begin();
6199 cs != this->plt_call_stubs_.end();
6200 ++cs)
6201 {
6202 const Output_data_plt_powerpc<size, big_endian>* plt;
6203 Address pltoff = this->plt_off(cs, &plt);
6204 Address plt_addr = pltoff + plt->address();
6205
6206 p = oview + cs->second.off_;
6207 this->build_tls_opt_head(&p, cs);
6208 if (cs->second.r2save_)
6209 {
6210 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6211 p += 4;
6212 }
6213 if (cs->second.notoc_)
6214 {
6215 Address from = this->stub_address() + (p - oview) + 8;
6216 Address off = plt_addr - from;
6217 p = build_notoc_offset<big_endian>(p, off, true);
6218 }
6219 else
6220 {
6221 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6222 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6223 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6224 Address off = plt_addr - got_addr;
6225
6226 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6227 this->plt_error(cs->first);
6228
6229 if (ha(off) != 0)
6230 {
6231 write_insn<big_endian>(p, addis_12_2 + ha(off));
6232 p += 4;
6233 write_insn<big_endian>(p, ld_12_12 + l(off));
6234 p += 4;
6235 }
6236 else
6237 {
6238 write_insn<big_endian>(p, ld_12_2 + l(off));
6239 p += 4;
6240 }
6241 }
6242 write_insn<big_endian>(p, mtctr_12);
6243 p += 4;
6244 if (!this->build_tls_opt_tail(p, cs))
6245 write_insn<big_endian>(p, bctr);
6246 }
6247 }
6248 else if (!this->plt_call_stubs_.empty())
6249 {
6250 // Write out plt call stubs for ELFv1.
ec661b9d
AM
6251 typename Plt_stub_entries::const_iterator cs;
6252 for (cs = this->plt_call_stubs_.begin();
6253 cs != this->plt_call_stubs_.end();
6254 ++cs)
e5d5f5ed 6255 {
08be3224
AM
6256 const Output_data_plt_powerpc<size, big_endian>* plt;
6257 Address pltoff = this->plt_off(cs, &plt);
6258 Address plt_addr = pltoff + plt->address();
ec661b9d
AM
6259 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6260 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6261 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 6262 Address off = plt_addr - got_addr;
ec661b9d 6263
32f59844
AM
6264 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6265 || cs->second.notoc_)
f073a3e8 6266 this->plt_error(cs->first);
ec661b9d 6267
32f59844
AM
6268 bool static_chain = parameters->options().plt_static_chain();
6269 bool thread_safe = this->targ_->plt_thread_safe();
9e69ed50
AM
6270 bool use_fake_dep = false;
6271 Address cmp_branch_off = 0;
407aa07c 6272 if (thread_safe)
9e69ed50
AM
6273 {
6274 unsigned int pltindex
6275 = ((pltoff - this->targ_->first_plt_entry_offset())
6276 / this->targ_->plt_entry_size());
6277 Address glinkoff
9e390558 6278 = (this->targ_->glink_section()->pltresolve_size()
9e69ed50
AM
6279 + pltindex * 8);
6280 if (pltindex > 32768)
6281 glinkoff += (pltindex - 32768) * 4;
6282 Address to
6283 = this->targ_->glink_section()->address() + glinkoff;
6284 Address from
7e57d19e
AM
6285 = (this->stub_address() + cs->second.off_ + 20
6286 + 4 * cs->second.r2save_
9e69ed50
AM
6287 + 4 * (ha(off) != 0)
6288 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6289 + 4 * static_chain);
6290 cmp_branch_off = to - from;
6291 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6292 }
6293
bdab445c 6294 p = oview + cs->second.off_;
32f59844
AM
6295 if (this->build_tls_opt_head(&p, cs))
6296 use_fake_dep = thread_safe;
6297 if (cs->second.r2save_)
34e0882b 6298 {
32f59844 6299 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
34e0882b 6300 p += 4;
34e0882b 6301 }
9e69ed50 6302 if (ha(off) != 0)
ec661b9d 6303 {
32f59844
AM
6304 write_insn<big_endian>(p, addis_11_2 + ha(off));
6305 p += 4;
6306 write_insn<big_endian>(p, ld_12_11 + l(off));
6307 p += 4;
6308 if (ha(off + 8 + 8 * static_chain) != ha(off))
397998fc 6309 {
32f59844 6310 write_insn<big_endian>(p, addi_11_11 + l(off));
397998fc 6311 p += 4;
32f59844 6312 off = 0;
397998fc 6313 }
32f59844
AM
6314 write_insn<big_endian>(p, mtctr_12);
6315 p += 4;
6316 if (use_fake_dep)
397998fc 6317 {
32f59844 6318 write_insn<big_endian>(p, xor_2_12_12);
397998fc 6319 p += 4;
32f59844 6320 write_insn<big_endian>(p, add_11_11_2);
397998fc
AM
6321 p += 4;
6322 }
32f59844 6323 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
b4f7960d 6324 p += 4;
32f59844 6325 if (static_chain)
9e69ed50 6326 {
32f59844 6327 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
b4f7960d 6328 p += 4;
9e69ed50 6329 }
ec661b9d
AM
6330 }
6331 else
6332 {
b4f7960d
AM
6333 write_insn<big_endian>(p, ld_12_2 + l(off));
6334 p += 4;
32f59844 6335 if (ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 6336 {
b4f7960d
AM
6337 write_insn<big_endian>(p, addi_2_2 + l(off));
6338 p += 4;
9e69ed50 6339 off = 0;
ec661b9d 6340 }
b4f7960d
AM
6341 write_insn<big_endian>(p, mtctr_12);
6342 p += 4;
32f59844 6343 if (use_fake_dep)
9e69ed50 6344 {
32f59844
AM
6345 write_insn<big_endian>(p, xor_11_12_12);
6346 p += 4;
6347 write_insn<big_endian>(p, add_2_2_11);
b4f7960d 6348 p += 4;
9e69ed50 6349 }
32f59844
AM
6350 if (static_chain)
6351 {
6352 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6353 p += 4;
6354 }
6355 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
34e0882b 6356 p += 4;
34e0882b 6357 }
32f59844
AM
6358 if (this->build_tls_opt_tail(p, cs))
6359 ;
34e0882b 6360 else if (thread_safe && !use_fake_dep)
9e69ed50 6361 {
b4f7960d
AM
6362 write_insn<big_endian>(p, cmpldi_2_0);
6363 p += 4;
6364 write_insn<big_endian>(p, bnectr_p4);
6365 p += 4;
9e69ed50
AM
6366 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6367 }
6368 else
407aa07c 6369 write_insn<big_endian>(p, bctr);
e5d5f5ed 6370 }
ec661b9d
AM
6371 }
6372
6373 // Write out long branch stubs.
6374 typename Branch_stub_entries::const_iterator bs;
6375 for (bs = this->long_branch_stubs_.begin();
6376 bs != this->long_branch_stubs_.end();
6377 ++bs)
6378 {
32f59844 6379 if (bs->second.save_res_)
d49044c7 6380 continue;
32f59844
AM
6381 Address off = this->plt_size_ + bs->second.off_;
6382 p = oview + off;
6383 Address loc = this->stub_address() + off;
ec661b9d 6384 Address delta = bs->first.dest_ - loc;
32f59844
AM
6385 if (bs->second.notoc_)
6386 {
6387 unsigned char* startp = p;
6388 p = build_notoc_offset<big_endian>(p, off, false);
6389 delta -= p - startp;
6390 }
6391 else if (delta + (1 << 25) >= 2 << 25)
cf43a2fe 6392 {
ec661b9d
AM
6393 Address brlt_addr
6394 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6395 gold_assert(brlt_addr != invalid_address);
6396 brlt_addr += this->targ_->brlt_section()->address();
6397 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6398 Address brltoff = brlt_addr - got_addr;
6399 if (ha(brltoff) == 0)
6400 {
32f59844
AM
6401 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6402 p += 4;
ec661b9d
AM
6403 }
6404 else
cf43a2fe 6405 {
32f59844
AM
6406 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6407 p += 4;
6408 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6409 p += 4;
cf43a2fe 6410 }
32f59844
AM
6411 }
6412 if (delta + (1 << 25) < 2 << 25)
6413 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6414 else
6415 {
6416 write_insn<big_endian>(p, mtctr_12);
6417 p += 4;
407aa07c 6418 write_insn<big_endian>(p, bctr);
cf43a2fe 6419 }
ec661b9d
AM
6420 }
6421 }
32f59844 6422 else // size == 32
ec661b9d
AM
6423 {
6424 if (!this->plt_call_stubs_.empty())
6425 {
ec661b9d
AM
6426 // The address of _GLOBAL_OFFSET_TABLE_.
6427 Address g_o_t = invalid_address;
6428
6429 // Write out plt call stubs.
6430 typename Plt_stub_entries::const_iterator cs;
6431 for (cs = this->plt_call_stubs_.begin();
6432 cs != this->plt_call_stubs_.end();
6433 ++cs)
cf43a2fe 6434 {
08be3224
AM
6435 const Output_data_plt_powerpc<size, big_endian>* plt;
6436 Address plt_addr = this->plt_off(cs, &plt);
6437 plt_addr += plt->address();
ec661b9d 6438
bdab445c 6439 p = oview + cs->second.off_;
32f59844 6440 this->build_tls_opt_head(&p, cs);
ec661b9d
AM
6441 if (parameters->options().output_is_position_independent())
6442 {
6443 Address got_addr;
6444 const Powerpc_relobj<size, big_endian>* ppcobj
6445 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6446 (cs->first.object_));
6447 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6448 {
6449 unsigned int got2 = ppcobj->got2_shndx();
6450 got_addr = ppcobj->get_output_section_offset(got2);
6451 gold_assert(got_addr != invalid_address);
6452 got_addr += (ppcobj->output_section(got2)->address()
6453 + cs->first.addend_);
6454 }
6455 else
6456 {
6457 if (g_o_t == invalid_address)
6458 {
6459 const Output_data_got_powerpc<size, big_endian>* got
6460 = this->targ_->got_section();
6461 g_o_t = got->address() + got->g_o_t();
6462 }
6463 got_addr = g_o_t;
6464 }
6465
9e69ed50
AM
6466 Address off = plt_addr - got_addr;
6467 if (ha(off) == 0)
9e390558 6468 write_insn<big_endian>(p, lwz_11_30 + l(off));
ec661b9d
AM
6469 else
6470 {
9e390558
AM
6471 write_insn<big_endian>(p, addis_11_30 + ha(off));
6472 p += 4;
6473 write_insn<big_endian>(p, lwz_11_11 + l(off));
ec661b9d
AM
6474 }
6475 }
6476 else
6477 {
9e390558
AM
6478 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6479 p += 4;
6480 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
ec661b9d 6481 }
9e390558
AM
6482 p += 4;
6483 write_insn<big_endian>(p, mtctr_11);
6484 p += 4;
407aa07c 6485 write_insn<big_endian>(p, bctr);
ec661b9d
AM
6486 }
6487 }
6488
6489 // Write out long branch stubs.
6490 typename Branch_stub_entries::const_iterator bs;
6491 for (bs = this->long_branch_stubs_.begin();
6492 bs != this->long_branch_stubs_.end();
6493 ++bs)
6494 {
32f59844 6495 if (bs->second.save_res_)
d49044c7 6496 continue;
32f59844
AM
6497 Address off = this->plt_size_ + bs->second.off_;
6498 p = oview + off;
6499 Address loc = this->stub_address() + off;
ec661b9d
AM
6500 Address delta = bs->first.dest_ - loc;
6501 if (delta + (1 << 25) < 2 << 25)
6502 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6503 else if (!parameters->options().output_is_position_independent())
6504 {
9e390558
AM
6505 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6506 p += 4;
6507 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
ec661b9d
AM
6508 }
6509 else
6510 {
6511 delta -= 8;
9e390558
AM
6512 write_insn<big_endian>(p, mflr_0);
6513 p += 4;
6514 write_insn<big_endian>(p, bcl_20_31);
6515 p += 4;
6516 write_insn<big_endian>(p, mflr_12);
6517 p += 4;
6518 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6519 p += 4;
6520 write_insn<big_endian>(p, addi_12_12 + l(delta));
6521 p += 4;
6522 write_insn<big_endian>(p, mtlr_0);
cf43a2fe 6523 }
9e390558
AM
6524 p += 4;
6525 write_insn<big_endian>(p, mtctr_12);
6526 p += 4;
407aa07c 6527 write_insn<big_endian>(p, bctr);
cf43a2fe 6528 }
ec661b9d 6529 }
d49044c7
AM
6530 if (this->need_save_res_)
6531 {
6532 p = oview + this->plt_size_ + this->branch_size_;
6533 memcpy (p, this->targ_->savres_section()->contents(),
6534 this->targ_->savres_section()->data_size());
6535 }
ec661b9d
AM
6536}
6537
6538// Write out .glink.
6539
6540template<int size, bool big_endian>
6541void
6542Output_data_glink<size, big_endian>::do_write(Output_file* of)
6543{
6544 const section_size_type off = this->offset();
6545 const section_size_type oview_size =
6546 convert_to_section_size_type(this->data_size());
6547 unsigned char* const oview = of->get_output_view(off, oview_size);
6548 unsigned char* p;
6549
6550 // The base address of the .plt section.
6551 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6552 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 6553
ec661b9d
AM
6554 if (size == 64)
6555 {
9055360d 6556 if (this->end_branch_table_ != 0)
b4f7960d 6557 {
9055360d
AM
6558 // Write pltresolve stub.
6559 p = oview;
6560 Address after_bcl = this->address() + 16;
6561 Address pltoff = plt_base - after_bcl;
6562
6563 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 6564
b4f7960d 6565 if (this->targ_->abiversion() < 2)
cf43a2fe 6566 {
9055360d
AM
6567 write_insn<big_endian>(p, mflr_12), p += 4;
6568 write_insn<big_endian>(p, bcl_20_31), p += 4;
6569 write_insn<big_endian>(p, mflr_11), p += 4;
6570 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6571 write_insn<big_endian>(p, mtlr_12), p += 4;
6572 write_insn<big_endian>(p, add_11_2_11), p += 4;
6573 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6574 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6575 write_insn<big_endian>(p, mtctr_12), p += 4;
6576 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6577 }
6578 else
6579 {
6580 write_insn<big_endian>(p, mflr_0), p += 4;
6581 write_insn<big_endian>(p, bcl_20_31), p += 4;
6582 write_insn<big_endian>(p, mflr_11), p += 4;
7ee7ff70 6583 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
9055360d
AM
6584 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6585 write_insn<big_endian>(p, mtlr_0), p += 4;
6586 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6587 write_insn<big_endian>(p, add_11_2_11), p += 4;
6588 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
6589 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6590 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6591 write_insn<big_endian>(p, mtctr_12), p += 4;
6592 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6593 }
407aa07c 6594 write_insn<big_endian>(p, bctr), p += 4;
9e390558 6595 gold_assert(p == oview + this->pltresolve_size());
9055360d
AM
6596
6597 // Write lazy link call stubs.
6598 uint32_t indx = 0;
6599 while (p < oview + this->end_branch_table_)
6600 {
6601 if (this->targ_->abiversion() < 2)
b4f7960d 6602 {
9055360d
AM
6603 if (indx < 0x8000)
6604 {
6605 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6606 }
6607 else
6608 {
bbec1a5d 6609 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
6610 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6611 }
b4f7960d 6612 }
9055360d
AM
6613 uint32_t branch_off = 8 - (p - oview);
6614 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6615 indx++;
cf43a2fe 6616 }
9055360d
AM
6617 }
6618
6619 Address plt_base = this->targ_->plt_section()->address();
6620 Address iplt_base = invalid_address;
9e390558 6621 unsigned int global_entry_off = this->global_entry_off();
9055360d
AM
6622 Address global_entry_base = this->address() + global_entry_off;
6623 typename Global_entry_stub_entries::const_iterator ge;
6624 for (ge = this->global_entry_stubs_.begin();
6625 ge != this->global_entry_stubs_.end();
6626 ++ge)
6627 {
6628 p = oview + global_entry_off + ge->second;
6629 Address plt_addr = ge->first->plt_offset();
6630 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6631 && ge->first->can_use_relative_reloc(false))
6632 {
6633 if (iplt_base == invalid_address)
6634 iplt_base = this->targ_->iplt_section()->address();
6635 plt_addr += iplt_base;
6636 }
6637 else
6638 plt_addr += plt_base;
6639 Address my_addr = global_entry_base + ge->second;
6640 Address off = plt_addr - my_addr;
6641
6642 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
f073a3e8 6643 gold_error(_("linkage table error against `%s'"),
9055360d
AM
6644 ge->first->demangled_name().c_str());
6645
6646 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6647 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6648 write_insn<big_endian>(p, mtctr_12), p += 4;
407aa07c 6649 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
6650 }
6651 }
6652 else
6653 {
ec661b9d
AM
6654 const Output_data_got_powerpc<size, big_endian>* got
6655 = this->targ_->got_section();
dd93cd0a
AM
6656 // The address of _GLOBAL_OFFSET_TABLE_.
6657 Address g_o_t = got->address() + got->g_o_t();
c9269dff 6658
cf43a2fe 6659 // Write out pltresolve branch table.
ec661b9d 6660 p = oview;
9e390558 6661 unsigned int the_end = oview_size - this->pltresolve_size();
c9269dff 6662 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
6663 while (p < end_p - 8 * 4)
6664 write_insn<big_endian>(p, b + end_p - p), p += 4;
6665 while (p < end_p)
6666 write_insn<big_endian>(p, nop), p += 4;
42cacb20 6667
cf43a2fe 6668 // Write out pltresolve call stub.
9e390558 6669 end_p = oview + oview_size;
cf43a2fe 6670 if (parameters->options().output_is_position_independent())
42cacb20 6671 {
ec661b9d 6672 Address res0_off = 0;
dd93cd0a
AM
6673 Address after_bcl_off = the_end + 12;
6674 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe 6675
9e390558
AM
6676 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6677 p += 4;
6678 write_insn<big_endian>(p, mflr_0);
6679 p += 4;
6680 write_insn<big_endian>(p, bcl_20_31);
6681 p += 4;
6682 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6683 p += 4;
6684 write_insn<big_endian>(p, mflr_12);
6685 p += 4;
6686 write_insn<big_endian>(p, mtlr_0);
6687 p += 4;
6688 write_insn<big_endian>(p, sub_11_11_12);
6689 p += 4;
cf43a2fe 6690
dd93cd0a 6691 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe 6692
9e390558
AM
6693 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6694 p += 4;
cf43a2fe
AM
6695 if (ha(got_bcl) == ha(got_bcl + 4))
6696 {
9e390558
AM
6697 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6698 p += 4;
6699 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
cf43a2fe
AM
6700 }
6701 else
6702 {
9e390558
AM
6703 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6704 p += 4;
6705 write_insn<big_endian>(p, lwz_12_12 + 4);
cf43a2fe 6706 }
9e390558
AM
6707 p += 4;
6708 write_insn<big_endian>(p, mtctr_0);
6709 p += 4;
6710 write_insn<big_endian>(p, add_0_11_11);
6711 p += 4;
6712 write_insn<big_endian>(p, add_11_0_11);
42cacb20 6713 }
cf43a2fe 6714 else
42cacb20 6715 {
ec661b9d 6716 Address res0 = this->address();
cf43a2fe 6717
9e390558
AM
6718 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6719 p += 4;
6720 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
6721 p += 4;
cf43a2fe 6722 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 6723 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
cf43a2fe 6724 else
9e390558
AM
6725 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
6726 p += 4;
6727 write_insn<big_endian>(p, addi_11_11 + l(-res0));
6728 p += 4;
6729 write_insn<big_endian>(p, mtctr_0);
6730 p += 4;
6731 write_insn<big_endian>(p, add_0_11_11);
6732 p += 4;
cf43a2fe 6733 if (ha(g_o_t + 4) == ha(g_o_t + 8))
9e390558 6734 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
cf43a2fe 6735 else
9e390558
AM
6736 write_insn<big_endian>(p, lwz_12_12 + 4);
6737 p += 4;
6738 write_insn<big_endian>(p, add_11_0_11);
6739 }
6740 p += 4;
407aa07c
AM
6741 write_insn<big_endian>(p, bctr);
6742 p += 4;
9e390558
AM
6743 while (p < end_p)
6744 {
6745 write_insn<big_endian>(p, nop);
6746 p += 4;
42cacb20
DE
6747 }
6748 }
6749
cf43a2fe
AM
6750 of->write_output_view(off, oview_size, oview);
6751}
6752
f3a0ed29
AM
6753
6754// A class to handle linker generated save/restore functions.
6755
6756template<int size, bool big_endian>
6757class Output_data_save_res : public Output_section_data_build
6758{
6759 public:
6760 Output_data_save_res(Symbol_table* symtab);
6761
d49044c7
AM
6762 const unsigned char*
6763 contents() const
6764 {
6765 return contents_;
6766 }
6767
f3a0ed29
AM
6768 protected:
6769 // Write to a map file.
6770 void
6771 do_print_to_mapfile(Mapfile* mapfile) const
6772 { mapfile->print_output_data(this, _("** save/restore")); }
6773
6774 void
6775 do_write(Output_file*);
6776
6777 private:
6778 // The maximum size of save/restore contents.
6779 static const unsigned int savres_max = 218*4;
6780
6781 void
6782 savres_define(Symbol_table* symtab,
6783 const char *name,
6784 unsigned int lo, unsigned int hi,
6785 unsigned char* write_ent(unsigned char*, int),
6786 unsigned char* write_tail(unsigned char*, int));
6787
6788 unsigned char *contents_;
6789};
6790
6791template<bool big_endian>
6792static unsigned char*
6793savegpr0(unsigned char* p, int r)
6794{
6795 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6796 write_insn<big_endian>(p, insn);
6797 return p + 4;
6798}
6799
6800template<bool big_endian>
6801static unsigned char*
6802savegpr0_tail(unsigned char* p, int r)
6803{
6804 p = savegpr0<big_endian>(p, r);
6805 uint32_t insn = std_0_1 + 16;
6806 write_insn<big_endian>(p, insn);
6807 p = p + 4;
6808 write_insn<big_endian>(p, blr);
6809 return p + 4;
6810}
6811
6812template<bool big_endian>
62fe925a 6813static unsigned char*
f3a0ed29
AM
6814restgpr0(unsigned char* p, int r)
6815{
6816 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6817 write_insn<big_endian>(p, insn);
6818 return p + 4;
6819}
6820
6821template<bool big_endian>
62fe925a 6822static unsigned char*
f3a0ed29
AM
6823restgpr0_tail(unsigned char* p, int r)
6824{
6825 uint32_t insn = ld_0_1 + 16;
6826 write_insn<big_endian>(p, insn);
6827 p = p + 4;
6828 p = restgpr0<big_endian>(p, r);
6829 write_insn<big_endian>(p, mtlr_0);
6830 p = p + 4;
6831 if (r == 29)
6832 {
6833 p = restgpr0<big_endian>(p, 30);
6834 p = restgpr0<big_endian>(p, 31);
6835 }
6836 write_insn<big_endian>(p, blr);
6837 return p + 4;
6838}
6839
6840template<bool big_endian>
62fe925a 6841static unsigned char*
f3a0ed29
AM
6842savegpr1(unsigned char* p, int r)
6843{
6844 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6845 write_insn<big_endian>(p, insn);
6846 return p + 4;
6847}
6848
6849template<bool big_endian>
62fe925a 6850static unsigned char*
f3a0ed29
AM
6851savegpr1_tail(unsigned char* p, int r)
6852{
6853 p = savegpr1<big_endian>(p, r);
6854 write_insn<big_endian>(p, blr);
6855 return p + 4;
6856}
6857
6858template<bool big_endian>
62fe925a 6859static unsigned char*
f3a0ed29
AM
6860restgpr1(unsigned char* p, int r)
6861{
6862 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6863 write_insn<big_endian>(p, insn);
6864 return p + 4;
6865}
6866
6867template<bool big_endian>
62fe925a 6868static unsigned char*
f3a0ed29
AM
6869restgpr1_tail(unsigned char* p, int r)
6870{
6871 p = restgpr1<big_endian>(p, r);
6872 write_insn<big_endian>(p, blr);
6873 return p + 4;
6874}
6875
6876template<bool big_endian>
62fe925a 6877static unsigned char*
f3a0ed29
AM
6878savefpr(unsigned char* p, int r)
6879{
6880 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6881 write_insn<big_endian>(p, insn);
6882 return p + 4;
6883}
6884
6885template<bool big_endian>
62fe925a 6886static unsigned char*
f3a0ed29
AM
6887savefpr0_tail(unsigned char* p, int r)
6888{
6889 p = savefpr<big_endian>(p, r);
6890 write_insn<big_endian>(p, std_0_1 + 16);
6891 p = p + 4;
6892 write_insn<big_endian>(p, blr);
6893 return p + 4;
6894}
6895
6896template<bool big_endian>
62fe925a 6897static unsigned char*
f3a0ed29
AM
6898restfpr(unsigned char* p, int r)
6899{
6900 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6901 write_insn<big_endian>(p, insn);
6902 return p + 4;
6903}
6904
6905template<bool big_endian>
62fe925a 6906static unsigned char*
f3a0ed29
AM
6907restfpr0_tail(unsigned char* p, int r)
6908{
6909 write_insn<big_endian>(p, ld_0_1 + 16);
6910 p = p + 4;
6911 p = restfpr<big_endian>(p, r);
6912 write_insn<big_endian>(p, mtlr_0);
6913 p = p + 4;
6914 if (r == 29)
6915 {
6916 p = restfpr<big_endian>(p, 30);
6917 p = restfpr<big_endian>(p, 31);
6918 }
6919 write_insn<big_endian>(p, blr);
6920 return p + 4;
6921}
6922
6923template<bool big_endian>
62fe925a 6924static unsigned char*
f3a0ed29
AM
6925savefpr1_tail(unsigned char* p, int r)
6926{
6927 p = savefpr<big_endian>(p, r);
6928 write_insn<big_endian>(p, blr);
6929 return p + 4;
6930}
6931
6932template<bool big_endian>
62fe925a 6933static unsigned char*
f3a0ed29
AM
6934restfpr1_tail(unsigned char* p, int r)
6935{
6936 p = restfpr<big_endian>(p, r);
6937 write_insn<big_endian>(p, blr);
6938 return p + 4;
6939}
6940
6941template<bool big_endian>
62fe925a 6942static unsigned char*
f3a0ed29
AM
6943savevr(unsigned char* p, int r)
6944{
6945 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6946 write_insn<big_endian>(p, insn);
6947 p = p + 4;
6948 insn = stvx_0_12_0 + (r << 21);
6949 write_insn<big_endian>(p, insn);
6950 return p + 4;
6951}
6952
6953template<bool big_endian>
62fe925a 6954static unsigned char*
f3a0ed29
AM
6955savevr_tail(unsigned char* p, int r)
6956{
6957 p = savevr<big_endian>(p, r);
6958 write_insn<big_endian>(p, blr);
6959 return p + 4;
6960}
6961
6962template<bool big_endian>
62fe925a 6963static unsigned char*
f3a0ed29
AM
6964restvr(unsigned char* p, int r)
6965{
6966 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6967 write_insn<big_endian>(p, insn);
6968 p = p + 4;
6969 insn = lvx_0_12_0 + (r << 21);
6970 write_insn<big_endian>(p, insn);
6971 return p + 4;
6972}
6973
6974template<bool big_endian>
62fe925a 6975static unsigned char*
f3a0ed29
AM
6976restvr_tail(unsigned char* p, int r)
6977{
6978 p = restvr<big_endian>(p, r);
6979 write_insn<big_endian>(p, blr);
6980 return p + 4;
6981}
6982
6983
6984template<int size, bool big_endian>
6985Output_data_save_res<size, big_endian>::Output_data_save_res(
6986 Symbol_table* symtab)
6987 : Output_section_data_build(4),
6988 contents_(NULL)
6989{
6990 this->savres_define(symtab,
6991 "_savegpr0_", 14, 31,
6992 savegpr0<big_endian>, savegpr0_tail<big_endian>);
6993 this->savres_define(symtab,
6994 "_restgpr0_", 14, 29,
6995 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6996 this->savres_define(symtab,
6997 "_restgpr0_", 30, 31,
6998 restgpr0<big_endian>, restgpr0_tail<big_endian>);
6999 this->savres_define(symtab,
7000 "_savegpr1_", 14, 31,
7001 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7002 this->savres_define(symtab,
7003 "_restgpr1_", 14, 31,
7004 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7005 this->savres_define(symtab,
7006 "_savefpr_", 14, 31,
7007 savefpr<big_endian>, savefpr0_tail<big_endian>);
7008 this->savres_define(symtab,
7009 "_restfpr_", 14, 29,
7010 restfpr<big_endian>, restfpr0_tail<big_endian>);
7011 this->savres_define(symtab,
7012 "_restfpr_", 30, 31,
7013 restfpr<big_endian>, restfpr0_tail<big_endian>);
7014 this->savres_define(symtab,
7015 "._savef", 14, 31,
7016 savefpr<big_endian>, savefpr1_tail<big_endian>);
7017 this->savres_define(symtab,
7018 "._restf", 14, 31,
7019 restfpr<big_endian>, restfpr1_tail<big_endian>);
7020 this->savres_define(symtab,
7021 "_savevr_", 20, 31,
7022 savevr<big_endian>, savevr_tail<big_endian>);
7023 this->savres_define(symtab,
7024 "_restvr_", 20, 31,
7025 restvr<big_endian>, restvr_tail<big_endian>);
7026}
7027
7028template<int size, bool big_endian>
7029void
7030Output_data_save_res<size, big_endian>::savres_define(
7031 Symbol_table* symtab,
7032 const char *name,
7033 unsigned int lo, unsigned int hi,
7034 unsigned char* write_ent(unsigned char*, int),
7035 unsigned char* write_tail(unsigned char*, int))
7036{
7037 size_t len = strlen(name);
7038 bool writing = false;
7039 char sym[16];
7040
7041 memcpy(sym, name, len);
7042 sym[len + 2] = 0;
7043
7044 for (unsigned int i = lo; i <= hi; i++)
7045 {
7046 sym[len + 0] = i / 10 + '0';
7047 sym[len + 1] = i % 10 + '0';
7048 Symbol* gsym = symtab->lookup(sym);
7049 bool refd = gsym != NULL && gsym->is_undefined();
7050 writing = writing || refd;
7051 if (writing)
7052 {
7053 if (this->contents_ == NULL)
7054 this->contents_ = new unsigned char[this->savres_max];
7055
ec661b9d 7056 section_size_type value = this->current_data_size();
f3a0ed29
AM
7057 unsigned char* p = this->contents_ + value;
7058 if (i != hi)
7059 p = write_ent(p, i);
7060 else
7061 p = write_tail(p, i);
ec661b9d 7062 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
7063 this->set_current_data_size(cur_size);
7064 if (refd)
7065 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7066 this, value, cur_size - value,
7067 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7068 elfcpp::STV_HIDDEN, 0, false, false);
7069 }
7070 }
7071}
7072
7073// Write out save/restore.
7074
7075template<int size, bool big_endian>
7076void
7077Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7078{
ec661b9d 7079 const section_size_type off = this->offset();
f3a0ed29
AM
7080 const section_size_type oview_size =
7081 convert_to_section_size_type(this->data_size());
7082 unsigned char* const oview = of->get_output_view(off, oview_size);
7083 memcpy(oview, this->contents_, oview_size);
7084 of->write_output_view(off, oview_size, oview);
7085}
7086
7087
cf43a2fe 7088// Create the glink section.
42cacb20 7089
cf43a2fe
AM
7090template<int size, bool big_endian>
7091void
7092Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7093{
7094 if (this->glink_ == NULL)
7095 {
7096 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 7097 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
7098 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7099 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7100 this->glink_, ORDER_TEXT, false);
7101 }
42cacb20
DE
7102}
7103
7104// Create a PLT entry for a global symbol.
7105
7106template<int size, bool big_endian>
7107void
ec661b9d
AM
7108Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7109 Layout* layout,
7110 Symbol* gsym)
42cacb20 7111{
e5d5f5ed
AM
7112 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7113 && gsym->can_use_relative_reloc(false))
7114 {
7115 if (this->iplt_ == NULL)
40b469d7 7116 this->make_iplt_section(symtab, layout);
03e25981 7117 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
7118 }
7119 else
7120 {
7121 if (this->plt_ == NULL)
40b469d7 7122 this->make_plt_section(symtab, layout);
03e25981 7123 this->plt_->add_entry(gsym);
e5d5f5ed 7124 }
e5d5f5ed 7125}
42cacb20 7126
2d7ad24e
AM
7127// Make a PLT entry for a local symbol.
7128
7129template<int size, bool big_endian>
7130void
7131Target_powerpc<size, big_endian>::make_local_plt_entry(
7132 Layout* layout,
7133 Sized_relobj_file<size, big_endian>* relobj,
7134 unsigned int r_sym)
7135{
7136 if (this->lplt_ == NULL)
7137 this->make_lplt_section(layout);
7138 this->lplt_->add_local_entry(relobj, r_sym);
7139}
7140
e5d5f5ed 7141// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 7142
e5d5f5ed
AM
7143template<int size, bool big_endian>
7144void
7145Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 7146 Symbol_table* symtab,
e5d5f5ed 7147 Layout* layout,
ec661b9d
AM
7148 Sized_relobj_file<size, big_endian>* relobj,
7149 unsigned int r_sym)
e5d5f5ed
AM
7150{
7151 if (this->iplt_ == NULL)
40b469d7 7152 this->make_iplt_section(symtab, layout);
03e25981 7153 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
7154}
7155
0e70b911
CC
7156// Return the number of entries in the PLT.
7157
7158template<int size, bool big_endian>
7159unsigned int
7160Target_powerpc<size, big_endian>::plt_entry_count() const
7161{
7162 if (this->plt_ == NULL)
7163 return 0;
b3ccdeb5 7164 return this->plt_->entry_count();
0e70b911
CC
7165}
7166
dd93cd0a 7167// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
7168
7169template<int size, bool big_endian>
7170unsigned int
dd93cd0a 7171Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
7172 Symbol_table* symtab,
7173 Layout* layout,
7174 Sized_relobj_file<size, big_endian>* object)
42cacb20 7175{
dd93cd0a 7176 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
7177 {
7178 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7179 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
7180 Output_data_got_powerpc<size, big_endian>* got
7181 = this->got_section(symtab, layout);
7182 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
7183 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7184 got_offset, 0);
dd93cd0a 7185 this->tlsld_got_offset_ = got_offset;
42cacb20 7186 }
dd93cd0a 7187 return this->tlsld_got_offset_;
42cacb20
DE
7188}
7189
95a2c8d6
RS
7190// Get the Reference_flags for a particular relocation.
7191
7192template<int size, bool big_endian>
7193int
88b8e639
AM
7194Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7195 unsigned int r_type,
7196 const Target_powerpc* target)
95a2c8d6 7197{
88b8e639
AM
7198 int ref = 0;
7199
95a2c8d6
RS
7200 switch (r_type)
7201 {
7202 case elfcpp::R_POWERPC_NONE:
7203 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7204 case elfcpp::R_POWERPC_GNU_VTENTRY:
7205 case elfcpp::R_PPC64_TOC:
7206 // No symbol reference.
88b8e639 7207 break;
95a2c8d6 7208
dd93cd0a
AM
7209 case elfcpp::R_PPC64_ADDR64:
7210 case elfcpp::R_PPC64_UADDR64:
7211 case elfcpp::R_POWERPC_ADDR32:
7212 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 7213 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 7214 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
7215 case elfcpp::R_POWERPC_ADDR16_LO:
7216 case elfcpp::R_POWERPC_ADDR16_HI:
7217 case elfcpp::R_POWERPC_ADDR16_HA:
89c52ae3
AM
7218 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7219 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7220 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7221 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7222 case elfcpp::R_PPC64_D34:
7223 case elfcpp::R_PPC64_D34_LO:
7224 case elfcpp::R_PPC64_D34_HI30:
7225 case elfcpp::R_PPC64_D34_HA30:
7226 case elfcpp::R_PPC64_D28:
88b8e639
AM
7227 ref = Symbol::ABSOLUTE_REF;
7228 break;
95a2c8d6 7229
dd93cd0a
AM
7230 case elfcpp::R_POWERPC_ADDR24:
7231 case elfcpp::R_POWERPC_ADDR14:
7232 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7233 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
7234 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7235 break;
dd93cd0a 7236
e5d5f5ed 7237 case elfcpp::R_PPC64_REL64:
dd93cd0a 7238 case elfcpp::R_POWERPC_REL32:
95a2c8d6 7239 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
7240 case elfcpp::R_POWERPC_REL16:
7241 case elfcpp::R_POWERPC_REL16_LO:
7242 case elfcpp::R_POWERPC_REL16_HI:
7243 case elfcpp::R_POWERPC_REL16_HA:
c432bbba
AM
7244 case elfcpp::R_PPC64_REL16_HIGH:
7245 case elfcpp::R_PPC64_REL16_HIGHA:
7246 case elfcpp::R_PPC64_REL16_HIGHER:
7247 case elfcpp::R_PPC64_REL16_HIGHERA:
7248 case elfcpp::R_PPC64_REL16_HIGHEST:
7249 case elfcpp::R_PPC64_REL16_HIGHESTA:
e4dff765
AM
7250 case elfcpp::R_PPC64_PCREL34:
7251 case elfcpp::R_PPC64_REL16_HIGHER34:
7252 case elfcpp::R_PPC64_REL16_HIGHERA34:
7253 case elfcpp::R_PPC64_REL16_HIGHEST34:
7254 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7255 case elfcpp::R_PPC64_PCREL28:
88b8e639
AM
7256 ref = Symbol::RELATIVE_REF;
7257 break;
95a2c8d6 7258
32f59844
AM
7259 case elfcpp::R_PPC64_REL24_NOTOC:
7260 if (size == 32)
7261 break;
7262 // Fall through.
dd93cd0a 7263 case elfcpp::R_POWERPC_REL24:
95a2c8d6 7264 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
7265 case elfcpp::R_POWERPC_REL14:
7266 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7267 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
7268 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7269 break;
95a2c8d6
RS
7270
7271 case elfcpp::R_POWERPC_GOT16:
7272 case elfcpp::R_POWERPC_GOT16_LO:
7273 case elfcpp::R_POWERPC_GOT16_HI:
7274 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
7275 case elfcpp::R_PPC64_GOT16_DS:
7276 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7277 case elfcpp::R_PPC64_GOT_PCREL34:
95a2c8d6
RS
7278 case elfcpp::R_PPC64_TOC16:
7279 case elfcpp::R_PPC64_TOC16_LO:
7280 case elfcpp::R_PPC64_TOC16_HI:
7281 case elfcpp::R_PPC64_TOC16_HA:
7282 case elfcpp::R_PPC64_TOC16_DS:
7283 case elfcpp::R_PPC64_TOC16_LO_DS:
08be3224
AM
7284 case elfcpp::R_POWERPC_PLT16_LO:
7285 case elfcpp::R_POWERPC_PLT16_HI:
7286 case elfcpp::R_POWERPC_PLT16_HA:
7287 case elfcpp::R_PPC64_PLT16_LO_DS:
e4dff765
AM
7288 case elfcpp::R_PPC64_PLT_PCREL34:
7289 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
32d849b3 7290 ref = Symbol::RELATIVE_REF;
88b8e639 7291 break;
95a2c8d6
RS
7292
7293 case elfcpp::R_POWERPC_GOT_TPREL16:
7294 case elfcpp::R_POWERPC_TLS:
89c52ae3
AM
7295 case elfcpp::R_PPC64_TLSGD:
7296 case elfcpp::R_PPC64_TLSLD:
7297 case elfcpp::R_PPC64_TPREL34:
7298 case elfcpp::R_PPC64_DTPREL34:
7299 case elfcpp::R_PPC64_GOT_TLSGD34:
7300 case elfcpp::R_PPC64_GOT_TLSLD34:
7301 case elfcpp::R_PPC64_GOT_TPREL34:
7302 case elfcpp::R_PPC64_GOT_DTPREL34:
88b8e639
AM
7303 ref = Symbol::TLS_REF;
7304 break;
95a2c8d6
RS
7305
7306 case elfcpp::R_POWERPC_COPY:
7307 case elfcpp::R_POWERPC_GLOB_DAT:
7308 case elfcpp::R_POWERPC_JMP_SLOT:
7309 case elfcpp::R_POWERPC_RELATIVE:
7310 case elfcpp::R_POWERPC_DTPMOD:
7311 default:
7312 // Not expected. We will give an error later.
88b8e639 7313 break;
95a2c8d6 7314 }
88b8e639
AM
7315
7316 if (size == 64 && target->abiversion() < 2)
7317 ref |= Symbol::FUNC_DESC_ABI;
7318 return ref;
95a2c8d6
RS
7319}
7320
42cacb20
DE
7321// Report an unsupported relocation against a local symbol.
7322
7323template<int size, bool big_endian>
7324void
7325Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
7326 Sized_relobj_file<size, big_endian>* object,
7327 unsigned int r_type)
42cacb20
DE
7328{
7329 gold_error(_("%s: unsupported reloc %u against local symbol"),
7330 object->name().c_str(), r_type);
7331}
7332
7333// We are about to emit a dynamic relocation of type R_TYPE. If the
7334// dynamic linker does not support it, issue an error.
7335
7336template<int size, bool big_endian>
7337void
7338Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7339 unsigned int r_type)
7340{
7341 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7342
7343 // These are the relocation types supported by glibc for both 32-bit
7344 // and 64-bit powerpc.
7345 switch (r_type)
7346 {
3ea0a085 7347 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
7348 case elfcpp::R_POWERPC_RELATIVE:
7349 case elfcpp::R_POWERPC_GLOB_DAT:
7350 case elfcpp::R_POWERPC_DTPMOD:
7351 case elfcpp::R_POWERPC_DTPREL:
7352 case elfcpp::R_POWERPC_TPREL:
7353 case elfcpp::R_POWERPC_JMP_SLOT:
7354 case elfcpp::R_POWERPC_COPY:
3ea0a085 7355 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 7356 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 7357 case elfcpp::R_POWERPC_UADDR32:
42cacb20 7358 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
7359 case elfcpp::R_POWERPC_ADDR16:
7360 case elfcpp::R_POWERPC_UADDR16:
7361 case elfcpp::R_POWERPC_ADDR16_LO:
7362 case elfcpp::R_POWERPC_ADDR16_HI:
7363 case elfcpp::R_POWERPC_ADDR16_HA:
7364 case elfcpp::R_POWERPC_ADDR14:
7365 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7366 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7367 case elfcpp::R_POWERPC_REL32:
3ea0a085
AM
7368 case elfcpp::R_POWERPC_TPREL16:
7369 case elfcpp::R_POWERPC_TPREL16_LO:
7370 case elfcpp::R_POWERPC_TPREL16_HI:
7371 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
7372 return;
7373
7374 default:
7375 break;
7376 }
7377
7378 if (size == 64)
7379 {
7380 switch (r_type)
7381 {
7382 // These are the relocation types supported only on 64-bit.
7383 case elfcpp::R_PPC64_ADDR64:
42cacb20 7384 case elfcpp::R_PPC64_UADDR64:
3ea0a085 7385 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 7386 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 7387 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
7388 case elfcpp::R_PPC64_ADDR16_HIGH:
7389 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
7390 case elfcpp::R_PPC64_ADDR16_HIGHER:
7391 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7392 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7393 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 7394 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
7395 case elfcpp::R_POWERPC_ADDR30:
7396 case elfcpp::R_PPC64_TPREL16_DS:
7397 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7398 case elfcpp::R_PPC64_TPREL16_HIGH:
7399 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
7400 case elfcpp::R_PPC64_TPREL16_HIGHER:
7401 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7402 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7403 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
7404 return;
7405
7406 default:
7407 break;
7408 }
7409 }
7410 else
7411 {
7412 switch (r_type)
7413 {
7414 // These are the relocation types supported only on 32-bit.
3ea0a085 7415 // ??? glibc ld.so doesn't need to support these.
e59a1001 7416 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
7417 case elfcpp::R_POWERPC_DTPREL16:
7418 case elfcpp::R_POWERPC_DTPREL16_LO:
7419 case elfcpp::R_POWERPC_DTPREL16_HI:
7420 case elfcpp::R_POWERPC_DTPREL16_HA:
7421 return;
42cacb20
DE
7422
7423 default:
7424 break;
7425 }
7426 }
7427
7428 // This prevents us from issuing more than one error per reloc
7429 // section. But we can still wind up issuing more than one
7430 // error per object file.
7431 if (this->issued_non_pic_error_)
7432 return;
33aea2fd 7433 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
7434 object->error(_("requires unsupported dynamic reloc; "
7435 "recompile with -fPIC"));
7436 this->issued_non_pic_error_ = true;
7437 return;
7438}
7439
e5d5f5ed
AM
7440// Return whether we need to make a PLT entry for a relocation of the
7441// given type against a STT_GNU_IFUNC symbol.
7442
7443template<int size, bool big_endian>
7444bool
7445Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 7446 Target_powerpc<size, big_endian>* target,
e5d5f5ed 7447 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
7448 unsigned int r_type,
7449 bool report_err)
e5d5f5ed 7450{
c9824451
AM
7451 // In non-pic code any reference will resolve to the plt call stub
7452 // for the ifunc symbol.
9055360d
AM
7453 if ((size == 32 || target->abiversion() >= 2)
7454 && !parameters->options().output_is_position_independent())
c9824451
AM
7455 return true;
7456
e5d5f5ed
AM
7457 switch (r_type)
7458 {
b3ccdeb5 7459 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
7460 case elfcpp::R_POWERPC_ADDR32:
7461 case elfcpp::R_POWERPC_UADDR32:
7462 if (size == 32)
b3ccdeb5 7463 return false;
e5d5f5ed
AM
7464 break;
7465
7466 case elfcpp::R_PPC64_ADDR64:
7467 case elfcpp::R_PPC64_UADDR64:
7468 if (size == 64)
b3ccdeb5 7469 return false;
e5d5f5ed
AM
7470 break;
7471
b3ccdeb5 7472 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
7473 case elfcpp::R_POWERPC_GOT16:
7474 case elfcpp::R_POWERPC_GOT16_LO:
7475 case elfcpp::R_POWERPC_GOT16_HI:
7476 case elfcpp::R_POWERPC_GOT16_HA:
7477 case elfcpp::R_PPC64_GOT16_DS:
7478 case elfcpp::R_PPC64_GOT16_LO_DS:
e4dff765 7479 case elfcpp::R_PPC64_GOT_PCREL34:
b3ccdeb5 7480 return false;
e5d5f5ed 7481
08be3224
AM
7482 // PLT relocs are OK and need a PLT entry.
7483 case elfcpp::R_POWERPC_PLT16_LO:
7484 case elfcpp::R_POWERPC_PLT16_HI:
7485 case elfcpp::R_POWERPC_PLT16_HA:
7486 case elfcpp::R_PPC64_PLT16_LO_DS:
23cedd1d
AM
7487 case elfcpp::R_POWERPC_PLTSEQ:
7488 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7489 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7490 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7491 case elfcpp::R_PPC64_PLT_PCREL34:
7492 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
7493 return true;
7494 break;
7495
b3ccdeb5 7496 // Function calls are good, and these do need a PLT entry.
32f59844
AM
7497 case elfcpp::R_PPC64_REL24_NOTOC:
7498 if (size == 32)
7499 break;
7500 // Fall through.
e5d5f5ed
AM
7501 case elfcpp::R_POWERPC_ADDR24:
7502 case elfcpp::R_POWERPC_ADDR14:
7503 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7504 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7505 case elfcpp::R_POWERPC_REL24:
7506 case elfcpp::R_PPC_PLTREL24:
7507 case elfcpp::R_POWERPC_REL14:
7508 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7509 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7510 return true;
7511
7512 default:
7513 break;
7514 }
7515
7516 // Anything else is a problem.
7517 // If we are building a static executable, the libc startup function
7518 // responsible for applying indirect function relocations is going
7519 // to complain about the reloc type.
7520 // If we are building a dynamic executable, we will have a text
7521 // relocation. The dynamic loader will set the text segment
7522 // writable and non-executable to apply text relocations. So we'll
7523 // segfault when trying to run the indirection function to resolve
7524 // the reloc.
b3ccdeb5
AM
7525 if (report_err)
7526 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
7527 object->name().c_str(), r_type);
7528 return false;
7529}
7530
5edad15d
AM
7531// Return TRUE iff INSN is one we expect on a _LO variety toc/got
7532// reloc.
7533
7534static bool
7535ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7536{
7537 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7538 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7539 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7540 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7541 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7542 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7543 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7544 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7545 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7546 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7547 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7548 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7549 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7550 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7551 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7552 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7553 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7554 /* Exclude lfqu by testing reloc. If relocs are ever
7555 defined for the reduced D field in psq_lu then those
7556 will need testing too. */
7557 && r_type != elfcpp::R_PPC64_TOC16_LO
7558 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7559 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7560 && (insn & 1) == 0)
7561 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7562 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7563 /* Exclude stfqu. psq_stu as above for psq_lu. */
7564 && r_type != elfcpp::R_PPC64_TOC16_LO
7565 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7566 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7567 && (insn & 1) == 0));
7568}
7569
42cacb20
DE
7570// Scan a relocation for a local symbol.
7571
7572template<int size, bool big_endian>
7573inline void
7574Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
7575 Symbol_table* symtab,
7576 Layout* layout,
7577 Target_powerpc<size, big_endian>* target,
7578 Sized_relobj_file<size, big_endian>* object,
7579 unsigned int data_shndx,
7580 Output_section* output_section,
7581 const elfcpp::Rela<size, big_endian>& reloc,
7582 unsigned int r_type,
e5d5f5ed 7583 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 7584 bool is_discarded)
42cacb20 7585{
34e0882b 7586 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
7587
7588 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7589 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7590 {
7591 this->expect_tls_get_addr_call();
7592 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7593 if (tls_type != tls::TLSOPT_NONE)
7594 this->skip_next_tls_get_addr_call();
7595 }
7596 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7597 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7598 {
7599 this->expect_tls_get_addr_call();
7600 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7601 if (tls_type != tls::TLSOPT_NONE)
7602 this->skip_next_tls_get_addr_call();
7603 }
7604
dd93cd0a
AM
7605 Powerpc_relobj<size, big_endian>* ppc_object
7606 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7607
bfdfa4cd
AM
7608 if (is_discarded)
7609 {
7610 if (size == 64
7611 && data_shndx == ppc_object->opd_shndx()
7612 && r_type == elfcpp::R_PPC64_ADDR64)
7613 ppc_object->set_opd_discard(reloc.get_r_offset());
7614 return;
7615 }
7616
e5d5f5ed
AM
7617 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7618 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 7619 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 7620 {
ec661b9d
AM
7621 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7622 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7623 r_type, r_sym, reloc.get_r_addend());
7624 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 7625 }
e5d5f5ed 7626
42cacb20
DE
7627 switch (r_type)
7628 {
7629 case elfcpp::R_POWERPC_NONE:
7630 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7631 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 7632 case elfcpp::R_POWERPC_TLS:
549dba71 7633 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
7634 case elfcpp::R_POWERPC_PLTSEQ:
7635 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
7636 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7637 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
7638 case elfcpp::R_PPC64_PCREL_OPT:
7639 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7640 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7641 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7642 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7643 case elfcpp::R_PPC64_REL16_HIGHER34:
7644 case elfcpp::R_PPC64_REL16_HIGHERA34:
7645 case elfcpp::R_PPC64_REL16_HIGHEST34:
7646 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
7647 case elfcpp::R_PPC64_D34:
7648 case elfcpp::R_PPC64_D34_LO:
7649 case elfcpp::R_PPC64_D34_HI30:
7650 case elfcpp::R_PPC64_D34_HA30:
7651 case elfcpp::R_PPC64_D28:
7652 case elfcpp::R_PPC64_PCREL34:
7653 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
7654 case elfcpp::R_PPC64_TPREL34:
7655 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
7656 break;
7657
7658 case elfcpp::R_PPC64_TOC:
7659 {
7660 Output_data_got_powerpc<size, big_endian>* got
7661 = target->got_section(symtab, layout);
7662 if (parameters->options().output_is_position_independent())
7663 {
bfdfa4cd
AM
7664 Address off = reloc.get_r_offset();
7665 if (size == 64
9055360d 7666 && target->abiversion() < 2
bfdfa4cd
AM
7667 && data_shndx == ppc_object->opd_shndx()
7668 && ppc_object->get_opd_discard(off - 8))
7669 break;
7670
dd93cd0a 7671 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 7672 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
7673 rela_dyn->add_output_section_relative(got->output_section(),
7674 elfcpp::R_POWERPC_RELATIVE,
7675 output_section,
bfdfa4cd
AM
7676 object, data_shndx, off,
7677 symobj->toc_base_offset());
dd93cd0a
AM
7678 }
7679 }
42cacb20
DE
7680 break;
7681
7682 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 7683 case elfcpp::R_PPC64_UADDR64:
42cacb20 7684 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
7685 case elfcpp::R_POWERPC_UADDR32:
7686 case elfcpp::R_POWERPC_ADDR24:
c9269dff 7687 case elfcpp::R_POWERPC_ADDR16:
42cacb20 7688 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
7689 case elfcpp::R_POWERPC_ADDR16_HI:
7690 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 7691 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
7692 case elfcpp::R_PPC64_ADDR16_HIGH:
7693 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
7694 case elfcpp::R_PPC64_ADDR16_HIGHER:
7695 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7696 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7697 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7698 case elfcpp::R_PPC64_ADDR16_DS:
7699 case elfcpp::R_PPC64_ADDR16_LO_DS:
7700 case elfcpp::R_POWERPC_ADDR14:
7701 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7702 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
7703 // If building a shared library (or a position-independent
7704 // executable), we need to create a dynamic relocation for
7705 // this location.
c9824451 7706 if (parameters->options().output_is_position_independent()
9055360d 7707 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 7708 {
b3ccdeb5
AM
7709 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7710 is_ifunc);
1f98a074 7711 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
7712 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7713 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 7714 {
b3ccdeb5
AM
7715 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7716 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7717 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
7718 output_section, data_shndx,
7719 reloc.get_r_offset(),
c9824451 7720 reloc.get_r_addend(), false);
2e702c99 7721 }
1f98a074 7722 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 7723 {
dd93cd0a 7724 check_non_pic(object, r_type);
dd93cd0a
AM
7725 rela_dyn->add_local(object, r_sym, r_type, output_section,
7726 data_shndx, reloc.get_r_offset(),
7727 reloc.get_r_addend());
2e702c99 7728 }
1f98a074
AM
7729 else
7730 {
7731 gold_assert(lsym.get_st_value() == 0);
7732 unsigned int shndx = lsym.get_st_shndx();
7733 bool is_ordinary;
7734 shndx = object->adjust_sym_shndx(r_sym, shndx,
7735 &is_ordinary);
7736 if (!is_ordinary)
7737 object->error(_("section symbol %u has bad shndx %u"),
7738 r_sym, shndx);
7739 else
7740 rela_dyn->add_local_section(object, shndx, r_type,
7741 output_section, data_shndx,
7742 reloc.get_r_offset());
7743 }
2e702c99 7744 }
42cacb20
DE
7745 break;
7746
e4dff765
AM
7747 case elfcpp::R_PPC64_PLT_PCREL34:
7748 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
2d7ad24e
AM
7749 case elfcpp::R_POWERPC_PLT16_LO:
7750 case elfcpp::R_POWERPC_PLT16_HI:
7751 case elfcpp::R_POWERPC_PLT16_HA:
7752 case elfcpp::R_PPC64_PLT16_LO_DS:
7753 if (!is_ifunc)
7754 {
7755 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7756 target->make_local_plt_entry(layout, object, r_sym);
7757 }
7758 break;
7759
32f59844
AM
7760 case elfcpp::R_PPC64_REL24_NOTOC:
7761 if (size == 32)
7762 break;
7763 // Fall through.
42cacb20 7764 case elfcpp::R_POWERPC_REL24:
c9824451 7765 case elfcpp::R_PPC_PLTREL24:
42cacb20 7766 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
7767 case elfcpp::R_POWERPC_REL14:
7768 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7769 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 7770 if (!is_ifunc)
0e123f69
AM
7771 {
7772 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7773 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7774 r_type, r_sym, reloc.get_r_addend());
7775 }
ec661b9d
AM
7776 break;
7777
7e57d19e
AM
7778 case elfcpp::R_PPC64_TOCSAVE:
7779 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7780 // caller has already saved r2 and thus a plt call stub need not
7781 // save r2.
7782 if (size == 64
7783 && target->mark_pltcall(ppc_object, data_shndx,
7784 reloc.get_r_offset() - 4, symtab))
7785 {
7786 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7787 unsigned int shndx = lsym.get_st_shndx();
7788 bool is_ordinary;
7789 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7790 if (!is_ordinary)
7791 object->error(_("tocsave symbol %u has bad shndx %u"),
7792 r_sym, shndx);
7793 else
7794 target->add_tocsave(ppc_object, shndx,
7795 lsym.get_st_value() + reloc.get_r_addend());
7796 }
7797 break;
7798
ec661b9d
AM
7799 case elfcpp::R_PPC64_REL64:
7800 case elfcpp::R_POWERPC_REL32:
dd93cd0a 7801 case elfcpp::R_POWERPC_REL16:
6ce78956 7802 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 7803 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 7804 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 7805 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
7806 case elfcpp::R_PPC64_REL16_HIGH:
7807 case elfcpp::R_PPC64_REL16_HIGHA:
7808 case elfcpp::R_PPC64_REL16_HIGHER:
7809 case elfcpp::R_PPC64_REL16_HIGHERA:
7810 case elfcpp::R_PPC64_REL16_HIGHEST:
7811 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 7812 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 7813 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 7814 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 7815 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
7816 case elfcpp::R_PPC64_SECTOFF_DS:
7817 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7818 case elfcpp::R_POWERPC_TPREL16:
7819 case elfcpp::R_POWERPC_TPREL16_LO:
7820 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 7821 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
7822 case elfcpp::R_PPC64_TPREL16_DS:
7823 case elfcpp::R_PPC64_TPREL16_LO_DS:
7824 case elfcpp::R_PPC64_TPREL16_HIGH:
7825 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 7826 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 7827 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 7828 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 7829 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
7830 case elfcpp::R_POWERPC_DTPREL16:
7831 case elfcpp::R_POWERPC_DTPREL16_LO:
7832 case elfcpp::R_POWERPC_DTPREL16_HI:
7833 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
7834 case elfcpp::R_PPC64_DTPREL16_DS:
7835 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
7836 case elfcpp::R_PPC64_DTPREL16_HIGH:
7837 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7838 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7839 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7840 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7841 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
7842 case elfcpp::R_PPC64_TLSGD:
7843 case elfcpp::R_PPC64_TLSLD:
45965137 7844 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
7845 break;
7846
e4dff765 7847 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
7848 case elfcpp::R_POWERPC_GOT16:
7849 case elfcpp::R_POWERPC_GOT16_LO:
7850 case elfcpp::R_POWERPC_GOT16_HI:
7851 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
7852 case elfcpp::R_PPC64_GOT16_DS:
7853 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 7854 {
c9269dff 7855 // The symbol requires a GOT entry.
dd93cd0a
AM
7856 Output_data_got_powerpc<size, big_endian>* got
7857 = target->got_section(symtab, layout);
7858 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 7859
e5d5f5ed 7860 if (!parameters->options().output_is_position_independent())
42cacb20 7861 {
b01a4b04
AM
7862 if (is_ifunc
7863 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
7864 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
7865 else
7866 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
7867 }
7868 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
7869 {
7870 // If we are generating a shared object or a pie, this
7871 // symbol's GOT entry will be set by a dynamic relocation.
7872 unsigned int off;
7873 off = got->add_constant(0);
7874 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 7875
b3ccdeb5
AM
7876 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7877 is_ifunc);
7878 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7879 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 7880 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 7881 got, off, 0, false);
2e702c99 7882 }
42cacb20
DE
7883 }
7884 break;
7885
cf43a2fe
AM
7886 case elfcpp::R_PPC64_TOC16:
7887 case elfcpp::R_PPC64_TOC16_LO:
7888 case elfcpp::R_PPC64_TOC16_HI:
7889 case elfcpp::R_PPC64_TOC16_HA:
7890 case elfcpp::R_PPC64_TOC16_DS:
7891 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
7892 // We need a GOT section.
7893 target->got_section(symtab, layout);
7894 break;
7895
89c52ae3 7896 case elfcpp::R_PPC64_GOT_TLSGD34:
dd93cd0a
AM
7897 case elfcpp::R_POWERPC_GOT_TLSGD16:
7898 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7899 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7900 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7901 {
7902 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7903 if (tls_type == tls::TLSOPT_NONE)
7904 {
7905 Output_data_got_powerpc<size, big_endian>* got
7906 = target->got_section(symtab, layout);
7907 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
7908 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7909 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
7910 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
7911 }
7912 else if (tls_type == tls::TLSOPT_TO_LE)
7913 {
7914 // no GOT relocs needed for Local Exec.
7915 }
7916 else
7917 gold_unreachable();
7918 }
42cacb20
DE
7919 break;
7920
89c52ae3 7921 case elfcpp::R_PPC64_GOT_TLSLD34:
dd93cd0a
AM
7922 case elfcpp::R_POWERPC_GOT_TLSLD16:
7923 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7924 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7925 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7926 {
7927 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7928 if (tls_type == tls::TLSOPT_NONE)
7929 target->tlsld_got_offset(symtab, layout, object);
7930 else if (tls_type == tls::TLSOPT_TO_LE)
7931 {
7932 // no GOT relocs needed for Local Exec.
7404fe1b
AM
7933 if (parameters->options().emit_relocs())
7934 {
7935 Output_section* os = layout->tls_segment()->first_section();
7936 gold_assert(os != NULL);
7937 os->set_needs_symtab_index();
7938 }
dd93cd0a
AM
7939 }
7940 else
7941 gold_unreachable();
7942 }
42cacb20 7943 break;
42cacb20 7944
89c52ae3 7945 case elfcpp::R_PPC64_GOT_DTPREL34:
dd93cd0a
AM
7946 case elfcpp::R_POWERPC_GOT_DTPREL16:
7947 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7948 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7949 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7950 {
7951 Output_data_got_powerpc<size, big_endian>* got
7952 = target->got_section(symtab, layout);
7953 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 7954 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
7955 }
7956 break;
42cacb20 7957
89c52ae3 7958 case elfcpp::R_PPC64_GOT_TPREL34:
dd93cd0a
AM
7959 case elfcpp::R_POWERPC_GOT_TPREL16:
7960 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7961 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7962 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7963 {
7964 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7965 if (tls_type == tls::TLSOPT_NONE)
7966 {
dd93cd0a 7967 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
7968 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7969 {
7970 Output_data_got_powerpc<size, big_endian>* got
7971 = target->got_section(symtab, layout);
7972 unsigned int off = got->add_constant(0);
7973 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7974
7975 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7976 rela_dyn->add_symbolless_local_addend(object, r_sym,
7977 elfcpp::R_POWERPC_TPREL,
7978 got, off, 0);
7979 }
dd93cd0a
AM
7980 }
7981 else if (tls_type == tls::TLSOPT_TO_LE)
7982 {
7983 // no GOT relocs needed for Local Exec.
7984 }
7985 else
7986 gold_unreachable();
7987 }
7988 break;
7989
7990 default:
7991 unsupported_reloc_local(object, r_type);
7992 break;
7993 }
d8f5a274 7994
5edad15d
AM
7995 if (size == 64
7996 && parameters->options().toc_optimize())
7997 {
7998 if (data_shndx == ppc_object->toc_shndx())
7999 {
8000 bool ok = true;
8001 if (r_type != elfcpp::R_PPC64_ADDR64
8002 || (is_ifunc && target->abiversion() < 2))
8003 ok = false;
8004 else if (parameters->options().output_is_position_independent())
8005 {
8006 if (is_ifunc)
8007 ok = false;
8008 else
8009 {
8010 unsigned int shndx = lsym.get_st_shndx();
8011 if (shndx >= elfcpp::SHN_LORESERVE
8012 && shndx != elfcpp::SHN_XINDEX)
8013 ok = false;
8014 }
8015 }
8016 if (!ok)
8017 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8018 }
8019
8020 enum {no_check, check_lo, check_ha} insn_check;
8021 switch (r_type)
8022 {
8023 default:
8024 insn_check = no_check;
8025 break;
8026
8027 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8028 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8029 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8030 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8031 case elfcpp::R_POWERPC_GOT16_HA:
8032 case elfcpp::R_PPC64_TOC16_HA:
8033 insn_check = check_ha;
8034 break;
8035
8036 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8037 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8038 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8039 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8040 case elfcpp::R_POWERPC_GOT16_LO:
8041 case elfcpp::R_PPC64_GOT16_LO_DS:
8042 case elfcpp::R_PPC64_TOC16_LO:
8043 case elfcpp::R_PPC64_TOC16_LO_DS:
8044 insn_check = check_lo;
8045 break;
8046 }
8047
8048 section_size_type slen;
8049 const unsigned char* view = NULL;
8050 if (insn_check != no_check)
8051 {
8052 view = ppc_object->section_contents(data_shndx, &slen, false);
8053 section_size_type off =
8054 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8055 if (off < slen)
8056 {
8057 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8058 if (insn_check == check_lo
8059 ? !ok_lo_toc_insn(insn, r_type)
8060 : ((insn & ((0x3f << 26) | 0x1f << 16))
8061 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8062 {
8063 ppc_object->set_no_toc_opt();
8064 gold_warning(_("%s: toc optimization is not supported "
8065 "for %#08x instruction"),
8066 ppc_object->name().c_str(), insn);
8067 }
8068 }
8069 }
8070
8071 switch (r_type)
8072 {
8073 default:
8074 break;
8075 case elfcpp::R_PPC64_TOC16:
8076 case elfcpp::R_PPC64_TOC16_LO:
8077 case elfcpp::R_PPC64_TOC16_HI:
8078 case elfcpp::R_PPC64_TOC16_HA:
8079 case elfcpp::R_PPC64_TOC16_DS:
8080 case elfcpp::R_PPC64_TOC16_LO_DS:
8081 unsigned int shndx = lsym.get_st_shndx();
8082 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8083 bool is_ordinary;
8084 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8085 if (is_ordinary && shndx == ppc_object->toc_shndx())
8086 {
412294da 8087 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
5edad15d
AM
8088 if (dst_off < ppc_object->section_size(shndx))
8089 {
8090 bool ok = false;
8091 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8092 ok = true;
8093 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8094 {
8095 // Need to check that the insn is a ld
8096 if (!view)
8097 view = ppc_object->section_contents(data_shndx,
8098 &slen,
8099 false);
8100 section_size_type off =
8101 (convert_to_section_size_type(reloc.get_r_offset())
8102 + (big_endian ? -2 : 3));
8103 if (off < slen
8104 && (view[off] & (0x3f << 2)) == 58u << 2)
8105 ok = true;
8106 }
8107 if (!ok)
8108 ppc_object->set_no_toc_opt(dst_off);
8109 }
8110 }
8111 break;
8112 }
8113 }
8114
f159cdb6
AM
8115 if (size == 32)
8116 {
8117 switch (r_type)
8118 {
8119 case elfcpp::R_POWERPC_REL32:
8120 if (ppc_object->got2_shndx() != 0
8121 && parameters->options().output_is_position_independent())
8122 {
8123 unsigned int shndx = lsym.get_st_shndx();
8124 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8125 bool is_ordinary;
8126 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8127 if (is_ordinary && shndx == ppc_object->got2_shndx()
8128 && (ppc_object->section_flags(data_shndx)
8129 & elfcpp::SHF_EXECINSTR) != 0)
8130 gold_error(_("%s: unsupported -mbss-plt code"),
8131 ppc_object->name().c_str());
8132 }
8133 break;
8134 default:
8135 break;
8136 }
8137 }
8138
d8f5a274
AM
8139 switch (r_type)
8140 {
8141 case elfcpp::R_POWERPC_GOT_TLSLD16:
8142 case elfcpp::R_POWERPC_GOT_TLSGD16:
8143 case elfcpp::R_POWERPC_GOT_TPREL16:
8144 case elfcpp::R_POWERPC_GOT_DTPREL16:
8145 case elfcpp::R_POWERPC_GOT16:
8146 case elfcpp::R_PPC64_GOT16_DS:
8147 case elfcpp::R_PPC64_TOC16:
8148 case elfcpp::R_PPC64_TOC16_DS:
8149 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8150 break;
8151 default:
8152 break;
8153 }
8154
8155 switch (r_type)
8156 {
89c52ae3
AM
8157 case elfcpp::R_PPC64_TPREL16_DS:
8158 case elfcpp::R_PPC64_TPREL16_LO_DS:
8159 case elfcpp::R_PPC64_TPREL16_HIGH:
8160 case elfcpp::R_PPC64_TPREL16_HIGHA:
8161 case elfcpp::R_PPC64_TPREL16_HIGHER:
8162 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8163 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8164 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8165 case elfcpp::R_PPC64_TPREL34:
93b9bf16
AM
8166 if (size != 64)
8167 break;
8168 // Fall through.
8169 case elfcpp::R_POWERPC_TPREL16:
8170 case elfcpp::R_POWERPC_TPREL16_LO:
8171 case elfcpp::R_POWERPC_TPREL16_HI:
8172 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8173 layout->set_has_static_tls();
8174 break;
8175 default:
8176 break;
8177 }
8178
93b9bf16
AM
8179 switch (r_type)
8180 {
8181 case elfcpp::R_POWERPC_TPREL16_HA:
8182 if (target->tprel_opt())
8183 {
8184 section_size_type slen;
8185 const unsigned char* view = NULL;
8186 view = ppc_object->section_contents(data_shndx, &slen, false);
8187 section_size_type off
8188 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8189 if (off < slen)
8190 {
8191 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8192 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8193 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8194 target->set_tprel_opt(false);
8195 }
8196 }
8197 break;
8198
8199 case elfcpp::R_PPC64_TPREL16_HIGH:
8200 case elfcpp::R_PPC64_TPREL16_HIGHA:
8201 case elfcpp::R_PPC64_TPREL16_HIGHER:
8202 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8203 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8204 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8205 if (size != 64)
8206 break;
8207 // Fall through.
8208 case elfcpp::R_POWERPC_TPREL16_HI:
8209 target->set_tprel_opt(false);
8210 break;
8211 default:
8212 break;
8213 }
8214
89c52ae3
AM
8215 switch (r_type)
8216 {
8217 case elfcpp::R_PPC64_D34:
8218 case elfcpp::R_PPC64_D34_LO:
8219 case elfcpp::R_PPC64_D34_HI30:
8220 case elfcpp::R_PPC64_D34_HA30:
8221 case elfcpp::R_PPC64_D28:
8222 case elfcpp::R_PPC64_PCREL34:
8223 case elfcpp::R_PPC64_PCREL28:
8224 case elfcpp::R_PPC64_TPREL34:
8225 case elfcpp::R_PPC64_DTPREL34:
8226 case elfcpp::R_PPC64_PLT_PCREL34:
8227 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8228 case elfcpp::R_PPC64_GOT_PCREL34:
8229 case elfcpp::R_PPC64_GOT_TLSGD34:
8230 case elfcpp::R_PPC64_GOT_TLSLD34:
8231 case elfcpp::R_PPC64_GOT_DTPREL34:
8232 case elfcpp::R_PPC64_GOT_TPREL34:
8233 target->set_powerxx_stubs();
8234 break;
d8f5a274
AM
8235 default:
8236 break;
8237 }
dd93cd0a
AM
8238}
8239
8240// Report an unsupported relocation against a global symbol.
8241
8242template<int size, bool big_endian>
8243void
8244Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8245 Sized_relobj_file<size, big_endian>* object,
8246 unsigned int r_type,
8247 Symbol* gsym)
8248{
42cacb20
DE
8249 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8250 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8251}
8252
8253// Scan a relocation for a global symbol.
8254
8255template<int size, bool big_endian>
8256inline void
8257Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
8258 Symbol_table* symtab,
8259 Layout* layout,
8260 Target_powerpc<size, big_endian>* target,
8261 Sized_relobj_file<size, big_endian>* object,
8262 unsigned int data_shndx,
8263 Output_section* output_section,
8264 const elfcpp::Rela<size, big_endian>& reloc,
8265 unsigned int r_type,
8266 Symbol* gsym)
42cacb20 8267{
34e0882b
AM
8268 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8269 == Track_tls::SKIP)
e3deeb9c
AM
8270 return;
8271
34e0882b
AM
8272 if (target->replace_tls_get_addr(gsym))
8273 // Change a __tls_get_addr reference to __tls_get_addr_opt
8274 // so dynamic relocs are emitted against the latter symbol.
8275 gsym = target->tls_get_addr_opt();
8276
e3deeb9c
AM
8277 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8278 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8279 {
8280 this->expect_tls_get_addr_call();
8281 const bool final = gsym->final_value_is_known();
8282 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8283 if (tls_type != tls::TLSOPT_NONE)
8284 this->skip_next_tls_get_addr_call();
8285 }
8286 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8287 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8288 {
8289 this->expect_tls_get_addr_call();
8290 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8291 if (tls_type != tls::TLSOPT_NONE)
8292 this->skip_next_tls_get_addr_call();
8293 }
8294
dd93cd0a
AM
8295 Powerpc_relobj<size, big_endian>* ppc_object
8296 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8297
e5d5f5ed 8298 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 8299 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
8300 bool pushed_ifunc = false;
8301 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 8302 {
0e123f69 8303 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 8304 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8305 r_type, r_sym, reloc.get_r_addend());
ec661b9d 8306 target->make_plt_entry(symtab, layout, gsym);
9055360d 8307 pushed_ifunc = true;
ec661b9d 8308 }
e5d5f5ed 8309
42cacb20
DE
8310 switch (r_type)
8311 {
8312 case elfcpp::R_POWERPC_NONE:
8313 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8314 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 8315 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 8316 case elfcpp::R_POWERPC_TLS:
549dba71 8317 case elfcpp::R_PPC64_ENTRY:
23cedd1d
AM
8318 case elfcpp::R_POWERPC_PLTSEQ:
8319 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
8320 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8321 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765
AM
8322 case elfcpp::R_PPC64_PCREL_OPT:
8323 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8324 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8325 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8326 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8327 case elfcpp::R_PPC64_REL16_HIGHER34:
8328 case elfcpp::R_PPC64_REL16_HIGHERA34:
8329 case elfcpp::R_PPC64_REL16_HIGHEST34:
8330 case elfcpp::R_PPC64_REL16_HIGHESTA34:
e4dff765
AM
8331 case elfcpp::R_PPC64_D34:
8332 case elfcpp::R_PPC64_D34_LO:
8333 case elfcpp::R_PPC64_D34_HI30:
8334 case elfcpp::R_PPC64_D34_HA30:
8335 case elfcpp::R_PPC64_D28:
8336 case elfcpp::R_PPC64_PCREL34:
8337 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
8338 case elfcpp::R_PPC64_TPREL34:
8339 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
8340 break;
8341
8342 case elfcpp::R_PPC64_TOC:
8343 {
8344 Output_data_got_powerpc<size, big_endian>* got
8345 = target->got_section(symtab, layout);
8346 if (parameters->options().output_is_position_independent())
8347 {
bfdfa4cd
AM
8348 Address off = reloc.get_r_offset();
8349 if (size == 64
8350 && data_shndx == ppc_object->opd_shndx()
8351 && ppc_object->get_opd_discard(off - 8))
8352 break;
8353
dd93cd0a
AM
8354 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8355 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8356 if (data_shndx != ppc_object->opd_shndx())
8357 symobj = static_cast
8358 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8359 rela_dyn->add_output_section_relative(got->output_section(),
8360 elfcpp::R_POWERPC_RELATIVE,
8361 output_section,
bfdfa4cd 8362 object, data_shndx, off,
dd93cd0a
AM
8363 symobj->toc_base_offset());
8364 }
8365 }
42cacb20
DE
8366 break;
8367
c9269dff 8368 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 8369 if (size == 64
9055360d 8370 && target->abiversion() < 2
bfdfa4cd
AM
8371 && data_shndx == ppc_object->opd_shndx()
8372 && (gsym->is_defined_in_discarded_section()
8373 || gsym->object() != object))
8374 {
8375 ppc_object->set_opd_discard(reloc.get_r_offset());
8376 break;
8377 }
d8e90251 8378 // Fall through.
dd93cd0a 8379 case elfcpp::R_PPC64_UADDR64:
c9269dff 8380 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
8381 case elfcpp::R_POWERPC_UADDR32:
8382 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
8383 case elfcpp::R_POWERPC_ADDR16:
8384 case elfcpp::R_POWERPC_ADDR16_LO:
8385 case elfcpp::R_POWERPC_ADDR16_HI:
8386 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8387 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
8388 case elfcpp::R_PPC64_ADDR16_HIGH:
8389 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
8390 case elfcpp::R_PPC64_ADDR16_HIGHER:
8391 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8392 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8393 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8394 case elfcpp::R_PPC64_ADDR16_DS:
8395 case elfcpp::R_PPC64_ADDR16_LO_DS:
8396 case elfcpp::R_POWERPC_ADDR14:
8397 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8398 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 8399 {
c9269dff
AM
8400 // Make a PLT entry if necessary.
8401 if (gsym->needs_plt_entry())
8402 {
9055360d
AM
8403 // Since this is not a PC-relative relocation, we may be
8404 // taking the address of a function. In that case we need to
8405 // set the entry in the dynamic symbol table to the address of
8406 // the PLT call stub.
8407 bool need_ifunc_plt = false;
8408 if ((size == 32 || target->abiversion() >= 2)
8409 && gsym->is_from_dynobj()
8410 && !parameters->options().output_is_position_independent())
8411 {
8412 gsym->set_needs_dynsym_value();
8413 need_ifunc_plt = true;
8414 }
8415 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 8416 {
0e123f69 8417 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8418 target->push_branch(ppc_object, data_shndx,
0e123f69 8419 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
8420 reloc.get_r_addend());
8421 target->make_plt_entry(symtab, layout, gsym);
8422 }
c9269dff
AM
8423 }
8424 // Make a dynamic relocation if necessary.
88b8e639 8425 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 8426 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 8427 {
a82bef93
ST
8428 if (!parameters->options().output_is_position_independent()
8429 && gsym->may_need_copy_reloc())
c9269dff
AM
8430 {
8431 target->copy_reloc(symtab, layout, object,
8432 data_shndx, output_section, gsym, reloc);
8433 }
9055360d
AM
8434 else if ((((size == 32
8435 && r_type == elfcpp::R_POWERPC_ADDR32)
8436 || (size == 64
8437 && r_type == elfcpp::R_PPC64_ADDR64
8438 && target->abiversion() >= 2))
627b30b7
AM
8439 && gsym->can_use_relative_reloc(false)
8440 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8441 && parameters->options().shared()))
8442 || (size == 64
8443 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 8444 && target->abiversion() < 2
627b30b7
AM
8445 && (gsym->can_use_relative_reloc(false)
8446 || data_shndx == ppc_object->opd_shndx())))
2e702c99 8447 {
b3ccdeb5
AM
8448 Reloc_section* rela_dyn
8449 = target->rela_dyn_section(symtab, layout, is_ifunc);
8450 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8451 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8452 rela_dyn->add_symbolless_global_addend(
8453 gsym, dynrel, output_section, object, data_shndx,
8454 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
8455 }
8456 else
8457 {
b3ccdeb5
AM
8458 Reloc_section* rela_dyn
8459 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 8460 check_non_pic(object, r_type);
dd93cd0a
AM
8461 rela_dyn->add_global(gsym, r_type, output_section,
8462 object, data_shndx,
8463 reloc.get_r_offset(),
8464 reloc.get_r_addend());
5edad15d
AM
8465
8466 if (size == 64
8467 && parameters->options().toc_optimize()
8468 && data_shndx == ppc_object->toc_shndx())
8469 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
8470 }
8471 }
42cacb20
DE
8472 }
8473 break;
8474
e4dff765
AM
8475 case elfcpp::R_PPC64_PLT_PCREL34:
8476 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
08be3224
AM
8477 case elfcpp::R_POWERPC_PLT16_LO:
8478 case elfcpp::R_POWERPC_PLT16_HI:
8479 case elfcpp::R_POWERPC_PLT16_HA:
8480 case elfcpp::R_PPC64_PLT16_LO_DS:
8481 if (!pushed_ifunc)
8482 target->make_plt_entry(symtab, layout, gsym);
8483 break;
8484
32f59844
AM
8485 case elfcpp::R_PPC64_REL24_NOTOC:
8486 if (size == 32)
8487 break;
8488 // Fall through.
cf43a2fe 8489 case elfcpp::R_PPC_PLTREL24:
42cacb20 8490 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
8491 if (!is_ifunc)
8492 {
0e123f69 8493 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 8494 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 8495 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
8496 if (gsym->needs_plt_entry()
8497 || (!gsym->final_value_is_known()
8498 && (gsym->is_undefined()
8499 || gsym->is_from_dynobj()
8500 || gsym->is_preemptible())))
8501 target->make_plt_entry(symtab, layout, gsym);
8502 }
d8e90251 8503 // Fall through.
42cacb20 8504
3ea0a085 8505 case elfcpp::R_PPC64_REL64:
dd93cd0a 8506 case elfcpp::R_POWERPC_REL32:
3ea0a085 8507 // Make a dynamic relocation if necessary.
88b8e639 8508 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 8509 {
a82bef93
ST
8510 if (!parameters->options().output_is_position_independent()
8511 && gsym->may_need_copy_reloc())
3ea0a085
AM
8512 {
8513 target->copy_reloc(symtab, layout, object,
8514 data_shndx, output_section, gsym,
8515 reloc);
8516 }
8517 else
8518 {
b3ccdeb5
AM
8519 Reloc_section* rela_dyn
8520 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
8521 check_non_pic(object, r_type);
8522 rela_dyn->add_global(gsym, r_type, output_section, object,
8523 data_shndx, reloc.get_r_offset(),
8524 reloc.get_r_addend());
8525 }
8526 }
8527 break;
8528
ec661b9d
AM
8529 case elfcpp::R_POWERPC_REL14:
8530 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8531 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 8532 if (!is_ifunc)
0e123f69
AM
8533 {
8534 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8535 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8536 r_type, r_sym, reloc.get_r_addend());
8537 }
ec661b9d
AM
8538 break;
8539
7e57d19e
AM
8540 case elfcpp::R_PPC64_TOCSAVE:
8541 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8542 // caller has already saved r2 and thus a plt call stub need not
8543 // save r2.
8544 if (size == 64
8545 && target->mark_pltcall(ppc_object, data_shndx,
8546 reloc.get_r_offset() - 4, symtab))
8547 {
8548 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8549 bool is_ordinary;
8550 unsigned int shndx = gsym->shndx(&is_ordinary);
8551 if (!is_ordinary)
8552 object->error(_("tocsave symbol %u has bad shndx %u"),
8553 r_sym, shndx);
8554 else
8555 {
8556 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8557 target->add_tocsave(ppc_object, shndx,
8558 sym->value() + reloc.get_r_addend());
8559 }
8560 }
8561 break;
8562
6ce78956
AM
8563 case elfcpp::R_POWERPC_REL16:
8564 case elfcpp::R_POWERPC_REL16_LO:
8565 case elfcpp::R_POWERPC_REL16_HI:
8566 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8567 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
8568 case elfcpp::R_PPC64_REL16_HIGH:
8569 case elfcpp::R_PPC64_REL16_HIGHA:
8570 case elfcpp::R_PPC64_REL16_HIGHER:
8571 case elfcpp::R_PPC64_REL16_HIGHERA:
8572 case elfcpp::R_PPC64_REL16_HIGHEST:
8573 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a 8574 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 8575 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 8576 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 8577 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
8578 case elfcpp::R_PPC64_SECTOFF_DS:
8579 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8580 case elfcpp::R_POWERPC_TPREL16:
8581 case elfcpp::R_POWERPC_TPREL16_LO:
8582 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 8583 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
8584 case elfcpp::R_PPC64_TPREL16_DS:
8585 case elfcpp::R_PPC64_TPREL16_LO_DS:
8586 case elfcpp::R_PPC64_TPREL16_HIGH:
8587 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8588 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 8589 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 8590 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 8591 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
8592 case elfcpp::R_POWERPC_DTPREL16:
8593 case elfcpp::R_POWERPC_DTPREL16_LO:
8594 case elfcpp::R_POWERPC_DTPREL16_HI:
8595 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
8596 case elfcpp::R_PPC64_DTPREL16_DS:
8597 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
8598 case elfcpp::R_PPC64_DTPREL16_HIGH:
8599 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8600 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8601 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8602 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8603 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
8604 case elfcpp::R_PPC64_TLSGD:
8605 case elfcpp::R_PPC64_TLSLD:
45965137 8606 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
8607 break;
8608
e4dff765 8609 case elfcpp::R_PPC64_GOT_PCREL34:
42cacb20
DE
8610 case elfcpp::R_POWERPC_GOT16:
8611 case elfcpp::R_POWERPC_GOT16_LO:
8612 case elfcpp::R_POWERPC_GOT16_HI:
8613 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
8614 case elfcpp::R_PPC64_GOT16_DS:
8615 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 8616 {
c9269dff
AM
8617 // The symbol requires a GOT entry.
8618 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
8619
8620 got = target->got_section(symtab, layout);
2e702c99 8621 if (gsym->final_value_is_known())
2e702c99 8622 {
b01a4b04
AM
8623 if (is_ifunc
8624 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
8625 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8626 else
8627 got->add_global(gsym, GOT_TYPE_STANDARD);
8628 }
8629 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8630 {
8631 // If we are generating a shared object or a pie, this
8632 // symbol's GOT entry will be set by a dynamic relocation.
8633 unsigned int off = got->add_constant(0);
8634 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8635
b3ccdeb5
AM
8636 Reloc_section* rela_dyn
8637 = target->rela_dyn_section(symtab, layout, is_ifunc);
8638
e5d5f5ed 8639 if (gsym->can_use_relative_reloc(false)
9055360d
AM
8640 && !((size == 32
8641 || target->abiversion() >= 2)
e5d5f5ed
AM
8642 && gsym->visibility() == elfcpp::STV_PROTECTED
8643 && parameters->options().shared()))
2e702c99 8644 {
b3ccdeb5
AM
8645 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8646 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
8647 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8648 }
8649 else
8650 {
8651 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8652 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 8653 }
2e702c99 8654 }
42cacb20
DE
8655 }
8656 break;
8657
cf43a2fe
AM
8658 case elfcpp::R_PPC64_TOC16:
8659 case elfcpp::R_PPC64_TOC16_LO:
8660 case elfcpp::R_PPC64_TOC16_HI:
8661 case elfcpp::R_PPC64_TOC16_HA:
8662 case elfcpp::R_PPC64_TOC16_DS:
8663 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
8664 // We need a GOT section.
8665 target->got_section(symtab, layout);
8666 break;
8667
89c52ae3 8668 case elfcpp::R_PPC64_GOT_TLSGD34:
dd93cd0a
AM
8669 case elfcpp::R_POWERPC_GOT_TLSGD16:
8670 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8671 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8672 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8673 {
8674 const bool final = gsym->final_value_is_known();
8675 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8676 if (tls_type == tls::TLSOPT_NONE)
8677 {
8678 Output_data_got_powerpc<size, big_endian>* got
8679 = target->got_section(symtab, layout);
b3ccdeb5
AM
8680 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8681 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
8682 elfcpp::R_POWERPC_DTPMOD,
8683 elfcpp::R_POWERPC_DTPREL);
8684 }
8685 else if (tls_type == tls::TLSOPT_TO_IE)
8686 {
acc276d8
AM
8687 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8688 {
8689 Output_data_got_powerpc<size, big_endian>* got
8690 = target->got_section(symtab, layout);
8691 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8692 if (gsym->is_undefined()
8693 || gsym->is_from_dynobj())
8694 {
8695 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8696 elfcpp::R_POWERPC_TPREL);
8697 }
8698 else
8699 {
8700 unsigned int off = got->add_constant(0);
8701 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8702 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8703 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8704 got, off, 0);
8705 }
8706 }
dd93cd0a
AM
8707 }
8708 else if (tls_type == tls::TLSOPT_TO_LE)
8709 {
8710 // no GOT relocs needed for Local Exec.
8711 }
8712 else
8713 gold_unreachable();
8714 }
42cacb20
DE
8715 break;
8716
89c52ae3 8717 case elfcpp::R_PPC64_GOT_TLSLD34:
dd93cd0a
AM
8718 case elfcpp::R_POWERPC_GOT_TLSLD16:
8719 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8720 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8721 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8722 {
8723 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8724 if (tls_type == tls::TLSOPT_NONE)
8725 target->tlsld_got_offset(symtab, layout, object);
8726 else if (tls_type == tls::TLSOPT_TO_LE)
8727 {
8728 // no GOT relocs needed for Local Exec.
7404fe1b
AM
8729 if (parameters->options().emit_relocs())
8730 {
8731 Output_section* os = layout->tls_segment()->first_section();
8732 gold_assert(os != NULL);
8733 os->set_needs_symtab_index();
8734 }
dd93cd0a
AM
8735 }
8736 else
8737 gold_unreachable();
8738 }
8739 break;
8740
89c52ae3 8741 case elfcpp::R_PPC64_GOT_DTPREL34:
dd93cd0a
AM
8742 case elfcpp::R_POWERPC_GOT_DTPREL16:
8743 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8744 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8745 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8746 {
8747 Output_data_got_powerpc<size, big_endian>* got
8748 = target->got_section(symtab, layout);
bd73a62d
AM
8749 if (!gsym->final_value_is_known()
8750 && (gsym->is_from_dynobj()
8751 || gsym->is_undefined()
8752 || gsym->is_preemptible()))
8753 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
8754 target->rela_dyn_section(layout),
8755 elfcpp::R_POWERPC_DTPREL);
8756 else
8757 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
8758 }
8759 break;
8760
89c52ae3 8761 case elfcpp::R_PPC64_GOT_TPREL34:
dd93cd0a
AM
8762 case elfcpp::R_POWERPC_GOT_TPREL16:
8763 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8764 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8765 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8766 {
8767 const bool final = gsym->final_value_is_known();
8768 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8769 if (tls_type == tls::TLSOPT_NONE)
8770 {
acc276d8
AM
8771 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8772 {
8773 Output_data_got_powerpc<size, big_endian>* got
8774 = target->got_section(symtab, layout);
8775 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8776 if (gsym->is_undefined()
8777 || gsym->is_from_dynobj())
8778 {
8779 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8780 elfcpp::R_POWERPC_TPREL);
8781 }
8782 else
8783 {
8784 unsigned int off = got->add_constant(0);
8785 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8786 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8787 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8788 got, off, 0);
8789 }
8790 }
dd93cd0a
AM
8791 }
8792 else if (tls_type == tls::TLSOPT_TO_LE)
8793 {
8794 // no GOT relocs needed for Local Exec.
8795 }
8796 else
8797 gold_unreachable();
8798 }
42cacb20
DE
8799 break;
8800
8801 default:
8802 unsupported_reloc_global(object, r_type, gsym);
8803 break;
8804 }
d8f5a274 8805
5edad15d
AM
8806 if (size == 64
8807 && parameters->options().toc_optimize())
8808 {
8809 if (data_shndx == ppc_object->toc_shndx())
8810 {
8811 bool ok = true;
8812 if (r_type != elfcpp::R_PPC64_ADDR64
8813 || (is_ifunc && target->abiversion() < 2))
8814 ok = false;
8815 else if (parameters->options().output_is_position_independent()
8816 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
8817 ok = false;
8818 if (!ok)
8819 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8820 }
8821
8822 enum {no_check, check_lo, check_ha} insn_check;
8823 switch (r_type)
8824 {
8825 default:
8826 insn_check = no_check;
8827 break;
8828
8829 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8830 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8831 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8832 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8833 case elfcpp::R_POWERPC_GOT16_HA:
8834 case elfcpp::R_PPC64_TOC16_HA:
8835 insn_check = check_ha;
8836 break;
8837
8838 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8839 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8840 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8841 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8842 case elfcpp::R_POWERPC_GOT16_LO:
8843 case elfcpp::R_PPC64_GOT16_LO_DS:
8844 case elfcpp::R_PPC64_TOC16_LO:
8845 case elfcpp::R_PPC64_TOC16_LO_DS:
8846 insn_check = check_lo;
8847 break;
8848 }
8849
8850 section_size_type slen;
8851 const unsigned char* view = NULL;
8852 if (insn_check != no_check)
8853 {
8854 view = ppc_object->section_contents(data_shndx, &slen, false);
8855 section_size_type off =
8856 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8857 if (off < slen)
8858 {
8859 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8860 if (insn_check == check_lo
8861 ? !ok_lo_toc_insn(insn, r_type)
8862 : ((insn & ((0x3f << 26) | 0x1f << 16))
8863 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8864 {
8865 ppc_object->set_no_toc_opt();
8866 gold_warning(_("%s: toc optimization is not supported "
8867 "for %#08x instruction"),
8868 ppc_object->name().c_str(), insn);
8869 }
8870 }
8871 }
8872
8873 switch (r_type)
8874 {
8875 default:
8876 break;
8877 case elfcpp::R_PPC64_TOC16:
8878 case elfcpp::R_PPC64_TOC16_LO:
8879 case elfcpp::R_PPC64_TOC16_HI:
8880 case elfcpp::R_PPC64_TOC16_HA:
8881 case elfcpp::R_PPC64_TOC16_DS:
8882 case elfcpp::R_PPC64_TOC16_LO_DS:
8883 if (gsym->source() == Symbol::FROM_OBJECT
8884 && !gsym->object()->is_dynamic())
8885 {
8886 Powerpc_relobj<size, big_endian>* sym_object
8887 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8888 bool is_ordinary;
8889 unsigned int shndx = gsym->shndx(&is_ordinary);
8890 if (shndx == sym_object->toc_shndx())
8891 {
8892 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
412294da 8893 Address dst_off = sym->value() + reloc.get_r_addend();
5edad15d
AM
8894 if (dst_off < sym_object->section_size(shndx))
8895 {
8896 bool ok = false;
8897 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8898 ok = true;
8899 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8900 {
8901 // Need to check that the insn is a ld
8902 if (!view)
8903 view = ppc_object->section_contents(data_shndx,
8904 &slen,
8905 false);
8906 section_size_type off =
8907 (convert_to_section_size_type(reloc.get_r_offset())
8908 + (big_endian ? -2 : 3));
8909 if (off < slen
8910 && (view[off] & (0x3f << 2)) == (58u << 2))
8911 ok = true;
8912 }
8913 if (!ok)
8914 sym_object->set_no_toc_opt(dst_off);
8915 }
8916 }
8917 }
8918 break;
8919 }
8920 }
8921
f159cdb6
AM
8922 if (size == 32)
8923 {
8924 switch (r_type)
8925 {
8926 case elfcpp::R_PPC_LOCAL24PC:
8927 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8928 gold_error(_("%s: unsupported -mbss-plt code"),
8929 ppc_object->name().c_str());
8930 break;
8931 default:
8932 break;
8933 }
8934 }
8935
d8f5a274
AM
8936 switch (r_type)
8937 {
8938 case elfcpp::R_POWERPC_GOT_TLSLD16:
8939 case elfcpp::R_POWERPC_GOT_TLSGD16:
8940 case elfcpp::R_POWERPC_GOT_TPREL16:
8941 case elfcpp::R_POWERPC_GOT_DTPREL16:
8942 case elfcpp::R_POWERPC_GOT16:
8943 case elfcpp::R_PPC64_GOT16_DS:
8944 case elfcpp::R_PPC64_TOC16:
8945 case elfcpp::R_PPC64_TOC16_DS:
8946 ppc_object->set_has_small_toc_reloc();
89c52ae3
AM
8947 break;
8948 default:
8949 break;
8950 }
8951
8952 switch (r_type)
8953 {
89c52ae3
AM
8954 case elfcpp::R_PPC64_TPREL16_DS:
8955 case elfcpp::R_PPC64_TPREL16_LO_DS:
8956 case elfcpp::R_PPC64_TPREL16_HIGH:
8957 case elfcpp::R_PPC64_TPREL16_HIGHA:
8958 case elfcpp::R_PPC64_TPREL16_HIGHER:
8959 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8960 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8961 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8962 case elfcpp::R_PPC64_TPREL34:
93b9bf16
AM
8963 if (size != 64)
8964 break;
8965 // Fall through.
8966 case elfcpp::R_POWERPC_TPREL16:
8967 case elfcpp::R_POWERPC_TPREL16_LO:
8968 case elfcpp::R_POWERPC_TPREL16_HI:
8969 case elfcpp::R_POWERPC_TPREL16_HA:
89c52ae3
AM
8970 layout->set_has_static_tls();
8971 break;
8972 default:
8973 break;
8974 }
8975
93b9bf16
AM
8976 switch (r_type)
8977 {
8978 case elfcpp::R_POWERPC_TPREL16_HA:
8979 if (target->tprel_opt())
8980 {
8981 section_size_type slen;
8982 const unsigned char* view = NULL;
8983 view = ppc_object->section_contents(data_shndx, &slen, false);
8984 section_size_type off
8985 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8986 if (off < slen)
8987 {
8988 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8989 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8990 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8991 target->set_tprel_opt(false);
8992 }
8993 }
8994 break;
8995
8996 case elfcpp::R_PPC64_TPREL16_HIGH:
8997 case elfcpp::R_PPC64_TPREL16_HIGHA:
8998 case elfcpp::R_PPC64_TPREL16_HIGHER:
8999 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9000 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9001 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9002 if (size != 64)
9003 break;
9004 // Fall through.
9005 case elfcpp::R_POWERPC_TPREL16_HI:
9006 target->set_tprel_opt(false);
9007 break;
9008 default:
9009 break;
9010 }
9011
89c52ae3
AM
9012 switch (r_type)
9013 {
9014 case elfcpp::R_PPC64_D34:
9015 case elfcpp::R_PPC64_D34_LO:
9016 case elfcpp::R_PPC64_D34_HI30:
9017 case elfcpp::R_PPC64_D34_HA30:
9018 case elfcpp::R_PPC64_D28:
9019 case elfcpp::R_PPC64_PCREL34:
9020 case elfcpp::R_PPC64_PCREL28:
9021 case elfcpp::R_PPC64_TPREL34:
9022 case elfcpp::R_PPC64_DTPREL34:
9023 case elfcpp::R_PPC64_PLT_PCREL34:
9024 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9025 case elfcpp::R_PPC64_GOT_PCREL34:
9026 case elfcpp::R_PPC64_GOT_TLSGD34:
9027 case elfcpp::R_PPC64_GOT_TLSLD34:
9028 case elfcpp::R_PPC64_GOT_DTPREL34:
9029 case elfcpp::R_PPC64_GOT_TPREL34:
9030 target->set_powerxx_stubs();
9031 break;
d8f5a274
AM
9032 default:
9033 break;
9034 }
42cacb20
DE
9035}
9036
6d03d481
ST
9037// Process relocations for gc.
9038
9039template<int size, bool big_endian>
9040void
9041Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
9042 Symbol_table* symtab,
9043 Layout* layout,
9044 Sized_relobj_file<size, big_endian>* object,
9045 unsigned int data_shndx,
9046 unsigned int,
9047 const unsigned char* prelocs,
9048 size_t reloc_count,
9049 Output_section* output_section,
9050 bool needs_special_offset_handling,
9051 size_t local_symbol_count,
9052 const unsigned char* plocal_symbols)
6d03d481
ST
9053{
9054 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9055 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9056 Classify_reloc;
9057
e81fea4d
AM
9058 Powerpc_relobj<size, big_endian>* ppc_object
9059 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9060 if (size == 64)
9061 ppc_object->set_opd_valid();
9062 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9063 {
9064 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9065 for (p = ppc_object->access_from_map()->begin();
9066 p != ppc_object->access_from_map()->end();
9067 ++p)
9068 {
9069 Address dst_off = p->first;
9070 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9071 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9072 for (s = p->second.begin(); s != p->second.end(); ++s)
9073 {
efc6fa12 9074 Relobj* src_obj = s->first;
e81fea4d
AM
9075 unsigned int src_indx = s->second;
9076 symtab->gc()->add_reference(src_obj, src_indx,
9077 ppc_object, dst_indx);
9078 }
9079 p->second.clear();
9080 }
9081 ppc_object->access_from_map()->clear();
c6de8ed4 9082 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
9083 // Don't look at .opd relocs as .opd will reference everything.
9084 return;
9085 }
6d03d481 9086
4d625b70 9087 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
9088 symtab,
9089 layout,
9090 this,
9091 object,
9092 data_shndx,
9093 prelocs,
9094 reloc_count,
9095 output_section,
9096 needs_special_offset_handling,
9097 local_symbol_count,
9098 plocal_symbols);
9099}
9100
e81fea4d
AM
9101// Handle target specific gc actions when adding a gc reference from
9102// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9103// and DST_OFF. For powerpc64, this adds a referenc to the code
9104// section of a function descriptor.
9105
9106template<int size, bool big_endian>
9107void
9108Target_powerpc<size, big_endian>::do_gc_add_reference(
9109 Symbol_table* symtab,
efc6fa12 9110 Relobj* src_obj,
e81fea4d 9111 unsigned int src_shndx,
efc6fa12 9112 Relobj* dst_obj,
e81fea4d
AM
9113 unsigned int dst_shndx,
9114 Address dst_off) const
9115{
6c77229c
AM
9116 if (size != 64 || dst_obj->is_dynamic())
9117 return;
9118
e81fea4d
AM
9119 Powerpc_relobj<size, big_endian>* ppc_object
9120 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 9121 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
9122 {
9123 if (ppc_object->opd_valid())
9124 {
9125 dst_shndx = ppc_object->get_opd_ent(dst_off);
9126 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9127 }
9128 else
9129 {
9130 // If we haven't run scan_opd_relocs, we must delay
9131 // processing this function descriptor reference.
9132 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9133 }
9134 }
9135}
9136
9137// Add any special sections for this symbol to the gc work list.
9138// For powerpc64, this adds the code section of a function
9139// descriptor.
9140
9141template<int size, bool big_endian>
9142void
9143Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9144 Symbol_table* symtab,
9145 Symbol* sym) const
9146{
9147 if (size == 64)
9148 {
9149 Powerpc_relobj<size, big_endian>* ppc_object
9150 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9151 bool is_ordinary;
9152 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 9153 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
9154 {
9155 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9156 Address dst_off = gsym->value();
c6de8ed4
AM
9157 if (ppc_object->opd_valid())
9158 {
9159 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
9160 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9161 dst_indx));
c6de8ed4
AM
9162 }
9163 else
9164 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
9165 }
9166 }
9167}
9168
dc3714f3
AM
9169// For a symbol location in .opd, set LOC to the location of the
9170// function entry.
9171
9172template<int size, bool big_endian>
9173void
9174Target_powerpc<size, big_endian>::do_function_location(
9175 Symbol_location* loc) const
9176{
a2d7bf59 9177 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
9178 {
9179 if (loc->object->is_dynamic())
9180 {
9181 Powerpc_dynobj<size, big_endian>* ppc_object
9182 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9183 if (loc->shndx == ppc_object->opd_shndx())
9184 {
9185 Address dest_off;
9186 Address off = loc->offset - ppc_object->opd_address();
9187 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9188 loc->offset = dest_off;
9189 }
9190 }
9191 else
9192 {
9193 const Powerpc_relobj<size, big_endian>* ppc_object
9194 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9195 if (loc->shndx == ppc_object->opd_shndx())
9196 {
9197 Address dest_off;
9198 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9199 loc->offset = dest_off;
9200 }
9201 }
9202 }
9203}
9204
bbec1a5d
AM
9205// FNOFFSET in section SHNDX in OBJECT is the start of a function
9206// compiled with -fsplit-stack. The function calls non-split-stack
9207// code. Change the function to ensure it has enough stack space to
9208// call some random function.
9209
9210template<int size, bool big_endian>
9211void
9212Target_powerpc<size, big_endian>::do_calls_non_split(
9213 Relobj* object,
9214 unsigned int shndx,
9215 section_offset_type fnoffset,
9216 section_size_type fnsize,
6e0813d3
CC
9217 const unsigned char* prelocs,
9218 size_t reloc_count,
bbec1a5d
AM
9219 unsigned char* view,
9220 section_size_type view_size,
9221 std::string* from,
9222 std::string* to) const
9223{
9224 // 32-bit not supported.
9225 if (size == 32)
9226 {
9227 // warn
9228 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
9229 prelocs, reloc_count, view, view_size,
9230 from, to);
bbec1a5d
AM
9231 return;
9232 }
9233
9234 // The function always starts with
9235 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9236 // addis %r12,%r1,-allocate@ha
9237 // addi %r12,%r12,-allocate@l
9238 // cmpld %r12,%r0
9239 // but note that the addis or addi may be replaced with a nop
9240
9241 unsigned char *entry = view + fnoffset;
9242 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9243
9244 if ((insn & 0xffff0000) == addis_2_12)
9245 {
9246 /* Skip ELFv2 global entry code. */
9247 entry += 8;
9248 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9249 }
9250
9251 unsigned char *pinsn = entry;
9252 bool ok = false;
9253 const uint32_t ld_private_ss = 0xe80d8fc0;
9254 if (insn == ld_private_ss)
9255 {
9256 int32_t allocate = 0;
9257 while (1)
9258 {
9259 pinsn += 4;
9260 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9261 if ((insn & 0xffff0000) == addis_12_1)
9262 allocate += (insn & 0xffff) << 16;
9263 else if ((insn & 0xffff0000) == addi_12_1
9264 || (insn & 0xffff0000) == addi_12_12)
9265 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9266 else if (insn != nop)
9267 break;
9268 }
9269 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9270 {
9271 int extra = parameters->options().split_stack_adjust_size();
9272 allocate -= extra;
9273 if (allocate >= 0 || extra < 0)
9274 {
9275 object->error(_("split-stack stack size overflow at "
9276 "section %u offset %0zx"),
9277 shndx, static_cast<size_t>(fnoffset));
9278 return;
9279 }
9280 pinsn = entry + 4;
9281 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9282 if (insn != addis_12_1)
9283 {
9284 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9285 pinsn += 4;
9286 insn = addi_12_12 | (allocate & 0xffff);
9287 if (insn != addi_12_12)
9288 {
9289 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9290 pinsn += 4;
9291 }
9292 }
9293 else
9294 {
9295 insn = addi_12_1 | (allocate & 0xffff);
9296 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9297 pinsn += 4;
9298 }
9299 if (pinsn != entry + 12)
9300 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9301
9302 ok = true;
9303 }
9304 }
9305
9306 if (!ok)
9307 {
9308 if (!object->has_no_split_stack())
9309 object->error(_("failed to match split-stack sequence at "
9310 "section %u offset %0zx"),
9311 shndx, static_cast<size_t>(fnoffset));
9312 }
9313}
9314
42cacb20
DE
9315// Scan relocations for a section.
9316
9317template<int size, bool big_endian>
9318void
9319Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
9320 Symbol_table* symtab,
9321 Layout* layout,
9322 Sized_relobj_file<size, big_endian>* object,
9323 unsigned int data_shndx,
9324 unsigned int sh_type,
9325 const unsigned char* prelocs,
9326 size_t reloc_count,
9327 Output_section* output_section,
9328 bool needs_special_offset_handling,
9329 size_t local_symbol_count,
9330 const unsigned char* plocal_symbols)
42cacb20
DE
9331{
9332 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
9333 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9334 Classify_reloc;
42cacb20 9335
7ee7ff70
AM
9336 if (!this->plt_localentry0_init_)
9337 {
9338 bool plt_localentry0 = false;
9339 if (size == 64
9340 && this->abiversion() >= 2)
9341 {
9342 if (parameters->options().user_set_plt_localentry())
9343 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
9344 if (plt_localentry0
9345 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9346 gold_warning(_("--plt-localentry is especially dangerous without "
9347 "ld.so support to detect ABI violations"));
7ee7ff70
AM
9348 }
9349 this->plt_localentry0_ = plt_localentry0;
9350 this->plt_localentry0_init_ = true;
9351 }
9352
42cacb20
DE
9353 if (sh_type == elfcpp::SHT_REL)
9354 {
9355 gold_error(_("%s: unsupported REL reloc section"),
9356 object->name().c_str());
9357 return;
9358 }
9359
4d625b70 9360 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
9361 symtab,
9362 layout,
9363 this,
9364 object,
9365 data_shndx,
9366 prelocs,
9367 reloc_count,
9368 output_section,
9369 needs_special_offset_handling,
9370 local_symbol_count,
9371 plocal_symbols);
9372}
9373
ec4dbad3
AM
9374// Functor class for processing the global symbol table.
9375// Removes symbols defined on discarded opd entries.
9376
9377template<bool big_endian>
9378class Global_symbol_visitor_opd
9379{
9380 public:
9381 Global_symbol_visitor_opd()
9382 { }
9383
9384 void
9385 operator()(Sized_symbol<64>* sym)
9386 {
9387 if (sym->has_symtab_index()
9388 || sym->source() != Symbol::FROM_OBJECT
9389 || !sym->in_real_elf())
9390 return;
9391
6c77229c
AM
9392 if (sym->object()->is_dynamic())
9393 return;
9394
ec4dbad3
AM
9395 Powerpc_relobj<64, big_endian>* symobj
9396 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 9397 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
9398 return;
9399
9400 bool is_ordinary;
9401 unsigned int shndx = sym->shndx(&is_ordinary);
9402 if (shndx == symobj->opd_shndx()
9403 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
9404 {
9405 sym->set_undefined();
e3ee8ed4 9406 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
9407 sym->set_is_defined_in_discarded_section();
9408 sym->set_symtab_index(-1U);
9409 }
ec4dbad3
AM
9410 }
9411};
9412
f3a0ed29
AM
9413template<int size, bool big_endian>
9414void
9415Target_powerpc<size, big_endian>::define_save_restore_funcs(
9416 Layout* layout,
9417 Symbol_table* symtab)
9418{
9419 if (size == 64)
9420 {
d49044c7
AM
9421 Output_data_save_res<size, big_endian>* savres
9422 = new Output_data_save_res<size, big_endian>(symtab);
9423 this->savres_section_ = savres;
f3a0ed29
AM
9424 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9425 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9426 savres, ORDER_TEXT, false);
9427 }
9428}
9429
d8f5a274
AM
9430// Sort linker created .got section first (for the header), then input
9431// sections belonging to files using small model code.
9432
9433template<bool big_endian>
9434class Sort_toc_sections
9435{
9436 public:
9437 bool
9438 operator()(const Output_section::Input_section& is1,
9439 const Output_section::Input_section& is2) const
9440 {
9441 if (!is1.is_input_section() && is2.is_input_section())
9442 return true;
9443 bool small1
9444 = (is1.is_input_section()
9445 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9446 ->has_small_toc_reloc()));
9447 bool small2
9448 = (is2.is_input_section()
9449 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9450 ->has_small_toc_reloc()));
9451 return small1 && !small2;
9452 }
9453};
9454
42cacb20
DE
9455// Finalize the sections.
9456
9457template<int size, bool big_endian>
9458void
d5b40221
DK
9459Target_powerpc<size, big_endian>::do_finalize_sections(
9460 Layout* layout,
724436fc 9461 const Input_objects* input_objects,
ec4dbad3 9462 Symbol_table* symtab)
42cacb20 9463{
c9824451
AM
9464 if (parameters->doing_static_link())
9465 {
9466 // At least some versions of glibc elf-init.o have a strong
9467 // reference to __rela_iplt marker syms. A weak ref would be
9468 // better..
9469 if (this->iplt_ != NULL)
9470 {
9471 Reloc_section* rel = this->iplt_->rel_plt();
9472 symtab->define_in_output_data("__rela_iplt_start", NULL,
9473 Symbol_table::PREDEFINED, rel, 0, 0,
9474 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9475 elfcpp::STV_HIDDEN, 0, false, true);
9476 symtab->define_in_output_data("__rela_iplt_end", NULL,
9477 Symbol_table::PREDEFINED, rel, 0, 0,
9478 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9479 elfcpp::STV_HIDDEN, 0, true, true);
9480 }
9481 else
9482 {
9483 symtab->define_as_constant("__rela_iplt_start", NULL,
9484 Symbol_table::PREDEFINED, 0, 0,
9485 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9486 elfcpp::STV_HIDDEN, 0, true, false);
9487 symtab->define_as_constant("__rela_iplt_end", NULL,
9488 Symbol_table::PREDEFINED, 0, 0,
9489 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9490 elfcpp::STV_HIDDEN, 0, true, false);
9491 }
9492 }
9493
ec4dbad3
AM
9494 if (size == 64)
9495 {
9496 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9497 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
9498
9499 if (!parameters->options().relocatable())
9500 {
9501 this->define_save_restore_funcs(layout, symtab);
9502
9503 // Annoyingly, we need to make these sections now whether or
9504 // not we need them. If we delay until do_relax then we
9505 // need to mess with the relaxation machinery checkpointing.
9506 this->got_section(symtab, layout);
9507 this->make_brlt_section(layout);
d8f5a274
AM
9508
9509 if (parameters->options().toc_sort())
9510 {
9511 Output_section* os = this->got_->output_section();
9512 if (os != NULL && os->input_sections().size() > 1)
9513 std::stable_sort(os->input_sections().begin(),
9514 os->input_sections().end(),
9515 Sort_toc_sections<big_endian>());
9516 }
ec661b9d 9517 }
ec4dbad3
AM
9518 }
9519
42cacb20 9520 // Fill in some more dynamic tags.
c9269dff 9521 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 9522 if (odyn != NULL)
cf43a2fe 9523 {
c9824451
AM
9524 const Reloc_section* rel_plt = (this->plt_ == NULL
9525 ? NULL
9526 : this->plt_->rel_plt());
9527 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9528 this->rela_dyn_, true, size == 32);
9529
9530 if (size == 32)
dd93cd0a 9531 {
c9824451
AM
9532 if (this->got_ != NULL)
9533 {
9534 this->got_->finalize_data_size();
9535 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9536 this->got_, this->got_->g_o_t());
9537 }
34e0882b
AM
9538 if (this->has_tls_get_addr_opt_)
9539 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 9540 }
c9824451 9541 else
dd93cd0a 9542 {
c9824451
AM
9543 if (this->glink_ != NULL)
9544 {
9545 this->glink_->finalize_data_size();
9546 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9547 this->glink_,
9e390558 9548 (this->glink_->pltresolve_size()
c9824451
AM
9549 - 32));
9550 }
34e0882b 9551 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 9552 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
9553 ((this->has_localentry0_
9554 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9555 | (this->has_tls_get_addr_opt_
9556 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 9557 }
c9269dff 9558 }
cf43a2fe 9559
42cacb20
DE
9560 // Emit any relocs we saved in an attempt to avoid generating COPY
9561 // relocs.
9562 if (this->copy_relocs_.any_saved_relocs())
9563 this->copy_relocs_.emit(this->rela_dyn_section(layout));
724436fc
AM
9564
9565 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9566 p != input_objects->relobj_end();
9567 ++p)
9568 {
9569 Powerpc_relobj<size, big_endian>* ppc_relobj
9570 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9571 if (ppc_relobj->attributes_section_data())
9572 this->merge_object_attributes(ppc_relobj->name().c_str(),
9573 ppc_relobj->attributes_section_data());
9574 }
9575 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9576 p != input_objects->dynobj_end();
9577 ++p)
9578 {
9579 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9580 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9581 if (ppc_dynobj->attributes_section_data())
9582 this->merge_object_attributes(ppc_dynobj->name().c_str(),
9583 ppc_dynobj->attributes_section_data());
9584 }
9585
9586 // Create a .gnu.attributes section if we have merged any attributes
9587 // from inputs.
9588 if (this->attributes_section_data_ != NULL
9589 && this->attributes_section_data_->size() != 0)
9590 {
9591 Output_attributes_section_data* attributes_section
9592 = new Output_attributes_section_data(*this->attributes_section_data_);
9593 layout->add_output_section_data(".gnu.attributes",
9594 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9595 attributes_section, ORDER_INVALID, false);
9596 }
9597}
9598
9599// Merge object attributes from input file called NAME with those of the
9600// output. The input object attributes are in the object pointed by PASD.
9601
9602template<int size, bool big_endian>
9603void
9604Target_powerpc<size, big_endian>::merge_object_attributes(
9605 const char* name,
9606 const Attributes_section_data* pasd)
9607{
9608 // Return if there is no attributes section data.
9609 if (pasd == NULL)
9610 return;
9611
9612 // Create output object attributes.
9613 if (this->attributes_section_data_ == NULL)
9614 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9615
9616 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9617 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9618 Object_attribute* out_attr
9619 = this->attributes_section_data_->known_attributes(vendor);
9620
9621 const char* err;
9622 const char* first;
9623 const char* second;
9624 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9625 int in_fp = in_attr[tag].int_value() & 0xf;
9626 int out_fp = out_attr[tag].int_value() & 0xf;
9627 if (in_fp != out_fp)
9628 {
9629 err = NULL;
9630 if ((in_fp & 3) == 0)
9631 ;
9632 else if ((out_fp & 3) == 0)
9633 {
9634 out_fp |= in_fp & 3;
9635 out_attr[tag].set_int_value(out_fp);
9636 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9637 this->last_fp_ = name;
9638 }
9639 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9640 {
9641 err = N_("%s uses hard float, %s uses soft float");
9642 first = this->last_fp_;
9643 second = name;
9644 }
9645 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9646 {
9647 err = N_("%s uses hard float, %s uses soft float");
9648 first = name;
9649 second = this->last_fp_;
9650 }
9651 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9652 {
9653 err = N_("%s uses double-precision hard float, "
9654 "%s uses single-precision hard float");
9655 first = this->last_fp_;
9656 second = name;
9657 }
9658 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9659 {
9660 err = N_("%s uses double-precision hard float, "
9661 "%s uses single-precision hard float");
9662 first = name;
9663 second = this->last_fp_;
9664 }
9665
9666 if (err || (in_fp & 0xc) == 0)
9667 ;
9668 else if ((out_fp & 0xc) == 0)
9669 {
9670 out_fp |= in_fp & 0xc;
9671 out_attr[tag].set_int_value(out_fp);
9672 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9673 this->last_ld_ = name;
9674 }
9675 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9676 {
9677 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9678 first = name;
9679 second = this->last_ld_;
9680 }
9681 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9682 {
9683 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9684 first = this->last_ld_;
9685 second = name;
9686 }
9687 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9688 {
9689 err = N_("%s uses IBM long double, %s uses IEEE long double");
9690 first = this->last_ld_;
9691 second = name;
9692 }
9693 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9694 {
9695 err = N_("%s uses IBM long double, %s uses IEEE long double");
9696 first = name;
9697 second = this->last_ld_;
9698 }
9699
9700 if (err)
9701 {
9702 if (parameters->options().warn_mismatch())
9703 gold_error(_(err), first, second);
9704 // Arrange for this attribute to be deleted. It's better to
9705 // say "don't know" about a file than to wrongly claim compliance.
9706 out_attr[tag].set_type(0);
9707 }
9708 }
9709
9710 if (size == 32)
9711 {
9712 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
9713 int in_vec = in_attr[tag].int_value() & 3;
9714 int out_vec = out_attr[tag].int_value() & 3;
9715 if (in_vec != out_vec)
9716 {
9717 err = NULL;
9718 if (in_vec == 0)
9719 ;
9720 else if (out_vec == 0)
9721 {
9722 out_vec = in_vec;
9723 out_attr[tag].set_int_value(out_vec);
9724 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9725 this->last_vec_ = name;
9726 }
9727 // For now, allow generic to transition to AltiVec or SPE
9728 // without a warning. If GCC marked files with their stack
9729 // alignment and used don't-care markings for files which are
9730 // not affected by the vector ABI, we could warn about this
9731 // case too. */
9732 else if (in_vec == 1)
9733 ;
9734 else if (out_vec == 1)
9735 {
9736 out_vec = in_vec;
9737 out_attr[tag].set_int_value(out_vec);
9738 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9739 this->last_vec_ = name;
9740 }
9741 else if (out_vec < in_vec)
9742 {
9743 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9744 first = this->last_vec_;
9745 second = name;
9746 }
9747 else if (out_vec > in_vec)
9748 {
9749 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9750 first = name;
9751 second = this->last_vec_;
9752 }
9753 if (err)
9754 {
9755 if (parameters->options().warn_mismatch())
9756 gold_error(_(err), first, second);
9757 out_attr[tag].set_type(0);
9758 }
9759 }
9760
9761 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
9762 int in_struct = in_attr[tag].int_value() & 3;
9763 int out_struct = out_attr[tag].int_value() & 3;
9764 if (in_struct != out_struct)
9765 {
9766 err = NULL;
9767 if (in_struct == 0 || in_struct == 3)
9768 ;
9769 else if (out_struct == 0)
9770 {
9771 out_struct = in_struct;
9772 out_attr[tag].set_int_value(out_struct);
9773 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9774 this->last_struct_ = name;
9775 }
9776 else if (out_struct < in_struct)
9777 {
9778 err = N_("%s uses r3/r4 for small structure returns, "
9779 "%s uses memory");
9780 first = this->last_struct_;
9781 second = name;
9782 }
9783 else if (out_struct > in_struct)
9784 {
9785 err = N_("%s uses r3/r4 for small structure returns, "
9786 "%s uses memory");
9787 first = name;
9788 second = this->last_struct_;
9789 }
9790 if (err)
9791 {
9792 if (parameters->options().warn_mismatch())
9793 gold_error(_(err), first, second);
9794 out_attr[tag].set_type(0);
9795 }
9796 }
9797 }
9798
9799 // Merge Tag_compatibility attributes and any common GNU ones.
9800 this->attributes_section_data_->merge(name, pasd);
42cacb20
DE
9801}
9802
5edad15d
AM
9803// Emit any saved relocs, and mark toc entries using any of these
9804// relocs as not optimizable.
aba6bc71 9805
5edad15d
AM
9806template<int sh_type, int size, bool big_endian>
9807void
9808Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
9809 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 9810{
5edad15d
AM
9811 if (size == 64
9812 && parameters->options().toc_optimize())
9813 {
9814 for (typename Copy_relocs<sh_type, size, big_endian>::
9815 Copy_reloc_entries::iterator p = this->entries_.begin();
9816 p != this->entries_.end();
9817 ++p)
9818 {
9819 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
9820 entry = *p;
9821
9822 // If the symbol is no longer defined in a dynamic object,
9823 // then we emitted a COPY relocation. If it is still
9824 // dynamic then we'll need dynamic relocations and thus
9825 // can't optimize toc entries.
9826 if (entry.sym_->is_from_dynobj())
9827 {
9828 Powerpc_relobj<size, big_endian>* ppc_object
9829 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
9830 if (entry.shndx_ == ppc_object->toc_shndx())
9831 ppc_object->set_no_toc_opt(entry.address_);
9832 }
9833 }
9834 }
9835
9836 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
9837}
9838
3ea0a085
AM
9839// Return the value to use for a branch relocation.
9840
9841template<int size, bool big_endian>
1611bc4a 9842bool
3ea0a085 9843Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 9844 const Symbol_table* symtab,
3ea0a085
AM
9845 const Sized_symbol<size>* gsym,
9846 Powerpc_relobj<size, big_endian>* object,
1611bc4a 9847 Address *value,
3ea0a085
AM
9848 unsigned int *dest_shndx)
9849{
9055360d
AM
9850 if (size == 32 || this->abiversion() >= 2)
9851 gold_unreachable();
3ea0a085 9852 *dest_shndx = 0;
3ea0a085
AM
9853
9854 // If the symbol is defined in an opd section, ie. is a function
9855 // descriptor, use the function descriptor code entry address
9856 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 9857 if (gsym != NULL
0e123f69
AM
9858 && (gsym->source() != Symbol::FROM_OBJECT
9859 || gsym->object()->is_dynamic()))
1611bc4a 9860 return true;
3ea0a085
AM
9861 if (gsym != NULL)
9862 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9863 unsigned int shndx = symobj->opd_shndx();
9864 if (shndx == 0)
1611bc4a 9865 return true;
3ea0a085 9866 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 9867 if (opd_addr == invalid_address)
1611bc4a 9868 return true;
c6905c28 9869 opd_addr += symobj->output_section_address(shndx);
1611bc4a 9870 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
9871 {
9872 Address sec_off;
1611bc4a 9873 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
9874 if (symtab->is_section_folded(symobj, *dest_shndx))
9875 {
9876 Section_id folded
9877 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
9878 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
9879 *dest_shndx = folded.second;
9880 }
3ea0a085 9881 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
9882 if (sec_addr == invalid_address)
9883 return false;
9884
3ea0a085 9885 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 9886 *value = sec_addr + sec_off;
3ea0a085 9887 }
1611bc4a 9888 return true;
3ea0a085
AM
9889}
9890
c9b8abb7
AM
9891template<int size>
9892static bool
9893relative_value_is_known(const Sized_symbol<size>* gsym)
9894{
9895 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9896 return false;
9897
9898 if (gsym->is_from_dynobj()
9899 || gsym->is_undefined()
9900 || gsym->is_preemptible())
9901 return false;
9902
9903 if (gsym->is_absolute())
9904 return !parameters->options().output_is_position_independent();
9905
9906 return true;
9907}
9908
9909template<int size>
9910static bool
9911relative_value_is_known(const Symbol_value<size>* psymval)
9912{
9913 if (psymval->is_ifunc_symbol())
9914 return false;
9915
9916 bool is_ordinary;
9917 unsigned int shndx = psymval->input_shndx(&is_ordinary);
9918
9919 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
9920}
9921
0c951c25
AM
9922// PCREL_OPT in one instance flags to the linker that a pair of insns:
9923// pld ra,symbol@got@pcrel
9924// load/store rt,0(ra)
9925// or
9926// pla ra,symbol@pcrel
9927// load/store rt,0(ra)
9928// may be translated to
9929// pload/pstore rt,symbol@pcrel
9930// nop.
9931// This function returns true if the optimization is possible, placing
9932// the prefix insn in *PINSN1 and a NOP in *PINSN2.
9933//
9934// On entry to this function, the linker has already determined that
9935// the pld can be replaced with pla: *PINSN1 is that pla insn,
9936// while *PINSN2 is the second instruction.
9937
9938inline bool
9939xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
9940{
9941 uint32_t insn2 = *pinsn2 >> 32;
9942 uint64_t i1new;
9943
9944 // Check that regs match.
9945 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
9946 return false;
9947
9948 switch ((insn2 >> 26) & 63)
9949 {
9950 default:
9951 return false;
9952
9953 case 32: // lwz
9954 case 34: // lbz
9955 case 36: // stw
9956 case 38: // stb
9957 case 40: // lhz
9958 case 42: // lha
9959 case 44: // sth
9960 case 48: // lfs
9961 case 50: // lfd
9962 case 52: // stfs
9963 case 54: // stfd
9964 // These are the PMLS cases, where we just need to tack a prefix
9965 // on the insn. Check that the D field is zero.
9966 if ((insn2 & 0xffff) != 0)
9967 return false;
9968 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
9969 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9970 break;
9971
9972 case 58: // lwa, ld
9973 if ((insn2 & 0xfffd) != 0)
9974 return false;
9975 i1new = ((1ULL << 58) | (1ULL << 52)
9976 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
9977 | (insn2 & (31ULL << 21)));
9978 break;
9979
9980 case 57: // lxsd, lxssp
9981 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
9982 return false;
9983 i1new = ((1ULL << 58) | (1ULL << 52)
9984 | ((40ULL | (insn2 & 3)) << 26)
9985 | (insn2 & (31ULL << 21)));
9986 break;
9987
9988 case 61: // stxsd, stxssp, lxv, stxv
9989 if ((insn2 & 3) == 0)
9990 return false;
9991 else if ((insn2 & 3) >= 2)
9992 {
9993 if ((insn2 & 0xfffc) != 0)
9994 return false;
9995 i1new = ((1ULL << 58) | (1ULL << 52)
9996 | ((44ULL | (insn2 & 3)) << 26)
9997 | (insn2 & (31ULL << 21)));
9998 }
9999 else
10000 {
10001 if ((insn2 & 0xfff0) != 0)
10002 return false;
10003 i1new = ((1ULL << 58) | (1ULL << 52)
10004 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10005 | (insn2 & (31ULL << 21)));
10006 }
10007 break;
10008
10009 case 56: // lq
10010 if ((insn2 & 0xffff) != 0)
10011 return false;
10012 i1new = ((1ULL << 58) | (1ULL << 52)
10013 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10014 break;
10015
10016 case 62: // std, stq
10017 if ((insn2 & 0xfffd) != 0)
10018 return false;
10019 i1new = ((1ULL << 58) | (1ULL << 52)
10020 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10021 | (insn2 & (31ULL << 21)));
10022 break;
10023 }
10024
10025 *pinsn1 = i1new;
10026 *pinsn2 = (uint64_t) nop << 32;
10027 return true;
10028}
10029
42cacb20
DE
10030// Perform a relocation.
10031
10032template<int size, bool big_endian>
10033inline bool
10034Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 10035 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 10036 unsigned int,
d83ce4e3
AM
10037 Target_powerpc* target,
10038 Output_section* os,
10039 size_t relnum,
91a65d2f 10040 const unsigned char* preloc,
d83ce4e3
AM
10041 const Sized_symbol<size>* gsym,
10042 const Symbol_value<size>* psymval,
10043 unsigned char* view,
c9269dff
AM
10044 Address address,
10045 section_size_type view_size)
42cacb20 10046{
23cedd1d
AM
10047 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10048 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10049 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10050
0e804863
ILT
10051 if (view == NULL)
10052 return true;
10053
34e0882b
AM
10054 if (target->replace_tls_get_addr(gsym))
10055 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10056
91a65d2f
AM
10057 const elfcpp::Rela<size, big_endian> rela(preloc);
10058 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
34e0882b 10059 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 10060 {
e3deeb9c
AM
10061 case Track_tls::NOT_EXPECTED:
10062 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10063 _("__tls_get_addr call lacks marker reloc"));
10064 break;
10065 case Track_tls::EXPECTED:
10066 // We have already complained.
10067 break;
10068 case Track_tls::SKIP:
23cedd1d 10069 if (is_plt16_reloc<size>(r_type)
32f59844
AM
10070 || r_type == elfcpp::R_POWERPC_PLTSEQ
10071 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
23cedd1d
AM
10072 {
10073 Insn* iview = reinterpret_cast<Insn*>(view);
10074 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10075 }
10076 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10077 {
10078 Insn* iview = reinterpret_cast<Insn*>(view);
10079 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10080 }
e4dff765
AM
10081 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10082 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10083 {
10084 Insn* iview = reinterpret_cast<Insn*>(view);
10085 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10086 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10087 }
e3deeb9c
AM
10088 return true;
10089 case Track_tls::NORMAL:
10090 break;
dd93cd0a 10091 }
dd93cd0a 10092
dcfc7dd4
AM
10093 // Offset from start of insn to d-field reloc.
10094 const int d_offset = big_endian ? 2 : 0;
10095
3ea0a085
AM
10096 Powerpc_relobj<size, big_endian>* const object
10097 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 10098 Address value = 0;
0cfb0717 10099 bool has_stub_value = false;
7ee7ff70 10100 bool localentry0 = false;
e5d5f5ed 10101 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
08be3224
AM
10102 bool has_plt_offset
10103 = (gsym != NULL
88b8e639 10104 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
08be3224
AM
10105 : object->local_has_plt_offset(r_sym));
10106 if (has_plt_offset
10107 && !is_plt16_reloc<size>(r_type)
e4dff765
AM
10108 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10109 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
23cedd1d
AM
10110 && r_type != elfcpp::R_POWERPC_PLTSEQ
10111 && r_type != elfcpp::R_POWERPC_PLTCALL
32f59844
AM
10112 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10113 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
b3ccdeb5 10114 && (!psymval->is_ifunc_symbol()
9055360d 10115 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 10116 {
9055360d
AM
10117 if (size == 64
10118 && gsym != NULL
10119 && target->abiversion() >= 2
10120 && !parameters->options().output_is_position_independent()
32f59844 10121 && !is_branch_reloc<size>(r_type))
ec661b9d 10122 {
faa2211d
AM
10123 Address off = target->glink_section()->find_global_entry(gsym);
10124 if (off != invalid_address)
6ec65f28
AM
10125 {
10126 value = target->glink_section()->global_entry_address() + off;
10127 has_stub_value = true;
10128 }
ec661b9d 10129 }
c9824451 10130 else
9055360d 10131 {
64b5d6d7
AM
10132 Stub_table<size, big_endian>* stub_table = NULL;
10133 if (target->stub_tables().size() == 1)
10134 stub_table = target->stub_tables()[0];
10135 if (stub_table == NULL
10136 && !(size == 32
10137 && gsym != NULL
10138 && !parameters->options().output_is_position_independent()
32f59844 10139 && !is_branch_reloc<size>(r_type)))
64b5d6d7 10140 stub_table = object->stub_table(relinfo->data_shndx);
9055360d
AM
10141 if (stub_table == NULL)
10142 {
64b5d6d7
AM
10143 // This is a ref from a data section to an ifunc symbol,
10144 // or a non-branch reloc for which we always want to use
10145 // one set of stubs for resolving function addresses.
9055360d
AM
10146 if (target->stub_tables().size() != 0)
10147 stub_table = target->stub_tables()[0];
10148 }
faa2211d
AM
10149 if (stub_table != NULL)
10150 {
7e57d19e 10151 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 10152 if (gsym != NULL)
7e57d19e 10153 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
10154 rela.get_r_addend());
10155 else
7e57d19e 10156 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 10157 rela.get_r_addend());
7e57d19e 10158 if (ent != NULL)
faa2211d 10159 {
7e57d19e
AM
10160 value = stub_table->stub_address() + ent->off_;
10161 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10162 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10163 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10164 if (size == 64
10165 && ent->r2save_
32f59844
AM
10166 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
10167 value += 4;
10168 else if (size == 64
10169 && ent->r2save_
10170 && relnum < reloc_count - 1)
7e57d19e
AM
10171 {
10172 Reltype next_rela(preloc + reloc_size);
10173 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10174 == elfcpp::R_PPC64_TOCSAVE
10175 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
10176 value += 4;
10177 }
7ee7ff70 10178 localentry0 = ent->localentry0_;
faa2211d
AM
10179 has_stub_value = true;
10180 }
10181 }
9055360d 10182 }
faa2211d
AM
10183 // We don't care too much about bogus debug references to
10184 // non-local functions, but otherwise there had better be a plt
10185 // call stub or global entry stub as appropriate.
10186 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 10187 }
cf43a2fe 10188
e4dff765
AM
10189 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10190 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10191 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
08be3224
AM
10192 {
10193 const Output_data_plt_powerpc<size, big_endian>* plt;
10194 if (gsym)
10195 value = target->plt_off(gsym, &plt);
10196 else
10197 value = target->plt_off(object, r_sym, &plt);
10198 value += plt->address();
10199
10200 if (size == 64)
e4dff765
AM
10201 {
10202 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10203 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10204 value -= (target->got_section()->output_section()->address()
10205 + object->toc_base_offset());
10206 }
08be3224
AM
10207 else if (parameters->options().output_is_position_independent())
10208 {
10209 if (rela.get_r_addend() >= 32768)
10210 {
10211 unsigned int got2 = object->got2_shndx();
10212 value -= (object->get_output_section_offset(got2)
10213 + object->output_section(got2)->address()
10214 + rela.get_r_addend());
10215 }
10216 else
10217 value -= (target->got_section()->address()
10218 + target->got_section()->g_o_t());
10219 }
10220 }
23cedd1d
AM
10221 else if (!has_plt_offset
10222 && (is_plt16_reloc<size>(r_type)
32f59844
AM
10223 || r_type == elfcpp::R_POWERPC_PLTSEQ
10224 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
23cedd1d
AM
10225 {
10226 Insn* iview = reinterpret_cast<Insn*>(view);
10227 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10228 r_type = elfcpp::R_POWERPC_NONE;
10229 }
e4dff765
AM
10230 else if (!has_plt_offset
10231 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10232 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10233 {
10234 Insn* iview = reinterpret_cast<Insn*>(view);
10235 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10236 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10237 r_type = elfcpp::R_POWERPC_NONE;
10238 }
08be3224
AM
10239 else if (r_type == elfcpp::R_POWERPC_GOT16
10240 || r_type == elfcpp::R_POWERPC_GOT16_LO
10241 || r_type == elfcpp::R_POWERPC_GOT16_HI
10242 || r_type == elfcpp::R_POWERPC_GOT16_HA
10243 || r_type == elfcpp::R_PPC64_GOT16_DS
e4dff765
AM
10244 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
10245 || r_type == elfcpp::R_PPC64_GOT_PCREL34)
42cacb20 10246 {
cf43a2fe
AM
10247 if (gsym != NULL)
10248 {
10249 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10250 value = gsym->got_offset(GOT_TYPE_STANDARD);
10251 }
10252 else
10253 {
cf43a2fe
AM
10254 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10255 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10256 }
e4dff765
AM
10257 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10258 value += target->got_section()->address();
10259 else
10260 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
10261 }
10262 else if (r_type == elfcpp::R_PPC64_TOC)
10263 {
c9269dff 10264 value = (target->got_section()->output_section()->address()
dd93cd0a 10265 + object->toc_base_offset());
cf43a2fe
AM
10266 }
10267 else if (gsym != NULL
10268 && (r_type == elfcpp::R_POWERPC_REL24
10269 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 10270 && has_stub_value)
cf43a2fe 10271 {
c9269dff
AM
10272 if (size == 64)
10273 {
10274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10275 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
10276 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10277 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 10278 {
3ea0a085 10279 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 10280 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
10281 if ((insn & 1) != 0
10282 && (insn2 == nop
10283 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 10284 {
b4f7960d
AM
10285 elfcpp::Swap<32, big_endian>::
10286 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
10287 can_plt_call = true;
10288 }
10289 }
10290 if (!can_plt_call)
3ea0a085
AM
10291 {
10292 // If we don't have a branch and link followed by a nop,
10293 // we can't go via the plt because there is no place to
10294 // put a toc restoring instruction.
10295 // Unless we know we won't be returning.
10296 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10297 can_plt_call = true;
10298 }
10299 if (!can_plt_call)
10300 {
ba8ca3e7
AM
10301 // g++ as of 20130507 emits self-calls without a
10302 // following nop. This is arguably wrong since we have
10303 // conflicting information. On the one hand a global
10304 // symbol and on the other a local call sequence, but
10305 // don't error for this special case.
10306 // It isn't possible to cheaply verify we have exactly
10307 // such a call. Allow all calls to the same section.
3ea0a085 10308 bool ok = false;
c9824451 10309 Address code = value;
3ea0a085
AM
10310 if (gsym->source() == Symbol::FROM_OBJECT
10311 && gsym->object() == object)
10312 {
9055360d
AM
10313 unsigned int dest_shndx = 0;
10314 if (target->abiversion() < 2)
10315 {
10316 Address addend = rela.get_r_addend();
1611bc4a
AM
10317 code = psymval->value(object, addend);
10318 target->symval_for_branch(relinfo->symtab, gsym, object,
10319 &code, &dest_shndx);
9055360d 10320 }
3ea0a085
AM
10321 bool is_ordinary;
10322 if (dest_shndx == 0)
10323 dest_shndx = gsym->shndx(&is_ordinary);
10324 ok = dest_shndx == relinfo->data_shndx;
10325 }
10326 if (!ok)
c9824451
AM
10327 {
10328 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10329 _("call lacks nop, can't restore toc; "
10330 "recompile with -fPIC"));
10331 value = code;
10332 }
3ea0a085 10333 }
c9269dff 10334 }
cf43a2fe 10335 }
dd93cd0a
AM
10336 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10337 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10338 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
89c52ae3
AM
10339 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10340 || r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a
AM
10341 {
10342 // First instruction of a global dynamic sequence, arg setup insn.
10343 const bool final = gsym == NULL || gsym->final_value_is_known();
10344 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10345 enum Got_type got_type = GOT_TYPE_STANDARD;
10346 if (tls_type == tls::TLSOPT_NONE)
10347 got_type = GOT_TYPE_TLSGD;
10348 else if (tls_type == tls::TLSOPT_TO_IE)
10349 got_type = GOT_TYPE_TPREL;
10350 if (got_type != GOT_TYPE_STANDARD)
10351 {
10352 if (gsym != NULL)
10353 {
10354 gold_assert(gsym->has_got_offset(got_type));
10355 value = gsym->got_offset(got_type);
10356 }
10357 else
10358 {
dd93cd0a
AM
10359 gold_assert(object->local_has_got_offset(r_sym, got_type));
10360 value = object->local_got_offset(r_sym, got_type);
10361 }
89c52ae3
AM
10362 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10363 value += target->got_section()->address();
10364 else
10365 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10366 }
10367 if (tls_type == tls::TLSOPT_TO_IE)
10368 {
89c52ae3 10369 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a 10370 {
89c52ae3
AM
10371 Insn* iview = reinterpret_cast<Insn*>(view);
10372 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10373 pinsn <<= 32;
10374 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10375 // pla -> pld
10376 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10377 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10378 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10379 pinsn & 0xffffffff);
10380 r_type = elfcpp::R_PPC64_GOT_TPREL34;
10381 }
10382 else
10383 {
10384 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10385 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10386 {
10387 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10388 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10389 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10390 if (size == 32)
10391 insn |= 32 << 26; // lwz
10392 else
10393 insn |= 58 << 26; // ld
10394 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10395 }
10396 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10397 - elfcpp::R_POWERPC_GOT_TLSGD16);
dd93cd0a 10398 }
dd93cd0a
AM
10399 }
10400 else if (tls_type == tls::TLSOPT_TO_LE)
10401 {
89c52ae3 10402 if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
dd93cd0a 10403 {
89c52ae3
AM
10404 Insn* iview = reinterpret_cast<Insn*>(view);
10405 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10406 pinsn <<= 32;
10407 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10408 // pla pcrel -> paddi r13
10409 pinsn += (-1ULL << 52) + (13ULL << 16);
10410 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10411 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10412 pinsn & 0xffffffff);
10413 r_type = elfcpp::R_PPC64_TPREL34;
dd93cd0a
AM
10414 value = psymval->value(object, rela.get_r_addend());
10415 }
10416 else
10417 {
89c52ae3
AM
10418 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10419 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10420 {
10421 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10422 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10423 insn &= (1 << 26) - (1 << 21); // extract rt
10424 if (size == 32)
10425 insn |= addis_0_2;
10426 else
10427 insn |= addis_0_13;
10428 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10429 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10430 value = psymval->value(object, rela.get_r_addend());
10431 }
10432 else
10433 {
10434 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10435 Insn insn = nop;
10436 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10437 r_type = elfcpp::R_POWERPC_NONE;
10438 }
dd93cd0a
AM
10439 }
10440 }
10441 }
10442 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10443 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10444 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
89c52ae3
AM
10445 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10446 || r_type == elfcpp::R_PPC64_GOT_TLSLD34)
dd93cd0a
AM
10447 {
10448 // First instruction of a local dynamic sequence, arg setup insn.
10449 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10450 if (tls_type == tls::TLSOPT_NONE)
10451 {
10452 value = target->tlsld_got_offset();
89c52ae3
AM
10453 if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10454 value += target->got_section()->address();
10455 else
10456 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10457 }
10458 else
10459 {
10460 gold_assert(tls_type == tls::TLSOPT_TO_LE);
89c52ae3
AM
10461 if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10462 {
10463 Insn* iview = reinterpret_cast<Insn*>(view);
10464 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10465 pinsn <<= 32;
10466 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10467 // pla pcrel -> paddi r13
10468 pinsn += (-1ULL << 52) + (13ULL << 16);
10469 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10470 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10471 pinsn & 0xffffffff);
10472 r_type = elfcpp::R_PPC64_TPREL34;
10473 value = dtp_offset;
10474 }
10475 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10476 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
dd93cd0a 10477 {
dcfc7dd4 10478 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
10479 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10480 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 10481 if (size == 32)
0f81d3f0
AM
10482 insn |= addis_0_2;
10483 else
10484 insn |= addis_0_13;
dd93cd0a
AM
10485 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10486 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 10487 value = dtp_offset;
dd93cd0a
AM
10488 }
10489 else
10490 {
dcfc7dd4 10491 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10492 Insn insn = nop;
10493 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10494 r_type = elfcpp::R_POWERPC_NONE;
10495 }
10496 }
10497 }
10498 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10499 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10500 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
89c52ae3
AM
10501 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10502 || r_type == elfcpp::R_PPC64_GOT_DTPREL34)
dd93cd0a
AM
10503 {
10504 // Accesses relative to a local dynamic sequence address,
10505 // no optimisation here.
10506 if (gsym != NULL)
10507 {
10508 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10509 value = gsym->got_offset(GOT_TYPE_DTPREL);
10510 }
10511 else
10512 {
dd93cd0a
AM
10513 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10514 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10515 }
89c52ae3
AM
10516 if (r_type == elfcpp::R_PPC64_GOT_DTPREL34)
10517 value += target->got_section()->address();
10518 else
10519 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10520 }
10521 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10522 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10523 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
89c52ae3
AM
10524 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10525 || r_type == elfcpp::R_PPC64_GOT_TPREL34)
dd93cd0a
AM
10526 {
10527 // First instruction of initial exec sequence.
10528 const bool final = gsym == NULL || gsym->final_value_is_known();
10529 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10530 if (tls_type == tls::TLSOPT_NONE)
10531 {
10532 if (gsym != NULL)
10533 {
10534 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10535 value = gsym->got_offset(GOT_TYPE_TPREL);
10536 }
10537 else
10538 {
dd93cd0a
AM
10539 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10540 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10541 }
89c52ae3
AM
10542 if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10543 value += target->got_section()->address();
10544 else
10545 value -= target->got_section()->got_base_offset(object);
dd93cd0a
AM
10546 }
10547 else
10548 {
10549 gold_assert(tls_type == tls::TLSOPT_TO_LE);
89c52ae3
AM
10550 if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10551 {
10552 Insn* iview = reinterpret_cast<Insn*>(view);
10553 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10554 pinsn <<= 32;
10555 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10556 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10557 pinsn += ((2ULL << 56) + (-1ULL << 52)
10558 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10559 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10560 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10561 pinsn & 0xffffffff);
10562 r_type = elfcpp::R_PPC64_TPREL34;
10563 value = psymval->value(object, rela.get_r_addend());
10564 }
10565 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10566 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
dd93cd0a 10567 {
dcfc7dd4 10568 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10569 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10570 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10571 if (size == 32)
10572 insn |= addis_0_2;
10573 else
10574 insn |= addis_0_13;
10575 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10576 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10577 value = psymval->value(object, rela.get_r_addend());
10578 }
10579 else
10580 {
dcfc7dd4 10581 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
10582 Insn insn = nop;
10583 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10584 r_type = elfcpp::R_POWERPC_NONE;
10585 }
10586 }
10587 }
10588 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10589 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10590 {
10591 // Second instruction of a global dynamic sequence,
10592 // the __tls_get_addr call
e3deeb9c 10593 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10594 const bool final = gsym == NULL || gsym->final_value_is_known();
10595 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10596 if (tls_type != tls::TLSOPT_NONE)
10597 {
10598 if (tls_type == tls::TLSOPT_TO_IE)
10599 {
10600 Insn* iview = reinterpret_cast<Insn*>(view);
10601 Insn insn = add_3_3_13;
10602 if (size == 32)
10603 insn = add_3_3_2;
10604 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10605 r_type = elfcpp::R_POWERPC_NONE;
10606 }
10607 else
10608 {
89c52ae3
AM
10609 bool is_pcrel = false;
10610 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10611 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10612 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10613 if (relnum < reloc_count - 1)
10614 {
10615 Reltype next_rela(preloc + reloc_size);
10616 unsigned int r_type2
10617 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10618 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10619 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10620 && next_rela.get_r_offset() == rela.get_r_offset())
10621 is_pcrel = true;
10622 }
dd93cd0a 10623 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10624 if (is_pcrel)
10625 {
10626 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10627 r_type = elfcpp::R_POWERPC_NONE;
10628 }
10629 else
10630 {
10631 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10632 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10633 view += d_offset;
10634 value = psymval->value(object, rela.get_r_addend());
10635 }
dd93cd0a 10636 }
e3deeb9c 10637 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10638 }
10639 }
10640 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10641 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10642 {
10643 // Second instruction of a local dynamic sequence,
10644 // the __tls_get_addr call
e3deeb9c 10645 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
10646 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10647 if (tls_type == tls::TLSOPT_TO_LE)
10648 {
89c52ae3
AM
10649 bool is_pcrel = false;
10650 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10651 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10652 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10653 if (relnum < reloc_count - 1)
10654 {
10655 Reltype next_rela(preloc + reloc_size);
10656 unsigned int r_type2
10657 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10658 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10659 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10660 && next_rela.get_r_offset() == rela.get_r_offset())
10661 is_pcrel = true;
10662 }
dd93cd0a 10663 Insn* iview = reinterpret_cast<Insn*>(view);
89c52ae3
AM
10664 if (is_pcrel)
10665 {
10666 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10667 r_type = elfcpp::R_POWERPC_NONE;
10668 }
10669 else
10670 {
10671 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10672 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10673 view += d_offset;
10674 value = dtp_offset;
10675 }
e3deeb9c 10676 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
10677 }
10678 }
10679 else if (r_type == elfcpp::R_POWERPC_TLS)
10680 {
10681 // Second instruction of an initial exec sequence
10682 const bool final = gsym == NULL || gsym->final_value_is_known();
10683 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10684 if (tls_type == tls::TLSOPT_TO_LE)
10685 {
89c52ae3
AM
10686 Address roff = rela.get_r_offset() & 3;
10687 Insn* iview = reinterpret_cast<Insn*>(view - roff);
dd93cd0a
AM
10688 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10689 unsigned int reg = size == 32 ? 2 : 13;
10690 insn = at_tls_transform(insn, reg);
10691 gold_assert(insn != 0);
89c52ae3
AM
10692 if (roff == 0)
10693 {
10694 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10695 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10696 view += d_offset;
10697 value = psymval->value(object, rela.get_r_addend());
10698 }
10699 else if (roff == 1)
10700 {
10701 // For pcrel IE to LE we already have the full offset
10702 // and thus don't need an addi here. A nop or mr will do.
10703 if ((insn & (0x3f << 26)) == 14 << 26)
10704 {
10705 // Extract regs from addi rt,ra,si.
10706 unsigned int rt = (insn >> 21) & 0x1f;
10707 unsigned int ra = (insn >> 16) & 0x1f;
10708 if (rt == ra)
10709 insn = nop;
10710 else
10711 {
10712 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
10713 insn = (rt << 16) | (ra << 21) | (ra << 11);
10714 insn |= (31u << 26) | (444u << 1);
10715 }
10716 }
10717 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10718 r_type = elfcpp::R_POWERPC_NONE;
10719 }
dd93cd0a
AM
10720 }
10721 }
0cfb0717 10722 else if (!has_stub_value)
cf43a2fe 10723 {
32f59844
AM
10724 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
10725 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
23cedd1d
AM
10726 {
10727 // PLTCALL without plt entry => convert to direct call
10728 Insn* iview = reinterpret_cast<Insn*>(view);
10729 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10730 insn = (insn & 1) | b;
10731 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10732 if (size == 32)
10733 r_type = elfcpp::R_PPC_PLTREL24;
32f59844
AM
10734 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10735 r_type = elfcpp::R_PPC64_REL24_NOTOC;
23cedd1d
AM
10736 else
10737 r_type = elfcpp::R_POWERPC_REL24;
10738 }
dd93cd0a 10739 Address addend = 0;
08be3224
AM
10740 if (!(size == 32
10741 && (r_type == elfcpp::R_PPC_PLTREL24
10742 || r_type == elfcpp::R_POWERPC_PLT16_LO
10743 || r_type == elfcpp::R_POWERPC_PLT16_HI
10744 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
cf43a2fe 10745 addend = rela.get_r_addend();
c9824451 10746 value = psymval->value(object, addend);
32f59844 10747 if (size == 64 && is_branch_reloc<size>(r_type))
9055360d
AM
10748 {
10749 if (target->abiversion() >= 2)
10750 {
10751 if (gsym != NULL)
10752 value += object->ppc64_local_entry_offset(gsym);
10753 else
10754 value += object->ppc64_local_entry_offset(r_sym);
10755 }
10756 else
1611bc4a
AM
10757 {
10758 unsigned int dest_shndx;
10759 target->symval_for_branch(relinfo->symtab, gsym, object,
10760 &value, &dest_shndx);
10761 }
9055360d 10762 }
32f59844 10763 Address max_branch_offset = max_branch_delta<size>(r_type);
ec661b9d 10764 if (max_branch_offset != 0
32f59844
AM
10765 && (value - address + max_branch_offset >= 2 * max_branch_offset
10766 || (size == 64
10767 && r_type == elfcpp::R_PPC64_REL24_NOTOC
10768 && (gsym != NULL
10769 ? object->ppc64_needs_toc(gsym)
10770 : object->ppc64_needs_toc(r_sym)))))
ec661b9d
AM
10771 {
10772 Stub_table<size, big_endian>* stub_table
10773 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
10774 if (stub_table != NULL)
10775 {
32f59844
AM
10776 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
10777 = stub_table->find_long_branch_entry(object, value);
10778 if (ent != NULL)
0cfb0717 10779 {
32f59844
AM
10780 if (ent->save_res_)
10781 value = (value - target->savres_section()->address()
10782 + stub_table->branch_size());
10783 else
10784 value = (stub_table->stub_address() + stub_table->plt_size()
10785 + ent->off_);
0cfb0717
AM
10786 has_stub_value = true;
10787 }
0cfdc767 10788 }
ec661b9d 10789 }
42cacb20
DE
10790 }
10791
42cacb20
DE
10792 switch (r_type)
10793 {
32f59844
AM
10794 case elfcpp::R_PPC64_REL24_NOTOC:
10795 if (size == 32)
10796 break;
10797 // Fall through.
dd93cd0a
AM
10798 case elfcpp::R_PPC64_REL64:
10799 case elfcpp::R_POWERPC_REL32:
10800 case elfcpp::R_POWERPC_REL24:
10801 case elfcpp::R_PPC_PLTREL24:
10802 case elfcpp::R_PPC_LOCAL24PC:
10803 case elfcpp::R_POWERPC_REL16:
10804 case elfcpp::R_POWERPC_REL16_LO:
10805 case elfcpp::R_POWERPC_REL16_HI:
10806 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 10807 case elfcpp::R_POWERPC_REL16DX_HA:
c432bbba
AM
10808 case elfcpp::R_PPC64_REL16_HIGH:
10809 case elfcpp::R_PPC64_REL16_HIGHA:
10810 case elfcpp::R_PPC64_REL16_HIGHER:
10811 case elfcpp::R_PPC64_REL16_HIGHERA:
10812 case elfcpp::R_PPC64_REL16_HIGHEST:
10813 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
10814 case elfcpp::R_POWERPC_REL14:
10815 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10816 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
10817 case elfcpp::R_PPC64_PCREL34:
10818 case elfcpp::R_PPC64_GOT_PCREL34:
10819 case elfcpp::R_PPC64_PLT_PCREL34:
10820 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
10821 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
10822 case elfcpp::R_PPC64_GOT_TLSGD34:
10823 case elfcpp::R_PPC64_GOT_TLSLD34:
10824 case elfcpp::R_PPC64_GOT_TPREL34:
10825 case elfcpp::R_PPC64_GOT_DTPREL34:
e4dff765
AM
10826 case elfcpp::R_PPC64_REL16_HIGHER34:
10827 case elfcpp::R_PPC64_REL16_HIGHERA34:
10828 case elfcpp::R_PPC64_REL16_HIGHEST34:
10829 case elfcpp::R_PPC64_REL16_HIGHESTA34:
dd93cd0a
AM
10830 value -= address;
10831 break;
10832
42cacb20
DE
10833 case elfcpp::R_PPC64_TOC16:
10834 case elfcpp::R_PPC64_TOC16_LO:
10835 case elfcpp::R_PPC64_TOC16_HI:
10836 case elfcpp::R_PPC64_TOC16_HA:
10837 case elfcpp::R_PPC64_TOC16_DS:
10838 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 10839 // Subtract the TOC base address.
c9269dff 10840 value -= (target->got_section()->output_section()->address()
dd93cd0a 10841 + object->toc_base_offset());
42cacb20
DE
10842 break;
10843
cf43a2fe
AM
10844 case elfcpp::R_POWERPC_SECTOFF:
10845 case elfcpp::R_POWERPC_SECTOFF_LO:
10846 case elfcpp::R_POWERPC_SECTOFF_HI:
10847 case elfcpp::R_POWERPC_SECTOFF_HA:
10848 case elfcpp::R_PPC64_SECTOFF_DS:
10849 case elfcpp::R_PPC64_SECTOFF_LO_DS:
10850 if (os != NULL)
10851 value -= os->address();
42cacb20
DE
10852 break;
10853
dd93cd0a
AM
10854 case elfcpp::R_PPC64_TPREL16_DS:
10855 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
10856 case elfcpp::R_PPC64_TPREL16_HIGH:
10857 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 10858 if (size != 64)
f9c6b907 10859 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 10860 break;
d8e90251 10861 // Fall through.
dd93cd0a
AM
10862 case elfcpp::R_POWERPC_TPREL16:
10863 case elfcpp::R_POWERPC_TPREL16_LO:
10864 case elfcpp::R_POWERPC_TPREL16_HI:
10865 case elfcpp::R_POWERPC_TPREL16_HA:
10866 case elfcpp::R_POWERPC_TPREL:
10867 case elfcpp::R_PPC64_TPREL16_HIGHER:
10868 case elfcpp::R_PPC64_TPREL16_HIGHERA:
10869 case elfcpp::R_PPC64_TPREL16_HIGHEST:
10870 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
89c52ae3 10871 case elfcpp::R_PPC64_TPREL34:
dd93cd0a
AM
10872 // tls symbol values are relative to tls_segment()->vaddr()
10873 value -= tp_offset;
10874 break;
10875
10876 case elfcpp::R_PPC64_DTPREL16_DS:
10877 case elfcpp::R_PPC64_DTPREL16_LO_DS:
10878 case elfcpp::R_PPC64_DTPREL16_HIGHER:
10879 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10880 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10881 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10882 if (size != 64)
10883 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
10884 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
10885 break;
d8e90251 10886 // Fall through.
dd93cd0a
AM
10887 case elfcpp::R_POWERPC_DTPREL16:
10888 case elfcpp::R_POWERPC_DTPREL16_LO:
10889 case elfcpp::R_POWERPC_DTPREL16_HI:
10890 case elfcpp::R_POWERPC_DTPREL16_HA:
10891 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
10892 case elfcpp::R_PPC64_DTPREL16_HIGH:
10893 case elfcpp::R_PPC64_DTPREL16_HIGHA:
89c52ae3 10894 case elfcpp::R_PPC64_DTPREL34:
dd93cd0a
AM
10895 // tls symbol values are relative to tls_segment()->vaddr()
10896 value -= dtp_offset;
10897 break;
10898
45965137
AM
10899 case elfcpp::R_PPC64_ADDR64_LOCAL:
10900 if (gsym != NULL)
10901 value += object->ppc64_local_entry_offset(gsym);
10902 else
10903 value += object->ppc64_local_entry_offset(r_sym);
10904 break;
10905
42cacb20
DE
10906 default:
10907 break;
10908 }
10909
dd93cd0a 10910 Insn branch_bit = 0;
42cacb20
DE
10911 switch (r_type)
10912 {
dd93cd0a
AM
10913 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10914 case elfcpp::R_POWERPC_REL14_BRTAKEN:
10915 branch_bit = 1 << 21;
d8e90251 10916 // Fall through.
dd93cd0a
AM
10917 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10918 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10919 {
10920 Insn* iview = reinterpret_cast<Insn*>(view);
10921 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10922 insn &= ~(1 << 21);
10923 insn |= branch_bit;
10924 if (this->is_isa_v2)
10925 {
10926 // Set 'a' bit. This is 0b00010 in BO field for branch
10927 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
10928 // for branch on CTR insns (BO == 1a00t or 1a01t).
10929 if ((insn & (0x14 << 21)) == (0x04 << 21))
10930 insn |= 0x02 << 21;
10931 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10932 insn |= 0x08 << 21;
10933 else
10934 break;
10935 }
10936 else
10937 {
10938 // Invert 'y' bit if not the default.
10939 if (static_cast<Signed_address>(value) < 0)
10940 insn ^= 1 << 21;
10941 }
10942 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10943 }
10944 break;
10945
08be3224
AM
10946 case elfcpp::R_POWERPC_PLT16_HA:
10947 if (size == 32
10948 && !parameters->options().output_is_position_independent())
10949 {
10950 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10951 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10952
10953 // Convert addis to lis.
10954 if ((insn & (0x3f << 26)) == 15u << 26
10955 && (insn & (0x1f << 16)) != 0)
10956 {
10957 insn &= ~(0x1f << 16);
10958 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10959 }
10960 }
10961 break;
10962
dd93cd0a
AM
10963 default:
10964 break;
10965 }
10966
93b9bf16
AM
10967 if (gsym
10968 ? relative_value_is_known(gsym)
10969 : relative_value_is_known(psymval))
aba6bc71 10970 {
0c951c25
AM
10971 Insn* iview;
10972 Insn* iview2;
10973 Insn insn;
10974 uint64_t pinsn, pinsn2;
10975
aba6bc71
AM
10976 switch (r_type)
10977 {
10978 default:
10979 break;
10980
5edad15d
AM
10981 // Multi-instruction sequences that access the GOT/TOC can
10982 // be optimized, eg.
10983 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
10984 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
10985 // and
10986 // addis ra,r2,0; addi rb,ra,x@toc@l;
10987 // to nop; addi rb,r2,x@toc;
aba6bc71
AM
10988 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10989 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10990 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10991 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10992 case elfcpp::R_POWERPC_GOT16_HA:
10993 case elfcpp::R_PPC64_TOC16_HA:
93b9bf16 10994 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 10995 {
0c951c25
AM
10996 iview = reinterpret_cast<Insn*>(view - d_offset);
10997 insn = elfcpp::Swap<32, big_endian>::readval(iview);
c9b8abb7
AM
10998 if ((r_type == elfcpp::R_PPC64_TOC16_HA
10999 && object->make_toc_relative(target, &value))
11000 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11001 && object->make_got_relative(target, psymval,
11002 rela.get_r_addend(),
11003 &value)))
5edad15d
AM
11004 {
11005 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11006 == ((15u << 26) | (2 << 16)));
11007 }
11008 if (((insn & ((0x3f << 26) | 0x1f << 16))
11009 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11010 && value + 0x8000 < 0x10000)
aba6bc71
AM
11011 {
11012 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11013 return true;
11014 }
11015 }
11016 break;
11017
11018 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11019 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11020 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11021 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11022 case elfcpp::R_POWERPC_GOT16_LO:
11023 case elfcpp::R_PPC64_GOT16_LO_DS:
11024 case elfcpp::R_PPC64_TOC16_LO:
11025 case elfcpp::R_PPC64_TOC16_LO_DS:
93b9bf16 11026 if (size == 64 && parameters->options().toc_optimize())
aba6bc71 11027 {
0c951c25
AM
11028 iview = reinterpret_cast<Insn*>(view - d_offset);
11029 insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d 11030 bool changed = false;
c9b8abb7
AM
11031 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11032 && object->make_toc_relative(target, &value))
11033 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11034 && object->make_got_relative(target, psymval,
11035 rela.get_r_addend(),
11036 &value)))
5edad15d
AM
11037 {
11038 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11039 insn ^= (14u << 26) ^ (58u << 26);
11040 r_type = elfcpp::R_PPC64_TOC16_LO;
11041 changed = true;
11042 }
11043 if (ok_lo_toc_insn(insn, r_type)
11044 && value + 0x8000 < 0x10000)
aba6bc71
AM
11045 {
11046 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11047 {
11048 // Transform addic to addi when we change reg.
11049 insn &= ~((0x3f << 26) | (0x1f << 16));
11050 insn |= (14u << 26) | (2 << 16);
11051 }
11052 else
11053 {
11054 insn &= ~(0x1f << 16);
11055 insn |= 2 << 16;
11056 }
5edad15d 11057 changed = true;
aba6bc71 11058 }
5edad15d
AM
11059 if (changed)
11060 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
11061 }
11062 break;
549dba71 11063
c9b8abb7 11064 case elfcpp::R_PPC64_GOT_PCREL34:
93b9bf16 11065 if (size == 64 && parameters->options().toc_optimize())
c9b8abb7 11066 {
0c951c25
AM
11067 iview = reinterpret_cast<Insn*>(view);
11068 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11069 pinsn <<= 32;
11070 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11071 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
c9b8abb7
AM
11072 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11073 break;
11074
11075 Address relval = psymval->value(object, rela.get_r_addend());
11076 relval -= address;
11077 if (relval + (1ULL << 33) < 1ULL << 34)
11078 {
11079 value = relval;
11080 // Replace with paddi
0c951c25
AM
11081 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11082 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
c9b8abb7 11083 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
0c951c25
AM
11084 pinsn & 0xffffffff);
11085 goto pcrelopt;
c9b8abb7
AM
11086 }
11087 }
11088 break;
11089
0c951c25 11090 case elfcpp::R_PPC64_PCREL34:
93b9bf16
AM
11091 if (size == 64)
11092 {
11093 iview = reinterpret_cast<Insn*>(view);
11094 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11095 pinsn <<= 32;
11096 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11097 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11098 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11099 | (14ULL << 26) /* paddi */))
11100 break;
0c951c25 11101
93b9bf16
AM
11102 pcrelopt:
11103 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11104 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11105 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11106 if (relnum >= reloc_count - 1)
11107 break;
0c951c25 11108
93b9bf16
AM
11109 Reltype next_rela(preloc + reloc_size);
11110 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11111 != elfcpp::R_PPC64_PCREL_OPT)
11112 || next_rela.get_r_offset() != rela.get_r_offset())
11113 break;
0c951c25 11114
93b9bf16
AM
11115 Address off = next_rela.get_r_addend();
11116 if (off == 0)
11117 off = 8; // zero means next insn.
11118 if (off + rela.get_r_offset() + 4 > view_size)
11119 break;
0c951c25 11120
93b9bf16
AM
11121 iview2 = reinterpret_cast<Insn*>(view + off);
11122 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11123 pinsn2 <<= 32;
11124 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11125 break;
11126 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11127 {
11128 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11129 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11130 pinsn & 0xffffffff);
11131 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11132 }
11133 }
0c951c25
AM
11134 break;
11135
9a23f96e 11136 case elfcpp::R_POWERPC_TPREL16_HA:
93b9bf16 11137 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11138 {
11139 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
93b9bf16
AM
11140 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11141 return true;
9a23f96e
AM
11142 }
11143 break;
11144
11145 case elfcpp::R_PPC64_TPREL16_LO_DS:
11146 if (size == 32)
11147 // R_PPC_TLSGD, R_PPC_TLSLD
11148 break;
11149 // Fall through.
11150 case elfcpp::R_POWERPC_TPREL16_LO:
93b9bf16 11151 if (target->tprel_opt() && value + 0x8000 < 0x10000)
9a23f96e
AM
11152 {
11153 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11154 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11155 insn &= ~(0x1f << 16);
11156 insn |= (size == 32 ? 2 : 13) << 16;
11157 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11158 }
11159 break;
11160
549dba71 11161 case elfcpp::R_PPC64_ENTRY:
93b9bf16 11162 if (size == 64)
549dba71 11163 {
93b9bf16
AM
11164 value = (target->got_section()->output_section()->address()
11165 + object->toc_base_offset());
11166 if (value + 0x80008000 <= 0xffffffff
11167 && !parameters->options().output_is_position_independent())
549dba71
AM
11168 {
11169 Insn* iview = reinterpret_cast<Insn*>(view);
11170 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11171 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11172
11173 if ((insn1 & ~0xfffc) == ld_2_12
11174 && insn2 == add_2_2_12)
11175 {
93b9bf16 11176 insn1 = lis_2 + ha(value);
549dba71
AM
11177 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11178 insn2 = addi_2_2 + l(value);
11179 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11180 return true;
11181 }
11182 }
93b9bf16
AM
11183 else
11184 {
11185 value -= address;
11186 if (value + 0x80008000 <= 0xffffffff)
11187 {
11188 Insn* iview = reinterpret_cast<Insn*>(view);
11189 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11190 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11191
11192 if ((insn1 & ~0xfffc) == ld_2_12
11193 && insn2 == add_2_2_12)
11194 {
11195 insn1 = addis_2_12 + ha(value);
11196 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11197 insn2 = addi_2_2 + l(value);
11198 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11199 return true;
11200 }
11201 }
11202 }
549dba71
AM
11203 }
11204 break;
e3a7574e
AM
11205
11206 case elfcpp::R_POWERPC_REL16_LO:
11207 // If we are generating a non-PIC executable, edit
11208 // 0: addis 2,12,.TOC.-0b@ha
11209 // addi 2,2,.TOC.-0b@l
11210 // used by ELFv2 global entry points to set up r2, to
11211 // lis 2,.TOC.@ha
11212 // addi 2,2,.TOC.@l
11213 // if .TOC. is in range. */
93b9bf16
AM
11214 if (size == 64
11215 && value + address - 4 + 0x80008000 <= 0xffffffff
f60c61e6 11216 && relnum + 1 > 1
e3a7574e
AM
11217 && preloc != NULL
11218 && target->abiversion() >= 2
11219 && !parameters->options().output_is_position_independent()
4f038ee5 11220 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
11221 && gsym != NULL
11222 && strcmp(gsym->name(), ".TOC.") == 0)
11223 {
0e123f69 11224 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
11225 Reltype prev_rela(preloc - reloc_size);
11226 if ((prev_rela.get_r_info()
11227 == elfcpp::elf_r_info<size>(r_sym,
11228 elfcpp::R_POWERPC_REL16_HA))
11229 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11230 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11231 {
dcfc7dd4 11232 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
11233 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11234 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11235
11236 if ((insn1 & 0xffff0000) == addis_2_12
11237 && (insn2 & 0xffff0000) == addi_2_2)
11238 {
11239 insn1 = lis_2 + ha(value + address - 4);
11240 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11241 insn2 = addi_2_2 + l(value + address - 4);
11242 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11243 if (relinfo->rr)
11244 {
11245 relinfo->rr->set_strategy(relnum - 1,
11246 Relocatable_relocs::RELOC_SPECIAL);
11247 relinfo->rr->set_strategy(relnum,
11248 Relocatable_relocs::RELOC_SPECIAL);
11249 }
11250 return true;
11251 }
11252 }
11253 }
11254 break;
aba6bc71
AM
11255 }
11256 }
11257
f4baf0d4 11258 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 11259 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
11260 switch (r_type)
11261 {
11262 case elfcpp::R_POWERPC_ADDR32:
11263 case elfcpp::R_POWERPC_UADDR32:
11264 if (size == 64)
f4baf0d4 11265 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11266 break;
11267
11268 case elfcpp::R_POWERPC_REL32:
a680de9a 11269 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 11270 if (size == 64)
f4baf0d4 11271 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
11272 break;
11273
dd93cd0a 11274 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 11275 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
11276 break;
11277
b80eed39
AM
11278 case elfcpp::R_POWERPC_ADDR16:
11279 // We really should have three separate relocations,
11280 // one for 16-bit data, one for insns with 16-bit signed fields,
11281 // and one for insns with 16-bit unsigned fields.
11282 overflow = Reloc::CHECK_BITFIELD;
11283 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11284 overflow = Reloc::CHECK_LOW_INSN;
11285 break;
11286
f9c6b907
AM
11287 case elfcpp::R_POWERPC_ADDR16_HI:
11288 case elfcpp::R_POWERPC_ADDR16_HA:
11289 case elfcpp::R_POWERPC_GOT16_HI:
11290 case elfcpp::R_POWERPC_GOT16_HA:
11291 case elfcpp::R_POWERPC_PLT16_HI:
11292 case elfcpp::R_POWERPC_PLT16_HA:
11293 case elfcpp::R_POWERPC_SECTOFF_HI:
11294 case elfcpp::R_POWERPC_SECTOFF_HA:
11295 case elfcpp::R_PPC64_TOC16_HI:
11296 case elfcpp::R_PPC64_TOC16_HA:
11297 case elfcpp::R_PPC64_PLTGOT16_HI:
11298 case elfcpp::R_PPC64_PLTGOT16_HA:
11299 case elfcpp::R_POWERPC_TPREL16_HI:
11300 case elfcpp::R_POWERPC_TPREL16_HA:
11301 case elfcpp::R_POWERPC_DTPREL16_HI:
11302 case elfcpp::R_POWERPC_DTPREL16_HA:
11303 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11304 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11305 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11306 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11307 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11308 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11309 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11310 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11311 case elfcpp::R_POWERPC_REL16_HI:
11312 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
11313 if (size != 32)
11314 overflow = Reloc::CHECK_HIGH_INSN;
11315 break;
11316
dd93cd0a
AM
11317 case elfcpp::R_POWERPC_REL16:
11318 case elfcpp::R_PPC64_TOC16:
11319 case elfcpp::R_POWERPC_GOT16:
11320 case elfcpp::R_POWERPC_SECTOFF:
11321 case elfcpp::R_POWERPC_TPREL16:
11322 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
11323 case elfcpp::R_POWERPC_GOT_TLSGD16:
11324 case elfcpp::R_POWERPC_GOT_TLSLD16:
11325 case elfcpp::R_POWERPC_GOT_TPREL16:
11326 case elfcpp::R_POWERPC_GOT_DTPREL16:
11327 overflow = Reloc::CHECK_LOW_INSN;
11328 break;
11329
32f59844
AM
11330 case elfcpp::R_PPC64_REL24_NOTOC:
11331 if (size == 32)
11332 break;
11333 // Fall through.
b80eed39
AM
11334 case elfcpp::R_POWERPC_ADDR24:
11335 case elfcpp::R_POWERPC_ADDR14:
11336 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11337 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11338 case elfcpp::R_PPC64_ADDR16_DS:
11339 case elfcpp::R_POWERPC_REL24:
11340 case elfcpp::R_PPC_PLTREL24:
11341 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
11342 case elfcpp::R_PPC64_TPREL16_DS:
11343 case elfcpp::R_PPC64_DTPREL16_DS:
11344 case elfcpp::R_PPC64_TOC16_DS:
11345 case elfcpp::R_PPC64_GOT16_DS:
11346 case elfcpp::R_PPC64_SECTOFF_DS:
11347 case elfcpp::R_POWERPC_REL14:
11348 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11349 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
e4dff765
AM
11350 case elfcpp::R_PPC64_D34:
11351 case elfcpp::R_PPC64_PCREL34:
11352 case elfcpp::R_PPC64_GOT_PCREL34:
11353 case elfcpp::R_PPC64_PLT_PCREL34:
11354 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11355 case elfcpp::R_PPC64_D28:
11356 case elfcpp::R_PPC64_PCREL28:
89c52ae3
AM
11357 case elfcpp::R_PPC64_TPREL34:
11358 case elfcpp::R_PPC64_DTPREL34:
11359 case elfcpp::R_PPC64_GOT_TLSGD34:
11360 case elfcpp::R_PPC64_GOT_TLSLD34:
11361 case elfcpp::R_PPC64_GOT_TPREL34:
11362 case elfcpp::R_PPC64_GOT_DTPREL34:
f4baf0d4 11363 overflow = Reloc::CHECK_SIGNED;
42cacb20 11364 break;
dd93cd0a 11365 }
42cacb20 11366
dcfc7dd4 11367 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
11368 Insn insn = 0;
11369
b80eed39
AM
11370 if (overflow == Reloc::CHECK_LOW_INSN
11371 || overflow == Reloc::CHECK_HIGH_INSN)
11372 {
a680de9a 11373 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 11374
a47622ac
AM
11375 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11376 overflow = Reloc::CHECK_BITFIELD;
11377 else if (overflow == Reloc::CHECK_LOW_INSN
11378 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11379 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11380 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11381 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11382 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11383 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 11384 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
11385 else
11386 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
11387 }
11388
a680de9a 11389 bool maybe_dq_reloc = false;
3ea0a085 11390 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 11391 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
11392 switch (r_type)
11393 {
11394 case elfcpp::R_POWERPC_NONE:
11395 case elfcpp::R_POWERPC_TLS:
11396 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11397 case elfcpp::R_POWERPC_GNU_VTENTRY:
23cedd1d
AM
11398 case elfcpp::R_POWERPC_PLTSEQ:
11399 case elfcpp::R_POWERPC_PLTCALL:
32f59844
AM
11400 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11401 case elfcpp::R_PPC64_PLTCALL_NOTOC:
e4dff765 11402 case elfcpp::R_PPC64_PCREL_OPT:
42cacb20
DE
11403 break;
11404
11405 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 11406 case elfcpp::R_PPC64_REL64:
cf43a2fe 11407 case elfcpp::R_PPC64_TOC:
45965137 11408 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
11409 Reloc::addr64(view, value);
11410 break;
11411
11412 case elfcpp::R_POWERPC_TPREL:
11413 case elfcpp::R_POWERPC_DTPREL:
11414 if (size == 64)
11415 Reloc::addr64(view, value);
11416 else
3ea0a085 11417 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11418 break;
11419
11420 case elfcpp::R_PPC64_UADDR64:
11421 Reloc::addr64_u(view, value);
42cacb20
DE
11422 break;
11423
11424 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 11425 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
11426 break;
11427
acc276d8 11428 case elfcpp::R_POWERPC_REL32:
dd93cd0a 11429 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 11430 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
11431 break;
11432
32f59844
AM
11433 case elfcpp::R_PPC64_REL24_NOTOC:
11434 if (size == 32)
11435 goto unsupp; // R_PPC_EMB_RELSDA
11436 // Fall through.
dd93cd0a
AM
11437 case elfcpp::R_POWERPC_ADDR24:
11438 case elfcpp::R_POWERPC_REL24:
11439 case elfcpp::R_PPC_PLTREL24:
11440 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 11441 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
11442 break;
11443
dd93cd0a
AM
11444 case elfcpp::R_POWERPC_GOT_DTPREL16:
11445 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
11446 case elfcpp::R_POWERPC_GOT_TPREL16:
11447 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
11448 if (size == 64)
11449 {
ec86f434 11450 // On ppc64 these are all ds form
a680de9a 11451 maybe_dq_reloc = true;
dd93cd0a
AM
11452 break;
11453 }
c25aa1e1 11454 // Fall through.
cf43a2fe 11455 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 11456 case elfcpp::R_POWERPC_REL16:
cf43a2fe 11457 case elfcpp::R_PPC64_TOC16:
42cacb20 11458 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 11459 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
11460 case elfcpp::R_POWERPC_TPREL16:
11461 case elfcpp::R_POWERPC_DTPREL16:
11462 case elfcpp::R_POWERPC_GOT_TLSGD16:
11463 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 11464 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 11465 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 11466 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 11467 case elfcpp::R_POWERPC_GOT16_LO:
08be3224 11468 case elfcpp::R_POWERPC_PLT16_LO:
cf43a2fe 11469 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
11470 case elfcpp::R_POWERPC_TPREL16_LO:
11471 case elfcpp::R_POWERPC_DTPREL16_LO:
11472 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11473 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
11474 if (size == 64)
11475 status = Reloc::addr16(view, value, overflow);
11476 else
11477 maybe_dq_reloc = true;
dd93cd0a
AM
11478 break;
11479
11480 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 11481 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
11482 break;
11483
f9c6b907
AM
11484 case elfcpp::R_PPC64_ADDR16_HIGH:
11485 case elfcpp::R_PPC64_TPREL16_HIGH:
11486 case elfcpp::R_PPC64_DTPREL16_HIGH:
11487 if (size == 32)
11488 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11489 goto unsupp;
d8e90251 11490 // Fall through.
cf43a2fe 11491 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 11492 case elfcpp::R_POWERPC_REL16_HI:
c432bbba 11493 case elfcpp::R_PPC64_REL16_HIGH:
cf43a2fe 11494 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 11495 case elfcpp::R_POWERPC_GOT16_HI:
08be3224 11496 case elfcpp::R_POWERPC_PLT16_HI:
cf43a2fe 11497 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
11498 case elfcpp::R_POWERPC_TPREL16_HI:
11499 case elfcpp::R_POWERPC_DTPREL16_HI:
11500 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11501 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11502 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11503 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11504 Reloc::addr16_hi(view, value);
42cacb20
DE
11505 break;
11506
f9c6b907
AM
11507 case elfcpp::R_PPC64_ADDR16_HIGHA:
11508 case elfcpp::R_PPC64_TPREL16_HIGHA:
11509 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11510 if (size == 32)
11511 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11512 goto unsupp;
d8e90251 11513 // Fall through.
cf43a2fe 11514 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 11515 case elfcpp::R_POWERPC_REL16_HA:
c432bbba 11516 case elfcpp::R_PPC64_REL16_HIGHA:
cf43a2fe 11517 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 11518 case elfcpp::R_POWERPC_GOT16_HA:
08be3224 11519 case elfcpp::R_POWERPC_PLT16_HA:
cf43a2fe 11520 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
11521 case elfcpp::R_POWERPC_TPREL16_HA:
11522 case elfcpp::R_POWERPC_DTPREL16_HA:
11523 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11524 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11525 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11526 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11527 Reloc::addr16_ha(view, value);
42cacb20
DE
11528 break;
11529
a680de9a
PB
11530 case elfcpp::R_POWERPC_REL16DX_HA:
11531 status = Reloc::addr16dx_ha(view, value, overflow);
11532 break;
11533
dd93cd0a
AM
11534 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11535 if (size == 32)
11536 // R_PPC_EMB_NADDR16_LO
11537 goto unsupp;
d8e90251 11538 // Fall through.
dd93cd0a 11539 case elfcpp::R_PPC64_ADDR16_HIGHER:
c432bbba 11540 case elfcpp::R_PPC64_REL16_HIGHER:
dd93cd0a
AM
11541 case elfcpp::R_PPC64_TPREL16_HIGHER:
11542 Reloc::addr16_hi2(view, value);
42cacb20
DE
11543 break;
11544
dd93cd0a
AM
11545 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11546 if (size == 32)
11547 // R_PPC_EMB_NADDR16_HI
11548 goto unsupp;
d8e90251 11549 // Fall through.
dd93cd0a 11550 case elfcpp::R_PPC64_ADDR16_HIGHERA:
c432bbba 11551 case elfcpp::R_PPC64_REL16_HIGHERA:
dd93cd0a
AM
11552 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11553 Reloc::addr16_ha2(view, value);
42cacb20
DE
11554 break;
11555
dd93cd0a
AM
11556 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11557 if (size == 32)
11558 // R_PPC_EMB_NADDR16_HA
11559 goto unsupp;
d8e90251 11560 // Fall through.
dd93cd0a 11561 case elfcpp::R_PPC64_ADDR16_HIGHEST:
c432bbba 11562 case elfcpp::R_PPC64_REL16_HIGHEST:
dd93cd0a
AM
11563 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11564 Reloc::addr16_hi3(view, value);
42cacb20
DE
11565 break;
11566
dd93cd0a
AM
11567 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11568 if (size == 32)
11569 // R_PPC_EMB_SDAI16
11570 goto unsupp;
d8e90251 11571 // Fall through.
dd93cd0a 11572 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
c432bbba 11573 case elfcpp::R_PPC64_REL16_HIGHESTA:
dd93cd0a
AM
11574 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11575 Reloc::addr16_ha3(view, value);
11576 break;
11577
11578 case elfcpp::R_PPC64_DTPREL16_DS:
11579 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11580 if (size == 32)
11581 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11582 goto unsupp;
d8e90251 11583 // Fall through.
dd93cd0a
AM
11584 case elfcpp::R_PPC64_TPREL16_DS:
11585 case elfcpp::R_PPC64_TPREL16_LO_DS:
11586 if (size == 32)
11587 // R_PPC_TLSGD, R_PPC_TLSLD
11588 break;
d8e90251 11589 // Fall through.
cf43a2fe
AM
11590 case elfcpp::R_PPC64_ADDR16_DS:
11591 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
11592 case elfcpp::R_PPC64_TOC16_DS:
11593 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
11594 case elfcpp::R_PPC64_GOT16_DS:
11595 case elfcpp::R_PPC64_GOT16_LO_DS:
08be3224 11596 case elfcpp::R_PPC64_PLT16_LO_DS:
cf43a2fe
AM
11597 case elfcpp::R_PPC64_SECTOFF_DS:
11598 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 11599 maybe_dq_reloc = true;
dd93cd0a
AM
11600 break;
11601
11602 case elfcpp::R_POWERPC_ADDR14:
11603 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11604 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11605 case elfcpp::R_POWERPC_REL14:
11606 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11607 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 11608 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
11609 break;
11610
11611 case elfcpp::R_POWERPC_COPY:
11612 case elfcpp::R_POWERPC_GLOB_DAT:
11613 case elfcpp::R_POWERPC_JMP_SLOT:
11614 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 11615 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
11616 case elfcpp::R_PPC64_JMP_IREL:
11617 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
11618 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11619 _("unexpected reloc %u in object file"),
11620 r_type);
11621 break;
11622
7e57d19e 11623 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 11624 if (size == 32)
7e57d19e 11625 // R_PPC_EMB_SDA21
dd93cd0a
AM
11626 goto unsupp;
11627 else
11628 {
7e57d19e
AM
11629 Symbol_location loc;
11630 loc.object = relinfo->object;
11631 loc.shndx = relinfo->data_shndx;
11632 loc.offset = rela.get_r_offset();
11633 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
11634 if (p != target->tocsave_loc().end())
11635 {
11636 // If we've generated plt calls using this tocsave, then
11637 // the nop needs to be changed to save r2.
11638 Insn* iview = reinterpret_cast<Insn*>(view);
11639 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11640 elfcpp::Swap<32, big_endian>::
11641 writeval(iview, std_2_1 + target->stk_toc());
11642 }
dd93cd0a
AM
11643 }
11644 break;
11645
11646 case elfcpp::R_PPC_EMB_SDA2I16:
11647 case elfcpp::R_PPC_EMB_SDA2REL:
11648 if (size == 32)
11649 goto unsupp;
11650 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
11651 break;
11652
e4dff765
AM
11653 case elfcpp::R_PPC64_D34:
11654 case elfcpp::R_PPC64_D34_LO:
11655 case elfcpp::R_PPC64_PCREL34:
11656 case elfcpp::R_PPC64_GOT_PCREL34:
11657 case elfcpp::R_PPC64_PLT_PCREL34:
11658 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
89c52ae3
AM
11659 case elfcpp::R_PPC64_TPREL34:
11660 case elfcpp::R_PPC64_DTPREL34:
11661 case elfcpp::R_PPC64_GOT_TLSGD34:
11662 case elfcpp::R_PPC64_GOT_TLSLD34:
11663 case elfcpp::R_PPC64_GOT_TPREL34:
11664 case elfcpp::R_PPC64_GOT_DTPREL34:
e4dff765
AM
11665 if (size == 32)
11666 goto unsupp;
11667 status = Reloc::addr34(view, value, overflow);
11668 break;
11669
11670 case elfcpp::R_PPC64_D34_HI30:
11671 if (size == 32)
11672 goto unsupp;
11673 Reloc::addr34_hi(view, value);
11674 break;
11675
11676 case elfcpp::R_PPC64_D34_HA30:
11677 if (size == 32)
11678 goto unsupp;
11679 Reloc::addr34_ha(view, value);
11680 break;
11681
11682 case elfcpp::R_PPC64_D28:
11683 case elfcpp::R_PPC64_PCREL28:
11684 if (size == 32)
11685 goto unsupp;
11686 status = Reloc::addr28(view, value, overflow);
11687 break;
11688
11689 case elfcpp::R_PPC64_ADDR16_HIGHER34:
11690 case elfcpp::R_PPC64_REL16_HIGHER34:
11691 if (size == 32)
11692 goto unsupp;
11693 Reloc::addr16_higher34(view, value);
11694 break;
11695
11696 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
11697 case elfcpp::R_PPC64_REL16_HIGHERA34:
11698 if (size == 32)
11699 goto unsupp;
11700 Reloc::addr16_highera34(view, value);
11701 break;
11702
11703 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
11704 case elfcpp::R_PPC64_REL16_HIGHEST34:
11705 if (size == 32)
11706 goto unsupp;
11707 Reloc::addr16_highest34(view, value);
11708 break;
11709
11710 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
11711 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11712 if (size == 32)
11713 goto unsupp;
11714 Reloc::addr16_highesta34(view, value);
11715 break;
11716
dd93cd0a
AM
11717 case elfcpp::R_POWERPC_PLT32:
11718 case elfcpp::R_POWERPC_PLTREL32:
dd93cd0a
AM
11719 case elfcpp::R_PPC_SDAREL16:
11720 case elfcpp::R_POWERPC_ADDR30:
11721 case elfcpp::R_PPC64_PLT64:
11722 case elfcpp::R_PPC64_PLTREL64:
11723 case elfcpp::R_PPC64_PLTGOT16:
11724 case elfcpp::R_PPC64_PLTGOT16_LO:
11725 case elfcpp::R_PPC64_PLTGOT16_HI:
11726 case elfcpp::R_PPC64_PLTGOT16_HA:
dd93cd0a
AM
11727 case elfcpp::R_PPC64_PLTGOT16_DS:
11728 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a 11729 case elfcpp::R_PPC_TOC16:
42cacb20 11730 default:
dd93cd0a 11731 unsupp:
42cacb20
DE
11732 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11733 _("unsupported reloc %u"),
11734 r_type);
11735 break;
11736 }
a680de9a
PB
11737
11738 if (maybe_dq_reloc)
11739 {
11740 if (insn == 0)
11741 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11742
11743 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
11744 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
11745 && (insn & 3) == 1))
11746 status = Reloc::addr16_dq(view, value, overflow);
11747 else if (size == 64
11748 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
11749 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
11750 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
11751 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
11752 status = Reloc::addr16_ds(view, value, overflow);
11753 else
11754 status = Reloc::addr16(view, value, overflow);
11755 }
11756
0cfb0717 11757 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
11758 && (has_stub_value
11759 || !(gsym != NULL
282c9750 11760 && gsym->is_undefined()
32f59844 11761 && is_branch_reloc<size>(r_type))))
0cfb0717
AM
11762 {
11763 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11764 _("relocation overflow"));
11765 if (has_stub_value)
11766 gold_info(_("try relinking with a smaller --stub-group-size"));
11767 }
42cacb20
DE
11768
11769 return true;
11770}
11771
42cacb20
DE
11772// Relocate section data.
11773
11774template<int size, bool big_endian>
11775void
11776Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
11777 const Relocate_info<size, big_endian>* relinfo,
11778 unsigned int sh_type,
11779 const unsigned char* prelocs,
11780 size_t reloc_count,
11781 Output_section* output_section,
11782 bool needs_special_offset_handling,
11783 unsigned char* view,
c9269dff 11784 Address address,
d83ce4e3
AM
11785 section_size_type view_size,
11786 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
11787{
11788 typedef Target_powerpc<size, big_endian> Powerpc;
11789 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
11790 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
11791 Powerpc_comdat_behavior;
4d625b70
CC
11792 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11793 Classify_reloc;
42cacb20
DE
11794
11795 gold_assert(sh_type == elfcpp::SHT_RELA);
11796
4d625b70
CC
11797 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
11798 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
11799 relinfo,
11800 this,
11801 prelocs,
11802 reloc_count,
11803 output_section,
11804 needs_special_offset_handling,
11805 view,
11806 address,
364c7fa5
ILT
11807 view_size,
11808 reloc_symbol_changes);
42cacb20
DE
11809}
11810
4d625b70 11811template<int size, bool big_endian>
cf43a2fe 11812class Powerpc_scan_relocatable_reloc
42cacb20 11813{
cf43a2fe 11814public:
0e123f69
AM
11815 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11816 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
11817 static const int sh_type = elfcpp::SHT_RELA;
11818
11819 // Return the symbol referred to by the relocation.
11820 static inline unsigned int
11821 get_r_sym(const Reltype* reloc)
11822 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
11823
11824 // Return the type of the relocation.
11825 static inline unsigned int
11826 get_r_type(const Reltype* reloc)
11827 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
11828
cf43a2fe
AM
11829 // Return the strategy to use for a local symbol which is not a
11830 // section symbol, given the relocation type.
11831 inline Relocatable_relocs::Reloc_strategy
11832 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
11833 {
11834 if (r_type == 0 && r_sym == 0)
11835 return Relocatable_relocs::RELOC_DISCARD;
11836 return Relocatable_relocs::RELOC_COPY;
11837 }
11838
11839 // Return the strategy to use for a local symbol which is a section
11840 // symbol, given the relocation type.
11841 inline Relocatable_relocs::Reloc_strategy
11842 local_section_strategy(unsigned int, Relobj*)
11843 {
11844 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
11845 }
11846
11847 // Return the strategy to use for a global symbol, given the
11848 // relocation type, the object, and the symbol index.
11849 inline Relocatable_relocs::Reloc_strategy
11850 global_strategy(unsigned int r_type, Relobj*, unsigned int)
11851 {
08be3224
AM
11852 if (size == 32
11853 && (r_type == elfcpp::R_PPC_PLTREL24
11854 || r_type == elfcpp::R_POWERPC_PLT16_LO
11855 || r_type == elfcpp::R_POWERPC_PLT16_HI
11856 || r_type == elfcpp::R_POWERPC_PLT16_HA))
cf43a2fe
AM
11857 return Relocatable_relocs::RELOC_SPECIAL;
11858 return Relocatable_relocs::RELOC_COPY;
11859 }
11860};
42cacb20
DE
11861
11862// Scan the relocs during a relocatable link.
11863
11864template<int size, bool big_endian>
11865void
11866Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
11867 Symbol_table* symtab,
11868 Layout* layout,
11869 Sized_relobj_file<size, big_endian>* object,
11870 unsigned int data_shndx,
11871 unsigned int sh_type,
11872 const unsigned char* prelocs,
11873 size_t reloc_count,
11874 Output_section* output_section,
11875 bool needs_special_offset_handling,
11876 size_t local_symbol_count,
11877 const unsigned char* plocal_symbols,
11878 Relocatable_relocs* rr)
42cacb20 11879{
4d625b70
CC
11880 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
11881
42cacb20
DE
11882 gold_assert(sh_type == elfcpp::SHT_RELA);
11883
4d625b70 11884 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
11885 symtab,
11886 layout,
11887 object,
11888 data_shndx,
11889 prelocs,
11890 reloc_count,
11891 output_section,
11892 needs_special_offset_handling,
11893 local_symbol_count,
11894 plocal_symbols,
11895 rr);
11896}
11897
4d625b70
CC
11898// Scan the relocs for --emit-relocs.
11899
11900template<int size, bool big_endian>
11901void
11902Target_powerpc<size, big_endian>::emit_relocs_scan(
11903 Symbol_table* symtab,
11904 Layout* layout,
11905 Sized_relobj_file<size, big_endian>* object,
11906 unsigned int data_shndx,
11907 unsigned int sh_type,
11908 const unsigned char* prelocs,
11909 size_t reloc_count,
11910 Output_section* output_section,
11911 bool needs_special_offset_handling,
11912 size_t local_symbol_count,
11913 const unsigned char* plocal_syms,
11914 Relocatable_relocs* rr)
11915{
11916 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11917 Classify_reloc;
11918 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
11919 Emit_relocs_strategy;
11920
11921 gold_assert(sh_type == elfcpp::SHT_RELA);
11922
11923 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
11924 symtab,
11925 layout,
11926 object,
11927 data_shndx,
11928 prelocs,
11929 reloc_count,
11930 output_section,
11931 needs_special_offset_handling,
11932 local_symbol_count,
11933 plocal_syms,
11934 rr);
11935}
11936
7404fe1b 11937// Emit relocations for a section.
dd93cd0a
AM
11938// This is a modified version of the function by the same name in
11939// target-reloc.h. Using relocate_special_relocatable for
11940// R_PPC_PLTREL24 would require duplication of the entire body of the
11941// loop, so we may as well duplicate the whole thing.
42cacb20
DE
11942
11943template<int size, bool big_endian>
11944void
7404fe1b 11945Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
11946 const Relocate_info<size, big_endian>* relinfo,
11947 unsigned int sh_type,
11948 const unsigned char* prelocs,
11949 size_t reloc_count,
11950 Output_section* output_section,
62fe925a 11951 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 11952 unsigned char*,
dd93cd0a 11953 Address view_address,
cf43a2fe 11954 section_size_type,
42cacb20
DE
11955 unsigned char* reloc_view,
11956 section_size_type reloc_view_size)
11957{
11958 gold_assert(sh_type == elfcpp::SHT_RELA);
11959
0e123f69
AM
11960 typedef typename elfcpp::Rela<size, big_endian> Reltype;
11961 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
11962 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
11963 // Offset from start of insn to d-field reloc.
11964 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
11965
11966 Powerpc_relobj<size, big_endian>* const object
11967 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
11968 const unsigned int local_count = object->local_symbol_count();
11969 unsigned int got2_shndx = object->got2_shndx();
c9269dff 11970 Address got2_addend = 0;
cf43a2fe 11971 if (got2_shndx != 0)
c9269dff
AM
11972 {
11973 got2_addend = object->get_output_section_offset(got2_shndx);
11974 gold_assert(got2_addend != invalid_address);
11975 }
cf43a2fe 11976
033bfb73
CC
11977 const bool relocatable = parameters->options().relocatable();
11978
cf43a2fe 11979 unsigned char* pwrite = reloc_view;
7404fe1b 11980 bool zap_next = false;
cf43a2fe
AM
11981 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11982 {
91a65d2f 11983 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
11984 if (strategy == Relocatable_relocs::RELOC_DISCARD)
11985 continue;
11986
11987 Reltype reloc(prelocs);
11988 Reltype_write reloc_write(pwrite);
11989
7404fe1b 11990 Address offset = reloc.get_r_offset();
cf43a2fe 11991 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
11992 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
11993 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
11994 const unsigned int orig_r_sym = r_sym;
11995 typename elfcpp::Elf_types<size>::Elf_Swxword addend
11996 = reloc.get_r_addend();
11997 const Symbol* gsym = NULL;
11998
11999 if (zap_next)
12000 {
12001 // We could arrange to discard these and other relocs for
12002 // tls optimised sequences in the strategy methods, but for
12003 // now do as BFD ld does.
12004 r_type = elfcpp::R_POWERPC_NONE;
12005 zap_next = false;
12006 }
cf43a2fe
AM
12007
12008 // Get the new symbol index.
9215b98b 12009 Output_section* os = NULL;
cf43a2fe
AM
12010 if (r_sym < local_count)
12011 {
12012 switch (strategy)
12013 {
12014 case Relocatable_relocs::RELOC_COPY:
12015 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 12016 if (r_sym != 0)
dd93cd0a 12017 {
7404fe1b
AM
12018 r_sym = object->symtab_index(r_sym);
12019 gold_assert(r_sym != -1U);
dd93cd0a 12020 }
cf43a2fe
AM
12021 break;
12022
12023 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12024 {
12025 // We are adjusting a section symbol. We need to find
12026 // the symbol table index of the section symbol for
12027 // the output section corresponding to input section
12028 // in which this symbol is defined.
12029 gold_assert(r_sym < local_count);
12030 bool is_ordinary;
12031 unsigned int shndx =
12032 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12033 gold_assert(is_ordinary);
9215b98b 12034 os = object->output_section(shndx);
cf43a2fe
AM
12035 gold_assert(os != NULL);
12036 gold_assert(os->needs_symtab_index());
7404fe1b 12037 r_sym = os->symtab_index();
cf43a2fe
AM
12038 }
12039 break;
12040
12041 default:
12042 gold_unreachable();
12043 }
12044 }
12045 else
12046 {
7404fe1b 12047 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
12048 gold_assert(gsym != NULL);
12049 if (gsym->is_forwarder())
12050 gsym = relinfo->symtab->resolve_forwards(gsym);
12051
12052 gold_assert(gsym->has_symtab_index());
7404fe1b 12053 r_sym = gsym->symtab_index();
cf43a2fe
AM
12054 }
12055
12056 // Get the new offset--the location in the output section where
12057 // this relocation should be applied.
cf43a2fe 12058 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12059 offset += offset_in_output_section;
cf43a2fe
AM
12060 else
12061 {
c9269dff
AM
12062 section_offset_type sot_offset =
12063 convert_types<section_offset_type, Address>(offset);
cf43a2fe 12064 section_offset_type new_sot_offset =
c9269dff
AM
12065 output_section->output_offset(object, relinfo->data_shndx,
12066 sot_offset);
cf43a2fe 12067 gold_assert(new_sot_offset != -1);
7404fe1b 12068 offset = new_sot_offset;
cf43a2fe
AM
12069 }
12070
dd93cd0a
AM
12071 // In an object file, r_offset is an offset within the section.
12072 // In an executable or dynamic object, generated by
12073 // --emit-relocs, r_offset is an absolute address.
033bfb73 12074 if (!relocatable)
dd93cd0a 12075 {
7404fe1b 12076 offset += view_address;
dd93cd0a 12077 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 12078 offset -= offset_in_output_section;
dd93cd0a
AM
12079 }
12080
cf43a2fe 12081 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
12082 if (strategy == Relocatable_relocs::RELOC_COPY)
12083 ;
12084 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12085 {
7404fe1b 12086 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
033bfb73
CC
12087 addend = psymval->value(object, addend);
12088 // In a relocatable link, the symbol value is relative to
12089 // the start of the output section. For a non-relocatable
12090 // link, we need to adjust the addend.
12091 if (!relocatable)
12092 {
12093 gold_assert(os != NULL);
12094 addend -= os->address();
12095 }
cf43a2fe
AM
12096 }
12097 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12098 {
e3a7574e
AM
12099 if (size == 32)
12100 {
12101 if (addend >= 32768)
12102 addend += got2_addend;
12103 }
12104 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12105 {
12106 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 12107 addend -= d_offset;
e3a7574e
AM
12108 }
12109 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12110 {
12111 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 12112 addend -= d_offset + 4;
e3a7574e 12113 }
cf43a2fe
AM
12114 }
12115 else
12116 gold_unreachable();
12117
033bfb73 12118 if (!relocatable)
7404fe1b
AM
12119 {
12120 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12121 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12122 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12123 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12124 {
12125 // First instruction of a global dynamic sequence,
12126 // arg setup insn.
12127 const bool final = gsym == NULL || gsym->final_value_is_known();
12128 switch (this->optimize_tls_gd(final))
12129 {
12130 case tls::TLSOPT_TO_IE:
12131 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12132 - elfcpp::R_POWERPC_GOT_TLSGD16);
12133 break;
12134 case tls::TLSOPT_TO_LE:
12135 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12136 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12137 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12138 else
12139 {
12140 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12141 offset -= d_offset;
7404fe1b
AM
12142 }
12143 break;
12144 default:
12145 break;
12146 }
12147 }
12148 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12149 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12150 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12151 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12152 {
12153 // First instruction of a local dynamic sequence,
12154 // arg setup insn.
12155 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12156 {
12157 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12158 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12159 {
12160 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12161 const Output_section* os = relinfo->layout->tls_segment()
12162 ->first_section();
12163 gold_assert(os != NULL);
12164 gold_assert(os->needs_symtab_index());
12165 r_sym = os->symtab_index();
12166 addend = dtp_offset;
12167 }
12168 else
12169 {
12170 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12171 offset -= d_offset;
7404fe1b
AM
12172 }
12173 }
12174 }
12175 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12176 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12177 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12178 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12179 {
12180 // First instruction of initial exec sequence.
12181 const bool final = gsym == NULL || gsym->final_value_is_known();
12182 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12183 {
12184 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12185 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12186 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12187 else
12188 {
12189 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 12190 offset -= d_offset;
7404fe1b
AM
12191 }
12192 }
12193 }
12194 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12195 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12196 {
12197 // Second instruction of a global dynamic sequence,
12198 // the __tls_get_addr call
12199 const bool final = gsym == NULL || gsym->final_value_is_known();
12200 switch (this->optimize_tls_gd(final))
12201 {
12202 case tls::TLSOPT_TO_IE:
12203 r_type = elfcpp::R_POWERPC_NONE;
12204 zap_next = true;
12205 break;
12206 case tls::TLSOPT_TO_LE:
12207 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12208 offset += d_offset;
7404fe1b
AM
12209 zap_next = true;
12210 break;
12211 default:
12212 break;
12213 }
12214 }
12215 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12216 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12217 {
12218 // Second instruction of a local dynamic sequence,
12219 // the __tls_get_addr call
12220 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12221 {
12222 const Output_section* os = relinfo->layout->tls_segment()
12223 ->first_section();
12224 gold_assert(os != NULL);
12225 gold_assert(os->needs_symtab_index());
12226 r_sym = os->symtab_index();
12227 addend = dtp_offset;
12228 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12229 offset += d_offset;
7404fe1b
AM
12230 zap_next = true;
12231 }
12232 }
12233 else if (r_type == elfcpp::R_POWERPC_TLS)
12234 {
12235 // Second instruction of an initial exec sequence
12236 const bool final = gsym == NULL || gsym->final_value_is_known();
12237 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12238 {
12239 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 12240 offset += d_offset;
7404fe1b
AM
12241 }
12242 }
12243 }
12244
12245 reloc_write.put_r_offset(offset);
12246 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12247 reloc_write.put_r_addend(addend);
cf43a2fe
AM
12248
12249 pwrite += reloc_size;
12250 }
12251
12252 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12253 == reloc_view_size);
42cacb20
DE
12254}
12255
ec661b9d 12256// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
12257// treatment. This is how we support equality comparisons of function
12258// pointers across shared library boundaries, as described in the
12259// processor specific ABI supplement.
12260
12261template<int size, bool big_endian>
12262uint64_t
12263Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12264{
cf43a2fe
AM
12265 if (size == 32)
12266 {
12267 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
12268 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12269 p != this->stub_tables_.end();
12270 ++p)
12271 {
7e57d19e
AM
12272 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12273 = (*p)->find_plt_call_entry(gsym);
12274 if (ent != NULL)
12275 return (*p)->stub_address() + ent->off_;
ec661b9d 12276 }
c9824451 12277 }
9055360d
AM
12278 else if (this->abiversion() >= 2)
12279 {
faa2211d
AM
12280 Address off = this->glink_section()->find_global_entry(gsym);
12281 if (off != invalid_address)
9055360d
AM
12282 return this->glink_section()->global_entry_address() + off;
12283 }
ec661b9d 12284 gold_unreachable();
c9824451
AM
12285}
12286
12287// Return the PLT address to use for a local symbol.
12288template<int size, bool big_endian>
12289uint64_t
12290Target_powerpc<size, big_endian>::do_plt_address_for_local(
12291 const Relobj* object,
12292 unsigned int symndx) const
12293{
12294 if (size == 32)
12295 {
12296 const Sized_relobj<size, big_endian>* relobj
12297 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
12298 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12299 p != this->stub_tables_.end();
12300 ++p)
12301 {
7e57d19e
AM
12302 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12303 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12304 if (ent != NULL)
12305 return (*p)->stub_address() + ent->off_;
ec661b9d 12306 }
c9824451 12307 }
ec661b9d 12308 gold_unreachable();
c9824451
AM
12309}
12310
12311// Return the PLT address to use for a global symbol.
12312template<int size, bool big_endian>
12313uint64_t
12314Target_powerpc<size, big_endian>::do_plt_address_for_global(
12315 const Symbol* gsym) const
12316{
12317 if (size == 32)
12318 {
ec661b9d
AM
12319 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12320 p != this->stub_tables_.end();
12321 ++p)
12322 {
7e57d19e
AM
12323 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12324 = (*p)->find_plt_call_entry(gsym);
12325 if (ent != NULL)
12326 return (*p)->stub_address() + ent->off_;
ec661b9d 12327 }
cf43a2fe 12328 }
9055360d
AM
12329 else if (this->abiversion() >= 2)
12330 {
faa2211d
AM
12331 Address off = this->glink_section()->find_global_entry(gsym);
12332 if (off != invalid_address)
9055360d
AM
12333 return this->glink_section()->global_entry_address() + off;
12334 }
ec661b9d 12335 gold_unreachable();
42cacb20
DE
12336}
12337
bd73a62d
AM
12338// Return the offset to use for the GOT_INDX'th got entry which is
12339// for a local tls symbol specified by OBJECT, SYMNDX.
12340template<int size, bool big_endian>
12341int64_t
12342Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12343 const Relobj* object,
12344 unsigned int symndx,
12345 unsigned int got_indx) const
12346{
12347 const Powerpc_relobj<size, big_endian>* ppc_object
12348 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12349 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12350 {
12351 for (Got_type got_type = GOT_TYPE_TLSGD;
12352 got_type <= GOT_TYPE_TPREL;
12353 got_type = Got_type(got_type + 1))
12354 if (ppc_object->local_has_got_offset(symndx, got_type))
12355 {
12356 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12357 if (got_type == GOT_TYPE_TLSGD)
12358 off += size / 8;
12359 if (off == got_indx * (size / 8))
12360 {
12361 if (got_type == GOT_TYPE_TPREL)
12362 return -tp_offset;
12363 else
12364 return -dtp_offset;
12365 }
12366 }
12367 }
12368 gold_unreachable();
12369}
12370
12371// Return the offset to use for the GOT_INDX'th got entry which is
12372// for global tls symbol GSYM.
12373template<int size, bool big_endian>
12374int64_t
12375Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12376 Symbol* gsym,
12377 unsigned int got_indx) const
12378{
12379 if (gsym->type() == elfcpp::STT_TLS)
12380 {
12381 for (Got_type got_type = GOT_TYPE_TLSGD;
12382 got_type <= GOT_TYPE_TPREL;
12383 got_type = Got_type(got_type + 1))
12384 if (gsym->has_got_offset(got_type))
12385 {
12386 unsigned int off = gsym->got_offset(got_type);
12387 if (got_type == GOT_TYPE_TLSGD)
12388 off += size / 8;
12389 if (off == got_indx * (size / 8))
12390 {
12391 if (got_type == GOT_TYPE_TPREL)
12392 return -tp_offset;
12393 else
12394 return -dtp_offset;
12395 }
12396 }
12397 }
12398 gold_unreachable();
12399}
12400
42cacb20
DE
12401// The selector for powerpc object files.
12402
12403template<int size, bool big_endian>
12404class Target_selector_powerpc : public Target_selector
12405{
12406public:
12407 Target_selector_powerpc()
edc27beb
AM
12408 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12409 size, big_endian,
03ef7571
ILT
12410 (size == 64
12411 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12412 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12413 (size == 64
12414 ? (big_endian ? "elf64ppc" : "elf64lppc")
12415 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
12416 { }
12417
2e702c99
RM
12418 virtual Target*
12419 do_instantiate_target()
7f055c20 12420 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
12421};
12422
12423Target_selector_powerpc<32, true> target_selector_ppc32;
12424Target_selector_powerpc<32, false> target_selector_ppc32le;
12425Target_selector_powerpc<64, true> target_selector_ppc64;
12426Target_selector_powerpc<64, false> target_selector_ppc64le;
12427
decdd3bc
AM
12428// Instantiate these constants for -O0
12429template<int size, bool big_endian>
9055360d
AM
12430const typename Output_data_glink<size, big_endian>::Address
12431 Output_data_glink<size, big_endian>::invalid_address;
12432template<int size, bool big_endian>
decdd3bc
AM
12433const typename Stub_table<size, big_endian>::Address
12434 Stub_table<size, big_endian>::invalid_address;
12435template<int size, bool big_endian>
12436const typename Target_powerpc<size, big_endian>::Address
12437 Target_powerpc<size, big_endian>::invalid_address;
12438
42cacb20 12439} // End anonymous namespace.