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