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