PR27625, powerpc64 gold __tls_get_addr calls
[deliverable/binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
6
7 // This file is part of gold.
8
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
23
24 #include "gold.h"
25
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"
45
46 namespace
47 {
48
49 using namespace gold;
50
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
53
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
56
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
59
60 template<int size, bool big_endian>
61 class Output_data_glink;
62
63 template<int size, bool big_endian>
64 class Stub_table;
65
66 template<int size, bool big_endian>
67 class Output_data_save_res;
68
69 template<int size, bool big_endian>
70 class Target_powerpc;
71
72 struct Stub_table_owner
73 {
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80 };
81
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
84
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
87
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
90
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93 {
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;
98
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 no_tls_marker_(false), tls_marker_(false), tls_opt_error_(false),
105 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
106 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
107 attributes_section_data_(NULL)
108 {
109 this->set_abiversion(0);
110 }
111
112 ~Powerpc_relobj()
113 { delete this->attributes_section_data_; }
114
115 // Read the symbols then set up st_other vector.
116 void
117 do_read_symbols(Read_symbols_data*);
118
119 // Arrange to always relocate .toc first.
120 virtual void
121 do_relocate_sections(
122 const Symbol_table* symtab, const Layout* layout,
123 const unsigned char* pshdrs, Output_file* of,
124 typename Sized_relobj_file<size, big_endian>::Views* pviews);
125
126 // The .toc section index.
127 unsigned int
128 toc_shndx() const
129 {
130 return this->toc_;
131 }
132
133 // Mark .toc entry at OFF as not optimizable.
134 void
135 set_no_toc_opt(Address off)
136 {
137 if (this->no_toc_opt_.empty())
138 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
139 / (size / 8));
140 off /= size / 8;
141 if (off < this->no_toc_opt_.size())
142 this->no_toc_opt_[off] = true;
143 }
144
145 // Mark the entire .toc as not optimizable.
146 void
147 set_no_toc_opt()
148 {
149 this->no_toc_opt_.resize(1);
150 this->no_toc_opt_[0] = true;
151 }
152
153 // Return true if code using the .toc entry at OFF should not be edited.
154 bool
155 no_toc_opt(Address off) const
156 {
157 if (this->no_toc_opt_.empty())
158 return false;
159 off /= size / 8;
160 if (off >= this->no_toc_opt_.size())
161 return true;
162 return this->no_toc_opt_[off];
163 }
164
165 void
166 set_no_tls_marker()
167 {
168 if (!this->no_tls_marker_ && this->tls_marker_)
169 this->tls_opt_error_ = true;
170 this->no_tls_marker_ = true;
171 }
172
173 bool
174 no_tls_marker() const
175 { return this->no_tls_marker_; }
176
177 void
178 set_tls_marker()
179 { this->tls_marker_ = true; }
180
181 bool
182 tls_marker() const
183 { return this->tls_marker_; }
184
185 bool
186 tls_opt_error() const
187 { return this->tls_opt_error_; }
188
189 // The .got2 section shndx.
190 unsigned int
191 got2_shndx() const
192 {
193 if (size == 32)
194 return this->special_;
195 else
196 return 0;
197 }
198
199 // The .opd section shndx.
200 unsigned int
201 opd_shndx() const
202 {
203 if (size == 32)
204 return 0;
205 else
206 return this->special_;
207 }
208
209 // Init OPD entry arrays.
210 void
211 init_opd(size_t opd_size)
212 {
213 size_t count = this->opd_ent_ndx(opd_size);
214 this->opd_ent_.resize(count);
215 }
216
217 // Return section and offset of function entry for .opd + R_OFF.
218 unsigned int
219 get_opd_ent(Address r_off, Address* value = NULL) const
220 {
221 size_t ndx = this->opd_ent_ndx(r_off);
222 gold_assert(ndx < this->opd_ent_.size());
223 gold_assert(this->opd_ent_[ndx].shndx != 0);
224 if (value != NULL)
225 *value = this->opd_ent_[ndx].off;
226 return this->opd_ent_[ndx].shndx;
227 }
228
229 // Set section and offset of function entry for .opd + R_OFF.
230 void
231 set_opd_ent(Address r_off, unsigned int shndx, Address value)
232 {
233 size_t ndx = this->opd_ent_ndx(r_off);
234 gold_assert(ndx < this->opd_ent_.size());
235 this->opd_ent_[ndx].shndx = shndx;
236 this->opd_ent_[ndx].off = value;
237 }
238
239 // Return discard flag for .opd + R_OFF.
240 bool
241 get_opd_discard(Address r_off) const
242 {
243 size_t ndx = this->opd_ent_ndx(r_off);
244 gold_assert(ndx < this->opd_ent_.size());
245 return this->opd_ent_[ndx].discard;
246 }
247
248 // Set discard flag for .opd + R_OFF.
249 void
250 set_opd_discard(Address r_off)
251 {
252 size_t ndx = this->opd_ent_ndx(r_off);
253 gold_assert(ndx < this->opd_ent_.size());
254 this->opd_ent_[ndx].discard = true;
255 }
256
257 bool
258 opd_valid() const
259 { return this->opd_valid_; }
260
261 void
262 set_opd_valid()
263 { this->opd_valid_ = true; }
264
265 // Examine .rela.opd to build info about function entry points.
266 void
267 scan_opd_relocs(size_t reloc_count,
268 const unsigned char* prelocs,
269 const unsigned char* plocal_syms);
270
271 // Returns true if a code sequence loading a TOC entry can be
272 // converted into code calculating a TOC pointer relative offset.
273 bool
274 make_toc_relative(Target_powerpc<size, big_endian>* target,
275 Address* value);
276
277 bool
278 make_got_relative(Target_powerpc<size, big_endian>* target,
279 const Symbol_value<size>* psymval,
280 Address addend,
281 Address* value);
282
283 // Perform the Sized_relobj_file method, then set up opd info from
284 // .opd relocs.
285 void
286 do_read_relocs(Read_relocs_data*);
287
288 bool
289 do_find_special_sections(Read_symbols_data* sd);
290
291 // Adjust this local symbol value. Return false if the symbol
292 // should be discarded from the output file.
293 bool
294 do_adjust_local_symbol(Symbol_value<size>* lv) const
295 {
296 if (size == 64 && this->opd_shndx() != 0)
297 {
298 bool is_ordinary;
299 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
300 return true;
301 if (this->get_opd_discard(lv->input_value()))
302 return false;
303 }
304 return true;
305 }
306
307 Access_from*
308 access_from_map()
309 { return &this->access_from_map_; }
310
311 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
312 // section at DST_OFF.
313 void
314 add_reference(Relobj* src_obj,
315 unsigned int src_indx,
316 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
317 {
318 Section_id src_id(src_obj, src_indx);
319 this->access_from_map_[dst_off].insert(src_id);
320 }
321
322 // Add a reference to the code section specified by the .opd entry
323 // at DST_OFF
324 void
325 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
326 {
327 size_t ndx = this->opd_ent_ndx(dst_off);
328 if (ndx >= this->opd_ent_.size())
329 this->opd_ent_.resize(ndx + 1);
330 this->opd_ent_[ndx].gc_mark = true;
331 }
332
333 void
334 process_gc_mark(Symbol_table* symtab)
335 {
336 for (size_t i = 0; i < this->opd_ent_.size(); i++)
337 if (this->opd_ent_[i].gc_mark)
338 {
339 unsigned int shndx = this->opd_ent_[i].shndx;
340 symtab->gc()->worklist().push_back(Section_id(this, shndx));
341 }
342 }
343
344 // Return offset in output GOT section that this object will use
345 // as a TOC pointer. Won't be just a constant with multi-toc support.
346 Address
347 toc_base_offset() const
348 { return 0x8000; }
349
350 void
351 set_has_small_toc_reloc()
352 { has_small_toc_reloc_ = true; }
353
354 bool
355 has_small_toc_reloc() const
356 { return has_small_toc_reloc_; }
357
358 void
359 set_has_14bit_branch(unsigned int shndx)
360 {
361 if (shndx >= this->has14_.size())
362 this->has14_.resize(shndx + 1);
363 this->has14_[shndx] = true;
364 }
365
366 bool
367 has_14bit_branch(unsigned int shndx) const
368 { return shndx < this->has14_.size() && this->has14_[shndx]; }
369
370 void
371 set_stub_table(unsigned int shndx, unsigned int stub_index)
372 {
373 if (shndx >= this->stub_table_index_.size())
374 this->stub_table_index_.resize(shndx + 1, -1);
375 this->stub_table_index_[shndx] = stub_index;
376 }
377
378 Stub_table<size, big_endian>*
379 stub_table(unsigned int shndx)
380 {
381 if (shndx < this->stub_table_index_.size())
382 {
383 Target_powerpc<size, big_endian>* target
384 = static_cast<Target_powerpc<size, big_endian>*>(
385 parameters->sized_target<size, big_endian>());
386 unsigned int indx = this->stub_table_index_[shndx];
387 if (indx < target->stub_tables().size())
388 return target->stub_tables()[indx];
389 }
390 return NULL;
391 }
392
393 void
394 clear_stub_table()
395 {
396 this->stub_table_index_.clear();
397 }
398
399 uint32_t
400 uniq() const
401 { return this->uniq_; }
402
403 int
404 abiversion() const
405 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
406
407 // Set ABI version for input and output
408 void
409 set_abiversion(int ver);
410
411 unsigned int
412 st_other (unsigned int symndx) const
413 {
414 return this->st_other_[symndx];
415 }
416
417 unsigned int
418 ppc64_local_entry_offset(const Symbol* sym) const
419 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
420
421 unsigned int
422 ppc64_local_entry_offset(unsigned int symndx) const
423 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
424
425 bool
426 ppc64_needs_toc(const Symbol* sym) const
427 { return sym->nonvis() > 1 << 3; }
428
429 bool
430 ppc64_needs_toc(unsigned int symndx) const
431 { return this->st_other_[symndx] > 1 << 5; }
432
433 // The contents of the .gnu.attributes section if there is one.
434 const Attributes_section_data*
435 attributes_section_data() const
436 { return this->attributes_section_data_; }
437
438 private:
439 struct Opd_ent
440 {
441 unsigned int shndx;
442 bool discard : 1;
443 bool gc_mark : 1;
444 Address off;
445 };
446
447 // Return index into opd_ent_ array for .opd entry at OFF.
448 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
449 // apart when the language doesn't use the last 8-byte word, the
450 // environment pointer. Thus dividing the entry section offset by
451 // 16 will give an index into opd_ent_ that works for either layout
452 // of .opd. (It leaves some elements of the vector unused when .opd
453 // entries are spaced 24 bytes apart, but we don't know the spacing
454 // until relocations are processed, and in any case it is possible
455 // for an object to have some entries spaced 16 bytes apart and
456 // others 24 bytes apart.)
457 size_t
458 opd_ent_ndx(size_t off) const
459 { return off >> 4;}
460
461 // Per object unique identifier
462 uint32_t uniq_;
463
464 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
465 unsigned int special_;
466
467 // For 64-bit the .rela.toc and .toc section shdnx.
468 unsigned int relatoc_;
469 unsigned int toc_;
470
471 // For 64-bit, whether this object uses small model relocs to access
472 // the toc.
473 bool has_small_toc_reloc_;
474
475 // Set at the start of gc_process_relocs, when we know opd_ent_
476 // vector is valid. The flag could be made atomic and set in
477 // do_read_relocs with memory_order_release and then tested with
478 // memory_order_acquire, potentially resulting in fewer entries in
479 // access_from_map_.
480 bool opd_valid_;
481
482 // Set when finding a __tls_get_addr call without marker relocs.
483 // Such a call disables GD and LD tls optimisations for the object file.
484 bool no_tls_marker_;
485
486 // Set when finding a __tls_get_addr call with marker relocs, or
487 // when finding a relocation that needs __tls_get_addr calls with
488 // marker relocs.
489 bool tls_marker_;
490
491 // Set when seeing a __tls_get_addr call without marker relocs after
492 // seeing some __tls_get_addr calls with marker relocs.
493 bool tls_opt_error_;
494
495 // Header e_flags
496 elfcpp::Elf_Word e_flags_;
497
498 // For 64-bit, an array with one entry per 64-bit word in the .toc
499 // section, set if accesses using that word cannot be optimised.
500 std::vector<bool> no_toc_opt_;
501
502 // The first 8-byte word of an OPD entry gives the address of the
503 // entry point of the function. Relocatable object files have a
504 // relocation on this word. The following vector records the
505 // section and offset specified by these relocations.
506 std::vector<Opd_ent> opd_ent_;
507
508 // References made to this object's .opd section when running
509 // gc_process_relocs for another object, before the opd_ent_ vector
510 // is valid for this object.
511 Access_from access_from_map_;
512
513 // Whether input section has a 14-bit branch reloc.
514 std::vector<bool> has14_;
515
516 // The stub table to use for a given input section.
517 std::vector<unsigned int> stub_table_index_;
518
519 // ELF st_other field for local symbols.
520 std::vector<unsigned char> st_other_;
521
522 // Object attributes if there is a .gnu.attributes section.
523 Attributes_section_data* attributes_section_data_;
524 };
525
526 template<int size, bool big_endian>
527 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
528 {
529 public:
530 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
531
532 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
533 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
534 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
535 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
536 attributes_section_data_(NULL)
537 {
538 this->set_abiversion(0);
539 }
540
541 ~Powerpc_dynobj()
542 { delete this->attributes_section_data_; }
543
544 // Call Sized_dynobj::do_read_symbols to read the symbols then
545 // read .opd from a dynamic object, filling in opd_ent_ vector,
546 void
547 do_read_symbols(Read_symbols_data*);
548
549 // The .opd section shndx.
550 unsigned int
551 opd_shndx() const
552 {
553 return this->opd_shndx_;
554 }
555
556 // The .opd section address.
557 Address
558 opd_address() const
559 {
560 return this->opd_address_;
561 }
562
563 // Init OPD entry arrays.
564 void
565 init_opd(size_t opd_size)
566 {
567 size_t count = this->opd_ent_ndx(opd_size);
568 this->opd_ent_.resize(count);
569 }
570
571 // Return section and offset of function entry for .opd + R_OFF.
572 unsigned int
573 get_opd_ent(Address r_off, Address* value = NULL) const
574 {
575 size_t ndx = this->opd_ent_ndx(r_off);
576 gold_assert(ndx < this->opd_ent_.size());
577 gold_assert(this->opd_ent_[ndx].shndx != 0);
578 if (value != NULL)
579 *value = this->opd_ent_[ndx].off;
580 return this->opd_ent_[ndx].shndx;
581 }
582
583 // Set section and offset of function entry for .opd + R_OFF.
584 void
585 set_opd_ent(Address r_off, unsigned int shndx, Address value)
586 {
587 size_t ndx = this->opd_ent_ndx(r_off);
588 gold_assert(ndx < this->opd_ent_.size());
589 this->opd_ent_[ndx].shndx = shndx;
590 this->opd_ent_[ndx].off = value;
591 }
592
593 int
594 abiversion() const
595 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
596
597 // Set ABI version for input and output.
598 void
599 set_abiversion(int ver);
600
601 // The contents of the .gnu.attributes section if there is one.
602 const Attributes_section_data*
603 attributes_section_data() const
604 { return this->attributes_section_data_; }
605
606 private:
607 // Used to specify extent of executable sections.
608 struct Sec_info
609 {
610 Sec_info(Address start_, Address len_, unsigned int shndx_)
611 : start(start_), len(len_), shndx(shndx_)
612 { }
613
614 bool
615 operator<(const Sec_info& that) const
616 { return this->start < that.start; }
617
618 Address start;
619 Address len;
620 unsigned int shndx;
621 };
622
623 struct Opd_ent
624 {
625 unsigned int shndx;
626 Address off;
627 };
628
629 // Return index into opd_ent_ array for .opd entry at OFF.
630 size_t
631 opd_ent_ndx(size_t off) const
632 { return off >> 4;}
633
634 // For 64-bit the .opd section shndx and address.
635 unsigned int opd_shndx_;
636 Address opd_address_;
637
638 // Header e_flags
639 elfcpp::Elf_Word e_flags_;
640
641 // The first 8-byte word of an OPD entry gives the address of the
642 // entry point of the function. Records the section and offset
643 // corresponding to the address. Note that in dynamic objects,
644 // offset is *not* relative to the section.
645 std::vector<Opd_ent> opd_ent_;
646
647 // Object attributes if there is a .gnu.attributes section.
648 Attributes_section_data* attributes_section_data_;
649 };
650
651 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
652 // base class will emit.
653
654 template<int sh_type, int size, bool big_endian>
655 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
656 {
657 public:
658 Powerpc_copy_relocs()
659 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
660 { }
661
662 // Emit any saved relocations which turn out to be needed. This is
663 // called after all the relocs have been scanned.
664 void
665 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
666 };
667
668 template<int size, bool big_endian>
669 class Target_powerpc : public Sized_target<size, big_endian>
670 {
671 public:
672 typedef
673 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
674 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
675 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
676 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
677 static const Address invalid_address = static_cast<Address>(0) - 1;
678 // Offset of tp and dtp pointers from start of TLS block.
679 static const Address tp_offset = 0x7000;
680 static const Address dtp_offset = 0x8000;
681
682 Target_powerpc()
683 : Sized_target<size, big_endian>(&powerpc_info),
684 got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
685 glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
686 tlsld_got_offset_(-1U),
687 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
688 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
689 plt_localentry0_init_(false), has_localentry0_(false),
690 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
691 relax_failed_(false), relax_fail_count_(0),
692 stub_group_size_(0), savres_section_(0),
693 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
694 attributes_section_data_(NULL),
695 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
696 {
697 }
698
699 // Process the relocations to determine unreferenced sections for
700 // garbage collection.
701 void
702 gc_process_relocs(Symbol_table* symtab,
703 Layout* layout,
704 Sized_relobj_file<size, big_endian>* object,
705 unsigned int data_shndx,
706 unsigned int sh_type,
707 const unsigned char* prelocs,
708 size_t reloc_count,
709 Output_section* output_section,
710 bool needs_special_offset_handling,
711 size_t local_symbol_count,
712 const unsigned char* plocal_symbols);
713
714 // Scan the relocations to look for symbol adjustments.
715 void
716 scan_relocs(Symbol_table* symtab,
717 Layout* layout,
718 Sized_relobj_file<size, big_endian>* object,
719 unsigned int data_shndx,
720 unsigned int sh_type,
721 const unsigned char* prelocs,
722 size_t reloc_count,
723 Output_section* output_section,
724 bool needs_special_offset_handling,
725 size_t local_symbol_count,
726 const unsigned char* plocal_symbols);
727
728 // Map input .toc section to output .got section.
729 const char*
730 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
731 {
732 if (size == 64 && strcmp(name, ".toc") == 0)
733 {
734 *plen = 4;
735 return ".got";
736 }
737 return NULL;
738 }
739
740 // Provide linker defined save/restore functions.
741 void
742 define_save_restore_funcs(Layout*, Symbol_table*);
743
744 // No stubs unless a final link.
745 bool
746 do_may_relax() const
747 { return !parameters->options().relocatable(); }
748
749 bool
750 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
751
752 void
753 do_plt_fde_location(const Output_data*, unsigned char*,
754 uint64_t*, off_t*) const;
755
756 // Stash info about branches, for stub generation.
757 void
758 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
759 unsigned int data_shndx, Address r_offset,
760 unsigned int r_type, unsigned int r_sym, Address addend)
761 {
762 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
763 this->branch_info_.push_back(info);
764 if (r_type == elfcpp::R_POWERPC_REL14
765 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
766 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
767 ppc_object->set_has_14bit_branch(data_shndx);
768 }
769
770 // Return whether the last branch is a plt call, and if so, mark the
771 // branch as having an R_PPC64_TOCSAVE.
772 bool
773 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
774 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
775 {
776 return (size == 64
777 && !this->branch_info_.empty()
778 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
779 r_offset, this, symtab));
780 }
781
782 // Say the given location, that of a nop in a function prologue with
783 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
784 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
785 void
786 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
787 unsigned int shndx, Address offset)
788 {
789 Symbol_location loc;
790 loc.object = ppc_object;
791 loc.shndx = shndx;
792 loc.offset = offset;
793 this->tocsave_loc_.insert(loc);
794 }
795
796 // Accessor
797 const Tocsave_loc*
798 tocsave_loc() const
799 {
800 return &this->tocsave_loc_;
801 }
802
803 void
804 do_define_standard_symbols(Symbol_table*, Layout*);
805
806 // Finalize the sections.
807 void
808 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
809
810 // Return the value to use for a dynamic which requires special
811 // treatment.
812 uint64_t
813 do_dynsym_value(const Symbol*) const;
814
815 // Return the PLT address to use for a local symbol.
816 uint64_t
817 do_plt_address_for_local(const Relobj*, unsigned int) const;
818
819 // Return the PLT address to use for a global symbol.
820 uint64_t
821 do_plt_address_for_global(const Symbol*) const;
822
823 // Return the offset to use for the GOT_INDX'th got entry which is
824 // for a local tls symbol specified by OBJECT, SYMNDX.
825 int64_t
826 do_tls_offset_for_local(const Relobj* object,
827 unsigned int symndx,
828 unsigned int got_indx) const;
829
830 // Return the offset to use for the GOT_INDX'th got entry which is
831 // for global tls symbol GSYM.
832 int64_t
833 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
834
835 void
836 do_function_location(Symbol_location*) const;
837
838 bool
839 do_can_check_for_function_pointers() const
840 { return true; }
841
842 // Adjust -fsplit-stack code which calls non-split-stack code.
843 void
844 do_calls_non_split(Relobj* object, unsigned int shndx,
845 section_offset_type fnoffset, section_size_type fnsize,
846 const unsigned char* prelocs, size_t reloc_count,
847 unsigned char* view, section_size_type view_size,
848 std::string* from, std::string* to) const;
849
850 // Relocate a section.
851 void
852 relocate_section(const Relocate_info<size, big_endian>*,
853 unsigned int sh_type,
854 const unsigned char* prelocs,
855 size_t reloc_count,
856 Output_section* output_section,
857 bool needs_special_offset_handling,
858 unsigned char* view,
859 Address view_address,
860 section_size_type view_size,
861 const Reloc_symbol_changes*);
862
863 // Scan the relocs during a relocatable link.
864 void
865 scan_relocatable_relocs(Symbol_table* symtab,
866 Layout* layout,
867 Sized_relobj_file<size, big_endian>* object,
868 unsigned int data_shndx,
869 unsigned int sh_type,
870 const unsigned char* prelocs,
871 size_t reloc_count,
872 Output_section* output_section,
873 bool needs_special_offset_handling,
874 size_t local_symbol_count,
875 const unsigned char* plocal_symbols,
876 Relocatable_relocs*);
877
878 // Scan the relocs for --emit-relocs.
879 void
880 emit_relocs_scan(Symbol_table* symtab,
881 Layout* layout,
882 Sized_relobj_file<size, big_endian>* object,
883 unsigned int data_shndx,
884 unsigned int sh_type,
885 const unsigned char* prelocs,
886 size_t reloc_count,
887 Output_section* output_section,
888 bool needs_special_offset_handling,
889 size_t local_symbol_count,
890 const unsigned char* plocal_syms,
891 Relocatable_relocs* rr);
892
893 // Emit relocations for a section.
894 void
895 relocate_relocs(const Relocate_info<size, big_endian>*,
896 unsigned int sh_type,
897 const unsigned char* prelocs,
898 size_t reloc_count,
899 Output_section* output_section,
900 typename elfcpp::Elf_types<size>::Elf_Off
901 offset_in_output_section,
902 unsigned char*,
903 Address view_address,
904 section_size_type,
905 unsigned char* reloc_view,
906 section_size_type reloc_view_size);
907
908 // Return whether SYM is defined by the ABI.
909 bool
910 do_is_defined_by_abi(const Symbol* sym) const
911 {
912 return strcmp(sym->name(), "__tls_get_addr") == 0;
913 }
914
915 // Return the size of the GOT section.
916 section_size_type
917 got_size() const
918 {
919 gold_assert(this->got_ != NULL);
920 return this->got_->data_size();
921 }
922
923 // Get the PLT section.
924 const Output_data_plt_powerpc<size, big_endian>*
925 plt_section() const
926 {
927 gold_assert(this->plt_ != NULL);
928 return this->plt_;
929 }
930
931 // Get the IPLT section.
932 const Output_data_plt_powerpc<size, big_endian>*
933 iplt_section() const
934 {
935 gold_assert(this->iplt_ != NULL);
936 return this->iplt_;
937 }
938
939 // Get the LPLT section.
940 const Output_data_plt_powerpc<size, big_endian>*
941 lplt_section() const
942 {
943 return this->lplt_;
944 }
945
946 // Return the plt offset and section for the given global sym.
947 Address
948 plt_off(const Symbol* gsym,
949 const Output_data_plt_powerpc<size, big_endian>** sec) const
950 {
951 if (gsym->type() == elfcpp::STT_GNU_IFUNC
952 && gsym->can_use_relative_reloc(false))
953 *sec = this->iplt_section();
954 else
955 *sec = this->plt_section();
956 return gsym->plt_offset();
957 }
958
959 // Return the plt offset and section for the given local sym.
960 Address
961 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
962 unsigned int local_sym_index,
963 const Output_data_plt_powerpc<size, big_endian>** sec) const
964 {
965 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
966 if (lsym->is_ifunc_symbol())
967 *sec = this->iplt_section();
968 else
969 *sec = this->lplt_section();
970 return relobj->local_plt_offset(local_sym_index);
971 }
972
973 // Get the .glink section.
974 const Output_data_glink<size, big_endian>*
975 glink_section() const
976 {
977 gold_assert(this->glink_ != NULL);
978 return this->glink_;
979 }
980
981 Output_data_glink<size, big_endian>*
982 glink_section()
983 {
984 gold_assert(this->glink_ != NULL);
985 return this->glink_;
986 }
987
988 bool has_glink() const
989 { return this->glink_ != NULL; }
990
991 // Get the GOT section.
992 const Output_data_got_powerpc<size, big_endian>*
993 got_section() const
994 {
995 gold_assert(this->got_ != NULL);
996 return this->got_;
997 }
998
999 // Get the GOT section, creating it if necessary.
1000 Output_data_got_powerpc<size, big_endian>*
1001 got_section(Symbol_table*, Layout*);
1002
1003 Object*
1004 do_make_elf_object(const std::string&, Input_file*, off_t,
1005 const elfcpp::Ehdr<size, big_endian>&);
1006
1007 // Return the number of entries in the GOT.
1008 unsigned int
1009 got_entry_count() const
1010 {
1011 if (this->got_ == NULL)
1012 return 0;
1013 return this->got_size() / (size / 8);
1014 }
1015
1016 // Return the number of entries in the PLT.
1017 unsigned int
1018 plt_entry_count() const;
1019
1020 // Return the offset of the first non-reserved PLT entry.
1021 unsigned int
1022 first_plt_entry_offset() const
1023 {
1024 if (size == 32)
1025 return 0;
1026 if (this->abiversion() >= 2)
1027 return 16;
1028 return 24;
1029 }
1030
1031 // Return the size of each PLT entry.
1032 unsigned int
1033 plt_entry_size() const
1034 {
1035 if (size == 32)
1036 return 4;
1037 if (this->abiversion() >= 2)
1038 return 8;
1039 return 24;
1040 }
1041
1042 Output_data_save_res<size, big_endian>*
1043 savres_section() const
1044 {
1045 return this->savres_section_;
1046 }
1047
1048 // Add any special sections for this symbol to the gc work list.
1049 // For powerpc64, this adds the code section of a function
1050 // descriptor.
1051 void
1052 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1053
1054 // Handle target specific gc actions when adding a gc reference from
1055 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1056 // and DST_OFF. For powerpc64, this adds a referenc to the code
1057 // section of a function descriptor.
1058 void
1059 do_gc_add_reference(Symbol_table* symtab,
1060 Relobj* src_obj,
1061 unsigned int src_shndx,
1062 Relobj* dst_obj,
1063 unsigned int dst_shndx,
1064 Address dst_off) const;
1065
1066 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1067 const Stub_tables&
1068 stub_tables() const
1069 { return this->stub_tables_; }
1070
1071 const Output_data_brlt_powerpc<size, big_endian>*
1072 brlt_section() const
1073 { return this->brlt_section_; }
1074
1075 void
1076 add_branch_lookup_table(Address to)
1077 {
1078 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1079 this->branch_lookup_table_.insert(std::make_pair(to, off));
1080 }
1081
1082 Address
1083 find_branch_lookup_table(Address to)
1084 {
1085 typename Branch_lookup_table::const_iterator p
1086 = this->branch_lookup_table_.find(to);
1087 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1088 }
1089
1090 void
1091 write_branch_lookup_table(unsigned char *oview)
1092 {
1093 for (typename Branch_lookup_table::const_iterator p
1094 = this->branch_lookup_table_.begin();
1095 p != this->branch_lookup_table_.end();
1096 ++p)
1097 {
1098 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1099 }
1100 }
1101
1102 // Wrapper used after relax to define a local symbol in output data,
1103 // from the end if value < 0.
1104 void
1105 define_local(Symbol_table* symtab, const char* name,
1106 Output_data* od, Address value, unsigned int symsize)
1107 {
1108 Symbol* sym
1109 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1110 od, value, symsize, elfcpp::STT_NOTYPE,
1111 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1112 static_cast<Signed_address>(value) < 0,
1113 false);
1114 // We are creating this symbol late, so need to fix up things
1115 // done early in Layout::finalize.
1116 sym->set_dynsym_index(-1U);
1117 }
1118
1119 void
1120 set_power10_relocs()
1121 {
1122 this->power10_relocs_ = true;
1123 }
1124
1125 bool
1126 power10_stubs() const
1127 {
1128 return (this->power10_relocs_
1129 && (parameters->options().power10_stubs_enum()
1130 != General_options::POWER10_STUBS_NO));
1131 }
1132
1133 bool
1134 power10_stubs_auto() const
1135 {
1136 return (parameters->options().power10_stubs_enum()
1137 == General_options::POWER10_STUBS_AUTO);
1138 }
1139
1140 bool
1141 plt_thread_safe() const
1142 { return this->plt_thread_safe_; }
1143
1144 bool
1145 plt_localentry0() const
1146 { return this->plt_localentry0_; }
1147
1148 bool
1149 has_localentry0() const
1150 { return this->has_localentry0_; }
1151
1152 void
1153 set_has_localentry0()
1154 {
1155 this->has_localentry0_ = true;
1156 }
1157
1158 bool
1159 is_elfv2_localentry0(const Symbol* gsym) const
1160 {
1161 return (size == 64
1162 && this->abiversion() >= 2
1163 && this->plt_localentry0()
1164 && gsym->type() == elfcpp::STT_FUNC
1165 && gsym->is_defined()
1166 && gsym->nonvis() >> 3 == 0
1167 && !gsym->non_zero_localentry());
1168 }
1169
1170 bool
1171 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1172 unsigned int r_sym) const
1173 {
1174 const Powerpc_relobj<size, big_endian>* ppc_object
1175 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1176
1177 if (size == 64
1178 && this->abiversion() >= 2
1179 && this->plt_localentry0()
1180 && ppc_object->st_other(r_sym) >> 5 == 0)
1181 {
1182 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1183 bool is_ordinary;
1184 if (!psymval->is_ifunc_symbol()
1185 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1186 && is_ordinary)
1187 return true;
1188 }
1189 return false;
1190 }
1191
1192 bool
1193 tprel_opt() const
1194 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
1195
1196 void
1197 set_no_tprel_opt()
1198 { this->no_tprel_opt_ = true; }
1199
1200 // Remember any symbols seen with non-zero localentry, even those
1201 // not providing a definition
1202 bool
1203 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1204 const char*)
1205 {
1206 if (size == 64)
1207 {
1208 unsigned char st_other = sym.get_st_other();
1209 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1210 to->set_non_zero_localentry();
1211 }
1212 // We haven't resolved anything, continue normal processing.
1213 return false;
1214 }
1215
1216 int
1217 abiversion() const
1218 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1219
1220 void
1221 set_abiversion(int ver)
1222 {
1223 elfcpp::Elf_Word flags = this->processor_specific_flags();
1224 flags &= ~elfcpp::EF_PPC64_ABI;
1225 flags |= ver & elfcpp::EF_PPC64_ABI;
1226 this->set_processor_specific_flags(flags);
1227 }
1228
1229 Symbol*
1230 tls_get_addr_opt() const
1231 { return this->tls_get_addr_opt_; }
1232
1233 Symbol*
1234 tls_get_addr() const
1235 { return this->tls_get_addr_; }
1236
1237 // If optimizing __tls_get_addr calls, whether this is the
1238 // "__tls_get_addr" symbol.
1239 bool
1240 is_tls_get_addr_opt(const Symbol* gsym) const
1241 {
1242 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1243 || gsym == this->tls_get_addr_opt_);
1244 }
1245
1246 bool
1247 replace_tls_get_addr(const Symbol* gsym) const
1248 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1249
1250 void
1251 set_has_tls_get_addr_opt()
1252 { this->has_tls_get_addr_opt_ = true; }
1253
1254 // Offset to toc save stack slot
1255 int
1256 stk_toc() const
1257 { return this->abiversion() < 2 ? 40 : 24; }
1258
1259 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1260 // so use the CR save slot. Used only by __tls_get_addr call stub,
1261 // relying on __tls_get_addr not saving CR itself.
1262 int
1263 stk_linker() const
1264 { return this->abiversion() < 2 ? 32 : 8; }
1265
1266 // Merge object attributes from input object with those in the output.
1267 void
1268 merge_object_attributes(const Object*, const Attributes_section_data*);
1269
1270 private:
1271
1272 class Track_tls
1273 {
1274 public:
1275 enum Tls_get_addr
1276 {
1277 NOT_EXPECTED = 0,
1278 EXPECTED = 1,
1279 SKIP = 2,
1280 NORMAL = 3
1281 };
1282
1283 Track_tls()
1284 : tls_get_addr_state_(NOT_EXPECTED),
1285 relinfo_(NULL), relnum_(0), r_offset_(0)
1286 { }
1287
1288 ~Track_tls()
1289 {
1290 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1291 this->missing();
1292 }
1293
1294 void
1295 missing(void)
1296 {
1297 if (this->relinfo_ != NULL)
1298 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1299 _("missing expected __tls_get_addr call"));
1300 }
1301
1302 void
1303 expect_tls_get_addr_call(
1304 const Relocate_info<size, big_endian>* relinfo,
1305 size_t relnum,
1306 Address r_offset)
1307 {
1308 this->tls_get_addr_state_ = EXPECTED;
1309 this->relinfo_ = relinfo;
1310 this->relnum_ = relnum;
1311 this->r_offset_ = r_offset;
1312 }
1313
1314 void
1315 expect_tls_get_addr_call()
1316 { this->tls_get_addr_state_ = EXPECTED; }
1317
1318 void
1319 skip_next_tls_get_addr_call()
1320 {this->tls_get_addr_state_ = SKIP; }
1321
1322 Tls_get_addr
1323 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1324 unsigned int r_type, const Symbol* gsym)
1325 {
1326 bool is_tls_call
1327 = ((r_type == elfcpp::R_POWERPC_REL24
1328 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1329 || r_type == elfcpp::R_PPC_PLTREL24
1330 || is_plt16_reloc<size>(r_type)
1331 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1332 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1333 || r_type == elfcpp::R_POWERPC_PLTSEQ
1334 || r_type == elfcpp::R_POWERPC_PLTCALL
1335 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1336 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1337 && gsym != NULL
1338 && (gsym == target->tls_get_addr()
1339 || gsym == target->tls_get_addr_opt()));
1340 Tls_get_addr last_tls = this->tls_get_addr_state_;
1341 this->tls_get_addr_state_ = NOT_EXPECTED;
1342 if (is_tls_call && last_tls != EXPECTED)
1343 return last_tls;
1344 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1345 {
1346 this->missing();
1347 return EXPECTED;
1348 }
1349 return NORMAL;
1350 }
1351
1352 private:
1353 // What we're up to regarding calls to __tls_get_addr.
1354 // On powerpc, the branch and link insn making a call to
1355 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1356 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1357 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1358 // The marker relocation always comes first, and has the same
1359 // symbol as the reloc on the insn setting up the __tls_get_addr
1360 // argument. This ties the arg setup insn with the call insn,
1361 // allowing ld to safely optimize away the call. We check that
1362 // every call to __tls_get_addr has a marker relocation, and that
1363 // every marker relocation is on a call to __tls_get_addr.
1364 Tls_get_addr tls_get_addr_state_;
1365 // Info about the last reloc for error message.
1366 const Relocate_info<size, big_endian>* relinfo_;
1367 size_t relnum_;
1368 Address r_offset_;
1369 };
1370
1371 // The class which scans relocations.
1372 class Scan : protected Track_tls
1373 {
1374 public:
1375 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1376
1377 Scan()
1378 : Track_tls(), issued_non_pic_error_(false)
1379 { }
1380
1381 static inline int
1382 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1383
1384 inline void
1385 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1386 Sized_relobj_file<size, big_endian>* object,
1387 unsigned int data_shndx,
1388 Output_section* output_section,
1389 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1390 const elfcpp::Sym<size, big_endian>& lsym,
1391 bool is_discarded);
1392
1393 inline void
1394 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1395 Sized_relobj_file<size, big_endian>* object,
1396 unsigned int data_shndx,
1397 Output_section* output_section,
1398 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1399 Symbol* gsym);
1400
1401 inline bool
1402 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1403 Target_powerpc* ,
1404 Sized_relobj_file<size, big_endian>* relobj,
1405 unsigned int ,
1406 Output_section* ,
1407 const elfcpp::Rela<size, big_endian>& ,
1408 unsigned int r_type,
1409 const elfcpp::Sym<size, big_endian>&)
1410 {
1411 // PowerPC64 .opd is not folded, so any identical function text
1412 // may be folded and we'll still keep function addresses distinct.
1413 // That means no reloc is of concern here.
1414 if (size == 64)
1415 {
1416 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1417 <Powerpc_relobj<size, big_endian>*>(relobj);
1418 if (ppcobj->abiversion() == 1)
1419 return false;
1420 }
1421 // For 32-bit and ELFv2, conservatively assume anything but calls to
1422 // function code might be taking the address of the function.
1423 return !is_branch_reloc<size>(r_type);
1424 }
1425
1426 inline bool
1427 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1428 Target_powerpc* ,
1429 Sized_relobj_file<size, big_endian>* relobj,
1430 unsigned int ,
1431 Output_section* ,
1432 const elfcpp::Rela<size, big_endian>& ,
1433 unsigned int r_type,
1434 Symbol*)
1435 {
1436 // As above.
1437 if (size == 64)
1438 {
1439 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1440 <Powerpc_relobj<size, big_endian>*>(relobj);
1441 if (ppcobj->abiversion() == 1)
1442 return false;
1443 }
1444 return !is_branch_reloc<size>(r_type);
1445 }
1446
1447 static bool
1448 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1449 Sized_relobj_file<size, big_endian>* object,
1450 unsigned int r_type, bool report_err);
1451
1452 private:
1453 static void
1454 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1455 unsigned int r_type);
1456
1457 static void
1458 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1459 unsigned int r_type, Symbol*);
1460
1461 static void
1462 generate_tls_call(Symbol_table* symtab, Layout* layout,
1463 Target_powerpc* target);
1464
1465 void
1466 check_non_pic(Relobj*, unsigned int r_type);
1467
1468 // Whether we have issued an error about a non-PIC compilation.
1469 bool issued_non_pic_error_;
1470 };
1471
1472 bool
1473 symval_for_branch(const Symbol_table* symtab,
1474 const Sized_symbol<size>* gsym,
1475 Powerpc_relobj<size, big_endian>* object,
1476 Address *value, unsigned int *dest_shndx);
1477
1478 // The class which implements relocation.
1479 class Relocate : protected Track_tls
1480 {
1481 public:
1482 // Use 'at' branch hints when true, 'y' when false.
1483 // FIXME maybe: set this with an option.
1484 static const bool is_isa_v2 = true;
1485
1486 Relocate()
1487 : Track_tls()
1488 { }
1489
1490 // Do a relocation. Return false if the caller should not issue
1491 // any warnings about this relocation.
1492 inline bool
1493 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1494 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1495 const Sized_symbol<size>*, const Symbol_value<size>*,
1496 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1497 section_size_type);
1498 };
1499
1500 class Relocate_comdat_behavior
1501 {
1502 public:
1503 // Decide what the linker should do for relocations that refer to
1504 // discarded comdat sections.
1505 inline Comdat_behavior
1506 get(const char* name)
1507 {
1508 gold::Default_comdat_behavior default_behavior;
1509 Comdat_behavior ret = default_behavior.get(name);
1510 if (ret == CB_ERROR)
1511 {
1512 if (size == 32
1513 && (strcmp(name, ".fixup") == 0
1514 || strcmp(name, ".got2") == 0))
1515 ret = CB_IGNORE;
1516 if (size == 64
1517 && (strcmp(name, ".opd") == 0
1518 || strcmp(name, ".toc") == 0
1519 || strcmp(name, ".toc1") == 0))
1520 ret = CB_IGNORE;
1521 }
1522 return ret;
1523 }
1524 };
1525
1526 // Optimize the TLS relocation type based on what we know about the
1527 // symbol. IS_FINAL is true if the final address of this symbol is
1528 // known at link time.
1529
1530 tls::Tls_optimization
1531 optimize_tls_gd(bool is_final)
1532 {
1533 // If we are generating a shared library, then we can't do anything
1534 // in the linker.
1535 if (parameters->options().shared()
1536 || !parameters->options().tls_optimize())
1537 return tls::TLSOPT_NONE;
1538
1539 if (!is_final)
1540 return tls::TLSOPT_TO_IE;
1541 return tls::TLSOPT_TO_LE;
1542 }
1543
1544 tls::Tls_optimization
1545 optimize_tls_ld()
1546 {
1547 if (parameters->options().shared()
1548 || !parameters->options().tls_optimize())
1549 return tls::TLSOPT_NONE;
1550
1551 return tls::TLSOPT_TO_LE;
1552 }
1553
1554 tls::Tls_optimization
1555 optimize_tls_ie(bool is_final)
1556 {
1557 if (!is_final
1558 || parameters->options().shared()
1559 || !parameters->options().tls_optimize())
1560 return tls::TLSOPT_NONE;
1561
1562 return tls::TLSOPT_TO_LE;
1563 }
1564
1565 // Create glink.
1566 void
1567 make_glink_section(Layout*);
1568
1569 // Create the PLT section.
1570 void
1571 make_plt_section(Symbol_table*, Layout*);
1572
1573 void
1574 make_iplt_section(Symbol_table*, Layout*);
1575
1576 void
1577 make_lplt_section(Layout*);
1578
1579 void
1580 make_brlt_section(Layout*);
1581
1582 // Create a PLT entry for a global symbol.
1583 void
1584 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1585
1586 // Create a PLT entry for a local IFUNC symbol.
1587 void
1588 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1589 Sized_relobj_file<size, big_endian>*,
1590 unsigned int);
1591
1592 // Create a PLT entry for a local non-IFUNC symbol.
1593 void
1594 make_local_plt_entry(Layout*,
1595 Sized_relobj_file<size, big_endian>*,
1596 unsigned int);
1597
1598
1599 // Create a GOT entry for local dynamic __tls_get_addr.
1600 unsigned int
1601 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1602 Sized_relobj_file<size, big_endian>* object);
1603
1604 unsigned int
1605 tlsld_got_offset() const
1606 {
1607 return this->tlsld_got_offset_;
1608 }
1609
1610 // Get the dynamic reloc section, creating it if necessary.
1611 Reloc_section*
1612 rela_dyn_section(Layout*);
1613
1614 // Similarly, but for ifunc symbols get the one for ifunc.
1615 Reloc_section*
1616 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1617
1618 // Copy a relocation against a global symbol.
1619 void
1620 copy_reloc(Symbol_table* symtab, Layout* layout,
1621 Sized_relobj_file<size, big_endian>* object,
1622 unsigned int shndx, Output_section* output_section,
1623 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1624 {
1625 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1626 this->copy_relocs_.copy_reloc(symtab, layout,
1627 symtab->get_sized_symbol<size>(sym),
1628 object, shndx, output_section,
1629 r_type, reloc.get_r_offset(),
1630 reloc.get_r_addend(),
1631 this->rela_dyn_section(layout));
1632 }
1633
1634 // Look over all the input sections, deciding where to place stubs.
1635 void
1636 group_sections(Layout*, const Task*, bool);
1637
1638 // Sort output sections by address.
1639 struct Sort_sections
1640 {
1641 bool
1642 operator()(const Output_section* sec1, const Output_section* sec2)
1643 { return sec1->address() < sec2->address(); }
1644 };
1645
1646 class Branch_info
1647 {
1648 public:
1649 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1650 unsigned int data_shndx,
1651 Address r_offset,
1652 unsigned int r_type,
1653 unsigned int r_sym,
1654 Address addend)
1655 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1656 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1657 { }
1658
1659 ~Branch_info()
1660 { }
1661
1662 // Return whether this branch is going via a plt call stub, and if
1663 // so, mark it as having an R_PPC64_TOCSAVE.
1664 bool
1665 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1666 unsigned int shndx, Address offset,
1667 Target_powerpc* target, Symbol_table* symtab);
1668
1669 // If this branch needs a plt call stub, or a long branch stub, make one.
1670 bool
1671 make_stub(Stub_table<size, big_endian>*,
1672 Stub_table<size, big_endian>*,
1673 Symbol_table*) const;
1674
1675 private:
1676 // The branch location..
1677 Powerpc_relobj<size, big_endian>* object_;
1678 unsigned int shndx_;
1679 Address offset_;
1680 // ..and the branch type and destination.
1681 unsigned int r_type_ : 31;
1682 unsigned int tocsave_ : 1;
1683 unsigned int r_sym_;
1684 Address addend_;
1685 };
1686
1687 // Information about this specific target which we pass to the
1688 // general Target structure.
1689 static Target::Target_info powerpc_info;
1690
1691 // The types of GOT entries needed for this platform.
1692 // These values are exposed to the ABI in an incremental link.
1693 // Do not renumber existing values without changing the version
1694 // number of the .gnu_incremental_inputs section.
1695 enum Got_type
1696 {
1697 GOT_TYPE_STANDARD,
1698 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1699 GOT_TYPE_DTPREL, // entry for @got@dtprel
1700 GOT_TYPE_TPREL // entry for @got@tprel
1701 };
1702
1703 // The GOT section.
1704 Output_data_got_powerpc<size, big_endian>* got_;
1705 // The PLT section. This is a container for a table of addresses,
1706 // and their relocations. Each address in the PLT has a dynamic
1707 // relocation (R_*_JMP_SLOT) and each address will have a
1708 // corresponding entry in .glink for lazy resolution of the PLT.
1709 // ppc32 initialises the PLT to point at the .glink entry, while
1710 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1711 // linker adds a stub that loads the PLT entry into ctr then
1712 // branches to ctr. There may be more than one stub for each PLT
1713 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1714 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1715 Output_data_plt_powerpc<size, big_endian>* plt_;
1716 // The IPLT section. Like plt_, this is a container for a table of
1717 // addresses and their relocations, specifically for STT_GNU_IFUNC
1718 // functions that resolve locally (STT_GNU_IFUNC functions that
1719 // don't resolve locally go in PLT). Unlike plt_, these have no
1720 // entry in .glink for lazy resolution, and the relocation section
1721 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1722 // the relocation section may contain relocations against
1723 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1724 // relocation section will appear at the end of other dynamic
1725 // relocations, so that ld.so applies these relocations after other
1726 // dynamic relocations. In a static executable, the relocation
1727 // section is emitted and marked with __rela_iplt_start and
1728 // __rela_iplt_end symbols.
1729 Output_data_plt_powerpc<size, big_endian>* iplt_;
1730 // A PLT style section for local, non-ifunc symbols
1731 Output_data_plt_powerpc<size, big_endian>* lplt_;
1732 // Section holding long branch destinations.
1733 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1734 // The .glink section.
1735 Output_data_glink<size, big_endian>* glink_;
1736 // The dynamic reloc section.
1737 Reloc_section* rela_dyn_;
1738 // Relocs saved to avoid a COPY reloc.
1739 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1740 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1741 unsigned int tlsld_got_offset_;
1742
1743 Stub_tables stub_tables_;
1744 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1745 Branch_lookup_table branch_lookup_table_;
1746
1747 typedef std::vector<Branch_info> Branches;
1748 Branches branch_info_;
1749 Tocsave_loc tocsave_loc_;
1750
1751 bool power10_relocs_;
1752 bool plt_thread_safe_;
1753 bool plt_localentry0_;
1754 bool plt_localentry0_init_;
1755 bool has_localentry0_;
1756 bool has_tls_get_addr_opt_;
1757 bool no_tprel_opt_;
1758
1759 bool relax_failed_;
1760 int relax_fail_count_;
1761 int32_t stub_group_size_;
1762
1763 Output_data_save_res<size, big_endian> *savres_section_;
1764
1765 // The "__tls_get_addr" symbol, if present
1766 Symbol* tls_get_addr_;
1767 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1768 Symbol* tls_get_addr_opt_;
1769
1770 // Attributes in output.
1771 Attributes_section_data* attributes_section_data_;
1772
1773 // Last input file to change various attribute tags
1774 const char* last_fp_;
1775 const char* last_ld_;
1776 const char* last_vec_;
1777 const char* last_struct_;
1778 };
1779
1780 template<>
1781 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1782 {
1783 32, // size
1784 true, // is_big_endian
1785 elfcpp::EM_PPC, // machine_code
1786 false, // has_make_symbol
1787 false, // has_resolve
1788 false, // has_code_fill
1789 true, // is_default_stack_executable
1790 false, // can_icf_inline_merge_sections
1791 '\0', // wrap_char
1792 "/usr/lib/ld.so.1", // dynamic_linker
1793 0x10000000, // default_text_segment_address
1794 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1795 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1796 false, // isolate_execinstr
1797 0, // rosegment_gap
1798 elfcpp::SHN_UNDEF, // small_common_shndx
1799 elfcpp::SHN_UNDEF, // large_common_shndx
1800 0, // small_common_section_flags
1801 0, // large_common_section_flags
1802 NULL, // attributes_section
1803 NULL, // attributes_vendor
1804 "_start", // entry_symbol_name
1805 32, // hash_entry_size
1806 elfcpp::SHT_PROGBITS, // unwind_section_type
1807 };
1808
1809 template<>
1810 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1811 {
1812 32, // size
1813 false, // is_big_endian
1814 elfcpp::EM_PPC, // machine_code
1815 false, // has_make_symbol
1816 false, // has_resolve
1817 false, // has_code_fill
1818 true, // is_default_stack_executable
1819 false, // can_icf_inline_merge_sections
1820 '\0', // wrap_char
1821 "/usr/lib/ld.so.1", // dynamic_linker
1822 0x10000000, // default_text_segment_address
1823 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1824 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1825 false, // isolate_execinstr
1826 0, // rosegment_gap
1827 elfcpp::SHN_UNDEF, // small_common_shndx
1828 elfcpp::SHN_UNDEF, // large_common_shndx
1829 0, // small_common_section_flags
1830 0, // large_common_section_flags
1831 NULL, // attributes_section
1832 NULL, // attributes_vendor
1833 "_start", // entry_symbol_name
1834 32, // hash_entry_size
1835 elfcpp::SHT_PROGBITS, // unwind_section_type
1836 };
1837
1838 template<>
1839 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1840 {
1841 64, // size
1842 true, // is_big_endian
1843 elfcpp::EM_PPC64, // machine_code
1844 false, // has_make_symbol
1845 true, // has_resolve
1846 false, // has_code_fill
1847 false, // is_default_stack_executable
1848 false, // can_icf_inline_merge_sections
1849 '\0', // wrap_char
1850 "/usr/lib/ld.so.1", // dynamic_linker
1851 0x10000000, // default_text_segment_address
1852 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1853 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1854 false, // isolate_execinstr
1855 0, // rosegment_gap
1856 elfcpp::SHN_UNDEF, // small_common_shndx
1857 elfcpp::SHN_UNDEF, // large_common_shndx
1858 0, // small_common_section_flags
1859 0, // large_common_section_flags
1860 NULL, // attributes_section
1861 NULL, // attributes_vendor
1862 "_start", // entry_symbol_name
1863 32, // hash_entry_size
1864 elfcpp::SHT_PROGBITS, // unwind_section_type
1865 };
1866
1867 template<>
1868 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1869 {
1870 64, // size
1871 false, // is_big_endian
1872 elfcpp::EM_PPC64, // machine_code
1873 false, // has_make_symbol
1874 true, // has_resolve
1875 false, // has_code_fill
1876 false, // is_default_stack_executable
1877 false, // can_icf_inline_merge_sections
1878 '\0', // wrap_char
1879 "/usr/lib/ld.so.1", // dynamic_linker
1880 0x10000000, // default_text_segment_address
1881 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1882 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1883 false, // isolate_execinstr
1884 0, // rosegment_gap
1885 elfcpp::SHN_UNDEF, // small_common_shndx
1886 elfcpp::SHN_UNDEF, // large_common_shndx
1887 0, // small_common_section_flags
1888 0, // large_common_section_flags
1889 NULL, // attributes_section
1890 NULL, // attributes_vendor
1891 "_start", // entry_symbol_name
1892 32, // hash_entry_size
1893 elfcpp::SHT_PROGBITS, // unwind_section_type
1894 };
1895
1896 template<int size>
1897 inline bool
1898 is_branch_reloc(unsigned int r_type)
1899 {
1900 return (r_type == elfcpp::R_POWERPC_REL24
1901 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1902 || r_type == elfcpp::R_PPC_PLTREL24
1903 || r_type == elfcpp::R_PPC_LOCAL24PC
1904 || r_type == elfcpp::R_POWERPC_REL14
1905 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1906 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1907 || r_type == elfcpp::R_POWERPC_ADDR24
1908 || r_type == elfcpp::R_POWERPC_ADDR14
1909 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1910 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1911 }
1912
1913 // Reloc resolves to plt entry.
1914 template<int size>
1915 inline bool
1916 is_plt16_reloc(unsigned int r_type)
1917 {
1918 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1919 || r_type == elfcpp::R_POWERPC_PLT16_HI
1920 || r_type == elfcpp::R_POWERPC_PLT16_HA
1921 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1922 }
1923
1924 // GOT_TYPE_STANDARD (ie. not TLS) GOT relocs
1925 inline bool
1926 is_got_reloc(unsigned int r_type)
1927 {
1928 return (r_type == elfcpp::R_POWERPC_GOT16
1929 || r_type == elfcpp::R_POWERPC_GOT16_LO
1930 || r_type == elfcpp::R_POWERPC_GOT16_HI
1931 || r_type == elfcpp::R_POWERPC_GOT16_HA
1932 || r_type == elfcpp::R_PPC64_GOT16_DS
1933 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1934 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1935 }
1936
1937 // If INSN is an opcode that may be used with an @tls operand, return
1938 // the transformed insn for TLS optimisation, otherwise return 0. If
1939 // REG is non-zero only match an insn with RB or RA equal to REG.
1940 uint32_t
1941 at_tls_transform(uint32_t insn, unsigned int reg)
1942 {
1943 if ((insn & (0x3f << 26)) != 31 << 26)
1944 return 0;
1945
1946 unsigned int rtra;
1947 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1948 rtra = insn & ((1 << 26) - (1 << 16));
1949 else if (((insn >> 16) & 0x1f) == reg)
1950 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1951 else
1952 return 0;
1953
1954 if ((insn & (0x3ff << 1)) == 266 << 1)
1955 // add -> addi
1956 insn = 14 << 26;
1957 else if ((insn & (0x1f << 1)) == 23 << 1
1958 && ((insn & (0x1f << 6)) < 14 << 6
1959 || ((insn & (0x1f << 6)) >= 16 << 6
1960 && (insn & (0x1f << 6)) < 24 << 6)))
1961 // load and store indexed -> dform
1962 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1963 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1964 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1965 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1966 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1967 // lwax -> lwa
1968 insn = (58 << 26) | 2;
1969 else
1970 return 0;
1971 insn |= rtra;
1972 return insn;
1973 }
1974
1975
1976 template<int size, bool big_endian>
1977 class Powerpc_relocate_functions
1978 {
1979 public:
1980 enum Overflow_check
1981 {
1982 CHECK_NONE,
1983 CHECK_SIGNED,
1984 CHECK_UNSIGNED,
1985 CHECK_BITFIELD,
1986 CHECK_LOW_INSN,
1987 CHECK_HIGH_INSN
1988 };
1989
1990 enum Status
1991 {
1992 STATUS_OK,
1993 STATUS_OVERFLOW
1994 };
1995
1996 private:
1997 typedef Powerpc_relocate_functions<size, big_endian> This;
1998 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1999 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
2000
2001 template<int valsize>
2002 static inline bool
2003 has_overflow_signed(Address value)
2004 {
2005 // limit = 1 << (valsize - 1) without shift count exceeding size of type
2006 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2007 limit <<= ((valsize - 1) >> 1);
2008 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2009 return value + limit > (limit << 1) - 1;
2010 }
2011
2012 template<int valsize>
2013 static inline bool
2014 has_overflow_unsigned(Address value)
2015 {
2016 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2017 limit <<= ((valsize - 1) >> 1);
2018 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2019 return value > (limit << 1) - 1;
2020 }
2021
2022 template<int valsize>
2023 static inline bool
2024 has_overflow_bitfield(Address value)
2025 {
2026 return (has_overflow_unsigned<valsize>(value)
2027 && has_overflow_signed<valsize>(value));
2028 }
2029
2030 template<int valsize>
2031 static inline Status
2032 overflowed(Address value, Overflow_check overflow)
2033 {
2034 if (overflow == CHECK_SIGNED)
2035 {
2036 if (has_overflow_signed<valsize>(value))
2037 return STATUS_OVERFLOW;
2038 }
2039 else if (overflow == CHECK_UNSIGNED)
2040 {
2041 if (has_overflow_unsigned<valsize>(value))
2042 return STATUS_OVERFLOW;
2043 }
2044 else if (overflow == CHECK_BITFIELD)
2045 {
2046 if (has_overflow_bitfield<valsize>(value))
2047 return STATUS_OVERFLOW;
2048 }
2049 return STATUS_OK;
2050 }
2051
2052 // Do a simple RELA relocation
2053 template<int fieldsize, int valsize>
2054 static inline Status
2055 rela(unsigned char* view, Address value, Overflow_check overflow)
2056 {
2057 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2058 Valtype* wv = reinterpret_cast<Valtype*>(view);
2059 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2060 return overflowed<valsize>(value, overflow);
2061 }
2062
2063 template<int fieldsize, int valsize>
2064 static inline Status
2065 rela(unsigned char* view,
2066 unsigned int right_shift,
2067 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2068 Address value,
2069 Overflow_check overflow)
2070 {
2071 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2072 Valtype* wv = reinterpret_cast<Valtype*>(view);
2073 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2074 if (overflow == CHECK_SIGNED)
2075 value = static_cast<SignedAddress>(value) >> right_shift;
2076 else
2077 value = value >> right_shift;
2078 Valtype reloc = value;
2079 val &= ~dst_mask;
2080 reloc &= dst_mask;
2081 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2082 return overflowed<valsize>(value, overflow);
2083 }
2084
2085 // Do a simple RELA relocation, unaligned.
2086 template<int fieldsize, int valsize>
2087 static inline Status
2088 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2089 {
2090 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2091 return overflowed<valsize>(value, overflow);
2092 }
2093
2094 template<int fieldsize, int valsize>
2095 static inline Status
2096 rela_ua(unsigned char* view,
2097 unsigned int right_shift,
2098 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2099 Address value,
2100 Overflow_check overflow)
2101 {
2102 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2103 Valtype;
2104 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2105 if (overflow == CHECK_SIGNED)
2106 value = static_cast<SignedAddress>(value) >> right_shift;
2107 else
2108 value = value >> right_shift;
2109 Valtype reloc = value;
2110 val &= ~dst_mask;
2111 reloc &= dst_mask;
2112 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2113 return overflowed<valsize>(value, overflow);
2114 }
2115
2116 public:
2117 // R_PPC64_ADDR64: (Symbol + Addend)
2118 static inline void
2119 addr64(unsigned char* view, Address value)
2120 { This::template rela<64,64>(view, value, CHECK_NONE); }
2121
2122 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2123 static inline void
2124 addr64_u(unsigned char* view, Address value)
2125 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2126
2127 // R_POWERPC_ADDR32: (Symbol + Addend)
2128 static inline Status
2129 addr32(unsigned char* view, Address value, Overflow_check overflow)
2130 { return This::template rela<32,32>(view, value, overflow); }
2131
2132 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2133 static inline Status
2134 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2135 { return This::template rela_ua<32,32>(view, value, overflow); }
2136
2137 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2138 static inline Status
2139 addr24(unsigned char* view, Address value, Overflow_check overflow)
2140 {
2141 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2142 value, overflow);
2143 if (overflow != CHECK_NONE && (value & 3) != 0)
2144 stat = STATUS_OVERFLOW;
2145 return stat;
2146 }
2147
2148 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2149 static inline Status
2150 addr16(unsigned char* view, Address value, Overflow_check overflow)
2151 { return This::template rela<16,16>(view, value, overflow); }
2152
2153 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2154 static inline Status
2155 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2156 { return This::template rela_ua<16,16>(view, value, overflow); }
2157
2158 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2159 static inline Status
2160 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2161 {
2162 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2163 if ((value & 3) != 0)
2164 stat = STATUS_OVERFLOW;
2165 return stat;
2166 }
2167
2168 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2169 static inline Status
2170 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2171 {
2172 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2173 if ((value & 15) != 0)
2174 stat = STATUS_OVERFLOW;
2175 return stat;
2176 }
2177
2178 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2179 static inline void
2180 addr16_hi(unsigned char* view, Address value)
2181 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2182
2183 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2184 static inline void
2185 addr16_ha(unsigned char* view, Address value)
2186 { This::addr16_hi(view, value + 0x8000); }
2187
2188 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2189 static inline void
2190 addr16_hi2(unsigned char* view, Address value)
2191 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2192
2193 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2194 static inline void
2195 addr16_ha2(unsigned char* view, Address value)
2196 { This::addr16_hi2(view, value + 0x8000); }
2197
2198 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2199 static inline void
2200 addr16_hi3(unsigned char* view, Address value)
2201 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2202
2203 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2204 static inline void
2205 addr16_ha3(unsigned char* view, Address value)
2206 { This::addr16_hi3(view, value + 0x8000); }
2207
2208 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2209 static inline Status
2210 addr14(unsigned char* view, Address value, Overflow_check overflow)
2211 {
2212 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2213 if (overflow != CHECK_NONE && (value & 3) != 0)
2214 stat = STATUS_OVERFLOW;
2215 return stat;
2216 }
2217
2218 // R_POWERPC_REL16DX_HA
2219 static inline Status
2220 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2221 {
2222 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2223 Valtype* wv = reinterpret_cast<Valtype*>(view);
2224 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2225 value += 0x8000;
2226 value = static_cast<SignedAddress>(value) >> 16;
2227 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2228 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2229 return overflowed<16>(value, overflow);
2230 }
2231
2232 // R_PPC64_D34
2233 static inline Status
2234 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2235 {
2236 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2237 value, overflow);
2238 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2239 return stat;
2240 }
2241
2242 // R_PPC64_D34_HI30
2243 static inline void
2244 addr34_hi(unsigned char *view, uint64_t value)
2245 { This::addr34(view, value >> 34, CHECK_NONE);}
2246
2247 // R_PPC64_D34_HA30
2248 static inline void
2249 addr34_ha(unsigned char *view, uint64_t value)
2250 { This::addr34_hi(view, value + (1ULL << 33));}
2251
2252 // R_PPC64_D28
2253 static inline Status
2254 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2255 {
2256 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2257 value, overflow);
2258 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2259 return stat;
2260 }
2261
2262 // R_PPC64_ADDR16_HIGHER34
2263 static inline void
2264 addr16_higher34(unsigned char* view, uint64_t value)
2265 { This::addr16(view, value >> 34, CHECK_NONE); }
2266
2267 // R_PPC64_ADDR16_HIGHERA34
2268 static inline void
2269 addr16_highera34(unsigned char* view, uint64_t value)
2270 { This::addr16_higher34(view, value + (1ULL << 33)); }
2271
2272 // R_PPC64_ADDR16_HIGHEST34
2273 static inline void
2274 addr16_highest34(unsigned char* view, uint64_t value)
2275 { This::addr16(view, value >> 50, CHECK_NONE); }
2276
2277 // R_PPC64_ADDR16_HIGHESTA34
2278 static inline void
2279 addr16_highesta34(unsigned char* view, uint64_t value)
2280 { This::addr16_highest34(view, value + (1ULL << 33)); }
2281 };
2282
2283 // Set ABI version for input and output.
2284
2285 template<int size, bool big_endian>
2286 void
2287 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2288 {
2289 this->e_flags_ |= ver;
2290 if (this->abiversion() != 0)
2291 {
2292 Target_powerpc<size, big_endian>* target =
2293 static_cast<Target_powerpc<size, big_endian>*>(
2294 parameters->sized_target<size, big_endian>());
2295 if (target->abiversion() == 0)
2296 target->set_abiversion(this->abiversion());
2297 else if (target->abiversion() != this->abiversion())
2298 gold_error(_("%s: ABI version %d is not compatible "
2299 "with ABI version %d output"),
2300 this->name().c_str(),
2301 this->abiversion(), target->abiversion());
2302
2303 }
2304 }
2305
2306 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2307 // relocatable object, if such sections exists.
2308
2309 template<int size, bool big_endian>
2310 bool
2311 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2312 Read_symbols_data* sd)
2313 {
2314 const unsigned char* const pshdrs = sd->section_headers->data();
2315 const unsigned char* namesu = sd->section_names->data();
2316 const char* names = reinterpret_cast<const char*>(namesu);
2317 section_size_type names_size = sd->section_names_size;
2318 const unsigned char* s;
2319
2320 s = this->template find_shdr<size, big_endian>(pshdrs,
2321 size == 32 ? ".got2" : ".opd",
2322 names, names_size, NULL);
2323 if (s != NULL)
2324 {
2325 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2326 this->special_ = ndx;
2327 if (size == 64)
2328 {
2329 if (this->abiversion() == 0)
2330 this->set_abiversion(1);
2331 else if (this->abiversion() > 1)
2332 gold_error(_("%s: .opd invalid in abiv%d"),
2333 this->name().c_str(), this->abiversion());
2334 }
2335 }
2336 if (size == 64)
2337 {
2338 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2339 names, names_size, NULL);
2340 if (s != NULL)
2341 {
2342 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2343 this->relatoc_ = ndx;
2344 typename elfcpp::Shdr<size, big_endian> shdr(s);
2345 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2346 }
2347 }
2348 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2349 }
2350
2351 // Examine .rela.opd to build info about function entry points.
2352
2353 template<int size, bool big_endian>
2354 void
2355 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2356 size_t reloc_count,
2357 const unsigned char* prelocs,
2358 const unsigned char* plocal_syms)
2359 {
2360 if (size == 64)
2361 {
2362 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2363 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2364 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2365 Address expected_off = 0;
2366 bool regular = true;
2367 unsigned int opd_ent_size = 0;
2368
2369 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2370 {
2371 Reltype reloc(prelocs);
2372 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2373 = reloc.get_r_info();
2374 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2375 if (r_type == elfcpp::R_PPC64_ADDR64)
2376 {
2377 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2378 typename elfcpp::Elf_types<size>::Elf_Addr value;
2379 bool is_ordinary;
2380 unsigned int shndx;
2381 if (r_sym < this->local_symbol_count())
2382 {
2383 typename elfcpp::Sym<size, big_endian>
2384 lsym(plocal_syms + r_sym * sym_size);
2385 shndx = lsym.get_st_shndx();
2386 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2387 value = lsym.get_st_value();
2388 }
2389 else
2390 shndx = this->symbol_section_and_value(r_sym, &value,
2391 &is_ordinary);
2392 this->set_opd_ent(reloc.get_r_offset(), shndx,
2393 value + reloc.get_r_addend());
2394 if (i == 2)
2395 {
2396 expected_off = reloc.get_r_offset();
2397 opd_ent_size = expected_off;
2398 }
2399 else if (expected_off != reloc.get_r_offset())
2400 regular = false;
2401 expected_off += opd_ent_size;
2402 }
2403 else if (r_type == elfcpp::R_PPC64_TOC)
2404 {
2405 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2406 regular = false;
2407 }
2408 else
2409 {
2410 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2411 this->name().c_str(), r_type);
2412 regular = false;
2413 }
2414 }
2415 if (reloc_count <= 2)
2416 opd_ent_size = this->section_size(this->opd_shndx());
2417 if (opd_ent_size != 24 && opd_ent_size != 16)
2418 regular = false;
2419 if (!regular)
2420 {
2421 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2422 this->name().c_str());
2423 opd_ent_size = 0;
2424 }
2425 }
2426 }
2427
2428 // Returns true if a code sequence loading the TOC entry at VALUE
2429 // relative to the TOC pointer can be converted into code calculating
2430 // a TOC pointer relative offset.
2431 // If so, the TOC pointer relative offset is stored to VALUE.
2432
2433 template<int size, bool big_endian>
2434 bool
2435 Powerpc_relobj<size, big_endian>::make_toc_relative(
2436 Target_powerpc<size, big_endian>* target,
2437 Address* value)
2438 {
2439 if (size != 64)
2440 return false;
2441
2442 // With -mcmodel=medium code it is quite possible to have
2443 // toc-relative relocs referring to objects outside the TOC.
2444 // Don't try to look at a non-existent TOC.
2445 if (this->toc_shndx() == 0)
2446 return false;
2447
2448 // Convert VALUE back to an address by adding got_base (see below),
2449 // then to an offset in the TOC by subtracting the TOC output
2450 // section address and the TOC output offset. Since this TOC output
2451 // section and the got output section are one and the same, we can
2452 // omit adding and subtracting the output section address.
2453 Address off = (*value + this->toc_base_offset()
2454 - this->output_section_offset(this->toc_shndx()));
2455 // Is this offset in the TOC? -mcmodel=medium code may be using
2456 // TOC relative access to variables outside the TOC. Those of
2457 // course can't be optimized. We also don't try to optimize code
2458 // that is using a different object's TOC.
2459 if (off >= this->section_size(this->toc_shndx()))
2460 return false;
2461
2462 if (this->no_toc_opt(off))
2463 return false;
2464
2465 section_size_type vlen;
2466 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2467 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2468 // The TOC pointer
2469 Address got_base = (target->got_section()->output_section()->address()
2470 + this->toc_base_offset());
2471 addr -= got_base;
2472 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2473 return false;
2474
2475 *value = addr;
2476 return true;
2477 }
2478
2479 template<int size, bool big_endian>
2480 bool
2481 Powerpc_relobj<size, big_endian>::make_got_relative(
2482 Target_powerpc<size, big_endian>* target,
2483 const Symbol_value<size>* psymval,
2484 Address addend,
2485 Address* value)
2486 {
2487 Address addr = psymval->value(this, addend);
2488 Address got_base = (target->got_section()->output_section()->address()
2489 + this->toc_base_offset());
2490 addr -= got_base;
2491 if (addr + 0x80008000 > 0xffffffff)
2492 return false;
2493
2494 *value = addr;
2495 return true;
2496 }
2497
2498 // Perform the Sized_relobj_file method, then set up opd info from
2499 // .opd relocs.
2500
2501 template<int size, bool big_endian>
2502 void
2503 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2504 {
2505 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2506 if (size == 64)
2507 {
2508 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2509 p != rd->relocs.end();
2510 ++p)
2511 {
2512 if (p->data_shndx == this->opd_shndx())
2513 {
2514 uint64_t opd_size = this->section_size(this->opd_shndx());
2515 gold_assert(opd_size == static_cast<size_t>(opd_size));
2516 if (opd_size != 0)
2517 {
2518 this->init_opd(opd_size);
2519 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2520 rd->local_symbols->data());
2521 }
2522 break;
2523 }
2524 }
2525 }
2526 }
2527
2528 // Read the symbols then set up st_other vector.
2529
2530 template<int size, bool big_endian>
2531 void
2532 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2533 {
2534 this->base_read_symbols(sd);
2535 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2536 return;
2537 if (size == 64)
2538 {
2539 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2540 const unsigned char* const pshdrs = sd->section_headers->data();
2541 const unsigned int loccount = this->do_local_symbol_count();
2542 if (loccount != 0)
2543 {
2544 this->st_other_.resize(loccount);
2545 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2546 off_t locsize = loccount * sym_size;
2547 const unsigned int symtab_shndx = this->symtab_shndx();
2548 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2549 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2550 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2551 locsize, true, false);
2552 psyms += sym_size;
2553 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2554 {
2555 elfcpp::Sym<size, big_endian> sym(psyms);
2556 unsigned char st_other = sym.get_st_other();
2557 this->st_other_[i] = st_other;
2558 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2559 {
2560 if (this->abiversion() == 0)
2561 this->set_abiversion(2);
2562 else if (this->abiversion() < 2)
2563 gold_error(_("%s: local symbol %d has invalid st_other"
2564 " for ABI version 1"),
2565 this->name().c_str(), i);
2566 }
2567 }
2568 }
2569 }
2570
2571 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2572 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2573 bool merge_attributes = false;
2574 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2575 {
2576 elfcpp::Shdr<size, big_endian> shdr(ps);
2577 switch (shdr.get_sh_type())
2578 {
2579 case elfcpp::SHT_GNU_ATTRIBUTES:
2580 {
2581 gold_assert(this->attributes_section_data_ == NULL);
2582 section_offset_type section_offset = shdr.get_sh_offset();
2583 section_size_type section_size =
2584 convert_to_section_size_type(shdr.get_sh_size());
2585 const unsigned char* view =
2586 this->get_view(section_offset, section_size, true, false);
2587 this->attributes_section_data_ =
2588 new Attributes_section_data(view, section_size);
2589 }
2590 break;
2591
2592 case elfcpp::SHT_SYMTAB:
2593 {
2594 // Sometimes an object has no contents except the section
2595 // name string table and an empty symbol table with the
2596 // undefined symbol. We don't want to merge
2597 // processor-specific flags from such an object.
2598 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2599 elfcpp::Elf_sizes<size>::sym_size;
2600 if (shdr.get_sh_size() > sym_size)
2601 merge_attributes = true;
2602 }
2603 break;
2604
2605 case elfcpp::SHT_STRTAB:
2606 break;
2607
2608 default:
2609 merge_attributes = true;
2610 break;
2611 }
2612 }
2613
2614 if (!merge_attributes)
2615 {
2616 // Should rarely happen.
2617 delete this->attributes_section_data_;
2618 this->attributes_section_data_ = NULL;
2619 }
2620 }
2621
2622 template<int size, bool big_endian>
2623 void
2624 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2625 {
2626 this->e_flags_ |= ver;
2627 if (this->abiversion() != 0)
2628 {
2629 Target_powerpc<size, big_endian>* target =
2630 static_cast<Target_powerpc<size, big_endian>*>(
2631 parameters->sized_target<size, big_endian>());
2632 if (target->abiversion() == 0)
2633 target->set_abiversion(this->abiversion());
2634 else if (target->abiversion() != this->abiversion())
2635 gold_error(_("%s: ABI version %d is not compatible "
2636 "with ABI version %d output"),
2637 this->name().c_str(),
2638 this->abiversion(), target->abiversion());
2639
2640 }
2641 }
2642
2643 // Call Sized_dynobj::base_read_symbols to read the symbols then
2644 // read .opd from a dynamic object, filling in opd_ent_ vector,
2645
2646 template<int size, bool big_endian>
2647 void
2648 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2649 {
2650 this->base_read_symbols(sd);
2651 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2652 const unsigned char* ps =
2653 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2654 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2655 {
2656 elfcpp::Shdr<size, big_endian> shdr(ps);
2657 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2658 {
2659 section_offset_type section_offset = shdr.get_sh_offset();
2660 section_size_type section_size =
2661 convert_to_section_size_type(shdr.get_sh_size());
2662 const unsigned char* view =
2663 this->get_view(section_offset, section_size, true, false);
2664 this->attributes_section_data_ =
2665 new Attributes_section_data(view, section_size);
2666 break;
2667 }
2668 }
2669 if (size == 64)
2670 {
2671 const unsigned char* const pshdrs = sd->section_headers->data();
2672 const unsigned char* namesu = sd->section_names->data();
2673 const char* names = reinterpret_cast<const char*>(namesu);
2674 const unsigned char* s = NULL;
2675 const unsigned char* opd;
2676 section_size_type opd_size;
2677
2678 // Find and read .opd section.
2679 while (1)
2680 {
2681 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2682 sd->section_names_size,
2683 s);
2684 if (s == NULL)
2685 return;
2686
2687 typename elfcpp::Shdr<size, big_endian> shdr(s);
2688 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2689 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2690 {
2691 if (this->abiversion() == 0)
2692 this->set_abiversion(1);
2693 else if (this->abiversion() > 1)
2694 gold_error(_("%s: .opd invalid in abiv%d"),
2695 this->name().c_str(), this->abiversion());
2696
2697 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2698 this->opd_address_ = shdr.get_sh_addr();
2699 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2700 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2701 true, false);
2702 break;
2703 }
2704 }
2705
2706 // Build set of executable sections.
2707 // Using a set is probably overkill. There is likely to be only
2708 // a few executable sections, typically .init, .text and .fini,
2709 // and they are generally grouped together.
2710 typedef std::set<Sec_info> Exec_sections;
2711 Exec_sections exec_sections;
2712 s = pshdrs;
2713 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2714 {
2715 typename elfcpp::Shdr<size, big_endian> shdr(s);
2716 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2717 && ((shdr.get_sh_flags()
2718 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2719 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2720 && shdr.get_sh_size() != 0)
2721 {
2722 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2723 shdr.get_sh_size(), i));
2724 }
2725 }
2726 if (exec_sections.empty())
2727 return;
2728
2729 // Look over the OPD entries. This is complicated by the fact
2730 // that some binaries will use two-word entries while others
2731 // will use the standard three-word entries. In most cases
2732 // the third word (the environment pointer for languages like
2733 // Pascal) is unused and will be zero. If the third word is
2734 // used it should not be pointing into executable sections,
2735 // I think.
2736 this->init_opd(opd_size);
2737 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2738 {
2739 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2740 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2741 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2742 if (val == 0)
2743 // Chances are that this is the third word of an OPD entry.
2744 continue;
2745 typename Exec_sections::const_iterator e
2746 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2747 if (e != exec_sections.begin())
2748 {
2749 --e;
2750 if (e->start <= val && val < e->start + e->len)
2751 {
2752 // We have an address in an executable section.
2753 // VAL ought to be the function entry, set it up.
2754 this->set_opd_ent(p - opd, e->shndx, val);
2755 // Skip second word of OPD entry, the TOC pointer.
2756 p += 8;
2757 }
2758 }
2759 // If we didn't match any executable sections, we likely
2760 // have a non-zero third word in the OPD entry.
2761 }
2762 }
2763 }
2764
2765 // Relocate sections.
2766
2767 template<int size, bool big_endian>
2768 void
2769 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2770 const Symbol_table* symtab, const Layout* layout,
2771 const unsigned char* pshdrs, Output_file* of,
2772 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2773 {
2774 unsigned int start = 1;
2775 if (size == 64
2776 && this->relatoc_ != 0
2777 && !parameters->options().relocatable())
2778 {
2779 // Relocate .toc first.
2780 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2781 this->relatoc_, this->relatoc_);
2782 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2783 1, this->relatoc_ - 1);
2784 start = this->relatoc_ + 1;
2785 }
2786 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2787 start, this->shnum() - 1);
2788
2789 if (!parameters->options().output_is_position_independent())
2790 {
2791 Target_powerpc<size, big_endian>* target
2792 = static_cast<Target_powerpc<size, big_endian>*>(
2793 parameters->sized_target<size, big_endian>());
2794 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2795 {
2796 const section_size_type offset = target->lplt_section()->offset();
2797 const section_size_type oview_size
2798 = convert_to_section_size_type(target->lplt_section()->data_size());
2799 unsigned char* const oview = of->get_output_view(offset, oview_size);
2800
2801 bool modified = false;
2802 unsigned int nsyms = this->local_symbol_count();
2803 for (unsigned int i = 0; i < nsyms; i++)
2804 if (this->local_has_plt_offset(i))
2805 {
2806 Address value = this->local_symbol_value(i, 0);
2807 size_t off = this->local_plt_offset(i);
2808 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2809 modified = true;
2810 }
2811 if (modified)
2812 of->write_output_view(offset, oview_size, oview);
2813 }
2814 }
2815 }
2816
2817 // Set up some symbols.
2818
2819 template<int size, bool big_endian>
2820 void
2821 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2822 Symbol_table* symtab,
2823 Layout* layout)
2824 {
2825 if (size == 32)
2826 {
2827 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2828 // undefined when scanning relocs (and thus requires
2829 // non-relative dynamic relocs). The proper value will be
2830 // updated later.
2831 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2832 if (gotsym != NULL && gotsym->is_undefined())
2833 {
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("_GLOBAL_OFFSET_TABLE_", NULL,
2840 Symbol_table::PREDEFINED,
2841 got, 0, 0,
2842 elfcpp::STT_OBJECT,
2843 elfcpp::STB_LOCAL,
2844 elfcpp::STV_HIDDEN, 0,
2845 false, false);
2846 }
2847
2848 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2849 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2850 if (sdasym != NULL && sdasym->is_undefined())
2851 {
2852 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2853 Output_section* os
2854 = layout->add_output_section_data(".sdata", 0,
2855 elfcpp::SHF_ALLOC
2856 | elfcpp::SHF_WRITE,
2857 sdata, ORDER_SMALL_DATA, false);
2858 symtab->define_in_output_data("_SDA_BASE_", NULL,
2859 Symbol_table::PREDEFINED,
2860 os, 32768, 0, elfcpp::STT_OBJECT,
2861 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2862 0, false, false);
2863 }
2864 }
2865 else
2866 {
2867 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2868 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2869 if (gotsym != NULL && gotsym->is_undefined())
2870 {
2871 Target_powerpc<size, big_endian>* target =
2872 static_cast<Target_powerpc<size, big_endian>*>(
2873 parameters->sized_target<size, big_endian>());
2874 Output_data_got_powerpc<size, big_endian>* got
2875 = target->got_section(symtab, layout);
2876 symtab->define_in_output_data(".TOC.", NULL,
2877 Symbol_table::PREDEFINED,
2878 got, 0x8000, 0,
2879 elfcpp::STT_OBJECT,
2880 elfcpp::STB_LOCAL,
2881 elfcpp::STV_HIDDEN, 0,
2882 false, false);
2883 }
2884 }
2885
2886 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2887 if (parameters->options().tls_get_addr_optimize()
2888 && this->tls_get_addr_ != NULL
2889 && this->tls_get_addr_->in_reg())
2890 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2891 if (this->tls_get_addr_opt_ != NULL)
2892 {
2893 if (this->tls_get_addr_->is_undefined()
2894 || this->tls_get_addr_->is_from_dynobj())
2895 {
2896 // Make it seem as if references to __tls_get_addr are
2897 // really to __tls_get_addr_opt, so the latter symbol is
2898 // made dynamic, not the former.
2899 this->tls_get_addr_->clear_in_reg();
2900 this->tls_get_addr_opt_->set_in_reg();
2901 }
2902 // We have a non-dynamic definition for __tls_get_addr.
2903 // Make __tls_get_addr_opt the same, if it does not already have
2904 // a non-dynamic definition.
2905 else if (this->tls_get_addr_opt_->is_undefined()
2906 || this->tls_get_addr_opt_->is_from_dynobj())
2907 {
2908 Sized_symbol<size>* from
2909 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2910 Sized_symbol<size>* to
2911 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2912 symtab->clone<size>(to, from);
2913 }
2914 }
2915 }
2916
2917 // Set up PowerPC target specific relobj.
2918
2919 template<int size, bool big_endian>
2920 Object*
2921 Target_powerpc<size, big_endian>::do_make_elf_object(
2922 const std::string& name,
2923 Input_file* input_file,
2924 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2925 {
2926 int et = ehdr.get_e_type();
2927 // ET_EXEC files are valid input for --just-symbols/-R,
2928 // and we treat them as relocatable objects.
2929 if (et == elfcpp::ET_REL
2930 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2931 {
2932 Powerpc_relobj<size, big_endian>* obj =
2933 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2934 obj->setup();
2935 return obj;
2936 }
2937 else if (et == elfcpp::ET_DYN)
2938 {
2939 Powerpc_dynobj<size, big_endian>* obj =
2940 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2941 obj->setup();
2942 return obj;
2943 }
2944 else
2945 {
2946 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2947 return NULL;
2948 }
2949 }
2950
2951 template<int size, bool big_endian>
2952 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2953 {
2954 public:
2955 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2956 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2957
2958 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2959 : Output_data_got<size, big_endian>(),
2960 symtab_(symtab), layout_(layout),
2961 header_ent_cnt_(size == 32 ? 3 : 1),
2962 header_index_(size == 32 ? 0x2000 : 0)
2963 {
2964 if (size == 64)
2965 this->set_addralign(256);
2966 }
2967
2968 // Override all the Output_data_got methods we use so as to first call
2969 // reserve_ent().
2970 bool
2971 add_global(Symbol* gsym, unsigned int got_type)
2972 {
2973 this->reserve_ent();
2974 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2975 }
2976
2977 bool
2978 add_global_plt(Symbol* gsym, unsigned int got_type)
2979 {
2980 this->reserve_ent();
2981 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2982 }
2983
2984 bool
2985 add_global_tls(Symbol* gsym, unsigned int got_type)
2986 { return this->add_global_plt(gsym, got_type); }
2987
2988 void
2989 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2990 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2991 {
2992 this->reserve_ent();
2993 Output_data_got<size, big_endian>::
2994 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2995 }
2996
2997 void
2998 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2999 Output_data_reloc_generic* rel_dyn,
3000 unsigned int r_type_1, unsigned int r_type_2)
3001 {
3002 if (gsym->has_got_offset(got_type))
3003 return;
3004
3005 this->reserve_ent(2);
3006 Output_data_got<size, big_endian>::
3007 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
3008 }
3009
3010 bool
3011 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
3012 {
3013 this->reserve_ent();
3014 return Output_data_got<size, big_endian>::add_local(object, sym_index,
3015 got_type);
3016 }
3017
3018 bool
3019 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
3020 {
3021 this->reserve_ent();
3022 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
3023 got_type);
3024 }
3025
3026 bool
3027 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
3028 { return this->add_local_plt(object, sym_index, got_type); }
3029
3030 void
3031 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3032 unsigned int got_type,
3033 Output_data_reloc_generic* rel_dyn,
3034 unsigned int r_type)
3035 {
3036 if (object->local_has_got_offset(sym_index, got_type))
3037 return;
3038
3039 this->reserve_ent(2);
3040 Output_data_got<size, big_endian>::
3041 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
3042 }
3043
3044 unsigned int
3045 add_constant(Valtype constant)
3046 {
3047 this->reserve_ent();
3048 return Output_data_got<size, big_endian>::add_constant(constant);
3049 }
3050
3051 unsigned int
3052 add_constant_pair(Valtype c1, Valtype c2)
3053 {
3054 this->reserve_ent(2);
3055 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3056 }
3057
3058 // Offset of _GLOBAL_OFFSET_TABLE_.
3059 unsigned int
3060 g_o_t() const
3061 {
3062 return this->got_offset(this->header_index_);
3063 }
3064
3065 // Offset of base used to access the GOT/TOC.
3066 // The got/toc pointer reg will be set to this value.
3067 Valtype
3068 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
3069 {
3070 if (size == 32)
3071 return this->g_o_t();
3072 else
3073 return (this->output_section()->address()
3074 + object->toc_base_offset()
3075 - this->address());
3076 }
3077
3078 // Ensure our GOT has a header.
3079 void
3080 set_final_data_size()
3081 {
3082 if (this->header_ent_cnt_ != 0)
3083 this->make_header();
3084 Output_data_got<size, big_endian>::set_final_data_size();
3085 }
3086
3087 // First word of GOT header needs some values that are not
3088 // handled by Output_data_got so poke them in here.
3089 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3090 void
3091 do_write(Output_file* of)
3092 {
3093 Valtype val = 0;
3094 if (size == 32 && this->layout_->dynamic_data() != NULL)
3095 val = this->layout_->dynamic_section()->address();
3096 if (size == 64)
3097 val = this->output_section()->address() + 0x8000;
3098 this->replace_constant(this->header_index_, val);
3099 Output_data_got<size, big_endian>::do_write(of);
3100 }
3101
3102 private:
3103 void
3104 reserve_ent(unsigned int cnt = 1)
3105 {
3106 if (this->header_ent_cnt_ == 0)
3107 return;
3108 if (this->num_entries() + cnt > this->header_index_)
3109 this->make_header();
3110 }
3111
3112 void
3113 make_header()
3114 {
3115 this->header_ent_cnt_ = 0;
3116 this->header_index_ = this->num_entries();
3117 if (size == 32)
3118 {
3119 Output_data_got<size, big_endian>::add_constant(0);
3120 Output_data_got<size, big_endian>::add_constant(0);
3121 Output_data_got<size, big_endian>::add_constant(0);
3122
3123 // Define _GLOBAL_OFFSET_TABLE_ at the header
3124 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3125 if (gotsym != NULL)
3126 {
3127 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3128 sym->set_value(this->g_o_t());
3129 }
3130 else
3131 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3132 Symbol_table::PREDEFINED,
3133 this, this->g_o_t(), 0,
3134 elfcpp::STT_OBJECT,
3135 elfcpp::STB_LOCAL,
3136 elfcpp::STV_HIDDEN, 0,
3137 false, false);
3138 }
3139 else
3140 Output_data_got<size, big_endian>::add_constant(0);
3141 }
3142
3143 // Stashed pointers.
3144 Symbol_table* symtab_;
3145 Layout* layout_;
3146
3147 // GOT header size.
3148 unsigned int header_ent_cnt_;
3149 // GOT header index.
3150 unsigned int header_index_;
3151 };
3152
3153 // Get the GOT section, creating it if necessary.
3154
3155 template<int size, bool big_endian>
3156 Output_data_got_powerpc<size, big_endian>*
3157 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3158 Layout* layout)
3159 {
3160 if (this->got_ == NULL)
3161 {
3162 gold_assert(symtab != NULL && layout != NULL);
3163
3164 this->got_
3165 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
3166
3167 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3168 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3169 this->got_, ORDER_DATA, false);
3170 }
3171
3172 return this->got_;
3173 }
3174
3175 // Get the dynamic reloc section, creating it if necessary.
3176
3177 template<int size, bool big_endian>
3178 typename Target_powerpc<size, big_endian>::Reloc_section*
3179 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3180 {
3181 if (this->rela_dyn_ == NULL)
3182 {
3183 gold_assert(layout != NULL);
3184 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3185 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3186 elfcpp::SHF_ALLOC, this->rela_dyn_,
3187 ORDER_DYNAMIC_RELOCS, false);
3188 }
3189 return this->rela_dyn_;
3190 }
3191
3192 // Similarly, but for ifunc symbols get the one for ifunc.
3193
3194 template<int size, bool big_endian>
3195 typename Target_powerpc<size, big_endian>::Reloc_section*
3196 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3197 Layout* layout,
3198 bool for_ifunc)
3199 {
3200 if (!for_ifunc)
3201 return this->rela_dyn_section(layout);
3202
3203 if (this->iplt_ == NULL)
3204 this->make_iplt_section(symtab, layout);
3205 return this->iplt_->rel_plt();
3206 }
3207
3208 class Stub_control
3209 {
3210 public:
3211 // Determine the stub group size. The group size is the absolute
3212 // value of the parameter --stub-group-size. If --stub-group-size
3213 // is passed a negative value, we restrict stubs to be always after
3214 // the stubbed branches.
3215 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3216 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3217 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3218 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3219 owner_(NULL), output_section_(NULL)
3220 {
3221 }
3222
3223 // Return true iff input section can be handled by current stub
3224 // group.
3225 bool
3226 can_add_to_stub_group(Output_section* o,
3227 const Output_section::Input_section* i,
3228 bool has14);
3229
3230 const Output_section::Input_section*
3231 owner()
3232 { return owner_; }
3233
3234 Output_section*
3235 output_section()
3236 { return output_section_; }
3237
3238 void
3239 set_output_and_owner(Output_section* o,
3240 const Output_section::Input_section* i)
3241 {
3242 this->output_section_ = o;
3243 this->owner_ = i;
3244 }
3245
3246 private:
3247 typedef enum
3248 {
3249 // Initial state.
3250 NO_GROUP,
3251 // Adding group sections before the stubs.
3252 FINDING_STUB_SECTION,
3253 // Adding group sections after the stubs.
3254 HAS_STUB_SECTION
3255 } State;
3256
3257 uint32_t stub_group_size_;
3258 bool stubs_always_after_branch_;
3259 bool suppress_size_errors_;
3260 // True if a stub group can serve multiple output sections.
3261 bool multi_os_;
3262 State state_;
3263 // Current max size of group. Starts at stub_group_size_ but is
3264 // reduced to stub_group_size_/1024 on seeing a section with
3265 // external conditional branches.
3266 uint32_t group_size_;
3267 uint64_t group_start_addr_;
3268 // owner_ and output_section_ specify the section to which stubs are
3269 // attached. The stubs are placed at the end of this section.
3270 const Output_section::Input_section* owner_;
3271 Output_section* output_section_;
3272 };
3273
3274 // Return true iff input section can be handled by current stub
3275 // group. Sections are presented to this function in order,
3276 // so the first section is the head of the group.
3277
3278 bool
3279 Stub_control::can_add_to_stub_group(Output_section* o,
3280 const Output_section::Input_section* i,
3281 bool has14)
3282 {
3283 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3284 uint64_t this_size;
3285 uint64_t start_addr = o->address();
3286
3287 if (whole_sec)
3288 // .init and .fini sections are pasted together to form a single
3289 // function. We can't be adding stubs in the middle of the function.
3290 this_size = o->data_size();
3291 else
3292 {
3293 start_addr += i->relobj()->output_section_offset(i->shndx());
3294 this_size = i->data_size();
3295 }
3296
3297 uint64_t end_addr = start_addr + this_size;
3298 uint32_t group_size = this->stub_group_size_;
3299 if (has14)
3300 this->group_size_ = group_size = group_size >> 10;
3301
3302 if (this_size > group_size && !this->suppress_size_errors_)
3303 gold_warning(_("%s:%s exceeds group size"),
3304 i->relobj()->name().c_str(),
3305 i->relobj()->section_name(i->shndx()).c_str());
3306
3307 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3308 has14 ? " 14bit" : "",
3309 i->relobj()->name().c_str(),
3310 i->relobj()->section_name(i->shndx()).c_str(),
3311 (long long) this_size,
3312 (this->state_ == NO_GROUP
3313 ? this_size
3314 : (long long) end_addr - this->group_start_addr_));
3315
3316 if (this->state_ == NO_GROUP)
3317 {
3318 // Only here on very first use of Stub_control
3319 this->owner_ = i;
3320 this->output_section_ = o;
3321 this->state_ = FINDING_STUB_SECTION;
3322 this->group_size_ = group_size;
3323 this->group_start_addr_ = start_addr;
3324 return true;
3325 }
3326 else if (!this->multi_os_ && this->output_section_ != o)
3327 ;
3328 else if (this->state_ == HAS_STUB_SECTION)
3329 {
3330 // Can we add this section, which is after the stubs, to the
3331 // group?
3332 if (end_addr - this->group_start_addr_ <= this->group_size_)
3333 return true;
3334 }
3335 else if (this->state_ == FINDING_STUB_SECTION)
3336 {
3337 if ((whole_sec && this->output_section_ == o)
3338 || end_addr - this->group_start_addr_ <= this->group_size_)
3339 {
3340 // Stubs are added at the end of "owner_".
3341 this->owner_ = i;
3342 this->output_section_ = o;
3343 return true;
3344 }
3345 // The group before the stubs has reached maximum size.
3346 // Now see about adding sections after the stubs to the
3347 // group. If the current section has a 14-bit branch and
3348 // the group before the stubs exceeds group_size_ (because
3349 // they didn't have 14-bit branches), don't add sections
3350 // after the stubs: The size of stubs for such a large
3351 // group may exceed the reach of a 14-bit branch.
3352 if (!this->stubs_always_after_branch_
3353 && this_size <= this->group_size_
3354 && start_addr - this->group_start_addr_ <= this->group_size_)
3355 {
3356 gold_debug(DEBUG_TARGET, "adding after stubs");
3357 this->state_ = HAS_STUB_SECTION;
3358 this->group_start_addr_ = start_addr;
3359 return true;
3360 }
3361 }
3362 else
3363 gold_unreachable();
3364
3365 gold_debug(DEBUG_TARGET,
3366 !this->multi_os_ && this->output_section_ != o
3367 ? "nope, new output section\n"
3368 : "nope, didn't fit\n");
3369
3370 // The section fails to fit in the current group. Set up a few
3371 // things for the next group. owner_ and output_section_ will be
3372 // set later after we've retrieved those values for the current
3373 // group.
3374 this->state_ = FINDING_STUB_SECTION;
3375 this->group_size_ = group_size;
3376 this->group_start_addr_ = start_addr;
3377 return false;
3378 }
3379
3380 // Look over all the input sections, deciding where to place stubs.
3381
3382 template<int size, bool big_endian>
3383 void
3384 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3385 const Task*,
3386 bool no_size_errors)
3387 {
3388 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3389 parameters->options().stub_group_multi());
3390
3391 // Group input sections and insert stub table
3392 Stub_table_owner* table_owner = NULL;
3393 std::vector<Stub_table_owner*> tables;
3394 Layout::Section_list section_list;
3395 layout->get_executable_sections(&section_list);
3396 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3397 for (Layout::Section_list::iterator o = section_list.begin();
3398 o != section_list.end();
3399 ++o)
3400 {
3401 typedef Output_section::Input_section_list Input_section_list;
3402 for (Input_section_list::const_iterator i
3403 = (*o)->input_sections().begin();
3404 i != (*o)->input_sections().end();
3405 ++i)
3406 {
3407 if (i->is_input_section()
3408 || i->is_relaxed_input_section())
3409 {
3410 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3411 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3412 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3413 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3414 {
3415 table_owner->output_section = stub_control.output_section();
3416 table_owner->owner = stub_control.owner();
3417 stub_control.set_output_and_owner(*o, &*i);
3418 table_owner = NULL;
3419 }
3420 if (table_owner == NULL)
3421 {
3422 table_owner = new Stub_table_owner;
3423 tables.push_back(table_owner);
3424 }
3425 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3426 }
3427 }
3428 }
3429 if (table_owner != NULL)
3430 {
3431 table_owner->output_section = stub_control.output_section();
3432 table_owner->owner = stub_control.owner();;
3433 }
3434 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3435 t != tables.end();
3436 ++t)
3437 {
3438 Stub_table<size, big_endian>* stub_table;
3439
3440 if ((*t)->owner->is_input_section())
3441 stub_table = new Stub_table<size, big_endian>(this,
3442 (*t)->output_section,
3443 (*t)->owner,
3444 this->stub_tables_.size());
3445 else if ((*t)->owner->is_relaxed_input_section())
3446 stub_table = static_cast<Stub_table<size, big_endian>*>(
3447 (*t)->owner->relaxed_input_section());
3448 else
3449 gold_unreachable();
3450 this->stub_tables_.push_back(stub_table);
3451 delete *t;
3452 }
3453 }
3454
3455 template<int size>
3456 static unsigned long
3457 max_branch_delta (unsigned int r_type)
3458 {
3459 if (r_type == elfcpp::R_POWERPC_REL14
3460 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3461 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3462 return 1L << 15;
3463 if (r_type == elfcpp::R_POWERPC_REL24
3464 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3465 || r_type == elfcpp::R_PPC_PLTREL24
3466 || r_type == elfcpp::R_PPC_LOCAL24PC)
3467 return 1L << 25;
3468 return 0;
3469 }
3470
3471 // Return whether this branch is going via a plt call stub.
3472
3473 template<int size, bool big_endian>
3474 bool
3475 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3476 Powerpc_relobj<size, big_endian>* ppc_object,
3477 unsigned int shndx,
3478 Address offset,
3479 Target_powerpc* target,
3480 Symbol_table* symtab)
3481 {
3482 if (this->object_ != ppc_object
3483 || this->shndx_ != shndx
3484 || this->offset_ != offset)
3485 return false;
3486
3487 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3488 if (sym != NULL && sym->is_forwarder())
3489 sym = symtab->resolve_forwards(sym);
3490 if (target->replace_tls_get_addr(sym))
3491 sym = target->tls_get_addr_opt();
3492 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3493 if (gsym != NULL
3494 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3495 && !target->is_elfv2_localentry0(gsym))
3496 : (this->object_->local_has_plt_offset(this->r_sym_)
3497 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3498 {
3499 this->tocsave_ = 1;
3500 return true;
3501 }
3502 return false;
3503 }
3504
3505 // If this branch needs a plt call stub, or a long branch stub, make one.
3506
3507 template<int size, bool big_endian>
3508 bool
3509 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3510 Stub_table<size, big_endian>* stub_table,
3511 Stub_table<size, big_endian>* ifunc_stub_table,
3512 Symbol_table* symtab) const
3513 {
3514 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3515 Target_powerpc<size, big_endian>* target =
3516 static_cast<Target_powerpc<size, big_endian>*>(
3517 parameters->sized_target<size, big_endian>());
3518 if (sym != NULL && sym->is_forwarder())
3519 sym = symtab->resolve_forwards(sym);
3520 if (target->replace_tls_get_addr(sym))
3521 sym = target->tls_get_addr_opt();
3522 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3523 bool ok = true;
3524
3525 if (gsym != NULL
3526 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3527 : this->object_->local_has_plt_offset(this->r_sym_))
3528 {
3529 if (size == 64
3530 && gsym != NULL
3531 && target->abiversion() >= 2
3532 && !parameters->options().output_is_position_independent()
3533 && !is_branch_reloc<size>(this->r_type_))
3534 target->glink_section()->add_global_entry(gsym);
3535 else
3536 {
3537 if (stub_table == NULL
3538 && !(size == 32
3539 && gsym != NULL
3540 && !parameters->options().output_is_position_independent()
3541 && !is_branch_reloc<size>(this->r_type_)))
3542 stub_table = this->object_->stub_table(this->shndx_);
3543 if (stub_table == NULL)
3544 {
3545 // This is a ref from a data section to an ifunc symbol,
3546 // or a non-branch reloc for which we always want to use
3547 // one set of stubs for resolving function addresses.
3548 stub_table = ifunc_stub_table;
3549 }
3550 gold_assert(stub_table != NULL);
3551 Address from = this->object_->get_output_section_offset(this->shndx_);
3552 if (from != invalid_address)
3553 from += (this->object_->output_section(this->shndx_)->address()
3554 + this->offset_);
3555 if (gsym != NULL)
3556 ok = stub_table->add_plt_call_entry(from,
3557 this->object_, gsym,
3558 this->r_type_, this->addend_,
3559 this->tocsave_);
3560 else
3561 ok = stub_table->add_plt_call_entry(from,
3562 this->object_, this->r_sym_,
3563 this->r_type_, this->addend_,
3564 this->tocsave_);
3565 }
3566 }
3567 else
3568 {
3569 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3570 if (max_branch_offset == 0)
3571 return true;
3572 Address from = this->object_->get_output_section_offset(this->shndx_);
3573 gold_assert(from != invalid_address);
3574 from += (this->object_->output_section(this->shndx_)->address()
3575 + this->offset_);
3576 Address to;
3577 unsigned int other = 0;
3578 if (gsym != NULL)
3579 {
3580 switch (gsym->source())
3581 {
3582 case Symbol::FROM_OBJECT:
3583 {
3584 Object* symobj = gsym->object();
3585 if (symobj->is_dynamic()
3586 || symobj->pluginobj() != NULL)
3587 return true;
3588 bool is_ordinary;
3589 unsigned int shndx = gsym->shndx(&is_ordinary);
3590 if (shndx == elfcpp::SHN_UNDEF)
3591 return true;
3592 }
3593 break;
3594
3595 case Symbol::IS_UNDEFINED:
3596 return true;
3597
3598 default:
3599 break;
3600 }
3601 Symbol_table::Compute_final_value_status status;
3602 to = symtab->compute_final_value<size>(gsym, &status);
3603 if (status != Symbol_table::CFVS_OK)
3604 return true;
3605 if (size == 64)
3606 other = gsym->nonvis() >> 3;
3607 }
3608 else
3609 {
3610 const Symbol_value<size>* psymval
3611 = this->object_->local_symbol(this->r_sym_);
3612 Symbol_value<size> symval;
3613 if (psymval->is_section_symbol())
3614 symval.set_is_section_symbol();
3615 typedef Sized_relobj_file<size, big_endian> ObjType;
3616 typename ObjType::Compute_final_local_value_status status
3617 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3618 &symval, symtab);
3619 if (status != ObjType::CFLV_OK
3620 || !symval.has_output_value())
3621 return true;
3622 to = symval.value(this->object_, 0);
3623 if (size == 64)
3624 other = this->object_->st_other(this->r_sym_) >> 5;
3625 }
3626 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3627 to += this->addend_;
3628 if (stub_table == NULL)
3629 stub_table = this->object_->stub_table(this->shndx_);
3630 if (size == 64 && target->abiversion() < 2)
3631 {
3632 unsigned int dest_shndx;
3633 if (!target->symval_for_branch(symtab, gsym, this->object_,
3634 &to, &dest_shndx))
3635 return true;
3636 }
3637 unsigned int local_ent = 0;
3638 if (size == 64
3639 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC)
3640 local_ent = elfcpp::ppc64_decode_local_entry(other);
3641 Address delta = to + local_ent - from;
3642 if (delta + max_branch_offset >= 2 * max_branch_offset
3643 || (size == 64
3644 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3645 && (gsym != NULL
3646 ? this->object_->ppc64_needs_toc(gsym)
3647 : this->object_->ppc64_needs_toc(this->r_sym_))))
3648 {
3649 if (stub_table == NULL)
3650 {
3651 gold_warning(_("%s:%s: branch in non-executable section,"
3652 " no long branch stub for you"),
3653 this->object_->name().c_str(),
3654 this->object_->section_name(this->shndx_).c_str());
3655 return true;
3656 }
3657 bool save_res = (size == 64
3658 && gsym != NULL
3659 && gsym->source() == Symbol::IN_OUTPUT_DATA
3660 && gsym->output_data() == target->savres_section());
3661 ok = stub_table->add_long_branch_entry(this->object_,
3662 this->r_type_,
3663 from, to, other, save_res);
3664 }
3665 }
3666 if (!ok)
3667 gold_debug(DEBUG_TARGET,
3668 "branch at %s:%s+%#lx\n"
3669 "can't reach stub attached to %s:%s",
3670 this->object_->name().c_str(),
3671 this->object_->section_name(this->shndx_).c_str(),
3672 (unsigned long) this->offset_,
3673 stub_table->relobj()->name().c_str(),
3674 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3675
3676 return ok;
3677 }
3678
3679 // Relaxation hook. This is where we do stub generation.
3680
3681 template<int size, bool big_endian>
3682 bool
3683 Target_powerpc<size, big_endian>::do_relax(int pass,
3684 const Input_objects*,
3685 Symbol_table* symtab,
3686 Layout* layout,
3687 const Task* task)
3688 {
3689 unsigned int prev_brlt_size = 0;
3690 if (pass == 1)
3691 {
3692 bool thread_safe
3693 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3694 if (size == 64
3695 && this->abiversion() < 2
3696 && !thread_safe
3697 && !parameters->options().user_set_plt_thread_safe())
3698 {
3699 static const char* const thread_starter[] =
3700 {
3701 "pthread_create",
3702 /* libstdc++ */
3703 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3704 /* librt */
3705 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3706 "mq_notify", "create_timer",
3707 /* libanl */
3708 "getaddrinfo_a",
3709 /* libgomp */
3710 "GOMP_parallel",
3711 "GOMP_parallel_start",
3712 "GOMP_parallel_loop_static",
3713 "GOMP_parallel_loop_static_start",
3714 "GOMP_parallel_loop_dynamic",
3715 "GOMP_parallel_loop_dynamic_start",
3716 "GOMP_parallel_loop_guided",
3717 "GOMP_parallel_loop_guided_start",
3718 "GOMP_parallel_loop_runtime",
3719 "GOMP_parallel_loop_runtime_start",
3720 "GOMP_parallel_sections",
3721 "GOMP_parallel_sections_start",
3722 /* libgo */
3723 "__go_go",
3724 };
3725
3726 if (parameters->options().shared())
3727 thread_safe = true;
3728 else
3729 {
3730 for (unsigned int i = 0;
3731 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3732 i++)
3733 {
3734 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3735 thread_safe = (sym != NULL
3736 && sym->in_reg()
3737 && sym->in_real_elf());
3738 if (thread_safe)
3739 break;
3740 }
3741 }
3742 }
3743 this->plt_thread_safe_ = thread_safe;
3744 }
3745
3746 if (pass == 1)
3747 {
3748 this->stub_group_size_ = parameters->options().stub_group_size();
3749 bool no_size_errors = true;
3750 if (this->stub_group_size_ == 1)
3751 this->stub_group_size_ = 0x1c00000;
3752 else if (this->stub_group_size_ == -1)
3753 this->stub_group_size_ = -0x1e00000;
3754 else
3755 no_size_errors = false;
3756 this->group_sections(layout, task, no_size_errors);
3757 }
3758 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3759 {
3760 this->branch_lookup_table_.clear();
3761 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3762 p != this->stub_tables_.end();
3763 ++p)
3764 {
3765 (*p)->clear_stubs(true);
3766 }
3767 this->stub_tables_.clear();
3768 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3769 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3770 program_name, this->stub_group_size_);
3771 this->group_sections(layout, task, true);
3772 }
3773
3774 // We need address of stub tables valid for make_stub.
3775 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3776 p != this->stub_tables_.end();
3777 ++p)
3778 {
3779 const Powerpc_relobj<size, big_endian>* object
3780 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3781 Address off = object->get_output_section_offset((*p)->shndx());
3782 gold_assert(off != invalid_address);
3783 Output_section* os = (*p)->output_section();
3784 (*p)->set_address_and_size(os, off);
3785 }
3786
3787 if (pass != 1)
3788 {
3789 // Clear plt call stubs, long branch stubs and branch lookup table.
3790 prev_brlt_size = this->branch_lookup_table_.size();
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 {
3796 (*p)->clear_stubs(false);
3797 }
3798 }
3799
3800 // Build all the stubs.
3801 this->relax_failed_ = false;
3802 Stub_table<size, big_endian>* ifunc_stub_table
3803 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3804 Stub_table<size, big_endian>* one_stub_table
3805 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3806 for (typename Branches::const_iterator b = this->branch_info_.begin();
3807 b != this->branch_info_.end();
3808 b++)
3809 {
3810 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3811 && !this->relax_failed_)
3812 {
3813 this->relax_failed_ = true;
3814 this->relax_fail_count_++;
3815 if (this->relax_fail_count_ < 3)
3816 return true;
3817 }
3818 }
3819 bool do_resize = false;
3820 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3821 p != this->stub_tables_.end();
3822 ++p)
3823 if ((*p)->need_resize())
3824 {
3825 do_resize = true;
3826 break;
3827 }
3828 if (do_resize)
3829 {
3830 this->branch_lookup_table_.clear();
3831 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3832 p != this->stub_tables_.end();
3833 ++p)
3834 (*p)->set_resizing(true);
3835 for (typename Branches::const_iterator b = this->branch_info_.begin();
3836 b != this->branch_info_.end();
3837 b++)
3838 {
3839 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3840 && !this->relax_failed_)
3841 {
3842 this->relax_failed_ = true;
3843 this->relax_fail_count_++;
3844 if (this->relax_fail_count_ < 3)
3845 return true;
3846 }
3847 }
3848 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3849 p != this->stub_tables_.end();
3850 ++p)
3851 (*p)->set_resizing(false);
3852 }
3853
3854 // Did anything change size?
3855 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3856 bool again = num_huge_branches != prev_brlt_size;
3857 if (size == 64 && num_huge_branches != 0)
3858 this->make_brlt_section(layout);
3859 if (size == 64 && again)
3860 this->brlt_section_->set_current_size(num_huge_branches);
3861
3862 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3863 p != this->stub_tables_.rend();
3864 ++p)
3865 (*p)->remove_eh_frame(layout);
3866
3867 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3868 p != this->stub_tables_.end();
3869 ++p)
3870 (*p)->add_eh_frame(layout);
3871
3872 typedef Unordered_set<Output_section*> Output_sections;
3873 Output_sections os_need_update;
3874 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3875 p != this->stub_tables_.end();
3876 ++p)
3877 {
3878 if ((*p)->size_update())
3879 {
3880 again = true;
3881 os_need_update.insert((*p)->output_section());
3882 }
3883 }
3884
3885 // Set output section offsets for all input sections in an output
3886 // section that just changed size. Anything past the stubs will
3887 // need updating.
3888 for (typename Output_sections::iterator p = os_need_update.begin();
3889 p != os_need_update.end();
3890 p++)
3891 {
3892 Output_section* os = *p;
3893 Address off = 0;
3894 typedef Output_section::Input_section_list Input_section_list;
3895 for (Input_section_list::const_iterator i = os->input_sections().begin();
3896 i != os->input_sections().end();
3897 ++i)
3898 {
3899 off = align_address(off, i->addralign());
3900 if (i->is_input_section() || i->is_relaxed_input_section())
3901 i->relobj()->set_section_offset(i->shndx(), off);
3902 if (i->is_relaxed_input_section())
3903 {
3904 Stub_table<size, big_endian>* stub_table
3905 = static_cast<Stub_table<size, big_endian>*>(
3906 i->relaxed_input_section());
3907 Address stub_table_size = stub_table->set_address_and_size(os, off);
3908 off += stub_table_size;
3909 // After a few iterations, set current stub table size
3910 // as min size threshold, so later stub tables can only
3911 // grow in size.
3912 if (pass >= 4)
3913 stub_table->set_min_size_threshold(stub_table_size);
3914 }
3915 else
3916 off += i->data_size();
3917 }
3918 // If .branch_lt is part of this output section, then we have
3919 // just done the offset adjustment.
3920 os->clear_section_offsets_need_adjustment();
3921 }
3922
3923 if (size == 64
3924 && !again
3925 && num_huge_branches != 0
3926 && parameters->options().output_is_position_independent())
3927 {
3928 // Fill in the BRLT relocs.
3929 this->brlt_section_->reset_brlt_sizes();
3930 for (typename Branch_lookup_table::const_iterator p
3931 = this->branch_lookup_table_.begin();
3932 p != this->branch_lookup_table_.end();
3933 ++p)
3934 {
3935 this->brlt_section_->add_reloc(p->first, p->second);
3936 }
3937 this->brlt_section_->finalize_brlt_sizes();
3938 }
3939
3940 if (!again
3941 && (parameters->options().user_set_emit_stub_syms()
3942 ? parameters->options().emit_stub_syms()
3943 : (size == 64
3944 || parameters->options().output_is_position_independent()
3945 || parameters->options().emit_relocs())))
3946 {
3947 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3948 p != this->stub_tables_.end();
3949 ++p)
3950 (*p)->define_stub_syms(symtab);
3951
3952 if (this->glink_ != NULL)
3953 {
3954 int stub_size = this->glink_->pltresolve_size();
3955 Address value = -stub_size;
3956 if (size == 64)
3957 {
3958 value = 8;
3959 stub_size -= 8;
3960 }
3961 this->define_local(symtab, "__glink_PLTresolve",
3962 this->glink_, value, stub_size);
3963
3964 if (size != 64)
3965 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3966 }
3967 }
3968
3969 return again;
3970 }
3971
3972 template<int size, bool big_endian>
3973 void
3974 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3975 unsigned char* oview,
3976 uint64_t* paddress,
3977 off_t* plen) const
3978 {
3979 uint64_t address = plt->address();
3980 off_t len = plt->data_size();
3981
3982 if (plt == this->glink_)
3983 {
3984 // See Output_data_glink::do_write() for glink contents.
3985 if (len == 0)
3986 {
3987 // Static linking may need stubs, to support ifunc and long
3988 // branches. We need to create an output section for
3989 // .eh_frame early in the link process, to have a place to
3990 // attach stub .eh_frame info. We also need to have
3991 // registered a CIE that matches the stub CIE. Both of
3992 // these requirements are satisfied by creating an FDE and
3993 // CIE for .glink, even though static linking will leave
3994 // .glink zero length.
3995 // ??? Hopefully generating an FDE with a zero address range
3996 // won't confuse anything that consumes .eh_frame info.
3997 }
3998 else if (size == 64)
3999 {
4000 // There is one word before __glink_PLTresolve
4001 address += 8;
4002 len -= 8;
4003 }
4004 else if (parameters->options().output_is_position_independent())
4005 {
4006 // There are two FDEs for a position independent glink.
4007 // The first covers the branch table, the second
4008 // __glink_PLTresolve at the end of glink.
4009 off_t resolve_size = this->glink_->pltresolve_size();
4010 if (oview[9] == elfcpp::DW_CFA_nop)
4011 len -= resolve_size;
4012 else
4013 {
4014 address += len - resolve_size;
4015 len = resolve_size;
4016 }
4017 }
4018 }
4019 else
4020 {
4021 // Must be a stub table.
4022 const Stub_table<size, big_endian>* stub_table
4023 = static_cast<const Stub_table<size, big_endian>*>(plt);
4024 uint64_t stub_address = stub_table->stub_address();
4025 len -= stub_address - address;
4026 address = stub_address;
4027 }
4028
4029 *paddress = address;
4030 *plen = len;
4031 }
4032
4033 // A class to handle the PLT data.
4034
4035 template<int size, bool big_endian>
4036 class Output_data_plt_powerpc : public Output_section_data_build
4037 {
4038 public:
4039 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4040 size, big_endian> Reloc_section;
4041
4042 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4043 Reloc_section* plt_rel,
4044 const char* name)
4045 : Output_section_data_build(size == 32 ? 4 : 8),
4046 rel_(plt_rel),
4047 targ_(targ),
4048 name_(name)
4049 { }
4050
4051 // Add an entry to the PLT.
4052 void
4053 add_entry(Symbol*);
4054
4055 void
4056 add_ifunc_entry(Symbol*);
4057
4058 void
4059 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4060
4061 void
4062 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4063
4064 // Return the .rela.plt section data.
4065 Reloc_section*
4066 rel_plt() const
4067 {
4068 return this->rel_;
4069 }
4070
4071 // Return the number of PLT entries.
4072 unsigned int
4073 entry_count() const
4074 {
4075 if (this->current_data_size() == 0)
4076 return 0;
4077 return ((this->current_data_size() - this->first_plt_entry_offset())
4078 / this->plt_entry_size());
4079 }
4080
4081 protected:
4082 void
4083 do_adjust_output_section(Output_section* os)
4084 {
4085 os->set_entsize(0);
4086 }
4087
4088 // Write to a map file.
4089 void
4090 do_print_to_mapfile(Mapfile* mapfile) const
4091 { mapfile->print_output_data(this, this->name_); }
4092
4093 private:
4094 // Return the offset of the first non-reserved PLT entry.
4095 unsigned int
4096 first_plt_entry_offset() const
4097 {
4098 // IPLT and LPLT have no reserved entry.
4099 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4100 return 0;
4101 return this->targ_->first_plt_entry_offset();
4102 }
4103
4104 // Return the size of each PLT entry.
4105 unsigned int
4106 plt_entry_size() const
4107 {
4108 return this->targ_->plt_entry_size();
4109 }
4110
4111 // Write out the PLT data.
4112 void
4113 do_write(Output_file*);
4114
4115 // The reloc section.
4116 Reloc_section* rel_;
4117 // Allows access to .glink for do_write.
4118 Target_powerpc<size, big_endian>* targ_;
4119 // What to report in map file.
4120 const char *name_;
4121 };
4122
4123 // Add an entry to the PLT.
4124
4125 template<int size, bool big_endian>
4126 void
4127 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
4128 {
4129 if (!gsym->has_plt_offset())
4130 {
4131 section_size_type off = this->current_data_size();
4132 if (off == 0)
4133 off += this->first_plt_entry_offset();
4134 gsym->set_plt_offset(off);
4135 gsym->set_needs_dynsym_entry();
4136 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4137 this->rel_->add_global(gsym, dynrel, this, off, 0);
4138 off += this->plt_entry_size();
4139 this->set_current_data_size(off);
4140 }
4141 }
4142
4143 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4144
4145 template<int size, bool big_endian>
4146 void
4147 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4148 {
4149 if (!gsym->has_plt_offset())
4150 {
4151 section_size_type off = this->current_data_size();
4152 gsym->set_plt_offset(off);
4153 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4154 if (size == 64 && this->targ_->abiversion() < 2)
4155 dynrel = elfcpp::R_PPC64_JMP_IREL;
4156 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4157 off += this->plt_entry_size();
4158 this->set_current_data_size(off);
4159 }
4160 }
4161
4162 // Add an entry for a local symbol to the PLT.
4163
4164 template<int size, bool big_endian>
4165 void
4166 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4167 Sized_relobj_file<size, big_endian>* relobj,
4168 unsigned int local_sym_index)
4169 {
4170 if (!relobj->local_has_plt_offset(local_sym_index))
4171 {
4172 section_size_type off = this->current_data_size();
4173 relobj->set_local_plt_offset(local_sym_index, off);
4174 if (this->rel_)
4175 {
4176 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4177 if (size == 64 && this->targ_->abiversion() < 2)
4178 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4179 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4180 dynrel, this, off, 0);
4181 }
4182 off += this->plt_entry_size();
4183 this->set_current_data_size(off);
4184 }
4185 }
4186
4187 // Add an entry for a local ifunc symbol to the IPLT.
4188
4189 template<int size, bool big_endian>
4190 void
4191 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4192 Sized_relobj_file<size, big_endian>* relobj,
4193 unsigned int local_sym_index)
4194 {
4195 if (!relobj->local_has_plt_offset(local_sym_index))
4196 {
4197 section_size_type off = this->current_data_size();
4198 relobj->set_local_plt_offset(local_sym_index, off);
4199 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4200 if (size == 64 && this->targ_->abiversion() < 2)
4201 dynrel = elfcpp::R_PPC64_JMP_IREL;
4202 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4203 this, off, 0);
4204 off += this->plt_entry_size();
4205 this->set_current_data_size(off);
4206 }
4207 }
4208
4209 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4210 static const uint32_t add_2_2_11 = 0x7c425a14;
4211 static const uint32_t add_2_2_12 = 0x7c426214;
4212 static const uint32_t add_3_3_2 = 0x7c631214;
4213 static const uint32_t add_3_3_13 = 0x7c636a14;
4214 static const uint32_t add_3_12_2 = 0x7c6c1214;
4215 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4216 static const uint32_t add_11_0_11 = 0x7d605a14;
4217 static const uint32_t add_11_2_11 = 0x7d625a14;
4218 static const uint32_t add_11_11_2 = 0x7d6b1214;
4219 static const uint32_t add_12_11_12 = 0x7d8b6214;
4220 static const uint32_t addi_0_12 = 0x380c0000;
4221 static const uint32_t addi_2_2 = 0x38420000;
4222 static const uint32_t addi_3_3 = 0x38630000;
4223 static const uint32_t addi_11_11 = 0x396b0000;
4224 static const uint32_t addi_12_1 = 0x39810000;
4225 static const uint32_t addi_12_11 = 0x398b0000;
4226 static const uint32_t addi_12_12 = 0x398c0000;
4227 static const uint32_t addis_0_2 = 0x3c020000;
4228 static const uint32_t addis_0_13 = 0x3c0d0000;
4229 static const uint32_t addis_2_12 = 0x3c4c0000;
4230 static const uint32_t addis_11_2 = 0x3d620000;
4231 static const uint32_t addis_11_11 = 0x3d6b0000;
4232 static const uint32_t addis_11_30 = 0x3d7e0000;
4233 static const uint32_t addis_12_1 = 0x3d810000;
4234 static const uint32_t addis_12_2 = 0x3d820000;
4235 static const uint32_t addis_12_11 = 0x3d8b0000;
4236 static const uint32_t addis_12_12 = 0x3d8c0000;
4237 static const uint32_t b = 0x48000000;
4238 static const uint32_t bcl_20_31 = 0x429f0005;
4239 static const uint32_t bctr = 0x4e800420;
4240 static const uint32_t bctrl = 0x4e800421;
4241 static const uint32_t beqlr = 0x4d820020;
4242 static const uint32_t blr = 0x4e800020;
4243 static const uint32_t bnectr_p4 = 0x4ce20420;
4244 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4245 static const uint32_t cmpldi_2_0 = 0x28220000;
4246 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4247 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4248 static const uint32_t cror_15_15_15 = 0x4def7b82;
4249 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4250 static const uint32_t ld_0_1 = 0xe8010000;
4251 static const uint32_t ld_0_11 = 0xe80b0000;
4252 static const uint32_t ld_0_12 = 0xe80c0000;
4253 static const uint32_t ld_2_1 = 0xe8410000;
4254 static const uint32_t ld_2_2 = 0xe8420000;
4255 static const uint32_t ld_2_11 = 0xe84b0000;
4256 static const uint32_t ld_2_12 = 0xe84c0000;
4257 static const uint32_t ld_11_1 = 0xe9610000;
4258 static const uint32_t ld_11_2 = 0xe9620000;
4259 static const uint32_t ld_11_3 = 0xe9630000;
4260 static const uint32_t ld_11_11 = 0xe96b0000;
4261 static const uint32_t ld_12_2 = 0xe9820000;
4262 static const uint32_t ld_12_3 = 0xe9830000;
4263 static const uint32_t ld_12_11 = 0xe98b0000;
4264 static const uint32_t ld_12_12 = 0xe98c0000;
4265 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4266 static const uint32_t lfd_0_1 = 0xc8010000;
4267 static const uint32_t li_0_0 = 0x38000000;
4268 static const uint32_t li_11_0 = 0x39600000;
4269 static const uint32_t li_12_0 = 0x39800000;
4270 static const uint32_t lis_0 = 0x3c000000;
4271 static const uint32_t lis_2 = 0x3c400000;
4272 static const uint32_t lis_11 = 0x3d600000;
4273 static const uint32_t lis_12 = 0x3d800000;
4274 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4275 static const uint32_t lwz_0_12 = 0x800c0000;
4276 static const uint32_t lwz_11_3 = 0x81630000;
4277 static const uint32_t lwz_11_11 = 0x816b0000;
4278 static const uint32_t lwz_11_30 = 0x817e0000;
4279 static const uint32_t lwz_12_3 = 0x81830000;
4280 static const uint32_t lwz_12_12 = 0x818c0000;
4281 static const uint32_t lwzu_0_12 = 0x840c0000;
4282 static const uint32_t mflr_0 = 0x7c0802a6;
4283 static const uint32_t mflr_11 = 0x7d6802a6;
4284 static const uint32_t mflr_12 = 0x7d8802a6;
4285 static const uint32_t mr_0_3 = 0x7c601b78;
4286 static const uint32_t mr_3_0 = 0x7c030378;
4287 static const uint32_t mtctr_0 = 0x7c0903a6;
4288 static const uint32_t mtctr_11 = 0x7d6903a6;
4289 static const uint32_t mtctr_12 = 0x7d8903a6;
4290 static const uint32_t mtlr_0 = 0x7c0803a6;
4291 static const uint32_t mtlr_11 = 0x7d6803a6;
4292 static const uint32_t mtlr_12 = 0x7d8803a6;
4293 static const uint32_t nop = 0x60000000;
4294 static const uint32_t ori_0_0_0 = 0x60000000;
4295 static const uint32_t ori_11_11_0 = 0x616b0000;
4296 static const uint32_t ori_12_12_0 = 0x618c0000;
4297 static const uint32_t oris_12_12_0 = 0x658c0000;
4298 static const uint32_t sldi_11_11_34 = 0x796b1746;
4299 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4300 static const uint32_t srdi_0_0_2 = 0x7800f082;
4301 static const uint32_t std_0_1 = 0xf8010000;
4302 static const uint32_t std_0_12 = 0xf80c0000;
4303 static const uint32_t std_2_1 = 0xf8410000;
4304 static const uint32_t std_11_1 = 0xf9610000;
4305 static const uint32_t stfd_0_1 = 0xd8010000;
4306 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4307 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4308 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4309 static const uint32_t xor_2_12_12 = 0x7d826278;
4310 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4311
4312 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4313 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4314 static const uint64_t pnop = 0x0700000000000000ULL;
4315
4316 // Write out the PLT.
4317
4318 template<int size, bool big_endian>
4319 void
4320 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4321 {
4322 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4323 {
4324 const section_size_type offset = this->offset();
4325 const section_size_type oview_size
4326 = convert_to_section_size_type(this->data_size());
4327 unsigned char* const oview = of->get_output_view(offset, oview_size);
4328 unsigned char* pov = oview;
4329 unsigned char* endpov = oview + oview_size;
4330
4331 // The address of the .glink branch table
4332 const Output_data_glink<size, big_endian>* glink
4333 = this->targ_->glink_section();
4334 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4335
4336 while (pov < endpov)
4337 {
4338 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4339 pov += 4;
4340 branch_tab += 4;
4341 }
4342
4343 of->write_output_view(offset, oview_size, oview);
4344 }
4345 }
4346
4347 // Create the PLT section.
4348
4349 template<int size, bool big_endian>
4350 void
4351 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4352 Layout* layout)
4353 {
4354 if (this->plt_ == NULL)
4355 {
4356 if (this->got_ == NULL)
4357 this->got_section(symtab, layout);
4358
4359 if (this->glink_ == NULL)
4360 make_glink_section(layout);
4361
4362 // Ensure that .rela.dyn always appears before .rela.plt This is
4363 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4364 // needs to include .rela.plt in its range.
4365 this->rela_dyn_section(layout);
4366
4367 Reloc_section* plt_rel = new Reloc_section(false);
4368 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4369 elfcpp::SHF_ALLOC, plt_rel,
4370 ORDER_DYNAMIC_PLT_RELOCS, false);
4371 this->plt_
4372 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4373 "** PLT");
4374 layout->add_output_section_data(".plt",
4375 (size == 32
4376 ? elfcpp::SHT_PROGBITS
4377 : elfcpp::SHT_NOBITS),
4378 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4379 this->plt_,
4380 (size == 32
4381 ? ORDER_SMALL_DATA
4382 : ORDER_SMALL_BSS),
4383 false);
4384
4385 Output_section* rela_plt_os = plt_rel->output_section();
4386 rela_plt_os->set_info_section(this->plt_->output_section());
4387 }
4388 }
4389
4390 // Create the IPLT section.
4391
4392 template<int size, bool big_endian>
4393 void
4394 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4395 Layout* layout)
4396 {
4397 if (this->iplt_ == NULL)
4398 {
4399 this->make_plt_section(symtab, layout);
4400 this->make_lplt_section(layout);
4401
4402 Reloc_section* iplt_rel = new Reloc_section(false);
4403 if (this->rela_dyn_->output_section())
4404 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4405 this->iplt_
4406 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4407 "** IPLT");
4408 if (this->plt_->output_section())
4409 this->plt_->output_section()->add_output_section_data(this->iplt_);
4410 }
4411 }
4412
4413 // Create the LPLT section.
4414
4415 template<int size, bool big_endian>
4416 void
4417 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4418 {
4419 if (this->lplt_ == NULL)
4420 {
4421 Reloc_section* lplt_rel = NULL;
4422 if (parameters->options().output_is_position_independent())
4423 {
4424 lplt_rel = new Reloc_section(false);
4425 this->rela_dyn_section(layout);
4426 if (this->rela_dyn_->output_section())
4427 this->rela_dyn_->output_section()
4428 ->add_output_section_data(lplt_rel);
4429 }
4430 this->lplt_
4431 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4432 "** LPLT");
4433 this->make_brlt_section(layout);
4434 if (this->brlt_section_ && this->brlt_section_->output_section())
4435 this->brlt_section_->output_section()
4436 ->add_output_section_data(this->lplt_);
4437 else
4438 layout->add_output_section_data(".branch_lt",
4439 elfcpp::SHT_PROGBITS,
4440 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4441 this->lplt_,
4442 ORDER_RELRO,
4443 true);
4444 }
4445 }
4446
4447 // A section for huge long branch addresses, similar to plt section.
4448
4449 template<int size, bool big_endian>
4450 class Output_data_brlt_powerpc : public Output_section_data_build
4451 {
4452 public:
4453 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4454 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4455 size, big_endian> Reloc_section;
4456
4457 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4458 Reloc_section* brlt_rel)
4459 : Output_section_data_build(size == 32 ? 4 : 8),
4460 rel_(brlt_rel),
4461 targ_(targ)
4462 { }
4463
4464 void
4465 reset_brlt_sizes()
4466 {
4467 this->reset_data_size();
4468 this->rel_->reset_data_size();
4469 }
4470
4471 void
4472 finalize_brlt_sizes()
4473 {
4474 this->finalize_data_size();
4475 this->rel_->finalize_data_size();
4476 }
4477
4478 // Add a reloc for an entry in the BRLT.
4479 void
4480 add_reloc(Address to, unsigned int off)
4481 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4482
4483 // Update section and reloc section size.
4484 void
4485 set_current_size(unsigned int num_branches)
4486 {
4487 this->reset_address_and_file_offset();
4488 this->set_current_data_size(num_branches * 16);
4489 this->finalize_data_size();
4490 Output_section* os = this->output_section();
4491 os->set_section_offsets_need_adjustment();
4492 if (this->rel_ != NULL)
4493 {
4494 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4495 this->rel_->reset_address_and_file_offset();
4496 this->rel_->set_current_data_size(num_branches * reloc_size);
4497 this->rel_->finalize_data_size();
4498 Output_section* os = this->rel_->output_section();
4499 os->set_section_offsets_need_adjustment();
4500 }
4501 }
4502
4503 protected:
4504 void
4505 do_adjust_output_section(Output_section* os)
4506 {
4507 os->set_entsize(0);
4508 }
4509
4510 // Write to a map file.
4511 void
4512 do_print_to_mapfile(Mapfile* mapfile) const
4513 { mapfile->print_output_data(this, "** BRLT"); }
4514
4515 private:
4516 // Write out the BRLT data.
4517 void
4518 do_write(Output_file*);
4519
4520 // The reloc section.
4521 Reloc_section* rel_;
4522 Target_powerpc<size, big_endian>* targ_;
4523 };
4524
4525 // Make the branch lookup table section.
4526
4527 template<int size, bool big_endian>
4528 void
4529 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4530 {
4531 if (size == 64 && this->brlt_section_ == NULL)
4532 {
4533 Reloc_section* brlt_rel = NULL;
4534 bool is_pic = parameters->options().output_is_position_independent();
4535 if (is_pic)
4536 {
4537 // When PIC we can't fill in .branch_lt but must initialise at
4538 // runtime via dynamic relocations.
4539 this->rela_dyn_section(layout);
4540 brlt_rel = new Reloc_section(false);
4541 if (this->rela_dyn_->output_section())
4542 this->rela_dyn_->output_section()
4543 ->add_output_section_data(brlt_rel);
4544 }
4545 this->brlt_section_
4546 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4547 if (this->plt_ && is_pic && this->plt_->output_section())
4548 this->plt_->output_section()
4549 ->add_output_section_data(this->brlt_section_);
4550 else
4551 layout->add_output_section_data(".branch_lt",
4552 elfcpp::SHT_PROGBITS,
4553 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4554 this->brlt_section_,
4555 ORDER_RELRO,
4556 true);
4557 }
4558 }
4559
4560 // Write out .branch_lt when non-PIC.
4561
4562 template<int size, bool big_endian>
4563 void
4564 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4565 {
4566 if (size == 64 && !parameters->options().output_is_position_independent())
4567 {
4568 const section_size_type offset = this->offset();
4569 const section_size_type oview_size
4570 = convert_to_section_size_type(this->data_size());
4571 unsigned char* const oview = of->get_output_view(offset, oview_size);
4572
4573 this->targ_->write_branch_lookup_table(oview);
4574 of->write_output_view(offset, oview_size, oview);
4575 }
4576 }
4577
4578 static inline uint32_t
4579 l(uint32_t a)
4580 {
4581 return a & 0xffff;
4582 }
4583
4584 static inline uint32_t
4585 hi(uint32_t a)
4586 {
4587 return l(a >> 16);
4588 }
4589
4590 static inline uint32_t
4591 ha(uint32_t a)
4592 {
4593 return hi(a + 0x8000);
4594 }
4595
4596 static inline uint64_t
4597 d34(uint64_t v)
4598 {
4599 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4600 }
4601
4602 static inline uint64_t
4603 ha34(uint64_t v)
4604 {
4605 return (v + (1ULL << 33)) >> 34;
4606 }
4607
4608 template<int size>
4609 struct Eh_cie
4610 {
4611 static const unsigned char eh_frame_cie[12];
4612 };
4613
4614 template<int size>
4615 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4616 {
4617 1, // CIE version.
4618 'z', 'R', 0, // Augmentation string.
4619 4, // Code alignment.
4620 0x80 - size / 8 , // Data alignment.
4621 65, // RA reg.
4622 1, // Augmentation size.
4623 (elfcpp::DW_EH_PE_pcrel
4624 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4625 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4626 };
4627
4628 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4629 static const unsigned char glink_eh_frame_fde_64v1[] =
4630 {
4631 0, 0, 0, 0, // Replaced with offset to .glink.
4632 0, 0, 0, 0, // Replaced with size of .glink.
4633 0, // Augmentation size.
4634 elfcpp::DW_CFA_advance_loc + 2,
4635 elfcpp::DW_CFA_register, 65, 12,
4636 elfcpp::DW_CFA_advance_loc + 4,
4637 elfcpp::DW_CFA_restore_extended, 65
4638 };
4639
4640 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4641 static const unsigned char glink_eh_frame_fde_64v2[] =
4642 {
4643 0, 0, 0, 0, // Replaced with offset to .glink.
4644 0, 0, 0, 0, // Replaced with size of .glink.
4645 0, // Augmentation size.
4646 elfcpp::DW_CFA_advance_loc + 2,
4647 elfcpp::DW_CFA_register, 65, 0,
4648 elfcpp::DW_CFA_advance_loc + 2,
4649 elfcpp::DW_CFA_restore_extended, 65
4650 };
4651
4652 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4653 {
4654 0, 0, 0, 0, // Replaced with offset to .glink.
4655 0, 0, 0, 0, // Replaced with size of .glink.
4656 0, // Augmentation size.
4657 elfcpp::DW_CFA_advance_loc + 3,
4658 elfcpp::DW_CFA_register, 65, 0,
4659 elfcpp::DW_CFA_advance_loc + 2,
4660 elfcpp::DW_CFA_restore_extended, 65
4661 };
4662
4663 // Describe __glink_PLTresolve use of LR, 32-bit version.
4664 static const unsigned char glink_eh_frame_fde_32[] =
4665 {
4666 0, 0, 0, 0, // Replaced with offset to .glink.
4667 0, 0, 0, 0, // Replaced with size of .glink.
4668 0, // Augmentation size.
4669 elfcpp::DW_CFA_advance_loc + 2,
4670 elfcpp::DW_CFA_register, 65, 0,
4671 elfcpp::DW_CFA_advance_loc + 4,
4672 elfcpp::DW_CFA_restore_extended, 65
4673 };
4674
4675 static const unsigned char default_fde[] =
4676 {
4677 0, 0, 0, 0, // Replaced with offset to stubs.
4678 0, 0, 0, 0, // Replaced with size of stubs.
4679 0, // Augmentation size.
4680 elfcpp::DW_CFA_nop, // Pad.
4681 elfcpp::DW_CFA_nop,
4682 elfcpp::DW_CFA_nop
4683 };
4684
4685 template<bool big_endian>
4686 static inline void
4687 write_insn(unsigned char* p, uint32_t v)
4688 {
4689 elfcpp::Swap<32, big_endian>::writeval(p, v);
4690 }
4691
4692 template<int size>
4693 static inline unsigned int
4694 param_plt_align()
4695 {
4696 if (!parameters->options().user_set_plt_align())
4697 return size == 64 ? 32 : 8;
4698 return 1 << parameters->options().plt_align();
4699 }
4700
4701 // Stub_table holds information about plt and long branch stubs.
4702 // Stubs are built in an area following some input section determined
4703 // by group_sections(). This input section is converted to a relaxed
4704 // input section allowing it to be resized to accommodate the stubs
4705
4706 template<int size, bool big_endian>
4707 class Stub_table : public Output_relaxed_input_section
4708 {
4709 public:
4710 struct Plt_stub_ent
4711 {
4712 Plt_stub_ent(unsigned int off, unsigned int indx)
4713 : off_(off), indx_(indx), iter_(0), notoc_(0), toc_(0),
4714 r2save_(0), localentry0_(0), tocoff_(0)
4715 { }
4716
4717 unsigned int off_;
4718 unsigned int indx_;
4719 unsigned int iter_ : 1;
4720 unsigned int notoc_ : 1;
4721 unsigned int toc_ : 1;
4722 unsigned int r2save_ : 1;
4723 unsigned int localentry0_ : 1;
4724 unsigned int tocoff_ : 8;
4725 };
4726 struct Branch_stub_ent
4727 {
4728 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4729 : off_(off), iter_(0), notoc_(notoc), toc_(0), save_res_(save_res),
4730 other_(0), tocoff_(0)
4731 { }
4732
4733 unsigned int off_;
4734 unsigned int iter_ : 1;
4735 unsigned int notoc_ : 1;
4736 unsigned int toc_ : 1;
4737 unsigned int save_res_ : 1;
4738 unsigned int other_ : 3;
4739 unsigned int tocoff_ : 8;
4740 };
4741 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4742 static const Address invalid_address = static_cast<Address>(0) - 1;
4743
4744 Stub_table(Target_powerpc<size, big_endian>* targ,
4745 Output_section* output_section,
4746 const Output_section::Input_section* owner,
4747 uint32_t id)
4748 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4749 owner->relobj()
4750 ->section_addralign(owner->shndx())),
4751 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4752 orig_data_size_(owner->current_data_size()),
4753 plt_size_(0), last_plt_size_(0),
4754 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4755 need_save_res_(false), need_resize_(false), resizing_(false),
4756 uniq_(id)
4757 {
4758 this->set_output_section(output_section);
4759
4760 std::vector<Output_relaxed_input_section*> new_relaxed;
4761 new_relaxed.push_back(this);
4762 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4763 }
4764
4765 // Add a plt call stub.
4766 bool
4767 add_plt_call_entry(Address,
4768 const Sized_relobj_file<size, big_endian>*,
4769 const Symbol*,
4770 unsigned int,
4771 Address,
4772 bool);
4773
4774 bool
4775 add_plt_call_entry(Address,
4776 const Sized_relobj_file<size, big_endian>*,
4777 unsigned int,
4778 unsigned int,
4779 Address,
4780 bool);
4781
4782 // Find a given plt call stub.
4783 const Plt_stub_ent*
4784 find_plt_call_entry(const Symbol*) const;
4785
4786 const Plt_stub_ent*
4787 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4788 unsigned int) const;
4789
4790 const Plt_stub_ent*
4791 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4792 const Symbol*,
4793 unsigned int,
4794 Address) const;
4795
4796 const Plt_stub_ent*
4797 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4798 unsigned int,
4799 unsigned int,
4800 Address) const;
4801
4802 // Add a long branch stub.
4803 bool
4804 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4805 unsigned int, Address, Address, unsigned int, bool);
4806
4807 const Branch_stub_ent*
4808 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4809 Address) const;
4810
4811 bool
4812 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4813 {
4814 Address max_branch_offset = max_branch_delta<size>(r_type);
4815 if (max_branch_offset == 0)
4816 return true;
4817 gold_assert(from != invalid_address);
4818 Address loc = off + this->stub_address();
4819 return loc - from + max_branch_offset < 2 * max_branch_offset;
4820 }
4821
4822 void
4823 clear_stubs(bool all)
4824 {
4825 this->plt_call_stubs_.clear();
4826 this->plt_size_ = 0;
4827 this->long_branch_stubs_.clear();
4828 this->branch_size_ = 0;
4829 this->need_save_res_ = false;
4830 if (all)
4831 {
4832 this->last_plt_size_ = 0;
4833 this->last_branch_size_ = 0;
4834 }
4835 }
4836
4837 bool
4838 need_resize() const
4839 { return need_resize_; }
4840
4841 void
4842 set_resizing(bool val)
4843 {
4844 this->resizing_ = val;
4845 if (val)
4846 {
4847 this->need_resize_ = false;
4848 this->plt_size_ = 0;
4849 this->branch_size_ = 0;
4850 this->need_save_res_ = false;
4851 }
4852 }
4853
4854 Address
4855 set_address_and_size(const Output_section* os, Address off)
4856 {
4857 Address start_off = off;
4858 off += this->orig_data_size_;
4859 Address my_size = this->plt_size_ + this->branch_size_;
4860 if (this->need_save_res_)
4861 my_size += this->targ_->savres_section()->data_size();
4862 if (my_size != 0)
4863 off = align_address(off, this->stub_align());
4864 // Include original section size and alignment padding in size
4865 my_size += off - start_off;
4866 // Ensure new size is always larger than min size
4867 // threshold. Alignment requirement is included in "my_size", so
4868 // increase "my_size" does not invalidate alignment.
4869 if (my_size < this->min_size_threshold_)
4870 my_size = this->min_size_threshold_;
4871 this->reset_address_and_file_offset();
4872 this->set_current_data_size(my_size);
4873 this->set_address_and_file_offset(os->address() + start_off,
4874 os->offset() + start_off);
4875 return my_size;
4876 }
4877
4878 Address
4879 stub_address() const
4880 {
4881 return align_address(this->address() + this->orig_data_size_,
4882 this->stub_align());
4883 }
4884
4885 Address
4886 stub_offset() const
4887 {
4888 return align_address(this->offset() + this->orig_data_size_,
4889 this->stub_align());
4890 }
4891
4892 section_size_type
4893 plt_size() const
4894 { return this->plt_size_; }
4895
4896 section_size_type
4897 branch_size() const
4898 { return this->branch_size_; }
4899
4900 void
4901 set_min_size_threshold(Address min_size)
4902 { this->min_size_threshold_ = min_size; }
4903
4904 void
4905 define_stub_syms(Symbol_table*);
4906
4907 bool
4908 size_update()
4909 {
4910 Output_section* os = this->output_section();
4911 if (os->addralign() < this->stub_align())
4912 {
4913 os->set_addralign(this->stub_align());
4914 // FIXME: get rid of the insane checkpointing.
4915 // We can't increase alignment of the input section to which
4916 // stubs are attached; The input section may be .init which
4917 // is pasted together with other .init sections to form a
4918 // function. Aligning might insert zero padding resulting in
4919 // sigill. However we do need to increase alignment of the
4920 // output section so that the align_address() on offset in
4921 // set_address_and_size() adds the same padding as the
4922 // align_address() on address in stub_address().
4923 // What's more, we need this alignment for the layout done in
4924 // relaxation_loop_body() so that the output section starts at
4925 // a suitably aligned address.
4926 os->checkpoint_set_addralign(this->stub_align());
4927 }
4928 if (this->last_plt_size_ != this->plt_size_
4929 || this->last_branch_size_ != this->branch_size_)
4930 {
4931 this->last_plt_size_ = this->plt_size_;
4932 this->last_branch_size_ = this->branch_size_;
4933 return true;
4934 }
4935 return false;
4936 }
4937
4938 // Add .eh_frame info for this stub section.
4939 void
4940 add_eh_frame(Layout* layout);
4941
4942 // Remove .eh_frame info for this stub section.
4943 void
4944 remove_eh_frame(Layout* layout);
4945
4946 Target_powerpc<size, big_endian>*
4947 targ() const
4948 { return targ_; }
4949
4950 private:
4951 class Plt_stub_key;
4952 class Plt_stub_key_hash;
4953 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4954 Plt_stub_key_hash> Plt_stub_entries;
4955 class Branch_stub_key;
4956 class Branch_stub_key_hash;
4957 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4958 Branch_stub_key_hash> Branch_stub_entries;
4959
4960 // Alignment of stub section.
4961 unsigned int
4962 stub_align() const
4963 {
4964 unsigned int min_align = size == 64 ? 32 : 16;
4965 unsigned int user_align = 1 << parameters->options().plt_align();
4966 return std::max(user_align, min_align);
4967 }
4968
4969 // Return the plt offset for the given call stub.
4970 Address
4971 plt_off(typename Plt_stub_entries::const_iterator p,
4972 const Output_data_plt_powerpc<size, big_endian>** sec) const
4973 {
4974 const Symbol* gsym = p->first.sym_;
4975 if (gsym != NULL)
4976 return this->targ_->plt_off(gsym, sec);
4977 else
4978 {
4979 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4980 unsigned int local_sym_index = p->first.locsym_;
4981 return this->targ_->plt_off(relobj, local_sym_index, sec);
4982 }
4983 }
4984
4985 // Size of a given plt call stub.
4986 unsigned int
4987 plt_call_size(typename Plt_stub_entries::iterator p) const;
4988
4989 unsigned int
4990 plt_call_align(unsigned int bytes) const
4991 {
4992 unsigned int align = param_plt_align<size>();
4993 return (bytes + align - 1) & -align;
4994 }
4995
4996 // Return long branch stub size.
4997 unsigned int
4998 branch_stub_size(typename Branch_stub_entries::iterator p,
4999 bool* need_lt);
5000
5001 void
5002 build_tls_opt_head(unsigned char** pp, bool save_lr);
5003
5004 void
5005 build_tls_opt_tail(unsigned char* p);
5006
5007 void
5008 plt_error(const Plt_stub_key& p);
5009
5010 // Write out stubs.
5011 void
5012 do_write(Output_file*);
5013
5014 // Plt call stub keys.
5015 class Plt_stub_key
5016 {
5017 public:
5018 Plt_stub_key(const Symbol* sym)
5019 : sym_(sym), object_(0), addend_(0), locsym_(0)
5020 { }
5021
5022 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5023 unsigned int locsym_index)
5024 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5025 { }
5026
5027 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5028 const Symbol* sym,
5029 unsigned int r_type,
5030 Address addend)
5031 : sym_(sym), object_(0), addend_(0), locsym_(0)
5032 {
5033 if (size != 32)
5034 this->addend_ = addend;
5035 else if (parameters->options().output_is_position_independent()
5036 && (r_type == elfcpp::R_PPC_PLTREL24
5037 || r_type == elfcpp::R_POWERPC_PLTCALL))
5038 {
5039 this->addend_ = addend;
5040 if (this->addend_ >= 32768)
5041 this->object_ = object;
5042 }
5043 }
5044
5045 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5046 unsigned int locsym_index,
5047 unsigned int r_type,
5048 Address addend)
5049 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5050 {
5051 if (size != 32)
5052 this->addend_ = addend;
5053 else if (parameters->options().output_is_position_independent()
5054 && (r_type == elfcpp::R_PPC_PLTREL24
5055 || r_type == elfcpp::R_POWERPC_PLTCALL))
5056 this->addend_ = addend;
5057 }
5058
5059 bool operator==(const Plt_stub_key& that) const
5060 {
5061 return (this->sym_ == that.sym_
5062 && this->object_ == that.object_
5063 && this->addend_ == that.addend_
5064 && this->locsym_ == that.locsym_);
5065 }
5066
5067 const Symbol* sym_;
5068 const Sized_relobj_file<size, big_endian>* object_;
5069 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5070 unsigned int locsym_;
5071 };
5072
5073 class Plt_stub_key_hash
5074 {
5075 public:
5076 size_t operator()(const Plt_stub_key& ent) const
5077 {
5078 return (reinterpret_cast<uintptr_t>(ent.sym_)
5079 ^ reinterpret_cast<uintptr_t>(ent.object_)
5080 ^ ent.addend_
5081 ^ ent.locsym_);
5082 }
5083 };
5084
5085 // Long branch stub keys.
5086 class Branch_stub_key
5087 {
5088 public:
5089 Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
5090 : dest_(to), toc_base_off_(0)
5091 {
5092 if (size == 64)
5093 toc_base_off_ = obj->toc_base_offset();
5094 }
5095
5096 bool operator==(const Branch_stub_key& that) const
5097 {
5098 return (this->dest_ == that.dest_
5099 && (size == 32
5100 || this->toc_base_off_ == that.toc_base_off_));
5101 }
5102
5103 Address dest_;
5104 unsigned int toc_base_off_;
5105 };
5106
5107 class Branch_stub_key_hash
5108 {
5109 public:
5110 size_t operator()(const Branch_stub_key& key) const
5111 { return key.dest_ ^ key.toc_base_off_; }
5112 };
5113
5114 // In a sane world this would be a global.
5115 Target_powerpc<size, big_endian>* targ_;
5116 // Map sym/object/addend to stub offset.
5117 Plt_stub_entries plt_call_stubs_;
5118 // Map destination address to stub offset.
5119 Branch_stub_entries long_branch_stubs_;
5120 // size of input section
5121 section_size_type orig_data_size_;
5122 // size of stubs
5123 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5124 // Some rare cases cause (PR/20529) fluctuation in stub table
5125 // size, which leads to an endless relax loop. This is to be fixed
5126 // by, after the first few iterations, allowing only increase of
5127 // stub table size. This variable sets the minimal possible size of
5128 // a stub table, it is zero for the first few iterations, then
5129 // increases monotonically.
5130 Address min_size_threshold_;
5131 // Set if this stub group needs a copy of out-of-line register
5132 // save/restore functions.
5133 bool need_save_res_;
5134 // Set when notoc_/r2save_ changes after sizing a stub
5135 bool need_resize_;
5136 // Set when resizing stubs
5137 bool resizing_;
5138 // Per stub table unique identifier.
5139 uint32_t uniq_;
5140 };
5141
5142 // Add a plt call stub, if we do not already have one for this
5143 // sym/object/addend combo.
5144
5145 template<int size, bool big_endian>
5146 bool
5147 Stub_table<size, big_endian>::add_plt_call_entry(
5148 Address from,
5149 const Sized_relobj_file<size, big_endian>* object,
5150 const Symbol* gsym,
5151 unsigned int r_type,
5152 Address addend,
5153 bool tocsave)
5154 {
5155 Plt_stub_key key(object, gsym, r_type, addend);
5156 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5157 std::pair<typename Plt_stub_entries::iterator, bool> p
5158 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5159 if (size == 64)
5160 {
5161 if (p.second
5162 && this->targ_->is_elfv2_localentry0(gsym))
5163 {
5164 p.first->second.localentry0_ = 1;
5165 this->targ_->set_has_localentry0();
5166 }
5167 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5168 {
5169 if (!p.second && !p.first->second.notoc_
5170 && (!this->targ_->power10_stubs()
5171 || this->targ_->power10_stubs_auto()))
5172 this->need_resize_ = true;
5173 p.first->second.notoc_ = 1;
5174 }
5175 else
5176 {
5177 if (!p.second && !p.first->second.toc_)
5178 this->need_resize_ = true;
5179 p.first->second.toc_ = 1;
5180 if (!tocsave && !p.first->second.localentry0_)
5181 {
5182 if (!p.second && !p.first->second.r2save_)
5183 this->need_resize_ = true;
5184 p.first->second.r2save_ = 1;
5185 }
5186 }
5187 }
5188 if (p.second || (this->resizing_ && !p.first->second.iter_))
5189 {
5190 if (this->resizing_)
5191 {
5192 p.first->second.iter_ = 1;
5193 p.first->second.off_ = this->plt_size_;
5194 }
5195 this->plt_size_ += this->plt_call_size(p.first);
5196 if (this->targ_->is_tls_get_addr_opt(gsym))
5197 this->targ_->set_has_tls_get_addr_opt();
5198 this->plt_size_ = this->plt_call_align(this->plt_size_);
5199 }
5200 return this->can_reach_stub(from, p.first->second.off_, r_type);
5201 }
5202
5203 template<int size, bool big_endian>
5204 bool
5205 Stub_table<size, big_endian>::add_plt_call_entry(
5206 Address from,
5207 const Sized_relobj_file<size, big_endian>* object,
5208 unsigned int locsym_index,
5209 unsigned int r_type,
5210 Address addend,
5211 bool tocsave)
5212 {
5213 Plt_stub_key key(object, locsym_index, r_type, addend);
5214 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5215 std::pair<typename Plt_stub_entries::iterator, bool> p
5216 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5217 if (size == 64)
5218 {
5219 if (p.second
5220 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5221 {
5222 p.first->second.localentry0_ = 1;
5223 this->targ_->set_has_localentry0();
5224 }
5225 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5226 {
5227 if (!p.second && !p.first->second.notoc_
5228 && (!this->targ_->power10_stubs()
5229 || this->targ_->power10_stubs_auto()))
5230 this->need_resize_ = true;
5231 p.first->second.notoc_ = 1;
5232 }
5233 else
5234 {
5235 if (!p.second && !p.first->second.toc_)
5236 this->need_resize_ = true;
5237 p.first->second.toc_ = 1;
5238 if (!tocsave && !p.first->second.localentry0_)
5239 {
5240 if (!p.second && !p.first->second.r2save_)
5241 this->need_resize_ = true;
5242 p.first->second.r2save_ = 1;
5243 }
5244 }
5245 }
5246 if (p.second || (this->resizing_ && !p.first->second.iter_))
5247 {
5248 if (this->resizing_)
5249 {
5250 p.first->second.iter_ = 1;
5251 p.first->second.off_ = this->plt_size_;
5252 }
5253 this->plt_size_ += this->plt_call_size(p.first);
5254 this->plt_size_ = this->plt_call_align(this->plt_size_);
5255 }
5256 return this->can_reach_stub(from, p.first->second.off_, r_type);
5257 }
5258
5259 // Find a plt call stub.
5260
5261 template<int size, bool big_endian>
5262 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5263 Stub_table<size, big_endian>::find_plt_call_entry(
5264 const Sized_relobj_file<size, big_endian>* object,
5265 const Symbol* gsym,
5266 unsigned int r_type,
5267 Address addend) const
5268 {
5269 Plt_stub_key key(object, gsym, r_type, addend);
5270 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5271 if (p == this->plt_call_stubs_.end())
5272 return NULL;
5273 return &p->second;
5274 }
5275
5276 template<int size, bool big_endian>
5277 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5278 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5279 {
5280 Plt_stub_key key(gsym);
5281 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5282 if (p == this->plt_call_stubs_.end())
5283 return NULL;
5284 return &p->second;
5285 }
5286
5287 template<int size, bool big_endian>
5288 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5289 Stub_table<size, big_endian>::find_plt_call_entry(
5290 const Sized_relobj_file<size, big_endian>* object,
5291 unsigned int locsym_index,
5292 unsigned int r_type,
5293 Address addend) const
5294 {
5295 Plt_stub_key key(object, locsym_index, r_type, addend);
5296 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5297 if (p == this->plt_call_stubs_.end())
5298 return NULL;
5299 return &p->second;
5300 }
5301
5302 template<int size, bool big_endian>
5303 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5304 Stub_table<size, big_endian>::find_plt_call_entry(
5305 const Sized_relobj_file<size, big_endian>* object,
5306 unsigned int locsym_index) const
5307 {
5308 Plt_stub_key key(object, locsym_index);
5309 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5310 if (p == this->plt_call_stubs_.end())
5311 return NULL;
5312 return &p->second;
5313 }
5314
5315 // Add a long branch stub if we don't already have one to given
5316 // destination.
5317
5318 template<int size, bool big_endian>
5319 bool
5320 Stub_table<size, big_endian>::add_long_branch_entry(
5321 const Powerpc_relobj<size, big_endian>* object,
5322 unsigned int r_type,
5323 Address from,
5324 Address to,
5325 unsigned int other,
5326 bool save_res)
5327 {
5328 Branch_stub_key key(object, to);
5329 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5330 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5331 std::pair<typename Branch_stub_entries::iterator, bool> p
5332 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5333 if (notoc)
5334 {
5335 if (!p.second && !p.first->second.notoc_)
5336 this->need_resize_ = true;
5337 p.first->second.notoc_ = true;
5338 }
5339 else
5340 {
5341 if (!p.second && !p.first->second.toc_)
5342 this->need_resize_ = true;
5343 p.first->second.toc_ = true;
5344 }
5345 if (size == 64 && p.first->second.other_ == 0)
5346 p.first->second.other_ = other;
5347 gold_assert(save_res == p.first->second.save_res_);
5348 if (p.second || (this->resizing_ && !p.first->second.iter_))
5349 {
5350 if (this->resizing_)
5351 {
5352 p.first->second.iter_ = 1;
5353 p.first->second.off_ = this->branch_size_;
5354 }
5355 if (save_res)
5356 this->need_save_res_ = true;
5357 else
5358 {
5359 bool need_lt = false;
5360 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5361 this->branch_size_ += stub_size;
5362 if (size == 64 && need_lt)
5363 this->targ_->add_branch_lookup_table(to);
5364 }
5365 }
5366 return this->can_reach_stub(from, p.first->second.off_, r_type);
5367 }
5368
5369 // Find long branch stub offset.
5370
5371 template<int size, bool big_endian>
5372 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5373 Stub_table<size, big_endian>::find_long_branch_entry(
5374 const Powerpc_relobj<size, big_endian>* object,
5375 Address to) const
5376 {
5377 Branch_stub_key key(object, to);
5378 typename Branch_stub_entries::const_iterator p
5379 = this->long_branch_stubs_.find(key);
5380 if (p == this->long_branch_stubs_.end())
5381 return NULL;
5382 return &p->second;
5383 }
5384
5385 template<bool big_endian>
5386 static void
5387 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5388 {
5389 delta /= 4;
5390 if (delta < 64)
5391 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5392 else if (delta < 256)
5393 {
5394 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5395 fde.push_back(delta);
5396 }
5397 else if (delta < 65536)
5398 {
5399 fde.resize(fde.size() + 3);
5400 unsigned char *p = &*fde.end() - 3;
5401 *p++ = elfcpp::DW_CFA_advance_loc2;
5402 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5403 }
5404 else
5405 {
5406 fde.resize(fde.size() + 5);
5407 unsigned char *p = &*fde.end() - 5;
5408 *p++ = elfcpp::DW_CFA_advance_loc4;
5409 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5410 }
5411 }
5412
5413 template<typename T>
5414 static bool
5415 stub_sort(T s1, T s2)
5416 {
5417 return s1->second.off_ < s2->second.off_;
5418 }
5419
5420 // Add .eh_frame info for this stub section. Unlike other linker
5421 // generated .eh_frame this is added late in the link, because we
5422 // only want the .eh_frame info if this particular stub section is
5423 // non-empty.
5424
5425 template<int size, bool big_endian>
5426 void
5427 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5428 {
5429 if (size != 64
5430 || !parameters->options().ld_generated_unwind_info())
5431 return;
5432
5433 // Since we add stub .eh_frame info late, it must be placed
5434 // after all other linker generated .eh_frame info so that
5435 // merge mapping need not be updated for input sections.
5436 // There is no provision to use a different CIE to that used
5437 // by .glink.
5438 if (!this->targ_->has_glink())
5439 return;
5440
5441 typedef typename Plt_stub_entries::iterator plt_iter;
5442 std::vector<plt_iter> calls;
5443 if (!this->plt_call_stubs_.empty())
5444 for (plt_iter cs = this->plt_call_stubs_.begin();
5445 cs != this->plt_call_stubs_.end();
5446 ++cs)
5447 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5448 && cs->second.r2save_
5449 && !cs->second.localentry0_)
5450 || (cs->second.notoc_
5451 && !this->targ_->power10_stubs()))
5452 calls.push_back(cs);
5453 if (calls.size() > 1)
5454 std::stable_sort(calls.begin(), calls.end(),
5455 stub_sort<plt_iter>);
5456
5457 typedef typename Branch_stub_entries::const_iterator branch_iter;
5458 std::vector<branch_iter> branches;
5459 if (!this->long_branch_stubs_.empty()
5460 && !this->targ_->power10_stubs())
5461 for (branch_iter bs = this->long_branch_stubs_.begin();
5462 bs != this->long_branch_stubs_.end();
5463 ++bs)
5464 if (bs->second.notoc_)
5465 branches.push_back(bs);
5466 if (branches.size() > 1)
5467 std::stable_sort(branches.begin(), branches.end(),
5468 stub_sort<branch_iter>);
5469
5470 if (calls.empty() && branches.empty())
5471 return;
5472
5473 unsigned int last_eh_loc = 0;
5474 // offset pcrel sdata4, size udata4, and augmentation size byte.
5475 std::vector<unsigned char> fde(9, 0);
5476
5477 for (unsigned int i = 0; i < calls.size(); i++)
5478 {
5479 plt_iter cs = calls[i];
5480 unsigned int off = cs->second.off_;
5481 // The __tls_get_addr_opt call stub needs to describe where
5482 // it saves LR, to support exceptions that might be thrown
5483 // from __tls_get_addr, and to support asynchronous exceptions.
5484 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5485 {
5486 off += 7 * 4;
5487 if (cs->second.r2save_
5488 && !cs->second.localentry0_)
5489 {
5490 off += 2 * 4;
5491 eh_advance<big_endian>(fde, off - last_eh_loc);
5492 fde.resize(fde.size() + 6);
5493 unsigned char* p = &*fde.end() - 6;
5494 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5495 *p++ = 65;
5496 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5497 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5498 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5499 *p++ = elfcpp::DW_CFA_restore_extended;
5500 *p++ = 65;
5501 last_eh_loc = off + delta;
5502 continue;
5503 }
5504 }
5505 // notoc stubs also should describe LR changes, to support
5506 // asynchronous exceptions.
5507 off += (cs->second.r2save_ ? 4 : 0) + 8;
5508 eh_advance<big_endian>(fde, off - last_eh_loc);
5509 fde.resize(fde.size() + 6);
5510 unsigned char* p = &*fde.end() - 6;
5511 *p++ = elfcpp::DW_CFA_register;
5512 *p++ = 65;
5513 *p++ = 12;
5514 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5515 *p++ = elfcpp::DW_CFA_restore_extended;
5516 *p++ = 65;
5517 last_eh_loc = off + 8;
5518 }
5519
5520 for (unsigned int i = 0; i < branches.size(); i++)
5521 {
5522 branch_iter bs = branches[i];
5523 unsigned int off = bs->second.off_ + 8;
5524 eh_advance<big_endian>(fde, off - last_eh_loc);
5525 fde.resize(fde.size() + 6);
5526 unsigned char* p = &*fde.end() - 6;
5527 *p++ = elfcpp::DW_CFA_register;
5528 *p++ = 65;
5529 *p++ = 12;
5530 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5531 *p++ = elfcpp::DW_CFA_restore_extended;
5532 *p++ = 65;
5533 last_eh_loc = off + 8;
5534 }
5535
5536 layout->add_eh_frame_for_plt(this,
5537 Eh_cie<size>::eh_frame_cie,
5538 sizeof (Eh_cie<size>::eh_frame_cie),
5539 &*fde.begin(), fde.size());
5540 }
5541
5542 template<int size, bool big_endian>
5543 void
5544 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5545 {
5546 if (size == 64
5547 && parameters->options().ld_generated_unwind_info()
5548 && this->targ_->has_glink())
5549 layout->remove_eh_frame_for_plt(this,
5550 Eh_cie<size>::eh_frame_cie,
5551 sizeof (Eh_cie<size>::eh_frame_cie));
5552 }
5553
5554 // A class to handle .glink.
5555
5556 template<int size, bool big_endian>
5557 class Output_data_glink : public Output_section_data
5558 {
5559 public:
5560 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5561 static const Address invalid_address = static_cast<Address>(0) - 1;
5562
5563 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5564 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5565 end_branch_table_(), ge_size_(0)
5566 { }
5567
5568 void
5569 add_eh_frame(Layout* layout);
5570
5571 void
5572 add_global_entry(const Symbol*);
5573
5574 Address
5575 find_global_entry(const Symbol*) const;
5576
5577 unsigned int
5578 global_entry_align(unsigned int off) const
5579 {
5580 unsigned int align = param_plt_align<size>();
5581 return (off + align - 1) & -align;
5582 }
5583
5584 unsigned int
5585 global_entry_off() const
5586 {
5587 return this->global_entry_align(this->end_branch_table_);
5588 }
5589
5590 Address
5591 global_entry_address() const
5592 {
5593 gold_assert(this->is_data_size_valid());
5594 return this->address() + this->global_entry_off();
5595 }
5596
5597 int
5598 pltresolve_size() const
5599 {
5600 if (size == 64)
5601 return (8
5602 + (this->targ_->abiversion() < 2 ? 11 * 4
5603 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5604 return 16 * 4;
5605 }
5606
5607 protected:
5608 // Write to a map file.
5609 void
5610 do_print_to_mapfile(Mapfile* mapfile) const
5611 { mapfile->print_output_data(this, _("** glink")); }
5612
5613 private:
5614 void
5615 set_final_data_size();
5616
5617 // Write out .glink
5618 void
5619 do_write(Output_file*);
5620
5621 // Allows access to .got and .plt for do_write.
5622 Target_powerpc<size, big_endian>* targ_;
5623
5624 // Map sym to stub offset.
5625 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5626 Global_entry_stub_entries global_entry_stubs_;
5627
5628 unsigned int end_branch_table_, ge_size_;
5629 };
5630
5631 template<int size, bool big_endian>
5632 void
5633 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5634 {
5635 if (!parameters->options().ld_generated_unwind_info())
5636 return;
5637
5638 if (size == 64)
5639 {
5640 if (this->targ_->abiversion() < 2)
5641 layout->add_eh_frame_for_plt(this,
5642 Eh_cie<64>::eh_frame_cie,
5643 sizeof (Eh_cie<64>::eh_frame_cie),
5644 glink_eh_frame_fde_64v1,
5645 sizeof (glink_eh_frame_fde_64v1));
5646 else if (this->targ_->has_localentry0())
5647 layout->add_eh_frame_for_plt(this,
5648 Eh_cie<64>::eh_frame_cie,
5649 sizeof (Eh_cie<64>::eh_frame_cie),
5650 glink_eh_frame_fde_64v2_localentry0,
5651 sizeof (glink_eh_frame_fde_64v2));
5652 else
5653 layout->add_eh_frame_for_plt(this,
5654 Eh_cie<64>::eh_frame_cie,
5655 sizeof (Eh_cie<64>::eh_frame_cie),
5656 glink_eh_frame_fde_64v2,
5657 sizeof (glink_eh_frame_fde_64v2));
5658 }
5659 else
5660 {
5661 // 32-bit .glink can use the default since the CIE return
5662 // address reg, LR, is valid.
5663 layout->add_eh_frame_for_plt(this,
5664 Eh_cie<32>::eh_frame_cie,
5665 sizeof (Eh_cie<32>::eh_frame_cie),
5666 default_fde,
5667 sizeof (default_fde));
5668 // Except where LR is used in a PIC __glink_PLTresolve.
5669 if (parameters->options().output_is_position_independent())
5670 layout->add_eh_frame_for_plt(this,
5671 Eh_cie<32>::eh_frame_cie,
5672 sizeof (Eh_cie<32>::eh_frame_cie),
5673 glink_eh_frame_fde_32,
5674 sizeof (glink_eh_frame_fde_32));
5675 }
5676 }
5677
5678 template<int size, bool big_endian>
5679 void
5680 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5681 {
5682 unsigned int off = this->global_entry_align(this->ge_size_);
5683 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5684 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5685 if (p.second)
5686 this->ge_size_ = off + 16;
5687 }
5688
5689 template<int size, bool big_endian>
5690 typename Output_data_glink<size, big_endian>::Address
5691 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5692 {
5693 typename Global_entry_stub_entries::const_iterator p
5694 = this->global_entry_stubs_.find(gsym);
5695 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5696 }
5697
5698 template<int size, bool big_endian>
5699 void
5700 Output_data_glink<size, big_endian>::set_final_data_size()
5701 {
5702 unsigned int count = this->targ_->plt_entry_count();
5703 section_size_type total = 0;
5704
5705 if (count != 0)
5706 {
5707 if (size == 32)
5708 {
5709 // space for branch table
5710 total += 4 * (count - 1);
5711
5712 total += -total & 15;
5713 total += this->pltresolve_size();
5714 }
5715 else
5716 {
5717 total += this->pltresolve_size();
5718
5719 // space for branch table
5720 total += 4 * count;
5721 if (this->targ_->abiversion() < 2)
5722 {
5723 total += 4 * count;
5724 if (count > 0x8000)
5725 total += 4 * (count - 0x8000);
5726 }
5727 }
5728 }
5729 this->end_branch_table_ = total;
5730 total = this->global_entry_align(total);
5731 total += this->ge_size_;
5732
5733 this->set_data_size(total);
5734 }
5735
5736 // Define symbols on stubs, identifying the stub.
5737
5738 template<int size, bool big_endian>
5739 void
5740 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5741 {
5742 if (!this->plt_call_stubs_.empty())
5743 {
5744 // The key for the plt call stub hash table includes addresses,
5745 // therefore traversal order depends on those addresses, which
5746 // can change between runs if gold is a PIE. Unfortunately the
5747 // output .symtab ordering depends on the order in which symbols
5748 // are added to the linker symtab. We want reproducible output
5749 // so must sort the call stub symbols.
5750 typedef typename Plt_stub_entries::iterator plt_iter;
5751 std::vector<plt_iter> sorted;
5752 sorted.resize(this->plt_call_stubs_.size());
5753
5754 for (plt_iter cs = this->plt_call_stubs_.begin();
5755 cs != this->plt_call_stubs_.end();
5756 ++cs)
5757 sorted[cs->second.indx_] = cs;
5758
5759 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5760 {
5761 plt_iter cs = sorted[i];
5762 char add[10];
5763 add[0] = 0;
5764 if (cs->first.addend_ != 0)
5765 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5766 char obj[10];
5767 obj[0] = 0;
5768 if (cs->first.object_)
5769 {
5770 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5771 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5772 sprintf(obj, "%x:", ppcobj->uniq());
5773 }
5774 char localname[9];
5775 const char *symname;
5776 if (cs->first.sym_ == NULL)
5777 {
5778 sprintf(localname, "%x", cs->first.locsym_);
5779 symname = localname;
5780 }
5781 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5782 symname = this->targ_->tls_get_addr_opt()->name();
5783 else
5784 symname = cs->first.sym_->name();
5785 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5786 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5787 Address value
5788 = this->stub_address() - this->address() + cs->second.off_;
5789 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5790 this->targ_->define_local(symtab, name, this, value, stub_size);
5791 }
5792 }
5793
5794 typedef typename Branch_stub_entries::iterator branch_iter;
5795 for (branch_iter bs = this->long_branch_stubs_.begin();
5796 bs != this->long_branch_stubs_.end();
5797 ++bs)
5798 {
5799 if (bs->second.save_res_)
5800 continue;
5801
5802 char* name = new char[8 + 13 + 16 + 1];
5803 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5804 static_cast<unsigned long long>(bs->first.dest_));
5805 Address value = (this->stub_address() - this->address()
5806 + this->plt_size_ + bs->second.off_);
5807 bool need_lt = false;
5808 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5809 this->targ_->define_local(symtab, name, this, value, stub_size);
5810 }
5811 }
5812
5813 // Emit the start of a __tls_get_addr_opt plt call stub.
5814
5815 template<int size, bool big_endian>
5816 void
5817 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5818 bool save_lr)
5819 {
5820 unsigned char* p = *pp;
5821 if (size == 64)
5822 {
5823 write_insn<big_endian>(p, ld_11_3 + 0);
5824 p += 4;
5825 write_insn<big_endian>(p, ld_12_3 + 8);
5826 p += 4;
5827 write_insn<big_endian>(p, mr_0_3);
5828 p += 4;
5829 write_insn<big_endian>(p, cmpdi_11_0);
5830 p += 4;
5831 write_insn<big_endian>(p, add_3_12_13);
5832 p += 4;
5833 write_insn<big_endian>(p, beqlr);
5834 p += 4;
5835 write_insn<big_endian>(p, mr_3_0);
5836 p += 4;
5837 if (save_lr)
5838 {
5839 write_insn<big_endian>(p, mflr_11);
5840 p += 4;
5841 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5842 p += 4;
5843 }
5844 }
5845 else
5846 {
5847 write_insn<big_endian>(p, lwz_11_3 + 0);
5848 p += 4;
5849 write_insn<big_endian>(p, lwz_12_3 + 4);
5850 p += 4;
5851 write_insn<big_endian>(p, mr_0_3);
5852 p += 4;
5853 write_insn<big_endian>(p, cmpwi_11_0);
5854 p += 4;
5855 write_insn<big_endian>(p, add_3_12_2);
5856 p += 4;
5857 write_insn<big_endian>(p, beqlr);
5858 p += 4;
5859 write_insn<big_endian>(p, mr_3_0);
5860 p += 4;
5861 write_insn<big_endian>(p, nop);
5862 p += 4;
5863 }
5864 *pp = p;
5865 }
5866
5867 // Emit the tail of a __tls_get_addr_opt plt call stub.
5868
5869 template<int size, bool big_endian>
5870 void
5871 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5872 {
5873 write_insn<big_endian>(p, bctrl);
5874 p += 4;
5875 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5876 p += 4;
5877 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5878 p += 4;
5879 write_insn<big_endian>(p, mtlr_11);
5880 p += 4;
5881 write_insn<big_endian>(p, blr);
5882 }
5883
5884 // Emit pc-relative plt call stub code.
5885
5886 template<bool big_endian>
5887 static unsigned char*
5888 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5889 {
5890 uint64_t insn;
5891 if (off - odd + (1ULL << 33) < 1ULL << 34)
5892 {
5893 off -= odd;
5894 if (odd)
5895 {
5896 write_insn<big_endian>(p, nop);
5897 p += 4;
5898 }
5899 if (load)
5900 insn = pld_12_pc;
5901 else
5902 insn = paddi_12_pc;
5903 insn |= d34(off);
5904 write_insn<big_endian>(p, insn >> 32);
5905 p += 4;
5906 write_insn<big_endian>(p, insn & 0xffffffff);
5907 }
5908 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5909 {
5910 off -= 8 - odd;
5911 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5912 p += 4;
5913 if (!odd)
5914 {
5915 write_insn<big_endian>(p, sldi_11_11_34);
5916 p += 4;
5917 }
5918 insn = paddi_12_pc | d34(off);
5919 write_insn<big_endian>(p, insn >> 32);
5920 p += 4;
5921 write_insn<big_endian>(p, insn & 0xffffffff);
5922 p += 4;
5923 if (odd)
5924 {
5925 write_insn<big_endian>(p, sldi_11_11_34);
5926 p += 4;
5927 }
5928 if (load)
5929 write_insn<big_endian>(p, ldx_12_11_12);
5930 else
5931 write_insn<big_endian>(p, add_12_11_12);
5932 }
5933 else
5934 {
5935 off -= odd + 8;
5936 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5937 p += 4;
5938 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5939 p += 4;
5940 if (odd)
5941 {
5942 write_insn<big_endian>(p, sldi_11_11_34);
5943 p += 4;
5944 }
5945 insn = paddi_12_pc | d34(off);
5946 write_insn<big_endian>(p, insn >> 32);
5947 p += 4;
5948 write_insn<big_endian>(p, insn & 0xffffffff);
5949 p += 4;
5950 if (!odd)
5951 {
5952 write_insn<big_endian>(p, sldi_11_11_34);
5953 p += 4;
5954 }
5955 if (load)
5956 write_insn<big_endian>(p, ldx_12_11_12);
5957 else
5958 write_insn<big_endian>(p, add_12_11_12);
5959 }
5960 p += 4;
5961 return p;
5962 }
5963
5964 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5965 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5966 // mflr %r12
5967 // bcl 20,31,1f
5968 // 1: mflr %r11
5969 // mtlr %r12
5970 // lis %r12,xxx-1b@highest
5971 // ori %r12,%r12,xxx-1b@higher
5972 // sldi %r12,%r12,32
5973 // oris %r12,%r12,xxx-1b@high
5974 // ori %r12,%r12,xxx-1b@l
5975 // add/ldx %r12,%r11,%r12
5976
5977 template<bool big_endian>
5978 static unsigned char*
5979 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5980 {
5981 write_insn<big_endian>(p, mflr_12);
5982 p += 4;
5983 write_insn<big_endian>(p, bcl_20_31);
5984 p += 4;
5985 write_insn<big_endian>(p, mflr_11);
5986 p += 4;
5987 write_insn<big_endian>(p, mtlr_12);
5988 p += 4;
5989 if (off + 0x8000 < 0x10000)
5990 {
5991 if (load)
5992 write_insn<big_endian>(p, ld_12_11 + l(off));
5993 else
5994 write_insn<big_endian>(p, addi_12_11 + l(off));
5995 }
5996 else if (off + 0x80008000ULL < 0x100000000ULL)
5997 {
5998 write_insn<big_endian>(p, addis_12_11 + ha(off));
5999 p += 4;
6000 if (load)
6001 write_insn<big_endian>(p, ld_12_12 + l(off));
6002 else
6003 write_insn<big_endian>(p, addi_12_12 + l(off));
6004 }
6005 else
6006 {
6007 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6008 {
6009 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6010 p += 4;
6011 }
6012 else
6013 {
6014 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6015 p += 4;
6016 if (((off >> 32) & 0xffff) != 0)
6017 {
6018 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6019 p += 4;
6020 }
6021 }
6022 if (((off >> 32) & 0xffffffffULL) != 0)
6023 {
6024 write_insn<big_endian>(p, sldi_12_12_32);
6025 p += 4;
6026 }
6027 if (hi(off) != 0)
6028 {
6029 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6030 p += 4;
6031 }
6032 if (l(off) != 0)
6033 {
6034 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6035 p += 4;
6036 }
6037 if (load)
6038 write_insn<big_endian>(p, ldx_12_11_12);
6039 else
6040 write_insn<big_endian>(p, add_12_11_12);
6041 }
6042 p += 4;
6043 return p;
6044 }
6045
6046 // Size of a given plt call stub.
6047
6048 template<int size, bool big_endian>
6049 unsigned int
6050 Stub_table<size, big_endian>::plt_call_size(
6051 typename Plt_stub_entries::iterator p) const
6052 {
6053 if (size == 32)
6054 {
6055 const Symbol* gsym = p->first.sym_;
6056 return (4 * 4
6057 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
6058 }
6059
6060 const Output_data_plt_powerpc<size, big_endian>* plt;
6061 uint64_t plt_addr = this->plt_off(p, &plt);
6062 plt_addr += plt->address();
6063 if (this->targ_->power10_stubs()
6064 && this->targ_->power10_stubs_auto())
6065 {
6066 unsigned int bytes = 0;
6067 if (p->second.notoc_)
6068 {
6069 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6070 bytes = 7 * 4;
6071 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6072 uint64_t odd = from & 4;
6073 uint64_t off = plt_addr - from;
6074 if (off - odd + (1ULL << 33) < 1ULL << 34)
6075 bytes += odd + 4 * 4;
6076 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6077 bytes += 7 * 4;
6078 else
6079 bytes += 8 * 4;
6080 bytes = this->plt_call_align(bytes);
6081 }
6082 unsigned int tail = 0;
6083 if (p->second.toc_)
6084 {
6085 p->second.tocoff_ = bytes;
6086 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6087 {
6088 bytes += 7 * 4;
6089 if (p->second.r2save_ && !p->second.localentry0_)
6090 {
6091 bytes += 2 * 4;
6092 tail = 4 * 4;
6093 }
6094 }
6095 if (p->second.r2save_)
6096 bytes += 4;
6097 uint64_t got_addr
6098 = this->targ_->got_section()->output_section()->address();
6099 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6100 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6101 got_addr += ppcobj->toc_base_offset();
6102 uint64_t off = plt_addr - got_addr;
6103 bytes += 3 * 4 + 4 * (ha(off) != 0);
6104 }
6105 return bytes + tail;
6106 }
6107 else
6108 {
6109 unsigned int bytes = 0;
6110 unsigned int tail = 0;
6111 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6112 {
6113 bytes = 7 * 4;
6114 if (p->second.r2save_ && !p->second.localentry0_)
6115 {
6116 bytes = 9 * 4;
6117 tail = 4 * 4;
6118 }
6119 }
6120
6121 if (p->second.r2save_)
6122 bytes += 4;
6123
6124 if (this->targ_->power10_stubs())
6125 {
6126 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6127 uint64_t odd = from & 4;
6128 uint64_t off = plt_addr - from;
6129 if (off - odd + (1ULL << 33) < 1ULL << 34)
6130 bytes += odd + 4 * 4;
6131 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6132 bytes += 7 * 4;
6133 else
6134 bytes += 8 * 4;
6135 return bytes + tail;
6136 }
6137
6138 if (p->second.notoc_)
6139 {
6140 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6141 uint64_t off = plt_addr - from;
6142 if (off + 0x8000 < 0x10000)
6143 bytes += 7 * 4;
6144 else if (off + 0x80008000ULL < 0x100000000ULL)
6145 bytes += 8 * 4;
6146 else
6147 {
6148 bytes += 8 * 4;
6149 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6150 && ((off >> 32) & 0xffff) != 0)
6151 bytes += 4;
6152 if (((off >> 32) & 0xffffffffULL) != 0)
6153 bytes += 4;
6154 if (hi(off) != 0)
6155 bytes += 4;
6156 if (l(off) != 0)
6157 bytes += 4;
6158 }
6159 return bytes + tail;
6160 }
6161
6162 uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6163 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6164 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6165 got_addr += ppcobj->toc_base_offset();
6166 uint64_t off = plt_addr - got_addr;
6167 bytes += 3 * 4 + 4 * (ha(off) != 0);
6168 if (this->targ_->abiversion() < 2)
6169 {
6170 bool static_chain = parameters->options().plt_static_chain();
6171 bool thread_safe = this->targ_->plt_thread_safe();
6172 bytes += (4
6173 + 4 * static_chain
6174 + 8 * thread_safe
6175 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6176 }
6177 return bytes + tail;
6178 }
6179 }
6180
6181 // Return long branch stub size.
6182
6183 template<int size, bool big_endian>
6184 unsigned int
6185 Stub_table<size, big_endian>::branch_stub_size(
6186 typename Branch_stub_entries::iterator p,
6187 bool* need_lt)
6188 {
6189 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6190 if (size == 32)
6191 {
6192 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6193 return 4;
6194 if (parameters->options().output_is_position_independent())
6195 return 32;
6196 return 16;
6197 }
6198
6199 uint64_t off = p->first.dest_ - loc;
6200 unsigned int bytes = 0;
6201 if (p->second.notoc_)
6202 {
6203 if (this->targ_->power10_stubs())
6204 {
6205 Address odd = loc & 4;
6206 if (off + (1 << 25) < 2 << 25)
6207 bytes = odd + 12;
6208 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6209 bytes = odd + 16;
6210 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6211 bytes = 28;
6212 else
6213 bytes = 32;
6214 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6215 return bytes;
6216 p->second.tocoff_ = bytes;
6217 }
6218 else
6219 {
6220 off -= 8;
6221 if (off + 0x8000 < 0x10000)
6222 return 24;
6223 if (off + 0x80008000ULL < 0x100000000ULL)
6224 {
6225 if (off + 24 + (1 << 25) < 2 << 25)
6226 return 28;
6227 return 32;
6228 }
6229
6230 bytes = 32;
6231 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6232 && ((off >> 32) & 0xffff) != 0)
6233 bytes += 4;
6234 if (((off >> 32) & 0xffffffffULL) != 0)
6235 bytes += 4;
6236 if (hi(off) != 0)
6237 bytes += 4;
6238 if (l(off) != 0)
6239 bytes += 4;
6240 return bytes;
6241 }
6242 }
6243
6244 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6245 if (off + (1 << 25) < 2 << 25)
6246 return bytes + 4;
6247 if (!this->targ_->power10_stubs()
6248 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6249 *need_lt = true;
6250 return bytes + 16;
6251 }
6252
6253 template<int size, bool big_endian>
6254 void
6255 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6256 {
6257 if (p.sym_)
6258 gold_error(_("linkage table error against `%s'"),
6259 p.sym_->demangled_name().c_str());
6260 else
6261 gold_error(_("linkage table error against `%s:[local %u]'"),
6262 p.object_->name().c_str(),
6263 p.locsym_);
6264 }
6265
6266 // Write out plt and long branch stub code.
6267
6268 template<int size, bool big_endian>
6269 void
6270 Stub_table<size, big_endian>::do_write(Output_file* of)
6271 {
6272 if (this->plt_call_stubs_.empty()
6273 && this->long_branch_stubs_.empty())
6274 return;
6275
6276 const section_size_type start_off = this->offset();
6277 const section_size_type off = this->stub_offset();
6278 const section_size_type oview_size =
6279 convert_to_section_size_type(this->data_size() - (off - start_off));
6280 unsigned char* const oview = of->get_output_view(off, oview_size);
6281 unsigned char* p;
6282
6283 if (size == 64
6284 && this->targ_->power10_stubs())
6285 {
6286 const Output_data_got_powerpc<size, big_endian>* got
6287 = this->targ_->got_section();
6288 Address got_os_addr = got->output_section()->address();
6289
6290 if (!this->plt_call_stubs_.empty())
6291 {
6292 // Write out plt call stubs.
6293 typename Plt_stub_entries::const_iterator cs;
6294 for (cs = this->plt_call_stubs_.begin();
6295 cs != this->plt_call_stubs_.end();
6296 ++cs)
6297 {
6298 p = oview + cs->second.off_;
6299 const Output_data_plt_powerpc<size, big_endian>* plt;
6300 Address pltoff = this->plt_off(cs, &plt);
6301 Address plt_addr = pltoff + plt->address();
6302 if (this->targ_->power10_stubs_auto())
6303 {
6304 if (cs->second.notoc_)
6305 {
6306 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6307 this->build_tls_opt_head(&p, false);
6308 Address from = this->stub_address() + (p - oview);
6309 Address delta = plt_addr - from;
6310 p = build_power10_offset<big_endian>(p, delta, from & 4,
6311 true);
6312 write_insn<big_endian>(p, mtctr_12);
6313 p += 4;
6314 write_insn<big_endian>(p, bctr);
6315 p += 4;
6316 p = oview + this->plt_call_align(p - oview);
6317 }
6318 if (cs->second.toc_)
6319 {
6320 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6321 {
6322 bool save_lr
6323 = cs->second.r2save_ && !cs->second.localentry0_;
6324 this->build_tls_opt_head(&p, save_lr);
6325 }
6326 const Powerpc_relobj<size, big_endian>* ppcobj
6327 = static_cast<const Powerpc_relobj<size, big_endian>*>(
6328 cs->first.object_);
6329 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6330 Address off = plt_addr - got_addr;
6331
6332 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6333 this->plt_error(cs->first);
6334
6335 if (cs->second.r2save_)
6336 {
6337 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6338 p += 4;
6339 }
6340 if (ha(off) != 0)
6341 {
6342 write_insn<big_endian>(p, addis_12_2 + ha(off));
6343 p += 4;
6344 write_insn<big_endian>(p, ld_12_12 + l(off));
6345 p += 4;
6346 }
6347 else
6348 {
6349 write_insn<big_endian>(p, ld_12_2 + l(off));
6350 p += 4;
6351 }
6352 write_insn<big_endian>(p, mtctr_12);
6353 p += 4;
6354 if (cs->second.r2save_
6355 && !cs->second.localentry0_
6356 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6357 this->build_tls_opt_tail(p);
6358 else
6359 write_insn<big_endian>(p, bctr);
6360 }
6361 }
6362 else
6363 {
6364 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6365 {
6366 bool save_lr
6367 = cs->second.r2save_ && !cs->second.localentry0_;
6368 this->build_tls_opt_head(&p, save_lr);
6369 }
6370 if (cs->second.r2save_)
6371 {
6372 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6373 p += 4;
6374 }
6375 Address from = this->stub_address() + (p - oview);
6376 Address delta = plt_addr - from;
6377 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6378 write_insn<big_endian>(p, mtctr_12);
6379 p += 4;
6380 if (cs->second.r2save_
6381 && !cs->second.localentry0_
6382 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6383 this->build_tls_opt_tail(p);
6384 else
6385 write_insn<big_endian>(p, bctr);
6386 }
6387 }
6388 }
6389
6390 // Write out long branch stubs.
6391 typename Branch_stub_entries::const_iterator bs;
6392 for (bs = this->long_branch_stubs_.begin();
6393 bs != this->long_branch_stubs_.end();
6394 ++bs)
6395 {
6396 if (bs->second.save_res_)
6397 continue;
6398 Address off = this->plt_size_ + bs->second.off_;
6399 p = oview + off;
6400 Address loc = this->stub_address() + off;
6401 Address delta = bs->first.dest_ - loc;
6402 if (this->targ_->power10_stubs_auto())
6403 {
6404 if (bs->second.notoc_)
6405 {
6406 unsigned char* startp = p;
6407 p = build_power10_offset<big_endian>(p, delta,
6408 loc & 4, false);
6409 delta -= p - startp;
6410 startp = p;
6411 if (delta + (1 << 25) < 2 << 25)
6412 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6413 else
6414 {
6415 write_insn<big_endian>(p, mtctr_12);
6416 p += 4;
6417 write_insn<big_endian>(p, bctr);
6418 }
6419 p += 4;
6420 delta -= p - startp;
6421 }
6422 if (bs->second.toc_)
6423 {
6424 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6425 if (delta + (1 << 25) >= 2 << 25)
6426 {
6427 Address brlt_addr
6428 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6429 gold_assert(brlt_addr != invalid_address);
6430 brlt_addr += this->targ_->brlt_section()->address();
6431 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6432 Address brltoff = brlt_addr - got_addr;
6433 if (ha(brltoff) == 0)
6434 {
6435 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6436 p += 4;
6437 }
6438 else
6439 {
6440 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6441 p += 4;
6442 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6443 p += 4;
6444 }
6445 }
6446 if (delta + (1 << 25) < 2 << 25)
6447 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6448 else
6449 {
6450 write_insn<big_endian>(p, mtctr_12);
6451 p += 4;
6452 write_insn<big_endian>(p, bctr);
6453 }
6454 }
6455 }
6456 else
6457 {
6458 if (!bs->second.notoc_)
6459 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6460 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6461 {
6462 unsigned char* startp = p;
6463 p = build_power10_offset<big_endian>(p, delta,
6464 loc & 4, false);
6465 delta -= p - startp;
6466 }
6467 if (delta + (1 << 25) < 2 << 25)
6468 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6469 else
6470 {
6471 write_insn<big_endian>(p, mtctr_12);
6472 p += 4;
6473 write_insn<big_endian>(p, bctr);
6474 }
6475 }
6476 }
6477 }
6478 else if (size == 64)
6479 {
6480 const Output_data_got_powerpc<size, big_endian>* got
6481 = this->targ_->got_section();
6482 Address got_os_addr = got->output_section()->address();
6483
6484 if (!this->plt_call_stubs_.empty()
6485 && this->targ_->abiversion() >= 2)
6486 {
6487 // Write out plt call stubs for ELFv2.
6488 typename Plt_stub_entries::const_iterator cs;
6489 for (cs = this->plt_call_stubs_.begin();
6490 cs != this->plt_call_stubs_.end();
6491 ++cs)
6492 {
6493 const Output_data_plt_powerpc<size, big_endian>* plt;
6494 Address pltoff = this->plt_off(cs, &plt);
6495 Address plt_addr = pltoff + plt->address();
6496
6497 p = oview + cs->second.off_;
6498 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6499 {
6500 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6501 this->build_tls_opt_head(&p, save_lr);
6502 }
6503 if (cs->second.r2save_)
6504 {
6505 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6506 p += 4;
6507 }
6508 if (cs->second.notoc_)
6509 {
6510 Address from = this->stub_address() + (p - oview) + 8;
6511 Address off = plt_addr - from;
6512 p = build_notoc_offset<big_endian>(p, off, true);
6513 }
6514 else
6515 {
6516 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6517 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6518 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6519 Address off = plt_addr - got_addr;
6520
6521 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6522 this->plt_error(cs->first);
6523
6524 if (ha(off) != 0)
6525 {
6526 write_insn<big_endian>(p, addis_12_2 + ha(off));
6527 p += 4;
6528 write_insn<big_endian>(p, ld_12_12 + l(off));
6529 p += 4;
6530 }
6531 else
6532 {
6533 write_insn<big_endian>(p, ld_12_2 + l(off));
6534 p += 4;
6535 }
6536 }
6537 write_insn<big_endian>(p, mtctr_12);
6538 p += 4;
6539 if (cs->second.r2save_
6540 && !cs->second.localentry0_
6541 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6542 this->build_tls_opt_tail(p);
6543 else
6544 write_insn<big_endian>(p, bctr);
6545 }
6546 }
6547 else if (!this->plt_call_stubs_.empty())
6548 {
6549 // Write out plt call stubs for ELFv1.
6550 typename Plt_stub_entries::const_iterator cs;
6551 for (cs = this->plt_call_stubs_.begin();
6552 cs != this->plt_call_stubs_.end();
6553 ++cs)
6554 {
6555 const Output_data_plt_powerpc<size, big_endian>* plt;
6556 Address pltoff = this->plt_off(cs, &plt);
6557 Address plt_addr = pltoff + plt->address();
6558 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6559 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6560 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6561 Address off = plt_addr - got_addr;
6562
6563 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6564 || cs->second.notoc_)
6565 this->plt_error(cs->first);
6566
6567 bool static_chain = parameters->options().plt_static_chain();
6568 bool thread_safe = this->targ_->plt_thread_safe();
6569 bool use_fake_dep = false;
6570 Address cmp_branch_off = 0;
6571 if (thread_safe)
6572 {
6573 unsigned int pltindex
6574 = ((pltoff - this->targ_->first_plt_entry_offset())
6575 / this->targ_->plt_entry_size());
6576 Address glinkoff
6577 = (this->targ_->glink_section()->pltresolve_size()
6578 + pltindex * 8);
6579 if (pltindex > 32768)
6580 glinkoff += (pltindex - 32768) * 4;
6581 Address to
6582 = this->targ_->glink_section()->address() + glinkoff;
6583 Address from
6584 = (this->stub_address() + cs->second.off_ + 20
6585 + 4 * cs->second.r2save_
6586 + 4 * (ha(off) != 0)
6587 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6588 + 4 * static_chain);
6589 cmp_branch_off = to - from;
6590 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6591 }
6592
6593 p = oview + cs->second.off_;
6594 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6595 {
6596 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6597 this->build_tls_opt_head(&p, save_lr);
6598 use_fake_dep = thread_safe;
6599 }
6600 if (cs->second.r2save_)
6601 {
6602 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6603 p += 4;
6604 }
6605 if (ha(off) != 0)
6606 {
6607 write_insn<big_endian>(p, addis_11_2 + ha(off));
6608 p += 4;
6609 write_insn<big_endian>(p, ld_12_11 + l(off));
6610 p += 4;
6611 if (ha(off + 8 + 8 * static_chain) != ha(off))
6612 {
6613 write_insn<big_endian>(p, addi_11_11 + l(off));
6614 p += 4;
6615 off = 0;
6616 }
6617 write_insn<big_endian>(p, mtctr_12);
6618 p += 4;
6619 if (use_fake_dep)
6620 {
6621 write_insn<big_endian>(p, xor_2_12_12);
6622 p += 4;
6623 write_insn<big_endian>(p, add_11_11_2);
6624 p += 4;
6625 }
6626 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6627 p += 4;
6628 if (static_chain)
6629 {
6630 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6631 p += 4;
6632 }
6633 }
6634 else
6635 {
6636 write_insn<big_endian>(p, ld_12_2 + l(off));
6637 p += 4;
6638 if (ha(off + 8 + 8 * static_chain) != ha(off))
6639 {
6640 write_insn<big_endian>(p, addi_2_2 + l(off));
6641 p += 4;
6642 off = 0;
6643 }
6644 write_insn<big_endian>(p, mtctr_12);
6645 p += 4;
6646 if (use_fake_dep)
6647 {
6648 write_insn<big_endian>(p, xor_11_12_12);
6649 p += 4;
6650 write_insn<big_endian>(p, add_2_2_11);
6651 p += 4;
6652 }
6653 if (static_chain)
6654 {
6655 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6656 p += 4;
6657 }
6658 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6659 p += 4;
6660 }
6661 if (cs->second.r2save_
6662 && !cs->second.localentry0_
6663 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6664 this->build_tls_opt_tail(p);
6665 else if (thread_safe && !use_fake_dep)
6666 {
6667 write_insn<big_endian>(p, cmpldi_2_0);
6668 p += 4;
6669 write_insn<big_endian>(p, bnectr_p4);
6670 p += 4;
6671 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6672 }
6673 else
6674 write_insn<big_endian>(p, bctr);
6675 }
6676 }
6677
6678 // Write out long branch stubs.
6679 typename Branch_stub_entries::const_iterator bs;
6680 for (bs = this->long_branch_stubs_.begin();
6681 bs != this->long_branch_stubs_.end();
6682 ++bs)
6683 {
6684 if (bs->second.save_res_)
6685 continue;
6686 Address off = this->plt_size_ + bs->second.off_;
6687 p = oview + off;
6688 Address loc = this->stub_address() + off;
6689 Address delta = bs->first.dest_ - loc;
6690 if (!bs->second.notoc_)
6691 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6692 if (bs->second.notoc_)
6693 {
6694 unsigned char* startp = p;
6695 p = build_notoc_offset<big_endian>(p, off, false);
6696 delta -= p - startp;
6697 }
6698 else if (delta + (1 << 25) >= 2 << 25)
6699 {
6700 Address brlt_addr
6701 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6702 gold_assert(brlt_addr != invalid_address);
6703 brlt_addr += this->targ_->brlt_section()->address();
6704 Address got_addr = got_os_addr + bs->first.toc_base_off_;
6705 Address brltoff = brlt_addr - got_addr;
6706 if (ha(brltoff) == 0)
6707 {
6708 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6709 p += 4;
6710 }
6711 else
6712 {
6713 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6714 p += 4;
6715 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6716 p += 4;
6717 }
6718 }
6719 if (delta + (1 << 25) < 2 << 25)
6720 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6721 else
6722 {
6723 write_insn<big_endian>(p, mtctr_12);
6724 p += 4;
6725 write_insn<big_endian>(p, bctr);
6726 }
6727 }
6728 }
6729 else // size == 32
6730 {
6731 if (!this->plt_call_stubs_.empty())
6732 {
6733 // The address of _GLOBAL_OFFSET_TABLE_.
6734 Address g_o_t = invalid_address;
6735
6736 // Write out plt call stubs.
6737 typename Plt_stub_entries::const_iterator cs;
6738 for (cs = this->plt_call_stubs_.begin();
6739 cs != this->plt_call_stubs_.end();
6740 ++cs)
6741 {
6742 const Output_data_plt_powerpc<size, big_endian>* plt;
6743 Address plt_addr = this->plt_off(cs, &plt);
6744 plt_addr += plt->address();
6745
6746 p = oview + cs->second.off_;
6747 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6748 this->build_tls_opt_head(&p, false);
6749 if (parameters->options().output_is_position_independent())
6750 {
6751 Address got_addr;
6752 const Powerpc_relobj<size, big_endian>* ppcobj
6753 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6754 (cs->first.object_));
6755 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6756 {
6757 unsigned int got2 = ppcobj->got2_shndx();
6758 got_addr = ppcobj->get_output_section_offset(got2);
6759 gold_assert(got_addr != invalid_address);
6760 got_addr += (ppcobj->output_section(got2)->address()
6761 + cs->first.addend_);
6762 }
6763 else
6764 {
6765 if (g_o_t == invalid_address)
6766 {
6767 const Output_data_got_powerpc<size, big_endian>* got
6768 = this->targ_->got_section();
6769 g_o_t = got->address() + got->g_o_t();
6770 }
6771 got_addr = g_o_t;
6772 }
6773
6774 Address off = plt_addr - got_addr;
6775 if (ha(off) == 0)
6776 write_insn<big_endian>(p, lwz_11_30 + l(off));
6777 else
6778 {
6779 write_insn<big_endian>(p, addis_11_30 + ha(off));
6780 p += 4;
6781 write_insn<big_endian>(p, lwz_11_11 + l(off));
6782 }
6783 }
6784 else
6785 {
6786 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6787 p += 4;
6788 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6789 }
6790 p += 4;
6791 write_insn<big_endian>(p, mtctr_11);
6792 p += 4;
6793 write_insn<big_endian>(p, bctr);
6794 }
6795 }
6796
6797 // Write out long branch stubs.
6798 typename Branch_stub_entries::const_iterator bs;
6799 for (bs = this->long_branch_stubs_.begin();
6800 bs != this->long_branch_stubs_.end();
6801 ++bs)
6802 {
6803 if (bs->second.save_res_)
6804 continue;
6805 Address off = this->plt_size_ + bs->second.off_;
6806 p = oview + off;
6807 Address loc = this->stub_address() + off;
6808 Address delta = bs->first.dest_ - loc;
6809 if (delta + (1 << 25) < 2 << 25)
6810 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6811 else if (!parameters->options().output_is_position_independent())
6812 {
6813 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6814 p += 4;
6815 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6816 }
6817 else
6818 {
6819 delta -= 8;
6820 write_insn<big_endian>(p, mflr_0);
6821 p += 4;
6822 write_insn<big_endian>(p, bcl_20_31);
6823 p += 4;
6824 write_insn<big_endian>(p, mflr_12);
6825 p += 4;
6826 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6827 p += 4;
6828 write_insn<big_endian>(p, addi_12_12 + l(delta));
6829 p += 4;
6830 write_insn<big_endian>(p, mtlr_0);
6831 }
6832 p += 4;
6833 write_insn<big_endian>(p, mtctr_12);
6834 p += 4;
6835 write_insn<big_endian>(p, bctr);
6836 }
6837 }
6838 if (this->need_save_res_)
6839 {
6840 p = oview + this->plt_size_ + this->branch_size_;
6841 memcpy (p, this->targ_->savres_section()->contents(),
6842 this->targ_->savres_section()->data_size());
6843 }
6844 }
6845
6846 // Write out .glink.
6847
6848 template<int size, bool big_endian>
6849 void
6850 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6851 {
6852 const section_size_type off = this->offset();
6853 const section_size_type oview_size =
6854 convert_to_section_size_type(this->data_size());
6855 unsigned char* const oview = of->get_output_view(off, oview_size);
6856 unsigned char* p;
6857
6858 // The base address of the .plt section.
6859 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6860 Address plt_base = this->targ_->plt_section()->address();
6861
6862 if (size == 64)
6863 {
6864 if (this->end_branch_table_ != 0)
6865 {
6866 // Write pltresolve stub.
6867 p = oview;
6868 Address after_bcl = this->address() + 16;
6869 Address pltoff = plt_base - after_bcl;
6870
6871 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
6872
6873 if (this->targ_->abiversion() < 2)
6874 {
6875 write_insn<big_endian>(p, mflr_12), p += 4;
6876 write_insn<big_endian>(p, bcl_20_31), p += 4;
6877 write_insn<big_endian>(p, mflr_11), p += 4;
6878 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6879 write_insn<big_endian>(p, mtlr_12), p += 4;
6880 write_insn<big_endian>(p, add_11_2_11), p += 4;
6881 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6882 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6883 write_insn<big_endian>(p, mtctr_12), p += 4;
6884 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6885 }
6886 else
6887 {
6888 if (this->targ_->has_localentry0())
6889 {
6890 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6891 }
6892 write_insn<big_endian>(p, mflr_0), p += 4;
6893 write_insn<big_endian>(p, bcl_20_31), p += 4;
6894 write_insn<big_endian>(p, mflr_11), p += 4;
6895 write_insn<big_endian>(p, mtlr_0), p += 4;
6896 if (this->targ_->has_localentry0())
6897 {
6898 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
6899 }
6900 else
6901 {
6902 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
6903 }
6904 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6905 write_insn<big_endian>(p, add_11_0_11), p += 4;
6906 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
6907 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6908 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6909 write_insn<big_endian>(p, mtctr_12), p += 4;
6910 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6911 }
6912 write_insn<big_endian>(p, bctr), p += 4;
6913 gold_assert(p == oview + this->pltresolve_size());
6914
6915 // Write lazy link call stubs.
6916 uint32_t indx = 0;
6917 while (p < oview + this->end_branch_table_)
6918 {
6919 if (this->targ_->abiversion() < 2)
6920 {
6921 if (indx < 0x8000)
6922 {
6923 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6924 }
6925 else
6926 {
6927 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
6928 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6929 }
6930 }
6931 uint32_t branch_off = 8 - (p - oview);
6932 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6933 indx++;
6934 }
6935 }
6936
6937 Address plt_base = this->targ_->plt_section()->address();
6938 Address iplt_base = invalid_address;
6939 unsigned int global_entry_off = this->global_entry_off();
6940 Address global_entry_base = this->address() + global_entry_off;
6941 typename Global_entry_stub_entries::const_iterator ge;
6942 for (ge = this->global_entry_stubs_.begin();
6943 ge != this->global_entry_stubs_.end();
6944 ++ge)
6945 {
6946 p = oview + global_entry_off + ge->second;
6947 Address plt_addr = ge->first->plt_offset();
6948 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6949 && ge->first->can_use_relative_reloc(false))
6950 {
6951 if (iplt_base == invalid_address)
6952 iplt_base = this->targ_->iplt_section()->address();
6953 plt_addr += iplt_base;
6954 }
6955 else
6956 plt_addr += plt_base;
6957 Address my_addr = global_entry_base + ge->second;
6958 Address off = plt_addr - my_addr;
6959
6960 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6961 gold_error(_("linkage table error against `%s'"),
6962 ge->first->demangled_name().c_str());
6963
6964 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6965 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6966 write_insn<big_endian>(p, mtctr_12), p += 4;
6967 write_insn<big_endian>(p, bctr);
6968 }
6969 }
6970 else
6971 {
6972 const Output_data_got_powerpc<size, big_endian>* got
6973 = this->targ_->got_section();
6974 // The address of _GLOBAL_OFFSET_TABLE_.
6975 Address g_o_t = got->address() + got->g_o_t();
6976
6977 // Write out pltresolve branch table.
6978 p = oview;
6979 unsigned int the_end = oview_size - this->pltresolve_size();
6980 unsigned char* end_p = oview + the_end;
6981 while (p < end_p - 8 * 4)
6982 write_insn<big_endian>(p, b + end_p - p), p += 4;
6983 while (p < end_p)
6984 write_insn<big_endian>(p, nop), p += 4;
6985
6986 // Write out pltresolve call stub.
6987 end_p = oview + oview_size;
6988 if (parameters->options().output_is_position_independent())
6989 {
6990 Address res0_off = 0;
6991 Address after_bcl_off = the_end + 12;
6992 Address bcl_res0 = after_bcl_off - res0_off;
6993
6994 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6995 p += 4;
6996 write_insn<big_endian>(p, mflr_0);
6997 p += 4;
6998 write_insn<big_endian>(p, bcl_20_31);
6999 p += 4;
7000 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7001 p += 4;
7002 write_insn<big_endian>(p, mflr_12);
7003 p += 4;
7004 write_insn<big_endian>(p, mtlr_0);
7005 p += 4;
7006 write_insn<big_endian>(p, sub_11_11_12);
7007 p += 4;
7008
7009 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
7010
7011 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7012 p += 4;
7013 if (ha(got_bcl) == ha(got_bcl + 4))
7014 {
7015 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7016 p += 4;
7017 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
7018 }
7019 else
7020 {
7021 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7022 p += 4;
7023 write_insn<big_endian>(p, lwz_12_12 + 4);
7024 }
7025 p += 4;
7026 write_insn<big_endian>(p, mtctr_0);
7027 p += 4;
7028 write_insn<big_endian>(p, add_0_11_11);
7029 p += 4;
7030 write_insn<big_endian>(p, add_11_0_11);
7031 }
7032 else
7033 {
7034 Address res0 = this->address();
7035
7036 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7037 p += 4;
7038 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7039 p += 4;
7040 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7041 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7042 else
7043 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7044 p += 4;
7045 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7046 p += 4;
7047 write_insn<big_endian>(p, mtctr_0);
7048 p += 4;
7049 write_insn<big_endian>(p, add_0_11_11);
7050 p += 4;
7051 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7052 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7053 else
7054 write_insn<big_endian>(p, lwz_12_12 + 4);
7055 p += 4;
7056 write_insn<big_endian>(p, add_11_0_11);
7057 }
7058 p += 4;
7059 write_insn<big_endian>(p, bctr);
7060 p += 4;
7061 while (p < end_p)
7062 {
7063 write_insn<big_endian>(p, nop);
7064 p += 4;
7065 }
7066 }
7067
7068 of->write_output_view(off, oview_size, oview);
7069 }
7070
7071
7072 // A class to handle linker generated save/restore functions.
7073
7074 template<int size, bool big_endian>
7075 class Output_data_save_res : public Output_section_data_build
7076 {
7077 public:
7078 Output_data_save_res(Symbol_table* symtab);
7079
7080 const unsigned char*
7081 contents() const
7082 {
7083 return contents_;
7084 }
7085
7086 protected:
7087 // Write to a map file.
7088 void
7089 do_print_to_mapfile(Mapfile* mapfile) const
7090 { mapfile->print_output_data(this, _("** save/restore")); }
7091
7092 void
7093 do_write(Output_file*);
7094
7095 private:
7096 // The maximum size of save/restore contents.
7097 static const unsigned int savres_max = 218*4;
7098
7099 void
7100 savres_define(Symbol_table* symtab,
7101 const char *name,
7102 unsigned int lo, unsigned int hi,
7103 unsigned char* write_ent(unsigned char*, int),
7104 unsigned char* write_tail(unsigned char*, int));
7105
7106 unsigned char *contents_;
7107 };
7108
7109 template<bool big_endian>
7110 static unsigned char*
7111 savegpr0(unsigned char* p, int r)
7112 {
7113 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7114 write_insn<big_endian>(p, insn);
7115 return p + 4;
7116 }
7117
7118 template<bool big_endian>
7119 static unsigned char*
7120 savegpr0_tail(unsigned char* p, int r)
7121 {
7122 p = savegpr0<big_endian>(p, r);
7123 uint32_t insn = std_0_1 + 16;
7124 write_insn<big_endian>(p, insn);
7125 p = p + 4;
7126 write_insn<big_endian>(p, blr);
7127 return p + 4;
7128 }
7129
7130 template<bool big_endian>
7131 static unsigned char*
7132 restgpr0(unsigned char* p, int r)
7133 {
7134 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7135 write_insn<big_endian>(p, insn);
7136 return p + 4;
7137 }
7138
7139 template<bool big_endian>
7140 static unsigned char*
7141 restgpr0_tail(unsigned char* p, int r)
7142 {
7143 uint32_t insn = ld_0_1 + 16;
7144 write_insn<big_endian>(p, insn);
7145 p = p + 4;
7146 p = restgpr0<big_endian>(p, r);
7147 write_insn<big_endian>(p, mtlr_0);
7148 p = p + 4;
7149 if (r == 29)
7150 {
7151 p = restgpr0<big_endian>(p, 30);
7152 p = restgpr0<big_endian>(p, 31);
7153 }
7154 write_insn<big_endian>(p, blr);
7155 return p + 4;
7156 }
7157
7158 template<bool big_endian>
7159 static unsigned char*
7160 savegpr1(unsigned char* p, int r)
7161 {
7162 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7163 write_insn<big_endian>(p, insn);
7164 return p + 4;
7165 }
7166
7167 template<bool big_endian>
7168 static unsigned char*
7169 savegpr1_tail(unsigned char* p, int r)
7170 {
7171 p = savegpr1<big_endian>(p, r);
7172 write_insn<big_endian>(p, blr);
7173 return p + 4;
7174 }
7175
7176 template<bool big_endian>
7177 static unsigned char*
7178 restgpr1(unsigned char* p, int r)
7179 {
7180 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7181 write_insn<big_endian>(p, insn);
7182 return p + 4;
7183 }
7184
7185 template<bool big_endian>
7186 static unsigned char*
7187 restgpr1_tail(unsigned char* p, int r)
7188 {
7189 p = restgpr1<big_endian>(p, r);
7190 write_insn<big_endian>(p, blr);
7191 return p + 4;
7192 }
7193
7194 template<bool big_endian>
7195 static unsigned char*
7196 savefpr(unsigned char* p, int r)
7197 {
7198 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7199 write_insn<big_endian>(p, insn);
7200 return p + 4;
7201 }
7202
7203 template<bool big_endian>
7204 static unsigned char*
7205 savefpr0_tail(unsigned char* p, int r)
7206 {
7207 p = savefpr<big_endian>(p, r);
7208 write_insn<big_endian>(p, std_0_1 + 16);
7209 p = p + 4;
7210 write_insn<big_endian>(p, blr);
7211 return p + 4;
7212 }
7213
7214 template<bool big_endian>
7215 static unsigned char*
7216 restfpr(unsigned char* p, int r)
7217 {
7218 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7219 write_insn<big_endian>(p, insn);
7220 return p + 4;
7221 }
7222
7223 template<bool big_endian>
7224 static unsigned char*
7225 restfpr0_tail(unsigned char* p, int r)
7226 {
7227 write_insn<big_endian>(p, ld_0_1 + 16);
7228 p = p + 4;
7229 p = restfpr<big_endian>(p, r);
7230 write_insn<big_endian>(p, mtlr_0);
7231 p = p + 4;
7232 if (r == 29)
7233 {
7234 p = restfpr<big_endian>(p, 30);
7235 p = restfpr<big_endian>(p, 31);
7236 }
7237 write_insn<big_endian>(p, blr);
7238 return p + 4;
7239 }
7240
7241 template<bool big_endian>
7242 static unsigned char*
7243 savefpr1_tail(unsigned char* p, int r)
7244 {
7245 p = savefpr<big_endian>(p, r);
7246 write_insn<big_endian>(p, blr);
7247 return p + 4;
7248 }
7249
7250 template<bool big_endian>
7251 static unsigned char*
7252 restfpr1_tail(unsigned char* p, int r)
7253 {
7254 p = restfpr<big_endian>(p, r);
7255 write_insn<big_endian>(p, blr);
7256 return p + 4;
7257 }
7258
7259 template<bool big_endian>
7260 static unsigned char*
7261 savevr(unsigned char* p, int r)
7262 {
7263 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7264 write_insn<big_endian>(p, insn);
7265 p = p + 4;
7266 insn = stvx_0_12_0 + (r << 21);
7267 write_insn<big_endian>(p, insn);
7268 return p + 4;
7269 }
7270
7271 template<bool big_endian>
7272 static unsigned char*
7273 savevr_tail(unsigned char* p, int r)
7274 {
7275 p = savevr<big_endian>(p, r);
7276 write_insn<big_endian>(p, blr);
7277 return p + 4;
7278 }
7279
7280 template<bool big_endian>
7281 static unsigned char*
7282 restvr(unsigned char* p, int r)
7283 {
7284 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7285 write_insn<big_endian>(p, insn);
7286 p = p + 4;
7287 insn = lvx_0_12_0 + (r << 21);
7288 write_insn<big_endian>(p, insn);
7289 return p + 4;
7290 }
7291
7292 template<bool big_endian>
7293 static unsigned char*
7294 restvr_tail(unsigned char* p, int r)
7295 {
7296 p = restvr<big_endian>(p, r);
7297 write_insn<big_endian>(p, blr);
7298 return p + 4;
7299 }
7300
7301
7302 template<int size, bool big_endian>
7303 Output_data_save_res<size, big_endian>::Output_data_save_res(
7304 Symbol_table* symtab)
7305 : Output_section_data_build(4),
7306 contents_(NULL)
7307 {
7308 this->savres_define(symtab,
7309 "_savegpr0_", 14, 31,
7310 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7311 this->savres_define(symtab,
7312 "_restgpr0_", 14, 29,
7313 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7314 this->savres_define(symtab,
7315 "_restgpr0_", 30, 31,
7316 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7317 this->savres_define(symtab,
7318 "_savegpr1_", 14, 31,
7319 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7320 this->savres_define(symtab,
7321 "_restgpr1_", 14, 31,
7322 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7323 this->savres_define(symtab,
7324 "_savefpr_", 14, 31,
7325 savefpr<big_endian>, savefpr0_tail<big_endian>);
7326 this->savres_define(symtab,
7327 "_restfpr_", 14, 29,
7328 restfpr<big_endian>, restfpr0_tail<big_endian>);
7329 this->savres_define(symtab,
7330 "_restfpr_", 30, 31,
7331 restfpr<big_endian>, restfpr0_tail<big_endian>);
7332 this->savres_define(symtab,
7333 "._savef", 14, 31,
7334 savefpr<big_endian>, savefpr1_tail<big_endian>);
7335 this->savres_define(symtab,
7336 "._restf", 14, 31,
7337 restfpr<big_endian>, restfpr1_tail<big_endian>);
7338 this->savres_define(symtab,
7339 "_savevr_", 20, 31,
7340 savevr<big_endian>, savevr_tail<big_endian>);
7341 this->savres_define(symtab,
7342 "_restvr_", 20, 31,
7343 restvr<big_endian>, restvr_tail<big_endian>);
7344 }
7345
7346 template<int size, bool big_endian>
7347 void
7348 Output_data_save_res<size, big_endian>::savres_define(
7349 Symbol_table* symtab,
7350 const char *name,
7351 unsigned int lo, unsigned int hi,
7352 unsigned char* write_ent(unsigned char*, int),
7353 unsigned char* write_tail(unsigned char*, int))
7354 {
7355 size_t len = strlen(name);
7356 bool writing = false;
7357 char sym[16];
7358
7359 memcpy(sym, name, len);
7360 sym[len + 2] = 0;
7361
7362 for (unsigned int i = lo; i <= hi; i++)
7363 {
7364 sym[len + 0] = i / 10 + '0';
7365 sym[len + 1] = i % 10 + '0';
7366 Symbol* gsym = symtab->lookup(sym);
7367 bool refd = gsym != NULL && gsym->is_undefined();
7368 writing = writing || refd;
7369 if (writing)
7370 {
7371 if (this->contents_ == NULL)
7372 this->contents_ = new unsigned char[this->savres_max];
7373
7374 section_size_type value = this->current_data_size();
7375 unsigned char* p = this->contents_ + value;
7376 if (i != hi)
7377 p = write_ent(p, i);
7378 else
7379 p = write_tail(p, i);
7380 section_size_type cur_size = p - this->contents_;
7381 this->set_current_data_size(cur_size);
7382 if (refd)
7383 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7384 this, value, cur_size - value,
7385 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7386 elfcpp::STV_HIDDEN, 0, false, false);
7387 }
7388 }
7389 }
7390
7391 // Write out save/restore.
7392
7393 template<int size, bool big_endian>
7394 void
7395 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7396 {
7397 const section_size_type off = this->offset();
7398 const section_size_type oview_size =
7399 convert_to_section_size_type(this->data_size());
7400 unsigned char* const oview = of->get_output_view(off, oview_size);
7401 memcpy(oview, this->contents_, oview_size);
7402 of->write_output_view(off, oview_size, oview);
7403 }
7404
7405
7406 // Create the glink section.
7407
7408 template<int size, bool big_endian>
7409 void
7410 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7411 {
7412 if (this->glink_ == NULL)
7413 {
7414 this->glink_ = new Output_data_glink<size, big_endian>(this);
7415 this->glink_->add_eh_frame(layout);
7416 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7417 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7418 this->glink_, ORDER_TEXT, false);
7419 }
7420 }
7421
7422 // Create a PLT entry for a global symbol.
7423
7424 template<int size, bool big_endian>
7425 void
7426 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7427 Layout* layout,
7428 Symbol* gsym)
7429 {
7430 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7431 && gsym->can_use_relative_reloc(false))
7432 {
7433 if (this->iplt_ == NULL)
7434 this->make_iplt_section(symtab, layout);
7435 this->iplt_->add_ifunc_entry(gsym);
7436 }
7437 else
7438 {
7439 if (this->plt_ == NULL)
7440 this->make_plt_section(symtab, layout);
7441 this->plt_->add_entry(gsym);
7442 }
7443 }
7444
7445 // Make a PLT entry for a local symbol.
7446
7447 template<int size, bool big_endian>
7448 void
7449 Target_powerpc<size, big_endian>::make_local_plt_entry(
7450 Layout* layout,
7451 Sized_relobj_file<size, big_endian>* relobj,
7452 unsigned int r_sym)
7453 {
7454 if (this->lplt_ == NULL)
7455 this->make_lplt_section(layout);
7456 this->lplt_->add_local_entry(relobj, r_sym);
7457 }
7458
7459 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7460
7461 template<int size, bool big_endian>
7462 void
7463 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7464 Symbol_table* symtab,
7465 Layout* layout,
7466 Sized_relobj_file<size, big_endian>* relobj,
7467 unsigned int r_sym)
7468 {
7469 if (this->iplt_ == NULL)
7470 this->make_iplt_section(symtab, layout);
7471 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7472 }
7473
7474 // Return the number of entries in the PLT.
7475
7476 template<int size, bool big_endian>
7477 unsigned int
7478 Target_powerpc<size, big_endian>::plt_entry_count() const
7479 {
7480 if (this->plt_ == NULL)
7481 return 0;
7482 return this->plt_->entry_count();
7483 }
7484
7485 // Create a GOT entry for local dynamic __tls_get_addr calls.
7486
7487 template<int size, bool big_endian>
7488 unsigned int
7489 Target_powerpc<size, big_endian>::tlsld_got_offset(
7490 Symbol_table* symtab,
7491 Layout* layout,
7492 Sized_relobj_file<size, big_endian>* object)
7493 {
7494 if (this->tlsld_got_offset_ == -1U)
7495 {
7496 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7497 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7498 Output_data_got_powerpc<size, big_endian>* got
7499 = this->got_section(symtab, layout);
7500 unsigned int got_offset = got->add_constant_pair(0, 0);
7501 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7502 got_offset, 0);
7503 this->tlsld_got_offset_ = got_offset;
7504 }
7505 return this->tlsld_got_offset_;
7506 }
7507
7508 // Get the Reference_flags for a particular relocation.
7509
7510 template<int size, bool big_endian>
7511 int
7512 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7513 unsigned int r_type,
7514 const Target_powerpc* target)
7515 {
7516 int ref = 0;
7517
7518 switch (r_type)
7519 {
7520 case elfcpp::R_POWERPC_NONE:
7521 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7522 case elfcpp::R_POWERPC_GNU_VTENTRY:
7523 case elfcpp::R_PPC64_TOC:
7524 // No symbol reference.
7525 break;
7526
7527 case elfcpp::R_PPC64_ADDR64:
7528 case elfcpp::R_PPC64_UADDR64:
7529 case elfcpp::R_POWERPC_ADDR32:
7530 case elfcpp::R_POWERPC_UADDR32:
7531 case elfcpp::R_POWERPC_ADDR16:
7532 case elfcpp::R_POWERPC_UADDR16:
7533 case elfcpp::R_POWERPC_ADDR16_LO:
7534 case elfcpp::R_POWERPC_ADDR16_HI:
7535 case elfcpp::R_POWERPC_ADDR16_HA:
7536 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7537 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7538 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7539 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7540 case elfcpp::R_PPC64_D34:
7541 case elfcpp::R_PPC64_D34_LO:
7542 case elfcpp::R_PPC64_D34_HI30:
7543 case elfcpp::R_PPC64_D34_HA30:
7544 case elfcpp::R_PPC64_D28:
7545 ref = Symbol::ABSOLUTE_REF;
7546 break;
7547
7548 case elfcpp::R_POWERPC_ADDR24:
7549 case elfcpp::R_POWERPC_ADDR14:
7550 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7551 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7552 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7553 break;
7554
7555 case elfcpp::R_PPC64_REL64:
7556 case elfcpp::R_POWERPC_REL32:
7557 case elfcpp::R_PPC_LOCAL24PC:
7558 case elfcpp::R_POWERPC_REL16:
7559 case elfcpp::R_POWERPC_REL16_LO:
7560 case elfcpp::R_POWERPC_REL16_HI:
7561 case elfcpp::R_POWERPC_REL16_HA:
7562 case elfcpp::R_PPC64_REL16_HIGH:
7563 case elfcpp::R_PPC64_REL16_HIGHA:
7564 case elfcpp::R_PPC64_REL16_HIGHER:
7565 case elfcpp::R_PPC64_REL16_HIGHERA:
7566 case elfcpp::R_PPC64_REL16_HIGHEST:
7567 case elfcpp::R_PPC64_REL16_HIGHESTA:
7568 case elfcpp::R_PPC64_PCREL34:
7569 case elfcpp::R_PPC64_REL16_HIGHER34:
7570 case elfcpp::R_PPC64_REL16_HIGHERA34:
7571 case elfcpp::R_PPC64_REL16_HIGHEST34:
7572 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7573 case elfcpp::R_PPC64_PCREL28:
7574 ref = Symbol::RELATIVE_REF;
7575 break;
7576
7577 case elfcpp::R_PPC64_REL24_NOTOC:
7578 if (size == 32)
7579 break;
7580 // Fall through.
7581 case elfcpp::R_POWERPC_REL24:
7582 case elfcpp::R_PPC_PLTREL24:
7583 case elfcpp::R_POWERPC_REL14:
7584 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7585 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7586 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7587 break;
7588
7589 case elfcpp::R_POWERPC_GOT16:
7590 case elfcpp::R_POWERPC_GOT16_LO:
7591 case elfcpp::R_POWERPC_GOT16_HI:
7592 case elfcpp::R_POWERPC_GOT16_HA:
7593 case elfcpp::R_PPC64_GOT16_DS:
7594 case elfcpp::R_PPC64_GOT16_LO_DS:
7595 case elfcpp::R_PPC64_GOT_PCREL34:
7596 case elfcpp::R_PPC64_TOC16:
7597 case elfcpp::R_PPC64_TOC16_LO:
7598 case elfcpp::R_PPC64_TOC16_HI:
7599 case elfcpp::R_PPC64_TOC16_HA:
7600 case elfcpp::R_PPC64_TOC16_DS:
7601 case elfcpp::R_PPC64_TOC16_LO_DS:
7602 case elfcpp::R_POWERPC_PLT16_LO:
7603 case elfcpp::R_POWERPC_PLT16_HI:
7604 case elfcpp::R_POWERPC_PLT16_HA:
7605 case elfcpp::R_PPC64_PLT16_LO_DS:
7606 case elfcpp::R_PPC64_PLT_PCREL34:
7607 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7608 ref = Symbol::RELATIVE_REF;
7609 break;
7610
7611 case elfcpp::R_POWERPC_GOT_TPREL16:
7612 case elfcpp::R_POWERPC_TLS:
7613 case elfcpp::R_PPC64_TLSGD:
7614 case elfcpp::R_PPC64_TLSLD:
7615 case elfcpp::R_PPC64_TPREL34:
7616 case elfcpp::R_PPC64_DTPREL34:
7617 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7618 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7619 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7620 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7621 ref = Symbol::TLS_REF;
7622 break;
7623
7624 case elfcpp::R_POWERPC_COPY:
7625 case elfcpp::R_POWERPC_GLOB_DAT:
7626 case elfcpp::R_POWERPC_JMP_SLOT:
7627 case elfcpp::R_POWERPC_RELATIVE:
7628 case elfcpp::R_POWERPC_DTPMOD:
7629 default:
7630 // Not expected. We will give an error later.
7631 break;
7632 }
7633
7634 if (size == 64 && target->abiversion() < 2)
7635 ref |= Symbol::FUNC_DESC_ABI;
7636 return ref;
7637 }
7638
7639 // Report an unsupported relocation against a local symbol.
7640
7641 template<int size, bool big_endian>
7642 void
7643 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7644 Sized_relobj_file<size, big_endian>* object,
7645 unsigned int r_type)
7646 {
7647 gold_error(_("%s: unsupported reloc %u against local symbol"),
7648 object->name().c_str(), r_type);
7649 }
7650
7651 // We are about to emit a dynamic relocation of type R_TYPE. If the
7652 // dynamic linker does not support it, issue an error.
7653
7654 template<int size, bool big_endian>
7655 void
7656 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7657 unsigned int r_type)
7658 {
7659 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7660
7661 // These are the relocation types supported by glibc for both 32-bit
7662 // and 64-bit powerpc.
7663 switch (r_type)
7664 {
7665 case elfcpp::R_POWERPC_NONE:
7666 case elfcpp::R_POWERPC_RELATIVE:
7667 case elfcpp::R_POWERPC_GLOB_DAT:
7668 case elfcpp::R_POWERPC_DTPMOD:
7669 case elfcpp::R_POWERPC_DTPREL:
7670 case elfcpp::R_POWERPC_TPREL:
7671 case elfcpp::R_POWERPC_JMP_SLOT:
7672 case elfcpp::R_POWERPC_COPY:
7673 case elfcpp::R_POWERPC_IRELATIVE:
7674 case elfcpp::R_POWERPC_ADDR32:
7675 case elfcpp::R_POWERPC_UADDR32:
7676 case elfcpp::R_POWERPC_ADDR24:
7677 case elfcpp::R_POWERPC_ADDR16:
7678 case elfcpp::R_POWERPC_UADDR16:
7679 case elfcpp::R_POWERPC_ADDR16_LO:
7680 case elfcpp::R_POWERPC_ADDR16_HI:
7681 case elfcpp::R_POWERPC_ADDR16_HA:
7682 case elfcpp::R_POWERPC_ADDR14:
7683 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7684 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7685 case elfcpp::R_POWERPC_REL32:
7686 case elfcpp::R_POWERPC_TPREL16:
7687 case elfcpp::R_POWERPC_TPREL16_LO:
7688 case elfcpp::R_POWERPC_TPREL16_HI:
7689 case elfcpp::R_POWERPC_TPREL16_HA:
7690 return;
7691
7692 default:
7693 break;
7694 }
7695
7696 if (size == 64)
7697 {
7698 switch (r_type)
7699 {
7700 // These are the relocation types supported only on 64-bit.
7701 case elfcpp::R_PPC64_ADDR64:
7702 case elfcpp::R_PPC64_UADDR64:
7703 case elfcpp::R_PPC64_JMP_IREL:
7704 case elfcpp::R_PPC64_ADDR16_DS:
7705 case elfcpp::R_PPC64_ADDR16_LO_DS:
7706 case elfcpp::R_PPC64_ADDR16_HIGH:
7707 case elfcpp::R_PPC64_ADDR16_HIGHA:
7708 case elfcpp::R_PPC64_ADDR16_HIGHER:
7709 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7710 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7711 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7712 case elfcpp::R_PPC64_REL64:
7713 case elfcpp::R_POWERPC_ADDR30:
7714 case elfcpp::R_PPC64_TPREL16_DS:
7715 case elfcpp::R_PPC64_TPREL16_LO_DS:
7716 case elfcpp::R_PPC64_TPREL16_HIGH:
7717 case elfcpp::R_PPC64_TPREL16_HIGHA:
7718 case elfcpp::R_PPC64_TPREL16_HIGHER:
7719 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7720 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7721 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7722 return;
7723
7724 default:
7725 break;
7726 }
7727 }
7728 else
7729 {
7730 switch (r_type)
7731 {
7732 // These are the relocation types supported only on 32-bit.
7733 // ??? glibc ld.so doesn't need to support these.
7734 case elfcpp::R_POWERPC_REL24:
7735 case elfcpp::R_POWERPC_DTPREL16:
7736 case elfcpp::R_POWERPC_DTPREL16_LO:
7737 case elfcpp::R_POWERPC_DTPREL16_HI:
7738 case elfcpp::R_POWERPC_DTPREL16_HA:
7739 return;
7740
7741 default:
7742 break;
7743 }
7744 }
7745
7746 // This prevents us from issuing more than one error per reloc
7747 // section. But we can still wind up issuing more than one
7748 // error per object file.
7749 if (this->issued_non_pic_error_)
7750 return;
7751 gold_assert(parameters->options().output_is_position_independent());
7752 object->error(_("requires unsupported dynamic reloc; "
7753 "recompile with -fPIC"));
7754 this->issued_non_pic_error_ = true;
7755 return;
7756 }
7757
7758 // Return whether we need to make a PLT entry for a relocation of the
7759 // given type against a STT_GNU_IFUNC symbol.
7760
7761 template<int size, bool big_endian>
7762 bool
7763 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7764 Target_powerpc<size, big_endian>* target,
7765 Sized_relobj_file<size, big_endian>* object,
7766 unsigned int r_type,
7767 bool report_err)
7768 {
7769 // In non-pic code any reference will resolve to the plt call stub
7770 // for the ifunc symbol.
7771 if ((size == 32 || target->abiversion() >= 2)
7772 && !parameters->options().output_is_position_independent())
7773 return true;
7774
7775 switch (r_type)
7776 {
7777 // Word size refs from data sections are OK, but don't need a PLT entry.
7778 case elfcpp::R_POWERPC_ADDR32:
7779 case elfcpp::R_POWERPC_UADDR32:
7780 if (size == 32)
7781 return false;
7782 break;
7783
7784 case elfcpp::R_PPC64_ADDR64:
7785 case elfcpp::R_PPC64_UADDR64:
7786 if (size == 64)
7787 return false;
7788 break;
7789
7790 // GOT refs are good, but also don't need a PLT entry.
7791 case elfcpp::R_POWERPC_GOT16:
7792 case elfcpp::R_POWERPC_GOT16_LO:
7793 case elfcpp::R_POWERPC_GOT16_HI:
7794 case elfcpp::R_POWERPC_GOT16_HA:
7795 case elfcpp::R_PPC64_GOT16_DS:
7796 case elfcpp::R_PPC64_GOT16_LO_DS:
7797 case elfcpp::R_PPC64_GOT_PCREL34:
7798 return false;
7799
7800 // PLT relocs are OK and need a PLT entry.
7801 case elfcpp::R_POWERPC_PLT16_LO:
7802 case elfcpp::R_POWERPC_PLT16_HI:
7803 case elfcpp::R_POWERPC_PLT16_HA:
7804 case elfcpp::R_PPC64_PLT16_LO_DS:
7805 case elfcpp::R_POWERPC_PLTSEQ:
7806 case elfcpp::R_POWERPC_PLTCALL:
7807 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7808 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7809 case elfcpp::R_PPC64_PLT_PCREL34:
7810 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7811 return true;
7812 break;
7813
7814 // Function calls are good, and these do need a PLT entry.
7815 case elfcpp::R_PPC64_REL24_NOTOC:
7816 if (size == 32)
7817 break;
7818 // Fall through.
7819 case elfcpp::R_POWERPC_ADDR24:
7820 case elfcpp::R_POWERPC_ADDR14:
7821 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7822 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7823 case elfcpp::R_POWERPC_REL24:
7824 case elfcpp::R_PPC_PLTREL24:
7825 case elfcpp::R_POWERPC_REL14:
7826 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7827 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7828 return true;
7829
7830 default:
7831 break;
7832 }
7833
7834 // Anything else is a problem.
7835 // If we are building a static executable, the libc startup function
7836 // responsible for applying indirect function relocations is going
7837 // to complain about the reloc type.
7838 // If we are building a dynamic executable, we will have a text
7839 // relocation. The dynamic loader will set the text segment
7840 // writable and non-executable to apply text relocations. So we'll
7841 // segfault when trying to run the indirection function to resolve
7842 // the reloc.
7843 if (report_err)
7844 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7845 object->name().c_str(), r_type);
7846 return false;
7847 }
7848
7849 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7850 // reloc.
7851
7852 static bool
7853 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7854 {
7855 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7856 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7857 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7858 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7859 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7860 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7861 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7862 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7863 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7864 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7865 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7866 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7867 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7868 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7869 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7870 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7871 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7872 /* Exclude lfqu by testing reloc. If relocs are ever
7873 defined for the reduced D field in psq_lu then those
7874 will need testing too. */
7875 && r_type != elfcpp::R_PPC64_TOC16_LO
7876 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7877 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7878 && (insn & 1) == 0)
7879 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7880 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7881 /* Exclude stfqu. psq_stu as above for psq_lu. */
7882 && r_type != elfcpp::R_PPC64_TOC16_LO
7883 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7884 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7885 && (insn & 1) == 0));
7886 }
7887
7888 // Scan a relocation for a local symbol.
7889
7890 template<int size, bool big_endian>
7891 inline void
7892 Target_powerpc<size, big_endian>::Scan::local(
7893 Symbol_table* symtab,
7894 Layout* layout,
7895 Target_powerpc<size, big_endian>* target,
7896 Sized_relobj_file<size, big_endian>* object,
7897 unsigned int data_shndx,
7898 Output_section* output_section,
7899 const elfcpp::Rela<size, big_endian>& reloc,
7900 unsigned int r_type,
7901 const elfcpp::Sym<size, big_endian>& lsym,
7902 bool is_discarded)
7903 {
7904 Powerpc_relobj<size, big_endian>* ppc_object
7905 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7906
7907 switch (this->maybe_skip_tls_get_addr_call(target, r_type, NULL))
7908 {
7909 case Track_tls::NOT_EXPECTED:
7910 ppc_object->set_no_tls_marker();
7911 break;
7912 default:
7913 break;
7914 }
7915
7916 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7917 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7918 {
7919 this->expect_tls_get_addr_call();
7920 if (!ppc_object->no_tls_marker())
7921 {
7922 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7923 if (tls_type != tls::TLSOPT_NONE)
7924 {
7925 this->skip_next_tls_get_addr_call();
7926 ppc_object->set_tls_marker();
7927 }
7928 }
7929 }
7930 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7931 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7932 {
7933 this->expect_tls_get_addr_call();
7934 if (!ppc_object->no_tls_marker())
7935 {
7936 tls::Tls_optimization tls_type = target->optimize_tls_ld();
7937 if (tls_type != tls::TLSOPT_NONE)
7938 {
7939 this->skip_next_tls_get_addr_call();
7940 ppc_object->set_tls_marker();
7941 }
7942 }
7943 }
7944
7945 if (is_discarded)
7946 {
7947 if (size == 64
7948 && data_shndx == ppc_object->opd_shndx()
7949 && r_type == elfcpp::R_PPC64_ADDR64)
7950 ppc_object->set_opd_discard(reloc.get_r_offset());
7951 return;
7952 }
7953
7954 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7955 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7956 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7957 {
7958 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7959 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7960 r_type, r_sym, reloc.get_r_addend());
7961 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7962 }
7963
7964 switch (r_type)
7965 {
7966 case elfcpp::R_POWERPC_NONE:
7967 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7968 case elfcpp::R_POWERPC_GNU_VTENTRY:
7969 case elfcpp::R_POWERPC_TLS:
7970 case elfcpp::R_PPC64_ENTRY:
7971 case elfcpp::R_POWERPC_PLTSEQ:
7972 case elfcpp::R_POWERPC_PLTCALL:
7973 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7974 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7975 case elfcpp::R_PPC64_PCREL_OPT:
7976 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7977 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7978 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7979 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7980 case elfcpp::R_PPC64_REL16_HIGHER34:
7981 case elfcpp::R_PPC64_REL16_HIGHERA34:
7982 case elfcpp::R_PPC64_REL16_HIGHEST34:
7983 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7984 case elfcpp::R_PPC64_D34:
7985 case elfcpp::R_PPC64_D34_LO:
7986 case elfcpp::R_PPC64_D34_HI30:
7987 case elfcpp::R_PPC64_D34_HA30:
7988 case elfcpp::R_PPC64_D28:
7989 case elfcpp::R_PPC64_PCREL34:
7990 case elfcpp::R_PPC64_PCREL28:
7991 case elfcpp::R_PPC64_TPREL34:
7992 case elfcpp::R_PPC64_DTPREL34:
7993 break;
7994
7995 case elfcpp::R_PPC64_TOC:
7996 {
7997 Output_data_got_powerpc<size, big_endian>* got
7998 = target->got_section(symtab, layout);
7999 if (parameters->options().output_is_position_independent())
8000 {
8001 Address off = reloc.get_r_offset();
8002 if (size == 64
8003 && target->abiversion() < 2
8004 && data_shndx == ppc_object->opd_shndx()
8005 && ppc_object->get_opd_discard(off - 8))
8006 break;
8007
8008 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8009 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8010 rela_dyn->add_output_section_relative(got->output_section(),
8011 elfcpp::R_POWERPC_RELATIVE,
8012 output_section,
8013 object, data_shndx, off,
8014 symobj->toc_base_offset());
8015 }
8016 }
8017 break;
8018
8019 case elfcpp::R_PPC64_ADDR64:
8020 case elfcpp::R_PPC64_UADDR64:
8021 case elfcpp::R_POWERPC_ADDR32:
8022 case elfcpp::R_POWERPC_UADDR32:
8023 case elfcpp::R_POWERPC_ADDR24:
8024 case elfcpp::R_POWERPC_ADDR16:
8025 case elfcpp::R_POWERPC_ADDR16_LO:
8026 case elfcpp::R_POWERPC_ADDR16_HI:
8027 case elfcpp::R_POWERPC_ADDR16_HA:
8028 case elfcpp::R_POWERPC_UADDR16:
8029 case elfcpp::R_PPC64_ADDR16_HIGH:
8030 case elfcpp::R_PPC64_ADDR16_HIGHA:
8031 case elfcpp::R_PPC64_ADDR16_HIGHER:
8032 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8033 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8034 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8035 case elfcpp::R_PPC64_ADDR16_DS:
8036 case elfcpp::R_PPC64_ADDR16_LO_DS:
8037 case elfcpp::R_POWERPC_ADDR14:
8038 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8039 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8040 // If building a shared library (or a position-independent
8041 // executable), we need to create a dynamic relocation for
8042 // this location.
8043 if (parameters->options().output_is_position_independent()
8044 || (size == 64 && is_ifunc && target->abiversion() < 2))
8045 {
8046 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8047 is_ifunc);
8048 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8049 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8050 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8051 {
8052 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8053 : elfcpp::R_POWERPC_RELATIVE);
8054 rela_dyn->add_local_relative(object, r_sym, dynrel,
8055 output_section, data_shndx,
8056 reloc.get_r_offset(),
8057 reloc.get_r_addend(), false);
8058 }
8059 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8060 {
8061 check_non_pic(object, r_type);
8062 rela_dyn->add_local(object, r_sym, r_type, output_section,
8063 data_shndx, reloc.get_r_offset(),
8064 reloc.get_r_addend());
8065 }
8066 else
8067 {
8068 gold_assert(lsym.get_st_value() == 0);
8069 unsigned int shndx = lsym.get_st_shndx();
8070 bool is_ordinary;
8071 shndx = object->adjust_sym_shndx(r_sym, shndx,
8072 &is_ordinary);
8073 if (!is_ordinary)
8074 object->error(_("section symbol %u has bad shndx %u"),
8075 r_sym, shndx);
8076 else
8077 rela_dyn->add_local_section(object, shndx, r_type,
8078 output_section, data_shndx,
8079 reloc.get_r_offset());
8080 }
8081 }
8082 break;
8083
8084 case elfcpp::R_PPC64_PLT_PCREL34:
8085 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8086 case elfcpp::R_POWERPC_PLT16_LO:
8087 case elfcpp::R_POWERPC_PLT16_HI:
8088 case elfcpp::R_POWERPC_PLT16_HA:
8089 case elfcpp::R_PPC64_PLT16_LO_DS:
8090 if (!is_ifunc)
8091 {
8092 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8093 target->make_local_plt_entry(layout, object, r_sym);
8094 }
8095 break;
8096
8097 case elfcpp::R_PPC64_REL24_NOTOC:
8098 if (size == 32)
8099 break;
8100 // Fall through.
8101 case elfcpp::R_POWERPC_REL24:
8102 case elfcpp::R_PPC_PLTREL24:
8103 case elfcpp::R_PPC_LOCAL24PC:
8104 case elfcpp::R_POWERPC_REL14:
8105 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8106 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8107 if (!is_ifunc)
8108 {
8109 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8110 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8111 r_type, r_sym, reloc.get_r_addend());
8112 }
8113 break;
8114
8115 case elfcpp::R_PPC64_TOCSAVE:
8116 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8117 // caller has already saved r2 and thus a plt call stub need not
8118 // save r2.
8119 if (size == 64
8120 && target->mark_pltcall(ppc_object, data_shndx,
8121 reloc.get_r_offset() - 4, symtab))
8122 {
8123 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8124 unsigned int shndx = lsym.get_st_shndx();
8125 bool is_ordinary;
8126 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8127 if (!is_ordinary)
8128 object->error(_("tocsave symbol %u has bad shndx %u"),
8129 r_sym, shndx);
8130 else
8131 target->add_tocsave(ppc_object, shndx,
8132 lsym.get_st_value() + reloc.get_r_addend());
8133 }
8134 break;
8135
8136 case elfcpp::R_PPC64_REL64:
8137 case elfcpp::R_POWERPC_REL32:
8138 case elfcpp::R_POWERPC_REL16:
8139 case elfcpp::R_POWERPC_REL16_LO:
8140 case elfcpp::R_POWERPC_REL16_HI:
8141 case elfcpp::R_POWERPC_REL16_HA:
8142 case elfcpp::R_POWERPC_REL16DX_HA:
8143 case elfcpp::R_PPC64_REL16_HIGH:
8144 case elfcpp::R_PPC64_REL16_HIGHA:
8145 case elfcpp::R_PPC64_REL16_HIGHER:
8146 case elfcpp::R_PPC64_REL16_HIGHERA:
8147 case elfcpp::R_PPC64_REL16_HIGHEST:
8148 case elfcpp::R_PPC64_REL16_HIGHESTA:
8149 case elfcpp::R_POWERPC_SECTOFF:
8150 case elfcpp::R_POWERPC_SECTOFF_LO:
8151 case elfcpp::R_POWERPC_SECTOFF_HI:
8152 case elfcpp::R_POWERPC_SECTOFF_HA:
8153 case elfcpp::R_PPC64_SECTOFF_DS:
8154 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8155 case elfcpp::R_POWERPC_TPREL16:
8156 case elfcpp::R_POWERPC_TPREL16_LO:
8157 case elfcpp::R_POWERPC_TPREL16_HI:
8158 case elfcpp::R_POWERPC_TPREL16_HA:
8159 case elfcpp::R_PPC64_TPREL16_DS:
8160 case elfcpp::R_PPC64_TPREL16_LO_DS:
8161 case elfcpp::R_PPC64_TPREL16_HIGH:
8162 case elfcpp::R_PPC64_TPREL16_HIGHA:
8163 case elfcpp::R_PPC64_TPREL16_HIGHER:
8164 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8165 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8166 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8167 case elfcpp::R_POWERPC_DTPREL16:
8168 case elfcpp::R_POWERPC_DTPREL16_LO:
8169 case elfcpp::R_POWERPC_DTPREL16_HI:
8170 case elfcpp::R_POWERPC_DTPREL16_HA:
8171 case elfcpp::R_PPC64_DTPREL16_DS:
8172 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8173 case elfcpp::R_PPC64_DTPREL16_HIGH:
8174 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8175 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8176 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8177 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8178 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8179 case elfcpp::R_PPC64_TLSGD:
8180 case elfcpp::R_PPC64_TLSLD:
8181 case elfcpp::R_PPC64_ADDR64_LOCAL:
8182 break;
8183
8184 case elfcpp::R_PPC64_GOT_PCREL34:
8185 case elfcpp::R_POWERPC_GOT16:
8186 case elfcpp::R_POWERPC_GOT16_LO:
8187 case elfcpp::R_POWERPC_GOT16_HI:
8188 case elfcpp::R_POWERPC_GOT16_HA:
8189 case elfcpp::R_PPC64_GOT16_DS:
8190 case elfcpp::R_PPC64_GOT16_LO_DS:
8191 {
8192 // The symbol requires a GOT entry.
8193 Output_data_got_powerpc<size, big_endian>* got
8194 = target->got_section(symtab, layout);
8195 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8196
8197 if (!parameters->options().output_is_position_independent())
8198 {
8199 if (is_ifunc
8200 && (size == 32 || target->abiversion() >= 2))
8201 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
8202 else
8203 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
8204 }
8205 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
8206 {
8207 // If we are generating a shared object or a pie, this
8208 // symbol's GOT entry will be set by a dynamic relocation.
8209 unsigned int off;
8210 off = got->add_constant(0);
8211 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
8212
8213 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8214 is_ifunc);
8215 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8216 : elfcpp::R_POWERPC_RELATIVE);
8217 rela_dyn->add_local_relative(object, r_sym, dynrel,
8218 got, off, 0, false);
8219 }
8220 }
8221 break;
8222
8223 case elfcpp::R_PPC64_TOC16:
8224 case elfcpp::R_PPC64_TOC16_LO:
8225 case elfcpp::R_PPC64_TOC16_HI:
8226 case elfcpp::R_PPC64_TOC16_HA:
8227 case elfcpp::R_PPC64_TOC16_DS:
8228 case elfcpp::R_PPC64_TOC16_LO_DS:
8229 // We need a GOT section.
8230 target->got_section(symtab, layout);
8231 break;
8232
8233 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8234 case elfcpp::R_POWERPC_GOT_TLSGD16:
8235 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8236 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8237 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8238 {
8239 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
8240 if (!ppc_object->no_tls_marker())
8241 tls_type = target->optimize_tls_gd(true);
8242 if (tls_type == tls::TLSOPT_NONE)
8243 {
8244 Output_data_got_powerpc<size, big_endian>* got
8245 = target->got_section(symtab, layout);
8246 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8247 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8248 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
8249 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
8250 }
8251 else if (tls_type == tls::TLSOPT_TO_LE)
8252 {
8253 // no GOT relocs needed for Local Exec.
8254 ppc_object->set_tls_marker();
8255 }
8256 else
8257 gold_unreachable();
8258 }
8259 break;
8260
8261 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8262 case elfcpp::R_POWERPC_GOT_TLSLD16:
8263 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8264 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8265 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8266 {
8267 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
8268 if (!ppc_object->no_tls_marker())
8269 tls_type = target->optimize_tls_ld();
8270 if (tls_type == tls::TLSOPT_NONE)
8271 target->tlsld_got_offset(symtab, layout, object);
8272 else if (tls_type == tls::TLSOPT_TO_LE)
8273 {
8274 // no GOT relocs needed for Local Exec.
8275 if (parameters->options().emit_relocs())
8276 {
8277 Output_section* os = layout->tls_segment()->first_section();
8278 gold_assert(os != NULL);
8279 os->set_needs_symtab_index();
8280 }
8281 ppc_object->set_tls_marker();
8282 }
8283 else
8284 gold_unreachable();
8285 }
8286 break;
8287
8288 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8289 case elfcpp::R_POWERPC_GOT_DTPREL16:
8290 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8291 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8292 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8293 {
8294 Output_data_got_powerpc<size, big_endian>* got
8295 = target->got_section(symtab, layout);
8296 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8297 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
8298 }
8299 break;
8300
8301 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8302 case elfcpp::R_POWERPC_GOT_TPREL16:
8303 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8304 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8305 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8306 {
8307 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8308 if (tls_type == tls::TLSOPT_NONE)
8309 {
8310 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8311 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
8312 {
8313 Output_data_got_powerpc<size, big_endian>* got
8314 = target->got_section(symtab, layout);
8315 unsigned int off = got->add_constant(0);
8316 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
8317
8318 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8319 rela_dyn->add_symbolless_local_addend(object, r_sym,
8320 elfcpp::R_POWERPC_TPREL,
8321 got, off, 0);
8322 }
8323 }
8324 else if (tls_type == tls::TLSOPT_TO_LE)
8325 {
8326 // no GOT relocs needed for Local Exec.
8327 }
8328 else
8329 gold_unreachable();
8330 }
8331 break;
8332
8333 default:
8334 unsupported_reloc_local(object, r_type);
8335 break;
8336 }
8337
8338 if (size == 64
8339 && parameters->options().toc_optimize())
8340 {
8341 if (data_shndx == ppc_object->toc_shndx())
8342 {
8343 bool ok = true;
8344 if (r_type != elfcpp::R_PPC64_ADDR64
8345 || (is_ifunc && target->abiversion() < 2))
8346 ok = false;
8347 else if (parameters->options().output_is_position_independent())
8348 {
8349 if (is_ifunc)
8350 ok = false;
8351 else
8352 {
8353 unsigned int shndx = lsym.get_st_shndx();
8354 if (shndx >= elfcpp::SHN_LORESERVE
8355 && shndx != elfcpp::SHN_XINDEX)
8356 ok = false;
8357 }
8358 }
8359 if (!ok)
8360 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8361 }
8362
8363 enum {no_check, check_lo, check_ha} insn_check;
8364 switch (r_type)
8365 {
8366 default:
8367 insn_check = no_check;
8368 break;
8369
8370 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8371 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8372 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8373 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8374 case elfcpp::R_POWERPC_GOT16_HA:
8375 case elfcpp::R_PPC64_TOC16_HA:
8376 insn_check = check_ha;
8377 break;
8378
8379 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8380 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8381 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8382 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8383 case elfcpp::R_POWERPC_GOT16_LO:
8384 case elfcpp::R_PPC64_GOT16_LO_DS:
8385 case elfcpp::R_PPC64_TOC16_LO:
8386 case elfcpp::R_PPC64_TOC16_LO_DS:
8387 insn_check = check_lo;
8388 break;
8389 }
8390
8391 section_size_type slen;
8392 const unsigned char* view = NULL;
8393 if (insn_check != no_check)
8394 {
8395 view = ppc_object->section_contents(data_shndx, &slen, false);
8396 section_size_type off =
8397 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8398 if (off < slen)
8399 {
8400 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8401 if (insn_check == check_lo
8402 ? !ok_lo_toc_insn(insn, r_type)
8403 : ((insn & ((0x3f << 26) | 0x1f << 16))
8404 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8405 {
8406 ppc_object->set_no_toc_opt();
8407 gold_warning(_("%s: toc optimization is not supported "
8408 "for %#08x instruction"),
8409 ppc_object->name().c_str(), insn);
8410 }
8411 }
8412 }
8413
8414 switch (r_type)
8415 {
8416 default:
8417 break;
8418 case elfcpp::R_PPC64_TOC16:
8419 case elfcpp::R_PPC64_TOC16_LO:
8420 case elfcpp::R_PPC64_TOC16_HI:
8421 case elfcpp::R_PPC64_TOC16_HA:
8422 case elfcpp::R_PPC64_TOC16_DS:
8423 case elfcpp::R_PPC64_TOC16_LO_DS:
8424 unsigned int shndx = lsym.get_st_shndx();
8425 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8426 bool is_ordinary;
8427 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8428 if (is_ordinary && shndx == ppc_object->toc_shndx())
8429 {
8430 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8431 if (dst_off < ppc_object->section_size(shndx))
8432 {
8433 bool ok = false;
8434 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8435 ok = true;
8436 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8437 {
8438 // Need to check that the insn is a ld
8439 if (!view)
8440 view = ppc_object->section_contents(data_shndx,
8441 &slen,
8442 false);
8443 section_size_type off =
8444 (convert_to_section_size_type(reloc.get_r_offset())
8445 + (big_endian ? -2 : 3));
8446 if (off < slen
8447 && (view[off] & (0x3f << 2)) == 58u << 2)
8448 ok = true;
8449 }
8450 if (!ok)
8451 ppc_object->set_no_toc_opt(dst_off);
8452 }
8453 }
8454 break;
8455 }
8456 }
8457
8458 if (size == 32)
8459 {
8460 switch (r_type)
8461 {
8462 case elfcpp::R_POWERPC_REL32:
8463 if (ppc_object->got2_shndx() != 0
8464 && parameters->options().output_is_position_independent())
8465 {
8466 unsigned int shndx = lsym.get_st_shndx();
8467 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8468 bool is_ordinary;
8469 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8470 if (is_ordinary && shndx == ppc_object->got2_shndx()
8471 && (ppc_object->section_flags(data_shndx)
8472 & elfcpp::SHF_EXECINSTR) != 0)
8473 gold_error(_("%s: unsupported -mbss-plt code"),
8474 ppc_object->name().c_str());
8475 }
8476 break;
8477 default:
8478 break;
8479 }
8480 }
8481
8482 switch (r_type)
8483 {
8484 case elfcpp::R_POWERPC_GOT_TLSLD16:
8485 case elfcpp::R_POWERPC_GOT_TLSGD16:
8486 case elfcpp::R_POWERPC_GOT_TPREL16:
8487 case elfcpp::R_POWERPC_GOT_DTPREL16:
8488 case elfcpp::R_POWERPC_GOT16:
8489 case elfcpp::R_PPC64_GOT16_DS:
8490 case elfcpp::R_PPC64_TOC16:
8491 case elfcpp::R_PPC64_TOC16_DS:
8492 ppc_object->set_has_small_toc_reloc();
8493 break;
8494 default:
8495 break;
8496 }
8497
8498 switch (r_type)
8499 {
8500 case elfcpp::R_PPC64_TPREL16_DS:
8501 case elfcpp::R_PPC64_TPREL16_LO_DS:
8502 case elfcpp::R_PPC64_TPREL16_HIGH:
8503 case elfcpp::R_PPC64_TPREL16_HIGHA:
8504 case elfcpp::R_PPC64_TPREL16_HIGHER:
8505 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8506 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8507 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8508 case elfcpp::R_PPC64_TPREL34:
8509 if (size != 64)
8510 break;
8511 // Fall through.
8512 case elfcpp::R_POWERPC_TPREL16:
8513 case elfcpp::R_POWERPC_TPREL16_LO:
8514 case elfcpp::R_POWERPC_TPREL16_HI:
8515 case elfcpp::R_POWERPC_TPREL16_HA:
8516 layout->set_has_static_tls();
8517 break;
8518 default:
8519 break;
8520 }
8521
8522 switch (r_type)
8523 {
8524 case elfcpp::R_POWERPC_TPREL16_HA:
8525 if (target->tprel_opt())
8526 {
8527 section_size_type slen;
8528 const unsigned char* view = NULL;
8529 view = ppc_object->section_contents(data_shndx, &slen, false);
8530 section_size_type off
8531 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8532 if (off < slen)
8533 {
8534 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8535 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8536 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8537 target->set_no_tprel_opt();
8538 }
8539 }
8540 break;
8541
8542 case elfcpp::R_PPC64_TPREL16_HIGH:
8543 case elfcpp::R_PPC64_TPREL16_HIGHA:
8544 case elfcpp::R_PPC64_TPREL16_HIGHER:
8545 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8546 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8547 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8548 if (size != 64)
8549 break;
8550 // Fall through.
8551 case elfcpp::R_POWERPC_TPREL16_HI:
8552 target->set_no_tprel_opt();
8553 break;
8554 default:
8555 break;
8556 }
8557
8558 switch (r_type)
8559 {
8560 case elfcpp::R_PPC64_D34:
8561 case elfcpp::R_PPC64_D34_LO:
8562 case elfcpp::R_PPC64_D34_HI30:
8563 case elfcpp::R_PPC64_D34_HA30:
8564 case elfcpp::R_PPC64_D28:
8565 case elfcpp::R_PPC64_PCREL34:
8566 case elfcpp::R_PPC64_PCREL28:
8567 case elfcpp::R_PPC64_TPREL34:
8568 case elfcpp::R_PPC64_DTPREL34:
8569 case elfcpp::R_PPC64_PLT_PCREL34:
8570 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8571 case elfcpp::R_PPC64_GOT_PCREL34:
8572 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8573 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8574 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8575 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8576 target->set_power10_relocs();
8577 break;
8578 default:
8579 break;
8580 }
8581 }
8582
8583 // Report an unsupported relocation against a global symbol.
8584
8585 template<int size, bool big_endian>
8586 void
8587 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8588 Sized_relobj_file<size, big_endian>* object,
8589 unsigned int r_type,
8590 Symbol* gsym)
8591 {
8592 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8593 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8594 }
8595
8596 // Scan a relocation for a global symbol.
8597
8598 template<int size, bool big_endian>
8599 inline void
8600 Target_powerpc<size, big_endian>::Scan::global(
8601 Symbol_table* symtab,
8602 Layout* layout,
8603 Target_powerpc<size, big_endian>* target,
8604 Sized_relobj_file<size, big_endian>* object,
8605 unsigned int data_shndx,
8606 Output_section* output_section,
8607 const elfcpp::Rela<size, big_endian>& reloc,
8608 unsigned int r_type,
8609 Symbol* gsym)
8610 {
8611 Powerpc_relobj<size, big_endian>* ppc_object
8612 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8613
8614 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8615 {
8616 case Track_tls::NOT_EXPECTED:
8617 ppc_object->set_no_tls_marker();
8618 break;
8619 case Track_tls::SKIP:
8620 return;
8621 default:
8622 break;
8623 }
8624
8625 if (target->replace_tls_get_addr(gsym))
8626 // Change a __tls_get_addr reference to __tls_get_addr_opt
8627 // so dynamic relocs are emitted against the latter symbol.
8628 gsym = target->tls_get_addr_opt();
8629
8630 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8631 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8632 {
8633 this->expect_tls_get_addr_call();
8634 if (!ppc_object->no_tls_marker())
8635 {
8636 bool final = gsym->final_value_is_known();
8637 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8638 if (tls_type != tls::TLSOPT_NONE)
8639 {
8640 this->skip_next_tls_get_addr_call();
8641 ppc_object->set_tls_marker();
8642 }
8643 }
8644 }
8645 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8646 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8647 {
8648 this->expect_tls_get_addr_call();
8649 if (!ppc_object->no_tls_marker())
8650 {
8651 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8652 if (tls_type != tls::TLSOPT_NONE)
8653 {
8654 this->skip_next_tls_get_addr_call();
8655 ppc_object->set_tls_marker();
8656 }
8657 }
8658 }
8659
8660 // A STT_GNU_IFUNC symbol may require a PLT entry.
8661 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8662 bool pushed_ifunc = false;
8663 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8664 {
8665 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8666 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8667 r_type, r_sym, reloc.get_r_addend());
8668 target->make_plt_entry(symtab, layout, gsym);
8669 pushed_ifunc = true;
8670 }
8671
8672 switch (r_type)
8673 {
8674 case elfcpp::R_POWERPC_NONE:
8675 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8676 case elfcpp::R_POWERPC_GNU_VTENTRY:
8677 case elfcpp::R_PPC_LOCAL24PC:
8678 case elfcpp::R_POWERPC_TLS:
8679 case elfcpp::R_PPC64_ENTRY:
8680 case elfcpp::R_POWERPC_PLTSEQ:
8681 case elfcpp::R_POWERPC_PLTCALL:
8682 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8683 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8684 case elfcpp::R_PPC64_PCREL_OPT:
8685 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8686 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8687 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8688 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8689 case elfcpp::R_PPC64_REL16_HIGHER34:
8690 case elfcpp::R_PPC64_REL16_HIGHERA34:
8691 case elfcpp::R_PPC64_REL16_HIGHEST34:
8692 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8693 case elfcpp::R_PPC64_D34:
8694 case elfcpp::R_PPC64_D34_LO:
8695 case elfcpp::R_PPC64_D34_HI30:
8696 case elfcpp::R_PPC64_D34_HA30:
8697 case elfcpp::R_PPC64_D28:
8698 case elfcpp::R_PPC64_PCREL34:
8699 case elfcpp::R_PPC64_PCREL28:
8700 case elfcpp::R_PPC64_TPREL34:
8701 case elfcpp::R_PPC64_DTPREL34:
8702 break;
8703
8704 case elfcpp::R_PPC64_TOC:
8705 {
8706 Output_data_got_powerpc<size, big_endian>* got
8707 = target->got_section(symtab, layout);
8708 if (parameters->options().output_is_position_independent())
8709 {
8710 Address off = reloc.get_r_offset();
8711 if (size == 64
8712 && data_shndx == ppc_object->opd_shndx()
8713 && ppc_object->get_opd_discard(off - 8))
8714 break;
8715
8716 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8717 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8718 if (data_shndx != ppc_object->opd_shndx())
8719 symobj = static_cast
8720 <Powerpc_relobj<size, big_endian>*>(gsym->object());
8721 rela_dyn->add_output_section_relative(got->output_section(),
8722 elfcpp::R_POWERPC_RELATIVE,
8723 output_section,
8724 object, data_shndx, off,
8725 symobj->toc_base_offset());
8726 }
8727 }
8728 break;
8729
8730 case elfcpp::R_PPC64_ADDR64:
8731 if (size == 64
8732 && target->abiversion() < 2
8733 && data_shndx == ppc_object->opd_shndx()
8734 && (gsym->is_defined_in_discarded_section()
8735 || gsym->object() != object))
8736 {
8737 ppc_object->set_opd_discard(reloc.get_r_offset());
8738 break;
8739 }
8740 // Fall through.
8741 case elfcpp::R_PPC64_UADDR64:
8742 case elfcpp::R_POWERPC_ADDR32:
8743 case elfcpp::R_POWERPC_UADDR32:
8744 case elfcpp::R_POWERPC_ADDR24:
8745 case elfcpp::R_POWERPC_ADDR16:
8746 case elfcpp::R_POWERPC_ADDR16_LO:
8747 case elfcpp::R_POWERPC_ADDR16_HI:
8748 case elfcpp::R_POWERPC_ADDR16_HA:
8749 case elfcpp::R_POWERPC_UADDR16:
8750 case elfcpp::R_PPC64_ADDR16_HIGH:
8751 case elfcpp::R_PPC64_ADDR16_HIGHA:
8752 case elfcpp::R_PPC64_ADDR16_HIGHER:
8753 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8754 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8755 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8756 case elfcpp::R_PPC64_ADDR16_DS:
8757 case elfcpp::R_PPC64_ADDR16_LO_DS:
8758 case elfcpp::R_POWERPC_ADDR14:
8759 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8760 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8761 {
8762 // Make a PLT entry if necessary.
8763 if (gsym->needs_plt_entry())
8764 {
8765 // Since this is not a PC-relative relocation, we may be
8766 // taking the address of a function. In that case we need to
8767 // set the entry in the dynamic symbol table to the address of
8768 // the PLT call stub.
8769 bool need_ifunc_plt = false;
8770 if ((size == 32 || target->abiversion() >= 2)
8771 && gsym->is_from_dynobj()
8772 && !parameters->options().output_is_position_independent())
8773 {
8774 gsym->set_needs_dynsym_value();
8775 need_ifunc_plt = true;
8776 }
8777 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8778 {
8779 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8780 target->push_branch(ppc_object, data_shndx,
8781 reloc.get_r_offset(), r_type, r_sym,
8782 reloc.get_r_addend());
8783 target->make_plt_entry(symtab, layout, gsym);
8784 }
8785 }
8786 // Make a dynamic relocation if necessary.
8787 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8788 || (size == 64 && is_ifunc && target->abiversion() < 2))
8789 {
8790 if (!parameters->options().output_is_position_independent()
8791 && gsym->may_need_copy_reloc())
8792 {
8793 target->copy_reloc(symtab, layout, object,
8794 data_shndx, output_section, gsym, reloc);
8795 }
8796 else if ((((size == 32
8797 && r_type == elfcpp::R_POWERPC_ADDR32)
8798 || (size == 64
8799 && r_type == elfcpp::R_PPC64_ADDR64
8800 && target->abiversion() >= 2))
8801 && gsym->can_use_relative_reloc(false)
8802 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8803 && parameters->options().shared()))
8804 || (size == 64
8805 && r_type == elfcpp::R_PPC64_ADDR64
8806 && target->abiversion() < 2
8807 && (gsym->can_use_relative_reloc(false)
8808 || data_shndx == ppc_object->opd_shndx())))
8809 {
8810 Reloc_section* rela_dyn
8811 = target->rela_dyn_section(symtab, layout, is_ifunc);
8812 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8813 : elfcpp::R_POWERPC_RELATIVE);
8814 rela_dyn->add_symbolless_global_addend(
8815 gsym, dynrel, output_section, object, data_shndx,
8816 reloc.get_r_offset(), reloc.get_r_addend());
8817 }
8818 else
8819 {
8820 Reloc_section* rela_dyn
8821 = target->rela_dyn_section(symtab, layout, is_ifunc);
8822 check_non_pic(object, r_type);
8823 rela_dyn->add_global(gsym, r_type, output_section,
8824 object, data_shndx,
8825 reloc.get_r_offset(),
8826 reloc.get_r_addend());
8827
8828 if (size == 64
8829 && parameters->options().toc_optimize()
8830 && data_shndx == ppc_object->toc_shndx())
8831 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8832 }
8833 }
8834 }
8835 break;
8836
8837 case elfcpp::R_PPC64_PLT_PCREL34:
8838 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8839 case elfcpp::R_POWERPC_PLT16_LO:
8840 case elfcpp::R_POWERPC_PLT16_HI:
8841 case elfcpp::R_POWERPC_PLT16_HA:
8842 case elfcpp::R_PPC64_PLT16_LO_DS:
8843 if (!pushed_ifunc)
8844 target->make_plt_entry(symtab, layout, gsym);
8845 break;
8846
8847 case elfcpp::R_PPC64_REL24_NOTOC:
8848 if (size == 32)
8849 break;
8850 // Fall through.
8851 case elfcpp::R_PPC_PLTREL24:
8852 case elfcpp::R_POWERPC_REL24:
8853 if (!is_ifunc)
8854 {
8855 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8856 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8857 r_type, r_sym, reloc.get_r_addend());
8858 if (gsym->needs_plt_entry()
8859 || (!gsym->final_value_is_known()
8860 && (gsym->is_undefined()
8861 || gsym->is_from_dynobj()
8862 || gsym->is_preemptible())))
8863 target->make_plt_entry(symtab, layout, gsym);
8864 }
8865 // Fall through.
8866
8867 case elfcpp::R_PPC64_REL64:
8868 case elfcpp::R_POWERPC_REL32:
8869 // Make a dynamic relocation if necessary.
8870 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8871 {
8872 if (!parameters->options().output_is_position_independent()
8873 && gsym->may_need_copy_reloc())
8874 {
8875 target->copy_reloc(symtab, layout, object,
8876 data_shndx, output_section, gsym,
8877 reloc);
8878 }
8879 else
8880 {
8881 Reloc_section* rela_dyn
8882 = target->rela_dyn_section(symtab, layout, is_ifunc);
8883 check_non_pic(object, r_type);
8884 rela_dyn->add_global(gsym, r_type, output_section, object,
8885 data_shndx, reloc.get_r_offset(),
8886 reloc.get_r_addend());
8887 }
8888 }
8889 break;
8890
8891 case elfcpp::R_POWERPC_REL14:
8892 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8893 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8894 if (!is_ifunc)
8895 {
8896 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8897 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8898 r_type, r_sym, reloc.get_r_addend());
8899 }
8900 break;
8901
8902 case elfcpp::R_PPC64_TOCSAVE:
8903 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8904 // caller has already saved r2 and thus a plt call stub need not
8905 // save r2.
8906 if (size == 64
8907 && target->mark_pltcall(ppc_object, data_shndx,
8908 reloc.get_r_offset() - 4, symtab))
8909 {
8910 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8911 bool is_ordinary;
8912 unsigned int shndx = gsym->shndx(&is_ordinary);
8913 if (!is_ordinary)
8914 object->error(_("tocsave symbol %u has bad shndx %u"),
8915 r_sym, shndx);
8916 else
8917 {
8918 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8919 target->add_tocsave(ppc_object, shndx,
8920 sym->value() + reloc.get_r_addend());
8921 }
8922 }
8923 break;
8924
8925 case elfcpp::R_POWERPC_REL16:
8926 case elfcpp::R_POWERPC_REL16_LO:
8927 case elfcpp::R_POWERPC_REL16_HI:
8928 case elfcpp::R_POWERPC_REL16_HA:
8929 case elfcpp::R_POWERPC_REL16DX_HA:
8930 case elfcpp::R_PPC64_REL16_HIGH:
8931 case elfcpp::R_PPC64_REL16_HIGHA:
8932 case elfcpp::R_PPC64_REL16_HIGHER:
8933 case elfcpp::R_PPC64_REL16_HIGHERA:
8934 case elfcpp::R_PPC64_REL16_HIGHEST:
8935 case elfcpp::R_PPC64_REL16_HIGHESTA:
8936 case elfcpp::R_POWERPC_SECTOFF:
8937 case elfcpp::R_POWERPC_SECTOFF_LO:
8938 case elfcpp::R_POWERPC_SECTOFF_HI:
8939 case elfcpp::R_POWERPC_SECTOFF_HA:
8940 case elfcpp::R_PPC64_SECTOFF_DS:
8941 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8942 case elfcpp::R_POWERPC_TPREL16:
8943 case elfcpp::R_POWERPC_TPREL16_LO:
8944 case elfcpp::R_POWERPC_TPREL16_HI:
8945 case elfcpp::R_POWERPC_TPREL16_HA:
8946 case elfcpp::R_PPC64_TPREL16_DS:
8947 case elfcpp::R_PPC64_TPREL16_LO_DS:
8948 case elfcpp::R_PPC64_TPREL16_HIGH:
8949 case elfcpp::R_PPC64_TPREL16_HIGHA:
8950 case elfcpp::R_PPC64_TPREL16_HIGHER:
8951 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8952 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8953 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8954 case elfcpp::R_POWERPC_DTPREL16:
8955 case elfcpp::R_POWERPC_DTPREL16_LO:
8956 case elfcpp::R_POWERPC_DTPREL16_HI:
8957 case elfcpp::R_POWERPC_DTPREL16_HA:
8958 case elfcpp::R_PPC64_DTPREL16_DS:
8959 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8960 case elfcpp::R_PPC64_DTPREL16_HIGH:
8961 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8962 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8963 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8964 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8965 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8966 case elfcpp::R_PPC64_TLSGD:
8967 case elfcpp::R_PPC64_TLSLD:
8968 case elfcpp::R_PPC64_ADDR64_LOCAL:
8969 break;
8970
8971 case elfcpp::R_PPC64_GOT_PCREL34:
8972 case elfcpp::R_POWERPC_GOT16:
8973 case elfcpp::R_POWERPC_GOT16_LO:
8974 case elfcpp::R_POWERPC_GOT16_HI:
8975 case elfcpp::R_POWERPC_GOT16_HA:
8976 case elfcpp::R_PPC64_GOT16_DS:
8977 case elfcpp::R_PPC64_GOT16_LO_DS:
8978 {
8979 // The symbol requires a GOT entry.
8980 Output_data_got_powerpc<size, big_endian>* got;
8981
8982 got = target->got_section(symtab, layout);
8983 if (gsym->final_value_is_known())
8984 {
8985 if (is_ifunc
8986 && (size == 32 || target->abiversion() >= 2))
8987 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8988 else
8989 got->add_global(gsym, GOT_TYPE_STANDARD);
8990 }
8991 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8992 {
8993 // If we are generating a shared object or a pie, this
8994 // symbol's GOT entry will be set by a dynamic relocation.
8995 unsigned int off = got->add_constant(0);
8996 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8997
8998 Reloc_section* rela_dyn
8999 = target->rela_dyn_section(symtab, layout, is_ifunc);
9000
9001 if (gsym->can_use_relative_reloc(false)
9002 && !((size == 32
9003 || target->abiversion() >= 2)
9004 && gsym->visibility() == elfcpp::STV_PROTECTED
9005 && parameters->options().shared()))
9006 {
9007 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9008 : elfcpp::R_POWERPC_RELATIVE);
9009 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
9010 }
9011 else
9012 {
9013 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
9014 rela_dyn->add_global(gsym, dynrel, got, off, 0);
9015 }
9016 }
9017 }
9018 break;
9019
9020 case elfcpp::R_PPC64_TOC16:
9021 case elfcpp::R_PPC64_TOC16_LO:
9022 case elfcpp::R_PPC64_TOC16_HI:
9023 case elfcpp::R_PPC64_TOC16_HA:
9024 case elfcpp::R_PPC64_TOC16_DS:
9025 case elfcpp::R_PPC64_TOC16_LO_DS:
9026 // We need a GOT section.
9027 target->got_section(symtab, layout);
9028 break;
9029
9030 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9031 case elfcpp::R_POWERPC_GOT_TLSGD16:
9032 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9033 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9034 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9035 {
9036 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
9037 if (!ppc_object->no_tls_marker())
9038 {
9039 bool final = gsym->final_value_is_known();
9040 tls_type = target->optimize_tls_gd(final);
9041 }
9042 if (tls_type == tls::TLSOPT_NONE)
9043 {
9044 Output_data_got_powerpc<size, big_endian>* got
9045 = target->got_section(symtab, layout);
9046 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9047 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
9048 elfcpp::R_POWERPC_DTPMOD,
9049 elfcpp::R_POWERPC_DTPREL);
9050 }
9051 else if (tls_type == tls::TLSOPT_TO_IE)
9052 {
9053 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
9054 {
9055 Output_data_got_powerpc<size, big_endian>* got
9056 = target->got_section(symtab, layout);
9057 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9058 if (gsym->is_undefined()
9059 || gsym->is_from_dynobj())
9060 {
9061 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
9062 elfcpp::R_POWERPC_TPREL);
9063 }
9064 else
9065 {
9066 unsigned int off = got->add_constant(0);
9067 gsym->set_got_offset(GOT_TYPE_TPREL, off);
9068 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9069 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9070 got, off, 0);
9071 }
9072 }
9073 ppc_object->set_tls_marker();
9074 }
9075 else if (tls_type == tls::TLSOPT_TO_LE)
9076 {
9077 // no GOT relocs needed for Local Exec.
9078 ppc_object->set_tls_marker();
9079 }
9080 else
9081 gold_unreachable();
9082 }
9083 break;
9084
9085 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9086 case elfcpp::R_POWERPC_GOT_TLSLD16:
9087 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9088 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9089 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9090 {
9091 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
9092 if (!ppc_object->no_tls_marker())
9093 tls_type = target->optimize_tls_ld();
9094 if (tls_type == tls::TLSOPT_NONE)
9095 target->tlsld_got_offset(symtab, layout, object);
9096 else if (tls_type == tls::TLSOPT_TO_LE)
9097 {
9098 // no GOT relocs needed for Local Exec.
9099 if (parameters->options().emit_relocs())
9100 {
9101 Output_section* os = layout->tls_segment()->first_section();
9102 gold_assert(os != NULL);
9103 os->set_needs_symtab_index();
9104 }
9105 ppc_object->set_tls_marker();
9106 }
9107 else
9108 gold_unreachable();
9109 }
9110 break;
9111
9112 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9113 case elfcpp::R_POWERPC_GOT_DTPREL16:
9114 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9115 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9116 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9117 {
9118 Output_data_got_powerpc<size, big_endian>* got
9119 = target->got_section(symtab, layout);
9120 if (!gsym->final_value_is_known()
9121 && (gsym->is_from_dynobj()
9122 || gsym->is_undefined()
9123 || gsym->is_preemptible()))
9124 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
9125 target->rela_dyn_section(layout),
9126 elfcpp::R_POWERPC_DTPREL);
9127 else
9128 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
9129 }
9130 break;
9131
9132 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9133 case elfcpp::R_POWERPC_GOT_TPREL16:
9134 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9135 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9136 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9137 {
9138 bool final = gsym->final_value_is_known();
9139 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9140 if (tls_type == tls::TLSOPT_NONE)
9141 {
9142 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
9143 {
9144 Output_data_got_powerpc<size, big_endian>* got
9145 = target->got_section(symtab, layout);
9146 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9147 if (gsym->is_undefined()
9148 || gsym->is_from_dynobj())
9149 {
9150 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
9151 elfcpp::R_POWERPC_TPREL);
9152 }
9153 else
9154 {
9155 unsigned int off = got->add_constant(0);
9156 gsym->set_got_offset(GOT_TYPE_TPREL, off);
9157 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9158 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9159 got, off, 0);
9160 }
9161 }
9162 }
9163 else if (tls_type == tls::TLSOPT_TO_LE)
9164 {
9165 // no GOT relocs needed for Local Exec.
9166 }
9167 else
9168 gold_unreachable();
9169 }
9170 break;
9171
9172 default:
9173 unsupported_reloc_global(object, r_type, gsym);
9174 break;
9175 }
9176
9177 if (size == 64
9178 && parameters->options().toc_optimize())
9179 {
9180 if (data_shndx == ppc_object->toc_shndx())
9181 {
9182 bool ok = true;
9183 if (r_type != elfcpp::R_PPC64_ADDR64
9184 || (is_ifunc && target->abiversion() < 2))
9185 ok = false;
9186 else if (parameters->options().output_is_position_independent()
9187 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9188 ok = false;
9189 if (!ok)
9190 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9191 }
9192
9193 enum {no_check, check_lo, check_ha} insn_check;
9194 switch (r_type)
9195 {
9196 default:
9197 insn_check = no_check;
9198 break;
9199
9200 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9201 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9202 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9203 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9204 case elfcpp::R_POWERPC_GOT16_HA:
9205 case elfcpp::R_PPC64_TOC16_HA:
9206 insn_check = check_ha;
9207 break;
9208
9209 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9210 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9211 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9212 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9213 case elfcpp::R_POWERPC_GOT16_LO:
9214 case elfcpp::R_PPC64_GOT16_LO_DS:
9215 case elfcpp::R_PPC64_TOC16_LO:
9216 case elfcpp::R_PPC64_TOC16_LO_DS:
9217 insn_check = check_lo;
9218 break;
9219 }
9220
9221 section_size_type slen;
9222 const unsigned char* view = NULL;
9223 if (insn_check != no_check)
9224 {
9225 view = ppc_object->section_contents(data_shndx, &slen, false);
9226 section_size_type off =
9227 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9228 if (off < slen)
9229 {
9230 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9231 if (insn_check == check_lo
9232 ? !ok_lo_toc_insn(insn, r_type)
9233 : ((insn & ((0x3f << 26) | 0x1f << 16))
9234 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9235 {
9236 ppc_object->set_no_toc_opt();
9237 gold_warning(_("%s: toc optimization is not supported "
9238 "for %#08x instruction"),
9239 ppc_object->name().c_str(), insn);
9240 }
9241 }
9242 }
9243
9244 switch (r_type)
9245 {
9246 default:
9247 break;
9248 case elfcpp::R_PPC64_TOC16:
9249 case elfcpp::R_PPC64_TOC16_LO:
9250 case elfcpp::R_PPC64_TOC16_HI:
9251 case elfcpp::R_PPC64_TOC16_HA:
9252 case elfcpp::R_PPC64_TOC16_DS:
9253 case elfcpp::R_PPC64_TOC16_LO_DS:
9254 if (gsym->source() == Symbol::FROM_OBJECT
9255 && !gsym->object()->is_dynamic())
9256 {
9257 Powerpc_relobj<size, big_endian>* sym_object
9258 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9259 bool is_ordinary;
9260 unsigned int shndx = gsym->shndx(&is_ordinary);
9261 if (shndx == sym_object->toc_shndx())
9262 {
9263 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9264 Address dst_off = sym->value() + reloc.get_r_addend();
9265 if (dst_off < sym_object->section_size(shndx))
9266 {
9267 bool ok = false;
9268 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9269 ok = true;
9270 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9271 {
9272 // Need to check that the insn is a ld
9273 if (!view)
9274 view = ppc_object->section_contents(data_shndx,
9275 &slen,
9276 false);
9277 section_size_type off =
9278 (convert_to_section_size_type(reloc.get_r_offset())
9279 + (big_endian ? -2 : 3));
9280 if (off < slen
9281 && (view[off] & (0x3f << 2)) == (58u << 2))
9282 ok = true;
9283 }
9284 if (!ok)
9285 sym_object->set_no_toc_opt(dst_off);
9286 }
9287 }
9288 }
9289 break;
9290 }
9291 }
9292
9293 if (size == 32)
9294 {
9295 switch (r_type)
9296 {
9297 case elfcpp::R_PPC_LOCAL24PC:
9298 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9299 gold_error(_("%s: unsupported -mbss-plt code"),
9300 ppc_object->name().c_str());
9301 break;
9302 default:
9303 break;
9304 }
9305 }
9306
9307 switch (r_type)
9308 {
9309 case elfcpp::R_POWERPC_GOT_TLSLD16:
9310 case elfcpp::R_POWERPC_GOT_TLSGD16:
9311 case elfcpp::R_POWERPC_GOT_TPREL16:
9312 case elfcpp::R_POWERPC_GOT_DTPREL16:
9313 case elfcpp::R_POWERPC_GOT16:
9314 case elfcpp::R_PPC64_GOT16_DS:
9315 case elfcpp::R_PPC64_TOC16:
9316 case elfcpp::R_PPC64_TOC16_DS:
9317 ppc_object->set_has_small_toc_reloc();
9318 break;
9319 default:
9320 break;
9321 }
9322
9323 switch (r_type)
9324 {
9325 case elfcpp::R_PPC64_TPREL16_DS:
9326 case elfcpp::R_PPC64_TPREL16_LO_DS:
9327 case elfcpp::R_PPC64_TPREL16_HIGH:
9328 case elfcpp::R_PPC64_TPREL16_HIGHA:
9329 case elfcpp::R_PPC64_TPREL16_HIGHER:
9330 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9331 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9332 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9333 case elfcpp::R_PPC64_TPREL34:
9334 if (size != 64)
9335 break;
9336 // Fall through.
9337 case elfcpp::R_POWERPC_TPREL16:
9338 case elfcpp::R_POWERPC_TPREL16_LO:
9339 case elfcpp::R_POWERPC_TPREL16_HI:
9340 case elfcpp::R_POWERPC_TPREL16_HA:
9341 layout->set_has_static_tls();
9342 break;
9343 default:
9344 break;
9345 }
9346
9347 switch (r_type)
9348 {
9349 case elfcpp::R_POWERPC_TPREL16_HA:
9350 if (target->tprel_opt())
9351 {
9352 section_size_type slen;
9353 const unsigned char* view = NULL;
9354 view = ppc_object->section_contents(data_shndx, &slen, false);
9355 section_size_type off
9356 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9357 if (off < slen)
9358 {
9359 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9360 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9361 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9362 target->set_no_tprel_opt();
9363 }
9364 }
9365 break;
9366
9367 case elfcpp::R_PPC64_TPREL16_HIGH:
9368 case elfcpp::R_PPC64_TPREL16_HIGHA:
9369 case elfcpp::R_PPC64_TPREL16_HIGHER:
9370 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9371 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9372 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9373 if (size != 64)
9374 break;
9375 // Fall through.
9376 case elfcpp::R_POWERPC_TPREL16_HI:
9377 target->set_no_tprel_opt();
9378 break;
9379 default:
9380 break;
9381 }
9382
9383 switch (r_type)
9384 {
9385 case elfcpp::R_PPC64_D34:
9386 case elfcpp::R_PPC64_D34_LO:
9387 case elfcpp::R_PPC64_D34_HI30:
9388 case elfcpp::R_PPC64_D34_HA30:
9389 case elfcpp::R_PPC64_D28:
9390 case elfcpp::R_PPC64_PCREL34:
9391 case elfcpp::R_PPC64_PCREL28:
9392 case elfcpp::R_PPC64_TPREL34:
9393 case elfcpp::R_PPC64_DTPREL34:
9394 case elfcpp::R_PPC64_PLT_PCREL34:
9395 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9396 case elfcpp::R_PPC64_GOT_PCREL34:
9397 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9398 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9399 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9400 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9401 target->set_power10_relocs();
9402 break;
9403 default:
9404 break;
9405 }
9406 }
9407
9408 // Process relocations for gc.
9409
9410 template<int size, bool big_endian>
9411 void
9412 Target_powerpc<size, big_endian>::gc_process_relocs(
9413 Symbol_table* symtab,
9414 Layout* layout,
9415 Sized_relobj_file<size, big_endian>* object,
9416 unsigned int data_shndx,
9417 unsigned int,
9418 const unsigned char* prelocs,
9419 size_t reloc_count,
9420 Output_section* output_section,
9421 bool needs_special_offset_handling,
9422 size_t local_symbol_count,
9423 const unsigned char* plocal_symbols)
9424 {
9425 typedef Target_powerpc<size, big_endian> Powerpc;
9426 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9427 Classify_reloc;
9428
9429 Powerpc_relobj<size, big_endian>* ppc_object
9430 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9431 if (size == 64)
9432 ppc_object->set_opd_valid();
9433 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9434 {
9435 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9436 for (p = ppc_object->access_from_map()->begin();
9437 p != ppc_object->access_from_map()->end();
9438 ++p)
9439 {
9440 Address dst_off = p->first;
9441 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9442 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9443 for (s = p->second.begin(); s != p->second.end(); ++s)
9444 {
9445 Relobj* src_obj = s->first;
9446 unsigned int src_indx = s->second;
9447 symtab->gc()->add_reference(src_obj, src_indx,
9448 ppc_object, dst_indx);
9449 }
9450 p->second.clear();
9451 }
9452 ppc_object->access_from_map()->clear();
9453 ppc_object->process_gc_mark(symtab);
9454 // Don't look at .opd relocs as .opd will reference everything.
9455 return;
9456 }
9457
9458 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9459 symtab,
9460 layout,
9461 this,
9462 object,
9463 data_shndx,
9464 prelocs,
9465 reloc_count,
9466 output_section,
9467 needs_special_offset_handling,
9468 local_symbol_count,
9469 plocal_symbols);
9470 }
9471
9472 // Handle target specific gc actions when adding a gc reference from
9473 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9474 // and DST_OFF. For powerpc64, this adds a referenc to the code
9475 // section of a function descriptor.
9476
9477 template<int size, bool big_endian>
9478 void
9479 Target_powerpc<size, big_endian>::do_gc_add_reference(
9480 Symbol_table* symtab,
9481 Relobj* src_obj,
9482 unsigned int src_shndx,
9483 Relobj* dst_obj,
9484 unsigned int dst_shndx,
9485 Address dst_off) const
9486 {
9487 if (size != 64 || dst_obj->is_dynamic())
9488 return;
9489
9490 Powerpc_relobj<size, big_endian>* ppc_object
9491 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9492 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9493 {
9494 if (ppc_object->opd_valid())
9495 {
9496 dst_shndx = ppc_object->get_opd_ent(dst_off);
9497 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9498 }
9499 else
9500 {
9501 // If we haven't run scan_opd_relocs, we must delay
9502 // processing this function descriptor reference.
9503 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9504 }
9505 }
9506 }
9507
9508 // Add any special sections for this symbol to the gc work list.
9509 // For powerpc64, this adds the code section of a function
9510 // descriptor.
9511
9512 template<int size, bool big_endian>
9513 void
9514 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9515 Symbol_table* symtab,
9516 Symbol* sym) const
9517 {
9518 if (size == 64 && sym->object()->pluginobj() == NULL)
9519 {
9520 Powerpc_relobj<size, big_endian>* ppc_object
9521 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9522 bool is_ordinary;
9523 unsigned int shndx = sym->shndx(&is_ordinary);
9524 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9525 {
9526 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9527 Address dst_off = gsym->value();
9528 if (ppc_object->opd_valid())
9529 {
9530 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9531 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9532 dst_indx));
9533 }
9534 else
9535 ppc_object->add_gc_mark(dst_off);
9536 }
9537 }
9538 }
9539
9540 // For a symbol location in .opd, set LOC to the location of the
9541 // function entry.
9542
9543 template<int size, bool big_endian>
9544 void
9545 Target_powerpc<size, big_endian>::do_function_location(
9546 Symbol_location* loc) const
9547 {
9548 if (size == 64 && loc->shndx != 0)
9549 {
9550 if (loc->object->is_dynamic())
9551 {
9552 Powerpc_dynobj<size, big_endian>* ppc_object
9553 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9554 if (loc->shndx == ppc_object->opd_shndx())
9555 {
9556 Address dest_off;
9557 Address off = loc->offset - ppc_object->opd_address();
9558 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9559 loc->offset = dest_off;
9560 }
9561 }
9562 else
9563 {
9564 const Powerpc_relobj<size, big_endian>* ppc_object
9565 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9566 if (loc->shndx == ppc_object->opd_shndx())
9567 {
9568 Address dest_off;
9569 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9570 loc->offset = dest_off;
9571 }
9572 }
9573 }
9574 }
9575
9576 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9577 // compiled with -fsplit-stack. The function calls non-split-stack
9578 // code. Change the function to ensure it has enough stack space to
9579 // call some random function.
9580
9581 template<int size, bool big_endian>
9582 void
9583 Target_powerpc<size, big_endian>::do_calls_non_split(
9584 Relobj* object,
9585 unsigned int shndx,
9586 section_offset_type fnoffset,
9587 section_size_type fnsize,
9588 const unsigned char* prelocs,
9589 size_t reloc_count,
9590 unsigned char* view,
9591 section_size_type view_size,
9592 std::string* from,
9593 std::string* to) const
9594 {
9595 // 32-bit not supported.
9596 if (size == 32)
9597 {
9598 // warn
9599 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9600 prelocs, reloc_count, view, view_size,
9601 from, to);
9602 return;
9603 }
9604
9605 // The function always starts with
9606 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9607 // addis %r12,%r1,-allocate@ha
9608 // addi %r12,%r12,-allocate@l
9609 // cmpld %r12,%r0
9610 // but note that the addis or addi may be replaced with a nop
9611
9612 unsigned char *entry = view + fnoffset;
9613 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9614
9615 if ((insn & 0xffff0000) == addis_2_12)
9616 {
9617 /* Skip ELFv2 global entry code. */
9618 entry += 8;
9619 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9620 }
9621
9622 unsigned char *pinsn = entry;
9623 bool ok = false;
9624 const uint32_t ld_private_ss = 0xe80d8fc0;
9625 if (insn == ld_private_ss)
9626 {
9627 int32_t allocate = 0;
9628 while (1)
9629 {
9630 pinsn += 4;
9631 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9632 if ((insn & 0xffff0000) == addis_12_1)
9633 allocate += (insn & 0xffff) << 16;
9634 else if ((insn & 0xffff0000) == addi_12_1
9635 || (insn & 0xffff0000) == addi_12_12)
9636 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9637 else if (insn != nop)
9638 break;
9639 }
9640 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9641 {
9642 int extra = parameters->options().split_stack_adjust_size();
9643 allocate -= extra;
9644 if (allocate >= 0 || extra < 0)
9645 {
9646 object->error(_("split-stack stack size overflow at "
9647 "section %u offset %0zx"),
9648 shndx, static_cast<size_t>(fnoffset));
9649 return;
9650 }
9651 pinsn = entry + 4;
9652 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9653 if (insn != addis_12_1)
9654 {
9655 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9656 pinsn += 4;
9657 insn = addi_12_12 | (allocate & 0xffff);
9658 if (insn != addi_12_12)
9659 {
9660 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9661 pinsn += 4;
9662 }
9663 }
9664 else
9665 {
9666 insn = addi_12_1 | (allocate & 0xffff);
9667 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9668 pinsn += 4;
9669 }
9670 if (pinsn != entry + 12)
9671 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9672
9673 ok = true;
9674 }
9675 }
9676
9677 if (!ok)
9678 {
9679 if (!object->has_no_split_stack())
9680 object->error(_("failed to match split-stack sequence at "
9681 "section %u offset %0zx"),
9682 shndx, static_cast<size_t>(fnoffset));
9683 }
9684 }
9685
9686 // Scan relocations for a section.
9687
9688 template<int size, bool big_endian>
9689 void
9690 Target_powerpc<size, big_endian>::scan_relocs(
9691 Symbol_table* symtab,
9692 Layout* layout,
9693 Sized_relobj_file<size, big_endian>* object,
9694 unsigned int data_shndx,
9695 unsigned int sh_type,
9696 const unsigned char* prelocs,
9697 size_t reloc_count,
9698 Output_section* output_section,
9699 bool needs_special_offset_handling,
9700 size_t local_symbol_count,
9701 const unsigned char* plocal_symbols)
9702 {
9703 typedef Target_powerpc<size, big_endian> Powerpc;
9704 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9705 Classify_reloc;
9706
9707 if (!this->plt_localentry0_init_)
9708 {
9709 bool plt_localentry0 = false;
9710 if (size == 64
9711 && this->abiversion() >= 2)
9712 {
9713 if (parameters->options().user_set_plt_localentry())
9714 plt_localentry0 = parameters->options().plt_localentry();
9715 if (plt_localentry0
9716 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9717 gold_warning(_("--plt-localentry is especially dangerous without "
9718 "ld.so support to detect ABI violations"));
9719 }
9720 this->plt_localentry0_ = plt_localentry0;
9721 this->plt_localentry0_init_ = true;
9722 }
9723
9724 if (sh_type == elfcpp::SHT_REL)
9725 {
9726 gold_error(_("%s: unsupported REL reloc section"),
9727 object->name().c_str());
9728 return;
9729 }
9730
9731 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9732 symtab,
9733 layout,
9734 this,
9735 object,
9736 data_shndx,
9737 prelocs,
9738 reloc_count,
9739 output_section,
9740 needs_special_offset_handling,
9741 local_symbol_count,
9742 plocal_symbols);
9743
9744 if (this->plt_localentry0_ && this->power10_relocs_)
9745 {
9746 gold_warning(_("--plt-localentry is incompatible with "
9747 "power10 pc-relative code"));
9748 this->plt_localentry0_ = false;
9749 }
9750 }
9751
9752 // Functor class for processing the global symbol table.
9753 // Removes symbols defined on discarded opd entries.
9754
9755 template<bool big_endian>
9756 class Global_symbol_visitor_opd
9757 {
9758 public:
9759 Global_symbol_visitor_opd()
9760 { }
9761
9762 void
9763 operator()(Sized_symbol<64>* sym)
9764 {
9765 if (sym->has_symtab_index()
9766 || sym->source() != Symbol::FROM_OBJECT
9767 || !sym->in_real_elf())
9768 return;
9769
9770 if (sym->object()->is_dynamic())
9771 return;
9772
9773 Powerpc_relobj<64, big_endian>* symobj
9774 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9775 if (symobj->opd_shndx() == 0)
9776 return;
9777
9778 bool is_ordinary;
9779 unsigned int shndx = sym->shndx(&is_ordinary);
9780 if (shndx == symobj->opd_shndx()
9781 && symobj->get_opd_discard(sym->value()))
9782 {
9783 sym->set_undefined();
9784 sym->set_visibility(elfcpp::STV_DEFAULT);
9785 sym->set_is_defined_in_discarded_section();
9786 sym->set_symtab_index(-1U);
9787 }
9788 }
9789 };
9790
9791 template<int size, bool big_endian>
9792 void
9793 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9794 Layout* layout,
9795 Symbol_table* symtab)
9796 {
9797 if (size == 64)
9798 {
9799 Output_data_save_res<size, big_endian>* savres
9800 = new Output_data_save_res<size, big_endian>(symtab);
9801 this->savres_section_ = savres;
9802 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9803 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9804 savres, ORDER_TEXT, false);
9805 }
9806 }
9807
9808 // Sort linker created .got section first (for the header), then input
9809 // sections belonging to files using small model code.
9810
9811 template<bool big_endian>
9812 class Sort_toc_sections
9813 {
9814 public:
9815 bool
9816 operator()(const Output_section::Input_section& is1,
9817 const Output_section::Input_section& is2) const
9818 {
9819 if (!is1.is_input_section() && is2.is_input_section())
9820 return true;
9821 bool small1
9822 = (is1.is_input_section()
9823 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9824 ->has_small_toc_reloc()));
9825 bool small2
9826 = (is2.is_input_section()
9827 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9828 ->has_small_toc_reloc()));
9829 return small1 && !small2;
9830 }
9831 };
9832
9833 // Finalize the sections.
9834
9835 template<int size, bool big_endian>
9836 void
9837 Target_powerpc<size, big_endian>::do_finalize_sections(
9838 Layout* layout,
9839 const Input_objects* input_objects,
9840 Symbol_table* symtab)
9841 {
9842 if (parameters->doing_static_link())
9843 {
9844 // At least some versions of glibc elf-init.o have a strong
9845 // reference to __rela_iplt marker syms. A weak ref would be
9846 // better..
9847 if (this->iplt_ != NULL)
9848 {
9849 Reloc_section* rel = this->iplt_->rel_plt();
9850 symtab->define_in_output_data("__rela_iplt_start", NULL,
9851 Symbol_table::PREDEFINED, rel, 0, 0,
9852 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9853 elfcpp::STV_HIDDEN, 0, false, true);
9854 symtab->define_in_output_data("__rela_iplt_end", NULL,
9855 Symbol_table::PREDEFINED, rel, 0, 0,
9856 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9857 elfcpp::STV_HIDDEN, 0, true, true);
9858 }
9859 else
9860 {
9861 symtab->define_as_constant("__rela_iplt_start", NULL,
9862 Symbol_table::PREDEFINED, 0, 0,
9863 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9864 elfcpp::STV_HIDDEN, 0, true, false);
9865 symtab->define_as_constant("__rela_iplt_end", NULL,
9866 Symbol_table::PREDEFINED, 0, 0,
9867 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9868 elfcpp::STV_HIDDEN, 0, true, false);
9869 }
9870 }
9871
9872 if (size == 64)
9873 {
9874 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9875 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
9876
9877 if (!parameters->options().relocatable())
9878 {
9879 this->define_save_restore_funcs(layout, symtab);
9880
9881 // Annoyingly, we need to make these sections now whether or
9882 // not we need them. If we delay until do_relax then we
9883 // need to mess with the relaxation machinery checkpointing.
9884 this->got_section(symtab, layout);
9885 this->make_brlt_section(layout);
9886
9887 if (parameters->options().toc_sort())
9888 {
9889 Output_section* os = this->got_->output_section();
9890 if (os != NULL && os->input_sections().size() > 1)
9891 std::stable_sort(os->input_sections().begin(),
9892 os->input_sections().end(),
9893 Sort_toc_sections<big_endian>());
9894 }
9895 }
9896 }
9897
9898 // Fill in some more dynamic tags.
9899 Output_data_dynamic* odyn = layout->dynamic_data();
9900 if (odyn != NULL)
9901 {
9902 const Reloc_section* rel_plt = (this->plt_ == NULL
9903 ? NULL
9904 : this->plt_->rel_plt());
9905 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9906 this->rela_dyn_, true, size == 32);
9907
9908 if (size == 32)
9909 {
9910 if (this->got_ != NULL)
9911 {
9912 this->got_->finalize_data_size();
9913 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9914 this->got_, this->got_->g_o_t());
9915 }
9916 if (this->has_tls_get_addr_opt_)
9917 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
9918 }
9919 else
9920 {
9921 if (this->glink_ != NULL)
9922 {
9923 this->glink_->finalize_data_size();
9924 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9925 this->glink_,
9926 (this->glink_->pltresolve_size()
9927 - 32));
9928 }
9929 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9930 odyn->add_constant(elfcpp::DT_PPC64_OPT,
9931 ((this->has_localentry0_
9932 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9933 | (this->has_tls_get_addr_opt_
9934 ? elfcpp::PPC64_OPT_TLS : 0)));
9935 }
9936 }
9937
9938 // Emit any relocs we saved in an attempt to avoid generating COPY
9939 // relocs.
9940 if (this->copy_relocs_.any_saved_relocs())
9941 this->copy_relocs_.emit(this->rela_dyn_section(layout));
9942
9943 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9944 p != input_objects->relobj_end();
9945 ++p)
9946 {
9947 Powerpc_relobj<size, big_endian>* ppc_relobj
9948 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9949 if (ppc_relobj->attributes_section_data())
9950 this->merge_object_attributes(ppc_relobj,
9951 ppc_relobj->attributes_section_data());
9952 }
9953 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9954 p != input_objects->dynobj_end();
9955 ++p)
9956 {
9957 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9958 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9959 if (ppc_dynobj->attributes_section_data())
9960 this->merge_object_attributes(ppc_dynobj,
9961 ppc_dynobj->attributes_section_data());
9962 }
9963
9964 // Create a .gnu.attributes section if we have merged any attributes
9965 // from inputs.
9966 if (this->attributes_section_data_ != NULL
9967 && this->attributes_section_data_->size() != 0)
9968 {
9969 Output_attributes_section_data* attributes_section
9970 = new Output_attributes_section_data(*this->attributes_section_data_);
9971 layout->add_output_section_data(".gnu.attributes",
9972 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9973 attributes_section, ORDER_INVALID, false);
9974 }
9975 }
9976
9977 // Merge object attributes from input file called NAME with those of the
9978 // output. The input object attributes are in the object pointed by PASD.
9979
9980 template<int size, bool big_endian>
9981 void
9982 Target_powerpc<size, big_endian>::merge_object_attributes(
9983 const Object* obj,
9984 const Attributes_section_data* pasd)
9985 {
9986 // Return if there is no attributes section data.
9987 if (pasd == NULL)
9988 return;
9989
9990 // Create output object attributes.
9991 if (this->attributes_section_data_ == NULL)
9992 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9993
9994 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9995 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9996 Object_attribute* out_attr
9997 = this->attributes_section_data_->known_attributes(vendor);
9998
9999 const char* name = obj->name().c_str();
10000 const char* err;
10001 const char* first;
10002 const char* second;
10003 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10004 int in_fp = in_attr[tag].int_value() & 0xf;
10005 int out_fp = out_attr[tag].int_value() & 0xf;
10006 bool warn_only = obj->is_dynamic();
10007 if (in_fp != out_fp)
10008 {
10009 err = NULL;
10010 if ((in_fp & 3) == 0)
10011 ;
10012 else if ((out_fp & 3) == 0)
10013 {
10014 if (!warn_only)
10015 {
10016 out_fp |= in_fp & 3;
10017 out_attr[tag].set_int_value(out_fp);
10018 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10019 this->last_fp_ = name;
10020 }
10021 }
10022 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10023 {
10024 err = N_("%s uses hard float, %s uses soft float");
10025 first = this->last_fp_;
10026 second = name;
10027 }
10028 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10029 {
10030 err = N_("%s uses hard float, %s uses soft float");
10031 first = name;
10032 second = this->last_fp_;
10033 }
10034 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10035 {
10036 err = N_("%s uses double-precision hard float, "
10037 "%s uses single-precision hard float");
10038 first = this->last_fp_;
10039 second = name;
10040 }
10041 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10042 {
10043 err = N_("%s uses double-precision hard float, "
10044 "%s uses single-precision hard float");
10045 first = name;
10046 second = this->last_fp_;
10047 }
10048
10049 if (err || (in_fp & 0xc) == 0)
10050 ;
10051 else if ((out_fp & 0xc) == 0)
10052 {
10053 if (!warn_only)
10054 {
10055 out_fp |= in_fp & 0xc;
10056 out_attr[tag].set_int_value(out_fp);
10057 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10058 this->last_ld_ = name;
10059 }
10060 }
10061 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10062 {
10063 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10064 first = name;
10065 second = this->last_ld_;
10066 }
10067 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10068 {
10069 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10070 first = this->last_ld_;
10071 second = name;
10072 }
10073 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10074 {
10075 err = N_("%s uses IBM long double, %s uses IEEE long double");
10076 first = this->last_ld_;
10077 second = name;
10078 }
10079 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10080 {
10081 err = N_("%s uses IBM long double, %s uses IEEE long double");
10082 first = name;
10083 second = this->last_ld_;
10084 }
10085
10086 if (err)
10087 {
10088 if (parameters->options().warn_mismatch())
10089 {
10090 if (warn_only)
10091 gold_warning(_(err), first, second);
10092 else
10093 gold_error(_(err), first, second);
10094 }
10095 // Arrange for this attribute to be deleted. It's better to
10096 // say "don't know" about a file than to wrongly claim compliance.
10097 if (!warn_only)
10098 out_attr[tag].set_type(0);
10099 }
10100 }
10101
10102 if (size == 32)
10103 {
10104 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10105 int in_vec = in_attr[tag].int_value() & 3;
10106 int out_vec = out_attr[tag].int_value() & 3;
10107 if (in_vec != out_vec)
10108 {
10109 err = NULL;
10110 if (in_vec == 0)
10111 ;
10112 else if (out_vec == 0)
10113 {
10114 out_vec = in_vec;
10115 out_attr[tag].set_int_value(out_vec);
10116 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10117 this->last_vec_ = name;
10118 }
10119 // For now, allow generic to transition to AltiVec or SPE
10120 // without a warning. If GCC marked files with their stack
10121 // alignment and used don't-care markings for files which are
10122 // not affected by the vector ABI, we could warn about this
10123 // case too. */
10124 else if (in_vec == 1)
10125 ;
10126 else if (out_vec == 1)
10127 {
10128 out_vec = in_vec;
10129 out_attr[tag].set_int_value(out_vec);
10130 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10131 this->last_vec_ = name;
10132 }
10133 else if (out_vec < in_vec)
10134 {
10135 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10136 first = this->last_vec_;
10137 second = name;
10138 }
10139 else if (out_vec > in_vec)
10140 {
10141 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10142 first = name;
10143 second = this->last_vec_;
10144 }
10145 if (err)
10146 {
10147 if (parameters->options().warn_mismatch())
10148 gold_error(_(err), first, second);
10149 out_attr[tag].set_type(0);
10150 }
10151 }
10152
10153 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10154 int in_struct = in_attr[tag].int_value() & 3;
10155 int out_struct = out_attr[tag].int_value() & 3;
10156 if (in_struct != out_struct)
10157 {
10158 err = NULL;
10159 if (in_struct == 0 || in_struct == 3)
10160 ;
10161 else if (out_struct == 0)
10162 {
10163 out_struct = in_struct;
10164 out_attr[tag].set_int_value(out_struct);
10165 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10166 this->last_struct_ = name;
10167 }
10168 else if (out_struct < in_struct)
10169 {
10170 err = N_("%s uses r3/r4 for small structure returns, "
10171 "%s uses memory");
10172 first = this->last_struct_;
10173 second = name;
10174 }
10175 else if (out_struct > in_struct)
10176 {
10177 err = N_("%s uses r3/r4 for small structure returns, "
10178 "%s uses memory");
10179 first = name;
10180 second = this->last_struct_;
10181 }
10182 if (err)
10183 {
10184 if (parameters->options().warn_mismatch())
10185 gold_error(_(err), first, second);
10186 out_attr[tag].set_type(0);
10187 }
10188 }
10189 }
10190
10191 // Merge Tag_compatibility attributes and any common GNU ones.
10192 this->attributes_section_data_->merge(name, pasd);
10193 }
10194
10195 // Emit any saved relocs, and mark toc entries using any of these
10196 // relocs as not optimizable.
10197
10198 template<int sh_type, int size, bool big_endian>
10199 void
10200 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10201 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10202 {
10203 if (size == 64
10204 && parameters->options().toc_optimize())
10205 {
10206 for (typename Copy_relocs<sh_type, size, big_endian>::
10207 Copy_reloc_entries::iterator p = this->entries_.begin();
10208 p != this->entries_.end();
10209 ++p)
10210 {
10211 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10212 entry = *p;
10213
10214 // If the symbol is no longer defined in a dynamic object,
10215 // then we emitted a COPY relocation. If it is still
10216 // dynamic then we'll need dynamic relocations and thus
10217 // can't optimize toc entries.
10218 if (entry.sym_->is_from_dynobj())
10219 {
10220 Powerpc_relobj<size, big_endian>* ppc_object
10221 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10222 if (entry.shndx_ == ppc_object->toc_shndx())
10223 ppc_object->set_no_toc_opt(entry.address_);
10224 }
10225 }
10226 }
10227
10228 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10229 }
10230
10231 // Return the value to use for a branch relocation.
10232
10233 template<int size, bool big_endian>
10234 bool
10235 Target_powerpc<size, big_endian>::symval_for_branch(
10236 const Symbol_table* symtab,
10237 const Sized_symbol<size>* gsym,
10238 Powerpc_relobj<size, big_endian>* object,
10239 Address *value,
10240 unsigned int *dest_shndx)
10241 {
10242 if (size == 32 || this->abiversion() >= 2)
10243 gold_unreachable();
10244 *dest_shndx = 0;
10245
10246 // If the symbol is defined in an opd section, ie. is a function
10247 // descriptor, use the function descriptor code entry address
10248 Powerpc_relobj<size, big_endian>* symobj = object;
10249 if (gsym != NULL
10250 && (gsym->source() != Symbol::FROM_OBJECT
10251 || gsym->object()->is_dynamic()))
10252 return true;
10253 if (gsym != NULL)
10254 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10255 unsigned int shndx = symobj->opd_shndx();
10256 if (shndx == 0)
10257 return true;
10258 Address opd_addr = symobj->get_output_section_offset(shndx);
10259 if (opd_addr == invalid_address)
10260 return true;
10261 opd_addr += symobj->output_section_address(shndx);
10262 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10263 {
10264 Address sec_off;
10265 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10266 if (symtab->is_section_folded(symobj, *dest_shndx))
10267 {
10268 Section_id folded
10269 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10270 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10271 *dest_shndx = folded.second;
10272 }
10273 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10274 if (sec_addr == invalid_address)
10275 return false;
10276
10277 sec_addr += symobj->output_section(*dest_shndx)->address();
10278 *value = sec_addr + sec_off;
10279 }
10280 return true;
10281 }
10282
10283 template<int size>
10284 static bool
10285 relative_value_is_known(const Sized_symbol<size>* gsym)
10286 {
10287 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10288 return false;
10289
10290 if (gsym->is_from_dynobj()
10291 || gsym->is_undefined()
10292 || gsym->is_preemptible())
10293 return false;
10294
10295 if (gsym->is_absolute())
10296 return !parameters->options().output_is_position_independent();
10297
10298 return true;
10299 }
10300
10301 template<int size>
10302 static bool
10303 relative_value_is_known(const Symbol_value<size>* psymval)
10304 {
10305 if (psymval->is_ifunc_symbol())
10306 return false;
10307
10308 bool is_ordinary;
10309 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10310
10311 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10312 }
10313
10314 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10315 // pld ra,symbol@got@pcrel
10316 // load/store rt,0(ra)
10317 // or
10318 // pla ra,symbol@pcrel
10319 // load/store rt,0(ra)
10320 // may be translated to
10321 // pload/pstore rt,symbol@pcrel
10322 // nop.
10323 // This function returns true if the optimization is possible, placing
10324 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10325 //
10326 // On entry to this function, the linker has already determined that
10327 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10328 // while *PINSN2 is the second instruction.
10329
10330 inline bool
10331 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10332 {
10333 uint32_t insn2 = *pinsn2 >> 32;
10334 uint64_t i1new;
10335
10336 // Check that regs match.
10337 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10338 return false;
10339
10340 switch ((insn2 >> 26) & 63)
10341 {
10342 default:
10343 return false;
10344
10345 case 32: // lwz
10346 case 34: // lbz
10347 case 36: // stw
10348 case 38: // stb
10349 case 40: // lhz
10350 case 42: // lha
10351 case 44: // sth
10352 case 48: // lfs
10353 case 50: // lfd
10354 case 52: // stfs
10355 case 54: // stfd
10356 // These are the PMLS cases, where we just need to tack a prefix
10357 // on the insn. Check that the D field is zero.
10358 if ((insn2 & 0xffff) != 0)
10359 return false;
10360 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10361 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10362 break;
10363
10364 case 58: // lwa, ld
10365 if ((insn2 & 0xfffd) != 0)
10366 return false;
10367 i1new = ((1ULL << 58) | (1ULL << 52)
10368 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10369 | (insn2 & (31ULL << 21)));
10370 break;
10371
10372 case 57: // lxsd, lxssp
10373 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10374 return false;
10375 i1new = ((1ULL << 58) | (1ULL << 52)
10376 | ((40ULL | (insn2 & 3)) << 26)
10377 | (insn2 & (31ULL << 21)));
10378 break;
10379
10380 case 61: // stxsd, stxssp, lxv, stxv
10381 if ((insn2 & 3) == 0)
10382 return false;
10383 else if ((insn2 & 3) >= 2)
10384 {
10385 if ((insn2 & 0xfffc) != 0)
10386 return false;
10387 i1new = ((1ULL << 58) | (1ULL << 52)
10388 | ((44ULL | (insn2 & 3)) << 26)
10389 | (insn2 & (31ULL << 21)));
10390 }
10391 else
10392 {
10393 if ((insn2 & 0xfff0) != 0)
10394 return false;
10395 i1new = ((1ULL << 58) | (1ULL << 52)
10396 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10397 | (insn2 & (31ULL << 21)));
10398 }
10399 break;
10400
10401 case 56: // lq
10402 if ((insn2 & 0xffff) != 0)
10403 return false;
10404 i1new = ((1ULL << 58) | (1ULL << 52)
10405 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10406 break;
10407
10408 case 62: // std, stq
10409 if ((insn2 & 0xfffd) != 0)
10410 return false;
10411 i1new = ((1ULL << 58) | (1ULL << 52)
10412 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10413 | (insn2 & (31ULL << 21)));
10414 break;
10415 }
10416
10417 *pinsn1 = i1new;
10418 *pinsn2 = (uint64_t) nop << 32;
10419 return true;
10420 }
10421
10422 // Perform a relocation.
10423
10424 template<int size, bool big_endian>
10425 inline bool
10426 Target_powerpc<size, big_endian>::Relocate::relocate(
10427 const Relocate_info<size, big_endian>* relinfo,
10428 unsigned int,
10429 Target_powerpc* target,
10430 Output_section* os,
10431 size_t relnum,
10432 const unsigned char* preloc,
10433 const Sized_symbol<size>* gsym,
10434 const Symbol_value<size>* psymval,
10435 unsigned char* view,
10436 Address address,
10437 section_size_type view_size)
10438 {
10439 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10440 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10441 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10442
10443 if (view == NULL)
10444 return true;
10445
10446 if (target->replace_tls_get_addr(gsym))
10447 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10448
10449 const elfcpp::Rela<size, big_endian> rela(preloc);
10450 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10451 Powerpc_relobj<size, big_endian>* const object
10452 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10453 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10454 {
10455 case Track_tls::NOT_EXPECTED:
10456 if (!parameters->options().shared()
10457 && parameters->options().tls_optimize())
10458 {
10459 // It is a hard error to see a __tls_get_addr call without
10460 // marker relocs after seeing calls with marker relocs in the
10461 // same object file, because dynamic relocation accounting
10462 // will be wrong.
10463 if (object->tls_opt_error())
10464 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10465 _("__tls_get_addr call lacks marker reloc"));
10466 else
10467 gold_warning_at_location(relinfo, relnum, rela.get_r_offset(),
10468 _("__tls_get_addr call lacks marker reloc"));
10469 }
10470 break;
10471 case Track_tls::EXPECTED:
10472 // We have already complained.
10473 break;
10474 case Track_tls::SKIP:
10475 if (is_plt16_reloc<size>(r_type)
10476 || r_type == elfcpp::R_POWERPC_PLTSEQ
10477 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10478 {
10479 Insn* iview = reinterpret_cast<Insn*>(view);
10480 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10481 }
10482 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10483 {
10484 Insn* iview = reinterpret_cast<Insn*>(view);
10485 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10486 }
10487 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10488 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10489 {
10490 Insn* iview = reinterpret_cast<Insn*>(view);
10491 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10492 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10493 }
10494 return true;
10495 case Track_tls::NORMAL:
10496 break;
10497 }
10498
10499 // Offset from start of insn to d-field reloc.
10500 const int d_offset = big_endian ? 2 : 0;
10501
10502 Address value = 0;
10503 bool has_stub_value = false;
10504 bool localentry0 = false;
10505 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10506 bool has_plt_offset
10507 = (gsym != NULL
10508 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10509 : object->local_has_plt_offset(r_sym));
10510 if (has_plt_offset
10511 && !is_got_reloc(r_type)
10512 && !is_plt16_reloc<size>(r_type)
10513 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10514 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10515 && r_type != elfcpp::R_POWERPC_PLTSEQ
10516 && r_type != elfcpp::R_POWERPC_PLTCALL
10517 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10518 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
10519 && (!psymval->is_ifunc_symbol()
10520 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
10521 {
10522 if (size == 64
10523 && gsym != NULL
10524 && target->abiversion() >= 2
10525 && !parameters->options().output_is_position_independent()
10526 && !is_branch_reloc<size>(r_type))
10527 {
10528 Address off = target->glink_section()->find_global_entry(gsym);
10529 if (off != invalid_address)
10530 {
10531 value = target->glink_section()->global_entry_address() + off;
10532 has_stub_value = true;
10533 }
10534 }
10535 else
10536 {
10537 Stub_table<size, big_endian>* stub_table = NULL;
10538 if (target->stub_tables().size() == 1)
10539 stub_table = target->stub_tables()[0];
10540 if (stub_table == NULL
10541 && !(size == 32
10542 && gsym != NULL
10543 && !parameters->options().output_is_position_independent()
10544 && !is_branch_reloc<size>(r_type)))
10545 stub_table = object->stub_table(relinfo->data_shndx);
10546 if (stub_table == NULL)
10547 {
10548 // This is a ref from a data section to an ifunc symbol,
10549 // or a non-branch reloc for which we always want to use
10550 // one set of stubs for resolving function addresses.
10551 if (target->stub_tables().size() != 0)
10552 stub_table = target->stub_tables()[0];
10553 }
10554 if (stub_table != NULL)
10555 {
10556 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10557 if (gsym != NULL)
10558 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10559 rela.get_r_addend());
10560 else
10561 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10562 rela.get_r_addend());
10563 if (ent != NULL)
10564 {
10565 value = stub_table->stub_address() + ent->off_;
10566 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10567 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10568 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10569 if (size == 64
10570 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
10571 value += ent->tocoff_;
10572 if (size == 64
10573 && ent->r2save_
10574 && !(gsym != NULL
10575 && target->is_tls_get_addr_opt(gsym)))
10576 {
10577 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10578 {
10579 if (!(target->power10_stubs()
10580 && target->power10_stubs_auto()))
10581 value += 4;
10582 }
10583 else if (relnum < reloc_count - 1)
10584 {
10585 Reltype next_rela(preloc + reloc_size);
10586 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10587 == elfcpp::R_PPC64_TOCSAVE
10588 && (next_rela.get_r_offset()
10589 == rela.get_r_offset() + 4))
10590 value += 4;
10591 }
10592 }
10593 localentry0 = ent->localentry0_;
10594 has_stub_value = true;
10595 }
10596 }
10597 }
10598 // We don't care too much about bogus debug references to
10599 // non-local functions, but otherwise there had better be a plt
10600 // call stub or global entry stub as appropriate.
10601 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10602 }
10603
10604 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10605 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10606 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10607 {
10608 const Output_data_plt_powerpc<size, big_endian>* plt;
10609 if (gsym)
10610 value = target->plt_off(gsym, &plt);
10611 else
10612 value = target->plt_off(object, r_sym, &plt);
10613 value += plt->address();
10614
10615 if (size == 64)
10616 {
10617 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10618 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10619 value -= (target->got_section()->output_section()->address()
10620 + object->toc_base_offset());
10621 }
10622 else if (parameters->options().output_is_position_independent())
10623 {
10624 if (rela.get_r_addend() >= 32768)
10625 {
10626 unsigned int got2 = object->got2_shndx();
10627 value -= (object->get_output_section_offset(got2)
10628 + object->output_section(got2)->address()
10629 + rela.get_r_addend());
10630 }
10631 else
10632 value -= (target->got_section()->address()
10633 + target->got_section()->g_o_t());
10634 }
10635 }
10636 else if (!has_plt_offset
10637 && (is_plt16_reloc<size>(r_type)
10638 || r_type == elfcpp::R_POWERPC_PLTSEQ
10639 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10640 {
10641 Insn* iview = reinterpret_cast<Insn*>(view);
10642 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10643 r_type = elfcpp::R_POWERPC_NONE;
10644 }
10645 else if (!has_plt_offset
10646 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10647 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10648 {
10649 Insn* iview = reinterpret_cast<Insn*>(view);
10650 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10651 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10652 r_type = elfcpp::R_POWERPC_NONE;
10653 }
10654 else if (is_got_reloc(r_type))
10655 {
10656 if (gsym != NULL)
10657 {
10658 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10659 value = gsym->got_offset(GOT_TYPE_STANDARD);
10660 }
10661 else
10662 {
10663 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10664 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10665 }
10666 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10667 value += target->got_section()->address();
10668 else
10669 value -= target->got_section()->got_base_offset(object);
10670 }
10671 else if (r_type == elfcpp::R_PPC64_TOC)
10672 {
10673 value = (target->got_section()->output_section()->address()
10674 + object->toc_base_offset());
10675 }
10676 else if (gsym != NULL
10677 && (r_type == elfcpp::R_POWERPC_REL24
10678 || r_type == elfcpp::R_PPC_PLTREL24)
10679 && has_stub_value)
10680 {
10681 if (size == 64)
10682 {
10683 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10684 Valtype* wv = reinterpret_cast<Valtype*>(view);
10685 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10686 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10687 {
10688 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10689 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10690 if ((insn & 1) != 0
10691 && (insn2 == nop
10692 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10693 {
10694 elfcpp::Swap<32, big_endian>::
10695 writeval(wv + 1, ld_2_1 + target->stk_toc());
10696 can_plt_call = true;
10697 }
10698 }
10699 if (!can_plt_call)
10700 {
10701 // If we don't have a branch and link followed by a nop,
10702 // we can't go via the plt because there is no place to
10703 // put a toc restoring instruction.
10704 // Unless we know we won't be returning.
10705 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10706 can_plt_call = true;
10707 }
10708 if (!can_plt_call)
10709 {
10710 // g++ as of 20130507 emits self-calls without a
10711 // following nop. This is arguably wrong since we have
10712 // conflicting information. On the one hand a global
10713 // symbol and on the other a local call sequence, but
10714 // don't error for this special case.
10715 // It isn't possible to cheaply verify we have exactly
10716 // such a call. Allow all calls to the same section.
10717 bool ok = false;
10718 Address code = value;
10719 if (gsym->source() == Symbol::FROM_OBJECT
10720 && gsym->object() == object)
10721 {
10722 unsigned int dest_shndx = 0;
10723 if (target->abiversion() < 2)
10724 {
10725 Address addend = rela.get_r_addend();
10726 code = psymval->value(object, addend);
10727 target->symval_for_branch(relinfo->symtab, gsym, object,
10728 &code, &dest_shndx);
10729 }
10730 bool is_ordinary;
10731 if (dest_shndx == 0)
10732 dest_shndx = gsym->shndx(&is_ordinary);
10733 ok = dest_shndx == relinfo->data_shndx;
10734 }
10735 if (!ok)
10736 {
10737 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10738 _("call lacks nop, can't restore toc; "
10739 "recompile with -fPIC"));
10740 value = code;
10741 }
10742 }
10743 }
10744 }
10745 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10746 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10747 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10748 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10749 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10750 {
10751 // First instruction of a global dynamic sequence, arg setup insn.
10752 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
10753 if (!object->no_tls_marker())
10754 {
10755 bool final = gsym == NULL || gsym->final_value_is_known();
10756 tls_type = target->optimize_tls_gd(final);
10757 }
10758 enum Got_type got_type = GOT_TYPE_STANDARD;
10759 if (tls_type == tls::TLSOPT_NONE)
10760 got_type = GOT_TYPE_TLSGD;
10761 else if (tls_type == tls::TLSOPT_TO_IE)
10762 got_type = GOT_TYPE_TPREL;
10763 if (got_type != GOT_TYPE_STANDARD)
10764 {
10765 if (gsym != NULL)
10766 {
10767 gold_assert(gsym->has_got_offset(got_type));
10768 value = gsym->got_offset(got_type);
10769 }
10770 else
10771 {
10772 gold_assert(object->local_has_got_offset(r_sym, got_type));
10773 value = object->local_got_offset(r_sym, got_type);
10774 }
10775 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10776 value += target->got_section()->address();
10777 else
10778 value -= target->got_section()->got_base_offset(object);
10779 }
10780 if (tls_type == tls::TLSOPT_TO_IE)
10781 {
10782 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10783 {
10784 Insn* iview = reinterpret_cast<Insn*>(view);
10785 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10786 pinsn <<= 32;
10787 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10788 // pla -> pld
10789 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10790 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10791 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10792 pinsn & 0xffffffff);
10793 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
10794 }
10795 else
10796 {
10797 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10798 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10799 {
10800 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10801 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10802 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10803 if (size == 32)
10804 insn |= 32 << 26; // lwz
10805 else
10806 insn |= 58 << 26; // ld
10807 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10808 }
10809 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10810 - elfcpp::R_POWERPC_GOT_TLSGD16);
10811 }
10812 }
10813 else if (tls_type == tls::TLSOPT_TO_LE)
10814 {
10815 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10816 {
10817 Insn* iview = reinterpret_cast<Insn*>(view);
10818 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10819 pinsn <<= 32;
10820 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10821 // pla pcrel -> paddi r13
10822 pinsn += (-1ULL << 52) + (13ULL << 16);
10823 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10824 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10825 pinsn & 0xffffffff);
10826 r_type = elfcpp::R_PPC64_TPREL34;
10827 value = psymval->value(object, rela.get_r_addend());
10828 }
10829 else
10830 {
10831 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10832 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10833 {
10834 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10835 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10836 insn &= (1 << 26) - (1 << 21); // extract rt
10837 if (size == 32)
10838 insn |= addis_0_2;
10839 else
10840 insn |= addis_0_13;
10841 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10842 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10843 value = psymval->value(object, rela.get_r_addend());
10844 }
10845 else
10846 {
10847 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10848 Insn insn = nop;
10849 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10850 r_type = elfcpp::R_POWERPC_NONE;
10851 }
10852 }
10853 }
10854 }
10855 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10856 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10857 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10858 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10859 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10860 {
10861 // First instruction of a local dynamic sequence, arg setup insn.
10862 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
10863 if (!object->no_tls_marker())
10864 tls_type = target->optimize_tls_ld();
10865 if (tls_type == tls::TLSOPT_NONE)
10866 {
10867 value = target->tlsld_got_offset();
10868 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10869 value += target->got_section()->address();
10870 else
10871 value -= target->got_section()->got_base_offset(object);
10872 }
10873 else
10874 {
10875 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10876 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10877 {
10878 Insn* iview = reinterpret_cast<Insn*>(view);
10879 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10880 pinsn <<= 32;
10881 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10882 // pla pcrel -> paddi r13
10883 pinsn += (-1ULL << 52) + (13ULL << 16);
10884 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10885 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10886 pinsn & 0xffffffff);
10887 r_type = elfcpp::R_PPC64_TPREL34;
10888 value = dtp_offset;
10889 }
10890 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10891 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10892 {
10893 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10894 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10895 insn &= (1 << 26) - (1 << 21); // extract rt
10896 if (size == 32)
10897 insn |= addis_0_2;
10898 else
10899 insn |= addis_0_13;
10900 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10901 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10902 value = dtp_offset;
10903 }
10904 else
10905 {
10906 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10907 Insn insn = nop;
10908 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10909 r_type = elfcpp::R_POWERPC_NONE;
10910 }
10911 }
10912 }
10913 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10914 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10915 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
10916 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10917 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10918 {
10919 // Accesses relative to a local dynamic sequence address,
10920 // no optimisation here.
10921 if (gsym != NULL)
10922 {
10923 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10924 value = gsym->got_offset(GOT_TYPE_DTPREL);
10925 }
10926 else
10927 {
10928 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10929 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10930 }
10931 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10932 value += target->got_section()->address();
10933 else
10934 value -= target->got_section()->got_base_offset(object);
10935 }
10936 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10937 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10938 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10939 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10940 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10941 {
10942 // First instruction of initial exec sequence.
10943 bool final = gsym == NULL || gsym->final_value_is_known();
10944 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10945 if (tls_type == tls::TLSOPT_NONE)
10946 {
10947 if (gsym != NULL)
10948 {
10949 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10950 value = gsym->got_offset(GOT_TYPE_TPREL);
10951 }
10952 else
10953 {
10954 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10955 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10956 }
10957 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10958 value += target->got_section()->address();
10959 else
10960 value -= target->got_section()->got_base_offset(object);
10961 }
10962 else
10963 {
10964 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10965 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10966 {
10967 Insn* iview = reinterpret_cast<Insn*>(view);
10968 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10969 pinsn <<= 32;
10970 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10971 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10972 pinsn += ((2ULL << 56) + (-1ULL << 52)
10973 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10974 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10975 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10976 pinsn & 0xffffffff);
10977 r_type = elfcpp::R_PPC64_TPREL34;
10978 value = psymval->value(object, rela.get_r_addend());
10979 }
10980 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10981 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10982 {
10983 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10984 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10985 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10986 if (size == 32)
10987 insn |= addis_0_2;
10988 else
10989 insn |= addis_0_13;
10990 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10991 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10992 value = psymval->value(object, rela.get_r_addend());
10993 }
10994 else
10995 {
10996 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10997 Insn insn = nop;
10998 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10999 r_type = elfcpp::R_POWERPC_NONE;
11000 }
11001 }
11002 }
11003 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11004 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11005 {
11006 // Second instruction of a global dynamic sequence,
11007 // the __tls_get_addr call
11008 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11009 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
11010 if (!object->no_tls_marker())
11011 {
11012 bool final = gsym == NULL || gsym->final_value_is_known();
11013 tls_type = target->optimize_tls_gd(final);
11014 }
11015 if (tls_type != tls::TLSOPT_NONE)
11016 {
11017 if (tls_type == tls::TLSOPT_TO_IE)
11018 {
11019 Insn* iview = reinterpret_cast<Insn*>(view);
11020 Insn insn = add_3_3_13;
11021 if (size == 32)
11022 insn = add_3_3_2;
11023 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11024 r_type = elfcpp::R_POWERPC_NONE;
11025 }
11026 else
11027 {
11028 bool is_pcrel = false;
11029 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11030 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11031 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11032 if (relnum < reloc_count - 1)
11033 {
11034 Reltype next_rela(preloc + reloc_size);
11035 unsigned int r_type2
11036 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11037 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11038 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11039 && next_rela.get_r_offset() == rela.get_r_offset())
11040 is_pcrel = true;
11041 }
11042 Insn* iview = reinterpret_cast<Insn*>(view);
11043 if (is_pcrel)
11044 {
11045 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11046 r_type = elfcpp::R_POWERPC_NONE;
11047 }
11048 else
11049 {
11050 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11051 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11052 view += d_offset;
11053 value = psymval->value(object, rela.get_r_addend());
11054 }
11055 }
11056 this->skip_next_tls_get_addr_call();
11057 }
11058 }
11059 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11060 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11061 {
11062 // Second instruction of a local dynamic sequence,
11063 // the __tls_get_addr call
11064 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11065 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
11066 if (!object->no_tls_marker())
11067 tls_type = target->optimize_tls_ld();
11068 if (tls_type == tls::TLSOPT_TO_LE)
11069 {
11070 bool is_pcrel = false;
11071 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11072 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11073 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11074 if (relnum < reloc_count - 1)
11075 {
11076 Reltype next_rela(preloc + reloc_size);
11077 unsigned int r_type2
11078 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11079 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11080 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11081 && next_rela.get_r_offset() == rela.get_r_offset())
11082 is_pcrel = true;
11083 }
11084 Insn* iview = reinterpret_cast<Insn*>(view);
11085 if (is_pcrel)
11086 {
11087 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11088 r_type = elfcpp::R_POWERPC_NONE;
11089 }
11090 else
11091 {
11092 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11093 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11094 view += d_offset;
11095 value = dtp_offset;
11096 }
11097 this->skip_next_tls_get_addr_call();
11098 }
11099 }
11100 else if (r_type == elfcpp::R_POWERPC_TLS)
11101 {
11102 // Second instruction of an initial exec sequence
11103 bool final = gsym == NULL || gsym->final_value_is_known();
11104 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11105 if (tls_type == tls::TLSOPT_TO_LE)
11106 {
11107 Address roff = rela.get_r_offset() & 3;
11108 Insn* iview = reinterpret_cast<Insn*>(view - roff);
11109 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11110 unsigned int reg = size == 32 ? 2 : 13;
11111 insn = at_tls_transform(insn, reg);
11112 gold_assert(insn != 0);
11113 if (roff == 0)
11114 {
11115 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11116 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11117 view += d_offset;
11118 value = psymval->value(object, rela.get_r_addend());
11119 }
11120 else if (roff == 1)
11121 {
11122 // For pcrel IE to LE we already have the full offset
11123 // and thus don't need an addi here. A nop or mr will do.
11124 if ((insn & (0x3f << 26)) == 14 << 26)
11125 {
11126 // Extract regs from addi rt,ra,si.
11127 unsigned int rt = (insn >> 21) & 0x1f;
11128 unsigned int ra = (insn >> 16) & 0x1f;
11129 if (rt == ra)
11130 insn = nop;
11131 else
11132 {
11133 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11134 insn = (rt << 16) | (ra << 21) | (ra << 11);
11135 insn |= (31u << 26) | (444u << 1);
11136 }
11137 }
11138 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11139 r_type = elfcpp::R_POWERPC_NONE;
11140 }
11141 }
11142 }
11143 else if (!has_stub_value)
11144 {
11145 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11146 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11147 {
11148 // PLTCALL without plt entry => convert to direct call
11149 Insn* iview = reinterpret_cast<Insn*>(view);
11150 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11151 insn = (insn & 1) | b;
11152 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11153 if (size == 32)
11154 r_type = elfcpp::R_PPC_PLTREL24;
11155 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11156 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11157 else
11158 r_type = elfcpp::R_POWERPC_REL24;
11159 }
11160 Address addend = 0;
11161 if (!(size == 32
11162 && (r_type == elfcpp::R_PPC_PLTREL24
11163 || r_type == elfcpp::R_POWERPC_PLT16_LO
11164 || r_type == elfcpp::R_POWERPC_PLT16_HI
11165 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11166 addend = rela.get_r_addend();
11167 value = psymval->value(object, addend);
11168 unsigned int local_ent = 0;
11169 if (size == 64 && is_branch_reloc<size>(r_type))
11170 {
11171 if (target->abiversion() >= 2)
11172 {
11173 if (gsym != NULL)
11174 local_ent = object->ppc64_local_entry_offset(gsym);
11175 else
11176 local_ent = object->ppc64_local_entry_offset(r_sym);
11177 }
11178 else
11179 {
11180 unsigned int dest_shndx;
11181 target->symval_for_branch(relinfo->symtab, gsym, object,
11182 &value, &dest_shndx);
11183 }
11184 }
11185 Address max_branch = max_branch_delta<size>(r_type);
11186 if (max_branch != 0
11187 && (value + local_ent - address + max_branch >= 2 * max_branch
11188 || (size == 64
11189 && r_type == elfcpp::R_PPC64_REL24_NOTOC
11190 && (gsym != NULL
11191 ? object->ppc64_needs_toc(gsym)
11192 : object->ppc64_needs_toc(r_sym)))))
11193 {
11194 Stub_table<size, big_endian>* stub_table
11195 = object->stub_table(relinfo->data_shndx);
11196 if (stub_table != NULL)
11197 {
11198 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11199 = stub_table->find_long_branch_entry(object, value);
11200 if (ent != NULL)
11201 {
11202 if (ent->save_res_)
11203 value = (value - target->savres_section()->address()
11204 + stub_table->branch_size());
11205 else
11206 {
11207 value = (stub_table->stub_address()
11208 + stub_table->plt_size()
11209 + ent->off_);
11210 if (size == 64
11211 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
11212 value += ent->tocoff_;
11213 }
11214 has_stub_value = true;
11215 }
11216 }
11217 }
11218 if (!has_stub_value)
11219 value += local_ent;
11220 }
11221
11222 switch (r_type)
11223 {
11224 case elfcpp::R_PPC64_REL24_NOTOC:
11225 if (size == 32)
11226 break;
11227 // Fall through.
11228 case elfcpp::R_PPC64_REL64:
11229 case elfcpp::R_POWERPC_REL32:
11230 case elfcpp::R_POWERPC_REL24:
11231 case elfcpp::R_PPC_PLTREL24:
11232 case elfcpp::R_PPC_LOCAL24PC:
11233 case elfcpp::R_POWERPC_REL16:
11234 case elfcpp::R_POWERPC_REL16_LO:
11235 case elfcpp::R_POWERPC_REL16_HI:
11236 case elfcpp::R_POWERPC_REL16_HA:
11237 case elfcpp::R_POWERPC_REL16DX_HA:
11238 case elfcpp::R_PPC64_REL16_HIGH:
11239 case elfcpp::R_PPC64_REL16_HIGHA:
11240 case elfcpp::R_PPC64_REL16_HIGHER:
11241 case elfcpp::R_PPC64_REL16_HIGHERA:
11242 case elfcpp::R_PPC64_REL16_HIGHEST:
11243 case elfcpp::R_PPC64_REL16_HIGHESTA:
11244 case elfcpp::R_POWERPC_REL14:
11245 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11246 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11247 case elfcpp::R_PPC64_PCREL34:
11248 case elfcpp::R_PPC64_GOT_PCREL34:
11249 case elfcpp::R_PPC64_PLT_PCREL34:
11250 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11251 case elfcpp::R_PPC64_PCREL28:
11252 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11253 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11254 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11255 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11256 case elfcpp::R_PPC64_REL16_HIGHER34:
11257 case elfcpp::R_PPC64_REL16_HIGHERA34:
11258 case elfcpp::R_PPC64_REL16_HIGHEST34:
11259 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11260 value -= address;
11261 break;
11262
11263 case elfcpp::R_PPC64_TOC16:
11264 case elfcpp::R_PPC64_TOC16_LO:
11265 case elfcpp::R_PPC64_TOC16_HI:
11266 case elfcpp::R_PPC64_TOC16_HA:
11267 case elfcpp::R_PPC64_TOC16_DS:
11268 case elfcpp::R_PPC64_TOC16_LO_DS:
11269 // Subtract the TOC base address.
11270 value -= (target->got_section()->output_section()->address()
11271 + object->toc_base_offset());
11272 break;
11273
11274 case elfcpp::R_POWERPC_SECTOFF:
11275 case elfcpp::R_POWERPC_SECTOFF_LO:
11276 case elfcpp::R_POWERPC_SECTOFF_HI:
11277 case elfcpp::R_POWERPC_SECTOFF_HA:
11278 case elfcpp::R_PPC64_SECTOFF_DS:
11279 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11280 if (os != NULL)
11281 value -= os->address();
11282 break;
11283
11284 case elfcpp::R_PPC64_TPREL16_DS:
11285 case elfcpp::R_PPC64_TPREL16_LO_DS:
11286 case elfcpp::R_PPC64_TPREL16_HIGH:
11287 case elfcpp::R_PPC64_TPREL16_HIGHA:
11288 if (size != 64)
11289 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11290 break;
11291 // Fall through.
11292 case elfcpp::R_POWERPC_TPREL16:
11293 case elfcpp::R_POWERPC_TPREL16_LO:
11294 case elfcpp::R_POWERPC_TPREL16_HI:
11295 case elfcpp::R_POWERPC_TPREL16_HA:
11296 case elfcpp::R_POWERPC_TPREL:
11297 case elfcpp::R_PPC64_TPREL16_HIGHER:
11298 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11299 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11300 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11301 case elfcpp::R_PPC64_TPREL34:
11302 // tls symbol values are relative to tls_segment()->vaddr()
11303 value -= tp_offset;
11304 break;
11305
11306 case elfcpp::R_PPC64_DTPREL16_DS:
11307 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11308 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11309 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11310 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11311 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11312 if (size != 64)
11313 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11314 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11315 break;
11316 // Fall through.
11317 case elfcpp::R_POWERPC_DTPREL16:
11318 case elfcpp::R_POWERPC_DTPREL16_LO:
11319 case elfcpp::R_POWERPC_DTPREL16_HI:
11320 case elfcpp::R_POWERPC_DTPREL16_HA:
11321 case elfcpp::R_POWERPC_DTPREL:
11322 case elfcpp::R_PPC64_DTPREL16_HIGH:
11323 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11324 case elfcpp::R_PPC64_DTPREL34:
11325 // tls symbol values are relative to tls_segment()->vaddr()
11326 value -= dtp_offset;
11327 break;
11328
11329 case elfcpp::R_PPC64_ADDR64_LOCAL:
11330 if (gsym != NULL)
11331 value += object->ppc64_local_entry_offset(gsym);
11332 else
11333 value += object->ppc64_local_entry_offset(r_sym);
11334 break;
11335
11336 default:
11337 break;
11338 }
11339
11340 Insn branch_bit = 0;
11341 switch (r_type)
11342 {
11343 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11344 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11345 branch_bit = 1 << 21;
11346 // Fall through.
11347 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11348 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11349 {
11350 Insn* iview = reinterpret_cast<Insn*>(view);
11351 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11352 insn &= ~(1 << 21);
11353 insn |= branch_bit;
11354 if (this->is_isa_v2)
11355 {
11356 // Set 'a' bit. This is 0b00010 in BO field for branch
11357 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11358 // for branch on CTR insns (BO == 1a00t or 1a01t).
11359 if ((insn & (0x14 << 21)) == (0x04 << 21))
11360 insn |= 0x02 << 21;
11361 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11362 insn |= 0x08 << 21;
11363 else
11364 break;
11365 }
11366 else
11367 {
11368 // Invert 'y' bit if not the default.
11369 if (static_cast<Signed_address>(value) < 0)
11370 insn ^= 1 << 21;
11371 }
11372 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11373 }
11374 break;
11375
11376 case elfcpp::R_POWERPC_PLT16_HA:
11377 if (size == 32
11378 && !parameters->options().output_is_position_independent())
11379 {
11380 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11381 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11382
11383 // Convert addis to lis.
11384 if ((insn & (0x3f << 26)) == 15u << 26
11385 && (insn & (0x1f << 16)) != 0)
11386 {
11387 insn &= ~(0x1f << 16);
11388 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11389 }
11390 }
11391 break;
11392
11393 default:
11394 break;
11395 }
11396
11397 if (gsym
11398 ? relative_value_is_known(gsym)
11399 : relative_value_is_known(psymval))
11400 {
11401 Insn* iview;
11402 Insn* iview2;
11403 Insn insn;
11404 uint64_t pinsn, pinsn2;
11405
11406 switch (r_type)
11407 {
11408 default:
11409 break;
11410
11411 // Multi-instruction sequences that access the GOT/TOC can
11412 // be optimized, eg.
11413 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11414 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11415 // and
11416 // addis ra,r2,0; addi rb,ra,x@toc@l;
11417 // to nop; addi rb,r2,x@toc;
11418 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11419 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11420 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11421 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11422 case elfcpp::R_POWERPC_GOT16_HA:
11423 case elfcpp::R_PPC64_TOC16_HA:
11424 if (size == 64 && parameters->options().toc_optimize())
11425 {
11426 iview = reinterpret_cast<Insn*>(view - d_offset);
11427 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11428 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11429 && object->make_toc_relative(target, &value))
11430 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11431 && object->make_got_relative(target, psymval,
11432 rela.get_r_addend(),
11433 &value)))
11434 {
11435 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11436 == ((15u << 26) | (2 << 16)));
11437 }
11438 if (((insn & ((0x3f << 26) | 0x1f << 16))
11439 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11440 && value + 0x8000 < 0x10000)
11441 {
11442 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11443 return true;
11444 }
11445 }
11446 break;
11447
11448 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11449 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11450 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11451 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11452 case elfcpp::R_POWERPC_GOT16_LO:
11453 case elfcpp::R_PPC64_GOT16_LO_DS:
11454 case elfcpp::R_PPC64_TOC16_LO:
11455 case elfcpp::R_PPC64_TOC16_LO_DS:
11456 if (size == 64 && parameters->options().toc_optimize())
11457 {
11458 iview = reinterpret_cast<Insn*>(view - d_offset);
11459 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11460 bool changed = false;
11461 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11462 && object->make_toc_relative(target, &value))
11463 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11464 && object->make_got_relative(target, psymval,
11465 rela.get_r_addend(),
11466 &value)))
11467 {
11468 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11469 insn ^= (14u << 26) ^ (58u << 26);
11470 r_type = elfcpp::R_PPC64_TOC16_LO;
11471 changed = true;
11472 }
11473 if (ok_lo_toc_insn(insn, r_type)
11474 && value + 0x8000 < 0x10000)
11475 {
11476 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11477 {
11478 // Transform addic to addi when we change reg.
11479 insn &= ~((0x3f << 26) | (0x1f << 16));
11480 insn |= (14u << 26) | (2 << 16);
11481 }
11482 else
11483 {
11484 insn &= ~(0x1f << 16);
11485 insn |= 2 << 16;
11486 }
11487 changed = true;
11488 }
11489 if (changed)
11490 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11491 }
11492 break;
11493
11494 case elfcpp::R_PPC64_GOT_PCREL34:
11495 if (size == 64 && parameters->options().toc_optimize())
11496 {
11497 iview = reinterpret_cast<Insn*>(view);
11498 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11499 pinsn <<= 32;
11500 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11501 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11502 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11503 break;
11504
11505 Address relval = psymval->value(object, rela.get_r_addend());
11506 relval -= address;
11507 if (relval + (1ULL << 33) < 1ULL << 34)
11508 {
11509 value = relval;
11510 // Replace with paddi
11511 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11512 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11513 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11514 pinsn & 0xffffffff);
11515 goto pcrelopt;
11516 }
11517 }
11518 break;
11519
11520 case elfcpp::R_PPC64_PCREL34:
11521 if (size == 64)
11522 {
11523 iview = reinterpret_cast<Insn*>(view);
11524 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11525 pinsn <<= 32;
11526 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11527 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11528 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11529 | (14ULL << 26) /* paddi */))
11530 break;
11531
11532 pcrelopt:
11533 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11534 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11535 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11536 if (relnum >= reloc_count - 1)
11537 break;
11538
11539 Reltype next_rela(preloc + reloc_size);
11540 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11541 != elfcpp::R_PPC64_PCREL_OPT)
11542 || next_rela.get_r_offset() != rela.get_r_offset())
11543 break;
11544
11545 Address off = next_rela.get_r_addend();
11546 if (off == 0)
11547 off = 8; // zero means next insn.
11548 if (off + rela.get_r_offset() + 4 > view_size)
11549 break;
11550
11551 iview2 = reinterpret_cast<Insn*>(view + off);
11552 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11553 pinsn2 <<= 32;
11554 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11555 break;
11556 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11557 {
11558 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11559 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11560 pinsn & 0xffffffff);
11561 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11562 }
11563 }
11564 break;
11565
11566 case elfcpp::R_POWERPC_TPREL16_HA:
11567 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11568 {
11569 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11570 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11571 return true;
11572 }
11573 break;
11574
11575 case elfcpp::R_PPC64_TPREL16_LO_DS:
11576 if (size == 32)
11577 // R_PPC_TLSGD, R_PPC_TLSLD
11578 break;
11579 // Fall through.
11580 case elfcpp::R_POWERPC_TPREL16_LO:
11581 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11582 {
11583 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11584 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11585 insn &= ~(0x1f << 16);
11586 insn |= (size == 32 ? 2 : 13) << 16;
11587 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11588 }
11589 break;
11590
11591 case elfcpp::R_PPC64_ENTRY:
11592 if (size == 64)
11593 {
11594 value = (target->got_section()->output_section()->address()
11595 + object->toc_base_offset());
11596 if (value + 0x80008000 <= 0xffffffff
11597 && !parameters->options().output_is_position_independent())
11598 {
11599 Insn* iview = reinterpret_cast<Insn*>(view);
11600 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11601 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11602
11603 if ((insn1 & ~0xfffc) == ld_2_12
11604 && insn2 == add_2_2_12)
11605 {
11606 insn1 = lis_2 + ha(value);
11607 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11608 insn2 = addi_2_2 + l(value);
11609 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11610 return true;
11611 }
11612 }
11613 else
11614 {
11615 value -= address;
11616 if (value + 0x80008000 <= 0xffffffff)
11617 {
11618 Insn* iview = reinterpret_cast<Insn*>(view);
11619 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11620 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11621
11622 if ((insn1 & ~0xfffc) == ld_2_12
11623 && insn2 == add_2_2_12)
11624 {
11625 insn1 = addis_2_12 + ha(value);
11626 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11627 insn2 = addi_2_2 + l(value);
11628 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11629 return true;
11630 }
11631 }
11632 }
11633 }
11634 break;
11635
11636 case elfcpp::R_POWERPC_REL16_LO:
11637 // If we are generating a non-PIC executable, edit
11638 // 0: addis 2,12,.TOC.-0b@ha
11639 // addi 2,2,.TOC.-0b@l
11640 // used by ELFv2 global entry points to set up r2, to
11641 // lis 2,.TOC.@ha
11642 // addi 2,2,.TOC.@l
11643 // if .TOC. is in range. */
11644 if (size == 64
11645 && value + address - 4 + 0x80008000 <= 0xffffffff
11646 && relnum + 1 > 1
11647 && preloc != NULL
11648 && target->abiversion() >= 2
11649 && !parameters->options().output_is_position_independent()
11650 && rela.get_r_addend() == d_offset + 4
11651 && gsym != NULL
11652 && strcmp(gsym->name(), ".TOC.") == 0)
11653 {
11654 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11655 Reltype prev_rela(preloc - reloc_size);
11656 if ((prev_rela.get_r_info()
11657 == elfcpp::elf_r_info<size>(r_sym,
11658 elfcpp::R_POWERPC_REL16_HA))
11659 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11660 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11661 {
11662 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11663 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11664 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11665
11666 if ((insn1 & 0xffff0000) == addis_2_12
11667 && (insn2 & 0xffff0000) == addi_2_2)
11668 {
11669 insn1 = lis_2 + ha(value + address - 4);
11670 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11671 insn2 = addi_2_2 + l(value + address - 4);
11672 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11673 if (relinfo->rr)
11674 {
11675 relinfo->rr->set_strategy(relnum - 1,
11676 Relocatable_relocs::RELOC_SPECIAL);
11677 relinfo->rr->set_strategy(relnum,
11678 Relocatable_relocs::RELOC_SPECIAL);
11679 }
11680 return true;
11681 }
11682 }
11683 }
11684 break;
11685 }
11686 }
11687
11688 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11689 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11690 switch (r_type)
11691 {
11692 case elfcpp::R_POWERPC_ADDR32:
11693 case elfcpp::R_POWERPC_UADDR32:
11694 if (size == 64)
11695 overflow = Reloc::CHECK_BITFIELD;
11696 break;
11697
11698 case elfcpp::R_POWERPC_REL32:
11699 case elfcpp::R_POWERPC_REL16DX_HA:
11700 if (size == 64)
11701 overflow = Reloc::CHECK_SIGNED;
11702 break;
11703
11704 case elfcpp::R_POWERPC_UADDR16:
11705 overflow = Reloc::CHECK_BITFIELD;
11706 break;
11707
11708 case elfcpp::R_POWERPC_ADDR16:
11709 // We really should have three separate relocations,
11710 // one for 16-bit data, one for insns with 16-bit signed fields,
11711 // and one for insns with 16-bit unsigned fields.
11712 overflow = Reloc::CHECK_BITFIELD;
11713 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11714 overflow = Reloc::CHECK_LOW_INSN;
11715 break;
11716
11717 case elfcpp::R_POWERPC_ADDR16_HI:
11718 case elfcpp::R_POWERPC_ADDR16_HA:
11719 case elfcpp::R_POWERPC_GOT16_HI:
11720 case elfcpp::R_POWERPC_GOT16_HA:
11721 case elfcpp::R_POWERPC_PLT16_HI:
11722 case elfcpp::R_POWERPC_PLT16_HA:
11723 case elfcpp::R_POWERPC_SECTOFF_HI:
11724 case elfcpp::R_POWERPC_SECTOFF_HA:
11725 case elfcpp::R_PPC64_TOC16_HI:
11726 case elfcpp::R_PPC64_TOC16_HA:
11727 case elfcpp::R_PPC64_PLTGOT16_HI:
11728 case elfcpp::R_PPC64_PLTGOT16_HA:
11729 case elfcpp::R_POWERPC_TPREL16_HI:
11730 case elfcpp::R_POWERPC_TPREL16_HA:
11731 case elfcpp::R_POWERPC_DTPREL16_HI:
11732 case elfcpp::R_POWERPC_DTPREL16_HA:
11733 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11734 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11735 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11736 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11737 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11738 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11739 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11740 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11741 case elfcpp::R_POWERPC_REL16_HI:
11742 case elfcpp::R_POWERPC_REL16_HA:
11743 if (size != 32)
11744 overflow = Reloc::CHECK_HIGH_INSN;
11745 break;
11746
11747 case elfcpp::R_POWERPC_REL16:
11748 case elfcpp::R_PPC64_TOC16:
11749 case elfcpp::R_POWERPC_GOT16:
11750 case elfcpp::R_POWERPC_SECTOFF:
11751 case elfcpp::R_POWERPC_TPREL16:
11752 case elfcpp::R_POWERPC_DTPREL16:
11753 case elfcpp::R_POWERPC_GOT_TLSGD16:
11754 case elfcpp::R_POWERPC_GOT_TLSLD16:
11755 case elfcpp::R_POWERPC_GOT_TPREL16:
11756 case elfcpp::R_POWERPC_GOT_DTPREL16:
11757 overflow = Reloc::CHECK_LOW_INSN;
11758 break;
11759
11760 case elfcpp::R_PPC64_REL24_NOTOC:
11761 if (size == 32)
11762 break;
11763 // Fall through.
11764 case elfcpp::R_POWERPC_ADDR24:
11765 case elfcpp::R_POWERPC_ADDR14:
11766 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11767 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11768 case elfcpp::R_PPC64_ADDR16_DS:
11769 case elfcpp::R_POWERPC_REL24:
11770 case elfcpp::R_PPC_PLTREL24:
11771 case elfcpp::R_PPC_LOCAL24PC:
11772 case elfcpp::R_PPC64_TPREL16_DS:
11773 case elfcpp::R_PPC64_DTPREL16_DS:
11774 case elfcpp::R_PPC64_TOC16_DS:
11775 case elfcpp::R_PPC64_GOT16_DS:
11776 case elfcpp::R_PPC64_SECTOFF_DS:
11777 case elfcpp::R_POWERPC_REL14:
11778 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11779 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11780 case elfcpp::R_PPC64_D34:
11781 case elfcpp::R_PPC64_PCREL34:
11782 case elfcpp::R_PPC64_GOT_PCREL34:
11783 case elfcpp::R_PPC64_PLT_PCREL34:
11784 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11785 case elfcpp::R_PPC64_D28:
11786 case elfcpp::R_PPC64_PCREL28:
11787 case elfcpp::R_PPC64_TPREL34:
11788 case elfcpp::R_PPC64_DTPREL34:
11789 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11790 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11791 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11792 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11793 overflow = Reloc::CHECK_SIGNED;
11794 break;
11795 }
11796
11797 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11798 Insn insn = 0;
11799
11800 if (overflow == Reloc::CHECK_LOW_INSN
11801 || overflow == Reloc::CHECK_HIGH_INSN)
11802 {
11803 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11804
11805 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11806 overflow = Reloc::CHECK_BITFIELD;
11807 else if (overflow == Reloc::CHECK_LOW_INSN
11808 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11809 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11810 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11811 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11812 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11813 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
11814 overflow = Reloc::CHECK_UNSIGNED;
11815 else
11816 overflow = Reloc::CHECK_SIGNED;
11817 }
11818
11819 bool maybe_dq_reloc = false;
11820 typename Powerpc_relocate_functions<size, big_endian>::Status status
11821 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
11822 switch (r_type)
11823 {
11824 case elfcpp::R_POWERPC_NONE:
11825 case elfcpp::R_POWERPC_TLS:
11826 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11827 case elfcpp::R_POWERPC_GNU_VTENTRY:
11828 case elfcpp::R_POWERPC_PLTSEQ:
11829 case elfcpp::R_POWERPC_PLTCALL:
11830 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11831 case elfcpp::R_PPC64_PLTCALL_NOTOC:
11832 case elfcpp::R_PPC64_PCREL_OPT:
11833 break;
11834
11835 case elfcpp::R_PPC64_ADDR64:
11836 case elfcpp::R_PPC64_REL64:
11837 case elfcpp::R_PPC64_TOC:
11838 case elfcpp::R_PPC64_ADDR64_LOCAL:
11839 Reloc::addr64(view, value);
11840 break;
11841
11842 case elfcpp::R_POWERPC_TPREL:
11843 case elfcpp::R_POWERPC_DTPREL:
11844 if (size == 64)
11845 Reloc::addr64(view, value);
11846 else
11847 status = Reloc::addr32(view, value, overflow);
11848 break;
11849
11850 case elfcpp::R_PPC64_UADDR64:
11851 Reloc::addr64_u(view, value);
11852 break;
11853
11854 case elfcpp::R_POWERPC_ADDR32:
11855 status = Reloc::addr32(view, value, overflow);
11856 break;
11857
11858 case elfcpp::R_POWERPC_REL32:
11859 case elfcpp::R_POWERPC_UADDR32:
11860 status = Reloc::addr32_u(view, value, overflow);
11861 break;
11862
11863 case elfcpp::R_PPC64_REL24_NOTOC:
11864 if (size == 32)
11865 goto unsupp; // R_PPC_EMB_RELSDA
11866 // Fall through.
11867 case elfcpp::R_POWERPC_ADDR24:
11868 case elfcpp::R_POWERPC_REL24:
11869 case elfcpp::R_PPC_PLTREL24:
11870 case elfcpp::R_PPC_LOCAL24PC:
11871 status = Reloc::addr24(view, value, overflow);
11872 break;
11873
11874 case elfcpp::R_POWERPC_GOT_DTPREL16:
11875 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11876 case elfcpp::R_POWERPC_GOT_TPREL16:
11877 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11878 if (size == 64)
11879 {
11880 // On ppc64 these are all ds form
11881 maybe_dq_reloc = true;
11882 break;
11883 }
11884 // Fall through.
11885 case elfcpp::R_POWERPC_ADDR16:
11886 case elfcpp::R_POWERPC_REL16:
11887 case elfcpp::R_PPC64_TOC16:
11888 case elfcpp::R_POWERPC_GOT16:
11889 case elfcpp::R_POWERPC_SECTOFF:
11890 case elfcpp::R_POWERPC_TPREL16:
11891 case elfcpp::R_POWERPC_DTPREL16:
11892 case elfcpp::R_POWERPC_GOT_TLSGD16:
11893 case elfcpp::R_POWERPC_GOT_TLSLD16:
11894 case elfcpp::R_POWERPC_ADDR16_LO:
11895 case elfcpp::R_POWERPC_REL16_LO:
11896 case elfcpp::R_PPC64_TOC16_LO:
11897 case elfcpp::R_POWERPC_GOT16_LO:
11898 case elfcpp::R_POWERPC_PLT16_LO:
11899 case elfcpp::R_POWERPC_SECTOFF_LO:
11900 case elfcpp::R_POWERPC_TPREL16_LO:
11901 case elfcpp::R_POWERPC_DTPREL16_LO:
11902 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11903 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11904 if (size == 64)
11905 status = Reloc::addr16(view, value, overflow);
11906 else
11907 maybe_dq_reloc = true;
11908 break;
11909
11910 case elfcpp::R_POWERPC_UADDR16:
11911 status = Reloc::addr16_u(view, value, overflow);
11912 break;
11913
11914 case elfcpp::R_PPC64_ADDR16_HIGH:
11915 case elfcpp::R_PPC64_TPREL16_HIGH:
11916 case elfcpp::R_PPC64_DTPREL16_HIGH:
11917 if (size == 32)
11918 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11919 goto unsupp;
11920 // Fall through.
11921 case elfcpp::R_POWERPC_ADDR16_HI:
11922 case elfcpp::R_POWERPC_REL16_HI:
11923 case elfcpp::R_PPC64_REL16_HIGH:
11924 case elfcpp::R_PPC64_TOC16_HI:
11925 case elfcpp::R_POWERPC_GOT16_HI:
11926 case elfcpp::R_POWERPC_PLT16_HI:
11927 case elfcpp::R_POWERPC_SECTOFF_HI:
11928 case elfcpp::R_POWERPC_TPREL16_HI:
11929 case elfcpp::R_POWERPC_DTPREL16_HI:
11930 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11931 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11932 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11933 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11934 Reloc::addr16_hi(view, value);
11935 break;
11936
11937 case elfcpp::R_PPC64_ADDR16_HIGHA:
11938 case elfcpp::R_PPC64_TPREL16_HIGHA:
11939 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11940 if (size == 32)
11941 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11942 goto unsupp;
11943 // Fall through.
11944 case elfcpp::R_POWERPC_ADDR16_HA:
11945 case elfcpp::R_POWERPC_REL16_HA:
11946 case elfcpp::R_PPC64_REL16_HIGHA:
11947 case elfcpp::R_PPC64_TOC16_HA:
11948 case elfcpp::R_POWERPC_GOT16_HA:
11949 case elfcpp::R_POWERPC_PLT16_HA:
11950 case elfcpp::R_POWERPC_SECTOFF_HA:
11951 case elfcpp::R_POWERPC_TPREL16_HA:
11952 case elfcpp::R_POWERPC_DTPREL16_HA:
11953 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11954 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11955 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11956 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11957 Reloc::addr16_ha(view, value);
11958 break;
11959
11960 case elfcpp::R_POWERPC_REL16DX_HA:
11961 status = Reloc::addr16dx_ha(view, value, overflow);
11962 break;
11963
11964 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11965 if (size == 32)
11966 // R_PPC_EMB_NADDR16_LO
11967 goto unsupp;
11968 // Fall through.
11969 case elfcpp::R_PPC64_ADDR16_HIGHER:
11970 case elfcpp::R_PPC64_REL16_HIGHER:
11971 case elfcpp::R_PPC64_TPREL16_HIGHER:
11972 Reloc::addr16_hi2(view, value);
11973 break;
11974
11975 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11976 if (size == 32)
11977 // R_PPC_EMB_NADDR16_HI
11978 goto unsupp;
11979 // Fall through.
11980 case elfcpp::R_PPC64_ADDR16_HIGHERA:
11981 case elfcpp::R_PPC64_REL16_HIGHERA:
11982 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11983 Reloc::addr16_ha2(view, value);
11984 break;
11985
11986 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11987 if (size == 32)
11988 // R_PPC_EMB_NADDR16_HA
11989 goto unsupp;
11990 // Fall through.
11991 case elfcpp::R_PPC64_ADDR16_HIGHEST:
11992 case elfcpp::R_PPC64_REL16_HIGHEST:
11993 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11994 Reloc::addr16_hi3(view, value);
11995 break;
11996
11997 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11998 if (size == 32)
11999 // R_PPC_EMB_SDAI16
12000 goto unsupp;
12001 // Fall through.
12002 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
12003 case elfcpp::R_PPC64_REL16_HIGHESTA:
12004 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12005 Reloc::addr16_ha3(view, value);
12006 break;
12007
12008 case elfcpp::R_PPC64_DTPREL16_DS:
12009 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12010 if (size == 32)
12011 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12012 goto unsupp;
12013 // Fall through.
12014 case elfcpp::R_PPC64_TPREL16_DS:
12015 case elfcpp::R_PPC64_TPREL16_LO_DS:
12016 if (size == 32)
12017 // R_PPC_TLSGD, R_PPC_TLSLD
12018 break;
12019 // Fall through.
12020 case elfcpp::R_PPC64_ADDR16_DS:
12021 case elfcpp::R_PPC64_ADDR16_LO_DS:
12022 case elfcpp::R_PPC64_TOC16_DS:
12023 case elfcpp::R_PPC64_TOC16_LO_DS:
12024 case elfcpp::R_PPC64_GOT16_DS:
12025 case elfcpp::R_PPC64_GOT16_LO_DS:
12026 case elfcpp::R_PPC64_PLT16_LO_DS:
12027 case elfcpp::R_PPC64_SECTOFF_DS:
12028 case elfcpp::R_PPC64_SECTOFF_LO_DS:
12029 maybe_dq_reloc = true;
12030 break;
12031
12032 case elfcpp::R_POWERPC_ADDR14:
12033 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12034 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12035 case elfcpp::R_POWERPC_REL14:
12036 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12037 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12038 status = Reloc::addr14(view, value, overflow);
12039 break;
12040
12041 case elfcpp::R_POWERPC_COPY:
12042 case elfcpp::R_POWERPC_GLOB_DAT:
12043 case elfcpp::R_POWERPC_JMP_SLOT:
12044 case elfcpp::R_POWERPC_RELATIVE:
12045 case elfcpp::R_POWERPC_DTPMOD:
12046 case elfcpp::R_PPC64_JMP_IREL:
12047 case elfcpp::R_POWERPC_IRELATIVE:
12048 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12049 _("unexpected reloc %u in object file"),
12050 r_type);
12051 break;
12052
12053 case elfcpp::R_PPC64_TOCSAVE:
12054 if (size == 32)
12055 // R_PPC_EMB_SDA21
12056 goto unsupp;
12057 else
12058 {
12059 Symbol_location loc;
12060 loc.object = relinfo->object;
12061 loc.shndx = relinfo->data_shndx;
12062 loc.offset = rela.get_r_offset();
12063 const Tocsave_loc *tocsave = target->tocsave_loc();
12064 if (tocsave->find(loc) != tocsave->end())
12065 {
12066 // If we've generated plt calls using this tocsave, then
12067 // the nop needs to be changed to save r2.
12068 Insn* iview = reinterpret_cast<Insn*>(view);
12069 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12070 elfcpp::Swap<32, big_endian>::
12071 writeval(iview, std_2_1 + target->stk_toc());
12072 }
12073 }
12074 break;
12075
12076 case elfcpp::R_PPC_EMB_SDA2I16:
12077 case elfcpp::R_PPC_EMB_SDA2REL:
12078 if (size == 32)
12079 goto unsupp;
12080 // R_PPC64_TLSGD, R_PPC64_TLSLD
12081 break;
12082
12083 case elfcpp::R_PPC64_D34:
12084 case elfcpp::R_PPC64_D34_LO:
12085 case elfcpp::R_PPC64_PCREL34:
12086 case elfcpp::R_PPC64_GOT_PCREL34:
12087 case elfcpp::R_PPC64_PLT_PCREL34:
12088 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12089 case elfcpp::R_PPC64_TPREL34:
12090 case elfcpp::R_PPC64_DTPREL34:
12091 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12092 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12093 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12094 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12095 if (size == 32)
12096 goto unsupp;
12097 status = Reloc::addr34(view, value, overflow);
12098 break;
12099
12100 case elfcpp::R_PPC64_D34_HI30:
12101 if (size == 32)
12102 goto unsupp;
12103 Reloc::addr34_hi(view, value);
12104 break;
12105
12106 case elfcpp::R_PPC64_D34_HA30:
12107 if (size == 32)
12108 goto unsupp;
12109 Reloc::addr34_ha(view, value);
12110 break;
12111
12112 case elfcpp::R_PPC64_D28:
12113 case elfcpp::R_PPC64_PCREL28:
12114 if (size == 32)
12115 goto unsupp;
12116 status = Reloc::addr28(view, value, overflow);
12117 break;
12118
12119 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12120 case elfcpp::R_PPC64_REL16_HIGHER34:
12121 if (size == 32)
12122 goto unsupp;
12123 Reloc::addr16_higher34(view, value);
12124 break;
12125
12126 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12127 case elfcpp::R_PPC64_REL16_HIGHERA34:
12128 if (size == 32)
12129 goto unsupp;
12130 Reloc::addr16_highera34(view, value);
12131 break;
12132
12133 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12134 case elfcpp::R_PPC64_REL16_HIGHEST34:
12135 if (size == 32)
12136 goto unsupp;
12137 Reloc::addr16_highest34(view, value);
12138 break;
12139
12140 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12141 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12142 if (size == 32)
12143 goto unsupp;
12144 Reloc::addr16_highesta34(view, value);
12145 break;
12146
12147 case elfcpp::R_POWERPC_PLT32:
12148 case elfcpp::R_POWERPC_PLTREL32:
12149 case elfcpp::R_PPC_SDAREL16:
12150 case elfcpp::R_POWERPC_ADDR30:
12151 case elfcpp::R_PPC64_PLT64:
12152 case elfcpp::R_PPC64_PLTREL64:
12153 case elfcpp::R_PPC64_PLTGOT16:
12154 case elfcpp::R_PPC64_PLTGOT16_LO:
12155 case elfcpp::R_PPC64_PLTGOT16_HI:
12156 case elfcpp::R_PPC64_PLTGOT16_HA:
12157 case elfcpp::R_PPC64_PLTGOT16_DS:
12158 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12159 case elfcpp::R_PPC_TOC16:
12160 default:
12161 unsupp:
12162 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12163 _("unsupported reloc %u"),
12164 r_type);
12165 break;
12166 }
12167
12168 if (maybe_dq_reloc)
12169 {
12170 if (insn == 0)
12171 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12172
12173 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12174 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12175 && (insn & 3) == 1))
12176 status = Reloc::addr16_dq(view, value, overflow);
12177 else if (size == 64
12178 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12179 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12180 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12181 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12182 status = Reloc::addr16_ds(view, value, overflow);
12183 else
12184 status = Reloc::addr16(view, value, overflow);
12185 }
12186
12187 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12188 && (has_stub_value
12189 || !(gsym != NULL
12190 && gsym->is_undefined()
12191 && is_branch_reloc<size>(r_type))))
12192 {
12193 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12194 _("relocation overflow"));
12195 if (has_stub_value)
12196 gold_info(_("try relinking with a smaller --stub-group-size"));
12197 }
12198
12199 return true;
12200 }
12201
12202 // Relocate section data.
12203
12204 template<int size, bool big_endian>
12205 void
12206 Target_powerpc<size, big_endian>::relocate_section(
12207 const Relocate_info<size, big_endian>* relinfo,
12208 unsigned int sh_type,
12209 const unsigned char* prelocs,
12210 size_t reloc_count,
12211 Output_section* output_section,
12212 bool needs_special_offset_handling,
12213 unsigned char* view,
12214 Address address,
12215 section_size_type view_size,
12216 const Reloc_symbol_changes* reloc_symbol_changes)
12217 {
12218 typedef Target_powerpc<size, big_endian> Powerpc;
12219 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12220 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12221 Powerpc_comdat_behavior;
12222 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12223 Classify_reloc;
12224
12225 gold_assert(sh_type == elfcpp::SHT_RELA);
12226
12227 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12228 Powerpc_comdat_behavior, Classify_reloc>(
12229 relinfo,
12230 this,
12231 prelocs,
12232 reloc_count,
12233 output_section,
12234 needs_special_offset_handling,
12235 view,
12236 address,
12237 view_size,
12238 reloc_symbol_changes);
12239 }
12240
12241 template<int size, bool big_endian>
12242 class Powerpc_scan_relocatable_reloc
12243 {
12244 public:
12245 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12246 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12247 static const int sh_type = elfcpp::SHT_RELA;
12248
12249 // Return the symbol referred to by the relocation.
12250 static inline unsigned int
12251 get_r_sym(const Reltype* reloc)
12252 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12253
12254 // Return the type of the relocation.
12255 static inline unsigned int
12256 get_r_type(const Reltype* reloc)
12257 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12258
12259 // Return the strategy to use for a local symbol which is not a
12260 // section symbol, given the relocation type.
12261 inline Relocatable_relocs::Reloc_strategy
12262 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12263 {
12264 if (r_type == 0 && r_sym == 0)
12265 return Relocatable_relocs::RELOC_DISCARD;
12266 return Relocatable_relocs::RELOC_COPY;
12267 }
12268
12269 // Return the strategy to use for a local symbol which is a section
12270 // symbol, given the relocation type.
12271 inline Relocatable_relocs::Reloc_strategy
12272 local_section_strategy(unsigned int, Relobj*)
12273 {
12274 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12275 }
12276
12277 // Return the strategy to use for a global symbol, given the
12278 // relocation type, the object, and the symbol index.
12279 inline Relocatable_relocs::Reloc_strategy
12280 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12281 {
12282 if (size == 32
12283 && (r_type == elfcpp::R_PPC_PLTREL24
12284 || r_type == elfcpp::R_POWERPC_PLT16_LO
12285 || r_type == elfcpp::R_POWERPC_PLT16_HI
12286 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12287 return Relocatable_relocs::RELOC_SPECIAL;
12288 return Relocatable_relocs::RELOC_COPY;
12289 }
12290 };
12291
12292 // Scan the relocs during a relocatable link.
12293
12294 template<int size, bool big_endian>
12295 void
12296 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12297 Symbol_table* symtab,
12298 Layout* layout,
12299 Sized_relobj_file<size, big_endian>* object,
12300 unsigned int data_shndx,
12301 unsigned int sh_type,
12302 const unsigned char* prelocs,
12303 size_t reloc_count,
12304 Output_section* output_section,
12305 bool needs_special_offset_handling,
12306 size_t local_symbol_count,
12307 const unsigned char* plocal_symbols,
12308 Relocatable_relocs* rr)
12309 {
12310 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12311
12312 gold_assert(sh_type == elfcpp::SHT_RELA);
12313
12314 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12315 symtab,
12316 layout,
12317 object,
12318 data_shndx,
12319 prelocs,
12320 reloc_count,
12321 output_section,
12322 needs_special_offset_handling,
12323 local_symbol_count,
12324 plocal_symbols,
12325 rr);
12326 }
12327
12328 // Scan the relocs for --emit-relocs.
12329
12330 template<int size, bool big_endian>
12331 void
12332 Target_powerpc<size, big_endian>::emit_relocs_scan(
12333 Symbol_table* symtab,
12334 Layout* layout,
12335 Sized_relobj_file<size, big_endian>* object,
12336 unsigned int data_shndx,
12337 unsigned int sh_type,
12338 const unsigned char* prelocs,
12339 size_t reloc_count,
12340 Output_section* output_section,
12341 bool needs_special_offset_handling,
12342 size_t local_symbol_count,
12343 const unsigned char* plocal_syms,
12344 Relocatable_relocs* rr)
12345 {
12346 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12347 Classify_reloc;
12348 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12349 Emit_relocs_strategy;
12350
12351 gold_assert(sh_type == elfcpp::SHT_RELA);
12352
12353 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12354 symtab,
12355 layout,
12356 object,
12357 data_shndx,
12358 prelocs,
12359 reloc_count,
12360 output_section,
12361 needs_special_offset_handling,
12362 local_symbol_count,
12363 plocal_syms,
12364 rr);
12365 }
12366
12367 // Emit relocations for a section.
12368 // This is a modified version of the function by the same name in
12369 // target-reloc.h. Using relocate_special_relocatable for
12370 // R_PPC_PLTREL24 would require duplication of the entire body of the
12371 // loop, so we may as well duplicate the whole thing.
12372
12373 template<int size, bool big_endian>
12374 void
12375 Target_powerpc<size, big_endian>::relocate_relocs(
12376 const Relocate_info<size, big_endian>* relinfo,
12377 unsigned int sh_type,
12378 const unsigned char* prelocs,
12379 size_t reloc_count,
12380 Output_section* output_section,
12381 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12382 unsigned char*,
12383 Address view_address,
12384 section_size_type,
12385 unsigned char* reloc_view,
12386 section_size_type reloc_view_size)
12387 {
12388 gold_assert(sh_type == elfcpp::SHT_RELA);
12389
12390 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12391 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12392 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12393 // Offset from start of insn to d-field reloc.
12394 const int d_offset = big_endian ? 2 : 0;
12395
12396 Powerpc_relobj<size, big_endian>* const object
12397 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12398 const unsigned int local_count = object->local_symbol_count();
12399 unsigned int got2_shndx = object->got2_shndx();
12400 Address got2_addend = 0;
12401 if (got2_shndx != 0)
12402 {
12403 got2_addend = object->get_output_section_offset(got2_shndx);
12404 gold_assert(got2_addend != invalid_address);
12405 }
12406
12407 const bool relocatable = parameters->options().relocatable();
12408
12409 unsigned char* pwrite = reloc_view;
12410 bool zap_next = false;
12411 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12412 {
12413 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12414 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12415 continue;
12416
12417 Reltype reloc(prelocs);
12418 Reltype_write reloc_write(pwrite);
12419
12420 Address offset = reloc.get_r_offset();
12421 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12422 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12423 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12424 const unsigned int orig_r_sym = r_sym;
12425 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12426 = reloc.get_r_addend();
12427 const Symbol* gsym = NULL;
12428
12429 if (zap_next)
12430 {
12431 // We could arrange to discard these and other relocs for
12432 // tls optimised sequences in the strategy methods, but for
12433 // now do as BFD ld does.
12434 r_type = elfcpp::R_POWERPC_NONE;
12435 zap_next = false;
12436 }
12437
12438 // Get the new symbol index.
12439 Output_section* os = NULL;
12440 if (r_sym < local_count)
12441 {
12442 switch (strategy)
12443 {
12444 case Relocatable_relocs::RELOC_COPY:
12445 case Relocatable_relocs::RELOC_SPECIAL:
12446 if (r_sym != 0)
12447 {
12448 r_sym = object->symtab_index(r_sym);
12449 gold_assert(r_sym != -1U);
12450 }
12451 break;
12452
12453 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12454 {
12455 // We are adjusting a section symbol. We need to find
12456 // the symbol table index of the section symbol for
12457 // the output section corresponding to input section
12458 // in which this symbol is defined.
12459 gold_assert(r_sym < local_count);
12460 bool is_ordinary;
12461 unsigned int shndx =
12462 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12463 gold_assert(is_ordinary);
12464 os = object->output_section(shndx);
12465 gold_assert(os != NULL);
12466 gold_assert(os->needs_symtab_index());
12467 r_sym = os->symtab_index();
12468 }
12469 break;
12470
12471 default:
12472 gold_unreachable();
12473 }
12474 }
12475 else
12476 {
12477 gsym = object->global_symbol(r_sym);
12478 gold_assert(gsym != NULL);
12479 if (gsym->is_forwarder())
12480 gsym = relinfo->symtab->resolve_forwards(gsym);
12481
12482 gold_assert(gsym->has_symtab_index());
12483 r_sym = gsym->symtab_index();
12484 }
12485
12486 // Get the new offset--the location in the output section where
12487 // this relocation should be applied.
12488 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12489 offset += offset_in_output_section;
12490 else
12491 {
12492 section_offset_type sot_offset =
12493 convert_types<section_offset_type, Address>(offset);
12494 section_offset_type new_sot_offset =
12495 output_section->output_offset(object, relinfo->data_shndx,
12496 sot_offset);
12497 gold_assert(new_sot_offset != -1);
12498 offset = new_sot_offset;
12499 }
12500
12501 // In an object file, r_offset is an offset within the section.
12502 // In an executable or dynamic object, generated by
12503 // --emit-relocs, r_offset is an absolute address.
12504 if (!relocatable)
12505 {
12506 offset += view_address;
12507 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12508 offset -= offset_in_output_section;
12509 }
12510
12511 // Handle the reloc addend based on the strategy.
12512 if (strategy == Relocatable_relocs::RELOC_COPY)
12513 ;
12514 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12515 {
12516 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12517 addend = psymval->value(object, addend);
12518 // In a relocatable link, the symbol value is relative to
12519 // the start of the output section. For a non-relocatable
12520 // link, we need to adjust the addend.
12521 if (!relocatable)
12522 {
12523 gold_assert(os != NULL);
12524 addend -= os->address();
12525 }
12526 }
12527 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12528 {
12529 if (size == 32)
12530 {
12531 if (addend >= 32768)
12532 addend += got2_addend;
12533 }
12534 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12535 {
12536 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12537 addend -= d_offset;
12538 }
12539 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12540 {
12541 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12542 addend -= d_offset + 4;
12543 }
12544 }
12545 else
12546 gold_unreachable();
12547
12548 if (!relocatable)
12549 {
12550 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12551 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12552 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12553 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12554 {
12555 // First instruction of a global dynamic sequence,
12556 // arg setup insn.
12557 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
12558 if (!object->no_tls_marker())
12559 {
12560 bool final = gsym == NULL || gsym->final_value_is_known();
12561 tls_type = this->optimize_tls_gd(final);
12562 }
12563 switch (tls_type)
12564 {
12565 case tls::TLSOPT_TO_IE:
12566 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12567 - elfcpp::R_POWERPC_GOT_TLSGD16);
12568 break;
12569 case tls::TLSOPT_TO_LE:
12570 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12571 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12572 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12573 else
12574 {
12575 r_type = elfcpp::R_POWERPC_NONE;
12576 offset -= d_offset;
12577 }
12578 break;
12579 default:
12580 break;
12581 }
12582 }
12583 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12584 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12585 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12586 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12587 {
12588 // First instruction of a local dynamic sequence,
12589 // arg setup insn.
12590 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
12591 if (!object->no_tls_marker())
12592 tls_type = this->optimize_tls_ld();
12593 if (tls_type == tls::TLSOPT_TO_LE)
12594 {
12595 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12596 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12597 {
12598 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12599 const Output_section* os = relinfo->layout->tls_segment()
12600 ->first_section();
12601 gold_assert(os != NULL);
12602 gold_assert(os->needs_symtab_index());
12603 r_sym = os->symtab_index();
12604 addend = dtp_offset;
12605 }
12606 else
12607 {
12608 r_type = elfcpp::R_POWERPC_NONE;
12609 offset -= d_offset;
12610 }
12611 }
12612 }
12613 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12614 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12615 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12616 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12617 {
12618 // First instruction of initial exec sequence.
12619 bool final = gsym == NULL || gsym->final_value_is_known();
12620 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12621 {
12622 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12623 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12624 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12625 else
12626 {
12627 r_type = elfcpp::R_POWERPC_NONE;
12628 offset -= d_offset;
12629 }
12630 }
12631 }
12632 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12633 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12634 {
12635 // Second instruction of a global dynamic sequence,
12636 // the __tls_get_addr call
12637 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
12638 if (!object->no_tls_marker())
12639 {
12640 bool final = gsym == NULL || gsym->final_value_is_known();
12641 tls_type = this->optimize_tls_gd(final);
12642 }
12643 switch (tls_type)
12644 {
12645 case tls::TLSOPT_TO_IE:
12646 r_type = elfcpp::R_POWERPC_NONE;
12647 zap_next = true;
12648 break;
12649 case tls::TLSOPT_TO_LE:
12650 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12651 offset += d_offset;
12652 zap_next = true;
12653 break;
12654 default:
12655 break;
12656 }
12657 }
12658 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12659 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12660 {
12661 // Second instruction of a local dynamic sequence,
12662 // the __tls_get_addr call
12663 tls::Tls_optimization tls_type = tls::TLSOPT_NONE;
12664 if (!object->no_tls_marker())
12665 tls_type = this->optimize_tls_ld();
12666 if (tls_type == tls::TLSOPT_TO_LE)
12667 {
12668 const Output_section* os = relinfo->layout->tls_segment()
12669 ->first_section();
12670 gold_assert(os != NULL);
12671 gold_assert(os->needs_symtab_index());
12672 r_sym = os->symtab_index();
12673 addend = dtp_offset;
12674 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12675 offset += d_offset;
12676 zap_next = true;
12677 }
12678 }
12679 else if (r_type == elfcpp::R_POWERPC_TLS)
12680 {
12681 // Second instruction of an initial exec sequence
12682 bool final = gsym == NULL || gsym->final_value_is_known();
12683 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12684 {
12685 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12686 offset += d_offset;
12687 }
12688 }
12689 }
12690
12691 reloc_write.put_r_offset(offset);
12692 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12693 reloc_write.put_r_addend(addend);
12694
12695 pwrite += reloc_size;
12696 }
12697
12698 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12699 == reloc_view_size);
12700 }
12701
12702 // Return the value to use for a dynamic symbol which requires special
12703 // treatment. This is how we support equality comparisons of function
12704 // pointers across shared library boundaries, as described in the
12705 // processor specific ABI supplement.
12706
12707 template<int size, bool big_endian>
12708 uint64_t
12709 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12710 {
12711 if (size == 32)
12712 {
12713 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12714 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12715 p != this->stub_tables_.end();
12716 ++p)
12717 {
12718 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12719 = (*p)->find_plt_call_entry(gsym);
12720 if (ent != NULL)
12721 return (*p)->stub_address() + ent->off_;
12722 }
12723 }
12724 else if (this->abiversion() >= 2)
12725 {
12726 Address off = this->glink_section()->find_global_entry(gsym);
12727 if (off != invalid_address)
12728 return this->glink_section()->global_entry_address() + off;
12729 }
12730 gold_unreachable();
12731 }
12732
12733 // Return the PLT address to use for a local symbol.
12734 template<int size, bool big_endian>
12735 uint64_t
12736 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12737 const Relobj* object,
12738 unsigned int symndx) const
12739 {
12740 if (size == 32)
12741 {
12742 const Sized_relobj<size, big_endian>* relobj
12743 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12744 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12745 p != this->stub_tables_.end();
12746 ++p)
12747 {
12748 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12749 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12750 if (ent != NULL)
12751 return (*p)->stub_address() + ent->off_;
12752 }
12753 }
12754 gold_unreachable();
12755 }
12756
12757 // Return the PLT address to use for a global symbol.
12758 template<int size, bool big_endian>
12759 uint64_t
12760 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12761 const Symbol* gsym) const
12762 {
12763 if (size == 32)
12764 {
12765 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12766 p != this->stub_tables_.end();
12767 ++p)
12768 {
12769 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12770 = (*p)->find_plt_call_entry(gsym);
12771 if (ent != NULL)
12772 return (*p)->stub_address() + ent->off_;
12773 }
12774 }
12775 else if (this->abiversion() >= 2)
12776 {
12777 Address off = this->glink_section()->find_global_entry(gsym);
12778 if (off != invalid_address)
12779 return this->glink_section()->global_entry_address() + off;
12780 }
12781 gold_unreachable();
12782 }
12783
12784 // Return the offset to use for the GOT_INDX'th got entry which is
12785 // for a local tls symbol specified by OBJECT, SYMNDX.
12786 template<int size, bool big_endian>
12787 int64_t
12788 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12789 const Relobj* object,
12790 unsigned int symndx,
12791 unsigned int got_indx) const
12792 {
12793 const Powerpc_relobj<size, big_endian>* ppc_object
12794 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12795 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12796 {
12797 for (Got_type got_type = GOT_TYPE_TLSGD;
12798 got_type <= GOT_TYPE_TPREL;
12799 got_type = Got_type(got_type + 1))
12800 if (ppc_object->local_has_got_offset(symndx, got_type))
12801 {
12802 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12803 if (got_type == GOT_TYPE_TLSGD)
12804 off += size / 8;
12805 if (off == got_indx * (size / 8))
12806 {
12807 if (got_type == GOT_TYPE_TPREL)
12808 return -tp_offset;
12809 else
12810 return -dtp_offset;
12811 }
12812 }
12813 }
12814 gold_unreachable();
12815 }
12816
12817 // Return the offset to use for the GOT_INDX'th got entry which is
12818 // for global tls symbol GSYM.
12819 template<int size, bool big_endian>
12820 int64_t
12821 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12822 Symbol* gsym,
12823 unsigned int got_indx) const
12824 {
12825 if (gsym->type() == elfcpp::STT_TLS)
12826 {
12827 for (Got_type got_type = GOT_TYPE_TLSGD;
12828 got_type <= GOT_TYPE_TPREL;
12829 got_type = Got_type(got_type + 1))
12830 if (gsym->has_got_offset(got_type))
12831 {
12832 unsigned int off = gsym->got_offset(got_type);
12833 if (got_type == GOT_TYPE_TLSGD)
12834 off += size / 8;
12835 if (off == got_indx * (size / 8))
12836 {
12837 if (got_type == GOT_TYPE_TPREL)
12838 return -tp_offset;
12839 else
12840 return -dtp_offset;
12841 }
12842 }
12843 }
12844 gold_unreachable();
12845 }
12846
12847 // The selector for powerpc object files.
12848
12849 template<int size, bool big_endian>
12850 class Target_selector_powerpc : public Target_selector
12851 {
12852 public:
12853 Target_selector_powerpc()
12854 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12855 size, big_endian,
12856 (size == 64
12857 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12858 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12859 (size == 64
12860 ? (big_endian ? "elf64ppc" : "elf64lppc")
12861 : (big_endian ? "elf32ppc" : "elf32lppc")))
12862 { }
12863
12864 virtual Target*
12865 do_instantiate_target()
12866 { return new Target_powerpc<size, big_endian>(); }
12867 };
12868
12869 Target_selector_powerpc<32, true> target_selector_ppc32;
12870 Target_selector_powerpc<32, false> target_selector_ppc32le;
12871 Target_selector_powerpc<64, true> target_selector_ppc64;
12872 Target_selector_powerpc<64, false> target_selector_ppc64le;
12873
12874 // Instantiate these constants for -O0
12875 template<int size, bool big_endian>
12876 const typename Output_data_glink<size, big_endian>::Address
12877 Output_data_glink<size, big_endian>::invalid_address;
12878 template<int size, bool big_endian>
12879 const typename Stub_table<size, big_endian>::Address
12880 Stub_table<size, big_endian>::invalid_address;
12881 template<int size, bool big_endian>
12882 const typename Target_powerpc<size, big_endian>::Address
12883 Target_powerpc<size, big_endian>::invalid_address;
12884
12885 } // End anonymous namespace.
This page took 0.30139 seconds and 4 git commands to generate.