Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
2571583a 3// Copyright (C) 2008-2017 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"
42cacb20
DE
44
45namespace
46{
47
48using namespace gold;
49
50template<int size, bool big_endian>
51class Output_data_plt_powerpc;
52
ec661b9d
AM
53template<int size, bool big_endian>
54class Output_data_brlt_powerpc;
55
cf43a2fe
AM
56template<int size, bool big_endian>
57class Output_data_got_powerpc;
58
59template<int size, bool big_endian>
60class Output_data_glink;
61
ec661b9d
AM
62template<int size, bool big_endian>
63class Stub_table;
64
d49044c7
AM
65template<int size, bool big_endian>
66class Output_data_save_res;
67
a3e60ddb
AM
68template<int size, bool big_endian>
69class Target_powerpc;
70
71struct Stub_table_owner
72{
dc60b26d
AM
73 Stub_table_owner()
74 : output_section(NULL), owner(NULL)
75 { }
76
a3e60ddb
AM
77 Output_section* output_section;
78 const Output_section::Input_section* owner;
79};
80
4d9aa155
AM
81inline bool
82is_branch_reloc(unsigned int r_type);
83
590b87ff
AM
84// Counter incremented on every Powerpc_relobj constructed.
85static uint32_t object_id = 0;
86
cf43a2fe
AM
87template<int size, bool big_endian>
88class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
89{
90public:
dd93cd0a 91 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
92 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
93 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 94
cf43a2fe
AM
95 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
96 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
97 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
590b87ff
AM
98 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
99 has_small_toc_reloc_(false), opd_valid_(false),
100 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
101 access_from_map_(), has14_(), stub_table_index_(), st_other_()
b4f7960d
AM
102 {
103 this->set_abiversion(0);
104 }
cf43a2fe
AM
105
106 ~Powerpc_relobj()
107 { }
108
b4f7960d
AM
109 // Read the symbols then set up st_other vector.
110 void
111 do_read_symbols(Read_symbols_data*);
112
5edad15d
AM
113 // Arrange to always relocate .toc first.
114 virtual void
115 do_relocate_sections(
116 const Symbol_table* symtab, const Layout* layout,
117 const unsigned char* pshdrs, Output_file* of,
118 typename Sized_relobj_file<size, big_endian>::Views* pviews);
119
120 // The .toc section index.
121 unsigned int
122 toc_shndx() const
123 {
124 return this->toc_;
125 }
126
127 // Mark .toc entry at OFF as not optimizable.
128 void
129 set_no_toc_opt(Address off)
130 {
131 if (this->no_toc_opt_.empty())
132 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
133 / (size / 8));
134 off /= size / 8;
135 if (off < this->no_toc_opt_.size())
136 this->no_toc_opt_[off] = true;
137 }
138
139 // Mark the entire .toc as not optimizable.
140 void
141 set_no_toc_opt()
142 {
143 this->no_toc_opt_.resize(1);
144 this->no_toc_opt_[0] = true;
145 }
146
147 // Return true if code using the .toc entry at OFF should not be edited.
148 bool
149 no_toc_opt(Address off) const
150 {
151 if (this->no_toc_opt_.empty())
152 return false;
153 off /= size / 8;
154 if (off >= this->no_toc_opt_.size())
155 return true;
156 return this->no_toc_opt_[off];
157 }
158
c9269dff 159 // The .got2 section shndx.
cf43a2fe
AM
160 unsigned int
161 got2_shndx() const
162 {
163 if (size == 32)
c9269dff 164 return this->special_;
cf43a2fe
AM
165 else
166 return 0;
167 }
168
c9269dff
AM
169 // The .opd section shndx.
170 unsigned int
171 opd_shndx() const
172 {
173 if (size == 32)
174 return 0;
175 else
176 return this->special_;
177 }
178
179 // Init OPD entry arrays.
180 void
181 init_opd(size_t opd_size)
182 {
183 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 184 this->opd_ent_.resize(count);
c9269dff
AM
185 }
186
187 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
188 unsigned int
189 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
190 {
191 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
192 gold_assert(ndx < this->opd_ent_.size());
193 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 194 if (value != NULL)
bfdfa4cd
AM
195 *value = this->opd_ent_[ndx].off;
196 return this->opd_ent_[ndx].shndx;
c9269dff
AM
197 }
198
199 // Set section and offset of function entry for .opd + R_OFF.
200 void
dd93cd0a 201 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
202 {
203 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
204 gold_assert(ndx < this->opd_ent_.size());
205 this->opd_ent_[ndx].shndx = shndx;
206 this->opd_ent_[ndx].off = value;
207 }
208
209 // Return discard flag for .opd + R_OFF.
210 bool
211 get_opd_discard(Address r_off) const
212 {
213 size_t ndx = this->opd_ent_ndx(r_off);
214 gold_assert(ndx < this->opd_ent_.size());
215 return this->opd_ent_[ndx].discard;
216 }
217
218 // Set discard flag for .opd + R_OFF.
219 void
220 set_opd_discard(Address r_off)
221 {
222 size_t ndx = this->opd_ent_ndx(r_off);
223 gold_assert(ndx < this->opd_ent_.size());
224 this->opd_ent_[ndx].discard = true;
c9269dff
AM
225 }
226
e81fea4d
AM
227 bool
228 opd_valid() const
229 { return this->opd_valid_; }
230
231 void
232 set_opd_valid()
233 { this->opd_valid_ = true; }
234
c9269dff
AM
235 // Examine .rela.opd to build info about function entry points.
236 void
237 scan_opd_relocs(size_t reloc_count,
238 const unsigned char* prelocs,
239 const unsigned char* plocal_syms);
240
5edad15d
AM
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
243 bool
244 make_toc_relative(Target_powerpc<size, big_endian>* target,
245 Address* value);
246
26a4e9cb
AM
247 // Perform the Sized_relobj_file method, then set up opd info from
248 // .opd relocs.
c9269dff
AM
249 void
250 do_read_relocs(Read_relocs_data*);
251
cf43a2fe
AM
252 bool
253 do_find_special_sections(Read_symbols_data* sd);
254
ec4dbad3
AM
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
257 bool
258 do_adjust_local_symbol(Symbol_value<size>* lv) const
259 {
260 if (size == 64 && this->opd_shndx() != 0)
261 {
262 bool is_ordinary;
263 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
264 return true;
265 if (this->get_opd_discard(lv->input_value()))
266 return false;
267 }
268 return true;
269 }
270
6c77229c
AM
271 Access_from*
272 access_from_map()
273 { return &this->access_from_map_; }
274
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
277 void
efc6fa12 278 add_reference(Relobj* src_obj,
6c77229c
AM
279 unsigned int src_indx,
280 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
281 {
282 Section_id src_id(src_obj, src_indx);
283 this->access_from_map_[dst_off].insert(src_id);
284 }
285
286 // Add a reference to the code section specified by the .opd entry
287 // at DST_OFF
288 void
289 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
290 {
291 size_t ndx = this->opd_ent_ndx(dst_off);
292 if (ndx >= this->opd_ent_.size())
293 this->opd_ent_.resize(ndx + 1);
294 this->opd_ent_[ndx].gc_mark = true;
295 }
296
297 void
298 process_gc_mark(Symbol_table* symtab)
299 {
300 for (size_t i = 0; i < this->opd_ent_.size(); i++)
301 if (this->opd_ent_[i].gc_mark)
302 {
303 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 304 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
305 }
306 }
307
dd93cd0a
AM
308 // Return offset in output GOT section that this object will use
309 // as a TOC pointer. Won't be just a constant with multi-toc support.
310 Address
311 toc_base_offset() const
312 { return 0x8000; }
313
d8f5a274
AM
314 void
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_ = true; }
317
318 bool
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_; }
321
ec661b9d
AM
322 void
323 set_has_14bit_branch(unsigned int shndx)
324 {
325 if (shndx >= this->has14_.size())
326 this->has14_.resize(shndx + 1);
327 this->has14_[shndx] = true;
328 }
329
330 bool
331 has_14bit_branch(unsigned int shndx) const
332 { return shndx < this->has14_.size() && this->has14_[shndx]; }
333
334 void
a3e60ddb 335 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 336 {
a3e60ddb 337 if (shndx >= this->stub_table_index_.size())
dc60b26d 338 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 339 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
340 }
341
342 Stub_table<size, big_endian>*
343 stub_table(unsigned int shndx)
344 {
a3e60ddb
AM
345 if (shndx < this->stub_table_index_.size())
346 {
347 Target_powerpc<size, big_endian>* target
348 = static_cast<Target_powerpc<size, big_endian>*>(
349 parameters->sized_target<size, big_endian>());
350 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
351 if (indx < target->stub_tables().size())
352 return target->stub_tables()[indx];
a3e60ddb 353 }
ec661b9d
AM
354 return NULL;
355 }
356
a3e60ddb
AM
357 void
358 clear_stub_table()
359 {
360 this->stub_table_index_.clear();
361 }
362
590b87ff
AM
363 uint32_t
364 uniq() const
365 { return this->uniq_; }
366
b4f7960d
AM
367 int
368 abiversion() const
369 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
370
371 // Set ABI version for input and output
372 void
373 set_abiversion(int ver);
374
7ee7ff70
AM
375 unsigned int
376 st_other (unsigned int symndx) const
377 {
378 return this->st_other_[symndx];
379 }
380
b4f7960d
AM
381 unsigned int
382 ppc64_local_entry_offset(const Symbol* sym) const
383 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
384
385 unsigned int
386 ppc64_local_entry_offset(unsigned int symndx) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
388
cf43a2fe 389private:
bfdfa4cd
AM
390 struct Opd_ent
391 {
392 unsigned int shndx;
c6de8ed4
AM
393 bool discard : 1;
394 bool gc_mark : 1;
26a4e9cb 395 Address off;
bfdfa4cd
AM
396 };
397
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
c9269dff
AM
408 size_t
409 opd_ent_ndx(size_t off) const
410 { return off >> 4;}
411
590b87ff
AM
412 // Per object unique identifier
413 uint32_t uniq_;
414
c9269dff
AM
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_;
bfdfa4cd 417
5edad15d
AM
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_;
420 unsigned int toc_;
421
d8f5a274
AM
422 // For 64-bit, whether this object uses small model relocs to access
423 // the toc.
424 bool has_small_toc_reloc_;
425
bfdfa4cd
AM
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
430 // access_from_map_.
431 bool opd_valid_;
432
590b87ff
AM
433 // Header e_flags
434 elfcpp::Elf_Word e_flags_;
435
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector<bool> no_toc_opt_;
439
c9269dff
AM
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
bfdfa4cd 442 // relocation on this word. The following vector records the
c9269dff 443 // section and offset specified by these relocations.
bfdfa4cd
AM
444 std::vector<Opd_ent> opd_ent_;
445
e81fea4d 446 // References made to this object's .opd section when running
bfdfa4cd
AM
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
e81fea4d 449 Access_from access_from_map_;
ec661b9d
AM
450
451 // Whether input section has a 14-bit branch reloc.
452 std::vector<bool> has14_;
453
454 // The stub table to use for a given input section.
a3e60ddb 455 std::vector<unsigned int> stub_table_index_;
b4f7960d 456
b4f7960d
AM
457 // ELF st_other field for local symbols.
458 std::vector<unsigned char> st_other_;
cf43a2fe
AM
459};
460
dc3714f3
AM
461template<int size, bool big_endian>
462class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
463{
464public:
465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
466
467 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
468 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
469 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
590b87ff 470 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_()
b4f7960d
AM
471 {
472 this->set_abiversion(0);
473 }
dc3714f3
AM
474
475 ~Powerpc_dynobj()
476 { }
477
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
480 void
481 do_read_symbols(Read_symbols_data*);
482
483 // The .opd section shndx.
484 unsigned int
485 opd_shndx() const
486 {
487 return this->opd_shndx_;
488 }
489
490 // The .opd section address.
491 Address
492 opd_address() const
493 {
494 return this->opd_address_;
495 }
496
497 // Init OPD entry arrays.
498 void
499 init_opd(size_t opd_size)
500 {
501 size_t count = this->opd_ent_ndx(opd_size);
502 this->opd_ent_.resize(count);
503 }
504
505 // Return section and offset of function entry for .opd + R_OFF.
506 unsigned int
507 get_opd_ent(Address r_off, Address* value = NULL) const
508 {
509 size_t ndx = this->opd_ent_ndx(r_off);
510 gold_assert(ndx < this->opd_ent_.size());
511 gold_assert(this->opd_ent_[ndx].shndx != 0);
512 if (value != NULL)
513 *value = this->opd_ent_[ndx].off;
514 return this->opd_ent_[ndx].shndx;
515 }
516
517 // Set section and offset of function entry for .opd + R_OFF.
518 void
519 set_opd_ent(Address r_off, unsigned int shndx, Address value)
520 {
521 size_t ndx = this->opd_ent_ndx(r_off);
522 gold_assert(ndx < this->opd_ent_.size());
523 this->opd_ent_[ndx].shndx = shndx;
524 this->opd_ent_[ndx].off = value;
525 }
526
b4f7960d
AM
527 int
528 abiversion() const
529 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
530
531 // Set ABI version for input and output.
532 void
533 set_abiversion(int ver);
534
dc3714f3
AM
535private:
536 // Used to specify extent of executable sections.
537 struct Sec_info
538 {
539 Sec_info(Address start_, Address len_, unsigned int shndx_)
540 : start(start_), len(len_), shndx(shndx_)
541 { }
542
543 bool
544 operator<(const Sec_info& that) const
545 { return this->start < that.start; }
546
547 Address start;
548 Address len;
549 unsigned int shndx;
550 };
551
552 struct Opd_ent
553 {
554 unsigned int shndx;
555 Address off;
556 };
557
558 // Return index into opd_ent_ array for .opd entry at OFF.
559 size_t
560 opd_ent_ndx(size_t off) const
561 { return off >> 4;}
562
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_;
565 Address opd_address_;
566
590b87ff
AM
567 // Header e_flags
568 elfcpp::Elf_Word e_flags_;
569
dc3714f3
AM
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector<Opd_ent> opd_ent_;
575};
576
5edad15d
AM
577// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578// base class will emit.
579
580template<int sh_type, int size, bool big_endian>
581class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
582{
583 public:
584 Powerpc_copy_relocs()
585 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
586 { }
587
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
590 void
591 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
592};
593
42cacb20
DE
594template<int size, bool big_endian>
595class Target_powerpc : public Sized_target<size, big_endian>
596{
597 public:
d83ce4e3
AM
598 typedef
599 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 600 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 601 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 602 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 603 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset = 0x7000;
606 static const Address dtp_offset = 0x8000;
42cacb20
DE
607
608 Target_powerpc()
609 : Sized_target<size, big_endian>(&powerpc_info),
ec661b9d 610 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
5edad15d 611 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 612 tlsld_got_offset_(-1U),
7e57d19e 613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
7ee7ff70
AM
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
616 relax_failed_(false), relax_fail_count_(0),
d49044c7 617 stub_group_size_(0), savres_section_(0)
42cacb20
DE
618 {
619 }
620
2e702c99 621 // Process the relocations to determine unreferenced sections for
6d03d481
ST
622 // garbage collection.
623 void
ad0f2072 624 gc_process_relocs(Symbol_table* symtab,
2e702c99
RM
625 Layout* layout,
626 Sized_relobj_file<size, big_endian>* object,
627 unsigned int data_shndx,
628 unsigned int sh_type,
629 const unsigned char* prelocs,
630 size_t reloc_count,
631 Output_section* output_section,
632 bool needs_special_offset_handling,
633 size_t local_symbol_count,
634 const unsigned char* plocal_symbols);
6d03d481 635
42cacb20
DE
636 // Scan the relocations to look for symbol adjustments.
637 void
ad0f2072 638 scan_relocs(Symbol_table* symtab,
42cacb20 639 Layout* layout,
6fa2a40b 640 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
641 unsigned int data_shndx,
642 unsigned int sh_type,
643 const unsigned char* prelocs,
644 size_t reloc_count,
645 Output_section* output_section,
646 bool needs_special_offset_handling,
647 size_t local_symbol_count,
648 const unsigned char* plocal_symbols);
921b5322
AM
649
650 // Map input .toc section to output .got section.
651 const char*
652 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
653 {
654 if (size == 64 && strcmp(name, ".toc") == 0)
655 {
656 *plen = 4;
657 return ".got";
658 }
659 return NULL;
660 }
661
f3a0ed29
AM
662 // Provide linker defined save/restore functions.
663 void
664 define_save_restore_funcs(Layout*, Symbol_table*);
665
ec661b9d
AM
666 // No stubs unless a final link.
667 bool
668 do_may_relax() const
669 { return !parameters->options().relocatable(); }
670
671 bool
672 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
673
9d5781f8
AM
674 void
675 do_plt_fde_location(const Output_data*, unsigned char*,
676 uint64_t*, off_t*) const;
677
ec661b9d
AM
678 // Stash info about branches, for stub generation.
679 void
680 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
681 unsigned int data_shndx, Address r_offset,
682 unsigned int r_type, unsigned int r_sym, Address addend)
683 {
684 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
685 this->branch_info_.push_back(info);
686 if (r_type == elfcpp::R_POWERPC_REL14
687 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
688 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
689 ppc_object->set_has_14bit_branch(data_shndx);
690 }
691
7e57d19e
AM
692 // Return whether the last branch is a plt call, and if so, mark the
693 // branch as having an R_PPC64_TOCSAVE.
694 bool
695 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
696 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
697 {
698 return (size == 64
699 && !this->branch_info_.empty()
700 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
701 r_offset, this, symtab));
702 }
703
704 // Say the given location, that of a nop in a function prologue with
705 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
706 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
707 void
708 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
709 unsigned int shndx, Address offset)
710 {
711 Symbol_location loc;
712 loc.object = ppc_object;
713 loc.shndx = shndx;
714 loc.offset = offset;
715 this->tocsave_loc_.insert(loc);
716 }
717
718 // Accessor
719 const Tocsave_loc
720 tocsave_loc() const
721 {
722 return this->tocsave_loc_;
723 }
724
f43ba157
AM
725 void
726 do_define_standard_symbols(Symbol_table*, Layout*);
727
42cacb20
DE
728 // Finalize the sections.
729 void
f59f41f3 730 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
42cacb20
DE
731
732 // Return the value to use for a dynamic which requires special
733 // treatment.
734 uint64_t
735 do_dynsym_value(const Symbol*) const;
736
c9824451
AM
737 // Return the PLT address to use for a local symbol.
738 uint64_t
739 do_plt_address_for_local(const Relobj*, unsigned int) const;
740
741 // Return the PLT address to use for a global symbol.
742 uint64_t
743 do_plt_address_for_global(const Symbol*) const;
744
bd73a62d
AM
745 // Return the offset to use for the GOT_INDX'th got entry which is
746 // for a local tls symbol specified by OBJECT, SYMNDX.
747 int64_t
748 do_tls_offset_for_local(const Relobj* object,
749 unsigned int symndx,
750 unsigned int got_indx) const;
751
752 // Return the offset to use for the GOT_INDX'th got entry which is
753 // for global tls symbol GSYM.
754 int64_t
755 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
756
dc3714f3
AM
757 void
758 do_function_location(Symbol_location*) const;
759
4d9aa155
AM
760 bool
761 do_can_check_for_function_pointers() const
762 { return true; }
763
bbec1a5d
AM
764 // Adjust -fsplit-stack code which calls non-split-stack code.
765 void
766 do_calls_non_split(Relobj* object, unsigned int shndx,
767 section_offset_type fnoffset, section_size_type fnsize,
6e0813d3 768 const unsigned char* prelocs, size_t reloc_count,
bbec1a5d
AM
769 unsigned char* view, section_size_type view_size,
770 std::string* from, std::string* to) const;
771
42cacb20
DE
772 // Relocate a section.
773 void
774 relocate_section(const Relocate_info<size, big_endian>*,
775 unsigned int sh_type,
776 const unsigned char* prelocs,
777 size_t reloc_count,
778 Output_section* output_section,
779 bool needs_special_offset_handling,
780 unsigned char* view,
c9269dff 781 Address view_address,
364c7fa5
ILT
782 section_size_type view_size,
783 const Reloc_symbol_changes*);
42cacb20
DE
784
785 // Scan the relocs during a relocatable link.
786 void
ad0f2072 787 scan_relocatable_relocs(Symbol_table* symtab,
42cacb20 788 Layout* layout,
6fa2a40b 789 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
790 unsigned int data_shndx,
791 unsigned int sh_type,
792 const unsigned char* prelocs,
793 size_t reloc_count,
794 Output_section* output_section,
795 bool needs_special_offset_handling,
796 size_t local_symbol_count,
797 const unsigned char* plocal_symbols,
798 Relocatable_relocs*);
799
4d625b70
CC
800 // Scan the relocs for --emit-relocs.
801 void
802 emit_relocs_scan(Symbol_table* symtab,
803 Layout* layout,
804 Sized_relobj_file<size, big_endian>* object,
805 unsigned int data_shndx,
806 unsigned int sh_type,
807 const unsigned char* prelocs,
808 size_t reloc_count,
809 Output_section* output_section,
810 bool needs_special_offset_handling,
811 size_t local_symbol_count,
812 const unsigned char* plocal_syms,
813 Relocatable_relocs* rr);
814
7404fe1b 815 // Emit relocations for a section.
42cacb20 816 void
7404fe1b
AM
817 relocate_relocs(const Relocate_info<size, big_endian>*,
818 unsigned int sh_type,
819 const unsigned char* prelocs,
820 size_t reloc_count,
821 Output_section* output_section,
62fe925a
RM
822 typename elfcpp::Elf_types<size>::Elf_Off
823 offset_in_output_section,
7404fe1b
AM
824 unsigned char*,
825 Address view_address,
826 section_size_type,
827 unsigned char* reloc_view,
828 section_size_type reloc_view_size);
42cacb20
DE
829
830 // Return whether SYM is defined by the ABI.
831 bool
9c2d0ef9 832 do_is_defined_by_abi(const Symbol* sym) const
42cacb20 833 {
cf43a2fe 834 return strcmp(sym->name(), "__tls_get_addr") == 0;
42cacb20
DE
835 }
836
837 // Return the size of the GOT section.
838 section_size_type
0e70b911 839 got_size() const
42cacb20
DE
840 {
841 gold_assert(this->got_ != NULL);
842 return this->got_->data_size();
843 }
844
cf43a2fe
AM
845 // Get the PLT section.
846 const Output_data_plt_powerpc<size, big_endian>*
847 plt_section() const
848 {
849 gold_assert(this->plt_ != NULL);
850 return this->plt_;
851 }
852
e5d5f5ed
AM
853 // Get the IPLT section.
854 const Output_data_plt_powerpc<size, big_endian>*
855 iplt_section() const
856 {
857 gold_assert(this->iplt_ != NULL);
858 return this->iplt_;
859 }
860
cf43a2fe
AM
861 // Get the .glink section.
862 const Output_data_glink<size, big_endian>*
863 glink_section() const
864 {
865 gold_assert(this->glink_ != NULL);
866 return this->glink_;
867 }
868
9055360d
AM
869 Output_data_glink<size, big_endian>*
870 glink_section()
871 {
872 gold_assert(this->glink_ != NULL);
873 return this->glink_;
874 }
875
9d5781f8
AM
876 bool has_glink() const
877 { return this->glink_ != NULL; }
878
cf43a2fe
AM
879 // Get the GOT section.
880 const Output_data_got_powerpc<size, big_endian>*
881 got_section() const
882 {
883 gold_assert(this->got_ != NULL);
884 return this->got_;
885 }
886
26a4e9cb
AM
887 // Get the GOT section, creating it if necessary.
888 Output_data_got_powerpc<size, big_endian>*
889 got_section(Symbol_table*, Layout*);
890
cf43a2fe
AM
891 Object*
892 do_make_elf_object(const std::string&, Input_file*, off_t,
893 const elfcpp::Ehdr<size, big_endian>&);
894
0e70b911
CC
895 // Return the number of entries in the GOT.
896 unsigned int
897 got_entry_count() const
898 {
899 if (this->got_ == NULL)
900 return 0;
901 return this->got_size() / (size / 8);
902 }
903
904 // Return the number of entries in the PLT.
905 unsigned int
906 plt_entry_count() const;
907
908 // Return the offset of the first non-reserved PLT entry.
909 unsigned int
b4f7960d
AM
910 first_plt_entry_offset() const
911 {
912 if (size == 32)
913 return 0;
914 if (this->abiversion() >= 2)
915 return 16;
916 return 24;
917 }
0e70b911
CC
918
919 // Return the size of each PLT entry.
920 unsigned int
b4f7960d
AM
921 plt_entry_size() const
922 {
923 if (size == 32)
924 return 4;
925 if (this->abiversion() >= 2)
926 return 8;
927 return 24;
928 }
0e70b911 929
d49044c7
AM
930 Output_data_save_res<size, big_endian>*
931 savres_section() const
932 {
933 return this->savres_section_;
934 }
935
e81fea4d
AM
936 // Add any special sections for this symbol to the gc work list.
937 // For powerpc64, this adds the code section of a function
938 // descriptor.
939 void
940 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
941
942 // Handle target specific gc actions when adding a gc reference from
943 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
944 // and DST_OFF. For powerpc64, this adds a referenc to the code
945 // section of a function descriptor.
946 void
947 do_gc_add_reference(Symbol_table* symtab,
efc6fa12 948 Relobj* src_obj,
e81fea4d 949 unsigned int src_shndx,
efc6fa12 950 Relobj* dst_obj,
e81fea4d
AM
951 unsigned int dst_shndx,
952 Address dst_off) const;
953
ec661b9d
AM
954 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
955 const Stub_tables&
956 stub_tables() const
957 { return this->stub_tables_; }
958
959 const Output_data_brlt_powerpc<size, big_endian>*
960 brlt_section() const
961 { return this->brlt_section_; }
962
963 void
964 add_branch_lookup_table(Address to)
965 {
966 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
967 this->branch_lookup_table_.insert(std::make_pair(to, off));
968 }
969
970 Address
971 find_branch_lookup_table(Address to)
972 {
973 typename Branch_lookup_table::const_iterator p
974 = this->branch_lookup_table_.find(to);
975 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
976 }
977
978 void
979 write_branch_lookup_table(unsigned char *oview)
980 {
981 for (typename Branch_lookup_table::const_iterator p
982 = this->branch_lookup_table_.begin();
983 p != this->branch_lookup_table_.end();
984 ++p)
985 {
4d5effb9 986 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
ec661b9d
AM
987 }
988 }
989
590b87ff
AM
990 // Wrapper used after relax to define a local symbol in output data,
991 // from the end if value < 0.
992 void
993 define_local(Symbol_table* symtab, const char* name,
994 Output_data* od, Address value, unsigned int symsize)
995 {
996 Symbol* sym
997 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
998 od, value, symsize, elfcpp::STT_NOTYPE,
999 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1000 static_cast<Signed_address>(value) < 0,
1001 false);
1002 // We are creating this symbol late, so need to fix up things
1003 // done early in Layout::finalize.
1004 sym->set_dynsym_index(-1U);
1005 }
1006
9e69ed50
AM
1007 bool
1008 plt_thread_safe() const
1009 { return this->plt_thread_safe_; }
1010
7ee7ff70
AM
1011 bool
1012 plt_localentry0() const
1013 { return this->plt_localentry0_; }
1014
1015 void
1016 set_has_localentry0()
1017 {
1018 this->has_localentry0_ = true;
1019 }
1020
1021 bool
1022 is_elfv2_localentry0(const Symbol* gsym) const
1023 {
1024 return (size == 64
1025 && this->abiversion() >= 2
1026 && this->plt_localentry0()
1027 && gsym->type() == elfcpp::STT_FUNC
1028 && gsym->is_defined()
1029 && gsym->nonvis() >> 3 == 0);
1030 }
1031
1032 bool
1033 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1034 unsigned int r_sym) const
1035 {
1036 const Powerpc_relobj<size, big_endian>* ppc_object
1037 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1038
1039 if (size == 64
1040 && this->abiversion() >= 2
1041 && this->plt_localentry0()
1042 && ppc_object->st_other(r_sym) >> 5 == 0)
1043 {
1044 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1045 bool is_ordinary;
1046 if (!psymval->is_ifunc_symbol()
1047 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1048 && is_ordinary)
1049 return true;
1050 }
1051 return false;
1052 }
1053
b4f7960d
AM
1054 int
1055 abiversion () const
1056 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1057
1058 void
1059 set_abiversion (int ver)
1060 {
1061 elfcpp::Elf_Word flags = this->processor_specific_flags();
1062 flags &= ~elfcpp::EF_PPC64_ABI;
1063 flags |= ver & elfcpp::EF_PPC64_ABI;
1064 this->set_processor_specific_flags(flags);
1065 }
1066
de194d85 1067 // Offset to save stack slot
b4f7960d
AM
1068 int
1069 stk_toc () const
1070 { return this->abiversion() < 2 ? 40 : 24; }
1071
42cacb20
DE
1072 private:
1073
e3deeb9c
AM
1074 class Track_tls
1075 {
1076 public:
1077 enum Tls_get_addr
1078 {
1079 NOT_EXPECTED = 0,
1080 EXPECTED = 1,
1081 SKIP = 2,
1082 NORMAL = 3
1083 };
1084
1085 Track_tls()
1086 : tls_get_addr_(NOT_EXPECTED),
1087 relinfo_(NULL), relnum_(0), r_offset_(0)
1088 { }
1089
1090 ~Track_tls()
1091 {
1092 if (this->tls_get_addr_ != NOT_EXPECTED)
1093 this->missing();
1094 }
1095
1096 void
1097 missing(void)
1098 {
1099 if (this->relinfo_ != NULL)
1100 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1101 _("missing expected __tls_get_addr call"));
1102 }
1103
1104 void
1105 expect_tls_get_addr_call(
1106 const Relocate_info<size, big_endian>* relinfo,
1107 size_t relnum,
1108 Address r_offset)
1109 {
1110 this->tls_get_addr_ = EXPECTED;
1111 this->relinfo_ = relinfo;
1112 this->relnum_ = relnum;
1113 this->r_offset_ = r_offset;
1114 }
1115
1116 void
1117 expect_tls_get_addr_call()
1118 { this->tls_get_addr_ = EXPECTED; }
1119
1120 void
1121 skip_next_tls_get_addr_call()
1122 {this->tls_get_addr_ = SKIP; }
1123
1124 Tls_get_addr
1125 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
1126 {
1127 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
1128 || r_type == elfcpp::R_PPC_PLTREL24)
1129 && gsym != NULL
1130 && strcmp(gsym->name(), "__tls_get_addr") == 0);
1131 Tls_get_addr last_tls = this->tls_get_addr_;
1132 this->tls_get_addr_ = NOT_EXPECTED;
1133 if (is_tls_call && last_tls != EXPECTED)
1134 return last_tls;
1135 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1136 {
1137 this->missing();
1138 return EXPECTED;
1139 }
1140 return NORMAL;
1141 }
1142
1143 private:
1144 // What we're up to regarding calls to __tls_get_addr.
1145 // On powerpc, the branch and link insn making a call to
1146 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1147 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1148 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
1149 // The marker relocation always comes first, and has the same
1150 // symbol as the reloc on the insn setting up the __tls_get_addr
1151 // argument. This ties the arg setup insn with the call insn,
1152 // allowing ld to safely optimize away the call. We check that
1153 // every call to __tls_get_addr has a marker relocation, and that
1154 // every marker relocation is on a call to __tls_get_addr.
1155 Tls_get_addr tls_get_addr_;
1156 // Info about the last reloc for error message.
1157 const Relocate_info<size, big_endian>* relinfo_;
1158 size_t relnum_;
1159 Address r_offset_;
1160 };
1161
42cacb20 1162 // The class which scans relocations.
e3deeb9c 1163 class Scan : protected Track_tls
42cacb20
DE
1164 {
1165 public:
bfdfa4cd
AM
1166 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1167
42cacb20 1168 Scan()
e3deeb9c 1169 : Track_tls(), issued_non_pic_error_(false)
42cacb20
DE
1170 { }
1171
95a2c8d6 1172 static inline int
88b8e639 1173 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
95a2c8d6 1174
42cacb20 1175 inline void
ad0f2072 1176 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1177 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1178 unsigned int data_shndx,
1179 Output_section* output_section,
1180 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
bfdfa4cd
AM
1181 const elfcpp::Sym<size, big_endian>& lsym,
1182 bool is_discarded);
42cacb20
DE
1183
1184 inline void
ad0f2072 1185 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
6fa2a40b 1186 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1187 unsigned int data_shndx,
1188 Output_section* output_section,
1189 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1190 Symbol* gsym);
1191
21bb3914
ST
1192 inline bool
1193 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1194 Target_powerpc* ,
f6971787 1195 Sized_relobj_file<size, big_endian>* relobj,
21bb3914 1196 unsigned int ,
2e702c99
RM
1197 Output_section* ,
1198 const elfcpp::Rela<size, big_endian>& ,
4d9aa155 1199 unsigned int r_type,
2e702c99 1200 const elfcpp::Sym<size, big_endian>&)
4d9aa155
AM
1201 {
1202 // PowerPC64 .opd is not folded, so any identical function text
1203 // may be folded and we'll still keep function addresses distinct.
1204 // That means no reloc is of concern here.
1205 if (size == 64)
f6971787
AM
1206 {
1207 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1208 <Powerpc_relobj<size, big_endian>*>(relobj);
1209 if (ppcobj->abiversion() == 1)
1210 return false;
1211 }
1212 // For 32-bit and ELFv2, conservatively assume anything but calls to
4d9aa155
AM
1213 // function code might be taking the address of the function.
1214 return !is_branch_reloc(r_type);
1215 }
21bb3914
ST
1216
1217 inline bool
1218 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1219 Target_powerpc* ,
f6971787 1220 Sized_relobj_file<size, big_endian>* relobj,
2e702c99
RM
1221 unsigned int ,
1222 Output_section* ,
4d9aa155
AM
1223 const elfcpp::Rela<size, big_endian>& ,
1224 unsigned int r_type,
1225 Symbol*)
1226 {
1227 // As above.
1228 if (size == 64)
f6971787
AM
1229 {
1230 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1231 <Powerpc_relobj<size, big_endian>*>(relobj);
1232 if (ppcobj->abiversion() == 1)
1233 return false;
1234 }
4d9aa155
AM
1235 return !is_branch_reloc(r_type);
1236 }
21bb3914 1237
b3ccdeb5 1238 static bool
9055360d
AM
1239 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1240 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
1241 unsigned int r_type, bool report_err);
1242
42cacb20
DE
1243 private:
1244 static void
6fa2a40b 1245 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1246 unsigned int r_type);
1247
1248 static void
6fa2a40b 1249 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
42cacb20
DE
1250 unsigned int r_type, Symbol*);
1251
1252 static void
1253 generate_tls_call(Symbol_table* symtab, Layout* layout,
1254 Target_powerpc* target);
1255
1256 void
1257 check_non_pic(Relobj*, unsigned int r_type);
1258
1259 // Whether we have issued an error about a non-PIC compilation.
1260 bool issued_non_pic_error_;
1261 };
1262
1611bc4a
AM
1263 bool
1264 symval_for_branch(const Symbol_table* symtab,
6c77229c 1265 const Sized_symbol<size>* gsym,
3ea0a085 1266 Powerpc_relobj<size, big_endian>* object,
1611bc4a 1267 Address *value, unsigned int *dest_shndx);
3ea0a085 1268
42cacb20 1269 // The class which implements relocation.
e3deeb9c 1270 class Relocate : protected Track_tls
42cacb20
DE
1271 {
1272 public:
dd93cd0a
AM
1273 // Use 'at' branch hints when true, 'y' when false.
1274 // FIXME maybe: set this with an option.
1275 static const bool is_isa_v2 = true;
1276
dd93cd0a 1277 Relocate()
e3deeb9c 1278 : Track_tls()
dd93cd0a
AM
1279 { }
1280
42cacb20
DE
1281 // Do a relocation. Return false if the caller should not issue
1282 // any warnings about this relocation.
1283 inline bool
91a65d2f
AM
1284 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1285 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1286 const Sized_symbol<size>*, const Symbol_value<size>*,
1287 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
42cacb20 1288 section_size_type);
42cacb20
DE
1289 };
1290
168a4726
AM
1291 class Relocate_comdat_behavior
1292 {
1293 public:
1294 // Decide what the linker should do for relocations that refer to
1295 // discarded comdat sections.
1296 inline Comdat_behavior
1297 get(const char* name)
1298 {
1299 gold::Default_comdat_behavior default_behavior;
1300 Comdat_behavior ret = default_behavior.get(name);
1301 if (ret == CB_WARNING)
1302 {
1303 if (size == 32
1304 && (strcmp(name, ".fixup") == 0
1305 || strcmp(name, ".got2") == 0))
1306 ret = CB_IGNORE;
1307 if (size == 64
1308 && (strcmp(name, ".opd") == 0
1309 || strcmp(name, ".toc") == 0
1310 || strcmp(name, ".toc1") == 0))
1311 ret = CB_IGNORE;
1312 }
1313 return ret;
1314 }
1315 };
1316
dd93cd0a
AM
1317 // Optimize the TLS relocation type based on what we know about the
1318 // symbol. IS_FINAL is true if the final address of this symbol is
1319 // known at link time.
1320
1321 tls::Tls_optimization
1322 optimize_tls_gd(bool is_final)
1323 {
1324 // If we are generating a shared library, then we can't do anything
1325 // in the linker.
1326 if (parameters->options().shared())
1327 return tls::TLSOPT_NONE;
1328
1329 if (!is_final)
1330 return tls::TLSOPT_TO_IE;
1331 return tls::TLSOPT_TO_LE;
1332 }
1333
1334 tls::Tls_optimization
1335 optimize_tls_ld()
1336 {
1337 if (parameters->options().shared())
1338 return tls::TLSOPT_NONE;
1339
1340 return tls::TLSOPT_TO_LE;
1341 }
1342
1343 tls::Tls_optimization
1344 optimize_tls_ie(bool is_final)
1345 {
1346 if (!is_final || parameters->options().shared())
1347 return tls::TLSOPT_NONE;
1348
1349 return tls::TLSOPT_TO_LE;
1350 }
cf43a2fe 1351
cf43a2fe
AM
1352 // Create glink.
1353 void
1354 make_glink_section(Layout*);
42cacb20 1355
cf43a2fe
AM
1356 // Create the PLT section.
1357 void
40b469d7 1358 make_plt_section(Symbol_table*, Layout*);
42cacb20 1359
e5d5f5ed 1360 void
40b469d7 1361 make_iplt_section(Symbol_table*, Layout*);
e5d5f5ed 1362
ec661b9d
AM
1363 void
1364 make_brlt_section(Layout*);
1365
42cacb20
DE
1366 // Create a PLT entry for a global symbol.
1367 void
ec661b9d 1368 make_plt_entry(Symbol_table*, Layout*, Symbol*);
e5d5f5ed
AM
1369
1370 // Create a PLT entry for a local IFUNC symbol.
1371 void
40b469d7 1372 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
ec661b9d
AM
1373 Sized_relobj_file<size, big_endian>*,
1374 unsigned int);
1375
42cacb20 1376
dd93cd0a
AM
1377 // Create a GOT entry for local dynamic __tls_get_addr.
1378 unsigned int
1379 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1380 Sized_relobj_file<size, big_endian>* object);
1381
42cacb20 1382 unsigned int
dd93cd0a
AM
1383 tlsld_got_offset() const
1384 {
1385 return this->tlsld_got_offset_;
1386 }
42cacb20 1387
42cacb20
DE
1388 // Get the dynamic reloc section, creating it if necessary.
1389 Reloc_section*
1390 rela_dyn_section(Layout*);
1391
b3ccdeb5
AM
1392 // Similarly, but for ifunc symbols get the one for ifunc.
1393 Reloc_section*
1394 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1395
42cacb20
DE
1396 // Copy a relocation against a global symbol.
1397 void
ef9beddf 1398 copy_reloc(Symbol_table* symtab, Layout* layout,
2e702c99 1399 Sized_relobj_file<size, big_endian>* object,
42cacb20
DE
1400 unsigned int shndx, Output_section* output_section,
1401 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1402 {
859d7987 1403 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
42cacb20
DE
1404 this->copy_relocs_.copy_reloc(symtab, layout,
1405 symtab->get_sized_symbol<size>(sym),
1406 object, shndx, output_section,
859d7987
CC
1407 r_type, reloc.get_r_offset(),
1408 reloc.get_r_addend(),
1409 this->rela_dyn_section(layout));
42cacb20
DE
1410 }
1411
0cfdc767 1412 // Look over all the input sections, deciding where to place stubs.
ec661b9d 1413 void
a3e60ddb 1414 group_sections(Layout*, const Task*, bool);
ec661b9d
AM
1415
1416 // Sort output sections by address.
1417 struct Sort_sections
1418 {
1419 bool
1420 operator()(const Output_section* sec1, const Output_section* sec2)
1421 { return sec1->address() < sec2->address(); }
1422 };
1423
1424 class Branch_info
1425 {
1426 public:
1427 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1428 unsigned int data_shndx,
1429 Address r_offset,
1430 unsigned int r_type,
1431 unsigned int r_sym,
1432 Address addend)
1433 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
7e57d19e 1434 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
ec661b9d
AM
1435 { }
1436
1437 ~Branch_info()
1438 { }
1439
7e57d19e
AM
1440 // Return whether this branch is going via a plt call stub, and if
1441 // so, mark it as having an R_PPC64_TOCSAVE.
1442 bool
1443 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1444 unsigned int shndx, Address offset,
1445 Target_powerpc* target, Symbol_table* symtab);
1446
ec661b9d 1447 // If this branch needs a plt call stub, or a long branch stub, make one.
a3e60ddb 1448 bool
ec661b9d
AM
1449 make_stub(Stub_table<size, big_endian>*,
1450 Stub_table<size, big_endian>*,
1451 Symbol_table*) const;
1452
1453 private:
1454 // The branch location..
1455 Powerpc_relobj<size, big_endian>* object_;
1456 unsigned int shndx_;
1457 Address offset_;
1458 // ..and the branch type and destination.
7e57d19e
AM
1459 unsigned int r_type_ : 31;
1460 unsigned int tocsave_ : 1;
ec661b9d
AM
1461 unsigned int r_sym_;
1462 Address addend_;
1463 };
1464
42cacb20
DE
1465 // Information about this specific target which we pass to the
1466 // general Target structure.
1467 static Target::Target_info powerpc_info;
1468
1469 // The types of GOT entries needed for this platform.
0e70b911
CC
1470 // These values are exposed to the ABI in an incremental link.
1471 // Do not renumber existing values without changing the version
1472 // number of the .gnu_incremental_inputs section.
42cacb20
DE
1473 enum Got_type
1474 {
dd93cd0a
AM
1475 GOT_TYPE_STANDARD,
1476 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1477 GOT_TYPE_DTPREL, // entry for @got@dtprel
1478 GOT_TYPE_TPREL // entry for @got@tprel
42cacb20
DE
1479 };
1480
ec661b9d 1481 // The GOT section.
cf43a2fe 1482 Output_data_got_powerpc<size, big_endian>* got_;
b3ccdeb5
AM
1483 // The PLT section. This is a container for a table of addresses,
1484 // and their relocations. Each address in the PLT has a dynamic
1485 // relocation (R_*_JMP_SLOT) and each address will have a
1486 // corresponding entry in .glink for lazy resolution of the PLT.
1487 // ppc32 initialises the PLT to point at the .glink entry, while
1488 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1489 // linker adds a stub that loads the PLT entry into ctr then
1490 // branches to ctr. There may be more than one stub for each PLT
1491 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1492 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
42cacb20 1493 Output_data_plt_powerpc<size, big_endian>* plt_;
b3ccdeb5
AM
1494 // The IPLT section. Like plt_, this is a container for a table of
1495 // addresses and their relocations, specifically for STT_GNU_IFUNC
1496 // functions that resolve locally (STT_GNU_IFUNC functions that
1497 // don't resolve locally go in PLT). Unlike plt_, these have no
1498 // entry in .glink for lazy resolution, and the relocation section
1499 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1500 // the relocation section may contain relocations against
1501 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1502 // relocation section will appear at the end of other dynamic
1503 // relocations, so that ld.so applies these relocations after other
1504 // dynamic relocations. In a static executable, the relocation
1505 // section is emitted and marked with __rela_iplt_start and
1506 // __rela_iplt_end symbols.
e5d5f5ed 1507 Output_data_plt_powerpc<size, big_endian>* iplt_;
ec661b9d
AM
1508 // Section holding long branch destinations.
1509 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1510 // The .glink section.
cf43a2fe 1511 Output_data_glink<size, big_endian>* glink_;
ec661b9d 1512 // The dynamic reloc section.
42cacb20
DE
1513 Reloc_section* rela_dyn_;
1514 // Relocs saved to avoid a COPY reloc.
5edad15d 1515 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
dd93cd0a
AM
1516 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1517 unsigned int tlsld_got_offset_;
ec661b9d
AM
1518
1519 Stub_tables stub_tables_;
1520 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1521 Branch_lookup_table branch_lookup_table_;
1522
1523 typedef std::vector<Branch_info> Branches;
1524 Branches branch_info_;
7e57d19e 1525 Tocsave_loc tocsave_loc_;
9e69ed50
AM
1526
1527 bool plt_thread_safe_;
7ee7ff70
AM
1528 bool plt_localentry0_;
1529 bool plt_localentry0_init_;
1530 bool has_localentry0_;
a3e60ddb
AM
1531
1532 bool relax_failed_;
1533 int relax_fail_count_;
1534 int32_t stub_group_size_;
d49044c7
AM
1535
1536 Output_data_save_res<size, big_endian> *savres_section_;
42cacb20
DE
1537};
1538
1539template<>
1540Target::Target_info Target_powerpc<32, true>::powerpc_info =
1541{
1542 32, // size
1543 true, // is_big_endian
1544 elfcpp::EM_PPC, // machine_code
1545 false, // has_make_symbol
1546 false, // has_resolve
1547 false, // has_code_fill
1548 true, // is_default_stack_executable
b3ce541e 1549 false, // can_icf_inline_merge_sections
42cacb20
DE
1550 '\0', // wrap_char
1551 "/usr/lib/ld.so.1", // dynamic_linker
1552 0x10000000, // default_text_segment_address
1553 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1554 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1555 false, // isolate_execinstr
1556 0, // rosegment_gap
8a5e3e08
ILT
1557 elfcpp::SHN_UNDEF, // small_common_shndx
1558 elfcpp::SHN_UNDEF, // large_common_shndx
1559 0, // small_common_section_flags
05a352e6
DK
1560 0, // large_common_section_flags
1561 NULL, // attributes_section
a67858e0 1562 NULL, // attributes_vendor
8d9743bd
MK
1563 "_start", // entry_symbol_name
1564 32, // hash_entry_size
42cacb20
DE
1565};
1566
1567template<>
1568Target::Target_info Target_powerpc<32, false>::powerpc_info =
1569{
1570 32, // size
1571 false, // is_big_endian
1572 elfcpp::EM_PPC, // machine_code
1573 false, // has_make_symbol
1574 false, // has_resolve
1575 false, // has_code_fill
1576 true, // is_default_stack_executable
b3ce541e 1577 false, // can_icf_inline_merge_sections
42cacb20
DE
1578 '\0', // wrap_char
1579 "/usr/lib/ld.so.1", // dynamic_linker
1580 0x10000000, // default_text_segment_address
1581 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08 1582 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1583 false, // isolate_execinstr
1584 0, // rosegment_gap
8a5e3e08
ILT
1585 elfcpp::SHN_UNDEF, // small_common_shndx
1586 elfcpp::SHN_UNDEF, // large_common_shndx
1587 0, // small_common_section_flags
05a352e6
DK
1588 0, // large_common_section_flags
1589 NULL, // attributes_section
a67858e0 1590 NULL, // attributes_vendor
8d9743bd
MK
1591 "_start", // entry_symbol_name
1592 32, // hash_entry_size
42cacb20
DE
1593};
1594
1595template<>
1596Target::Target_info Target_powerpc<64, true>::powerpc_info =
1597{
1598 64, // size
1599 true, // is_big_endian
1600 elfcpp::EM_PPC64, // machine_code
1601 false, // has_make_symbol
1602 false, // has_resolve
1603 false, // has_code_fill
1604 true, // is_default_stack_executable
b3ce541e 1605 false, // can_icf_inline_merge_sections
42cacb20
DE
1606 '\0', // wrap_char
1607 "/usr/lib/ld.so.1", // dynamic_linker
1608 0x10000000, // default_text_segment_address
1609 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1610 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1611 false, // isolate_execinstr
1612 0, // rosegment_gap
8a5e3e08
ILT
1613 elfcpp::SHN_UNDEF, // small_common_shndx
1614 elfcpp::SHN_UNDEF, // large_common_shndx
1615 0, // small_common_section_flags
05a352e6
DK
1616 0, // large_common_section_flags
1617 NULL, // attributes_section
a67858e0 1618 NULL, // attributes_vendor
8d9743bd
MK
1619 "_start", // entry_symbol_name
1620 32, // hash_entry_size
42cacb20
DE
1621};
1622
1623template<>
1624Target::Target_info Target_powerpc<64, false>::powerpc_info =
1625{
1626 64, // size
1627 false, // is_big_endian
1628 elfcpp::EM_PPC64, // machine_code
1629 false, // has_make_symbol
1630 false, // has_resolve
1631 false, // has_code_fill
1632 true, // is_default_stack_executable
b3ce541e 1633 false, // can_icf_inline_merge_sections
42cacb20
DE
1634 '\0', // wrap_char
1635 "/usr/lib/ld.so.1", // dynamic_linker
1636 0x10000000, // default_text_segment_address
1637 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
dd93cd0a 1638 4 * 1024, // common_pagesize (overridable by -z common-page-size)
c9269dff
AM
1639 false, // isolate_execinstr
1640 0, // rosegment_gap
8a5e3e08
ILT
1641 elfcpp::SHN_UNDEF, // small_common_shndx
1642 elfcpp::SHN_UNDEF, // large_common_shndx
1643 0, // small_common_section_flags
05a352e6
DK
1644 0, // large_common_section_flags
1645 NULL, // attributes_section
a67858e0 1646 NULL, // attributes_vendor
8d9743bd
MK
1647 "_start", // entry_symbol_name
1648 32, // hash_entry_size
42cacb20
DE
1649};
1650
dd93cd0a
AM
1651inline bool
1652is_branch_reloc(unsigned int r_type)
1653{
1654 return (r_type == elfcpp::R_POWERPC_REL24
1655 || r_type == elfcpp::R_PPC_PLTREL24
1656 || r_type == elfcpp::R_PPC_LOCAL24PC
1657 || r_type == elfcpp::R_POWERPC_REL14
1658 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1659 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1660 || r_type == elfcpp::R_POWERPC_ADDR24
1661 || r_type == elfcpp::R_POWERPC_ADDR14
1662 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1663 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1664}
1665
1666// If INSN is an opcode that may be used with an @tls operand, return
1667// the transformed insn for TLS optimisation, otherwise return 0. If
1668// REG is non-zero only match an insn with RB or RA equal to REG.
1669uint32_t
1670at_tls_transform(uint32_t insn, unsigned int reg)
1671{
1672 if ((insn & (0x3f << 26)) != 31 << 26)
1673 return 0;
1674
1675 unsigned int rtra;
1676 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1677 rtra = insn & ((1 << 26) - (1 << 16));
1678 else if (((insn >> 16) & 0x1f) == reg)
1679 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1680 else
1681 return 0;
1682
1683 if ((insn & (0x3ff << 1)) == 266 << 1)
1684 // add -> addi
1685 insn = 14 << 26;
1686 else if ((insn & (0x1f << 1)) == 23 << 1
1687 && ((insn & (0x1f << 6)) < 14 << 6
1688 || ((insn & (0x1f << 6)) >= 16 << 6
1689 && (insn & (0x1f << 6)) < 24 << 6)))
1690 // load and store indexed -> dform
1691 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1692 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1693 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1694 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1695 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1696 // lwax -> lwa
1697 insn = (58 << 26) | 2;
1698 else
1699 return 0;
1700 insn |= rtra;
1701 return insn;
1702}
1703
dd93cd0a 1704
42cacb20
DE
1705template<int size, bool big_endian>
1706class Powerpc_relocate_functions
1707{
dd93cd0a 1708public:
f4baf0d4 1709 enum Overflow_check
dd93cd0a 1710 {
f4baf0d4
AM
1711 CHECK_NONE,
1712 CHECK_SIGNED,
b80eed39
AM
1713 CHECK_UNSIGNED,
1714 CHECK_BITFIELD,
1715 CHECK_LOW_INSN,
1716 CHECK_HIGH_INSN
dd93cd0a
AM
1717 };
1718
f4baf0d4 1719 enum Status
dd93cd0a 1720 {
f4baf0d4
AM
1721 STATUS_OK,
1722 STATUS_OVERFLOW
1723 };
dd93cd0a 1724
42cacb20 1725private:
c9269dff 1726 typedef Powerpc_relocate_functions<size, big_endian> This;
c9269dff 1727 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
a680de9a 1728 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
c9269dff 1729
dd93cd0a
AM
1730 template<int valsize>
1731 static inline bool
1732 has_overflow_signed(Address value)
1733 {
1734 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1735 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1736 limit <<= ((valsize - 1) >> 1);
1737 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1738 return value + limit > (limit << 1) - 1;
1739 }
1740
1741 template<int valsize>
1742 static inline bool
b80eed39 1743 has_overflow_unsigned(Address value)
dd93cd0a
AM
1744 {
1745 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1746 limit <<= ((valsize - 1) >> 1);
1747 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
b80eed39
AM
1748 return value > (limit << 1) - 1;
1749 }
1750
1751 template<int valsize>
1752 static inline bool
1753 has_overflow_bitfield(Address value)
1754 {
1755 return (has_overflow_unsigned<valsize>(value)
1756 && has_overflow_signed<valsize>(value));
dd93cd0a
AM
1757 }
1758
1759 template<int valsize>
f4baf0d4
AM
1760 static inline Status
1761 overflowed(Address value, Overflow_check overflow)
dd93cd0a 1762 {
f4baf0d4 1763 if (overflow == CHECK_SIGNED)
dd93cd0a
AM
1764 {
1765 if (has_overflow_signed<valsize>(value))
f4baf0d4 1766 return STATUS_OVERFLOW;
dd93cd0a 1767 }
b80eed39
AM
1768 else if (overflow == CHECK_UNSIGNED)
1769 {
1770 if (has_overflow_unsigned<valsize>(value))
1771 return STATUS_OVERFLOW;
1772 }
f4baf0d4 1773 else if (overflow == CHECK_BITFIELD)
dd93cd0a
AM
1774 {
1775 if (has_overflow_bitfield<valsize>(value))
f4baf0d4 1776 return STATUS_OVERFLOW;
dd93cd0a 1777 }
f4baf0d4 1778 return STATUS_OK;
dd93cd0a
AM
1779 }
1780
cf43a2fe 1781 // Do a simple RELA relocation
0cfb0717 1782 template<int fieldsize, int valsize>
f4baf0d4
AM
1783 static inline Status
1784 rela(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1785 {
0cfb0717 1786 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
dd93cd0a 1787 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1788 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
dd93cd0a
AM
1789 return overflowed<valsize>(value, overflow);
1790 }
1791
0cfb0717 1792 template<int fieldsize, int valsize>
f4baf0d4 1793 static inline Status
42cacb20
DE
1794 rela(unsigned char* view,
1795 unsigned int right_shift,
0cfb0717 1796 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 1797 Address value,
f4baf0d4 1798 Overflow_check overflow)
42cacb20 1799 {
0cfb0717 1800 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
42cacb20 1801 Valtype* wv = reinterpret_cast<Valtype*>(view);
0cfb0717 1802 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
dd93cd0a 1803 Valtype reloc = value >> right_shift;
42cacb20
DE
1804 val &= ~dst_mask;
1805 reloc &= dst_mask;
0cfb0717 1806 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
dd93cd0a 1807 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1808 }
1809
cf43a2fe 1810 // Do a simple RELA relocation, unaligned.
0cfb0717 1811 template<int fieldsize, int valsize>
f4baf0d4
AM
1812 static inline Status
1813 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1814 {
0cfb0717 1815 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
dd93cd0a
AM
1816 return overflowed<valsize>(value, overflow);
1817 }
1818
0cfb0717 1819 template<int fieldsize, int valsize>
f4baf0d4 1820 static inline Status
cf43a2fe
AM
1821 rela_ua(unsigned char* view,
1822 unsigned int right_shift,
0cfb0717 1823 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
c9269dff 1824 Address value,
f4baf0d4 1825 Overflow_check overflow)
42cacb20 1826 {
0cfb0717 1827 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
c9269dff 1828 Valtype;
0cfb0717 1829 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
dd93cd0a 1830 Valtype reloc = value >> right_shift;
42cacb20
DE
1831 val &= ~dst_mask;
1832 reloc &= dst_mask;
0cfb0717 1833 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
dd93cd0a 1834 return overflowed<valsize>(value >> right_shift, overflow);
42cacb20
DE
1835 }
1836
42cacb20 1837public:
dd93cd0a 1838 // R_PPC64_ADDR64: (Symbol + Addend)
42cacb20 1839 static inline void
dd93cd0a 1840 addr64(unsigned char* view, Address value)
0cfb0717 1841 { This::template rela<64,64>(view, value, CHECK_NONE); }
42cacb20 1842
dd93cd0a 1843 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
42cacb20 1844 static inline void
dd93cd0a 1845 addr64_u(unsigned char* view, Address value)
0cfb0717 1846 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
dd93cd0a
AM
1847
1848 // R_POWERPC_ADDR32: (Symbol + Addend)
f4baf0d4
AM
1849 static inline Status
1850 addr32(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1851 { return This::template rela<32,32>(view, value, overflow); }
dd93cd0a
AM
1852
1853 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
f4baf0d4
AM
1854 static inline Status
1855 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1856 { return This::template rela_ua<32,32>(view, value, overflow); }
dd93cd0a
AM
1857
1858 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
f4baf0d4
AM
1859 static inline Status
1860 addr24(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1861 {
0cfb0717
AM
1862 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1863 value, overflow);
f4baf0d4
AM
1864 if (overflow != CHECK_NONE && (value & 3) != 0)
1865 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1866 return stat;
1867 }
42cacb20
DE
1868
1869 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
f4baf0d4
AM
1870 static inline Status
1871 addr16(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1872 { return This::template rela<16,16>(view, value, overflow); }
42cacb20 1873
dd93cd0a 1874 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
f4baf0d4
AM
1875 static inline Status
1876 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
0cfb0717 1877 { return This::template rela_ua<16,16>(view, value, overflow); }
42cacb20 1878
dd93cd0a 1879 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1880 static inline Status
1881 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1882 {
0cfb0717 1883 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
ec86f434 1884 if ((value & 3) != 0)
f4baf0d4 1885 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1886 return stat;
1887 }
42cacb20 1888
a680de9a
PB
1889 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
1890 static inline Status
1891 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
1892 {
1893 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
1894 if ((value & 15) != 0)
1895 stat = STATUS_OVERFLOW;
1896 return stat;
1897 }
1898
42cacb20
DE
1899 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1900 static inline void
dd93cd0a 1901 addr16_hi(unsigned char* view, Address value)
0cfb0717 1902 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
42cacb20 1903
c9269dff 1904 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
42cacb20 1905 static inline void
dd93cd0a
AM
1906 addr16_ha(unsigned char* view, Address value)
1907 { This::addr16_hi(view, value + 0x8000); }
42cacb20 1908
dd93cd0a 1909 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
42cacb20 1910 static inline void
dd93cd0a 1911 addr16_hi2(unsigned char* view, Address value)
0cfb0717 1912 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
42cacb20 1913
dd93cd0a 1914 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
42cacb20 1915 static inline void
dd93cd0a
AM
1916 addr16_ha2(unsigned char* view, Address value)
1917 { This::addr16_hi2(view, value + 0x8000); }
42cacb20 1918
dd93cd0a 1919 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
42cacb20 1920 static inline void
dd93cd0a 1921 addr16_hi3(unsigned char* view, Address value)
0cfb0717 1922 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
42cacb20 1923
dd93cd0a 1924 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
42cacb20 1925 static inline void
dd93cd0a
AM
1926 addr16_ha3(unsigned char* view, Address value)
1927 { This::addr16_hi3(view, value + 0x8000); }
1928
1929 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
f4baf0d4
AM
1930 static inline Status
1931 addr14(unsigned char* view, Address value, Overflow_check overflow)
dd93cd0a 1932 {
0cfb0717 1933 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
f4baf0d4
AM
1934 if (overflow != CHECK_NONE && (value & 3) != 0)
1935 stat = STATUS_OVERFLOW;
dd93cd0a
AM
1936 return stat;
1937 }
a680de9a
PB
1938
1939 // R_POWERPC_REL16DX_HA
1940 static inline Status
1941 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
1942 {
1943 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1944 Valtype* wv = reinterpret_cast<Valtype*>(view);
1945 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1946 value += 0x8000;
1947 value = static_cast<SignedAddress>(value) >> 16;
1948 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
1949 elfcpp::Swap<32, big_endian>::writeval(wv, val);
1950 return overflowed<16>(value, overflow);
1951 }
cf43a2fe
AM
1952};
1953
b4f7960d
AM
1954// Set ABI version for input and output.
1955
1956template<int size, bool big_endian>
1957void
1958Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1959{
1960 this->e_flags_ |= ver;
1961 if (this->abiversion() != 0)
1962 {
1963 Target_powerpc<size, big_endian>* target =
1964 static_cast<Target_powerpc<size, big_endian>*>(
1965 parameters->sized_target<size, big_endian>());
1966 if (target->abiversion() == 0)
1967 target->set_abiversion(this->abiversion());
1968 else if (target->abiversion() != this->abiversion())
1969 gold_error(_("%s: ABI version %d is not compatible "
1970 "with ABI version %d output"),
1971 this->name().c_str(),
1972 this->abiversion(), target->abiversion());
1973
1974 }
1975}
1976
5edad15d
AM
1977// Stash away the index of .got2, .opd, .rela.toc, and .toc in a
1978// relocatable object, if such sections exists.
cf43a2fe
AM
1979
1980template<int size, bool big_endian>
1981bool
1982Powerpc_relobj<size, big_endian>::do_find_special_sections(
1983 Read_symbols_data* sd)
1984{
c9269dff
AM
1985 const unsigned char* const pshdrs = sd->section_headers->data();
1986 const unsigned char* namesu = sd->section_names->data();
1987 const char* names = reinterpret_cast<const char*>(namesu);
1988 section_size_type names_size = sd->section_names_size;
1989 const unsigned char* s;
1990
dc3714f3
AM
1991 s = this->template find_shdr<size, big_endian>(pshdrs,
1992 size == 32 ? ".got2" : ".opd",
1993 names, names_size, NULL);
c9269dff
AM
1994 if (s != NULL)
1995 {
1996 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1997 this->special_ = ndx;
b4f7960d
AM
1998 if (size == 64)
1999 {
2000 if (this->abiversion() == 0)
2001 this->set_abiversion(1);
2002 else if (this->abiversion() > 1)
2003 gold_error(_("%s: .opd invalid in abiv%d"),
2004 this->name().c_str(), this->abiversion());
2005 }
c9269dff 2006 }
5edad15d
AM
2007 if (size == 64)
2008 {
2009 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2010 names, names_size, NULL);
2011 if (s != NULL)
2012 {
2013 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2014 this->relatoc_ = ndx;
2015 typename elfcpp::Shdr<size, big_endian> shdr(s);
2016 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2017 }
2018 }
c9269dff
AM
2019 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2020}
2021
2022// Examine .rela.opd to build info about function entry points.
2023
2024template<int size, bool big_endian>
2025void
2026Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2027 size_t reloc_count,
2028 const unsigned char* prelocs,
2029 const unsigned char* plocal_syms)
2030{
2031 if (size == 64)
cf43a2fe 2032 {
0e123f69
AM
2033 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2034 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
c9269dff 2035 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
ec4dbad3
AM
2036 Address expected_off = 0;
2037 bool regular = true;
2038 unsigned int opd_ent_size = 0;
c9269dff
AM
2039
2040 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
cf43a2fe 2041 {
c9269dff
AM
2042 Reltype reloc(prelocs);
2043 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2044 = reloc.get_r_info();
2045 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2046 if (r_type == elfcpp::R_PPC64_ADDR64)
2047 {
2048 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2049 typename elfcpp::Elf_types<size>::Elf_Addr value;
2050 bool is_ordinary;
2051 unsigned int shndx;
2052 if (r_sym < this->local_symbol_count())
2053 {
2054 typename elfcpp::Sym<size, big_endian>
2055 lsym(plocal_syms + r_sym * sym_size);
2056 shndx = lsym.get_st_shndx();
2057 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2058 value = lsym.get_st_value();
2059 }
2060 else
2061 shndx = this->symbol_section_and_value(r_sym, &value,
2062 &is_ordinary);
2063 this->set_opd_ent(reloc.get_r_offset(), shndx,
2064 value + reloc.get_r_addend());
ec4dbad3
AM
2065 if (i == 2)
2066 {
2067 expected_off = reloc.get_r_offset();
2068 opd_ent_size = expected_off;
2069 }
2070 else if (expected_off != reloc.get_r_offset())
2071 regular = false;
2072 expected_off += opd_ent_size;
2073 }
2074 else if (r_type == elfcpp::R_PPC64_TOC)
2075 {
2076 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2077 regular = false;
2078 }
2079 else
2080 {
2081 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2082 this->name().c_str(), r_type);
2083 regular = false;
c9269dff
AM
2084 }
2085 }
ec4dbad3
AM
2086 if (reloc_count <= 2)
2087 opd_ent_size = this->section_size(this->opd_shndx());
2088 if (opd_ent_size != 24 && opd_ent_size != 16)
2089 regular = false;
2090 if (!regular)
2091 {
2092 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2093 this->name().c_str());
2094 opd_ent_size = 0;
2095 }
c9269dff
AM
2096 }
2097}
2098
5edad15d
AM
2099// Returns true if a code sequence loading the TOC entry at VALUE
2100// relative to the TOC pointer can be converted into code calculating
2101// a TOC pointer relative offset.
2102// If so, the TOC pointer relative offset is stored to VALUE.
2103
2104template<int size, bool big_endian>
2105bool
2106Powerpc_relobj<size, big_endian>::make_toc_relative(
2107 Target_powerpc<size, big_endian>* target,
2108 Address* value)
2109{
2110 if (size != 64)
2111 return false;
2112
e666304e
AM
2113 // With -mcmodel=medium code it is quite possible to have
2114 // toc-relative relocs referring to objects outside the TOC.
2115 // Don't try to look at a non-existent TOC.
2116 if (this->toc_shndx() == 0)
2117 return false;
2118
5edad15d
AM
2119 // Convert VALUE back to an address by adding got_base (see below),
2120 // then to an offset in the TOC by subtracting the TOC output
2121 // section address and the TOC output offset. Since this TOC output
2122 // section and the got output section are one and the same, we can
2123 // omit adding and subtracting the output section address.
2124 Address off = (*value + this->toc_base_offset()
2125 - this->output_section_offset(this->toc_shndx()));
2126 // Is this offset in the TOC? -mcmodel=medium code may be using
2127 // TOC relative access to variables outside the TOC. Those of
2128 // course can't be optimized. We also don't try to optimize code
2129 // that is using a different object's TOC.
2130 if (off >= this->section_size(this->toc_shndx()))
2131 return false;
2132
2133 if (this->no_toc_opt(off))
2134 return false;
2135
2136 section_size_type vlen;
2137 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2138 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2139 // The TOC pointer
2140 Address got_base = (target->got_section()->output_section()->address()
2141 + this->toc_base_offset());
2142 addr -= got_base;
857e829e 2143 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
5edad15d
AM
2144 return false;
2145
2146 *value = addr;
2147 return true;
2148}
2149
2150// Perform the Sized_relobj_file method, then set up opd info from
2151// .opd relocs.
2152
c9269dff
AM
2153template<int size, bool big_endian>
2154void
2155Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2156{
2157 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2158 if (size == 64)
2159 {
2160 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2161 p != rd->relocs.end();
2162 ++p)
2163 {
2164 if (p->data_shndx == this->opd_shndx())
2165 {
ec4dbad3
AM
2166 uint64_t opd_size = this->section_size(this->opd_shndx());
2167 gold_assert(opd_size == static_cast<size_t>(opd_size));
2168 if (opd_size != 0)
2169 {
2170 this->init_opd(opd_size);
2171 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2172 rd->local_symbols->data());
2173 }
c9269dff
AM
2174 break;
2175 }
cf43a2fe
AM
2176 }
2177 }
cf43a2fe
AM
2178}
2179
b4f7960d
AM
2180// Read the symbols then set up st_other vector.
2181
2182template<int size, bool big_endian>
2183void
2184Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2185{
f35c4853 2186 this->base_read_symbols(sd);
b4f7960d
AM
2187 if (size == 64)
2188 {
2189 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2190 const unsigned char* const pshdrs = sd->section_headers->data();
2191 const unsigned int loccount = this->do_local_symbol_count();
2192 if (loccount != 0)
2193 {
2194 this->st_other_.resize(loccount);
2195 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2196 off_t locsize = loccount * sym_size;
2197 const unsigned int symtab_shndx = this->symtab_shndx();
2198 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2199 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2200 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2201 locsize, true, false);
2202 psyms += sym_size;
2203 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2204 {
2205 elfcpp::Sym<size, big_endian> sym(psyms);
2206 unsigned char st_other = sym.get_st_other();
2207 this->st_other_[i] = st_other;
2208 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2209 {
2210 if (this->abiversion() == 0)
2211 this->set_abiversion(2);
2212 else if (this->abiversion() < 2)
2213 gold_error(_("%s: local symbol %d has invalid st_other"
2214 " for ABI version 1"),
2215 this->name().c_str(), i);
2216 }
2217 }
2218 }
2219 }
2220}
2221
2222template<int size, bool big_endian>
2223void
2224Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2225{
2226 this->e_flags_ |= ver;
2227 if (this->abiversion() != 0)
2228 {
2229 Target_powerpc<size, big_endian>* target =
2230 static_cast<Target_powerpc<size, big_endian>*>(
2231 parameters->sized_target<size, big_endian>());
2232 if (target->abiversion() == 0)
2233 target->set_abiversion(this->abiversion());
2234 else if (target->abiversion() != this->abiversion())
2235 gold_error(_("%s: ABI version %d is not compatible "
2236 "with ABI version %d output"),
2237 this->name().c_str(),
2238 this->abiversion(), target->abiversion());
2239
2240 }
2241}
2242
f35c4853 2243// Call Sized_dynobj::base_read_symbols to read the symbols then
dc3714f3
AM
2244// read .opd from a dynamic object, filling in opd_ent_ vector,
2245
2246template<int size, bool big_endian>
2247void
2248Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2249{
f35c4853 2250 this->base_read_symbols(sd);
dc3714f3
AM
2251 if (size == 64)
2252 {
2253 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2254 const unsigned char* const pshdrs = sd->section_headers->data();
2255 const unsigned char* namesu = sd->section_names->data();
2256 const char* names = reinterpret_cast<const char*>(namesu);
2257 const unsigned char* s = NULL;
2258 const unsigned char* opd;
2259 section_size_type opd_size;
2260
2261 // Find and read .opd section.
2262 while (1)
2263 {
2264 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2265 sd->section_names_size,
2266 s);
2267 if (s == NULL)
2268 return;
2269
2270 typename elfcpp::Shdr<size, big_endian> shdr(s);
2271 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2272 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2273 {
b4f7960d
AM
2274 if (this->abiversion() == 0)
2275 this->set_abiversion(1);
2276 else if (this->abiversion() > 1)
2277 gold_error(_("%s: .opd invalid in abiv%d"),
2278 this->name().c_str(), this->abiversion());
2279
dc3714f3
AM
2280 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2281 this->opd_address_ = shdr.get_sh_addr();
2282 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2283 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2284 true, false);
2285 break;
2286 }
2287 }
2288
2289 // Build set of executable sections.
2290 // Using a set is probably overkill. There is likely to be only
2291 // a few executable sections, typically .init, .text and .fini,
2292 // and they are generally grouped together.
2293 typedef std::set<Sec_info> Exec_sections;
2294 Exec_sections exec_sections;
2295 s = pshdrs;
2296 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2297 {
2298 typename elfcpp::Shdr<size, big_endian> shdr(s);
2299 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2300 && ((shdr.get_sh_flags()
2301 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2302 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2303 && shdr.get_sh_size() != 0)
2304 {
2305 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2306 shdr.get_sh_size(), i));
2307 }
2308 }
2309 if (exec_sections.empty())
2310 return;
2311
2312 // Look over the OPD entries. This is complicated by the fact
2313 // that some binaries will use two-word entries while others
2314 // will use the standard three-word entries. In most cases
2315 // the third word (the environment pointer for languages like
2316 // Pascal) is unused and will be zero. If the third word is
2317 // used it should not be pointing into executable sections,
2318 // I think.
2319 this->init_opd(opd_size);
2320 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2321 {
2322 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2323 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2324 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2325 if (val == 0)
2326 // Chances are that this is the third word of an OPD entry.
2327 continue;
2328 typename Exec_sections::const_iterator e
2329 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2330 if (e != exec_sections.begin())
2331 {
2332 --e;
2333 if (e->start <= val && val < e->start + e->len)
2334 {
2335 // We have an address in an executable section.
2336 // VAL ought to be the function entry, set it up.
2337 this->set_opd_ent(p - opd, e->shndx, val);
2338 // Skip second word of OPD entry, the TOC pointer.
2339 p += 8;
2340 }
2341 }
2342 // If we didn't match any executable sections, we likely
2343 // have a non-zero third word in the OPD entry.
2344 }
2345 }
2346}
2347
5edad15d
AM
2348// Relocate sections.
2349
2350template<int size, bool big_endian>
2351void
2352Powerpc_relobj<size, big_endian>::do_relocate_sections(
2353 const Symbol_table* symtab, const Layout* layout,
2354 const unsigned char* pshdrs, Output_file* of,
2355 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2356{
2357 unsigned int start = 1;
2358 if (size == 64
2359 && this->relatoc_ != 0
2360 && !parameters->options().relocatable())
2361 {
2362 // Relocate .toc first.
2363 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2364 this->relatoc_, this->relatoc_);
2365 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2366 1, this->relatoc_ - 1);
2367 start = this->relatoc_ + 1;
2368 }
2369 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2370 start, this->shnum() - 1);
2371}
2372
f43ba157 2373// Set up some symbols.
26a4e9cb
AM
2374
2375template<int size, bool big_endian>
2376void
f43ba157
AM
2377Target_powerpc<size, big_endian>::do_define_standard_symbols(
2378 Symbol_table* symtab,
2379 Layout* layout)
26a4e9cb
AM
2380{
2381 if (size == 32)
2382 {
bb66a627
AM
2383 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2384 // undefined when scanning relocs (and thus requires
26a4e9cb
AM
2385 // non-relative dynamic relocs). The proper value will be
2386 // updated later.
2387 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2388 if (gotsym != NULL && gotsym->is_undefined())
2389 {
2390 Target_powerpc<size, big_endian>* target =
2391 static_cast<Target_powerpc<size, big_endian>*>(
2392 parameters->sized_target<size, big_endian>());
2393 Output_data_got_powerpc<size, big_endian>* got
2394 = target->got_section(symtab, layout);
2395 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2396 Symbol_table::PREDEFINED,
2397 got, 0, 0,
2398 elfcpp::STT_OBJECT,
bb66a627 2399 elfcpp::STB_LOCAL,
26a4e9cb
AM
2400 elfcpp::STV_HIDDEN, 0,
2401 false, false);
2402 }
2403
2404 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2405 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2406 if (sdasym != NULL && sdasym->is_undefined())
2407 {
2408 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2409 Output_section* os
2410 = layout->add_output_section_data(".sdata", 0,
2411 elfcpp::SHF_ALLOC
2412 | elfcpp::SHF_WRITE,
2413 sdata, ORDER_SMALL_DATA, false);
2414 symtab->define_in_output_data("_SDA_BASE_", NULL,
2415 Symbol_table::PREDEFINED,
2416 os, 32768, 0, elfcpp::STT_OBJECT,
2417 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2418 0, false, false);
2419 }
2420 }
b4f7960d
AM
2421 else
2422 {
2423 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2424 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2425 if (gotsym != NULL && gotsym->is_undefined())
2426 {
2427 Target_powerpc<size, big_endian>* target =
2428 static_cast<Target_powerpc<size, big_endian>*>(
2429 parameters->sized_target<size, big_endian>());
2430 Output_data_got_powerpc<size, big_endian>* got
2431 = target->got_section(symtab, layout);
2432 symtab->define_in_output_data(".TOC.", NULL,
2433 Symbol_table::PREDEFINED,
2434 got, 0x8000, 0,
2435 elfcpp::STT_OBJECT,
2436 elfcpp::STB_LOCAL,
2437 elfcpp::STV_HIDDEN, 0,
2438 false, false);
2439 }
2440 }
26a4e9cb
AM
2441}
2442
cf43a2fe
AM
2443// Set up PowerPC target specific relobj.
2444
2445template<int size, bool big_endian>
2446Object*
2447Target_powerpc<size, big_endian>::do_make_elf_object(
2448 const std::string& name,
2449 Input_file* input_file,
2450 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2451{
2452 int et = ehdr.get_e_type();
957564c9
AS
2453 // ET_EXEC files are valid input for --just-symbols/-R,
2454 // and we treat them as relocatable objects.
2455 if (et == elfcpp::ET_REL
2456 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
cf43a2fe
AM
2457 {
2458 Powerpc_relobj<size, big_endian>* obj =
c9269dff 2459 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2460 obj->setup();
2461 return obj;
2462 }
2463 else if (et == elfcpp::ET_DYN)
2464 {
dc3714f3
AM
2465 Powerpc_dynobj<size, big_endian>* obj =
2466 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
cf43a2fe
AM
2467 obj->setup();
2468 return obj;
2469 }
2470 else
2471 {
c9269dff 2472 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
cf43a2fe
AM
2473 return NULL;
2474 }
2475}
2476
2477template<int size, bool big_endian>
2478class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2479{
2480public:
2481 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2482 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2483
2484 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2485 : Output_data_got<size, big_endian>(),
2486 symtab_(symtab), layout_(layout),
2487 header_ent_cnt_(size == 32 ? 3 : 1),
2488 header_index_(size == 32 ? 0x2000 : 0)
751e4d66
AM
2489 {
2490 if (size == 64)
2491 this->set_addralign(256);
2492 }
cf43a2fe 2493
e84fe78f
AM
2494 // Override all the Output_data_got methods we use so as to first call
2495 // reserve_ent().
2496 bool
2497 add_global(Symbol* gsym, unsigned int got_type)
2498 {
2499 this->reserve_ent();
2500 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2501 }
2502
2503 bool
2504 add_global_plt(Symbol* gsym, unsigned int got_type)
2505 {
2506 this->reserve_ent();
2507 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2508 }
2509
2510 bool
2511 add_global_tls(Symbol* gsym, unsigned int got_type)
2512 { return this->add_global_plt(gsym, got_type); }
2513
2514 void
2515 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2516 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2517 {
2518 this->reserve_ent();
2519 Output_data_got<size, big_endian>::
2520 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2521 }
2522
2523 void
2524 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2525 Output_data_reloc_generic* rel_dyn,
2526 unsigned int r_type_1, unsigned int r_type_2)
2527 {
2528 this->reserve_ent(2);
2529 Output_data_got<size, big_endian>::
2530 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2531 }
2532
2533 bool
2534 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2535 {
2536 this->reserve_ent();
2537 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2538 got_type);
2539 }
2540
2541 bool
2542 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2543 {
2544 this->reserve_ent();
2545 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2546 got_type);
2547 }
2548
2549 bool
2550 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2551 { return this->add_local_plt(object, sym_index, got_type); }
2552
2553 void
2554 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2555 unsigned int got_type,
2556 Output_data_reloc_generic* rel_dyn,
2557 unsigned int r_type)
2558 {
2559 this->reserve_ent(2);
2560 Output_data_got<size, big_endian>::
2561 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2562 }
2563
2564 unsigned int
2565 add_constant(Valtype constant)
2566 {
2567 this->reserve_ent();
2568 return Output_data_got<size, big_endian>::add_constant(constant);
2569 }
2570
dd93cd0a
AM
2571 unsigned int
2572 add_constant_pair(Valtype c1, Valtype c2)
2573 {
2574 this->reserve_ent(2);
e84fe78f 2575 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
dd93cd0a
AM
2576 }
2577
2578 // Offset of _GLOBAL_OFFSET_TABLE_.
cf43a2fe
AM
2579 unsigned int
2580 g_o_t() const
2581 {
2582 return this->got_offset(this->header_index_);
42cacb20 2583 }
cf43a2fe 2584
dd93cd0a
AM
2585 // Offset of base used to access the GOT/TOC.
2586 // The got/toc pointer reg will be set to this value.
26a4e9cb 2587 Valtype
dd93cd0a
AM
2588 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2589 {
2590 if (size == 32)
2591 return this->g_o_t();
2592 else
2593 return (this->output_section()->address()
2594 + object->toc_base_offset()
2595 - this->address());
2596 }
2597
cf43a2fe
AM
2598 // Ensure our GOT has a header.
2599 void
2600 set_final_data_size()
2601 {
2602 if (this->header_ent_cnt_ != 0)
2603 this->make_header();
2604 Output_data_got<size, big_endian>::set_final_data_size();
2605 }
2606
2607 // First word of GOT header needs some values that are not
2608 // handled by Output_data_got so poke them in here.
2609 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2610 void
2611 do_write(Output_file* of)
2612 {
c9824451
AM
2613 Valtype val = 0;
2614 if (size == 32 && this->layout_->dynamic_data() != NULL)
2615 val = this->layout_->dynamic_section()->address();
2616 if (size == 64)
2617 val = this->output_section()->address() + 0x8000;
2618 this->replace_constant(this->header_index_, val);
cf43a2fe
AM
2619 Output_data_got<size, big_endian>::do_write(of);
2620 }
2621
2622private:
2623 void
2624 reserve_ent(unsigned int cnt = 1)
2625 {
2626 if (this->header_ent_cnt_ == 0)
2627 return;
2628 if (this->num_entries() + cnt > this->header_index_)
2629 this->make_header();
2630 }
2631
2632 void
2633 make_header()
2634 {
2635 this->header_ent_cnt_ = 0;
2636 this->header_index_ = this->num_entries();
2637 if (size == 32)
2638 {
2639 Output_data_got<size, big_endian>::add_constant(0);
2640 Output_data_got<size, big_endian>::add_constant(0);
2641 Output_data_got<size, big_endian>::add_constant(0);
2642
2643 // Define _GLOBAL_OFFSET_TABLE_ at the header
bb66a627
AM
2644 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2645 if (gotsym != NULL)
2646 {
2647 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2648 sym->set_value(this->g_o_t());
2649 }
2650 else
2651 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2652 Symbol_table::PREDEFINED,
2653 this, this->g_o_t(), 0,
2654 elfcpp::STT_OBJECT,
2655 elfcpp::STB_LOCAL,
2656 elfcpp::STV_HIDDEN, 0,
2657 false, false);
cf43a2fe
AM
2658 }
2659 else
2660 Output_data_got<size, big_endian>::add_constant(0);
2661 }
2662
2663 // Stashed pointers.
2664 Symbol_table* symtab_;
2665 Layout* layout_;
2666
2667 // GOT header size.
2668 unsigned int header_ent_cnt_;
2669 // GOT header index.
2670 unsigned int header_index_;
42cacb20
DE
2671};
2672
2673// Get the GOT section, creating it if necessary.
2674
2675template<int size, bool big_endian>
cf43a2fe 2676Output_data_got_powerpc<size, big_endian>*
42cacb20
DE
2677Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2678 Layout* layout)
2679{
2680 if (this->got_ == NULL)
2681 {
2682 gold_assert(symtab != NULL && layout != NULL);
2683
cf43a2fe
AM
2684 this->got_
2685 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
42cacb20
DE
2686
2687 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2688 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
22f0da72 2689 this->got_, ORDER_DATA, false);
42cacb20
DE
2690 }
2691
2692 return this->got_;
2693}
2694
2695// Get the dynamic reloc section, creating it if necessary.
2696
2697template<int size, bool big_endian>
2698typename Target_powerpc<size, big_endian>::Reloc_section*
2699Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2700{
2701 if (this->rela_dyn_ == NULL)
2702 {
2703 gold_assert(layout != NULL);
2704 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2705 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
22f0da72
ILT
2706 elfcpp::SHF_ALLOC, this->rela_dyn_,
2707 ORDER_DYNAMIC_RELOCS, false);
42cacb20
DE
2708 }
2709 return this->rela_dyn_;
2710}
2711
b3ccdeb5
AM
2712// Similarly, but for ifunc symbols get the one for ifunc.
2713
2714template<int size, bool big_endian>
2715typename Target_powerpc<size, big_endian>::Reloc_section*
2716Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2717 Layout* layout,
2718 bool for_ifunc)
2719{
2720 if (!for_ifunc)
2721 return this->rela_dyn_section(layout);
2722
2723 if (this->iplt_ == NULL)
2724 this->make_iplt_section(symtab, layout);
2725 return this->iplt_->rel_plt();
2726}
2727
ec661b9d
AM
2728class Stub_control
2729{
2730 public:
2731 // Determine the stub group size. The group size is the absolute
2732 // value of the parameter --stub-group-size. If --stub-group-size
a5018ae5 2733 // is passed a negative value, we restrict stubs to be always after
ec661b9d 2734 // the stubbed branches.
1c3a5fbe
AM
2735 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
2736 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
2737 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
2738 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
2739 owner_(NULL), output_section_(NULL)
ec661b9d 2740 {
ec661b9d
AM
2741 }
2742
2743 // Return true iff input section can be handled by current stub
2744 // group.
2745 bool
2746 can_add_to_stub_group(Output_section* o,
2747 const Output_section::Input_section* i,
2748 bool has14);
2749
2750 const Output_section::Input_section*
2751 owner()
2752 { return owner_; }
2753
2754 Output_section*
2755 output_section()
2756 { return output_section_; }
2757
a20605cf
AM
2758 void
2759 set_output_and_owner(Output_section* o,
2760 const Output_section::Input_section* i)
2761 {
2762 this->output_section_ = o;
2763 this->owner_ = i;
2764 }
2765
ec661b9d
AM
2766 private:
2767 typedef enum
2768 {
1c3a5fbe 2769 // Initial state.
ec661b9d 2770 NO_GROUP,
1c3a5fbe 2771 // Adding group sections before the stubs.
ec661b9d 2772 FINDING_STUB_SECTION,
1c3a5fbe 2773 // Adding group sections after the stubs.
ec661b9d
AM
2774 HAS_STUB_SECTION
2775 } State;
2776
ec661b9d 2777 uint32_t stub_group_size_;
a5018ae5 2778 bool stubs_always_after_branch_;
ec661b9d 2779 bool suppress_size_errors_;
1c3a5fbe
AM
2780 // True if a stub group can serve multiple output sections.
2781 bool multi_os_;
2782 State state_;
8a37735f
AM
2783 // Current max size of group. Starts at stub_group_size_ but is
2784 // reduced to stub_group_size_/1024 on seeing a section with
2785 // external conditional branches.
2786 uint32_t group_size_;
a5018ae5 2787 uint64_t group_start_addr_;
57f6d32d
AM
2788 // owner_ and output_section_ specify the section to which stubs are
2789 // attached. The stubs are placed at the end of this section.
ec661b9d
AM
2790 const Output_section::Input_section* owner_;
2791 Output_section* output_section_;
2792};
2793
0cfdc767 2794// Return true iff input section can be handled by current stub
a5018ae5
AM
2795// group. Sections are presented to this function in order,
2796// so the first section is the head of the group.
ec661b9d
AM
2797
2798bool
2799Stub_control::can_add_to_stub_group(Output_section* o,
2800 const Output_section::Input_section* i,
2801 bool has14)
2802{
ec661b9d
AM
2803 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2804 uint64_t this_size;
2805 uint64_t start_addr = o->address();
2806
2807 if (whole_sec)
2808 // .init and .fini sections are pasted together to form a single
2809 // function. We can't be adding stubs in the middle of the function.
2810 this_size = o->data_size();
2811 else
2812 {
2813 start_addr += i->relobj()->output_section_offset(i->shndx());
2814 this_size = i->data_size();
2815 }
57f6d32d 2816
a5018ae5 2817 uint64_t end_addr = start_addr + this_size;
8a37735f
AM
2818 uint32_t group_size = this->stub_group_size_;
2819 if (has14)
2820 this->group_size_ = group_size = group_size >> 10;
ec661b9d 2821
57f6d32d 2822 if (this_size > group_size && !this->suppress_size_errors_)
ec661b9d
AM
2823 gold_warning(_("%s:%s exceeds group size"),
2824 i->relobj()->name().c_str(),
2825 i->relobj()->section_name(i->shndx()).c_str());
2826
afe002dd
AM
2827 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2828 has14 ? " 14bit" : "",
2829 i->relobj()->name().c_str(),
2830 i->relobj()->section_name(i->shndx()).c_str(),
2831 (long long) this_size,
a5018ae5
AM
2832 (this->state_ == NO_GROUP
2833 ? this_size
2834 : (long long) end_addr - this->group_start_addr_));
afe002dd 2835
1c3a5fbe
AM
2836 if (this->state_ == NO_GROUP)
2837 {
2838 // Only here on very first use of Stub_control
2839 this->owner_ = i;
2840 this->output_section_ = o;
2841 this->state_ = FINDING_STUB_SECTION;
2842 this->group_size_ = group_size;
2843 this->group_start_addr_ = start_addr;
2844 return true;
2845 }
2846 else if (!this->multi_os_ && this->output_section_ != o)
2847 ;
2848 else if (this->state_ == HAS_STUB_SECTION)
ec661b9d 2849 {
a5018ae5 2850 // Can we add this section, which is after the stubs, to the
57f6d32d 2851 // group?
a5018ae5 2852 if (end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2853 return true;
ec661b9d 2854 }
a5018ae5 2855 else if (this->state_ == FINDING_STUB_SECTION)
ec661b9d 2856 {
a5018ae5
AM
2857 if ((whole_sec && this->output_section_ == o)
2858 || end_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2859 {
a5018ae5 2860 // Stubs are added at the end of "owner_".
57f6d32d
AM
2861 this->owner_ = i;
2862 this->output_section_ = o;
a5018ae5 2863 return true;
57f6d32d 2864 }
a5018ae5
AM
2865 // The group before the stubs has reached maximum size.
2866 // Now see about adding sections after the stubs to the
2867 // group. If the current section has a 14-bit branch and
2868 // the group before the stubs exceeds group_size_ (because
2869 // they didn't have 14-bit branches), don't add sections
2870 // after the stubs: The size of stubs for such a large
2871 // group may exceed the reach of a 14-bit branch.
2872 if (!this->stubs_always_after_branch_
2873 && this_size <= this->group_size_
2874 && start_addr - this->group_start_addr_ <= this->group_size_)
57f6d32d 2875 {
a5018ae5
AM
2876 gold_debug(DEBUG_TARGET, "adding after stubs");
2877 this->state_ = HAS_STUB_SECTION;
2878 this->group_start_addr_ = start_addr;
57f6d32d
AM
2879 return true;
2880 }
ec661b9d 2881 }
a5018ae5
AM
2882 else
2883 gold_unreachable();
57f6d32d 2884
1c3a5fbe
AM
2885 gold_debug(DEBUG_TARGET,
2886 !this->multi_os_ && this->output_section_ != o
2887 ? "nope, new output section\n"
2888 : "nope, didn't fit\n");
afe002dd 2889
57f6d32d
AM
2890 // The section fails to fit in the current group. Set up a few
2891 // things for the next group. owner_ and output_section_ will be
2892 // set later after we've retrieved those values for the current
2893 // group.
2894 this->state_ = FINDING_STUB_SECTION;
8a37735f 2895 this->group_size_ = group_size;
a5018ae5 2896 this->group_start_addr_ = start_addr;
57f6d32d 2897 return false;
ec661b9d
AM
2898}
2899
2900// Look over all the input sections, deciding where to place stubs.
2901
2902template<int size, bool big_endian>
2903void
2904Target_powerpc<size, big_endian>::group_sections(Layout* layout,
a3e60ddb
AM
2905 const Task*,
2906 bool no_size_errors)
ec661b9d 2907{
1c3a5fbe
AM
2908 Stub_control stub_control(this->stub_group_size_, no_size_errors,
2909 parameters->options().stub_group_multi());
ec661b9d
AM
2910
2911 // Group input sections and insert stub table
a3e60ddb
AM
2912 Stub_table_owner* table_owner = NULL;
2913 std::vector<Stub_table_owner*> tables;
ec661b9d
AM
2914 Layout::Section_list section_list;
2915 layout->get_executable_sections(&section_list);
2916 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
a5018ae5
AM
2917 for (Layout::Section_list::iterator o = section_list.begin();
2918 o != section_list.end();
ec661b9d
AM
2919 ++o)
2920 {
2921 typedef Output_section::Input_section_list Input_section_list;
a5018ae5
AM
2922 for (Input_section_list::const_iterator i
2923 = (*o)->input_sections().begin();
2924 i != (*o)->input_sections().end();
ec661b9d
AM
2925 ++i)
2926 {
a3e60ddb
AM
2927 if (i->is_input_section()
2928 || i->is_relaxed_input_section())
ec661b9d
AM
2929 {
2930 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2931 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2932 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2933 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2934 {
a3e60ddb
AM
2935 table_owner->output_section = stub_control.output_section();
2936 table_owner->owner = stub_control.owner();
a20605cf 2937 stub_control.set_output_and_owner(*o, &*i);
a3e60ddb 2938 table_owner = NULL;
ec661b9d 2939 }
a3e60ddb
AM
2940 if (table_owner == NULL)
2941 {
2942 table_owner = new Stub_table_owner;
2943 tables.push_back(table_owner);
2944 }
2945 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
ec661b9d
AM
2946 }
2947 }
2948 }
a3e60ddb 2949 if (table_owner != NULL)
0cfdc767 2950 {
a5018ae5
AM
2951 table_owner->output_section = stub_control.output_section();
2952 table_owner->owner = stub_control.owner();;
a3e60ddb
AM
2953 }
2954 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
2955 t != tables.end();
2956 ++t)
2957 {
2958 Stub_table<size, big_endian>* stub_table;
2959
2960 if ((*t)->owner->is_input_section())
2961 stub_table = new Stub_table<size, big_endian>(this,
2962 (*t)->output_section,
590b87ff
AM
2963 (*t)->owner,
2964 this->stub_tables_.size());
a3e60ddb
AM
2965 else if ((*t)->owner->is_relaxed_input_section())
2966 stub_table = static_cast<Stub_table<size, big_endian>*>(
2967 (*t)->owner->relaxed_input_section());
0cfdc767 2968 else
a3e60ddb
AM
2969 gold_unreachable();
2970 this->stub_tables_.push_back(stub_table);
2971 delete *t;
0cfdc767 2972 }
ec661b9d
AM
2973}
2974
a3e60ddb
AM
2975static unsigned long
2976max_branch_delta (unsigned int r_type)
2977{
2978 if (r_type == elfcpp::R_POWERPC_REL14
2979 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
2980 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2981 return 1L << 15;
2982 if (r_type == elfcpp::R_POWERPC_REL24
2983 || r_type == elfcpp::R_PPC_PLTREL24
2984 || r_type == elfcpp::R_PPC_LOCAL24PC)
2985 return 1L << 25;
2986 return 0;
2987}
2988
7e57d19e
AM
2989// Return whether this branch is going via a plt call stub.
2990
2991template<int size, bool big_endian>
2992bool
2993Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
2994 Powerpc_relobj<size, big_endian>* ppc_object,
2995 unsigned int shndx,
2996 Address offset,
2997 Target_powerpc* target,
2998 Symbol_table* symtab)
2999{
3000 if (this->object_ != ppc_object
3001 || this->shndx_ != shndx
3002 || this->offset_ != offset)
3003 return false;
3004
3005 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3006 if (sym != NULL && sym->is_forwarder())
3007 sym = symtab->resolve_forwards(sym);
3008 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3009 if (gsym != NULL
7ee7ff70
AM
3010 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3011 && !target->is_elfv2_localentry0(gsym))
3012 : (this->object_->local_has_plt_offset(this->r_sym_)
3013 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
7e57d19e
AM
3014 {
3015 this->tocsave_ = 1;
3016 return true;
3017 }
3018 return false;
3019}
3020
ec661b9d
AM
3021// If this branch needs a plt call stub, or a long branch stub, make one.
3022
3023template<int size, bool big_endian>
a3e60ddb 3024bool
ec661b9d
AM
3025Target_powerpc<size, big_endian>::Branch_info::make_stub(
3026 Stub_table<size, big_endian>* stub_table,
3027 Stub_table<size, big_endian>* ifunc_stub_table,
3028 Symbol_table* symtab) const
3029{
3030 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3031 if (sym != NULL && sym->is_forwarder())
3032 sym = symtab->resolve_forwards(sym);
3033 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
88b8e639
AM
3034 Target_powerpc<size, big_endian>* target =
3035 static_cast<Target_powerpc<size, big_endian>*>(
3036 parameters->sized_target<size, big_endian>());
dc60b26d
AM
3037 bool ok = true;
3038
ec661b9d 3039 if (gsym != NULL
88b8e639 3040 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
ec661b9d
AM
3041 : this->object_->local_has_plt_offset(this->r_sym_))
3042 {
9055360d
AM
3043 if (size == 64
3044 && gsym != NULL
3045 && target->abiversion() >= 2
3046 && !parameters->options().output_is_position_independent()
3047 && !is_branch_reloc(this->r_type_))
3048 target->glink_section()->add_global_entry(gsym);
3049 else
ec661b9d 3050 {
9055360d
AM
3051 if (stub_table == NULL)
3052 stub_table = this->object_->stub_table(this->shndx_);
3053 if (stub_table == NULL)
3054 {
3055 // This is a ref from a data section to an ifunc symbol.
3056 stub_table = ifunc_stub_table;
3057 }
3058 gold_assert(stub_table != NULL);
a3e60ddb
AM
3059 Address from = this->object_->get_output_section_offset(this->shndx_);
3060 if (from != invalid_address)
3061 from += (this->object_->output_section(this->shndx_)->address()
3062 + this->offset_);
9055360d 3063 if (gsym != NULL)
dc60b26d
AM
3064 ok = stub_table->add_plt_call_entry(from,
3065 this->object_, gsym,
7e57d19e
AM
3066 this->r_type_, this->addend_,
3067 this->tocsave_);
9055360d 3068 else
dc60b26d
AM
3069 ok = stub_table->add_plt_call_entry(from,
3070 this->object_, this->r_sym_,
7e57d19e
AM
3071 this->r_type_, this->addend_,
3072 this->tocsave_);
ec661b9d 3073 }
ec661b9d
AM
3074 }
3075 else
3076 {
cbcb23fa 3077 Address max_branch_offset = max_branch_delta(this->r_type_);
a3e60ddb
AM
3078 if (max_branch_offset == 0)
3079 return true;
ec661b9d
AM
3080 Address from = this->object_->get_output_section_offset(this->shndx_);
3081 gold_assert(from != invalid_address);
3082 from += (this->object_->output_section(this->shndx_)->address()
3083 + this->offset_);
3084 Address to;
3085 if (gsym != NULL)
3086 {
3087 switch (gsym->source())
3088 {
3089 case Symbol::FROM_OBJECT:
3090 {
3091 Object* symobj = gsym->object();
3092 if (symobj->is_dynamic()
3093 || symobj->pluginobj() != NULL)
a3e60ddb 3094 return true;
ec661b9d
AM
3095 bool is_ordinary;
3096 unsigned int shndx = gsym->shndx(&is_ordinary);
3097 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3098 return true;
ec661b9d
AM
3099 }
3100 break;
3101
3102 case Symbol::IS_UNDEFINED:
a3e60ddb 3103 return true;
ec661b9d
AM
3104
3105 default:
3106 break;
3107 }
3108 Symbol_table::Compute_final_value_status status;
3109 to = symtab->compute_final_value<size>(gsym, &status);
3110 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3111 return true;
9055360d
AM
3112 if (size == 64)
3113 to += this->object_->ppc64_local_entry_offset(gsym);
ec661b9d
AM
3114 }
3115 else
3116 {
3117 const Symbol_value<size>* psymval
3118 = this->object_->local_symbol(this->r_sym_);
3119 Symbol_value<size> symval;
0f125432
CC
3120 if (psymval->is_section_symbol())
3121 symval.set_is_section_symbol();
ec661b9d
AM
3122 typedef Sized_relobj_file<size, big_endian> ObjType;
3123 typename ObjType::Compute_final_local_value_status status
3124 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3125 &symval, symtab);
3126 if (status != ObjType::CFLV_OK
3127 || !symval.has_output_value())
a3e60ddb 3128 return true;
ec661b9d 3129 to = symval.value(this->object_, 0);
9055360d
AM
3130 if (size == 64)
3131 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
ec661b9d 3132 }
cbcb23fa
AM
3133 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3134 to += this->addend_;
ec661b9d
AM
3135 if (stub_table == NULL)
3136 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3137 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3138 {
3139 unsigned int dest_shndx;
1611bc4a
AM
3140 if (!target->symval_for_branch(symtab, gsym, this->object_,
3141 &to, &dest_shndx))
3142 return true;
ec661b9d
AM
3143 }
3144 Address delta = to - from;
3145 if (delta + max_branch_offset >= 2 * max_branch_offset)
3146 {
0cfdc767
AM
3147 if (stub_table == NULL)
3148 {
3149 gold_warning(_("%s:%s: branch in non-executable section,"
3150 " no long branch stub for you"),
3151 this->object_->name().c_str(),
3152 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3153 return true;
0cfdc767 3154 }
d49044c7
AM
3155 bool save_res = (size == 64
3156 && gsym != NULL
3157 && gsym->source() == Symbol::IN_OUTPUT_DATA
3158 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3159 ok = stub_table->add_long_branch_entry(this->object_,
3160 this->r_type_,
3161 from, to, save_res);
ec661b9d
AM
3162 }
3163 }
dc60b26d
AM
3164 if (!ok)
3165 gold_debug(DEBUG_TARGET,
3166 "branch at %s:%s+%#lx\n"
3167 "can't reach stub attached to %s:%s",
3168 this->object_->name().c_str(),
3169 this->object_->section_name(this->shndx_).c_str(),
3170 (unsigned long) this->offset_,
3171 stub_table->relobj()->name().c_str(),
3172 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3173
3174 return ok;
ec661b9d
AM
3175}
3176
3177// Relaxation hook. This is where we do stub generation.
3178
3179template<int size, bool big_endian>
3180bool
3181Target_powerpc<size, big_endian>::do_relax(int pass,
3182 const Input_objects*,
3183 Symbol_table* symtab,
3184 Layout* layout,
3185 const Task* task)
3186{
3187 unsigned int prev_brlt_size = 0;
3188 if (pass == 1)
ec661b9d 3189 {
b4f7960d
AM
3190 bool thread_safe
3191 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3192 if (size == 64
3193 && this->abiversion() < 2
3194 && !thread_safe
3195 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3196 {
e2458743 3197 static const char* const thread_starter[] =
9e69ed50
AM
3198 {
3199 "pthread_create",
3200 /* libstdc++ */
3201 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3202 /* librt */
3203 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3204 "mq_notify", "create_timer",
3205 /* libanl */
3206 "getaddrinfo_a",
3207 /* libgomp */
80272b8c 3208 "GOMP_parallel",
9e69ed50 3209 "GOMP_parallel_start",
80272b8c 3210 "GOMP_parallel_loop_static",
9e69ed50 3211 "GOMP_parallel_loop_static_start",
80272b8c 3212 "GOMP_parallel_loop_dynamic",
9e69ed50 3213 "GOMP_parallel_loop_dynamic_start",
80272b8c 3214 "GOMP_parallel_loop_guided",
9e69ed50 3215 "GOMP_parallel_loop_guided_start",
80272b8c 3216 "GOMP_parallel_loop_runtime",
9e69ed50 3217 "GOMP_parallel_loop_runtime_start",
80272b8c 3218 "GOMP_parallel_sections",
43819297 3219 "GOMP_parallel_sections_start",
f9dffbf0
AM
3220 /* libgo */
3221 "__go_go",
9e69ed50
AM
3222 };
3223
e2458743
AM
3224 if (parameters->options().shared())
3225 thread_safe = true;
3226 else
9e69ed50 3227 {
e2458743
AM
3228 for (unsigned int i = 0;
3229 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3230 i++)
3231 {
3232 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3233 thread_safe = (sym != NULL
3234 && sym->in_reg()
3235 && sym->in_real_elf());
3236 if (thread_safe)
3237 break;
3238 }
9e69ed50 3239 }
ec661b9d 3240 }
9e69ed50 3241 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3242 }
3243
3244 if (pass == 1)
3245 {
3246 this->stub_group_size_ = parameters->options().stub_group_size();
3247 bool no_size_errors = true;
3248 if (this->stub_group_size_ == 1)
3249 this->stub_group_size_ = 0x1c00000;
3250 else if (this->stub_group_size_ == -1)
3251 this->stub_group_size_ = -0x1e00000;
3252 else
3253 no_size_errors = false;
3254 this->group_sections(layout, task, no_size_errors);
3255 }
3256 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3257 {
3258 this->branch_lookup_table_.clear();
3259 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3260 p != this->stub_tables_.end();
3261 ++p)
3262 {
3263 (*p)->clear_stubs(true);
3264 }
3265 this->stub_tables_.clear();
3266 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3267 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3268 program_name, this->stub_group_size_);
3269 this->group_sections(layout, task, true);
ec661b9d
AM
3270 }
3271
3272 // We need address of stub tables valid for make_stub.
3273 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3274 p != this->stub_tables_.end();
3275 ++p)
3276 {
3277 const Powerpc_relobj<size, big_endian>* object
3278 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3279 Address off = object->get_output_section_offset((*p)->shndx());
3280 gold_assert(off != invalid_address);
3281 Output_section* os = (*p)->output_section();
3282 (*p)->set_address_and_size(os, off);
3283 }
3284
9e69ed50
AM
3285 if (pass != 1)
3286 {
3287 // Clear plt call stubs, long branch stubs and branch lookup table.
3288 prev_brlt_size = this->branch_lookup_table_.size();
3289 this->branch_lookup_table_.clear();
3290 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3291 p != this->stub_tables_.end();
3292 ++p)
3293 {
a3e60ddb 3294 (*p)->clear_stubs(false);
9e69ed50
AM
3295 }
3296 }
3297
3298 // Build all the stubs.
a3e60ddb 3299 this->relax_failed_ = false;
ec661b9d
AM
3300 Stub_table<size, big_endian>* ifunc_stub_table
3301 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3302 Stub_table<size, big_endian>* one_stub_table
3303 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3304 for (typename Branches::const_iterator b = this->branch_info_.begin();
3305 b != this->branch_info_.end();
3306 b++)
3307 {
a3e60ddb
AM
3308 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3309 && !this->relax_failed_)
3310 {
3311 this->relax_failed_ = true;
3312 this->relax_fail_count_++;
3313 if (this->relax_fail_count_ < 3)
3314 return true;
3315 }
ec661b9d
AM
3316 }
3317
9e69ed50 3318 // Did anything change size?
ec661b9d
AM
3319 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3320 bool again = num_huge_branches != prev_brlt_size;
3321 if (size == 64 && num_huge_branches != 0)
3322 this->make_brlt_section(layout);
3323 if (size == 64 && again)
3324 this->brlt_section_->set_current_size(num_huge_branches);
3325
3326 typedef Unordered_set<Output_section*> Output_sections;
3327 Output_sections os_need_update;
3328 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3329 p != this->stub_tables_.end();
3330 ++p)
3331 {
3332 if ((*p)->size_update())
3333 {
3334 again = true;
9d5781f8 3335 (*p)->add_eh_frame(layout);
ec661b9d
AM
3336 os_need_update.insert((*p)->output_section());
3337 }
3338 }
3339
9e69ed50
AM
3340 // Set output section offsets for all input sections in an output
3341 // section that just changed size. Anything past the stubs will
3342 // need updating.
ec661b9d
AM
3343 for (typename Output_sections::iterator p = os_need_update.begin();
3344 p != os_need_update.end();
3345 p++)
3346 {
3347 Output_section* os = *p;
3348 Address off = 0;
3349 typedef Output_section::Input_section_list Input_section_list;
3350 for (Input_section_list::const_iterator i = os->input_sections().begin();
3351 i != os->input_sections().end();
3352 ++i)
3353 {
3354 off = align_address(off, i->addralign());
3355 if (i->is_input_section() || i->is_relaxed_input_section())
3356 i->relobj()->set_section_offset(i->shndx(), off);
3357 if (i->is_relaxed_input_section())
3358 {
3359 Stub_table<size, big_endian>* stub_table
3360 = static_cast<Stub_table<size, big_endian>*>(
3361 i->relaxed_input_section());
6395d38b
HS
3362 Address stub_table_size = stub_table->set_address_and_size(os, off);
3363 off += stub_table_size;
3364 // After a few iterations, set current stub table size
3365 // as min size threshold, so later stub tables can only
3366 // grow in size.
3367 if (pass >= 4)
3368 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3369 }
3370 else
3371 off += i->data_size();
3372 }
6830ee24
AM
3373 // If .branch_lt is part of this output section, then we have
3374 // just done the offset adjustment.
ec661b9d
AM
3375 os->clear_section_offsets_need_adjustment();
3376 }
3377
3378 if (size == 64
3379 && !again
3380 && num_huge_branches != 0
3381 && parameters->options().output_is_position_independent())
3382 {
3383 // Fill in the BRLT relocs.
06f30c9d 3384 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3385 for (typename Branch_lookup_table::const_iterator p
3386 = this->branch_lookup_table_.begin();
3387 p != this->branch_lookup_table_.end();
3388 ++p)
3389 {
3390 this->brlt_section_->add_reloc(p->first, p->second);
3391 }
06f30c9d 3392 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3393 }
590b87ff
AM
3394
3395 if (!again
3396 && (parameters->options().user_set_emit_stub_syms()
3397 ? parameters->options().emit_stub_syms()
3398 : (size == 64
3399 || parameters->options().output_is_position_independent()
3400 || parameters->options().emit_relocs())))
3401 {
3402 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3403 p != this->stub_tables_.end();
3404 ++p)
3405 (*p)->define_stub_syms(symtab);
3406
3407 if (this->glink_ != NULL)
3408 {
3409 int stub_size = this->glink_->pltresolve_size;
3410 Address value = -stub_size;
3411 if (size == 64)
3412 {
3413 value = 8;
3414 stub_size -= 8;
3415 }
3416 this->define_local(symtab, "__glink_PLTresolve",
3417 this->glink_, value, stub_size);
3418
3419 if (size != 64)
3420 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3421 }
3422 }
3423
ec661b9d
AM
3424 return again;
3425}
3426
9d5781f8
AM
3427template<int size, bool big_endian>
3428void
3429Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3430 unsigned char* oview,
3431 uint64_t* paddress,
3432 off_t* plen) const
3433{
3434 uint64_t address = plt->address();
3435 off_t len = plt->data_size();
3436
3437 if (plt == this->glink_)
3438 {
3439 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3440 if (len == 0)
3441 {
3442 gold_assert(parameters->doing_static_link());
3443 // Static linking may need stubs, to support ifunc and long
3444 // branches. We need to create an output section for
3445 // .eh_frame early in the link process, to have a place to
3446 // attach stub .eh_frame info. We also need to have
3447 // registered a CIE that matches the stub CIE. Both of
3448 // these requirements are satisfied by creating an FDE and
3449 // CIE for .glink, even though static linking will leave
3450 // .glink zero length.
3451 // ??? Hopefully generating an FDE with a zero address range
3452 // won't confuse anything that consumes .eh_frame info.
3453 }
3454 else if (size == 64)
9d5781f8
AM
3455 {
3456 // There is one word before __glink_PLTresolve
3457 address += 8;
3458 len -= 8;
3459 }
3460 else if (parameters->options().output_is_position_independent())
3461 {
3462 // There are two FDEs for a position independent glink.
3463 // The first covers the branch table, the second
3464 // __glink_PLTresolve at the end of glink.
3465 off_t resolve_size = this->glink_->pltresolve_size;
5fe7ffdc 3466 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3467 len -= resolve_size;
3468 else
3469 {
3470 address += len - resolve_size;
3471 len = resolve_size;
3472 }
3473 }
3474 }
3475 else
3476 {
3477 // Must be a stub table.
3478 const Stub_table<size, big_endian>* stub_table
3479 = static_cast<const Stub_table<size, big_endian>*>(plt);
3480 uint64_t stub_address = stub_table->stub_address();
3481 len -= stub_address - address;
3482 address = stub_address;
3483 }
3484
3485 *paddress = address;
3486 *plen = len;
3487}
3488
42cacb20
DE
3489// A class to handle the PLT data.
3490
3491template<int size, bool big_endian>
cf43a2fe 3492class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3493{
3494 public:
3495 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3496 size, big_endian> Reloc_section;
3497
e5d5f5ed
AM
3498 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3499 Reloc_section* plt_rel,
e5d5f5ed
AM
3500 const char* name)
3501 : Output_section_data_build(size == 32 ? 4 : 8),
3502 rel_(plt_rel),
3503 targ_(targ),
e5d5f5ed
AM
3504 name_(name)
3505 { }
42cacb20
DE
3506
3507 // Add an entry to the PLT.
03e25981 3508 void
cf43a2fe 3509 add_entry(Symbol*);
42cacb20 3510
03e25981 3511 void
e5d5f5ed
AM
3512 add_ifunc_entry(Symbol*);
3513
03e25981 3514 void
e5d5f5ed
AM
3515 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3516
42cacb20 3517 // Return the .rela.plt section data.
e5d5f5ed 3518 Reloc_section*
cf43a2fe
AM
3519 rel_plt() const
3520 {
42cacb20
DE
3521 return this->rel_;
3522 }
3523
0e70b911
CC
3524 // Return the number of PLT entries.
3525 unsigned int
3526 entry_count() const
d83ce4e3 3527 {
b3ccdeb5
AM
3528 if (this->current_data_size() == 0)
3529 return 0;
b4f7960d
AM
3530 return ((this->current_data_size() - this->first_plt_entry_offset())
3531 / this->plt_entry_size());
d83ce4e3 3532 }
0e70b911 3533
42cacb20 3534 protected:
42cacb20 3535 void
cf43a2fe 3536 do_adjust_output_section(Output_section* os)
42cacb20 3537 {
cf43a2fe 3538 os->set_entsize(0);
42cacb20
DE
3539 }
3540
6ce78956
AM
3541 // Write to a map file.
3542 void
3543 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 3544 { mapfile->print_output_data(this, this->name_); }
6ce78956 3545
cf43a2fe 3546 private:
b4f7960d
AM
3547 // Return the offset of the first non-reserved PLT entry.
3548 unsigned int
3549 first_plt_entry_offset() const
3550 {
3551 // IPLT has no reserved entry.
3552 if (this->name_[3] == 'I')
3553 return 0;
3554 return this->targ_->first_plt_entry_offset();
3555 }
3556
3557 // Return the size of each PLT entry.
3558 unsigned int
3559 plt_entry_size() const
3560 {
3561 return this->targ_->plt_entry_size();
3562 }
cf43a2fe 3563
42cacb20
DE
3564 // Write out the PLT data.
3565 void
3566 do_write(Output_file*);
3567
3568 // The reloc section.
3569 Reloc_section* rel_;
cf43a2fe
AM
3570 // Allows access to .glink for do_write.
3571 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
3572 // What to report in map file.
3573 const char *name_;
42cacb20
DE
3574};
3575
e5d5f5ed 3576// Add an entry to the PLT.
42cacb20
DE
3577
3578template<int size, bool big_endian>
03e25981 3579void
e5d5f5ed 3580Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 3581{
e5d5f5ed
AM
3582 if (!gsym->has_plt_offset())
3583 {
ec661b9d 3584 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3585 if (off == 0)
3586 off += this->first_plt_entry_offset();
3587 gsym->set_plt_offset(off);
3588 gsym->set_needs_dynsym_entry();
3589 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3590 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 3591 off += this->plt_entry_size();
e5d5f5ed
AM
3592 this->set_current_data_size(off);
3593 }
42cacb20
DE
3594}
3595
e5d5f5ed 3596// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
3597
3598template<int size, bool big_endian>
03e25981 3599void
e5d5f5ed 3600Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 3601{
cf43a2fe
AM
3602 if (!gsym->has_plt_offset())
3603 {
ec661b9d 3604 section_size_type off = this->current_data_size();
cf43a2fe 3605 gsym->set_plt_offset(off);
e5d5f5ed 3606 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3607 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3608 dynrel = elfcpp::R_PPC64_JMP_IREL;
3609 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 3610 off += this->plt_entry_size();
e5d5f5ed
AM
3611 this->set_current_data_size(off);
3612 }
3613}
3614
3615// Add an entry for a local ifunc symbol to the IPLT.
3616
3617template<int size, bool big_endian>
03e25981 3618void
e5d5f5ed
AM
3619Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3620 Sized_relobj_file<size, big_endian>* relobj,
3621 unsigned int local_sym_index)
3622{
3623 if (!relobj->local_has_plt_offset(local_sym_index))
3624 {
ec661b9d 3625 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3626 relobj->set_local_plt_offset(local_sym_index, off);
3627 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3628 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3629 dynrel = elfcpp::R_PPC64_JMP_IREL;
3630 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3631 this, off, 0);
b4f7960d 3632 off += this->plt_entry_size();
cf43a2fe
AM
3633 this->set_current_data_size(off);
3634 }
42cacb20
DE
3635}
3636
dd93cd0a 3637static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 3638static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 3639static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
3640static const uint32_t add_3_3_2 = 0x7c631214;
3641static const uint32_t add_3_3_13 = 0x7c636a14;
3642static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
3643static const uint32_t add_11_2_11 = 0x7d625a14;
3644static const uint32_t add_11_11_2 = 0x7d6b1214;
3645static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 3646static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 3647static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 3648static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 3649static const uint32_t addi_12_1 = 0x39810000;
b4f7960d 3650static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
3651static const uint32_t addis_0_2 = 0x3c020000;
3652static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 3653static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 3654static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
3655static const uint32_t addis_11_11 = 0x3d6b0000;
3656static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 3657static const uint32_t addis_12_1 = 0x3d810000;
397998fc 3658static const uint32_t addis_12_2 = 0x3d820000;
c9269dff 3659static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
3660static const uint32_t b = 0x48000000;
3661static const uint32_t bcl_20_31 = 0x429f0005;
3662static const uint32_t bctr = 0x4e800420;
f3a0ed29 3663static const uint32_t blr = 0x4e800020;
9e69ed50 3664static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 3665static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 3666static const uint32_t cmpldi_2_0 = 0x28220000;
dd93cd0a
AM
3667static const uint32_t cror_15_15_15 = 0x4def7b82;
3668static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29
AM
3669static const uint32_t ld_0_1 = 0xe8010000;
3670static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 3671static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 3672static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 3673static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 3674static const uint32_t ld_2_12 = 0xe84c0000;
b4f7960d
AM
3675static const uint32_t ld_11_2 = 0xe9620000;
3676static const uint32_t ld_11_11 = 0xe96b0000;
3677static const uint32_t ld_12_2 = 0xe9820000;
3678static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 3679static const uint32_t ld_12_12 = 0xe98c0000;
f3a0ed29 3680static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 3681static const uint32_t li_0_0 = 0x38000000;
f3a0ed29 3682static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 3683static const uint32_t lis_0 = 0x3c000000;
549dba71 3684static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
3685static const uint32_t lis_11 = 0x3d600000;
3686static const uint32_t lis_12 = 0x3d800000;
b4f7960d 3687static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff
AM
3688static const uint32_t lwz_0_12 = 0x800c0000;
3689static const uint32_t lwz_11_11 = 0x816b0000;
3690static const uint32_t lwz_11_30 = 0x817e0000;
3691static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 3692static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 3693static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 3694static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff
AM
3695static const uint32_t mflr_12 = 0x7d8802a6;
3696static const uint32_t mtctr_0 = 0x7c0903a6;
3697static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 3698static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 3699static const uint32_t mtlr_0 = 0x7c0803a6;
c9269dff 3700static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 3701static const uint32_t nop = 0x60000000;
c9269dff 3702static const uint32_t ori_0_0_0 = 0x60000000;
b4f7960d 3703static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
3704static const uint32_t std_0_1 = 0xf8010000;
3705static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 3706static const uint32_t std_2_1 = 0xf8410000;
f3a0ed29
AM
3707static const uint32_t stfd_0_1 = 0xd8010000;
3708static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 3709static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
3710static const uint32_t sub_12_12_11 = 0x7d8b6050;
3711static const uint32_t xor_2_12_12 = 0x7d826278;
3712static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20
DE
3713
3714// Write out the PLT.
3715
3716template<int size, bool big_endian>
3717void
3718Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3719{
b3ccdeb5 3720 if (size == 32 && this->name_[3] != 'I')
cf43a2fe 3721 {
ec661b9d 3722 const section_size_type offset = this->offset();
cf43a2fe
AM
3723 const section_size_type oview_size
3724 = convert_to_section_size_type(this->data_size());
3725 unsigned char* const oview = of->get_output_view(offset, oview_size);
3726 unsigned char* pov = oview;
3727 unsigned char* endpov = oview + oview_size;
3728
e5d5f5ed 3729 // The address of the .glink branch table
cf43a2fe
AM
3730 const Output_data_glink<size, big_endian>* glink
3731 = this->targ_->glink_section();
ec661b9d 3732 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
3733
3734 while (pov < endpov)
3735 {
3736 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3737 pov += 4;
3738 branch_tab += 4;
3739 }
3740
3741 of->write_output_view(offset, oview_size, oview);
3742 }
3743}
3744
3745// Create the PLT section.
3746
3747template<int size, bool big_endian>
3748void
40b469d7
AM
3749Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3750 Layout* layout)
cf43a2fe
AM
3751{
3752 if (this->plt_ == NULL)
3753 {
40b469d7
AM
3754 if (this->got_ == NULL)
3755 this->got_section(symtab, layout);
3756
cf43a2fe
AM
3757 if (this->glink_ == NULL)
3758 make_glink_section(layout);
3759
3760 // Ensure that .rela.dyn always appears before .rela.plt This is
3761 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 3762 // needs to include .rela.plt in its range.
cf43a2fe
AM
3763 this->rela_dyn_section(layout);
3764
e5d5f5ed
AM
3765 Reloc_section* plt_rel = new Reloc_section(false);
3766 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3767 elfcpp::SHF_ALLOC, plt_rel,
3768 ORDER_DYNAMIC_PLT_RELOCS, false);
3769 this->plt_
3770 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 3771 "** PLT");
cf43a2fe
AM
3772 layout->add_output_section_data(".plt",
3773 (size == 32
3774 ? elfcpp::SHT_PROGBITS
3775 : elfcpp::SHT_NOBITS),
3776 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3777 this->plt_,
3778 (size == 32
3779 ? ORDER_SMALL_DATA
3780 : ORDER_SMALL_BSS),
3781 false);
3254d32c
AM
3782
3783 Output_section* rela_plt_os = plt_rel->output_section();
3784 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
3785 }
3786}
3787
e5d5f5ed
AM
3788// Create the IPLT section.
3789
3790template<int size, bool big_endian>
3791void
40b469d7
AM
3792Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3793 Layout* layout)
e5d5f5ed
AM
3794{
3795 if (this->iplt_ == NULL)
3796 {
40b469d7 3797 this->make_plt_section(symtab, layout);
e5d5f5ed
AM
3798
3799 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
3800 if (this->rela_dyn_->output_section())
3801 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
3802 this->iplt_
3803 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 3804 "** IPLT");
6528b6eb
AM
3805 if (this->plt_->output_section())
3806 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
3807 }
3808}
3809
ec661b9d 3810// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
3811
3812template<int size, bool big_endian>
ec661b9d 3813class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
3814{
3815 public:
ec661b9d
AM
3816 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3817 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3818 size, big_endian> Reloc_section;
c9269dff 3819
ec661b9d
AM
3820 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3821 Reloc_section* brlt_rel)
3822 : Output_section_data_build(size == 32 ? 4 : 8),
3823 rel_(brlt_rel),
3824 targ_(targ)
3825 { }
cf43a2fe 3826
06f30c9d
CC
3827 void
3828 reset_brlt_sizes()
3829 {
3830 this->reset_data_size();
3831 this->rel_->reset_data_size();
3832 }
3833
3834 void
3835 finalize_brlt_sizes()
3836 {
3837 this->finalize_data_size();
3838 this->rel_->finalize_data_size();
3839 }
3840
ec661b9d 3841 // Add a reloc for an entry in the BRLT.
cf43a2fe 3842 void
ec661b9d
AM
3843 add_reloc(Address to, unsigned int off)
3844 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 3845
ec661b9d 3846 // Update section and reloc section size.
e5d5f5ed 3847 void
ec661b9d
AM
3848 set_current_size(unsigned int num_branches)
3849 {
3850 this->reset_address_and_file_offset();
3851 this->set_current_data_size(num_branches * 16);
3852 this->finalize_data_size();
3853 Output_section* os = this->output_section();
3854 os->set_section_offsets_need_adjustment();
3855 if (this->rel_ != NULL)
3856 {
0e123f69 3857 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
3858 this->rel_->reset_address_and_file_offset();
3859 this->rel_->set_current_data_size(num_branches * reloc_size);
3860 this->rel_->finalize_data_size();
3861 Output_section* os = this->rel_->output_section();
3862 os->set_section_offsets_need_adjustment();
3863 }
3864 }
cf43a2fe 3865
ec661b9d
AM
3866 protected:
3867 void
3868 do_adjust_output_section(Output_section* os)
3869 {
3870 os->set_entsize(0);
3871 }
e5d5f5ed 3872
ec661b9d
AM
3873 // Write to a map file.
3874 void
3875 do_print_to_mapfile(Mapfile* mapfile) const
3876 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 3877
ec661b9d
AM
3878 private:
3879 // Write out the BRLT data.
3880 void
3881 do_write(Output_file*);
c9824451 3882
ec661b9d
AM
3883 // The reloc section.
3884 Reloc_section* rel_;
3885 Target_powerpc<size, big_endian>* targ_;
3886};
cf43a2fe 3887
ec661b9d
AM
3888// Make the branch lookup table section.
3889
3890template<int size, bool big_endian>
3891void
3892Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3893{
3894 if (size == 64 && this->brlt_section_ == NULL)
3895 {
3896 Reloc_section* brlt_rel = NULL;
3897 bool is_pic = parameters->options().output_is_position_independent();
3898 if (is_pic)
3899 {
6830ee24
AM
3900 // When PIC we can't fill in .branch_lt (like .plt it can be
3901 // a bss style section) but must initialise at runtime via
6528b6eb 3902 // dynamic relocations.
ec661b9d
AM
3903 this->rela_dyn_section(layout);
3904 brlt_rel = new Reloc_section(false);
6528b6eb
AM
3905 if (this->rela_dyn_->output_section())
3906 this->rela_dyn_->output_section()
3907 ->add_output_section_data(brlt_rel);
ec661b9d
AM
3908 }
3909 this->brlt_section_
3910 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 3911 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
3912 this->plt_->output_section()
3913 ->add_output_section_data(this->brlt_section_);
3914 else
6830ee24 3915 layout->add_output_section_data(".branch_lt",
ec661b9d
AM
3916 (is_pic ? elfcpp::SHT_NOBITS
3917 : elfcpp::SHT_PROGBITS),
3918 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3919 this->brlt_section_,
3920 (is_pic ? ORDER_SMALL_BSS
3921 : ORDER_SMALL_DATA),
3922 false);
3923 }
3924}
3925
6830ee24 3926// Write out .branch_lt when non-PIC.
ec661b9d
AM
3927
3928template<int size, bool big_endian>
3929void
3930Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3931{
3932 if (size == 64 && !parameters->options().output_is_position_independent())
3933 {
3934 const section_size_type offset = this->offset();
3935 const section_size_type oview_size
3936 = convert_to_section_size_type(this->data_size());
3937 unsigned char* const oview = of->get_output_view(offset, oview_size);
3938
3939 this->targ_->write_branch_lookup_table(oview);
3940 of->write_output_view(offset, oview_size, oview);
3941 }
3942}
3943
9e69ed50
AM
3944static inline uint32_t
3945l(uint32_t a)
3946{
3947 return a & 0xffff;
3948}
3949
3950static inline uint32_t
3951hi(uint32_t a)
3952{
3953 return l(a >> 16);
3954}
3955
3956static inline uint32_t
3957ha(uint32_t a)
3958{
3959 return hi(a + 0x8000);
3960}
3961
9d5781f8
AM
3962template<int size>
3963struct Eh_cie
3964{
3965 static const unsigned char eh_frame_cie[12];
3966};
3967
3968template<int size>
3969const unsigned char Eh_cie<size>::eh_frame_cie[] =
3970{
3971 1, // CIE version.
3972 'z', 'R', 0, // Augmentation string.
3973 4, // Code alignment.
3974 0x80 - size / 8 , // Data alignment.
3975 65, // RA reg.
3976 1, // Augmentation size.
3977 (elfcpp::DW_EH_PE_pcrel
3978 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3979 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3980};
3981
b4f7960d
AM
3982// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3983static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
3984{
3985 0, 0, 0, 0, // Replaced with offset to .glink.
3986 0, 0, 0, 0, // Replaced with size of .glink.
3987 0, // Augmentation size.
3988 elfcpp::DW_CFA_advance_loc + 1,
3989 elfcpp::DW_CFA_register, 65, 12,
15a3a14f 3990 elfcpp::DW_CFA_advance_loc + 5,
9d5781f8
AM
3991 elfcpp::DW_CFA_restore_extended, 65
3992};
3993
b4f7960d
AM
3994// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3995static const unsigned char glink_eh_frame_fde_64v2[] =
3996{
3997 0, 0, 0, 0, // Replaced with offset to .glink.
3998 0, 0, 0, 0, // Replaced with size of .glink.
3999 0, // Augmentation size.
4000 elfcpp::DW_CFA_advance_loc + 1,
4001 elfcpp::DW_CFA_register, 65, 0,
15a3a14f 4002 elfcpp::DW_CFA_advance_loc + 7,
b4f7960d
AM
4003 elfcpp::DW_CFA_restore_extended, 65
4004};
4005
9d5781f8
AM
4006// Describe __glink_PLTresolve use of LR, 32-bit version.
4007static const unsigned char glink_eh_frame_fde_32[] =
4008{
4009 0, 0, 0, 0, // Replaced with offset to .glink.
4010 0, 0, 0, 0, // Replaced with size of .glink.
4011 0, // Augmentation size.
4012 elfcpp::DW_CFA_advance_loc + 2,
4013 elfcpp::DW_CFA_register, 65, 0,
4014 elfcpp::DW_CFA_advance_loc + 4,
4015 elfcpp::DW_CFA_restore_extended, 65
4016};
4017
4018static const unsigned char default_fde[] =
4019{
4020 0, 0, 0, 0, // Replaced with offset to stubs.
4021 0, 0, 0, 0, // Replaced with size of stubs.
4022 0, // Augmentation size.
4023 elfcpp::DW_CFA_nop, // Pad.
4024 elfcpp::DW_CFA_nop,
4025 elfcpp::DW_CFA_nop
4026};
4027
9e69ed50
AM
4028template<bool big_endian>
4029static inline void
4030write_insn(unsigned char* p, uint32_t v)
4031{
4032 elfcpp::Swap<32, big_endian>::writeval(p, v);
4033}
4034
ec661b9d
AM
4035// Stub_table holds information about plt and long branch stubs.
4036// Stubs are built in an area following some input section determined
4037// by group_sections(). This input section is converted to a relaxed
4038// input section allowing it to be resized to accommodate the stubs
4039
4040template<int size, bool big_endian>
4041class Stub_table : public Output_relaxed_input_section
4042{
4043 public:
7e57d19e
AM
4044 struct Plt_stub_ent
4045 {
4046 Plt_stub_ent(unsigned int off, unsigned int indx)
7ee7ff70 4047 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
7e57d19e
AM
4048 { }
4049
4050 unsigned int off_;
7ee7ff70 4051 unsigned int indx_ : 30;
7e57d19e 4052 unsigned int r2save_ : 1;
7ee7ff70 4053 unsigned int localentry0_ : 1;
7e57d19e 4054 };
ec661b9d
AM
4055 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4056 static const Address invalid_address = static_cast<Address>(0) - 1;
4057
a3e60ddb
AM
4058 Stub_table(Target_powerpc<size, big_endian>* targ,
4059 Output_section* output_section,
590b87ff
AM
4060 const Output_section::Input_section* owner,
4061 uint32_t id)
a3e60ddb
AM
4062 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4063 owner->relobj()
4064 ->section_addralign(owner->shndx())),
ec661b9d 4065 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4066 orig_data_size_(owner->current_data_size()),
4067 plt_size_(0), last_plt_size_(0),
6395d38b 4068 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
590b87ff 4069 eh_frame_added_(false), need_save_res_(false), uniq_(id)
a3e60ddb
AM
4070 {
4071 this->set_output_section(output_section);
ec661b9d 4072
a3e60ddb
AM
4073 std::vector<Output_relaxed_input_section*> new_relaxed;
4074 new_relaxed.push_back(this);
4075 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4076 }
ec661b9d
AM
4077
4078 // Add a plt call stub.
a3e60ddb
AM
4079 bool
4080 add_plt_call_entry(Address,
4081 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4082 const Symbol*,
4083 unsigned int,
7e57d19e
AM
4084 Address,
4085 bool);
ec661b9d 4086
a3e60ddb
AM
4087 bool
4088 add_plt_call_entry(Address,
4089 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4090 unsigned int,
4091 unsigned int,
7e57d19e
AM
4092 Address,
4093 bool);
ec661b9d
AM
4094
4095 // Find a given plt call stub.
7e57d19e 4096 const Plt_stub_ent*
ec661b9d
AM
4097 find_plt_call_entry(const Symbol*) const;
4098
7e57d19e 4099 const Plt_stub_ent*
ec661b9d
AM
4100 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4101 unsigned int) const;
4102
7e57d19e 4103 const Plt_stub_ent*
ec661b9d
AM
4104 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4105 const Symbol*,
4106 unsigned int,
4107 Address) const;
4108
7e57d19e 4109 const Plt_stub_ent*
ec661b9d
AM
4110 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4111 unsigned int,
4112 unsigned int,
4113 Address) const;
4114
4115 // Add a long branch stub.
a3e60ddb
AM
4116 bool
4117 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
d49044c7 4118 unsigned int, Address, Address, bool);
ec661b9d
AM
4119
4120 Address
9d5781f8
AM
4121 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4122 Address) const;
ec661b9d 4123
a3e60ddb
AM
4124 bool
4125 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4126 {
cbcb23fa 4127 Address max_branch_offset = max_branch_delta(r_type);
a3e60ddb
AM
4128 if (max_branch_offset == 0)
4129 return true;
4130 gold_assert(from != invalid_address);
4131 Address loc = off + this->stub_address();
4132 return loc - from + max_branch_offset < 2 * max_branch_offset;
4133 }
4134
ec661b9d 4135 void
a3e60ddb 4136 clear_stubs(bool all)
cf43a2fe 4137 {
9e69ed50
AM
4138 this->plt_call_stubs_.clear();
4139 this->plt_size_ = 0;
ec661b9d
AM
4140 this->long_branch_stubs_.clear();
4141 this->branch_size_ = 0;
d49044c7 4142 this->need_save_res_ = false;
a3e60ddb
AM
4143 if (all)
4144 {
4145 this->last_plt_size_ = 0;
4146 this->last_branch_size_ = 0;
4147 }
cf43a2fe
AM
4148 }
4149
ec661b9d
AM
4150 Address
4151 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4152 {
ec661b9d
AM
4153 Address start_off = off;
4154 off += this->orig_data_size_;
4155 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4156 if (this->need_save_res_)
4157 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4158 if (my_size != 0)
4159 off = align_address(off, this->stub_align());
4160 // Include original section size and alignment padding in size
4161 my_size += off - start_off;
6395d38b
HS
4162 // Ensure new size is always larger than min size
4163 // threshold. Alignment requirement is included in "my_size", so
4164 // increase "my_size" does not invalidate alignment.
4165 if (my_size < this->min_size_threshold_)
4166 my_size = this->min_size_threshold_;
ec661b9d
AM
4167 this->reset_address_and_file_offset();
4168 this->set_current_data_size(my_size);
4169 this->set_address_and_file_offset(os->address() + start_off,
4170 os->offset() + start_off);
4171 return my_size;
cf43a2fe
AM
4172 }
4173
ec661b9d 4174 Address
9d5781f8 4175 stub_address() const
ec661b9d
AM
4176 {
4177 return align_address(this->address() + this->orig_data_size_,
4178 this->stub_align());
4179 }
4180
4181 Address
9d5781f8 4182 stub_offset() const
ec661b9d
AM
4183 {
4184 return align_address(this->offset() + this->orig_data_size_,
4185 this->stub_align());
4186 }
4187
4188 section_size_type
4189 plt_size() const
4190 { return this->plt_size_; }
4191
590b87ff
AM
4192 void
4193 set_min_size_threshold(Address min_size)
6395d38b
HS
4194 { this->min_size_threshold_ = min_size; }
4195
590b87ff
AM
4196 void
4197 define_stub_syms(Symbol_table*);
4198
ec661b9d
AM
4199 bool
4200 size_update()
4201 {
4202 Output_section* os = this->output_section();
4203 if (os->addralign() < this->stub_align())
4204 {
4205 os->set_addralign(this->stub_align());
4206 // FIXME: get rid of the insane checkpointing.
4207 // We can't increase alignment of the input section to which
4208 // stubs are attached; The input section may be .init which
4209 // is pasted together with other .init sections to form a
4210 // function. Aligning might insert zero padding resulting in
4211 // sigill. However we do need to increase alignment of the
4212 // output section so that the align_address() on offset in
4213 // set_address_and_size() adds the same padding as the
4214 // align_address() on address in stub_address().
4215 // What's more, we need this alignment for the layout done in
4216 // relaxation_loop_body() so that the output section starts at
4217 // a suitably aligned address.
4218 os->checkpoint_set_addralign(this->stub_align());
4219 }
9e69ed50
AM
4220 if (this->last_plt_size_ != this->plt_size_
4221 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4222 {
9e69ed50
AM
4223 this->last_plt_size_ = this->plt_size_;
4224 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4225 return true;
4226 }
4227 return false;
4228 }
4229
9d5781f8
AM
4230 // Add .eh_frame info for this stub section. Unlike other linker
4231 // generated .eh_frame this is added late in the link, because we
4232 // only want the .eh_frame info if this particular stub section is
4233 // non-empty.
4234 void
4235 add_eh_frame(Layout* layout)
4236 {
4237 if (!this->eh_frame_added_)
4238 {
4239 if (!parameters->options().ld_generated_unwind_info())
4240 return;
4241
4242 // Since we add stub .eh_frame info late, it must be placed
4243 // after all other linker generated .eh_frame info so that
4244 // merge mapping need not be updated for input sections.
4245 // There is no provision to use a different CIE to that used
4246 // by .glink.
4247 if (!this->targ_->has_glink())
4248 return;
4249
4250 layout->add_eh_frame_for_plt(this,
4251 Eh_cie<size>::eh_frame_cie,
4252 sizeof (Eh_cie<size>::eh_frame_cie),
4253 default_fde,
4254 sizeof (default_fde));
4255 this->eh_frame_added_ = true;
4256 }
4257 }
4258
ec661b9d
AM
4259 Target_powerpc<size, big_endian>*
4260 targ() const
4261 { return targ_; }
6ce78956 4262
cf43a2fe 4263 private:
bdab445c
AM
4264 class Plt_stub_key;
4265 class Plt_stub_key_hash;
bdab445c
AM
4266 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4267 Plt_stub_key_hash> Plt_stub_entries;
590b87ff
AM
4268 class Branch_stub_ent;
4269 class Branch_stub_ent_hash;
4270 typedef Unordered_map<Branch_stub_ent, unsigned int,
4271 Branch_stub_ent_hash> Branch_stub_entries;
9e69ed50
AM
4272
4273 // Alignment of stub section.
ec661b9d 4274 unsigned int
9e69ed50
AM
4275 stub_align() const
4276 {
4277 if (size == 32)
4278 return 16;
4279 unsigned int min_align = 32;
4280 unsigned int user_align = 1 << parameters->options().plt_align();
4281 return std::max(user_align, min_align);
4282 }
cf43a2fe 4283
91c2b899
AM
4284 // Return the plt offset for the given call stub.
4285 Address
4286 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4287 {
4288 const Symbol* gsym = p->first.sym_;
4289 if (gsym != NULL)
4290 {
4291 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4292 && gsym->can_use_relative_reloc(false));
4293 return gsym->plt_offset();
4294 }
4295 else
4296 {
4297 *is_iplt = true;
4298 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4299 unsigned int local_sym_index = p->first.locsym_;
4300 return relobj->local_plt_offset(local_sym_index);
4301 }
4302 }
4303
9e69ed50 4304 // Size of a given plt call stub.
ec661b9d 4305 unsigned int
9e69ed50
AM
4306 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4307 {
4308 if (size == 32)
4309 return 16;
4310
91c2b899
AM
4311 bool is_iplt;
4312 Address plt_addr = this->plt_off(p, &is_iplt);
4313 if (is_iplt)
4314 plt_addr += this->targ_->iplt_section()->address();
9e69ed50 4315 else
91c2b899
AM
4316 plt_addr += this->targ_->plt_section()->address();
4317 Address got_addr = this->targ_->got_section()->output_section()->address();
9e69ed50
AM
4318 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4319 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
91c2b899
AM
4320 got_addr += ppcobj->toc_base_offset();
4321 Address off = plt_addr - got_addr;
b4f7960d
AM
4322 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4323 if (this->targ_->abiversion() < 2)
4324 {
4325 bool static_chain = parameters->options().plt_static_chain();
4326 bool thread_safe = this->targ_->plt_thread_safe();
4327 bytes += (4
4328 + 4 * static_chain
4329 + 8 * thread_safe
4330 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4331 }
9e69ed50
AM
4332 unsigned int align = 1 << parameters->options().plt_align();
4333 if (align > 1)
4334 bytes = (bytes + align - 1) & -align;
4335 return bytes;
4336 }
ec661b9d
AM
4337
4338 // Return long branch stub size.
4339 unsigned int
590b87ff 4340 branch_stub_size(typename Branch_stub_entries::const_iterator p)
ec661b9d 4341 {
590b87ff
AM
4342 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4343 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
ec661b9d
AM
4344 return 4;
4345 if (size == 64 || !parameters->options().output_is_position_independent())
4346 return 16;
4347 return 32;
4348 }
4349
4350 // Write out stubs.
cf43a2fe
AM
4351 void
4352 do_write(Output_file*);
4353
ec661b9d 4354 // Plt call stub keys.
bdab445c 4355 class Plt_stub_key
cf43a2fe 4356 {
d1a8cabd 4357 public:
bdab445c 4358 Plt_stub_key(const Symbol* sym)
c9824451
AM
4359 : sym_(sym), object_(0), addend_(0), locsym_(0)
4360 { }
4361
bdab445c 4362 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4363 unsigned int locsym_index)
c9824451
AM
4364 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4365 { }
4366
bdab445c 4367 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4368 const Symbol* sym,
4369 unsigned int r_type,
4370 Address addend)
e5d5f5ed 4371 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4372 {
4373 if (size != 32)
ec661b9d 4374 this->addend_ = addend;
d1a8cabd 4375 else if (parameters->options().output_is_position_independent()
ec661b9d 4376 && r_type == elfcpp::R_PPC_PLTREL24)
cf43a2fe 4377 {
ec661b9d 4378 this->addend_ = addend;
e5d5f5ed 4379 if (this->addend_ >= 32768)
d1a8cabd 4380 this->object_ = object;
cf43a2fe
AM
4381 }
4382 }
4383
bdab445c 4384 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4385 unsigned int locsym_index,
4386 unsigned int r_type,
4387 Address addend)
e5d5f5ed
AM
4388 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4389 {
4390 if (size != 32)
ec661b9d 4391 this->addend_ = addend;
e5d5f5ed 4392 else if (parameters->options().output_is_position_independent()
ec661b9d
AM
4393 && r_type == elfcpp::R_PPC_PLTREL24)
4394 this->addend_ = addend;
e5d5f5ed
AM
4395 }
4396
bdab445c 4397 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
4398 {
4399 return (this->sym_ == that.sym_
4400 && this->object_ == that.object_
e5d5f5ed
AM
4401 && this->addend_ == that.addend_
4402 && this->locsym_ == that.locsym_);
cf43a2fe 4403 }
c9269dff
AM
4404
4405 const Symbol* sym_;
e5d5f5ed
AM
4406 const Sized_relobj_file<size, big_endian>* object_;
4407 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4408 unsigned int locsym_;
cf43a2fe
AM
4409 };
4410
bdab445c 4411 class Plt_stub_key_hash
cf43a2fe 4412 {
d1a8cabd 4413 public:
bdab445c 4414 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
4415 {
4416 return (reinterpret_cast<uintptr_t>(ent.sym_)
4417 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
4418 ^ ent.addend_
4419 ^ ent.locsym_);
cf43a2fe 4420 }
ec661b9d
AM
4421 };
4422
4423 // Long branch stub keys.
4424 class Branch_stub_ent
4425 {
4426 public:
d49044c7
AM
4427 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4428 Address to, bool save_res)
4429 : dest_(to), toc_base_off_(0), save_res_(save_res)
ec661b9d
AM
4430 {
4431 if (size == 64)
4432 toc_base_off_ = obj->toc_base_offset();
4433 }
4434
4435 bool operator==(const Branch_stub_ent& that) const
4436 {
4437 return (this->dest_ == that.dest_
4438 && (size == 32
4439 || this->toc_base_off_ == that.toc_base_off_));
4440 }
cf43a2fe 4441
ec661b9d
AM
4442 Address dest_;
4443 unsigned int toc_base_off_;
d49044c7 4444 bool save_res_;
ec661b9d 4445 };
cf43a2fe 4446
ec661b9d
AM
4447 class Branch_stub_ent_hash
4448 {
4449 public:
4450 size_t operator()(const Branch_stub_ent& ent) const
4451 { return ent.dest_ ^ ent.toc_base_off_; }
4452 };
cf43a2fe 4453
ec661b9d 4454 // In a sane world this would be a global.
cf43a2fe 4455 Target_powerpc<size, big_endian>* targ_;
ec661b9d 4456 // Map sym/object/addend to stub offset.
ec661b9d
AM
4457 Plt_stub_entries plt_call_stubs_;
4458 // Map destination address to stub offset.
ec661b9d
AM
4459 Branch_stub_entries long_branch_stubs_;
4460 // size of input section
4461 section_size_type orig_data_size_;
4462 // size of stubs
9e69ed50 4463 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
4464 // Some rare cases cause (PR/20529) fluctuation in stub table
4465 // size, which leads to an endless relax loop. This is to be fixed
4466 // by, after the first few iterations, allowing only increase of
4467 // stub table size. This variable sets the minimal possible size of
4468 // a stub table, it is zero for the first few iterations, then
4469 // increases monotonically.
4470 Address min_size_threshold_;
9d5781f8
AM
4471 // Whether .eh_frame info has been created for this stub section.
4472 bool eh_frame_added_;
d49044c7
AM
4473 // Set if this stub group needs a copy of out-of-line register
4474 // save/restore functions.
4475 bool need_save_res_;
590b87ff
AM
4476 // Per stub table unique identifier.
4477 uint32_t uniq_;
cf43a2fe
AM
4478};
4479
ec661b9d 4480// Add a plt call stub, if we do not already have one for this
d1a8cabd 4481// sym/object/addend combo.
cf43a2fe
AM
4482
4483template<int size, bool big_endian>
a3e60ddb 4484bool
ec661b9d 4485Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4486 Address from,
c9824451 4487 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4488 const Symbol* gsym,
ec661b9d 4489 unsigned int r_type,
7e57d19e
AM
4490 Address addend,
4491 bool tocsave)
cf43a2fe 4492{
bdab445c
AM
4493 Plt_stub_key key(object, gsym, r_type, addend);
4494 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 4495 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 4496 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
9e69ed50 4497 if (p.second)
7ee7ff70
AM
4498 {
4499 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4500 if (size == 64
4501 && this->targ_->is_elfv2_localentry0(gsym))
4502 {
4503 p.first->second.localentry0_ = 1;
4504 this->targ_->set_has_localentry0();
4505 }
4506 }
4507 if (size == 64
4508 && !tocsave
4509 && !p.first->second.localentry0_)
7e57d19e 4510 p.first->second.r2save_ = 1;
bdab445c 4511 return this->can_reach_stub(from, ent.off_, r_type);
cf43a2fe
AM
4512}
4513
e5d5f5ed 4514template<int size, bool big_endian>
a3e60ddb 4515bool
ec661b9d 4516Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4517 Address from,
c9824451 4518 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4519 unsigned int locsym_index,
ec661b9d 4520 unsigned int r_type,
7e57d19e
AM
4521 Address addend,
4522 bool tocsave)
e5d5f5ed 4523{
bdab445c
AM
4524 Plt_stub_key key(object, locsym_index, r_type, addend);
4525 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 4526 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 4527 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
9e69ed50 4528 if (p.second)
7ee7ff70
AM
4529 {
4530 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4531 if (size == 64
4532 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4533 {
4534 p.first->second.localentry0_ = 1;
4535 this->targ_->set_has_localentry0();
4536 }
4537 }
4538 if (size == 64
4539 && !tocsave
4540 && !p.first->second.localentry0_)
7e57d19e 4541 p.first->second.r2save_ = 1;
bdab445c 4542 return this->can_reach_stub(from, ent.off_, r_type);
e5d5f5ed
AM
4543}
4544
ec661b9d
AM
4545// Find a plt call stub.
4546
cf43a2fe 4547template<int size, bool big_endian>
7e57d19e 4548const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4549Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4550 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4551 const Symbol* gsym,
ec661b9d
AM
4552 unsigned int r_type,
4553 Address addend) const
c9824451 4554{
bdab445c
AM
4555 Plt_stub_key key(object, gsym, r_type, addend);
4556 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4557 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
4558 return NULL;
4559 return &p->second;
c9824451
AM
4560}
4561
4562template<int size, bool big_endian>
7e57d19e 4563const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4564Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 4565{
bdab445c
AM
4566 Plt_stub_key key(gsym);
4567 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
4568 if (p == this->plt_call_stubs_.end())
4569 return NULL;
4570 return &p->second;
cf43a2fe
AM
4571}
4572
e5d5f5ed 4573template<int size, bool big_endian>
7e57d19e 4574const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4575Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4576 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4577 unsigned int locsym_index,
ec661b9d
AM
4578 unsigned int r_type,
4579 Address addend) const
e5d5f5ed 4580{
bdab445c
AM
4581 Plt_stub_key key(object, locsym_index, r_type, addend);
4582 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4583 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
4584 return NULL;
4585 return &p->second;
c9824451
AM
4586}
4587
4588template<int size, bool big_endian>
7e57d19e 4589const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4590Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
4591 const Sized_relobj_file<size, big_endian>* object,
4592 unsigned int locsym_index) const
4593{
bdab445c
AM
4594 Plt_stub_key key(object, locsym_index);
4595 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
4596 if (p == this->plt_call_stubs_.end())
4597 return NULL;
4598 return &p->second;
ec661b9d
AM
4599}
4600
4601// Add a long branch stub if we don't already have one to given
4602// destination.
4603
4604template<int size, bool big_endian>
a3e60ddb 4605bool
ec661b9d
AM
4606Stub_table<size, big_endian>::add_long_branch_entry(
4607 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
4608 unsigned int r_type,
4609 Address from,
d49044c7
AM
4610 Address to,
4611 bool save_res)
ec661b9d 4612{
d49044c7 4613 Branch_stub_ent ent(object, to, save_res);
ec661b9d 4614 Address off = this->branch_size_;
590b87ff
AM
4615 std::pair<typename Branch_stub_entries::iterator, bool> p
4616 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4617 if (p.second)
ec661b9d 4618 {
d49044c7
AM
4619 if (save_res)
4620 this->need_save_res_ = true;
4621 else
4622 {
590b87ff 4623 unsigned int stub_size = this->branch_stub_size(p.first);
d49044c7
AM
4624 this->branch_size_ = off + stub_size;
4625 if (size == 64 && stub_size != 4)
4626 this->targ_->add_branch_lookup_table(to);
4627 }
ec661b9d 4628 }
a3e60ddb 4629 return this->can_reach_stub(from, off, r_type);
ec661b9d
AM
4630}
4631
d49044c7 4632// Find long branch stub offset.
ec661b9d
AM
4633
4634template<int size, bool big_endian>
ec5b8187 4635typename Stub_table<size, big_endian>::Address
ec661b9d
AM
4636Stub_table<size, big_endian>::find_long_branch_entry(
4637 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 4638 Address to) const
ec661b9d 4639{
d49044c7 4640 Branch_stub_ent ent(object, to, false);
ec661b9d
AM
4641 typename Branch_stub_entries::const_iterator p
4642 = this->long_branch_stubs_.find(ent);
d49044c7
AM
4643 if (p == this->long_branch_stubs_.end())
4644 return invalid_address;
4645 if (p->first.save_res_)
4646 return to - this->targ_->savres_section()->address() + this->branch_size_;
4647 return p->second;
e5d5f5ed
AM
4648}
4649
ec661b9d
AM
4650// A class to handle .glink.
4651
4652template<int size, bool big_endian>
4653class Output_data_glink : public Output_section_data
4654{
4655 public:
9055360d
AM
4656 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4657 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
4658 static const int pltresolve_size = 16*4;
4659
4660 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
4661 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4662 end_branch_table_(), ge_size_(0)
ec661b9d
AM
4663 { }
4664
9d5781f8 4665 void
9055360d 4666 add_eh_frame(Layout* layout);
9d5781f8 4667
9055360d
AM
4668 void
4669 add_global_entry(const Symbol*);
4670
4671 Address
4672 find_global_entry(const Symbol*) const;
4673
4674 Address
4675 global_entry_address() const
4676 {
4677 gold_assert(this->is_data_size_valid());
4678 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4679 return this->address() + global_entry_off;
9d5781f8
AM
4680 }
4681
ec661b9d
AM
4682 protected:
4683 // Write to a map file.
4684 void
4685 do_print_to_mapfile(Mapfile* mapfile) const
4686 { mapfile->print_output_data(this, _("** glink")); }
4687
4688 private:
4689 void
4690 set_final_data_size();
4691
4692 // Write out .glink
4693 void
4694 do_write(Output_file*);
4695
4696 // Allows access to .got and .plt for do_write.
4697 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
4698
4699 // Map sym to stub offset.
4700 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4701 Global_entry_stub_entries global_entry_stubs_;
4702
4703 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
4704};
4705
9055360d
AM
4706template<int size, bool big_endian>
4707void
4708Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4709{
4710 if (!parameters->options().ld_generated_unwind_info())
4711 return;
4712
4713 if (size == 64)
4714 {
4715 if (this->targ_->abiversion() < 2)
4716 layout->add_eh_frame_for_plt(this,
4717 Eh_cie<64>::eh_frame_cie,
4718 sizeof (Eh_cie<64>::eh_frame_cie),
4719 glink_eh_frame_fde_64v1,
4720 sizeof (glink_eh_frame_fde_64v1));
4721 else
4722 layout->add_eh_frame_for_plt(this,
4723 Eh_cie<64>::eh_frame_cie,
4724 sizeof (Eh_cie<64>::eh_frame_cie),
4725 glink_eh_frame_fde_64v2,
4726 sizeof (glink_eh_frame_fde_64v2));
4727 }
4728 else
4729 {
4730 // 32-bit .glink can use the default since the CIE return
4731 // address reg, LR, is valid.
4732 layout->add_eh_frame_for_plt(this,
4733 Eh_cie<32>::eh_frame_cie,
4734 sizeof (Eh_cie<32>::eh_frame_cie),
4735 default_fde,
4736 sizeof (default_fde));
4737 // Except where LR is used in a PIC __glink_PLTresolve.
4738 if (parameters->options().output_is_position_independent())
4739 layout->add_eh_frame_for_plt(this,
4740 Eh_cie<32>::eh_frame_cie,
4741 sizeof (Eh_cie<32>::eh_frame_cie),
4742 glink_eh_frame_fde_32,
4743 sizeof (glink_eh_frame_fde_32));
4744 }
4745}
4746
4747template<int size, bool big_endian>
4748void
4749Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4750{
4751 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4752 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4753 if (p.second)
4754 this->ge_size_ += 16;
4755}
4756
4757template<int size, bool big_endian>
4758typename Output_data_glink<size, big_endian>::Address
4759Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4760{
4761 typename Global_entry_stub_entries::const_iterator p
4762 = this->global_entry_stubs_.find(gsym);
4763 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4764}
4765
cf43a2fe
AM
4766template<int size, bool big_endian>
4767void
4768Output_data_glink<size, big_endian>::set_final_data_size()
4769{
ec661b9d
AM
4770 unsigned int count = this->targ_->plt_entry_count();
4771 section_size_type total = 0;
cf43a2fe
AM
4772
4773 if (count != 0)
4774 {
4775 if (size == 32)
4776 {
cf43a2fe
AM
4777 // space for branch table
4778 total += 4 * (count - 1);
4779
4780 total += -total & 15;
4781 total += this->pltresolve_size;
4782 }
4783 else
4784 {
cf43a2fe
AM
4785 total += this->pltresolve_size;
4786
4787 // space for branch table
b4f7960d
AM
4788 total += 4 * count;
4789 if (this->targ_->abiversion() < 2)
4790 {
4791 total += 4 * count;
4792 if (count > 0x8000)
4793 total += 4 * (count - 0x8000);
4794 }
cf43a2fe
AM
4795 }
4796 }
9055360d
AM
4797 this->end_branch_table_ = total;
4798 total = (total + 15) & -16;
4799 total += this->ge_size_;
cf43a2fe
AM
4800
4801 this->set_data_size(total);
4802}
4803
590b87ff
AM
4804// Define symbols on stubs, identifying the stub.
4805
4806template<int size, bool big_endian>
4807void
4808Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
4809{
4810 if (!this->plt_call_stubs_.empty())
4811 {
4812 // The key for the plt call stub hash table includes addresses,
4813 // therefore traversal order depends on those addresses, which
4814 // can change between runs if gold is a PIE. Unfortunately the
4815 // output .symtab ordering depends on the order in which symbols
4816 // are added to the linker symtab. We want reproducible output
4817 // so must sort the call stub symbols.
4818 typedef typename Plt_stub_entries::const_iterator plt_iter;
4819 std::vector<plt_iter> sorted;
4820 sorted.resize(this->plt_call_stubs_.size());
4821
4822 for (plt_iter cs = this->plt_call_stubs_.begin();
4823 cs != this->plt_call_stubs_.end();
4824 ++cs)
bdab445c 4825 sorted[cs->second.indx_] = cs;
590b87ff
AM
4826
4827 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
4828 {
4829 plt_iter cs = sorted[i];
4830 char add[10];
4831 add[0] = 0;
4832 if (cs->first.addend_ != 0)
4833 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
4834 char obj[10];
4835 obj[0] = 0;
4836 if (cs->first.object_)
590b87ff
AM
4837 {
4838 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4839 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
4840 sprintf(obj, "%x:", ppcobj->uniq());
4841 }
4842 char localname[9];
4843 const char *symname;
4844 if (cs->first.sym_ == NULL)
4845 {
4846 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
4847 symname = localname;
4848 }
4849 else
4850 symname = cs->first.sym_->name();
94de2a2c
JC
4851 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
4852 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
4853 Address value
4854 = this->stub_address() - this->address() + cs->second.off_;
590b87ff
AM
4855 unsigned int stub_size = this->plt_call_size(cs);
4856 this->targ_->define_local(symtab, name, this, value, stub_size);
4857 }
4858 }
4859
4860 typedef typename Branch_stub_entries::const_iterator branch_iter;
4861 for (branch_iter bs = this->long_branch_stubs_.begin();
4862 bs != this->long_branch_stubs_.end();
4863 ++bs)
4864 {
4865 if (bs->first.save_res_)
4866 continue;
4867
4868 char* name = new char[8 + 13 + 16 + 1];
4869 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
4870 static_cast<unsigned long long>(bs->first.dest_));
4871 Address value = (this->stub_address() - this->address()
4872 + this->plt_size_ + bs->second);
4873 unsigned int stub_size = this->branch_stub_size(bs);
4874 this->targ_->define_local(symtab, name, this, value, stub_size);
4875 }
4876}
4877
ec661b9d 4878// Write out plt and long branch stub code.
cf43a2fe
AM
4879
4880template<int size, bool big_endian>
4881void
ec661b9d 4882Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 4883{
ec661b9d
AM
4884 if (this->plt_call_stubs_.empty()
4885 && this->long_branch_stubs_.empty())
4886 return;
4887
4888 const section_size_type start_off = this->offset();
4889 const section_size_type off = this->stub_offset();
42cacb20 4890 const section_size_type oview_size =
ec661b9d 4891 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 4892 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 4893 unsigned char* p;
42cacb20 4894
cf43a2fe
AM
4895 if (size == 64)
4896 {
ec661b9d
AM
4897 const Output_data_got_powerpc<size, big_endian>* got
4898 = this->targ_->got_section();
dd93cd0a 4899 Address got_os_addr = got->output_section()->address();
c9269dff 4900
ec661b9d 4901 if (!this->plt_call_stubs_.empty())
cf43a2fe 4902 {
ec661b9d
AM
4903 // The base address of the .plt section.
4904 Address plt_base = this->targ_->plt_section()->address();
4905 Address iplt_base = invalid_address;
4906
4907 // Write out plt call stubs.
4908 typename Plt_stub_entries::const_iterator cs;
4909 for (cs = this->plt_call_stubs_.begin();
4910 cs != this->plt_call_stubs_.end();
4911 ++cs)
e5d5f5ed 4912 {
91c2b899
AM
4913 bool is_iplt;
4914 Address pltoff = this->plt_off(cs, &is_iplt);
9e69ed50 4915 Address plt_addr = pltoff;
91c2b899 4916 if (is_iplt)
ec661b9d
AM
4917 {
4918 if (iplt_base == invalid_address)
4919 iplt_base = this->targ_->iplt_section()->address();
4920 plt_addr += iplt_base;
4921 }
4922 else
4923 plt_addr += plt_base;
4924 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4925 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4926 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 4927 Address off = plt_addr - got_addr;
ec661b9d 4928
9e69ed50 4929 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
ec661b9d
AM
4930 gold_error(_("%s: linkage table error against `%s'"),
4931 cs->first.object_->name().c_str(),
4932 cs->first.sym_->demangled_name().c_str());
4933
b4f7960d
AM
4934 bool plt_load_toc = this->targ_->abiversion() < 2;
4935 bool static_chain
4936 = plt_load_toc && parameters->options().plt_static_chain();
4937 bool thread_safe
4938 = plt_load_toc && this->targ_->plt_thread_safe();
9e69ed50
AM
4939 bool use_fake_dep = false;
4940 Address cmp_branch_off = 0;
4941 if (thread_safe)
4942 {
4943 unsigned int pltindex
4944 = ((pltoff - this->targ_->first_plt_entry_offset())
4945 / this->targ_->plt_entry_size());
4946 Address glinkoff
4947 = (this->targ_->glink_section()->pltresolve_size
4948 + pltindex * 8);
4949 if (pltindex > 32768)
4950 glinkoff += (pltindex - 32768) * 4;
4951 Address to
4952 = this->targ_->glink_section()->address() + glinkoff;
4953 Address from
7e57d19e
AM
4954 = (this->stub_address() + cs->second.off_ + 20
4955 + 4 * cs->second.r2save_
9e69ed50
AM
4956 + 4 * (ha(off) != 0)
4957 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4958 + 4 * static_chain);
4959 cmp_branch_off = to - from;
4960 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4961 }
4962
bdab445c 4963 p = oview + cs->second.off_;
9e69ed50 4964 if (ha(off) != 0)
ec661b9d 4965 {
7e57d19e
AM
4966 if (cs->second.r2save_)
4967 {
4968 write_insn<big_endian>(p,
4969 std_2_1 + this->targ_->stk_toc());
4970 p += 4;
4971 }
397998fc
AM
4972 if (plt_load_toc)
4973 {
4974 write_insn<big_endian>(p, addis_11_2 + ha(off));
4975 p += 4;
4976 write_insn<big_endian>(p, ld_12_11 + l(off));
4977 p += 4;
4978 }
4979 else
4980 {
4981 write_insn<big_endian>(p, addis_12_2 + ha(off));
4982 p += 4;
4983 write_insn<big_endian>(p, ld_12_12 + l(off));
4984 p += 4;
4985 }
b4f7960d
AM
4986 if (plt_load_toc
4987 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 4988 {
b4f7960d
AM
4989 write_insn<big_endian>(p, addi_11_11 + l(off));
4990 p += 4;
9e69ed50 4991 off = 0;
ec661b9d 4992 }
b4f7960d
AM
4993 write_insn<big_endian>(p, mtctr_12);
4994 p += 4;
4995 if (plt_load_toc)
9e69ed50 4996 {
b4f7960d
AM
4997 if (use_fake_dep)
4998 {
4999 write_insn<big_endian>(p, xor_2_12_12);
5000 p += 4;
5001 write_insn<big_endian>(p, add_11_11_2);
5002 p += 4;
5003 }
5004 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5005 p += 4;
5006 if (static_chain)
5007 {
5008 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5009 p += 4;
5010 }
9e69ed50 5011 }
ec661b9d
AM
5012 }
5013 else
5014 {
7e57d19e
AM
5015 if (cs->second.r2save_)
5016 {
5017 write_insn<big_endian>(p,
5018 std_2_1 + this->targ_->stk_toc());
5019 p += 4;
5020 }
b4f7960d
AM
5021 write_insn<big_endian>(p, ld_12_2 + l(off));
5022 p += 4;
5023 if (plt_load_toc
5024 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 5025 {
b4f7960d
AM
5026 write_insn<big_endian>(p, addi_2_2 + l(off));
5027 p += 4;
9e69ed50 5028 off = 0;
ec661b9d 5029 }
b4f7960d
AM
5030 write_insn<big_endian>(p, mtctr_12);
5031 p += 4;
5032 if (plt_load_toc)
9e69ed50 5033 {
b4f7960d
AM
5034 if (use_fake_dep)
5035 {
5036 write_insn<big_endian>(p, xor_11_12_12);
5037 p += 4;
5038 write_insn<big_endian>(p, add_2_2_11);
5039 p += 4;
5040 }
5041 if (static_chain)
5042 {
5043 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5044 p += 4;
5045 }
5046 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5047 p += 4;
9e69ed50 5048 }
ec661b9d 5049 }
9e69ed50
AM
5050 if (thread_safe && !use_fake_dep)
5051 {
b4f7960d
AM
5052 write_insn<big_endian>(p, cmpldi_2_0);
5053 p += 4;
5054 write_insn<big_endian>(p, bnectr_p4);
5055 p += 4;
9e69ed50
AM
5056 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5057 }
5058 else
5059 write_insn<big_endian>(p, bctr);
e5d5f5ed 5060 }
ec661b9d
AM
5061 }
5062
5063 // Write out long branch stubs.
5064 typename Branch_stub_entries::const_iterator bs;
5065 for (bs = this->long_branch_stubs_.begin();
5066 bs != this->long_branch_stubs_.end();
5067 ++bs)
5068 {
d49044c7
AM
5069 if (bs->first.save_res_)
5070 continue;
ec661b9d
AM
5071 p = oview + this->plt_size_ + bs->second;
5072 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5073 Address delta = bs->first.dest_ - loc;
5074 if (delta + (1 << 25) < 2 << 25)
5075 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
e5d5f5ed 5076 else
cf43a2fe 5077 {
ec661b9d
AM
5078 Address brlt_addr
5079 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5080 gold_assert(brlt_addr != invalid_address);
5081 brlt_addr += this->targ_->brlt_section()->address();
5082 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5083 Address brltoff = brlt_addr - got_addr;
5084 if (ha(brltoff) == 0)
5085 {
b4f7960d 5086 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
ec661b9d
AM
5087 }
5088 else
cf43a2fe 5089 {
397998fc
AM
5090 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5091 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
cf43a2fe 5092 }
b4f7960d 5093 write_insn<big_endian>(p, mtctr_12), p += 4;
ec661b9d 5094 write_insn<big_endian>(p, bctr);
cf43a2fe 5095 }
ec661b9d
AM
5096 }
5097 }
5098 else
5099 {
5100 if (!this->plt_call_stubs_.empty())
5101 {
5102 // The base address of the .plt section.
5103 Address plt_base = this->targ_->plt_section()->address();
5104 Address iplt_base = invalid_address;
5105 // The address of _GLOBAL_OFFSET_TABLE_.
5106 Address g_o_t = invalid_address;
5107
5108 // Write out plt call stubs.
5109 typename Plt_stub_entries::const_iterator cs;
5110 for (cs = this->plt_call_stubs_.begin();
5111 cs != this->plt_call_stubs_.end();
5112 ++cs)
cf43a2fe 5113 {
91c2b899
AM
5114 bool is_iplt;
5115 Address plt_addr = this->plt_off(cs, &is_iplt);
5116 if (is_iplt)
ec661b9d
AM
5117 {
5118 if (iplt_base == invalid_address)
5119 iplt_base = this->targ_->iplt_section()->address();
5120 plt_addr += iplt_base;
5121 }
5122 else
5123 plt_addr += plt_base;
5124
bdab445c 5125 p = oview + cs->second.off_;
ec661b9d
AM
5126 if (parameters->options().output_is_position_independent())
5127 {
5128 Address got_addr;
5129 const Powerpc_relobj<size, big_endian>* ppcobj
5130 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5131 (cs->first.object_));
5132 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5133 {
5134 unsigned int got2 = ppcobj->got2_shndx();
5135 got_addr = ppcobj->get_output_section_offset(got2);
5136 gold_assert(got_addr != invalid_address);
5137 got_addr += (ppcobj->output_section(got2)->address()
5138 + cs->first.addend_);
5139 }
5140 else
5141 {
5142 if (g_o_t == invalid_address)
5143 {
5144 const Output_data_got_powerpc<size, big_endian>* got
5145 = this->targ_->got_section();
5146 g_o_t = got->address() + got->g_o_t();
5147 }
5148 got_addr = g_o_t;
5149 }
5150
9e69ed50
AM
5151 Address off = plt_addr - got_addr;
5152 if (ha(off) == 0)
ec661b9d 5153 {
9e69ed50 5154 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
ec661b9d
AM
5155 write_insn<big_endian>(p + 4, mtctr_11);
5156 write_insn<big_endian>(p + 8, bctr);
5157 }
5158 else
5159 {
9e69ed50
AM
5160 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
5161 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
ec661b9d
AM
5162 write_insn<big_endian>(p + 8, mtctr_11);
5163 write_insn<big_endian>(p + 12, bctr);
5164 }
5165 }
5166 else
5167 {
5168 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
5169 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
5170 write_insn<big_endian>(p + 8, mtctr_11);
5171 write_insn<big_endian>(p + 12, bctr);
5172 }
5173 }
5174 }
5175
5176 // Write out long branch stubs.
5177 typename Branch_stub_entries::const_iterator bs;
5178 for (bs = this->long_branch_stubs_.begin();
5179 bs != this->long_branch_stubs_.end();
5180 ++bs)
5181 {
d49044c7
AM
5182 if (bs->first.save_res_)
5183 continue;
ec661b9d
AM
5184 p = oview + this->plt_size_ + bs->second;
5185 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5186 Address delta = bs->first.dest_ - loc;
5187 if (delta + (1 << 25) < 2 << 25)
5188 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5189 else if (!parameters->options().output_is_position_independent())
5190 {
5191 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
5192 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
5193 write_insn<big_endian>(p + 8, mtctr_12);
5194 write_insn<big_endian>(p + 12, bctr);
5195 }
5196 else
5197 {
5198 delta -= 8;
5199 write_insn<big_endian>(p + 0, mflr_0);
5200 write_insn<big_endian>(p + 4, bcl_20_31);
5201 write_insn<big_endian>(p + 8, mflr_12);
5202 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
5203 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
5204 write_insn<big_endian>(p + 20, mtlr_0);
5205 write_insn<big_endian>(p + 24, mtctr_12);
5206 write_insn<big_endian>(p + 28, bctr);
cf43a2fe
AM
5207 }
5208 }
ec661b9d 5209 }
d49044c7
AM
5210 if (this->need_save_res_)
5211 {
5212 p = oview + this->plt_size_ + this->branch_size_;
5213 memcpy (p, this->targ_->savres_section()->contents(),
5214 this->targ_->savres_section()->data_size());
5215 }
ec661b9d
AM
5216}
5217
5218// Write out .glink.
5219
5220template<int size, bool big_endian>
5221void
5222Output_data_glink<size, big_endian>::do_write(Output_file* of)
5223{
5224 const section_size_type off = this->offset();
5225 const section_size_type oview_size =
5226 convert_to_section_size_type(this->data_size());
5227 unsigned char* const oview = of->get_output_view(off, oview_size);
5228 unsigned char* p;
5229
5230 // The base address of the .plt section.
5231 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5232 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 5233
ec661b9d
AM
5234 if (size == 64)
5235 {
9055360d 5236 if (this->end_branch_table_ != 0)
b4f7960d 5237 {
9055360d
AM
5238 // Write pltresolve stub.
5239 p = oview;
5240 Address after_bcl = this->address() + 16;
5241 Address pltoff = plt_base - after_bcl;
5242
5243 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 5244
b4f7960d 5245 if (this->targ_->abiversion() < 2)
cf43a2fe 5246 {
9055360d
AM
5247 write_insn<big_endian>(p, mflr_12), p += 4;
5248 write_insn<big_endian>(p, bcl_20_31), p += 4;
5249 write_insn<big_endian>(p, mflr_11), p += 4;
5250 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5251 write_insn<big_endian>(p, mtlr_12), p += 4;
5252 write_insn<big_endian>(p, add_11_2_11), p += 4;
5253 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5254 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5255 write_insn<big_endian>(p, mtctr_12), p += 4;
5256 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5257 }
5258 else
5259 {
5260 write_insn<big_endian>(p, mflr_0), p += 4;
5261 write_insn<big_endian>(p, bcl_20_31), p += 4;
5262 write_insn<big_endian>(p, mflr_11), p += 4;
7ee7ff70 5263 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
9055360d
AM
5264 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5265 write_insn<big_endian>(p, mtlr_0), p += 4;
5266 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5267 write_insn<big_endian>(p, add_11_2_11), p += 4;
5268 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5269 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5270 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5271 write_insn<big_endian>(p, mtctr_12), p += 4;
5272 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5273 }
5274 write_insn<big_endian>(p, bctr), p += 4;
5275 while (p < oview + this->pltresolve_size)
5276 write_insn<big_endian>(p, nop), p += 4;
5277
5278 // Write lazy link call stubs.
5279 uint32_t indx = 0;
5280 while (p < oview + this->end_branch_table_)
5281 {
5282 if (this->targ_->abiversion() < 2)
b4f7960d 5283 {
9055360d
AM
5284 if (indx < 0x8000)
5285 {
5286 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5287 }
5288 else
5289 {
bbec1a5d 5290 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
5291 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5292 }
b4f7960d 5293 }
9055360d
AM
5294 uint32_t branch_off = 8 - (p - oview);
5295 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5296 indx++;
cf43a2fe 5297 }
9055360d
AM
5298 }
5299
5300 Address plt_base = this->targ_->plt_section()->address();
5301 Address iplt_base = invalid_address;
5302 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
5303 Address global_entry_base = this->address() + global_entry_off;
5304 typename Global_entry_stub_entries::const_iterator ge;
5305 for (ge = this->global_entry_stubs_.begin();
5306 ge != this->global_entry_stubs_.end();
5307 ++ge)
5308 {
5309 p = oview + global_entry_off + ge->second;
5310 Address plt_addr = ge->first->plt_offset();
5311 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5312 && ge->first->can_use_relative_reloc(false))
5313 {
5314 if (iplt_base == invalid_address)
5315 iplt_base = this->targ_->iplt_section()->address();
5316 plt_addr += iplt_base;
5317 }
5318 else
5319 plt_addr += plt_base;
5320 Address my_addr = global_entry_base + ge->second;
5321 Address off = plt_addr - my_addr;
5322
5323 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5324 gold_error(_("%s: linkage table error against `%s'"),
5325 ge->first->object()->name().c_str(),
5326 ge->first->demangled_name().c_str());
5327
5328 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5329 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5330 write_insn<big_endian>(p, mtctr_12), p += 4;
5331 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
5332 }
5333 }
5334 else
5335 {
ec661b9d
AM
5336 const Output_data_got_powerpc<size, big_endian>* got
5337 = this->targ_->got_section();
dd93cd0a
AM
5338 // The address of _GLOBAL_OFFSET_TABLE_.
5339 Address g_o_t = got->address() + got->g_o_t();
c9269dff 5340
cf43a2fe 5341 // Write out pltresolve branch table.
ec661b9d 5342 p = oview;
cf43a2fe 5343 unsigned int the_end = oview_size - this->pltresolve_size;
c9269dff 5344 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
5345 while (p < end_p - 8 * 4)
5346 write_insn<big_endian>(p, b + end_p - p), p += 4;
5347 while (p < end_p)
5348 write_insn<big_endian>(p, nop), p += 4;
42cacb20 5349
cf43a2fe
AM
5350 // Write out pltresolve call stub.
5351 if (parameters->options().output_is_position_independent())
42cacb20 5352 {
ec661b9d 5353 Address res0_off = 0;
dd93cd0a
AM
5354 Address after_bcl_off = the_end + 12;
5355 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe
AM
5356
5357 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5358 write_insn<big_endian>(p + 4, mflr_0);
5359 write_insn<big_endian>(p + 8, bcl_20_31);
5360 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5361 write_insn<big_endian>(p + 16, mflr_12);
5362 write_insn<big_endian>(p + 20, mtlr_0);
5363 write_insn<big_endian>(p + 24, sub_11_11_12);
5364
dd93cd0a 5365 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe
AM
5366
5367 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5368 if (ha(got_bcl) == ha(got_bcl + 4))
5369 {
5370 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5371 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5372 }
5373 else
5374 {
5375 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5376 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5377 }
5378 write_insn<big_endian>(p + 40, mtctr_0);
5379 write_insn<big_endian>(p + 44, add_0_11_11);
5380 write_insn<big_endian>(p + 48, add_11_0_11);
5381 write_insn<big_endian>(p + 52, bctr);
5382 write_insn<big_endian>(p + 56, nop);
5383 write_insn<big_endian>(p + 60, nop);
42cacb20 5384 }
cf43a2fe 5385 else
42cacb20 5386 {
ec661b9d 5387 Address res0 = this->address();
cf43a2fe
AM
5388
5389 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5390 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5391 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5392 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5393 else
5394 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5395 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5396 write_insn<big_endian>(p + 16, mtctr_0);
5397 write_insn<big_endian>(p + 20, add_0_11_11);
5398 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5399 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5400 else
5401 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5402 write_insn<big_endian>(p + 28, add_11_0_11);
5403 write_insn<big_endian>(p + 32, bctr);
5404 write_insn<big_endian>(p + 36, nop);
5405 write_insn<big_endian>(p + 40, nop);
5406 write_insn<big_endian>(p + 44, nop);
5407 write_insn<big_endian>(p + 48, nop);
5408 write_insn<big_endian>(p + 52, nop);
5409 write_insn<big_endian>(p + 56, nop);
5410 write_insn<big_endian>(p + 60, nop);
42cacb20 5411 }
cf43a2fe 5412 p += 64;
42cacb20
DE
5413 }
5414
cf43a2fe
AM
5415 of->write_output_view(off, oview_size, oview);
5416}
5417
f3a0ed29
AM
5418
5419// A class to handle linker generated save/restore functions.
5420
5421template<int size, bool big_endian>
5422class Output_data_save_res : public Output_section_data_build
5423{
5424 public:
5425 Output_data_save_res(Symbol_table* symtab);
5426
d49044c7
AM
5427 const unsigned char*
5428 contents() const
5429 {
5430 return contents_;
5431 }
5432
f3a0ed29
AM
5433 protected:
5434 // Write to a map file.
5435 void
5436 do_print_to_mapfile(Mapfile* mapfile) const
5437 { mapfile->print_output_data(this, _("** save/restore")); }
5438
5439 void
5440 do_write(Output_file*);
5441
5442 private:
5443 // The maximum size of save/restore contents.
5444 static const unsigned int savres_max = 218*4;
5445
5446 void
5447 savres_define(Symbol_table* symtab,
5448 const char *name,
5449 unsigned int lo, unsigned int hi,
5450 unsigned char* write_ent(unsigned char*, int),
5451 unsigned char* write_tail(unsigned char*, int));
5452
5453 unsigned char *contents_;
5454};
5455
5456template<bool big_endian>
5457static unsigned char*
5458savegpr0(unsigned char* p, int r)
5459{
5460 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5461 write_insn<big_endian>(p, insn);
5462 return p + 4;
5463}
5464
5465template<bool big_endian>
5466static unsigned char*
5467savegpr0_tail(unsigned char* p, int r)
5468{
5469 p = savegpr0<big_endian>(p, r);
5470 uint32_t insn = std_0_1 + 16;
5471 write_insn<big_endian>(p, insn);
5472 p = p + 4;
5473 write_insn<big_endian>(p, blr);
5474 return p + 4;
5475}
5476
5477template<bool big_endian>
62fe925a 5478static unsigned char*
f3a0ed29
AM
5479restgpr0(unsigned char* p, int r)
5480{
5481 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5482 write_insn<big_endian>(p, insn);
5483 return p + 4;
5484}
5485
5486template<bool big_endian>
62fe925a 5487static unsigned char*
f3a0ed29
AM
5488restgpr0_tail(unsigned char* p, int r)
5489{
5490 uint32_t insn = ld_0_1 + 16;
5491 write_insn<big_endian>(p, insn);
5492 p = p + 4;
5493 p = restgpr0<big_endian>(p, r);
5494 write_insn<big_endian>(p, mtlr_0);
5495 p = p + 4;
5496 if (r == 29)
5497 {
5498 p = restgpr0<big_endian>(p, 30);
5499 p = restgpr0<big_endian>(p, 31);
5500 }
5501 write_insn<big_endian>(p, blr);
5502 return p + 4;
5503}
5504
5505template<bool big_endian>
62fe925a 5506static unsigned char*
f3a0ed29
AM
5507savegpr1(unsigned char* p, int r)
5508{
5509 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5510 write_insn<big_endian>(p, insn);
5511 return p + 4;
5512}
5513
5514template<bool big_endian>
62fe925a 5515static unsigned char*
f3a0ed29
AM
5516savegpr1_tail(unsigned char* p, int r)
5517{
5518 p = savegpr1<big_endian>(p, r);
5519 write_insn<big_endian>(p, blr);
5520 return p + 4;
5521}
5522
5523template<bool big_endian>
62fe925a 5524static unsigned char*
f3a0ed29
AM
5525restgpr1(unsigned char* p, int r)
5526{
5527 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5528 write_insn<big_endian>(p, insn);
5529 return p + 4;
5530}
5531
5532template<bool big_endian>
62fe925a 5533static unsigned char*
f3a0ed29
AM
5534restgpr1_tail(unsigned char* p, int r)
5535{
5536 p = restgpr1<big_endian>(p, r);
5537 write_insn<big_endian>(p, blr);
5538 return p + 4;
5539}
5540
5541template<bool big_endian>
62fe925a 5542static unsigned char*
f3a0ed29
AM
5543savefpr(unsigned char* p, int r)
5544{
5545 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5546 write_insn<big_endian>(p, insn);
5547 return p + 4;
5548}
5549
5550template<bool big_endian>
62fe925a 5551static unsigned char*
f3a0ed29
AM
5552savefpr0_tail(unsigned char* p, int r)
5553{
5554 p = savefpr<big_endian>(p, r);
5555 write_insn<big_endian>(p, std_0_1 + 16);
5556 p = p + 4;
5557 write_insn<big_endian>(p, blr);
5558 return p + 4;
5559}
5560
5561template<bool big_endian>
62fe925a 5562static unsigned char*
f3a0ed29
AM
5563restfpr(unsigned char* p, int r)
5564{
5565 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5566 write_insn<big_endian>(p, insn);
5567 return p + 4;
5568}
5569
5570template<bool big_endian>
62fe925a 5571static unsigned char*
f3a0ed29
AM
5572restfpr0_tail(unsigned char* p, int r)
5573{
5574 write_insn<big_endian>(p, ld_0_1 + 16);
5575 p = p + 4;
5576 p = restfpr<big_endian>(p, r);
5577 write_insn<big_endian>(p, mtlr_0);
5578 p = p + 4;
5579 if (r == 29)
5580 {
5581 p = restfpr<big_endian>(p, 30);
5582 p = restfpr<big_endian>(p, 31);
5583 }
5584 write_insn<big_endian>(p, blr);
5585 return p + 4;
5586}
5587
5588template<bool big_endian>
62fe925a 5589static unsigned char*
f3a0ed29
AM
5590savefpr1_tail(unsigned char* p, int r)
5591{
5592 p = savefpr<big_endian>(p, r);
5593 write_insn<big_endian>(p, blr);
5594 return p + 4;
5595}
5596
5597template<bool big_endian>
62fe925a 5598static unsigned char*
f3a0ed29
AM
5599restfpr1_tail(unsigned char* p, int r)
5600{
5601 p = restfpr<big_endian>(p, r);
5602 write_insn<big_endian>(p, blr);
5603 return p + 4;
5604}
5605
5606template<bool big_endian>
62fe925a 5607static unsigned char*
f3a0ed29
AM
5608savevr(unsigned char* p, int r)
5609{
5610 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5611 write_insn<big_endian>(p, insn);
5612 p = p + 4;
5613 insn = stvx_0_12_0 + (r << 21);
5614 write_insn<big_endian>(p, insn);
5615 return p + 4;
5616}
5617
5618template<bool big_endian>
62fe925a 5619static unsigned char*
f3a0ed29
AM
5620savevr_tail(unsigned char* p, int r)
5621{
5622 p = savevr<big_endian>(p, r);
5623 write_insn<big_endian>(p, blr);
5624 return p + 4;
5625}
5626
5627template<bool big_endian>
62fe925a 5628static unsigned char*
f3a0ed29
AM
5629restvr(unsigned char* p, int r)
5630{
5631 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5632 write_insn<big_endian>(p, insn);
5633 p = p + 4;
5634 insn = lvx_0_12_0 + (r << 21);
5635 write_insn<big_endian>(p, insn);
5636 return p + 4;
5637}
5638
5639template<bool big_endian>
62fe925a 5640static unsigned char*
f3a0ed29
AM
5641restvr_tail(unsigned char* p, int r)
5642{
5643 p = restvr<big_endian>(p, r);
5644 write_insn<big_endian>(p, blr);
5645 return p + 4;
5646}
5647
5648
5649template<int size, bool big_endian>
5650Output_data_save_res<size, big_endian>::Output_data_save_res(
5651 Symbol_table* symtab)
5652 : Output_section_data_build(4),
5653 contents_(NULL)
5654{
5655 this->savres_define(symtab,
5656 "_savegpr0_", 14, 31,
5657 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5658 this->savres_define(symtab,
5659 "_restgpr0_", 14, 29,
5660 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5661 this->savres_define(symtab,
5662 "_restgpr0_", 30, 31,
5663 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5664 this->savres_define(symtab,
5665 "_savegpr1_", 14, 31,
5666 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5667 this->savres_define(symtab,
5668 "_restgpr1_", 14, 31,
5669 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5670 this->savres_define(symtab,
5671 "_savefpr_", 14, 31,
5672 savefpr<big_endian>, savefpr0_tail<big_endian>);
5673 this->savres_define(symtab,
5674 "_restfpr_", 14, 29,
5675 restfpr<big_endian>, restfpr0_tail<big_endian>);
5676 this->savres_define(symtab,
5677 "_restfpr_", 30, 31,
5678 restfpr<big_endian>, restfpr0_tail<big_endian>);
5679 this->savres_define(symtab,
5680 "._savef", 14, 31,
5681 savefpr<big_endian>, savefpr1_tail<big_endian>);
5682 this->savres_define(symtab,
5683 "._restf", 14, 31,
5684 restfpr<big_endian>, restfpr1_tail<big_endian>);
5685 this->savres_define(symtab,
5686 "_savevr_", 20, 31,
5687 savevr<big_endian>, savevr_tail<big_endian>);
5688 this->savres_define(symtab,
5689 "_restvr_", 20, 31,
5690 restvr<big_endian>, restvr_tail<big_endian>);
5691}
5692
5693template<int size, bool big_endian>
5694void
5695Output_data_save_res<size, big_endian>::savres_define(
5696 Symbol_table* symtab,
5697 const char *name,
5698 unsigned int lo, unsigned int hi,
5699 unsigned char* write_ent(unsigned char*, int),
5700 unsigned char* write_tail(unsigned char*, int))
5701{
5702 size_t len = strlen(name);
5703 bool writing = false;
5704 char sym[16];
5705
5706 memcpy(sym, name, len);
5707 sym[len + 2] = 0;
5708
5709 for (unsigned int i = lo; i <= hi; i++)
5710 {
5711 sym[len + 0] = i / 10 + '0';
5712 sym[len + 1] = i % 10 + '0';
5713 Symbol* gsym = symtab->lookup(sym);
5714 bool refd = gsym != NULL && gsym->is_undefined();
5715 writing = writing || refd;
5716 if (writing)
5717 {
5718 if (this->contents_ == NULL)
5719 this->contents_ = new unsigned char[this->savres_max];
5720
ec661b9d 5721 section_size_type value = this->current_data_size();
f3a0ed29
AM
5722 unsigned char* p = this->contents_ + value;
5723 if (i != hi)
5724 p = write_ent(p, i);
5725 else
5726 p = write_tail(p, i);
ec661b9d 5727 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
5728 this->set_current_data_size(cur_size);
5729 if (refd)
5730 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5731 this, value, cur_size - value,
5732 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5733 elfcpp::STV_HIDDEN, 0, false, false);
5734 }
5735 }
5736}
5737
5738// Write out save/restore.
5739
5740template<int size, bool big_endian>
5741void
5742Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5743{
ec661b9d 5744 const section_size_type off = this->offset();
f3a0ed29
AM
5745 const section_size_type oview_size =
5746 convert_to_section_size_type(this->data_size());
5747 unsigned char* const oview = of->get_output_view(off, oview_size);
5748 memcpy(oview, this->contents_, oview_size);
5749 of->write_output_view(off, oview_size, oview);
5750}
5751
5752
cf43a2fe 5753// Create the glink section.
42cacb20 5754
cf43a2fe
AM
5755template<int size, bool big_endian>
5756void
5757Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5758{
5759 if (this->glink_ == NULL)
5760 {
5761 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 5762 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
5763 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5764 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5765 this->glink_, ORDER_TEXT, false);
5766 }
42cacb20
DE
5767}
5768
5769// Create a PLT entry for a global symbol.
5770
5771template<int size, bool big_endian>
5772void
ec661b9d
AM
5773Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5774 Layout* layout,
5775 Symbol* gsym)
42cacb20 5776{
e5d5f5ed
AM
5777 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5778 && gsym->can_use_relative_reloc(false))
5779 {
5780 if (this->iplt_ == NULL)
40b469d7 5781 this->make_iplt_section(symtab, layout);
03e25981 5782 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
5783 }
5784 else
5785 {
5786 if (this->plt_ == NULL)
40b469d7 5787 this->make_plt_section(symtab, layout);
03e25981 5788 this->plt_->add_entry(gsym);
e5d5f5ed 5789 }
e5d5f5ed 5790}
42cacb20 5791
e5d5f5ed 5792// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 5793
e5d5f5ed
AM
5794template<int size, bool big_endian>
5795void
5796Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 5797 Symbol_table* symtab,
e5d5f5ed 5798 Layout* layout,
ec661b9d
AM
5799 Sized_relobj_file<size, big_endian>* relobj,
5800 unsigned int r_sym)
e5d5f5ed
AM
5801{
5802 if (this->iplt_ == NULL)
40b469d7 5803 this->make_iplt_section(symtab, layout);
03e25981 5804 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
5805}
5806
0e70b911
CC
5807// Return the number of entries in the PLT.
5808
5809template<int size, bool big_endian>
5810unsigned int
5811Target_powerpc<size, big_endian>::plt_entry_count() const
5812{
5813 if (this->plt_ == NULL)
5814 return 0;
b3ccdeb5 5815 return this->plt_->entry_count();
0e70b911
CC
5816}
5817
dd93cd0a 5818// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
5819
5820template<int size, bool big_endian>
5821unsigned int
dd93cd0a 5822Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
5823 Symbol_table* symtab,
5824 Layout* layout,
5825 Sized_relobj_file<size, big_endian>* object)
42cacb20 5826{
dd93cd0a 5827 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
5828 {
5829 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5830 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
5831 Output_data_got_powerpc<size, big_endian>* got
5832 = this->got_section(symtab, layout);
5833 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
5834 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5835 got_offset, 0);
dd93cd0a 5836 this->tlsld_got_offset_ = got_offset;
42cacb20 5837 }
dd93cd0a 5838 return this->tlsld_got_offset_;
42cacb20
DE
5839}
5840
95a2c8d6
RS
5841// Get the Reference_flags for a particular relocation.
5842
5843template<int size, bool big_endian>
5844int
88b8e639
AM
5845Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5846 unsigned int r_type,
5847 const Target_powerpc* target)
95a2c8d6 5848{
88b8e639
AM
5849 int ref = 0;
5850
95a2c8d6
RS
5851 switch (r_type)
5852 {
5853 case elfcpp::R_POWERPC_NONE:
5854 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5855 case elfcpp::R_POWERPC_GNU_VTENTRY:
5856 case elfcpp::R_PPC64_TOC:
5857 // No symbol reference.
88b8e639 5858 break;
95a2c8d6 5859
dd93cd0a
AM
5860 case elfcpp::R_PPC64_ADDR64:
5861 case elfcpp::R_PPC64_UADDR64:
5862 case elfcpp::R_POWERPC_ADDR32:
5863 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 5864 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 5865 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
5866 case elfcpp::R_POWERPC_ADDR16_LO:
5867 case elfcpp::R_POWERPC_ADDR16_HI:
5868 case elfcpp::R_POWERPC_ADDR16_HA:
88b8e639
AM
5869 ref = Symbol::ABSOLUTE_REF;
5870 break;
95a2c8d6 5871
dd93cd0a
AM
5872 case elfcpp::R_POWERPC_ADDR24:
5873 case elfcpp::R_POWERPC_ADDR14:
5874 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5875 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
5876 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5877 break;
dd93cd0a 5878
e5d5f5ed 5879 case elfcpp::R_PPC64_REL64:
dd93cd0a 5880 case elfcpp::R_POWERPC_REL32:
95a2c8d6 5881 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
5882 case elfcpp::R_POWERPC_REL16:
5883 case elfcpp::R_POWERPC_REL16_LO:
5884 case elfcpp::R_POWERPC_REL16_HI:
5885 case elfcpp::R_POWERPC_REL16_HA:
88b8e639
AM
5886 ref = Symbol::RELATIVE_REF;
5887 break;
95a2c8d6 5888
dd93cd0a 5889 case elfcpp::R_POWERPC_REL24:
95a2c8d6 5890 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
5891 case elfcpp::R_POWERPC_REL14:
5892 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5893 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
5894 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5895 break;
95a2c8d6
RS
5896
5897 case elfcpp::R_POWERPC_GOT16:
5898 case elfcpp::R_POWERPC_GOT16_LO:
5899 case elfcpp::R_POWERPC_GOT16_HI:
5900 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
5901 case elfcpp::R_PPC64_GOT16_DS:
5902 case elfcpp::R_PPC64_GOT16_LO_DS:
95a2c8d6
RS
5903 case elfcpp::R_PPC64_TOC16:
5904 case elfcpp::R_PPC64_TOC16_LO:
5905 case elfcpp::R_PPC64_TOC16_HI:
5906 case elfcpp::R_PPC64_TOC16_HA:
5907 case elfcpp::R_PPC64_TOC16_DS:
5908 case elfcpp::R_PPC64_TOC16_LO_DS:
32d849b3 5909 ref = Symbol::RELATIVE_REF;
88b8e639 5910 break;
95a2c8d6
RS
5911
5912 case elfcpp::R_POWERPC_GOT_TPREL16:
5913 case elfcpp::R_POWERPC_TLS:
88b8e639
AM
5914 ref = Symbol::TLS_REF;
5915 break;
95a2c8d6
RS
5916
5917 case elfcpp::R_POWERPC_COPY:
5918 case elfcpp::R_POWERPC_GLOB_DAT:
5919 case elfcpp::R_POWERPC_JMP_SLOT:
5920 case elfcpp::R_POWERPC_RELATIVE:
5921 case elfcpp::R_POWERPC_DTPMOD:
5922 default:
5923 // Not expected. We will give an error later.
88b8e639 5924 break;
95a2c8d6 5925 }
88b8e639
AM
5926
5927 if (size == 64 && target->abiversion() < 2)
5928 ref |= Symbol::FUNC_DESC_ABI;
5929 return ref;
95a2c8d6
RS
5930}
5931
42cacb20
DE
5932// Report an unsupported relocation against a local symbol.
5933
5934template<int size, bool big_endian>
5935void
5936Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
5937 Sized_relobj_file<size, big_endian>* object,
5938 unsigned int r_type)
42cacb20
DE
5939{
5940 gold_error(_("%s: unsupported reloc %u against local symbol"),
5941 object->name().c_str(), r_type);
5942}
5943
5944// We are about to emit a dynamic relocation of type R_TYPE. If the
5945// dynamic linker does not support it, issue an error.
5946
5947template<int size, bool big_endian>
5948void
5949Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5950 unsigned int r_type)
5951{
5952 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5953
5954 // These are the relocation types supported by glibc for both 32-bit
5955 // and 64-bit powerpc.
5956 switch (r_type)
5957 {
3ea0a085 5958 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
5959 case elfcpp::R_POWERPC_RELATIVE:
5960 case elfcpp::R_POWERPC_GLOB_DAT:
5961 case elfcpp::R_POWERPC_DTPMOD:
5962 case elfcpp::R_POWERPC_DTPREL:
5963 case elfcpp::R_POWERPC_TPREL:
5964 case elfcpp::R_POWERPC_JMP_SLOT:
5965 case elfcpp::R_POWERPC_COPY:
3ea0a085 5966 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 5967 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 5968 case elfcpp::R_POWERPC_UADDR32:
42cacb20 5969 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
5970 case elfcpp::R_POWERPC_ADDR16:
5971 case elfcpp::R_POWERPC_UADDR16:
5972 case elfcpp::R_POWERPC_ADDR16_LO:
5973 case elfcpp::R_POWERPC_ADDR16_HI:
5974 case elfcpp::R_POWERPC_ADDR16_HA:
5975 case elfcpp::R_POWERPC_ADDR14:
5976 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5977 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5978 case elfcpp::R_POWERPC_REL32:
42cacb20 5979 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
5980 case elfcpp::R_POWERPC_TPREL16:
5981 case elfcpp::R_POWERPC_TPREL16_LO:
5982 case elfcpp::R_POWERPC_TPREL16_HI:
5983 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
5984 return;
5985
5986 default:
5987 break;
5988 }
5989
5990 if (size == 64)
5991 {
5992 switch (r_type)
5993 {
5994 // These are the relocation types supported only on 64-bit.
5995 case elfcpp::R_PPC64_ADDR64:
42cacb20 5996 case elfcpp::R_PPC64_UADDR64:
3ea0a085 5997 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 5998 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 5999 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
6000 case elfcpp::R_PPC64_ADDR16_HIGH:
6001 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
6002 case elfcpp::R_PPC64_ADDR16_HIGHER:
6003 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6004 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6005 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 6006 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
6007 case elfcpp::R_POWERPC_ADDR30:
6008 case elfcpp::R_PPC64_TPREL16_DS:
6009 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6010 case elfcpp::R_PPC64_TPREL16_HIGH:
6011 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
6012 case elfcpp::R_PPC64_TPREL16_HIGHER:
6013 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6014 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6015 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
6016 return;
6017
6018 default:
6019 break;
6020 }
6021 }
6022 else
6023 {
6024 switch (r_type)
6025 {
6026 // These are the relocation types supported only on 32-bit.
3ea0a085
AM
6027 // ??? glibc ld.so doesn't need to support these.
6028 case elfcpp::R_POWERPC_DTPREL16:
6029 case elfcpp::R_POWERPC_DTPREL16_LO:
6030 case elfcpp::R_POWERPC_DTPREL16_HI:
6031 case elfcpp::R_POWERPC_DTPREL16_HA:
6032 return;
42cacb20
DE
6033
6034 default:
6035 break;
6036 }
6037 }
6038
6039 // This prevents us from issuing more than one error per reloc
6040 // section. But we can still wind up issuing more than one
6041 // error per object file.
6042 if (this->issued_non_pic_error_)
6043 return;
33aea2fd 6044 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
6045 object->error(_("requires unsupported dynamic reloc; "
6046 "recompile with -fPIC"));
6047 this->issued_non_pic_error_ = true;
6048 return;
6049}
6050
e5d5f5ed
AM
6051// Return whether we need to make a PLT entry for a relocation of the
6052// given type against a STT_GNU_IFUNC symbol.
6053
6054template<int size, bool big_endian>
6055bool
6056Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 6057 Target_powerpc<size, big_endian>* target,
e5d5f5ed 6058 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
6059 unsigned int r_type,
6060 bool report_err)
e5d5f5ed 6061{
c9824451
AM
6062 // In non-pic code any reference will resolve to the plt call stub
6063 // for the ifunc symbol.
9055360d
AM
6064 if ((size == 32 || target->abiversion() >= 2)
6065 && !parameters->options().output_is_position_independent())
c9824451
AM
6066 return true;
6067
e5d5f5ed
AM
6068 switch (r_type)
6069 {
b3ccdeb5 6070 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
6071 case elfcpp::R_POWERPC_ADDR32:
6072 case elfcpp::R_POWERPC_UADDR32:
6073 if (size == 32)
b3ccdeb5 6074 return false;
e5d5f5ed
AM
6075 break;
6076
6077 case elfcpp::R_PPC64_ADDR64:
6078 case elfcpp::R_PPC64_UADDR64:
6079 if (size == 64)
b3ccdeb5 6080 return false;
e5d5f5ed
AM
6081 break;
6082
b3ccdeb5 6083 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
6084 case elfcpp::R_POWERPC_GOT16:
6085 case elfcpp::R_POWERPC_GOT16_LO:
6086 case elfcpp::R_POWERPC_GOT16_HI:
6087 case elfcpp::R_POWERPC_GOT16_HA:
6088 case elfcpp::R_PPC64_GOT16_DS:
6089 case elfcpp::R_PPC64_GOT16_LO_DS:
b3ccdeb5 6090 return false;
e5d5f5ed 6091
b3ccdeb5 6092 // Function calls are good, and these do need a PLT entry.
e5d5f5ed
AM
6093 case elfcpp::R_POWERPC_ADDR24:
6094 case elfcpp::R_POWERPC_ADDR14:
6095 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6096 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6097 case elfcpp::R_POWERPC_REL24:
6098 case elfcpp::R_PPC_PLTREL24:
6099 case elfcpp::R_POWERPC_REL14:
6100 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6101 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6102 return true;
6103
6104 default:
6105 break;
6106 }
6107
6108 // Anything else is a problem.
6109 // If we are building a static executable, the libc startup function
6110 // responsible for applying indirect function relocations is going
6111 // to complain about the reloc type.
6112 // If we are building a dynamic executable, we will have a text
6113 // relocation. The dynamic loader will set the text segment
6114 // writable and non-executable to apply text relocations. So we'll
6115 // segfault when trying to run the indirection function to resolve
6116 // the reloc.
b3ccdeb5
AM
6117 if (report_err)
6118 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
6119 object->name().c_str(), r_type);
6120 return false;
6121}
6122
5edad15d
AM
6123// Return TRUE iff INSN is one we expect on a _LO variety toc/got
6124// reloc.
6125
6126static bool
6127ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6128{
6129 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6130 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6131 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6132 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6133 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6134 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6135 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6136 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6137 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6138 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6139 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6140 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6141 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6142 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6143 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6144 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6145 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6146 /* Exclude lfqu by testing reloc. If relocs are ever
6147 defined for the reduced D field in psq_lu then those
6148 will need testing too. */
6149 && r_type != elfcpp::R_PPC64_TOC16_LO
6150 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6151 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6152 && (insn & 1) == 0)
6153 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6154 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6155 /* Exclude stfqu. psq_stu as above for psq_lu. */
6156 && r_type != elfcpp::R_PPC64_TOC16_LO
6157 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6158 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6159 && (insn & 1) == 0));
6160}
6161
42cacb20
DE
6162// Scan a relocation for a local symbol.
6163
6164template<int size, bool big_endian>
6165inline void
6166Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
6167 Symbol_table* symtab,
6168 Layout* layout,
6169 Target_powerpc<size, big_endian>* target,
6170 Sized_relobj_file<size, big_endian>* object,
6171 unsigned int data_shndx,
6172 Output_section* output_section,
6173 const elfcpp::Rela<size, big_endian>& reloc,
6174 unsigned int r_type,
e5d5f5ed 6175 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 6176 bool is_discarded)
42cacb20 6177{
e3deeb9c
AM
6178 this->maybe_skip_tls_get_addr_call(r_type, NULL);
6179
6180 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6181 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6182 {
6183 this->expect_tls_get_addr_call();
6184 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6185 if (tls_type != tls::TLSOPT_NONE)
6186 this->skip_next_tls_get_addr_call();
6187 }
6188 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6189 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6190 {
6191 this->expect_tls_get_addr_call();
6192 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6193 if (tls_type != tls::TLSOPT_NONE)
6194 this->skip_next_tls_get_addr_call();
6195 }
6196
dd93cd0a
AM
6197 Powerpc_relobj<size, big_endian>* ppc_object
6198 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6199
bfdfa4cd
AM
6200 if (is_discarded)
6201 {
6202 if (size == 64
6203 && data_shndx == ppc_object->opd_shndx()
6204 && r_type == elfcpp::R_PPC64_ADDR64)
6205 ppc_object->set_opd_discard(reloc.get_r_offset());
6206 return;
6207 }
6208
e5d5f5ed
AM
6209 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6210 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 6211 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 6212 {
ec661b9d
AM
6213 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6214 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6215 r_type, r_sym, reloc.get_r_addend());
6216 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 6217 }
e5d5f5ed 6218
42cacb20
DE
6219 switch (r_type)
6220 {
6221 case elfcpp::R_POWERPC_NONE:
6222 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6223 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 6224 case elfcpp::R_POWERPC_TLS:
549dba71 6225 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
6226 break;
6227
6228 case elfcpp::R_PPC64_TOC:
6229 {
6230 Output_data_got_powerpc<size, big_endian>* got
6231 = target->got_section(symtab, layout);
6232 if (parameters->options().output_is_position_independent())
6233 {
bfdfa4cd
AM
6234 Address off = reloc.get_r_offset();
6235 if (size == 64
9055360d 6236 && target->abiversion() < 2
bfdfa4cd
AM
6237 && data_shndx == ppc_object->opd_shndx()
6238 && ppc_object->get_opd_discard(off - 8))
6239 break;
6240
dd93cd0a 6241 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 6242 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
6243 rela_dyn->add_output_section_relative(got->output_section(),
6244 elfcpp::R_POWERPC_RELATIVE,
6245 output_section,
bfdfa4cd
AM
6246 object, data_shndx, off,
6247 symobj->toc_base_offset());
dd93cd0a
AM
6248 }
6249 }
42cacb20
DE
6250 break;
6251
6252 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 6253 case elfcpp::R_PPC64_UADDR64:
42cacb20 6254 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
6255 case elfcpp::R_POWERPC_UADDR32:
6256 case elfcpp::R_POWERPC_ADDR24:
c9269dff 6257 case elfcpp::R_POWERPC_ADDR16:
42cacb20 6258 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
6259 case elfcpp::R_POWERPC_ADDR16_HI:
6260 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 6261 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
6262 case elfcpp::R_PPC64_ADDR16_HIGH:
6263 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
6264 case elfcpp::R_PPC64_ADDR16_HIGHER:
6265 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6266 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6267 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6268 case elfcpp::R_PPC64_ADDR16_DS:
6269 case elfcpp::R_PPC64_ADDR16_LO_DS:
6270 case elfcpp::R_POWERPC_ADDR14:
6271 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6272 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
6273 // If building a shared library (or a position-independent
6274 // executable), we need to create a dynamic relocation for
6275 // this location.
c9824451 6276 if (parameters->options().output_is_position_independent()
9055360d 6277 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 6278 {
b3ccdeb5
AM
6279 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6280 is_ifunc);
1f98a074 6281 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
6282 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6283 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 6284 {
b3ccdeb5
AM
6285 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6286 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 6287 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
6288 output_section, data_shndx,
6289 reloc.get_r_offset(),
c9824451 6290 reloc.get_r_addend(), false);
2e702c99 6291 }
1f98a074 6292 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 6293 {
dd93cd0a 6294 check_non_pic(object, r_type);
dd93cd0a
AM
6295 rela_dyn->add_local(object, r_sym, r_type, output_section,
6296 data_shndx, reloc.get_r_offset(),
6297 reloc.get_r_addend());
2e702c99 6298 }
1f98a074
AM
6299 else
6300 {
6301 gold_assert(lsym.get_st_value() == 0);
6302 unsigned int shndx = lsym.get_st_shndx();
6303 bool is_ordinary;
6304 shndx = object->adjust_sym_shndx(r_sym, shndx,
6305 &is_ordinary);
6306 if (!is_ordinary)
6307 object->error(_("section symbol %u has bad shndx %u"),
6308 r_sym, shndx);
6309 else
6310 rela_dyn->add_local_section(object, shndx, r_type,
6311 output_section, data_shndx,
6312 reloc.get_r_offset());
6313 }
2e702c99 6314 }
42cacb20
DE
6315 break;
6316
6317 case elfcpp::R_POWERPC_REL24:
c9824451 6318 case elfcpp::R_PPC_PLTREL24:
42cacb20 6319 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
6320 case elfcpp::R_POWERPC_REL14:
6321 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6322 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 6323 if (!is_ifunc)
0e123f69
AM
6324 {
6325 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6326 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6327 r_type, r_sym, reloc.get_r_addend());
6328 }
ec661b9d
AM
6329 break;
6330
7e57d19e
AM
6331 case elfcpp::R_PPC64_TOCSAVE:
6332 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6333 // caller has already saved r2 and thus a plt call stub need not
6334 // save r2.
6335 if (size == 64
6336 && target->mark_pltcall(ppc_object, data_shndx,
6337 reloc.get_r_offset() - 4, symtab))
6338 {
6339 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6340 unsigned int shndx = lsym.get_st_shndx();
6341 bool is_ordinary;
6342 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6343 if (!is_ordinary)
6344 object->error(_("tocsave symbol %u has bad shndx %u"),
6345 r_sym, shndx);
6346 else
6347 target->add_tocsave(ppc_object, shndx,
6348 lsym.get_st_value() + reloc.get_r_addend());
6349 }
6350 break;
6351
ec661b9d
AM
6352 case elfcpp::R_PPC64_REL64:
6353 case elfcpp::R_POWERPC_REL32:
dd93cd0a 6354 case elfcpp::R_POWERPC_REL16:
6ce78956 6355 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 6356 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 6357 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 6358 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 6359 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 6360 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 6361 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 6362 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
6363 case elfcpp::R_PPC64_SECTOFF_DS:
6364 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6365 case elfcpp::R_POWERPC_TPREL16:
6366 case elfcpp::R_POWERPC_TPREL16_LO:
6367 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 6368 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
6369 case elfcpp::R_PPC64_TPREL16_DS:
6370 case elfcpp::R_PPC64_TPREL16_LO_DS:
6371 case elfcpp::R_PPC64_TPREL16_HIGH:
6372 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 6373 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 6374 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 6375 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 6376 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
6377 case elfcpp::R_POWERPC_DTPREL16:
6378 case elfcpp::R_POWERPC_DTPREL16_LO:
6379 case elfcpp::R_POWERPC_DTPREL16_HI:
6380 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
6381 case elfcpp::R_PPC64_DTPREL16_DS:
6382 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
6383 case elfcpp::R_PPC64_DTPREL16_HIGH:
6384 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6385 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6386 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6387 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6388 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
6389 case elfcpp::R_PPC64_TLSGD:
6390 case elfcpp::R_PPC64_TLSLD:
45965137 6391 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
6392 break;
6393
6394 case elfcpp::R_POWERPC_GOT16:
6395 case elfcpp::R_POWERPC_GOT16_LO:
6396 case elfcpp::R_POWERPC_GOT16_HI:
6397 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
6398 case elfcpp::R_PPC64_GOT16_DS:
6399 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 6400 {
c9269dff 6401 // The symbol requires a GOT entry.
dd93cd0a
AM
6402 Output_data_got_powerpc<size, big_endian>* got
6403 = target->got_section(symtab, layout);
6404 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 6405
e5d5f5ed 6406 if (!parameters->options().output_is_position_independent())
42cacb20 6407 {
b01a4b04
AM
6408 if (is_ifunc
6409 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
6410 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6411 else
6412 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6413 }
6414 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6415 {
6416 // If we are generating a shared object or a pie, this
6417 // symbol's GOT entry will be set by a dynamic relocation.
6418 unsigned int off;
6419 off = got->add_constant(0);
6420 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 6421
b3ccdeb5
AM
6422 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6423 is_ifunc);
6424 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6425 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 6426 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 6427 got, off, 0, false);
2e702c99 6428 }
42cacb20
DE
6429 }
6430 break;
6431
cf43a2fe
AM
6432 case elfcpp::R_PPC64_TOC16:
6433 case elfcpp::R_PPC64_TOC16_LO:
6434 case elfcpp::R_PPC64_TOC16_HI:
6435 case elfcpp::R_PPC64_TOC16_HA:
6436 case elfcpp::R_PPC64_TOC16_DS:
6437 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
6438 // We need a GOT section.
6439 target->got_section(symtab, layout);
6440 break;
6441
dd93cd0a
AM
6442 case elfcpp::R_POWERPC_GOT_TLSGD16:
6443 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6444 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6445 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6446 {
6447 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6448 if (tls_type == tls::TLSOPT_NONE)
6449 {
6450 Output_data_got_powerpc<size, big_endian>* got
6451 = target->got_section(symtab, layout);
6452 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
6453 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6454 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6455 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
6456 }
6457 else if (tls_type == tls::TLSOPT_TO_LE)
6458 {
6459 // no GOT relocs needed for Local Exec.
6460 }
6461 else
6462 gold_unreachable();
6463 }
42cacb20
DE
6464 break;
6465
dd93cd0a
AM
6466 case elfcpp::R_POWERPC_GOT_TLSLD16:
6467 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6468 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6469 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6470 {
6471 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6472 if (tls_type == tls::TLSOPT_NONE)
6473 target->tlsld_got_offset(symtab, layout, object);
6474 else if (tls_type == tls::TLSOPT_TO_LE)
6475 {
6476 // no GOT relocs needed for Local Exec.
7404fe1b
AM
6477 if (parameters->options().emit_relocs())
6478 {
6479 Output_section* os = layout->tls_segment()->first_section();
6480 gold_assert(os != NULL);
6481 os->set_needs_symtab_index();
6482 }
dd93cd0a
AM
6483 }
6484 else
6485 gold_unreachable();
6486 }
42cacb20 6487 break;
42cacb20 6488
dd93cd0a
AM
6489 case elfcpp::R_POWERPC_GOT_DTPREL16:
6490 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6491 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6492 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6493 {
6494 Output_data_got_powerpc<size, big_endian>* got
6495 = target->got_section(symtab, layout);
6496 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 6497 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
6498 }
6499 break;
42cacb20 6500
dd93cd0a
AM
6501 case elfcpp::R_POWERPC_GOT_TPREL16:
6502 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6503 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6504 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6505 {
6506 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6507 if (tls_type == tls::TLSOPT_NONE)
6508 {
dd93cd0a 6509 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
6510 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6511 {
6512 Output_data_got_powerpc<size, big_endian>* got
6513 = target->got_section(symtab, layout);
6514 unsigned int off = got->add_constant(0);
6515 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6516
6517 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6518 rela_dyn->add_symbolless_local_addend(object, r_sym,
6519 elfcpp::R_POWERPC_TPREL,
6520 got, off, 0);
6521 }
dd93cd0a
AM
6522 }
6523 else if (tls_type == tls::TLSOPT_TO_LE)
6524 {
6525 // no GOT relocs needed for Local Exec.
6526 }
6527 else
6528 gold_unreachable();
6529 }
6530 break;
6531
6532 default:
6533 unsupported_reloc_local(object, r_type);
6534 break;
6535 }
d8f5a274 6536
5edad15d
AM
6537 if (size == 64
6538 && parameters->options().toc_optimize())
6539 {
6540 if (data_shndx == ppc_object->toc_shndx())
6541 {
6542 bool ok = true;
6543 if (r_type != elfcpp::R_PPC64_ADDR64
6544 || (is_ifunc && target->abiversion() < 2))
6545 ok = false;
6546 else if (parameters->options().output_is_position_independent())
6547 {
6548 if (is_ifunc)
6549 ok = false;
6550 else
6551 {
6552 unsigned int shndx = lsym.get_st_shndx();
6553 if (shndx >= elfcpp::SHN_LORESERVE
6554 && shndx != elfcpp::SHN_XINDEX)
6555 ok = false;
6556 }
6557 }
6558 if (!ok)
6559 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6560 }
6561
6562 enum {no_check, check_lo, check_ha} insn_check;
6563 switch (r_type)
6564 {
6565 default:
6566 insn_check = no_check;
6567 break;
6568
6569 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6570 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6571 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6572 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6573 case elfcpp::R_POWERPC_GOT16_HA:
6574 case elfcpp::R_PPC64_TOC16_HA:
6575 insn_check = check_ha;
6576 break;
6577
6578 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6579 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6580 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6581 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6582 case elfcpp::R_POWERPC_GOT16_LO:
6583 case elfcpp::R_PPC64_GOT16_LO_DS:
6584 case elfcpp::R_PPC64_TOC16_LO:
6585 case elfcpp::R_PPC64_TOC16_LO_DS:
6586 insn_check = check_lo;
6587 break;
6588 }
6589
6590 section_size_type slen;
6591 const unsigned char* view = NULL;
6592 if (insn_check != no_check)
6593 {
6594 view = ppc_object->section_contents(data_shndx, &slen, false);
6595 section_size_type off =
6596 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6597 if (off < slen)
6598 {
6599 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6600 if (insn_check == check_lo
6601 ? !ok_lo_toc_insn(insn, r_type)
6602 : ((insn & ((0x3f << 26) | 0x1f << 16))
6603 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6604 {
6605 ppc_object->set_no_toc_opt();
6606 gold_warning(_("%s: toc optimization is not supported "
6607 "for %#08x instruction"),
6608 ppc_object->name().c_str(), insn);
6609 }
6610 }
6611 }
6612
6613 switch (r_type)
6614 {
6615 default:
6616 break;
6617 case elfcpp::R_PPC64_TOC16:
6618 case elfcpp::R_PPC64_TOC16_LO:
6619 case elfcpp::R_PPC64_TOC16_HI:
6620 case elfcpp::R_PPC64_TOC16_HA:
6621 case elfcpp::R_PPC64_TOC16_DS:
6622 case elfcpp::R_PPC64_TOC16_LO_DS:
6623 unsigned int shndx = lsym.get_st_shndx();
6624 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6625 bool is_ordinary;
6626 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6627 if (is_ordinary && shndx == ppc_object->toc_shndx())
6628 {
6629 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6630 if (dst_off < ppc_object->section_size(shndx))
6631 {
6632 bool ok = false;
6633 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6634 ok = true;
6635 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6636 {
6637 // Need to check that the insn is a ld
6638 if (!view)
6639 view = ppc_object->section_contents(data_shndx,
6640 &slen,
6641 false);
6642 section_size_type off =
6643 (convert_to_section_size_type(reloc.get_r_offset())
6644 + (big_endian ? -2 : 3));
6645 if (off < slen
6646 && (view[off] & (0x3f << 2)) == 58u << 2)
6647 ok = true;
6648 }
6649 if (!ok)
6650 ppc_object->set_no_toc_opt(dst_off);
6651 }
6652 }
6653 break;
6654 }
6655 }
6656
f159cdb6
AM
6657 if (size == 32)
6658 {
6659 switch (r_type)
6660 {
6661 case elfcpp::R_POWERPC_REL32:
6662 if (ppc_object->got2_shndx() != 0
6663 && parameters->options().output_is_position_independent())
6664 {
6665 unsigned int shndx = lsym.get_st_shndx();
6666 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6667 bool is_ordinary;
6668 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6669 if (is_ordinary && shndx == ppc_object->got2_shndx()
6670 && (ppc_object->section_flags(data_shndx)
6671 & elfcpp::SHF_EXECINSTR) != 0)
6672 gold_error(_("%s: unsupported -mbss-plt code"),
6673 ppc_object->name().c_str());
6674 }
6675 break;
6676 default:
6677 break;
6678 }
6679 }
6680
d8f5a274
AM
6681 switch (r_type)
6682 {
6683 case elfcpp::R_POWERPC_GOT_TLSLD16:
6684 case elfcpp::R_POWERPC_GOT_TLSGD16:
6685 case elfcpp::R_POWERPC_GOT_TPREL16:
6686 case elfcpp::R_POWERPC_GOT_DTPREL16:
6687 case elfcpp::R_POWERPC_GOT16:
6688 case elfcpp::R_PPC64_GOT16_DS:
6689 case elfcpp::R_PPC64_TOC16:
6690 case elfcpp::R_PPC64_TOC16_DS:
6691 ppc_object->set_has_small_toc_reloc();
6692 default:
6693 break;
6694 }
dd93cd0a
AM
6695}
6696
6697// Report an unsupported relocation against a global symbol.
6698
6699template<int size, bool big_endian>
6700void
6701Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6702 Sized_relobj_file<size, big_endian>* object,
6703 unsigned int r_type,
6704 Symbol* gsym)
6705{
42cacb20
DE
6706 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6707 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6708}
6709
6710// Scan a relocation for a global symbol.
6711
6712template<int size, bool big_endian>
6713inline void
6714Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
6715 Symbol_table* symtab,
6716 Layout* layout,
6717 Target_powerpc<size, big_endian>* target,
6718 Sized_relobj_file<size, big_endian>* object,
6719 unsigned int data_shndx,
6720 Output_section* output_section,
6721 const elfcpp::Rela<size, big_endian>& reloc,
6722 unsigned int r_type,
6723 Symbol* gsym)
42cacb20 6724{
e3deeb9c
AM
6725 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6726 return;
6727
6728 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6729 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6730 {
6731 this->expect_tls_get_addr_call();
6732 const bool final = gsym->final_value_is_known();
6733 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6734 if (tls_type != tls::TLSOPT_NONE)
6735 this->skip_next_tls_get_addr_call();
6736 }
6737 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6738 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6739 {
6740 this->expect_tls_get_addr_call();
6741 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6742 if (tls_type != tls::TLSOPT_NONE)
6743 this->skip_next_tls_get_addr_call();
6744 }
6745
dd93cd0a
AM
6746 Powerpc_relobj<size, big_endian>* ppc_object
6747 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6748
e5d5f5ed 6749 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 6750 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
6751 bool pushed_ifunc = false;
6752 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 6753 {
0e123f69 6754 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 6755 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 6756 r_type, r_sym, reloc.get_r_addend());
ec661b9d 6757 target->make_plt_entry(symtab, layout, gsym);
9055360d 6758 pushed_ifunc = true;
ec661b9d 6759 }
e5d5f5ed 6760
42cacb20
DE
6761 switch (r_type)
6762 {
6763 case elfcpp::R_POWERPC_NONE:
6764 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6765 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 6766 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 6767 case elfcpp::R_POWERPC_TLS:
549dba71 6768 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
6769 break;
6770
6771 case elfcpp::R_PPC64_TOC:
6772 {
6773 Output_data_got_powerpc<size, big_endian>* got
6774 = target->got_section(symtab, layout);
6775 if (parameters->options().output_is_position_independent())
6776 {
bfdfa4cd
AM
6777 Address off = reloc.get_r_offset();
6778 if (size == 64
6779 && data_shndx == ppc_object->opd_shndx()
6780 && ppc_object->get_opd_discard(off - 8))
6781 break;
6782
dd93cd0a
AM
6783 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6784 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6785 if (data_shndx != ppc_object->opd_shndx())
6786 symobj = static_cast
6787 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6788 rela_dyn->add_output_section_relative(got->output_section(),
6789 elfcpp::R_POWERPC_RELATIVE,
6790 output_section,
bfdfa4cd 6791 object, data_shndx, off,
dd93cd0a
AM
6792 symobj->toc_base_offset());
6793 }
6794 }
42cacb20
DE
6795 break;
6796
c9269dff 6797 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 6798 if (size == 64
9055360d 6799 && target->abiversion() < 2
bfdfa4cd
AM
6800 && data_shndx == ppc_object->opd_shndx()
6801 && (gsym->is_defined_in_discarded_section()
6802 || gsym->object() != object))
6803 {
6804 ppc_object->set_opd_discard(reloc.get_r_offset());
6805 break;
6806 }
d8e90251 6807 // Fall through.
dd93cd0a 6808 case elfcpp::R_PPC64_UADDR64:
c9269dff 6809 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
6810 case elfcpp::R_POWERPC_UADDR32:
6811 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
6812 case elfcpp::R_POWERPC_ADDR16:
6813 case elfcpp::R_POWERPC_ADDR16_LO:
6814 case elfcpp::R_POWERPC_ADDR16_HI:
6815 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 6816 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
6817 case elfcpp::R_PPC64_ADDR16_HIGH:
6818 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
6819 case elfcpp::R_PPC64_ADDR16_HIGHER:
6820 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6821 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6822 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6823 case elfcpp::R_PPC64_ADDR16_DS:
6824 case elfcpp::R_PPC64_ADDR16_LO_DS:
6825 case elfcpp::R_POWERPC_ADDR14:
6826 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6827 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 6828 {
c9269dff
AM
6829 // Make a PLT entry if necessary.
6830 if (gsym->needs_plt_entry())
6831 {
9055360d
AM
6832 // Since this is not a PC-relative relocation, we may be
6833 // taking the address of a function. In that case we need to
6834 // set the entry in the dynamic symbol table to the address of
6835 // the PLT call stub.
6836 bool need_ifunc_plt = false;
6837 if ((size == 32 || target->abiversion() >= 2)
6838 && gsym->is_from_dynobj()
6839 && !parameters->options().output_is_position_independent())
6840 {
6841 gsym->set_needs_dynsym_value();
6842 need_ifunc_plt = true;
6843 }
6844 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 6845 {
0e123f69 6846 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 6847 target->push_branch(ppc_object, data_shndx,
0e123f69 6848 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
6849 reloc.get_r_addend());
6850 target->make_plt_entry(symtab, layout, gsym);
6851 }
c9269dff
AM
6852 }
6853 // Make a dynamic relocation if necessary.
88b8e639 6854 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 6855 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 6856 {
a82bef93
ST
6857 if (!parameters->options().output_is_position_independent()
6858 && gsym->may_need_copy_reloc())
c9269dff
AM
6859 {
6860 target->copy_reloc(symtab, layout, object,
6861 data_shndx, output_section, gsym, reloc);
6862 }
9055360d
AM
6863 else if ((((size == 32
6864 && r_type == elfcpp::R_POWERPC_ADDR32)
6865 || (size == 64
6866 && r_type == elfcpp::R_PPC64_ADDR64
6867 && target->abiversion() >= 2))
627b30b7
AM
6868 && gsym->can_use_relative_reloc(false)
6869 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6870 && parameters->options().shared()))
6871 || (size == 64
6872 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 6873 && target->abiversion() < 2
627b30b7
AM
6874 && (gsym->can_use_relative_reloc(false)
6875 || data_shndx == ppc_object->opd_shndx())))
2e702c99 6876 {
b3ccdeb5
AM
6877 Reloc_section* rela_dyn
6878 = target->rela_dyn_section(symtab, layout, is_ifunc);
6879 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6880 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
6881 rela_dyn->add_symbolless_global_addend(
6882 gsym, dynrel, output_section, object, data_shndx,
6883 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
6884 }
6885 else
6886 {
b3ccdeb5
AM
6887 Reloc_section* rela_dyn
6888 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 6889 check_non_pic(object, r_type);
dd93cd0a
AM
6890 rela_dyn->add_global(gsym, r_type, output_section,
6891 object, data_shndx,
6892 reloc.get_r_offset(),
6893 reloc.get_r_addend());
5edad15d
AM
6894
6895 if (size == 64
6896 && parameters->options().toc_optimize()
6897 && data_shndx == ppc_object->toc_shndx())
6898 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
6899 }
6900 }
42cacb20
DE
6901 }
6902 break;
6903
cf43a2fe 6904 case elfcpp::R_PPC_PLTREL24:
42cacb20 6905 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
6906 if (!is_ifunc)
6907 {
0e123f69 6908 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 6909 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 6910 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
6911 if (gsym->needs_plt_entry()
6912 || (!gsym->final_value_is_known()
6913 && (gsym->is_undefined()
6914 || gsym->is_from_dynobj()
6915 || gsym->is_preemptible())))
6916 target->make_plt_entry(symtab, layout, gsym);
6917 }
d8e90251 6918 // Fall through.
42cacb20 6919
3ea0a085 6920 case elfcpp::R_PPC64_REL64:
dd93cd0a 6921 case elfcpp::R_POWERPC_REL32:
3ea0a085 6922 // Make a dynamic relocation if necessary.
88b8e639 6923 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 6924 {
a82bef93
ST
6925 if (!parameters->options().output_is_position_independent()
6926 && gsym->may_need_copy_reloc())
3ea0a085
AM
6927 {
6928 target->copy_reloc(symtab, layout, object,
6929 data_shndx, output_section, gsym,
6930 reloc);
6931 }
6932 else
6933 {
b3ccdeb5
AM
6934 Reloc_section* rela_dyn
6935 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
6936 check_non_pic(object, r_type);
6937 rela_dyn->add_global(gsym, r_type, output_section, object,
6938 data_shndx, reloc.get_r_offset(),
6939 reloc.get_r_addend());
6940 }
6941 }
6942 break;
6943
ec661b9d
AM
6944 case elfcpp::R_POWERPC_REL14:
6945 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6946 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 6947 if (!is_ifunc)
0e123f69
AM
6948 {
6949 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6950 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6951 r_type, r_sym, reloc.get_r_addend());
6952 }
ec661b9d
AM
6953 break;
6954
7e57d19e
AM
6955 case elfcpp::R_PPC64_TOCSAVE:
6956 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6957 // caller has already saved r2 and thus a plt call stub need not
6958 // save r2.
6959 if (size == 64
6960 && target->mark_pltcall(ppc_object, data_shndx,
6961 reloc.get_r_offset() - 4, symtab))
6962 {
6963 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6964 bool is_ordinary;
6965 unsigned int shndx = gsym->shndx(&is_ordinary);
6966 if (!is_ordinary)
6967 object->error(_("tocsave symbol %u has bad shndx %u"),
6968 r_sym, shndx);
6969 else
6970 {
6971 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
6972 target->add_tocsave(ppc_object, shndx,
6973 sym->value() + reloc.get_r_addend());
6974 }
6975 }
6976 break;
6977
6ce78956
AM
6978 case elfcpp::R_POWERPC_REL16:
6979 case elfcpp::R_POWERPC_REL16_LO:
6980 case elfcpp::R_POWERPC_REL16_HI:
6981 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 6982 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 6983 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 6984 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 6985 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 6986 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
6987 case elfcpp::R_PPC64_SECTOFF_DS:
6988 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6989 case elfcpp::R_POWERPC_TPREL16:
6990 case elfcpp::R_POWERPC_TPREL16_LO:
6991 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 6992 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
6993 case elfcpp::R_PPC64_TPREL16_DS:
6994 case elfcpp::R_PPC64_TPREL16_LO_DS:
6995 case elfcpp::R_PPC64_TPREL16_HIGH:
6996 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 6997 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 6998 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 6999 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 7000 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
7001 case elfcpp::R_POWERPC_DTPREL16:
7002 case elfcpp::R_POWERPC_DTPREL16_LO:
7003 case elfcpp::R_POWERPC_DTPREL16_HI:
7004 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
7005 case elfcpp::R_PPC64_DTPREL16_DS:
7006 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
7007 case elfcpp::R_PPC64_DTPREL16_HIGH:
7008 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7009 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7010 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7011 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7012 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
7013 case elfcpp::R_PPC64_TLSGD:
7014 case elfcpp::R_PPC64_TLSLD:
45965137 7015 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
7016 break;
7017
42cacb20
DE
7018 case elfcpp::R_POWERPC_GOT16:
7019 case elfcpp::R_POWERPC_GOT16_LO:
7020 case elfcpp::R_POWERPC_GOT16_HI:
7021 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
7022 case elfcpp::R_PPC64_GOT16_DS:
7023 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 7024 {
c9269dff
AM
7025 // The symbol requires a GOT entry.
7026 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
7027
7028 got = target->got_section(symtab, layout);
2e702c99 7029 if (gsym->final_value_is_known())
2e702c99 7030 {
b01a4b04
AM
7031 if (is_ifunc
7032 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
7033 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7034 else
7035 got->add_global(gsym, GOT_TYPE_STANDARD);
7036 }
7037 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7038 {
7039 // If we are generating a shared object or a pie, this
7040 // symbol's GOT entry will be set by a dynamic relocation.
7041 unsigned int off = got->add_constant(0);
7042 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7043
b3ccdeb5
AM
7044 Reloc_section* rela_dyn
7045 = target->rela_dyn_section(symtab, layout, is_ifunc);
7046
e5d5f5ed 7047 if (gsym->can_use_relative_reloc(false)
9055360d
AM
7048 && !((size == 32
7049 || target->abiversion() >= 2)
e5d5f5ed
AM
7050 && gsym->visibility() == elfcpp::STV_PROTECTED
7051 && parameters->options().shared()))
2e702c99 7052 {
b3ccdeb5
AM
7053 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7054 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
7055 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7056 }
7057 else
7058 {
7059 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7060 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 7061 }
2e702c99 7062 }
42cacb20
DE
7063 }
7064 break;
7065
cf43a2fe
AM
7066 case elfcpp::R_PPC64_TOC16:
7067 case elfcpp::R_PPC64_TOC16_LO:
7068 case elfcpp::R_PPC64_TOC16_HI:
7069 case elfcpp::R_PPC64_TOC16_HA:
7070 case elfcpp::R_PPC64_TOC16_DS:
7071 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
7072 // We need a GOT section.
7073 target->got_section(symtab, layout);
7074 break;
7075
dd93cd0a
AM
7076 case elfcpp::R_POWERPC_GOT_TLSGD16:
7077 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7078 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7079 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7080 {
7081 const bool final = gsym->final_value_is_known();
7082 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7083 if (tls_type == tls::TLSOPT_NONE)
7084 {
7085 Output_data_got_powerpc<size, big_endian>* got
7086 = target->got_section(symtab, layout);
b3ccdeb5
AM
7087 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7088 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
7089 elfcpp::R_POWERPC_DTPMOD,
7090 elfcpp::R_POWERPC_DTPREL);
7091 }
7092 else if (tls_type == tls::TLSOPT_TO_IE)
7093 {
acc276d8
AM
7094 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7095 {
7096 Output_data_got_powerpc<size, big_endian>* got
7097 = target->got_section(symtab, layout);
7098 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7099 if (gsym->is_undefined()
7100 || gsym->is_from_dynobj())
7101 {
7102 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7103 elfcpp::R_POWERPC_TPREL);
7104 }
7105 else
7106 {
7107 unsigned int off = got->add_constant(0);
7108 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7109 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7110 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7111 got, off, 0);
7112 }
7113 }
dd93cd0a
AM
7114 }
7115 else if (tls_type == tls::TLSOPT_TO_LE)
7116 {
7117 // no GOT relocs needed for Local Exec.
7118 }
7119 else
7120 gold_unreachable();
7121 }
42cacb20
DE
7122 break;
7123
dd93cd0a
AM
7124 case elfcpp::R_POWERPC_GOT_TLSLD16:
7125 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7126 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7127 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7128 {
7129 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7130 if (tls_type == tls::TLSOPT_NONE)
7131 target->tlsld_got_offset(symtab, layout, object);
7132 else if (tls_type == tls::TLSOPT_TO_LE)
7133 {
7134 // no GOT relocs needed for Local Exec.
7404fe1b
AM
7135 if (parameters->options().emit_relocs())
7136 {
7137 Output_section* os = layout->tls_segment()->first_section();
7138 gold_assert(os != NULL);
7139 os->set_needs_symtab_index();
7140 }
dd93cd0a
AM
7141 }
7142 else
7143 gold_unreachable();
7144 }
7145 break;
7146
7147 case elfcpp::R_POWERPC_GOT_DTPREL16:
7148 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7149 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7150 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7151 {
7152 Output_data_got_powerpc<size, big_endian>* got
7153 = target->got_section(symtab, layout);
bd73a62d
AM
7154 if (!gsym->final_value_is_known()
7155 && (gsym->is_from_dynobj()
7156 || gsym->is_undefined()
7157 || gsym->is_preemptible()))
7158 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7159 target->rela_dyn_section(layout),
7160 elfcpp::R_POWERPC_DTPREL);
7161 else
7162 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
7163 }
7164 break;
7165
7166 case elfcpp::R_POWERPC_GOT_TPREL16:
7167 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7168 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7169 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7170 {
7171 const bool final = gsym->final_value_is_known();
7172 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7173 if (tls_type == tls::TLSOPT_NONE)
7174 {
acc276d8
AM
7175 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7176 {
7177 Output_data_got_powerpc<size, big_endian>* got
7178 = target->got_section(symtab, layout);
7179 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7180 if (gsym->is_undefined()
7181 || gsym->is_from_dynobj())
7182 {
7183 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7184 elfcpp::R_POWERPC_TPREL);
7185 }
7186 else
7187 {
7188 unsigned int off = got->add_constant(0);
7189 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7190 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7191 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7192 got, off, 0);
7193 }
7194 }
dd93cd0a
AM
7195 }
7196 else if (tls_type == tls::TLSOPT_TO_LE)
7197 {
7198 // no GOT relocs needed for Local Exec.
7199 }
7200 else
7201 gold_unreachable();
7202 }
42cacb20
DE
7203 break;
7204
7205 default:
7206 unsupported_reloc_global(object, r_type, gsym);
7207 break;
7208 }
d8f5a274 7209
5edad15d
AM
7210 if (size == 64
7211 && parameters->options().toc_optimize())
7212 {
7213 if (data_shndx == ppc_object->toc_shndx())
7214 {
7215 bool ok = true;
7216 if (r_type != elfcpp::R_PPC64_ADDR64
7217 || (is_ifunc && target->abiversion() < 2))
7218 ok = false;
7219 else if (parameters->options().output_is_position_independent()
7220 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7221 ok = false;
7222 if (!ok)
7223 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7224 }
7225
7226 enum {no_check, check_lo, check_ha} insn_check;
7227 switch (r_type)
7228 {
7229 default:
7230 insn_check = no_check;
7231 break;
7232
7233 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7234 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7235 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7236 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7237 case elfcpp::R_POWERPC_GOT16_HA:
7238 case elfcpp::R_PPC64_TOC16_HA:
7239 insn_check = check_ha;
7240 break;
7241
7242 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7243 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7244 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7245 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7246 case elfcpp::R_POWERPC_GOT16_LO:
7247 case elfcpp::R_PPC64_GOT16_LO_DS:
7248 case elfcpp::R_PPC64_TOC16_LO:
7249 case elfcpp::R_PPC64_TOC16_LO_DS:
7250 insn_check = check_lo;
7251 break;
7252 }
7253
7254 section_size_type slen;
7255 const unsigned char* view = NULL;
7256 if (insn_check != no_check)
7257 {
7258 view = ppc_object->section_contents(data_shndx, &slen, false);
7259 section_size_type off =
7260 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7261 if (off < slen)
7262 {
7263 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7264 if (insn_check == check_lo
7265 ? !ok_lo_toc_insn(insn, r_type)
7266 : ((insn & ((0x3f << 26) | 0x1f << 16))
7267 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7268 {
7269 ppc_object->set_no_toc_opt();
7270 gold_warning(_("%s: toc optimization is not supported "
7271 "for %#08x instruction"),
7272 ppc_object->name().c_str(), insn);
7273 }
7274 }
7275 }
7276
7277 switch (r_type)
7278 {
7279 default:
7280 break;
7281 case elfcpp::R_PPC64_TOC16:
7282 case elfcpp::R_PPC64_TOC16_LO:
7283 case elfcpp::R_PPC64_TOC16_HI:
7284 case elfcpp::R_PPC64_TOC16_HA:
7285 case elfcpp::R_PPC64_TOC16_DS:
7286 case elfcpp::R_PPC64_TOC16_LO_DS:
7287 if (gsym->source() == Symbol::FROM_OBJECT
7288 && !gsym->object()->is_dynamic())
7289 {
7290 Powerpc_relobj<size, big_endian>* sym_object
7291 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7292 bool is_ordinary;
7293 unsigned int shndx = gsym->shndx(&is_ordinary);
7294 if (shndx == sym_object->toc_shndx())
7295 {
7296 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7297 Address dst_off = sym->value() + reloc.get_r_offset();
7298 if (dst_off < sym_object->section_size(shndx))
7299 {
7300 bool ok = false;
7301 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7302 ok = true;
7303 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7304 {
7305 // Need to check that the insn is a ld
7306 if (!view)
7307 view = ppc_object->section_contents(data_shndx,
7308 &slen,
7309 false);
7310 section_size_type off =
7311 (convert_to_section_size_type(reloc.get_r_offset())
7312 + (big_endian ? -2 : 3));
7313 if (off < slen
7314 && (view[off] & (0x3f << 2)) == (58u << 2))
7315 ok = true;
7316 }
7317 if (!ok)
7318 sym_object->set_no_toc_opt(dst_off);
7319 }
7320 }
7321 }
7322 break;
7323 }
7324 }
7325
f159cdb6
AM
7326 if (size == 32)
7327 {
7328 switch (r_type)
7329 {
7330 case elfcpp::R_PPC_LOCAL24PC:
7331 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7332 gold_error(_("%s: unsupported -mbss-plt code"),
7333 ppc_object->name().c_str());
7334 break;
7335 default:
7336 break;
7337 }
7338 }
7339
d8f5a274
AM
7340 switch (r_type)
7341 {
7342 case elfcpp::R_POWERPC_GOT_TLSLD16:
7343 case elfcpp::R_POWERPC_GOT_TLSGD16:
7344 case elfcpp::R_POWERPC_GOT_TPREL16:
7345 case elfcpp::R_POWERPC_GOT_DTPREL16:
7346 case elfcpp::R_POWERPC_GOT16:
7347 case elfcpp::R_PPC64_GOT16_DS:
7348 case elfcpp::R_PPC64_TOC16:
7349 case elfcpp::R_PPC64_TOC16_DS:
7350 ppc_object->set_has_small_toc_reloc();
7351 default:
7352 break;
7353 }
42cacb20
DE
7354}
7355
6d03d481
ST
7356// Process relocations for gc.
7357
7358template<int size, bool big_endian>
7359void
7360Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
7361 Symbol_table* symtab,
7362 Layout* layout,
7363 Sized_relobj_file<size, big_endian>* object,
7364 unsigned int data_shndx,
7365 unsigned int,
7366 const unsigned char* prelocs,
7367 size_t reloc_count,
7368 Output_section* output_section,
7369 bool needs_special_offset_handling,
7370 size_t local_symbol_count,
7371 const unsigned char* plocal_symbols)
6d03d481
ST
7372{
7373 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
7374 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7375 Classify_reloc;
7376
e81fea4d
AM
7377 Powerpc_relobj<size, big_endian>* ppc_object
7378 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7379 if (size == 64)
7380 ppc_object->set_opd_valid();
7381 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7382 {
7383 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7384 for (p = ppc_object->access_from_map()->begin();
7385 p != ppc_object->access_from_map()->end();
7386 ++p)
7387 {
7388 Address dst_off = p->first;
7389 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7390 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7391 for (s = p->second.begin(); s != p->second.end(); ++s)
7392 {
efc6fa12 7393 Relobj* src_obj = s->first;
e81fea4d
AM
7394 unsigned int src_indx = s->second;
7395 symtab->gc()->add_reference(src_obj, src_indx,
7396 ppc_object, dst_indx);
7397 }
7398 p->second.clear();
7399 }
7400 ppc_object->access_from_map()->clear();
c6de8ed4 7401 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
7402 // Don't look at .opd relocs as .opd will reference everything.
7403 return;
7404 }
6d03d481 7405
4d625b70 7406 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
7407 symtab,
7408 layout,
7409 this,
7410 object,
7411 data_shndx,
7412 prelocs,
7413 reloc_count,
7414 output_section,
7415 needs_special_offset_handling,
7416 local_symbol_count,
7417 plocal_symbols);
7418}
7419
e81fea4d
AM
7420// Handle target specific gc actions when adding a gc reference from
7421// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7422// and DST_OFF. For powerpc64, this adds a referenc to the code
7423// section of a function descriptor.
7424
7425template<int size, bool big_endian>
7426void
7427Target_powerpc<size, big_endian>::do_gc_add_reference(
7428 Symbol_table* symtab,
efc6fa12 7429 Relobj* src_obj,
e81fea4d 7430 unsigned int src_shndx,
efc6fa12 7431 Relobj* dst_obj,
e81fea4d
AM
7432 unsigned int dst_shndx,
7433 Address dst_off) const
7434{
6c77229c
AM
7435 if (size != 64 || dst_obj->is_dynamic())
7436 return;
7437
e81fea4d
AM
7438 Powerpc_relobj<size, big_endian>* ppc_object
7439 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 7440 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
7441 {
7442 if (ppc_object->opd_valid())
7443 {
7444 dst_shndx = ppc_object->get_opd_ent(dst_off);
7445 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7446 }
7447 else
7448 {
7449 // If we haven't run scan_opd_relocs, we must delay
7450 // processing this function descriptor reference.
7451 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7452 }
7453 }
7454}
7455
7456// Add any special sections for this symbol to the gc work list.
7457// For powerpc64, this adds the code section of a function
7458// descriptor.
7459
7460template<int size, bool big_endian>
7461void
7462Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7463 Symbol_table* symtab,
7464 Symbol* sym) const
7465{
7466 if (size == 64)
7467 {
7468 Powerpc_relobj<size, big_endian>* ppc_object
7469 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7470 bool is_ordinary;
7471 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 7472 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
7473 {
7474 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7475 Address dst_off = gsym->value();
c6de8ed4
AM
7476 if (ppc_object->opd_valid())
7477 {
7478 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
7479 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7480 dst_indx));
c6de8ed4
AM
7481 }
7482 else
7483 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
7484 }
7485 }
7486}
7487
dc3714f3
AM
7488// For a symbol location in .opd, set LOC to the location of the
7489// function entry.
7490
7491template<int size, bool big_endian>
7492void
7493Target_powerpc<size, big_endian>::do_function_location(
7494 Symbol_location* loc) const
7495{
a2d7bf59 7496 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
7497 {
7498 if (loc->object->is_dynamic())
7499 {
7500 Powerpc_dynobj<size, big_endian>* ppc_object
7501 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7502 if (loc->shndx == ppc_object->opd_shndx())
7503 {
7504 Address dest_off;
7505 Address off = loc->offset - ppc_object->opd_address();
7506 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7507 loc->offset = dest_off;
7508 }
7509 }
7510 else
7511 {
7512 const Powerpc_relobj<size, big_endian>* ppc_object
7513 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7514 if (loc->shndx == ppc_object->opd_shndx())
7515 {
7516 Address dest_off;
7517 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7518 loc->offset = dest_off;
7519 }
7520 }
7521 }
7522}
7523
bbec1a5d
AM
7524// FNOFFSET in section SHNDX in OBJECT is the start of a function
7525// compiled with -fsplit-stack. The function calls non-split-stack
7526// code. Change the function to ensure it has enough stack space to
7527// call some random function.
7528
7529template<int size, bool big_endian>
7530void
7531Target_powerpc<size, big_endian>::do_calls_non_split(
7532 Relobj* object,
7533 unsigned int shndx,
7534 section_offset_type fnoffset,
7535 section_size_type fnsize,
6e0813d3
CC
7536 const unsigned char* prelocs,
7537 size_t reloc_count,
bbec1a5d
AM
7538 unsigned char* view,
7539 section_size_type view_size,
7540 std::string* from,
7541 std::string* to) const
7542{
7543 // 32-bit not supported.
7544 if (size == 32)
7545 {
7546 // warn
7547 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
7548 prelocs, reloc_count, view, view_size,
7549 from, to);
bbec1a5d
AM
7550 return;
7551 }
7552
7553 // The function always starts with
7554 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7555 // addis %r12,%r1,-allocate@ha
7556 // addi %r12,%r12,-allocate@l
7557 // cmpld %r12,%r0
7558 // but note that the addis or addi may be replaced with a nop
7559
7560 unsigned char *entry = view + fnoffset;
7561 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7562
7563 if ((insn & 0xffff0000) == addis_2_12)
7564 {
7565 /* Skip ELFv2 global entry code. */
7566 entry += 8;
7567 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7568 }
7569
7570 unsigned char *pinsn = entry;
7571 bool ok = false;
7572 const uint32_t ld_private_ss = 0xe80d8fc0;
7573 if (insn == ld_private_ss)
7574 {
7575 int32_t allocate = 0;
7576 while (1)
7577 {
7578 pinsn += 4;
7579 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7580 if ((insn & 0xffff0000) == addis_12_1)
7581 allocate += (insn & 0xffff) << 16;
7582 else if ((insn & 0xffff0000) == addi_12_1
7583 || (insn & 0xffff0000) == addi_12_12)
7584 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7585 else if (insn != nop)
7586 break;
7587 }
7588 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7589 {
7590 int extra = parameters->options().split_stack_adjust_size();
7591 allocate -= extra;
7592 if (allocate >= 0 || extra < 0)
7593 {
7594 object->error(_("split-stack stack size overflow at "
7595 "section %u offset %0zx"),
7596 shndx, static_cast<size_t>(fnoffset));
7597 return;
7598 }
7599 pinsn = entry + 4;
7600 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7601 if (insn != addis_12_1)
7602 {
7603 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7604 pinsn += 4;
7605 insn = addi_12_12 | (allocate & 0xffff);
7606 if (insn != addi_12_12)
7607 {
7608 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7609 pinsn += 4;
7610 }
7611 }
7612 else
7613 {
7614 insn = addi_12_1 | (allocate & 0xffff);
7615 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7616 pinsn += 4;
7617 }
7618 if (pinsn != entry + 12)
7619 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7620
7621 ok = true;
7622 }
7623 }
7624
7625 if (!ok)
7626 {
7627 if (!object->has_no_split_stack())
7628 object->error(_("failed to match split-stack sequence at "
7629 "section %u offset %0zx"),
7630 shndx, static_cast<size_t>(fnoffset));
7631 }
7632}
7633
42cacb20
DE
7634// Scan relocations for a section.
7635
7636template<int size, bool big_endian>
7637void
7638Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
7639 Symbol_table* symtab,
7640 Layout* layout,
7641 Sized_relobj_file<size, big_endian>* object,
7642 unsigned int data_shndx,
7643 unsigned int sh_type,
7644 const unsigned char* prelocs,
7645 size_t reloc_count,
7646 Output_section* output_section,
7647 bool needs_special_offset_handling,
7648 size_t local_symbol_count,
7649 const unsigned char* plocal_symbols)
42cacb20
DE
7650{
7651 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
7652 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7653 Classify_reloc;
42cacb20 7654
7ee7ff70
AM
7655 if (!this->plt_localentry0_init_)
7656 {
7657 bool plt_localentry0 = false;
7658 if (size == 64
7659 && this->abiversion() >= 2)
7660 {
7661 if (parameters->options().user_set_plt_localentry())
7662 plt_localentry0 = parameters->options().plt_localentry();
7ee7ff70
AM
7663 }
7664 this->plt_localentry0_ = plt_localentry0;
7665 this->plt_localentry0_init_ = true;
7666 }
7667
42cacb20
DE
7668 if (sh_type == elfcpp::SHT_REL)
7669 {
7670 gold_error(_("%s: unsupported REL reloc section"),
7671 object->name().c_str());
7672 return;
7673 }
7674
4d625b70 7675 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
7676 symtab,
7677 layout,
7678 this,
7679 object,
7680 data_shndx,
7681 prelocs,
7682 reloc_count,
7683 output_section,
7684 needs_special_offset_handling,
7685 local_symbol_count,
7686 plocal_symbols);
7687}
7688
ec4dbad3
AM
7689// Functor class for processing the global symbol table.
7690// Removes symbols defined on discarded opd entries.
7691
7692template<bool big_endian>
7693class Global_symbol_visitor_opd
7694{
7695 public:
7696 Global_symbol_visitor_opd()
7697 { }
7698
7699 void
7700 operator()(Sized_symbol<64>* sym)
7701 {
7702 if (sym->has_symtab_index()
7703 || sym->source() != Symbol::FROM_OBJECT
7704 || !sym->in_real_elf())
7705 return;
7706
6c77229c
AM
7707 if (sym->object()->is_dynamic())
7708 return;
7709
ec4dbad3
AM
7710 Powerpc_relobj<64, big_endian>* symobj
7711 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 7712 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
7713 return;
7714
7715 bool is_ordinary;
7716 unsigned int shndx = sym->shndx(&is_ordinary);
7717 if (shndx == symobj->opd_shndx()
7718 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
7719 {
7720 sym->set_undefined();
e3ee8ed4 7721 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
7722 sym->set_is_defined_in_discarded_section();
7723 sym->set_symtab_index(-1U);
7724 }
ec4dbad3
AM
7725 }
7726};
7727
f3a0ed29
AM
7728template<int size, bool big_endian>
7729void
7730Target_powerpc<size, big_endian>::define_save_restore_funcs(
7731 Layout* layout,
7732 Symbol_table* symtab)
7733{
7734 if (size == 64)
7735 {
d49044c7
AM
7736 Output_data_save_res<size, big_endian>* savres
7737 = new Output_data_save_res<size, big_endian>(symtab);
7738 this->savres_section_ = savres;
f3a0ed29
AM
7739 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7740 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7741 savres, ORDER_TEXT, false);
7742 }
7743}
7744
d8f5a274
AM
7745// Sort linker created .got section first (for the header), then input
7746// sections belonging to files using small model code.
7747
7748template<bool big_endian>
7749class Sort_toc_sections
7750{
7751 public:
7752 bool
7753 operator()(const Output_section::Input_section& is1,
7754 const Output_section::Input_section& is2) const
7755 {
7756 if (!is1.is_input_section() && is2.is_input_section())
7757 return true;
7758 bool small1
7759 = (is1.is_input_section()
7760 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
7761 ->has_small_toc_reloc()));
7762 bool small2
7763 = (is2.is_input_section()
7764 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
7765 ->has_small_toc_reloc()));
7766 return small1 && !small2;
7767 }
7768};
7769
42cacb20
DE
7770// Finalize the sections.
7771
7772template<int size, bool big_endian>
7773void
d5b40221
DK
7774Target_powerpc<size, big_endian>::do_finalize_sections(
7775 Layout* layout,
f59f41f3 7776 const Input_objects*,
ec4dbad3 7777 Symbol_table* symtab)
42cacb20 7778{
c9824451
AM
7779 if (parameters->doing_static_link())
7780 {
7781 // At least some versions of glibc elf-init.o have a strong
7782 // reference to __rela_iplt marker syms. A weak ref would be
7783 // better..
7784 if (this->iplt_ != NULL)
7785 {
7786 Reloc_section* rel = this->iplt_->rel_plt();
7787 symtab->define_in_output_data("__rela_iplt_start", NULL,
7788 Symbol_table::PREDEFINED, rel, 0, 0,
7789 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7790 elfcpp::STV_HIDDEN, 0, false, true);
7791 symtab->define_in_output_data("__rela_iplt_end", NULL,
7792 Symbol_table::PREDEFINED, rel, 0, 0,
7793 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7794 elfcpp::STV_HIDDEN, 0, true, true);
7795 }
7796 else
7797 {
7798 symtab->define_as_constant("__rela_iplt_start", NULL,
7799 Symbol_table::PREDEFINED, 0, 0,
7800 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7801 elfcpp::STV_HIDDEN, 0, true, false);
7802 symtab->define_as_constant("__rela_iplt_end", NULL,
7803 Symbol_table::PREDEFINED, 0, 0,
7804 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7805 elfcpp::STV_HIDDEN, 0, true, false);
7806 }
7807 }
7808
ec4dbad3
AM
7809 if (size == 64)
7810 {
7811 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
7812 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
7813
7814 if (!parameters->options().relocatable())
7815 {
7816 this->define_save_restore_funcs(layout, symtab);
7817
7818 // Annoyingly, we need to make these sections now whether or
7819 // not we need them. If we delay until do_relax then we
7820 // need to mess with the relaxation machinery checkpointing.
7821 this->got_section(symtab, layout);
7822 this->make_brlt_section(layout);
d8f5a274
AM
7823
7824 if (parameters->options().toc_sort())
7825 {
7826 Output_section* os = this->got_->output_section();
7827 if (os != NULL && os->input_sections().size() > 1)
7828 std::stable_sort(os->input_sections().begin(),
7829 os->input_sections().end(),
7830 Sort_toc_sections<big_endian>());
7831 }
ec661b9d 7832 }
ec4dbad3
AM
7833 }
7834
42cacb20 7835 // Fill in some more dynamic tags.
c9269dff 7836 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 7837 if (odyn != NULL)
cf43a2fe 7838 {
c9824451
AM
7839 const Reloc_section* rel_plt = (this->plt_ == NULL
7840 ? NULL
7841 : this->plt_->rel_plt());
7842 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
7843 this->rela_dyn_, true, size == 32);
7844
7845 if (size == 32)
dd93cd0a 7846 {
c9824451
AM
7847 if (this->got_ != NULL)
7848 {
7849 this->got_->finalize_data_size();
7850 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
7851 this->got_, this->got_->g_o_t());
7852 }
dd93cd0a 7853 }
c9824451 7854 else
dd93cd0a 7855 {
c9824451
AM
7856 if (this->glink_ != NULL)
7857 {
7858 this->glink_->finalize_data_size();
7859 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
7860 this->glink_,
ec661b9d 7861 (this->glink_->pltresolve_size
c9824451
AM
7862 - 32));
7863 }
7ee7ff70
AM
7864 if (this->has_localentry0_)
7865 odyn->add_constant(elfcpp::DT_PPC64_OPT,
7866 elfcpp::PPC64_OPT_LOCALENTRY);
dd93cd0a 7867 }
c9269dff 7868 }
cf43a2fe 7869
42cacb20
DE
7870 // Emit any relocs we saved in an attempt to avoid generating COPY
7871 // relocs.
7872 if (this->copy_relocs_.any_saved_relocs())
7873 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7874}
7875
5edad15d
AM
7876// Emit any saved relocs, and mark toc entries using any of these
7877// relocs as not optimizable.
aba6bc71 7878
5edad15d
AM
7879template<int sh_type, int size, bool big_endian>
7880void
7881Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
7882 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 7883{
5edad15d
AM
7884 if (size == 64
7885 && parameters->options().toc_optimize())
7886 {
7887 for (typename Copy_relocs<sh_type, size, big_endian>::
7888 Copy_reloc_entries::iterator p = this->entries_.begin();
7889 p != this->entries_.end();
7890 ++p)
7891 {
7892 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
7893 entry = *p;
7894
7895 // If the symbol is no longer defined in a dynamic object,
7896 // then we emitted a COPY relocation. If it is still
7897 // dynamic then we'll need dynamic relocations and thus
7898 // can't optimize toc entries.
7899 if (entry.sym_->is_from_dynobj())
7900 {
7901 Powerpc_relobj<size, big_endian>* ppc_object
7902 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
7903 if (entry.shndx_ == ppc_object->toc_shndx())
7904 ppc_object->set_no_toc_opt(entry.address_);
7905 }
7906 }
7907 }
7908
7909 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
7910}
7911
3ea0a085
AM
7912// Return the value to use for a branch relocation.
7913
7914template<int size, bool big_endian>
1611bc4a 7915bool
3ea0a085 7916Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 7917 const Symbol_table* symtab,
3ea0a085
AM
7918 const Sized_symbol<size>* gsym,
7919 Powerpc_relobj<size, big_endian>* object,
1611bc4a 7920 Address *value,
3ea0a085
AM
7921 unsigned int *dest_shndx)
7922{
9055360d
AM
7923 if (size == 32 || this->abiversion() >= 2)
7924 gold_unreachable();
3ea0a085 7925 *dest_shndx = 0;
3ea0a085
AM
7926
7927 // If the symbol is defined in an opd section, ie. is a function
7928 // descriptor, use the function descriptor code entry address
7929 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 7930 if (gsym != NULL
0e123f69
AM
7931 && (gsym->source() != Symbol::FROM_OBJECT
7932 || gsym->object()->is_dynamic()))
1611bc4a 7933 return true;
3ea0a085
AM
7934 if (gsym != NULL)
7935 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7936 unsigned int shndx = symobj->opd_shndx();
7937 if (shndx == 0)
1611bc4a 7938 return true;
3ea0a085 7939 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 7940 if (opd_addr == invalid_address)
1611bc4a 7941 return true;
c6905c28 7942 opd_addr += symobj->output_section_address(shndx);
1611bc4a 7943 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
7944 {
7945 Address sec_off;
1611bc4a 7946 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
7947 if (symtab->is_section_folded(symobj, *dest_shndx))
7948 {
7949 Section_id folded
7950 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7951 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7952 *dest_shndx = folded.second;
7953 }
3ea0a085 7954 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
7955 if (sec_addr == invalid_address)
7956 return false;
7957
3ea0a085 7958 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 7959 *value = sec_addr + sec_off;
3ea0a085 7960 }
1611bc4a 7961 return true;
3ea0a085
AM
7962}
7963
42cacb20
DE
7964// Perform a relocation.
7965
7966template<int size, bool big_endian>
7967inline bool
7968Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 7969 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 7970 unsigned int,
d83ce4e3
AM
7971 Target_powerpc* target,
7972 Output_section* os,
7973 size_t relnum,
91a65d2f 7974 const unsigned char* preloc,
d83ce4e3
AM
7975 const Sized_symbol<size>* gsym,
7976 const Symbol_value<size>* psymval,
7977 unsigned char* view,
c9269dff
AM
7978 Address address,
7979 section_size_type view_size)
42cacb20 7980{
0e804863
ILT
7981 if (view == NULL)
7982 return true;
7983
91a65d2f
AM
7984 const elfcpp::Rela<size, big_endian> rela(preloc);
7985 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
e3deeb9c 7986 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
dd93cd0a 7987 {
e3deeb9c
AM
7988 case Track_tls::NOT_EXPECTED:
7989 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7990 _("__tls_get_addr call lacks marker reloc"));
7991 break;
7992 case Track_tls::EXPECTED:
7993 // We have already complained.
7994 break;
7995 case Track_tls::SKIP:
7996 return true;
7997 case Track_tls::NORMAL:
7998 break;
dd93cd0a 7999 }
dd93cd0a 8000
42cacb20 8001 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
dd93cd0a 8002 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
0e123f69 8003 typedef typename elfcpp::Rela<size, big_endian> Reltype;
dcfc7dd4
AM
8004 // Offset from start of insn to d-field reloc.
8005 const int d_offset = big_endian ? 2 : 0;
8006
3ea0a085
AM
8007 Powerpc_relobj<size, big_endian>* const object
8008 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 8009 Address value = 0;
0cfb0717 8010 bool has_stub_value = false;
7ee7ff70 8011 bool localentry0 = false;
e5d5f5ed 8012 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
b3ccdeb5 8013 if ((gsym != NULL
88b8e639 8014 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
b3ccdeb5
AM
8015 : object->local_has_plt_offset(r_sym))
8016 && (!psymval->is_ifunc_symbol()
9055360d 8017 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 8018 {
9055360d
AM
8019 if (size == 64
8020 && gsym != NULL
8021 && target->abiversion() >= 2
8022 && !parameters->options().output_is_position_independent()
8023 && !is_branch_reloc(r_type))
ec661b9d 8024 {
faa2211d
AM
8025 Address off = target->glink_section()->find_global_entry(gsym);
8026 if (off != invalid_address)
6ec65f28
AM
8027 {
8028 value = target->glink_section()->global_entry_address() + off;
8029 has_stub_value = true;
8030 }
ec661b9d 8031 }
c9824451 8032 else
9055360d
AM
8033 {
8034 Stub_table<size, big_endian>* stub_table
8035 = object->stub_table(relinfo->data_shndx);
8036 if (stub_table == NULL)
8037 {
8038 // This is a ref from a data section to an ifunc symbol.
8039 if (target->stub_tables().size() != 0)
8040 stub_table = target->stub_tables()[0];
8041 }
faa2211d
AM
8042 if (stub_table != NULL)
8043 {
7e57d19e 8044 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 8045 if (gsym != NULL)
7e57d19e 8046 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
8047 rela.get_r_addend());
8048 else
7e57d19e 8049 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 8050 rela.get_r_addend());
7e57d19e 8051 if (ent != NULL)
faa2211d 8052 {
7e57d19e
AM
8053 value = stub_table->stub_address() + ent->off_;
8054 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8055 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8056 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8057 if (size == 64
8058 && ent->r2save_
8059 && relnum + 1 < reloc_count)
8060 {
8061 Reltype next_rela(preloc + reloc_size);
8062 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8063 == elfcpp::R_PPC64_TOCSAVE
8064 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8065 value += 4;
8066 }
7ee7ff70 8067 localentry0 = ent->localentry0_;
faa2211d
AM
8068 has_stub_value = true;
8069 }
8070 }
9055360d 8071 }
faa2211d
AM
8072 // We don't care too much about bogus debug references to
8073 // non-local functions, but otherwise there had better be a plt
8074 // call stub or global entry stub as appropriate.
8075 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 8076 }
cf43a2fe
AM
8077
8078 if (r_type == elfcpp::R_POWERPC_GOT16
8079 || r_type == elfcpp::R_POWERPC_GOT16_LO
8080 || r_type == elfcpp::R_POWERPC_GOT16_HI
8081 || r_type == elfcpp::R_POWERPC_GOT16_HA
8082 || r_type == elfcpp::R_PPC64_GOT16_DS
8083 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 8084 {
cf43a2fe
AM
8085 if (gsym != NULL)
8086 {
8087 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8088 value = gsym->got_offset(GOT_TYPE_STANDARD);
8089 }
8090 else
8091 {
cf43a2fe
AM
8092 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8093 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8094 }
dd93cd0a 8095 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
8096 }
8097 else if (r_type == elfcpp::R_PPC64_TOC)
8098 {
c9269dff 8099 value = (target->got_section()->output_section()->address()
dd93cd0a 8100 + object->toc_base_offset());
cf43a2fe
AM
8101 }
8102 else if (gsym != NULL
8103 && (r_type == elfcpp::R_POWERPC_REL24
8104 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 8105 && has_stub_value)
cf43a2fe 8106 {
c9269dff
AM
8107 if (size == 64)
8108 {
8109 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8110 Valtype* wv = reinterpret_cast<Valtype*>(view);
7ee7ff70
AM
8111 bool can_plt_call = localentry0;
8112 if (!localentry0 && rela.get_r_offset() + 8 <= view_size)
c9269dff 8113 {
3ea0a085 8114 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 8115 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
8116 if ((insn & 1) != 0
8117 && (insn2 == nop
8118 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 8119 {
b4f7960d
AM
8120 elfcpp::Swap<32, big_endian>::
8121 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
8122 can_plt_call = true;
8123 }
8124 }
8125 if (!can_plt_call)
3ea0a085
AM
8126 {
8127 // If we don't have a branch and link followed by a nop,
8128 // we can't go via the plt because there is no place to
8129 // put a toc restoring instruction.
8130 // Unless we know we won't be returning.
8131 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8132 can_plt_call = true;
8133 }
8134 if (!can_plt_call)
8135 {
ba8ca3e7
AM
8136 // g++ as of 20130507 emits self-calls without a
8137 // following nop. This is arguably wrong since we have
8138 // conflicting information. On the one hand a global
8139 // symbol and on the other a local call sequence, but
8140 // don't error for this special case.
8141 // It isn't possible to cheaply verify we have exactly
8142 // such a call. Allow all calls to the same section.
3ea0a085 8143 bool ok = false;
c9824451 8144 Address code = value;
3ea0a085
AM
8145 if (gsym->source() == Symbol::FROM_OBJECT
8146 && gsym->object() == object)
8147 {
9055360d
AM
8148 unsigned int dest_shndx = 0;
8149 if (target->abiversion() < 2)
8150 {
8151 Address addend = rela.get_r_addend();
1611bc4a
AM
8152 code = psymval->value(object, addend);
8153 target->symval_for_branch(relinfo->symtab, gsym, object,
8154 &code, &dest_shndx);
9055360d 8155 }
3ea0a085
AM
8156 bool is_ordinary;
8157 if (dest_shndx == 0)
8158 dest_shndx = gsym->shndx(&is_ordinary);
8159 ok = dest_shndx == relinfo->data_shndx;
8160 }
8161 if (!ok)
c9824451
AM
8162 {
8163 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8164 _("call lacks nop, can't restore toc; "
8165 "recompile with -fPIC"));
8166 value = code;
8167 }
3ea0a085 8168 }
c9269dff 8169 }
cf43a2fe 8170 }
dd93cd0a
AM
8171 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8172 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8173 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8174 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8175 {
8176 // First instruction of a global dynamic sequence, arg setup insn.
8177 const bool final = gsym == NULL || gsym->final_value_is_known();
8178 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8179 enum Got_type got_type = GOT_TYPE_STANDARD;
8180 if (tls_type == tls::TLSOPT_NONE)
8181 got_type = GOT_TYPE_TLSGD;
8182 else if (tls_type == tls::TLSOPT_TO_IE)
8183 got_type = GOT_TYPE_TPREL;
8184 if (got_type != GOT_TYPE_STANDARD)
8185 {
8186 if (gsym != NULL)
8187 {
8188 gold_assert(gsym->has_got_offset(got_type));
8189 value = gsym->got_offset(got_type);
8190 }
8191 else
8192 {
dd93cd0a
AM
8193 gold_assert(object->local_has_got_offset(r_sym, got_type));
8194 value = object->local_got_offset(r_sym, got_type);
8195 }
8196 value -= target->got_section()->got_base_offset(object);
8197 }
8198 if (tls_type == tls::TLSOPT_TO_IE)
8199 {
8200 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8201 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8202 {
dcfc7dd4 8203 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8204 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8205 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8206 if (size == 32)
8207 insn |= 32 << 26; // lwz
8208 else
8209 insn |= 58 << 26; // ld
8210 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8211 }
8212 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8213 - elfcpp::R_POWERPC_GOT_TLSGD16);
8214 }
8215 else if (tls_type == tls::TLSOPT_TO_LE)
8216 {
8217 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8218 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8219 {
dcfc7dd4 8220 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
8221 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8222 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 8223 if (size == 32)
0f81d3f0
AM
8224 insn |= addis_0_2;
8225 else
8226 insn |= addis_0_13;
dd93cd0a
AM
8227 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8228 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8229 value = psymval->value(object, rela.get_r_addend());
8230 }
8231 else
8232 {
dcfc7dd4 8233 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8234 Insn insn = nop;
8235 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8236 r_type = elfcpp::R_POWERPC_NONE;
8237 }
8238 }
8239 }
8240 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8241 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8242 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8243 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8244 {
8245 // First instruction of a local dynamic sequence, arg setup insn.
8246 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8247 if (tls_type == tls::TLSOPT_NONE)
8248 {
8249 value = target->tlsld_got_offset();
8250 value -= target->got_section()->got_base_offset(object);
8251 }
8252 else
8253 {
8254 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8255 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8256 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8257 {
dcfc7dd4 8258 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
8259 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8260 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 8261 if (size == 32)
0f81d3f0
AM
8262 insn |= addis_0_2;
8263 else
8264 insn |= addis_0_13;
dd93cd0a
AM
8265 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8266 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 8267 value = dtp_offset;
dd93cd0a
AM
8268 }
8269 else
8270 {
dcfc7dd4 8271 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8272 Insn insn = nop;
8273 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8274 r_type = elfcpp::R_POWERPC_NONE;
8275 }
8276 }
8277 }
8278 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8279 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8280 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8281 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8282 {
8283 // Accesses relative to a local dynamic sequence address,
8284 // no optimisation here.
8285 if (gsym != NULL)
8286 {
8287 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8288 value = gsym->got_offset(GOT_TYPE_DTPREL);
8289 }
8290 else
8291 {
dd93cd0a
AM
8292 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8293 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8294 }
8295 value -= target->got_section()->got_base_offset(object);
8296 }
8297 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8298 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8299 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8300 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8301 {
8302 // First instruction of initial exec sequence.
8303 const bool final = gsym == NULL || gsym->final_value_is_known();
8304 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8305 if (tls_type == tls::TLSOPT_NONE)
8306 {
8307 if (gsym != NULL)
8308 {
8309 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8310 value = gsym->got_offset(GOT_TYPE_TPREL);
8311 }
8312 else
8313 {
dd93cd0a
AM
8314 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8315 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8316 }
8317 value -= target->got_section()->got_base_offset(object);
8318 }
8319 else
8320 {
8321 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8322 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8323 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8324 {
dcfc7dd4 8325 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8326 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8327 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8328 if (size == 32)
8329 insn |= addis_0_2;
8330 else
8331 insn |= addis_0_13;
8332 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8333 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8334 value = psymval->value(object, rela.get_r_addend());
8335 }
8336 else
8337 {
dcfc7dd4 8338 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8339 Insn insn = nop;
8340 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8341 r_type = elfcpp::R_POWERPC_NONE;
8342 }
8343 }
8344 }
8345 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8346 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8347 {
8348 // Second instruction of a global dynamic sequence,
8349 // the __tls_get_addr call
e3deeb9c 8350 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
8351 const bool final = gsym == NULL || gsym->final_value_is_known();
8352 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8353 if (tls_type != tls::TLSOPT_NONE)
8354 {
8355 if (tls_type == tls::TLSOPT_TO_IE)
8356 {
8357 Insn* iview = reinterpret_cast<Insn*>(view);
8358 Insn insn = add_3_3_13;
8359 if (size == 32)
8360 insn = add_3_3_2;
8361 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8362 r_type = elfcpp::R_POWERPC_NONE;
8363 }
8364 else
8365 {
8366 Insn* iview = reinterpret_cast<Insn*>(view);
8367 Insn insn = addi_3_3;
8368 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8369 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8370 view += d_offset;
dd93cd0a
AM
8371 value = psymval->value(object, rela.get_r_addend());
8372 }
e3deeb9c 8373 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
8374 }
8375 }
8376 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8377 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8378 {
8379 // Second instruction of a local dynamic sequence,
8380 // the __tls_get_addr call
e3deeb9c 8381 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
8382 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8383 if (tls_type == tls::TLSOPT_TO_LE)
8384 {
8385 Insn* iview = reinterpret_cast<Insn*>(view);
8386 Insn insn = addi_3_3;
8387 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
e3deeb9c 8388 this->skip_next_tls_get_addr_call();
dd93cd0a 8389 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8390 view += d_offset;
7404fe1b 8391 value = dtp_offset;
dd93cd0a
AM
8392 }
8393 }
8394 else if (r_type == elfcpp::R_POWERPC_TLS)
8395 {
8396 // Second instruction of an initial exec sequence
8397 const bool final = gsym == NULL || gsym->final_value_is_known();
8398 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8399 if (tls_type == tls::TLSOPT_TO_LE)
8400 {
8401 Insn* iview = reinterpret_cast<Insn*>(view);
8402 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8403 unsigned int reg = size == 32 ? 2 : 13;
8404 insn = at_tls_transform(insn, reg);
8405 gold_assert(insn != 0);
8406 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8407 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8408 view += d_offset;
dd93cd0a
AM
8409 value = psymval->value(object, rela.get_r_addend());
8410 }
8411 }
0cfb0717 8412 else if (!has_stub_value)
cf43a2fe 8413 {
dd93cd0a 8414 Address addend = 0;
cbcb23fa 8415 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
cf43a2fe 8416 addend = rela.get_r_addend();
c9824451 8417 value = psymval->value(object, addend);
dd93cd0a 8418 if (size == 64 && is_branch_reloc(r_type))
9055360d
AM
8419 {
8420 if (target->abiversion() >= 2)
8421 {
8422 if (gsym != NULL)
8423 value += object->ppc64_local_entry_offset(gsym);
8424 else
8425 value += object->ppc64_local_entry_offset(r_sym);
8426 }
8427 else
1611bc4a
AM
8428 {
8429 unsigned int dest_shndx;
8430 target->symval_for_branch(relinfo->symtab, gsym, object,
8431 &value, &dest_shndx);
8432 }
9055360d 8433 }
cbcb23fa 8434 Address max_branch_offset = max_branch_delta(r_type);
ec661b9d
AM
8435 if (max_branch_offset != 0
8436 && value - address + max_branch_offset >= 2 * max_branch_offset)
8437 {
8438 Stub_table<size, big_endian>* stub_table
8439 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
8440 if (stub_table != NULL)
8441 {
8442 Address off = stub_table->find_long_branch_entry(object, value);
8443 if (off != invalid_address)
0cfb0717
AM
8444 {
8445 value = (stub_table->stub_address() + stub_table->plt_size()
8446 + off);
8447 has_stub_value = true;
8448 }
0cfdc767 8449 }
ec661b9d 8450 }
42cacb20
DE
8451 }
8452
42cacb20
DE
8453 switch (r_type)
8454 {
dd93cd0a
AM
8455 case elfcpp::R_PPC64_REL64:
8456 case elfcpp::R_POWERPC_REL32:
8457 case elfcpp::R_POWERPC_REL24:
8458 case elfcpp::R_PPC_PLTREL24:
8459 case elfcpp::R_PPC_LOCAL24PC:
8460 case elfcpp::R_POWERPC_REL16:
8461 case elfcpp::R_POWERPC_REL16_LO:
8462 case elfcpp::R_POWERPC_REL16_HI:
8463 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8464 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a
AM
8465 case elfcpp::R_POWERPC_REL14:
8466 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8467 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8468 value -= address;
8469 break;
8470
42cacb20
DE
8471 case elfcpp::R_PPC64_TOC16:
8472 case elfcpp::R_PPC64_TOC16_LO:
8473 case elfcpp::R_PPC64_TOC16_HI:
8474 case elfcpp::R_PPC64_TOC16_HA:
8475 case elfcpp::R_PPC64_TOC16_DS:
8476 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 8477 // Subtract the TOC base address.
c9269dff 8478 value -= (target->got_section()->output_section()->address()
dd93cd0a 8479 + object->toc_base_offset());
42cacb20
DE
8480 break;
8481
cf43a2fe
AM
8482 case elfcpp::R_POWERPC_SECTOFF:
8483 case elfcpp::R_POWERPC_SECTOFF_LO:
8484 case elfcpp::R_POWERPC_SECTOFF_HI:
8485 case elfcpp::R_POWERPC_SECTOFF_HA:
8486 case elfcpp::R_PPC64_SECTOFF_DS:
8487 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8488 if (os != NULL)
8489 value -= os->address();
42cacb20
DE
8490 break;
8491
dd93cd0a
AM
8492 case elfcpp::R_PPC64_TPREL16_DS:
8493 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
8494 case elfcpp::R_PPC64_TPREL16_HIGH:
8495 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8496 if (size != 64)
f9c6b907 8497 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 8498 break;
d8e90251 8499 // Fall through.
dd93cd0a
AM
8500 case elfcpp::R_POWERPC_TPREL16:
8501 case elfcpp::R_POWERPC_TPREL16_LO:
8502 case elfcpp::R_POWERPC_TPREL16_HI:
8503 case elfcpp::R_POWERPC_TPREL16_HA:
8504 case elfcpp::R_POWERPC_TPREL:
8505 case elfcpp::R_PPC64_TPREL16_HIGHER:
8506 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8507 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8508 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8509 // tls symbol values are relative to tls_segment()->vaddr()
8510 value -= tp_offset;
8511 break;
8512
8513 case elfcpp::R_PPC64_DTPREL16_DS:
8514 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8515 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8516 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8517 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8518 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8519 if (size != 64)
8520 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8521 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8522 break;
d8e90251 8523 // Fall through.
dd93cd0a
AM
8524 case elfcpp::R_POWERPC_DTPREL16:
8525 case elfcpp::R_POWERPC_DTPREL16_LO:
8526 case elfcpp::R_POWERPC_DTPREL16_HI:
8527 case elfcpp::R_POWERPC_DTPREL16_HA:
8528 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
8529 case elfcpp::R_PPC64_DTPREL16_HIGH:
8530 case elfcpp::R_PPC64_DTPREL16_HIGHA:
dd93cd0a
AM
8531 // tls symbol values are relative to tls_segment()->vaddr()
8532 value -= dtp_offset;
8533 break;
8534
45965137
AM
8535 case elfcpp::R_PPC64_ADDR64_LOCAL:
8536 if (gsym != NULL)
8537 value += object->ppc64_local_entry_offset(gsym);
8538 else
8539 value += object->ppc64_local_entry_offset(r_sym);
8540 break;
8541
42cacb20
DE
8542 default:
8543 break;
8544 }
8545
dd93cd0a 8546 Insn branch_bit = 0;
42cacb20
DE
8547 switch (r_type)
8548 {
dd93cd0a
AM
8549 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8550 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8551 branch_bit = 1 << 21;
d8e90251 8552 // Fall through.
dd93cd0a
AM
8553 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8554 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8555 {
8556 Insn* iview = reinterpret_cast<Insn*>(view);
8557 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8558 insn &= ~(1 << 21);
8559 insn |= branch_bit;
8560 if (this->is_isa_v2)
8561 {
8562 // Set 'a' bit. This is 0b00010 in BO field for branch
8563 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8564 // for branch on CTR insns (BO == 1a00t or 1a01t).
8565 if ((insn & (0x14 << 21)) == (0x04 << 21))
8566 insn |= 0x02 << 21;
8567 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8568 insn |= 0x08 << 21;
8569 else
8570 break;
8571 }
8572 else
8573 {
8574 // Invert 'y' bit if not the default.
8575 if (static_cast<Signed_address>(value) < 0)
8576 insn ^= 1 << 21;
8577 }
8578 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8579 }
8580 break;
8581
8582 default:
8583 break;
8584 }
8585
aba6bc71
AM
8586 if (size == 64)
8587 {
aba6bc71
AM
8588 switch (r_type)
8589 {
8590 default:
8591 break;
8592
5edad15d
AM
8593 // Multi-instruction sequences that access the GOT/TOC can
8594 // be optimized, eg.
8595 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8596 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8597 // and
8598 // addis ra,r2,0; addi rb,ra,x@toc@l;
8599 // to nop; addi rb,r2,x@toc;
8600 // FIXME: the @got sequence shown above is not yet
8601 // optimized. Note that gcc as of 2017-01-07 doesn't use
8602 // the ELF @got relocs except for TLS, instead using the
8603 // PowerOpen variant of a compiler managed GOT (called TOC).
8604 // The PowerOpen TOC sequence equivalent to the first
8605 // example is optimized.
aba6bc71
AM
8606 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8607 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8608 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8609 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8610 case elfcpp::R_POWERPC_GOT16_HA:
8611 case elfcpp::R_PPC64_TOC16_HA:
d8f5a274 8612 if (parameters->options().toc_optimize())
aba6bc71 8613 {
dcfc7dd4 8614 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8615 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8616 if (r_type == elfcpp::R_PPC64_TOC16_HA
8617 && object->make_toc_relative(target, &value))
8618 {
8619 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8620 == ((15u << 26) | (2 << 16)));
8621 }
8622 if (((insn & ((0x3f << 26) | 0x1f << 16))
8623 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8624 && value + 0x8000 < 0x10000)
aba6bc71
AM
8625 {
8626 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8627 return true;
8628 }
8629 }
8630 break;
8631
8632 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8633 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8634 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8635 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8636 case elfcpp::R_POWERPC_GOT16_LO:
8637 case elfcpp::R_PPC64_GOT16_LO_DS:
8638 case elfcpp::R_PPC64_TOC16_LO:
8639 case elfcpp::R_PPC64_TOC16_LO_DS:
d8f5a274 8640 if (parameters->options().toc_optimize())
aba6bc71 8641 {
dcfc7dd4 8642 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8643 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8644 bool changed = false;
8645 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8646 && object->make_toc_relative(target, &value))
8647 {
8648 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8649 insn ^= (14u << 26) ^ (58u << 26);
8650 r_type = elfcpp::R_PPC64_TOC16_LO;
8651 changed = true;
8652 }
8653 if (ok_lo_toc_insn(insn, r_type)
8654 && value + 0x8000 < 0x10000)
aba6bc71
AM
8655 {
8656 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8657 {
8658 // Transform addic to addi when we change reg.
8659 insn &= ~((0x3f << 26) | (0x1f << 16));
8660 insn |= (14u << 26) | (2 << 16);
8661 }
8662 else
8663 {
8664 insn &= ~(0x1f << 16);
8665 insn |= 2 << 16;
8666 }
5edad15d 8667 changed = true;
aba6bc71 8668 }
5edad15d
AM
8669 if (changed)
8670 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
8671 }
8672 break;
549dba71
AM
8673
8674 case elfcpp::R_PPC64_ENTRY:
8675 value = (target->got_section()->output_section()->address()
8676 + object->toc_base_offset());
8677 if (value + 0x80008000 <= 0xffffffff
8678 && !parameters->options().output_is_position_independent())
8679 {
8680 Insn* iview = reinterpret_cast<Insn*>(view);
8681 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8682 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8683
8684 if ((insn1 & ~0xfffc) == ld_2_12
8685 && insn2 == add_2_2_12)
8686 {
8687 insn1 = lis_2 + ha(value);
8688 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8689 insn2 = addi_2_2 + l(value);
8690 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8691 return true;
8692 }
8693 }
8694 else
8695 {
8696 value -= address;
8697 if (value + 0x80008000 <= 0xffffffff)
8698 {
8699 Insn* iview = reinterpret_cast<Insn*>(view);
8700 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8701 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8702
8703 if ((insn1 & ~0xfffc) == ld_2_12
8704 && insn2 == add_2_2_12)
8705 {
8706 insn1 = addis_2_12 + ha(value);
8707 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8708 insn2 = addi_2_2 + l(value);
8709 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8710 return true;
8711 }
8712 }
8713 }
8714 break;
e3a7574e
AM
8715
8716 case elfcpp::R_POWERPC_REL16_LO:
8717 // If we are generating a non-PIC executable, edit
8718 // 0: addis 2,12,.TOC.-0b@ha
8719 // addi 2,2,.TOC.-0b@l
8720 // used by ELFv2 global entry points to set up r2, to
8721 // lis 2,.TOC.@ha
8722 // addi 2,2,.TOC.@l
8723 // if .TOC. is in range. */
8724 if (value + address - 4 + 0x80008000 <= 0xffffffff
8725 && relnum != 0
8726 && preloc != NULL
8727 && target->abiversion() >= 2
8728 && !parameters->options().output_is_position_independent()
4f038ee5 8729 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
8730 && gsym != NULL
8731 && strcmp(gsym->name(), ".TOC.") == 0)
8732 {
0e123f69 8733 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
8734 Reltype prev_rela(preloc - reloc_size);
8735 if ((prev_rela.get_r_info()
8736 == elfcpp::elf_r_info<size>(r_sym,
8737 elfcpp::R_POWERPC_REL16_HA))
8738 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
8739 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
8740 {
dcfc7dd4 8741 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
8742 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
8743 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
8744
8745 if ((insn1 & 0xffff0000) == addis_2_12
8746 && (insn2 & 0xffff0000) == addi_2_2)
8747 {
8748 insn1 = lis_2 + ha(value + address - 4);
8749 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
8750 insn2 = addi_2_2 + l(value + address - 4);
8751 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
8752 if (relinfo->rr)
8753 {
8754 relinfo->rr->set_strategy(relnum - 1,
8755 Relocatable_relocs::RELOC_SPECIAL);
8756 relinfo->rr->set_strategy(relnum,
8757 Relocatable_relocs::RELOC_SPECIAL);
8758 }
8759 return true;
8760 }
8761 }
8762 }
8763 break;
aba6bc71
AM
8764 }
8765 }
8766
f4baf0d4 8767 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 8768 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
8769 switch (r_type)
8770 {
8771 case elfcpp::R_POWERPC_ADDR32:
8772 case elfcpp::R_POWERPC_UADDR32:
8773 if (size == 64)
f4baf0d4 8774 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
8775 break;
8776
8777 case elfcpp::R_POWERPC_REL32:
a680de9a 8778 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 8779 if (size == 64)
f4baf0d4 8780 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
8781 break;
8782
dd93cd0a 8783 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 8784 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
8785 break;
8786
b80eed39
AM
8787 case elfcpp::R_POWERPC_ADDR16:
8788 // We really should have three separate relocations,
8789 // one for 16-bit data, one for insns with 16-bit signed fields,
8790 // and one for insns with 16-bit unsigned fields.
8791 overflow = Reloc::CHECK_BITFIELD;
8792 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
8793 overflow = Reloc::CHECK_LOW_INSN;
8794 break;
8795
f9c6b907
AM
8796 case elfcpp::R_POWERPC_ADDR16_HI:
8797 case elfcpp::R_POWERPC_ADDR16_HA:
8798 case elfcpp::R_POWERPC_GOT16_HI:
8799 case elfcpp::R_POWERPC_GOT16_HA:
8800 case elfcpp::R_POWERPC_PLT16_HI:
8801 case elfcpp::R_POWERPC_PLT16_HA:
8802 case elfcpp::R_POWERPC_SECTOFF_HI:
8803 case elfcpp::R_POWERPC_SECTOFF_HA:
8804 case elfcpp::R_PPC64_TOC16_HI:
8805 case elfcpp::R_PPC64_TOC16_HA:
8806 case elfcpp::R_PPC64_PLTGOT16_HI:
8807 case elfcpp::R_PPC64_PLTGOT16_HA:
8808 case elfcpp::R_POWERPC_TPREL16_HI:
8809 case elfcpp::R_POWERPC_TPREL16_HA:
8810 case elfcpp::R_POWERPC_DTPREL16_HI:
8811 case elfcpp::R_POWERPC_DTPREL16_HA:
8812 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8813 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8814 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8815 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8816 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8817 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8818 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8819 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8820 case elfcpp::R_POWERPC_REL16_HI:
8821 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
8822 if (size != 32)
8823 overflow = Reloc::CHECK_HIGH_INSN;
8824 break;
8825
dd93cd0a
AM
8826 case elfcpp::R_POWERPC_REL16:
8827 case elfcpp::R_PPC64_TOC16:
8828 case elfcpp::R_POWERPC_GOT16:
8829 case elfcpp::R_POWERPC_SECTOFF:
8830 case elfcpp::R_POWERPC_TPREL16:
8831 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
8832 case elfcpp::R_POWERPC_GOT_TLSGD16:
8833 case elfcpp::R_POWERPC_GOT_TLSLD16:
8834 case elfcpp::R_POWERPC_GOT_TPREL16:
8835 case elfcpp::R_POWERPC_GOT_DTPREL16:
8836 overflow = Reloc::CHECK_LOW_INSN;
8837 break;
8838
8839 case elfcpp::R_POWERPC_ADDR24:
8840 case elfcpp::R_POWERPC_ADDR14:
8841 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8842 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8843 case elfcpp::R_PPC64_ADDR16_DS:
8844 case elfcpp::R_POWERPC_REL24:
8845 case elfcpp::R_PPC_PLTREL24:
8846 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
8847 case elfcpp::R_PPC64_TPREL16_DS:
8848 case elfcpp::R_PPC64_DTPREL16_DS:
8849 case elfcpp::R_PPC64_TOC16_DS:
8850 case elfcpp::R_PPC64_GOT16_DS:
8851 case elfcpp::R_PPC64_SECTOFF_DS:
8852 case elfcpp::R_POWERPC_REL14:
8853 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8854 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
f4baf0d4 8855 overflow = Reloc::CHECK_SIGNED;
42cacb20 8856 break;
dd93cd0a 8857 }
42cacb20 8858
dcfc7dd4 8859 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
8860 Insn insn = 0;
8861
b80eed39
AM
8862 if (overflow == Reloc::CHECK_LOW_INSN
8863 || overflow == Reloc::CHECK_HIGH_INSN)
8864 {
a680de9a 8865 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 8866
a47622ac
AM
8867 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
8868 overflow = Reloc::CHECK_BITFIELD;
8869 else if (overflow == Reloc::CHECK_LOW_INSN
8870 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
8871 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
8872 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
8873 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
8874 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
8875 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 8876 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
8877 else
8878 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
8879 }
8880
a680de9a 8881 bool maybe_dq_reloc = false;
3ea0a085 8882 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 8883 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
8884 switch (r_type)
8885 {
8886 case elfcpp::R_POWERPC_NONE:
8887 case elfcpp::R_POWERPC_TLS:
8888 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8889 case elfcpp::R_POWERPC_GNU_VTENTRY:
42cacb20
DE
8890 break;
8891
8892 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 8893 case elfcpp::R_PPC64_REL64:
cf43a2fe 8894 case elfcpp::R_PPC64_TOC:
45965137 8895 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
8896 Reloc::addr64(view, value);
8897 break;
8898
8899 case elfcpp::R_POWERPC_TPREL:
8900 case elfcpp::R_POWERPC_DTPREL:
8901 if (size == 64)
8902 Reloc::addr64(view, value);
8903 else
3ea0a085 8904 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
8905 break;
8906
8907 case elfcpp::R_PPC64_UADDR64:
8908 Reloc::addr64_u(view, value);
42cacb20
DE
8909 break;
8910
8911 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 8912 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
8913 break;
8914
acc276d8 8915 case elfcpp::R_POWERPC_REL32:
dd93cd0a 8916 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 8917 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
8918 break;
8919
8920 case elfcpp::R_POWERPC_ADDR24:
8921 case elfcpp::R_POWERPC_REL24:
8922 case elfcpp::R_PPC_PLTREL24:
8923 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 8924 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
8925 break;
8926
dd93cd0a
AM
8927 case elfcpp::R_POWERPC_GOT_DTPREL16:
8928 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
8929 case elfcpp::R_POWERPC_GOT_TPREL16:
8930 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
8931 if (size == 64)
8932 {
ec86f434 8933 // On ppc64 these are all ds form
a680de9a 8934 maybe_dq_reloc = true;
dd93cd0a
AM
8935 break;
8936 }
c25aa1e1 8937 // Fall through.
cf43a2fe 8938 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 8939 case elfcpp::R_POWERPC_REL16:
cf43a2fe 8940 case elfcpp::R_PPC64_TOC16:
42cacb20 8941 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 8942 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
8943 case elfcpp::R_POWERPC_TPREL16:
8944 case elfcpp::R_POWERPC_DTPREL16:
8945 case elfcpp::R_POWERPC_GOT_TLSGD16:
8946 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 8947 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 8948 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 8949 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 8950 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe 8951 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
8952 case elfcpp::R_POWERPC_TPREL16_LO:
8953 case elfcpp::R_POWERPC_DTPREL16_LO:
8954 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8955 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
8956 if (size == 64)
8957 status = Reloc::addr16(view, value, overflow);
8958 else
8959 maybe_dq_reloc = true;
dd93cd0a
AM
8960 break;
8961
8962 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 8963 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
8964 break;
8965
f9c6b907
AM
8966 case elfcpp::R_PPC64_ADDR16_HIGH:
8967 case elfcpp::R_PPC64_TPREL16_HIGH:
8968 case elfcpp::R_PPC64_DTPREL16_HIGH:
8969 if (size == 32)
8970 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8971 goto unsupp;
d8e90251 8972 // Fall through.
cf43a2fe 8973 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 8974 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 8975 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 8976 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe 8977 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
8978 case elfcpp::R_POWERPC_TPREL16_HI:
8979 case elfcpp::R_POWERPC_DTPREL16_HI:
8980 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8981 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8982 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8983 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8984 Reloc::addr16_hi(view, value);
42cacb20
DE
8985 break;
8986
f9c6b907
AM
8987 case elfcpp::R_PPC64_ADDR16_HIGHA:
8988 case elfcpp::R_PPC64_TPREL16_HIGHA:
8989 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8990 if (size == 32)
8991 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8992 goto unsupp;
d8e90251 8993 // Fall through.
cf43a2fe 8994 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 8995 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 8996 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 8997 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe 8998 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
8999 case elfcpp::R_POWERPC_TPREL16_HA:
9000 case elfcpp::R_POWERPC_DTPREL16_HA:
9001 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9002 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9003 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9004 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9005 Reloc::addr16_ha(view, value);
42cacb20
DE
9006 break;
9007
a680de9a
PB
9008 case elfcpp::R_POWERPC_REL16DX_HA:
9009 status = Reloc::addr16dx_ha(view, value, overflow);
9010 break;
9011
dd93cd0a
AM
9012 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9013 if (size == 32)
9014 // R_PPC_EMB_NADDR16_LO
9015 goto unsupp;
d8e90251 9016 // Fall through.
dd93cd0a
AM
9017 case elfcpp::R_PPC64_ADDR16_HIGHER:
9018 case elfcpp::R_PPC64_TPREL16_HIGHER:
9019 Reloc::addr16_hi2(view, value);
42cacb20
DE
9020 break;
9021
dd93cd0a
AM
9022 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9023 if (size == 32)
9024 // R_PPC_EMB_NADDR16_HI
9025 goto unsupp;
d8e90251 9026 // Fall through.
dd93cd0a
AM
9027 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9028 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9029 Reloc::addr16_ha2(view, value);
42cacb20
DE
9030 break;
9031
dd93cd0a
AM
9032 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9033 if (size == 32)
9034 // R_PPC_EMB_NADDR16_HA
9035 goto unsupp;
d8e90251 9036 // Fall through.
dd93cd0a
AM
9037 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9038 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9039 Reloc::addr16_hi3(view, value);
42cacb20
DE
9040 break;
9041
dd93cd0a
AM
9042 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9043 if (size == 32)
9044 // R_PPC_EMB_SDAI16
9045 goto unsupp;
d8e90251 9046 // Fall through.
dd93cd0a
AM
9047 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9048 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9049 Reloc::addr16_ha3(view, value);
9050 break;
9051
9052 case elfcpp::R_PPC64_DTPREL16_DS:
9053 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9054 if (size == 32)
9055 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9056 goto unsupp;
d8e90251 9057 // Fall through.
dd93cd0a
AM
9058 case elfcpp::R_PPC64_TPREL16_DS:
9059 case elfcpp::R_PPC64_TPREL16_LO_DS:
9060 if (size == 32)
9061 // R_PPC_TLSGD, R_PPC_TLSLD
9062 break;
d8e90251 9063 // Fall through.
cf43a2fe
AM
9064 case elfcpp::R_PPC64_ADDR16_DS:
9065 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
9066 case elfcpp::R_PPC64_TOC16_DS:
9067 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
9068 case elfcpp::R_PPC64_GOT16_DS:
9069 case elfcpp::R_PPC64_GOT16_LO_DS:
9070 case elfcpp::R_PPC64_SECTOFF_DS:
9071 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 9072 maybe_dq_reloc = true;
dd93cd0a
AM
9073 break;
9074
9075 case elfcpp::R_POWERPC_ADDR14:
9076 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9077 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9078 case elfcpp::R_POWERPC_REL14:
9079 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9080 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 9081 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
9082 break;
9083
9084 case elfcpp::R_POWERPC_COPY:
9085 case elfcpp::R_POWERPC_GLOB_DAT:
9086 case elfcpp::R_POWERPC_JMP_SLOT:
9087 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 9088 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
9089 case elfcpp::R_PPC64_JMP_IREL:
9090 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
9091 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9092 _("unexpected reloc %u in object file"),
9093 r_type);
9094 break;
9095
7e57d19e 9096 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 9097 if (size == 32)
7e57d19e 9098 // R_PPC_EMB_SDA21
dd93cd0a
AM
9099 goto unsupp;
9100 else
9101 {
7e57d19e
AM
9102 Symbol_location loc;
9103 loc.object = relinfo->object;
9104 loc.shndx = relinfo->data_shndx;
9105 loc.offset = rela.get_r_offset();
9106 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9107 if (p != target->tocsave_loc().end())
9108 {
9109 // If we've generated plt calls using this tocsave, then
9110 // the nop needs to be changed to save r2.
9111 Insn* iview = reinterpret_cast<Insn*>(view);
9112 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9113 elfcpp::Swap<32, big_endian>::
9114 writeval(iview, std_2_1 + target->stk_toc());
9115 }
dd93cd0a
AM
9116 }
9117 break;
9118
9119 case elfcpp::R_PPC_EMB_SDA2I16:
9120 case elfcpp::R_PPC_EMB_SDA2REL:
9121 if (size == 32)
9122 goto unsupp;
9123 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
9124 break;
9125
dd93cd0a
AM
9126 case elfcpp::R_POWERPC_PLT32:
9127 case elfcpp::R_POWERPC_PLTREL32:
9128 case elfcpp::R_POWERPC_PLT16_LO:
9129 case elfcpp::R_POWERPC_PLT16_HI:
9130 case elfcpp::R_POWERPC_PLT16_HA:
9131 case elfcpp::R_PPC_SDAREL16:
9132 case elfcpp::R_POWERPC_ADDR30:
9133 case elfcpp::R_PPC64_PLT64:
9134 case elfcpp::R_PPC64_PLTREL64:
9135 case elfcpp::R_PPC64_PLTGOT16:
9136 case elfcpp::R_PPC64_PLTGOT16_LO:
9137 case elfcpp::R_PPC64_PLTGOT16_HI:
9138 case elfcpp::R_PPC64_PLTGOT16_HA:
9139 case elfcpp::R_PPC64_PLT16_LO_DS:
9140 case elfcpp::R_PPC64_PLTGOT16_DS:
9141 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a
AM
9142 case elfcpp::R_PPC_EMB_RELSDA:
9143 case elfcpp::R_PPC_TOC16:
42cacb20 9144 default:
dd93cd0a 9145 unsupp:
42cacb20
DE
9146 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9147 _("unsupported reloc %u"),
9148 r_type);
9149 break;
9150 }
a680de9a
PB
9151
9152 if (maybe_dq_reloc)
9153 {
9154 if (insn == 0)
9155 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9156
9157 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9158 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9159 && (insn & 3) == 1))
9160 status = Reloc::addr16_dq(view, value, overflow);
9161 else if (size == 64
9162 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9163 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9164 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9165 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9166 status = Reloc::addr16_ds(view, value, overflow);
9167 else
9168 status = Reloc::addr16(view, value, overflow);
9169 }
9170
0cfb0717 9171 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
9172 && (has_stub_value
9173 || !(gsym != NULL
282c9750 9174 && gsym->is_undefined()
3ffaac20 9175 && is_branch_reloc(r_type))))
0cfb0717
AM
9176 {
9177 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9178 _("relocation overflow"));
9179 if (has_stub_value)
9180 gold_info(_("try relinking with a smaller --stub-group-size"));
9181 }
42cacb20
DE
9182
9183 return true;
9184}
9185
42cacb20
DE
9186// Relocate section data.
9187
9188template<int size, bool big_endian>
9189void
9190Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
9191 const Relocate_info<size, big_endian>* relinfo,
9192 unsigned int sh_type,
9193 const unsigned char* prelocs,
9194 size_t reloc_count,
9195 Output_section* output_section,
9196 bool needs_special_offset_handling,
9197 unsigned char* view,
c9269dff 9198 Address address,
d83ce4e3
AM
9199 section_size_type view_size,
9200 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
9201{
9202 typedef Target_powerpc<size, big_endian> Powerpc;
9203 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
9204 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9205 Powerpc_comdat_behavior;
4d625b70
CC
9206 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9207 Classify_reloc;
42cacb20
DE
9208
9209 gold_assert(sh_type == elfcpp::SHT_RELA);
9210
4d625b70
CC
9211 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9212 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
9213 relinfo,
9214 this,
9215 prelocs,
9216 reloc_count,
9217 output_section,
9218 needs_special_offset_handling,
9219 view,
9220 address,
364c7fa5
ILT
9221 view_size,
9222 reloc_symbol_changes);
42cacb20
DE
9223}
9224
4d625b70 9225template<int size, bool big_endian>
cf43a2fe 9226class Powerpc_scan_relocatable_reloc
42cacb20 9227{
cf43a2fe 9228public:
0e123f69
AM
9229 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9230 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
9231 static const int sh_type = elfcpp::SHT_RELA;
9232
9233 // Return the symbol referred to by the relocation.
9234 static inline unsigned int
9235 get_r_sym(const Reltype* reloc)
9236 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9237
9238 // Return the type of the relocation.
9239 static inline unsigned int
9240 get_r_type(const Reltype* reloc)
9241 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9242
cf43a2fe
AM
9243 // Return the strategy to use for a local symbol which is not a
9244 // section symbol, given the relocation type.
9245 inline Relocatable_relocs::Reloc_strategy
9246 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9247 {
9248 if (r_type == 0 && r_sym == 0)
9249 return Relocatable_relocs::RELOC_DISCARD;
9250 return Relocatable_relocs::RELOC_COPY;
9251 }
9252
9253 // Return the strategy to use for a local symbol which is a section
9254 // symbol, given the relocation type.
9255 inline Relocatable_relocs::Reloc_strategy
9256 local_section_strategy(unsigned int, Relobj*)
9257 {
9258 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9259 }
9260
9261 // Return the strategy to use for a global symbol, given the
9262 // relocation type, the object, and the symbol index.
9263 inline Relocatable_relocs::Reloc_strategy
9264 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9265 {
9266 if (r_type == elfcpp::R_PPC_PLTREL24)
9267 return Relocatable_relocs::RELOC_SPECIAL;
9268 return Relocatable_relocs::RELOC_COPY;
9269 }
9270};
42cacb20
DE
9271
9272// Scan the relocs during a relocatable link.
9273
9274template<int size, bool big_endian>
9275void
9276Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
9277 Symbol_table* symtab,
9278 Layout* layout,
9279 Sized_relobj_file<size, big_endian>* object,
9280 unsigned int data_shndx,
9281 unsigned int sh_type,
9282 const unsigned char* prelocs,
9283 size_t reloc_count,
9284 Output_section* output_section,
9285 bool needs_special_offset_handling,
9286 size_t local_symbol_count,
9287 const unsigned char* plocal_symbols,
9288 Relocatable_relocs* rr)
42cacb20 9289{
4d625b70
CC
9290 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9291
42cacb20
DE
9292 gold_assert(sh_type == elfcpp::SHT_RELA);
9293
4d625b70 9294 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
9295 symtab,
9296 layout,
9297 object,
9298 data_shndx,
9299 prelocs,
9300 reloc_count,
9301 output_section,
9302 needs_special_offset_handling,
9303 local_symbol_count,
9304 plocal_symbols,
9305 rr);
9306}
9307
4d625b70
CC
9308// Scan the relocs for --emit-relocs.
9309
9310template<int size, bool big_endian>
9311void
9312Target_powerpc<size, big_endian>::emit_relocs_scan(
9313 Symbol_table* symtab,
9314 Layout* layout,
9315 Sized_relobj_file<size, big_endian>* object,
9316 unsigned int data_shndx,
9317 unsigned int sh_type,
9318 const unsigned char* prelocs,
9319 size_t reloc_count,
9320 Output_section* output_section,
9321 bool needs_special_offset_handling,
9322 size_t local_symbol_count,
9323 const unsigned char* plocal_syms,
9324 Relocatable_relocs* rr)
9325{
9326 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9327 Classify_reloc;
9328 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9329 Emit_relocs_strategy;
9330
9331 gold_assert(sh_type == elfcpp::SHT_RELA);
9332
9333 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9334 symtab,
9335 layout,
9336 object,
9337 data_shndx,
9338 prelocs,
9339 reloc_count,
9340 output_section,
9341 needs_special_offset_handling,
9342 local_symbol_count,
9343 plocal_syms,
9344 rr);
9345}
9346
7404fe1b 9347// Emit relocations for a section.
dd93cd0a
AM
9348// This is a modified version of the function by the same name in
9349// target-reloc.h. Using relocate_special_relocatable for
9350// R_PPC_PLTREL24 would require duplication of the entire body of the
9351// loop, so we may as well duplicate the whole thing.
42cacb20
DE
9352
9353template<int size, bool big_endian>
9354void
7404fe1b 9355Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
9356 const Relocate_info<size, big_endian>* relinfo,
9357 unsigned int sh_type,
9358 const unsigned char* prelocs,
9359 size_t reloc_count,
9360 Output_section* output_section,
62fe925a 9361 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 9362 unsigned char*,
dd93cd0a 9363 Address view_address,
cf43a2fe 9364 section_size_type,
42cacb20
DE
9365 unsigned char* reloc_view,
9366 section_size_type reloc_view_size)
9367{
9368 gold_assert(sh_type == elfcpp::SHT_RELA);
9369
0e123f69
AM
9370 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9371 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9372 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
9373 // Offset from start of insn to d-field reloc.
9374 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
9375
9376 Powerpc_relobj<size, big_endian>* const object
9377 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9378 const unsigned int local_count = object->local_symbol_count();
9379 unsigned int got2_shndx = object->got2_shndx();
c9269dff 9380 Address got2_addend = 0;
cf43a2fe 9381 if (got2_shndx != 0)
c9269dff
AM
9382 {
9383 got2_addend = object->get_output_section_offset(got2_shndx);
9384 gold_assert(got2_addend != invalid_address);
9385 }
cf43a2fe
AM
9386
9387 unsigned char* pwrite = reloc_view;
7404fe1b 9388 bool zap_next = false;
cf43a2fe
AM
9389 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9390 {
91a65d2f 9391 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
9392 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9393 continue;
9394
9395 Reltype reloc(prelocs);
9396 Reltype_write reloc_write(pwrite);
9397
7404fe1b 9398 Address offset = reloc.get_r_offset();
cf43a2fe 9399 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
9400 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9401 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9402 const unsigned int orig_r_sym = r_sym;
9403 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9404 = reloc.get_r_addend();
9405 const Symbol* gsym = NULL;
9406
9407 if (zap_next)
9408 {
9409 // We could arrange to discard these and other relocs for
9410 // tls optimised sequences in the strategy methods, but for
9411 // now do as BFD ld does.
9412 r_type = elfcpp::R_POWERPC_NONE;
9413 zap_next = false;
9414 }
cf43a2fe
AM
9415
9416 // Get the new symbol index.
9215b98b 9417 Output_section* os = NULL;
cf43a2fe
AM
9418 if (r_sym < local_count)
9419 {
9420 switch (strategy)
9421 {
9422 case Relocatable_relocs::RELOC_COPY:
9423 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 9424 if (r_sym != 0)
dd93cd0a 9425 {
7404fe1b
AM
9426 r_sym = object->symtab_index(r_sym);
9427 gold_assert(r_sym != -1U);
dd93cd0a 9428 }
cf43a2fe
AM
9429 break;
9430
9431 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9432 {
9433 // We are adjusting a section symbol. We need to find
9434 // the symbol table index of the section symbol for
9435 // the output section corresponding to input section
9436 // in which this symbol is defined.
9437 gold_assert(r_sym < local_count);
9438 bool is_ordinary;
9439 unsigned int shndx =
9440 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9441 gold_assert(is_ordinary);
9215b98b 9442 os = object->output_section(shndx);
cf43a2fe
AM
9443 gold_assert(os != NULL);
9444 gold_assert(os->needs_symtab_index());
7404fe1b 9445 r_sym = os->symtab_index();
cf43a2fe
AM
9446 }
9447 break;
9448
9449 default:
9450 gold_unreachable();
9451 }
9452 }
9453 else
9454 {
7404fe1b 9455 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
9456 gold_assert(gsym != NULL);
9457 if (gsym->is_forwarder())
9458 gsym = relinfo->symtab->resolve_forwards(gsym);
9459
9460 gold_assert(gsym->has_symtab_index());
7404fe1b 9461 r_sym = gsym->symtab_index();
cf43a2fe
AM
9462 }
9463
9464 // Get the new offset--the location in the output section where
9465 // this relocation should be applied.
cf43a2fe 9466 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9467 offset += offset_in_output_section;
cf43a2fe
AM
9468 else
9469 {
c9269dff
AM
9470 section_offset_type sot_offset =
9471 convert_types<section_offset_type, Address>(offset);
cf43a2fe 9472 section_offset_type new_sot_offset =
c9269dff
AM
9473 output_section->output_offset(object, relinfo->data_shndx,
9474 sot_offset);
cf43a2fe 9475 gold_assert(new_sot_offset != -1);
7404fe1b 9476 offset = new_sot_offset;
cf43a2fe
AM
9477 }
9478
dd93cd0a
AM
9479 // In an object file, r_offset is an offset within the section.
9480 // In an executable or dynamic object, generated by
9481 // --emit-relocs, r_offset is an absolute address.
7404fe1b 9482 if (!parameters->options().relocatable())
dd93cd0a 9483 {
7404fe1b 9484 offset += view_address;
dd93cd0a 9485 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9486 offset -= offset_in_output_section;
dd93cd0a
AM
9487 }
9488
cf43a2fe 9489 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
9490 if (strategy == Relocatable_relocs::RELOC_COPY)
9491 ;
9492 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9493 {
7404fe1b 9494 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9215b98b
AM
9495 gold_assert(os != NULL);
9496 addend = psymval->value(object, addend) - os->address();
cf43a2fe
AM
9497 }
9498 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9499 {
e3a7574e
AM
9500 if (size == 32)
9501 {
9502 if (addend >= 32768)
9503 addend += got2_addend;
9504 }
9505 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9506 {
9507 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 9508 addend -= d_offset;
e3a7574e
AM
9509 }
9510 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9511 {
9512 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 9513 addend -= d_offset + 4;
e3a7574e 9514 }
cf43a2fe
AM
9515 }
9516 else
9517 gold_unreachable();
9518
7404fe1b
AM
9519 if (!parameters->options().relocatable())
9520 {
9521 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9522 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9523 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9524 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9525 {
9526 // First instruction of a global dynamic sequence,
9527 // arg setup insn.
9528 const bool final = gsym == NULL || gsym->final_value_is_known();
9529 switch (this->optimize_tls_gd(final))
9530 {
9531 case tls::TLSOPT_TO_IE:
9532 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9533 - elfcpp::R_POWERPC_GOT_TLSGD16);
9534 break;
9535 case tls::TLSOPT_TO_LE:
9536 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9537 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9538 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9539 else
9540 {
9541 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9542 offset -= d_offset;
7404fe1b
AM
9543 }
9544 break;
9545 default:
9546 break;
9547 }
9548 }
9549 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9550 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9551 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9552 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9553 {
9554 // First instruction of a local dynamic sequence,
9555 // arg setup insn.
9556 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9557 {
9558 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9559 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9560 {
9561 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9562 const Output_section* os = relinfo->layout->tls_segment()
9563 ->first_section();
9564 gold_assert(os != NULL);
9565 gold_assert(os->needs_symtab_index());
9566 r_sym = os->symtab_index();
9567 addend = dtp_offset;
9568 }
9569 else
9570 {
9571 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9572 offset -= d_offset;
7404fe1b
AM
9573 }
9574 }
9575 }
9576 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9577 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9578 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9579 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9580 {
9581 // First instruction of initial exec sequence.
9582 const bool final = gsym == NULL || gsym->final_value_is_known();
9583 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9584 {
9585 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9586 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9587 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9588 else
9589 {
9590 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9591 offset -= d_offset;
7404fe1b
AM
9592 }
9593 }
9594 }
9595 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9596 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9597 {
9598 // Second instruction of a global dynamic sequence,
9599 // the __tls_get_addr call
9600 const bool final = gsym == NULL || gsym->final_value_is_known();
9601 switch (this->optimize_tls_gd(final))
9602 {
9603 case tls::TLSOPT_TO_IE:
9604 r_type = elfcpp::R_POWERPC_NONE;
9605 zap_next = true;
9606 break;
9607 case tls::TLSOPT_TO_LE:
9608 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9609 offset += d_offset;
7404fe1b
AM
9610 zap_next = true;
9611 break;
9612 default:
9613 break;
9614 }
9615 }
9616 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9617 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9618 {
9619 // Second instruction of a local dynamic sequence,
9620 // the __tls_get_addr call
9621 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9622 {
9623 const Output_section* os = relinfo->layout->tls_segment()
9624 ->first_section();
9625 gold_assert(os != NULL);
9626 gold_assert(os->needs_symtab_index());
9627 r_sym = os->symtab_index();
9628 addend = dtp_offset;
9629 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9630 offset += d_offset;
7404fe1b
AM
9631 zap_next = true;
9632 }
9633 }
9634 else if (r_type == elfcpp::R_POWERPC_TLS)
9635 {
9636 // Second instruction of an initial exec sequence
9637 const bool final = gsym == NULL || gsym->final_value_is_known();
9638 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9639 {
9640 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9641 offset += d_offset;
7404fe1b
AM
9642 }
9643 }
9644 }
9645
9646 reloc_write.put_r_offset(offset);
9647 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9648 reloc_write.put_r_addend(addend);
cf43a2fe
AM
9649
9650 pwrite += reloc_size;
9651 }
9652
9653 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9654 == reloc_view_size);
42cacb20
DE
9655}
9656
ec661b9d 9657// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
9658// treatment. This is how we support equality comparisons of function
9659// pointers across shared library boundaries, as described in the
9660// processor specific ABI supplement.
9661
9662template<int size, bool big_endian>
9663uint64_t
9664Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9665{
cf43a2fe
AM
9666 if (size == 32)
9667 {
9668 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
9669 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9670 p != this->stub_tables_.end();
9671 ++p)
9672 {
7e57d19e
AM
9673 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9674 = (*p)->find_plt_call_entry(gsym);
9675 if (ent != NULL)
9676 return (*p)->stub_address() + ent->off_;
ec661b9d 9677 }
c9824451 9678 }
9055360d
AM
9679 else if (this->abiversion() >= 2)
9680 {
faa2211d
AM
9681 Address off = this->glink_section()->find_global_entry(gsym);
9682 if (off != invalid_address)
9055360d
AM
9683 return this->glink_section()->global_entry_address() + off;
9684 }
ec661b9d 9685 gold_unreachable();
c9824451
AM
9686}
9687
9688// Return the PLT address to use for a local symbol.
9689template<int size, bool big_endian>
9690uint64_t
9691Target_powerpc<size, big_endian>::do_plt_address_for_local(
9692 const Relobj* object,
9693 unsigned int symndx) const
9694{
9695 if (size == 32)
9696 {
9697 const Sized_relobj<size, big_endian>* relobj
9698 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
9699 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9700 p != this->stub_tables_.end();
9701 ++p)
9702 {
7e57d19e
AM
9703 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9704 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
9705 if (ent != NULL)
9706 return (*p)->stub_address() + ent->off_;
ec661b9d 9707 }
c9824451 9708 }
ec661b9d 9709 gold_unreachable();
c9824451
AM
9710}
9711
9712// Return the PLT address to use for a global symbol.
9713template<int size, bool big_endian>
9714uint64_t
9715Target_powerpc<size, big_endian>::do_plt_address_for_global(
9716 const Symbol* gsym) const
9717{
9718 if (size == 32)
9719 {
ec661b9d
AM
9720 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9721 p != this->stub_tables_.end();
9722 ++p)
9723 {
7e57d19e
AM
9724 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9725 = (*p)->find_plt_call_entry(gsym);
9726 if (ent != NULL)
9727 return (*p)->stub_address() + ent->off_;
ec661b9d 9728 }
cf43a2fe 9729 }
9055360d
AM
9730 else if (this->abiversion() >= 2)
9731 {
faa2211d
AM
9732 Address off = this->glink_section()->find_global_entry(gsym);
9733 if (off != invalid_address)
9055360d
AM
9734 return this->glink_section()->global_entry_address() + off;
9735 }
ec661b9d 9736 gold_unreachable();
42cacb20
DE
9737}
9738
bd73a62d
AM
9739// Return the offset to use for the GOT_INDX'th got entry which is
9740// for a local tls symbol specified by OBJECT, SYMNDX.
9741template<int size, bool big_endian>
9742int64_t
9743Target_powerpc<size, big_endian>::do_tls_offset_for_local(
9744 const Relobj* object,
9745 unsigned int symndx,
9746 unsigned int got_indx) const
9747{
9748 const Powerpc_relobj<size, big_endian>* ppc_object
9749 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
9750 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
9751 {
9752 for (Got_type got_type = GOT_TYPE_TLSGD;
9753 got_type <= GOT_TYPE_TPREL;
9754 got_type = Got_type(got_type + 1))
9755 if (ppc_object->local_has_got_offset(symndx, got_type))
9756 {
9757 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
9758 if (got_type == GOT_TYPE_TLSGD)
9759 off += size / 8;
9760 if (off == got_indx * (size / 8))
9761 {
9762 if (got_type == GOT_TYPE_TPREL)
9763 return -tp_offset;
9764 else
9765 return -dtp_offset;
9766 }
9767 }
9768 }
9769 gold_unreachable();
9770}
9771
9772// Return the offset to use for the GOT_INDX'th got entry which is
9773// for global tls symbol GSYM.
9774template<int size, bool big_endian>
9775int64_t
9776Target_powerpc<size, big_endian>::do_tls_offset_for_global(
9777 Symbol* gsym,
9778 unsigned int got_indx) const
9779{
9780 if (gsym->type() == elfcpp::STT_TLS)
9781 {
9782 for (Got_type got_type = GOT_TYPE_TLSGD;
9783 got_type <= GOT_TYPE_TPREL;
9784 got_type = Got_type(got_type + 1))
9785 if (gsym->has_got_offset(got_type))
9786 {
9787 unsigned int off = gsym->got_offset(got_type);
9788 if (got_type == GOT_TYPE_TLSGD)
9789 off += size / 8;
9790 if (off == got_indx * (size / 8))
9791 {
9792 if (got_type == GOT_TYPE_TPREL)
9793 return -tp_offset;
9794 else
9795 return -dtp_offset;
9796 }
9797 }
9798 }
9799 gold_unreachable();
9800}
9801
42cacb20
DE
9802// The selector for powerpc object files.
9803
9804template<int size, bool big_endian>
9805class Target_selector_powerpc : public Target_selector
9806{
9807public:
9808 Target_selector_powerpc()
edc27beb
AM
9809 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
9810 size, big_endian,
03ef7571
ILT
9811 (size == 64
9812 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
9813 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
9814 (size == 64
9815 ? (big_endian ? "elf64ppc" : "elf64lppc")
9816 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
9817 { }
9818
2e702c99
RM
9819 virtual Target*
9820 do_instantiate_target()
7f055c20 9821 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
9822};
9823
9824Target_selector_powerpc<32, true> target_selector_ppc32;
9825Target_selector_powerpc<32, false> target_selector_ppc32le;
9826Target_selector_powerpc<64, true> target_selector_ppc64;
9827Target_selector_powerpc<64, false> target_selector_ppc64le;
9828
decdd3bc
AM
9829// Instantiate these constants for -O0
9830template<int size, bool big_endian>
9831const int Output_data_glink<size, big_endian>::pltresolve_size;
9832template<int size, bool big_endian>
9055360d
AM
9833const typename Output_data_glink<size, big_endian>::Address
9834 Output_data_glink<size, big_endian>::invalid_address;
9835template<int size, bool big_endian>
decdd3bc
AM
9836const typename Stub_table<size, big_endian>::Address
9837 Stub_table<size, big_endian>::invalid_address;
9838template<int size, bool big_endian>
9839const typename Target_powerpc<size, big_endian>::Address
9840 Target_powerpc<size, big_endian>::invalid_address;
9841
42cacb20 9842} // End anonymous namespace.
This page took 0.959062 seconds and 4 git commands to generate.