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