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