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