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