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