]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/powerpc.cc
e66d9cbb9005fddc11210a8581ab33fd1d95c39e
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright (C) 2008-2023 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
6
7 // This file is part of gold.
8
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
23
24 #include "gold.h"
25
26 #include <set>
27 #include <algorithm>
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "powerpc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "errors.h"
43 #include "gc.h"
44 #include "attributes.h"
45
46 namespace
47 {
48
49 using namespace gold;
50
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
53
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
56
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
59
60 template<int size, bool big_endian>
61 class Output_data_glink;
62
63 template<int size, bool big_endian>
64 class Stub_table;
65
66 template<int size, bool big_endian>
67 class Output_data_save_res;
68
69 template<int size, bool big_endian>
70 class Target_powerpc;
71
72 struct Stub_table_owner
73 {
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
77
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
80 };
81
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
84
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
87
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
90
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93 {
94 public:
95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
98
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
107 {
108 this->set_abiversion(0);
109 }
110
111 ~Powerpc_relobj()
112 { delete this->attributes_section_data_; }
113
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
117
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
124
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
128 {
129 return this->toc_;
130 }
131
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
135 {
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
142 }
143
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
147 {
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
150 }
151
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
155 {
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
162 }
163
164 // The .got2 section shndx.
165 unsigned int
166 got2_shndx() const
167 {
168 if (size == 32)
169 return this->special_;
170 else
171 return 0;
172 }
173
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
177 {
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
182 }
183
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
187 {
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
190 }
191
192 // Return section and offset of function entry for .opd + R_OFF.
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
195 {
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
199 if (value != NULL)
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
202 }
203
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
207 {
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
212 }
213
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
217 {
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
221 }
222
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
226 {
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
230 }
231
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
235
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
239
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
245
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
251
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
257
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
260 void
261 do_read_relocs(Read_relocs_data*);
262
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
265
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
270 {
271 if (size == 64 && this->opd_shndx() != 0)
272 {
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
278 }
279 return true;
280 }
281
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
285
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
289 add_reference(Relobj* src_obj,
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292 {
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
295 }
296
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301 {
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
306 }
307
308 void
309 process_gc_mark(Symbol_table* symtab)
310 {
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
313 {
314 unsigned int shndx = this->opd_ent_[i].shndx;
315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
316 }
317 }
318
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
322
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
326
327 void
328 set_has_14bit_branch(unsigned int shndx)
329 {
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
333 }
334
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
338
339 void
340 set_stub_table(unsigned int shndx, unsigned int stub_index)
341 {
342 if (shndx >= this->stub_table_index_.size())
343 this->stub_table_index_.resize(shndx + 1, -1);
344 this->stub_table_index_[shndx] = stub_index;
345 }
346
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
349 {
350 if (shndx < this->stub_table_index_.size())
351 {
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
358 }
359 return NULL;
360 }
361
362 void
363 clear_stub_table()
364 {
365 this->stub_table_index_.clear();
366 }
367
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
371
372 int
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
379
380 unsigned int
381 st_other (unsigned int symndx) const
382 {
383 return this->st_other_[symndx];
384 }
385
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
397
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
401
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
406
407 private:
408 struct Opd_ent
409 {
410 unsigned int shndx;
411 bool discard : 1;
412 bool gc_mark : 1;
413 Address off;
414 };
415
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
429
430 // Per object unique identifier
431 uint32_t uniq_;
432
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
435
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
439
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
443
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
450
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
453
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
457
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
460 // relocation on this word. The following vector records the
461 // section and offset specified by these relocations.
462 std::vector<Opd_ent> opd_ent_;
463
464 // References made to this object's .opd section when running
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
467 Access_from access_from_map_;
468
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
471
472 // The stub table to use for a given input section.
473 std::vector<unsigned int> stub_table_index_;
474
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
477
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
480 };
481
482 template<int size, bool big_endian>
483 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484 {
485 public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
493 {
494 this->set_abiversion(0);
495 }
496
497 ~Powerpc_dynobj()
498 { delete this->attributes_section_data_; }
499
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
504
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
508 {
509 return this->opd_shndx_;
510 }
511
512 // The .opd section address.
513 Address
514 opd_address() const
515 {
516 return this->opd_address_;
517 }
518
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
522 {
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
525 }
526
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
530 {
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
537 }
538
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
542 {
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
547 }
548
549 int
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
556
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
561
562 private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
565 {
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
568 { }
569
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
573
574 Address start;
575 Address len;
576 unsigned int shndx;
577 };
578
579 struct Opd_ent
580 {
581 unsigned int shndx;
582 Address off;
583 };
584
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
589
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
593
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
596
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
602
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
605 };
606
607 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608 // base class will emit.
609
610 template<int sh_type, int size, bool big_endian>
611 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612 {
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616 { }
617
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622 };
623
624 // The types of GOT entries needed for this platform.
625 // These values are exposed to the ABI in an incremental link, but
626 // powerpc does not support incremental linking as yet.
627 enum Got_type
628 {
629 GOT_TYPE_STANDARD = 0,
630 GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
631 GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
632 GOT_TYPE_TPREL = 3, // entry for @got@tprel
633 GOT_TYPE_SMALL = 4,
634 GOT_TYPE_SMALL_TLSGD = 5,
635 GOT_TYPE_SMALL_DTPREL = 6,
636 GOT_TYPE_SMALL_TPREL = 7
637 };
638
639 // gsym->needs_plt_entry purpose is to decide whether a non-branch
640 // reloc should reference a plt entry. It can't be used to decide
641 // whether branches need a plt entry. In fact the call to
642 // needs_plt_entry here is not needed; All cases where it might
643 // return true ought to be covered already. However, since this
644 // function is used to decide between plt_ and lplt_ sections in
645 // plt_off, make certain that every case where make_plt_entry puts
646 // entries in plt_ is covered here.
647 static bool
648 branch_needs_plt_entry(const Symbol* gsym)
649 {
650 return (((!gsym->is_defined()
651 || gsym->is_from_dynobj()
652 || gsym->is_preemptible())
653 && !gsym->final_value_is_known())
654 || gsym->needs_plt_entry());
655 }
656
657 template<int size, bool big_endian>
658 class Target_powerpc : public Sized_target<size, big_endian>
659 {
660 public:
661 typedef
662 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
664 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
665 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
666 static const Address invalid_address = static_cast<Address>(0) - 1;
667 // Offset of tp and dtp pointers from start of TLS block.
668 static const Address tp_offset = 0x7000;
669 static const Address dtp_offset = 0x8000;
670
671 Target_powerpc()
672 : Sized_target<size, big_endian>(&powerpc_info),
673 got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
674 brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
675 tlsld_got_offset_(-1U),
676 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
677 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
678 plt_localentry0_init_(false), has_localentry0_(false),
679 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
680 relax_failed_(false), relax_fail_count_(0),
681 stub_group_size_(0), savres_section_(0),
682 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
683 attributes_section_data_(NULL),
684 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
685 {
686 }
687
688 // Process the relocations to determine unreferenced sections for
689 // garbage collection.
690 void
691 gc_process_relocs(Symbol_table* symtab,
692 Layout* layout,
693 Sized_relobj_file<size, big_endian>* object,
694 unsigned int data_shndx,
695 unsigned int sh_type,
696 const unsigned char* prelocs,
697 size_t reloc_count,
698 Output_section* output_section,
699 bool needs_special_offset_handling,
700 size_t local_symbol_count,
701 const unsigned char* plocal_symbols);
702
703 // Scan the relocations to look for symbol adjustments.
704 void
705 scan_relocs(Symbol_table* symtab,
706 Layout* layout,
707 Sized_relobj_file<size, big_endian>* object,
708 unsigned int data_shndx,
709 unsigned int sh_type,
710 const unsigned char* prelocs,
711 size_t reloc_count,
712 Output_section* output_section,
713 bool needs_special_offset_handling,
714 size_t local_symbol_count,
715 const unsigned char* plocal_symbols);
716
717 // Map input .toc section to output .got section.
718 const char*
719 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
720 {
721 if (size == 64 && strcmp(name, ".toc") == 0)
722 {
723 *plen = 4;
724 return ".got";
725 }
726 return NULL;
727 }
728
729 // Provide linker defined save/restore functions.
730 void
731 define_save_restore_funcs(Layout*, Symbol_table*);
732
733 // No stubs unless a final link.
734 bool
735 do_may_relax() const
736 { return !parameters->options().relocatable(); }
737
738 bool
739 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
740
741 void
742 do_plt_fde_location(const Output_data*, unsigned char*,
743 uint64_t*, off_t*) const;
744
745 // Stash info about branches, for stub generation.
746 void
747 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
748 unsigned int data_shndx, Address r_offset,
749 unsigned int r_type, unsigned int r_sym, Address addend)
750 {
751 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
752 this->branch_info_.push_back(info);
753 if (r_type == elfcpp::R_POWERPC_REL14
754 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
755 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
756 ppc_object->set_has_14bit_branch(data_shndx);
757 }
758
759 // Return whether the last branch is a plt call, and if so, mark the
760 // branch as having an R_PPC64_TOCSAVE.
761 bool
762 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
763 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
764 {
765 return (size == 64
766 && !this->branch_info_.empty()
767 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
768 r_offset, this, symtab));
769 }
770
771 // Say the given location, that of a nop in a function prologue with
772 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
773 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
774 void
775 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
776 unsigned int shndx, Address offset)
777 {
778 Symbol_location loc;
779 loc.object = ppc_object;
780 loc.shndx = shndx;
781 loc.offset = offset;
782 this->tocsave_loc_.insert(loc);
783 }
784
785 // Accessor
786 const Tocsave_loc*
787 tocsave_loc() const
788 {
789 return &this->tocsave_loc_;
790 }
791
792 void
793 do_define_standard_symbols(Symbol_table*, Layout*);
794
795 // Finalize the sections.
796 void
797 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
798
799 // Get the custom dynamic tag value.
800 unsigned int
801 do_dynamic_tag_custom_value(elfcpp::DT) const;
802
803 // Return the value to use for a dynamic which requires special
804 // treatment.
805 uint64_t
806 do_dynsym_value(const Symbol*) const;
807
808 // Return the PLT address to use for a local symbol.
809 uint64_t
810 do_plt_address_for_local(const Relobj*, unsigned int) const;
811
812 // Return the PLT address to use for a global symbol.
813 uint64_t
814 do_plt_address_for_global(const Symbol*) const;
815
816 // Return the offset to use for the GOT_INDX'th got entry which is
817 // for a local tls symbol specified by OBJECT, SYMNDX.
818 int64_t
819 do_tls_offset_for_local(const Relobj* object,
820 unsigned int symndx,
821 Output_data_got_base* got,
822 unsigned int got_indx,
823 uint64_t addend) const;
824
825 // Return the offset to use for the GOT_INDX'th got entry which is
826 // for global tls symbol GSYM.
827 int64_t
828 do_tls_offset_for_global(Symbol* gsym,
829 Output_data_got_base* got, unsigned int got_indx,
830 uint64_t addend) const;
831
832 void
833 do_function_location(Symbol_location*) const;
834
835 bool
836 do_can_check_for_function_pointers() const
837 { return true; }
838
839 // Adjust -fsplit-stack code which calls non-split-stack code.
840 void
841 do_calls_non_split(Relobj* object, unsigned int shndx,
842 section_offset_type fnoffset, section_size_type fnsize,
843 const unsigned char* prelocs, size_t reloc_count,
844 unsigned char* view, section_size_type view_size,
845 std::string* from, std::string* to) const;
846
847 // Relocate a section.
848 void
849 relocate_section(const Relocate_info<size, big_endian>*,
850 unsigned int sh_type,
851 const unsigned char* prelocs,
852 size_t reloc_count,
853 Output_section* output_section,
854 bool needs_special_offset_handling,
855 unsigned char* view,
856 Address view_address,
857 section_size_type view_size,
858 const Reloc_symbol_changes*);
859
860 // Scan the relocs during a relocatable link.
861 void
862 scan_relocatable_relocs(Symbol_table* symtab,
863 Layout* layout,
864 Sized_relobj_file<size, big_endian>* object,
865 unsigned int data_shndx,
866 unsigned int sh_type,
867 const unsigned char* prelocs,
868 size_t reloc_count,
869 Output_section* output_section,
870 bool needs_special_offset_handling,
871 size_t local_symbol_count,
872 const unsigned char* plocal_symbols,
873 Relocatable_relocs*);
874
875 // Scan the relocs for --emit-relocs.
876 void
877 emit_relocs_scan(Symbol_table* symtab,
878 Layout* layout,
879 Sized_relobj_file<size, big_endian>* object,
880 unsigned int data_shndx,
881 unsigned int sh_type,
882 const unsigned char* prelocs,
883 size_t reloc_count,
884 Output_section* output_section,
885 bool needs_special_offset_handling,
886 size_t local_symbol_count,
887 const unsigned char* plocal_syms,
888 Relocatable_relocs* rr);
889
890 // Emit relocations for a section.
891 void
892 relocate_relocs(const Relocate_info<size, big_endian>*,
893 unsigned int sh_type,
894 const unsigned char* prelocs,
895 size_t reloc_count,
896 Output_section* output_section,
897 typename elfcpp::Elf_types<size>::Elf_Off
898 offset_in_output_section,
899 unsigned char*,
900 Address view_address,
901 section_size_type,
902 unsigned char* reloc_view,
903 section_size_type reloc_view_size);
904
905 // Return whether SYM is defined by the ABI.
906 bool
907 do_is_defined_by_abi(const Symbol* sym) const
908 {
909 return strcmp(sym->name(), "__tls_get_addr") == 0;
910 }
911
912 // Return the size of the GOT section, for incremental linking
913 section_size_type
914 got_size() const
915 {
916 gold_assert(this->got_ != NULL);
917 return this->got_->data_size() + (this->biggot_
918 ? this->biggot_->data_size() : 0);
919 }
920
921 // Get the PLT section.
922 const Output_data_plt_powerpc<size, big_endian>*
923 plt_section() const
924 {
925 gold_assert(this->plt_ != NULL);
926 return this->plt_;
927 }
928
929 // Get the IPLT section.
930 const Output_data_plt_powerpc<size, big_endian>*
931 iplt_section() const
932 {
933 gold_assert(this->iplt_ != NULL);
934 return this->iplt_;
935 }
936
937 // Get the LPLT section.
938 const Output_data_plt_powerpc<size, big_endian>*
939 lplt_section() const
940 {
941 return this->lplt_;
942 }
943
944 // Return the plt offset and section for the given global sym.
945 Address
946 plt_off(const Symbol* gsym,
947 const Output_data_plt_powerpc<size, big_endian>** sec) const
948 {
949 if (gsym->type() == elfcpp::STT_GNU_IFUNC
950 && gsym->can_use_relative_reloc(false))
951 *sec = this->iplt_section();
952 else if (branch_needs_plt_entry(gsym))
953 *sec = this->plt_section();
954 else
955 *sec = this->lplt_section();
956 return gsym->plt_offset();
957 }
958
959 // Return the plt offset and section for the given local sym.
960 Address
961 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
962 unsigned int local_sym_index,
963 const Output_data_plt_powerpc<size, big_endian>** sec) const
964 {
965 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
966 if (lsym->is_ifunc_symbol())
967 *sec = this->iplt_section();
968 else
969 *sec = this->lplt_section();
970 return relobj->local_plt_offset(local_sym_index);
971 }
972
973 // Get the .glink section.
974 const Output_data_glink<size, big_endian>*
975 glink_section() const
976 {
977 gold_assert(this->glink_ != NULL);
978 return this->glink_;
979 }
980
981 Output_data_glink<size, big_endian>*
982 glink_section()
983 {
984 gold_assert(this->glink_ != NULL);
985 return this->glink_;
986 }
987
988 bool has_glink() const
989 { return this->glink_ != NULL; }
990
991 // Get the GOT section.
992 const Output_data_got_powerpc<size, big_endian>*
993 got_section(Got_type got_type) const
994 {
995 gold_assert(this->got_ != NULL);
996 if (size == 32 || (got_type & GOT_TYPE_SMALL))
997 return this->got_;
998 gold_assert(this->biggot_ != NULL);
999 return this->biggot_;
1000 }
1001
1002 // Get the GOT section, creating it if necessary.
1003 Output_data_got_powerpc<size, big_endian>*
1004 got_section(Symbol_table*, Layout*, Got_type);
1005
1006 // The toc/got pointer reg will be set to this value.
1007 Address
1008 toc_pointer() const
1009 {
1010 return this->got_->address() + this->got_->g_o_t();
1011 }
1012
1013 // Offset of base used to access the GOT/TOC relative to the GOT section.
1014 Address
1015 got_base_offset(Got_type got_type) const
1016 {
1017 if (size == 32 || (got_type & GOT_TYPE_SMALL))
1018 return this->got_->g_o_t();
1019 return this->toc_pointer() - this->biggot_->address();
1020 }
1021
1022 Object*
1023 do_make_elf_object(const std::string&, Input_file*, off_t,
1024 const elfcpp::Ehdr<size, big_endian>&);
1025
1026 // Return the number of entries in the GOT.
1027 unsigned int
1028 got_entry_count() const
1029 {
1030 if (this->got_ == NULL)
1031 return 0;
1032 return this->got_size() / (size / 8);
1033 }
1034
1035 // Return the number of entries in the PLT.
1036 unsigned int
1037 plt_entry_count() const;
1038
1039 // Return the offset of the first non-reserved PLT entry.
1040 unsigned int
1041 first_plt_entry_offset() const
1042 {
1043 if (size == 32)
1044 return 0;
1045 if (this->abiversion() >= 2)
1046 return 16;
1047 return 24;
1048 }
1049
1050 // Return the size of each PLT entry.
1051 unsigned int
1052 plt_entry_size() const
1053 {
1054 if (size == 32)
1055 return 4;
1056 if (this->abiversion() >= 2)
1057 return 8;
1058 return 24;
1059 }
1060
1061 Output_data_save_res<size, big_endian>*
1062 savres_section() const
1063 {
1064 return this->savres_section_;
1065 }
1066
1067 // Add any special sections for this symbol to the gc work list.
1068 // For powerpc64, this adds the code section of a function
1069 // descriptor.
1070 void
1071 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1072
1073 // Handle target specific gc actions when adding a gc reference from
1074 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1075 // and DST_OFF. For powerpc64, this adds a referenc to the code
1076 // section of a function descriptor.
1077 void
1078 do_gc_add_reference(Symbol_table* symtab,
1079 Relobj* src_obj,
1080 unsigned int src_shndx,
1081 Relobj* dst_obj,
1082 unsigned int dst_shndx,
1083 Address dst_off) const;
1084
1085 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1086 const Stub_tables&
1087 stub_tables() const
1088 { return this->stub_tables_; }
1089
1090 const Output_data_brlt_powerpc<size, big_endian>*
1091 brlt_section() const
1092 { return this->brlt_section_; }
1093
1094 void
1095 add_branch_lookup_table(Address to)
1096 {
1097 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1098 this->branch_lookup_table_.insert(std::make_pair(to, off));
1099 }
1100
1101 Address
1102 find_branch_lookup_table(Address to)
1103 {
1104 typename Branch_lookup_table::const_iterator p
1105 = this->branch_lookup_table_.find(to);
1106 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1107 }
1108
1109 void
1110 write_branch_lookup_table(unsigned char *oview)
1111 {
1112 for (typename Branch_lookup_table::const_iterator p
1113 = this->branch_lookup_table_.begin();
1114 p != this->branch_lookup_table_.end();
1115 ++p)
1116 {
1117 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1118 }
1119 }
1120
1121 // Wrapper used after relax to define a local symbol in output data,
1122 // from the end if value < 0.
1123 void
1124 define_local(Symbol_table* symtab, const char* name,
1125 Output_data* od, Address value, unsigned int symsize)
1126 {
1127 Symbol* sym
1128 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1129 od, value, symsize, elfcpp::STT_NOTYPE,
1130 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1131 static_cast<Signed_address>(value) < 0,
1132 false);
1133 // We are creating this symbol late, so need to fix up things
1134 // done early in Layout::finalize.
1135 sym->set_dynsym_index(-1U);
1136 }
1137
1138 void
1139 set_power10_relocs()
1140 {
1141 this->power10_relocs_ = true;
1142 }
1143
1144 bool
1145 power10_stubs() const
1146 {
1147 return (this->power10_relocs_
1148 && (parameters->options().power10_stubs_enum()
1149 != General_options::POWER10_STUBS_NO));
1150 }
1151
1152 bool
1153 power10_stubs_auto() const
1154 {
1155 return (parameters->options().power10_stubs_enum()
1156 == General_options::POWER10_STUBS_AUTO);
1157 }
1158
1159 bool
1160 plt_thread_safe() const
1161 { return this->plt_thread_safe_; }
1162
1163 bool
1164 plt_localentry0() const
1165 { return this->plt_localentry0_; }
1166
1167 bool
1168 has_localentry0() const
1169 { return this->has_localentry0_; }
1170
1171 void
1172 set_has_localentry0()
1173 {
1174 this->has_localentry0_ = true;
1175 }
1176
1177 bool
1178 is_elfv2_localentry0(const Symbol* gsym) const
1179 {
1180 return (size == 64
1181 && this->abiversion() >= 2
1182 && this->plt_localentry0()
1183 && gsym->type() == elfcpp::STT_FUNC
1184 && gsym->is_defined()
1185 && gsym->nonvis() >> 3 == 0
1186 && !gsym->non_zero_localentry());
1187 }
1188
1189 bool
1190 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1191 unsigned int r_sym) const
1192 {
1193 const Powerpc_relobj<size, big_endian>* ppc_object
1194 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1195
1196 if (size == 64
1197 && this->abiversion() >= 2
1198 && this->plt_localentry0()
1199 && ppc_object->st_other(r_sym) >> 5 == 0)
1200 {
1201 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1202 bool is_ordinary;
1203 if (!psymval->is_ifunc_symbol()
1204 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1205 && is_ordinary)
1206 return true;
1207 }
1208 return false;
1209 }
1210
1211 bool
1212 tprel_opt() const
1213 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
1214
1215 void
1216 set_no_tprel_opt()
1217 { this->no_tprel_opt_ = true; }
1218
1219 // Remember any symbols seen with non-zero localentry, even those
1220 // not providing a definition
1221 bool
1222 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1223 const char*)
1224 {
1225 if (size == 64)
1226 {
1227 unsigned char st_other = sym.get_st_other();
1228 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1229 to->set_non_zero_localentry();
1230 }
1231 // We haven't resolved anything, continue normal processing.
1232 return false;
1233 }
1234
1235 int
1236 abiversion() const
1237 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1238
1239 void
1240 set_abiversion(int ver)
1241 {
1242 elfcpp::Elf_Word flags = this->processor_specific_flags();
1243 flags &= ~elfcpp::EF_PPC64_ABI;
1244 flags |= ver & elfcpp::EF_PPC64_ABI;
1245 this->set_processor_specific_flags(flags);
1246 }
1247
1248 Symbol*
1249 tls_get_addr_opt() const
1250 { return this->tls_get_addr_opt_; }
1251
1252 Symbol*
1253 tls_get_addr() const
1254 { return this->tls_get_addr_; }
1255
1256 // If optimizing __tls_get_addr calls, whether this is the
1257 // "__tls_get_addr" symbol.
1258 bool
1259 is_tls_get_addr_opt(const Symbol* gsym) const
1260 {
1261 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1262 || gsym == this->tls_get_addr_opt_);
1263 }
1264
1265 bool
1266 replace_tls_get_addr(const Symbol* gsym) const
1267 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1268
1269 void
1270 set_has_tls_get_addr_opt()
1271 { this->has_tls_get_addr_opt_ = true; }
1272
1273 // Offset to toc save stack slot
1274 int
1275 stk_toc() const
1276 { return this->abiversion() < 2 ? 40 : 24; }
1277
1278 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1279 // so use the CR save slot. Used only by __tls_get_addr call stub,
1280 // relying on __tls_get_addr not saving CR itself.
1281 int
1282 stk_linker() const
1283 { return this->abiversion() < 2 ? 32 : 8; }
1284
1285 // Merge object attributes from input object with those in the output.
1286 void
1287 merge_object_attributes(const Object*, const Attributes_section_data*);
1288
1289 bool
1290 symval_for_branch(const Symbol_table* symtab,
1291 const Sized_symbol<size>* gsym,
1292 Powerpc_relobj<size, big_endian>* object,
1293 Address *value, unsigned int *dest_shndx);
1294
1295 private:
1296
1297 class Track_tls
1298 {
1299 public:
1300 enum Tls_get_addr
1301 {
1302 NOT_EXPECTED = 0,
1303 EXPECTED = 1,
1304 SKIP = 2,
1305 NORMAL = 3
1306 };
1307
1308 Track_tls()
1309 : tls_get_addr_state_(NOT_EXPECTED),
1310 relinfo_(NULL), relnum_(0), r_offset_(0)
1311 { }
1312
1313 ~Track_tls()
1314 {
1315 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1316 this->missing();
1317 }
1318
1319 void
1320 missing(void)
1321 {
1322 if (this->relinfo_ != NULL)
1323 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1324 _("missing expected __tls_get_addr call"));
1325 }
1326
1327 void
1328 expect_tls_get_addr_call(
1329 const Relocate_info<size, big_endian>* relinfo,
1330 size_t relnum,
1331 Address r_offset)
1332 {
1333 this->tls_get_addr_state_ = EXPECTED;
1334 this->relinfo_ = relinfo;
1335 this->relnum_ = relnum;
1336 this->r_offset_ = r_offset;
1337 }
1338
1339 void
1340 expect_tls_get_addr_call()
1341 { this->tls_get_addr_state_ = EXPECTED; }
1342
1343 void
1344 skip_next_tls_get_addr_call()
1345 {this->tls_get_addr_state_ = SKIP; }
1346
1347 Tls_get_addr
1348 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1349 unsigned int r_type, const Symbol* gsym)
1350 {
1351 bool is_tls_call
1352 = ((r_type == elfcpp::R_POWERPC_REL24
1353 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1354 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1355 || r_type == elfcpp::R_PPC_PLTREL24
1356 || is_plt16_reloc<size>(r_type)
1357 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1358 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1359 || r_type == elfcpp::R_POWERPC_PLTSEQ
1360 || r_type == elfcpp::R_POWERPC_PLTCALL
1361 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1362 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1363 && gsym != NULL
1364 && (gsym == target->tls_get_addr()
1365 || gsym == target->tls_get_addr_opt()));
1366 Tls_get_addr last_tls = this->tls_get_addr_state_;
1367 this->tls_get_addr_state_ = NOT_EXPECTED;
1368 if (is_tls_call && last_tls != EXPECTED)
1369 return last_tls;
1370 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1371 {
1372 this->missing();
1373 return EXPECTED;
1374 }
1375 return NORMAL;
1376 }
1377
1378 private:
1379 // What we're up to regarding calls to __tls_get_addr.
1380 // On powerpc, the branch and link insn making a call to
1381 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1382 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1383 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1384 // The marker relocation always comes first, and has the same
1385 // symbol as the reloc on the insn setting up the __tls_get_addr
1386 // argument. This ties the arg setup insn with the call insn,
1387 // allowing ld to safely optimize away the call. We check that
1388 // every call to __tls_get_addr has a marker relocation, and that
1389 // every marker relocation is on a call to __tls_get_addr.
1390 Tls_get_addr tls_get_addr_state_;
1391 // Info about the last reloc for error message.
1392 const Relocate_info<size, big_endian>* relinfo_;
1393 size_t relnum_;
1394 Address r_offset_;
1395 };
1396
1397 // The class which scans relocations.
1398 class Scan : protected Track_tls
1399 {
1400 public:
1401 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1402
1403 Scan()
1404 : Track_tls(), issued_non_pic_error_(false)
1405 { }
1406
1407 static inline int
1408 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1409
1410 inline void
1411 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1412 Sized_relobj_file<size, big_endian>* object,
1413 unsigned int data_shndx,
1414 Output_section* output_section,
1415 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1416 const elfcpp::Sym<size, big_endian>& lsym,
1417 bool is_discarded);
1418
1419 inline void
1420 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1421 Sized_relobj_file<size, big_endian>* object,
1422 unsigned int data_shndx,
1423 Output_section* output_section,
1424 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1425 Symbol* gsym);
1426
1427 inline bool
1428 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1429 Target_powerpc* ,
1430 Sized_relobj_file<size, big_endian>* relobj,
1431 unsigned int ,
1432 Output_section* ,
1433 const elfcpp::Rela<size, big_endian>& ,
1434 unsigned int r_type,
1435 const elfcpp::Sym<size, big_endian>&)
1436 {
1437 // PowerPC64 .opd is not folded, so any identical function text
1438 // may be folded and we'll still keep function addresses distinct.
1439 // That means no reloc is of concern here.
1440 if (size == 64)
1441 {
1442 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1443 <Powerpc_relobj<size, big_endian>*>(relobj);
1444 if (ppcobj->abiversion() == 1)
1445 return false;
1446 }
1447 // For 32-bit and ELFv2, conservatively assume anything but calls to
1448 // function code might be taking the address of the function.
1449 return !is_branch_reloc<size>(r_type);
1450 }
1451
1452 inline bool
1453 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1454 Target_powerpc* ,
1455 Sized_relobj_file<size, big_endian>* relobj,
1456 unsigned int ,
1457 Output_section* ,
1458 const elfcpp::Rela<size, big_endian>& ,
1459 unsigned int r_type,
1460 Symbol*)
1461 {
1462 // As above.
1463 if (size == 64)
1464 {
1465 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1466 <Powerpc_relobj<size, big_endian>*>(relobj);
1467 if (ppcobj->abiversion() == 1)
1468 return false;
1469 }
1470 return !is_branch_reloc<size>(r_type);
1471 }
1472
1473 static bool
1474 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1475 Sized_relobj_file<size, big_endian>* object,
1476 unsigned int r_type, bool report_err);
1477
1478 private:
1479 static void
1480 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1481 unsigned int r_type);
1482
1483 static void
1484 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1485 unsigned int r_type, Symbol*);
1486
1487 static void
1488 generate_tls_call(Symbol_table* symtab, Layout* layout,
1489 Target_powerpc* target);
1490
1491 void
1492 check_non_pic(Relobj*, unsigned int r_type);
1493
1494 // Whether we have issued an error about a non-PIC compilation.
1495 bool issued_non_pic_error_;
1496 };
1497
1498 // The class which implements relocation.
1499 class Relocate : protected Track_tls
1500 {
1501 public:
1502 // Use 'at' branch hints when true, 'y' when false.
1503 // FIXME maybe: set this with an option.
1504 static const bool is_isa_v2 = true;
1505
1506 Relocate()
1507 : Track_tls()
1508 { }
1509
1510 // Do a relocation. Return false if the caller should not issue
1511 // any warnings about this relocation.
1512 inline bool
1513 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1514 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1515 const Sized_symbol<size>*, const Symbol_value<size>*,
1516 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1517 section_size_type);
1518 };
1519
1520 class Relocate_comdat_behavior
1521 {
1522 public:
1523 // Decide what the linker should do for relocations that refer to
1524 // discarded comdat sections.
1525 inline Comdat_behavior
1526 get(const char* name)
1527 {
1528 gold::Default_comdat_behavior default_behavior;
1529 Comdat_behavior ret = default_behavior.get(name);
1530 if (ret == CB_ERROR)
1531 {
1532 if (size == 32
1533 && (strcmp(name, ".fixup") == 0
1534 || strcmp(name, ".got2") == 0))
1535 ret = CB_IGNORE;
1536 if (size == 64
1537 && (strcmp(name, ".opd") == 0
1538 || strcmp(name, ".toc") == 0
1539 || strcmp(name, ".toc1") == 0))
1540 ret = CB_IGNORE;
1541 }
1542 return ret;
1543 }
1544 };
1545
1546 // Optimize the TLS relocation type based on what we know about the
1547 // symbol. IS_FINAL is true if the final address of this symbol is
1548 // known at link time.
1549
1550 tls::Tls_optimization
1551 optimize_tls_gd(bool is_final)
1552 {
1553 // If we are generating a shared library, then we can't do anything
1554 // in the linker.
1555 if (parameters->options().shared()
1556 || !parameters->options().tls_optimize())
1557 return tls::TLSOPT_NONE;
1558
1559 if (!is_final)
1560 return tls::TLSOPT_TO_IE;
1561 return tls::TLSOPT_TO_LE;
1562 }
1563
1564 tls::Tls_optimization
1565 optimize_tls_ld()
1566 {
1567 if (parameters->options().shared()
1568 || !parameters->options().tls_optimize())
1569 return tls::TLSOPT_NONE;
1570
1571 return tls::TLSOPT_TO_LE;
1572 }
1573
1574 tls::Tls_optimization
1575 optimize_tls_ie(bool is_final)
1576 {
1577 if (!is_final
1578 || parameters->options().shared()
1579 || !parameters->options().tls_optimize())
1580 return tls::TLSOPT_NONE;
1581
1582 return tls::TLSOPT_TO_LE;
1583 }
1584
1585 // Create glink.
1586 void
1587 make_glink_section(Layout*);
1588
1589 // Create the PLT section.
1590 void
1591 make_plt_section(Symbol_table*, Layout*);
1592
1593 void
1594 make_iplt_section(Symbol_table*, Layout*);
1595
1596 void
1597 make_lplt_section(Symbol_table*, Layout*);
1598
1599 void
1600 make_brlt_section(Layout*);
1601
1602 // Create a PLT entry for a global symbol.
1603 void
1604 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1605
1606 // Create a PLT entry for a local IFUNC symbol.
1607 void
1608 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1609 Sized_relobj_file<size, big_endian>*,
1610 unsigned int);
1611
1612 // Create a PLT entry for a local non-IFUNC symbol.
1613 void
1614 make_local_plt_entry(Symbol_table*, Layout*,
1615 Sized_relobj_file<size, big_endian>*,
1616 unsigned int);
1617
1618 void
1619 make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
1620
1621 // Create a GOT entry for local dynamic __tls_get_addr.
1622 unsigned int
1623 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1624 Sized_relobj_file<size, big_endian>* object);
1625
1626 unsigned int
1627 tlsld_got_offset() const
1628 {
1629 return this->tlsld_got_offset_;
1630 }
1631
1632 // Get the dynamic reloc section, creating it if necessary.
1633 Reloc_section*
1634 rela_dyn_section(Layout*);
1635
1636 // Similarly, but for ifunc symbols get the one for ifunc.
1637 Reloc_section*
1638 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1639
1640 // Copy a relocation against a global symbol.
1641 void
1642 copy_reloc(Symbol_table* symtab, Layout* layout,
1643 Sized_relobj_file<size, big_endian>* object,
1644 unsigned int shndx, Output_section* output_section,
1645 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1646 {
1647 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1648 this->copy_relocs_.copy_reloc(symtab, layout,
1649 symtab->get_sized_symbol<size>(sym),
1650 object, shndx, output_section,
1651 r_type, reloc.get_r_offset(),
1652 reloc.get_r_addend(),
1653 this->rela_dyn_section(layout));
1654 }
1655
1656 // Look over all the input sections, deciding where to place stubs.
1657 void
1658 group_sections(Layout*, const Task*, bool);
1659
1660 // Sort output sections by address.
1661 struct Sort_sections
1662 {
1663 bool
1664 operator()(const Output_section* sec1, const Output_section* sec2)
1665 { return sec1->address() < sec2->address(); }
1666 };
1667
1668 class Branch_info
1669 {
1670 public:
1671 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1672 unsigned int data_shndx,
1673 Address r_offset,
1674 unsigned int r_type,
1675 unsigned int r_sym,
1676 Address addend)
1677 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1678 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1679 { }
1680
1681 ~Branch_info()
1682 { }
1683
1684 // Return whether this branch is going via a plt call stub, and if
1685 // so, mark it as having an R_PPC64_TOCSAVE.
1686 bool
1687 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1688 unsigned int shndx, Address offset,
1689 Target_powerpc* target, Symbol_table* symtab);
1690
1691 // If this branch needs a plt call stub, or a long branch stub, make one.
1692 bool
1693 make_stub(Stub_table<size, big_endian>*,
1694 Stub_table<size, big_endian>*,
1695 Symbol_table*) const;
1696
1697 private:
1698 // The branch location..
1699 Powerpc_relobj<size, big_endian>* object_;
1700 unsigned int shndx_;
1701 Address offset_;
1702 // ..and the branch type and destination.
1703 unsigned int r_type_ : 31;
1704 unsigned int tocsave_ : 1;
1705 unsigned int r_sym_;
1706 Address addend_;
1707 };
1708
1709 // Information about this specific target which we pass to the
1710 // general Target structure.
1711 static Target::Target_info powerpc_info;
1712
1713 // The small GOT section used by ppc32, and by ppc64 for entries that
1714 // must be addresseed +/-32k from the got pointer.
1715 Output_data_got_powerpc<size, big_endian>* got_;
1716 // Another GOT section used for entries that can be addressed +/- 2G
1717 // from the got pointer.
1718 Output_data_got_powerpc<size, big_endian>* biggot_;
1719
1720 // The PLT section. This is a container for a table of addresses,
1721 // and their relocations. Each address in the PLT has a dynamic
1722 // relocation (R_*_JMP_SLOT) and each address will have a
1723 // corresponding entry in .glink for lazy resolution of the PLT.
1724 // ppc32 initialises the PLT to point at the .glink entry, while
1725 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1726 // linker adds a stub that loads the PLT entry into ctr then
1727 // branches to ctr. There may be more than one stub for each PLT
1728 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1729 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1730 Output_data_plt_powerpc<size, big_endian>* plt_;
1731 // The IPLT section. Like plt_, this is a container for a table of
1732 // addresses and their relocations, specifically for STT_GNU_IFUNC
1733 // functions that resolve locally (STT_GNU_IFUNC functions that
1734 // don't resolve locally go in PLT). Unlike plt_, these have no
1735 // entry in .glink for lazy resolution, and the relocation section
1736 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1737 // the relocation section may contain relocations against
1738 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1739 // relocation section will appear at the end of other dynamic
1740 // relocations, so that ld.so applies these relocations after other
1741 // dynamic relocations. In a static executable, the relocation
1742 // section is emitted and marked with __rela_iplt_start and
1743 // __rela_iplt_end symbols.
1744 Output_data_plt_powerpc<size, big_endian>* iplt_;
1745 // A PLT style section for local, non-ifunc symbols
1746 Output_data_plt_powerpc<size, big_endian>* lplt_;
1747 // Section holding long branch destinations.
1748 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1749 // The .glink section.
1750 Output_data_glink<size, big_endian>* glink_;
1751 // The dynamic reloc section.
1752 Reloc_section* rela_dyn_;
1753 // Relocs saved to avoid a COPY reloc.
1754 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1755 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1756 unsigned int tlsld_got_offset_;
1757
1758 Stub_tables stub_tables_;
1759 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1760 Branch_lookup_table branch_lookup_table_;
1761
1762 typedef std::vector<Branch_info> Branches;
1763 Branches branch_info_;
1764 Tocsave_loc tocsave_loc_;
1765
1766 off_t rela_dyn_size_;
1767
1768 bool power10_relocs_;
1769 bool plt_thread_safe_;
1770 bool plt_localentry0_;
1771 bool plt_localentry0_init_;
1772 bool has_localentry0_;
1773 bool has_tls_get_addr_opt_;
1774 bool no_tprel_opt_;
1775
1776 bool relax_failed_;
1777 int relax_fail_count_;
1778 int32_t stub_group_size_;
1779
1780 Output_data_save_res<size, big_endian> *savres_section_;
1781
1782 // The "__tls_get_addr" symbol, if present
1783 Symbol* tls_get_addr_;
1784 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1785 Symbol* tls_get_addr_opt_;
1786
1787 // Attributes in output.
1788 Attributes_section_data* attributes_section_data_;
1789
1790 // Last input file to change various attribute tags
1791 const char* last_fp_;
1792 const char* last_ld_;
1793 const char* last_vec_;
1794 const char* last_struct_;
1795 };
1796
1797 template<>
1798 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1799 {
1800 32, // size
1801 true, // is_big_endian
1802 elfcpp::EM_PPC, // machine_code
1803 false, // has_make_symbol
1804 false, // has_resolve
1805 false, // has_code_fill
1806 true, // is_default_stack_executable
1807 false, // can_icf_inline_merge_sections
1808 '\0', // wrap_char
1809 "/usr/lib/ld.so.1", // dynamic_linker
1810 0x10000000, // default_text_segment_address
1811 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1812 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1813 false, // isolate_execinstr
1814 0, // rosegment_gap
1815 elfcpp::SHN_UNDEF, // small_common_shndx
1816 elfcpp::SHN_UNDEF, // large_common_shndx
1817 0, // small_common_section_flags
1818 0, // large_common_section_flags
1819 NULL, // attributes_section
1820 NULL, // attributes_vendor
1821 "_start", // entry_symbol_name
1822 32, // hash_entry_size
1823 elfcpp::SHT_PROGBITS, // unwind_section_type
1824 };
1825
1826 template<>
1827 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1828 {
1829 32, // size
1830 false, // is_big_endian
1831 elfcpp::EM_PPC, // machine_code
1832 false, // has_make_symbol
1833 false, // has_resolve
1834 false, // has_code_fill
1835 true, // is_default_stack_executable
1836 false, // can_icf_inline_merge_sections
1837 '\0', // wrap_char
1838 "/usr/lib/ld.so.1", // dynamic_linker
1839 0x10000000, // default_text_segment_address
1840 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1841 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1842 false, // isolate_execinstr
1843 0, // rosegment_gap
1844 elfcpp::SHN_UNDEF, // small_common_shndx
1845 elfcpp::SHN_UNDEF, // large_common_shndx
1846 0, // small_common_section_flags
1847 0, // large_common_section_flags
1848 NULL, // attributes_section
1849 NULL, // attributes_vendor
1850 "_start", // entry_symbol_name
1851 32, // hash_entry_size
1852 elfcpp::SHT_PROGBITS, // unwind_section_type
1853 };
1854
1855 template<>
1856 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1857 {
1858 64, // size
1859 true, // is_big_endian
1860 elfcpp::EM_PPC64, // machine_code
1861 false, // has_make_symbol
1862 true, // has_resolve
1863 false, // has_code_fill
1864 false, // is_default_stack_executable
1865 false, // can_icf_inline_merge_sections
1866 '\0', // wrap_char
1867 "/usr/lib/ld.so.1", // dynamic_linker
1868 0x10000000, // default_text_segment_address
1869 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1870 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1871 false, // isolate_execinstr
1872 0, // rosegment_gap
1873 elfcpp::SHN_UNDEF, // small_common_shndx
1874 elfcpp::SHN_UNDEF, // large_common_shndx
1875 0, // small_common_section_flags
1876 0, // large_common_section_flags
1877 NULL, // attributes_section
1878 NULL, // attributes_vendor
1879 "_start", // entry_symbol_name
1880 32, // hash_entry_size
1881 elfcpp::SHT_PROGBITS, // unwind_section_type
1882 };
1883
1884 template<>
1885 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1886 {
1887 64, // size
1888 false, // is_big_endian
1889 elfcpp::EM_PPC64, // machine_code
1890 false, // has_make_symbol
1891 true, // has_resolve
1892 false, // has_code_fill
1893 false, // is_default_stack_executable
1894 false, // can_icf_inline_merge_sections
1895 '\0', // wrap_char
1896 "/usr/lib/ld.so.1", // dynamic_linker
1897 0x10000000, // default_text_segment_address
1898 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1899 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1900 false, // isolate_execinstr
1901 0, // rosegment_gap
1902 elfcpp::SHN_UNDEF, // small_common_shndx
1903 elfcpp::SHN_UNDEF, // large_common_shndx
1904 0, // small_common_section_flags
1905 0, // large_common_section_flags
1906 NULL, // attributes_section
1907 NULL, // attributes_vendor
1908 "_start", // entry_symbol_name
1909 32, // hash_entry_size
1910 elfcpp::SHT_PROGBITS, // unwind_section_type
1911 };
1912
1913 template<int size>
1914 inline bool
1915 is_branch_reloc(unsigned int r_type)
1916 {
1917 return (r_type == elfcpp::R_POWERPC_REL24
1918 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1919 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1920 || r_type == elfcpp::R_PPC_PLTREL24
1921 || r_type == elfcpp::R_PPC_LOCAL24PC
1922 || r_type == elfcpp::R_POWERPC_REL14
1923 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1924 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1925 || r_type == elfcpp::R_POWERPC_ADDR24
1926 || r_type == elfcpp::R_POWERPC_ADDR14
1927 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1928 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1929 }
1930
1931 // Reloc resolves to plt entry.
1932 template<int size>
1933 inline bool
1934 is_plt16_reloc(unsigned int r_type)
1935 {
1936 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1937 || r_type == elfcpp::R_POWERPC_PLT16_HI
1938 || r_type == elfcpp::R_POWERPC_PLT16_HA
1939 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1940 }
1941
1942 // GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
1943 inline bool
1944 is_got_reloc(unsigned int r_type)
1945 {
1946 return (r_type == elfcpp::R_POWERPC_GOT16
1947 || r_type == elfcpp::R_POWERPC_GOT16_LO
1948 || r_type == elfcpp::R_POWERPC_GOT16_HI
1949 || r_type == elfcpp::R_POWERPC_GOT16_HA
1950 || r_type == elfcpp::R_PPC64_GOT16_DS
1951 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1952 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1953 }
1954
1955 // If INSN is an opcode that may be used with an @tls operand, return
1956 // the transformed insn for TLS optimisation, otherwise return 0. If
1957 // REG is non-zero only match an insn with RB or RA equal to REG.
1958 uint32_t
1959 at_tls_transform(uint32_t insn, unsigned int reg)
1960 {
1961 if ((insn & (0x3f << 26)) != 31 << 26)
1962 return 0;
1963
1964 unsigned int rtra;
1965 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1966 rtra = insn & ((1 << 26) - (1 << 16));
1967 else if (((insn >> 16) & 0x1f) == reg)
1968 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1969 else
1970 return 0;
1971
1972 if ((insn & (0x3ff << 1)) == 266 << 1)
1973 // add -> addi
1974 insn = 14 << 26;
1975 else if ((insn & (0x1f << 1)) == 23 << 1
1976 && ((insn & (0x1f << 6)) < 14 << 6
1977 || ((insn & (0x1f << 6)) >= 16 << 6
1978 && (insn & (0x1f << 6)) < 24 << 6)))
1979 // load and store indexed -> dform
1980 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1981 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1982 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1983 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1984 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1985 // lwax -> lwa
1986 insn = (58 << 26) | 2;
1987 else
1988 return 0;
1989 insn |= rtra;
1990 return insn;
1991 }
1992
1993
1994 template<int size, bool big_endian>
1995 class Powerpc_relocate_functions
1996 {
1997 public:
1998 enum Overflow_check
1999 {
2000 CHECK_NONE,
2001 CHECK_SIGNED,
2002 CHECK_UNSIGNED,
2003 CHECK_BITFIELD,
2004 CHECK_LOW_INSN,
2005 CHECK_HIGH_INSN
2006 };
2007
2008 enum Status
2009 {
2010 STATUS_OK,
2011 STATUS_OVERFLOW
2012 };
2013
2014 private:
2015 typedef Powerpc_relocate_functions<size, big_endian> This;
2016 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2017 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
2018
2019 template<int valsize>
2020 static inline bool
2021 has_overflow_signed(Address value)
2022 {
2023 // limit = 1 << (valsize - 1) without shift count exceeding size of type
2024 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2025 limit <<= ((valsize - 1) >> 1);
2026 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2027 return value + limit > (limit << 1) - 1;
2028 }
2029
2030 template<int valsize>
2031 static inline bool
2032 has_overflow_unsigned(Address value)
2033 {
2034 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2035 limit <<= ((valsize - 1) >> 1);
2036 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2037 return value > (limit << 1) - 1;
2038 }
2039
2040 template<int valsize>
2041 static inline bool
2042 has_overflow_bitfield(Address value)
2043 {
2044 return (has_overflow_unsigned<valsize>(value)
2045 && has_overflow_signed<valsize>(value));
2046 }
2047
2048 template<int valsize>
2049 static inline Status
2050 overflowed(Address value, Overflow_check overflow)
2051 {
2052 if (overflow == CHECK_SIGNED)
2053 {
2054 if (has_overflow_signed<valsize>(value))
2055 return STATUS_OVERFLOW;
2056 }
2057 else if (overflow == CHECK_UNSIGNED)
2058 {
2059 if (has_overflow_unsigned<valsize>(value))
2060 return STATUS_OVERFLOW;
2061 }
2062 else if (overflow == CHECK_BITFIELD)
2063 {
2064 if (has_overflow_bitfield<valsize>(value))
2065 return STATUS_OVERFLOW;
2066 }
2067 return STATUS_OK;
2068 }
2069
2070 // Do a simple RELA relocation
2071 template<int fieldsize, int valsize>
2072 static inline Status
2073 rela(unsigned char* view, Address value, Overflow_check overflow)
2074 {
2075 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2076 Valtype* wv = reinterpret_cast<Valtype*>(view);
2077 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2078 return overflowed<valsize>(value, overflow);
2079 }
2080
2081 template<int fieldsize, int valsize>
2082 static inline Status
2083 rela(unsigned char* view,
2084 unsigned int right_shift,
2085 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2086 Address value,
2087 Overflow_check overflow)
2088 {
2089 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2090 Valtype* wv = reinterpret_cast<Valtype*>(view);
2091 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2092 if (overflow == CHECK_SIGNED)
2093 value = static_cast<SignedAddress>(value) >> right_shift;
2094 else
2095 value = value >> right_shift;
2096 Valtype reloc = value;
2097 val &= ~dst_mask;
2098 reloc &= dst_mask;
2099 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2100 return overflowed<valsize>(value, overflow);
2101 }
2102
2103 // Do a simple RELA relocation, unaligned.
2104 template<int fieldsize, int valsize>
2105 static inline Status
2106 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2107 {
2108 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2109 return overflowed<valsize>(value, overflow);
2110 }
2111
2112 template<int fieldsize, int valsize>
2113 static inline Status
2114 rela_ua(unsigned char* view,
2115 unsigned int right_shift,
2116 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2117 Address value,
2118 Overflow_check overflow)
2119 {
2120 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2121 Valtype;
2122 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2123 if (overflow == CHECK_SIGNED)
2124 value = static_cast<SignedAddress>(value) >> right_shift;
2125 else
2126 value = value >> right_shift;
2127 Valtype reloc = value;
2128 val &= ~dst_mask;
2129 reloc &= dst_mask;
2130 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2131 return overflowed<valsize>(value, overflow);
2132 }
2133
2134 public:
2135 // R_PPC64_ADDR64: (Symbol + Addend)
2136 static inline void
2137 addr64(unsigned char* view, Address value)
2138 { This::template rela<64,64>(view, value, CHECK_NONE); }
2139
2140 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2141 static inline void
2142 addr64_u(unsigned char* view, Address value)
2143 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2144
2145 // R_POWERPC_ADDR32: (Symbol + Addend)
2146 static inline Status
2147 addr32(unsigned char* view, Address value, Overflow_check overflow)
2148 { return This::template rela<32,32>(view, value, overflow); }
2149
2150 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2151 static inline Status
2152 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2153 { return This::template rela_ua<32,32>(view, value, overflow); }
2154
2155 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2156 static inline Status
2157 addr24(unsigned char* view, Address value, Overflow_check overflow)
2158 {
2159 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2160 value, overflow);
2161 if (overflow != CHECK_NONE && (value & 3) != 0)
2162 stat = STATUS_OVERFLOW;
2163 return stat;
2164 }
2165
2166 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2167 static inline Status
2168 addr16(unsigned char* view, Address value, Overflow_check overflow)
2169 { return This::template rela<16,16>(view, value, overflow); }
2170
2171 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2172 static inline Status
2173 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2174 { return This::template rela_ua<16,16>(view, value, overflow); }
2175
2176 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2177 static inline Status
2178 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2179 {
2180 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2181 if ((value & 3) != 0)
2182 stat = STATUS_OVERFLOW;
2183 return stat;
2184 }
2185
2186 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2187 static inline Status
2188 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2189 {
2190 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2191 if ((value & 15) != 0)
2192 stat = STATUS_OVERFLOW;
2193 return stat;
2194 }
2195
2196 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2197 static inline void
2198 addr16_hi(unsigned char* view, Address value)
2199 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2200
2201 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2202 static inline void
2203 addr16_ha(unsigned char* view, Address value)
2204 { This::addr16_hi(view, value + 0x8000); }
2205
2206 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2207 static inline void
2208 addr16_hi2(unsigned char* view, Address value)
2209 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2210
2211 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2212 static inline void
2213 addr16_ha2(unsigned char* view, Address value)
2214 { This::addr16_hi2(view, value + 0x8000); }
2215
2216 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2217 static inline void
2218 addr16_hi3(unsigned char* view, Address value)
2219 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2220
2221 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2222 static inline void
2223 addr16_ha3(unsigned char* view, Address value)
2224 { This::addr16_hi3(view, value + 0x8000); }
2225
2226 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2227 static inline Status
2228 addr14(unsigned char* view, Address value, Overflow_check overflow)
2229 {
2230 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2231 if (overflow != CHECK_NONE && (value & 3) != 0)
2232 stat = STATUS_OVERFLOW;
2233 return stat;
2234 }
2235
2236 // R_POWERPC_REL16DX_HA
2237 static inline Status
2238 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2239 {
2240 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2241 Valtype* wv = reinterpret_cast<Valtype*>(view);
2242 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2243 value += 0x8000;
2244 value = static_cast<SignedAddress>(value) >> 16;
2245 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2246 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2247 return overflowed<16>(value, overflow);
2248 }
2249
2250 // R_PPC64_D34
2251 static inline Status
2252 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2253 {
2254 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2255 value, overflow);
2256 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2257 return stat;
2258 }
2259
2260 // R_PPC64_D34_HI30
2261 static inline void
2262 addr34_hi(unsigned char *view, uint64_t value)
2263 { This::addr34(view, value >> 34, CHECK_NONE);}
2264
2265 // R_PPC64_D34_HA30
2266 static inline void
2267 addr34_ha(unsigned char *view, uint64_t value)
2268 { This::addr34_hi(view, value + (1ULL << 33));}
2269
2270 // R_PPC64_D28
2271 static inline Status
2272 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2273 {
2274 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2275 value, overflow);
2276 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2277 return stat;
2278 }
2279
2280 // R_PPC64_ADDR16_HIGHER34
2281 static inline void
2282 addr16_higher34(unsigned char* view, uint64_t value)
2283 { This::addr16(view, value >> 34, CHECK_NONE); }
2284
2285 // R_PPC64_ADDR16_HIGHERA34
2286 static inline void
2287 addr16_highera34(unsigned char* view, uint64_t value)
2288 { This::addr16_higher34(view, value + (1ULL << 33)); }
2289
2290 // R_PPC64_ADDR16_HIGHEST34
2291 static inline void
2292 addr16_highest34(unsigned char* view, uint64_t value)
2293 { This::addr16(view, value >> 50, CHECK_NONE); }
2294
2295 // R_PPC64_ADDR16_HIGHESTA34
2296 static inline void
2297 addr16_highesta34(unsigned char* view, uint64_t value)
2298 { This::addr16_highest34(view, value + (1ULL << 33)); }
2299 };
2300
2301 // Set ABI version for input and output.
2302
2303 template<int size, bool big_endian>
2304 void
2305 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2306 {
2307 this->e_flags_ |= ver;
2308 if (this->abiversion() != 0)
2309 {
2310 Target_powerpc<size, big_endian>* target =
2311 static_cast<Target_powerpc<size, big_endian>*>(
2312 parameters->sized_target<size, big_endian>());
2313 if (target->abiversion() == 0)
2314 target->set_abiversion(this->abiversion());
2315 else if (target->abiversion() != this->abiversion())
2316 gold_error(_("%s: ABI version %d is not compatible "
2317 "with ABI version %d output"),
2318 this->name().c_str(),
2319 this->abiversion(), target->abiversion());
2320
2321 }
2322 }
2323
2324 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2325 // relocatable object, if such sections exists.
2326
2327 template<int size, bool big_endian>
2328 bool
2329 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2330 Read_symbols_data* sd)
2331 {
2332 const unsigned char* const pshdrs = sd->section_headers->data();
2333 const unsigned char* namesu = sd->section_names->data();
2334 const char* names = reinterpret_cast<const char*>(namesu);
2335 section_size_type names_size = sd->section_names_size;
2336 const unsigned char* s;
2337
2338 s = this->template find_shdr<size, big_endian>(pshdrs,
2339 size == 32 ? ".got2" : ".opd",
2340 names, names_size, NULL);
2341 if (s != NULL)
2342 {
2343 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2344 this->special_ = ndx;
2345 if (size == 64)
2346 {
2347 if (this->abiversion() == 0)
2348 this->set_abiversion(1);
2349 else if (this->abiversion() > 1)
2350 gold_error(_("%s: .opd invalid in abiv%d"),
2351 this->name().c_str(), this->abiversion());
2352 }
2353 }
2354 if (size == 64)
2355 {
2356 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2357 names, names_size, NULL);
2358 if (s != NULL)
2359 {
2360 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2361 this->relatoc_ = ndx;
2362 typename elfcpp::Shdr<size, big_endian> shdr(s);
2363 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2364 }
2365 }
2366 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2367 }
2368
2369 // Examine .rela.opd to build info about function entry points.
2370
2371 template<int size, bool big_endian>
2372 void
2373 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2374 size_t reloc_count,
2375 const unsigned char* prelocs,
2376 const unsigned char* plocal_syms)
2377 {
2378 if (size == 64)
2379 {
2380 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2381 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2382 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2383 Address expected_off = 0;
2384 bool regular = true;
2385 unsigned int opd_ent_size = 0;
2386
2387 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2388 {
2389 Reltype reloc(prelocs);
2390 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2391 = reloc.get_r_info();
2392 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2393 if (r_type == elfcpp::R_PPC64_ADDR64)
2394 {
2395 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2396 typename elfcpp::Elf_types<size>::Elf_Addr value;
2397 bool is_ordinary;
2398 unsigned int shndx;
2399 if (r_sym < this->local_symbol_count())
2400 {
2401 typename elfcpp::Sym<size, big_endian>
2402 lsym(plocal_syms + r_sym * sym_size);
2403 shndx = lsym.get_st_shndx();
2404 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2405 value = lsym.get_st_value();
2406 }
2407 else
2408 shndx = this->symbol_section_and_value(r_sym, &value,
2409 &is_ordinary);
2410 this->set_opd_ent(reloc.get_r_offset(), shndx,
2411 value + reloc.get_r_addend());
2412 if (i == 2)
2413 {
2414 expected_off = reloc.get_r_offset();
2415 opd_ent_size = expected_off;
2416 }
2417 else if (expected_off != reloc.get_r_offset())
2418 regular = false;
2419 expected_off += opd_ent_size;
2420 }
2421 else if (r_type == elfcpp::R_PPC64_TOC)
2422 {
2423 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2424 regular = false;
2425 }
2426 else
2427 {
2428 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2429 this->name().c_str(), r_type);
2430 regular = false;
2431 }
2432 }
2433 if (reloc_count <= 2)
2434 opd_ent_size = this->section_size(this->opd_shndx());
2435 if (opd_ent_size != 24 && opd_ent_size != 16)
2436 regular = false;
2437 if (!regular)
2438 {
2439 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2440 this->name().c_str());
2441 opd_ent_size = 0;
2442 }
2443 }
2444 }
2445
2446 // Returns true if a code sequence loading the TOC entry at VALUE
2447 // relative to the TOC pointer can be converted into code calculating
2448 // a TOC pointer relative offset.
2449 // If so, the TOC pointer relative offset is stored to VALUE.
2450
2451 template<int size, bool big_endian>
2452 bool
2453 Powerpc_relobj<size, big_endian>::make_toc_relative(
2454 Target_powerpc<size, big_endian>* target,
2455 Address* value)
2456 {
2457 if (size != 64)
2458 return false;
2459
2460 // With -mcmodel=medium code it is quite possible to have
2461 // toc-relative relocs referring to objects outside the TOC.
2462 // Don't try to look at a non-existent TOC.
2463 if (this->toc_shndx() == 0
2464 || this->output_section(this->toc_shndx()) == 0)
2465 return false;
2466
2467 // Convert VALUE back to an address by adding got_base (see below),
2468 // then to an offset in the TOC by subtracting the TOC output
2469 // section address and the TOC output offset.
2470 Address off = (*value + target->toc_pointer()
2471 - this->output_section(this->toc_shndx())->address()
2472 - this->output_section_offset(this->toc_shndx()));
2473 // Is this offset in the TOC? -mcmodel=medium code may be using
2474 // TOC relative access to variables outside the TOC. Those of
2475 // course can't be optimized. We also don't try to optimize code
2476 // that is using a different object's TOC.
2477 if (off >= this->section_size(this->toc_shndx()))
2478 return false;
2479
2480 if (this->no_toc_opt(off))
2481 return false;
2482
2483 section_size_type vlen;
2484 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2485 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2486 // The TOC pointer
2487 Address got_base = target->toc_pointer();
2488 addr -= got_base;
2489 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2490 return false;
2491
2492 *value = addr;
2493 return true;
2494 }
2495
2496 template<int size, bool big_endian>
2497 bool
2498 Powerpc_relobj<size, big_endian>::make_got_relative(
2499 Target_powerpc<size, big_endian>* target,
2500 const Symbol_value<size>* psymval,
2501 Address addend,
2502 Address* value)
2503 {
2504 Address addr = psymval->value(this, addend);
2505 Address got_base = target->toc_pointer();
2506 addr -= got_base;
2507 if (addr + 0x80008000 > 0xffffffff)
2508 return false;
2509
2510 *value = addr;
2511 return true;
2512 }
2513
2514 // Perform the Sized_relobj_file method, then set up opd info from
2515 // .opd relocs.
2516
2517 template<int size, bool big_endian>
2518 void
2519 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2520 {
2521 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2522 if (size == 64)
2523 {
2524 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2525 p != rd->relocs.end();
2526 ++p)
2527 {
2528 if (p->data_shndx == this->opd_shndx())
2529 {
2530 uint64_t opd_size = this->section_size(this->opd_shndx());
2531 gold_assert(opd_size == static_cast<size_t>(opd_size));
2532 if (opd_size != 0)
2533 {
2534 this->init_opd(opd_size);
2535 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2536 rd->local_symbols->data());
2537 }
2538 break;
2539 }
2540 }
2541 }
2542 }
2543
2544 // Read the symbols then set up st_other vector.
2545
2546 template<int size, bool big_endian>
2547 void
2548 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2549 {
2550 this->base_read_symbols(sd);
2551 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2552 return;
2553 if (size == 64)
2554 {
2555 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2556 const unsigned char* const pshdrs = sd->section_headers->data();
2557 const unsigned int loccount = this->do_local_symbol_count();
2558 if (loccount != 0)
2559 {
2560 this->st_other_.resize(loccount);
2561 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2562 off_t locsize = loccount * sym_size;
2563 const unsigned int symtab_shndx = this->symtab_shndx();
2564 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2565 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2566 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2567 locsize, true, false);
2568 psyms += sym_size;
2569 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2570 {
2571 elfcpp::Sym<size, big_endian> sym(psyms);
2572 unsigned char st_other = sym.get_st_other();
2573 this->st_other_[i] = st_other;
2574 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2575 {
2576 if (this->abiversion() == 0)
2577 this->set_abiversion(2);
2578 else if (this->abiversion() < 2)
2579 gold_error(_("%s: local symbol %d has invalid st_other"
2580 " for ABI version 1"),
2581 this->name().c_str(), i);
2582 }
2583 }
2584 }
2585 }
2586
2587 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2588 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2589 bool merge_attributes = false;
2590 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2591 {
2592 elfcpp::Shdr<size, big_endian> shdr(ps);
2593 switch (shdr.get_sh_type())
2594 {
2595 case elfcpp::SHT_GNU_ATTRIBUTES:
2596 {
2597 gold_assert(this->attributes_section_data_ == NULL);
2598 section_offset_type section_offset = shdr.get_sh_offset();
2599 section_size_type section_size =
2600 convert_to_section_size_type(shdr.get_sh_size());
2601 const unsigned char* view =
2602 this->get_view(section_offset, section_size, true, false);
2603 this->attributes_section_data_ =
2604 new Attributes_section_data(view, section_size);
2605 }
2606 break;
2607
2608 case elfcpp::SHT_SYMTAB:
2609 {
2610 // Sometimes an object has no contents except the section
2611 // name string table and an empty symbol table with the
2612 // undefined symbol. We don't want to merge
2613 // processor-specific flags from such an object.
2614 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2615 elfcpp::Elf_sizes<size>::sym_size;
2616 if (shdr.get_sh_size() > sym_size)
2617 merge_attributes = true;
2618 }
2619 break;
2620
2621 case elfcpp::SHT_STRTAB:
2622 break;
2623
2624 default:
2625 merge_attributes = true;
2626 break;
2627 }
2628 }
2629
2630 if (!merge_attributes)
2631 {
2632 // Should rarely happen.
2633 delete this->attributes_section_data_;
2634 this->attributes_section_data_ = NULL;
2635 }
2636 }
2637
2638 template<int size, bool big_endian>
2639 void
2640 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2641 {
2642 this->e_flags_ |= ver;
2643 if (this->abiversion() != 0)
2644 {
2645 Target_powerpc<size, big_endian>* target =
2646 static_cast<Target_powerpc<size, big_endian>*>(
2647 parameters->sized_target<size, big_endian>());
2648 if (target->abiversion() == 0)
2649 target->set_abiversion(this->abiversion());
2650 else if (target->abiversion() != this->abiversion())
2651 gold_error(_("%s: ABI version %d is not compatible "
2652 "with ABI version %d output"),
2653 this->name().c_str(),
2654 this->abiversion(), target->abiversion());
2655
2656 }
2657 }
2658
2659 // Call Sized_dynobj::base_read_symbols to read the symbols then
2660 // read .opd from a dynamic object, filling in opd_ent_ vector,
2661
2662 template<int size, bool big_endian>
2663 void
2664 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2665 {
2666 this->base_read_symbols(sd);
2667 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2668 const unsigned char* ps =
2669 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2670 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2671 {
2672 elfcpp::Shdr<size, big_endian> shdr(ps);
2673 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2674 {
2675 section_offset_type section_offset = shdr.get_sh_offset();
2676 section_size_type section_size =
2677 convert_to_section_size_type(shdr.get_sh_size());
2678 const unsigned char* view =
2679 this->get_view(section_offset, section_size, true, false);
2680 this->attributes_section_data_ =
2681 new Attributes_section_data(view, section_size);
2682 break;
2683 }
2684 }
2685 if (size == 64)
2686 {
2687 const unsigned char* const pshdrs = sd->section_headers->data();
2688 const unsigned char* namesu = sd->section_names->data();
2689 const char* names = reinterpret_cast<const char*>(namesu);
2690 const unsigned char* s = NULL;
2691 const unsigned char* opd;
2692 section_size_type opd_size;
2693
2694 // Find and read .opd section.
2695 while (1)
2696 {
2697 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2698 sd->section_names_size,
2699 s);
2700 if (s == NULL)
2701 return;
2702
2703 typename elfcpp::Shdr<size, big_endian> shdr(s);
2704 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2705 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2706 {
2707 if (this->abiversion() == 0)
2708 this->set_abiversion(1);
2709 else if (this->abiversion() > 1)
2710 gold_error(_("%s: .opd invalid in abiv%d"),
2711 this->name().c_str(), this->abiversion());
2712
2713 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2714 this->opd_address_ = shdr.get_sh_addr();
2715 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2716 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2717 true, false);
2718 break;
2719 }
2720 }
2721
2722 // Build set of executable sections.
2723 // Using a set is probably overkill. There is likely to be only
2724 // a few executable sections, typically .init, .text and .fini,
2725 // and they are generally grouped together.
2726 typedef std::set<Sec_info> Exec_sections;
2727 Exec_sections exec_sections;
2728 s = pshdrs;
2729 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2730 {
2731 typename elfcpp::Shdr<size, big_endian> shdr(s);
2732 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2733 && ((shdr.get_sh_flags()
2734 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2735 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2736 && shdr.get_sh_size() != 0)
2737 {
2738 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2739 shdr.get_sh_size(), i));
2740 }
2741 }
2742 if (exec_sections.empty())
2743 return;
2744
2745 // Look over the OPD entries. This is complicated by the fact
2746 // that some binaries will use two-word entries while others
2747 // will use the standard three-word entries. In most cases
2748 // the third word (the environment pointer for languages like
2749 // Pascal) is unused and will be zero. If the third word is
2750 // used it should not be pointing into executable sections,
2751 // I think.
2752 this->init_opd(opd_size);
2753 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2754 {
2755 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2756 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2757 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2758 if (val == 0)
2759 // Chances are that this is the third word of an OPD entry.
2760 continue;
2761 typename Exec_sections::const_iterator e
2762 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2763 if (e != exec_sections.begin())
2764 {
2765 --e;
2766 if (e->start <= val && val < e->start + e->len)
2767 {
2768 // We have an address in an executable section.
2769 // VAL ought to be the function entry, set it up.
2770 this->set_opd_ent(p - opd, e->shndx, val);
2771 // Skip second word of OPD entry, the TOC pointer.
2772 p += 8;
2773 }
2774 }
2775 // If we didn't match any executable sections, we likely
2776 // have a non-zero third word in the OPD entry.
2777 }
2778 }
2779 }
2780
2781 // Relocate sections.
2782
2783 template<int size, bool big_endian>
2784 void
2785 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2786 const Symbol_table* symtab, const Layout* layout,
2787 const unsigned char* pshdrs, Output_file* of,
2788 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2789 {
2790 unsigned int start = 1;
2791 if (size == 64
2792 && this->relatoc_ != 0
2793 && !parameters->options().relocatable())
2794 {
2795 // Relocate .toc first.
2796 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2797 this->relatoc_, this->relatoc_);
2798 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2799 1, this->relatoc_ - 1);
2800 start = this->relatoc_ + 1;
2801 }
2802 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2803 start, this->shnum() - 1);
2804 }
2805
2806 // Set up some symbols.
2807
2808 template<int size, bool big_endian>
2809 void
2810 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2811 Symbol_table* symtab,
2812 Layout* layout)
2813 {
2814 if (size == 32)
2815 {
2816 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2817 // undefined when scanning relocs (and thus requires
2818 // non-relative dynamic relocs). The proper value will be
2819 // updated later.
2820 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2821 if (gotsym != NULL && gotsym->is_undefined())
2822 {
2823 Target_powerpc<size, big_endian>* target =
2824 static_cast<Target_powerpc<size, big_endian>*>(
2825 parameters->sized_target<size, big_endian>());
2826 Output_data_got_powerpc<size, big_endian>* got
2827 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2828 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2829 Symbol_table::PREDEFINED,
2830 got, 0, 0,
2831 elfcpp::STT_OBJECT,
2832 elfcpp::STB_LOCAL,
2833 elfcpp::STV_HIDDEN, 0,
2834 false, false);
2835 }
2836
2837 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2838 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2839 if (sdasym != NULL && sdasym->is_undefined())
2840 {
2841 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2842 Output_section* os
2843 = layout->add_output_section_data(".sdata", 0,
2844 elfcpp::SHF_ALLOC
2845 | elfcpp::SHF_WRITE,
2846 sdata, ORDER_SMALL_DATA, false);
2847 symtab->define_in_output_data("_SDA_BASE_", NULL,
2848 Symbol_table::PREDEFINED,
2849 os, 32768, 0, elfcpp::STT_OBJECT,
2850 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2851 0, false, false);
2852 }
2853 }
2854 else
2855 {
2856 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2857 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2858 if (gotsym != NULL && gotsym->is_undefined())
2859 {
2860 Target_powerpc<size, big_endian>* target =
2861 static_cast<Target_powerpc<size, big_endian>*>(
2862 parameters->sized_target<size, big_endian>());
2863 Output_data_got_powerpc<size, big_endian>* got
2864 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2865 symtab->define_in_output_data(".TOC.", NULL,
2866 Symbol_table::PREDEFINED,
2867 got, 0x8000, 0,
2868 elfcpp::STT_OBJECT,
2869 elfcpp::STB_LOCAL,
2870 elfcpp::STV_HIDDEN, 0,
2871 false, false);
2872 }
2873 }
2874
2875 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2876 if (parameters->options().tls_get_addr_optimize()
2877 && this->tls_get_addr_ != NULL
2878 && this->tls_get_addr_->in_reg())
2879 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2880 if (this->tls_get_addr_opt_ != NULL)
2881 {
2882 if (this->tls_get_addr_->is_undefined()
2883 || this->tls_get_addr_->is_from_dynobj())
2884 {
2885 // Make it seem as if references to __tls_get_addr are
2886 // really to __tls_get_addr_opt, so the latter symbol is
2887 // made dynamic, not the former.
2888 this->tls_get_addr_->clear_in_reg();
2889 this->tls_get_addr_opt_->set_in_reg();
2890 }
2891 // We have a non-dynamic definition for __tls_get_addr.
2892 // Make __tls_get_addr_opt the same, if it does not already have
2893 // a non-dynamic definition.
2894 else if (this->tls_get_addr_opt_->is_undefined()
2895 || this->tls_get_addr_opt_->is_from_dynobj())
2896 {
2897 Sized_symbol<size>* from
2898 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2899 Sized_symbol<size>* to
2900 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2901 symtab->clone<size>(to, from);
2902 }
2903 }
2904 }
2905
2906 // Set up PowerPC target specific relobj.
2907
2908 template<int size, bool big_endian>
2909 Object*
2910 Target_powerpc<size, big_endian>::do_make_elf_object(
2911 const std::string& name,
2912 Input_file* input_file,
2913 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2914 {
2915 int et = ehdr.get_e_type();
2916 // ET_EXEC files are valid input for --just-symbols/-R,
2917 // and we treat them as relocatable objects.
2918 if (et == elfcpp::ET_REL
2919 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2920 {
2921 Powerpc_relobj<size, big_endian>* obj =
2922 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2923 obj->setup();
2924 return obj;
2925 }
2926 else if (et == elfcpp::ET_DYN)
2927 {
2928 Powerpc_dynobj<size, big_endian>* obj =
2929 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2930 obj->setup();
2931 return obj;
2932 }
2933 else
2934 {
2935 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2936 return NULL;
2937 }
2938 }
2939
2940 template<int size, bool big_endian>
2941 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2942 {
2943 public:
2944 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2945 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2946
2947 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2948 Got_type got_type)
2949 : Output_data_got<size, big_endian>(),
2950 symtab_(symtab), layout_(layout),
2951 header_ent_cnt_(size == 32 ? 3 : 1),
2952 header_index_(size == 32 ? 0x2000 : -1u)
2953 {
2954 if (size == 64)
2955 this->set_addralign(256);
2956 if (size == 64 && (got_type & GOT_TYPE_SMALL))
2957 this->make_header();
2958 }
2959
2960 // Override all the Output_data_got methods we use so as to first call
2961 // reserve_ent().
2962 bool
2963 add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
2964 {
2965 this->reserve_ent();
2966 return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2967 addend);
2968 }
2969
2970 bool
2971 add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
2972 {
2973 this->reserve_ent();
2974 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2975 addend);
2976 }
2977
2978 bool
2979 add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2980 { return this->add_global_plt(gsym, got_type, addend); }
2981
2982 void
2983 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2984 Output_data_reloc_generic* rel_dyn,
2985 unsigned int r_type, uint64_t addend)
2986 {
2987 this->reserve_ent();
2988 Output_data_got<size, big_endian>::
2989 add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
2990 }
2991
2992 void
2993 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2994 Output_data_reloc_generic* rel_dyn,
2995 unsigned int r_type_1, unsigned int r_type_2,
2996 uint64_t addend)
2997 {
2998 if (gsym->has_got_offset(got_type))
2999 return;
3000
3001 this->reserve_ent(2);
3002 Output_data_got<size, big_endian>::
3003 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
3004 addend);
3005 }
3006
3007 bool
3008 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
3009 uint64_t addend)
3010 {
3011 this->reserve_ent();
3012 return Output_data_got<size, big_endian>::add_local(object, sym_index,
3013 got_type, addend);
3014 }
3015
3016 bool
3017 add_local_plt(Relobj* object, unsigned int sym_index,
3018 unsigned int got_type, uint64_t addend)
3019 {
3020 this->reserve_ent();
3021 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
3022 got_type, addend);
3023 }
3024
3025 bool
3026 add_local_tls(Relobj* object, unsigned int sym_index,
3027 unsigned int got_type, uint64_t addend)
3028 { return this->add_local_plt(object, sym_index, got_type, addend); }
3029
3030 void
3031 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3032 unsigned int got_type,
3033 Output_data_reloc_generic* rel_dyn,
3034 unsigned int r_type, uint64_t addend)
3035 {
3036 if (object->local_has_got_offset(sym_index, got_type, addend))
3037 return;
3038
3039 this->reserve_ent(2);
3040 Output_data_got<size, big_endian>::
3041 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
3042 }
3043
3044 unsigned int
3045 add_constant(Valtype constant)
3046 {
3047 this->reserve_ent();
3048 return Output_data_got<size, big_endian>::add_constant(constant);
3049 }
3050
3051 unsigned int
3052 add_constant_pair(Valtype c1, Valtype c2)
3053 {
3054 this->reserve_ent(2);
3055 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3056 }
3057
3058 // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
3059 unsigned int
3060 g_o_t() const
3061 {
3062 if (size == 32)
3063 return this->got_offset(this->header_index_);
3064 else if (this->header_index_ != -1u)
3065 return this->got_offset(this->header_index_) + 0x8000;
3066 else
3067 gold_unreachable();
3068 }
3069
3070 // Ensure our GOT has a header.
3071 void
3072 set_final_data_size()
3073 {
3074 if (size == 32 && this->header_ent_cnt_ != 0)
3075 this->make_header();
3076 Output_data_got<size, big_endian>::set_final_data_size();
3077 }
3078
3079 // First word of GOT header needs some values that are not
3080 // handled by Output_data_got so poke them in here.
3081 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3082 void
3083 do_write(Output_file* of)
3084 {
3085 if (this->header_index_ != -1u)
3086 {
3087 Valtype val = 0;
3088 if (size == 32 && this->layout_->dynamic_data() != NULL)
3089 val = this->layout_->dynamic_section()->address();
3090 if (size == 64)
3091 val = this->address() + this->g_o_t();
3092 this->replace_constant(this->header_index_, val);
3093 }
3094 Output_data_got<size, big_endian>::do_write(of);
3095 }
3096
3097 private:
3098 void
3099 reserve_ent(unsigned int cnt = 1)
3100 {
3101 if (size != 32 || this->header_ent_cnt_ == 0)
3102 return;
3103 if (this->num_entries() + cnt > this->header_index_)
3104 this->make_header();
3105 }
3106
3107 void
3108 make_header()
3109 {
3110 this->header_ent_cnt_ = 0;
3111 this->header_index_ = this->num_entries();
3112 if (size == 32)
3113 {
3114 Output_data_got<size, big_endian>::add_constant(0);
3115 Output_data_got<size, big_endian>::add_constant(0);
3116 Output_data_got<size, big_endian>::add_constant(0);
3117
3118 // Define _GLOBAL_OFFSET_TABLE_ at the header
3119 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3120 if (gotsym != NULL)
3121 {
3122 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3123 sym->set_value(this->g_o_t());
3124 }
3125 else
3126 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3127 Symbol_table::PREDEFINED,
3128 this, this->g_o_t(), 0,
3129 elfcpp::STT_OBJECT,
3130 elfcpp::STB_LOCAL,
3131 elfcpp::STV_HIDDEN, 0,
3132 false, false);
3133 }
3134 else
3135 Output_data_got<size, big_endian>::add_constant(0);
3136 }
3137
3138 // Stashed pointers.
3139 Symbol_table* symtab_;
3140 Layout* layout_;
3141
3142 // GOT header size.
3143 unsigned int header_ent_cnt_;
3144 // GOT header index.
3145 unsigned int header_index_;
3146 };
3147
3148 // Get the GOT section, creating it if necessary.
3149
3150 template<int size, bool big_endian>
3151 Output_data_got_powerpc<size, big_endian>*
3152 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3153 Layout* layout,
3154 Got_type got_type)
3155 {
3156 if (this->got_ == NULL)
3157 {
3158 gold_assert(symtab != NULL && layout != NULL);
3159
3160 this->got_
3161 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3162 GOT_TYPE_SMALL);
3163
3164 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3165 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3166 this->got_, ORDER_DATA, false);
3167 }
3168
3169 if (size == 32 || (got_type & GOT_TYPE_SMALL))
3170 return this->got_;
3171
3172 if (this->biggot_ == NULL)
3173 {
3174 this->biggot_
3175 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3176 GOT_TYPE_STANDARD);
3177
3178 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3179 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3180 this->biggot_, ORDER_DATA, false);
3181 }
3182
3183 return this->biggot_;
3184 }
3185
3186 // Get the dynamic reloc section, creating it if necessary.
3187
3188 template<int size, bool big_endian>
3189 typename Target_powerpc<size, big_endian>::Reloc_section*
3190 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3191 {
3192 if (this->rela_dyn_ == NULL)
3193 {
3194 gold_assert(layout != NULL);
3195 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3196 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3197 elfcpp::SHF_ALLOC, this->rela_dyn_,
3198 ORDER_DYNAMIC_RELOCS, false);
3199 }
3200 return this->rela_dyn_;
3201 }
3202
3203 // Similarly, but for ifunc symbols get the one for ifunc.
3204
3205 template<int size, bool big_endian>
3206 typename Target_powerpc<size, big_endian>::Reloc_section*
3207 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3208 Layout* layout,
3209 bool for_ifunc)
3210 {
3211 if (!for_ifunc)
3212 return this->rela_dyn_section(layout);
3213
3214 if (this->iplt_ == NULL)
3215 this->make_iplt_section(symtab, layout);
3216 return this->iplt_->rel_plt();
3217 }
3218
3219 class Stub_control
3220 {
3221 public:
3222 // Determine the stub group size. The group size is the absolute
3223 // value of the parameter --stub-group-size. If --stub-group-size
3224 // is passed a negative value, we restrict stubs to be always after
3225 // the stubbed branches.
3226 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3227 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3228 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3229 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3230 owner_(NULL), output_section_(NULL)
3231 {
3232 }
3233
3234 // Return true iff input section can be handled by current stub
3235 // group.
3236 bool
3237 can_add_to_stub_group(Output_section* o,
3238 const Output_section::Input_section* i,
3239 bool has14);
3240
3241 const Output_section::Input_section*
3242 owner()
3243 { return owner_; }
3244
3245 Output_section*
3246 output_section()
3247 { return output_section_; }
3248
3249 void
3250 set_output_and_owner(Output_section* o,
3251 const Output_section::Input_section* i)
3252 {
3253 this->output_section_ = o;
3254 this->owner_ = i;
3255 }
3256
3257 private:
3258 typedef enum
3259 {
3260 // Initial state.
3261 NO_GROUP,
3262 // Adding group sections before the stubs.
3263 FINDING_STUB_SECTION,
3264 // Adding group sections after the stubs.
3265 HAS_STUB_SECTION
3266 } State;
3267
3268 uint32_t stub_group_size_;
3269 bool stubs_always_after_branch_;
3270 bool suppress_size_errors_;
3271 // True if a stub group can serve multiple output sections.
3272 bool multi_os_;
3273 State state_;
3274 // Current max size of group. Starts at stub_group_size_ but is
3275 // reduced to stub_group_size_/1024 on seeing a section with
3276 // external conditional branches.
3277 uint32_t group_size_;
3278 uint64_t group_start_addr_;
3279 // owner_ and output_section_ specify the section to which stubs are
3280 // attached. The stubs are placed at the end of this section.
3281 const Output_section::Input_section* owner_;
3282 Output_section* output_section_;
3283 };
3284
3285 // Return true iff input section can be handled by current stub
3286 // group. Sections are presented to this function in order,
3287 // so the first section is the head of the group.
3288
3289 bool
3290 Stub_control::can_add_to_stub_group(Output_section* o,
3291 const Output_section::Input_section* i,
3292 bool has14)
3293 {
3294 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3295 uint64_t this_size;
3296 uint64_t start_addr = o->address();
3297
3298 if (whole_sec)
3299 // .init and .fini sections are pasted together to form a single
3300 // function. We can't be adding stubs in the middle of the function.
3301 this_size = o->data_size();
3302 else
3303 {
3304 start_addr += i->relobj()->output_section_offset(i->shndx());
3305 this_size = i->data_size();
3306 }
3307
3308 uint64_t end_addr = start_addr + this_size;
3309 uint32_t group_size = this->stub_group_size_;
3310 if (has14)
3311 this->group_size_ = group_size = group_size >> 10;
3312
3313 if (this_size > group_size && !this->suppress_size_errors_)
3314 gold_warning(_("%s:%s exceeds group size"),
3315 i->relobj()->name().c_str(),
3316 i->relobj()->section_name(i->shndx()).c_str());
3317
3318 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3319 has14 ? " 14bit" : "",
3320 i->relobj()->name().c_str(),
3321 i->relobj()->section_name(i->shndx()).c_str(),
3322 (long long) this_size,
3323 (this->state_ == NO_GROUP
3324 ? this_size
3325 : (long long) end_addr - this->group_start_addr_));
3326
3327 if (this->state_ == NO_GROUP)
3328 {
3329 // Only here on very first use of Stub_control
3330 this->owner_ = i;
3331 this->output_section_ = o;
3332 this->state_ = FINDING_STUB_SECTION;
3333 this->group_size_ = group_size;
3334 this->group_start_addr_ = start_addr;
3335 return true;
3336 }
3337 else if (!this->multi_os_ && this->output_section_ != o)
3338 ;
3339 else if (this->state_ == HAS_STUB_SECTION)
3340 {
3341 // Can we add this section, which is after the stubs, to the
3342 // group?
3343 if (end_addr - this->group_start_addr_ <= this->group_size_)
3344 return true;
3345 }
3346 else if (this->state_ == FINDING_STUB_SECTION)
3347 {
3348 if ((whole_sec && this->output_section_ == o)
3349 || end_addr - this->group_start_addr_ <= this->group_size_)
3350 {
3351 // Stubs are added at the end of "owner_".
3352 this->owner_ = i;
3353 this->output_section_ = o;
3354 return true;
3355 }
3356 // The group before the stubs has reached maximum size.
3357 // Now see about adding sections after the stubs to the
3358 // group. If the current section has a 14-bit branch and
3359 // the group before the stubs exceeds group_size_ (because
3360 // they didn't have 14-bit branches), don't add sections
3361 // after the stubs: The size of stubs for such a large
3362 // group may exceed the reach of a 14-bit branch.
3363 if (!this->stubs_always_after_branch_
3364 && this_size <= this->group_size_
3365 && start_addr - this->group_start_addr_ <= this->group_size_)
3366 {
3367 gold_debug(DEBUG_TARGET, "adding after stubs");
3368 this->state_ = HAS_STUB_SECTION;
3369 this->group_start_addr_ = start_addr;
3370 return true;
3371 }
3372 }
3373 else
3374 gold_unreachable();
3375
3376 gold_debug(DEBUG_TARGET,
3377 !this->multi_os_ && this->output_section_ != o
3378 ? "nope, new output section\n"
3379 : "nope, didn't fit\n");
3380
3381 // The section fails to fit in the current group. Set up a few
3382 // things for the next group. owner_ and output_section_ will be
3383 // set later after we've retrieved those values for the current
3384 // group.
3385 this->state_ = FINDING_STUB_SECTION;
3386 this->group_size_ = group_size;
3387 this->group_start_addr_ = start_addr;
3388 return false;
3389 }
3390
3391 // Look over all the input sections, deciding where to place stubs.
3392
3393 template<int size, bool big_endian>
3394 void
3395 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3396 const Task*,
3397 bool no_size_errors)
3398 {
3399 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3400 parameters->options().stub_group_multi());
3401
3402 // Group input sections and insert stub table
3403 Stub_table_owner* table_owner = NULL;
3404 std::vector<Stub_table_owner*> tables;
3405 Layout::Section_list section_list;
3406 layout->get_executable_sections(&section_list);
3407 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3408 for (Layout::Section_list::iterator o = section_list.begin();
3409 o != section_list.end();
3410 ++o)
3411 {
3412 typedef Output_section::Input_section_list Input_section_list;
3413 for (Input_section_list::const_iterator i
3414 = (*o)->input_sections().begin();
3415 i != (*o)->input_sections().end();
3416 ++i)
3417 {
3418 if (i->is_input_section()
3419 || i->is_relaxed_input_section())
3420 {
3421 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3422 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3423 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3424 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3425 {
3426 table_owner->output_section = stub_control.output_section();
3427 table_owner->owner = stub_control.owner();
3428 stub_control.set_output_and_owner(*o, &*i);
3429 table_owner = NULL;
3430 }
3431 if (table_owner == NULL)
3432 {
3433 table_owner = new Stub_table_owner;
3434 tables.push_back(table_owner);
3435 }
3436 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3437 }
3438 }
3439 }
3440 if (table_owner != NULL)
3441 {
3442 table_owner->output_section = stub_control.output_section();
3443 table_owner->owner = stub_control.owner();;
3444 }
3445 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3446 t != tables.end();
3447 ++t)
3448 {
3449 Stub_table<size, big_endian>* stub_table;
3450
3451 if ((*t)->owner->is_input_section())
3452 stub_table = new Stub_table<size, big_endian>(this,
3453 (*t)->output_section,
3454 (*t)->owner,
3455 this->stub_tables_.size());
3456 else if ((*t)->owner->is_relaxed_input_section())
3457 stub_table = static_cast<Stub_table<size, big_endian>*>(
3458 (*t)->owner->relaxed_input_section());
3459 else
3460 gold_unreachable();
3461 this->stub_tables_.push_back(stub_table);
3462 delete *t;
3463 }
3464 }
3465
3466 template<int size>
3467 static unsigned long
3468 max_branch_delta (unsigned int r_type)
3469 {
3470 if (r_type == elfcpp::R_POWERPC_REL14
3471 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3472 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3473 return 1L << 15;
3474 if (r_type == elfcpp::R_POWERPC_REL24
3475 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3476 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
3477 || r_type == elfcpp::R_PPC_PLTREL24
3478 || r_type == elfcpp::R_PPC_LOCAL24PC)
3479 return 1L << 25;
3480 return 0;
3481 }
3482
3483 // Return whether this branch is going via a plt call stub.
3484
3485 template<int size, bool big_endian>
3486 bool
3487 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3488 Powerpc_relobj<size, big_endian>* ppc_object,
3489 unsigned int shndx,
3490 Address offset,
3491 Target_powerpc* target,
3492 Symbol_table* symtab)
3493 {
3494 if (this->object_ != ppc_object
3495 || this->shndx_ != shndx
3496 || this->offset_ != offset)
3497 return false;
3498
3499 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3500 if (sym != NULL && sym->is_forwarder())
3501 sym = symtab->resolve_forwards(sym);
3502 if (target->replace_tls_get_addr(sym))
3503 sym = target->tls_get_addr_opt();
3504 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3505 if (gsym != NULL
3506 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3507 && !target->is_elfv2_localentry0(gsym))
3508 : (this->object_->local_has_plt_offset(this->r_sym_)
3509 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3510 {
3511 this->tocsave_ = 1;
3512 return true;
3513 }
3514 return false;
3515 }
3516
3517 // If this branch needs a plt call stub, or a long branch stub, make one.
3518
3519 template<int size, bool big_endian>
3520 bool
3521 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3522 Stub_table<size, big_endian>* stub_table,
3523 Stub_table<size, big_endian>* ifunc_stub_table,
3524 Symbol_table* symtab) const
3525 {
3526 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3527 Target_powerpc<size, big_endian>* target =
3528 static_cast<Target_powerpc<size, big_endian>*>(
3529 parameters->sized_target<size, big_endian>());
3530 if (sym != NULL && sym->is_forwarder())
3531 sym = symtab->resolve_forwards(sym);
3532 if (target->replace_tls_get_addr(sym))
3533 sym = target->tls_get_addr_opt();
3534 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3535 bool ok = true;
3536
3537 if (gsym != NULL
3538 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3539 : this->object_->local_has_plt_offset(this->r_sym_))
3540 {
3541 if (size == 64
3542 && gsym != NULL
3543 && target->abiversion() >= 2
3544 && !parameters->options().output_is_position_independent()
3545 && !is_branch_reloc<size>(this->r_type_))
3546 target->glink_section()->add_global_entry(gsym);
3547 else
3548 {
3549 if (stub_table == NULL
3550 && !(size == 32
3551 && gsym != NULL
3552 && !parameters->options().output_is_position_independent()
3553 && !is_branch_reloc<size>(this->r_type_)))
3554 stub_table = this->object_->stub_table(this->shndx_);
3555 if (stub_table == NULL)
3556 {
3557 // This is a ref from a data section to an ifunc symbol,
3558 // or a non-branch reloc for which we always want to use
3559 // one set of stubs for resolving function addresses.
3560 stub_table = ifunc_stub_table;
3561 }
3562 gold_assert(stub_table != NULL);
3563 Address from = this->object_->get_output_section_offset(this->shndx_);
3564 if (from != invalid_address)
3565 from += (this->object_->output_section(this->shndx_)->address()
3566 + this->offset_);
3567 if (gsym != NULL)
3568 ok = stub_table->add_plt_call_entry(from,
3569 this->object_, gsym,
3570 this->r_type_, this->addend_,
3571 this->tocsave_);
3572 else
3573 ok = stub_table->add_plt_call_entry(from,
3574 this->object_, this->r_sym_,
3575 this->r_type_, this->addend_,
3576 this->tocsave_);
3577 }
3578 }
3579 else
3580 {
3581 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3582 if (max_branch_offset == 0)
3583 return true;
3584 Address from = this->object_->get_output_section_offset(this->shndx_);
3585 gold_assert(from != invalid_address);
3586 from += (this->object_->output_section(this->shndx_)->address()
3587 + this->offset_);
3588 Address to;
3589 unsigned int other = 0;
3590 if (gsym != NULL)
3591 {
3592 switch (gsym->source())
3593 {
3594 case Symbol::FROM_OBJECT:
3595 {
3596 Object* symobj = gsym->object();
3597 if (symobj->is_dynamic()
3598 || symobj->pluginobj() != NULL)
3599 return true;
3600 bool is_ordinary;
3601 unsigned int shndx = gsym->shndx(&is_ordinary);
3602 if (shndx == elfcpp::SHN_UNDEF)
3603 return true;
3604 }
3605 break;
3606
3607 case Symbol::IS_UNDEFINED:
3608 return true;
3609
3610 default:
3611 break;
3612 }
3613 Symbol_table::Compute_final_value_status status;
3614 to = symtab->compute_final_value<size>(gsym, &status);
3615 if (status != Symbol_table::CFVS_OK)
3616 return true;
3617 if (size == 64)
3618 other = gsym->nonvis() >> 3;
3619 }
3620 else
3621 {
3622 const Symbol_value<size>* psymval
3623 = this->object_->local_symbol(this->r_sym_);
3624 Symbol_value<size> symval;
3625 if (psymval->is_section_symbol())
3626 symval.set_is_section_symbol();
3627 typedef Sized_relobj_file<size, big_endian> ObjType;
3628 typename ObjType::Compute_final_local_value_status status
3629 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3630 &symval, symtab);
3631 if (status != ObjType::CFLV_OK
3632 || !symval.has_output_value())
3633 return true;
3634 to = symval.value(this->object_, 0);
3635 if (size == 64)
3636 other = this->object_->st_other(this->r_sym_) >> 5;
3637 }
3638 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3639 to += this->addend_;
3640 if (stub_table == NULL)
3641 stub_table = this->object_->stub_table(this->shndx_);
3642 if (size == 64 && target->abiversion() < 2)
3643 {
3644 unsigned int dest_shndx;
3645 if (!target->symval_for_branch(symtab, gsym, this->object_,
3646 &to, &dest_shndx))
3647 return true;
3648 }
3649 unsigned int local_ent = 0;
3650 if (size == 64
3651 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
3652 && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
3653 local_ent = elfcpp::ppc64_decode_local_entry(other);
3654 Address delta = to + local_ent - from;
3655 if (delta + max_branch_offset >= 2 * max_branch_offset
3656 || (size == 64
3657 && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3658 || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
3659 && (gsym != NULL
3660 ? this->object_->ppc64_needs_toc(gsym)
3661 : this->object_->ppc64_needs_toc(this->r_sym_))))
3662 {
3663 if (stub_table == NULL)
3664 {
3665 gold_warning(_("%s:%s: branch in non-executable section,"
3666 " no long branch stub for you"),
3667 this->object_->name().c_str(),
3668 this->object_->section_name(this->shndx_).c_str());
3669 return true;
3670 }
3671 bool save_res = (size == 64
3672 && gsym != NULL
3673 && gsym->source() == Symbol::IN_OUTPUT_DATA
3674 && gsym->output_data() == target->savres_section());
3675 ok = stub_table->add_long_branch_entry(this->r_type_,
3676 from, to, other, save_res);
3677 }
3678 }
3679 if (!ok)
3680 gold_debug(DEBUG_TARGET,
3681 "branch at %s:%s+%#lx\n"
3682 "can't reach stub attached to %s:%s",
3683 this->object_->name().c_str(),
3684 this->object_->section_name(this->shndx_).c_str(),
3685 (unsigned long) this->offset_,
3686 stub_table->relobj()->name().c_str(),
3687 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3688
3689 return ok;
3690 }
3691
3692 // Helper for do_relax, avoiding checks that size, address and offset
3693 // are not set more than once.
3694
3695 static inline void
3696 update_current_size(Output_section_data_build* od, off_t cur_size)
3697 {
3698 od->reset_address_and_file_offset();
3699 od->set_current_data_size(cur_size);
3700 od->finalize_data_size();
3701 od->output_section()->set_section_offsets_need_adjustment();
3702 }
3703
3704 // Relaxation hook. This is where we do stub generation.
3705
3706 template<int size, bool big_endian>
3707 bool
3708 Target_powerpc<size, big_endian>::do_relax(int pass,
3709 const Input_objects*,
3710 Symbol_table* symtab,
3711 Layout* layout,
3712 const Task* task)
3713 {
3714 unsigned int prev_brlt_size = 0;
3715 if (pass == 1)
3716 {
3717 bool thread_safe
3718 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3719 if (size == 64
3720 && this->abiversion() < 2
3721 && !thread_safe
3722 && !parameters->options().user_set_plt_thread_safe())
3723 {
3724 static const char* const thread_starter[] =
3725 {
3726 "pthread_create",
3727 /* libstdc++ */
3728 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3729 /* librt */
3730 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3731 "mq_notify", "create_timer",
3732 /* libanl */
3733 "getaddrinfo_a",
3734 /* libgomp */
3735 "GOMP_parallel",
3736 "GOMP_parallel_start",
3737 "GOMP_parallel_loop_static",
3738 "GOMP_parallel_loop_static_start",
3739 "GOMP_parallel_loop_dynamic",
3740 "GOMP_parallel_loop_dynamic_start",
3741 "GOMP_parallel_loop_guided",
3742 "GOMP_parallel_loop_guided_start",
3743 "GOMP_parallel_loop_runtime",
3744 "GOMP_parallel_loop_runtime_start",
3745 "GOMP_parallel_sections",
3746 "GOMP_parallel_sections_start",
3747 /* libgo */
3748 "__go_go",
3749 };
3750
3751 if (parameters->options().shared())
3752 thread_safe = true;
3753 else
3754 {
3755 for (unsigned int i = 0;
3756 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3757 i++)
3758 {
3759 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3760 thread_safe = (sym != NULL
3761 && sym->in_reg()
3762 && sym->in_real_elf());
3763 if (thread_safe)
3764 break;
3765 }
3766 }
3767 }
3768 this->plt_thread_safe_ = thread_safe;
3769
3770 if (parameters->options().output_is_position_independent())
3771 this->rela_dyn_size_
3772 = this->rela_dyn_section(layout)->current_data_size();
3773
3774 this->stub_group_size_ = parameters->options().stub_group_size();
3775 bool no_size_errors = true;
3776 if (this->stub_group_size_ == 1)
3777 this->stub_group_size_ = 0x1c00000;
3778 else if (this->stub_group_size_ == -1)
3779 this->stub_group_size_ = -0x1e00000;
3780 else
3781 no_size_errors = false;
3782 this->group_sections(layout, task, no_size_errors);
3783 }
3784 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3785 {
3786 this->branch_lookup_table_.clear();
3787 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3788 p != this->stub_tables_.end();
3789 ++p)
3790 {
3791 (*p)->clear_stubs(true);
3792 }
3793 this->stub_tables_.clear();
3794 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3795 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3796 program_name, this->stub_group_size_);
3797 this->group_sections(layout, task, true);
3798 }
3799
3800 // We need address of stub tables valid for make_stub.
3801 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3802 p != this->stub_tables_.end();
3803 ++p)
3804 {
3805 const Powerpc_relobj<size, big_endian>* object
3806 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3807 Address off = object->get_output_section_offset((*p)->shndx());
3808 gold_assert(off != invalid_address);
3809 Output_section* os = (*p)->output_section();
3810 (*p)->set_address_and_size(os, off);
3811 }
3812
3813 if (pass != 1)
3814 {
3815 // Clear plt call stubs, long branch stubs and branch lookup table.
3816 prev_brlt_size = this->branch_lookup_table_.size();
3817 this->branch_lookup_table_.clear();
3818 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3819 p != this->stub_tables_.end();
3820 ++p)
3821 {
3822 (*p)->clear_stubs(false);
3823 }
3824 }
3825
3826 // Build all the stubs.
3827 this->relax_failed_ = false;
3828 Stub_table<size, big_endian>* ifunc_stub_table
3829 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3830 Stub_table<size, big_endian>* one_stub_table
3831 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3832 for (typename Branches::const_iterator b = this->branch_info_.begin();
3833 b != this->branch_info_.end();
3834 b++)
3835 {
3836 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3837 && !this->relax_failed_)
3838 {
3839 this->relax_failed_ = true;
3840 this->relax_fail_count_++;
3841 if (this->relax_fail_count_ < 3)
3842 return true;
3843 }
3844 }
3845 bool do_resize = false;
3846 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3847 p != this->stub_tables_.end();
3848 ++p)
3849 if ((*p)->need_resize())
3850 {
3851 do_resize = true;
3852 break;
3853 }
3854 if (do_resize)
3855 {
3856 this->branch_lookup_table_.clear();
3857 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3858 p != this->stub_tables_.end();
3859 ++p)
3860 (*p)->set_resizing(true);
3861 for (typename Branches::const_iterator b = this->branch_info_.begin();
3862 b != this->branch_info_.end();
3863 b++)
3864 {
3865 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3866 && !this->relax_failed_)
3867 {
3868 this->relax_failed_ = true;
3869 this->relax_fail_count_++;
3870 if (this->relax_fail_count_ < 3)
3871 return true;
3872 }
3873 }
3874 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3875 p != this->stub_tables_.end();
3876 ++p)
3877 (*p)->set_resizing(false);
3878 }
3879
3880 // Did anything change size?
3881 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3882 bool again = num_huge_branches != prev_brlt_size;
3883 if (size == 64 && num_huge_branches != 0)
3884 this->make_brlt_section(layout);
3885 if (size == 64 && again)
3886 {
3887 update_current_size(this->brlt_section_, num_huge_branches * 16);
3888 if (parameters->options().output_is_position_independent())
3889 {
3890 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3891 off_t cur = this->rela_dyn_size_ + num_huge_branches * reloc_size;
3892 update_current_size(this->rela_dyn_, cur);
3893 }
3894 }
3895
3896 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3897 p != this->stub_tables_.rend();
3898 ++p)
3899 (*p)->remove_eh_frame(layout);
3900
3901 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3902 p != this->stub_tables_.end();
3903 ++p)
3904 (*p)->add_eh_frame(layout);
3905
3906 typedef Unordered_set<Output_section*> Output_sections;
3907 Output_sections os_need_update;
3908 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3909 p != this->stub_tables_.end();
3910 ++p)
3911 {
3912 if ((*p)->size_update())
3913 {
3914 again = true;
3915 os_need_update.insert((*p)->output_section());
3916 }
3917 }
3918
3919 // Set output section offsets for all input sections in an output
3920 // section that just changed size. Anything past the stubs will
3921 // need updating.
3922 for (typename Output_sections::iterator p = os_need_update.begin();
3923 p != os_need_update.end();
3924 p++)
3925 {
3926 Output_section* os = *p;
3927 Address off = 0;
3928 typedef Output_section::Input_section_list Input_section_list;
3929 for (Input_section_list::const_iterator i = os->input_sections().begin();
3930 i != os->input_sections().end();
3931 ++i)
3932 {
3933 off = align_address(off, i->addralign());
3934 if (i->is_input_section() || i->is_relaxed_input_section())
3935 i->relobj()->set_section_offset(i->shndx(), off);
3936 if (i->is_relaxed_input_section())
3937 {
3938 Stub_table<size, big_endian>* stub_table
3939 = static_cast<Stub_table<size, big_endian>*>(
3940 i->relaxed_input_section());
3941 Address stub_table_size = stub_table->set_address_and_size(os, off);
3942 off += stub_table_size;
3943 // After a few iterations, set current stub table size
3944 // as min size threshold, so later stub tables can only
3945 // grow in size.
3946 if (pass >= 4)
3947 stub_table->set_min_size_threshold(stub_table_size);
3948 }
3949 else
3950 off += i->data_size();
3951 }
3952 // If .branch_lt is part of this output section, then we have
3953 // just done the offset adjustment.
3954 os->clear_section_offsets_need_adjustment();
3955 }
3956
3957 if (size == 64
3958 && !again
3959 && num_huge_branches != 0
3960 && parameters->options().output_is_position_independent())
3961 {
3962 // Fill in the BRLT relocs.
3963 this->rela_dyn_->reset_data_size();
3964 this->rela_dyn_->set_current_data_size(this->rela_dyn_size_);
3965 for (typename Branch_lookup_table::const_iterator p
3966 = this->branch_lookup_table_.begin();
3967 p != this->branch_lookup_table_.end();
3968 ++p)
3969 {
3970 this->rela_dyn_->add_relative(elfcpp::R_POWERPC_RELATIVE,
3971 this->brlt_section_, p->second,
3972 p->first);
3973 }
3974 this->rela_dyn_->finalize_data_size();
3975 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3976 gold_assert(this->rela_dyn_->data_size()
3977 == this->rela_dyn_size_ + num_huge_branches * reloc_size);
3978 }
3979
3980 if (!again
3981 && (parameters->options().user_set_emit_stub_syms()
3982 ? parameters->options().emit_stub_syms()
3983 : (size == 64
3984 || parameters->options().output_is_position_independent()
3985 || parameters->options().emit_relocs())))
3986 {
3987 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3988 p != this->stub_tables_.end();
3989 ++p)
3990 (*p)->define_stub_syms(symtab);
3991
3992 if (this->glink_ != NULL)
3993 {
3994 int stub_size = this->glink_->pltresolve_size();
3995 Address value = -stub_size;
3996 if (size == 64)
3997 {
3998 value = 8;
3999 stub_size -= 8;
4000 }
4001 this->define_local(symtab, "__glink_PLTresolve",
4002 this->glink_, value, stub_size);
4003
4004 if (size != 64)
4005 this->define_local(symtab, "__glink", this->glink_, 0, 0);
4006 }
4007 }
4008
4009 return again;
4010 }
4011
4012 template<int size, bool big_endian>
4013 void
4014 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
4015 unsigned char* oview,
4016 uint64_t* paddress,
4017 off_t* plen) const
4018 {
4019 uint64_t address = plt->address();
4020 off_t len = plt->data_size();
4021
4022 if (plt == this->glink_)
4023 {
4024 // See Output_data_glink::do_write() for glink contents.
4025 if (len == 0)
4026 {
4027 // Static linking may need stubs, to support ifunc and long
4028 // branches. We need to create an output section for
4029 // .eh_frame early in the link process, to have a place to
4030 // attach stub .eh_frame info. We also need to have
4031 // registered a CIE that matches the stub CIE. Both of
4032 // these requirements are satisfied by creating an FDE and
4033 // CIE for .glink, even though static linking will leave
4034 // .glink zero length.
4035 // ??? Hopefully generating an FDE with a zero address range
4036 // won't confuse anything that consumes .eh_frame info.
4037 }
4038 else if (size == 64)
4039 {
4040 // There is one word before __glink_PLTresolve
4041 address += 8;
4042 len -= 8;
4043 }
4044 else if (parameters->options().output_is_position_independent())
4045 {
4046 // There are two FDEs for a position independent glink.
4047 // The first covers the branch table, the second
4048 // __glink_PLTresolve at the end of glink.
4049 off_t resolve_size = this->glink_->pltresolve_size();
4050 if (oview[9] == elfcpp::DW_CFA_nop)
4051 len -= resolve_size;
4052 else
4053 {
4054 address += len - resolve_size;
4055 len = resolve_size;
4056 }
4057 }
4058 }
4059 else
4060 {
4061 // Must be a stub table.
4062 const Stub_table<size, big_endian>* stub_table
4063 = static_cast<const Stub_table<size, big_endian>*>(plt);
4064 uint64_t stub_address = stub_table->stub_address();
4065 len -= stub_address - address;
4066 address = stub_address;
4067 }
4068
4069 *paddress = address;
4070 *plen = len;
4071 }
4072
4073 // A class to handle the PLT data.
4074
4075 template<int size, bool big_endian>
4076 class Output_data_plt_powerpc : public Output_section_data_build
4077 {
4078 public:
4079 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4080 size, big_endian> Reloc_section;
4081
4082 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4083 Symbol_table* symtab,
4084 Reloc_section* plt_rel,
4085 const char* name)
4086 : Output_section_data_build(size == 32 ? 4 : 8),
4087 rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
4088 { }
4089
4090 // Add an entry to the PLT.
4091 void
4092 add_entry(Symbol*, bool = false);
4093
4094 void
4095 add_ifunc_entry(Symbol*);
4096
4097 void
4098 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4099
4100 void
4101 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4102
4103 // Return the .rela.plt section data.
4104 Reloc_section*
4105 rel_plt() const
4106 {
4107 return this->rel_;
4108 }
4109
4110 // Return the number of PLT entries.
4111 unsigned int
4112 entry_count() const
4113 {
4114 if (this->current_data_size() == 0)
4115 return 0;
4116 return ((this->current_data_size() - this->first_plt_entry_offset())
4117 / this->plt_entry_size());
4118 }
4119
4120 protected:
4121 void
4122 do_adjust_output_section(Output_section* os)
4123 {
4124 os->set_entsize(0);
4125 }
4126
4127 // Write to a map file.
4128 void
4129 do_print_to_mapfile(Mapfile* mapfile) const
4130 { mapfile->print_output_data(this, this->name_); }
4131
4132 private:
4133 struct Local_plt_ent
4134 {
4135 Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4136 { rsym_ = rsym; u.obj_ = obj; }
4137 Local_plt_ent(Symbol* sym)
4138 { rsym_ = -1u; u.gsym_ = sym; }
4139 ~Local_plt_ent()
4140 { }
4141
4142 unsigned int rsym_;
4143 union
4144 {
4145 Sized_relobj_file<size, big_endian>* obj_;
4146 Symbol* gsym_;
4147 } u;
4148 };
4149
4150 // Return the offset of the first non-reserved PLT entry.
4151 unsigned int
4152 first_plt_entry_offset() const
4153 {
4154 // IPLT and LPLT have no reserved entry.
4155 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4156 return 0;
4157 return this->targ_->first_plt_entry_offset();
4158 }
4159
4160 // Return the size of each PLT entry.
4161 unsigned int
4162 plt_entry_size() const
4163 {
4164 return this->targ_->plt_entry_size();
4165 }
4166
4167 // Write out the PLT data.
4168 void
4169 do_write(Output_file*);
4170
4171 // The reloc section.
4172 Reloc_section* rel_;
4173 // Allows access to .glink for do_write.
4174 Target_powerpc<size, big_endian>* targ_;
4175 Symbol_table* symtab_;
4176 // What to report in map file.
4177 const char *name_;
4178
4179 std::vector<Local_plt_ent> sym_ents_;
4180 };
4181
4182 // Add an entry to the PLT.
4183
4184 template<int size, bool big_endian>
4185 void
4186 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
4187 bool is_local)
4188 {
4189 if (!gsym->has_plt_offset())
4190 {
4191 section_size_type off = this->current_data_size();
4192 if (off == 0)
4193 off += this->first_plt_entry_offset();
4194 gsym->set_plt_offset(off);
4195 if (this->rel_)
4196 {
4197 if (is_local)
4198 {
4199 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4200 if (size == 64 && this->targ_->abiversion() < 2)
4201 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4202 this->rel_->add_symbolless_global_addend(gsym, dynrel,
4203 this, off, 0);
4204 }
4205 else
4206 {
4207 gsym->set_needs_dynsym_entry();
4208 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4209 this->rel_->add_global(gsym, dynrel, this, off, 0);
4210 }
4211 }
4212 off += this->plt_entry_size();
4213 this->set_current_data_size(off);
4214 if (is_local)
4215 {
4216 Local_plt_ent sym(gsym);
4217 this->sym_ents_.push_back(sym);
4218 }
4219 }
4220 }
4221
4222 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4223
4224 template<int size, bool big_endian>
4225 void
4226 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4227 {
4228 if (!gsym->has_plt_offset())
4229 {
4230 section_size_type off = this->current_data_size();
4231 gsym->set_plt_offset(off);
4232 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4233 if (size == 64 && this->targ_->abiversion() < 2)
4234 dynrel = elfcpp::R_PPC64_JMP_IREL;
4235 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4236 off += this->plt_entry_size();
4237 this->set_current_data_size(off);
4238 }
4239 }
4240
4241 // Add an entry for a local symbol to the PLT.
4242
4243 template<int size, bool big_endian>
4244 void
4245 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4246 Sized_relobj_file<size, big_endian>* relobj,
4247 unsigned int local_sym_index)
4248 {
4249 if (!relobj->local_has_plt_offset(local_sym_index))
4250 {
4251 section_size_type off = this->current_data_size();
4252 relobj->set_local_plt_offset(local_sym_index, off);
4253 if (this->rel_)
4254 {
4255 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4256 if (size == 64 && this->targ_->abiversion() < 2)
4257 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4258 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4259 dynrel, this, off, 0);
4260 }
4261 off += this->plt_entry_size();
4262 this->set_current_data_size(off);
4263 Local_plt_ent sym(relobj, local_sym_index);
4264 this->sym_ents_.push_back(sym);
4265 }
4266 }
4267
4268 // Add an entry for a local ifunc symbol to the IPLT.
4269
4270 template<int size, bool big_endian>
4271 void
4272 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4273 Sized_relobj_file<size, big_endian>* relobj,
4274 unsigned int local_sym_index)
4275 {
4276 if (!relobj->local_has_plt_offset(local_sym_index))
4277 {
4278 section_size_type off = this->current_data_size();
4279 relobj->set_local_plt_offset(local_sym_index, off);
4280 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4281 if (size == 64 && this->targ_->abiversion() < 2)
4282 dynrel = elfcpp::R_PPC64_JMP_IREL;
4283 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4284 this, off, 0);
4285 off += this->plt_entry_size();
4286 this->set_current_data_size(off);
4287 }
4288 }
4289
4290 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4291 static const uint32_t add_2_2_11 = 0x7c425a14;
4292 static const uint32_t add_2_2_12 = 0x7c426214;
4293 static const uint32_t add_3_3_2 = 0x7c631214;
4294 static const uint32_t add_3_3_13 = 0x7c636a14;
4295 static const uint32_t add_3_12_2 = 0x7c6c1214;
4296 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4297 static const uint32_t add_11_0_11 = 0x7d605a14;
4298 static const uint32_t add_11_2_11 = 0x7d625a14;
4299 static const uint32_t add_11_11_2 = 0x7d6b1214;
4300 static const uint32_t add_12_11_12 = 0x7d8b6214;
4301 static const uint32_t addi_0_12 = 0x380c0000;
4302 static const uint32_t addi_2_2 = 0x38420000;
4303 static const uint32_t addi_3_3 = 0x38630000;
4304 static const uint32_t addi_11_11 = 0x396b0000;
4305 static const uint32_t addi_12_1 = 0x39810000;
4306 static const uint32_t addi_12_11 = 0x398b0000;
4307 static const uint32_t addi_12_12 = 0x398c0000;
4308 static const uint32_t addis_0_2 = 0x3c020000;
4309 static const uint32_t addis_0_13 = 0x3c0d0000;
4310 static const uint32_t addis_2_12 = 0x3c4c0000;
4311 static const uint32_t addis_11_2 = 0x3d620000;
4312 static const uint32_t addis_11_11 = 0x3d6b0000;
4313 static const uint32_t addis_11_30 = 0x3d7e0000;
4314 static const uint32_t addis_12_1 = 0x3d810000;
4315 static const uint32_t addis_12_2 = 0x3d820000;
4316 static const uint32_t addis_12_11 = 0x3d8b0000;
4317 static const uint32_t addis_12_12 = 0x3d8c0000;
4318 static const uint32_t b = 0x48000000;
4319 static const uint32_t bcl_20_31 = 0x429f0005;
4320 static const uint32_t bctr = 0x4e800420;
4321 static const uint32_t bctrl = 0x4e800421;
4322 static const uint32_t beqlr = 0x4d820020;
4323 static const uint32_t blr = 0x4e800020;
4324 static const uint32_t bnectr_p4 = 0x4ce20420;
4325 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4326 static const uint32_t cmpldi_2_0 = 0x28220000;
4327 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4328 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4329 static const uint32_t cror_15_15_15 = 0x4def7b82;
4330 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4331 static const uint32_t ld_0_1 = 0xe8010000;
4332 static const uint32_t ld_0_11 = 0xe80b0000;
4333 static const uint32_t ld_0_12 = 0xe80c0000;
4334 static const uint32_t ld_2_1 = 0xe8410000;
4335 static const uint32_t ld_2_2 = 0xe8420000;
4336 static const uint32_t ld_2_11 = 0xe84b0000;
4337 static const uint32_t ld_2_12 = 0xe84c0000;
4338 static const uint32_t ld_11_1 = 0xe9610000;
4339 static const uint32_t ld_11_2 = 0xe9620000;
4340 static const uint32_t ld_11_3 = 0xe9630000;
4341 static const uint32_t ld_11_11 = 0xe96b0000;
4342 static const uint32_t ld_12_2 = 0xe9820000;
4343 static const uint32_t ld_12_3 = 0xe9830000;
4344 static const uint32_t ld_12_11 = 0xe98b0000;
4345 static const uint32_t ld_12_12 = 0xe98c0000;
4346 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4347 static const uint32_t lfd_0_1 = 0xc8010000;
4348 static const uint32_t li_0_0 = 0x38000000;
4349 static const uint32_t li_11_0 = 0x39600000;
4350 static const uint32_t li_12_0 = 0x39800000;
4351 static const uint32_t lis_0 = 0x3c000000;
4352 static const uint32_t lis_2 = 0x3c400000;
4353 static const uint32_t lis_11 = 0x3d600000;
4354 static const uint32_t lis_12 = 0x3d800000;
4355 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4356 static const uint32_t lwz_0_12 = 0x800c0000;
4357 static const uint32_t lwz_11_3 = 0x81630000;
4358 static const uint32_t lwz_11_11 = 0x816b0000;
4359 static const uint32_t lwz_11_30 = 0x817e0000;
4360 static const uint32_t lwz_12_3 = 0x81830000;
4361 static const uint32_t lwz_12_12 = 0x818c0000;
4362 static const uint32_t lwzu_0_12 = 0x840c0000;
4363 static const uint32_t mflr_0 = 0x7c0802a6;
4364 static const uint32_t mflr_11 = 0x7d6802a6;
4365 static const uint32_t mflr_12 = 0x7d8802a6;
4366 static const uint32_t mr_0_3 = 0x7c601b78;
4367 static const uint32_t mr_3_0 = 0x7c030378;
4368 static const uint32_t mtctr_0 = 0x7c0903a6;
4369 static const uint32_t mtctr_11 = 0x7d6903a6;
4370 static const uint32_t mtctr_12 = 0x7d8903a6;
4371 static const uint32_t mtlr_0 = 0x7c0803a6;
4372 static const uint32_t mtlr_11 = 0x7d6803a6;
4373 static const uint32_t mtlr_12 = 0x7d8803a6;
4374 static const uint32_t nop = 0x60000000;
4375 static const uint32_t ori_0_0_0 = 0x60000000;
4376 static const uint32_t ori_11_11_0 = 0x616b0000;
4377 static const uint32_t ori_12_12_0 = 0x618c0000;
4378 static const uint32_t oris_12_12_0 = 0x658c0000;
4379 static const uint32_t sldi_11_11_34 = 0x796b1746;
4380 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4381 static const uint32_t srdi_0_0_2 = 0x7800f082;
4382 static const uint32_t std_0_1 = 0xf8010000;
4383 static const uint32_t std_0_12 = 0xf80c0000;
4384 static const uint32_t std_2_1 = 0xf8410000;
4385 static const uint32_t std_11_1 = 0xf9610000;
4386 static const uint32_t stfd_0_1 = 0xd8010000;
4387 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4388 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4389 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4390 static const uint32_t xor_2_12_12 = 0x7d826278;
4391 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4392
4393 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4394 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4395 static const uint64_t pnop = 0x0700000000000000ULL;
4396
4397 // Write out the PLT.
4398
4399 template<int size, bool big_endian>
4400 void
4401 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4402 {
4403 if (!this->sym_ents_.empty()
4404 && !parameters->options().output_is_position_independent())
4405 {
4406 const section_size_type offset = this->offset();
4407 const section_size_type oview_size
4408 = convert_to_section_size_type(this->data_size());
4409 unsigned char* const oview = of->get_output_view(offset, oview_size);
4410 unsigned char* pov = oview;
4411 unsigned char* endpov = oview + oview_size;
4412
4413 for (typename std::vector<Local_plt_ent>::iterator e
4414 = this->sym_ents_.begin();
4415 e != this->sym_ents_.end();
4416 e++)
4417 {
4418 typename elfcpp::Elf_types<size>::Elf_Addr val;
4419 Sized_symbol<size>* gsym = NULL;
4420 Powerpc_relobj<size, big_endian>* obj = NULL;
4421 if (e->rsym_ == -1u)
4422 {
4423 gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4424 val = gsym->value();
4425 }
4426 else
4427 {
4428 obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4429 val = obj->local_symbol(e->rsym_)->value(obj, 0);
4430 }
4431 if (this->targ_->abiversion() >= 2)
4432 {
4433 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4434 pov += size / 8;
4435 }
4436 else
4437 {
4438 unsigned int shndx;
4439 this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4440 &val, &shndx);
4441 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4442 pov += size / 8;
4443 val = this->targ_->toc_pointer();
4444 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4445 pov += size / 8;
4446 if (this->plt_entry_size() > 16)
4447 {
4448 elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4449 pov += size / 8;
4450 }
4451 }
4452 }
4453 gold_assert(pov == endpov);
4454 }
4455
4456 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4457 {
4458 const section_size_type offset = this->offset();
4459 const section_size_type oview_size
4460 = convert_to_section_size_type(this->data_size());
4461 unsigned char* const oview = of->get_output_view(offset, oview_size);
4462 unsigned char* pov = oview;
4463 unsigned char* endpov = oview + oview_size;
4464
4465 // The address of the .glink branch table
4466 const Output_data_glink<size, big_endian>* glink
4467 = this->targ_->glink_section();
4468 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4469
4470 while (pov < endpov)
4471 {
4472 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4473 pov += 4;
4474 branch_tab += 4;
4475 }
4476
4477 of->write_output_view(offset, oview_size, oview);
4478 }
4479 }
4480
4481 // Create the PLT section.
4482
4483 template<int size, bool big_endian>
4484 void
4485 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4486 Layout* layout)
4487 {
4488 if (this->plt_ == NULL)
4489 {
4490 if (this->got_ == NULL)
4491 this->got_section(symtab, layout, GOT_TYPE_SMALL);
4492
4493 if (this->glink_ == NULL)
4494 make_glink_section(layout);
4495
4496 // Ensure that .rela.dyn always appears before .rela.plt This is
4497 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4498 // needs to include .rela.plt in its range.
4499 this->rela_dyn_section(layout);
4500
4501 Reloc_section* plt_rel = new Reloc_section(false);
4502 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4503 elfcpp::SHF_ALLOC, plt_rel,
4504 ORDER_DYNAMIC_PLT_RELOCS, false);
4505 this->plt_
4506 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
4507 "** PLT");
4508 layout->add_output_section_data(".plt",
4509 (size == 32
4510 ? elfcpp::SHT_PROGBITS
4511 : elfcpp::SHT_NOBITS),
4512 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4513 this->plt_,
4514 (size == 32
4515 ? ORDER_SMALL_DATA
4516 : ORDER_SMALL_BSS),
4517 false);
4518
4519 Output_section* rela_plt_os = plt_rel->output_section();
4520 rela_plt_os->set_info_section(this->plt_->output_section());
4521 }
4522 }
4523
4524 // Create the IPLT section.
4525
4526 template<int size, bool big_endian>
4527 void
4528 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4529 Layout* layout)
4530 {
4531 if (this->iplt_ == NULL)
4532 {
4533 this->make_plt_section(symtab, layout);
4534 this->make_lplt_section(symtab, layout);
4535
4536 Reloc_section* iplt_rel = new Reloc_section(false);
4537 if (this->rela_dyn_->output_section())
4538 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4539 this->iplt_
4540 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
4541 "** IPLT");
4542 if (this->plt_->output_section())
4543 this->plt_->output_section()->add_output_section_data(this->iplt_);
4544 }
4545 }
4546
4547 // Create the LPLT section.
4548
4549 template<int size, bool big_endian>
4550 void
4551 Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4552 Layout* layout)
4553 {
4554 if (this->lplt_ == NULL)
4555 {
4556 Reloc_section* lplt_rel = NULL;
4557 if (parameters->options().output_is_position_independent())
4558 lplt_rel = this->rela_dyn_section(layout);
4559 this->lplt_
4560 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
4561 "** LPLT");
4562 this->make_brlt_section(layout);
4563 if (this->brlt_section_ && this->brlt_section_->output_section())
4564 this->brlt_section_->output_section()
4565 ->add_output_section_data(this->lplt_);
4566 else
4567 layout->add_output_section_data(".branch_lt",
4568 elfcpp::SHT_PROGBITS,
4569 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4570 this->lplt_,
4571 ORDER_RELRO,
4572 true);
4573 }
4574 }
4575
4576 // A section for huge long branch addresses, similar to plt section.
4577
4578 template<int size, bool big_endian>
4579 class Output_data_brlt_powerpc : public Output_section_data_build
4580 {
4581 public:
4582 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4583 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4584 size, big_endian> Reloc_section;
4585
4586 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ)
4587 : Output_section_data_build(size == 32 ? 4 : 8),
4588 targ_(targ)
4589 { }
4590
4591 protected:
4592 void
4593 do_adjust_output_section(Output_section* os)
4594 {
4595 os->set_entsize(0);
4596 }
4597
4598 // Write to a map file.
4599 void
4600 do_print_to_mapfile(Mapfile* mapfile) const
4601 { mapfile->print_output_data(this, "** BRLT"); }
4602
4603 private:
4604 // Write out the BRLT data.
4605 void
4606 do_write(Output_file*);
4607
4608 Target_powerpc<size, big_endian>* targ_;
4609 };
4610
4611 // Make the branch lookup table section.
4612
4613 template<int size, bool big_endian>
4614 void
4615 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4616 {
4617 if (size == 64 && this->brlt_section_ == NULL)
4618 {
4619 bool is_pic = parameters->options().output_is_position_independent();
4620 if (is_pic)
4621 {
4622 // When PIC we can't fill in .branch_lt but must initialise at
4623 // runtime via dynamic relocations.
4624 this->rela_dyn_section(layout);
4625 }
4626 this->brlt_section_
4627 = new Output_data_brlt_powerpc<size, big_endian>(this);
4628 if (this->plt_ && is_pic && this->plt_->output_section())
4629 this->plt_->output_section()
4630 ->add_output_section_data(this->brlt_section_);
4631 else
4632 layout->add_output_section_data(".branch_lt",
4633 elfcpp::SHT_PROGBITS,
4634 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4635 this->brlt_section_,
4636 ORDER_RELRO,
4637 true);
4638 }
4639 }
4640
4641 // Write out .branch_lt when non-PIC.
4642
4643 template<int size, bool big_endian>
4644 void
4645 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4646 {
4647 if (size == 64 && !parameters->options().output_is_position_independent())
4648 {
4649 const section_size_type offset = this->offset();
4650 const section_size_type oview_size
4651 = convert_to_section_size_type(this->data_size());
4652 unsigned char* const oview = of->get_output_view(offset, oview_size);
4653
4654 this->targ_->write_branch_lookup_table(oview);
4655 of->write_output_view(offset, oview_size, oview);
4656 }
4657 }
4658
4659 static inline uint32_t
4660 l(uint32_t a)
4661 {
4662 return a & 0xffff;
4663 }
4664
4665 static inline uint32_t
4666 hi(uint32_t a)
4667 {
4668 return l(a >> 16);
4669 }
4670
4671 static inline uint32_t
4672 ha(uint32_t a)
4673 {
4674 return hi(a + 0x8000);
4675 }
4676
4677 static inline uint64_t
4678 d34(uint64_t v)
4679 {
4680 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4681 }
4682
4683 static inline uint64_t
4684 ha34(uint64_t v)
4685 {
4686 return (v + (1ULL << 33)) >> 34;
4687 }
4688
4689 template<int size>
4690 struct Eh_cie
4691 {
4692 static const unsigned char eh_frame_cie[12];
4693 };
4694
4695 template<int size>
4696 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4697 {
4698 1, // CIE version.
4699 'z', 'R', 0, // Augmentation string.
4700 4, // Code alignment.
4701 0x80 - size / 8 , // Data alignment.
4702 65, // RA reg.
4703 1, // Augmentation size.
4704 (elfcpp::DW_EH_PE_pcrel
4705 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4706 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4707 };
4708
4709 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4710 static const unsigned char glink_eh_frame_fde_64v1[] =
4711 {
4712 0, 0, 0, 0, // Replaced with offset to .glink.
4713 0, 0, 0, 0, // Replaced with size of .glink.
4714 0, // Augmentation size.
4715 elfcpp::DW_CFA_advance_loc + 2,
4716 elfcpp::DW_CFA_register, 65, 12,
4717 elfcpp::DW_CFA_advance_loc + 4,
4718 elfcpp::DW_CFA_restore_extended, 65
4719 };
4720
4721 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4722 static const unsigned char glink_eh_frame_fde_64v2[] =
4723 {
4724 0, 0, 0, 0, // Replaced with offset to .glink.
4725 0, 0, 0, 0, // Replaced with size of .glink.
4726 0, // Augmentation size.
4727 elfcpp::DW_CFA_advance_loc + 2,
4728 elfcpp::DW_CFA_register, 65, 0,
4729 elfcpp::DW_CFA_advance_loc + 2,
4730 elfcpp::DW_CFA_restore_extended, 65
4731 };
4732
4733 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4734 {
4735 0, 0, 0, 0, // Replaced with offset to .glink.
4736 0, 0, 0, 0, // Replaced with size of .glink.
4737 0, // Augmentation size.
4738 elfcpp::DW_CFA_advance_loc + 3,
4739 elfcpp::DW_CFA_register, 65, 0,
4740 elfcpp::DW_CFA_advance_loc + 2,
4741 elfcpp::DW_CFA_restore_extended, 65
4742 };
4743
4744 // Describe __glink_PLTresolve use of LR, 32-bit version.
4745 static const unsigned char glink_eh_frame_fde_32[] =
4746 {
4747 0, 0, 0, 0, // Replaced with offset to .glink.
4748 0, 0, 0, 0, // Replaced with size of .glink.
4749 0, // Augmentation size.
4750 elfcpp::DW_CFA_advance_loc + 2,
4751 elfcpp::DW_CFA_register, 65, 0,
4752 elfcpp::DW_CFA_advance_loc + 4,
4753 elfcpp::DW_CFA_restore_extended, 65
4754 };
4755
4756 static const unsigned char default_fde[] =
4757 {
4758 0, 0, 0, 0, // Replaced with offset to stubs.
4759 0, 0, 0, 0, // Replaced with size of stubs.
4760 0, // Augmentation size.
4761 elfcpp::DW_CFA_nop, // Pad.
4762 elfcpp::DW_CFA_nop,
4763 elfcpp::DW_CFA_nop
4764 };
4765
4766 template<bool big_endian>
4767 static inline void
4768 write_insn(unsigned char* p, uint32_t v)
4769 {
4770 elfcpp::Swap<32, big_endian>::writeval(p, v);
4771 }
4772
4773 template<int size>
4774 static inline unsigned int
4775 param_plt_align()
4776 {
4777 if (!parameters->options().user_set_plt_align())
4778 return size == 64 ? 32 : 8;
4779 return 1 << parameters->options().plt_align();
4780 }
4781
4782 // Stub_table holds information about plt and long branch stubs.
4783 // Stubs are built in an area following some input section determined
4784 // by group_sections(). This input section is converted to a relaxed
4785 // input section allowing it to be resized to accommodate the stubs
4786
4787 template<int size, bool big_endian>
4788 class Stub_table : public Output_relaxed_input_section
4789 {
4790 public:
4791 struct Plt_stub_ent
4792 {
4793 Plt_stub_ent(unsigned int off, unsigned int indx)
4794 : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4795 toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
4796 { }
4797
4798 unsigned int off_;
4799 unsigned int indx_;
4800 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4801 // toc stub, p9off_ is offset to p9notoc stub
4802 unsigned int tocoff_ : 8;
4803 unsigned int p9off_ : 8;
4804 // The size of the toc stub, used to locate blr on tls_get_addr stub.
4805 unsigned int tsize_ : 8;
4806 // Stub revision management
4807 unsigned int iter_ : 1;
4808 // The three types of stubs.
4809 unsigned int toc_ : 1;
4810 unsigned int notoc_ : 1;
4811 unsigned int p9notoc_ : 1;
4812 // Each with a possible variant saving r2 first
4813 unsigned int r2save_ : 1;
4814 // Handy cached info from symbol
4815 unsigned int localentry0_ : 1;
4816 };
4817 struct Branch_stub_ent
4818 {
4819 Branch_stub_ent(unsigned int off)
4820 : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4821 p9notoc_(0), save_res_(0), other_(0)
4822 { }
4823
4824 unsigned int off_;
4825 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4826 // toc stub, p9off_ is offset to p9notoc stub
4827 unsigned int tocoff_ : 8;
4828 unsigned int p9off_ : 8;
4829 // Stub revision management
4830 unsigned int iter_ : 1;
4831 // Four types of stubs.
4832 unsigned int toc_ : 1;
4833 unsigned int notoc_ : 1;
4834 unsigned int p9notoc_ : 1;
4835 unsigned int save_res_ : 1;
4836 // Handy cached info from symbol
4837 unsigned int other_ : 3;
4838 };
4839 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4840 static const Address invalid_address = static_cast<Address>(0) - 1;
4841
4842 Stub_table(Target_powerpc<size, big_endian>* targ,
4843 Output_section* output_section,
4844 const Output_section::Input_section* owner,
4845 uint32_t id)
4846 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4847 owner->relobj()
4848 ->section_addralign(owner->shndx())),
4849 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4850 orig_data_size_(owner->current_data_size()),
4851 plt_size_(0), last_plt_size_(0),
4852 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4853 need_save_res_(false), need_resize_(false), resizing_(false),
4854 uniq_(id)
4855 {
4856 this->set_output_section(output_section);
4857
4858 std::vector<Output_relaxed_input_section*> new_relaxed;
4859 new_relaxed.push_back(this);
4860 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4861 }
4862
4863 // Add a plt call stub.
4864 bool
4865 add_plt_call_entry(Address,
4866 const Sized_relobj_file<size, big_endian>*,
4867 const Symbol*,
4868 unsigned int,
4869 Address,
4870 bool);
4871
4872 bool
4873 add_plt_call_entry(Address,
4874 const Sized_relobj_file<size, big_endian>*,
4875 unsigned int,
4876 unsigned int,
4877 Address,
4878 bool);
4879
4880 // Find a given plt call stub.
4881 const Plt_stub_ent*
4882 find_plt_call_entry(const Symbol*) const;
4883
4884 const Plt_stub_ent*
4885 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4886 unsigned int) const;
4887
4888 const Plt_stub_ent*
4889 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4890 const Symbol*,
4891 unsigned int,
4892 Address) const;
4893
4894 const Plt_stub_ent*
4895 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4896 unsigned int,
4897 unsigned int,
4898 Address) const;
4899
4900 // Add a long branch stub.
4901 bool
4902 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
4903
4904 const Branch_stub_ent*
4905 find_long_branch_entry(Address) const;
4906
4907 bool
4908 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4909 {
4910 Address max_branch_offset = max_branch_delta<size>(r_type);
4911 if (max_branch_offset == 0)
4912 return true;
4913 gold_assert(from != invalid_address);
4914 Address loc = off + this->stub_address();
4915 return loc - from + max_branch_offset < 2 * max_branch_offset;
4916 }
4917
4918 void
4919 clear_stubs(bool all)
4920 {
4921 this->plt_call_stubs_.clear();
4922 this->plt_size_ = 0;
4923 this->long_branch_stubs_.clear();
4924 this->branch_size_ = 0;
4925 this->need_save_res_ = false;
4926 if (all)
4927 {
4928 this->last_plt_size_ = 0;
4929 this->last_branch_size_ = 0;
4930 }
4931 }
4932
4933 bool
4934 need_resize() const
4935 { return need_resize_; }
4936
4937 void
4938 set_resizing(bool val)
4939 {
4940 this->resizing_ = val;
4941 if (val)
4942 {
4943 this->need_resize_ = false;
4944 this->plt_size_ = 0;
4945 this->branch_size_ = 0;
4946 this->need_save_res_ = false;
4947 }
4948 }
4949
4950 Address
4951 set_address_and_size(const Output_section* os, Address off)
4952 {
4953 Address start_off = off;
4954 off += this->orig_data_size_;
4955 Address my_size = this->plt_size_ + this->branch_size_;
4956 if (this->need_save_res_)
4957 my_size += this->targ_->savres_section()->data_size();
4958 if (my_size != 0)
4959 off = align_address(off, this->stub_align());
4960 // Include original section size and alignment padding in size
4961 my_size += off - start_off;
4962 // Ensure new size is always larger than min size
4963 // threshold. Alignment requirement is included in "my_size", so
4964 // increase "my_size" does not invalidate alignment.
4965 if (my_size < this->min_size_threshold_)
4966 my_size = this->min_size_threshold_;
4967 this->reset_address_and_file_offset();
4968 this->set_current_data_size(my_size);
4969 this->set_address_and_file_offset(os->address() + start_off,
4970 os->offset() + start_off);
4971 return my_size;
4972 }
4973
4974 Address
4975 stub_address() const
4976 {
4977 return align_address(this->address() + this->orig_data_size_,
4978 this->stub_align());
4979 }
4980
4981 Address
4982 stub_offset() const
4983 {
4984 return align_address(this->offset() + this->orig_data_size_,
4985 this->stub_align());
4986 }
4987
4988 section_size_type
4989 plt_size() const
4990 { return this->plt_size_; }
4991
4992 section_size_type
4993 branch_size() const
4994 { return this->branch_size_; }
4995
4996 void
4997 set_min_size_threshold(Address min_size)
4998 { this->min_size_threshold_ = min_size; }
4999
5000 void
5001 define_stub_syms(Symbol_table*);
5002
5003 bool
5004 size_update()
5005 {
5006 Output_section* os = this->output_section();
5007 if (os->addralign() < this->stub_align())
5008 {
5009 os->set_addralign(this->stub_align());
5010 // FIXME: get rid of the insane checkpointing.
5011 // We can't increase alignment of the input section to which
5012 // stubs are attached; The input section may be .init which
5013 // is pasted together with other .init sections to form a
5014 // function. Aligning might insert zero padding resulting in
5015 // sigill. However we do need to increase alignment of the
5016 // output section so that the align_address() on offset in
5017 // set_address_and_size() adds the same padding as the
5018 // align_address() on address in stub_address().
5019 // What's more, we need this alignment for the layout done in
5020 // relaxation_loop_body() so that the output section starts at
5021 // a suitably aligned address.
5022 os->checkpoint_set_addralign(this->stub_align());
5023 }
5024 if (this->last_plt_size_ != this->plt_size_
5025 || this->last_branch_size_ != this->branch_size_)
5026 {
5027 this->last_plt_size_ = this->plt_size_;
5028 this->last_branch_size_ = this->branch_size_;
5029 return true;
5030 }
5031 return false;
5032 }
5033
5034 // Add .eh_frame info for this stub section.
5035 void
5036 add_eh_frame(Layout* layout);
5037
5038 // Remove .eh_frame info for this stub section.
5039 void
5040 remove_eh_frame(Layout* layout);
5041
5042 Target_powerpc<size, big_endian>*
5043 targ() const
5044 { return targ_; }
5045
5046 private:
5047 class Plt_stub_key;
5048 class Plt_stub_key_hash;
5049 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5050 Plt_stub_key_hash> Plt_stub_entries;
5051 class Branch_stub_key;
5052 class Branch_stub_key_hash;
5053 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5054 Branch_stub_key_hash> Branch_stub_entries;
5055
5056 // Alignment of stub section.
5057 unsigned int
5058 stub_align() const
5059 {
5060 unsigned int min_align = size == 64 ? 32 : 16;
5061 unsigned int user_align = 1 << parameters->options().plt_align();
5062 return std::max(user_align, min_align);
5063 }
5064
5065 // Return the plt offset for the given call stub.
5066 Address
5067 plt_off(typename Plt_stub_entries::const_iterator p,
5068 const Output_data_plt_powerpc<size, big_endian>** sec) const
5069 {
5070 const Symbol* gsym = p->first.sym_;
5071 if (gsym != NULL)
5072 return this->targ_->plt_off(gsym, sec);
5073 else
5074 {
5075 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5076 unsigned int local_sym_index = p->first.locsym_;
5077 return this->targ_->plt_off(relobj, local_sym_index, sec);
5078 }
5079 }
5080
5081 // Size of a given plt call stub.
5082 unsigned int
5083 plt_call_size(typename Plt_stub_entries::iterator p) const;
5084
5085 unsigned int
5086 plt_call_align(unsigned int bytes) const
5087 {
5088 unsigned int align = param_plt_align<size>();
5089 return (bytes + align - 1) & -align;
5090 }
5091
5092 // Return long branch stub size.
5093 unsigned int
5094 branch_stub_size(typename Branch_stub_entries::iterator p,
5095 bool* need_lt);
5096
5097 void
5098 build_tls_opt_head(unsigned char** pp, bool save_lr);
5099
5100 void
5101 build_tls_opt_tail(unsigned char* p);
5102
5103 void
5104 plt_error(const Plt_stub_key& p);
5105
5106 // Write out stubs.
5107 void
5108 do_write(Output_file*);
5109
5110 // Plt call stub keys.
5111 class Plt_stub_key
5112 {
5113 public:
5114 Plt_stub_key(const Symbol* sym)
5115 : sym_(sym), object_(0), addend_(0), locsym_(0)
5116 { }
5117
5118 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5119 unsigned int locsym_index)
5120 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5121 { }
5122
5123 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5124 const Symbol* sym,
5125 unsigned int r_type,
5126 Address addend)
5127 : sym_(sym), object_(0), addend_(0), locsym_(0)
5128 {
5129 if (size != 32)
5130 this->addend_ = addend;
5131 else if (parameters->options().output_is_position_independent()
5132 && (r_type == elfcpp::R_PPC_PLTREL24
5133 || r_type == elfcpp::R_POWERPC_PLTCALL))
5134 {
5135 this->addend_ = addend;
5136 if (this->addend_ >= 32768)
5137 this->object_ = object;
5138 }
5139 }
5140
5141 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5142 unsigned int locsym_index,
5143 unsigned int r_type,
5144 Address addend)
5145 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5146 {
5147 if (size != 32)
5148 this->addend_ = addend;
5149 else if (parameters->options().output_is_position_independent()
5150 && (r_type == elfcpp::R_PPC_PLTREL24
5151 || r_type == elfcpp::R_POWERPC_PLTCALL))
5152 this->addend_ = addend;
5153 }
5154
5155 bool operator==(const Plt_stub_key& that) const
5156 {
5157 return (this->sym_ == that.sym_
5158 && this->object_ == that.object_
5159 && this->addend_ == that.addend_
5160 && this->locsym_ == that.locsym_);
5161 }
5162
5163 const Symbol* sym_;
5164 const Sized_relobj_file<size, big_endian>* object_;
5165 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5166 unsigned int locsym_;
5167 };
5168
5169 class Plt_stub_key_hash
5170 {
5171 public:
5172 size_t operator()(const Plt_stub_key& ent) const
5173 {
5174 return (reinterpret_cast<uintptr_t>(ent.sym_)
5175 ^ reinterpret_cast<uintptr_t>(ent.object_)
5176 ^ ent.addend_
5177 ^ ent.locsym_);
5178 }
5179 };
5180
5181 // Long branch stub keys.
5182 class Branch_stub_key
5183 {
5184 public:
5185 Branch_stub_key(Address to)
5186 : dest_(to)
5187 { }
5188
5189 bool operator==(const Branch_stub_key& that) const
5190 {
5191 return this->dest_ == that.dest_;
5192 }
5193
5194 Address dest_;
5195 };
5196
5197 class Branch_stub_key_hash
5198 {
5199 public:
5200 size_t operator()(const Branch_stub_key& key) const
5201 { return key.dest_; }
5202 };
5203
5204 // In a sane world this would be a global.
5205 Target_powerpc<size, big_endian>* targ_;
5206 // Map sym/object/addend to stub offset.
5207 Plt_stub_entries plt_call_stubs_;
5208 // Map destination address to stub offset.
5209 Branch_stub_entries long_branch_stubs_;
5210 // size of input section
5211 section_size_type orig_data_size_;
5212 // size of stubs
5213 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5214 // Some rare cases cause (PR/20529) fluctuation in stub table
5215 // size, which leads to an endless relax loop. This is to be fixed
5216 // by, after the first few iterations, allowing only increase of
5217 // stub table size. This variable sets the minimal possible size of
5218 // a stub table, it is zero for the first few iterations, then
5219 // increases monotonically.
5220 Address min_size_threshold_;
5221 // Set if this stub group needs a copy of out-of-line register
5222 // save/restore functions.
5223 bool need_save_res_;
5224 // Set when notoc_/r2save_ changes after sizing a stub
5225 bool need_resize_;
5226 // Set when resizing stubs
5227 bool resizing_;
5228 // Per stub table unique identifier.
5229 uint32_t uniq_;
5230 };
5231
5232 // Add a plt call stub, if we do not already have one for this
5233 // sym/object/addend combo.
5234
5235 template<int size, bool big_endian>
5236 bool
5237 Stub_table<size, big_endian>::add_plt_call_entry(
5238 Address from,
5239 const Sized_relobj_file<size, big_endian>* object,
5240 const Symbol* gsym,
5241 unsigned int r_type,
5242 Address addend,
5243 bool tocsave)
5244 {
5245 Plt_stub_key key(object, gsym, r_type, addend);
5246 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5247 std::pair<typename Plt_stub_entries::iterator, bool> p
5248 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5249 if (size == 64)
5250 {
5251 if (p.second
5252 && this->targ_->is_elfv2_localentry0(gsym))
5253 {
5254 p.first->second.localentry0_ = 1;
5255 this->targ_->set_has_localentry0();
5256 }
5257 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5258 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5259 {
5260 if (this->targ_->power10_stubs()
5261 && (!this->targ_->power10_stubs_auto()
5262 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5263 {
5264 if (!p.second && !p.first->second.notoc_)
5265 this->need_resize_ = true;
5266 p.first->second.notoc_ = 1;
5267 }
5268 else
5269 {
5270 if (!p.second && !p.first->second.p9notoc_)
5271 this->need_resize_ = true;
5272 p.first->second.p9notoc_ = 1;
5273 }
5274 }
5275 else
5276 {
5277 if (!p.second && !p.first->second.toc_)
5278 this->need_resize_ = true;
5279 p.first->second.toc_ = 1;
5280 if (!tocsave && !p.first->second.localentry0_)
5281 {
5282 if (!p.second && !p.first->second.r2save_)
5283 this->need_resize_ = true;
5284 p.first->second.r2save_ = 1;
5285 }
5286 }
5287 }
5288 if (p.second || (this->resizing_ && !p.first->second.iter_))
5289 {
5290 if (this->resizing_)
5291 {
5292 p.first->second.iter_ = 1;
5293 p.first->second.off_ = this->plt_size_;
5294 }
5295 this->plt_size_ += this->plt_call_size(p.first);
5296 if (this->targ_->is_tls_get_addr_opt(gsym))
5297 this->targ_->set_has_tls_get_addr_opt();
5298 }
5299 return this->can_reach_stub(from, p.first->second.off_, r_type);
5300 }
5301
5302 template<int size, bool big_endian>
5303 bool
5304 Stub_table<size, big_endian>::add_plt_call_entry(
5305 Address from,
5306 const Sized_relobj_file<size, big_endian>* object,
5307 unsigned int locsym_index,
5308 unsigned int r_type,
5309 Address addend,
5310 bool tocsave)
5311 {
5312 Plt_stub_key key(object, locsym_index, r_type, addend);
5313 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5314 std::pair<typename Plt_stub_entries::iterator, bool> p
5315 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5316 if (size == 64)
5317 {
5318 if (p.second
5319 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5320 {
5321 p.first->second.localentry0_ = 1;
5322 this->targ_->set_has_localentry0();
5323 }
5324 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5325 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5326 {
5327 if (this->targ_->power10_stubs()
5328 && (!this->targ_->power10_stubs_auto()
5329 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5330 {
5331 if (!p.second && !p.first->second.notoc_)
5332 this->need_resize_ = true;
5333 p.first->second.notoc_ = 1;
5334 }
5335 else
5336 {
5337 if (!p.second && !p.first->second.p9notoc_)
5338 this->need_resize_ = true;
5339 p.first->second.p9notoc_ = 1;
5340 }
5341 }
5342 else
5343 {
5344 if (!p.second && !p.first->second.toc_)
5345 this->need_resize_ = true;
5346 p.first->second.toc_ = 1;
5347 if (!tocsave && !p.first->second.localentry0_)
5348 {
5349 if (!p.second && !p.first->second.r2save_)
5350 this->need_resize_ = true;
5351 p.first->second.r2save_ = 1;
5352 }
5353 }
5354 }
5355 if (p.second || (this->resizing_ && !p.first->second.iter_))
5356 {
5357 if (this->resizing_)
5358 {
5359 p.first->second.iter_ = 1;
5360 p.first->second.off_ = this->plt_size_;
5361 }
5362 this->plt_size_ += this->plt_call_size(p.first);
5363 }
5364 return this->can_reach_stub(from, p.first->second.off_, r_type);
5365 }
5366
5367 // Find a plt call stub.
5368
5369 template<int size, bool big_endian>
5370 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5371 Stub_table<size, big_endian>::find_plt_call_entry(
5372 const Sized_relobj_file<size, big_endian>* object,
5373 const Symbol* gsym,
5374 unsigned int r_type,
5375 Address addend) const
5376 {
5377 Plt_stub_key key(object, gsym, r_type, addend);
5378 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5379 if (p == this->plt_call_stubs_.end())
5380 return NULL;
5381 return &p->second;
5382 }
5383
5384 template<int size, bool big_endian>
5385 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5386 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5387 {
5388 Plt_stub_key key(gsym);
5389 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5390 if (p == this->plt_call_stubs_.end())
5391 return NULL;
5392 return &p->second;
5393 }
5394
5395 template<int size, bool big_endian>
5396 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5397 Stub_table<size, big_endian>::find_plt_call_entry(
5398 const Sized_relobj_file<size, big_endian>* object,
5399 unsigned int locsym_index,
5400 unsigned int r_type,
5401 Address addend) const
5402 {
5403 Plt_stub_key key(object, locsym_index, r_type, addend);
5404 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5405 if (p == this->plt_call_stubs_.end())
5406 return NULL;
5407 return &p->second;
5408 }
5409
5410 template<int size, bool big_endian>
5411 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5412 Stub_table<size, big_endian>::find_plt_call_entry(
5413 const Sized_relobj_file<size, big_endian>* object,
5414 unsigned int locsym_index) const
5415 {
5416 Plt_stub_key key(object, locsym_index);
5417 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5418 if (p == this->plt_call_stubs_.end())
5419 return NULL;
5420 return &p->second;
5421 }
5422
5423 // Add a long branch stub if we don't already have one to given
5424 // destination.
5425
5426 template<int size, bool big_endian>
5427 bool
5428 Stub_table<size, big_endian>::add_long_branch_entry(
5429 unsigned int r_type,
5430 Address from,
5431 Address to,
5432 unsigned int other,
5433 bool save_res)
5434 {
5435 Branch_stub_key key(to);
5436 Branch_stub_ent ent(this->branch_size_);
5437 std::pair<typename Branch_stub_entries::iterator, bool> p
5438 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5439 if (save_res)
5440 {
5441 if (!p.second && !p.first->second.save_res_)
5442 this->need_resize_ = true;
5443 p.first->second.save_res_ = true;
5444 }
5445 else if (size == 64
5446 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5447 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5448 {
5449 if (this->targ_->power10_stubs()
5450 && (!this->targ_->power10_stubs_auto()
5451 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5452 {
5453 if (!p.second && !p.first->second.notoc_)
5454 this->need_resize_ = true;
5455 p.first->second.notoc_ = true;
5456 }
5457 else
5458 {
5459 if (!p.second && !p.first->second.p9notoc_)
5460 this->need_resize_ = true;
5461 p.first->second.p9notoc_ = true;
5462 }
5463 }
5464 else
5465 {
5466 if (!p.second && !p.first->second.toc_)
5467 this->need_resize_ = true;
5468 p.first->second.toc_ = true;
5469 }
5470 if (size == 64 && p.first->second.other_ == 0)
5471 p.first->second.other_ = other;
5472 if (p.second || (this->resizing_ && !p.first->second.iter_))
5473 {
5474 if (this->resizing_)
5475 {
5476 p.first->second.iter_ = 1;
5477 p.first->second.off_ = this->branch_size_;
5478 }
5479 if (save_res)
5480 this->need_save_res_ = true;
5481 else
5482 {
5483 bool need_lt = false;
5484 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5485 this->branch_size_ += stub_size;
5486 if (size == 64 && need_lt)
5487 this->targ_->add_branch_lookup_table(to);
5488 }
5489 }
5490 return this->can_reach_stub(from, p.first->second.off_, r_type);
5491 }
5492
5493 // Find long branch stub offset.
5494
5495 template<int size, bool big_endian>
5496 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5497 Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
5498 {
5499 Branch_stub_key key(to);
5500 typename Branch_stub_entries::const_iterator p
5501 = this->long_branch_stubs_.find(key);
5502 if (p == this->long_branch_stubs_.end())
5503 return NULL;
5504 return &p->second;
5505 }
5506
5507 template<bool big_endian>
5508 static void
5509 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5510 {
5511 delta /= 4;
5512 if (delta < 64)
5513 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5514 else if (delta < 256)
5515 {
5516 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5517 fde.push_back(delta);
5518 }
5519 else if (delta < 65536)
5520 {
5521 fde.resize(fde.size() + 3);
5522 unsigned char *p = &*fde.end() - 3;
5523 *p++ = elfcpp::DW_CFA_advance_loc2;
5524 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5525 }
5526 else
5527 {
5528 fde.resize(fde.size() + 5);
5529 unsigned char *p = &*fde.end() - 5;
5530 *p++ = elfcpp::DW_CFA_advance_loc4;
5531 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5532 }
5533 }
5534
5535 template<typename T>
5536 static bool
5537 stub_sort(T s1, T s2)
5538 {
5539 return s1->second.off_ < s2->second.off_;
5540 }
5541
5542 // Add .eh_frame info for this stub section. Unlike other linker
5543 // generated .eh_frame this is added late in the link, because we
5544 // only want the .eh_frame info if this particular stub section is
5545 // non-empty.
5546
5547 template<int size, bool big_endian>
5548 void
5549 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5550 {
5551 if (size != 64
5552 || !parameters->options().ld_generated_unwind_info())
5553 return;
5554
5555 // Since we add stub .eh_frame info late, it must be placed
5556 // after all other linker generated .eh_frame info so that
5557 // merge mapping need not be updated for input sections.
5558 // There is no provision to use a different CIE to that used
5559 // by .glink.
5560 if (!this->targ_->has_glink())
5561 return;
5562
5563 typedef typename Plt_stub_entries::iterator plt_iter;
5564 std::vector<plt_iter> calls;
5565 if (!this->plt_call_stubs_.empty())
5566 for (plt_iter cs = this->plt_call_stubs_.begin();
5567 cs != this->plt_call_stubs_.end();
5568 ++cs)
5569 if (cs->second.p9notoc_
5570 || (cs->second.toc_
5571 && cs->second.r2save_
5572 && !cs->second.localentry0_
5573 && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
5574 calls.push_back(cs);
5575 if (calls.size() > 1)
5576 std::stable_sort(calls.begin(), calls.end(),
5577 stub_sort<plt_iter>);
5578
5579 typedef typename Branch_stub_entries::const_iterator branch_iter;
5580 std::vector<branch_iter> branches;
5581 if (!this->long_branch_stubs_.empty()
5582 && !this->targ_->power10_stubs())
5583 for (branch_iter bs = this->long_branch_stubs_.begin();
5584 bs != this->long_branch_stubs_.end();
5585 ++bs)
5586 if (bs->second.notoc_)
5587 branches.push_back(bs);
5588 if (branches.size() > 1)
5589 std::stable_sort(branches.begin(), branches.end(),
5590 stub_sort<branch_iter>);
5591
5592 if (calls.empty() && branches.empty())
5593 return;
5594
5595 unsigned int last_eh_loc = 0;
5596 // offset pcrel sdata4, size udata4, and augmentation size byte.
5597 std::vector<unsigned char> fde(9, 0);
5598
5599 for (unsigned int i = 0; i < calls.size(); i++)
5600 {
5601 plt_iter cs = calls[i];
5602 unsigned int off = cs->second.off_;
5603 // The __tls_get_addr_opt call stub needs to describe where
5604 // it saves LR, to support exceptions that might be thrown
5605 // from __tls_get_addr, and to support asynchronous exceptions.
5606 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5607 {
5608 off += 7 * 4;
5609 if (cs->second.toc_
5610 && cs->second.r2save_
5611 && !cs->second.localentry0_)
5612 {
5613 off += cs->second.tocoff_ + 2 * 4;
5614 eh_advance<big_endian>(fde, off - last_eh_loc);
5615 fde.resize(fde.size() + 6);
5616 unsigned char* p = &*fde.end() - 6;
5617 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5618 *p++ = 65;
5619 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5620 unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
5621 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5622 *p++ = elfcpp::DW_CFA_restore_extended;
5623 *p++ = 65;
5624 last_eh_loc = off + delta;
5625 off = cs->second.off_ + 7 * 4;
5626 }
5627 }
5628 // notoc stubs also should describe LR changes, to support
5629 // asynchronous exceptions.
5630 if (cs->second.p9notoc_)
5631 {
5632 off += cs->second.p9off_;
5633 off += (cs->second.r2save_ ? 4 : 0) + 8;
5634 eh_advance<big_endian>(fde, off - last_eh_loc);
5635 fde.resize(fde.size() + 6);
5636 unsigned char* p = &*fde.end() - 6;
5637 *p++ = elfcpp::DW_CFA_register;
5638 *p++ = 65;
5639 *p++ = 12;
5640 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5641 *p++ = elfcpp::DW_CFA_restore_extended;
5642 *p++ = 65;
5643 last_eh_loc = off + 8;
5644 }
5645 }
5646
5647 for (unsigned int i = 0; i < branches.size(); i++)
5648 {
5649 branch_iter bs = branches[i];
5650 unsigned int off = bs->second.off_ + 8;
5651 eh_advance<big_endian>(fde, off - last_eh_loc);
5652 fde.resize(fde.size() + 6);
5653 unsigned char* p = &*fde.end() - 6;
5654 *p++ = elfcpp::DW_CFA_register;
5655 *p++ = 65;
5656 *p++ = 12;
5657 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5658 *p++ = elfcpp::DW_CFA_restore_extended;
5659 *p++ = 65;
5660 last_eh_loc = off + 8;
5661 }
5662
5663 layout->add_eh_frame_for_plt(this,
5664 Eh_cie<size>::eh_frame_cie,
5665 sizeof (Eh_cie<size>::eh_frame_cie),
5666 &*fde.begin(), fde.size());
5667 }
5668
5669 template<int size, bool big_endian>
5670 void
5671 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5672 {
5673 if (size == 64
5674 && parameters->options().ld_generated_unwind_info()
5675 && this->targ_->has_glink())
5676 layout->remove_eh_frame_for_plt(this,
5677 Eh_cie<size>::eh_frame_cie,
5678 sizeof (Eh_cie<size>::eh_frame_cie));
5679 }
5680
5681 // A class to handle .glink.
5682
5683 template<int size, bool big_endian>
5684 class Output_data_glink : public Output_section_data
5685 {
5686 public:
5687 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5688 static const Address invalid_address = static_cast<Address>(0) - 1;
5689
5690 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5691 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5692 end_branch_table_(), ge_size_(0)
5693 { }
5694
5695 void
5696 add_eh_frame(Layout* layout);
5697
5698 void
5699 add_global_entry(const Symbol*);
5700
5701 Address
5702 find_global_entry(const Symbol*) const;
5703
5704 unsigned int
5705 global_entry_align(unsigned int off) const
5706 {
5707 unsigned int align = param_plt_align<size>();
5708 return (off + align - 1) & -align;
5709 }
5710
5711 unsigned int
5712 global_entry_off() const
5713 {
5714 return this->global_entry_align(this->end_branch_table_);
5715 }
5716
5717 Address
5718 global_entry_address() const
5719 {
5720 gold_assert(this->is_data_size_valid());
5721 return this->address() + this->global_entry_off();
5722 }
5723
5724 int
5725 pltresolve_size() const
5726 {
5727 if (size == 64)
5728 return (8
5729 + (this->targ_->abiversion() < 2 ? 11 * 4
5730 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5731 return 16 * 4;
5732 }
5733
5734 protected:
5735 // Write to a map file.
5736 void
5737 do_print_to_mapfile(Mapfile* mapfile) const
5738 { mapfile->print_output_data(this, _("** glink")); }
5739
5740 private:
5741 void
5742 set_final_data_size();
5743
5744 // Write out .glink
5745 void
5746 do_write(Output_file*);
5747
5748 // Allows access to .got and .plt for do_write.
5749 Target_powerpc<size, big_endian>* targ_;
5750
5751 // Map sym to stub offset.
5752 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5753 Global_entry_stub_entries global_entry_stubs_;
5754
5755 unsigned int end_branch_table_, ge_size_;
5756 };
5757
5758 template<int size, bool big_endian>
5759 void
5760 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5761 {
5762 if (!parameters->options().ld_generated_unwind_info())
5763 return;
5764
5765 if (size == 64)
5766 {
5767 if (this->targ_->abiversion() < 2)
5768 layout->add_eh_frame_for_plt(this,
5769 Eh_cie<64>::eh_frame_cie,
5770 sizeof (Eh_cie<64>::eh_frame_cie),
5771 glink_eh_frame_fde_64v1,
5772 sizeof (glink_eh_frame_fde_64v1));
5773 else if (this->targ_->has_localentry0())
5774 layout->add_eh_frame_for_plt(this,
5775 Eh_cie<64>::eh_frame_cie,
5776 sizeof (Eh_cie<64>::eh_frame_cie),
5777 glink_eh_frame_fde_64v2_localentry0,
5778 sizeof (glink_eh_frame_fde_64v2));
5779 else
5780 layout->add_eh_frame_for_plt(this,
5781 Eh_cie<64>::eh_frame_cie,
5782 sizeof (Eh_cie<64>::eh_frame_cie),
5783 glink_eh_frame_fde_64v2,
5784 sizeof (glink_eh_frame_fde_64v2));
5785 }
5786 else
5787 {
5788 // 32-bit .glink can use the default since the CIE return
5789 // address reg, LR, is valid.
5790 layout->add_eh_frame_for_plt(this,
5791 Eh_cie<32>::eh_frame_cie,
5792 sizeof (Eh_cie<32>::eh_frame_cie),
5793 default_fde,
5794 sizeof (default_fde));
5795 // Except where LR is used in a PIC __glink_PLTresolve.
5796 if (parameters->options().output_is_position_independent())
5797 layout->add_eh_frame_for_plt(this,
5798 Eh_cie<32>::eh_frame_cie,
5799 sizeof (Eh_cie<32>::eh_frame_cie),
5800 glink_eh_frame_fde_32,
5801 sizeof (glink_eh_frame_fde_32));
5802 }
5803 }
5804
5805 template<int size, bool big_endian>
5806 void
5807 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5808 {
5809 unsigned int off = this->global_entry_align(this->ge_size_);
5810 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5811 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5812 if (p.second)
5813 this->ge_size_ = off + 16;
5814 }
5815
5816 template<int size, bool big_endian>
5817 typename Output_data_glink<size, big_endian>::Address
5818 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5819 {
5820 typename Global_entry_stub_entries::const_iterator p
5821 = this->global_entry_stubs_.find(gsym);
5822 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5823 }
5824
5825 template<int size, bool big_endian>
5826 void
5827 Output_data_glink<size, big_endian>::set_final_data_size()
5828 {
5829 unsigned int count = this->targ_->plt_entry_count();
5830 section_size_type total = 0;
5831
5832 if (count != 0)
5833 {
5834 if (size == 32)
5835 {
5836 // space for branch table
5837 total += 4 * (count - 1);
5838
5839 total += -total & 15;
5840 total += this->pltresolve_size();
5841 }
5842 else
5843 {
5844 total += this->pltresolve_size();
5845
5846 // space for branch table
5847 total += 4 * count;
5848 if (this->targ_->abiversion() < 2)
5849 {
5850 total += 4 * count;
5851 if (count > 0x8000)
5852 total += 4 * (count - 0x8000);
5853 }
5854 }
5855 }
5856 this->end_branch_table_ = total;
5857 total = this->global_entry_align(total);
5858 total += this->ge_size_;
5859
5860 this->set_data_size(total);
5861 }
5862
5863 // Define symbols on stubs, identifying the stub.
5864
5865 template<int size, bool big_endian>
5866 void
5867 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5868 {
5869 if (!this->plt_call_stubs_.empty())
5870 {
5871 // The key for the plt call stub hash table includes addresses,
5872 // therefore traversal order depends on those addresses, which
5873 // can change between runs if gold is a PIE. Unfortunately the
5874 // output .symtab ordering depends on the order in which symbols
5875 // are added to the linker symtab. We want reproducible output
5876 // so must sort the call stub symbols.
5877 typedef typename Plt_stub_entries::iterator plt_iter;
5878 std::vector<plt_iter> sorted;
5879 sorted.resize(this->plt_call_stubs_.size());
5880
5881 for (plt_iter cs = this->plt_call_stubs_.begin();
5882 cs != this->plt_call_stubs_.end();
5883 ++cs)
5884 sorted[cs->second.indx_] = cs;
5885
5886 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5887 {
5888 plt_iter cs = sorted[i];
5889 char add[10];
5890 add[0] = 0;
5891 if (cs->first.addend_ != 0)
5892 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5893 char obj[10];
5894 obj[0] = 0;
5895 if (cs->first.object_)
5896 {
5897 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5898 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5899 sprintf(obj, "%x:", ppcobj->uniq());
5900 }
5901 char localname[9];
5902 const char *symname;
5903 if (cs->first.sym_ == NULL)
5904 {
5905 sprintf(localname, "%x", cs->first.locsym_);
5906 symname = localname;
5907 }
5908 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5909 symname = this->targ_->tls_get_addr_opt()->name();
5910 else
5911 symname = cs->first.sym_->name();
5912 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5913 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5914 Address value
5915 = this->stub_address() - this->address() + cs->second.off_;
5916 unsigned int stub_size = this->plt_call_size(cs);
5917 this->targ_->define_local(symtab, name, this, value, stub_size);
5918 }
5919 }
5920
5921 typedef typename Branch_stub_entries::iterator branch_iter;
5922 for (branch_iter bs = this->long_branch_stubs_.begin();
5923 bs != this->long_branch_stubs_.end();
5924 ++bs)
5925 {
5926 if (bs->second.save_res_)
5927 continue;
5928
5929 char* name = new char[8 + 13 + 16 + 1];
5930 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5931 static_cast<unsigned long long>(bs->first.dest_));
5932 Address value = (this->stub_address() - this->address()
5933 + this->plt_size_ + bs->second.off_);
5934 bool need_lt = false;
5935 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5936 this->targ_->define_local(symtab, name, this, value, stub_size);
5937 }
5938 }
5939
5940 // Emit the start of a __tls_get_addr_opt plt call stub.
5941
5942 template<int size, bool big_endian>
5943 void
5944 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5945 bool save_lr)
5946 {
5947 unsigned char* p = *pp;
5948 if (size == 64)
5949 {
5950 write_insn<big_endian>(p, ld_11_3 + 0);
5951 p += 4;
5952 write_insn<big_endian>(p, ld_12_3 + 8);
5953 p += 4;
5954 write_insn<big_endian>(p, mr_0_3);
5955 p += 4;
5956 write_insn<big_endian>(p, cmpdi_11_0);
5957 p += 4;
5958 write_insn<big_endian>(p, add_3_12_13);
5959 p += 4;
5960 write_insn<big_endian>(p, beqlr);
5961 p += 4;
5962 write_insn<big_endian>(p, mr_3_0);
5963 p += 4;
5964 if (save_lr)
5965 {
5966 write_insn<big_endian>(p, mflr_11);
5967 p += 4;
5968 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5969 p += 4;
5970 }
5971 }
5972 else
5973 {
5974 write_insn<big_endian>(p, lwz_11_3 + 0);
5975 p += 4;
5976 write_insn<big_endian>(p, lwz_12_3 + 4);
5977 p += 4;
5978 write_insn<big_endian>(p, mr_0_3);
5979 p += 4;
5980 write_insn<big_endian>(p, cmpwi_11_0);
5981 p += 4;
5982 write_insn<big_endian>(p, add_3_12_2);
5983 p += 4;
5984 write_insn<big_endian>(p, beqlr);
5985 p += 4;
5986 write_insn<big_endian>(p, mr_3_0);
5987 p += 4;
5988 write_insn<big_endian>(p, nop);
5989 p += 4;
5990 }
5991 *pp = p;
5992 }
5993
5994 // Emit the tail of a __tls_get_addr_opt plt call stub.
5995
5996 template<int size, bool big_endian>
5997 void
5998 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5999 {
6000 write_insn<big_endian>(p, bctrl);
6001 p += 4;
6002 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
6003 p += 4;
6004 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
6005 p += 4;
6006 write_insn<big_endian>(p, mtlr_11);
6007 p += 4;
6008 write_insn<big_endian>(p, blr);
6009 }
6010
6011 // Emit pc-relative plt call stub code.
6012
6013 template<bool big_endian>
6014 static unsigned char*
6015 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
6016 {
6017 uint64_t insn;
6018 if (off - odd + (1ULL << 33) < 1ULL << 34)
6019 {
6020 off -= odd;
6021 if (odd)
6022 {
6023 write_insn<big_endian>(p, nop);
6024 p += 4;
6025 }
6026 if (load)
6027 insn = pld_12_pc;
6028 else
6029 insn = paddi_12_pc;
6030 insn |= d34(off);
6031 write_insn<big_endian>(p, insn >> 32);
6032 p += 4;
6033 write_insn<big_endian>(p, insn & 0xffffffff);
6034 }
6035 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6036 {
6037 off -= 8 - odd;
6038 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6039 p += 4;
6040 if (!odd)
6041 {
6042 write_insn<big_endian>(p, sldi_11_11_34);
6043 p += 4;
6044 }
6045 insn = paddi_12_pc | d34(off);
6046 write_insn<big_endian>(p, insn >> 32);
6047 p += 4;
6048 write_insn<big_endian>(p, insn & 0xffffffff);
6049 p += 4;
6050 if (odd)
6051 {
6052 write_insn<big_endian>(p, sldi_11_11_34);
6053 p += 4;
6054 }
6055 if (load)
6056 write_insn<big_endian>(p, ldx_12_11_12);
6057 else
6058 write_insn<big_endian>(p, add_12_11_12);
6059 }
6060 else
6061 {
6062 off -= odd + 8;
6063 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6064 p += 4;
6065 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6066 p += 4;
6067 if (odd)
6068 {
6069 write_insn<big_endian>(p, sldi_11_11_34);
6070 p += 4;
6071 }
6072 insn = paddi_12_pc | d34(off);
6073 write_insn<big_endian>(p, insn >> 32);
6074 p += 4;
6075 write_insn<big_endian>(p, insn & 0xffffffff);
6076 p += 4;
6077 if (!odd)
6078 {
6079 write_insn<big_endian>(p, sldi_11_11_34);
6080 p += 4;
6081 }
6082 if (load)
6083 write_insn<big_endian>(p, ldx_12_11_12);
6084 else
6085 write_insn<big_endian>(p, add_12_11_12);
6086 }
6087 p += 4;
6088 return p;
6089 }
6090
6091 // Gets the address of a label (1:) in r11 and builds an offset in r12,
6092 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6093 // mflr %r12
6094 // bcl 20,31,1f
6095 // 1: mflr %r11
6096 // mtlr %r12
6097 // lis %r12,xxx-1b@highest
6098 // ori %r12,%r12,xxx-1b@higher
6099 // sldi %r12,%r12,32
6100 // oris %r12,%r12,xxx-1b@high
6101 // ori %r12,%r12,xxx-1b@l
6102 // add/ldx %r12,%r11,%r12
6103
6104 template<bool big_endian>
6105 static unsigned char*
6106 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6107 {
6108 write_insn<big_endian>(p, mflr_12);
6109 p += 4;
6110 write_insn<big_endian>(p, bcl_20_31);
6111 p += 4;
6112 write_insn<big_endian>(p, mflr_11);
6113 p += 4;
6114 write_insn<big_endian>(p, mtlr_12);
6115 p += 4;
6116 if (off + 0x8000 < 0x10000)
6117 {
6118 if (load)
6119 write_insn<big_endian>(p, ld_12_11 + l(off));
6120 else
6121 write_insn<big_endian>(p, addi_12_11 + l(off));
6122 }
6123 else if (off + 0x80008000ULL < 0x100000000ULL)
6124 {
6125 write_insn<big_endian>(p, addis_12_11 + ha(off));
6126 p += 4;
6127 if (load)
6128 write_insn<big_endian>(p, ld_12_12 + l(off));
6129 else
6130 write_insn<big_endian>(p, addi_12_12 + l(off));
6131 }
6132 else
6133 {
6134 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6135 {
6136 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6137 p += 4;
6138 }
6139 else
6140 {
6141 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6142 p += 4;
6143 if (((off >> 32) & 0xffff) != 0)
6144 {
6145 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6146 p += 4;
6147 }
6148 }
6149 if (((off >> 32) & 0xffffffffULL) != 0)
6150 {
6151 write_insn<big_endian>(p, sldi_12_12_32);
6152 p += 4;
6153 }
6154 if (hi(off) != 0)
6155 {
6156 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6157 p += 4;
6158 }
6159 if (l(off) != 0)
6160 {
6161 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6162 p += 4;
6163 }
6164 if (load)
6165 write_insn<big_endian>(p, ldx_12_11_12);
6166 else
6167 write_insn<big_endian>(p, add_12_11_12);
6168 }
6169 p += 4;
6170 return p;
6171 }
6172
6173 // Size of a given plt call stub.
6174
6175 template<int size, bool big_endian>
6176 unsigned int
6177 Stub_table<size, big_endian>::plt_call_size(
6178 typename Plt_stub_entries::iterator p) const
6179 {
6180 if (size == 32)
6181 {
6182 unsigned int bytes = 4 * 4;
6183 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6184 bytes = 12 * 4;
6185 return this->plt_call_align(bytes);
6186 }
6187
6188 const Output_data_plt_powerpc<size, big_endian>* plt;
6189 uint64_t plt_addr = this->plt_off(p, &plt);
6190 plt_addr += plt->address();
6191 if (this->targ_->power10_stubs()
6192 && this->targ_->power10_stubs_auto())
6193 {
6194 unsigned int bytes = 0;
6195 if (p->second.notoc_)
6196 {
6197 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6198 bytes = 7 * 4;
6199 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6200 uint64_t odd = from & 4;
6201 uint64_t off = plt_addr - from;
6202 if (off - odd + (1ULL << 33) < 1ULL << 34)
6203 bytes += odd + 4 * 4;
6204 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6205 bytes += 7 * 4;
6206 else
6207 bytes += 8 * 4;
6208 bytes = this->plt_call_align(bytes);
6209 }
6210 if (p->second.toc_)
6211 {
6212 p->second.tocoff_ = bytes;
6213 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6214 {
6215 bytes += 7 * 4;
6216 if (p->second.r2save_ && !p->second.localentry0_)
6217 bytes += 2 * 4 + 4 * 4;
6218 }
6219 if (p->second.r2save_)
6220 bytes += 4;
6221 uint64_t got_addr = this->targ_->toc_pointer();
6222 uint64_t off = plt_addr - got_addr;
6223 bytes += 3 * 4 + 4 * (ha(off) != 0);
6224 p->second.tsize_ = bytes - p->second.tocoff_;
6225 bytes = this->plt_call_align(bytes);
6226 }
6227 if (p->second.p9notoc_)
6228 {
6229 p->second.p9off_ = bytes;
6230 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6231 bytes += 7 * 4;
6232 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6233 uint64_t off = plt_addr - from;
6234 if (off + 0x8000 < 0x10000)
6235 bytes += 7 * 4;
6236 else if (off + 0x80008000ULL < 0x100000000ULL)
6237 bytes += 8 * 4;
6238 else
6239 {
6240 bytes += 8 * 4;
6241 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6242 && ((off >> 32) & 0xffff) != 0)
6243 bytes += 4;
6244 if (((off >> 32) & 0xffffffffULL) != 0)
6245 bytes += 4;
6246 if (hi(off) != 0)
6247 bytes += 4;
6248 if (l(off) != 0)
6249 bytes += 4;
6250 }
6251 bytes = this->plt_call_align(bytes);
6252 }
6253 return bytes;
6254 }
6255 else
6256 {
6257 unsigned int bytes = 0;
6258 unsigned int tail = 0;
6259 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6260 {
6261 bytes = 7 * 4;
6262 if (p->second.r2save_ && !p->second.localentry0_)
6263 {
6264 bytes = 9 * 4;
6265 tail = 4 * 4;
6266 }
6267 }
6268
6269 if (p->second.r2save_)
6270 bytes += 4;
6271
6272 if (this->targ_->power10_stubs())
6273 {
6274 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6275 uint64_t odd = from & 4;
6276 uint64_t off = plt_addr - from;
6277 if (off - odd + (1ULL << 33) < 1ULL << 34)
6278 bytes += odd + 4 * 4;
6279 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6280 bytes += 7 * 4;
6281 else
6282 bytes += 8 * 4;
6283 return this->plt_call_align(bytes + tail);
6284 }
6285
6286 if (p->second.p9notoc_)
6287 {
6288 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6289 uint64_t off = plt_addr - from;
6290 if (off + 0x8000 < 0x10000)
6291 bytes += 7 * 4;
6292 else if (off + 0x80008000ULL < 0x100000000ULL)
6293 bytes += 8 * 4;
6294 else
6295 {
6296 bytes += 8 * 4;
6297 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6298 && ((off >> 32) & 0xffff) != 0)
6299 bytes += 4;
6300 if (((off >> 32) & 0xffffffffULL) != 0)
6301 bytes += 4;
6302 if (hi(off) != 0)
6303 bytes += 4;
6304 if (l(off) != 0)
6305 bytes += 4;
6306 }
6307 return this->plt_call_align(bytes + tail);
6308 }
6309
6310 uint64_t got_addr = this->targ_->toc_pointer();
6311 uint64_t off = plt_addr - got_addr;
6312 bytes += 3 * 4 + 4 * (ha(off) != 0);
6313 if (this->targ_->abiversion() < 2)
6314 {
6315 bool static_chain = parameters->options().plt_static_chain();
6316 bool thread_safe = this->targ_->plt_thread_safe();
6317 bytes += (4
6318 + 4 * static_chain
6319 + 8 * thread_safe
6320 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6321 }
6322 return this->plt_call_align(bytes + tail);
6323 }
6324 }
6325
6326 // Return long branch stub size.
6327
6328 template<int size, bool big_endian>
6329 unsigned int
6330 Stub_table<size, big_endian>::branch_stub_size(
6331 typename Branch_stub_entries::iterator p,
6332 bool* need_lt)
6333 {
6334 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6335 if (size == 32)
6336 {
6337 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6338 return 4;
6339 if (parameters->options().output_is_position_independent())
6340 return 32;
6341 return 16;
6342 }
6343
6344 uint64_t off = p->first.dest_ - loc;
6345 unsigned int bytes = 0;
6346 if (p->second.notoc_)
6347 {
6348 if (this->targ_->power10_stubs())
6349 {
6350 Address odd = loc & 4;
6351 if (off + (1 << 25) < 2 << 25)
6352 bytes = odd + 12;
6353 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6354 bytes = odd + 16;
6355 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6356 bytes = 28;
6357 else
6358 bytes = 32;
6359 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6360 return bytes;
6361 p->second.tocoff_ = bytes;
6362 }
6363 else
6364 {
6365 off -= 8;
6366 if (off + 0x8000 < 0x10000)
6367 return 24;
6368 if (off + 0x80008000ULL < 0x100000000ULL)
6369 {
6370 if (off + 24 + (1 << 25) < 2 << 25)
6371 return 28;
6372 return 32;
6373 }
6374
6375 bytes = 32;
6376 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6377 && ((off >> 32) & 0xffff) != 0)
6378 bytes += 4;
6379 if (((off >> 32) & 0xffffffffULL) != 0)
6380 bytes += 4;
6381 if (hi(off) != 0)
6382 bytes += 4;
6383 if (l(off) != 0)
6384 bytes += 4;
6385 return bytes;
6386 }
6387 }
6388
6389 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6390 if (off + (1 << 25) < 2 << 25)
6391 return bytes + 4;
6392 if (!this->targ_->power10_stubs()
6393 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6394 *need_lt = true;
6395 return bytes + 16;
6396 }
6397
6398 template<int size, bool big_endian>
6399 void
6400 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6401 {
6402 if (p.sym_)
6403 gold_error(_("linkage table error against `%s'"),
6404 p.sym_->demangled_name().c_str());
6405 else
6406 gold_error(_("linkage table error against `%s:[local %u]'"),
6407 p.object_->name().c_str(),
6408 p.locsym_);
6409 }
6410
6411 // Write out plt and long branch stub code.
6412
6413 template<int size, bool big_endian>
6414 void
6415 Stub_table<size, big_endian>::do_write(Output_file* of)
6416 {
6417 if (this->plt_call_stubs_.empty()
6418 && this->long_branch_stubs_.empty())
6419 return;
6420
6421 const section_size_type start_off = this->offset();
6422 const section_size_type off = this->stub_offset();
6423 const section_size_type oview_size =
6424 convert_to_section_size_type(this->data_size() - (off - start_off));
6425 unsigned char* const oview = of->get_output_view(off, oview_size);
6426 unsigned char* p;
6427
6428 if (size == 64
6429 && this->targ_->power10_stubs())
6430 {
6431 if (!this->plt_call_stubs_.empty())
6432 {
6433 // Write out plt call stubs.
6434 typename Plt_stub_entries::const_iterator cs;
6435 for (cs = this->plt_call_stubs_.begin();
6436 cs != this->plt_call_stubs_.end();
6437 ++cs)
6438 {
6439 p = oview + cs->second.off_;
6440 const Output_data_plt_powerpc<size, big_endian>* plt;
6441 Address pltoff = this->plt_off(cs, &plt);
6442 Address plt_addr = pltoff + plt->address();
6443 if (this->targ_->power10_stubs_auto())
6444 {
6445 if (cs->second.notoc_)
6446 {
6447 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6448 this->build_tls_opt_head(&p, false);
6449 Address from = this->stub_address() + (p - oview);
6450 Address delta = plt_addr - from;
6451 p = build_power10_offset<big_endian>(p, delta, from & 4,
6452 true);
6453 write_insn<big_endian>(p, mtctr_12);
6454 p += 4;
6455 write_insn<big_endian>(p, bctr);
6456 p += 4;
6457 p = oview + this->plt_call_align(p - oview);
6458 }
6459 if (cs->second.toc_)
6460 {
6461 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6462 {
6463 bool save_lr
6464 = cs->second.r2save_ && !cs->second.localentry0_;
6465 this->build_tls_opt_head(&p, save_lr);
6466 }
6467 Address got_addr = this->targ_->toc_pointer();
6468 Address off = plt_addr - got_addr;
6469
6470 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6471 this->plt_error(cs->first);
6472
6473 if (cs->second.r2save_)
6474 {
6475 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6476 p += 4;
6477 }
6478 if (ha(off) != 0)
6479 {
6480 write_insn<big_endian>(p, addis_12_2 + ha(off));
6481 p += 4;
6482 write_insn<big_endian>(p, ld_12_12 + l(off));
6483 p += 4;
6484 }
6485 else
6486 {
6487 write_insn<big_endian>(p, ld_12_2 + l(off));
6488 p += 4;
6489 }
6490 write_insn<big_endian>(p, mtctr_12);
6491 p += 4;
6492 if (cs->second.r2save_
6493 && !cs->second.localentry0_
6494 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6495 this->build_tls_opt_tail(p);
6496 else
6497 write_insn<big_endian>(p, bctr);
6498 }
6499 if (cs->second.p9notoc_)
6500 {
6501 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6502 this->build_tls_opt_head(&p, false);
6503 Address from = this->stub_address() + (p - oview);
6504 Address delta = plt_addr - from;
6505 p = build_notoc_offset<big_endian>(p, delta, true);
6506 write_insn<big_endian>(p, mtctr_12);
6507 p += 4;
6508 write_insn<big_endian>(p, bctr);
6509 p += 4;
6510 p = oview + this->plt_call_align(p - oview);
6511 }
6512 }
6513 else
6514 {
6515 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6516 {
6517 bool save_lr
6518 = cs->second.r2save_ && !cs->second.localentry0_;
6519 this->build_tls_opt_head(&p, save_lr);
6520 }
6521 if (cs->second.r2save_)
6522 {
6523 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6524 p += 4;
6525 }
6526 Address from = this->stub_address() + (p - oview);
6527 Address delta = plt_addr - from;
6528 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6529 write_insn<big_endian>(p, mtctr_12);
6530 p += 4;
6531 if (cs->second.r2save_
6532 && !cs->second.localentry0_
6533 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6534 this->build_tls_opt_tail(p);
6535 else
6536 write_insn<big_endian>(p, bctr);
6537 }
6538 }
6539 }
6540
6541 // Write out long branch stubs.
6542 typename Branch_stub_entries::const_iterator bs;
6543 for (bs = this->long_branch_stubs_.begin();
6544 bs != this->long_branch_stubs_.end();
6545 ++bs)
6546 {
6547 if (bs->second.save_res_)
6548 continue;
6549 Address off = this->plt_size_ + bs->second.off_;
6550 p = oview + off;
6551 Address loc = this->stub_address() + off;
6552 Address delta = bs->first.dest_ - loc;
6553 if (this->targ_->power10_stubs_auto())
6554 {
6555 if (bs->second.notoc_)
6556 {
6557 unsigned char* startp = p;
6558 p = build_power10_offset<big_endian>(p, delta,
6559 loc & 4, false);
6560 delta -= p - startp;
6561 startp = p;
6562 if (delta + (1 << 25) < 2 << 25)
6563 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6564 else
6565 {
6566 write_insn<big_endian>(p, mtctr_12);
6567 p += 4;
6568 write_insn<big_endian>(p, bctr);
6569 }
6570 p += 4;
6571 delta -= p - startp;
6572 }
6573 if (bs->second.toc_)
6574 {
6575 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6576 if (delta + (1 << 25) >= 2 << 25)
6577 {
6578 Address brlt_addr
6579 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6580 gold_assert(brlt_addr != invalid_address);
6581 brlt_addr += this->targ_->brlt_section()->address();
6582 Address got_addr = this->targ_->toc_pointer();
6583 Address brltoff = brlt_addr - got_addr;
6584 if (ha(brltoff) == 0)
6585 {
6586 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6587 p += 4;
6588 }
6589 else
6590 {
6591 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6592 p += 4;
6593 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6594 p += 4;
6595 }
6596 }
6597 if (delta + (1 << 25) < 2 << 25)
6598 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6599 else
6600 {
6601 write_insn<big_endian>(p, mtctr_12);
6602 p += 4;
6603 write_insn<big_endian>(p, bctr);
6604 }
6605 }
6606 if (bs->second.p9notoc_)
6607 {
6608 unsigned char* startp = p;
6609 p = build_notoc_offset<big_endian>(p, delta, false);
6610 delta -= p - startp;
6611 startp = p;
6612 if (delta + (1 << 25) < 2 << 25)
6613 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6614 else
6615 {
6616 write_insn<big_endian>(p, mtctr_12);
6617 p += 4;
6618 write_insn<big_endian>(p, bctr);
6619 }
6620 p += 4;
6621 delta -= p - startp;
6622 }
6623 }
6624 else
6625 {
6626 if (!bs->second.notoc_)
6627 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6628 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6629 {
6630 unsigned char* startp = p;
6631 p = build_power10_offset<big_endian>(p, delta,
6632 loc & 4, false);
6633 delta -= p - startp;
6634 }
6635 if (delta + (1 << 25) < 2 << 25)
6636 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6637 else
6638 {
6639 write_insn<big_endian>(p, mtctr_12);
6640 p += 4;
6641 write_insn<big_endian>(p, bctr);
6642 }
6643 }
6644 }
6645 }
6646 else if (size == 64)
6647 {
6648
6649 if (!this->plt_call_stubs_.empty()
6650 && this->targ_->abiversion() >= 2)
6651 {
6652 // Write out plt call stubs for ELFv2.
6653 typename Plt_stub_entries::const_iterator cs;
6654 for (cs = this->plt_call_stubs_.begin();
6655 cs != this->plt_call_stubs_.end();
6656 ++cs)
6657 {
6658 const Output_data_plt_powerpc<size, big_endian>* plt;
6659 Address pltoff = this->plt_off(cs, &plt);
6660 Address plt_addr = pltoff + plt->address();
6661
6662 p = oview + cs->second.off_;
6663 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6664 {
6665 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6666 this->build_tls_opt_head(&p, save_lr);
6667 }
6668 if (cs->second.r2save_)
6669 {
6670 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6671 p += 4;
6672 }
6673 if (cs->second.p9notoc_)
6674 {
6675 Address from = this->stub_address() + (p - oview) + 8;
6676 Address off = plt_addr - from;
6677 p = build_notoc_offset<big_endian>(p, off, true);
6678 }
6679 else
6680 {
6681 Address got_addr = this->targ_->toc_pointer();
6682 Address off = plt_addr - got_addr;
6683
6684 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6685 this->plt_error(cs->first);
6686
6687 if (ha(off) != 0)
6688 {
6689 write_insn<big_endian>(p, addis_12_2 + ha(off));
6690 p += 4;
6691 write_insn<big_endian>(p, ld_12_12 + l(off));
6692 p += 4;
6693 }
6694 else
6695 {
6696 write_insn<big_endian>(p, ld_12_2 + l(off));
6697 p += 4;
6698 }
6699 }
6700 write_insn<big_endian>(p, mtctr_12);
6701 p += 4;
6702 if (cs->second.r2save_
6703 && !cs->second.localentry0_
6704 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6705 this->build_tls_opt_tail(p);
6706 else
6707 write_insn<big_endian>(p, bctr);
6708 }
6709 }
6710 else if (!this->plt_call_stubs_.empty())
6711 {
6712 // Write out plt call stubs for ELFv1.
6713 typename Plt_stub_entries::const_iterator cs;
6714 for (cs = this->plt_call_stubs_.begin();
6715 cs != this->plt_call_stubs_.end();
6716 ++cs)
6717 {
6718 const Output_data_plt_powerpc<size, big_endian>* plt;
6719 Address pltoff = this->plt_off(cs, &plt);
6720 Address plt_addr = pltoff + plt->address();
6721 Address got_addr = this->targ_->toc_pointer();
6722 Address off = plt_addr - got_addr;
6723
6724 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6725 || cs->second.notoc_)
6726 this->plt_error(cs->first);
6727
6728 bool static_chain = parameters->options().plt_static_chain();
6729 bool thread_safe = this->targ_->plt_thread_safe();
6730 bool use_fake_dep = false;
6731 Address cmp_branch_off = 0;
6732 if (thread_safe)
6733 {
6734 unsigned int pltindex
6735 = ((pltoff - this->targ_->first_plt_entry_offset())
6736 / this->targ_->plt_entry_size());
6737 Address glinkoff
6738 = (this->targ_->glink_section()->pltresolve_size()
6739 + pltindex * 8);
6740 if (pltindex > 32768)
6741 glinkoff += (pltindex - 32768) * 4;
6742 Address to
6743 = this->targ_->glink_section()->address() + glinkoff;
6744 Address from
6745 = (this->stub_address() + cs->second.off_ + 20
6746 + 4 * cs->second.r2save_
6747 + 4 * (ha(off) != 0)
6748 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6749 + 4 * static_chain);
6750 cmp_branch_off = to - from;
6751 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6752 }
6753
6754 p = oview + cs->second.off_;
6755 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6756 {
6757 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6758 this->build_tls_opt_head(&p, save_lr);
6759 use_fake_dep = thread_safe;
6760 }
6761 if (cs->second.r2save_)
6762 {
6763 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6764 p += 4;
6765 }
6766 if (ha(off) != 0)
6767 {
6768 write_insn<big_endian>(p, addis_11_2 + ha(off));
6769 p += 4;
6770 write_insn<big_endian>(p, ld_12_11 + l(off));
6771 p += 4;
6772 if (ha(off + 8 + 8 * static_chain) != ha(off))
6773 {
6774 write_insn<big_endian>(p, addi_11_11 + l(off));
6775 p += 4;
6776 off = 0;
6777 }
6778 write_insn<big_endian>(p, mtctr_12);
6779 p += 4;
6780 if (use_fake_dep)
6781 {
6782 write_insn<big_endian>(p, xor_2_12_12);
6783 p += 4;
6784 write_insn<big_endian>(p, add_11_11_2);
6785 p += 4;
6786 }
6787 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6788 p += 4;
6789 if (static_chain)
6790 {
6791 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6792 p += 4;
6793 }
6794 }
6795 else
6796 {
6797 write_insn<big_endian>(p, ld_12_2 + l(off));
6798 p += 4;
6799 if (ha(off + 8 + 8 * static_chain) != ha(off))
6800 {
6801 write_insn<big_endian>(p, addi_2_2 + l(off));
6802 p += 4;
6803 off = 0;
6804 }
6805 write_insn<big_endian>(p, mtctr_12);
6806 p += 4;
6807 if (use_fake_dep)
6808 {
6809 write_insn<big_endian>(p, xor_11_12_12);
6810 p += 4;
6811 write_insn<big_endian>(p, add_2_2_11);
6812 p += 4;
6813 }
6814 if (static_chain)
6815 {
6816 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6817 p += 4;
6818 }
6819 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6820 p += 4;
6821 }
6822 if (cs->second.r2save_
6823 && !cs->second.localentry0_
6824 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6825 this->build_tls_opt_tail(p);
6826 else if (thread_safe && !use_fake_dep)
6827 {
6828 write_insn<big_endian>(p, cmpldi_2_0);
6829 p += 4;
6830 write_insn<big_endian>(p, bnectr_p4);
6831 p += 4;
6832 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6833 }
6834 else
6835 write_insn<big_endian>(p, bctr);
6836 }
6837 }
6838
6839 // Write out long branch stubs.
6840 typename Branch_stub_entries::const_iterator bs;
6841 for (bs = this->long_branch_stubs_.begin();
6842 bs != this->long_branch_stubs_.end();
6843 ++bs)
6844 {
6845 if (bs->second.save_res_)
6846 continue;
6847 Address off = this->plt_size_ + bs->second.off_;
6848 p = oview + off;
6849 Address loc = this->stub_address() + off;
6850 Address delta = bs->first.dest_ - loc;
6851 if (!bs->second.p9notoc_)
6852 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6853 if (bs->second.p9notoc_)
6854 {
6855 unsigned char* startp = p;
6856 p = build_notoc_offset<big_endian>(p, off, false);
6857 delta -= p - startp;
6858 }
6859 else if (delta + (1 << 25) >= 2 << 25)
6860 {
6861 Address brlt_addr
6862 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6863 gold_assert(brlt_addr != invalid_address);
6864 brlt_addr += this->targ_->brlt_section()->address();
6865 Address got_addr = this->targ_->toc_pointer();
6866 Address brltoff = brlt_addr - got_addr;
6867 if (ha(brltoff) == 0)
6868 {
6869 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6870 p += 4;
6871 }
6872 else
6873 {
6874 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6875 p += 4;
6876 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6877 p += 4;
6878 }
6879 }
6880 if (delta + (1 << 25) < 2 << 25)
6881 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6882 else
6883 {
6884 write_insn<big_endian>(p, mtctr_12);
6885 p += 4;
6886 write_insn<big_endian>(p, bctr);
6887 }
6888 }
6889 }
6890 else // size == 32
6891 {
6892 if (!this->plt_call_stubs_.empty())
6893 {
6894 // The address of _GLOBAL_OFFSET_TABLE_.
6895 Address g_o_t = invalid_address;
6896
6897 // Write out plt call stubs.
6898 typename Plt_stub_entries::const_iterator cs;
6899 for (cs = this->plt_call_stubs_.begin();
6900 cs != this->plt_call_stubs_.end();
6901 ++cs)
6902 {
6903 const Output_data_plt_powerpc<size, big_endian>* plt;
6904 Address plt_addr = this->plt_off(cs, &plt);
6905 plt_addr += plt->address();
6906
6907 p = oview + cs->second.off_;
6908 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6909 this->build_tls_opt_head(&p, false);
6910 if (parameters->options().output_is_position_independent())
6911 {
6912 Address got_addr;
6913 const Powerpc_relobj<size, big_endian>* ppcobj
6914 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6915 (cs->first.object_));
6916 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6917 {
6918 unsigned int got2 = ppcobj->got2_shndx();
6919 got_addr = ppcobj->get_output_section_offset(got2);
6920 gold_assert(got_addr != invalid_address);
6921 got_addr += (ppcobj->output_section(got2)->address()
6922 + cs->first.addend_);
6923 }
6924 else
6925 {
6926 if (g_o_t == invalid_address)
6927 g_o_t = this->targ_->toc_pointer();
6928 got_addr = g_o_t;
6929 }
6930
6931 Address off = plt_addr - got_addr;
6932 if (ha(off) == 0)
6933 write_insn<big_endian>(p, lwz_11_30 + l(off));
6934 else
6935 {
6936 write_insn<big_endian>(p, addis_11_30 + ha(off));
6937 p += 4;
6938 write_insn<big_endian>(p, lwz_11_11 + l(off));
6939 }
6940 }
6941 else
6942 {
6943 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6944 p += 4;
6945 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6946 }
6947 p += 4;
6948 write_insn<big_endian>(p, mtctr_11);
6949 p += 4;
6950 write_insn<big_endian>(p, bctr);
6951 }
6952 }
6953
6954 // Write out long branch stubs.
6955 typename Branch_stub_entries::const_iterator bs;
6956 for (bs = this->long_branch_stubs_.begin();
6957 bs != this->long_branch_stubs_.end();
6958 ++bs)
6959 {
6960 if (bs->second.save_res_)
6961 continue;
6962 Address off = this->plt_size_ + bs->second.off_;
6963 p = oview + off;
6964 Address loc = this->stub_address() + off;
6965 Address delta = bs->first.dest_ - loc;
6966 if (delta + (1 << 25) < 2 << 25)
6967 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6968 else if (!parameters->options().output_is_position_independent())
6969 {
6970 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6971 p += 4;
6972 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6973 }
6974 else
6975 {
6976 delta -= 8;
6977 write_insn<big_endian>(p, mflr_0);
6978 p += 4;
6979 write_insn<big_endian>(p, bcl_20_31);
6980 p += 4;
6981 write_insn<big_endian>(p, mflr_12);
6982 p += 4;
6983 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6984 p += 4;
6985 write_insn<big_endian>(p, addi_12_12 + l(delta));
6986 p += 4;
6987 write_insn<big_endian>(p, mtlr_0);
6988 }
6989 p += 4;
6990 write_insn<big_endian>(p, mtctr_12);
6991 p += 4;
6992 write_insn<big_endian>(p, bctr);
6993 }
6994 }
6995 if (this->need_save_res_)
6996 {
6997 p = oview + this->plt_size_ + this->branch_size_;
6998 memcpy (p, this->targ_->savres_section()->contents(),
6999 this->targ_->savres_section()->data_size());
7000 }
7001 }
7002
7003 // Write out .glink.
7004
7005 template<int size, bool big_endian>
7006 void
7007 Output_data_glink<size, big_endian>::do_write(Output_file* of)
7008 {
7009 const section_size_type off = this->offset();
7010 const section_size_type oview_size =
7011 convert_to_section_size_type(this->data_size());
7012 unsigned char* const oview = of->get_output_view(off, oview_size);
7013 unsigned char* p;
7014
7015 // The base address of the .plt section.
7016 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7017 Address plt_base = this->targ_->plt_section()->address();
7018
7019 if (size == 64)
7020 {
7021 if (this->end_branch_table_ != 0)
7022 {
7023 // Write pltresolve stub.
7024 p = oview;
7025 Address after_bcl = this->address() + 16;
7026 Address pltoff = plt_base - after_bcl;
7027
7028 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
7029
7030 if (this->targ_->abiversion() < 2)
7031 {
7032 write_insn<big_endian>(p, mflr_12), p += 4;
7033 write_insn<big_endian>(p, bcl_20_31), p += 4;
7034 write_insn<big_endian>(p, mflr_11), p += 4;
7035 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
7036 write_insn<big_endian>(p, mtlr_12), p += 4;
7037 write_insn<big_endian>(p, add_11_2_11), p += 4;
7038 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7039 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
7040 write_insn<big_endian>(p, mtctr_12), p += 4;
7041 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
7042 }
7043 else
7044 {
7045 if (this->targ_->has_localentry0())
7046 {
7047 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
7048 }
7049 write_insn<big_endian>(p, mflr_0), p += 4;
7050 write_insn<big_endian>(p, bcl_20_31), p += 4;
7051 write_insn<big_endian>(p, mflr_11), p += 4;
7052 write_insn<big_endian>(p, mtlr_0), p += 4;
7053 if (this->targ_->has_localentry0())
7054 {
7055 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
7056 }
7057 else
7058 {
7059 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
7060 }
7061 write_insn<big_endian>(p, sub_12_12_11), p += 4;
7062 write_insn<big_endian>(p, add_11_0_11), p += 4;
7063 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
7064 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7065 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
7066 write_insn<big_endian>(p, mtctr_12), p += 4;
7067 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
7068 }
7069 write_insn<big_endian>(p, bctr), p += 4;
7070 gold_assert(p == oview + this->pltresolve_size());
7071
7072 // Write lazy link call stubs.
7073 uint32_t indx = 0;
7074 while (p < oview + this->end_branch_table_)
7075 {
7076 if (this->targ_->abiversion() < 2)
7077 {
7078 if (indx < 0x8000)
7079 {
7080 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
7081 }
7082 else
7083 {
7084 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
7085 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
7086 }
7087 }
7088 uint32_t branch_off = 8 - (p - oview);
7089 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
7090 indx++;
7091 }
7092 }
7093
7094 Address plt_base = this->targ_->plt_section()->address();
7095 Address iplt_base = invalid_address;
7096 unsigned int global_entry_off = this->global_entry_off();
7097 Address global_entry_base = this->address() + global_entry_off;
7098 typename Global_entry_stub_entries::const_iterator ge;
7099 for (ge = this->global_entry_stubs_.begin();
7100 ge != this->global_entry_stubs_.end();
7101 ++ge)
7102 {
7103 p = oview + global_entry_off + ge->second;
7104 Address plt_addr = ge->first->plt_offset();
7105 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7106 && ge->first->can_use_relative_reloc(false))
7107 {
7108 if (iplt_base == invalid_address)
7109 iplt_base = this->targ_->iplt_section()->address();
7110 plt_addr += iplt_base;
7111 }
7112 else
7113 plt_addr += plt_base;
7114 Address my_addr = global_entry_base + ge->second;
7115 Address off = plt_addr - my_addr;
7116
7117 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
7118 gold_error(_("linkage table error against `%s'"),
7119 ge->first->demangled_name().c_str());
7120
7121 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
7122 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
7123 write_insn<big_endian>(p, mtctr_12), p += 4;
7124 write_insn<big_endian>(p, bctr);
7125 }
7126 }
7127 else
7128 {
7129 // The address of _GLOBAL_OFFSET_TABLE_.
7130 Address g_o_t = this->targ_->toc_pointer();
7131
7132 // Write out pltresolve branch table.
7133 p = oview;
7134 unsigned int the_end = oview_size - this->pltresolve_size();
7135 unsigned char* end_p = oview + the_end;
7136 while (p < end_p - 8 * 4)
7137 write_insn<big_endian>(p, b + end_p - p), p += 4;
7138 while (p < end_p)
7139 write_insn<big_endian>(p, nop), p += 4;
7140
7141 // Write out pltresolve call stub.
7142 end_p = oview + oview_size;
7143 if (parameters->options().output_is_position_independent())
7144 {
7145 Address res0_off = 0;
7146 Address after_bcl_off = the_end + 12;
7147 Address bcl_res0 = after_bcl_off - res0_off;
7148
7149 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7150 p += 4;
7151 write_insn<big_endian>(p, mflr_0);
7152 p += 4;
7153 write_insn<big_endian>(p, bcl_20_31);
7154 p += 4;
7155 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7156 p += 4;
7157 write_insn<big_endian>(p, mflr_12);
7158 p += 4;
7159 write_insn<big_endian>(p, mtlr_0);
7160 p += 4;
7161 write_insn<big_endian>(p, sub_11_11_12);
7162 p += 4;
7163
7164 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
7165
7166 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7167 p += 4;
7168 if (ha(got_bcl) == ha(got_bcl + 4))
7169 {
7170 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7171 p += 4;
7172 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
7173 }
7174 else
7175 {
7176 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7177 p += 4;
7178 write_insn<big_endian>(p, lwz_12_12 + 4);
7179 }
7180 p += 4;
7181 write_insn<big_endian>(p, mtctr_0);
7182 p += 4;
7183 write_insn<big_endian>(p, add_0_11_11);
7184 p += 4;
7185 write_insn<big_endian>(p, add_11_0_11);
7186 }
7187 else
7188 {
7189 Address res0 = this->address();
7190
7191 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7192 p += 4;
7193 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7194 p += 4;
7195 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7196 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7197 else
7198 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7199 p += 4;
7200 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7201 p += 4;
7202 write_insn<big_endian>(p, mtctr_0);
7203 p += 4;
7204 write_insn<big_endian>(p, add_0_11_11);
7205 p += 4;
7206 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7207 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7208 else
7209 write_insn<big_endian>(p, lwz_12_12 + 4);
7210 p += 4;
7211 write_insn<big_endian>(p, add_11_0_11);
7212 }
7213 p += 4;
7214 write_insn<big_endian>(p, bctr);
7215 p += 4;
7216 while (p < end_p)
7217 {
7218 write_insn<big_endian>(p, nop);
7219 p += 4;
7220 }
7221 }
7222
7223 of->write_output_view(off, oview_size, oview);
7224 }
7225
7226
7227 // A class to handle linker generated save/restore functions.
7228
7229 template<int size, bool big_endian>
7230 class Output_data_save_res : public Output_section_data_build
7231 {
7232 public:
7233 Output_data_save_res(Symbol_table* symtab);
7234
7235 const unsigned char*
7236 contents() const
7237 {
7238 return contents_;
7239 }
7240
7241 protected:
7242 // Write to a map file.
7243 void
7244 do_print_to_mapfile(Mapfile* mapfile) const
7245 { mapfile->print_output_data(this, _("** save/restore")); }
7246
7247 void
7248 do_write(Output_file*);
7249
7250 private:
7251 // The maximum size of save/restore contents.
7252 static const unsigned int savres_max = 218*4;
7253
7254 void
7255 savres_define(Symbol_table* symtab,
7256 const char *name,
7257 unsigned int lo, unsigned int hi,
7258 unsigned char* write_ent(unsigned char*, int),
7259 unsigned char* write_tail(unsigned char*, int));
7260
7261 unsigned char *contents_;
7262 };
7263
7264 template<bool big_endian>
7265 static unsigned char*
7266 savegpr0(unsigned char* p, int r)
7267 {
7268 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7269 write_insn<big_endian>(p, insn);
7270 return p + 4;
7271 }
7272
7273 template<bool big_endian>
7274 static unsigned char*
7275 savegpr0_tail(unsigned char* p, int r)
7276 {
7277 p = savegpr0<big_endian>(p, r);
7278 uint32_t insn = std_0_1 + 16;
7279 write_insn<big_endian>(p, insn);
7280 p = p + 4;
7281 write_insn<big_endian>(p, blr);
7282 return p + 4;
7283 }
7284
7285 template<bool big_endian>
7286 static unsigned char*
7287 restgpr0(unsigned char* p, int r)
7288 {
7289 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7290 write_insn<big_endian>(p, insn);
7291 return p + 4;
7292 }
7293
7294 template<bool big_endian>
7295 static unsigned char*
7296 restgpr0_tail(unsigned char* p, int r)
7297 {
7298 uint32_t insn = ld_0_1 + 16;
7299 write_insn<big_endian>(p, insn);
7300 p = p + 4;
7301 p = restgpr0<big_endian>(p, r);
7302 write_insn<big_endian>(p, mtlr_0);
7303 p = p + 4;
7304 if (r == 29)
7305 {
7306 p = restgpr0<big_endian>(p, 30);
7307 p = restgpr0<big_endian>(p, 31);
7308 }
7309 write_insn<big_endian>(p, blr);
7310 return p + 4;
7311 }
7312
7313 template<bool big_endian>
7314 static unsigned char*
7315 savegpr1(unsigned char* p, int r)
7316 {
7317 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7318 write_insn<big_endian>(p, insn);
7319 return p + 4;
7320 }
7321
7322 template<bool big_endian>
7323 static unsigned char*
7324 savegpr1_tail(unsigned char* p, int r)
7325 {
7326 p = savegpr1<big_endian>(p, r);
7327 write_insn<big_endian>(p, blr);
7328 return p + 4;
7329 }
7330
7331 template<bool big_endian>
7332 static unsigned char*
7333 restgpr1(unsigned char* p, int r)
7334 {
7335 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7336 write_insn<big_endian>(p, insn);
7337 return p + 4;
7338 }
7339
7340 template<bool big_endian>
7341 static unsigned char*
7342 restgpr1_tail(unsigned char* p, int r)
7343 {
7344 p = restgpr1<big_endian>(p, r);
7345 write_insn<big_endian>(p, blr);
7346 return p + 4;
7347 }
7348
7349 template<bool big_endian>
7350 static unsigned char*
7351 savefpr(unsigned char* p, int r)
7352 {
7353 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7354 write_insn<big_endian>(p, insn);
7355 return p + 4;
7356 }
7357
7358 template<bool big_endian>
7359 static unsigned char*
7360 savefpr0_tail(unsigned char* p, int r)
7361 {
7362 p = savefpr<big_endian>(p, r);
7363 write_insn<big_endian>(p, std_0_1 + 16);
7364 p = p + 4;
7365 write_insn<big_endian>(p, blr);
7366 return p + 4;
7367 }
7368
7369 template<bool big_endian>
7370 static unsigned char*
7371 restfpr(unsigned char* p, int r)
7372 {
7373 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7374 write_insn<big_endian>(p, insn);
7375 return p + 4;
7376 }
7377
7378 template<bool big_endian>
7379 static unsigned char*
7380 restfpr0_tail(unsigned char* p, int r)
7381 {
7382 write_insn<big_endian>(p, ld_0_1 + 16);
7383 p = p + 4;
7384 p = restfpr<big_endian>(p, r);
7385 write_insn<big_endian>(p, mtlr_0);
7386 p = p + 4;
7387 if (r == 29)
7388 {
7389 p = restfpr<big_endian>(p, 30);
7390 p = restfpr<big_endian>(p, 31);
7391 }
7392 write_insn<big_endian>(p, blr);
7393 return p + 4;
7394 }
7395
7396 template<bool big_endian>
7397 static unsigned char*
7398 savefpr1_tail(unsigned char* p, int r)
7399 {
7400 p = savefpr<big_endian>(p, r);
7401 write_insn<big_endian>(p, blr);
7402 return p + 4;
7403 }
7404
7405 template<bool big_endian>
7406 static unsigned char*
7407 restfpr1_tail(unsigned char* p, int r)
7408 {
7409 p = restfpr<big_endian>(p, r);
7410 write_insn<big_endian>(p, blr);
7411 return p + 4;
7412 }
7413
7414 template<bool big_endian>
7415 static unsigned char*
7416 savevr(unsigned char* p, int r)
7417 {
7418 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7419 write_insn<big_endian>(p, insn);
7420 p = p + 4;
7421 insn = stvx_0_12_0 + (r << 21);
7422 write_insn<big_endian>(p, insn);
7423 return p + 4;
7424 }
7425
7426 template<bool big_endian>
7427 static unsigned char*
7428 savevr_tail(unsigned char* p, int r)
7429 {
7430 p = savevr<big_endian>(p, r);
7431 write_insn<big_endian>(p, blr);
7432 return p + 4;
7433 }
7434
7435 template<bool big_endian>
7436 static unsigned char*
7437 restvr(unsigned char* p, int r)
7438 {
7439 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7440 write_insn<big_endian>(p, insn);
7441 p = p + 4;
7442 insn = lvx_0_12_0 + (r << 21);
7443 write_insn<big_endian>(p, insn);
7444 return p + 4;
7445 }
7446
7447 template<bool big_endian>
7448 static unsigned char*
7449 restvr_tail(unsigned char* p, int r)
7450 {
7451 p = restvr<big_endian>(p, r);
7452 write_insn<big_endian>(p, blr);
7453 return p + 4;
7454 }
7455
7456
7457 template<int size, bool big_endian>
7458 Output_data_save_res<size, big_endian>::Output_data_save_res(
7459 Symbol_table* symtab)
7460 : Output_section_data_build(4),
7461 contents_(NULL)
7462 {
7463 this->savres_define(symtab,
7464 "_savegpr0_", 14, 31,
7465 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7466 this->savres_define(symtab,
7467 "_restgpr0_", 14, 29,
7468 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7469 this->savres_define(symtab,
7470 "_restgpr0_", 30, 31,
7471 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7472 this->savres_define(symtab,
7473 "_savegpr1_", 14, 31,
7474 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7475 this->savres_define(symtab,
7476 "_restgpr1_", 14, 31,
7477 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7478 this->savres_define(symtab,
7479 "_savefpr_", 14, 31,
7480 savefpr<big_endian>, savefpr0_tail<big_endian>);
7481 this->savres_define(symtab,
7482 "_restfpr_", 14, 29,
7483 restfpr<big_endian>, restfpr0_tail<big_endian>);
7484 this->savres_define(symtab,
7485 "_restfpr_", 30, 31,
7486 restfpr<big_endian>, restfpr0_tail<big_endian>);
7487 this->savres_define(symtab,
7488 "._savef", 14, 31,
7489 savefpr<big_endian>, savefpr1_tail<big_endian>);
7490 this->savres_define(symtab,
7491 "._restf", 14, 31,
7492 restfpr<big_endian>, restfpr1_tail<big_endian>);
7493 this->savres_define(symtab,
7494 "_savevr_", 20, 31,
7495 savevr<big_endian>, savevr_tail<big_endian>);
7496 this->savres_define(symtab,
7497 "_restvr_", 20, 31,
7498 restvr<big_endian>, restvr_tail<big_endian>);
7499 }
7500
7501 template<int size, bool big_endian>
7502 void
7503 Output_data_save_res<size, big_endian>::savres_define(
7504 Symbol_table* symtab,
7505 const char *name,
7506 unsigned int lo, unsigned int hi,
7507 unsigned char* write_ent(unsigned char*, int),
7508 unsigned char* write_tail(unsigned char*, int))
7509 {
7510 size_t len = strlen(name);
7511 bool writing = false;
7512 char sym[16];
7513
7514 memcpy(sym, name, len);
7515 sym[len + 2] = 0;
7516
7517 for (unsigned int i = lo; i <= hi; i++)
7518 {
7519 sym[len + 0] = i / 10 + '0';
7520 sym[len + 1] = i % 10 + '0';
7521 Symbol* gsym = symtab->lookup(sym);
7522 bool refd = gsym != NULL && gsym->is_undefined();
7523 writing = writing || refd;
7524 if (writing)
7525 {
7526 if (this->contents_ == NULL)
7527 this->contents_ = new unsigned char[this->savres_max];
7528
7529 section_size_type value = this->current_data_size();
7530 unsigned char* p = this->contents_ + value;
7531 if (i != hi)
7532 p = write_ent(p, i);
7533 else
7534 p = write_tail(p, i);
7535 section_size_type cur_size = p - this->contents_;
7536 this->set_current_data_size(cur_size);
7537 if (refd)
7538 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7539 this, value, cur_size - value,
7540 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7541 elfcpp::STV_HIDDEN, 0, false, false);
7542 }
7543 }
7544 }
7545
7546 // Write out save/restore.
7547
7548 template<int size, bool big_endian>
7549 void
7550 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7551 {
7552 const section_size_type off = this->offset();
7553 const section_size_type oview_size =
7554 convert_to_section_size_type(this->data_size());
7555 unsigned char* const oview = of->get_output_view(off, oview_size);
7556 memcpy(oview, this->contents_, oview_size);
7557 of->write_output_view(off, oview_size, oview);
7558 }
7559
7560
7561 // Create the glink section.
7562
7563 template<int size, bool big_endian>
7564 void
7565 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7566 {
7567 if (this->glink_ == NULL)
7568 {
7569 this->glink_ = new Output_data_glink<size, big_endian>(this);
7570 this->glink_->add_eh_frame(layout);
7571 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7572 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7573 this->glink_, ORDER_TEXT, false);
7574 }
7575 }
7576
7577 // Create a PLT entry for a global symbol.
7578
7579 template<int size, bool big_endian>
7580 void
7581 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7582 Layout* layout,
7583 Symbol* gsym)
7584 {
7585 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7586 && gsym->can_use_relative_reloc(false))
7587 {
7588 if (this->iplt_ == NULL)
7589 this->make_iplt_section(symtab, layout);
7590 this->iplt_->add_ifunc_entry(gsym);
7591 }
7592 else
7593 {
7594 if (this->plt_ == NULL)
7595 this->make_plt_section(symtab, layout);
7596 this->plt_->add_entry(gsym);
7597 }
7598 }
7599
7600 // Make a PLT entry for a local symbol.
7601
7602 template<int size, bool big_endian>
7603 void
7604 Target_powerpc<size, big_endian>::make_local_plt_entry(
7605 Symbol_table* symtab,
7606 Layout* layout,
7607 Sized_relobj_file<size, big_endian>* relobj,
7608 unsigned int r_sym)
7609 {
7610 if (this->lplt_ == NULL)
7611 this->make_lplt_section(symtab, layout);
7612 this->lplt_->add_local_entry(relobj, r_sym);
7613 }
7614
7615 template<int size, bool big_endian>
7616 void
7617 Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7618 Layout* layout,
7619 Symbol* gsym)
7620 {
7621 if (this->lplt_ == NULL)
7622 this->make_lplt_section(symtab, layout);
7623 this->lplt_->add_entry(gsym, true);
7624 }
7625
7626 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7627
7628 template<int size, bool big_endian>
7629 void
7630 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7631 Symbol_table* symtab,
7632 Layout* layout,
7633 Sized_relobj_file<size, big_endian>* relobj,
7634 unsigned int r_sym)
7635 {
7636 if (this->iplt_ == NULL)
7637 this->make_iplt_section(symtab, layout);
7638 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7639 }
7640
7641 // Return the number of entries in the PLT.
7642
7643 template<int size, bool big_endian>
7644 unsigned int
7645 Target_powerpc<size, big_endian>::plt_entry_count() const
7646 {
7647 if (this->plt_ == NULL)
7648 return 0;
7649 return this->plt_->entry_count();
7650 }
7651
7652 // Create a GOT entry for local dynamic __tls_get_addr calls.
7653
7654 template<int size, bool big_endian>
7655 unsigned int
7656 Target_powerpc<size, big_endian>::tlsld_got_offset(
7657 Symbol_table* symtab,
7658 Layout* layout,
7659 Sized_relobj_file<size, big_endian>* object)
7660 {
7661 if (this->tlsld_got_offset_ == -1U)
7662 {
7663 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7664 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7665 Output_data_got_powerpc<size, big_endian>* got
7666 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
7667 unsigned int got_offset = got->add_constant_pair(0, 0);
7668 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7669 got_offset, 0);
7670 this->tlsld_got_offset_ = got_offset;
7671 }
7672 return this->tlsld_got_offset_;
7673 }
7674
7675 // Get the Reference_flags for a particular relocation.
7676
7677 template<int size, bool big_endian>
7678 int
7679 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7680 unsigned int r_type,
7681 const Target_powerpc* target)
7682 {
7683 int ref = 0;
7684
7685 switch (r_type)
7686 {
7687 case elfcpp::R_PPC64_TOC:
7688 if (size != 64)
7689 break;
7690 // Fall through.
7691 case elfcpp::R_POWERPC_NONE:
7692 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7693 case elfcpp::R_POWERPC_GNU_VTENTRY:
7694 // No symbol reference.
7695 break;
7696
7697 case elfcpp::R_PPC64_ADDR64:
7698 case elfcpp::R_PPC64_UADDR64:
7699 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7700 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7701 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7702 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7703 case elfcpp::R_PPC64_D34:
7704 case elfcpp::R_PPC64_D34_LO:
7705 case elfcpp::R_PPC64_D34_HI30:
7706 case elfcpp::R_PPC64_D34_HA30:
7707 case elfcpp::R_PPC64_D28:
7708 if (size != 64)
7709 break;
7710 // Fall through.
7711 case elfcpp::R_POWERPC_ADDR32:
7712 case elfcpp::R_POWERPC_UADDR32:
7713 case elfcpp::R_POWERPC_ADDR16:
7714 case elfcpp::R_POWERPC_UADDR16:
7715 case elfcpp::R_POWERPC_ADDR16_LO:
7716 case elfcpp::R_POWERPC_ADDR16_HI:
7717 case elfcpp::R_POWERPC_ADDR16_HA:
7718 ref = Symbol::ABSOLUTE_REF;
7719 break;
7720
7721 case elfcpp::R_POWERPC_ADDR24:
7722 case elfcpp::R_POWERPC_ADDR14:
7723 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7724 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7725 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7726 break;
7727
7728 case elfcpp::R_PPC_LOCAL24PC:
7729 if (size != 32)
7730 break;
7731 // Fall through.
7732 ref = Symbol::RELATIVE_REF;
7733 break;
7734
7735 case elfcpp::R_PPC64_REL64:
7736 case elfcpp::R_PPC64_REL16_HIGH:
7737 case elfcpp::R_PPC64_REL16_HIGHA:
7738 case elfcpp::R_PPC64_REL16_HIGHER:
7739 case elfcpp::R_PPC64_REL16_HIGHERA:
7740 case elfcpp::R_PPC64_REL16_HIGHEST:
7741 case elfcpp::R_PPC64_REL16_HIGHESTA:
7742 case elfcpp::R_PPC64_PCREL34:
7743 case elfcpp::R_PPC64_REL16_HIGHER34:
7744 case elfcpp::R_PPC64_REL16_HIGHERA34:
7745 case elfcpp::R_PPC64_REL16_HIGHEST34:
7746 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7747 case elfcpp::R_PPC64_PCREL28:
7748 if (size != 64)
7749 break;
7750 // Fall through.
7751 case elfcpp::R_POWERPC_REL32:
7752 case elfcpp::R_POWERPC_REL16:
7753 case elfcpp::R_POWERPC_REL16_LO:
7754 case elfcpp::R_POWERPC_REL16_HI:
7755 case elfcpp::R_POWERPC_REL16_HA:
7756 ref = Symbol::RELATIVE_REF;
7757 break;
7758
7759 case elfcpp::R_PPC_PLTREL24:
7760 if (size != 32)
7761 break;
7762 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7763 break;
7764
7765 case elfcpp::R_PPC64_REL24_NOTOC:
7766 case elfcpp::R_PPC64_REL24_P9NOTOC:
7767 case elfcpp::R_PPC64_PLT16_LO_DS:
7768 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7769 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7770 case elfcpp::R_PPC64_PLT_PCREL34:
7771 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7772 if (size != 64)
7773 break;
7774 // Fall through.
7775 case elfcpp::R_POWERPC_REL24:
7776 case elfcpp::R_POWERPC_REL14:
7777 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7778 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7779 case elfcpp::R_POWERPC_PLT16_LO:
7780 case elfcpp::R_POWERPC_PLT16_HI:
7781 case elfcpp::R_POWERPC_PLT16_HA:
7782 case elfcpp::R_POWERPC_PLTSEQ:
7783 case elfcpp::R_POWERPC_PLTCALL:
7784 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7785 break;
7786
7787 case elfcpp::R_PPC64_GOT16_DS:
7788 case elfcpp::R_PPC64_GOT16_LO_DS:
7789 case elfcpp::R_PPC64_GOT_PCREL34:
7790 case elfcpp::R_PPC64_TOC16:
7791 case elfcpp::R_PPC64_TOC16_LO:
7792 case elfcpp::R_PPC64_TOC16_HI:
7793 case elfcpp::R_PPC64_TOC16_HA:
7794 case elfcpp::R_PPC64_TOC16_DS:
7795 case elfcpp::R_PPC64_TOC16_LO_DS:
7796 if (size != 64)
7797 break;
7798 // Fall through.
7799 case elfcpp::R_POWERPC_GOT16:
7800 case elfcpp::R_POWERPC_GOT16_LO:
7801 case elfcpp::R_POWERPC_GOT16_HI:
7802 case elfcpp::R_POWERPC_GOT16_HA:
7803 ref = Symbol::RELATIVE_REF;
7804 break;
7805
7806 case elfcpp::R_PPC64_TLSGD:
7807 case elfcpp::R_PPC64_TLSLD:
7808 case elfcpp::R_PPC64_TPREL34:
7809 case elfcpp::R_PPC64_DTPREL34:
7810 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7811 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7812 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7813 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7814 if (size != 64)
7815 break;
7816 // Fall through.
7817 case elfcpp::R_POWERPC_GOT_TPREL16:
7818 case elfcpp::R_POWERPC_TLS:
7819 ref = Symbol::TLS_REF;
7820 break;
7821
7822 case elfcpp::R_POWERPC_COPY:
7823 case elfcpp::R_POWERPC_GLOB_DAT:
7824 case elfcpp::R_POWERPC_JMP_SLOT:
7825 case elfcpp::R_POWERPC_RELATIVE:
7826 case elfcpp::R_POWERPC_DTPMOD:
7827 default:
7828 // Not expected. We will give an error later.
7829 break;
7830 }
7831
7832 if (size == 64 && target->abiversion() < 2)
7833 ref |= Symbol::FUNC_DESC_ABI;
7834 return ref;
7835 }
7836
7837 // Report an unsupported relocation against a local symbol.
7838
7839 template<int size, bool big_endian>
7840 void
7841 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7842 Sized_relobj_file<size, big_endian>* object,
7843 unsigned int r_type)
7844 {
7845 gold_error(_("%s: unsupported reloc %u against local symbol"),
7846 object->name().c_str(), r_type);
7847 }
7848
7849 // We are about to emit a dynamic relocation of type R_TYPE. If the
7850 // dynamic linker does not support it, issue an error.
7851
7852 template<int size, bool big_endian>
7853 void
7854 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7855 unsigned int r_type)
7856 {
7857 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7858
7859 // These are the relocation types supported by glibc for both 32-bit
7860 // and 64-bit powerpc.
7861 switch (r_type)
7862 {
7863 case elfcpp::R_POWERPC_NONE:
7864 case elfcpp::R_POWERPC_RELATIVE:
7865 case elfcpp::R_POWERPC_GLOB_DAT:
7866 case elfcpp::R_POWERPC_DTPMOD:
7867 case elfcpp::R_POWERPC_DTPREL:
7868 case elfcpp::R_POWERPC_TPREL:
7869 case elfcpp::R_POWERPC_JMP_SLOT:
7870 case elfcpp::R_POWERPC_COPY:
7871 case elfcpp::R_POWERPC_IRELATIVE:
7872 case elfcpp::R_POWERPC_ADDR32:
7873 case elfcpp::R_POWERPC_UADDR32:
7874 case elfcpp::R_POWERPC_ADDR24:
7875 case elfcpp::R_POWERPC_ADDR16:
7876 case elfcpp::R_POWERPC_UADDR16:
7877 case elfcpp::R_POWERPC_ADDR16_LO:
7878 case elfcpp::R_POWERPC_ADDR16_HI:
7879 case elfcpp::R_POWERPC_ADDR16_HA:
7880 case elfcpp::R_POWERPC_ADDR14:
7881 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7882 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7883 case elfcpp::R_POWERPC_REL32:
7884 case elfcpp::R_POWERPC_TPREL16:
7885 case elfcpp::R_POWERPC_TPREL16_LO:
7886 case elfcpp::R_POWERPC_TPREL16_HI:
7887 case elfcpp::R_POWERPC_TPREL16_HA:
7888 return;
7889
7890 default:
7891 break;
7892 }
7893
7894 if (size == 64)
7895 {
7896 switch (r_type)
7897 {
7898 // These are the relocation types supported only on 64-bit.
7899 case elfcpp::R_PPC64_ADDR64:
7900 case elfcpp::R_PPC64_UADDR64:
7901 case elfcpp::R_PPC64_JMP_IREL:
7902 case elfcpp::R_PPC64_ADDR16_DS:
7903 case elfcpp::R_PPC64_ADDR16_LO_DS:
7904 case elfcpp::R_PPC64_ADDR16_HIGH:
7905 case elfcpp::R_PPC64_ADDR16_HIGHA:
7906 case elfcpp::R_PPC64_ADDR16_HIGHER:
7907 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7908 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7909 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7910 case elfcpp::R_PPC64_REL64:
7911 case elfcpp::R_POWERPC_ADDR30:
7912 case elfcpp::R_PPC64_TPREL16_DS:
7913 case elfcpp::R_PPC64_TPREL16_LO_DS:
7914 case elfcpp::R_PPC64_TPREL16_HIGH:
7915 case elfcpp::R_PPC64_TPREL16_HIGHA:
7916 case elfcpp::R_PPC64_TPREL16_HIGHER:
7917 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7918 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7919 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7920 return;
7921
7922 default:
7923 break;
7924 }
7925 }
7926 else
7927 {
7928 switch (r_type)
7929 {
7930 // These are the relocation types supported only on 32-bit.
7931 // ??? glibc ld.so doesn't need to support these.
7932 case elfcpp::R_POWERPC_REL24:
7933 case elfcpp::R_POWERPC_DTPREL16:
7934 case elfcpp::R_POWERPC_DTPREL16_LO:
7935 case elfcpp::R_POWERPC_DTPREL16_HI:
7936 case elfcpp::R_POWERPC_DTPREL16_HA:
7937 return;
7938
7939 default:
7940 break;
7941 }
7942 }
7943
7944 // This prevents us from issuing more than one error per reloc
7945 // section. But we can still wind up issuing more than one
7946 // error per object file.
7947 if (this->issued_non_pic_error_)
7948 return;
7949 gold_assert(parameters->options().output_is_position_independent());
7950 object->error(_("requires unsupported dynamic reloc; "
7951 "recompile with -fPIC"));
7952 this->issued_non_pic_error_ = true;
7953 return;
7954 }
7955
7956 // Return whether we need to make a PLT entry for a relocation of the
7957 // given type against a STT_GNU_IFUNC symbol.
7958
7959 template<int size, bool big_endian>
7960 bool
7961 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7962 Target_powerpc<size, big_endian>* target,
7963 Sized_relobj_file<size, big_endian>* object,
7964 unsigned int r_type,
7965 bool report_err)
7966 {
7967 // In non-pic code any reference will resolve to the plt call stub
7968 // for the ifunc symbol.
7969 if ((size == 32 || target->abiversion() >= 2)
7970 && !parameters->options().output_is_position_independent())
7971 return true;
7972
7973 switch (r_type)
7974 {
7975 // Word size refs from data sections are OK, but don't need a PLT entry.
7976 case elfcpp::R_POWERPC_ADDR32:
7977 case elfcpp::R_POWERPC_UADDR32:
7978 if (size == 32)
7979 return false;
7980 break;
7981
7982 case elfcpp::R_PPC64_ADDR64:
7983 case elfcpp::R_PPC64_UADDR64:
7984 if (size == 64)
7985 return false;
7986 break;
7987
7988 // GOT refs are good, but also don't need a PLT entry.
7989 case elfcpp::R_POWERPC_GOT16:
7990 case elfcpp::R_POWERPC_GOT16_LO:
7991 case elfcpp::R_POWERPC_GOT16_HI:
7992 case elfcpp::R_POWERPC_GOT16_HA:
7993 case elfcpp::R_PPC64_GOT16_DS:
7994 case elfcpp::R_PPC64_GOT16_LO_DS:
7995 case elfcpp::R_PPC64_GOT_PCREL34:
7996 return false;
7997
7998 // PLT relocs are OK and need a PLT entry.
7999 case elfcpp::R_POWERPC_PLT16_LO:
8000 case elfcpp::R_POWERPC_PLT16_HI:
8001 case elfcpp::R_POWERPC_PLT16_HA:
8002 case elfcpp::R_PPC64_PLT16_LO_DS:
8003 case elfcpp::R_POWERPC_PLTSEQ:
8004 case elfcpp::R_POWERPC_PLTCALL:
8005 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8006 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8007 case elfcpp::R_PPC64_PLT_PCREL34:
8008 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8009 return true;
8010 break;
8011
8012 // Function calls are good, and these do need a PLT entry.
8013 case elfcpp::R_PPC64_REL24_NOTOC:
8014 if (size == 32)
8015 break;
8016 // Fall through.
8017 case elfcpp::R_PPC64_REL24_P9NOTOC:
8018 case elfcpp::R_POWERPC_ADDR24:
8019 case elfcpp::R_POWERPC_ADDR14:
8020 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8021 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8022 case elfcpp::R_POWERPC_REL24:
8023 case elfcpp::R_PPC_PLTREL24:
8024 case elfcpp::R_POWERPC_REL14:
8025 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8026 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8027 return true;
8028
8029 default:
8030 break;
8031 }
8032
8033 // Anything else is a problem.
8034 // If we are building a static executable, the libc startup function
8035 // responsible for applying indirect function relocations is going
8036 // to complain about the reloc type.
8037 // If we are building a dynamic executable, we will have a text
8038 // relocation. The dynamic loader will set the text segment
8039 // writable and non-executable to apply text relocations. So we'll
8040 // segfault when trying to run the indirection function to resolve
8041 // the reloc.
8042 if (report_err)
8043 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
8044 object->name().c_str(), r_type);
8045 return false;
8046 }
8047
8048 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
8049 // reloc.
8050
8051 static bool
8052 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8053 {
8054 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8055 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8056 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8057 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8058 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8059 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8060 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8061 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8062 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8063 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8064 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8065 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8066 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8067 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8068 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8069 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8070 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8071 /* Exclude lfqu by testing reloc. If relocs are ever
8072 defined for the reduced D field in psq_lu then those
8073 will need testing too. */
8074 && r_type != elfcpp::R_PPC64_TOC16_LO
8075 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8076 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8077 && (insn & 1) == 0)
8078 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8079 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8080 /* Exclude stfqu. psq_stu as above for psq_lu. */
8081 && r_type != elfcpp::R_PPC64_TOC16_LO
8082 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8083 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8084 && (insn & 1) == 0));
8085 }
8086
8087 // Scan a relocation for a local symbol.
8088
8089 template<int size, bool big_endian>
8090 inline void
8091 Target_powerpc<size, big_endian>::Scan::local(
8092 Symbol_table* symtab,
8093 Layout* layout,
8094 Target_powerpc<size, big_endian>* target,
8095 Sized_relobj_file<size, big_endian>* object,
8096 unsigned int data_shndx,
8097 Output_section* output_section,
8098 const elfcpp::Rela<size, big_endian>& reloc,
8099 unsigned int r_type,
8100 const elfcpp::Sym<size, big_endian>& lsym,
8101 bool is_discarded)
8102 {
8103 Powerpc_relobj<size, big_endian>* ppc_object
8104 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8105
8106 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
8107
8108 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8109 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8110 {
8111 this->expect_tls_get_addr_call();
8112 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8113 if (tls_type != tls::TLSOPT_NONE)
8114 this->skip_next_tls_get_addr_call();
8115 }
8116 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8117 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8118 {
8119 this->expect_tls_get_addr_call();
8120 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8121 if (tls_type != tls::TLSOPT_NONE)
8122 this->skip_next_tls_get_addr_call();
8123 }
8124
8125 if (is_discarded)
8126 {
8127 if (size == 64
8128 && data_shndx == ppc_object->opd_shndx()
8129 && r_type == elfcpp::R_PPC64_ADDR64)
8130 ppc_object->set_opd_discard(reloc.get_r_offset());
8131 return;
8132 }
8133
8134 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8135 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
8136 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8137 {
8138 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8139 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8140 r_type, r_sym, reloc.get_r_addend());
8141 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
8142 }
8143
8144 switch (r_type)
8145 {
8146 case elfcpp::R_POWERPC_NONE:
8147 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8148 case elfcpp::R_POWERPC_GNU_VTENTRY:
8149 case elfcpp::R_POWERPC_TLS:
8150 case elfcpp::R_PPC64_ENTRY:
8151 case elfcpp::R_POWERPC_PLTSEQ:
8152 case elfcpp::R_POWERPC_PLTCALL:
8153 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8154 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8155 case elfcpp::R_PPC64_PCREL_OPT:
8156 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8157 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8158 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8159 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8160 case elfcpp::R_PPC64_REL16_HIGHER34:
8161 case elfcpp::R_PPC64_REL16_HIGHERA34:
8162 case elfcpp::R_PPC64_REL16_HIGHEST34:
8163 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8164 case elfcpp::R_PPC64_D34:
8165 case elfcpp::R_PPC64_D34_LO:
8166 case elfcpp::R_PPC64_D34_HI30:
8167 case elfcpp::R_PPC64_D34_HA30:
8168 case elfcpp::R_PPC64_D28:
8169 case elfcpp::R_PPC64_PCREL34:
8170 case elfcpp::R_PPC64_PCREL28:
8171 case elfcpp::R_PPC64_TPREL34:
8172 case elfcpp::R_PPC64_DTPREL34:
8173 break;
8174
8175 case elfcpp::R_PPC64_TOC:
8176 {
8177 Output_data_got_powerpc<size, big_endian>* got
8178 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8179 if (parameters->options().output_is_position_independent())
8180 {
8181 Address off = reloc.get_r_offset();
8182 if (size == 64
8183 && target->abiversion() < 2
8184 && data_shndx == ppc_object->opd_shndx()
8185 && ppc_object->get_opd_discard(off - 8))
8186 break;
8187
8188 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8189 Address got_off = got->g_o_t();
8190 rela_dyn->add_output_section_relative(got->output_section(),
8191 elfcpp::R_POWERPC_RELATIVE,
8192 output_section,
8193 object, data_shndx, off,
8194 got_off);
8195 }
8196 }
8197 break;
8198
8199 case elfcpp::R_PPC64_ADDR64:
8200 case elfcpp::R_PPC64_UADDR64:
8201 case elfcpp::R_POWERPC_ADDR32:
8202 case elfcpp::R_POWERPC_UADDR32:
8203 case elfcpp::R_POWERPC_ADDR24:
8204 case elfcpp::R_POWERPC_ADDR16:
8205 case elfcpp::R_POWERPC_ADDR16_LO:
8206 case elfcpp::R_POWERPC_ADDR16_HI:
8207 case elfcpp::R_POWERPC_ADDR16_HA:
8208 case elfcpp::R_POWERPC_UADDR16:
8209 case elfcpp::R_PPC64_ADDR16_HIGH:
8210 case elfcpp::R_PPC64_ADDR16_HIGHA:
8211 case elfcpp::R_PPC64_ADDR16_HIGHER:
8212 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8213 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8214 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8215 case elfcpp::R_PPC64_ADDR16_DS:
8216 case elfcpp::R_PPC64_ADDR16_LO_DS:
8217 case elfcpp::R_POWERPC_ADDR14:
8218 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8219 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8220 // If building a shared library (or a position-independent
8221 // executable), we need to create a dynamic relocation for
8222 // this location.
8223 if (parameters->options().output_is_position_independent()
8224 || (size == 64 && is_ifunc && target->abiversion() < 2))
8225 {
8226 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8227 is_ifunc);
8228 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8229 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8230 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8231 {
8232 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8233 : elfcpp::R_POWERPC_RELATIVE);
8234 rela_dyn->add_local_relative(object, r_sym, dynrel,
8235 output_section, data_shndx,
8236 reloc.get_r_offset(),
8237 reloc.get_r_addend(), false);
8238 }
8239 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8240 {
8241 check_non_pic(object, r_type);
8242 rela_dyn->add_local(object, r_sym, r_type, output_section,
8243 data_shndx, reloc.get_r_offset(),
8244 reloc.get_r_addend());
8245 }
8246 else
8247 {
8248 gold_assert(lsym.get_st_value() == 0);
8249 unsigned int shndx = lsym.get_st_shndx();
8250 bool is_ordinary;
8251 shndx = object->adjust_sym_shndx(r_sym, shndx,
8252 &is_ordinary);
8253 if (!is_ordinary)
8254 object->error(_("section symbol %u has bad shndx %u"),
8255 r_sym, shndx);
8256 else
8257 rela_dyn->add_local_section(object, shndx, r_type,
8258 output_section, data_shndx,
8259 reloc.get_r_offset());
8260 }
8261 }
8262 break;
8263
8264 case elfcpp::R_PPC64_PLT_PCREL34:
8265 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8266 case elfcpp::R_POWERPC_PLT16_LO:
8267 case elfcpp::R_POWERPC_PLT16_HI:
8268 case elfcpp::R_POWERPC_PLT16_HA:
8269 case elfcpp::R_PPC64_PLT16_LO_DS:
8270 if (!is_ifunc)
8271 {
8272 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8273 target->make_local_plt_entry(symtab, layout, object, r_sym);
8274 }
8275 break;
8276
8277 case elfcpp::R_PPC64_REL24_NOTOC:
8278 if (size == 32)
8279 break;
8280 // Fall through.
8281 case elfcpp::R_PPC64_REL24_P9NOTOC:
8282 case elfcpp::R_POWERPC_REL24:
8283 case elfcpp::R_PPC_PLTREL24:
8284 case elfcpp::R_PPC_LOCAL24PC:
8285 case elfcpp::R_POWERPC_REL14:
8286 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8287 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8288 if (!is_ifunc)
8289 {
8290 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8291 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8292 r_type, r_sym, reloc.get_r_addend());
8293 }
8294 break;
8295
8296 case elfcpp::R_PPC64_TOCSAVE:
8297 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8298 // caller has already saved r2 and thus a plt call stub need not
8299 // save r2.
8300 if (size == 64
8301 && target->mark_pltcall(ppc_object, data_shndx,
8302 reloc.get_r_offset() - 4, symtab))
8303 {
8304 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8305 unsigned int shndx = lsym.get_st_shndx();
8306 bool is_ordinary;
8307 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8308 if (!is_ordinary)
8309 object->error(_("tocsave symbol %u has bad shndx %u"),
8310 r_sym, shndx);
8311 else
8312 target->add_tocsave(ppc_object, shndx,
8313 lsym.get_st_value() + reloc.get_r_addend());
8314 }
8315 break;
8316
8317 case elfcpp::R_PPC64_REL64:
8318 case elfcpp::R_POWERPC_REL32:
8319 case elfcpp::R_POWERPC_REL16:
8320 case elfcpp::R_POWERPC_REL16_LO:
8321 case elfcpp::R_POWERPC_REL16_HI:
8322 case elfcpp::R_POWERPC_REL16_HA:
8323 case elfcpp::R_POWERPC_REL16DX_HA:
8324 case elfcpp::R_PPC64_REL16_HIGH:
8325 case elfcpp::R_PPC64_REL16_HIGHA:
8326 case elfcpp::R_PPC64_REL16_HIGHER:
8327 case elfcpp::R_PPC64_REL16_HIGHERA:
8328 case elfcpp::R_PPC64_REL16_HIGHEST:
8329 case elfcpp::R_PPC64_REL16_HIGHESTA:
8330 case elfcpp::R_POWERPC_SECTOFF:
8331 case elfcpp::R_POWERPC_SECTOFF_LO:
8332 case elfcpp::R_POWERPC_SECTOFF_HI:
8333 case elfcpp::R_POWERPC_SECTOFF_HA:
8334 case elfcpp::R_PPC64_SECTOFF_DS:
8335 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8336 case elfcpp::R_POWERPC_TPREL16:
8337 case elfcpp::R_POWERPC_TPREL16_LO:
8338 case elfcpp::R_POWERPC_TPREL16_HI:
8339 case elfcpp::R_POWERPC_TPREL16_HA:
8340 case elfcpp::R_PPC64_TPREL16_DS:
8341 case elfcpp::R_PPC64_TPREL16_LO_DS:
8342 case elfcpp::R_PPC64_TPREL16_HIGH:
8343 case elfcpp::R_PPC64_TPREL16_HIGHA:
8344 case elfcpp::R_PPC64_TPREL16_HIGHER:
8345 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8346 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8347 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8348 case elfcpp::R_POWERPC_DTPREL16:
8349 case elfcpp::R_POWERPC_DTPREL16_LO:
8350 case elfcpp::R_POWERPC_DTPREL16_HI:
8351 case elfcpp::R_POWERPC_DTPREL16_HA:
8352 case elfcpp::R_PPC64_DTPREL16_DS:
8353 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8354 case elfcpp::R_PPC64_DTPREL16_HIGH:
8355 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8356 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8357 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8358 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8359 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8360 case elfcpp::R_PPC64_TLSGD:
8361 case elfcpp::R_PPC64_TLSLD:
8362 case elfcpp::R_PPC64_ADDR64_LOCAL:
8363 break;
8364
8365 case elfcpp::R_PPC64_GOT_PCREL34:
8366 case elfcpp::R_POWERPC_GOT16:
8367 case elfcpp::R_POWERPC_GOT16_LO:
8368 case elfcpp::R_POWERPC_GOT16_HI:
8369 case elfcpp::R_POWERPC_GOT16_HA:
8370 case elfcpp::R_PPC64_GOT16_DS:
8371 case elfcpp::R_PPC64_GOT16_LO_DS:
8372 {
8373 // The symbol requires a GOT entry.
8374 Got_type got_type = ((size == 32
8375 || r_type == elfcpp::R_POWERPC_GOT16
8376 || r_type == elfcpp::R_PPC64_GOT16_DS)
8377 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8378 Output_data_got_powerpc<size, big_endian>* got
8379 = target->got_section(symtab, layout, got_type);
8380 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8381 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8382
8383 if (!parameters->options().output_is_position_independent())
8384 {
8385 if (is_ifunc
8386 && (size == 32 || target->abiversion() >= 2))
8387 got->add_local_plt(object, r_sym, got_type, addend);
8388 else
8389 got->add_local(object, r_sym, got_type, addend);
8390 }
8391 else if (!object->local_has_got_offset(r_sym, got_type, addend))
8392 {
8393 // If we are generating a shared object or a pie, this
8394 // symbol's GOT entry will be set by a dynamic relocation.
8395 unsigned int off;
8396 off = got->add_constant(0);
8397 object->set_local_got_offset(r_sym, got_type, off, addend);
8398
8399 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8400 is_ifunc);
8401 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8402 : elfcpp::R_POWERPC_RELATIVE);
8403 rela_dyn->add_local_relative(object, r_sym, dynrel,
8404 got, off, addend, false);
8405 }
8406 }
8407 break;
8408
8409 case elfcpp::R_PPC64_TOC16:
8410 case elfcpp::R_PPC64_TOC16_LO:
8411 case elfcpp::R_PPC64_TOC16_HI:
8412 case elfcpp::R_PPC64_TOC16_HA:
8413 case elfcpp::R_PPC64_TOC16_DS:
8414 case elfcpp::R_PPC64_TOC16_LO_DS:
8415 // We need a GOT section.
8416 target->got_section(symtab, layout, GOT_TYPE_SMALL);
8417 break;
8418
8419 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8420 case elfcpp::R_POWERPC_GOT_TLSGD16:
8421 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8422 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8423 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8424 {
8425 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8426 if (tls_type == tls::TLSOPT_NONE)
8427 {
8428 Got_type got_type = ((size == 32
8429 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8430 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8431 Output_data_got_powerpc<size, big_endian>* got
8432 = target->got_section(symtab, layout, got_type);
8433 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8434 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8435 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8436 got->add_local_tls_pair(object, r_sym, got_type,
8437 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8438 addend);
8439 }
8440 else if (tls_type == tls::TLSOPT_TO_LE)
8441 {
8442 // no GOT relocs needed for Local Exec.
8443 }
8444 else
8445 gold_unreachable();
8446 }
8447 break;
8448
8449 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8450 case elfcpp::R_POWERPC_GOT_TLSLD16:
8451 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8452 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8453 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8454 {
8455 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8456 if (tls_type == tls::TLSOPT_NONE)
8457 target->tlsld_got_offset(symtab, layout, object);
8458 else if (tls_type == tls::TLSOPT_TO_LE)
8459 {
8460 // no GOT relocs needed for Local Exec.
8461 if (parameters->options().emit_relocs())
8462 {
8463 Output_section* os = layout->tls_segment()->first_section();
8464 gold_assert(os != NULL);
8465 os->set_needs_symtab_index();
8466 }
8467 }
8468 else
8469 gold_unreachable();
8470 }
8471 break;
8472
8473 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8474 case elfcpp::R_POWERPC_GOT_DTPREL16:
8475 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8476 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8477 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8478 {
8479 Got_type got_type = ((size == 32
8480 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8481 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
8482 Output_data_got_powerpc<size, big_endian>* got
8483 = target->got_section(symtab, layout, got_type);
8484 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8485 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8486 got->add_local_tls(object, r_sym, got_type, addend);
8487 }
8488 break;
8489
8490 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8491 case elfcpp::R_POWERPC_GOT_TPREL16:
8492 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8493 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8494 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8495 {
8496 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8497 if (tls_type == tls::TLSOPT_NONE)
8498 {
8499 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8500 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8501 Got_type got_type = ((size == 32
8502 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8503 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8504 if (!object->local_has_got_offset(r_sym, got_type, addend))
8505 {
8506 Output_data_got_powerpc<size, big_endian>* got
8507 = target->got_section(symtab, layout, got_type);
8508 unsigned int off = got->add_constant(0);
8509 object->set_local_got_offset(r_sym, got_type, off, addend);
8510
8511 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8512 rela_dyn->add_symbolless_local_addend(object, r_sym,
8513 elfcpp::R_POWERPC_TPREL,
8514 got, off, addend);
8515 }
8516 }
8517 else if (tls_type == tls::TLSOPT_TO_LE)
8518 {
8519 // no GOT relocs needed for Local Exec.
8520 }
8521 else
8522 gold_unreachable();
8523 }
8524 break;
8525
8526 default:
8527 unsupported_reloc_local(object, r_type);
8528 break;
8529 }
8530
8531 if (size == 64
8532 && parameters->options().toc_optimize())
8533 {
8534 if (data_shndx == ppc_object->toc_shndx())
8535 {
8536 bool ok = true;
8537 if (r_type != elfcpp::R_PPC64_ADDR64
8538 || (is_ifunc && target->abiversion() < 2))
8539 ok = false;
8540 else if (parameters->options().output_is_position_independent())
8541 {
8542 if (is_ifunc)
8543 ok = false;
8544 else
8545 {
8546 unsigned int shndx = lsym.get_st_shndx();
8547 if (shndx >= elfcpp::SHN_LORESERVE
8548 && shndx != elfcpp::SHN_XINDEX)
8549 ok = false;
8550 }
8551 }
8552 if (!ok)
8553 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8554 }
8555
8556 enum {no_check, check_lo, check_ha} insn_check;
8557 switch (r_type)
8558 {
8559 default:
8560 insn_check = no_check;
8561 break;
8562
8563 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8564 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8565 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8566 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8567 case elfcpp::R_POWERPC_GOT16_HA:
8568 case elfcpp::R_PPC64_TOC16_HA:
8569 insn_check = check_ha;
8570 break;
8571
8572 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8573 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8574 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8575 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8576 case elfcpp::R_POWERPC_GOT16_LO:
8577 case elfcpp::R_PPC64_GOT16_LO_DS:
8578 case elfcpp::R_PPC64_TOC16_LO:
8579 case elfcpp::R_PPC64_TOC16_LO_DS:
8580 insn_check = check_lo;
8581 break;
8582 }
8583
8584 section_size_type slen;
8585 const unsigned char* view = NULL;
8586 if (insn_check != no_check)
8587 {
8588 view = ppc_object->section_contents(data_shndx, &slen, false);
8589 section_size_type off =
8590 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8591 if (off < slen)
8592 {
8593 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8594 if (insn_check == check_lo
8595 ? !ok_lo_toc_insn(insn, r_type)
8596 : ((insn & ((0x3f << 26) | 0x1f << 16))
8597 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8598 {
8599 ppc_object->set_no_toc_opt();
8600 gold_warning(_("%s: toc optimization is not supported "
8601 "for %#08x instruction"),
8602 ppc_object->name().c_str(), insn);
8603 }
8604 }
8605 }
8606
8607 switch (r_type)
8608 {
8609 default:
8610 break;
8611 case elfcpp::R_PPC64_TOC16:
8612 case elfcpp::R_PPC64_TOC16_LO:
8613 case elfcpp::R_PPC64_TOC16_HI:
8614 case elfcpp::R_PPC64_TOC16_HA:
8615 case elfcpp::R_PPC64_TOC16_DS:
8616 case elfcpp::R_PPC64_TOC16_LO_DS:
8617 unsigned int shndx = lsym.get_st_shndx();
8618 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8619 bool is_ordinary;
8620 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8621 if (is_ordinary && shndx == ppc_object->toc_shndx())
8622 {
8623 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8624 if (dst_off < ppc_object->section_size(shndx))
8625 {
8626 bool ok = false;
8627 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8628 ok = true;
8629 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8630 {
8631 // Need to check that the insn is a ld
8632 if (!view)
8633 view = ppc_object->section_contents(data_shndx,
8634 &slen,
8635 false);
8636 section_size_type off =
8637 (convert_to_section_size_type(reloc.get_r_offset())
8638 + (big_endian ? -2 : 3));
8639 if (off < slen
8640 && (view[off] & (0x3f << 2)) == 58u << 2)
8641 ok = true;
8642 }
8643 if (!ok)
8644 ppc_object->set_no_toc_opt(dst_off);
8645 }
8646 }
8647 break;
8648 }
8649 }
8650
8651 if (size == 32)
8652 {
8653 switch (r_type)
8654 {
8655 case elfcpp::R_POWERPC_REL32:
8656 if (ppc_object->got2_shndx() != 0
8657 && parameters->options().output_is_position_independent())
8658 {
8659 unsigned int shndx = lsym.get_st_shndx();
8660 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8661 bool is_ordinary;
8662 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8663 if (is_ordinary && shndx == ppc_object->got2_shndx()
8664 && (ppc_object->section_flags(data_shndx)
8665 & elfcpp::SHF_EXECINSTR) != 0)
8666 gold_error(_("%s: unsupported -mbss-plt code"),
8667 ppc_object->name().c_str());
8668 }
8669 break;
8670 default:
8671 break;
8672 }
8673 }
8674
8675 switch (r_type)
8676 {
8677 case elfcpp::R_POWERPC_GOT_TLSLD16:
8678 case elfcpp::R_POWERPC_GOT_TLSGD16:
8679 case elfcpp::R_POWERPC_GOT_TPREL16:
8680 case elfcpp::R_POWERPC_GOT_DTPREL16:
8681 case elfcpp::R_POWERPC_GOT16:
8682 case elfcpp::R_PPC64_GOT16_DS:
8683 case elfcpp::R_PPC64_TOC16:
8684 case elfcpp::R_PPC64_TOC16_DS:
8685 ppc_object->set_has_small_toc_reloc();
8686 break;
8687 default:
8688 break;
8689 }
8690
8691 switch (r_type)
8692 {
8693 case elfcpp::R_PPC64_TPREL16_DS:
8694 case elfcpp::R_PPC64_TPREL16_LO_DS:
8695 case elfcpp::R_PPC64_TPREL16_HIGH:
8696 case elfcpp::R_PPC64_TPREL16_HIGHA:
8697 case elfcpp::R_PPC64_TPREL16_HIGHER:
8698 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8699 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8700 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8701 case elfcpp::R_PPC64_TPREL34:
8702 if (size != 64)
8703 break;
8704 // Fall through.
8705 case elfcpp::R_POWERPC_TPREL16:
8706 case elfcpp::R_POWERPC_TPREL16_LO:
8707 case elfcpp::R_POWERPC_TPREL16_HI:
8708 case elfcpp::R_POWERPC_TPREL16_HA:
8709 layout->set_has_static_tls();
8710 break;
8711 default:
8712 break;
8713 }
8714
8715 switch (r_type)
8716 {
8717 case elfcpp::R_POWERPC_TPREL16_HA:
8718 if (target->tprel_opt())
8719 {
8720 section_size_type slen;
8721 const unsigned char* view = NULL;
8722 view = ppc_object->section_contents(data_shndx, &slen, false);
8723 section_size_type off
8724 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8725 if (off < slen)
8726 {
8727 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8728 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8729 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8730 target->set_no_tprel_opt();
8731 }
8732 }
8733 break;
8734
8735 case elfcpp::R_PPC64_TPREL16_HIGH:
8736 case elfcpp::R_PPC64_TPREL16_HIGHA:
8737 case elfcpp::R_PPC64_TPREL16_HIGHER:
8738 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8739 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8740 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8741 if (size != 64)
8742 break;
8743 // Fall through.
8744 case elfcpp::R_POWERPC_TPREL16_HI:
8745 target->set_no_tprel_opt();
8746 break;
8747 default:
8748 break;
8749 }
8750
8751 switch (r_type)
8752 {
8753 case elfcpp::R_PPC64_D34:
8754 case elfcpp::R_PPC64_D34_LO:
8755 case elfcpp::R_PPC64_D34_HI30:
8756 case elfcpp::R_PPC64_D34_HA30:
8757 case elfcpp::R_PPC64_D28:
8758 case elfcpp::R_PPC64_PCREL34:
8759 case elfcpp::R_PPC64_PCREL28:
8760 case elfcpp::R_PPC64_TPREL34:
8761 case elfcpp::R_PPC64_DTPREL34:
8762 case elfcpp::R_PPC64_PLT_PCREL34:
8763 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8764 case elfcpp::R_PPC64_GOT_PCREL34:
8765 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8766 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8767 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8768 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8769 target->set_power10_relocs();
8770 break;
8771 default:
8772 break;
8773 }
8774 }
8775
8776 // Report an unsupported relocation against a global symbol.
8777
8778 template<int size, bool big_endian>
8779 void
8780 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8781 Sized_relobj_file<size, big_endian>* object,
8782 unsigned int r_type,
8783 Symbol* gsym)
8784 {
8785 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8786 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8787 }
8788
8789 // Scan a relocation for a global symbol.
8790
8791 template<int size, bool big_endian>
8792 inline void
8793 Target_powerpc<size, big_endian>::Scan::global(
8794 Symbol_table* symtab,
8795 Layout* layout,
8796 Target_powerpc<size, big_endian>* target,
8797 Sized_relobj_file<size, big_endian>* object,
8798 unsigned int data_shndx,
8799 Output_section* output_section,
8800 const elfcpp::Rela<size, big_endian>& reloc,
8801 unsigned int r_type,
8802 Symbol* gsym)
8803 {
8804 Powerpc_relobj<size, big_endian>* ppc_object
8805 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8806
8807 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8808 {
8809 case Track_tls::SKIP:
8810 return;
8811 default:
8812 break;
8813 }
8814
8815 if (target->replace_tls_get_addr(gsym))
8816 // Change a __tls_get_addr reference to __tls_get_addr_opt
8817 // so dynamic relocs are emitted against the latter symbol.
8818 gsym = target->tls_get_addr_opt();
8819
8820 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8821 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8822 {
8823 this->expect_tls_get_addr_call();
8824 bool final = gsym->final_value_is_known();
8825 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8826 if (tls_type != tls::TLSOPT_NONE)
8827 this->skip_next_tls_get_addr_call();
8828 }
8829 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8830 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8831 {
8832 this->expect_tls_get_addr_call();
8833 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8834 if (tls_type != tls::TLSOPT_NONE)
8835 this->skip_next_tls_get_addr_call();
8836 }
8837
8838 // A STT_GNU_IFUNC symbol may require a PLT entry.
8839 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8840 bool pushed_ifunc = false;
8841 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8842 {
8843 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8844 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8845 r_type, r_sym, reloc.get_r_addend());
8846 target->make_plt_entry(symtab, layout, gsym);
8847 pushed_ifunc = true;
8848 }
8849
8850 switch (r_type)
8851 {
8852 case elfcpp::R_POWERPC_NONE:
8853 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8854 case elfcpp::R_POWERPC_GNU_VTENTRY:
8855 case elfcpp::R_PPC_LOCAL24PC:
8856 case elfcpp::R_POWERPC_TLS:
8857 case elfcpp::R_PPC64_ENTRY:
8858 case elfcpp::R_POWERPC_PLTSEQ:
8859 case elfcpp::R_POWERPC_PLTCALL:
8860 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8861 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8862 case elfcpp::R_PPC64_PCREL_OPT:
8863 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8864 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8865 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8866 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8867 case elfcpp::R_PPC64_REL16_HIGHER34:
8868 case elfcpp::R_PPC64_REL16_HIGHERA34:
8869 case elfcpp::R_PPC64_REL16_HIGHEST34:
8870 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8871 case elfcpp::R_PPC64_D34:
8872 case elfcpp::R_PPC64_D34_LO:
8873 case elfcpp::R_PPC64_D34_HI30:
8874 case elfcpp::R_PPC64_D34_HA30:
8875 case elfcpp::R_PPC64_D28:
8876 case elfcpp::R_PPC64_PCREL34:
8877 case elfcpp::R_PPC64_PCREL28:
8878 case elfcpp::R_PPC64_TPREL34:
8879 case elfcpp::R_PPC64_DTPREL34:
8880 break;
8881
8882 case elfcpp::R_PPC64_TOC:
8883 {
8884 Output_data_got_powerpc<size, big_endian>* got
8885 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8886 if (parameters->options().output_is_position_independent())
8887 {
8888 Address off = reloc.get_r_offset();
8889 if (size == 64
8890 && data_shndx == ppc_object->opd_shndx()
8891 && ppc_object->get_opd_discard(off - 8))
8892 break;
8893
8894 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8895 Address got_off = got->g_o_t();
8896 rela_dyn->add_output_section_relative(got->output_section(),
8897 elfcpp::R_POWERPC_RELATIVE,
8898 output_section,
8899 object, data_shndx, off,
8900 got_off);
8901 }
8902 }
8903 break;
8904
8905 case elfcpp::R_PPC64_ADDR64:
8906 if (size == 64
8907 && target->abiversion() < 2
8908 && data_shndx == ppc_object->opd_shndx()
8909 && (gsym->is_defined_in_discarded_section()
8910 || gsym->object() != object))
8911 {
8912 ppc_object->set_opd_discard(reloc.get_r_offset());
8913 break;
8914 }
8915 // Fall through.
8916 case elfcpp::R_PPC64_UADDR64:
8917 case elfcpp::R_POWERPC_ADDR32:
8918 case elfcpp::R_POWERPC_UADDR32:
8919 case elfcpp::R_POWERPC_ADDR24:
8920 case elfcpp::R_POWERPC_ADDR16:
8921 case elfcpp::R_POWERPC_ADDR16_LO:
8922 case elfcpp::R_POWERPC_ADDR16_HI:
8923 case elfcpp::R_POWERPC_ADDR16_HA:
8924 case elfcpp::R_POWERPC_UADDR16:
8925 case elfcpp::R_PPC64_ADDR16_HIGH:
8926 case elfcpp::R_PPC64_ADDR16_HIGHA:
8927 case elfcpp::R_PPC64_ADDR16_HIGHER:
8928 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8929 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8930 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8931 case elfcpp::R_PPC64_ADDR16_DS:
8932 case elfcpp::R_PPC64_ADDR16_LO_DS:
8933 case elfcpp::R_POWERPC_ADDR14:
8934 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8935 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8936 {
8937 // Make a PLT entry if necessary.
8938 if (gsym->needs_plt_entry())
8939 {
8940 // Since this is not a PC-relative relocation, we may be
8941 // taking the address of a function. In that case we need to
8942 // set the entry in the dynamic symbol table to the address of
8943 // the PLT call stub.
8944 bool need_ifunc_plt = false;
8945 if ((size == 32 || target->abiversion() >= 2)
8946 && gsym->is_from_dynobj()
8947 && !parameters->options().output_is_position_independent())
8948 {
8949 gsym->set_needs_dynsym_value();
8950 need_ifunc_plt = true;
8951 }
8952 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8953 {
8954 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8955 target->push_branch(ppc_object, data_shndx,
8956 reloc.get_r_offset(), r_type, r_sym,
8957 reloc.get_r_addend());
8958 target->make_plt_entry(symtab, layout, gsym);
8959 }
8960 }
8961 // Make a dynamic relocation if necessary.
8962 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8963 || (size == 64 && is_ifunc && target->abiversion() < 2))
8964 {
8965 if (!parameters->options().output_is_position_independent()
8966 && gsym->may_need_copy_reloc())
8967 {
8968 target->copy_reloc(symtab, layout, object,
8969 data_shndx, output_section, gsym, reloc);
8970 }
8971 else if ((((size == 32
8972 && r_type == elfcpp::R_POWERPC_ADDR32)
8973 || (size == 64
8974 && r_type == elfcpp::R_PPC64_ADDR64
8975 && target->abiversion() >= 2))
8976 && gsym->can_use_relative_reloc(false)
8977 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8978 && parameters->options().shared()))
8979 || (size == 64
8980 && r_type == elfcpp::R_PPC64_ADDR64
8981 && target->abiversion() < 2
8982 && (gsym->can_use_relative_reloc(false)
8983 || data_shndx == ppc_object->opd_shndx())))
8984 {
8985 Reloc_section* rela_dyn
8986 = target->rela_dyn_section(symtab, layout, is_ifunc);
8987 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8988 : elfcpp::R_POWERPC_RELATIVE);
8989 // Use the "add" method that marks the reloc as being
8990 // relative. This is proper here and in other places
8991 // that add IRELATIVE relocs because those relocs go
8992 // into a separate section that isn't sorted, so it
8993 // doesn't matter that they are marked is_relative.
8994 rela_dyn->add_global_relative(
8995 gsym, dynrel, output_section, object, data_shndx,
8996 reloc.get_r_offset(), reloc.get_r_addend(), false);
8997 }
8998 else
8999 {
9000 Reloc_section* rela_dyn
9001 = target->rela_dyn_section(symtab, layout, is_ifunc);
9002 check_non_pic(object, r_type);
9003 rela_dyn->add_global(gsym, r_type, output_section,
9004 object, data_shndx,
9005 reloc.get_r_offset(),
9006 reloc.get_r_addend());
9007
9008 if (size == 64
9009 && parameters->options().toc_optimize()
9010 && data_shndx == ppc_object->toc_shndx())
9011 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9012 }
9013 }
9014 }
9015 break;
9016
9017 case elfcpp::R_PPC64_PLT_PCREL34:
9018 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9019 case elfcpp::R_POWERPC_PLT16_LO:
9020 case elfcpp::R_POWERPC_PLT16_HI:
9021 case elfcpp::R_POWERPC_PLT16_HA:
9022 case elfcpp::R_PPC64_PLT16_LO_DS:
9023 if (!pushed_ifunc)
9024 {
9025 if (branch_needs_plt_entry(gsym))
9026 target->make_plt_entry(symtab, layout, gsym);
9027 else
9028 target->make_local_plt_entry(symtab, layout, gsym);
9029 }
9030 break;
9031
9032 case elfcpp::R_PPC64_REL24_NOTOC:
9033 if (size == 32)
9034 break;
9035 // Fall through.
9036 case elfcpp::R_PPC64_REL24_P9NOTOC:
9037 case elfcpp::R_PPC_PLTREL24:
9038 case elfcpp::R_POWERPC_REL24:
9039 if (!is_ifunc)
9040 {
9041 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9042 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9043 r_type, r_sym, reloc.get_r_addend());
9044 if (branch_needs_plt_entry(gsym))
9045 target->make_plt_entry(symtab, layout, gsym);
9046 }
9047 // Fall through.
9048
9049 case elfcpp::R_PPC64_REL64:
9050 case elfcpp::R_POWERPC_REL32:
9051 // Make a dynamic relocation if necessary.
9052 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
9053 {
9054 if (!parameters->options().output_is_position_independent()
9055 && gsym->may_need_copy_reloc())
9056 {
9057 target->copy_reloc(symtab, layout, object,
9058 data_shndx, output_section, gsym,
9059 reloc);
9060 }
9061 else
9062 {
9063 Reloc_section* rela_dyn
9064 = target->rela_dyn_section(symtab, layout, is_ifunc);
9065 check_non_pic(object, r_type);
9066 rela_dyn->add_global(gsym, r_type, output_section, object,
9067 data_shndx, reloc.get_r_offset(),
9068 reloc.get_r_addend());
9069 }
9070 }
9071 break;
9072
9073 case elfcpp::R_POWERPC_REL14:
9074 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9075 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9076 if (!is_ifunc)
9077 {
9078 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9079 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9080 r_type, r_sym, reloc.get_r_addend());
9081 }
9082 break;
9083
9084 case elfcpp::R_PPC64_TOCSAVE:
9085 // R_PPC64_TOCSAVE follows a call instruction to indicate the
9086 // caller has already saved r2 and thus a plt call stub need not
9087 // save r2.
9088 if (size == 64
9089 && target->mark_pltcall(ppc_object, data_shndx,
9090 reloc.get_r_offset() - 4, symtab))
9091 {
9092 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9093 bool is_ordinary;
9094 unsigned int shndx = gsym->shndx(&is_ordinary);
9095 if (!is_ordinary)
9096 object->error(_("tocsave symbol %u has bad shndx %u"),
9097 r_sym, shndx);
9098 else
9099 {
9100 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9101 target->add_tocsave(ppc_object, shndx,
9102 sym->value() + reloc.get_r_addend());
9103 }
9104 }
9105 break;
9106
9107 case elfcpp::R_POWERPC_REL16:
9108 case elfcpp::R_POWERPC_REL16_LO:
9109 case elfcpp::R_POWERPC_REL16_HI:
9110 case elfcpp::R_POWERPC_REL16_HA:
9111 case elfcpp::R_POWERPC_REL16DX_HA:
9112 case elfcpp::R_PPC64_REL16_HIGH:
9113 case elfcpp::R_PPC64_REL16_HIGHA:
9114 case elfcpp::R_PPC64_REL16_HIGHER:
9115 case elfcpp::R_PPC64_REL16_HIGHERA:
9116 case elfcpp::R_PPC64_REL16_HIGHEST:
9117 case elfcpp::R_PPC64_REL16_HIGHESTA:
9118 case elfcpp::R_POWERPC_SECTOFF:
9119 case elfcpp::R_POWERPC_SECTOFF_LO:
9120 case elfcpp::R_POWERPC_SECTOFF_HI:
9121 case elfcpp::R_POWERPC_SECTOFF_HA:
9122 case elfcpp::R_PPC64_SECTOFF_DS:
9123 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9124 case elfcpp::R_POWERPC_TPREL16:
9125 case elfcpp::R_POWERPC_TPREL16_LO:
9126 case elfcpp::R_POWERPC_TPREL16_HI:
9127 case elfcpp::R_POWERPC_TPREL16_HA:
9128 case elfcpp::R_PPC64_TPREL16_DS:
9129 case elfcpp::R_PPC64_TPREL16_LO_DS:
9130 case elfcpp::R_PPC64_TPREL16_HIGH:
9131 case elfcpp::R_PPC64_TPREL16_HIGHA:
9132 case elfcpp::R_PPC64_TPREL16_HIGHER:
9133 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9134 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9135 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9136 case elfcpp::R_POWERPC_DTPREL16:
9137 case elfcpp::R_POWERPC_DTPREL16_LO:
9138 case elfcpp::R_POWERPC_DTPREL16_HI:
9139 case elfcpp::R_POWERPC_DTPREL16_HA:
9140 case elfcpp::R_PPC64_DTPREL16_DS:
9141 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9142 case elfcpp::R_PPC64_DTPREL16_HIGH:
9143 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9144 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9145 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9146 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9147 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9148 case elfcpp::R_PPC64_TLSGD:
9149 case elfcpp::R_PPC64_TLSLD:
9150 case elfcpp::R_PPC64_ADDR64_LOCAL:
9151 break;
9152
9153 case elfcpp::R_PPC64_GOT_PCREL34:
9154 case elfcpp::R_POWERPC_GOT16:
9155 case elfcpp::R_POWERPC_GOT16_LO:
9156 case elfcpp::R_POWERPC_GOT16_HI:
9157 case elfcpp::R_POWERPC_GOT16_HA:
9158 case elfcpp::R_PPC64_GOT16_DS:
9159 case elfcpp::R_PPC64_GOT16_LO_DS:
9160 {
9161 // The symbol requires a GOT entry.
9162 Output_data_got_powerpc<size, big_endian>* got;
9163 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9164 Got_type got_type = ((size == 32
9165 || r_type == elfcpp::R_POWERPC_GOT16
9166 || r_type == elfcpp::R_PPC64_GOT16_DS)
9167 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
9168
9169 got = target->got_section(symtab, layout, got_type);
9170 if (gsym->final_value_is_known())
9171 {
9172 if (is_ifunc
9173 && (size == 32 || target->abiversion() >= 2))
9174 got->add_global_plt(gsym, got_type, addend);
9175 else
9176 got->add_global(gsym, got_type, addend);
9177 }
9178 else if (!gsym->has_got_offset(got_type, addend))
9179 {
9180 // If we are generating a shared object or a pie, this
9181 // symbol's GOT entry will be set by a dynamic relocation.
9182 unsigned int off = got->add_constant(0);
9183 gsym->set_got_offset(got_type, off, addend);
9184
9185 Reloc_section* rela_dyn
9186 = target->rela_dyn_section(symtab, layout, is_ifunc);
9187
9188 if (gsym->can_use_relative_reloc(false)
9189 && !((size == 32
9190 || target->abiversion() >= 2)
9191 && gsym->visibility() == elfcpp::STV_PROTECTED
9192 && parameters->options().shared()))
9193 {
9194 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9195 : elfcpp::R_POWERPC_RELATIVE);
9196 rela_dyn->add_global_relative(gsym, dynrel, got, off,
9197 addend, false);
9198 }
9199 else
9200 {
9201 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
9202 rela_dyn->add_global(gsym, dynrel, got, off, addend);
9203 }
9204 }
9205 }
9206 break;
9207
9208 case elfcpp::R_PPC64_TOC16:
9209 case elfcpp::R_PPC64_TOC16_LO:
9210 case elfcpp::R_PPC64_TOC16_HI:
9211 case elfcpp::R_PPC64_TOC16_HA:
9212 case elfcpp::R_PPC64_TOC16_DS:
9213 case elfcpp::R_PPC64_TOC16_LO_DS:
9214 // We need a GOT section.
9215 target->got_section(symtab, layout, GOT_TYPE_SMALL);
9216 break;
9217
9218 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9219 case elfcpp::R_POWERPC_GOT_TLSGD16:
9220 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9221 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9222 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9223 {
9224 bool final = gsym->final_value_is_known();
9225 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9226 if (tls_type == tls::TLSOPT_NONE)
9227 {
9228 Got_type got_type = ((size == 32
9229 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9230 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
9231 Output_data_got_powerpc<size, big_endian>* got
9232 = target->got_section(symtab, layout, got_type);
9233 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9234 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9235 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
9236 elfcpp::R_POWERPC_DTPMOD,
9237 elfcpp::R_POWERPC_DTPREL,
9238 addend);
9239 }
9240 else if (tls_type == tls::TLSOPT_TO_IE)
9241 {
9242 Got_type got_type = ((size == 32
9243 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9244 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9245 if (!gsym->has_got_offset(got_type))
9246 {
9247 Output_data_got_powerpc<size, big_endian>* got
9248 = target->got_section(symtab, layout, got_type);
9249 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9250 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9251 if (gsym->is_undefined()
9252 || gsym->is_from_dynobj())
9253 {
9254 got->add_global_with_rel(gsym, got_type, rela_dyn,
9255 elfcpp::R_POWERPC_TPREL, addend);
9256 }
9257 else
9258 {
9259 unsigned int off = got->add_constant(0);
9260 gsym->set_got_offset(got_type, off);
9261 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9262 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9263 got, off, addend);
9264 }
9265 }
9266 }
9267 else if (tls_type == tls::TLSOPT_TO_LE)
9268 {
9269 // no GOT relocs needed for Local Exec.
9270 }
9271 else
9272 gold_unreachable();
9273 }
9274 break;
9275
9276 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9277 case elfcpp::R_POWERPC_GOT_TLSLD16:
9278 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9279 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9280 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9281 {
9282 tls::Tls_optimization tls_type = target->optimize_tls_ld();
9283 if (tls_type == tls::TLSOPT_NONE)
9284 target->tlsld_got_offset(symtab, layout, object);
9285 else if (tls_type == tls::TLSOPT_TO_LE)
9286 {
9287 // no GOT relocs needed for Local Exec.
9288 if (parameters->options().emit_relocs())
9289 {
9290 Output_section* os = layout->tls_segment()->first_section();
9291 gold_assert(os != NULL);
9292 os->set_needs_symtab_index();
9293 }
9294 }
9295 else
9296 gold_unreachable();
9297 }
9298 break;
9299
9300 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9301 case elfcpp::R_POWERPC_GOT_DTPREL16:
9302 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9303 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9304 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9305 {
9306 Got_type got_type = ((size == 32
9307 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9308 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
9309 Output_data_got_powerpc<size, big_endian>* got
9310 = target->got_section(symtab, layout, got_type);
9311 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9312 if (!gsym->final_value_is_known()
9313 && (gsym->is_from_dynobj()
9314 || gsym->is_undefined()
9315 || gsym->is_preemptible()))
9316 got->add_global_with_rel(gsym, got_type,
9317 target->rela_dyn_section(layout),
9318 elfcpp::R_POWERPC_DTPREL, addend);
9319 else
9320 got->add_global_tls(gsym, got_type, addend);
9321 }
9322 break;
9323
9324 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9325 case elfcpp::R_POWERPC_GOT_TPREL16:
9326 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9327 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9328 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9329 {
9330 bool final = gsym->final_value_is_known();
9331 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9332 if (tls_type == tls::TLSOPT_NONE)
9333 {
9334 Got_type got_type = ((size == 32
9335 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9336 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9337 if (!gsym->has_got_offset(got_type))
9338 {
9339 Output_data_got_powerpc<size, big_endian>* got
9340 = target->got_section(symtab, layout, got_type);
9341 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9342 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9343 if (gsym->is_undefined()
9344 || gsym->is_from_dynobj())
9345 {
9346 got->add_global_with_rel(gsym, got_type, rela_dyn,
9347 elfcpp::R_POWERPC_TPREL, addend);
9348 }
9349 else
9350 {
9351 unsigned int off = got->add_constant(0);
9352 gsym->set_got_offset(got_type, off);
9353 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9354 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9355 got, off, addend);
9356 }
9357 }
9358 }
9359 else if (tls_type == tls::TLSOPT_TO_LE)
9360 {
9361 // no GOT relocs needed for Local Exec.
9362 }
9363 else
9364 gold_unreachable();
9365 }
9366 break;
9367
9368 default:
9369 unsupported_reloc_global(object, r_type, gsym);
9370 break;
9371 }
9372
9373 if (size == 64
9374 && parameters->options().toc_optimize())
9375 {
9376 if (data_shndx == ppc_object->toc_shndx())
9377 {
9378 bool ok = true;
9379 if (r_type != elfcpp::R_PPC64_ADDR64
9380 || (is_ifunc && target->abiversion() < 2))
9381 ok = false;
9382 else if (parameters->options().output_is_position_independent()
9383 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9384 ok = false;
9385 if (!ok)
9386 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9387 }
9388
9389 enum {no_check, check_lo, check_ha} insn_check;
9390 switch (r_type)
9391 {
9392 default:
9393 insn_check = no_check;
9394 break;
9395
9396 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9397 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9398 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9399 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9400 case elfcpp::R_POWERPC_GOT16_HA:
9401 case elfcpp::R_PPC64_TOC16_HA:
9402 insn_check = check_ha;
9403 break;
9404
9405 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9406 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9407 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9408 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9409 case elfcpp::R_POWERPC_GOT16_LO:
9410 case elfcpp::R_PPC64_GOT16_LO_DS:
9411 case elfcpp::R_PPC64_TOC16_LO:
9412 case elfcpp::R_PPC64_TOC16_LO_DS:
9413 insn_check = check_lo;
9414 break;
9415 }
9416
9417 section_size_type slen;
9418 const unsigned char* view = NULL;
9419 if (insn_check != no_check)
9420 {
9421 view = ppc_object->section_contents(data_shndx, &slen, false);
9422 section_size_type off =
9423 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9424 if (off < slen)
9425 {
9426 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9427 if (insn_check == check_lo
9428 ? !ok_lo_toc_insn(insn, r_type)
9429 : ((insn & ((0x3f << 26) | 0x1f << 16))
9430 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9431 {
9432 ppc_object->set_no_toc_opt();
9433 gold_warning(_("%s: toc optimization is not supported "
9434 "for %#08x instruction"),
9435 ppc_object->name().c_str(), insn);
9436 }
9437 }
9438 }
9439
9440 switch (r_type)
9441 {
9442 default:
9443 break;
9444 case elfcpp::R_PPC64_TOC16:
9445 case elfcpp::R_PPC64_TOC16_LO:
9446 case elfcpp::R_PPC64_TOC16_HI:
9447 case elfcpp::R_PPC64_TOC16_HA:
9448 case elfcpp::R_PPC64_TOC16_DS:
9449 case elfcpp::R_PPC64_TOC16_LO_DS:
9450 if (gsym->source() == Symbol::FROM_OBJECT
9451 && !gsym->object()->is_dynamic())
9452 {
9453 Powerpc_relobj<size, big_endian>* sym_object
9454 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9455 bool is_ordinary;
9456 unsigned int shndx = gsym->shndx(&is_ordinary);
9457 if (shndx == sym_object->toc_shndx())
9458 {
9459 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9460 Address dst_off = sym->value() + reloc.get_r_addend();
9461 if (dst_off < sym_object->section_size(shndx))
9462 {
9463 bool ok = false;
9464 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9465 ok = true;
9466 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9467 {
9468 // Need to check that the insn is a ld
9469 if (!view)
9470 view = ppc_object->section_contents(data_shndx,
9471 &slen,
9472 false);
9473 section_size_type off =
9474 (convert_to_section_size_type(reloc.get_r_offset())
9475 + (big_endian ? -2 : 3));
9476 if (off < slen
9477 && (view[off] & (0x3f << 2)) == (58u << 2))
9478 ok = true;
9479 }
9480 if (!ok)
9481 sym_object->set_no_toc_opt(dst_off);
9482 }
9483 }
9484 }
9485 break;
9486 }
9487 }
9488
9489 if (size == 32)
9490 {
9491 switch (r_type)
9492 {
9493 case elfcpp::R_PPC_LOCAL24PC:
9494 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9495 gold_error(_("%s: unsupported -mbss-plt code"),
9496 ppc_object->name().c_str());
9497 break;
9498 default:
9499 break;
9500 }
9501 }
9502
9503 switch (r_type)
9504 {
9505 case elfcpp::R_POWERPC_GOT_TLSLD16:
9506 case elfcpp::R_POWERPC_GOT_TLSGD16:
9507 case elfcpp::R_POWERPC_GOT_TPREL16:
9508 case elfcpp::R_POWERPC_GOT_DTPREL16:
9509 case elfcpp::R_POWERPC_GOT16:
9510 case elfcpp::R_PPC64_GOT16_DS:
9511 case elfcpp::R_PPC64_TOC16:
9512 case elfcpp::R_PPC64_TOC16_DS:
9513 ppc_object->set_has_small_toc_reloc();
9514 break;
9515 default:
9516 break;
9517 }
9518
9519 switch (r_type)
9520 {
9521 case elfcpp::R_PPC64_TPREL16_DS:
9522 case elfcpp::R_PPC64_TPREL16_LO_DS:
9523 case elfcpp::R_PPC64_TPREL16_HIGH:
9524 case elfcpp::R_PPC64_TPREL16_HIGHA:
9525 case elfcpp::R_PPC64_TPREL16_HIGHER:
9526 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9527 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9528 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9529 case elfcpp::R_PPC64_TPREL34:
9530 if (size != 64)
9531 break;
9532 // Fall through.
9533 case elfcpp::R_POWERPC_TPREL16:
9534 case elfcpp::R_POWERPC_TPREL16_LO:
9535 case elfcpp::R_POWERPC_TPREL16_HI:
9536 case elfcpp::R_POWERPC_TPREL16_HA:
9537 layout->set_has_static_tls();
9538 break;
9539 default:
9540 break;
9541 }
9542
9543 switch (r_type)
9544 {
9545 case elfcpp::R_POWERPC_TPREL16_HA:
9546 if (target->tprel_opt())
9547 {
9548 section_size_type slen;
9549 const unsigned char* view = NULL;
9550 view = ppc_object->section_contents(data_shndx, &slen, false);
9551 section_size_type off
9552 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9553 if (off < slen)
9554 {
9555 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9556 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9557 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9558 target->set_no_tprel_opt();
9559 }
9560 }
9561 break;
9562
9563 case elfcpp::R_PPC64_TPREL16_HIGH:
9564 case elfcpp::R_PPC64_TPREL16_HIGHA:
9565 case elfcpp::R_PPC64_TPREL16_HIGHER:
9566 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9567 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9568 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9569 if (size != 64)
9570 break;
9571 // Fall through.
9572 case elfcpp::R_POWERPC_TPREL16_HI:
9573 target->set_no_tprel_opt();
9574 break;
9575 default:
9576 break;
9577 }
9578
9579 switch (r_type)
9580 {
9581 case elfcpp::R_PPC64_D34:
9582 case elfcpp::R_PPC64_D34_LO:
9583 case elfcpp::R_PPC64_D34_HI30:
9584 case elfcpp::R_PPC64_D34_HA30:
9585 case elfcpp::R_PPC64_D28:
9586 case elfcpp::R_PPC64_PCREL34:
9587 case elfcpp::R_PPC64_PCREL28:
9588 case elfcpp::R_PPC64_TPREL34:
9589 case elfcpp::R_PPC64_DTPREL34:
9590 case elfcpp::R_PPC64_PLT_PCREL34:
9591 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9592 case elfcpp::R_PPC64_GOT_PCREL34:
9593 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9594 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9595 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9596 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9597 target->set_power10_relocs();
9598 break;
9599 default:
9600 break;
9601 }
9602 }
9603
9604 // Process relocations for gc.
9605
9606 template<int size, bool big_endian>
9607 void
9608 Target_powerpc<size, big_endian>::gc_process_relocs(
9609 Symbol_table* symtab,
9610 Layout* layout,
9611 Sized_relobj_file<size, big_endian>* object,
9612 unsigned int data_shndx,
9613 unsigned int,
9614 const unsigned char* prelocs,
9615 size_t reloc_count,
9616 Output_section* output_section,
9617 bool needs_special_offset_handling,
9618 size_t local_symbol_count,
9619 const unsigned char* plocal_symbols)
9620 {
9621 typedef Target_powerpc<size, big_endian> Powerpc;
9622 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9623 Classify_reloc;
9624
9625 Powerpc_relobj<size, big_endian>* ppc_object
9626 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9627 if (size == 64)
9628 ppc_object->set_opd_valid();
9629 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9630 {
9631 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9632 for (p = ppc_object->access_from_map()->begin();
9633 p != ppc_object->access_from_map()->end();
9634 ++p)
9635 {
9636 Address dst_off = p->first;
9637 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9638 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9639 for (s = p->second.begin(); s != p->second.end(); ++s)
9640 {
9641 Relobj* src_obj = s->first;
9642 unsigned int src_indx = s->second;
9643 symtab->gc()->add_reference(src_obj, src_indx,
9644 ppc_object, dst_indx);
9645 }
9646 p->second.clear();
9647 }
9648 ppc_object->access_from_map()->clear();
9649 ppc_object->process_gc_mark(symtab);
9650 // Don't look at .opd relocs as .opd will reference everything.
9651 return;
9652 }
9653
9654 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9655 symtab,
9656 layout,
9657 this,
9658 object,
9659 data_shndx,
9660 prelocs,
9661 reloc_count,
9662 output_section,
9663 needs_special_offset_handling,
9664 local_symbol_count,
9665 plocal_symbols);
9666 }
9667
9668 // Handle target specific gc actions when adding a gc reference from
9669 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9670 // and DST_OFF. For powerpc64, this adds a referenc to the code
9671 // section of a function descriptor.
9672
9673 template<int size, bool big_endian>
9674 void
9675 Target_powerpc<size, big_endian>::do_gc_add_reference(
9676 Symbol_table* symtab,
9677 Relobj* src_obj,
9678 unsigned int src_shndx,
9679 Relobj* dst_obj,
9680 unsigned int dst_shndx,
9681 Address dst_off) const
9682 {
9683 if (size != 64 || dst_obj->is_dynamic())
9684 return;
9685
9686 Powerpc_relobj<size, big_endian>* ppc_object
9687 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9688 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9689 {
9690 if (ppc_object->opd_valid())
9691 {
9692 dst_shndx = ppc_object->get_opd_ent(dst_off);
9693 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9694 }
9695 else
9696 {
9697 // If we haven't run scan_opd_relocs, we must delay
9698 // processing this function descriptor reference.
9699 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9700 }
9701 }
9702 }
9703
9704 // Add any special sections for this symbol to the gc work list.
9705 // For powerpc64, this adds the code section of a function
9706 // descriptor.
9707
9708 template<int size, bool big_endian>
9709 void
9710 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9711 Symbol_table* symtab,
9712 Symbol* sym) const
9713 {
9714 if (size == 64 && sym->object()->pluginobj() == NULL)
9715 {
9716 Powerpc_relobj<size, big_endian>* ppc_object
9717 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9718 bool is_ordinary;
9719 unsigned int shndx = sym->shndx(&is_ordinary);
9720 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9721 {
9722 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9723 Address dst_off = gsym->value();
9724 if (ppc_object->opd_valid())
9725 {
9726 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9727 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9728 dst_indx));
9729 }
9730 else
9731 ppc_object->add_gc_mark(dst_off);
9732 }
9733 }
9734 }
9735
9736 // For a symbol location in .opd, set LOC to the location of the
9737 // function entry.
9738
9739 template<int size, bool big_endian>
9740 void
9741 Target_powerpc<size, big_endian>::do_function_location(
9742 Symbol_location* loc) const
9743 {
9744 if (size == 64 && loc->shndx != 0)
9745 {
9746 if (loc->object->is_dynamic())
9747 {
9748 Powerpc_dynobj<size, big_endian>* ppc_object
9749 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9750 if (loc->shndx == ppc_object->opd_shndx())
9751 {
9752 Address dest_off;
9753 Address off = loc->offset - ppc_object->opd_address();
9754 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9755 loc->offset = dest_off;
9756 }
9757 }
9758 else
9759 {
9760 const Powerpc_relobj<size, big_endian>* ppc_object
9761 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9762 if (loc->shndx == ppc_object->opd_shndx())
9763 {
9764 Address dest_off;
9765 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9766 loc->offset = dest_off;
9767 }
9768 }
9769 }
9770 }
9771
9772 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9773 // compiled with -fsplit-stack. The function calls non-split-stack
9774 // code. Change the function to ensure it has enough stack space to
9775 // call some random function.
9776
9777 template<int size, bool big_endian>
9778 void
9779 Target_powerpc<size, big_endian>::do_calls_non_split(
9780 Relobj* object,
9781 unsigned int shndx,
9782 section_offset_type fnoffset,
9783 section_size_type fnsize,
9784 const unsigned char* prelocs,
9785 size_t reloc_count,
9786 unsigned char* view,
9787 section_size_type view_size,
9788 std::string* from,
9789 std::string* to) const
9790 {
9791 // 32-bit not supported.
9792 if (size == 32)
9793 {
9794 // warn
9795 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9796 prelocs, reloc_count, view, view_size,
9797 from, to);
9798 return;
9799 }
9800
9801 // The function always starts with
9802 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9803 // addis %r12,%r1,-allocate@ha
9804 // addi %r12,%r12,-allocate@l
9805 // cmpld %r12,%r0
9806 // but note that the addis or addi may be replaced with a nop
9807
9808 unsigned char *entry = view + fnoffset;
9809 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9810
9811 if ((insn & 0xffff0000) == addis_2_12)
9812 {
9813 /* Skip ELFv2 global entry code. */
9814 entry += 8;
9815 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9816 }
9817
9818 unsigned char *pinsn = entry;
9819 bool ok = false;
9820 const uint32_t ld_private_ss = 0xe80d8fc0;
9821 if (insn == ld_private_ss)
9822 {
9823 int32_t allocate = 0;
9824 while (1)
9825 {
9826 pinsn += 4;
9827 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9828 if ((insn & 0xffff0000) == addis_12_1)
9829 allocate += (insn & 0xffff) << 16;
9830 else if ((insn & 0xffff0000) == addi_12_1
9831 || (insn & 0xffff0000) == addi_12_12)
9832 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9833 else if (insn != nop)
9834 break;
9835 }
9836 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9837 {
9838 int extra = parameters->options().split_stack_adjust_size();
9839 allocate -= extra;
9840 if (allocate >= 0 || extra < 0)
9841 {
9842 object->error(_("split-stack stack size overflow at "
9843 "section %u offset %0zx"),
9844 shndx, static_cast<size_t>(fnoffset));
9845 return;
9846 }
9847 pinsn = entry + 4;
9848 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9849 if (insn != addis_12_1)
9850 {
9851 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9852 pinsn += 4;
9853 insn = addi_12_12 | (allocate & 0xffff);
9854 if (insn != addi_12_12)
9855 {
9856 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9857 pinsn += 4;
9858 }
9859 }
9860 else
9861 {
9862 insn = addi_12_1 | (allocate & 0xffff);
9863 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9864 pinsn += 4;
9865 }
9866 if (pinsn != entry + 12)
9867 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9868
9869 ok = true;
9870 }
9871 }
9872
9873 if (!ok)
9874 {
9875 if (!object->has_no_split_stack())
9876 object->error(_("failed to match split-stack sequence at "
9877 "section %u offset %0zx"),
9878 shndx, static_cast<size_t>(fnoffset));
9879 }
9880 }
9881
9882 // Scan relocations for a section.
9883
9884 template<int size, bool big_endian>
9885 void
9886 Target_powerpc<size, big_endian>::scan_relocs(
9887 Symbol_table* symtab,
9888 Layout* layout,
9889 Sized_relobj_file<size, big_endian>* object,
9890 unsigned int data_shndx,
9891 unsigned int sh_type,
9892 const unsigned char* prelocs,
9893 size_t reloc_count,
9894 Output_section* output_section,
9895 bool needs_special_offset_handling,
9896 size_t local_symbol_count,
9897 const unsigned char* plocal_symbols)
9898 {
9899 typedef Target_powerpc<size, big_endian> Powerpc;
9900 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9901 Classify_reloc;
9902
9903 if (!this->plt_localentry0_init_)
9904 {
9905 bool plt_localentry0 = false;
9906 if (size == 64
9907 && this->abiversion() >= 2)
9908 {
9909 if (parameters->options().user_set_plt_localentry())
9910 plt_localentry0 = parameters->options().plt_localentry();
9911 if (plt_localentry0
9912 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9913 gold_warning(_("--plt-localentry is especially dangerous without "
9914 "ld.so support to detect ABI violations"));
9915 }
9916 this->plt_localentry0_ = plt_localentry0;
9917 this->plt_localentry0_init_ = true;
9918 }
9919
9920 if (sh_type == elfcpp::SHT_REL)
9921 {
9922 gold_error(_("%s: unsupported REL reloc section"),
9923 object->name().c_str());
9924 return;
9925 }
9926
9927 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9928 symtab,
9929 layout,
9930 this,
9931 object,
9932 data_shndx,
9933 prelocs,
9934 reloc_count,
9935 output_section,
9936 needs_special_offset_handling,
9937 local_symbol_count,
9938 plocal_symbols);
9939
9940 if (this->plt_localentry0_ && this->power10_relocs_)
9941 {
9942 gold_warning(_("--plt-localentry is incompatible with "
9943 "power10 pc-relative code"));
9944 this->plt_localentry0_ = false;
9945 }
9946 }
9947
9948 // Functor class for processing the global symbol table.
9949 // Removes symbols defined on discarded opd entries.
9950
9951 template<bool big_endian>
9952 class Global_symbol_visitor_opd
9953 {
9954 public:
9955 Global_symbol_visitor_opd()
9956 { }
9957
9958 void
9959 operator()(Sized_symbol<64>* sym)
9960 {
9961 if (sym->has_symtab_index()
9962 || sym->source() != Symbol::FROM_OBJECT
9963 || !sym->in_real_elf())
9964 return;
9965
9966 if (sym->object()->is_dynamic())
9967 return;
9968
9969 Powerpc_relobj<64, big_endian>* symobj
9970 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9971 if (symobj->opd_shndx() == 0)
9972 return;
9973
9974 bool is_ordinary;
9975 unsigned int shndx = sym->shndx(&is_ordinary);
9976 if (shndx == symobj->opd_shndx()
9977 && symobj->get_opd_discard(sym->value()))
9978 {
9979 sym->set_undefined();
9980 sym->set_visibility(elfcpp::STV_DEFAULT);
9981 sym->set_is_defined_in_discarded_section();
9982 sym->set_symtab_index(-1U);
9983 }
9984 }
9985 };
9986
9987 template<int size, bool big_endian>
9988 void
9989 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9990 Layout* layout,
9991 Symbol_table* symtab)
9992 {
9993 if (size == 64)
9994 {
9995 Output_data_save_res<size, big_endian>* savres
9996 = new Output_data_save_res<size, big_endian>(symtab);
9997 this->savres_section_ = savres;
9998 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9999 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
10000 savres, ORDER_TEXT, false);
10001 }
10002 }
10003
10004 // Sort linker created .got section first (for the header), then input
10005 // sections belonging to files using small model code.
10006
10007 template<bool big_endian>
10008 class Sort_toc_sections
10009 {
10010 const Output_section_data*
10011 small_got_section() const
10012 {
10013 return (static_cast<Target_powerpc<64, big_endian>*>(
10014 parameters->sized_target<64, big_endian>())
10015 ->got_section(GOT_TYPE_SMALL));
10016 }
10017
10018 int
10019 rank(const Output_section::Input_section& isec) const
10020 {
10021 if (!isec.is_input_section())
10022 {
10023 if (isec.output_section_data() == this->small_got_section())
10024 return 0;
10025 return 2;
10026 }
10027 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
10028 ->has_small_toc_reloc())
10029 return 1;
10030 return 3;
10031 }
10032
10033 public:
10034 bool
10035 operator()(const Output_section::Input_section& is1,
10036 const Output_section::Input_section& is2) const
10037 {
10038 return rank(is1) < rank(is2);
10039 }
10040 };
10041
10042 // Finalize the sections.
10043
10044 template<int size, bool big_endian>
10045 void
10046 Target_powerpc<size, big_endian>::do_finalize_sections(
10047 Layout* layout,
10048 const Input_objects* input_objects,
10049 Symbol_table* symtab)
10050 {
10051 if (parameters->doing_static_link())
10052 {
10053 // At least some versions of glibc elf-init.o have a strong
10054 // reference to __rela_iplt marker syms. A weak ref would be
10055 // better..
10056 if (this->iplt_ != NULL)
10057 {
10058 Reloc_section* rel = this->iplt_->rel_plt();
10059 symtab->define_in_output_data("__rela_iplt_start", NULL,
10060 Symbol_table::PREDEFINED, rel, 0, 0,
10061 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10062 elfcpp::STV_HIDDEN, 0, false, true);
10063 symtab->define_in_output_data("__rela_iplt_end", NULL,
10064 Symbol_table::PREDEFINED, rel, 0, 0,
10065 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10066 elfcpp::STV_HIDDEN, 0, true, true);
10067 }
10068 else
10069 {
10070 symtab->define_as_constant("__rela_iplt_start", NULL,
10071 Symbol_table::PREDEFINED, 0, 0,
10072 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10073 elfcpp::STV_HIDDEN, 0, true, false);
10074 symtab->define_as_constant("__rela_iplt_end", NULL,
10075 Symbol_table::PREDEFINED, 0, 0,
10076 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10077 elfcpp::STV_HIDDEN, 0, true, false);
10078 }
10079 }
10080
10081 if (size == 64)
10082 {
10083 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10084 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
10085
10086 if (!parameters->options().relocatable())
10087 {
10088 this->define_save_restore_funcs(layout, symtab);
10089
10090 // Annoyingly, we need to make these sections now whether or
10091 // not we need them. If we delay until do_relax then we
10092 // need to mess with the relaxation machinery checkpointing.
10093 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
10094 this->make_brlt_section(layout);
10095
10096 // FIXME, maybe. Here we could run through all the got
10097 // entries in the small got section, removing any duplicates
10098 // found in the big got section and renumbering offsets.
10099
10100 if (parameters->options().toc_sort())
10101 {
10102 Output_section* os = this->got_->output_section();
10103 if (os != NULL && os->input_sections().size() > 1)
10104 std::stable_sort(os->input_sections().begin(),
10105 os->input_sections().end(),
10106 Sort_toc_sections<big_endian>());
10107 }
10108 }
10109 }
10110
10111 // Fill in some more dynamic tags.
10112 Output_data_dynamic* odyn = layout->dynamic_data();
10113 if (odyn != NULL)
10114 {
10115 const Reloc_section* rel_plt = (this->plt_ == NULL
10116 ? NULL
10117 : this->plt_->rel_plt());
10118 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
10119 this->rela_dyn_, true, size == 32, true);
10120
10121 if (size == 32)
10122 {
10123 if (this->got_ != NULL)
10124 {
10125 this->got_->finalize_data_size();
10126 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10127 this->got_, this->got_->g_o_t());
10128 }
10129 if (this->has_tls_get_addr_opt_)
10130 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
10131 }
10132 else
10133 {
10134 if (this->glink_ != NULL)
10135 {
10136 this->glink_->finalize_data_size();
10137 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10138 this->glink_,
10139 (this->glink_->pltresolve_size()
10140 - 32));
10141 }
10142 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
10143 odyn->add_constant(elfcpp::DT_PPC64_OPT,
10144 ((this->has_localentry0_
10145 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10146 | (this->has_tls_get_addr_opt_
10147 ? elfcpp::PPC64_OPT_TLS : 0)));
10148 }
10149 }
10150
10151 // Emit any relocs we saved in an attempt to avoid generating COPY
10152 // relocs.
10153 if (this->copy_relocs_.any_saved_relocs())
10154 this->copy_relocs_.emit(this->rela_dyn_section(layout));
10155
10156 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10157 p != input_objects->relobj_end();
10158 ++p)
10159 {
10160 Powerpc_relobj<size, big_endian>* ppc_relobj
10161 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10162 if (ppc_relobj->attributes_section_data())
10163 this->merge_object_attributes(ppc_relobj,
10164 ppc_relobj->attributes_section_data());
10165 }
10166 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10167 p != input_objects->dynobj_end();
10168 ++p)
10169 {
10170 Powerpc_dynobj<size, big_endian>* ppc_dynobj
10171 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10172 if (ppc_dynobj->attributes_section_data())
10173 this->merge_object_attributes(ppc_dynobj,
10174 ppc_dynobj->attributes_section_data());
10175 }
10176
10177 // Create a .gnu.attributes section if we have merged any attributes
10178 // from inputs.
10179 if (this->attributes_section_data_ != NULL
10180 && this->attributes_section_data_->size() != 0)
10181 {
10182 Output_attributes_section_data* attributes_section
10183 = new Output_attributes_section_data(*this->attributes_section_data_);
10184 layout->add_output_section_data(".gnu.attributes",
10185 elfcpp::SHT_GNU_ATTRIBUTES, 0,
10186 attributes_section, ORDER_INVALID, false);
10187 }
10188 }
10189
10190 // Get the custom dynamic tag value.
10191
10192 template<int size, bool big_endian>
10193 unsigned int
10194 Target_powerpc<size, big_endian>::do_dynamic_tag_custom_value(
10195 elfcpp::DT tag) const
10196 {
10197 if (tag != elfcpp::DT_RELACOUNT)
10198 gold_unreachable();
10199 return this->rela_dyn_->relative_reloc_count();
10200 }
10201
10202 // Merge object attributes from input file called NAME with those of the
10203 // output. The input object attributes are in the object pointed by PASD.
10204
10205 template<int size, bool big_endian>
10206 void
10207 Target_powerpc<size, big_endian>::merge_object_attributes(
10208 const Object* obj,
10209 const Attributes_section_data* pasd)
10210 {
10211 // Return if there is no attributes section data.
10212 if (pasd == NULL)
10213 return;
10214
10215 // Create output object attributes.
10216 if (this->attributes_section_data_ == NULL)
10217 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10218
10219 const int vendor = Object_attribute::OBJ_ATTR_GNU;
10220 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10221 Object_attribute* out_attr
10222 = this->attributes_section_data_->known_attributes(vendor);
10223
10224 const char* name = obj->name().c_str();
10225 const char* err;
10226 const char* first;
10227 const char* second;
10228 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10229 int in_fp = in_attr[tag].int_value() & 0xf;
10230 int out_fp = out_attr[tag].int_value() & 0xf;
10231 bool warn_only = obj->is_dynamic();
10232 if (in_fp != out_fp)
10233 {
10234 err = NULL;
10235 if ((in_fp & 3) == 0)
10236 ;
10237 else if ((out_fp & 3) == 0)
10238 {
10239 if (!warn_only)
10240 {
10241 out_fp |= in_fp & 3;
10242 out_attr[tag].set_int_value(out_fp);
10243 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10244 this->last_fp_ = name;
10245 }
10246 }
10247 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10248 {
10249 err = N_("%s uses hard float, %s uses soft float");
10250 first = this->last_fp_;
10251 second = name;
10252 }
10253 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10254 {
10255 err = N_("%s uses hard float, %s uses soft float");
10256 first = name;
10257 second = this->last_fp_;
10258 }
10259 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10260 {
10261 err = N_("%s uses double-precision hard float, "
10262 "%s uses single-precision hard float");
10263 first = this->last_fp_;
10264 second = name;
10265 }
10266 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10267 {
10268 err = N_("%s uses double-precision hard float, "
10269 "%s uses single-precision hard float");
10270 first = name;
10271 second = this->last_fp_;
10272 }
10273
10274 if (err || (in_fp & 0xc) == 0)
10275 ;
10276 else if ((out_fp & 0xc) == 0)
10277 {
10278 if (!warn_only)
10279 {
10280 out_fp |= in_fp & 0xc;
10281 out_attr[tag].set_int_value(out_fp);
10282 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10283 this->last_ld_ = name;
10284 }
10285 }
10286 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10287 {
10288 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10289 first = name;
10290 second = this->last_ld_;
10291 }
10292 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10293 {
10294 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10295 first = this->last_ld_;
10296 second = name;
10297 }
10298 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10299 {
10300 err = N_("%s uses IBM long double, %s uses IEEE long double");
10301 first = this->last_ld_;
10302 second = name;
10303 }
10304 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10305 {
10306 err = N_("%s uses IBM long double, %s uses IEEE long double");
10307 first = name;
10308 second = this->last_ld_;
10309 }
10310
10311 if (err)
10312 {
10313 if (parameters->options().warn_mismatch())
10314 {
10315 if (warn_only)
10316 gold_warning(_(err), first, second);
10317 else
10318 gold_error(_(err), first, second);
10319 }
10320 // Arrange for this attribute to be deleted. It's better to
10321 // say "don't know" about a file than to wrongly claim compliance.
10322 if (!warn_only)
10323 out_attr[tag].set_type(0);
10324 }
10325 }
10326
10327 if (size == 32)
10328 {
10329 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10330 int in_vec = in_attr[tag].int_value() & 3;
10331 int out_vec = out_attr[tag].int_value() & 3;
10332 if (in_vec != out_vec)
10333 {
10334 err = NULL;
10335 if (in_vec == 0)
10336 ;
10337 else if (out_vec == 0)
10338 {
10339 out_vec = in_vec;
10340 out_attr[tag].set_int_value(out_vec);
10341 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10342 this->last_vec_ = name;
10343 }
10344 // For now, allow generic to transition to AltiVec or SPE
10345 // without a warning. If GCC marked files with their stack
10346 // alignment and used don't-care markings for files which are
10347 // not affected by the vector ABI, we could warn about this
10348 // case too. */
10349 else if (in_vec == 1)
10350 ;
10351 else if (out_vec == 1)
10352 {
10353 out_vec = in_vec;
10354 out_attr[tag].set_int_value(out_vec);
10355 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10356 this->last_vec_ = name;
10357 }
10358 else if (out_vec < in_vec)
10359 {
10360 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10361 first = this->last_vec_;
10362 second = name;
10363 }
10364 else if (out_vec > in_vec)
10365 {
10366 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10367 first = name;
10368 second = this->last_vec_;
10369 }
10370 if (err)
10371 {
10372 if (parameters->options().warn_mismatch())
10373 gold_error(_(err), first, second);
10374 out_attr[tag].set_type(0);
10375 }
10376 }
10377
10378 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10379 int in_struct = in_attr[tag].int_value() & 3;
10380 int out_struct = out_attr[tag].int_value() & 3;
10381 if (in_struct != out_struct)
10382 {
10383 err = NULL;
10384 if (in_struct == 0 || in_struct == 3)
10385 ;
10386 else if (out_struct == 0)
10387 {
10388 out_struct = in_struct;
10389 out_attr[tag].set_int_value(out_struct);
10390 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10391 this->last_struct_ = name;
10392 }
10393 else if (out_struct < in_struct)
10394 {
10395 err = N_("%s uses r3/r4 for small structure returns, "
10396 "%s uses memory");
10397 first = this->last_struct_;
10398 second = name;
10399 }
10400 else if (out_struct > in_struct)
10401 {
10402 err = N_("%s uses r3/r4 for small structure returns, "
10403 "%s uses memory");
10404 first = name;
10405 second = this->last_struct_;
10406 }
10407 if (err)
10408 {
10409 if (parameters->options().warn_mismatch())
10410 gold_error(_(err), first, second);
10411 out_attr[tag].set_type(0);
10412 }
10413 }
10414 }
10415
10416 // Merge Tag_compatibility attributes and any common GNU ones.
10417 this->attributes_section_data_->merge(name, pasd);
10418 }
10419
10420 // Emit any saved relocs, and mark toc entries using any of these
10421 // relocs as not optimizable.
10422
10423 template<int sh_type, int size, bool big_endian>
10424 void
10425 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10426 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10427 {
10428 if (size == 64
10429 && parameters->options().toc_optimize())
10430 {
10431 for (typename Copy_relocs<sh_type, size, big_endian>::
10432 Copy_reloc_entries::iterator p = this->entries_.begin();
10433 p != this->entries_.end();
10434 ++p)
10435 {
10436 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10437 entry = *p;
10438
10439 // If the symbol is no longer defined in a dynamic object,
10440 // then we emitted a COPY relocation. If it is still
10441 // dynamic then we'll need dynamic relocations and thus
10442 // can't optimize toc entries.
10443 if (entry.sym_->is_from_dynobj())
10444 {
10445 Powerpc_relobj<size, big_endian>* ppc_object
10446 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10447 if (entry.shndx_ == ppc_object->toc_shndx())
10448 ppc_object->set_no_toc_opt(entry.address_);
10449 }
10450 }
10451 }
10452
10453 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10454 }
10455
10456 // Return the value to use for a branch relocation.
10457
10458 template<int size, bool big_endian>
10459 bool
10460 Target_powerpc<size, big_endian>::symval_for_branch(
10461 const Symbol_table* symtab,
10462 const Sized_symbol<size>* gsym,
10463 Powerpc_relobj<size, big_endian>* object,
10464 Address *value,
10465 unsigned int *dest_shndx)
10466 {
10467 if (size == 32 || this->abiversion() >= 2)
10468 gold_unreachable();
10469 *dest_shndx = 0;
10470
10471 // If the symbol is defined in an opd section, ie. is a function
10472 // descriptor, use the function descriptor code entry address
10473 Powerpc_relobj<size, big_endian>* symobj = object;
10474 if (gsym != NULL
10475 && (gsym->source() != Symbol::FROM_OBJECT
10476 || gsym->object()->is_dynamic()))
10477 return true;
10478 if (gsym != NULL)
10479 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10480 unsigned int shndx = symobj->opd_shndx();
10481 if (shndx == 0)
10482 return true;
10483 Address opd_addr = symobj->get_output_section_offset(shndx);
10484 if (opd_addr == invalid_address)
10485 return true;
10486 opd_addr += symobj->output_section_address(shndx);
10487 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10488 {
10489 Address sec_off;
10490 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10491 if (symtab->is_section_folded(symobj, *dest_shndx))
10492 {
10493 Section_id folded
10494 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10495 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10496 *dest_shndx = folded.second;
10497 }
10498 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10499 if (sec_addr == invalid_address)
10500 return false;
10501
10502 sec_addr += symobj->output_section(*dest_shndx)->address();
10503 *value = sec_addr + sec_off;
10504 }
10505 return true;
10506 }
10507
10508 template<int size>
10509 static bool
10510 relative_value_is_known(const Sized_symbol<size>* gsym)
10511 {
10512 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10513 return false;
10514
10515 if (gsym->is_from_dynobj()
10516 || gsym->is_undefined()
10517 || gsym->is_preemptible())
10518 return false;
10519
10520 if (gsym->is_absolute())
10521 return !parameters->options().output_is_position_independent();
10522
10523 return true;
10524 }
10525
10526 template<int size>
10527 static bool
10528 relative_value_is_known(const Symbol_value<size>* psymval)
10529 {
10530 if (psymval->is_ifunc_symbol())
10531 return false;
10532
10533 bool is_ordinary;
10534 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10535
10536 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10537 }
10538
10539 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10540 // pld ra,symbol@got@pcrel
10541 // load/store rt,0(ra)
10542 // or
10543 // pla ra,symbol@pcrel
10544 // load/store rt,0(ra)
10545 // may be translated to
10546 // pload/pstore rt,symbol@pcrel
10547 // nop.
10548 // This function returns true if the optimization is possible, placing
10549 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10550 //
10551 // On entry to this function, the linker has already determined that
10552 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10553 // while *PINSN2 is the second instruction.
10554
10555 inline bool
10556 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10557 {
10558 uint32_t insn2 = *pinsn2 >> 32;
10559 uint64_t i1new;
10560
10561 // Check that regs match.
10562 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10563 return false;
10564
10565 switch ((insn2 >> 26) & 63)
10566 {
10567 default:
10568 return false;
10569
10570 case 32: // lwz
10571 case 34: // lbz
10572 case 36: // stw
10573 case 38: // stb
10574 case 40: // lhz
10575 case 42: // lha
10576 case 44: // sth
10577 case 48: // lfs
10578 case 50: // lfd
10579 case 52: // stfs
10580 case 54: // stfd
10581 // These are the PMLS cases, where we just need to tack a prefix
10582 // on the insn. Check that the D field is zero.
10583 if ((insn2 & 0xffff) != 0)
10584 return false;
10585 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10586 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10587 break;
10588
10589 case 58: // lwa, ld
10590 if ((insn2 & 0xfffd) != 0)
10591 return false;
10592 i1new = ((1ULL << 58) | (1ULL << 52)
10593 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10594 | (insn2 & (31ULL << 21)));
10595 break;
10596
10597 case 57: // lxsd, lxssp
10598 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10599 return false;
10600 i1new = ((1ULL << 58) | (1ULL << 52)
10601 | ((40ULL | (insn2 & 3)) << 26)
10602 | (insn2 & (31ULL << 21)));
10603 break;
10604
10605 case 61: // stxsd, stxssp, lxv, stxv
10606 if ((insn2 & 3) == 0)
10607 return false;
10608 else if ((insn2 & 3) >= 2)
10609 {
10610 if ((insn2 & 0xfffc) != 0)
10611 return false;
10612 i1new = ((1ULL << 58) | (1ULL << 52)
10613 | ((44ULL | (insn2 & 3)) << 26)
10614 | (insn2 & (31ULL << 21)));
10615 }
10616 else
10617 {
10618 if ((insn2 & 0xfff0) != 0)
10619 return false;
10620 i1new = ((1ULL << 58) | (1ULL << 52)
10621 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10622 | (insn2 & (31ULL << 21)));
10623 }
10624 break;
10625
10626 case 56: // lq
10627 if ((insn2 & 0xffff) != 0)
10628 return false;
10629 i1new = ((1ULL << 58) | (1ULL << 52)
10630 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10631 break;
10632
10633 case 62: // std, stq
10634 if ((insn2 & 0xfffd) != 0)
10635 return false;
10636 i1new = ((1ULL << 58) | (1ULL << 52)
10637 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10638 | (insn2 & (31ULL << 21)));
10639 break;
10640 }
10641
10642 *pinsn1 = i1new;
10643 *pinsn2 = (uint64_t) nop << 32;
10644 return true;
10645 }
10646
10647 // Perform a relocation.
10648
10649 template<int size, bool big_endian>
10650 inline bool
10651 Target_powerpc<size, big_endian>::Relocate::relocate(
10652 const Relocate_info<size, big_endian>* relinfo,
10653 unsigned int,
10654 Target_powerpc* target,
10655 Output_section* os,
10656 size_t relnum,
10657 const unsigned char* preloc,
10658 const Sized_symbol<size>* gsym,
10659 const Symbol_value<size>* psymval,
10660 unsigned char* view,
10661 Address address,
10662 section_size_type view_size)
10663 {
10664 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10665 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10666 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10667
10668 if (view == NULL)
10669 return true;
10670
10671 if (target->replace_tls_get_addr(gsym))
10672 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10673
10674 const elfcpp::Rela<size, big_endian> rela(preloc);
10675 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10676 Powerpc_relobj<size, big_endian>* const object
10677 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10678 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10679 {
10680 case Track_tls::NOT_EXPECTED:
10681 // No warning. This will result in really old code without tls
10682 // marker relocs being mis-optimised, but there shouldn't be too
10683 // much of that code around. The problem with warning is that
10684 // glibc and libphobos both construct direct calls to
10685 // __tls_get_addr in a way that is harmless.
10686 break;
10687 case Track_tls::EXPECTED:
10688 // We have already complained.
10689 break;
10690 case Track_tls::SKIP:
10691 if (is_plt16_reloc<size>(r_type)
10692 || r_type == elfcpp::R_POWERPC_PLTSEQ
10693 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10694 {
10695 Insn* iview = reinterpret_cast<Insn*>(view);
10696 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10697 }
10698 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10699 {
10700 Insn* iview = reinterpret_cast<Insn*>(view);
10701 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10702 }
10703 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10704 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10705 {
10706 Insn* iview = reinterpret_cast<Insn*>(view);
10707 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10708 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10709 }
10710 return true;
10711 case Track_tls::NORMAL:
10712 break;
10713 }
10714
10715 // Offset from start of insn to d-field reloc.
10716 const int d_offset = big_endian ? 2 : 0;
10717
10718 Address value = 0;
10719 bool has_stub_value = false;
10720 bool localentry0 = false;
10721 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10722 bool pltcall_to_direct = false;
10723
10724 if (is_plt16_reloc<size>(r_type)
10725 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10726 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10727 || r_type == elfcpp::R_POWERPC_PLTSEQ
10728 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10729 || r_type == elfcpp::R_POWERPC_PLTCALL
10730 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10731 {
10732 // It would be possible to replace inline plt calls with direct
10733 // calls if the PLTCALL is in range. The only difficulty is
10734 // that the decision depends on the PLTCALL reloc, and we don't
10735 // know the address of that instruction when processing others
10736 // in the sequence. So the decision needs to be made in
10737 // do_relax().
10738 pltcall_to_direct = !(gsym != NULL
10739 ? gsym->has_plt_offset()
10740 : object->local_has_plt_offset(r_sym));
10741 }
10742 else if ((gsym != NULL
10743 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10744 : psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
10745 && !is_got_reloc(r_type)
10746 && (!psymval->is_ifunc_symbol()
10747 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type,
10748 false)))
10749 {
10750 if (size == 64
10751 && gsym != NULL
10752 && target->abiversion() >= 2
10753 && !parameters->options().output_is_position_independent()
10754 && !is_branch_reloc<size>(r_type))
10755 {
10756 Address off = target->glink_section()->find_global_entry(gsym);
10757 if (off != invalid_address)
10758 {
10759 value = target->glink_section()->global_entry_address() + off;
10760 has_stub_value = true;
10761 }
10762 }
10763 else
10764 {
10765 Stub_table<size, big_endian>* stub_table = NULL;
10766 if (target->stub_tables().size() == 1)
10767 stub_table = target->stub_tables()[0];
10768 if (stub_table == NULL
10769 && !(size == 32
10770 && gsym != NULL
10771 && !parameters->options().output_is_position_independent()
10772 && !is_branch_reloc<size>(r_type)))
10773 stub_table = object->stub_table(relinfo->data_shndx);
10774 if (stub_table == NULL)
10775 {
10776 // This is a ref from a data section to an ifunc symbol,
10777 // or a non-branch reloc for which we always want to use
10778 // one set of stubs for resolving function addresses.
10779 if (target->stub_tables().size() != 0)
10780 stub_table = target->stub_tables()[0];
10781 }
10782 if (stub_table != NULL)
10783 {
10784 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10785 if (gsym != NULL)
10786 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10787 rela.get_r_addend());
10788 else
10789 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10790 rela.get_r_addend());
10791 if (ent != NULL)
10792 {
10793 value = stub_table->stub_address() + ent->off_;
10794 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10795 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10796 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10797 if (size == 64)
10798 {
10799 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10800 {
10801 if (!ent->notoc_)
10802 value += ent->p9off_;
10803 }
10804 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10805 value += ent->p9off_;
10806 else
10807 value += ent->tocoff_;
10808 }
10809 if (size == 64
10810 && ent->r2save_
10811 && !(gsym != NULL
10812 && target->is_tls_get_addr_opt(gsym)))
10813 {
10814 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10815 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10816 {
10817 if (!(target->power10_stubs()
10818 && target->power10_stubs_auto()))
10819 value += 4;
10820 }
10821 else if (relnum < reloc_count - 1)
10822 {
10823 Reltype next_rela(preloc + reloc_size);
10824 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10825 == elfcpp::R_PPC64_TOCSAVE
10826 && (next_rela.get_r_offset()
10827 == rela.get_r_offset() + 4))
10828 value += 4;
10829 }
10830 }
10831 localentry0 = ent->localentry0_;
10832 has_stub_value = true;
10833 }
10834 }
10835 }
10836 // We don't care too much about bogus debug references to
10837 // non-local functions, but otherwise there had better be a plt
10838 // call stub or global entry stub as appropriate.
10839 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10840 }
10841
10842 if (!pltcall_to_direct && (is_plt16_reloc<size>(r_type)
10843 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10844 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10845 {
10846 const Output_data_plt_powerpc<size, big_endian>* plt;
10847 if (gsym)
10848 value = target->plt_off(gsym, &plt);
10849 else
10850 value = target->plt_off(object, r_sym, &plt);
10851 value += plt->address();
10852
10853 if (size == 64)
10854 {
10855 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10856 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10857 value -= target->toc_pointer();
10858 }
10859 else if (parameters->options().output_is_position_independent())
10860 {
10861 if (rela.get_r_addend() >= 32768)
10862 {
10863 unsigned int got2 = object->got2_shndx();
10864 value -= (object->get_output_section_offset(got2)
10865 + object->output_section(got2)->address()
10866 + rela.get_r_addend());
10867 }
10868 else
10869 value -= target->toc_pointer();
10870 }
10871 }
10872 else if (pltcall_to_direct
10873 && (is_plt16_reloc<size>(r_type)
10874 || r_type == elfcpp::R_POWERPC_PLTSEQ
10875 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10876 {
10877 Insn* iview = reinterpret_cast<Insn*>(view);
10878 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10879 r_type = elfcpp::R_POWERPC_NONE;
10880 }
10881 else if (pltcall_to_direct
10882 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10883 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10884 {
10885 Insn* iview = reinterpret_cast<Insn*>(view);
10886 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10887 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10888 r_type = elfcpp::R_POWERPC_NONE;
10889 }
10890 else if (is_got_reloc(r_type))
10891 {
10892 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10893 Got_type got_type = ((size == 32
10894 || r_type == elfcpp::R_POWERPC_GOT16
10895 || r_type == elfcpp::R_PPC64_GOT16_DS)
10896 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10897 if (gsym != NULL)
10898 value = gsym->got_offset(got_type, addend);
10899 else
10900 value = object->local_got_offset(r_sym, got_type, addend);
10901 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10902 value += target->got_section(got_type)->address();
10903 else
10904 value -= target->got_base_offset(got_type);
10905 }
10906 else if (r_type == elfcpp::R_PPC64_TOC)
10907 {
10908 value = target->toc_pointer();
10909 }
10910 else if (gsym != NULL
10911 && (r_type == elfcpp::R_POWERPC_REL24
10912 || r_type == elfcpp::R_PPC_PLTREL24)
10913 && has_stub_value)
10914 {
10915 if (size == 64)
10916 {
10917 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10918 Valtype* wv = reinterpret_cast<Valtype*>(view);
10919 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10920 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10921 {
10922 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10923 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10924 if ((insn & 1) != 0
10925 && (insn2 == nop
10926 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10927 {
10928 elfcpp::Swap<32, big_endian>::
10929 writeval(wv + 1, ld_2_1 + target->stk_toc());
10930 can_plt_call = true;
10931 }
10932 }
10933 if (!can_plt_call)
10934 {
10935 // If we don't have a branch and link followed by a nop,
10936 // we can't go via the plt because there is no place to
10937 // put a toc restoring instruction.
10938 // Unless we know we won't be returning.
10939 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10940 can_plt_call = true;
10941 }
10942 if (!can_plt_call)
10943 {
10944 // g++ as of 20130507 emits self-calls without a
10945 // following nop. This is arguably wrong since we have
10946 // conflicting information. On the one hand a global
10947 // symbol and on the other a local call sequence, but
10948 // don't error for this special case.
10949 // It isn't possible to cheaply verify we have exactly
10950 // such a call. Allow all calls to the same section.
10951 bool ok = false;
10952 Address code = value;
10953 if (gsym->source() == Symbol::FROM_OBJECT
10954 && gsym->object() == object)
10955 {
10956 unsigned int dest_shndx = 0;
10957 if (target->abiversion() < 2)
10958 {
10959 Address addend = rela.get_r_addend();
10960 code = psymval->value(object, addend);
10961 target->symval_for_branch(relinfo->symtab, gsym, object,
10962 &code, &dest_shndx);
10963 }
10964 bool is_ordinary;
10965 if (dest_shndx == 0)
10966 dest_shndx = gsym->shndx(&is_ordinary);
10967 ok = dest_shndx == relinfo->data_shndx;
10968 }
10969 if (!ok)
10970 {
10971 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10972 _("call lacks nop, can't restore toc; "
10973 "recompile with -fPIC"));
10974 value = code;
10975 }
10976 }
10977 }
10978 }
10979 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10980 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10981 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10982 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10983 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10984 {
10985 // First instruction of a global dynamic sequence, arg setup insn.
10986 bool final = gsym == NULL || gsym->final_value_is_known();
10987 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10988 Got_type got_type = ((size == 32
10989 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10990 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10991 if (tls_type == tls::TLSOPT_NONE)
10992 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
10993 else if (tls_type == tls::TLSOPT_TO_IE)
10994 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10995 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
10996 {
10997 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10998 if (gsym != NULL)
10999 value = gsym->got_offset(got_type, addend);
11000 else
11001 value = object->local_got_offset(r_sym, got_type, addend);
11002 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11003 value += target->got_section(got_type)->address();
11004 else
11005 value -= target->got_base_offset(got_type);
11006 }
11007 if (tls_type == tls::TLSOPT_TO_IE)
11008 {
11009 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11010 {
11011 Insn* iview = reinterpret_cast<Insn*>(view);
11012 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11013 pinsn <<= 32;
11014 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11015 // pla -> pld
11016 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
11017 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11018 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11019 pinsn & 0xffffffff);
11020 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
11021 }
11022 else
11023 {
11024 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11025 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11026 {
11027 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11028 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11029 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
11030 if (size == 32)
11031 insn |= 32 << 26; // lwz
11032 else
11033 insn |= 58 << 26; // ld
11034 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11035 }
11036 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11037 - elfcpp::R_POWERPC_GOT_TLSGD16);
11038 }
11039 }
11040 else if (tls_type == tls::TLSOPT_TO_LE)
11041 {
11042 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11043 {
11044 Insn* iview = reinterpret_cast<Insn*>(view);
11045 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11046 pinsn <<= 32;
11047 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11048 // pla pcrel -> paddi r13
11049 pinsn += (-1ULL << 52) + (13ULL << 16);
11050 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11051 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11052 pinsn & 0xffffffff);
11053 r_type = elfcpp::R_PPC64_TPREL34;
11054 value = psymval->value(object, rela.get_r_addend());
11055 }
11056 else
11057 {
11058 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11059 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11060 {
11061 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11062 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11063 insn &= (1 << 26) - (1 << 21); // extract rt
11064 if (size == 32)
11065 insn |= addis_0_2;
11066 else
11067 insn |= addis_0_13;
11068 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11069 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11070 value = psymval->value(object, rela.get_r_addend());
11071 }
11072 else
11073 {
11074 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11075 Insn insn = nop;
11076 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11077 r_type = elfcpp::R_POWERPC_NONE;
11078 }
11079 }
11080 }
11081 }
11082 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11083 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11084 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11085 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
11086 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11087 {
11088 // First instruction of a local dynamic sequence, arg setup insn.
11089 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11090 if (tls_type == tls::TLSOPT_NONE)
11091 {
11092 value = target->tlsld_got_offset();
11093 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11094 value += target->got_section(GOT_TYPE_SMALL)->address();
11095 else
11096 value -= target->got_base_offset(GOT_TYPE_SMALL);
11097 }
11098 else
11099 {
11100 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11101 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11102 {
11103 Insn* iview = reinterpret_cast<Insn*>(view);
11104 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11105 pinsn <<= 32;
11106 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11107 // pla pcrel -> paddi r13
11108 pinsn += (-1ULL << 52) + (13ULL << 16);
11109 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11110 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11111 pinsn & 0xffffffff);
11112 r_type = elfcpp::R_PPC64_TPREL34;
11113 value = dtp_offset;
11114 }
11115 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11116 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11117 {
11118 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11119 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11120 insn &= (1 << 26) - (1 << 21); // extract rt
11121 if (size == 32)
11122 insn |= addis_0_2;
11123 else
11124 insn |= addis_0_13;
11125 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11126 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11127 value = dtp_offset;
11128 }
11129 else
11130 {
11131 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11132 Insn insn = nop;
11133 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11134 r_type = elfcpp::R_POWERPC_NONE;
11135 }
11136 }
11137 }
11138 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11139 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11140 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
11141 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
11142 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11143 {
11144 // Accesses relative to a local dynamic sequence address,
11145 // no optimisation here.
11146 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11147 Got_type got_type = ((size == 32
11148 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11149 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
11150 if (gsym != NULL)
11151 value = gsym->got_offset(got_type, addend);
11152 else
11153 value = object->local_got_offset(r_sym, got_type, addend);
11154 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11155 value += target->got_section(got_type)->address();
11156 else
11157 value -= target->got_base_offset(got_type);
11158 }
11159 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11160 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11161 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11162 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
11163 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11164 {
11165 // First instruction of initial exec sequence.
11166 bool final = gsym == NULL || gsym->final_value_is_known();
11167 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11168 if (tls_type == tls::TLSOPT_NONE)
11169 {
11170 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11171 Got_type got_type = ((size == 32
11172 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11173 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
11174 if (gsym != NULL)
11175 value = gsym->got_offset(got_type, addend);
11176 else
11177 value = object->local_got_offset(r_sym, got_type, addend);
11178 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11179 value += target->got_section(got_type)->address();
11180 else
11181 value -= target->got_base_offset(got_type);
11182 }
11183 else
11184 {
11185 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11186 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11187 {
11188 Insn* iview = reinterpret_cast<Insn*>(view);
11189 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11190 pinsn <<= 32;
11191 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11192 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11193 pinsn += ((2ULL << 56) + (-1ULL << 52)
11194 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11195 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11196 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11197 pinsn & 0xffffffff);
11198 r_type = elfcpp::R_PPC64_TPREL34;
11199 value = psymval->value(object, rela.get_r_addend());
11200 }
11201 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11202 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11203 {
11204 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11205 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11206 insn &= (1 << 26) - (1 << 21); // extract rt from ld
11207 if (size == 32)
11208 insn |= addis_0_2;
11209 else
11210 insn |= addis_0_13;
11211 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11212 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11213 value = psymval->value(object, rela.get_r_addend());
11214 }
11215 else
11216 {
11217 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11218 Insn insn = nop;
11219 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11220 r_type = elfcpp::R_POWERPC_NONE;
11221 }
11222 }
11223 }
11224 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11225 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11226 {
11227 // Second instruction of a global dynamic sequence,
11228 // the __tls_get_addr call
11229 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11230 bool final = gsym == NULL || gsym->final_value_is_known();
11231 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
11232 if (tls_type != tls::TLSOPT_NONE)
11233 {
11234 if (tls_type == tls::TLSOPT_TO_IE)
11235 {
11236 Insn* iview = reinterpret_cast<Insn*>(view);
11237 Insn insn = add_3_3_13;
11238 if (size == 32)
11239 insn = add_3_3_2;
11240 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11241 r_type = elfcpp::R_POWERPC_NONE;
11242 }
11243 else
11244 {
11245 bool is_pcrel = false;
11246 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11247 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11248 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11249 if (relnum < reloc_count - 1)
11250 {
11251 Reltype next_rela(preloc + reloc_size);
11252 unsigned int r_type2
11253 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11254 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11255 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11256 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11257 && next_rela.get_r_offset() == rela.get_r_offset())
11258 is_pcrel = true;
11259 }
11260 Insn* iview = reinterpret_cast<Insn*>(view);
11261 if (is_pcrel)
11262 {
11263 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11264 r_type = elfcpp::R_POWERPC_NONE;
11265 }
11266 else
11267 {
11268 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11269 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11270 view += d_offset;
11271 value = psymval->value(object, rela.get_r_addend());
11272 }
11273 }
11274 this->skip_next_tls_get_addr_call();
11275 }
11276 }
11277 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11278 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11279 {
11280 // Second instruction of a local dynamic sequence,
11281 // the __tls_get_addr call
11282 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11283 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11284 if (tls_type == tls::TLSOPT_TO_LE)
11285 {
11286 bool is_pcrel = false;
11287 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11288 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11289 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11290 if (relnum < reloc_count - 1)
11291 {
11292 Reltype next_rela(preloc + reloc_size);
11293 unsigned int r_type2
11294 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11295 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11296 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11297 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11298 && next_rela.get_r_offset() == rela.get_r_offset())
11299 is_pcrel = true;
11300 }
11301 Insn* iview = reinterpret_cast<Insn*>(view);
11302 if (is_pcrel)
11303 {
11304 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11305 r_type = elfcpp::R_POWERPC_NONE;
11306 }
11307 else
11308 {
11309 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11310 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11311 view += d_offset;
11312 value = dtp_offset;
11313 }
11314 this->skip_next_tls_get_addr_call();
11315 }
11316 }
11317 else if (r_type == elfcpp::R_POWERPC_TLS)
11318 {
11319 // Second instruction of an initial exec sequence
11320 bool final = gsym == NULL || gsym->final_value_is_known();
11321 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11322 if (tls_type == tls::TLSOPT_TO_LE)
11323 {
11324 Address roff = rela.get_r_offset() & 3;
11325 Insn* iview = reinterpret_cast<Insn*>(view - roff);
11326 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11327 unsigned int reg = size == 32 ? 2 : 13;
11328 insn = at_tls_transform(insn, reg);
11329 gold_assert(insn != 0);
11330 if (roff == 0)
11331 {
11332 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11333 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11334 view += d_offset;
11335 value = psymval->value(object, rela.get_r_addend());
11336 }
11337 else if (roff == 1)
11338 {
11339 // For pcrel IE to LE we already have the full offset
11340 // and thus don't need an addi here. A nop or mr will do.
11341 if ((insn & (0x3f << 26)) == 14 << 26)
11342 {
11343 // Extract regs from addi rt,ra,si.
11344 unsigned int rt = (insn >> 21) & 0x1f;
11345 unsigned int ra = (insn >> 16) & 0x1f;
11346 if (rt == ra)
11347 insn = nop;
11348 else
11349 {
11350 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11351 insn = (rt << 16) | (ra << 21) | (ra << 11);
11352 insn |= (31u << 26) | (444u << 1);
11353 }
11354 }
11355 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11356 r_type = elfcpp::R_POWERPC_NONE;
11357 }
11358 }
11359 }
11360 else if (!has_stub_value)
11361 {
11362 if (pltcall_to_direct && (r_type == elfcpp::R_POWERPC_PLTCALL
11363 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11364 {
11365 // PLTCALL without plt entry => convert to direct call
11366 Insn* iview = reinterpret_cast<Insn*>(view);
11367 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11368 insn = (insn & 1) | b;
11369 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11370 if (size == 32)
11371 r_type = elfcpp::R_PPC_PLTREL24;
11372 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11373 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11374 else
11375 r_type = elfcpp::R_POWERPC_REL24;
11376 }
11377 Address addend = 0;
11378 if (!(size == 32
11379 && (r_type == elfcpp::R_PPC_PLTREL24
11380 || r_type == elfcpp::R_POWERPC_PLT16_LO
11381 || r_type == elfcpp::R_POWERPC_PLT16_HI
11382 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11383 addend = rela.get_r_addend();
11384 value = psymval->value(object, addend);
11385 unsigned int local_ent = 0;
11386 if (size == 64 && is_branch_reloc<size>(r_type))
11387 {
11388 if (target->abiversion() >= 2)
11389 {
11390 if (gsym != NULL)
11391 local_ent = object->ppc64_local_entry_offset(gsym);
11392 else
11393 local_ent = object->ppc64_local_entry_offset(r_sym);
11394 }
11395 else
11396 {
11397 unsigned int dest_shndx;
11398 target->symval_for_branch(relinfo->symtab, gsym, object,
11399 &value, &dest_shndx);
11400 }
11401 }
11402 Address max_branch = max_branch_delta<size>(r_type);
11403 if (max_branch != 0
11404 && (value + local_ent - address + max_branch >= 2 * max_branch
11405 || (size == 64
11406 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11407 || r_type == elfcpp::R_PPC64_REL24_NOTOC)
11408 && (gsym != NULL
11409 ? object->ppc64_needs_toc(gsym)
11410 : object->ppc64_needs_toc(r_sym)))))
11411 {
11412 Stub_table<size, big_endian>* stub_table
11413 = object->stub_table(relinfo->data_shndx);
11414 if (stub_table != NULL)
11415 {
11416 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11417 = stub_table->find_long_branch_entry(value);
11418 if (ent != NULL)
11419 {
11420 if (ent->save_res_)
11421 value = (value - target->savres_section()->address()
11422 + stub_table->stub_address()
11423 + stub_table->plt_size()
11424 + stub_table->branch_size());
11425 else
11426 {
11427 value = (stub_table->stub_address()
11428 + stub_table->plt_size()
11429 + ent->off_);
11430 if (size == 64)
11431 {
11432 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11433 {
11434 if (!ent->notoc_)
11435 value += ent->p9off_;
11436 }
11437 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11438 value += ent->p9off_;
11439 else
11440 value += ent->tocoff_;
11441 }
11442 }
11443 has_stub_value = true;
11444 }
11445 }
11446 }
11447 if (!has_stub_value)
11448 value += local_ent;
11449 }
11450
11451 switch (r_type)
11452 {
11453 case elfcpp::R_PPC64_REL24_NOTOC:
11454 if (size == 32)
11455 break;
11456 // Fall through.
11457 case elfcpp::R_PPC64_REL24_P9NOTOC:
11458 case elfcpp::R_PPC64_REL64:
11459 case elfcpp::R_POWERPC_REL32:
11460 case elfcpp::R_POWERPC_REL24:
11461 case elfcpp::R_PPC_PLTREL24:
11462 case elfcpp::R_PPC_LOCAL24PC:
11463 case elfcpp::R_POWERPC_REL16:
11464 case elfcpp::R_POWERPC_REL16_LO:
11465 case elfcpp::R_POWERPC_REL16_HI:
11466 case elfcpp::R_POWERPC_REL16_HA:
11467 case elfcpp::R_POWERPC_REL16DX_HA:
11468 case elfcpp::R_PPC64_REL16_HIGH:
11469 case elfcpp::R_PPC64_REL16_HIGHA:
11470 case elfcpp::R_PPC64_REL16_HIGHER:
11471 case elfcpp::R_PPC64_REL16_HIGHERA:
11472 case elfcpp::R_PPC64_REL16_HIGHEST:
11473 case elfcpp::R_PPC64_REL16_HIGHESTA:
11474 case elfcpp::R_POWERPC_REL14:
11475 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11476 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11477 case elfcpp::R_PPC64_PCREL34:
11478 case elfcpp::R_PPC64_GOT_PCREL34:
11479 case elfcpp::R_PPC64_PLT_PCREL34:
11480 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11481 case elfcpp::R_PPC64_PCREL28:
11482 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11483 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11484 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11485 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11486 case elfcpp::R_PPC64_REL16_HIGHER34:
11487 case elfcpp::R_PPC64_REL16_HIGHERA34:
11488 case elfcpp::R_PPC64_REL16_HIGHEST34:
11489 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11490 value -= address;
11491 break;
11492
11493 case elfcpp::R_PPC64_TOC16:
11494 case elfcpp::R_PPC64_TOC16_LO:
11495 case elfcpp::R_PPC64_TOC16_HI:
11496 case elfcpp::R_PPC64_TOC16_HA:
11497 case elfcpp::R_PPC64_TOC16_DS:
11498 case elfcpp::R_PPC64_TOC16_LO_DS:
11499 // Subtract the TOC base address.
11500 value -= target->toc_pointer();
11501 break;
11502
11503 case elfcpp::R_POWERPC_SECTOFF:
11504 case elfcpp::R_POWERPC_SECTOFF_LO:
11505 case elfcpp::R_POWERPC_SECTOFF_HI:
11506 case elfcpp::R_POWERPC_SECTOFF_HA:
11507 case elfcpp::R_PPC64_SECTOFF_DS:
11508 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11509 if (os != NULL)
11510 value -= os->address();
11511 break;
11512
11513 case elfcpp::R_PPC64_TPREL16_DS:
11514 case elfcpp::R_PPC64_TPREL16_LO_DS:
11515 case elfcpp::R_PPC64_TPREL16_HIGH:
11516 case elfcpp::R_PPC64_TPREL16_HIGHA:
11517 if (size != 64)
11518 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11519 break;
11520 // Fall through.
11521 case elfcpp::R_POWERPC_TPREL16:
11522 case elfcpp::R_POWERPC_TPREL16_LO:
11523 case elfcpp::R_POWERPC_TPREL16_HI:
11524 case elfcpp::R_POWERPC_TPREL16_HA:
11525 case elfcpp::R_POWERPC_TPREL:
11526 case elfcpp::R_PPC64_TPREL16_HIGHER:
11527 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11528 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11529 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11530 case elfcpp::R_PPC64_TPREL34:
11531 // tls symbol values are relative to tls_segment()->vaddr()
11532 value -= tp_offset;
11533 break;
11534
11535 case elfcpp::R_PPC64_DTPREL16_DS:
11536 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11537 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11538 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11539 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11540 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11541 if (size != 64)
11542 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11543 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11544 break;
11545 // Fall through.
11546 case elfcpp::R_POWERPC_DTPREL16:
11547 case elfcpp::R_POWERPC_DTPREL16_LO:
11548 case elfcpp::R_POWERPC_DTPREL16_HI:
11549 case elfcpp::R_POWERPC_DTPREL16_HA:
11550 case elfcpp::R_POWERPC_DTPREL:
11551 case elfcpp::R_PPC64_DTPREL16_HIGH:
11552 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11553 case elfcpp::R_PPC64_DTPREL34:
11554 // tls symbol values are relative to tls_segment()->vaddr()
11555 value -= dtp_offset;
11556 break;
11557
11558 case elfcpp::R_PPC64_ADDR64_LOCAL:
11559 if (gsym != NULL)
11560 value += object->ppc64_local_entry_offset(gsym);
11561 else
11562 value += object->ppc64_local_entry_offset(r_sym);
11563 break;
11564
11565 default:
11566 break;
11567 }
11568
11569 Insn branch_bit = 0;
11570 switch (r_type)
11571 {
11572 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11573 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11574 branch_bit = 1 << 21;
11575 // Fall through.
11576 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11577 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11578 {
11579 Insn* iview = reinterpret_cast<Insn*>(view);
11580 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11581 insn &= ~(1 << 21);
11582 insn |= branch_bit;
11583 if (this->is_isa_v2)
11584 {
11585 // Set 'a' bit. This is 0b00010 in BO field for branch
11586 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11587 // for branch on CTR insns (BO == 1a00t or 1a01t).
11588 if ((insn & (0x14 << 21)) == (0x04 << 21))
11589 insn |= 0x02 << 21;
11590 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11591 insn |= 0x08 << 21;
11592 else
11593 break;
11594 }
11595 else
11596 {
11597 // Invert 'y' bit if not the default.
11598 if (static_cast<Signed_address>(value) < 0)
11599 insn ^= 1 << 21;
11600 }
11601 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11602 }
11603 break;
11604
11605 case elfcpp::R_POWERPC_PLT16_HA:
11606 if (size == 32
11607 && !parameters->options().output_is_position_independent())
11608 {
11609 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11610 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11611
11612 // Convert addis to lis.
11613 if ((insn & (0x3f << 26)) == 15u << 26
11614 && (insn & (0x1f << 16)) != 0)
11615 {
11616 insn &= ~(0x1f << 16);
11617 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11618 }
11619 }
11620 break;
11621
11622 default:
11623 break;
11624 }
11625
11626 if (gsym
11627 ? relative_value_is_known(gsym)
11628 : relative_value_is_known(psymval))
11629 {
11630 Insn* iview;
11631 Insn* iview2;
11632 Insn insn;
11633 uint64_t pinsn, pinsn2;
11634
11635 switch (r_type)
11636 {
11637 default:
11638 break;
11639
11640 // Multi-instruction sequences that access the GOT/TOC can
11641 // be optimized, eg.
11642 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11643 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11644 // and
11645 // addis ra,r2,0; addi rb,ra,x@toc@l;
11646 // to nop; addi rb,r2,x@toc;
11647 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11648 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11649 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11650 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11651 case elfcpp::R_POWERPC_GOT16_HA:
11652 case elfcpp::R_PPC64_TOC16_HA:
11653 if (size == 64 && parameters->options().toc_optimize())
11654 {
11655 iview = reinterpret_cast<Insn*>(view - d_offset);
11656 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11657 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11658 && object->make_toc_relative(target, &value))
11659 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11660 && object->make_got_relative(target, psymval,
11661 rela.get_r_addend(),
11662 &value)))
11663 {
11664 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11665 == ((15u << 26) | (2 << 16)));
11666 }
11667 if (((insn & ((0x3f << 26) | 0x1f << 16))
11668 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11669 && value + 0x8000 < 0x10000)
11670 {
11671 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11672 return true;
11673 }
11674 }
11675 break;
11676
11677 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11678 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11679 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11680 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11681 case elfcpp::R_POWERPC_GOT16_LO:
11682 case elfcpp::R_PPC64_GOT16_LO_DS:
11683 case elfcpp::R_PPC64_TOC16_LO:
11684 case elfcpp::R_PPC64_TOC16_LO_DS:
11685 if (size == 64 && parameters->options().toc_optimize())
11686 {
11687 iview = reinterpret_cast<Insn*>(view - d_offset);
11688 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11689 bool changed = false;
11690 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11691 && object->make_toc_relative(target, &value))
11692 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11693 && object->make_got_relative(target, psymval,
11694 rela.get_r_addend(),
11695 &value)))
11696 {
11697 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11698 insn ^= (14u << 26) ^ (58u << 26);
11699 r_type = elfcpp::R_PPC64_TOC16_LO;
11700 changed = true;
11701 }
11702 if (ok_lo_toc_insn(insn, r_type)
11703 && value + 0x8000 < 0x10000)
11704 {
11705 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11706 {
11707 // Transform addic to addi when we change reg.
11708 insn &= ~((0x3f << 26) | (0x1f << 16));
11709 insn |= (14u << 26) | (2 << 16);
11710 }
11711 else
11712 {
11713 insn &= ~(0x1f << 16);
11714 insn |= 2 << 16;
11715 }
11716 changed = true;
11717 }
11718 if (changed)
11719 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11720 }
11721 break;
11722
11723 case elfcpp::R_PPC64_GOT_PCREL34:
11724 if (size == 64 && parameters->options().toc_optimize())
11725 {
11726 iview = reinterpret_cast<Insn*>(view);
11727 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11728 pinsn <<= 32;
11729 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11730 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11731 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11732 break;
11733
11734 Address relval = psymval->value(object, rela.get_r_addend());
11735 relval -= address;
11736 if (relval + (1ULL << 33) < 1ULL << 34)
11737 {
11738 value = relval;
11739 // Replace with paddi
11740 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11741 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11742 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11743 pinsn & 0xffffffff);
11744 goto pcrelopt;
11745 }
11746 }
11747 break;
11748
11749 case elfcpp::R_PPC64_PCREL34:
11750 if (size == 64)
11751 {
11752 iview = reinterpret_cast<Insn*>(view);
11753 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11754 pinsn <<= 32;
11755 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11756 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11757 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11758 | (14ULL << 26) /* paddi */))
11759 break;
11760
11761 pcrelopt:
11762 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11763 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11764 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11765 if (relnum >= reloc_count - 1)
11766 break;
11767
11768 Reltype next_rela(preloc + reloc_size);
11769 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11770 != elfcpp::R_PPC64_PCREL_OPT)
11771 || next_rela.get_r_offset() != rela.get_r_offset())
11772 break;
11773
11774 Address off = next_rela.get_r_addend();
11775 if (off == 0)
11776 off = 8; // zero means next insn.
11777 if (off + rela.get_r_offset() + 4 > view_size)
11778 break;
11779
11780 iview2 = reinterpret_cast<Insn*>(view + off);
11781 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11782 pinsn2 <<= 32;
11783 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11784 break;
11785 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11786 {
11787 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11788 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11789 pinsn & 0xffffffff);
11790 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11791 }
11792 }
11793 break;
11794
11795 case elfcpp::R_POWERPC_TPREL16_HA:
11796 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11797 {
11798 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11799 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11800 return true;
11801 }
11802 break;
11803
11804 case elfcpp::R_PPC64_TPREL16_LO_DS:
11805 if (size == 32)
11806 // R_PPC_TLSGD, R_PPC_TLSLD
11807 break;
11808 // Fall through.
11809 case elfcpp::R_POWERPC_TPREL16_LO:
11810 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11811 {
11812 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11813 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11814 insn &= ~(0x1f << 16);
11815 insn |= (size == 32 ? 2 : 13) << 16;
11816 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11817 }
11818 break;
11819
11820 case elfcpp::R_PPC64_ENTRY:
11821 if (size == 64)
11822 {
11823 value = target->toc_pointer();
11824 if (value + 0x80008000 <= 0xffffffff
11825 && !parameters->options().output_is_position_independent())
11826 {
11827 Insn* iview = reinterpret_cast<Insn*>(view);
11828 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11829 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11830
11831 if ((insn1 & ~0xfffc) == ld_2_12
11832 && insn2 == add_2_2_12)
11833 {
11834 insn1 = lis_2 + ha(value);
11835 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11836 insn2 = addi_2_2 + l(value);
11837 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11838 return true;
11839 }
11840 }
11841 else
11842 {
11843 value -= address;
11844 if (value + 0x80008000 <= 0xffffffff)
11845 {
11846 Insn* iview = reinterpret_cast<Insn*>(view);
11847 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11848 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11849
11850 if ((insn1 & ~0xfffc) == ld_2_12
11851 && insn2 == add_2_2_12)
11852 {
11853 insn1 = addis_2_12 + ha(value);
11854 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11855 insn2 = addi_2_2 + l(value);
11856 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11857 return true;
11858 }
11859 }
11860 }
11861 }
11862 break;
11863
11864 case elfcpp::R_POWERPC_REL16_LO:
11865 // If we are generating a non-PIC executable, edit
11866 // 0: addis 2,12,.TOC.-0b@ha
11867 // addi 2,2,.TOC.-0b@l
11868 // used by ELFv2 global entry points to set up r2, to
11869 // lis 2,.TOC.@ha
11870 // addi 2,2,.TOC.@l
11871 // if .TOC. is in range. */
11872 if (size == 64
11873 && value + address - 4 + 0x80008000 <= 0xffffffff
11874 && relnum + 1 > 1
11875 && preloc != NULL
11876 && target->abiversion() >= 2
11877 && !parameters->options().output_is_position_independent()
11878 && rela.get_r_addend() == d_offset + 4
11879 && gsym != NULL
11880 && strcmp(gsym->name(), ".TOC.") == 0)
11881 {
11882 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11883 Reltype prev_rela(preloc - reloc_size);
11884 if ((prev_rela.get_r_info()
11885 == elfcpp::elf_r_info<size>(r_sym,
11886 elfcpp::R_POWERPC_REL16_HA))
11887 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11888 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11889 {
11890 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11891 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11892 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11893
11894 if ((insn1 & 0xffff0000) == addis_2_12
11895 && (insn2 & 0xffff0000) == addi_2_2)
11896 {
11897 insn1 = lis_2 + ha(value + address - 4);
11898 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11899 insn2 = addi_2_2 + l(value + address - 4);
11900 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11901 if (relinfo->rr)
11902 {
11903 relinfo->rr->set_strategy(relnum - 1,
11904 Relocatable_relocs::RELOC_SPECIAL);
11905 relinfo->rr->set_strategy(relnum,
11906 Relocatable_relocs::RELOC_SPECIAL);
11907 }
11908 return true;
11909 }
11910 }
11911 }
11912 break;
11913 }
11914 }
11915
11916 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11917 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11918 switch (r_type)
11919 {
11920 case elfcpp::R_POWERPC_ADDR32:
11921 case elfcpp::R_POWERPC_UADDR32:
11922 if (size == 64)
11923 overflow = Reloc::CHECK_BITFIELD;
11924 break;
11925
11926 case elfcpp::R_POWERPC_REL32:
11927 case elfcpp::R_POWERPC_REL16DX_HA:
11928 if (size == 64)
11929 overflow = Reloc::CHECK_SIGNED;
11930 break;
11931
11932 case elfcpp::R_POWERPC_UADDR16:
11933 overflow = Reloc::CHECK_BITFIELD;
11934 break;
11935
11936 case elfcpp::R_POWERPC_ADDR16:
11937 // We really should have three separate relocations,
11938 // one for 16-bit data, one for insns with 16-bit signed fields,
11939 // and one for insns with 16-bit unsigned fields.
11940 overflow = Reloc::CHECK_BITFIELD;
11941 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11942 overflow = Reloc::CHECK_LOW_INSN;
11943 break;
11944
11945 case elfcpp::R_POWERPC_ADDR16_HI:
11946 case elfcpp::R_POWERPC_ADDR16_HA:
11947 case elfcpp::R_POWERPC_GOT16_HI:
11948 case elfcpp::R_POWERPC_GOT16_HA:
11949 case elfcpp::R_POWERPC_PLT16_HI:
11950 case elfcpp::R_POWERPC_PLT16_HA:
11951 case elfcpp::R_POWERPC_SECTOFF_HI:
11952 case elfcpp::R_POWERPC_SECTOFF_HA:
11953 case elfcpp::R_PPC64_TOC16_HI:
11954 case elfcpp::R_PPC64_TOC16_HA:
11955 case elfcpp::R_PPC64_PLTGOT16_HI:
11956 case elfcpp::R_PPC64_PLTGOT16_HA:
11957 case elfcpp::R_POWERPC_TPREL16_HI:
11958 case elfcpp::R_POWERPC_TPREL16_HA:
11959 case elfcpp::R_POWERPC_DTPREL16_HI:
11960 case elfcpp::R_POWERPC_DTPREL16_HA:
11961 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11962 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11963 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11964 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11965 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11966 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11967 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11968 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11969 case elfcpp::R_POWERPC_REL16_HI:
11970 case elfcpp::R_POWERPC_REL16_HA:
11971 if (size != 32)
11972 overflow = Reloc::CHECK_HIGH_INSN;
11973 break;
11974
11975 case elfcpp::R_POWERPC_REL16:
11976 case elfcpp::R_PPC64_TOC16:
11977 case elfcpp::R_POWERPC_GOT16:
11978 case elfcpp::R_POWERPC_SECTOFF:
11979 case elfcpp::R_POWERPC_TPREL16:
11980 case elfcpp::R_POWERPC_DTPREL16:
11981 case elfcpp::R_POWERPC_GOT_TLSGD16:
11982 case elfcpp::R_POWERPC_GOT_TLSLD16:
11983 case elfcpp::R_POWERPC_GOT_TPREL16:
11984 case elfcpp::R_POWERPC_GOT_DTPREL16:
11985 overflow = Reloc::CHECK_LOW_INSN;
11986 break;
11987
11988 case elfcpp::R_PPC64_REL24_NOTOC:
11989 if (size == 32)
11990 break;
11991 // Fall through.
11992 case elfcpp::R_PPC64_REL24_P9NOTOC:
11993 case elfcpp::R_POWERPC_ADDR24:
11994 case elfcpp::R_POWERPC_ADDR14:
11995 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11996 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11997 case elfcpp::R_PPC64_ADDR16_DS:
11998 case elfcpp::R_POWERPC_REL24:
11999 case elfcpp::R_PPC_PLTREL24:
12000 case elfcpp::R_PPC_LOCAL24PC:
12001 case elfcpp::R_PPC64_TPREL16_DS:
12002 case elfcpp::R_PPC64_DTPREL16_DS:
12003 case elfcpp::R_PPC64_TOC16_DS:
12004 case elfcpp::R_PPC64_GOT16_DS:
12005 case elfcpp::R_PPC64_SECTOFF_DS:
12006 case elfcpp::R_POWERPC_REL14:
12007 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12008 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12009 case elfcpp::R_PPC64_D34:
12010 case elfcpp::R_PPC64_PCREL34:
12011 case elfcpp::R_PPC64_GOT_PCREL34:
12012 case elfcpp::R_PPC64_PLT_PCREL34:
12013 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12014 case elfcpp::R_PPC64_D28:
12015 case elfcpp::R_PPC64_PCREL28:
12016 case elfcpp::R_PPC64_TPREL34:
12017 case elfcpp::R_PPC64_DTPREL34:
12018 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12019 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12020 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12021 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12022 overflow = Reloc::CHECK_SIGNED;
12023 break;
12024 }
12025
12026 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
12027 Insn insn = 0;
12028
12029 if (overflow == Reloc::CHECK_LOW_INSN
12030 || overflow == Reloc::CHECK_HIGH_INSN)
12031 {
12032 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12033
12034 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
12035 overflow = Reloc::CHECK_BITFIELD;
12036 else if (overflow == Reloc::CHECK_LOW_INSN
12037 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
12038 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
12039 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
12040 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
12041 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
12042 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
12043 overflow = Reloc::CHECK_UNSIGNED;
12044 else
12045 overflow = Reloc::CHECK_SIGNED;
12046 }
12047
12048 bool maybe_dq_reloc = false;
12049 typename Powerpc_relocate_functions<size, big_endian>::Status status
12050 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
12051 switch (r_type)
12052 {
12053 case elfcpp::R_POWERPC_NONE:
12054 case elfcpp::R_POWERPC_TLS:
12055 case elfcpp::R_POWERPC_GNU_VTINHERIT:
12056 case elfcpp::R_POWERPC_GNU_VTENTRY:
12057 case elfcpp::R_POWERPC_PLTSEQ:
12058 case elfcpp::R_POWERPC_PLTCALL:
12059 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12060 case elfcpp::R_PPC64_PLTCALL_NOTOC:
12061 case elfcpp::R_PPC64_PCREL_OPT:
12062 break;
12063
12064 case elfcpp::R_PPC64_ADDR64:
12065 case elfcpp::R_PPC64_REL64:
12066 case elfcpp::R_PPC64_TOC:
12067 case elfcpp::R_PPC64_ADDR64_LOCAL:
12068 Reloc::addr64(view, value);
12069 break;
12070
12071 case elfcpp::R_POWERPC_TPREL:
12072 case elfcpp::R_POWERPC_DTPREL:
12073 if (size == 64)
12074 Reloc::addr64(view, value);
12075 else
12076 status = Reloc::addr32(view, value, overflow);
12077 break;
12078
12079 case elfcpp::R_PPC64_UADDR64:
12080 Reloc::addr64_u(view, value);
12081 break;
12082
12083 case elfcpp::R_POWERPC_ADDR32:
12084 status = Reloc::addr32(view, value, overflow);
12085 break;
12086
12087 case elfcpp::R_POWERPC_REL32:
12088 case elfcpp::R_POWERPC_UADDR32:
12089 status = Reloc::addr32_u(view, value, overflow);
12090 break;
12091
12092 case elfcpp::R_PPC64_REL24_NOTOC:
12093 if (size == 32)
12094 goto unsupp; // R_PPC_EMB_RELSDA
12095 // Fall through.
12096 case elfcpp::R_PPC64_REL24_P9NOTOC:
12097 case elfcpp::R_POWERPC_ADDR24:
12098 case elfcpp::R_POWERPC_REL24:
12099 case elfcpp::R_PPC_PLTREL24:
12100 case elfcpp::R_PPC_LOCAL24PC:
12101 status = Reloc::addr24(view, value, overflow);
12102 break;
12103
12104 case elfcpp::R_POWERPC_GOT_DTPREL16:
12105 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
12106 case elfcpp::R_POWERPC_GOT_TPREL16:
12107 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
12108 if (size == 64)
12109 {
12110 // On ppc64 these are all ds form
12111 maybe_dq_reloc = true;
12112 break;
12113 }
12114 // Fall through.
12115 case elfcpp::R_POWERPC_ADDR16:
12116 case elfcpp::R_POWERPC_REL16:
12117 case elfcpp::R_PPC64_TOC16:
12118 case elfcpp::R_POWERPC_GOT16:
12119 case elfcpp::R_POWERPC_SECTOFF:
12120 case elfcpp::R_POWERPC_TPREL16:
12121 case elfcpp::R_POWERPC_DTPREL16:
12122 case elfcpp::R_POWERPC_GOT_TLSGD16:
12123 case elfcpp::R_POWERPC_GOT_TLSLD16:
12124 case elfcpp::R_POWERPC_ADDR16_LO:
12125 case elfcpp::R_POWERPC_REL16_LO:
12126 case elfcpp::R_PPC64_TOC16_LO:
12127 case elfcpp::R_POWERPC_GOT16_LO:
12128 case elfcpp::R_POWERPC_PLT16_LO:
12129 case elfcpp::R_POWERPC_SECTOFF_LO:
12130 case elfcpp::R_POWERPC_TPREL16_LO:
12131 case elfcpp::R_POWERPC_DTPREL16_LO:
12132 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12133 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
12134 if (size == 64)
12135 status = Reloc::addr16(view, value, overflow);
12136 else
12137 maybe_dq_reloc = true;
12138 break;
12139
12140 case elfcpp::R_POWERPC_UADDR16:
12141 status = Reloc::addr16_u(view, value, overflow);
12142 break;
12143
12144 case elfcpp::R_PPC64_ADDR16_HIGH:
12145 case elfcpp::R_PPC64_TPREL16_HIGH:
12146 case elfcpp::R_PPC64_DTPREL16_HIGH:
12147 if (size == 32)
12148 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12149 goto unsupp;
12150 // Fall through.
12151 case elfcpp::R_POWERPC_ADDR16_HI:
12152 case elfcpp::R_POWERPC_REL16_HI:
12153 case elfcpp::R_PPC64_REL16_HIGH:
12154 case elfcpp::R_PPC64_TOC16_HI:
12155 case elfcpp::R_POWERPC_GOT16_HI:
12156 case elfcpp::R_POWERPC_PLT16_HI:
12157 case elfcpp::R_POWERPC_SECTOFF_HI:
12158 case elfcpp::R_POWERPC_TPREL16_HI:
12159 case elfcpp::R_POWERPC_DTPREL16_HI:
12160 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12161 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12162 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12163 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12164 Reloc::addr16_hi(view, value);
12165 break;
12166
12167 case elfcpp::R_PPC64_ADDR16_HIGHA:
12168 case elfcpp::R_PPC64_TPREL16_HIGHA:
12169 case elfcpp::R_PPC64_DTPREL16_HIGHA:
12170 if (size == 32)
12171 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12172 goto unsupp;
12173 // Fall through.
12174 case elfcpp::R_POWERPC_ADDR16_HA:
12175 case elfcpp::R_POWERPC_REL16_HA:
12176 case elfcpp::R_PPC64_REL16_HIGHA:
12177 case elfcpp::R_PPC64_TOC16_HA:
12178 case elfcpp::R_POWERPC_GOT16_HA:
12179 case elfcpp::R_POWERPC_PLT16_HA:
12180 case elfcpp::R_POWERPC_SECTOFF_HA:
12181 case elfcpp::R_POWERPC_TPREL16_HA:
12182 case elfcpp::R_POWERPC_DTPREL16_HA:
12183 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12184 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12185 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12186 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12187 Reloc::addr16_ha(view, value);
12188 break;
12189
12190 case elfcpp::R_POWERPC_REL16DX_HA:
12191 status = Reloc::addr16dx_ha(view, value, overflow);
12192 break;
12193
12194 case elfcpp::R_PPC64_DTPREL16_HIGHER:
12195 if (size == 32)
12196 // R_PPC_EMB_NADDR16_LO
12197 goto unsupp;
12198 // Fall through.
12199 case elfcpp::R_PPC64_ADDR16_HIGHER:
12200 case elfcpp::R_PPC64_REL16_HIGHER:
12201 case elfcpp::R_PPC64_TPREL16_HIGHER:
12202 Reloc::addr16_hi2(view, value);
12203 break;
12204
12205 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12206 if (size == 32)
12207 // R_PPC_EMB_NADDR16_HI
12208 goto unsupp;
12209 // Fall through.
12210 case elfcpp::R_PPC64_ADDR16_HIGHERA:
12211 case elfcpp::R_PPC64_REL16_HIGHERA:
12212 case elfcpp::R_PPC64_TPREL16_HIGHERA:
12213 Reloc::addr16_ha2(view, value);
12214 break;
12215
12216 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12217 if (size == 32)
12218 // R_PPC_EMB_NADDR16_HA
12219 goto unsupp;
12220 // Fall through.
12221 case elfcpp::R_PPC64_ADDR16_HIGHEST:
12222 case elfcpp::R_PPC64_REL16_HIGHEST:
12223 case elfcpp::R_PPC64_TPREL16_HIGHEST:
12224 Reloc::addr16_hi3(view, value);
12225 break;
12226
12227 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12228 if (size == 32)
12229 // R_PPC_EMB_SDAI16
12230 goto unsupp;
12231 // Fall through.
12232 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
12233 case elfcpp::R_PPC64_REL16_HIGHESTA:
12234 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12235 Reloc::addr16_ha3(view, value);
12236 break;
12237
12238 case elfcpp::R_PPC64_DTPREL16_DS:
12239 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12240 if (size == 32)
12241 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12242 goto unsupp;
12243 // Fall through.
12244 case elfcpp::R_PPC64_TPREL16_DS:
12245 case elfcpp::R_PPC64_TPREL16_LO_DS:
12246 if (size == 32)
12247 // R_PPC_TLSGD, R_PPC_TLSLD
12248 break;
12249 // Fall through.
12250 case elfcpp::R_PPC64_ADDR16_DS:
12251 case elfcpp::R_PPC64_ADDR16_LO_DS:
12252 case elfcpp::R_PPC64_TOC16_DS:
12253 case elfcpp::R_PPC64_TOC16_LO_DS:
12254 case elfcpp::R_PPC64_GOT16_DS:
12255 case elfcpp::R_PPC64_GOT16_LO_DS:
12256 case elfcpp::R_PPC64_PLT16_LO_DS:
12257 case elfcpp::R_PPC64_SECTOFF_DS:
12258 case elfcpp::R_PPC64_SECTOFF_LO_DS:
12259 maybe_dq_reloc = true;
12260 break;
12261
12262 case elfcpp::R_POWERPC_ADDR14:
12263 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12264 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12265 case elfcpp::R_POWERPC_REL14:
12266 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12267 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12268 status = Reloc::addr14(view, value, overflow);
12269 break;
12270
12271 case elfcpp::R_POWERPC_COPY:
12272 case elfcpp::R_POWERPC_GLOB_DAT:
12273 case elfcpp::R_POWERPC_JMP_SLOT:
12274 case elfcpp::R_POWERPC_RELATIVE:
12275 case elfcpp::R_POWERPC_DTPMOD:
12276 case elfcpp::R_PPC64_JMP_IREL:
12277 case elfcpp::R_POWERPC_IRELATIVE:
12278 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12279 _("unexpected reloc %u in object file"),
12280 r_type);
12281 break;
12282
12283 case elfcpp::R_PPC64_TOCSAVE:
12284 if (size == 32)
12285 // R_PPC_EMB_SDA21
12286 goto unsupp;
12287 else
12288 {
12289 Symbol_location loc;
12290 loc.object = relinfo->object;
12291 loc.shndx = relinfo->data_shndx;
12292 loc.offset = rela.get_r_offset();
12293 const Tocsave_loc *tocsave = target->tocsave_loc();
12294 if (tocsave->find(loc) != tocsave->end())
12295 {
12296 // If we've generated plt calls using this tocsave, then
12297 // the nop needs to be changed to save r2.
12298 Insn* iview = reinterpret_cast<Insn*>(view);
12299 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12300 elfcpp::Swap<32, big_endian>::
12301 writeval(iview, std_2_1 + target->stk_toc());
12302 }
12303 }
12304 break;
12305
12306 case elfcpp::R_PPC_EMB_SDA2I16:
12307 case elfcpp::R_PPC_EMB_SDA2REL:
12308 if (size == 32)
12309 goto unsupp;
12310 // R_PPC64_TLSGD, R_PPC64_TLSLD
12311 break;
12312
12313 case elfcpp::R_PPC64_D34:
12314 case elfcpp::R_PPC64_D34_LO:
12315 case elfcpp::R_PPC64_PCREL34:
12316 case elfcpp::R_PPC64_GOT_PCREL34:
12317 case elfcpp::R_PPC64_PLT_PCREL34:
12318 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12319 case elfcpp::R_PPC64_TPREL34:
12320 case elfcpp::R_PPC64_DTPREL34:
12321 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12322 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12323 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12324 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12325 if (size == 32)
12326 goto unsupp;
12327 status = Reloc::addr34(view, value, overflow);
12328 break;
12329
12330 case elfcpp::R_PPC64_D34_HI30:
12331 if (size == 32)
12332 goto unsupp;
12333 Reloc::addr34_hi(view, value);
12334 break;
12335
12336 case elfcpp::R_PPC64_D34_HA30:
12337 if (size == 32)
12338 goto unsupp;
12339 Reloc::addr34_ha(view, value);
12340 break;
12341
12342 case elfcpp::R_PPC64_D28:
12343 case elfcpp::R_PPC64_PCREL28:
12344 if (size == 32)
12345 goto unsupp;
12346 status = Reloc::addr28(view, value, overflow);
12347 break;
12348
12349 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12350 case elfcpp::R_PPC64_REL16_HIGHER34:
12351 if (size == 32)
12352 goto unsupp;
12353 Reloc::addr16_higher34(view, value);
12354 break;
12355
12356 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12357 case elfcpp::R_PPC64_REL16_HIGHERA34:
12358 if (size == 32)
12359 goto unsupp;
12360 Reloc::addr16_highera34(view, value);
12361 break;
12362
12363 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12364 case elfcpp::R_PPC64_REL16_HIGHEST34:
12365 if (size == 32)
12366 goto unsupp;
12367 Reloc::addr16_highest34(view, value);
12368 break;
12369
12370 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12371 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12372 if (size == 32)
12373 goto unsupp;
12374 Reloc::addr16_highesta34(view, value);
12375 break;
12376
12377 case elfcpp::R_POWERPC_PLT32:
12378 case elfcpp::R_POWERPC_PLTREL32:
12379 case elfcpp::R_PPC_SDAREL16:
12380 case elfcpp::R_POWERPC_ADDR30:
12381 case elfcpp::R_PPC64_PLT64:
12382 case elfcpp::R_PPC64_PLTREL64:
12383 case elfcpp::R_PPC64_PLTGOT16:
12384 case elfcpp::R_PPC64_PLTGOT16_LO:
12385 case elfcpp::R_PPC64_PLTGOT16_HI:
12386 case elfcpp::R_PPC64_PLTGOT16_HA:
12387 case elfcpp::R_PPC64_PLTGOT16_DS:
12388 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12389 case elfcpp::R_PPC_TOC16:
12390 default:
12391 unsupp:
12392 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12393 _("unsupported reloc %u"),
12394 r_type);
12395 break;
12396 }
12397
12398 if (maybe_dq_reloc)
12399 {
12400 if (insn == 0)
12401 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12402
12403 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12404 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12405 && (insn & 3) == 1))
12406 status = Reloc::addr16_dq(view, value, overflow);
12407 else if (size == 64
12408 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12409 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12410 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12411 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12412 status = Reloc::addr16_ds(view, value, overflow);
12413 else
12414 status = Reloc::addr16(view, value, overflow);
12415 }
12416
12417 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12418 && (has_stub_value
12419 || !(gsym != NULL
12420 && gsym->is_undefined()
12421 && is_branch_reloc<size>(r_type))))
12422 {
12423 std::string name;
12424 if (gsym)
12425 name = gsym->demangled_name();
12426 else
12427 name = relinfo->object->get_symbol_name(r_sym);
12428 if (os->flags() & elfcpp::SHF_ALLOC)
12429 {
12430 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12431 _("reloc type %u overflow against '%s'"),
12432 r_type, name.c_str());
12433 if (has_stub_value)
12434 gold_info(_("try relinking with a smaller --stub-group-size"));
12435 }
12436 else
12437 {
12438 gold_warning_at_location(relinfo, relnum, rela.get_r_offset(),
12439 _("reloc type %u overflow against '%s'"),
12440 r_type, name.c_str());
12441 gold_info(_("debug info may be unreliable, compile with -gdwarf64"));
12442 }
12443 }
12444
12445 return true;
12446 }
12447
12448 // Relocate section data.
12449
12450 template<int size, bool big_endian>
12451 void
12452 Target_powerpc<size, big_endian>::relocate_section(
12453 const Relocate_info<size, big_endian>* relinfo,
12454 unsigned int sh_type,
12455 const unsigned char* prelocs,
12456 size_t reloc_count,
12457 Output_section* output_section,
12458 bool needs_special_offset_handling,
12459 unsigned char* view,
12460 Address address,
12461 section_size_type view_size,
12462 const Reloc_symbol_changes* reloc_symbol_changes)
12463 {
12464 typedef Target_powerpc<size, big_endian> Powerpc;
12465 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12466 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12467 Powerpc_comdat_behavior;
12468 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12469 Classify_reloc;
12470
12471 gold_assert(sh_type == elfcpp::SHT_RELA);
12472
12473 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12474 Powerpc_comdat_behavior, Classify_reloc>(
12475 relinfo,
12476 this,
12477 prelocs,
12478 reloc_count,
12479 output_section,
12480 needs_special_offset_handling,
12481 view,
12482 address,
12483 view_size,
12484 reloc_symbol_changes);
12485 }
12486
12487 template<int size, bool big_endian>
12488 class Powerpc_scan_relocatable_reloc
12489 {
12490 public:
12491 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12492 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12493 static const int sh_type = elfcpp::SHT_RELA;
12494
12495 // Return the symbol referred to by the relocation.
12496 static inline unsigned int
12497 get_r_sym(const Reltype* reloc)
12498 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12499
12500 // Return the type of the relocation.
12501 static inline unsigned int
12502 get_r_type(const Reltype* reloc)
12503 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12504
12505 // Return the strategy to use for a local symbol which is not a
12506 // section symbol, given the relocation type.
12507 inline Relocatable_relocs::Reloc_strategy
12508 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12509 {
12510 if (r_type == 0 && r_sym == 0)
12511 return Relocatable_relocs::RELOC_DISCARD;
12512 return Relocatable_relocs::RELOC_COPY;
12513 }
12514
12515 // Return the strategy to use for a local symbol which is a section
12516 // symbol, given the relocation type.
12517 inline Relocatable_relocs::Reloc_strategy
12518 local_section_strategy(unsigned int, Relobj*)
12519 {
12520 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12521 }
12522
12523 // Return the strategy to use for a global symbol, given the
12524 // relocation type, the object, and the symbol index.
12525 inline Relocatable_relocs::Reloc_strategy
12526 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12527 {
12528 if (size == 32
12529 && (r_type == elfcpp::R_PPC_PLTREL24
12530 || r_type == elfcpp::R_POWERPC_PLT16_LO
12531 || r_type == elfcpp::R_POWERPC_PLT16_HI
12532 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12533 return Relocatable_relocs::RELOC_SPECIAL;
12534 return Relocatable_relocs::RELOC_COPY;
12535 }
12536 };
12537
12538 // Scan the relocs during a relocatable link.
12539
12540 template<int size, bool big_endian>
12541 void
12542 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12543 Symbol_table* symtab,
12544 Layout* layout,
12545 Sized_relobj_file<size, big_endian>* object,
12546 unsigned int data_shndx,
12547 unsigned int sh_type,
12548 const unsigned char* prelocs,
12549 size_t reloc_count,
12550 Output_section* output_section,
12551 bool needs_special_offset_handling,
12552 size_t local_symbol_count,
12553 const unsigned char* plocal_symbols,
12554 Relocatable_relocs* rr)
12555 {
12556 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12557
12558 gold_assert(sh_type == elfcpp::SHT_RELA);
12559
12560 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12561 symtab,
12562 layout,
12563 object,
12564 data_shndx,
12565 prelocs,
12566 reloc_count,
12567 output_section,
12568 needs_special_offset_handling,
12569 local_symbol_count,
12570 plocal_symbols,
12571 rr);
12572 }
12573
12574 // Scan the relocs for --emit-relocs.
12575
12576 template<int size, bool big_endian>
12577 void
12578 Target_powerpc<size, big_endian>::emit_relocs_scan(
12579 Symbol_table* symtab,
12580 Layout* layout,
12581 Sized_relobj_file<size, big_endian>* object,
12582 unsigned int data_shndx,
12583 unsigned int sh_type,
12584 const unsigned char* prelocs,
12585 size_t reloc_count,
12586 Output_section* output_section,
12587 bool needs_special_offset_handling,
12588 size_t local_symbol_count,
12589 const unsigned char* plocal_syms,
12590 Relocatable_relocs* rr)
12591 {
12592 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12593 Classify_reloc;
12594 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12595 Emit_relocs_strategy;
12596
12597 gold_assert(sh_type == elfcpp::SHT_RELA);
12598
12599 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12600 symtab,
12601 layout,
12602 object,
12603 data_shndx,
12604 prelocs,
12605 reloc_count,
12606 output_section,
12607 needs_special_offset_handling,
12608 local_symbol_count,
12609 plocal_syms,
12610 rr);
12611 }
12612
12613 // Emit relocations for a section.
12614 // This is a modified version of the function by the same name in
12615 // target-reloc.h. Using relocate_special_relocatable for
12616 // R_PPC_PLTREL24 would require duplication of the entire body of the
12617 // loop, so we may as well duplicate the whole thing.
12618
12619 template<int size, bool big_endian>
12620 void
12621 Target_powerpc<size, big_endian>::relocate_relocs(
12622 const Relocate_info<size, big_endian>* relinfo,
12623 unsigned int sh_type,
12624 const unsigned char* prelocs,
12625 size_t reloc_count,
12626 Output_section* output_section,
12627 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12628 unsigned char*,
12629 Address view_address,
12630 section_size_type,
12631 unsigned char* reloc_view,
12632 section_size_type reloc_view_size)
12633 {
12634 gold_assert(sh_type == elfcpp::SHT_RELA);
12635
12636 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12637 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12638 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12639 // Offset from start of insn to d-field reloc.
12640 const int d_offset = big_endian ? 2 : 0;
12641
12642 Powerpc_relobj<size, big_endian>* const object
12643 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12644 const unsigned int local_count = object->local_symbol_count();
12645 unsigned int got2_shndx = object->got2_shndx();
12646 Address got2_addend = 0;
12647 if (got2_shndx != 0)
12648 {
12649 got2_addend = object->get_output_section_offset(got2_shndx);
12650 gold_assert(got2_addend != invalid_address);
12651 }
12652
12653 const bool relocatable = parameters->options().relocatable();
12654
12655 unsigned char* pwrite = reloc_view;
12656 bool zap_next = false;
12657 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12658 {
12659 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12660 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12661 continue;
12662
12663 Reltype reloc(prelocs);
12664 Reltype_write reloc_write(pwrite);
12665
12666 Address offset = reloc.get_r_offset();
12667 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12668 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12669 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12670 const unsigned int orig_r_sym = r_sym;
12671 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12672 = reloc.get_r_addend();
12673 const Symbol* gsym = NULL;
12674
12675 if (zap_next)
12676 {
12677 // We could arrange to discard these and other relocs for
12678 // tls optimised sequences in the strategy methods, but for
12679 // now do as BFD ld does.
12680 r_type = elfcpp::R_POWERPC_NONE;
12681 zap_next = false;
12682 }
12683
12684 // Get the new symbol index.
12685 Output_section* os = NULL;
12686 if (r_sym < local_count)
12687 {
12688 switch (strategy)
12689 {
12690 case Relocatable_relocs::RELOC_COPY:
12691 case Relocatable_relocs::RELOC_SPECIAL:
12692 if (r_sym != 0)
12693 {
12694 r_sym = object->symtab_index(r_sym);
12695 gold_assert(r_sym != -1U);
12696 }
12697 break;
12698
12699 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12700 {
12701 // We are adjusting a section symbol. We need to find
12702 // the symbol table index of the section symbol for
12703 // the output section corresponding to input section
12704 // in which this symbol is defined.
12705 gold_assert(r_sym < local_count);
12706 bool is_ordinary;
12707 unsigned int shndx =
12708 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12709 gold_assert(is_ordinary);
12710 os = object->output_section(shndx);
12711 gold_assert(os != NULL);
12712 gold_assert(os->needs_symtab_index());
12713 r_sym = os->symtab_index();
12714 }
12715 break;
12716
12717 default:
12718 gold_unreachable();
12719 }
12720 }
12721 else
12722 {
12723 gsym = object->global_symbol(r_sym);
12724 gold_assert(gsym != NULL);
12725 if (gsym->is_forwarder())
12726 gsym = relinfo->symtab->resolve_forwards(gsym);
12727
12728 gold_assert(gsym->has_symtab_index());
12729 r_sym = gsym->symtab_index();
12730 }
12731
12732 // Get the new offset--the location in the output section where
12733 // this relocation should be applied.
12734 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12735 offset += offset_in_output_section;
12736 else
12737 {
12738 section_offset_type sot_offset =
12739 convert_types<section_offset_type, Address>(offset);
12740 section_offset_type new_sot_offset =
12741 output_section->output_offset(object, relinfo->data_shndx,
12742 sot_offset);
12743 gold_assert(new_sot_offset != -1);
12744 offset = new_sot_offset;
12745 }
12746
12747 // In an object file, r_offset is an offset within the section.
12748 // In an executable or dynamic object, generated by
12749 // --emit-relocs, r_offset is an absolute address.
12750 if (!relocatable)
12751 {
12752 offset += view_address;
12753 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12754 offset -= offset_in_output_section;
12755 }
12756
12757 // Handle the reloc addend based on the strategy.
12758 if (strategy == Relocatable_relocs::RELOC_COPY)
12759 ;
12760 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12761 {
12762 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12763 addend = psymval->value(object, addend);
12764 // In a relocatable link, the symbol value is relative to
12765 // the start of the output section. For a non-relocatable
12766 // link, we need to adjust the addend.
12767 if (!relocatable)
12768 {
12769 gold_assert(os != NULL);
12770 addend -= os->address();
12771 }
12772 }
12773 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12774 {
12775 if (size == 32)
12776 {
12777 if (addend >= 32768)
12778 addend += got2_addend;
12779 }
12780 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12781 {
12782 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12783 addend -= d_offset;
12784 }
12785 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12786 {
12787 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12788 addend -= d_offset + 4;
12789 }
12790 }
12791 else
12792 gold_unreachable();
12793
12794 if (!relocatable)
12795 {
12796 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12797 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12798 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12799 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12800 {
12801 // First instruction of a global dynamic sequence,
12802 // arg setup insn.
12803 bool final = gsym == NULL || gsym->final_value_is_known();
12804 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12805 switch (tls_type)
12806 {
12807 case tls::TLSOPT_TO_IE:
12808 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12809 - elfcpp::R_POWERPC_GOT_TLSGD16);
12810 break;
12811 case tls::TLSOPT_TO_LE:
12812 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12813 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12814 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12815 else
12816 {
12817 r_type = elfcpp::R_POWERPC_NONE;
12818 offset -= d_offset;
12819 }
12820 break;
12821 default:
12822 break;
12823 }
12824 }
12825 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12826 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12827 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12828 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12829 {
12830 // First instruction of a local dynamic sequence,
12831 // arg setup insn.
12832 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12833 if (tls_type == tls::TLSOPT_TO_LE)
12834 {
12835 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12836 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12837 {
12838 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12839 const Output_section* os = relinfo->layout->tls_segment()
12840 ->first_section();
12841 gold_assert(os != NULL);
12842 gold_assert(os->needs_symtab_index());
12843 r_sym = os->symtab_index();
12844 addend = dtp_offset;
12845 }
12846 else
12847 {
12848 r_type = elfcpp::R_POWERPC_NONE;
12849 offset -= d_offset;
12850 }
12851 }
12852 }
12853 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12854 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12855 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12856 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12857 {
12858 // First instruction of initial exec sequence.
12859 bool final = gsym == NULL || gsym->final_value_is_known();
12860 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12861 {
12862 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12863 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12864 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12865 else
12866 {
12867 r_type = elfcpp::R_POWERPC_NONE;
12868 offset -= d_offset;
12869 }
12870 }
12871 }
12872 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12873 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12874 {
12875 // Second instruction of a global dynamic sequence,
12876 // the __tls_get_addr call
12877 bool final = gsym == NULL || gsym->final_value_is_known();
12878 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12879 switch (tls_type)
12880 {
12881 case tls::TLSOPT_TO_IE:
12882 r_type = elfcpp::R_POWERPC_NONE;
12883 zap_next = true;
12884 break;
12885 case tls::TLSOPT_TO_LE:
12886 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12887 offset += d_offset;
12888 zap_next = true;
12889 break;
12890 default:
12891 break;
12892 }
12893 }
12894 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12895 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12896 {
12897 // Second instruction of a local dynamic sequence,
12898 // the __tls_get_addr call
12899 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12900 if (tls_type == tls::TLSOPT_TO_LE)
12901 {
12902 const Output_section* os = relinfo->layout->tls_segment()
12903 ->first_section();
12904 gold_assert(os != NULL);
12905 gold_assert(os->needs_symtab_index());
12906 r_sym = os->symtab_index();
12907 addend = dtp_offset;
12908 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12909 offset += d_offset;
12910 zap_next = true;
12911 }
12912 }
12913 else if (r_type == elfcpp::R_POWERPC_TLS)
12914 {
12915 // Second instruction of an initial exec sequence
12916 bool final = gsym == NULL || gsym->final_value_is_known();
12917 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12918 {
12919 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12920 offset += d_offset;
12921 }
12922 }
12923 }
12924
12925 reloc_write.put_r_offset(offset);
12926 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12927 reloc_write.put_r_addend(addend);
12928
12929 pwrite += reloc_size;
12930 }
12931
12932 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12933 == reloc_view_size);
12934 }
12935
12936 // Return the value to use for a dynamic symbol which requires special
12937 // treatment. This is how we support equality comparisons of function
12938 // pointers across shared library boundaries, as described in the
12939 // processor specific ABI supplement.
12940
12941 template<int size, bool big_endian>
12942 uint64_t
12943 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12944 {
12945 if (size == 32)
12946 {
12947 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12948 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12949 p != this->stub_tables_.end();
12950 ++p)
12951 {
12952 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12953 = (*p)->find_plt_call_entry(gsym);
12954 if (ent != NULL)
12955 return (*p)->stub_address() + ent->off_;
12956 }
12957 }
12958 else if (this->abiversion() >= 2)
12959 {
12960 Address off = this->glink_section()->find_global_entry(gsym);
12961 if (off != invalid_address)
12962 return this->glink_section()->global_entry_address() + off;
12963 }
12964 gold_unreachable();
12965 }
12966
12967 // Return the PLT address to use for a local symbol.
12968 template<int size, bool big_endian>
12969 uint64_t
12970 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12971 const Relobj* object,
12972 unsigned int symndx) const
12973 {
12974 if (size == 32)
12975 {
12976 const Sized_relobj<size, big_endian>* relobj
12977 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12978 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12979 p != this->stub_tables_.end();
12980 ++p)
12981 {
12982 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12983 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12984 if (ent != NULL)
12985 return (*p)->stub_address() + ent->off_;
12986 }
12987 }
12988 gold_unreachable();
12989 }
12990
12991 // Return the PLT address to use for a global symbol.
12992 template<int size, bool big_endian>
12993 uint64_t
12994 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12995 const Symbol* gsym) const
12996 {
12997 if (size == 32)
12998 {
12999 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
13000 p != this->stub_tables_.end();
13001 ++p)
13002 {
13003 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
13004 = (*p)->find_plt_call_entry(gsym);
13005 if (ent != NULL)
13006 return (*p)->stub_address() + ent->off_;
13007 }
13008 }
13009 else if (this->abiversion() >= 2)
13010 {
13011 Address off = this->glink_section()->find_global_entry(gsym);
13012 if (off != invalid_address)
13013 return this->glink_section()->global_entry_address() + off;
13014 }
13015 gold_unreachable();
13016 }
13017
13018 // Return the offset to use for the GOT_INDX'th got entry which is
13019 // for a local tls symbol specified by OBJECT, SYMNDX.
13020 template<int size, bool big_endian>
13021 int64_t
13022 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
13023 const Relobj* object,
13024 unsigned int symndx,
13025 Output_data_got_base* got,
13026 unsigned int got_indx,
13027 uint64_t addend) const
13028 {
13029 const Powerpc_relobj<size, big_endian>* ppc_object
13030 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
13031 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
13032 {
13033 for (Got_type got_type = (size == 32
13034 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13035 got_type <= GOT_TYPE_SMALL_TPREL;
13036 got_type = Got_type(got_type + 1))
13037 if (got_type != GOT_TYPE_SMALL
13038 && ppc_object->local_has_got_offset(symndx, got_type, addend))
13039 {
13040 unsigned int off
13041 = ppc_object->local_got_offset(symndx, got_type, addend);
13042 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13043 off += size / 8;
13044 if (off == got_indx * (size / 8)
13045 && (size == 32 || got == this->got_section(got_type)))
13046 {
13047 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13048 return -tp_offset;
13049 else
13050 return -dtp_offset;
13051 }
13052 }
13053 }
13054 gold_unreachable();
13055 }
13056
13057 // Return the offset to use for the GOT_INDX'th got entry which is
13058 // for global tls symbol GSYM.
13059 template<int size, bool big_endian>
13060 int64_t
13061 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13062 Symbol* gsym,
13063 Output_data_got_base* got,
13064 unsigned int got_indx,
13065 uint64_t addend) const
13066 {
13067 if (gsym->type() == elfcpp::STT_TLS)
13068 {
13069 for (Got_type got_type = (size == 32
13070 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13071 got_type <= GOT_TYPE_SMALL_TPREL;
13072 got_type = Got_type(got_type + 1))
13073 if (got_type != GOT_TYPE_SMALL
13074 && gsym->has_got_offset(got_type, addend))
13075 {
13076 unsigned int off = gsym->got_offset(got_type, addend);
13077 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13078 off += size / 8;
13079 if (off == got_indx * (size / 8)
13080 && (size == 32 || got == this->got_section(got_type)))
13081 {
13082 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13083 return -tp_offset;
13084 else
13085 return -dtp_offset;
13086 }
13087 }
13088 }
13089 gold_unreachable();
13090 }
13091
13092 // The selector for powerpc object files.
13093
13094 template<int size, bool big_endian>
13095 class Target_selector_powerpc : public Target_selector
13096 {
13097 public:
13098 Target_selector_powerpc()
13099 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13100 size, big_endian,
13101 (size == 64
13102 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13103 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13104 (size == 64
13105 ? (big_endian ? "elf64ppc" : "elf64lppc")
13106 : (big_endian ? "elf32ppc" : "elf32lppc")))
13107 { }
13108
13109 virtual Target*
13110 do_instantiate_target()
13111 { return new Target_powerpc<size, big_endian>(); }
13112 };
13113
13114 Target_selector_powerpc<32, true> target_selector_ppc32;
13115 Target_selector_powerpc<32, false> target_selector_ppc32le;
13116 Target_selector_powerpc<64, true> target_selector_ppc64;
13117 Target_selector_powerpc<64, false> target_selector_ppc64le;
13118
13119 // Instantiate these constants for -O0
13120 template<int size, bool big_endian>
13121 const typename Output_data_glink<size, big_endian>::Address
13122 Output_data_glink<size, big_endian>::invalid_address;
13123 template<int size, bool big_endian>
13124 const typename Stub_table<size, big_endian>::Address
13125 Stub_table<size, big_endian>::invalid_address;
13126 template<int size, bool big_endian>
13127 const typename Target_powerpc<size, big_endian>::Address
13128 Target_powerpc<size, big_endian>::invalid_address;
13129
13130 } // End anonymous namespace.