[GOLD] PowerPC tls_get_addr_optimize
[deliverable/binutils-gdb.git] / gold / powerpc.cc
CommitLineData
42cacb20
DE
1// powerpc.cc -- powerpc target support for gold.
2
2571583a 3// Copyright (C) 2008-2017 Free Software Foundation, Inc.
42cacb20
DE
4// Written by David S. Miller <davem@davemloft.net>
5// and David Edelsohn <edelsohn@gnu.org>
6
7// This file is part of gold.
8
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 3 of the License, or
12// (at your option) any later version.
13
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22// MA 02110-1301, USA.
23
24#include "gold.h"
25
dc3714f3 26#include <set>
ec661b9d 27#include <algorithm>
42cacb20 28#include "elfcpp.h"
9d5781f8 29#include "dwarf.h"
42cacb20
DE
30#include "parameters.h"
31#include "reloc.h"
32#include "powerpc.h"
33#include "object.h"
34#include "symtab.h"
35#include "layout.h"
36#include "output.h"
37#include "copy-relocs.h"
38#include "target.h"
39#include "target-reloc.h"
40#include "target-select.h"
41#include "tls.h"
42#include "errors.h"
f345227a 43#include "gc.h"
42cacb20
DE
44
45namespace
46{
47
48using namespace gold;
49
50template<int size, bool big_endian>
51class Output_data_plt_powerpc;
52
ec661b9d
AM
53template<int size, bool big_endian>
54class Output_data_brlt_powerpc;
55
cf43a2fe
AM
56template<int size, bool big_endian>
57class Output_data_got_powerpc;
58
59template<int size, bool big_endian>
60class Output_data_glink;
61
ec661b9d
AM
62template<int size, bool big_endian>
63class Stub_table;
64
d49044c7
AM
65template<int size, bool big_endian>
66class Output_data_save_res;
67
a3e60ddb
AM
68template<int size, bool big_endian>
69class Target_powerpc;
70
71struct Stub_table_owner
72{
dc60b26d
AM
73 Stub_table_owner()
74 : output_section(NULL), owner(NULL)
75 { }
76
a3e60ddb
AM
77 Output_section* output_section;
78 const Output_section::Input_section* owner;
79};
80
4d9aa155
AM
81inline bool
82is_branch_reloc(unsigned int r_type);
83
590b87ff
AM
84// Counter incremented on every Powerpc_relobj constructed.
85static uint32_t object_id = 0;
86
cf43a2fe
AM
87template<int size, bool big_endian>
88class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
89{
90public:
dd93cd0a 91 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
e81fea4d
AM
92 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
93 typedef Unordered_map<Address, Section_refs> Access_from;
c9269dff 94
cf43a2fe
AM
95 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
96 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
97 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
590b87ff
AM
98 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
99 has_small_toc_reloc_(false), opd_valid_(false),
100 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
101 access_from_map_(), has14_(), stub_table_index_(), st_other_()
b4f7960d
AM
102 {
103 this->set_abiversion(0);
104 }
cf43a2fe
AM
105
106 ~Powerpc_relobj()
107 { }
108
b4f7960d
AM
109 // Read the symbols then set up st_other vector.
110 void
111 do_read_symbols(Read_symbols_data*);
112
5edad15d
AM
113 // Arrange to always relocate .toc first.
114 virtual void
115 do_relocate_sections(
116 const Symbol_table* symtab, const Layout* layout,
117 const unsigned char* pshdrs, Output_file* of,
118 typename Sized_relobj_file<size, big_endian>::Views* pviews);
119
120 // The .toc section index.
121 unsigned int
122 toc_shndx() const
123 {
124 return this->toc_;
125 }
126
127 // Mark .toc entry at OFF as not optimizable.
128 void
129 set_no_toc_opt(Address off)
130 {
131 if (this->no_toc_opt_.empty())
132 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
133 / (size / 8));
134 off /= size / 8;
135 if (off < this->no_toc_opt_.size())
136 this->no_toc_opt_[off] = true;
137 }
138
139 // Mark the entire .toc as not optimizable.
140 void
141 set_no_toc_opt()
142 {
143 this->no_toc_opt_.resize(1);
144 this->no_toc_opt_[0] = true;
145 }
146
147 // Return true if code using the .toc entry at OFF should not be edited.
148 bool
149 no_toc_opt(Address off) const
150 {
151 if (this->no_toc_opt_.empty())
152 return false;
153 off /= size / 8;
154 if (off >= this->no_toc_opt_.size())
155 return true;
156 return this->no_toc_opt_[off];
157 }
158
c9269dff 159 // The .got2 section shndx.
cf43a2fe
AM
160 unsigned int
161 got2_shndx() const
162 {
163 if (size == 32)
c9269dff 164 return this->special_;
cf43a2fe
AM
165 else
166 return 0;
167 }
168
c9269dff
AM
169 // The .opd section shndx.
170 unsigned int
171 opd_shndx() const
172 {
173 if (size == 32)
174 return 0;
175 else
176 return this->special_;
177 }
178
179 // Init OPD entry arrays.
180 void
181 init_opd(size_t opd_size)
182 {
183 size_t count = this->opd_ent_ndx(opd_size);
bfdfa4cd 184 this->opd_ent_.resize(count);
c9269dff
AM
185 }
186
187 // Return section and offset of function entry for .opd + R_OFF.
e81fea4d
AM
188 unsigned int
189 get_opd_ent(Address r_off, Address* value = NULL) const
c9269dff
AM
190 {
191 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
192 gold_assert(ndx < this->opd_ent_.size());
193 gold_assert(this->opd_ent_[ndx].shndx != 0);
e81fea4d 194 if (value != NULL)
bfdfa4cd
AM
195 *value = this->opd_ent_[ndx].off;
196 return this->opd_ent_[ndx].shndx;
c9269dff
AM
197 }
198
199 // Set section and offset of function entry for .opd + R_OFF.
200 void
dd93cd0a 201 set_opd_ent(Address r_off, unsigned int shndx, Address value)
c9269dff
AM
202 {
203 size_t ndx = this->opd_ent_ndx(r_off);
bfdfa4cd
AM
204 gold_assert(ndx < this->opd_ent_.size());
205 this->opd_ent_[ndx].shndx = shndx;
206 this->opd_ent_[ndx].off = value;
207 }
208
209 // Return discard flag for .opd + R_OFF.
210 bool
211 get_opd_discard(Address r_off) const
212 {
213 size_t ndx = this->opd_ent_ndx(r_off);
214 gold_assert(ndx < this->opd_ent_.size());
215 return this->opd_ent_[ndx].discard;
216 }
217
218 // Set discard flag for .opd + R_OFF.
219 void
220 set_opd_discard(Address r_off)
221 {
222 size_t ndx = this->opd_ent_ndx(r_off);
223 gold_assert(ndx < this->opd_ent_.size());
224 this->opd_ent_[ndx].discard = true;
c9269dff
AM
225 }
226
e81fea4d
AM
227 bool
228 opd_valid() const
229 { return this->opd_valid_; }
230
231 void
232 set_opd_valid()
233 { this->opd_valid_ = true; }
234
c9269dff
AM
235 // Examine .rela.opd to build info about function entry points.
236 void
237 scan_opd_relocs(size_t reloc_count,
238 const unsigned char* prelocs,
239 const unsigned char* plocal_syms);
240
5edad15d
AM
241 // Returns true if a code sequence loading a TOC entry can be
242 // converted into code calculating a TOC pointer relative offset.
243 bool
244 make_toc_relative(Target_powerpc<size, big_endian>* target,
245 Address* value);
246
26a4e9cb
AM
247 // Perform the Sized_relobj_file method, then set up opd info from
248 // .opd relocs.
c9269dff
AM
249 void
250 do_read_relocs(Read_relocs_data*);
251
cf43a2fe
AM
252 bool
253 do_find_special_sections(Read_symbols_data* sd);
254
ec4dbad3
AM
255 // Adjust this local symbol value. Return false if the symbol
256 // should be discarded from the output file.
257 bool
258 do_adjust_local_symbol(Symbol_value<size>* lv) const
259 {
260 if (size == 64 && this->opd_shndx() != 0)
261 {
262 bool is_ordinary;
263 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
264 return true;
265 if (this->get_opd_discard(lv->input_value()))
266 return false;
267 }
268 return true;
269 }
270
6c77229c
AM
271 Access_from*
272 access_from_map()
273 { return &this->access_from_map_; }
274
275 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
276 // section at DST_OFF.
277 void
efc6fa12 278 add_reference(Relobj* src_obj,
6c77229c
AM
279 unsigned int src_indx,
280 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
281 {
282 Section_id src_id(src_obj, src_indx);
283 this->access_from_map_[dst_off].insert(src_id);
284 }
285
286 // Add a reference to the code section specified by the .opd entry
287 // at DST_OFF
288 void
289 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
290 {
291 size_t ndx = this->opd_ent_ndx(dst_off);
292 if (ndx >= this->opd_ent_.size())
293 this->opd_ent_.resize(ndx + 1);
294 this->opd_ent_[ndx].gc_mark = true;
295 }
296
297 void
298 process_gc_mark(Symbol_table* symtab)
299 {
300 for (size_t i = 0; i < this->opd_ent_.size(); i++)
301 if (this->opd_ent_[i].gc_mark)
302 {
303 unsigned int shndx = this->opd_ent_[i].shndx;
4277535c 304 symtab->gc()->worklist().push_back(Section_id(this, shndx));
6c77229c
AM
305 }
306 }
307
dd93cd0a
AM
308 // Return offset in output GOT section that this object will use
309 // as a TOC pointer. Won't be just a constant with multi-toc support.
310 Address
311 toc_base_offset() const
312 { return 0x8000; }
313
d8f5a274
AM
314 void
315 set_has_small_toc_reloc()
316 { has_small_toc_reloc_ = true; }
317
318 bool
319 has_small_toc_reloc() const
320 { return has_small_toc_reloc_; }
321
ec661b9d
AM
322 void
323 set_has_14bit_branch(unsigned int shndx)
324 {
325 if (shndx >= this->has14_.size())
326 this->has14_.resize(shndx + 1);
327 this->has14_[shndx] = true;
328 }
329
330 bool
331 has_14bit_branch(unsigned int shndx) const
332 { return shndx < this->has14_.size() && this->has14_[shndx]; }
333
334 void
a3e60ddb 335 set_stub_table(unsigned int shndx, unsigned int stub_index)
ec661b9d 336 {
a3e60ddb 337 if (shndx >= this->stub_table_index_.size())
dc60b26d 338 this->stub_table_index_.resize(shndx + 1, -1);
a3e60ddb 339 this->stub_table_index_[shndx] = stub_index;
ec661b9d
AM
340 }
341
342 Stub_table<size, big_endian>*
343 stub_table(unsigned int shndx)
344 {
a3e60ddb
AM
345 if (shndx < this->stub_table_index_.size())
346 {
347 Target_powerpc<size, big_endian>* target
348 = static_cast<Target_powerpc<size, big_endian>*>(
349 parameters->sized_target<size, big_endian>());
350 unsigned int indx = this->stub_table_index_[shndx];
980d0cdd
AM
351 if (indx < target->stub_tables().size())
352 return target->stub_tables()[indx];
a3e60ddb 353 }
ec661b9d
AM
354 return NULL;
355 }
356
a3e60ddb
AM
357 void
358 clear_stub_table()
359 {
360 this->stub_table_index_.clear();
361 }
362
590b87ff
AM
363 uint32_t
364 uniq() const
365 { return this->uniq_; }
366
b4f7960d
AM
367 int
368 abiversion() const
369 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
370
371 // Set ABI version for input and output
372 void
373 set_abiversion(int ver);
374
7ee7ff70
AM
375 unsigned int
376 st_other (unsigned int symndx) const
377 {
378 return this->st_other_[symndx];
379 }
380
b4f7960d
AM
381 unsigned int
382 ppc64_local_entry_offset(const Symbol* sym) const
383 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
384
385 unsigned int
386 ppc64_local_entry_offset(unsigned int symndx) const
387 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
388
cf43a2fe 389private:
bfdfa4cd
AM
390 struct Opd_ent
391 {
392 unsigned int shndx;
c6de8ed4
AM
393 bool discard : 1;
394 bool gc_mark : 1;
26a4e9cb 395 Address off;
bfdfa4cd
AM
396 };
397
398 // Return index into opd_ent_ array for .opd entry at OFF.
399 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
400 // apart when the language doesn't use the last 8-byte word, the
401 // environment pointer. Thus dividing the entry section offset by
402 // 16 will give an index into opd_ent_ that works for either layout
403 // of .opd. (It leaves some elements of the vector unused when .opd
404 // entries are spaced 24 bytes apart, but we don't know the spacing
405 // until relocations are processed, and in any case it is possible
406 // for an object to have some entries spaced 16 bytes apart and
407 // others 24 bytes apart.)
c9269dff
AM
408 size_t
409 opd_ent_ndx(size_t off) const
410 { return off >> 4;}
411
590b87ff
AM
412 // Per object unique identifier
413 uint32_t uniq_;
414
c9269dff
AM
415 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
416 unsigned int special_;
bfdfa4cd 417
5edad15d
AM
418 // For 64-bit the .rela.toc and .toc section shdnx.
419 unsigned int relatoc_;
420 unsigned int toc_;
421
d8f5a274
AM
422 // For 64-bit, whether this object uses small model relocs to access
423 // the toc.
424 bool has_small_toc_reloc_;
425
bfdfa4cd
AM
426 // Set at the start of gc_process_relocs, when we know opd_ent_
427 // vector is valid. The flag could be made atomic and set in
428 // do_read_relocs with memory_order_release and then tested with
429 // memory_order_acquire, potentially resulting in fewer entries in
430 // access_from_map_.
431 bool opd_valid_;
432
590b87ff
AM
433 // Header e_flags
434 elfcpp::Elf_Word e_flags_;
435
436 // For 64-bit, an array with one entry per 64-bit word in the .toc
437 // section, set if accesses using that word cannot be optimised.
438 std::vector<bool> no_toc_opt_;
439
c9269dff
AM
440 // The first 8-byte word of an OPD entry gives the address of the
441 // entry point of the function. Relocatable object files have a
bfdfa4cd 442 // relocation on this word. The following vector records the
c9269dff 443 // section and offset specified by these relocations.
bfdfa4cd
AM
444 std::vector<Opd_ent> opd_ent_;
445
e81fea4d 446 // References made to this object's .opd section when running
bfdfa4cd
AM
447 // gc_process_relocs for another object, before the opd_ent_ vector
448 // is valid for this object.
e81fea4d 449 Access_from access_from_map_;
ec661b9d
AM
450
451 // Whether input section has a 14-bit branch reloc.
452 std::vector<bool> has14_;
453
454 // The stub table to use for a given input section.
a3e60ddb 455 std::vector<unsigned int> stub_table_index_;
b4f7960d 456
b4f7960d
AM
457 // ELF st_other field for local symbols.
458 std::vector<unsigned char> st_other_;
cf43a2fe
AM
459};
460
dc3714f3
AM
461template<int size, bool big_endian>
462class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
463{
464public:
465 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
466
467 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
468 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
469 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
590b87ff 470 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_()
b4f7960d
AM
471 {
472 this->set_abiversion(0);
473 }
dc3714f3
AM
474
475 ~Powerpc_dynobj()
476 { }
477
478 // Call Sized_dynobj::do_read_symbols to read the symbols then
479 // read .opd from a dynamic object, filling in opd_ent_ vector,
480 void
481 do_read_symbols(Read_symbols_data*);
482
483 // The .opd section shndx.
484 unsigned int
485 opd_shndx() const
486 {
487 return this->opd_shndx_;
488 }
489
490 // The .opd section address.
491 Address
492 opd_address() const
493 {
494 return this->opd_address_;
495 }
496
497 // Init OPD entry arrays.
498 void
499 init_opd(size_t opd_size)
500 {
501 size_t count = this->opd_ent_ndx(opd_size);
502 this->opd_ent_.resize(count);
503 }
504
505 // Return section and offset of function entry for .opd + R_OFF.
506 unsigned int
507 get_opd_ent(Address r_off, Address* value = NULL) const
508 {
509 size_t ndx = this->opd_ent_ndx(r_off);
510 gold_assert(ndx < this->opd_ent_.size());
511 gold_assert(this->opd_ent_[ndx].shndx != 0);
512 if (value != NULL)
513 *value = this->opd_ent_[ndx].off;
514 return this->opd_ent_[ndx].shndx;
515 }
516
517 // Set section and offset of function entry for .opd + R_OFF.
518 void
519 set_opd_ent(Address r_off, unsigned int shndx, Address value)
520 {
521 size_t ndx = this->opd_ent_ndx(r_off);
522 gold_assert(ndx < this->opd_ent_.size());
523 this->opd_ent_[ndx].shndx = shndx;
524 this->opd_ent_[ndx].off = value;
525 }
526
b4f7960d
AM
527 int
528 abiversion() const
529 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
530
531 // Set ABI version for input and output.
532 void
533 set_abiversion(int ver);
534
dc3714f3
AM
535private:
536 // Used to specify extent of executable sections.
537 struct Sec_info
538 {
539 Sec_info(Address start_, Address len_, unsigned int shndx_)
540 : start(start_), len(len_), shndx(shndx_)
541 { }
542
543 bool
544 operator<(const Sec_info& that) const
545 { return this->start < that.start; }
546
547 Address start;
548 Address len;
549 unsigned int shndx;
550 };
551
552 struct Opd_ent
553 {
554 unsigned int shndx;
555 Address off;
556 };
557
558 // Return index into opd_ent_ array for .opd entry at OFF.
559 size_t
560 opd_ent_ndx(size_t off) const
561 { return off >> 4;}
562
563 // For 64-bit the .opd section shndx and address.
564 unsigned int opd_shndx_;
565 Address opd_address_;
566
590b87ff
AM
567 // Header e_flags
568 elfcpp::Elf_Word e_flags_;
569
dc3714f3
AM
570 // The first 8-byte word of an OPD entry gives the address of the
571 // entry point of the function. Records the section and offset
572 // corresponding to the address. Note that in dynamic objects,
573 // offset is *not* relative to the section.
574 std::vector<Opd_ent> opd_ent_;
575};
576
5edad15d
AM
577// Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
578// base class will emit.
579
580template<int sh_type, int size, bool big_endian>
581class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
582{
583 public:
584 Powerpc_copy_relocs()
585 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
586 { }
587
588 // Emit any saved relocations which turn out to be needed. This is
589 // called after all the relocs have been scanned.
590 void
591 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
592};
593
42cacb20
DE
594template<int size, bool big_endian>
595class Target_powerpc : public Sized_target<size, big_endian>
596{
597 public:
d83ce4e3
AM
598 typedef
599 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
c9269dff 600 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
dd93cd0a 601 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
7e57d19e 602 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
c9269dff 603 static const Address invalid_address = static_cast<Address>(0) - 1;
dd93cd0a
AM
604 // Offset of tp and dtp pointers from start of TLS block.
605 static const Address tp_offset = 0x7000;
606 static const Address dtp_offset = 0x8000;
42cacb20
DE
607
608 Target_powerpc()
609 : Sized_target<size, big_endian>(&powerpc_info),
ec661b9d 610 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
5edad15d 611 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
43819297 612 tlsld_got_offset_(-1U),
7e57d19e 613 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
7ee7ff70
AM
614 plt_thread_safe_(false), plt_localentry0_(false),
615 plt_localentry0_init_(false), has_localentry0_(false),
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
DE
1666 false, // has_code_fill
1667 true, // 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
DE
1694 false, // has_code_fill
1695 true, // 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_);
34e0882b
AM
3105 if (target->replace_tls_get_addr(sym))
3106 sym = target->tls_get_addr_opt();
7e57d19e
AM
3107 if (sym != NULL && sym->is_forwarder())
3108 sym = symtab->resolve_forwards(sym);
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 (target->replace_tls_get_addr(sym))
3136 sym = target->tls_get_addr_opt();
3137 if (sym != NULL && sym->is_forwarder())
3138 sym = symtab->resolve_forwards(sym);
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 {
9055360d
AM
3154 if (stub_table == NULL)
3155 stub_table = this->object_->stub_table(this->shndx_);
3156 if (stub_table == NULL)
3157 {
3158 // This is a ref from a data section to an ifunc symbol.
3159 stub_table = ifunc_stub_table;
3160 }
3161 gold_assert(stub_table != NULL);
a3e60ddb
AM
3162 Address from = this->object_->get_output_section_offset(this->shndx_);
3163 if (from != invalid_address)
3164 from += (this->object_->output_section(this->shndx_)->address()
3165 + this->offset_);
9055360d 3166 if (gsym != NULL)
dc60b26d
AM
3167 ok = stub_table->add_plt_call_entry(from,
3168 this->object_, gsym,
7e57d19e
AM
3169 this->r_type_, this->addend_,
3170 this->tocsave_);
9055360d 3171 else
dc60b26d
AM
3172 ok = stub_table->add_plt_call_entry(from,
3173 this->object_, this->r_sym_,
7e57d19e
AM
3174 this->r_type_, this->addend_,
3175 this->tocsave_);
ec661b9d 3176 }
ec661b9d
AM
3177 }
3178 else
3179 {
cbcb23fa 3180 Address max_branch_offset = max_branch_delta(this->r_type_);
a3e60ddb
AM
3181 if (max_branch_offset == 0)
3182 return true;
ec661b9d
AM
3183 Address from = this->object_->get_output_section_offset(this->shndx_);
3184 gold_assert(from != invalid_address);
3185 from += (this->object_->output_section(this->shndx_)->address()
3186 + this->offset_);
3187 Address to;
3188 if (gsym != NULL)
3189 {
3190 switch (gsym->source())
3191 {
3192 case Symbol::FROM_OBJECT:
3193 {
3194 Object* symobj = gsym->object();
3195 if (symobj->is_dynamic()
3196 || symobj->pluginobj() != NULL)
a3e60ddb 3197 return true;
ec661b9d
AM
3198 bool is_ordinary;
3199 unsigned int shndx = gsym->shndx(&is_ordinary);
3200 if (shndx == elfcpp::SHN_UNDEF)
a3e60ddb 3201 return true;
ec661b9d
AM
3202 }
3203 break;
3204
3205 case Symbol::IS_UNDEFINED:
a3e60ddb 3206 return true;
ec661b9d
AM
3207
3208 default:
3209 break;
3210 }
3211 Symbol_table::Compute_final_value_status status;
3212 to = symtab->compute_final_value<size>(gsym, &status);
3213 if (status != Symbol_table::CFVS_OK)
a3e60ddb 3214 return true;
9055360d
AM
3215 if (size == 64)
3216 to += this->object_->ppc64_local_entry_offset(gsym);
ec661b9d
AM
3217 }
3218 else
3219 {
3220 const Symbol_value<size>* psymval
3221 = this->object_->local_symbol(this->r_sym_);
3222 Symbol_value<size> symval;
0f125432
CC
3223 if (psymval->is_section_symbol())
3224 symval.set_is_section_symbol();
ec661b9d
AM
3225 typedef Sized_relobj_file<size, big_endian> ObjType;
3226 typename ObjType::Compute_final_local_value_status status
3227 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3228 &symval, symtab);
3229 if (status != ObjType::CFLV_OK
3230 || !symval.has_output_value())
a3e60ddb 3231 return true;
ec661b9d 3232 to = symval.value(this->object_, 0);
9055360d
AM
3233 if (size == 64)
3234 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
ec661b9d 3235 }
cbcb23fa
AM
3236 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3237 to += this->addend_;
ec661b9d
AM
3238 if (stub_table == NULL)
3239 stub_table = this->object_->stub_table(this->shndx_);
9055360d 3240 if (size == 64 && target->abiversion() < 2)
ec661b9d
AM
3241 {
3242 unsigned int dest_shndx;
1611bc4a
AM
3243 if (!target->symval_for_branch(symtab, gsym, this->object_,
3244 &to, &dest_shndx))
3245 return true;
ec661b9d
AM
3246 }
3247 Address delta = to - from;
3248 if (delta + max_branch_offset >= 2 * max_branch_offset)
3249 {
0cfdc767
AM
3250 if (stub_table == NULL)
3251 {
3252 gold_warning(_("%s:%s: branch in non-executable section,"
3253 " no long branch stub for you"),
3254 this->object_->name().c_str(),
3255 this->object_->section_name(this->shndx_).c_str());
a3e60ddb 3256 return true;
0cfdc767 3257 }
d49044c7
AM
3258 bool save_res = (size == 64
3259 && gsym != NULL
3260 && gsym->source() == Symbol::IN_OUTPUT_DATA
3261 && gsym->output_data() == target->savres_section());
dc60b26d
AM
3262 ok = stub_table->add_long_branch_entry(this->object_,
3263 this->r_type_,
3264 from, to, save_res);
ec661b9d
AM
3265 }
3266 }
dc60b26d
AM
3267 if (!ok)
3268 gold_debug(DEBUG_TARGET,
3269 "branch at %s:%s+%#lx\n"
3270 "can't reach stub attached to %s:%s",
3271 this->object_->name().c_str(),
3272 this->object_->section_name(this->shndx_).c_str(),
3273 (unsigned long) this->offset_,
3274 stub_table->relobj()->name().c_str(),
3275 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3276
3277 return ok;
ec661b9d
AM
3278}
3279
3280// Relaxation hook. This is where we do stub generation.
3281
3282template<int size, bool big_endian>
3283bool
3284Target_powerpc<size, big_endian>::do_relax(int pass,
3285 const Input_objects*,
3286 Symbol_table* symtab,
3287 Layout* layout,
3288 const Task* task)
3289{
3290 unsigned int prev_brlt_size = 0;
3291 if (pass == 1)
ec661b9d 3292 {
b4f7960d
AM
3293 bool thread_safe
3294 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3295 if (size == 64
3296 && this->abiversion() < 2
3297 && !thread_safe
3298 && !parameters->options().user_set_plt_thread_safe())
ec661b9d 3299 {
e2458743 3300 static const char* const thread_starter[] =
9e69ed50
AM
3301 {
3302 "pthread_create",
3303 /* libstdc++ */
3304 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3305 /* librt */
3306 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3307 "mq_notify", "create_timer",
3308 /* libanl */
3309 "getaddrinfo_a",
3310 /* libgomp */
80272b8c 3311 "GOMP_parallel",
9e69ed50 3312 "GOMP_parallel_start",
80272b8c 3313 "GOMP_parallel_loop_static",
9e69ed50 3314 "GOMP_parallel_loop_static_start",
80272b8c 3315 "GOMP_parallel_loop_dynamic",
9e69ed50 3316 "GOMP_parallel_loop_dynamic_start",
80272b8c 3317 "GOMP_parallel_loop_guided",
9e69ed50 3318 "GOMP_parallel_loop_guided_start",
80272b8c 3319 "GOMP_parallel_loop_runtime",
9e69ed50 3320 "GOMP_parallel_loop_runtime_start",
80272b8c 3321 "GOMP_parallel_sections",
43819297 3322 "GOMP_parallel_sections_start",
f9dffbf0
AM
3323 /* libgo */
3324 "__go_go",
9e69ed50
AM
3325 };
3326
e2458743
AM
3327 if (parameters->options().shared())
3328 thread_safe = true;
3329 else
9e69ed50 3330 {
e2458743
AM
3331 for (unsigned int i = 0;
3332 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3333 i++)
3334 {
3335 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3336 thread_safe = (sym != NULL
3337 && sym->in_reg()
3338 && sym->in_real_elf());
3339 if (thread_safe)
3340 break;
3341 }
9e69ed50 3342 }
ec661b9d 3343 }
9e69ed50 3344 this->plt_thread_safe_ = thread_safe;
a3e60ddb
AM
3345 }
3346
3347 if (pass == 1)
3348 {
3349 this->stub_group_size_ = parameters->options().stub_group_size();
3350 bool no_size_errors = true;
3351 if (this->stub_group_size_ == 1)
3352 this->stub_group_size_ = 0x1c00000;
3353 else if (this->stub_group_size_ == -1)
3354 this->stub_group_size_ = -0x1e00000;
3355 else
3356 no_size_errors = false;
3357 this->group_sections(layout, task, no_size_errors);
3358 }
3359 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3360 {
3361 this->branch_lookup_table_.clear();
3362 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3363 p != this->stub_tables_.end();
3364 ++p)
3365 {
3366 (*p)->clear_stubs(true);
3367 }
3368 this->stub_tables_.clear();
3369 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
57f6d32d 3370 gold_info(_("%s: stub group size is too large; retrying with %#x"),
a3e60ddb
AM
3371 program_name, this->stub_group_size_);
3372 this->group_sections(layout, task, true);
ec661b9d
AM
3373 }
3374
3375 // We need address of stub tables valid for make_stub.
3376 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3377 p != this->stub_tables_.end();
3378 ++p)
3379 {
3380 const Powerpc_relobj<size, big_endian>* object
3381 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3382 Address off = object->get_output_section_offset((*p)->shndx());
3383 gold_assert(off != invalid_address);
3384 Output_section* os = (*p)->output_section();
3385 (*p)->set_address_and_size(os, off);
3386 }
3387
9e69ed50
AM
3388 if (pass != 1)
3389 {
3390 // Clear plt call stubs, long branch stubs and branch lookup table.
3391 prev_brlt_size = this->branch_lookup_table_.size();
3392 this->branch_lookup_table_.clear();
3393 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3394 p != this->stub_tables_.end();
3395 ++p)
3396 {
a3e60ddb 3397 (*p)->clear_stubs(false);
9e69ed50
AM
3398 }
3399 }
3400
3401 // Build all the stubs.
a3e60ddb 3402 this->relax_failed_ = false;
ec661b9d
AM
3403 Stub_table<size, big_endian>* ifunc_stub_table
3404 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3405 Stub_table<size, big_endian>* one_stub_table
3406 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3407 for (typename Branches::const_iterator b = this->branch_info_.begin();
3408 b != this->branch_info_.end();
3409 b++)
3410 {
a3e60ddb
AM
3411 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3412 && !this->relax_failed_)
3413 {
3414 this->relax_failed_ = true;
3415 this->relax_fail_count_++;
3416 if (this->relax_fail_count_ < 3)
3417 return true;
3418 }
ec661b9d
AM
3419 }
3420
9e69ed50 3421 // Did anything change size?
ec661b9d
AM
3422 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3423 bool again = num_huge_branches != prev_brlt_size;
3424 if (size == 64 && num_huge_branches != 0)
3425 this->make_brlt_section(layout);
3426 if (size == 64 && again)
3427 this->brlt_section_->set_current_size(num_huge_branches);
3428
be897fb7
AM
3429 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3430 p != this->stub_tables_.rend();
3431 ++p)
3432 (*p)->remove_eh_frame(layout);
3433
3434 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3435 p != this->stub_tables_.end();
3436 ++p)
3437 (*p)->add_eh_frame(layout);
3438
ec661b9d
AM
3439 typedef Unordered_set<Output_section*> Output_sections;
3440 Output_sections os_need_update;
3441 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3442 p != this->stub_tables_.end();
3443 ++p)
3444 {
3445 if ((*p)->size_update())
3446 {
3447 again = true;
3448 os_need_update.insert((*p)->output_section());
3449 }
3450 }
3451
9e69ed50
AM
3452 // Set output section offsets for all input sections in an output
3453 // section that just changed size. Anything past the stubs will
3454 // need updating.
ec661b9d
AM
3455 for (typename Output_sections::iterator p = os_need_update.begin();
3456 p != os_need_update.end();
3457 p++)
3458 {
3459 Output_section* os = *p;
3460 Address off = 0;
3461 typedef Output_section::Input_section_list Input_section_list;
3462 for (Input_section_list::const_iterator i = os->input_sections().begin();
3463 i != os->input_sections().end();
3464 ++i)
3465 {
3466 off = align_address(off, i->addralign());
3467 if (i->is_input_section() || i->is_relaxed_input_section())
3468 i->relobj()->set_section_offset(i->shndx(), off);
3469 if (i->is_relaxed_input_section())
3470 {
3471 Stub_table<size, big_endian>* stub_table
3472 = static_cast<Stub_table<size, big_endian>*>(
3473 i->relaxed_input_section());
6395d38b
HS
3474 Address stub_table_size = stub_table->set_address_and_size(os, off);
3475 off += stub_table_size;
3476 // After a few iterations, set current stub table size
3477 // as min size threshold, so later stub tables can only
3478 // grow in size.
3479 if (pass >= 4)
3480 stub_table->set_min_size_threshold(stub_table_size);
ec661b9d
AM
3481 }
3482 else
3483 off += i->data_size();
3484 }
6830ee24
AM
3485 // If .branch_lt is part of this output section, then we have
3486 // just done the offset adjustment.
ec661b9d
AM
3487 os->clear_section_offsets_need_adjustment();
3488 }
3489
3490 if (size == 64
3491 && !again
3492 && num_huge_branches != 0
3493 && parameters->options().output_is_position_independent())
3494 {
3495 // Fill in the BRLT relocs.
06f30c9d 3496 this->brlt_section_->reset_brlt_sizes();
ec661b9d
AM
3497 for (typename Branch_lookup_table::const_iterator p
3498 = this->branch_lookup_table_.begin();
3499 p != this->branch_lookup_table_.end();
3500 ++p)
3501 {
3502 this->brlt_section_->add_reloc(p->first, p->second);
3503 }
06f30c9d 3504 this->brlt_section_->finalize_brlt_sizes();
ec661b9d 3505 }
590b87ff
AM
3506
3507 if (!again
3508 && (parameters->options().user_set_emit_stub_syms()
3509 ? parameters->options().emit_stub_syms()
3510 : (size == 64
3511 || parameters->options().output_is_position_independent()
3512 || parameters->options().emit_relocs())))
3513 {
3514 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3515 p != this->stub_tables_.end();
3516 ++p)
3517 (*p)->define_stub_syms(symtab);
3518
3519 if (this->glink_ != NULL)
3520 {
3521 int stub_size = this->glink_->pltresolve_size;
3522 Address value = -stub_size;
3523 if (size == 64)
3524 {
3525 value = 8;
3526 stub_size -= 8;
3527 }
3528 this->define_local(symtab, "__glink_PLTresolve",
3529 this->glink_, value, stub_size);
3530
3531 if (size != 64)
3532 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3533 }
3534 }
3535
ec661b9d
AM
3536 return again;
3537}
3538
9d5781f8
AM
3539template<int size, bool big_endian>
3540void
3541Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3542 unsigned char* oview,
3543 uint64_t* paddress,
3544 off_t* plen) const
3545{
3546 uint64_t address = plt->address();
3547 off_t len = plt->data_size();
3548
3549 if (plt == this->glink_)
3550 {
3551 // See Output_data_glink::do_write() for glink contents.
5fe7ffdc
AM
3552 if (len == 0)
3553 {
3554 gold_assert(parameters->doing_static_link());
3555 // Static linking may need stubs, to support ifunc and long
3556 // branches. We need to create an output section for
3557 // .eh_frame early in the link process, to have a place to
3558 // attach stub .eh_frame info. We also need to have
3559 // registered a CIE that matches the stub CIE. Both of
3560 // these requirements are satisfied by creating an FDE and
3561 // CIE for .glink, even though static linking will leave
3562 // .glink zero length.
3563 // ??? Hopefully generating an FDE with a zero address range
3564 // won't confuse anything that consumes .eh_frame info.
3565 }
3566 else if (size == 64)
9d5781f8
AM
3567 {
3568 // There is one word before __glink_PLTresolve
3569 address += 8;
3570 len -= 8;
3571 }
3572 else if (parameters->options().output_is_position_independent())
3573 {
3574 // There are two FDEs for a position independent glink.
3575 // The first covers the branch table, the second
3576 // __glink_PLTresolve at the end of glink.
3577 off_t resolve_size = this->glink_->pltresolve_size;
5fe7ffdc 3578 if (oview[9] == elfcpp::DW_CFA_nop)
9d5781f8
AM
3579 len -= resolve_size;
3580 else
3581 {
3582 address += len - resolve_size;
3583 len = resolve_size;
3584 }
3585 }
3586 }
3587 else
3588 {
3589 // Must be a stub table.
3590 const Stub_table<size, big_endian>* stub_table
3591 = static_cast<const Stub_table<size, big_endian>*>(plt);
3592 uint64_t stub_address = stub_table->stub_address();
3593 len -= stub_address - address;
3594 address = stub_address;
3595 }
3596
3597 *paddress = address;
3598 *plen = len;
3599}
3600
42cacb20
DE
3601// A class to handle the PLT data.
3602
3603template<int size, bool big_endian>
cf43a2fe 3604class Output_data_plt_powerpc : public Output_section_data_build
42cacb20
DE
3605{
3606 public:
3607 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3608 size, big_endian> Reloc_section;
3609
e5d5f5ed
AM
3610 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3611 Reloc_section* plt_rel,
e5d5f5ed
AM
3612 const char* name)
3613 : Output_section_data_build(size == 32 ? 4 : 8),
3614 rel_(plt_rel),
3615 targ_(targ),
e5d5f5ed
AM
3616 name_(name)
3617 { }
42cacb20
DE
3618
3619 // Add an entry to the PLT.
03e25981 3620 void
cf43a2fe 3621 add_entry(Symbol*);
42cacb20 3622
03e25981 3623 void
e5d5f5ed
AM
3624 add_ifunc_entry(Symbol*);
3625
03e25981 3626 void
e5d5f5ed
AM
3627 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3628
42cacb20 3629 // Return the .rela.plt section data.
e5d5f5ed 3630 Reloc_section*
cf43a2fe
AM
3631 rel_plt() const
3632 {
42cacb20
DE
3633 return this->rel_;
3634 }
3635
0e70b911
CC
3636 // Return the number of PLT entries.
3637 unsigned int
3638 entry_count() const
d83ce4e3 3639 {
b3ccdeb5
AM
3640 if (this->current_data_size() == 0)
3641 return 0;
b4f7960d
AM
3642 return ((this->current_data_size() - this->first_plt_entry_offset())
3643 / this->plt_entry_size());
d83ce4e3 3644 }
0e70b911 3645
42cacb20 3646 protected:
42cacb20 3647 void
cf43a2fe 3648 do_adjust_output_section(Output_section* os)
42cacb20 3649 {
cf43a2fe 3650 os->set_entsize(0);
42cacb20
DE
3651 }
3652
6ce78956
AM
3653 // Write to a map file.
3654 void
3655 do_print_to_mapfile(Mapfile* mapfile) const
e5d5f5ed 3656 { mapfile->print_output_data(this, this->name_); }
6ce78956 3657
cf43a2fe 3658 private:
b4f7960d
AM
3659 // Return the offset of the first non-reserved PLT entry.
3660 unsigned int
3661 first_plt_entry_offset() const
3662 {
3663 // IPLT has no reserved entry.
3664 if (this->name_[3] == 'I')
3665 return 0;
3666 return this->targ_->first_plt_entry_offset();
3667 }
3668
3669 // Return the size of each PLT entry.
3670 unsigned int
3671 plt_entry_size() const
3672 {
3673 return this->targ_->plt_entry_size();
3674 }
cf43a2fe 3675
42cacb20
DE
3676 // Write out the PLT data.
3677 void
3678 do_write(Output_file*);
3679
3680 // The reloc section.
3681 Reloc_section* rel_;
cf43a2fe
AM
3682 // Allows access to .glink for do_write.
3683 Target_powerpc<size, big_endian>* targ_;
e5d5f5ed
AM
3684 // What to report in map file.
3685 const char *name_;
42cacb20
DE
3686};
3687
e5d5f5ed 3688// Add an entry to the PLT.
42cacb20
DE
3689
3690template<int size, bool big_endian>
03e25981 3691void
e5d5f5ed 3692Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
42cacb20 3693{
e5d5f5ed
AM
3694 if (!gsym->has_plt_offset())
3695 {
ec661b9d 3696 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3697 if (off == 0)
3698 off += this->first_plt_entry_offset();
3699 gsym->set_plt_offset(off);
3700 gsym->set_needs_dynsym_entry();
3701 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3702 this->rel_->add_global(gsym, dynrel, this, off, 0);
b4f7960d 3703 off += this->plt_entry_size();
e5d5f5ed
AM
3704 this->set_current_data_size(off);
3705 }
42cacb20
DE
3706}
3707
e5d5f5ed 3708// Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
42cacb20
DE
3709
3710template<int size, bool big_endian>
03e25981 3711void
e5d5f5ed 3712Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
42cacb20 3713{
cf43a2fe
AM
3714 if (!gsym->has_plt_offset())
3715 {
ec661b9d 3716 section_size_type off = this->current_data_size();
cf43a2fe 3717 gsym->set_plt_offset(off);
e5d5f5ed 3718 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3719 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3720 dynrel = elfcpp::R_PPC64_JMP_IREL;
3721 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
b4f7960d 3722 off += this->plt_entry_size();
e5d5f5ed
AM
3723 this->set_current_data_size(off);
3724 }
3725}
3726
3727// Add an entry for a local ifunc symbol to the IPLT.
3728
3729template<int size, bool big_endian>
03e25981 3730void
e5d5f5ed
AM
3731Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3732 Sized_relobj_file<size, big_endian>* relobj,
3733 unsigned int local_sym_index)
3734{
3735 if (!relobj->local_has_plt_offset(local_sym_index))
3736 {
ec661b9d 3737 section_size_type off = this->current_data_size();
e5d5f5ed
AM
3738 relobj->set_local_plt_offset(local_sym_index, off);
3739 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
b4f7960d 3740 if (size == 64 && this->targ_->abiversion() < 2)
e5d5f5ed
AM
3741 dynrel = elfcpp::R_PPC64_JMP_IREL;
3742 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3743 this, off, 0);
b4f7960d 3744 off += this->plt_entry_size();
cf43a2fe
AM
3745 this->set_current_data_size(off);
3746 }
42cacb20
DE
3747}
3748
dd93cd0a 3749static const uint32_t add_0_11_11 = 0x7c0b5a14;
9e69ed50 3750static const uint32_t add_2_2_11 = 0x7c425a14;
549dba71 3751static const uint32_t add_2_2_12 = 0x7c426214;
dd93cd0a
AM
3752static const uint32_t add_3_3_2 = 0x7c631214;
3753static const uint32_t add_3_3_13 = 0x7c636a14;
34e0882b
AM
3754static const uint32_t add_3_12_2 = 0x7c6c1214;
3755static const uint32_t add_3_12_13 = 0x7c6c6a14;
dd93cd0a 3756static const uint32_t add_11_0_11 = 0x7d605a14;
b4f7960d
AM
3757static const uint32_t add_11_2_11 = 0x7d625a14;
3758static const uint32_t add_11_11_2 = 0x7d6b1214;
3759static const uint32_t addi_0_12 = 0x380c0000;
dd93cd0a 3760static const uint32_t addi_2_2 = 0x38420000;
dd93cd0a 3761static const uint32_t addi_3_3 = 0x38630000;
b4f7960d 3762static const uint32_t addi_11_11 = 0x396b0000;
bbec1a5d 3763static const uint32_t addi_12_1 = 0x39810000;
b4f7960d 3764static const uint32_t addi_12_12 = 0x398c0000;
dd93cd0a
AM
3765static const uint32_t addis_0_2 = 0x3c020000;
3766static const uint32_t addis_0_13 = 0x3c0d0000;
bbec1a5d 3767static const uint32_t addis_2_12 = 0x3c4c0000;
b4f7960d 3768static const uint32_t addis_11_2 = 0x3d620000;
c9269dff
AM
3769static const uint32_t addis_11_11 = 0x3d6b0000;
3770static const uint32_t addis_11_30 = 0x3d7e0000;
bbec1a5d 3771static const uint32_t addis_12_1 = 0x3d810000;
397998fc 3772static const uint32_t addis_12_2 = 0x3d820000;
c9269dff 3773static const uint32_t addis_12_12 = 0x3d8c0000;
c9269dff
AM
3774static const uint32_t b = 0x48000000;
3775static const uint32_t bcl_20_31 = 0x429f0005;
3776static const uint32_t bctr = 0x4e800420;
34e0882b
AM
3777static const uint32_t bctrl = 0x4e800421;
3778static const uint32_t beqlr = 0x4d820020;
f3a0ed29 3779static const uint32_t blr = 0x4e800020;
9e69ed50 3780static const uint32_t bnectr_p4 = 0x4ce20420;
bbec1a5d 3781static const uint32_t cmpld_7_12_0 = 0x7fac0040;
9e69ed50 3782static const uint32_t cmpldi_2_0 = 0x28220000;
34e0882b
AM
3783static const uint32_t cmpdi_11_0 = 0x2c2b0000;
3784static const uint32_t cmpwi_11_0 = 0x2c0b0000;
dd93cd0a
AM
3785static const uint32_t cror_15_15_15 = 0x4def7b82;
3786static const uint32_t cror_31_31_31 = 0x4ffffb82;
f3a0ed29
AM
3787static const uint32_t ld_0_1 = 0xe8010000;
3788static const uint32_t ld_0_12 = 0xe80c0000;
dd93cd0a 3789static const uint32_t ld_2_1 = 0xe8410000;
dd93cd0a 3790static const uint32_t ld_2_2 = 0xe8420000;
b4f7960d 3791static const uint32_t ld_2_11 = 0xe84b0000;
549dba71 3792static const uint32_t ld_2_12 = 0xe84c0000;
34e0882b 3793static const uint32_t ld_11_1 = 0xe9610000;
b4f7960d 3794static const uint32_t ld_11_2 = 0xe9620000;
34e0882b 3795static const uint32_t ld_11_3 = 0xe9630000;
b4f7960d
AM
3796static const uint32_t ld_11_11 = 0xe96b0000;
3797static const uint32_t ld_12_2 = 0xe9820000;
34e0882b 3798static const uint32_t ld_12_3 = 0xe9830000;
b4f7960d 3799static const uint32_t ld_12_11 = 0xe98b0000;
9055360d 3800static const uint32_t ld_12_12 = 0xe98c0000;
f3a0ed29 3801static const uint32_t lfd_0_1 = 0xc8010000;
dd93cd0a 3802static const uint32_t li_0_0 = 0x38000000;
f3a0ed29 3803static const uint32_t li_12_0 = 0x39800000;
bbec1a5d 3804static const uint32_t lis_0 = 0x3c000000;
549dba71 3805static const uint32_t lis_2 = 0x3c400000;
c9269dff
AM
3806static const uint32_t lis_11 = 0x3d600000;
3807static const uint32_t lis_12 = 0x3d800000;
b4f7960d 3808static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
c9269dff 3809static const uint32_t lwz_0_12 = 0x800c0000;
34e0882b 3810static const uint32_t lwz_11_3 = 0x81630000;
c9269dff
AM
3811static const uint32_t lwz_11_11 = 0x816b0000;
3812static const uint32_t lwz_11_30 = 0x817e0000;
34e0882b 3813static const uint32_t lwz_12_3 = 0x81830000;
c9269dff 3814static const uint32_t lwz_12_12 = 0x818c0000;
dd93cd0a 3815static const uint32_t lwzu_0_12 = 0x840c0000;
c9269dff 3816static const uint32_t mflr_0 = 0x7c0802a6;
dd93cd0a 3817static const uint32_t mflr_11 = 0x7d6802a6;
c9269dff 3818static const uint32_t mflr_12 = 0x7d8802a6;
34e0882b
AM
3819static const uint32_t mr_0_3 = 0x7c601b78;
3820static const uint32_t mr_3_0 = 0x7c030378;
c9269dff
AM
3821static const uint32_t mtctr_0 = 0x7c0903a6;
3822static const uint32_t mtctr_11 = 0x7d6903a6;
ec661b9d 3823static const uint32_t mtctr_12 = 0x7d8903a6;
c9269dff 3824static const uint32_t mtlr_0 = 0x7c0803a6;
34e0882b 3825static const uint32_t mtlr_11 = 0x7d6803a6;
c9269dff 3826static const uint32_t mtlr_12 = 0x7d8803a6;
dd93cd0a 3827static const uint32_t nop = 0x60000000;
c9269dff 3828static const uint32_t ori_0_0_0 = 0x60000000;
b4f7960d 3829static const uint32_t srdi_0_0_2 = 0x7800f082;
f3a0ed29
AM
3830static const uint32_t std_0_1 = 0xf8010000;
3831static const uint32_t std_0_12 = 0xf80c0000;
dd93cd0a 3832static const uint32_t std_2_1 = 0xf8410000;
34e0882b 3833static const uint32_t std_11_1 = 0xf9610000;
f3a0ed29
AM
3834static const uint32_t stfd_0_1 = 0xd8010000;
3835static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
dd93cd0a 3836static const uint32_t sub_11_11_12 = 0x7d6c5850;
b4f7960d
AM
3837static const uint32_t sub_12_12_11 = 0x7d8b6050;
3838static const uint32_t xor_2_12_12 = 0x7d826278;
3839static const uint32_t xor_11_12_12 = 0x7d8b6278;
42cacb20
DE
3840
3841// Write out the PLT.
3842
3843template<int size, bool big_endian>
3844void
3845Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3846{
b3ccdeb5 3847 if (size == 32 && this->name_[3] != 'I')
cf43a2fe 3848 {
ec661b9d 3849 const section_size_type offset = this->offset();
cf43a2fe
AM
3850 const section_size_type oview_size
3851 = convert_to_section_size_type(this->data_size());
3852 unsigned char* const oview = of->get_output_view(offset, oview_size);
3853 unsigned char* pov = oview;
3854 unsigned char* endpov = oview + oview_size;
3855
e5d5f5ed 3856 // The address of the .glink branch table
cf43a2fe
AM
3857 const Output_data_glink<size, big_endian>* glink
3858 = this->targ_->glink_section();
ec661b9d 3859 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
cf43a2fe
AM
3860
3861 while (pov < endpov)
3862 {
3863 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3864 pov += 4;
3865 branch_tab += 4;
3866 }
3867
3868 of->write_output_view(offset, oview_size, oview);
3869 }
3870}
3871
3872// Create the PLT section.
3873
3874template<int size, bool big_endian>
3875void
40b469d7
AM
3876Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3877 Layout* layout)
cf43a2fe
AM
3878{
3879 if (this->plt_ == NULL)
3880 {
40b469d7
AM
3881 if (this->got_ == NULL)
3882 this->got_section(symtab, layout);
3883
cf43a2fe
AM
3884 if (this->glink_ == NULL)
3885 make_glink_section(layout);
3886
3887 // Ensure that .rela.dyn always appears before .rela.plt This is
3888 // necessary due to how, on PowerPC and some other targets, .rela.dyn
b3ccdeb5 3889 // needs to include .rela.plt in its range.
cf43a2fe
AM
3890 this->rela_dyn_section(layout);
3891
e5d5f5ed
AM
3892 Reloc_section* plt_rel = new Reloc_section(false);
3893 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3894 elfcpp::SHF_ALLOC, plt_rel,
3895 ORDER_DYNAMIC_PLT_RELOCS, false);
3896 this->plt_
3897 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
e5d5f5ed 3898 "** PLT");
cf43a2fe
AM
3899 layout->add_output_section_data(".plt",
3900 (size == 32
3901 ? elfcpp::SHT_PROGBITS
3902 : elfcpp::SHT_NOBITS),
3903 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3904 this->plt_,
3905 (size == 32
3906 ? ORDER_SMALL_DATA
3907 : ORDER_SMALL_BSS),
3908 false);
3254d32c
AM
3909
3910 Output_section* rela_plt_os = plt_rel->output_section();
3911 rela_plt_os->set_info_section(this->plt_->output_section());
cf43a2fe
AM
3912 }
3913}
3914
e5d5f5ed
AM
3915// Create the IPLT section.
3916
3917template<int size, bool big_endian>
3918void
40b469d7
AM
3919Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3920 Layout* layout)
e5d5f5ed
AM
3921{
3922 if (this->iplt_ == NULL)
3923 {
40b469d7 3924 this->make_plt_section(symtab, layout);
e5d5f5ed
AM
3925
3926 Reloc_section* iplt_rel = new Reloc_section(false);
6528b6eb
AM
3927 if (this->rela_dyn_->output_section())
3928 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
e5d5f5ed
AM
3929 this->iplt_
3930 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
b4f7960d 3931 "** IPLT");
6528b6eb
AM
3932 if (this->plt_->output_section())
3933 this->plt_->output_section()->add_output_section_data(this->iplt_);
e5d5f5ed
AM
3934 }
3935}
3936
ec661b9d 3937// A section for huge long branch addresses, similar to plt section.
cf43a2fe
AM
3938
3939template<int size, bool big_endian>
ec661b9d 3940class Output_data_brlt_powerpc : public Output_section_data_build
cf43a2fe
AM
3941{
3942 public:
ec661b9d
AM
3943 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3944 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3945 size, big_endian> Reloc_section;
c9269dff 3946
ec661b9d
AM
3947 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3948 Reloc_section* brlt_rel)
3949 : Output_section_data_build(size == 32 ? 4 : 8),
3950 rel_(brlt_rel),
3951 targ_(targ)
3952 { }
cf43a2fe 3953
06f30c9d
CC
3954 void
3955 reset_brlt_sizes()
3956 {
3957 this->reset_data_size();
3958 this->rel_->reset_data_size();
3959 }
3960
3961 void
3962 finalize_brlt_sizes()
3963 {
3964 this->finalize_data_size();
3965 this->rel_->finalize_data_size();
3966 }
3967
ec661b9d 3968 // Add a reloc for an entry in the BRLT.
cf43a2fe 3969 void
ec661b9d
AM
3970 add_reloc(Address to, unsigned int off)
3971 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
e5d5f5ed 3972
ec661b9d 3973 // Update section and reloc section size.
e5d5f5ed 3974 void
ec661b9d
AM
3975 set_current_size(unsigned int num_branches)
3976 {
3977 this->reset_address_and_file_offset();
3978 this->set_current_data_size(num_branches * 16);
3979 this->finalize_data_size();
3980 Output_section* os = this->output_section();
3981 os->set_section_offsets_need_adjustment();
3982 if (this->rel_ != NULL)
3983 {
0e123f69 3984 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
ec661b9d
AM
3985 this->rel_->reset_address_and_file_offset();
3986 this->rel_->set_current_data_size(num_branches * reloc_size);
3987 this->rel_->finalize_data_size();
3988 Output_section* os = this->rel_->output_section();
3989 os->set_section_offsets_need_adjustment();
3990 }
3991 }
cf43a2fe 3992
ec661b9d
AM
3993 protected:
3994 void
3995 do_adjust_output_section(Output_section* os)
3996 {
3997 os->set_entsize(0);
3998 }
e5d5f5ed 3999
ec661b9d
AM
4000 // Write to a map file.
4001 void
4002 do_print_to_mapfile(Mapfile* mapfile) const
4003 { mapfile->print_output_data(this, "** BRLT"); }
c9824451 4004
ec661b9d
AM
4005 private:
4006 // Write out the BRLT data.
4007 void
4008 do_write(Output_file*);
c9824451 4009
ec661b9d
AM
4010 // The reloc section.
4011 Reloc_section* rel_;
4012 Target_powerpc<size, big_endian>* targ_;
4013};
cf43a2fe 4014
ec661b9d
AM
4015// Make the branch lookup table section.
4016
4017template<int size, bool big_endian>
4018void
4019Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4020{
4021 if (size == 64 && this->brlt_section_ == NULL)
4022 {
4023 Reloc_section* brlt_rel = NULL;
4024 bool is_pic = parameters->options().output_is_position_independent();
4025 if (is_pic)
4026 {
6830ee24
AM
4027 // When PIC we can't fill in .branch_lt (like .plt it can be
4028 // a bss style section) but must initialise at runtime via
6528b6eb 4029 // dynamic relocations.
ec661b9d
AM
4030 this->rela_dyn_section(layout);
4031 brlt_rel = new Reloc_section(false);
6528b6eb
AM
4032 if (this->rela_dyn_->output_section())
4033 this->rela_dyn_->output_section()
4034 ->add_output_section_data(brlt_rel);
ec661b9d
AM
4035 }
4036 this->brlt_section_
4037 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
6528b6eb 4038 if (this->plt_ && is_pic && this->plt_->output_section())
ec661b9d
AM
4039 this->plt_->output_section()
4040 ->add_output_section_data(this->brlt_section_);
4041 else
6830ee24 4042 layout->add_output_section_data(".branch_lt",
ec661b9d
AM
4043 (is_pic ? elfcpp::SHT_NOBITS
4044 : elfcpp::SHT_PROGBITS),
4045 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4046 this->brlt_section_,
4047 (is_pic ? ORDER_SMALL_BSS
4048 : ORDER_SMALL_DATA),
4049 false);
4050 }
4051}
4052
6830ee24 4053// Write out .branch_lt when non-PIC.
ec661b9d
AM
4054
4055template<int size, bool big_endian>
4056void
4057Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4058{
4059 if (size == 64 && !parameters->options().output_is_position_independent())
4060 {
4061 const section_size_type offset = this->offset();
4062 const section_size_type oview_size
4063 = convert_to_section_size_type(this->data_size());
4064 unsigned char* const oview = of->get_output_view(offset, oview_size);
4065
4066 this->targ_->write_branch_lookup_table(oview);
4067 of->write_output_view(offset, oview_size, oview);
4068 }
4069}
4070
9e69ed50
AM
4071static inline uint32_t
4072l(uint32_t a)
4073{
4074 return a & 0xffff;
4075}
4076
4077static inline uint32_t
4078hi(uint32_t a)
4079{
4080 return l(a >> 16);
4081}
4082
4083static inline uint32_t
4084ha(uint32_t a)
4085{
4086 return hi(a + 0x8000);
4087}
4088
9d5781f8
AM
4089template<int size>
4090struct Eh_cie
4091{
4092 static const unsigned char eh_frame_cie[12];
4093};
4094
4095template<int size>
4096const unsigned char Eh_cie<size>::eh_frame_cie[] =
4097{
4098 1, // CIE version.
4099 'z', 'R', 0, // Augmentation string.
4100 4, // Code alignment.
4101 0x80 - size / 8 , // Data alignment.
4102 65, // RA reg.
4103 1, // Augmentation size.
4104 (elfcpp::DW_EH_PE_pcrel
4105 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4106 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4107};
4108
b4f7960d
AM
4109// Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4110static const unsigned char glink_eh_frame_fde_64v1[] =
9d5781f8
AM
4111{
4112 0, 0, 0, 0, // Replaced with offset to .glink.
4113 0, 0, 0, 0, // Replaced with size of .glink.
4114 0, // Augmentation size.
4115 elfcpp::DW_CFA_advance_loc + 1,
4116 elfcpp::DW_CFA_register, 65, 12,
15a3a14f 4117 elfcpp::DW_CFA_advance_loc + 5,
9d5781f8
AM
4118 elfcpp::DW_CFA_restore_extended, 65
4119};
4120
b4f7960d
AM
4121// Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4122static const unsigned char glink_eh_frame_fde_64v2[] =
4123{
4124 0, 0, 0, 0, // Replaced with offset to .glink.
4125 0, 0, 0, 0, // Replaced with size of .glink.
4126 0, // Augmentation size.
4127 elfcpp::DW_CFA_advance_loc + 1,
4128 elfcpp::DW_CFA_register, 65, 0,
15a3a14f 4129 elfcpp::DW_CFA_advance_loc + 7,
b4f7960d
AM
4130 elfcpp::DW_CFA_restore_extended, 65
4131};
4132
9d5781f8
AM
4133// Describe __glink_PLTresolve use of LR, 32-bit version.
4134static const unsigned char glink_eh_frame_fde_32[] =
4135{
4136 0, 0, 0, 0, // Replaced with offset to .glink.
4137 0, 0, 0, 0, // Replaced with size of .glink.
4138 0, // Augmentation size.
4139 elfcpp::DW_CFA_advance_loc + 2,
4140 elfcpp::DW_CFA_register, 65, 0,
4141 elfcpp::DW_CFA_advance_loc + 4,
4142 elfcpp::DW_CFA_restore_extended, 65
4143};
4144
4145static const unsigned char default_fde[] =
4146{
4147 0, 0, 0, 0, // Replaced with offset to stubs.
4148 0, 0, 0, 0, // Replaced with size of stubs.
4149 0, // Augmentation size.
4150 elfcpp::DW_CFA_nop, // Pad.
4151 elfcpp::DW_CFA_nop,
4152 elfcpp::DW_CFA_nop
4153};
4154
9e69ed50
AM
4155template<bool big_endian>
4156static inline void
4157write_insn(unsigned char* p, uint32_t v)
4158{
4159 elfcpp::Swap<32, big_endian>::writeval(p, v);
4160}
4161
ec661b9d
AM
4162// Stub_table holds information about plt and long branch stubs.
4163// Stubs are built in an area following some input section determined
4164// by group_sections(). This input section is converted to a relaxed
4165// input section allowing it to be resized to accommodate the stubs
4166
4167template<int size, bool big_endian>
4168class Stub_table : public Output_relaxed_input_section
4169{
4170 public:
7e57d19e
AM
4171 struct Plt_stub_ent
4172 {
4173 Plt_stub_ent(unsigned int off, unsigned int indx)
7ee7ff70 4174 : off_(off), indx_(indx), r2save_(0), localentry0_(0)
7e57d19e
AM
4175 { }
4176
4177 unsigned int off_;
7ee7ff70 4178 unsigned int indx_ : 30;
7e57d19e 4179 unsigned int r2save_ : 1;
7ee7ff70 4180 unsigned int localentry0_ : 1;
7e57d19e 4181 };
ec661b9d
AM
4182 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4183 static const Address invalid_address = static_cast<Address>(0) - 1;
4184
a3e60ddb
AM
4185 Stub_table(Target_powerpc<size, big_endian>* targ,
4186 Output_section* output_section,
590b87ff
AM
4187 const Output_section::Input_section* owner,
4188 uint32_t id)
a3e60ddb
AM
4189 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4190 owner->relobj()
4191 ->section_addralign(owner->shndx())),
ec661b9d 4192 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
a3e60ddb
AM
4193 orig_data_size_(owner->current_data_size()),
4194 plt_size_(0), last_plt_size_(0),
6395d38b 4195 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
34e0882b
AM
4196 need_save_res_(false), uniq_(id), tls_get_addr_opt_bctrl_(-1u),
4197 plt_fde_len_(0)
a3e60ddb
AM
4198 {
4199 this->set_output_section(output_section);
ec661b9d 4200
a3e60ddb
AM
4201 std::vector<Output_relaxed_input_section*> new_relaxed;
4202 new_relaxed.push_back(this);
4203 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4204 }
ec661b9d
AM
4205
4206 // Add a plt call stub.
a3e60ddb
AM
4207 bool
4208 add_plt_call_entry(Address,
4209 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4210 const Symbol*,
4211 unsigned int,
7e57d19e
AM
4212 Address,
4213 bool);
ec661b9d 4214
a3e60ddb
AM
4215 bool
4216 add_plt_call_entry(Address,
4217 const Sized_relobj_file<size, big_endian>*,
ec661b9d
AM
4218 unsigned int,
4219 unsigned int,
7e57d19e
AM
4220 Address,
4221 bool);
ec661b9d
AM
4222
4223 // Find a given plt call stub.
7e57d19e 4224 const Plt_stub_ent*
ec661b9d
AM
4225 find_plt_call_entry(const Symbol*) const;
4226
7e57d19e 4227 const Plt_stub_ent*
ec661b9d
AM
4228 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4229 unsigned int) const;
4230
7e57d19e 4231 const Plt_stub_ent*
ec661b9d
AM
4232 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4233 const Symbol*,
4234 unsigned int,
4235 Address) const;
4236
7e57d19e 4237 const Plt_stub_ent*
ec661b9d
AM
4238 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4239 unsigned int,
4240 unsigned int,
4241 Address) const;
4242
4243 // Add a long branch stub.
a3e60ddb
AM
4244 bool
4245 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
d49044c7 4246 unsigned int, Address, Address, bool);
ec661b9d
AM
4247
4248 Address
9d5781f8
AM
4249 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4250 Address) const;
ec661b9d 4251
a3e60ddb
AM
4252 bool
4253 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4254 {
cbcb23fa 4255 Address max_branch_offset = max_branch_delta(r_type);
a3e60ddb
AM
4256 if (max_branch_offset == 0)
4257 return true;
4258 gold_assert(from != invalid_address);
4259 Address loc = off + this->stub_address();
4260 return loc - from + max_branch_offset < 2 * max_branch_offset;
4261 }
4262
ec661b9d 4263 void
a3e60ddb 4264 clear_stubs(bool all)
cf43a2fe 4265 {
9e69ed50
AM
4266 this->plt_call_stubs_.clear();
4267 this->plt_size_ = 0;
ec661b9d
AM
4268 this->long_branch_stubs_.clear();
4269 this->branch_size_ = 0;
d49044c7 4270 this->need_save_res_ = false;
a3e60ddb
AM
4271 if (all)
4272 {
4273 this->last_plt_size_ = 0;
4274 this->last_branch_size_ = 0;
4275 }
cf43a2fe
AM
4276 }
4277
ec661b9d
AM
4278 Address
4279 set_address_and_size(const Output_section* os, Address off)
cf43a2fe 4280 {
ec661b9d
AM
4281 Address start_off = off;
4282 off += this->orig_data_size_;
4283 Address my_size = this->plt_size_ + this->branch_size_;
d49044c7
AM
4284 if (this->need_save_res_)
4285 my_size += this->targ_->savres_section()->data_size();
ec661b9d
AM
4286 if (my_size != 0)
4287 off = align_address(off, this->stub_align());
4288 // Include original section size and alignment padding in size
4289 my_size += off - start_off;
6395d38b
HS
4290 // Ensure new size is always larger than min size
4291 // threshold. Alignment requirement is included in "my_size", so
4292 // increase "my_size" does not invalidate alignment.
4293 if (my_size < this->min_size_threshold_)
4294 my_size = this->min_size_threshold_;
ec661b9d
AM
4295 this->reset_address_and_file_offset();
4296 this->set_current_data_size(my_size);
4297 this->set_address_and_file_offset(os->address() + start_off,
4298 os->offset() + start_off);
4299 return my_size;
cf43a2fe
AM
4300 }
4301
ec661b9d 4302 Address
9d5781f8 4303 stub_address() const
ec661b9d
AM
4304 {
4305 return align_address(this->address() + this->orig_data_size_,
4306 this->stub_align());
4307 }
4308
4309 Address
9d5781f8 4310 stub_offset() const
ec661b9d
AM
4311 {
4312 return align_address(this->offset() + this->orig_data_size_,
4313 this->stub_align());
4314 }
4315
4316 section_size_type
4317 plt_size() const
4318 { return this->plt_size_; }
4319
590b87ff
AM
4320 void
4321 set_min_size_threshold(Address min_size)
6395d38b
HS
4322 { this->min_size_threshold_ = min_size; }
4323
590b87ff
AM
4324 void
4325 define_stub_syms(Symbol_table*);
4326
ec661b9d
AM
4327 bool
4328 size_update()
4329 {
4330 Output_section* os = this->output_section();
4331 if (os->addralign() < this->stub_align())
4332 {
4333 os->set_addralign(this->stub_align());
4334 // FIXME: get rid of the insane checkpointing.
4335 // We can't increase alignment of the input section to which
4336 // stubs are attached; The input section may be .init which
4337 // is pasted together with other .init sections to form a
4338 // function. Aligning might insert zero padding resulting in
4339 // sigill. However we do need to increase alignment of the
4340 // output section so that the align_address() on offset in
4341 // set_address_and_size() adds the same padding as the
4342 // align_address() on address in stub_address().
4343 // What's more, we need this alignment for the layout done in
4344 // relaxation_loop_body() so that the output section starts at
4345 // a suitably aligned address.
4346 os->checkpoint_set_addralign(this->stub_align());
4347 }
9e69ed50
AM
4348 if (this->last_plt_size_ != this->plt_size_
4349 || this->last_branch_size_ != this->branch_size_)
ec661b9d 4350 {
9e69ed50
AM
4351 this->last_plt_size_ = this->plt_size_;
4352 this->last_branch_size_ = this->branch_size_;
ec661b9d
AM
4353 return true;
4354 }
4355 return false;
4356 }
4357
34e0882b 4358 // Generate a suitable FDE to describe code in this stub group.
9d5781f8 4359 void
34e0882b 4360 init_plt_fde();
be897fb7 4361
34e0882b
AM
4362 // Add .eh_frame info for this stub section.
4363 void
4364 add_eh_frame(Layout* layout);
be897fb7 4365
34e0882b 4366 // Remove .eh_frame info for this stub section.
be897fb7 4367 void
34e0882b 4368 remove_eh_frame(Layout* layout);
9d5781f8 4369
ec661b9d
AM
4370 Target_powerpc<size, big_endian>*
4371 targ() const
4372 { return targ_; }
6ce78956 4373
cf43a2fe 4374 private:
bdab445c
AM
4375 class Plt_stub_key;
4376 class Plt_stub_key_hash;
bdab445c
AM
4377 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4378 Plt_stub_key_hash> Plt_stub_entries;
590b87ff
AM
4379 class Branch_stub_ent;
4380 class Branch_stub_ent_hash;
4381 typedef Unordered_map<Branch_stub_ent, unsigned int,
4382 Branch_stub_ent_hash> Branch_stub_entries;
9e69ed50
AM
4383
4384 // Alignment of stub section.
ec661b9d 4385 unsigned int
9e69ed50
AM
4386 stub_align() const
4387 {
4388 if (size == 32)
4389 return 16;
4390 unsigned int min_align = 32;
4391 unsigned int user_align = 1 << parameters->options().plt_align();
4392 return std::max(user_align, min_align);
4393 }
cf43a2fe 4394
91c2b899
AM
4395 // Return the plt offset for the given call stub.
4396 Address
4397 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4398 {
4399 const Symbol* gsym = p->first.sym_;
4400 if (gsym != NULL)
4401 {
4402 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4403 && gsym->can_use_relative_reloc(false));
4404 return gsym->plt_offset();
4405 }
4406 else
4407 {
4408 *is_iplt = true;
4409 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4410 unsigned int local_sym_index = p->first.locsym_;
4411 return relobj->local_plt_offset(local_sym_index);
4412 }
4413 }
4414
9e69ed50 4415 // Size of a given plt call stub.
ec661b9d 4416 unsigned int
9e69ed50
AM
4417 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4418 {
4419 if (size == 32)
34e0882b
AM
4420 {
4421 const Symbol* gsym = p->first.sym_;
4422 if (this->targ_->is_tls_get_addr_opt(gsym))
4423 return 12 * 4;
4424 return 4 * 4;
4425 }
9e69ed50 4426
91c2b899
AM
4427 bool is_iplt;
4428 Address plt_addr = this->plt_off(p, &is_iplt);
4429 if (is_iplt)
4430 plt_addr += this->targ_->iplt_section()->address();
9e69ed50 4431 else
91c2b899
AM
4432 plt_addr += this->targ_->plt_section()->address();
4433 Address got_addr = this->targ_->got_section()->output_section()->address();
9e69ed50
AM
4434 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4435 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
91c2b899
AM
4436 got_addr += ppcobj->toc_base_offset();
4437 Address off = plt_addr - got_addr;
b4f7960d 4438 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
34e0882b
AM
4439 const Symbol* gsym = p->first.sym_;
4440 if (this->targ_->is_tls_get_addr_opt(gsym))
4441 bytes += 13 * 4;
b4f7960d
AM
4442 if (this->targ_->abiversion() < 2)
4443 {
4444 bool static_chain = parameters->options().plt_static_chain();
4445 bool thread_safe = this->targ_->plt_thread_safe();
4446 bytes += (4
4447 + 4 * static_chain
4448 + 8 * thread_safe
4449 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4450 }
34e0882b
AM
4451 return bytes;
4452 }
4453
4454 unsigned int
4455 plt_call_align(unsigned int bytes) const
4456 {
9e69ed50
AM
4457 unsigned int align = 1 << parameters->options().plt_align();
4458 if (align > 1)
4459 bytes = (bytes + align - 1) & -align;
4460 return bytes;
4461 }
ec661b9d
AM
4462
4463 // Return long branch stub size.
4464 unsigned int
590b87ff 4465 branch_stub_size(typename Branch_stub_entries::const_iterator p)
ec661b9d 4466 {
590b87ff
AM
4467 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4468 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
ec661b9d
AM
4469 return 4;
4470 if (size == 64 || !parameters->options().output_is_position_independent())
4471 return 16;
4472 return 32;
4473 }
4474
4475 // Write out stubs.
cf43a2fe
AM
4476 void
4477 do_write(Output_file*);
4478
ec661b9d 4479 // Plt call stub keys.
bdab445c 4480 class Plt_stub_key
cf43a2fe 4481 {
d1a8cabd 4482 public:
bdab445c 4483 Plt_stub_key(const Symbol* sym)
c9824451
AM
4484 : sym_(sym), object_(0), addend_(0), locsym_(0)
4485 { }
4486
bdab445c 4487 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d 4488 unsigned int locsym_index)
c9824451
AM
4489 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4490 { }
4491
bdab445c 4492 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4493 const Symbol* sym,
4494 unsigned int r_type,
4495 Address addend)
e5d5f5ed 4496 : sym_(sym), object_(0), addend_(0), locsym_(0)
cf43a2fe
AM
4497 {
4498 if (size != 32)
ec661b9d 4499 this->addend_ = addend;
d1a8cabd 4500 else if (parameters->options().output_is_position_independent()
ec661b9d 4501 && r_type == elfcpp::R_PPC_PLTREL24)
cf43a2fe 4502 {
ec661b9d 4503 this->addend_ = addend;
e5d5f5ed 4504 if (this->addend_ >= 32768)
d1a8cabd 4505 this->object_ = object;
cf43a2fe
AM
4506 }
4507 }
4508
bdab445c 4509 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
ec661b9d
AM
4510 unsigned int locsym_index,
4511 unsigned int r_type,
4512 Address addend)
e5d5f5ed
AM
4513 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4514 {
4515 if (size != 32)
ec661b9d 4516 this->addend_ = addend;
e5d5f5ed 4517 else if (parameters->options().output_is_position_independent()
ec661b9d
AM
4518 && r_type == elfcpp::R_PPC_PLTREL24)
4519 this->addend_ = addend;
e5d5f5ed
AM
4520 }
4521
bdab445c 4522 bool operator==(const Plt_stub_key& that) const
cf43a2fe
AM
4523 {
4524 return (this->sym_ == that.sym_
4525 && this->object_ == that.object_
e5d5f5ed
AM
4526 && this->addend_ == that.addend_
4527 && this->locsym_ == that.locsym_);
cf43a2fe 4528 }
c9269dff
AM
4529
4530 const Symbol* sym_;
e5d5f5ed
AM
4531 const Sized_relobj_file<size, big_endian>* object_;
4532 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4533 unsigned int locsym_;
cf43a2fe
AM
4534 };
4535
bdab445c 4536 class Plt_stub_key_hash
cf43a2fe 4537 {
d1a8cabd 4538 public:
bdab445c 4539 size_t operator()(const Plt_stub_key& ent) const
cf43a2fe
AM
4540 {
4541 return (reinterpret_cast<uintptr_t>(ent.sym_)
4542 ^ reinterpret_cast<uintptr_t>(ent.object_)
e5d5f5ed
AM
4543 ^ ent.addend_
4544 ^ ent.locsym_);
cf43a2fe 4545 }
ec661b9d
AM
4546 };
4547
4548 // Long branch stub keys.
4549 class Branch_stub_ent
4550 {
4551 public:
d49044c7
AM
4552 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4553 Address to, bool save_res)
4554 : dest_(to), toc_base_off_(0), save_res_(save_res)
ec661b9d
AM
4555 {
4556 if (size == 64)
4557 toc_base_off_ = obj->toc_base_offset();
4558 }
4559
4560 bool operator==(const Branch_stub_ent& that) const
4561 {
4562 return (this->dest_ == that.dest_
4563 && (size == 32
4564 || this->toc_base_off_ == that.toc_base_off_));
4565 }
cf43a2fe 4566
ec661b9d
AM
4567 Address dest_;
4568 unsigned int toc_base_off_;
d49044c7 4569 bool save_res_;
ec661b9d 4570 };
cf43a2fe 4571
ec661b9d
AM
4572 class Branch_stub_ent_hash
4573 {
4574 public:
4575 size_t operator()(const Branch_stub_ent& ent) const
4576 { return ent.dest_ ^ ent.toc_base_off_; }
4577 };
cf43a2fe 4578
ec661b9d 4579 // In a sane world this would be a global.
cf43a2fe 4580 Target_powerpc<size, big_endian>* targ_;
ec661b9d 4581 // Map sym/object/addend to stub offset.
ec661b9d
AM
4582 Plt_stub_entries plt_call_stubs_;
4583 // Map destination address to stub offset.
ec661b9d
AM
4584 Branch_stub_entries long_branch_stubs_;
4585 // size of input section
4586 section_size_type orig_data_size_;
4587 // size of stubs
9e69ed50 4588 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
6395d38b
HS
4589 // Some rare cases cause (PR/20529) fluctuation in stub table
4590 // size, which leads to an endless relax loop. This is to be fixed
4591 // by, after the first few iterations, allowing only increase of
4592 // stub table size. This variable sets the minimal possible size of
4593 // a stub table, it is zero for the first few iterations, then
4594 // increases monotonically.
4595 Address min_size_threshold_;
d49044c7
AM
4596 // Set if this stub group needs a copy of out-of-line register
4597 // save/restore functions.
4598 bool need_save_res_;
590b87ff
AM
4599 // Per stub table unique identifier.
4600 uint32_t uniq_;
34e0882b
AM
4601 // The bctrl in the __tls_get_addr_opt stub, if present.
4602 unsigned int tls_get_addr_opt_bctrl_;
4603 // FDE unwind info for this stub group.
4604 unsigned int plt_fde_len_;
4605 unsigned char plt_fde_[20];
cf43a2fe
AM
4606};
4607
ec661b9d 4608// Add a plt call stub, if we do not already have one for this
d1a8cabd 4609// sym/object/addend combo.
cf43a2fe
AM
4610
4611template<int size, bool big_endian>
a3e60ddb 4612bool
ec661b9d 4613Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4614 Address from,
c9824451 4615 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4616 const Symbol* gsym,
ec661b9d 4617 unsigned int r_type,
7e57d19e
AM
4618 Address addend,
4619 bool tocsave)
cf43a2fe 4620{
bdab445c
AM
4621 Plt_stub_key key(object, gsym, r_type, addend);
4622 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 4623 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 4624 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
9e69ed50 4625 if (p.second)
7ee7ff70
AM
4626 {
4627 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4628 if (size == 64
4629 && this->targ_->is_elfv2_localentry0(gsym))
4630 {
4631 p.first->second.localentry0_ = 1;
4632 this->targ_->set_has_localentry0();
4633 }
34e0882b
AM
4634 if (this->targ_->is_tls_get_addr_opt(gsym))
4635 {
4636 this->targ_->set_has_tls_get_addr_opt();
4637 this->tls_get_addr_opt_bctrl_ = this->plt_size_ - 5 * 4;
4638 }
4639 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70
AM
4640 }
4641 if (size == 64
4642 && !tocsave
4643 && !p.first->second.localentry0_)
7e57d19e 4644 p.first->second.r2save_ = 1;
bdab445c 4645 return this->can_reach_stub(from, ent.off_, r_type);
cf43a2fe
AM
4646}
4647
e5d5f5ed 4648template<int size, bool big_endian>
a3e60ddb 4649bool
ec661b9d 4650Stub_table<size, big_endian>::add_plt_call_entry(
a3e60ddb 4651 Address from,
c9824451 4652 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4653 unsigned int locsym_index,
ec661b9d 4654 unsigned int r_type,
7e57d19e
AM
4655 Address addend,
4656 bool tocsave)
e5d5f5ed 4657{
bdab445c
AM
4658 Plt_stub_key key(object, locsym_index, r_type, addend);
4659 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
9e69ed50 4660 std::pair<typename Plt_stub_entries::iterator, bool> p
bdab445c 4661 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
9e69ed50 4662 if (p.second)
7ee7ff70
AM
4663 {
4664 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
34e0882b 4665 this->plt_size_ = this->plt_call_align(this->plt_size_);
7ee7ff70
AM
4666 if (size == 64
4667 && this->targ_->is_elfv2_localentry0(object, locsym_index))
4668 {
4669 p.first->second.localentry0_ = 1;
4670 this->targ_->set_has_localentry0();
4671 }
4672 }
4673 if (size == 64
4674 && !tocsave
4675 && !p.first->second.localentry0_)
7e57d19e 4676 p.first->second.r2save_ = 1;
bdab445c 4677 return this->can_reach_stub(from, ent.off_, r_type);
e5d5f5ed
AM
4678}
4679
ec661b9d
AM
4680// Find a plt call stub.
4681
cf43a2fe 4682template<int size, bool big_endian>
7e57d19e 4683const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4684Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4685 const Sized_relobj_file<size, big_endian>* object,
d83ce4e3 4686 const Symbol* gsym,
ec661b9d
AM
4687 unsigned int r_type,
4688 Address addend) const
c9824451 4689{
bdab445c
AM
4690 Plt_stub_key key(object, gsym, r_type, addend);
4691 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4692 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
4693 return NULL;
4694 return &p->second;
c9824451
AM
4695}
4696
4697template<int size, bool big_endian>
7e57d19e 4698const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4699Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
cf43a2fe 4700{
bdab445c
AM
4701 Plt_stub_key key(gsym);
4702 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
4703 if (p == this->plt_call_stubs_.end())
4704 return NULL;
4705 return &p->second;
cf43a2fe
AM
4706}
4707
e5d5f5ed 4708template<int size, bool big_endian>
7e57d19e 4709const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4710Stub_table<size, big_endian>::find_plt_call_entry(
c9824451 4711 const Sized_relobj_file<size, big_endian>* object,
e5d5f5ed 4712 unsigned int locsym_index,
ec661b9d
AM
4713 unsigned int r_type,
4714 Address addend) const
e5d5f5ed 4715{
bdab445c
AM
4716 Plt_stub_key key(object, locsym_index, r_type, addend);
4717 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4718 if (p == this->plt_call_stubs_.end())
7e57d19e
AM
4719 return NULL;
4720 return &p->second;
c9824451
AM
4721}
4722
4723template<int size, bool big_endian>
7e57d19e 4724const typename Stub_table<size, big_endian>::Plt_stub_ent*
ec661b9d 4725Stub_table<size, big_endian>::find_plt_call_entry(
c9824451
AM
4726 const Sized_relobj_file<size, big_endian>* object,
4727 unsigned int locsym_index) const
4728{
bdab445c
AM
4729 Plt_stub_key key(object, locsym_index);
4730 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
7e57d19e
AM
4731 if (p == this->plt_call_stubs_.end())
4732 return NULL;
4733 return &p->second;
ec661b9d
AM
4734}
4735
4736// Add a long branch stub if we don't already have one to given
4737// destination.
4738
4739template<int size, bool big_endian>
a3e60ddb 4740bool
ec661b9d
AM
4741Stub_table<size, big_endian>::add_long_branch_entry(
4742 const Powerpc_relobj<size, big_endian>* object,
a3e60ddb
AM
4743 unsigned int r_type,
4744 Address from,
d49044c7
AM
4745 Address to,
4746 bool save_res)
ec661b9d 4747{
d49044c7 4748 Branch_stub_ent ent(object, to, save_res);
ec661b9d 4749 Address off = this->branch_size_;
590b87ff
AM
4750 std::pair<typename Branch_stub_entries::iterator, bool> p
4751 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4752 if (p.second)
ec661b9d 4753 {
d49044c7
AM
4754 if (save_res)
4755 this->need_save_res_ = true;
4756 else
4757 {
590b87ff 4758 unsigned int stub_size = this->branch_stub_size(p.first);
d49044c7
AM
4759 this->branch_size_ = off + stub_size;
4760 if (size == 64 && stub_size != 4)
4761 this->targ_->add_branch_lookup_table(to);
4762 }
ec661b9d 4763 }
a3e60ddb 4764 return this->can_reach_stub(from, off, r_type);
ec661b9d
AM
4765}
4766
d49044c7 4767// Find long branch stub offset.
ec661b9d
AM
4768
4769template<int size, bool big_endian>
ec5b8187 4770typename Stub_table<size, big_endian>::Address
ec661b9d
AM
4771Stub_table<size, big_endian>::find_long_branch_entry(
4772 const Powerpc_relobj<size, big_endian>* object,
9d5781f8 4773 Address to) const
ec661b9d 4774{
d49044c7 4775 Branch_stub_ent ent(object, to, false);
ec661b9d
AM
4776 typename Branch_stub_entries::const_iterator p
4777 = this->long_branch_stubs_.find(ent);
d49044c7
AM
4778 if (p == this->long_branch_stubs_.end())
4779 return invalid_address;
4780 if (p->first.save_res_)
4781 return to - this->targ_->savres_section()->address() + this->branch_size_;
4782 return p->second;
e5d5f5ed
AM
4783}
4784
34e0882b
AM
4785// Generate a suitable FDE to describe code in this stub group.
4786// The __tls_get_addr_opt call stub needs to describe where it saves
4787// LR, to support exceptions that might be thrown from __tls_get_addr.
4788
4789template<int size, bool big_endian>
4790void
4791Stub_table<size, big_endian>::init_plt_fde()
4792{
4793 unsigned char* p = this->plt_fde_;
4794 // offset pcrel sdata4, size udata4, and augmentation size byte.
4795 memset (p, 0, 9);
4796 p += 9;
4797 if (this->tls_get_addr_opt_bctrl_ != -1u)
4798 {
4799 unsigned int to_bctrl = this->tls_get_addr_opt_bctrl_ / 4;
4800 if (to_bctrl < 64)
4801 *p++ = elfcpp::DW_CFA_advance_loc + to_bctrl;
4802 else if (to_bctrl < 256)
4803 {
4804 *p++ = elfcpp::DW_CFA_advance_loc1;
4805 *p++ = to_bctrl;
4806 }
4807 else if (to_bctrl < 65536)
4808 {
4809 *p++ = elfcpp::DW_CFA_advance_loc2;
4810 elfcpp::Swap<16, big_endian>::writeval(p, to_bctrl);
4811 p += 2;
4812 }
4813 else
4814 {
4815 *p++ = elfcpp::DW_CFA_advance_loc4;
4816 elfcpp::Swap<32, big_endian>::writeval(p, to_bctrl);
4817 p += 4;
4818 }
4819 *p++ = elfcpp::DW_CFA_offset_extended_sf;
4820 *p++ = 65;
4821 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
4822 *p++ = elfcpp::DW_CFA_advance_loc + 4;
4823 *p++ = elfcpp::DW_CFA_restore_extended;
4824 *p++ = 65;
4825 }
4826 this->plt_fde_len_ = p - this->plt_fde_;
4827}
4828
4829// Add .eh_frame info for this stub section. Unlike other linker
4830// generated .eh_frame this is added late in the link, because we
4831// only want the .eh_frame info if this particular stub section is
4832// non-empty.
4833
4834template<int size, bool big_endian>
4835void
4836Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
4837{
4838 if (!parameters->options().ld_generated_unwind_info())
4839 return;
4840
4841 // Since we add stub .eh_frame info late, it must be placed
4842 // after all other linker generated .eh_frame info so that
4843 // merge mapping need not be updated for input sections.
4844 // There is no provision to use a different CIE to that used
4845 // by .glink.
4846 if (!this->targ_->has_glink())
4847 return;
4848
4849 if (this->plt_size_ + this->branch_size_ + this->need_save_res_ == 0)
4850 return;
4851
4852 this->init_plt_fde();
4853 layout->add_eh_frame_for_plt(this,
4854 Eh_cie<size>::eh_frame_cie,
4855 sizeof (Eh_cie<size>::eh_frame_cie),
4856 this->plt_fde_, this->plt_fde_len_);
4857}
4858
4859template<int size, bool big_endian>
4860void
4861Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
4862{
4863 if (this->plt_fde_len_ != 0)
4864 {
4865 layout->remove_eh_frame_for_plt(this,
4866 Eh_cie<size>::eh_frame_cie,
4867 sizeof (Eh_cie<size>::eh_frame_cie),
4868 this->plt_fde_, this->plt_fde_len_);
4869 this->plt_fde_len_ = 0;
4870 }
4871}
4872
ec661b9d
AM
4873// A class to handle .glink.
4874
4875template<int size, bool big_endian>
4876class Output_data_glink : public Output_section_data
4877{
4878 public:
9055360d
AM
4879 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4880 static const Address invalid_address = static_cast<Address>(0) - 1;
ec661b9d
AM
4881 static const int pltresolve_size = 16*4;
4882
4883 Output_data_glink(Target_powerpc<size, big_endian>* targ)
9055360d
AM
4884 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4885 end_branch_table_(), ge_size_(0)
ec661b9d
AM
4886 { }
4887
9d5781f8 4888 void
9055360d 4889 add_eh_frame(Layout* layout);
9d5781f8 4890
9055360d
AM
4891 void
4892 add_global_entry(const Symbol*);
4893
4894 Address
4895 find_global_entry(const Symbol*) const;
4896
4897 Address
4898 global_entry_address() const
4899 {
4900 gold_assert(this->is_data_size_valid());
4901 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4902 return this->address() + global_entry_off;
9d5781f8
AM
4903 }
4904
ec661b9d
AM
4905 protected:
4906 // Write to a map file.
4907 void
4908 do_print_to_mapfile(Mapfile* mapfile) const
4909 { mapfile->print_output_data(this, _("** glink")); }
4910
4911 private:
4912 void
4913 set_final_data_size();
4914
4915 // Write out .glink
4916 void
4917 do_write(Output_file*);
4918
4919 // Allows access to .got and .plt for do_write.
4920 Target_powerpc<size, big_endian>* targ_;
9055360d
AM
4921
4922 // Map sym to stub offset.
4923 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4924 Global_entry_stub_entries global_entry_stubs_;
4925
4926 unsigned int end_branch_table_, ge_size_;
ec661b9d
AM
4927};
4928
9055360d
AM
4929template<int size, bool big_endian>
4930void
4931Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4932{
4933 if (!parameters->options().ld_generated_unwind_info())
4934 return;
4935
4936 if (size == 64)
4937 {
4938 if (this->targ_->abiversion() < 2)
4939 layout->add_eh_frame_for_plt(this,
4940 Eh_cie<64>::eh_frame_cie,
4941 sizeof (Eh_cie<64>::eh_frame_cie),
4942 glink_eh_frame_fde_64v1,
4943 sizeof (glink_eh_frame_fde_64v1));
4944 else
4945 layout->add_eh_frame_for_plt(this,
4946 Eh_cie<64>::eh_frame_cie,
4947 sizeof (Eh_cie<64>::eh_frame_cie),
4948 glink_eh_frame_fde_64v2,
4949 sizeof (glink_eh_frame_fde_64v2));
4950 }
4951 else
4952 {
4953 // 32-bit .glink can use the default since the CIE return
4954 // address reg, LR, is valid.
4955 layout->add_eh_frame_for_plt(this,
4956 Eh_cie<32>::eh_frame_cie,
4957 sizeof (Eh_cie<32>::eh_frame_cie),
4958 default_fde,
4959 sizeof (default_fde));
4960 // Except where LR is used in a PIC __glink_PLTresolve.
4961 if (parameters->options().output_is_position_independent())
4962 layout->add_eh_frame_for_plt(this,
4963 Eh_cie<32>::eh_frame_cie,
4964 sizeof (Eh_cie<32>::eh_frame_cie),
4965 glink_eh_frame_fde_32,
4966 sizeof (glink_eh_frame_fde_32));
4967 }
4968}
4969
4970template<int size, bool big_endian>
4971void
4972Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4973{
4974 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4975 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4976 if (p.second)
4977 this->ge_size_ += 16;
4978}
4979
4980template<int size, bool big_endian>
4981typename Output_data_glink<size, big_endian>::Address
4982Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4983{
4984 typename Global_entry_stub_entries::const_iterator p
4985 = this->global_entry_stubs_.find(gsym);
4986 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4987}
4988
cf43a2fe
AM
4989template<int size, bool big_endian>
4990void
4991Output_data_glink<size, big_endian>::set_final_data_size()
4992{
ec661b9d
AM
4993 unsigned int count = this->targ_->plt_entry_count();
4994 section_size_type total = 0;
cf43a2fe
AM
4995
4996 if (count != 0)
4997 {
4998 if (size == 32)
4999 {
cf43a2fe
AM
5000 // space for branch table
5001 total += 4 * (count - 1);
5002
5003 total += -total & 15;
5004 total += this->pltresolve_size;
5005 }
5006 else
5007 {
cf43a2fe
AM
5008 total += this->pltresolve_size;
5009
5010 // space for branch table
b4f7960d
AM
5011 total += 4 * count;
5012 if (this->targ_->abiversion() < 2)
5013 {
5014 total += 4 * count;
5015 if (count > 0x8000)
5016 total += 4 * (count - 0x8000);
5017 }
cf43a2fe
AM
5018 }
5019 }
9055360d
AM
5020 this->end_branch_table_ = total;
5021 total = (total + 15) & -16;
5022 total += this->ge_size_;
cf43a2fe
AM
5023
5024 this->set_data_size(total);
5025}
5026
590b87ff
AM
5027// Define symbols on stubs, identifying the stub.
5028
5029template<int size, bool big_endian>
5030void
5031Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5032{
5033 if (!this->plt_call_stubs_.empty())
5034 {
5035 // The key for the plt call stub hash table includes addresses,
5036 // therefore traversal order depends on those addresses, which
5037 // can change between runs if gold is a PIE. Unfortunately the
5038 // output .symtab ordering depends on the order in which symbols
5039 // are added to the linker symtab. We want reproducible output
5040 // so must sort the call stub symbols.
5041 typedef typename Plt_stub_entries::const_iterator plt_iter;
5042 std::vector<plt_iter> sorted;
5043 sorted.resize(this->plt_call_stubs_.size());
5044
5045 for (plt_iter cs = this->plt_call_stubs_.begin();
5046 cs != this->plt_call_stubs_.end();
5047 ++cs)
bdab445c 5048 sorted[cs->second.indx_] = cs;
590b87ff
AM
5049
5050 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5051 {
5052 plt_iter cs = sorted[i];
5053 char add[10];
5054 add[0] = 0;
5055 if (cs->first.addend_ != 0)
5056 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
94de2a2c
JC
5057 char obj[10];
5058 obj[0] = 0;
5059 if (cs->first.object_)
590b87ff
AM
5060 {
5061 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5062 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
94de2a2c
JC
5063 sprintf(obj, "%x:", ppcobj->uniq());
5064 }
5065 char localname[9];
5066 const char *symname;
5067 if (cs->first.sym_ == NULL)
5068 {
5069 sprintf(localname, "%x", cs->first.locsym_);
590b87ff
AM
5070 symname = localname;
5071 }
34e0882b
AM
5072 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5073 symname = this->targ_->tls_get_addr_opt()->name();
590b87ff
AM
5074 else
5075 symname = cs->first.sym_->name();
94de2a2c
JC
5076 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5077 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
bdab445c
AM
5078 Address value
5079 = this->stub_address() - this->address() + cs->second.off_;
34e0882b 5080 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
590b87ff
AM
5081 this->targ_->define_local(symtab, name, this, value, stub_size);
5082 }
5083 }
5084
5085 typedef typename Branch_stub_entries::const_iterator branch_iter;
5086 for (branch_iter bs = this->long_branch_stubs_.begin();
5087 bs != this->long_branch_stubs_.end();
5088 ++bs)
5089 {
5090 if (bs->first.save_res_)
5091 continue;
5092
5093 char* name = new char[8 + 13 + 16 + 1];
5094 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5095 static_cast<unsigned long long>(bs->first.dest_));
5096 Address value = (this->stub_address() - this->address()
5097 + this->plt_size_ + bs->second);
5098 unsigned int stub_size = this->branch_stub_size(bs);
5099 this->targ_->define_local(symtab, name, this, value, stub_size);
5100 }
5101}
5102
ec661b9d 5103// Write out plt and long branch stub code.
cf43a2fe
AM
5104
5105template<int size, bool big_endian>
5106void
ec661b9d 5107Stub_table<size, big_endian>::do_write(Output_file* of)
cf43a2fe 5108{
ec661b9d
AM
5109 if (this->plt_call_stubs_.empty()
5110 && this->long_branch_stubs_.empty())
5111 return;
5112
5113 const section_size_type start_off = this->offset();
5114 const section_size_type off = this->stub_offset();
42cacb20 5115 const section_size_type oview_size =
ec661b9d 5116 convert_to_section_size_type(this->data_size() - (off - start_off));
cf43a2fe 5117 unsigned char* const oview = of->get_output_view(off, oview_size);
c9269dff 5118 unsigned char* p;
42cacb20 5119
cf43a2fe
AM
5120 if (size == 64)
5121 {
ec661b9d
AM
5122 const Output_data_got_powerpc<size, big_endian>* got
5123 = this->targ_->got_section();
dd93cd0a 5124 Address got_os_addr = got->output_section()->address();
c9269dff 5125
ec661b9d 5126 if (!this->plt_call_stubs_.empty())
cf43a2fe 5127 {
ec661b9d
AM
5128 // The base address of the .plt section.
5129 Address plt_base = this->targ_->plt_section()->address();
5130 Address iplt_base = invalid_address;
5131
5132 // Write out plt call stubs.
5133 typename Plt_stub_entries::const_iterator cs;
5134 for (cs = this->plt_call_stubs_.begin();
5135 cs != this->plt_call_stubs_.end();
5136 ++cs)
e5d5f5ed 5137 {
91c2b899
AM
5138 bool is_iplt;
5139 Address pltoff = this->plt_off(cs, &is_iplt);
9e69ed50 5140 Address plt_addr = pltoff;
91c2b899 5141 if (is_iplt)
ec661b9d
AM
5142 {
5143 if (iplt_base == invalid_address)
5144 iplt_base = this->targ_->iplt_section()->address();
5145 plt_addr += iplt_base;
5146 }
5147 else
5148 plt_addr += plt_base;
5149 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5150 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5151 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
9e69ed50 5152 Address off = plt_addr - got_addr;
ec661b9d 5153
9e69ed50 5154 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
ec661b9d
AM
5155 gold_error(_("%s: linkage table error against `%s'"),
5156 cs->first.object_->name().c_str(),
5157 cs->first.sym_->demangled_name().c_str());
5158
b4f7960d
AM
5159 bool plt_load_toc = this->targ_->abiversion() < 2;
5160 bool static_chain
5161 = plt_load_toc && parameters->options().plt_static_chain();
5162 bool thread_safe
5163 = plt_load_toc && this->targ_->plt_thread_safe();
9e69ed50
AM
5164 bool use_fake_dep = false;
5165 Address cmp_branch_off = 0;
5166 if (thread_safe)
5167 {
5168 unsigned int pltindex
5169 = ((pltoff - this->targ_->first_plt_entry_offset())
5170 / this->targ_->plt_entry_size());
5171 Address glinkoff
5172 = (this->targ_->glink_section()->pltresolve_size
5173 + pltindex * 8);
5174 if (pltindex > 32768)
5175 glinkoff += (pltindex - 32768) * 4;
5176 Address to
5177 = this->targ_->glink_section()->address() + glinkoff;
5178 Address from
7e57d19e
AM
5179 = (this->stub_address() + cs->second.off_ + 20
5180 + 4 * cs->second.r2save_
9e69ed50
AM
5181 + 4 * (ha(off) != 0)
5182 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
5183 + 4 * static_chain);
5184 cmp_branch_off = to - from;
5185 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
5186 }
5187
bdab445c 5188 p = oview + cs->second.off_;
34e0882b
AM
5189 const Symbol* gsym = cs->first.sym_;
5190 if (this->targ_->is_tls_get_addr_opt(gsym))
5191 {
5192 write_insn<big_endian>(p, ld_11_3 + 0);
5193 p += 4;
5194 write_insn<big_endian>(p, ld_12_3 + 8);
5195 p += 4;
5196 write_insn<big_endian>(p, mr_0_3);
5197 p += 4;
5198 write_insn<big_endian>(p, cmpdi_11_0);
5199 p += 4;
5200 write_insn<big_endian>(p, add_3_12_13);
5201 p += 4;
5202 write_insn<big_endian>(p, beqlr);
5203 p += 4;
5204 write_insn<big_endian>(p, mr_3_0);
5205 p += 4;
5206 if (!cs->second.localentry0_)
5207 {
5208 write_insn<big_endian>(p, mflr_11);
5209 p += 4;
5210 write_insn<big_endian>(p, (std_11_1
5211 + this->targ_->stk_linker()));
5212 p += 4;
5213 }
5214 use_fake_dep = thread_safe;
5215 }
9e69ed50 5216 if (ha(off) != 0)
ec661b9d 5217 {
7e57d19e
AM
5218 if (cs->second.r2save_)
5219 {
5220 write_insn<big_endian>(p,
5221 std_2_1 + this->targ_->stk_toc());
5222 p += 4;
5223 }
397998fc
AM
5224 if (plt_load_toc)
5225 {
5226 write_insn<big_endian>(p, addis_11_2 + ha(off));
5227 p += 4;
5228 write_insn<big_endian>(p, ld_12_11 + l(off));
5229 p += 4;
5230 }
5231 else
5232 {
5233 write_insn<big_endian>(p, addis_12_2 + ha(off));
5234 p += 4;
5235 write_insn<big_endian>(p, ld_12_12 + l(off));
5236 p += 4;
5237 }
b4f7960d
AM
5238 if (plt_load_toc
5239 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 5240 {
b4f7960d
AM
5241 write_insn<big_endian>(p, addi_11_11 + l(off));
5242 p += 4;
9e69ed50 5243 off = 0;
ec661b9d 5244 }
b4f7960d
AM
5245 write_insn<big_endian>(p, mtctr_12);
5246 p += 4;
5247 if (plt_load_toc)
9e69ed50 5248 {
b4f7960d
AM
5249 if (use_fake_dep)
5250 {
5251 write_insn<big_endian>(p, xor_2_12_12);
5252 p += 4;
5253 write_insn<big_endian>(p, add_11_11_2);
5254 p += 4;
5255 }
5256 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
5257 p += 4;
5258 if (static_chain)
5259 {
5260 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
5261 p += 4;
5262 }
9e69ed50 5263 }
ec661b9d
AM
5264 }
5265 else
5266 {
7e57d19e
AM
5267 if (cs->second.r2save_)
5268 {
5269 write_insn<big_endian>(p,
5270 std_2_1 + this->targ_->stk_toc());
5271 p += 4;
5272 }
b4f7960d
AM
5273 write_insn<big_endian>(p, ld_12_2 + l(off));
5274 p += 4;
5275 if (plt_load_toc
5276 && ha(off + 8 + 8 * static_chain) != ha(off))
ec661b9d 5277 {
b4f7960d
AM
5278 write_insn<big_endian>(p, addi_2_2 + l(off));
5279 p += 4;
9e69ed50 5280 off = 0;
ec661b9d 5281 }
b4f7960d
AM
5282 write_insn<big_endian>(p, mtctr_12);
5283 p += 4;
5284 if (plt_load_toc)
9e69ed50 5285 {
b4f7960d
AM
5286 if (use_fake_dep)
5287 {
5288 write_insn<big_endian>(p, xor_11_12_12);
5289 p += 4;
5290 write_insn<big_endian>(p, add_2_2_11);
5291 p += 4;
5292 }
5293 if (static_chain)
5294 {
5295 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
5296 p += 4;
5297 }
5298 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
5299 p += 4;
9e69ed50 5300 }
ec661b9d 5301 }
34e0882b
AM
5302 if (!cs->second.localentry0_
5303 && this->targ_->is_tls_get_addr_opt(gsym))
5304 {
5305 write_insn<big_endian>(p, bctrl);
5306 p += 4;
5307 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5308 p += 4;
5309 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5310 p += 4;
5311 write_insn<big_endian>(p, mtlr_11);
5312 p += 4;
5313 write_insn<big_endian>(p, blr);
5314 }
5315 else if (thread_safe && !use_fake_dep)
9e69ed50 5316 {
b4f7960d
AM
5317 write_insn<big_endian>(p, cmpldi_2_0);
5318 p += 4;
5319 write_insn<big_endian>(p, bnectr_p4);
5320 p += 4;
9e69ed50
AM
5321 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
5322 }
5323 else
5324 write_insn<big_endian>(p, bctr);
e5d5f5ed 5325 }
ec661b9d
AM
5326 }
5327
5328 // Write out long branch stubs.
5329 typename Branch_stub_entries::const_iterator bs;
5330 for (bs = this->long_branch_stubs_.begin();
5331 bs != this->long_branch_stubs_.end();
5332 ++bs)
5333 {
d49044c7
AM
5334 if (bs->first.save_res_)
5335 continue;
ec661b9d
AM
5336 p = oview + this->plt_size_ + bs->second;
5337 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5338 Address delta = bs->first.dest_ - loc;
5339 if (delta + (1 << 25) < 2 << 25)
5340 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
e5d5f5ed 5341 else
cf43a2fe 5342 {
ec661b9d
AM
5343 Address brlt_addr
5344 = this->targ_->find_branch_lookup_table(bs->first.dest_);
5345 gold_assert(brlt_addr != invalid_address);
5346 brlt_addr += this->targ_->brlt_section()->address();
5347 Address got_addr = got_os_addr + bs->first.toc_base_off_;
5348 Address brltoff = brlt_addr - got_addr;
5349 if (ha(brltoff) == 0)
5350 {
b4f7960d 5351 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
ec661b9d
AM
5352 }
5353 else
cf43a2fe 5354 {
397998fc
AM
5355 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
5356 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
cf43a2fe 5357 }
b4f7960d 5358 write_insn<big_endian>(p, mtctr_12), p += 4;
ec661b9d 5359 write_insn<big_endian>(p, bctr);
cf43a2fe 5360 }
ec661b9d
AM
5361 }
5362 }
5363 else
5364 {
5365 if (!this->plt_call_stubs_.empty())
5366 {
5367 // The base address of the .plt section.
5368 Address plt_base = this->targ_->plt_section()->address();
5369 Address iplt_base = invalid_address;
5370 // The address of _GLOBAL_OFFSET_TABLE_.
5371 Address g_o_t = invalid_address;
5372
5373 // Write out plt call stubs.
5374 typename Plt_stub_entries::const_iterator cs;
5375 for (cs = this->plt_call_stubs_.begin();
5376 cs != this->plt_call_stubs_.end();
5377 ++cs)
cf43a2fe 5378 {
91c2b899
AM
5379 bool is_iplt;
5380 Address plt_addr = this->plt_off(cs, &is_iplt);
5381 if (is_iplt)
ec661b9d
AM
5382 {
5383 if (iplt_base == invalid_address)
5384 iplt_base = this->targ_->iplt_section()->address();
5385 plt_addr += iplt_base;
5386 }
5387 else
5388 plt_addr += plt_base;
5389
bdab445c 5390 p = oview + cs->second.off_;
34e0882b
AM
5391 const Symbol* gsym = cs->first.sym_;
5392 if (this->targ_->is_tls_get_addr_opt(gsym))
5393 {
5394 write_insn<big_endian>(p, lwz_11_3 + 0);
5395 p += 4;
5396 write_insn<big_endian>(p, lwz_12_3 + 4);
5397 p += 4;
5398 write_insn<big_endian>(p, mr_0_3);
5399 p += 4;
5400 write_insn<big_endian>(p, cmpwi_11_0);
5401 p += 4;
5402 write_insn<big_endian>(p, add_3_12_2);
5403 p += 4;
5404 write_insn<big_endian>(p, beqlr);
5405 p += 4;
5406 write_insn<big_endian>(p, mr_3_0);
5407 p += 4;
5408 write_insn<big_endian>(p, nop);
5409 p += 4;
5410 }
ec661b9d
AM
5411 if (parameters->options().output_is_position_independent())
5412 {
5413 Address got_addr;
5414 const Powerpc_relobj<size, big_endian>* ppcobj
5415 = (static_cast<const Powerpc_relobj<size, big_endian>*>
5416 (cs->first.object_));
5417 if (ppcobj != NULL && cs->first.addend_ >= 32768)
5418 {
5419 unsigned int got2 = ppcobj->got2_shndx();
5420 got_addr = ppcobj->get_output_section_offset(got2);
5421 gold_assert(got_addr != invalid_address);
5422 got_addr += (ppcobj->output_section(got2)->address()
5423 + cs->first.addend_);
5424 }
5425 else
5426 {
5427 if (g_o_t == invalid_address)
5428 {
5429 const Output_data_got_powerpc<size, big_endian>* got
5430 = this->targ_->got_section();
5431 g_o_t = got->address() + got->g_o_t();
5432 }
5433 got_addr = g_o_t;
5434 }
5435
9e69ed50
AM
5436 Address off = plt_addr - got_addr;
5437 if (ha(off) == 0)
ec661b9d 5438 {
9e69ed50 5439 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
ec661b9d
AM
5440 write_insn<big_endian>(p + 4, mtctr_11);
5441 write_insn<big_endian>(p + 8, bctr);
5442 }
5443 else
5444 {
9e69ed50
AM
5445 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
5446 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
ec661b9d
AM
5447 write_insn<big_endian>(p + 8, mtctr_11);
5448 write_insn<big_endian>(p + 12, bctr);
5449 }
5450 }
5451 else
5452 {
5453 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
5454 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
5455 write_insn<big_endian>(p + 8, mtctr_11);
5456 write_insn<big_endian>(p + 12, bctr);
5457 }
5458 }
5459 }
5460
5461 // Write out long branch stubs.
5462 typename Branch_stub_entries::const_iterator bs;
5463 for (bs = this->long_branch_stubs_.begin();
5464 bs != this->long_branch_stubs_.end();
5465 ++bs)
5466 {
d49044c7
AM
5467 if (bs->first.save_res_)
5468 continue;
ec661b9d
AM
5469 p = oview + this->plt_size_ + bs->second;
5470 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5471 Address delta = bs->first.dest_ - loc;
5472 if (delta + (1 << 25) < 2 << 25)
5473 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5474 else if (!parameters->options().output_is_position_independent())
5475 {
5476 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
5477 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
5478 write_insn<big_endian>(p + 8, mtctr_12);
5479 write_insn<big_endian>(p + 12, bctr);
5480 }
5481 else
5482 {
5483 delta -= 8;
5484 write_insn<big_endian>(p + 0, mflr_0);
5485 write_insn<big_endian>(p + 4, bcl_20_31);
5486 write_insn<big_endian>(p + 8, mflr_12);
5487 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
5488 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
5489 write_insn<big_endian>(p + 20, mtlr_0);
5490 write_insn<big_endian>(p + 24, mtctr_12);
5491 write_insn<big_endian>(p + 28, bctr);
cf43a2fe
AM
5492 }
5493 }
ec661b9d 5494 }
d49044c7
AM
5495 if (this->need_save_res_)
5496 {
5497 p = oview + this->plt_size_ + this->branch_size_;
5498 memcpy (p, this->targ_->savres_section()->contents(),
5499 this->targ_->savres_section()->data_size());
5500 }
ec661b9d
AM
5501}
5502
5503// Write out .glink.
5504
5505template<int size, bool big_endian>
5506void
5507Output_data_glink<size, big_endian>::do_write(Output_file* of)
5508{
5509 const section_size_type off = this->offset();
5510 const section_size_type oview_size =
5511 convert_to_section_size_type(this->data_size());
5512 unsigned char* const oview = of->get_output_view(off, oview_size);
5513 unsigned char* p;
5514
5515 // The base address of the .plt section.
5516 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5517 Address plt_base = this->targ_->plt_section()->address();
cf43a2fe 5518
ec661b9d
AM
5519 if (size == 64)
5520 {
9055360d 5521 if (this->end_branch_table_ != 0)
b4f7960d 5522 {
9055360d
AM
5523 // Write pltresolve stub.
5524 p = oview;
5525 Address after_bcl = this->address() + 16;
5526 Address pltoff = plt_base - after_bcl;
5527
5528 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
cf43a2fe 5529
b4f7960d 5530 if (this->targ_->abiversion() < 2)
cf43a2fe 5531 {
9055360d
AM
5532 write_insn<big_endian>(p, mflr_12), p += 4;
5533 write_insn<big_endian>(p, bcl_20_31), p += 4;
5534 write_insn<big_endian>(p, mflr_11), p += 4;
5535 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5536 write_insn<big_endian>(p, mtlr_12), p += 4;
5537 write_insn<big_endian>(p, add_11_2_11), p += 4;
5538 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5539 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5540 write_insn<big_endian>(p, mtctr_12), p += 4;
5541 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5542 }
5543 else
5544 {
5545 write_insn<big_endian>(p, mflr_0), p += 4;
5546 write_insn<big_endian>(p, bcl_20_31), p += 4;
5547 write_insn<big_endian>(p, mflr_11), p += 4;
7ee7ff70 5548 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
9055360d
AM
5549 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5550 write_insn<big_endian>(p, mtlr_0), p += 4;
5551 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5552 write_insn<big_endian>(p, add_11_2_11), p += 4;
5553 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5554 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5555 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5556 write_insn<big_endian>(p, mtctr_12), p += 4;
5557 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5558 }
5559 write_insn<big_endian>(p, bctr), p += 4;
5560 while (p < oview + this->pltresolve_size)
5561 write_insn<big_endian>(p, nop), p += 4;
5562
5563 // Write lazy link call stubs.
5564 uint32_t indx = 0;
5565 while (p < oview + this->end_branch_table_)
5566 {
5567 if (this->targ_->abiversion() < 2)
b4f7960d 5568 {
9055360d
AM
5569 if (indx < 0x8000)
5570 {
5571 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5572 }
5573 else
5574 {
bbec1a5d 5575 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
9055360d
AM
5576 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5577 }
b4f7960d 5578 }
9055360d
AM
5579 uint32_t branch_off = 8 - (p - oview);
5580 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5581 indx++;
cf43a2fe 5582 }
9055360d
AM
5583 }
5584
5585 Address plt_base = this->targ_->plt_section()->address();
5586 Address iplt_base = invalid_address;
5587 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
5588 Address global_entry_base = this->address() + global_entry_off;
5589 typename Global_entry_stub_entries::const_iterator ge;
5590 for (ge = this->global_entry_stubs_.begin();
5591 ge != this->global_entry_stubs_.end();
5592 ++ge)
5593 {
5594 p = oview + global_entry_off + ge->second;
5595 Address plt_addr = ge->first->plt_offset();
5596 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5597 && ge->first->can_use_relative_reloc(false))
5598 {
5599 if (iplt_base == invalid_address)
5600 iplt_base = this->targ_->iplt_section()->address();
5601 plt_addr += iplt_base;
5602 }
5603 else
5604 plt_addr += plt_base;
5605 Address my_addr = global_entry_base + ge->second;
5606 Address off = plt_addr - my_addr;
5607
5608 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5609 gold_error(_("%s: linkage table error against `%s'"),
5610 ge->first->object()->name().c_str(),
5611 ge->first->demangled_name().c_str());
5612
5613 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5614 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5615 write_insn<big_endian>(p, mtctr_12), p += 4;
5616 write_insn<big_endian>(p, bctr);
cf43a2fe
AM
5617 }
5618 }
5619 else
5620 {
ec661b9d
AM
5621 const Output_data_got_powerpc<size, big_endian>* got
5622 = this->targ_->got_section();
dd93cd0a
AM
5623 // The address of _GLOBAL_OFFSET_TABLE_.
5624 Address g_o_t = got->address() + got->g_o_t();
c9269dff 5625
cf43a2fe 5626 // Write out pltresolve branch table.
ec661b9d 5627 p = oview;
cf43a2fe 5628 unsigned int the_end = oview_size - this->pltresolve_size;
c9269dff 5629 unsigned char* end_p = oview + the_end;
cf43a2fe
AM
5630 while (p < end_p - 8 * 4)
5631 write_insn<big_endian>(p, b + end_p - p), p += 4;
5632 while (p < end_p)
5633 write_insn<big_endian>(p, nop), p += 4;
42cacb20 5634
cf43a2fe
AM
5635 // Write out pltresolve call stub.
5636 if (parameters->options().output_is_position_independent())
42cacb20 5637 {
ec661b9d 5638 Address res0_off = 0;
dd93cd0a
AM
5639 Address after_bcl_off = the_end + 12;
5640 Address bcl_res0 = after_bcl_off - res0_off;
cf43a2fe
AM
5641
5642 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5643 write_insn<big_endian>(p + 4, mflr_0);
5644 write_insn<big_endian>(p + 8, bcl_20_31);
5645 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5646 write_insn<big_endian>(p + 16, mflr_12);
5647 write_insn<big_endian>(p + 20, mtlr_0);
5648 write_insn<big_endian>(p + 24, sub_11_11_12);
5649
dd93cd0a 5650 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
cf43a2fe
AM
5651
5652 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5653 if (ha(got_bcl) == ha(got_bcl + 4))
5654 {
5655 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5656 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5657 }
5658 else
5659 {
5660 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5661 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5662 }
5663 write_insn<big_endian>(p + 40, mtctr_0);
5664 write_insn<big_endian>(p + 44, add_0_11_11);
5665 write_insn<big_endian>(p + 48, add_11_0_11);
5666 write_insn<big_endian>(p + 52, bctr);
5667 write_insn<big_endian>(p + 56, nop);
5668 write_insn<big_endian>(p + 60, nop);
42cacb20 5669 }
cf43a2fe 5670 else
42cacb20 5671 {
ec661b9d 5672 Address res0 = this->address();
cf43a2fe
AM
5673
5674 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5675 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5676 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5677 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5678 else
5679 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5680 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5681 write_insn<big_endian>(p + 16, mtctr_0);
5682 write_insn<big_endian>(p + 20, add_0_11_11);
5683 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5684 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5685 else
5686 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5687 write_insn<big_endian>(p + 28, add_11_0_11);
5688 write_insn<big_endian>(p + 32, bctr);
5689 write_insn<big_endian>(p + 36, nop);
5690 write_insn<big_endian>(p + 40, nop);
5691 write_insn<big_endian>(p + 44, nop);
5692 write_insn<big_endian>(p + 48, nop);
5693 write_insn<big_endian>(p + 52, nop);
5694 write_insn<big_endian>(p + 56, nop);
5695 write_insn<big_endian>(p + 60, nop);
42cacb20 5696 }
cf43a2fe 5697 p += 64;
42cacb20
DE
5698 }
5699
cf43a2fe
AM
5700 of->write_output_view(off, oview_size, oview);
5701}
5702
f3a0ed29
AM
5703
5704// A class to handle linker generated save/restore functions.
5705
5706template<int size, bool big_endian>
5707class Output_data_save_res : public Output_section_data_build
5708{
5709 public:
5710 Output_data_save_res(Symbol_table* symtab);
5711
d49044c7
AM
5712 const unsigned char*
5713 contents() const
5714 {
5715 return contents_;
5716 }
5717
f3a0ed29
AM
5718 protected:
5719 // Write to a map file.
5720 void
5721 do_print_to_mapfile(Mapfile* mapfile) const
5722 { mapfile->print_output_data(this, _("** save/restore")); }
5723
5724 void
5725 do_write(Output_file*);
5726
5727 private:
5728 // The maximum size of save/restore contents.
5729 static const unsigned int savres_max = 218*4;
5730
5731 void
5732 savres_define(Symbol_table* symtab,
5733 const char *name,
5734 unsigned int lo, unsigned int hi,
5735 unsigned char* write_ent(unsigned char*, int),
5736 unsigned char* write_tail(unsigned char*, int));
5737
5738 unsigned char *contents_;
5739};
5740
5741template<bool big_endian>
5742static unsigned char*
5743savegpr0(unsigned char* p, int r)
5744{
5745 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5746 write_insn<big_endian>(p, insn);
5747 return p + 4;
5748}
5749
5750template<bool big_endian>
5751static unsigned char*
5752savegpr0_tail(unsigned char* p, int r)
5753{
5754 p = savegpr0<big_endian>(p, r);
5755 uint32_t insn = std_0_1 + 16;
5756 write_insn<big_endian>(p, insn);
5757 p = p + 4;
5758 write_insn<big_endian>(p, blr);
5759 return p + 4;
5760}
5761
5762template<bool big_endian>
62fe925a 5763static unsigned char*
f3a0ed29
AM
5764restgpr0(unsigned char* p, int r)
5765{
5766 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5767 write_insn<big_endian>(p, insn);
5768 return p + 4;
5769}
5770
5771template<bool big_endian>
62fe925a 5772static unsigned char*
f3a0ed29
AM
5773restgpr0_tail(unsigned char* p, int r)
5774{
5775 uint32_t insn = ld_0_1 + 16;
5776 write_insn<big_endian>(p, insn);
5777 p = p + 4;
5778 p = restgpr0<big_endian>(p, r);
5779 write_insn<big_endian>(p, mtlr_0);
5780 p = p + 4;
5781 if (r == 29)
5782 {
5783 p = restgpr0<big_endian>(p, 30);
5784 p = restgpr0<big_endian>(p, 31);
5785 }
5786 write_insn<big_endian>(p, blr);
5787 return p + 4;
5788}
5789
5790template<bool big_endian>
62fe925a 5791static unsigned char*
f3a0ed29
AM
5792savegpr1(unsigned char* p, int r)
5793{
5794 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5795 write_insn<big_endian>(p, insn);
5796 return p + 4;
5797}
5798
5799template<bool big_endian>
62fe925a 5800static unsigned char*
f3a0ed29
AM
5801savegpr1_tail(unsigned char* p, int r)
5802{
5803 p = savegpr1<big_endian>(p, r);
5804 write_insn<big_endian>(p, blr);
5805 return p + 4;
5806}
5807
5808template<bool big_endian>
62fe925a 5809static unsigned char*
f3a0ed29
AM
5810restgpr1(unsigned char* p, int r)
5811{
5812 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5813 write_insn<big_endian>(p, insn);
5814 return p + 4;
5815}
5816
5817template<bool big_endian>
62fe925a 5818static unsigned char*
f3a0ed29
AM
5819restgpr1_tail(unsigned char* p, int r)
5820{
5821 p = restgpr1<big_endian>(p, r);
5822 write_insn<big_endian>(p, blr);
5823 return p + 4;
5824}
5825
5826template<bool big_endian>
62fe925a 5827static unsigned char*
f3a0ed29
AM
5828savefpr(unsigned char* p, int r)
5829{
5830 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5831 write_insn<big_endian>(p, insn);
5832 return p + 4;
5833}
5834
5835template<bool big_endian>
62fe925a 5836static unsigned char*
f3a0ed29
AM
5837savefpr0_tail(unsigned char* p, int r)
5838{
5839 p = savefpr<big_endian>(p, r);
5840 write_insn<big_endian>(p, std_0_1 + 16);
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
5848restfpr(unsigned char* p, int r)
5849{
5850 uint32_t insn = lfd_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
5857restfpr0_tail(unsigned char* p, int r)
5858{
5859 write_insn<big_endian>(p, ld_0_1 + 16);
5860 p = p + 4;
5861 p = restfpr<big_endian>(p, r);
5862 write_insn<big_endian>(p, mtlr_0);
5863 p = p + 4;
5864 if (r == 29)
5865 {
5866 p = restfpr<big_endian>(p, 30);
5867 p = restfpr<big_endian>(p, 31);
5868 }
5869 write_insn<big_endian>(p, blr);
5870 return p + 4;
5871}
5872
5873template<bool big_endian>
62fe925a 5874static unsigned char*
f3a0ed29
AM
5875savefpr1_tail(unsigned char* p, int r)
5876{
5877 p = savefpr<big_endian>(p, r);
5878 write_insn<big_endian>(p, blr);
5879 return p + 4;
5880}
5881
5882template<bool big_endian>
62fe925a 5883static unsigned char*
f3a0ed29
AM
5884restfpr1_tail(unsigned char* p, int r)
5885{
5886 p = restfpr<big_endian>(p, r);
5887 write_insn<big_endian>(p, blr);
5888 return p + 4;
5889}
5890
5891template<bool big_endian>
62fe925a 5892static unsigned char*
f3a0ed29
AM
5893savevr(unsigned char* p, int r)
5894{
5895 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5896 write_insn<big_endian>(p, insn);
5897 p = p + 4;
5898 insn = stvx_0_12_0 + (r << 21);
5899 write_insn<big_endian>(p, insn);
5900 return p + 4;
5901}
5902
5903template<bool big_endian>
62fe925a 5904static unsigned char*
f3a0ed29
AM
5905savevr_tail(unsigned char* p, int r)
5906{
5907 p = savevr<big_endian>(p, r);
5908 write_insn<big_endian>(p, blr);
5909 return p + 4;
5910}
5911
5912template<bool big_endian>
62fe925a 5913static unsigned char*
f3a0ed29
AM
5914restvr(unsigned char* p, int r)
5915{
5916 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5917 write_insn<big_endian>(p, insn);
5918 p = p + 4;
5919 insn = lvx_0_12_0 + (r << 21);
5920 write_insn<big_endian>(p, insn);
5921 return p + 4;
5922}
5923
5924template<bool big_endian>
62fe925a 5925static unsigned char*
f3a0ed29
AM
5926restvr_tail(unsigned char* p, int r)
5927{
5928 p = restvr<big_endian>(p, r);
5929 write_insn<big_endian>(p, blr);
5930 return p + 4;
5931}
5932
5933
5934template<int size, bool big_endian>
5935Output_data_save_res<size, big_endian>::Output_data_save_res(
5936 Symbol_table* symtab)
5937 : Output_section_data_build(4),
5938 contents_(NULL)
5939{
5940 this->savres_define(symtab,
5941 "_savegpr0_", 14, 31,
5942 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5943 this->savres_define(symtab,
5944 "_restgpr0_", 14, 29,
5945 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5946 this->savres_define(symtab,
5947 "_restgpr0_", 30, 31,
5948 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5949 this->savres_define(symtab,
5950 "_savegpr1_", 14, 31,
5951 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5952 this->savres_define(symtab,
5953 "_restgpr1_", 14, 31,
5954 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5955 this->savres_define(symtab,
5956 "_savefpr_", 14, 31,
5957 savefpr<big_endian>, savefpr0_tail<big_endian>);
5958 this->savres_define(symtab,
5959 "_restfpr_", 14, 29,
5960 restfpr<big_endian>, restfpr0_tail<big_endian>);
5961 this->savres_define(symtab,
5962 "_restfpr_", 30, 31,
5963 restfpr<big_endian>, restfpr0_tail<big_endian>);
5964 this->savres_define(symtab,
5965 "._savef", 14, 31,
5966 savefpr<big_endian>, savefpr1_tail<big_endian>);
5967 this->savres_define(symtab,
5968 "._restf", 14, 31,
5969 restfpr<big_endian>, restfpr1_tail<big_endian>);
5970 this->savres_define(symtab,
5971 "_savevr_", 20, 31,
5972 savevr<big_endian>, savevr_tail<big_endian>);
5973 this->savres_define(symtab,
5974 "_restvr_", 20, 31,
5975 restvr<big_endian>, restvr_tail<big_endian>);
5976}
5977
5978template<int size, bool big_endian>
5979void
5980Output_data_save_res<size, big_endian>::savres_define(
5981 Symbol_table* symtab,
5982 const char *name,
5983 unsigned int lo, unsigned int hi,
5984 unsigned char* write_ent(unsigned char*, int),
5985 unsigned char* write_tail(unsigned char*, int))
5986{
5987 size_t len = strlen(name);
5988 bool writing = false;
5989 char sym[16];
5990
5991 memcpy(sym, name, len);
5992 sym[len + 2] = 0;
5993
5994 for (unsigned int i = lo; i <= hi; i++)
5995 {
5996 sym[len + 0] = i / 10 + '0';
5997 sym[len + 1] = i % 10 + '0';
5998 Symbol* gsym = symtab->lookup(sym);
5999 bool refd = gsym != NULL && gsym->is_undefined();
6000 writing = writing || refd;
6001 if (writing)
6002 {
6003 if (this->contents_ == NULL)
6004 this->contents_ = new unsigned char[this->savres_max];
6005
ec661b9d 6006 section_size_type value = this->current_data_size();
f3a0ed29
AM
6007 unsigned char* p = this->contents_ + value;
6008 if (i != hi)
6009 p = write_ent(p, i);
6010 else
6011 p = write_tail(p, i);
ec661b9d 6012 section_size_type cur_size = p - this->contents_;
f3a0ed29
AM
6013 this->set_current_data_size(cur_size);
6014 if (refd)
6015 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
6016 this, value, cur_size - value,
6017 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
6018 elfcpp::STV_HIDDEN, 0, false, false);
6019 }
6020 }
6021}
6022
6023// Write out save/restore.
6024
6025template<int size, bool big_endian>
6026void
6027Output_data_save_res<size, big_endian>::do_write(Output_file* of)
6028{
ec661b9d 6029 const section_size_type off = this->offset();
f3a0ed29
AM
6030 const section_size_type oview_size =
6031 convert_to_section_size_type(this->data_size());
6032 unsigned char* const oview = of->get_output_view(off, oview_size);
6033 memcpy(oview, this->contents_, oview_size);
6034 of->write_output_view(off, oview_size, oview);
6035}
6036
6037
cf43a2fe 6038// Create the glink section.
42cacb20 6039
cf43a2fe
AM
6040template<int size, bool big_endian>
6041void
6042Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
6043{
6044 if (this->glink_ == NULL)
6045 {
6046 this->glink_ = new Output_data_glink<size, big_endian>(this);
9d5781f8 6047 this->glink_->add_eh_frame(layout);
cf43a2fe
AM
6048 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6049 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6050 this->glink_, ORDER_TEXT, false);
6051 }
42cacb20
DE
6052}
6053
6054// Create a PLT entry for a global symbol.
6055
6056template<int size, bool big_endian>
6057void
ec661b9d
AM
6058Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
6059 Layout* layout,
6060 Symbol* gsym)
42cacb20 6061{
e5d5f5ed
AM
6062 if (gsym->type() == elfcpp::STT_GNU_IFUNC
6063 && gsym->can_use_relative_reloc(false))
6064 {
6065 if (this->iplt_ == NULL)
40b469d7 6066 this->make_iplt_section(symtab, layout);
03e25981 6067 this->iplt_->add_ifunc_entry(gsym);
e5d5f5ed
AM
6068 }
6069 else
6070 {
6071 if (this->plt_ == NULL)
40b469d7 6072 this->make_plt_section(symtab, layout);
03e25981 6073 this->plt_->add_entry(gsym);
e5d5f5ed 6074 }
e5d5f5ed 6075}
42cacb20 6076
e5d5f5ed 6077// Make a PLT entry for a local STT_GNU_IFUNC symbol.
612a8d3d 6078
e5d5f5ed
AM
6079template<int size, bool big_endian>
6080void
6081Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
40b469d7 6082 Symbol_table* symtab,
e5d5f5ed 6083 Layout* layout,
ec661b9d
AM
6084 Sized_relobj_file<size, big_endian>* relobj,
6085 unsigned int r_sym)
e5d5f5ed
AM
6086{
6087 if (this->iplt_ == NULL)
40b469d7 6088 this->make_iplt_section(symtab, layout);
03e25981 6089 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
42cacb20
DE
6090}
6091
0e70b911
CC
6092// Return the number of entries in the PLT.
6093
6094template<int size, bool big_endian>
6095unsigned int
6096Target_powerpc<size, big_endian>::plt_entry_count() const
6097{
6098 if (this->plt_ == NULL)
6099 return 0;
b3ccdeb5 6100 return this->plt_->entry_count();
0e70b911
CC
6101}
6102
dd93cd0a 6103// Create a GOT entry for local dynamic __tls_get_addr calls.
42cacb20
DE
6104
6105template<int size, bool big_endian>
6106unsigned int
dd93cd0a 6107Target_powerpc<size, big_endian>::tlsld_got_offset(
6fa2a40b
CC
6108 Symbol_table* symtab,
6109 Layout* layout,
6110 Sized_relobj_file<size, big_endian>* object)
42cacb20 6111{
dd93cd0a 6112 if (this->tlsld_got_offset_ == -1U)
42cacb20
DE
6113 {
6114 gold_assert(symtab != NULL && layout != NULL && object != NULL);
6115 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
dd93cd0a
AM
6116 Output_data_got_powerpc<size, big_endian>* got
6117 = this->got_section(symtab, layout);
6118 unsigned int got_offset = got->add_constant_pair(0, 0);
42cacb20
DE
6119 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
6120 got_offset, 0);
dd93cd0a 6121 this->tlsld_got_offset_ = got_offset;
42cacb20 6122 }
dd93cd0a 6123 return this->tlsld_got_offset_;
42cacb20
DE
6124}
6125
95a2c8d6
RS
6126// Get the Reference_flags for a particular relocation.
6127
6128template<int size, bool big_endian>
6129int
88b8e639
AM
6130Target_powerpc<size, big_endian>::Scan::get_reference_flags(
6131 unsigned int r_type,
6132 const Target_powerpc* target)
95a2c8d6 6133{
88b8e639
AM
6134 int ref = 0;
6135
95a2c8d6
RS
6136 switch (r_type)
6137 {
6138 case elfcpp::R_POWERPC_NONE:
6139 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6140 case elfcpp::R_POWERPC_GNU_VTENTRY:
6141 case elfcpp::R_PPC64_TOC:
6142 // No symbol reference.
88b8e639 6143 break;
95a2c8d6 6144
dd93cd0a
AM
6145 case elfcpp::R_PPC64_ADDR64:
6146 case elfcpp::R_PPC64_UADDR64:
6147 case elfcpp::R_POWERPC_ADDR32:
6148 case elfcpp::R_POWERPC_UADDR32:
95a2c8d6 6149 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 6150 case elfcpp::R_POWERPC_UADDR16:
95a2c8d6
RS
6151 case elfcpp::R_POWERPC_ADDR16_LO:
6152 case elfcpp::R_POWERPC_ADDR16_HI:
6153 case elfcpp::R_POWERPC_ADDR16_HA:
88b8e639
AM
6154 ref = Symbol::ABSOLUTE_REF;
6155 break;
95a2c8d6 6156
dd93cd0a
AM
6157 case elfcpp::R_POWERPC_ADDR24:
6158 case elfcpp::R_POWERPC_ADDR14:
6159 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6160 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
88b8e639
AM
6161 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
6162 break;
dd93cd0a 6163
e5d5f5ed 6164 case elfcpp::R_PPC64_REL64:
dd93cd0a 6165 case elfcpp::R_POWERPC_REL32:
95a2c8d6 6166 case elfcpp::R_PPC_LOCAL24PC:
6ce78956
AM
6167 case elfcpp::R_POWERPC_REL16:
6168 case elfcpp::R_POWERPC_REL16_LO:
6169 case elfcpp::R_POWERPC_REL16_HI:
6170 case elfcpp::R_POWERPC_REL16_HA:
88b8e639
AM
6171 ref = Symbol::RELATIVE_REF;
6172 break;
95a2c8d6 6173
dd93cd0a 6174 case elfcpp::R_POWERPC_REL24:
95a2c8d6 6175 case elfcpp::R_PPC_PLTREL24:
dd93cd0a
AM
6176 case elfcpp::R_POWERPC_REL14:
6177 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6178 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
88b8e639
AM
6179 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
6180 break;
95a2c8d6
RS
6181
6182 case elfcpp::R_POWERPC_GOT16:
6183 case elfcpp::R_POWERPC_GOT16_LO:
6184 case elfcpp::R_POWERPC_GOT16_HI:
6185 case elfcpp::R_POWERPC_GOT16_HA:
e5d5f5ed
AM
6186 case elfcpp::R_PPC64_GOT16_DS:
6187 case elfcpp::R_PPC64_GOT16_LO_DS:
95a2c8d6
RS
6188 case elfcpp::R_PPC64_TOC16:
6189 case elfcpp::R_PPC64_TOC16_LO:
6190 case elfcpp::R_PPC64_TOC16_HI:
6191 case elfcpp::R_PPC64_TOC16_HA:
6192 case elfcpp::R_PPC64_TOC16_DS:
6193 case elfcpp::R_PPC64_TOC16_LO_DS:
32d849b3 6194 ref = Symbol::RELATIVE_REF;
88b8e639 6195 break;
95a2c8d6
RS
6196
6197 case elfcpp::R_POWERPC_GOT_TPREL16:
6198 case elfcpp::R_POWERPC_TLS:
88b8e639
AM
6199 ref = Symbol::TLS_REF;
6200 break;
95a2c8d6
RS
6201
6202 case elfcpp::R_POWERPC_COPY:
6203 case elfcpp::R_POWERPC_GLOB_DAT:
6204 case elfcpp::R_POWERPC_JMP_SLOT:
6205 case elfcpp::R_POWERPC_RELATIVE:
6206 case elfcpp::R_POWERPC_DTPMOD:
6207 default:
6208 // Not expected. We will give an error later.
88b8e639 6209 break;
95a2c8d6 6210 }
88b8e639
AM
6211
6212 if (size == 64 && target->abiversion() < 2)
6213 ref |= Symbol::FUNC_DESC_ABI;
6214 return ref;
95a2c8d6
RS
6215}
6216
42cacb20
DE
6217// Report an unsupported relocation against a local symbol.
6218
6219template<int size, bool big_endian>
6220void
6221Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
d83ce4e3
AM
6222 Sized_relobj_file<size, big_endian>* object,
6223 unsigned int r_type)
42cacb20
DE
6224{
6225 gold_error(_("%s: unsupported reloc %u against local symbol"),
6226 object->name().c_str(), r_type);
6227}
6228
6229// We are about to emit a dynamic relocation of type R_TYPE. If the
6230// dynamic linker does not support it, issue an error.
6231
6232template<int size, bool big_endian>
6233void
6234Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
6235 unsigned int r_type)
6236{
6237 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
6238
6239 // These are the relocation types supported by glibc for both 32-bit
6240 // and 64-bit powerpc.
6241 switch (r_type)
6242 {
3ea0a085 6243 case elfcpp::R_POWERPC_NONE:
42cacb20
DE
6244 case elfcpp::R_POWERPC_RELATIVE:
6245 case elfcpp::R_POWERPC_GLOB_DAT:
6246 case elfcpp::R_POWERPC_DTPMOD:
6247 case elfcpp::R_POWERPC_DTPREL:
6248 case elfcpp::R_POWERPC_TPREL:
6249 case elfcpp::R_POWERPC_JMP_SLOT:
6250 case elfcpp::R_POWERPC_COPY:
3ea0a085 6251 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20 6252 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 6253 case elfcpp::R_POWERPC_UADDR32:
42cacb20 6254 case elfcpp::R_POWERPC_ADDR24:
3ea0a085
AM
6255 case elfcpp::R_POWERPC_ADDR16:
6256 case elfcpp::R_POWERPC_UADDR16:
6257 case elfcpp::R_POWERPC_ADDR16_LO:
6258 case elfcpp::R_POWERPC_ADDR16_HI:
6259 case elfcpp::R_POWERPC_ADDR16_HA:
6260 case elfcpp::R_POWERPC_ADDR14:
6261 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6262 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6263 case elfcpp::R_POWERPC_REL32:
42cacb20 6264 case elfcpp::R_POWERPC_REL24:
3ea0a085
AM
6265 case elfcpp::R_POWERPC_TPREL16:
6266 case elfcpp::R_POWERPC_TPREL16_LO:
6267 case elfcpp::R_POWERPC_TPREL16_HI:
6268 case elfcpp::R_POWERPC_TPREL16_HA:
42cacb20
DE
6269 return;
6270
6271 default:
6272 break;
6273 }
6274
6275 if (size == 64)
6276 {
6277 switch (r_type)
6278 {
6279 // These are the relocation types supported only on 64-bit.
6280 case elfcpp::R_PPC64_ADDR64:
42cacb20 6281 case elfcpp::R_PPC64_UADDR64:
3ea0a085 6282 case elfcpp::R_PPC64_JMP_IREL:
42cacb20 6283 case elfcpp::R_PPC64_ADDR16_DS:
3ea0a085 6284 case elfcpp::R_PPC64_ADDR16_LO_DS:
f9c6b907
AM
6285 case elfcpp::R_PPC64_ADDR16_HIGH:
6286 case elfcpp::R_PPC64_ADDR16_HIGHA:
42cacb20
DE
6287 case elfcpp::R_PPC64_ADDR16_HIGHER:
6288 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6289 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6290 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
42cacb20 6291 case elfcpp::R_PPC64_REL64:
3ea0a085
AM
6292 case elfcpp::R_POWERPC_ADDR30:
6293 case elfcpp::R_PPC64_TPREL16_DS:
6294 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6295 case elfcpp::R_PPC64_TPREL16_HIGH:
6296 case elfcpp::R_PPC64_TPREL16_HIGHA:
3ea0a085
AM
6297 case elfcpp::R_PPC64_TPREL16_HIGHER:
6298 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6299 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6300 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
42cacb20
DE
6301 return;
6302
6303 default:
6304 break;
6305 }
6306 }
6307 else
6308 {
6309 switch (r_type)
6310 {
6311 // These are the relocation types supported only on 32-bit.
3ea0a085
AM
6312 // ??? glibc ld.so doesn't need to support these.
6313 case elfcpp::R_POWERPC_DTPREL16:
6314 case elfcpp::R_POWERPC_DTPREL16_LO:
6315 case elfcpp::R_POWERPC_DTPREL16_HI:
6316 case elfcpp::R_POWERPC_DTPREL16_HA:
6317 return;
42cacb20
DE
6318
6319 default:
6320 break;
6321 }
6322 }
6323
6324 // This prevents us from issuing more than one error per reloc
6325 // section. But we can still wind up issuing more than one
6326 // error per object file.
6327 if (this->issued_non_pic_error_)
6328 return;
33aea2fd 6329 gold_assert(parameters->options().output_is_position_independent());
42cacb20
DE
6330 object->error(_("requires unsupported dynamic reloc; "
6331 "recompile with -fPIC"));
6332 this->issued_non_pic_error_ = true;
6333 return;
6334}
6335
e5d5f5ed
AM
6336// Return whether we need to make a PLT entry for a relocation of the
6337// given type against a STT_GNU_IFUNC symbol.
6338
6339template<int size, bool big_endian>
6340bool
6341Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
9055360d 6342 Target_powerpc<size, big_endian>* target,
e5d5f5ed 6343 Sized_relobj_file<size, big_endian>* object,
b3ccdeb5
AM
6344 unsigned int r_type,
6345 bool report_err)
e5d5f5ed 6346{
c9824451
AM
6347 // In non-pic code any reference will resolve to the plt call stub
6348 // for the ifunc symbol.
9055360d
AM
6349 if ((size == 32 || target->abiversion() >= 2)
6350 && !parameters->options().output_is_position_independent())
c9824451
AM
6351 return true;
6352
e5d5f5ed
AM
6353 switch (r_type)
6354 {
b3ccdeb5 6355 // Word size refs from data sections are OK, but don't need a PLT entry.
e5d5f5ed
AM
6356 case elfcpp::R_POWERPC_ADDR32:
6357 case elfcpp::R_POWERPC_UADDR32:
6358 if (size == 32)
b3ccdeb5 6359 return false;
e5d5f5ed
AM
6360 break;
6361
6362 case elfcpp::R_PPC64_ADDR64:
6363 case elfcpp::R_PPC64_UADDR64:
6364 if (size == 64)
b3ccdeb5 6365 return false;
e5d5f5ed
AM
6366 break;
6367
b3ccdeb5 6368 // GOT refs are good, but also don't need a PLT entry.
e5d5f5ed
AM
6369 case elfcpp::R_POWERPC_GOT16:
6370 case elfcpp::R_POWERPC_GOT16_LO:
6371 case elfcpp::R_POWERPC_GOT16_HI:
6372 case elfcpp::R_POWERPC_GOT16_HA:
6373 case elfcpp::R_PPC64_GOT16_DS:
6374 case elfcpp::R_PPC64_GOT16_LO_DS:
b3ccdeb5 6375 return false;
e5d5f5ed 6376
b3ccdeb5 6377 // Function calls are good, and these do need a PLT entry.
e5d5f5ed
AM
6378 case elfcpp::R_POWERPC_ADDR24:
6379 case elfcpp::R_POWERPC_ADDR14:
6380 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6381 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6382 case elfcpp::R_POWERPC_REL24:
6383 case elfcpp::R_PPC_PLTREL24:
6384 case elfcpp::R_POWERPC_REL14:
6385 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6386 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6387 return true;
6388
6389 default:
6390 break;
6391 }
6392
6393 // Anything else is a problem.
6394 // If we are building a static executable, the libc startup function
6395 // responsible for applying indirect function relocations is going
6396 // to complain about the reloc type.
6397 // If we are building a dynamic executable, we will have a text
6398 // relocation. The dynamic loader will set the text segment
6399 // writable and non-executable to apply text relocations. So we'll
6400 // segfault when trying to run the indirection function to resolve
6401 // the reloc.
b3ccdeb5
AM
6402 if (report_err)
6403 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
e5d5f5ed
AM
6404 object->name().c_str(), r_type);
6405 return false;
6406}
6407
5edad15d
AM
6408// Return TRUE iff INSN is one we expect on a _LO variety toc/got
6409// reloc.
6410
6411static bool
6412ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
6413{
6414 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
6415 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
6416 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6417 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6418 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6419 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6420 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6421 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6422 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6423 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6424 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6425 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6426 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6427 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6428 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6429 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
6430 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
6431 /* Exclude lfqu by testing reloc. If relocs are ever
6432 defined for the reduced D field in psq_lu then those
6433 will need testing too. */
6434 && r_type != elfcpp::R_PPC64_TOC16_LO
6435 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6436 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
6437 && (insn & 1) == 0)
6438 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
6439 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
6440 /* Exclude stfqu. psq_stu as above for psq_lu. */
6441 && r_type != elfcpp::R_PPC64_TOC16_LO
6442 && r_type != elfcpp::R_POWERPC_GOT16_LO)
6443 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
6444 && (insn & 1) == 0));
6445}
6446
42cacb20
DE
6447// Scan a relocation for a local symbol.
6448
6449template<int size, bool big_endian>
6450inline void
6451Target_powerpc<size, big_endian>::Scan::local(
d83ce4e3
AM
6452 Symbol_table* symtab,
6453 Layout* layout,
6454 Target_powerpc<size, big_endian>* target,
6455 Sized_relobj_file<size, big_endian>* object,
6456 unsigned int data_shndx,
6457 Output_section* output_section,
6458 const elfcpp::Rela<size, big_endian>& reloc,
6459 unsigned int r_type,
e5d5f5ed 6460 const elfcpp::Sym<size, big_endian>& lsym,
bfdfa4cd 6461 bool is_discarded)
42cacb20 6462{
34e0882b 6463 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
e3deeb9c
AM
6464
6465 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6466 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6467 {
6468 this->expect_tls_get_addr_call();
6469 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6470 if (tls_type != tls::TLSOPT_NONE)
6471 this->skip_next_tls_get_addr_call();
6472 }
6473 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6474 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6475 {
6476 this->expect_tls_get_addr_call();
6477 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6478 if (tls_type != tls::TLSOPT_NONE)
6479 this->skip_next_tls_get_addr_call();
6480 }
6481
dd93cd0a
AM
6482 Powerpc_relobj<size, big_endian>* ppc_object
6483 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6484
bfdfa4cd
AM
6485 if (is_discarded)
6486 {
6487 if (size == 64
6488 && data_shndx == ppc_object->opd_shndx()
6489 && r_type == elfcpp::R_PPC64_ADDR64)
6490 ppc_object->set_opd_discard(reloc.get_r_offset());
6491 return;
6492 }
6493
e5d5f5ed
AM
6494 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6495 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
9055360d 6496 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
40b469d7 6497 {
ec661b9d
AM
6498 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6499 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6500 r_type, r_sym, reloc.get_r_addend());
6501 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
40b469d7 6502 }
e5d5f5ed 6503
42cacb20
DE
6504 switch (r_type)
6505 {
6506 case elfcpp::R_POWERPC_NONE:
6507 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6508 case elfcpp::R_POWERPC_GNU_VTENTRY:
7404fe1b 6509 case elfcpp::R_POWERPC_TLS:
549dba71 6510 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
6511 break;
6512
6513 case elfcpp::R_PPC64_TOC:
6514 {
6515 Output_data_got_powerpc<size, big_endian>* got
6516 = target->got_section(symtab, layout);
6517 if (parameters->options().output_is_position_independent())
6518 {
bfdfa4cd
AM
6519 Address off = reloc.get_r_offset();
6520 if (size == 64
9055360d 6521 && target->abiversion() < 2
bfdfa4cd
AM
6522 && data_shndx == ppc_object->opd_shndx()
6523 && ppc_object->get_opd_discard(off - 8))
6524 break;
6525
dd93cd0a 6526 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
bfdfa4cd 6527 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
dd93cd0a
AM
6528 rela_dyn->add_output_section_relative(got->output_section(),
6529 elfcpp::R_POWERPC_RELATIVE,
6530 output_section,
bfdfa4cd
AM
6531 object, data_shndx, off,
6532 symobj->toc_base_offset());
dd93cd0a
AM
6533 }
6534 }
42cacb20
DE
6535 break;
6536
6537 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 6538 case elfcpp::R_PPC64_UADDR64:
42cacb20 6539 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
6540 case elfcpp::R_POWERPC_UADDR32:
6541 case elfcpp::R_POWERPC_ADDR24:
c9269dff 6542 case elfcpp::R_POWERPC_ADDR16:
42cacb20 6543 case elfcpp::R_POWERPC_ADDR16_LO:
c9269dff
AM
6544 case elfcpp::R_POWERPC_ADDR16_HI:
6545 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 6546 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
6547 case elfcpp::R_PPC64_ADDR16_HIGH:
6548 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
6549 case elfcpp::R_PPC64_ADDR16_HIGHER:
6550 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6551 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6552 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6553 case elfcpp::R_PPC64_ADDR16_DS:
6554 case elfcpp::R_PPC64_ADDR16_LO_DS:
6555 case elfcpp::R_POWERPC_ADDR14:
6556 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6557 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20
DE
6558 // If building a shared library (or a position-independent
6559 // executable), we need to create a dynamic relocation for
6560 // this location.
c9824451 6561 if (parameters->options().output_is_position_independent()
9055360d 6562 || (size == 64 && is_ifunc && target->abiversion() < 2))
2e702c99 6563 {
b3ccdeb5
AM
6564 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6565 is_ifunc);
1f98a074 6566 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
dd93cd0a
AM
6567 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6568 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2e702c99 6569 {
b3ccdeb5
AM
6570 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6571 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 6572 rela_dyn->add_local_relative(object, r_sym, dynrel,
dd93cd0a
AM
6573 output_section, data_shndx,
6574 reloc.get_r_offset(),
c9824451 6575 reloc.get_r_addend(), false);
2e702c99 6576 }
1f98a074 6577 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
2e702c99 6578 {
dd93cd0a 6579 check_non_pic(object, r_type);
dd93cd0a
AM
6580 rela_dyn->add_local(object, r_sym, r_type, output_section,
6581 data_shndx, reloc.get_r_offset(),
6582 reloc.get_r_addend());
2e702c99 6583 }
1f98a074
AM
6584 else
6585 {
6586 gold_assert(lsym.get_st_value() == 0);
6587 unsigned int shndx = lsym.get_st_shndx();
6588 bool is_ordinary;
6589 shndx = object->adjust_sym_shndx(r_sym, shndx,
6590 &is_ordinary);
6591 if (!is_ordinary)
6592 object->error(_("section symbol %u has bad shndx %u"),
6593 r_sym, shndx);
6594 else
6595 rela_dyn->add_local_section(object, shndx, r_type,
6596 output_section, data_shndx,
6597 reloc.get_r_offset());
6598 }
2e702c99 6599 }
42cacb20
DE
6600 break;
6601
6602 case elfcpp::R_POWERPC_REL24:
c9824451 6603 case elfcpp::R_PPC_PLTREL24:
42cacb20 6604 case elfcpp::R_PPC_LOCAL24PC:
ec661b9d
AM
6605 case elfcpp::R_POWERPC_REL14:
6606 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6607 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 6608 if (!is_ifunc)
0e123f69
AM
6609 {
6610 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6611 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6612 r_type, r_sym, reloc.get_r_addend());
6613 }
ec661b9d
AM
6614 break;
6615
7e57d19e
AM
6616 case elfcpp::R_PPC64_TOCSAVE:
6617 // R_PPC64_TOCSAVE follows a call instruction to indicate the
6618 // caller has already saved r2 and thus a plt call stub need not
6619 // save r2.
6620 if (size == 64
6621 && target->mark_pltcall(ppc_object, data_shndx,
6622 reloc.get_r_offset() - 4, symtab))
6623 {
6624 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6625 unsigned int shndx = lsym.get_st_shndx();
6626 bool is_ordinary;
6627 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6628 if (!is_ordinary)
6629 object->error(_("tocsave symbol %u has bad shndx %u"),
6630 r_sym, shndx);
6631 else
6632 target->add_tocsave(ppc_object, shndx,
6633 lsym.get_st_value() + reloc.get_r_addend());
6634 }
6635 break;
6636
ec661b9d
AM
6637 case elfcpp::R_PPC64_REL64:
6638 case elfcpp::R_POWERPC_REL32:
dd93cd0a 6639 case elfcpp::R_POWERPC_REL16:
6ce78956 6640 case elfcpp::R_POWERPC_REL16_LO:
dd93cd0a 6641 case elfcpp::R_POWERPC_REL16_HI:
6ce78956 6642 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 6643 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 6644 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 6645 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 6646 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 6647 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
6648 case elfcpp::R_PPC64_SECTOFF_DS:
6649 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6650 case elfcpp::R_POWERPC_TPREL16:
6651 case elfcpp::R_POWERPC_TPREL16_LO:
6652 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 6653 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
6654 case elfcpp::R_PPC64_TPREL16_DS:
6655 case elfcpp::R_PPC64_TPREL16_LO_DS:
6656 case elfcpp::R_PPC64_TPREL16_HIGH:
6657 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 6658 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 6659 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 6660 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 6661 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
6662 case elfcpp::R_POWERPC_DTPREL16:
6663 case elfcpp::R_POWERPC_DTPREL16_LO:
6664 case elfcpp::R_POWERPC_DTPREL16_HI:
6665 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
6666 case elfcpp::R_PPC64_DTPREL16_DS:
6667 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
6668 case elfcpp::R_PPC64_DTPREL16_HIGH:
6669 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6670 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6671 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6672 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6673 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
6674 case elfcpp::R_PPC64_TLSGD:
6675 case elfcpp::R_PPC64_TLSLD:
45965137 6676 case elfcpp::R_PPC64_ADDR64_LOCAL:
42cacb20
DE
6677 break;
6678
6679 case elfcpp::R_POWERPC_GOT16:
6680 case elfcpp::R_POWERPC_GOT16_LO:
6681 case elfcpp::R_POWERPC_GOT16_HI:
6682 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
6683 case elfcpp::R_PPC64_GOT16_DS:
6684 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 6685 {
c9269dff 6686 // The symbol requires a GOT entry.
dd93cd0a
AM
6687 Output_data_got_powerpc<size, big_endian>* got
6688 = target->got_section(symtab, layout);
6689 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
42cacb20 6690
e5d5f5ed 6691 if (!parameters->options().output_is_position_independent())
42cacb20 6692 {
b01a4b04
AM
6693 if (is_ifunc
6694 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
6695 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6696 else
6697 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6698 }
6699 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6700 {
6701 // If we are generating a shared object or a pie, this
6702 // symbol's GOT entry will be set by a dynamic relocation.
6703 unsigned int off;
6704 off = got->add_constant(0);
6705 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
42cacb20 6706
b3ccdeb5
AM
6707 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6708 is_ifunc);
6709 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6710 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed 6711 rela_dyn->add_local_relative(object, r_sym, dynrel,
c9824451 6712 got, off, 0, false);
2e702c99 6713 }
42cacb20
DE
6714 }
6715 break;
6716
cf43a2fe
AM
6717 case elfcpp::R_PPC64_TOC16:
6718 case elfcpp::R_PPC64_TOC16_LO:
6719 case elfcpp::R_PPC64_TOC16_HI:
6720 case elfcpp::R_PPC64_TOC16_HA:
6721 case elfcpp::R_PPC64_TOC16_DS:
6722 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
6723 // We need a GOT section.
6724 target->got_section(symtab, layout);
6725 break;
6726
dd93cd0a
AM
6727 case elfcpp::R_POWERPC_GOT_TLSGD16:
6728 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6729 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6730 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6731 {
6732 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6733 if (tls_type == tls::TLSOPT_NONE)
6734 {
6735 Output_data_got_powerpc<size, big_endian>* got
6736 = target->got_section(symtab, layout);
6737 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d
AM
6738 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6739 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6740 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
dd93cd0a
AM
6741 }
6742 else if (tls_type == tls::TLSOPT_TO_LE)
6743 {
6744 // no GOT relocs needed for Local Exec.
6745 }
6746 else
6747 gold_unreachable();
6748 }
42cacb20
DE
6749 break;
6750
dd93cd0a
AM
6751 case elfcpp::R_POWERPC_GOT_TLSLD16:
6752 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6753 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6754 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6755 {
6756 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6757 if (tls_type == tls::TLSOPT_NONE)
6758 target->tlsld_got_offset(symtab, layout, object);
6759 else if (tls_type == tls::TLSOPT_TO_LE)
6760 {
6761 // no GOT relocs needed for Local Exec.
7404fe1b
AM
6762 if (parameters->options().emit_relocs())
6763 {
6764 Output_section* os = layout->tls_segment()->first_section();
6765 gold_assert(os != NULL);
6766 os->set_needs_symtab_index();
6767 }
dd93cd0a
AM
6768 }
6769 else
6770 gold_unreachable();
6771 }
42cacb20 6772 break;
42cacb20 6773
dd93cd0a
AM
6774 case elfcpp::R_POWERPC_GOT_DTPREL16:
6775 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6776 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6777 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6778 {
6779 Output_data_got_powerpc<size, big_endian>* got
6780 = target->got_section(symtab, layout);
6781 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
bd73a62d 6782 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
dd93cd0a
AM
6783 }
6784 break;
42cacb20 6785
dd93cd0a
AM
6786 case elfcpp::R_POWERPC_GOT_TPREL16:
6787 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6788 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6789 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6790 {
6791 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6792 if (tls_type == tls::TLSOPT_NONE)
6793 {
dd93cd0a 6794 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
acc276d8
AM
6795 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6796 {
6797 Output_data_got_powerpc<size, big_endian>* got
6798 = target->got_section(symtab, layout);
6799 unsigned int off = got->add_constant(0);
6800 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6801
6802 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6803 rela_dyn->add_symbolless_local_addend(object, r_sym,
6804 elfcpp::R_POWERPC_TPREL,
6805 got, off, 0);
6806 }
dd93cd0a
AM
6807 }
6808 else if (tls_type == tls::TLSOPT_TO_LE)
6809 {
6810 // no GOT relocs needed for Local Exec.
6811 }
6812 else
6813 gold_unreachable();
6814 }
6815 break;
6816
6817 default:
6818 unsupported_reloc_local(object, r_type);
6819 break;
6820 }
d8f5a274 6821
5edad15d
AM
6822 if (size == 64
6823 && parameters->options().toc_optimize())
6824 {
6825 if (data_shndx == ppc_object->toc_shndx())
6826 {
6827 bool ok = true;
6828 if (r_type != elfcpp::R_PPC64_ADDR64
6829 || (is_ifunc && target->abiversion() < 2))
6830 ok = false;
6831 else if (parameters->options().output_is_position_independent())
6832 {
6833 if (is_ifunc)
6834 ok = false;
6835 else
6836 {
6837 unsigned int shndx = lsym.get_st_shndx();
6838 if (shndx >= elfcpp::SHN_LORESERVE
6839 && shndx != elfcpp::SHN_XINDEX)
6840 ok = false;
6841 }
6842 }
6843 if (!ok)
6844 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6845 }
6846
6847 enum {no_check, check_lo, check_ha} insn_check;
6848 switch (r_type)
6849 {
6850 default:
6851 insn_check = no_check;
6852 break;
6853
6854 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6855 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6856 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6857 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6858 case elfcpp::R_POWERPC_GOT16_HA:
6859 case elfcpp::R_PPC64_TOC16_HA:
6860 insn_check = check_ha;
6861 break;
6862
6863 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6864 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6865 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6866 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6867 case elfcpp::R_POWERPC_GOT16_LO:
6868 case elfcpp::R_PPC64_GOT16_LO_DS:
6869 case elfcpp::R_PPC64_TOC16_LO:
6870 case elfcpp::R_PPC64_TOC16_LO_DS:
6871 insn_check = check_lo;
6872 break;
6873 }
6874
6875 section_size_type slen;
6876 const unsigned char* view = NULL;
6877 if (insn_check != no_check)
6878 {
6879 view = ppc_object->section_contents(data_shndx, &slen, false);
6880 section_size_type off =
6881 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6882 if (off < slen)
6883 {
6884 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6885 if (insn_check == check_lo
6886 ? !ok_lo_toc_insn(insn, r_type)
6887 : ((insn & ((0x3f << 26) | 0x1f << 16))
6888 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6889 {
6890 ppc_object->set_no_toc_opt();
6891 gold_warning(_("%s: toc optimization is not supported "
6892 "for %#08x instruction"),
6893 ppc_object->name().c_str(), insn);
6894 }
6895 }
6896 }
6897
6898 switch (r_type)
6899 {
6900 default:
6901 break;
6902 case elfcpp::R_PPC64_TOC16:
6903 case elfcpp::R_PPC64_TOC16_LO:
6904 case elfcpp::R_PPC64_TOC16_HI:
6905 case elfcpp::R_PPC64_TOC16_HA:
6906 case elfcpp::R_PPC64_TOC16_DS:
6907 case elfcpp::R_PPC64_TOC16_LO_DS:
6908 unsigned int shndx = lsym.get_st_shndx();
6909 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6910 bool is_ordinary;
6911 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6912 if (is_ordinary && shndx == ppc_object->toc_shndx())
6913 {
6914 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6915 if (dst_off < ppc_object->section_size(shndx))
6916 {
6917 bool ok = false;
6918 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6919 ok = true;
6920 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6921 {
6922 // Need to check that the insn is a ld
6923 if (!view)
6924 view = ppc_object->section_contents(data_shndx,
6925 &slen,
6926 false);
6927 section_size_type off =
6928 (convert_to_section_size_type(reloc.get_r_offset())
6929 + (big_endian ? -2 : 3));
6930 if (off < slen
6931 && (view[off] & (0x3f << 2)) == 58u << 2)
6932 ok = true;
6933 }
6934 if (!ok)
6935 ppc_object->set_no_toc_opt(dst_off);
6936 }
6937 }
6938 break;
6939 }
6940 }
6941
f159cdb6
AM
6942 if (size == 32)
6943 {
6944 switch (r_type)
6945 {
6946 case elfcpp::R_POWERPC_REL32:
6947 if (ppc_object->got2_shndx() != 0
6948 && parameters->options().output_is_position_independent())
6949 {
6950 unsigned int shndx = lsym.get_st_shndx();
6951 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6952 bool is_ordinary;
6953 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6954 if (is_ordinary && shndx == ppc_object->got2_shndx()
6955 && (ppc_object->section_flags(data_shndx)
6956 & elfcpp::SHF_EXECINSTR) != 0)
6957 gold_error(_("%s: unsupported -mbss-plt code"),
6958 ppc_object->name().c_str());
6959 }
6960 break;
6961 default:
6962 break;
6963 }
6964 }
6965
d8f5a274
AM
6966 switch (r_type)
6967 {
6968 case elfcpp::R_POWERPC_GOT_TLSLD16:
6969 case elfcpp::R_POWERPC_GOT_TLSGD16:
6970 case elfcpp::R_POWERPC_GOT_TPREL16:
6971 case elfcpp::R_POWERPC_GOT_DTPREL16:
6972 case elfcpp::R_POWERPC_GOT16:
6973 case elfcpp::R_PPC64_GOT16_DS:
6974 case elfcpp::R_PPC64_TOC16:
6975 case elfcpp::R_PPC64_TOC16_DS:
6976 ppc_object->set_has_small_toc_reloc();
6977 default:
6978 break;
6979 }
dd93cd0a
AM
6980}
6981
6982// Report an unsupported relocation against a global symbol.
6983
6984template<int size, bool big_endian>
6985void
6986Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6987 Sized_relobj_file<size, big_endian>* object,
6988 unsigned int r_type,
6989 Symbol* gsym)
6990{
42cacb20
DE
6991 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6992 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6993}
6994
6995// Scan a relocation for a global symbol.
6996
6997template<int size, bool big_endian>
6998inline void
6999Target_powerpc<size, big_endian>::Scan::global(
d83ce4e3
AM
7000 Symbol_table* symtab,
7001 Layout* layout,
7002 Target_powerpc<size, big_endian>* target,
7003 Sized_relobj_file<size, big_endian>* object,
7004 unsigned int data_shndx,
7005 Output_section* output_section,
7006 const elfcpp::Rela<size, big_endian>& reloc,
7007 unsigned int r_type,
7008 Symbol* gsym)
42cacb20 7009{
34e0882b
AM
7010 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
7011 == Track_tls::SKIP)
e3deeb9c
AM
7012 return;
7013
34e0882b
AM
7014 if (target->replace_tls_get_addr(gsym))
7015 // Change a __tls_get_addr reference to __tls_get_addr_opt
7016 // so dynamic relocs are emitted against the latter symbol.
7017 gsym = target->tls_get_addr_opt();
7018
e3deeb9c
AM
7019 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7020 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7021 {
7022 this->expect_tls_get_addr_call();
7023 const bool final = gsym->final_value_is_known();
7024 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7025 if (tls_type != tls::TLSOPT_NONE)
7026 this->skip_next_tls_get_addr_call();
7027 }
7028 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7029 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7030 {
7031 this->expect_tls_get_addr_call();
7032 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7033 if (tls_type != tls::TLSOPT_NONE)
7034 this->skip_next_tls_get_addr_call();
7035 }
7036
dd93cd0a
AM
7037 Powerpc_relobj<size, big_endian>* ppc_object
7038 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7039
e5d5f5ed 7040 // A STT_GNU_IFUNC symbol may require a PLT entry.
b3ccdeb5 7041 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
9055360d
AM
7042 bool pushed_ifunc = false;
7043 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
ec661b9d 7044 {
0e123f69 7045 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
ec661b9d 7046 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 7047 r_type, r_sym, reloc.get_r_addend());
ec661b9d 7048 target->make_plt_entry(symtab, layout, gsym);
9055360d 7049 pushed_ifunc = true;
ec661b9d 7050 }
e5d5f5ed 7051
42cacb20
DE
7052 switch (r_type)
7053 {
7054 case elfcpp::R_POWERPC_NONE:
7055 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7056 case elfcpp::R_POWERPC_GNU_VTENTRY:
cf43a2fe 7057 case elfcpp::R_PPC_LOCAL24PC:
7404fe1b 7058 case elfcpp::R_POWERPC_TLS:
549dba71 7059 case elfcpp::R_PPC64_ENTRY:
dd93cd0a
AM
7060 break;
7061
7062 case elfcpp::R_PPC64_TOC:
7063 {
7064 Output_data_got_powerpc<size, big_endian>* got
7065 = target->got_section(symtab, layout);
7066 if (parameters->options().output_is_position_independent())
7067 {
bfdfa4cd
AM
7068 Address off = reloc.get_r_offset();
7069 if (size == 64
7070 && data_shndx == ppc_object->opd_shndx()
7071 && ppc_object->get_opd_discard(off - 8))
7072 break;
7073
dd93cd0a
AM
7074 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7075 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7076 if (data_shndx != ppc_object->opd_shndx())
7077 symobj = static_cast
7078 <Powerpc_relobj<size, big_endian>*>(gsym->object());
7079 rela_dyn->add_output_section_relative(got->output_section(),
7080 elfcpp::R_POWERPC_RELATIVE,
7081 output_section,
bfdfa4cd 7082 object, data_shndx, off,
dd93cd0a
AM
7083 symobj->toc_base_offset());
7084 }
7085 }
42cacb20
DE
7086 break;
7087
c9269dff 7088 case elfcpp::R_PPC64_ADDR64:
bfdfa4cd 7089 if (size == 64
9055360d 7090 && target->abiversion() < 2
bfdfa4cd
AM
7091 && data_shndx == ppc_object->opd_shndx()
7092 && (gsym->is_defined_in_discarded_section()
7093 || gsym->object() != object))
7094 {
7095 ppc_object->set_opd_discard(reloc.get_r_offset());
7096 break;
7097 }
d8e90251 7098 // Fall through.
dd93cd0a 7099 case elfcpp::R_PPC64_UADDR64:
c9269dff 7100 case elfcpp::R_POWERPC_ADDR32:
dd93cd0a
AM
7101 case elfcpp::R_POWERPC_UADDR32:
7102 case elfcpp::R_POWERPC_ADDR24:
42cacb20
DE
7103 case elfcpp::R_POWERPC_ADDR16:
7104 case elfcpp::R_POWERPC_ADDR16_LO:
7105 case elfcpp::R_POWERPC_ADDR16_HI:
7106 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 7107 case elfcpp::R_POWERPC_UADDR16:
f9c6b907
AM
7108 case elfcpp::R_PPC64_ADDR16_HIGH:
7109 case elfcpp::R_PPC64_ADDR16_HIGHA:
dd93cd0a
AM
7110 case elfcpp::R_PPC64_ADDR16_HIGHER:
7111 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7112 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7113 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7114 case elfcpp::R_PPC64_ADDR16_DS:
7115 case elfcpp::R_PPC64_ADDR16_LO_DS:
7116 case elfcpp::R_POWERPC_ADDR14:
7117 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7118 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
42cacb20 7119 {
c9269dff
AM
7120 // Make a PLT entry if necessary.
7121 if (gsym->needs_plt_entry())
7122 {
9055360d
AM
7123 // Since this is not a PC-relative relocation, we may be
7124 // taking the address of a function. In that case we need to
7125 // set the entry in the dynamic symbol table to the address of
7126 // the PLT call stub.
7127 bool need_ifunc_plt = false;
7128 if ((size == 32 || target->abiversion() >= 2)
7129 && gsym->is_from_dynobj()
7130 && !parameters->options().output_is_position_independent())
7131 {
7132 gsym->set_needs_dynsym_value();
7133 need_ifunc_plt = true;
7134 }
7135 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
b3ccdeb5 7136 {
0e123f69 7137 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 7138 target->push_branch(ppc_object, data_shndx,
0e123f69 7139 reloc.get_r_offset(), r_type, r_sym,
b3ccdeb5
AM
7140 reloc.get_r_addend());
7141 target->make_plt_entry(symtab, layout, gsym);
7142 }
c9269dff
AM
7143 }
7144 // Make a dynamic relocation if necessary.
88b8e639 7145 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
9055360d 7146 || (size == 64 && is_ifunc && target->abiversion() < 2))
c9269dff 7147 {
a82bef93
ST
7148 if (!parameters->options().output_is_position_independent()
7149 && gsym->may_need_copy_reloc())
c9269dff
AM
7150 {
7151 target->copy_reloc(symtab, layout, object,
7152 data_shndx, output_section, gsym, reloc);
7153 }
9055360d
AM
7154 else if ((((size == 32
7155 && r_type == elfcpp::R_POWERPC_ADDR32)
7156 || (size == 64
7157 && r_type == elfcpp::R_PPC64_ADDR64
7158 && target->abiversion() >= 2))
627b30b7
AM
7159 && gsym->can_use_relative_reloc(false)
7160 && !(gsym->visibility() == elfcpp::STV_PROTECTED
7161 && parameters->options().shared()))
7162 || (size == 64
7163 && r_type == elfcpp::R_PPC64_ADDR64
9055360d 7164 && target->abiversion() < 2
627b30b7
AM
7165 && (gsym->can_use_relative_reloc(false)
7166 || data_shndx == ppc_object->opd_shndx())))
2e702c99 7167 {
b3ccdeb5
AM
7168 Reloc_section* rela_dyn
7169 = target->rela_dyn_section(symtab, layout, is_ifunc);
7170 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7171 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
7172 rela_dyn->add_symbolless_global_addend(
7173 gsym, dynrel, output_section, object, data_shndx,
7174 reloc.get_r_offset(), reloc.get_r_addend());
2e702c99
RM
7175 }
7176 else
7177 {
b3ccdeb5
AM
7178 Reloc_section* rela_dyn
7179 = target->rela_dyn_section(symtab, layout, is_ifunc);
42cacb20 7180 check_non_pic(object, r_type);
dd93cd0a
AM
7181 rela_dyn->add_global(gsym, r_type, output_section,
7182 object, data_shndx,
7183 reloc.get_r_offset(),
7184 reloc.get_r_addend());
5edad15d
AM
7185
7186 if (size == 64
7187 && parameters->options().toc_optimize()
7188 && data_shndx == ppc_object->toc_shndx())
7189 ppc_object->set_no_toc_opt(reloc.get_r_offset());
2e702c99
RM
7190 }
7191 }
42cacb20
DE
7192 }
7193 break;
7194
cf43a2fe 7195 case elfcpp::R_PPC_PLTREL24:
42cacb20 7196 case elfcpp::R_POWERPC_REL24:
b3ccdeb5
AM
7197 if (!is_ifunc)
7198 {
0e123f69 7199 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
b3ccdeb5 7200 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
0e123f69 7201 r_type, r_sym, reloc.get_r_addend());
b3ccdeb5
AM
7202 if (gsym->needs_plt_entry()
7203 || (!gsym->final_value_is_known()
7204 && (gsym->is_undefined()
7205 || gsym->is_from_dynobj()
7206 || gsym->is_preemptible())))
7207 target->make_plt_entry(symtab, layout, gsym);
7208 }
d8e90251 7209 // Fall through.
42cacb20 7210
3ea0a085 7211 case elfcpp::R_PPC64_REL64:
dd93cd0a 7212 case elfcpp::R_POWERPC_REL32:
3ea0a085 7213 // Make a dynamic relocation if necessary.
88b8e639 7214 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
3ea0a085 7215 {
a82bef93
ST
7216 if (!parameters->options().output_is_position_independent()
7217 && gsym->may_need_copy_reloc())
3ea0a085
AM
7218 {
7219 target->copy_reloc(symtab, layout, object,
7220 data_shndx, output_section, gsym,
7221 reloc);
7222 }
7223 else
7224 {
b3ccdeb5
AM
7225 Reloc_section* rela_dyn
7226 = target->rela_dyn_section(symtab, layout, is_ifunc);
3ea0a085
AM
7227 check_non_pic(object, r_type);
7228 rela_dyn->add_global(gsym, r_type, output_section, object,
7229 data_shndx, reloc.get_r_offset(),
7230 reloc.get_r_addend());
7231 }
7232 }
7233 break;
7234
ec661b9d
AM
7235 case elfcpp::R_POWERPC_REL14:
7236 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7237 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
b3ccdeb5 7238 if (!is_ifunc)
0e123f69
AM
7239 {
7240 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7241 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7242 r_type, r_sym, reloc.get_r_addend());
7243 }
ec661b9d
AM
7244 break;
7245
7e57d19e
AM
7246 case elfcpp::R_PPC64_TOCSAVE:
7247 // R_PPC64_TOCSAVE follows a call instruction to indicate the
7248 // caller has already saved r2 and thus a plt call stub need not
7249 // save r2.
7250 if (size == 64
7251 && target->mark_pltcall(ppc_object, data_shndx,
7252 reloc.get_r_offset() - 4, symtab))
7253 {
7254 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7255 bool is_ordinary;
7256 unsigned int shndx = gsym->shndx(&is_ordinary);
7257 if (!is_ordinary)
7258 object->error(_("tocsave symbol %u has bad shndx %u"),
7259 r_sym, shndx);
7260 else
7261 {
7262 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7263 target->add_tocsave(ppc_object, shndx,
7264 sym->value() + reloc.get_r_addend());
7265 }
7266 }
7267 break;
7268
6ce78956
AM
7269 case elfcpp::R_POWERPC_REL16:
7270 case elfcpp::R_POWERPC_REL16_LO:
7271 case elfcpp::R_POWERPC_REL16_HI:
7272 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 7273 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 7274 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a 7275 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a 7276 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a 7277 case elfcpp::R_POWERPC_SECTOFF_HA:
f9c6b907
AM
7278 case elfcpp::R_PPC64_SECTOFF_DS:
7279 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7280 case elfcpp::R_POWERPC_TPREL16:
7281 case elfcpp::R_POWERPC_TPREL16_LO:
7282 case elfcpp::R_POWERPC_TPREL16_HI:
dd93cd0a 7283 case elfcpp::R_POWERPC_TPREL16_HA:
f9c6b907
AM
7284 case elfcpp::R_PPC64_TPREL16_DS:
7285 case elfcpp::R_PPC64_TPREL16_LO_DS:
7286 case elfcpp::R_PPC64_TPREL16_HIGH:
7287 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 7288 case elfcpp::R_PPC64_TPREL16_HIGHER:
dd93cd0a 7289 case elfcpp::R_PPC64_TPREL16_HIGHERA:
dd93cd0a 7290 case elfcpp::R_PPC64_TPREL16_HIGHEST:
dd93cd0a 7291 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
7292 case elfcpp::R_POWERPC_DTPREL16:
7293 case elfcpp::R_POWERPC_DTPREL16_LO:
7294 case elfcpp::R_POWERPC_DTPREL16_HI:
7295 case elfcpp::R_POWERPC_DTPREL16_HA:
dd93cd0a
AM
7296 case elfcpp::R_PPC64_DTPREL16_DS:
7297 case elfcpp::R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
7298 case elfcpp::R_PPC64_DTPREL16_HIGH:
7299 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7300 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7301 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7302 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7303 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
dd93cd0a
AM
7304 case elfcpp::R_PPC64_TLSGD:
7305 case elfcpp::R_PPC64_TLSLD:
45965137 7306 case elfcpp::R_PPC64_ADDR64_LOCAL:
cf43a2fe
AM
7307 break;
7308
42cacb20
DE
7309 case elfcpp::R_POWERPC_GOT16:
7310 case elfcpp::R_POWERPC_GOT16_LO:
7311 case elfcpp::R_POWERPC_GOT16_HI:
7312 case elfcpp::R_POWERPC_GOT16_HA:
dd93cd0a
AM
7313 case elfcpp::R_PPC64_GOT16_DS:
7314 case elfcpp::R_PPC64_GOT16_LO_DS:
42cacb20 7315 {
c9269dff
AM
7316 // The symbol requires a GOT entry.
7317 Output_data_got_powerpc<size, big_endian>* got;
42cacb20
DE
7318
7319 got = target->got_section(symtab, layout);
2e702c99 7320 if (gsym->final_value_is_known())
2e702c99 7321 {
b01a4b04
AM
7322 if (is_ifunc
7323 && (size == 32 || target->abiversion() >= 2))
e5d5f5ed
AM
7324 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
7325 else
7326 got->add_global(gsym, GOT_TYPE_STANDARD);
7327 }
7328 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
7329 {
7330 // If we are generating a shared object or a pie, this
7331 // symbol's GOT entry will be set by a dynamic relocation.
7332 unsigned int off = got->add_constant(0);
7333 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
7334
b3ccdeb5
AM
7335 Reloc_section* rela_dyn
7336 = target->rela_dyn_section(symtab, layout, is_ifunc);
7337
e5d5f5ed 7338 if (gsym->can_use_relative_reloc(false)
9055360d
AM
7339 && !((size == 32
7340 || target->abiversion() >= 2)
e5d5f5ed
AM
7341 && gsym->visibility() == elfcpp::STV_PROTECTED
7342 && parameters->options().shared()))
2e702c99 7343 {
b3ccdeb5
AM
7344 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7345 : elfcpp::R_POWERPC_RELATIVE);
e5d5f5ed
AM
7346 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
7347 }
7348 else
7349 {
7350 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
7351 rela_dyn->add_global(gsym, dynrel, got, off, 0);
42cacb20 7352 }
2e702c99 7353 }
42cacb20
DE
7354 }
7355 break;
7356
cf43a2fe
AM
7357 case elfcpp::R_PPC64_TOC16:
7358 case elfcpp::R_PPC64_TOC16_LO:
7359 case elfcpp::R_PPC64_TOC16_HI:
7360 case elfcpp::R_PPC64_TOC16_HA:
7361 case elfcpp::R_PPC64_TOC16_DS:
7362 case elfcpp::R_PPC64_TOC16_LO_DS:
42cacb20
DE
7363 // We need a GOT section.
7364 target->got_section(symtab, layout);
7365 break;
7366
dd93cd0a
AM
7367 case elfcpp::R_POWERPC_GOT_TLSGD16:
7368 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7369 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7370 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7371 {
7372 const bool final = gsym->final_value_is_known();
7373 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7374 if (tls_type == tls::TLSOPT_NONE)
7375 {
7376 Output_data_got_powerpc<size, big_endian>* got
7377 = target->got_section(symtab, layout);
b3ccdeb5
AM
7378 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7379 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
dd93cd0a
AM
7380 elfcpp::R_POWERPC_DTPMOD,
7381 elfcpp::R_POWERPC_DTPREL);
7382 }
7383 else if (tls_type == tls::TLSOPT_TO_IE)
7384 {
acc276d8
AM
7385 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7386 {
7387 Output_data_got_powerpc<size, big_endian>* got
7388 = target->got_section(symtab, layout);
7389 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7390 if (gsym->is_undefined()
7391 || gsym->is_from_dynobj())
7392 {
7393 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7394 elfcpp::R_POWERPC_TPREL);
7395 }
7396 else
7397 {
7398 unsigned int off = got->add_constant(0);
7399 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7400 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7401 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7402 got, off, 0);
7403 }
7404 }
dd93cd0a
AM
7405 }
7406 else if (tls_type == tls::TLSOPT_TO_LE)
7407 {
7408 // no GOT relocs needed for Local Exec.
7409 }
7410 else
7411 gold_unreachable();
7412 }
42cacb20
DE
7413 break;
7414
dd93cd0a
AM
7415 case elfcpp::R_POWERPC_GOT_TLSLD16:
7416 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7417 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7418 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7419 {
7420 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7421 if (tls_type == tls::TLSOPT_NONE)
7422 target->tlsld_got_offset(symtab, layout, object);
7423 else if (tls_type == tls::TLSOPT_TO_LE)
7424 {
7425 // no GOT relocs needed for Local Exec.
7404fe1b
AM
7426 if (parameters->options().emit_relocs())
7427 {
7428 Output_section* os = layout->tls_segment()->first_section();
7429 gold_assert(os != NULL);
7430 os->set_needs_symtab_index();
7431 }
dd93cd0a
AM
7432 }
7433 else
7434 gold_unreachable();
7435 }
7436 break;
7437
7438 case elfcpp::R_POWERPC_GOT_DTPREL16:
7439 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7440 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7441 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7442 {
7443 Output_data_got_powerpc<size, big_endian>* got
7444 = target->got_section(symtab, layout);
bd73a62d
AM
7445 if (!gsym->final_value_is_known()
7446 && (gsym->is_from_dynobj()
7447 || gsym->is_undefined()
7448 || gsym->is_preemptible()))
7449 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
7450 target->rela_dyn_section(layout),
7451 elfcpp::R_POWERPC_DTPREL);
7452 else
7453 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
dd93cd0a
AM
7454 }
7455 break;
7456
7457 case elfcpp::R_POWERPC_GOT_TPREL16:
7458 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7459 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7460 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7461 {
7462 const bool final = gsym->final_value_is_known();
7463 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7464 if (tls_type == tls::TLSOPT_NONE)
7465 {
acc276d8
AM
7466 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
7467 {
7468 Output_data_got_powerpc<size, big_endian>* got
7469 = target->got_section(symtab, layout);
7470 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7471 if (gsym->is_undefined()
7472 || gsym->is_from_dynobj())
7473 {
7474 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
7475 elfcpp::R_POWERPC_TPREL);
7476 }
7477 else
7478 {
7479 unsigned int off = got->add_constant(0);
7480 gsym->set_got_offset(GOT_TYPE_TPREL, off);
7481 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
7482 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
7483 got, off, 0);
7484 }
7485 }
dd93cd0a
AM
7486 }
7487 else if (tls_type == tls::TLSOPT_TO_LE)
7488 {
7489 // no GOT relocs needed for Local Exec.
7490 }
7491 else
7492 gold_unreachable();
7493 }
42cacb20
DE
7494 break;
7495
7496 default:
7497 unsupported_reloc_global(object, r_type, gsym);
7498 break;
7499 }
d8f5a274 7500
5edad15d
AM
7501 if (size == 64
7502 && parameters->options().toc_optimize())
7503 {
7504 if (data_shndx == ppc_object->toc_shndx())
7505 {
7506 bool ok = true;
7507 if (r_type != elfcpp::R_PPC64_ADDR64
7508 || (is_ifunc && target->abiversion() < 2))
7509 ok = false;
7510 else if (parameters->options().output_is_position_independent()
7511 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7512 ok = false;
7513 if (!ok)
7514 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7515 }
7516
7517 enum {no_check, check_lo, check_ha} insn_check;
7518 switch (r_type)
7519 {
7520 default:
7521 insn_check = no_check;
7522 break;
7523
7524 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7525 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7526 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7527 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7528 case elfcpp::R_POWERPC_GOT16_HA:
7529 case elfcpp::R_PPC64_TOC16_HA:
7530 insn_check = check_ha;
7531 break;
7532
7533 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7534 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7535 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7536 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7537 case elfcpp::R_POWERPC_GOT16_LO:
7538 case elfcpp::R_PPC64_GOT16_LO_DS:
7539 case elfcpp::R_PPC64_TOC16_LO:
7540 case elfcpp::R_PPC64_TOC16_LO_DS:
7541 insn_check = check_lo;
7542 break;
7543 }
7544
7545 section_size_type slen;
7546 const unsigned char* view = NULL;
7547 if (insn_check != no_check)
7548 {
7549 view = ppc_object->section_contents(data_shndx, &slen, false);
7550 section_size_type off =
7551 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7552 if (off < slen)
7553 {
7554 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7555 if (insn_check == check_lo
7556 ? !ok_lo_toc_insn(insn, r_type)
7557 : ((insn & ((0x3f << 26) | 0x1f << 16))
7558 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7559 {
7560 ppc_object->set_no_toc_opt();
7561 gold_warning(_("%s: toc optimization is not supported "
7562 "for %#08x instruction"),
7563 ppc_object->name().c_str(), insn);
7564 }
7565 }
7566 }
7567
7568 switch (r_type)
7569 {
7570 default:
7571 break;
7572 case elfcpp::R_PPC64_TOC16:
7573 case elfcpp::R_PPC64_TOC16_LO:
7574 case elfcpp::R_PPC64_TOC16_HI:
7575 case elfcpp::R_PPC64_TOC16_HA:
7576 case elfcpp::R_PPC64_TOC16_DS:
7577 case elfcpp::R_PPC64_TOC16_LO_DS:
7578 if (gsym->source() == Symbol::FROM_OBJECT
7579 && !gsym->object()->is_dynamic())
7580 {
7581 Powerpc_relobj<size, big_endian>* sym_object
7582 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7583 bool is_ordinary;
7584 unsigned int shndx = gsym->shndx(&is_ordinary);
7585 if (shndx == sym_object->toc_shndx())
7586 {
7587 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7588 Address dst_off = sym->value() + reloc.get_r_offset();
7589 if (dst_off < sym_object->section_size(shndx))
7590 {
7591 bool ok = false;
7592 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7593 ok = true;
7594 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7595 {
7596 // Need to check that the insn is a ld
7597 if (!view)
7598 view = ppc_object->section_contents(data_shndx,
7599 &slen,
7600 false);
7601 section_size_type off =
7602 (convert_to_section_size_type(reloc.get_r_offset())
7603 + (big_endian ? -2 : 3));
7604 if (off < slen
7605 && (view[off] & (0x3f << 2)) == (58u << 2))
7606 ok = true;
7607 }
7608 if (!ok)
7609 sym_object->set_no_toc_opt(dst_off);
7610 }
7611 }
7612 }
7613 break;
7614 }
7615 }
7616
f159cdb6
AM
7617 if (size == 32)
7618 {
7619 switch (r_type)
7620 {
7621 case elfcpp::R_PPC_LOCAL24PC:
7622 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7623 gold_error(_("%s: unsupported -mbss-plt code"),
7624 ppc_object->name().c_str());
7625 break;
7626 default:
7627 break;
7628 }
7629 }
7630
d8f5a274
AM
7631 switch (r_type)
7632 {
7633 case elfcpp::R_POWERPC_GOT_TLSLD16:
7634 case elfcpp::R_POWERPC_GOT_TLSGD16:
7635 case elfcpp::R_POWERPC_GOT_TPREL16:
7636 case elfcpp::R_POWERPC_GOT_DTPREL16:
7637 case elfcpp::R_POWERPC_GOT16:
7638 case elfcpp::R_PPC64_GOT16_DS:
7639 case elfcpp::R_PPC64_TOC16:
7640 case elfcpp::R_PPC64_TOC16_DS:
7641 ppc_object->set_has_small_toc_reloc();
7642 default:
7643 break;
7644 }
42cacb20
DE
7645}
7646
6d03d481
ST
7647// Process relocations for gc.
7648
7649template<int size, bool big_endian>
7650void
7651Target_powerpc<size, big_endian>::gc_process_relocs(
d83ce4e3
AM
7652 Symbol_table* symtab,
7653 Layout* layout,
7654 Sized_relobj_file<size, big_endian>* object,
7655 unsigned int data_shndx,
7656 unsigned int,
7657 const unsigned char* prelocs,
7658 size_t reloc_count,
7659 Output_section* output_section,
7660 bool needs_special_offset_handling,
7661 size_t local_symbol_count,
7662 const unsigned char* plocal_symbols)
6d03d481
ST
7663{
7664 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
7665 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7666 Classify_reloc;
7667
e81fea4d
AM
7668 Powerpc_relobj<size, big_endian>* ppc_object
7669 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7670 if (size == 64)
7671 ppc_object->set_opd_valid();
7672 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7673 {
7674 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7675 for (p = ppc_object->access_from_map()->begin();
7676 p != ppc_object->access_from_map()->end();
7677 ++p)
7678 {
7679 Address dst_off = p->first;
7680 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7681 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7682 for (s = p->second.begin(); s != p->second.end(); ++s)
7683 {
efc6fa12 7684 Relobj* src_obj = s->first;
e81fea4d
AM
7685 unsigned int src_indx = s->second;
7686 symtab->gc()->add_reference(src_obj, src_indx,
7687 ppc_object, dst_indx);
7688 }
7689 p->second.clear();
7690 }
7691 ppc_object->access_from_map()->clear();
c6de8ed4 7692 ppc_object->process_gc_mark(symtab);
e81fea4d
AM
7693 // Don't look at .opd relocs as .opd will reference everything.
7694 return;
7695 }
6d03d481 7696
4d625b70 7697 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6d03d481
ST
7698 symtab,
7699 layout,
7700 this,
7701 object,
7702 data_shndx,
7703 prelocs,
7704 reloc_count,
7705 output_section,
7706 needs_special_offset_handling,
7707 local_symbol_count,
7708 plocal_symbols);
7709}
7710
e81fea4d
AM
7711// Handle target specific gc actions when adding a gc reference from
7712// SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7713// and DST_OFF. For powerpc64, this adds a referenc to the code
7714// section of a function descriptor.
7715
7716template<int size, bool big_endian>
7717void
7718Target_powerpc<size, big_endian>::do_gc_add_reference(
7719 Symbol_table* symtab,
efc6fa12 7720 Relobj* src_obj,
e81fea4d 7721 unsigned int src_shndx,
efc6fa12 7722 Relobj* dst_obj,
e81fea4d
AM
7723 unsigned int dst_shndx,
7724 Address dst_off) const
7725{
6c77229c
AM
7726 if (size != 64 || dst_obj->is_dynamic())
7727 return;
7728
e81fea4d
AM
7729 Powerpc_relobj<size, big_endian>* ppc_object
7730 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
a2d7bf59 7731 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
e81fea4d
AM
7732 {
7733 if (ppc_object->opd_valid())
7734 {
7735 dst_shndx = ppc_object->get_opd_ent(dst_off);
7736 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7737 }
7738 else
7739 {
7740 // If we haven't run scan_opd_relocs, we must delay
7741 // processing this function descriptor reference.
7742 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7743 }
7744 }
7745}
7746
7747// Add any special sections for this symbol to the gc work list.
7748// For powerpc64, this adds the code section of a function
7749// descriptor.
7750
7751template<int size, bool big_endian>
7752void
7753Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7754 Symbol_table* symtab,
7755 Symbol* sym) const
7756{
7757 if (size == 64)
7758 {
7759 Powerpc_relobj<size, big_endian>* ppc_object
7760 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7761 bool is_ordinary;
7762 unsigned int shndx = sym->shndx(&is_ordinary);
a2d7bf59 7763 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
e81fea4d
AM
7764 {
7765 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7766 Address dst_off = gsym->value();
c6de8ed4
AM
7767 if (ppc_object->opd_valid())
7768 {
7769 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4277535c
RÁE
7770 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7771 dst_indx));
c6de8ed4
AM
7772 }
7773 else
7774 ppc_object->add_gc_mark(dst_off);
e81fea4d
AM
7775 }
7776 }
7777}
7778
dc3714f3
AM
7779// For a symbol location in .opd, set LOC to the location of the
7780// function entry.
7781
7782template<int size, bool big_endian>
7783void
7784Target_powerpc<size, big_endian>::do_function_location(
7785 Symbol_location* loc) const
7786{
a2d7bf59 7787 if (size == 64 && loc->shndx != 0)
dc3714f3
AM
7788 {
7789 if (loc->object->is_dynamic())
7790 {
7791 Powerpc_dynobj<size, big_endian>* ppc_object
7792 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7793 if (loc->shndx == ppc_object->opd_shndx())
7794 {
7795 Address dest_off;
7796 Address off = loc->offset - ppc_object->opd_address();
7797 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7798 loc->offset = dest_off;
7799 }
7800 }
7801 else
7802 {
7803 const Powerpc_relobj<size, big_endian>* ppc_object
7804 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7805 if (loc->shndx == ppc_object->opd_shndx())
7806 {
7807 Address dest_off;
7808 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7809 loc->offset = dest_off;
7810 }
7811 }
7812 }
7813}
7814
bbec1a5d
AM
7815// FNOFFSET in section SHNDX in OBJECT is the start of a function
7816// compiled with -fsplit-stack. The function calls non-split-stack
7817// code. Change the function to ensure it has enough stack space to
7818// call some random function.
7819
7820template<int size, bool big_endian>
7821void
7822Target_powerpc<size, big_endian>::do_calls_non_split(
7823 Relobj* object,
7824 unsigned int shndx,
7825 section_offset_type fnoffset,
7826 section_size_type fnsize,
6e0813d3
CC
7827 const unsigned char* prelocs,
7828 size_t reloc_count,
bbec1a5d
AM
7829 unsigned char* view,
7830 section_size_type view_size,
7831 std::string* from,
7832 std::string* to) const
7833{
7834 // 32-bit not supported.
7835 if (size == 32)
7836 {
7837 // warn
7838 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6e0813d3
CC
7839 prelocs, reloc_count, view, view_size,
7840 from, to);
bbec1a5d
AM
7841 return;
7842 }
7843
7844 // The function always starts with
7845 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7846 // addis %r12,%r1,-allocate@ha
7847 // addi %r12,%r12,-allocate@l
7848 // cmpld %r12,%r0
7849 // but note that the addis or addi may be replaced with a nop
7850
7851 unsigned char *entry = view + fnoffset;
7852 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7853
7854 if ((insn & 0xffff0000) == addis_2_12)
7855 {
7856 /* Skip ELFv2 global entry code. */
7857 entry += 8;
7858 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7859 }
7860
7861 unsigned char *pinsn = entry;
7862 bool ok = false;
7863 const uint32_t ld_private_ss = 0xe80d8fc0;
7864 if (insn == ld_private_ss)
7865 {
7866 int32_t allocate = 0;
7867 while (1)
7868 {
7869 pinsn += 4;
7870 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7871 if ((insn & 0xffff0000) == addis_12_1)
7872 allocate += (insn & 0xffff) << 16;
7873 else if ((insn & 0xffff0000) == addi_12_1
7874 || (insn & 0xffff0000) == addi_12_12)
7875 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7876 else if (insn != nop)
7877 break;
7878 }
7879 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7880 {
7881 int extra = parameters->options().split_stack_adjust_size();
7882 allocate -= extra;
7883 if (allocate >= 0 || extra < 0)
7884 {
7885 object->error(_("split-stack stack size overflow at "
7886 "section %u offset %0zx"),
7887 shndx, static_cast<size_t>(fnoffset));
7888 return;
7889 }
7890 pinsn = entry + 4;
7891 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7892 if (insn != addis_12_1)
7893 {
7894 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7895 pinsn += 4;
7896 insn = addi_12_12 | (allocate & 0xffff);
7897 if (insn != addi_12_12)
7898 {
7899 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7900 pinsn += 4;
7901 }
7902 }
7903 else
7904 {
7905 insn = addi_12_1 | (allocate & 0xffff);
7906 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7907 pinsn += 4;
7908 }
7909 if (pinsn != entry + 12)
7910 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7911
7912 ok = true;
7913 }
7914 }
7915
7916 if (!ok)
7917 {
7918 if (!object->has_no_split_stack())
7919 object->error(_("failed to match split-stack sequence at "
7920 "section %u offset %0zx"),
7921 shndx, static_cast<size_t>(fnoffset));
7922 }
7923}
7924
42cacb20
DE
7925// Scan relocations for a section.
7926
7927template<int size, bool big_endian>
7928void
7929Target_powerpc<size, big_endian>::scan_relocs(
d83ce4e3
AM
7930 Symbol_table* symtab,
7931 Layout* layout,
7932 Sized_relobj_file<size, big_endian>* object,
7933 unsigned int data_shndx,
7934 unsigned int sh_type,
7935 const unsigned char* prelocs,
7936 size_t reloc_count,
7937 Output_section* output_section,
7938 bool needs_special_offset_handling,
7939 size_t local_symbol_count,
7940 const unsigned char* plocal_symbols)
42cacb20
DE
7941{
7942 typedef Target_powerpc<size, big_endian> Powerpc;
4d625b70
CC
7943 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7944 Classify_reloc;
42cacb20 7945
7ee7ff70
AM
7946 if (!this->plt_localentry0_init_)
7947 {
7948 bool plt_localentry0 = false;
7949 if (size == 64
7950 && this->abiversion() >= 2)
7951 {
7952 if (parameters->options().user_set_plt_localentry())
7953 plt_localentry0 = parameters->options().plt_localentry();
d44c746a
AM
7954 if (plt_localentry0
7955 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
7956 gold_warning(_("--plt-localentry is especially dangerous without "
7957 "ld.so support to detect ABI violations"));
7ee7ff70
AM
7958 }
7959 this->plt_localentry0_ = plt_localentry0;
7960 this->plt_localentry0_init_ = true;
7961 }
7962
42cacb20
DE
7963 if (sh_type == elfcpp::SHT_REL)
7964 {
7965 gold_error(_("%s: unsupported REL reloc section"),
7966 object->name().c_str());
7967 return;
7968 }
7969
4d625b70 7970 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
42cacb20
DE
7971 symtab,
7972 layout,
7973 this,
7974 object,
7975 data_shndx,
7976 prelocs,
7977 reloc_count,
7978 output_section,
7979 needs_special_offset_handling,
7980 local_symbol_count,
7981 plocal_symbols);
7982}
7983
ec4dbad3
AM
7984// Functor class for processing the global symbol table.
7985// Removes symbols defined on discarded opd entries.
7986
7987template<bool big_endian>
7988class Global_symbol_visitor_opd
7989{
7990 public:
7991 Global_symbol_visitor_opd()
7992 { }
7993
7994 void
7995 operator()(Sized_symbol<64>* sym)
7996 {
7997 if (sym->has_symtab_index()
7998 || sym->source() != Symbol::FROM_OBJECT
7999 || !sym->in_real_elf())
8000 return;
8001
6c77229c
AM
8002 if (sym->object()->is_dynamic())
8003 return;
8004
ec4dbad3
AM
8005 Powerpc_relobj<64, big_endian>* symobj
8006 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6c77229c 8007 if (symobj->opd_shndx() == 0)
ec4dbad3
AM
8008 return;
8009
8010 bool is_ordinary;
8011 unsigned int shndx = sym->shndx(&is_ordinary);
8012 if (shndx == symobj->opd_shndx()
8013 && symobj->get_opd_discard(sym->value()))
1611bc4a
AM
8014 {
8015 sym->set_undefined();
e3ee8ed4 8016 sym->set_visibility(elfcpp::STV_DEFAULT);
1611bc4a
AM
8017 sym->set_is_defined_in_discarded_section();
8018 sym->set_symtab_index(-1U);
8019 }
ec4dbad3
AM
8020 }
8021};
8022
f3a0ed29
AM
8023template<int size, bool big_endian>
8024void
8025Target_powerpc<size, big_endian>::define_save_restore_funcs(
8026 Layout* layout,
8027 Symbol_table* symtab)
8028{
8029 if (size == 64)
8030 {
d49044c7
AM
8031 Output_data_save_res<size, big_endian>* savres
8032 = new Output_data_save_res<size, big_endian>(symtab);
8033 this->savres_section_ = savres;
f3a0ed29
AM
8034 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
8035 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
8036 savres, ORDER_TEXT, false);
8037 }
8038}
8039
d8f5a274
AM
8040// Sort linker created .got section first (for the header), then input
8041// sections belonging to files using small model code.
8042
8043template<bool big_endian>
8044class Sort_toc_sections
8045{
8046 public:
8047 bool
8048 operator()(const Output_section::Input_section& is1,
8049 const Output_section::Input_section& is2) const
8050 {
8051 if (!is1.is_input_section() && is2.is_input_section())
8052 return true;
8053 bool small1
8054 = (is1.is_input_section()
8055 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
8056 ->has_small_toc_reloc()));
8057 bool small2
8058 = (is2.is_input_section()
8059 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
8060 ->has_small_toc_reloc()));
8061 return small1 && !small2;
8062 }
8063};
8064
42cacb20
DE
8065// Finalize the sections.
8066
8067template<int size, bool big_endian>
8068void
d5b40221
DK
8069Target_powerpc<size, big_endian>::do_finalize_sections(
8070 Layout* layout,
f59f41f3 8071 const Input_objects*,
ec4dbad3 8072 Symbol_table* symtab)
42cacb20 8073{
c9824451
AM
8074 if (parameters->doing_static_link())
8075 {
8076 // At least some versions of glibc elf-init.o have a strong
8077 // reference to __rela_iplt marker syms. A weak ref would be
8078 // better..
8079 if (this->iplt_ != NULL)
8080 {
8081 Reloc_section* rel = this->iplt_->rel_plt();
8082 symtab->define_in_output_data("__rela_iplt_start", NULL,
8083 Symbol_table::PREDEFINED, rel, 0, 0,
8084 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8085 elfcpp::STV_HIDDEN, 0, false, true);
8086 symtab->define_in_output_data("__rela_iplt_end", NULL,
8087 Symbol_table::PREDEFINED, rel, 0, 0,
8088 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8089 elfcpp::STV_HIDDEN, 0, true, true);
8090 }
8091 else
8092 {
8093 symtab->define_as_constant("__rela_iplt_start", NULL,
8094 Symbol_table::PREDEFINED, 0, 0,
8095 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8096 elfcpp::STV_HIDDEN, 0, true, false);
8097 symtab->define_as_constant("__rela_iplt_end", NULL,
8098 Symbol_table::PREDEFINED, 0, 0,
8099 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
8100 elfcpp::STV_HIDDEN, 0, true, false);
8101 }
8102 }
8103
ec4dbad3
AM
8104 if (size == 64)
8105 {
8106 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
8107 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
ec661b9d
AM
8108
8109 if (!parameters->options().relocatable())
8110 {
8111 this->define_save_restore_funcs(layout, symtab);
8112
8113 // Annoyingly, we need to make these sections now whether or
8114 // not we need them. If we delay until do_relax then we
8115 // need to mess with the relaxation machinery checkpointing.
8116 this->got_section(symtab, layout);
8117 this->make_brlt_section(layout);
d8f5a274
AM
8118
8119 if (parameters->options().toc_sort())
8120 {
8121 Output_section* os = this->got_->output_section();
8122 if (os != NULL && os->input_sections().size() > 1)
8123 std::stable_sort(os->input_sections().begin(),
8124 os->input_sections().end(),
8125 Sort_toc_sections<big_endian>());
8126 }
ec661b9d 8127 }
ec4dbad3
AM
8128 }
8129
42cacb20 8130 // Fill in some more dynamic tags.
c9269dff 8131 Output_data_dynamic* odyn = layout->dynamic_data();
c9824451 8132 if (odyn != NULL)
cf43a2fe 8133 {
c9824451
AM
8134 const Reloc_section* rel_plt = (this->plt_ == NULL
8135 ? NULL
8136 : this->plt_->rel_plt());
8137 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
8138 this->rela_dyn_, true, size == 32);
8139
8140 if (size == 32)
dd93cd0a 8141 {
c9824451
AM
8142 if (this->got_ != NULL)
8143 {
8144 this->got_->finalize_data_size();
8145 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
8146 this->got_, this->got_->g_o_t());
8147 }
34e0882b
AM
8148 if (this->has_tls_get_addr_opt_)
8149 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
dd93cd0a 8150 }
c9824451 8151 else
dd93cd0a 8152 {
c9824451
AM
8153 if (this->glink_ != NULL)
8154 {
8155 this->glink_->finalize_data_size();
8156 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
8157 this->glink_,
ec661b9d 8158 (this->glink_->pltresolve_size
c9824451
AM
8159 - 32));
8160 }
34e0882b 8161 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
7ee7ff70 8162 odyn->add_constant(elfcpp::DT_PPC64_OPT,
34e0882b
AM
8163 ((this->has_localentry0_
8164 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
8165 | (this->has_tls_get_addr_opt_
8166 ? elfcpp::PPC64_OPT_TLS : 0)));
dd93cd0a 8167 }
c9269dff 8168 }
cf43a2fe 8169
42cacb20
DE
8170 // Emit any relocs we saved in an attempt to avoid generating COPY
8171 // relocs.
8172 if (this->copy_relocs_.any_saved_relocs())
8173 this->copy_relocs_.emit(this->rela_dyn_section(layout));
8174}
8175
5edad15d
AM
8176// Emit any saved relocs, and mark toc entries using any of these
8177// relocs as not optimizable.
aba6bc71 8178
5edad15d
AM
8179template<int sh_type, int size, bool big_endian>
8180void
8181Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
8182 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
aba6bc71 8183{
5edad15d
AM
8184 if (size == 64
8185 && parameters->options().toc_optimize())
8186 {
8187 for (typename Copy_relocs<sh_type, size, big_endian>::
8188 Copy_reloc_entries::iterator p = this->entries_.begin();
8189 p != this->entries_.end();
8190 ++p)
8191 {
8192 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
8193 entry = *p;
8194
8195 // If the symbol is no longer defined in a dynamic object,
8196 // then we emitted a COPY relocation. If it is still
8197 // dynamic then we'll need dynamic relocations and thus
8198 // can't optimize toc entries.
8199 if (entry.sym_->is_from_dynobj())
8200 {
8201 Powerpc_relobj<size, big_endian>* ppc_object
8202 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
8203 if (entry.shndx_ == ppc_object->toc_shndx())
8204 ppc_object->set_no_toc_opt(entry.address_);
8205 }
8206 }
8207 }
8208
8209 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
aba6bc71
AM
8210}
8211
3ea0a085
AM
8212// Return the value to use for a branch relocation.
8213
8214template<int size, bool big_endian>
1611bc4a 8215bool
3ea0a085 8216Target_powerpc<size, big_endian>::symval_for_branch(
6c77229c 8217 const Symbol_table* symtab,
3ea0a085
AM
8218 const Sized_symbol<size>* gsym,
8219 Powerpc_relobj<size, big_endian>* object,
1611bc4a 8220 Address *value,
3ea0a085
AM
8221 unsigned int *dest_shndx)
8222{
9055360d
AM
8223 if (size == 32 || this->abiversion() >= 2)
8224 gold_unreachable();
3ea0a085 8225 *dest_shndx = 0;
3ea0a085
AM
8226
8227 // If the symbol is defined in an opd section, ie. is a function
8228 // descriptor, use the function descriptor code entry address
8229 Powerpc_relobj<size, big_endian>* symobj = object;
f3a0ed29 8230 if (gsym != NULL
0e123f69
AM
8231 && (gsym->source() != Symbol::FROM_OBJECT
8232 || gsym->object()->is_dynamic()))
1611bc4a 8233 return true;
3ea0a085
AM
8234 if (gsym != NULL)
8235 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8236 unsigned int shndx = symobj->opd_shndx();
8237 if (shndx == 0)
1611bc4a 8238 return true;
3ea0a085 8239 Address opd_addr = symobj->get_output_section_offset(shndx);
a2d7bf59 8240 if (opd_addr == invalid_address)
1611bc4a 8241 return true;
c6905c28 8242 opd_addr += symobj->output_section_address(shndx);
1611bc4a 8243 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
3ea0a085
AM
8244 {
8245 Address sec_off;
1611bc4a 8246 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6c77229c
AM
8247 if (symtab->is_section_folded(symobj, *dest_shndx))
8248 {
8249 Section_id folded
8250 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
8251 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
8252 *dest_shndx = folded.second;
8253 }
3ea0a085 8254 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
1611bc4a
AM
8255 if (sec_addr == invalid_address)
8256 return false;
8257
3ea0a085 8258 sec_addr += symobj->output_section(*dest_shndx)->address();
1611bc4a 8259 *value = sec_addr + sec_off;
3ea0a085 8260 }
1611bc4a 8261 return true;
3ea0a085
AM
8262}
8263
42cacb20
DE
8264// Perform a relocation.
8265
8266template<int size, bool big_endian>
8267inline bool
8268Target_powerpc<size, big_endian>::Relocate::relocate(
d83ce4e3 8269 const Relocate_info<size, big_endian>* relinfo,
91a65d2f 8270 unsigned int,
d83ce4e3
AM
8271 Target_powerpc* target,
8272 Output_section* os,
8273 size_t relnum,
91a65d2f 8274 const unsigned char* preloc,
d83ce4e3
AM
8275 const Sized_symbol<size>* gsym,
8276 const Symbol_value<size>* psymval,
8277 unsigned char* view,
c9269dff
AM
8278 Address address,
8279 section_size_type view_size)
42cacb20 8280{
0e804863
ILT
8281 if (view == NULL)
8282 return true;
8283
34e0882b
AM
8284 if (target->replace_tls_get_addr(gsym))
8285 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
8286
91a65d2f
AM
8287 const elfcpp::Rela<size, big_endian> rela(preloc);
8288 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
34e0882b 8289 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
dd93cd0a 8290 {
e3deeb9c
AM
8291 case Track_tls::NOT_EXPECTED:
8292 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8293 _("__tls_get_addr call lacks marker reloc"));
8294 break;
8295 case Track_tls::EXPECTED:
8296 // We have already complained.
8297 break;
8298 case Track_tls::SKIP:
8299 return true;
8300 case Track_tls::NORMAL:
8301 break;
dd93cd0a 8302 }
dd93cd0a 8303
42cacb20 8304 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
dd93cd0a 8305 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
0e123f69 8306 typedef typename elfcpp::Rela<size, big_endian> Reltype;
dcfc7dd4
AM
8307 // Offset from start of insn to d-field reloc.
8308 const int d_offset = big_endian ? 2 : 0;
8309
3ea0a085
AM
8310 Powerpc_relobj<size, big_endian>* const object
8311 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
dd93cd0a 8312 Address value = 0;
0cfb0717 8313 bool has_stub_value = false;
7ee7ff70 8314 bool localentry0 = false;
e5d5f5ed 8315 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
b3ccdeb5 8316 if ((gsym != NULL
88b8e639 8317 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
b3ccdeb5
AM
8318 : object->local_has_plt_offset(r_sym))
8319 && (!psymval->is_ifunc_symbol()
9055360d 8320 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
dd93cd0a 8321 {
9055360d
AM
8322 if (size == 64
8323 && gsym != NULL
8324 && target->abiversion() >= 2
8325 && !parameters->options().output_is_position_independent()
8326 && !is_branch_reloc(r_type))
ec661b9d 8327 {
faa2211d
AM
8328 Address off = target->glink_section()->find_global_entry(gsym);
8329 if (off != invalid_address)
6ec65f28
AM
8330 {
8331 value = target->glink_section()->global_entry_address() + off;
8332 has_stub_value = true;
8333 }
ec661b9d 8334 }
c9824451 8335 else
9055360d
AM
8336 {
8337 Stub_table<size, big_endian>* stub_table
8338 = object->stub_table(relinfo->data_shndx);
8339 if (stub_table == NULL)
8340 {
8341 // This is a ref from a data section to an ifunc symbol.
8342 if (target->stub_tables().size() != 0)
8343 stub_table = target->stub_tables()[0];
8344 }
faa2211d
AM
8345 if (stub_table != NULL)
8346 {
7e57d19e 8347 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
faa2211d 8348 if (gsym != NULL)
7e57d19e 8349 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
faa2211d
AM
8350 rela.get_r_addend());
8351 else
7e57d19e 8352 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
faa2211d 8353 rela.get_r_addend());
7e57d19e 8354 if (ent != NULL)
faa2211d 8355 {
7e57d19e
AM
8356 value = stub_table->stub_address() + ent->off_;
8357 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8358 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
8359 size_t reloc_count = shdr.get_sh_size() / reloc_size;
8360 if (size == 64
8361 && ent->r2save_
8362 && relnum + 1 < reloc_count)
8363 {
8364 Reltype next_rela(preloc + reloc_size);
8365 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
8366 == elfcpp::R_PPC64_TOCSAVE
8367 && next_rela.get_r_offset() == rela.get_r_offset() + 4)
8368 value += 4;
8369 }
7ee7ff70 8370 localentry0 = ent->localentry0_;
faa2211d
AM
8371 has_stub_value = true;
8372 }
8373 }
9055360d 8374 }
faa2211d
AM
8375 // We don't care too much about bogus debug references to
8376 // non-local functions, but otherwise there had better be a plt
8377 // call stub or global entry stub as appropriate.
8378 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
dd93cd0a 8379 }
cf43a2fe
AM
8380
8381 if (r_type == elfcpp::R_POWERPC_GOT16
8382 || r_type == elfcpp::R_POWERPC_GOT16_LO
8383 || r_type == elfcpp::R_POWERPC_GOT16_HI
8384 || r_type == elfcpp::R_POWERPC_GOT16_HA
8385 || r_type == elfcpp::R_PPC64_GOT16_DS
8386 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
42cacb20 8387 {
cf43a2fe
AM
8388 if (gsym != NULL)
8389 {
8390 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
8391 value = gsym->got_offset(GOT_TYPE_STANDARD);
8392 }
8393 else
8394 {
cf43a2fe
AM
8395 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
8396 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
8397 }
dd93cd0a 8398 value -= target->got_section()->got_base_offset(object);
cf43a2fe
AM
8399 }
8400 else if (r_type == elfcpp::R_PPC64_TOC)
8401 {
c9269dff 8402 value = (target->got_section()->output_section()->address()
dd93cd0a 8403 + object->toc_base_offset());
cf43a2fe
AM
8404 }
8405 else if (gsym != NULL
8406 && (r_type == elfcpp::R_POWERPC_REL24
8407 || r_type == elfcpp::R_PPC_PLTREL24)
0cfb0717 8408 && has_stub_value)
cf43a2fe 8409 {
c9269dff
AM
8410 if (size == 64)
8411 {
8412 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
8413 Valtype* wv = reinterpret_cast<Valtype*>(view);
34e0882b
AM
8414 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
8415 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
c9269dff 8416 {
3ea0a085 8417 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
c9269dff 8418 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3ea0a085
AM
8419 if ((insn & 1) != 0
8420 && (insn2 == nop
8421 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
c9269dff 8422 {
b4f7960d
AM
8423 elfcpp::Swap<32, big_endian>::
8424 writeval(wv + 1, ld_2_1 + target->stk_toc());
c9269dff
AM
8425 can_plt_call = true;
8426 }
8427 }
8428 if (!can_plt_call)
3ea0a085
AM
8429 {
8430 // If we don't have a branch and link followed by a nop,
8431 // we can't go via the plt because there is no place to
8432 // put a toc restoring instruction.
8433 // Unless we know we won't be returning.
8434 if (strcmp(gsym->name(), "__libc_start_main") == 0)
8435 can_plt_call = true;
8436 }
8437 if (!can_plt_call)
8438 {
ba8ca3e7
AM
8439 // g++ as of 20130507 emits self-calls without a
8440 // following nop. This is arguably wrong since we have
8441 // conflicting information. On the one hand a global
8442 // symbol and on the other a local call sequence, but
8443 // don't error for this special case.
8444 // It isn't possible to cheaply verify we have exactly
8445 // such a call. Allow all calls to the same section.
3ea0a085 8446 bool ok = false;
c9824451 8447 Address code = value;
3ea0a085
AM
8448 if (gsym->source() == Symbol::FROM_OBJECT
8449 && gsym->object() == object)
8450 {
9055360d
AM
8451 unsigned int dest_shndx = 0;
8452 if (target->abiversion() < 2)
8453 {
8454 Address addend = rela.get_r_addend();
1611bc4a
AM
8455 code = psymval->value(object, addend);
8456 target->symval_for_branch(relinfo->symtab, gsym, object,
8457 &code, &dest_shndx);
9055360d 8458 }
3ea0a085
AM
8459 bool is_ordinary;
8460 if (dest_shndx == 0)
8461 dest_shndx = gsym->shndx(&is_ordinary);
8462 ok = dest_shndx == relinfo->data_shndx;
8463 }
8464 if (!ok)
c9824451
AM
8465 {
8466 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8467 _("call lacks nop, can't restore toc; "
8468 "recompile with -fPIC"));
8469 value = code;
8470 }
3ea0a085 8471 }
c9269dff 8472 }
cf43a2fe 8473 }
dd93cd0a
AM
8474 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8475 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8476 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8477 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8478 {
8479 // First instruction of a global dynamic sequence, arg setup insn.
8480 const bool final = gsym == NULL || gsym->final_value_is_known();
8481 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8482 enum Got_type got_type = GOT_TYPE_STANDARD;
8483 if (tls_type == tls::TLSOPT_NONE)
8484 got_type = GOT_TYPE_TLSGD;
8485 else if (tls_type == tls::TLSOPT_TO_IE)
8486 got_type = GOT_TYPE_TPREL;
8487 if (got_type != GOT_TYPE_STANDARD)
8488 {
8489 if (gsym != NULL)
8490 {
8491 gold_assert(gsym->has_got_offset(got_type));
8492 value = gsym->got_offset(got_type);
8493 }
8494 else
8495 {
dd93cd0a
AM
8496 gold_assert(object->local_has_got_offset(r_sym, got_type));
8497 value = object->local_got_offset(r_sym, got_type);
8498 }
8499 value -= target->got_section()->got_base_offset(object);
8500 }
8501 if (tls_type == tls::TLSOPT_TO_IE)
8502 {
8503 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8504 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8505 {
dcfc7dd4 8506 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8507 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8508 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
8509 if (size == 32)
8510 insn |= 32 << 26; // lwz
8511 else
8512 insn |= 58 << 26; // ld
8513 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8514 }
8515 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8516 - elfcpp::R_POWERPC_GOT_TLSGD16);
8517 }
8518 else if (tls_type == tls::TLSOPT_TO_LE)
8519 {
8520 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8521 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8522 {
dcfc7dd4 8523 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
8524 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8525 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 8526 if (size == 32)
0f81d3f0
AM
8527 insn |= addis_0_2;
8528 else
8529 insn |= addis_0_13;
dd93cd0a
AM
8530 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8531 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8532 value = psymval->value(object, rela.get_r_addend());
8533 }
8534 else
8535 {
dcfc7dd4 8536 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8537 Insn insn = nop;
8538 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8539 r_type = elfcpp::R_POWERPC_NONE;
8540 }
8541 }
8542 }
8543 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8544 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8545 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8546 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8547 {
8548 // First instruction of a local dynamic sequence, arg setup insn.
8549 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8550 if (tls_type == tls::TLSOPT_NONE)
8551 {
8552 value = target->tlsld_got_offset();
8553 value -= target->got_section()->got_base_offset(object);
8554 }
8555 else
8556 {
8557 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8558 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8559 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8560 {
dcfc7dd4 8561 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
0f81d3f0
AM
8562 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8563 insn &= (1 << 26) - (1 << 21); // extract rt
dd93cd0a 8564 if (size == 32)
0f81d3f0
AM
8565 insn |= addis_0_2;
8566 else
8567 insn |= addis_0_13;
dd93cd0a
AM
8568 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8569 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7404fe1b 8570 value = dtp_offset;
dd93cd0a
AM
8571 }
8572 else
8573 {
dcfc7dd4 8574 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8575 Insn insn = nop;
8576 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8577 r_type = elfcpp::R_POWERPC_NONE;
8578 }
8579 }
8580 }
8581 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8582 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8583 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8584 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8585 {
8586 // Accesses relative to a local dynamic sequence address,
8587 // no optimisation here.
8588 if (gsym != NULL)
8589 {
8590 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8591 value = gsym->got_offset(GOT_TYPE_DTPREL);
8592 }
8593 else
8594 {
dd93cd0a
AM
8595 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8596 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8597 }
8598 value -= target->got_section()->got_base_offset(object);
8599 }
8600 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8601 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8602 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8603 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8604 {
8605 // First instruction of initial exec sequence.
8606 const bool final = gsym == NULL || gsym->final_value_is_known();
8607 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8608 if (tls_type == tls::TLSOPT_NONE)
8609 {
8610 if (gsym != NULL)
8611 {
8612 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8613 value = gsym->got_offset(GOT_TYPE_TPREL);
8614 }
8615 else
8616 {
dd93cd0a
AM
8617 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8618 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8619 }
8620 value -= target->got_section()->got_base_offset(object);
8621 }
8622 else
8623 {
8624 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8625 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8626 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8627 {
dcfc7dd4 8628 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8629 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8630 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8631 if (size == 32)
8632 insn |= addis_0_2;
8633 else
8634 insn |= addis_0_13;
8635 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8636 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8637 value = psymval->value(object, rela.get_r_addend());
8638 }
8639 else
8640 {
dcfc7dd4 8641 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
dd93cd0a
AM
8642 Insn insn = nop;
8643 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8644 r_type = elfcpp::R_POWERPC_NONE;
8645 }
8646 }
8647 }
8648 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8649 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8650 {
8651 // Second instruction of a global dynamic sequence,
8652 // the __tls_get_addr call
e3deeb9c 8653 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
8654 const bool final = gsym == NULL || gsym->final_value_is_known();
8655 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8656 if (tls_type != tls::TLSOPT_NONE)
8657 {
8658 if (tls_type == tls::TLSOPT_TO_IE)
8659 {
8660 Insn* iview = reinterpret_cast<Insn*>(view);
8661 Insn insn = add_3_3_13;
8662 if (size == 32)
8663 insn = add_3_3_2;
8664 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8665 r_type = elfcpp::R_POWERPC_NONE;
8666 }
8667 else
8668 {
8669 Insn* iview = reinterpret_cast<Insn*>(view);
8670 Insn insn = addi_3_3;
8671 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8672 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8673 view += d_offset;
dd93cd0a
AM
8674 value = psymval->value(object, rela.get_r_addend());
8675 }
e3deeb9c 8676 this->skip_next_tls_get_addr_call();
dd93cd0a
AM
8677 }
8678 }
8679 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8680 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8681 {
8682 // Second instruction of a local dynamic sequence,
8683 // the __tls_get_addr call
e3deeb9c 8684 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
dd93cd0a
AM
8685 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8686 if (tls_type == tls::TLSOPT_TO_LE)
8687 {
8688 Insn* iview = reinterpret_cast<Insn*>(view);
8689 Insn insn = addi_3_3;
8690 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
e3deeb9c 8691 this->skip_next_tls_get_addr_call();
dd93cd0a 8692 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8693 view += d_offset;
7404fe1b 8694 value = dtp_offset;
dd93cd0a
AM
8695 }
8696 }
8697 else if (r_type == elfcpp::R_POWERPC_TLS)
8698 {
8699 // Second instruction of an initial exec sequence
8700 const bool final = gsym == NULL || gsym->final_value_is_known();
8701 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8702 if (tls_type == tls::TLSOPT_TO_LE)
8703 {
8704 Insn* iview = reinterpret_cast<Insn*>(view);
8705 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8706 unsigned int reg = size == 32 ? 2 : 13;
8707 insn = at_tls_transform(insn, reg);
8708 gold_assert(insn != 0);
8709 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8710 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 8711 view += d_offset;
dd93cd0a
AM
8712 value = psymval->value(object, rela.get_r_addend());
8713 }
8714 }
0cfb0717 8715 else if (!has_stub_value)
cf43a2fe 8716 {
dd93cd0a 8717 Address addend = 0;
cbcb23fa 8718 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
cf43a2fe 8719 addend = rela.get_r_addend();
c9824451 8720 value = psymval->value(object, addend);
dd93cd0a 8721 if (size == 64 && is_branch_reloc(r_type))
9055360d
AM
8722 {
8723 if (target->abiversion() >= 2)
8724 {
8725 if (gsym != NULL)
8726 value += object->ppc64_local_entry_offset(gsym);
8727 else
8728 value += object->ppc64_local_entry_offset(r_sym);
8729 }
8730 else
1611bc4a
AM
8731 {
8732 unsigned int dest_shndx;
8733 target->symval_for_branch(relinfo->symtab, gsym, object,
8734 &value, &dest_shndx);
8735 }
9055360d 8736 }
cbcb23fa 8737 Address max_branch_offset = max_branch_delta(r_type);
ec661b9d
AM
8738 if (max_branch_offset != 0
8739 && value - address + max_branch_offset >= 2 * max_branch_offset)
8740 {
8741 Stub_table<size, big_endian>* stub_table
8742 = object->stub_table(relinfo->data_shndx);
0cfdc767
AM
8743 if (stub_table != NULL)
8744 {
8745 Address off = stub_table->find_long_branch_entry(object, value);
8746 if (off != invalid_address)
0cfb0717
AM
8747 {
8748 value = (stub_table->stub_address() + stub_table->plt_size()
8749 + off);
8750 has_stub_value = true;
8751 }
0cfdc767 8752 }
ec661b9d 8753 }
42cacb20
DE
8754 }
8755
42cacb20
DE
8756 switch (r_type)
8757 {
dd93cd0a
AM
8758 case elfcpp::R_PPC64_REL64:
8759 case elfcpp::R_POWERPC_REL32:
8760 case elfcpp::R_POWERPC_REL24:
8761 case elfcpp::R_PPC_PLTREL24:
8762 case elfcpp::R_PPC_LOCAL24PC:
8763 case elfcpp::R_POWERPC_REL16:
8764 case elfcpp::R_POWERPC_REL16_LO:
8765 case elfcpp::R_POWERPC_REL16_HI:
8766 case elfcpp::R_POWERPC_REL16_HA:
a680de9a 8767 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a
AM
8768 case elfcpp::R_POWERPC_REL14:
8769 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8770 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8771 value -= address;
8772 break;
8773
42cacb20
DE
8774 case elfcpp::R_PPC64_TOC16:
8775 case elfcpp::R_PPC64_TOC16_LO:
8776 case elfcpp::R_PPC64_TOC16_HI:
8777 case elfcpp::R_PPC64_TOC16_HA:
8778 case elfcpp::R_PPC64_TOC16_DS:
8779 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe 8780 // Subtract the TOC base address.
c9269dff 8781 value -= (target->got_section()->output_section()->address()
dd93cd0a 8782 + object->toc_base_offset());
42cacb20
DE
8783 break;
8784
cf43a2fe
AM
8785 case elfcpp::R_POWERPC_SECTOFF:
8786 case elfcpp::R_POWERPC_SECTOFF_LO:
8787 case elfcpp::R_POWERPC_SECTOFF_HI:
8788 case elfcpp::R_POWERPC_SECTOFF_HA:
8789 case elfcpp::R_PPC64_SECTOFF_DS:
8790 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8791 if (os != NULL)
8792 value -= os->address();
42cacb20
DE
8793 break;
8794
dd93cd0a
AM
8795 case elfcpp::R_PPC64_TPREL16_DS:
8796 case elfcpp::R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
8797 case elfcpp::R_PPC64_TPREL16_HIGH:
8798 case elfcpp::R_PPC64_TPREL16_HIGHA:
dd93cd0a 8799 if (size != 64)
f9c6b907 8800 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
dd93cd0a 8801 break;
d8e90251 8802 // Fall through.
dd93cd0a
AM
8803 case elfcpp::R_POWERPC_TPREL16:
8804 case elfcpp::R_POWERPC_TPREL16_LO:
8805 case elfcpp::R_POWERPC_TPREL16_HI:
8806 case elfcpp::R_POWERPC_TPREL16_HA:
8807 case elfcpp::R_POWERPC_TPREL:
8808 case elfcpp::R_PPC64_TPREL16_HIGHER:
8809 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8810 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8811 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8812 // tls symbol values are relative to tls_segment()->vaddr()
8813 value -= tp_offset;
8814 break;
8815
8816 case elfcpp::R_PPC64_DTPREL16_DS:
8817 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8818 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8819 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8820 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8821 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8822 if (size != 64)
8823 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8824 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8825 break;
d8e90251 8826 // Fall through.
dd93cd0a
AM
8827 case elfcpp::R_POWERPC_DTPREL16:
8828 case elfcpp::R_POWERPC_DTPREL16_LO:
8829 case elfcpp::R_POWERPC_DTPREL16_HI:
8830 case elfcpp::R_POWERPC_DTPREL16_HA:
8831 case elfcpp::R_POWERPC_DTPREL:
f9c6b907
AM
8832 case elfcpp::R_PPC64_DTPREL16_HIGH:
8833 case elfcpp::R_PPC64_DTPREL16_HIGHA:
dd93cd0a
AM
8834 // tls symbol values are relative to tls_segment()->vaddr()
8835 value -= dtp_offset;
8836 break;
8837
45965137
AM
8838 case elfcpp::R_PPC64_ADDR64_LOCAL:
8839 if (gsym != NULL)
8840 value += object->ppc64_local_entry_offset(gsym);
8841 else
8842 value += object->ppc64_local_entry_offset(r_sym);
8843 break;
8844
42cacb20
DE
8845 default:
8846 break;
8847 }
8848
dd93cd0a 8849 Insn branch_bit = 0;
42cacb20
DE
8850 switch (r_type)
8851 {
dd93cd0a
AM
8852 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8853 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8854 branch_bit = 1 << 21;
d8e90251 8855 // Fall through.
dd93cd0a
AM
8856 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8857 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8858 {
8859 Insn* iview = reinterpret_cast<Insn*>(view);
8860 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8861 insn &= ~(1 << 21);
8862 insn |= branch_bit;
8863 if (this->is_isa_v2)
8864 {
8865 // Set 'a' bit. This is 0b00010 in BO field for branch
8866 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8867 // for branch on CTR insns (BO == 1a00t or 1a01t).
8868 if ((insn & (0x14 << 21)) == (0x04 << 21))
8869 insn |= 0x02 << 21;
8870 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8871 insn |= 0x08 << 21;
8872 else
8873 break;
8874 }
8875 else
8876 {
8877 // Invert 'y' bit if not the default.
8878 if (static_cast<Signed_address>(value) < 0)
8879 insn ^= 1 << 21;
8880 }
8881 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8882 }
8883 break;
8884
8885 default:
8886 break;
8887 }
8888
aba6bc71
AM
8889 if (size == 64)
8890 {
aba6bc71
AM
8891 switch (r_type)
8892 {
8893 default:
8894 break;
8895
5edad15d
AM
8896 // Multi-instruction sequences that access the GOT/TOC can
8897 // be optimized, eg.
8898 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8899 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8900 // and
8901 // addis ra,r2,0; addi rb,ra,x@toc@l;
8902 // to nop; addi rb,r2,x@toc;
8903 // FIXME: the @got sequence shown above is not yet
8904 // optimized. Note that gcc as of 2017-01-07 doesn't use
8905 // the ELF @got relocs except for TLS, instead using the
8906 // PowerOpen variant of a compiler managed GOT (called TOC).
8907 // The PowerOpen TOC sequence equivalent to the first
8908 // example is optimized.
aba6bc71
AM
8909 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8910 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8911 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8912 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8913 case elfcpp::R_POWERPC_GOT16_HA:
8914 case elfcpp::R_PPC64_TOC16_HA:
d8f5a274 8915 if (parameters->options().toc_optimize())
aba6bc71 8916 {
dcfc7dd4 8917 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8918 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8919 if (r_type == elfcpp::R_PPC64_TOC16_HA
8920 && object->make_toc_relative(target, &value))
8921 {
8922 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8923 == ((15u << 26) | (2 << 16)));
8924 }
8925 if (((insn & ((0x3f << 26) | 0x1f << 16))
8926 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8927 && value + 0x8000 < 0x10000)
aba6bc71
AM
8928 {
8929 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8930 return true;
8931 }
8932 }
8933 break;
8934
8935 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8936 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8937 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8938 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8939 case elfcpp::R_POWERPC_GOT16_LO:
8940 case elfcpp::R_PPC64_GOT16_LO_DS:
8941 case elfcpp::R_PPC64_TOC16_LO:
8942 case elfcpp::R_PPC64_TOC16_LO_DS:
d8f5a274 8943 if (parameters->options().toc_optimize())
aba6bc71 8944 {
dcfc7dd4 8945 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
aba6bc71 8946 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5edad15d
AM
8947 bool changed = false;
8948 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8949 && object->make_toc_relative(target, &value))
8950 {
8951 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8952 insn ^= (14u << 26) ^ (58u << 26);
8953 r_type = elfcpp::R_PPC64_TOC16_LO;
8954 changed = true;
8955 }
8956 if (ok_lo_toc_insn(insn, r_type)
8957 && value + 0x8000 < 0x10000)
aba6bc71
AM
8958 {
8959 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8960 {
8961 // Transform addic to addi when we change reg.
8962 insn &= ~((0x3f << 26) | (0x1f << 16));
8963 insn |= (14u << 26) | (2 << 16);
8964 }
8965 else
8966 {
8967 insn &= ~(0x1f << 16);
8968 insn |= 2 << 16;
8969 }
5edad15d 8970 changed = true;
aba6bc71 8971 }
5edad15d
AM
8972 if (changed)
8973 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
aba6bc71
AM
8974 }
8975 break;
549dba71
AM
8976
8977 case elfcpp::R_PPC64_ENTRY:
8978 value = (target->got_section()->output_section()->address()
8979 + object->toc_base_offset());
8980 if (value + 0x80008000 <= 0xffffffff
8981 && !parameters->options().output_is_position_independent())
8982 {
8983 Insn* iview = reinterpret_cast<Insn*>(view);
8984 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8985 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8986
8987 if ((insn1 & ~0xfffc) == ld_2_12
8988 && insn2 == add_2_2_12)
8989 {
8990 insn1 = lis_2 + ha(value);
8991 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8992 insn2 = addi_2_2 + l(value);
8993 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8994 return true;
8995 }
8996 }
8997 else
8998 {
8999 value -= address;
9000 if (value + 0x80008000 <= 0xffffffff)
9001 {
9002 Insn* iview = reinterpret_cast<Insn*>(view);
9003 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
9004 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
9005
9006 if ((insn1 & ~0xfffc) == ld_2_12
9007 && insn2 == add_2_2_12)
9008 {
9009 insn1 = addis_2_12 + ha(value);
9010 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
9011 insn2 = addi_2_2 + l(value);
9012 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
9013 return true;
9014 }
9015 }
9016 }
9017 break;
e3a7574e
AM
9018
9019 case elfcpp::R_POWERPC_REL16_LO:
9020 // If we are generating a non-PIC executable, edit
9021 // 0: addis 2,12,.TOC.-0b@ha
9022 // addi 2,2,.TOC.-0b@l
9023 // used by ELFv2 global entry points to set up r2, to
9024 // lis 2,.TOC.@ha
9025 // addi 2,2,.TOC.@l
9026 // if .TOC. is in range. */
9027 if (value + address - 4 + 0x80008000 <= 0xffffffff
9028 && relnum != 0
9029 && preloc != NULL
9030 && target->abiversion() >= 2
9031 && !parameters->options().output_is_position_independent()
4f038ee5 9032 && rela.get_r_addend() == d_offset + 4
e3a7574e
AM
9033 && gsym != NULL
9034 && strcmp(gsym->name(), ".TOC.") == 0)
9035 {
0e123f69 9036 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
e3a7574e
AM
9037 Reltype prev_rela(preloc - reloc_size);
9038 if ((prev_rela.get_r_info()
9039 == elfcpp::elf_r_info<size>(r_sym,
9040 elfcpp::R_POWERPC_REL16_HA))
9041 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
9042 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
9043 {
dcfc7dd4 9044 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
e3a7574e
AM
9045 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
9046 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
9047
9048 if ((insn1 & 0xffff0000) == addis_2_12
9049 && (insn2 & 0xffff0000) == addi_2_2)
9050 {
9051 insn1 = lis_2 + ha(value + address - 4);
9052 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
9053 insn2 = addi_2_2 + l(value + address - 4);
9054 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
9055 if (relinfo->rr)
9056 {
9057 relinfo->rr->set_strategy(relnum - 1,
9058 Relocatable_relocs::RELOC_SPECIAL);
9059 relinfo->rr->set_strategy(relnum,
9060 Relocatable_relocs::RELOC_SPECIAL);
9061 }
9062 return true;
9063 }
9064 }
9065 }
9066 break;
aba6bc71
AM
9067 }
9068 }
9069
f4baf0d4 9070 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
b80eed39 9071 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
dd93cd0a
AM
9072 switch (r_type)
9073 {
9074 case elfcpp::R_POWERPC_ADDR32:
9075 case elfcpp::R_POWERPC_UADDR32:
9076 if (size == 64)
f4baf0d4 9077 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
9078 break;
9079
9080 case elfcpp::R_POWERPC_REL32:
a680de9a 9081 case elfcpp::R_POWERPC_REL16DX_HA:
dd93cd0a 9082 if (size == 64)
f4baf0d4 9083 overflow = Reloc::CHECK_SIGNED;
dd93cd0a
AM
9084 break;
9085
dd93cd0a 9086 case elfcpp::R_POWERPC_UADDR16:
f4baf0d4 9087 overflow = Reloc::CHECK_BITFIELD;
42cacb20
DE
9088 break;
9089
b80eed39
AM
9090 case elfcpp::R_POWERPC_ADDR16:
9091 // We really should have three separate relocations,
9092 // one for 16-bit data, one for insns with 16-bit signed fields,
9093 // and one for insns with 16-bit unsigned fields.
9094 overflow = Reloc::CHECK_BITFIELD;
9095 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
9096 overflow = Reloc::CHECK_LOW_INSN;
9097 break;
9098
f9c6b907
AM
9099 case elfcpp::R_POWERPC_ADDR16_HI:
9100 case elfcpp::R_POWERPC_ADDR16_HA:
9101 case elfcpp::R_POWERPC_GOT16_HI:
9102 case elfcpp::R_POWERPC_GOT16_HA:
9103 case elfcpp::R_POWERPC_PLT16_HI:
9104 case elfcpp::R_POWERPC_PLT16_HA:
9105 case elfcpp::R_POWERPC_SECTOFF_HI:
9106 case elfcpp::R_POWERPC_SECTOFF_HA:
9107 case elfcpp::R_PPC64_TOC16_HI:
9108 case elfcpp::R_PPC64_TOC16_HA:
9109 case elfcpp::R_PPC64_PLTGOT16_HI:
9110 case elfcpp::R_PPC64_PLTGOT16_HA:
9111 case elfcpp::R_POWERPC_TPREL16_HI:
9112 case elfcpp::R_POWERPC_TPREL16_HA:
9113 case elfcpp::R_POWERPC_DTPREL16_HI:
9114 case elfcpp::R_POWERPC_DTPREL16_HA:
9115 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9116 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9117 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9118 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9119 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9120 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9121 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9122 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9123 case elfcpp::R_POWERPC_REL16_HI:
9124 case elfcpp::R_POWERPC_REL16_HA:
b80eed39
AM
9125 if (size != 32)
9126 overflow = Reloc::CHECK_HIGH_INSN;
9127 break;
9128
dd93cd0a
AM
9129 case elfcpp::R_POWERPC_REL16:
9130 case elfcpp::R_PPC64_TOC16:
9131 case elfcpp::R_POWERPC_GOT16:
9132 case elfcpp::R_POWERPC_SECTOFF:
9133 case elfcpp::R_POWERPC_TPREL16:
9134 case elfcpp::R_POWERPC_DTPREL16:
b80eed39
AM
9135 case elfcpp::R_POWERPC_GOT_TLSGD16:
9136 case elfcpp::R_POWERPC_GOT_TLSLD16:
9137 case elfcpp::R_POWERPC_GOT_TPREL16:
9138 case elfcpp::R_POWERPC_GOT_DTPREL16:
9139 overflow = Reloc::CHECK_LOW_INSN;
9140 break;
9141
9142 case elfcpp::R_POWERPC_ADDR24:
9143 case elfcpp::R_POWERPC_ADDR14:
9144 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9145 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9146 case elfcpp::R_PPC64_ADDR16_DS:
9147 case elfcpp::R_POWERPC_REL24:
9148 case elfcpp::R_PPC_PLTREL24:
9149 case elfcpp::R_PPC_LOCAL24PC:
dd93cd0a
AM
9150 case elfcpp::R_PPC64_TPREL16_DS:
9151 case elfcpp::R_PPC64_DTPREL16_DS:
9152 case elfcpp::R_PPC64_TOC16_DS:
9153 case elfcpp::R_PPC64_GOT16_DS:
9154 case elfcpp::R_PPC64_SECTOFF_DS:
9155 case elfcpp::R_POWERPC_REL14:
9156 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9157 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
f4baf0d4 9158 overflow = Reloc::CHECK_SIGNED;
42cacb20 9159 break;
dd93cd0a 9160 }
42cacb20 9161
dcfc7dd4 9162 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
a680de9a
PB
9163 Insn insn = 0;
9164
b80eed39
AM
9165 if (overflow == Reloc::CHECK_LOW_INSN
9166 || overflow == Reloc::CHECK_HIGH_INSN)
9167 {
a680de9a 9168 insn = elfcpp::Swap<32, big_endian>::readval(iview);
b80eed39 9169
a47622ac
AM
9170 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
9171 overflow = Reloc::CHECK_BITFIELD;
9172 else if (overflow == Reloc::CHECK_LOW_INSN
9173 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
9174 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
9175 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
9176 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
9177 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
9178 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
b80eed39 9179 overflow = Reloc::CHECK_UNSIGNED;
e30880c2
CC
9180 else
9181 overflow = Reloc::CHECK_SIGNED;
b80eed39
AM
9182 }
9183
a680de9a 9184 bool maybe_dq_reloc = false;
3ea0a085 9185 typename Powerpc_relocate_functions<size, big_endian>::Status status
f4baf0d4 9186 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
dd93cd0a
AM
9187 switch (r_type)
9188 {
9189 case elfcpp::R_POWERPC_NONE:
9190 case elfcpp::R_POWERPC_TLS:
9191 case elfcpp::R_POWERPC_GNU_VTINHERIT:
9192 case elfcpp::R_POWERPC_GNU_VTENTRY:
42cacb20
DE
9193 break;
9194
9195 case elfcpp::R_PPC64_ADDR64:
dd93cd0a 9196 case elfcpp::R_PPC64_REL64:
cf43a2fe 9197 case elfcpp::R_PPC64_TOC:
45965137 9198 case elfcpp::R_PPC64_ADDR64_LOCAL:
dd93cd0a
AM
9199 Reloc::addr64(view, value);
9200 break;
9201
9202 case elfcpp::R_POWERPC_TPREL:
9203 case elfcpp::R_POWERPC_DTPREL:
9204 if (size == 64)
9205 Reloc::addr64(view, value);
9206 else
3ea0a085 9207 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
9208 break;
9209
9210 case elfcpp::R_PPC64_UADDR64:
9211 Reloc::addr64_u(view, value);
42cacb20
DE
9212 break;
9213
9214 case elfcpp::R_POWERPC_ADDR32:
3ea0a085 9215 status = Reloc::addr32(view, value, overflow);
dd93cd0a
AM
9216 break;
9217
acc276d8 9218 case elfcpp::R_POWERPC_REL32:
dd93cd0a 9219 case elfcpp::R_POWERPC_UADDR32:
3ea0a085 9220 status = Reloc::addr32_u(view, value, overflow);
dd93cd0a
AM
9221 break;
9222
9223 case elfcpp::R_POWERPC_ADDR24:
9224 case elfcpp::R_POWERPC_REL24:
9225 case elfcpp::R_PPC_PLTREL24:
9226 case elfcpp::R_PPC_LOCAL24PC:
3ea0a085 9227 status = Reloc::addr24(view, value, overflow);
42cacb20
DE
9228 break;
9229
dd93cd0a
AM
9230 case elfcpp::R_POWERPC_GOT_DTPREL16:
9231 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
ec86f434
AM
9232 case elfcpp::R_POWERPC_GOT_TPREL16:
9233 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
dd93cd0a
AM
9234 if (size == 64)
9235 {
ec86f434 9236 // On ppc64 these are all ds form
a680de9a 9237 maybe_dq_reloc = true;
dd93cd0a
AM
9238 break;
9239 }
c25aa1e1 9240 // Fall through.
cf43a2fe 9241 case elfcpp::R_POWERPC_ADDR16:
dd93cd0a 9242 case elfcpp::R_POWERPC_REL16:
cf43a2fe 9243 case elfcpp::R_PPC64_TOC16:
42cacb20 9244 case elfcpp::R_POWERPC_GOT16:
cf43a2fe 9245 case elfcpp::R_POWERPC_SECTOFF:
dd93cd0a
AM
9246 case elfcpp::R_POWERPC_TPREL16:
9247 case elfcpp::R_POWERPC_DTPREL16:
9248 case elfcpp::R_POWERPC_GOT_TLSGD16:
9249 case elfcpp::R_POWERPC_GOT_TLSLD16:
cf43a2fe 9250 case elfcpp::R_POWERPC_ADDR16_LO:
dd93cd0a 9251 case elfcpp::R_POWERPC_REL16_LO:
cf43a2fe 9252 case elfcpp::R_PPC64_TOC16_LO:
42cacb20 9253 case elfcpp::R_POWERPC_GOT16_LO:
cf43a2fe 9254 case elfcpp::R_POWERPC_SECTOFF_LO:
dd93cd0a
AM
9255 case elfcpp::R_POWERPC_TPREL16_LO:
9256 case elfcpp::R_POWERPC_DTPREL16_LO:
9257 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9258 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
a680de9a
PB
9259 if (size == 64)
9260 status = Reloc::addr16(view, value, overflow);
9261 else
9262 maybe_dq_reloc = true;
dd93cd0a
AM
9263 break;
9264
9265 case elfcpp::R_POWERPC_UADDR16:
3ea0a085 9266 status = Reloc::addr16_u(view, value, overflow);
42cacb20
DE
9267 break;
9268
f9c6b907
AM
9269 case elfcpp::R_PPC64_ADDR16_HIGH:
9270 case elfcpp::R_PPC64_TPREL16_HIGH:
9271 case elfcpp::R_PPC64_DTPREL16_HIGH:
9272 if (size == 32)
9273 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
9274 goto unsupp;
d8e90251 9275 // Fall through.
cf43a2fe 9276 case elfcpp::R_POWERPC_ADDR16_HI:
dd93cd0a 9277 case elfcpp::R_POWERPC_REL16_HI:
cf43a2fe 9278 case elfcpp::R_PPC64_TOC16_HI:
42cacb20 9279 case elfcpp::R_POWERPC_GOT16_HI:
cf43a2fe 9280 case elfcpp::R_POWERPC_SECTOFF_HI:
dd93cd0a
AM
9281 case elfcpp::R_POWERPC_TPREL16_HI:
9282 case elfcpp::R_POWERPC_DTPREL16_HI:
9283 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9284 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9285 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9286 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9287 Reloc::addr16_hi(view, value);
42cacb20
DE
9288 break;
9289
f9c6b907
AM
9290 case elfcpp::R_PPC64_ADDR16_HIGHA:
9291 case elfcpp::R_PPC64_TPREL16_HIGHA:
9292 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9293 if (size == 32)
9294 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
9295 goto unsupp;
d8e90251 9296 // Fall through.
cf43a2fe 9297 case elfcpp::R_POWERPC_ADDR16_HA:
dd93cd0a 9298 case elfcpp::R_POWERPC_REL16_HA:
cf43a2fe 9299 case elfcpp::R_PPC64_TOC16_HA:
42cacb20 9300 case elfcpp::R_POWERPC_GOT16_HA:
cf43a2fe 9301 case elfcpp::R_POWERPC_SECTOFF_HA:
dd93cd0a
AM
9302 case elfcpp::R_POWERPC_TPREL16_HA:
9303 case elfcpp::R_POWERPC_DTPREL16_HA:
9304 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9305 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9306 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9307 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9308 Reloc::addr16_ha(view, value);
42cacb20
DE
9309 break;
9310
a680de9a
PB
9311 case elfcpp::R_POWERPC_REL16DX_HA:
9312 status = Reloc::addr16dx_ha(view, value, overflow);
9313 break;
9314
dd93cd0a
AM
9315 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9316 if (size == 32)
9317 // R_PPC_EMB_NADDR16_LO
9318 goto unsupp;
d8e90251 9319 // Fall through.
dd93cd0a
AM
9320 case elfcpp::R_PPC64_ADDR16_HIGHER:
9321 case elfcpp::R_PPC64_TPREL16_HIGHER:
9322 Reloc::addr16_hi2(view, value);
42cacb20
DE
9323 break;
9324
dd93cd0a
AM
9325 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9326 if (size == 32)
9327 // R_PPC_EMB_NADDR16_HI
9328 goto unsupp;
d8e90251 9329 // Fall through.
dd93cd0a
AM
9330 case elfcpp::R_PPC64_ADDR16_HIGHERA:
9331 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9332 Reloc::addr16_ha2(view, value);
42cacb20
DE
9333 break;
9334
dd93cd0a
AM
9335 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9336 if (size == 32)
9337 // R_PPC_EMB_NADDR16_HA
9338 goto unsupp;
d8e90251 9339 // Fall through.
dd93cd0a
AM
9340 case elfcpp::R_PPC64_ADDR16_HIGHEST:
9341 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9342 Reloc::addr16_hi3(view, value);
42cacb20
DE
9343 break;
9344
dd93cd0a
AM
9345 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9346 if (size == 32)
9347 // R_PPC_EMB_SDAI16
9348 goto unsupp;
d8e90251 9349 // Fall through.
dd93cd0a
AM
9350 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
9351 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9352 Reloc::addr16_ha3(view, value);
9353 break;
9354
9355 case elfcpp::R_PPC64_DTPREL16_DS:
9356 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9357 if (size == 32)
9358 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
9359 goto unsupp;
d8e90251 9360 // Fall through.
dd93cd0a
AM
9361 case elfcpp::R_PPC64_TPREL16_DS:
9362 case elfcpp::R_PPC64_TPREL16_LO_DS:
9363 if (size == 32)
9364 // R_PPC_TLSGD, R_PPC_TLSLD
9365 break;
d8e90251 9366 // Fall through.
cf43a2fe
AM
9367 case elfcpp::R_PPC64_ADDR16_DS:
9368 case elfcpp::R_PPC64_ADDR16_LO_DS:
42cacb20
DE
9369 case elfcpp::R_PPC64_TOC16_DS:
9370 case elfcpp::R_PPC64_TOC16_LO_DS:
cf43a2fe
AM
9371 case elfcpp::R_PPC64_GOT16_DS:
9372 case elfcpp::R_PPC64_GOT16_LO_DS:
9373 case elfcpp::R_PPC64_SECTOFF_DS:
9374 case elfcpp::R_PPC64_SECTOFF_LO_DS:
a680de9a 9375 maybe_dq_reloc = true;
dd93cd0a
AM
9376 break;
9377
9378 case elfcpp::R_POWERPC_ADDR14:
9379 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
9380 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
9381 case elfcpp::R_POWERPC_REL14:
9382 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9383 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3ea0a085 9384 status = Reloc::addr14(view, value, overflow);
42cacb20
DE
9385 break;
9386
9387 case elfcpp::R_POWERPC_COPY:
9388 case elfcpp::R_POWERPC_GLOB_DAT:
9389 case elfcpp::R_POWERPC_JMP_SLOT:
9390 case elfcpp::R_POWERPC_RELATIVE:
42cacb20 9391 case elfcpp::R_POWERPC_DTPMOD:
dd93cd0a
AM
9392 case elfcpp::R_PPC64_JMP_IREL:
9393 case elfcpp::R_POWERPC_IRELATIVE:
42cacb20
DE
9394 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9395 _("unexpected reloc %u in object file"),
9396 r_type);
9397 break;
9398
7e57d19e 9399 case elfcpp::R_PPC64_TOCSAVE:
dd93cd0a 9400 if (size == 32)
7e57d19e 9401 // R_PPC_EMB_SDA21
dd93cd0a
AM
9402 goto unsupp;
9403 else
9404 {
7e57d19e
AM
9405 Symbol_location loc;
9406 loc.object = relinfo->object;
9407 loc.shndx = relinfo->data_shndx;
9408 loc.offset = rela.get_r_offset();
9409 Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
9410 if (p != target->tocsave_loc().end())
9411 {
9412 // If we've generated plt calls using this tocsave, then
9413 // the nop needs to be changed to save r2.
9414 Insn* iview = reinterpret_cast<Insn*>(view);
9415 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
9416 elfcpp::Swap<32, big_endian>::
9417 writeval(iview, std_2_1 + target->stk_toc());
9418 }
dd93cd0a
AM
9419 }
9420 break;
9421
9422 case elfcpp::R_PPC_EMB_SDA2I16:
9423 case elfcpp::R_PPC_EMB_SDA2REL:
9424 if (size == 32)
9425 goto unsupp;
9426 // R_PPC64_TLSGD, R_PPC64_TLSLD
6ce78956
AM
9427 break;
9428
dd93cd0a
AM
9429 case elfcpp::R_POWERPC_PLT32:
9430 case elfcpp::R_POWERPC_PLTREL32:
9431 case elfcpp::R_POWERPC_PLT16_LO:
9432 case elfcpp::R_POWERPC_PLT16_HI:
9433 case elfcpp::R_POWERPC_PLT16_HA:
9434 case elfcpp::R_PPC_SDAREL16:
9435 case elfcpp::R_POWERPC_ADDR30:
9436 case elfcpp::R_PPC64_PLT64:
9437 case elfcpp::R_PPC64_PLTREL64:
9438 case elfcpp::R_PPC64_PLTGOT16:
9439 case elfcpp::R_PPC64_PLTGOT16_LO:
9440 case elfcpp::R_PPC64_PLTGOT16_HI:
9441 case elfcpp::R_PPC64_PLTGOT16_HA:
9442 case elfcpp::R_PPC64_PLT16_LO_DS:
9443 case elfcpp::R_PPC64_PLTGOT16_DS:
9444 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
dd93cd0a
AM
9445 case elfcpp::R_PPC_EMB_RELSDA:
9446 case elfcpp::R_PPC_TOC16:
42cacb20 9447 default:
dd93cd0a 9448 unsupp:
42cacb20
DE
9449 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9450 _("unsupported reloc %u"),
9451 r_type);
9452 break;
9453 }
a680de9a
PB
9454
9455 if (maybe_dq_reloc)
9456 {
9457 if (insn == 0)
9458 insn = elfcpp::Swap<32, big_endian>::readval(iview);
9459
9460 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
9461 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
9462 && (insn & 3) == 1))
9463 status = Reloc::addr16_dq(view, value, overflow);
9464 else if (size == 64
9465 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
9466 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
9467 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
9468 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
9469 status = Reloc::addr16_ds(view, value, overflow);
9470 else
9471 status = Reloc::addr16(view, value, overflow);
9472 }
9473
0cfb0717 9474 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
3ffaac20
AM
9475 && (has_stub_value
9476 || !(gsym != NULL
282c9750 9477 && gsym->is_undefined()
3ffaac20 9478 && is_branch_reloc(r_type))))
0cfb0717
AM
9479 {
9480 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9481 _("relocation overflow"));
9482 if (has_stub_value)
9483 gold_info(_("try relinking with a smaller --stub-group-size"));
9484 }
42cacb20
DE
9485
9486 return true;
9487}
9488
42cacb20
DE
9489// Relocate section data.
9490
9491template<int size, bool big_endian>
9492void
9493Target_powerpc<size, big_endian>::relocate_section(
d83ce4e3
AM
9494 const Relocate_info<size, big_endian>* relinfo,
9495 unsigned int sh_type,
9496 const unsigned char* prelocs,
9497 size_t reloc_count,
9498 Output_section* output_section,
9499 bool needs_special_offset_handling,
9500 unsigned char* view,
c9269dff 9501 Address address,
d83ce4e3
AM
9502 section_size_type view_size,
9503 const Reloc_symbol_changes* reloc_symbol_changes)
42cacb20
DE
9504{
9505 typedef Target_powerpc<size, big_endian> Powerpc;
9506 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
168a4726
AM
9507 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
9508 Powerpc_comdat_behavior;
4d625b70
CC
9509 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9510 Classify_reloc;
42cacb20
DE
9511
9512 gold_assert(sh_type == elfcpp::SHT_RELA);
9513
4d625b70
CC
9514 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
9515 Powerpc_comdat_behavior, Classify_reloc>(
42cacb20
DE
9516 relinfo,
9517 this,
9518 prelocs,
9519 reloc_count,
9520 output_section,
9521 needs_special_offset_handling,
9522 view,
9523 address,
364c7fa5
ILT
9524 view_size,
9525 reloc_symbol_changes);
42cacb20
DE
9526}
9527
4d625b70 9528template<int size, bool big_endian>
cf43a2fe 9529class Powerpc_scan_relocatable_reloc
42cacb20 9530{
cf43a2fe 9531public:
0e123f69
AM
9532 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9533 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4d625b70
CC
9534 static const int sh_type = elfcpp::SHT_RELA;
9535
9536 // Return the symbol referred to by the relocation.
9537 static inline unsigned int
9538 get_r_sym(const Reltype* reloc)
9539 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
9540
9541 // Return the type of the relocation.
9542 static inline unsigned int
9543 get_r_type(const Reltype* reloc)
9544 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
9545
cf43a2fe
AM
9546 // Return the strategy to use for a local symbol which is not a
9547 // section symbol, given the relocation type.
9548 inline Relocatable_relocs::Reloc_strategy
9549 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
9550 {
9551 if (r_type == 0 && r_sym == 0)
9552 return Relocatable_relocs::RELOC_DISCARD;
9553 return Relocatable_relocs::RELOC_COPY;
9554 }
9555
9556 // Return the strategy to use for a local symbol which is a section
9557 // symbol, given the relocation type.
9558 inline Relocatable_relocs::Reloc_strategy
9559 local_section_strategy(unsigned int, Relobj*)
9560 {
9561 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
9562 }
9563
9564 // Return the strategy to use for a global symbol, given the
9565 // relocation type, the object, and the symbol index.
9566 inline Relocatable_relocs::Reloc_strategy
9567 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9568 {
9569 if (r_type == elfcpp::R_PPC_PLTREL24)
9570 return Relocatable_relocs::RELOC_SPECIAL;
9571 return Relocatable_relocs::RELOC_COPY;
9572 }
9573};
42cacb20
DE
9574
9575// Scan the relocs during a relocatable link.
9576
9577template<int size, bool big_endian>
9578void
9579Target_powerpc<size, big_endian>::scan_relocatable_relocs(
d83ce4e3
AM
9580 Symbol_table* symtab,
9581 Layout* layout,
9582 Sized_relobj_file<size, big_endian>* object,
9583 unsigned int data_shndx,
9584 unsigned int sh_type,
9585 const unsigned char* prelocs,
9586 size_t reloc_count,
9587 Output_section* output_section,
9588 bool needs_special_offset_handling,
9589 size_t local_symbol_count,
9590 const unsigned char* plocal_symbols,
9591 Relocatable_relocs* rr)
42cacb20 9592{
4d625b70
CC
9593 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9594
42cacb20
DE
9595 gold_assert(sh_type == elfcpp::SHT_RELA);
9596
4d625b70 9597 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
42cacb20
DE
9598 symtab,
9599 layout,
9600 object,
9601 data_shndx,
9602 prelocs,
9603 reloc_count,
9604 output_section,
9605 needs_special_offset_handling,
9606 local_symbol_count,
9607 plocal_symbols,
9608 rr);
9609}
9610
4d625b70
CC
9611// Scan the relocs for --emit-relocs.
9612
9613template<int size, bool big_endian>
9614void
9615Target_powerpc<size, big_endian>::emit_relocs_scan(
9616 Symbol_table* symtab,
9617 Layout* layout,
9618 Sized_relobj_file<size, big_endian>* object,
9619 unsigned int data_shndx,
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 size_t local_symbol_count,
9626 const unsigned char* plocal_syms,
9627 Relocatable_relocs* rr)
9628{
9629 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9630 Classify_reloc;
9631 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9632 Emit_relocs_strategy;
9633
9634 gold_assert(sh_type == elfcpp::SHT_RELA);
9635
9636 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9637 symtab,
9638 layout,
9639 object,
9640 data_shndx,
9641 prelocs,
9642 reloc_count,
9643 output_section,
9644 needs_special_offset_handling,
9645 local_symbol_count,
9646 plocal_syms,
9647 rr);
9648}
9649
7404fe1b 9650// Emit relocations for a section.
dd93cd0a
AM
9651// This is a modified version of the function by the same name in
9652// target-reloc.h. Using relocate_special_relocatable for
9653// R_PPC_PLTREL24 would require duplication of the entire body of the
9654// loop, so we may as well duplicate the whole thing.
42cacb20
DE
9655
9656template<int size, bool big_endian>
9657void
7404fe1b 9658Target_powerpc<size, big_endian>::relocate_relocs(
42cacb20
DE
9659 const Relocate_info<size, big_endian>* relinfo,
9660 unsigned int sh_type,
9661 const unsigned char* prelocs,
9662 size_t reloc_count,
9663 Output_section* output_section,
62fe925a 9664 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
cf43a2fe 9665 unsigned char*,
dd93cd0a 9666 Address view_address,
cf43a2fe 9667 section_size_type,
42cacb20
DE
9668 unsigned char* reloc_view,
9669 section_size_type reloc_view_size)
9670{
9671 gold_assert(sh_type == elfcpp::SHT_RELA);
9672
0e123f69
AM
9673 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9674 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9675 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
dcfc7dd4
AM
9676 // Offset from start of insn to d-field reloc.
9677 const int d_offset = big_endian ? 2 : 0;
cf43a2fe
AM
9678
9679 Powerpc_relobj<size, big_endian>* const object
9680 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9681 const unsigned int local_count = object->local_symbol_count();
9682 unsigned int got2_shndx = object->got2_shndx();
c9269dff 9683 Address got2_addend = 0;
cf43a2fe 9684 if (got2_shndx != 0)
c9269dff
AM
9685 {
9686 got2_addend = object->get_output_section_offset(got2_shndx);
9687 gold_assert(got2_addend != invalid_address);
9688 }
cf43a2fe
AM
9689
9690 unsigned char* pwrite = reloc_view;
7404fe1b 9691 bool zap_next = false;
cf43a2fe
AM
9692 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9693 {
91a65d2f 9694 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
cf43a2fe
AM
9695 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9696 continue;
9697
9698 Reltype reloc(prelocs);
9699 Reltype_write reloc_write(pwrite);
9700
7404fe1b 9701 Address offset = reloc.get_r_offset();
cf43a2fe 9702 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7404fe1b
AM
9703 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9704 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9705 const unsigned int orig_r_sym = r_sym;
9706 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9707 = reloc.get_r_addend();
9708 const Symbol* gsym = NULL;
9709
9710 if (zap_next)
9711 {
9712 // We could arrange to discard these and other relocs for
9713 // tls optimised sequences in the strategy methods, but for
9714 // now do as BFD ld does.
9715 r_type = elfcpp::R_POWERPC_NONE;
9716 zap_next = false;
9717 }
cf43a2fe
AM
9718
9719 // Get the new symbol index.
9215b98b 9720 Output_section* os = NULL;
cf43a2fe
AM
9721 if (r_sym < local_count)
9722 {
9723 switch (strategy)
9724 {
9725 case Relocatable_relocs::RELOC_COPY:
9726 case Relocatable_relocs::RELOC_SPECIAL:
7404fe1b 9727 if (r_sym != 0)
dd93cd0a 9728 {
7404fe1b
AM
9729 r_sym = object->symtab_index(r_sym);
9730 gold_assert(r_sym != -1U);
dd93cd0a 9731 }
cf43a2fe
AM
9732 break;
9733
9734 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9735 {
9736 // We are adjusting a section symbol. We need to find
9737 // the symbol table index of the section symbol for
9738 // the output section corresponding to input section
9739 // in which this symbol is defined.
9740 gold_assert(r_sym < local_count);
9741 bool is_ordinary;
9742 unsigned int shndx =
9743 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9744 gold_assert(is_ordinary);
9215b98b 9745 os = object->output_section(shndx);
cf43a2fe
AM
9746 gold_assert(os != NULL);
9747 gold_assert(os->needs_symtab_index());
7404fe1b 9748 r_sym = os->symtab_index();
cf43a2fe
AM
9749 }
9750 break;
9751
9752 default:
9753 gold_unreachable();
9754 }
9755 }
9756 else
9757 {
7404fe1b 9758 gsym = object->global_symbol(r_sym);
cf43a2fe
AM
9759 gold_assert(gsym != NULL);
9760 if (gsym->is_forwarder())
9761 gsym = relinfo->symtab->resolve_forwards(gsym);
9762
9763 gold_assert(gsym->has_symtab_index());
7404fe1b 9764 r_sym = gsym->symtab_index();
cf43a2fe
AM
9765 }
9766
9767 // Get the new offset--the location in the output section where
9768 // this relocation should be applied.
cf43a2fe 9769 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9770 offset += offset_in_output_section;
cf43a2fe
AM
9771 else
9772 {
c9269dff
AM
9773 section_offset_type sot_offset =
9774 convert_types<section_offset_type, Address>(offset);
cf43a2fe 9775 section_offset_type new_sot_offset =
c9269dff
AM
9776 output_section->output_offset(object, relinfo->data_shndx,
9777 sot_offset);
cf43a2fe 9778 gold_assert(new_sot_offset != -1);
7404fe1b 9779 offset = new_sot_offset;
cf43a2fe
AM
9780 }
9781
dd93cd0a
AM
9782 // In an object file, r_offset is an offset within the section.
9783 // In an executable or dynamic object, generated by
9784 // --emit-relocs, r_offset is an absolute address.
7404fe1b 9785 if (!parameters->options().relocatable())
dd93cd0a 9786 {
7404fe1b 9787 offset += view_address;
dd93cd0a 9788 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7404fe1b 9789 offset -= offset_in_output_section;
dd93cd0a
AM
9790 }
9791
cf43a2fe 9792 // Handle the reloc addend based on the strategy.
cf43a2fe
AM
9793 if (strategy == Relocatable_relocs::RELOC_COPY)
9794 ;
9795 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9796 {
7404fe1b 9797 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9215b98b
AM
9798 gold_assert(os != NULL);
9799 addend = psymval->value(object, addend) - os->address();
cf43a2fe
AM
9800 }
9801 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9802 {
e3a7574e
AM
9803 if (size == 32)
9804 {
9805 if (addend >= 32768)
9806 addend += got2_addend;
9807 }
9808 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9809 {
9810 r_type = elfcpp::R_POWERPC_ADDR16_HA;
dcfc7dd4 9811 addend -= d_offset;
e3a7574e
AM
9812 }
9813 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9814 {
9815 r_type = elfcpp::R_POWERPC_ADDR16_LO;
dcfc7dd4 9816 addend -= d_offset + 4;
e3a7574e 9817 }
cf43a2fe
AM
9818 }
9819 else
9820 gold_unreachable();
9821
7404fe1b
AM
9822 if (!parameters->options().relocatable())
9823 {
9824 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9825 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9826 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9827 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9828 {
9829 // First instruction of a global dynamic sequence,
9830 // arg setup insn.
9831 const bool final = gsym == NULL || gsym->final_value_is_known();
9832 switch (this->optimize_tls_gd(final))
9833 {
9834 case tls::TLSOPT_TO_IE:
9835 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9836 - elfcpp::R_POWERPC_GOT_TLSGD16);
9837 break;
9838 case tls::TLSOPT_TO_LE:
9839 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9840 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9841 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9842 else
9843 {
9844 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9845 offset -= d_offset;
7404fe1b
AM
9846 }
9847 break;
9848 default:
9849 break;
9850 }
9851 }
9852 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9853 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9854 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9855 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9856 {
9857 // First instruction of a local dynamic sequence,
9858 // arg setup insn.
9859 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9860 {
9861 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9862 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9863 {
9864 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9865 const Output_section* os = relinfo->layout->tls_segment()
9866 ->first_section();
9867 gold_assert(os != NULL);
9868 gold_assert(os->needs_symtab_index());
9869 r_sym = os->symtab_index();
9870 addend = dtp_offset;
9871 }
9872 else
9873 {
9874 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9875 offset -= d_offset;
7404fe1b
AM
9876 }
9877 }
9878 }
9879 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9880 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9881 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9882 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9883 {
9884 // First instruction of initial exec sequence.
9885 const bool final = gsym == NULL || gsym->final_value_is_known();
9886 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9887 {
9888 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9889 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9890 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9891 else
9892 {
9893 r_type = elfcpp::R_POWERPC_NONE;
dcfc7dd4 9894 offset -= d_offset;
7404fe1b
AM
9895 }
9896 }
9897 }
9898 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9899 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9900 {
9901 // Second instruction of a global dynamic sequence,
9902 // the __tls_get_addr call
9903 const bool final = gsym == NULL || gsym->final_value_is_known();
9904 switch (this->optimize_tls_gd(final))
9905 {
9906 case tls::TLSOPT_TO_IE:
9907 r_type = elfcpp::R_POWERPC_NONE;
9908 zap_next = true;
9909 break;
9910 case tls::TLSOPT_TO_LE:
9911 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9912 offset += d_offset;
7404fe1b
AM
9913 zap_next = true;
9914 break;
9915 default:
9916 break;
9917 }
9918 }
9919 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9920 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9921 {
9922 // Second instruction of a local dynamic sequence,
9923 // the __tls_get_addr call
9924 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9925 {
9926 const Output_section* os = relinfo->layout->tls_segment()
9927 ->first_section();
9928 gold_assert(os != NULL);
9929 gold_assert(os->needs_symtab_index());
9930 r_sym = os->symtab_index();
9931 addend = dtp_offset;
9932 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9933 offset += d_offset;
7404fe1b
AM
9934 zap_next = true;
9935 }
9936 }
9937 else if (r_type == elfcpp::R_POWERPC_TLS)
9938 {
9939 // Second instruction of an initial exec sequence
9940 const bool final = gsym == NULL || gsym->final_value_is_known();
9941 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9942 {
9943 r_type = elfcpp::R_POWERPC_TPREL16_LO;
dcfc7dd4 9944 offset += d_offset;
7404fe1b
AM
9945 }
9946 }
9947 }
9948
9949 reloc_write.put_r_offset(offset);
9950 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9951 reloc_write.put_r_addend(addend);
cf43a2fe
AM
9952
9953 pwrite += reloc_size;
9954 }
9955
9956 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9957 == reloc_view_size);
42cacb20
DE
9958}
9959
ec661b9d 9960// Return the value to use for a dynamic symbol which requires special
42cacb20
DE
9961// treatment. This is how we support equality comparisons of function
9962// pointers across shared library boundaries, as described in the
9963// processor specific ABI supplement.
9964
9965template<int size, bool big_endian>
9966uint64_t
9967Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9968{
cf43a2fe
AM
9969 if (size == 32)
9970 {
9971 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
ec661b9d
AM
9972 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9973 p != this->stub_tables_.end();
9974 ++p)
9975 {
7e57d19e
AM
9976 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
9977 = (*p)->find_plt_call_entry(gsym);
9978 if (ent != NULL)
9979 return (*p)->stub_address() + ent->off_;
ec661b9d 9980 }
c9824451 9981 }
9055360d
AM
9982 else if (this->abiversion() >= 2)
9983 {
faa2211d
AM
9984 Address off = this->glink_section()->find_global_entry(gsym);
9985 if (off != invalid_address)
9055360d
AM
9986 return this->glink_section()->global_entry_address() + off;
9987 }
ec661b9d 9988 gold_unreachable();
c9824451
AM
9989}
9990
9991// Return the PLT address to use for a local symbol.
9992template<int size, bool big_endian>
9993uint64_t
9994Target_powerpc<size, big_endian>::do_plt_address_for_local(
9995 const Relobj* object,
9996 unsigned int symndx) const
9997{
9998 if (size == 32)
9999 {
10000 const Sized_relobj<size, big_endian>* relobj
10001 = static_cast<const Sized_relobj<size, big_endian>*>(object);
ec661b9d
AM
10002 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10003 p != this->stub_tables_.end();
10004 ++p)
10005 {
7e57d19e
AM
10006 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10007 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
10008 if (ent != NULL)
10009 return (*p)->stub_address() + ent->off_;
ec661b9d 10010 }
c9824451 10011 }
ec661b9d 10012 gold_unreachable();
c9824451
AM
10013}
10014
10015// Return the PLT address to use for a global symbol.
10016template<int size, bool big_endian>
10017uint64_t
10018Target_powerpc<size, big_endian>::do_plt_address_for_global(
10019 const Symbol* gsym) const
10020{
10021 if (size == 32)
10022 {
ec661b9d
AM
10023 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
10024 p != this->stub_tables_.end();
10025 ++p)
10026 {
7e57d19e
AM
10027 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
10028 = (*p)->find_plt_call_entry(gsym);
10029 if (ent != NULL)
10030 return (*p)->stub_address() + ent->off_;
ec661b9d 10031 }
cf43a2fe 10032 }
9055360d
AM
10033 else if (this->abiversion() >= 2)
10034 {
faa2211d
AM
10035 Address off = this->glink_section()->find_global_entry(gsym);
10036 if (off != invalid_address)
9055360d
AM
10037 return this->glink_section()->global_entry_address() + off;
10038 }
ec661b9d 10039 gold_unreachable();
42cacb20
DE
10040}
10041
bd73a62d
AM
10042// Return the offset to use for the GOT_INDX'th got entry which is
10043// for a local tls symbol specified by OBJECT, SYMNDX.
10044template<int size, bool big_endian>
10045int64_t
10046Target_powerpc<size, big_endian>::do_tls_offset_for_local(
10047 const Relobj* object,
10048 unsigned int symndx,
10049 unsigned int got_indx) const
10050{
10051 const Powerpc_relobj<size, big_endian>* ppc_object
10052 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
10053 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
10054 {
10055 for (Got_type got_type = GOT_TYPE_TLSGD;
10056 got_type <= GOT_TYPE_TPREL;
10057 got_type = Got_type(got_type + 1))
10058 if (ppc_object->local_has_got_offset(symndx, got_type))
10059 {
10060 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
10061 if (got_type == GOT_TYPE_TLSGD)
10062 off += size / 8;
10063 if (off == got_indx * (size / 8))
10064 {
10065 if (got_type == GOT_TYPE_TPREL)
10066 return -tp_offset;
10067 else
10068 return -dtp_offset;
10069 }
10070 }
10071 }
10072 gold_unreachable();
10073}
10074
10075// Return the offset to use for the GOT_INDX'th got entry which is
10076// for global tls symbol GSYM.
10077template<int size, bool big_endian>
10078int64_t
10079Target_powerpc<size, big_endian>::do_tls_offset_for_global(
10080 Symbol* gsym,
10081 unsigned int got_indx) const
10082{
10083 if (gsym->type() == elfcpp::STT_TLS)
10084 {
10085 for (Got_type got_type = GOT_TYPE_TLSGD;
10086 got_type <= GOT_TYPE_TPREL;
10087 got_type = Got_type(got_type + 1))
10088 if (gsym->has_got_offset(got_type))
10089 {
10090 unsigned int off = gsym->got_offset(got_type);
10091 if (got_type == GOT_TYPE_TLSGD)
10092 off += size / 8;
10093 if (off == got_indx * (size / 8))
10094 {
10095 if (got_type == GOT_TYPE_TPREL)
10096 return -tp_offset;
10097 else
10098 return -dtp_offset;
10099 }
10100 }
10101 }
10102 gold_unreachable();
10103}
10104
42cacb20
DE
10105// The selector for powerpc object files.
10106
10107template<int size, bool big_endian>
10108class Target_selector_powerpc : public Target_selector
10109{
10110public:
10111 Target_selector_powerpc()
edc27beb
AM
10112 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
10113 size, big_endian,
03ef7571
ILT
10114 (size == 64
10115 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
10116 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
10117 (size == 64
10118 ? (big_endian ? "elf64ppc" : "elf64lppc")
10119 : (big_endian ? "elf32ppc" : "elf32lppc")))
42cacb20
DE
10120 { }
10121
2e702c99
RM
10122 virtual Target*
10123 do_instantiate_target()
7f055c20 10124 { return new Target_powerpc<size, big_endian>(); }
42cacb20
DE
10125};
10126
10127Target_selector_powerpc<32, true> target_selector_ppc32;
10128Target_selector_powerpc<32, false> target_selector_ppc32le;
10129Target_selector_powerpc<64, true> target_selector_ppc64;
10130Target_selector_powerpc<64, false> target_selector_ppc64le;
10131
decdd3bc
AM
10132// Instantiate these constants for -O0
10133template<int size, bool big_endian>
10134const int Output_data_glink<size, big_endian>::pltresolve_size;
10135template<int size, bool big_endian>
9055360d
AM
10136const typename Output_data_glink<size, big_endian>::Address
10137 Output_data_glink<size, big_endian>::invalid_address;
10138template<int size, bool big_endian>
decdd3bc
AM
10139const typename Stub_table<size, big_endian>::Address
10140 Stub_table<size, big_endian>::invalid_address;
10141template<int size, bool big_endian>
10142const typename Target_powerpc<size, big_endian>::Address
10143 Target_powerpc<size, big_endian>::invalid_address;
10144
42cacb20 10145} // End anonymous namespace.
This page took 0.944183 seconds and 4 git commands to generate.