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