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