Move uinteger_pow gdb/valarith.c to gdb/utils.c and make it public
[binutils-gdb.git] / gold / powerpc.cc
blobb0d6a74bec5ac477918091190ff906d1a417fbbc
1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2020 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
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.
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.
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.
24 #include "gold.h"
26 #include <set>
27 #include <algorithm>
28 #include "elfcpp.h"
29 #include "dwarf.h"
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"
43 #include "gc.h"
44 #include "attributes.h"
46 namespace
49 using namespace gold;
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
60 template<int size, bool big_endian>
61 class Output_data_glink;
63 template<int size, bool big_endian>
64 class Stub_table;
66 template<int size, bool big_endian>
67 class Output_data_save_res;
69 template<int size, bool big_endian>
70 class Target_powerpc;
72 struct Stub_table_owner
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
94 public:
95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
108 this->set_abiversion(0);
111 ~Powerpc_relobj()
112 { delete this->attributes_section_data_; }
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
129 return this->toc_;
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
164 // The .got2 section shndx.
165 unsigned int
166 got2_shndx() const
168 if (size == 32)
169 return this->special_;
170 else
171 return 0;
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
192 // Return section and offset of function entry for .opd + R_OFF.
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
199 if (value != NULL)
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
260 void
261 do_read_relocs(Read_relocs_data*);
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
271 if (size == 64 && this->opd_shndx() != 0)
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
279 return true;
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
289 add_reference(Relobj* src_obj,
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
308 void
309 process_gc_mark(Symbol_table* symtab)
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
314 unsigned int shndx = this->opd_ent_[i].shndx;
315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
319 // Return offset in output GOT section that this object will use
320 // as a TOC pointer. Won't be just a constant with multi-toc support.
321 Address
322 toc_base_offset() const
323 { return 0x8000; }
325 void
326 set_has_small_toc_reloc()
327 { has_small_toc_reloc_ = true; }
329 bool
330 has_small_toc_reloc() const
331 { return has_small_toc_reloc_; }
333 void
334 set_has_14bit_branch(unsigned int shndx)
336 if (shndx >= this->has14_.size())
337 this->has14_.resize(shndx + 1);
338 this->has14_[shndx] = true;
341 bool
342 has_14bit_branch(unsigned int shndx) const
343 { return shndx < this->has14_.size() && this->has14_[shndx]; }
345 void
346 set_stub_table(unsigned int shndx, unsigned int stub_index)
348 if (shndx >= this->stub_table_index_.size())
349 this->stub_table_index_.resize(shndx + 1, -1);
350 this->stub_table_index_[shndx] = stub_index;
353 Stub_table<size, big_endian>*
354 stub_table(unsigned int shndx)
356 if (shndx < this->stub_table_index_.size())
358 Target_powerpc<size, big_endian>* target
359 = static_cast<Target_powerpc<size, big_endian>*>(
360 parameters->sized_target<size, big_endian>());
361 unsigned int indx = this->stub_table_index_[shndx];
362 if (indx < target->stub_tables().size())
363 return target->stub_tables()[indx];
365 return NULL;
368 void
369 clear_stub_table()
371 this->stub_table_index_.clear();
374 uint32_t
375 uniq() const
376 { return this->uniq_; }
379 abiversion() const
380 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
382 // Set ABI version for input and output
383 void
384 set_abiversion(int ver);
386 unsigned int
387 st_other (unsigned int symndx) const
389 return this->st_other_[symndx];
392 unsigned int
393 ppc64_local_entry_offset(const Symbol* sym) const
394 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
396 unsigned int
397 ppc64_local_entry_offset(unsigned int symndx) const
398 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
400 bool
401 ppc64_needs_toc(const Symbol* sym) const
402 { return sym->nonvis() > 1 << 3; }
404 bool
405 ppc64_needs_toc(unsigned int symndx) const
406 { return this->st_other_[symndx] > 1 << 5; }
408 // The contents of the .gnu.attributes section if there is one.
409 const Attributes_section_data*
410 attributes_section_data() const
411 { return this->attributes_section_data_; }
413 private:
414 struct Opd_ent
416 unsigned int shndx;
417 bool discard : 1;
418 bool gc_mark : 1;
419 Address off;
422 // Return index into opd_ent_ array for .opd entry at OFF.
423 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
424 // apart when the language doesn't use the last 8-byte word, the
425 // environment pointer. Thus dividing the entry section offset by
426 // 16 will give an index into opd_ent_ that works for either layout
427 // of .opd. (It leaves some elements of the vector unused when .opd
428 // entries are spaced 24 bytes apart, but we don't know the spacing
429 // until relocations are processed, and in any case it is possible
430 // for an object to have some entries spaced 16 bytes apart and
431 // others 24 bytes apart.)
432 size_t
433 opd_ent_ndx(size_t off) const
434 { return off >> 4;}
436 // Per object unique identifier
437 uint32_t uniq_;
439 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
440 unsigned int special_;
442 // For 64-bit the .rela.toc and .toc section shdnx.
443 unsigned int relatoc_;
444 unsigned int toc_;
446 // For 64-bit, whether this object uses small model relocs to access
447 // the toc.
448 bool has_small_toc_reloc_;
450 // Set at the start of gc_process_relocs, when we know opd_ent_
451 // vector is valid. The flag could be made atomic and set in
452 // do_read_relocs with memory_order_release and then tested with
453 // memory_order_acquire, potentially resulting in fewer entries in
454 // access_from_map_.
455 bool opd_valid_;
457 // Header e_flags
458 elfcpp::Elf_Word e_flags_;
460 // For 64-bit, an array with one entry per 64-bit word in the .toc
461 // section, set if accesses using that word cannot be optimised.
462 std::vector<bool> no_toc_opt_;
464 // The first 8-byte word of an OPD entry gives the address of the
465 // entry point of the function. Relocatable object files have a
466 // relocation on this word. The following vector records the
467 // section and offset specified by these relocations.
468 std::vector<Opd_ent> opd_ent_;
470 // References made to this object's .opd section when running
471 // gc_process_relocs for another object, before the opd_ent_ vector
472 // is valid for this object.
473 Access_from access_from_map_;
475 // Whether input section has a 14-bit branch reloc.
476 std::vector<bool> has14_;
478 // The stub table to use for a given input section.
479 std::vector<unsigned int> stub_table_index_;
481 // ELF st_other field for local symbols.
482 std::vector<unsigned char> st_other_;
484 // Object attributes if there is a .gnu.attributes section.
485 Attributes_section_data* attributes_section_data_;
488 template<int size, bool big_endian>
489 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
491 public:
492 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
494 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
495 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
496 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
497 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
498 attributes_section_data_(NULL)
500 this->set_abiversion(0);
503 ~Powerpc_dynobj()
504 { delete this->attributes_section_data_; }
506 // Call Sized_dynobj::do_read_symbols to read the symbols then
507 // read .opd from a dynamic object, filling in opd_ent_ vector,
508 void
509 do_read_symbols(Read_symbols_data*);
511 // The .opd section shndx.
512 unsigned int
513 opd_shndx() const
515 return this->opd_shndx_;
518 // The .opd section address.
519 Address
520 opd_address() const
522 return this->opd_address_;
525 // Init OPD entry arrays.
526 void
527 init_opd(size_t opd_size)
529 size_t count = this->opd_ent_ndx(opd_size);
530 this->opd_ent_.resize(count);
533 // Return section and offset of function entry for .opd + R_OFF.
534 unsigned int
535 get_opd_ent(Address r_off, Address* value = NULL) const
537 size_t ndx = this->opd_ent_ndx(r_off);
538 gold_assert(ndx < this->opd_ent_.size());
539 gold_assert(this->opd_ent_[ndx].shndx != 0);
540 if (value != NULL)
541 *value = this->opd_ent_[ndx].off;
542 return this->opd_ent_[ndx].shndx;
545 // Set section and offset of function entry for .opd + R_OFF.
546 void
547 set_opd_ent(Address r_off, unsigned int shndx, Address value)
549 size_t ndx = this->opd_ent_ndx(r_off);
550 gold_assert(ndx < this->opd_ent_.size());
551 this->opd_ent_[ndx].shndx = shndx;
552 this->opd_ent_[ndx].off = value;
556 abiversion() const
557 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
559 // Set ABI version for input and output.
560 void
561 set_abiversion(int ver);
563 // The contents of the .gnu.attributes section if there is one.
564 const Attributes_section_data*
565 attributes_section_data() const
566 { return this->attributes_section_data_; }
568 private:
569 // Used to specify extent of executable sections.
570 struct Sec_info
572 Sec_info(Address start_, Address len_, unsigned int shndx_)
573 : start(start_), len(len_), shndx(shndx_)
576 bool
577 operator<(const Sec_info& that) const
578 { return this->start < that.start; }
580 Address start;
581 Address len;
582 unsigned int shndx;
585 struct Opd_ent
587 unsigned int shndx;
588 Address off;
591 // Return index into opd_ent_ array for .opd entry at OFF.
592 size_t
593 opd_ent_ndx(size_t off) const
594 { return off >> 4;}
596 // For 64-bit the .opd section shndx and address.
597 unsigned int opd_shndx_;
598 Address opd_address_;
600 // Header e_flags
601 elfcpp::Elf_Word e_flags_;
603 // The first 8-byte word of an OPD entry gives the address of the
604 // entry point of the function. Records the section and offset
605 // corresponding to the address. Note that in dynamic objects,
606 // offset is *not* relative to the section.
607 std::vector<Opd_ent> opd_ent_;
609 // Object attributes if there is a .gnu.attributes section.
610 Attributes_section_data* attributes_section_data_;
613 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
614 // base class will emit.
616 template<int sh_type, int size, bool big_endian>
617 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
619 public:
620 Powerpc_copy_relocs()
621 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
624 // Emit any saved relocations which turn out to be needed. This is
625 // called after all the relocs have been scanned.
626 void
627 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
630 template<int size, bool big_endian>
631 class Target_powerpc : public Sized_target<size, big_endian>
633 public:
634 typedef
635 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
636 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
637 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
638 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
639 static const Address invalid_address = static_cast<Address>(0) - 1;
640 // Offset of tp and dtp pointers from start of TLS block.
641 static const Address tp_offset = 0x7000;
642 static const Address dtp_offset = 0x8000;
644 Target_powerpc()
645 : Sized_target<size, big_endian>(&powerpc_info),
646 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
647 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
648 tlsld_got_offset_(-1U),
649 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
650 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
651 plt_localentry0_init_(false), has_localentry0_(false),
652 has_tls_get_addr_opt_(false),
653 tprel_opt_(parameters->options().tls_optimize()),
654 relax_failed_(false), relax_fail_count_(0),
655 stub_group_size_(0), savres_section_(0),
656 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
657 attributes_section_data_(NULL),
658 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
662 // Process the relocations to determine unreferenced sections for
663 // garbage collection.
664 void
665 gc_process_relocs(Symbol_table* symtab,
666 Layout* layout,
667 Sized_relobj_file<size, big_endian>* object,
668 unsigned int data_shndx,
669 unsigned int sh_type,
670 const unsigned char* prelocs,
671 size_t reloc_count,
672 Output_section* output_section,
673 bool needs_special_offset_handling,
674 size_t local_symbol_count,
675 const unsigned char* plocal_symbols);
677 // Scan the relocations to look for symbol adjustments.
678 void
679 scan_relocs(Symbol_table* symtab,
680 Layout* layout,
681 Sized_relobj_file<size, big_endian>* object,
682 unsigned int data_shndx,
683 unsigned int sh_type,
684 const unsigned char* prelocs,
685 size_t reloc_count,
686 Output_section* output_section,
687 bool needs_special_offset_handling,
688 size_t local_symbol_count,
689 const unsigned char* plocal_symbols);
691 // Map input .toc section to output .got section.
692 const char*
693 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
695 if (size == 64 && strcmp(name, ".toc") == 0)
697 *plen = 4;
698 return ".got";
700 return NULL;
703 // Provide linker defined save/restore functions.
704 void
705 define_save_restore_funcs(Layout*, Symbol_table*);
707 // No stubs unless a final link.
708 bool
709 do_may_relax() const
710 { return !parameters->options().relocatable(); }
712 bool
713 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
715 void
716 do_plt_fde_location(const Output_data*, unsigned char*,
717 uint64_t*, off_t*) const;
719 // Stash info about branches, for stub generation.
720 void
721 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
722 unsigned int data_shndx, Address r_offset,
723 unsigned int r_type, unsigned int r_sym, Address addend)
725 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
726 this->branch_info_.push_back(info);
727 if (r_type == elfcpp::R_POWERPC_REL14
728 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
729 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
730 ppc_object->set_has_14bit_branch(data_shndx);
733 // Return whether the last branch is a plt call, and if so, mark the
734 // branch as having an R_PPC64_TOCSAVE.
735 bool
736 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
737 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
739 return (size == 64
740 && !this->branch_info_.empty()
741 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
742 r_offset, this, symtab));
745 // Say the given location, that of a nop in a function prologue with
746 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
747 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
748 void
749 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
750 unsigned int shndx, Address offset)
752 Symbol_location loc;
753 loc.object = ppc_object;
754 loc.shndx = shndx;
755 loc.offset = offset;
756 this->tocsave_loc_.insert(loc);
759 // Accessor
760 const Tocsave_loc*
761 tocsave_loc() const
763 return &this->tocsave_loc_;
766 void
767 do_define_standard_symbols(Symbol_table*, Layout*);
769 // Finalize the sections.
770 void
771 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
773 // Return the value to use for a dynamic which requires special
774 // treatment.
775 uint64_t
776 do_dynsym_value(const Symbol*) const;
778 // Return the PLT address to use for a local symbol.
779 uint64_t
780 do_plt_address_for_local(const Relobj*, unsigned int) const;
782 // Return the PLT address to use for a global symbol.
783 uint64_t
784 do_plt_address_for_global(const Symbol*) const;
786 // Return the offset to use for the GOT_INDX'th got entry which is
787 // for a local tls symbol specified by OBJECT, SYMNDX.
788 int64_t
789 do_tls_offset_for_local(const Relobj* object,
790 unsigned int symndx,
791 unsigned int got_indx) const;
793 // Return the offset to use for the GOT_INDX'th got entry which is
794 // for global tls symbol GSYM.
795 int64_t
796 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
798 void
799 do_function_location(Symbol_location*) const;
801 bool
802 do_can_check_for_function_pointers() const
803 { return true; }
805 // Adjust -fsplit-stack code which calls non-split-stack code.
806 void
807 do_calls_non_split(Relobj* object, unsigned int shndx,
808 section_offset_type fnoffset, section_size_type fnsize,
809 const unsigned char* prelocs, size_t reloc_count,
810 unsigned char* view, section_size_type view_size,
811 std::string* from, std::string* to) const;
813 // Relocate a section.
814 void
815 relocate_section(const Relocate_info<size, big_endian>*,
816 unsigned int sh_type,
817 const unsigned char* prelocs,
818 size_t reloc_count,
819 Output_section* output_section,
820 bool needs_special_offset_handling,
821 unsigned char* view,
822 Address view_address,
823 section_size_type view_size,
824 const Reloc_symbol_changes*);
826 // Scan the relocs during a relocatable link.
827 void
828 scan_relocatable_relocs(Symbol_table* symtab,
829 Layout* layout,
830 Sized_relobj_file<size, big_endian>* object,
831 unsigned int data_shndx,
832 unsigned int sh_type,
833 const unsigned char* prelocs,
834 size_t reloc_count,
835 Output_section* output_section,
836 bool needs_special_offset_handling,
837 size_t local_symbol_count,
838 const unsigned char* plocal_symbols,
839 Relocatable_relocs*);
841 // Scan the relocs for --emit-relocs.
842 void
843 emit_relocs_scan(Symbol_table* symtab,
844 Layout* layout,
845 Sized_relobj_file<size, big_endian>* object,
846 unsigned int data_shndx,
847 unsigned int sh_type,
848 const unsigned char* prelocs,
849 size_t reloc_count,
850 Output_section* output_section,
851 bool needs_special_offset_handling,
852 size_t local_symbol_count,
853 const unsigned char* plocal_syms,
854 Relocatable_relocs* rr);
856 // Emit relocations for a section.
857 void
858 relocate_relocs(const Relocate_info<size, big_endian>*,
859 unsigned int sh_type,
860 const unsigned char* prelocs,
861 size_t reloc_count,
862 Output_section* output_section,
863 typename elfcpp::Elf_types<size>::Elf_Off
864 offset_in_output_section,
865 unsigned char*,
866 Address view_address,
867 section_size_type,
868 unsigned char* reloc_view,
869 section_size_type reloc_view_size);
871 // Return whether SYM is defined by the ABI.
872 bool
873 do_is_defined_by_abi(const Symbol* sym) const
875 return strcmp(sym->name(), "__tls_get_addr") == 0;
878 // Return the size of the GOT section.
879 section_size_type
880 got_size() const
882 gold_assert(this->got_ != NULL);
883 return this->got_->data_size();
886 // Get the PLT section.
887 const Output_data_plt_powerpc<size, big_endian>*
888 plt_section() const
890 gold_assert(this->plt_ != NULL);
891 return this->plt_;
894 // Get the IPLT section.
895 const Output_data_plt_powerpc<size, big_endian>*
896 iplt_section() const
898 gold_assert(this->iplt_ != NULL);
899 return this->iplt_;
902 // Get the LPLT section.
903 const Output_data_plt_powerpc<size, big_endian>*
904 lplt_section() const
906 return this->lplt_;
909 // Return the plt offset and section for the given global sym.
910 Address
911 plt_off(const Symbol* gsym,
912 const Output_data_plt_powerpc<size, big_endian>** sec) const
914 if (gsym->type() == elfcpp::STT_GNU_IFUNC
915 && gsym->can_use_relative_reloc(false))
916 *sec = this->iplt_section();
917 else
918 *sec = this->plt_section();
919 return gsym->plt_offset();
922 // Return the plt offset and section for the given local sym.
923 Address
924 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
925 unsigned int local_sym_index,
926 const Output_data_plt_powerpc<size, big_endian>** sec) const
928 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
929 if (lsym->is_ifunc_symbol())
930 *sec = this->iplt_section();
931 else
932 *sec = this->lplt_section();
933 return relobj->local_plt_offset(local_sym_index);
936 // Get the .glink section.
937 const Output_data_glink<size, big_endian>*
938 glink_section() const
940 gold_assert(this->glink_ != NULL);
941 return this->glink_;
944 Output_data_glink<size, big_endian>*
945 glink_section()
947 gold_assert(this->glink_ != NULL);
948 return this->glink_;
951 bool has_glink() const
952 { return this->glink_ != NULL; }
954 // Get the GOT section.
955 const Output_data_got_powerpc<size, big_endian>*
956 got_section() const
958 gold_assert(this->got_ != NULL);
959 return this->got_;
962 // Get the GOT section, creating it if necessary.
963 Output_data_got_powerpc<size, big_endian>*
964 got_section(Symbol_table*, Layout*);
966 Object*
967 do_make_elf_object(const std::string&, Input_file*, off_t,
968 const elfcpp::Ehdr<size, big_endian>&);
970 // Return the number of entries in the GOT.
971 unsigned int
972 got_entry_count() const
974 if (this->got_ == NULL)
975 return 0;
976 return this->got_size() / (size / 8);
979 // Return the number of entries in the PLT.
980 unsigned int
981 plt_entry_count() const;
983 // Return the offset of the first non-reserved PLT entry.
984 unsigned int
985 first_plt_entry_offset() const
987 if (size == 32)
988 return 0;
989 if (this->abiversion() >= 2)
990 return 16;
991 return 24;
994 // Return the size of each PLT entry.
995 unsigned int
996 plt_entry_size() const
998 if (size == 32)
999 return 4;
1000 if (this->abiversion() >= 2)
1001 return 8;
1002 return 24;
1005 Output_data_save_res<size, big_endian>*
1006 savres_section() const
1008 return this->savres_section_;
1011 // Add any special sections for this symbol to the gc work list.
1012 // For powerpc64, this adds the code section of a function
1013 // descriptor.
1014 void
1015 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1017 // Handle target specific gc actions when adding a gc reference from
1018 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1019 // and DST_OFF. For powerpc64, this adds a referenc to the code
1020 // section of a function descriptor.
1021 void
1022 do_gc_add_reference(Symbol_table* symtab,
1023 Relobj* src_obj,
1024 unsigned int src_shndx,
1025 Relobj* dst_obj,
1026 unsigned int dst_shndx,
1027 Address dst_off) const;
1029 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1030 const Stub_tables&
1031 stub_tables() const
1032 { return this->stub_tables_; }
1034 const Output_data_brlt_powerpc<size, big_endian>*
1035 brlt_section() const
1036 { return this->brlt_section_; }
1038 void
1039 add_branch_lookup_table(Address to)
1041 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1042 this->branch_lookup_table_.insert(std::make_pair(to, off));
1045 Address
1046 find_branch_lookup_table(Address to)
1048 typename Branch_lookup_table::const_iterator p
1049 = this->branch_lookup_table_.find(to);
1050 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1053 void
1054 write_branch_lookup_table(unsigned char *oview)
1056 for (typename Branch_lookup_table::const_iterator p
1057 = this->branch_lookup_table_.begin();
1058 p != this->branch_lookup_table_.end();
1059 ++p)
1061 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1065 // Wrapper used after relax to define a local symbol in output data,
1066 // from the end if value < 0.
1067 void
1068 define_local(Symbol_table* symtab, const char* name,
1069 Output_data* od, Address value, unsigned int symsize)
1071 Symbol* sym
1072 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1073 od, value, symsize, elfcpp::STT_NOTYPE,
1074 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1075 static_cast<Signed_address>(value) < 0,
1076 false);
1077 // We are creating this symbol late, so need to fix up things
1078 // done early in Layout::finalize.
1079 sym->set_dynsym_index(-1U);
1082 void
1083 set_power10_relocs()
1085 this->power10_relocs_ = true;
1088 bool
1089 power10_stubs() const
1091 return (this->power10_relocs_
1092 && (parameters->options().power10_stubs_enum()
1093 != General_options::POWER10_STUBS_NO));
1096 bool
1097 power10_stubs_auto() const
1099 return (parameters->options().power10_stubs_enum()
1100 == General_options::POWER10_STUBS_AUTO);
1103 bool
1104 plt_thread_safe() const
1105 { return this->plt_thread_safe_; }
1107 bool
1108 plt_localentry0() const
1109 { return this->plt_localentry0_; }
1111 bool
1112 has_localentry0() const
1113 { return this->has_localentry0_; }
1115 void
1116 set_has_localentry0()
1118 this->has_localentry0_ = true;
1121 bool
1122 is_elfv2_localentry0(const Symbol* gsym) const
1124 return (size == 64
1125 && this->abiversion() >= 2
1126 && this->plt_localentry0()
1127 && gsym->type() == elfcpp::STT_FUNC
1128 && gsym->is_defined()
1129 && gsym->nonvis() >> 3 == 0
1130 && !gsym->non_zero_localentry());
1133 bool
1134 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1135 unsigned int r_sym) const
1137 const Powerpc_relobj<size, big_endian>* ppc_object
1138 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1140 if (size == 64
1141 && this->abiversion() >= 2
1142 && this->plt_localentry0()
1143 && ppc_object->st_other(r_sym) >> 5 == 0)
1145 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1146 bool is_ordinary;
1147 if (!psymval->is_ifunc_symbol()
1148 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1149 && is_ordinary)
1150 return true;
1152 return false;
1155 bool
1156 tprel_opt() const
1157 { return this->tprel_opt_; }
1159 void
1160 set_tprel_opt(bool val)
1161 { this->tprel_opt_ = val; }
1163 // Remember any symbols seen with non-zero localentry, even those
1164 // not providing a definition
1165 bool
1166 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1167 const char*)
1169 if (size == 64)
1171 unsigned char st_other = sym.get_st_other();
1172 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1173 to->set_non_zero_localentry();
1175 // We haven't resolved anything, continue normal processing.
1176 return false;
1180 abiversion() const
1181 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1183 void
1184 set_abiversion(int ver)
1186 elfcpp::Elf_Word flags = this->processor_specific_flags();
1187 flags &= ~elfcpp::EF_PPC64_ABI;
1188 flags |= ver & elfcpp::EF_PPC64_ABI;
1189 this->set_processor_specific_flags(flags);
1192 Symbol*
1193 tls_get_addr_opt() const
1194 { return this->tls_get_addr_opt_; }
1196 Symbol*
1197 tls_get_addr() const
1198 { return this->tls_get_addr_; }
1200 // If optimizing __tls_get_addr calls, whether this is the
1201 // "__tls_get_addr" symbol.
1202 bool
1203 is_tls_get_addr_opt(const Symbol* gsym) const
1205 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1206 || gsym == this->tls_get_addr_opt_);
1209 bool
1210 replace_tls_get_addr(const Symbol* gsym) const
1211 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1213 void
1214 set_has_tls_get_addr_opt()
1215 { this->has_tls_get_addr_opt_ = true; }
1217 // Offset to toc save stack slot
1219 stk_toc() const
1220 { return this->abiversion() < 2 ? 40 : 24; }
1222 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1223 // so use the CR save slot. Used only by __tls_get_addr call stub,
1224 // relying on __tls_get_addr not saving CR itself.
1226 stk_linker() const
1227 { return this->abiversion() < 2 ? 32 : 8; }
1229 // Merge object attributes from input object with those in the output.
1230 void
1231 merge_object_attributes(const Object*, const Attributes_section_data*);
1233 private:
1235 class Track_tls
1237 public:
1238 enum Tls_get_addr
1240 NOT_EXPECTED = 0,
1241 EXPECTED = 1,
1242 SKIP = 2,
1243 NORMAL = 3
1246 Track_tls()
1247 : tls_get_addr_state_(NOT_EXPECTED),
1248 relinfo_(NULL), relnum_(0), r_offset_(0)
1251 ~Track_tls()
1253 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1254 this->missing();
1257 void
1258 missing(void)
1260 if (this->relinfo_ != NULL)
1261 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1262 _("missing expected __tls_get_addr call"));
1265 void
1266 expect_tls_get_addr_call(
1267 const Relocate_info<size, big_endian>* relinfo,
1268 size_t relnum,
1269 Address r_offset)
1271 this->tls_get_addr_state_ = EXPECTED;
1272 this->relinfo_ = relinfo;
1273 this->relnum_ = relnum;
1274 this->r_offset_ = r_offset;
1277 void
1278 expect_tls_get_addr_call()
1279 { this->tls_get_addr_state_ = EXPECTED; }
1281 void
1282 skip_next_tls_get_addr_call()
1283 {this->tls_get_addr_state_ = SKIP; }
1285 Tls_get_addr
1286 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1287 unsigned int r_type, const Symbol* gsym)
1289 bool is_tls_call
1290 = ((r_type == elfcpp::R_POWERPC_REL24
1291 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1292 || r_type == elfcpp::R_PPC_PLTREL24
1293 || is_plt16_reloc<size>(r_type)
1294 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1295 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1296 || r_type == elfcpp::R_POWERPC_PLTSEQ
1297 || r_type == elfcpp::R_POWERPC_PLTCALL
1298 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1299 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1300 && gsym != NULL
1301 && (gsym == target->tls_get_addr()
1302 || gsym == target->tls_get_addr_opt()));
1303 Tls_get_addr last_tls = this->tls_get_addr_state_;
1304 this->tls_get_addr_state_ = NOT_EXPECTED;
1305 if (is_tls_call && last_tls != EXPECTED)
1306 return last_tls;
1307 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1309 this->missing();
1310 return EXPECTED;
1312 return NORMAL;
1315 private:
1316 // What we're up to regarding calls to __tls_get_addr.
1317 // On powerpc, the branch and link insn making a call to
1318 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1319 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1320 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1321 // The marker relocation always comes first, and has the same
1322 // symbol as the reloc on the insn setting up the __tls_get_addr
1323 // argument. This ties the arg setup insn with the call insn,
1324 // allowing ld to safely optimize away the call. We check that
1325 // every call to __tls_get_addr has a marker relocation, and that
1326 // every marker relocation is on a call to __tls_get_addr.
1327 Tls_get_addr tls_get_addr_state_;
1328 // Info about the last reloc for error message.
1329 const Relocate_info<size, big_endian>* relinfo_;
1330 size_t relnum_;
1331 Address r_offset_;
1334 // The class which scans relocations.
1335 class Scan : protected Track_tls
1337 public:
1338 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1340 Scan()
1341 : Track_tls(), issued_non_pic_error_(false)
1344 static inline int
1345 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1347 inline void
1348 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1349 Sized_relobj_file<size, big_endian>* object,
1350 unsigned int data_shndx,
1351 Output_section* output_section,
1352 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1353 const elfcpp::Sym<size, big_endian>& lsym,
1354 bool is_discarded);
1356 inline void
1357 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1358 Sized_relobj_file<size, big_endian>* object,
1359 unsigned int data_shndx,
1360 Output_section* output_section,
1361 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1362 Symbol* gsym);
1364 inline bool
1365 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1366 Target_powerpc* ,
1367 Sized_relobj_file<size, big_endian>* relobj,
1368 unsigned int ,
1369 Output_section* ,
1370 const elfcpp::Rela<size, big_endian>& ,
1371 unsigned int r_type,
1372 const elfcpp::Sym<size, big_endian>&)
1374 // PowerPC64 .opd is not folded, so any identical function text
1375 // may be folded and we'll still keep function addresses distinct.
1376 // That means no reloc is of concern here.
1377 if (size == 64)
1379 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1380 <Powerpc_relobj<size, big_endian>*>(relobj);
1381 if (ppcobj->abiversion() == 1)
1382 return false;
1384 // For 32-bit and ELFv2, conservatively assume anything but calls to
1385 // function code might be taking the address of the function.
1386 return !is_branch_reloc<size>(r_type);
1389 inline bool
1390 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1391 Target_powerpc* ,
1392 Sized_relobj_file<size, big_endian>* relobj,
1393 unsigned int ,
1394 Output_section* ,
1395 const elfcpp::Rela<size, big_endian>& ,
1396 unsigned int r_type,
1397 Symbol*)
1399 // As above.
1400 if (size == 64)
1402 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1403 <Powerpc_relobj<size, big_endian>*>(relobj);
1404 if (ppcobj->abiversion() == 1)
1405 return false;
1407 return !is_branch_reloc<size>(r_type);
1410 static bool
1411 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1412 Sized_relobj_file<size, big_endian>* object,
1413 unsigned int r_type, bool report_err);
1415 private:
1416 static void
1417 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1418 unsigned int r_type);
1420 static void
1421 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1422 unsigned int r_type, Symbol*);
1424 static void
1425 generate_tls_call(Symbol_table* symtab, Layout* layout,
1426 Target_powerpc* target);
1428 void
1429 check_non_pic(Relobj*, unsigned int r_type);
1431 // Whether we have issued an error about a non-PIC compilation.
1432 bool issued_non_pic_error_;
1435 bool
1436 symval_for_branch(const Symbol_table* symtab,
1437 const Sized_symbol<size>* gsym,
1438 Powerpc_relobj<size, big_endian>* object,
1439 Address *value, unsigned int *dest_shndx);
1441 // The class which implements relocation.
1442 class Relocate : protected Track_tls
1444 public:
1445 // Use 'at' branch hints when true, 'y' when false.
1446 // FIXME maybe: set this with an option.
1447 static const bool is_isa_v2 = true;
1449 Relocate()
1450 : Track_tls()
1453 // Do a relocation. Return false if the caller should not issue
1454 // any warnings about this relocation.
1455 inline bool
1456 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1457 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1458 const Sized_symbol<size>*, const Symbol_value<size>*,
1459 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1460 section_size_type);
1463 class Relocate_comdat_behavior
1465 public:
1466 // Decide what the linker should do for relocations that refer to
1467 // discarded comdat sections.
1468 inline Comdat_behavior
1469 get(const char* name)
1471 gold::Default_comdat_behavior default_behavior;
1472 Comdat_behavior ret = default_behavior.get(name);
1473 if (ret == CB_ERROR)
1475 if (size == 32
1476 && (strcmp(name, ".fixup") == 0
1477 || strcmp(name, ".got2") == 0))
1478 ret = CB_IGNORE;
1479 if (size == 64
1480 && (strcmp(name, ".opd") == 0
1481 || strcmp(name, ".toc") == 0
1482 || strcmp(name, ".toc1") == 0))
1483 ret = CB_IGNORE;
1485 return ret;
1489 // Optimize the TLS relocation type based on what we know about the
1490 // symbol. IS_FINAL is true if the final address of this symbol is
1491 // known at link time.
1493 tls::Tls_optimization
1494 optimize_tls_gd(bool is_final)
1496 // If we are generating a shared library, then we can't do anything
1497 // in the linker.
1498 if (parameters->options().shared()
1499 || !parameters->options().tls_optimize())
1500 return tls::TLSOPT_NONE;
1502 if (!is_final)
1503 return tls::TLSOPT_TO_IE;
1504 return tls::TLSOPT_TO_LE;
1507 tls::Tls_optimization
1508 optimize_tls_ld()
1510 if (parameters->options().shared()
1511 || !parameters->options().tls_optimize())
1512 return tls::TLSOPT_NONE;
1514 return tls::TLSOPT_TO_LE;
1517 tls::Tls_optimization
1518 optimize_tls_ie(bool is_final)
1520 if (!is_final
1521 || parameters->options().shared()
1522 || !parameters->options().tls_optimize())
1523 return tls::TLSOPT_NONE;
1525 return tls::TLSOPT_TO_LE;
1528 // Create glink.
1529 void
1530 make_glink_section(Layout*);
1532 // Create the PLT section.
1533 void
1534 make_plt_section(Symbol_table*, Layout*);
1536 void
1537 make_iplt_section(Symbol_table*, Layout*);
1539 void
1540 make_lplt_section(Layout*);
1542 void
1543 make_brlt_section(Layout*);
1545 // Create a PLT entry for a global symbol.
1546 void
1547 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1549 // Create a PLT entry for a local IFUNC symbol.
1550 void
1551 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1552 Sized_relobj_file<size, big_endian>*,
1553 unsigned int);
1555 // Create a PLT entry for a local non-IFUNC symbol.
1556 void
1557 make_local_plt_entry(Layout*,
1558 Sized_relobj_file<size, big_endian>*,
1559 unsigned int);
1562 // Create a GOT entry for local dynamic __tls_get_addr.
1563 unsigned int
1564 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1565 Sized_relobj_file<size, big_endian>* object);
1567 unsigned int
1568 tlsld_got_offset() const
1570 return this->tlsld_got_offset_;
1573 // Get the dynamic reloc section, creating it if necessary.
1574 Reloc_section*
1575 rela_dyn_section(Layout*);
1577 // Similarly, but for ifunc symbols get the one for ifunc.
1578 Reloc_section*
1579 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1581 // Copy a relocation against a global symbol.
1582 void
1583 copy_reloc(Symbol_table* symtab, Layout* layout,
1584 Sized_relobj_file<size, big_endian>* object,
1585 unsigned int shndx, Output_section* output_section,
1586 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1588 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1589 this->copy_relocs_.copy_reloc(symtab, layout,
1590 symtab->get_sized_symbol<size>(sym),
1591 object, shndx, output_section,
1592 r_type, reloc.get_r_offset(),
1593 reloc.get_r_addend(),
1594 this->rela_dyn_section(layout));
1597 // Look over all the input sections, deciding where to place stubs.
1598 void
1599 group_sections(Layout*, const Task*, bool);
1601 // Sort output sections by address.
1602 struct Sort_sections
1604 bool
1605 operator()(const Output_section* sec1, const Output_section* sec2)
1606 { return sec1->address() < sec2->address(); }
1609 class Branch_info
1611 public:
1612 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1613 unsigned int data_shndx,
1614 Address r_offset,
1615 unsigned int r_type,
1616 unsigned int r_sym,
1617 Address addend)
1618 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1619 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1622 ~Branch_info()
1625 // Return whether this branch is going via a plt call stub, and if
1626 // so, mark it as having an R_PPC64_TOCSAVE.
1627 bool
1628 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1629 unsigned int shndx, Address offset,
1630 Target_powerpc* target, Symbol_table* symtab);
1632 // If this branch needs a plt call stub, or a long branch stub, make one.
1633 bool
1634 make_stub(Stub_table<size, big_endian>*,
1635 Stub_table<size, big_endian>*,
1636 Symbol_table*) const;
1638 private:
1639 // The branch location..
1640 Powerpc_relobj<size, big_endian>* object_;
1641 unsigned int shndx_;
1642 Address offset_;
1643 // ..and the branch type and destination.
1644 unsigned int r_type_ : 31;
1645 unsigned int tocsave_ : 1;
1646 unsigned int r_sym_;
1647 Address addend_;
1650 // Information about this specific target which we pass to the
1651 // general Target structure.
1652 static Target::Target_info powerpc_info;
1654 // The types of GOT entries needed for this platform.
1655 // These values are exposed to the ABI in an incremental link.
1656 // Do not renumber existing values without changing the version
1657 // number of the .gnu_incremental_inputs section.
1658 enum Got_type
1660 GOT_TYPE_STANDARD,
1661 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1662 GOT_TYPE_DTPREL, // entry for @got@dtprel
1663 GOT_TYPE_TPREL // entry for @got@tprel
1666 // The GOT section.
1667 Output_data_got_powerpc<size, big_endian>* got_;
1668 // The PLT section. This is a container for a table of addresses,
1669 // and their relocations. Each address in the PLT has a dynamic
1670 // relocation (R_*_JMP_SLOT) and each address will have a
1671 // corresponding entry in .glink for lazy resolution of the PLT.
1672 // ppc32 initialises the PLT to point at the .glink entry, while
1673 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1674 // linker adds a stub that loads the PLT entry into ctr then
1675 // branches to ctr. There may be more than one stub for each PLT
1676 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1677 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1678 Output_data_plt_powerpc<size, big_endian>* plt_;
1679 // The IPLT section. Like plt_, this is a container for a table of
1680 // addresses and their relocations, specifically for STT_GNU_IFUNC
1681 // functions that resolve locally (STT_GNU_IFUNC functions that
1682 // don't resolve locally go in PLT). Unlike plt_, these have no
1683 // entry in .glink for lazy resolution, and the relocation section
1684 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1685 // the relocation section may contain relocations against
1686 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1687 // relocation section will appear at the end of other dynamic
1688 // relocations, so that ld.so applies these relocations after other
1689 // dynamic relocations. In a static executable, the relocation
1690 // section is emitted and marked with __rela_iplt_start and
1691 // __rela_iplt_end symbols.
1692 Output_data_plt_powerpc<size, big_endian>* iplt_;
1693 // A PLT style section for local, non-ifunc symbols
1694 Output_data_plt_powerpc<size, big_endian>* lplt_;
1695 // Section holding long branch destinations.
1696 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1697 // The .glink section.
1698 Output_data_glink<size, big_endian>* glink_;
1699 // The dynamic reloc section.
1700 Reloc_section* rela_dyn_;
1701 // Relocs saved to avoid a COPY reloc.
1702 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1703 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1704 unsigned int tlsld_got_offset_;
1706 Stub_tables stub_tables_;
1707 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1708 Branch_lookup_table branch_lookup_table_;
1710 typedef std::vector<Branch_info> Branches;
1711 Branches branch_info_;
1712 Tocsave_loc tocsave_loc_;
1714 bool power10_relocs_;
1715 bool plt_thread_safe_;
1716 bool plt_localentry0_;
1717 bool plt_localentry0_init_;
1718 bool has_localentry0_;
1719 bool has_tls_get_addr_opt_;
1720 bool tprel_opt_;
1722 bool relax_failed_;
1723 int relax_fail_count_;
1724 int32_t stub_group_size_;
1726 Output_data_save_res<size, big_endian> *savres_section_;
1728 // The "__tls_get_addr" symbol, if present
1729 Symbol* tls_get_addr_;
1730 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1731 Symbol* tls_get_addr_opt_;
1733 // Attributes in output.
1734 Attributes_section_data* attributes_section_data_;
1736 // Last input file to change various attribute tags
1737 const char* last_fp_;
1738 const char* last_ld_;
1739 const char* last_vec_;
1740 const char* last_struct_;
1743 template<>
1744 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1746 32, // size
1747 true, // is_big_endian
1748 elfcpp::EM_PPC, // machine_code
1749 false, // has_make_symbol
1750 false, // has_resolve
1751 false, // has_code_fill
1752 true, // is_default_stack_executable
1753 false, // can_icf_inline_merge_sections
1754 '\0', // wrap_char
1755 "/usr/lib/ld.so.1", // dynamic_linker
1756 0x10000000, // default_text_segment_address
1757 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1758 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1759 false, // isolate_execinstr
1760 0, // rosegment_gap
1761 elfcpp::SHN_UNDEF, // small_common_shndx
1762 elfcpp::SHN_UNDEF, // large_common_shndx
1763 0, // small_common_section_flags
1764 0, // large_common_section_flags
1765 NULL, // attributes_section
1766 NULL, // attributes_vendor
1767 "_start", // entry_symbol_name
1768 32, // hash_entry_size
1769 elfcpp::SHT_PROGBITS, // unwind_section_type
1772 template<>
1773 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1775 32, // size
1776 false, // is_big_endian
1777 elfcpp::EM_PPC, // machine_code
1778 false, // has_make_symbol
1779 false, // has_resolve
1780 false, // has_code_fill
1781 true, // is_default_stack_executable
1782 false, // can_icf_inline_merge_sections
1783 '\0', // wrap_char
1784 "/usr/lib/ld.so.1", // dynamic_linker
1785 0x10000000, // default_text_segment_address
1786 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1787 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1788 false, // isolate_execinstr
1789 0, // rosegment_gap
1790 elfcpp::SHN_UNDEF, // small_common_shndx
1791 elfcpp::SHN_UNDEF, // large_common_shndx
1792 0, // small_common_section_flags
1793 0, // large_common_section_flags
1794 NULL, // attributes_section
1795 NULL, // attributes_vendor
1796 "_start", // entry_symbol_name
1797 32, // hash_entry_size
1798 elfcpp::SHT_PROGBITS, // unwind_section_type
1801 template<>
1802 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1804 64, // size
1805 true, // is_big_endian
1806 elfcpp::EM_PPC64, // machine_code
1807 false, // has_make_symbol
1808 true, // has_resolve
1809 false, // has_code_fill
1810 false, // is_default_stack_executable
1811 false, // can_icf_inline_merge_sections
1812 '\0', // wrap_char
1813 "/usr/lib/ld.so.1", // dynamic_linker
1814 0x10000000, // default_text_segment_address
1815 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1816 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1817 false, // isolate_execinstr
1818 0, // rosegment_gap
1819 elfcpp::SHN_UNDEF, // small_common_shndx
1820 elfcpp::SHN_UNDEF, // large_common_shndx
1821 0, // small_common_section_flags
1822 0, // large_common_section_flags
1823 NULL, // attributes_section
1824 NULL, // attributes_vendor
1825 "_start", // entry_symbol_name
1826 32, // hash_entry_size
1827 elfcpp::SHT_PROGBITS, // unwind_section_type
1830 template<>
1831 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1833 64, // size
1834 false, // is_big_endian
1835 elfcpp::EM_PPC64, // machine_code
1836 false, // has_make_symbol
1837 true, // has_resolve
1838 false, // has_code_fill
1839 false, // is_default_stack_executable
1840 false, // can_icf_inline_merge_sections
1841 '\0', // wrap_char
1842 "/usr/lib/ld.so.1", // dynamic_linker
1843 0x10000000, // default_text_segment_address
1844 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1845 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1846 false, // isolate_execinstr
1847 0, // rosegment_gap
1848 elfcpp::SHN_UNDEF, // small_common_shndx
1849 elfcpp::SHN_UNDEF, // large_common_shndx
1850 0, // small_common_section_flags
1851 0, // large_common_section_flags
1852 NULL, // attributes_section
1853 NULL, // attributes_vendor
1854 "_start", // entry_symbol_name
1855 32, // hash_entry_size
1856 elfcpp::SHT_PROGBITS, // unwind_section_type
1859 template<int size>
1860 inline bool
1861 is_branch_reloc(unsigned int r_type)
1863 return (r_type == elfcpp::R_POWERPC_REL24
1864 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1865 || r_type == elfcpp::R_PPC_PLTREL24
1866 || r_type == elfcpp::R_PPC_LOCAL24PC
1867 || r_type == elfcpp::R_POWERPC_REL14
1868 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1869 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1870 || r_type == elfcpp::R_POWERPC_ADDR24
1871 || r_type == elfcpp::R_POWERPC_ADDR14
1872 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1873 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1876 // Reloc resolves to plt entry.
1877 template<int size>
1878 inline bool
1879 is_plt16_reloc(unsigned int r_type)
1881 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1882 || r_type == elfcpp::R_POWERPC_PLT16_HI
1883 || r_type == elfcpp::R_POWERPC_PLT16_HA
1884 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1887 // GOT_TYPE_STANDARD (ie. not TLS) GOT relocs
1888 inline bool
1889 is_got_reloc(unsigned int r_type)
1891 return (r_type == elfcpp::R_POWERPC_GOT16
1892 || r_type == elfcpp::R_POWERPC_GOT16_LO
1893 || r_type == elfcpp::R_POWERPC_GOT16_HI
1894 || r_type == elfcpp::R_POWERPC_GOT16_HA
1895 || r_type == elfcpp::R_PPC64_GOT16_DS
1896 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1897 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1900 // If INSN is an opcode that may be used with an @tls operand, return
1901 // the transformed insn for TLS optimisation, otherwise return 0. If
1902 // REG is non-zero only match an insn with RB or RA equal to REG.
1903 uint32_t
1904 at_tls_transform(uint32_t insn, unsigned int reg)
1906 if ((insn & (0x3f << 26)) != 31 << 26)
1907 return 0;
1909 unsigned int rtra;
1910 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1911 rtra = insn & ((1 << 26) - (1 << 16));
1912 else if (((insn >> 16) & 0x1f) == reg)
1913 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1914 else
1915 return 0;
1917 if ((insn & (0x3ff << 1)) == 266 << 1)
1918 // add -> addi
1919 insn = 14 << 26;
1920 else if ((insn & (0x1f << 1)) == 23 << 1
1921 && ((insn & (0x1f << 6)) < 14 << 6
1922 || ((insn & (0x1f << 6)) >= 16 << 6
1923 && (insn & (0x1f << 6)) < 24 << 6)))
1924 // load and store indexed -> dform
1925 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1926 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1927 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1928 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1929 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1930 // lwax -> lwa
1931 insn = (58 << 26) | 2;
1932 else
1933 return 0;
1934 insn |= rtra;
1935 return insn;
1939 template<int size, bool big_endian>
1940 class Powerpc_relocate_functions
1942 public:
1943 enum Overflow_check
1945 CHECK_NONE,
1946 CHECK_SIGNED,
1947 CHECK_UNSIGNED,
1948 CHECK_BITFIELD,
1949 CHECK_LOW_INSN,
1950 CHECK_HIGH_INSN
1953 enum Status
1955 STATUS_OK,
1956 STATUS_OVERFLOW
1959 private:
1960 typedef Powerpc_relocate_functions<size, big_endian> This;
1961 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1962 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1964 template<int valsize>
1965 static inline bool
1966 has_overflow_signed(Address value)
1968 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1969 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1970 limit <<= ((valsize - 1) >> 1);
1971 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1972 return value + limit > (limit << 1) - 1;
1975 template<int valsize>
1976 static inline bool
1977 has_overflow_unsigned(Address value)
1979 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1980 limit <<= ((valsize - 1) >> 1);
1981 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1982 return value > (limit << 1) - 1;
1985 template<int valsize>
1986 static inline bool
1987 has_overflow_bitfield(Address value)
1989 return (has_overflow_unsigned<valsize>(value)
1990 && has_overflow_signed<valsize>(value));
1993 template<int valsize>
1994 static inline Status
1995 overflowed(Address value, Overflow_check overflow)
1997 if (overflow == CHECK_SIGNED)
1999 if (has_overflow_signed<valsize>(value))
2000 return STATUS_OVERFLOW;
2002 else if (overflow == CHECK_UNSIGNED)
2004 if (has_overflow_unsigned<valsize>(value))
2005 return STATUS_OVERFLOW;
2007 else if (overflow == CHECK_BITFIELD)
2009 if (has_overflow_bitfield<valsize>(value))
2010 return STATUS_OVERFLOW;
2012 return STATUS_OK;
2015 // Do a simple RELA relocation
2016 template<int fieldsize, int valsize>
2017 static inline Status
2018 rela(unsigned char* view, Address value, Overflow_check overflow)
2020 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2021 Valtype* wv = reinterpret_cast<Valtype*>(view);
2022 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2023 return overflowed<valsize>(value, overflow);
2026 template<int fieldsize, int valsize>
2027 static inline Status
2028 rela(unsigned char* view,
2029 unsigned int right_shift,
2030 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2031 Address value,
2032 Overflow_check overflow)
2034 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2035 Valtype* wv = reinterpret_cast<Valtype*>(view);
2036 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2037 if (overflow == CHECK_SIGNED)
2038 value = static_cast<SignedAddress>(value) >> right_shift;
2039 else
2040 value = value >> right_shift;
2041 Valtype reloc = value;
2042 val &= ~dst_mask;
2043 reloc &= dst_mask;
2044 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2045 return overflowed<valsize>(value, overflow);
2048 // Do a simple RELA relocation, unaligned.
2049 template<int fieldsize, int valsize>
2050 static inline Status
2051 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2053 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2054 return overflowed<valsize>(value, overflow);
2057 template<int fieldsize, int valsize>
2058 static inline Status
2059 rela_ua(unsigned char* view,
2060 unsigned int right_shift,
2061 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2062 Address value,
2063 Overflow_check overflow)
2065 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2066 Valtype;
2067 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2068 if (overflow == CHECK_SIGNED)
2069 value = static_cast<SignedAddress>(value) >> right_shift;
2070 else
2071 value = value >> right_shift;
2072 Valtype reloc = value;
2073 val &= ~dst_mask;
2074 reloc &= dst_mask;
2075 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2076 return overflowed<valsize>(value, overflow);
2079 public:
2080 // R_PPC64_ADDR64: (Symbol + Addend)
2081 static inline void
2082 addr64(unsigned char* view, Address value)
2083 { This::template rela<64,64>(view, value, CHECK_NONE); }
2085 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2086 static inline void
2087 addr64_u(unsigned char* view, Address value)
2088 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2090 // R_POWERPC_ADDR32: (Symbol + Addend)
2091 static inline Status
2092 addr32(unsigned char* view, Address value, Overflow_check overflow)
2093 { return This::template rela<32,32>(view, value, overflow); }
2095 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2096 static inline Status
2097 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2098 { return This::template rela_ua<32,32>(view, value, overflow); }
2100 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2101 static inline Status
2102 addr24(unsigned char* view, Address value, Overflow_check overflow)
2104 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2105 value, overflow);
2106 if (overflow != CHECK_NONE && (value & 3) != 0)
2107 stat = STATUS_OVERFLOW;
2108 return stat;
2111 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2112 static inline Status
2113 addr16(unsigned char* view, Address value, Overflow_check overflow)
2114 { return This::template rela<16,16>(view, value, overflow); }
2116 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2117 static inline Status
2118 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2119 { return This::template rela_ua<16,16>(view, value, overflow); }
2121 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2122 static inline Status
2123 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2125 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2126 if ((value & 3) != 0)
2127 stat = STATUS_OVERFLOW;
2128 return stat;
2131 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2132 static inline Status
2133 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2135 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2136 if ((value & 15) != 0)
2137 stat = STATUS_OVERFLOW;
2138 return stat;
2141 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2142 static inline void
2143 addr16_hi(unsigned char* view, Address value)
2144 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2146 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2147 static inline void
2148 addr16_ha(unsigned char* view, Address value)
2149 { This::addr16_hi(view, value + 0x8000); }
2151 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2152 static inline void
2153 addr16_hi2(unsigned char* view, Address value)
2154 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2156 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2157 static inline void
2158 addr16_ha2(unsigned char* view, Address value)
2159 { This::addr16_hi2(view, value + 0x8000); }
2161 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2162 static inline void
2163 addr16_hi3(unsigned char* view, Address value)
2164 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2166 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2167 static inline void
2168 addr16_ha3(unsigned char* view, Address value)
2169 { This::addr16_hi3(view, value + 0x8000); }
2171 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2172 static inline Status
2173 addr14(unsigned char* view, Address value, Overflow_check overflow)
2175 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2176 if (overflow != CHECK_NONE && (value & 3) != 0)
2177 stat = STATUS_OVERFLOW;
2178 return stat;
2181 // R_POWERPC_REL16DX_HA
2182 static inline Status
2183 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2185 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2186 Valtype* wv = reinterpret_cast<Valtype*>(view);
2187 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2188 value += 0x8000;
2189 value = static_cast<SignedAddress>(value) >> 16;
2190 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2191 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2192 return overflowed<16>(value, overflow);
2195 // R_PPC64_D34
2196 static inline Status
2197 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2199 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2200 value, overflow);
2201 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2202 return stat;
2205 // R_PPC64_D34_HI30
2206 static inline void
2207 addr34_hi(unsigned char *view, uint64_t value)
2208 { This::addr34(view, value >> 34, CHECK_NONE);}
2210 // R_PPC64_D34_HA30
2211 static inline void
2212 addr34_ha(unsigned char *view, uint64_t value)
2213 { This::addr34_hi(view, value + (1ULL << 33));}
2215 // R_PPC64_D28
2216 static inline Status
2217 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2219 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2220 value, overflow);
2221 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2222 return stat;
2225 // R_PPC64_ADDR16_HIGHER34
2226 static inline void
2227 addr16_higher34(unsigned char* view, uint64_t value)
2228 { This::addr16(view, value >> 34, CHECK_NONE); }
2230 // R_PPC64_ADDR16_HIGHERA34
2231 static inline void
2232 addr16_highera34(unsigned char* view, uint64_t value)
2233 { This::addr16_higher34(view, value + (1ULL << 33)); }
2235 // R_PPC64_ADDR16_HIGHEST34
2236 static inline void
2237 addr16_highest34(unsigned char* view, uint64_t value)
2238 { This::addr16(view, value >> 50, CHECK_NONE); }
2240 // R_PPC64_ADDR16_HIGHESTA34
2241 static inline void
2242 addr16_highesta34(unsigned char* view, uint64_t value)
2243 { This::addr16_highest34(view, value + (1ULL << 33)); }
2246 // Set ABI version for input and output.
2248 template<int size, bool big_endian>
2249 void
2250 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2252 this->e_flags_ |= ver;
2253 if (this->abiversion() != 0)
2255 Target_powerpc<size, big_endian>* target =
2256 static_cast<Target_powerpc<size, big_endian>*>(
2257 parameters->sized_target<size, big_endian>());
2258 if (target->abiversion() == 0)
2259 target->set_abiversion(this->abiversion());
2260 else if (target->abiversion() != this->abiversion())
2261 gold_error(_("%s: ABI version %d is not compatible "
2262 "with ABI version %d output"),
2263 this->name().c_str(),
2264 this->abiversion(), target->abiversion());
2269 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2270 // relocatable object, if such sections exists.
2272 template<int size, bool big_endian>
2273 bool
2274 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2275 Read_symbols_data* sd)
2277 const unsigned char* const pshdrs = sd->section_headers->data();
2278 const unsigned char* namesu = sd->section_names->data();
2279 const char* names = reinterpret_cast<const char*>(namesu);
2280 section_size_type names_size = sd->section_names_size;
2281 const unsigned char* s;
2283 s = this->template find_shdr<size, big_endian>(pshdrs,
2284 size == 32 ? ".got2" : ".opd",
2285 names, names_size, NULL);
2286 if (s != NULL)
2288 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2289 this->special_ = ndx;
2290 if (size == 64)
2292 if (this->abiversion() == 0)
2293 this->set_abiversion(1);
2294 else if (this->abiversion() > 1)
2295 gold_error(_("%s: .opd invalid in abiv%d"),
2296 this->name().c_str(), this->abiversion());
2299 if (size == 64)
2301 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2302 names, names_size, NULL);
2303 if (s != NULL)
2305 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2306 this->relatoc_ = ndx;
2307 typename elfcpp::Shdr<size, big_endian> shdr(s);
2308 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2311 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2314 // Examine .rela.opd to build info about function entry points.
2316 template<int size, bool big_endian>
2317 void
2318 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2319 size_t reloc_count,
2320 const unsigned char* prelocs,
2321 const unsigned char* plocal_syms)
2323 if (size == 64)
2325 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2326 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2327 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2328 Address expected_off = 0;
2329 bool regular = true;
2330 unsigned int opd_ent_size = 0;
2332 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2334 Reltype reloc(prelocs);
2335 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2336 = reloc.get_r_info();
2337 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2338 if (r_type == elfcpp::R_PPC64_ADDR64)
2340 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2341 typename elfcpp::Elf_types<size>::Elf_Addr value;
2342 bool is_ordinary;
2343 unsigned int shndx;
2344 if (r_sym < this->local_symbol_count())
2346 typename elfcpp::Sym<size, big_endian>
2347 lsym(plocal_syms + r_sym * sym_size);
2348 shndx = lsym.get_st_shndx();
2349 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2350 value = lsym.get_st_value();
2352 else
2353 shndx = this->symbol_section_and_value(r_sym, &value,
2354 &is_ordinary);
2355 this->set_opd_ent(reloc.get_r_offset(), shndx,
2356 value + reloc.get_r_addend());
2357 if (i == 2)
2359 expected_off = reloc.get_r_offset();
2360 opd_ent_size = expected_off;
2362 else if (expected_off != reloc.get_r_offset())
2363 regular = false;
2364 expected_off += opd_ent_size;
2366 else if (r_type == elfcpp::R_PPC64_TOC)
2368 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2369 regular = false;
2371 else
2373 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2374 this->name().c_str(), r_type);
2375 regular = false;
2378 if (reloc_count <= 2)
2379 opd_ent_size = this->section_size(this->opd_shndx());
2380 if (opd_ent_size != 24 && opd_ent_size != 16)
2381 regular = false;
2382 if (!regular)
2384 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2385 this->name().c_str());
2386 opd_ent_size = 0;
2391 // Returns true if a code sequence loading the TOC entry at VALUE
2392 // relative to the TOC pointer can be converted into code calculating
2393 // a TOC pointer relative offset.
2394 // If so, the TOC pointer relative offset is stored to VALUE.
2396 template<int size, bool big_endian>
2397 bool
2398 Powerpc_relobj<size, big_endian>::make_toc_relative(
2399 Target_powerpc<size, big_endian>* target,
2400 Address* value)
2402 if (size != 64)
2403 return false;
2405 // With -mcmodel=medium code it is quite possible to have
2406 // toc-relative relocs referring to objects outside the TOC.
2407 // Don't try to look at a non-existent TOC.
2408 if (this->toc_shndx() == 0)
2409 return false;
2411 // Convert VALUE back to an address by adding got_base (see below),
2412 // then to an offset in the TOC by subtracting the TOC output
2413 // section address and the TOC output offset. Since this TOC output
2414 // section and the got output section are one and the same, we can
2415 // omit adding and subtracting the output section address.
2416 Address off = (*value + this->toc_base_offset()
2417 - this->output_section_offset(this->toc_shndx()));
2418 // Is this offset in the TOC? -mcmodel=medium code may be using
2419 // TOC relative access to variables outside the TOC. Those of
2420 // course can't be optimized. We also don't try to optimize code
2421 // that is using a different object's TOC.
2422 if (off >= this->section_size(this->toc_shndx()))
2423 return false;
2425 if (this->no_toc_opt(off))
2426 return false;
2428 section_size_type vlen;
2429 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2430 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2431 // The TOC pointer
2432 Address got_base = (target->got_section()->output_section()->address()
2433 + this->toc_base_offset());
2434 addr -= got_base;
2435 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2436 return false;
2438 *value = addr;
2439 return true;
2442 template<int size, bool big_endian>
2443 bool
2444 Powerpc_relobj<size, big_endian>::make_got_relative(
2445 Target_powerpc<size, big_endian>* target,
2446 const Symbol_value<size>* psymval,
2447 Address addend,
2448 Address* value)
2450 Address addr = psymval->value(this, addend);
2451 Address got_base = (target->got_section()->output_section()->address()
2452 + this->toc_base_offset());
2453 addr -= got_base;
2454 if (addr + 0x80008000 > 0xffffffff)
2455 return false;
2457 *value = addr;
2458 return true;
2461 // Perform the Sized_relobj_file method, then set up opd info from
2462 // .opd relocs.
2464 template<int size, bool big_endian>
2465 void
2466 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2468 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2469 if (size == 64)
2471 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2472 p != rd->relocs.end();
2473 ++p)
2475 if (p->data_shndx == this->opd_shndx())
2477 uint64_t opd_size = this->section_size(this->opd_shndx());
2478 gold_assert(opd_size == static_cast<size_t>(opd_size));
2479 if (opd_size != 0)
2481 this->init_opd(opd_size);
2482 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2483 rd->local_symbols->data());
2485 break;
2491 // Read the symbols then set up st_other vector.
2493 template<int size, bool big_endian>
2494 void
2495 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2497 this->base_read_symbols(sd);
2498 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2499 return;
2500 if (size == 64)
2502 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2503 const unsigned char* const pshdrs = sd->section_headers->data();
2504 const unsigned int loccount = this->do_local_symbol_count();
2505 if (loccount != 0)
2507 this->st_other_.resize(loccount);
2508 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2509 off_t locsize = loccount * sym_size;
2510 const unsigned int symtab_shndx = this->symtab_shndx();
2511 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2512 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2513 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2514 locsize, true, false);
2515 psyms += sym_size;
2516 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2518 elfcpp::Sym<size, big_endian> sym(psyms);
2519 unsigned char st_other = sym.get_st_other();
2520 this->st_other_[i] = st_other;
2521 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2523 if (this->abiversion() == 0)
2524 this->set_abiversion(2);
2525 else if (this->abiversion() < 2)
2526 gold_error(_("%s: local symbol %d has invalid st_other"
2527 " for ABI version 1"),
2528 this->name().c_str(), i);
2534 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2535 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2536 bool merge_attributes = false;
2537 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2539 elfcpp::Shdr<size, big_endian> shdr(ps);
2540 switch (shdr.get_sh_type())
2542 case elfcpp::SHT_GNU_ATTRIBUTES:
2544 gold_assert(this->attributes_section_data_ == NULL);
2545 section_offset_type section_offset = shdr.get_sh_offset();
2546 section_size_type section_size =
2547 convert_to_section_size_type(shdr.get_sh_size());
2548 const unsigned char* view =
2549 this->get_view(section_offset, section_size, true, false);
2550 this->attributes_section_data_ =
2551 new Attributes_section_data(view, section_size);
2553 break;
2555 case elfcpp::SHT_SYMTAB:
2557 // Sometimes an object has no contents except the section
2558 // name string table and an empty symbol table with the
2559 // undefined symbol. We don't want to merge
2560 // processor-specific flags from such an object.
2561 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2562 elfcpp::Elf_sizes<size>::sym_size;
2563 if (shdr.get_sh_size() > sym_size)
2564 merge_attributes = true;
2566 break;
2568 case elfcpp::SHT_STRTAB:
2569 break;
2571 default:
2572 merge_attributes = true;
2573 break;
2577 if (!merge_attributes)
2579 // Should rarely happen.
2580 delete this->attributes_section_data_;
2581 this->attributes_section_data_ = NULL;
2585 template<int size, bool big_endian>
2586 void
2587 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2589 this->e_flags_ |= ver;
2590 if (this->abiversion() != 0)
2592 Target_powerpc<size, big_endian>* target =
2593 static_cast<Target_powerpc<size, big_endian>*>(
2594 parameters->sized_target<size, big_endian>());
2595 if (target->abiversion() == 0)
2596 target->set_abiversion(this->abiversion());
2597 else if (target->abiversion() != this->abiversion())
2598 gold_error(_("%s: ABI version %d is not compatible "
2599 "with ABI version %d output"),
2600 this->name().c_str(),
2601 this->abiversion(), target->abiversion());
2606 // Call Sized_dynobj::base_read_symbols to read the symbols then
2607 // read .opd from a dynamic object, filling in opd_ent_ vector,
2609 template<int size, bool big_endian>
2610 void
2611 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2613 this->base_read_symbols(sd);
2614 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2615 const unsigned char* ps =
2616 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2617 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2619 elfcpp::Shdr<size, big_endian> shdr(ps);
2620 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2622 section_offset_type section_offset = shdr.get_sh_offset();
2623 section_size_type section_size =
2624 convert_to_section_size_type(shdr.get_sh_size());
2625 const unsigned char* view =
2626 this->get_view(section_offset, section_size, true, false);
2627 this->attributes_section_data_ =
2628 new Attributes_section_data(view, section_size);
2629 break;
2632 if (size == 64)
2634 const unsigned char* const pshdrs = sd->section_headers->data();
2635 const unsigned char* namesu = sd->section_names->data();
2636 const char* names = reinterpret_cast<const char*>(namesu);
2637 const unsigned char* s = NULL;
2638 const unsigned char* opd;
2639 section_size_type opd_size;
2641 // Find and read .opd section.
2642 while (1)
2644 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2645 sd->section_names_size,
2647 if (s == NULL)
2648 return;
2650 typename elfcpp::Shdr<size, big_endian> shdr(s);
2651 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2652 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2654 if (this->abiversion() == 0)
2655 this->set_abiversion(1);
2656 else if (this->abiversion() > 1)
2657 gold_error(_("%s: .opd invalid in abiv%d"),
2658 this->name().c_str(), this->abiversion());
2660 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2661 this->opd_address_ = shdr.get_sh_addr();
2662 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2663 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2664 true, false);
2665 break;
2669 // Build set of executable sections.
2670 // Using a set is probably overkill. There is likely to be only
2671 // a few executable sections, typically .init, .text and .fini,
2672 // and they are generally grouped together.
2673 typedef std::set<Sec_info> Exec_sections;
2674 Exec_sections exec_sections;
2675 s = pshdrs;
2676 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2678 typename elfcpp::Shdr<size, big_endian> shdr(s);
2679 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2680 && ((shdr.get_sh_flags()
2681 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2682 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2683 && shdr.get_sh_size() != 0)
2685 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2686 shdr.get_sh_size(), i));
2689 if (exec_sections.empty())
2690 return;
2692 // Look over the OPD entries. This is complicated by the fact
2693 // that some binaries will use two-word entries while others
2694 // will use the standard three-word entries. In most cases
2695 // the third word (the environment pointer for languages like
2696 // Pascal) is unused and will be zero. If the third word is
2697 // used it should not be pointing into executable sections,
2698 // I think.
2699 this->init_opd(opd_size);
2700 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2702 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2703 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2704 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2705 if (val == 0)
2706 // Chances are that this is the third word of an OPD entry.
2707 continue;
2708 typename Exec_sections::const_iterator e
2709 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2710 if (e != exec_sections.begin())
2712 --e;
2713 if (e->start <= val && val < e->start + e->len)
2715 // We have an address in an executable section.
2716 // VAL ought to be the function entry, set it up.
2717 this->set_opd_ent(p - opd, e->shndx, val);
2718 // Skip second word of OPD entry, the TOC pointer.
2719 p += 8;
2722 // If we didn't match any executable sections, we likely
2723 // have a non-zero third word in the OPD entry.
2728 // Relocate sections.
2730 template<int size, bool big_endian>
2731 void
2732 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2733 const Symbol_table* symtab, const Layout* layout,
2734 const unsigned char* pshdrs, Output_file* of,
2735 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2737 unsigned int start = 1;
2738 if (size == 64
2739 && this->relatoc_ != 0
2740 && !parameters->options().relocatable())
2742 // Relocate .toc first.
2743 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2744 this->relatoc_, this->relatoc_);
2745 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2746 1, this->relatoc_ - 1);
2747 start = this->relatoc_ + 1;
2749 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2750 start, this->shnum() - 1);
2752 if (!parameters->options().output_is_position_independent())
2754 Target_powerpc<size, big_endian>* target
2755 = static_cast<Target_powerpc<size, big_endian>*>(
2756 parameters->sized_target<size, big_endian>());
2757 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2759 const section_size_type offset = target->lplt_section()->offset();
2760 const section_size_type oview_size
2761 = convert_to_section_size_type(target->lplt_section()->data_size());
2762 unsigned char* const oview = of->get_output_view(offset, oview_size);
2764 bool modified = false;
2765 unsigned int nsyms = this->local_symbol_count();
2766 for (unsigned int i = 0; i < nsyms; i++)
2767 if (this->local_has_plt_offset(i))
2769 Address value = this->local_symbol_value(i, 0);
2770 size_t off = this->local_plt_offset(i);
2771 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2772 modified = true;
2774 if (modified)
2775 of->write_output_view(offset, oview_size, oview);
2780 // Set up some symbols.
2782 template<int size, bool big_endian>
2783 void
2784 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2785 Symbol_table* symtab,
2786 Layout* layout)
2788 if (size == 32)
2790 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2791 // undefined when scanning relocs (and thus requires
2792 // non-relative dynamic relocs). The proper value will be
2793 // updated later.
2794 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2795 if (gotsym != NULL && gotsym->is_undefined())
2797 Target_powerpc<size, big_endian>* target =
2798 static_cast<Target_powerpc<size, big_endian>*>(
2799 parameters->sized_target<size, big_endian>());
2800 Output_data_got_powerpc<size, big_endian>* got
2801 = target->got_section(symtab, layout);
2802 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2803 Symbol_table::PREDEFINED,
2804 got, 0, 0,
2805 elfcpp::STT_OBJECT,
2806 elfcpp::STB_LOCAL,
2807 elfcpp::STV_HIDDEN, 0,
2808 false, false);
2811 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2812 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2813 if (sdasym != NULL && sdasym->is_undefined())
2815 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2816 Output_section* os
2817 = layout->add_output_section_data(".sdata", 0,
2818 elfcpp::SHF_ALLOC
2819 | elfcpp::SHF_WRITE,
2820 sdata, ORDER_SMALL_DATA, false);
2821 symtab->define_in_output_data("_SDA_BASE_", NULL,
2822 Symbol_table::PREDEFINED,
2823 os, 32768, 0, elfcpp::STT_OBJECT,
2824 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2825 0, false, false);
2828 else
2830 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2831 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2832 if (gotsym != NULL && gotsym->is_undefined())
2834 Target_powerpc<size, big_endian>* target =
2835 static_cast<Target_powerpc<size, big_endian>*>(
2836 parameters->sized_target<size, big_endian>());
2837 Output_data_got_powerpc<size, big_endian>* got
2838 = target->got_section(symtab, layout);
2839 symtab->define_in_output_data(".TOC.", NULL,
2840 Symbol_table::PREDEFINED,
2841 got, 0x8000, 0,
2842 elfcpp::STT_OBJECT,
2843 elfcpp::STB_LOCAL,
2844 elfcpp::STV_HIDDEN, 0,
2845 false, false);
2849 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2850 if (parameters->options().tls_get_addr_optimize()
2851 && this->tls_get_addr_ != NULL
2852 && this->tls_get_addr_->in_reg())
2853 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2854 if (this->tls_get_addr_opt_ != NULL)
2856 if (this->tls_get_addr_->is_undefined()
2857 || this->tls_get_addr_->is_from_dynobj())
2859 // Make it seem as if references to __tls_get_addr are
2860 // really to __tls_get_addr_opt, so the latter symbol is
2861 // made dynamic, not the former.
2862 this->tls_get_addr_->clear_in_reg();
2863 this->tls_get_addr_opt_->set_in_reg();
2865 // We have a non-dynamic definition for __tls_get_addr.
2866 // Make __tls_get_addr_opt the same, if it does not already have
2867 // a non-dynamic definition.
2868 else if (this->tls_get_addr_opt_->is_undefined()
2869 || this->tls_get_addr_opt_->is_from_dynobj())
2871 Sized_symbol<size>* from
2872 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2873 Sized_symbol<size>* to
2874 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2875 symtab->clone<size>(to, from);
2880 // Set up PowerPC target specific relobj.
2882 template<int size, bool big_endian>
2883 Object*
2884 Target_powerpc<size, big_endian>::do_make_elf_object(
2885 const std::string& name,
2886 Input_file* input_file,
2887 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2889 int et = ehdr.get_e_type();
2890 // ET_EXEC files are valid input for --just-symbols/-R,
2891 // and we treat them as relocatable objects.
2892 if (et == elfcpp::ET_REL
2893 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2895 Powerpc_relobj<size, big_endian>* obj =
2896 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2897 obj->setup();
2898 return obj;
2900 else if (et == elfcpp::ET_DYN)
2902 Powerpc_dynobj<size, big_endian>* obj =
2903 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2904 obj->setup();
2905 return obj;
2907 else
2909 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2910 return NULL;
2914 template<int size, bool big_endian>
2915 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2917 public:
2918 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2919 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2921 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2922 : Output_data_got<size, big_endian>(),
2923 symtab_(symtab), layout_(layout),
2924 header_ent_cnt_(size == 32 ? 3 : 1),
2925 header_index_(size == 32 ? 0x2000 : 0)
2927 if (size == 64)
2928 this->set_addralign(256);
2931 // Override all the Output_data_got methods we use so as to first call
2932 // reserve_ent().
2933 bool
2934 add_global(Symbol* gsym, unsigned int got_type)
2936 this->reserve_ent();
2937 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2940 bool
2941 add_global_plt(Symbol* gsym, unsigned int got_type)
2943 this->reserve_ent();
2944 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2947 bool
2948 add_global_tls(Symbol* gsym, unsigned int got_type)
2949 { return this->add_global_plt(gsym, got_type); }
2951 void
2952 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2953 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2955 this->reserve_ent();
2956 Output_data_got<size, big_endian>::
2957 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2960 void
2961 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2962 Output_data_reloc_generic* rel_dyn,
2963 unsigned int r_type_1, unsigned int r_type_2)
2965 if (gsym->has_got_offset(got_type))
2966 return;
2968 this->reserve_ent(2);
2969 Output_data_got<size, big_endian>::
2970 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2973 bool
2974 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2976 this->reserve_ent();
2977 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2978 got_type);
2981 bool
2982 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2984 this->reserve_ent();
2985 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2986 got_type);
2989 bool
2990 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2991 { return this->add_local_plt(object, sym_index, got_type); }
2993 void
2994 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2995 unsigned int got_type,
2996 Output_data_reloc_generic* rel_dyn,
2997 unsigned int r_type)
2999 if (object->local_has_got_offset(sym_index, got_type))
3000 return;
3002 this->reserve_ent(2);
3003 Output_data_got<size, big_endian>::
3004 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
3007 unsigned int
3008 add_constant(Valtype constant)
3010 this->reserve_ent();
3011 return Output_data_got<size, big_endian>::add_constant(constant);
3014 unsigned int
3015 add_constant_pair(Valtype c1, Valtype c2)
3017 this->reserve_ent(2);
3018 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3021 // Offset of _GLOBAL_OFFSET_TABLE_.
3022 unsigned int
3023 g_o_t() const
3025 return this->got_offset(this->header_index_);
3028 // Offset of base used to access the GOT/TOC.
3029 // The got/toc pointer reg will be set to this value.
3030 Valtype
3031 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
3033 if (size == 32)
3034 return this->g_o_t();
3035 else
3036 return (this->output_section()->address()
3037 + object->toc_base_offset()
3038 - this->address());
3041 // Ensure our GOT has a header.
3042 void
3043 set_final_data_size()
3045 if (this->header_ent_cnt_ != 0)
3046 this->make_header();
3047 Output_data_got<size, big_endian>::set_final_data_size();
3050 // First word of GOT header needs some values that are not
3051 // handled by Output_data_got so poke them in here.
3052 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3053 void
3054 do_write(Output_file* of)
3056 Valtype val = 0;
3057 if (size == 32 && this->layout_->dynamic_data() != NULL)
3058 val = this->layout_->dynamic_section()->address();
3059 if (size == 64)
3060 val = this->output_section()->address() + 0x8000;
3061 this->replace_constant(this->header_index_, val);
3062 Output_data_got<size, big_endian>::do_write(of);
3065 private:
3066 void
3067 reserve_ent(unsigned int cnt = 1)
3069 if (this->header_ent_cnt_ == 0)
3070 return;
3071 if (this->num_entries() + cnt > this->header_index_)
3072 this->make_header();
3075 void
3076 make_header()
3078 this->header_ent_cnt_ = 0;
3079 this->header_index_ = this->num_entries();
3080 if (size == 32)
3082 Output_data_got<size, big_endian>::add_constant(0);
3083 Output_data_got<size, big_endian>::add_constant(0);
3084 Output_data_got<size, big_endian>::add_constant(0);
3086 // Define _GLOBAL_OFFSET_TABLE_ at the header
3087 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3088 if (gotsym != NULL)
3090 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3091 sym->set_value(this->g_o_t());
3093 else
3094 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3095 Symbol_table::PREDEFINED,
3096 this, this->g_o_t(), 0,
3097 elfcpp::STT_OBJECT,
3098 elfcpp::STB_LOCAL,
3099 elfcpp::STV_HIDDEN, 0,
3100 false, false);
3102 else
3103 Output_data_got<size, big_endian>::add_constant(0);
3106 // Stashed pointers.
3107 Symbol_table* symtab_;
3108 Layout* layout_;
3110 // GOT header size.
3111 unsigned int header_ent_cnt_;
3112 // GOT header index.
3113 unsigned int header_index_;
3116 // Get the GOT section, creating it if necessary.
3118 template<int size, bool big_endian>
3119 Output_data_got_powerpc<size, big_endian>*
3120 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3121 Layout* layout)
3123 if (this->got_ == NULL)
3125 gold_assert(symtab != NULL && layout != NULL);
3127 this->got_
3128 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
3130 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3131 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3132 this->got_, ORDER_DATA, false);
3135 return this->got_;
3138 // Get the dynamic reloc section, creating it if necessary.
3140 template<int size, bool big_endian>
3141 typename Target_powerpc<size, big_endian>::Reloc_section*
3142 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3144 if (this->rela_dyn_ == NULL)
3146 gold_assert(layout != NULL);
3147 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3148 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3149 elfcpp::SHF_ALLOC, this->rela_dyn_,
3150 ORDER_DYNAMIC_RELOCS, false);
3152 return this->rela_dyn_;
3155 // Similarly, but for ifunc symbols get the one for ifunc.
3157 template<int size, bool big_endian>
3158 typename Target_powerpc<size, big_endian>::Reloc_section*
3159 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3160 Layout* layout,
3161 bool for_ifunc)
3163 if (!for_ifunc)
3164 return this->rela_dyn_section(layout);
3166 if (this->iplt_ == NULL)
3167 this->make_iplt_section(symtab, layout);
3168 return this->iplt_->rel_plt();
3171 class Stub_control
3173 public:
3174 // Determine the stub group size. The group size is the absolute
3175 // value of the parameter --stub-group-size. If --stub-group-size
3176 // is passed a negative value, we restrict stubs to be always after
3177 // the stubbed branches.
3178 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3179 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3180 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3181 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3182 owner_(NULL), output_section_(NULL)
3186 // Return true iff input section can be handled by current stub
3187 // group.
3188 bool
3189 can_add_to_stub_group(Output_section* o,
3190 const Output_section::Input_section* i,
3191 bool has14);
3193 const Output_section::Input_section*
3194 owner()
3195 { return owner_; }
3197 Output_section*
3198 output_section()
3199 { return output_section_; }
3201 void
3202 set_output_and_owner(Output_section* o,
3203 const Output_section::Input_section* i)
3205 this->output_section_ = o;
3206 this->owner_ = i;
3209 private:
3210 typedef enum
3212 // Initial state.
3213 NO_GROUP,
3214 // Adding group sections before the stubs.
3215 FINDING_STUB_SECTION,
3216 // Adding group sections after the stubs.
3217 HAS_STUB_SECTION
3218 } State;
3220 uint32_t stub_group_size_;
3221 bool stubs_always_after_branch_;
3222 bool suppress_size_errors_;
3223 // True if a stub group can serve multiple output sections.
3224 bool multi_os_;
3225 State state_;
3226 // Current max size of group. Starts at stub_group_size_ but is
3227 // reduced to stub_group_size_/1024 on seeing a section with
3228 // external conditional branches.
3229 uint32_t group_size_;
3230 uint64_t group_start_addr_;
3231 // owner_ and output_section_ specify the section to which stubs are
3232 // attached. The stubs are placed at the end of this section.
3233 const Output_section::Input_section* owner_;
3234 Output_section* output_section_;
3237 // Return true iff input section can be handled by current stub
3238 // group. Sections are presented to this function in order,
3239 // so the first section is the head of the group.
3241 bool
3242 Stub_control::can_add_to_stub_group(Output_section* o,
3243 const Output_section::Input_section* i,
3244 bool has14)
3246 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3247 uint64_t this_size;
3248 uint64_t start_addr = o->address();
3250 if (whole_sec)
3251 // .init and .fini sections are pasted together to form a single
3252 // function. We can't be adding stubs in the middle of the function.
3253 this_size = o->data_size();
3254 else
3256 start_addr += i->relobj()->output_section_offset(i->shndx());
3257 this_size = i->data_size();
3260 uint64_t end_addr = start_addr + this_size;
3261 uint32_t group_size = this->stub_group_size_;
3262 if (has14)
3263 this->group_size_ = group_size = group_size >> 10;
3265 if (this_size > group_size && !this->suppress_size_errors_)
3266 gold_warning(_("%s:%s exceeds group size"),
3267 i->relobj()->name().c_str(),
3268 i->relobj()->section_name(i->shndx()).c_str());
3270 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3271 has14 ? " 14bit" : "",
3272 i->relobj()->name().c_str(),
3273 i->relobj()->section_name(i->shndx()).c_str(),
3274 (long long) this_size,
3275 (this->state_ == NO_GROUP
3276 ? this_size
3277 : (long long) end_addr - this->group_start_addr_));
3279 if (this->state_ == NO_GROUP)
3281 // Only here on very first use of Stub_control
3282 this->owner_ = i;
3283 this->output_section_ = o;
3284 this->state_ = FINDING_STUB_SECTION;
3285 this->group_size_ = group_size;
3286 this->group_start_addr_ = start_addr;
3287 return true;
3289 else if (!this->multi_os_ && this->output_section_ != o)
3291 else if (this->state_ == HAS_STUB_SECTION)
3293 // Can we add this section, which is after the stubs, to the
3294 // group?
3295 if (end_addr - this->group_start_addr_ <= this->group_size_)
3296 return true;
3298 else if (this->state_ == FINDING_STUB_SECTION)
3300 if ((whole_sec && this->output_section_ == o)
3301 || end_addr - this->group_start_addr_ <= this->group_size_)
3303 // Stubs are added at the end of "owner_".
3304 this->owner_ = i;
3305 this->output_section_ = o;
3306 return true;
3308 // The group before the stubs has reached maximum size.
3309 // Now see about adding sections after the stubs to the
3310 // group. If the current section has a 14-bit branch and
3311 // the group before the stubs exceeds group_size_ (because
3312 // they didn't have 14-bit branches), don't add sections
3313 // after the stubs: The size of stubs for such a large
3314 // group may exceed the reach of a 14-bit branch.
3315 if (!this->stubs_always_after_branch_
3316 && this_size <= this->group_size_
3317 && start_addr - this->group_start_addr_ <= this->group_size_)
3319 gold_debug(DEBUG_TARGET, "adding after stubs");
3320 this->state_ = HAS_STUB_SECTION;
3321 this->group_start_addr_ = start_addr;
3322 return true;
3325 else
3326 gold_unreachable();
3328 gold_debug(DEBUG_TARGET,
3329 !this->multi_os_ && this->output_section_ != o
3330 ? "nope, new output section\n"
3331 : "nope, didn't fit\n");
3333 // The section fails to fit in the current group. Set up a few
3334 // things for the next group. owner_ and output_section_ will be
3335 // set later after we've retrieved those values for the current
3336 // group.
3337 this->state_ = FINDING_STUB_SECTION;
3338 this->group_size_ = group_size;
3339 this->group_start_addr_ = start_addr;
3340 return false;
3343 // Look over all the input sections, deciding where to place stubs.
3345 template<int size, bool big_endian>
3346 void
3347 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3348 const Task*,
3349 bool no_size_errors)
3351 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3352 parameters->options().stub_group_multi());
3354 // Group input sections and insert stub table
3355 Stub_table_owner* table_owner = NULL;
3356 std::vector<Stub_table_owner*> tables;
3357 Layout::Section_list section_list;
3358 layout->get_executable_sections(&section_list);
3359 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3360 for (Layout::Section_list::iterator o = section_list.begin();
3361 o != section_list.end();
3362 ++o)
3364 typedef Output_section::Input_section_list Input_section_list;
3365 for (Input_section_list::const_iterator i
3366 = (*o)->input_sections().begin();
3367 i != (*o)->input_sections().end();
3368 ++i)
3370 if (i->is_input_section()
3371 || i->is_relaxed_input_section())
3373 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3374 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3375 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3376 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3378 table_owner->output_section = stub_control.output_section();
3379 table_owner->owner = stub_control.owner();
3380 stub_control.set_output_and_owner(*o, &*i);
3381 table_owner = NULL;
3383 if (table_owner == NULL)
3385 table_owner = new Stub_table_owner;
3386 tables.push_back(table_owner);
3388 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3392 if (table_owner != NULL)
3394 table_owner->output_section = stub_control.output_section();
3395 table_owner->owner = stub_control.owner();;
3397 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3398 t != tables.end();
3399 ++t)
3401 Stub_table<size, big_endian>* stub_table;
3403 if ((*t)->owner->is_input_section())
3404 stub_table = new Stub_table<size, big_endian>(this,
3405 (*t)->output_section,
3406 (*t)->owner,
3407 this->stub_tables_.size());
3408 else if ((*t)->owner->is_relaxed_input_section())
3409 stub_table = static_cast<Stub_table<size, big_endian>*>(
3410 (*t)->owner->relaxed_input_section());
3411 else
3412 gold_unreachable();
3413 this->stub_tables_.push_back(stub_table);
3414 delete *t;
3418 template<int size>
3419 static unsigned long
3420 max_branch_delta (unsigned int r_type)
3422 if (r_type == elfcpp::R_POWERPC_REL14
3423 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3424 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3425 return 1L << 15;
3426 if (r_type == elfcpp::R_POWERPC_REL24
3427 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3428 || r_type == elfcpp::R_PPC_PLTREL24
3429 || r_type == elfcpp::R_PPC_LOCAL24PC)
3430 return 1L << 25;
3431 return 0;
3434 // Return whether this branch is going via a plt call stub.
3436 template<int size, bool big_endian>
3437 bool
3438 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3439 Powerpc_relobj<size, big_endian>* ppc_object,
3440 unsigned int shndx,
3441 Address offset,
3442 Target_powerpc* target,
3443 Symbol_table* symtab)
3445 if (this->object_ != ppc_object
3446 || this->shndx_ != shndx
3447 || this->offset_ != offset)
3448 return false;
3450 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3451 if (sym != NULL && sym->is_forwarder())
3452 sym = symtab->resolve_forwards(sym);
3453 if (target->replace_tls_get_addr(sym))
3454 sym = target->tls_get_addr_opt();
3455 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3456 if (gsym != NULL
3457 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3458 && !target->is_elfv2_localentry0(gsym))
3459 : (this->object_->local_has_plt_offset(this->r_sym_)
3460 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3462 this->tocsave_ = 1;
3463 return true;
3465 return false;
3468 // If this branch needs a plt call stub, or a long branch stub, make one.
3470 template<int size, bool big_endian>
3471 bool
3472 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3473 Stub_table<size, big_endian>* stub_table,
3474 Stub_table<size, big_endian>* ifunc_stub_table,
3475 Symbol_table* symtab) const
3477 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3478 Target_powerpc<size, big_endian>* target =
3479 static_cast<Target_powerpc<size, big_endian>*>(
3480 parameters->sized_target<size, big_endian>());
3481 if (sym != NULL && sym->is_forwarder())
3482 sym = symtab->resolve_forwards(sym);
3483 if (target->replace_tls_get_addr(sym))
3484 sym = target->tls_get_addr_opt();
3485 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3486 bool ok = true;
3488 if (gsym != NULL
3489 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3490 : this->object_->local_has_plt_offset(this->r_sym_))
3492 if (size == 64
3493 && gsym != NULL
3494 && target->abiversion() >= 2
3495 && !parameters->options().output_is_position_independent()
3496 && !is_branch_reloc<size>(this->r_type_))
3497 target->glink_section()->add_global_entry(gsym);
3498 else
3500 if (stub_table == NULL
3501 && !(size == 32
3502 && gsym != NULL
3503 && !parameters->options().output_is_position_independent()
3504 && !is_branch_reloc<size>(this->r_type_)))
3505 stub_table = this->object_->stub_table(this->shndx_);
3506 if (stub_table == NULL)
3508 // This is a ref from a data section to an ifunc symbol,
3509 // or a non-branch reloc for which we always want to use
3510 // one set of stubs for resolving function addresses.
3511 stub_table = ifunc_stub_table;
3513 gold_assert(stub_table != NULL);
3514 Address from = this->object_->get_output_section_offset(this->shndx_);
3515 if (from != invalid_address)
3516 from += (this->object_->output_section(this->shndx_)->address()
3517 + this->offset_);
3518 if (gsym != NULL)
3519 ok = stub_table->add_plt_call_entry(from,
3520 this->object_, gsym,
3521 this->r_type_, this->addend_,
3522 this->tocsave_);
3523 else
3524 ok = stub_table->add_plt_call_entry(from,
3525 this->object_, this->r_sym_,
3526 this->r_type_, this->addend_,
3527 this->tocsave_);
3530 else
3532 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3533 if (max_branch_offset == 0)
3534 return true;
3535 Address from = this->object_->get_output_section_offset(this->shndx_);
3536 gold_assert(from != invalid_address);
3537 from += (this->object_->output_section(this->shndx_)->address()
3538 + this->offset_);
3539 Address to;
3540 unsigned int other;
3541 if (gsym != NULL)
3543 switch (gsym->source())
3545 case Symbol::FROM_OBJECT:
3547 Object* symobj = gsym->object();
3548 if (symobj->is_dynamic()
3549 || symobj->pluginobj() != NULL)
3550 return true;
3551 bool is_ordinary;
3552 unsigned int shndx = gsym->shndx(&is_ordinary);
3553 if (shndx == elfcpp::SHN_UNDEF)
3554 return true;
3556 break;
3558 case Symbol::IS_UNDEFINED:
3559 return true;
3561 default:
3562 break;
3564 Symbol_table::Compute_final_value_status status;
3565 to = symtab->compute_final_value<size>(gsym, &status);
3566 if (status != Symbol_table::CFVS_OK)
3567 return true;
3568 other = gsym->nonvis() >> 3;
3570 else
3572 const Symbol_value<size>* psymval
3573 = this->object_->local_symbol(this->r_sym_);
3574 Symbol_value<size> symval;
3575 if (psymval->is_section_symbol())
3576 symval.set_is_section_symbol();
3577 typedef Sized_relobj_file<size, big_endian> ObjType;
3578 typename ObjType::Compute_final_local_value_status status
3579 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3580 &symval, symtab);
3581 if (status != ObjType::CFLV_OK
3582 || !symval.has_output_value())
3583 return true;
3584 to = symval.value(this->object_, 0);
3585 other = this->object_->st_other(this->r_sym_) >> 5;
3587 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3588 to += this->addend_;
3589 if (stub_table == NULL)
3590 stub_table = this->object_->stub_table(this->shndx_);
3591 if (size == 64 && target->abiversion() < 2)
3593 unsigned int dest_shndx;
3594 if (!target->symval_for_branch(symtab, gsym, this->object_,
3595 &to, &dest_shndx))
3596 return true;
3598 unsigned int local_ent = 0;
3599 if (size == 64
3600 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC)
3601 local_ent = elfcpp::ppc64_decode_local_entry(other);
3602 Address delta = to + local_ent - from;
3603 if (delta + max_branch_offset >= 2 * max_branch_offset
3604 || (size == 64
3605 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3606 && (gsym != NULL
3607 ? this->object_->ppc64_needs_toc(gsym)
3608 : this->object_->ppc64_needs_toc(this->r_sym_))))
3610 if (stub_table == NULL)
3612 gold_warning(_("%s:%s: branch in non-executable section,"
3613 " no long branch stub for you"),
3614 this->object_->name().c_str(),
3615 this->object_->section_name(this->shndx_).c_str());
3616 return true;
3618 bool save_res = (size == 64
3619 && gsym != NULL
3620 && gsym->source() == Symbol::IN_OUTPUT_DATA
3621 && gsym->output_data() == target->savres_section());
3622 ok = stub_table->add_long_branch_entry(this->object_,
3623 this->r_type_,
3624 from, to, other, save_res);
3627 if (!ok)
3628 gold_debug(DEBUG_TARGET,
3629 "branch at %s:%s+%#lx\n"
3630 "can't reach stub attached to %s:%s",
3631 this->object_->name().c_str(),
3632 this->object_->section_name(this->shndx_).c_str(),
3633 (unsigned long) this->offset_,
3634 stub_table->relobj()->name().c_str(),
3635 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3637 return ok;
3640 // Relaxation hook. This is where we do stub generation.
3642 template<int size, bool big_endian>
3643 bool
3644 Target_powerpc<size, big_endian>::do_relax(int pass,
3645 const Input_objects*,
3646 Symbol_table* symtab,
3647 Layout* layout,
3648 const Task* task)
3650 unsigned int prev_brlt_size = 0;
3651 if (pass == 1)
3653 bool thread_safe
3654 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3655 if (size == 64
3656 && this->abiversion() < 2
3657 && !thread_safe
3658 && !parameters->options().user_set_plt_thread_safe())
3660 static const char* const thread_starter[] =
3662 "pthread_create",
3663 /* libstdc++ */
3664 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3665 /* librt */
3666 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3667 "mq_notify", "create_timer",
3668 /* libanl */
3669 "getaddrinfo_a",
3670 /* libgomp */
3671 "GOMP_parallel",
3672 "GOMP_parallel_start",
3673 "GOMP_parallel_loop_static",
3674 "GOMP_parallel_loop_static_start",
3675 "GOMP_parallel_loop_dynamic",
3676 "GOMP_parallel_loop_dynamic_start",
3677 "GOMP_parallel_loop_guided",
3678 "GOMP_parallel_loop_guided_start",
3679 "GOMP_parallel_loop_runtime",
3680 "GOMP_parallel_loop_runtime_start",
3681 "GOMP_parallel_sections",
3682 "GOMP_parallel_sections_start",
3683 /* libgo */
3684 "__go_go",
3687 if (parameters->options().shared())
3688 thread_safe = true;
3689 else
3691 for (unsigned int i = 0;
3692 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3693 i++)
3695 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3696 thread_safe = (sym != NULL
3697 && sym->in_reg()
3698 && sym->in_real_elf());
3699 if (thread_safe)
3700 break;
3704 this->plt_thread_safe_ = thread_safe;
3707 if (pass == 1)
3709 this->stub_group_size_ = parameters->options().stub_group_size();
3710 bool no_size_errors = true;
3711 if (this->stub_group_size_ == 1)
3712 this->stub_group_size_ = 0x1c00000;
3713 else if (this->stub_group_size_ == -1)
3714 this->stub_group_size_ = -0x1e00000;
3715 else
3716 no_size_errors = false;
3717 this->group_sections(layout, task, no_size_errors);
3719 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3721 this->branch_lookup_table_.clear();
3722 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3723 p != this->stub_tables_.end();
3724 ++p)
3726 (*p)->clear_stubs(true);
3728 this->stub_tables_.clear();
3729 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3730 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3731 program_name, this->stub_group_size_);
3732 this->group_sections(layout, task, true);
3735 // We need address of stub tables valid for make_stub.
3736 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3737 p != this->stub_tables_.end();
3738 ++p)
3740 const Powerpc_relobj<size, big_endian>* object
3741 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3742 Address off = object->get_output_section_offset((*p)->shndx());
3743 gold_assert(off != invalid_address);
3744 Output_section* os = (*p)->output_section();
3745 (*p)->set_address_and_size(os, off);
3748 if (pass != 1)
3750 // Clear plt call stubs, long branch stubs and branch lookup table.
3751 prev_brlt_size = this->branch_lookup_table_.size();
3752 this->branch_lookup_table_.clear();
3753 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3754 p != this->stub_tables_.end();
3755 ++p)
3757 (*p)->clear_stubs(false);
3761 // Build all the stubs.
3762 this->relax_failed_ = false;
3763 Stub_table<size, big_endian>* ifunc_stub_table
3764 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3765 Stub_table<size, big_endian>* one_stub_table
3766 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3767 for (typename Branches::const_iterator b = this->branch_info_.begin();
3768 b != this->branch_info_.end();
3769 b++)
3771 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3772 && !this->relax_failed_)
3774 this->relax_failed_ = true;
3775 this->relax_fail_count_++;
3776 if (this->relax_fail_count_ < 3)
3777 return true;
3780 bool do_resize = false;
3781 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3782 p != this->stub_tables_.end();
3783 ++p)
3784 if ((*p)->need_resize())
3786 do_resize = true;
3787 break;
3789 if (do_resize)
3791 this->branch_lookup_table_.clear();
3792 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3793 p != this->stub_tables_.end();
3794 ++p)
3795 (*p)->set_resizing(true);
3796 for (typename Branches::const_iterator b = this->branch_info_.begin();
3797 b != this->branch_info_.end();
3798 b++)
3800 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3801 && !this->relax_failed_)
3803 this->relax_failed_ = true;
3804 this->relax_fail_count_++;
3805 if (this->relax_fail_count_ < 3)
3806 return true;
3809 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3810 p != this->stub_tables_.end();
3811 ++p)
3812 (*p)->set_resizing(false);
3815 // Did anything change size?
3816 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3817 bool again = num_huge_branches != prev_brlt_size;
3818 if (size == 64 && num_huge_branches != 0)
3819 this->make_brlt_section(layout);
3820 if (size == 64 && again)
3821 this->brlt_section_->set_current_size(num_huge_branches);
3823 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3824 p != this->stub_tables_.rend();
3825 ++p)
3826 (*p)->remove_eh_frame(layout);
3828 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3829 p != this->stub_tables_.end();
3830 ++p)
3831 (*p)->add_eh_frame(layout);
3833 typedef Unordered_set<Output_section*> Output_sections;
3834 Output_sections os_need_update;
3835 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3836 p != this->stub_tables_.end();
3837 ++p)
3839 if ((*p)->size_update())
3841 again = true;
3842 os_need_update.insert((*p)->output_section());
3846 // Set output section offsets for all input sections in an output
3847 // section that just changed size. Anything past the stubs will
3848 // need updating.
3849 for (typename Output_sections::iterator p = os_need_update.begin();
3850 p != os_need_update.end();
3851 p++)
3853 Output_section* os = *p;
3854 Address off = 0;
3855 typedef Output_section::Input_section_list Input_section_list;
3856 for (Input_section_list::const_iterator i = os->input_sections().begin();
3857 i != os->input_sections().end();
3858 ++i)
3860 off = align_address(off, i->addralign());
3861 if (i->is_input_section() || i->is_relaxed_input_section())
3862 i->relobj()->set_section_offset(i->shndx(), off);
3863 if (i->is_relaxed_input_section())
3865 Stub_table<size, big_endian>* stub_table
3866 = static_cast<Stub_table<size, big_endian>*>(
3867 i->relaxed_input_section());
3868 Address stub_table_size = stub_table->set_address_and_size(os, off);
3869 off += stub_table_size;
3870 // After a few iterations, set current stub table size
3871 // as min size threshold, so later stub tables can only
3872 // grow in size.
3873 if (pass >= 4)
3874 stub_table->set_min_size_threshold(stub_table_size);
3876 else
3877 off += i->data_size();
3879 // If .branch_lt is part of this output section, then we have
3880 // just done the offset adjustment.
3881 os->clear_section_offsets_need_adjustment();
3884 if (size == 64
3885 && !again
3886 && num_huge_branches != 0
3887 && parameters->options().output_is_position_independent())
3889 // Fill in the BRLT relocs.
3890 this->brlt_section_->reset_brlt_sizes();
3891 for (typename Branch_lookup_table::const_iterator p
3892 = this->branch_lookup_table_.begin();
3893 p != this->branch_lookup_table_.end();
3894 ++p)
3896 this->brlt_section_->add_reloc(p->first, p->second);
3898 this->brlt_section_->finalize_brlt_sizes();
3901 if (!again
3902 && (parameters->options().user_set_emit_stub_syms()
3903 ? parameters->options().emit_stub_syms()
3904 : (size == 64
3905 || parameters->options().output_is_position_independent()
3906 || parameters->options().emit_relocs())))
3908 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3909 p != this->stub_tables_.end();
3910 ++p)
3911 (*p)->define_stub_syms(symtab);
3913 if (this->glink_ != NULL)
3915 int stub_size = this->glink_->pltresolve_size();
3916 Address value = -stub_size;
3917 if (size == 64)
3919 value = 8;
3920 stub_size -= 8;
3922 this->define_local(symtab, "__glink_PLTresolve",
3923 this->glink_, value, stub_size);
3925 if (size != 64)
3926 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3930 return again;
3933 template<int size, bool big_endian>
3934 void
3935 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3936 unsigned char* oview,
3937 uint64_t* paddress,
3938 off_t* plen) const
3940 uint64_t address = plt->address();
3941 off_t len = plt->data_size();
3943 if (plt == this->glink_)
3945 // See Output_data_glink::do_write() for glink contents.
3946 if (len == 0)
3948 gold_assert(parameters->doing_static_link());
3949 // Static linking may need stubs, to support ifunc and long
3950 // branches. We need to create an output section for
3951 // .eh_frame early in the link process, to have a place to
3952 // attach stub .eh_frame info. We also need to have
3953 // registered a CIE that matches the stub CIE. Both of
3954 // these requirements are satisfied by creating an FDE and
3955 // CIE for .glink, even though static linking will leave
3956 // .glink zero length.
3957 // ??? Hopefully generating an FDE with a zero address range
3958 // won't confuse anything that consumes .eh_frame info.
3960 else if (size == 64)
3962 // There is one word before __glink_PLTresolve
3963 address += 8;
3964 len -= 8;
3966 else if (parameters->options().output_is_position_independent())
3968 // There are two FDEs for a position independent glink.
3969 // The first covers the branch table, the second
3970 // __glink_PLTresolve at the end of glink.
3971 off_t resolve_size = this->glink_->pltresolve_size();
3972 if (oview[9] == elfcpp::DW_CFA_nop)
3973 len -= resolve_size;
3974 else
3976 address += len - resolve_size;
3977 len = resolve_size;
3981 else
3983 // Must be a stub table.
3984 const Stub_table<size, big_endian>* stub_table
3985 = static_cast<const Stub_table<size, big_endian>*>(plt);
3986 uint64_t stub_address = stub_table->stub_address();
3987 len -= stub_address - address;
3988 address = stub_address;
3991 *paddress = address;
3992 *plen = len;
3995 // A class to handle the PLT data.
3997 template<int size, bool big_endian>
3998 class Output_data_plt_powerpc : public Output_section_data_build
4000 public:
4001 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4002 size, big_endian> Reloc_section;
4004 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4005 Reloc_section* plt_rel,
4006 const char* name)
4007 : Output_section_data_build(size == 32 ? 4 : 8),
4008 rel_(plt_rel),
4009 targ_(targ),
4010 name_(name)
4013 // Add an entry to the PLT.
4014 void
4015 add_entry(Symbol*);
4017 void
4018 add_ifunc_entry(Symbol*);
4020 void
4021 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4023 void
4024 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4026 // Return the .rela.plt section data.
4027 Reloc_section*
4028 rel_plt() const
4030 return this->rel_;
4033 // Return the number of PLT entries.
4034 unsigned int
4035 entry_count() const
4037 if (this->current_data_size() == 0)
4038 return 0;
4039 return ((this->current_data_size() - this->first_plt_entry_offset())
4040 / this->plt_entry_size());
4043 protected:
4044 void
4045 do_adjust_output_section(Output_section* os)
4047 os->set_entsize(0);
4050 // Write to a map file.
4051 void
4052 do_print_to_mapfile(Mapfile* mapfile) const
4053 { mapfile->print_output_data(this, this->name_); }
4055 private:
4056 // Return the offset of the first non-reserved PLT entry.
4057 unsigned int
4058 first_plt_entry_offset() const
4060 // IPLT and LPLT have no reserved entry.
4061 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4062 return 0;
4063 return this->targ_->first_plt_entry_offset();
4066 // Return the size of each PLT entry.
4067 unsigned int
4068 plt_entry_size() const
4070 return this->targ_->plt_entry_size();
4073 // Write out the PLT data.
4074 void
4075 do_write(Output_file*);
4077 // The reloc section.
4078 Reloc_section* rel_;
4079 // Allows access to .glink for do_write.
4080 Target_powerpc<size, big_endian>* targ_;
4081 // What to report in map file.
4082 const char *name_;
4085 // Add an entry to the PLT.
4087 template<int size, bool big_endian>
4088 void
4089 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
4091 if (!gsym->has_plt_offset())
4093 section_size_type off = this->current_data_size();
4094 if (off == 0)
4095 off += this->first_plt_entry_offset();
4096 gsym->set_plt_offset(off);
4097 gsym->set_needs_dynsym_entry();
4098 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4099 this->rel_->add_global(gsym, dynrel, this, off, 0);
4100 off += this->plt_entry_size();
4101 this->set_current_data_size(off);
4105 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4107 template<int size, bool big_endian>
4108 void
4109 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4111 if (!gsym->has_plt_offset())
4113 section_size_type off = this->current_data_size();
4114 gsym->set_plt_offset(off);
4115 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4116 if (size == 64 && this->targ_->abiversion() < 2)
4117 dynrel = elfcpp::R_PPC64_JMP_IREL;
4118 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4119 off += this->plt_entry_size();
4120 this->set_current_data_size(off);
4124 // Add an entry for a local symbol to the PLT.
4126 template<int size, bool big_endian>
4127 void
4128 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4129 Sized_relobj_file<size, big_endian>* relobj,
4130 unsigned int local_sym_index)
4132 if (!relobj->local_has_plt_offset(local_sym_index))
4134 section_size_type off = this->current_data_size();
4135 relobj->set_local_plt_offset(local_sym_index, off);
4136 if (this->rel_)
4138 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4139 if (size == 64 && this->targ_->abiversion() < 2)
4140 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4141 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4142 dynrel, this, off, 0);
4144 off += this->plt_entry_size();
4145 this->set_current_data_size(off);
4149 // Add an entry for a local ifunc symbol to the IPLT.
4151 template<int size, bool big_endian>
4152 void
4153 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4154 Sized_relobj_file<size, big_endian>* relobj,
4155 unsigned int local_sym_index)
4157 if (!relobj->local_has_plt_offset(local_sym_index))
4159 section_size_type off = this->current_data_size();
4160 relobj->set_local_plt_offset(local_sym_index, off);
4161 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4162 if (size == 64 && this->targ_->abiversion() < 2)
4163 dynrel = elfcpp::R_PPC64_JMP_IREL;
4164 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4165 this, off, 0);
4166 off += this->plt_entry_size();
4167 this->set_current_data_size(off);
4171 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4172 static const uint32_t add_2_2_11 = 0x7c425a14;
4173 static const uint32_t add_2_2_12 = 0x7c426214;
4174 static const uint32_t add_3_3_2 = 0x7c631214;
4175 static const uint32_t add_3_3_13 = 0x7c636a14;
4176 static const uint32_t add_3_12_2 = 0x7c6c1214;
4177 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4178 static const uint32_t add_11_0_11 = 0x7d605a14;
4179 static const uint32_t add_11_2_11 = 0x7d625a14;
4180 static const uint32_t add_11_11_2 = 0x7d6b1214;
4181 static const uint32_t add_12_11_12 = 0x7d8b6214;
4182 static const uint32_t addi_0_12 = 0x380c0000;
4183 static const uint32_t addi_2_2 = 0x38420000;
4184 static const uint32_t addi_3_3 = 0x38630000;
4185 static const uint32_t addi_11_11 = 0x396b0000;
4186 static const uint32_t addi_12_1 = 0x39810000;
4187 static const uint32_t addi_12_11 = 0x398b0000;
4188 static const uint32_t addi_12_12 = 0x398c0000;
4189 static const uint32_t addis_0_2 = 0x3c020000;
4190 static const uint32_t addis_0_13 = 0x3c0d0000;
4191 static const uint32_t addis_2_12 = 0x3c4c0000;
4192 static const uint32_t addis_11_2 = 0x3d620000;
4193 static const uint32_t addis_11_11 = 0x3d6b0000;
4194 static const uint32_t addis_11_30 = 0x3d7e0000;
4195 static const uint32_t addis_12_1 = 0x3d810000;
4196 static const uint32_t addis_12_2 = 0x3d820000;
4197 static const uint32_t addis_12_11 = 0x3d8b0000;
4198 static const uint32_t addis_12_12 = 0x3d8c0000;
4199 static const uint32_t b = 0x48000000;
4200 static const uint32_t bcl_20_31 = 0x429f0005;
4201 static const uint32_t bctr = 0x4e800420;
4202 static const uint32_t bctrl = 0x4e800421;
4203 static const uint32_t beqlr = 0x4d820020;
4204 static const uint32_t blr = 0x4e800020;
4205 static const uint32_t bnectr_p4 = 0x4ce20420;
4206 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4207 static const uint32_t cmpldi_2_0 = 0x28220000;
4208 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4209 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4210 static const uint32_t cror_15_15_15 = 0x4def7b82;
4211 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4212 static const uint32_t ld_0_1 = 0xe8010000;
4213 static const uint32_t ld_0_11 = 0xe80b0000;
4214 static const uint32_t ld_0_12 = 0xe80c0000;
4215 static const uint32_t ld_2_1 = 0xe8410000;
4216 static const uint32_t ld_2_2 = 0xe8420000;
4217 static const uint32_t ld_2_11 = 0xe84b0000;
4218 static const uint32_t ld_2_12 = 0xe84c0000;
4219 static const uint32_t ld_11_1 = 0xe9610000;
4220 static const uint32_t ld_11_2 = 0xe9620000;
4221 static const uint32_t ld_11_3 = 0xe9630000;
4222 static const uint32_t ld_11_11 = 0xe96b0000;
4223 static const uint32_t ld_12_2 = 0xe9820000;
4224 static const uint32_t ld_12_3 = 0xe9830000;
4225 static const uint32_t ld_12_11 = 0xe98b0000;
4226 static const uint32_t ld_12_12 = 0xe98c0000;
4227 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4228 static const uint32_t lfd_0_1 = 0xc8010000;
4229 static const uint32_t li_0_0 = 0x38000000;
4230 static const uint32_t li_11_0 = 0x39600000;
4231 static const uint32_t li_12_0 = 0x39800000;
4232 static const uint32_t lis_0 = 0x3c000000;
4233 static const uint32_t lis_2 = 0x3c400000;
4234 static const uint32_t lis_11 = 0x3d600000;
4235 static const uint32_t lis_12 = 0x3d800000;
4236 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4237 static const uint32_t lwz_0_12 = 0x800c0000;
4238 static const uint32_t lwz_11_3 = 0x81630000;
4239 static const uint32_t lwz_11_11 = 0x816b0000;
4240 static const uint32_t lwz_11_30 = 0x817e0000;
4241 static const uint32_t lwz_12_3 = 0x81830000;
4242 static const uint32_t lwz_12_12 = 0x818c0000;
4243 static const uint32_t lwzu_0_12 = 0x840c0000;
4244 static const uint32_t mflr_0 = 0x7c0802a6;
4245 static const uint32_t mflr_11 = 0x7d6802a6;
4246 static const uint32_t mflr_12 = 0x7d8802a6;
4247 static const uint32_t mr_0_3 = 0x7c601b78;
4248 static const uint32_t mr_3_0 = 0x7c030378;
4249 static const uint32_t mtctr_0 = 0x7c0903a6;
4250 static const uint32_t mtctr_11 = 0x7d6903a6;
4251 static const uint32_t mtctr_12 = 0x7d8903a6;
4252 static const uint32_t mtlr_0 = 0x7c0803a6;
4253 static const uint32_t mtlr_11 = 0x7d6803a6;
4254 static const uint32_t mtlr_12 = 0x7d8803a6;
4255 static const uint32_t nop = 0x60000000;
4256 static const uint32_t ori_0_0_0 = 0x60000000;
4257 static const uint32_t ori_11_11_0 = 0x616b0000;
4258 static const uint32_t ori_12_12_0 = 0x618c0000;
4259 static const uint32_t oris_12_12_0 = 0x658c0000;
4260 static const uint32_t sldi_11_11_34 = 0x796b1746;
4261 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4262 static const uint32_t srdi_0_0_2 = 0x7800f082;
4263 static const uint32_t std_0_1 = 0xf8010000;
4264 static const uint32_t std_0_12 = 0xf80c0000;
4265 static const uint32_t std_2_1 = 0xf8410000;
4266 static const uint32_t std_11_1 = 0xf9610000;
4267 static const uint32_t stfd_0_1 = 0xd8010000;
4268 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4269 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4270 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4271 static const uint32_t xor_2_12_12 = 0x7d826278;
4272 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4274 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4275 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4276 static const uint64_t pnop = 0x0700000000000000ULL;
4278 // Write out the PLT.
4280 template<int size, bool big_endian>
4281 void
4282 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4284 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4286 const section_size_type offset = this->offset();
4287 const section_size_type oview_size
4288 = convert_to_section_size_type(this->data_size());
4289 unsigned char* const oview = of->get_output_view(offset, oview_size);
4290 unsigned char* pov = oview;
4291 unsigned char* endpov = oview + oview_size;
4293 // The address of the .glink branch table
4294 const Output_data_glink<size, big_endian>* glink
4295 = this->targ_->glink_section();
4296 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4298 while (pov < endpov)
4300 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4301 pov += 4;
4302 branch_tab += 4;
4305 of->write_output_view(offset, oview_size, oview);
4309 // Create the PLT section.
4311 template<int size, bool big_endian>
4312 void
4313 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4314 Layout* layout)
4316 if (this->plt_ == NULL)
4318 if (this->got_ == NULL)
4319 this->got_section(symtab, layout);
4321 if (this->glink_ == NULL)
4322 make_glink_section(layout);
4324 // Ensure that .rela.dyn always appears before .rela.plt This is
4325 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4326 // needs to include .rela.plt in its range.
4327 this->rela_dyn_section(layout);
4329 Reloc_section* plt_rel = new Reloc_section(false);
4330 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4331 elfcpp::SHF_ALLOC, plt_rel,
4332 ORDER_DYNAMIC_PLT_RELOCS, false);
4333 this->plt_
4334 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4335 "** PLT");
4336 layout->add_output_section_data(".plt",
4337 (size == 32
4338 ? elfcpp::SHT_PROGBITS
4339 : elfcpp::SHT_NOBITS),
4340 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4341 this->plt_,
4342 (size == 32
4343 ? ORDER_SMALL_DATA
4344 : ORDER_SMALL_BSS),
4345 false);
4347 Output_section* rela_plt_os = plt_rel->output_section();
4348 rela_plt_os->set_info_section(this->plt_->output_section());
4352 // Create the IPLT section.
4354 template<int size, bool big_endian>
4355 void
4356 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4357 Layout* layout)
4359 if (this->iplt_ == NULL)
4361 this->make_plt_section(symtab, layout);
4362 this->make_lplt_section(layout);
4364 Reloc_section* iplt_rel = new Reloc_section(false);
4365 if (this->rela_dyn_->output_section())
4366 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4367 this->iplt_
4368 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4369 "** IPLT");
4370 if (this->plt_->output_section())
4371 this->plt_->output_section()->add_output_section_data(this->iplt_);
4375 // Create the LPLT section.
4377 template<int size, bool big_endian>
4378 void
4379 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4381 if (this->lplt_ == NULL)
4383 Reloc_section* lplt_rel = NULL;
4384 if (parameters->options().output_is_position_independent())
4386 lplt_rel = new Reloc_section(false);
4387 this->rela_dyn_section(layout);
4388 if (this->rela_dyn_->output_section())
4389 this->rela_dyn_->output_section()
4390 ->add_output_section_data(lplt_rel);
4392 this->lplt_
4393 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4394 "** LPLT");
4395 this->make_brlt_section(layout);
4396 if (this->brlt_section_ && this->brlt_section_->output_section())
4397 this->brlt_section_->output_section()
4398 ->add_output_section_data(this->lplt_);
4399 else
4400 layout->add_output_section_data(".branch_lt",
4401 elfcpp::SHT_PROGBITS,
4402 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4403 this->lplt_,
4404 ORDER_RELRO,
4405 true);
4409 // A section for huge long branch addresses, similar to plt section.
4411 template<int size, bool big_endian>
4412 class Output_data_brlt_powerpc : public Output_section_data_build
4414 public:
4415 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4416 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4417 size, big_endian> Reloc_section;
4419 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4420 Reloc_section* brlt_rel)
4421 : Output_section_data_build(size == 32 ? 4 : 8),
4422 rel_(brlt_rel),
4423 targ_(targ)
4426 void
4427 reset_brlt_sizes()
4429 this->reset_data_size();
4430 this->rel_->reset_data_size();
4433 void
4434 finalize_brlt_sizes()
4436 this->finalize_data_size();
4437 this->rel_->finalize_data_size();
4440 // Add a reloc for an entry in the BRLT.
4441 void
4442 add_reloc(Address to, unsigned int off)
4443 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4445 // Update section and reloc section size.
4446 void
4447 set_current_size(unsigned int num_branches)
4449 this->reset_address_and_file_offset();
4450 this->set_current_data_size(num_branches * 16);
4451 this->finalize_data_size();
4452 Output_section* os = this->output_section();
4453 os->set_section_offsets_need_adjustment();
4454 if (this->rel_ != NULL)
4456 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4457 this->rel_->reset_address_and_file_offset();
4458 this->rel_->set_current_data_size(num_branches * reloc_size);
4459 this->rel_->finalize_data_size();
4460 Output_section* os = this->rel_->output_section();
4461 os->set_section_offsets_need_adjustment();
4465 protected:
4466 void
4467 do_adjust_output_section(Output_section* os)
4469 os->set_entsize(0);
4472 // Write to a map file.
4473 void
4474 do_print_to_mapfile(Mapfile* mapfile) const
4475 { mapfile->print_output_data(this, "** BRLT"); }
4477 private:
4478 // Write out the BRLT data.
4479 void
4480 do_write(Output_file*);
4482 // The reloc section.
4483 Reloc_section* rel_;
4484 Target_powerpc<size, big_endian>* targ_;
4487 // Make the branch lookup table section.
4489 template<int size, bool big_endian>
4490 void
4491 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4493 if (size == 64 && this->brlt_section_ == NULL)
4495 Reloc_section* brlt_rel = NULL;
4496 bool is_pic = parameters->options().output_is_position_independent();
4497 if (is_pic)
4499 // When PIC we can't fill in .branch_lt but must initialise at
4500 // runtime via dynamic relocations.
4501 this->rela_dyn_section(layout);
4502 brlt_rel = new Reloc_section(false);
4503 if (this->rela_dyn_->output_section())
4504 this->rela_dyn_->output_section()
4505 ->add_output_section_data(brlt_rel);
4507 this->brlt_section_
4508 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4509 if (this->plt_ && is_pic && this->plt_->output_section())
4510 this->plt_->output_section()
4511 ->add_output_section_data(this->brlt_section_);
4512 else
4513 layout->add_output_section_data(".branch_lt",
4514 elfcpp::SHT_PROGBITS,
4515 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4516 this->brlt_section_,
4517 ORDER_RELRO,
4518 true);
4522 // Write out .branch_lt when non-PIC.
4524 template<int size, bool big_endian>
4525 void
4526 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4528 if (size == 64 && !parameters->options().output_is_position_independent())
4530 const section_size_type offset = this->offset();
4531 const section_size_type oview_size
4532 = convert_to_section_size_type(this->data_size());
4533 unsigned char* const oview = of->get_output_view(offset, oview_size);
4535 this->targ_->write_branch_lookup_table(oview);
4536 of->write_output_view(offset, oview_size, oview);
4540 static inline uint32_t
4541 l(uint32_t a)
4543 return a & 0xffff;
4546 static inline uint32_t
4547 hi(uint32_t a)
4549 return l(a >> 16);
4552 static inline uint32_t
4553 ha(uint32_t a)
4555 return hi(a + 0x8000);
4558 static inline uint64_t
4559 d34(uint64_t v)
4561 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4564 static inline uint64_t
4565 ha34(uint64_t v)
4567 return (v + (1ULL << 33)) >> 34;
4570 template<int size>
4571 struct Eh_cie
4573 static const unsigned char eh_frame_cie[12];
4576 template<int size>
4577 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4579 1, // CIE version.
4580 'z', 'R', 0, // Augmentation string.
4581 4, // Code alignment.
4582 0x80 - size / 8 , // Data alignment.
4583 65, // RA reg.
4584 1, // Augmentation size.
4585 (elfcpp::DW_EH_PE_pcrel
4586 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4587 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4590 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4591 static const unsigned char glink_eh_frame_fde_64v1[] =
4593 0, 0, 0, 0, // Replaced with offset to .glink.
4594 0, 0, 0, 0, // Replaced with size of .glink.
4595 0, // Augmentation size.
4596 elfcpp::DW_CFA_advance_loc + 2,
4597 elfcpp::DW_CFA_register, 65, 12,
4598 elfcpp::DW_CFA_advance_loc + 4,
4599 elfcpp::DW_CFA_restore_extended, 65
4602 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4603 static const unsigned char glink_eh_frame_fde_64v2[] =
4605 0, 0, 0, 0, // Replaced with offset to .glink.
4606 0, 0, 0, 0, // Replaced with size of .glink.
4607 0, // Augmentation size.
4608 elfcpp::DW_CFA_advance_loc + 2,
4609 elfcpp::DW_CFA_register, 65, 0,
4610 elfcpp::DW_CFA_advance_loc + 2,
4611 elfcpp::DW_CFA_restore_extended, 65
4614 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4616 0, 0, 0, 0, // Replaced with offset to .glink.
4617 0, 0, 0, 0, // Replaced with size of .glink.
4618 0, // Augmentation size.
4619 elfcpp::DW_CFA_advance_loc + 3,
4620 elfcpp::DW_CFA_register, 65, 0,
4621 elfcpp::DW_CFA_advance_loc + 2,
4622 elfcpp::DW_CFA_restore_extended, 65
4625 // Describe __glink_PLTresolve use of LR, 32-bit version.
4626 static const unsigned char glink_eh_frame_fde_32[] =
4628 0, 0, 0, 0, // Replaced with offset to .glink.
4629 0, 0, 0, 0, // Replaced with size of .glink.
4630 0, // Augmentation size.
4631 elfcpp::DW_CFA_advance_loc + 2,
4632 elfcpp::DW_CFA_register, 65, 0,
4633 elfcpp::DW_CFA_advance_loc + 4,
4634 elfcpp::DW_CFA_restore_extended, 65
4637 static const unsigned char default_fde[] =
4639 0, 0, 0, 0, // Replaced with offset to stubs.
4640 0, 0, 0, 0, // Replaced with size of stubs.
4641 0, // Augmentation size.
4642 elfcpp::DW_CFA_nop, // Pad.
4643 elfcpp::DW_CFA_nop,
4644 elfcpp::DW_CFA_nop
4647 template<bool big_endian>
4648 static inline void
4649 write_insn(unsigned char* p, uint32_t v)
4651 elfcpp::Swap<32, big_endian>::writeval(p, v);
4654 template<int size>
4655 static inline unsigned int
4656 param_plt_align()
4658 if (!parameters->options().user_set_plt_align())
4659 return size == 64 ? 32 : 8;
4660 return 1 << parameters->options().plt_align();
4663 // Stub_table holds information about plt and long branch stubs.
4664 // Stubs are built in an area following some input section determined
4665 // by group_sections(). This input section is converted to a relaxed
4666 // input section allowing it to be resized to accommodate the stubs
4668 template<int size, bool big_endian>
4669 class Stub_table : public Output_relaxed_input_section
4671 public:
4672 struct Plt_stub_ent
4674 Plt_stub_ent(unsigned int off, unsigned int indx)
4675 : off_(off), indx_(indx), iter_(0), notoc_(0), toc_(0),
4676 r2save_(0), localentry0_(0), tocoff_(0)
4679 unsigned int off_;
4680 unsigned int indx_;
4681 unsigned int iter_ : 1;
4682 unsigned int notoc_ : 1;
4683 unsigned int toc_ : 1;
4684 unsigned int r2save_ : 1;
4685 unsigned int localentry0_ : 1;
4686 unsigned int tocoff_ : 8;
4688 struct Branch_stub_ent
4690 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4691 : off_(off), iter_(0), notoc_(notoc), toc_(0), save_res_(save_res),
4692 other_(0), tocoff_(0)
4695 unsigned int off_;
4696 unsigned int iter_ : 1;
4697 unsigned int notoc_ : 1;
4698 unsigned int toc_ : 1;
4699 unsigned int save_res_ : 1;
4700 unsigned int other_ : 3;
4701 unsigned int tocoff_ : 8;
4703 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4704 static const Address invalid_address = static_cast<Address>(0) - 1;
4706 Stub_table(Target_powerpc<size, big_endian>* targ,
4707 Output_section* output_section,
4708 const Output_section::Input_section* owner,
4709 uint32_t id)
4710 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4711 owner->relobj()
4712 ->section_addralign(owner->shndx())),
4713 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4714 orig_data_size_(owner->current_data_size()),
4715 plt_size_(0), last_plt_size_(0),
4716 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4717 need_save_res_(false), need_resize_(false), resizing_(false),
4718 uniq_(id)
4720 this->set_output_section(output_section);
4722 std::vector<Output_relaxed_input_section*> new_relaxed;
4723 new_relaxed.push_back(this);
4724 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4727 // Add a plt call stub.
4728 bool
4729 add_plt_call_entry(Address,
4730 const Sized_relobj_file<size, big_endian>*,
4731 const Symbol*,
4732 unsigned int,
4733 Address,
4734 bool);
4736 bool
4737 add_plt_call_entry(Address,
4738 const Sized_relobj_file<size, big_endian>*,
4739 unsigned int,
4740 unsigned int,
4741 Address,
4742 bool);
4744 // Find a given plt call stub.
4745 const Plt_stub_ent*
4746 find_plt_call_entry(const Symbol*) const;
4748 const Plt_stub_ent*
4749 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4750 unsigned int) const;
4752 const Plt_stub_ent*
4753 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4754 const Symbol*,
4755 unsigned int,
4756 Address) const;
4758 const Plt_stub_ent*
4759 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4760 unsigned int,
4761 unsigned int,
4762 Address) const;
4764 // Add a long branch stub.
4765 bool
4766 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4767 unsigned int, Address, Address, unsigned int, bool);
4769 const Branch_stub_ent*
4770 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4771 Address) const;
4773 bool
4774 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4776 Address max_branch_offset = max_branch_delta<size>(r_type);
4777 if (max_branch_offset == 0)
4778 return true;
4779 gold_assert(from != invalid_address);
4780 Address loc = off + this->stub_address();
4781 return loc - from + max_branch_offset < 2 * max_branch_offset;
4784 void
4785 clear_stubs(bool all)
4787 this->plt_call_stubs_.clear();
4788 this->plt_size_ = 0;
4789 this->long_branch_stubs_.clear();
4790 this->branch_size_ = 0;
4791 this->need_save_res_ = false;
4792 if (all)
4794 this->last_plt_size_ = 0;
4795 this->last_branch_size_ = 0;
4799 bool
4800 need_resize() const
4801 { return need_resize_; }
4803 void
4804 set_resizing(bool val)
4806 this->resizing_ = val;
4807 if (val)
4809 this->need_resize_ = false;
4810 this->plt_size_ = 0;
4811 this->branch_size_ = 0;
4812 this->need_save_res_ = false;
4816 Address
4817 set_address_and_size(const Output_section* os, Address off)
4819 Address start_off = off;
4820 off += this->orig_data_size_;
4821 Address my_size = this->plt_size_ + this->branch_size_;
4822 if (this->need_save_res_)
4823 my_size += this->targ_->savres_section()->data_size();
4824 if (my_size != 0)
4825 off = align_address(off, this->stub_align());
4826 // Include original section size and alignment padding in size
4827 my_size += off - start_off;
4828 // Ensure new size is always larger than min size
4829 // threshold. Alignment requirement is included in "my_size", so
4830 // increase "my_size" does not invalidate alignment.
4831 if (my_size < this->min_size_threshold_)
4832 my_size = this->min_size_threshold_;
4833 this->reset_address_and_file_offset();
4834 this->set_current_data_size(my_size);
4835 this->set_address_and_file_offset(os->address() + start_off,
4836 os->offset() + start_off);
4837 return my_size;
4840 Address
4841 stub_address() const
4843 return align_address(this->address() + this->orig_data_size_,
4844 this->stub_align());
4847 Address
4848 stub_offset() const
4850 return align_address(this->offset() + this->orig_data_size_,
4851 this->stub_align());
4854 section_size_type
4855 plt_size() const
4856 { return this->plt_size_; }
4858 section_size_type
4859 branch_size() const
4860 { return this->branch_size_; }
4862 void
4863 set_min_size_threshold(Address min_size)
4864 { this->min_size_threshold_ = min_size; }
4866 void
4867 define_stub_syms(Symbol_table*);
4869 bool
4870 size_update()
4872 Output_section* os = this->output_section();
4873 if (os->addralign() < this->stub_align())
4875 os->set_addralign(this->stub_align());
4876 // FIXME: get rid of the insane checkpointing.
4877 // We can't increase alignment of the input section to which
4878 // stubs are attached; The input section may be .init which
4879 // is pasted together with other .init sections to form a
4880 // function. Aligning might insert zero padding resulting in
4881 // sigill. However we do need to increase alignment of the
4882 // output section so that the align_address() on offset in
4883 // set_address_and_size() adds the same padding as the
4884 // align_address() on address in stub_address().
4885 // What's more, we need this alignment for the layout done in
4886 // relaxation_loop_body() so that the output section starts at
4887 // a suitably aligned address.
4888 os->checkpoint_set_addralign(this->stub_align());
4890 if (this->last_plt_size_ != this->plt_size_
4891 || this->last_branch_size_ != this->branch_size_)
4893 this->last_plt_size_ = this->plt_size_;
4894 this->last_branch_size_ = this->branch_size_;
4895 return true;
4897 return false;
4900 // Add .eh_frame info for this stub section.
4901 void
4902 add_eh_frame(Layout* layout);
4904 // Remove .eh_frame info for this stub section.
4905 void
4906 remove_eh_frame(Layout* layout);
4908 Target_powerpc<size, big_endian>*
4909 targ() const
4910 { return targ_; }
4912 private:
4913 class Plt_stub_key;
4914 class Plt_stub_key_hash;
4915 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4916 Plt_stub_key_hash> Plt_stub_entries;
4917 class Branch_stub_key;
4918 class Branch_stub_key_hash;
4919 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4920 Branch_stub_key_hash> Branch_stub_entries;
4922 // Alignment of stub section.
4923 unsigned int
4924 stub_align() const
4926 unsigned int min_align = size == 64 ? 32 : 16;
4927 unsigned int user_align = 1 << parameters->options().plt_align();
4928 return std::max(user_align, min_align);
4931 // Return the plt offset for the given call stub.
4932 Address
4933 plt_off(typename Plt_stub_entries::const_iterator p,
4934 const Output_data_plt_powerpc<size, big_endian>** sec) const
4936 const Symbol* gsym = p->first.sym_;
4937 if (gsym != NULL)
4938 return this->targ_->plt_off(gsym, sec);
4939 else
4941 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4942 unsigned int local_sym_index = p->first.locsym_;
4943 return this->targ_->plt_off(relobj, local_sym_index, sec);
4947 // Size of a given plt call stub.
4948 unsigned int
4949 plt_call_size(typename Plt_stub_entries::iterator p) const;
4951 unsigned int
4952 plt_call_align(unsigned int bytes) const
4954 unsigned int align = param_plt_align<size>();
4955 return (bytes + align - 1) & -align;
4958 // Return long branch stub size.
4959 unsigned int
4960 branch_stub_size(typename Branch_stub_entries::iterator p,
4961 bool* need_lt);
4963 void
4964 build_tls_opt_head(unsigned char** pp, bool save_lr);
4966 void
4967 build_tls_opt_tail(unsigned char* p);
4969 void
4970 plt_error(const Plt_stub_key& p);
4972 // Write out stubs.
4973 void
4974 do_write(Output_file*);
4976 // Plt call stub keys.
4977 class Plt_stub_key
4979 public:
4980 Plt_stub_key(const Symbol* sym)
4981 : sym_(sym), object_(0), addend_(0), locsym_(0)
4984 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4985 unsigned int locsym_index)
4986 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4989 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4990 const Symbol* sym,
4991 unsigned int r_type,
4992 Address addend)
4993 : sym_(sym), object_(0), addend_(0), locsym_(0)
4995 if (size != 32)
4996 this->addend_ = addend;
4997 else if (parameters->options().output_is_position_independent()
4998 && (r_type == elfcpp::R_PPC_PLTREL24
4999 || r_type == elfcpp::R_POWERPC_PLTCALL))
5001 this->addend_ = addend;
5002 if (this->addend_ >= 32768)
5003 this->object_ = object;
5007 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5008 unsigned int locsym_index,
5009 unsigned int r_type,
5010 Address addend)
5011 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5013 if (size != 32)
5014 this->addend_ = addend;
5015 else if (parameters->options().output_is_position_independent()
5016 && (r_type == elfcpp::R_PPC_PLTREL24
5017 || r_type == elfcpp::R_POWERPC_PLTCALL))
5018 this->addend_ = addend;
5021 bool operator==(const Plt_stub_key& that) const
5023 return (this->sym_ == that.sym_
5024 && this->object_ == that.object_
5025 && this->addend_ == that.addend_
5026 && this->locsym_ == that.locsym_);
5029 const Symbol* sym_;
5030 const Sized_relobj_file<size, big_endian>* object_;
5031 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5032 unsigned int locsym_;
5035 class Plt_stub_key_hash
5037 public:
5038 size_t operator()(const Plt_stub_key& ent) const
5040 return (reinterpret_cast<uintptr_t>(ent.sym_)
5041 ^ reinterpret_cast<uintptr_t>(ent.object_)
5042 ^ ent.addend_
5043 ^ ent.locsym_);
5047 // Long branch stub keys.
5048 class Branch_stub_key
5050 public:
5051 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
5052 : dest_(to), toc_base_off_(0)
5054 if (size == 64)
5055 toc_base_off_ = obj->toc_base_offset();
5058 bool operator==(const Branch_stub_key& that) const
5060 return (this->dest_ == that.dest_
5061 && (size == 32
5062 || this->toc_base_off_ == that.toc_base_off_));
5065 Address dest_;
5066 unsigned int toc_base_off_;
5069 class Branch_stub_key_hash
5071 public:
5072 size_t operator()(const Branch_stub_key& key) const
5073 { return key.dest_ ^ key.toc_base_off_; }
5076 // In a sane world this would be a global.
5077 Target_powerpc<size, big_endian>* targ_;
5078 // Map sym/object/addend to stub offset.
5079 Plt_stub_entries plt_call_stubs_;
5080 // Map destination address to stub offset.
5081 Branch_stub_entries long_branch_stubs_;
5082 // size of input section
5083 section_size_type orig_data_size_;
5084 // size of stubs
5085 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5086 // Some rare cases cause (PR/20529) fluctuation in stub table
5087 // size, which leads to an endless relax loop. This is to be fixed
5088 // by, after the first few iterations, allowing only increase of
5089 // stub table size. This variable sets the minimal possible size of
5090 // a stub table, it is zero for the first few iterations, then
5091 // increases monotonically.
5092 Address min_size_threshold_;
5093 // Set if this stub group needs a copy of out-of-line register
5094 // save/restore functions.
5095 bool need_save_res_;
5096 // Set when notoc_/r2save_ changes after sizing a stub
5097 bool need_resize_;
5098 // Set when resizing stubs
5099 bool resizing_;
5100 // Per stub table unique identifier.
5101 uint32_t uniq_;
5104 // Add a plt call stub, if we do not already have one for this
5105 // sym/object/addend combo.
5107 template<int size, bool big_endian>
5108 bool
5109 Stub_table<size, big_endian>::add_plt_call_entry(
5110 Address from,
5111 const Sized_relobj_file<size, big_endian>* object,
5112 const Symbol* gsym,
5113 unsigned int r_type,
5114 Address addend,
5115 bool tocsave)
5117 Plt_stub_key key(object, gsym, r_type, addend);
5118 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5119 std::pair<typename Plt_stub_entries::iterator, bool> p
5120 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5121 if (size == 64)
5123 if (p.second
5124 && this->targ_->is_elfv2_localentry0(gsym))
5126 p.first->second.localentry0_ = 1;
5127 this->targ_->set_has_localentry0();
5129 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5131 if (!p.second && !p.first->second.notoc_
5132 && (!this->targ_->power10_stubs()
5133 || this->targ_->power10_stubs_auto()))
5134 this->need_resize_ = true;
5135 p.first->second.notoc_ = 1;
5137 else
5139 if (!p.second && !p.first->second.toc_)
5140 this->need_resize_ = true;
5141 p.first->second.toc_ = 1;
5142 if (!tocsave && !p.first->second.localentry0_)
5144 if (!p.second && !p.first->second.r2save_)
5145 this->need_resize_ = true;
5146 p.first->second.r2save_ = 1;
5150 if (p.second || (this->resizing_ && !p.first->second.iter_))
5152 if (this->resizing_)
5154 p.first->second.iter_ = 1;
5155 p.first->second.off_ = this->plt_size_;
5157 this->plt_size_ += this->plt_call_size(p.first);
5158 if (this->targ_->is_tls_get_addr_opt(gsym))
5159 this->targ_->set_has_tls_get_addr_opt();
5160 this->plt_size_ = this->plt_call_align(this->plt_size_);
5162 return this->can_reach_stub(from, p.first->second.off_, r_type);
5165 template<int size, bool big_endian>
5166 bool
5167 Stub_table<size, big_endian>::add_plt_call_entry(
5168 Address from,
5169 const Sized_relobj_file<size, big_endian>* object,
5170 unsigned int locsym_index,
5171 unsigned int r_type,
5172 Address addend,
5173 bool tocsave)
5175 Plt_stub_key key(object, locsym_index, r_type, addend);
5176 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5177 std::pair<typename Plt_stub_entries::iterator, bool> p
5178 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5179 if (size == 64)
5181 if (p.second
5182 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5184 p.first->second.localentry0_ = 1;
5185 this->targ_->set_has_localentry0();
5187 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5189 if (!p.second && !p.first->second.notoc_
5190 && (!this->targ_->power10_stubs()
5191 || this->targ_->power10_stubs_auto()))
5192 this->need_resize_ = true;
5193 p.first->second.notoc_ = 1;
5195 else
5197 if (!p.second && !p.first->second.toc_)
5198 this->need_resize_ = true;
5199 p.first->second.toc_ = 1;
5200 if (!tocsave && !p.first->second.localentry0_)
5202 if (!p.second && !p.first->second.r2save_)
5203 this->need_resize_ = true;
5204 p.first->second.r2save_ = 1;
5208 if (p.second || (this->resizing_ && !p.first->second.iter_))
5210 if (this->resizing_)
5212 p.first->second.iter_ = 1;
5213 p.first->second.off_ = this->plt_size_;
5215 this->plt_size_ += this->plt_call_size(p.first);
5216 this->plt_size_ = this->plt_call_align(this->plt_size_);
5218 return this->can_reach_stub(from, p.first->second.off_, r_type);
5221 // Find a plt call stub.
5223 template<int size, bool big_endian>
5224 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5225 Stub_table<size, big_endian>::find_plt_call_entry(
5226 const Sized_relobj_file<size, big_endian>* object,
5227 const Symbol* gsym,
5228 unsigned int r_type,
5229 Address addend) const
5231 Plt_stub_key key(object, gsym, r_type, addend);
5232 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5233 if (p == this->plt_call_stubs_.end())
5234 return NULL;
5235 return &p->second;
5238 template<int size, bool big_endian>
5239 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5240 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5242 Plt_stub_key key(gsym);
5243 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5244 if (p == this->plt_call_stubs_.end())
5245 return NULL;
5246 return &p->second;
5249 template<int size, bool big_endian>
5250 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5251 Stub_table<size, big_endian>::find_plt_call_entry(
5252 const Sized_relobj_file<size, big_endian>* object,
5253 unsigned int locsym_index,
5254 unsigned int r_type,
5255 Address addend) const
5257 Plt_stub_key key(object, locsym_index, r_type, addend);
5258 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5259 if (p == this->plt_call_stubs_.end())
5260 return NULL;
5261 return &p->second;
5264 template<int size, bool big_endian>
5265 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5266 Stub_table<size, big_endian>::find_plt_call_entry(
5267 const Sized_relobj_file<size, big_endian>* object,
5268 unsigned int locsym_index) const
5270 Plt_stub_key key(object, locsym_index);
5271 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5272 if (p == this->plt_call_stubs_.end())
5273 return NULL;
5274 return &p->second;
5277 // Add a long branch stub if we don't already have one to given
5278 // destination.
5280 template<int size, bool big_endian>
5281 bool
5282 Stub_table<size, big_endian>::add_long_branch_entry(
5283 const Powerpc_relobj<size, big_endian>* object,
5284 unsigned int r_type,
5285 Address from,
5286 Address to,
5287 unsigned int other,
5288 bool save_res)
5290 Branch_stub_key key(object, to);
5291 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5292 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5293 std::pair<typename Branch_stub_entries::iterator, bool> p
5294 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5295 if (notoc)
5297 if (!p.second && !p.first->second.notoc_)
5298 this->need_resize_ = true;
5299 p.first->second.notoc_ = true;
5301 else
5303 if (!p.second && !p.first->second.toc_)
5304 this->need_resize_ = true;
5305 p.first->second.toc_ = true;
5307 if (p.first->second.other_ == 0)
5308 p.first->second.other_ = other;
5309 gold_assert(save_res == p.first->second.save_res_);
5310 if (p.second || (this->resizing_ && !p.first->second.iter_))
5312 if (this->resizing_)
5314 p.first->second.iter_ = 1;
5315 p.first->second.off_ = this->branch_size_;
5317 if (save_res)
5318 this->need_save_res_ = true;
5319 else
5321 bool need_lt = false;
5322 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5323 this->branch_size_ += stub_size;
5324 if (size == 64 && need_lt)
5325 this->targ_->add_branch_lookup_table(to);
5328 return this->can_reach_stub(from, p.first->second.off_, r_type);
5331 // Find long branch stub offset.
5333 template<int size, bool big_endian>
5334 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5335 Stub_table<size, big_endian>::find_long_branch_entry(
5336 const Powerpc_relobj<size, big_endian>* object,
5337 Address to) const
5339 Branch_stub_key key(object, to);
5340 typename Branch_stub_entries::const_iterator p
5341 = this->long_branch_stubs_.find(key);
5342 if (p == this->long_branch_stubs_.end())
5343 return NULL;
5344 return &p->second;
5347 template<bool big_endian>
5348 static void
5349 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5351 delta /= 4;
5352 if (delta < 64)
5353 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5354 else if (delta < 256)
5356 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5357 fde.push_back(delta);
5359 else if (delta < 65536)
5361 fde.resize(fde.size() + 3);
5362 unsigned char *p = &*fde.end() - 3;
5363 *p++ = elfcpp::DW_CFA_advance_loc2;
5364 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5366 else
5368 fde.resize(fde.size() + 5);
5369 unsigned char *p = &*fde.end() - 5;
5370 *p++ = elfcpp::DW_CFA_advance_loc4;
5371 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5375 template<typename T>
5376 static bool
5377 stub_sort(T s1, T s2)
5379 return s1->second.off_ < s2->second.off_;
5382 // Add .eh_frame info for this stub section. Unlike other linker
5383 // generated .eh_frame this is added late in the link, because we
5384 // only want the .eh_frame info if this particular stub section is
5385 // non-empty.
5387 template<int size, bool big_endian>
5388 void
5389 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5391 if (size != 64
5392 || !parameters->options().ld_generated_unwind_info())
5393 return;
5395 // Since we add stub .eh_frame info late, it must be placed
5396 // after all other linker generated .eh_frame info so that
5397 // merge mapping need not be updated for input sections.
5398 // There is no provision to use a different CIE to that used
5399 // by .glink.
5400 if (!this->targ_->has_glink())
5401 return;
5403 typedef typename Plt_stub_entries::iterator plt_iter;
5404 std::vector<plt_iter> calls;
5405 if (!this->plt_call_stubs_.empty())
5406 for (plt_iter cs = this->plt_call_stubs_.begin();
5407 cs != this->plt_call_stubs_.end();
5408 ++cs)
5409 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5410 && cs->second.r2save_
5411 && !cs->second.localentry0_)
5412 || (cs->second.notoc_
5413 && !this->targ_->power10_stubs()))
5414 calls.push_back(cs);
5415 if (calls.size() > 1)
5416 std::stable_sort(calls.begin(), calls.end(),
5417 stub_sort<plt_iter>);
5419 typedef typename Branch_stub_entries::const_iterator branch_iter;
5420 std::vector<branch_iter> branches;
5421 if (!this->long_branch_stubs_.empty()
5422 && !this->targ_->power10_stubs())
5423 for (branch_iter bs = this->long_branch_stubs_.begin();
5424 bs != this->long_branch_stubs_.end();
5425 ++bs)
5426 if (bs->second.notoc_)
5427 branches.push_back(bs);
5428 if (branches.size() > 1)
5429 std::stable_sort(branches.begin(), branches.end(),
5430 stub_sort<branch_iter>);
5432 if (calls.empty() && branches.empty())
5433 return;
5435 unsigned int last_eh_loc = 0;
5436 // offset pcrel sdata4, size udata4, and augmentation size byte.
5437 std::vector<unsigned char> fde(9, 0);
5439 for (unsigned int i = 0; i < calls.size(); i++)
5441 plt_iter cs = calls[i];
5442 unsigned int off = cs->second.off_;
5443 // The __tls_get_addr_opt call stub needs to describe where
5444 // it saves LR, to support exceptions that might be thrown
5445 // from __tls_get_addr, and to support asynchronous exceptions.
5446 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5448 off += 7 * 4;
5449 if (cs->second.r2save_
5450 && !cs->second.localentry0_)
5452 off += 2 * 4;
5453 eh_advance<big_endian>(fde, off - last_eh_loc);
5454 fde.resize(fde.size() + 6);
5455 unsigned char* p = &*fde.end() - 6;
5456 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5457 *p++ = 65;
5458 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5459 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5460 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5461 *p++ = elfcpp::DW_CFA_restore_extended;
5462 *p++ = 65;
5463 last_eh_loc = off + delta;
5464 continue;
5467 // notoc stubs also should describe LR changes, to support
5468 // asynchronous exceptions.
5469 off += (cs->second.r2save_ ? 4 : 0) + 8;
5470 eh_advance<big_endian>(fde, off - last_eh_loc);
5471 fde.resize(fde.size() + 6);
5472 unsigned char* p = &*fde.end() - 6;
5473 *p++ = elfcpp::DW_CFA_register;
5474 *p++ = 65;
5475 *p++ = 12;
5476 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5477 *p++ = elfcpp::DW_CFA_restore_extended;
5478 *p++ = 65;
5479 last_eh_loc = off + 8;
5482 for (unsigned int i = 0; i < branches.size(); i++)
5484 branch_iter bs = branches[i];
5485 unsigned int off = bs->second.off_ + 8;
5486 eh_advance<big_endian>(fde, off - last_eh_loc);
5487 fde.resize(fde.size() + 6);
5488 unsigned char* p = &*fde.end() - 6;
5489 *p++ = elfcpp::DW_CFA_register;
5490 *p++ = 65;
5491 *p++ = 12;
5492 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5493 *p++ = elfcpp::DW_CFA_restore_extended;
5494 *p++ = 65;
5495 last_eh_loc = off + 8;
5498 layout->add_eh_frame_for_plt(this,
5499 Eh_cie<size>::eh_frame_cie,
5500 sizeof (Eh_cie<size>::eh_frame_cie),
5501 &*fde.begin(), fde.size());
5504 template<int size, bool big_endian>
5505 void
5506 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5508 if (size == 64
5509 && parameters->options().ld_generated_unwind_info()
5510 && this->targ_->has_glink())
5511 layout->remove_eh_frame_for_plt(this,
5512 Eh_cie<size>::eh_frame_cie,
5513 sizeof (Eh_cie<size>::eh_frame_cie));
5516 // A class to handle .glink.
5518 template<int size, bool big_endian>
5519 class Output_data_glink : public Output_section_data
5521 public:
5522 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5523 static const Address invalid_address = static_cast<Address>(0) - 1;
5525 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5526 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5527 end_branch_table_(), ge_size_(0)
5530 void
5531 add_eh_frame(Layout* layout);
5533 void
5534 add_global_entry(const Symbol*);
5536 Address
5537 find_global_entry(const Symbol*) const;
5539 unsigned int
5540 global_entry_align(unsigned int off) const
5542 unsigned int align = param_plt_align<size>();
5543 return (off + align - 1) & -align;
5546 unsigned int
5547 global_entry_off() const
5549 return this->global_entry_align(this->end_branch_table_);
5552 Address
5553 global_entry_address() const
5555 gold_assert(this->is_data_size_valid());
5556 return this->address() + this->global_entry_off();
5560 pltresolve_size() const
5562 if (size == 64)
5563 return (8
5564 + (this->targ_->abiversion() < 2 ? 11 * 4
5565 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5566 return 16 * 4;
5569 protected:
5570 // Write to a map file.
5571 void
5572 do_print_to_mapfile(Mapfile* mapfile) const
5573 { mapfile->print_output_data(this, _("** glink")); }
5575 private:
5576 void
5577 set_final_data_size();
5579 // Write out .glink
5580 void
5581 do_write(Output_file*);
5583 // Allows access to .got and .plt for do_write.
5584 Target_powerpc<size, big_endian>* targ_;
5586 // Map sym to stub offset.
5587 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5588 Global_entry_stub_entries global_entry_stubs_;
5590 unsigned int end_branch_table_, ge_size_;
5593 template<int size, bool big_endian>
5594 void
5595 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5597 if (!parameters->options().ld_generated_unwind_info())
5598 return;
5600 if (size == 64)
5602 if (this->targ_->abiversion() < 2)
5603 layout->add_eh_frame_for_plt(this,
5604 Eh_cie<64>::eh_frame_cie,
5605 sizeof (Eh_cie<64>::eh_frame_cie),
5606 glink_eh_frame_fde_64v1,
5607 sizeof (glink_eh_frame_fde_64v1));
5608 else if (this->targ_->has_localentry0())
5609 layout->add_eh_frame_for_plt(this,
5610 Eh_cie<64>::eh_frame_cie,
5611 sizeof (Eh_cie<64>::eh_frame_cie),
5612 glink_eh_frame_fde_64v2_localentry0,
5613 sizeof (glink_eh_frame_fde_64v2));
5614 else
5615 layout->add_eh_frame_for_plt(this,
5616 Eh_cie<64>::eh_frame_cie,
5617 sizeof (Eh_cie<64>::eh_frame_cie),
5618 glink_eh_frame_fde_64v2,
5619 sizeof (glink_eh_frame_fde_64v2));
5621 else
5623 // 32-bit .glink can use the default since the CIE return
5624 // address reg, LR, is valid.
5625 layout->add_eh_frame_for_plt(this,
5626 Eh_cie<32>::eh_frame_cie,
5627 sizeof (Eh_cie<32>::eh_frame_cie),
5628 default_fde,
5629 sizeof (default_fde));
5630 // Except where LR is used in a PIC __glink_PLTresolve.
5631 if (parameters->options().output_is_position_independent())
5632 layout->add_eh_frame_for_plt(this,
5633 Eh_cie<32>::eh_frame_cie,
5634 sizeof (Eh_cie<32>::eh_frame_cie),
5635 glink_eh_frame_fde_32,
5636 sizeof (glink_eh_frame_fde_32));
5640 template<int size, bool big_endian>
5641 void
5642 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5644 unsigned int off = this->global_entry_align(this->ge_size_);
5645 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5646 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5647 if (p.second)
5648 this->ge_size_ = off + 16;
5651 template<int size, bool big_endian>
5652 typename Output_data_glink<size, big_endian>::Address
5653 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5655 typename Global_entry_stub_entries::const_iterator p
5656 = this->global_entry_stubs_.find(gsym);
5657 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5660 template<int size, bool big_endian>
5661 void
5662 Output_data_glink<size, big_endian>::set_final_data_size()
5664 unsigned int count = this->targ_->plt_entry_count();
5665 section_size_type total = 0;
5667 if (count != 0)
5669 if (size == 32)
5671 // space for branch table
5672 total += 4 * (count - 1);
5674 total += -total & 15;
5675 total += this->pltresolve_size();
5677 else
5679 total += this->pltresolve_size();
5681 // space for branch table
5682 total += 4 * count;
5683 if (this->targ_->abiversion() < 2)
5685 total += 4 * count;
5686 if (count > 0x8000)
5687 total += 4 * (count - 0x8000);
5691 this->end_branch_table_ = total;
5692 total = this->global_entry_align(total);
5693 total += this->ge_size_;
5695 this->set_data_size(total);
5698 // Define symbols on stubs, identifying the stub.
5700 template<int size, bool big_endian>
5701 void
5702 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5704 if (!this->plt_call_stubs_.empty())
5706 // The key for the plt call stub hash table includes addresses,
5707 // therefore traversal order depends on those addresses, which
5708 // can change between runs if gold is a PIE. Unfortunately the
5709 // output .symtab ordering depends on the order in which symbols
5710 // are added to the linker symtab. We want reproducible output
5711 // so must sort the call stub symbols.
5712 typedef typename Plt_stub_entries::iterator plt_iter;
5713 std::vector<plt_iter> sorted;
5714 sorted.resize(this->plt_call_stubs_.size());
5716 for (plt_iter cs = this->plt_call_stubs_.begin();
5717 cs != this->plt_call_stubs_.end();
5718 ++cs)
5719 sorted[cs->second.indx_] = cs;
5721 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5723 plt_iter cs = sorted[i];
5724 char add[10];
5725 add[0] = 0;
5726 if (cs->first.addend_ != 0)
5727 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5728 char obj[10];
5729 obj[0] = 0;
5730 if (cs->first.object_)
5732 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5733 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5734 sprintf(obj, "%x:", ppcobj->uniq());
5736 char localname[9];
5737 const char *symname;
5738 if (cs->first.sym_ == NULL)
5740 sprintf(localname, "%x", cs->first.locsym_);
5741 symname = localname;
5743 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5744 symname = this->targ_->tls_get_addr_opt()->name();
5745 else
5746 symname = cs->first.sym_->name();
5747 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5748 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5749 Address value
5750 = this->stub_address() - this->address() + cs->second.off_;
5751 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5752 this->targ_->define_local(symtab, name, this, value, stub_size);
5756 typedef typename Branch_stub_entries::iterator branch_iter;
5757 for (branch_iter bs = this->long_branch_stubs_.begin();
5758 bs != this->long_branch_stubs_.end();
5759 ++bs)
5761 if (bs->second.save_res_)
5762 continue;
5764 char* name = new char[8 + 13 + 16 + 1];
5765 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5766 static_cast<unsigned long long>(bs->first.dest_));
5767 Address value = (this->stub_address() - this->address()
5768 + this->plt_size_ + bs->second.off_);
5769 bool need_lt = false;
5770 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5771 this->targ_->define_local(symtab, name, this, value, stub_size);
5775 // Emit the start of a __tls_get_addr_opt plt call stub.
5777 template<int size, bool big_endian>
5778 void
5779 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5780 bool save_lr)
5782 unsigned char* p = *pp;
5783 if (size == 64)
5785 write_insn<big_endian>(p, ld_11_3 + 0);
5786 p += 4;
5787 write_insn<big_endian>(p, ld_12_3 + 8);
5788 p += 4;
5789 write_insn<big_endian>(p, mr_0_3);
5790 p += 4;
5791 write_insn<big_endian>(p, cmpdi_11_0);
5792 p += 4;
5793 write_insn<big_endian>(p, add_3_12_13);
5794 p += 4;
5795 write_insn<big_endian>(p, beqlr);
5796 p += 4;
5797 write_insn<big_endian>(p, mr_3_0);
5798 p += 4;
5799 if (save_lr)
5801 write_insn<big_endian>(p, mflr_11);
5802 p += 4;
5803 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5804 p += 4;
5807 else
5809 write_insn<big_endian>(p, lwz_11_3 + 0);
5810 p += 4;
5811 write_insn<big_endian>(p, lwz_12_3 + 4);
5812 p += 4;
5813 write_insn<big_endian>(p, mr_0_3);
5814 p += 4;
5815 write_insn<big_endian>(p, cmpwi_11_0);
5816 p += 4;
5817 write_insn<big_endian>(p, add_3_12_2);
5818 p += 4;
5819 write_insn<big_endian>(p, beqlr);
5820 p += 4;
5821 write_insn<big_endian>(p, mr_3_0);
5822 p += 4;
5823 write_insn<big_endian>(p, nop);
5824 p += 4;
5826 *pp = p;
5829 // Emit the tail of a __tls_get_addr_opt plt call stub.
5831 template<int size, bool big_endian>
5832 void
5833 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5835 write_insn<big_endian>(p, bctrl);
5836 p += 4;
5837 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5838 p += 4;
5839 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5840 p += 4;
5841 write_insn<big_endian>(p, mtlr_11);
5842 p += 4;
5843 write_insn<big_endian>(p, blr);
5846 // Emit pc-relative plt call stub code.
5848 template<bool big_endian>
5849 static unsigned char*
5850 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5852 uint64_t insn;
5853 if (off - odd + (1ULL << 33) < 1ULL << 34)
5855 off -= odd;
5856 if (odd)
5858 write_insn<big_endian>(p, nop);
5859 p += 4;
5861 if (load)
5862 insn = pld_12_pc;
5863 else
5864 insn = paddi_12_pc;
5865 insn |= d34(off);
5866 write_insn<big_endian>(p, insn >> 32);
5867 p += 4;
5868 write_insn<big_endian>(p, insn & 0xffffffff);
5870 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5872 off -= 8 - odd;
5873 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5874 p += 4;
5875 if (!odd)
5877 write_insn<big_endian>(p, sldi_11_11_34);
5878 p += 4;
5880 insn = paddi_12_pc | d34(off);
5881 write_insn<big_endian>(p, insn >> 32);
5882 p += 4;
5883 write_insn<big_endian>(p, insn & 0xffffffff);
5884 p += 4;
5885 if (odd)
5887 write_insn<big_endian>(p, sldi_11_11_34);
5888 p += 4;
5890 if (load)
5891 write_insn<big_endian>(p, ldx_12_11_12);
5892 else
5893 write_insn<big_endian>(p, add_12_11_12);
5895 else
5897 off -= odd + 8;
5898 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5899 p += 4;
5900 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5901 p += 4;
5902 if (odd)
5904 write_insn<big_endian>(p, sldi_11_11_34);
5905 p += 4;
5907 insn = paddi_12_pc | d34(off);
5908 write_insn<big_endian>(p, insn >> 32);
5909 p += 4;
5910 write_insn<big_endian>(p, insn & 0xffffffff);
5911 p += 4;
5912 if (!odd)
5914 write_insn<big_endian>(p, sldi_11_11_34);
5915 p += 4;
5917 if (load)
5918 write_insn<big_endian>(p, ldx_12_11_12);
5919 else
5920 write_insn<big_endian>(p, add_12_11_12);
5922 p += 4;
5923 return p;
5926 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5927 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5928 // mflr %r12
5929 // bcl 20,31,1f
5930 // 1: mflr %r11
5931 // mtlr %r12
5932 // lis %r12,xxx-1b@highest
5933 // ori %r12,%r12,xxx-1b@higher
5934 // sldi %r12,%r12,32
5935 // oris %r12,%r12,xxx-1b@high
5936 // ori %r12,%r12,xxx-1b@l
5937 // add/ldx %r12,%r11,%r12
5939 template<bool big_endian>
5940 static unsigned char*
5941 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5943 write_insn<big_endian>(p, mflr_12);
5944 p += 4;
5945 write_insn<big_endian>(p, bcl_20_31);
5946 p += 4;
5947 write_insn<big_endian>(p, mflr_11);
5948 p += 4;
5949 write_insn<big_endian>(p, mtlr_12);
5950 p += 4;
5951 if (off + 0x8000 < 0x10000)
5953 if (load)
5954 write_insn<big_endian>(p, ld_12_11 + l(off));
5955 else
5956 write_insn<big_endian>(p, addi_12_11 + l(off));
5958 else if (off + 0x80008000ULL < 0x100000000ULL)
5960 write_insn<big_endian>(p, addis_12_11 + ha(off));
5961 p += 4;
5962 if (load)
5963 write_insn<big_endian>(p, ld_12_12 + l(off));
5964 else
5965 write_insn<big_endian>(p, addi_12_12 + l(off));
5967 else
5969 if (off + 0x800000000000ULL < 0x1000000000000ULL)
5971 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5972 p += 4;
5974 else
5976 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5977 p += 4;
5978 if (((off >> 32) & 0xffff) != 0)
5980 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5981 p += 4;
5984 if (((off >> 32) & 0xffffffffULL) != 0)
5986 write_insn<big_endian>(p, sldi_12_12_32);
5987 p += 4;
5989 if (hi(off) != 0)
5991 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5992 p += 4;
5994 if (l(off) != 0)
5996 write_insn<big_endian>(p, ori_12_12_0 + l(off));
5997 p += 4;
5999 if (load)
6000 write_insn<big_endian>(p, ldx_12_11_12);
6001 else
6002 write_insn<big_endian>(p, add_12_11_12);
6004 p += 4;
6005 return p;
6008 // Size of a given plt call stub.
6010 template<int size, bool big_endian>
6011 unsigned int
6012 Stub_table<size, big_endian>::plt_call_size(
6013 typename Plt_stub_entries::iterator p) const
6015 if (size == 32)
6017 const Symbol* gsym = p->first.sym_;
6018 return (4 * 4
6019 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
6022 const Output_data_plt_powerpc<size, big_endian>* plt;
6023 uint64_t plt_addr = this->plt_off(p, &plt);
6024 plt_addr += plt->address();
6025 if (this->targ_->power10_stubs()
6026 && this->targ_->power10_stubs_auto())
6028 unsigned int bytes = 0;
6029 if (p->second.notoc_)
6031 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6032 bytes = 7 * 4;
6033 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6034 uint64_t odd = from & 4;
6035 uint64_t off = plt_addr - from;
6036 if (off - odd + (1ULL << 33) < 1ULL << 34)
6037 bytes += odd + 4 * 4;
6038 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6039 bytes += 7 * 4;
6040 else
6041 bytes += 8 * 4;
6042 bytes = this->plt_call_align(bytes);
6044 unsigned int tail = 0;
6045 if (p->second.toc_)
6047 p->second.tocoff_ = bytes;
6048 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6050 bytes += 7 * 4;
6051 if (p->second.r2save_ && !p->second.localentry0_)
6053 bytes += 2 * 4;
6054 tail = 4 * 4;
6057 if (p->second.r2save_)
6058 bytes += 4;
6059 uint64_t got_addr
6060 = this->targ_->got_section()->output_section()->address();
6061 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6062 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6063 got_addr += ppcobj->toc_base_offset();
6064 uint64_t off = plt_addr - got_addr;
6065 bytes += 3 * 4 + 4 * (ha(off) != 0);
6067 return bytes + tail;
6069 else
6071 unsigned int bytes = 0;
6072 unsigned int tail = 0;
6073 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6075 bytes = 7 * 4;
6076 if (p->second.r2save_ && !p->second.localentry0_)
6078 bytes = 9 * 4;
6079 tail = 4 * 4;
6083 if (p->second.r2save_)
6084 bytes += 4;
6086 if (this->targ_->power10_stubs())
6088 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6089 uint64_t odd = from & 4;
6090 uint64_t off = plt_addr - from;
6091 if (off - odd + (1ULL << 33) < 1ULL << 34)
6092 bytes += odd + 4 * 4;
6093 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6094 bytes += 7 * 4;
6095 else
6096 bytes += 8 * 4;
6097 return bytes + tail;
6100 if (p->second.notoc_)
6102 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6103 uint64_t off = plt_addr - from;
6104 if (off + 0x8000 < 0x10000)
6105 bytes += 7 * 4;
6106 else if (off + 0x80008000ULL < 0x100000000ULL)
6107 bytes += 8 * 4;
6108 else
6110 bytes += 8 * 4;
6111 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6112 && ((off >> 32) & 0xffff) != 0)
6113 bytes += 4;
6114 if (((off >> 32) & 0xffffffffULL) != 0)
6115 bytes += 4;
6116 if (hi(off) != 0)
6117 bytes += 4;
6118 if (l(off) != 0)
6119 bytes += 4;
6121 return bytes + tail;
6124 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6125 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6126 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6127 got_addr += ppcobj->toc_base_offset();
6128 uint64_t off = plt_addr - got_addr;
6129 bytes += 3 * 4 + 4 * (ha(off) != 0);
6130 if (this->targ_->abiversion() < 2)
6132 bool static_chain = parameters->options().plt_static_chain();
6133 bool thread_safe = this->targ_->plt_thread_safe();
6134 bytes += (4
6135 + 4 * static_chain
6136 + 8 * thread_safe
6137 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6139 return bytes + tail;
6143 // Return long branch stub size.
6145 template<int size, bool big_endian>
6146 unsigned int
6147 Stub_table<size, big_endian>::branch_stub_size(
6148 typename Branch_stub_entries::iterator p,
6149 bool* need_lt)
6151 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6152 if (size == 32)
6154 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6155 return 4;
6156 if (parameters->options().output_is_position_independent())
6157 return 32;
6158 return 16;
6161 uint64_t off = p->first.dest_ - loc;
6162 unsigned int bytes = 0;
6163 if (p->second.notoc_)
6165 if (this->targ_->power10_stubs())
6167 Address odd = loc & 4;
6168 if (off + (1 << 25) < 2 << 25)
6169 bytes = odd + 12;
6170 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6171 bytes = odd + 16;
6172 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6173 bytes = 28;
6174 else
6175 bytes = 32;
6176 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6177 return bytes;
6178 p->second.tocoff_ = bytes;
6180 else
6182 off -= 8;
6183 if (off + 0x8000 < 0x10000)
6184 return 24;
6185 if (off + 0x80008000ULL < 0x100000000ULL)
6187 if (off + 24 + (1 << 25) < 2 << 25)
6188 return 28;
6189 return 32;
6192 bytes = 32;
6193 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6194 && ((off >> 32) & 0xffff) != 0)
6195 bytes += 4;
6196 if (((off >> 32) & 0xffffffffULL) != 0)
6197 bytes += 4;
6198 if (hi(off) != 0)
6199 bytes += 4;
6200 if (l(off) != 0)
6201 bytes += 4;
6202 return bytes;
6206 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6207 if (off + (1 << 25) < 2 << 25)
6208 return bytes + 4;
6209 if (!this->targ_->power10_stubs()
6210 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6211 *need_lt = true;
6212 return bytes + 16;
6215 template<int size, bool big_endian>
6216 void
6217 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6219 if (p.sym_)
6220 gold_error(_("linkage table error against `%s'"),
6221 p.sym_->demangled_name().c_str());
6222 else
6223 gold_error(_("linkage table error against `%s:[local %u]'"),
6224 p.object_->name().c_str(),
6225 p.locsym_);
6228 // Write out plt and long branch stub code.
6230 template<int size, bool big_endian>
6231 void
6232 Stub_table<size, big_endian>::do_write(Output_file* of)
6234 if (this->plt_call_stubs_.empty()
6235 && this->long_branch_stubs_.empty())
6236 return;
6238 const section_size_type start_off = this->offset();
6239 const section_size_type off = this->stub_offset();
6240 const section_size_type oview_size =
6241 convert_to_section_size_type(this->data_size() - (off - start_off));
6242 unsigned char* const oview = of->get_output_view(off, oview_size);
6243 unsigned char* p;
6245 if (size == 64
6246 && this->targ_->power10_stubs())
6248 const Output_data_got_powerpc<size, big_endian>* got
6249 = this->targ_->got_section();
6250 Address got_os_addr = got->output_section()->address();
6252 if (!this->plt_call_stubs_.empty())
6254 // Write out plt call stubs.
6255 typename Plt_stub_entries::const_iterator cs;
6256 for (cs = this->plt_call_stubs_.begin();
6257 cs != this->plt_call_stubs_.end();
6258 ++cs)
6260 p = oview + cs->second.off_;
6261 const Output_data_plt_powerpc<size, big_endian>* plt;
6262 Address pltoff = this->plt_off(cs, &plt);
6263 Address plt_addr = pltoff + plt->address();
6264 if (this->targ_->power10_stubs_auto())
6266 if (cs->second.notoc_)
6268 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6269 this->build_tls_opt_head(&p, false);
6270 Address from = this->stub_address() + (p - oview);
6271 Address delta = plt_addr - from;
6272 p = build_power10_offset<big_endian>(p, delta, from & 4,
6273 true);
6274 write_insn<big_endian>(p, mtctr_12);
6275 p += 4;
6276 write_insn<big_endian>(p, bctr);
6277 p += 4;
6278 p = oview + this->plt_call_align(p - oview);
6280 if (cs->second.toc_)
6282 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6284 bool save_lr
6285 = cs->second.r2save_ && !cs->second.localentry0_;
6286 this->build_tls_opt_head(&p, save_lr);
6288 const Powerpc_relobj<size, big_endian>* ppcobj
6289 = static_cast<const Powerpc_relobj<size, big_endian>*>(
6290 cs->first.object_);
6291 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6292 Address off = plt_addr - got_addr;
6294 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6295 this->plt_error(cs->first);
6297 if (cs->second.r2save_)
6299 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6300 p += 4;
6302 if (ha(off) != 0)
6304 write_insn<big_endian>(p, addis_12_2 + ha(off));
6305 p += 4;
6306 write_insn<big_endian>(p, ld_12_12 + l(off));
6307 p += 4;
6309 else
6311 write_insn<big_endian>(p, ld_12_2 + l(off));
6312 p += 4;
6314 write_insn<big_endian>(p, mtctr_12);
6315 p += 4;
6316 if (cs->second.r2save_
6317 && !cs->second.localentry0_
6318 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6319 this->build_tls_opt_tail(p);
6320 else
6321 write_insn<big_endian>(p, bctr);
6324 else
6326 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6328 bool save_lr
6329 = cs->second.r2save_ && !cs->second.localentry0_;
6330 this->build_tls_opt_head(&p, save_lr);
6332 if (cs->second.r2save_)
6334 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6335 p += 4;
6337 Address from = this->stub_address() + (p - oview);
6338 Address delta = plt_addr - from;
6339 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6340 write_insn<big_endian>(p, mtctr_12);
6341 p += 4;
6342 if (cs->second.r2save_
6343 && !cs->second.localentry0_
6344 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6345 this->build_tls_opt_tail(p);
6346 else
6347 write_insn<big_endian>(p, bctr);
6352 // Write out long branch stubs.
6353 typename Branch_stub_entries::const_iterator bs;
6354 for (bs = this->long_branch_stubs_.begin();
6355 bs != this->long_branch_stubs_.end();
6356 ++bs)
6358 if (bs->second.save_res_)
6359 continue;
6360 Address off = this->plt_size_ + bs->second.off_;
6361 p = oview + off;
6362 Address loc = this->stub_address() + off;
6363 Address delta = bs->first.dest_ - loc;
6364 if (this->targ_->power10_stubs_auto())
6366 if (bs->second.notoc_)
6368 unsigned char* startp = p;
6369 p = build_power10_offset<big_endian>(p, delta,
6370 loc & 4, false);
6371 delta -= p - startp;
6372 startp = p;
6373 if (delta + (1 << 25) < 2 << 25)
6374 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6375 else
6377 write_insn<big_endian>(p, mtctr_12);
6378 p += 4;
6379 write_insn<big_endian>(p, bctr);
6381 p += 4;
6382 delta -= p - startp;
6384 if (bs->second.toc_)
6386 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6387 if (delta + (1 << 25) >= 2 << 25)
6389 Address brlt_addr
6390 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6391 gold_assert(brlt_addr != invalid_address);
6392 brlt_addr += this->targ_->brlt_section()->address();
6393 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6394 Address brltoff = brlt_addr - got_addr;
6395 if (ha(brltoff) == 0)
6397 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6398 p += 4;
6400 else
6402 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6403 p += 4;
6404 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6405 p += 4;
6408 if (delta + (1 << 25) < 2 << 25)
6409 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6410 else
6412 write_insn<big_endian>(p, mtctr_12);
6413 p += 4;
6414 write_insn<big_endian>(p, bctr);
6418 else
6420 if (!bs->second.notoc_)
6421 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6422 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6424 unsigned char* startp = p;
6425 p = build_power10_offset<big_endian>(p, delta,
6426 loc & 4, false);
6427 delta -= p - startp;
6429 if (delta + (1 << 25) < 2 << 25)
6430 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6431 else
6433 write_insn<big_endian>(p, mtctr_12);
6434 p += 4;
6435 write_insn<big_endian>(p, bctr);
6440 else if (size == 64)
6442 const Output_data_got_powerpc<size, big_endian>* got
6443 = this->targ_->got_section();
6444 Address got_os_addr = got->output_section()->address();
6446 if (!this->plt_call_stubs_.empty()
6447 && this->targ_->abiversion() >= 2)
6449 // Write out plt call stubs for ELFv2.
6450 typename Plt_stub_entries::const_iterator cs;
6451 for (cs = this->plt_call_stubs_.begin();
6452 cs != this->plt_call_stubs_.end();
6453 ++cs)
6455 const Output_data_plt_powerpc<size, big_endian>* plt;
6456 Address pltoff = this->plt_off(cs, &plt);
6457 Address plt_addr = pltoff + plt->address();
6459 p = oview + cs->second.off_;
6460 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6462 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6463 this->build_tls_opt_head(&p, save_lr);
6465 if (cs->second.r2save_)
6467 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6468 p += 4;
6470 if (cs->second.notoc_)
6472 Address from = this->stub_address() + (p - oview) + 8;
6473 Address off = plt_addr - from;
6474 p = build_notoc_offset<big_endian>(p, off, true);
6476 else
6478 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6479 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6480 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6481 Address off = plt_addr - got_addr;
6483 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6484 this->plt_error(cs->first);
6486 if (ha(off) != 0)
6488 write_insn<big_endian>(p, addis_12_2 + ha(off));
6489 p += 4;
6490 write_insn<big_endian>(p, ld_12_12 + l(off));
6491 p += 4;
6493 else
6495 write_insn<big_endian>(p, ld_12_2 + l(off));
6496 p += 4;
6499 write_insn<big_endian>(p, mtctr_12);
6500 p += 4;
6501 if (cs->second.r2save_
6502 && !cs->second.localentry0_
6503 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6504 this->build_tls_opt_tail(p);
6505 else
6506 write_insn<big_endian>(p, bctr);
6509 else if (!this->plt_call_stubs_.empty())
6511 // Write out plt call stubs for ELFv1.
6512 typename Plt_stub_entries::const_iterator cs;
6513 for (cs = this->plt_call_stubs_.begin();
6514 cs != this->plt_call_stubs_.end();
6515 ++cs)
6517 const Output_data_plt_powerpc<size, big_endian>* plt;
6518 Address pltoff = this->plt_off(cs, &plt);
6519 Address plt_addr = pltoff + plt->address();
6520 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6521 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6522 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6523 Address off = plt_addr - got_addr;
6525 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6526 || cs->second.notoc_)
6527 this->plt_error(cs->first);
6529 bool static_chain = parameters->options().plt_static_chain();
6530 bool thread_safe = this->targ_->plt_thread_safe();
6531 bool use_fake_dep = false;
6532 Address cmp_branch_off = 0;
6533 if (thread_safe)
6535 unsigned int pltindex
6536 = ((pltoff - this->targ_->first_plt_entry_offset())
6537 / this->targ_->plt_entry_size());
6538 Address glinkoff
6539 = (this->targ_->glink_section()->pltresolve_size()
6540 + pltindex * 8);
6541 if (pltindex > 32768)
6542 glinkoff += (pltindex - 32768) * 4;
6543 Address to
6544 = this->targ_->glink_section()->address() + glinkoff;
6545 Address from
6546 = (this->stub_address() + cs->second.off_ + 20
6547 + 4 * cs->second.r2save_
6548 + 4 * (ha(off) != 0)
6549 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6550 + 4 * static_chain);
6551 cmp_branch_off = to - from;
6552 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6555 p = oview + cs->second.off_;
6556 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6558 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6559 this->build_tls_opt_head(&p, save_lr);
6560 use_fake_dep = thread_safe;
6562 if (cs->second.r2save_)
6564 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6565 p += 4;
6567 if (ha(off) != 0)
6569 write_insn<big_endian>(p, addis_11_2 + ha(off));
6570 p += 4;
6571 write_insn<big_endian>(p, ld_12_11 + l(off));
6572 p += 4;
6573 if (ha(off + 8 + 8 * static_chain) != ha(off))
6575 write_insn<big_endian>(p, addi_11_11 + l(off));
6576 p += 4;
6577 off = 0;
6579 write_insn<big_endian>(p, mtctr_12);
6580 p += 4;
6581 if (use_fake_dep)
6583 write_insn<big_endian>(p, xor_2_12_12);
6584 p += 4;
6585 write_insn<big_endian>(p, add_11_11_2);
6586 p += 4;
6588 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6589 p += 4;
6590 if (static_chain)
6592 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6593 p += 4;
6596 else
6598 write_insn<big_endian>(p, ld_12_2 + l(off));
6599 p += 4;
6600 if (ha(off + 8 + 8 * static_chain) != ha(off))
6602 write_insn<big_endian>(p, addi_2_2 + l(off));
6603 p += 4;
6604 off = 0;
6606 write_insn<big_endian>(p, mtctr_12);
6607 p += 4;
6608 if (use_fake_dep)
6610 write_insn<big_endian>(p, xor_11_12_12);
6611 p += 4;
6612 write_insn<big_endian>(p, add_2_2_11);
6613 p += 4;
6615 if (static_chain)
6617 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6618 p += 4;
6620 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6621 p += 4;
6623 if (cs->second.r2save_
6624 && !cs->second.localentry0_
6625 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6626 this->build_tls_opt_tail(p);
6627 else if (thread_safe && !use_fake_dep)
6629 write_insn<big_endian>(p, cmpldi_2_0);
6630 p += 4;
6631 write_insn<big_endian>(p, bnectr_p4);
6632 p += 4;
6633 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6635 else
6636 write_insn<big_endian>(p, bctr);
6640 // Write out long branch stubs.
6641 typename Branch_stub_entries::const_iterator bs;
6642 for (bs = this->long_branch_stubs_.begin();
6643 bs != this->long_branch_stubs_.end();
6644 ++bs)
6646 if (bs->second.save_res_)
6647 continue;
6648 Address off = this->plt_size_ + bs->second.off_;
6649 p = oview + off;
6650 Address loc = this->stub_address() + off;
6651 Address delta = bs->first.dest_ - loc;
6652 if (!bs->second.notoc_)
6653 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6654 if (bs->second.notoc_)
6656 unsigned char* startp = p;
6657 p = build_notoc_offset<big_endian>(p, off, false);
6658 delta -= p - startp;
6660 else if (delta + (1 << 25) >= 2 << 25)
6662 Address brlt_addr
6663 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6664 gold_assert(brlt_addr != invalid_address);
6665 brlt_addr += this->targ_->brlt_section()->address();
6666 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6667 Address brltoff = brlt_addr - got_addr;
6668 if (ha(brltoff) == 0)
6670 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6671 p += 4;
6673 else
6675 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6676 p += 4;
6677 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6678 p += 4;
6681 if (delta + (1 << 25) < 2 << 25)
6682 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6683 else
6685 write_insn<big_endian>(p, mtctr_12);
6686 p += 4;
6687 write_insn<big_endian>(p, bctr);
6691 else // size == 32
6693 if (!this->plt_call_stubs_.empty())
6695 // The address of _GLOBAL_OFFSET_TABLE_.
6696 Address g_o_t = invalid_address;
6698 // Write out plt call stubs.
6699 typename Plt_stub_entries::const_iterator cs;
6700 for (cs = this->plt_call_stubs_.begin();
6701 cs != this->plt_call_stubs_.end();
6702 ++cs)
6704 const Output_data_plt_powerpc<size, big_endian>* plt;
6705 Address plt_addr = this->plt_off(cs, &plt);
6706 plt_addr += plt->address();
6708 p = oview + cs->second.off_;
6709 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6710 this->build_tls_opt_head(&p, false);
6711 if (parameters->options().output_is_position_independent())
6713 Address got_addr;
6714 const Powerpc_relobj<size, big_endian>* ppcobj
6715 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6716 (cs->first.object_));
6717 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6719 unsigned int got2 = ppcobj->got2_shndx();
6720 got_addr = ppcobj->get_output_section_offset(got2);
6721 gold_assert(got_addr != invalid_address);
6722 got_addr += (ppcobj->output_section(got2)->address()
6723 + cs->first.addend_);
6725 else
6727 if (g_o_t == invalid_address)
6729 const Output_data_got_powerpc<size, big_endian>* got
6730 = this->targ_->got_section();
6731 g_o_t = got->address() + got->g_o_t();
6733 got_addr = g_o_t;
6736 Address off = plt_addr - got_addr;
6737 if (ha(off) == 0)
6738 write_insn<big_endian>(p, lwz_11_30 + l(off));
6739 else
6741 write_insn<big_endian>(p, addis_11_30 + ha(off));
6742 p += 4;
6743 write_insn<big_endian>(p, lwz_11_11 + l(off));
6746 else
6748 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6749 p += 4;
6750 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6752 p += 4;
6753 write_insn<big_endian>(p, mtctr_11);
6754 p += 4;
6755 write_insn<big_endian>(p, bctr);
6759 // Write out long branch stubs.
6760 typename Branch_stub_entries::const_iterator bs;
6761 for (bs = this->long_branch_stubs_.begin();
6762 bs != this->long_branch_stubs_.end();
6763 ++bs)
6765 if (bs->second.save_res_)
6766 continue;
6767 Address off = this->plt_size_ + bs->second.off_;
6768 p = oview + off;
6769 Address loc = this->stub_address() + off;
6770 Address delta = bs->first.dest_ - loc;
6771 if (delta + (1 << 25) < 2 << 25)
6772 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6773 else if (!parameters->options().output_is_position_independent())
6775 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6776 p += 4;
6777 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6779 else
6781 delta -= 8;
6782 write_insn<big_endian>(p, mflr_0);
6783 p += 4;
6784 write_insn<big_endian>(p, bcl_20_31);
6785 p += 4;
6786 write_insn<big_endian>(p, mflr_12);
6787 p += 4;
6788 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6789 p += 4;
6790 write_insn<big_endian>(p, addi_12_12 + l(delta));
6791 p += 4;
6792 write_insn<big_endian>(p, mtlr_0);
6794 p += 4;
6795 write_insn<big_endian>(p, mtctr_12);
6796 p += 4;
6797 write_insn<big_endian>(p, bctr);
6800 if (this->need_save_res_)
6802 p = oview + this->plt_size_ + this->branch_size_;
6803 memcpy (p, this->targ_->savres_section()->contents(),
6804 this->targ_->savres_section()->data_size());
6808 // Write out .glink.
6810 template<int size, bool big_endian>
6811 void
6812 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6814 const section_size_type off = this->offset();
6815 const section_size_type oview_size =
6816 convert_to_section_size_type(this->data_size());
6817 unsigned char* const oview = of->get_output_view(off, oview_size);
6818 unsigned char* p;
6820 // The base address of the .plt section.
6821 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6822 Address plt_base = this->targ_->plt_section()->address();
6824 if (size == 64)
6826 if (this->end_branch_table_ != 0)
6828 // Write pltresolve stub.
6829 p = oview;
6830 Address after_bcl = this->address() + 16;
6831 Address pltoff = plt_base - after_bcl;
6833 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
6835 if (this->targ_->abiversion() < 2)
6837 write_insn<big_endian>(p, mflr_12), p += 4;
6838 write_insn<big_endian>(p, bcl_20_31), p += 4;
6839 write_insn<big_endian>(p, mflr_11), p += 4;
6840 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6841 write_insn<big_endian>(p, mtlr_12), p += 4;
6842 write_insn<big_endian>(p, add_11_2_11), p += 4;
6843 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6844 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6845 write_insn<big_endian>(p, mtctr_12), p += 4;
6846 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6848 else
6850 if (this->targ_->has_localentry0())
6852 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6854 write_insn<big_endian>(p, mflr_0), p += 4;
6855 write_insn<big_endian>(p, bcl_20_31), p += 4;
6856 write_insn<big_endian>(p, mflr_11), p += 4;
6857 write_insn<big_endian>(p, mtlr_0), p += 4;
6858 if (this->targ_->has_localentry0())
6860 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
6862 else
6864 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
6866 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6867 write_insn<big_endian>(p, add_11_0_11), p += 4;
6868 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
6869 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6870 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6871 write_insn<big_endian>(p, mtctr_12), p += 4;
6872 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6874 write_insn<big_endian>(p, bctr), p += 4;
6875 gold_assert(p == oview + this->pltresolve_size());
6877 // Write lazy link call stubs.
6878 uint32_t indx = 0;
6879 while (p < oview + this->end_branch_table_)
6881 if (this->targ_->abiversion() < 2)
6883 if (indx < 0x8000)
6885 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6887 else
6889 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
6890 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6893 uint32_t branch_off = 8 - (p - oview);
6894 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6895 indx++;
6899 Address plt_base = this->targ_->plt_section()->address();
6900 Address iplt_base = invalid_address;
6901 unsigned int global_entry_off = this->global_entry_off();
6902 Address global_entry_base = this->address() + global_entry_off;
6903 typename Global_entry_stub_entries::const_iterator ge;
6904 for (ge = this->global_entry_stubs_.begin();
6905 ge != this->global_entry_stubs_.end();
6906 ++ge)
6908 p = oview + global_entry_off + ge->second;
6909 Address plt_addr = ge->first->plt_offset();
6910 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6911 && ge->first->can_use_relative_reloc(false))
6913 if (iplt_base == invalid_address)
6914 iplt_base = this->targ_->iplt_section()->address();
6915 plt_addr += iplt_base;
6917 else
6918 plt_addr += plt_base;
6919 Address my_addr = global_entry_base + ge->second;
6920 Address off = plt_addr - my_addr;
6922 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6923 gold_error(_("linkage table error against `%s'"),
6924 ge->first->demangled_name().c_str());
6926 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6927 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6928 write_insn<big_endian>(p, mtctr_12), p += 4;
6929 write_insn<big_endian>(p, bctr);
6932 else
6934 const Output_data_got_powerpc<size, big_endian>* got
6935 = this->targ_->got_section();
6936 // The address of _GLOBAL_OFFSET_TABLE_.
6937 Address g_o_t = got->address() + got->g_o_t();
6939 // Write out pltresolve branch table.
6940 p = oview;
6941 unsigned int the_end = oview_size - this->pltresolve_size();
6942 unsigned char* end_p = oview + the_end;
6943 while (p < end_p - 8 * 4)
6944 write_insn<big_endian>(p, b + end_p - p), p += 4;
6945 while (p < end_p)
6946 write_insn<big_endian>(p, nop), p += 4;
6948 // Write out pltresolve call stub.
6949 end_p = oview + oview_size;
6950 if (parameters->options().output_is_position_independent())
6952 Address res0_off = 0;
6953 Address after_bcl_off = the_end + 12;
6954 Address bcl_res0 = after_bcl_off - res0_off;
6956 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6957 p += 4;
6958 write_insn<big_endian>(p, mflr_0);
6959 p += 4;
6960 write_insn<big_endian>(p, bcl_20_31);
6961 p += 4;
6962 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6963 p += 4;
6964 write_insn<big_endian>(p, mflr_12);
6965 p += 4;
6966 write_insn<big_endian>(p, mtlr_0);
6967 p += 4;
6968 write_insn<big_endian>(p, sub_11_11_12);
6969 p += 4;
6971 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
6973 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6974 p += 4;
6975 if (ha(got_bcl) == ha(got_bcl + 4))
6977 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6978 p += 4;
6979 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
6981 else
6983 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6984 p += 4;
6985 write_insn<big_endian>(p, lwz_12_12 + 4);
6987 p += 4;
6988 write_insn<big_endian>(p, mtctr_0);
6989 p += 4;
6990 write_insn<big_endian>(p, add_0_11_11);
6991 p += 4;
6992 write_insn<big_endian>(p, add_11_0_11);
6994 else
6996 Address res0 = this->address();
6998 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6999 p += 4;
7000 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7001 p += 4;
7002 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7003 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7004 else
7005 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7006 p += 4;
7007 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7008 p += 4;
7009 write_insn<big_endian>(p, mtctr_0);
7010 p += 4;
7011 write_insn<big_endian>(p, add_0_11_11);
7012 p += 4;
7013 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7014 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7015 else
7016 write_insn<big_endian>(p, lwz_12_12 + 4);
7017 p += 4;
7018 write_insn<big_endian>(p, add_11_0_11);
7020 p += 4;
7021 write_insn<big_endian>(p, bctr);
7022 p += 4;
7023 while (p < end_p)
7025 write_insn<big_endian>(p, nop);
7026 p += 4;
7030 of->write_output_view(off, oview_size, oview);
7034 // A class to handle linker generated save/restore functions.
7036 template<int size, bool big_endian>
7037 class Output_data_save_res : public Output_section_data_build
7039 public:
7040 Output_data_save_res(Symbol_table* symtab);
7042 const unsigned char*
7043 contents() const
7045 return contents_;
7048 protected:
7049 // Write to a map file.
7050 void
7051 do_print_to_mapfile(Mapfile* mapfile) const
7052 { mapfile->print_output_data(this, _("** save/restore")); }
7054 void
7055 do_write(Output_file*);
7057 private:
7058 // The maximum size of save/restore contents.
7059 static const unsigned int savres_max = 218*4;
7061 void
7062 savres_define(Symbol_table* symtab,
7063 const char *name,
7064 unsigned int lo, unsigned int hi,
7065 unsigned char* write_ent(unsigned char*, int),
7066 unsigned char* write_tail(unsigned char*, int));
7068 unsigned char *contents_;
7071 template<bool big_endian>
7072 static unsigned char*
7073 savegpr0(unsigned char* p, int r)
7075 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7076 write_insn<big_endian>(p, insn);
7077 return p + 4;
7080 template<bool big_endian>
7081 static unsigned char*
7082 savegpr0_tail(unsigned char* p, int r)
7084 p = savegpr0<big_endian>(p, r);
7085 uint32_t insn = std_0_1 + 16;
7086 write_insn<big_endian>(p, insn);
7087 p = p + 4;
7088 write_insn<big_endian>(p, blr);
7089 return p + 4;
7092 template<bool big_endian>
7093 static unsigned char*
7094 restgpr0(unsigned char* p, int r)
7096 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7097 write_insn<big_endian>(p, insn);
7098 return p + 4;
7101 template<bool big_endian>
7102 static unsigned char*
7103 restgpr0_tail(unsigned char* p, int r)
7105 uint32_t insn = ld_0_1 + 16;
7106 write_insn<big_endian>(p, insn);
7107 p = p + 4;
7108 p = restgpr0<big_endian>(p, r);
7109 write_insn<big_endian>(p, mtlr_0);
7110 p = p + 4;
7111 if (r == 29)
7113 p = restgpr0<big_endian>(p, 30);
7114 p = restgpr0<big_endian>(p, 31);
7116 write_insn<big_endian>(p, blr);
7117 return p + 4;
7120 template<bool big_endian>
7121 static unsigned char*
7122 savegpr1(unsigned char* p, int r)
7124 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7125 write_insn<big_endian>(p, insn);
7126 return p + 4;
7129 template<bool big_endian>
7130 static unsigned char*
7131 savegpr1_tail(unsigned char* p, int r)
7133 p = savegpr1<big_endian>(p, r);
7134 write_insn<big_endian>(p, blr);
7135 return p + 4;
7138 template<bool big_endian>
7139 static unsigned char*
7140 restgpr1(unsigned char* p, int r)
7142 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7143 write_insn<big_endian>(p, insn);
7144 return p + 4;
7147 template<bool big_endian>
7148 static unsigned char*
7149 restgpr1_tail(unsigned char* p, int r)
7151 p = restgpr1<big_endian>(p, r);
7152 write_insn<big_endian>(p, blr);
7153 return p + 4;
7156 template<bool big_endian>
7157 static unsigned char*
7158 savefpr(unsigned char* p, int r)
7160 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7161 write_insn<big_endian>(p, insn);
7162 return p + 4;
7165 template<bool big_endian>
7166 static unsigned char*
7167 savefpr0_tail(unsigned char* p, int r)
7169 p = savefpr<big_endian>(p, r);
7170 write_insn<big_endian>(p, std_0_1 + 16);
7171 p = p + 4;
7172 write_insn<big_endian>(p, blr);
7173 return p + 4;
7176 template<bool big_endian>
7177 static unsigned char*
7178 restfpr(unsigned char* p, int r)
7180 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7181 write_insn<big_endian>(p, insn);
7182 return p + 4;
7185 template<bool big_endian>
7186 static unsigned char*
7187 restfpr0_tail(unsigned char* p, int r)
7189 write_insn<big_endian>(p, ld_0_1 + 16);
7190 p = p + 4;
7191 p = restfpr<big_endian>(p, r);
7192 write_insn<big_endian>(p, mtlr_0);
7193 p = p + 4;
7194 if (r == 29)
7196 p = restfpr<big_endian>(p, 30);
7197 p = restfpr<big_endian>(p, 31);
7199 write_insn<big_endian>(p, blr);
7200 return p + 4;
7203 template<bool big_endian>
7204 static unsigned char*
7205 savefpr1_tail(unsigned char* p, int r)
7207 p = savefpr<big_endian>(p, r);
7208 write_insn<big_endian>(p, blr);
7209 return p + 4;
7212 template<bool big_endian>
7213 static unsigned char*
7214 restfpr1_tail(unsigned char* p, int r)
7216 p = restfpr<big_endian>(p, r);
7217 write_insn<big_endian>(p, blr);
7218 return p + 4;
7221 template<bool big_endian>
7222 static unsigned char*
7223 savevr(unsigned char* p, int r)
7225 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7226 write_insn<big_endian>(p, insn);
7227 p = p + 4;
7228 insn = stvx_0_12_0 + (r << 21);
7229 write_insn<big_endian>(p, insn);
7230 return p + 4;
7233 template<bool big_endian>
7234 static unsigned char*
7235 savevr_tail(unsigned char* p, int r)
7237 p = savevr<big_endian>(p, r);
7238 write_insn<big_endian>(p, blr);
7239 return p + 4;
7242 template<bool big_endian>
7243 static unsigned char*
7244 restvr(unsigned char* p, int r)
7246 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7247 write_insn<big_endian>(p, insn);
7248 p = p + 4;
7249 insn = lvx_0_12_0 + (r << 21);
7250 write_insn<big_endian>(p, insn);
7251 return p + 4;
7254 template<bool big_endian>
7255 static unsigned char*
7256 restvr_tail(unsigned char* p, int r)
7258 p = restvr<big_endian>(p, r);
7259 write_insn<big_endian>(p, blr);
7260 return p + 4;
7264 template<int size, bool big_endian>
7265 Output_data_save_res<size, big_endian>::Output_data_save_res(
7266 Symbol_table* symtab)
7267 : Output_section_data_build(4),
7268 contents_(NULL)
7270 this->savres_define(symtab,
7271 "_savegpr0_", 14, 31,
7272 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7273 this->savres_define(symtab,
7274 "_restgpr0_", 14, 29,
7275 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7276 this->savres_define(symtab,
7277 "_restgpr0_", 30, 31,
7278 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7279 this->savres_define(symtab,
7280 "_savegpr1_", 14, 31,
7281 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7282 this->savres_define(symtab,
7283 "_restgpr1_", 14, 31,
7284 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7285 this->savres_define(symtab,
7286 "_savefpr_", 14, 31,
7287 savefpr<big_endian>, savefpr0_tail<big_endian>);
7288 this->savres_define(symtab,
7289 "_restfpr_", 14, 29,
7290 restfpr<big_endian>, restfpr0_tail<big_endian>);
7291 this->savres_define(symtab,
7292 "_restfpr_", 30, 31,
7293 restfpr<big_endian>, restfpr0_tail<big_endian>);
7294 this->savres_define(symtab,
7295 "._savef", 14, 31,
7296 savefpr<big_endian>, savefpr1_tail<big_endian>);
7297 this->savres_define(symtab,
7298 "._restf", 14, 31,
7299 restfpr<big_endian>, restfpr1_tail<big_endian>);
7300 this->savres_define(symtab,
7301 "_savevr_", 20, 31,
7302 savevr<big_endian>, savevr_tail<big_endian>);
7303 this->savres_define(symtab,
7304 "_restvr_", 20, 31,
7305 restvr<big_endian>, restvr_tail<big_endian>);
7308 template<int size, bool big_endian>
7309 void
7310 Output_data_save_res<size, big_endian>::savres_define(
7311 Symbol_table* symtab,
7312 const char *name,
7313 unsigned int lo, unsigned int hi,
7314 unsigned char* write_ent(unsigned char*, int),
7315 unsigned char* write_tail(unsigned char*, int))
7317 size_t len = strlen(name);
7318 bool writing = false;
7319 char sym[16];
7321 memcpy(sym, name, len);
7322 sym[len + 2] = 0;
7324 for (unsigned int i = lo; i <= hi; i++)
7326 sym[len + 0] = i / 10 + '0';
7327 sym[len + 1] = i % 10 + '0';
7328 Symbol* gsym = symtab->lookup(sym);
7329 bool refd = gsym != NULL && gsym->is_undefined();
7330 writing = writing || refd;
7331 if (writing)
7333 if (this->contents_ == NULL)
7334 this->contents_ = new unsigned char[this->savres_max];
7336 section_size_type value = this->current_data_size();
7337 unsigned char* p = this->contents_ + value;
7338 if (i != hi)
7339 p = write_ent(p, i);
7340 else
7341 p = write_tail(p, i);
7342 section_size_type cur_size = p - this->contents_;
7343 this->set_current_data_size(cur_size);
7344 if (refd)
7345 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7346 this, value, cur_size - value,
7347 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7348 elfcpp::STV_HIDDEN, 0, false, false);
7353 // Write out save/restore.
7355 template<int size, bool big_endian>
7356 void
7357 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7359 const section_size_type off = this->offset();
7360 const section_size_type oview_size =
7361 convert_to_section_size_type(this->data_size());
7362 unsigned char* const oview = of->get_output_view(off, oview_size);
7363 memcpy(oview, this->contents_, oview_size);
7364 of->write_output_view(off, oview_size, oview);
7368 // Create the glink section.
7370 template<int size, bool big_endian>
7371 void
7372 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7374 if (this->glink_ == NULL)
7376 this->glink_ = new Output_data_glink<size, big_endian>(this);
7377 this->glink_->add_eh_frame(layout);
7378 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7379 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7380 this->glink_, ORDER_TEXT, false);
7384 // Create a PLT entry for a global symbol.
7386 template<int size, bool big_endian>
7387 void
7388 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7389 Layout* layout,
7390 Symbol* gsym)
7392 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7393 && gsym->can_use_relative_reloc(false))
7395 if (this->iplt_ == NULL)
7396 this->make_iplt_section(symtab, layout);
7397 this->iplt_->add_ifunc_entry(gsym);
7399 else
7401 if (this->plt_ == NULL)
7402 this->make_plt_section(symtab, layout);
7403 this->plt_->add_entry(gsym);
7407 // Make a PLT entry for a local symbol.
7409 template<int size, bool big_endian>
7410 void
7411 Target_powerpc<size, big_endian>::make_local_plt_entry(
7412 Layout* layout,
7413 Sized_relobj_file<size, big_endian>* relobj,
7414 unsigned int r_sym)
7416 if (this->lplt_ == NULL)
7417 this->make_lplt_section(layout);
7418 this->lplt_->add_local_entry(relobj, r_sym);
7421 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7423 template<int size, bool big_endian>
7424 void
7425 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7426 Symbol_table* symtab,
7427 Layout* layout,
7428 Sized_relobj_file<size, big_endian>* relobj,
7429 unsigned int r_sym)
7431 if (this->iplt_ == NULL)
7432 this->make_iplt_section(symtab, layout);
7433 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7436 // Return the number of entries in the PLT.
7438 template<int size, bool big_endian>
7439 unsigned int
7440 Target_powerpc<size, big_endian>::plt_entry_count() const
7442 if (this->plt_ == NULL)
7443 return 0;
7444 return this->plt_->entry_count();
7447 // Create a GOT entry for local dynamic __tls_get_addr calls.
7449 template<int size, bool big_endian>
7450 unsigned int
7451 Target_powerpc<size, big_endian>::tlsld_got_offset(
7452 Symbol_table* symtab,
7453 Layout* layout,
7454 Sized_relobj_file<size, big_endian>* object)
7456 if (this->tlsld_got_offset_ == -1U)
7458 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7459 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7460 Output_data_got_powerpc<size, big_endian>* got
7461 = this->got_section(symtab, layout);
7462 unsigned int got_offset = got->add_constant_pair(0, 0);
7463 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7464 got_offset, 0);
7465 this->tlsld_got_offset_ = got_offset;
7467 return this->tlsld_got_offset_;
7470 // Get the Reference_flags for a particular relocation.
7472 template<int size, bool big_endian>
7474 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7475 unsigned int r_type,
7476 const Target_powerpc* target)
7478 int ref = 0;
7480 switch (r_type)
7482 case elfcpp::R_POWERPC_NONE:
7483 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7484 case elfcpp::R_POWERPC_GNU_VTENTRY:
7485 case elfcpp::R_PPC64_TOC:
7486 // No symbol reference.
7487 break;
7489 case elfcpp::R_PPC64_ADDR64:
7490 case elfcpp::R_PPC64_UADDR64:
7491 case elfcpp::R_POWERPC_ADDR32:
7492 case elfcpp::R_POWERPC_UADDR32:
7493 case elfcpp::R_POWERPC_ADDR16:
7494 case elfcpp::R_POWERPC_UADDR16:
7495 case elfcpp::R_POWERPC_ADDR16_LO:
7496 case elfcpp::R_POWERPC_ADDR16_HI:
7497 case elfcpp::R_POWERPC_ADDR16_HA:
7498 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7499 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7500 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7501 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7502 case elfcpp::R_PPC64_D34:
7503 case elfcpp::R_PPC64_D34_LO:
7504 case elfcpp::R_PPC64_D34_HI30:
7505 case elfcpp::R_PPC64_D34_HA30:
7506 case elfcpp::R_PPC64_D28:
7507 ref = Symbol::ABSOLUTE_REF;
7508 break;
7510 case elfcpp::R_POWERPC_ADDR24:
7511 case elfcpp::R_POWERPC_ADDR14:
7512 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7513 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7514 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7515 break;
7517 case elfcpp::R_PPC64_REL64:
7518 case elfcpp::R_POWERPC_REL32:
7519 case elfcpp::R_PPC_LOCAL24PC:
7520 case elfcpp::R_POWERPC_REL16:
7521 case elfcpp::R_POWERPC_REL16_LO:
7522 case elfcpp::R_POWERPC_REL16_HI:
7523 case elfcpp::R_POWERPC_REL16_HA:
7524 case elfcpp::R_PPC64_REL16_HIGH:
7525 case elfcpp::R_PPC64_REL16_HIGHA:
7526 case elfcpp::R_PPC64_REL16_HIGHER:
7527 case elfcpp::R_PPC64_REL16_HIGHERA:
7528 case elfcpp::R_PPC64_REL16_HIGHEST:
7529 case elfcpp::R_PPC64_REL16_HIGHESTA:
7530 case elfcpp::R_PPC64_PCREL34:
7531 case elfcpp::R_PPC64_REL16_HIGHER34:
7532 case elfcpp::R_PPC64_REL16_HIGHERA34:
7533 case elfcpp::R_PPC64_REL16_HIGHEST34:
7534 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7535 case elfcpp::R_PPC64_PCREL28:
7536 ref = Symbol::RELATIVE_REF;
7537 break;
7539 case elfcpp::R_PPC64_REL24_NOTOC:
7540 if (size == 32)
7541 break;
7542 // Fall through.
7543 case elfcpp::R_POWERPC_REL24:
7544 case elfcpp::R_PPC_PLTREL24:
7545 case elfcpp::R_POWERPC_REL14:
7546 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7547 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7548 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7549 break;
7551 case elfcpp::R_POWERPC_GOT16:
7552 case elfcpp::R_POWERPC_GOT16_LO:
7553 case elfcpp::R_POWERPC_GOT16_HI:
7554 case elfcpp::R_POWERPC_GOT16_HA:
7555 case elfcpp::R_PPC64_GOT16_DS:
7556 case elfcpp::R_PPC64_GOT16_LO_DS:
7557 case elfcpp::R_PPC64_GOT_PCREL34:
7558 case elfcpp::R_PPC64_TOC16:
7559 case elfcpp::R_PPC64_TOC16_LO:
7560 case elfcpp::R_PPC64_TOC16_HI:
7561 case elfcpp::R_PPC64_TOC16_HA:
7562 case elfcpp::R_PPC64_TOC16_DS:
7563 case elfcpp::R_PPC64_TOC16_LO_DS:
7564 case elfcpp::R_POWERPC_PLT16_LO:
7565 case elfcpp::R_POWERPC_PLT16_HI:
7566 case elfcpp::R_POWERPC_PLT16_HA:
7567 case elfcpp::R_PPC64_PLT16_LO_DS:
7568 case elfcpp::R_PPC64_PLT_PCREL34:
7569 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7570 ref = Symbol::RELATIVE_REF;
7571 break;
7573 case elfcpp::R_POWERPC_GOT_TPREL16:
7574 case elfcpp::R_POWERPC_TLS:
7575 case elfcpp::R_PPC64_TLSGD:
7576 case elfcpp::R_PPC64_TLSLD:
7577 case elfcpp::R_PPC64_TPREL34:
7578 case elfcpp::R_PPC64_DTPREL34:
7579 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7580 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7581 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7582 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7583 ref = Symbol::TLS_REF;
7584 break;
7586 case elfcpp::R_POWERPC_COPY:
7587 case elfcpp::R_POWERPC_GLOB_DAT:
7588 case elfcpp::R_POWERPC_JMP_SLOT:
7589 case elfcpp::R_POWERPC_RELATIVE:
7590 case elfcpp::R_POWERPC_DTPMOD:
7591 default:
7592 // Not expected. We will give an error later.
7593 break;
7596 if (size == 64 && target->abiversion() < 2)
7597 ref |= Symbol::FUNC_DESC_ABI;
7598 return ref;
7601 // Report an unsupported relocation against a local symbol.
7603 template<int size, bool big_endian>
7604 void
7605 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7606 Sized_relobj_file<size, big_endian>* object,
7607 unsigned int r_type)
7609 gold_error(_("%s: unsupported reloc %u against local symbol"),
7610 object->name().c_str(), r_type);
7613 // We are about to emit a dynamic relocation of type R_TYPE. If the
7614 // dynamic linker does not support it, issue an error.
7616 template<int size, bool big_endian>
7617 void
7618 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7619 unsigned int r_type)
7621 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7623 // These are the relocation types supported by glibc for both 32-bit
7624 // and 64-bit powerpc.
7625 switch (r_type)
7627 case elfcpp::R_POWERPC_NONE:
7628 case elfcpp::R_POWERPC_RELATIVE:
7629 case elfcpp::R_POWERPC_GLOB_DAT:
7630 case elfcpp::R_POWERPC_DTPMOD:
7631 case elfcpp::R_POWERPC_DTPREL:
7632 case elfcpp::R_POWERPC_TPREL:
7633 case elfcpp::R_POWERPC_JMP_SLOT:
7634 case elfcpp::R_POWERPC_COPY:
7635 case elfcpp::R_POWERPC_IRELATIVE:
7636 case elfcpp::R_POWERPC_ADDR32:
7637 case elfcpp::R_POWERPC_UADDR32:
7638 case elfcpp::R_POWERPC_ADDR24:
7639 case elfcpp::R_POWERPC_ADDR16:
7640 case elfcpp::R_POWERPC_UADDR16:
7641 case elfcpp::R_POWERPC_ADDR16_LO:
7642 case elfcpp::R_POWERPC_ADDR16_HI:
7643 case elfcpp::R_POWERPC_ADDR16_HA:
7644 case elfcpp::R_POWERPC_ADDR14:
7645 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7646 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7647 case elfcpp::R_POWERPC_REL32:
7648 case elfcpp::R_POWERPC_TPREL16:
7649 case elfcpp::R_POWERPC_TPREL16_LO:
7650 case elfcpp::R_POWERPC_TPREL16_HI:
7651 case elfcpp::R_POWERPC_TPREL16_HA:
7652 return;
7654 default:
7655 break;
7658 if (size == 64)
7660 switch (r_type)
7662 // These are the relocation types supported only on 64-bit.
7663 case elfcpp::R_PPC64_ADDR64:
7664 case elfcpp::R_PPC64_UADDR64:
7665 case elfcpp::R_PPC64_JMP_IREL:
7666 case elfcpp::R_PPC64_ADDR16_DS:
7667 case elfcpp::R_PPC64_ADDR16_LO_DS:
7668 case elfcpp::R_PPC64_ADDR16_HIGH:
7669 case elfcpp::R_PPC64_ADDR16_HIGHA:
7670 case elfcpp::R_PPC64_ADDR16_HIGHER:
7671 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7672 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7673 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7674 case elfcpp::R_PPC64_REL64:
7675 case elfcpp::R_POWERPC_ADDR30:
7676 case elfcpp::R_PPC64_TPREL16_DS:
7677 case elfcpp::R_PPC64_TPREL16_LO_DS:
7678 case elfcpp::R_PPC64_TPREL16_HIGH:
7679 case elfcpp::R_PPC64_TPREL16_HIGHA:
7680 case elfcpp::R_PPC64_TPREL16_HIGHER:
7681 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7682 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7683 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7684 return;
7686 default:
7687 break;
7690 else
7692 switch (r_type)
7694 // These are the relocation types supported only on 32-bit.
7695 // ??? glibc ld.so doesn't need to support these.
7696 case elfcpp::R_POWERPC_REL24:
7697 case elfcpp::R_POWERPC_DTPREL16:
7698 case elfcpp::R_POWERPC_DTPREL16_LO:
7699 case elfcpp::R_POWERPC_DTPREL16_HI:
7700 case elfcpp::R_POWERPC_DTPREL16_HA:
7701 return;
7703 default:
7704 break;
7708 // This prevents us from issuing more than one error per reloc
7709 // section. But we can still wind up issuing more than one
7710 // error per object file.
7711 if (this->issued_non_pic_error_)
7712 return;
7713 gold_assert(parameters->options().output_is_position_independent());
7714 object->error(_("requires unsupported dynamic reloc; "
7715 "recompile with -fPIC"));
7716 this->issued_non_pic_error_ = true;
7717 return;
7720 // Return whether we need to make a PLT entry for a relocation of the
7721 // given type against a STT_GNU_IFUNC symbol.
7723 template<int size, bool big_endian>
7724 bool
7725 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7726 Target_powerpc<size, big_endian>* target,
7727 Sized_relobj_file<size, big_endian>* object,
7728 unsigned int r_type,
7729 bool report_err)
7731 // In non-pic code any reference will resolve to the plt call stub
7732 // for the ifunc symbol.
7733 if ((size == 32 || target->abiversion() >= 2)
7734 && !parameters->options().output_is_position_independent())
7735 return true;
7737 switch (r_type)
7739 // Word size refs from data sections are OK, but don't need a PLT entry.
7740 case elfcpp::R_POWERPC_ADDR32:
7741 case elfcpp::R_POWERPC_UADDR32:
7742 if (size == 32)
7743 return false;
7744 break;
7746 case elfcpp::R_PPC64_ADDR64:
7747 case elfcpp::R_PPC64_UADDR64:
7748 if (size == 64)
7749 return false;
7750 break;
7752 // GOT refs are good, but also don't need a PLT entry.
7753 case elfcpp::R_POWERPC_GOT16:
7754 case elfcpp::R_POWERPC_GOT16_LO:
7755 case elfcpp::R_POWERPC_GOT16_HI:
7756 case elfcpp::R_POWERPC_GOT16_HA:
7757 case elfcpp::R_PPC64_GOT16_DS:
7758 case elfcpp::R_PPC64_GOT16_LO_DS:
7759 case elfcpp::R_PPC64_GOT_PCREL34:
7760 return false;
7762 // PLT relocs are OK and need a PLT entry.
7763 case elfcpp::R_POWERPC_PLT16_LO:
7764 case elfcpp::R_POWERPC_PLT16_HI:
7765 case elfcpp::R_POWERPC_PLT16_HA:
7766 case elfcpp::R_PPC64_PLT16_LO_DS:
7767 case elfcpp::R_POWERPC_PLTSEQ:
7768 case elfcpp::R_POWERPC_PLTCALL:
7769 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7770 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7771 case elfcpp::R_PPC64_PLT_PCREL34:
7772 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7773 return true;
7774 break;
7776 // Function calls are good, and these do need a PLT entry.
7777 case elfcpp::R_PPC64_REL24_NOTOC:
7778 if (size == 32)
7779 break;
7780 // Fall through.
7781 case elfcpp::R_POWERPC_ADDR24:
7782 case elfcpp::R_POWERPC_ADDR14:
7783 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7784 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7785 case elfcpp::R_POWERPC_REL24:
7786 case elfcpp::R_PPC_PLTREL24:
7787 case elfcpp::R_POWERPC_REL14:
7788 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7789 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7790 return true;
7792 default:
7793 break;
7796 // Anything else is a problem.
7797 // If we are building a static executable, the libc startup function
7798 // responsible for applying indirect function relocations is going
7799 // to complain about the reloc type.
7800 // If we are building a dynamic executable, we will have a text
7801 // relocation. The dynamic loader will set the text segment
7802 // writable and non-executable to apply text relocations. So we'll
7803 // segfault when trying to run the indirection function to resolve
7804 // the reloc.
7805 if (report_err)
7806 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7807 object->name().c_str(), r_type);
7808 return false;
7811 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7812 // reloc.
7814 static bool
7815 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7817 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7818 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7819 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7820 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7821 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7822 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7823 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7824 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7825 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7826 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7827 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7828 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7829 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7830 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7831 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7832 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7833 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7834 /* Exclude lfqu by testing reloc. If relocs are ever
7835 defined for the reduced D field in psq_lu then those
7836 will need testing too. */
7837 && r_type != elfcpp::R_PPC64_TOC16_LO
7838 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7839 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7840 && (insn & 1) == 0)
7841 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7842 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7843 /* Exclude stfqu. psq_stu as above for psq_lu. */
7844 && r_type != elfcpp::R_PPC64_TOC16_LO
7845 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7846 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7847 && (insn & 1) == 0));
7850 // Scan a relocation for a local symbol.
7852 template<int size, bool big_endian>
7853 inline void
7854 Target_powerpc<size, big_endian>::Scan::local(
7855 Symbol_table* symtab,
7856 Layout* layout,
7857 Target_powerpc<size, big_endian>* target,
7858 Sized_relobj_file<size, big_endian>* object,
7859 unsigned int data_shndx,
7860 Output_section* output_section,
7861 const elfcpp::Rela<size, big_endian>& reloc,
7862 unsigned int r_type,
7863 const elfcpp::Sym<size, big_endian>& lsym,
7864 bool is_discarded)
7866 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
7868 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7869 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7871 this->expect_tls_get_addr_call();
7872 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7873 if (tls_type != tls::TLSOPT_NONE)
7874 this->skip_next_tls_get_addr_call();
7876 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7877 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7879 this->expect_tls_get_addr_call();
7880 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7881 if (tls_type != tls::TLSOPT_NONE)
7882 this->skip_next_tls_get_addr_call();
7885 Powerpc_relobj<size, big_endian>* ppc_object
7886 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7888 if (is_discarded)
7890 if (size == 64
7891 && data_shndx == ppc_object->opd_shndx()
7892 && r_type == elfcpp::R_PPC64_ADDR64)
7893 ppc_object->set_opd_discard(reloc.get_r_offset());
7894 return;
7897 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7898 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7899 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7901 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7902 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7903 r_type, r_sym, reloc.get_r_addend());
7904 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7907 switch (r_type)
7909 case elfcpp::R_POWERPC_NONE:
7910 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7911 case elfcpp::R_POWERPC_GNU_VTENTRY:
7912 case elfcpp::R_POWERPC_TLS:
7913 case elfcpp::R_PPC64_ENTRY:
7914 case elfcpp::R_POWERPC_PLTSEQ:
7915 case elfcpp::R_POWERPC_PLTCALL:
7916 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7917 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7918 case elfcpp::R_PPC64_PCREL_OPT:
7919 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7920 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7921 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7922 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7923 case elfcpp::R_PPC64_REL16_HIGHER34:
7924 case elfcpp::R_PPC64_REL16_HIGHERA34:
7925 case elfcpp::R_PPC64_REL16_HIGHEST34:
7926 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7927 case elfcpp::R_PPC64_D34:
7928 case elfcpp::R_PPC64_D34_LO:
7929 case elfcpp::R_PPC64_D34_HI30:
7930 case elfcpp::R_PPC64_D34_HA30:
7931 case elfcpp::R_PPC64_D28:
7932 case elfcpp::R_PPC64_PCREL34:
7933 case elfcpp::R_PPC64_PCREL28:
7934 case elfcpp::R_PPC64_TPREL34:
7935 case elfcpp::R_PPC64_DTPREL34:
7936 break;
7938 case elfcpp::R_PPC64_TOC:
7940 Output_data_got_powerpc<size, big_endian>* got
7941 = target->got_section(symtab, layout);
7942 if (parameters->options().output_is_position_independent())
7944 Address off = reloc.get_r_offset();
7945 if (size == 64
7946 && target->abiversion() < 2
7947 && data_shndx == ppc_object->opd_shndx()
7948 && ppc_object->get_opd_discard(off - 8))
7949 break;
7951 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7952 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7953 rela_dyn->add_output_section_relative(got->output_section(),
7954 elfcpp::R_POWERPC_RELATIVE,
7955 output_section,
7956 object, data_shndx, off,
7957 symobj->toc_base_offset());
7960 break;
7962 case elfcpp::R_PPC64_ADDR64:
7963 case elfcpp::R_PPC64_UADDR64:
7964 case elfcpp::R_POWERPC_ADDR32:
7965 case elfcpp::R_POWERPC_UADDR32:
7966 case elfcpp::R_POWERPC_ADDR24:
7967 case elfcpp::R_POWERPC_ADDR16:
7968 case elfcpp::R_POWERPC_ADDR16_LO:
7969 case elfcpp::R_POWERPC_ADDR16_HI:
7970 case elfcpp::R_POWERPC_ADDR16_HA:
7971 case elfcpp::R_POWERPC_UADDR16:
7972 case elfcpp::R_PPC64_ADDR16_HIGH:
7973 case elfcpp::R_PPC64_ADDR16_HIGHA:
7974 case elfcpp::R_PPC64_ADDR16_HIGHER:
7975 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7976 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7977 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7978 case elfcpp::R_PPC64_ADDR16_DS:
7979 case elfcpp::R_PPC64_ADDR16_LO_DS:
7980 case elfcpp::R_POWERPC_ADDR14:
7981 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7982 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7983 // If building a shared library (or a position-independent
7984 // executable), we need to create a dynamic relocation for
7985 // this location.
7986 if (parameters->options().output_is_position_independent()
7987 || (size == 64 && is_ifunc && target->abiversion() < 2))
7989 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7990 is_ifunc);
7991 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7992 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7993 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
7995 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7996 : elfcpp::R_POWERPC_RELATIVE);
7997 rela_dyn->add_local_relative(object, r_sym, dynrel,
7998 output_section, data_shndx,
7999 reloc.get_r_offset(),
8000 reloc.get_r_addend(), false);
8002 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8004 check_non_pic(object, r_type);
8005 rela_dyn->add_local(object, r_sym, r_type, output_section,
8006 data_shndx, reloc.get_r_offset(),
8007 reloc.get_r_addend());
8009 else
8011 gold_assert(lsym.get_st_value() == 0);
8012 unsigned int shndx = lsym.get_st_shndx();
8013 bool is_ordinary;
8014 shndx = object->adjust_sym_shndx(r_sym, shndx,
8015 &is_ordinary);
8016 if (!is_ordinary)
8017 object->error(_("section symbol %u has bad shndx %u"),
8018 r_sym, shndx);
8019 else
8020 rela_dyn->add_local_section(object, shndx, r_type,
8021 output_section, data_shndx,
8022 reloc.get_r_offset());
8025 break;
8027 case elfcpp::R_PPC64_PLT_PCREL34:
8028 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8029 case elfcpp::R_POWERPC_PLT16_LO:
8030 case elfcpp::R_POWERPC_PLT16_HI:
8031 case elfcpp::R_POWERPC_PLT16_HA:
8032 case elfcpp::R_PPC64_PLT16_LO_DS:
8033 if (!is_ifunc)
8035 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8036 target->make_local_plt_entry(layout, object, r_sym);
8038 break;
8040 case elfcpp::R_PPC64_REL24_NOTOC:
8041 if (size == 32)
8042 break;
8043 // Fall through.
8044 case elfcpp::R_POWERPC_REL24:
8045 case elfcpp::R_PPC_PLTREL24:
8046 case elfcpp::R_PPC_LOCAL24PC:
8047 case elfcpp::R_POWERPC_REL14:
8048 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8049 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8050 if (!is_ifunc)
8052 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8053 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8054 r_type, r_sym, reloc.get_r_addend());
8056 break;
8058 case elfcpp::R_PPC64_TOCSAVE:
8059 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8060 // caller has already saved r2 and thus a plt call stub need not
8061 // save r2.
8062 if (size == 64
8063 && target->mark_pltcall(ppc_object, data_shndx,
8064 reloc.get_r_offset() - 4, symtab))
8066 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8067 unsigned int shndx = lsym.get_st_shndx();
8068 bool is_ordinary;
8069 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8070 if (!is_ordinary)
8071 object->error(_("tocsave symbol %u has bad shndx %u"),
8072 r_sym, shndx);
8073 else
8074 target->add_tocsave(ppc_object, shndx,
8075 lsym.get_st_value() + reloc.get_r_addend());
8077 break;
8079 case elfcpp::R_PPC64_REL64:
8080 case elfcpp::R_POWERPC_REL32:
8081 case elfcpp::R_POWERPC_REL16:
8082 case elfcpp::R_POWERPC_REL16_LO:
8083 case elfcpp::R_POWERPC_REL16_HI:
8084 case elfcpp::R_POWERPC_REL16_HA:
8085 case elfcpp::R_POWERPC_REL16DX_HA:
8086 case elfcpp::R_PPC64_REL16_HIGH:
8087 case elfcpp::R_PPC64_REL16_HIGHA:
8088 case elfcpp::R_PPC64_REL16_HIGHER:
8089 case elfcpp::R_PPC64_REL16_HIGHERA:
8090 case elfcpp::R_PPC64_REL16_HIGHEST:
8091 case elfcpp::R_PPC64_REL16_HIGHESTA:
8092 case elfcpp::R_POWERPC_SECTOFF:
8093 case elfcpp::R_POWERPC_SECTOFF_LO:
8094 case elfcpp::R_POWERPC_SECTOFF_HI:
8095 case elfcpp::R_POWERPC_SECTOFF_HA:
8096 case elfcpp::R_PPC64_SECTOFF_DS:
8097 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8098 case elfcpp::R_POWERPC_TPREL16:
8099 case elfcpp::R_POWERPC_TPREL16_LO:
8100 case elfcpp::R_POWERPC_TPREL16_HI:
8101 case elfcpp::R_POWERPC_TPREL16_HA:
8102 case elfcpp::R_PPC64_TPREL16_DS:
8103 case elfcpp::R_PPC64_TPREL16_LO_DS:
8104 case elfcpp::R_PPC64_TPREL16_HIGH:
8105 case elfcpp::R_PPC64_TPREL16_HIGHA:
8106 case elfcpp::R_PPC64_TPREL16_HIGHER:
8107 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8108 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8109 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8110 case elfcpp::R_POWERPC_DTPREL16:
8111 case elfcpp::R_POWERPC_DTPREL16_LO:
8112 case elfcpp::R_POWERPC_DTPREL16_HI:
8113 case elfcpp::R_POWERPC_DTPREL16_HA:
8114 case elfcpp::R_PPC64_DTPREL16_DS:
8115 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8116 case elfcpp::R_PPC64_DTPREL16_HIGH:
8117 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8118 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8119 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8120 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8121 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8122 case elfcpp::R_PPC64_TLSGD:
8123 case elfcpp::R_PPC64_TLSLD:
8124 case elfcpp::R_PPC64_ADDR64_LOCAL:
8125 break;
8127 case elfcpp::R_PPC64_GOT_PCREL34:
8128 case elfcpp::R_POWERPC_GOT16:
8129 case elfcpp::R_POWERPC_GOT16_LO:
8130 case elfcpp::R_POWERPC_GOT16_HI:
8131 case elfcpp::R_POWERPC_GOT16_HA:
8132 case elfcpp::R_PPC64_GOT16_DS:
8133 case elfcpp::R_PPC64_GOT16_LO_DS:
8135 // The symbol requires a GOT entry.
8136 Output_data_got_powerpc<size, big_endian>* got
8137 = target->got_section(symtab, layout);
8138 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8140 if (!parameters->options().output_is_position_independent())
8142 if (is_ifunc
8143 && (size == 32 || target->abiversion() >= 2))
8144 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
8145 else
8146 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
8148 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
8150 // If we are generating a shared object or a pie, this
8151 // symbol's GOT entry will be set by a dynamic relocation.
8152 unsigned int off;
8153 off = got->add_constant(0);
8154 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
8156 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8157 is_ifunc);
8158 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8159 : elfcpp::R_POWERPC_RELATIVE);
8160 rela_dyn->add_local_relative(object, r_sym, dynrel,
8161 got, off, 0, false);
8164 break;
8166 case elfcpp::R_PPC64_TOC16:
8167 case elfcpp::R_PPC64_TOC16_LO:
8168 case elfcpp::R_PPC64_TOC16_HI:
8169 case elfcpp::R_PPC64_TOC16_HA:
8170 case elfcpp::R_PPC64_TOC16_DS:
8171 case elfcpp::R_PPC64_TOC16_LO_DS:
8172 // We need a GOT section.
8173 target->got_section(symtab, layout);
8174 break;
8176 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8177 case elfcpp::R_POWERPC_GOT_TLSGD16:
8178 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8179 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8180 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8182 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8183 if (tls_type == tls::TLSOPT_NONE)
8185 Output_data_got_powerpc<size, big_endian>* got
8186 = target->got_section(symtab, layout);
8187 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8188 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8189 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
8190 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
8192 else if (tls_type == tls::TLSOPT_TO_LE)
8194 // no GOT relocs needed for Local Exec.
8196 else
8197 gold_unreachable();
8199 break;
8201 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8202 case elfcpp::R_POWERPC_GOT_TLSLD16:
8203 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8204 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8205 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8207 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8208 if (tls_type == tls::TLSOPT_NONE)
8209 target->tlsld_got_offset(symtab, layout, object);
8210 else if (tls_type == tls::TLSOPT_TO_LE)
8212 // no GOT relocs needed for Local Exec.
8213 if (parameters->options().emit_relocs())
8215 Output_section* os = layout->tls_segment()->first_section();
8216 gold_assert(os != NULL);
8217 os->set_needs_symtab_index();
8220 else
8221 gold_unreachable();
8223 break;
8225 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8226 case elfcpp::R_POWERPC_GOT_DTPREL16:
8227 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8228 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8229 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8231 Output_data_got_powerpc<size, big_endian>* got
8232 = target->got_section(symtab, layout);
8233 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8234 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
8236 break;
8238 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8239 case elfcpp::R_POWERPC_GOT_TPREL16:
8240 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8241 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8242 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8244 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8245 if (tls_type == tls::TLSOPT_NONE)
8247 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8248 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
8250 Output_data_got_powerpc<size, big_endian>* got
8251 = target->got_section(symtab, layout);
8252 unsigned int off = got->add_constant(0);
8253 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
8255 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8256 rela_dyn->add_symbolless_local_addend(object, r_sym,
8257 elfcpp::R_POWERPC_TPREL,
8258 got, off, 0);
8261 else if (tls_type == tls::TLSOPT_TO_LE)
8263 // no GOT relocs needed for Local Exec.
8265 else
8266 gold_unreachable();
8268 break;
8270 default:
8271 unsupported_reloc_local(object, r_type);
8272 break;
8275 if (size == 64
8276 && parameters->options().toc_optimize())
8278 if (data_shndx == ppc_object->toc_shndx())
8280 bool ok = true;
8281 if (r_type != elfcpp::R_PPC64_ADDR64
8282 || (is_ifunc && target->abiversion() < 2))
8283 ok = false;
8284 else if (parameters->options().output_is_position_independent())
8286 if (is_ifunc)
8287 ok = false;
8288 else
8290 unsigned int shndx = lsym.get_st_shndx();
8291 if (shndx >= elfcpp::SHN_LORESERVE
8292 && shndx != elfcpp::SHN_XINDEX)
8293 ok = false;
8296 if (!ok)
8297 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8300 enum {no_check, check_lo, check_ha} insn_check;
8301 switch (r_type)
8303 default:
8304 insn_check = no_check;
8305 break;
8307 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8308 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8309 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8310 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8311 case elfcpp::R_POWERPC_GOT16_HA:
8312 case elfcpp::R_PPC64_TOC16_HA:
8313 insn_check = check_ha;
8314 break;
8316 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8317 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8318 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8319 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8320 case elfcpp::R_POWERPC_GOT16_LO:
8321 case elfcpp::R_PPC64_GOT16_LO_DS:
8322 case elfcpp::R_PPC64_TOC16_LO:
8323 case elfcpp::R_PPC64_TOC16_LO_DS:
8324 insn_check = check_lo;
8325 break;
8328 section_size_type slen;
8329 const unsigned char* view = NULL;
8330 if (insn_check != no_check)
8332 view = ppc_object->section_contents(data_shndx, &slen, false);
8333 section_size_type off =
8334 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8335 if (off < slen)
8337 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8338 if (insn_check == check_lo
8339 ? !ok_lo_toc_insn(insn, r_type)
8340 : ((insn & ((0x3f << 26) | 0x1f << 16))
8341 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8343 ppc_object->set_no_toc_opt();
8344 gold_warning(_("%s: toc optimization is not supported "
8345 "for %#08x instruction"),
8346 ppc_object->name().c_str(), insn);
8351 switch (r_type)
8353 default:
8354 break;
8355 case elfcpp::R_PPC64_TOC16:
8356 case elfcpp::R_PPC64_TOC16_LO:
8357 case elfcpp::R_PPC64_TOC16_HI:
8358 case elfcpp::R_PPC64_TOC16_HA:
8359 case elfcpp::R_PPC64_TOC16_DS:
8360 case elfcpp::R_PPC64_TOC16_LO_DS:
8361 unsigned int shndx = lsym.get_st_shndx();
8362 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8363 bool is_ordinary;
8364 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8365 if (is_ordinary && shndx == ppc_object->toc_shndx())
8367 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8368 if (dst_off < ppc_object->section_size(shndx))
8370 bool ok = false;
8371 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8372 ok = true;
8373 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8375 // Need to check that the insn is a ld
8376 if (!view)
8377 view = ppc_object->section_contents(data_shndx,
8378 &slen,
8379 false);
8380 section_size_type off =
8381 (convert_to_section_size_type(reloc.get_r_offset())
8382 + (big_endian ? -2 : 3));
8383 if (off < slen
8384 && (view[off] & (0x3f << 2)) == 58u << 2)
8385 ok = true;
8387 if (!ok)
8388 ppc_object->set_no_toc_opt(dst_off);
8391 break;
8395 if (size == 32)
8397 switch (r_type)
8399 case elfcpp::R_POWERPC_REL32:
8400 if (ppc_object->got2_shndx() != 0
8401 && parameters->options().output_is_position_independent())
8403 unsigned int shndx = lsym.get_st_shndx();
8404 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8405 bool is_ordinary;
8406 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8407 if (is_ordinary && shndx == ppc_object->got2_shndx()
8408 && (ppc_object->section_flags(data_shndx)
8409 & elfcpp::SHF_EXECINSTR) != 0)
8410 gold_error(_("%s: unsupported -mbss-plt code"),
8411 ppc_object->name().c_str());
8413 break;
8414 default:
8415 break;
8419 switch (r_type)
8421 case elfcpp::R_POWERPC_GOT_TLSLD16:
8422 case elfcpp::R_POWERPC_GOT_TLSGD16:
8423 case elfcpp::R_POWERPC_GOT_TPREL16:
8424 case elfcpp::R_POWERPC_GOT_DTPREL16:
8425 case elfcpp::R_POWERPC_GOT16:
8426 case elfcpp::R_PPC64_GOT16_DS:
8427 case elfcpp::R_PPC64_TOC16:
8428 case elfcpp::R_PPC64_TOC16_DS:
8429 ppc_object->set_has_small_toc_reloc();
8430 break;
8431 default:
8432 break;
8435 switch (r_type)
8437 case elfcpp::R_PPC64_TPREL16_DS:
8438 case elfcpp::R_PPC64_TPREL16_LO_DS:
8439 case elfcpp::R_PPC64_TPREL16_HIGH:
8440 case elfcpp::R_PPC64_TPREL16_HIGHA:
8441 case elfcpp::R_PPC64_TPREL16_HIGHER:
8442 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8443 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8444 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8445 case elfcpp::R_PPC64_TPREL34:
8446 if (size != 64)
8447 break;
8448 // Fall through.
8449 case elfcpp::R_POWERPC_TPREL16:
8450 case elfcpp::R_POWERPC_TPREL16_LO:
8451 case elfcpp::R_POWERPC_TPREL16_HI:
8452 case elfcpp::R_POWERPC_TPREL16_HA:
8453 layout->set_has_static_tls();
8454 break;
8455 default:
8456 break;
8459 switch (r_type)
8461 case elfcpp::R_POWERPC_TPREL16_HA:
8462 if (target->tprel_opt())
8464 section_size_type slen;
8465 const unsigned char* view = NULL;
8466 view = ppc_object->section_contents(data_shndx, &slen, false);
8467 section_size_type off
8468 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8469 if (off < slen)
8471 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8472 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8473 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8474 target->set_tprel_opt(false);
8477 break;
8479 case elfcpp::R_PPC64_TPREL16_HIGH:
8480 case elfcpp::R_PPC64_TPREL16_HIGHA:
8481 case elfcpp::R_PPC64_TPREL16_HIGHER:
8482 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8483 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8484 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8485 if (size != 64)
8486 break;
8487 // Fall through.
8488 case elfcpp::R_POWERPC_TPREL16_HI:
8489 target->set_tprel_opt(false);
8490 break;
8491 default:
8492 break;
8495 switch (r_type)
8497 case elfcpp::R_PPC64_D34:
8498 case elfcpp::R_PPC64_D34_LO:
8499 case elfcpp::R_PPC64_D34_HI30:
8500 case elfcpp::R_PPC64_D34_HA30:
8501 case elfcpp::R_PPC64_D28:
8502 case elfcpp::R_PPC64_PCREL34:
8503 case elfcpp::R_PPC64_PCREL28:
8504 case elfcpp::R_PPC64_TPREL34:
8505 case elfcpp::R_PPC64_DTPREL34:
8506 case elfcpp::R_PPC64_PLT_PCREL34:
8507 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8508 case elfcpp::R_PPC64_GOT_PCREL34:
8509 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8510 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8511 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8512 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8513 target->set_power10_relocs();
8514 break;
8515 default:
8516 break;
8520 // Report an unsupported relocation against a global symbol.
8522 template<int size, bool big_endian>
8523 void
8524 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8525 Sized_relobj_file<size, big_endian>* object,
8526 unsigned int r_type,
8527 Symbol* gsym)
8529 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8530 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8533 // Scan a relocation for a global symbol.
8535 template<int size, bool big_endian>
8536 inline void
8537 Target_powerpc<size, big_endian>::Scan::global(
8538 Symbol_table* symtab,
8539 Layout* layout,
8540 Target_powerpc<size, big_endian>* target,
8541 Sized_relobj_file<size, big_endian>* object,
8542 unsigned int data_shndx,
8543 Output_section* output_section,
8544 const elfcpp::Rela<size, big_endian>& reloc,
8545 unsigned int r_type,
8546 Symbol* gsym)
8548 if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8549 == Track_tls::SKIP)
8550 return;
8552 if (target->replace_tls_get_addr(gsym))
8553 // Change a __tls_get_addr reference to __tls_get_addr_opt
8554 // so dynamic relocs are emitted against the latter symbol.
8555 gsym = target->tls_get_addr_opt();
8557 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8558 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8560 this->expect_tls_get_addr_call();
8561 const bool final = gsym->final_value_is_known();
8562 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8563 if (tls_type != tls::TLSOPT_NONE)
8564 this->skip_next_tls_get_addr_call();
8566 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8567 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8569 this->expect_tls_get_addr_call();
8570 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8571 if (tls_type != tls::TLSOPT_NONE)
8572 this->skip_next_tls_get_addr_call();
8575 Powerpc_relobj<size, big_endian>* ppc_object
8576 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8578 // A STT_GNU_IFUNC symbol may require a PLT entry.
8579 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8580 bool pushed_ifunc = false;
8581 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8583 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8584 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8585 r_type, r_sym, reloc.get_r_addend());
8586 target->make_plt_entry(symtab, layout, gsym);
8587 pushed_ifunc = true;
8590 switch (r_type)
8592 case elfcpp::R_POWERPC_NONE:
8593 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8594 case elfcpp::R_POWERPC_GNU_VTENTRY:
8595 case elfcpp::R_PPC_LOCAL24PC:
8596 case elfcpp::R_POWERPC_TLS:
8597 case elfcpp::R_PPC64_ENTRY:
8598 case elfcpp::R_POWERPC_PLTSEQ:
8599 case elfcpp::R_POWERPC_PLTCALL:
8600 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8601 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8602 case elfcpp::R_PPC64_PCREL_OPT:
8603 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8604 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8605 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8606 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8607 case elfcpp::R_PPC64_REL16_HIGHER34:
8608 case elfcpp::R_PPC64_REL16_HIGHERA34:
8609 case elfcpp::R_PPC64_REL16_HIGHEST34:
8610 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8611 case elfcpp::R_PPC64_D34:
8612 case elfcpp::R_PPC64_D34_LO:
8613 case elfcpp::R_PPC64_D34_HI30:
8614 case elfcpp::R_PPC64_D34_HA30:
8615 case elfcpp::R_PPC64_D28:
8616 case elfcpp::R_PPC64_PCREL34:
8617 case elfcpp::R_PPC64_PCREL28:
8618 case elfcpp::R_PPC64_TPREL34:
8619 case elfcpp::R_PPC64_DTPREL34:
8620 break;
8622 case elfcpp::R_PPC64_TOC:
8624 Output_data_got_powerpc<size, big_endian>* got
8625 = target->got_section(symtab, layout);
8626 if (parameters->options().output_is_position_independent())
8628 Address off = reloc.get_r_offset();
8629 if (size == 64
8630 && data_shndx == ppc_object->opd_shndx()
8631 && ppc_object->get_opd_discard(off - 8))
8632 break;
8634 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8635 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8636 if (data_shndx != ppc_object->opd_shndx())
8637 symobj = static_cast
8638 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8639 rela_dyn->add_output_section_relative(got->output_section(),
8640 elfcpp::R_POWERPC_RELATIVE,
8641 output_section,
8642 object, data_shndx, off,
8643 symobj->toc_base_offset());
8646 break;
8648 case elfcpp::R_PPC64_ADDR64:
8649 if (size == 64
8650 && target->abiversion() < 2
8651 && data_shndx == ppc_object->opd_shndx()
8652 && (gsym->is_defined_in_discarded_section()
8653 || gsym->object() != object))
8655 ppc_object->set_opd_discard(reloc.get_r_offset());
8656 break;
8658 // Fall through.
8659 case elfcpp::R_PPC64_UADDR64:
8660 case elfcpp::R_POWERPC_ADDR32:
8661 case elfcpp::R_POWERPC_UADDR32:
8662 case elfcpp::R_POWERPC_ADDR24:
8663 case elfcpp::R_POWERPC_ADDR16:
8664 case elfcpp::R_POWERPC_ADDR16_LO:
8665 case elfcpp::R_POWERPC_ADDR16_HI:
8666 case elfcpp::R_POWERPC_ADDR16_HA:
8667 case elfcpp::R_POWERPC_UADDR16:
8668 case elfcpp::R_PPC64_ADDR16_HIGH:
8669 case elfcpp::R_PPC64_ADDR16_HIGHA:
8670 case elfcpp::R_PPC64_ADDR16_HIGHER:
8671 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8672 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8673 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8674 case elfcpp::R_PPC64_ADDR16_DS:
8675 case elfcpp::R_PPC64_ADDR16_LO_DS:
8676 case elfcpp::R_POWERPC_ADDR14:
8677 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8678 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8680 // Make a PLT entry if necessary.
8681 if (gsym->needs_plt_entry())
8683 // Since this is not a PC-relative relocation, we may be
8684 // taking the address of a function. In that case we need to
8685 // set the entry in the dynamic symbol table to the address of
8686 // the PLT call stub.
8687 bool need_ifunc_plt = false;
8688 if ((size == 32 || target->abiversion() >= 2)
8689 && gsym->is_from_dynobj()
8690 && !parameters->options().output_is_position_independent())
8692 gsym->set_needs_dynsym_value();
8693 need_ifunc_plt = true;
8695 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8697 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8698 target->push_branch(ppc_object, data_shndx,
8699 reloc.get_r_offset(), r_type, r_sym,
8700 reloc.get_r_addend());
8701 target->make_plt_entry(symtab, layout, gsym);
8704 // Make a dynamic relocation if necessary.
8705 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8706 || (size == 64 && is_ifunc && target->abiversion() < 2))
8708 if (!parameters->options().output_is_position_independent()
8709 && gsym->may_need_copy_reloc())
8711 target->copy_reloc(symtab, layout, object,
8712 data_shndx, output_section, gsym, reloc);
8714 else if ((((size == 32
8715 && r_type == elfcpp::R_POWERPC_ADDR32)
8716 || (size == 64
8717 && r_type == elfcpp::R_PPC64_ADDR64
8718 && target->abiversion() >= 2))
8719 && gsym->can_use_relative_reloc(false)
8720 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8721 && parameters->options().shared()))
8722 || (size == 64
8723 && r_type == elfcpp::R_PPC64_ADDR64
8724 && target->abiversion() < 2
8725 && (gsym->can_use_relative_reloc(false)
8726 || data_shndx == ppc_object->opd_shndx())))
8728 Reloc_section* rela_dyn
8729 = target->rela_dyn_section(symtab, layout, is_ifunc);
8730 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8731 : elfcpp::R_POWERPC_RELATIVE);
8732 rela_dyn->add_symbolless_global_addend(
8733 gsym, dynrel, output_section, object, data_shndx,
8734 reloc.get_r_offset(), reloc.get_r_addend());
8736 else
8738 Reloc_section* rela_dyn
8739 = target->rela_dyn_section(symtab, layout, is_ifunc);
8740 check_non_pic(object, r_type);
8741 rela_dyn->add_global(gsym, r_type, output_section,
8742 object, data_shndx,
8743 reloc.get_r_offset(),
8744 reloc.get_r_addend());
8746 if (size == 64
8747 && parameters->options().toc_optimize()
8748 && data_shndx == ppc_object->toc_shndx())
8749 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8753 break;
8755 case elfcpp::R_PPC64_PLT_PCREL34:
8756 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8757 case elfcpp::R_POWERPC_PLT16_LO:
8758 case elfcpp::R_POWERPC_PLT16_HI:
8759 case elfcpp::R_POWERPC_PLT16_HA:
8760 case elfcpp::R_PPC64_PLT16_LO_DS:
8761 if (!pushed_ifunc)
8762 target->make_plt_entry(symtab, layout, gsym);
8763 break;
8765 case elfcpp::R_PPC64_REL24_NOTOC:
8766 if (size == 32)
8767 break;
8768 // Fall through.
8769 case elfcpp::R_PPC_PLTREL24:
8770 case elfcpp::R_POWERPC_REL24:
8771 if (!is_ifunc)
8773 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8774 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8775 r_type, r_sym, reloc.get_r_addend());
8776 if (gsym->needs_plt_entry()
8777 || (!gsym->final_value_is_known()
8778 && (gsym->is_undefined()
8779 || gsym->is_from_dynobj()
8780 || gsym->is_preemptible())))
8781 target->make_plt_entry(symtab, layout, gsym);
8783 // Fall through.
8785 case elfcpp::R_PPC64_REL64:
8786 case elfcpp::R_POWERPC_REL32:
8787 // Make a dynamic relocation if necessary.
8788 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8790 if (!parameters->options().output_is_position_independent()
8791 && gsym->may_need_copy_reloc())
8793 target->copy_reloc(symtab, layout, object,
8794 data_shndx, output_section, gsym,
8795 reloc);
8797 else
8799 Reloc_section* rela_dyn
8800 = target->rela_dyn_section(symtab, layout, is_ifunc);
8801 check_non_pic(object, r_type);
8802 rela_dyn->add_global(gsym, r_type, output_section, object,
8803 data_shndx, reloc.get_r_offset(),
8804 reloc.get_r_addend());
8807 break;
8809 case elfcpp::R_POWERPC_REL14:
8810 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8811 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8812 if (!is_ifunc)
8814 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8815 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8816 r_type, r_sym, reloc.get_r_addend());
8818 break;
8820 case elfcpp::R_PPC64_TOCSAVE:
8821 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8822 // caller has already saved r2 and thus a plt call stub need not
8823 // save r2.
8824 if (size == 64
8825 && target->mark_pltcall(ppc_object, data_shndx,
8826 reloc.get_r_offset() - 4, symtab))
8828 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8829 bool is_ordinary;
8830 unsigned int shndx = gsym->shndx(&is_ordinary);
8831 if (!is_ordinary)
8832 object->error(_("tocsave symbol %u has bad shndx %u"),
8833 r_sym, shndx);
8834 else
8836 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8837 target->add_tocsave(ppc_object, shndx,
8838 sym->value() + reloc.get_r_addend());
8841 break;
8843 case elfcpp::R_POWERPC_REL16:
8844 case elfcpp::R_POWERPC_REL16_LO:
8845 case elfcpp::R_POWERPC_REL16_HI:
8846 case elfcpp::R_POWERPC_REL16_HA:
8847 case elfcpp::R_POWERPC_REL16DX_HA:
8848 case elfcpp::R_PPC64_REL16_HIGH:
8849 case elfcpp::R_PPC64_REL16_HIGHA:
8850 case elfcpp::R_PPC64_REL16_HIGHER:
8851 case elfcpp::R_PPC64_REL16_HIGHERA:
8852 case elfcpp::R_PPC64_REL16_HIGHEST:
8853 case elfcpp::R_PPC64_REL16_HIGHESTA:
8854 case elfcpp::R_POWERPC_SECTOFF:
8855 case elfcpp::R_POWERPC_SECTOFF_LO:
8856 case elfcpp::R_POWERPC_SECTOFF_HI:
8857 case elfcpp::R_POWERPC_SECTOFF_HA:
8858 case elfcpp::R_PPC64_SECTOFF_DS:
8859 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8860 case elfcpp::R_POWERPC_TPREL16:
8861 case elfcpp::R_POWERPC_TPREL16_LO:
8862 case elfcpp::R_POWERPC_TPREL16_HI:
8863 case elfcpp::R_POWERPC_TPREL16_HA:
8864 case elfcpp::R_PPC64_TPREL16_DS:
8865 case elfcpp::R_PPC64_TPREL16_LO_DS:
8866 case elfcpp::R_PPC64_TPREL16_HIGH:
8867 case elfcpp::R_PPC64_TPREL16_HIGHA:
8868 case elfcpp::R_PPC64_TPREL16_HIGHER:
8869 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8870 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8871 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8872 case elfcpp::R_POWERPC_DTPREL16:
8873 case elfcpp::R_POWERPC_DTPREL16_LO:
8874 case elfcpp::R_POWERPC_DTPREL16_HI:
8875 case elfcpp::R_POWERPC_DTPREL16_HA:
8876 case elfcpp::R_PPC64_DTPREL16_DS:
8877 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8878 case elfcpp::R_PPC64_DTPREL16_HIGH:
8879 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8880 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8881 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8882 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8883 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8884 case elfcpp::R_PPC64_TLSGD:
8885 case elfcpp::R_PPC64_TLSLD:
8886 case elfcpp::R_PPC64_ADDR64_LOCAL:
8887 break;
8889 case elfcpp::R_PPC64_GOT_PCREL34:
8890 case elfcpp::R_POWERPC_GOT16:
8891 case elfcpp::R_POWERPC_GOT16_LO:
8892 case elfcpp::R_POWERPC_GOT16_HI:
8893 case elfcpp::R_POWERPC_GOT16_HA:
8894 case elfcpp::R_PPC64_GOT16_DS:
8895 case elfcpp::R_PPC64_GOT16_LO_DS:
8897 // The symbol requires a GOT entry.
8898 Output_data_got_powerpc<size, big_endian>* got;
8900 got = target->got_section(symtab, layout);
8901 if (gsym->final_value_is_known())
8903 if (is_ifunc
8904 && (size == 32 || target->abiversion() >= 2))
8905 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8906 else
8907 got->add_global(gsym, GOT_TYPE_STANDARD);
8909 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8911 // If we are generating a shared object or a pie, this
8912 // symbol's GOT entry will be set by a dynamic relocation.
8913 unsigned int off = got->add_constant(0);
8914 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8916 Reloc_section* rela_dyn
8917 = target->rela_dyn_section(symtab, layout, is_ifunc);
8919 if (gsym->can_use_relative_reloc(false)
8920 && !((size == 32
8921 || target->abiversion() >= 2)
8922 && gsym->visibility() == elfcpp::STV_PROTECTED
8923 && parameters->options().shared()))
8925 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8926 : elfcpp::R_POWERPC_RELATIVE);
8927 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8929 else
8931 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8932 rela_dyn->add_global(gsym, dynrel, got, off, 0);
8936 break;
8938 case elfcpp::R_PPC64_TOC16:
8939 case elfcpp::R_PPC64_TOC16_LO:
8940 case elfcpp::R_PPC64_TOC16_HI:
8941 case elfcpp::R_PPC64_TOC16_HA:
8942 case elfcpp::R_PPC64_TOC16_DS:
8943 case elfcpp::R_PPC64_TOC16_LO_DS:
8944 // We need a GOT section.
8945 target->got_section(symtab, layout);
8946 break;
8948 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8949 case elfcpp::R_POWERPC_GOT_TLSGD16:
8950 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8951 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8952 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8954 const bool final = gsym->final_value_is_known();
8955 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8956 if (tls_type == tls::TLSOPT_NONE)
8958 Output_data_got_powerpc<size, big_endian>* got
8959 = target->got_section(symtab, layout);
8960 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8961 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
8962 elfcpp::R_POWERPC_DTPMOD,
8963 elfcpp::R_POWERPC_DTPREL);
8965 else if (tls_type == tls::TLSOPT_TO_IE)
8967 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8969 Output_data_got_powerpc<size, big_endian>* got
8970 = target->got_section(symtab, layout);
8971 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8972 if (gsym->is_undefined()
8973 || gsym->is_from_dynobj())
8975 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8976 elfcpp::R_POWERPC_TPREL);
8978 else
8980 unsigned int off = got->add_constant(0);
8981 gsym->set_got_offset(GOT_TYPE_TPREL, off);
8982 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8983 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8984 got, off, 0);
8988 else if (tls_type == tls::TLSOPT_TO_LE)
8990 // no GOT relocs needed for Local Exec.
8992 else
8993 gold_unreachable();
8995 break;
8997 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8998 case elfcpp::R_POWERPC_GOT_TLSLD16:
8999 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9000 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9001 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9003 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
9004 if (tls_type == tls::TLSOPT_NONE)
9005 target->tlsld_got_offset(symtab, layout, object);
9006 else if (tls_type == tls::TLSOPT_TO_LE)
9008 // no GOT relocs needed for Local Exec.
9009 if (parameters->options().emit_relocs())
9011 Output_section* os = layout->tls_segment()->first_section();
9012 gold_assert(os != NULL);
9013 os->set_needs_symtab_index();
9016 else
9017 gold_unreachable();
9019 break;
9021 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9022 case elfcpp::R_POWERPC_GOT_DTPREL16:
9023 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9024 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9025 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9027 Output_data_got_powerpc<size, big_endian>* got
9028 = target->got_section(symtab, layout);
9029 if (!gsym->final_value_is_known()
9030 && (gsym->is_from_dynobj()
9031 || gsym->is_undefined()
9032 || gsym->is_preemptible()))
9033 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
9034 target->rela_dyn_section(layout),
9035 elfcpp::R_POWERPC_DTPREL);
9036 else
9037 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
9039 break;
9041 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9042 case elfcpp::R_POWERPC_GOT_TPREL16:
9043 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9044 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9045 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9047 const bool final = gsym->final_value_is_known();
9048 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9049 if (tls_type == tls::TLSOPT_NONE)
9051 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
9053 Output_data_got_powerpc<size, big_endian>* got
9054 = target->got_section(symtab, layout);
9055 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9056 if (gsym->is_undefined()
9057 || gsym->is_from_dynobj())
9059 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
9060 elfcpp::R_POWERPC_TPREL);
9062 else
9064 unsigned int off = got->add_constant(0);
9065 gsym->set_got_offset(GOT_TYPE_TPREL, off);
9066 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9067 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9068 got, off, 0);
9072 else if (tls_type == tls::TLSOPT_TO_LE)
9074 // no GOT relocs needed for Local Exec.
9076 else
9077 gold_unreachable();
9079 break;
9081 default:
9082 unsupported_reloc_global(object, r_type, gsym);
9083 break;
9086 if (size == 64
9087 && parameters->options().toc_optimize())
9089 if (data_shndx == ppc_object->toc_shndx())
9091 bool ok = true;
9092 if (r_type != elfcpp::R_PPC64_ADDR64
9093 || (is_ifunc && target->abiversion() < 2))
9094 ok = false;
9095 else if (parameters->options().output_is_position_independent()
9096 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9097 ok = false;
9098 if (!ok)
9099 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9102 enum {no_check, check_lo, check_ha} insn_check;
9103 switch (r_type)
9105 default:
9106 insn_check = no_check;
9107 break;
9109 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9110 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9111 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9112 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9113 case elfcpp::R_POWERPC_GOT16_HA:
9114 case elfcpp::R_PPC64_TOC16_HA:
9115 insn_check = check_ha;
9116 break;
9118 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9119 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9120 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9121 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9122 case elfcpp::R_POWERPC_GOT16_LO:
9123 case elfcpp::R_PPC64_GOT16_LO_DS:
9124 case elfcpp::R_PPC64_TOC16_LO:
9125 case elfcpp::R_PPC64_TOC16_LO_DS:
9126 insn_check = check_lo;
9127 break;
9130 section_size_type slen;
9131 const unsigned char* view = NULL;
9132 if (insn_check != no_check)
9134 view = ppc_object->section_contents(data_shndx, &slen, false);
9135 section_size_type off =
9136 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9137 if (off < slen)
9139 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9140 if (insn_check == check_lo
9141 ? !ok_lo_toc_insn(insn, r_type)
9142 : ((insn & ((0x3f << 26) | 0x1f << 16))
9143 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9145 ppc_object->set_no_toc_opt();
9146 gold_warning(_("%s: toc optimization is not supported "
9147 "for %#08x instruction"),
9148 ppc_object->name().c_str(), insn);
9153 switch (r_type)
9155 default:
9156 break;
9157 case elfcpp::R_PPC64_TOC16:
9158 case elfcpp::R_PPC64_TOC16_LO:
9159 case elfcpp::R_PPC64_TOC16_HI:
9160 case elfcpp::R_PPC64_TOC16_HA:
9161 case elfcpp::R_PPC64_TOC16_DS:
9162 case elfcpp::R_PPC64_TOC16_LO_DS:
9163 if (gsym->source() == Symbol::FROM_OBJECT
9164 && !gsym->object()->is_dynamic())
9166 Powerpc_relobj<size, big_endian>* sym_object
9167 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9168 bool is_ordinary;
9169 unsigned int shndx = gsym->shndx(&is_ordinary);
9170 if (shndx == sym_object->toc_shndx())
9172 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9173 Address dst_off = sym->value() + reloc.get_r_addend();
9174 if (dst_off < sym_object->section_size(shndx))
9176 bool ok = false;
9177 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9178 ok = true;
9179 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9181 // Need to check that the insn is a ld
9182 if (!view)
9183 view = ppc_object->section_contents(data_shndx,
9184 &slen,
9185 false);
9186 section_size_type off =
9187 (convert_to_section_size_type(reloc.get_r_offset())
9188 + (big_endian ? -2 : 3));
9189 if (off < slen
9190 && (view[off] & (0x3f << 2)) == (58u << 2))
9191 ok = true;
9193 if (!ok)
9194 sym_object->set_no_toc_opt(dst_off);
9198 break;
9202 if (size == 32)
9204 switch (r_type)
9206 case elfcpp::R_PPC_LOCAL24PC:
9207 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9208 gold_error(_("%s: unsupported -mbss-plt code"),
9209 ppc_object->name().c_str());
9210 break;
9211 default:
9212 break;
9216 switch (r_type)
9218 case elfcpp::R_POWERPC_GOT_TLSLD16:
9219 case elfcpp::R_POWERPC_GOT_TLSGD16:
9220 case elfcpp::R_POWERPC_GOT_TPREL16:
9221 case elfcpp::R_POWERPC_GOT_DTPREL16:
9222 case elfcpp::R_POWERPC_GOT16:
9223 case elfcpp::R_PPC64_GOT16_DS:
9224 case elfcpp::R_PPC64_TOC16:
9225 case elfcpp::R_PPC64_TOC16_DS:
9226 ppc_object->set_has_small_toc_reloc();
9227 break;
9228 default:
9229 break;
9232 switch (r_type)
9234 case elfcpp::R_PPC64_TPREL16_DS:
9235 case elfcpp::R_PPC64_TPREL16_LO_DS:
9236 case elfcpp::R_PPC64_TPREL16_HIGH:
9237 case elfcpp::R_PPC64_TPREL16_HIGHA:
9238 case elfcpp::R_PPC64_TPREL16_HIGHER:
9239 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9240 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9241 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9242 case elfcpp::R_PPC64_TPREL34:
9243 if (size != 64)
9244 break;
9245 // Fall through.
9246 case elfcpp::R_POWERPC_TPREL16:
9247 case elfcpp::R_POWERPC_TPREL16_LO:
9248 case elfcpp::R_POWERPC_TPREL16_HI:
9249 case elfcpp::R_POWERPC_TPREL16_HA:
9250 layout->set_has_static_tls();
9251 break;
9252 default:
9253 break;
9256 switch (r_type)
9258 case elfcpp::R_POWERPC_TPREL16_HA:
9259 if (target->tprel_opt())
9261 section_size_type slen;
9262 const unsigned char* view = NULL;
9263 view = ppc_object->section_contents(data_shndx, &slen, false);
9264 section_size_type off
9265 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9266 if (off < slen)
9268 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9269 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9270 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9271 target->set_tprel_opt(false);
9274 break;
9276 case elfcpp::R_PPC64_TPREL16_HIGH:
9277 case elfcpp::R_PPC64_TPREL16_HIGHA:
9278 case elfcpp::R_PPC64_TPREL16_HIGHER:
9279 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9280 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9281 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9282 if (size != 64)
9283 break;
9284 // Fall through.
9285 case elfcpp::R_POWERPC_TPREL16_HI:
9286 target->set_tprel_opt(false);
9287 break;
9288 default:
9289 break;
9292 switch (r_type)
9294 case elfcpp::R_PPC64_D34:
9295 case elfcpp::R_PPC64_D34_LO:
9296 case elfcpp::R_PPC64_D34_HI30:
9297 case elfcpp::R_PPC64_D34_HA30:
9298 case elfcpp::R_PPC64_D28:
9299 case elfcpp::R_PPC64_PCREL34:
9300 case elfcpp::R_PPC64_PCREL28:
9301 case elfcpp::R_PPC64_TPREL34:
9302 case elfcpp::R_PPC64_DTPREL34:
9303 case elfcpp::R_PPC64_PLT_PCREL34:
9304 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9305 case elfcpp::R_PPC64_GOT_PCREL34:
9306 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9307 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9308 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9309 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9310 target->set_power10_relocs();
9311 break;
9312 default:
9313 break;
9317 // Process relocations for gc.
9319 template<int size, bool big_endian>
9320 void
9321 Target_powerpc<size, big_endian>::gc_process_relocs(
9322 Symbol_table* symtab,
9323 Layout* layout,
9324 Sized_relobj_file<size, big_endian>* object,
9325 unsigned int data_shndx,
9326 unsigned int,
9327 const unsigned char* prelocs,
9328 size_t reloc_count,
9329 Output_section* output_section,
9330 bool needs_special_offset_handling,
9331 size_t local_symbol_count,
9332 const unsigned char* plocal_symbols)
9334 typedef Target_powerpc<size, big_endian> Powerpc;
9335 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9336 Classify_reloc;
9338 Powerpc_relobj<size, big_endian>* ppc_object
9339 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9340 if (size == 64)
9341 ppc_object->set_opd_valid();
9342 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9344 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9345 for (p = ppc_object->access_from_map()->begin();
9346 p != ppc_object->access_from_map()->end();
9347 ++p)
9349 Address dst_off = p->first;
9350 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9351 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9352 for (s = p->second.begin(); s != p->second.end(); ++s)
9354 Relobj* src_obj = s->first;
9355 unsigned int src_indx = s->second;
9356 symtab->gc()->add_reference(src_obj, src_indx,
9357 ppc_object, dst_indx);
9359 p->second.clear();
9361 ppc_object->access_from_map()->clear();
9362 ppc_object->process_gc_mark(symtab);
9363 // Don't look at .opd relocs as .opd will reference everything.
9364 return;
9367 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9368 symtab,
9369 layout,
9370 this,
9371 object,
9372 data_shndx,
9373 prelocs,
9374 reloc_count,
9375 output_section,
9376 needs_special_offset_handling,
9377 local_symbol_count,
9378 plocal_symbols);
9381 // Handle target specific gc actions when adding a gc reference from
9382 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9383 // and DST_OFF. For powerpc64, this adds a referenc to the code
9384 // section of a function descriptor.
9386 template<int size, bool big_endian>
9387 void
9388 Target_powerpc<size, big_endian>::do_gc_add_reference(
9389 Symbol_table* symtab,
9390 Relobj* src_obj,
9391 unsigned int src_shndx,
9392 Relobj* dst_obj,
9393 unsigned int dst_shndx,
9394 Address dst_off) const
9396 if (size != 64 || dst_obj->is_dynamic())
9397 return;
9399 Powerpc_relobj<size, big_endian>* ppc_object
9400 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9401 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9403 if (ppc_object->opd_valid())
9405 dst_shndx = ppc_object->get_opd_ent(dst_off);
9406 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9408 else
9410 // If we haven't run scan_opd_relocs, we must delay
9411 // processing this function descriptor reference.
9412 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9417 // Add any special sections for this symbol to the gc work list.
9418 // For powerpc64, this adds the code section of a function
9419 // descriptor.
9421 template<int size, bool big_endian>
9422 void
9423 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9424 Symbol_table* symtab,
9425 Symbol* sym) const
9427 if (size == 64 && sym->object()->pluginobj() == NULL)
9429 Powerpc_relobj<size, big_endian>* ppc_object
9430 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9431 bool is_ordinary;
9432 unsigned int shndx = sym->shndx(&is_ordinary);
9433 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9435 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9436 Address dst_off = gsym->value();
9437 if (ppc_object->opd_valid())
9439 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9440 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9441 dst_indx));
9443 else
9444 ppc_object->add_gc_mark(dst_off);
9449 // For a symbol location in .opd, set LOC to the location of the
9450 // function entry.
9452 template<int size, bool big_endian>
9453 void
9454 Target_powerpc<size, big_endian>::do_function_location(
9455 Symbol_location* loc) const
9457 if (size == 64 && loc->shndx != 0)
9459 if (loc->object->is_dynamic())
9461 Powerpc_dynobj<size, big_endian>* ppc_object
9462 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9463 if (loc->shndx == ppc_object->opd_shndx())
9465 Address dest_off;
9466 Address off = loc->offset - ppc_object->opd_address();
9467 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9468 loc->offset = dest_off;
9471 else
9473 const Powerpc_relobj<size, big_endian>* ppc_object
9474 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9475 if (loc->shndx == ppc_object->opd_shndx())
9477 Address dest_off;
9478 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9479 loc->offset = dest_off;
9485 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9486 // compiled with -fsplit-stack. The function calls non-split-stack
9487 // code. Change the function to ensure it has enough stack space to
9488 // call some random function.
9490 template<int size, bool big_endian>
9491 void
9492 Target_powerpc<size, big_endian>::do_calls_non_split(
9493 Relobj* object,
9494 unsigned int shndx,
9495 section_offset_type fnoffset,
9496 section_size_type fnsize,
9497 const unsigned char* prelocs,
9498 size_t reloc_count,
9499 unsigned char* view,
9500 section_size_type view_size,
9501 std::string* from,
9502 std::string* to) const
9504 // 32-bit not supported.
9505 if (size == 32)
9507 // warn
9508 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9509 prelocs, reloc_count, view, view_size,
9510 from, to);
9511 return;
9514 // The function always starts with
9515 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9516 // addis %r12,%r1,-allocate@ha
9517 // addi %r12,%r12,-allocate@l
9518 // cmpld %r12,%r0
9519 // but note that the addis or addi may be replaced with a nop
9521 unsigned char *entry = view + fnoffset;
9522 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9524 if ((insn & 0xffff0000) == addis_2_12)
9526 /* Skip ELFv2 global entry code. */
9527 entry += 8;
9528 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9531 unsigned char *pinsn = entry;
9532 bool ok = false;
9533 const uint32_t ld_private_ss = 0xe80d8fc0;
9534 if (insn == ld_private_ss)
9536 int32_t allocate = 0;
9537 while (1)
9539 pinsn += 4;
9540 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9541 if ((insn & 0xffff0000) == addis_12_1)
9542 allocate += (insn & 0xffff) << 16;
9543 else if ((insn & 0xffff0000) == addi_12_1
9544 || (insn & 0xffff0000) == addi_12_12)
9545 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9546 else if (insn != nop)
9547 break;
9549 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9551 int extra = parameters->options().split_stack_adjust_size();
9552 allocate -= extra;
9553 if (allocate >= 0 || extra < 0)
9555 object->error(_("split-stack stack size overflow at "
9556 "section %u offset %0zx"),
9557 shndx, static_cast<size_t>(fnoffset));
9558 return;
9560 pinsn = entry + 4;
9561 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9562 if (insn != addis_12_1)
9564 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9565 pinsn += 4;
9566 insn = addi_12_12 | (allocate & 0xffff);
9567 if (insn != addi_12_12)
9569 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9570 pinsn += 4;
9573 else
9575 insn = addi_12_1 | (allocate & 0xffff);
9576 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9577 pinsn += 4;
9579 if (pinsn != entry + 12)
9580 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9582 ok = true;
9586 if (!ok)
9588 if (!object->has_no_split_stack())
9589 object->error(_("failed to match split-stack sequence at "
9590 "section %u offset %0zx"),
9591 shndx, static_cast<size_t>(fnoffset));
9595 // Scan relocations for a section.
9597 template<int size, bool big_endian>
9598 void
9599 Target_powerpc<size, big_endian>::scan_relocs(
9600 Symbol_table* symtab,
9601 Layout* layout,
9602 Sized_relobj_file<size, big_endian>* object,
9603 unsigned int data_shndx,
9604 unsigned int sh_type,
9605 const unsigned char* prelocs,
9606 size_t reloc_count,
9607 Output_section* output_section,
9608 bool needs_special_offset_handling,
9609 size_t local_symbol_count,
9610 const unsigned char* plocal_symbols)
9612 typedef Target_powerpc<size, big_endian> Powerpc;
9613 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9614 Classify_reloc;
9616 if (!this->plt_localentry0_init_)
9618 bool plt_localentry0 = false;
9619 if (size == 64
9620 && this->abiversion() >= 2)
9622 if (parameters->options().user_set_plt_localentry())
9623 plt_localentry0 = parameters->options().plt_localentry();
9624 if (plt_localentry0
9625 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9626 gold_warning(_("--plt-localentry is especially dangerous without "
9627 "ld.so support to detect ABI violations"));
9629 this->plt_localentry0_ = plt_localentry0;
9630 this->plt_localentry0_init_ = true;
9633 if (sh_type == elfcpp::SHT_REL)
9635 gold_error(_("%s: unsupported REL reloc section"),
9636 object->name().c_str());
9637 return;
9640 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9641 symtab,
9642 layout,
9643 this,
9644 object,
9645 data_shndx,
9646 prelocs,
9647 reloc_count,
9648 output_section,
9649 needs_special_offset_handling,
9650 local_symbol_count,
9651 plocal_symbols);
9653 if (this->plt_localentry0_ && this->power10_relocs_)
9655 gold_warning(_("--plt-localentry is incompatible with "
9656 "power10 pc-relative code"));
9657 this->plt_localentry0_ = false;
9661 // Functor class for processing the global symbol table.
9662 // Removes symbols defined on discarded opd entries.
9664 template<bool big_endian>
9665 class Global_symbol_visitor_opd
9667 public:
9668 Global_symbol_visitor_opd()
9671 void
9672 operator()(Sized_symbol<64>* sym)
9674 if (sym->has_symtab_index()
9675 || sym->source() != Symbol::FROM_OBJECT
9676 || !sym->in_real_elf())
9677 return;
9679 if (sym->object()->is_dynamic())
9680 return;
9682 Powerpc_relobj<64, big_endian>* symobj
9683 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9684 if (symobj->opd_shndx() == 0)
9685 return;
9687 bool is_ordinary;
9688 unsigned int shndx = sym->shndx(&is_ordinary);
9689 if (shndx == symobj->opd_shndx()
9690 && symobj->get_opd_discard(sym->value()))
9692 sym->set_undefined();
9693 sym->set_visibility(elfcpp::STV_DEFAULT);
9694 sym->set_is_defined_in_discarded_section();
9695 sym->set_symtab_index(-1U);
9700 template<int size, bool big_endian>
9701 void
9702 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9703 Layout* layout,
9704 Symbol_table* symtab)
9706 if (size == 64)
9708 Output_data_save_res<size, big_endian>* savres
9709 = new Output_data_save_res<size, big_endian>(symtab);
9710 this->savres_section_ = savres;
9711 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9712 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9713 savres, ORDER_TEXT, false);
9717 // Sort linker created .got section first (for the header), then input
9718 // sections belonging to files using small model code.
9720 template<bool big_endian>
9721 class Sort_toc_sections
9723 public:
9724 bool
9725 operator()(const Output_section::Input_section& is1,
9726 const Output_section::Input_section& is2) const
9728 if (!is1.is_input_section() && is2.is_input_section())
9729 return true;
9730 bool small1
9731 = (is1.is_input_section()
9732 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9733 ->has_small_toc_reloc()));
9734 bool small2
9735 = (is2.is_input_section()
9736 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9737 ->has_small_toc_reloc()));
9738 return small1 && !small2;
9742 // Finalize the sections.
9744 template<int size, bool big_endian>
9745 void
9746 Target_powerpc<size, big_endian>::do_finalize_sections(
9747 Layout* layout,
9748 const Input_objects* input_objects,
9749 Symbol_table* symtab)
9751 if (parameters->doing_static_link())
9753 // At least some versions of glibc elf-init.o have a strong
9754 // reference to __rela_iplt marker syms. A weak ref would be
9755 // better..
9756 if (this->iplt_ != NULL)
9758 Reloc_section* rel = this->iplt_->rel_plt();
9759 symtab->define_in_output_data("__rela_iplt_start", NULL,
9760 Symbol_table::PREDEFINED, rel, 0, 0,
9761 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9762 elfcpp::STV_HIDDEN, 0, false, true);
9763 symtab->define_in_output_data("__rela_iplt_end", NULL,
9764 Symbol_table::PREDEFINED, rel, 0, 0,
9765 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9766 elfcpp::STV_HIDDEN, 0, true, true);
9768 else
9770 symtab->define_as_constant("__rela_iplt_start", NULL,
9771 Symbol_table::PREDEFINED, 0, 0,
9772 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9773 elfcpp::STV_HIDDEN, 0, true, false);
9774 symtab->define_as_constant("__rela_iplt_end", NULL,
9775 Symbol_table::PREDEFINED, 0, 0,
9776 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9777 elfcpp::STV_HIDDEN, 0, true, false);
9781 if (size == 64)
9783 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9784 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
9786 if (!parameters->options().relocatable())
9788 this->define_save_restore_funcs(layout, symtab);
9790 // Annoyingly, we need to make these sections now whether or
9791 // not we need them. If we delay until do_relax then we
9792 // need to mess with the relaxation machinery checkpointing.
9793 this->got_section(symtab, layout);
9794 this->make_brlt_section(layout);
9796 if (parameters->options().toc_sort())
9798 Output_section* os = this->got_->output_section();
9799 if (os != NULL && os->input_sections().size() > 1)
9800 std::stable_sort(os->input_sections().begin(),
9801 os->input_sections().end(),
9802 Sort_toc_sections<big_endian>());
9807 // Fill in some more dynamic tags.
9808 Output_data_dynamic* odyn = layout->dynamic_data();
9809 if (odyn != NULL)
9811 const Reloc_section* rel_plt = (this->plt_ == NULL
9812 ? NULL
9813 : this->plt_->rel_plt());
9814 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9815 this->rela_dyn_, true, size == 32);
9817 if (size == 32)
9819 if (this->got_ != NULL)
9821 this->got_->finalize_data_size();
9822 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9823 this->got_, this->got_->g_o_t());
9825 if (this->has_tls_get_addr_opt_)
9826 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
9828 else
9830 if (this->glink_ != NULL)
9832 this->glink_->finalize_data_size();
9833 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9834 this->glink_,
9835 (this->glink_->pltresolve_size()
9836 - 32));
9838 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9839 odyn->add_constant(elfcpp::DT_PPC64_OPT,
9840 ((this->has_localentry0_
9841 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9842 | (this->has_tls_get_addr_opt_
9843 ? elfcpp::PPC64_OPT_TLS : 0)));
9847 // Emit any relocs we saved in an attempt to avoid generating COPY
9848 // relocs.
9849 if (this->copy_relocs_.any_saved_relocs())
9850 this->copy_relocs_.emit(this->rela_dyn_section(layout));
9852 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9853 p != input_objects->relobj_end();
9854 ++p)
9856 Powerpc_relobj<size, big_endian>* ppc_relobj
9857 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9858 if (ppc_relobj->attributes_section_data())
9859 this->merge_object_attributes(ppc_relobj,
9860 ppc_relobj->attributes_section_data());
9862 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9863 p != input_objects->dynobj_end();
9864 ++p)
9866 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9867 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9868 if (ppc_dynobj->attributes_section_data())
9869 this->merge_object_attributes(ppc_dynobj,
9870 ppc_dynobj->attributes_section_data());
9873 // Create a .gnu.attributes section if we have merged any attributes
9874 // from inputs.
9875 if (this->attributes_section_data_ != NULL
9876 && this->attributes_section_data_->size() != 0)
9878 Output_attributes_section_data* attributes_section
9879 = new Output_attributes_section_data(*this->attributes_section_data_);
9880 layout->add_output_section_data(".gnu.attributes",
9881 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9882 attributes_section, ORDER_INVALID, false);
9886 // Merge object attributes from input file called NAME with those of the
9887 // output. The input object attributes are in the object pointed by PASD.
9889 template<int size, bool big_endian>
9890 void
9891 Target_powerpc<size, big_endian>::merge_object_attributes(
9892 const Object* obj,
9893 const Attributes_section_data* pasd)
9895 // Return if there is no attributes section data.
9896 if (pasd == NULL)
9897 return;
9899 // Create output object attributes.
9900 if (this->attributes_section_data_ == NULL)
9901 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9903 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9904 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9905 Object_attribute* out_attr
9906 = this->attributes_section_data_->known_attributes(vendor);
9908 const char* name = obj->name().c_str();
9909 const char* err;
9910 const char* first;
9911 const char* second;
9912 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9913 int in_fp = in_attr[tag].int_value() & 0xf;
9914 int out_fp = out_attr[tag].int_value() & 0xf;
9915 bool warn_only = obj->is_dynamic();
9916 if (in_fp != out_fp)
9918 err = NULL;
9919 if ((in_fp & 3) == 0)
9921 else if ((out_fp & 3) == 0)
9923 if (!warn_only)
9925 out_fp |= in_fp & 3;
9926 out_attr[tag].set_int_value(out_fp);
9927 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9928 this->last_fp_ = name;
9931 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9933 err = N_("%s uses hard float, %s uses soft float");
9934 first = this->last_fp_;
9935 second = name;
9937 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9939 err = N_("%s uses hard float, %s uses soft float");
9940 first = name;
9941 second = this->last_fp_;
9943 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9945 err = N_("%s uses double-precision hard float, "
9946 "%s uses single-precision hard float");
9947 first = this->last_fp_;
9948 second = name;
9950 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9952 err = N_("%s uses double-precision hard float, "
9953 "%s uses single-precision hard float");
9954 first = name;
9955 second = this->last_fp_;
9958 if (err || (in_fp & 0xc) == 0)
9960 else if ((out_fp & 0xc) == 0)
9962 if (!warn_only)
9964 out_fp |= in_fp & 0xc;
9965 out_attr[tag].set_int_value(out_fp);
9966 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9967 this->last_ld_ = name;
9970 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9972 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9973 first = name;
9974 second = this->last_ld_;
9976 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9978 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9979 first = this->last_ld_;
9980 second = name;
9982 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9984 err = N_("%s uses IBM long double, %s uses IEEE long double");
9985 first = this->last_ld_;
9986 second = name;
9988 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9990 err = N_("%s uses IBM long double, %s uses IEEE long double");
9991 first = name;
9992 second = this->last_ld_;
9995 if (err)
9997 if (parameters->options().warn_mismatch())
9999 if (warn_only)
10000 gold_warning(_(err), first, second);
10001 else
10002 gold_error(_(err), first, second);
10004 // Arrange for this attribute to be deleted. It's better to
10005 // say "don't know" about a file than to wrongly claim compliance.
10006 if (!warn_only)
10007 out_attr[tag].set_type(0);
10011 if (size == 32)
10013 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10014 int in_vec = in_attr[tag].int_value() & 3;
10015 int out_vec = out_attr[tag].int_value() & 3;
10016 if (in_vec != out_vec)
10018 err = NULL;
10019 if (in_vec == 0)
10021 else if (out_vec == 0)
10023 out_vec = in_vec;
10024 out_attr[tag].set_int_value(out_vec);
10025 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10026 this->last_vec_ = name;
10028 // For now, allow generic to transition to AltiVec or SPE
10029 // without a warning. If GCC marked files with their stack
10030 // alignment and used don't-care markings for files which are
10031 // not affected by the vector ABI, we could warn about this
10032 // case too. */
10033 else if (in_vec == 1)
10035 else if (out_vec == 1)
10037 out_vec = in_vec;
10038 out_attr[tag].set_int_value(out_vec);
10039 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10040 this->last_vec_ = name;
10042 else if (out_vec < in_vec)
10044 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10045 first = this->last_vec_;
10046 second = name;
10048 else if (out_vec > in_vec)
10050 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10051 first = name;
10052 second = this->last_vec_;
10054 if (err)
10056 if (parameters->options().warn_mismatch())
10057 gold_error(_(err), first, second);
10058 out_attr[tag].set_type(0);
10062 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10063 int in_struct = in_attr[tag].int_value() & 3;
10064 int out_struct = out_attr[tag].int_value() & 3;
10065 if (in_struct != out_struct)
10067 err = NULL;
10068 if (in_struct == 0 || in_struct == 3)
10070 else if (out_struct == 0)
10072 out_struct = in_struct;
10073 out_attr[tag].set_int_value(out_struct);
10074 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10075 this->last_struct_ = name;
10077 else if (out_struct < in_struct)
10079 err = N_("%s uses r3/r4 for small structure returns, "
10080 "%s uses memory");
10081 first = this->last_struct_;
10082 second = name;
10084 else if (out_struct > in_struct)
10086 err = N_("%s uses r3/r4 for small structure returns, "
10087 "%s uses memory");
10088 first = name;
10089 second = this->last_struct_;
10091 if (err)
10093 if (parameters->options().warn_mismatch())
10094 gold_error(_(err), first, second);
10095 out_attr[tag].set_type(0);
10100 // Merge Tag_compatibility attributes and any common GNU ones.
10101 this->attributes_section_data_->merge(name, pasd);
10104 // Emit any saved relocs, and mark toc entries using any of these
10105 // relocs as not optimizable.
10107 template<int sh_type, int size, bool big_endian>
10108 void
10109 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10110 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10112 if (size == 64
10113 && parameters->options().toc_optimize())
10115 for (typename Copy_relocs<sh_type, size, big_endian>::
10116 Copy_reloc_entries::iterator p = this->entries_.begin();
10117 p != this->entries_.end();
10118 ++p)
10120 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10121 entry = *p;
10123 // If the symbol is no longer defined in a dynamic object,
10124 // then we emitted a COPY relocation. If it is still
10125 // dynamic then we'll need dynamic relocations and thus
10126 // can't optimize toc entries.
10127 if (entry.sym_->is_from_dynobj())
10129 Powerpc_relobj<size, big_endian>* ppc_object
10130 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10131 if (entry.shndx_ == ppc_object->toc_shndx())
10132 ppc_object->set_no_toc_opt(entry.address_);
10137 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10140 // Return the value to use for a branch relocation.
10142 template<int size, bool big_endian>
10143 bool
10144 Target_powerpc<size, big_endian>::symval_for_branch(
10145 const Symbol_table* symtab,
10146 const Sized_symbol<size>* gsym,
10147 Powerpc_relobj<size, big_endian>* object,
10148 Address *value,
10149 unsigned int *dest_shndx)
10151 if (size == 32 || this->abiversion() >= 2)
10152 gold_unreachable();
10153 *dest_shndx = 0;
10155 // If the symbol is defined in an opd section, ie. is a function
10156 // descriptor, use the function descriptor code entry address
10157 Powerpc_relobj<size, big_endian>* symobj = object;
10158 if (gsym != NULL
10159 && (gsym->source() != Symbol::FROM_OBJECT
10160 || gsym->object()->is_dynamic()))
10161 return true;
10162 if (gsym != NULL)
10163 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10164 unsigned int shndx = symobj->opd_shndx();
10165 if (shndx == 0)
10166 return true;
10167 Address opd_addr = symobj->get_output_section_offset(shndx);
10168 if (opd_addr == invalid_address)
10169 return true;
10170 opd_addr += symobj->output_section_address(shndx);
10171 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10173 Address sec_off;
10174 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10175 if (symtab->is_section_folded(symobj, *dest_shndx))
10177 Section_id folded
10178 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10179 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10180 *dest_shndx = folded.second;
10182 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10183 if (sec_addr == invalid_address)
10184 return false;
10186 sec_addr += symobj->output_section(*dest_shndx)->address();
10187 *value = sec_addr + sec_off;
10189 return true;
10192 template<int size>
10193 static bool
10194 relative_value_is_known(const Sized_symbol<size>* gsym)
10196 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10197 return false;
10199 if (gsym->is_from_dynobj()
10200 || gsym->is_undefined()
10201 || gsym->is_preemptible())
10202 return false;
10204 if (gsym->is_absolute())
10205 return !parameters->options().output_is_position_independent();
10207 return true;
10210 template<int size>
10211 static bool
10212 relative_value_is_known(const Symbol_value<size>* psymval)
10214 if (psymval->is_ifunc_symbol())
10215 return false;
10217 bool is_ordinary;
10218 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10220 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10223 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10224 // pld ra,symbol@got@pcrel
10225 // load/store rt,0(ra)
10226 // or
10227 // pla ra,symbol@pcrel
10228 // load/store rt,0(ra)
10229 // may be translated to
10230 // pload/pstore rt,symbol@pcrel
10231 // nop.
10232 // This function returns true if the optimization is possible, placing
10233 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10235 // On entry to this function, the linker has already determined that
10236 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10237 // while *PINSN2 is the second instruction.
10239 inline bool
10240 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10242 uint32_t insn2 = *pinsn2 >> 32;
10243 uint64_t i1new;
10245 // Check that regs match.
10246 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10247 return false;
10249 switch ((insn2 >> 26) & 63)
10251 default:
10252 return false;
10254 case 32: // lwz
10255 case 34: // lbz
10256 case 36: // stw
10257 case 38: // stb
10258 case 40: // lhz
10259 case 42: // lha
10260 case 44: // sth
10261 case 48: // lfs
10262 case 50: // lfd
10263 case 52: // stfs
10264 case 54: // stfd
10265 // These are the PMLS cases, where we just need to tack a prefix
10266 // on the insn. Check that the D field is zero.
10267 if ((insn2 & 0xffff) != 0)
10268 return false;
10269 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10270 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10271 break;
10273 case 58: // lwa, ld
10274 if ((insn2 & 0xfffd) != 0)
10275 return false;
10276 i1new = ((1ULL << 58) | (1ULL << 52)
10277 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10278 | (insn2 & (31ULL << 21)));
10279 break;
10281 case 57: // lxsd, lxssp
10282 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10283 return false;
10284 i1new = ((1ULL << 58) | (1ULL << 52)
10285 | ((40ULL | (insn2 & 3)) << 26)
10286 | (insn2 & (31ULL << 21)));
10287 break;
10289 case 61: // stxsd, stxssp, lxv, stxv
10290 if ((insn2 & 3) == 0)
10291 return false;
10292 else if ((insn2 & 3) >= 2)
10294 if ((insn2 & 0xfffc) != 0)
10295 return false;
10296 i1new = ((1ULL << 58) | (1ULL << 52)
10297 | ((44ULL | (insn2 & 3)) << 26)
10298 | (insn2 & (31ULL << 21)));
10300 else
10302 if ((insn2 & 0xfff0) != 0)
10303 return false;
10304 i1new = ((1ULL << 58) | (1ULL << 52)
10305 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10306 | (insn2 & (31ULL << 21)));
10308 break;
10310 case 56: // lq
10311 if ((insn2 & 0xffff) != 0)
10312 return false;
10313 i1new = ((1ULL << 58) | (1ULL << 52)
10314 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10315 break;
10317 case 62: // std, stq
10318 if ((insn2 & 0xfffd) != 0)
10319 return false;
10320 i1new = ((1ULL << 58) | (1ULL << 52)
10321 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10322 | (insn2 & (31ULL << 21)));
10323 break;
10326 *pinsn1 = i1new;
10327 *pinsn2 = (uint64_t) nop << 32;
10328 return true;
10331 // Perform a relocation.
10333 template<int size, bool big_endian>
10334 inline bool
10335 Target_powerpc<size, big_endian>::Relocate::relocate(
10336 const Relocate_info<size, big_endian>* relinfo,
10337 unsigned int,
10338 Target_powerpc* target,
10339 Output_section* os,
10340 size_t relnum,
10341 const unsigned char* preloc,
10342 const Sized_symbol<size>* gsym,
10343 const Symbol_value<size>* psymval,
10344 unsigned char* view,
10345 Address address,
10346 section_size_type view_size)
10348 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10349 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10350 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10352 if (view == NULL)
10353 return true;
10355 if (target->replace_tls_get_addr(gsym))
10356 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10358 const elfcpp::Rela<size, big_endian> rela(preloc);
10359 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10360 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10362 case Track_tls::NOT_EXPECTED:
10363 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10364 _("__tls_get_addr call lacks marker reloc"));
10365 break;
10366 case Track_tls::EXPECTED:
10367 // We have already complained.
10368 break;
10369 case Track_tls::SKIP:
10370 if (is_plt16_reloc<size>(r_type)
10371 || r_type == elfcpp::R_POWERPC_PLTSEQ
10372 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10374 Insn* iview = reinterpret_cast<Insn*>(view);
10375 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10377 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10379 Insn* iview = reinterpret_cast<Insn*>(view);
10380 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10382 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10383 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10385 Insn* iview = reinterpret_cast<Insn*>(view);
10386 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10387 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10389 return true;
10390 case Track_tls::NORMAL:
10391 break;
10394 // Offset from start of insn to d-field reloc.
10395 const int d_offset = big_endian ? 2 : 0;
10397 Powerpc_relobj<size, big_endian>* const object
10398 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10399 Address value = 0;
10400 bool has_stub_value = false;
10401 bool localentry0 = false;
10402 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10403 bool has_plt_offset
10404 = (gsym != NULL
10405 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10406 : object->local_has_plt_offset(r_sym));
10407 if (has_plt_offset
10408 && !is_got_reloc(r_type)
10409 && !is_plt16_reloc<size>(r_type)
10410 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10411 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10412 && r_type != elfcpp::R_POWERPC_PLTSEQ
10413 && r_type != elfcpp::R_POWERPC_PLTCALL
10414 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10415 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
10416 && (!psymval->is_ifunc_symbol()
10417 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
10419 if (size == 64
10420 && gsym != NULL
10421 && target->abiversion() >= 2
10422 && !parameters->options().output_is_position_independent()
10423 && !is_branch_reloc<size>(r_type))
10425 Address off = target->glink_section()->find_global_entry(gsym);
10426 if (off != invalid_address)
10428 value = target->glink_section()->global_entry_address() + off;
10429 has_stub_value = true;
10432 else
10434 Stub_table<size, big_endian>* stub_table = NULL;
10435 if (target->stub_tables().size() == 1)
10436 stub_table = target->stub_tables()[0];
10437 if (stub_table == NULL
10438 && !(size == 32
10439 && gsym != NULL
10440 && !parameters->options().output_is_position_independent()
10441 && !is_branch_reloc<size>(r_type)))
10442 stub_table = object->stub_table(relinfo->data_shndx);
10443 if (stub_table == NULL)
10445 // This is a ref from a data section to an ifunc symbol,
10446 // or a non-branch reloc for which we always want to use
10447 // one set of stubs for resolving function addresses.
10448 if (target->stub_tables().size() != 0)
10449 stub_table = target->stub_tables()[0];
10451 if (stub_table != NULL)
10453 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10454 if (gsym != NULL)
10455 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10456 rela.get_r_addend());
10457 else
10458 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10459 rela.get_r_addend());
10460 if (ent != NULL)
10462 value = stub_table->stub_address() + ent->off_;
10463 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10464 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10465 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10466 if (size == 64
10467 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
10468 value += ent->tocoff_;
10469 if (size == 64
10470 && ent->r2save_
10471 && !(gsym != NULL
10472 && target->is_tls_get_addr_opt(gsym)))
10474 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10476 if (!(target->power10_stubs()
10477 && target->power10_stubs_auto()))
10478 value += 4;
10480 else if (relnum < reloc_count - 1)
10482 Reltype next_rela(preloc + reloc_size);
10483 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10484 == elfcpp::R_PPC64_TOCSAVE
10485 && (next_rela.get_r_offset()
10486 == rela.get_r_offset() + 4))
10487 value += 4;
10490 localentry0 = ent->localentry0_;
10491 has_stub_value = true;
10495 // We don't care too much about bogus debug references to
10496 // non-local functions, but otherwise there had better be a plt
10497 // call stub or global entry stub as appropriate.
10498 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10501 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10502 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10503 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10505 const Output_data_plt_powerpc<size, big_endian>* plt;
10506 if (gsym)
10507 value = target->plt_off(gsym, &plt);
10508 else
10509 value = target->plt_off(object, r_sym, &plt);
10510 value += plt->address();
10512 if (size == 64)
10514 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10515 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10516 value -= (target->got_section()->output_section()->address()
10517 + object->toc_base_offset());
10519 else if (parameters->options().output_is_position_independent())
10521 if (rela.get_r_addend() >= 32768)
10523 unsigned int got2 = object->got2_shndx();
10524 value -= (object->get_output_section_offset(got2)
10525 + object->output_section(got2)->address()
10526 + rela.get_r_addend());
10528 else
10529 value -= (target->got_section()->address()
10530 + target->got_section()->g_o_t());
10533 else if (!has_plt_offset
10534 && (is_plt16_reloc<size>(r_type)
10535 || r_type == elfcpp::R_POWERPC_PLTSEQ
10536 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10538 Insn* iview = reinterpret_cast<Insn*>(view);
10539 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10540 r_type = elfcpp::R_POWERPC_NONE;
10542 else if (!has_plt_offset
10543 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10544 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10546 Insn* iview = reinterpret_cast<Insn*>(view);
10547 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10548 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10549 r_type = elfcpp::R_POWERPC_NONE;
10551 else if (is_got_reloc(r_type))
10553 if (gsym != NULL)
10555 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10556 value = gsym->got_offset(GOT_TYPE_STANDARD);
10558 else
10560 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10561 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10563 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10564 value += target->got_section()->address();
10565 else
10566 value -= target->got_section()->got_base_offset(object);
10568 else if (r_type == elfcpp::R_PPC64_TOC)
10570 value = (target->got_section()->output_section()->address()
10571 + object->toc_base_offset());
10573 else if (gsym != NULL
10574 && (r_type == elfcpp::R_POWERPC_REL24
10575 || r_type == elfcpp::R_PPC_PLTREL24)
10576 && has_stub_value)
10578 if (size == 64)
10580 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10581 Valtype* wv = reinterpret_cast<Valtype*>(view);
10582 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10583 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10585 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10586 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10587 if ((insn & 1) != 0
10588 && (insn2 == nop
10589 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10591 elfcpp::Swap<32, big_endian>::
10592 writeval(wv + 1, ld_2_1 + target->stk_toc());
10593 can_plt_call = true;
10596 if (!can_plt_call)
10598 // If we don't have a branch and link followed by a nop,
10599 // we can't go via the plt because there is no place to
10600 // put a toc restoring instruction.
10601 // Unless we know we won't be returning.
10602 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10603 can_plt_call = true;
10605 if (!can_plt_call)
10607 // g++ as of 20130507 emits self-calls without a
10608 // following nop. This is arguably wrong since we have
10609 // conflicting information. On the one hand a global
10610 // symbol and on the other a local call sequence, but
10611 // don't error for this special case.
10612 // It isn't possible to cheaply verify we have exactly
10613 // such a call. Allow all calls to the same section.
10614 bool ok = false;
10615 Address code = value;
10616 if (gsym->source() == Symbol::FROM_OBJECT
10617 && gsym->object() == object)
10619 unsigned int dest_shndx = 0;
10620 if (target->abiversion() < 2)
10622 Address addend = rela.get_r_addend();
10623 code = psymval->value(object, addend);
10624 target->symval_for_branch(relinfo->symtab, gsym, object,
10625 &code, &dest_shndx);
10627 bool is_ordinary;
10628 if (dest_shndx == 0)
10629 dest_shndx = gsym->shndx(&is_ordinary);
10630 ok = dest_shndx == relinfo->data_shndx;
10632 if (!ok)
10634 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10635 _("call lacks nop, can't restore toc; "
10636 "recompile with -fPIC"));
10637 value = code;
10642 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10643 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10644 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10645 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10646 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10648 // First instruction of a global dynamic sequence, arg setup insn.
10649 const bool final = gsym == NULL || gsym->final_value_is_known();
10650 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10651 enum Got_type got_type = GOT_TYPE_STANDARD;
10652 if (tls_type == tls::TLSOPT_NONE)
10653 got_type = GOT_TYPE_TLSGD;
10654 else if (tls_type == tls::TLSOPT_TO_IE)
10655 got_type = GOT_TYPE_TPREL;
10656 if (got_type != GOT_TYPE_STANDARD)
10658 if (gsym != NULL)
10660 gold_assert(gsym->has_got_offset(got_type));
10661 value = gsym->got_offset(got_type);
10663 else
10665 gold_assert(object->local_has_got_offset(r_sym, got_type));
10666 value = object->local_got_offset(r_sym, got_type);
10668 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10669 value += target->got_section()->address();
10670 else
10671 value -= target->got_section()->got_base_offset(object);
10673 if (tls_type == tls::TLSOPT_TO_IE)
10675 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10677 Insn* iview = reinterpret_cast<Insn*>(view);
10678 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10679 pinsn <<= 32;
10680 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10681 // pla -> pld
10682 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10683 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10684 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10685 pinsn & 0xffffffff);
10686 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
10688 else
10690 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10691 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10693 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10694 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10695 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10696 if (size == 32)
10697 insn |= 32 << 26; // lwz
10698 else
10699 insn |= 58 << 26; // ld
10700 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10702 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10703 - elfcpp::R_POWERPC_GOT_TLSGD16);
10706 else if (tls_type == tls::TLSOPT_TO_LE)
10708 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10710 Insn* iview = reinterpret_cast<Insn*>(view);
10711 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10712 pinsn <<= 32;
10713 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10714 // pla pcrel -> paddi r13
10715 pinsn += (-1ULL << 52) + (13ULL << 16);
10716 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10717 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10718 pinsn & 0xffffffff);
10719 r_type = elfcpp::R_PPC64_TPREL34;
10720 value = psymval->value(object, rela.get_r_addend());
10722 else
10724 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10725 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10727 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10728 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10729 insn &= (1 << 26) - (1 << 21); // extract rt
10730 if (size == 32)
10731 insn |= addis_0_2;
10732 else
10733 insn |= addis_0_13;
10734 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10735 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10736 value = psymval->value(object, rela.get_r_addend());
10738 else
10740 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10741 Insn insn = nop;
10742 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10743 r_type = elfcpp::R_POWERPC_NONE;
10748 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10749 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10750 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10751 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10752 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10754 // First instruction of a local dynamic sequence, arg setup insn.
10755 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10756 if (tls_type == tls::TLSOPT_NONE)
10758 value = target->tlsld_got_offset();
10759 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10760 value += target->got_section()->address();
10761 else
10762 value -= target->got_section()->got_base_offset(object);
10764 else
10766 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10767 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10769 Insn* iview = reinterpret_cast<Insn*>(view);
10770 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10771 pinsn <<= 32;
10772 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10773 // pla pcrel -> paddi r13
10774 pinsn += (-1ULL << 52) + (13ULL << 16);
10775 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10776 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10777 pinsn & 0xffffffff);
10778 r_type = elfcpp::R_PPC64_TPREL34;
10779 value = dtp_offset;
10781 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10782 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10784 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10785 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10786 insn &= (1 << 26) - (1 << 21); // extract rt
10787 if (size == 32)
10788 insn |= addis_0_2;
10789 else
10790 insn |= addis_0_13;
10791 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10792 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10793 value = dtp_offset;
10795 else
10797 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10798 Insn insn = nop;
10799 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10800 r_type = elfcpp::R_POWERPC_NONE;
10804 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10805 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10806 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
10807 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10808 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10810 // Accesses relative to a local dynamic sequence address,
10811 // no optimisation here.
10812 if (gsym != NULL)
10814 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10815 value = gsym->got_offset(GOT_TYPE_DTPREL);
10817 else
10819 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10820 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10822 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10823 value += target->got_section()->address();
10824 else
10825 value -= target->got_section()->got_base_offset(object);
10827 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10828 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10829 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10830 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10831 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10833 // First instruction of initial exec sequence.
10834 const bool final = gsym == NULL || gsym->final_value_is_known();
10835 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10836 if (tls_type == tls::TLSOPT_NONE)
10838 if (gsym != NULL)
10840 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10841 value = gsym->got_offset(GOT_TYPE_TPREL);
10843 else
10845 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10846 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10848 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10849 value += target->got_section()->address();
10850 else
10851 value -= target->got_section()->got_base_offset(object);
10853 else
10855 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10856 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10858 Insn* iview = reinterpret_cast<Insn*>(view);
10859 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10860 pinsn <<= 32;
10861 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10862 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10863 pinsn += ((2ULL << 56) + (-1ULL << 52)
10864 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10865 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10866 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10867 pinsn & 0xffffffff);
10868 r_type = elfcpp::R_PPC64_TPREL34;
10869 value = psymval->value(object, rela.get_r_addend());
10871 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10872 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10874 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10875 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10876 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10877 if (size == 32)
10878 insn |= addis_0_2;
10879 else
10880 insn |= addis_0_13;
10881 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10882 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10883 value = psymval->value(object, rela.get_r_addend());
10885 else
10887 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10888 Insn insn = nop;
10889 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10890 r_type = elfcpp::R_POWERPC_NONE;
10894 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10895 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10897 // Second instruction of a global dynamic sequence,
10898 // the __tls_get_addr call
10899 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10900 const bool final = gsym == NULL || gsym->final_value_is_known();
10901 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10902 if (tls_type != tls::TLSOPT_NONE)
10904 if (tls_type == tls::TLSOPT_TO_IE)
10906 Insn* iview = reinterpret_cast<Insn*>(view);
10907 Insn insn = add_3_3_13;
10908 if (size == 32)
10909 insn = add_3_3_2;
10910 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10911 r_type = elfcpp::R_POWERPC_NONE;
10913 else
10915 bool is_pcrel = false;
10916 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10917 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10918 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10919 if (relnum < reloc_count - 1)
10921 Reltype next_rela(preloc + reloc_size);
10922 unsigned int r_type2
10923 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10924 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10925 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10926 && next_rela.get_r_offset() == rela.get_r_offset())
10927 is_pcrel = true;
10929 Insn* iview = reinterpret_cast<Insn*>(view);
10930 if (is_pcrel)
10932 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10933 r_type = elfcpp::R_POWERPC_NONE;
10935 else
10937 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10938 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10939 view += d_offset;
10940 value = psymval->value(object, rela.get_r_addend());
10943 this->skip_next_tls_get_addr_call();
10946 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10947 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10949 // Second instruction of a local dynamic sequence,
10950 // the __tls_get_addr call
10951 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10952 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10953 if (tls_type == tls::TLSOPT_TO_LE)
10955 bool is_pcrel = false;
10956 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10957 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10958 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10959 if (relnum < reloc_count - 1)
10961 Reltype next_rela(preloc + reloc_size);
10962 unsigned int r_type2
10963 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10964 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10965 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10966 && next_rela.get_r_offset() == rela.get_r_offset())
10967 is_pcrel = true;
10969 Insn* iview = reinterpret_cast<Insn*>(view);
10970 if (is_pcrel)
10972 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10973 r_type = elfcpp::R_POWERPC_NONE;
10975 else
10977 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10978 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10979 view += d_offset;
10980 value = dtp_offset;
10982 this->skip_next_tls_get_addr_call();
10985 else if (r_type == elfcpp::R_POWERPC_TLS)
10987 // Second instruction of an initial exec sequence
10988 const bool final = gsym == NULL || gsym->final_value_is_known();
10989 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10990 if (tls_type == tls::TLSOPT_TO_LE)
10992 Address roff = rela.get_r_offset() & 3;
10993 Insn* iview = reinterpret_cast<Insn*>(view - roff);
10994 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10995 unsigned int reg = size == 32 ? 2 : 13;
10996 insn = at_tls_transform(insn, reg);
10997 gold_assert(insn != 0);
10998 if (roff == 0)
11000 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11001 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11002 view += d_offset;
11003 value = psymval->value(object, rela.get_r_addend());
11005 else if (roff == 1)
11007 // For pcrel IE to LE we already have the full offset
11008 // and thus don't need an addi here. A nop or mr will do.
11009 if ((insn & (0x3f << 26)) == 14 << 26)
11011 // Extract regs from addi rt,ra,si.
11012 unsigned int rt = (insn >> 21) & 0x1f;
11013 unsigned int ra = (insn >> 16) & 0x1f;
11014 if (rt == ra)
11015 insn = nop;
11016 else
11018 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11019 insn = (rt << 16) | (ra << 21) | (ra << 11);
11020 insn |= (31u << 26) | (444u << 1);
11023 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11024 r_type = elfcpp::R_POWERPC_NONE;
11028 else if (!has_stub_value)
11030 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11031 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11033 // PLTCALL without plt entry => convert to direct call
11034 Insn* iview = reinterpret_cast<Insn*>(view);
11035 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11036 insn = (insn & 1) | b;
11037 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11038 if (size == 32)
11039 r_type = elfcpp::R_PPC_PLTREL24;
11040 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11041 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11042 else
11043 r_type = elfcpp::R_POWERPC_REL24;
11045 Address addend = 0;
11046 if (!(size == 32
11047 && (r_type == elfcpp::R_PPC_PLTREL24
11048 || r_type == elfcpp::R_POWERPC_PLT16_LO
11049 || r_type == elfcpp::R_POWERPC_PLT16_HI
11050 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11051 addend = rela.get_r_addend();
11052 value = psymval->value(object, addend);
11053 unsigned int local_ent = 0;
11054 if (size == 64 && is_branch_reloc<size>(r_type))
11056 if (target->abiversion() >= 2)
11058 if (gsym != NULL)
11059 local_ent = object->ppc64_local_entry_offset(gsym);
11060 else
11061 local_ent = object->ppc64_local_entry_offset(r_sym);
11063 else
11065 unsigned int dest_shndx;
11066 target->symval_for_branch(relinfo->symtab, gsym, object,
11067 &value, &dest_shndx);
11070 Address max_branch = max_branch_delta<size>(r_type);
11071 if (max_branch != 0
11072 && (value + local_ent - address + max_branch >= 2 * max_branch
11073 || (size == 64
11074 && r_type == elfcpp::R_PPC64_REL24_NOTOC
11075 && (gsym != NULL
11076 ? object->ppc64_needs_toc(gsym)
11077 : object->ppc64_needs_toc(r_sym)))))
11079 Stub_table<size, big_endian>* stub_table
11080 = object->stub_table(relinfo->data_shndx);
11081 if (stub_table != NULL)
11083 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11084 = stub_table->find_long_branch_entry(object, value);
11085 if (ent != NULL)
11087 if (ent->save_res_)
11088 value = (value - target->savres_section()->address()
11089 + stub_table->branch_size());
11090 else
11092 value = (stub_table->stub_address()
11093 + stub_table->plt_size()
11094 + ent->off_);
11095 if (size == 64
11096 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
11097 value += (elfcpp::ppc64_decode_local_entry(ent->other_)
11098 + ent->tocoff_);
11100 has_stub_value = true;
11104 if (!has_stub_value)
11105 value += local_ent;
11108 switch (r_type)
11110 case elfcpp::R_PPC64_REL24_NOTOC:
11111 if (size == 32)
11112 break;
11113 // Fall through.
11114 case elfcpp::R_PPC64_REL64:
11115 case elfcpp::R_POWERPC_REL32:
11116 case elfcpp::R_POWERPC_REL24:
11117 case elfcpp::R_PPC_PLTREL24:
11118 case elfcpp::R_PPC_LOCAL24PC:
11119 case elfcpp::R_POWERPC_REL16:
11120 case elfcpp::R_POWERPC_REL16_LO:
11121 case elfcpp::R_POWERPC_REL16_HI:
11122 case elfcpp::R_POWERPC_REL16_HA:
11123 case elfcpp::R_POWERPC_REL16DX_HA:
11124 case elfcpp::R_PPC64_REL16_HIGH:
11125 case elfcpp::R_PPC64_REL16_HIGHA:
11126 case elfcpp::R_PPC64_REL16_HIGHER:
11127 case elfcpp::R_PPC64_REL16_HIGHERA:
11128 case elfcpp::R_PPC64_REL16_HIGHEST:
11129 case elfcpp::R_PPC64_REL16_HIGHESTA:
11130 case elfcpp::R_POWERPC_REL14:
11131 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11132 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11133 case elfcpp::R_PPC64_PCREL34:
11134 case elfcpp::R_PPC64_GOT_PCREL34:
11135 case elfcpp::R_PPC64_PLT_PCREL34:
11136 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11137 case elfcpp::R_PPC64_PCREL28:
11138 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11139 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11140 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11141 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11142 case elfcpp::R_PPC64_REL16_HIGHER34:
11143 case elfcpp::R_PPC64_REL16_HIGHERA34:
11144 case elfcpp::R_PPC64_REL16_HIGHEST34:
11145 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11146 value -= address;
11147 break;
11149 case elfcpp::R_PPC64_TOC16:
11150 case elfcpp::R_PPC64_TOC16_LO:
11151 case elfcpp::R_PPC64_TOC16_HI:
11152 case elfcpp::R_PPC64_TOC16_HA:
11153 case elfcpp::R_PPC64_TOC16_DS:
11154 case elfcpp::R_PPC64_TOC16_LO_DS:
11155 // Subtract the TOC base address.
11156 value -= (target->got_section()->output_section()->address()
11157 + object->toc_base_offset());
11158 break;
11160 case elfcpp::R_POWERPC_SECTOFF:
11161 case elfcpp::R_POWERPC_SECTOFF_LO:
11162 case elfcpp::R_POWERPC_SECTOFF_HI:
11163 case elfcpp::R_POWERPC_SECTOFF_HA:
11164 case elfcpp::R_PPC64_SECTOFF_DS:
11165 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11166 if (os != NULL)
11167 value -= os->address();
11168 break;
11170 case elfcpp::R_PPC64_TPREL16_DS:
11171 case elfcpp::R_PPC64_TPREL16_LO_DS:
11172 case elfcpp::R_PPC64_TPREL16_HIGH:
11173 case elfcpp::R_PPC64_TPREL16_HIGHA:
11174 if (size != 64)
11175 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11176 break;
11177 // Fall through.
11178 case elfcpp::R_POWERPC_TPREL16:
11179 case elfcpp::R_POWERPC_TPREL16_LO:
11180 case elfcpp::R_POWERPC_TPREL16_HI:
11181 case elfcpp::R_POWERPC_TPREL16_HA:
11182 case elfcpp::R_POWERPC_TPREL:
11183 case elfcpp::R_PPC64_TPREL16_HIGHER:
11184 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11185 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11186 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11187 case elfcpp::R_PPC64_TPREL34:
11188 // tls symbol values are relative to tls_segment()->vaddr()
11189 value -= tp_offset;
11190 break;
11192 case elfcpp::R_PPC64_DTPREL16_DS:
11193 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11194 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11195 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11196 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11197 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11198 if (size != 64)
11199 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11200 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11201 break;
11202 // Fall through.
11203 case elfcpp::R_POWERPC_DTPREL16:
11204 case elfcpp::R_POWERPC_DTPREL16_LO:
11205 case elfcpp::R_POWERPC_DTPREL16_HI:
11206 case elfcpp::R_POWERPC_DTPREL16_HA:
11207 case elfcpp::R_POWERPC_DTPREL:
11208 case elfcpp::R_PPC64_DTPREL16_HIGH:
11209 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11210 case elfcpp::R_PPC64_DTPREL34:
11211 // tls symbol values are relative to tls_segment()->vaddr()
11212 value -= dtp_offset;
11213 break;
11215 case elfcpp::R_PPC64_ADDR64_LOCAL:
11216 if (gsym != NULL)
11217 value += object->ppc64_local_entry_offset(gsym);
11218 else
11219 value += object->ppc64_local_entry_offset(r_sym);
11220 break;
11222 default:
11223 break;
11226 Insn branch_bit = 0;
11227 switch (r_type)
11229 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11230 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11231 branch_bit = 1 << 21;
11232 // Fall through.
11233 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11234 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11236 Insn* iview = reinterpret_cast<Insn*>(view);
11237 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11238 insn &= ~(1 << 21);
11239 insn |= branch_bit;
11240 if (this->is_isa_v2)
11242 // Set 'a' bit. This is 0b00010 in BO field for branch
11243 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11244 // for branch on CTR insns (BO == 1a00t or 1a01t).
11245 if ((insn & (0x14 << 21)) == (0x04 << 21))
11246 insn |= 0x02 << 21;
11247 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11248 insn |= 0x08 << 21;
11249 else
11250 break;
11252 else
11254 // Invert 'y' bit if not the default.
11255 if (static_cast<Signed_address>(value) < 0)
11256 insn ^= 1 << 21;
11258 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11260 break;
11262 case elfcpp::R_POWERPC_PLT16_HA:
11263 if (size == 32
11264 && !parameters->options().output_is_position_independent())
11266 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11267 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11269 // Convert addis to lis.
11270 if ((insn & (0x3f << 26)) == 15u << 26
11271 && (insn & (0x1f << 16)) != 0)
11273 insn &= ~(0x1f << 16);
11274 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11277 break;
11279 default:
11280 break;
11283 if (gsym
11284 ? relative_value_is_known(gsym)
11285 : relative_value_is_known(psymval))
11287 Insn* iview;
11288 Insn* iview2;
11289 Insn insn;
11290 uint64_t pinsn, pinsn2;
11292 switch (r_type)
11294 default:
11295 break;
11297 // Multi-instruction sequences that access the GOT/TOC can
11298 // be optimized, eg.
11299 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11300 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11301 // and
11302 // addis ra,r2,0; addi rb,ra,x@toc@l;
11303 // to nop; addi rb,r2,x@toc;
11304 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11305 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11306 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11307 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11308 case elfcpp::R_POWERPC_GOT16_HA:
11309 case elfcpp::R_PPC64_TOC16_HA:
11310 if (size == 64 && parameters->options().toc_optimize())
11312 iview = reinterpret_cast<Insn*>(view - d_offset);
11313 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11314 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11315 && object->make_toc_relative(target, &value))
11316 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11317 && object->make_got_relative(target, psymval,
11318 rela.get_r_addend(),
11319 &value)))
11321 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11322 == ((15u << 26) | (2 << 16)));
11324 if (((insn & ((0x3f << 26) | 0x1f << 16))
11325 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11326 && value + 0x8000 < 0x10000)
11328 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11329 return true;
11332 break;
11334 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11335 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11336 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11337 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11338 case elfcpp::R_POWERPC_GOT16_LO:
11339 case elfcpp::R_PPC64_GOT16_LO_DS:
11340 case elfcpp::R_PPC64_TOC16_LO:
11341 case elfcpp::R_PPC64_TOC16_LO_DS:
11342 if (size == 64 && parameters->options().toc_optimize())
11344 iview = reinterpret_cast<Insn*>(view - d_offset);
11345 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11346 bool changed = false;
11347 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11348 && object->make_toc_relative(target, &value))
11349 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11350 && object->make_got_relative(target, psymval,
11351 rela.get_r_addend(),
11352 &value)))
11354 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11355 insn ^= (14u << 26) ^ (58u << 26);
11356 r_type = elfcpp::R_PPC64_TOC16_LO;
11357 changed = true;
11359 if (ok_lo_toc_insn(insn, r_type)
11360 && value + 0x8000 < 0x10000)
11362 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11364 // Transform addic to addi when we change reg.
11365 insn &= ~((0x3f << 26) | (0x1f << 16));
11366 insn |= (14u << 26) | (2 << 16);
11368 else
11370 insn &= ~(0x1f << 16);
11371 insn |= 2 << 16;
11373 changed = true;
11375 if (changed)
11376 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11378 break;
11380 case elfcpp::R_PPC64_GOT_PCREL34:
11381 if (size == 64 && parameters->options().toc_optimize())
11383 iview = reinterpret_cast<Insn*>(view);
11384 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11385 pinsn <<= 32;
11386 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11387 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11388 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11389 break;
11391 Address relval = psymval->value(object, rela.get_r_addend());
11392 relval -= address;
11393 if (relval + (1ULL << 33) < 1ULL << 34)
11395 value = relval;
11396 // Replace with paddi
11397 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11398 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11399 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11400 pinsn & 0xffffffff);
11401 goto pcrelopt;
11404 break;
11406 case elfcpp::R_PPC64_PCREL34:
11407 if (size == 64)
11409 iview = reinterpret_cast<Insn*>(view);
11410 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11411 pinsn <<= 32;
11412 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11413 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11414 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11415 | (14ULL << 26) /* paddi */))
11416 break;
11418 pcrelopt:
11419 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11420 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11421 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11422 if (relnum >= reloc_count - 1)
11423 break;
11425 Reltype next_rela(preloc + reloc_size);
11426 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11427 != elfcpp::R_PPC64_PCREL_OPT)
11428 || next_rela.get_r_offset() != rela.get_r_offset())
11429 break;
11431 Address off = next_rela.get_r_addend();
11432 if (off == 0)
11433 off = 8; // zero means next insn.
11434 if (off + rela.get_r_offset() + 4 > view_size)
11435 break;
11437 iview2 = reinterpret_cast<Insn*>(view + off);
11438 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11439 pinsn2 <<= 32;
11440 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11441 break;
11442 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11444 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11445 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11446 pinsn & 0xffffffff);
11447 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11450 break;
11452 case elfcpp::R_POWERPC_TPREL16_HA:
11453 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11455 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11456 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11457 return true;
11459 break;
11461 case elfcpp::R_PPC64_TPREL16_LO_DS:
11462 if (size == 32)
11463 // R_PPC_TLSGD, R_PPC_TLSLD
11464 break;
11465 // Fall through.
11466 case elfcpp::R_POWERPC_TPREL16_LO:
11467 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11469 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11470 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11471 insn &= ~(0x1f << 16);
11472 insn |= (size == 32 ? 2 : 13) << 16;
11473 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11475 break;
11477 case elfcpp::R_PPC64_ENTRY:
11478 if (size == 64)
11480 value = (target->got_section()->output_section()->address()
11481 + object->toc_base_offset());
11482 if (value + 0x80008000 <= 0xffffffff
11483 && !parameters->options().output_is_position_independent())
11485 Insn* iview = reinterpret_cast<Insn*>(view);
11486 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11487 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11489 if ((insn1 & ~0xfffc) == ld_2_12
11490 && insn2 == add_2_2_12)
11492 insn1 = lis_2 + ha(value);
11493 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11494 insn2 = addi_2_2 + l(value);
11495 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11496 return true;
11499 else
11501 value -= address;
11502 if (value + 0x80008000 <= 0xffffffff)
11504 Insn* iview = reinterpret_cast<Insn*>(view);
11505 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11506 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11508 if ((insn1 & ~0xfffc) == ld_2_12
11509 && insn2 == add_2_2_12)
11511 insn1 = addis_2_12 + ha(value);
11512 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11513 insn2 = addi_2_2 + l(value);
11514 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11515 return true;
11520 break;
11522 case elfcpp::R_POWERPC_REL16_LO:
11523 // If we are generating a non-PIC executable, edit
11524 // 0: addis 2,12,.TOC.-0b@ha
11525 // addi 2,2,.TOC.-0b@l
11526 // used by ELFv2 global entry points to set up r2, to
11527 // lis 2,.TOC.@ha
11528 // addi 2,2,.TOC.@l
11529 // if .TOC. is in range. */
11530 if (size == 64
11531 && value + address - 4 + 0x80008000 <= 0xffffffff
11532 && relnum + 1 > 1
11533 && preloc != NULL
11534 && target->abiversion() >= 2
11535 && !parameters->options().output_is_position_independent()
11536 && rela.get_r_addend() == d_offset + 4
11537 && gsym != NULL
11538 && strcmp(gsym->name(), ".TOC.") == 0)
11540 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11541 Reltype prev_rela(preloc - reloc_size);
11542 if ((prev_rela.get_r_info()
11543 == elfcpp::elf_r_info<size>(r_sym,
11544 elfcpp::R_POWERPC_REL16_HA))
11545 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11546 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11548 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11549 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11550 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11552 if ((insn1 & 0xffff0000) == addis_2_12
11553 && (insn2 & 0xffff0000) == addi_2_2)
11555 insn1 = lis_2 + ha(value + address - 4);
11556 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11557 insn2 = addi_2_2 + l(value + address - 4);
11558 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11559 if (relinfo->rr)
11561 relinfo->rr->set_strategy(relnum - 1,
11562 Relocatable_relocs::RELOC_SPECIAL);
11563 relinfo->rr->set_strategy(relnum,
11564 Relocatable_relocs::RELOC_SPECIAL);
11566 return true;
11570 break;
11574 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11575 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11576 switch (r_type)
11578 case elfcpp::R_POWERPC_ADDR32:
11579 case elfcpp::R_POWERPC_UADDR32:
11580 if (size == 64)
11581 overflow = Reloc::CHECK_BITFIELD;
11582 break;
11584 case elfcpp::R_POWERPC_REL32:
11585 case elfcpp::R_POWERPC_REL16DX_HA:
11586 if (size == 64)
11587 overflow = Reloc::CHECK_SIGNED;
11588 break;
11590 case elfcpp::R_POWERPC_UADDR16:
11591 overflow = Reloc::CHECK_BITFIELD;
11592 break;
11594 case elfcpp::R_POWERPC_ADDR16:
11595 // We really should have three separate relocations,
11596 // one for 16-bit data, one for insns with 16-bit signed fields,
11597 // and one for insns with 16-bit unsigned fields.
11598 overflow = Reloc::CHECK_BITFIELD;
11599 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11600 overflow = Reloc::CHECK_LOW_INSN;
11601 break;
11603 case elfcpp::R_POWERPC_ADDR16_HI:
11604 case elfcpp::R_POWERPC_ADDR16_HA:
11605 case elfcpp::R_POWERPC_GOT16_HI:
11606 case elfcpp::R_POWERPC_GOT16_HA:
11607 case elfcpp::R_POWERPC_PLT16_HI:
11608 case elfcpp::R_POWERPC_PLT16_HA:
11609 case elfcpp::R_POWERPC_SECTOFF_HI:
11610 case elfcpp::R_POWERPC_SECTOFF_HA:
11611 case elfcpp::R_PPC64_TOC16_HI:
11612 case elfcpp::R_PPC64_TOC16_HA:
11613 case elfcpp::R_PPC64_PLTGOT16_HI:
11614 case elfcpp::R_PPC64_PLTGOT16_HA:
11615 case elfcpp::R_POWERPC_TPREL16_HI:
11616 case elfcpp::R_POWERPC_TPREL16_HA:
11617 case elfcpp::R_POWERPC_DTPREL16_HI:
11618 case elfcpp::R_POWERPC_DTPREL16_HA:
11619 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11620 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11621 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11622 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11623 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11624 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11625 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11626 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11627 case elfcpp::R_POWERPC_REL16_HI:
11628 case elfcpp::R_POWERPC_REL16_HA:
11629 if (size != 32)
11630 overflow = Reloc::CHECK_HIGH_INSN;
11631 break;
11633 case elfcpp::R_POWERPC_REL16:
11634 case elfcpp::R_PPC64_TOC16:
11635 case elfcpp::R_POWERPC_GOT16:
11636 case elfcpp::R_POWERPC_SECTOFF:
11637 case elfcpp::R_POWERPC_TPREL16:
11638 case elfcpp::R_POWERPC_DTPREL16:
11639 case elfcpp::R_POWERPC_GOT_TLSGD16:
11640 case elfcpp::R_POWERPC_GOT_TLSLD16:
11641 case elfcpp::R_POWERPC_GOT_TPREL16:
11642 case elfcpp::R_POWERPC_GOT_DTPREL16:
11643 overflow = Reloc::CHECK_LOW_INSN;
11644 break;
11646 case elfcpp::R_PPC64_REL24_NOTOC:
11647 if (size == 32)
11648 break;
11649 // Fall through.
11650 case elfcpp::R_POWERPC_ADDR24:
11651 case elfcpp::R_POWERPC_ADDR14:
11652 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11653 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11654 case elfcpp::R_PPC64_ADDR16_DS:
11655 case elfcpp::R_POWERPC_REL24:
11656 case elfcpp::R_PPC_PLTREL24:
11657 case elfcpp::R_PPC_LOCAL24PC:
11658 case elfcpp::R_PPC64_TPREL16_DS:
11659 case elfcpp::R_PPC64_DTPREL16_DS:
11660 case elfcpp::R_PPC64_TOC16_DS:
11661 case elfcpp::R_PPC64_GOT16_DS:
11662 case elfcpp::R_PPC64_SECTOFF_DS:
11663 case elfcpp::R_POWERPC_REL14:
11664 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11665 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11666 case elfcpp::R_PPC64_D34:
11667 case elfcpp::R_PPC64_PCREL34:
11668 case elfcpp::R_PPC64_GOT_PCREL34:
11669 case elfcpp::R_PPC64_PLT_PCREL34:
11670 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11671 case elfcpp::R_PPC64_D28:
11672 case elfcpp::R_PPC64_PCREL28:
11673 case elfcpp::R_PPC64_TPREL34:
11674 case elfcpp::R_PPC64_DTPREL34:
11675 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11676 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11677 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11678 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11679 overflow = Reloc::CHECK_SIGNED;
11680 break;
11683 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11684 Insn insn = 0;
11686 if (overflow == Reloc::CHECK_LOW_INSN
11687 || overflow == Reloc::CHECK_HIGH_INSN)
11689 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11691 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11692 overflow = Reloc::CHECK_BITFIELD;
11693 else if (overflow == Reloc::CHECK_LOW_INSN
11694 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11695 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11696 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11697 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11698 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11699 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
11700 overflow = Reloc::CHECK_UNSIGNED;
11701 else
11702 overflow = Reloc::CHECK_SIGNED;
11705 bool maybe_dq_reloc = false;
11706 typename Powerpc_relocate_functions<size, big_endian>::Status status
11707 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
11708 switch (r_type)
11710 case elfcpp::R_POWERPC_NONE:
11711 case elfcpp::R_POWERPC_TLS:
11712 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11713 case elfcpp::R_POWERPC_GNU_VTENTRY:
11714 case elfcpp::R_POWERPC_PLTSEQ:
11715 case elfcpp::R_POWERPC_PLTCALL:
11716 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11717 case elfcpp::R_PPC64_PLTCALL_NOTOC:
11718 case elfcpp::R_PPC64_PCREL_OPT:
11719 break;
11721 case elfcpp::R_PPC64_ADDR64:
11722 case elfcpp::R_PPC64_REL64:
11723 case elfcpp::R_PPC64_TOC:
11724 case elfcpp::R_PPC64_ADDR64_LOCAL:
11725 Reloc::addr64(view, value);
11726 break;
11728 case elfcpp::R_POWERPC_TPREL:
11729 case elfcpp::R_POWERPC_DTPREL:
11730 if (size == 64)
11731 Reloc::addr64(view, value);
11732 else
11733 status = Reloc::addr32(view, value, overflow);
11734 break;
11736 case elfcpp::R_PPC64_UADDR64:
11737 Reloc::addr64_u(view, value);
11738 break;
11740 case elfcpp::R_POWERPC_ADDR32:
11741 status = Reloc::addr32(view, value, overflow);
11742 break;
11744 case elfcpp::R_POWERPC_REL32:
11745 case elfcpp::R_POWERPC_UADDR32:
11746 status = Reloc::addr32_u(view, value, overflow);
11747 break;
11749 case elfcpp::R_PPC64_REL24_NOTOC:
11750 if (size == 32)
11751 goto unsupp; // R_PPC_EMB_RELSDA
11752 // Fall through.
11753 case elfcpp::R_POWERPC_ADDR24:
11754 case elfcpp::R_POWERPC_REL24:
11755 case elfcpp::R_PPC_PLTREL24:
11756 case elfcpp::R_PPC_LOCAL24PC:
11757 status = Reloc::addr24(view, value, overflow);
11758 break;
11760 case elfcpp::R_POWERPC_GOT_DTPREL16:
11761 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11762 case elfcpp::R_POWERPC_GOT_TPREL16:
11763 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11764 if (size == 64)
11766 // On ppc64 these are all ds form
11767 maybe_dq_reloc = true;
11768 break;
11770 // Fall through.
11771 case elfcpp::R_POWERPC_ADDR16:
11772 case elfcpp::R_POWERPC_REL16:
11773 case elfcpp::R_PPC64_TOC16:
11774 case elfcpp::R_POWERPC_GOT16:
11775 case elfcpp::R_POWERPC_SECTOFF:
11776 case elfcpp::R_POWERPC_TPREL16:
11777 case elfcpp::R_POWERPC_DTPREL16:
11778 case elfcpp::R_POWERPC_GOT_TLSGD16:
11779 case elfcpp::R_POWERPC_GOT_TLSLD16:
11780 case elfcpp::R_POWERPC_ADDR16_LO:
11781 case elfcpp::R_POWERPC_REL16_LO:
11782 case elfcpp::R_PPC64_TOC16_LO:
11783 case elfcpp::R_POWERPC_GOT16_LO:
11784 case elfcpp::R_POWERPC_PLT16_LO:
11785 case elfcpp::R_POWERPC_SECTOFF_LO:
11786 case elfcpp::R_POWERPC_TPREL16_LO:
11787 case elfcpp::R_POWERPC_DTPREL16_LO:
11788 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11789 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11790 if (size == 64)
11791 status = Reloc::addr16(view, value, overflow);
11792 else
11793 maybe_dq_reloc = true;
11794 break;
11796 case elfcpp::R_POWERPC_UADDR16:
11797 status = Reloc::addr16_u(view, value, overflow);
11798 break;
11800 case elfcpp::R_PPC64_ADDR16_HIGH:
11801 case elfcpp::R_PPC64_TPREL16_HIGH:
11802 case elfcpp::R_PPC64_DTPREL16_HIGH:
11803 if (size == 32)
11804 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11805 goto unsupp;
11806 // Fall through.
11807 case elfcpp::R_POWERPC_ADDR16_HI:
11808 case elfcpp::R_POWERPC_REL16_HI:
11809 case elfcpp::R_PPC64_REL16_HIGH:
11810 case elfcpp::R_PPC64_TOC16_HI:
11811 case elfcpp::R_POWERPC_GOT16_HI:
11812 case elfcpp::R_POWERPC_PLT16_HI:
11813 case elfcpp::R_POWERPC_SECTOFF_HI:
11814 case elfcpp::R_POWERPC_TPREL16_HI:
11815 case elfcpp::R_POWERPC_DTPREL16_HI:
11816 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11817 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11818 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11819 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11820 Reloc::addr16_hi(view, value);
11821 break;
11823 case elfcpp::R_PPC64_ADDR16_HIGHA:
11824 case elfcpp::R_PPC64_TPREL16_HIGHA:
11825 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11826 if (size == 32)
11827 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11828 goto unsupp;
11829 // Fall through.
11830 case elfcpp::R_POWERPC_ADDR16_HA:
11831 case elfcpp::R_POWERPC_REL16_HA:
11832 case elfcpp::R_PPC64_REL16_HIGHA:
11833 case elfcpp::R_PPC64_TOC16_HA:
11834 case elfcpp::R_POWERPC_GOT16_HA:
11835 case elfcpp::R_POWERPC_PLT16_HA:
11836 case elfcpp::R_POWERPC_SECTOFF_HA:
11837 case elfcpp::R_POWERPC_TPREL16_HA:
11838 case elfcpp::R_POWERPC_DTPREL16_HA:
11839 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11840 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11841 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11842 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11843 Reloc::addr16_ha(view, value);
11844 break;
11846 case elfcpp::R_POWERPC_REL16DX_HA:
11847 status = Reloc::addr16dx_ha(view, value, overflow);
11848 break;
11850 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11851 if (size == 32)
11852 // R_PPC_EMB_NADDR16_LO
11853 goto unsupp;
11854 // Fall through.
11855 case elfcpp::R_PPC64_ADDR16_HIGHER:
11856 case elfcpp::R_PPC64_REL16_HIGHER:
11857 case elfcpp::R_PPC64_TPREL16_HIGHER:
11858 Reloc::addr16_hi2(view, value);
11859 break;
11861 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11862 if (size == 32)
11863 // R_PPC_EMB_NADDR16_HI
11864 goto unsupp;
11865 // Fall through.
11866 case elfcpp::R_PPC64_ADDR16_HIGHERA:
11867 case elfcpp::R_PPC64_REL16_HIGHERA:
11868 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11869 Reloc::addr16_ha2(view, value);
11870 break;
11872 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11873 if (size == 32)
11874 // R_PPC_EMB_NADDR16_HA
11875 goto unsupp;
11876 // Fall through.
11877 case elfcpp::R_PPC64_ADDR16_HIGHEST:
11878 case elfcpp::R_PPC64_REL16_HIGHEST:
11879 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11880 Reloc::addr16_hi3(view, value);
11881 break;
11883 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11884 if (size == 32)
11885 // R_PPC_EMB_SDAI16
11886 goto unsupp;
11887 // Fall through.
11888 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
11889 case elfcpp::R_PPC64_REL16_HIGHESTA:
11890 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11891 Reloc::addr16_ha3(view, value);
11892 break;
11894 case elfcpp::R_PPC64_DTPREL16_DS:
11895 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11896 if (size == 32)
11897 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11898 goto unsupp;
11899 // Fall through.
11900 case elfcpp::R_PPC64_TPREL16_DS:
11901 case elfcpp::R_PPC64_TPREL16_LO_DS:
11902 if (size == 32)
11903 // R_PPC_TLSGD, R_PPC_TLSLD
11904 break;
11905 // Fall through.
11906 case elfcpp::R_PPC64_ADDR16_DS:
11907 case elfcpp::R_PPC64_ADDR16_LO_DS:
11908 case elfcpp::R_PPC64_TOC16_DS:
11909 case elfcpp::R_PPC64_TOC16_LO_DS:
11910 case elfcpp::R_PPC64_GOT16_DS:
11911 case elfcpp::R_PPC64_GOT16_LO_DS:
11912 case elfcpp::R_PPC64_PLT16_LO_DS:
11913 case elfcpp::R_PPC64_SECTOFF_DS:
11914 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11915 maybe_dq_reloc = true;
11916 break;
11918 case elfcpp::R_POWERPC_ADDR14:
11919 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11920 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11921 case elfcpp::R_POWERPC_REL14:
11922 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11923 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11924 status = Reloc::addr14(view, value, overflow);
11925 break;
11927 case elfcpp::R_POWERPC_COPY:
11928 case elfcpp::R_POWERPC_GLOB_DAT:
11929 case elfcpp::R_POWERPC_JMP_SLOT:
11930 case elfcpp::R_POWERPC_RELATIVE:
11931 case elfcpp::R_POWERPC_DTPMOD:
11932 case elfcpp::R_PPC64_JMP_IREL:
11933 case elfcpp::R_POWERPC_IRELATIVE:
11934 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11935 _("unexpected reloc %u in object file"),
11936 r_type);
11937 break;
11939 case elfcpp::R_PPC64_TOCSAVE:
11940 if (size == 32)
11941 // R_PPC_EMB_SDA21
11942 goto unsupp;
11943 else
11945 Symbol_location loc;
11946 loc.object = relinfo->object;
11947 loc.shndx = relinfo->data_shndx;
11948 loc.offset = rela.get_r_offset();
11949 const Tocsave_loc *tocsave = target->tocsave_loc();
11950 if (tocsave->find(loc) != tocsave->end())
11952 // If we've generated plt calls using this tocsave, then
11953 // the nop needs to be changed to save r2.
11954 Insn* iview = reinterpret_cast<Insn*>(view);
11955 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11956 elfcpp::Swap<32, big_endian>::
11957 writeval(iview, std_2_1 + target->stk_toc());
11960 break;
11962 case elfcpp::R_PPC_EMB_SDA2I16:
11963 case elfcpp::R_PPC_EMB_SDA2REL:
11964 if (size == 32)
11965 goto unsupp;
11966 // R_PPC64_TLSGD, R_PPC64_TLSLD
11967 break;
11969 case elfcpp::R_PPC64_D34:
11970 case elfcpp::R_PPC64_D34_LO:
11971 case elfcpp::R_PPC64_PCREL34:
11972 case elfcpp::R_PPC64_GOT_PCREL34:
11973 case elfcpp::R_PPC64_PLT_PCREL34:
11974 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11975 case elfcpp::R_PPC64_TPREL34:
11976 case elfcpp::R_PPC64_DTPREL34:
11977 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11978 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11979 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11980 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11981 if (size == 32)
11982 goto unsupp;
11983 status = Reloc::addr34(view, value, overflow);
11984 break;
11986 case elfcpp::R_PPC64_D34_HI30:
11987 if (size == 32)
11988 goto unsupp;
11989 Reloc::addr34_hi(view, value);
11990 break;
11992 case elfcpp::R_PPC64_D34_HA30:
11993 if (size == 32)
11994 goto unsupp;
11995 Reloc::addr34_ha(view, value);
11996 break;
11998 case elfcpp::R_PPC64_D28:
11999 case elfcpp::R_PPC64_PCREL28:
12000 if (size == 32)
12001 goto unsupp;
12002 status = Reloc::addr28(view, value, overflow);
12003 break;
12005 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12006 case elfcpp::R_PPC64_REL16_HIGHER34:
12007 if (size == 32)
12008 goto unsupp;
12009 Reloc::addr16_higher34(view, value);
12010 break;
12012 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12013 case elfcpp::R_PPC64_REL16_HIGHERA34:
12014 if (size == 32)
12015 goto unsupp;
12016 Reloc::addr16_highera34(view, value);
12017 break;
12019 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12020 case elfcpp::R_PPC64_REL16_HIGHEST34:
12021 if (size == 32)
12022 goto unsupp;
12023 Reloc::addr16_highest34(view, value);
12024 break;
12026 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12027 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12028 if (size == 32)
12029 goto unsupp;
12030 Reloc::addr16_highesta34(view, value);
12031 break;
12033 case elfcpp::R_POWERPC_PLT32:
12034 case elfcpp::R_POWERPC_PLTREL32:
12035 case elfcpp::R_PPC_SDAREL16:
12036 case elfcpp::R_POWERPC_ADDR30:
12037 case elfcpp::R_PPC64_PLT64:
12038 case elfcpp::R_PPC64_PLTREL64:
12039 case elfcpp::R_PPC64_PLTGOT16:
12040 case elfcpp::R_PPC64_PLTGOT16_LO:
12041 case elfcpp::R_PPC64_PLTGOT16_HI:
12042 case elfcpp::R_PPC64_PLTGOT16_HA:
12043 case elfcpp::R_PPC64_PLTGOT16_DS:
12044 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12045 case elfcpp::R_PPC_TOC16:
12046 default:
12047 unsupp:
12048 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12049 _("unsupported reloc %u"),
12050 r_type);
12051 break;
12054 if (maybe_dq_reloc)
12056 if (insn == 0)
12057 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12059 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12060 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12061 && (insn & 3) == 1))
12062 status = Reloc::addr16_dq(view, value, overflow);
12063 else if (size == 64
12064 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12065 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12066 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12067 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12068 status = Reloc::addr16_ds(view, value, overflow);
12069 else
12070 status = Reloc::addr16(view, value, overflow);
12073 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12074 && (has_stub_value
12075 || !(gsym != NULL
12076 && gsym->is_undefined()
12077 && is_branch_reloc<size>(r_type))))
12079 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12080 _("relocation overflow"));
12081 if (has_stub_value)
12082 gold_info(_("try relinking with a smaller --stub-group-size"));
12085 return true;
12088 // Relocate section data.
12090 template<int size, bool big_endian>
12091 void
12092 Target_powerpc<size, big_endian>::relocate_section(
12093 const Relocate_info<size, big_endian>* relinfo,
12094 unsigned int sh_type,
12095 const unsigned char* prelocs,
12096 size_t reloc_count,
12097 Output_section* output_section,
12098 bool needs_special_offset_handling,
12099 unsigned char* view,
12100 Address address,
12101 section_size_type view_size,
12102 const Reloc_symbol_changes* reloc_symbol_changes)
12104 typedef Target_powerpc<size, big_endian> Powerpc;
12105 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12106 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12107 Powerpc_comdat_behavior;
12108 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12109 Classify_reloc;
12111 gold_assert(sh_type == elfcpp::SHT_RELA);
12113 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12114 Powerpc_comdat_behavior, Classify_reloc>(
12115 relinfo,
12116 this,
12117 prelocs,
12118 reloc_count,
12119 output_section,
12120 needs_special_offset_handling,
12121 view,
12122 address,
12123 view_size,
12124 reloc_symbol_changes);
12127 template<int size, bool big_endian>
12128 class Powerpc_scan_relocatable_reloc
12130 public:
12131 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12132 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12133 static const int sh_type = elfcpp::SHT_RELA;
12135 // Return the symbol referred to by the relocation.
12136 static inline unsigned int
12137 get_r_sym(const Reltype* reloc)
12138 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12140 // Return the type of the relocation.
12141 static inline unsigned int
12142 get_r_type(const Reltype* reloc)
12143 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12145 // Return the strategy to use for a local symbol which is not a
12146 // section symbol, given the relocation type.
12147 inline Relocatable_relocs::Reloc_strategy
12148 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12150 if (r_type == 0 && r_sym == 0)
12151 return Relocatable_relocs::RELOC_DISCARD;
12152 return Relocatable_relocs::RELOC_COPY;
12155 // Return the strategy to use for a local symbol which is a section
12156 // symbol, given the relocation type.
12157 inline Relocatable_relocs::Reloc_strategy
12158 local_section_strategy(unsigned int, Relobj*)
12160 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12163 // Return the strategy to use for a global symbol, given the
12164 // relocation type, the object, and the symbol index.
12165 inline Relocatable_relocs::Reloc_strategy
12166 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12168 if (size == 32
12169 && (r_type == elfcpp::R_PPC_PLTREL24
12170 || r_type == elfcpp::R_POWERPC_PLT16_LO
12171 || r_type == elfcpp::R_POWERPC_PLT16_HI
12172 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12173 return Relocatable_relocs::RELOC_SPECIAL;
12174 return Relocatable_relocs::RELOC_COPY;
12178 // Scan the relocs during a relocatable link.
12180 template<int size, bool big_endian>
12181 void
12182 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12183 Symbol_table* symtab,
12184 Layout* layout,
12185 Sized_relobj_file<size, big_endian>* object,
12186 unsigned int data_shndx,
12187 unsigned int sh_type,
12188 const unsigned char* prelocs,
12189 size_t reloc_count,
12190 Output_section* output_section,
12191 bool needs_special_offset_handling,
12192 size_t local_symbol_count,
12193 const unsigned char* plocal_symbols,
12194 Relocatable_relocs* rr)
12196 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12198 gold_assert(sh_type == elfcpp::SHT_RELA);
12200 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12201 symtab,
12202 layout,
12203 object,
12204 data_shndx,
12205 prelocs,
12206 reloc_count,
12207 output_section,
12208 needs_special_offset_handling,
12209 local_symbol_count,
12210 plocal_symbols,
12211 rr);
12214 // Scan the relocs for --emit-relocs.
12216 template<int size, bool big_endian>
12217 void
12218 Target_powerpc<size, big_endian>::emit_relocs_scan(
12219 Symbol_table* symtab,
12220 Layout* layout,
12221 Sized_relobj_file<size, big_endian>* object,
12222 unsigned int data_shndx,
12223 unsigned int sh_type,
12224 const unsigned char* prelocs,
12225 size_t reloc_count,
12226 Output_section* output_section,
12227 bool needs_special_offset_handling,
12228 size_t local_symbol_count,
12229 const unsigned char* plocal_syms,
12230 Relocatable_relocs* rr)
12232 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12233 Classify_reloc;
12234 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12235 Emit_relocs_strategy;
12237 gold_assert(sh_type == elfcpp::SHT_RELA);
12239 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12240 symtab,
12241 layout,
12242 object,
12243 data_shndx,
12244 prelocs,
12245 reloc_count,
12246 output_section,
12247 needs_special_offset_handling,
12248 local_symbol_count,
12249 plocal_syms,
12250 rr);
12253 // Emit relocations for a section.
12254 // This is a modified version of the function by the same name in
12255 // target-reloc.h. Using relocate_special_relocatable for
12256 // R_PPC_PLTREL24 would require duplication of the entire body of the
12257 // loop, so we may as well duplicate the whole thing.
12259 template<int size, bool big_endian>
12260 void
12261 Target_powerpc<size, big_endian>::relocate_relocs(
12262 const Relocate_info<size, big_endian>* relinfo,
12263 unsigned int sh_type,
12264 const unsigned char* prelocs,
12265 size_t reloc_count,
12266 Output_section* output_section,
12267 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12268 unsigned char*,
12269 Address view_address,
12270 section_size_type,
12271 unsigned char* reloc_view,
12272 section_size_type reloc_view_size)
12274 gold_assert(sh_type == elfcpp::SHT_RELA);
12276 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12277 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12278 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12279 // Offset from start of insn to d-field reloc.
12280 const int d_offset = big_endian ? 2 : 0;
12282 Powerpc_relobj<size, big_endian>* const object
12283 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12284 const unsigned int local_count = object->local_symbol_count();
12285 unsigned int got2_shndx = object->got2_shndx();
12286 Address got2_addend = 0;
12287 if (got2_shndx != 0)
12289 got2_addend = object->get_output_section_offset(got2_shndx);
12290 gold_assert(got2_addend != invalid_address);
12293 const bool relocatable = parameters->options().relocatable();
12295 unsigned char* pwrite = reloc_view;
12296 bool zap_next = false;
12297 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12299 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12300 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12301 continue;
12303 Reltype reloc(prelocs);
12304 Reltype_write reloc_write(pwrite);
12306 Address offset = reloc.get_r_offset();
12307 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12308 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12309 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12310 const unsigned int orig_r_sym = r_sym;
12311 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12312 = reloc.get_r_addend();
12313 const Symbol* gsym = NULL;
12315 if (zap_next)
12317 // We could arrange to discard these and other relocs for
12318 // tls optimised sequences in the strategy methods, but for
12319 // now do as BFD ld does.
12320 r_type = elfcpp::R_POWERPC_NONE;
12321 zap_next = false;
12324 // Get the new symbol index.
12325 Output_section* os = NULL;
12326 if (r_sym < local_count)
12328 switch (strategy)
12330 case Relocatable_relocs::RELOC_COPY:
12331 case Relocatable_relocs::RELOC_SPECIAL:
12332 if (r_sym != 0)
12334 r_sym = object->symtab_index(r_sym);
12335 gold_assert(r_sym != -1U);
12337 break;
12339 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12341 // We are adjusting a section symbol. We need to find
12342 // the symbol table index of the section symbol for
12343 // the output section corresponding to input section
12344 // in which this symbol is defined.
12345 gold_assert(r_sym < local_count);
12346 bool is_ordinary;
12347 unsigned int shndx =
12348 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12349 gold_assert(is_ordinary);
12350 os = object->output_section(shndx);
12351 gold_assert(os != NULL);
12352 gold_assert(os->needs_symtab_index());
12353 r_sym = os->symtab_index();
12355 break;
12357 default:
12358 gold_unreachable();
12361 else
12363 gsym = object->global_symbol(r_sym);
12364 gold_assert(gsym != NULL);
12365 if (gsym->is_forwarder())
12366 gsym = relinfo->symtab->resolve_forwards(gsym);
12368 gold_assert(gsym->has_symtab_index());
12369 r_sym = gsym->symtab_index();
12372 // Get the new offset--the location in the output section where
12373 // this relocation should be applied.
12374 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12375 offset += offset_in_output_section;
12376 else
12378 section_offset_type sot_offset =
12379 convert_types<section_offset_type, Address>(offset);
12380 section_offset_type new_sot_offset =
12381 output_section->output_offset(object, relinfo->data_shndx,
12382 sot_offset);
12383 gold_assert(new_sot_offset != -1);
12384 offset = new_sot_offset;
12387 // In an object file, r_offset is an offset within the section.
12388 // In an executable or dynamic object, generated by
12389 // --emit-relocs, r_offset is an absolute address.
12390 if (!relocatable)
12392 offset += view_address;
12393 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12394 offset -= offset_in_output_section;
12397 // Handle the reloc addend based on the strategy.
12398 if (strategy == Relocatable_relocs::RELOC_COPY)
12400 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12402 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12403 addend = psymval->value(object, addend);
12404 // In a relocatable link, the symbol value is relative to
12405 // the start of the output section. For a non-relocatable
12406 // link, we need to adjust the addend.
12407 if (!relocatable)
12409 gold_assert(os != NULL);
12410 addend -= os->address();
12413 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12415 if (size == 32)
12417 if (addend >= 32768)
12418 addend += got2_addend;
12420 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12422 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12423 addend -= d_offset;
12425 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12427 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12428 addend -= d_offset + 4;
12431 else
12432 gold_unreachable();
12434 if (!relocatable)
12436 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12437 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12438 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12439 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12441 // First instruction of a global dynamic sequence,
12442 // arg setup insn.
12443 const bool final = gsym == NULL || gsym->final_value_is_known();
12444 switch (this->optimize_tls_gd(final))
12446 case tls::TLSOPT_TO_IE:
12447 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12448 - elfcpp::R_POWERPC_GOT_TLSGD16);
12449 break;
12450 case tls::TLSOPT_TO_LE:
12451 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12452 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12453 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12454 else
12456 r_type = elfcpp::R_POWERPC_NONE;
12457 offset -= d_offset;
12459 break;
12460 default:
12461 break;
12464 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12465 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12466 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12467 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12469 // First instruction of a local dynamic sequence,
12470 // arg setup insn.
12471 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12473 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12474 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12476 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12477 const Output_section* os = relinfo->layout->tls_segment()
12478 ->first_section();
12479 gold_assert(os != NULL);
12480 gold_assert(os->needs_symtab_index());
12481 r_sym = os->symtab_index();
12482 addend = dtp_offset;
12484 else
12486 r_type = elfcpp::R_POWERPC_NONE;
12487 offset -= d_offset;
12491 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12492 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12493 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12494 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12496 // First instruction of initial exec sequence.
12497 const bool final = gsym == NULL || gsym->final_value_is_known();
12498 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12500 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12501 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12502 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12503 else
12505 r_type = elfcpp::R_POWERPC_NONE;
12506 offset -= d_offset;
12510 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12511 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12513 // Second instruction of a global dynamic sequence,
12514 // the __tls_get_addr call
12515 const bool final = gsym == NULL || gsym->final_value_is_known();
12516 switch (this->optimize_tls_gd(final))
12518 case tls::TLSOPT_TO_IE:
12519 r_type = elfcpp::R_POWERPC_NONE;
12520 zap_next = true;
12521 break;
12522 case tls::TLSOPT_TO_LE:
12523 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12524 offset += d_offset;
12525 zap_next = true;
12526 break;
12527 default:
12528 break;
12531 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12532 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12534 // Second instruction of a local dynamic sequence,
12535 // the __tls_get_addr call
12536 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12538 const Output_section* os = relinfo->layout->tls_segment()
12539 ->first_section();
12540 gold_assert(os != NULL);
12541 gold_assert(os->needs_symtab_index());
12542 r_sym = os->symtab_index();
12543 addend = dtp_offset;
12544 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12545 offset += d_offset;
12546 zap_next = true;
12549 else if (r_type == elfcpp::R_POWERPC_TLS)
12551 // Second instruction of an initial exec sequence
12552 const bool final = gsym == NULL || gsym->final_value_is_known();
12553 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12555 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12556 offset += d_offset;
12561 reloc_write.put_r_offset(offset);
12562 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12563 reloc_write.put_r_addend(addend);
12565 pwrite += reloc_size;
12568 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12569 == reloc_view_size);
12572 // Return the value to use for a dynamic symbol which requires special
12573 // treatment. This is how we support equality comparisons of function
12574 // pointers across shared library boundaries, as described in the
12575 // processor specific ABI supplement.
12577 template<int size, bool big_endian>
12578 uint64_t
12579 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12581 if (size == 32)
12583 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12584 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12585 p != this->stub_tables_.end();
12586 ++p)
12588 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12589 = (*p)->find_plt_call_entry(gsym);
12590 if (ent != NULL)
12591 return (*p)->stub_address() + ent->off_;
12594 else if (this->abiversion() >= 2)
12596 Address off = this->glink_section()->find_global_entry(gsym);
12597 if (off != invalid_address)
12598 return this->glink_section()->global_entry_address() + off;
12600 gold_unreachable();
12603 // Return the PLT address to use for a local symbol.
12604 template<int size, bool big_endian>
12605 uint64_t
12606 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12607 const Relobj* object,
12608 unsigned int symndx) const
12610 if (size == 32)
12612 const Sized_relobj<size, big_endian>* relobj
12613 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12614 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12615 p != this->stub_tables_.end();
12616 ++p)
12618 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12619 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12620 if (ent != NULL)
12621 return (*p)->stub_address() + ent->off_;
12624 gold_unreachable();
12627 // Return the PLT address to use for a global symbol.
12628 template<int size, bool big_endian>
12629 uint64_t
12630 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12631 const Symbol* gsym) const
12633 if (size == 32)
12635 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12636 p != this->stub_tables_.end();
12637 ++p)
12639 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12640 = (*p)->find_plt_call_entry(gsym);
12641 if (ent != NULL)
12642 return (*p)->stub_address() + ent->off_;
12645 else if (this->abiversion() >= 2)
12647 Address off = this->glink_section()->find_global_entry(gsym);
12648 if (off != invalid_address)
12649 return this->glink_section()->global_entry_address() + off;
12651 gold_unreachable();
12654 // Return the offset to use for the GOT_INDX'th got entry which is
12655 // for a local tls symbol specified by OBJECT, SYMNDX.
12656 template<int size, bool big_endian>
12657 int64_t
12658 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12659 const Relobj* object,
12660 unsigned int symndx,
12661 unsigned int got_indx) const
12663 const Powerpc_relobj<size, big_endian>* ppc_object
12664 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12665 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12667 for (Got_type got_type = GOT_TYPE_TLSGD;
12668 got_type <= GOT_TYPE_TPREL;
12669 got_type = Got_type(got_type + 1))
12670 if (ppc_object->local_has_got_offset(symndx, got_type))
12672 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12673 if (got_type == GOT_TYPE_TLSGD)
12674 off += size / 8;
12675 if (off == got_indx * (size / 8))
12677 if (got_type == GOT_TYPE_TPREL)
12678 return -tp_offset;
12679 else
12680 return -dtp_offset;
12684 gold_unreachable();
12687 // Return the offset to use for the GOT_INDX'th got entry which is
12688 // for global tls symbol GSYM.
12689 template<int size, bool big_endian>
12690 int64_t
12691 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12692 Symbol* gsym,
12693 unsigned int got_indx) const
12695 if (gsym->type() == elfcpp::STT_TLS)
12697 for (Got_type got_type = GOT_TYPE_TLSGD;
12698 got_type <= GOT_TYPE_TPREL;
12699 got_type = Got_type(got_type + 1))
12700 if (gsym->has_got_offset(got_type))
12702 unsigned int off = gsym->got_offset(got_type);
12703 if (got_type == GOT_TYPE_TLSGD)
12704 off += size / 8;
12705 if (off == got_indx * (size / 8))
12707 if (got_type == GOT_TYPE_TPREL)
12708 return -tp_offset;
12709 else
12710 return -dtp_offset;
12714 gold_unreachable();
12717 // The selector for powerpc object files.
12719 template<int size, bool big_endian>
12720 class Target_selector_powerpc : public Target_selector
12722 public:
12723 Target_selector_powerpc()
12724 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12725 size, big_endian,
12726 (size == 64
12727 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12728 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12729 (size == 64
12730 ? (big_endian ? "elf64ppc" : "elf64lppc")
12731 : (big_endian ? "elf32ppc" : "elf32lppc")))
12734 virtual Target*
12735 do_instantiate_target()
12736 { return new Target_powerpc<size, big_endian>(); }
12739 Target_selector_powerpc<32, true> target_selector_ppc32;
12740 Target_selector_powerpc<32, false> target_selector_ppc32le;
12741 Target_selector_powerpc<64, true> target_selector_ppc64;
12742 Target_selector_powerpc<64, false> target_selector_ppc64le;
12744 // Instantiate these constants for -O0
12745 template<int size, bool big_endian>
12746 const typename Output_data_glink<size, big_endian>::Address
12747 Output_data_glink<size, big_endian>::invalid_address;
12748 template<int size, bool big_endian>
12749 const typename Stub_table<size, big_endian>::Address
12750 Stub_table<size, big_endian>::invalid_address;
12751 template<int size, bool big_endian>
12752 const typename Target_powerpc<size, big_endian>::Address
12753 Target_powerpc<size, big_endian>::invalid_address;
12755 } // End anonymous namespace.