[GOLD] PowerPC move plt indx_ out of unordered map key
[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_key;
4121 class Plt_stub_key_hash;
4122 struct Plt_stub_ent
4123 {
4124 Plt_stub_ent(unsigned int off, unsigned int indx)
4125 : off_(off), indx_(indx)
4126 { }
4127
4128 unsigned int off_;
4129 unsigned int indx_;
4130 };
4131 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4132 Plt_stub_key_hash> Plt_stub_entries;
4133 class Branch_stub_ent;
4134 class Branch_stub_ent_hash;
4135 typedef Unordered_map<Branch_stub_ent, unsigned int,
4136 Branch_stub_ent_hash> Branch_stub_entries;
4137
4138 // Alignment of stub section.
4139 unsigned int
4140 stub_align() const
4141 {
4142 if (size == 32)
4143 return 16;
4144 unsigned int min_align = 32;
4145 unsigned int user_align = 1 << parameters->options().plt_align();
4146 return std::max(user_align, min_align);
4147 }
4148
4149 // Return the plt offset for the given call stub.
4150 Address
4151 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
4152 {
4153 const Symbol* gsym = p->first.sym_;
4154 if (gsym != NULL)
4155 {
4156 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
4157 && gsym->can_use_relative_reloc(false));
4158 return gsym->plt_offset();
4159 }
4160 else
4161 {
4162 *is_iplt = true;
4163 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4164 unsigned int local_sym_index = p->first.locsym_;
4165 return relobj->local_plt_offset(local_sym_index);
4166 }
4167 }
4168
4169 // Size of a given plt call stub.
4170 unsigned int
4171 plt_call_size(typename Plt_stub_entries::const_iterator p) const
4172 {
4173 if (size == 32)
4174 return 16;
4175
4176 bool is_iplt;
4177 Address plt_addr = this->plt_off(p, &is_iplt);
4178 if (is_iplt)
4179 plt_addr += this->targ_->iplt_section()->address();
4180 else
4181 plt_addr += this->targ_->plt_section()->address();
4182 Address got_addr = this->targ_->got_section()->output_section()->address();
4183 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4184 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
4185 got_addr += ppcobj->toc_base_offset();
4186 Address off = plt_addr - got_addr;
4187 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
4188 if (this->targ_->abiversion() < 2)
4189 {
4190 bool static_chain = parameters->options().plt_static_chain();
4191 bool thread_safe = this->targ_->plt_thread_safe();
4192 bytes += (4
4193 + 4 * static_chain
4194 + 8 * thread_safe
4195 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
4196 }
4197 unsigned int align = 1 << parameters->options().plt_align();
4198 if (align > 1)
4199 bytes = (bytes + align - 1) & -align;
4200 return bytes;
4201 }
4202
4203 // Return long branch stub size.
4204 unsigned int
4205 branch_stub_size(typename Branch_stub_entries::const_iterator p)
4206 {
4207 Address loc = this->stub_address() + this->last_plt_size_ + p->second;
4208 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
4209 return 4;
4210 if (size == 64 || !parameters->options().output_is_position_independent())
4211 return 16;
4212 return 32;
4213 }
4214
4215 // Write out stubs.
4216 void
4217 do_write(Output_file*);
4218
4219 // Plt call stub keys.
4220 class Plt_stub_key
4221 {
4222 public:
4223 Plt_stub_key(const Symbol* sym)
4224 : sym_(sym), object_(0), addend_(0), locsym_(0)
4225 { }
4226
4227 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4228 unsigned int locsym_index)
4229 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4230 { }
4231
4232 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4233 const Symbol* sym,
4234 unsigned int r_type,
4235 Address addend)
4236 : sym_(sym), object_(0), addend_(0), locsym_(0)
4237 {
4238 if (size != 32)
4239 this->addend_ = addend;
4240 else if (parameters->options().output_is_position_independent()
4241 && r_type == elfcpp::R_PPC_PLTREL24)
4242 {
4243 this->addend_ = addend;
4244 if (this->addend_ >= 32768)
4245 this->object_ = object;
4246 }
4247 }
4248
4249 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4250 unsigned int locsym_index,
4251 unsigned int r_type,
4252 Address addend)
4253 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4254 {
4255 if (size != 32)
4256 this->addend_ = addend;
4257 else if (parameters->options().output_is_position_independent()
4258 && r_type == elfcpp::R_PPC_PLTREL24)
4259 this->addend_ = addend;
4260 }
4261
4262 bool operator==(const Plt_stub_key& that) const
4263 {
4264 return (this->sym_ == that.sym_
4265 && this->object_ == that.object_
4266 && this->addend_ == that.addend_
4267 && this->locsym_ == that.locsym_);
4268 }
4269
4270 const Symbol* sym_;
4271 const Sized_relobj_file<size, big_endian>* object_;
4272 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4273 unsigned int locsym_;
4274 };
4275
4276 class Plt_stub_key_hash
4277 {
4278 public:
4279 size_t operator()(const Plt_stub_key& ent) const
4280 {
4281 return (reinterpret_cast<uintptr_t>(ent.sym_)
4282 ^ reinterpret_cast<uintptr_t>(ent.object_)
4283 ^ ent.addend_
4284 ^ ent.locsym_);
4285 }
4286 };
4287
4288 // Long branch stub keys.
4289 class Branch_stub_ent
4290 {
4291 public:
4292 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4293 Address to, bool save_res)
4294 : dest_(to), toc_base_off_(0), save_res_(save_res)
4295 {
4296 if (size == 64)
4297 toc_base_off_ = obj->toc_base_offset();
4298 }
4299
4300 bool operator==(const Branch_stub_ent& that) const
4301 {
4302 return (this->dest_ == that.dest_
4303 && (size == 32
4304 || this->toc_base_off_ == that.toc_base_off_));
4305 }
4306
4307 Address dest_;
4308 unsigned int toc_base_off_;
4309 bool save_res_;
4310 };
4311
4312 class Branch_stub_ent_hash
4313 {
4314 public:
4315 size_t operator()(const Branch_stub_ent& ent) const
4316 { return ent.dest_ ^ ent.toc_base_off_; }
4317 };
4318
4319 // In a sane world this would be a global.
4320 Target_powerpc<size, big_endian>* targ_;
4321 // Map sym/object/addend to stub offset.
4322 Plt_stub_entries plt_call_stubs_;
4323 // Map destination address to stub offset.
4324 Branch_stub_entries long_branch_stubs_;
4325 // size of input section
4326 section_size_type orig_data_size_;
4327 // size of stubs
4328 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4329 // Some rare cases cause (PR/20529) fluctuation in stub table
4330 // size, which leads to an endless relax loop. This is to be fixed
4331 // by, after the first few iterations, allowing only increase of
4332 // stub table size. This variable sets the minimal possible size of
4333 // a stub table, it is zero for the first few iterations, then
4334 // increases monotonically.
4335 Address min_size_threshold_;
4336 // Whether .eh_frame info has been created for this stub section.
4337 bool eh_frame_added_;
4338 // Set if this stub group needs a copy of out-of-line register
4339 // save/restore functions.
4340 bool need_save_res_;
4341 // Per stub table unique identifier.
4342 uint32_t uniq_;
4343 };
4344
4345 // Add a plt call stub, if we do not already have one for this
4346 // sym/object/addend combo.
4347
4348 template<int size, bool big_endian>
4349 bool
4350 Stub_table<size, big_endian>::add_plt_call_entry(
4351 Address from,
4352 const Sized_relobj_file<size, big_endian>* object,
4353 const Symbol* gsym,
4354 unsigned int r_type,
4355 Address addend)
4356 {
4357 Plt_stub_key key(object, gsym, r_type, addend);
4358 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4359 std::pair<typename Plt_stub_entries::iterator, bool> p
4360 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4361 if (p.second)
4362 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4363 return this->can_reach_stub(from, ent.off_, r_type);
4364 }
4365
4366 template<int size, bool big_endian>
4367 bool
4368 Stub_table<size, big_endian>::add_plt_call_entry(
4369 Address from,
4370 const Sized_relobj_file<size, big_endian>* object,
4371 unsigned int locsym_index,
4372 unsigned int r_type,
4373 Address addend)
4374 {
4375 Plt_stub_key key(object, locsym_index, r_type, addend);
4376 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
4377 std::pair<typename Plt_stub_entries::iterator, bool> p
4378 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
4379 if (p.second)
4380 this->plt_size_ = ent.off_ + this->plt_call_size(p.first);
4381 return this->can_reach_stub(from, ent.off_, r_type);
4382 }
4383
4384 // Find a plt call stub.
4385
4386 template<int size, bool big_endian>
4387 typename Stub_table<size, big_endian>::Address
4388 Stub_table<size, big_endian>::find_plt_call_entry(
4389 const Sized_relobj_file<size, big_endian>* object,
4390 const Symbol* gsym,
4391 unsigned int r_type,
4392 Address addend) const
4393 {
4394 Plt_stub_key key(object, gsym, r_type, addend);
4395 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4396 if (p == this->plt_call_stubs_.end())
4397 return invalid_address;
4398 return p->second.off_;
4399 }
4400
4401 template<int size, bool big_endian>
4402 typename Stub_table<size, big_endian>::Address
4403 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4404 {
4405 Plt_stub_key key(gsym);
4406 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4407 return p == this->plt_call_stubs_.end() ? invalid_address : p->second.off_;
4408 }
4409
4410 template<int size, bool big_endian>
4411 typename Stub_table<size, big_endian>::Address
4412 Stub_table<size, big_endian>::find_plt_call_entry(
4413 const Sized_relobj_file<size, big_endian>* object,
4414 unsigned int locsym_index,
4415 unsigned int r_type,
4416 Address addend) const
4417 {
4418 Plt_stub_key key(object, locsym_index, r_type, addend);
4419 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4420 if (p == this->plt_call_stubs_.end())
4421 return invalid_address;
4422 return p->second.off_;
4423 }
4424
4425 template<int size, bool big_endian>
4426 typename Stub_table<size, big_endian>::Address
4427 Stub_table<size, big_endian>::find_plt_call_entry(
4428 const Sized_relobj_file<size, big_endian>* object,
4429 unsigned int locsym_index) const
4430 {
4431 Plt_stub_key key(object, locsym_index);
4432 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
4433 return p == this->plt_call_stubs_.end() ? invalid_address : p->second.off_;
4434 }
4435
4436 // Add a long branch stub if we don't already have one to given
4437 // destination.
4438
4439 template<int size, bool big_endian>
4440 bool
4441 Stub_table<size, big_endian>::add_long_branch_entry(
4442 const Powerpc_relobj<size, big_endian>* object,
4443 unsigned int r_type,
4444 Address from,
4445 Address to,
4446 bool save_res)
4447 {
4448 Branch_stub_ent ent(object, to, save_res);
4449 Address off = this->branch_size_;
4450 std::pair<typename Branch_stub_entries::iterator, bool> p
4451 = this->long_branch_stubs_.insert(std::make_pair(ent, off));
4452 if (p.second)
4453 {
4454 if (save_res)
4455 this->need_save_res_ = true;
4456 else
4457 {
4458 unsigned int stub_size = this->branch_stub_size(p.first);
4459 this->branch_size_ = off + stub_size;
4460 if (size == 64 && stub_size != 4)
4461 this->targ_->add_branch_lookup_table(to);
4462 }
4463 }
4464 return this->can_reach_stub(from, off, r_type);
4465 }
4466
4467 // Find long branch stub offset.
4468
4469 template<int size, bool big_endian>
4470 typename Stub_table<size, big_endian>::Address
4471 Stub_table<size, big_endian>::find_long_branch_entry(
4472 const Powerpc_relobj<size, big_endian>* object,
4473 Address to) const
4474 {
4475 Branch_stub_ent ent(object, to, false);
4476 typename Branch_stub_entries::const_iterator p
4477 = this->long_branch_stubs_.find(ent);
4478 if (p == this->long_branch_stubs_.end())
4479 return invalid_address;
4480 if (p->first.save_res_)
4481 return to - this->targ_->savres_section()->address() + this->branch_size_;
4482 return p->second;
4483 }
4484
4485 // A class to handle .glink.
4486
4487 template<int size, bool big_endian>
4488 class Output_data_glink : public Output_section_data
4489 {
4490 public:
4491 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4492 static const Address invalid_address = static_cast<Address>(0) - 1;
4493 static const int pltresolve_size = 16*4;
4494
4495 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4496 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4497 end_branch_table_(), ge_size_(0)
4498 { }
4499
4500 void
4501 add_eh_frame(Layout* layout);
4502
4503 void
4504 add_global_entry(const Symbol*);
4505
4506 Address
4507 find_global_entry(const Symbol*) const;
4508
4509 Address
4510 global_entry_address() const
4511 {
4512 gold_assert(this->is_data_size_valid());
4513 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4514 return this->address() + global_entry_off;
4515 }
4516
4517 protected:
4518 // Write to a map file.
4519 void
4520 do_print_to_mapfile(Mapfile* mapfile) const
4521 { mapfile->print_output_data(this, _("** glink")); }
4522
4523 private:
4524 void
4525 set_final_data_size();
4526
4527 // Write out .glink
4528 void
4529 do_write(Output_file*);
4530
4531 // Allows access to .got and .plt for do_write.
4532 Target_powerpc<size, big_endian>* targ_;
4533
4534 // Map sym to stub offset.
4535 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4536 Global_entry_stub_entries global_entry_stubs_;
4537
4538 unsigned int end_branch_table_, ge_size_;
4539 };
4540
4541 template<int size, bool big_endian>
4542 void
4543 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4544 {
4545 if (!parameters->options().ld_generated_unwind_info())
4546 return;
4547
4548 if (size == 64)
4549 {
4550 if (this->targ_->abiversion() < 2)
4551 layout->add_eh_frame_for_plt(this,
4552 Eh_cie<64>::eh_frame_cie,
4553 sizeof (Eh_cie<64>::eh_frame_cie),
4554 glink_eh_frame_fde_64v1,
4555 sizeof (glink_eh_frame_fde_64v1));
4556 else
4557 layout->add_eh_frame_for_plt(this,
4558 Eh_cie<64>::eh_frame_cie,
4559 sizeof (Eh_cie<64>::eh_frame_cie),
4560 glink_eh_frame_fde_64v2,
4561 sizeof (glink_eh_frame_fde_64v2));
4562 }
4563 else
4564 {
4565 // 32-bit .glink can use the default since the CIE return
4566 // address reg, LR, is valid.
4567 layout->add_eh_frame_for_plt(this,
4568 Eh_cie<32>::eh_frame_cie,
4569 sizeof (Eh_cie<32>::eh_frame_cie),
4570 default_fde,
4571 sizeof (default_fde));
4572 // Except where LR is used in a PIC __glink_PLTresolve.
4573 if (parameters->options().output_is_position_independent())
4574 layout->add_eh_frame_for_plt(this,
4575 Eh_cie<32>::eh_frame_cie,
4576 sizeof (Eh_cie<32>::eh_frame_cie),
4577 glink_eh_frame_fde_32,
4578 sizeof (glink_eh_frame_fde_32));
4579 }
4580 }
4581
4582 template<int size, bool big_endian>
4583 void
4584 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4585 {
4586 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4587 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4588 if (p.second)
4589 this->ge_size_ += 16;
4590 }
4591
4592 template<int size, bool big_endian>
4593 typename Output_data_glink<size, big_endian>::Address
4594 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4595 {
4596 typename Global_entry_stub_entries::const_iterator p
4597 = this->global_entry_stubs_.find(gsym);
4598 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4599 }
4600
4601 template<int size, bool big_endian>
4602 void
4603 Output_data_glink<size, big_endian>::set_final_data_size()
4604 {
4605 unsigned int count = this->targ_->plt_entry_count();
4606 section_size_type total = 0;
4607
4608 if (count != 0)
4609 {
4610 if (size == 32)
4611 {
4612 // space for branch table
4613 total += 4 * (count - 1);
4614
4615 total += -total & 15;
4616 total += this->pltresolve_size;
4617 }
4618 else
4619 {
4620 total += this->pltresolve_size;
4621
4622 // space for branch table
4623 total += 4 * count;
4624 if (this->targ_->abiversion() < 2)
4625 {
4626 total += 4 * count;
4627 if (count > 0x8000)
4628 total += 4 * (count - 0x8000);
4629 }
4630 }
4631 }
4632 this->end_branch_table_ = total;
4633 total = (total + 15) & -16;
4634 total += this->ge_size_;
4635
4636 this->set_data_size(total);
4637 }
4638
4639 // Define symbols on stubs, identifying the stub.
4640
4641 template<int size, bool big_endian>
4642 void
4643 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
4644 {
4645 if (!this->plt_call_stubs_.empty())
4646 {
4647 // The key for the plt call stub hash table includes addresses,
4648 // therefore traversal order depends on those addresses, which
4649 // can change between runs if gold is a PIE. Unfortunately the
4650 // output .symtab ordering depends on the order in which symbols
4651 // are added to the linker symtab. We want reproducible output
4652 // so must sort the call stub symbols.
4653 typedef typename Plt_stub_entries::const_iterator plt_iter;
4654 std::vector<plt_iter> sorted;
4655 sorted.resize(this->plt_call_stubs_.size());
4656
4657 for (plt_iter cs = this->plt_call_stubs_.begin();
4658 cs != this->plt_call_stubs_.end();
4659 ++cs)
4660 sorted[cs->second.indx_] = cs;
4661
4662 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
4663 {
4664 plt_iter cs = sorted[i];
4665 char add[10];
4666 add[0] = 0;
4667 if (cs->first.addend_ != 0)
4668 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
4669 char obj[10];
4670 obj[0] = 0;
4671 if (cs->first.object_)
4672 {
4673 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4674 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4675 sprintf(obj, "%x:", ppcobj->uniq());
4676 }
4677 char localname[9];
4678 const char *symname;
4679 if (cs->first.sym_ == NULL)
4680 {
4681 sprintf(localname, "%x", cs->first.locsym_);
4682 symname = localname;
4683 }
4684 else
4685 symname = cs->first.sym_->name();
4686 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
4687 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
4688 Address value
4689 = this->stub_address() - this->address() + cs->second.off_;
4690 unsigned int stub_size = this->plt_call_size(cs);
4691 this->targ_->define_local(symtab, name, this, value, stub_size);
4692 }
4693 }
4694
4695 typedef typename Branch_stub_entries::const_iterator branch_iter;
4696 for (branch_iter bs = this->long_branch_stubs_.begin();
4697 bs != this->long_branch_stubs_.end();
4698 ++bs)
4699 {
4700 if (bs->first.save_res_)
4701 continue;
4702
4703 char* name = new char[8 + 13 + 16 + 1];
4704 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
4705 static_cast<unsigned long long>(bs->first.dest_));
4706 Address value = (this->stub_address() - this->address()
4707 + this->plt_size_ + bs->second);
4708 unsigned int stub_size = this->branch_stub_size(bs);
4709 this->targ_->define_local(symtab, name, this, value, stub_size);
4710 }
4711 }
4712
4713 // Write out plt and long branch stub code.
4714
4715 template<int size, bool big_endian>
4716 void
4717 Stub_table<size, big_endian>::do_write(Output_file* of)
4718 {
4719 if (this->plt_call_stubs_.empty()
4720 && this->long_branch_stubs_.empty())
4721 return;
4722
4723 const section_size_type start_off = this->offset();
4724 const section_size_type off = this->stub_offset();
4725 const section_size_type oview_size =
4726 convert_to_section_size_type(this->data_size() - (off - start_off));
4727 unsigned char* const oview = of->get_output_view(off, oview_size);
4728 unsigned char* p;
4729
4730 if (size == 64)
4731 {
4732 const Output_data_got_powerpc<size, big_endian>* got
4733 = this->targ_->got_section();
4734 Address got_os_addr = got->output_section()->address();
4735
4736 if (!this->plt_call_stubs_.empty())
4737 {
4738 // The base address of the .plt section.
4739 Address plt_base = this->targ_->plt_section()->address();
4740 Address iplt_base = invalid_address;
4741
4742 // Write out plt call stubs.
4743 typename Plt_stub_entries::const_iterator cs;
4744 for (cs = this->plt_call_stubs_.begin();
4745 cs != this->plt_call_stubs_.end();
4746 ++cs)
4747 {
4748 bool is_iplt;
4749 Address pltoff = this->plt_off(cs, &is_iplt);
4750 Address plt_addr = pltoff;
4751 if (is_iplt)
4752 {
4753 if (iplt_base == invalid_address)
4754 iplt_base = this->targ_->iplt_section()->address();
4755 plt_addr += iplt_base;
4756 }
4757 else
4758 plt_addr += plt_base;
4759 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4760 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4761 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4762 Address off = plt_addr - got_addr;
4763
4764 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4765 gold_error(_("%s: linkage table error against `%s'"),
4766 cs->first.object_->name().c_str(),
4767 cs->first.sym_->demangled_name().c_str());
4768
4769 bool plt_load_toc = this->targ_->abiversion() < 2;
4770 bool static_chain
4771 = plt_load_toc && parameters->options().plt_static_chain();
4772 bool thread_safe
4773 = plt_load_toc && this->targ_->plt_thread_safe();
4774 bool use_fake_dep = false;
4775 Address cmp_branch_off = 0;
4776 if (thread_safe)
4777 {
4778 unsigned int pltindex
4779 = ((pltoff - this->targ_->first_plt_entry_offset())
4780 / this->targ_->plt_entry_size());
4781 Address glinkoff
4782 = (this->targ_->glink_section()->pltresolve_size
4783 + pltindex * 8);
4784 if (pltindex > 32768)
4785 glinkoff += (pltindex - 32768) * 4;
4786 Address to
4787 = this->targ_->glink_section()->address() + glinkoff;
4788 Address from
4789 = (this->stub_address() + cs->second.off_ + 24
4790 + 4 * (ha(off) != 0)
4791 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4792 + 4 * static_chain);
4793 cmp_branch_off = to - from;
4794 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4795 }
4796
4797 p = oview + cs->second.off_;
4798 if (ha(off) != 0)
4799 {
4800 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4801 p += 4;
4802 if (plt_load_toc)
4803 {
4804 write_insn<big_endian>(p, addis_11_2 + ha(off));
4805 p += 4;
4806 write_insn<big_endian>(p, ld_12_11 + l(off));
4807 p += 4;
4808 }
4809 else
4810 {
4811 write_insn<big_endian>(p, addis_12_2 + ha(off));
4812 p += 4;
4813 write_insn<big_endian>(p, ld_12_12 + l(off));
4814 p += 4;
4815 }
4816 if (plt_load_toc
4817 && ha(off + 8 + 8 * static_chain) != ha(off))
4818 {
4819 write_insn<big_endian>(p, addi_11_11 + l(off));
4820 p += 4;
4821 off = 0;
4822 }
4823 write_insn<big_endian>(p, mtctr_12);
4824 p += 4;
4825 if (plt_load_toc)
4826 {
4827 if (use_fake_dep)
4828 {
4829 write_insn<big_endian>(p, xor_2_12_12);
4830 p += 4;
4831 write_insn<big_endian>(p, add_11_11_2);
4832 p += 4;
4833 }
4834 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4835 p += 4;
4836 if (static_chain)
4837 {
4838 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4839 p += 4;
4840 }
4841 }
4842 }
4843 else
4844 {
4845 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4846 p += 4;
4847 write_insn<big_endian>(p, ld_12_2 + l(off));
4848 p += 4;
4849 if (plt_load_toc
4850 && ha(off + 8 + 8 * static_chain) != ha(off))
4851 {
4852 write_insn<big_endian>(p, addi_2_2 + l(off));
4853 p += 4;
4854 off = 0;
4855 }
4856 write_insn<big_endian>(p, mtctr_12);
4857 p += 4;
4858 if (plt_load_toc)
4859 {
4860 if (use_fake_dep)
4861 {
4862 write_insn<big_endian>(p, xor_11_12_12);
4863 p += 4;
4864 write_insn<big_endian>(p, add_2_2_11);
4865 p += 4;
4866 }
4867 if (static_chain)
4868 {
4869 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4870 p += 4;
4871 }
4872 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4873 p += 4;
4874 }
4875 }
4876 if (thread_safe && !use_fake_dep)
4877 {
4878 write_insn<big_endian>(p, cmpldi_2_0);
4879 p += 4;
4880 write_insn<big_endian>(p, bnectr_p4);
4881 p += 4;
4882 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4883 }
4884 else
4885 write_insn<big_endian>(p, bctr);
4886 }
4887 }
4888
4889 // Write out long branch stubs.
4890 typename Branch_stub_entries::const_iterator bs;
4891 for (bs = this->long_branch_stubs_.begin();
4892 bs != this->long_branch_stubs_.end();
4893 ++bs)
4894 {
4895 if (bs->first.save_res_)
4896 continue;
4897 p = oview + this->plt_size_ + bs->second;
4898 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4899 Address delta = bs->first.dest_ - loc;
4900 if (delta + (1 << 25) < 2 << 25)
4901 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4902 else
4903 {
4904 Address brlt_addr
4905 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4906 gold_assert(brlt_addr != invalid_address);
4907 brlt_addr += this->targ_->brlt_section()->address();
4908 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4909 Address brltoff = brlt_addr - got_addr;
4910 if (ha(brltoff) == 0)
4911 {
4912 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
4913 }
4914 else
4915 {
4916 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
4917 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
4918 }
4919 write_insn<big_endian>(p, mtctr_12), p += 4;
4920 write_insn<big_endian>(p, bctr);
4921 }
4922 }
4923 }
4924 else
4925 {
4926 if (!this->plt_call_stubs_.empty())
4927 {
4928 // The base address of the .plt section.
4929 Address plt_base = this->targ_->plt_section()->address();
4930 Address iplt_base = invalid_address;
4931 // The address of _GLOBAL_OFFSET_TABLE_.
4932 Address g_o_t = invalid_address;
4933
4934 // Write out plt call stubs.
4935 typename Plt_stub_entries::const_iterator cs;
4936 for (cs = this->plt_call_stubs_.begin();
4937 cs != this->plt_call_stubs_.end();
4938 ++cs)
4939 {
4940 bool is_iplt;
4941 Address plt_addr = this->plt_off(cs, &is_iplt);
4942 if (is_iplt)
4943 {
4944 if (iplt_base == invalid_address)
4945 iplt_base = this->targ_->iplt_section()->address();
4946 plt_addr += iplt_base;
4947 }
4948 else
4949 plt_addr += plt_base;
4950
4951 p = oview + cs->second.off_;
4952 if (parameters->options().output_is_position_independent())
4953 {
4954 Address got_addr;
4955 const Powerpc_relobj<size, big_endian>* ppcobj
4956 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4957 (cs->first.object_));
4958 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4959 {
4960 unsigned int got2 = ppcobj->got2_shndx();
4961 got_addr = ppcobj->get_output_section_offset(got2);
4962 gold_assert(got_addr != invalid_address);
4963 got_addr += (ppcobj->output_section(got2)->address()
4964 + cs->first.addend_);
4965 }
4966 else
4967 {
4968 if (g_o_t == invalid_address)
4969 {
4970 const Output_data_got_powerpc<size, big_endian>* got
4971 = this->targ_->got_section();
4972 g_o_t = got->address() + got->g_o_t();
4973 }
4974 got_addr = g_o_t;
4975 }
4976
4977 Address off = plt_addr - got_addr;
4978 if (ha(off) == 0)
4979 {
4980 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4981 write_insn<big_endian>(p + 4, mtctr_11);
4982 write_insn<big_endian>(p + 8, bctr);
4983 }
4984 else
4985 {
4986 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4987 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4988 write_insn<big_endian>(p + 8, mtctr_11);
4989 write_insn<big_endian>(p + 12, bctr);
4990 }
4991 }
4992 else
4993 {
4994 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4995 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4996 write_insn<big_endian>(p + 8, mtctr_11);
4997 write_insn<big_endian>(p + 12, bctr);
4998 }
4999 }
5000 }
5001
5002 // Write out long branch stubs.
5003 typename Branch_stub_entries::const_iterator bs;
5004 for (bs = this->long_branch_stubs_.begin();
5005 bs != this->long_branch_stubs_.end();
5006 ++bs)
5007 {
5008 if (bs->first.save_res_)
5009 continue;
5010 p = oview + this->plt_size_ + bs->second;
5011 Address loc = this->stub_address() + this->plt_size_ + bs->second;
5012 Address delta = bs->first.dest_ - loc;
5013 if (delta + (1 << 25) < 2 << 25)
5014 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
5015 else if (!parameters->options().output_is_position_independent())
5016 {
5017 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
5018 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
5019 write_insn<big_endian>(p + 8, mtctr_12);
5020 write_insn<big_endian>(p + 12, bctr);
5021 }
5022 else
5023 {
5024 delta -= 8;
5025 write_insn<big_endian>(p + 0, mflr_0);
5026 write_insn<big_endian>(p + 4, bcl_20_31);
5027 write_insn<big_endian>(p + 8, mflr_12);
5028 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
5029 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
5030 write_insn<big_endian>(p + 20, mtlr_0);
5031 write_insn<big_endian>(p + 24, mtctr_12);
5032 write_insn<big_endian>(p + 28, bctr);
5033 }
5034 }
5035 }
5036 if (this->need_save_res_)
5037 {
5038 p = oview + this->plt_size_ + this->branch_size_;
5039 memcpy (p, this->targ_->savres_section()->contents(),
5040 this->targ_->savres_section()->data_size());
5041 }
5042 }
5043
5044 // Write out .glink.
5045
5046 template<int size, bool big_endian>
5047 void
5048 Output_data_glink<size, big_endian>::do_write(Output_file* of)
5049 {
5050 const section_size_type off = this->offset();
5051 const section_size_type oview_size =
5052 convert_to_section_size_type(this->data_size());
5053 unsigned char* const oview = of->get_output_view(off, oview_size);
5054 unsigned char* p;
5055
5056 // The base address of the .plt section.
5057 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5058 Address plt_base = this->targ_->plt_section()->address();
5059
5060 if (size == 64)
5061 {
5062 if (this->end_branch_table_ != 0)
5063 {
5064 // Write pltresolve stub.
5065 p = oview;
5066 Address after_bcl = this->address() + 16;
5067 Address pltoff = plt_base - after_bcl;
5068
5069 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
5070
5071 if (this->targ_->abiversion() < 2)
5072 {
5073 write_insn<big_endian>(p, mflr_12), p += 4;
5074 write_insn<big_endian>(p, bcl_20_31), p += 4;
5075 write_insn<big_endian>(p, mflr_11), p += 4;
5076 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5077 write_insn<big_endian>(p, mtlr_12), p += 4;
5078 write_insn<big_endian>(p, add_11_2_11), p += 4;
5079 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5080 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
5081 write_insn<big_endian>(p, mtctr_12), p += 4;
5082 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
5083 }
5084 else
5085 {
5086 write_insn<big_endian>(p, mflr_0), p += 4;
5087 write_insn<big_endian>(p, bcl_20_31), p += 4;
5088 write_insn<big_endian>(p, mflr_11), p += 4;
5089 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
5090 write_insn<big_endian>(p, mtlr_0), p += 4;
5091 write_insn<big_endian>(p, sub_12_12_11), p += 4;
5092 write_insn<big_endian>(p, add_11_2_11), p += 4;
5093 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
5094 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
5095 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
5096 write_insn<big_endian>(p, mtctr_12), p += 4;
5097 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
5098 }
5099 write_insn<big_endian>(p, bctr), p += 4;
5100 while (p < oview + this->pltresolve_size)
5101 write_insn<big_endian>(p, nop), p += 4;
5102
5103 // Write lazy link call stubs.
5104 uint32_t indx = 0;
5105 while (p < oview + this->end_branch_table_)
5106 {
5107 if (this->targ_->abiversion() < 2)
5108 {
5109 if (indx < 0x8000)
5110 {
5111 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
5112 }
5113 else
5114 {
5115 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
5116 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
5117 }
5118 }
5119 uint32_t branch_off = 8 - (p - oview);
5120 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
5121 indx++;
5122 }
5123 }
5124
5125 Address plt_base = this->targ_->plt_section()->address();
5126 Address iplt_base = invalid_address;
5127 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
5128 Address global_entry_base = this->address() + global_entry_off;
5129 typename Global_entry_stub_entries::const_iterator ge;
5130 for (ge = this->global_entry_stubs_.begin();
5131 ge != this->global_entry_stubs_.end();
5132 ++ge)
5133 {
5134 p = oview + global_entry_off + ge->second;
5135 Address plt_addr = ge->first->plt_offset();
5136 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
5137 && ge->first->can_use_relative_reloc(false))
5138 {
5139 if (iplt_base == invalid_address)
5140 iplt_base = this->targ_->iplt_section()->address();
5141 plt_addr += iplt_base;
5142 }
5143 else
5144 plt_addr += plt_base;
5145 Address my_addr = global_entry_base + ge->second;
5146 Address off = plt_addr - my_addr;
5147
5148 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
5149 gold_error(_("%s: linkage table error against `%s'"),
5150 ge->first->object()->name().c_str(),
5151 ge->first->demangled_name().c_str());
5152
5153 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
5154 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
5155 write_insn<big_endian>(p, mtctr_12), p += 4;
5156 write_insn<big_endian>(p, bctr);
5157 }
5158 }
5159 else
5160 {
5161 const Output_data_got_powerpc<size, big_endian>* got
5162 = this->targ_->got_section();
5163 // The address of _GLOBAL_OFFSET_TABLE_.
5164 Address g_o_t = got->address() + got->g_o_t();
5165
5166 // Write out pltresolve branch table.
5167 p = oview;
5168 unsigned int the_end = oview_size - this->pltresolve_size;
5169 unsigned char* end_p = oview + the_end;
5170 while (p < end_p - 8 * 4)
5171 write_insn<big_endian>(p, b + end_p - p), p += 4;
5172 while (p < end_p)
5173 write_insn<big_endian>(p, nop), p += 4;
5174
5175 // Write out pltresolve call stub.
5176 if (parameters->options().output_is_position_independent())
5177 {
5178 Address res0_off = 0;
5179 Address after_bcl_off = the_end + 12;
5180 Address bcl_res0 = after_bcl_off - res0_off;
5181
5182 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
5183 write_insn<big_endian>(p + 4, mflr_0);
5184 write_insn<big_endian>(p + 8, bcl_20_31);
5185 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
5186 write_insn<big_endian>(p + 16, mflr_12);
5187 write_insn<big_endian>(p + 20, mtlr_0);
5188 write_insn<big_endian>(p + 24, sub_11_11_12);
5189
5190 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
5191
5192 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
5193 if (ha(got_bcl) == ha(got_bcl + 4))
5194 {
5195 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
5196 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
5197 }
5198 else
5199 {
5200 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
5201 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
5202 }
5203 write_insn<big_endian>(p + 40, mtctr_0);
5204 write_insn<big_endian>(p + 44, add_0_11_11);
5205 write_insn<big_endian>(p + 48, add_11_0_11);
5206 write_insn<big_endian>(p + 52, bctr);
5207 write_insn<big_endian>(p + 56, nop);
5208 write_insn<big_endian>(p + 60, nop);
5209 }
5210 else
5211 {
5212 Address res0 = this->address();
5213
5214 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
5215 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
5216 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5217 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
5218 else
5219 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
5220 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
5221 write_insn<big_endian>(p + 16, mtctr_0);
5222 write_insn<big_endian>(p + 20, add_0_11_11);
5223 if (ha(g_o_t + 4) == ha(g_o_t + 8))
5224 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
5225 else
5226 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
5227 write_insn<big_endian>(p + 28, add_11_0_11);
5228 write_insn<big_endian>(p + 32, bctr);
5229 write_insn<big_endian>(p + 36, nop);
5230 write_insn<big_endian>(p + 40, nop);
5231 write_insn<big_endian>(p + 44, nop);
5232 write_insn<big_endian>(p + 48, nop);
5233 write_insn<big_endian>(p + 52, nop);
5234 write_insn<big_endian>(p + 56, nop);
5235 write_insn<big_endian>(p + 60, nop);
5236 }
5237 p += 64;
5238 }
5239
5240 of->write_output_view(off, oview_size, oview);
5241 }
5242
5243
5244 // A class to handle linker generated save/restore functions.
5245
5246 template<int size, bool big_endian>
5247 class Output_data_save_res : public Output_section_data_build
5248 {
5249 public:
5250 Output_data_save_res(Symbol_table* symtab);
5251
5252 const unsigned char*
5253 contents() const
5254 {
5255 return contents_;
5256 }
5257
5258 protected:
5259 // Write to a map file.
5260 void
5261 do_print_to_mapfile(Mapfile* mapfile) const
5262 { mapfile->print_output_data(this, _("** save/restore")); }
5263
5264 void
5265 do_write(Output_file*);
5266
5267 private:
5268 // The maximum size of save/restore contents.
5269 static const unsigned int savres_max = 218*4;
5270
5271 void
5272 savres_define(Symbol_table* symtab,
5273 const char *name,
5274 unsigned int lo, unsigned int hi,
5275 unsigned char* write_ent(unsigned char*, int),
5276 unsigned char* write_tail(unsigned char*, int));
5277
5278 unsigned char *contents_;
5279 };
5280
5281 template<bool big_endian>
5282 static unsigned char*
5283 savegpr0(unsigned char* p, int r)
5284 {
5285 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5286 write_insn<big_endian>(p, insn);
5287 return p + 4;
5288 }
5289
5290 template<bool big_endian>
5291 static unsigned char*
5292 savegpr0_tail(unsigned char* p, int r)
5293 {
5294 p = savegpr0<big_endian>(p, r);
5295 uint32_t insn = std_0_1 + 16;
5296 write_insn<big_endian>(p, insn);
5297 p = p + 4;
5298 write_insn<big_endian>(p, blr);
5299 return p + 4;
5300 }
5301
5302 template<bool big_endian>
5303 static unsigned char*
5304 restgpr0(unsigned char* p, int r)
5305 {
5306 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5307 write_insn<big_endian>(p, insn);
5308 return p + 4;
5309 }
5310
5311 template<bool big_endian>
5312 static unsigned char*
5313 restgpr0_tail(unsigned char* p, int r)
5314 {
5315 uint32_t insn = ld_0_1 + 16;
5316 write_insn<big_endian>(p, insn);
5317 p = p + 4;
5318 p = restgpr0<big_endian>(p, r);
5319 write_insn<big_endian>(p, mtlr_0);
5320 p = p + 4;
5321 if (r == 29)
5322 {
5323 p = restgpr0<big_endian>(p, 30);
5324 p = restgpr0<big_endian>(p, 31);
5325 }
5326 write_insn<big_endian>(p, blr);
5327 return p + 4;
5328 }
5329
5330 template<bool big_endian>
5331 static unsigned char*
5332 savegpr1(unsigned char* p, int r)
5333 {
5334 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5335 write_insn<big_endian>(p, insn);
5336 return p + 4;
5337 }
5338
5339 template<bool big_endian>
5340 static unsigned char*
5341 savegpr1_tail(unsigned char* p, int r)
5342 {
5343 p = savegpr1<big_endian>(p, r);
5344 write_insn<big_endian>(p, blr);
5345 return p + 4;
5346 }
5347
5348 template<bool big_endian>
5349 static unsigned char*
5350 restgpr1(unsigned char* p, int r)
5351 {
5352 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5353 write_insn<big_endian>(p, insn);
5354 return p + 4;
5355 }
5356
5357 template<bool big_endian>
5358 static unsigned char*
5359 restgpr1_tail(unsigned char* p, int r)
5360 {
5361 p = restgpr1<big_endian>(p, r);
5362 write_insn<big_endian>(p, blr);
5363 return p + 4;
5364 }
5365
5366 template<bool big_endian>
5367 static unsigned char*
5368 savefpr(unsigned char* p, int r)
5369 {
5370 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5371 write_insn<big_endian>(p, insn);
5372 return p + 4;
5373 }
5374
5375 template<bool big_endian>
5376 static unsigned char*
5377 savefpr0_tail(unsigned char* p, int r)
5378 {
5379 p = savefpr<big_endian>(p, r);
5380 write_insn<big_endian>(p, std_0_1 + 16);
5381 p = p + 4;
5382 write_insn<big_endian>(p, blr);
5383 return p + 4;
5384 }
5385
5386 template<bool big_endian>
5387 static unsigned char*
5388 restfpr(unsigned char* p, int r)
5389 {
5390 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5391 write_insn<big_endian>(p, insn);
5392 return p + 4;
5393 }
5394
5395 template<bool big_endian>
5396 static unsigned char*
5397 restfpr0_tail(unsigned char* p, int r)
5398 {
5399 write_insn<big_endian>(p, ld_0_1 + 16);
5400 p = p + 4;
5401 p = restfpr<big_endian>(p, r);
5402 write_insn<big_endian>(p, mtlr_0);
5403 p = p + 4;
5404 if (r == 29)
5405 {
5406 p = restfpr<big_endian>(p, 30);
5407 p = restfpr<big_endian>(p, 31);
5408 }
5409 write_insn<big_endian>(p, blr);
5410 return p + 4;
5411 }
5412
5413 template<bool big_endian>
5414 static unsigned char*
5415 savefpr1_tail(unsigned char* p, int r)
5416 {
5417 p = savefpr<big_endian>(p, r);
5418 write_insn<big_endian>(p, blr);
5419 return p + 4;
5420 }
5421
5422 template<bool big_endian>
5423 static unsigned char*
5424 restfpr1_tail(unsigned char* p, int r)
5425 {
5426 p = restfpr<big_endian>(p, r);
5427 write_insn<big_endian>(p, blr);
5428 return p + 4;
5429 }
5430
5431 template<bool big_endian>
5432 static unsigned char*
5433 savevr(unsigned char* p, int r)
5434 {
5435 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5436 write_insn<big_endian>(p, insn);
5437 p = p + 4;
5438 insn = stvx_0_12_0 + (r << 21);
5439 write_insn<big_endian>(p, insn);
5440 return p + 4;
5441 }
5442
5443 template<bool big_endian>
5444 static unsigned char*
5445 savevr_tail(unsigned char* p, int r)
5446 {
5447 p = savevr<big_endian>(p, r);
5448 write_insn<big_endian>(p, blr);
5449 return p + 4;
5450 }
5451
5452 template<bool big_endian>
5453 static unsigned char*
5454 restvr(unsigned char* p, int r)
5455 {
5456 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5457 write_insn<big_endian>(p, insn);
5458 p = p + 4;
5459 insn = lvx_0_12_0 + (r << 21);
5460 write_insn<big_endian>(p, insn);
5461 return p + 4;
5462 }
5463
5464 template<bool big_endian>
5465 static unsigned char*
5466 restvr_tail(unsigned char* p, int r)
5467 {
5468 p = restvr<big_endian>(p, r);
5469 write_insn<big_endian>(p, blr);
5470 return p + 4;
5471 }
5472
5473
5474 template<int size, bool big_endian>
5475 Output_data_save_res<size, big_endian>::Output_data_save_res(
5476 Symbol_table* symtab)
5477 : Output_section_data_build(4),
5478 contents_(NULL)
5479 {
5480 this->savres_define(symtab,
5481 "_savegpr0_", 14, 31,
5482 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5483 this->savres_define(symtab,
5484 "_restgpr0_", 14, 29,
5485 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5486 this->savres_define(symtab,
5487 "_restgpr0_", 30, 31,
5488 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5489 this->savres_define(symtab,
5490 "_savegpr1_", 14, 31,
5491 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5492 this->savres_define(symtab,
5493 "_restgpr1_", 14, 31,
5494 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5495 this->savres_define(symtab,
5496 "_savefpr_", 14, 31,
5497 savefpr<big_endian>, savefpr0_tail<big_endian>);
5498 this->savres_define(symtab,
5499 "_restfpr_", 14, 29,
5500 restfpr<big_endian>, restfpr0_tail<big_endian>);
5501 this->savres_define(symtab,
5502 "_restfpr_", 30, 31,
5503 restfpr<big_endian>, restfpr0_tail<big_endian>);
5504 this->savres_define(symtab,
5505 "._savef", 14, 31,
5506 savefpr<big_endian>, savefpr1_tail<big_endian>);
5507 this->savres_define(symtab,
5508 "._restf", 14, 31,
5509 restfpr<big_endian>, restfpr1_tail<big_endian>);
5510 this->savres_define(symtab,
5511 "_savevr_", 20, 31,
5512 savevr<big_endian>, savevr_tail<big_endian>);
5513 this->savres_define(symtab,
5514 "_restvr_", 20, 31,
5515 restvr<big_endian>, restvr_tail<big_endian>);
5516 }
5517
5518 template<int size, bool big_endian>
5519 void
5520 Output_data_save_res<size, big_endian>::savres_define(
5521 Symbol_table* symtab,
5522 const char *name,
5523 unsigned int lo, unsigned int hi,
5524 unsigned char* write_ent(unsigned char*, int),
5525 unsigned char* write_tail(unsigned char*, int))
5526 {
5527 size_t len = strlen(name);
5528 bool writing = false;
5529 char sym[16];
5530
5531 memcpy(sym, name, len);
5532 sym[len + 2] = 0;
5533
5534 for (unsigned int i = lo; i <= hi; i++)
5535 {
5536 sym[len + 0] = i / 10 + '0';
5537 sym[len + 1] = i % 10 + '0';
5538 Symbol* gsym = symtab->lookup(sym);
5539 bool refd = gsym != NULL && gsym->is_undefined();
5540 writing = writing || refd;
5541 if (writing)
5542 {
5543 if (this->contents_ == NULL)
5544 this->contents_ = new unsigned char[this->savres_max];
5545
5546 section_size_type value = this->current_data_size();
5547 unsigned char* p = this->contents_ + value;
5548 if (i != hi)
5549 p = write_ent(p, i);
5550 else
5551 p = write_tail(p, i);
5552 section_size_type cur_size = p - this->contents_;
5553 this->set_current_data_size(cur_size);
5554 if (refd)
5555 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5556 this, value, cur_size - value,
5557 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5558 elfcpp::STV_HIDDEN, 0, false, false);
5559 }
5560 }
5561 }
5562
5563 // Write out save/restore.
5564
5565 template<int size, bool big_endian>
5566 void
5567 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5568 {
5569 const section_size_type off = this->offset();
5570 const section_size_type oview_size =
5571 convert_to_section_size_type(this->data_size());
5572 unsigned char* const oview = of->get_output_view(off, oview_size);
5573 memcpy(oview, this->contents_, oview_size);
5574 of->write_output_view(off, oview_size, oview);
5575 }
5576
5577
5578 // Create the glink section.
5579
5580 template<int size, bool big_endian>
5581 void
5582 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5583 {
5584 if (this->glink_ == NULL)
5585 {
5586 this->glink_ = new Output_data_glink<size, big_endian>(this);
5587 this->glink_->add_eh_frame(layout);
5588 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5589 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5590 this->glink_, ORDER_TEXT, false);
5591 }
5592 }
5593
5594 // Create a PLT entry for a global symbol.
5595
5596 template<int size, bool big_endian>
5597 void
5598 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5599 Layout* layout,
5600 Symbol* gsym)
5601 {
5602 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5603 && gsym->can_use_relative_reloc(false))
5604 {
5605 if (this->iplt_ == NULL)
5606 this->make_iplt_section(symtab, layout);
5607 this->iplt_->add_ifunc_entry(gsym);
5608 }
5609 else
5610 {
5611 if (this->plt_ == NULL)
5612 this->make_plt_section(symtab, layout);
5613 this->plt_->add_entry(gsym);
5614 }
5615 }
5616
5617 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5618
5619 template<int size, bool big_endian>
5620 void
5621 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5622 Symbol_table* symtab,
5623 Layout* layout,
5624 Sized_relobj_file<size, big_endian>* relobj,
5625 unsigned int r_sym)
5626 {
5627 if (this->iplt_ == NULL)
5628 this->make_iplt_section(symtab, layout);
5629 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5630 }
5631
5632 // Return the number of entries in the PLT.
5633
5634 template<int size, bool big_endian>
5635 unsigned int
5636 Target_powerpc<size, big_endian>::plt_entry_count() const
5637 {
5638 if (this->plt_ == NULL)
5639 return 0;
5640 return this->plt_->entry_count();
5641 }
5642
5643 // Create a GOT entry for local dynamic __tls_get_addr calls.
5644
5645 template<int size, bool big_endian>
5646 unsigned int
5647 Target_powerpc<size, big_endian>::tlsld_got_offset(
5648 Symbol_table* symtab,
5649 Layout* layout,
5650 Sized_relobj_file<size, big_endian>* object)
5651 {
5652 if (this->tlsld_got_offset_ == -1U)
5653 {
5654 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5655 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5656 Output_data_got_powerpc<size, big_endian>* got
5657 = this->got_section(symtab, layout);
5658 unsigned int got_offset = got->add_constant_pair(0, 0);
5659 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5660 got_offset, 0);
5661 this->tlsld_got_offset_ = got_offset;
5662 }
5663 return this->tlsld_got_offset_;
5664 }
5665
5666 // Get the Reference_flags for a particular relocation.
5667
5668 template<int size, bool big_endian>
5669 int
5670 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5671 unsigned int r_type,
5672 const Target_powerpc* target)
5673 {
5674 int ref = 0;
5675
5676 switch (r_type)
5677 {
5678 case elfcpp::R_POWERPC_NONE:
5679 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5680 case elfcpp::R_POWERPC_GNU_VTENTRY:
5681 case elfcpp::R_PPC64_TOC:
5682 // No symbol reference.
5683 break;
5684
5685 case elfcpp::R_PPC64_ADDR64:
5686 case elfcpp::R_PPC64_UADDR64:
5687 case elfcpp::R_POWERPC_ADDR32:
5688 case elfcpp::R_POWERPC_UADDR32:
5689 case elfcpp::R_POWERPC_ADDR16:
5690 case elfcpp::R_POWERPC_UADDR16:
5691 case elfcpp::R_POWERPC_ADDR16_LO:
5692 case elfcpp::R_POWERPC_ADDR16_HI:
5693 case elfcpp::R_POWERPC_ADDR16_HA:
5694 ref = Symbol::ABSOLUTE_REF;
5695 break;
5696
5697 case elfcpp::R_POWERPC_ADDR24:
5698 case elfcpp::R_POWERPC_ADDR14:
5699 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5700 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5701 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5702 break;
5703
5704 case elfcpp::R_PPC64_REL64:
5705 case elfcpp::R_POWERPC_REL32:
5706 case elfcpp::R_PPC_LOCAL24PC:
5707 case elfcpp::R_POWERPC_REL16:
5708 case elfcpp::R_POWERPC_REL16_LO:
5709 case elfcpp::R_POWERPC_REL16_HI:
5710 case elfcpp::R_POWERPC_REL16_HA:
5711 ref = Symbol::RELATIVE_REF;
5712 break;
5713
5714 case elfcpp::R_POWERPC_REL24:
5715 case elfcpp::R_PPC_PLTREL24:
5716 case elfcpp::R_POWERPC_REL14:
5717 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5718 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5719 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5720 break;
5721
5722 case elfcpp::R_POWERPC_GOT16:
5723 case elfcpp::R_POWERPC_GOT16_LO:
5724 case elfcpp::R_POWERPC_GOT16_HI:
5725 case elfcpp::R_POWERPC_GOT16_HA:
5726 case elfcpp::R_PPC64_GOT16_DS:
5727 case elfcpp::R_PPC64_GOT16_LO_DS:
5728 case elfcpp::R_PPC64_TOC16:
5729 case elfcpp::R_PPC64_TOC16_LO:
5730 case elfcpp::R_PPC64_TOC16_HI:
5731 case elfcpp::R_PPC64_TOC16_HA:
5732 case elfcpp::R_PPC64_TOC16_DS:
5733 case elfcpp::R_PPC64_TOC16_LO_DS:
5734 ref = Symbol::RELATIVE_REF;
5735 break;
5736
5737 case elfcpp::R_POWERPC_GOT_TPREL16:
5738 case elfcpp::R_POWERPC_TLS:
5739 ref = Symbol::TLS_REF;
5740 break;
5741
5742 case elfcpp::R_POWERPC_COPY:
5743 case elfcpp::R_POWERPC_GLOB_DAT:
5744 case elfcpp::R_POWERPC_JMP_SLOT:
5745 case elfcpp::R_POWERPC_RELATIVE:
5746 case elfcpp::R_POWERPC_DTPMOD:
5747 default:
5748 // Not expected. We will give an error later.
5749 break;
5750 }
5751
5752 if (size == 64 && target->abiversion() < 2)
5753 ref |= Symbol::FUNC_DESC_ABI;
5754 return ref;
5755 }
5756
5757 // Report an unsupported relocation against a local symbol.
5758
5759 template<int size, bool big_endian>
5760 void
5761 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5762 Sized_relobj_file<size, big_endian>* object,
5763 unsigned int r_type)
5764 {
5765 gold_error(_("%s: unsupported reloc %u against local symbol"),
5766 object->name().c_str(), r_type);
5767 }
5768
5769 // We are about to emit a dynamic relocation of type R_TYPE. If the
5770 // dynamic linker does not support it, issue an error.
5771
5772 template<int size, bool big_endian>
5773 void
5774 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5775 unsigned int r_type)
5776 {
5777 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5778
5779 // These are the relocation types supported by glibc for both 32-bit
5780 // and 64-bit powerpc.
5781 switch (r_type)
5782 {
5783 case elfcpp::R_POWERPC_NONE:
5784 case elfcpp::R_POWERPC_RELATIVE:
5785 case elfcpp::R_POWERPC_GLOB_DAT:
5786 case elfcpp::R_POWERPC_DTPMOD:
5787 case elfcpp::R_POWERPC_DTPREL:
5788 case elfcpp::R_POWERPC_TPREL:
5789 case elfcpp::R_POWERPC_JMP_SLOT:
5790 case elfcpp::R_POWERPC_COPY:
5791 case elfcpp::R_POWERPC_IRELATIVE:
5792 case elfcpp::R_POWERPC_ADDR32:
5793 case elfcpp::R_POWERPC_UADDR32:
5794 case elfcpp::R_POWERPC_ADDR24:
5795 case elfcpp::R_POWERPC_ADDR16:
5796 case elfcpp::R_POWERPC_UADDR16:
5797 case elfcpp::R_POWERPC_ADDR16_LO:
5798 case elfcpp::R_POWERPC_ADDR16_HI:
5799 case elfcpp::R_POWERPC_ADDR16_HA:
5800 case elfcpp::R_POWERPC_ADDR14:
5801 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5802 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5803 case elfcpp::R_POWERPC_REL32:
5804 case elfcpp::R_POWERPC_REL24:
5805 case elfcpp::R_POWERPC_TPREL16:
5806 case elfcpp::R_POWERPC_TPREL16_LO:
5807 case elfcpp::R_POWERPC_TPREL16_HI:
5808 case elfcpp::R_POWERPC_TPREL16_HA:
5809 return;
5810
5811 default:
5812 break;
5813 }
5814
5815 if (size == 64)
5816 {
5817 switch (r_type)
5818 {
5819 // These are the relocation types supported only on 64-bit.
5820 case elfcpp::R_PPC64_ADDR64:
5821 case elfcpp::R_PPC64_UADDR64:
5822 case elfcpp::R_PPC64_JMP_IREL:
5823 case elfcpp::R_PPC64_ADDR16_DS:
5824 case elfcpp::R_PPC64_ADDR16_LO_DS:
5825 case elfcpp::R_PPC64_ADDR16_HIGH:
5826 case elfcpp::R_PPC64_ADDR16_HIGHA:
5827 case elfcpp::R_PPC64_ADDR16_HIGHER:
5828 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5829 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5830 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5831 case elfcpp::R_PPC64_REL64:
5832 case elfcpp::R_POWERPC_ADDR30:
5833 case elfcpp::R_PPC64_TPREL16_DS:
5834 case elfcpp::R_PPC64_TPREL16_LO_DS:
5835 case elfcpp::R_PPC64_TPREL16_HIGH:
5836 case elfcpp::R_PPC64_TPREL16_HIGHA:
5837 case elfcpp::R_PPC64_TPREL16_HIGHER:
5838 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5839 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5840 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5841 return;
5842
5843 default:
5844 break;
5845 }
5846 }
5847 else
5848 {
5849 switch (r_type)
5850 {
5851 // These are the relocation types supported only on 32-bit.
5852 // ??? glibc ld.so doesn't need to support these.
5853 case elfcpp::R_POWERPC_DTPREL16:
5854 case elfcpp::R_POWERPC_DTPREL16_LO:
5855 case elfcpp::R_POWERPC_DTPREL16_HI:
5856 case elfcpp::R_POWERPC_DTPREL16_HA:
5857 return;
5858
5859 default:
5860 break;
5861 }
5862 }
5863
5864 // This prevents us from issuing more than one error per reloc
5865 // section. But we can still wind up issuing more than one
5866 // error per object file.
5867 if (this->issued_non_pic_error_)
5868 return;
5869 gold_assert(parameters->options().output_is_position_independent());
5870 object->error(_("requires unsupported dynamic reloc; "
5871 "recompile with -fPIC"));
5872 this->issued_non_pic_error_ = true;
5873 return;
5874 }
5875
5876 // Return whether we need to make a PLT entry for a relocation of the
5877 // given type against a STT_GNU_IFUNC symbol.
5878
5879 template<int size, bool big_endian>
5880 bool
5881 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5882 Target_powerpc<size, big_endian>* target,
5883 Sized_relobj_file<size, big_endian>* object,
5884 unsigned int r_type,
5885 bool report_err)
5886 {
5887 // In non-pic code any reference will resolve to the plt call stub
5888 // for the ifunc symbol.
5889 if ((size == 32 || target->abiversion() >= 2)
5890 && !parameters->options().output_is_position_independent())
5891 return true;
5892
5893 switch (r_type)
5894 {
5895 // Word size refs from data sections are OK, but don't need a PLT entry.
5896 case elfcpp::R_POWERPC_ADDR32:
5897 case elfcpp::R_POWERPC_UADDR32:
5898 if (size == 32)
5899 return false;
5900 break;
5901
5902 case elfcpp::R_PPC64_ADDR64:
5903 case elfcpp::R_PPC64_UADDR64:
5904 if (size == 64)
5905 return false;
5906 break;
5907
5908 // GOT refs are good, but also don't need a PLT entry.
5909 case elfcpp::R_POWERPC_GOT16:
5910 case elfcpp::R_POWERPC_GOT16_LO:
5911 case elfcpp::R_POWERPC_GOT16_HI:
5912 case elfcpp::R_POWERPC_GOT16_HA:
5913 case elfcpp::R_PPC64_GOT16_DS:
5914 case elfcpp::R_PPC64_GOT16_LO_DS:
5915 return false;
5916
5917 // Function calls are good, and these do need a PLT entry.
5918 case elfcpp::R_POWERPC_ADDR24:
5919 case elfcpp::R_POWERPC_ADDR14:
5920 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5921 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5922 case elfcpp::R_POWERPC_REL24:
5923 case elfcpp::R_PPC_PLTREL24:
5924 case elfcpp::R_POWERPC_REL14:
5925 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5926 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5927 return true;
5928
5929 default:
5930 break;
5931 }
5932
5933 // Anything else is a problem.
5934 // If we are building a static executable, the libc startup function
5935 // responsible for applying indirect function relocations is going
5936 // to complain about the reloc type.
5937 // If we are building a dynamic executable, we will have a text
5938 // relocation. The dynamic loader will set the text segment
5939 // writable and non-executable to apply text relocations. So we'll
5940 // segfault when trying to run the indirection function to resolve
5941 // the reloc.
5942 if (report_err)
5943 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5944 object->name().c_str(), r_type);
5945 return false;
5946 }
5947
5948 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5949 // reloc.
5950
5951 static bool
5952 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
5953 {
5954 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
5955 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
5956 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
5957 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
5958 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
5959 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
5960 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
5961 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
5962 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
5963 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
5964 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
5965 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
5966 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
5967 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
5968 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
5969 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
5970 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
5971 /* Exclude lfqu by testing reloc. If relocs are ever
5972 defined for the reduced D field in psq_lu then those
5973 will need testing too. */
5974 && r_type != elfcpp::R_PPC64_TOC16_LO
5975 && r_type != elfcpp::R_POWERPC_GOT16_LO)
5976 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
5977 && (insn & 1) == 0)
5978 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
5979 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
5980 /* Exclude stfqu. psq_stu as above for psq_lu. */
5981 && r_type != elfcpp::R_PPC64_TOC16_LO
5982 && r_type != elfcpp::R_POWERPC_GOT16_LO)
5983 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
5984 && (insn & 1) == 0));
5985 }
5986
5987 // Scan a relocation for a local symbol.
5988
5989 template<int size, bool big_endian>
5990 inline void
5991 Target_powerpc<size, big_endian>::Scan::local(
5992 Symbol_table* symtab,
5993 Layout* layout,
5994 Target_powerpc<size, big_endian>* target,
5995 Sized_relobj_file<size, big_endian>* object,
5996 unsigned int data_shndx,
5997 Output_section* output_section,
5998 const elfcpp::Rela<size, big_endian>& reloc,
5999 unsigned int r_type,
6000 const elfcpp::Sym<size, big_endian>& lsym,
6001 bool is_discarded)
6002 {
6003 this->maybe_skip_tls_get_addr_call(r_type, NULL);
6004
6005 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6006 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6007 {
6008 this->expect_tls_get_addr_call();
6009 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6010 if (tls_type != tls::TLSOPT_NONE)
6011 this->skip_next_tls_get_addr_call();
6012 }
6013 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6014 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6015 {
6016 this->expect_tls_get_addr_call();
6017 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6018 if (tls_type != tls::TLSOPT_NONE)
6019 this->skip_next_tls_get_addr_call();
6020 }
6021
6022 Powerpc_relobj<size, big_endian>* ppc_object
6023 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6024
6025 if (is_discarded)
6026 {
6027 if (size == 64
6028 && data_shndx == ppc_object->opd_shndx()
6029 && r_type == elfcpp::R_PPC64_ADDR64)
6030 ppc_object->set_opd_discard(reloc.get_r_offset());
6031 return;
6032 }
6033
6034 // A local STT_GNU_IFUNC symbol may require a PLT entry.
6035 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
6036 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6037 {
6038 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6039 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6040 r_type, r_sym, reloc.get_r_addend());
6041 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
6042 }
6043
6044 switch (r_type)
6045 {
6046 case elfcpp::R_POWERPC_NONE:
6047 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6048 case elfcpp::R_POWERPC_GNU_VTENTRY:
6049 case elfcpp::R_PPC64_TOCSAVE:
6050 case elfcpp::R_POWERPC_TLS:
6051 case elfcpp::R_PPC64_ENTRY:
6052 break;
6053
6054 case elfcpp::R_PPC64_TOC:
6055 {
6056 Output_data_got_powerpc<size, big_endian>* got
6057 = target->got_section(symtab, layout);
6058 if (parameters->options().output_is_position_independent())
6059 {
6060 Address off = reloc.get_r_offset();
6061 if (size == 64
6062 && target->abiversion() < 2
6063 && data_shndx == ppc_object->opd_shndx()
6064 && ppc_object->get_opd_discard(off - 8))
6065 break;
6066
6067 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6068 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6069 rela_dyn->add_output_section_relative(got->output_section(),
6070 elfcpp::R_POWERPC_RELATIVE,
6071 output_section,
6072 object, data_shndx, off,
6073 symobj->toc_base_offset());
6074 }
6075 }
6076 break;
6077
6078 case elfcpp::R_PPC64_ADDR64:
6079 case elfcpp::R_PPC64_UADDR64:
6080 case elfcpp::R_POWERPC_ADDR32:
6081 case elfcpp::R_POWERPC_UADDR32:
6082 case elfcpp::R_POWERPC_ADDR24:
6083 case elfcpp::R_POWERPC_ADDR16:
6084 case elfcpp::R_POWERPC_ADDR16_LO:
6085 case elfcpp::R_POWERPC_ADDR16_HI:
6086 case elfcpp::R_POWERPC_ADDR16_HA:
6087 case elfcpp::R_POWERPC_UADDR16:
6088 case elfcpp::R_PPC64_ADDR16_HIGH:
6089 case elfcpp::R_PPC64_ADDR16_HIGHA:
6090 case elfcpp::R_PPC64_ADDR16_HIGHER:
6091 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6092 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6093 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6094 case elfcpp::R_PPC64_ADDR16_DS:
6095 case elfcpp::R_PPC64_ADDR16_LO_DS:
6096 case elfcpp::R_POWERPC_ADDR14:
6097 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6098 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6099 // If building a shared library (or a position-independent
6100 // executable), we need to create a dynamic relocation for
6101 // this location.
6102 if (parameters->options().output_is_position_independent()
6103 || (size == 64 && is_ifunc && target->abiversion() < 2))
6104 {
6105 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6106 is_ifunc);
6107 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6108 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
6109 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
6110 {
6111 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6112 : elfcpp::R_POWERPC_RELATIVE);
6113 rela_dyn->add_local_relative(object, r_sym, dynrel,
6114 output_section, data_shndx,
6115 reloc.get_r_offset(),
6116 reloc.get_r_addend(), false);
6117 }
6118 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
6119 {
6120 check_non_pic(object, r_type);
6121 rela_dyn->add_local(object, r_sym, r_type, output_section,
6122 data_shndx, reloc.get_r_offset(),
6123 reloc.get_r_addend());
6124 }
6125 else
6126 {
6127 gold_assert(lsym.get_st_value() == 0);
6128 unsigned int shndx = lsym.get_st_shndx();
6129 bool is_ordinary;
6130 shndx = object->adjust_sym_shndx(r_sym, shndx,
6131 &is_ordinary);
6132 if (!is_ordinary)
6133 object->error(_("section symbol %u has bad shndx %u"),
6134 r_sym, shndx);
6135 else
6136 rela_dyn->add_local_section(object, shndx, r_type,
6137 output_section, data_shndx,
6138 reloc.get_r_offset());
6139 }
6140 }
6141 break;
6142
6143 case elfcpp::R_POWERPC_REL24:
6144 case elfcpp::R_PPC_PLTREL24:
6145 case elfcpp::R_PPC_LOCAL24PC:
6146 case elfcpp::R_POWERPC_REL14:
6147 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6148 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6149 if (!is_ifunc)
6150 {
6151 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6152 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6153 r_type, r_sym, reloc.get_r_addend());
6154 }
6155 break;
6156
6157 case elfcpp::R_PPC64_REL64:
6158 case elfcpp::R_POWERPC_REL32:
6159 case elfcpp::R_POWERPC_REL16:
6160 case elfcpp::R_POWERPC_REL16_LO:
6161 case elfcpp::R_POWERPC_REL16_HI:
6162 case elfcpp::R_POWERPC_REL16_HA:
6163 case elfcpp::R_POWERPC_REL16DX_HA:
6164 case elfcpp::R_POWERPC_SECTOFF:
6165 case elfcpp::R_POWERPC_SECTOFF_LO:
6166 case elfcpp::R_POWERPC_SECTOFF_HI:
6167 case elfcpp::R_POWERPC_SECTOFF_HA:
6168 case elfcpp::R_PPC64_SECTOFF_DS:
6169 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6170 case elfcpp::R_POWERPC_TPREL16:
6171 case elfcpp::R_POWERPC_TPREL16_LO:
6172 case elfcpp::R_POWERPC_TPREL16_HI:
6173 case elfcpp::R_POWERPC_TPREL16_HA:
6174 case elfcpp::R_PPC64_TPREL16_DS:
6175 case elfcpp::R_PPC64_TPREL16_LO_DS:
6176 case elfcpp::R_PPC64_TPREL16_HIGH:
6177 case elfcpp::R_PPC64_TPREL16_HIGHA:
6178 case elfcpp::R_PPC64_TPREL16_HIGHER:
6179 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6180 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6181 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6182 case elfcpp::R_POWERPC_DTPREL16:
6183 case elfcpp::R_POWERPC_DTPREL16_LO:
6184 case elfcpp::R_POWERPC_DTPREL16_HI:
6185 case elfcpp::R_POWERPC_DTPREL16_HA:
6186 case elfcpp::R_PPC64_DTPREL16_DS:
6187 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6188 case elfcpp::R_PPC64_DTPREL16_HIGH:
6189 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6190 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6191 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6192 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6193 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6194 case elfcpp::R_PPC64_TLSGD:
6195 case elfcpp::R_PPC64_TLSLD:
6196 case elfcpp::R_PPC64_ADDR64_LOCAL:
6197 break;
6198
6199 case elfcpp::R_POWERPC_GOT16:
6200 case elfcpp::R_POWERPC_GOT16_LO:
6201 case elfcpp::R_POWERPC_GOT16_HI:
6202 case elfcpp::R_POWERPC_GOT16_HA:
6203 case elfcpp::R_PPC64_GOT16_DS:
6204 case elfcpp::R_PPC64_GOT16_LO_DS:
6205 {
6206 // The symbol requires a GOT entry.
6207 Output_data_got_powerpc<size, big_endian>* got
6208 = target->got_section(symtab, layout);
6209 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6210
6211 if (!parameters->options().output_is_position_independent())
6212 {
6213 if (is_ifunc
6214 && (size == 32 || target->abiversion() >= 2))
6215 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
6216 else
6217 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
6218 }
6219 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
6220 {
6221 // If we are generating a shared object or a pie, this
6222 // symbol's GOT entry will be set by a dynamic relocation.
6223 unsigned int off;
6224 off = got->add_constant(0);
6225 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
6226
6227 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
6228 is_ifunc);
6229 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6230 : elfcpp::R_POWERPC_RELATIVE);
6231 rela_dyn->add_local_relative(object, r_sym, dynrel,
6232 got, off, 0, false);
6233 }
6234 }
6235 break;
6236
6237 case elfcpp::R_PPC64_TOC16:
6238 case elfcpp::R_PPC64_TOC16_LO:
6239 case elfcpp::R_PPC64_TOC16_HI:
6240 case elfcpp::R_PPC64_TOC16_HA:
6241 case elfcpp::R_PPC64_TOC16_DS:
6242 case elfcpp::R_PPC64_TOC16_LO_DS:
6243 // We need a GOT section.
6244 target->got_section(symtab, layout);
6245 break;
6246
6247 case elfcpp::R_POWERPC_GOT_TLSGD16:
6248 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6249 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6250 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6251 {
6252 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
6253 if (tls_type == tls::TLSOPT_NONE)
6254 {
6255 Output_data_got_powerpc<size, big_endian>* got
6256 = target->got_section(symtab, layout);
6257 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6258 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6259 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
6260 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
6261 }
6262 else if (tls_type == tls::TLSOPT_TO_LE)
6263 {
6264 // no GOT relocs needed for Local Exec.
6265 }
6266 else
6267 gold_unreachable();
6268 }
6269 break;
6270
6271 case elfcpp::R_POWERPC_GOT_TLSLD16:
6272 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6273 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6274 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6275 {
6276 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6277 if (tls_type == tls::TLSOPT_NONE)
6278 target->tlsld_got_offset(symtab, layout, object);
6279 else if (tls_type == tls::TLSOPT_TO_LE)
6280 {
6281 // no GOT relocs needed for Local Exec.
6282 if (parameters->options().emit_relocs())
6283 {
6284 Output_section* os = layout->tls_segment()->first_section();
6285 gold_assert(os != NULL);
6286 os->set_needs_symtab_index();
6287 }
6288 }
6289 else
6290 gold_unreachable();
6291 }
6292 break;
6293
6294 case elfcpp::R_POWERPC_GOT_DTPREL16:
6295 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6296 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6297 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6298 {
6299 Output_data_got_powerpc<size, big_endian>* got
6300 = target->got_section(symtab, layout);
6301 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6302 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
6303 }
6304 break;
6305
6306 case elfcpp::R_POWERPC_GOT_TPREL16:
6307 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6308 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6309 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6310 {
6311 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
6312 if (tls_type == tls::TLSOPT_NONE)
6313 {
6314 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6315 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
6316 {
6317 Output_data_got_powerpc<size, big_endian>* got
6318 = target->got_section(symtab, layout);
6319 unsigned int off = got->add_constant(0);
6320 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
6321
6322 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6323 rela_dyn->add_symbolless_local_addend(object, r_sym,
6324 elfcpp::R_POWERPC_TPREL,
6325 got, off, 0);
6326 }
6327 }
6328 else if (tls_type == tls::TLSOPT_TO_LE)
6329 {
6330 // no GOT relocs needed for Local Exec.
6331 }
6332 else
6333 gold_unreachable();
6334 }
6335 break;
6336
6337 default:
6338 unsupported_reloc_local(object, r_type);
6339 break;
6340 }
6341
6342 if (size == 64
6343 && parameters->options().toc_optimize())
6344 {
6345 if (data_shndx == ppc_object->toc_shndx())
6346 {
6347 bool ok = true;
6348 if (r_type != elfcpp::R_PPC64_ADDR64
6349 || (is_ifunc && target->abiversion() < 2))
6350 ok = false;
6351 else if (parameters->options().output_is_position_independent())
6352 {
6353 if (is_ifunc)
6354 ok = false;
6355 else
6356 {
6357 unsigned int shndx = lsym.get_st_shndx();
6358 if (shndx >= elfcpp::SHN_LORESERVE
6359 && shndx != elfcpp::SHN_XINDEX)
6360 ok = false;
6361 }
6362 }
6363 if (!ok)
6364 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6365 }
6366
6367 enum {no_check, check_lo, check_ha} insn_check;
6368 switch (r_type)
6369 {
6370 default:
6371 insn_check = no_check;
6372 break;
6373
6374 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6375 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6376 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6377 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6378 case elfcpp::R_POWERPC_GOT16_HA:
6379 case elfcpp::R_PPC64_TOC16_HA:
6380 insn_check = check_ha;
6381 break;
6382
6383 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6384 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6385 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6386 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6387 case elfcpp::R_POWERPC_GOT16_LO:
6388 case elfcpp::R_PPC64_GOT16_LO_DS:
6389 case elfcpp::R_PPC64_TOC16_LO:
6390 case elfcpp::R_PPC64_TOC16_LO_DS:
6391 insn_check = check_lo;
6392 break;
6393 }
6394
6395 section_size_type slen;
6396 const unsigned char* view = NULL;
6397 if (insn_check != no_check)
6398 {
6399 view = ppc_object->section_contents(data_shndx, &slen, false);
6400 section_size_type off =
6401 convert_to_section_size_type(reloc.get_r_offset()) & -4;
6402 if (off < slen)
6403 {
6404 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
6405 if (insn_check == check_lo
6406 ? !ok_lo_toc_insn(insn, r_type)
6407 : ((insn & ((0x3f << 26) | 0x1f << 16))
6408 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
6409 {
6410 ppc_object->set_no_toc_opt();
6411 gold_warning(_("%s: toc optimization is not supported "
6412 "for %#08x instruction"),
6413 ppc_object->name().c_str(), insn);
6414 }
6415 }
6416 }
6417
6418 switch (r_type)
6419 {
6420 default:
6421 break;
6422 case elfcpp::R_PPC64_TOC16:
6423 case elfcpp::R_PPC64_TOC16_LO:
6424 case elfcpp::R_PPC64_TOC16_HI:
6425 case elfcpp::R_PPC64_TOC16_HA:
6426 case elfcpp::R_PPC64_TOC16_DS:
6427 case elfcpp::R_PPC64_TOC16_LO_DS:
6428 unsigned int shndx = lsym.get_st_shndx();
6429 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6430 bool is_ordinary;
6431 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6432 if (is_ordinary && shndx == ppc_object->toc_shndx())
6433 {
6434 Address dst_off = lsym.get_st_value() + reloc.get_r_offset();
6435 if (dst_off < ppc_object->section_size(shndx))
6436 {
6437 bool ok = false;
6438 if (r_type == elfcpp::R_PPC64_TOC16_HA)
6439 ok = true;
6440 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
6441 {
6442 // Need to check that the insn is a ld
6443 if (!view)
6444 view = ppc_object->section_contents(data_shndx,
6445 &slen,
6446 false);
6447 section_size_type off =
6448 (convert_to_section_size_type(reloc.get_r_offset())
6449 + (big_endian ? -2 : 3));
6450 if (off < slen
6451 && (view[off] & (0x3f << 2)) == 58u << 2)
6452 ok = true;
6453 }
6454 if (!ok)
6455 ppc_object->set_no_toc_opt(dst_off);
6456 }
6457 }
6458 break;
6459 }
6460 }
6461
6462 if (size == 32)
6463 {
6464 switch (r_type)
6465 {
6466 case elfcpp::R_POWERPC_REL32:
6467 if (ppc_object->got2_shndx() != 0
6468 && parameters->options().output_is_position_independent())
6469 {
6470 unsigned int shndx = lsym.get_st_shndx();
6471 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6472 bool is_ordinary;
6473 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
6474 if (is_ordinary && shndx == ppc_object->got2_shndx()
6475 && (ppc_object->section_flags(data_shndx)
6476 & elfcpp::SHF_EXECINSTR) != 0)
6477 gold_error(_("%s: unsupported -mbss-plt code"),
6478 ppc_object->name().c_str());
6479 }
6480 break;
6481 default:
6482 break;
6483 }
6484 }
6485
6486 switch (r_type)
6487 {
6488 case elfcpp::R_POWERPC_GOT_TLSLD16:
6489 case elfcpp::R_POWERPC_GOT_TLSGD16:
6490 case elfcpp::R_POWERPC_GOT_TPREL16:
6491 case elfcpp::R_POWERPC_GOT_DTPREL16:
6492 case elfcpp::R_POWERPC_GOT16:
6493 case elfcpp::R_PPC64_GOT16_DS:
6494 case elfcpp::R_PPC64_TOC16:
6495 case elfcpp::R_PPC64_TOC16_DS:
6496 ppc_object->set_has_small_toc_reloc();
6497 default:
6498 break;
6499 }
6500 }
6501
6502 // Report an unsupported relocation against a global symbol.
6503
6504 template<int size, bool big_endian>
6505 void
6506 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6507 Sized_relobj_file<size, big_endian>* object,
6508 unsigned int r_type,
6509 Symbol* gsym)
6510 {
6511 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6512 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6513 }
6514
6515 // Scan a relocation for a global symbol.
6516
6517 template<int size, bool big_endian>
6518 inline void
6519 Target_powerpc<size, big_endian>::Scan::global(
6520 Symbol_table* symtab,
6521 Layout* layout,
6522 Target_powerpc<size, big_endian>* target,
6523 Sized_relobj_file<size, big_endian>* object,
6524 unsigned int data_shndx,
6525 Output_section* output_section,
6526 const elfcpp::Rela<size, big_endian>& reloc,
6527 unsigned int r_type,
6528 Symbol* gsym)
6529 {
6530 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6531 return;
6532
6533 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6534 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6535 {
6536 this->expect_tls_get_addr_call();
6537 const bool final = gsym->final_value_is_known();
6538 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6539 if (tls_type != tls::TLSOPT_NONE)
6540 this->skip_next_tls_get_addr_call();
6541 }
6542 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6543 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6544 {
6545 this->expect_tls_get_addr_call();
6546 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6547 if (tls_type != tls::TLSOPT_NONE)
6548 this->skip_next_tls_get_addr_call();
6549 }
6550
6551 Powerpc_relobj<size, big_endian>* ppc_object
6552 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6553
6554 // A STT_GNU_IFUNC symbol may require a PLT entry.
6555 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
6556 bool pushed_ifunc = false;
6557 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6558 {
6559 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6560 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6561 r_type, r_sym, reloc.get_r_addend());
6562 target->make_plt_entry(symtab, layout, gsym);
6563 pushed_ifunc = true;
6564 }
6565
6566 switch (r_type)
6567 {
6568 case elfcpp::R_POWERPC_NONE:
6569 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6570 case elfcpp::R_POWERPC_GNU_VTENTRY:
6571 case elfcpp::R_PPC_LOCAL24PC:
6572 case elfcpp::R_POWERPC_TLS:
6573 case elfcpp::R_PPC64_ENTRY:
6574 break;
6575
6576 case elfcpp::R_PPC64_TOC:
6577 {
6578 Output_data_got_powerpc<size, big_endian>* got
6579 = target->got_section(symtab, layout);
6580 if (parameters->options().output_is_position_independent())
6581 {
6582 Address off = reloc.get_r_offset();
6583 if (size == 64
6584 && data_shndx == ppc_object->opd_shndx()
6585 && ppc_object->get_opd_discard(off - 8))
6586 break;
6587
6588 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6589 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6590 if (data_shndx != ppc_object->opd_shndx())
6591 symobj = static_cast
6592 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6593 rela_dyn->add_output_section_relative(got->output_section(),
6594 elfcpp::R_POWERPC_RELATIVE,
6595 output_section,
6596 object, data_shndx, off,
6597 symobj->toc_base_offset());
6598 }
6599 }
6600 break;
6601
6602 case elfcpp::R_PPC64_ADDR64:
6603 if (size == 64
6604 && target->abiversion() < 2
6605 && data_shndx == ppc_object->opd_shndx()
6606 && (gsym->is_defined_in_discarded_section()
6607 || gsym->object() != object))
6608 {
6609 ppc_object->set_opd_discard(reloc.get_r_offset());
6610 break;
6611 }
6612 // Fall through.
6613 case elfcpp::R_PPC64_UADDR64:
6614 case elfcpp::R_POWERPC_ADDR32:
6615 case elfcpp::R_POWERPC_UADDR32:
6616 case elfcpp::R_POWERPC_ADDR24:
6617 case elfcpp::R_POWERPC_ADDR16:
6618 case elfcpp::R_POWERPC_ADDR16_LO:
6619 case elfcpp::R_POWERPC_ADDR16_HI:
6620 case elfcpp::R_POWERPC_ADDR16_HA:
6621 case elfcpp::R_POWERPC_UADDR16:
6622 case elfcpp::R_PPC64_ADDR16_HIGH:
6623 case elfcpp::R_PPC64_ADDR16_HIGHA:
6624 case elfcpp::R_PPC64_ADDR16_HIGHER:
6625 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6626 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6627 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6628 case elfcpp::R_PPC64_ADDR16_DS:
6629 case elfcpp::R_PPC64_ADDR16_LO_DS:
6630 case elfcpp::R_POWERPC_ADDR14:
6631 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6632 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6633 {
6634 // Make a PLT entry if necessary.
6635 if (gsym->needs_plt_entry())
6636 {
6637 // Since this is not a PC-relative relocation, we may be
6638 // taking the address of a function. In that case we need to
6639 // set the entry in the dynamic symbol table to the address of
6640 // the PLT call stub.
6641 bool need_ifunc_plt = false;
6642 if ((size == 32 || target->abiversion() >= 2)
6643 && gsym->is_from_dynobj()
6644 && !parameters->options().output_is_position_independent())
6645 {
6646 gsym->set_needs_dynsym_value();
6647 need_ifunc_plt = true;
6648 }
6649 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
6650 {
6651 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6652 target->push_branch(ppc_object, data_shndx,
6653 reloc.get_r_offset(), r_type, r_sym,
6654 reloc.get_r_addend());
6655 target->make_plt_entry(symtab, layout, gsym);
6656 }
6657 }
6658 // Make a dynamic relocation if necessary.
6659 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
6660 || (size == 64 && is_ifunc && target->abiversion() < 2))
6661 {
6662 if (!parameters->options().output_is_position_independent()
6663 && gsym->may_need_copy_reloc())
6664 {
6665 target->copy_reloc(symtab, layout, object,
6666 data_shndx, output_section, gsym, reloc);
6667 }
6668 else if ((((size == 32
6669 && r_type == elfcpp::R_POWERPC_ADDR32)
6670 || (size == 64
6671 && r_type == elfcpp::R_PPC64_ADDR64
6672 && target->abiversion() >= 2))
6673 && gsym->can_use_relative_reloc(false)
6674 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6675 && parameters->options().shared()))
6676 || (size == 64
6677 && r_type == elfcpp::R_PPC64_ADDR64
6678 && target->abiversion() < 2
6679 && (gsym->can_use_relative_reloc(false)
6680 || data_shndx == ppc_object->opd_shndx())))
6681 {
6682 Reloc_section* rela_dyn
6683 = target->rela_dyn_section(symtab, layout, is_ifunc);
6684 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6685 : elfcpp::R_POWERPC_RELATIVE);
6686 rela_dyn->add_symbolless_global_addend(
6687 gsym, dynrel, output_section, object, data_shndx,
6688 reloc.get_r_offset(), reloc.get_r_addend());
6689 }
6690 else
6691 {
6692 Reloc_section* rela_dyn
6693 = target->rela_dyn_section(symtab, layout, is_ifunc);
6694 check_non_pic(object, r_type);
6695 rela_dyn->add_global(gsym, r_type, output_section,
6696 object, data_shndx,
6697 reloc.get_r_offset(),
6698 reloc.get_r_addend());
6699
6700 if (size == 64
6701 && parameters->options().toc_optimize()
6702 && data_shndx == ppc_object->toc_shndx())
6703 ppc_object->set_no_toc_opt(reloc.get_r_offset());
6704 }
6705 }
6706 }
6707 break;
6708
6709 case elfcpp::R_PPC_PLTREL24:
6710 case elfcpp::R_POWERPC_REL24:
6711 if (!is_ifunc)
6712 {
6713 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6714 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6715 r_type, r_sym, reloc.get_r_addend());
6716 if (gsym->needs_plt_entry()
6717 || (!gsym->final_value_is_known()
6718 && (gsym->is_undefined()
6719 || gsym->is_from_dynobj()
6720 || gsym->is_preemptible())))
6721 target->make_plt_entry(symtab, layout, gsym);
6722 }
6723 // Fall through.
6724
6725 case elfcpp::R_PPC64_REL64:
6726 case elfcpp::R_POWERPC_REL32:
6727 // Make a dynamic relocation if necessary.
6728 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
6729 {
6730 if (!parameters->options().output_is_position_independent()
6731 && gsym->may_need_copy_reloc())
6732 {
6733 target->copy_reloc(symtab, layout, object,
6734 data_shndx, output_section, gsym,
6735 reloc);
6736 }
6737 else
6738 {
6739 Reloc_section* rela_dyn
6740 = target->rela_dyn_section(symtab, layout, is_ifunc);
6741 check_non_pic(object, r_type);
6742 rela_dyn->add_global(gsym, r_type, output_section, object,
6743 data_shndx, reloc.get_r_offset(),
6744 reloc.get_r_addend());
6745 }
6746 }
6747 break;
6748
6749 case elfcpp::R_POWERPC_REL14:
6750 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6751 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6752 if (!is_ifunc)
6753 {
6754 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
6755 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6756 r_type, r_sym, reloc.get_r_addend());
6757 }
6758 break;
6759
6760 case elfcpp::R_POWERPC_REL16:
6761 case elfcpp::R_POWERPC_REL16_LO:
6762 case elfcpp::R_POWERPC_REL16_HI:
6763 case elfcpp::R_POWERPC_REL16_HA:
6764 case elfcpp::R_POWERPC_REL16DX_HA:
6765 case elfcpp::R_POWERPC_SECTOFF:
6766 case elfcpp::R_POWERPC_SECTOFF_LO:
6767 case elfcpp::R_POWERPC_SECTOFF_HI:
6768 case elfcpp::R_POWERPC_SECTOFF_HA:
6769 case elfcpp::R_PPC64_SECTOFF_DS:
6770 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6771 case elfcpp::R_POWERPC_TPREL16:
6772 case elfcpp::R_POWERPC_TPREL16_LO:
6773 case elfcpp::R_POWERPC_TPREL16_HI:
6774 case elfcpp::R_POWERPC_TPREL16_HA:
6775 case elfcpp::R_PPC64_TPREL16_DS:
6776 case elfcpp::R_PPC64_TPREL16_LO_DS:
6777 case elfcpp::R_PPC64_TPREL16_HIGH:
6778 case elfcpp::R_PPC64_TPREL16_HIGHA:
6779 case elfcpp::R_PPC64_TPREL16_HIGHER:
6780 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6781 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6782 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6783 case elfcpp::R_POWERPC_DTPREL16:
6784 case elfcpp::R_POWERPC_DTPREL16_LO:
6785 case elfcpp::R_POWERPC_DTPREL16_HI:
6786 case elfcpp::R_POWERPC_DTPREL16_HA:
6787 case elfcpp::R_PPC64_DTPREL16_DS:
6788 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6789 case elfcpp::R_PPC64_DTPREL16_HIGH:
6790 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6791 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6792 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6793 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6794 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6795 case elfcpp::R_PPC64_TLSGD:
6796 case elfcpp::R_PPC64_TLSLD:
6797 case elfcpp::R_PPC64_ADDR64_LOCAL:
6798 break;
6799
6800 case elfcpp::R_POWERPC_GOT16:
6801 case elfcpp::R_POWERPC_GOT16_LO:
6802 case elfcpp::R_POWERPC_GOT16_HI:
6803 case elfcpp::R_POWERPC_GOT16_HA:
6804 case elfcpp::R_PPC64_GOT16_DS:
6805 case elfcpp::R_PPC64_GOT16_LO_DS:
6806 {
6807 // The symbol requires a GOT entry.
6808 Output_data_got_powerpc<size, big_endian>* got;
6809
6810 got = target->got_section(symtab, layout);
6811 if (gsym->final_value_is_known())
6812 {
6813 if (is_ifunc
6814 && (size == 32 || target->abiversion() >= 2))
6815 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6816 else
6817 got->add_global(gsym, GOT_TYPE_STANDARD);
6818 }
6819 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6820 {
6821 // If we are generating a shared object or a pie, this
6822 // symbol's GOT entry will be set by a dynamic relocation.
6823 unsigned int off = got->add_constant(0);
6824 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6825
6826 Reloc_section* rela_dyn
6827 = target->rela_dyn_section(symtab, layout, is_ifunc);
6828
6829 if (gsym->can_use_relative_reloc(false)
6830 && !((size == 32
6831 || target->abiversion() >= 2)
6832 && gsym->visibility() == elfcpp::STV_PROTECTED
6833 && parameters->options().shared()))
6834 {
6835 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6836 : elfcpp::R_POWERPC_RELATIVE);
6837 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6838 }
6839 else
6840 {
6841 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6842 rela_dyn->add_global(gsym, dynrel, got, off, 0);
6843 }
6844 }
6845 }
6846 break;
6847
6848 case elfcpp::R_PPC64_TOC16:
6849 case elfcpp::R_PPC64_TOC16_LO:
6850 case elfcpp::R_PPC64_TOC16_HI:
6851 case elfcpp::R_PPC64_TOC16_HA:
6852 case elfcpp::R_PPC64_TOC16_DS:
6853 case elfcpp::R_PPC64_TOC16_LO_DS:
6854 // We need a GOT section.
6855 target->got_section(symtab, layout);
6856 break;
6857
6858 case elfcpp::R_POWERPC_GOT_TLSGD16:
6859 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6860 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6861 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6862 {
6863 const bool final = gsym->final_value_is_known();
6864 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6865 if (tls_type == tls::TLSOPT_NONE)
6866 {
6867 Output_data_got_powerpc<size, big_endian>* got
6868 = target->got_section(symtab, layout);
6869 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6870 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
6871 elfcpp::R_POWERPC_DTPMOD,
6872 elfcpp::R_POWERPC_DTPREL);
6873 }
6874 else if (tls_type == tls::TLSOPT_TO_IE)
6875 {
6876 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6877 {
6878 Output_data_got_powerpc<size, big_endian>* got
6879 = target->got_section(symtab, layout);
6880 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6881 if (gsym->is_undefined()
6882 || gsym->is_from_dynobj())
6883 {
6884 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6885 elfcpp::R_POWERPC_TPREL);
6886 }
6887 else
6888 {
6889 unsigned int off = got->add_constant(0);
6890 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6891 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6892 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6893 got, off, 0);
6894 }
6895 }
6896 }
6897 else if (tls_type == tls::TLSOPT_TO_LE)
6898 {
6899 // no GOT relocs needed for Local Exec.
6900 }
6901 else
6902 gold_unreachable();
6903 }
6904 break;
6905
6906 case elfcpp::R_POWERPC_GOT_TLSLD16:
6907 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6908 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6909 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6910 {
6911 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6912 if (tls_type == tls::TLSOPT_NONE)
6913 target->tlsld_got_offset(symtab, layout, object);
6914 else if (tls_type == tls::TLSOPT_TO_LE)
6915 {
6916 // no GOT relocs needed for Local Exec.
6917 if (parameters->options().emit_relocs())
6918 {
6919 Output_section* os = layout->tls_segment()->first_section();
6920 gold_assert(os != NULL);
6921 os->set_needs_symtab_index();
6922 }
6923 }
6924 else
6925 gold_unreachable();
6926 }
6927 break;
6928
6929 case elfcpp::R_POWERPC_GOT_DTPREL16:
6930 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6931 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6932 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6933 {
6934 Output_data_got_powerpc<size, big_endian>* got
6935 = target->got_section(symtab, layout);
6936 if (!gsym->final_value_is_known()
6937 && (gsym->is_from_dynobj()
6938 || gsym->is_undefined()
6939 || gsym->is_preemptible()))
6940 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6941 target->rela_dyn_section(layout),
6942 elfcpp::R_POWERPC_DTPREL);
6943 else
6944 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6945 }
6946 break;
6947
6948 case elfcpp::R_POWERPC_GOT_TPREL16:
6949 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6950 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6951 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6952 {
6953 const bool final = gsym->final_value_is_known();
6954 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6955 if (tls_type == tls::TLSOPT_NONE)
6956 {
6957 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6958 {
6959 Output_data_got_powerpc<size, big_endian>* got
6960 = target->got_section(symtab, layout);
6961 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6962 if (gsym->is_undefined()
6963 || gsym->is_from_dynobj())
6964 {
6965 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6966 elfcpp::R_POWERPC_TPREL);
6967 }
6968 else
6969 {
6970 unsigned int off = got->add_constant(0);
6971 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6972 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6973 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6974 got, off, 0);
6975 }
6976 }
6977 }
6978 else if (tls_type == tls::TLSOPT_TO_LE)
6979 {
6980 // no GOT relocs needed for Local Exec.
6981 }
6982 else
6983 gold_unreachable();
6984 }
6985 break;
6986
6987 default:
6988 unsupported_reloc_global(object, r_type, gsym);
6989 break;
6990 }
6991
6992 if (size == 64
6993 && parameters->options().toc_optimize())
6994 {
6995 if (data_shndx == ppc_object->toc_shndx())
6996 {
6997 bool ok = true;
6998 if (r_type != elfcpp::R_PPC64_ADDR64
6999 || (is_ifunc && target->abiversion() < 2))
7000 ok = false;
7001 else if (parameters->options().output_is_position_independent()
7002 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
7003 ok = false;
7004 if (!ok)
7005 ppc_object->set_no_toc_opt(reloc.get_r_offset());
7006 }
7007
7008 enum {no_check, check_lo, check_ha} insn_check;
7009 switch (r_type)
7010 {
7011 default:
7012 insn_check = no_check;
7013 break;
7014
7015 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7016 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7017 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7018 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7019 case elfcpp::R_POWERPC_GOT16_HA:
7020 case elfcpp::R_PPC64_TOC16_HA:
7021 insn_check = check_ha;
7022 break;
7023
7024 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7025 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7026 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7027 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7028 case elfcpp::R_POWERPC_GOT16_LO:
7029 case elfcpp::R_PPC64_GOT16_LO_DS:
7030 case elfcpp::R_PPC64_TOC16_LO:
7031 case elfcpp::R_PPC64_TOC16_LO_DS:
7032 insn_check = check_lo;
7033 break;
7034 }
7035
7036 section_size_type slen;
7037 const unsigned char* view = NULL;
7038 if (insn_check != no_check)
7039 {
7040 view = ppc_object->section_contents(data_shndx, &slen, false);
7041 section_size_type off =
7042 convert_to_section_size_type(reloc.get_r_offset()) & -4;
7043 if (off < slen)
7044 {
7045 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
7046 if (insn_check == check_lo
7047 ? !ok_lo_toc_insn(insn, r_type)
7048 : ((insn & ((0x3f << 26) | 0x1f << 16))
7049 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
7050 {
7051 ppc_object->set_no_toc_opt();
7052 gold_warning(_("%s: toc optimization is not supported "
7053 "for %#08x instruction"),
7054 ppc_object->name().c_str(), insn);
7055 }
7056 }
7057 }
7058
7059 switch (r_type)
7060 {
7061 default:
7062 break;
7063 case elfcpp::R_PPC64_TOC16:
7064 case elfcpp::R_PPC64_TOC16_LO:
7065 case elfcpp::R_PPC64_TOC16_HI:
7066 case elfcpp::R_PPC64_TOC16_HA:
7067 case elfcpp::R_PPC64_TOC16_DS:
7068 case elfcpp::R_PPC64_TOC16_LO_DS:
7069 if (gsym->source() == Symbol::FROM_OBJECT
7070 && !gsym->object()->is_dynamic())
7071 {
7072 Powerpc_relobj<size, big_endian>* sym_object
7073 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7074 bool is_ordinary;
7075 unsigned int shndx = gsym->shndx(&is_ordinary);
7076 if (shndx == sym_object->toc_shndx())
7077 {
7078 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
7079 Address dst_off = sym->value() + reloc.get_r_offset();
7080 if (dst_off < sym_object->section_size(shndx))
7081 {
7082 bool ok = false;
7083 if (r_type == elfcpp::R_PPC64_TOC16_HA)
7084 ok = true;
7085 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
7086 {
7087 // Need to check that the insn is a ld
7088 if (!view)
7089 view = ppc_object->section_contents(data_shndx,
7090 &slen,
7091 false);
7092 section_size_type off =
7093 (convert_to_section_size_type(reloc.get_r_offset())
7094 + (big_endian ? -2 : 3));
7095 if (off < slen
7096 && (view[off] & (0x3f << 2)) == (58u << 2))
7097 ok = true;
7098 }
7099 if (!ok)
7100 sym_object->set_no_toc_opt(dst_off);
7101 }
7102 }
7103 }
7104 break;
7105 }
7106 }
7107
7108 if (size == 32)
7109 {
7110 switch (r_type)
7111 {
7112 case elfcpp::R_PPC_LOCAL24PC:
7113 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
7114 gold_error(_("%s: unsupported -mbss-plt code"),
7115 ppc_object->name().c_str());
7116 break;
7117 default:
7118 break;
7119 }
7120 }
7121
7122 switch (r_type)
7123 {
7124 case elfcpp::R_POWERPC_GOT_TLSLD16:
7125 case elfcpp::R_POWERPC_GOT_TLSGD16:
7126 case elfcpp::R_POWERPC_GOT_TPREL16:
7127 case elfcpp::R_POWERPC_GOT_DTPREL16:
7128 case elfcpp::R_POWERPC_GOT16:
7129 case elfcpp::R_PPC64_GOT16_DS:
7130 case elfcpp::R_PPC64_TOC16:
7131 case elfcpp::R_PPC64_TOC16_DS:
7132 ppc_object->set_has_small_toc_reloc();
7133 default:
7134 break;
7135 }
7136 }
7137
7138 // Process relocations for gc.
7139
7140 template<int size, bool big_endian>
7141 void
7142 Target_powerpc<size, big_endian>::gc_process_relocs(
7143 Symbol_table* symtab,
7144 Layout* layout,
7145 Sized_relobj_file<size, big_endian>* object,
7146 unsigned int data_shndx,
7147 unsigned int,
7148 const unsigned char* prelocs,
7149 size_t reloc_count,
7150 Output_section* output_section,
7151 bool needs_special_offset_handling,
7152 size_t local_symbol_count,
7153 const unsigned char* plocal_symbols)
7154 {
7155 typedef Target_powerpc<size, big_endian> Powerpc;
7156 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7157 Classify_reloc;
7158
7159 Powerpc_relobj<size, big_endian>* ppc_object
7160 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7161 if (size == 64)
7162 ppc_object->set_opd_valid();
7163 if (size == 64 && data_shndx == ppc_object->opd_shndx())
7164 {
7165 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
7166 for (p = ppc_object->access_from_map()->begin();
7167 p != ppc_object->access_from_map()->end();
7168 ++p)
7169 {
7170 Address dst_off = p->first;
7171 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7172 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
7173 for (s = p->second.begin(); s != p->second.end(); ++s)
7174 {
7175 Relobj* src_obj = s->first;
7176 unsigned int src_indx = s->second;
7177 symtab->gc()->add_reference(src_obj, src_indx,
7178 ppc_object, dst_indx);
7179 }
7180 p->second.clear();
7181 }
7182 ppc_object->access_from_map()->clear();
7183 ppc_object->process_gc_mark(symtab);
7184 // Don't look at .opd relocs as .opd will reference everything.
7185 return;
7186 }
7187
7188 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7189 symtab,
7190 layout,
7191 this,
7192 object,
7193 data_shndx,
7194 prelocs,
7195 reloc_count,
7196 output_section,
7197 needs_special_offset_handling,
7198 local_symbol_count,
7199 plocal_symbols);
7200 }
7201
7202 // Handle target specific gc actions when adding a gc reference from
7203 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
7204 // and DST_OFF. For powerpc64, this adds a referenc to the code
7205 // section of a function descriptor.
7206
7207 template<int size, bool big_endian>
7208 void
7209 Target_powerpc<size, big_endian>::do_gc_add_reference(
7210 Symbol_table* symtab,
7211 Relobj* src_obj,
7212 unsigned int src_shndx,
7213 Relobj* dst_obj,
7214 unsigned int dst_shndx,
7215 Address dst_off) const
7216 {
7217 if (size != 64 || dst_obj->is_dynamic())
7218 return;
7219
7220 Powerpc_relobj<size, big_endian>* ppc_object
7221 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
7222 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
7223 {
7224 if (ppc_object->opd_valid())
7225 {
7226 dst_shndx = ppc_object->get_opd_ent(dst_off);
7227 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
7228 }
7229 else
7230 {
7231 // If we haven't run scan_opd_relocs, we must delay
7232 // processing this function descriptor reference.
7233 ppc_object->add_reference(src_obj, src_shndx, dst_off);
7234 }
7235 }
7236 }
7237
7238 // Add any special sections for this symbol to the gc work list.
7239 // For powerpc64, this adds the code section of a function
7240 // descriptor.
7241
7242 template<int size, bool big_endian>
7243 void
7244 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
7245 Symbol_table* symtab,
7246 Symbol* sym) const
7247 {
7248 if (size == 64)
7249 {
7250 Powerpc_relobj<size, big_endian>* ppc_object
7251 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
7252 bool is_ordinary;
7253 unsigned int shndx = sym->shndx(&is_ordinary);
7254 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
7255 {
7256 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
7257 Address dst_off = gsym->value();
7258 if (ppc_object->opd_valid())
7259 {
7260 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
7261 symtab->gc()->worklist().push_back(Section_id(ppc_object,
7262 dst_indx));
7263 }
7264 else
7265 ppc_object->add_gc_mark(dst_off);
7266 }
7267 }
7268 }
7269
7270 // For a symbol location in .opd, set LOC to the location of the
7271 // function entry.
7272
7273 template<int size, bool big_endian>
7274 void
7275 Target_powerpc<size, big_endian>::do_function_location(
7276 Symbol_location* loc) const
7277 {
7278 if (size == 64 && loc->shndx != 0)
7279 {
7280 if (loc->object->is_dynamic())
7281 {
7282 Powerpc_dynobj<size, big_endian>* ppc_object
7283 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
7284 if (loc->shndx == ppc_object->opd_shndx())
7285 {
7286 Address dest_off;
7287 Address off = loc->offset - ppc_object->opd_address();
7288 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
7289 loc->offset = dest_off;
7290 }
7291 }
7292 else
7293 {
7294 const Powerpc_relobj<size, big_endian>* ppc_object
7295 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
7296 if (loc->shndx == ppc_object->opd_shndx())
7297 {
7298 Address dest_off;
7299 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
7300 loc->offset = dest_off;
7301 }
7302 }
7303 }
7304 }
7305
7306 // FNOFFSET in section SHNDX in OBJECT is the start of a function
7307 // compiled with -fsplit-stack. The function calls non-split-stack
7308 // code. Change the function to ensure it has enough stack space to
7309 // call some random function.
7310
7311 template<int size, bool big_endian>
7312 void
7313 Target_powerpc<size, big_endian>::do_calls_non_split(
7314 Relobj* object,
7315 unsigned int shndx,
7316 section_offset_type fnoffset,
7317 section_size_type fnsize,
7318 const unsigned char* prelocs,
7319 size_t reloc_count,
7320 unsigned char* view,
7321 section_size_type view_size,
7322 std::string* from,
7323 std::string* to) const
7324 {
7325 // 32-bit not supported.
7326 if (size == 32)
7327 {
7328 // warn
7329 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
7330 prelocs, reloc_count, view, view_size,
7331 from, to);
7332 return;
7333 }
7334
7335 // The function always starts with
7336 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
7337 // addis %r12,%r1,-allocate@ha
7338 // addi %r12,%r12,-allocate@l
7339 // cmpld %r12,%r0
7340 // but note that the addis or addi may be replaced with a nop
7341
7342 unsigned char *entry = view + fnoffset;
7343 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
7344
7345 if ((insn & 0xffff0000) == addis_2_12)
7346 {
7347 /* Skip ELFv2 global entry code. */
7348 entry += 8;
7349 insn = elfcpp::Swap<32, big_endian>::readval(entry);
7350 }
7351
7352 unsigned char *pinsn = entry;
7353 bool ok = false;
7354 const uint32_t ld_private_ss = 0xe80d8fc0;
7355 if (insn == ld_private_ss)
7356 {
7357 int32_t allocate = 0;
7358 while (1)
7359 {
7360 pinsn += 4;
7361 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
7362 if ((insn & 0xffff0000) == addis_12_1)
7363 allocate += (insn & 0xffff) << 16;
7364 else if ((insn & 0xffff0000) == addi_12_1
7365 || (insn & 0xffff0000) == addi_12_12)
7366 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
7367 else if (insn != nop)
7368 break;
7369 }
7370 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
7371 {
7372 int extra = parameters->options().split_stack_adjust_size();
7373 allocate -= extra;
7374 if (allocate >= 0 || extra < 0)
7375 {
7376 object->error(_("split-stack stack size overflow at "
7377 "section %u offset %0zx"),
7378 shndx, static_cast<size_t>(fnoffset));
7379 return;
7380 }
7381 pinsn = entry + 4;
7382 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
7383 if (insn != addis_12_1)
7384 {
7385 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7386 pinsn += 4;
7387 insn = addi_12_12 | (allocate & 0xffff);
7388 if (insn != addi_12_12)
7389 {
7390 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7391 pinsn += 4;
7392 }
7393 }
7394 else
7395 {
7396 insn = addi_12_1 | (allocate & 0xffff);
7397 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
7398 pinsn += 4;
7399 }
7400 if (pinsn != entry + 12)
7401 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
7402
7403 ok = true;
7404 }
7405 }
7406
7407 if (!ok)
7408 {
7409 if (!object->has_no_split_stack())
7410 object->error(_("failed to match split-stack sequence at "
7411 "section %u offset %0zx"),
7412 shndx, static_cast<size_t>(fnoffset));
7413 }
7414 }
7415
7416 // Scan relocations for a section.
7417
7418 template<int size, bool big_endian>
7419 void
7420 Target_powerpc<size, big_endian>::scan_relocs(
7421 Symbol_table* symtab,
7422 Layout* layout,
7423 Sized_relobj_file<size, big_endian>* object,
7424 unsigned int data_shndx,
7425 unsigned int sh_type,
7426 const unsigned char* prelocs,
7427 size_t reloc_count,
7428 Output_section* output_section,
7429 bool needs_special_offset_handling,
7430 size_t local_symbol_count,
7431 const unsigned char* plocal_symbols)
7432 {
7433 typedef Target_powerpc<size, big_endian> Powerpc;
7434 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
7435 Classify_reloc;
7436
7437 if (sh_type == elfcpp::SHT_REL)
7438 {
7439 gold_error(_("%s: unsupported REL reloc section"),
7440 object->name().c_str());
7441 return;
7442 }
7443
7444 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
7445 symtab,
7446 layout,
7447 this,
7448 object,
7449 data_shndx,
7450 prelocs,
7451 reloc_count,
7452 output_section,
7453 needs_special_offset_handling,
7454 local_symbol_count,
7455 plocal_symbols);
7456 }
7457
7458 // Functor class for processing the global symbol table.
7459 // Removes symbols defined on discarded opd entries.
7460
7461 template<bool big_endian>
7462 class Global_symbol_visitor_opd
7463 {
7464 public:
7465 Global_symbol_visitor_opd()
7466 { }
7467
7468 void
7469 operator()(Sized_symbol<64>* sym)
7470 {
7471 if (sym->has_symtab_index()
7472 || sym->source() != Symbol::FROM_OBJECT
7473 || !sym->in_real_elf())
7474 return;
7475
7476 if (sym->object()->is_dynamic())
7477 return;
7478
7479 Powerpc_relobj<64, big_endian>* symobj
7480 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
7481 if (symobj->opd_shndx() == 0)
7482 return;
7483
7484 bool is_ordinary;
7485 unsigned int shndx = sym->shndx(&is_ordinary);
7486 if (shndx == symobj->opd_shndx()
7487 && symobj->get_opd_discard(sym->value()))
7488 {
7489 sym->set_undefined();
7490 sym->set_visibility(elfcpp::STV_DEFAULT);
7491 sym->set_is_defined_in_discarded_section();
7492 sym->set_symtab_index(-1U);
7493 }
7494 }
7495 };
7496
7497 template<int size, bool big_endian>
7498 void
7499 Target_powerpc<size, big_endian>::define_save_restore_funcs(
7500 Layout* layout,
7501 Symbol_table* symtab)
7502 {
7503 if (size == 64)
7504 {
7505 Output_data_save_res<size, big_endian>* savres
7506 = new Output_data_save_res<size, big_endian>(symtab);
7507 this->savres_section_ = savres;
7508 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7509 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7510 savres, ORDER_TEXT, false);
7511 }
7512 }
7513
7514 // Sort linker created .got section first (for the header), then input
7515 // sections belonging to files using small model code.
7516
7517 template<bool big_endian>
7518 class Sort_toc_sections
7519 {
7520 public:
7521 bool
7522 operator()(const Output_section::Input_section& is1,
7523 const Output_section::Input_section& is2) const
7524 {
7525 if (!is1.is_input_section() && is2.is_input_section())
7526 return true;
7527 bool small1
7528 = (is1.is_input_section()
7529 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
7530 ->has_small_toc_reloc()));
7531 bool small2
7532 = (is2.is_input_section()
7533 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
7534 ->has_small_toc_reloc()));
7535 return small1 && !small2;
7536 }
7537 };
7538
7539 // Finalize the sections.
7540
7541 template<int size, bool big_endian>
7542 void
7543 Target_powerpc<size, big_endian>::do_finalize_sections(
7544 Layout* layout,
7545 const Input_objects*,
7546 Symbol_table* symtab)
7547 {
7548 if (parameters->doing_static_link())
7549 {
7550 // At least some versions of glibc elf-init.o have a strong
7551 // reference to __rela_iplt marker syms. A weak ref would be
7552 // better..
7553 if (this->iplt_ != NULL)
7554 {
7555 Reloc_section* rel = this->iplt_->rel_plt();
7556 symtab->define_in_output_data("__rela_iplt_start", NULL,
7557 Symbol_table::PREDEFINED, rel, 0, 0,
7558 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7559 elfcpp::STV_HIDDEN, 0, false, true);
7560 symtab->define_in_output_data("__rela_iplt_end", NULL,
7561 Symbol_table::PREDEFINED, rel, 0, 0,
7562 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7563 elfcpp::STV_HIDDEN, 0, true, true);
7564 }
7565 else
7566 {
7567 symtab->define_as_constant("__rela_iplt_start", NULL,
7568 Symbol_table::PREDEFINED, 0, 0,
7569 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7570 elfcpp::STV_HIDDEN, 0, true, false);
7571 symtab->define_as_constant("__rela_iplt_end", NULL,
7572 Symbol_table::PREDEFINED, 0, 0,
7573 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
7574 elfcpp::STV_HIDDEN, 0, true, false);
7575 }
7576 }
7577
7578 if (size == 64)
7579 {
7580 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
7581 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
7582
7583 if (!parameters->options().relocatable())
7584 {
7585 this->define_save_restore_funcs(layout, symtab);
7586
7587 // Annoyingly, we need to make these sections now whether or
7588 // not we need them. If we delay until do_relax then we
7589 // need to mess with the relaxation machinery checkpointing.
7590 this->got_section(symtab, layout);
7591 this->make_brlt_section(layout);
7592
7593 if (parameters->options().toc_sort())
7594 {
7595 Output_section* os = this->got_->output_section();
7596 if (os != NULL && os->input_sections().size() > 1)
7597 std::stable_sort(os->input_sections().begin(),
7598 os->input_sections().end(),
7599 Sort_toc_sections<big_endian>());
7600 }
7601 }
7602 }
7603
7604 // Fill in some more dynamic tags.
7605 Output_data_dynamic* odyn = layout->dynamic_data();
7606 if (odyn != NULL)
7607 {
7608 const Reloc_section* rel_plt = (this->plt_ == NULL
7609 ? NULL
7610 : this->plt_->rel_plt());
7611 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
7612 this->rela_dyn_, true, size == 32);
7613
7614 if (size == 32)
7615 {
7616 if (this->got_ != NULL)
7617 {
7618 this->got_->finalize_data_size();
7619 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
7620 this->got_, this->got_->g_o_t());
7621 }
7622 }
7623 else
7624 {
7625 if (this->glink_ != NULL)
7626 {
7627 this->glink_->finalize_data_size();
7628 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
7629 this->glink_,
7630 (this->glink_->pltresolve_size
7631 - 32));
7632 }
7633 }
7634 }
7635
7636 // Emit any relocs we saved in an attempt to avoid generating COPY
7637 // relocs.
7638 if (this->copy_relocs_.any_saved_relocs())
7639 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7640 }
7641
7642 // Emit any saved relocs, and mark toc entries using any of these
7643 // relocs as not optimizable.
7644
7645 template<int sh_type, int size, bool big_endian>
7646 void
7647 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
7648 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
7649 {
7650 if (size == 64
7651 && parameters->options().toc_optimize())
7652 {
7653 for (typename Copy_relocs<sh_type, size, big_endian>::
7654 Copy_reloc_entries::iterator p = this->entries_.begin();
7655 p != this->entries_.end();
7656 ++p)
7657 {
7658 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
7659 entry = *p;
7660
7661 // If the symbol is no longer defined in a dynamic object,
7662 // then we emitted a COPY relocation. If it is still
7663 // dynamic then we'll need dynamic relocations and thus
7664 // can't optimize toc entries.
7665 if (entry.sym_->is_from_dynobj())
7666 {
7667 Powerpc_relobj<size, big_endian>* ppc_object
7668 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
7669 if (entry.shndx_ == ppc_object->toc_shndx())
7670 ppc_object->set_no_toc_opt(entry.address_);
7671 }
7672 }
7673 }
7674
7675 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
7676 }
7677
7678 // Return the value to use for a branch relocation.
7679
7680 template<int size, bool big_endian>
7681 bool
7682 Target_powerpc<size, big_endian>::symval_for_branch(
7683 const Symbol_table* symtab,
7684 const Sized_symbol<size>* gsym,
7685 Powerpc_relobj<size, big_endian>* object,
7686 Address *value,
7687 unsigned int *dest_shndx)
7688 {
7689 if (size == 32 || this->abiversion() >= 2)
7690 gold_unreachable();
7691 *dest_shndx = 0;
7692
7693 // If the symbol is defined in an opd section, ie. is a function
7694 // descriptor, use the function descriptor code entry address
7695 Powerpc_relobj<size, big_endian>* symobj = object;
7696 if (gsym != NULL
7697 && (gsym->source() != Symbol::FROM_OBJECT
7698 || gsym->object()->is_dynamic()))
7699 return true;
7700 if (gsym != NULL)
7701 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7702 unsigned int shndx = symobj->opd_shndx();
7703 if (shndx == 0)
7704 return true;
7705 Address opd_addr = symobj->get_output_section_offset(shndx);
7706 if (opd_addr == invalid_address)
7707 return true;
7708 opd_addr += symobj->output_section_address(shndx);
7709 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
7710 {
7711 Address sec_off;
7712 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
7713 if (symtab->is_section_folded(symobj, *dest_shndx))
7714 {
7715 Section_id folded
7716 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7717 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7718 *dest_shndx = folded.second;
7719 }
7720 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
7721 if (sec_addr == invalid_address)
7722 return false;
7723
7724 sec_addr += symobj->output_section(*dest_shndx)->address();
7725 *value = sec_addr + sec_off;
7726 }
7727 return true;
7728 }
7729
7730 // Perform a relocation.
7731
7732 template<int size, bool big_endian>
7733 inline bool
7734 Target_powerpc<size, big_endian>::Relocate::relocate(
7735 const Relocate_info<size, big_endian>* relinfo,
7736 unsigned int,
7737 Target_powerpc* target,
7738 Output_section* os,
7739 size_t relnum,
7740 const unsigned char* preloc,
7741 const Sized_symbol<size>* gsym,
7742 const Symbol_value<size>* psymval,
7743 unsigned char* view,
7744 Address address,
7745 section_size_type view_size)
7746 {
7747 if (view == NULL)
7748 return true;
7749
7750 const elfcpp::Rela<size, big_endian> rela(preloc);
7751 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
7752 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
7753 {
7754 case Track_tls::NOT_EXPECTED:
7755 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7756 _("__tls_get_addr call lacks marker reloc"));
7757 break;
7758 case Track_tls::EXPECTED:
7759 // We have already complained.
7760 break;
7761 case Track_tls::SKIP:
7762 return true;
7763 case Track_tls::NORMAL:
7764 break;
7765 }
7766
7767 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
7768 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
7769 typedef typename elfcpp::Rela<size, big_endian> Reltype;
7770 // Offset from start of insn to d-field reloc.
7771 const int d_offset = big_endian ? 2 : 0;
7772
7773 Powerpc_relobj<size, big_endian>* const object
7774 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7775 Address value = 0;
7776 bool has_stub_value = false;
7777 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7778 if ((gsym != NULL
7779 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
7780 : object->local_has_plt_offset(r_sym))
7781 && (!psymval->is_ifunc_symbol()
7782 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
7783 {
7784 if (size == 64
7785 && gsym != NULL
7786 && target->abiversion() >= 2
7787 && !parameters->options().output_is_position_independent()
7788 && !is_branch_reloc(r_type))
7789 {
7790 Address off = target->glink_section()->find_global_entry(gsym);
7791 if (off != invalid_address)
7792 {
7793 value = target->glink_section()->global_entry_address() + off;
7794 has_stub_value = true;
7795 }
7796 }
7797 else
7798 {
7799 Stub_table<size, big_endian>* stub_table
7800 = object->stub_table(relinfo->data_shndx);
7801 if (stub_table == NULL)
7802 {
7803 // This is a ref from a data section to an ifunc symbol.
7804 if (target->stub_tables().size() != 0)
7805 stub_table = target->stub_tables()[0];
7806 }
7807 if (stub_table != NULL)
7808 {
7809 Address off;
7810 if (gsym != NULL)
7811 off = stub_table->find_plt_call_entry(object, gsym, r_type,
7812 rela.get_r_addend());
7813 else
7814 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
7815 rela.get_r_addend());
7816 if (off != invalid_address)
7817 {
7818 value = stub_table->stub_address() + off;
7819 has_stub_value = true;
7820 }
7821 }
7822 }
7823 // We don't care too much about bogus debug references to
7824 // non-local functions, but otherwise there had better be a plt
7825 // call stub or global entry stub as appropriate.
7826 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
7827 }
7828
7829 if (r_type == elfcpp::R_POWERPC_GOT16
7830 || r_type == elfcpp::R_POWERPC_GOT16_LO
7831 || r_type == elfcpp::R_POWERPC_GOT16_HI
7832 || r_type == elfcpp::R_POWERPC_GOT16_HA
7833 || r_type == elfcpp::R_PPC64_GOT16_DS
7834 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
7835 {
7836 if (gsym != NULL)
7837 {
7838 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7839 value = gsym->got_offset(GOT_TYPE_STANDARD);
7840 }
7841 else
7842 {
7843 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7844 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
7845 }
7846 value -= target->got_section()->got_base_offset(object);
7847 }
7848 else if (r_type == elfcpp::R_PPC64_TOC)
7849 {
7850 value = (target->got_section()->output_section()->address()
7851 + object->toc_base_offset());
7852 }
7853 else if (gsym != NULL
7854 && (r_type == elfcpp::R_POWERPC_REL24
7855 || r_type == elfcpp::R_PPC_PLTREL24)
7856 && has_stub_value)
7857 {
7858 if (size == 64)
7859 {
7860 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7861 Valtype* wv = reinterpret_cast<Valtype*>(view);
7862 bool can_plt_call = false;
7863 if (rela.get_r_offset() + 8 <= view_size)
7864 {
7865 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
7866 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
7867 if ((insn & 1) != 0
7868 && (insn2 == nop
7869 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
7870 {
7871 elfcpp::Swap<32, big_endian>::
7872 writeval(wv + 1, ld_2_1 + target->stk_toc());
7873 can_plt_call = true;
7874 }
7875 }
7876 if (!can_plt_call)
7877 {
7878 // If we don't have a branch and link followed by a nop,
7879 // we can't go via the plt because there is no place to
7880 // put a toc restoring instruction.
7881 // Unless we know we won't be returning.
7882 if (strcmp(gsym->name(), "__libc_start_main") == 0)
7883 can_plt_call = true;
7884 }
7885 if (!can_plt_call)
7886 {
7887 // g++ as of 20130507 emits self-calls without a
7888 // following nop. This is arguably wrong since we have
7889 // conflicting information. On the one hand a global
7890 // symbol and on the other a local call sequence, but
7891 // don't error for this special case.
7892 // It isn't possible to cheaply verify we have exactly
7893 // such a call. Allow all calls to the same section.
7894 bool ok = false;
7895 Address code = value;
7896 if (gsym->source() == Symbol::FROM_OBJECT
7897 && gsym->object() == object)
7898 {
7899 unsigned int dest_shndx = 0;
7900 if (target->abiversion() < 2)
7901 {
7902 Address addend = rela.get_r_addend();
7903 code = psymval->value(object, addend);
7904 target->symval_for_branch(relinfo->symtab, gsym, object,
7905 &code, &dest_shndx);
7906 }
7907 bool is_ordinary;
7908 if (dest_shndx == 0)
7909 dest_shndx = gsym->shndx(&is_ordinary);
7910 ok = dest_shndx == relinfo->data_shndx;
7911 }
7912 if (!ok)
7913 {
7914 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7915 _("call lacks nop, can't restore toc; "
7916 "recompile with -fPIC"));
7917 value = code;
7918 }
7919 }
7920 }
7921 }
7922 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7923 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7924 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7925 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7926 {
7927 // First instruction of a global dynamic sequence, arg setup insn.
7928 const bool final = gsym == NULL || gsym->final_value_is_known();
7929 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7930 enum Got_type got_type = GOT_TYPE_STANDARD;
7931 if (tls_type == tls::TLSOPT_NONE)
7932 got_type = GOT_TYPE_TLSGD;
7933 else if (tls_type == tls::TLSOPT_TO_IE)
7934 got_type = GOT_TYPE_TPREL;
7935 if (got_type != GOT_TYPE_STANDARD)
7936 {
7937 if (gsym != NULL)
7938 {
7939 gold_assert(gsym->has_got_offset(got_type));
7940 value = gsym->got_offset(got_type);
7941 }
7942 else
7943 {
7944 gold_assert(object->local_has_got_offset(r_sym, got_type));
7945 value = object->local_got_offset(r_sym, got_type);
7946 }
7947 value -= target->got_section()->got_base_offset(object);
7948 }
7949 if (tls_type == tls::TLSOPT_TO_IE)
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 << 16); // extract rt,ra from addi
7957 if (size == 32)
7958 insn |= 32 << 26; // lwz
7959 else
7960 insn |= 58 << 26; // ld
7961 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7962 }
7963 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7964 - elfcpp::R_POWERPC_GOT_TLSGD16);
7965 }
7966 else if (tls_type == tls::TLSOPT_TO_LE)
7967 {
7968 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7969 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7970 {
7971 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7972 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7973 insn &= (1 << 26) - (1 << 21); // extract rt
7974 if (size == 32)
7975 insn |= addis_0_2;
7976 else
7977 insn |= addis_0_13;
7978 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7979 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7980 value = psymval->value(object, rela.get_r_addend());
7981 }
7982 else
7983 {
7984 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7985 Insn insn = nop;
7986 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7987 r_type = elfcpp::R_POWERPC_NONE;
7988 }
7989 }
7990 }
7991 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7992 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7993 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7994 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7995 {
7996 // First instruction of a local dynamic sequence, arg setup insn.
7997 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7998 if (tls_type == tls::TLSOPT_NONE)
7999 {
8000 value = target->tlsld_got_offset();
8001 value -= target->got_section()->got_base_offset(object);
8002 }
8003 else
8004 {
8005 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8006 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8007 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8008 {
8009 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8010 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8011 insn &= (1 << 26) - (1 << 21); // extract rt
8012 if (size == 32)
8013 insn |= addis_0_2;
8014 else
8015 insn |= addis_0_13;
8016 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8017 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8018 value = dtp_offset;
8019 }
8020 else
8021 {
8022 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8023 Insn insn = nop;
8024 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8025 r_type = elfcpp::R_POWERPC_NONE;
8026 }
8027 }
8028 }
8029 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
8030 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
8031 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
8032 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
8033 {
8034 // Accesses relative to a local dynamic sequence address,
8035 // no optimisation here.
8036 if (gsym != NULL)
8037 {
8038 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
8039 value = gsym->got_offset(GOT_TYPE_DTPREL);
8040 }
8041 else
8042 {
8043 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
8044 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
8045 }
8046 value -= target->got_section()->got_base_offset(object);
8047 }
8048 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8049 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8050 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8051 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8052 {
8053 // First instruction of initial exec sequence.
8054 const bool final = gsym == NULL || gsym->final_value_is_known();
8055 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8056 if (tls_type == tls::TLSOPT_NONE)
8057 {
8058 if (gsym != NULL)
8059 {
8060 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
8061 value = gsym->got_offset(GOT_TYPE_TPREL);
8062 }
8063 else
8064 {
8065 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
8066 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
8067 }
8068 value -= target->got_section()->got_base_offset(object);
8069 }
8070 else
8071 {
8072 gold_assert(tls_type == tls::TLSOPT_TO_LE);
8073 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8074 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8075 {
8076 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8077 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8078 insn &= (1 << 26) - (1 << 21); // extract rt from ld
8079 if (size == 32)
8080 insn |= addis_0_2;
8081 else
8082 insn |= addis_0_13;
8083 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8084 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8085 value = psymval->value(object, rela.get_r_addend());
8086 }
8087 else
8088 {
8089 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8090 Insn insn = nop;
8091 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8092 r_type = elfcpp::R_POWERPC_NONE;
8093 }
8094 }
8095 }
8096 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8097 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8098 {
8099 // Second instruction of a global dynamic sequence,
8100 // the __tls_get_addr call
8101 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8102 const bool final = gsym == NULL || gsym->final_value_is_known();
8103 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8104 if (tls_type != tls::TLSOPT_NONE)
8105 {
8106 if (tls_type == tls::TLSOPT_TO_IE)
8107 {
8108 Insn* iview = reinterpret_cast<Insn*>(view);
8109 Insn insn = add_3_3_13;
8110 if (size == 32)
8111 insn = add_3_3_2;
8112 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8113 r_type = elfcpp::R_POWERPC_NONE;
8114 }
8115 else
8116 {
8117 Insn* iview = reinterpret_cast<Insn*>(view);
8118 Insn insn = addi_3_3;
8119 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8120 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8121 view += d_offset;
8122 value = psymval->value(object, rela.get_r_addend());
8123 }
8124 this->skip_next_tls_get_addr_call();
8125 }
8126 }
8127 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8128 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8129 {
8130 // Second instruction of a local dynamic sequence,
8131 // the __tls_get_addr call
8132 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
8133 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8134 if (tls_type == tls::TLSOPT_TO_LE)
8135 {
8136 Insn* iview = reinterpret_cast<Insn*>(view);
8137 Insn insn = addi_3_3;
8138 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8139 this->skip_next_tls_get_addr_call();
8140 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8141 view += d_offset;
8142 value = dtp_offset;
8143 }
8144 }
8145 else if (r_type == elfcpp::R_POWERPC_TLS)
8146 {
8147 // Second instruction of an initial exec sequence
8148 const bool final = gsym == NULL || gsym->final_value_is_known();
8149 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8150 if (tls_type == tls::TLSOPT_TO_LE)
8151 {
8152 Insn* iview = reinterpret_cast<Insn*>(view);
8153 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8154 unsigned int reg = size == 32 ? 2 : 13;
8155 insn = at_tls_transform(insn, reg);
8156 gold_assert(insn != 0);
8157 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8158 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8159 view += d_offset;
8160 value = psymval->value(object, rela.get_r_addend());
8161 }
8162 }
8163 else if (!has_stub_value)
8164 {
8165 Address addend = 0;
8166 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
8167 addend = rela.get_r_addend();
8168 value = psymval->value(object, addend);
8169 if (size == 64 && is_branch_reloc(r_type))
8170 {
8171 if (target->abiversion() >= 2)
8172 {
8173 if (gsym != NULL)
8174 value += object->ppc64_local_entry_offset(gsym);
8175 else
8176 value += object->ppc64_local_entry_offset(r_sym);
8177 }
8178 else
8179 {
8180 unsigned int dest_shndx;
8181 target->symval_for_branch(relinfo->symtab, gsym, object,
8182 &value, &dest_shndx);
8183 }
8184 }
8185 Address max_branch_offset = max_branch_delta(r_type);
8186 if (max_branch_offset != 0
8187 && value - address + max_branch_offset >= 2 * max_branch_offset)
8188 {
8189 Stub_table<size, big_endian>* stub_table
8190 = object->stub_table(relinfo->data_shndx);
8191 if (stub_table != NULL)
8192 {
8193 Address off = stub_table->find_long_branch_entry(object, value);
8194 if (off != invalid_address)
8195 {
8196 value = (stub_table->stub_address() + stub_table->plt_size()
8197 + off);
8198 has_stub_value = true;
8199 }
8200 }
8201 }
8202 }
8203
8204 switch (r_type)
8205 {
8206 case elfcpp::R_PPC64_REL64:
8207 case elfcpp::R_POWERPC_REL32:
8208 case elfcpp::R_POWERPC_REL24:
8209 case elfcpp::R_PPC_PLTREL24:
8210 case elfcpp::R_PPC_LOCAL24PC:
8211 case elfcpp::R_POWERPC_REL16:
8212 case elfcpp::R_POWERPC_REL16_LO:
8213 case elfcpp::R_POWERPC_REL16_HI:
8214 case elfcpp::R_POWERPC_REL16_HA:
8215 case elfcpp::R_POWERPC_REL16DX_HA:
8216 case elfcpp::R_POWERPC_REL14:
8217 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8218 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8219 value -= address;
8220 break;
8221
8222 case elfcpp::R_PPC64_TOC16:
8223 case elfcpp::R_PPC64_TOC16_LO:
8224 case elfcpp::R_PPC64_TOC16_HI:
8225 case elfcpp::R_PPC64_TOC16_HA:
8226 case elfcpp::R_PPC64_TOC16_DS:
8227 case elfcpp::R_PPC64_TOC16_LO_DS:
8228 // Subtract the TOC base address.
8229 value -= (target->got_section()->output_section()->address()
8230 + object->toc_base_offset());
8231 break;
8232
8233 case elfcpp::R_POWERPC_SECTOFF:
8234 case elfcpp::R_POWERPC_SECTOFF_LO:
8235 case elfcpp::R_POWERPC_SECTOFF_HI:
8236 case elfcpp::R_POWERPC_SECTOFF_HA:
8237 case elfcpp::R_PPC64_SECTOFF_DS:
8238 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8239 if (os != NULL)
8240 value -= os->address();
8241 break;
8242
8243 case elfcpp::R_PPC64_TPREL16_DS:
8244 case elfcpp::R_PPC64_TPREL16_LO_DS:
8245 case elfcpp::R_PPC64_TPREL16_HIGH:
8246 case elfcpp::R_PPC64_TPREL16_HIGHA:
8247 if (size != 64)
8248 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
8249 break;
8250 // Fall through.
8251 case elfcpp::R_POWERPC_TPREL16:
8252 case elfcpp::R_POWERPC_TPREL16_LO:
8253 case elfcpp::R_POWERPC_TPREL16_HI:
8254 case elfcpp::R_POWERPC_TPREL16_HA:
8255 case elfcpp::R_POWERPC_TPREL:
8256 case elfcpp::R_PPC64_TPREL16_HIGHER:
8257 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8258 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8259 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8260 // tls symbol values are relative to tls_segment()->vaddr()
8261 value -= tp_offset;
8262 break;
8263
8264 case elfcpp::R_PPC64_DTPREL16_DS:
8265 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8266 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8267 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8268 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8269 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8270 if (size != 64)
8271 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
8272 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
8273 break;
8274 // Fall through.
8275 case elfcpp::R_POWERPC_DTPREL16:
8276 case elfcpp::R_POWERPC_DTPREL16_LO:
8277 case elfcpp::R_POWERPC_DTPREL16_HI:
8278 case elfcpp::R_POWERPC_DTPREL16_HA:
8279 case elfcpp::R_POWERPC_DTPREL:
8280 case elfcpp::R_PPC64_DTPREL16_HIGH:
8281 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8282 // tls symbol values are relative to tls_segment()->vaddr()
8283 value -= dtp_offset;
8284 break;
8285
8286 case elfcpp::R_PPC64_ADDR64_LOCAL:
8287 if (gsym != NULL)
8288 value += object->ppc64_local_entry_offset(gsym);
8289 else
8290 value += object->ppc64_local_entry_offset(r_sym);
8291 break;
8292
8293 default:
8294 break;
8295 }
8296
8297 Insn branch_bit = 0;
8298 switch (r_type)
8299 {
8300 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8301 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8302 branch_bit = 1 << 21;
8303 // Fall through.
8304 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8305 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8306 {
8307 Insn* iview = reinterpret_cast<Insn*>(view);
8308 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8309 insn &= ~(1 << 21);
8310 insn |= branch_bit;
8311 if (this->is_isa_v2)
8312 {
8313 // Set 'a' bit. This is 0b00010 in BO field for branch
8314 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
8315 // for branch on CTR insns (BO == 1a00t or 1a01t).
8316 if ((insn & (0x14 << 21)) == (0x04 << 21))
8317 insn |= 0x02 << 21;
8318 else if ((insn & (0x14 << 21)) == (0x10 << 21))
8319 insn |= 0x08 << 21;
8320 else
8321 break;
8322 }
8323 else
8324 {
8325 // Invert 'y' bit if not the default.
8326 if (static_cast<Signed_address>(value) < 0)
8327 insn ^= 1 << 21;
8328 }
8329 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8330 }
8331 break;
8332
8333 default:
8334 break;
8335 }
8336
8337 if (size == 64)
8338 {
8339 switch (r_type)
8340 {
8341 default:
8342 break;
8343
8344 // Multi-instruction sequences that access the GOT/TOC can
8345 // be optimized, eg.
8346 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
8347 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
8348 // and
8349 // addis ra,r2,0; addi rb,ra,x@toc@l;
8350 // to nop; addi rb,r2,x@toc;
8351 // FIXME: the @got sequence shown above is not yet
8352 // optimized. Note that gcc as of 2017-01-07 doesn't use
8353 // the ELF @got relocs except for TLS, instead using the
8354 // PowerOpen variant of a compiler managed GOT (called TOC).
8355 // The PowerOpen TOC sequence equivalent to the first
8356 // example is optimized.
8357 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8358 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8359 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8360 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8361 case elfcpp::R_POWERPC_GOT16_HA:
8362 case elfcpp::R_PPC64_TOC16_HA:
8363 if (parameters->options().toc_optimize())
8364 {
8365 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8366 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8367 if (r_type == elfcpp::R_PPC64_TOC16_HA
8368 && object->make_toc_relative(target, &value))
8369 {
8370 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
8371 == ((15u << 26) | (2 << 16)));
8372 }
8373 if (((insn & ((0x3f << 26) | 0x1f << 16))
8374 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8375 && value + 0x8000 < 0x10000)
8376 {
8377 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
8378 return true;
8379 }
8380 }
8381 break;
8382
8383 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8384 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8385 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8386 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8387 case elfcpp::R_POWERPC_GOT16_LO:
8388 case elfcpp::R_PPC64_GOT16_LO_DS:
8389 case elfcpp::R_PPC64_TOC16_LO:
8390 case elfcpp::R_PPC64_TOC16_LO_DS:
8391 if (parameters->options().toc_optimize())
8392 {
8393 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8394 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
8395 bool changed = false;
8396 if (r_type == elfcpp::R_PPC64_TOC16_LO_DS
8397 && object->make_toc_relative(target, &value))
8398 {
8399 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
8400 insn ^= (14u << 26) ^ (58u << 26);
8401 r_type = elfcpp::R_PPC64_TOC16_LO;
8402 changed = true;
8403 }
8404 if (ok_lo_toc_insn(insn, r_type)
8405 && value + 0x8000 < 0x10000)
8406 {
8407 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
8408 {
8409 // Transform addic to addi when we change reg.
8410 insn &= ~((0x3f << 26) | (0x1f << 16));
8411 insn |= (14u << 26) | (2 << 16);
8412 }
8413 else
8414 {
8415 insn &= ~(0x1f << 16);
8416 insn |= 2 << 16;
8417 }
8418 changed = true;
8419 }
8420 if (changed)
8421 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
8422 }
8423 break;
8424
8425 case elfcpp::R_PPC64_ENTRY:
8426 value = (target->got_section()->output_section()->address()
8427 + object->toc_base_offset());
8428 if (value + 0x80008000 <= 0xffffffff
8429 && !parameters->options().output_is_position_independent())
8430 {
8431 Insn* iview = reinterpret_cast<Insn*>(view);
8432 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8433 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8434
8435 if ((insn1 & ~0xfffc) == ld_2_12
8436 && insn2 == add_2_2_12)
8437 {
8438 insn1 = lis_2 + ha(value);
8439 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8440 insn2 = addi_2_2 + l(value);
8441 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8442 return true;
8443 }
8444 }
8445 else
8446 {
8447 value -= address;
8448 if (value + 0x80008000 <= 0xffffffff)
8449 {
8450 Insn* iview = reinterpret_cast<Insn*>(view);
8451 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
8452 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
8453
8454 if ((insn1 & ~0xfffc) == ld_2_12
8455 && insn2 == add_2_2_12)
8456 {
8457 insn1 = addis_2_12 + ha(value);
8458 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
8459 insn2 = addi_2_2 + l(value);
8460 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
8461 return true;
8462 }
8463 }
8464 }
8465 break;
8466
8467 case elfcpp::R_POWERPC_REL16_LO:
8468 // If we are generating a non-PIC executable, edit
8469 // 0: addis 2,12,.TOC.-0b@ha
8470 // addi 2,2,.TOC.-0b@l
8471 // used by ELFv2 global entry points to set up r2, to
8472 // lis 2,.TOC.@ha
8473 // addi 2,2,.TOC.@l
8474 // if .TOC. is in range. */
8475 if (value + address - 4 + 0x80008000 <= 0xffffffff
8476 && relnum != 0
8477 && preloc != NULL
8478 && target->abiversion() >= 2
8479 && !parameters->options().output_is_position_independent()
8480 && rela.get_r_addend() == d_offset + 4
8481 && gsym != NULL
8482 && strcmp(gsym->name(), ".TOC.") == 0)
8483 {
8484 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8485 Reltype prev_rela(preloc - reloc_size);
8486 if ((prev_rela.get_r_info()
8487 == elfcpp::elf_r_info<size>(r_sym,
8488 elfcpp::R_POWERPC_REL16_HA))
8489 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
8490 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
8491 {
8492 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8493 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
8494 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
8495
8496 if ((insn1 & 0xffff0000) == addis_2_12
8497 && (insn2 & 0xffff0000) == addi_2_2)
8498 {
8499 insn1 = lis_2 + ha(value + address - 4);
8500 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
8501 insn2 = addi_2_2 + l(value + address - 4);
8502 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
8503 if (relinfo->rr)
8504 {
8505 relinfo->rr->set_strategy(relnum - 1,
8506 Relocatable_relocs::RELOC_SPECIAL);
8507 relinfo->rr->set_strategy(relnum,
8508 Relocatable_relocs::RELOC_SPECIAL);
8509 }
8510 return true;
8511 }
8512 }
8513 }
8514 break;
8515 }
8516 }
8517
8518 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
8519 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
8520 switch (r_type)
8521 {
8522 case elfcpp::R_POWERPC_ADDR32:
8523 case elfcpp::R_POWERPC_UADDR32:
8524 if (size == 64)
8525 overflow = Reloc::CHECK_BITFIELD;
8526 break;
8527
8528 case elfcpp::R_POWERPC_REL32:
8529 case elfcpp::R_POWERPC_REL16DX_HA:
8530 if (size == 64)
8531 overflow = Reloc::CHECK_SIGNED;
8532 break;
8533
8534 case elfcpp::R_POWERPC_UADDR16:
8535 overflow = Reloc::CHECK_BITFIELD;
8536 break;
8537
8538 case elfcpp::R_POWERPC_ADDR16:
8539 // We really should have three separate relocations,
8540 // one for 16-bit data, one for insns with 16-bit signed fields,
8541 // and one for insns with 16-bit unsigned fields.
8542 overflow = Reloc::CHECK_BITFIELD;
8543 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
8544 overflow = Reloc::CHECK_LOW_INSN;
8545 break;
8546
8547 case elfcpp::R_POWERPC_ADDR16_HI:
8548 case elfcpp::R_POWERPC_ADDR16_HA:
8549 case elfcpp::R_POWERPC_GOT16_HI:
8550 case elfcpp::R_POWERPC_GOT16_HA:
8551 case elfcpp::R_POWERPC_PLT16_HI:
8552 case elfcpp::R_POWERPC_PLT16_HA:
8553 case elfcpp::R_POWERPC_SECTOFF_HI:
8554 case elfcpp::R_POWERPC_SECTOFF_HA:
8555 case elfcpp::R_PPC64_TOC16_HI:
8556 case elfcpp::R_PPC64_TOC16_HA:
8557 case elfcpp::R_PPC64_PLTGOT16_HI:
8558 case elfcpp::R_PPC64_PLTGOT16_HA:
8559 case elfcpp::R_POWERPC_TPREL16_HI:
8560 case elfcpp::R_POWERPC_TPREL16_HA:
8561 case elfcpp::R_POWERPC_DTPREL16_HI:
8562 case elfcpp::R_POWERPC_DTPREL16_HA:
8563 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8564 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8565 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8566 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8567 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8568 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8569 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8570 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8571 case elfcpp::R_POWERPC_REL16_HI:
8572 case elfcpp::R_POWERPC_REL16_HA:
8573 if (size != 32)
8574 overflow = Reloc::CHECK_HIGH_INSN;
8575 break;
8576
8577 case elfcpp::R_POWERPC_REL16:
8578 case elfcpp::R_PPC64_TOC16:
8579 case elfcpp::R_POWERPC_GOT16:
8580 case elfcpp::R_POWERPC_SECTOFF:
8581 case elfcpp::R_POWERPC_TPREL16:
8582 case elfcpp::R_POWERPC_DTPREL16:
8583 case elfcpp::R_POWERPC_GOT_TLSGD16:
8584 case elfcpp::R_POWERPC_GOT_TLSLD16:
8585 case elfcpp::R_POWERPC_GOT_TPREL16:
8586 case elfcpp::R_POWERPC_GOT_DTPREL16:
8587 overflow = Reloc::CHECK_LOW_INSN;
8588 break;
8589
8590 case elfcpp::R_POWERPC_ADDR24:
8591 case elfcpp::R_POWERPC_ADDR14:
8592 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8593 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8594 case elfcpp::R_PPC64_ADDR16_DS:
8595 case elfcpp::R_POWERPC_REL24:
8596 case elfcpp::R_PPC_PLTREL24:
8597 case elfcpp::R_PPC_LOCAL24PC:
8598 case elfcpp::R_PPC64_TPREL16_DS:
8599 case elfcpp::R_PPC64_DTPREL16_DS:
8600 case elfcpp::R_PPC64_TOC16_DS:
8601 case elfcpp::R_PPC64_GOT16_DS:
8602 case elfcpp::R_PPC64_SECTOFF_DS:
8603 case elfcpp::R_POWERPC_REL14:
8604 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8605 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8606 overflow = Reloc::CHECK_SIGNED;
8607 break;
8608 }
8609
8610 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
8611 Insn insn = 0;
8612
8613 if (overflow == Reloc::CHECK_LOW_INSN
8614 || overflow == Reloc::CHECK_HIGH_INSN)
8615 {
8616 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8617
8618 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
8619 overflow = Reloc::CHECK_BITFIELD;
8620 else if (overflow == Reloc::CHECK_LOW_INSN
8621 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
8622 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
8623 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
8624 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
8625 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
8626 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
8627 overflow = Reloc::CHECK_UNSIGNED;
8628 else
8629 overflow = Reloc::CHECK_SIGNED;
8630 }
8631
8632 bool maybe_dq_reloc = false;
8633 typename Powerpc_relocate_functions<size, big_endian>::Status status
8634 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
8635 switch (r_type)
8636 {
8637 case elfcpp::R_POWERPC_NONE:
8638 case elfcpp::R_POWERPC_TLS:
8639 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8640 case elfcpp::R_POWERPC_GNU_VTENTRY:
8641 break;
8642
8643 case elfcpp::R_PPC64_ADDR64:
8644 case elfcpp::R_PPC64_REL64:
8645 case elfcpp::R_PPC64_TOC:
8646 case elfcpp::R_PPC64_ADDR64_LOCAL:
8647 Reloc::addr64(view, value);
8648 break;
8649
8650 case elfcpp::R_POWERPC_TPREL:
8651 case elfcpp::R_POWERPC_DTPREL:
8652 if (size == 64)
8653 Reloc::addr64(view, value);
8654 else
8655 status = Reloc::addr32(view, value, overflow);
8656 break;
8657
8658 case elfcpp::R_PPC64_UADDR64:
8659 Reloc::addr64_u(view, value);
8660 break;
8661
8662 case elfcpp::R_POWERPC_ADDR32:
8663 status = Reloc::addr32(view, value, overflow);
8664 break;
8665
8666 case elfcpp::R_POWERPC_REL32:
8667 case elfcpp::R_POWERPC_UADDR32:
8668 status = Reloc::addr32_u(view, value, overflow);
8669 break;
8670
8671 case elfcpp::R_POWERPC_ADDR24:
8672 case elfcpp::R_POWERPC_REL24:
8673 case elfcpp::R_PPC_PLTREL24:
8674 case elfcpp::R_PPC_LOCAL24PC:
8675 status = Reloc::addr24(view, value, overflow);
8676 break;
8677
8678 case elfcpp::R_POWERPC_GOT_DTPREL16:
8679 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8680 case elfcpp::R_POWERPC_GOT_TPREL16:
8681 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8682 if (size == 64)
8683 {
8684 // On ppc64 these are all ds form
8685 maybe_dq_reloc = true;
8686 break;
8687 }
8688 // Fall through.
8689 case elfcpp::R_POWERPC_ADDR16:
8690 case elfcpp::R_POWERPC_REL16:
8691 case elfcpp::R_PPC64_TOC16:
8692 case elfcpp::R_POWERPC_GOT16:
8693 case elfcpp::R_POWERPC_SECTOFF:
8694 case elfcpp::R_POWERPC_TPREL16:
8695 case elfcpp::R_POWERPC_DTPREL16:
8696 case elfcpp::R_POWERPC_GOT_TLSGD16:
8697 case elfcpp::R_POWERPC_GOT_TLSLD16:
8698 case elfcpp::R_POWERPC_ADDR16_LO:
8699 case elfcpp::R_POWERPC_REL16_LO:
8700 case elfcpp::R_PPC64_TOC16_LO:
8701 case elfcpp::R_POWERPC_GOT16_LO:
8702 case elfcpp::R_POWERPC_SECTOFF_LO:
8703 case elfcpp::R_POWERPC_TPREL16_LO:
8704 case elfcpp::R_POWERPC_DTPREL16_LO:
8705 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8706 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8707 if (size == 64)
8708 status = Reloc::addr16(view, value, overflow);
8709 else
8710 maybe_dq_reloc = true;
8711 break;
8712
8713 case elfcpp::R_POWERPC_UADDR16:
8714 status = Reloc::addr16_u(view, value, overflow);
8715 break;
8716
8717 case elfcpp::R_PPC64_ADDR16_HIGH:
8718 case elfcpp::R_PPC64_TPREL16_HIGH:
8719 case elfcpp::R_PPC64_DTPREL16_HIGH:
8720 if (size == 32)
8721 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8722 goto unsupp;
8723 // Fall through.
8724 case elfcpp::R_POWERPC_ADDR16_HI:
8725 case elfcpp::R_POWERPC_REL16_HI:
8726 case elfcpp::R_PPC64_TOC16_HI:
8727 case elfcpp::R_POWERPC_GOT16_HI:
8728 case elfcpp::R_POWERPC_SECTOFF_HI:
8729 case elfcpp::R_POWERPC_TPREL16_HI:
8730 case elfcpp::R_POWERPC_DTPREL16_HI:
8731 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8732 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8733 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8734 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8735 Reloc::addr16_hi(view, value);
8736 break;
8737
8738 case elfcpp::R_PPC64_ADDR16_HIGHA:
8739 case elfcpp::R_PPC64_TPREL16_HIGHA:
8740 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8741 if (size == 32)
8742 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8743 goto unsupp;
8744 // Fall through.
8745 case elfcpp::R_POWERPC_ADDR16_HA:
8746 case elfcpp::R_POWERPC_REL16_HA:
8747 case elfcpp::R_PPC64_TOC16_HA:
8748 case elfcpp::R_POWERPC_GOT16_HA:
8749 case elfcpp::R_POWERPC_SECTOFF_HA:
8750 case elfcpp::R_POWERPC_TPREL16_HA:
8751 case elfcpp::R_POWERPC_DTPREL16_HA:
8752 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8753 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8754 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8755 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8756 Reloc::addr16_ha(view, value);
8757 break;
8758
8759 case elfcpp::R_POWERPC_REL16DX_HA:
8760 status = Reloc::addr16dx_ha(view, value, overflow);
8761 break;
8762
8763 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8764 if (size == 32)
8765 // R_PPC_EMB_NADDR16_LO
8766 goto unsupp;
8767 // Fall through.
8768 case elfcpp::R_PPC64_ADDR16_HIGHER:
8769 case elfcpp::R_PPC64_TPREL16_HIGHER:
8770 Reloc::addr16_hi2(view, value);
8771 break;
8772
8773 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8774 if (size == 32)
8775 // R_PPC_EMB_NADDR16_HI
8776 goto unsupp;
8777 // Fall through.
8778 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8779 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8780 Reloc::addr16_ha2(view, value);
8781 break;
8782
8783 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8784 if (size == 32)
8785 // R_PPC_EMB_NADDR16_HA
8786 goto unsupp;
8787 // Fall through.
8788 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8789 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8790 Reloc::addr16_hi3(view, value);
8791 break;
8792
8793 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8794 if (size == 32)
8795 // R_PPC_EMB_SDAI16
8796 goto unsupp;
8797 // Fall through.
8798 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8799 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8800 Reloc::addr16_ha3(view, value);
8801 break;
8802
8803 case elfcpp::R_PPC64_DTPREL16_DS:
8804 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8805 if (size == 32)
8806 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8807 goto unsupp;
8808 // Fall through.
8809 case elfcpp::R_PPC64_TPREL16_DS:
8810 case elfcpp::R_PPC64_TPREL16_LO_DS:
8811 if (size == 32)
8812 // R_PPC_TLSGD, R_PPC_TLSLD
8813 break;
8814 // Fall through.
8815 case elfcpp::R_PPC64_ADDR16_DS:
8816 case elfcpp::R_PPC64_ADDR16_LO_DS:
8817 case elfcpp::R_PPC64_TOC16_DS:
8818 case elfcpp::R_PPC64_TOC16_LO_DS:
8819 case elfcpp::R_PPC64_GOT16_DS:
8820 case elfcpp::R_PPC64_GOT16_LO_DS:
8821 case elfcpp::R_PPC64_SECTOFF_DS:
8822 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8823 maybe_dq_reloc = true;
8824 break;
8825
8826 case elfcpp::R_POWERPC_ADDR14:
8827 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8828 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8829 case elfcpp::R_POWERPC_REL14:
8830 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8831 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8832 status = Reloc::addr14(view, value, overflow);
8833 break;
8834
8835 case elfcpp::R_POWERPC_COPY:
8836 case elfcpp::R_POWERPC_GLOB_DAT:
8837 case elfcpp::R_POWERPC_JMP_SLOT:
8838 case elfcpp::R_POWERPC_RELATIVE:
8839 case elfcpp::R_POWERPC_DTPMOD:
8840 case elfcpp::R_PPC64_JMP_IREL:
8841 case elfcpp::R_POWERPC_IRELATIVE:
8842 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8843 _("unexpected reloc %u in object file"),
8844 r_type);
8845 break;
8846
8847 case elfcpp::R_PPC_EMB_SDA21:
8848 if (size == 32)
8849 goto unsupp;
8850 else
8851 {
8852 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8853 }
8854 break;
8855
8856 case elfcpp::R_PPC_EMB_SDA2I16:
8857 case elfcpp::R_PPC_EMB_SDA2REL:
8858 if (size == 32)
8859 goto unsupp;
8860 // R_PPC64_TLSGD, R_PPC64_TLSLD
8861 break;
8862
8863 case elfcpp::R_POWERPC_PLT32:
8864 case elfcpp::R_POWERPC_PLTREL32:
8865 case elfcpp::R_POWERPC_PLT16_LO:
8866 case elfcpp::R_POWERPC_PLT16_HI:
8867 case elfcpp::R_POWERPC_PLT16_HA:
8868 case elfcpp::R_PPC_SDAREL16:
8869 case elfcpp::R_POWERPC_ADDR30:
8870 case elfcpp::R_PPC64_PLT64:
8871 case elfcpp::R_PPC64_PLTREL64:
8872 case elfcpp::R_PPC64_PLTGOT16:
8873 case elfcpp::R_PPC64_PLTGOT16_LO:
8874 case elfcpp::R_PPC64_PLTGOT16_HI:
8875 case elfcpp::R_PPC64_PLTGOT16_HA:
8876 case elfcpp::R_PPC64_PLT16_LO_DS:
8877 case elfcpp::R_PPC64_PLTGOT16_DS:
8878 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
8879 case elfcpp::R_PPC_EMB_RELSDA:
8880 case elfcpp::R_PPC_TOC16:
8881 default:
8882 unsupp:
8883 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8884 _("unsupported reloc %u"),
8885 r_type);
8886 break;
8887 }
8888
8889 if (maybe_dq_reloc)
8890 {
8891 if (insn == 0)
8892 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8893
8894 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
8895 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8896 && (insn & 3) == 1))
8897 status = Reloc::addr16_dq(view, value, overflow);
8898 else if (size == 64
8899 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8900 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8901 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
8902 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
8903 status = Reloc::addr16_ds(view, value, overflow);
8904 else
8905 status = Reloc::addr16(view, value, overflow);
8906 }
8907
8908 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
8909 && (has_stub_value
8910 || !(gsym != NULL
8911 && gsym->is_undefined()
8912 && is_branch_reloc(r_type))))
8913 {
8914 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8915 _("relocation overflow"));
8916 if (has_stub_value)
8917 gold_info(_("try relinking with a smaller --stub-group-size"));
8918 }
8919
8920 return true;
8921 }
8922
8923 // Relocate section data.
8924
8925 template<int size, bool big_endian>
8926 void
8927 Target_powerpc<size, big_endian>::relocate_section(
8928 const Relocate_info<size, big_endian>* relinfo,
8929 unsigned int sh_type,
8930 const unsigned char* prelocs,
8931 size_t reloc_count,
8932 Output_section* output_section,
8933 bool needs_special_offset_handling,
8934 unsigned char* view,
8935 Address address,
8936 section_size_type view_size,
8937 const Reloc_symbol_changes* reloc_symbol_changes)
8938 {
8939 typedef Target_powerpc<size, big_endian> Powerpc;
8940 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
8941 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
8942 Powerpc_comdat_behavior;
8943 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8944 Classify_reloc;
8945
8946 gold_assert(sh_type == elfcpp::SHT_RELA);
8947
8948 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
8949 Powerpc_comdat_behavior, Classify_reloc>(
8950 relinfo,
8951 this,
8952 prelocs,
8953 reloc_count,
8954 output_section,
8955 needs_special_offset_handling,
8956 view,
8957 address,
8958 view_size,
8959 reloc_symbol_changes);
8960 }
8961
8962 template<int size, bool big_endian>
8963 class Powerpc_scan_relocatable_reloc
8964 {
8965 public:
8966 typedef typename elfcpp::Rela<size, big_endian> Reltype;
8967 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
8968 static const int sh_type = elfcpp::SHT_RELA;
8969
8970 // Return the symbol referred to by the relocation.
8971 static inline unsigned int
8972 get_r_sym(const Reltype* reloc)
8973 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
8974
8975 // Return the type of the relocation.
8976 static inline unsigned int
8977 get_r_type(const Reltype* reloc)
8978 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
8979
8980 // Return the strategy to use for a local symbol which is not a
8981 // section symbol, given the relocation type.
8982 inline Relocatable_relocs::Reloc_strategy
8983 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
8984 {
8985 if (r_type == 0 && r_sym == 0)
8986 return Relocatable_relocs::RELOC_DISCARD;
8987 return Relocatable_relocs::RELOC_COPY;
8988 }
8989
8990 // Return the strategy to use for a local symbol which is a section
8991 // symbol, given the relocation type.
8992 inline Relocatable_relocs::Reloc_strategy
8993 local_section_strategy(unsigned int, Relobj*)
8994 {
8995 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
8996 }
8997
8998 // Return the strategy to use for a global symbol, given the
8999 // relocation type, the object, and the symbol index.
9000 inline Relocatable_relocs::Reloc_strategy
9001 global_strategy(unsigned int r_type, Relobj*, unsigned int)
9002 {
9003 if (r_type == elfcpp::R_PPC_PLTREL24)
9004 return Relocatable_relocs::RELOC_SPECIAL;
9005 return Relocatable_relocs::RELOC_COPY;
9006 }
9007 };
9008
9009 // Scan the relocs during a relocatable link.
9010
9011 template<int size, bool big_endian>
9012 void
9013 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
9014 Symbol_table* symtab,
9015 Layout* layout,
9016 Sized_relobj_file<size, big_endian>* object,
9017 unsigned int data_shndx,
9018 unsigned int sh_type,
9019 const unsigned char* prelocs,
9020 size_t reloc_count,
9021 Output_section* output_section,
9022 bool needs_special_offset_handling,
9023 size_t local_symbol_count,
9024 const unsigned char* plocal_symbols,
9025 Relocatable_relocs* rr)
9026 {
9027 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
9028
9029 gold_assert(sh_type == elfcpp::SHT_RELA);
9030
9031 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
9032 symtab,
9033 layout,
9034 object,
9035 data_shndx,
9036 prelocs,
9037 reloc_count,
9038 output_section,
9039 needs_special_offset_handling,
9040 local_symbol_count,
9041 plocal_symbols,
9042 rr);
9043 }
9044
9045 // Scan the relocs for --emit-relocs.
9046
9047 template<int size, bool big_endian>
9048 void
9049 Target_powerpc<size, big_endian>::emit_relocs_scan(
9050 Symbol_table* symtab,
9051 Layout* layout,
9052 Sized_relobj_file<size, big_endian>* object,
9053 unsigned int data_shndx,
9054 unsigned int sh_type,
9055 const unsigned char* prelocs,
9056 size_t reloc_count,
9057 Output_section* output_section,
9058 bool needs_special_offset_handling,
9059 size_t local_symbol_count,
9060 const unsigned char* plocal_syms,
9061 Relocatable_relocs* rr)
9062 {
9063 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9064 Classify_reloc;
9065 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9066 Emit_relocs_strategy;
9067
9068 gold_assert(sh_type == elfcpp::SHT_RELA);
9069
9070 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
9071 symtab,
9072 layout,
9073 object,
9074 data_shndx,
9075 prelocs,
9076 reloc_count,
9077 output_section,
9078 needs_special_offset_handling,
9079 local_symbol_count,
9080 plocal_syms,
9081 rr);
9082 }
9083
9084 // Emit relocations for a section.
9085 // This is a modified version of the function by the same name in
9086 // target-reloc.h. Using relocate_special_relocatable for
9087 // R_PPC_PLTREL24 would require duplication of the entire body of the
9088 // loop, so we may as well duplicate the whole thing.
9089
9090 template<int size, bool big_endian>
9091 void
9092 Target_powerpc<size, big_endian>::relocate_relocs(
9093 const Relocate_info<size, big_endian>* relinfo,
9094 unsigned int sh_type,
9095 const unsigned char* prelocs,
9096 size_t reloc_count,
9097 Output_section* output_section,
9098 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
9099 unsigned char*,
9100 Address view_address,
9101 section_size_type,
9102 unsigned char* reloc_view,
9103 section_size_type reloc_view_size)
9104 {
9105 gold_assert(sh_type == elfcpp::SHT_RELA);
9106
9107 typedef typename elfcpp::Rela<size, big_endian> Reltype;
9108 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
9109 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
9110 // Offset from start of insn to d-field reloc.
9111 const int d_offset = big_endian ? 2 : 0;
9112
9113 Powerpc_relobj<size, big_endian>* const object
9114 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
9115 const unsigned int local_count = object->local_symbol_count();
9116 unsigned int got2_shndx = object->got2_shndx();
9117 Address got2_addend = 0;
9118 if (got2_shndx != 0)
9119 {
9120 got2_addend = object->get_output_section_offset(got2_shndx);
9121 gold_assert(got2_addend != invalid_address);
9122 }
9123
9124 unsigned char* pwrite = reloc_view;
9125 bool zap_next = false;
9126 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
9127 {
9128 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
9129 if (strategy == Relocatable_relocs::RELOC_DISCARD)
9130 continue;
9131
9132 Reltype reloc(prelocs);
9133 Reltype_write reloc_write(pwrite);
9134
9135 Address offset = reloc.get_r_offset();
9136 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
9137 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
9138 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
9139 const unsigned int orig_r_sym = r_sym;
9140 typename elfcpp::Elf_types<size>::Elf_Swxword addend
9141 = reloc.get_r_addend();
9142 const Symbol* gsym = NULL;
9143
9144 if (zap_next)
9145 {
9146 // We could arrange to discard these and other relocs for
9147 // tls optimised sequences in the strategy methods, but for
9148 // now do as BFD ld does.
9149 r_type = elfcpp::R_POWERPC_NONE;
9150 zap_next = false;
9151 }
9152
9153 // Get the new symbol index.
9154 Output_section* os = NULL;
9155 if (r_sym < local_count)
9156 {
9157 switch (strategy)
9158 {
9159 case Relocatable_relocs::RELOC_COPY:
9160 case Relocatable_relocs::RELOC_SPECIAL:
9161 if (r_sym != 0)
9162 {
9163 r_sym = object->symtab_index(r_sym);
9164 gold_assert(r_sym != -1U);
9165 }
9166 break;
9167
9168 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
9169 {
9170 // We are adjusting a section symbol. We need to find
9171 // the symbol table index of the section symbol for
9172 // the output section corresponding to input section
9173 // in which this symbol is defined.
9174 gold_assert(r_sym < local_count);
9175 bool is_ordinary;
9176 unsigned int shndx =
9177 object->local_symbol_input_shndx(r_sym, &is_ordinary);
9178 gold_assert(is_ordinary);
9179 os = object->output_section(shndx);
9180 gold_assert(os != NULL);
9181 gold_assert(os->needs_symtab_index());
9182 r_sym = os->symtab_index();
9183 }
9184 break;
9185
9186 default:
9187 gold_unreachable();
9188 }
9189 }
9190 else
9191 {
9192 gsym = object->global_symbol(r_sym);
9193 gold_assert(gsym != NULL);
9194 if (gsym->is_forwarder())
9195 gsym = relinfo->symtab->resolve_forwards(gsym);
9196
9197 gold_assert(gsym->has_symtab_index());
9198 r_sym = gsym->symtab_index();
9199 }
9200
9201 // Get the new offset--the location in the output section where
9202 // this relocation should be applied.
9203 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9204 offset += offset_in_output_section;
9205 else
9206 {
9207 section_offset_type sot_offset =
9208 convert_types<section_offset_type, Address>(offset);
9209 section_offset_type new_sot_offset =
9210 output_section->output_offset(object, relinfo->data_shndx,
9211 sot_offset);
9212 gold_assert(new_sot_offset != -1);
9213 offset = new_sot_offset;
9214 }
9215
9216 // In an object file, r_offset is an offset within the section.
9217 // In an executable or dynamic object, generated by
9218 // --emit-relocs, r_offset is an absolute address.
9219 if (!parameters->options().relocatable())
9220 {
9221 offset += view_address;
9222 if (static_cast<Address>(offset_in_output_section) != invalid_address)
9223 offset -= offset_in_output_section;
9224 }
9225
9226 // Handle the reloc addend based on the strategy.
9227 if (strategy == Relocatable_relocs::RELOC_COPY)
9228 ;
9229 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
9230 {
9231 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
9232 gold_assert(os != NULL);
9233 addend = psymval->value(object, addend) - os->address();
9234 }
9235 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
9236 {
9237 if (size == 32)
9238 {
9239 if (addend >= 32768)
9240 addend += got2_addend;
9241 }
9242 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
9243 {
9244 r_type = elfcpp::R_POWERPC_ADDR16_HA;
9245 addend -= d_offset;
9246 }
9247 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
9248 {
9249 r_type = elfcpp::R_POWERPC_ADDR16_LO;
9250 addend -= d_offset + 4;
9251 }
9252 }
9253 else
9254 gold_unreachable();
9255
9256 if (!parameters->options().relocatable())
9257 {
9258 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9259 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
9260 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
9261 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
9262 {
9263 // First instruction of a global dynamic sequence,
9264 // arg setup insn.
9265 const bool final = gsym == NULL || gsym->final_value_is_known();
9266 switch (this->optimize_tls_gd(final))
9267 {
9268 case tls::TLSOPT_TO_IE:
9269 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
9270 - elfcpp::R_POWERPC_GOT_TLSGD16);
9271 break;
9272 case tls::TLSOPT_TO_LE:
9273 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
9274 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
9275 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9276 else
9277 {
9278 r_type = elfcpp::R_POWERPC_NONE;
9279 offset -= d_offset;
9280 }
9281 break;
9282 default:
9283 break;
9284 }
9285 }
9286 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9287 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
9288 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
9289 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
9290 {
9291 // First instruction of a local dynamic sequence,
9292 // arg setup insn.
9293 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9294 {
9295 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
9296 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
9297 {
9298 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9299 const Output_section* os = relinfo->layout->tls_segment()
9300 ->first_section();
9301 gold_assert(os != NULL);
9302 gold_assert(os->needs_symtab_index());
9303 r_sym = os->symtab_index();
9304 addend = dtp_offset;
9305 }
9306 else
9307 {
9308 r_type = elfcpp::R_POWERPC_NONE;
9309 offset -= d_offset;
9310 }
9311 }
9312 }
9313 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9314 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
9315 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
9316 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
9317 {
9318 // First instruction of initial exec sequence.
9319 const bool final = gsym == NULL || gsym->final_value_is_known();
9320 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9321 {
9322 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
9323 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
9324 r_type = elfcpp::R_POWERPC_TPREL16_HA;
9325 else
9326 {
9327 r_type = elfcpp::R_POWERPC_NONE;
9328 offset -= d_offset;
9329 }
9330 }
9331 }
9332 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
9333 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
9334 {
9335 // Second instruction of a global dynamic sequence,
9336 // the __tls_get_addr call
9337 const bool final = gsym == NULL || gsym->final_value_is_known();
9338 switch (this->optimize_tls_gd(final))
9339 {
9340 case tls::TLSOPT_TO_IE:
9341 r_type = elfcpp::R_POWERPC_NONE;
9342 zap_next = true;
9343 break;
9344 case tls::TLSOPT_TO_LE:
9345 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9346 offset += d_offset;
9347 zap_next = true;
9348 break;
9349 default:
9350 break;
9351 }
9352 }
9353 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
9354 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
9355 {
9356 // Second instruction of a local dynamic sequence,
9357 // the __tls_get_addr call
9358 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
9359 {
9360 const Output_section* os = relinfo->layout->tls_segment()
9361 ->first_section();
9362 gold_assert(os != NULL);
9363 gold_assert(os->needs_symtab_index());
9364 r_sym = os->symtab_index();
9365 addend = dtp_offset;
9366 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9367 offset += d_offset;
9368 zap_next = true;
9369 }
9370 }
9371 else if (r_type == elfcpp::R_POWERPC_TLS)
9372 {
9373 // Second instruction of an initial exec sequence
9374 const bool final = gsym == NULL || gsym->final_value_is_known();
9375 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
9376 {
9377 r_type = elfcpp::R_POWERPC_TPREL16_LO;
9378 offset += d_offset;
9379 }
9380 }
9381 }
9382
9383 reloc_write.put_r_offset(offset);
9384 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
9385 reloc_write.put_r_addend(addend);
9386
9387 pwrite += reloc_size;
9388 }
9389
9390 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
9391 == reloc_view_size);
9392 }
9393
9394 // Return the value to use for a dynamic symbol which requires special
9395 // treatment. This is how we support equality comparisons of function
9396 // pointers across shared library boundaries, as described in the
9397 // processor specific ABI supplement.
9398
9399 template<int size, bool big_endian>
9400 uint64_t
9401 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
9402 {
9403 if (size == 32)
9404 {
9405 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9406 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9407 p != this->stub_tables_.end();
9408 ++p)
9409 {
9410 Address off = (*p)->find_plt_call_entry(gsym);
9411 if (off != invalid_address)
9412 return (*p)->stub_address() + off;
9413 }
9414 }
9415 else if (this->abiversion() >= 2)
9416 {
9417 Address off = this->glink_section()->find_global_entry(gsym);
9418 if (off != invalid_address)
9419 return this->glink_section()->global_entry_address() + off;
9420 }
9421 gold_unreachable();
9422 }
9423
9424 // Return the PLT address to use for a local symbol.
9425 template<int size, bool big_endian>
9426 uint64_t
9427 Target_powerpc<size, big_endian>::do_plt_address_for_local(
9428 const Relobj* object,
9429 unsigned int symndx) const
9430 {
9431 if (size == 32)
9432 {
9433 const Sized_relobj<size, big_endian>* relobj
9434 = static_cast<const Sized_relobj<size, big_endian>*>(object);
9435 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9436 p != this->stub_tables_.end();
9437 ++p)
9438 {
9439 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
9440 symndx);
9441 if (off != invalid_address)
9442 return (*p)->stub_address() + off;
9443 }
9444 }
9445 gold_unreachable();
9446 }
9447
9448 // Return the PLT address to use for a global symbol.
9449 template<int size, bool big_endian>
9450 uint64_t
9451 Target_powerpc<size, big_endian>::do_plt_address_for_global(
9452 const Symbol* gsym) const
9453 {
9454 if (size == 32)
9455 {
9456 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
9457 p != this->stub_tables_.end();
9458 ++p)
9459 {
9460 Address off = (*p)->find_plt_call_entry(gsym);
9461 if (off != invalid_address)
9462 return (*p)->stub_address() + off;
9463 }
9464 }
9465 else if (this->abiversion() >= 2)
9466 {
9467 Address off = this->glink_section()->find_global_entry(gsym);
9468 if (off != invalid_address)
9469 return this->glink_section()->global_entry_address() + off;
9470 }
9471 gold_unreachable();
9472 }
9473
9474 // Return the offset to use for the GOT_INDX'th got entry which is
9475 // for a local tls symbol specified by OBJECT, SYMNDX.
9476 template<int size, bool big_endian>
9477 int64_t
9478 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
9479 const Relobj* object,
9480 unsigned int symndx,
9481 unsigned int got_indx) const
9482 {
9483 const Powerpc_relobj<size, big_endian>* ppc_object
9484 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
9485 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
9486 {
9487 for (Got_type got_type = GOT_TYPE_TLSGD;
9488 got_type <= GOT_TYPE_TPREL;
9489 got_type = Got_type(got_type + 1))
9490 if (ppc_object->local_has_got_offset(symndx, got_type))
9491 {
9492 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
9493 if (got_type == GOT_TYPE_TLSGD)
9494 off += size / 8;
9495 if (off == got_indx * (size / 8))
9496 {
9497 if (got_type == GOT_TYPE_TPREL)
9498 return -tp_offset;
9499 else
9500 return -dtp_offset;
9501 }
9502 }
9503 }
9504 gold_unreachable();
9505 }
9506
9507 // Return the offset to use for the GOT_INDX'th got entry which is
9508 // for global tls symbol GSYM.
9509 template<int size, bool big_endian>
9510 int64_t
9511 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
9512 Symbol* gsym,
9513 unsigned int got_indx) const
9514 {
9515 if (gsym->type() == elfcpp::STT_TLS)
9516 {
9517 for (Got_type got_type = GOT_TYPE_TLSGD;
9518 got_type <= GOT_TYPE_TPREL;
9519 got_type = Got_type(got_type + 1))
9520 if (gsym->has_got_offset(got_type))
9521 {
9522 unsigned int off = gsym->got_offset(got_type);
9523 if (got_type == GOT_TYPE_TLSGD)
9524 off += size / 8;
9525 if (off == got_indx * (size / 8))
9526 {
9527 if (got_type == GOT_TYPE_TPREL)
9528 return -tp_offset;
9529 else
9530 return -dtp_offset;
9531 }
9532 }
9533 }
9534 gold_unreachable();
9535 }
9536
9537 // The selector for powerpc object files.
9538
9539 template<int size, bool big_endian>
9540 class Target_selector_powerpc : public Target_selector
9541 {
9542 public:
9543 Target_selector_powerpc()
9544 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
9545 size, big_endian,
9546 (size == 64
9547 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
9548 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
9549 (size == 64
9550 ? (big_endian ? "elf64ppc" : "elf64lppc")
9551 : (big_endian ? "elf32ppc" : "elf32lppc")))
9552 { }
9553
9554 virtual Target*
9555 do_instantiate_target()
9556 { return new Target_powerpc<size, big_endian>(); }
9557 };
9558
9559 Target_selector_powerpc<32, true> target_selector_ppc32;
9560 Target_selector_powerpc<32, false> target_selector_ppc32le;
9561 Target_selector_powerpc<64, true> target_selector_ppc64;
9562 Target_selector_powerpc<64, false> target_selector_ppc64le;
9563
9564 // Instantiate these constants for -O0
9565 template<int size, bool big_endian>
9566 const int Output_data_glink<size, big_endian>::pltresolve_size;
9567 template<int size, bool big_endian>
9568 const typename Output_data_glink<size, big_endian>::Address
9569 Output_data_glink<size, big_endian>::invalid_address;
9570 template<int size, bool big_endian>
9571 const typename Stub_table<size, big_endian>::Address
9572 Stub_table<size, big_endian>::invalid_address;
9573 template<int size, bool big_endian>
9574 const typename Target_powerpc<size, big_endian>::Address
9575 Target_powerpc<size, big_endian>::invalid_address;
9576
9577 } // End anonymous namespace.
This page took 0.275534 seconds and 5 git commands to generate.