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