]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/powerpc.cc
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright (C) 2008-2024 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 if (size == 64 && this->abiversion() < 2)
3718 {
3719 static const char* const thread_starter[] =
3720 {
3721 "pthread_create",
3722 /* libstdc++ */
3723 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3724 /* librt */
3725 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3726 "mq_notify", "create_timer",
3727 /* libanl */
3728 "getaddrinfo_a",
3729 /* libgomp */
3730 "GOMP_parallel",
3731 "GOMP_parallel_start",
3732 "GOMP_parallel_loop_static",
3733 "GOMP_parallel_loop_static_start",
3734 "GOMP_parallel_loop_dynamic",
3735 "GOMP_parallel_loop_dynamic_start",
3736 "GOMP_parallel_loop_guided",
3737 "GOMP_parallel_loop_guided_start",
3738 "GOMP_parallel_loop_runtime",
3739 "GOMP_parallel_loop_runtime_start",
3740 "GOMP_parallel_sections",
3741 "GOMP_parallel_sections_start",
3742 /* libgo */
3743 "__go_go",
3744 };
3745 bool thread_safe = parameters->options().plt_thread_safe();
3746
3747 if (!thread_safe
3748 && !parameters->options().user_set_plt_thread_safe())
3749 {
3750 if (parameters->options().shared())
3751 thread_safe = true;
3752 else
3753 {
3754 for (unsigned int i = 0;
3755 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3756 i++)
3757 {
3758 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3759 thread_safe = (sym != NULL
3760 && sym->in_reg()
3761 && sym->in_real_elf());
3762 if (thread_safe)
3763 break;
3764 }
3765 }
3766 }
3767 this->plt_thread_safe_ = thread_safe;
3768 }
3769
3770 if (size == 64
3771 && parameters->options().output_is_position_independent())
3772 {
3773 gold_assert (this->rela_dyn_);
3774 this->rela_dyn_size_ = this->rela_dyn_->current_data_size();
3775 }
3776
3777 this->stub_group_size_ = parameters->options().stub_group_size();
3778 bool no_size_errors = true;
3779 if (this->stub_group_size_ == 1)
3780 this->stub_group_size_ = 0x1c00000;
3781 else if (this->stub_group_size_ == -1)
3782 this->stub_group_size_ = -0x1e00000;
3783 else
3784 no_size_errors = false;
3785 this->group_sections(layout, task, no_size_errors);
3786 }
3787 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3788 {
3789 this->branch_lookup_table_.clear();
3790 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3791 p != this->stub_tables_.end();
3792 ++p)
3793 {
3794 (*p)->clear_stubs(true);
3795 }
3796 this->stub_tables_.clear();
3797 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3798 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3799 program_name, this->stub_group_size_);
3800 this->group_sections(layout, task, true);
3801 }
3802
3803 // We need address of stub tables valid for make_stub.
3804 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3805 p != this->stub_tables_.end();
3806 ++p)
3807 {
3808 const Powerpc_relobj<size, big_endian>* object
3809 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3810 Address off = object->get_output_section_offset((*p)->shndx());
3811 gold_assert(off != invalid_address);
3812 Output_section* os = (*p)->output_section();
3813 (*p)->set_address_and_size(os, off);
3814 }
3815
3816 if (pass != 1)
3817 {
3818 // Clear plt call stubs, long branch stubs and branch lookup table.
3819 prev_brlt_size = this->branch_lookup_table_.size();
3820 this->branch_lookup_table_.clear();
3821 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3822 p != this->stub_tables_.end();
3823 ++p)
3824 {
3825 (*p)->clear_stubs(false);
3826 }
3827 }
3828
3829 // Build all the stubs.
3830 this->relax_failed_ = false;
3831 Stub_table<size, big_endian>* ifunc_stub_table
3832 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3833 Stub_table<size, big_endian>* one_stub_table
3834 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3835 for (typename Branches::const_iterator b = this->branch_info_.begin();
3836 b != this->branch_info_.end();
3837 b++)
3838 {
3839 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3840 && !this->relax_failed_)
3841 {
3842 this->relax_failed_ = true;
3843 this->relax_fail_count_++;
3844 if (this->relax_fail_count_ < 3)
3845 return true;
3846 }
3847 }
3848 bool do_resize = false;
3849 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3850 p != this->stub_tables_.end();
3851 ++p)
3852 if ((*p)->need_resize())
3853 {
3854 do_resize = true;
3855 break;
3856 }
3857 if (do_resize)
3858 {
3859 this->branch_lookup_table_.clear();
3860 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3861 p != this->stub_tables_.end();
3862 ++p)
3863 (*p)->set_resizing(true);
3864 for (typename Branches::const_iterator b = this->branch_info_.begin();
3865 b != this->branch_info_.end();
3866 b++)
3867 {
3868 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3869 && !this->relax_failed_)
3870 {
3871 this->relax_failed_ = true;
3872 this->relax_fail_count_++;
3873 if (this->relax_fail_count_ < 3)
3874 return true;
3875 }
3876 }
3877 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3878 p != this->stub_tables_.end();
3879 ++p)
3880 (*p)->set_resizing(false);
3881 }
3882
3883 // Did anything change size?
3884 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3885 bool again = num_huge_branches != prev_brlt_size;
3886 if (size == 64 && num_huge_branches != 0)
3887 this->make_brlt_section(layout);
3888 if (size == 64 && again)
3889 {
3890 update_current_size(this->brlt_section_, num_huge_branches * 16);
3891 if (parameters->options().output_is_position_independent())
3892 {
3893 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3894 off_t cur = this->rela_dyn_size_ + num_huge_branches * reloc_size;
3895 update_current_size(this->rela_dyn_, cur);
3896 }
3897 }
3898
3899 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3900 p != this->stub_tables_.rend();
3901 ++p)
3902 (*p)->remove_eh_frame(layout);
3903
3904 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3905 p != this->stub_tables_.end();
3906 ++p)
3907 (*p)->add_eh_frame(layout);
3908
3909 typedef Unordered_set<Output_section*> Output_sections;
3910 Output_sections os_need_update;
3911 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3912 p != this->stub_tables_.end();
3913 ++p)
3914 {
3915 if ((*p)->size_update())
3916 {
3917 again = true;
3918 os_need_update.insert((*p)->output_section());
3919 }
3920 }
3921
3922 // Set output section offsets for all input sections in an output
3923 // section that just changed size. Anything past the stubs will
3924 // need updating.
3925 for (typename Output_sections::iterator p = os_need_update.begin();
3926 p != os_need_update.end();
3927 p++)
3928 {
3929 Output_section* os = *p;
3930 Address off = 0;
3931 typedef Output_section::Input_section_list Input_section_list;
3932 for (Input_section_list::const_iterator i = os->input_sections().begin();
3933 i != os->input_sections().end();
3934 ++i)
3935 {
3936 off = align_address(off, i->addralign());
3937 if (i->is_input_section() || i->is_relaxed_input_section())
3938 i->relobj()->set_section_offset(i->shndx(), off);
3939 if (i->is_relaxed_input_section())
3940 {
3941 Stub_table<size, big_endian>* stub_table
3942 = static_cast<Stub_table<size, big_endian>*>(
3943 i->relaxed_input_section());
3944 Address stub_table_size = stub_table->set_address_and_size(os, off);
3945 off += stub_table_size;
3946 // After a few iterations, set current stub table size
3947 // as min size threshold, so later stub tables can only
3948 // grow in size.
3949 if (pass >= 4)
3950 stub_table->set_min_size_threshold(stub_table_size);
3951 }
3952 else
3953 off += i->data_size();
3954 }
3955 // If .branch_lt is part of this output section, then we have
3956 // just done the offset adjustment.
3957 os->clear_section_offsets_need_adjustment();
3958 }
3959
3960 if (size == 64
3961 && !again
3962 && num_huge_branches != 0
3963 && parameters->options().output_is_position_independent())
3964 {
3965 // Fill in the BRLT relocs.
3966 this->rela_dyn_->reset_data_size();
3967 this->rela_dyn_->set_current_data_size(this->rela_dyn_size_);
3968 for (typename Branch_lookup_table::const_iterator p
3969 = this->branch_lookup_table_.begin();
3970 p != this->branch_lookup_table_.end();
3971 ++p)
3972 {
3973 this->rela_dyn_->add_relative(elfcpp::R_POWERPC_RELATIVE,
3974 this->brlt_section_, p->second,
3975 p->first);
3976 }
3977 this->rela_dyn_->finalize_data_size();
3978 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
3979 gold_assert(this->rela_dyn_->data_size()
3980 == this->rela_dyn_size_ + num_huge_branches * reloc_size);
3981 }
3982
3983 if (!again
3984 && (parameters->options().user_set_emit_stub_syms()
3985 ? parameters->options().emit_stub_syms()
3986 : (size == 64
3987 || parameters->options().output_is_position_independent()
3988 || parameters->options().emit_relocs())))
3989 {
3990 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3991 p != this->stub_tables_.end();
3992 ++p)
3993 (*p)->define_stub_syms(symtab);
3994
3995 if (this->glink_ != NULL)
3996 {
3997 int stub_size = this->glink_->pltresolve_size();
3998 Address value = -stub_size;
3999 if (size == 64)
4000 {
4001 value = 8;
4002 stub_size -= 8;
4003 }
4004 this->define_local(symtab, "__glink_PLTresolve",
4005 this->glink_, value, stub_size);
4006
4007 if (size != 64)
4008 this->define_local(symtab, "__glink", this->glink_, 0, 0);
4009 }
4010 }
4011
4012 return again;
4013 }
4014
4015 template<int size, bool big_endian>
4016 void
4017 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
4018 unsigned char* oview,
4019 uint64_t* paddress,
4020 off_t* plen) const
4021 {
4022 uint64_t address = plt->address();
4023 off_t len = plt->data_size();
4024
4025 if (plt == this->glink_)
4026 {
4027 // See Output_data_glink::do_write() for glink contents.
4028 if (len == 0)
4029 {
4030 // Static linking may need stubs, to support ifunc and long
4031 // branches. We need to create an output section for
4032 // .eh_frame early in the link process, to have a place to
4033 // attach stub .eh_frame info. We also need to have
4034 // registered a CIE that matches the stub CIE. Both of
4035 // these requirements are satisfied by creating an FDE and
4036 // CIE for .glink, even though static linking will leave
4037 // .glink zero length.
4038 // ??? Hopefully generating an FDE with a zero address range
4039 // won't confuse anything that consumes .eh_frame info.
4040 }
4041 else if (size == 64)
4042 {
4043 // There is one word before __glink_PLTresolve
4044 address += 8;
4045 len -= 8;
4046 }
4047 else if (parameters->options().output_is_position_independent())
4048 {
4049 // There are two FDEs for a position independent glink.
4050 // The first covers the branch table, the second
4051 // __glink_PLTresolve at the end of glink.
4052 off_t resolve_size = this->glink_->pltresolve_size();
4053 if (oview[9] == elfcpp::DW_CFA_nop)
4054 len -= resolve_size;
4055 else
4056 {
4057 address += len - resolve_size;
4058 len = resolve_size;
4059 }
4060 }
4061 }
4062 else
4063 {
4064 // Must be a stub table.
4065 const Stub_table<size, big_endian>* stub_table
4066 = static_cast<const Stub_table<size, big_endian>*>(plt);
4067 uint64_t stub_address = stub_table->stub_address();
4068 len -= stub_address - address;
4069 address = stub_address;
4070 }
4071
4072 *paddress = address;
4073 *plen = len;
4074 }
4075
4076 // A class to handle the PLT data.
4077
4078 template<int size, bool big_endian>
4079 class Output_data_plt_powerpc : public Output_section_data_build
4080 {
4081 public:
4082 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4083 size, big_endian> Reloc_section;
4084
4085 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4086 Symbol_table* symtab,
4087 Reloc_section* plt_rel,
4088 const char* name)
4089 : Output_section_data_build(size == 32 ? 4 : 8),
4090 rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
4091 { }
4092
4093 // Add an entry to the PLT.
4094 void
4095 add_entry(Symbol*, bool = false);
4096
4097 void
4098 add_ifunc_entry(Symbol*);
4099
4100 void
4101 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4102
4103 void
4104 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4105
4106 // Return the .rela.plt section data.
4107 Reloc_section*
4108 rel_plt() const
4109 {
4110 return this->rel_;
4111 }
4112
4113 // Return the number of PLT entries.
4114 unsigned int
4115 entry_count() const
4116 {
4117 if (this->current_data_size() == 0)
4118 return 0;
4119 return ((this->current_data_size() - this->first_plt_entry_offset())
4120 / this->plt_entry_size());
4121 }
4122
4123 protected:
4124 void
4125 do_adjust_output_section(Output_section* os)
4126 {
4127 os->set_entsize(0);
4128 }
4129
4130 // Write to a map file.
4131 void
4132 do_print_to_mapfile(Mapfile* mapfile) const
4133 { mapfile->print_output_data(this, this->name_); }
4134
4135 private:
4136 struct Local_plt_ent
4137 {
4138 Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4139 { rsym_ = rsym; u.obj_ = obj; }
4140 Local_plt_ent(Symbol* sym)
4141 { rsym_ = -1u; u.gsym_ = sym; }
4142 ~Local_plt_ent()
4143 { }
4144
4145 unsigned int rsym_;
4146 union
4147 {
4148 Sized_relobj_file<size, big_endian>* obj_;
4149 Symbol* gsym_;
4150 } u;
4151 };
4152
4153 // Return the offset of the first non-reserved PLT entry.
4154 unsigned int
4155 first_plt_entry_offset() const
4156 {
4157 // IPLT and LPLT have no reserved entry.
4158 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4159 return 0;
4160 return this->targ_->first_plt_entry_offset();
4161 }
4162
4163 // Return the size of each PLT entry.
4164 unsigned int
4165 plt_entry_size() const
4166 {
4167 return this->targ_->plt_entry_size();
4168 }
4169
4170 // Write out the PLT data.
4171 void
4172 do_write(Output_file*);
4173
4174 // The reloc section.
4175 Reloc_section* rel_;
4176 // Allows access to .glink for do_write.
4177 Target_powerpc<size, big_endian>* targ_;
4178 Symbol_table* symtab_;
4179 // What to report in map file.
4180 const char *name_;
4181
4182 std::vector<Local_plt_ent> sym_ents_;
4183 };
4184
4185 // Add an entry to the PLT.
4186
4187 template<int size, bool big_endian>
4188 void
4189 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
4190 bool is_local)
4191 {
4192 if (!gsym->has_plt_offset())
4193 {
4194 section_size_type off = this->current_data_size();
4195 if (off == 0)
4196 off += this->first_plt_entry_offset();
4197 gsym->set_plt_offset(off);
4198 if (this->rel_)
4199 {
4200 if (is_local)
4201 {
4202 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4203 if (size == 64 && this->targ_->abiversion() < 2)
4204 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4205 this->rel_->add_symbolless_global_addend(gsym, dynrel,
4206 this, off, 0);
4207 }
4208 else
4209 {
4210 gsym->set_needs_dynsym_entry();
4211 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4212 this->rel_->add_global(gsym, dynrel, this, off, 0);
4213 }
4214 }
4215 off += this->plt_entry_size();
4216 this->set_current_data_size(off);
4217 if (is_local)
4218 {
4219 Local_plt_ent sym(gsym);
4220 this->sym_ents_.push_back(sym);
4221 }
4222 }
4223 }
4224
4225 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4226
4227 template<int size, bool big_endian>
4228 void
4229 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4230 {
4231 if (!gsym->has_plt_offset())
4232 {
4233 section_size_type off = this->current_data_size();
4234 gsym->set_plt_offset(off);
4235 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4236 if (size == 64 && this->targ_->abiversion() < 2)
4237 dynrel = elfcpp::R_PPC64_JMP_IREL;
4238 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4239 off += this->plt_entry_size();
4240 this->set_current_data_size(off);
4241 }
4242 }
4243
4244 // Add an entry for a local symbol to the PLT.
4245
4246 template<int size, bool big_endian>
4247 void
4248 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4249 Sized_relobj_file<size, big_endian>* relobj,
4250 unsigned int local_sym_index)
4251 {
4252 if (!relobj->local_has_plt_offset(local_sym_index))
4253 {
4254 section_size_type off = this->current_data_size();
4255 relobj->set_local_plt_offset(local_sym_index, off);
4256 if (this->rel_)
4257 {
4258 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4259 if (size == 64 && this->targ_->abiversion() < 2)
4260 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4261 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4262 dynrel, this, off, 0);
4263 }
4264 off += this->plt_entry_size();
4265 this->set_current_data_size(off);
4266 Local_plt_ent sym(relobj, local_sym_index);
4267 this->sym_ents_.push_back(sym);
4268 }
4269 }
4270
4271 // Add an entry for a local ifunc symbol to the IPLT.
4272
4273 template<int size, bool big_endian>
4274 void
4275 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4276 Sized_relobj_file<size, big_endian>* relobj,
4277 unsigned int local_sym_index)
4278 {
4279 if (!relobj->local_has_plt_offset(local_sym_index))
4280 {
4281 section_size_type off = this->current_data_size();
4282 relobj->set_local_plt_offset(local_sym_index, off);
4283 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4284 if (size == 64 && this->targ_->abiversion() < 2)
4285 dynrel = elfcpp::R_PPC64_JMP_IREL;
4286 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4287 this, off, 0);
4288 off += this->plt_entry_size();
4289 this->set_current_data_size(off);
4290 }
4291 }
4292
4293 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4294 static const uint32_t add_2_2_11 = 0x7c425a14;
4295 static const uint32_t add_2_2_12 = 0x7c426214;
4296 static const uint32_t add_3_3_2 = 0x7c631214;
4297 static const uint32_t add_3_3_13 = 0x7c636a14;
4298 static const uint32_t add_3_12_2 = 0x7c6c1214;
4299 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4300 static const uint32_t add_11_0_11 = 0x7d605a14;
4301 static const uint32_t add_11_2_11 = 0x7d625a14;
4302 static const uint32_t add_11_11_2 = 0x7d6b1214;
4303 static const uint32_t add_12_11_12 = 0x7d8b6214;
4304 static const uint32_t addi_0_12 = 0x380c0000;
4305 static const uint32_t addi_2_2 = 0x38420000;
4306 static const uint32_t addi_3_3 = 0x38630000;
4307 static const uint32_t addi_11_11 = 0x396b0000;
4308 static const uint32_t addi_12_1 = 0x39810000;
4309 static const uint32_t addi_12_11 = 0x398b0000;
4310 static const uint32_t addi_12_12 = 0x398c0000;
4311 static const uint32_t addis_0_2 = 0x3c020000;
4312 static const uint32_t addis_0_13 = 0x3c0d0000;
4313 static const uint32_t addis_2_12 = 0x3c4c0000;
4314 static const uint32_t addis_11_2 = 0x3d620000;
4315 static const uint32_t addis_11_11 = 0x3d6b0000;
4316 static const uint32_t addis_11_30 = 0x3d7e0000;
4317 static const uint32_t addis_12_1 = 0x3d810000;
4318 static const uint32_t addis_12_2 = 0x3d820000;
4319 static const uint32_t addis_12_11 = 0x3d8b0000;
4320 static const uint32_t addis_12_12 = 0x3d8c0000;
4321 static const uint32_t b = 0x48000000;
4322 static const uint32_t bcl_20_31 = 0x429f0005;
4323 static const uint32_t bctr = 0x4e800420;
4324 static const uint32_t bctrl = 0x4e800421;
4325 static const uint32_t beqlr = 0x4d820020;
4326 static const uint32_t blr = 0x4e800020;
4327 static const uint32_t bnectr_p4 = 0x4ce20420;
4328 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4329 static const uint32_t cmpldi_2_0 = 0x28220000;
4330 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4331 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4332 static const uint32_t cror_15_15_15 = 0x4def7b82;
4333 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4334 static const uint32_t ld_0_1 = 0xe8010000;
4335 static const uint32_t ld_0_11 = 0xe80b0000;
4336 static const uint32_t ld_0_12 = 0xe80c0000;
4337 static const uint32_t ld_2_1 = 0xe8410000;
4338 static const uint32_t ld_2_2 = 0xe8420000;
4339 static const uint32_t ld_2_11 = 0xe84b0000;
4340 static const uint32_t ld_2_12 = 0xe84c0000;
4341 static const uint32_t ld_11_1 = 0xe9610000;
4342 static const uint32_t ld_11_2 = 0xe9620000;
4343 static const uint32_t ld_11_3 = 0xe9630000;
4344 static const uint32_t ld_11_11 = 0xe96b0000;
4345 static const uint32_t ld_12_2 = 0xe9820000;
4346 static const uint32_t ld_12_3 = 0xe9830000;
4347 static const uint32_t ld_12_11 = 0xe98b0000;
4348 static const uint32_t ld_12_12 = 0xe98c0000;
4349 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4350 static const uint32_t lfd_0_1 = 0xc8010000;
4351 static const uint32_t li_0_0 = 0x38000000;
4352 static const uint32_t li_11_0 = 0x39600000;
4353 static const uint32_t li_12_0 = 0x39800000;
4354 static const uint32_t lis_0 = 0x3c000000;
4355 static const uint32_t lis_2 = 0x3c400000;
4356 static const uint32_t lis_11 = 0x3d600000;
4357 static const uint32_t lis_12 = 0x3d800000;
4358 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4359 static const uint32_t lwz_0_12 = 0x800c0000;
4360 static const uint32_t lwz_11_3 = 0x81630000;
4361 static const uint32_t lwz_11_11 = 0x816b0000;
4362 static const uint32_t lwz_11_30 = 0x817e0000;
4363 static const uint32_t lwz_12_3 = 0x81830000;
4364 static const uint32_t lwz_12_12 = 0x818c0000;
4365 static const uint32_t lwzu_0_12 = 0x840c0000;
4366 static const uint32_t mflr_0 = 0x7c0802a6;
4367 static const uint32_t mflr_11 = 0x7d6802a6;
4368 static const uint32_t mflr_12 = 0x7d8802a6;
4369 static const uint32_t mr_0_3 = 0x7c601b78;
4370 static const uint32_t mr_3_0 = 0x7c030378;
4371 static const uint32_t mtctr_0 = 0x7c0903a6;
4372 static const uint32_t mtctr_11 = 0x7d6903a6;
4373 static const uint32_t mtctr_12 = 0x7d8903a6;
4374 static const uint32_t mtlr_0 = 0x7c0803a6;
4375 static const uint32_t mtlr_11 = 0x7d6803a6;
4376 static const uint32_t mtlr_12 = 0x7d8803a6;
4377 static const uint32_t nop = 0x60000000;
4378 static const uint32_t ori_0_0_0 = 0x60000000;
4379 static const uint32_t ori_11_11_0 = 0x616b0000;
4380 static const uint32_t ori_12_12_0 = 0x618c0000;
4381 static const uint32_t oris_12_12_0 = 0x658c0000;
4382 static const uint32_t sldi_11_11_34 = 0x796b1746;
4383 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4384 static const uint32_t srdi_0_0_2 = 0x7800f082;
4385 static const uint32_t std_0_1 = 0xf8010000;
4386 static const uint32_t std_0_12 = 0xf80c0000;
4387 static const uint32_t std_2_1 = 0xf8410000;
4388 static const uint32_t std_11_1 = 0xf9610000;
4389 static const uint32_t stfd_0_1 = 0xd8010000;
4390 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4391 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4392 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4393 static const uint32_t xor_2_12_12 = 0x7d826278;
4394 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4395
4396 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4397 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4398 static const uint64_t pnop = 0x0700000000000000ULL;
4399
4400 // Write out the PLT.
4401
4402 template<int size, bool big_endian>
4403 void
4404 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4405 {
4406 if (!this->sym_ents_.empty()
4407 && !parameters->options().output_is_position_independent())
4408 {
4409 const section_size_type offset = this->offset();
4410 const section_size_type oview_size
4411 = convert_to_section_size_type(this->data_size());
4412 unsigned char* const oview = of->get_output_view(offset, oview_size);
4413 unsigned char* pov = oview;
4414 unsigned char* endpov = oview + oview_size;
4415
4416 for (typename std::vector<Local_plt_ent>::iterator e
4417 = this->sym_ents_.begin();
4418 e != this->sym_ents_.end();
4419 e++)
4420 {
4421 typename elfcpp::Elf_types<size>::Elf_Addr val;
4422 Sized_symbol<size>* gsym = NULL;
4423 Powerpc_relobj<size, big_endian>* obj = NULL;
4424 if (e->rsym_ == -1u)
4425 {
4426 gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4427 val = gsym->value();
4428 }
4429 else
4430 {
4431 obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4432 val = obj->local_symbol(e->rsym_)->value(obj, 0);
4433 }
4434 if (this->targ_->abiversion() >= 2)
4435 {
4436 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4437 pov += size / 8;
4438 }
4439 else
4440 {
4441 unsigned int shndx;
4442 this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4443 &val, &shndx);
4444 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4445 pov += size / 8;
4446 val = this->targ_->toc_pointer();
4447 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4448 pov += size / 8;
4449 if (this->plt_entry_size() > 16)
4450 {
4451 elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4452 pov += size / 8;
4453 }
4454 }
4455 }
4456 gold_assert(pov == endpov);
4457 }
4458
4459 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4460 {
4461 const section_size_type offset = this->offset();
4462 const section_size_type oview_size
4463 = convert_to_section_size_type(this->data_size());
4464 unsigned char* const oview = of->get_output_view(offset, oview_size);
4465 unsigned char* pov = oview;
4466 unsigned char* endpov = oview + oview_size;
4467
4468 // The address of the .glink branch table
4469 const Output_data_glink<size, big_endian>* glink
4470 = this->targ_->glink_section();
4471 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4472
4473 while (pov < endpov)
4474 {
4475 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4476 pov += 4;
4477 branch_tab += 4;
4478 }
4479
4480 of->write_output_view(offset, oview_size, oview);
4481 }
4482 }
4483
4484 // Create the PLT section.
4485
4486 template<int size, bool big_endian>
4487 void
4488 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4489 Layout* layout)
4490 {
4491 if (this->plt_ == NULL)
4492 {
4493 if (this->got_ == NULL)
4494 this->got_section(symtab, layout, GOT_TYPE_SMALL);
4495
4496 if (this->glink_ == NULL)
4497 make_glink_section(layout);
4498
4499 // Ensure that .rela.dyn always appears before .rela.plt This is
4500 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4501 // needs to include .rela.plt in its range.
4502 this->rela_dyn_section(layout);
4503
4504 Reloc_section* plt_rel = new Reloc_section(false);
4505 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4506 elfcpp::SHF_ALLOC, plt_rel,
4507 ORDER_DYNAMIC_PLT_RELOCS, false);
4508 this->plt_
4509 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
4510 "** PLT");
4511 layout->add_output_section_data(".plt",
4512 (size == 32
4513 ? elfcpp::SHT_PROGBITS
4514 : elfcpp::SHT_NOBITS),
4515 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4516 this->plt_,
4517 (size == 32
4518 ? ORDER_SMALL_DATA
4519 : ORDER_SMALL_BSS),
4520 false);
4521
4522 Output_section* rela_plt_os = plt_rel->output_section();
4523 rela_plt_os->set_info_section(this->plt_->output_section());
4524 }
4525 }
4526
4527 // Create the IPLT section.
4528
4529 template<int size, bool big_endian>
4530 void
4531 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4532 Layout* layout)
4533 {
4534 if (this->iplt_ == NULL)
4535 {
4536 this->make_plt_section(symtab, layout);
4537 this->make_lplt_section(symtab, layout);
4538
4539 Reloc_section* iplt_rel = new Reloc_section(false);
4540 if (this->rela_dyn_->output_section())
4541 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4542 this->iplt_
4543 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
4544 "** IPLT");
4545 if (this->plt_->output_section())
4546 this->plt_->output_section()->add_output_section_data(this->iplt_);
4547 }
4548 }
4549
4550 // Create the LPLT section.
4551
4552 template<int size, bool big_endian>
4553 void
4554 Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4555 Layout* layout)
4556 {
4557 if (this->lplt_ == NULL)
4558 {
4559 Reloc_section* lplt_rel = NULL;
4560 if (parameters->options().output_is_position_independent())
4561 lplt_rel = this->rela_dyn_section(layout);
4562 this->lplt_
4563 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
4564 "** LPLT");
4565 this->make_brlt_section(layout);
4566 if (this->brlt_section_ && this->brlt_section_->output_section())
4567 this->brlt_section_->output_section()
4568 ->add_output_section_data(this->lplt_);
4569 else
4570 layout->add_output_section_data(".branch_lt",
4571 elfcpp::SHT_PROGBITS,
4572 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4573 this->lplt_,
4574 ORDER_RELRO,
4575 true);
4576 }
4577 }
4578
4579 // A section for huge long branch addresses, similar to plt section.
4580
4581 template<int size, bool big_endian>
4582 class Output_data_brlt_powerpc : public Output_section_data_build
4583 {
4584 public:
4585 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4586 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4587 size, big_endian> Reloc_section;
4588
4589 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ)
4590 : Output_section_data_build(size == 32 ? 4 : 8),
4591 targ_(targ)
4592 { }
4593
4594 protected:
4595 void
4596 do_adjust_output_section(Output_section* os)
4597 {
4598 os->set_entsize(0);
4599 }
4600
4601 // Write to a map file.
4602 void
4603 do_print_to_mapfile(Mapfile* mapfile) const
4604 { mapfile->print_output_data(this, "** BRLT"); }
4605
4606 private:
4607 // Write out the BRLT data.
4608 void
4609 do_write(Output_file*);
4610
4611 Target_powerpc<size, big_endian>* targ_;
4612 };
4613
4614 // Make the branch lookup table section.
4615
4616 template<int size, bool big_endian>
4617 void
4618 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4619 {
4620 if (size == 64 && this->brlt_section_ == NULL)
4621 {
4622 bool is_pic = parameters->options().output_is_position_independent();
4623 if (is_pic)
4624 {
4625 // When PIC we can't fill in .branch_lt but must initialise at
4626 // runtime via dynamic relocations.
4627 this->rela_dyn_section(layout);
4628 }
4629 this->brlt_section_
4630 = new Output_data_brlt_powerpc<size, big_endian>(this);
4631 if (this->plt_ && is_pic && this->plt_->output_section())
4632 this->plt_->output_section()
4633 ->add_output_section_data(this->brlt_section_);
4634 else
4635 layout->add_output_section_data(".branch_lt",
4636 elfcpp::SHT_PROGBITS,
4637 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4638 this->brlt_section_,
4639 ORDER_RELRO,
4640 true);
4641 }
4642 }
4643
4644 // Write out .branch_lt when non-PIC.
4645
4646 template<int size, bool big_endian>
4647 void
4648 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4649 {
4650 if (size == 64 && !parameters->options().output_is_position_independent())
4651 {
4652 const section_size_type offset = this->offset();
4653 const section_size_type oview_size
4654 = convert_to_section_size_type(this->data_size());
4655 unsigned char* const oview = of->get_output_view(offset, oview_size);
4656
4657 this->targ_->write_branch_lookup_table(oview);
4658 of->write_output_view(offset, oview_size, oview);
4659 }
4660 }
4661
4662 static inline uint32_t
4663 l(uint32_t a)
4664 {
4665 return a & 0xffff;
4666 }
4667
4668 static inline uint32_t
4669 hi(uint32_t a)
4670 {
4671 return l(a >> 16);
4672 }
4673
4674 static inline uint32_t
4675 ha(uint32_t a)
4676 {
4677 return hi(a + 0x8000);
4678 }
4679
4680 static inline uint64_t
4681 d34(uint64_t v)
4682 {
4683 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4684 }
4685
4686 static inline uint64_t
4687 ha34(uint64_t v)
4688 {
4689 return (v + (1ULL << 33)) >> 34;
4690 }
4691
4692 template<int size>
4693 struct Eh_cie
4694 {
4695 static const unsigned char eh_frame_cie[12];
4696 };
4697
4698 template<int size>
4699 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4700 {
4701 1, // CIE version.
4702 'z', 'R', 0, // Augmentation string.
4703 4, // Code alignment.
4704 0x80 - size / 8 , // Data alignment.
4705 65, // RA reg.
4706 1, // Augmentation size.
4707 (elfcpp::DW_EH_PE_pcrel
4708 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4709 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4710 };
4711
4712 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4713 static const unsigned char glink_eh_frame_fde_64v1[] =
4714 {
4715 0, 0, 0, 0, // Replaced with offset to .glink.
4716 0, 0, 0, 0, // Replaced with size of .glink.
4717 0, // Augmentation size.
4718 elfcpp::DW_CFA_advance_loc + 2,
4719 elfcpp::DW_CFA_register, 65, 12,
4720 elfcpp::DW_CFA_advance_loc + 4,
4721 elfcpp::DW_CFA_restore_extended, 65
4722 };
4723
4724 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4725 static const unsigned char glink_eh_frame_fde_64v2[] =
4726 {
4727 0, 0, 0, 0, // Replaced with offset to .glink.
4728 0, 0, 0, 0, // Replaced with size of .glink.
4729 0, // Augmentation size.
4730 elfcpp::DW_CFA_advance_loc + 2,
4731 elfcpp::DW_CFA_register, 65, 0,
4732 elfcpp::DW_CFA_advance_loc + 2,
4733 elfcpp::DW_CFA_restore_extended, 65
4734 };
4735
4736 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4737 {
4738 0, 0, 0, 0, // Replaced with offset to .glink.
4739 0, 0, 0, 0, // Replaced with size of .glink.
4740 0, // Augmentation size.
4741 elfcpp::DW_CFA_advance_loc + 3,
4742 elfcpp::DW_CFA_register, 65, 0,
4743 elfcpp::DW_CFA_advance_loc + 2,
4744 elfcpp::DW_CFA_restore_extended, 65
4745 };
4746
4747 // Describe __glink_PLTresolve use of LR, 32-bit version.
4748 static const unsigned char glink_eh_frame_fde_32[] =
4749 {
4750 0, 0, 0, 0, // Replaced with offset to .glink.
4751 0, 0, 0, 0, // Replaced with size of .glink.
4752 0, // Augmentation size.
4753 elfcpp::DW_CFA_advance_loc + 2,
4754 elfcpp::DW_CFA_register, 65, 0,
4755 elfcpp::DW_CFA_advance_loc + 4,
4756 elfcpp::DW_CFA_restore_extended, 65
4757 };
4758
4759 static const unsigned char default_fde[] =
4760 {
4761 0, 0, 0, 0, // Replaced with offset to stubs.
4762 0, 0, 0, 0, // Replaced with size of stubs.
4763 0, // Augmentation size.
4764 elfcpp::DW_CFA_nop, // Pad.
4765 elfcpp::DW_CFA_nop,
4766 elfcpp::DW_CFA_nop
4767 };
4768
4769 template<bool big_endian>
4770 static inline void
4771 write_insn(unsigned char* p, uint32_t v)
4772 {
4773 elfcpp::Swap<32, big_endian>::writeval(p, v);
4774 }
4775
4776 template<int size>
4777 static inline unsigned int
4778 param_plt_align()
4779 {
4780 if (!parameters->options().user_set_plt_align())
4781 return size == 64 ? 32 : 8;
4782 return 1 << parameters->options().plt_align();
4783 }
4784
4785 // Stub_table holds information about plt and long branch stubs.
4786 // Stubs are built in an area following some input section determined
4787 // by group_sections(). This input section is converted to a relaxed
4788 // input section allowing it to be resized to accommodate the stubs
4789
4790 template<int size, bool big_endian>
4791 class Stub_table : public Output_relaxed_input_section
4792 {
4793 public:
4794 struct Plt_stub_ent
4795 {
4796 Plt_stub_ent(unsigned int off, unsigned int indx)
4797 : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4798 toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
4799 { }
4800
4801 unsigned int off_;
4802 unsigned int indx_;
4803 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4804 // toc stub, p9off_ is offset to p9notoc stub
4805 unsigned int tocoff_ : 8;
4806 unsigned int p9off_ : 8;
4807 // The size of the toc stub, used to locate blr on tls_get_addr stub.
4808 unsigned int tsize_ : 8;
4809 // Stub revision management
4810 unsigned int iter_ : 1;
4811 // The three types of stubs.
4812 unsigned int toc_ : 1;
4813 unsigned int notoc_ : 1;
4814 unsigned int p9notoc_ : 1;
4815 // Each with a possible variant saving r2 first
4816 unsigned int r2save_ : 1;
4817 // Handy cached info from symbol
4818 unsigned int localentry0_ : 1;
4819 };
4820 struct Branch_stub_ent
4821 {
4822 Branch_stub_ent(unsigned int off)
4823 : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4824 p9notoc_(0), save_res_(0), other_(0)
4825 { }
4826
4827 unsigned int off_;
4828 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4829 // toc stub, p9off_ is offset to p9notoc stub
4830 unsigned int tocoff_ : 8;
4831 unsigned int p9off_ : 8;
4832 // Stub revision management
4833 unsigned int iter_ : 1;
4834 // Four types of stubs.
4835 unsigned int toc_ : 1;
4836 unsigned int notoc_ : 1;
4837 unsigned int p9notoc_ : 1;
4838 unsigned int save_res_ : 1;
4839 // Handy cached info from symbol
4840 unsigned int other_ : 3;
4841 };
4842 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4843 static const Address invalid_address = static_cast<Address>(0) - 1;
4844
4845 Stub_table(Target_powerpc<size, big_endian>* targ,
4846 Output_section* output_section,
4847 const Output_section::Input_section* owner,
4848 uint32_t id)
4849 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4850 owner->relobj()
4851 ->section_addralign(owner->shndx())),
4852 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4853 orig_data_size_(owner->current_data_size()),
4854 plt_size_(0), last_plt_size_(0),
4855 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4856 need_save_res_(false), need_resize_(false), resizing_(false),
4857 uniq_(id)
4858 {
4859 this->set_output_section(output_section);
4860
4861 std::vector<Output_relaxed_input_section*> new_relaxed;
4862 new_relaxed.push_back(this);
4863 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4864 }
4865
4866 // Add a plt call stub.
4867 bool
4868 add_plt_call_entry(Address,
4869 const Sized_relobj_file<size, big_endian>*,
4870 const Symbol*,
4871 unsigned int,
4872 Address,
4873 bool);
4874
4875 bool
4876 add_plt_call_entry(Address,
4877 const Sized_relobj_file<size, big_endian>*,
4878 unsigned int,
4879 unsigned int,
4880 Address,
4881 bool);
4882
4883 // Find a given plt call stub.
4884 const Plt_stub_ent*
4885 find_plt_call_entry(const Symbol*) const;
4886
4887 const Plt_stub_ent*
4888 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4889 unsigned int) const;
4890
4891 const Plt_stub_ent*
4892 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4893 const Symbol*,
4894 unsigned int,
4895 Address) const;
4896
4897 const Plt_stub_ent*
4898 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4899 unsigned int,
4900 unsigned int,
4901 Address) const;
4902
4903 // Add a long branch stub.
4904 bool
4905 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
4906
4907 const Branch_stub_ent*
4908 find_long_branch_entry(Address) const;
4909
4910 bool
4911 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4912 {
4913 Address max_branch_offset = max_branch_delta<size>(r_type);
4914 if (max_branch_offset == 0)
4915 return true;
4916 gold_assert(from != invalid_address);
4917 Address loc = off + this->stub_address();
4918 return loc - from + max_branch_offset < 2 * max_branch_offset;
4919 }
4920
4921 void
4922 clear_stubs(bool all)
4923 {
4924 this->plt_call_stubs_.clear();
4925 this->plt_size_ = 0;
4926 this->long_branch_stubs_.clear();
4927 this->branch_size_ = 0;
4928 this->need_save_res_ = false;
4929 if (all)
4930 {
4931 this->last_plt_size_ = 0;
4932 this->last_branch_size_ = 0;
4933 }
4934 }
4935
4936 bool
4937 need_resize() const
4938 { return need_resize_; }
4939
4940 void
4941 set_resizing(bool val)
4942 {
4943 this->resizing_ = val;
4944 if (val)
4945 {
4946 this->need_resize_ = false;
4947 this->plt_size_ = 0;
4948 this->branch_size_ = 0;
4949 this->need_save_res_ = false;
4950 }
4951 }
4952
4953 Address
4954 set_address_and_size(const Output_section* os, Address off)
4955 {
4956 Address start_off = off;
4957 off += this->orig_data_size_;
4958 Address my_size = this->plt_size_ + this->branch_size_;
4959 if (this->need_save_res_)
4960 my_size += this->targ_->savres_section()->data_size();
4961 if (my_size != 0)
4962 off = align_address(off, this->stub_align());
4963 // Include original section size and alignment padding in size
4964 my_size += off - start_off;
4965 // Ensure new size is always larger than min size
4966 // threshold. Alignment requirement is included in "my_size", so
4967 // increase "my_size" does not invalidate alignment.
4968 if (my_size < this->min_size_threshold_)
4969 my_size = this->min_size_threshold_;
4970 this->reset_address_and_file_offset();
4971 this->set_current_data_size(my_size);
4972 this->set_address_and_file_offset(os->address() + start_off,
4973 os->offset() + start_off);
4974 return my_size;
4975 }
4976
4977 Address
4978 stub_address() const
4979 {
4980 return align_address(this->address() + this->orig_data_size_,
4981 this->stub_align());
4982 }
4983
4984 Address
4985 stub_offset() const
4986 {
4987 return align_address(this->offset() + this->orig_data_size_,
4988 this->stub_align());
4989 }
4990
4991 section_size_type
4992 plt_size() const
4993 { return this->plt_size_; }
4994
4995 section_size_type
4996 branch_size() const
4997 { return this->branch_size_; }
4998
4999 void
5000 set_min_size_threshold(Address min_size)
5001 { this->min_size_threshold_ = min_size; }
5002
5003 void
5004 define_stub_syms(Symbol_table*);
5005
5006 bool
5007 size_update()
5008 {
5009 Output_section* os = this->output_section();
5010 if (os->addralign() < this->stub_align())
5011 {
5012 os->set_addralign(this->stub_align());
5013 // FIXME: get rid of the insane checkpointing.
5014 // We can't increase alignment of the input section to which
5015 // stubs are attached; The input section may be .init which
5016 // is pasted together with other .init sections to form a
5017 // function. Aligning might insert zero padding resulting in
5018 // sigill. However we do need to increase alignment of the
5019 // output section so that the align_address() on offset in
5020 // set_address_and_size() adds the same padding as the
5021 // align_address() on address in stub_address().
5022 // What's more, we need this alignment for the layout done in
5023 // relaxation_loop_body() so that the output section starts at
5024 // a suitably aligned address.
5025 os->checkpoint_set_addralign(this->stub_align());
5026 }
5027 if (this->last_plt_size_ != this->plt_size_
5028 || this->last_branch_size_ != this->branch_size_)
5029 {
5030 this->last_plt_size_ = this->plt_size_;
5031 this->last_branch_size_ = this->branch_size_;
5032 return true;
5033 }
5034 return false;
5035 }
5036
5037 // Add .eh_frame info for this stub section.
5038 void
5039 add_eh_frame(Layout* layout);
5040
5041 // Remove .eh_frame info for this stub section.
5042 void
5043 remove_eh_frame(Layout* layout);
5044
5045 Target_powerpc<size, big_endian>*
5046 targ() const
5047 { return targ_; }
5048
5049 private:
5050 class Plt_stub_key;
5051 class Plt_stub_key_hash;
5052 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5053 Plt_stub_key_hash> Plt_stub_entries;
5054 class Branch_stub_key;
5055 class Branch_stub_key_hash;
5056 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5057 Branch_stub_key_hash> Branch_stub_entries;
5058
5059 // Alignment of stub section.
5060 unsigned int
5061 stub_align() const
5062 {
5063 unsigned int min_align = size == 64 ? 32 : 16;
5064 unsigned int user_align = 1 << parameters->options().plt_align();
5065 return std::max(user_align, min_align);
5066 }
5067
5068 // Return the plt offset for the given call stub.
5069 Address
5070 plt_off(typename Plt_stub_entries::const_iterator p,
5071 const Output_data_plt_powerpc<size, big_endian>** sec) const
5072 {
5073 const Symbol* gsym = p->first.sym_;
5074 if (gsym != NULL)
5075 return this->targ_->plt_off(gsym, sec);
5076 else
5077 {
5078 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5079 unsigned int local_sym_index = p->first.locsym_;
5080 return this->targ_->plt_off(relobj, local_sym_index, sec);
5081 }
5082 }
5083
5084 // Size of a given plt call stub.
5085 unsigned int
5086 plt_call_size(typename Plt_stub_entries::iterator p) const;
5087
5088 unsigned int
5089 plt_call_align(unsigned int bytes) const
5090 {
5091 unsigned int align = param_plt_align<size>();
5092 return (bytes + align - 1) & -align;
5093 }
5094
5095 // Return long branch stub size.
5096 unsigned int
5097 branch_stub_size(typename Branch_stub_entries::iterator p,
5098 bool* need_lt);
5099
5100 void
5101 build_tls_opt_head(unsigned char** pp, bool save_lr);
5102
5103 void
5104 build_tls_opt_tail(unsigned char* p);
5105
5106 void
5107 plt_error(const Plt_stub_key& p);
5108
5109 // Write out stubs.
5110 void
5111 do_write(Output_file*);
5112
5113 // Plt call stub keys.
5114 class Plt_stub_key
5115 {
5116 public:
5117 Plt_stub_key(const Symbol* sym)
5118 : sym_(sym), object_(0), addend_(0), locsym_(0)
5119 { }
5120
5121 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5122 unsigned int locsym_index)
5123 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5124 { }
5125
5126 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5127 const Symbol* sym,
5128 unsigned int r_type,
5129 Address addend)
5130 : sym_(sym), object_(0), addend_(0), locsym_(0)
5131 {
5132 if (size != 32)
5133 this->addend_ = addend;
5134 else if (parameters->options().output_is_position_independent()
5135 && (r_type == elfcpp::R_PPC_PLTREL24
5136 || r_type == elfcpp::R_POWERPC_PLTCALL))
5137 {
5138 this->addend_ = addend;
5139 if (this->addend_ >= 32768)
5140 this->object_ = object;
5141 }
5142 }
5143
5144 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5145 unsigned int locsym_index,
5146 unsigned int r_type,
5147 Address addend)
5148 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5149 {
5150 if (size != 32)
5151 this->addend_ = addend;
5152 else if (parameters->options().output_is_position_independent()
5153 && (r_type == elfcpp::R_PPC_PLTREL24
5154 || r_type == elfcpp::R_POWERPC_PLTCALL))
5155 this->addend_ = addend;
5156 }
5157
5158 bool operator==(const Plt_stub_key& that) const
5159 {
5160 return (this->sym_ == that.sym_
5161 && this->object_ == that.object_
5162 && this->addend_ == that.addend_
5163 && this->locsym_ == that.locsym_);
5164 }
5165
5166 const Symbol* sym_;
5167 const Sized_relobj_file<size, big_endian>* object_;
5168 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5169 unsigned int locsym_;
5170 };
5171
5172 class Plt_stub_key_hash
5173 {
5174 public:
5175 size_t operator()(const Plt_stub_key& ent) const
5176 {
5177 return (reinterpret_cast<uintptr_t>(ent.sym_)
5178 ^ reinterpret_cast<uintptr_t>(ent.object_)
5179 ^ ent.addend_
5180 ^ ent.locsym_);
5181 }
5182 };
5183
5184 // Long branch stub keys.
5185 class Branch_stub_key
5186 {
5187 public:
5188 Branch_stub_key(Address to)
5189 : dest_(to)
5190 { }
5191
5192 bool operator==(const Branch_stub_key& that) const
5193 {
5194 return this->dest_ == that.dest_;
5195 }
5196
5197 Address dest_;
5198 };
5199
5200 class Branch_stub_key_hash
5201 {
5202 public:
5203 size_t operator()(const Branch_stub_key& key) const
5204 { return key.dest_; }
5205 };
5206
5207 // In a sane world this would be a global.
5208 Target_powerpc<size, big_endian>* targ_;
5209 // Map sym/object/addend to stub offset.
5210 Plt_stub_entries plt_call_stubs_;
5211 // Map destination address to stub offset.
5212 Branch_stub_entries long_branch_stubs_;
5213 // size of input section
5214 section_size_type orig_data_size_;
5215 // size of stubs
5216 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5217 // Some rare cases cause (PR/20529) fluctuation in stub table
5218 // size, which leads to an endless relax loop. This is to be fixed
5219 // by, after the first few iterations, allowing only increase of
5220 // stub table size. This variable sets the minimal possible size of
5221 // a stub table, it is zero for the first few iterations, then
5222 // increases monotonically.
5223 Address min_size_threshold_;
5224 // Set if this stub group needs a copy of out-of-line register
5225 // save/restore functions.
5226 bool need_save_res_;
5227 // Set when notoc_/r2save_ changes after sizing a stub
5228 bool need_resize_;
5229 // Set when resizing stubs
5230 bool resizing_;
5231 // Per stub table unique identifier.
5232 uint32_t uniq_;
5233 };
5234
5235 // Add a plt call stub, if we do not already have one for this
5236 // sym/object/addend combo.
5237
5238 template<int size, bool big_endian>
5239 bool
5240 Stub_table<size, big_endian>::add_plt_call_entry(
5241 Address from,
5242 const Sized_relobj_file<size, big_endian>* object,
5243 const Symbol* gsym,
5244 unsigned int r_type,
5245 Address addend,
5246 bool tocsave)
5247 {
5248 Plt_stub_key key(object, gsym, r_type, addend);
5249 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5250 std::pair<typename Plt_stub_entries::iterator, bool> p
5251 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5252 if (size == 64)
5253 {
5254 if (p.second
5255 && this->targ_->is_elfv2_localentry0(gsym))
5256 {
5257 p.first->second.localentry0_ = 1;
5258 this->targ_->set_has_localentry0();
5259 }
5260 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5261 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5262 {
5263 if (this->targ_->power10_stubs()
5264 && (!this->targ_->power10_stubs_auto()
5265 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5266 {
5267 if (!p.second && !p.first->second.notoc_)
5268 this->need_resize_ = true;
5269 p.first->second.notoc_ = 1;
5270 }
5271 else
5272 {
5273 if (!p.second && !p.first->second.p9notoc_)
5274 this->need_resize_ = true;
5275 p.first->second.p9notoc_ = 1;
5276 }
5277 }
5278 else
5279 {
5280 if (!p.second && !p.first->second.toc_)
5281 this->need_resize_ = true;
5282 p.first->second.toc_ = 1;
5283 if (!tocsave && !p.first->second.localentry0_)
5284 {
5285 if (!p.second && !p.first->second.r2save_)
5286 this->need_resize_ = true;
5287 p.first->second.r2save_ = 1;
5288 }
5289 }
5290 }
5291 if (p.second || (this->resizing_ && !p.first->second.iter_))
5292 {
5293 if (this->resizing_)
5294 {
5295 p.first->second.iter_ = 1;
5296 p.first->second.off_ = this->plt_size_;
5297 }
5298 this->plt_size_ += this->plt_call_size(p.first);
5299 if (this->targ_->is_tls_get_addr_opt(gsym))
5300 this->targ_->set_has_tls_get_addr_opt();
5301 }
5302 return this->can_reach_stub(from, p.first->second.off_, r_type);
5303 }
5304
5305 template<int size, bool big_endian>
5306 bool
5307 Stub_table<size, big_endian>::add_plt_call_entry(
5308 Address from,
5309 const Sized_relobj_file<size, big_endian>* object,
5310 unsigned int locsym_index,
5311 unsigned int r_type,
5312 Address addend,
5313 bool tocsave)
5314 {
5315 Plt_stub_key key(object, locsym_index, r_type, addend);
5316 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5317 std::pair<typename Plt_stub_entries::iterator, bool> p
5318 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5319 if (size == 64)
5320 {
5321 if (p.second
5322 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5323 {
5324 p.first->second.localentry0_ = 1;
5325 this->targ_->set_has_localentry0();
5326 }
5327 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5328 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5329 {
5330 if (this->targ_->power10_stubs()
5331 && (!this->targ_->power10_stubs_auto()
5332 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5333 {
5334 if (!p.second && !p.first->second.notoc_)
5335 this->need_resize_ = true;
5336 p.first->second.notoc_ = 1;
5337 }
5338 else
5339 {
5340 if (!p.second && !p.first->second.p9notoc_)
5341 this->need_resize_ = true;
5342 p.first->second.p9notoc_ = 1;
5343 }
5344 }
5345 else
5346 {
5347 if (!p.second && !p.first->second.toc_)
5348 this->need_resize_ = true;
5349 p.first->second.toc_ = 1;
5350 if (!tocsave && !p.first->second.localentry0_)
5351 {
5352 if (!p.second && !p.first->second.r2save_)
5353 this->need_resize_ = true;
5354 p.first->second.r2save_ = 1;
5355 }
5356 }
5357 }
5358 if (p.second || (this->resizing_ && !p.first->second.iter_))
5359 {
5360 if (this->resizing_)
5361 {
5362 p.first->second.iter_ = 1;
5363 p.first->second.off_ = this->plt_size_;
5364 }
5365 this->plt_size_ += this->plt_call_size(p.first);
5366 }
5367 return this->can_reach_stub(from, p.first->second.off_, r_type);
5368 }
5369
5370 // Find a plt call stub.
5371
5372 template<int size, bool big_endian>
5373 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5374 Stub_table<size, big_endian>::find_plt_call_entry(
5375 const Sized_relobj_file<size, big_endian>* object,
5376 const Symbol* gsym,
5377 unsigned int r_type,
5378 Address addend) const
5379 {
5380 Plt_stub_key key(object, gsym, r_type, addend);
5381 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5382 if (p == this->plt_call_stubs_.end())
5383 return NULL;
5384 return &p->second;
5385 }
5386
5387 template<int size, bool big_endian>
5388 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5389 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5390 {
5391 Plt_stub_key key(gsym);
5392 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5393 if (p == this->plt_call_stubs_.end())
5394 return NULL;
5395 return &p->second;
5396 }
5397
5398 template<int size, bool big_endian>
5399 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5400 Stub_table<size, big_endian>::find_plt_call_entry(
5401 const Sized_relobj_file<size, big_endian>* object,
5402 unsigned int locsym_index,
5403 unsigned int r_type,
5404 Address addend) const
5405 {
5406 Plt_stub_key key(object, locsym_index, r_type, addend);
5407 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5408 if (p == this->plt_call_stubs_.end())
5409 return NULL;
5410 return &p->second;
5411 }
5412
5413 template<int size, bool big_endian>
5414 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5415 Stub_table<size, big_endian>::find_plt_call_entry(
5416 const Sized_relobj_file<size, big_endian>* object,
5417 unsigned int locsym_index) const
5418 {
5419 Plt_stub_key key(object, locsym_index);
5420 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5421 if (p == this->plt_call_stubs_.end())
5422 return NULL;
5423 return &p->second;
5424 }
5425
5426 // Add a long branch stub if we don't already have one to given
5427 // destination.
5428
5429 template<int size, bool big_endian>
5430 bool
5431 Stub_table<size, big_endian>::add_long_branch_entry(
5432 unsigned int r_type,
5433 Address from,
5434 Address to,
5435 unsigned int other,
5436 bool save_res)
5437 {
5438 Branch_stub_key key(to);
5439 Branch_stub_ent ent(this->branch_size_);
5440 std::pair<typename Branch_stub_entries::iterator, bool> p
5441 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5442 if (save_res)
5443 {
5444 if (!p.second && !p.first->second.save_res_)
5445 this->need_resize_ = true;
5446 p.first->second.save_res_ = true;
5447 }
5448 else if (size == 64
5449 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5450 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5451 {
5452 if (this->targ_->power10_stubs()
5453 && (!this->targ_->power10_stubs_auto()
5454 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5455 {
5456 if (!p.second && !p.first->second.notoc_)
5457 this->need_resize_ = true;
5458 p.first->second.notoc_ = true;
5459 }
5460 else
5461 {
5462 if (!p.second && !p.first->second.p9notoc_)
5463 this->need_resize_ = true;
5464 p.first->second.p9notoc_ = true;
5465 }
5466 }
5467 else
5468 {
5469 if (!p.second && !p.first->second.toc_)
5470 this->need_resize_ = true;
5471 p.first->second.toc_ = true;
5472 }
5473 if (size == 64 && p.first->second.other_ == 0)
5474 p.first->second.other_ = other;
5475 if (p.second || (this->resizing_ && !p.first->second.iter_))
5476 {
5477 if (this->resizing_)
5478 {
5479 p.first->second.iter_ = 1;
5480 p.first->second.off_ = this->branch_size_;
5481 }
5482 if (save_res)
5483 this->need_save_res_ = true;
5484 else
5485 {
5486 bool need_lt = false;
5487 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5488 this->branch_size_ += stub_size;
5489 if (size == 64 && need_lt)
5490 this->targ_->add_branch_lookup_table(to);
5491 }
5492 }
5493 return this->can_reach_stub(from, p.first->second.off_, r_type);
5494 }
5495
5496 // Find long branch stub offset.
5497
5498 template<int size, bool big_endian>
5499 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5500 Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
5501 {
5502 Branch_stub_key key(to);
5503 typename Branch_stub_entries::const_iterator p
5504 = this->long_branch_stubs_.find(key);
5505 if (p == this->long_branch_stubs_.end())
5506 return NULL;
5507 return &p->second;
5508 }
5509
5510 template<bool big_endian>
5511 static void
5512 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5513 {
5514 delta /= 4;
5515 if (delta < 64)
5516 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5517 else if (delta < 256)
5518 {
5519 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5520 fde.push_back(delta);
5521 }
5522 else if (delta < 65536)
5523 {
5524 fde.resize(fde.size() + 3);
5525 unsigned char *p = &*fde.end() - 3;
5526 *p++ = elfcpp::DW_CFA_advance_loc2;
5527 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5528 }
5529 else
5530 {
5531 fde.resize(fde.size() + 5);
5532 unsigned char *p = &*fde.end() - 5;
5533 *p++ = elfcpp::DW_CFA_advance_loc4;
5534 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5535 }
5536 }
5537
5538 template<typename T>
5539 static bool
5540 stub_sort(T s1, T s2)
5541 {
5542 return s1->second.off_ < s2->second.off_;
5543 }
5544
5545 // Add .eh_frame info for this stub section. Unlike other linker
5546 // generated .eh_frame this is added late in the link, because we
5547 // only want the .eh_frame info if this particular stub section is
5548 // non-empty.
5549
5550 template<int size, bool big_endian>
5551 void
5552 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5553 {
5554 if (size != 64
5555 || !parameters->options().ld_generated_unwind_info())
5556 return;
5557
5558 // Since we add stub .eh_frame info late, it must be placed
5559 // after all other linker generated .eh_frame info so that
5560 // merge mapping need not be updated for input sections.
5561 // There is no provision to use a different CIE to that used
5562 // by .glink.
5563 if (!this->targ_->has_glink())
5564 return;
5565
5566 typedef typename Plt_stub_entries::iterator plt_iter;
5567 std::vector<plt_iter> calls;
5568 if (!this->plt_call_stubs_.empty())
5569 for (plt_iter cs = this->plt_call_stubs_.begin();
5570 cs != this->plt_call_stubs_.end();
5571 ++cs)
5572 if (cs->second.p9notoc_
5573 || (cs->second.toc_
5574 && cs->second.r2save_
5575 && !cs->second.localentry0_
5576 && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
5577 calls.push_back(cs);
5578 if (calls.size() > 1)
5579 std::stable_sort(calls.begin(), calls.end(),
5580 stub_sort<plt_iter>);
5581
5582 typedef typename Branch_stub_entries::const_iterator branch_iter;
5583 std::vector<branch_iter> branches;
5584 if (!this->long_branch_stubs_.empty()
5585 && !this->targ_->power10_stubs())
5586 for (branch_iter bs = this->long_branch_stubs_.begin();
5587 bs != this->long_branch_stubs_.end();
5588 ++bs)
5589 if (bs->second.notoc_)
5590 branches.push_back(bs);
5591 if (branches.size() > 1)
5592 std::stable_sort(branches.begin(), branches.end(),
5593 stub_sort<branch_iter>);
5594
5595 if (calls.empty() && branches.empty())
5596 return;
5597
5598 unsigned int last_eh_loc = 0;
5599 // offset pcrel sdata4, size udata4, and augmentation size byte.
5600 std::vector<unsigned char> fde(9, 0);
5601
5602 for (unsigned int i = 0; i < calls.size(); i++)
5603 {
5604 plt_iter cs = calls[i];
5605 unsigned int off = cs->second.off_;
5606 // The __tls_get_addr_opt call stub needs to describe where
5607 // it saves LR, to support exceptions that might be thrown
5608 // from __tls_get_addr, and to support asynchronous exceptions.
5609 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5610 {
5611 off += 7 * 4;
5612 if (cs->second.toc_
5613 && cs->second.r2save_
5614 && !cs->second.localentry0_)
5615 {
5616 off += cs->second.tocoff_ + 2 * 4;
5617 eh_advance<big_endian>(fde, off - last_eh_loc);
5618 fde.resize(fde.size() + 6);
5619 unsigned char* p = &*fde.end() - 6;
5620 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5621 *p++ = 65;
5622 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5623 unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
5624 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5625 *p++ = elfcpp::DW_CFA_restore_extended;
5626 *p++ = 65;
5627 last_eh_loc = off + delta;
5628 off = cs->second.off_ + 7 * 4;
5629 }
5630 }
5631 // notoc stubs also should describe LR changes, to support
5632 // asynchronous exceptions.
5633 if (cs->second.p9notoc_)
5634 {
5635 off += cs->second.p9off_;
5636 off += (cs->second.r2save_ ? 4 : 0) + 8;
5637 eh_advance<big_endian>(fde, off - last_eh_loc);
5638 fde.resize(fde.size() + 6);
5639 unsigned char* p = &*fde.end() - 6;
5640 *p++ = elfcpp::DW_CFA_register;
5641 *p++ = 65;
5642 *p++ = 12;
5643 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5644 *p++ = elfcpp::DW_CFA_restore_extended;
5645 *p++ = 65;
5646 last_eh_loc = off + 8;
5647 }
5648 }
5649
5650 for (unsigned int i = 0; i < branches.size(); i++)
5651 {
5652 branch_iter bs = branches[i];
5653 unsigned int off = bs->second.off_ + 8;
5654 eh_advance<big_endian>(fde, off - last_eh_loc);
5655 fde.resize(fde.size() + 6);
5656 unsigned char* p = &*fde.end() - 6;
5657 *p++ = elfcpp::DW_CFA_register;
5658 *p++ = 65;
5659 *p++ = 12;
5660 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5661 *p++ = elfcpp::DW_CFA_restore_extended;
5662 *p++ = 65;
5663 last_eh_loc = off + 8;
5664 }
5665
5666 layout->add_eh_frame_for_plt(this,
5667 Eh_cie<size>::eh_frame_cie,
5668 sizeof (Eh_cie<size>::eh_frame_cie),
5669 &*fde.begin(), fde.size());
5670 }
5671
5672 template<int size, bool big_endian>
5673 void
5674 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5675 {
5676 if (size == 64
5677 && parameters->options().ld_generated_unwind_info()
5678 && this->targ_->has_glink())
5679 layout->remove_eh_frame_for_plt(this,
5680 Eh_cie<size>::eh_frame_cie,
5681 sizeof (Eh_cie<size>::eh_frame_cie));
5682 }
5683
5684 // A class to handle .glink.
5685
5686 template<int size, bool big_endian>
5687 class Output_data_glink : public Output_section_data
5688 {
5689 public:
5690 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5691 static const Address invalid_address = static_cast<Address>(0) - 1;
5692
5693 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5694 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5695 end_branch_table_(), ge_size_(0)
5696 { }
5697
5698 void
5699 add_eh_frame(Layout* layout);
5700
5701 void
5702 add_global_entry(const Symbol*);
5703
5704 Address
5705 find_global_entry(const Symbol*) const;
5706
5707 unsigned int
5708 global_entry_align(unsigned int off) const
5709 {
5710 unsigned int align = param_plt_align<size>();
5711 return (off + align - 1) & -align;
5712 }
5713
5714 unsigned int
5715 global_entry_off() const
5716 {
5717 return this->global_entry_align(this->end_branch_table_);
5718 }
5719
5720 Address
5721 global_entry_address() const
5722 {
5723 gold_assert(this->is_data_size_valid());
5724 return this->address() + this->global_entry_off();
5725 }
5726
5727 int
5728 pltresolve_size() const
5729 {
5730 if (size == 64)
5731 return (8
5732 + (this->targ_->abiversion() < 2 ? 11 * 4
5733 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5734 return 16 * 4;
5735 }
5736
5737 protected:
5738 // Write to a map file.
5739 void
5740 do_print_to_mapfile(Mapfile* mapfile) const
5741 { mapfile->print_output_data(this, _("** glink")); }
5742
5743 private:
5744 void
5745 set_final_data_size();
5746
5747 // Write out .glink
5748 void
5749 do_write(Output_file*);
5750
5751 // Allows access to .got and .plt for do_write.
5752 Target_powerpc<size, big_endian>* targ_;
5753
5754 // Map sym to stub offset.
5755 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5756 Global_entry_stub_entries global_entry_stubs_;
5757
5758 unsigned int end_branch_table_, ge_size_;
5759 };
5760
5761 template<int size, bool big_endian>
5762 void
5763 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5764 {
5765 if (!parameters->options().ld_generated_unwind_info())
5766 return;
5767
5768 if (size == 64)
5769 {
5770 if (this->targ_->abiversion() < 2)
5771 layout->add_eh_frame_for_plt(this,
5772 Eh_cie<64>::eh_frame_cie,
5773 sizeof (Eh_cie<64>::eh_frame_cie),
5774 glink_eh_frame_fde_64v1,
5775 sizeof (glink_eh_frame_fde_64v1));
5776 else if (this->targ_->has_localentry0())
5777 layout->add_eh_frame_for_plt(this,
5778 Eh_cie<64>::eh_frame_cie,
5779 sizeof (Eh_cie<64>::eh_frame_cie),
5780 glink_eh_frame_fde_64v2_localentry0,
5781 sizeof (glink_eh_frame_fde_64v2));
5782 else
5783 layout->add_eh_frame_for_plt(this,
5784 Eh_cie<64>::eh_frame_cie,
5785 sizeof (Eh_cie<64>::eh_frame_cie),
5786 glink_eh_frame_fde_64v2,
5787 sizeof (glink_eh_frame_fde_64v2));
5788 }
5789 else
5790 {
5791 // 32-bit .glink can use the default since the CIE return
5792 // address reg, LR, is valid.
5793 layout->add_eh_frame_for_plt(this,
5794 Eh_cie<32>::eh_frame_cie,
5795 sizeof (Eh_cie<32>::eh_frame_cie),
5796 default_fde,
5797 sizeof (default_fde));
5798 // Except where LR is used in a PIC __glink_PLTresolve.
5799 if (parameters->options().output_is_position_independent())
5800 layout->add_eh_frame_for_plt(this,
5801 Eh_cie<32>::eh_frame_cie,
5802 sizeof (Eh_cie<32>::eh_frame_cie),
5803 glink_eh_frame_fde_32,
5804 sizeof (glink_eh_frame_fde_32));
5805 }
5806 }
5807
5808 template<int size, bool big_endian>
5809 void
5810 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5811 {
5812 unsigned int off = this->global_entry_align(this->ge_size_);
5813 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5814 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5815 if (p.second)
5816 this->ge_size_ = off + 16;
5817 }
5818
5819 template<int size, bool big_endian>
5820 typename Output_data_glink<size, big_endian>::Address
5821 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5822 {
5823 typename Global_entry_stub_entries::const_iterator p
5824 = this->global_entry_stubs_.find(gsym);
5825 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5826 }
5827
5828 template<int size, bool big_endian>
5829 void
5830 Output_data_glink<size, big_endian>::set_final_data_size()
5831 {
5832 unsigned int count = this->targ_->plt_entry_count();
5833 section_size_type total = 0;
5834
5835 if (count != 0)
5836 {
5837 if (size == 32)
5838 {
5839 // space for branch table
5840 total += 4 * (count - 1);
5841
5842 total += -total & 15;
5843 total += this->pltresolve_size();
5844 }
5845 else
5846 {
5847 total += this->pltresolve_size();
5848
5849 // space for branch table
5850 total += 4 * count;
5851 if (this->targ_->abiversion() < 2)
5852 {
5853 total += 4 * count;
5854 if (count > 0x8000)
5855 total += 4 * (count - 0x8000);
5856 }
5857 }
5858 }
5859 this->end_branch_table_ = total;
5860 total = this->global_entry_align(total);
5861 total += this->ge_size_;
5862
5863 this->set_data_size(total);
5864 }
5865
5866 // Define symbols on stubs, identifying the stub.
5867
5868 template<int size, bool big_endian>
5869 void
5870 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5871 {
5872 if (!this->plt_call_stubs_.empty())
5873 {
5874 // The key for the plt call stub hash table includes addresses,
5875 // therefore traversal order depends on those addresses, which
5876 // can change between runs if gold is a PIE. Unfortunately the
5877 // output .symtab ordering depends on the order in which symbols
5878 // are added to the linker symtab. We want reproducible output
5879 // so must sort the call stub symbols.
5880 typedef typename Plt_stub_entries::iterator plt_iter;
5881 std::vector<plt_iter> sorted;
5882 sorted.resize(this->plt_call_stubs_.size());
5883
5884 for (plt_iter cs = this->plt_call_stubs_.begin();
5885 cs != this->plt_call_stubs_.end();
5886 ++cs)
5887 sorted[cs->second.indx_] = cs;
5888
5889 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5890 {
5891 plt_iter cs = sorted[i];
5892 char add[10];
5893 add[0] = 0;
5894 if (cs->first.addend_ != 0)
5895 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5896 char obj[10];
5897 obj[0] = 0;
5898 if (cs->first.object_)
5899 {
5900 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5901 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5902 sprintf(obj, "%x:", ppcobj->uniq());
5903 }
5904 char localname[9];
5905 const char *symname;
5906 if (cs->first.sym_ == NULL)
5907 {
5908 sprintf(localname, "%x", cs->first.locsym_);
5909 symname = localname;
5910 }
5911 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5912 symname = this->targ_->tls_get_addr_opt()->name();
5913 else
5914 symname = cs->first.sym_->name();
5915 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5916 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5917 Address value
5918 = this->stub_address() - this->address() + cs->second.off_;
5919 unsigned int stub_size = this->plt_call_size(cs);
5920 this->targ_->define_local(symtab, name, this, value, stub_size);
5921 }
5922 }
5923
5924 typedef typename Branch_stub_entries::iterator branch_iter;
5925 for (branch_iter bs = this->long_branch_stubs_.begin();
5926 bs != this->long_branch_stubs_.end();
5927 ++bs)
5928 {
5929 if (bs->second.save_res_)
5930 continue;
5931
5932 char* name = new char[8 + 13 + 16 + 1];
5933 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5934 static_cast<unsigned long long>(bs->first.dest_));
5935 Address value = (this->stub_address() - this->address()
5936 + this->plt_size_ + bs->second.off_);
5937 bool need_lt = false;
5938 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5939 this->targ_->define_local(symtab, name, this, value, stub_size);
5940 }
5941 }
5942
5943 // Emit the start of a __tls_get_addr_opt plt call stub.
5944
5945 template<int size, bool big_endian>
5946 void
5947 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5948 bool save_lr)
5949 {
5950 unsigned char* p = *pp;
5951 if (size == 64)
5952 {
5953 write_insn<big_endian>(p, ld_11_3 + 0);
5954 p += 4;
5955 write_insn<big_endian>(p, ld_12_3 + 8);
5956 p += 4;
5957 write_insn<big_endian>(p, mr_0_3);
5958 p += 4;
5959 write_insn<big_endian>(p, cmpdi_11_0);
5960 p += 4;
5961 write_insn<big_endian>(p, add_3_12_13);
5962 p += 4;
5963 write_insn<big_endian>(p, beqlr);
5964 p += 4;
5965 write_insn<big_endian>(p, mr_3_0);
5966 p += 4;
5967 if (save_lr)
5968 {
5969 write_insn<big_endian>(p, mflr_11);
5970 p += 4;
5971 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5972 p += 4;
5973 }
5974 }
5975 else
5976 {
5977 write_insn<big_endian>(p, lwz_11_3 + 0);
5978 p += 4;
5979 write_insn<big_endian>(p, lwz_12_3 + 4);
5980 p += 4;
5981 write_insn<big_endian>(p, mr_0_3);
5982 p += 4;
5983 write_insn<big_endian>(p, cmpwi_11_0);
5984 p += 4;
5985 write_insn<big_endian>(p, add_3_12_2);
5986 p += 4;
5987 write_insn<big_endian>(p, beqlr);
5988 p += 4;
5989 write_insn<big_endian>(p, mr_3_0);
5990 p += 4;
5991 write_insn<big_endian>(p, nop);
5992 p += 4;
5993 }
5994 *pp = p;
5995 }
5996
5997 // Emit the tail of a __tls_get_addr_opt plt call stub.
5998
5999 template<int size, bool big_endian>
6000 void
6001 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
6002 {
6003 write_insn<big_endian>(p, bctrl);
6004 p += 4;
6005 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
6006 p += 4;
6007 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
6008 p += 4;
6009 write_insn<big_endian>(p, mtlr_11);
6010 p += 4;
6011 write_insn<big_endian>(p, blr);
6012 }
6013
6014 // Emit pc-relative plt call stub code.
6015
6016 template<bool big_endian>
6017 static unsigned char*
6018 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
6019 {
6020 uint64_t insn;
6021 if (off - odd + (1ULL << 33) < 1ULL << 34)
6022 {
6023 off -= odd;
6024 if (odd)
6025 {
6026 write_insn<big_endian>(p, nop);
6027 p += 4;
6028 }
6029 if (load)
6030 insn = pld_12_pc;
6031 else
6032 insn = paddi_12_pc;
6033 insn |= d34(off);
6034 write_insn<big_endian>(p, insn >> 32);
6035 p += 4;
6036 write_insn<big_endian>(p, insn & 0xffffffff);
6037 }
6038 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6039 {
6040 off -= 8 - odd;
6041 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6042 p += 4;
6043 if (!odd)
6044 {
6045 write_insn<big_endian>(p, sldi_11_11_34);
6046 p += 4;
6047 }
6048 insn = paddi_12_pc | d34(off);
6049 write_insn<big_endian>(p, insn >> 32);
6050 p += 4;
6051 write_insn<big_endian>(p, insn & 0xffffffff);
6052 p += 4;
6053 if (odd)
6054 {
6055 write_insn<big_endian>(p, sldi_11_11_34);
6056 p += 4;
6057 }
6058 if (load)
6059 write_insn<big_endian>(p, ldx_12_11_12);
6060 else
6061 write_insn<big_endian>(p, add_12_11_12);
6062 }
6063 else
6064 {
6065 off -= odd + 8;
6066 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6067 p += 4;
6068 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6069 p += 4;
6070 if (odd)
6071 {
6072 write_insn<big_endian>(p, sldi_11_11_34);
6073 p += 4;
6074 }
6075 insn = paddi_12_pc | d34(off);
6076 write_insn<big_endian>(p, insn >> 32);
6077 p += 4;
6078 write_insn<big_endian>(p, insn & 0xffffffff);
6079 p += 4;
6080 if (!odd)
6081 {
6082 write_insn<big_endian>(p, sldi_11_11_34);
6083 p += 4;
6084 }
6085 if (load)
6086 write_insn<big_endian>(p, ldx_12_11_12);
6087 else
6088 write_insn<big_endian>(p, add_12_11_12);
6089 }
6090 p += 4;
6091 return p;
6092 }
6093
6094 // Gets the address of a label (1:) in r11 and builds an offset in r12,
6095 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6096 // mflr %r12
6097 // bcl 20,31,1f
6098 // 1: mflr %r11
6099 // mtlr %r12
6100 // lis %r12,xxx-1b@highest
6101 // ori %r12,%r12,xxx-1b@higher
6102 // sldi %r12,%r12,32
6103 // oris %r12,%r12,xxx-1b@high
6104 // ori %r12,%r12,xxx-1b@l
6105 // add/ldx %r12,%r11,%r12
6106
6107 template<bool big_endian>
6108 static unsigned char*
6109 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6110 {
6111 write_insn<big_endian>(p, mflr_12);
6112 p += 4;
6113 write_insn<big_endian>(p, bcl_20_31);
6114 p += 4;
6115 write_insn<big_endian>(p, mflr_11);
6116 p += 4;
6117 write_insn<big_endian>(p, mtlr_12);
6118 p += 4;
6119 if (off + 0x8000 < 0x10000)
6120 {
6121 if (load)
6122 write_insn<big_endian>(p, ld_12_11 + l(off));
6123 else
6124 write_insn<big_endian>(p, addi_12_11 + l(off));
6125 }
6126 else if (off + 0x80008000ULL < 0x100000000ULL)
6127 {
6128 write_insn<big_endian>(p, addis_12_11 + ha(off));
6129 p += 4;
6130 if (load)
6131 write_insn<big_endian>(p, ld_12_12 + l(off));
6132 else
6133 write_insn<big_endian>(p, addi_12_12 + l(off));
6134 }
6135 else
6136 {
6137 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6138 {
6139 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6140 p += 4;
6141 }
6142 else
6143 {
6144 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6145 p += 4;
6146 if (((off >> 32) & 0xffff) != 0)
6147 {
6148 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6149 p += 4;
6150 }
6151 }
6152 if (((off >> 32) & 0xffffffffULL) != 0)
6153 {
6154 write_insn<big_endian>(p, sldi_12_12_32);
6155 p += 4;
6156 }
6157 if (hi(off) != 0)
6158 {
6159 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6160 p += 4;
6161 }
6162 if (l(off) != 0)
6163 {
6164 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6165 p += 4;
6166 }
6167 if (load)
6168 write_insn<big_endian>(p, ldx_12_11_12);
6169 else
6170 write_insn<big_endian>(p, add_12_11_12);
6171 }
6172 p += 4;
6173 return p;
6174 }
6175
6176 // Size of a given plt call stub.
6177
6178 template<int size, bool big_endian>
6179 unsigned int
6180 Stub_table<size, big_endian>::plt_call_size(
6181 typename Plt_stub_entries::iterator p) const
6182 {
6183 if (size == 32)
6184 {
6185 unsigned int bytes = 4 * 4;
6186 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6187 bytes = 12 * 4;
6188 return this->plt_call_align(bytes);
6189 }
6190
6191 const Output_data_plt_powerpc<size, big_endian>* plt;
6192 uint64_t plt_addr = this->plt_off(p, &plt);
6193 plt_addr += plt->address();
6194 if (this->targ_->power10_stubs()
6195 && this->targ_->power10_stubs_auto())
6196 {
6197 unsigned int bytes = 0;
6198 if (p->second.notoc_)
6199 {
6200 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6201 bytes = 7 * 4;
6202 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6203 uint64_t odd = from & 4;
6204 uint64_t off = plt_addr - from;
6205 if (off - odd + (1ULL << 33) < 1ULL << 34)
6206 bytes += odd + 4 * 4;
6207 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6208 bytes += 7 * 4;
6209 else
6210 bytes += 8 * 4;
6211 bytes = this->plt_call_align(bytes);
6212 }
6213 if (p->second.toc_)
6214 {
6215 p->second.tocoff_ = bytes;
6216 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6217 {
6218 bytes += 7 * 4;
6219 if (p->second.r2save_ && !p->second.localentry0_)
6220 bytes += 2 * 4 + 4 * 4;
6221 }
6222 if (p->second.r2save_)
6223 bytes += 4;
6224 uint64_t got_addr = this->targ_->toc_pointer();
6225 uint64_t off = plt_addr - got_addr;
6226 bytes += 3 * 4 + 4 * (ha(off) != 0);
6227 p->second.tsize_ = bytes - p->second.tocoff_;
6228 bytes = this->plt_call_align(bytes);
6229 }
6230 if (p->second.p9notoc_)
6231 {
6232 p->second.p9off_ = bytes;
6233 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6234 bytes += 7 * 4;
6235 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6236 uint64_t off = plt_addr - from;
6237 if (off + 0x8000 < 0x10000)
6238 bytes += 7 * 4;
6239 else if (off + 0x80008000ULL < 0x100000000ULL)
6240 bytes += 8 * 4;
6241 else
6242 {
6243 bytes += 8 * 4;
6244 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6245 && ((off >> 32) & 0xffff) != 0)
6246 bytes += 4;
6247 if (((off >> 32) & 0xffffffffULL) != 0)
6248 bytes += 4;
6249 if (hi(off) != 0)
6250 bytes += 4;
6251 if (l(off) != 0)
6252 bytes += 4;
6253 }
6254 bytes = this->plt_call_align(bytes);
6255 }
6256 return bytes;
6257 }
6258 else
6259 {
6260 unsigned int bytes = 0;
6261 unsigned int tail = 0;
6262 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6263 {
6264 bytes = 7 * 4;
6265 if (p->second.r2save_ && !p->second.localentry0_)
6266 {
6267 bytes = 9 * 4;
6268 tail = 4 * 4;
6269 }
6270 }
6271
6272 if (p->second.r2save_)
6273 bytes += 4;
6274
6275 if (this->targ_->power10_stubs())
6276 {
6277 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6278 uint64_t odd = from & 4;
6279 uint64_t off = plt_addr - from;
6280 if (off - odd + (1ULL << 33) < 1ULL << 34)
6281 bytes += odd + 4 * 4;
6282 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6283 bytes += 7 * 4;
6284 else
6285 bytes += 8 * 4;
6286 return this->plt_call_align(bytes + tail);
6287 }
6288
6289 if (p->second.p9notoc_)
6290 {
6291 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6292 uint64_t off = plt_addr - from;
6293 if (off + 0x8000 < 0x10000)
6294 bytes += 7 * 4;
6295 else if (off + 0x80008000ULL < 0x100000000ULL)
6296 bytes += 8 * 4;
6297 else
6298 {
6299 bytes += 8 * 4;
6300 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6301 && ((off >> 32) & 0xffff) != 0)
6302 bytes += 4;
6303 if (((off >> 32) & 0xffffffffULL) != 0)
6304 bytes += 4;
6305 if (hi(off) != 0)
6306 bytes += 4;
6307 if (l(off) != 0)
6308 bytes += 4;
6309 }
6310 return this->plt_call_align(bytes + tail);
6311 }
6312
6313 uint64_t got_addr = this->targ_->toc_pointer();
6314 uint64_t off = plt_addr - got_addr;
6315 bytes += 3 * 4 + 4 * (ha(off) != 0);
6316 if (this->targ_->abiversion() < 2)
6317 {
6318 bool static_chain = parameters->options().plt_static_chain();
6319 bool thread_safe = this->targ_->plt_thread_safe();
6320 bytes += (4
6321 + 4 * static_chain
6322 + 8 * thread_safe
6323 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6324 }
6325 return this->plt_call_align(bytes + tail);
6326 }
6327 }
6328
6329 // Return long branch stub size.
6330
6331 template<int size, bool big_endian>
6332 unsigned int
6333 Stub_table<size, big_endian>::branch_stub_size(
6334 typename Branch_stub_entries::iterator p,
6335 bool* need_lt)
6336 {
6337 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6338 if (size == 32)
6339 {
6340 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6341 return 4;
6342 if (parameters->options().output_is_position_independent())
6343 return 32;
6344 return 16;
6345 }
6346
6347 uint64_t off = p->first.dest_ - loc;
6348 unsigned int bytes = 0;
6349 if (p->second.notoc_)
6350 {
6351 if (this->targ_->power10_stubs())
6352 {
6353 Address odd = loc & 4;
6354 if (off + (1 << 25) < 2 << 25)
6355 bytes = odd + 12;
6356 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6357 bytes = odd + 16;
6358 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6359 bytes = 28;
6360 else
6361 bytes = 32;
6362 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6363 return bytes;
6364 p->second.tocoff_ = bytes;
6365 }
6366 else
6367 {
6368 off -= 8;
6369 if (off + 0x8000 < 0x10000)
6370 return 24;
6371 if (off + 0x80008000ULL < 0x100000000ULL)
6372 {
6373 if (off + 24 + (1 << 25) < 2 << 25)
6374 return 28;
6375 return 32;
6376 }
6377
6378 bytes = 32;
6379 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6380 && ((off >> 32) & 0xffff) != 0)
6381 bytes += 4;
6382 if (((off >> 32) & 0xffffffffULL) != 0)
6383 bytes += 4;
6384 if (hi(off) != 0)
6385 bytes += 4;
6386 if (l(off) != 0)
6387 bytes += 4;
6388 return bytes;
6389 }
6390 }
6391
6392 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6393 if (off + (1 << 25) < 2 << 25)
6394 return bytes + 4;
6395 if (!this->targ_->power10_stubs()
6396 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6397 *need_lt = true;
6398 return bytes + 16;
6399 }
6400
6401 template<int size, bool big_endian>
6402 void
6403 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6404 {
6405 if (p.sym_)
6406 gold_error(_("linkage table error against `%s'"),
6407 p.sym_->demangled_name().c_str());
6408 else
6409 gold_error(_("linkage table error against `%s:[local %u]'"),
6410 p.object_->name().c_str(),
6411 p.locsym_);
6412 }
6413
6414 // Write out plt and long branch stub code.
6415
6416 template<int size, bool big_endian>
6417 void
6418 Stub_table<size, big_endian>::do_write(Output_file* of)
6419 {
6420 if (this->plt_call_stubs_.empty()
6421 && this->long_branch_stubs_.empty())
6422 return;
6423
6424 const section_size_type start_off = this->offset();
6425 const section_size_type off = this->stub_offset();
6426 const section_size_type oview_size =
6427 convert_to_section_size_type(this->data_size() - (off - start_off));
6428 unsigned char* const oview = of->get_output_view(off, oview_size);
6429 unsigned char* p;
6430
6431 if (size == 64
6432 && this->targ_->power10_stubs())
6433 {
6434 if (!this->plt_call_stubs_.empty())
6435 {
6436 // Write out plt call stubs.
6437 typename Plt_stub_entries::const_iterator cs;
6438 for (cs = this->plt_call_stubs_.begin();
6439 cs != this->plt_call_stubs_.end();
6440 ++cs)
6441 {
6442 p = oview + cs->second.off_;
6443 const Output_data_plt_powerpc<size, big_endian>* plt;
6444 Address pltoff = this->plt_off(cs, &plt);
6445 Address plt_addr = pltoff + plt->address();
6446 if (this->targ_->power10_stubs_auto())
6447 {
6448 if (cs->second.notoc_)
6449 {
6450 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6451 this->build_tls_opt_head(&p, false);
6452 Address from = this->stub_address() + (p - oview);
6453 Address delta = plt_addr - from;
6454 p = build_power10_offset<big_endian>(p, delta, from & 4,
6455 true);
6456 write_insn<big_endian>(p, mtctr_12);
6457 p += 4;
6458 write_insn<big_endian>(p, bctr);
6459 p += 4;
6460 p = oview + this->plt_call_align(p - oview);
6461 }
6462 if (cs->second.toc_)
6463 {
6464 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6465 {
6466 bool save_lr
6467 = cs->second.r2save_ && !cs->second.localentry0_;
6468 this->build_tls_opt_head(&p, save_lr);
6469 }
6470 Address got_addr = this->targ_->toc_pointer();
6471 Address off = plt_addr - got_addr;
6472
6473 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6474 this->plt_error(cs->first);
6475
6476 if (cs->second.r2save_)
6477 {
6478 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6479 p += 4;
6480 }
6481 if (ha(off) != 0)
6482 {
6483 write_insn<big_endian>(p, addis_12_2 + ha(off));
6484 p += 4;
6485 write_insn<big_endian>(p, ld_12_12 + l(off));
6486 p += 4;
6487 }
6488 else
6489 {
6490 write_insn<big_endian>(p, ld_12_2 + l(off));
6491 p += 4;
6492 }
6493 write_insn<big_endian>(p, mtctr_12);
6494 p += 4;
6495 if (cs->second.r2save_
6496 && !cs->second.localentry0_
6497 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6498 this->build_tls_opt_tail(p);
6499 else
6500 write_insn<big_endian>(p, bctr);
6501 }
6502 if (cs->second.p9notoc_)
6503 {
6504 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6505 this->build_tls_opt_head(&p, false);
6506 Address from = this->stub_address() + (p - oview);
6507 Address delta = plt_addr - from;
6508 p = build_notoc_offset<big_endian>(p, delta, true);
6509 write_insn<big_endian>(p, mtctr_12);
6510 p += 4;
6511 write_insn<big_endian>(p, bctr);
6512 p += 4;
6513 p = oview + this->plt_call_align(p - oview);
6514 }
6515 }
6516 else
6517 {
6518 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6519 {
6520 bool save_lr
6521 = cs->second.r2save_ && !cs->second.localentry0_;
6522 this->build_tls_opt_head(&p, save_lr);
6523 }
6524 if (cs->second.r2save_)
6525 {
6526 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6527 p += 4;
6528 }
6529 Address from = this->stub_address() + (p - oview);
6530 Address delta = plt_addr - from;
6531 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6532 write_insn<big_endian>(p, mtctr_12);
6533 p += 4;
6534 if (cs->second.r2save_
6535 && !cs->second.localentry0_
6536 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6537 this->build_tls_opt_tail(p);
6538 else
6539 write_insn<big_endian>(p, bctr);
6540 }
6541 }
6542 }
6543
6544 // Write out long branch stubs.
6545 typename Branch_stub_entries::const_iterator bs;
6546 for (bs = this->long_branch_stubs_.begin();
6547 bs != this->long_branch_stubs_.end();
6548 ++bs)
6549 {
6550 if (bs->second.save_res_)
6551 continue;
6552 Address off = this->plt_size_ + bs->second.off_;
6553 p = oview + off;
6554 Address loc = this->stub_address() + off;
6555 Address delta = bs->first.dest_ - loc;
6556 if (this->targ_->power10_stubs_auto())
6557 {
6558 if (bs->second.notoc_)
6559 {
6560 unsigned char* startp = p;
6561 p = build_power10_offset<big_endian>(p, delta,
6562 loc & 4, false);
6563 delta -= p - startp;
6564 startp = p;
6565 if (delta + (1 << 25) < 2 << 25)
6566 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6567 else
6568 {
6569 write_insn<big_endian>(p, mtctr_12);
6570 p += 4;
6571 write_insn<big_endian>(p, bctr);
6572 }
6573 p += 4;
6574 delta -= p - startp;
6575 }
6576 if (bs->second.toc_)
6577 {
6578 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6579 if (delta + (1 << 25) >= 2 << 25)
6580 {
6581 Address brlt_addr
6582 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6583 gold_assert(brlt_addr != invalid_address);
6584 brlt_addr += this->targ_->brlt_section()->address();
6585 Address got_addr = this->targ_->toc_pointer();
6586 Address brltoff = brlt_addr - got_addr;
6587 if (ha(brltoff) == 0)
6588 {
6589 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6590 p += 4;
6591 }
6592 else
6593 {
6594 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6595 p += 4;
6596 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6597 p += 4;
6598 }
6599 }
6600 if (delta + (1 << 25) < 2 << 25)
6601 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6602 else
6603 {
6604 write_insn<big_endian>(p, mtctr_12);
6605 p += 4;
6606 write_insn<big_endian>(p, bctr);
6607 }
6608 }
6609 if (bs->second.p9notoc_)
6610 {
6611 unsigned char* startp = p;
6612 p = build_notoc_offset<big_endian>(p, delta, false);
6613 delta -= p - startp;
6614 startp = p;
6615 if (delta + (1 << 25) < 2 << 25)
6616 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6617 else
6618 {
6619 write_insn<big_endian>(p, mtctr_12);
6620 p += 4;
6621 write_insn<big_endian>(p, bctr);
6622 }
6623 p += 4;
6624 delta -= p - startp;
6625 }
6626 }
6627 else
6628 {
6629 if (!bs->second.notoc_)
6630 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6631 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6632 {
6633 unsigned char* startp = p;
6634 p = build_power10_offset<big_endian>(p, delta,
6635 loc & 4, false);
6636 delta -= p - startp;
6637 }
6638 if (delta + (1 << 25) < 2 << 25)
6639 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6640 else
6641 {
6642 write_insn<big_endian>(p, mtctr_12);
6643 p += 4;
6644 write_insn<big_endian>(p, bctr);
6645 }
6646 }
6647 }
6648 }
6649 else if (size == 64)
6650 {
6651
6652 if (!this->plt_call_stubs_.empty()
6653 && this->targ_->abiversion() >= 2)
6654 {
6655 // Write out plt call stubs for ELFv2.
6656 typename Plt_stub_entries::const_iterator cs;
6657 for (cs = this->plt_call_stubs_.begin();
6658 cs != this->plt_call_stubs_.end();
6659 ++cs)
6660 {
6661 const Output_data_plt_powerpc<size, big_endian>* plt;
6662 Address pltoff = this->plt_off(cs, &plt);
6663 Address plt_addr = pltoff + plt->address();
6664
6665 p = oview + cs->second.off_;
6666 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6667 {
6668 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6669 this->build_tls_opt_head(&p, save_lr);
6670 }
6671 if (cs->second.r2save_)
6672 {
6673 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6674 p += 4;
6675 }
6676 if (cs->second.p9notoc_)
6677 {
6678 Address from = this->stub_address() + (p - oview) + 8;
6679 Address off = plt_addr - from;
6680 p = build_notoc_offset<big_endian>(p, off, true);
6681 }
6682 else
6683 {
6684 Address got_addr = this->targ_->toc_pointer();
6685 Address off = plt_addr - got_addr;
6686
6687 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6688 this->plt_error(cs->first);
6689
6690 if (ha(off) != 0)
6691 {
6692 write_insn<big_endian>(p, addis_12_2 + ha(off));
6693 p += 4;
6694 write_insn<big_endian>(p, ld_12_12 + l(off));
6695 p += 4;
6696 }
6697 else
6698 {
6699 write_insn<big_endian>(p, ld_12_2 + l(off));
6700 p += 4;
6701 }
6702 }
6703 write_insn<big_endian>(p, mtctr_12);
6704 p += 4;
6705 if (cs->second.r2save_
6706 && !cs->second.localentry0_
6707 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6708 this->build_tls_opt_tail(p);
6709 else
6710 write_insn<big_endian>(p, bctr);
6711 }
6712 }
6713 else if (!this->plt_call_stubs_.empty())
6714 {
6715 // Write out plt call stubs for ELFv1.
6716 typename Plt_stub_entries::const_iterator cs;
6717 for (cs = this->plt_call_stubs_.begin();
6718 cs != this->plt_call_stubs_.end();
6719 ++cs)
6720 {
6721 const Output_data_plt_powerpc<size, big_endian>* plt;
6722 Address pltoff = this->plt_off(cs, &plt);
6723 Address plt_addr = pltoff + plt->address();
6724 Address got_addr = this->targ_->toc_pointer();
6725 Address off = plt_addr - got_addr;
6726
6727 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6728 || cs->second.notoc_)
6729 this->plt_error(cs->first);
6730
6731 bool static_chain = parameters->options().plt_static_chain();
6732 bool thread_safe = this->targ_->plt_thread_safe();
6733 bool use_fake_dep = false;
6734 Address cmp_branch_off = 0;
6735 if (thread_safe)
6736 {
6737 unsigned int pltindex
6738 = ((pltoff - this->targ_->first_plt_entry_offset())
6739 / this->targ_->plt_entry_size());
6740 Address glinkoff
6741 = (this->targ_->glink_section()->pltresolve_size()
6742 + pltindex * 8);
6743 if (pltindex > 32768)
6744 glinkoff += (pltindex - 32768) * 4;
6745 Address to
6746 = this->targ_->glink_section()->address() + glinkoff;
6747 Address from
6748 = (this->stub_address() + cs->second.off_ + 20
6749 + 4 * cs->second.r2save_
6750 + 4 * (ha(off) != 0)
6751 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6752 + 4 * static_chain);
6753 cmp_branch_off = to - from;
6754 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6755 }
6756
6757 p = oview + cs->second.off_;
6758 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6759 {
6760 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6761 this->build_tls_opt_head(&p, save_lr);
6762 use_fake_dep = thread_safe;
6763 }
6764 if (cs->second.r2save_)
6765 {
6766 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6767 p += 4;
6768 }
6769 if (ha(off) != 0)
6770 {
6771 write_insn<big_endian>(p, addis_11_2 + ha(off));
6772 p += 4;
6773 write_insn<big_endian>(p, ld_12_11 + l(off));
6774 p += 4;
6775 if (ha(off + 8 + 8 * static_chain) != ha(off))
6776 {
6777 write_insn<big_endian>(p, addi_11_11 + l(off));
6778 p += 4;
6779 off = 0;
6780 }
6781 write_insn<big_endian>(p, mtctr_12);
6782 p += 4;
6783 if (use_fake_dep)
6784 {
6785 write_insn<big_endian>(p, xor_2_12_12);
6786 p += 4;
6787 write_insn<big_endian>(p, add_11_11_2);
6788 p += 4;
6789 }
6790 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6791 p += 4;
6792 if (static_chain)
6793 {
6794 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6795 p += 4;
6796 }
6797 }
6798 else
6799 {
6800 write_insn<big_endian>(p, ld_12_2 + l(off));
6801 p += 4;
6802 if (ha(off + 8 + 8 * static_chain) != ha(off))
6803 {
6804 write_insn<big_endian>(p, addi_2_2 + l(off));
6805 p += 4;
6806 off = 0;
6807 }
6808 write_insn<big_endian>(p, mtctr_12);
6809 p += 4;
6810 if (use_fake_dep)
6811 {
6812 write_insn<big_endian>(p, xor_11_12_12);
6813 p += 4;
6814 write_insn<big_endian>(p, add_2_2_11);
6815 p += 4;
6816 }
6817 if (static_chain)
6818 {
6819 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6820 p += 4;
6821 }
6822 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6823 p += 4;
6824 }
6825 if (cs->second.r2save_
6826 && !cs->second.localentry0_
6827 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6828 this->build_tls_opt_tail(p);
6829 else if (thread_safe && !use_fake_dep)
6830 {
6831 write_insn<big_endian>(p, cmpldi_2_0);
6832 p += 4;
6833 write_insn<big_endian>(p, bnectr_p4);
6834 p += 4;
6835 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6836 }
6837 else
6838 write_insn<big_endian>(p, bctr);
6839 }
6840 }
6841
6842 // Write out long branch stubs.
6843 typename Branch_stub_entries::const_iterator bs;
6844 for (bs = this->long_branch_stubs_.begin();
6845 bs != this->long_branch_stubs_.end();
6846 ++bs)
6847 {
6848 if (bs->second.save_res_)
6849 continue;
6850 Address off = this->plt_size_ + bs->second.off_;
6851 p = oview + off;
6852 Address loc = this->stub_address() + off;
6853 Address delta = bs->first.dest_ - loc;
6854 if (!bs->second.p9notoc_)
6855 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6856 if (bs->second.p9notoc_)
6857 {
6858 unsigned char* startp = p;
6859 p = build_notoc_offset<big_endian>(p, off, false);
6860 delta -= p - startp;
6861 }
6862 else if (delta + (1 << 25) >= 2 << 25)
6863 {
6864 Address brlt_addr
6865 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6866 gold_assert(brlt_addr != invalid_address);
6867 brlt_addr += this->targ_->brlt_section()->address();
6868 Address got_addr = this->targ_->toc_pointer();
6869 Address brltoff = brlt_addr - got_addr;
6870 if (ha(brltoff) == 0)
6871 {
6872 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6873 p += 4;
6874 }
6875 else
6876 {
6877 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6878 p += 4;
6879 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6880 p += 4;
6881 }
6882 }
6883 if (delta + (1 << 25) < 2 << 25)
6884 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6885 else
6886 {
6887 write_insn<big_endian>(p, mtctr_12);
6888 p += 4;
6889 write_insn<big_endian>(p, bctr);
6890 }
6891 }
6892 }
6893 else // size == 32
6894 {
6895 if (!this->plt_call_stubs_.empty())
6896 {
6897 // The address of _GLOBAL_OFFSET_TABLE_.
6898 Address g_o_t = invalid_address;
6899
6900 // Write out plt call stubs.
6901 typename Plt_stub_entries::const_iterator cs;
6902 for (cs = this->plt_call_stubs_.begin();
6903 cs != this->plt_call_stubs_.end();
6904 ++cs)
6905 {
6906 const Output_data_plt_powerpc<size, big_endian>* plt;
6907 Address plt_addr = this->plt_off(cs, &plt);
6908 plt_addr += plt->address();
6909
6910 p = oview + cs->second.off_;
6911 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6912 this->build_tls_opt_head(&p, false);
6913 if (parameters->options().output_is_position_independent())
6914 {
6915 Address got_addr;
6916 const Powerpc_relobj<size, big_endian>* ppcobj
6917 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6918 (cs->first.object_));
6919 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6920 {
6921 unsigned int got2 = ppcobj->got2_shndx();
6922 got_addr = ppcobj->get_output_section_offset(got2);
6923 gold_assert(got_addr != invalid_address);
6924 got_addr += (ppcobj->output_section(got2)->address()
6925 + cs->first.addend_);
6926 }
6927 else
6928 {
6929 if (g_o_t == invalid_address)
6930 g_o_t = this->targ_->toc_pointer();
6931 got_addr = g_o_t;
6932 }
6933
6934 Address off = plt_addr - got_addr;
6935 if (ha(off) == 0)
6936 write_insn<big_endian>(p, lwz_11_30 + l(off));
6937 else
6938 {
6939 write_insn<big_endian>(p, addis_11_30 + ha(off));
6940 p += 4;
6941 write_insn<big_endian>(p, lwz_11_11 + l(off));
6942 }
6943 }
6944 else
6945 {
6946 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6947 p += 4;
6948 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6949 }
6950 p += 4;
6951 write_insn<big_endian>(p, mtctr_11);
6952 p += 4;
6953 write_insn<big_endian>(p, bctr);
6954 }
6955 }
6956
6957 // Write out long branch stubs.
6958 typename Branch_stub_entries::const_iterator bs;
6959 for (bs = this->long_branch_stubs_.begin();
6960 bs != this->long_branch_stubs_.end();
6961 ++bs)
6962 {
6963 if (bs->second.save_res_)
6964 continue;
6965 Address off = this->plt_size_ + bs->second.off_;
6966 p = oview + off;
6967 Address loc = this->stub_address() + off;
6968 Address delta = bs->first.dest_ - loc;
6969 if (delta + (1 << 25) < 2 << 25)
6970 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6971 else if (!parameters->options().output_is_position_independent())
6972 {
6973 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6974 p += 4;
6975 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6976 }
6977 else
6978 {
6979 delta -= 8;
6980 write_insn<big_endian>(p, mflr_0);
6981 p += 4;
6982 write_insn<big_endian>(p, bcl_20_31);
6983 p += 4;
6984 write_insn<big_endian>(p, mflr_12);
6985 p += 4;
6986 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6987 p += 4;
6988 write_insn<big_endian>(p, addi_12_12 + l(delta));
6989 p += 4;
6990 write_insn<big_endian>(p, mtlr_0);
6991 }
6992 p += 4;
6993 write_insn<big_endian>(p, mtctr_12);
6994 p += 4;
6995 write_insn<big_endian>(p, bctr);
6996 }
6997 }
6998 if (this->need_save_res_)
6999 {
7000 p = oview + this->plt_size_ + this->branch_size_;
7001 memcpy (p, this->targ_->savres_section()->contents(),
7002 this->targ_->savres_section()->data_size());
7003 }
7004 }
7005
7006 // Write out .glink.
7007
7008 template<int size, bool big_endian>
7009 void
7010 Output_data_glink<size, big_endian>::do_write(Output_file* of)
7011 {
7012 const section_size_type off = this->offset();
7013 const section_size_type oview_size =
7014 convert_to_section_size_type(this->data_size());
7015 unsigned char* const oview = of->get_output_view(off, oview_size);
7016 unsigned char* p;
7017
7018 // The base address of the .plt section.
7019 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7020 Address plt_base = this->targ_->plt_section()->address();
7021
7022 if (size == 64)
7023 {
7024 if (this->end_branch_table_ != 0)
7025 {
7026 // Write pltresolve stub.
7027 p = oview;
7028 Address after_bcl = this->address() + 16;
7029 Address pltoff = plt_base - after_bcl;
7030
7031 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
7032
7033 if (this->targ_->abiversion() < 2)
7034 {
7035 write_insn<big_endian>(p, mflr_12), p += 4;
7036 write_insn<big_endian>(p, bcl_20_31), p += 4;
7037 write_insn<big_endian>(p, mflr_11), p += 4;
7038 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
7039 write_insn<big_endian>(p, mtlr_12), p += 4;
7040 write_insn<big_endian>(p, add_11_2_11), p += 4;
7041 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7042 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
7043 write_insn<big_endian>(p, mtctr_12), p += 4;
7044 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
7045 }
7046 else
7047 {
7048 if (this->targ_->has_localentry0())
7049 {
7050 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
7051 }
7052 write_insn<big_endian>(p, mflr_0), p += 4;
7053 write_insn<big_endian>(p, bcl_20_31), p += 4;
7054 write_insn<big_endian>(p, mflr_11), p += 4;
7055 write_insn<big_endian>(p, mtlr_0), p += 4;
7056 if (this->targ_->has_localentry0())
7057 {
7058 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
7059 }
7060 else
7061 {
7062 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
7063 }
7064 write_insn<big_endian>(p, sub_12_12_11), p += 4;
7065 write_insn<big_endian>(p, add_11_0_11), p += 4;
7066 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
7067 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7068 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
7069 write_insn<big_endian>(p, mtctr_12), p += 4;
7070 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
7071 }
7072 write_insn<big_endian>(p, bctr), p += 4;
7073 gold_assert(p == oview + this->pltresolve_size());
7074
7075 // Write lazy link call stubs.
7076 uint32_t indx = 0;
7077 while (p < oview + this->end_branch_table_)
7078 {
7079 if (this->targ_->abiversion() < 2)
7080 {
7081 if (indx < 0x8000)
7082 {
7083 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
7084 }
7085 else
7086 {
7087 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
7088 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
7089 }
7090 }
7091 uint32_t branch_off = 8 - (p - oview);
7092 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
7093 indx++;
7094 }
7095 }
7096
7097 Address plt_base = this->targ_->plt_section()->address();
7098 Address iplt_base = invalid_address;
7099 unsigned int global_entry_off = this->global_entry_off();
7100 Address global_entry_base = this->address() + global_entry_off;
7101 typename Global_entry_stub_entries::const_iterator ge;
7102 for (ge = this->global_entry_stubs_.begin();
7103 ge != this->global_entry_stubs_.end();
7104 ++ge)
7105 {
7106 p = oview + global_entry_off + ge->second;
7107 Address plt_addr = ge->first->plt_offset();
7108 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7109 && ge->first->can_use_relative_reloc(false))
7110 {
7111 if (iplt_base == invalid_address)
7112 iplt_base = this->targ_->iplt_section()->address();
7113 plt_addr += iplt_base;
7114 }
7115 else
7116 plt_addr += plt_base;
7117 Address my_addr = global_entry_base + ge->second;
7118 Address off = plt_addr - my_addr;
7119
7120 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
7121 gold_error(_("linkage table error against `%s'"),
7122 ge->first->demangled_name().c_str());
7123
7124 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
7125 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
7126 write_insn<big_endian>(p, mtctr_12), p += 4;
7127 write_insn<big_endian>(p, bctr);
7128 }
7129 }
7130 else
7131 {
7132 // The address of _GLOBAL_OFFSET_TABLE_.
7133 Address g_o_t = this->targ_->toc_pointer();
7134
7135 // Write out pltresolve branch table.
7136 p = oview;
7137 unsigned int the_end = oview_size - this->pltresolve_size();
7138 unsigned char* end_p = oview + the_end;
7139 while (p < end_p - 8 * 4)
7140 write_insn<big_endian>(p, b + end_p - p), p += 4;
7141 while (p < end_p)
7142 write_insn<big_endian>(p, nop), p += 4;
7143
7144 // Write out pltresolve call stub.
7145 end_p = oview + oview_size;
7146 if (parameters->options().output_is_position_independent())
7147 {
7148 Address res0_off = 0;
7149 Address after_bcl_off = the_end + 12;
7150 Address bcl_res0 = after_bcl_off - res0_off;
7151
7152 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7153 p += 4;
7154 write_insn<big_endian>(p, mflr_0);
7155 p += 4;
7156 write_insn<big_endian>(p, bcl_20_31);
7157 p += 4;
7158 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7159 p += 4;
7160 write_insn<big_endian>(p, mflr_12);
7161 p += 4;
7162 write_insn<big_endian>(p, mtlr_0);
7163 p += 4;
7164 write_insn<big_endian>(p, sub_11_11_12);
7165 p += 4;
7166
7167 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
7168
7169 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7170 p += 4;
7171 if (ha(got_bcl) == ha(got_bcl + 4))
7172 {
7173 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7174 p += 4;
7175 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
7176 }
7177 else
7178 {
7179 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7180 p += 4;
7181 write_insn<big_endian>(p, lwz_12_12 + 4);
7182 }
7183 p += 4;
7184 write_insn<big_endian>(p, mtctr_0);
7185 p += 4;
7186 write_insn<big_endian>(p, add_0_11_11);
7187 p += 4;
7188 write_insn<big_endian>(p, add_11_0_11);
7189 }
7190 else
7191 {
7192 Address res0 = this->address();
7193
7194 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7195 p += 4;
7196 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7197 p += 4;
7198 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7199 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7200 else
7201 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7202 p += 4;
7203 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7204 p += 4;
7205 write_insn<big_endian>(p, mtctr_0);
7206 p += 4;
7207 write_insn<big_endian>(p, add_0_11_11);
7208 p += 4;
7209 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7210 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7211 else
7212 write_insn<big_endian>(p, lwz_12_12 + 4);
7213 p += 4;
7214 write_insn<big_endian>(p, add_11_0_11);
7215 }
7216 p += 4;
7217 write_insn<big_endian>(p, bctr);
7218 p += 4;
7219 while (p < end_p)
7220 {
7221 write_insn<big_endian>(p, nop);
7222 p += 4;
7223 }
7224 }
7225
7226 of->write_output_view(off, oview_size, oview);
7227 }
7228
7229
7230 // A class to handle linker generated save/restore functions.
7231
7232 template<int size, bool big_endian>
7233 class Output_data_save_res : public Output_section_data_build
7234 {
7235 public:
7236 Output_data_save_res(Symbol_table* symtab);
7237
7238 const unsigned char*
7239 contents() const
7240 {
7241 return contents_;
7242 }
7243
7244 protected:
7245 // Write to a map file.
7246 void
7247 do_print_to_mapfile(Mapfile* mapfile) const
7248 { mapfile->print_output_data(this, _("** save/restore")); }
7249
7250 void
7251 do_write(Output_file*);
7252
7253 private:
7254 // The maximum size of save/restore contents.
7255 static const unsigned int savres_max = 218*4;
7256
7257 void
7258 savres_define(Symbol_table* symtab,
7259 const char *name,
7260 unsigned int lo, unsigned int hi,
7261 unsigned char* write_ent(unsigned char*, int),
7262 unsigned char* write_tail(unsigned char*, int));
7263
7264 unsigned char *contents_;
7265 };
7266
7267 template<bool big_endian>
7268 static unsigned char*
7269 savegpr0(unsigned char* p, int r)
7270 {
7271 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7272 write_insn<big_endian>(p, insn);
7273 return p + 4;
7274 }
7275
7276 template<bool big_endian>
7277 static unsigned char*
7278 savegpr0_tail(unsigned char* p, int r)
7279 {
7280 p = savegpr0<big_endian>(p, r);
7281 uint32_t insn = std_0_1 + 16;
7282 write_insn<big_endian>(p, insn);
7283 p = p + 4;
7284 write_insn<big_endian>(p, blr);
7285 return p + 4;
7286 }
7287
7288 template<bool big_endian>
7289 static unsigned char*
7290 restgpr0(unsigned char* p, int r)
7291 {
7292 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7293 write_insn<big_endian>(p, insn);
7294 return p + 4;
7295 }
7296
7297 template<bool big_endian>
7298 static unsigned char*
7299 restgpr0_tail(unsigned char* p, int r)
7300 {
7301 uint32_t insn = ld_0_1 + 16;
7302 write_insn<big_endian>(p, insn);
7303 p = p + 4;
7304 p = restgpr0<big_endian>(p, r);
7305 write_insn<big_endian>(p, mtlr_0);
7306 p = p + 4;
7307 if (r == 29)
7308 {
7309 p = restgpr0<big_endian>(p, 30);
7310 p = restgpr0<big_endian>(p, 31);
7311 }
7312 write_insn<big_endian>(p, blr);
7313 return p + 4;
7314 }
7315
7316 template<bool big_endian>
7317 static unsigned char*
7318 savegpr1(unsigned char* p, int r)
7319 {
7320 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7321 write_insn<big_endian>(p, insn);
7322 return p + 4;
7323 }
7324
7325 template<bool big_endian>
7326 static unsigned char*
7327 savegpr1_tail(unsigned char* p, int r)
7328 {
7329 p = savegpr1<big_endian>(p, r);
7330 write_insn<big_endian>(p, blr);
7331 return p + 4;
7332 }
7333
7334 template<bool big_endian>
7335 static unsigned char*
7336 restgpr1(unsigned char* p, int r)
7337 {
7338 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7339 write_insn<big_endian>(p, insn);
7340 return p + 4;
7341 }
7342
7343 template<bool big_endian>
7344 static unsigned char*
7345 restgpr1_tail(unsigned char* p, int r)
7346 {
7347 p = restgpr1<big_endian>(p, r);
7348 write_insn<big_endian>(p, blr);
7349 return p + 4;
7350 }
7351
7352 template<bool big_endian>
7353 static unsigned char*
7354 savefpr(unsigned char* p, int r)
7355 {
7356 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7357 write_insn<big_endian>(p, insn);
7358 return p + 4;
7359 }
7360
7361 template<bool big_endian>
7362 static unsigned char*
7363 savefpr0_tail(unsigned char* p, int r)
7364 {
7365 p = savefpr<big_endian>(p, r);
7366 write_insn<big_endian>(p, std_0_1 + 16);
7367 p = p + 4;
7368 write_insn<big_endian>(p, blr);
7369 return p + 4;
7370 }
7371
7372 template<bool big_endian>
7373 static unsigned char*
7374 restfpr(unsigned char* p, int r)
7375 {
7376 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7377 write_insn<big_endian>(p, insn);
7378 return p + 4;
7379 }
7380
7381 template<bool big_endian>
7382 static unsigned char*
7383 restfpr0_tail(unsigned char* p, int r)
7384 {
7385 write_insn<big_endian>(p, ld_0_1 + 16);
7386 p = p + 4;
7387 p = restfpr<big_endian>(p, r);
7388 write_insn<big_endian>(p, mtlr_0);
7389 p = p + 4;
7390 if (r == 29)
7391 {
7392 p = restfpr<big_endian>(p, 30);
7393 p = restfpr<big_endian>(p, 31);
7394 }
7395 write_insn<big_endian>(p, blr);
7396 return p + 4;
7397 }
7398
7399 template<bool big_endian>
7400 static unsigned char*
7401 savefpr1_tail(unsigned char* p, int r)
7402 {
7403 p = savefpr<big_endian>(p, r);
7404 write_insn<big_endian>(p, blr);
7405 return p + 4;
7406 }
7407
7408 template<bool big_endian>
7409 static unsigned char*
7410 restfpr1_tail(unsigned char* p, int r)
7411 {
7412 p = restfpr<big_endian>(p, r);
7413 write_insn<big_endian>(p, blr);
7414 return p + 4;
7415 }
7416
7417 template<bool big_endian>
7418 static unsigned char*
7419 savevr(unsigned char* p, int r)
7420 {
7421 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7422 write_insn<big_endian>(p, insn);
7423 p = p + 4;
7424 insn = stvx_0_12_0 + (r << 21);
7425 write_insn<big_endian>(p, insn);
7426 return p + 4;
7427 }
7428
7429 template<bool big_endian>
7430 static unsigned char*
7431 savevr_tail(unsigned char* p, int r)
7432 {
7433 p = savevr<big_endian>(p, r);
7434 write_insn<big_endian>(p, blr);
7435 return p + 4;
7436 }
7437
7438 template<bool big_endian>
7439 static unsigned char*
7440 restvr(unsigned char* p, int r)
7441 {
7442 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7443 write_insn<big_endian>(p, insn);
7444 p = p + 4;
7445 insn = lvx_0_12_0 + (r << 21);
7446 write_insn<big_endian>(p, insn);
7447 return p + 4;
7448 }
7449
7450 template<bool big_endian>
7451 static unsigned char*
7452 restvr_tail(unsigned char* p, int r)
7453 {
7454 p = restvr<big_endian>(p, r);
7455 write_insn<big_endian>(p, blr);
7456 return p + 4;
7457 }
7458
7459
7460 template<int size, bool big_endian>
7461 Output_data_save_res<size, big_endian>::Output_data_save_res(
7462 Symbol_table* symtab)
7463 : Output_section_data_build(4),
7464 contents_(NULL)
7465 {
7466 this->savres_define(symtab,
7467 "_savegpr0_", 14, 31,
7468 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7469 this->savres_define(symtab,
7470 "_restgpr0_", 14, 29,
7471 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7472 this->savres_define(symtab,
7473 "_restgpr0_", 30, 31,
7474 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7475 this->savres_define(symtab,
7476 "_savegpr1_", 14, 31,
7477 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7478 this->savres_define(symtab,
7479 "_restgpr1_", 14, 31,
7480 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7481 this->savres_define(symtab,
7482 "_savefpr_", 14, 31,
7483 savefpr<big_endian>, savefpr0_tail<big_endian>);
7484 this->savres_define(symtab,
7485 "_restfpr_", 14, 29,
7486 restfpr<big_endian>, restfpr0_tail<big_endian>);
7487 this->savres_define(symtab,
7488 "_restfpr_", 30, 31,
7489 restfpr<big_endian>, restfpr0_tail<big_endian>);
7490 this->savres_define(symtab,
7491 "._savef", 14, 31,
7492 savefpr<big_endian>, savefpr1_tail<big_endian>);
7493 this->savres_define(symtab,
7494 "._restf", 14, 31,
7495 restfpr<big_endian>, restfpr1_tail<big_endian>);
7496 this->savres_define(symtab,
7497 "_savevr_", 20, 31,
7498 savevr<big_endian>, savevr_tail<big_endian>);
7499 this->savres_define(symtab,
7500 "_restvr_", 20, 31,
7501 restvr<big_endian>, restvr_tail<big_endian>);
7502 }
7503
7504 template<int size, bool big_endian>
7505 void
7506 Output_data_save_res<size, big_endian>::savres_define(
7507 Symbol_table* symtab,
7508 const char *name,
7509 unsigned int lo, unsigned int hi,
7510 unsigned char* write_ent(unsigned char*, int),
7511 unsigned char* write_tail(unsigned char*, int))
7512 {
7513 size_t len = strlen(name);
7514 bool writing = false;
7515 char sym[16];
7516
7517 memcpy(sym, name, len);
7518 sym[len + 2] = 0;
7519
7520 for (unsigned int i = lo; i <= hi; i++)
7521 {
7522 sym[len + 0] = i / 10 + '0';
7523 sym[len + 1] = i % 10 + '0';
7524 Symbol* gsym = symtab->lookup(sym);
7525 bool refd = gsym != NULL && gsym->is_undefined();
7526 writing = writing || refd;
7527 if (writing)
7528 {
7529 if (this->contents_ == NULL)
7530 this->contents_ = new unsigned char[this->savres_max];
7531
7532 section_size_type value = this->current_data_size();
7533 unsigned char* p = this->contents_ + value;
7534 if (i != hi)
7535 p = write_ent(p, i);
7536 else
7537 p = write_tail(p, i);
7538 section_size_type cur_size = p - this->contents_;
7539 this->set_current_data_size(cur_size);
7540 if (refd)
7541 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7542 this, value, cur_size - value,
7543 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7544 elfcpp::STV_HIDDEN, 0, false, false);
7545 }
7546 }
7547 }
7548
7549 // Write out save/restore.
7550
7551 template<int size, bool big_endian>
7552 void
7553 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7554 {
7555 const section_size_type off = this->offset();
7556 const section_size_type oview_size =
7557 convert_to_section_size_type(this->data_size());
7558 unsigned char* const oview = of->get_output_view(off, oview_size);
7559 memcpy(oview, this->contents_, oview_size);
7560 of->write_output_view(off, oview_size, oview);
7561 }
7562
7563
7564 // Create the glink section.
7565
7566 template<int size, bool big_endian>
7567 void
7568 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7569 {
7570 if (this->glink_ == NULL)
7571 {
7572 this->glink_ = new Output_data_glink<size, big_endian>(this);
7573 this->glink_->add_eh_frame(layout);
7574 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7575 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7576 this->glink_, ORDER_TEXT, false);
7577 }
7578 }
7579
7580 // Create a PLT entry for a global symbol.
7581
7582 template<int size, bool big_endian>
7583 void
7584 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7585 Layout* layout,
7586 Symbol* gsym)
7587 {
7588 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7589 && gsym->can_use_relative_reloc(false))
7590 {
7591 if (this->iplt_ == NULL)
7592 this->make_iplt_section(symtab, layout);
7593 this->iplt_->add_ifunc_entry(gsym);
7594 }
7595 else
7596 {
7597 if (this->plt_ == NULL)
7598 this->make_plt_section(symtab, layout);
7599 this->plt_->add_entry(gsym);
7600 }
7601 }
7602
7603 // Make a PLT entry for a local symbol.
7604
7605 template<int size, bool big_endian>
7606 void
7607 Target_powerpc<size, big_endian>::make_local_plt_entry(
7608 Symbol_table* symtab,
7609 Layout* layout,
7610 Sized_relobj_file<size, big_endian>* relobj,
7611 unsigned int r_sym)
7612 {
7613 if (this->lplt_ == NULL)
7614 this->make_lplt_section(symtab, layout);
7615 this->lplt_->add_local_entry(relobj, r_sym);
7616 }
7617
7618 template<int size, bool big_endian>
7619 void
7620 Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7621 Layout* layout,
7622 Symbol* gsym)
7623 {
7624 if (this->lplt_ == NULL)
7625 this->make_lplt_section(symtab, layout);
7626 this->lplt_->add_entry(gsym, true);
7627 }
7628
7629 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7630
7631 template<int size, bool big_endian>
7632 void
7633 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7634 Symbol_table* symtab,
7635 Layout* layout,
7636 Sized_relobj_file<size, big_endian>* relobj,
7637 unsigned int r_sym)
7638 {
7639 if (this->iplt_ == NULL)
7640 this->make_iplt_section(symtab, layout);
7641 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7642 }
7643
7644 // Return the number of entries in the PLT.
7645
7646 template<int size, bool big_endian>
7647 unsigned int
7648 Target_powerpc<size, big_endian>::plt_entry_count() const
7649 {
7650 if (this->plt_ == NULL)
7651 return 0;
7652 return this->plt_->entry_count();
7653 }
7654
7655 // Create a GOT entry for local dynamic __tls_get_addr calls.
7656
7657 template<int size, bool big_endian>
7658 unsigned int
7659 Target_powerpc<size, big_endian>::tlsld_got_offset(
7660 Symbol_table* symtab,
7661 Layout* layout,
7662 Sized_relobj_file<size, big_endian>* object)
7663 {
7664 if (this->tlsld_got_offset_ == -1U)
7665 {
7666 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7667 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7668 Output_data_got_powerpc<size, big_endian>* got
7669 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
7670 unsigned int got_offset = got->add_constant_pair(0, 0);
7671 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7672 got_offset, 0);
7673 this->tlsld_got_offset_ = got_offset;
7674 }
7675 return this->tlsld_got_offset_;
7676 }
7677
7678 // Get the Reference_flags for a particular relocation.
7679
7680 template<int size, bool big_endian>
7681 int
7682 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7683 unsigned int r_type,
7684 const Target_powerpc* target)
7685 {
7686 int ref = 0;
7687
7688 switch (r_type)
7689 {
7690 case elfcpp::R_PPC64_TOC:
7691 if (size != 64)
7692 break;
7693 // Fall through.
7694 case elfcpp::R_POWERPC_NONE:
7695 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7696 case elfcpp::R_POWERPC_GNU_VTENTRY:
7697 // No symbol reference.
7698 break;
7699
7700 case elfcpp::R_PPC64_ADDR64:
7701 case elfcpp::R_PPC64_UADDR64:
7702 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7703 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7704 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7705 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7706 case elfcpp::R_PPC64_D34:
7707 case elfcpp::R_PPC64_D34_LO:
7708 case elfcpp::R_PPC64_D34_HI30:
7709 case elfcpp::R_PPC64_D34_HA30:
7710 case elfcpp::R_PPC64_D28:
7711 if (size != 64)
7712 break;
7713 // Fall through.
7714 case elfcpp::R_POWERPC_ADDR32:
7715 case elfcpp::R_POWERPC_UADDR32:
7716 case elfcpp::R_POWERPC_ADDR16:
7717 case elfcpp::R_POWERPC_UADDR16:
7718 case elfcpp::R_POWERPC_ADDR16_LO:
7719 case elfcpp::R_POWERPC_ADDR16_HI:
7720 case elfcpp::R_POWERPC_ADDR16_HA:
7721 ref = Symbol::ABSOLUTE_REF;
7722 break;
7723
7724 case elfcpp::R_POWERPC_ADDR24:
7725 case elfcpp::R_POWERPC_ADDR14:
7726 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7727 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7728 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7729 break;
7730
7731 case elfcpp::R_PPC_LOCAL24PC:
7732 if (size != 32)
7733 break;
7734 // Fall through.
7735 ref = Symbol::RELATIVE_REF;
7736 break;
7737
7738 case elfcpp::R_PPC64_REL64:
7739 case elfcpp::R_PPC64_REL16_HIGH:
7740 case elfcpp::R_PPC64_REL16_HIGHA:
7741 case elfcpp::R_PPC64_REL16_HIGHER:
7742 case elfcpp::R_PPC64_REL16_HIGHERA:
7743 case elfcpp::R_PPC64_REL16_HIGHEST:
7744 case elfcpp::R_PPC64_REL16_HIGHESTA:
7745 case elfcpp::R_PPC64_PCREL34:
7746 case elfcpp::R_PPC64_REL16_HIGHER34:
7747 case elfcpp::R_PPC64_REL16_HIGHERA34:
7748 case elfcpp::R_PPC64_REL16_HIGHEST34:
7749 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7750 case elfcpp::R_PPC64_PCREL28:
7751 if (size != 64)
7752 break;
7753 // Fall through.
7754 case elfcpp::R_POWERPC_REL32:
7755 case elfcpp::R_POWERPC_REL16:
7756 case elfcpp::R_POWERPC_REL16_LO:
7757 case elfcpp::R_POWERPC_REL16_HI:
7758 case elfcpp::R_POWERPC_REL16_HA:
7759 ref = Symbol::RELATIVE_REF;
7760 break;
7761
7762 case elfcpp::R_PPC_PLTREL24:
7763 if (size != 32)
7764 break;
7765 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7766 break;
7767
7768 case elfcpp::R_PPC64_REL24_NOTOC:
7769 case elfcpp::R_PPC64_REL24_P9NOTOC:
7770 case elfcpp::R_PPC64_PLT16_LO_DS:
7771 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7772 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7773 case elfcpp::R_PPC64_PLT_PCREL34:
7774 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7775 if (size != 64)
7776 break;
7777 // Fall through.
7778 case elfcpp::R_POWERPC_REL24:
7779 case elfcpp::R_POWERPC_REL14:
7780 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7781 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7782 case elfcpp::R_POWERPC_PLT16_LO:
7783 case elfcpp::R_POWERPC_PLT16_HI:
7784 case elfcpp::R_POWERPC_PLT16_HA:
7785 case elfcpp::R_POWERPC_PLTSEQ:
7786 case elfcpp::R_POWERPC_PLTCALL:
7787 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7788 break;
7789
7790 case elfcpp::R_PPC64_GOT16_DS:
7791 case elfcpp::R_PPC64_GOT16_LO_DS:
7792 case elfcpp::R_PPC64_GOT_PCREL34:
7793 case elfcpp::R_PPC64_TOC16:
7794 case elfcpp::R_PPC64_TOC16_LO:
7795 case elfcpp::R_PPC64_TOC16_HI:
7796 case elfcpp::R_PPC64_TOC16_HA:
7797 case elfcpp::R_PPC64_TOC16_DS:
7798 case elfcpp::R_PPC64_TOC16_LO_DS:
7799 if (size != 64)
7800 break;
7801 // Fall through.
7802 case elfcpp::R_POWERPC_GOT16:
7803 case elfcpp::R_POWERPC_GOT16_LO:
7804 case elfcpp::R_POWERPC_GOT16_HI:
7805 case elfcpp::R_POWERPC_GOT16_HA:
7806 ref = Symbol::RELATIVE_REF;
7807 break;
7808
7809 case elfcpp::R_PPC64_TLSGD:
7810 case elfcpp::R_PPC64_TLSLD:
7811 case elfcpp::R_PPC64_TPREL34:
7812 case elfcpp::R_PPC64_DTPREL34:
7813 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7814 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7815 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7816 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7817 if (size != 64)
7818 break;
7819 // Fall through.
7820 case elfcpp::R_POWERPC_GOT_TPREL16:
7821 case elfcpp::R_POWERPC_TLS:
7822 ref = Symbol::TLS_REF;
7823 break;
7824
7825 case elfcpp::R_POWERPC_COPY:
7826 case elfcpp::R_POWERPC_GLOB_DAT:
7827 case elfcpp::R_POWERPC_JMP_SLOT:
7828 case elfcpp::R_POWERPC_RELATIVE:
7829 case elfcpp::R_POWERPC_DTPMOD:
7830 default:
7831 // Not expected. We will give an error later.
7832 break;
7833 }
7834
7835 if (size == 64 && target->abiversion() < 2)
7836 ref |= Symbol::FUNC_DESC_ABI;
7837 return ref;
7838 }
7839
7840 // Report an unsupported relocation against a local symbol.
7841
7842 template<int size, bool big_endian>
7843 void
7844 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7845 Sized_relobj_file<size, big_endian>* object,
7846 unsigned int r_type)
7847 {
7848 gold_error(_("%s: unsupported reloc %u against local symbol"),
7849 object->name().c_str(), r_type);
7850 }
7851
7852 // We are about to emit a dynamic relocation of type R_TYPE. If the
7853 // dynamic linker does not support it, issue an error.
7854
7855 template<int size, bool big_endian>
7856 void
7857 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7858 unsigned int r_type)
7859 {
7860 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7861
7862 // These are the relocation types supported by glibc for both 32-bit
7863 // and 64-bit powerpc.
7864 switch (r_type)
7865 {
7866 case elfcpp::R_POWERPC_NONE:
7867 case elfcpp::R_POWERPC_RELATIVE:
7868 case elfcpp::R_POWERPC_GLOB_DAT:
7869 case elfcpp::R_POWERPC_DTPMOD:
7870 case elfcpp::R_POWERPC_DTPREL:
7871 case elfcpp::R_POWERPC_TPREL:
7872 case elfcpp::R_POWERPC_JMP_SLOT:
7873 case elfcpp::R_POWERPC_COPY:
7874 case elfcpp::R_POWERPC_IRELATIVE:
7875 case elfcpp::R_POWERPC_ADDR32:
7876 case elfcpp::R_POWERPC_UADDR32:
7877 case elfcpp::R_POWERPC_ADDR24:
7878 case elfcpp::R_POWERPC_ADDR16:
7879 case elfcpp::R_POWERPC_UADDR16:
7880 case elfcpp::R_POWERPC_ADDR16_LO:
7881 case elfcpp::R_POWERPC_ADDR16_HI:
7882 case elfcpp::R_POWERPC_ADDR16_HA:
7883 case elfcpp::R_POWERPC_ADDR14:
7884 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7885 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7886 case elfcpp::R_POWERPC_REL32:
7887 case elfcpp::R_POWERPC_TPREL16:
7888 case elfcpp::R_POWERPC_TPREL16_LO:
7889 case elfcpp::R_POWERPC_TPREL16_HI:
7890 case elfcpp::R_POWERPC_TPREL16_HA:
7891 return;
7892
7893 default:
7894 break;
7895 }
7896
7897 if (size == 64)
7898 {
7899 switch (r_type)
7900 {
7901 // These are the relocation types supported only on 64-bit.
7902 case elfcpp::R_PPC64_ADDR64:
7903 case elfcpp::R_PPC64_UADDR64:
7904 case elfcpp::R_PPC64_JMP_IREL:
7905 case elfcpp::R_PPC64_ADDR16_DS:
7906 case elfcpp::R_PPC64_ADDR16_LO_DS:
7907 case elfcpp::R_PPC64_ADDR16_HIGH:
7908 case elfcpp::R_PPC64_ADDR16_HIGHA:
7909 case elfcpp::R_PPC64_ADDR16_HIGHER:
7910 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7911 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7912 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7913 case elfcpp::R_PPC64_REL64:
7914 case elfcpp::R_POWERPC_ADDR30:
7915 case elfcpp::R_PPC64_TPREL16_DS:
7916 case elfcpp::R_PPC64_TPREL16_LO_DS:
7917 case elfcpp::R_PPC64_TPREL16_HIGH:
7918 case elfcpp::R_PPC64_TPREL16_HIGHA:
7919 case elfcpp::R_PPC64_TPREL16_HIGHER:
7920 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7921 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7922 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7923 return;
7924
7925 default:
7926 break;
7927 }
7928 }
7929 else
7930 {
7931 switch (r_type)
7932 {
7933 // These are the relocation types supported only on 32-bit.
7934 // ??? glibc ld.so doesn't need to support these.
7935 case elfcpp::R_POWERPC_REL24:
7936 case elfcpp::R_POWERPC_DTPREL16:
7937 case elfcpp::R_POWERPC_DTPREL16_LO:
7938 case elfcpp::R_POWERPC_DTPREL16_HI:
7939 case elfcpp::R_POWERPC_DTPREL16_HA:
7940 return;
7941
7942 default:
7943 break;
7944 }
7945 }
7946
7947 // This prevents us from issuing more than one error per reloc
7948 // section. But we can still wind up issuing more than one
7949 // error per object file.
7950 if (this->issued_non_pic_error_)
7951 return;
7952 gold_assert(parameters->options().output_is_position_independent());
7953 object->error(_("requires unsupported dynamic reloc; "
7954 "recompile with -fPIC"));
7955 this->issued_non_pic_error_ = true;
7956 return;
7957 }
7958
7959 // Return whether we need to make a PLT entry for a relocation of the
7960 // given type against a STT_GNU_IFUNC symbol.
7961
7962 template<int size, bool big_endian>
7963 bool
7964 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7965 Target_powerpc<size, big_endian>* target,
7966 Sized_relobj_file<size, big_endian>* object,
7967 unsigned int r_type,
7968 bool report_err)
7969 {
7970 // In non-pic code any reference will resolve to the plt call stub
7971 // for the ifunc symbol.
7972 if ((size == 32 || target->abiversion() >= 2)
7973 && !parameters->options().output_is_position_independent())
7974 return true;
7975
7976 switch (r_type)
7977 {
7978 // Word size refs from data sections are OK, but don't need a PLT entry.
7979 case elfcpp::R_POWERPC_ADDR32:
7980 case elfcpp::R_POWERPC_UADDR32:
7981 if (size == 32)
7982 return false;
7983 break;
7984
7985 case elfcpp::R_PPC64_ADDR64:
7986 case elfcpp::R_PPC64_UADDR64:
7987 if (size == 64)
7988 return false;
7989 break;
7990
7991 // GOT refs are good, but also don't need a PLT entry.
7992 case elfcpp::R_POWERPC_GOT16:
7993 case elfcpp::R_POWERPC_GOT16_LO:
7994 case elfcpp::R_POWERPC_GOT16_HI:
7995 case elfcpp::R_POWERPC_GOT16_HA:
7996 case elfcpp::R_PPC64_GOT16_DS:
7997 case elfcpp::R_PPC64_GOT16_LO_DS:
7998 case elfcpp::R_PPC64_GOT_PCREL34:
7999 return false;
8000
8001 // PLT relocs are OK and need a PLT entry.
8002 case elfcpp::R_POWERPC_PLT16_LO:
8003 case elfcpp::R_POWERPC_PLT16_HI:
8004 case elfcpp::R_POWERPC_PLT16_HA:
8005 case elfcpp::R_PPC64_PLT16_LO_DS:
8006 case elfcpp::R_POWERPC_PLTSEQ:
8007 case elfcpp::R_POWERPC_PLTCALL:
8008 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8009 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8010 case elfcpp::R_PPC64_PLT_PCREL34:
8011 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8012 return true;
8013 break;
8014
8015 // Function calls are good, and these do need a PLT entry.
8016 case elfcpp::R_PPC64_REL24_NOTOC:
8017 if (size == 32)
8018 break;
8019 // Fall through.
8020 case elfcpp::R_PPC64_REL24_P9NOTOC:
8021 case elfcpp::R_POWERPC_ADDR24:
8022 case elfcpp::R_POWERPC_ADDR14:
8023 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8024 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8025 case elfcpp::R_POWERPC_REL24:
8026 case elfcpp::R_PPC_PLTREL24:
8027 case elfcpp::R_POWERPC_REL14:
8028 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8029 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8030 return true;
8031
8032 default:
8033 break;
8034 }
8035
8036 // Anything else is a problem.
8037 // If we are building a static executable, the libc startup function
8038 // responsible for applying indirect function relocations is going
8039 // to complain about the reloc type.
8040 // If we are building a dynamic executable, we will have a text
8041 // relocation. The dynamic loader will set the text segment
8042 // writable and non-executable to apply text relocations. So we'll
8043 // segfault when trying to run the indirection function to resolve
8044 // the reloc.
8045 if (report_err)
8046 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
8047 object->name().c_str(), r_type);
8048 return false;
8049 }
8050
8051 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
8052 // reloc.
8053
8054 static bool
8055 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8056 {
8057 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8058 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8059 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8060 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8061 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8062 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8063 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8064 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8065 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8066 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8067 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8068 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8069 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8070 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8071 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8072 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8073 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8074 /* Exclude lfqu by testing reloc. If relocs are ever
8075 defined for the reduced D field in psq_lu then those
8076 will need testing too. */
8077 && r_type != elfcpp::R_PPC64_TOC16_LO
8078 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8079 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8080 && (insn & 1) == 0)
8081 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8082 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8083 /* Exclude stfqu. psq_stu as above for psq_lu. */
8084 && r_type != elfcpp::R_PPC64_TOC16_LO
8085 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8086 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8087 && (insn & 1) == 0));
8088 }
8089
8090 // Scan a relocation for a local symbol.
8091
8092 template<int size, bool big_endian>
8093 inline void
8094 Target_powerpc<size, big_endian>::Scan::local(
8095 Symbol_table* symtab,
8096 Layout* layout,
8097 Target_powerpc<size, big_endian>* target,
8098 Sized_relobj_file<size, big_endian>* object,
8099 unsigned int data_shndx,
8100 Output_section* output_section,
8101 const elfcpp::Rela<size, big_endian>& reloc,
8102 unsigned int r_type,
8103 const elfcpp::Sym<size, big_endian>& lsym,
8104 bool is_discarded)
8105 {
8106 Powerpc_relobj<size, big_endian>* ppc_object
8107 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8108
8109 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
8110
8111 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8112 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8113 {
8114 this->expect_tls_get_addr_call();
8115 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8116 if (tls_type != tls::TLSOPT_NONE)
8117 this->skip_next_tls_get_addr_call();
8118 }
8119 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8120 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8121 {
8122 this->expect_tls_get_addr_call();
8123 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8124 if (tls_type != tls::TLSOPT_NONE)
8125 this->skip_next_tls_get_addr_call();
8126 }
8127
8128 if (is_discarded)
8129 {
8130 if (size == 64
8131 && data_shndx == ppc_object->opd_shndx()
8132 && r_type == elfcpp::R_PPC64_ADDR64)
8133 ppc_object->set_opd_discard(reloc.get_r_offset());
8134 return;
8135 }
8136
8137 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8138 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
8139 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8140 {
8141 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8142 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8143 r_type, r_sym, reloc.get_r_addend());
8144 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
8145 }
8146
8147 switch (r_type)
8148 {
8149 case elfcpp::R_POWERPC_NONE:
8150 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8151 case elfcpp::R_POWERPC_GNU_VTENTRY:
8152 case elfcpp::R_POWERPC_TLS:
8153 case elfcpp::R_PPC64_ENTRY:
8154 case elfcpp::R_POWERPC_PLTSEQ:
8155 case elfcpp::R_POWERPC_PLTCALL:
8156 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8157 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8158 case elfcpp::R_PPC64_PCREL_OPT:
8159 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8160 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8161 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8162 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8163 case elfcpp::R_PPC64_REL16_HIGHER34:
8164 case elfcpp::R_PPC64_REL16_HIGHERA34:
8165 case elfcpp::R_PPC64_REL16_HIGHEST34:
8166 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8167 case elfcpp::R_PPC64_D34:
8168 case elfcpp::R_PPC64_D34_LO:
8169 case elfcpp::R_PPC64_D34_HI30:
8170 case elfcpp::R_PPC64_D34_HA30:
8171 case elfcpp::R_PPC64_D28:
8172 case elfcpp::R_PPC64_PCREL34:
8173 case elfcpp::R_PPC64_PCREL28:
8174 case elfcpp::R_PPC64_TPREL34:
8175 case elfcpp::R_PPC64_DTPREL34:
8176 break;
8177
8178 case elfcpp::R_PPC64_TOC:
8179 {
8180 Output_data_got_powerpc<size, big_endian>* got
8181 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8182 if (parameters->options().output_is_position_independent())
8183 {
8184 Address off = reloc.get_r_offset();
8185 if (size == 64
8186 && target->abiversion() < 2
8187 && data_shndx == ppc_object->opd_shndx()
8188 && ppc_object->get_opd_discard(off - 8))
8189 break;
8190
8191 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8192 Address got_off = got->g_o_t();
8193 rela_dyn->add_output_section_relative(got->output_section(),
8194 elfcpp::R_POWERPC_RELATIVE,
8195 output_section,
8196 object, data_shndx, off,
8197 got_off);
8198 }
8199 }
8200 break;
8201
8202 case elfcpp::R_PPC64_ADDR64:
8203 case elfcpp::R_PPC64_UADDR64:
8204 case elfcpp::R_POWERPC_ADDR32:
8205 case elfcpp::R_POWERPC_UADDR32:
8206 case elfcpp::R_POWERPC_ADDR24:
8207 case elfcpp::R_POWERPC_ADDR16:
8208 case elfcpp::R_POWERPC_ADDR16_LO:
8209 case elfcpp::R_POWERPC_ADDR16_HI:
8210 case elfcpp::R_POWERPC_ADDR16_HA:
8211 case elfcpp::R_POWERPC_UADDR16:
8212 case elfcpp::R_PPC64_ADDR16_HIGH:
8213 case elfcpp::R_PPC64_ADDR16_HIGHA:
8214 case elfcpp::R_PPC64_ADDR16_HIGHER:
8215 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8216 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8217 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8218 case elfcpp::R_PPC64_ADDR16_DS:
8219 case elfcpp::R_PPC64_ADDR16_LO_DS:
8220 case elfcpp::R_POWERPC_ADDR14:
8221 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8222 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8223 // If building a shared library (or a position-independent
8224 // executable), we need to create a dynamic relocation for
8225 // this location.
8226 if (parameters->options().output_is_position_independent()
8227 || (size == 64 && is_ifunc && target->abiversion() < 2))
8228 {
8229 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8230 is_ifunc);
8231 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8232 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8233 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8234 {
8235 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8236 : elfcpp::R_POWERPC_RELATIVE);
8237 rela_dyn->add_local_relative(object, r_sym, dynrel,
8238 output_section, data_shndx,
8239 reloc.get_r_offset(),
8240 reloc.get_r_addend(), false);
8241 }
8242 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8243 {
8244 check_non_pic(object, r_type);
8245 rela_dyn->add_local(object, r_sym, r_type, output_section,
8246 data_shndx, reloc.get_r_offset(),
8247 reloc.get_r_addend());
8248 }
8249 else
8250 {
8251 gold_assert(lsym.get_st_value() == 0);
8252 unsigned int shndx = lsym.get_st_shndx();
8253 bool is_ordinary;
8254 shndx = object->adjust_sym_shndx(r_sym, shndx,
8255 &is_ordinary);
8256 if (!is_ordinary)
8257 object->error(_("section symbol %u has bad shndx %u"),
8258 r_sym, shndx);
8259 else
8260 rela_dyn->add_local_section(object, shndx, r_type,
8261 output_section, data_shndx,
8262 reloc.get_r_offset());
8263 }
8264 }
8265 break;
8266
8267 case elfcpp::R_PPC64_PLT_PCREL34:
8268 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8269 case elfcpp::R_POWERPC_PLT16_LO:
8270 case elfcpp::R_POWERPC_PLT16_HI:
8271 case elfcpp::R_POWERPC_PLT16_HA:
8272 case elfcpp::R_PPC64_PLT16_LO_DS:
8273 if (!is_ifunc)
8274 {
8275 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8276 target->make_local_plt_entry(symtab, layout, object, r_sym);
8277 }
8278 break;
8279
8280 case elfcpp::R_PPC64_REL24_NOTOC:
8281 if (size == 32)
8282 break;
8283 // Fall through.
8284 case elfcpp::R_PPC64_REL24_P9NOTOC:
8285 case elfcpp::R_POWERPC_REL24:
8286 case elfcpp::R_PPC_PLTREL24:
8287 case elfcpp::R_PPC_LOCAL24PC:
8288 case elfcpp::R_POWERPC_REL14:
8289 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8290 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8291 if (!is_ifunc)
8292 {
8293 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8294 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8295 r_type, r_sym, reloc.get_r_addend());
8296 }
8297 break;
8298
8299 case elfcpp::R_PPC64_TOCSAVE:
8300 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8301 // caller has already saved r2 and thus a plt call stub need not
8302 // save r2.
8303 if (size == 64
8304 && target->mark_pltcall(ppc_object, data_shndx,
8305 reloc.get_r_offset() - 4, symtab))
8306 {
8307 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8308 unsigned int shndx = lsym.get_st_shndx();
8309 bool is_ordinary;
8310 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8311 if (!is_ordinary)
8312 object->error(_("tocsave symbol %u has bad shndx %u"),
8313 r_sym, shndx);
8314 else
8315 target->add_tocsave(ppc_object, shndx,
8316 lsym.get_st_value() + reloc.get_r_addend());
8317 }
8318 break;
8319
8320 case elfcpp::R_PPC64_REL64:
8321 case elfcpp::R_POWERPC_REL32:
8322 case elfcpp::R_POWERPC_REL16:
8323 case elfcpp::R_POWERPC_REL16_LO:
8324 case elfcpp::R_POWERPC_REL16_HI:
8325 case elfcpp::R_POWERPC_REL16_HA:
8326 case elfcpp::R_POWERPC_REL16DX_HA:
8327 case elfcpp::R_PPC64_REL16_HIGH:
8328 case elfcpp::R_PPC64_REL16_HIGHA:
8329 case elfcpp::R_PPC64_REL16_HIGHER:
8330 case elfcpp::R_PPC64_REL16_HIGHERA:
8331 case elfcpp::R_PPC64_REL16_HIGHEST:
8332 case elfcpp::R_PPC64_REL16_HIGHESTA:
8333 case elfcpp::R_POWERPC_SECTOFF:
8334 case elfcpp::R_POWERPC_SECTOFF_LO:
8335 case elfcpp::R_POWERPC_SECTOFF_HI:
8336 case elfcpp::R_POWERPC_SECTOFF_HA:
8337 case elfcpp::R_PPC64_SECTOFF_DS:
8338 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8339 case elfcpp::R_POWERPC_TPREL16:
8340 case elfcpp::R_POWERPC_TPREL16_LO:
8341 case elfcpp::R_POWERPC_TPREL16_HI:
8342 case elfcpp::R_POWERPC_TPREL16_HA:
8343 case elfcpp::R_PPC64_TPREL16_DS:
8344 case elfcpp::R_PPC64_TPREL16_LO_DS:
8345 case elfcpp::R_PPC64_TPREL16_HIGH:
8346 case elfcpp::R_PPC64_TPREL16_HIGHA:
8347 case elfcpp::R_PPC64_TPREL16_HIGHER:
8348 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8349 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8350 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8351 case elfcpp::R_POWERPC_DTPREL16:
8352 case elfcpp::R_POWERPC_DTPREL16_LO:
8353 case elfcpp::R_POWERPC_DTPREL16_HI:
8354 case elfcpp::R_POWERPC_DTPREL16_HA:
8355 case elfcpp::R_PPC64_DTPREL16_DS:
8356 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8357 case elfcpp::R_PPC64_DTPREL16_HIGH:
8358 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8359 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8360 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8361 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8362 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8363 case elfcpp::R_PPC64_TLSGD:
8364 case elfcpp::R_PPC64_TLSLD:
8365 case elfcpp::R_PPC64_ADDR64_LOCAL:
8366 break;
8367
8368 case elfcpp::R_PPC64_GOT_PCREL34:
8369 case elfcpp::R_POWERPC_GOT16:
8370 case elfcpp::R_POWERPC_GOT16_LO:
8371 case elfcpp::R_POWERPC_GOT16_HI:
8372 case elfcpp::R_POWERPC_GOT16_HA:
8373 case elfcpp::R_PPC64_GOT16_DS:
8374 case elfcpp::R_PPC64_GOT16_LO_DS:
8375 {
8376 // The symbol requires a GOT entry.
8377 Got_type got_type = ((size == 32
8378 || r_type == elfcpp::R_POWERPC_GOT16
8379 || r_type == elfcpp::R_PPC64_GOT16_DS)
8380 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8381 Output_data_got_powerpc<size, big_endian>* got
8382 = target->got_section(symtab, layout, got_type);
8383 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8384 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8385
8386 if (!parameters->options().output_is_position_independent())
8387 {
8388 if (is_ifunc
8389 && (size == 32 || target->abiversion() >= 2))
8390 got->add_local_plt(object, r_sym, got_type, addend);
8391 else
8392 got->add_local(object, r_sym, got_type, addend);
8393 }
8394 else if (!object->local_has_got_offset(r_sym, got_type, addend))
8395 {
8396 // If we are generating a shared object or a pie, this
8397 // symbol's GOT entry will be set by a dynamic relocation.
8398 unsigned int off;
8399 off = got->add_constant(0);
8400 object->set_local_got_offset(r_sym, got_type, off, addend);
8401
8402 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8403 is_ifunc);
8404 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8405 : elfcpp::R_POWERPC_RELATIVE);
8406 rela_dyn->add_local_relative(object, r_sym, dynrel,
8407 got, off, addend, false);
8408 }
8409 }
8410 break;
8411
8412 case elfcpp::R_PPC64_TOC16:
8413 case elfcpp::R_PPC64_TOC16_LO:
8414 case elfcpp::R_PPC64_TOC16_HI:
8415 case elfcpp::R_PPC64_TOC16_HA:
8416 case elfcpp::R_PPC64_TOC16_DS:
8417 case elfcpp::R_PPC64_TOC16_LO_DS:
8418 // We need a GOT section.
8419 target->got_section(symtab, layout, GOT_TYPE_SMALL);
8420 break;
8421
8422 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8423 case elfcpp::R_POWERPC_GOT_TLSGD16:
8424 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8425 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8426 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8427 {
8428 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8429 if (tls_type == tls::TLSOPT_NONE)
8430 {
8431 Got_type got_type = ((size == 32
8432 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8433 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8434 Output_data_got_powerpc<size, big_endian>* got
8435 = target->got_section(symtab, layout, got_type);
8436 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8437 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8438 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8439 got->add_local_tls_pair(object, r_sym, got_type,
8440 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8441 addend);
8442 }
8443 else if (tls_type == tls::TLSOPT_TO_LE)
8444 {
8445 // no GOT relocs needed for Local Exec.
8446 }
8447 else
8448 gold_unreachable();
8449 }
8450 break;
8451
8452 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8453 case elfcpp::R_POWERPC_GOT_TLSLD16:
8454 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8455 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8456 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8457 {
8458 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8459 if (tls_type == tls::TLSOPT_NONE)
8460 target->tlsld_got_offset(symtab, layout, object);
8461 else if (tls_type == tls::TLSOPT_TO_LE)
8462 {
8463 // no GOT relocs needed for Local Exec.
8464 if (parameters->options().emit_relocs())
8465 {
8466 Output_section* os = layout->tls_segment()->first_section();
8467 gold_assert(os != NULL);
8468 os->set_needs_symtab_index();
8469 }
8470 }
8471 else
8472 gold_unreachable();
8473 }
8474 break;
8475
8476 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8477 case elfcpp::R_POWERPC_GOT_DTPREL16:
8478 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8479 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8480 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8481 {
8482 Got_type got_type = ((size == 32
8483 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8484 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
8485 Output_data_got_powerpc<size, big_endian>* got
8486 = target->got_section(symtab, layout, got_type);
8487 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8488 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8489 got->add_local_tls(object, r_sym, got_type, addend);
8490 }
8491 break;
8492
8493 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8494 case elfcpp::R_POWERPC_GOT_TPREL16:
8495 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8496 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8497 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8498 {
8499 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8500 if (tls_type == tls::TLSOPT_NONE)
8501 {
8502 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8503 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8504 Got_type got_type = ((size == 32
8505 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8506 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8507 if (!object->local_has_got_offset(r_sym, got_type, addend))
8508 {
8509 Output_data_got_powerpc<size, big_endian>* got
8510 = target->got_section(symtab, layout, got_type);
8511 unsigned int off = got->add_constant(0);
8512 object->set_local_got_offset(r_sym, got_type, off, addend);
8513
8514 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8515 rela_dyn->add_symbolless_local_addend(object, r_sym,
8516 elfcpp::R_POWERPC_TPREL,
8517 got, off, addend);
8518 }
8519 }
8520 else if (tls_type == tls::TLSOPT_TO_LE)
8521 {
8522 // no GOT relocs needed for Local Exec.
8523 }
8524 else
8525 gold_unreachable();
8526 }
8527 break;
8528
8529 default:
8530 unsupported_reloc_local(object, r_type);
8531 break;
8532 }
8533
8534 if (size == 64
8535 && parameters->options().toc_optimize())
8536 {
8537 if (data_shndx == ppc_object->toc_shndx())
8538 {
8539 bool ok = true;
8540 if (r_type != elfcpp::R_PPC64_ADDR64
8541 || (is_ifunc && target->abiversion() < 2))
8542 ok = false;
8543 else if (parameters->options().output_is_position_independent())
8544 {
8545 if (is_ifunc)
8546 ok = false;
8547 else
8548 {
8549 unsigned int shndx = lsym.get_st_shndx();
8550 if (shndx >= elfcpp::SHN_LORESERVE
8551 && shndx != elfcpp::SHN_XINDEX)
8552 ok = false;
8553 }
8554 }
8555 if (!ok)
8556 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8557 }
8558
8559 enum {no_check, check_lo, check_ha} insn_check;
8560 switch (r_type)
8561 {
8562 default:
8563 insn_check = no_check;
8564 break;
8565
8566 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8567 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8568 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8569 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8570 case elfcpp::R_POWERPC_GOT16_HA:
8571 case elfcpp::R_PPC64_TOC16_HA:
8572 insn_check = check_ha;
8573 break;
8574
8575 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8576 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8577 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8578 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8579 case elfcpp::R_POWERPC_GOT16_LO:
8580 case elfcpp::R_PPC64_GOT16_LO_DS:
8581 case elfcpp::R_PPC64_TOC16_LO:
8582 case elfcpp::R_PPC64_TOC16_LO_DS:
8583 insn_check = check_lo;
8584 break;
8585 }
8586
8587 section_size_type slen;
8588 const unsigned char* view = NULL;
8589 if (insn_check != no_check)
8590 {
8591 view = ppc_object->section_contents(data_shndx, &slen, false);
8592 section_size_type off =
8593 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8594 if (off < slen)
8595 {
8596 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8597 if (insn_check == check_lo
8598 ? !ok_lo_toc_insn(insn, r_type)
8599 : ((insn & ((0x3f << 26) | 0x1f << 16))
8600 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8601 {
8602 ppc_object->set_no_toc_opt();
8603 gold_warning(_("%s: toc optimization is not supported "
8604 "for %#08x instruction"),
8605 ppc_object->name().c_str(), insn);
8606 }
8607 }
8608 }
8609
8610 switch (r_type)
8611 {
8612 default:
8613 break;
8614 case elfcpp::R_PPC64_TOC16:
8615 case elfcpp::R_PPC64_TOC16_LO:
8616 case elfcpp::R_PPC64_TOC16_HI:
8617 case elfcpp::R_PPC64_TOC16_HA:
8618 case elfcpp::R_PPC64_TOC16_DS:
8619 case elfcpp::R_PPC64_TOC16_LO_DS:
8620 unsigned int shndx = lsym.get_st_shndx();
8621 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8622 bool is_ordinary;
8623 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8624 if (is_ordinary && shndx == ppc_object->toc_shndx())
8625 {
8626 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8627 if (dst_off < ppc_object->section_size(shndx))
8628 {
8629 bool ok = false;
8630 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8631 ok = true;
8632 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8633 {
8634 // Need to check that the insn is a ld
8635 if (!view)
8636 view = ppc_object->section_contents(data_shndx,
8637 &slen,
8638 false);
8639 section_size_type off =
8640 (convert_to_section_size_type(reloc.get_r_offset())
8641 + (big_endian ? -2 : 3));
8642 if (off < slen
8643 && (view[off] & (0x3f << 2)) == 58u << 2)
8644 ok = true;
8645 }
8646 if (!ok)
8647 ppc_object->set_no_toc_opt(dst_off);
8648 }
8649 }
8650 break;
8651 }
8652 }
8653
8654 if (size == 32)
8655 {
8656 switch (r_type)
8657 {
8658 case elfcpp::R_POWERPC_REL32:
8659 if (ppc_object->got2_shndx() != 0
8660 && parameters->options().output_is_position_independent())
8661 {
8662 unsigned int shndx = lsym.get_st_shndx();
8663 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8664 bool is_ordinary;
8665 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8666 if (is_ordinary && shndx == ppc_object->got2_shndx()
8667 && (ppc_object->section_flags(data_shndx)
8668 & elfcpp::SHF_EXECINSTR) != 0)
8669 gold_error(_("%s: unsupported -mbss-plt code"),
8670 ppc_object->name().c_str());
8671 }
8672 break;
8673 default:
8674 break;
8675 }
8676 }
8677
8678 switch (r_type)
8679 {
8680 case elfcpp::R_POWERPC_GOT_TLSLD16:
8681 case elfcpp::R_POWERPC_GOT_TLSGD16:
8682 case elfcpp::R_POWERPC_GOT_TPREL16:
8683 case elfcpp::R_POWERPC_GOT_DTPREL16:
8684 case elfcpp::R_POWERPC_GOT16:
8685 case elfcpp::R_PPC64_GOT16_DS:
8686 case elfcpp::R_PPC64_TOC16:
8687 case elfcpp::R_PPC64_TOC16_DS:
8688 ppc_object->set_has_small_toc_reloc();
8689 break;
8690 default:
8691 break;
8692 }
8693
8694 switch (r_type)
8695 {
8696 case elfcpp::R_PPC64_TPREL16_DS:
8697 case elfcpp::R_PPC64_TPREL16_LO_DS:
8698 case elfcpp::R_PPC64_TPREL16_HIGH:
8699 case elfcpp::R_PPC64_TPREL16_HIGHA:
8700 case elfcpp::R_PPC64_TPREL16_HIGHER:
8701 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8702 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8703 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8704 case elfcpp::R_PPC64_TPREL34:
8705 if (size != 64)
8706 break;
8707 // Fall through.
8708 case elfcpp::R_POWERPC_TPREL16:
8709 case elfcpp::R_POWERPC_TPREL16_LO:
8710 case elfcpp::R_POWERPC_TPREL16_HI:
8711 case elfcpp::R_POWERPC_TPREL16_HA:
8712 layout->set_has_static_tls();
8713 break;
8714 default:
8715 break;
8716 }
8717
8718 switch (r_type)
8719 {
8720 case elfcpp::R_POWERPC_TPREL16_HA:
8721 if (target->tprel_opt())
8722 {
8723 section_size_type slen;
8724 const unsigned char* view = NULL;
8725 view = ppc_object->section_contents(data_shndx, &slen, false);
8726 section_size_type off
8727 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8728 if (off < slen)
8729 {
8730 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8731 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8732 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8733 target->set_no_tprel_opt();
8734 }
8735 }
8736 break;
8737
8738 case elfcpp::R_PPC64_TPREL16_HIGH:
8739 case elfcpp::R_PPC64_TPREL16_HIGHA:
8740 case elfcpp::R_PPC64_TPREL16_HIGHER:
8741 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8742 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8743 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8744 if (size != 64)
8745 break;
8746 // Fall through.
8747 case elfcpp::R_POWERPC_TPREL16_HI:
8748 target->set_no_tprel_opt();
8749 break;
8750 default:
8751 break;
8752 }
8753
8754 switch (r_type)
8755 {
8756 case elfcpp::R_PPC64_D34:
8757 case elfcpp::R_PPC64_D34_LO:
8758 case elfcpp::R_PPC64_D34_HI30:
8759 case elfcpp::R_PPC64_D34_HA30:
8760 case elfcpp::R_PPC64_D28:
8761 case elfcpp::R_PPC64_PCREL34:
8762 case elfcpp::R_PPC64_PCREL28:
8763 case elfcpp::R_PPC64_TPREL34:
8764 case elfcpp::R_PPC64_DTPREL34:
8765 case elfcpp::R_PPC64_PLT_PCREL34:
8766 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8767 case elfcpp::R_PPC64_GOT_PCREL34:
8768 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8769 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8770 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8771 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8772 target->set_power10_relocs();
8773 break;
8774 default:
8775 break;
8776 }
8777 }
8778
8779 // Report an unsupported relocation against a global symbol.
8780
8781 template<int size, bool big_endian>
8782 void
8783 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8784 Sized_relobj_file<size, big_endian>* object,
8785 unsigned int r_type,
8786 Symbol* gsym)
8787 {
8788 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8789 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8790 }
8791
8792 // Scan a relocation for a global symbol.
8793
8794 template<int size, bool big_endian>
8795 inline void
8796 Target_powerpc<size, big_endian>::Scan::global(
8797 Symbol_table* symtab,
8798 Layout* layout,
8799 Target_powerpc<size, big_endian>* target,
8800 Sized_relobj_file<size, big_endian>* object,
8801 unsigned int data_shndx,
8802 Output_section* output_section,
8803 const elfcpp::Rela<size, big_endian>& reloc,
8804 unsigned int r_type,
8805 Symbol* gsym)
8806 {
8807 Powerpc_relobj<size, big_endian>* ppc_object
8808 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8809
8810 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8811 {
8812 case Track_tls::SKIP:
8813 return;
8814 default:
8815 break;
8816 }
8817
8818 if (target->replace_tls_get_addr(gsym))
8819 // Change a __tls_get_addr reference to __tls_get_addr_opt
8820 // so dynamic relocs are emitted against the latter symbol.
8821 gsym = target->tls_get_addr_opt();
8822
8823 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8824 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8825 {
8826 this->expect_tls_get_addr_call();
8827 bool final = gsym->final_value_is_known();
8828 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8829 if (tls_type != tls::TLSOPT_NONE)
8830 this->skip_next_tls_get_addr_call();
8831 }
8832 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8833 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8834 {
8835 this->expect_tls_get_addr_call();
8836 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8837 if (tls_type != tls::TLSOPT_NONE)
8838 this->skip_next_tls_get_addr_call();
8839 }
8840
8841 // A STT_GNU_IFUNC symbol may require a PLT entry.
8842 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8843 bool pushed_ifunc = false;
8844 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8845 {
8846 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8847 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8848 r_type, r_sym, reloc.get_r_addend());
8849 target->make_plt_entry(symtab, layout, gsym);
8850 pushed_ifunc = true;
8851 }
8852
8853 switch (r_type)
8854 {
8855 case elfcpp::R_POWERPC_NONE:
8856 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8857 case elfcpp::R_POWERPC_GNU_VTENTRY:
8858 case elfcpp::R_PPC_LOCAL24PC:
8859 case elfcpp::R_POWERPC_TLS:
8860 case elfcpp::R_PPC64_ENTRY:
8861 case elfcpp::R_POWERPC_PLTSEQ:
8862 case elfcpp::R_POWERPC_PLTCALL:
8863 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8864 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8865 case elfcpp::R_PPC64_PCREL_OPT:
8866 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8867 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8868 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8869 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8870 case elfcpp::R_PPC64_REL16_HIGHER34:
8871 case elfcpp::R_PPC64_REL16_HIGHERA34:
8872 case elfcpp::R_PPC64_REL16_HIGHEST34:
8873 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8874 case elfcpp::R_PPC64_D34:
8875 case elfcpp::R_PPC64_D34_LO:
8876 case elfcpp::R_PPC64_D34_HI30:
8877 case elfcpp::R_PPC64_D34_HA30:
8878 case elfcpp::R_PPC64_D28:
8879 case elfcpp::R_PPC64_PCREL34:
8880 case elfcpp::R_PPC64_PCREL28:
8881 case elfcpp::R_PPC64_TPREL34:
8882 case elfcpp::R_PPC64_DTPREL34:
8883 break;
8884
8885 case elfcpp::R_PPC64_TOC:
8886 {
8887 Output_data_got_powerpc<size, big_endian>* got
8888 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8889 if (parameters->options().output_is_position_independent())
8890 {
8891 Address off = reloc.get_r_offset();
8892 if (size == 64
8893 && data_shndx == ppc_object->opd_shndx()
8894 && ppc_object->get_opd_discard(off - 8))
8895 break;
8896
8897 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8898 Address got_off = got->g_o_t();
8899 rela_dyn->add_output_section_relative(got->output_section(),
8900 elfcpp::R_POWERPC_RELATIVE,
8901 output_section,
8902 object, data_shndx, off,
8903 got_off);
8904 }
8905 }
8906 break;
8907
8908 case elfcpp::R_PPC64_ADDR64:
8909 if (size == 64
8910 && target->abiversion() < 2
8911 && data_shndx == ppc_object->opd_shndx()
8912 && (gsym->is_defined_in_discarded_section()
8913 || gsym->object() != object))
8914 {
8915 ppc_object->set_opd_discard(reloc.get_r_offset());
8916 break;
8917 }
8918 // Fall through.
8919 case elfcpp::R_PPC64_UADDR64:
8920 case elfcpp::R_POWERPC_ADDR32:
8921 case elfcpp::R_POWERPC_UADDR32:
8922 case elfcpp::R_POWERPC_ADDR24:
8923 case elfcpp::R_POWERPC_ADDR16:
8924 case elfcpp::R_POWERPC_ADDR16_LO:
8925 case elfcpp::R_POWERPC_ADDR16_HI:
8926 case elfcpp::R_POWERPC_ADDR16_HA:
8927 case elfcpp::R_POWERPC_UADDR16:
8928 case elfcpp::R_PPC64_ADDR16_HIGH:
8929 case elfcpp::R_PPC64_ADDR16_HIGHA:
8930 case elfcpp::R_PPC64_ADDR16_HIGHER:
8931 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8932 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8933 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8934 case elfcpp::R_PPC64_ADDR16_DS:
8935 case elfcpp::R_PPC64_ADDR16_LO_DS:
8936 case elfcpp::R_POWERPC_ADDR14:
8937 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8938 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8939 {
8940 // Make a PLT entry if necessary.
8941 if (gsym->needs_plt_entry())
8942 {
8943 // Since this is not a PC-relative relocation, we may be
8944 // taking the address of a function. In that case we need to
8945 // set the entry in the dynamic symbol table to the address of
8946 // the PLT call stub.
8947 bool need_ifunc_plt = false;
8948 if ((size == 32 || target->abiversion() >= 2)
8949 && gsym->is_from_dynobj()
8950 && !parameters->options().output_is_position_independent())
8951 {
8952 gsym->set_needs_dynsym_value();
8953 need_ifunc_plt = true;
8954 }
8955 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8956 {
8957 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8958 target->push_branch(ppc_object, data_shndx,
8959 reloc.get_r_offset(), r_type, r_sym,
8960 reloc.get_r_addend());
8961 target->make_plt_entry(symtab, layout, gsym);
8962 }
8963 }
8964 // Make a dynamic relocation if necessary.
8965 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8966 || (size == 64 && is_ifunc && target->abiversion() < 2))
8967 {
8968 if (!parameters->options().output_is_position_independent()
8969 && gsym->may_need_copy_reloc())
8970 {
8971 target->copy_reloc(symtab, layout, object,
8972 data_shndx, output_section, gsym, reloc);
8973 }
8974 else if ((((size == 32
8975 && r_type == elfcpp::R_POWERPC_ADDR32)
8976 || (size == 64
8977 && r_type == elfcpp::R_PPC64_ADDR64
8978 && target->abiversion() >= 2))
8979 && gsym->can_use_relative_reloc(false)
8980 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8981 && parameters->options().shared()))
8982 || (size == 64
8983 && r_type == elfcpp::R_PPC64_ADDR64
8984 && target->abiversion() < 2
8985 && (gsym->can_use_relative_reloc(false)
8986 || data_shndx == ppc_object->opd_shndx())))
8987 {
8988 Reloc_section* rela_dyn
8989 = target->rela_dyn_section(symtab, layout, is_ifunc);
8990 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8991 : elfcpp::R_POWERPC_RELATIVE);
8992 // Use the "add" method that marks the reloc as being
8993 // relative. This is proper here and in other places
8994 // that add IRELATIVE relocs because those relocs go
8995 // into a separate section that isn't sorted, so it
8996 // doesn't matter that they are marked is_relative.
8997 rela_dyn->add_global_relative(
8998 gsym, dynrel, output_section, object, data_shndx,
8999 reloc.get_r_offset(), reloc.get_r_addend(), false);
9000 }
9001 else
9002 {
9003 Reloc_section* rela_dyn
9004 = target->rela_dyn_section(symtab, layout, is_ifunc);
9005 check_non_pic(object, r_type);
9006 rela_dyn->add_global(gsym, r_type, output_section,
9007 object, data_shndx,
9008 reloc.get_r_offset(),
9009 reloc.get_r_addend());
9010
9011 if (size == 64
9012 && parameters->options().toc_optimize()
9013 && data_shndx == ppc_object->toc_shndx())
9014 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9015 }
9016 }
9017 }
9018 break;
9019
9020 case elfcpp::R_PPC64_PLT_PCREL34:
9021 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9022 case elfcpp::R_POWERPC_PLT16_LO:
9023 case elfcpp::R_POWERPC_PLT16_HI:
9024 case elfcpp::R_POWERPC_PLT16_HA:
9025 case elfcpp::R_PPC64_PLT16_LO_DS:
9026 if (!pushed_ifunc)
9027 {
9028 if (branch_needs_plt_entry(gsym))
9029 target->make_plt_entry(symtab, layout, gsym);
9030 else
9031 target->make_local_plt_entry(symtab, layout, gsym);
9032 }
9033 break;
9034
9035 case elfcpp::R_PPC64_REL24_NOTOC:
9036 if (size == 32)
9037 break;
9038 // Fall through.
9039 case elfcpp::R_PPC64_REL24_P9NOTOC:
9040 case elfcpp::R_PPC_PLTREL24:
9041 case elfcpp::R_POWERPC_REL24:
9042 if (!is_ifunc)
9043 {
9044 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9045 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9046 r_type, r_sym, reloc.get_r_addend());
9047 if (branch_needs_plt_entry(gsym))
9048 target->make_plt_entry(symtab, layout, gsym);
9049 }
9050 // Fall through.
9051
9052 case elfcpp::R_PPC64_REL64:
9053 case elfcpp::R_POWERPC_REL32:
9054 // Make a dynamic relocation if necessary.
9055 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
9056 {
9057 if (!parameters->options().output_is_position_independent()
9058 && gsym->may_need_copy_reloc())
9059 {
9060 target->copy_reloc(symtab, layout, object,
9061 data_shndx, output_section, gsym,
9062 reloc);
9063 }
9064 else
9065 {
9066 Reloc_section* rela_dyn
9067 = target->rela_dyn_section(symtab, layout, is_ifunc);
9068 check_non_pic(object, r_type);
9069 rela_dyn->add_global(gsym, r_type, output_section, object,
9070 data_shndx, reloc.get_r_offset(),
9071 reloc.get_r_addend());
9072 }
9073 }
9074 break;
9075
9076 case elfcpp::R_POWERPC_REL14:
9077 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9078 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9079 if (!is_ifunc)
9080 {
9081 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9082 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9083 r_type, r_sym, reloc.get_r_addend());
9084 }
9085 break;
9086
9087 case elfcpp::R_PPC64_TOCSAVE:
9088 // R_PPC64_TOCSAVE follows a call instruction to indicate the
9089 // caller has already saved r2 and thus a plt call stub need not
9090 // save r2.
9091 if (size == 64
9092 && target->mark_pltcall(ppc_object, data_shndx,
9093 reloc.get_r_offset() - 4, symtab))
9094 {
9095 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9096 bool is_ordinary;
9097 unsigned int shndx = gsym->shndx(&is_ordinary);
9098 if (!is_ordinary)
9099 object->error(_("tocsave symbol %u has bad shndx %u"),
9100 r_sym, shndx);
9101 else
9102 {
9103 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9104 target->add_tocsave(ppc_object, shndx,
9105 sym->value() + reloc.get_r_addend());
9106 }
9107 }
9108 break;
9109
9110 case elfcpp::R_POWERPC_REL16:
9111 case elfcpp::R_POWERPC_REL16_LO:
9112 case elfcpp::R_POWERPC_REL16_HI:
9113 case elfcpp::R_POWERPC_REL16_HA:
9114 case elfcpp::R_POWERPC_REL16DX_HA:
9115 case elfcpp::R_PPC64_REL16_HIGH:
9116 case elfcpp::R_PPC64_REL16_HIGHA:
9117 case elfcpp::R_PPC64_REL16_HIGHER:
9118 case elfcpp::R_PPC64_REL16_HIGHERA:
9119 case elfcpp::R_PPC64_REL16_HIGHEST:
9120 case elfcpp::R_PPC64_REL16_HIGHESTA:
9121 case elfcpp::R_POWERPC_SECTOFF:
9122 case elfcpp::R_POWERPC_SECTOFF_LO:
9123 case elfcpp::R_POWERPC_SECTOFF_HI:
9124 case elfcpp::R_POWERPC_SECTOFF_HA:
9125 case elfcpp::R_PPC64_SECTOFF_DS:
9126 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9127 case elfcpp::R_POWERPC_TPREL16:
9128 case elfcpp::R_POWERPC_TPREL16_LO:
9129 case elfcpp::R_POWERPC_TPREL16_HI:
9130 case elfcpp::R_POWERPC_TPREL16_HA:
9131 case elfcpp::R_PPC64_TPREL16_DS:
9132 case elfcpp::R_PPC64_TPREL16_LO_DS:
9133 case elfcpp::R_PPC64_TPREL16_HIGH:
9134 case elfcpp::R_PPC64_TPREL16_HIGHA:
9135 case elfcpp::R_PPC64_TPREL16_HIGHER:
9136 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9137 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9138 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9139 case elfcpp::R_POWERPC_DTPREL16:
9140 case elfcpp::R_POWERPC_DTPREL16_LO:
9141 case elfcpp::R_POWERPC_DTPREL16_HI:
9142 case elfcpp::R_POWERPC_DTPREL16_HA:
9143 case elfcpp::R_PPC64_DTPREL16_DS:
9144 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9145 case elfcpp::R_PPC64_DTPREL16_HIGH:
9146 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9147 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9148 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9149 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9150 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9151 case elfcpp::R_PPC64_TLSGD:
9152 case elfcpp::R_PPC64_TLSLD:
9153 case elfcpp::R_PPC64_ADDR64_LOCAL:
9154 break;
9155
9156 case elfcpp::R_PPC64_GOT_PCREL34:
9157 case elfcpp::R_POWERPC_GOT16:
9158 case elfcpp::R_POWERPC_GOT16_LO:
9159 case elfcpp::R_POWERPC_GOT16_HI:
9160 case elfcpp::R_POWERPC_GOT16_HA:
9161 case elfcpp::R_PPC64_GOT16_DS:
9162 case elfcpp::R_PPC64_GOT16_LO_DS:
9163 {
9164 // The symbol requires a GOT entry.
9165 Output_data_got_powerpc<size, big_endian>* got;
9166 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9167 Got_type got_type = ((size == 32
9168 || r_type == elfcpp::R_POWERPC_GOT16
9169 || r_type == elfcpp::R_PPC64_GOT16_DS)
9170 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
9171
9172 got = target->got_section(symtab, layout, got_type);
9173 if (gsym->final_value_is_known())
9174 {
9175 if (is_ifunc
9176 && (size == 32 || target->abiversion() >= 2))
9177 got->add_global_plt(gsym, got_type, addend);
9178 else
9179 got->add_global(gsym, got_type, addend);
9180 }
9181 else if (!gsym->has_got_offset(got_type, addend))
9182 {
9183 // If we are generating a shared object or a pie, this
9184 // symbol's GOT entry will be set by a dynamic relocation.
9185 unsigned int off = got->add_constant(0);
9186 gsym->set_got_offset(got_type, off, addend);
9187
9188 Reloc_section* rela_dyn
9189 = target->rela_dyn_section(symtab, layout, is_ifunc);
9190
9191 if (gsym->can_use_relative_reloc(false)
9192 && !((size == 32
9193 || target->abiversion() >= 2)
9194 && gsym->visibility() == elfcpp::STV_PROTECTED
9195 && parameters->options().shared()))
9196 {
9197 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9198 : elfcpp::R_POWERPC_RELATIVE);
9199 rela_dyn->add_global_relative(gsym, dynrel, got, off,
9200 addend, false);
9201 }
9202 else
9203 {
9204 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
9205 rela_dyn->add_global(gsym, dynrel, got, off, addend);
9206 }
9207 }
9208 }
9209 break;
9210
9211 case elfcpp::R_PPC64_TOC16:
9212 case elfcpp::R_PPC64_TOC16_LO:
9213 case elfcpp::R_PPC64_TOC16_HI:
9214 case elfcpp::R_PPC64_TOC16_HA:
9215 case elfcpp::R_PPC64_TOC16_DS:
9216 case elfcpp::R_PPC64_TOC16_LO_DS:
9217 // We need a GOT section.
9218 target->got_section(symtab, layout, GOT_TYPE_SMALL);
9219 break;
9220
9221 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9222 case elfcpp::R_POWERPC_GOT_TLSGD16:
9223 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9224 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9225 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9226 {
9227 bool final = gsym->final_value_is_known();
9228 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9229 if (tls_type == tls::TLSOPT_NONE)
9230 {
9231 Got_type got_type = ((size == 32
9232 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9233 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
9234 Output_data_got_powerpc<size, big_endian>* got
9235 = target->got_section(symtab, layout, got_type);
9236 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9237 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9238 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
9239 elfcpp::R_POWERPC_DTPMOD,
9240 elfcpp::R_POWERPC_DTPREL,
9241 addend);
9242 }
9243 else if (tls_type == tls::TLSOPT_TO_IE)
9244 {
9245 Got_type got_type = ((size == 32
9246 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9247 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9248 if (!gsym->has_got_offset(got_type))
9249 {
9250 Output_data_got_powerpc<size, big_endian>* got
9251 = target->got_section(symtab, layout, got_type);
9252 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9253 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9254 if (gsym->is_undefined()
9255 || gsym->is_from_dynobj())
9256 {
9257 got->add_global_with_rel(gsym, got_type, rela_dyn,
9258 elfcpp::R_POWERPC_TPREL, addend);
9259 }
9260 else
9261 {
9262 unsigned int off = got->add_constant(0);
9263 gsym->set_got_offset(got_type, off);
9264 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9265 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9266 got, off, addend);
9267 }
9268 }
9269 }
9270 else if (tls_type == tls::TLSOPT_TO_LE)
9271 {
9272 // no GOT relocs needed for Local Exec.
9273 }
9274 else
9275 gold_unreachable();
9276 }
9277 break;
9278
9279 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9280 case elfcpp::R_POWERPC_GOT_TLSLD16:
9281 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9282 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9283 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9284 {
9285 tls::Tls_optimization tls_type = target->optimize_tls_ld();
9286 if (tls_type == tls::TLSOPT_NONE)
9287 target->tlsld_got_offset(symtab, layout, object);
9288 else if (tls_type == tls::TLSOPT_TO_LE)
9289 {
9290 // no GOT relocs needed for Local Exec.
9291 if (parameters->options().emit_relocs())
9292 {
9293 Output_section* os = layout->tls_segment()->first_section();
9294 gold_assert(os != NULL);
9295 os->set_needs_symtab_index();
9296 }
9297 }
9298 else
9299 gold_unreachable();
9300 }
9301 break;
9302
9303 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9304 case elfcpp::R_POWERPC_GOT_DTPREL16:
9305 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9306 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9307 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9308 {
9309 Got_type got_type = ((size == 32
9310 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9311 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
9312 Output_data_got_powerpc<size, big_endian>* got
9313 = target->got_section(symtab, layout, got_type);
9314 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9315 if (!gsym->final_value_is_known()
9316 && (gsym->is_from_dynobj()
9317 || gsym->is_undefined()
9318 || gsym->is_preemptible()))
9319 got->add_global_with_rel(gsym, got_type,
9320 target->rela_dyn_section(layout),
9321 elfcpp::R_POWERPC_DTPREL, addend);
9322 else
9323 got->add_global_tls(gsym, got_type, addend);
9324 }
9325 break;
9326
9327 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9328 case elfcpp::R_POWERPC_GOT_TPREL16:
9329 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9330 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9331 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9332 {
9333 bool final = gsym->final_value_is_known();
9334 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9335 if (tls_type == tls::TLSOPT_NONE)
9336 {
9337 Got_type got_type = ((size == 32
9338 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9339 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9340 if (!gsym->has_got_offset(got_type))
9341 {
9342 Output_data_got_powerpc<size, big_endian>* got
9343 = target->got_section(symtab, layout, got_type);
9344 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9345 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9346 if (gsym->is_undefined()
9347 || gsym->is_from_dynobj())
9348 {
9349 got->add_global_with_rel(gsym, got_type, rela_dyn,
9350 elfcpp::R_POWERPC_TPREL, addend);
9351 }
9352 else
9353 {
9354 unsigned int off = got->add_constant(0);
9355 gsym->set_got_offset(got_type, off);
9356 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9357 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9358 got, off, addend);
9359 }
9360 }
9361 }
9362 else if (tls_type == tls::TLSOPT_TO_LE)
9363 {
9364 // no GOT relocs needed for Local Exec.
9365 }
9366 else
9367 gold_unreachable();
9368 }
9369 break;
9370
9371 default:
9372 unsupported_reloc_global(object, r_type, gsym);
9373 break;
9374 }
9375
9376 if (size == 64
9377 && parameters->options().toc_optimize())
9378 {
9379 if (data_shndx == ppc_object->toc_shndx())
9380 {
9381 bool ok = true;
9382 if (r_type != elfcpp::R_PPC64_ADDR64
9383 || (is_ifunc && target->abiversion() < 2))
9384 ok = false;
9385 else if (parameters->options().output_is_position_independent()
9386 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9387 ok = false;
9388 if (!ok)
9389 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9390 }
9391
9392 enum {no_check, check_lo, check_ha} insn_check;
9393 switch (r_type)
9394 {
9395 default:
9396 insn_check = no_check;
9397 break;
9398
9399 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9400 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9401 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9402 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9403 case elfcpp::R_POWERPC_GOT16_HA:
9404 case elfcpp::R_PPC64_TOC16_HA:
9405 insn_check = check_ha;
9406 break;
9407
9408 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9409 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9410 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9411 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9412 case elfcpp::R_POWERPC_GOT16_LO:
9413 case elfcpp::R_PPC64_GOT16_LO_DS:
9414 case elfcpp::R_PPC64_TOC16_LO:
9415 case elfcpp::R_PPC64_TOC16_LO_DS:
9416 insn_check = check_lo;
9417 break;
9418 }
9419
9420 section_size_type slen;
9421 const unsigned char* view = NULL;
9422 if (insn_check != no_check)
9423 {
9424 view = ppc_object->section_contents(data_shndx, &slen, false);
9425 section_size_type off =
9426 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9427 if (off < slen)
9428 {
9429 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9430 if (insn_check == check_lo
9431 ? !ok_lo_toc_insn(insn, r_type)
9432 : ((insn & ((0x3f << 26) | 0x1f << 16))
9433 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9434 {
9435 ppc_object->set_no_toc_opt();
9436 gold_warning(_("%s: toc optimization is not supported "
9437 "for %#08x instruction"),
9438 ppc_object->name().c_str(), insn);
9439 }
9440 }
9441 }
9442
9443 switch (r_type)
9444 {
9445 default:
9446 break;
9447 case elfcpp::R_PPC64_TOC16:
9448 case elfcpp::R_PPC64_TOC16_LO:
9449 case elfcpp::R_PPC64_TOC16_HI:
9450 case elfcpp::R_PPC64_TOC16_HA:
9451 case elfcpp::R_PPC64_TOC16_DS:
9452 case elfcpp::R_PPC64_TOC16_LO_DS:
9453 if (gsym->source() == Symbol::FROM_OBJECT
9454 && !gsym->object()->is_dynamic())
9455 {
9456 Powerpc_relobj<size, big_endian>* sym_object
9457 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9458 bool is_ordinary;
9459 unsigned int shndx = gsym->shndx(&is_ordinary);
9460 if (shndx == sym_object->toc_shndx())
9461 {
9462 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9463 Address dst_off = sym->value() + reloc.get_r_addend();
9464 if (dst_off < sym_object->section_size(shndx))
9465 {
9466 bool ok = false;
9467 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9468 ok = true;
9469 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9470 {
9471 // Need to check that the insn is a ld
9472 if (!view)
9473 view = ppc_object->section_contents(data_shndx,
9474 &slen,
9475 false);
9476 section_size_type off =
9477 (convert_to_section_size_type(reloc.get_r_offset())
9478 + (big_endian ? -2 : 3));
9479 if (off < slen
9480 && (view[off] & (0x3f << 2)) == (58u << 2))
9481 ok = true;
9482 }
9483 if (!ok)
9484 sym_object->set_no_toc_opt(dst_off);
9485 }
9486 }
9487 }
9488 break;
9489 }
9490 }
9491
9492 if (size == 32)
9493 {
9494 switch (r_type)
9495 {
9496 case elfcpp::R_PPC_LOCAL24PC:
9497 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9498 gold_error(_("%s: unsupported -mbss-plt code"),
9499 ppc_object->name().c_str());
9500 break;
9501 default:
9502 break;
9503 }
9504 }
9505
9506 switch (r_type)
9507 {
9508 case elfcpp::R_POWERPC_GOT_TLSLD16:
9509 case elfcpp::R_POWERPC_GOT_TLSGD16:
9510 case elfcpp::R_POWERPC_GOT_TPREL16:
9511 case elfcpp::R_POWERPC_GOT_DTPREL16:
9512 case elfcpp::R_POWERPC_GOT16:
9513 case elfcpp::R_PPC64_GOT16_DS:
9514 case elfcpp::R_PPC64_TOC16:
9515 case elfcpp::R_PPC64_TOC16_DS:
9516 ppc_object->set_has_small_toc_reloc();
9517 break;
9518 default:
9519 break;
9520 }
9521
9522 switch (r_type)
9523 {
9524 case elfcpp::R_PPC64_TPREL16_DS:
9525 case elfcpp::R_PPC64_TPREL16_LO_DS:
9526 case elfcpp::R_PPC64_TPREL16_HIGH:
9527 case elfcpp::R_PPC64_TPREL16_HIGHA:
9528 case elfcpp::R_PPC64_TPREL16_HIGHER:
9529 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9530 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9531 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9532 case elfcpp::R_PPC64_TPREL34:
9533 if (size != 64)
9534 break;
9535 // Fall through.
9536 case elfcpp::R_POWERPC_TPREL16:
9537 case elfcpp::R_POWERPC_TPREL16_LO:
9538 case elfcpp::R_POWERPC_TPREL16_HI:
9539 case elfcpp::R_POWERPC_TPREL16_HA:
9540 layout->set_has_static_tls();
9541 break;
9542 default:
9543 break;
9544 }
9545
9546 switch (r_type)
9547 {
9548 case elfcpp::R_POWERPC_TPREL16_HA:
9549 if (target->tprel_opt())
9550 {
9551 section_size_type slen;
9552 const unsigned char* view = NULL;
9553 view = ppc_object->section_contents(data_shndx, &slen, false);
9554 section_size_type off
9555 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9556 if (off < slen)
9557 {
9558 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9559 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9560 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9561 target->set_no_tprel_opt();
9562 }
9563 }
9564 break;
9565
9566 case elfcpp::R_PPC64_TPREL16_HIGH:
9567 case elfcpp::R_PPC64_TPREL16_HIGHA:
9568 case elfcpp::R_PPC64_TPREL16_HIGHER:
9569 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9570 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9571 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9572 if (size != 64)
9573 break;
9574 // Fall through.
9575 case elfcpp::R_POWERPC_TPREL16_HI:
9576 target->set_no_tprel_opt();
9577 break;
9578 default:
9579 break;
9580 }
9581
9582 switch (r_type)
9583 {
9584 case elfcpp::R_PPC64_D34:
9585 case elfcpp::R_PPC64_D34_LO:
9586 case elfcpp::R_PPC64_D34_HI30:
9587 case elfcpp::R_PPC64_D34_HA30:
9588 case elfcpp::R_PPC64_D28:
9589 case elfcpp::R_PPC64_PCREL34:
9590 case elfcpp::R_PPC64_PCREL28:
9591 case elfcpp::R_PPC64_TPREL34:
9592 case elfcpp::R_PPC64_DTPREL34:
9593 case elfcpp::R_PPC64_PLT_PCREL34:
9594 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9595 case elfcpp::R_PPC64_GOT_PCREL34:
9596 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9597 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9598 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9599 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9600 target->set_power10_relocs();
9601 break;
9602 default:
9603 break;
9604 }
9605 }
9606
9607 // Process relocations for gc.
9608
9609 template<int size, bool big_endian>
9610 void
9611 Target_powerpc<size, big_endian>::gc_process_relocs(
9612 Symbol_table* symtab,
9613 Layout* layout,
9614 Sized_relobj_file<size, big_endian>* object,
9615 unsigned int data_shndx,
9616 unsigned int,
9617 const unsigned char* prelocs,
9618 size_t reloc_count,
9619 Output_section* output_section,
9620 bool needs_special_offset_handling,
9621 size_t local_symbol_count,
9622 const unsigned char* plocal_symbols)
9623 {
9624 typedef Target_powerpc<size, big_endian> Powerpc;
9625 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9626 Classify_reloc;
9627
9628 Powerpc_relobj<size, big_endian>* ppc_object
9629 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9630 if (size == 64)
9631 ppc_object->set_opd_valid();
9632 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9633 {
9634 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9635 for (p = ppc_object->access_from_map()->begin();
9636 p != ppc_object->access_from_map()->end();
9637 ++p)
9638 {
9639 Address dst_off = p->first;
9640 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9641 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9642 for (s = p->second.begin(); s != p->second.end(); ++s)
9643 {
9644 Relobj* src_obj = s->first;
9645 unsigned int src_indx = s->second;
9646 symtab->gc()->add_reference(src_obj, src_indx,
9647 ppc_object, dst_indx);
9648 }
9649 p->second.clear();
9650 }
9651 ppc_object->access_from_map()->clear();
9652 ppc_object->process_gc_mark(symtab);
9653 // Don't look at .opd relocs as .opd will reference everything.
9654 return;
9655 }
9656
9657 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9658 symtab,
9659 layout,
9660 this,
9661 object,
9662 data_shndx,
9663 prelocs,
9664 reloc_count,
9665 output_section,
9666 needs_special_offset_handling,
9667 local_symbol_count,
9668 plocal_symbols);
9669 }
9670
9671 // Handle target specific gc actions when adding a gc reference from
9672 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9673 // and DST_OFF. For powerpc64, this adds a referenc to the code
9674 // section of a function descriptor.
9675
9676 template<int size, bool big_endian>
9677 void
9678 Target_powerpc<size, big_endian>::do_gc_add_reference(
9679 Symbol_table* symtab,
9680 Relobj* src_obj,
9681 unsigned int src_shndx,
9682 Relobj* dst_obj,
9683 unsigned int dst_shndx,
9684 Address dst_off) const
9685 {
9686 if (size != 64 || dst_obj->is_dynamic())
9687 return;
9688
9689 Powerpc_relobj<size, big_endian>* ppc_object
9690 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9691 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9692 {
9693 if (ppc_object->opd_valid())
9694 {
9695 dst_shndx = ppc_object->get_opd_ent(dst_off);
9696 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9697 }
9698 else
9699 {
9700 // If we haven't run scan_opd_relocs, we must delay
9701 // processing this function descriptor reference.
9702 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9703 }
9704 }
9705 }
9706
9707 // Add any special sections for this symbol to the gc work list.
9708 // For powerpc64, this adds the code section of a function
9709 // descriptor.
9710
9711 template<int size, bool big_endian>
9712 void
9713 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9714 Symbol_table* symtab,
9715 Symbol* sym) const
9716 {
9717 if (size == 64 && sym->object()->pluginobj() == NULL)
9718 {
9719 Powerpc_relobj<size, big_endian>* ppc_object
9720 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9721 bool is_ordinary;
9722 unsigned int shndx = sym->shndx(&is_ordinary);
9723 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9724 {
9725 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9726 Address dst_off = gsym->value();
9727 if (ppc_object->opd_valid())
9728 {
9729 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9730 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9731 dst_indx));
9732 }
9733 else
9734 ppc_object->add_gc_mark(dst_off);
9735 }
9736 }
9737 }
9738
9739 // For a symbol location in .opd, set LOC to the location of the
9740 // function entry.
9741
9742 template<int size, bool big_endian>
9743 void
9744 Target_powerpc<size, big_endian>::do_function_location(
9745 Symbol_location* loc) const
9746 {
9747 if (size == 64 && loc->shndx != 0)
9748 {
9749 if (loc->object->is_dynamic())
9750 {
9751 Powerpc_dynobj<size, big_endian>* ppc_object
9752 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9753 if (loc->shndx == ppc_object->opd_shndx())
9754 {
9755 Address dest_off;
9756 Address off = loc->offset - ppc_object->opd_address();
9757 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9758 loc->offset = dest_off;
9759 }
9760 }
9761 else
9762 {
9763 const Powerpc_relobj<size, big_endian>* ppc_object
9764 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9765 if (loc->shndx == ppc_object->opd_shndx())
9766 {
9767 Address dest_off;
9768 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9769 loc->offset = dest_off;
9770 }
9771 }
9772 }
9773 }
9774
9775 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9776 // compiled with -fsplit-stack. The function calls non-split-stack
9777 // code. Change the function to ensure it has enough stack space to
9778 // call some random function.
9779
9780 template<int size, bool big_endian>
9781 void
9782 Target_powerpc<size, big_endian>::do_calls_non_split(
9783 Relobj* object,
9784 unsigned int shndx,
9785 section_offset_type fnoffset,
9786 section_size_type fnsize,
9787 const unsigned char* prelocs,
9788 size_t reloc_count,
9789 unsigned char* view,
9790 section_size_type view_size,
9791 std::string* from,
9792 std::string* to) const
9793 {
9794 // 32-bit not supported.
9795 if (size == 32)
9796 {
9797 // warn
9798 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9799 prelocs, reloc_count, view, view_size,
9800 from, to);
9801 return;
9802 }
9803
9804 // The function always starts with
9805 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9806 // addis %r12,%r1,-allocate@ha
9807 // addi %r12,%r12,-allocate@l
9808 // cmpld %r12,%r0
9809 // but note that the addis or addi may be replaced with a nop
9810
9811 unsigned char *entry = view + fnoffset;
9812 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9813
9814 if ((insn & 0xffff0000) == addis_2_12)
9815 {
9816 /* Skip ELFv2 global entry code. */
9817 entry += 8;
9818 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9819 }
9820
9821 unsigned char *pinsn = entry;
9822 bool ok = false;
9823 const uint32_t ld_private_ss = 0xe80d8fc0;
9824 if (insn == ld_private_ss)
9825 {
9826 int32_t allocate = 0;
9827 while (1)
9828 {
9829 pinsn += 4;
9830 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9831 if ((insn & 0xffff0000) == addis_12_1)
9832 allocate += (insn & 0xffff) << 16;
9833 else if ((insn & 0xffff0000) == addi_12_1
9834 || (insn & 0xffff0000) == addi_12_12)
9835 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9836 else if (insn != nop)
9837 break;
9838 }
9839 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9840 {
9841 int extra = parameters->options().split_stack_adjust_size();
9842 allocate -= extra;
9843 if (allocate >= 0 || extra < 0)
9844 {
9845 object->error(_("split-stack stack size overflow at "
9846 "section %u offset %0zx"),
9847 shndx, static_cast<size_t>(fnoffset));
9848 return;
9849 }
9850 pinsn = entry + 4;
9851 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9852 if (insn != addis_12_1)
9853 {
9854 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9855 pinsn += 4;
9856 insn = addi_12_12 | (allocate & 0xffff);
9857 if (insn != addi_12_12)
9858 {
9859 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9860 pinsn += 4;
9861 }
9862 }
9863 else
9864 {
9865 insn = addi_12_1 | (allocate & 0xffff);
9866 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9867 pinsn += 4;
9868 }
9869 if (pinsn != entry + 12)
9870 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9871
9872 ok = true;
9873 }
9874 }
9875
9876 if (!ok)
9877 {
9878 if (!object->has_no_split_stack())
9879 object->error(_("failed to match split-stack sequence at "
9880 "section %u offset %0zx"),
9881 shndx, static_cast<size_t>(fnoffset));
9882 }
9883 }
9884
9885 // Scan relocations for a section.
9886
9887 template<int size, bool big_endian>
9888 void
9889 Target_powerpc<size, big_endian>::scan_relocs(
9890 Symbol_table* symtab,
9891 Layout* layout,
9892 Sized_relobj_file<size, big_endian>* object,
9893 unsigned int data_shndx,
9894 unsigned int sh_type,
9895 const unsigned char* prelocs,
9896 size_t reloc_count,
9897 Output_section* output_section,
9898 bool needs_special_offset_handling,
9899 size_t local_symbol_count,
9900 const unsigned char* plocal_symbols)
9901 {
9902 typedef Target_powerpc<size, big_endian> Powerpc;
9903 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9904 Classify_reloc;
9905
9906 if (!this->plt_localentry0_init_)
9907 {
9908 bool plt_localentry0 = false;
9909 if (size == 64
9910 && this->abiversion() >= 2)
9911 {
9912 if (parameters->options().user_set_plt_localentry())
9913 plt_localentry0 = parameters->options().plt_localentry();
9914 if (plt_localentry0
9915 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9916 gold_warning(_("--plt-localentry is especially dangerous without "
9917 "ld.so support to detect ABI violations"));
9918 }
9919 this->plt_localentry0_ = plt_localentry0;
9920 this->plt_localentry0_init_ = true;
9921 }
9922
9923 if (sh_type == elfcpp::SHT_REL)
9924 {
9925 gold_error(_("%s: unsupported REL reloc section"),
9926 object->name().c_str());
9927 return;
9928 }
9929
9930 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9931 symtab,
9932 layout,
9933 this,
9934 object,
9935 data_shndx,
9936 prelocs,
9937 reloc_count,
9938 output_section,
9939 needs_special_offset_handling,
9940 local_symbol_count,
9941 plocal_symbols);
9942
9943 if (this->plt_localentry0_ && this->power10_relocs_)
9944 {
9945 gold_warning(_("--plt-localentry is incompatible with "
9946 "power10 pc-relative code"));
9947 this->plt_localentry0_ = false;
9948 }
9949 }
9950
9951 // Functor class for processing the global symbol table.
9952 // Removes symbols defined on discarded opd entries.
9953
9954 template<bool big_endian>
9955 class Global_symbol_visitor_opd
9956 {
9957 public:
9958 Global_symbol_visitor_opd()
9959 { }
9960
9961 void
9962 operator()(Sized_symbol<64>* sym)
9963 {
9964 if (sym->has_symtab_index()
9965 || sym->source() != Symbol::FROM_OBJECT
9966 || !sym->in_real_elf())
9967 return;
9968
9969 if (sym->object()->is_dynamic())
9970 return;
9971
9972 Powerpc_relobj<64, big_endian>* symobj
9973 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9974 if (symobj->opd_shndx() == 0)
9975 return;
9976
9977 bool is_ordinary;
9978 unsigned int shndx = sym->shndx(&is_ordinary);
9979 if (shndx == symobj->opd_shndx()
9980 && symobj->get_opd_discard(sym->value()))
9981 {
9982 sym->set_undefined();
9983 sym->set_visibility(elfcpp::STV_DEFAULT);
9984 sym->set_is_defined_in_discarded_section();
9985 sym->set_symtab_index(-1U);
9986 }
9987 }
9988 };
9989
9990 template<int size, bool big_endian>
9991 void
9992 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9993 Layout* layout,
9994 Symbol_table* symtab)
9995 {
9996 if (size == 64)
9997 {
9998 Output_data_save_res<size, big_endian>* savres
9999 = new Output_data_save_res<size, big_endian>(symtab);
10000 this->savres_section_ = savres;
10001 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
10002 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
10003 savres, ORDER_TEXT, false);
10004 }
10005 }
10006
10007 // Sort linker created .got section first (for the header), then input
10008 // sections belonging to files using small model code.
10009
10010 template<bool big_endian>
10011 class Sort_toc_sections
10012 {
10013 const Output_section_data*
10014 small_got_section() const
10015 {
10016 return (static_cast<Target_powerpc<64, big_endian>*>(
10017 parameters->sized_target<64, big_endian>())
10018 ->got_section(GOT_TYPE_SMALL));
10019 }
10020
10021 int
10022 rank(const Output_section::Input_section& isec) const
10023 {
10024 if (!isec.is_input_section())
10025 {
10026 if (isec.output_section_data() == this->small_got_section())
10027 return 0;
10028 return 2;
10029 }
10030 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
10031 ->has_small_toc_reloc())
10032 return 1;
10033 return 3;
10034 }
10035
10036 public:
10037 bool
10038 operator()(const Output_section::Input_section& is1,
10039 const Output_section::Input_section& is2) const
10040 {
10041 return rank(is1) < rank(is2);
10042 }
10043 };
10044
10045 // Finalize the sections.
10046
10047 template<int size, bool big_endian>
10048 void
10049 Target_powerpc<size, big_endian>::do_finalize_sections(
10050 Layout* layout,
10051 const Input_objects* input_objects,
10052 Symbol_table* symtab)
10053 {
10054 if (parameters->doing_static_link())
10055 {
10056 // At least some versions of glibc elf-init.o have a strong
10057 // reference to __rela_iplt marker syms. A weak ref would be
10058 // better..
10059 if (this->iplt_ != NULL)
10060 {
10061 Reloc_section* rel = this->iplt_->rel_plt();
10062 symtab->define_in_output_data("__rela_iplt_start", NULL,
10063 Symbol_table::PREDEFINED, rel, 0, 0,
10064 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10065 elfcpp::STV_HIDDEN, 0, false, true);
10066 symtab->define_in_output_data("__rela_iplt_end", NULL,
10067 Symbol_table::PREDEFINED, rel, 0, 0,
10068 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10069 elfcpp::STV_HIDDEN, 0, true, true);
10070 }
10071 else
10072 {
10073 symtab->define_as_constant("__rela_iplt_start", NULL,
10074 Symbol_table::PREDEFINED, 0, 0,
10075 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10076 elfcpp::STV_HIDDEN, 0, true, false);
10077 symtab->define_as_constant("__rela_iplt_end", NULL,
10078 Symbol_table::PREDEFINED, 0, 0,
10079 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10080 elfcpp::STV_HIDDEN, 0, true, false);
10081 }
10082 }
10083
10084 if (size == 64)
10085 {
10086 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10087 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
10088
10089 if (!parameters->options().relocatable())
10090 {
10091 this->define_save_restore_funcs(layout, symtab);
10092
10093 // Annoyingly, we need to make these sections now whether or
10094 // not we need them. If we delay until do_relax then we
10095 // need to mess with the relaxation machinery checkpointing.
10096 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
10097 this->make_brlt_section(layout);
10098
10099 // FIXME, maybe. Here we could run through all the got
10100 // entries in the small got section, removing any duplicates
10101 // found in the big got section and renumbering offsets.
10102
10103 if (parameters->options().toc_sort())
10104 {
10105 Output_section* os = this->got_->output_section();
10106 if (os != NULL && os->input_sections().size() > 1)
10107 std::stable_sort(os->input_sections().begin(),
10108 os->input_sections().end(),
10109 Sort_toc_sections<big_endian>());
10110 }
10111 }
10112 }
10113
10114 // Fill in some more dynamic tags.
10115 Output_data_dynamic* odyn = layout->dynamic_data();
10116 if (odyn != NULL)
10117 {
10118 const Reloc_section* rel_plt = (this->plt_ == NULL
10119 ? NULL
10120 : this->plt_->rel_plt());
10121 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
10122 this->rela_dyn_, true, size == 32, true);
10123
10124 if (size == 32)
10125 {
10126 if (this->got_ != NULL)
10127 {
10128 this->got_->finalize_data_size();
10129 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10130 this->got_, this->got_->g_o_t());
10131 }
10132 if (this->has_tls_get_addr_opt_)
10133 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
10134 }
10135 else
10136 {
10137 if (this->glink_ != NULL)
10138 {
10139 this->glink_->finalize_data_size();
10140 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10141 this->glink_,
10142 (this->glink_->pltresolve_size()
10143 - 32));
10144 }
10145 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
10146 odyn->add_constant(elfcpp::DT_PPC64_OPT,
10147 ((this->has_localentry0_
10148 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10149 | (this->has_tls_get_addr_opt_
10150 ? elfcpp::PPC64_OPT_TLS : 0)));
10151 }
10152 }
10153
10154 // Emit any relocs we saved in an attempt to avoid generating COPY
10155 // relocs.
10156 if (this->copy_relocs_.any_saved_relocs())
10157 this->copy_relocs_.emit(this->rela_dyn_section(layout));
10158
10159 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10160 p != input_objects->relobj_end();
10161 ++p)
10162 {
10163 Powerpc_relobj<size, big_endian>* ppc_relobj
10164 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10165 if (ppc_relobj->attributes_section_data())
10166 this->merge_object_attributes(ppc_relobj,
10167 ppc_relobj->attributes_section_data());
10168 }
10169 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10170 p != input_objects->dynobj_end();
10171 ++p)
10172 {
10173 Powerpc_dynobj<size, big_endian>* ppc_dynobj
10174 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10175 if (ppc_dynobj->attributes_section_data())
10176 this->merge_object_attributes(ppc_dynobj,
10177 ppc_dynobj->attributes_section_data());
10178 }
10179
10180 // Create a .gnu.attributes section if we have merged any attributes
10181 // from inputs.
10182 if (this->attributes_section_data_ != NULL
10183 && this->attributes_section_data_->size() != 0)
10184 {
10185 Output_attributes_section_data* attributes_section
10186 = new Output_attributes_section_data(*this->attributes_section_data_);
10187 layout->add_output_section_data(".gnu.attributes",
10188 elfcpp::SHT_GNU_ATTRIBUTES, 0,
10189 attributes_section, ORDER_INVALID, false);
10190 }
10191 }
10192
10193 // Get the custom dynamic tag value.
10194
10195 template<int size, bool big_endian>
10196 unsigned int
10197 Target_powerpc<size, big_endian>::do_dynamic_tag_custom_value(
10198 elfcpp::DT tag) const
10199 {
10200 if (tag != elfcpp::DT_RELACOUNT)
10201 gold_unreachable();
10202 return this->rela_dyn_->relative_reloc_count();
10203 }
10204
10205 // Merge object attributes from input file called NAME with those of the
10206 // output. The input object attributes are in the object pointed by PASD.
10207
10208 template<int size, bool big_endian>
10209 void
10210 Target_powerpc<size, big_endian>::merge_object_attributes(
10211 const Object* obj,
10212 const Attributes_section_data* pasd)
10213 {
10214 // Return if there is no attributes section data.
10215 if (pasd == NULL)
10216 return;
10217
10218 // Create output object attributes.
10219 if (this->attributes_section_data_ == NULL)
10220 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10221
10222 const int vendor = Object_attribute::OBJ_ATTR_GNU;
10223 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10224 Object_attribute* out_attr
10225 = this->attributes_section_data_->known_attributes(vendor);
10226
10227 const char* name = obj->name().c_str();
10228 const char* err;
10229 const char* first;
10230 const char* second;
10231 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10232 int in_fp = in_attr[tag].int_value() & 0xf;
10233 int out_fp = out_attr[tag].int_value() & 0xf;
10234 bool warn_only = obj->is_dynamic();
10235 if (in_fp != out_fp)
10236 {
10237 err = NULL;
10238 if ((in_fp & 3) == 0)
10239 ;
10240 else if ((out_fp & 3) == 0)
10241 {
10242 if (!warn_only)
10243 {
10244 out_fp |= in_fp & 3;
10245 out_attr[tag].set_int_value(out_fp);
10246 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10247 this->last_fp_ = name;
10248 }
10249 }
10250 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10251 {
10252 err = N_("%s uses hard float, %s uses soft float");
10253 first = this->last_fp_;
10254 second = name;
10255 }
10256 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10257 {
10258 err = N_("%s uses hard float, %s uses soft float");
10259 first = name;
10260 second = this->last_fp_;
10261 }
10262 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10263 {
10264 err = N_("%s uses double-precision hard float, "
10265 "%s uses single-precision hard float");
10266 first = this->last_fp_;
10267 second = name;
10268 }
10269 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10270 {
10271 err = N_("%s uses double-precision hard float, "
10272 "%s uses single-precision hard float");
10273 first = name;
10274 second = this->last_fp_;
10275 }
10276
10277 if (err || (in_fp & 0xc) == 0)
10278 ;
10279 else if ((out_fp & 0xc) == 0)
10280 {
10281 if (!warn_only)
10282 {
10283 out_fp |= in_fp & 0xc;
10284 out_attr[tag].set_int_value(out_fp);
10285 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10286 this->last_ld_ = name;
10287 }
10288 }
10289 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10290 {
10291 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10292 first = name;
10293 second = this->last_ld_;
10294 }
10295 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10296 {
10297 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10298 first = this->last_ld_;
10299 second = name;
10300 }
10301 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10302 {
10303 err = N_("%s uses IBM long double, %s uses IEEE long double");
10304 first = this->last_ld_;
10305 second = name;
10306 }
10307 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10308 {
10309 err = N_("%s uses IBM long double, %s uses IEEE long double");
10310 first = name;
10311 second = this->last_ld_;
10312 }
10313
10314 if (err)
10315 {
10316 if (parameters->options().warn_mismatch())
10317 {
10318 if (warn_only)
10319 gold_warning(_(err), first, second);
10320 else
10321 gold_error(_(err), first, second);
10322 }
10323 // Arrange for this attribute to be deleted. It's better to
10324 // say "don't know" about a file than to wrongly claim compliance.
10325 if (!warn_only)
10326 out_attr[tag].set_type(0);
10327 }
10328 }
10329
10330 if (size == 32)
10331 {
10332 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10333 int in_vec = in_attr[tag].int_value() & 3;
10334 int out_vec = out_attr[tag].int_value() & 3;
10335 if (in_vec != out_vec)
10336 {
10337 err = NULL;
10338 if (in_vec == 0)
10339 ;
10340 else if (out_vec == 0)
10341 {
10342 out_vec = in_vec;
10343 out_attr[tag].set_int_value(out_vec);
10344 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10345 this->last_vec_ = name;
10346 }
10347 // For now, allow generic to transition to AltiVec or SPE
10348 // without a warning. If GCC marked files with their stack
10349 // alignment and used don't-care markings for files which are
10350 // not affected by the vector ABI, we could warn about this
10351 // case too. */
10352 else if (in_vec == 1)
10353 ;
10354 else if (out_vec == 1)
10355 {
10356 out_vec = in_vec;
10357 out_attr[tag].set_int_value(out_vec);
10358 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10359 this->last_vec_ = name;
10360 }
10361 else if (out_vec < in_vec)
10362 {
10363 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10364 first = this->last_vec_;
10365 second = name;
10366 }
10367 else if (out_vec > in_vec)
10368 {
10369 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10370 first = name;
10371 second = this->last_vec_;
10372 }
10373 if (err)
10374 {
10375 if (parameters->options().warn_mismatch())
10376 gold_error(_(err), first, second);
10377 out_attr[tag].set_type(0);
10378 }
10379 }
10380
10381 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10382 int in_struct = in_attr[tag].int_value() & 3;
10383 int out_struct = out_attr[tag].int_value() & 3;
10384 if (in_struct != out_struct)
10385 {
10386 err = NULL;
10387 if (in_struct == 0 || in_struct == 3)
10388 ;
10389 else if (out_struct == 0)
10390 {
10391 out_struct = in_struct;
10392 out_attr[tag].set_int_value(out_struct);
10393 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10394 this->last_struct_ = name;
10395 }
10396 else if (out_struct < in_struct)
10397 {
10398 err = N_("%s uses r3/r4 for small structure returns, "
10399 "%s uses memory");
10400 first = this->last_struct_;
10401 second = name;
10402 }
10403 else if (out_struct > in_struct)
10404 {
10405 err = N_("%s uses r3/r4 for small structure returns, "
10406 "%s uses memory");
10407 first = name;
10408 second = this->last_struct_;
10409 }
10410 if (err)
10411 {
10412 if (parameters->options().warn_mismatch())
10413 gold_error(_(err), first, second);
10414 out_attr[tag].set_type(0);
10415 }
10416 }
10417 }
10418
10419 // Merge Tag_compatibility attributes and any common GNU ones.
10420 this->attributes_section_data_->merge(name, pasd);
10421 }
10422
10423 // Emit any saved relocs, and mark toc entries using any of these
10424 // relocs as not optimizable.
10425
10426 template<int sh_type, int size, bool big_endian>
10427 void
10428 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10429 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10430 {
10431 if (size == 64
10432 && parameters->options().toc_optimize())
10433 {
10434 for (typename Copy_relocs<sh_type, size, big_endian>::
10435 Copy_reloc_entries::iterator p = this->entries_.begin();
10436 p != this->entries_.end();
10437 ++p)
10438 {
10439 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10440 entry = *p;
10441
10442 // If the symbol is no longer defined in a dynamic object,
10443 // then we emitted a COPY relocation. If it is still
10444 // dynamic then we'll need dynamic relocations and thus
10445 // can't optimize toc entries.
10446 if (entry.sym_->is_from_dynobj())
10447 {
10448 Powerpc_relobj<size, big_endian>* ppc_object
10449 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10450 if (entry.shndx_ == ppc_object->toc_shndx())
10451 ppc_object->set_no_toc_opt(entry.address_);
10452 }
10453 }
10454 }
10455
10456 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10457 }
10458
10459 // Return the value to use for a branch relocation.
10460
10461 template<int size, bool big_endian>
10462 bool
10463 Target_powerpc<size, big_endian>::symval_for_branch(
10464 const Symbol_table* symtab,
10465 const Sized_symbol<size>* gsym,
10466 Powerpc_relobj<size, big_endian>* object,
10467 Address *value,
10468 unsigned int *dest_shndx)
10469 {
10470 if (size == 32 || this->abiversion() >= 2)
10471 gold_unreachable();
10472 *dest_shndx = 0;
10473
10474 // If the symbol is defined in an opd section, ie. is a function
10475 // descriptor, use the function descriptor code entry address
10476 Powerpc_relobj<size, big_endian>* symobj = object;
10477 if (gsym != NULL
10478 && (gsym->source() != Symbol::FROM_OBJECT
10479 || gsym->object()->is_dynamic()))
10480 return true;
10481 if (gsym != NULL)
10482 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10483 unsigned int shndx = symobj->opd_shndx();
10484 if (shndx == 0)
10485 return true;
10486 Address opd_addr = symobj->get_output_section_offset(shndx);
10487 if (opd_addr == invalid_address)
10488 return true;
10489 opd_addr += symobj->output_section_address(shndx);
10490 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10491 {
10492 Address sec_off;
10493 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10494 if (symtab->is_section_folded(symobj, *dest_shndx))
10495 {
10496 Section_id folded
10497 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10498 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10499 *dest_shndx = folded.second;
10500 }
10501 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10502 if (sec_addr == invalid_address)
10503 return false;
10504
10505 sec_addr += symobj->output_section(*dest_shndx)->address();
10506 *value = sec_addr + sec_off;
10507 }
10508 return true;
10509 }
10510
10511 template<int size>
10512 static bool
10513 relative_value_is_known(const Sized_symbol<size>* gsym)
10514 {
10515 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10516 return false;
10517
10518 if (gsym->is_from_dynobj()
10519 || gsym->is_undefined()
10520 || gsym->is_preemptible())
10521 return false;
10522
10523 if (gsym->is_absolute())
10524 return !parameters->options().output_is_position_independent();
10525
10526 return true;
10527 }
10528
10529 template<int size>
10530 static bool
10531 relative_value_is_known(const Symbol_value<size>* psymval)
10532 {
10533 if (psymval->is_ifunc_symbol())
10534 return false;
10535
10536 bool is_ordinary;
10537 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10538
10539 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10540 }
10541
10542 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10543 // pld ra,symbol@got@pcrel
10544 // load/store rt,0(ra)
10545 // or
10546 // pla ra,symbol@pcrel
10547 // load/store rt,0(ra)
10548 // may be translated to
10549 // pload/pstore rt,symbol@pcrel
10550 // nop.
10551 // This function returns true if the optimization is possible, placing
10552 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10553 //
10554 // On entry to this function, the linker has already determined that
10555 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10556 // while *PINSN2 is the second instruction.
10557
10558 inline bool
10559 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10560 {
10561 uint32_t insn2 = *pinsn2 >> 32;
10562 uint64_t i1new;
10563
10564 // Check that regs match.
10565 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10566 return false;
10567
10568 switch ((insn2 >> 26) & 63)
10569 {
10570 default:
10571 return false;
10572
10573 case 32: // lwz
10574 case 34: // lbz
10575 case 36: // stw
10576 case 38: // stb
10577 case 40: // lhz
10578 case 42: // lha
10579 case 44: // sth
10580 case 48: // lfs
10581 case 50: // lfd
10582 case 52: // stfs
10583 case 54: // stfd
10584 // These are the PMLS cases, where we just need to tack a prefix
10585 // on the insn. Check that the D field is zero.
10586 if ((insn2 & 0xffff) != 0)
10587 return false;
10588 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10589 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10590 break;
10591
10592 case 58: // lwa, ld
10593 if ((insn2 & 0xfffd) != 0)
10594 return false;
10595 i1new = ((1ULL << 58) | (1ULL << 52)
10596 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10597 | (insn2 & (31ULL << 21)));
10598 break;
10599
10600 case 57: // lxsd, lxssp
10601 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10602 return false;
10603 i1new = ((1ULL << 58) | (1ULL << 52)
10604 | ((40ULL | (insn2 & 3)) << 26)
10605 | (insn2 & (31ULL << 21)));
10606 break;
10607
10608 case 61: // stxsd, stxssp, lxv, stxv
10609 if ((insn2 & 3) == 0)
10610 return false;
10611 else if ((insn2 & 3) >= 2)
10612 {
10613 if ((insn2 & 0xfffc) != 0)
10614 return false;
10615 i1new = ((1ULL << 58) | (1ULL << 52)
10616 | ((44ULL | (insn2 & 3)) << 26)
10617 | (insn2 & (31ULL << 21)));
10618 }
10619 else
10620 {
10621 if ((insn2 & 0xfff0) != 0)
10622 return false;
10623 i1new = ((1ULL << 58) | (1ULL << 52)
10624 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10625 | (insn2 & (31ULL << 21)));
10626 }
10627 break;
10628
10629 case 56: // lq
10630 if ((insn2 & 0xffff) != 0)
10631 return false;
10632 i1new = ((1ULL << 58) | (1ULL << 52)
10633 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10634 break;
10635
10636 case 62: // std, stq
10637 if ((insn2 & 0xfffd) != 0)
10638 return false;
10639 i1new = ((1ULL << 58) | (1ULL << 52)
10640 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10641 | (insn2 & (31ULL << 21)));
10642 break;
10643 }
10644
10645 *pinsn1 = i1new;
10646 *pinsn2 = (uint64_t) nop << 32;
10647 return true;
10648 }
10649
10650 // Perform a relocation.
10651
10652 template<int size, bool big_endian>
10653 inline bool
10654 Target_powerpc<size, big_endian>::Relocate::relocate(
10655 const Relocate_info<size, big_endian>* relinfo,
10656 unsigned int,
10657 Target_powerpc* target,
10658 Output_section* os,
10659 size_t relnum,
10660 const unsigned char* preloc,
10661 const Sized_symbol<size>* gsym,
10662 const Symbol_value<size>* psymval,
10663 unsigned char* view,
10664 Address address,
10665 section_size_type view_size)
10666 {
10667 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10668 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10669 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10670
10671 if (view == NULL)
10672 return true;
10673
10674 if (target->replace_tls_get_addr(gsym))
10675 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10676
10677 const elfcpp::Rela<size, big_endian> rela(preloc);
10678 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10679 Powerpc_relobj<size, big_endian>* const object
10680 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10681 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10682 {
10683 case Track_tls::NOT_EXPECTED:
10684 // No warning. This will result in really old code without tls
10685 // marker relocs being mis-optimised, but there shouldn't be too
10686 // much of that code around. The problem with warning is that
10687 // glibc and libphobos both construct direct calls to
10688 // __tls_get_addr in a way that is harmless.
10689 break;
10690 case Track_tls::EXPECTED:
10691 // We have already complained.
10692 break;
10693 case Track_tls::SKIP:
10694 if (is_plt16_reloc<size>(r_type)
10695 || r_type == elfcpp::R_POWERPC_PLTSEQ
10696 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10697 {
10698 Insn* iview = reinterpret_cast<Insn*>(view);
10699 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10700 }
10701 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10702 {
10703 Insn* iview = reinterpret_cast<Insn*>(view);
10704 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10705 }
10706 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10707 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10708 {
10709 Insn* iview = reinterpret_cast<Insn*>(view);
10710 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10711 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10712 }
10713 return true;
10714 case Track_tls::NORMAL:
10715 break;
10716 }
10717
10718 // Offset from start of insn to d-field reloc.
10719 const int d_offset = big_endian ? 2 : 0;
10720
10721 Address value = 0;
10722 bool has_stub_value = false;
10723 bool localentry0 = false;
10724 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10725 bool pltcall_to_direct = false;
10726
10727 if (is_plt16_reloc<size>(r_type)
10728 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10729 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10730 || r_type == elfcpp::R_POWERPC_PLTSEQ
10731 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10732 || r_type == elfcpp::R_POWERPC_PLTCALL
10733 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10734 {
10735 // It would be possible to replace inline plt calls with direct
10736 // calls if the PLTCALL is in range. The only difficulty is
10737 // that the decision depends on the PLTCALL reloc, and we don't
10738 // know the address of that instruction when processing others
10739 // in the sequence. So the decision needs to be made in
10740 // do_relax().
10741 pltcall_to_direct = !(gsym != NULL
10742 ? gsym->has_plt_offset()
10743 : object->local_has_plt_offset(r_sym));
10744 }
10745 else if ((gsym != NULL
10746 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10747 : psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
10748 && !is_got_reloc(r_type)
10749 && (!psymval->is_ifunc_symbol()
10750 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type,
10751 false)))
10752 {
10753 if (size == 64
10754 && gsym != NULL
10755 && target->abiversion() >= 2
10756 && !parameters->options().output_is_position_independent()
10757 && !is_branch_reloc<size>(r_type))
10758 {
10759 Address off = target->glink_section()->find_global_entry(gsym);
10760 if (off != invalid_address)
10761 {
10762 value = target->glink_section()->global_entry_address() + off;
10763 has_stub_value = true;
10764 }
10765 }
10766 else
10767 {
10768 Stub_table<size, big_endian>* stub_table = NULL;
10769 if (target->stub_tables().size() == 1)
10770 stub_table = target->stub_tables()[0];
10771 if (stub_table == NULL
10772 && !(size == 32
10773 && gsym != NULL
10774 && !parameters->options().output_is_position_independent()
10775 && !is_branch_reloc<size>(r_type)))
10776 stub_table = object->stub_table(relinfo->data_shndx);
10777 if (stub_table == NULL)
10778 {
10779 // This is a ref from a data section to an ifunc symbol,
10780 // or a non-branch reloc for which we always want to use
10781 // one set of stubs for resolving function addresses.
10782 if (target->stub_tables().size() != 0)
10783 stub_table = target->stub_tables()[0];
10784 }
10785 if (stub_table != NULL)
10786 {
10787 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10788 if (gsym != NULL)
10789 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10790 rela.get_r_addend());
10791 else
10792 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10793 rela.get_r_addend());
10794 if (ent != NULL)
10795 {
10796 value = stub_table->stub_address() + ent->off_;
10797 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10798 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10799 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10800 if (size == 64)
10801 {
10802 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10803 {
10804 if (!ent->notoc_)
10805 value += ent->p9off_;
10806 }
10807 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10808 value += ent->p9off_;
10809 else
10810 value += ent->tocoff_;
10811 }
10812 if (size == 64
10813 && ent->r2save_
10814 && !(gsym != NULL
10815 && target->is_tls_get_addr_opt(gsym)))
10816 {
10817 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10818 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10819 {
10820 if (!(target->power10_stubs()
10821 && target->power10_stubs_auto()))
10822 value += 4;
10823 }
10824 else if (relnum < reloc_count - 1)
10825 {
10826 Reltype next_rela(preloc + reloc_size);
10827 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10828 == elfcpp::R_PPC64_TOCSAVE
10829 && (next_rela.get_r_offset()
10830 == rela.get_r_offset() + 4))
10831 value += 4;
10832 }
10833 }
10834 localentry0 = ent->localentry0_;
10835 has_stub_value = true;
10836 }
10837 }
10838 }
10839 // We don't care too much about bogus debug references to
10840 // non-local functions, but otherwise there had better be a plt
10841 // call stub or global entry stub as appropriate.
10842 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10843 }
10844
10845 if (!pltcall_to_direct && (is_plt16_reloc<size>(r_type)
10846 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10847 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10848 {
10849 const Output_data_plt_powerpc<size, big_endian>* plt;
10850 if (gsym)
10851 value = target->plt_off(gsym, &plt);
10852 else
10853 value = target->plt_off(object, r_sym, &plt);
10854 value += plt->address();
10855
10856 if (size == 64)
10857 {
10858 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10859 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10860 value -= target->toc_pointer();
10861 }
10862 else if (parameters->options().output_is_position_independent())
10863 {
10864 if (rela.get_r_addend() >= 32768)
10865 {
10866 unsigned int got2 = object->got2_shndx();
10867 value -= (object->get_output_section_offset(got2)
10868 + object->output_section(got2)->address()
10869 + rela.get_r_addend());
10870 }
10871 else
10872 value -= target->toc_pointer();
10873 }
10874 }
10875 else if (pltcall_to_direct
10876 && (is_plt16_reloc<size>(r_type)
10877 || r_type == elfcpp::R_POWERPC_PLTSEQ
10878 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10879 {
10880 Insn* iview = reinterpret_cast<Insn*>(view);
10881 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10882 r_type = elfcpp::R_POWERPC_NONE;
10883 }
10884 else if (pltcall_to_direct
10885 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10886 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10887 {
10888 Insn* iview = reinterpret_cast<Insn*>(view);
10889 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10890 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10891 r_type = elfcpp::R_POWERPC_NONE;
10892 }
10893 else if (is_got_reloc(r_type))
10894 {
10895 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10896 Got_type got_type = ((size == 32
10897 || r_type == elfcpp::R_POWERPC_GOT16
10898 || r_type == elfcpp::R_PPC64_GOT16_DS)
10899 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10900 if (gsym != NULL)
10901 value = gsym->got_offset(got_type, addend);
10902 else
10903 value = object->local_got_offset(r_sym, got_type, addend);
10904 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10905 value += target->got_section(got_type)->address();
10906 else
10907 value -= target->got_base_offset(got_type);
10908 }
10909 else if (r_type == elfcpp::R_PPC64_TOC)
10910 {
10911 value = target->toc_pointer();
10912 }
10913 else if (gsym != NULL
10914 && (r_type == elfcpp::R_POWERPC_REL24
10915 || r_type == elfcpp::R_PPC_PLTREL24)
10916 && has_stub_value)
10917 {
10918 if (size == 64)
10919 {
10920 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10921 Valtype* wv = reinterpret_cast<Valtype*>(view);
10922 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10923 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10924 {
10925 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10926 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10927 if ((insn & 1) != 0
10928 && (insn2 == nop
10929 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10930 {
10931 elfcpp::Swap<32, big_endian>::
10932 writeval(wv + 1, ld_2_1 + target->stk_toc());
10933 can_plt_call = true;
10934 }
10935 }
10936 if (!can_plt_call)
10937 {
10938 // If we don't have a branch and link followed by a nop,
10939 // we can't go via the plt because there is no place to
10940 // put a toc restoring instruction.
10941 // Unless we know we won't be returning.
10942 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10943 can_plt_call = true;
10944 }
10945 if (!can_plt_call)
10946 {
10947 // g++ as of 20130507 emits self-calls without a
10948 // following nop. This is arguably wrong since we have
10949 // conflicting information. On the one hand a global
10950 // symbol and on the other a local call sequence, but
10951 // don't error for this special case.
10952 // It isn't possible to cheaply verify we have exactly
10953 // such a call. Allow all calls to the same section.
10954 bool ok = false;
10955 Address code = value;
10956 if (gsym->source() == Symbol::FROM_OBJECT
10957 && gsym->object() == object)
10958 {
10959 unsigned int dest_shndx = 0;
10960 if (target->abiversion() < 2)
10961 {
10962 Address addend = rela.get_r_addend();
10963 code = psymval->value(object, addend);
10964 target->symval_for_branch(relinfo->symtab, gsym, object,
10965 &code, &dest_shndx);
10966 }
10967 bool is_ordinary;
10968 if (dest_shndx == 0)
10969 dest_shndx = gsym->shndx(&is_ordinary);
10970 ok = dest_shndx == relinfo->data_shndx;
10971 }
10972 if (!ok)
10973 {
10974 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10975 _("call lacks nop, can't restore toc; "
10976 "recompile with -fPIC"));
10977 value = code;
10978 }
10979 }
10980 }
10981 }
10982 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10983 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10984 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10985 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10986 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10987 {
10988 // First instruction of a global dynamic sequence, arg setup insn.
10989 bool final = gsym == NULL || gsym->final_value_is_known();
10990 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10991 Got_type got_type = ((size == 32
10992 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10993 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10994 if (tls_type == tls::TLSOPT_NONE)
10995 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
10996 else if (tls_type == tls::TLSOPT_TO_IE)
10997 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10998 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
10999 {
11000 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11001 if (gsym != NULL)
11002 value = gsym->got_offset(got_type, addend);
11003 else
11004 value = object->local_got_offset(r_sym, got_type, addend);
11005 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11006 value += target->got_section(got_type)->address();
11007 else
11008 value -= target->got_base_offset(got_type);
11009 }
11010 if (tls_type == tls::TLSOPT_TO_IE)
11011 {
11012 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11013 {
11014 Insn* iview = reinterpret_cast<Insn*>(view);
11015 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11016 pinsn <<= 32;
11017 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11018 // pla -> pld
11019 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
11020 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11021 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11022 pinsn & 0xffffffff);
11023 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
11024 }
11025 else
11026 {
11027 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11028 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11029 {
11030 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11031 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11032 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
11033 if (size == 32)
11034 insn |= 32 << 26; // lwz
11035 else
11036 insn |= 58 << 26; // ld
11037 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11038 }
11039 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11040 - elfcpp::R_POWERPC_GOT_TLSGD16);
11041 }
11042 }
11043 else if (tls_type == tls::TLSOPT_TO_LE)
11044 {
11045 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11046 {
11047 Insn* iview = reinterpret_cast<Insn*>(view);
11048 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11049 pinsn <<= 32;
11050 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11051 // pla pcrel -> paddi r13
11052 pinsn += (-1ULL << 52) + (13ULL << 16);
11053 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11054 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11055 pinsn & 0xffffffff);
11056 r_type = elfcpp::R_PPC64_TPREL34;
11057 value = psymval->value(object, rela.get_r_addend());
11058 }
11059 else
11060 {
11061 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11062 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11063 {
11064 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11065 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11066 insn &= (1 << 26) - (1 << 21); // extract rt
11067 if (size == 32)
11068 insn |= addis_0_2;
11069 else
11070 insn |= addis_0_13;
11071 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11072 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11073 value = psymval->value(object, rela.get_r_addend());
11074 }
11075 else
11076 {
11077 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11078 Insn insn = nop;
11079 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11080 r_type = elfcpp::R_POWERPC_NONE;
11081 }
11082 }
11083 }
11084 }
11085 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11086 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11087 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11088 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
11089 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11090 {
11091 // First instruction of a local dynamic sequence, arg setup insn.
11092 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11093 if (tls_type == tls::TLSOPT_NONE)
11094 {
11095 value = target->tlsld_got_offset();
11096 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11097 value += target->got_section(GOT_TYPE_SMALL)->address();
11098 else
11099 value -= target->got_base_offset(GOT_TYPE_SMALL);
11100 }
11101 else
11102 {
11103 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11104 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11105 {
11106 Insn* iview = reinterpret_cast<Insn*>(view);
11107 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11108 pinsn <<= 32;
11109 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11110 // pla pcrel -> paddi r13
11111 pinsn += (-1ULL << 52) + (13ULL << 16);
11112 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11113 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11114 pinsn & 0xffffffff);
11115 r_type = elfcpp::R_PPC64_TPREL34;
11116 value = dtp_offset;
11117 }
11118 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11119 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11120 {
11121 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11122 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11123 insn &= (1 << 26) - (1 << 21); // extract rt
11124 if (size == 32)
11125 insn |= addis_0_2;
11126 else
11127 insn |= addis_0_13;
11128 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11129 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11130 value = dtp_offset;
11131 }
11132 else
11133 {
11134 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11135 Insn insn = nop;
11136 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11137 r_type = elfcpp::R_POWERPC_NONE;
11138 }
11139 }
11140 }
11141 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11142 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11143 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
11144 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
11145 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11146 {
11147 // Accesses relative to a local dynamic sequence address,
11148 // no optimisation here.
11149 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11150 Got_type got_type = ((size == 32
11151 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11152 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
11153 if (gsym != NULL)
11154 value = gsym->got_offset(got_type, addend);
11155 else
11156 value = object->local_got_offset(r_sym, got_type, addend);
11157 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11158 value += target->got_section(got_type)->address();
11159 else
11160 value -= target->got_base_offset(got_type);
11161 }
11162 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11163 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11164 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11165 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
11166 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11167 {
11168 // First instruction of initial exec sequence.
11169 bool final = gsym == NULL || gsym->final_value_is_known();
11170 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11171 if (tls_type == tls::TLSOPT_NONE)
11172 {
11173 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11174 Got_type got_type = ((size == 32
11175 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11176 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
11177 if (gsym != NULL)
11178 value = gsym->got_offset(got_type, addend);
11179 else
11180 value = object->local_got_offset(r_sym, got_type, addend);
11181 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11182 value += target->got_section(got_type)->address();
11183 else
11184 value -= target->got_base_offset(got_type);
11185 }
11186 else
11187 {
11188 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11189 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11190 {
11191 Insn* iview = reinterpret_cast<Insn*>(view);
11192 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11193 pinsn <<= 32;
11194 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11195 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11196 pinsn += ((2ULL << 56) + (-1ULL << 52)
11197 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11198 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11199 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11200 pinsn & 0xffffffff);
11201 r_type = elfcpp::R_PPC64_TPREL34;
11202 value = psymval->value(object, rela.get_r_addend());
11203 }
11204 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11205 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11206 {
11207 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11208 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11209 insn &= (1 << 26) - (1 << 21); // extract rt from ld
11210 if (size == 32)
11211 insn |= addis_0_2;
11212 else
11213 insn |= addis_0_13;
11214 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11215 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11216 value = psymval->value(object, rela.get_r_addend());
11217 }
11218 else
11219 {
11220 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11221 Insn insn = nop;
11222 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11223 r_type = elfcpp::R_POWERPC_NONE;
11224 }
11225 }
11226 }
11227 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11228 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11229 {
11230 // Second instruction of a global dynamic sequence,
11231 // the __tls_get_addr call
11232 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11233 bool final = gsym == NULL || gsym->final_value_is_known();
11234 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
11235 if (tls_type != tls::TLSOPT_NONE)
11236 {
11237 if (tls_type == tls::TLSOPT_TO_IE)
11238 {
11239 Insn* iview = reinterpret_cast<Insn*>(view);
11240 Insn insn = add_3_3_13;
11241 if (size == 32)
11242 insn = add_3_3_2;
11243 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11244 r_type = elfcpp::R_POWERPC_NONE;
11245 }
11246 else
11247 {
11248 bool is_pcrel = false;
11249 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11250 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11251 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11252 if (relnum < reloc_count - 1)
11253 {
11254 Reltype next_rela(preloc + reloc_size);
11255 unsigned int r_type2
11256 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11257 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11258 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11259 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11260 && next_rela.get_r_offset() == rela.get_r_offset())
11261 is_pcrel = true;
11262 }
11263 Insn* iview = reinterpret_cast<Insn*>(view);
11264 if (is_pcrel)
11265 {
11266 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11267 r_type = elfcpp::R_POWERPC_NONE;
11268 }
11269 else
11270 {
11271 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11272 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11273 view += d_offset;
11274 value = psymval->value(object, rela.get_r_addend());
11275 }
11276 }
11277 this->skip_next_tls_get_addr_call();
11278 }
11279 }
11280 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11281 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11282 {
11283 // Second instruction of a local dynamic sequence,
11284 // the __tls_get_addr call
11285 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11286 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11287 if (tls_type == tls::TLSOPT_TO_LE)
11288 {
11289 bool is_pcrel = false;
11290 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11291 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11292 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11293 if (relnum < reloc_count - 1)
11294 {
11295 Reltype next_rela(preloc + reloc_size);
11296 unsigned int r_type2
11297 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11298 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11299 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11300 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11301 && next_rela.get_r_offset() == rela.get_r_offset())
11302 is_pcrel = true;
11303 }
11304 Insn* iview = reinterpret_cast<Insn*>(view);
11305 if (is_pcrel)
11306 {
11307 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11308 r_type = elfcpp::R_POWERPC_NONE;
11309 }
11310 else
11311 {
11312 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11313 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11314 view += d_offset;
11315 value = dtp_offset;
11316 }
11317 this->skip_next_tls_get_addr_call();
11318 }
11319 }
11320 else if (r_type == elfcpp::R_POWERPC_TLS)
11321 {
11322 // Second instruction of an initial exec sequence
11323 bool final = gsym == NULL || gsym->final_value_is_known();
11324 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11325 if (tls_type == tls::TLSOPT_TO_LE)
11326 {
11327 Address roff = rela.get_r_offset() & 3;
11328 Insn* iview = reinterpret_cast<Insn*>(view - roff);
11329 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11330 unsigned int reg = size == 32 ? 2 : 13;
11331 insn = at_tls_transform(insn, reg);
11332 gold_assert(insn != 0);
11333 if (roff == 0)
11334 {
11335 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11336 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11337 view += d_offset;
11338 value = psymval->value(object, rela.get_r_addend());
11339 }
11340 else if (roff == 1)
11341 {
11342 // For pcrel IE to LE we already have the full offset
11343 // and thus don't need an addi here. A nop or mr will do.
11344 if ((insn & (0x3f << 26)) == 14 << 26)
11345 {
11346 // Extract regs from addi rt,ra,si.
11347 unsigned int rt = (insn >> 21) & 0x1f;
11348 unsigned int ra = (insn >> 16) & 0x1f;
11349 if (rt == ra)
11350 insn = nop;
11351 else
11352 {
11353 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11354 insn = (rt << 16) | (ra << 21) | (ra << 11);
11355 insn |= (31u << 26) | (444u << 1);
11356 }
11357 }
11358 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11359 r_type = elfcpp::R_POWERPC_NONE;
11360 }
11361 }
11362 }
11363 else if (!has_stub_value)
11364 {
11365 if (pltcall_to_direct && (r_type == elfcpp::R_POWERPC_PLTCALL
11366 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11367 {
11368 // PLTCALL without plt entry => convert to direct call
11369 Insn* iview = reinterpret_cast<Insn*>(view);
11370 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11371 insn = (insn & 1) | b;
11372 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11373 if (size == 32)
11374 r_type = elfcpp::R_PPC_PLTREL24;
11375 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11376 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11377 else
11378 r_type = elfcpp::R_POWERPC_REL24;
11379 }
11380 Address addend = 0;
11381 if (!(size == 32
11382 && (r_type == elfcpp::R_PPC_PLTREL24
11383 || r_type == elfcpp::R_POWERPC_PLT16_LO
11384 || r_type == elfcpp::R_POWERPC_PLT16_HI
11385 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11386 addend = rela.get_r_addend();
11387 value = psymval->value(object, addend);
11388 unsigned int local_ent = 0;
11389 if (size == 64 && is_branch_reloc<size>(r_type))
11390 {
11391 if (target->abiversion() >= 2)
11392 {
11393 if (gsym != NULL)
11394 local_ent = object->ppc64_local_entry_offset(gsym);
11395 else
11396 local_ent = object->ppc64_local_entry_offset(r_sym);
11397 }
11398 else
11399 {
11400 unsigned int dest_shndx;
11401 target->symval_for_branch(relinfo->symtab, gsym, object,
11402 &value, &dest_shndx);
11403 }
11404 }
11405 Address max_branch = max_branch_delta<size>(r_type);
11406 if (max_branch != 0
11407 && (value + local_ent - address + max_branch >= 2 * max_branch
11408 || (size == 64
11409 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11410 || r_type == elfcpp::R_PPC64_REL24_NOTOC)
11411 && (gsym != NULL
11412 ? object->ppc64_needs_toc(gsym)
11413 : object->ppc64_needs_toc(r_sym)))))
11414 {
11415 Stub_table<size, big_endian>* stub_table
11416 = object->stub_table(relinfo->data_shndx);
11417 if (stub_table != NULL)
11418 {
11419 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11420 = stub_table->find_long_branch_entry(value);
11421 if (ent != NULL)
11422 {
11423 if (ent->save_res_)
11424 value = (value - target->savres_section()->address()
11425 + stub_table->stub_address()
11426 + stub_table->plt_size()
11427 + stub_table->branch_size());
11428 else
11429 {
11430 value = (stub_table->stub_address()
11431 + stub_table->plt_size()
11432 + ent->off_);
11433 if (size == 64)
11434 {
11435 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11436 {
11437 if (!ent->notoc_)
11438 value += ent->p9off_;
11439 }
11440 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11441 value += ent->p9off_;
11442 else
11443 value += ent->tocoff_;
11444 }
11445 }
11446 has_stub_value = true;
11447 }
11448 }
11449 }
11450 if (!has_stub_value)
11451 value += local_ent;
11452 }
11453
11454 switch (r_type)
11455 {
11456 case elfcpp::R_PPC64_REL24_NOTOC:
11457 if (size == 32)
11458 break;
11459 // Fall through.
11460 case elfcpp::R_PPC64_REL24_P9NOTOC:
11461 case elfcpp::R_PPC64_REL64:
11462 case elfcpp::R_POWERPC_REL32:
11463 case elfcpp::R_POWERPC_REL24:
11464 case elfcpp::R_PPC_PLTREL24:
11465 case elfcpp::R_PPC_LOCAL24PC:
11466 case elfcpp::R_POWERPC_REL16:
11467 case elfcpp::R_POWERPC_REL16_LO:
11468 case elfcpp::R_POWERPC_REL16_HI:
11469 case elfcpp::R_POWERPC_REL16_HA:
11470 case elfcpp::R_POWERPC_REL16DX_HA:
11471 case elfcpp::R_PPC64_REL16_HIGH:
11472 case elfcpp::R_PPC64_REL16_HIGHA:
11473 case elfcpp::R_PPC64_REL16_HIGHER:
11474 case elfcpp::R_PPC64_REL16_HIGHERA:
11475 case elfcpp::R_PPC64_REL16_HIGHEST:
11476 case elfcpp::R_PPC64_REL16_HIGHESTA:
11477 case elfcpp::R_POWERPC_REL14:
11478 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11479 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11480 case elfcpp::R_PPC64_PCREL34:
11481 case elfcpp::R_PPC64_GOT_PCREL34:
11482 case elfcpp::R_PPC64_PLT_PCREL34:
11483 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11484 case elfcpp::R_PPC64_PCREL28:
11485 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11486 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11487 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11488 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11489 case elfcpp::R_PPC64_REL16_HIGHER34:
11490 case elfcpp::R_PPC64_REL16_HIGHERA34:
11491 case elfcpp::R_PPC64_REL16_HIGHEST34:
11492 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11493 value -= address;
11494 break;
11495
11496 case elfcpp::R_PPC64_TOC16:
11497 case elfcpp::R_PPC64_TOC16_LO:
11498 case elfcpp::R_PPC64_TOC16_HI:
11499 case elfcpp::R_PPC64_TOC16_HA:
11500 case elfcpp::R_PPC64_TOC16_DS:
11501 case elfcpp::R_PPC64_TOC16_LO_DS:
11502 // Subtract the TOC base address.
11503 value -= target->toc_pointer();
11504 break;
11505
11506 case elfcpp::R_POWERPC_SECTOFF:
11507 case elfcpp::R_POWERPC_SECTOFF_LO:
11508 case elfcpp::R_POWERPC_SECTOFF_HI:
11509 case elfcpp::R_POWERPC_SECTOFF_HA:
11510 case elfcpp::R_PPC64_SECTOFF_DS:
11511 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11512 if (os != NULL)
11513 value -= os->address();
11514 break;
11515
11516 case elfcpp::R_PPC64_TPREL16_DS:
11517 case elfcpp::R_PPC64_TPREL16_LO_DS:
11518 case elfcpp::R_PPC64_TPREL16_HIGH:
11519 case elfcpp::R_PPC64_TPREL16_HIGHA:
11520 if (size != 64)
11521 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11522 break;
11523 // Fall through.
11524 case elfcpp::R_POWERPC_TPREL16:
11525 case elfcpp::R_POWERPC_TPREL16_LO:
11526 case elfcpp::R_POWERPC_TPREL16_HI:
11527 case elfcpp::R_POWERPC_TPREL16_HA:
11528 case elfcpp::R_POWERPC_TPREL:
11529 case elfcpp::R_PPC64_TPREL16_HIGHER:
11530 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11531 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11532 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11533 case elfcpp::R_PPC64_TPREL34:
11534 // tls symbol values are relative to tls_segment()->vaddr()
11535 value -= tp_offset;
11536 break;
11537
11538 case elfcpp::R_PPC64_DTPREL16_DS:
11539 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11540 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11541 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11542 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11543 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11544 if (size != 64)
11545 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11546 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11547 break;
11548 // Fall through.
11549 case elfcpp::R_POWERPC_DTPREL16:
11550 case elfcpp::R_POWERPC_DTPREL16_LO:
11551 case elfcpp::R_POWERPC_DTPREL16_HI:
11552 case elfcpp::R_POWERPC_DTPREL16_HA:
11553 case elfcpp::R_POWERPC_DTPREL:
11554 case elfcpp::R_PPC64_DTPREL16_HIGH:
11555 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11556 case elfcpp::R_PPC64_DTPREL34:
11557 // tls symbol values are relative to tls_segment()->vaddr()
11558 value -= dtp_offset;
11559 break;
11560
11561 case elfcpp::R_PPC64_ADDR64_LOCAL:
11562 if (gsym != NULL)
11563 value += object->ppc64_local_entry_offset(gsym);
11564 else
11565 value += object->ppc64_local_entry_offset(r_sym);
11566 break;
11567
11568 default:
11569 break;
11570 }
11571
11572 Insn branch_bit = 0;
11573 switch (r_type)
11574 {
11575 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11576 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11577 branch_bit = 1 << 21;
11578 // Fall through.
11579 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11580 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11581 {
11582 Insn* iview = reinterpret_cast<Insn*>(view);
11583 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11584 insn &= ~(1 << 21);
11585 insn |= branch_bit;
11586 if (this->is_isa_v2)
11587 {
11588 // Set 'a' bit. This is 0b00010 in BO field for branch
11589 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11590 // for branch on CTR insns (BO == 1a00t or 1a01t).
11591 if ((insn & (0x14 << 21)) == (0x04 << 21))
11592 insn |= 0x02 << 21;
11593 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11594 insn |= 0x08 << 21;
11595 else
11596 break;
11597 }
11598 else
11599 {
11600 // Invert 'y' bit if not the default.
11601 if (static_cast<Signed_address>(value) < 0)
11602 insn ^= 1 << 21;
11603 }
11604 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11605 }
11606 break;
11607
11608 case elfcpp::R_POWERPC_PLT16_HA:
11609 if (size == 32
11610 && !parameters->options().output_is_position_independent())
11611 {
11612 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11613 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11614
11615 // Convert addis to lis.
11616 if ((insn & (0x3f << 26)) == 15u << 26
11617 && (insn & (0x1f << 16)) != 0)
11618 {
11619 insn &= ~(0x1f << 16);
11620 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11621 }
11622 }
11623 break;
11624
11625 default:
11626 break;
11627 }
11628
11629 if (gsym
11630 ? relative_value_is_known(gsym)
11631 : relative_value_is_known(psymval))
11632 {
11633 Insn* iview;
11634 Insn* iview2;
11635 Insn insn;
11636 uint64_t pinsn, pinsn2;
11637
11638 switch (r_type)
11639 {
11640 default:
11641 break;
11642
11643 // Multi-instruction sequences that access the GOT/TOC can
11644 // be optimized, eg.
11645 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11646 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11647 // and
11648 // addis ra,r2,0; addi rb,ra,x@toc@l;
11649 // to nop; addi rb,r2,x@toc;
11650 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11651 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11652 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11653 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11654 case elfcpp::R_POWERPC_GOT16_HA:
11655 case elfcpp::R_PPC64_TOC16_HA:
11656 if (size == 64 && parameters->options().toc_optimize())
11657 {
11658 iview = reinterpret_cast<Insn*>(view - d_offset);
11659 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11660 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11661 && object->make_toc_relative(target, &value))
11662 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11663 && object->make_got_relative(target, psymval,
11664 rela.get_r_addend(),
11665 &value)))
11666 {
11667 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11668 == ((15u << 26) | (2 << 16)));
11669 }
11670 if (((insn & ((0x3f << 26) | 0x1f << 16))
11671 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11672 && value + 0x8000 < 0x10000)
11673 {
11674 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11675 return true;
11676 }
11677 }
11678 break;
11679
11680 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11681 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11682 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11683 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11684 case elfcpp::R_POWERPC_GOT16_LO:
11685 case elfcpp::R_PPC64_GOT16_LO_DS:
11686 case elfcpp::R_PPC64_TOC16_LO:
11687 case elfcpp::R_PPC64_TOC16_LO_DS:
11688 if (size == 64 && parameters->options().toc_optimize())
11689 {
11690 iview = reinterpret_cast<Insn*>(view - d_offset);
11691 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11692 bool changed = false;
11693 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11694 && object->make_toc_relative(target, &value))
11695 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11696 && object->make_got_relative(target, psymval,
11697 rela.get_r_addend(),
11698 &value)))
11699 {
11700 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11701 insn ^= (14u << 26) ^ (58u << 26);
11702 r_type = elfcpp::R_PPC64_TOC16_LO;
11703 changed = true;
11704 }
11705 if (ok_lo_toc_insn(insn, r_type)
11706 && value + 0x8000 < 0x10000)
11707 {
11708 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11709 {
11710 // Transform addic to addi when we change reg.
11711 insn &= ~((0x3f << 26) | (0x1f << 16));
11712 insn |= (14u << 26) | (2 << 16);
11713 }
11714 else
11715 {
11716 insn &= ~(0x1f << 16);
11717 insn |= 2 << 16;
11718 }
11719 changed = true;
11720 }
11721 if (changed)
11722 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11723 }
11724 break;
11725
11726 case elfcpp::R_PPC64_GOT_PCREL34:
11727 if (size == 64 && parameters->options().toc_optimize())
11728 {
11729 iview = reinterpret_cast<Insn*>(view);
11730 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11731 pinsn <<= 32;
11732 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11733 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11734 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11735 break;
11736
11737 Address relval = psymval->value(object, rela.get_r_addend());
11738 relval -= address;
11739 if (relval + (1ULL << 33) < 1ULL << 34)
11740 {
11741 value = relval;
11742 // Replace with paddi
11743 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11744 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11745 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11746 pinsn & 0xffffffff);
11747 goto pcrelopt;
11748 }
11749 }
11750 break;
11751
11752 case elfcpp::R_PPC64_PCREL34:
11753 if (size == 64)
11754 {
11755 iview = reinterpret_cast<Insn*>(view);
11756 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11757 pinsn <<= 32;
11758 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11759 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11760 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11761 | (14ULL << 26) /* paddi */))
11762 break;
11763
11764 pcrelopt:
11765 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11766 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11767 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11768 if (relnum >= reloc_count - 1)
11769 break;
11770
11771 Reltype next_rela(preloc + reloc_size);
11772 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11773 != elfcpp::R_PPC64_PCREL_OPT)
11774 || next_rela.get_r_offset() != rela.get_r_offset())
11775 break;
11776
11777 Address off = next_rela.get_r_addend();
11778 if (off == 0)
11779 off = 8; // zero means next insn.
11780 if (off + rela.get_r_offset() + 4 > view_size)
11781 break;
11782
11783 iview2 = reinterpret_cast<Insn*>(view + off);
11784 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11785 pinsn2 <<= 32;
11786 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11787 break;
11788 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11789 {
11790 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11791 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11792 pinsn & 0xffffffff);
11793 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11794 }
11795 }
11796 break;
11797
11798 case elfcpp::R_POWERPC_TPREL16_HA:
11799 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11800 {
11801 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11802 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11803 return true;
11804 }
11805 break;
11806
11807 case elfcpp::R_PPC64_TPREL16_LO_DS:
11808 if (size == 32)
11809 // R_PPC_TLSGD, R_PPC_TLSLD
11810 break;
11811 // Fall through.
11812 case elfcpp::R_POWERPC_TPREL16_LO:
11813 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11814 {
11815 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11816 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11817 insn &= ~(0x1f << 16);
11818 insn |= (size == 32 ? 2 : 13) << 16;
11819 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11820 }
11821 break;
11822
11823 case elfcpp::R_PPC64_ENTRY:
11824 if (size == 64)
11825 {
11826 value = target->toc_pointer();
11827 if (value + 0x80008000 <= 0xffffffff
11828 && !parameters->options().output_is_position_independent())
11829 {
11830 Insn* iview = reinterpret_cast<Insn*>(view);
11831 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11832 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11833
11834 if ((insn1 & ~0xfffc) == ld_2_12
11835 && insn2 == add_2_2_12)
11836 {
11837 insn1 = lis_2 + ha(value);
11838 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11839 insn2 = addi_2_2 + l(value);
11840 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11841 return true;
11842 }
11843 }
11844 else
11845 {
11846 value -= address;
11847 if (value + 0x80008000 <= 0xffffffff)
11848 {
11849 Insn* iview = reinterpret_cast<Insn*>(view);
11850 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11851 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11852
11853 if ((insn1 & ~0xfffc) == ld_2_12
11854 && insn2 == add_2_2_12)
11855 {
11856 insn1 = addis_2_12 + ha(value);
11857 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11858 insn2 = addi_2_2 + l(value);
11859 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11860 return true;
11861 }
11862 }
11863 }
11864 }
11865 break;
11866
11867 case elfcpp::R_POWERPC_REL16_LO:
11868 // If we are generating a non-PIC executable, edit
11869 // 0: addis 2,12,.TOC.-0b@ha
11870 // addi 2,2,.TOC.-0b@l
11871 // used by ELFv2 global entry points to set up r2, to
11872 // lis 2,.TOC.@ha
11873 // addi 2,2,.TOC.@l
11874 // if .TOC. is in range. */
11875 if (size == 64
11876 && value + address - 4 + 0x80008000 <= 0xffffffff
11877 && relnum + 1 > 1
11878 && preloc != NULL
11879 && target->abiversion() >= 2
11880 && !parameters->options().output_is_position_independent()
11881 && rela.get_r_addend() == d_offset + 4
11882 && gsym != NULL
11883 && strcmp(gsym->name(), ".TOC.") == 0)
11884 {
11885 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11886 Reltype prev_rela(preloc - reloc_size);
11887 if ((prev_rela.get_r_info()
11888 == elfcpp::elf_r_info<size>(r_sym,
11889 elfcpp::R_POWERPC_REL16_HA))
11890 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11891 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11892 {
11893 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11894 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11895 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11896
11897 if ((insn1 & 0xffff0000) == addis_2_12
11898 && (insn2 & 0xffff0000) == addi_2_2)
11899 {
11900 insn1 = lis_2 + ha(value + address - 4);
11901 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11902 insn2 = addi_2_2 + l(value + address - 4);
11903 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11904 if (relinfo->rr)
11905 {
11906 relinfo->rr->set_strategy(relnum - 1,
11907 Relocatable_relocs::RELOC_SPECIAL);
11908 relinfo->rr->set_strategy(relnum,
11909 Relocatable_relocs::RELOC_SPECIAL);
11910 }
11911 return true;
11912 }
11913 }
11914 }
11915 break;
11916 }
11917 }
11918
11919 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11920 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11921 switch (r_type)
11922 {
11923 case elfcpp::R_POWERPC_ADDR32:
11924 case elfcpp::R_POWERPC_UADDR32:
11925 if (size == 64)
11926 overflow = Reloc::CHECK_BITFIELD;
11927 break;
11928
11929 case elfcpp::R_POWERPC_REL32:
11930 case elfcpp::R_POWERPC_REL16DX_HA:
11931 if (size == 64)
11932 overflow = Reloc::CHECK_SIGNED;
11933 break;
11934
11935 case elfcpp::R_POWERPC_UADDR16:
11936 overflow = Reloc::CHECK_BITFIELD;
11937 break;
11938
11939 case elfcpp::R_POWERPC_ADDR16:
11940 // We really should have three separate relocations,
11941 // one for 16-bit data, one for insns with 16-bit signed fields,
11942 // and one for insns with 16-bit unsigned fields.
11943 overflow = Reloc::CHECK_BITFIELD;
11944 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11945 overflow = Reloc::CHECK_LOW_INSN;
11946 break;
11947
11948 case elfcpp::R_POWERPC_ADDR16_HI:
11949 case elfcpp::R_POWERPC_ADDR16_HA:
11950 case elfcpp::R_POWERPC_GOT16_HI:
11951 case elfcpp::R_POWERPC_GOT16_HA:
11952 case elfcpp::R_POWERPC_PLT16_HI:
11953 case elfcpp::R_POWERPC_PLT16_HA:
11954 case elfcpp::R_POWERPC_SECTOFF_HI:
11955 case elfcpp::R_POWERPC_SECTOFF_HA:
11956 case elfcpp::R_PPC64_TOC16_HI:
11957 case elfcpp::R_PPC64_TOC16_HA:
11958 case elfcpp::R_PPC64_PLTGOT16_HI:
11959 case elfcpp::R_PPC64_PLTGOT16_HA:
11960 case elfcpp::R_POWERPC_TPREL16_HI:
11961 case elfcpp::R_POWERPC_TPREL16_HA:
11962 case elfcpp::R_POWERPC_DTPREL16_HI:
11963 case elfcpp::R_POWERPC_DTPREL16_HA:
11964 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11965 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11966 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11967 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11968 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11969 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11970 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11971 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11972 case elfcpp::R_POWERPC_REL16_HI:
11973 case elfcpp::R_POWERPC_REL16_HA:
11974 if (size != 32)
11975 overflow = Reloc::CHECK_HIGH_INSN;
11976 break;
11977
11978 case elfcpp::R_POWERPC_REL16:
11979 case elfcpp::R_PPC64_TOC16:
11980 case elfcpp::R_POWERPC_GOT16:
11981 case elfcpp::R_POWERPC_SECTOFF:
11982 case elfcpp::R_POWERPC_TPREL16:
11983 case elfcpp::R_POWERPC_DTPREL16:
11984 case elfcpp::R_POWERPC_GOT_TLSGD16:
11985 case elfcpp::R_POWERPC_GOT_TLSLD16:
11986 case elfcpp::R_POWERPC_GOT_TPREL16:
11987 case elfcpp::R_POWERPC_GOT_DTPREL16:
11988 overflow = Reloc::CHECK_LOW_INSN;
11989 break;
11990
11991 case elfcpp::R_PPC64_REL24_NOTOC:
11992 if (size == 32)
11993 break;
11994 // Fall through.
11995 case elfcpp::R_PPC64_REL24_P9NOTOC:
11996 case elfcpp::R_POWERPC_ADDR24:
11997 case elfcpp::R_POWERPC_ADDR14:
11998 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11999 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12000 case elfcpp::R_PPC64_ADDR16_DS:
12001 case elfcpp::R_POWERPC_REL24:
12002 case elfcpp::R_PPC_PLTREL24:
12003 case elfcpp::R_PPC_LOCAL24PC:
12004 case elfcpp::R_PPC64_TPREL16_DS:
12005 case elfcpp::R_PPC64_DTPREL16_DS:
12006 case elfcpp::R_PPC64_TOC16_DS:
12007 case elfcpp::R_PPC64_GOT16_DS:
12008 case elfcpp::R_PPC64_SECTOFF_DS:
12009 case elfcpp::R_POWERPC_REL14:
12010 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12011 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12012 case elfcpp::R_PPC64_D34:
12013 case elfcpp::R_PPC64_PCREL34:
12014 case elfcpp::R_PPC64_GOT_PCREL34:
12015 case elfcpp::R_PPC64_PLT_PCREL34:
12016 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12017 case elfcpp::R_PPC64_D28:
12018 case elfcpp::R_PPC64_PCREL28:
12019 case elfcpp::R_PPC64_TPREL34:
12020 case elfcpp::R_PPC64_DTPREL34:
12021 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12022 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12023 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12024 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12025 overflow = Reloc::CHECK_SIGNED;
12026 break;
12027 }
12028
12029 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
12030 Insn insn = 0;
12031
12032 if (overflow == Reloc::CHECK_LOW_INSN
12033 || overflow == Reloc::CHECK_HIGH_INSN)
12034 {
12035 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12036
12037 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
12038 overflow = Reloc::CHECK_BITFIELD;
12039 else if (overflow == Reloc::CHECK_LOW_INSN
12040 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
12041 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
12042 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
12043 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
12044 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
12045 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
12046 overflow = Reloc::CHECK_UNSIGNED;
12047 else
12048 overflow = Reloc::CHECK_SIGNED;
12049 }
12050
12051 bool maybe_dq_reloc = false;
12052 typename Powerpc_relocate_functions<size, big_endian>::Status status
12053 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
12054 switch (r_type)
12055 {
12056 case elfcpp::R_POWERPC_NONE:
12057 case elfcpp::R_POWERPC_TLS:
12058 case elfcpp::R_POWERPC_GNU_VTINHERIT:
12059 case elfcpp::R_POWERPC_GNU_VTENTRY:
12060 case elfcpp::R_POWERPC_PLTSEQ:
12061 case elfcpp::R_POWERPC_PLTCALL:
12062 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12063 case elfcpp::R_PPC64_PLTCALL_NOTOC:
12064 case elfcpp::R_PPC64_PCREL_OPT:
12065 break;
12066
12067 case elfcpp::R_PPC64_ADDR64:
12068 case elfcpp::R_PPC64_REL64:
12069 case elfcpp::R_PPC64_TOC:
12070 case elfcpp::R_PPC64_ADDR64_LOCAL:
12071 Reloc::addr64(view, value);
12072 break;
12073
12074 case elfcpp::R_POWERPC_TPREL:
12075 case elfcpp::R_POWERPC_DTPREL:
12076 if (size == 64)
12077 Reloc::addr64(view, value);
12078 else
12079 status = Reloc::addr32(view, value, overflow);
12080 break;
12081
12082 case elfcpp::R_PPC64_UADDR64:
12083 Reloc::addr64_u(view, value);
12084 break;
12085
12086 case elfcpp::R_POWERPC_ADDR32:
12087 status = Reloc::addr32(view, value, overflow);
12088 break;
12089
12090 case elfcpp::R_POWERPC_REL32:
12091 case elfcpp::R_POWERPC_UADDR32:
12092 status = Reloc::addr32_u(view, value, overflow);
12093 break;
12094
12095 case elfcpp::R_PPC64_REL24_NOTOC:
12096 if (size == 32)
12097 goto unsupp; // R_PPC_EMB_RELSDA
12098 // Fall through.
12099 case elfcpp::R_PPC64_REL24_P9NOTOC:
12100 case elfcpp::R_POWERPC_ADDR24:
12101 case elfcpp::R_POWERPC_REL24:
12102 case elfcpp::R_PPC_PLTREL24:
12103 case elfcpp::R_PPC_LOCAL24PC:
12104 status = Reloc::addr24(view, value, overflow);
12105 break;
12106
12107 case elfcpp::R_POWERPC_GOT_DTPREL16:
12108 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
12109 case elfcpp::R_POWERPC_GOT_TPREL16:
12110 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
12111 if (size == 64)
12112 {
12113 // On ppc64 these are all ds form
12114 maybe_dq_reloc = true;
12115 break;
12116 }
12117 // Fall through.
12118 case elfcpp::R_POWERPC_ADDR16:
12119 case elfcpp::R_POWERPC_REL16:
12120 case elfcpp::R_PPC64_TOC16:
12121 case elfcpp::R_POWERPC_GOT16:
12122 case elfcpp::R_POWERPC_SECTOFF:
12123 case elfcpp::R_POWERPC_TPREL16:
12124 case elfcpp::R_POWERPC_DTPREL16:
12125 case elfcpp::R_POWERPC_GOT_TLSGD16:
12126 case elfcpp::R_POWERPC_GOT_TLSLD16:
12127 case elfcpp::R_POWERPC_ADDR16_LO:
12128 case elfcpp::R_POWERPC_REL16_LO:
12129 case elfcpp::R_PPC64_TOC16_LO:
12130 case elfcpp::R_POWERPC_GOT16_LO:
12131 case elfcpp::R_POWERPC_PLT16_LO:
12132 case elfcpp::R_POWERPC_SECTOFF_LO:
12133 case elfcpp::R_POWERPC_TPREL16_LO:
12134 case elfcpp::R_POWERPC_DTPREL16_LO:
12135 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12136 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
12137 if (size == 64)
12138 status = Reloc::addr16(view, value, overflow);
12139 else
12140 maybe_dq_reloc = true;
12141 break;
12142
12143 case elfcpp::R_POWERPC_UADDR16:
12144 status = Reloc::addr16_u(view, value, overflow);
12145 break;
12146
12147 case elfcpp::R_PPC64_ADDR16_HIGH:
12148 case elfcpp::R_PPC64_TPREL16_HIGH:
12149 case elfcpp::R_PPC64_DTPREL16_HIGH:
12150 if (size == 32)
12151 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12152 goto unsupp;
12153 // Fall through.
12154 case elfcpp::R_POWERPC_ADDR16_HI:
12155 case elfcpp::R_POWERPC_REL16_HI:
12156 case elfcpp::R_PPC64_REL16_HIGH:
12157 case elfcpp::R_PPC64_TOC16_HI:
12158 case elfcpp::R_POWERPC_GOT16_HI:
12159 case elfcpp::R_POWERPC_PLT16_HI:
12160 case elfcpp::R_POWERPC_SECTOFF_HI:
12161 case elfcpp::R_POWERPC_TPREL16_HI:
12162 case elfcpp::R_POWERPC_DTPREL16_HI:
12163 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12164 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12165 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12166 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12167 Reloc::addr16_hi(view, value);
12168 break;
12169
12170 case elfcpp::R_PPC64_ADDR16_HIGHA:
12171 case elfcpp::R_PPC64_TPREL16_HIGHA:
12172 case elfcpp::R_PPC64_DTPREL16_HIGHA:
12173 if (size == 32)
12174 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12175 goto unsupp;
12176 // Fall through.
12177 case elfcpp::R_POWERPC_ADDR16_HA:
12178 case elfcpp::R_POWERPC_REL16_HA:
12179 case elfcpp::R_PPC64_REL16_HIGHA:
12180 case elfcpp::R_PPC64_TOC16_HA:
12181 case elfcpp::R_POWERPC_GOT16_HA:
12182 case elfcpp::R_POWERPC_PLT16_HA:
12183 case elfcpp::R_POWERPC_SECTOFF_HA:
12184 case elfcpp::R_POWERPC_TPREL16_HA:
12185 case elfcpp::R_POWERPC_DTPREL16_HA:
12186 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12187 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12188 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12189 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12190 Reloc::addr16_ha(view, value);
12191 break;
12192
12193 case elfcpp::R_POWERPC_REL16DX_HA:
12194 status = Reloc::addr16dx_ha(view, value, overflow);
12195 break;
12196
12197 case elfcpp::R_PPC64_DTPREL16_HIGHER:
12198 if (size == 32)
12199 // R_PPC_EMB_NADDR16_LO
12200 goto unsupp;
12201 // Fall through.
12202 case elfcpp::R_PPC64_ADDR16_HIGHER:
12203 case elfcpp::R_PPC64_REL16_HIGHER:
12204 case elfcpp::R_PPC64_TPREL16_HIGHER:
12205 Reloc::addr16_hi2(view, value);
12206 break;
12207
12208 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12209 if (size == 32)
12210 // R_PPC_EMB_NADDR16_HI
12211 goto unsupp;
12212 // Fall through.
12213 case elfcpp::R_PPC64_ADDR16_HIGHERA:
12214 case elfcpp::R_PPC64_REL16_HIGHERA:
12215 case elfcpp::R_PPC64_TPREL16_HIGHERA:
12216 Reloc::addr16_ha2(view, value);
12217 break;
12218
12219 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12220 if (size == 32)
12221 // R_PPC_EMB_NADDR16_HA
12222 goto unsupp;
12223 // Fall through.
12224 case elfcpp::R_PPC64_ADDR16_HIGHEST:
12225 case elfcpp::R_PPC64_REL16_HIGHEST:
12226 case elfcpp::R_PPC64_TPREL16_HIGHEST:
12227 Reloc::addr16_hi3(view, value);
12228 break;
12229
12230 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12231 if (size == 32)
12232 // R_PPC_EMB_SDAI16
12233 goto unsupp;
12234 // Fall through.
12235 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
12236 case elfcpp::R_PPC64_REL16_HIGHESTA:
12237 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12238 Reloc::addr16_ha3(view, value);
12239 break;
12240
12241 case elfcpp::R_PPC64_DTPREL16_DS:
12242 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12243 if (size == 32)
12244 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12245 goto unsupp;
12246 // Fall through.
12247 case elfcpp::R_PPC64_TPREL16_DS:
12248 case elfcpp::R_PPC64_TPREL16_LO_DS:
12249 if (size == 32)
12250 // R_PPC_TLSGD, R_PPC_TLSLD
12251 break;
12252 // Fall through.
12253 case elfcpp::R_PPC64_ADDR16_DS:
12254 case elfcpp::R_PPC64_ADDR16_LO_DS:
12255 case elfcpp::R_PPC64_TOC16_DS:
12256 case elfcpp::R_PPC64_TOC16_LO_DS:
12257 case elfcpp::R_PPC64_GOT16_DS:
12258 case elfcpp::R_PPC64_GOT16_LO_DS:
12259 case elfcpp::R_PPC64_PLT16_LO_DS:
12260 case elfcpp::R_PPC64_SECTOFF_DS:
12261 case elfcpp::R_PPC64_SECTOFF_LO_DS:
12262 maybe_dq_reloc = true;
12263 break;
12264
12265 case elfcpp::R_POWERPC_ADDR14:
12266 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12267 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12268 case elfcpp::R_POWERPC_REL14:
12269 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12270 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12271 status = Reloc::addr14(view, value, overflow);
12272 break;
12273
12274 case elfcpp::R_POWERPC_COPY:
12275 case elfcpp::R_POWERPC_GLOB_DAT:
12276 case elfcpp::R_POWERPC_JMP_SLOT:
12277 case elfcpp::R_POWERPC_RELATIVE:
12278 case elfcpp::R_POWERPC_DTPMOD:
12279 case elfcpp::R_PPC64_JMP_IREL:
12280 case elfcpp::R_POWERPC_IRELATIVE:
12281 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12282 _("unexpected reloc %u in object file"),
12283 r_type);
12284 break;
12285
12286 case elfcpp::R_PPC64_TOCSAVE:
12287 if (size == 32)
12288 // R_PPC_EMB_SDA21
12289 goto unsupp;
12290 else
12291 {
12292 Symbol_location loc;
12293 loc.object = relinfo->object;
12294 loc.shndx = relinfo->data_shndx;
12295 loc.offset = rela.get_r_offset();
12296 const Tocsave_loc *tocsave = target->tocsave_loc();
12297 if (tocsave->find(loc) != tocsave->end())
12298 {
12299 // If we've generated plt calls using this tocsave, then
12300 // the nop needs to be changed to save r2.
12301 Insn* iview = reinterpret_cast<Insn*>(view);
12302 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12303 elfcpp::Swap<32, big_endian>::
12304 writeval(iview, std_2_1 + target->stk_toc());
12305 }
12306 }
12307 break;
12308
12309 case elfcpp::R_PPC_EMB_SDA2I16:
12310 case elfcpp::R_PPC_EMB_SDA2REL:
12311 if (size == 32)
12312 goto unsupp;
12313 // R_PPC64_TLSGD, R_PPC64_TLSLD
12314 break;
12315
12316 case elfcpp::R_PPC64_D34:
12317 case elfcpp::R_PPC64_D34_LO:
12318 case elfcpp::R_PPC64_PCREL34:
12319 case elfcpp::R_PPC64_GOT_PCREL34:
12320 case elfcpp::R_PPC64_PLT_PCREL34:
12321 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12322 case elfcpp::R_PPC64_TPREL34:
12323 case elfcpp::R_PPC64_DTPREL34:
12324 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12325 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12326 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12327 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12328 if (size == 32)
12329 goto unsupp;
12330 status = Reloc::addr34(view, value, overflow);
12331 break;
12332
12333 case elfcpp::R_PPC64_D34_HI30:
12334 if (size == 32)
12335 goto unsupp;
12336 Reloc::addr34_hi(view, value);
12337 break;
12338
12339 case elfcpp::R_PPC64_D34_HA30:
12340 if (size == 32)
12341 goto unsupp;
12342 Reloc::addr34_ha(view, value);
12343 break;
12344
12345 case elfcpp::R_PPC64_D28:
12346 case elfcpp::R_PPC64_PCREL28:
12347 if (size == 32)
12348 goto unsupp;
12349 status = Reloc::addr28(view, value, overflow);
12350 break;
12351
12352 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12353 case elfcpp::R_PPC64_REL16_HIGHER34:
12354 if (size == 32)
12355 goto unsupp;
12356 Reloc::addr16_higher34(view, value);
12357 break;
12358
12359 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12360 case elfcpp::R_PPC64_REL16_HIGHERA34:
12361 if (size == 32)
12362 goto unsupp;
12363 Reloc::addr16_highera34(view, value);
12364 break;
12365
12366 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12367 case elfcpp::R_PPC64_REL16_HIGHEST34:
12368 if (size == 32)
12369 goto unsupp;
12370 Reloc::addr16_highest34(view, value);
12371 break;
12372
12373 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12374 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12375 if (size == 32)
12376 goto unsupp;
12377 Reloc::addr16_highesta34(view, value);
12378 break;
12379
12380 case elfcpp::R_POWERPC_PLT32:
12381 case elfcpp::R_POWERPC_PLTREL32:
12382 case elfcpp::R_PPC_SDAREL16:
12383 case elfcpp::R_POWERPC_ADDR30:
12384 case elfcpp::R_PPC64_PLT64:
12385 case elfcpp::R_PPC64_PLTREL64:
12386 case elfcpp::R_PPC64_PLTGOT16:
12387 case elfcpp::R_PPC64_PLTGOT16_LO:
12388 case elfcpp::R_PPC64_PLTGOT16_HI:
12389 case elfcpp::R_PPC64_PLTGOT16_HA:
12390 case elfcpp::R_PPC64_PLTGOT16_DS:
12391 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12392 case elfcpp::R_PPC_TOC16:
12393 default:
12394 unsupp:
12395 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12396 _("unsupported reloc %u"),
12397 r_type);
12398 break;
12399 }
12400
12401 if (maybe_dq_reloc)
12402 {
12403 if (insn == 0)
12404 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12405
12406 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12407 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12408 && (insn & 3) == 1))
12409 status = Reloc::addr16_dq(view, value, overflow);
12410 else if (size == 64
12411 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12412 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12413 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12414 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12415 status = Reloc::addr16_ds(view, value, overflow);
12416 else
12417 status = Reloc::addr16(view, value, overflow);
12418 }
12419
12420 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12421 && (has_stub_value
12422 || !(gsym != NULL
12423 && gsym->is_undefined()
12424 && is_branch_reloc<size>(r_type))))
12425 {
12426 std::string name;
12427 if (gsym)
12428 name = gsym->demangled_name();
12429 else
12430 name = relinfo->object->get_symbol_name(r_sym);
12431 if (os->flags() & elfcpp::SHF_ALLOC)
12432 {
12433 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12434 _("reloc type %u overflow against '%s'"),
12435 r_type, name.c_str());
12436 if (has_stub_value)
12437 gold_info(_("try relinking with a smaller --stub-group-size"));
12438 }
12439 else
12440 {
12441 gold_warning_at_location(relinfo, relnum, rela.get_r_offset(),
12442 _("reloc type %u overflow against '%s'"),
12443 r_type, name.c_str());
12444 gold_info(_("debug info may be unreliable, compile with -gdwarf64"));
12445 }
12446 }
12447
12448 return true;
12449 }
12450
12451 // Relocate section data.
12452
12453 template<int size, bool big_endian>
12454 void
12455 Target_powerpc<size, big_endian>::relocate_section(
12456 const Relocate_info<size, big_endian>* relinfo,
12457 unsigned int sh_type,
12458 const unsigned char* prelocs,
12459 size_t reloc_count,
12460 Output_section* output_section,
12461 bool needs_special_offset_handling,
12462 unsigned char* view,
12463 Address address,
12464 section_size_type view_size,
12465 const Reloc_symbol_changes* reloc_symbol_changes)
12466 {
12467 typedef Target_powerpc<size, big_endian> Powerpc;
12468 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12469 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12470 Powerpc_comdat_behavior;
12471 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12472 Classify_reloc;
12473
12474 gold_assert(sh_type == elfcpp::SHT_RELA);
12475
12476 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12477 Powerpc_comdat_behavior, Classify_reloc>(
12478 relinfo,
12479 this,
12480 prelocs,
12481 reloc_count,
12482 output_section,
12483 needs_special_offset_handling,
12484 view,
12485 address,
12486 view_size,
12487 reloc_symbol_changes);
12488 }
12489
12490 template<int size, bool big_endian>
12491 class Powerpc_scan_relocatable_reloc
12492 {
12493 public:
12494 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12495 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12496 static const int sh_type = elfcpp::SHT_RELA;
12497
12498 // Return the symbol referred to by the relocation.
12499 static inline unsigned int
12500 get_r_sym(const Reltype* reloc)
12501 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12502
12503 // Return the type of the relocation.
12504 static inline unsigned int
12505 get_r_type(const Reltype* reloc)
12506 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12507
12508 // Return the strategy to use for a local symbol which is not a
12509 // section symbol, given the relocation type.
12510 inline Relocatable_relocs::Reloc_strategy
12511 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12512 {
12513 if (r_type == 0 && r_sym == 0)
12514 return Relocatable_relocs::RELOC_DISCARD;
12515 return Relocatable_relocs::RELOC_COPY;
12516 }
12517
12518 // Return the strategy to use for a local symbol which is a section
12519 // symbol, given the relocation type.
12520 inline Relocatable_relocs::Reloc_strategy
12521 local_section_strategy(unsigned int, Relobj*)
12522 {
12523 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12524 }
12525
12526 // Return the strategy to use for a global symbol, given the
12527 // relocation type, the object, and the symbol index.
12528 inline Relocatable_relocs::Reloc_strategy
12529 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12530 {
12531 if (size == 32
12532 && (r_type == elfcpp::R_PPC_PLTREL24
12533 || r_type == elfcpp::R_POWERPC_PLT16_LO
12534 || r_type == elfcpp::R_POWERPC_PLT16_HI
12535 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12536 return Relocatable_relocs::RELOC_SPECIAL;
12537 return Relocatable_relocs::RELOC_COPY;
12538 }
12539 };
12540
12541 // Scan the relocs during a relocatable link.
12542
12543 template<int size, bool big_endian>
12544 void
12545 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12546 Symbol_table* symtab,
12547 Layout* layout,
12548 Sized_relobj_file<size, big_endian>* object,
12549 unsigned int data_shndx,
12550 unsigned int sh_type,
12551 const unsigned char* prelocs,
12552 size_t reloc_count,
12553 Output_section* output_section,
12554 bool needs_special_offset_handling,
12555 size_t local_symbol_count,
12556 const unsigned char* plocal_symbols,
12557 Relocatable_relocs* rr)
12558 {
12559 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12560
12561 gold_assert(sh_type == elfcpp::SHT_RELA);
12562
12563 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12564 symtab,
12565 layout,
12566 object,
12567 data_shndx,
12568 prelocs,
12569 reloc_count,
12570 output_section,
12571 needs_special_offset_handling,
12572 local_symbol_count,
12573 plocal_symbols,
12574 rr);
12575 }
12576
12577 // Scan the relocs for --emit-relocs.
12578
12579 template<int size, bool big_endian>
12580 void
12581 Target_powerpc<size, big_endian>::emit_relocs_scan(
12582 Symbol_table* symtab,
12583 Layout* layout,
12584 Sized_relobj_file<size, big_endian>* object,
12585 unsigned int data_shndx,
12586 unsigned int sh_type,
12587 const unsigned char* prelocs,
12588 size_t reloc_count,
12589 Output_section* output_section,
12590 bool needs_special_offset_handling,
12591 size_t local_symbol_count,
12592 const unsigned char* plocal_syms,
12593 Relocatable_relocs* rr)
12594 {
12595 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12596 Classify_reloc;
12597 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12598 Emit_relocs_strategy;
12599
12600 gold_assert(sh_type == elfcpp::SHT_RELA);
12601
12602 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12603 symtab,
12604 layout,
12605 object,
12606 data_shndx,
12607 prelocs,
12608 reloc_count,
12609 output_section,
12610 needs_special_offset_handling,
12611 local_symbol_count,
12612 plocal_syms,
12613 rr);
12614 }
12615
12616 // Emit relocations for a section.
12617 // This is a modified version of the function by the same name in
12618 // target-reloc.h. Using relocate_special_relocatable for
12619 // R_PPC_PLTREL24 would require duplication of the entire body of the
12620 // loop, so we may as well duplicate the whole thing.
12621
12622 template<int size, bool big_endian>
12623 void
12624 Target_powerpc<size, big_endian>::relocate_relocs(
12625 const Relocate_info<size, big_endian>* relinfo,
12626 unsigned int sh_type,
12627 const unsigned char* prelocs,
12628 size_t reloc_count,
12629 Output_section* output_section,
12630 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12631 unsigned char*,
12632 Address view_address,
12633 section_size_type,
12634 unsigned char* reloc_view,
12635 section_size_type reloc_view_size)
12636 {
12637 gold_assert(sh_type == elfcpp::SHT_RELA);
12638
12639 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12640 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12641 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12642 // Offset from start of insn to d-field reloc.
12643 const int d_offset = big_endian ? 2 : 0;
12644
12645 Powerpc_relobj<size, big_endian>* const object
12646 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12647 const unsigned int local_count = object->local_symbol_count();
12648 unsigned int got2_shndx = object->got2_shndx();
12649 Address got2_addend = 0;
12650 if (got2_shndx != 0)
12651 {
12652 got2_addend = object->get_output_section_offset(got2_shndx);
12653 gold_assert(got2_addend != invalid_address);
12654 }
12655
12656 const bool relocatable = parameters->options().relocatable();
12657
12658 unsigned char* pwrite = reloc_view;
12659 bool zap_next = false;
12660 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12661 {
12662 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12663 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12664 continue;
12665
12666 Reltype reloc(prelocs);
12667 Reltype_write reloc_write(pwrite);
12668
12669 Address offset = reloc.get_r_offset();
12670 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12671 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12672 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12673 const unsigned int orig_r_sym = r_sym;
12674 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12675 = reloc.get_r_addend();
12676 const Symbol* gsym = NULL;
12677
12678 if (zap_next)
12679 {
12680 // We could arrange to discard these and other relocs for
12681 // tls optimised sequences in the strategy methods, but for
12682 // now do as BFD ld does.
12683 r_type = elfcpp::R_POWERPC_NONE;
12684 zap_next = false;
12685 }
12686
12687 // Get the new symbol index.
12688 Output_section* os = NULL;
12689 if (r_sym < local_count)
12690 {
12691 switch (strategy)
12692 {
12693 case Relocatable_relocs::RELOC_COPY:
12694 case Relocatable_relocs::RELOC_SPECIAL:
12695 if (r_sym != 0)
12696 {
12697 r_sym = object->symtab_index(r_sym);
12698 gold_assert(r_sym != -1U);
12699 }
12700 break;
12701
12702 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12703 {
12704 // We are adjusting a section symbol. We need to find
12705 // the symbol table index of the section symbol for
12706 // the output section corresponding to input section
12707 // in which this symbol is defined.
12708 gold_assert(r_sym < local_count);
12709 bool is_ordinary;
12710 unsigned int shndx =
12711 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12712 gold_assert(is_ordinary);
12713 os = object->output_section(shndx);
12714 gold_assert(os != NULL);
12715 gold_assert(os->needs_symtab_index());
12716 r_sym = os->symtab_index();
12717 }
12718 break;
12719
12720 default:
12721 gold_unreachable();
12722 }
12723 }
12724 else
12725 {
12726 gsym = object->global_symbol(r_sym);
12727 gold_assert(gsym != NULL);
12728 if (gsym->is_forwarder())
12729 gsym = relinfo->symtab->resolve_forwards(gsym);
12730
12731 gold_assert(gsym->has_symtab_index());
12732 r_sym = gsym->symtab_index();
12733 }
12734
12735 // Get the new offset--the location in the output section where
12736 // this relocation should be applied.
12737 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12738 offset += offset_in_output_section;
12739 else
12740 {
12741 section_offset_type sot_offset =
12742 convert_types<section_offset_type, Address>(offset);
12743 section_offset_type new_sot_offset =
12744 output_section->output_offset(object, relinfo->data_shndx,
12745 sot_offset);
12746 gold_assert(new_sot_offset != -1);
12747 offset = new_sot_offset;
12748 }
12749
12750 // In an object file, r_offset is an offset within the section.
12751 // In an executable or dynamic object, generated by
12752 // --emit-relocs, r_offset is an absolute address.
12753 if (!relocatable)
12754 {
12755 offset += view_address;
12756 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12757 offset -= offset_in_output_section;
12758 }
12759
12760 // Handle the reloc addend based on the strategy.
12761 if (strategy == Relocatable_relocs::RELOC_COPY)
12762 ;
12763 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12764 {
12765 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12766 addend = psymval->value(object, addend);
12767 // In a relocatable link, the symbol value is relative to
12768 // the start of the output section. For a non-relocatable
12769 // link, we need to adjust the addend.
12770 if (!relocatable)
12771 {
12772 gold_assert(os != NULL);
12773 addend -= os->address();
12774 }
12775 }
12776 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12777 {
12778 if (size == 32)
12779 {
12780 if (addend >= 32768)
12781 addend += got2_addend;
12782 }
12783 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12784 {
12785 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12786 addend -= d_offset;
12787 }
12788 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12789 {
12790 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12791 addend -= d_offset + 4;
12792 }
12793 }
12794 else
12795 gold_unreachable();
12796
12797 if (!relocatable)
12798 {
12799 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12800 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12801 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12802 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12803 {
12804 // First instruction of a global dynamic sequence,
12805 // arg setup insn.
12806 bool final = gsym == NULL || gsym->final_value_is_known();
12807 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12808 switch (tls_type)
12809 {
12810 case tls::TLSOPT_TO_IE:
12811 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12812 - elfcpp::R_POWERPC_GOT_TLSGD16);
12813 break;
12814 case tls::TLSOPT_TO_LE:
12815 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12816 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12817 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12818 else
12819 {
12820 r_type = elfcpp::R_POWERPC_NONE;
12821 offset -= d_offset;
12822 }
12823 break;
12824 default:
12825 break;
12826 }
12827 }
12828 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12829 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12830 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12831 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12832 {
12833 // First instruction of a local dynamic sequence,
12834 // arg setup insn.
12835 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12836 if (tls_type == tls::TLSOPT_TO_LE)
12837 {
12838 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12839 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12840 {
12841 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12842 const Output_section* os = relinfo->layout->tls_segment()
12843 ->first_section();
12844 gold_assert(os != NULL);
12845 gold_assert(os->needs_symtab_index());
12846 r_sym = os->symtab_index();
12847 addend = dtp_offset;
12848 }
12849 else
12850 {
12851 r_type = elfcpp::R_POWERPC_NONE;
12852 offset -= d_offset;
12853 }
12854 }
12855 }
12856 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12857 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12858 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12859 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12860 {
12861 // First instruction of initial exec sequence.
12862 bool final = gsym == NULL || gsym->final_value_is_known();
12863 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12864 {
12865 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12866 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12867 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12868 else
12869 {
12870 r_type = elfcpp::R_POWERPC_NONE;
12871 offset -= d_offset;
12872 }
12873 }
12874 }
12875 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12876 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12877 {
12878 // Second instruction of a global dynamic sequence,
12879 // the __tls_get_addr call
12880 bool final = gsym == NULL || gsym->final_value_is_known();
12881 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12882 switch (tls_type)
12883 {
12884 case tls::TLSOPT_TO_IE:
12885 r_type = elfcpp::R_POWERPC_NONE;
12886 zap_next = true;
12887 break;
12888 case tls::TLSOPT_TO_LE:
12889 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12890 offset += d_offset;
12891 zap_next = true;
12892 break;
12893 default:
12894 break;
12895 }
12896 }
12897 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12898 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12899 {
12900 // Second instruction of a local dynamic sequence,
12901 // the __tls_get_addr call
12902 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12903 if (tls_type == tls::TLSOPT_TO_LE)
12904 {
12905 const Output_section* os = relinfo->layout->tls_segment()
12906 ->first_section();
12907 gold_assert(os != NULL);
12908 gold_assert(os->needs_symtab_index());
12909 r_sym = os->symtab_index();
12910 addend = dtp_offset;
12911 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12912 offset += d_offset;
12913 zap_next = true;
12914 }
12915 }
12916 else if (r_type == elfcpp::R_POWERPC_TLS)
12917 {
12918 // Second instruction of an initial exec sequence
12919 bool final = gsym == NULL || gsym->final_value_is_known();
12920 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12921 {
12922 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12923 offset += d_offset;
12924 }
12925 }
12926 }
12927
12928 reloc_write.put_r_offset(offset);
12929 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12930 reloc_write.put_r_addend(addend);
12931
12932 pwrite += reloc_size;
12933 }
12934
12935 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12936 == reloc_view_size);
12937 }
12938
12939 // Return the value to use for a dynamic symbol which requires special
12940 // treatment. This is how we support equality comparisons of function
12941 // pointers across shared library boundaries, as described in the
12942 // processor specific ABI supplement.
12943
12944 template<int size, bool big_endian>
12945 uint64_t
12946 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12947 {
12948 if (size == 32)
12949 {
12950 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12951 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12952 p != this->stub_tables_.end();
12953 ++p)
12954 {
12955 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12956 = (*p)->find_plt_call_entry(gsym);
12957 if (ent != NULL)
12958 return (*p)->stub_address() + ent->off_;
12959 }
12960 }
12961 else if (this->abiversion() >= 2)
12962 {
12963 Address off = this->glink_section()->find_global_entry(gsym);
12964 if (off != invalid_address)
12965 return this->glink_section()->global_entry_address() + off;
12966 }
12967 gold_unreachable();
12968 }
12969
12970 // Return the PLT address to use for a local symbol.
12971 template<int size, bool big_endian>
12972 uint64_t
12973 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12974 const Relobj* object,
12975 unsigned int symndx) const
12976 {
12977 if (size == 32)
12978 {
12979 const Sized_relobj<size, big_endian>* relobj
12980 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12981 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12982 p != this->stub_tables_.end();
12983 ++p)
12984 {
12985 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12986 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12987 if (ent != NULL)
12988 return (*p)->stub_address() + ent->off_;
12989 }
12990 }
12991 gold_unreachable();
12992 }
12993
12994 // Return the PLT address to use for a global symbol.
12995 template<int size, bool big_endian>
12996 uint64_t
12997 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12998 const Symbol* gsym) const
12999 {
13000 if (size == 32)
13001 {
13002 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
13003 p != this->stub_tables_.end();
13004 ++p)
13005 {
13006 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
13007 = (*p)->find_plt_call_entry(gsym);
13008 if (ent != NULL)
13009 return (*p)->stub_address() + ent->off_;
13010 }
13011 }
13012 else if (this->abiversion() >= 2)
13013 {
13014 Address off = this->glink_section()->find_global_entry(gsym);
13015 if (off != invalid_address)
13016 return this->glink_section()->global_entry_address() + off;
13017 }
13018 gold_unreachable();
13019 }
13020
13021 // Return the offset to use for the GOT_INDX'th got entry which is
13022 // for a local tls symbol specified by OBJECT, SYMNDX.
13023 template<int size, bool big_endian>
13024 int64_t
13025 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
13026 const Relobj* object,
13027 unsigned int symndx,
13028 Output_data_got_base* got,
13029 unsigned int got_indx,
13030 uint64_t addend) const
13031 {
13032 const Powerpc_relobj<size, big_endian>* ppc_object
13033 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
13034 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
13035 {
13036 for (Got_type got_type = (size == 32
13037 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13038 got_type <= GOT_TYPE_SMALL_TPREL;
13039 got_type = Got_type(got_type + 1))
13040 if (got_type != GOT_TYPE_SMALL
13041 && ppc_object->local_has_got_offset(symndx, got_type, addend))
13042 {
13043 unsigned int off
13044 = ppc_object->local_got_offset(symndx, got_type, addend);
13045 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13046 off += size / 8;
13047 if (off == got_indx * (size / 8)
13048 && (size == 32 || got == this->got_section(got_type)))
13049 {
13050 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13051 return -tp_offset;
13052 else
13053 return -dtp_offset;
13054 }
13055 }
13056 }
13057 gold_unreachable();
13058 }
13059
13060 // Return the offset to use for the GOT_INDX'th got entry which is
13061 // for global tls symbol GSYM.
13062 template<int size, bool big_endian>
13063 int64_t
13064 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13065 Symbol* gsym,
13066 Output_data_got_base* got,
13067 unsigned int got_indx,
13068 uint64_t addend) const
13069 {
13070 if (gsym->type() == elfcpp::STT_TLS)
13071 {
13072 for (Got_type got_type = (size == 32
13073 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13074 got_type <= GOT_TYPE_SMALL_TPREL;
13075 got_type = Got_type(got_type + 1))
13076 if (got_type != GOT_TYPE_SMALL
13077 && gsym->has_got_offset(got_type, addend))
13078 {
13079 unsigned int off = gsym->got_offset(got_type, addend);
13080 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13081 off += size / 8;
13082 if (off == got_indx * (size / 8)
13083 && (size == 32 || got == this->got_section(got_type)))
13084 {
13085 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13086 return -tp_offset;
13087 else
13088 return -dtp_offset;
13089 }
13090 }
13091 }
13092 gold_unreachable();
13093 }
13094
13095 // The selector for powerpc object files.
13096
13097 template<int size, bool big_endian>
13098 class Target_selector_powerpc : public Target_selector
13099 {
13100 public:
13101 Target_selector_powerpc()
13102 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13103 size, big_endian,
13104 (size == 64
13105 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13106 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13107 (size == 64
13108 ? (big_endian ? "elf64ppc" : "elf64lppc")
13109 : (big_endian ? "elf32ppc" : "elf32lppc")))
13110 { }
13111
13112 virtual Target*
13113 do_instantiate_target()
13114 { return new Target_powerpc<size, big_endian>(); }
13115 };
13116
13117 Target_selector_powerpc<32, true> target_selector_ppc32;
13118 Target_selector_powerpc<32, false> target_selector_ppc32le;
13119 Target_selector_powerpc<64, true> target_selector_ppc64;
13120 Target_selector_powerpc<64, false> target_selector_ppc64le;
13121
13122 // Instantiate these constants for -O0
13123 template<int size, bool big_endian>
13124 const typename Output_data_glink<size, big_endian>::Address
13125 Output_data_glink<size, big_endian>::invalid_address;
13126 template<int size, bool big_endian>
13127 const typename Stub_table<size, big_endian>::Address
13128 Stub_table<size, big_endian>::invalid_address;
13129 template<int size, bool big_endian>
13130 const typename Target_powerpc<size, big_endian>::Address
13131 Target_powerpc<size, big_endian>::invalid_address;
13132
13133 } // End anonymous namespace.