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