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