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