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