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