gdb: fix value.c build on 32-bits
[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 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
107 {
108 this->set_abiversion(0);
109 }
110
111 ~Powerpc_relobj()
112 { delete this->attributes_section_data_; }
113
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
164 // The .got2 section shndx.
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
169 return this->special_;
170 else
171 return 0;
172 }
173
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
199 if (value != NULL)
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
230 }
231
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
257
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
260 void
261 do_read_relocs(Read_relocs_data*);
262
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
265
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
270 {
271 if (size == 64 && this->opd_shndx() != 0)
272 {
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
278 }
279 return true;
280 }
281
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
285
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
289 add_reference(Relobj* src_obj,
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292 {
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
295 }
296
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301 {
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
306 }
307
308 void
309 process_gc_mark(Symbol_table* symtab)
310 {
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
313 {
314 unsigned int shndx = this->opd_ent_[i].shndx;
315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
316 }
317 }
318
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
322
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
326
327 void
328 set_has_14bit_branch(unsigned int shndx)
329 {
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
333 }
334
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
338
339 void
340 set_stub_table(unsigned int shndx, unsigned int stub_index)
341 {
342 if (shndx >= this->stub_table_index_.size())
343 this->stub_table_index_.resize(shndx + 1, -1);
344 this->stub_table_index_[shndx] = stub_index;
345 }
346
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
349 {
350 if (shndx < this->stub_table_index_.size())
351 {
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
358 }
359 return NULL;
360 }
361
362 void
363 clear_stub_table()
364 {
365 this->stub_table_index_.clear();
366 }
367
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
371
372 int
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
379
380 unsigned int
381 st_other (unsigned int symndx) const
382 {
383 return this->st_other_[symndx];
384 }
385
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
397
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
401
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
406
407 private:
408 struct Opd_ent
409 {
410 unsigned int shndx;
411 bool discard : 1;
412 bool gc_mark : 1;
413 Address off;
414 };
415
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
429
430 // Per object unique identifier
431 uint32_t uniq_;
432
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
435
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
439
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
443
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
450
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
453
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
457
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
460 // relocation on this word. The following vector records the
461 // section and offset specified by these relocations.
462 std::vector<Opd_ent> opd_ent_;
463
464 // References made to this object's .opd section when running
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
467 Access_from access_from_map_;
468
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
471
472 // The stub table to use for a given input section.
473 std::vector<unsigned int> stub_table_index_;
474
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
477
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
480 };
481
482 template<int size, bool big_endian>
483 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484 {
485 public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
493 {
494 this->set_abiversion(0);
495 }
496
497 ~Powerpc_dynobj()
498 { delete this->attributes_section_data_; }
499
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
504
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
508 {
509 return this->opd_shndx_;
510 }
511
512 // The .opd section address.
513 Address
514 opd_address() const
515 {
516 return this->opd_address_;
517 }
518
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
522 {
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
525 }
526
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
530 {
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
537 }
538
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
542 {
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
547 }
548
549 int
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
556
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
561
562 private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
565 {
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
568 { }
569
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
573
574 Address start;
575 Address len;
576 unsigned int shndx;
577 };
578
579 struct Opd_ent
580 {
581 unsigned int shndx;
582 Address off;
583 };
584
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
589
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
593
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
596
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
602
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
605 };
606
607 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608 // base class will emit.
609
610 template<int sh_type, int size, bool big_endian>
611 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612 {
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616 { }
617
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622 };
623
624 // The types of GOT entries needed for this platform.
625 // These values are exposed to the ABI in an incremental link, but
626 // powerpc does not support incremental linking as yet.
627 enum Got_type
628 {
629 GOT_TYPE_STANDARD = 0,
630 GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
631 GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
632 GOT_TYPE_TPREL = 3, // entry for @got@tprel
633 GOT_TYPE_SMALL = 4,
634 GOT_TYPE_SMALL_TLSGD = 5,
635 GOT_TYPE_SMALL_DTPREL = 6,
636 GOT_TYPE_SMALL_TPREL = 7
637 };
638
639 template<int size, bool big_endian>
640 class Target_powerpc : public Sized_target<size, big_endian>
641 {
642 public:
643 typedef
644 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
645 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
646 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
647 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
648 static const Address invalid_address = static_cast<Address>(0) - 1;
649 // Offset of tp and dtp pointers from start of TLS block.
650 static const Address tp_offset = 0x7000;
651 static const Address dtp_offset = 0x8000;
652
653 Target_powerpc()
654 : Sized_target<size, big_endian>(&powerpc_info),
655 got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
656 brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
657 tlsld_got_offset_(-1U),
658 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
659 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
660 plt_localentry0_init_(false), has_localentry0_(false),
661 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
662 relax_failed_(false), relax_fail_count_(0),
663 stub_group_size_(0), savres_section_(0),
664 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
665 attributes_section_data_(NULL),
666 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
667 {
668 }
669
670 // Process the relocations to determine unreferenced sections for
671 // garbage collection.
672 void
673 gc_process_relocs(Symbol_table* symtab,
674 Layout* layout,
675 Sized_relobj_file<size, big_endian>* object,
676 unsigned int data_shndx,
677 unsigned int sh_type,
678 const unsigned char* prelocs,
679 size_t reloc_count,
680 Output_section* output_section,
681 bool needs_special_offset_handling,
682 size_t local_symbol_count,
683 const unsigned char* plocal_symbols);
684
685 // Scan the relocations to look for symbol adjustments.
686 void
687 scan_relocs(Symbol_table* symtab,
688 Layout* layout,
689 Sized_relobj_file<size, big_endian>* object,
690 unsigned int data_shndx,
691 unsigned int sh_type,
692 const unsigned char* prelocs,
693 size_t reloc_count,
694 Output_section* output_section,
695 bool needs_special_offset_handling,
696 size_t local_symbol_count,
697 const unsigned char* plocal_symbols);
698
699 // Map input .toc section to output .got section.
700 const char*
701 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
702 {
703 if (size == 64 && strcmp(name, ".toc") == 0)
704 {
705 *plen = 4;
706 return ".got";
707 }
708 return NULL;
709 }
710
711 // Provide linker defined save/restore functions.
712 void
713 define_save_restore_funcs(Layout*, Symbol_table*);
714
715 // No stubs unless a final link.
716 bool
717 do_may_relax() const
718 { return !parameters->options().relocatable(); }
719
720 bool
721 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
722
723 void
724 do_plt_fde_location(const Output_data*, unsigned char*,
725 uint64_t*, off_t*) const;
726
727 // Stash info about branches, for stub generation.
728 void
729 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
730 unsigned int data_shndx, Address r_offset,
731 unsigned int r_type, unsigned int r_sym, Address addend)
732 {
733 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
734 this->branch_info_.push_back(info);
735 if (r_type == elfcpp::R_POWERPC_REL14
736 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
737 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
738 ppc_object->set_has_14bit_branch(data_shndx);
739 }
740
741 // Return whether the last branch is a plt call, and if so, mark the
742 // branch as having an R_PPC64_TOCSAVE.
743 bool
744 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
745 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
746 {
747 return (size == 64
748 && !this->branch_info_.empty()
749 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
750 r_offset, this, symtab));
751 }
752
753 // Say the given location, that of a nop in a function prologue with
754 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
755 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
756 void
757 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
758 unsigned int shndx, Address offset)
759 {
760 Symbol_location loc;
761 loc.object = ppc_object;
762 loc.shndx = shndx;
763 loc.offset = offset;
764 this->tocsave_loc_.insert(loc);
765 }
766
767 // Accessor
768 const Tocsave_loc*
769 tocsave_loc() const
770 {
771 return &this->tocsave_loc_;
772 }
773
774 void
775 do_define_standard_symbols(Symbol_table*, Layout*);
776
777 // Finalize the sections.
778 void
779 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
780
781 // Return the value to use for a dynamic which requires special
782 // treatment.
783 uint64_t
784 do_dynsym_value(const Symbol*) const;
785
786 // Return the PLT address to use for a local symbol.
787 uint64_t
788 do_plt_address_for_local(const Relobj*, unsigned int) const;
789
790 // Return the PLT address to use for a global symbol.
791 uint64_t
792 do_plt_address_for_global(const Symbol*) const;
793
794 // Return the offset to use for the GOT_INDX'th got entry which is
795 // for a local tls symbol specified by OBJECT, SYMNDX.
796 int64_t
797 do_tls_offset_for_local(const Relobj* object,
798 unsigned int symndx,
799 Output_data_got_base* got,
800 unsigned int got_indx,
801 uint64_t addend) const;
802
803 // Return the offset to use for the GOT_INDX'th got entry which is
804 // for global tls symbol GSYM.
805 int64_t
806 do_tls_offset_for_global(Symbol* gsym,
807 Output_data_got_base* got, unsigned int got_indx,
808 uint64_t addend) const;
809
810 void
811 do_function_location(Symbol_location*) const;
812
813 bool
814 do_can_check_for_function_pointers() const
815 { return true; }
816
817 // Adjust -fsplit-stack code which calls non-split-stack code.
818 void
819 do_calls_non_split(Relobj* object, unsigned int shndx,
820 section_offset_type fnoffset, section_size_type fnsize,
821 const unsigned char* prelocs, size_t reloc_count,
822 unsigned char* view, section_size_type view_size,
823 std::string* from, std::string* to) const;
824
825 // Relocate a section.
826 void
827 relocate_section(const Relocate_info<size, big_endian>*,
828 unsigned int sh_type,
829 const unsigned char* prelocs,
830 size_t reloc_count,
831 Output_section* output_section,
832 bool needs_special_offset_handling,
833 unsigned char* view,
834 Address view_address,
835 section_size_type view_size,
836 const Reloc_symbol_changes*);
837
838 // Scan the relocs during a relocatable link.
839 void
840 scan_relocatable_relocs(Symbol_table* symtab,
841 Layout* layout,
842 Sized_relobj_file<size, big_endian>* object,
843 unsigned int data_shndx,
844 unsigned int sh_type,
845 const unsigned char* prelocs,
846 size_t reloc_count,
847 Output_section* output_section,
848 bool needs_special_offset_handling,
849 size_t local_symbol_count,
850 const unsigned char* plocal_symbols,
851 Relocatable_relocs*);
852
853 // Scan the relocs for --emit-relocs.
854 void
855 emit_relocs_scan(Symbol_table* symtab,
856 Layout* layout,
857 Sized_relobj_file<size, big_endian>* object,
858 unsigned int data_shndx,
859 unsigned int sh_type,
860 const unsigned char* prelocs,
861 size_t reloc_count,
862 Output_section* output_section,
863 bool needs_special_offset_handling,
864 size_t local_symbol_count,
865 const unsigned char* plocal_syms,
866 Relocatable_relocs* rr);
867
868 // Emit relocations for a section.
869 void
870 relocate_relocs(const Relocate_info<size, big_endian>*,
871 unsigned int sh_type,
872 const unsigned char* prelocs,
873 size_t reloc_count,
874 Output_section* output_section,
875 typename elfcpp::Elf_types<size>::Elf_Off
876 offset_in_output_section,
877 unsigned char*,
878 Address view_address,
879 section_size_type,
880 unsigned char* reloc_view,
881 section_size_type reloc_view_size);
882
883 // Return whether SYM is defined by the ABI.
884 bool
885 do_is_defined_by_abi(const Symbol* sym) const
886 {
887 return strcmp(sym->name(), "__tls_get_addr") == 0;
888 }
889
890 // Return the size of the GOT section, for incremental linking
891 section_size_type
892 got_size() const
893 {
894 gold_assert(this->got_ != NULL);
895 return this->got_->data_size() + (this->biggot_
896 ? this->biggot_->data_size() : 0);
897 }
898
899 // Get the PLT section.
900 const Output_data_plt_powerpc<size, big_endian>*
901 plt_section() const
902 {
903 gold_assert(this->plt_ != NULL);
904 return this->plt_;
905 }
906
907 // Get the IPLT section.
908 const Output_data_plt_powerpc<size, big_endian>*
909 iplt_section() const
910 {
911 gold_assert(this->iplt_ != NULL);
912 return this->iplt_;
913 }
914
915 // Get the LPLT section.
916 const Output_data_plt_powerpc<size, big_endian>*
917 lplt_section() const
918 {
919 return this->lplt_;
920 }
921
922 // Return the plt offset and section for the given global sym.
923 Address
924 plt_off(const Symbol* gsym,
925 const Output_data_plt_powerpc<size, big_endian>** sec) const
926 {
927 if (gsym->type() == elfcpp::STT_GNU_IFUNC
928 && gsym->can_use_relative_reloc(false))
929 *sec = this->iplt_section();
930 else
931 *sec = this->plt_section();
932 return gsym->plt_offset();
933 }
934
935 // Return the plt offset and section for the given local sym.
936 Address
937 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
938 unsigned int local_sym_index,
939 const Output_data_plt_powerpc<size, big_endian>** sec) const
940 {
941 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
942 if (lsym->is_ifunc_symbol())
943 *sec = this->iplt_section();
944 else
945 *sec = this->lplt_section();
946 return relobj->local_plt_offset(local_sym_index);
947 }
948
949 // Get the .glink section.
950 const Output_data_glink<size, big_endian>*
951 glink_section() const
952 {
953 gold_assert(this->glink_ != NULL);
954 return this->glink_;
955 }
956
957 Output_data_glink<size, big_endian>*
958 glink_section()
959 {
960 gold_assert(this->glink_ != NULL);
961 return this->glink_;
962 }
963
964 bool has_glink() const
965 { return this->glink_ != NULL; }
966
967 // Get the GOT section.
968 const Output_data_got_powerpc<size, big_endian>*
969 got_section(Got_type got_type) const
970 {
971 gold_assert(this->got_ != NULL);
972 if (size == 32 || (got_type & GOT_TYPE_SMALL))
973 return this->got_;
974 gold_assert(this->biggot_ != NULL);
975 return this->biggot_;
976 }
977
978 // Get the GOT section, creating it if necessary.
979 Output_data_got_powerpc<size, big_endian>*
980 got_section(Symbol_table*, Layout*, Got_type);
981
982 // The toc/got pointer reg will be set to this value.
983 Address
984 toc_pointer() const
985 {
986 return this->got_->address() + this->got_->g_o_t();
987 }
988
989 // Offset of base used to access the GOT/TOC relative to the GOT section.
990 Address
991 got_base_offset(Got_type got_type) const
992 {
993 if (size == 32 || (got_type & GOT_TYPE_SMALL))
994 return this->got_->g_o_t();
995 return this->toc_pointer() - this->biggot_->address();
996 }
997
998 Object*
999 do_make_elf_object(const std::string&, Input_file*, off_t,
1000 const elfcpp::Ehdr<size, big_endian>&);
1001
1002 // Return the number of entries in the GOT.
1003 unsigned int
1004 got_entry_count() const
1005 {
1006 if (this->got_ == NULL)
1007 return 0;
1008 return this->got_size() / (size / 8);
1009 }
1010
1011 // Return the number of entries in the PLT.
1012 unsigned int
1013 plt_entry_count() const;
1014
1015 // Return the offset of the first non-reserved PLT entry.
1016 unsigned int
1017 first_plt_entry_offset() const
1018 {
1019 if (size == 32)
1020 return 0;
1021 if (this->abiversion() >= 2)
1022 return 16;
1023 return 24;
1024 }
1025
1026 // Return the size of each PLT entry.
1027 unsigned int
1028 plt_entry_size() const
1029 {
1030 if (size == 32)
1031 return 4;
1032 if (this->abiversion() >= 2)
1033 return 8;
1034 return 24;
1035 }
1036
1037 Output_data_save_res<size, big_endian>*
1038 savres_section() const
1039 {
1040 return this->savres_section_;
1041 }
1042
1043 // Add any special sections for this symbol to the gc work list.
1044 // For powerpc64, this adds the code section of a function
1045 // descriptor.
1046 void
1047 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1048
1049 // Handle target specific gc actions when adding a gc reference from
1050 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1051 // and DST_OFF. For powerpc64, this adds a referenc to the code
1052 // section of a function descriptor.
1053 void
1054 do_gc_add_reference(Symbol_table* symtab,
1055 Relobj* src_obj,
1056 unsigned int src_shndx,
1057 Relobj* dst_obj,
1058 unsigned int dst_shndx,
1059 Address dst_off) const;
1060
1061 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1062 const Stub_tables&
1063 stub_tables() const
1064 { return this->stub_tables_; }
1065
1066 const Output_data_brlt_powerpc<size, big_endian>*
1067 brlt_section() const
1068 { return this->brlt_section_; }
1069
1070 void
1071 add_branch_lookup_table(Address to)
1072 {
1073 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1074 this->branch_lookup_table_.insert(std::make_pair(to, off));
1075 }
1076
1077 Address
1078 find_branch_lookup_table(Address to)
1079 {
1080 typename Branch_lookup_table::const_iterator p
1081 = this->branch_lookup_table_.find(to);
1082 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1083 }
1084
1085 void
1086 write_branch_lookup_table(unsigned char *oview)
1087 {
1088 for (typename Branch_lookup_table::const_iterator p
1089 = this->branch_lookup_table_.begin();
1090 p != this->branch_lookup_table_.end();
1091 ++p)
1092 {
1093 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1094 }
1095 }
1096
1097 // Wrapper used after relax to define a local symbol in output data,
1098 // from the end if value < 0.
1099 void
1100 define_local(Symbol_table* symtab, const char* name,
1101 Output_data* od, Address value, unsigned int symsize)
1102 {
1103 Symbol* sym
1104 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1105 od, value, symsize, elfcpp::STT_NOTYPE,
1106 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1107 static_cast<Signed_address>(value) < 0,
1108 false);
1109 // We are creating this symbol late, so need to fix up things
1110 // done early in Layout::finalize.
1111 sym->set_dynsym_index(-1U);
1112 }
1113
1114 void
1115 set_power10_relocs()
1116 {
1117 this->power10_relocs_ = true;
1118 }
1119
1120 bool
1121 power10_stubs() const
1122 {
1123 return (this->power10_relocs_
1124 && (parameters->options().power10_stubs_enum()
1125 != General_options::POWER10_STUBS_NO));
1126 }
1127
1128 bool
1129 power10_stubs_auto() const
1130 {
1131 return (parameters->options().power10_stubs_enum()
1132 == General_options::POWER10_STUBS_AUTO);
1133 }
1134
1135 bool
1136 plt_thread_safe() const
1137 { return this->plt_thread_safe_; }
1138
1139 bool
1140 plt_localentry0() const
1141 { return this->plt_localentry0_; }
1142
1143 bool
1144 has_localentry0() const
1145 { return this->has_localentry0_; }
1146
1147 void
1148 set_has_localentry0()
1149 {
1150 this->has_localentry0_ = true;
1151 }
1152
1153 bool
1154 is_elfv2_localentry0(const Symbol* gsym) const
1155 {
1156 return (size == 64
1157 && this->abiversion() >= 2
1158 && this->plt_localentry0()
1159 && gsym->type() == elfcpp::STT_FUNC
1160 && gsym->is_defined()
1161 && gsym->nonvis() >> 3 == 0
1162 && !gsym->non_zero_localentry());
1163 }
1164
1165 bool
1166 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1167 unsigned int r_sym) const
1168 {
1169 const Powerpc_relobj<size, big_endian>* ppc_object
1170 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1171
1172 if (size == 64
1173 && this->abiversion() >= 2
1174 && this->plt_localentry0()
1175 && ppc_object->st_other(r_sym) >> 5 == 0)
1176 {
1177 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1178 bool is_ordinary;
1179 if (!psymval->is_ifunc_symbol()
1180 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1181 && is_ordinary)
1182 return true;
1183 }
1184 return false;
1185 }
1186
1187 bool
1188 tprel_opt() const
1189 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
1190
1191 void
1192 set_no_tprel_opt()
1193 { this->no_tprel_opt_ = true; }
1194
1195 // Remember any symbols seen with non-zero localentry, even those
1196 // not providing a definition
1197 bool
1198 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1199 const char*)
1200 {
1201 if (size == 64)
1202 {
1203 unsigned char st_other = sym.get_st_other();
1204 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1205 to->set_non_zero_localentry();
1206 }
1207 // We haven't resolved anything, continue normal processing.
1208 return false;
1209 }
1210
1211 int
1212 abiversion() const
1213 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1214
1215 void
1216 set_abiversion(int ver)
1217 {
1218 elfcpp::Elf_Word flags = this->processor_specific_flags();
1219 flags &= ~elfcpp::EF_PPC64_ABI;
1220 flags |= ver & elfcpp::EF_PPC64_ABI;
1221 this->set_processor_specific_flags(flags);
1222 }
1223
1224 Symbol*
1225 tls_get_addr_opt() const
1226 { return this->tls_get_addr_opt_; }
1227
1228 Symbol*
1229 tls_get_addr() const
1230 { return this->tls_get_addr_; }
1231
1232 // If optimizing __tls_get_addr calls, whether this is the
1233 // "__tls_get_addr" symbol.
1234 bool
1235 is_tls_get_addr_opt(const Symbol* gsym) const
1236 {
1237 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1238 || gsym == this->tls_get_addr_opt_);
1239 }
1240
1241 bool
1242 replace_tls_get_addr(const Symbol* gsym) const
1243 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1244
1245 void
1246 set_has_tls_get_addr_opt()
1247 { this->has_tls_get_addr_opt_ = true; }
1248
1249 // Offset to toc save stack slot
1250 int
1251 stk_toc() const
1252 { return this->abiversion() < 2 ? 40 : 24; }
1253
1254 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1255 // so use the CR save slot. Used only by __tls_get_addr call stub,
1256 // relying on __tls_get_addr not saving CR itself.
1257 int
1258 stk_linker() const
1259 { return this->abiversion() < 2 ? 32 : 8; }
1260
1261 // Merge object attributes from input object with those in the output.
1262 void
1263 merge_object_attributes(const Object*, const Attributes_section_data*);
1264
1265 private:
1266
1267 class Track_tls
1268 {
1269 public:
1270 enum Tls_get_addr
1271 {
1272 NOT_EXPECTED = 0,
1273 EXPECTED = 1,
1274 SKIP = 2,
1275 NORMAL = 3
1276 };
1277
1278 Track_tls()
1279 : tls_get_addr_state_(NOT_EXPECTED),
1280 relinfo_(NULL), relnum_(0), r_offset_(0)
1281 { }
1282
1283 ~Track_tls()
1284 {
1285 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1286 this->missing();
1287 }
1288
1289 void
1290 missing(void)
1291 {
1292 if (this->relinfo_ != NULL)
1293 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1294 _("missing expected __tls_get_addr call"));
1295 }
1296
1297 void
1298 expect_tls_get_addr_call(
1299 const Relocate_info<size, big_endian>* relinfo,
1300 size_t relnum,
1301 Address r_offset)
1302 {
1303 this->tls_get_addr_state_ = EXPECTED;
1304 this->relinfo_ = relinfo;
1305 this->relnum_ = relnum;
1306 this->r_offset_ = r_offset;
1307 }
1308
1309 void
1310 expect_tls_get_addr_call()
1311 { this->tls_get_addr_state_ = EXPECTED; }
1312
1313 void
1314 skip_next_tls_get_addr_call()
1315 {this->tls_get_addr_state_ = SKIP; }
1316
1317 Tls_get_addr
1318 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1319 unsigned int r_type, const Symbol* gsym)
1320 {
1321 bool is_tls_call
1322 = ((r_type == elfcpp::R_POWERPC_REL24
1323 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1324 || r_type == elfcpp::R_PPC_PLTREL24
1325 || is_plt16_reloc<size>(r_type)
1326 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1327 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1328 || r_type == elfcpp::R_POWERPC_PLTSEQ
1329 || r_type == elfcpp::R_POWERPC_PLTCALL
1330 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1331 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1332 && gsym != NULL
1333 && (gsym == target->tls_get_addr()
1334 || gsym == target->tls_get_addr_opt()));
1335 Tls_get_addr last_tls = this->tls_get_addr_state_;
1336 this->tls_get_addr_state_ = NOT_EXPECTED;
1337 if (is_tls_call && last_tls != EXPECTED)
1338 return last_tls;
1339 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1340 {
1341 this->missing();
1342 return EXPECTED;
1343 }
1344 return NORMAL;
1345 }
1346
1347 private:
1348 // What we're up to regarding calls to __tls_get_addr.
1349 // On powerpc, the branch and link insn making a call to
1350 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1351 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1352 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1353 // The marker relocation always comes first, and has the same
1354 // symbol as the reloc on the insn setting up the __tls_get_addr
1355 // argument. This ties the arg setup insn with the call insn,
1356 // allowing ld to safely optimize away the call. We check that
1357 // every call to __tls_get_addr has a marker relocation, and that
1358 // every marker relocation is on a call to __tls_get_addr.
1359 Tls_get_addr tls_get_addr_state_;
1360 // Info about the last reloc for error message.
1361 const Relocate_info<size, big_endian>* relinfo_;
1362 size_t relnum_;
1363 Address r_offset_;
1364 };
1365
1366 // The class which scans relocations.
1367 class Scan : protected Track_tls
1368 {
1369 public:
1370 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1371
1372 Scan()
1373 : Track_tls(), issued_non_pic_error_(false)
1374 { }
1375
1376 static inline int
1377 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1378
1379 inline void
1380 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1381 Sized_relobj_file<size, big_endian>* object,
1382 unsigned int data_shndx,
1383 Output_section* output_section,
1384 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1385 const elfcpp::Sym<size, big_endian>& lsym,
1386 bool is_discarded);
1387
1388 inline void
1389 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1390 Sized_relobj_file<size, big_endian>* object,
1391 unsigned int data_shndx,
1392 Output_section* output_section,
1393 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1394 Symbol* gsym);
1395
1396 inline bool
1397 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1398 Target_powerpc* ,
1399 Sized_relobj_file<size, big_endian>* relobj,
1400 unsigned int ,
1401 Output_section* ,
1402 const elfcpp::Rela<size, big_endian>& ,
1403 unsigned int r_type,
1404 const elfcpp::Sym<size, big_endian>&)
1405 {
1406 // PowerPC64 .opd is not folded, so any identical function text
1407 // may be folded and we'll still keep function addresses distinct.
1408 // That means no reloc is of concern here.
1409 if (size == 64)
1410 {
1411 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1412 <Powerpc_relobj<size, big_endian>*>(relobj);
1413 if (ppcobj->abiversion() == 1)
1414 return false;
1415 }
1416 // For 32-bit and ELFv2, conservatively assume anything but calls to
1417 // function code might be taking the address of the function.
1418 return !is_branch_reloc<size>(r_type);
1419 }
1420
1421 inline bool
1422 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1423 Target_powerpc* ,
1424 Sized_relobj_file<size, big_endian>* relobj,
1425 unsigned int ,
1426 Output_section* ,
1427 const elfcpp::Rela<size, big_endian>& ,
1428 unsigned int r_type,
1429 Symbol*)
1430 {
1431 // As above.
1432 if (size == 64)
1433 {
1434 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1435 <Powerpc_relobj<size, big_endian>*>(relobj);
1436 if (ppcobj->abiversion() == 1)
1437 return false;
1438 }
1439 return !is_branch_reloc<size>(r_type);
1440 }
1441
1442 static bool
1443 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1444 Sized_relobj_file<size, big_endian>* object,
1445 unsigned int r_type, bool report_err);
1446
1447 private:
1448 static void
1449 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1450 unsigned int r_type);
1451
1452 static void
1453 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1454 unsigned int r_type, Symbol*);
1455
1456 static void
1457 generate_tls_call(Symbol_table* symtab, Layout* layout,
1458 Target_powerpc* target);
1459
1460 void
1461 check_non_pic(Relobj*, unsigned int r_type);
1462
1463 // Whether we have issued an error about a non-PIC compilation.
1464 bool issued_non_pic_error_;
1465 };
1466
1467 bool
1468 symval_for_branch(const Symbol_table* symtab,
1469 const Sized_symbol<size>* gsym,
1470 Powerpc_relobj<size, big_endian>* object,
1471 Address *value, unsigned int *dest_shndx);
1472
1473 // The class which implements relocation.
1474 class Relocate : protected Track_tls
1475 {
1476 public:
1477 // Use 'at' branch hints when true, 'y' when false.
1478 // FIXME maybe: set this with an option.
1479 static const bool is_isa_v2 = true;
1480
1481 Relocate()
1482 : Track_tls()
1483 { }
1484
1485 // Do a relocation. Return false if the caller should not issue
1486 // any warnings about this relocation.
1487 inline bool
1488 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1489 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1490 const Sized_symbol<size>*, const Symbol_value<size>*,
1491 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1492 section_size_type);
1493 };
1494
1495 class Relocate_comdat_behavior
1496 {
1497 public:
1498 // Decide what the linker should do for relocations that refer to
1499 // discarded comdat sections.
1500 inline Comdat_behavior
1501 get(const char* name)
1502 {
1503 gold::Default_comdat_behavior default_behavior;
1504 Comdat_behavior ret = default_behavior.get(name);
1505 if (ret == CB_ERROR)
1506 {
1507 if (size == 32
1508 && (strcmp(name, ".fixup") == 0
1509 || strcmp(name, ".got2") == 0))
1510 ret = CB_IGNORE;
1511 if (size == 64
1512 && (strcmp(name, ".opd") == 0
1513 || strcmp(name, ".toc") == 0
1514 || strcmp(name, ".toc1") == 0))
1515 ret = CB_IGNORE;
1516 }
1517 return ret;
1518 }
1519 };
1520
1521 // Optimize the TLS relocation type based on what we know about the
1522 // symbol. IS_FINAL is true if the final address of this symbol is
1523 // known at link time.
1524
1525 tls::Tls_optimization
1526 optimize_tls_gd(bool is_final)
1527 {
1528 // If we are generating a shared library, then we can't do anything
1529 // in the linker.
1530 if (parameters->options().shared()
1531 || !parameters->options().tls_optimize())
1532 return tls::TLSOPT_NONE;
1533
1534 if (!is_final)
1535 return tls::TLSOPT_TO_IE;
1536 return tls::TLSOPT_TO_LE;
1537 }
1538
1539 tls::Tls_optimization
1540 optimize_tls_ld()
1541 {
1542 if (parameters->options().shared()
1543 || !parameters->options().tls_optimize())
1544 return tls::TLSOPT_NONE;
1545
1546 return tls::TLSOPT_TO_LE;
1547 }
1548
1549 tls::Tls_optimization
1550 optimize_tls_ie(bool is_final)
1551 {
1552 if (!is_final
1553 || parameters->options().shared()
1554 || !parameters->options().tls_optimize())
1555 return tls::TLSOPT_NONE;
1556
1557 return tls::TLSOPT_TO_LE;
1558 }
1559
1560 // Create glink.
1561 void
1562 make_glink_section(Layout*);
1563
1564 // Create the PLT section.
1565 void
1566 make_plt_section(Symbol_table*, Layout*);
1567
1568 void
1569 make_iplt_section(Symbol_table*, Layout*);
1570
1571 void
1572 make_lplt_section(Layout*);
1573
1574 void
1575 make_brlt_section(Layout*);
1576
1577 // Create a PLT entry for a global symbol.
1578 void
1579 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1580
1581 // Create a PLT entry for a local IFUNC symbol.
1582 void
1583 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1584 Sized_relobj_file<size, big_endian>*,
1585 unsigned int);
1586
1587 // Create a PLT entry for a local non-IFUNC symbol.
1588 void
1589 make_local_plt_entry(Layout*,
1590 Sized_relobj_file<size, big_endian>*,
1591 unsigned int);
1592
1593
1594 // Create a GOT entry for local dynamic __tls_get_addr.
1595 unsigned int
1596 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1597 Sized_relobj_file<size, big_endian>* object);
1598
1599 unsigned int
1600 tlsld_got_offset() const
1601 {
1602 return this->tlsld_got_offset_;
1603 }
1604
1605 // Get the dynamic reloc section, creating it if necessary.
1606 Reloc_section*
1607 rela_dyn_section(Layout*);
1608
1609 // Similarly, but for ifunc symbols get the one for ifunc.
1610 Reloc_section*
1611 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1612
1613 // Copy a relocation against a global symbol.
1614 void
1615 copy_reloc(Symbol_table* symtab, Layout* layout,
1616 Sized_relobj_file<size, big_endian>* object,
1617 unsigned int shndx, Output_section* output_section,
1618 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1619 {
1620 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1621 this->copy_relocs_.copy_reloc(symtab, layout,
1622 symtab->get_sized_symbol<size>(sym),
1623 object, shndx, output_section,
1624 r_type, reloc.get_r_offset(),
1625 reloc.get_r_addend(),
1626 this->rela_dyn_section(layout));
1627 }
1628
1629 // Look over all the input sections, deciding where to place stubs.
1630 void
1631 group_sections(Layout*, const Task*, bool);
1632
1633 // Sort output sections by address.
1634 struct Sort_sections
1635 {
1636 bool
1637 operator()(const Output_section* sec1, const Output_section* sec2)
1638 { return sec1->address() < sec2->address(); }
1639 };
1640
1641 class Branch_info
1642 {
1643 public:
1644 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1645 unsigned int data_shndx,
1646 Address r_offset,
1647 unsigned int r_type,
1648 unsigned int r_sym,
1649 Address addend)
1650 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1651 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1652 { }
1653
1654 ~Branch_info()
1655 { }
1656
1657 // Return whether this branch is going via a plt call stub, and if
1658 // so, mark it as having an R_PPC64_TOCSAVE.
1659 bool
1660 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1661 unsigned int shndx, Address offset,
1662 Target_powerpc* target, Symbol_table* symtab);
1663
1664 // If this branch needs a plt call stub, or a long branch stub, make one.
1665 bool
1666 make_stub(Stub_table<size, big_endian>*,
1667 Stub_table<size, big_endian>*,
1668 Symbol_table*) const;
1669
1670 private:
1671 // The branch location..
1672 Powerpc_relobj<size, big_endian>* object_;
1673 unsigned int shndx_;
1674 Address offset_;
1675 // ..and the branch type and destination.
1676 unsigned int r_type_ : 31;
1677 unsigned int tocsave_ : 1;
1678 unsigned int r_sym_;
1679 Address addend_;
1680 };
1681
1682 // Information about this specific target which we pass to the
1683 // general Target structure.
1684 static Target::Target_info powerpc_info;
1685
1686 // The small GOT section used by ppc32, and by ppc64 for entries that
1687 // must be addresseed +/-32k from the got pointer.
1688 Output_data_got_powerpc<size, big_endian>* got_;
1689 // Another GOT section used for entries that can be addressed +/- 2G
1690 // from the got pointer.
1691 Output_data_got_powerpc<size, big_endian>* biggot_;
1692
1693 // The PLT section. This is a container for a table of addresses,
1694 // and their relocations. Each address in the PLT has a dynamic
1695 // relocation (R_*_JMP_SLOT) and each address will have a
1696 // corresponding entry in .glink for lazy resolution of the PLT.
1697 // ppc32 initialises the PLT to point at the .glink entry, while
1698 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1699 // linker adds a stub that loads the PLT entry into ctr then
1700 // branches to ctr. There may be more than one stub for each PLT
1701 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1702 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1703 Output_data_plt_powerpc<size, big_endian>* plt_;
1704 // The IPLT section. Like plt_, this is a container for a table of
1705 // addresses and their relocations, specifically for STT_GNU_IFUNC
1706 // functions that resolve locally (STT_GNU_IFUNC functions that
1707 // don't resolve locally go in PLT). Unlike plt_, these have no
1708 // entry in .glink for lazy resolution, and the relocation section
1709 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1710 // the relocation section may contain relocations against
1711 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1712 // relocation section will appear at the end of other dynamic
1713 // relocations, so that ld.so applies these relocations after other
1714 // dynamic relocations. In a static executable, the relocation
1715 // section is emitted and marked with __rela_iplt_start and
1716 // __rela_iplt_end symbols.
1717 Output_data_plt_powerpc<size, big_endian>* iplt_;
1718 // A PLT style section for local, non-ifunc symbols
1719 Output_data_plt_powerpc<size, big_endian>* lplt_;
1720 // Section holding long branch destinations.
1721 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1722 // The .glink section.
1723 Output_data_glink<size, big_endian>* glink_;
1724 // The dynamic reloc section.
1725 Reloc_section* rela_dyn_;
1726 // Relocs saved to avoid a COPY reloc.
1727 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1728 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1729 unsigned int tlsld_got_offset_;
1730
1731 Stub_tables stub_tables_;
1732 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1733 Branch_lookup_table branch_lookup_table_;
1734
1735 typedef std::vector<Branch_info> Branches;
1736 Branches branch_info_;
1737 Tocsave_loc tocsave_loc_;
1738
1739 bool power10_relocs_;
1740 bool plt_thread_safe_;
1741 bool plt_localentry0_;
1742 bool plt_localentry0_init_;
1743 bool has_localentry0_;
1744 bool has_tls_get_addr_opt_;
1745 bool no_tprel_opt_;
1746
1747 bool relax_failed_;
1748 int relax_fail_count_;
1749 int32_t stub_group_size_;
1750
1751 Output_data_save_res<size, big_endian> *savres_section_;
1752
1753 // The "__tls_get_addr" symbol, if present
1754 Symbol* tls_get_addr_;
1755 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1756 Symbol* tls_get_addr_opt_;
1757
1758 // Attributes in output.
1759 Attributes_section_data* attributes_section_data_;
1760
1761 // Last input file to change various attribute tags
1762 const char* last_fp_;
1763 const char* last_ld_;
1764 const char* last_vec_;
1765 const char* last_struct_;
1766 };
1767
1768 template<>
1769 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1770 {
1771 32, // size
1772 true, // is_big_endian
1773 elfcpp::EM_PPC, // machine_code
1774 false, // has_make_symbol
1775 false, // has_resolve
1776 false, // has_code_fill
1777 true, // is_default_stack_executable
1778 false, // can_icf_inline_merge_sections
1779 '\0', // wrap_char
1780 "/usr/lib/ld.so.1", // dynamic_linker
1781 0x10000000, // default_text_segment_address
1782 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1783 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1784 false, // isolate_execinstr
1785 0, // rosegment_gap
1786 elfcpp::SHN_UNDEF, // small_common_shndx
1787 elfcpp::SHN_UNDEF, // large_common_shndx
1788 0, // small_common_section_flags
1789 0, // large_common_section_flags
1790 NULL, // attributes_section
1791 NULL, // attributes_vendor
1792 "_start", // entry_symbol_name
1793 32, // hash_entry_size
1794 elfcpp::SHT_PROGBITS, // unwind_section_type
1795 };
1796
1797 template<>
1798 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1799 {
1800 32, // size
1801 false, // is_big_endian
1802 elfcpp::EM_PPC, // machine_code
1803 false, // has_make_symbol
1804 false, // has_resolve
1805 false, // has_code_fill
1806 true, // is_default_stack_executable
1807 false, // can_icf_inline_merge_sections
1808 '\0', // wrap_char
1809 "/usr/lib/ld.so.1", // dynamic_linker
1810 0x10000000, // default_text_segment_address
1811 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1812 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1813 false, // isolate_execinstr
1814 0, // rosegment_gap
1815 elfcpp::SHN_UNDEF, // small_common_shndx
1816 elfcpp::SHN_UNDEF, // large_common_shndx
1817 0, // small_common_section_flags
1818 0, // large_common_section_flags
1819 NULL, // attributes_section
1820 NULL, // attributes_vendor
1821 "_start", // entry_symbol_name
1822 32, // hash_entry_size
1823 elfcpp::SHT_PROGBITS, // unwind_section_type
1824 };
1825
1826 template<>
1827 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1828 {
1829 64, // size
1830 true, // is_big_endian
1831 elfcpp::EM_PPC64, // machine_code
1832 false, // has_make_symbol
1833 true, // has_resolve
1834 false, // has_code_fill
1835 false, // is_default_stack_executable
1836 false, // can_icf_inline_merge_sections
1837 '\0', // wrap_char
1838 "/usr/lib/ld.so.1", // dynamic_linker
1839 0x10000000, // default_text_segment_address
1840 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1841 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1842 false, // isolate_execinstr
1843 0, // rosegment_gap
1844 elfcpp::SHN_UNDEF, // small_common_shndx
1845 elfcpp::SHN_UNDEF, // large_common_shndx
1846 0, // small_common_section_flags
1847 0, // large_common_section_flags
1848 NULL, // attributes_section
1849 NULL, // attributes_vendor
1850 "_start", // entry_symbol_name
1851 32, // hash_entry_size
1852 elfcpp::SHT_PROGBITS, // unwind_section_type
1853 };
1854
1855 template<>
1856 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1857 {
1858 64, // size
1859 false, // is_big_endian
1860 elfcpp::EM_PPC64, // machine_code
1861 false, // has_make_symbol
1862 true, // has_resolve
1863 false, // has_code_fill
1864 false, // is_default_stack_executable
1865 false, // can_icf_inline_merge_sections
1866 '\0', // wrap_char
1867 "/usr/lib/ld.so.1", // dynamic_linker
1868 0x10000000, // default_text_segment_address
1869 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1870 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1871 false, // isolate_execinstr
1872 0, // rosegment_gap
1873 elfcpp::SHN_UNDEF, // small_common_shndx
1874 elfcpp::SHN_UNDEF, // large_common_shndx
1875 0, // small_common_section_flags
1876 0, // large_common_section_flags
1877 NULL, // attributes_section
1878 NULL, // attributes_vendor
1879 "_start", // entry_symbol_name
1880 32, // hash_entry_size
1881 elfcpp::SHT_PROGBITS, // unwind_section_type
1882 };
1883
1884 template<int size>
1885 inline bool
1886 is_branch_reloc(unsigned int r_type)
1887 {
1888 return (r_type == elfcpp::R_POWERPC_REL24
1889 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1890 || r_type == elfcpp::R_PPC_PLTREL24
1891 || r_type == elfcpp::R_PPC_LOCAL24PC
1892 || r_type == elfcpp::R_POWERPC_REL14
1893 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1894 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1895 || r_type == elfcpp::R_POWERPC_ADDR24
1896 || r_type == elfcpp::R_POWERPC_ADDR14
1897 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1898 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1899 }
1900
1901 // Reloc resolves to plt entry.
1902 template<int size>
1903 inline bool
1904 is_plt16_reloc(unsigned int r_type)
1905 {
1906 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1907 || r_type == elfcpp::R_POWERPC_PLT16_HI
1908 || r_type == elfcpp::R_POWERPC_PLT16_HA
1909 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1910 }
1911
1912 // GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
1913 inline bool
1914 is_got_reloc(unsigned int r_type)
1915 {
1916 return (r_type == elfcpp::R_POWERPC_GOT16
1917 || r_type == elfcpp::R_POWERPC_GOT16_LO
1918 || r_type == elfcpp::R_POWERPC_GOT16_HI
1919 || r_type == elfcpp::R_POWERPC_GOT16_HA
1920 || r_type == elfcpp::R_PPC64_GOT16_DS
1921 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1922 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1923 }
1924
1925 // If INSN is an opcode that may be used with an @tls operand, return
1926 // the transformed insn for TLS optimisation, otherwise return 0. If
1927 // REG is non-zero only match an insn with RB or RA equal to REG.
1928 uint32_t
1929 at_tls_transform(uint32_t insn, unsigned int reg)
1930 {
1931 if ((insn & (0x3f << 26)) != 31 << 26)
1932 return 0;
1933
1934 unsigned int rtra;
1935 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1936 rtra = insn & ((1 << 26) - (1 << 16));
1937 else if (((insn >> 16) & 0x1f) == reg)
1938 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1939 else
1940 return 0;
1941
1942 if ((insn & (0x3ff << 1)) == 266 << 1)
1943 // add -> addi
1944 insn = 14 << 26;
1945 else if ((insn & (0x1f << 1)) == 23 << 1
1946 && ((insn & (0x1f << 6)) < 14 << 6
1947 || ((insn & (0x1f << 6)) >= 16 << 6
1948 && (insn & (0x1f << 6)) < 24 << 6)))
1949 // load and store indexed -> dform
1950 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1951 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1952 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1953 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1954 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1955 // lwax -> lwa
1956 insn = (58 << 26) | 2;
1957 else
1958 return 0;
1959 insn |= rtra;
1960 return insn;
1961 }
1962
1963
1964 template<int size, bool big_endian>
1965 class Powerpc_relocate_functions
1966 {
1967 public:
1968 enum Overflow_check
1969 {
1970 CHECK_NONE,
1971 CHECK_SIGNED,
1972 CHECK_UNSIGNED,
1973 CHECK_BITFIELD,
1974 CHECK_LOW_INSN,
1975 CHECK_HIGH_INSN
1976 };
1977
1978 enum Status
1979 {
1980 STATUS_OK,
1981 STATUS_OVERFLOW
1982 };
1983
1984 private:
1985 typedef Powerpc_relocate_functions<size, big_endian> This;
1986 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1987 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1988
1989 template<int valsize>
1990 static inline bool
1991 has_overflow_signed(Address value)
1992 {
1993 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1994 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1995 limit <<= ((valsize - 1) >> 1);
1996 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1997 return value + limit > (limit << 1) - 1;
1998 }
1999
2000 template<int valsize>
2001 static inline bool
2002 has_overflow_unsigned(Address value)
2003 {
2004 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2005 limit <<= ((valsize - 1) >> 1);
2006 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2007 return value > (limit << 1) - 1;
2008 }
2009
2010 template<int valsize>
2011 static inline bool
2012 has_overflow_bitfield(Address value)
2013 {
2014 return (has_overflow_unsigned<valsize>(value)
2015 && has_overflow_signed<valsize>(value));
2016 }
2017
2018 template<int valsize>
2019 static inline Status
2020 overflowed(Address value, Overflow_check overflow)
2021 {
2022 if (overflow == CHECK_SIGNED)
2023 {
2024 if (has_overflow_signed<valsize>(value))
2025 return STATUS_OVERFLOW;
2026 }
2027 else if (overflow == CHECK_UNSIGNED)
2028 {
2029 if (has_overflow_unsigned<valsize>(value))
2030 return STATUS_OVERFLOW;
2031 }
2032 else if (overflow == CHECK_BITFIELD)
2033 {
2034 if (has_overflow_bitfield<valsize>(value))
2035 return STATUS_OVERFLOW;
2036 }
2037 return STATUS_OK;
2038 }
2039
2040 // Do a simple RELA relocation
2041 template<int fieldsize, int valsize>
2042 static inline Status
2043 rela(unsigned char* view, Address value, Overflow_check overflow)
2044 {
2045 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2046 Valtype* wv = reinterpret_cast<Valtype*>(view);
2047 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2048 return overflowed<valsize>(value, overflow);
2049 }
2050
2051 template<int fieldsize, int valsize>
2052 static inline Status
2053 rela(unsigned char* view,
2054 unsigned int right_shift,
2055 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2056 Address value,
2057 Overflow_check overflow)
2058 {
2059 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2060 Valtype* wv = reinterpret_cast<Valtype*>(view);
2061 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2062 if (overflow == CHECK_SIGNED)
2063 value = static_cast<SignedAddress>(value) >> right_shift;
2064 else
2065 value = value >> right_shift;
2066 Valtype reloc = value;
2067 val &= ~dst_mask;
2068 reloc &= dst_mask;
2069 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2070 return overflowed<valsize>(value, overflow);
2071 }
2072
2073 // Do a simple RELA relocation, unaligned.
2074 template<int fieldsize, int valsize>
2075 static inline Status
2076 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2077 {
2078 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2079 return overflowed<valsize>(value, overflow);
2080 }
2081
2082 template<int fieldsize, int valsize>
2083 static inline Status
2084 rela_ua(unsigned char* view,
2085 unsigned int right_shift,
2086 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2087 Address value,
2088 Overflow_check overflow)
2089 {
2090 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2091 Valtype;
2092 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2093 if (overflow == CHECK_SIGNED)
2094 value = static_cast<SignedAddress>(value) >> right_shift;
2095 else
2096 value = value >> right_shift;
2097 Valtype reloc = value;
2098 val &= ~dst_mask;
2099 reloc &= dst_mask;
2100 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2101 return overflowed<valsize>(value, overflow);
2102 }
2103
2104 public:
2105 // R_PPC64_ADDR64: (Symbol + Addend)
2106 static inline void
2107 addr64(unsigned char* view, Address value)
2108 { This::template rela<64,64>(view, value, CHECK_NONE); }
2109
2110 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2111 static inline void
2112 addr64_u(unsigned char* view, Address value)
2113 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2114
2115 // R_POWERPC_ADDR32: (Symbol + Addend)
2116 static inline Status
2117 addr32(unsigned char* view, Address value, Overflow_check overflow)
2118 { return This::template rela<32,32>(view, value, overflow); }
2119
2120 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2121 static inline Status
2122 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2123 { return This::template rela_ua<32,32>(view, value, overflow); }
2124
2125 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2126 static inline Status
2127 addr24(unsigned char* view, Address value, Overflow_check overflow)
2128 {
2129 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2130 value, overflow);
2131 if (overflow != CHECK_NONE && (value & 3) != 0)
2132 stat = STATUS_OVERFLOW;
2133 return stat;
2134 }
2135
2136 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2137 static inline Status
2138 addr16(unsigned char* view, Address value, Overflow_check overflow)
2139 { return This::template rela<16,16>(view, value, overflow); }
2140
2141 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2142 static inline Status
2143 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2144 { return This::template rela_ua<16,16>(view, value, overflow); }
2145
2146 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2147 static inline Status
2148 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2149 {
2150 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2151 if ((value & 3) != 0)
2152 stat = STATUS_OVERFLOW;
2153 return stat;
2154 }
2155
2156 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2157 static inline Status
2158 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2159 {
2160 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2161 if ((value & 15) != 0)
2162 stat = STATUS_OVERFLOW;
2163 return stat;
2164 }
2165
2166 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2167 static inline void
2168 addr16_hi(unsigned char* view, Address value)
2169 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2170
2171 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2172 static inline void
2173 addr16_ha(unsigned char* view, Address value)
2174 { This::addr16_hi(view, value + 0x8000); }
2175
2176 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2177 static inline void
2178 addr16_hi2(unsigned char* view, Address value)
2179 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2180
2181 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2182 static inline void
2183 addr16_ha2(unsigned char* view, Address value)
2184 { This::addr16_hi2(view, value + 0x8000); }
2185
2186 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2187 static inline void
2188 addr16_hi3(unsigned char* view, Address value)
2189 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2190
2191 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2192 static inline void
2193 addr16_ha3(unsigned char* view, Address value)
2194 { This::addr16_hi3(view, value + 0x8000); }
2195
2196 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2197 static inline Status
2198 addr14(unsigned char* view, Address value, Overflow_check overflow)
2199 {
2200 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2201 if (overflow != CHECK_NONE && (value & 3) != 0)
2202 stat = STATUS_OVERFLOW;
2203 return stat;
2204 }
2205
2206 // R_POWERPC_REL16DX_HA
2207 static inline Status
2208 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2209 {
2210 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2211 Valtype* wv = reinterpret_cast<Valtype*>(view);
2212 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2213 value += 0x8000;
2214 value = static_cast<SignedAddress>(value) >> 16;
2215 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2216 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2217 return overflowed<16>(value, overflow);
2218 }
2219
2220 // R_PPC64_D34
2221 static inline Status
2222 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2223 {
2224 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2225 value, overflow);
2226 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2227 return stat;
2228 }
2229
2230 // R_PPC64_D34_HI30
2231 static inline void
2232 addr34_hi(unsigned char *view, uint64_t value)
2233 { This::addr34(view, value >> 34, CHECK_NONE);}
2234
2235 // R_PPC64_D34_HA30
2236 static inline void
2237 addr34_ha(unsigned char *view, uint64_t value)
2238 { This::addr34_hi(view, value + (1ULL << 33));}
2239
2240 // R_PPC64_D28
2241 static inline Status
2242 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2243 {
2244 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2245 value, overflow);
2246 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2247 return stat;
2248 }
2249
2250 // R_PPC64_ADDR16_HIGHER34
2251 static inline void
2252 addr16_higher34(unsigned char* view, uint64_t value)
2253 { This::addr16(view, value >> 34, CHECK_NONE); }
2254
2255 // R_PPC64_ADDR16_HIGHERA34
2256 static inline void
2257 addr16_highera34(unsigned char* view, uint64_t value)
2258 { This::addr16_higher34(view, value + (1ULL << 33)); }
2259
2260 // R_PPC64_ADDR16_HIGHEST34
2261 static inline void
2262 addr16_highest34(unsigned char* view, uint64_t value)
2263 { This::addr16(view, value >> 50, CHECK_NONE); }
2264
2265 // R_PPC64_ADDR16_HIGHESTA34
2266 static inline void
2267 addr16_highesta34(unsigned char* view, uint64_t value)
2268 { This::addr16_highest34(view, value + (1ULL << 33)); }
2269 };
2270
2271 // Set ABI version for input and output.
2272
2273 template<int size, bool big_endian>
2274 void
2275 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2276 {
2277 this->e_flags_ |= ver;
2278 if (this->abiversion() != 0)
2279 {
2280 Target_powerpc<size, big_endian>* target =
2281 static_cast<Target_powerpc<size, big_endian>*>(
2282 parameters->sized_target<size, big_endian>());
2283 if (target->abiversion() == 0)
2284 target->set_abiversion(this->abiversion());
2285 else if (target->abiversion() != this->abiversion())
2286 gold_error(_("%s: ABI version %d is not compatible "
2287 "with ABI version %d output"),
2288 this->name().c_str(),
2289 this->abiversion(), target->abiversion());
2290
2291 }
2292 }
2293
2294 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2295 // relocatable object, if such sections exists.
2296
2297 template<int size, bool big_endian>
2298 bool
2299 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2300 Read_symbols_data* sd)
2301 {
2302 const unsigned char* const pshdrs = sd->section_headers->data();
2303 const unsigned char* namesu = sd->section_names->data();
2304 const char* names = reinterpret_cast<const char*>(namesu);
2305 section_size_type names_size = sd->section_names_size;
2306 const unsigned char* s;
2307
2308 s = this->template find_shdr<size, big_endian>(pshdrs,
2309 size == 32 ? ".got2" : ".opd",
2310 names, names_size, NULL);
2311 if (s != NULL)
2312 {
2313 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2314 this->special_ = ndx;
2315 if (size == 64)
2316 {
2317 if (this->abiversion() == 0)
2318 this->set_abiversion(1);
2319 else if (this->abiversion() > 1)
2320 gold_error(_("%s: .opd invalid in abiv%d"),
2321 this->name().c_str(), this->abiversion());
2322 }
2323 }
2324 if (size == 64)
2325 {
2326 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2327 names, names_size, NULL);
2328 if (s != NULL)
2329 {
2330 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2331 this->relatoc_ = ndx;
2332 typename elfcpp::Shdr<size, big_endian> shdr(s);
2333 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2334 }
2335 }
2336 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2337 }
2338
2339 // Examine .rela.opd to build info about function entry points.
2340
2341 template<int size, bool big_endian>
2342 void
2343 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2344 size_t reloc_count,
2345 const unsigned char* prelocs,
2346 const unsigned char* plocal_syms)
2347 {
2348 if (size == 64)
2349 {
2350 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2351 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2352 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2353 Address expected_off = 0;
2354 bool regular = true;
2355 unsigned int opd_ent_size = 0;
2356
2357 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2358 {
2359 Reltype reloc(prelocs);
2360 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2361 = reloc.get_r_info();
2362 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2363 if (r_type == elfcpp::R_PPC64_ADDR64)
2364 {
2365 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2366 typename elfcpp::Elf_types<size>::Elf_Addr value;
2367 bool is_ordinary;
2368 unsigned int shndx;
2369 if (r_sym < this->local_symbol_count())
2370 {
2371 typename elfcpp::Sym<size, big_endian>
2372 lsym(plocal_syms + r_sym * sym_size);
2373 shndx = lsym.get_st_shndx();
2374 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2375 value = lsym.get_st_value();
2376 }
2377 else
2378 shndx = this->symbol_section_and_value(r_sym, &value,
2379 &is_ordinary);
2380 this->set_opd_ent(reloc.get_r_offset(), shndx,
2381 value + reloc.get_r_addend());
2382 if (i == 2)
2383 {
2384 expected_off = reloc.get_r_offset();
2385 opd_ent_size = expected_off;
2386 }
2387 else if (expected_off != reloc.get_r_offset())
2388 regular = false;
2389 expected_off += opd_ent_size;
2390 }
2391 else if (r_type == elfcpp::R_PPC64_TOC)
2392 {
2393 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2394 regular = false;
2395 }
2396 else
2397 {
2398 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2399 this->name().c_str(), r_type);
2400 regular = false;
2401 }
2402 }
2403 if (reloc_count <= 2)
2404 opd_ent_size = this->section_size(this->opd_shndx());
2405 if (opd_ent_size != 24 && opd_ent_size != 16)
2406 regular = false;
2407 if (!regular)
2408 {
2409 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2410 this->name().c_str());
2411 opd_ent_size = 0;
2412 }
2413 }
2414 }
2415
2416 // Returns true if a code sequence loading the TOC entry at VALUE
2417 // relative to the TOC pointer can be converted into code calculating
2418 // a TOC pointer relative offset.
2419 // If so, the TOC pointer relative offset is stored to VALUE.
2420
2421 template<int size, bool big_endian>
2422 bool
2423 Powerpc_relobj<size, big_endian>::make_toc_relative(
2424 Target_powerpc<size, big_endian>* target,
2425 Address* value)
2426 {
2427 if (size != 64)
2428 return false;
2429
2430 // With -mcmodel=medium code it is quite possible to have
2431 // toc-relative relocs referring to objects outside the TOC.
2432 // Don't try to look at a non-existent TOC.
2433 if (this->toc_shndx() == 0
2434 || this->output_section(this->toc_shndx()) == 0)
2435 return false;
2436
2437 // Convert VALUE back to an address by adding got_base (see below),
2438 // then to an offset in the TOC by subtracting the TOC output
2439 // section address and the TOC output offset.
2440 Address off = (*value + target->toc_pointer()
2441 - this->output_section(this->toc_shndx())->address()
2442 - this->output_section_offset(this->toc_shndx()));
2443 // Is this offset in the TOC? -mcmodel=medium code may be using
2444 // TOC relative access to variables outside the TOC. Those of
2445 // course can't be optimized. We also don't try to optimize code
2446 // that is using a different object's TOC.
2447 if (off >= this->section_size(this->toc_shndx()))
2448 return false;
2449
2450 if (this->no_toc_opt(off))
2451 return false;
2452
2453 section_size_type vlen;
2454 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2455 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2456 // The TOC pointer
2457 Address got_base = target->toc_pointer();
2458 addr -= got_base;
2459 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2460 return false;
2461
2462 *value = addr;
2463 return true;
2464 }
2465
2466 template<int size, bool big_endian>
2467 bool
2468 Powerpc_relobj<size, big_endian>::make_got_relative(
2469 Target_powerpc<size, big_endian>* target,
2470 const Symbol_value<size>* psymval,
2471 Address addend,
2472 Address* value)
2473 {
2474 Address addr = psymval->value(this, addend);
2475 Address got_base = target->toc_pointer();
2476 addr -= got_base;
2477 if (addr + 0x80008000 > 0xffffffff)
2478 return false;
2479
2480 *value = addr;
2481 return true;
2482 }
2483
2484 // Perform the Sized_relobj_file method, then set up opd info from
2485 // .opd relocs.
2486
2487 template<int size, bool big_endian>
2488 void
2489 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2490 {
2491 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2492 if (size == 64)
2493 {
2494 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2495 p != rd->relocs.end();
2496 ++p)
2497 {
2498 if (p->data_shndx == this->opd_shndx())
2499 {
2500 uint64_t opd_size = this->section_size(this->opd_shndx());
2501 gold_assert(opd_size == static_cast<size_t>(opd_size));
2502 if (opd_size != 0)
2503 {
2504 this->init_opd(opd_size);
2505 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2506 rd->local_symbols->data());
2507 }
2508 break;
2509 }
2510 }
2511 }
2512 }
2513
2514 // Read the symbols then set up st_other vector.
2515
2516 template<int size, bool big_endian>
2517 void
2518 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2519 {
2520 this->base_read_symbols(sd);
2521 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2522 return;
2523 if (size == 64)
2524 {
2525 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2526 const unsigned char* const pshdrs = sd->section_headers->data();
2527 const unsigned int loccount = this->do_local_symbol_count();
2528 if (loccount != 0)
2529 {
2530 this->st_other_.resize(loccount);
2531 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2532 off_t locsize = loccount * sym_size;
2533 const unsigned int symtab_shndx = this->symtab_shndx();
2534 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2535 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2536 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2537 locsize, true, false);
2538 psyms += sym_size;
2539 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2540 {
2541 elfcpp::Sym<size, big_endian> sym(psyms);
2542 unsigned char st_other = sym.get_st_other();
2543 this->st_other_[i] = st_other;
2544 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2545 {
2546 if (this->abiversion() == 0)
2547 this->set_abiversion(2);
2548 else if (this->abiversion() < 2)
2549 gold_error(_("%s: local symbol %d has invalid st_other"
2550 " for ABI version 1"),
2551 this->name().c_str(), i);
2552 }
2553 }
2554 }
2555 }
2556
2557 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2558 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2559 bool merge_attributes = false;
2560 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2561 {
2562 elfcpp::Shdr<size, big_endian> shdr(ps);
2563 switch (shdr.get_sh_type())
2564 {
2565 case elfcpp::SHT_GNU_ATTRIBUTES:
2566 {
2567 gold_assert(this->attributes_section_data_ == NULL);
2568 section_offset_type section_offset = shdr.get_sh_offset();
2569 section_size_type section_size =
2570 convert_to_section_size_type(shdr.get_sh_size());
2571 const unsigned char* view =
2572 this->get_view(section_offset, section_size, true, false);
2573 this->attributes_section_data_ =
2574 new Attributes_section_data(view, section_size);
2575 }
2576 break;
2577
2578 case elfcpp::SHT_SYMTAB:
2579 {
2580 // Sometimes an object has no contents except the section
2581 // name string table and an empty symbol table with the
2582 // undefined symbol. We don't want to merge
2583 // processor-specific flags from such an object.
2584 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2585 elfcpp::Elf_sizes<size>::sym_size;
2586 if (shdr.get_sh_size() > sym_size)
2587 merge_attributes = true;
2588 }
2589 break;
2590
2591 case elfcpp::SHT_STRTAB:
2592 break;
2593
2594 default:
2595 merge_attributes = true;
2596 break;
2597 }
2598 }
2599
2600 if (!merge_attributes)
2601 {
2602 // Should rarely happen.
2603 delete this->attributes_section_data_;
2604 this->attributes_section_data_ = NULL;
2605 }
2606 }
2607
2608 template<int size, bool big_endian>
2609 void
2610 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2611 {
2612 this->e_flags_ |= ver;
2613 if (this->abiversion() != 0)
2614 {
2615 Target_powerpc<size, big_endian>* target =
2616 static_cast<Target_powerpc<size, big_endian>*>(
2617 parameters->sized_target<size, big_endian>());
2618 if (target->abiversion() == 0)
2619 target->set_abiversion(this->abiversion());
2620 else if (target->abiversion() != this->abiversion())
2621 gold_error(_("%s: ABI version %d is not compatible "
2622 "with ABI version %d output"),
2623 this->name().c_str(),
2624 this->abiversion(), target->abiversion());
2625
2626 }
2627 }
2628
2629 // Call Sized_dynobj::base_read_symbols to read the symbols then
2630 // read .opd from a dynamic object, filling in opd_ent_ vector,
2631
2632 template<int size, bool big_endian>
2633 void
2634 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2635 {
2636 this->base_read_symbols(sd);
2637 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2638 const unsigned char* ps =
2639 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2640 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2641 {
2642 elfcpp::Shdr<size, big_endian> shdr(ps);
2643 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2644 {
2645 section_offset_type section_offset = shdr.get_sh_offset();
2646 section_size_type section_size =
2647 convert_to_section_size_type(shdr.get_sh_size());
2648 const unsigned char* view =
2649 this->get_view(section_offset, section_size, true, false);
2650 this->attributes_section_data_ =
2651 new Attributes_section_data(view, section_size);
2652 break;
2653 }
2654 }
2655 if (size == 64)
2656 {
2657 const unsigned char* const pshdrs = sd->section_headers->data();
2658 const unsigned char* namesu = sd->section_names->data();
2659 const char* names = reinterpret_cast<const char*>(namesu);
2660 const unsigned char* s = NULL;
2661 const unsigned char* opd;
2662 section_size_type opd_size;
2663
2664 // Find and read .opd section.
2665 while (1)
2666 {
2667 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2668 sd->section_names_size,
2669 s);
2670 if (s == NULL)
2671 return;
2672
2673 typename elfcpp::Shdr<size, big_endian> shdr(s);
2674 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2675 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2676 {
2677 if (this->abiversion() == 0)
2678 this->set_abiversion(1);
2679 else if (this->abiversion() > 1)
2680 gold_error(_("%s: .opd invalid in abiv%d"),
2681 this->name().c_str(), this->abiversion());
2682
2683 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2684 this->opd_address_ = shdr.get_sh_addr();
2685 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2686 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2687 true, false);
2688 break;
2689 }
2690 }
2691
2692 // Build set of executable sections.
2693 // Using a set is probably overkill. There is likely to be only
2694 // a few executable sections, typically .init, .text and .fini,
2695 // and they are generally grouped together.
2696 typedef std::set<Sec_info> Exec_sections;
2697 Exec_sections exec_sections;
2698 s = pshdrs;
2699 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2700 {
2701 typename elfcpp::Shdr<size, big_endian> shdr(s);
2702 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2703 && ((shdr.get_sh_flags()
2704 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2705 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2706 && shdr.get_sh_size() != 0)
2707 {
2708 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2709 shdr.get_sh_size(), i));
2710 }
2711 }
2712 if (exec_sections.empty())
2713 return;
2714
2715 // Look over the OPD entries. This is complicated by the fact
2716 // that some binaries will use two-word entries while others
2717 // will use the standard three-word entries. In most cases
2718 // the third word (the environment pointer for languages like
2719 // Pascal) is unused and will be zero. If the third word is
2720 // used it should not be pointing into executable sections,
2721 // I think.
2722 this->init_opd(opd_size);
2723 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2724 {
2725 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2726 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2727 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2728 if (val == 0)
2729 // Chances are that this is the third word of an OPD entry.
2730 continue;
2731 typename Exec_sections::const_iterator e
2732 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2733 if (e != exec_sections.begin())
2734 {
2735 --e;
2736 if (e->start <= val && val < e->start + e->len)
2737 {
2738 // We have an address in an executable section.
2739 // VAL ought to be the function entry, set it up.
2740 this->set_opd_ent(p - opd, e->shndx, val);
2741 // Skip second word of OPD entry, the TOC pointer.
2742 p += 8;
2743 }
2744 }
2745 // If we didn't match any executable sections, we likely
2746 // have a non-zero third word in the OPD entry.
2747 }
2748 }
2749 }
2750
2751 // Relocate sections.
2752
2753 template<int size, bool big_endian>
2754 void
2755 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2756 const Symbol_table* symtab, const Layout* layout,
2757 const unsigned char* pshdrs, Output_file* of,
2758 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2759 {
2760 unsigned int start = 1;
2761 if (size == 64
2762 && this->relatoc_ != 0
2763 && !parameters->options().relocatable())
2764 {
2765 // Relocate .toc first.
2766 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2767 this->relatoc_, this->relatoc_);
2768 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2769 1, this->relatoc_ - 1);
2770 start = this->relatoc_ + 1;
2771 }
2772 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2773 start, this->shnum() - 1);
2774
2775 if (!parameters->options().output_is_position_independent())
2776 {
2777 Target_powerpc<size, big_endian>* target
2778 = static_cast<Target_powerpc<size, big_endian>*>(
2779 parameters->sized_target<size, big_endian>());
2780 if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2781 {
2782 const section_size_type offset = target->lplt_section()->offset();
2783 const section_size_type oview_size
2784 = convert_to_section_size_type(target->lplt_section()->data_size());
2785 unsigned char* const oview = of->get_output_view(offset, oview_size);
2786
2787 bool modified = false;
2788 unsigned int nsyms = this->local_symbol_count();
2789 for (unsigned int i = 0; i < nsyms; i++)
2790 if (this->local_has_plt_offset(i))
2791 {
2792 Address value = this->local_symbol_value(i, 0);
2793 size_t off = this->local_plt_offset(i);
2794 elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2795 modified = true;
2796 }
2797 if (modified)
2798 of->write_output_view(offset, oview_size, oview);
2799 }
2800 }
2801 }
2802
2803 // Set up some symbols.
2804
2805 template<int size, bool big_endian>
2806 void
2807 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2808 Symbol_table* symtab,
2809 Layout* layout)
2810 {
2811 if (size == 32)
2812 {
2813 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2814 // undefined when scanning relocs (and thus requires
2815 // non-relative dynamic relocs). The proper value will be
2816 // updated later.
2817 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2818 if (gotsym != NULL && gotsym->is_undefined())
2819 {
2820 Target_powerpc<size, big_endian>* target =
2821 static_cast<Target_powerpc<size, big_endian>*>(
2822 parameters->sized_target<size, big_endian>());
2823 Output_data_got_powerpc<size, big_endian>* got
2824 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2825 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2826 Symbol_table::PREDEFINED,
2827 got, 0, 0,
2828 elfcpp::STT_OBJECT,
2829 elfcpp::STB_LOCAL,
2830 elfcpp::STV_HIDDEN, 0,
2831 false, false);
2832 }
2833
2834 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2835 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2836 if (sdasym != NULL && sdasym->is_undefined())
2837 {
2838 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2839 Output_section* os
2840 = layout->add_output_section_data(".sdata", 0,
2841 elfcpp::SHF_ALLOC
2842 | elfcpp::SHF_WRITE,
2843 sdata, ORDER_SMALL_DATA, false);
2844 symtab->define_in_output_data("_SDA_BASE_", NULL,
2845 Symbol_table::PREDEFINED,
2846 os, 32768, 0, elfcpp::STT_OBJECT,
2847 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2848 0, false, false);
2849 }
2850 }
2851 else
2852 {
2853 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2854 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2855 if (gotsym != NULL && gotsym->is_undefined())
2856 {
2857 Target_powerpc<size, big_endian>* target =
2858 static_cast<Target_powerpc<size, big_endian>*>(
2859 parameters->sized_target<size, big_endian>());
2860 Output_data_got_powerpc<size, big_endian>* got
2861 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2862 symtab->define_in_output_data(".TOC.", NULL,
2863 Symbol_table::PREDEFINED,
2864 got, 0x8000, 0,
2865 elfcpp::STT_OBJECT,
2866 elfcpp::STB_LOCAL,
2867 elfcpp::STV_HIDDEN, 0,
2868 false, false);
2869 }
2870 }
2871
2872 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2873 if (parameters->options().tls_get_addr_optimize()
2874 && this->tls_get_addr_ != NULL
2875 && this->tls_get_addr_->in_reg())
2876 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2877 if (this->tls_get_addr_opt_ != NULL)
2878 {
2879 if (this->tls_get_addr_->is_undefined()
2880 || this->tls_get_addr_->is_from_dynobj())
2881 {
2882 // Make it seem as if references to __tls_get_addr are
2883 // really to __tls_get_addr_opt, so the latter symbol is
2884 // made dynamic, not the former.
2885 this->tls_get_addr_->clear_in_reg();
2886 this->tls_get_addr_opt_->set_in_reg();
2887 }
2888 // We have a non-dynamic definition for __tls_get_addr.
2889 // Make __tls_get_addr_opt the same, if it does not already have
2890 // a non-dynamic definition.
2891 else if (this->tls_get_addr_opt_->is_undefined()
2892 || this->tls_get_addr_opt_->is_from_dynobj())
2893 {
2894 Sized_symbol<size>* from
2895 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2896 Sized_symbol<size>* to
2897 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2898 symtab->clone<size>(to, from);
2899 }
2900 }
2901 }
2902
2903 // Set up PowerPC target specific relobj.
2904
2905 template<int size, bool big_endian>
2906 Object*
2907 Target_powerpc<size, big_endian>::do_make_elf_object(
2908 const std::string& name,
2909 Input_file* input_file,
2910 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2911 {
2912 int et = ehdr.get_e_type();
2913 // ET_EXEC files are valid input for --just-symbols/-R,
2914 // and we treat them as relocatable objects.
2915 if (et == elfcpp::ET_REL
2916 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2917 {
2918 Powerpc_relobj<size, big_endian>* obj =
2919 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2920 obj->setup();
2921 return obj;
2922 }
2923 else if (et == elfcpp::ET_DYN)
2924 {
2925 Powerpc_dynobj<size, big_endian>* obj =
2926 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2927 obj->setup();
2928 return obj;
2929 }
2930 else
2931 {
2932 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2933 return NULL;
2934 }
2935 }
2936
2937 template<int size, bool big_endian>
2938 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2939 {
2940 public:
2941 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2942 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2943
2944 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2945 Got_type got_type)
2946 : Output_data_got<size, big_endian>(),
2947 symtab_(symtab), layout_(layout),
2948 header_ent_cnt_(size == 32 ? 3 : 1),
2949 header_index_(size == 32 ? 0x2000 : -1u)
2950 {
2951 if (size == 64)
2952 this->set_addralign(256);
2953 if (size == 64 && (got_type & GOT_TYPE_SMALL))
2954 this->make_header();
2955 }
2956
2957 // Override all the Output_data_got methods we use so as to first call
2958 // reserve_ent().
2959 bool
2960 add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
2961 {
2962 this->reserve_ent();
2963 return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2964 addend);
2965 }
2966
2967 bool
2968 add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
2969 {
2970 this->reserve_ent();
2971 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2972 addend);
2973 }
2974
2975 bool
2976 add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2977 { return this->add_global_plt(gsym, got_type, addend); }
2978
2979 void
2980 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2981 Output_data_reloc_generic* rel_dyn,
2982 unsigned int r_type, uint64_t addend)
2983 {
2984 this->reserve_ent();
2985 Output_data_got<size, big_endian>::
2986 add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
2987 }
2988
2989 void
2990 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2991 Output_data_reloc_generic* rel_dyn,
2992 unsigned int r_type_1, unsigned int r_type_2,
2993 uint64_t addend)
2994 {
2995 if (gsym->has_got_offset(got_type))
2996 return;
2997
2998 this->reserve_ent(2);
2999 Output_data_got<size, big_endian>::
3000 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
3001 addend);
3002 }
3003
3004 bool
3005 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
3006 uint64_t addend)
3007 {
3008 this->reserve_ent();
3009 return Output_data_got<size, big_endian>::add_local(object, sym_index,
3010 got_type, addend);
3011 }
3012
3013 bool
3014 add_local_plt(Relobj* object, unsigned int sym_index,
3015 unsigned int got_type, uint64_t addend)
3016 {
3017 this->reserve_ent();
3018 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
3019 got_type, addend);
3020 }
3021
3022 bool
3023 add_local_tls(Relobj* object, unsigned int sym_index,
3024 unsigned int got_type, uint64_t addend)
3025 { return this->add_local_plt(object, sym_index, got_type, addend); }
3026
3027 void
3028 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3029 unsigned int got_type,
3030 Output_data_reloc_generic* rel_dyn,
3031 unsigned int r_type, uint64_t addend)
3032 {
3033 if (object->local_has_got_offset(sym_index, got_type, addend))
3034 return;
3035
3036 this->reserve_ent(2);
3037 Output_data_got<size, big_endian>::
3038 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
3039 }
3040
3041 unsigned int
3042 add_constant(Valtype constant)
3043 {
3044 this->reserve_ent();
3045 return Output_data_got<size, big_endian>::add_constant(constant);
3046 }
3047
3048 unsigned int
3049 add_constant_pair(Valtype c1, Valtype c2)
3050 {
3051 this->reserve_ent(2);
3052 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3053 }
3054
3055 // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
3056 unsigned int
3057 g_o_t() const
3058 {
3059 if (size == 32)
3060 return this->got_offset(this->header_index_);
3061 else if (this->header_index_ != -1u)
3062 return this->got_offset(this->header_index_) + 0x8000;
3063 else
3064 gold_unreachable();
3065 }
3066
3067 // Ensure our GOT has a header.
3068 void
3069 set_final_data_size()
3070 {
3071 if (size == 32 && this->header_ent_cnt_ != 0)
3072 this->make_header();
3073 Output_data_got<size, big_endian>::set_final_data_size();
3074 }
3075
3076 // First word of GOT header needs some values that are not
3077 // handled by Output_data_got so poke them in here.
3078 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3079 void
3080 do_write(Output_file* of)
3081 {
3082 if (this->header_index_ != -1u)
3083 {
3084 Valtype val = 0;
3085 if (size == 32 && this->layout_->dynamic_data() != NULL)
3086 val = this->layout_->dynamic_section()->address();
3087 if (size == 64)
3088 val = this->address() + this->g_o_t();
3089 this->replace_constant(this->header_index_, val);
3090 }
3091 Output_data_got<size, big_endian>::do_write(of);
3092 }
3093
3094 private:
3095 void
3096 reserve_ent(unsigned int cnt = 1)
3097 {
3098 if (size != 32 || this->header_ent_cnt_ == 0)
3099 return;
3100 if (this->num_entries() + cnt > this->header_index_)
3101 this->make_header();
3102 }
3103
3104 void
3105 make_header()
3106 {
3107 this->header_ent_cnt_ = 0;
3108 this->header_index_ = this->num_entries();
3109 if (size == 32)
3110 {
3111 Output_data_got<size, big_endian>::add_constant(0);
3112 Output_data_got<size, big_endian>::add_constant(0);
3113 Output_data_got<size, big_endian>::add_constant(0);
3114
3115 // Define _GLOBAL_OFFSET_TABLE_ at the header
3116 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3117 if (gotsym != NULL)
3118 {
3119 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3120 sym->set_value(this->g_o_t());
3121 }
3122 else
3123 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3124 Symbol_table::PREDEFINED,
3125 this, this->g_o_t(), 0,
3126 elfcpp::STT_OBJECT,
3127 elfcpp::STB_LOCAL,
3128 elfcpp::STV_HIDDEN, 0,
3129 false, false);
3130 }
3131 else
3132 Output_data_got<size, big_endian>::add_constant(0);
3133 }
3134
3135 // Stashed pointers.
3136 Symbol_table* symtab_;
3137 Layout* layout_;
3138
3139 // GOT header size.
3140 unsigned int header_ent_cnt_;
3141 // GOT header index.
3142 unsigned int header_index_;
3143 };
3144
3145 // Get the GOT section, creating it if necessary.
3146
3147 template<int size, bool big_endian>
3148 Output_data_got_powerpc<size, big_endian>*
3149 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3150 Layout* layout,
3151 Got_type got_type)
3152 {
3153 if (this->got_ == NULL)
3154 {
3155 gold_assert(symtab != NULL && layout != NULL);
3156
3157 this->got_
3158 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3159 GOT_TYPE_SMALL);
3160
3161 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3162 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3163 this->got_, ORDER_DATA, false);
3164 }
3165
3166 if (size == 32 || (got_type & GOT_TYPE_SMALL))
3167 return this->got_;
3168
3169 if (this->biggot_ == NULL)
3170 {
3171 this->biggot_
3172 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3173 GOT_TYPE_STANDARD);
3174
3175 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3176 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3177 this->biggot_, ORDER_DATA, false);
3178 }
3179
3180 return this->biggot_;
3181 }
3182
3183 // Get the dynamic reloc section, creating it if necessary.
3184
3185 template<int size, bool big_endian>
3186 typename Target_powerpc<size, big_endian>::Reloc_section*
3187 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3188 {
3189 if (this->rela_dyn_ == NULL)
3190 {
3191 gold_assert(layout != NULL);
3192 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3193 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3194 elfcpp::SHF_ALLOC, this->rela_dyn_,
3195 ORDER_DYNAMIC_RELOCS, false);
3196 }
3197 return this->rela_dyn_;
3198 }
3199
3200 // Similarly, but for ifunc symbols get the one for ifunc.
3201
3202 template<int size, bool big_endian>
3203 typename Target_powerpc<size, big_endian>::Reloc_section*
3204 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3205 Layout* layout,
3206 bool for_ifunc)
3207 {
3208 if (!for_ifunc)
3209 return this->rela_dyn_section(layout);
3210
3211 if (this->iplt_ == NULL)
3212 this->make_iplt_section(symtab, layout);
3213 return this->iplt_->rel_plt();
3214 }
3215
3216 class Stub_control
3217 {
3218 public:
3219 // Determine the stub group size. The group size is the absolute
3220 // value of the parameter --stub-group-size. If --stub-group-size
3221 // is passed a negative value, we restrict stubs to be always after
3222 // the stubbed branches.
3223 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3224 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3225 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3226 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3227 owner_(NULL), output_section_(NULL)
3228 {
3229 }
3230
3231 // Return true iff input section can be handled by current stub
3232 // group.
3233 bool
3234 can_add_to_stub_group(Output_section* o,
3235 const Output_section::Input_section* i,
3236 bool has14);
3237
3238 const Output_section::Input_section*
3239 owner()
3240 { return owner_; }
3241
3242 Output_section*
3243 output_section()
3244 { return output_section_; }
3245
3246 void
3247 set_output_and_owner(Output_section* o,
3248 const Output_section::Input_section* i)
3249 {
3250 this->output_section_ = o;
3251 this->owner_ = i;
3252 }
3253
3254 private:
3255 typedef enum
3256 {
3257 // Initial state.
3258 NO_GROUP,
3259 // Adding group sections before the stubs.
3260 FINDING_STUB_SECTION,
3261 // Adding group sections after the stubs.
3262 HAS_STUB_SECTION
3263 } State;
3264
3265 uint32_t stub_group_size_;
3266 bool stubs_always_after_branch_;
3267 bool suppress_size_errors_;
3268 // True if a stub group can serve multiple output sections.
3269 bool multi_os_;
3270 State state_;
3271 // Current max size of group. Starts at stub_group_size_ but is
3272 // reduced to stub_group_size_/1024 on seeing a section with
3273 // external conditional branches.
3274 uint32_t group_size_;
3275 uint64_t group_start_addr_;
3276 // owner_ and output_section_ specify the section to which stubs are
3277 // attached. The stubs are placed at the end of this section.
3278 const Output_section::Input_section* owner_;
3279 Output_section* output_section_;
3280 };
3281
3282 // Return true iff input section can be handled by current stub
3283 // group. Sections are presented to this function in order,
3284 // so the first section is the head of the group.
3285
3286 bool
3287 Stub_control::can_add_to_stub_group(Output_section* o,
3288 const Output_section::Input_section* i,
3289 bool has14)
3290 {
3291 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3292 uint64_t this_size;
3293 uint64_t start_addr = o->address();
3294
3295 if (whole_sec)
3296 // .init and .fini sections are pasted together to form a single
3297 // function. We can't be adding stubs in the middle of the function.
3298 this_size = o->data_size();
3299 else
3300 {
3301 start_addr += i->relobj()->output_section_offset(i->shndx());
3302 this_size = i->data_size();
3303 }
3304
3305 uint64_t end_addr = start_addr + this_size;
3306 uint32_t group_size = this->stub_group_size_;
3307 if (has14)
3308 this->group_size_ = group_size = group_size >> 10;
3309
3310 if (this_size > group_size && !this->suppress_size_errors_)
3311 gold_warning(_("%s:%s exceeds group size"),
3312 i->relobj()->name().c_str(),
3313 i->relobj()->section_name(i->shndx()).c_str());
3314
3315 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3316 has14 ? " 14bit" : "",
3317 i->relobj()->name().c_str(),
3318 i->relobj()->section_name(i->shndx()).c_str(),
3319 (long long) this_size,
3320 (this->state_ == NO_GROUP
3321 ? this_size
3322 : (long long) end_addr - this->group_start_addr_));
3323
3324 if (this->state_ == NO_GROUP)
3325 {
3326 // Only here on very first use of Stub_control
3327 this->owner_ = i;
3328 this->output_section_ = o;
3329 this->state_ = FINDING_STUB_SECTION;
3330 this->group_size_ = group_size;
3331 this->group_start_addr_ = start_addr;
3332 return true;
3333 }
3334 else if (!this->multi_os_ && this->output_section_ != o)
3335 ;
3336 else if (this->state_ == HAS_STUB_SECTION)
3337 {
3338 // Can we add this section, which is after the stubs, to the
3339 // group?
3340 if (end_addr - this->group_start_addr_ <= this->group_size_)
3341 return true;
3342 }
3343 else if (this->state_ == FINDING_STUB_SECTION)
3344 {
3345 if ((whole_sec && this->output_section_ == o)
3346 || end_addr - this->group_start_addr_ <= this->group_size_)
3347 {
3348 // Stubs are added at the end of "owner_".
3349 this->owner_ = i;
3350 this->output_section_ = o;
3351 return true;
3352 }
3353 // The group before the stubs has reached maximum size.
3354 // Now see about adding sections after the stubs to the
3355 // group. If the current section has a 14-bit branch and
3356 // the group before the stubs exceeds group_size_ (because
3357 // they didn't have 14-bit branches), don't add sections
3358 // after the stubs: The size of stubs for such a large
3359 // group may exceed the reach of a 14-bit branch.
3360 if (!this->stubs_always_after_branch_
3361 && this_size <= this->group_size_
3362 && start_addr - this->group_start_addr_ <= this->group_size_)
3363 {
3364 gold_debug(DEBUG_TARGET, "adding after stubs");
3365 this->state_ = HAS_STUB_SECTION;
3366 this->group_start_addr_ = start_addr;
3367 return true;
3368 }
3369 }
3370 else
3371 gold_unreachable();
3372
3373 gold_debug(DEBUG_TARGET,
3374 !this->multi_os_ && this->output_section_ != o
3375 ? "nope, new output section\n"
3376 : "nope, didn't fit\n");
3377
3378 // The section fails to fit in the current group. Set up a few
3379 // things for the next group. owner_ and output_section_ will be
3380 // set later after we've retrieved those values for the current
3381 // group.
3382 this->state_ = FINDING_STUB_SECTION;
3383 this->group_size_ = group_size;
3384 this->group_start_addr_ = start_addr;
3385 return false;
3386 }
3387
3388 // Look over all the input sections, deciding where to place stubs.
3389
3390 template<int size, bool big_endian>
3391 void
3392 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3393 const Task*,
3394 bool no_size_errors)
3395 {
3396 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3397 parameters->options().stub_group_multi());
3398
3399 // Group input sections and insert stub table
3400 Stub_table_owner* table_owner = NULL;
3401 std::vector<Stub_table_owner*> tables;
3402 Layout::Section_list section_list;
3403 layout->get_executable_sections(&section_list);
3404 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3405 for (Layout::Section_list::iterator o = section_list.begin();
3406 o != section_list.end();
3407 ++o)
3408 {
3409 typedef Output_section::Input_section_list Input_section_list;
3410 for (Input_section_list::const_iterator i
3411 = (*o)->input_sections().begin();
3412 i != (*o)->input_sections().end();
3413 ++i)
3414 {
3415 if (i->is_input_section()
3416 || i->is_relaxed_input_section())
3417 {
3418 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3419 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3420 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3421 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3422 {
3423 table_owner->output_section = stub_control.output_section();
3424 table_owner->owner = stub_control.owner();
3425 stub_control.set_output_and_owner(*o, &*i);
3426 table_owner = NULL;
3427 }
3428 if (table_owner == NULL)
3429 {
3430 table_owner = new Stub_table_owner;
3431 tables.push_back(table_owner);
3432 }
3433 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3434 }
3435 }
3436 }
3437 if (table_owner != NULL)
3438 {
3439 table_owner->output_section = stub_control.output_section();
3440 table_owner->owner = stub_control.owner();;
3441 }
3442 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3443 t != tables.end();
3444 ++t)
3445 {
3446 Stub_table<size, big_endian>* stub_table;
3447
3448 if ((*t)->owner->is_input_section())
3449 stub_table = new Stub_table<size, big_endian>(this,
3450 (*t)->output_section,
3451 (*t)->owner,
3452 this->stub_tables_.size());
3453 else if ((*t)->owner->is_relaxed_input_section())
3454 stub_table = static_cast<Stub_table<size, big_endian>*>(
3455 (*t)->owner->relaxed_input_section());
3456 else
3457 gold_unreachable();
3458 this->stub_tables_.push_back(stub_table);
3459 delete *t;
3460 }
3461 }
3462
3463 template<int size>
3464 static unsigned long
3465 max_branch_delta (unsigned int r_type)
3466 {
3467 if (r_type == elfcpp::R_POWERPC_REL14
3468 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3469 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3470 return 1L << 15;
3471 if (r_type == elfcpp::R_POWERPC_REL24
3472 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3473 || r_type == elfcpp::R_PPC_PLTREL24
3474 || r_type == elfcpp::R_PPC_LOCAL24PC)
3475 return 1L << 25;
3476 return 0;
3477 }
3478
3479 // Return whether this branch is going via a plt call stub.
3480
3481 template<int size, bool big_endian>
3482 bool
3483 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3484 Powerpc_relobj<size, big_endian>* ppc_object,
3485 unsigned int shndx,
3486 Address offset,
3487 Target_powerpc* target,
3488 Symbol_table* symtab)
3489 {
3490 if (this->object_ != ppc_object
3491 || this->shndx_ != shndx
3492 || this->offset_ != offset)
3493 return false;
3494
3495 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3496 if (sym != NULL && sym->is_forwarder())
3497 sym = symtab->resolve_forwards(sym);
3498 if (target->replace_tls_get_addr(sym))
3499 sym = target->tls_get_addr_opt();
3500 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3501 if (gsym != NULL
3502 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3503 && !target->is_elfv2_localentry0(gsym))
3504 : (this->object_->local_has_plt_offset(this->r_sym_)
3505 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3506 {
3507 this->tocsave_ = 1;
3508 return true;
3509 }
3510 return false;
3511 }
3512
3513 // If this branch needs a plt call stub, or a long branch stub, make one.
3514
3515 template<int size, bool big_endian>
3516 bool
3517 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3518 Stub_table<size, big_endian>* stub_table,
3519 Stub_table<size, big_endian>* ifunc_stub_table,
3520 Symbol_table* symtab) const
3521 {
3522 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3523 Target_powerpc<size, big_endian>* target =
3524 static_cast<Target_powerpc<size, big_endian>*>(
3525 parameters->sized_target<size, big_endian>());
3526 if (sym != NULL && sym->is_forwarder())
3527 sym = symtab->resolve_forwards(sym);
3528 if (target->replace_tls_get_addr(sym))
3529 sym = target->tls_get_addr_opt();
3530 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3531 bool ok = true;
3532
3533 if (gsym != NULL
3534 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3535 : this->object_->local_has_plt_offset(this->r_sym_))
3536 {
3537 if (size == 64
3538 && gsym != NULL
3539 && target->abiversion() >= 2
3540 && !parameters->options().output_is_position_independent()
3541 && !is_branch_reloc<size>(this->r_type_))
3542 target->glink_section()->add_global_entry(gsym);
3543 else
3544 {
3545 if (stub_table == NULL
3546 && !(size == 32
3547 && gsym != NULL
3548 && !parameters->options().output_is_position_independent()
3549 && !is_branch_reloc<size>(this->r_type_)))
3550 stub_table = this->object_->stub_table(this->shndx_);
3551 if (stub_table == NULL)
3552 {
3553 // This is a ref from a data section to an ifunc symbol,
3554 // or a non-branch reloc for which we always want to use
3555 // one set of stubs for resolving function addresses.
3556 stub_table = ifunc_stub_table;
3557 }
3558 gold_assert(stub_table != NULL);
3559 Address from = this->object_->get_output_section_offset(this->shndx_);
3560 if (from != invalid_address)
3561 from += (this->object_->output_section(this->shndx_)->address()
3562 + this->offset_);
3563 if (gsym != NULL)
3564 ok = stub_table->add_plt_call_entry(from,
3565 this->object_, gsym,
3566 this->r_type_, this->addend_,
3567 this->tocsave_);
3568 else
3569 ok = stub_table->add_plt_call_entry(from,
3570 this->object_, this->r_sym_,
3571 this->r_type_, this->addend_,
3572 this->tocsave_);
3573 }
3574 }
3575 else
3576 {
3577 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3578 if (max_branch_offset == 0)
3579 return true;
3580 Address from = this->object_->get_output_section_offset(this->shndx_);
3581 gold_assert(from != invalid_address);
3582 from += (this->object_->output_section(this->shndx_)->address()
3583 + this->offset_);
3584 Address to;
3585 unsigned int other = 0;
3586 if (gsym != NULL)
3587 {
3588 switch (gsym->source())
3589 {
3590 case Symbol::FROM_OBJECT:
3591 {
3592 Object* symobj = gsym->object();
3593 if (symobj->is_dynamic()
3594 || symobj->pluginobj() != NULL)
3595 return true;
3596 bool is_ordinary;
3597 unsigned int shndx = gsym->shndx(&is_ordinary);
3598 if (shndx == elfcpp::SHN_UNDEF)
3599 return true;
3600 }
3601 break;
3602
3603 case Symbol::IS_UNDEFINED:
3604 return true;
3605
3606 default:
3607 break;
3608 }
3609 Symbol_table::Compute_final_value_status status;
3610 to = symtab->compute_final_value<size>(gsym, &status);
3611 if (status != Symbol_table::CFVS_OK)
3612 return true;
3613 if (size == 64)
3614 other = gsym->nonvis() >> 3;
3615 }
3616 else
3617 {
3618 const Symbol_value<size>* psymval
3619 = this->object_->local_symbol(this->r_sym_);
3620 Symbol_value<size> symval;
3621 if (psymval->is_section_symbol())
3622 symval.set_is_section_symbol();
3623 typedef Sized_relobj_file<size, big_endian> ObjType;
3624 typename ObjType::Compute_final_local_value_status status
3625 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3626 &symval, symtab);
3627 if (status != ObjType::CFLV_OK
3628 || !symval.has_output_value())
3629 return true;
3630 to = symval.value(this->object_, 0);
3631 if (size == 64)
3632 other = this->object_->st_other(this->r_sym_) >> 5;
3633 }
3634 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3635 to += this->addend_;
3636 if (stub_table == NULL)
3637 stub_table = this->object_->stub_table(this->shndx_);
3638 if (size == 64 && target->abiversion() < 2)
3639 {
3640 unsigned int dest_shndx;
3641 if (!target->symval_for_branch(symtab, gsym, this->object_,
3642 &to, &dest_shndx))
3643 return true;
3644 }
3645 unsigned int local_ent = 0;
3646 if (size == 64
3647 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC)
3648 local_ent = elfcpp::ppc64_decode_local_entry(other);
3649 Address delta = to + local_ent - from;
3650 if (delta + max_branch_offset >= 2 * max_branch_offset
3651 || (size == 64
3652 && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3653 && (gsym != NULL
3654 ? this->object_->ppc64_needs_toc(gsym)
3655 : this->object_->ppc64_needs_toc(this->r_sym_))))
3656 {
3657 if (stub_table == NULL)
3658 {
3659 gold_warning(_("%s:%s: branch in non-executable section,"
3660 " no long branch stub for you"),
3661 this->object_->name().c_str(),
3662 this->object_->section_name(this->shndx_).c_str());
3663 return true;
3664 }
3665 bool save_res = (size == 64
3666 && gsym != NULL
3667 && gsym->source() == Symbol::IN_OUTPUT_DATA
3668 && gsym->output_data() == target->savres_section());
3669 ok = stub_table->add_long_branch_entry(this->r_type_,
3670 from, to, other, save_res);
3671 }
3672 }
3673 if (!ok)
3674 gold_debug(DEBUG_TARGET,
3675 "branch at %s:%s+%#lx\n"
3676 "can't reach stub attached to %s:%s",
3677 this->object_->name().c_str(),
3678 this->object_->section_name(this->shndx_).c_str(),
3679 (unsigned long) this->offset_,
3680 stub_table->relobj()->name().c_str(),
3681 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3682
3683 return ok;
3684 }
3685
3686 // Relaxation hook. This is where we do stub generation.
3687
3688 template<int size, bool big_endian>
3689 bool
3690 Target_powerpc<size, big_endian>::do_relax(int pass,
3691 const Input_objects*,
3692 Symbol_table* symtab,
3693 Layout* layout,
3694 const Task* task)
3695 {
3696 unsigned int prev_brlt_size = 0;
3697 if (pass == 1)
3698 {
3699 bool thread_safe
3700 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3701 if (size == 64
3702 && this->abiversion() < 2
3703 && !thread_safe
3704 && !parameters->options().user_set_plt_thread_safe())
3705 {
3706 static const char* const thread_starter[] =
3707 {
3708 "pthread_create",
3709 /* libstdc++ */
3710 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3711 /* librt */
3712 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3713 "mq_notify", "create_timer",
3714 /* libanl */
3715 "getaddrinfo_a",
3716 /* libgomp */
3717 "GOMP_parallel",
3718 "GOMP_parallel_start",
3719 "GOMP_parallel_loop_static",
3720 "GOMP_parallel_loop_static_start",
3721 "GOMP_parallel_loop_dynamic",
3722 "GOMP_parallel_loop_dynamic_start",
3723 "GOMP_parallel_loop_guided",
3724 "GOMP_parallel_loop_guided_start",
3725 "GOMP_parallel_loop_runtime",
3726 "GOMP_parallel_loop_runtime_start",
3727 "GOMP_parallel_sections",
3728 "GOMP_parallel_sections_start",
3729 /* libgo */
3730 "__go_go",
3731 };
3732
3733 if (parameters->options().shared())
3734 thread_safe = true;
3735 else
3736 {
3737 for (unsigned int i = 0;
3738 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3739 i++)
3740 {
3741 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3742 thread_safe = (sym != NULL
3743 && sym->in_reg()
3744 && sym->in_real_elf());
3745 if (thread_safe)
3746 break;
3747 }
3748 }
3749 }
3750 this->plt_thread_safe_ = thread_safe;
3751 }
3752
3753 if (pass == 1)
3754 {
3755 this->stub_group_size_ = parameters->options().stub_group_size();
3756 bool no_size_errors = true;
3757 if (this->stub_group_size_ == 1)
3758 this->stub_group_size_ = 0x1c00000;
3759 else if (this->stub_group_size_ == -1)
3760 this->stub_group_size_ = -0x1e00000;
3761 else
3762 no_size_errors = false;
3763 this->group_sections(layout, task, no_size_errors);
3764 }
3765 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3766 {
3767 this->branch_lookup_table_.clear();
3768 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3769 p != this->stub_tables_.end();
3770 ++p)
3771 {
3772 (*p)->clear_stubs(true);
3773 }
3774 this->stub_tables_.clear();
3775 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3776 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3777 program_name, this->stub_group_size_);
3778 this->group_sections(layout, task, true);
3779 }
3780
3781 // We need address of stub tables valid for make_stub.
3782 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3783 p != this->stub_tables_.end();
3784 ++p)
3785 {
3786 const Powerpc_relobj<size, big_endian>* object
3787 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3788 Address off = object->get_output_section_offset((*p)->shndx());
3789 gold_assert(off != invalid_address);
3790 Output_section* os = (*p)->output_section();
3791 (*p)->set_address_and_size(os, off);
3792 }
3793
3794 if (pass != 1)
3795 {
3796 // Clear plt call stubs, long branch stubs and branch lookup table.
3797 prev_brlt_size = this->branch_lookup_table_.size();
3798 this->branch_lookup_table_.clear();
3799 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3800 p != this->stub_tables_.end();
3801 ++p)
3802 {
3803 (*p)->clear_stubs(false);
3804 }
3805 }
3806
3807 // Build all the stubs.
3808 this->relax_failed_ = false;
3809 Stub_table<size, big_endian>* ifunc_stub_table
3810 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3811 Stub_table<size, big_endian>* one_stub_table
3812 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3813 for (typename Branches::const_iterator b = this->branch_info_.begin();
3814 b != this->branch_info_.end();
3815 b++)
3816 {
3817 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3818 && !this->relax_failed_)
3819 {
3820 this->relax_failed_ = true;
3821 this->relax_fail_count_++;
3822 if (this->relax_fail_count_ < 3)
3823 return true;
3824 }
3825 }
3826 bool do_resize = false;
3827 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3828 p != this->stub_tables_.end();
3829 ++p)
3830 if ((*p)->need_resize())
3831 {
3832 do_resize = true;
3833 break;
3834 }
3835 if (do_resize)
3836 {
3837 this->branch_lookup_table_.clear();
3838 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3839 p != this->stub_tables_.end();
3840 ++p)
3841 (*p)->set_resizing(true);
3842 for (typename Branches::const_iterator b = this->branch_info_.begin();
3843 b != this->branch_info_.end();
3844 b++)
3845 {
3846 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3847 && !this->relax_failed_)
3848 {
3849 this->relax_failed_ = true;
3850 this->relax_fail_count_++;
3851 if (this->relax_fail_count_ < 3)
3852 return true;
3853 }
3854 }
3855 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3856 p != this->stub_tables_.end();
3857 ++p)
3858 (*p)->set_resizing(false);
3859 }
3860
3861 // Did anything change size?
3862 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3863 bool again = num_huge_branches != prev_brlt_size;
3864 if (size == 64 && num_huge_branches != 0)
3865 this->make_brlt_section(layout);
3866 if (size == 64 && again)
3867 this->brlt_section_->set_current_size(num_huge_branches);
3868
3869 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3870 p != this->stub_tables_.rend();
3871 ++p)
3872 (*p)->remove_eh_frame(layout);
3873
3874 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3875 p != this->stub_tables_.end();
3876 ++p)
3877 (*p)->add_eh_frame(layout);
3878
3879 typedef Unordered_set<Output_section*> Output_sections;
3880 Output_sections os_need_update;
3881 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3882 p != this->stub_tables_.end();
3883 ++p)
3884 {
3885 if ((*p)->size_update())
3886 {
3887 again = true;
3888 os_need_update.insert((*p)->output_section());
3889 }
3890 }
3891
3892 // Set output section offsets for all input sections in an output
3893 // section that just changed size. Anything past the stubs will
3894 // need updating.
3895 for (typename Output_sections::iterator p = os_need_update.begin();
3896 p != os_need_update.end();
3897 p++)
3898 {
3899 Output_section* os = *p;
3900 Address off = 0;
3901 typedef Output_section::Input_section_list Input_section_list;
3902 for (Input_section_list::const_iterator i = os->input_sections().begin();
3903 i != os->input_sections().end();
3904 ++i)
3905 {
3906 off = align_address(off, i->addralign());
3907 if (i->is_input_section() || i->is_relaxed_input_section())
3908 i->relobj()->set_section_offset(i->shndx(), off);
3909 if (i->is_relaxed_input_section())
3910 {
3911 Stub_table<size, big_endian>* stub_table
3912 = static_cast<Stub_table<size, big_endian>*>(
3913 i->relaxed_input_section());
3914 Address stub_table_size = stub_table->set_address_and_size(os, off);
3915 off += stub_table_size;
3916 // After a few iterations, set current stub table size
3917 // as min size threshold, so later stub tables can only
3918 // grow in size.
3919 if (pass >= 4)
3920 stub_table->set_min_size_threshold(stub_table_size);
3921 }
3922 else
3923 off += i->data_size();
3924 }
3925 // If .branch_lt is part of this output section, then we have
3926 // just done the offset adjustment.
3927 os->clear_section_offsets_need_adjustment();
3928 }
3929
3930 if (size == 64
3931 && !again
3932 && num_huge_branches != 0
3933 && parameters->options().output_is_position_independent())
3934 {
3935 // Fill in the BRLT relocs.
3936 this->brlt_section_->reset_brlt_sizes();
3937 for (typename Branch_lookup_table::const_iterator p
3938 = this->branch_lookup_table_.begin();
3939 p != this->branch_lookup_table_.end();
3940 ++p)
3941 {
3942 this->brlt_section_->add_reloc(p->first, p->second);
3943 }
3944 this->brlt_section_->finalize_brlt_sizes();
3945 }
3946
3947 if (!again
3948 && (parameters->options().user_set_emit_stub_syms()
3949 ? parameters->options().emit_stub_syms()
3950 : (size == 64
3951 || parameters->options().output_is_position_independent()
3952 || parameters->options().emit_relocs())))
3953 {
3954 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3955 p != this->stub_tables_.end();
3956 ++p)
3957 (*p)->define_stub_syms(symtab);
3958
3959 if (this->glink_ != NULL)
3960 {
3961 int stub_size = this->glink_->pltresolve_size();
3962 Address value = -stub_size;
3963 if (size == 64)
3964 {
3965 value = 8;
3966 stub_size -= 8;
3967 }
3968 this->define_local(symtab, "__glink_PLTresolve",
3969 this->glink_, value, stub_size);
3970
3971 if (size != 64)
3972 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3973 }
3974 }
3975
3976 return again;
3977 }
3978
3979 template<int size, bool big_endian>
3980 void
3981 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3982 unsigned char* oview,
3983 uint64_t* paddress,
3984 off_t* plen) const
3985 {
3986 uint64_t address = plt->address();
3987 off_t len = plt->data_size();
3988
3989 if (plt == this->glink_)
3990 {
3991 // See Output_data_glink::do_write() for glink contents.
3992 if (len == 0)
3993 {
3994 // Static linking may need stubs, to support ifunc and long
3995 // branches. We need to create an output section for
3996 // .eh_frame early in the link process, to have a place to
3997 // attach stub .eh_frame info. We also need to have
3998 // registered a CIE that matches the stub CIE. Both of
3999 // these requirements are satisfied by creating an FDE and
4000 // CIE for .glink, even though static linking will leave
4001 // .glink zero length.
4002 // ??? Hopefully generating an FDE with a zero address range
4003 // won't confuse anything that consumes .eh_frame info.
4004 }
4005 else if (size == 64)
4006 {
4007 // There is one word before __glink_PLTresolve
4008 address += 8;
4009 len -= 8;
4010 }
4011 else if (parameters->options().output_is_position_independent())
4012 {
4013 // There are two FDEs for a position independent glink.
4014 // The first covers the branch table, the second
4015 // __glink_PLTresolve at the end of glink.
4016 off_t resolve_size = this->glink_->pltresolve_size();
4017 if (oview[9] == elfcpp::DW_CFA_nop)
4018 len -= resolve_size;
4019 else
4020 {
4021 address += len - resolve_size;
4022 len = resolve_size;
4023 }
4024 }
4025 }
4026 else
4027 {
4028 // Must be a stub table.
4029 const Stub_table<size, big_endian>* stub_table
4030 = static_cast<const Stub_table<size, big_endian>*>(plt);
4031 uint64_t stub_address = stub_table->stub_address();
4032 len -= stub_address - address;
4033 address = stub_address;
4034 }
4035
4036 *paddress = address;
4037 *plen = len;
4038 }
4039
4040 // A class to handle the PLT data.
4041
4042 template<int size, bool big_endian>
4043 class Output_data_plt_powerpc : public Output_section_data_build
4044 {
4045 public:
4046 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4047 size, big_endian> Reloc_section;
4048
4049 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4050 Reloc_section* plt_rel,
4051 const char* name)
4052 : Output_section_data_build(size == 32 ? 4 : 8),
4053 rel_(plt_rel),
4054 targ_(targ),
4055 name_(name)
4056 { }
4057
4058 // Add an entry to the PLT.
4059 void
4060 add_entry(Symbol*);
4061
4062 void
4063 add_ifunc_entry(Symbol*);
4064
4065 void
4066 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4067
4068 void
4069 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4070
4071 // Return the .rela.plt section data.
4072 Reloc_section*
4073 rel_plt() const
4074 {
4075 return this->rel_;
4076 }
4077
4078 // Return the number of PLT entries.
4079 unsigned int
4080 entry_count() const
4081 {
4082 if (this->current_data_size() == 0)
4083 return 0;
4084 return ((this->current_data_size() - this->first_plt_entry_offset())
4085 / this->plt_entry_size());
4086 }
4087
4088 protected:
4089 void
4090 do_adjust_output_section(Output_section* os)
4091 {
4092 os->set_entsize(0);
4093 }
4094
4095 // Write to a map file.
4096 void
4097 do_print_to_mapfile(Mapfile* mapfile) const
4098 { mapfile->print_output_data(this, this->name_); }
4099
4100 private:
4101 // Return the offset of the first non-reserved PLT entry.
4102 unsigned int
4103 first_plt_entry_offset() const
4104 {
4105 // IPLT and LPLT have no reserved entry.
4106 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4107 return 0;
4108 return this->targ_->first_plt_entry_offset();
4109 }
4110
4111 // Return the size of each PLT entry.
4112 unsigned int
4113 plt_entry_size() const
4114 {
4115 return this->targ_->plt_entry_size();
4116 }
4117
4118 // Write out the PLT data.
4119 void
4120 do_write(Output_file*);
4121
4122 // The reloc section.
4123 Reloc_section* rel_;
4124 // Allows access to .glink for do_write.
4125 Target_powerpc<size, big_endian>* targ_;
4126 // What to report in map file.
4127 const char *name_;
4128 };
4129
4130 // Add an entry to the PLT.
4131
4132 template<int size, bool big_endian>
4133 void
4134 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
4135 {
4136 if (!gsym->has_plt_offset())
4137 {
4138 section_size_type off = this->current_data_size();
4139 if (off == 0)
4140 off += this->first_plt_entry_offset();
4141 gsym->set_plt_offset(off);
4142 gsym->set_needs_dynsym_entry();
4143 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4144 this->rel_->add_global(gsym, dynrel, this, off, 0);
4145 off += this->plt_entry_size();
4146 this->set_current_data_size(off);
4147 }
4148 }
4149
4150 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4151
4152 template<int size, bool big_endian>
4153 void
4154 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4155 {
4156 if (!gsym->has_plt_offset())
4157 {
4158 section_size_type off = this->current_data_size();
4159 gsym->set_plt_offset(off);
4160 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4161 if (size == 64 && this->targ_->abiversion() < 2)
4162 dynrel = elfcpp::R_PPC64_JMP_IREL;
4163 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4164 off += this->plt_entry_size();
4165 this->set_current_data_size(off);
4166 }
4167 }
4168
4169 // Add an entry for a local symbol to the PLT.
4170
4171 template<int size, bool big_endian>
4172 void
4173 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4174 Sized_relobj_file<size, big_endian>* relobj,
4175 unsigned int local_sym_index)
4176 {
4177 if (!relobj->local_has_plt_offset(local_sym_index))
4178 {
4179 section_size_type off = this->current_data_size();
4180 relobj->set_local_plt_offset(local_sym_index, off);
4181 if (this->rel_)
4182 {
4183 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4184 if (size == 64 && this->targ_->abiversion() < 2)
4185 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4186 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4187 dynrel, this, off, 0);
4188 }
4189 off += this->plt_entry_size();
4190 this->set_current_data_size(off);
4191 }
4192 }
4193
4194 // Add an entry for a local ifunc symbol to the IPLT.
4195
4196 template<int size, bool big_endian>
4197 void
4198 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4199 Sized_relobj_file<size, big_endian>* relobj,
4200 unsigned int local_sym_index)
4201 {
4202 if (!relobj->local_has_plt_offset(local_sym_index))
4203 {
4204 section_size_type off = this->current_data_size();
4205 relobj->set_local_plt_offset(local_sym_index, off);
4206 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4207 if (size == 64 && this->targ_->abiversion() < 2)
4208 dynrel = elfcpp::R_PPC64_JMP_IREL;
4209 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4210 this, off, 0);
4211 off += this->plt_entry_size();
4212 this->set_current_data_size(off);
4213 }
4214 }
4215
4216 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4217 static const uint32_t add_2_2_11 = 0x7c425a14;
4218 static const uint32_t add_2_2_12 = 0x7c426214;
4219 static const uint32_t add_3_3_2 = 0x7c631214;
4220 static const uint32_t add_3_3_13 = 0x7c636a14;
4221 static const uint32_t add_3_12_2 = 0x7c6c1214;
4222 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4223 static const uint32_t add_11_0_11 = 0x7d605a14;
4224 static const uint32_t add_11_2_11 = 0x7d625a14;
4225 static const uint32_t add_11_11_2 = 0x7d6b1214;
4226 static const uint32_t add_12_11_12 = 0x7d8b6214;
4227 static const uint32_t addi_0_12 = 0x380c0000;
4228 static const uint32_t addi_2_2 = 0x38420000;
4229 static const uint32_t addi_3_3 = 0x38630000;
4230 static const uint32_t addi_11_11 = 0x396b0000;
4231 static const uint32_t addi_12_1 = 0x39810000;
4232 static const uint32_t addi_12_11 = 0x398b0000;
4233 static const uint32_t addi_12_12 = 0x398c0000;
4234 static const uint32_t addis_0_2 = 0x3c020000;
4235 static const uint32_t addis_0_13 = 0x3c0d0000;
4236 static const uint32_t addis_2_12 = 0x3c4c0000;
4237 static const uint32_t addis_11_2 = 0x3d620000;
4238 static const uint32_t addis_11_11 = 0x3d6b0000;
4239 static const uint32_t addis_11_30 = 0x3d7e0000;
4240 static const uint32_t addis_12_1 = 0x3d810000;
4241 static const uint32_t addis_12_2 = 0x3d820000;
4242 static const uint32_t addis_12_11 = 0x3d8b0000;
4243 static const uint32_t addis_12_12 = 0x3d8c0000;
4244 static const uint32_t b = 0x48000000;
4245 static const uint32_t bcl_20_31 = 0x429f0005;
4246 static const uint32_t bctr = 0x4e800420;
4247 static const uint32_t bctrl = 0x4e800421;
4248 static const uint32_t beqlr = 0x4d820020;
4249 static const uint32_t blr = 0x4e800020;
4250 static const uint32_t bnectr_p4 = 0x4ce20420;
4251 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4252 static const uint32_t cmpldi_2_0 = 0x28220000;
4253 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4254 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4255 static const uint32_t cror_15_15_15 = 0x4def7b82;
4256 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4257 static const uint32_t ld_0_1 = 0xe8010000;
4258 static const uint32_t ld_0_11 = 0xe80b0000;
4259 static const uint32_t ld_0_12 = 0xe80c0000;
4260 static const uint32_t ld_2_1 = 0xe8410000;
4261 static const uint32_t ld_2_2 = 0xe8420000;
4262 static const uint32_t ld_2_11 = 0xe84b0000;
4263 static const uint32_t ld_2_12 = 0xe84c0000;
4264 static const uint32_t ld_11_1 = 0xe9610000;
4265 static const uint32_t ld_11_2 = 0xe9620000;
4266 static const uint32_t ld_11_3 = 0xe9630000;
4267 static const uint32_t ld_11_11 = 0xe96b0000;
4268 static const uint32_t ld_12_2 = 0xe9820000;
4269 static const uint32_t ld_12_3 = 0xe9830000;
4270 static const uint32_t ld_12_11 = 0xe98b0000;
4271 static const uint32_t ld_12_12 = 0xe98c0000;
4272 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4273 static const uint32_t lfd_0_1 = 0xc8010000;
4274 static const uint32_t li_0_0 = 0x38000000;
4275 static const uint32_t li_11_0 = 0x39600000;
4276 static const uint32_t li_12_0 = 0x39800000;
4277 static const uint32_t lis_0 = 0x3c000000;
4278 static const uint32_t lis_2 = 0x3c400000;
4279 static const uint32_t lis_11 = 0x3d600000;
4280 static const uint32_t lis_12 = 0x3d800000;
4281 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4282 static const uint32_t lwz_0_12 = 0x800c0000;
4283 static const uint32_t lwz_11_3 = 0x81630000;
4284 static const uint32_t lwz_11_11 = 0x816b0000;
4285 static const uint32_t lwz_11_30 = 0x817e0000;
4286 static const uint32_t lwz_12_3 = 0x81830000;
4287 static const uint32_t lwz_12_12 = 0x818c0000;
4288 static const uint32_t lwzu_0_12 = 0x840c0000;
4289 static const uint32_t mflr_0 = 0x7c0802a6;
4290 static const uint32_t mflr_11 = 0x7d6802a6;
4291 static const uint32_t mflr_12 = 0x7d8802a6;
4292 static const uint32_t mr_0_3 = 0x7c601b78;
4293 static const uint32_t mr_3_0 = 0x7c030378;
4294 static const uint32_t mtctr_0 = 0x7c0903a6;
4295 static const uint32_t mtctr_11 = 0x7d6903a6;
4296 static const uint32_t mtctr_12 = 0x7d8903a6;
4297 static const uint32_t mtlr_0 = 0x7c0803a6;
4298 static const uint32_t mtlr_11 = 0x7d6803a6;
4299 static const uint32_t mtlr_12 = 0x7d8803a6;
4300 static const uint32_t nop = 0x60000000;
4301 static const uint32_t ori_0_0_0 = 0x60000000;
4302 static const uint32_t ori_11_11_0 = 0x616b0000;
4303 static const uint32_t ori_12_12_0 = 0x618c0000;
4304 static const uint32_t oris_12_12_0 = 0x658c0000;
4305 static const uint32_t sldi_11_11_34 = 0x796b1746;
4306 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4307 static const uint32_t srdi_0_0_2 = 0x7800f082;
4308 static const uint32_t std_0_1 = 0xf8010000;
4309 static const uint32_t std_0_12 = 0xf80c0000;
4310 static const uint32_t std_2_1 = 0xf8410000;
4311 static const uint32_t std_11_1 = 0xf9610000;
4312 static const uint32_t stfd_0_1 = 0xd8010000;
4313 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4314 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4315 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4316 static const uint32_t xor_2_12_12 = 0x7d826278;
4317 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4318
4319 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4320 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4321 static const uint64_t pnop = 0x0700000000000000ULL;
4322
4323 // Write out the PLT.
4324
4325 template<int size, bool big_endian>
4326 void
4327 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4328 {
4329 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4330 {
4331 const section_size_type offset = this->offset();
4332 const section_size_type oview_size
4333 = convert_to_section_size_type(this->data_size());
4334 unsigned char* const oview = of->get_output_view(offset, oview_size);
4335 unsigned char* pov = oview;
4336 unsigned char* endpov = oview + oview_size;
4337
4338 // The address of the .glink branch table
4339 const Output_data_glink<size, big_endian>* glink
4340 = this->targ_->glink_section();
4341 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4342
4343 while (pov < endpov)
4344 {
4345 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4346 pov += 4;
4347 branch_tab += 4;
4348 }
4349
4350 of->write_output_view(offset, oview_size, oview);
4351 }
4352 }
4353
4354 // Create the PLT section.
4355
4356 template<int size, bool big_endian>
4357 void
4358 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4359 Layout* layout)
4360 {
4361 if (this->plt_ == NULL)
4362 {
4363 if (this->got_ == NULL)
4364 this->got_section(symtab, layout, GOT_TYPE_SMALL);
4365
4366 if (this->glink_ == NULL)
4367 make_glink_section(layout);
4368
4369 // Ensure that .rela.dyn always appears before .rela.plt This is
4370 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4371 // needs to include .rela.plt in its range.
4372 this->rela_dyn_section(layout);
4373
4374 Reloc_section* plt_rel = new Reloc_section(false);
4375 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4376 elfcpp::SHF_ALLOC, plt_rel,
4377 ORDER_DYNAMIC_PLT_RELOCS, false);
4378 this->plt_
4379 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4380 "** PLT");
4381 layout->add_output_section_data(".plt",
4382 (size == 32
4383 ? elfcpp::SHT_PROGBITS
4384 : elfcpp::SHT_NOBITS),
4385 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4386 this->plt_,
4387 (size == 32
4388 ? ORDER_SMALL_DATA
4389 : ORDER_SMALL_BSS),
4390 false);
4391
4392 Output_section* rela_plt_os = plt_rel->output_section();
4393 rela_plt_os->set_info_section(this->plt_->output_section());
4394 }
4395 }
4396
4397 // Create the IPLT section.
4398
4399 template<int size, bool big_endian>
4400 void
4401 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4402 Layout* layout)
4403 {
4404 if (this->iplt_ == NULL)
4405 {
4406 this->make_plt_section(symtab, layout);
4407 this->make_lplt_section(layout);
4408
4409 Reloc_section* iplt_rel = new Reloc_section(false);
4410 if (this->rela_dyn_->output_section())
4411 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4412 this->iplt_
4413 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4414 "** IPLT");
4415 if (this->plt_->output_section())
4416 this->plt_->output_section()->add_output_section_data(this->iplt_);
4417 }
4418 }
4419
4420 // Create the LPLT section.
4421
4422 template<int size, bool big_endian>
4423 void
4424 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4425 {
4426 if (this->lplt_ == NULL)
4427 {
4428 Reloc_section* lplt_rel = NULL;
4429 if (parameters->options().output_is_position_independent())
4430 {
4431 lplt_rel = new Reloc_section(false);
4432 this->rela_dyn_section(layout);
4433 if (this->rela_dyn_->output_section())
4434 this->rela_dyn_->output_section()
4435 ->add_output_section_data(lplt_rel);
4436 }
4437 this->lplt_
4438 = new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4439 "** LPLT");
4440 this->make_brlt_section(layout);
4441 if (this->brlt_section_ && this->brlt_section_->output_section())
4442 this->brlt_section_->output_section()
4443 ->add_output_section_data(this->lplt_);
4444 else
4445 layout->add_output_section_data(".branch_lt",
4446 elfcpp::SHT_PROGBITS,
4447 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4448 this->lplt_,
4449 ORDER_RELRO,
4450 true);
4451 }
4452 }
4453
4454 // A section for huge long branch addresses, similar to plt section.
4455
4456 template<int size, bool big_endian>
4457 class Output_data_brlt_powerpc : public Output_section_data_build
4458 {
4459 public:
4460 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4461 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4462 size, big_endian> Reloc_section;
4463
4464 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4465 Reloc_section* brlt_rel)
4466 : Output_section_data_build(size == 32 ? 4 : 8),
4467 rel_(brlt_rel),
4468 targ_(targ)
4469 { }
4470
4471 void
4472 reset_brlt_sizes()
4473 {
4474 this->reset_data_size();
4475 this->rel_->reset_data_size();
4476 }
4477
4478 void
4479 finalize_brlt_sizes()
4480 {
4481 this->finalize_data_size();
4482 this->rel_->finalize_data_size();
4483 }
4484
4485 // Add a reloc for an entry in the BRLT.
4486 void
4487 add_reloc(Address to, unsigned int off)
4488 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4489
4490 // Update section and reloc section size.
4491 void
4492 set_current_size(unsigned int num_branches)
4493 {
4494 this->reset_address_and_file_offset();
4495 this->set_current_data_size(num_branches * 16);
4496 this->finalize_data_size();
4497 Output_section* os = this->output_section();
4498 os->set_section_offsets_need_adjustment();
4499 if (this->rel_ != NULL)
4500 {
4501 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4502 this->rel_->reset_address_and_file_offset();
4503 this->rel_->set_current_data_size(num_branches * reloc_size);
4504 this->rel_->finalize_data_size();
4505 Output_section* os = this->rel_->output_section();
4506 os->set_section_offsets_need_adjustment();
4507 }
4508 }
4509
4510 protected:
4511 void
4512 do_adjust_output_section(Output_section* os)
4513 {
4514 os->set_entsize(0);
4515 }
4516
4517 // Write to a map file.
4518 void
4519 do_print_to_mapfile(Mapfile* mapfile) const
4520 { mapfile->print_output_data(this, "** BRLT"); }
4521
4522 private:
4523 // Write out the BRLT data.
4524 void
4525 do_write(Output_file*);
4526
4527 // The reloc section.
4528 Reloc_section* rel_;
4529 Target_powerpc<size, big_endian>* targ_;
4530 };
4531
4532 // Make the branch lookup table section.
4533
4534 template<int size, bool big_endian>
4535 void
4536 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4537 {
4538 if (size == 64 && this->brlt_section_ == NULL)
4539 {
4540 Reloc_section* brlt_rel = NULL;
4541 bool is_pic = parameters->options().output_is_position_independent();
4542 if (is_pic)
4543 {
4544 // When PIC we can't fill in .branch_lt but must initialise at
4545 // runtime via dynamic relocations.
4546 this->rela_dyn_section(layout);
4547 brlt_rel = new Reloc_section(false);
4548 if (this->rela_dyn_->output_section())
4549 this->rela_dyn_->output_section()
4550 ->add_output_section_data(brlt_rel);
4551 }
4552 this->brlt_section_
4553 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4554 if (this->plt_ && is_pic && this->plt_->output_section())
4555 this->plt_->output_section()
4556 ->add_output_section_data(this->brlt_section_);
4557 else
4558 layout->add_output_section_data(".branch_lt",
4559 elfcpp::SHT_PROGBITS,
4560 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4561 this->brlt_section_,
4562 ORDER_RELRO,
4563 true);
4564 }
4565 }
4566
4567 // Write out .branch_lt when non-PIC.
4568
4569 template<int size, bool big_endian>
4570 void
4571 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4572 {
4573 if (size == 64 && !parameters->options().output_is_position_independent())
4574 {
4575 const section_size_type offset = this->offset();
4576 const section_size_type oview_size
4577 = convert_to_section_size_type(this->data_size());
4578 unsigned char* const oview = of->get_output_view(offset, oview_size);
4579
4580 this->targ_->write_branch_lookup_table(oview);
4581 of->write_output_view(offset, oview_size, oview);
4582 }
4583 }
4584
4585 static inline uint32_t
4586 l(uint32_t a)
4587 {
4588 return a & 0xffff;
4589 }
4590
4591 static inline uint32_t
4592 hi(uint32_t a)
4593 {
4594 return l(a >> 16);
4595 }
4596
4597 static inline uint32_t
4598 ha(uint32_t a)
4599 {
4600 return hi(a + 0x8000);
4601 }
4602
4603 static inline uint64_t
4604 d34(uint64_t v)
4605 {
4606 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4607 }
4608
4609 static inline uint64_t
4610 ha34(uint64_t v)
4611 {
4612 return (v + (1ULL << 33)) >> 34;
4613 }
4614
4615 template<int size>
4616 struct Eh_cie
4617 {
4618 static const unsigned char eh_frame_cie[12];
4619 };
4620
4621 template<int size>
4622 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4623 {
4624 1, // CIE version.
4625 'z', 'R', 0, // Augmentation string.
4626 4, // Code alignment.
4627 0x80 - size / 8 , // Data alignment.
4628 65, // RA reg.
4629 1, // Augmentation size.
4630 (elfcpp::DW_EH_PE_pcrel
4631 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4632 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4633 };
4634
4635 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4636 static const unsigned char glink_eh_frame_fde_64v1[] =
4637 {
4638 0, 0, 0, 0, // Replaced with offset to .glink.
4639 0, 0, 0, 0, // Replaced with size of .glink.
4640 0, // Augmentation size.
4641 elfcpp::DW_CFA_advance_loc + 2,
4642 elfcpp::DW_CFA_register, 65, 12,
4643 elfcpp::DW_CFA_advance_loc + 4,
4644 elfcpp::DW_CFA_restore_extended, 65
4645 };
4646
4647 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4648 static const unsigned char glink_eh_frame_fde_64v2[] =
4649 {
4650 0, 0, 0, 0, // Replaced with offset to .glink.
4651 0, 0, 0, 0, // Replaced with size of .glink.
4652 0, // Augmentation size.
4653 elfcpp::DW_CFA_advance_loc + 2,
4654 elfcpp::DW_CFA_register, 65, 0,
4655 elfcpp::DW_CFA_advance_loc + 2,
4656 elfcpp::DW_CFA_restore_extended, 65
4657 };
4658
4659 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4660 {
4661 0, 0, 0, 0, // Replaced with offset to .glink.
4662 0, 0, 0, 0, // Replaced with size of .glink.
4663 0, // Augmentation size.
4664 elfcpp::DW_CFA_advance_loc + 3,
4665 elfcpp::DW_CFA_register, 65, 0,
4666 elfcpp::DW_CFA_advance_loc + 2,
4667 elfcpp::DW_CFA_restore_extended, 65
4668 };
4669
4670 // Describe __glink_PLTresolve use of LR, 32-bit version.
4671 static const unsigned char glink_eh_frame_fde_32[] =
4672 {
4673 0, 0, 0, 0, // Replaced with offset to .glink.
4674 0, 0, 0, 0, // Replaced with size of .glink.
4675 0, // Augmentation size.
4676 elfcpp::DW_CFA_advance_loc + 2,
4677 elfcpp::DW_CFA_register, 65, 0,
4678 elfcpp::DW_CFA_advance_loc + 4,
4679 elfcpp::DW_CFA_restore_extended, 65
4680 };
4681
4682 static const unsigned char default_fde[] =
4683 {
4684 0, 0, 0, 0, // Replaced with offset to stubs.
4685 0, 0, 0, 0, // Replaced with size of stubs.
4686 0, // Augmentation size.
4687 elfcpp::DW_CFA_nop, // Pad.
4688 elfcpp::DW_CFA_nop,
4689 elfcpp::DW_CFA_nop
4690 };
4691
4692 template<bool big_endian>
4693 static inline void
4694 write_insn(unsigned char* p, uint32_t v)
4695 {
4696 elfcpp::Swap<32, big_endian>::writeval(p, v);
4697 }
4698
4699 template<int size>
4700 static inline unsigned int
4701 param_plt_align()
4702 {
4703 if (!parameters->options().user_set_plt_align())
4704 return size == 64 ? 32 : 8;
4705 return 1 << parameters->options().plt_align();
4706 }
4707
4708 // Stub_table holds information about plt and long branch stubs.
4709 // Stubs are built in an area following some input section determined
4710 // by group_sections(). This input section is converted to a relaxed
4711 // input section allowing it to be resized to accommodate the stubs
4712
4713 template<int size, bool big_endian>
4714 class Stub_table : public Output_relaxed_input_section
4715 {
4716 public:
4717 struct Plt_stub_ent
4718 {
4719 Plt_stub_ent(unsigned int off, unsigned int indx)
4720 : off_(off), indx_(indx), iter_(0), notoc_(0), toc_(0),
4721 r2save_(0), localentry0_(0), tocoff_(0)
4722 { }
4723
4724 unsigned int off_;
4725 unsigned int indx_;
4726 unsigned int iter_ : 1;
4727 unsigned int notoc_ : 1;
4728 unsigned int toc_ : 1;
4729 unsigned int r2save_ : 1;
4730 unsigned int localentry0_ : 1;
4731 unsigned int tocoff_ : 8;
4732 };
4733 struct Branch_stub_ent
4734 {
4735 Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4736 : off_(off), iter_(0), notoc_(notoc), toc_(0), save_res_(save_res),
4737 other_(0), tocoff_(0)
4738 { }
4739
4740 unsigned int off_;
4741 unsigned int iter_ : 1;
4742 unsigned int notoc_ : 1;
4743 unsigned int toc_ : 1;
4744 unsigned int save_res_ : 1;
4745 unsigned int other_ : 3;
4746 unsigned int tocoff_ : 8;
4747 };
4748 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4749 static const Address invalid_address = static_cast<Address>(0) - 1;
4750
4751 Stub_table(Target_powerpc<size, big_endian>* targ,
4752 Output_section* output_section,
4753 const Output_section::Input_section* owner,
4754 uint32_t id)
4755 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4756 owner->relobj()
4757 ->section_addralign(owner->shndx())),
4758 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4759 orig_data_size_(owner->current_data_size()),
4760 plt_size_(0), last_plt_size_(0),
4761 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4762 need_save_res_(false), need_resize_(false), resizing_(false),
4763 uniq_(id)
4764 {
4765 this->set_output_section(output_section);
4766
4767 std::vector<Output_relaxed_input_section*> new_relaxed;
4768 new_relaxed.push_back(this);
4769 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4770 }
4771
4772 // Add a plt call stub.
4773 bool
4774 add_plt_call_entry(Address,
4775 const Sized_relobj_file<size, big_endian>*,
4776 const Symbol*,
4777 unsigned int,
4778 Address,
4779 bool);
4780
4781 bool
4782 add_plt_call_entry(Address,
4783 const Sized_relobj_file<size, big_endian>*,
4784 unsigned int,
4785 unsigned int,
4786 Address,
4787 bool);
4788
4789 // Find a given plt call stub.
4790 const Plt_stub_ent*
4791 find_plt_call_entry(const Symbol*) const;
4792
4793 const Plt_stub_ent*
4794 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4795 unsigned int) const;
4796
4797 const Plt_stub_ent*
4798 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4799 const Symbol*,
4800 unsigned int,
4801 Address) const;
4802
4803 const Plt_stub_ent*
4804 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4805 unsigned int,
4806 unsigned int,
4807 Address) const;
4808
4809 // Add a long branch stub.
4810 bool
4811 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
4812
4813 const Branch_stub_ent*
4814 find_long_branch_entry(Address) const;
4815
4816 bool
4817 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4818 {
4819 Address max_branch_offset = max_branch_delta<size>(r_type);
4820 if (max_branch_offset == 0)
4821 return true;
4822 gold_assert(from != invalid_address);
4823 Address loc = off + this->stub_address();
4824 return loc - from + max_branch_offset < 2 * max_branch_offset;
4825 }
4826
4827 void
4828 clear_stubs(bool all)
4829 {
4830 this->plt_call_stubs_.clear();
4831 this->plt_size_ = 0;
4832 this->long_branch_stubs_.clear();
4833 this->branch_size_ = 0;
4834 this->need_save_res_ = false;
4835 if (all)
4836 {
4837 this->last_plt_size_ = 0;
4838 this->last_branch_size_ = 0;
4839 }
4840 }
4841
4842 bool
4843 need_resize() const
4844 { return need_resize_; }
4845
4846 void
4847 set_resizing(bool val)
4848 {
4849 this->resizing_ = val;
4850 if (val)
4851 {
4852 this->need_resize_ = false;
4853 this->plt_size_ = 0;
4854 this->branch_size_ = 0;
4855 this->need_save_res_ = false;
4856 }
4857 }
4858
4859 Address
4860 set_address_and_size(const Output_section* os, Address off)
4861 {
4862 Address start_off = off;
4863 off += this->orig_data_size_;
4864 Address my_size = this->plt_size_ + this->branch_size_;
4865 if (this->need_save_res_)
4866 my_size += this->targ_->savres_section()->data_size();
4867 if (my_size != 0)
4868 off = align_address(off, this->stub_align());
4869 // Include original section size and alignment padding in size
4870 my_size += off - start_off;
4871 // Ensure new size is always larger than min size
4872 // threshold. Alignment requirement is included in "my_size", so
4873 // increase "my_size" does not invalidate alignment.
4874 if (my_size < this->min_size_threshold_)
4875 my_size = this->min_size_threshold_;
4876 this->reset_address_and_file_offset();
4877 this->set_current_data_size(my_size);
4878 this->set_address_and_file_offset(os->address() + start_off,
4879 os->offset() + start_off);
4880 return my_size;
4881 }
4882
4883 Address
4884 stub_address() const
4885 {
4886 return align_address(this->address() + this->orig_data_size_,
4887 this->stub_align());
4888 }
4889
4890 Address
4891 stub_offset() const
4892 {
4893 return align_address(this->offset() + this->orig_data_size_,
4894 this->stub_align());
4895 }
4896
4897 section_size_type
4898 plt_size() const
4899 { return this->plt_size_; }
4900
4901 section_size_type
4902 branch_size() const
4903 { return this->branch_size_; }
4904
4905 void
4906 set_min_size_threshold(Address min_size)
4907 { this->min_size_threshold_ = min_size; }
4908
4909 void
4910 define_stub_syms(Symbol_table*);
4911
4912 bool
4913 size_update()
4914 {
4915 Output_section* os = this->output_section();
4916 if (os->addralign() < this->stub_align())
4917 {
4918 os->set_addralign(this->stub_align());
4919 // FIXME: get rid of the insane checkpointing.
4920 // We can't increase alignment of the input section to which
4921 // stubs are attached; The input section may be .init which
4922 // is pasted together with other .init sections to form a
4923 // function. Aligning might insert zero padding resulting in
4924 // sigill. However we do need to increase alignment of the
4925 // output section so that the align_address() on offset in
4926 // set_address_and_size() adds the same padding as the
4927 // align_address() on address in stub_address().
4928 // What's more, we need this alignment for the layout done in
4929 // relaxation_loop_body() so that the output section starts at
4930 // a suitably aligned address.
4931 os->checkpoint_set_addralign(this->stub_align());
4932 }
4933 if (this->last_plt_size_ != this->plt_size_
4934 || this->last_branch_size_ != this->branch_size_)
4935 {
4936 this->last_plt_size_ = this->plt_size_;
4937 this->last_branch_size_ = this->branch_size_;
4938 return true;
4939 }
4940 return false;
4941 }
4942
4943 // Add .eh_frame info for this stub section.
4944 void
4945 add_eh_frame(Layout* layout);
4946
4947 // Remove .eh_frame info for this stub section.
4948 void
4949 remove_eh_frame(Layout* layout);
4950
4951 Target_powerpc<size, big_endian>*
4952 targ() const
4953 { return targ_; }
4954
4955 private:
4956 class Plt_stub_key;
4957 class Plt_stub_key_hash;
4958 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4959 Plt_stub_key_hash> Plt_stub_entries;
4960 class Branch_stub_key;
4961 class Branch_stub_key_hash;
4962 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4963 Branch_stub_key_hash> Branch_stub_entries;
4964
4965 // Alignment of stub section.
4966 unsigned int
4967 stub_align() const
4968 {
4969 unsigned int min_align = size == 64 ? 32 : 16;
4970 unsigned int user_align = 1 << parameters->options().plt_align();
4971 return std::max(user_align, min_align);
4972 }
4973
4974 // Return the plt offset for the given call stub.
4975 Address
4976 plt_off(typename Plt_stub_entries::const_iterator p,
4977 const Output_data_plt_powerpc<size, big_endian>** sec) const
4978 {
4979 const Symbol* gsym = p->first.sym_;
4980 if (gsym != NULL)
4981 return this->targ_->plt_off(gsym, sec);
4982 else
4983 {
4984 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4985 unsigned int local_sym_index = p->first.locsym_;
4986 return this->targ_->plt_off(relobj, local_sym_index, sec);
4987 }
4988 }
4989
4990 // Size of a given plt call stub.
4991 unsigned int
4992 plt_call_size(typename Plt_stub_entries::iterator p) const;
4993
4994 unsigned int
4995 plt_call_align(unsigned int bytes) const
4996 {
4997 unsigned int align = param_plt_align<size>();
4998 return (bytes + align - 1) & -align;
4999 }
5000
5001 // Return long branch stub size.
5002 unsigned int
5003 branch_stub_size(typename Branch_stub_entries::iterator p,
5004 bool* need_lt);
5005
5006 void
5007 build_tls_opt_head(unsigned char** pp, bool save_lr);
5008
5009 void
5010 build_tls_opt_tail(unsigned char* p);
5011
5012 void
5013 plt_error(const Plt_stub_key& p);
5014
5015 // Write out stubs.
5016 void
5017 do_write(Output_file*);
5018
5019 // Plt call stub keys.
5020 class Plt_stub_key
5021 {
5022 public:
5023 Plt_stub_key(const Symbol* sym)
5024 : sym_(sym), object_(0), addend_(0), locsym_(0)
5025 { }
5026
5027 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5028 unsigned int locsym_index)
5029 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5030 { }
5031
5032 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5033 const Symbol* sym,
5034 unsigned int r_type,
5035 Address addend)
5036 : sym_(sym), object_(0), addend_(0), locsym_(0)
5037 {
5038 if (size != 32)
5039 this->addend_ = addend;
5040 else if (parameters->options().output_is_position_independent()
5041 && (r_type == elfcpp::R_PPC_PLTREL24
5042 || r_type == elfcpp::R_POWERPC_PLTCALL))
5043 {
5044 this->addend_ = addend;
5045 if (this->addend_ >= 32768)
5046 this->object_ = object;
5047 }
5048 }
5049
5050 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5051 unsigned int locsym_index,
5052 unsigned int r_type,
5053 Address addend)
5054 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5055 {
5056 if (size != 32)
5057 this->addend_ = addend;
5058 else if (parameters->options().output_is_position_independent()
5059 && (r_type == elfcpp::R_PPC_PLTREL24
5060 || r_type == elfcpp::R_POWERPC_PLTCALL))
5061 this->addend_ = addend;
5062 }
5063
5064 bool operator==(const Plt_stub_key& that) const
5065 {
5066 return (this->sym_ == that.sym_
5067 && this->object_ == that.object_
5068 && this->addend_ == that.addend_
5069 && this->locsym_ == that.locsym_);
5070 }
5071
5072 const Symbol* sym_;
5073 const Sized_relobj_file<size, big_endian>* object_;
5074 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5075 unsigned int locsym_;
5076 };
5077
5078 class Plt_stub_key_hash
5079 {
5080 public:
5081 size_t operator()(const Plt_stub_key& ent) const
5082 {
5083 return (reinterpret_cast<uintptr_t>(ent.sym_)
5084 ^ reinterpret_cast<uintptr_t>(ent.object_)
5085 ^ ent.addend_
5086 ^ ent.locsym_);
5087 }
5088 };
5089
5090 // Long branch stub keys.
5091 class Branch_stub_key
5092 {
5093 public:
5094 Branch_stub_key(Address to)
5095 : dest_(to)
5096 { }
5097
5098 bool operator==(const Branch_stub_key& that) const
5099 {
5100 return this->dest_ == that.dest_;
5101 }
5102
5103 Address dest_;
5104 };
5105
5106 class Branch_stub_key_hash
5107 {
5108 public:
5109 size_t operator()(const Branch_stub_key& key) const
5110 { return key.dest_; }
5111 };
5112
5113 // In a sane world this would be a global.
5114 Target_powerpc<size, big_endian>* targ_;
5115 // Map sym/object/addend to stub offset.
5116 Plt_stub_entries plt_call_stubs_;
5117 // Map destination address to stub offset.
5118 Branch_stub_entries long_branch_stubs_;
5119 // size of input section
5120 section_size_type orig_data_size_;
5121 // size of stubs
5122 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5123 // Some rare cases cause (PR/20529) fluctuation in stub table
5124 // size, which leads to an endless relax loop. This is to be fixed
5125 // by, after the first few iterations, allowing only increase of
5126 // stub table size. This variable sets the minimal possible size of
5127 // a stub table, it is zero for the first few iterations, then
5128 // increases monotonically.
5129 Address min_size_threshold_;
5130 // Set if this stub group needs a copy of out-of-line register
5131 // save/restore functions.
5132 bool need_save_res_;
5133 // Set when notoc_/r2save_ changes after sizing a stub
5134 bool need_resize_;
5135 // Set when resizing stubs
5136 bool resizing_;
5137 // Per stub table unique identifier.
5138 uint32_t uniq_;
5139 };
5140
5141 // Add a plt call stub, if we do not already have one for this
5142 // sym/object/addend combo.
5143
5144 template<int size, bool big_endian>
5145 bool
5146 Stub_table<size, big_endian>::add_plt_call_entry(
5147 Address from,
5148 const Sized_relobj_file<size, big_endian>* object,
5149 const Symbol* gsym,
5150 unsigned int r_type,
5151 Address addend,
5152 bool tocsave)
5153 {
5154 Plt_stub_key key(object, gsym, r_type, addend);
5155 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5156 std::pair<typename Plt_stub_entries::iterator, bool> p
5157 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5158 if (size == 64)
5159 {
5160 if (p.second
5161 && this->targ_->is_elfv2_localentry0(gsym))
5162 {
5163 p.first->second.localentry0_ = 1;
5164 this->targ_->set_has_localentry0();
5165 }
5166 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5167 {
5168 if (!p.second && !p.first->second.notoc_
5169 && (!this->targ_->power10_stubs()
5170 || this->targ_->power10_stubs_auto()))
5171 this->need_resize_ = true;
5172 p.first->second.notoc_ = 1;
5173 }
5174 else
5175 {
5176 if (!p.second && !p.first->second.toc_)
5177 this->need_resize_ = true;
5178 p.first->second.toc_ = 1;
5179 if (!tocsave && !p.first->second.localentry0_)
5180 {
5181 if (!p.second && !p.first->second.r2save_)
5182 this->need_resize_ = true;
5183 p.first->second.r2save_ = 1;
5184 }
5185 }
5186 }
5187 if (p.second || (this->resizing_ && !p.first->second.iter_))
5188 {
5189 if (this->resizing_)
5190 {
5191 p.first->second.iter_ = 1;
5192 p.first->second.off_ = this->plt_size_;
5193 }
5194 this->plt_size_ += this->plt_call_size(p.first);
5195 if (this->targ_->is_tls_get_addr_opt(gsym))
5196 this->targ_->set_has_tls_get_addr_opt();
5197 this->plt_size_ = this->plt_call_align(this->plt_size_);
5198 }
5199 return this->can_reach_stub(from, p.first->second.off_, r_type);
5200 }
5201
5202 template<int size, bool big_endian>
5203 bool
5204 Stub_table<size, big_endian>::add_plt_call_entry(
5205 Address from,
5206 const Sized_relobj_file<size, big_endian>* object,
5207 unsigned int locsym_index,
5208 unsigned int r_type,
5209 Address addend,
5210 bool tocsave)
5211 {
5212 Plt_stub_key key(object, locsym_index, r_type, addend);
5213 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5214 std::pair<typename Plt_stub_entries::iterator, bool> p
5215 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5216 if (size == 64)
5217 {
5218 if (p.second
5219 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5220 {
5221 p.first->second.localentry0_ = 1;
5222 this->targ_->set_has_localentry0();
5223 }
5224 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5225 {
5226 if (!p.second && !p.first->second.notoc_
5227 && (!this->targ_->power10_stubs()
5228 || this->targ_->power10_stubs_auto()))
5229 this->need_resize_ = true;
5230 p.first->second.notoc_ = 1;
5231 }
5232 else
5233 {
5234 if (!p.second && !p.first->second.toc_)
5235 this->need_resize_ = true;
5236 p.first->second.toc_ = 1;
5237 if (!tocsave && !p.first->second.localentry0_)
5238 {
5239 if (!p.second && !p.first->second.r2save_)
5240 this->need_resize_ = true;
5241 p.first->second.r2save_ = 1;
5242 }
5243 }
5244 }
5245 if (p.second || (this->resizing_ && !p.first->second.iter_))
5246 {
5247 if (this->resizing_)
5248 {
5249 p.first->second.iter_ = 1;
5250 p.first->second.off_ = this->plt_size_;
5251 }
5252 this->plt_size_ += this->plt_call_size(p.first);
5253 this->plt_size_ = this->plt_call_align(this->plt_size_);
5254 }
5255 return this->can_reach_stub(from, p.first->second.off_, r_type);
5256 }
5257
5258 // Find a plt call stub.
5259
5260 template<int size, bool big_endian>
5261 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5262 Stub_table<size, big_endian>::find_plt_call_entry(
5263 const Sized_relobj_file<size, big_endian>* object,
5264 const Symbol* gsym,
5265 unsigned int r_type,
5266 Address addend) const
5267 {
5268 Plt_stub_key key(object, gsym, r_type, addend);
5269 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5270 if (p == this->plt_call_stubs_.end())
5271 return NULL;
5272 return &p->second;
5273 }
5274
5275 template<int size, bool big_endian>
5276 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5277 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5278 {
5279 Plt_stub_key key(gsym);
5280 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5281 if (p == this->plt_call_stubs_.end())
5282 return NULL;
5283 return &p->second;
5284 }
5285
5286 template<int size, bool big_endian>
5287 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5288 Stub_table<size, big_endian>::find_plt_call_entry(
5289 const Sized_relobj_file<size, big_endian>* object,
5290 unsigned int locsym_index,
5291 unsigned int r_type,
5292 Address addend) const
5293 {
5294 Plt_stub_key key(object, locsym_index, r_type, addend);
5295 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5296 if (p == this->plt_call_stubs_.end())
5297 return NULL;
5298 return &p->second;
5299 }
5300
5301 template<int size, bool big_endian>
5302 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5303 Stub_table<size, big_endian>::find_plt_call_entry(
5304 const Sized_relobj_file<size, big_endian>* object,
5305 unsigned int locsym_index) const
5306 {
5307 Plt_stub_key key(object, locsym_index);
5308 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5309 if (p == this->plt_call_stubs_.end())
5310 return NULL;
5311 return &p->second;
5312 }
5313
5314 // Add a long branch stub if we don't already have one to given
5315 // destination.
5316
5317 template<int size, bool big_endian>
5318 bool
5319 Stub_table<size, big_endian>::add_long_branch_entry(
5320 unsigned int r_type,
5321 Address from,
5322 Address to,
5323 unsigned int other,
5324 bool save_res)
5325 {
5326 Branch_stub_key key(to);
5327 bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5328 Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5329 std::pair<typename Branch_stub_entries::iterator, bool> p
5330 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5331 if (notoc)
5332 {
5333 if (!p.second && !p.first->second.notoc_)
5334 this->need_resize_ = true;
5335 p.first->second.notoc_ = true;
5336 }
5337 else
5338 {
5339 if (!p.second && !p.first->second.toc_)
5340 this->need_resize_ = true;
5341 p.first->second.toc_ = true;
5342 }
5343 if (size == 64 && p.first->second.other_ == 0)
5344 p.first->second.other_ = other;
5345 gold_assert(save_res == p.first->second.save_res_);
5346 if (p.second || (this->resizing_ && !p.first->second.iter_))
5347 {
5348 if (this->resizing_)
5349 {
5350 p.first->second.iter_ = 1;
5351 p.first->second.off_ = this->branch_size_;
5352 }
5353 if (save_res)
5354 this->need_save_res_ = true;
5355 else
5356 {
5357 bool need_lt = false;
5358 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5359 this->branch_size_ += stub_size;
5360 if (size == 64 && need_lt)
5361 this->targ_->add_branch_lookup_table(to);
5362 }
5363 }
5364 return this->can_reach_stub(from, p.first->second.off_, r_type);
5365 }
5366
5367 // Find long branch stub offset.
5368
5369 template<int size, bool big_endian>
5370 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5371 Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
5372 {
5373 Branch_stub_key key(to);
5374 typename Branch_stub_entries::const_iterator p
5375 = this->long_branch_stubs_.find(key);
5376 if (p == this->long_branch_stubs_.end())
5377 return NULL;
5378 return &p->second;
5379 }
5380
5381 template<bool big_endian>
5382 static void
5383 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5384 {
5385 delta /= 4;
5386 if (delta < 64)
5387 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5388 else if (delta < 256)
5389 {
5390 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5391 fde.push_back(delta);
5392 }
5393 else if (delta < 65536)
5394 {
5395 fde.resize(fde.size() + 3);
5396 unsigned char *p = &*fde.end() - 3;
5397 *p++ = elfcpp::DW_CFA_advance_loc2;
5398 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5399 }
5400 else
5401 {
5402 fde.resize(fde.size() + 5);
5403 unsigned char *p = &*fde.end() - 5;
5404 *p++ = elfcpp::DW_CFA_advance_loc4;
5405 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5406 }
5407 }
5408
5409 template<typename T>
5410 static bool
5411 stub_sort(T s1, T s2)
5412 {
5413 return s1->second.off_ < s2->second.off_;
5414 }
5415
5416 // Add .eh_frame info for this stub section. Unlike other linker
5417 // generated .eh_frame this is added late in the link, because we
5418 // only want the .eh_frame info if this particular stub section is
5419 // non-empty.
5420
5421 template<int size, bool big_endian>
5422 void
5423 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5424 {
5425 if (size != 64
5426 || !parameters->options().ld_generated_unwind_info())
5427 return;
5428
5429 // Since we add stub .eh_frame info late, it must be placed
5430 // after all other linker generated .eh_frame info so that
5431 // merge mapping need not be updated for input sections.
5432 // There is no provision to use a different CIE to that used
5433 // by .glink.
5434 if (!this->targ_->has_glink())
5435 return;
5436
5437 typedef typename Plt_stub_entries::iterator plt_iter;
5438 std::vector<plt_iter> calls;
5439 if (!this->plt_call_stubs_.empty())
5440 for (plt_iter cs = this->plt_call_stubs_.begin();
5441 cs != this->plt_call_stubs_.end();
5442 ++cs)
5443 if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5444 && cs->second.r2save_
5445 && !cs->second.localentry0_)
5446 || (cs->second.notoc_
5447 && !this->targ_->power10_stubs()))
5448 calls.push_back(cs);
5449 if (calls.size() > 1)
5450 std::stable_sort(calls.begin(), calls.end(),
5451 stub_sort<plt_iter>);
5452
5453 typedef typename Branch_stub_entries::const_iterator branch_iter;
5454 std::vector<branch_iter> branches;
5455 if (!this->long_branch_stubs_.empty()
5456 && !this->targ_->power10_stubs())
5457 for (branch_iter bs = this->long_branch_stubs_.begin();
5458 bs != this->long_branch_stubs_.end();
5459 ++bs)
5460 if (bs->second.notoc_)
5461 branches.push_back(bs);
5462 if (branches.size() > 1)
5463 std::stable_sort(branches.begin(), branches.end(),
5464 stub_sort<branch_iter>);
5465
5466 if (calls.empty() && branches.empty())
5467 return;
5468
5469 unsigned int last_eh_loc = 0;
5470 // offset pcrel sdata4, size udata4, and augmentation size byte.
5471 std::vector<unsigned char> fde(9, 0);
5472
5473 for (unsigned int i = 0; i < calls.size(); i++)
5474 {
5475 plt_iter cs = calls[i];
5476 unsigned int off = cs->second.off_;
5477 // The __tls_get_addr_opt call stub needs to describe where
5478 // it saves LR, to support exceptions that might be thrown
5479 // from __tls_get_addr, and to support asynchronous exceptions.
5480 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5481 {
5482 off += 7 * 4;
5483 if (cs->second.r2save_
5484 && !cs->second.localentry0_)
5485 {
5486 off += 2 * 4;
5487 eh_advance<big_endian>(fde, off - last_eh_loc);
5488 fde.resize(fde.size() + 6);
5489 unsigned char* p = &*fde.end() - 6;
5490 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5491 *p++ = 65;
5492 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5493 unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5494 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5495 *p++ = elfcpp::DW_CFA_restore_extended;
5496 *p++ = 65;
5497 last_eh_loc = off + delta;
5498 continue;
5499 }
5500 }
5501 // notoc stubs also should describe LR changes, to support
5502 // asynchronous exceptions.
5503 off += (cs->second.r2save_ ? 4 : 0) + 8;
5504 eh_advance<big_endian>(fde, off - last_eh_loc);
5505 fde.resize(fde.size() + 6);
5506 unsigned char* p = &*fde.end() - 6;
5507 *p++ = elfcpp::DW_CFA_register;
5508 *p++ = 65;
5509 *p++ = 12;
5510 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5511 *p++ = elfcpp::DW_CFA_restore_extended;
5512 *p++ = 65;
5513 last_eh_loc = off + 8;
5514 }
5515
5516 for (unsigned int i = 0; i < branches.size(); i++)
5517 {
5518 branch_iter bs = branches[i];
5519 unsigned int off = bs->second.off_ + 8;
5520 eh_advance<big_endian>(fde, off - last_eh_loc);
5521 fde.resize(fde.size() + 6);
5522 unsigned char* p = &*fde.end() - 6;
5523 *p++ = elfcpp::DW_CFA_register;
5524 *p++ = 65;
5525 *p++ = 12;
5526 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5527 *p++ = elfcpp::DW_CFA_restore_extended;
5528 *p++ = 65;
5529 last_eh_loc = off + 8;
5530 }
5531
5532 layout->add_eh_frame_for_plt(this,
5533 Eh_cie<size>::eh_frame_cie,
5534 sizeof (Eh_cie<size>::eh_frame_cie),
5535 &*fde.begin(), fde.size());
5536 }
5537
5538 template<int size, bool big_endian>
5539 void
5540 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5541 {
5542 if (size == 64
5543 && parameters->options().ld_generated_unwind_info()
5544 && this->targ_->has_glink())
5545 layout->remove_eh_frame_for_plt(this,
5546 Eh_cie<size>::eh_frame_cie,
5547 sizeof (Eh_cie<size>::eh_frame_cie));
5548 }
5549
5550 // A class to handle .glink.
5551
5552 template<int size, bool big_endian>
5553 class Output_data_glink : public Output_section_data
5554 {
5555 public:
5556 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5557 static const Address invalid_address = static_cast<Address>(0) - 1;
5558
5559 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5560 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5561 end_branch_table_(), ge_size_(0)
5562 { }
5563
5564 void
5565 add_eh_frame(Layout* layout);
5566
5567 void
5568 add_global_entry(const Symbol*);
5569
5570 Address
5571 find_global_entry(const Symbol*) const;
5572
5573 unsigned int
5574 global_entry_align(unsigned int off) const
5575 {
5576 unsigned int align = param_plt_align<size>();
5577 return (off + align - 1) & -align;
5578 }
5579
5580 unsigned int
5581 global_entry_off() const
5582 {
5583 return this->global_entry_align(this->end_branch_table_);
5584 }
5585
5586 Address
5587 global_entry_address() const
5588 {
5589 gold_assert(this->is_data_size_valid());
5590 return this->address() + this->global_entry_off();
5591 }
5592
5593 int
5594 pltresolve_size() const
5595 {
5596 if (size == 64)
5597 return (8
5598 + (this->targ_->abiversion() < 2 ? 11 * 4
5599 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5600 return 16 * 4;
5601 }
5602
5603 protected:
5604 // Write to a map file.
5605 void
5606 do_print_to_mapfile(Mapfile* mapfile) const
5607 { mapfile->print_output_data(this, _("** glink")); }
5608
5609 private:
5610 void
5611 set_final_data_size();
5612
5613 // Write out .glink
5614 void
5615 do_write(Output_file*);
5616
5617 // Allows access to .got and .plt for do_write.
5618 Target_powerpc<size, big_endian>* targ_;
5619
5620 // Map sym to stub offset.
5621 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5622 Global_entry_stub_entries global_entry_stubs_;
5623
5624 unsigned int end_branch_table_, ge_size_;
5625 };
5626
5627 template<int size, bool big_endian>
5628 void
5629 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5630 {
5631 if (!parameters->options().ld_generated_unwind_info())
5632 return;
5633
5634 if (size == 64)
5635 {
5636 if (this->targ_->abiversion() < 2)
5637 layout->add_eh_frame_for_plt(this,
5638 Eh_cie<64>::eh_frame_cie,
5639 sizeof (Eh_cie<64>::eh_frame_cie),
5640 glink_eh_frame_fde_64v1,
5641 sizeof (glink_eh_frame_fde_64v1));
5642 else if (this->targ_->has_localentry0())
5643 layout->add_eh_frame_for_plt(this,
5644 Eh_cie<64>::eh_frame_cie,
5645 sizeof (Eh_cie<64>::eh_frame_cie),
5646 glink_eh_frame_fde_64v2_localentry0,
5647 sizeof (glink_eh_frame_fde_64v2));
5648 else
5649 layout->add_eh_frame_for_plt(this,
5650 Eh_cie<64>::eh_frame_cie,
5651 sizeof (Eh_cie<64>::eh_frame_cie),
5652 glink_eh_frame_fde_64v2,
5653 sizeof (glink_eh_frame_fde_64v2));
5654 }
5655 else
5656 {
5657 // 32-bit .glink can use the default since the CIE return
5658 // address reg, LR, is valid.
5659 layout->add_eh_frame_for_plt(this,
5660 Eh_cie<32>::eh_frame_cie,
5661 sizeof (Eh_cie<32>::eh_frame_cie),
5662 default_fde,
5663 sizeof (default_fde));
5664 // Except where LR is used in a PIC __glink_PLTresolve.
5665 if (parameters->options().output_is_position_independent())
5666 layout->add_eh_frame_for_plt(this,
5667 Eh_cie<32>::eh_frame_cie,
5668 sizeof (Eh_cie<32>::eh_frame_cie),
5669 glink_eh_frame_fde_32,
5670 sizeof (glink_eh_frame_fde_32));
5671 }
5672 }
5673
5674 template<int size, bool big_endian>
5675 void
5676 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5677 {
5678 unsigned int off = this->global_entry_align(this->ge_size_);
5679 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5680 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5681 if (p.second)
5682 this->ge_size_ = off + 16;
5683 }
5684
5685 template<int size, bool big_endian>
5686 typename Output_data_glink<size, big_endian>::Address
5687 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5688 {
5689 typename Global_entry_stub_entries::const_iterator p
5690 = this->global_entry_stubs_.find(gsym);
5691 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5692 }
5693
5694 template<int size, bool big_endian>
5695 void
5696 Output_data_glink<size, big_endian>::set_final_data_size()
5697 {
5698 unsigned int count = this->targ_->plt_entry_count();
5699 section_size_type total = 0;
5700
5701 if (count != 0)
5702 {
5703 if (size == 32)
5704 {
5705 // space for branch table
5706 total += 4 * (count - 1);
5707
5708 total += -total & 15;
5709 total += this->pltresolve_size();
5710 }
5711 else
5712 {
5713 total += this->pltresolve_size();
5714
5715 // space for branch table
5716 total += 4 * count;
5717 if (this->targ_->abiversion() < 2)
5718 {
5719 total += 4 * count;
5720 if (count > 0x8000)
5721 total += 4 * (count - 0x8000);
5722 }
5723 }
5724 }
5725 this->end_branch_table_ = total;
5726 total = this->global_entry_align(total);
5727 total += this->ge_size_;
5728
5729 this->set_data_size(total);
5730 }
5731
5732 // Define symbols on stubs, identifying the stub.
5733
5734 template<int size, bool big_endian>
5735 void
5736 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5737 {
5738 if (!this->plt_call_stubs_.empty())
5739 {
5740 // The key for the plt call stub hash table includes addresses,
5741 // therefore traversal order depends on those addresses, which
5742 // can change between runs if gold is a PIE. Unfortunately the
5743 // output .symtab ordering depends on the order in which symbols
5744 // are added to the linker symtab. We want reproducible output
5745 // so must sort the call stub symbols.
5746 typedef typename Plt_stub_entries::iterator plt_iter;
5747 std::vector<plt_iter> sorted;
5748 sorted.resize(this->plt_call_stubs_.size());
5749
5750 for (plt_iter cs = this->plt_call_stubs_.begin();
5751 cs != this->plt_call_stubs_.end();
5752 ++cs)
5753 sorted[cs->second.indx_] = cs;
5754
5755 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5756 {
5757 plt_iter cs = sorted[i];
5758 char add[10];
5759 add[0] = 0;
5760 if (cs->first.addend_ != 0)
5761 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5762 char obj[10];
5763 obj[0] = 0;
5764 if (cs->first.object_)
5765 {
5766 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5767 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5768 sprintf(obj, "%x:", ppcobj->uniq());
5769 }
5770 char localname[9];
5771 const char *symname;
5772 if (cs->first.sym_ == NULL)
5773 {
5774 sprintf(localname, "%x", cs->first.locsym_);
5775 symname = localname;
5776 }
5777 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5778 symname = this->targ_->tls_get_addr_opt()->name();
5779 else
5780 symname = cs->first.sym_->name();
5781 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5782 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5783 Address value
5784 = this->stub_address() - this->address() + cs->second.off_;
5785 unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5786 this->targ_->define_local(symtab, name, this, value, stub_size);
5787 }
5788 }
5789
5790 typedef typename Branch_stub_entries::iterator branch_iter;
5791 for (branch_iter bs = this->long_branch_stubs_.begin();
5792 bs != this->long_branch_stubs_.end();
5793 ++bs)
5794 {
5795 if (bs->second.save_res_)
5796 continue;
5797
5798 char* name = new char[8 + 13 + 16 + 1];
5799 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5800 static_cast<unsigned long long>(bs->first.dest_));
5801 Address value = (this->stub_address() - this->address()
5802 + this->plt_size_ + bs->second.off_);
5803 bool need_lt = false;
5804 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5805 this->targ_->define_local(symtab, name, this, value, stub_size);
5806 }
5807 }
5808
5809 // Emit the start of a __tls_get_addr_opt plt call stub.
5810
5811 template<int size, bool big_endian>
5812 void
5813 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5814 bool save_lr)
5815 {
5816 unsigned char* p = *pp;
5817 if (size == 64)
5818 {
5819 write_insn<big_endian>(p, ld_11_3 + 0);
5820 p += 4;
5821 write_insn<big_endian>(p, ld_12_3 + 8);
5822 p += 4;
5823 write_insn<big_endian>(p, mr_0_3);
5824 p += 4;
5825 write_insn<big_endian>(p, cmpdi_11_0);
5826 p += 4;
5827 write_insn<big_endian>(p, add_3_12_13);
5828 p += 4;
5829 write_insn<big_endian>(p, beqlr);
5830 p += 4;
5831 write_insn<big_endian>(p, mr_3_0);
5832 p += 4;
5833 if (save_lr)
5834 {
5835 write_insn<big_endian>(p, mflr_11);
5836 p += 4;
5837 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5838 p += 4;
5839 }
5840 }
5841 else
5842 {
5843 write_insn<big_endian>(p, lwz_11_3 + 0);
5844 p += 4;
5845 write_insn<big_endian>(p, lwz_12_3 + 4);
5846 p += 4;
5847 write_insn<big_endian>(p, mr_0_3);
5848 p += 4;
5849 write_insn<big_endian>(p, cmpwi_11_0);
5850 p += 4;
5851 write_insn<big_endian>(p, add_3_12_2);
5852 p += 4;
5853 write_insn<big_endian>(p, beqlr);
5854 p += 4;
5855 write_insn<big_endian>(p, mr_3_0);
5856 p += 4;
5857 write_insn<big_endian>(p, nop);
5858 p += 4;
5859 }
5860 *pp = p;
5861 }
5862
5863 // Emit the tail of a __tls_get_addr_opt plt call stub.
5864
5865 template<int size, bool big_endian>
5866 void
5867 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5868 {
5869 write_insn<big_endian>(p, bctrl);
5870 p += 4;
5871 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5872 p += 4;
5873 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5874 p += 4;
5875 write_insn<big_endian>(p, mtlr_11);
5876 p += 4;
5877 write_insn<big_endian>(p, blr);
5878 }
5879
5880 // Emit pc-relative plt call stub code.
5881
5882 template<bool big_endian>
5883 static unsigned char*
5884 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5885 {
5886 uint64_t insn;
5887 if (off - odd + (1ULL << 33) < 1ULL << 34)
5888 {
5889 off -= odd;
5890 if (odd)
5891 {
5892 write_insn<big_endian>(p, nop);
5893 p += 4;
5894 }
5895 if (load)
5896 insn = pld_12_pc;
5897 else
5898 insn = paddi_12_pc;
5899 insn |= d34(off);
5900 write_insn<big_endian>(p, insn >> 32);
5901 p += 4;
5902 write_insn<big_endian>(p, insn & 0xffffffff);
5903 }
5904 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5905 {
5906 off -= 8 - odd;
5907 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5908 p += 4;
5909 if (!odd)
5910 {
5911 write_insn<big_endian>(p, sldi_11_11_34);
5912 p += 4;
5913 }
5914 insn = paddi_12_pc | d34(off);
5915 write_insn<big_endian>(p, insn >> 32);
5916 p += 4;
5917 write_insn<big_endian>(p, insn & 0xffffffff);
5918 p += 4;
5919 if (odd)
5920 {
5921 write_insn<big_endian>(p, sldi_11_11_34);
5922 p += 4;
5923 }
5924 if (load)
5925 write_insn<big_endian>(p, ldx_12_11_12);
5926 else
5927 write_insn<big_endian>(p, add_12_11_12);
5928 }
5929 else
5930 {
5931 off -= odd + 8;
5932 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5933 p += 4;
5934 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5935 p += 4;
5936 if (odd)
5937 {
5938 write_insn<big_endian>(p, sldi_11_11_34);
5939 p += 4;
5940 }
5941 insn = paddi_12_pc | d34(off);
5942 write_insn<big_endian>(p, insn >> 32);
5943 p += 4;
5944 write_insn<big_endian>(p, insn & 0xffffffff);
5945 p += 4;
5946 if (!odd)
5947 {
5948 write_insn<big_endian>(p, sldi_11_11_34);
5949 p += 4;
5950 }
5951 if (load)
5952 write_insn<big_endian>(p, ldx_12_11_12);
5953 else
5954 write_insn<big_endian>(p, add_12_11_12);
5955 }
5956 p += 4;
5957 return p;
5958 }
5959
5960 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5961 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5962 // mflr %r12
5963 // bcl 20,31,1f
5964 // 1: mflr %r11
5965 // mtlr %r12
5966 // lis %r12,xxx-1b@highest
5967 // ori %r12,%r12,xxx-1b@higher
5968 // sldi %r12,%r12,32
5969 // oris %r12,%r12,xxx-1b@high
5970 // ori %r12,%r12,xxx-1b@l
5971 // add/ldx %r12,%r11,%r12
5972
5973 template<bool big_endian>
5974 static unsigned char*
5975 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5976 {
5977 write_insn<big_endian>(p, mflr_12);
5978 p += 4;
5979 write_insn<big_endian>(p, bcl_20_31);
5980 p += 4;
5981 write_insn<big_endian>(p, mflr_11);
5982 p += 4;
5983 write_insn<big_endian>(p, mtlr_12);
5984 p += 4;
5985 if (off + 0x8000 < 0x10000)
5986 {
5987 if (load)
5988 write_insn<big_endian>(p, ld_12_11 + l(off));
5989 else
5990 write_insn<big_endian>(p, addi_12_11 + l(off));
5991 }
5992 else if (off + 0x80008000ULL < 0x100000000ULL)
5993 {
5994 write_insn<big_endian>(p, addis_12_11 + ha(off));
5995 p += 4;
5996 if (load)
5997 write_insn<big_endian>(p, ld_12_12 + l(off));
5998 else
5999 write_insn<big_endian>(p, addi_12_12 + l(off));
6000 }
6001 else
6002 {
6003 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6004 {
6005 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6006 p += 4;
6007 }
6008 else
6009 {
6010 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6011 p += 4;
6012 if (((off >> 32) & 0xffff) != 0)
6013 {
6014 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6015 p += 4;
6016 }
6017 }
6018 if (((off >> 32) & 0xffffffffULL) != 0)
6019 {
6020 write_insn<big_endian>(p, sldi_12_12_32);
6021 p += 4;
6022 }
6023 if (hi(off) != 0)
6024 {
6025 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6026 p += 4;
6027 }
6028 if (l(off) != 0)
6029 {
6030 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6031 p += 4;
6032 }
6033 if (load)
6034 write_insn<big_endian>(p, ldx_12_11_12);
6035 else
6036 write_insn<big_endian>(p, add_12_11_12);
6037 }
6038 p += 4;
6039 return p;
6040 }
6041
6042 // Size of a given plt call stub.
6043
6044 template<int size, bool big_endian>
6045 unsigned int
6046 Stub_table<size, big_endian>::plt_call_size(
6047 typename Plt_stub_entries::iterator p) const
6048 {
6049 if (size == 32)
6050 {
6051 const Symbol* gsym = p->first.sym_;
6052 return (4 * 4
6053 + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
6054 }
6055
6056 const Output_data_plt_powerpc<size, big_endian>* plt;
6057 uint64_t plt_addr = this->plt_off(p, &plt);
6058 plt_addr += plt->address();
6059 if (this->targ_->power10_stubs()
6060 && this->targ_->power10_stubs_auto())
6061 {
6062 unsigned int bytes = 0;
6063 if (p->second.notoc_)
6064 {
6065 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6066 bytes = 7 * 4;
6067 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6068 uint64_t odd = from & 4;
6069 uint64_t off = plt_addr - from;
6070 if (off - odd + (1ULL << 33) < 1ULL << 34)
6071 bytes += odd + 4 * 4;
6072 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6073 bytes += 7 * 4;
6074 else
6075 bytes += 8 * 4;
6076 bytes = this->plt_call_align(bytes);
6077 }
6078 unsigned int tail = 0;
6079 if (p->second.toc_)
6080 {
6081 p->second.tocoff_ = bytes;
6082 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6083 {
6084 bytes += 7 * 4;
6085 if (p->second.r2save_ && !p->second.localentry0_)
6086 {
6087 bytes += 2 * 4;
6088 tail = 4 * 4;
6089 }
6090 }
6091 if (p->second.r2save_)
6092 bytes += 4;
6093 uint64_t got_addr = this->targ_->toc_pointer();
6094 uint64_t off = plt_addr - got_addr;
6095 bytes += 3 * 4 + 4 * (ha(off) != 0);
6096 }
6097 return bytes + tail;
6098 }
6099 else
6100 {
6101 unsigned int bytes = 0;
6102 unsigned int tail = 0;
6103 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6104 {
6105 bytes = 7 * 4;
6106 if (p->second.r2save_ && !p->second.localentry0_)
6107 {
6108 bytes = 9 * 4;
6109 tail = 4 * 4;
6110 }
6111 }
6112
6113 if (p->second.r2save_)
6114 bytes += 4;
6115
6116 if (this->targ_->power10_stubs())
6117 {
6118 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6119 uint64_t odd = from & 4;
6120 uint64_t off = plt_addr - from;
6121 if (off - odd + (1ULL << 33) < 1ULL << 34)
6122 bytes += odd + 4 * 4;
6123 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6124 bytes += 7 * 4;
6125 else
6126 bytes += 8 * 4;
6127 return bytes + tail;
6128 }
6129
6130 if (p->second.notoc_)
6131 {
6132 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6133 uint64_t off = plt_addr - from;
6134 if (off + 0x8000 < 0x10000)
6135 bytes += 7 * 4;
6136 else if (off + 0x80008000ULL < 0x100000000ULL)
6137 bytes += 8 * 4;
6138 else
6139 {
6140 bytes += 8 * 4;
6141 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6142 && ((off >> 32) & 0xffff) != 0)
6143 bytes += 4;
6144 if (((off >> 32) & 0xffffffffULL) != 0)
6145 bytes += 4;
6146 if (hi(off) != 0)
6147 bytes += 4;
6148 if (l(off) != 0)
6149 bytes += 4;
6150 }
6151 return bytes + tail;
6152 }
6153
6154 uint64_t got_addr = this->targ_->toc_pointer();
6155 uint64_t off = plt_addr - got_addr;
6156 bytes += 3 * 4 + 4 * (ha(off) != 0);
6157 if (this->targ_->abiversion() < 2)
6158 {
6159 bool static_chain = parameters->options().plt_static_chain();
6160 bool thread_safe = this->targ_->plt_thread_safe();
6161 bytes += (4
6162 + 4 * static_chain
6163 + 8 * thread_safe
6164 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6165 }
6166 return bytes + tail;
6167 }
6168 }
6169
6170 // Return long branch stub size.
6171
6172 template<int size, bool big_endian>
6173 unsigned int
6174 Stub_table<size, big_endian>::branch_stub_size(
6175 typename Branch_stub_entries::iterator p,
6176 bool* need_lt)
6177 {
6178 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6179 if (size == 32)
6180 {
6181 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6182 return 4;
6183 if (parameters->options().output_is_position_independent())
6184 return 32;
6185 return 16;
6186 }
6187
6188 uint64_t off = p->first.dest_ - loc;
6189 unsigned int bytes = 0;
6190 if (p->second.notoc_)
6191 {
6192 if (this->targ_->power10_stubs())
6193 {
6194 Address odd = loc & 4;
6195 if (off + (1 << 25) < 2 << 25)
6196 bytes = odd + 12;
6197 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6198 bytes = odd + 16;
6199 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6200 bytes = 28;
6201 else
6202 bytes = 32;
6203 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6204 return bytes;
6205 p->second.tocoff_ = bytes;
6206 }
6207 else
6208 {
6209 off -= 8;
6210 if (off + 0x8000 < 0x10000)
6211 return 24;
6212 if (off + 0x80008000ULL < 0x100000000ULL)
6213 {
6214 if (off + 24 + (1 << 25) < 2 << 25)
6215 return 28;
6216 return 32;
6217 }
6218
6219 bytes = 32;
6220 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6221 && ((off >> 32) & 0xffff) != 0)
6222 bytes += 4;
6223 if (((off >> 32) & 0xffffffffULL) != 0)
6224 bytes += 4;
6225 if (hi(off) != 0)
6226 bytes += 4;
6227 if (l(off) != 0)
6228 bytes += 4;
6229 return bytes;
6230 }
6231 }
6232
6233 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6234 if (off + (1 << 25) < 2 << 25)
6235 return bytes + 4;
6236 if (!this->targ_->power10_stubs()
6237 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6238 *need_lt = true;
6239 return bytes + 16;
6240 }
6241
6242 template<int size, bool big_endian>
6243 void
6244 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6245 {
6246 if (p.sym_)
6247 gold_error(_("linkage table error against `%s'"),
6248 p.sym_->demangled_name().c_str());
6249 else
6250 gold_error(_("linkage table error against `%s:[local %u]'"),
6251 p.object_->name().c_str(),
6252 p.locsym_);
6253 }
6254
6255 // Write out plt and long branch stub code.
6256
6257 template<int size, bool big_endian>
6258 void
6259 Stub_table<size, big_endian>::do_write(Output_file* of)
6260 {
6261 if (this->plt_call_stubs_.empty()
6262 && this->long_branch_stubs_.empty())
6263 return;
6264
6265 const section_size_type start_off = this->offset();
6266 const section_size_type off = this->stub_offset();
6267 const section_size_type oview_size =
6268 convert_to_section_size_type(this->data_size() - (off - start_off));
6269 unsigned char* const oview = of->get_output_view(off, oview_size);
6270 unsigned char* p;
6271
6272 if (size == 64
6273 && this->targ_->power10_stubs())
6274 {
6275 if (!this->plt_call_stubs_.empty())
6276 {
6277 // Write out plt call stubs.
6278 typename Plt_stub_entries::const_iterator cs;
6279 for (cs = this->plt_call_stubs_.begin();
6280 cs != this->plt_call_stubs_.end();
6281 ++cs)
6282 {
6283 p = oview + cs->second.off_;
6284 const Output_data_plt_powerpc<size, big_endian>* plt;
6285 Address pltoff = this->plt_off(cs, &plt);
6286 Address plt_addr = pltoff + plt->address();
6287 if (this->targ_->power10_stubs_auto())
6288 {
6289 if (cs->second.notoc_)
6290 {
6291 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6292 this->build_tls_opt_head(&p, false);
6293 Address from = this->stub_address() + (p - oview);
6294 Address delta = plt_addr - from;
6295 p = build_power10_offset<big_endian>(p, delta, from & 4,
6296 true);
6297 write_insn<big_endian>(p, mtctr_12);
6298 p += 4;
6299 write_insn<big_endian>(p, bctr);
6300 p += 4;
6301 p = oview + this->plt_call_align(p - oview);
6302 }
6303 if (cs->second.toc_)
6304 {
6305 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6306 {
6307 bool save_lr
6308 = cs->second.r2save_ && !cs->second.localentry0_;
6309 this->build_tls_opt_head(&p, save_lr);
6310 }
6311 Address got_addr = this->targ_->toc_pointer();
6312 Address off = plt_addr - got_addr;
6313
6314 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6315 this->plt_error(cs->first);
6316
6317 if (cs->second.r2save_)
6318 {
6319 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6320 p += 4;
6321 }
6322 if (ha(off) != 0)
6323 {
6324 write_insn<big_endian>(p, addis_12_2 + ha(off));
6325 p += 4;
6326 write_insn<big_endian>(p, ld_12_12 + l(off));
6327 p += 4;
6328 }
6329 else
6330 {
6331 write_insn<big_endian>(p, ld_12_2 + l(off));
6332 p += 4;
6333 }
6334 write_insn<big_endian>(p, mtctr_12);
6335 p += 4;
6336 if (cs->second.r2save_
6337 && !cs->second.localentry0_
6338 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6339 this->build_tls_opt_tail(p);
6340 else
6341 write_insn<big_endian>(p, bctr);
6342 }
6343 }
6344 else
6345 {
6346 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6347 {
6348 bool save_lr
6349 = cs->second.r2save_ && !cs->second.localentry0_;
6350 this->build_tls_opt_head(&p, save_lr);
6351 }
6352 if (cs->second.r2save_)
6353 {
6354 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6355 p += 4;
6356 }
6357 Address from = this->stub_address() + (p - oview);
6358 Address delta = plt_addr - from;
6359 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6360 write_insn<big_endian>(p, mtctr_12);
6361 p += 4;
6362 if (cs->second.r2save_
6363 && !cs->second.localentry0_
6364 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6365 this->build_tls_opt_tail(p);
6366 else
6367 write_insn<big_endian>(p, bctr);
6368 }
6369 }
6370 }
6371
6372 // Write out long branch stubs.
6373 typename Branch_stub_entries::const_iterator bs;
6374 for (bs = this->long_branch_stubs_.begin();
6375 bs != this->long_branch_stubs_.end();
6376 ++bs)
6377 {
6378 if (bs->second.save_res_)
6379 continue;
6380 Address off = this->plt_size_ + bs->second.off_;
6381 p = oview + off;
6382 Address loc = this->stub_address() + off;
6383 Address delta = bs->first.dest_ - loc;
6384 if (this->targ_->power10_stubs_auto())
6385 {
6386 if (bs->second.notoc_)
6387 {
6388 unsigned char* startp = p;
6389 p = build_power10_offset<big_endian>(p, delta,
6390 loc & 4, false);
6391 delta -= p - startp;
6392 startp = p;
6393 if (delta + (1 << 25) < 2 << 25)
6394 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6395 else
6396 {
6397 write_insn<big_endian>(p, mtctr_12);
6398 p += 4;
6399 write_insn<big_endian>(p, bctr);
6400 }
6401 p += 4;
6402 delta -= p - startp;
6403 }
6404 if (bs->second.toc_)
6405 {
6406 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6407 if (delta + (1 << 25) >= 2 << 25)
6408 {
6409 Address brlt_addr
6410 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6411 gold_assert(brlt_addr != invalid_address);
6412 brlt_addr += this->targ_->brlt_section()->address();
6413 Address got_addr = this->targ_->toc_pointer();
6414 Address brltoff = brlt_addr - got_addr;
6415 if (ha(brltoff) == 0)
6416 {
6417 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6418 p += 4;
6419 }
6420 else
6421 {
6422 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6423 p += 4;
6424 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6425 p += 4;
6426 }
6427 }
6428 if (delta + (1 << 25) < 2 << 25)
6429 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6430 else
6431 {
6432 write_insn<big_endian>(p, mtctr_12);
6433 p += 4;
6434 write_insn<big_endian>(p, bctr);
6435 }
6436 }
6437 }
6438 else
6439 {
6440 if (!bs->second.notoc_)
6441 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6442 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6443 {
6444 unsigned char* startp = p;
6445 p = build_power10_offset<big_endian>(p, delta,
6446 loc & 4, false);
6447 delta -= p - startp;
6448 }
6449 if (delta + (1 << 25) < 2 << 25)
6450 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6451 else
6452 {
6453 write_insn<big_endian>(p, mtctr_12);
6454 p += 4;
6455 write_insn<big_endian>(p, bctr);
6456 }
6457 }
6458 }
6459 }
6460 else if (size == 64)
6461 {
6462
6463 if (!this->plt_call_stubs_.empty()
6464 && this->targ_->abiversion() >= 2)
6465 {
6466 // Write out plt call stubs for ELFv2.
6467 typename Plt_stub_entries::const_iterator cs;
6468 for (cs = this->plt_call_stubs_.begin();
6469 cs != this->plt_call_stubs_.end();
6470 ++cs)
6471 {
6472 const Output_data_plt_powerpc<size, big_endian>* plt;
6473 Address pltoff = this->plt_off(cs, &plt);
6474 Address plt_addr = pltoff + plt->address();
6475
6476 p = oview + cs->second.off_;
6477 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6478 {
6479 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6480 this->build_tls_opt_head(&p, save_lr);
6481 }
6482 if (cs->second.r2save_)
6483 {
6484 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6485 p += 4;
6486 }
6487 if (cs->second.notoc_)
6488 {
6489 Address from = this->stub_address() + (p - oview) + 8;
6490 Address off = plt_addr - from;
6491 p = build_notoc_offset<big_endian>(p, off, true);
6492 }
6493 else
6494 {
6495 Address got_addr = this->targ_->toc_pointer();
6496 Address off = plt_addr - got_addr;
6497
6498 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6499 this->plt_error(cs->first);
6500
6501 if (ha(off) != 0)
6502 {
6503 write_insn<big_endian>(p, addis_12_2 + ha(off));
6504 p += 4;
6505 write_insn<big_endian>(p, ld_12_12 + l(off));
6506 p += 4;
6507 }
6508 else
6509 {
6510 write_insn<big_endian>(p, ld_12_2 + l(off));
6511 p += 4;
6512 }
6513 }
6514 write_insn<big_endian>(p, mtctr_12);
6515 p += 4;
6516 if (cs->second.r2save_
6517 && !cs->second.localentry0_
6518 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6519 this->build_tls_opt_tail(p);
6520 else
6521 write_insn<big_endian>(p, bctr);
6522 }
6523 }
6524 else if (!this->plt_call_stubs_.empty())
6525 {
6526 // Write out plt call stubs for ELFv1.
6527 typename Plt_stub_entries::const_iterator cs;
6528 for (cs = this->plt_call_stubs_.begin();
6529 cs != this->plt_call_stubs_.end();
6530 ++cs)
6531 {
6532 const Output_data_plt_powerpc<size, big_endian>* plt;
6533 Address pltoff = this->plt_off(cs, &plt);
6534 Address plt_addr = pltoff + plt->address();
6535 Address got_addr = this->targ_->toc_pointer();
6536 Address off = plt_addr - got_addr;
6537
6538 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6539 || cs->second.notoc_)
6540 this->plt_error(cs->first);
6541
6542 bool static_chain = parameters->options().plt_static_chain();
6543 bool thread_safe = this->targ_->plt_thread_safe();
6544 bool use_fake_dep = false;
6545 Address cmp_branch_off = 0;
6546 if (thread_safe)
6547 {
6548 unsigned int pltindex
6549 = ((pltoff - this->targ_->first_plt_entry_offset())
6550 / this->targ_->plt_entry_size());
6551 Address glinkoff
6552 = (this->targ_->glink_section()->pltresolve_size()
6553 + pltindex * 8);
6554 if (pltindex > 32768)
6555 glinkoff += (pltindex - 32768) * 4;
6556 Address to
6557 = this->targ_->glink_section()->address() + glinkoff;
6558 Address from
6559 = (this->stub_address() + cs->second.off_ + 20
6560 + 4 * cs->second.r2save_
6561 + 4 * (ha(off) != 0)
6562 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6563 + 4 * static_chain);
6564 cmp_branch_off = to - from;
6565 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6566 }
6567
6568 p = oview + cs->second.off_;
6569 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6570 {
6571 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6572 this->build_tls_opt_head(&p, save_lr);
6573 use_fake_dep = thread_safe;
6574 }
6575 if (cs->second.r2save_)
6576 {
6577 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6578 p += 4;
6579 }
6580 if (ha(off) != 0)
6581 {
6582 write_insn<big_endian>(p, addis_11_2 + ha(off));
6583 p += 4;
6584 write_insn<big_endian>(p, ld_12_11 + l(off));
6585 p += 4;
6586 if (ha(off + 8 + 8 * static_chain) != ha(off))
6587 {
6588 write_insn<big_endian>(p, addi_11_11 + l(off));
6589 p += 4;
6590 off = 0;
6591 }
6592 write_insn<big_endian>(p, mtctr_12);
6593 p += 4;
6594 if (use_fake_dep)
6595 {
6596 write_insn<big_endian>(p, xor_2_12_12);
6597 p += 4;
6598 write_insn<big_endian>(p, add_11_11_2);
6599 p += 4;
6600 }
6601 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6602 p += 4;
6603 if (static_chain)
6604 {
6605 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6606 p += 4;
6607 }
6608 }
6609 else
6610 {
6611 write_insn<big_endian>(p, ld_12_2 + l(off));
6612 p += 4;
6613 if (ha(off + 8 + 8 * static_chain) != ha(off))
6614 {
6615 write_insn<big_endian>(p, addi_2_2 + l(off));
6616 p += 4;
6617 off = 0;
6618 }
6619 write_insn<big_endian>(p, mtctr_12);
6620 p += 4;
6621 if (use_fake_dep)
6622 {
6623 write_insn<big_endian>(p, xor_11_12_12);
6624 p += 4;
6625 write_insn<big_endian>(p, add_2_2_11);
6626 p += 4;
6627 }
6628 if (static_chain)
6629 {
6630 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6631 p += 4;
6632 }
6633 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6634 p += 4;
6635 }
6636 if (cs->second.r2save_
6637 && !cs->second.localentry0_
6638 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6639 this->build_tls_opt_tail(p);
6640 else if (thread_safe && !use_fake_dep)
6641 {
6642 write_insn<big_endian>(p, cmpldi_2_0);
6643 p += 4;
6644 write_insn<big_endian>(p, bnectr_p4);
6645 p += 4;
6646 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6647 }
6648 else
6649 write_insn<big_endian>(p, bctr);
6650 }
6651 }
6652
6653 // Write out long branch stubs.
6654 typename Branch_stub_entries::const_iterator bs;
6655 for (bs = this->long_branch_stubs_.begin();
6656 bs != this->long_branch_stubs_.end();
6657 ++bs)
6658 {
6659 if (bs->second.save_res_)
6660 continue;
6661 Address off = this->plt_size_ + bs->second.off_;
6662 p = oview + off;
6663 Address loc = this->stub_address() + off;
6664 Address delta = bs->first.dest_ - loc;
6665 if (!bs->second.notoc_)
6666 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6667 if (bs->second.notoc_)
6668 {
6669 unsigned char* startp = p;
6670 p = build_notoc_offset<big_endian>(p, off, false);
6671 delta -= p - startp;
6672 }
6673 else if (delta + (1 << 25) >= 2 << 25)
6674 {
6675 Address brlt_addr
6676 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6677 gold_assert(brlt_addr != invalid_address);
6678 brlt_addr += this->targ_->brlt_section()->address();
6679 Address got_addr = this->targ_->toc_pointer();
6680 Address brltoff = brlt_addr - got_addr;
6681 if (ha(brltoff) == 0)
6682 {
6683 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6684 p += 4;
6685 }
6686 else
6687 {
6688 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6689 p += 4;
6690 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6691 p += 4;
6692 }
6693 }
6694 if (delta + (1 << 25) < 2 << 25)
6695 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6696 else
6697 {
6698 write_insn<big_endian>(p, mtctr_12);
6699 p += 4;
6700 write_insn<big_endian>(p, bctr);
6701 }
6702 }
6703 }
6704 else // size == 32
6705 {
6706 if (!this->plt_call_stubs_.empty())
6707 {
6708 // The address of _GLOBAL_OFFSET_TABLE_.
6709 Address g_o_t = invalid_address;
6710
6711 // Write out plt call stubs.
6712 typename Plt_stub_entries::const_iterator cs;
6713 for (cs = this->plt_call_stubs_.begin();
6714 cs != this->plt_call_stubs_.end();
6715 ++cs)
6716 {
6717 const Output_data_plt_powerpc<size, big_endian>* plt;
6718 Address plt_addr = this->plt_off(cs, &plt);
6719 plt_addr += plt->address();
6720
6721 p = oview + cs->second.off_;
6722 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6723 this->build_tls_opt_head(&p, false);
6724 if (parameters->options().output_is_position_independent())
6725 {
6726 Address got_addr;
6727 const Powerpc_relobj<size, big_endian>* ppcobj
6728 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6729 (cs->first.object_));
6730 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6731 {
6732 unsigned int got2 = ppcobj->got2_shndx();
6733 got_addr = ppcobj->get_output_section_offset(got2);
6734 gold_assert(got_addr != invalid_address);
6735 got_addr += (ppcobj->output_section(got2)->address()
6736 + cs->first.addend_);
6737 }
6738 else
6739 {
6740 if (g_o_t == invalid_address)
6741 g_o_t = this->targ_->toc_pointer();
6742 got_addr = g_o_t;
6743 }
6744
6745 Address off = plt_addr - got_addr;
6746 if (ha(off) == 0)
6747 write_insn<big_endian>(p, lwz_11_30 + l(off));
6748 else
6749 {
6750 write_insn<big_endian>(p, addis_11_30 + ha(off));
6751 p += 4;
6752 write_insn<big_endian>(p, lwz_11_11 + l(off));
6753 }
6754 }
6755 else
6756 {
6757 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6758 p += 4;
6759 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6760 }
6761 p += 4;
6762 write_insn<big_endian>(p, mtctr_11);
6763 p += 4;
6764 write_insn<big_endian>(p, bctr);
6765 }
6766 }
6767
6768 // Write out long branch stubs.
6769 typename Branch_stub_entries::const_iterator bs;
6770 for (bs = this->long_branch_stubs_.begin();
6771 bs != this->long_branch_stubs_.end();
6772 ++bs)
6773 {
6774 if (bs->second.save_res_)
6775 continue;
6776 Address off = this->plt_size_ + bs->second.off_;
6777 p = oview + off;
6778 Address loc = this->stub_address() + off;
6779 Address delta = bs->first.dest_ - loc;
6780 if (delta + (1 << 25) < 2 << 25)
6781 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6782 else if (!parameters->options().output_is_position_independent())
6783 {
6784 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6785 p += 4;
6786 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6787 }
6788 else
6789 {
6790 delta -= 8;
6791 write_insn<big_endian>(p, mflr_0);
6792 p += 4;
6793 write_insn<big_endian>(p, bcl_20_31);
6794 p += 4;
6795 write_insn<big_endian>(p, mflr_12);
6796 p += 4;
6797 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6798 p += 4;
6799 write_insn<big_endian>(p, addi_12_12 + l(delta));
6800 p += 4;
6801 write_insn<big_endian>(p, mtlr_0);
6802 }
6803 p += 4;
6804 write_insn<big_endian>(p, mtctr_12);
6805 p += 4;
6806 write_insn<big_endian>(p, bctr);
6807 }
6808 }
6809 if (this->need_save_res_)
6810 {
6811 p = oview + this->plt_size_ + this->branch_size_;
6812 memcpy (p, this->targ_->savres_section()->contents(),
6813 this->targ_->savres_section()->data_size());
6814 }
6815 }
6816
6817 // Write out .glink.
6818
6819 template<int size, bool big_endian>
6820 void
6821 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6822 {
6823 const section_size_type off = this->offset();
6824 const section_size_type oview_size =
6825 convert_to_section_size_type(this->data_size());
6826 unsigned char* const oview = of->get_output_view(off, oview_size);
6827 unsigned char* p;
6828
6829 // The base address of the .plt section.
6830 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6831 Address plt_base = this->targ_->plt_section()->address();
6832
6833 if (size == 64)
6834 {
6835 if (this->end_branch_table_ != 0)
6836 {
6837 // Write pltresolve stub.
6838 p = oview;
6839 Address after_bcl = this->address() + 16;
6840 Address pltoff = plt_base - after_bcl;
6841
6842 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
6843
6844 if (this->targ_->abiversion() < 2)
6845 {
6846 write_insn<big_endian>(p, mflr_12), p += 4;
6847 write_insn<big_endian>(p, bcl_20_31), p += 4;
6848 write_insn<big_endian>(p, mflr_11), p += 4;
6849 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
6850 write_insn<big_endian>(p, mtlr_12), p += 4;
6851 write_insn<big_endian>(p, add_11_2_11), p += 4;
6852 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6853 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
6854 write_insn<big_endian>(p, mtctr_12), p += 4;
6855 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
6856 }
6857 else
6858 {
6859 if (this->targ_->has_localentry0())
6860 {
6861 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
6862 }
6863 write_insn<big_endian>(p, mflr_0), p += 4;
6864 write_insn<big_endian>(p, bcl_20_31), p += 4;
6865 write_insn<big_endian>(p, mflr_11), p += 4;
6866 write_insn<big_endian>(p, mtlr_0), p += 4;
6867 if (this->targ_->has_localentry0())
6868 {
6869 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
6870 }
6871 else
6872 {
6873 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
6874 }
6875 write_insn<big_endian>(p, sub_12_12_11), p += 4;
6876 write_insn<big_endian>(p, add_11_0_11), p += 4;
6877 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
6878 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
6879 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
6880 write_insn<big_endian>(p, mtctr_12), p += 4;
6881 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
6882 }
6883 write_insn<big_endian>(p, bctr), p += 4;
6884 gold_assert(p == oview + this->pltresolve_size());
6885
6886 // Write lazy link call stubs.
6887 uint32_t indx = 0;
6888 while (p < oview + this->end_branch_table_)
6889 {
6890 if (this->targ_->abiversion() < 2)
6891 {
6892 if (indx < 0x8000)
6893 {
6894 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
6895 }
6896 else
6897 {
6898 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
6899 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
6900 }
6901 }
6902 uint32_t branch_off = 8 - (p - oview);
6903 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
6904 indx++;
6905 }
6906 }
6907
6908 Address plt_base = this->targ_->plt_section()->address();
6909 Address iplt_base = invalid_address;
6910 unsigned int global_entry_off = this->global_entry_off();
6911 Address global_entry_base = this->address() + global_entry_off;
6912 typename Global_entry_stub_entries::const_iterator ge;
6913 for (ge = this->global_entry_stubs_.begin();
6914 ge != this->global_entry_stubs_.end();
6915 ++ge)
6916 {
6917 p = oview + global_entry_off + ge->second;
6918 Address plt_addr = ge->first->plt_offset();
6919 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6920 && ge->first->can_use_relative_reloc(false))
6921 {
6922 if (iplt_base == invalid_address)
6923 iplt_base = this->targ_->iplt_section()->address();
6924 plt_addr += iplt_base;
6925 }
6926 else
6927 plt_addr += plt_base;
6928 Address my_addr = global_entry_base + ge->second;
6929 Address off = plt_addr - my_addr;
6930
6931 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6932 gold_error(_("linkage table error against `%s'"),
6933 ge->first->demangled_name().c_str());
6934
6935 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
6936 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
6937 write_insn<big_endian>(p, mtctr_12), p += 4;
6938 write_insn<big_endian>(p, bctr);
6939 }
6940 }
6941 else
6942 {
6943 // The address of _GLOBAL_OFFSET_TABLE_.
6944 Address g_o_t = this->targ_->toc_pointer();
6945
6946 // Write out pltresolve branch table.
6947 p = oview;
6948 unsigned int the_end = oview_size - this->pltresolve_size();
6949 unsigned char* end_p = oview + the_end;
6950 while (p < end_p - 8 * 4)
6951 write_insn<big_endian>(p, b + end_p - p), p += 4;
6952 while (p < end_p)
6953 write_insn<big_endian>(p, nop), p += 4;
6954
6955 // Write out pltresolve call stub.
6956 end_p = oview + oview_size;
6957 if (parameters->options().output_is_position_independent())
6958 {
6959 Address res0_off = 0;
6960 Address after_bcl_off = the_end + 12;
6961 Address bcl_res0 = after_bcl_off - res0_off;
6962
6963 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6964 p += 4;
6965 write_insn<big_endian>(p, mflr_0);
6966 p += 4;
6967 write_insn<big_endian>(p, bcl_20_31);
6968 p += 4;
6969 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6970 p += 4;
6971 write_insn<big_endian>(p, mflr_12);
6972 p += 4;
6973 write_insn<big_endian>(p, mtlr_0);
6974 p += 4;
6975 write_insn<big_endian>(p, sub_11_11_12);
6976 p += 4;
6977
6978 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
6979
6980 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6981 p += 4;
6982 if (ha(got_bcl) == ha(got_bcl + 4))
6983 {
6984 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6985 p += 4;
6986 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
6987 }
6988 else
6989 {
6990 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6991 p += 4;
6992 write_insn<big_endian>(p, lwz_12_12 + 4);
6993 }
6994 p += 4;
6995 write_insn<big_endian>(p, mtctr_0);
6996 p += 4;
6997 write_insn<big_endian>(p, add_0_11_11);
6998 p += 4;
6999 write_insn<big_endian>(p, add_11_0_11);
7000 }
7001 else
7002 {
7003 Address res0 = this->address();
7004
7005 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7006 p += 4;
7007 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7008 p += 4;
7009 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7010 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7011 else
7012 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7013 p += 4;
7014 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7015 p += 4;
7016 write_insn<big_endian>(p, mtctr_0);
7017 p += 4;
7018 write_insn<big_endian>(p, add_0_11_11);
7019 p += 4;
7020 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7021 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7022 else
7023 write_insn<big_endian>(p, lwz_12_12 + 4);
7024 p += 4;
7025 write_insn<big_endian>(p, add_11_0_11);
7026 }
7027 p += 4;
7028 write_insn<big_endian>(p, bctr);
7029 p += 4;
7030 while (p < end_p)
7031 {
7032 write_insn<big_endian>(p, nop);
7033 p += 4;
7034 }
7035 }
7036
7037 of->write_output_view(off, oview_size, oview);
7038 }
7039
7040
7041 // A class to handle linker generated save/restore functions.
7042
7043 template<int size, bool big_endian>
7044 class Output_data_save_res : public Output_section_data_build
7045 {
7046 public:
7047 Output_data_save_res(Symbol_table* symtab);
7048
7049 const unsigned char*
7050 contents() const
7051 {
7052 return contents_;
7053 }
7054
7055 protected:
7056 // Write to a map file.
7057 void
7058 do_print_to_mapfile(Mapfile* mapfile) const
7059 { mapfile->print_output_data(this, _("** save/restore")); }
7060
7061 void
7062 do_write(Output_file*);
7063
7064 private:
7065 // The maximum size of save/restore contents.
7066 static const unsigned int savres_max = 218*4;
7067
7068 void
7069 savres_define(Symbol_table* symtab,
7070 const char *name,
7071 unsigned int lo, unsigned int hi,
7072 unsigned char* write_ent(unsigned char*, int),
7073 unsigned char* write_tail(unsigned char*, int));
7074
7075 unsigned char *contents_;
7076 };
7077
7078 template<bool big_endian>
7079 static unsigned char*
7080 savegpr0(unsigned char* p, int r)
7081 {
7082 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7083 write_insn<big_endian>(p, insn);
7084 return p + 4;
7085 }
7086
7087 template<bool big_endian>
7088 static unsigned char*
7089 savegpr0_tail(unsigned char* p, int r)
7090 {
7091 p = savegpr0<big_endian>(p, r);
7092 uint32_t insn = std_0_1 + 16;
7093 write_insn<big_endian>(p, insn);
7094 p = p + 4;
7095 write_insn<big_endian>(p, blr);
7096 return p + 4;
7097 }
7098
7099 template<bool big_endian>
7100 static unsigned char*
7101 restgpr0(unsigned char* p, int r)
7102 {
7103 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7104 write_insn<big_endian>(p, insn);
7105 return p + 4;
7106 }
7107
7108 template<bool big_endian>
7109 static unsigned char*
7110 restgpr0_tail(unsigned char* p, int r)
7111 {
7112 uint32_t insn = ld_0_1 + 16;
7113 write_insn<big_endian>(p, insn);
7114 p = p + 4;
7115 p = restgpr0<big_endian>(p, r);
7116 write_insn<big_endian>(p, mtlr_0);
7117 p = p + 4;
7118 if (r == 29)
7119 {
7120 p = restgpr0<big_endian>(p, 30);
7121 p = restgpr0<big_endian>(p, 31);
7122 }
7123 write_insn<big_endian>(p, blr);
7124 return p + 4;
7125 }
7126
7127 template<bool big_endian>
7128 static unsigned char*
7129 savegpr1(unsigned char* p, int r)
7130 {
7131 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7132 write_insn<big_endian>(p, insn);
7133 return p + 4;
7134 }
7135
7136 template<bool big_endian>
7137 static unsigned char*
7138 savegpr1_tail(unsigned char* p, int r)
7139 {
7140 p = savegpr1<big_endian>(p, r);
7141 write_insn<big_endian>(p, blr);
7142 return p + 4;
7143 }
7144
7145 template<bool big_endian>
7146 static unsigned char*
7147 restgpr1(unsigned char* p, int r)
7148 {
7149 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7150 write_insn<big_endian>(p, insn);
7151 return p + 4;
7152 }
7153
7154 template<bool big_endian>
7155 static unsigned char*
7156 restgpr1_tail(unsigned char* p, int r)
7157 {
7158 p = restgpr1<big_endian>(p, r);
7159 write_insn<big_endian>(p, blr);
7160 return p + 4;
7161 }
7162
7163 template<bool big_endian>
7164 static unsigned char*
7165 savefpr(unsigned char* p, int r)
7166 {
7167 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7168 write_insn<big_endian>(p, insn);
7169 return p + 4;
7170 }
7171
7172 template<bool big_endian>
7173 static unsigned char*
7174 savefpr0_tail(unsigned char* p, int r)
7175 {
7176 p = savefpr<big_endian>(p, r);
7177 write_insn<big_endian>(p, std_0_1 + 16);
7178 p = p + 4;
7179 write_insn<big_endian>(p, blr);
7180 return p + 4;
7181 }
7182
7183 template<bool big_endian>
7184 static unsigned char*
7185 restfpr(unsigned char* p, int r)
7186 {
7187 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7188 write_insn<big_endian>(p, insn);
7189 return p + 4;
7190 }
7191
7192 template<bool big_endian>
7193 static unsigned char*
7194 restfpr0_tail(unsigned char* p, int r)
7195 {
7196 write_insn<big_endian>(p, ld_0_1 + 16);
7197 p = p + 4;
7198 p = restfpr<big_endian>(p, r);
7199 write_insn<big_endian>(p, mtlr_0);
7200 p = p + 4;
7201 if (r == 29)
7202 {
7203 p = restfpr<big_endian>(p, 30);
7204 p = restfpr<big_endian>(p, 31);
7205 }
7206 write_insn<big_endian>(p, blr);
7207 return p + 4;
7208 }
7209
7210 template<bool big_endian>
7211 static unsigned char*
7212 savefpr1_tail(unsigned char* p, int r)
7213 {
7214 p = savefpr<big_endian>(p, r);
7215 write_insn<big_endian>(p, blr);
7216 return p + 4;
7217 }
7218
7219 template<bool big_endian>
7220 static unsigned char*
7221 restfpr1_tail(unsigned char* p, int r)
7222 {
7223 p = restfpr<big_endian>(p, r);
7224 write_insn<big_endian>(p, blr);
7225 return p + 4;
7226 }
7227
7228 template<bool big_endian>
7229 static unsigned char*
7230 savevr(unsigned char* p, int r)
7231 {
7232 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7233 write_insn<big_endian>(p, insn);
7234 p = p + 4;
7235 insn = stvx_0_12_0 + (r << 21);
7236 write_insn<big_endian>(p, insn);
7237 return p + 4;
7238 }
7239
7240 template<bool big_endian>
7241 static unsigned char*
7242 savevr_tail(unsigned char* p, int r)
7243 {
7244 p = savevr<big_endian>(p, r);
7245 write_insn<big_endian>(p, blr);
7246 return p + 4;
7247 }
7248
7249 template<bool big_endian>
7250 static unsigned char*
7251 restvr(unsigned char* p, int r)
7252 {
7253 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7254 write_insn<big_endian>(p, insn);
7255 p = p + 4;
7256 insn = lvx_0_12_0 + (r << 21);
7257 write_insn<big_endian>(p, insn);
7258 return p + 4;
7259 }
7260
7261 template<bool big_endian>
7262 static unsigned char*
7263 restvr_tail(unsigned char* p, int r)
7264 {
7265 p = restvr<big_endian>(p, r);
7266 write_insn<big_endian>(p, blr);
7267 return p + 4;
7268 }
7269
7270
7271 template<int size, bool big_endian>
7272 Output_data_save_res<size, big_endian>::Output_data_save_res(
7273 Symbol_table* symtab)
7274 : Output_section_data_build(4),
7275 contents_(NULL)
7276 {
7277 this->savres_define(symtab,
7278 "_savegpr0_", 14, 31,
7279 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7280 this->savres_define(symtab,
7281 "_restgpr0_", 14, 29,
7282 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7283 this->savres_define(symtab,
7284 "_restgpr0_", 30, 31,
7285 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7286 this->savres_define(symtab,
7287 "_savegpr1_", 14, 31,
7288 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7289 this->savres_define(symtab,
7290 "_restgpr1_", 14, 31,
7291 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7292 this->savres_define(symtab,
7293 "_savefpr_", 14, 31,
7294 savefpr<big_endian>, savefpr0_tail<big_endian>);
7295 this->savres_define(symtab,
7296 "_restfpr_", 14, 29,
7297 restfpr<big_endian>, restfpr0_tail<big_endian>);
7298 this->savres_define(symtab,
7299 "_restfpr_", 30, 31,
7300 restfpr<big_endian>, restfpr0_tail<big_endian>);
7301 this->savres_define(symtab,
7302 "._savef", 14, 31,
7303 savefpr<big_endian>, savefpr1_tail<big_endian>);
7304 this->savres_define(symtab,
7305 "._restf", 14, 31,
7306 restfpr<big_endian>, restfpr1_tail<big_endian>);
7307 this->savres_define(symtab,
7308 "_savevr_", 20, 31,
7309 savevr<big_endian>, savevr_tail<big_endian>);
7310 this->savres_define(symtab,
7311 "_restvr_", 20, 31,
7312 restvr<big_endian>, restvr_tail<big_endian>);
7313 }
7314
7315 template<int size, bool big_endian>
7316 void
7317 Output_data_save_res<size, big_endian>::savres_define(
7318 Symbol_table* symtab,
7319 const char *name,
7320 unsigned int lo, unsigned int hi,
7321 unsigned char* write_ent(unsigned char*, int),
7322 unsigned char* write_tail(unsigned char*, int))
7323 {
7324 size_t len = strlen(name);
7325 bool writing = false;
7326 char sym[16];
7327
7328 memcpy(sym, name, len);
7329 sym[len + 2] = 0;
7330
7331 for (unsigned int i = lo; i <= hi; i++)
7332 {
7333 sym[len + 0] = i / 10 + '0';
7334 sym[len + 1] = i % 10 + '0';
7335 Symbol* gsym = symtab->lookup(sym);
7336 bool refd = gsym != NULL && gsym->is_undefined();
7337 writing = writing || refd;
7338 if (writing)
7339 {
7340 if (this->contents_ == NULL)
7341 this->contents_ = new unsigned char[this->savres_max];
7342
7343 section_size_type value = this->current_data_size();
7344 unsigned char* p = this->contents_ + value;
7345 if (i != hi)
7346 p = write_ent(p, i);
7347 else
7348 p = write_tail(p, i);
7349 section_size_type cur_size = p - this->contents_;
7350 this->set_current_data_size(cur_size);
7351 if (refd)
7352 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7353 this, value, cur_size - value,
7354 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7355 elfcpp::STV_HIDDEN, 0, false, false);
7356 }
7357 }
7358 }
7359
7360 // Write out save/restore.
7361
7362 template<int size, bool big_endian>
7363 void
7364 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7365 {
7366 const section_size_type off = this->offset();
7367 const section_size_type oview_size =
7368 convert_to_section_size_type(this->data_size());
7369 unsigned char* const oview = of->get_output_view(off, oview_size);
7370 memcpy(oview, this->contents_, oview_size);
7371 of->write_output_view(off, oview_size, oview);
7372 }
7373
7374
7375 // Create the glink section.
7376
7377 template<int size, bool big_endian>
7378 void
7379 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7380 {
7381 if (this->glink_ == NULL)
7382 {
7383 this->glink_ = new Output_data_glink<size, big_endian>(this);
7384 this->glink_->add_eh_frame(layout);
7385 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7386 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7387 this->glink_, ORDER_TEXT, false);
7388 }
7389 }
7390
7391 // Create a PLT entry for a global symbol.
7392
7393 template<int size, bool big_endian>
7394 void
7395 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7396 Layout* layout,
7397 Symbol* gsym)
7398 {
7399 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7400 && gsym->can_use_relative_reloc(false))
7401 {
7402 if (this->iplt_ == NULL)
7403 this->make_iplt_section(symtab, layout);
7404 this->iplt_->add_ifunc_entry(gsym);
7405 }
7406 else
7407 {
7408 if (this->plt_ == NULL)
7409 this->make_plt_section(symtab, layout);
7410 this->plt_->add_entry(gsym);
7411 }
7412 }
7413
7414 // Make a PLT entry for a local symbol.
7415
7416 template<int size, bool big_endian>
7417 void
7418 Target_powerpc<size, big_endian>::make_local_plt_entry(
7419 Layout* layout,
7420 Sized_relobj_file<size, big_endian>* relobj,
7421 unsigned int r_sym)
7422 {
7423 if (this->lplt_ == NULL)
7424 this->make_lplt_section(layout);
7425 this->lplt_->add_local_entry(relobj, r_sym);
7426 }
7427
7428 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7429
7430 template<int size, bool big_endian>
7431 void
7432 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7433 Symbol_table* symtab,
7434 Layout* layout,
7435 Sized_relobj_file<size, big_endian>* relobj,
7436 unsigned int r_sym)
7437 {
7438 if (this->iplt_ == NULL)
7439 this->make_iplt_section(symtab, layout);
7440 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7441 }
7442
7443 // Return the number of entries in the PLT.
7444
7445 template<int size, bool big_endian>
7446 unsigned int
7447 Target_powerpc<size, big_endian>::plt_entry_count() const
7448 {
7449 if (this->plt_ == NULL)
7450 return 0;
7451 return this->plt_->entry_count();
7452 }
7453
7454 // Create a GOT entry for local dynamic __tls_get_addr calls.
7455
7456 template<int size, bool big_endian>
7457 unsigned int
7458 Target_powerpc<size, big_endian>::tlsld_got_offset(
7459 Symbol_table* symtab,
7460 Layout* layout,
7461 Sized_relobj_file<size, big_endian>* object)
7462 {
7463 if (this->tlsld_got_offset_ == -1U)
7464 {
7465 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7466 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7467 Output_data_got_powerpc<size, big_endian>* got
7468 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
7469 unsigned int got_offset = got->add_constant_pair(0, 0);
7470 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7471 got_offset, 0);
7472 this->tlsld_got_offset_ = got_offset;
7473 }
7474 return this->tlsld_got_offset_;
7475 }
7476
7477 // Get the Reference_flags for a particular relocation.
7478
7479 template<int size, bool big_endian>
7480 int
7481 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7482 unsigned int r_type,
7483 const Target_powerpc* target)
7484 {
7485 int ref = 0;
7486
7487 switch (r_type)
7488 {
7489 case elfcpp::R_POWERPC_NONE:
7490 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7491 case elfcpp::R_POWERPC_GNU_VTENTRY:
7492 case elfcpp::R_PPC64_TOC:
7493 // No symbol reference.
7494 break;
7495
7496 case elfcpp::R_PPC64_ADDR64:
7497 case elfcpp::R_PPC64_UADDR64:
7498 case elfcpp::R_POWERPC_ADDR32:
7499 case elfcpp::R_POWERPC_UADDR32:
7500 case elfcpp::R_POWERPC_ADDR16:
7501 case elfcpp::R_POWERPC_UADDR16:
7502 case elfcpp::R_POWERPC_ADDR16_LO:
7503 case elfcpp::R_POWERPC_ADDR16_HI:
7504 case elfcpp::R_POWERPC_ADDR16_HA:
7505 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7506 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7507 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7508 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7509 case elfcpp::R_PPC64_D34:
7510 case elfcpp::R_PPC64_D34_LO:
7511 case elfcpp::R_PPC64_D34_HI30:
7512 case elfcpp::R_PPC64_D34_HA30:
7513 case elfcpp::R_PPC64_D28:
7514 ref = Symbol::ABSOLUTE_REF;
7515 break;
7516
7517 case elfcpp::R_POWERPC_ADDR24:
7518 case elfcpp::R_POWERPC_ADDR14:
7519 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7520 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7521 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7522 break;
7523
7524 case elfcpp::R_PPC64_REL64:
7525 case elfcpp::R_POWERPC_REL32:
7526 case elfcpp::R_PPC_LOCAL24PC:
7527 case elfcpp::R_POWERPC_REL16:
7528 case elfcpp::R_POWERPC_REL16_LO:
7529 case elfcpp::R_POWERPC_REL16_HI:
7530 case elfcpp::R_POWERPC_REL16_HA:
7531 case elfcpp::R_PPC64_REL16_HIGH:
7532 case elfcpp::R_PPC64_REL16_HIGHA:
7533 case elfcpp::R_PPC64_REL16_HIGHER:
7534 case elfcpp::R_PPC64_REL16_HIGHERA:
7535 case elfcpp::R_PPC64_REL16_HIGHEST:
7536 case elfcpp::R_PPC64_REL16_HIGHESTA:
7537 case elfcpp::R_PPC64_PCREL34:
7538 case elfcpp::R_PPC64_REL16_HIGHER34:
7539 case elfcpp::R_PPC64_REL16_HIGHERA34:
7540 case elfcpp::R_PPC64_REL16_HIGHEST34:
7541 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7542 case elfcpp::R_PPC64_PCREL28:
7543 ref = Symbol::RELATIVE_REF;
7544 break;
7545
7546 case elfcpp::R_PPC64_REL24_NOTOC:
7547 if (size == 32)
7548 break;
7549 // Fall through.
7550 case elfcpp::R_POWERPC_REL24:
7551 case elfcpp::R_PPC_PLTREL24:
7552 case elfcpp::R_POWERPC_REL14:
7553 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7554 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7555 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7556 break;
7557
7558 case elfcpp::R_POWERPC_GOT16:
7559 case elfcpp::R_POWERPC_GOT16_LO:
7560 case elfcpp::R_POWERPC_GOT16_HI:
7561 case elfcpp::R_POWERPC_GOT16_HA:
7562 case elfcpp::R_PPC64_GOT16_DS:
7563 case elfcpp::R_PPC64_GOT16_LO_DS:
7564 case elfcpp::R_PPC64_GOT_PCREL34:
7565 case elfcpp::R_PPC64_TOC16:
7566 case elfcpp::R_PPC64_TOC16_LO:
7567 case elfcpp::R_PPC64_TOC16_HI:
7568 case elfcpp::R_PPC64_TOC16_HA:
7569 case elfcpp::R_PPC64_TOC16_DS:
7570 case elfcpp::R_PPC64_TOC16_LO_DS:
7571 case elfcpp::R_POWERPC_PLT16_LO:
7572 case elfcpp::R_POWERPC_PLT16_HI:
7573 case elfcpp::R_POWERPC_PLT16_HA:
7574 case elfcpp::R_PPC64_PLT16_LO_DS:
7575 case elfcpp::R_PPC64_PLT_PCREL34:
7576 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7577 ref = Symbol::RELATIVE_REF;
7578 break;
7579
7580 case elfcpp::R_POWERPC_GOT_TPREL16:
7581 case elfcpp::R_POWERPC_TLS:
7582 case elfcpp::R_PPC64_TLSGD:
7583 case elfcpp::R_PPC64_TLSLD:
7584 case elfcpp::R_PPC64_TPREL34:
7585 case elfcpp::R_PPC64_DTPREL34:
7586 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7587 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7588 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7589 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7590 ref = Symbol::TLS_REF;
7591 break;
7592
7593 case elfcpp::R_POWERPC_COPY:
7594 case elfcpp::R_POWERPC_GLOB_DAT:
7595 case elfcpp::R_POWERPC_JMP_SLOT:
7596 case elfcpp::R_POWERPC_RELATIVE:
7597 case elfcpp::R_POWERPC_DTPMOD:
7598 default:
7599 // Not expected. We will give an error later.
7600 break;
7601 }
7602
7603 if (size == 64 && target->abiversion() < 2)
7604 ref |= Symbol::FUNC_DESC_ABI;
7605 return ref;
7606 }
7607
7608 // Report an unsupported relocation against a local symbol.
7609
7610 template<int size, bool big_endian>
7611 void
7612 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7613 Sized_relobj_file<size, big_endian>* object,
7614 unsigned int r_type)
7615 {
7616 gold_error(_("%s: unsupported reloc %u against local symbol"),
7617 object->name().c_str(), r_type);
7618 }
7619
7620 // We are about to emit a dynamic relocation of type R_TYPE. If the
7621 // dynamic linker does not support it, issue an error.
7622
7623 template<int size, bool big_endian>
7624 void
7625 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7626 unsigned int r_type)
7627 {
7628 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7629
7630 // These are the relocation types supported by glibc for both 32-bit
7631 // and 64-bit powerpc.
7632 switch (r_type)
7633 {
7634 case elfcpp::R_POWERPC_NONE:
7635 case elfcpp::R_POWERPC_RELATIVE:
7636 case elfcpp::R_POWERPC_GLOB_DAT:
7637 case elfcpp::R_POWERPC_DTPMOD:
7638 case elfcpp::R_POWERPC_DTPREL:
7639 case elfcpp::R_POWERPC_TPREL:
7640 case elfcpp::R_POWERPC_JMP_SLOT:
7641 case elfcpp::R_POWERPC_COPY:
7642 case elfcpp::R_POWERPC_IRELATIVE:
7643 case elfcpp::R_POWERPC_ADDR32:
7644 case elfcpp::R_POWERPC_UADDR32:
7645 case elfcpp::R_POWERPC_ADDR24:
7646 case elfcpp::R_POWERPC_ADDR16:
7647 case elfcpp::R_POWERPC_UADDR16:
7648 case elfcpp::R_POWERPC_ADDR16_LO:
7649 case elfcpp::R_POWERPC_ADDR16_HI:
7650 case elfcpp::R_POWERPC_ADDR16_HA:
7651 case elfcpp::R_POWERPC_ADDR14:
7652 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7653 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7654 case elfcpp::R_POWERPC_REL32:
7655 case elfcpp::R_POWERPC_TPREL16:
7656 case elfcpp::R_POWERPC_TPREL16_LO:
7657 case elfcpp::R_POWERPC_TPREL16_HI:
7658 case elfcpp::R_POWERPC_TPREL16_HA:
7659 return;
7660
7661 default:
7662 break;
7663 }
7664
7665 if (size == 64)
7666 {
7667 switch (r_type)
7668 {
7669 // These are the relocation types supported only on 64-bit.
7670 case elfcpp::R_PPC64_ADDR64:
7671 case elfcpp::R_PPC64_UADDR64:
7672 case elfcpp::R_PPC64_JMP_IREL:
7673 case elfcpp::R_PPC64_ADDR16_DS:
7674 case elfcpp::R_PPC64_ADDR16_LO_DS:
7675 case elfcpp::R_PPC64_ADDR16_HIGH:
7676 case elfcpp::R_PPC64_ADDR16_HIGHA:
7677 case elfcpp::R_PPC64_ADDR16_HIGHER:
7678 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7679 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7680 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7681 case elfcpp::R_PPC64_REL64:
7682 case elfcpp::R_POWERPC_ADDR30:
7683 case elfcpp::R_PPC64_TPREL16_DS:
7684 case elfcpp::R_PPC64_TPREL16_LO_DS:
7685 case elfcpp::R_PPC64_TPREL16_HIGH:
7686 case elfcpp::R_PPC64_TPREL16_HIGHA:
7687 case elfcpp::R_PPC64_TPREL16_HIGHER:
7688 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7689 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7690 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7691 return;
7692
7693 default:
7694 break;
7695 }
7696 }
7697 else
7698 {
7699 switch (r_type)
7700 {
7701 // These are the relocation types supported only on 32-bit.
7702 // ??? glibc ld.so doesn't need to support these.
7703 case elfcpp::R_POWERPC_REL24:
7704 case elfcpp::R_POWERPC_DTPREL16:
7705 case elfcpp::R_POWERPC_DTPREL16_LO:
7706 case elfcpp::R_POWERPC_DTPREL16_HI:
7707 case elfcpp::R_POWERPC_DTPREL16_HA:
7708 return;
7709
7710 default:
7711 break;
7712 }
7713 }
7714
7715 // This prevents us from issuing more than one error per reloc
7716 // section. But we can still wind up issuing more than one
7717 // error per object file.
7718 if (this->issued_non_pic_error_)
7719 return;
7720 gold_assert(parameters->options().output_is_position_independent());
7721 object->error(_("requires unsupported dynamic reloc; "
7722 "recompile with -fPIC"));
7723 this->issued_non_pic_error_ = true;
7724 return;
7725 }
7726
7727 // Return whether we need to make a PLT entry for a relocation of the
7728 // given type against a STT_GNU_IFUNC symbol.
7729
7730 template<int size, bool big_endian>
7731 bool
7732 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7733 Target_powerpc<size, big_endian>* target,
7734 Sized_relobj_file<size, big_endian>* object,
7735 unsigned int r_type,
7736 bool report_err)
7737 {
7738 // In non-pic code any reference will resolve to the plt call stub
7739 // for the ifunc symbol.
7740 if ((size == 32 || target->abiversion() >= 2)
7741 && !parameters->options().output_is_position_independent())
7742 return true;
7743
7744 switch (r_type)
7745 {
7746 // Word size refs from data sections are OK, but don't need a PLT entry.
7747 case elfcpp::R_POWERPC_ADDR32:
7748 case elfcpp::R_POWERPC_UADDR32:
7749 if (size == 32)
7750 return false;
7751 break;
7752
7753 case elfcpp::R_PPC64_ADDR64:
7754 case elfcpp::R_PPC64_UADDR64:
7755 if (size == 64)
7756 return false;
7757 break;
7758
7759 // GOT refs are good, but also don't need a PLT entry.
7760 case elfcpp::R_POWERPC_GOT16:
7761 case elfcpp::R_POWERPC_GOT16_LO:
7762 case elfcpp::R_POWERPC_GOT16_HI:
7763 case elfcpp::R_POWERPC_GOT16_HA:
7764 case elfcpp::R_PPC64_GOT16_DS:
7765 case elfcpp::R_PPC64_GOT16_LO_DS:
7766 case elfcpp::R_PPC64_GOT_PCREL34:
7767 return false;
7768
7769 // PLT relocs are OK and need a PLT entry.
7770 case elfcpp::R_POWERPC_PLT16_LO:
7771 case elfcpp::R_POWERPC_PLT16_HI:
7772 case elfcpp::R_POWERPC_PLT16_HA:
7773 case elfcpp::R_PPC64_PLT16_LO_DS:
7774 case elfcpp::R_POWERPC_PLTSEQ:
7775 case elfcpp::R_POWERPC_PLTCALL:
7776 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7777 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7778 case elfcpp::R_PPC64_PLT_PCREL34:
7779 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7780 return true;
7781 break;
7782
7783 // Function calls are good, and these do need a PLT entry.
7784 case elfcpp::R_PPC64_REL24_NOTOC:
7785 if (size == 32)
7786 break;
7787 // Fall through.
7788 case elfcpp::R_POWERPC_ADDR24:
7789 case elfcpp::R_POWERPC_ADDR14:
7790 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7791 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7792 case elfcpp::R_POWERPC_REL24:
7793 case elfcpp::R_PPC_PLTREL24:
7794 case elfcpp::R_POWERPC_REL14:
7795 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7796 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7797 return true;
7798
7799 default:
7800 break;
7801 }
7802
7803 // Anything else is a problem.
7804 // If we are building a static executable, the libc startup function
7805 // responsible for applying indirect function relocations is going
7806 // to complain about the reloc type.
7807 // If we are building a dynamic executable, we will have a text
7808 // relocation. The dynamic loader will set the text segment
7809 // writable and non-executable to apply text relocations. So we'll
7810 // segfault when trying to run the indirection function to resolve
7811 // the reloc.
7812 if (report_err)
7813 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7814 object->name().c_str(), r_type);
7815 return false;
7816 }
7817
7818 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7819 // reloc.
7820
7821 static bool
7822 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7823 {
7824 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7825 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7826 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7827 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7828 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7829 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7830 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7831 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7832 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7833 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7834 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7835 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7836 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7837 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7838 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7839 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7840 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7841 /* Exclude lfqu by testing reloc. If relocs are ever
7842 defined for the reduced D field in psq_lu then those
7843 will need testing too. */
7844 && r_type != elfcpp::R_PPC64_TOC16_LO
7845 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7846 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7847 && (insn & 1) == 0)
7848 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7849 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7850 /* Exclude stfqu. psq_stu as above for psq_lu. */
7851 && r_type != elfcpp::R_PPC64_TOC16_LO
7852 && r_type != elfcpp::R_POWERPC_GOT16_LO)
7853 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7854 && (insn & 1) == 0));
7855 }
7856
7857 // Scan a relocation for a local symbol.
7858
7859 template<int size, bool big_endian>
7860 inline void
7861 Target_powerpc<size, big_endian>::Scan::local(
7862 Symbol_table* symtab,
7863 Layout* layout,
7864 Target_powerpc<size, big_endian>* target,
7865 Sized_relobj_file<size, big_endian>* object,
7866 unsigned int data_shndx,
7867 Output_section* output_section,
7868 const elfcpp::Rela<size, big_endian>& reloc,
7869 unsigned int r_type,
7870 const elfcpp::Sym<size, big_endian>& lsym,
7871 bool is_discarded)
7872 {
7873 Powerpc_relobj<size, big_endian>* ppc_object
7874 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7875
7876 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
7877
7878 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7879 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7880 {
7881 this->expect_tls_get_addr_call();
7882 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7883 if (tls_type != tls::TLSOPT_NONE)
7884 this->skip_next_tls_get_addr_call();
7885 }
7886 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7887 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7888 {
7889 this->expect_tls_get_addr_call();
7890 tls::Tls_optimization tls_type = target->optimize_tls_ld();
7891 if (tls_type != tls::TLSOPT_NONE)
7892 this->skip_next_tls_get_addr_call();
7893 }
7894
7895 if (is_discarded)
7896 {
7897 if (size == 64
7898 && data_shndx == ppc_object->opd_shndx()
7899 && r_type == elfcpp::R_PPC64_ADDR64)
7900 ppc_object->set_opd_discard(reloc.get_r_offset());
7901 return;
7902 }
7903
7904 // A local STT_GNU_IFUNC symbol may require a PLT entry.
7905 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7906 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7907 {
7908 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7909 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7910 r_type, r_sym, reloc.get_r_addend());
7911 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7912 }
7913
7914 switch (r_type)
7915 {
7916 case elfcpp::R_POWERPC_NONE:
7917 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7918 case elfcpp::R_POWERPC_GNU_VTENTRY:
7919 case elfcpp::R_POWERPC_TLS:
7920 case elfcpp::R_PPC64_ENTRY:
7921 case elfcpp::R_POWERPC_PLTSEQ:
7922 case elfcpp::R_POWERPC_PLTCALL:
7923 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7924 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7925 case elfcpp::R_PPC64_PCREL_OPT:
7926 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7927 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7928 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7929 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7930 case elfcpp::R_PPC64_REL16_HIGHER34:
7931 case elfcpp::R_PPC64_REL16_HIGHERA34:
7932 case elfcpp::R_PPC64_REL16_HIGHEST34:
7933 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7934 case elfcpp::R_PPC64_D34:
7935 case elfcpp::R_PPC64_D34_LO:
7936 case elfcpp::R_PPC64_D34_HI30:
7937 case elfcpp::R_PPC64_D34_HA30:
7938 case elfcpp::R_PPC64_D28:
7939 case elfcpp::R_PPC64_PCREL34:
7940 case elfcpp::R_PPC64_PCREL28:
7941 case elfcpp::R_PPC64_TPREL34:
7942 case elfcpp::R_PPC64_DTPREL34:
7943 break;
7944
7945 case elfcpp::R_PPC64_TOC:
7946 {
7947 Output_data_got_powerpc<size, big_endian>* got
7948 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
7949 if (parameters->options().output_is_position_independent())
7950 {
7951 Address off = reloc.get_r_offset();
7952 if (size == 64
7953 && target->abiversion() < 2
7954 && data_shndx == ppc_object->opd_shndx()
7955 && ppc_object->get_opd_discard(off - 8))
7956 break;
7957
7958 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7959 Address got_off = got->g_o_t();
7960 rela_dyn->add_output_section_relative(got->output_section(),
7961 elfcpp::R_POWERPC_RELATIVE,
7962 output_section,
7963 object, data_shndx, off,
7964 got_off);
7965 }
7966 }
7967 break;
7968
7969 case elfcpp::R_PPC64_ADDR64:
7970 case elfcpp::R_PPC64_UADDR64:
7971 case elfcpp::R_POWERPC_ADDR32:
7972 case elfcpp::R_POWERPC_UADDR32:
7973 case elfcpp::R_POWERPC_ADDR24:
7974 case elfcpp::R_POWERPC_ADDR16:
7975 case elfcpp::R_POWERPC_ADDR16_LO:
7976 case elfcpp::R_POWERPC_ADDR16_HI:
7977 case elfcpp::R_POWERPC_ADDR16_HA:
7978 case elfcpp::R_POWERPC_UADDR16:
7979 case elfcpp::R_PPC64_ADDR16_HIGH:
7980 case elfcpp::R_PPC64_ADDR16_HIGHA:
7981 case elfcpp::R_PPC64_ADDR16_HIGHER:
7982 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7983 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7984 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7985 case elfcpp::R_PPC64_ADDR16_DS:
7986 case elfcpp::R_PPC64_ADDR16_LO_DS:
7987 case elfcpp::R_POWERPC_ADDR14:
7988 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7989 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7990 // If building a shared library (or a position-independent
7991 // executable), we need to create a dynamic relocation for
7992 // this location.
7993 if (parameters->options().output_is_position_independent()
7994 || (size == 64 && is_ifunc && target->abiversion() < 2))
7995 {
7996 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7997 is_ifunc);
7998 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7999 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8000 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8001 {
8002 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8003 : elfcpp::R_POWERPC_RELATIVE);
8004 rela_dyn->add_local_relative(object, r_sym, dynrel,
8005 output_section, data_shndx,
8006 reloc.get_r_offset(),
8007 reloc.get_r_addend(), false);
8008 }
8009 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8010 {
8011 check_non_pic(object, r_type);
8012 rela_dyn->add_local(object, r_sym, r_type, output_section,
8013 data_shndx, reloc.get_r_offset(),
8014 reloc.get_r_addend());
8015 }
8016 else
8017 {
8018 gold_assert(lsym.get_st_value() == 0);
8019 unsigned int shndx = lsym.get_st_shndx();
8020 bool is_ordinary;
8021 shndx = object->adjust_sym_shndx(r_sym, shndx,
8022 &is_ordinary);
8023 if (!is_ordinary)
8024 object->error(_("section symbol %u has bad shndx %u"),
8025 r_sym, shndx);
8026 else
8027 rela_dyn->add_local_section(object, shndx, r_type,
8028 output_section, data_shndx,
8029 reloc.get_r_offset());
8030 }
8031 }
8032 break;
8033
8034 case elfcpp::R_PPC64_PLT_PCREL34:
8035 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8036 case elfcpp::R_POWERPC_PLT16_LO:
8037 case elfcpp::R_POWERPC_PLT16_HI:
8038 case elfcpp::R_POWERPC_PLT16_HA:
8039 case elfcpp::R_PPC64_PLT16_LO_DS:
8040 if (!is_ifunc)
8041 {
8042 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8043 target->make_local_plt_entry(layout, object, r_sym);
8044 }
8045 break;
8046
8047 case elfcpp::R_PPC64_REL24_NOTOC:
8048 if (size == 32)
8049 break;
8050 // Fall through.
8051 case elfcpp::R_POWERPC_REL24:
8052 case elfcpp::R_PPC_PLTREL24:
8053 case elfcpp::R_PPC_LOCAL24PC:
8054 case elfcpp::R_POWERPC_REL14:
8055 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8056 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8057 if (!is_ifunc)
8058 {
8059 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8060 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8061 r_type, r_sym, reloc.get_r_addend());
8062 }
8063 break;
8064
8065 case elfcpp::R_PPC64_TOCSAVE:
8066 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8067 // caller has already saved r2 and thus a plt call stub need not
8068 // save r2.
8069 if (size == 64
8070 && target->mark_pltcall(ppc_object, data_shndx,
8071 reloc.get_r_offset() - 4, symtab))
8072 {
8073 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8074 unsigned int shndx = lsym.get_st_shndx();
8075 bool is_ordinary;
8076 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8077 if (!is_ordinary)
8078 object->error(_("tocsave symbol %u has bad shndx %u"),
8079 r_sym, shndx);
8080 else
8081 target->add_tocsave(ppc_object, shndx,
8082 lsym.get_st_value() + reloc.get_r_addend());
8083 }
8084 break;
8085
8086 case elfcpp::R_PPC64_REL64:
8087 case elfcpp::R_POWERPC_REL32:
8088 case elfcpp::R_POWERPC_REL16:
8089 case elfcpp::R_POWERPC_REL16_LO:
8090 case elfcpp::R_POWERPC_REL16_HI:
8091 case elfcpp::R_POWERPC_REL16_HA:
8092 case elfcpp::R_POWERPC_REL16DX_HA:
8093 case elfcpp::R_PPC64_REL16_HIGH:
8094 case elfcpp::R_PPC64_REL16_HIGHA:
8095 case elfcpp::R_PPC64_REL16_HIGHER:
8096 case elfcpp::R_PPC64_REL16_HIGHERA:
8097 case elfcpp::R_PPC64_REL16_HIGHEST:
8098 case elfcpp::R_PPC64_REL16_HIGHESTA:
8099 case elfcpp::R_POWERPC_SECTOFF:
8100 case elfcpp::R_POWERPC_SECTOFF_LO:
8101 case elfcpp::R_POWERPC_SECTOFF_HI:
8102 case elfcpp::R_POWERPC_SECTOFF_HA:
8103 case elfcpp::R_PPC64_SECTOFF_DS:
8104 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8105 case elfcpp::R_POWERPC_TPREL16:
8106 case elfcpp::R_POWERPC_TPREL16_LO:
8107 case elfcpp::R_POWERPC_TPREL16_HI:
8108 case elfcpp::R_POWERPC_TPREL16_HA:
8109 case elfcpp::R_PPC64_TPREL16_DS:
8110 case elfcpp::R_PPC64_TPREL16_LO_DS:
8111 case elfcpp::R_PPC64_TPREL16_HIGH:
8112 case elfcpp::R_PPC64_TPREL16_HIGHA:
8113 case elfcpp::R_PPC64_TPREL16_HIGHER:
8114 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8115 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8116 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8117 case elfcpp::R_POWERPC_DTPREL16:
8118 case elfcpp::R_POWERPC_DTPREL16_LO:
8119 case elfcpp::R_POWERPC_DTPREL16_HI:
8120 case elfcpp::R_POWERPC_DTPREL16_HA:
8121 case elfcpp::R_PPC64_DTPREL16_DS:
8122 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8123 case elfcpp::R_PPC64_DTPREL16_HIGH:
8124 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8125 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8126 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8127 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8128 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8129 case elfcpp::R_PPC64_TLSGD:
8130 case elfcpp::R_PPC64_TLSLD:
8131 case elfcpp::R_PPC64_ADDR64_LOCAL:
8132 break;
8133
8134 case elfcpp::R_PPC64_GOT_PCREL34:
8135 case elfcpp::R_POWERPC_GOT16:
8136 case elfcpp::R_POWERPC_GOT16_LO:
8137 case elfcpp::R_POWERPC_GOT16_HI:
8138 case elfcpp::R_POWERPC_GOT16_HA:
8139 case elfcpp::R_PPC64_GOT16_DS:
8140 case elfcpp::R_PPC64_GOT16_LO_DS:
8141 {
8142 // The symbol requires a GOT entry.
8143 Got_type got_type = ((size == 32
8144 || r_type == elfcpp::R_POWERPC_GOT16
8145 || r_type == elfcpp::R_PPC64_GOT16_DS)
8146 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8147 Output_data_got_powerpc<size, big_endian>* got
8148 = target->got_section(symtab, layout, got_type);
8149 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8150 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8151
8152 if (!parameters->options().output_is_position_independent())
8153 {
8154 if (is_ifunc
8155 && (size == 32 || target->abiversion() >= 2))
8156 got->add_local_plt(object, r_sym, got_type, addend);
8157 else
8158 got->add_local(object, r_sym, got_type, addend);
8159 }
8160 else if (!object->local_has_got_offset(r_sym, got_type, addend))
8161 {
8162 // If we are generating a shared object or a pie, this
8163 // symbol's GOT entry will be set by a dynamic relocation.
8164 unsigned int off;
8165 off = got->add_constant(0);
8166 object->set_local_got_offset(r_sym, got_type, off, addend);
8167
8168 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8169 is_ifunc);
8170 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8171 : elfcpp::R_POWERPC_RELATIVE);
8172 rela_dyn->add_local_relative(object, r_sym, dynrel,
8173 got, off, addend, false);
8174 }
8175 }
8176 break;
8177
8178 case elfcpp::R_PPC64_TOC16:
8179 case elfcpp::R_PPC64_TOC16_LO:
8180 case elfcpp::R_PPC64_TOC16_HI:
8181 case elfcpp::R_PPC64_TOC16_HA:
8182 case elfcpp::R_PPC64_TOC16_DS:
8183 case elfcpp::R_PPC64_TOC16_LO_DS:
8184 // We need a GOT section.
8185 target->got_section(symtab, layout, GOT_TYPE_SMALL);
8186 break;
8187
8188 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8189 case elfcpp::R_POWERPC_GOT_TLSGD16:
8190 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8191 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8192 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8193 {
8194 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8195 if (tls_type == tls::TLSOPT_NONE)
8196 {
8197 Got_type got_type = ((size == 32
8198 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8199 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8200 Output_data_got_powerpc<size, big_endian>* got
8201 = target->got_section(symtab, layout, got_type);
8202 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8203 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8204 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8205 got->add_local_tls_pair(object, r_sym, got_type,
8206 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8207 addend);
8208 }
8209 else if (tls_type == tls::TLSOPT_TO_LE)
8210 {
8211 // no GOT relocs needed for Local Exec.
8212 }
8213 else
8214 gold_unreachable();
8215 }
8216 break;
8217
8218 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8219 case elfcpp::R_POWERPC_GOT_TLSLD16:
8220 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8221 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8222 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8223 {
8224 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8225 if (tls_type == tls::TLSOPT_NONE)
8226 target->tlsld_got_offset(symtab, layout, object);
8227 else if (tls_type == tls::TLSOPT_TO_LE)
8228 {
8229 // no GOT relocs needed for Local Exec.
8230 if (parameters->options().emit_relocs())
8231 {
8232 Output_section* os = layout->tls_segment()->first_section();
8233 gold_assert(os != NULL);
8234 os->set_needs_symtab_index();
8235 }
8236 }
8237 else
8238 gold_unreachable();
8239 }
8240 break;
8241
8242 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8243 case elfcpp::R_POWERPC_GOT_DTPREL16:
8244 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8245 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8246 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8247 {
8248 Got_type got_type = ((size == 32
8249 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8250 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
8251 Output_data_got_powerpc<size, big_endian>* got
8252 = target->got_section(symtab, layout, got_type);
8253 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8254 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8255 got->add_local_tls(object, r_sym, got_type, addend);
8256 }
8257 break;
8258
8259 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8260 case elfcpp::R_POWERPC_GOT_TPREL16:
8261 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8262 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8263 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8264 {
8265 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8266 if (tls_type == tls::TLSOPT_NONE)
8267 {
8268 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8269 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8270 Got_type got_type = ((size == 32
8271 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8272 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8273 if (!object->local_has_got_offset(r_sym, got_type, addend))
8274 {
8275 Output_data_got_powerpc<size, big_endian>* got
8276 = target->got_section(symtab, layout, got_type);
8277 unsigned int off = got->add_constant(0);
8278 object->set_local_got_offset(r_sym, got_type, off, addend);
8279
8280 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8281 rela_dyn->add_symbolless_local_addend(object, r_sym,
8282 elfcpp::R_POWERPC_TPREL,
8283 got, off, addend);
8284 }
8285 }
8286 else if (tls_type == tls::TLSOPT_TO_LE)
8287 {
8288 // no GOT relocs needed for Local Exec.
8289 }
8290 else
8291 gold_unreachable();
8292 }
8293 break;
8294
8295 default:
8296 unsupported_reloc_local(object, r_type);
8297 break;
8298 }
8299
8300 if (size == 64
8301 && parameters->options().toc_optimize())
8302 {
8303 if (data_shndx == ppc_object->toc_shndx())
8304 {
8305 bool ok = true;
8306 if (r_type != elfcpp::R_PPC64_ADDR64
8307 || (is_ifunc && target->abiversion() < 2))
8308 ok = false;
8309 else if (parameters->options().output_is_position_independent())
8310 {
8311 if (is_ifunc)
8312 ok = false;
8313 else
8314 {
8315 unsigned int shndx = lsym.get_st_shndx();
8316 if (shndx >= elfcpp::SHN_LORESERVE
8317 && shndx != elfcpp::SHN_XINDEX)
8318 ok = false;
8319 }
8320 }
8321 if (!ok)
8322 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8323 }
8324
8325 enum {no_check, check_lo, check_ha} insn_check;
8326 switch (r_type)
8327 {
8328 default:
8329 insn_check = no_check;
8330 break;
8331
8332 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8333 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8334 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8335 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8336 case elfcpp::R_POWERPC_GOT16_HA:
8337 case elfcpp::R_PPC64_TOC16_HA:
8338 insn_check = check_ha;
8339 break;
8340
8341 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8342 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8343 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8344 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8345 case elfcpp::R_POWERPC_GOT16_LO:
8346 case elfcpp::R_PPC64_GOT16_LO_DS:
8347 case elfcpp::R_PPC64_TOC16_LO:
8348 case elfcpp::R_PPC64_TOC16_LO_DS:
8349 insn_check = check_lo;
8350 break;
8351 }
8352
8353 section_size_type slen;
8354 const unsigned char* view = NULL;
8355 if (insn_check != no_check)
8356 {
8357 view = ppc_object->section_contents(data_shndx, &slen, false);
8358 section_size_type off =
8359 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8360 if (off < slen)
8361 {
8362 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8363 if (insn_check == check_lo
8364 ? !ok_lo_toc_insn(insn, r_type)
8365 : ((insn & ((0x3f << 26) | 0x1f << 16))
8366 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8367 {
8368 ppc_object->set_no_toc_opt();
8369 gold_warning(_("%s: toc optimization is not supported "
8370 "for %#08x instruction"),
8371 ppc_object->name().c_str(), insn);
8372 }
8373 }
8374 }
8375
8376 switch (r_type)
8377 {
8378 default:
8379 break;
8380 case elfcpp::R_PPC64_TOC16:
8381 case elfcpp::R_PPC64_TOC16_LO:
8382 case elfcpp::R_PPC64_TOC16_HI:
8383 case elfcpp::R_PPC64_TOC16_HA:
8384 case elfcpp::R_PPC64_TOC16_DS:
8385 case elfcpp::R_PPC64_TOC16_LO_DS:
8386 unsigned int shndx = lsym.get_st_shndx();
8387 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8388 bool is_ordinary;
8389 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8390 if (is_ordinary && shndx == ppc_object->toc_shndx())
8391 {
8392 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8393 if (dst_off < ppc_object->section_size(shndx))
8394 {
8395 bool ok = false;
8396 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8397 ok = true;
8398 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8399 {
8400 // Need to check that the insn is a ld
8401 if (!view)
8402 view = ppc_object->section_contents(data_shndx,
8403 &slen,
8404 false);
8405 section_size_type off =
8406 (convert_to_section_size_type(reloc.get_r_offset())
8407 + (big_endian ? -2 : 3));
8408 if (off < slen
8409 && (view[off] & (0x3f << 2)) == 58u << 2)
8410 ok = true;
8411 }
8412 if (!ok)
8413 ppc_object->set_no_toc_opt(dst_off);
8414 }
8415 }
8416 break;
8417 }
8418 }
8419
8420 if (size == 32)
8421 {
8422 switch (r_type)
8423 {
8424 case elfcpp::R_POWERPC_REL32:
8425 if (ppc_object->got2_shndx() != 0
8426 && parameters->options().output_is_position_independent())
8427 {
8428 unsigned int shndx = lsym.get_st_shndx();
8429 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8430 bool is_ordinary;
8431 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8432 if (is_ordinary && shndx == ppc_object->got2_shndx()
8433 && (ppc_object->section_flags(data_shndx)
8434 & elfcpp::SHF_EXECINSTR) != 0)
8435 gold_error(_("%s: unsupported -mbss-plt code"),
8436 ppc_object->name().c_str());
8437 }
8438 break;
8439 default:
8440 break;
8441 }
8442 }
8443
8444 switch (r_type)
8445 {
8446 case elfcpp::R_POWERPC_GOT_TLSLD16:
8447 case elfcpp::R_POWERPC_GOT_TLSGD16:
8448 case elfcpp::R_POWERPC_GOT_TPREL16:
8449 case elfcpp::R_POWERPC_GOT_DTPREL16:
8450 case elfcpp::R_POWERPC_GOT16:
8451 case elfcpp::R_PPC64_GOT16_DS:
8452 case elfcpp::R_PPC64_TOC16:
8453 case elfcpp::R_PPC64_TOC16_DS:
8454 ppc_object->set_has_small_toc_reloc();
8455 break;
8456 default:
8457 break;
8458 }
8459
8460 switch (r_type)
8461 {
8462 case elfcpp::R_PPC64_TPREL16_DS:
8463 case elfcpp::R_PPC64_TPREL16_LO_DS:
8464 case elfcpp::R_PPC64_TPREL16_HIGH:
8465 case elfcpp::R_PPC64_TPREL16_HIGHA:
8466 case elfcpp::R_PPC64_TPREL16_HIGHER:
8467 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8468 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8469 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8470 case elfcpp::R_PPC64_TPREL34:
8471 if (size != 64)
8472 break;
8473 // Fall through.
8474 case elfcpp::R_POWERPC_TPREL16:
8475 case elfcpp::R_POWERPC_TPREL16_LO:
8476 case elfcpp::R_POWERPC_TPREL16_HI:
8477 case elfcpp::R_POWERPC_TPREL16_HA:
8478 layout->set_has_static_tls();
8479 break;
8480 default:
8481 break;
8482 }
8483
8484 switch (r_type)
8485 {
8486 case elfcpp::R_POWERPC_TPREL16_HA:
8487 if (target->tprel_opt())
8488 {
8489 section_size_type slen;
8490 const unsigned char* view = NULL;
8491 view = ppc_object->section_contents(data_shndx, &slen, false);
8492 section_size_type off
8493 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8494 if (off < slen)
8495 {
8496 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8497 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8498 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8499 target->set_no_tprel_opt();
8500 }
8501 }
8502 break;
8503
8504 case elfcpp::R_PPC64_TPREL16_HIGH:
8505 case elfcpp::R_PPC64_TPREL16_HIGHA:
8506 case elfcpp::R_PPC64_TPREL16_HIGHER:
8507 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8508 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8509 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8510 if (size != 64)
8511 break;
8512 // Fall through.
8513 case elfcpp::R_POWERPC_TPREL16_HI:
8514 target->set_no_tprel_opt();
8515 break;
8516 default:
8517 break;
8518 }
8519
8520 switch (r_type)
8521 {
8522 case elfcpp::R_PPC64_D34:
8523 case elfcpp::R_PPC64_D34_LO:
8524 case elfcpp::R_PPC64_D34_HI30:
8525 case elfcpp::R_PPC64_D34_HA30:
8526 case elfcpp::R_PPC64_D28:
8527 case elfcpp::R_PPC64_PCREL34:
8528 case elfcpp::R_PPC64_PCREL28:
8529 case elfcpp::R_PPC64_TPREL34:
8530 case elfcpp::R_PPC64_DTPREL34:
8531 case elfcpp::R_PPC64_PLT_PCREL34:
8532 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8533 case elfcpp::R_PPC64_GOT_PCREL34:
8534 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8535 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8536 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8537 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8538 target->set_power10_relocs();
8539 break;
8540 default:
8541 break;
8542 }
8543 }
8544
8545 // Report an unsupported relocation against a global symbol.
8546
8547 template<int size, bool big_endian>
8548 void
8549 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8550 Sized_relobj_file<size, big_endian>* object,
8551 unsigned int r_type,
8552 Symbol* gsym)
8553 {
8554 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8555 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8556 }
8557
8558 // Scan a relocation for a global symbol.
8559
8560 template<int size, bool big_endian>
8561 inline void
8562 Target_powerpc<size, big_endian>::Scan::global(
8563 Symbol_table* symtab,
8564 Layout* layout,
8565 Target_powerpc<size, big_endian>* target,
8566 Sized_relobj_file<size, big_endian>* object,
8567 unsigned int data_shndx,
8568 Output_section* output_section,
8569 const elfcpp::Rela<size, big_endian>& reloc,
8570 unsigned int r_type,
8571 Symbol* gsym)
8572 {
8573 Powerpc_relobj<size, big_endian>* ppc_object
8574 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8575
8576 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8577 {
8578 case Track_tls::SKIP:
8579 return;
8580 default:
8581 break;
8582 }
8583
8584 if (target->replace_tls_get_addr(gsym))
8585 // Change a __tls_get_addr reference to __tls_get_addr_opt
8586 // so dynamic relocs are emitted against the latter symbol.
8587 gsym = target->tls_get_addr_opt();
8588
8589 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8590 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8591 {
8592 this->expect_tls_get_addr_call();
8593 bool final = gsym->final_value_is_known();
8594 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8595 if (tls_type != tls::TLSOPT_NONE)
8596 this->skip_next_tls_get_addr_call();
8597 }
8598 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8599 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8600 {
8601 this->expect_tls_get_addr_call();
8602 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8603 if (tls_type != tls::TLSOPT_NONE)
8604 this->skip_next_tls_get_addr_call();
8605 }
8606
8607 // A STT_GNU_IFUNC symbol may require a PLT entry.
8608 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8609 bool pushed_ifunc = false;
8610 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8611 {
8612 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8613 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8614 r_type, r_sym, reloc.get_r_addend());
8615 target->make_plt_entry(symtab, layout, gsym);
8616 pushed_ifunc = true;
8617 }
8618
8619 switch (r_type)
8620 {
8621 case elfcpp::R_POWERPC_NONE:
8622 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8623 case elfcpp::R_POWERPC_GNU_VTENTRY:
8624 case elfcpp::R_PPC_LOCAL24PC:
8625 case elfcpp::R_POWERPC_TLS:
8626 case elfcpp::R_PPC64_ENTRY:
8627 case elfcpp::R_POWERPC_PLTSEQ:
8628 case elfcpp::R_POWERPC_PLTCALL:
8629 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8630 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8631 case elfcpp::R_PPC64_PCREL_OPT:
8632 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8633 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8634 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8635 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8636 case elfcpp::R_PPC64_REL16_HIGHER34:
8637 case elfcpp::R_PPC64_REL16_HIGHERA34:
8638 case elfcpp::R_PPC64_REL16_HIGHEST34:
8639 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8640 case elfcpp::R_PPC64_D34:
8641 case elfcpp::R_PPC64_D34_LO:
8642 case elfcpp::R_PPC64_D34_HI30:
8643 case elfcpp::R_PPC64_D34_HA30:
8644 case elfcpp::R_PPC64_D28:
8645 case elfcpp::R_PPC64_PCREL34:
8646 case elfcpp::R_PPC64_PCREL28:
8647 case elfcpp::R_PPC64_TPREL34:
8648 case elfcpp::R_PPC64_DTPREL34:
8649 break;
8650
8651 case elfcpp::R_PPC64_TOC:
8652 {
8653 Output_data_got_powerpc<size, big_endian>* got
8654 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8655 if (parameters->options().output_is_position_independent())
8656 {
8657 Address off = reloc.get_r_offset();
8658 if (size == 64
8659 && data_shndx == ppc_object->opd_shndx()
8660 && ppc_object->get_opd_discard(off - 8))
8661 break;
8662
8663 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8664 Address got_off = got->g_o_t();
8665 rela_dyn->add_output_section_relative(got->output_section(),
8666 elfcpp::R_POWERPC_RELATIVE,
8667 output_section,
8668 object, data_shndx, off,
8669 got_off);
8670 }
8671 }
8672 break;
8673
8674 case elfcpp::R_PPC64_ADDR64:
8675 if (size == 64
8676 && target->abiversion() < 2
8677 && data_shndx == ppc_object->opd_shndx()
8678 && (gsym->is_defined_in_discarded_section()
8679 || gsym->object() != object))
8680 {
8681 ppc_object->set_opd_discard(reloc.get_r_offset());
8682 break;
8683 }
8684 // Fall through.
8685 case elfcpp::R_PPC64_UADDR64:
8686 case elfcpp::R_POWERPC_ADDR32:
8687 case elfcpp::R_POWERPC_UADDR32:
8688 case elfcpp::R_POWERPC_ADDR24:
8689 case elfcpp::R_POWERPC_ADDR16:
8690 case elfcpp::R_POWERPC_ADDR16_LO:
8691 case elfcpp::R_POWERPC_ADDR16_HI:
8692 case elfcpp::R_POWERPC_ADDR16_HA:
8693 case elfcpp::R_POWERPC_UADDR16:
8694 case elfcpp::R_PPC64_ADDR16_HIGH:
8695 case elfcpp::R_PPC64_ADDR16_HIGHA:
8696 case elfcpp::R_PPC64_ADDR16_HIGHER:
8697 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8698 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8699 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8700 case elfcpp::R_PPC64_ADDR16_DS:
8701 case elfcpp::R_PPC64_ADDR16_LO_DS:
8702 case elfcpp::R_POWERPC_ADDR14:
8703 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8704 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8705 {
8706 // Make a PLT entry if necessary.
8707 if (gsym->needs_plt_entry())
8708 {
8709 // Since this is not a PC-relative relocation, we may be
8710 // taking the address of a function. In that case we need to
8711 // set the entry in the dynamic symbol table to the address of
8712 // the PLT call stub.
8713 bool need_ifunc_plt = false;
8714 if ((size == 32 || target->abiversion() >= 2)
8715 && gsym->is_from_dynobj()
8716 && !parameters->options().output_is_position_independent())
8717 {
8718 gsym->set_needs_dynsym_value();
8719 need_ifunc_plt = true;
8720 }
8721 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8722 {
8723 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8724 target->push_branch(ppc_object, data_shndx,
8725 reloc.get_r_offset(), r_type, r_sym,
8726 reloc.get_r_addend());
8727 target->make_plt_entry(symtab, layout, gsym);
8728 }
8729 }
8730 // Make a dynamic relocation if necessary.
8731 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8732 || (size == 64 && is_ifunc && target->abiversion() < 2))
8733 {
8734 if (!parameters->options().output_is_position_independent()
8735 && gsym->may_need_copy_reloc())
8736 {
8737 target->copy_reloc(symtab, layout, object,
8738 data_shndx, output_section, gsym, reloc);
8739 }
8740 else if ((((size == 32
8741 && r_type == elfcpp::R_POWERPC_ADDR32)
8742 || (size == 64
8743 && r_type == elfcpp::R_PPC64_ADDR64
8744 && target->abiversion() >= 2))
8745 && gsym->can_use_relative_reloc(false)
8746 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8747 && parameters->options().shared()))
8748 || (size == 64
8749 && r_type == elfcpp::R_PPC64_ADDR64
8750 && target->abiversion() < 2
8751 && (gsym->can_use_relative_reloc(false)
8752 || data_shndx == ppc_object->opd_shndx())))
8753 {
8754 Reloc_section* rela_dyn
8755 = target->rela_dyn_section(symtab, layout, is_ifunc);
8756 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8757 : elfcpp::R_POWERPC_RELATIVE);
8758 rela_dyn->add_symbolless_global_addend(
8759 gsym, dynrel, output_section, object, data_shndx,
8760 reloc.get_r_offset(), reloc.get_r_addend());
8761 }
8762 else
8763 {
8764 Reloc_section* rela_dyn
8765 = target->rela_dyn_section(symtab, layout, is_ifunc);
8766 check_non_pic(object, r_type);
8767 rela_dyn->add_global(gsym, r_type, output_section,
8768 object, data_shndx,
8769 reloc.get_r_offset(),
8770 reloc.get_r_addend());
8771
8772 if (size == 64
8773 && parameters->options().toc_optimize()
8774 && data_shndx == ppc_object->toc_shndx())
8775 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8776 }
8777 }
8778 }
8779 break;
8780
8781 case elfcpp::R_PPC64_PLT_PCREL34:
8782 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8783 case elfcpp::R_POWERPC_PLT16_LO:
8784 case elfcpp::R_POWERPC_PLT16_HI:
8785 case elfcpp::R_POWERPC_PLT16_HA:
8786 case elfcpp::R_PPC64_PLT16_LO_DS:
8787 if (!pushed_ifunc)
8788 target->make_plt_entry(symtab, layout, gsym);
8789 break;
8790
8791 case elfcpp::R_PPC64_REL24_NOTOC:
8792 if (size == 32)
8793 break;
8794 // Fall through.
8795 case elfcpp::R_PPC_PLTREL24:
8796 case elfcpp::R_POWERPC_REL24:
8797 if (!is_ifunc)
8798 {
8799 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8800 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8801 r_type, r_sym, reloc.get_r_addend());
8802 if (gsym->needs_plt_entry()
8803 || (!gsym->final_value_is_known()
8804 && (gsym->is_undefined()
8805 || gsym->is_from_dynobj()
8806 || gsym->is_preemptible())))
8807 target->make_plt_entry(symtab, layout, gsym);
8808 }
8809 // Fall through.
8810
8811 case elfcpp::R_PPC64_REL64:
8812 case elfcpp::R_POWERPC_REL32:
8813 // Make a dynamic relocation if necessary.
8814 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8815 {
8816 if (!parameters->options().output_is_position_independent()
8817 && gsym->may_need_copy_reloc())
8818 {
8819 target->copy_reloc(symtab, layout, object,
8820 data_shndx, output_section, gsym,
8821 reloc);
8822 }
8823 else
8824 {
8825 Reloc_section* rela_dyn
8826 = target->rela_dyn_section(symtab, layout, is_ifunc);
8827 check_non_pic(object, r_type);
8828 rela_dyn->add_global(gsym, r_type, output_section, object,
8829 data_shndx, reloc.get_r_offset(),
8830 reloc.get_r_addend());
8831 }
8832 }
8833 break;
8834
8835 case elfcpp::R_POWERPC_REL14:
8836 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8837 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8838 if (!is_ifunc)
8839 {
8840 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8841 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8842 r_type, r_sym, reloc.get_r_addend());
8843 }
8844 break;
8845
8846 case elfcpp::R_PPC64_TOCSAVE:
8847 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8848 // caller has already saved r2 and thus a plt call stub need not
8849 // save r2.
8850 if (size == 64
8851 && target->mark_pltcall(ppc_object, data_shndx,
8852 reloc.get_r_offset() - 4, symtab))
8853 {
8854 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8855 bool is_ordinary;
8856 unsigned int shndx = gsym->shndx(&is_ordinary);
8857 if (!is_ordinary)
8858 object->error(_("tocsave symbol %u has bad shndx %u"),
8859 r_sym, shndx);
8860 else
8861 {
8862 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8863 target->add_tocsave(ppc_object, shndx,
8864 sym->value() + reloc.get_r_addend());
8865 }
8866 }
8867 break;
8868
8869 case elfcpp::R_POWERPC_REL16:
8870 case elfcpp::R_POWERPC_REL16_LO:
8871 case elfcpp::R_POWERPC_REL16_HI:
8872 case elfcpp::R_POWERPC_REL16_HA:
8873 case elfcpp::R_POWERPC_REL16DX_HA:
8874 case elfcpp::R_PPC64_REL16_HIGH:
8875 case elfcpp::R_PPC64_REL16_HIGHA:
8876 case elfcpp::R_PPC64_REL16_HIGHER:
8877 case elfcpp::R_PPC64_REL16_HIGHERA:
8878 case elfcpp::R_PPC64_REL16_HIGHEST:
8879 case elfcpp::R_PPC64_REL16_HIGHESTA:
8880 case elfcpp::R_POWERPC_SECTOFF:
8881 case elfcpp::R_POWERPC_SECTOFF_LO:
8882 case elfcpp::R_POWERPC_SECTOFF_HI:
8883 case elfcpp::R_POWERPC_SECTOFF_HA:
8884 case elfcpp::R_PPC64_SECTOFF_DS:
8885 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8886 case elfcpp::R_POWERPC_TPREL16:
8887 case elfcpp::R_POWERPC_TPREL16_LO:
8888 case elfcpp::R_POWERPC_TPREL16_HI:
8889 case elfcpp::R_POWERPC_TPREL16_HA:
8890 case elfcpp::R_PPC64_TPREL16_DS:
8891 case elfcpp::R_PPC64_TPREL16_LO_DS:
8892 case elfcpp::R_PPC64_TPREL16_HIGH:
8893 case elfcpp::R_PPC64_TPREL16_HIGHA:
8894 case elfcpp::R_PPC64_TPREL16_HIGHER:
8895 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8896 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8897 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8898 case elfcpp::R_POWERPC_DTPREL16:
8899 case elfcpp::R_POWERPC_DTPREL16_LO:
8900 case elfcpp::R_POWERPC_DTPREL16_HI:
8901 case elfcpp::R_POWERPC_DTPREL16_HA:
8902 case elfcpp::R_PPC64_DTPREL16_DS:
8903 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8904 case elfcpp::R_PPC64_DTPREL16_HIGH:
8905 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8906 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8907 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8908 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8909 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8910 case elfcpp::R_PPC64_TLSGD:
8911 case elfcpp::R_PPC64_TLSLD:
8912 case elfcpp::R_PPC64_ADDR64_LOCAL:
8913 break;
8914
8915 case elfcpp::R_PPC64_GOT_PCREL34:
8916 case elfcpp::R_POWERPC_GOT16:
8917 case elfcpp::R_POWERPC_GOT16_LO:
8918 case elfcpp::R_POWERPC_GOT16_HI:
8919 case elfcpp::R_POWERPC_GOT16_HA:
8920 case elfcpp::R_PPC64_GOT16_DS:
8921 case elfcpp::R_PPC64_GOT16_LO_DS:
8922 {
8923 // The symbol requires a GOT entry.
8924 Output_data_got_powerpc<size, big_endian>* got;
8925 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8926 Got_type got_type = ((size == 32
8927 || r_type == elfcpp::R_POWERPC_GOT16
8928 || r_type == elfcpp::R_PPC64_GOT16_DS)
8929 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8930
8931 got = target->got_section(symtab, layout, got_type);
8932 if (gsym->final_value_is_known())
8933 {
8934 if (is_ifunc
8935 && (size == 32 || target->abiversion() >= 2))
8936 got->add_global_plt(gsym, got_type, addend);
8937 else
8938 got->add_global(gsym, got_type, addend);
8939 }
8940 else if (!gsym->has_got_offset(got_type, addend))
8941 {
8942 // If we are generating a shared object or a pie, this
8943 // symbol's GOT entry will be set by a dynamic relocation.
8944 unsigned int off = got->add_constant(0);
8945 gsym->set_got_offset(got_type, off, addend);
8946
8947 Reloc_section* rela_dyn
8948 = target->rela_dyn_section(symtab, layout, is_ifunc);
8949
8950 if (gsym->can_use_relative_reloc(false)
8951 && !((size == 32
8952 || target->abiversion() >= 2)
8953 && gsym->visibility() == elfcpp::STV_PROTECTED
8954 && parameters->options().shared()))
8955 {
8956 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8957 : elfcpp::R_POWERPC_RELATIVE);
8958 rela_dyn->add_global_relative(gsym, dynrel, got, off,
8959 addend, false);
8960 }
8961 else
8962 {
8963 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8964 rela_dyn->add_global(gsym, dynrel, got, off, addend);
8965 }
8966 }
8967 }
8968 break;
8969
8970 case elfcpp::R_PPC64_TOC16:
8971 case elfcpp::R_PPC64_TOC16_LO:
8972 case elfcpp::R_PPC64_TOC16_HI:
8973 case elfcpp::R_PPC64_TOC16_HA:
8974 case elfcpp::R_PPC64_TOC16_DS:
8975 case elfcpp::R_PPC64_TOC16_LO_DS:
8976 // We need a GOT section.
8977 target->got_section(symtab, layout, GOT_TYPE_SMALL);
8978 break;
8979
8980 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8981 case elfcpp::R_POWERPC_GOT_TLSGD16:
8982 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8983 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8984 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8985 {
8986 bool final = gsym->final_value_is_known();
8987 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8988 if (tls_type == tls::TLSOPT_NONE)
8989 {
8990 Got_type got_type = ((size == 32
8991 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8992 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8993 Output_data_got_powerpc<size, big_endian>* got
8994 = target->got_section(symtab, layout, got_type);
8995 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8996 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8997 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
8998 elfcpp::R_POWERPC_DTPMOD,
8999 elfcpp::R_POWERPC_DTPREL,
9000 addend);
9001 }
9002 else if (tls_type == tls::TLSOPT_TO_IE)
9003 {
9004 Got_type got_type = ((size == 32
9005 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9006 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9007 if (!gsym->has_got_offset(got_type))
9008 {
9009 Output_data_got_powerpc<size, big_endian>* got
9010 = target->got_section(symtab, layout, got_type);
9011 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9012 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9013 if (gsym->is_undefined()
9014 || gsym->is_from_dynobj())
9015 {
9016 got->add_global_with_rel(gsym, got_type, rela_dyn,
9017 elfcpp::R_POWERPC_TPREL, addend);
9018 }
9019 else
9020 {
9021 unsigned int off = got->add_constant(0);
9022 gsym->set_got_offset(got_type, off);
9023 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9024 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9025 got, off, addend);
9026 }
9027 }
9028 }
9029 else if (tls_type == tls::TLSOPT_TO_LE)
9030 {
9031 // no GOT relocs needed for Local Exec.
9032 }
9033 else
9034 gold_unreachable();
9035 }
9036 break;
9037
9038 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9039 case elfcpp::R_POWERPC_GOT_TLSLD16:
9040 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9041 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9042 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9043 {
9044 tls::Tls_optimization tls_type = target->optimize_tls_ld();
9045 if (tls_type == tls::TLSOPT_NONE)
9046 target->tlsld_got_offset(symtab, layout, object);
9047 else if (tls_type == tls::TLSOPT_TO_LE)
9048 {
9049 // no GOT relocs needed for Local Exec.
9050 if (parameters->options().emit_relocs())
9051 {
9052 Output_section* os = layout->tls_segment()->first_section();
9053 gold_assert(os != NULL);
9054 os->set_needs_symtab_index();
9055 }
9056 }
9057 else
9058 gold_unreachable();
9059 }
9060 break;
9061
9062 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9063 case elfcpp::R_POWERPC_GOT_DTPREL16:
9064 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9065 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9066 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9067 {
9068 Got_type got_type = ((size == 32
9069 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9070 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
9071 Output_data_got_powerpc<size, big_endian>* got
9072 = target->got_section(symtab, layout, got_type);
9073 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9074 if (!gsym->final_value_is_known()
9075 && (gsym->is_from_dynobj()
9076 || gsym->is_undefined()
9077 || gsym->is_preemptible()))
9078 got->add_global_with_rel(gsym, got_type,
9079 target->rela_dyn_section(layout),
9080 elfcpp::R_POWERPC_DTPREL, addend);
9081 else
9082 got->add_global_tls(gsym, got_type, addend);
9083 }
9084 break;
9085
9086 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9087 case elfcpp::R_POWERPC_GOT_TPREL16:
9088 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9089 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9090 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9091 {
9092 bool final = gsym->final_value_is_known();
9093 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9094 if (tls_type == tls::TLSOPT_NONE)
9095 {
9096 Got_type got_type = ((size == 32
9097 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9098 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9099 if (!gsym->has_got_offset(got_type))
9100 {
9101 Output_data_got_powerpc<size, big_endian>* got
9102 = target->got_section(symtab, layout, got_type);
9103 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9104 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9105 if (gsym->is_undefined()
9106 || gsym->is_from_dynobj())
9107 {
9108 got->add_global_with_rel(gsym, got_type, rela_dyn,
9109 elfcpp::R_POWERPC_TPREL, addend);
9110 }
9111 else
9112 {
9113 unsigned int off = got->add_constant(0);
9114 gsym->set_got_offset(got_type, off);
9115 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9116 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9117 got, off, addend);
9118 }
9119 }
9120 }
9121 else if (tls_type == tls::TLSOPT_TO_LE)
9122 {
9123 // no GOT relocs needed for Local Exec.
9124 }
9125 else
9126 gold_unreachable();
9127 }
9128 break;
9129
9130 default:
9131 unsupported_reloc_global(object, r_type, gsym);
9132 break;
9133 }
9134
9135 if (size == 64
9136 && parameters->options().toc_optimize())
9137 {
9138 if (data_shndx == ppc_object->toc_shndx())
9139 {
9140 bool ok = true;
9141 if (r_type != elfcpp::R_PPC64_ADDR64
9142 || (is_ifunc && target->abiversion() < 2))
9143 ok = false;
9144 else if (parameters->options().output_is_position_independent()
9145 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9146 ok = false;
9147 if (!ok)
9148 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9149 }
9150
9151 enum {no_check, check_lo, check_ha} insn_check;
9152 switch (r_type)
9153 {
9154 default:
9155 insn_check = no_check;
9156 break;
9157
9158 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9159 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9160 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9161 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9162 case elfcpp::R_POWERPC_GOT16_HA:
9163 case elfcpp::R_PPC64_TOC16_HA:
9164 insn_check = check_ha;
9165 break;
9166
9167 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9168 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9169 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9170 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9171 case elfcpp::R_POWERPC_GOT16_LO:
9172 case elfcpp::R_PPC64_GOT16_LO_DS:
9173 case elfcpp::R_PPC64_TOC16_LO:
9174 case elfcpp::R_PPC64_TOC16_LO_DS:
9175 insn_check = check_lo;
9176 break;
9177 }
9178
9179 section_size_type slen;
9180 const unsigned char* view = NULL;
9181 if (insn_check != no_check)
9182 {
9183 view = ppc_object->section_contents(data_shndx, &slen, false);
9184 section_size_type off =
9185 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9186 if (off < slen)
9187 {
9188 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9189 if (insn_check == check_lo
9190 ? !ok_lo_toc_insn(insn, r_type)
9191 : ((insn & ((0x3f << 26) | 0x1f << 16))
9192 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9193 {
9194 ppc_object->set_no_toc_opt();
9195 gold_warning(_("%s: toc optimization is not supported "
9196 "for %#08x instruction"),
9197 ppc_object->name().c_str(), insn);
9198 }
9199 }
9200 }
9201
9202 switch (r_type)
9203 {
9204 default:
9205 break;
9206 case elfcpp::R_PPC64_TOC16:
9207 case elfcpp::R_PPC64_TOC16_LO:
9208 case elfcpp::R_PPC64_TOC16_HI:
9209 case elfcpp::R_PPC64_TOC16_HA:
9210 case elfcpp::R_PPC64_TOC16_DS:
9211 case elfcpp::R_PPC64_TOC16_LO_DS:
9212 if (gsym->source() == Symbol::FROM_OBJECT
9213 && !gsym->object()->is_dynamic())
9214 {
9215 Powerpc_relobj<size, big_endian>* sym_object
9216 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9217 bool is_ordinary;
9218 unsigned int shndx = gsym->shndx(&is_ordinary);
9219 if (shndx == sym_object->toc_shndx())
9220 {
9221 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9222 Address dst_off = sym->value() + reloc.get_r_addend();
9223 if (dst_off < sym_object->section_size(shndx))
9224 {
9225 bool ok = false;
9226 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9227 ok = true;
9228 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9229 {
9230 // Need to check that the insn is a ld
9231 if (!view)
9232 view = ppc_object->section_contents(data_shndx,
9233 &slen,
9234 false);
9235 section_size_type off =
9236 (convert_to_section_size_type(reloc.get_r_offset())
9237 + (big_endian ? -2 : 3));
9238 if (off < slen
9239 && (view[off] & (0x3f << 2)) == (58u << 2))
9240 ok = true;
9241 }
9242 if (!ok)
9243 sym_object->set_no_toc_opt(dst_off);
9244 }
9245 }
9246 }
9247 break;
9248 }
9249 }
9250
9251 if (size == 32)
9252 {
9253 switch (r_type)
9254 {
9255 case elfcpp::R_PPC_LOCAL24PC:
9256 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9257 gold_error(_("%s: unsupported -mbss-plt code"),
9258 ppc_object->name().c_str());
9259 break;
9260 default:
9261 break;
9262 }
9263 }
9264
9265 switch (r_type)
9266 {
9267 case elfcpp::R_POWERPC_GOT_TLSLD16:
9268 case elfcpp::R_POWERPC_GOT_TLSGD16:
9269 case elfcpp::R_POWERPC_GOT_TPREL16:
9270 case elfcpp::R_POWERPC_GOT_DTPREL16:
9271 case elfcpp::R_POWERPC_GOT16:
9272 case elfcpp::R_PPC64_GOT16_DS:
9273 case elfcpp::R_PPC64_TOC16:
9274 case elfcpp::R_PPC64_TOC16_DS:
9275 ppc_object->set_has_small_toc_reloc();
9276 break;
9277 default:
9278 break;
9279 }
9280
9281 switch (r_type)
9282 {
9283 case elfcpp::R_PPC64_TPREL16_DS:
9284 case elfcpp::R_PPC64_TPREL16_LO_DS:
9285 case elfcpp::R_PPC64_TPREL16_HIGH:
9286 case elfcpp::R_PPC64_TPREL16_HIGHA:
9287 case elfcpp::R_PPC64_TPREL16_HIGHER:
9288 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9289 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9290 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9291 case elfcpp::R_PPC64_TPREL34:
9292 if (size != 64)
9293 break;
9294 // Fall through.
9295 case elfcpp::R_POWERPC_TPREL16:
9296 case elfcpp::R_POWERPC_TPREL16_LO:
9297 case elfcpp::R_POWERPC_TPREL16_HI:
9298 case elfcpp::R_POWERPC_TPREL16_HA:
9299 layout->set_has_static_tls();
9300 break;
9301 default:
9302 break;
9303 }
9304
9305 switch (r_type)
9306 {
9307 case elfcpp::R_POWERPC_TPREL16_HA:
9308 if (target->tprel_opt())
9309 {
9310 section_size_type slen;
9311 const unsigned char* view = NULL;
9312 view = ppc_object->section_contents(data_shndx, &slen, false);
9313 section_size_type off
9314 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9315 if (off < slen)
9316 {
9317 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9318 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9319 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9320 target->set_no_tprel_opt();
9321 }
9322 }
9323 break;
9324
9325 case elfcpp::R_PPC64_TPREL16_HIGH:
9326 case elfcpp::R_PPC64_TPREL16_HIGHA:
9327 case elfcpp::R_PPC64_TPREL16_HIGHER:
9328 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9329 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9330 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9331 if (size != 64)
9332 break;
9333 // Fall through.
9334 case elfcpp::R_POWERPC_TPREL16_HI:
9335 target->set_no_tprel_opt();
9336 break;
9337 default:
9338 break;
9339 }
9340
9341 switch (r_type)
9342 {
9343 case elfcpp::R_PPC64_D34:
9344 case elfcpp::R_PPC64_D34_LO:
9345 case elfcpp::R_PPC64_D34_HI30:
9346 case elfcpp::R_PPC64_D34_HA30:
9347 case elfcpp::R_PPC64_D28:
9348 case elfcpp::R_PPC64_PCREL34:
9349 case elfcpp::R_PPC64_PCREL28:
9350 case elfcpp::R_PPC64_TPREL34:
9351 case elfcpp::R_PPC64_DTPREL34:
9352 case elfcpp::R_PPC64_PLT_PCREL34:
9353 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9354 case elfcpp::R_PPC64_GOT_PCREL34:
9355 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9356 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9357 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9358 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9359 target->set_power10_relocs();
9360 break;
9361 default:
9362 break;
9363 }
9364 }
9365
9366 // Process relocations for gc.
9367
9368 template<int size, bool big_endian>
9369 void
9370 Target_powerpc<size, big_endian>::gc_process_relocs(
9371 Symbol_table* symtab,
9372 Layout* layout,
9373 Sized_relobj_file<size, big_endian>* object,
9374 unsigned int data_shndx,
9375 unsigned int,
9376 const unsigned char* prelocs,
9377 size_t reloc_count,
9378 Output_section* output_section,
9379 bool needs_special_offset_handling,
9380 size_t local_symbol_count,
9381 const unsigned char* plocal_symbols)
9382 {
9383 typedef Target_powerpc<size, big_endian> Powerpc;
9384 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9385 Classify_reloc;
9386
9387 Powerpc_relobj<size, big_endian>* ppc_object
9388 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9389 if (size == 64)
9390 ppc_object->set_opd_valid();
9391 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9392 {
9393 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9394 for (p = ppc_object->access_from_map()->begin();
9395 p != ppc_object->access_from_map()->end();
9396 ++p)
9397 {
9398 Address dst_off = p->first;
9399 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9400 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9401 for (s = p->second.begin(); s != p->second.end(); ++s)
9402 {
9403 Relobj* src_obj = s->first;
9404 unsigned int src_indx = s->second;
9405 symtab->gc()->add_reference(src_obj, src_indx,
9406 ppc_object, dst_indx);
9407 }
9408 p->second.clear();
9409 }
9410 ppc_object->access_from_map()->clear();
9411 ppc_object->process_gc_mark(symtab);
9412 // Don't look at .opd relocs as .opd will reference everything.
9413 return;
9414 }
9415
9416 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9417 symtab,
9418 layout,
9419 this,
9420 object,
9421 data_shndx,
9422 prelocs,
9423 reloc_count,
9424 output_section,
9425 needs_special_offset_handling,
9426 local_symbol_count,
9427 plocal_symbols);
9428 }
9429
9430 // Handle target specific gc actions when adding a gc reference from
9431 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9432 // and DST_OFF. For powerpc64, this adds a referenc to the code
9433 // section of a function descriptor.
9434
9435 template<int size, bool big_endian>
9436 void
9437 Target_powerpc<size, big_endian>::do_gc_add_reference(
9438 Symbol_table* symtab,
9439 Relobj* src_obj,
9440 unsigned int src_shndx,
9441 Relobj* dst_obj,
9442 unsigned int dst_shndx,
9443 Address dst_off) const
9444 {
9445 if (size != 64 || dst_obj->is_dynamic())
9446 return;
9447
9448 Powerpc_relobj<size, big_endian>* ppc_object
9449 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9450 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9451 {
9452 if (ppc_object->opd_valid())
9453 {
9454 dst_shndx = ppc_object->get_opd_ent(dst_off);
9455 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9456 }
9457 else
9458 {
9459 // If we haven't run scan_opd_relocs, we must delay
9460 // processing this function descriptor reference.
9461 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9462 }
9463 }
9464 }
9465
9466 // Add any special sections for this symbol to the gc work list.
9467 // For powerpc64, this adds the code section of a function
9468 // descriptor.
9469
9470 template<int size, bool big_endian>
9471 void
9472 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9473 Symbol_table* symtab,
9474 Symbol* sym) const
9475 {
9476 if (size == 64 && sym->object()->pluginobj() == NULL)
9477 {
9478 Powerpc_relobj<size, big_endian>* ppc_object
9479 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9480 bool is_ordinary;
9481 unsigned int shndx = sym->shndx(&is_ordinary);
9482 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9483 {
9484 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9485 Address dst_off = gsym->value();
9486 if (ppc_object->opd_valid())
9487 {
9488 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9489 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9490 dst_indx));
9491 }
9492 else
9493 ppc_object->add_gc_mark(dst_off);
9494 }
9495 }
9496 }
9497
9498 // For a symbol location in .opd, set LOC to the location of the
9499 // function entry.
9500
9501 template<int size, bool big_endian>
9502 void
9503 Target_powerpc<size, big_endian>::do_function_location(
9504 Symbol_location* loc) const
9505 {
9506 if (size == 64 && loc->shndx != 0)
9507 {
9508 if (loc->object->is_dynamic())
9509 {
9510 Powerpc_dynobj<size, big_endian>* ppc_object
9511 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9512 if (loc->shndx == ppc_object->opd_shndx())
9513 {
9514 Address dest_off;
9515 Address off = loc->offset - ppc_object->opd_address();
9516 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9517 loc->offset = dest_off;
9518 }
9519 }
9520 else
9521 {
9522 const Powerpc_relobj<size, big_endian>* ppc_object
9523 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9524 if (loc->shndx == ppc_object->opd_shndx())
9525 {
9526 Address dest_off;
9527 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9528 loc->offset = dest_off;
9529 }
9530 }
9531 }
9532 }
9533
9534 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9535 // compiled with -fsplit-stack. The function calls non-split-stack
9536 // code. Change the function to ensure it has enough stack space to
9537 // call some random function.
9538
9539 template<int size, bool big_endian>
9540 void
9541 Target_powerpc<size, big_endian>::do_calls_non_split(
9542 Relobj* object,
9543 unsigned int shndx,
9544 section_offset_type fnoffset,
9545 section_size_type fnsize,
9546 const unsigned char* prelocs,
9547 size_t reloc_count,
9548 unsigned char* view,
9549 section_size_type view_size,
9550 std::string* from,
9551 std::string* to) const
9552 {
9553 // 32-bit not supported.
9554 if (size == 32)
9555 {
9556 // warn
9557 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9558 prelocs, reloc_count, view, view_size,
9559 from, to);
9560 return;
9561 }
9562
9563 // The function always starts with
9564 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9565 // addis %r12,%r1,-allocate@ha
9566 // addi %r12,%r12,-allocate@l
9567 // cmpld %r12,%r0
9568 // but note that the addis or addi may be replaced with a nop
9569
9570 unsigned char *entry = view + fnoffset;
9571 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9572
9573 if ((insn & 0xffff0000) == addis_2_12)
9574 {
9575 /* Skip ELFv2 global entry code. */
9576 entry += 8;
9577 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9578 }
9579
9580 unsigned char *pinsn = entry;
9581 bool ok = false;
9582 const uint32_t ld_private_ss = 0xe80d8fc0;
9583 if (insn == ld_private_ss)
9584 {
9585 int32_t allocate = 0;
9586 while (1)
9587 {
9588 pinsn += 4;
9589 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9590 if ((insn & 0xffff0000) == addis_12_1)
9591 allocate += (insn & 0xffff) << 16;
9592 else if ((insn & 0xffff0000) == addi_12_1
9593 || (insn & 0xffff0000) == addi_12_12)
9594 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9595 else if (insn != nop)
9596 break;
9597 }
9598 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9599 {
9600 int extra = parameters->options().split_stack_adjust_size();
9601 allocate -= extra;
9602 if (allocate >= 0 || extra < 0)
9603 {
9604 object->error(_("split-stack stack size overflow at "
9605 "section %u offset %0zx"),
9606 shndx, static_cast<size_t>(fnoffset));
9607 return;
9608 }
9609 pinsn = entry + 4;
9610 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9611 if (insn != addis_12_1)
9612 {
9613 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9614 pinsn += 4;
9615 insn = addi_12_12 | (allocate & 0xffff);
9616 if (insn != addi_12_12)
9617 {
9618 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9619 pinsn += 4;
9620 }
9621 }
9622 else
9623 {
9624 insn = addi_12_1 | (allocate & 0xffff);
9625 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9626 pinsn += 4;
9627 }
9628 if (pinsn != entry + 12)
9629 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9630
9631 ok = true;
9632 }
9633 }
9634
9635 if (!ok)
9636 {
9637 if (!object->has_no_split_stack())
9638 object->error(_("failed to match split-stack sequence at "
9639 "section %u offset %0zx"),
9640 shndx, static_cast<size_t>(fnoffset));
9641 }
9642 }
9643
9644 // Scan relocations for a section.
9645
9646 template<int size, bool big_endian>
9647 void
9648 Target_powerpc<size, big_endian>::scan_relocs(
9649 Symbol_table* symtab,
9650 Layout* layout,
9651 Sized_relobj_file<size, big_endian>* object,
9652 unsigned int data_shndx,
9653 unsigned int sh_type,
9654 const unsigned char* prelocs,
9655 size_t reloc_count,
9656 Output_section* output_section,
9657 bool needs_special_offset_handling,
9658 size_t local_symbol_count,
9659 const unsigned char* plocal_symbols)
9660 {
9661 typedef Target_powerpc<size, big_endian> Powerpc;
9662 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9663 Classify_reloc;
9664
9665 if (!this->plt_localentry0_init_)
9666 {
9667 bool plt_localentry0 = false;
9668 if (size == 64
9669 && this->abiversion() >= 2)
9670 {
9671 if (parameters->options().user_set_plt_localentry())
9672 plt_localentry0 = parameters->options().plt_localentry();
9673 if (plt_localentry0
9674 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9675 gold_warning(_("--plt-localentry is especially dangerous without "
9676 "ld.so support to detect ABI violations"));
9677 }
9678 this->plt_localentry0_ = plt_localentry0;
9679 this->plt_localentry0_init_ = true;
9680 }
9681
9682 if (sh_type == elfcpp::SHT_REL)
9683 {
9684 gold_error(_("%s: unsupported REL reloc section"),
9685 object->name().c_str());
9686 return;
9687 }
9688
9689 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9690 symtab,
9691 layout,
9692 this,
9693 object,
9694 data_shndx,
9695 prelocs,
9696 reloc_count,
9697 output_section,
9698 needs_special_offset_handling,
9699 local_symbol_count,
9700 plocal_symbols);
9701
9702 if (this->plt_localentry0_ && this->power10_relocs_)
9703 {
9704 gold_warning(_("--plt-localentry is incompatible with "
9705 "power10 pc-relative code"));
9706 this->plt_localentry0_ = false;
9707 }
9708 }
9709
9710 // Functor class for processing the global symbol table.
9711 // Removes symbols defined on discarded opd entries.
9712
9713 template<bool big_endian>
9714 class Global_symbol_visitor_opd
9715 {
9716 public:
9717 Global_symbol_visitor_opd()
9718 { }
9719
9720 void
9721 operator()(Sized_symbol<64>* sym)
9722 {
9723 if (sym->has_symtab_index()
9724 || sym->source() != Symbol::FROM_OBJECT
9725 || !sym->in_real_elf())
9726 return;
9727
9728 if (sym->object()->is_dynamic())
9729 return;
9730
9731 Powerpc_relobj<64, big_endian>* symobj
9732 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9733 if (symobj->opd_shndx() == 0)
9734 return;
9735
9736 bool is_ordinary;
9737 unsigned int shndx = sym->shndx(&is_ordinary);
9738 if (shndx == symobj->opd_shndx()
9739 && symobj->get_opd_discard(sym->value()))
9740 {
9741 sym->set_undefined();
9742 sym->set_visibility(elfcpp::STV_DEFAULT);
9743 sym->set_is_defined_in_discarded_section();
9744 sym->set_symtab_index(-1U);
9745 }
9746 }
9747 };
9748
9749 template<int size, bool big_endian>
9750 void
9751 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9752 Layout* layout,
9753 Symbol_table* symtab)
9754 {
9755 if (size == 64)
9756 {
9757 Output_data_save_res<size, big_endian>* savres
9758 = new Output_data_save_res<size, big_endian>(symtab);
9759 this->savres_section_ = savres;
9760 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9761 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9762 savres, ORDER_TEXT, false);
9763 }
9764 }
9765
9766 // Sort linker created .got section first (for the header), then input
9767 // sections belonging to files using small model code.
9768
9769 template<bool big_endian>
9770 class Sort_toc_sections
9771 {
9772 const Output_section_data*
9773 small_got_section() const
9774 {
9775 return (static_cast<Target_powerpc<64, big_endian>*>(
9776 parameters->sized_target<64, big_endian>())
9777 ->got_section(GOT_TYPE_SMALL));
9778 }
9779
9780 int
9781 rank(const Output_section::Input_section& isec) const
9782 {
9783 if (!isec.is_input_section())
9784 {
9785 if (isec.output_section_data() == this->small_got_section())
9786 return 0;
9787 return 2;
9788 }
9789 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
9790 ->has_small_toc_reloc())
9791 return 1;
9792 return 3;
9793 }
9794
9795 public:
9796 bool
9797 operator()(const Output_section::Input_section& is1,
9798 const Output_section::Input_section& is2) const
9799 {
9800 return rank(is1) < rank(is2);
9801 }
9802 };
9803
9804 // Finalize the sections.
9805
9806 template<int size, bool big_endian>
9807 void
9808 Target_powerpc<size, big_endian>::do_finalize_sections(
9809 Layout* layout,
9810 const Input_objects* input_objects,
9811 Symbol_table* symtab)
9812 {
9813 if (parameters->doing_static_link())
9814 {
9815 // At least some versions of glibc elf-init.o have a strong
9816 // reference to __rela_iplt marker syms. A weak ref would be
9817 // better..
9818 if (this->iplt_ != NULL)
9819 {
9820 Reloc_section* rel = this->iplt_->rel_plt();
9821 symtab->define_in_output_data("__rela_iplt_start", NULL,
9822 Symbol_table::PREDEFINED, rel, 0, 0,
9823 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9824 elfcpp::STV_HIDDEN, 0, false, true);
9825 symtab->define_in_output_data("__rela_iplt_end", NULL,
9826 Symbol_table::PREDEFINED, rel, 0, 0,
9827 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9828 elfcpp::STV_HIDDEN, 0, true, true);
9829 }
9830 else
9831 {
9832 symtab->define_as_constant("__rela_iplt_start", NULL,
9833 Symbol_table::PREDEFINED, 0, 0,
9834 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9835 elfcpp::STV_HIDDEN, 0, true, false);
9836 symtab->define_as_constant("__rela_iplt_end", NULL,
9837 Symbol_table::PREDEFINED, 0, 0,
9838 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9839 elfcpp::STV_HIDDEN, 0, true, false);
9840 }
9841 }
9842
9843 if (size == 64)
9844 {
9845 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9846 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
9847
9848 if (!parameters->options().relocatable())
9849 {
9850 this->define_save_restore_funcs(layout, symtab);
9851
9852 // Annoyingly, we need to make these sections now whether or
9853 // not we need them. If we delay until do_relax then we
9854 // need to mess with the relaxation machinery checkpointing.
9855 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
9856 this->make_brlt_section(layout);
9857
9858 // FIXME, maybe. Here we could run through all the got
9859 // entries in the small got section, removing any duplicates
9860 // found in the big got section and renumbering offsets.
9861
9862 if (parameters->options().toc_sort())
9863 {
9864 Output_section* os = this->got_->output_section();
9865 if (os != NULL && os->input_sections().size() > 1)
9866 std::stable_sort(os->input_sections().begin(),
9867 os->input_sections().end(),
9868 Sort_toc_sections<big_endian>());
9869 }
9870 }
9871 }
9872
9873 // Fill in some more dynamic tags.
9874 Output_data_dynamic* odyn = layout->dynamic_data();
9875 if (odyn != NULL)
9876 {
9877 const Reloc_section* rel_plt = (this->plt_ == NULL
9878 ? NULL
9879 : this->plt_->rel_plt());
9880 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9881 this->rela_dyn_, true, size == 32);
9882
9883 if (size == 32)
9884 {
9885 if (this->got_ != NULL)
9886 {
9887 this->got_->finalize_data_size();
9888 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9889 this->got_, this->got_->g_o_t());
9890 }
9891 if (this->has_tls_get_addr_opt_)
9892 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
9893 }
9894 else
9895 {
9896 if (this->glink_ != NULL)
9897 {
9898 this->glink_->finalize_data_size();
9899 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9900 this->glink_,
9901 (this->glink_->pltresolve_size()
9902 - 32));
9903 }
9904 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9905 odyn->add_constant(elfcpp::DT_PPC64_OPT,
9906 ((this->has_localentry0_
9907 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9908 | (this->has_tls_get_addr_opt_
9909 ? elfcpp::PPC64_OPT_TLS : 0)));
9910 }
9911 }
9912
9913 // Emit any relocs we saved in an attempt to avoid generating COPY
9914 // relocs.
9915 if (this->copy_relocs_.any_saved_relocs())
9916 this->copy_relocs_.emit(this->rela_dyn_section(layout));
9917
9918 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9919 p != input_objects->relobj_end();
9920 ++p)
9921 {
9922 Powerpc_relobj<size, big_endian>* ppc_relobj
9923 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9924 if (ppc_relobj->attributes_section_data())
9925 this->merge_object_attributes(ppc_relobj,
9926 ppc_relobj->attributes_section_data());
9927 }
9928 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9929 p != input_objects->dynobj_end();
9930 ++p)
9931 {
9932 Powerpc_dynobj<size, big_endian>* ppc_dynobj
9933 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9934 if (ppc_dynobj->attributes_section_data())
9935 this->merge_object_attributes(ppc_dynobj,
9936 ppc_dynobj->attributes_section_data());
9937 }
9938
9939 // Create a .gnu.attributes section if we have merged any attributes
9940 // from inputs.
9941 if (this->attributes_section_data_ != NULL
9942 && this->attributes_section_data_->size() != 0)
9943 {
9944 Output_attributes_section_data* attributes_section
9945 = new Output_attributes_section_data(*this->attributes_section_data_);
9946 layout->add_output_section_data(".gnu.attributes",
9947 elfcpp::SHT_GNU_ATTRIBUTES, 0,
9948 attributes_section, ORDER_INVALID, false);
9949 }
9950 }
9951
9952 // Merge object attributes from input file called NAME with those of the
9953 // output. The input object attributes are in the object pointed by PASD.
9954
9955 template<int size, bool big_endian>
9956 void
9957 Target_powerpc<size, big_endian>::merge_object_attributes(
9958 const Object* obj,
9959 const Attributes_section_data* pasd)
9960 {
9961 // Return if there is no attributes section data.
9962 if (pasd == NULL)
9963 return;
9964
9965 // Create output object attributes.
9966 if (this->attributes_section_data_ == NULL)
9967 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9968
9969 const int vendor = Object_attribute::OBJ_ATTR_GNU;
9970 const Object_attribute* in_attr = pasd->known_attributes(vendor);
9971 Object_attribute* out_attr
9972 = this->attributes_section_data_->known_attributes(vendor);
9973
9974 const char* name = obj->name().c_str();
9975 const char* err;
9976 const char* first;
9977 const char* second;
9978 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9979 int in_fp = in_attr[tag].int_value() & 0xf;
9980 int out_fp = out_attr[tag].int_value() & 0xf;
9981 bool warn_only = obj->is_dynamic();
9982 if (in_fp != out_fp)
9983 {
9984 err = NULL;
9985 if ((in_fp & 3) == 0)
9986 ;
9987 else if ((out_fp & 3) == 0)
9988 {
9989 if (!warn_only)
9990 {
9991 out_fp |= in_fp & 3;
9992 out_attr[tag].set_int_value(out_fp);
9993 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9994 this->last_fp_ = name;
9995 }
9996 }
9997 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9998 {
9999 err = N_("%s uses hard float, %s uses soft float");
10000 first = this->last_fp_;
10001 second = name;
10002 }
10003 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10004 {
10005 err = N_("%s uses hard float, %s uses soft float");
10006 first = name;
10007 second = this->last_fp_;
10008 }
10009 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10010 {
10011 err = N_("%s uses double-precision hard float, "
10012 "%s uses single-precision hard float");
10013 first = this->last_fp_;
10014 second = name;
10015 }
10016 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10017 {
10018 err = N_("%s uses double-precision hard float, "
10019 "%s uses single-precision hard float");
10020 first = name;
10021 second = this->last_fp_;
10022 }
10023
10024 if (err || (in_fp & 0xc) == 0)
10025 ;
10026 else if ((out_fp & 0xc) == 0)
10027 {
10028 if (!warn_only)
10029 {
10030 out_fp |= in_fp & 0xc;
10031 out_attr[tag].set_int_value(out_fp);
10032 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10033 this->last_ld_ = name;
10034 }
10035 }
10036 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10037 {
10038 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10039 first = name;
10040 second = this->last_ld_;
10041 }
10042 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10043 {
10044 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10045 first = this->last_ld_;
10046 second = name;
10047 }
10048 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10049 {
10050 err = N_("%s uses IBM long double, %s uses IEEE long double");
10051 first = this->last_ld_;
10052 second = name;
10053 }
10054 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10055 {
10056 err = N_("%s uses IBM long double, %s uses IEEE long double");
10057 first = name;
10058 second = this->last_ld_;
10059 }
10060
10061 if (err)
10062 {
10063 if (parameters->options().warn_mismatch())
10064 {
10065 if (warn_only)
10066 gold_warning(_(err), first, second);
10067 else
10068 gold_error(_(err), first, second);
10069 }
10070 // Arrange for this attribute to be deleted. It's better to
10071 // say "don't know" about a file than to wrongly claim compliance.
10072 if (!warn_only)
10073 out_attr[tag].set_type(0);
10074 }
10075 }
10076
10077 if (size == 32)
10078 {
10079 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10080 int in_vec = in_attr[tag].int_value() & 3;
10081 int out_vec = out_attr[tag].int_value() & 3;
10082 if (in_vec != out_vec)
10083 {
10084 err = NULL;
10085 if (in_vec == 0)
10086 ;
10087 else if (out_vec == 0)
10088 {
10089 out_vec = in_vec;
10090 out_attr[tag].set_int_value(out_vec);
10091 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10092 this->last_vec_ = name;
10093 }
10094 // For now, allow generic to transition to AltiVec or SPE
10095 // without a warning. If GCC marked files with their stack
10096 // alignment and used don't-care markings for files which are
10097 // not affected by the vector ABI, we could warn about this
10098 // case too. */
10099 else if (in_vec == 1)
10100 ;
10101 else if (out_vec == 1)
10102 {
10103 out_vec = in_vec;
10104 out_attr[tag].set_int_value(out_vec);
10105 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10106 this->last_vec_ = name;
10107 }
10108 else if (out_vec < in_vec)
10109 {
10110 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10111 first = this->last_vec_;
10112 second = name;
10113 }
10114 else if (out_vec > in_vec)
10115 {
10116 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10117 first = name;
10118 second = this->last_vec_;
10119 }
10120 if (err)
10121 {
10122 if (parameters->options().warn_mismatch())
10123 gold_error(_(err), first, second);
10124 out_attr[tag].set_type(0);
10125 }
10126 }
10127
10128 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10129 int in_struct = in_attr[tag].int_value() & 3;
10130 int out_struct = out_attr[tag].int_value() & 3;
10131 if (in_struct != out_struct)
10132 {
10133 err = NULL;
10134 if (in_struct == 0 || in_struct == 3)
10135 ;
10136 else if (out_struct == 0)
10137 {
10138 out_struct = in_struct;
10139 out_attr[tag].set_int_value(out_struct);
10140 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10141 this->last_struct_ = name;
10142 }
10143 else if (out_struct < in_struct)
10144 {
10145 err = N_("%s uses r3/r4 for small structure returns, "
10146 "%s uses memory");
10147 first = this->last_struct_;
10148 second = name;
10149 }
10150 else if (out_struct > in_struct)
10151 {
10152 err = N_("%s uses r3/r4 for small structure returns, "
10153 "%s uses memory");
10154 first = name;
10155 second = this->last_struct_;
10156 }
10157 if (err)
10158 {
10159 if (parameters->options().warn_mismatch())
10160 gold_error(_(err), first, second);
10161 out_attr[tag].set_type(0);
10162 }
10163 }
10164 }
10165
10166 // Merge Tag_compatibility attributes and any common GNU ones.
10167 this->attributes_section_data_->merge(name, pasd);
10168 }
10169
10170 // Emit any saved relocs, and mark toc entries using any of these
10171 // relocs as not optimizable.
10172
10173 template<int sh_type, int size, bool big_endian>
10174 void
10175 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10176 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10177 {
10178 if (size == 64
10179 && parameters->options().toc_optimize())
10180 {
10181 for (typename Copy_relocs<sh_type, size, big_endian>::
10182 Copy_reloc_entries::iterator p = this->entries_.begin();
10183 p != this->entries_.end();
10184 ++p)
10185 {
10186 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10187 entry = *p;
10188
10189 // If the symbol is no longer defined in a dynamic object,
10190 // then we emitted a COPY relocation. If it is still
10191 // dynamic then we'll need dynamic relocations and thus
10192 // can't optimize toc entries.
10193 if (entry.sym_->is_from_dynobj())
10194 {
10195 Powerpc_relobj<size, big_endian>* ppc_object
10196 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10197 if (entry.shndx_ == ppc_object->toc_shndx())
10198 ppc_object->set_no_toc_opt(entry.address_);
10199 }
10200 }
10201 }
10202
10203 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10204 }
10205
10206 // Return the value to use for a branch relocation.
10207
10208 template<int size, bool big_endian>
10209 bool
10210 Target_powerpc<size, big_endian>::symval_for_branch(
10211 const Symbol_table* symtab,
10212 const Sized_symbol<size>* gsym,
10213 Powerpc_relobj<size, big_endian>* object,
10214 Address *value,
10215 unsigned int *dest_shndx)
10216 {
10217 if (size == 32 || this->abiversion() >= 2)
10218 gold_unreachable();
10219 *dest_shndx = 0;
10220
10221 // If the symbol is defined in an opd section, ie. is a function
10222 // descriptor, use the function descriptor code entry address
10223 Powerpc_relobj<size, big_endian>* symobj = object;
10224 if (gsym != NULL
10225 && (gsym->source() != Symbol::FROM_OBJECT
10226 || gsym->object()->is_dynamic()))
10227 return true;
10228 if (gsym != NULL)
10229 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10230 unsigned int shndx = symobj->opd_shndx();
10231 if (shndx == 0)
10232 return true;
10233 Address opd_addr = symobj->get_output_section_offset(shndx);
10234 if (opd_addr == invalid_address)
10235 return true;
10236 opd_addr += symobj->output_section_address(shndx);
10237 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10238 {
10239 Address sec_off;
10240 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10241 if (symtab->is_section_folded(symobj, *dest_shndx))
10242 {
10243 Section_id folded
10244 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10245 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10246 *dest_shndx = folded.second;
10247 }
10248 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10249 if (sec_addr == invalid_address)
10250 return false;
10251
10252 sec_addr += symobj->output_section(*dest_shndx)->address();
10253 *value = sec_addr + sec_off;
10254 }
10255 return true;
10256 }
10257
10258 template<int size>
10259 static bool
10260 relative_value_is_known(const Sized_symbol<size>* gsym)
10261 {
10262 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10263 return false;
10264
10265 if (gsym->is_from_dynobj()
10266 || gsym->is_undefined()
10267 || gsym->is_preemptible())
10268 return false;
10269
10270 if (gsym->is_absolute())
10271 return !parameters->options().output_is_position_independent();
10272
10273 return true;
10274 }
10275
10276 template<int size>
10277 static bool
10278 relative_value_is_known(const Symbol_value<size>* psymval)
10279 {
10280 if (psymval->is_ifunc_symbol())
10281 return false;
10282
10283 bool is_ordinary;
10284 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10285
10286 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10287 }
10288
10289 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10290 // pld ra,symbol@got@pcrel
10291 // load/store rt,0(ra)
10292 // or
10293 // pla ra,symbol@pcrel
10294 // load/store rt,0(ra)
10295 // may be translated to
10296 // pload/pstore rt,symbol@pcrel
10297 // nop.
10298 // This function returns true if the optimization is possible, placing
10299 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10300 //
10301 // On entry to this function, the linker has already determined that
10302 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10303 // while *PINSN2 is the second instruction.
10304
10305 inline bool
10306 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10307 {
10308 uint32_t insn2 = *pinsn2 >> 32;
10309 uint64_t i1new;
10310
10311 // Check that regs match.
10312 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10313 return false;
10314
10315 switch ((insn2 >> 26) & 63)
10316 {
10317 default:
10318 return false;
10319
10320 case 32: // lwz
10321 case 34: // lbz
10322 case 36: // stw
10323 case 38: // stb
10324 case 40: // lhz
10325 case 42: // lha
10326 case 44: // sth
10327 case 48: // lfs
10328 case 50: // lfd
10329 case 52: // stfs
10330 case 54: // stfd
10331 // These are the PMLS cases, where we just need to tack a prefix
10332 // on the insn. Check that the D field is zero.
10333 if ((insn2 & 0xffff) != 0)
10334 return false;
10335 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10336 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10337 break;
10338
10339 case 58: // lwa, ld
10340 if ((insn2 & 0xfffd) != 0)
10341 return false;
10342 i1new = ((1ULL << 58) | (1ULL << 52)
10343 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10344 | (insn2 & (31ULL << 21)));
10345 break;
10346
10347 case 57: // lxsd, lxssp
10348 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10349 return false;
10350 i1new = ((1ULL << 58) | (1ULL << 52)
10351 | ((40ULL | (insn2 & 3)) << 26)
10352 | (insn2 & (31ULL << 21)));
10353 break;
10354
10355 case 61: // stxsd, stxssp, lxv, stxv
10356 if ((insn2 & 3) == 0)
10357 return false;
10358 else if ((insn2 & 3) >= 2)
10359 {
10360 if ((insn2 & 0xfffc) != 0)
10361 return false;
10362 i1new = ((1ULL << 58) | (1ULL << 52)
10363 | ((44ULL | (insn2 & 3)) << 26)
10364 | (insn2 & (31ULL << 21)));
10365 }
10366 else
10367 {
10368 if ((insn2 & 0xfff0) != 0)
10369 return false;
10370 i1new = ((1ULL << 58) | (1ULL << 52)
10371 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10372 | (insn2 & (31ULL << 21)));
10373 }
10374 break;
10375
10376 case 56: // lq
10377 if ((insn2 & 0xffff) != 0)
10378 return false;
10379 i1new = ((1ULL << 58) | (1ULL << 52)
10380 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10381 break;
10382
10383 case 62: // std, stq
10384 if ((insn2 & 0xfffd) != 0)
10385 return false;
10386 i1new = ((1ULL << 58) | (1ULL << 52)
10387 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10388 | (insn2 & (31ULL << 21)));
10389 break;
10390 }
10391
10392 *pinsn1 = i1new;
10393 *pinsn2 = (uint64_t) nop << 32;
10394 return true;
10395 }
10396
10397 // Perform a relocation.
10398
10399 template<int size, bool big_endian>
10400 inline bool
10401 Target_powerpc<size, big_endian>::Relocate::relocate(
10402 const Relocate_info<size, big_endian>* relinfo,
10403 unsigned int,
10404 Target_powerpc* target,
10405 Output_section* os,
10406 size_t relnum,
10407 const unsigned char* preloc,
10408 const Sized_symbol<size>* gsym,
10409 const Symbol_value<size>* psymval,
10410 unsigned char* view,
10411 Address address,
10412 section_size_type view_size)
10413 {
10414 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10415 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10416 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10417
10418 if (view == NULL)
10419 return true;
10420
10421 if (target->replace_tls_get_addr(gsym))
10422 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10423
10424 const elfcpp::Rela<size, big_endian> rela(preloc);
10425 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10426 Powerpc_relobj<size, big_endian>* const object
10427 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10428 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10429 {
10430 case Track_tls::NOT_EXPECTED:
10431 // No warning. This will result in really old code without tls
10432 // marker relocs being mis-optimised, but there shouldn't be too
10433 // much of that code around. The problem with warning is that
10434 // glibc and libphobos both construct direct calls to
10435 // __tls_get_addr in a way that is harmless.
10436 break;
10437 case Track_tls::EXPECTED:
10438 // We have already complained.
10439 break;
10440 case Track_tls::SKIP:
10441 if (is_plt16_reloc<size>(r_type)
10442 || r_type == elfcpp::R_POWERPC_PLTSEQ
10443 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10444 {
10445 Insn* iview = reinterpret_cast<Insn*>(view);
10446 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10447 }
10448 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10449 {
10450 Insn* iview = reinterpret_cast<Insn*>(view);
10451 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10452 }
10453 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10454 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10455 {
10456 Insn* iview = reinterpret_cast<Insn*>(view);
10457 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10458 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10459 }
10460 return true;
10461 case Track_tls::NORMAL:
10462 break;
10463 }
10464
10465 // Offset from start of insn to d-field reloc.
10466 const int d_offset = big_endian ? 2 : 0;
10467
10468 Address value = 0;
10469 bool has_stub_value = false;
10470 bool localentry0 = false;
10471 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10472 bool has_plt_offset
10473 = (gsym != NULL
10474 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10475 : object->local_has_plt_offset(r_sym));
10476 if (has_plt_offset
10477 && !is_got_reloc(r_type)
10478 && !is_plt16_reloc<size>(r_type)
10479 && r_type != elfcpp::R_PPC64_PLT_PCREL34
10480 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10481 && r_type != elfcpp::R_POWERPC_PLTSEQ
10482 && r_type != elfcpp::R_POWERPC_PLTCALL
10483 && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10484 && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
10485 && (!psymval->is_ifunc_symbol()
10486 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
10487 {
10488 if (size == 64
10489 && gsym != NULL
10490 && target->abiversion() >= 2
10491 && !parameters->options().output_is_position_independent()
10492 && !is_branch_reloc<size>(r_type))
10493 {
10494 Address off = target->glink_section()->find_global_entry(gsym);
10495 if (off != invalid_address)
10496 {
10497 value = target->glink_section()->global_entry_address() + off;
10498 has_stub_value = true;
10499 }
10500 }
10501 else
10502 {
10503 Stub_table<size, big_endian>* stub_table = NULL;
10504 if (target->stub_tables().size() == 1)
10505 stub_table = target->stub_tables()[0];
10506 if (stub_table == NULL
10507 && !(size == 32
10508 && gsym != NULL
10509 && !parameters->options().output_is_position_independent()
10510 && !is_branch_reloc<size>(r_type)))
10511 stub_table = object->stub_table(relinfo->data_shndx);
10512 if (stub_table == NULL)
10513 {
10514 // This is a ref from a data section to an ifunc symbol,
10515 // or a non-branch reloc for which we always want to use
10516 // one set of stubs for resolving function addresses.
10517 if (target->stub_tables().size() != 0)
10518 stub_table = target->stub_tables()[0];
10519 }
10520 if (stub_table != NULL)
10521 {
10522 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10523 if (gsym != NULL)
10524 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10525 rela.get_r_addend());
10526 else
10527 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10528 rela.get_r_addend());
10529 if (ent != NULL)
10530 {
10531 value = stub_table->stub_address() + ent->off_;
10532 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10533 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10534 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10535 if (size == 64
10536 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
10537 value += ent->tocoff_;
10538 if (size == 64
10539 && ent->r2save_
10540 && !(gsym != NULL
10541 && target->is_tls_get_addr_opt(gsym)))
10542 {
10543 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10544 {
10545 if (!(target->power10_stubs()
10546 && target->power10_stubs_auto()))
10547 value += 4;
10548 }
10549 else if (relnum < reloc_count - 1)
10550 {
10551 Reltype next_rela(preloc + reloc_size);
10552 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10553 == elfcpp::R_PPC64_TOCSAVE
10554 && (next_rela.get_r_offset()
10555 == rela.get_r_offset() + 4))
10556 value += 4;
10557 }
10558 }
10559 localentry0 = ent->localentry0_;
10560 has_stub_value = true;
10561 }
10562 }
10563 }
10564 // We don't care too much about bogus debug references to
10565 // non-local functions, but otherwise there had better be a plt
10566 // call stub or global entry stub as appropriate.
10567 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10568 }
10569
10570 if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10571 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10572 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10573 {
10574 const Output_data_plt_powerpc<size, big_endian>* plt;
10575 if (gsym)
10576 value = target->plt_off(gsym, &plt);
10577 else
10578 value = target->plt_off(object, r_sym, &plt);
10579 value += plt->address();
10580
10581 if (size == 64)
10582 {
10583 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10584 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10585 value -= target->toc_pointer();
10586 }
10587 else if (parameters->options().output_is_position_independent())
10588 {
10589 if (rela.get_r_addend() >= 32768)
10590 {
10591 unsigned int got2 = object->got2_shndx();
10592 value -= (object->get_output_section_offset(got2)
10593 + object->output_section(got2)->address()
10594 + rela.get_r_addend());
10595 }
10596 else
10597 value -= target->toc_pointer();
10598 }
10599 }
10600 else if (!has_plt_offset
10601 && (is_plt16_reloc<size>(r_type)
10602 || r_type == elfcpp::R_POWERPC_PLTSEQ
10603 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10604 {
10605 Insn* iview = reinterpret_cast<Insn*>(view);
10606 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10607 r_type = elfcpp::R_POWERPC_NONE;
10608 }
10609 else if (!has_plt_offset
10610 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10611 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10612 {
10613 Insn* iview = reinterpret_cast<Insn*>(view);
10614 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10615 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10616 r_type = elfcpp::R_POWERPC_NONE;
10617 }
10618 else if (is_got_reloc(r_type))
10619 {
10620 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10621 Got_type got_type = ((size == 32
10622 || r_type == elfcpp::R_POWERPC_GOT16
10623 || r_type == elfcpp::R_PPC64_GOT16_DS)
10624 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10625 if (gsym != NULL)
10626 value = gsym->got_offset(got_type, addend);
10627 else
10628 value = object->local_got_offset(r_sym, got_type, addend);
10629 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10630 value += target->got_section(got_type)->address();
10631 else
10632 value -= target->got_base_offset(got_type);
10633 }
10634 else if (r_type == elfcpp::R_PPC64_TOC)
10635 {
10636 value = target->toc_pointer();
10637 }
10638 else if (gsym != NULL
10639 && (r_type == elfcpp::R_POWERPC_REL24
10640 || r_type == elfcpp::R_PPC_PLTREL24)
10641 && has_stub_value)
10642 {
10643 if (size == 64)
10644 {
10645 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10646 Valtype* wv = reinterpret_cast<Valtype*>(view);
10647 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10648 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10649 {
10650 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10651 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10652 if ((insn & 1) != 0
10653 && (insn2 == nop
10654 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10655 {
10656 elfcpp::Swap<32, big_endian>::
10657 writeval(wv + 1, ld_2_1 + target->stk_toc());
10658 can_plt_call = true;
10659 }
10660 }
10661 if (!can_plt_call)
10662 {
10663 // If we don't have a branch and link followed by a nop,
10664 // we can't go via the plt because there is no place to
10665 // put a toc restoring instruction.
10666 // Unless we know we won't be returning.
10667 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10668 can_plt_call = true;
10669 }
10670 if (!can_plt_call)
10671 {
10672 // g++ as of 20130507 emits self-calls without a
10673 // following nop. This is arguably wrong since we have
10674 // conflicting information. On the one hand a global
10675 // symbol and on the other a local call sequence, but
10676 // don't error for this special case.
10677 // It isn't possible to cheaply verify we have exactly
10678 // such a call. Allow all calls to the same section.
10679 bool ok = false;
10680 Address code = value;
10681 if (gsym->source() == Symbol::FROM_OBJECT
10682 && gsym->object() == object)
10683 {
10684 unsigned int dest_shndx = 0;
10685 if (target->abiversion() < 2)
10686 {
10687 Address addend = rela.get_r_addend();
10688 code = psymval->value(object, addend);
10689 target->symval_for_branch(relinfo->symtab, gsym, object,
10690 &code, &dest_shndx);
10691 }
10692 bool is_ordinary;
10693 if (dest_shndx == 0)
10694 dest_shndx = gsym->shndx(&is_ordinary);
10695 ok = dest_shndx == relinfo->data_shndx;
10696 }
10697 if (!ok)
10698 {
10699 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10700 _("call lacks nop, can't restore toc; "
10701 "recompile with -fPIC"));
10702 value = code;
10703 }
10704 }
10705 }
10706 }
10707 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10708 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10709 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10710 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10711 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10712 {
10713 // First instruction of a global dynamic sequence, arg setup insn.
10714 bool final = gsym == NULL || gsym->final_value_is_known();
10715 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10716 Got_type got_type = ((size == 32
10717 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10718 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10719 if (tls_type == tls::TLSOPT_NONE)
10720 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
10721 else if (tls_type == tls::TLSOPT_TO_IE)
10722 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10723 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
10724 {
10725 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10726 if (gsym != NULL)
10727 value = gsym->got_offset(got_type, addend);
10728 else
10729 value = object->local_got_offset(r_sym, got_type, addend);
10730 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10731 value += target->got_section(got_type)->address();
10732 else
10733 value -= target->got_base_offset(got_type);
10734 }
10735 if (tls_type == tls::TLSOPT_TO_IE)
10736 {
10737 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10738 {
10739 Insn* iview = reinterpret_cast<Insn*>(view);
10740 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10741 pinsn <<= 32;
10742 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10743 // pla -> pld
10744 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10745 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10746 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10747 pinsn & 0xffffffff);
10748 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
10749 }
10750 else
10751 {
10752 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10753 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10754 {
10755 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10756 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10757 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10758 if (size == 32)
10759 insn |= 32 << 26; // lwz
10760 else
10761 insn |= 58 << 26; // ld
10762 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10763 }
10764 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10765 - elfcpp::R_POWERPC_GOT_TLSGD16);
10766 }
10767 }
10768 else if (tls_type == tls::TLSOPT_TO_LE)
10769 {
10770 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10771 {
10772 Insn* iview = reinterpret_cast<Insn*>(view);
10773 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10774 pinsn <<= 32;
10775 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10776 // pla pcrel -> paddi r13
10777 pinsn += (-1ULL << 52) + (13ULL << 16);
10778 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10779 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10780 pinsn & 0xffffffff);
10781 r_type = elfcpp::R_PPC64_TPREL34;
10782 value = psymval->value(object, rela.get_r_addend());
10783 }
10784 else
10785 {
10786 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10787 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10788 {
10789 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10790 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10791 insn &= (1 << 26) - (1 << 21); // extract rt
10792 if (size == 32)
10793 insn |= addis_0_2;
10794 else
10795 insn |= addis_0_13;
10796 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10797 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10798 value = psymval->value(object, rela.get_r_addend());
10799 }
10800 else
10801 {
10802 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10803 Insn insn = nop;
10804 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10805 r_type = elfcpp::R_POWERPC_NONE;
10806 }
10807 }
10808 }
10809 }
10810 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10811 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10812 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10813 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10814 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10815 {
10816 // First instruction of a local dynamic sequence, arg setup insn.
10817 tls::Tls_optimization tls_type = target->optimize_tls_ld();
10818 if (tls_type == tls::TLSOPT_NONE)
10819 {
10820 value = target->tlsld_got_offset();
10821 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10822 value += target->got_section(GOT_TYPE_SMALL)->address();
10823 else
10824 value -= target->got_base_offset(GOT_TYPE_SMALL);
10825 }
10826 else
10827 {
10828 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10829 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
10830 {
10831 Insn* iview = reinterpret_cast<Insn*>(view);
10832 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10833 pinsn <<= 32;
10834 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10835 // pla pcrel -> paddi r13
10836 pinsn += (-1ULL << 52) + (13ULL << 16);
10837 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10838 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10839 pinsn & 0xffffffff);
10840 r_type = elfcpp::R_PPC64_TPREL34;
10841 value = dtp_offset;
10842 }
10843 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10844 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10845 {
10846 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10847 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10848 insn &= (1 << 26) - (1 << 21); // extract rt
10849 if (size == 32)
10850 insn |= addis_0_2;
10851 else
10852 insn |= addis_0_13;
10853 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10854 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10855 value = dtp_offset;
10856 }
10857 else
10858 {
10859 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10860 Insn insn = nop;
10861 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10862 r_type = elfcpp::R_POWERPC_NONE;
10863 }
10864 }
10865 }
10866 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10867 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10868 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
10869 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10870 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10871 {
10872 // Accesses relative to a local dynamic sequence address,
10873 // no optimisation here.
10874 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10875 Got_type got_type = ((size == 32
10876 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
10877 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
10878 if (gsym != NULL)
10879 value = gsym->got_offset(got_type, addend);
10880 else
10881 value = object->local_got_offset(r_sym, got_type, addend);
10882 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
10883 value += target->got_section(got_type)->address();
10884 else
10885 value -= target->got_base_offset(got_type);
10886 }
10887 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10888 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10889 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10890 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10891 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10892 {
10893 // First instruction of initial exec sequence.
10894 bool final = gsym == NULL || gsym->final_value_is_known();
10895 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10896 if (tls_type == tls::TLSOPT_NONE)
10897 {
10898 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10899 Got_type got_type = ((size == 32
10900 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
10901 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
10902 if (gsym != NULL)
10903 value = gsym->got_offset(got_type, addend);
10904 else
10905 value = object->local_got_offset(r_sym, got_type, addend);
10906 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10907 value += target->got_section(got_type)->address();
10908 else
10909 value -= target->got_base_offset(got_type);
10910 }
10911 else
10912 {
10913 gold_assert(tls_type == tls::TLSOPT_TO_LE);
10914 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
10915 {
10916 Insn* iview = reinterpret_cast<Insn*>(view);
10917 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10918 pinsn <<= 32;
10919 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10920 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10921 pinsn += ((2ULL << 56) + (-1ULL << 52)
10922 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10923 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10924 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10925 pinsn & 0xffffffff);
10926 r_type = elfcpp::R_PPC64_TPREL34;
10927 value = psymval->value(object, rela.get_r_addend());
10928 }
10929 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10930 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10931 {
10932 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10933 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10934 insn &= (1 << 26) - (1 << 21); // extract rt from ld
10935 if (size == 32)
10936 insn |= addis_0_2;
10937 else
10938 insn |= addis_0_13;
10939 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10940 r_type = elfcpp::R_POWERPC_TPREL16_HA;
10941 value = psymval->value(object, rela.get_r_addend());
10942 }
10943 else
10944 {
10945 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10946 Insn insn = nop;
10947 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10948 r_type = elfcpp::R_POWERPC_NONE;
10949 }
10950 }
10951 }
10952 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10953 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10954 {
10955 // Second instruction of a global dynamic sequence,
10956 // the __tls_get_addr call
10957 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10958 bool final = gsym == NULL || gsym->final_value_is_known();
10959 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10960 if (tls_type != tls::TLSOPT_NONE)
10961 {
10962 if (tls_type == tls::TLSOPT_TO_IE)
10963 {
10964 Insn* iview = reinterpret_cast<Insn*>(view);
10965 Insn insn = add_3_3_13;
10966 if (size == 32)
10967 insn = add_3_3_2;
10968 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10969 r_type = elfcpp::R_POWERPC_NONE;
10970 }
10971 else
10972 {
10973 bool is_pcrel = false;
10974 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10975 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10976 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10977 if (relnum < reloc_count - 1)
10978 {
10979 Reltype next_rela(preloc + reloc_size);
10980 unsigned int r_type2
10981 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10982 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10983 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10984 && next_rela.get_r_offset() == rela.get_r_offset())
10985 is_pcrel = true;
10986 }
10987 Insn* iview = reinterpret_cast<Insn*>(view);
10988 if (is_pcrel)
10989 {
10990 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10991 r_type = elfcpp::R_POWERPC_NONE;
10992 }
10993 else
10994 {
10995 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10996 r_type = elfcpp::R_POWERPC_TPREL16_LO;
10997 view += d_offset;
10998 value = psymval->value(object, rela.get_r_addend());
10999 }
11000 }
11001 this->skip_next_tls_get_addr_call();
11002 }
11003 }
11004 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11005 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11006 {
11007 // Second instruction of a local dynamic sequence,
11008 // the __tls_get_addr call
11009 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11010 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11011 if (tls_type == tls::TLSOPT_TO_LE)
11012 {
11013 bool is_pcrel = false;
11014 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11015 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11016 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11017 if (relnum < reloc_count - 1)
11018 {
11019 Reltype next_rela(preloc + reloc_size);
11020 unsigned int r_type2
11021 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11022 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11023 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11024 && next_rela.get_r_offset() == rela.get_r_offset())
11025 is_pcrel = true;
11026 }
11027 Insn* iview = reinterpret_cast<Insn*>(view);
11028 if (is_pcrel)
11029 {
11030 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11031 r_type = elfcpp::R_POWERPC_NONE;
11032 }
11033 else
11034 {
11035 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11036 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11037 view += d_offset;
11038 value = dtp_offset;
11039 }
11040 this->skip_next_tls_get_addr_call();
11041 }
11042 }
11043 else if (r_type == elfcpp::R_POWERPC_TLS)
11044 {
11045 // Second instruction of an initial exec sequence
11046 bool final = gsym == NULL || gsym->final_value_is_known();
11047 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11048 if (tls_type == tls::TLSOPT_TO_LE)
11049 {
11050 Address roff = rela.get_r_offset() & 3;
11051 Insn* iview = reinterpret_cast<Insn*>(view - roff);
11052 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11053 unsigned int reg = size == 32 ? 2 : 13;
11054 insn = at_tls_transform(insn, reg);
11055 gold_assert(insn != 0);
11056 if (roff == 0)
11057 {
11058 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11059 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11060 view += d_offset;
11061 value = psymval->value(object, rela.get_r_addend());
11062 }
11063 else if (roff == 1)
11064 {
11065 // For pcrel IE to LE we already have the full offset
11066 // and thus don't need an addi here. A nop or mr will do.
11067 if ((insn & (0x3f << 26)) == 14 << 26)
11068 {
11069 // Extract regs from addi rt,ra,si.
11070 unsigned int rt = (insn >> 21) & 0x1f;
11071 unsigned int ra = (insn >> 16) & 0x1f;
11072 if (rt == ra)
11073 insn = nop;
11074 else
11075 {
11076 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11077 insn = (rt << 16) | (ra << 21) | (ra << 11);
11078 insn |= (31u << 26) | (444u << 1);
11079 }
11080 }
11081 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11082 r_type = elfcpp::R_POWERPC_NONE;
11083 }
11084 }
11085 }
11086 else if (!has_stub_value)
11087 {
11088 if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11089 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11090 {
11091 // PLTCALL without plt entry => convert to direct call
11092 Insn* iview = reinterpret_cast<Insn*>(view);
11093 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11094 insn = (insn & 1) | b;
11095 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11096 if (size == 32)
11097 r_type = elfcpp::R_PPC_PLTREL24;
11098 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11099 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11100 else
11101 r_type = elfcpp::R_POWERPC_REL24;
11102 }
11103 Address addend = 0;
11104 if (!(size == 32
11105 && (r_type == elfcpp::R_PPC_PLTREL24
11106 || r_type == elfcpp::R_POWERPC_PLT16_LO
11107 || r_type == elfcpp::R_POWERPC_PLT16_HI
11108 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11109 addend = rela.get_r_addend();
11110 value = psymval->value(object, addend);
11111 unsigned int local_ent = 0;
11112 if (size == 64 && is_branch_reloc<size>(r_type))
11113 {
11114 if (target->abiversion() >= 2)
11115 {
11116 if (gsym != NULL)
11117 local_ent = object->ppc64_local_entry_offset(gsym);
11118 else
11119 local_ent = object->ppc64_local_entry_offset(r_sym);
11120 }
11121 else
11122 {
11123 unsigned int dest_shndx;
11124 target->symval_for_branch(relinfo->symtab, gsym, object,
11125 &value, &dest_shndx);
11126 }
11127 }
11128 Address max_branch = max_branch_delta<size>(r_type);
11129 if (max_branch != 0
11130 && (value + local_ent - address + max_branch >= 2 * max_branch
11131 || (size == 64
11132 && r_type == elfcpp::R_PPC64_REL24_NOTOC
11133 && (gsym != NULL
11134 ? object->ppc64_needs_toc(gsym)
11135 : object->ppc64_needs_toc(r_sym)))))
11136 {
11137 Stub_table<size, big_endian>* stub_table
11138 = object->stub_table(relinfo->data_shndx);
11139 if (stub_table != NULL)
11140 {
11141 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11142 = stub_table->find_long_branch_entry(value);
11143 if (ent != NULL)
11144 {
11145 if (ent->save_res_)
11146 value = (value - target->savres_section()->address()
11147 + stub_table->stub_address()
11148 + stub_table->plt_size()
11149 + stub_table->branch_size());
11150 else
11151 {
11152 value = (stub_table->stub_address()
11153 + stub_table->plt_size()
11154 + ent->off_);
11155 if (size == 64
11156 && r_type != elfcpp::R_PPC64_REL24_NOTOC)
11157 value += ent->tocoff_;
11158 }
11159 has_stub_value = true;
11160 }
11161 }
11162 }
11163 if (!has_stub_value)
11164 value += local_ent;
11165 }
11166
11167 switch (r_type)
11168 {
11169 case elfcpp::R_PPC64_REL24_NOTOC:
11170 if (size == 32)
11171 break;
11172 // Fall through.
11173 case elfcpp::R_PPC64_REL64:
11174 case elfcpp::R_POWERPC_REL32:
11175 case elfcpp::R_POWERPC_REL24:
11176 case elfcpp::R_PPC_PLTREL24:
11177 case elfcpp::R_PPC_LOCAL24PC:
11178 case elfcpp::R_POWERPC_REL16:
11179 case elfcpp::R_POWERPC_REL16_LO:
11180 case elfcpp::R_POWERPC_REL16_HI:
11181 case elfcpp::R_POWERPC_REL16_HA:
11182 case elfcpp::R_POWERPC_REL16DX_HA:
11183 case elfcpp::R_PPC64_REL16_HIGH:
11184 case elfcpp::R_PPC64_REL16_HIGHA:
11185 case elfcpp::R_PPC64_REL16_HIGHER:
11186 case elfcpp::R_PPC64_REL16_HIGHERA:
11187 case elfcpp::R_PPC64_REL16_HIGHEST:
11188 case elfcpp::R_PPC64_REL16_HIGHESTA:
11189 case elfcpp::R_POWERPC_REL14:
11190 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11191 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11192 case elfcpp::R_PPC64_PCREL34:
11193 case elfcpp::R_PPC64_GOT_PCREL34:
11194 case elfcpp::R_PPC64_PLT_PCREL34:
11195 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11196 case elfcpp::R_PPC64_PCREL28:
11197 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11198 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11199 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11200 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11201 case elfcpp::R_PPC64_REL16_HIGHER34:
11202 case elfcpp::R_PPC64_REL16_HIGHERA34:
11203 case elfcpp::R_PPC64_REL16_HIGHEST34:
11204 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11205 value -= address;
11206 break;
11207
11208 case elfcpp::R_PPC64_TOC16:
11209 case elfcpp::R_PPC64_TOC16_LO:
11210 case elfcpp::R_PPC64_TOC16_HI:
11211 case elfcpp::R_PPC64_TOC16_HA:
11212 case elfcpp::R_PPC64_TOC16_DS:
11213 case elfcpp::R_PPC64_TOC16_LO_DS:
11214 // Subtract the TOC base address.
11215 value -= target->toc_pointer();
11216 break;
11217
11218 case elfcpp::R_POWERPC_SECTOFF:
11219 case elfcpp::R_POWERPC_SECTOFF_LO:
11220 case elfcpp::R_POWERPC_SECTOFF_HI:
11221 case elfcpp::R_POWERPC_SECTOFF_HA:
11222 case elfcpp::R_PPC64_SECTOFF_DS:
11223 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11224 if (os != NULL)
11225 value -= os->address();
11226 break;
11227
11228 case elfcpp::R_PPC64_TPREL16_DS:
11229 case elfcpp::R_PPC64_TPREL16_LO_DS:
11230 case elfcpp::R_PPC64_TPREL16_HIGH:
11231 case elfcpp::R_PPC64_TPREL16_HIGHA:
11232 if (size != 64)
11233 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11234 break;
11235 // Fall through.
11236 case elfcpp::R_POWERPC_TPREL16:
11237 case elfcpp::R_POWERPC_TPREL16_LO:
11238 case elfcpp::R_POWERPC_TPREL16_HI:
11239 case elfcpp::R_POWERPC_TPREL16_HA:
11240 case elfcpp::R_POWERPC_TPREL:
11241 case elfcpp::R_PPC64_TPREL16_HIGHER:
11242 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11243 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11244 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11245 case elfcpp::R_PPC64_TPREL34:
11246 // tls symbol values are relative to tls_segment()->vaddr()
11247 value -= tp_offset;
11248 break;
11249
11250 case elfcpp::R_PPC64_DTPREL16_DS:
11251 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11252 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11253 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11254 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11255 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11256 if (size != 64)
11257 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11258 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11259 break;
11260 // Fall through.
11261 case elfcpp::R_POWERPC_DTPREL16:
11262 case elfcpp::R_POWERPC_DTPREL16_LO:
11263 case elfcpp::R_POWERPC_DTPREL16_HI:
11264 case elfcpp::R_POWERPC_DTPREL16_HA:
11265 case elfcpp::R_POWERPC_DTPREL:
11266 case elfcpp::R_PPC64_DTPREL16_HIGH:
11267 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11268 case elfcpp::R_PPC64_DTPREL34:
11269 // tls symbol values are relative to tls_segment()->vaddr()
11270 value -= dtp_offset;
11271 break;
11272
11273 case elfcpp::R_PPC64_ADDR64_LOCAL:
11274 if (gsym != NULL)
11275 value += object->ppc64_local_entry_offset(gsym);
11276 else
11277 value += object->ppc64_local_entry_offset(r_sym);
11278 break;
11279
11280 default:
11281 break;
11282 }
11283
11284 Insn branch_bit = 0;
11285 switch (r_type)
11286 {
11287 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11288 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11289 branch_bit = 1 << 21;
11290 // Fall through.
11291 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11292 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11293 {
11294 Insn* iview = reinterpret_cast<Insn*>(view);
11295 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11296 insn &= ~(1 << 21);
11297 insn |= branch_bit;
11298 if (this->is_isa_v2)
11299 {
11300 // Set 'a' bit. This is 0b00010 in BO field for branch
11301 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11302 // for branch on CTR insns (BO == 1a00t or 1a01t).
11303 if ((insn & (0x14 << 21)) == (0x04 << 21))
11304 insn |= 0x02 << 21;
11305 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11306 insn |= 0x08 << 21;
11307 else
11308 break;
11309 }
11310 else
11311 {
11312 // Invert 'y' bit if not the default.
11313 if (static_cast<Signed_address>(value) < 0)
11314 insn ^= 1 << 21;
11315 }
11316 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11317 }
11318 break;
11319
11320 case elfcpp::R_POWERPC_PLT16_HA:
11321 if (size == 32
11322 && !parameters->options().output_is_position_independent())
11323 {
11324 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11325 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11326
11327 // Convert addis to lis.
11328 if ((insn & (0x3f << 26)) == 15u << 26
11329 && (insn & (0x1f << 16)) != 0)
11330 {
11331 insn &= ~(0x1f << 16);
11332 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11333 }
11334 }
11335 break;
11336
11337 default:
11338 break;
11339 }
11340
11341 if (gsym
11342 ? relative_value_is_known(gsym)
11343 : relative_value_is_known(psymval))
11344 {
11345 Insn* iview;
11346 Insn* iview2;
11347 Insn insn;
11348 uint64_t pinsn, pinsn2;
11349
11350 switch (r_type)
11351 {
11352 default:
11353 break;
11354
11355 // Multi-instruction sequences that access the GOT/TOC can
11356 // be optimized, eg.
11357 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11358 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11359 // and
11360 // addis ra,r2,0; addi rb,ra,x@toc@l;
11361 // to nop; addi rb,r2,x@toc;
11362 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11363 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11364 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11365 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11366 case elfcpp::R_POWERPC_GOT16_HA:
11367 case elfcpp::R_PPC64_TOC16_HA:
11368 if (size == 64 && parameters->options().toc_optimize())
11369 {
11370 iview = reinterpret_cast<Insn*>(view - d_offset);
11371 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11372 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11373 && object->make_toc_relative(target, &value))
11374 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11375 && object->make_got_relative(target, psymval,
11376 rela.get_r_addend(),
11377 &value)))
11378 {
11379 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11380 == ((15u << 26) | (2 << 16)));
11381 }
11382 if (((insn & ((0x3f << 26) | 0x1f << 16))
11383 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11384 && value + 0x8000 < 0x10000)
11385 {
11386 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11387 return true;
11388 }
11389 }
11390 break;
11391
11392 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11393 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11394 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11395 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11396 case elfcpp::R_POWERPC_GOT16_LO:
11397 case elfcpp::R_PPC64_GOT16_LO_DS:
11398 case elfcpp::R_PPC64_TOC16_LO:
11399 case elfcpp::R_PPC64_TOC16_LO_DS:
11400 if (size == 64 && parameters->options().toc_optimize())
11401 {
11402 iview = reinterpret_cast<Insn*>(view - d_offset);
11403 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11404 bool changed = false;
11405 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11406 && object->make_toc_relative(target, &value))
11407 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11408 && object->make_got_relative(target, psymval,
11409 rela.get_r_addend(),
11410 &value)))
11411 {
11412 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11413 insn ^= (14u << 26) ^ (58u << 26);
11414 r_type = elfcpp::R_PPC64_TOC16_LO;
11415 changed = true;
11416 }
11417 if (ok_lo_toc_insn(insn, r_type)
11418 && value + 0x8000 < 0x10000)
11419 {
11420 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11421 {
11422 // Transform addic to addi when we change reg.
11423 insn &= ~((0x3f << 26) | (0x1f << 16));
11424 insn |= (14u << 26) | (2 << 16);
11425 }
11426 else
11427 {
11428 insn &= ~(0x1f << 16);
11429 insn |= 2 << 16;
11430 }
11431 changed = true;
11432 }
11433 if (changed)
11434 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11435 }
11436 break;
11437
11438 case elfcpp::R_PPC64_GOT_PCREL34:
11439 if (size == 64 && parameters->options().toc_optimize())
11440 {
11441 iview = reinterpret_cast<Insn*>(view);
11442 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11443 pinsn <<= 32;
11444 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11445 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11446 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11447 break;
11448
11449 Address relval = psymval->value(object, rela.get_r_addend());
11450 relval -= address;
11451 if (relval + (1ULL << 33) < 1ULL << 34)
11452 {
11453 value = relval;
11454 // Replace with paddi
11455 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11456 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11457 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11458 pinsn & 0xffffffff);
11459 goto pcrelopt;
11460 }
11461 }
11462 break;
11463
11464 case elfcpp::R_PPC64_PCREL34:
11465 if (size == 64)
11466 {
11467 iview = reinterpret_cast<Insn*>(view);
11468 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11469 pinsn <<= 32;
11470 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11471 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11472 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11473 | (14ULL << 26) /* paddi */))
11474 break;
11475
11476 pcrelopt:
11477 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11478 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11479 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11480 if (relnum >= reloc_count - 1)
11481 break;
11482
11483 Reltype next_rela(preloc + reloc_size);
11484 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11485 != elfcpp::R_PPC64_PCREL_OPT)
11486 || next_rela.get_r_offset() != rela.get_r_offset())
11487 break;
11488
11489 Address off = next_rela.get_r_addend();
11490 if (off == 0)
11491 off = 8; // zero means next insn.
11492 if (off + rela.get_r_offset() + 4 > view_size)
11493 break;
11494
11495 iview2 = reinterpret_cast<Insn*>(view + off);
11496 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11497 pinsn2 <<= 32;
11498 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11499 break;
11500 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11501 {
11502 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11503 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11504 pinsn & 0xffffffff);
11505 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11506 }
11507 }
11508 break;
11509
11510 case elfcpp::R_POWERPC_TPREL16_HA:
11511 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11512 {
11513 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11514 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11515 return true;
11516 }
11517 break;
11518
11519 case elfcpp::R_PPC64_TPREL16_LO_DS:
11520 if (size == 32)
11521 // R_PPC_TLSGD, R_PPC_TLSLD
11522 break;
11523 // Fall through.
11524 case elfcpp::R_POWERPC_TPREL16_LO:
11525 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11526 {
11527 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11528 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11529 insn &= ~(0x1f << 16);
11530 insn |= (size == 32 ? 2 : 13) << 16;
11531 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11532 }
11533 break;
11534
11535 case elfcpp::R_PPC64_ENTRY:
11536 if (size == 64)
11537 {
11538 value = target->toc_pointer();
11539 if (value + 0x80008000 <= 0xffffffff
11540 && !parameters->options().output_is_position_independent())
11541 {
11542 Insn* iview = reinterpret_cast<Insn*>(view);
11543 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11544 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11545
11546 if ((insn1 & ~0xfffc) == ld_2_12
11547 && insn2 == add_2_2_12)
11548 {
11549 insn1 = lis_2 + ha(value);
11550 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11551 insn2 = addi_2_2 + l(value);
11552 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11553 return true;
11554 }
11555 }
11556 else
11557 {
11558 value -= address;
11559 if (value + 0x80008000 <= 0xffffffff)
11560 {
11561 Insn* iview = reinterpret_cast<Insn*>(view);
11562 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11563 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11564
11565 if ((insn1 & ~0xfffc) == ld_2_12
11566 && insn2 == add_2_2_12)
11567 {
11568 insn1 = addis_2_12 + ha(value);
11569 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11570 insn2 = addi_2_2 + l(value);
11571 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11572 return true;
11573 }
11574 }
11575 }
11576 }
11577 break;
11578
11579 case elfcpp::R_POWERPC_REL16_LO:
11580 // If we are generating a non-PIC executable, edit
11581 // 0: addis 2,12,.TOC.-0b@ha
11582 // addi 2,2,.TOC.-0b@l
11583 // used by ELFv2 global entry points to set up r2, to
11584 // lis 2,.TOC.@ha
11585 // addi 2,2,.TOC.@l
11586 // if .TOC. is in range. */
11587 if (size == 64
11588 && value + address - 4 + 0x80008000 <= 0xffffffff
11589 && relnum + 1 > 1
11590 && preloc != NULL
11591 && target->abiversion() >= 2
11592 && !parameters->options().output_is_position_independent()
11593 && rela.get_r_addend() == d_offset + 4
11594 && gsym != NULL
11595 && strcmp(gsym->name(), ".TOC.") == 0)
11596 {
11597 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11598 Reltype prev_rela(preloc - reloc_size);
11599 if ((prev_rela.get_r_info()
11600 == elfcpp::elf_r_info<size>(r_sym,
11601 elfcpp::R_POWERPC_REL16_HA))
11602 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11603 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11604 {
11605 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11606 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11607 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11608
11609 if ((insn1 & 0xffff0000) == addis_2_12
11610 && (insn2 & 0xffff0000) == addi_2_2)
11611 {
11612 insn1 = lis_2 + ha(value + address - 4);
11613 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11614 insn2 = addi_2_2 + l(value + address - 4);
11615 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11616 if (relinfo->rr)
11617 {
11618 relinfo->rr->set_strategy(relnum - 1,
11619 Relocatable_relocs::RELOC_SPECIAL);
11620 relinfo->rr->set_strategy(relnum,
11621 Relocatable_relocs::RELOC_SPECIAL);
11622 }
11623 return true;
11624 }
11625 }
11626 }
11627 break;
11628 }
11629 }
11630
11631 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11632 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11633 switch (r_type)
11634 {
11635 case elfcpp::R_POWERPC_ADDR32:
11636 case elfcpp::R_POWERPC_UADDR32:
11637 if (size == 64)
11638 overflow = Reloc::CHECK_BITFIELD;
11639 break;
11640
11641 case elfcpp::R_POWERPC_REL32:
11642 case elfcpp::R_POWERPC_REL16DX_HA:
11643 if (size == 64)
11644 overflow = Reloc::CHECK_SIGNED;
11645 break;
11646
11647 case elfcpp::R_POWERPC_UADDR16:
11648 overflow = Reloc::CHECK_BITFIELD;
11649 break;
11650
11651 case elfcpp::R_POWERPC_ADDR16:
11652 // We really should have three separate relocations,
11653 // one for 16-bit data, one for insns with 16-bit signed fields,
11654 // and one for insns with 16-bit unsigned fields.
11655 overflow = Reloc::CHECK_BITFIELD;
11656 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11657 overflow = Reloc::CHECK_LOW_INSN;
11658 break;
11659
11660 case elfcpp::R_POWERPC_ADDR16_HI:
11661 case elfcpp::R_POWERPC_ADDR16_HA:
11662 case elfcpp::R_POWERPC_GOT16_HI:
11663 case elfcpp::R_POWERPC_GOT16_HA:
11664 case elfcpp::R_POWERPC_PLT16_HI:
11665 case elfcpp::R_POWERPC_PLT16_HA:
11666 case elfcpp::R_POWERPC_SECTOFF_HI:
11667 case elfcpp::R_POWERPC_SECTOFF_HA:
11668 case elfcpp::R_PPC64_TOC16_HI:
11669 case elfcpp::R_PPC64_TOC16_HA:
11670 case elfcpp::R_PPC64_PLTGOT16_HI:
11671 case elfcpp::R_PPC64_PLTGOT16_HA:
11672 case elfcpp::R_POWERPC_TPREL16_HI:
11673 case elfcpp::R_POWERPC_TPREL16_HA:
11674 case elfcpp::R_POWERPC_DTPREL16_HI:
11675 case elfcpp::R_POWERPC_DTPREL16_HA:
11676 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11677 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11678 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11679 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11680 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11681 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11682 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11683 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11684 case elfcpp::R_POWERPC_REL16_HI:
11685 case elfcpp::R_POWERPC_REL16_HA:
11686 if (size != 32)
11687 overflow = Reloc::CHECK_HIGH_INSN;
11688 break;
11689
11690 case elfcpp::R_POWERPC_REL16:
11691 case elfcpp::R_PPC64_TOC16:
11692 case elfcpp::R_POWERPC_GOT16:
11693 case elfcpp::R_POWERPC_SECTOFF:
11694 case elfcpp::R_POWERPC_TPREL16:
11695 case elfcpp::R_POWERPC_DTPREL16:
11696 case elfcpp::R_POWERPC_GOT_TLSGD16:
11697 case elfcpp::R_POWERPC_GOT_TLSLD16:
11698 case elfcpp::R_POWERPC_GOT_TPREL16:
11699 case elfcpp::R_POWERPC_GOT_DTPREL16:
11700 overflow = Reloc::CHECK_LOW_INSN;
11701 break;
11702
11703 case elfcpp::R_PPC64_REL24_NOTOC:
11704 if (size == 32)
11705 break;
11706 // Fall through.
11707 case elfcpp::R_POWERPC_ADDR24:
11708 case elfcpp::R_POWERPC_ADDR14:
11709 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11710 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11711 case elfcpp::R_PPC64_ADDR16_DS:
11712 case elfcpp::R_POWERPC_REL24:
11713 case elfcpp::R_PPC_PLTREL24:
11714 case elfcpp::R_PPC_LOCAL24PC:
11715 case elfcpp::R_PPC64_TPREL16_DS:
11716 case elfcpp::R_PPC64_DTPREL16_DS:
11717 case elfcpp::R_PPC64_TOC16_DS:
11718 case elfcpp::R_PPC64_GOT16_DS:
11719 case elfcpp::R_PPC64_SECTOFF_DS:
11720 case elfcpp::R_POWERPC_REL14:
11721 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11722 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11723 case elfcpp::R_PPC64_D34:
11724 case elfcpp::R_PPC64_PCREL34:
11725 case elfcpp::R_PPC64_GOT_PCREL34:
11726 case elfcpp::R_PPC64_PLT_PCREL34:
11727 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11728 case elfcpp::R_PPC64_D28:
11729 case elfcpp::R_PPC64_PCREL28:
11730 case elfcpp::R_PPC64_TPREL34:
11731 case elfcpp::R_PPC64_DTPREL34:
11732 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11733 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11734 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11735 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11736 overflow = Reloc::CHECK_SIGNED;
11737 break;
11738 }
11739
11740 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11741 Insn insn = 0;
11742
11743 if (overflow == Reloc::CHECK_LOW_INSN
11744 || overflow == Reloc::CHECK_HIGH_INSN)
11745 {
11746 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11747
11748 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11749 overflow = Reloc::CHECK_BITFIELD;
11750 else if (overflow == Reloc::CHECK_LOW_INSN
11751 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11752 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11753 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11754 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11755 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11756 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
11757 overflow = Reloc::CHECK_UNSIGNED;
11758 else
11759 overflow = Reloc::CHECK_SIGNED;
11760 }
11761
11762 bool maybe_dq_reloc = false;
11763 typename Powerpc_relocate_functions<size, big_endian>::Status status
11764 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
11765 switch (r_type)
11766 {
11767 case elfcpp::R_POWERPC_NONE:
11768 case elfcpp::R_POWERPC_TLS:
11769 case elfcpp::R_POWERPC_GNU_VTINHERIT:
11770 case elfcpp::R_POWERPC_GNU_VTENTRY:
11771 case elfcpp::R_POWERPC_PLTSEQ:
11772 case elfcpp::R_POWERPC_PLTCALL:
11773 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11774 case elfcpp::R_PPC64_PLTCALL_NOTOC:
11775 case elfcpp::R_PPC64_PCREL_OPT:
11776 break;
11777
11778 case elfcpp::R_PPC64_ADDR64:
11779 case elfcpp::R_PPC64_REL64:
11780 case elfcpp::R_PPC64_TOC:
11781 case elfcpp::R_PPC64_ADDR64_LOCAL:
11782 Reloc::addr64(view, value);
11783 break;
11784
11785 case elfcpp::R_POWERPC_TPREL:
11786 case elfcpp::R_POWERPC_DTPREL:
11787 if (size == 64)
11788 Reloc::addr64(view, value);
11789 else
11790 status = Reloc::addr32(view, value, overflow);
11791 break;
11792
11793 case elfcpp::R_PPC64_UADDR64:
11794 Reloc::addr64_u(view, value);
11795 break;
11796
11797 case elfcpp::R_POWERPC_ADDR32:
11798 status = Reloc::addr32(view, value, overflow);
11799 break;
11800
11801 case elfcpp::R_POWERPC_REL32:
11802 case elfcpp::R_POWERPC_UADDR32:
11803 status = Reloc::addr32_u(view, value, overflow);
11804 break;
11805
11806 case elfcpp::R_PPC64_REL24_NOTOC:
11807 if (size == 32)
11808 goto unsupp; // R_PPC_EMB_RELSDA
11809 // Fall through.
11810 case elfcpp::R_POWERPC_ADDR24:
11811 case elfcpp::R_POWERPC_REL24:
11812 case elfcpp::R_PPC_PLTREL24:
11813 case elfcpp::R_PPC_LOCAL24PC:
11814 status = Reloc::addr24(view, value, overflow);
11815 break;
11816
11817 case elfcpp::R_POWERPC_GOT_DTPREL16:
11818 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11819 case elfcpp::R_POWERPC_GOT_TPREL16:
11820 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11821 if (size == 64)
11822 {
11823 // On ppc64 these are all ds form
11824 maybe_dq_reloc = true;
11825 break;
11826 }
11827 // Fall through.
11828 case elfcpp::R_POWERPC_ADDR16:
11829 case elfcpp::R_POWERPC_REL16:
11830 case elfcpp::R_PPC64_TOC16:
11831 case elfcpp::R_POWERPC_GOT16:
11832 case elfcpp::R_POWERPC_SECTOFF:
11833 case elfcpp::R_POWERPC_TPREL16:
11834 case elfcpp::R_POWERPC_DTPREL16:
11835 case elfcpp::R_POWERPC_GOT_TLSGD16:
11836 case elfcpp::R_POWERPC_GOT_TLSLD16:
11837 case elfcpp::R_POWERPC_ADDR16_LO:
11838 case elfcpp::R_POWERPC_REL16_LO:
11839 case elfcpp::R_PPC64_TOC16_LO:
11840 case elfcpp::R_POWERPC_GOT16_LO:
11841 case elfcpp::R_POWERPC_PLT16_LO:
11842 case elfcpp::R_POWERPC_SECTOFF_LO:
11843 case elfcpp::R_POWERPC_TPREL16_LO:
11844 case elfcpp::R_POWERPC_DTPREL16_LO:
11845 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11846 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11847 if (size == 64)
11848 status = Reloc::addr16(view, value, overflow);
11849 else
11850 maybe_dq_reloc = true;
11851 break;
11852
11853 case elfcpp::R_POWERPC_UADDR16:
11854 status = Reloc::addr16_u(view, value, overflow);
11855 break;
11856
11857 case elfcpp::R_PPC64_ADDR16_HIGH:
11858 case elfcpp::R_PPC64_TPREL16_HIGH:
11859 case elfcpp::R_PPC64_DTPREL16_HIGH:
11860 if (size == 32)
11861 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11862 goto unsupp;
11863 // Fall through.
11864 case elfcpp::R_POWERPC_ADDR16_HI:
11865 case elfcpp::R_POWERPC_REL16_HI:
11866 case elfcpp::R_PPC64_REL16_HIGH:
11867 case elfcpp::R_PPC64_TOC16_HI:
11868 case elfcpp::R_POWERPC_GOT16_HI:
11869 case elfcpp::R_POWERPC_PLT16_HI:
11870 case elfcpp::R_POWERPC_SECTOFF_HI:
11871 case elfcpp::R_POWERPC_TPREL16_HI:
11872 case elfcpp::R_POWERPC_DTPREL16_HI:
11873 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11874 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11875 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11876 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11877 Reloc::addr16_hi(view, value);
11878 break;
11879
11880 case elfcpp::R_PPC64_ADDR16_HIGHA:
11881 case elfcpp::R_PPC64_TPREL16_HIGHA:
11882 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11883 if (size == 32)
11884 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11885 goto unsupp;
11886 // Fall through.
11887 case elfcpp::R_POWERPC_ADDR16_HA:
11888 case elfcpp::R_POWERPC_REL16_HA:
11889 case elfcpp::R_PPC64_REL16_HIGHA:
11890 case elfcpp::R_PPC64_TOC16_HA:
11891 case elfcpp::R_POWERPC_GOT16_HA:
11892 case elfcpp::R_POWERPC_PLT16_HA:
11893 case elfcpp::R_POWERPC_SECTOFF_HA:
11894 case elfcpp::R_POWERPC_TPREL16_HA:
11895 case elfcpp::R_POWERPC_DTPREL16_HA:
11896 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11897 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11898 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11899 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11900 Reloc::addr16_ha(view, value);
11901 break;
11902
11903 case elfcpp::R_POWERPC_REL16DX_HA:
11904 status = Reloc::addr16dx_ha(view, value, overflow);
11905 break;
11906
11907 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11908 if (size == 32)
11909 // R_PPC_EMB_NADDR16_LO
11910 goto unsupp;
11911 // Fall through.
11912 case elfcpp::R_PPC64_ADDR16_HIGHER:
11913 case elfcpp::R_PPC64_REL16_HIGHER:
11914 case elfcpp::R_PPC64_TPREL16_HIGHER:
11915 Reloc::addr16_hi2(view, value);
11916 break;
11917
11918 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11919 if (size == 32)
11920 // R_PPC_EMB_NADDR16_HI
11921 goto unsupp;
11922 // Fall through.
11923 case elfcpp::R_PPC64_ADDR16_HIGHERA:
11924 case elfcpp::R_PPC64_REL16_HIGHERA:
11925 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11926 Reloc::addr16_ha2(view, value);
11927 break;
11928
11929 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11930 if (size == 32)
11931 // R_PPC_EMB_NADDR16_HA
11932 goto unsupp;
11933 // Fall through.
11934 case elfcpp::R_PPC64_ADDR16_HIGHEST:
11935 case elfcpp::R_PPC64_REL16_HIGHEST:
11936 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11937 Reloc::addr16_hi3(view, value);
11938 break;
11939
11940 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11941 if (size == 32)
11942 // R_PPC_EMB_SDAI16
11943 goto unsupp;
11944 // Fall through.
11945 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
11946 case elfcpp::R_PPC64_REL16_HIGHESTA:
11947 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11948 Reloc::addr16_ha3(view, value);
11949 break;
11950
11951 case elfcpp::R_PPC64_DTPREL16_DS:
11952 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11953 if (size == 32)
11954 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11955 goto unsupp;
11956 // Fall through.
11957 case elfcpp::R_PPC64_TPREL16_DS:
11958 case elfcpp::R_PPC64_TPREL16_LO_DS:
11959 if (size == 32)
11960 // R_PPC_TLSGD, R_PPC_TLSLD
11961 break;
11962 // Fall through.
11963 case elfcpp::R_PPC64_ADDR16_DS:
11964 case elfcpp::R_PPC64_ADDR16_LO_DS:
11965 case elfcpp::R_PPC64_TOC16_DS:
11966 case elfcpp::R_PPC64_TOC16_LO_DS:
11967 case elfcpp::R_PPC64_GOT16_DS:
11968 case elfcpp::R_PPC64_GOT16_LO_DS:
11969 case elfcpp::R_PPC64_PLT16_LO_DS:
11970 case elfcpp::R_PPC64_SECTOFF_DS:
11971 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11972 maybe_dq_reloc = true;
11973 break;
11974
11975 case elfcpp::R_POWERPC_ADDR14:
11976 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11977 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11978 case elfcpp::R_POWERPC_REL14:
11979 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11980 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11981 status = Reloc::addr14(view, value, overflow);
11982 break;
11983
11984 case elfcpp::R_POWERPC_COPY:
11985 case elfcpp::R_POWERPC_GLOB_DAT:
11986 case elfcpp::R_POWERPC_JMP_SLOT:
11987 case elfcpp::R_POWERPC_RELATIVE:
11988 case elfcpp::R_POWERPC_DTPMOD:
11989 case elfcpp::R_PPC64_JMP_IREL:
11990 case elfcpp::R_POWERPC_IRELATIVE:
11991 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11992 _("unexpected reloc %u in object file"),
11993 r_type);
11994 break;
11995
11996 case elfcpp::R_PPC64_TOCSAVE:
11997 if (size == 32)
11998 // R_PPC_EMB_SDA21
11999 goto unsupp;
12000 else
12001 {
12002 Symbol_location loc;
12003 loc.object = relinfo->object;
12004 loc.shndx = relinfo->data_shndx;
12005 loc.offset = rela.get_r_offset();
12006 const Tocsave_loc *tocsave = target->tocsave_loc();
12007 if (tocsave->find(loc) != tocsave->end())
12008 {
12009 // If we've generated plt calls using this tocsave, then
12010 // the nop needs to be changed to save r2.
12011 Insn* iview = reinterpret_cast<Insn*>(view);
12012 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12013 elfcpp::Swap<32, big_endian>::
12014 writeval(iview, std_2_1 + target->stk_toc());
12015 }
12016 }
12017 break;
12018
12019 case elfcpp::R_PPC_EMB_SDA2I16:
12020 case elfcpp::R_PPC_EMB_SDA2REL:
12021 if (size == 32)
12022 goto unsupp;
12023 // R_PPC64_TLSGD, R_PPC64_TLSLD
12024 break;
12025
12026 case elfcpp::R_PPC64_D34:
12027 case elfcpp::R_PPC64_D34_LO:
12028 case elfcpp::R_PPC64_PCREL34:
12029 case elfcpp::R_PPC64_GOT_PCREL34:
12030 case elfcpp::R_PPC64_PLT_PCREL34:
12031 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12032 case elfcpp::R_PPC64_TPREL34:
12033 case elfcpp::R_PPC64_DTPREL34:
12034 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12035 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12036 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12037 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12038 if (size == 32)
12039 goto unsupp;
12040 status = Reloc::addr34(view, value, overflow);
12041 break;
12042
12043 case elfcpp::R_PPC64_D34_HI30:
12044 if (size == 32)
12045 goto unsupp;
12046 Reloc::addr34_hi(view, value);
12047 break;
12048
12049 case elfcpp::R_PPC64_D34_HA30:
12050 if (size == 32)
12051 goto unsupp;
12052 Reloc::addr34_ha(view, value);
12053 break;
12054
12055 case elfcpp::R_PPC64_D28:
12056 case elfcpp::R_PPC64_PCREL28:
12057 if (size == 32)
12058 goto unsupp;
12059 status = Reloc::addr28(view, value, overflow);
12060 break;
12061
12062 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12063 case elfcpp::R_PPC64_REL16_HIGHER34:
12064 if (size == 32)
12065 goto unsupp;
12066 Reloc::addr16_higher34(view, value);
12067 break;
12068
12069 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12070 case elfcpp::R_PPC64_REL16_HIGHERA34:
12071 if (size == 32)
12072 goto unsupp;
12073 Reloc::addr16_highera34(view, value);
12074 break;
12075
12076 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12077 case elfcpp::R_PPC64_REL16_HIGHEST34:
12078 if (size == 32)
12079 goto unsupp;
12080 Reloc::addr16_highest34(view, value);
12081 break;
12082
12083 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12084 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12085 if (size == 32)
12086 goto unsupp;
12087 Reloc::addr16_highesta34(view, value);
12088 break;
12089
12090 case elfcpp::R_POWERPC_PLT32:
12091 case elfcpp::R_POWERPC_PLTREL32:
12092 case elfcpp::R_PPC_SDAREL16:
12093 case elfcpp::R_POWERPC_ADDR30:
12094 case elfcpp::R_PPC64_PLT64:
12095 case elfcpp::R_PPC64_PLTREL64:
12096 case elfcpp::R_PPC64_PLTGOT16:
12097 case elfcpp::R_PPC64_PLTGOT16_LO:
12098 case elfcpp::R_PPC64_PLTGOT16_HI:
12099 case elfcpp::R_PPC64_PLTGOT16_HA:
12100 case elfcpp::R_PPC64_PLTGOT16_DS:
12101 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12102 case elfcpp::R_PPC_TOC16:
12103 default:
12104 unsupp:
12105 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12106 _("unsupported reloc %u"),
12107 r_type);
12108 break;
12109 }
12110
12111 if (maybe_dq_reloc)
12112 {
12113 if (insn == 0)
12114 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12115
12116 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12117 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12118 && (insn & 3) == 1))
12119 status = Reloc::addr16_dq(view, value, overflow);
12120 else if (size == 64
12121 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12122 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12123 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12124 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12125 status = Reloc::addr16_ds(view, value, overflow);
12126 else
12127 status = Reloc::addr16(view, value, overflow);
12128 }
12129
12130 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12131 && (has_stub_value
12132 || !(gsym != NULL
12133 && gsym->is_undefined()
12134 && is_branch_reloc<size>(r_type))))
12135 {
12136 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12137 _("relocation overflow"));
12138 if (has_stub_value)
12139 gold_info(_("try relinking with a smaller --stub-group-size"));
12140 }
12141
12142 return true;
12143 }
12144
12145 // Relocate section data.
12146
12147 template<int size, bool big_endian>
12148 void
12149 Target_powerpc<size, big_endian>::relocate_section(
12150 const Relocate_info<size, big_endian>* relinfo,
12151 unsigned int sh_type,
12152 const unsigned char* prelocs,
12153 size_t reloc_count,
12154 Output_section* output_section,
12155 bool needs_special_offset_handling,
12156 unsigned char* view,
12157 Address address,
12158 section_size_type view_size,
12159 const Reloc_symbol_changes* reloc_symbol_changes)
12160 {
12161 typedef Target_powerpc<size, big_endian> Powerpc;
12162 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12163 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12164 Powerpc_comdat_behavior;
12165 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12166 Classify_reloc;
12167
12168 gold_assert(sh_type == elfcpp::SHT_RELA);
12169
12170 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12171 Powerpc_comdat_behavior, Classify_reloc>(
12172 relinfo,
12173 this,
12174 prelocs,
12175 reloc_count,
12176 output_section,
12177 needs_special_offset_handling,
12178 view,
12179 address,
12180 view_size,
12181 reloc_symbol_changes);
12182 }
12183
12184 template<int size, bool big_endian>
12185 class Powerpc_scan_relocatable_reloc
12186 {
12187 public:
12188 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12189 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12190 static const int sh_type = elfcpp::SHT_RELA;
12191
12192 // Return the symbol referred to by the relocation.
12193 static inline unsigned int
12194 get_r_sym(const Reltype* reloc)
12195 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12196
12197 // Return the type of the relocation.
12198 static inline unsigned int
12199 get_r_type(const Reltype* reloc)
12200 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12201
12202 // Return the strategy to use for a local symbol which is not a
12203 // section symbol, given the relocation type.
12204 inline Relocatable_relocs::Reloc_strategy
12205 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12206 {
12207 if (r_type == 0 && r_sym == 0)
12208 return Relocatable_relocs::RELOC_DISCARD;
12209 return Relocatable_relocs::RELOC_COPY;
12210 }
12211
12212 // Return the strategy to use for a local symbol which is a section
12213 // symbol, given the relocation type.
12214 inline Relocatable_relocs::Reloc_strategy
12215 local_section_strategy(unsigned int, Relobj*)
12216 {
12217 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12218 }
12219
12220 // Return the strategy to use for a global symbol, given the
12221 // relocation type, the object, and the symbol index.
12222 inline Relocatable_relocs::Reloc_strategy
12223 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12224 {
12225 if (size == 32
12226 && (r_type == elfcpp::R_PPC_PLTREL24
12227 || r_type == elfcpp::R_POWERPC_PLT16_LO
12228 || r_type == elfcpp::R_POWERPC_PLT16_HI
12229 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12230 return Relocatable_relocs::RELOC_SPECIAL;
12231 return Relocatable_relocs::RELOC_COPY;
12232 }
12233 };
12234
12235 // Scan the relocs during a relocatable link.
12236
12237 template<int size, bool big_endian>
12238 void
12239 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12240 Symbol_table* symtab,
12241 Layout* layout,
12242 Sized_relobj_file<size, big_endian>* object,
12243 unsigned int data_shndx,
12244 unsigned int sh_type,
12245 const unsigned char* prelocs,
12246 size_t reloc_count,
12247 Output_section* output_section,
12248 bool needs_special_offset_handling,
12249 size_t local_symbol_count,
12250 const unsigned char* plocal_symbols,
12251 Relocatable_relocs* rr)
12252 {
12253 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12254
12255 gold_assert(sh_type == elfcpp::SHT_RELA);
12256
12257 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12258 symtab,
12259 layout,
12260 object,
12261 data_shndx,
12262 prelocs,
12263 reloc_count,
12264 output_section,
12265 needs_special_offset_handling,
12266 local_symbol_count,
12267 plocal_symbols,
12268 rr);
12269 }
12270
12271 // Scan the relocs for --emit-relocs.
12272
12273 template<int size, bool big_endian>
12274 void
12275 Target_powerpc<size, big_endian>::emit_relocs_scan(
12276 Symbol_table* symtab,
12277 Layout* layout,
12278 Sized_relobj_file<size, big_endian>* object,
12279 unsigned int data_shndx,
12280 unsigned int sh_type,
12281 const unsigned char* prelocs,
12282 size_t reloc_count,
12283 Output_section* output_section,
12284 bool needs_special_offset_handling,
12285 size_t local_symbol_count,
12286 const unsigned char* plocal_syms,
12287 Relocatable_relocs* rr)
12288 {
12289 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12290 Classify_reloc;
12291 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12292 Emit_relocs_strategy;
12293
12294 gold_assert(sh_type == elfcpp::SHT_RELA);
12295
12296 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12297 symtab,
12298 layout,
12299 object,
12300 data_shndx,
12301 prelocs,
12302 reloc_count,
12303 output_section,
12304 needs_special_offset_handling,
12305 local_symbol_count,
12306 plocal_syms,
12307 rr);
12308 }
12309
12310 // Emit relocations for a section.
12311 // This is a modified version of the function by the same name in
12312 // target-reloc.h. Using relocate_special_relocatable for
12313 // R_PPC_PLTREL24 would require duplication of the entire body of the
12314 // loop, so we may as well duplicate the whole thing.
12315
12316 template<int size, bool big_endian>
12317 void
12318 Target_powerpc<size, big_endian>::relocate_relocs(
12319 const Relocate_info<size, big_endian>* relinfo,
12320 unsigned int sh_type,
12321 const unsigned char* prelocs,
12322 size_t reloc_count,
12323 Output_section* output_section,
12324 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12325 unsigned char*,
12326 Address view_address,
12327 section_size_type,
12328 unsigned char* reloc_view,
12329 section_size_type reloc_view_size)
12330 {
12331 gold_assert(sh_type == elfcpp::SHT_RELA);
12332
12333 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12334 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12335 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12336 // Offset from start of insn to d-field reloc.
12337 const int d_offset = big_endian ? 2 : 0;
12338
12339 Powerpc_relobj<size, big_endian>* const object
12340 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12341 const unsigned int local_count = object->local_symbol_count();
12342 unsigned int got2_shndx = object->got2_shndx();
12343 Address got2_addend = 0;
12344 if (got2_shndx != 0)
12345 {
12346 got2_addend = object->get_output_section_offset(got2_shndx);
12347 gold_assert(got2_addend != invalid_address);
12348 }
12349
12350 const bool relocatable = parameters->options().relocatable();
12351
12352 unsigned char* pwrite = reloc_view;
12353 bool zap_next = false;
12354 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12355 {
12356 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12357 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12358 continue;
12359
12360 Reltype reloc(prelocs);
12361 Reltype_write reloc_write(pwrite);
12362
12363 Address offset = reloc.get_r_offset();
12364 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12365 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12366 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12367 const unsigned int orig_r_sym = r_sym;
12368 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12369 = reloc.get_r_addend();
12370 const Symbol* gsym = NULL;
12371
12372 if (zap_next)
12373 {
12374 // We could arrange to discard these and other relocs for
12375 // tls optimised sequences in the strategy methods, but for
12376 // now do as BFD ld does.
12377 r_type = elfcpp::R_POWERPC_NONE;
12378 zap_next = false;
12379 }
12380
12381 // Get the new symbol index.
12382 Output_section* os = NULL;
12383 if (r_sym < local_count)
12384 {
12385 switch (strategy)
12386 {
12387 case Relocatable_relocs::RELOC_COPY:
12388 case Relocatable_relocs::RELOC_SPECIAL:
12389 if (r_sym != 0)
12390 {
12391 r_sym = object->symtab_index(r_sym);
12392 gold_assert(r_sym != -1U);
12393 }
12394 break;
12395
12396 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12397 {
12398 // We are adjusting a section symbol. We need to find
12399 // the symbol table index of the section symbol for
12400 // the output section corresponding to input section
12401 // in which this symbol is defined.
12402 gold_assert(r_sym < local_count);
12403 bool is_ordinary;
12404 unsigned int shndx =
12405 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12406 gold_assert(is_ordinary);
12407 os = object->output_section(shndx);
12408 gold_assert(os != NULL);
12409 gold_assert(os->needs_symtab_index());
12410 r_sym = os->symtab_index();
12411 }
12412 break;
12413
12414 default:
12415 gold_unreachable();
12416 }
12417 }
12418 else
12419 {
12420 gsym = object->global_symbol(r_sym);
12421 gold_assert(gsym != NULL);
12422 if (gsym->is_forwarder())
12423 gsym = relinfo->symtab->resolve_forwards(gsym);
12424
12425 gold_assert(gsym->has_symtab_index());
12426 r_sym = gsym->symtab_index();
12427 }
12428
12429 // Get the new offset--the location in the output section where
12430 // this relocation should be applied.
12431 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12432 offset += offset_in_output_section;
12433 else
12434 {
12435 section_offset_type sot_offset =
12436 convert_types<section_offset_type, Address>(offset);
12437 section_offset_type new_sot_offset =
12438 output_section->output_offset(object, relinfo->data_shndx,
12439 sot_offset);
12440 gold_assert(new_sot_offset != -1);
12441 offset = new_sot_offset;
12442 }
12443
12444 // In an object file, r_offset is an offset within the section.
12445 // In an executable or dynamic object, generated by
12446 // --emit-relocs, r_offset is an absolute address.
12447 if (!relocatable)
12448 {
12449 offset += view_address;
12450 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12451 offset -= offset_in_output_section;
12452 }
12453
12454 // Handle the reloc addend based on the strategy.
12455 if (strategy == Relocatable_relocs::RELOC_COPY)
12456 ;
12457 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12458 {
12459 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12460 addend = psymval->value(object, addend);
12461 // In a relocatable link, the symbol value is relative to
12462 // the start of the output section. For a non-relocatable
12463 // link, we need to adjust the addend.
12464 if (!relocatable)
12465 {
12466 gold_assert(os != NULL);
12467 addend -= os->address();
12468 }
12469 }
12470 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12471 {
12472 if (size == 32)
12473 {
12474 if (addend >= 32768)
12475 addend += got2_addend;
12476 }
12477 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12478 {
12479 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12480 addend -= d_offset;
12481 }
12482 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12483 {
12484 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12485 addend -= d_offset + 4;
12486 }
12487 }
12488 else
12489 gold_unreachable();
12490
12491 if (!relocatable)
12492 {
12493 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12494 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12495 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12496 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12497 {
12498 // First instruction of a global dynamic sequence,
12499 // arg setup insn.
12500 bool final = gsym == NULL || gsym->final_value_is_known();
12501 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12502 switch (tls_type)
12503 {
12504 case tls::TLSOPT_TO_IE:
12505 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12506 - elfcpp::R_POWERPC_GOT_TLSGD16);
12507 break;
12508 case tls::TLSOPT_TO_LE:
12509 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12510 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12511 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12512 else
12513 {
12514 r_type = elfcpp::R_POWERPC_NONE;
12515 offset -= d_offset;
12516 }
12517 break;
12518 default:
12519 break;
12520 }
12521 }
12522 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12523 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12524 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12525 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12526 {
12527 // First instruction of a local dynamic sequence,
12528 // arg setup insn.
12529 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12530 if (tls_type == tls::TLSOPT_TO_LE)
12531 {
12532 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12533 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12534 {
12535 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12536 const Output_section* os = relinfo->layout->tls_segment()
12537 ->first_section();
12538 gold_assert(os != NULL);
12539 gold_assert(os->needs_symtab_index());
12540 r_sym = os->symtab_index();
12541 addend = dtp_offset;
12542 }
12543 else
12544 {
12545 r_type = elfcpp::R_POWERPC_NONE;
12546 offset -= d_offset;
12547 }
12548 }
12549 }
12550 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12551 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12552 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12553 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12554 {
12555 // First instruction of initial exec sequence.
12556 bool final = gsym == NULL || gsym->final_value_is_known();
12557 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12558 {
12559 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12560 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12561 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12562 else
12563 {
12564 r_type = elfcpp::R_POWERPC_NONE;
12565 offset -= d_offset;
12566 }
12567 }
12568 }
12569 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12570 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12571 {
12572 // Second instruction of a global dynamic sequence,
12573 // the __tls_get_addr call
12574 bool final = gsym == NULL || gsym->final_value_is_known();
12575 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12576 switch (tls_type)
12577 {
12578 case tls::TLSOPT_TO_IE:
12579 r_type = elfcpp::R_POWERPC_NONE;
12580 zap_next = true;
12581 break;
12582 case tls::TLSOPT_TO_LE:
12583 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12584 offset += d_offset;
12585 zap_next = true;
12586 break;
12587 default:
12588 break;
12589 }
12590 }
12591 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12592 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12593 {
12594 // Second instruction of a local dynamic sequence,
12595 // the __tls_get_addr call
12596 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12597 if (tls_type == tls::TLSOPT_TO_LE)
12598 {
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 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12606 offset += d_offset;
12607 zap_next = true;
12608 }
12609 }
12610 else if (r_type == elfcpp::R_POWERPC_TLS)
12611 {
12612 // Second instruction of an initial exec sequence
12613 bool final = gsym == NULL || gsym->final_value_is_known();
12614 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12615 {
12616 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12617 offset += d_offset;
12618 }
12619 }
12620 }
12621
12622 reloc_write.put_r_offset(offset);
12623 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12624 reloc_write.put_r_addend(addend);
12625
12626 pwrite += reloc_size;
12627 }
12628
12629 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12630 == reloc_view_size);
12631 }
12632
12633 // Return the value to use for a dynamic symbol which requires special
12634 // treatment. This is how we support equality comparisons of function
12635 // pointers across shared library boundaries, as described in the
12636 // processor specific ABI supplement.
12637
12638 template<int size, bool big_endian>
12639 uint64_t
12640 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12641 {
12642 if (size == 32)
12643 {
12644 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12645 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12646 p != this->stub_tables_.end();
12647 ++p)
12648 {
12649 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12650 = (*p)->find_plt_call_entry(gsym);
12651 if (ent != NULL)
12652 return (*p)->stub_address() + ent->off_;
12653 }
12654 }
12655 else if (this->abiversion() >= 2)
12656 {
12657 Address off = this->glink_section()->find_global_entry(gsym);
12658 if (off != invalid_address)
12659 return this->glink_section()->global_entry_address() + off;
12660 }
12661 gold_unreachable();
12662 }
12663
12664 // Return the PLT address to use for a local symbol.
12665 template<int size, bool big_endian>
12666 uint64_t
12667 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12668 const Relobj* object,
12669 unsigned int symndx) const
12670 {
12671 if (size == 32)
12672 {
12673 const Sized_relobj<size, big_endian>* relobj
12674 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12675 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12676 p != this->stub_tables_.end();
12677 ++p)
12678 {
12679 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12680 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12681 if (ent != NULL)
12682 return (*p)->stub_address() + ent->off_;
12683 }
12684 }
12685 gold_unreachable();
12686 }
12687
12688 // Return the PLT address to use for a global symbol.
12689 template<int size, bool big_endian>
12690 uint64_t
12691 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12692 const Symbol* gsym) const
12693 {
12694 if (size == 32)
12695 {
12696 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12697 p != this->stub_tables_.end();
12698 ++p)
12699 {
12700 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12701 = (*p)->find_plt_call_entry(gsym);
12702 if (ent != NULL)
12703 return (*p)->stub_address() + ent->off_;
12704 }
12705 }
12706 else if (this->abiversion() >= 2)
12707 {
12708 Address off = this->glink_section()->find_global_entry(gsym);
12709 if (off != invalid_address)
12710 return this->glink_section()->global_entry_address() + off;
12711 }
12712 gold_unreachable();
12713 }
12714
12715 // Return the offset to use for the GOT_INDX'th got entry which is
12716 // for a local tls symbol specified by OBJECT, SYMNDX.
12717 template<int size, bool big_endian>
12718 int64_t
12719 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12720 const Relobj* object,
12721 unsigned int symndx,
12722 Output_data_got_base* got,
12723 unsigned int got_indx,
12724 uint64_t addend) const
12725 {
12726 const Powerpc_relobj<size, big_endian>* ppc_object
12727 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12728 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12729 {
12730 for (Got_type got_type = (size == 32
12731 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
12732 got_type <= GOT_TYPE_SMALL_TPREL;
12733 got_type = Got_type(got_type + 1))
12734 if (got_type != GOT_TYPE_SMALL
12735 && ppc_object->local_has_got_offset(symndx, got_type, addend))
12736 {
12737 unsigned int off
12738 = ppc_object->local_got_offset(symndx, got_type, addend);
12739 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
12740 off += size / 8;
12741 if (off == got_indx * (size / 8)
12742 && (size == 32 || got == this->got_section(got_type)))
12743 {
12744 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
12745 return -tp_offset;
12746 else
12747 return -dtp_offset;
12748 }
12749 }
12750 }
12751 gold_unreachable();
12752 }
12753
12754 // Return the offset to use for the GOT_INDX'th got entry which is
12755 // for global tls symbol GSYM.
12756 template<int size, bool big_endian>
12757 int64_t
12758 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12759 Symbol* gsym,
12760 Output_data_got_base* got,
12761 unsigned int got_indx,
12762 uint64_t addend) const
12763 {
12764 if (gsym->type() == elfcpp::STT_TLS)
12765 {
12766 for (Got_type got_type = (size == 32
12767 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
12768 got_type <= GOT_TYPE_SMALL_TPREL;
12769 got_type = Got_type(got_type + 1))
12770 if (got_type != GOT_TYPE_SMALL
12771 && gsym->has_got_offset(got_type, addend))
12772 {
12773 unsigned int off = gsym->got_offset(got_type, addend);
12774 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
12775 off += size / 8;
12776 if (off == got_indx * (size / 8)
12777 && (size == 32 || got == this->got_section(got_type)))
12778 {
12779 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
12780 return -tp_offset;
12781 else
12782 return -dtp_offset;
12783 }
12784 }
12785 }
12786 gold_unreachable();
12787 }
12788
12789 // The selector for powerpc object files.
12790
12791 template<int size, bool big_endian>
12792 class Target_selector_powerpc : public Target_selector
12793 {
12794 public:
12795 Target_selector_powerpc()
12796 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12797 size, big_endian,
12798 (size == 64
12799 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12800 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12801 (size == 64
12802 ? (big_endian ? "elf64ppc" : "elf64lppc")
12803 : (big_endian ? "elf32ppc" : "elf32lppc")))
12804 { }
12805
12806 virtual Target*
12807 do_instantiate_target()
12808 { return new Target_powerpc<size, big_endian>(); }
12809 };
12810
12811 Target_selector_powerpc<32, true> target_selector_ppc32;
12812 Target_selector_powerpc<32, false> target_selector_ppc32le;
12813 Target_selector_powerpc<64, true> target_selector_ppc64;
12814 Target_selector_powerpc<64, false> target_selector_ppc64le;
12815
12816 // Instantiate these constants for -O0
12817 template<int size, bool big_endian>
12818 const typename Output_data_glink<size, big_endian>::Address
12819 Output_data_glink<size, big_endian>::invalid_address;
12820 template<int size, bool big_endian>
12821 const typename Stub_table<size, big_endian>::Address
12822 Stub_table<size, big_endian>::invalid_address;
12823 template<int size, bool big_endian>
12824 const typename Target_powerpc<size, big_endian>::Address
12825 Target_powerpc<size, big_endian>::invalid_address;
12826
12827 } // End anonymous namespace.