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