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