[GOLD] PowerPC stub debug
[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 before
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_before_branch_(size < 0),
2447 suppress_size_errors_(no_size_errors), group_size_(0),
2448 group_end_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_before_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_end_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 reverse order,
2500 // so the first section is the tail 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 uint32_t group_size = this->stub_group_size_;
2522 if (has14)
2523 this->group_size_ = group_size = group_size >> 10;
2524
2525 if (this_size > group_size && !this->suppress_size_errors_)
2526 gold_warning(_("%s:%s exceeds group size"),
2527 i->relobj()->name().c_str(),
2528 i->relobj()->section_name(i->shndx()).c_str());
2529
2530 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
2531 has14 ? " 14bit" : "",
2532 i->relobj()->name().c_str(),
2533 i->relobj()->section_name(i->shndx()).c_str(),
2534 (long long) this_size,
2535 (long long) this->group_end_addr_ - start_addr);
2536
2537 uint64_t end_addr = start_addr + this_size;
2538 if (this->state_ == HAS_STUB_SECTION)
2539 {
2540 // Can we add this section, which is before the stubs, to the
2541 // group?
2542 if (this->group_end_addr_ - start_addr <= this->group_size_)
2543 return true;
2544 }
2545 else
2546 {
2547 // Stubs are added at the end of "owner_".
2548 // The current section can always be the stub owner, except when
2549 // whole_sec is true and the current section isn't the last of
2550 // the pasted sections. (This restriction for the whole_sec
2551 // case is just to simplify the corner case mentioned in
2552 // group_sections.)
2553 // Note that "owner_" itself is not necessarily part of the
2554 // group of sections served by these stubs!
2555 if (!whole_sec || this->output_section_ != o)
2556 {
2557 this->owner_ = i;
2558 this->output_section_ = o;
2559 }
2560
2561 if (this->state_ == FINDING_STUB_SECTION)
2562 {
2563 if (this->group_end_addr_ - start_addr <= this->group_size_)
2564 return true;
2565 // The group after the stubs has reached maximum size.
2566 // Now see about adding sections before the stubs to the
2567 // group. If the current section has a 14-bit branch and
2568 // the group after the stubs exceeds group_size_ (because
2569 // they didn't have 14-bit branches), don't add sections
2570 // before the stubs: The size of stubs for such a large
2571 // group may exceed the reach of a 14-bit branch.
2572 if (!this->stubs_always_before_branch_
2573 && this_size <= this->group_size_
2574 && this->group_end_addr_ - end_addr <= this->group_size_)
2575 {
2576 gold_debug(DEBUG_TARGET, "adding before stubs");
2577 this->state_ = HAS_STUB_SECTION;
2578 this->group_end_addr_ = end_addr;
2579 return true;
2580 }
2581 }
2582 else if (this->state_ == NO_GROUP)
2583 {
2584 // Only here on very first use of Stub_control
2585 this->state_ = FINDING_STUB_SECTION;
2586 this->group_size_ = group_size;
2587 this->group_end_addr_ = end_addr;
2588 return true;
2589 }
2590 else
2591 gold_unreachable();
2592 }
2593
2594 gold_debug(DEBUG_TARGET, "nope, didn't fit\n");
2595
2596 // The section fails to fit in the current group. Set up a few
2597 // things for the next group. owner_ and output_section_ will be
2598 // set later after we've retrieved those values for the current
2599 // group.
2600 this->state_ = FINDING_STUB_SECTION;
2601 this->group_size_ = group_size;
2602 this->group_end_addr_ = end_addr;
2603 return false;
2604 }
2605
2606 // Look over all the input sections, deciding where to place stubs.
2607
2608 template<int size, bool big_endian>
2609 void
2610 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2611 const Task*,
2612 bool no_size_errors)
2613 {
2614 Stub_control stub_control(this->stub_group_size_, no_size_errors);
2615
2616 // Group input sections and insert stub table
2617 Stub_table_owner* table_owner = NULL;
2618 std::vector<Stub_table_owner*> tables;
2619 Layout::Section_list section_list;
2620 layout->get_executable_sections(&section_list);
2621 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2622 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2623 o != section_list.rend();
2624 ++o)
2625 {
2626 typedef Output_section::Input_section_list Input_section_list;
2627 for (Input_section_list::const_reverse_iterator i
2628 = (*o)->input_sections().rbegin();
2629 i != (*o)->input_sections().rend();
2630 ++i)
2631 {
2632 if (i->is_input_section()
2633 || i->is_relaxed_input_section())
2634 {
2635 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2636 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2637 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2638 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2639 {
2640 table_owner->output_section = stub_control.output_section();
2641 table_owner->owner = stub_control.owner();
2642 stub_control.set_output_and_owner(*o, &*i);
2643 table_owner = NULL;
2644 }
2645 if (table_owner == NULL)
2646 {
2647 table_owner = new Stub_table_owner;
2648 tables.push_back(table_owner);
2649 }
2650 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
2651 }
2652 }
2653 }
2654 if (table_owner != NULL)
2655 {
2656 const Output_section::Input_section* i = stub_control.owner();
2657
2658 if (tables.size() >= 2 && tables[tables.size() - 2]->owner == i)
2659 {
2660 // Corner case. A new stub group was made for the first
2661 // section (last one looked at here) for some reason, but
2662 // the first section is already being used as the owner for
2663 // a stub table for following sections. Force it into that
2664 // stub group.
2665 tables.pop_back();
2666 delete table_owner;
2667 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2668 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2669 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
2670 }
2671 else
2672 {
2673 table_owner->output_section = stub_control.output_section();
2674 table_owner->owner = i;
2675 }
2676 }
2677 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
2678 t != tables.end();
2679 ++t)
2680 {
2681 Stub_table<size, big_endian>* stub_table;
2682
2683 if ((*t)->owner->is_input_section())
2684 stub_table = new Stub_table<size, big_endian>(this,
2685 (*t)->output_section,
2686 (*t)->owner);
2687 else if ((*t)->owner->is_relaxed_input_section())
2688 stub_table = static_cast<Stub_table<size, big_endian>*>(
2689 (*t)->owner->relaxed_input_section());
2690 else
2691 gold_unreachable();
2692 this->stub_tables_.push_back(stub_table);
2693 delete *t;
2694 }
2695 }
2696
2697 static unsigned long
2698 max_branch_delta (unsigned int r_type)
2699 {
2700 if (r_type == elfcpp::R_POWERPC_REL14
2701 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
2702 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2703 return 1L << 15;
2704 if (r_type == elfcpp::R_POWERPC_REL24
2705 || r_type == elfcpp::R_PPC_PLTREL24
2706 || r_type == elfcpp::R_PPC_LOCAL24PC)
2707 return 1L << 25;
2708 return 0;
2709 }
2710
2711 // If this branch needs a plt call stub, or a long branch stub, make one.
2712
2713 template<int size, bool big_endian>
2714 bool
2715 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2716 Stub_table<size, big_endian>* stub_table,
2717 Stub_table<size, big_endian>* ifunc_stub_table,
2718 Symbol_table* symtab) const
2719 {
2720 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2721 if (sym != NULL && sym->is_forwarder())
2722 sym = symtab->resolve_forwards(sym);
2723 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2724 Target_powerpc<size, big_endian>* target =
2725 static_cast<Target_powerpc<size, big_endian>*>(
2726 parameters->sized_target<size, big_endian>());
2727 bool ok = true;
2728
2729 if (gsym != NULL
2730 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
2731 : this->object_->local_has_plt_offset(this->r_sym_))
2732 {
2733 if (size == 64
2734 && gsym != NULL
2735 && target->abiversion() >= 2
2736 && !parameters->options().output_is_position_independent()
2737 && !is_branch_reloc(this->r_type_))
2738 target->glink_section()->add_global_entry(gsym);
2739 else
2740 {
2741 if (stub_table == NULL)
2742 stub_table = this->object_->stub_table(this->shndx_);
2743 if (stub_table == NULL)
2744 {
2745 // This is a ref from a data section to an ifunc symbol.
2746 stub_table = ifunc_stub_table;
2747 }
2748 gold_assert(stub_table != NULL);
2749 Address from = this->object_->get_output_section_offset(this->shndx_);
2750 if (from != invalid_address)
2751 from += (this->object_->output_section(this->shndx_)->address()
2752 + this->offset_);
2753 if (gsym != NULL)
2754 ok = stub_table->add_plt_call_entry(from,
2755 this->object_, gsym,
2756 this->r_type_, this->addend_);
2757 else
2758 ok = stub_table->add_plt_call_entry(from,
2759 this->object_, this->r_sym_,
2760 this->r_type_, this->addend_);
2761 }
2762 }
2763 else
2764 {
2765 Address max_branch_offset = max_branch_delta(this->r_type_);
2766 if (max_branch_offset == 0)
2767 return true;
2768 Address from = this->object_->get_output_section_offset(this->shndx_);
2769 gold_assert(from != invalid_address);
2770 from += (this->object_->output_section(this->shndx_)->address()
2771 + this->offset_);
2772 Address to;
2773 if (gsym != NULL)
2774 {
2775 switch (gsym->source())
2776 {
2777 case Symbol::FROM_OBJECT:
2778 {
2779 Object* symobj = gsym->object();
2780 if (symobj->is_dynamic()
2781 || symobj->pluginobj() != NULL)
2782 return true;
2783 bool is_ordinary;
2784 unsigned int shndx = gsym->shndx(&is_ordinary);
2785 if (shndx == elfcpp::SHN_UNDEF)
2786 return true;
2787 }
2788 break;
2789
2790 case Symbol::IS_UNDEFINED:
2791 return true;
2792
2793 default:
2794 break;
2795 }
2796 Symbol_table::Compute_final_value_status status;
2797 to = symtab->compute_final_value<size>(gsym, &status);
2798 if (status != Symbol_table::CFVS_OK)
2799 return true;
2800 if (size == 64)
2801 to += this->object_->ppc64_local_entry_offset(gsym);
2802 }
2803 else
2804 {
2805 const Symbol_value<size>* psymval
2806 = this->object_->local_symbol(this->r_sym_);
2807 Symbol_value<size> symval;
2808 if (psymval->is_section_symbol())
2809 symval.set_is_section_symbol();
2810 typedef Sized_relobj_file<size, big_endian> ObjType;
2811 typename ObjType::Compute_final_local_value_status status
2812 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2813 &symval, symtab);
2814 if (status != ObjType::CFLV_OK
2815 || !symval.has_output_value())
2816 return true;
2817 to = symval.value(this->object_, 0);
2818 if (size == 64)
2819 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
2820 }
2821 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
2822 to += this->addend_;
2823 if (stub_table == NULL)
2824 stub_table = this->object_->stub_table(this->shndx_);
2825 if (size == 64 && target->abiversion() < 2)
2826 {
2827 unsigned int dest_shndx;
2828 if (!target->symval_for_branch(symtab, gsym, this->object_,
2829 &to, &dest_shndx))
2830 return true;
2831 }
2832 Address delta = to - from;
2833 if (delta + max_branch_offset >= 2 * max_branch_offset)
2834 {
2835 if (stub_table == NULL)
2836 {
2837 gold_warning(_("%s:%s: branch in non-executable section,"
2838 " no long branch stub for you"),
2839 this->object_->name().c_str(),
2840 this->object_->section_name(this->shndx_).c_str());
2841 return true;
2842 }
2843 bool save_res = (size == 64
2844 && gsym != NULL
2845 && gsym->source() == Symbol::IN_OUTPUT_DATA
2846 && gsym->output_data() == target->savres_section());
2847 ok = stub_table->add_long_branch_entry(this->object_,
2848 this->r_type_,
2849 from, to, save_res);
2850 }
2851 }
2852 if (!ok)
2853 gold_debug(DEBUG_TARGET,
2854 "branch at %s:%s+%#lx\n"
2855 "can't reach stub attached to %s:%s",
2856 this->object_->name().c_str(),
2857 this->object_->section_name(this->shndx_).c_str(),
2858 (unsigned long) this->offset_,
2859 stub_table->relobj()->name().c_str(),
2860 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
2861
2862 return ok;
2863 }
2864
2865 // Relaxation hook. This is where we do stub generation.
2866
2867 template<int size, bool big_endian>
2868 bool
2869 Target_powerpc<size, big_endian>::do_relax(int pass,
2870 const Input_objects*,
2871 Symbol_table* symtab,
2872 Layout* layout,
2873 const Task* task)
2874 {
2875 unsigned int prev_brlt_size = 0;
2876 if (pass == 1)
2877 {
2878 bool thread_safe
2879 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
2880 if (size == 64
2881 && this->abiversion() < 2
2882 && !thread_safe
2883 && !parameters->options().user_set_plt_thread_safe())
2884 {
2885 static const char* const thread_starter[] =
2886 {
2887 "pthread_create",
2888 /* libstdc++ */
2889 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2890 /* librt */
2891 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2892 "mq_notify", "create_timer",
2893 /* libanl */
2894 "getaddrinfo_a",
2895 /* libgomp */
2896 "GOMP_parallel",
2897 "GOMP_parallel_start",
2898 "GOMP_parallel_loop_static",
2899 "GOMP_parallel_loop_static_start",
2900 "GOMP_parallel_loop_dynamic",
2901 "GOMP_parallel_loop_dynamic_start",
2902 "GOMP_parallel_loop_guided",
2903 "GOMP_parallel_loop_guided_start",
2904 "GOMP_parallel_loop_runtime",
2905 "GOMP_parallel_loop_runtime_start",
2906 "GOMP_parallel_sections",
2907 "GOMP_parallel_sections_start",
2908 /* libgo */
2909 "__go_go",
2910 };
2911
2912 if (parameters->options().shared())
2913 thread_safe = true;
2914 else
2915 {
2916 for (unsigned int i = 0;
2917 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2918 i++)
2919 {
2920 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2921 thread_safe = (sym != NULL
2922 && sym->in_reg()
2923 && sym->in_real_elf());
2924 if (thread_safe)
2925 break;
2926 }
2927 }
2928 }
2929 this->plt_thread_safe_ = thread_safe;
2930 }
2931
2932 if (pass == 1)
2933 {
2934 this->stub_group_size_ = parameters->options().stub_group_size();
2935 bool no_size_errors = true;
2936 if (this->stub_group_size_ == 1)
2937 this->stub_group_size_ = 0x1c00000;
2938 else if (this->stub_group_size_ == -1)
2939 this->stub_group_size_ = -0x1e00000;
2940 else
2941 no_size_errors = false;
2942 this->group_sections(layout, task, no_size_errors);
2943 }
2944 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
2945 {
2946 this->branch_lookup_table_.clear();
2947 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2948 p != this->stub_tables_.end();
2949 ++p)
2950 {
2951 (*p)->clear_stubs(true);
2952 }
2953 this->stub_tables_.clear();
2954 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
2955 gold_info(_("%s: stub group size is too large; retrying with %#x"),
2956 program_name, this->stub_group_size_);
2957 this->group_sections(layout, task, true);
2958 }
2959
2960 // We need address of stub tables valid for make_stub.
2961 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2962 p != this->stub_tables_.end();
2963 ++p)
2964 {
2965 const Powerpc_relobj<size, big_endian>* object
2966 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2967 Address off = object->get_output_section_offset((*p)->shndx());
2968 gold_assert(off != invalid_address);
2969 Output_section* os = (*p)->output_section();
2970 (*p)->set_address_and_size(os, off);
2971 }
2972
2973 if (pass != 1)
2974 {
2975 // Clear plt call stubs, long branch stubs and branch lookup table.
2976 prev_brlt_size = this->branch_lookup_table_.size();
2977 this->branch_lookup_table_.clear();
2978 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2979 p != this->stub_tables_.end();
2980 ++p)
2981 {
2982 (*p)->clear_stubs(false);
2983 }
2984 }
2985
2986 // Build all the stubs.
2987 this->relax_failed_ = false;
2988 Stub_table<size, big_endian>* ifunc_stub_table
2989 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2990 Stub_table<size, big_endian>* one_stub_table
2991 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2992 for (typename Branches::const_iterator b = this->branch_info_.begin();
2993 b != this->branch_info_.end();
2994 b++)
2995 {
2996 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
2997 && !this->relax_failed_)
2998 {
2999 this->relax_failed_ = true;
3000 this->relax_fail_count_++;
3001 if (this->relax_fail_count_ < 3)
3002 return true;
3003 }
3004 }
3005
3006 // Did anything change size?
3007 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3008 bool again = num_huge_branches != prev_brlt_size;
3009 if (size == 64 && num_huge_branches != 0)
3010 this->make_brlt_section(layout);
3011 if (size == 64 && again)
3012 this->brlt_section_->set_current_size(num_huge_branches);
3013
3014 typedef Unordered_set<Output_section*> Output_sections;
3015 Output_sections os_need_update;
3016 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3017 p != this->stub_tables_.end();
3018 ++p)
3019 {
3020 if ((*p)->size_update())
3021 {
3022 again = true;
3023 (*p)->add_eh_frame(layout);
3024 os_need_update.insert((*p)->output_section());
3025 }
3026 }
3027
3028 // Set output section offsets for all input sections in an output
3029 // section that just changed size. Anything past the stubs will
3030 // need updating.
3031 for (typename Output_sections::iterator p = os_need_update.begin();
3032 p != os_need_update.end();
3033 p++)
3034 {
3035 Output_section* os = *p;
3036 Address off = 0;
3037 typedef Output_section::Input_section_list Input_section_list;
3038 for (Input_section_list::const_iterator i = os->input_sections().begin();
3039 i != os->input_sections().end();
3040 ++i)
3041 {
3042 off = align_address(off, i->addralign());
3043 if (i->is_input_section() || i->is_relaxed_input_section())
3044 i->relobj()->set_section_offset(i->shndx(), off);
3045 if (i->is_relaxed_input_section())
3046 {
3047 Stub_table<size, big_endian>* stub_table
3048 = static_cast<Stub_table<size, big_endian>*>(
3049 i->relaxed_input_section());
3050 Address stub_table_size = stub_table->set_address_and_size(os, off);
3051 off += stub_table_size;
3052 // After a few iterations, set current stub table size
3053 // as min size threshold, so later stub tables can only
3054 // grow in size.
3055 if (pass >= 4)
3056 stub_table->set_min_size_threshold(stub_table_size);
3057 }
3058 else
3059 off += i->data_size();
3060 }
3061 // If .branch_lt is part of this output section, then we have
3062 // just done the offset adjustment.
3063 os->clear_section_offsets_need_adjustment();
3064 }
3065
3066 if (size == 64
3067 && !again
3068 && num_huge_branches != 0
3069 && parameters->options().output_is_position_independent())
3070 {
3071 // Fill in the BRLT relocs.
3072 this->brlt_section_->reset_brlt_sizes();
3073 for (typename Branch_lookup_table::const_iterator p
3074 = this->branch_lookup_table_.begin();
3075 p != this->branch_lookup_table_.end();
3076 ++p)
3077 {
3078 this->brlt_section_->add_reloc(p->first, p->second);
3079 }
3080 this->brlt_section_->finalize_brlt_sizes();
3081 }
3082 return again;
3083 }
3084
3085 template<int size, bool big_endian>
3086 void
3087 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3088 unsigned char* oview,
3089 uint64_t* paddress,
3090 off_t* plen) const
3091 {
3092 uint64_t address = plt->address();
3093 off_t len = plt->data_size();
3094
3095 if (plt == this->glink_)
3096 {
3097 // See Output_data_glink::do_write() for glink contents.
3098 if (len == 0)
3099 {
3100 gold_assert(parameters->doing_static_link());
3101 // Static linking may need stubs, to support ifunc and long
3102 // branches. We need to create an output section for
3103 // .eh_frame early in the link process, to have a place to
3104 // attach stub .eh_frame info. We also need to have
3105 // registered a CIE that matches the stub CIE. Both of
3106 // these requirements are satisfied by creating an FDE and
3107 // CIE for .glink, even though static linking will leave
3108 // .glink zero length.
3109 // ??? Hopefully generating an FDE with a zero address range
3110 // won't confuse anything that consumes .eh_frame info.
3111 }
3112 else if (size == 64)
3113 {
3114 // There is one word before __glink_PLTresolve
3115 address += 8;
3116 len -= 8;
3117 }
3118 else if (parameters->options().output_is_position_independent())
3119 {
3120 // There are two FDEs for a position independent glink.
3121 // The first covers the branch table, the second
3122 // __glink_PLTresolve at the end of glink.
3123 off_t resolve_size = this->glink_->pltresolve_size;
3124 if (oview[9] == elfcpp::DW_CFA_nop)
3125 len -= resolve_size;
3126 else
3127 {
3128 address += len - resolve_size;
3129 len = resolve_size;
3130 }
3131 }
3132 }
3133 else
3134 {
3135 // Must be a stub table.
3136 const Stub_table<size, big_endian>* stub_table
3137 = static_cast<const Stub_table<size, big_endian>*>(plt);
3138 uint64_t stub_address = stub_table->stub_address();
3139 len -= stub_address - address;
3140 address = stub_address;
3141 }
3142
3143 *paddress = address;
3144 *plen = len;
3145 }
3146
3147 // A class to handle the PLT data.
3148
3149 template<int size, bool big_endian>
3150 class Output_data_plt_powerpc : public Output_section_data_build
3151 {
3152 public:
3153 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3154 size, big_endian> Reloc_section;
3155
3156 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3157 Reloc_section* plt_rel,
3158 const char* name)
3159 : Output_section_data_build(size == 32 ? 4 : 8),
3160 rel_(plt_rel),
3161 targ_(targ),
3162 name_(name)
3163 { }
3164
3165 // Add an entry to the PLT.
3166 void
3167 add_entry(Symbol*);
3168
3169 void
3170 add_ifunc_entry(Symbol*);
3171
3172 void
3173 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3174
3175 // Return the .rela.plt section data.
3176 Reloc_section*
3177 rel_plt() const
3178 {
3179 return this->rel_;
3180 }
3181
3182 // Return the number of PLT entries.
3183 unsigned int
3184 entry_count() const
3185 {
3186 if (this->current_data_size() == 0)
3187 return 0;
3188 return ((this->current_data_size() - this->first_plt_entry_offset())
3189 / this->plt_entry_size());
3190 }
3191
3192 protected:
3193 void
3194 do_adjust_output_section(Output_section* os)
3195 {
3196 os->set_entsize(0);
3197 }
3198
3199 // Write to a map file.
3200 void
3201 do_print_to_mapfile(Mapfile* mapfile) const
3202 { mapfile->print_output_data(this, this->name_); }
3203
3204 private:
3205 // Return the offset of the first non-reserved PLT entry.
3206 unsigned int
3207 first_plt_entry_offset() const
3208 {
3209 // IPLT has no reserved entry.
3210 if (this->name_[3] == 'I')
3211 return 0;
3212 return this->targ_->first_plt_entry_offset();
3213 }
3214
3215 // Return the size of each PLT entry.
3216 unsigned int
3217 plt_entry_size() const
3218 {
3219 return this->targ_->plt_entry_size();
3220 }
3221
3222 // Write out the PLT data.
3223 void
3224 do_write(Output_file*);
3225
3226 // The reloc section.
3227 Reloc_section* rel_;
3228 // Allows access to .glink for do_write.
3229 Target_powerpc<size, big_endian>* targ_;
3230 // What to report in map file.
3231 const char *name_;
3232 };
3233
3234 // Add an entry to the PLT.
3235
3236 template<int size, bool big_endian>
3237 void
3238 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3239 {
3240 if (!gsym->has_plt_offset())
3241 {
3242 section_size_type off = this->current_data_size();
3243 if (off == 0)
3244 off += this->first_plt_entry_offset();
3245 gsym->set_plt_offset(off);
3246 gsym->set_needs_dynsym_entry();
3247 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3248 this->rel_->add_global(gsym, dynrel, this, off, 0);
3249 off += this->plt_entry_size();
3250 this->set_current_data_size(off);
3251 }
3252 }
3253
3254 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3255
3256 template<int size, bool big_endian>
3257 void
3258 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3259 {
3260 if (!gsym->has_plt_offset())
3261 {
3262 section_size_type off = this->current_data_size();
3263 gsym->set_plt_offset(off);
3264 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3265 if (size == 64 && this->targ_->abiversion() < 2)
3266 dynrel = elfcpp::R_PPC64_JMP_IREL;
3267 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3268 off += this->plt_entry_size();
3269 this->set_current_data_size(off);
3270 }
3271 }
3272
3273 // Add an entry for a local ifunc symbol to the IPLT.
3274
3275 template<int size, bool big_endian>
3276 void
3277 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3278 Sized_relobj_file<size, big_endian>* relobj,
3279 unsigned int local_sym_index)
3280 {
3281 if (!relobj->local_has_plt_offset(local_sym_index))
3282 {
3283 section_size_type off = this->current_data_size();
3284 relobj->set_local_plt_offset(local_sym_index, off);
3285 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3286 if (size == 64 && this->targ_->abiversion() < 2)
3287 dynrel = elfcpp::R_PPC64_JMP_IREL;
3288 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3289 this, off, 0);
3290 off += this->plt_entry_size();
3291 this->set_current_data_size(off);
3292 }
3293 }
3294
3295 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3296 static const uint32_t add_2_2_11 = 0x7c425a14;
3297 static const uint32_t add_2_2_12 = 0x7c426214;
3298 static const uint32_t add_3_3_2 = 0x7c631214;
3299 static const uint32_t add_3_3_13 = 0x7c636a14;
3300 static const uint32_t add_11_0_11 = 0x7d605a14;
3301 static const uint32_t add_11_2_11 = 0x7d625a14;
3302 static const uint32_t add_11_11_2 = 0x7d6b1214;
3303 static const uint32_t addi_0_12 = 0x380c0000;
3304 static const uint32_t addi_2_2 = 0x38420000;
3305 static const uint32_t addi_3_3 = 0x38630000;
3306 static const uint32_t addi_11_11 = 0x396b0000;
3307 static const uint32_t addi_12_1 = 0x39810000;
3308 static const uint32_t addi_12_12 = 0x398c0000;
3309 static const uint32_t addis_0_2 = 0x3c020000;
3310 static const uint32_t addis_0_13 = 0x3c0d0000;
3311 static const uint32_t addis_2_12 = 0x3c4c0000;
3312 static const uint32_t addis_11_2 = 0x3d620000;
3313 static const uint32_t addis_11_11 = 0x3d6b0000;
3314 static const uint32_t addis_11_30 = 0x3d7e0000;
3315 static const uint32_t addis_12_1 = 0x3d810000;
3316 static const uint32_t addis_12_2 = 0x3d820000;
3317 static const uint32_t addis_12_12 = 0x3d8c0000;
3318 static const uint32_t b = 0x48000000;
3319 static const uint32_t bcl_20_31 = 0x429f0005;
3320 static const uint32_t bctr = 0x4e800420;
3321 static const uint32_t blr = 0x4e800020;
3322 static const uint32_t bnectr_p4 = 0x4ce20420;
3323 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3324 static const uint32_t cmpldi_2_0 = 0x28220000;
3325 static const uint32_t cror_15_15_15 = 0x4def7b82;
3326 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3327 static const uint32_t ld_0_1 = 0xe8010000;
3328 static const uint32_t ld_0_12 = 0xe80c0000;
3329 static const uint32_t ld_2_1 = 0xe8410000;
3330 static const uint32_t ld_2_2 = 0xe8420000;
3331 static const uint32_t ld_2_11 = 0xe84b0000;
3332 static const uint32_t ld_2_12 = 0xe84c0000;
3333 static const uint32_t ld_11_2 = 0xe9620000;
3334 static const uint32_t ld_11_11 = 0xe96b0000;
3335 static const uint32_t ld_12_2 = 0xe9820000;
3336 static const uint32_t ld_12_11 = 0xe98b0000;
3337 static const uint32_t ld_12_12 = 0xe98c0000;
3338 static const uint32_t lfd_0_1 = 0xc8010000;
3339 static const uint32_t li_0_0 = 0x38000000;
3340 static const uint32_t li_12_0 = 0x39800000;
3341 static const uint32_t lis_0 = 0x3c000000;
3342 static const uint32_t lis_2 = 0x3c400000;
3343 static const uint32_t lis_11 = 0x3d600000;
3344 static const uint32_t lis_12 = 0x3d800000;
3345 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3346 static const uint32_t lwz_0_12 = 0x800c0000;
3347 static const uint32_t lwz_11_11 = 0x816b0000;
3348 static const uint32_t lwz_11_30 = 0x817e0000;
3349 static const uint32_t lwz_12_12 = 0x818c0000;
3350 static const uint32_t lwzu_0_12 = 0x840c0000;
3351 static const uint32_t mflr_0 = 0x7c0802a6;
3352 static const uint32_t mflr_11 = 0x7d6802a6;
3353 static const uint32_t mflr_12 = 0x7d8802a6;
3354 static const uint32_t mtctr_0 = 0x7c0903a6;
3355 static const uint32_t mtctr_11 = 0x7d6903a6;
3356 static const uint32_t mtctr_12 = 0x7d8903a6;
3357 static const uint32_t mtlr_0 = 0x7c0803a6;
3358 static const uint32_t mtlr_12 = 0x7d8803a6;
3359 static const uint32_t nop = 0x60000000;
3360 static const uint32_t ori_0_0_0 = 0x60000000;
3361 static const uint32_t srdi_0_0_2 = 0x7800f082;
3362 static const uint32_t std_0_1 = 0xf8010000;
3363 static const uint32_t std_0_12 = 0xf80c0000;
3364 static const uint32_t std_2_1 = 0xf8410000;
3365 static const uint32_t stfd_0_1 = 0xd8010000;
3366 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3367 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3368 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3369 static const uint32_t xor_2_12_12 = 0x7d826278;
3370 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3371
3372 // Write out the PLT.
3373
3374 template<int size, bool big_endian>
3375 void
3376 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3377 {
3378 if (size == 32 && this->name_[3] != 'I')
3379 {
3380 const section_size_type offset = this->offset();
3381 const section_size_type oview_size
3382 = convert_to_section_size_type(this->data_size());
3383 unsigned char* const oview = of->get_output_view(offset, oview_size);
3384 unsigned char* pov = oview;
3385 unsigned char* endpov = oview + oview_size;
3386
3387 // The address of the .glink branch table
3388 const Output_data_glink<size, big_endian>* glink
3389 = this->targ_->glink_section();
3390 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3391
3392 while (pov < endpov)
3393 {
3394 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3395 pov += 4;
3396 branch_tab += 4;
3397 }
3398
3399 of->write_output_view(offset, oview_size, oview);
3400 }
3401 }
3402
3403 // Create the PLT section.
3404
3405 template<int size, bool big_endian>
3406 void
3407 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3408 Layout* layout)
3409 {
3410 if (this->plt_ == NULL)
3411 {
3412 if (this->got_ == NULL)
3413 this->got_section(symtab, layout);
3414
3415 if (this->glink_ == NULL)
3416 make_glink_section(layout);
3417
3418 // Ensure that .rela.dyn always appears before .rela.plt This is
3419 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3420 // needs to include .rela.plt in its range.
3421 this->rela_dyn_section(layout);
3422
3423 Reloc_section* plt_rel = new Reloc_section(false);
3424 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3425 elfcpp::SHF_ALLOC, plt_rel,
3426 ORDER_DYNAMIC_PLT_RELOCS, false);
3427 this->plt_
3428 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3429 "** PLT");
3430 layout->add_output_section_data(".plt",
3431 (size == 32
3432 ? elfcpp::SHT_PROGBITS
3433 : elfcpp::SHT_NOBITS),
3434 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3435 this->plt_,
3436 (size == 32
3437 ? ORDER_SMALL_DATA
3438 : ORDER_SMALL_BSS),
3439 false);
3440 }
3441 }
3442
3443 // Create the IPLT section.
3444
3445 template<int size, bool big_endian>
3446 void
3447 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3448 Layout* layout)
3449 {
3450 if (this->iplt_ == NULL)
3451 {
3452 this->make_plt_section(symtab, layout);
3453
3454 Reloc_section* iplt_rel = new Reloc_section(false);
3455 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3456 this->iplt_
3457 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3458 "** IPLT");
3459 this->plt_->output_section()->add_output_section_data(this->iplt_);
3460 }
3461 }
3462
3463 // A section for huge long branch addresses, similar to plt section.
3464
3465 template<int size, bool big_endian>
3466 class Output_data_brlt_powerpc : public Output_section_data_build
3467 {
3468 public:
3469 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3470 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3471 size, big_endian> Reloc_section;
3472
3473 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3474 Reloc_section* brlt_rel)
3475 : Output_section_data_build(size == 32 ? 4 : 8),
3476 rel_(brlt_rel),
3477 targ_(targ)
3478 { }
3479
3480 void
3481 reset_brlt_sizes()
3482 {
3483 this->reset_data_size();
3484 this->rel_->reset_data_size();
3485 }
3486
3487 void
3488 finalize_brlt_sizes()
3489 {
3490 this->finalize_data_size();
3491 this->rel_->finalize_data_size();
3492 }
3493
3494 // Add a reloc for an entry in the BRLT.
3495 void
3496 add_reloc(Address to, unsigned int off)
3497 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3498
3499 // Update section and reloc section size.
3500 void
3501 set_current_size(unsigned int num_branches)
3502 {
3503 this->reset_address_and_file_offset();
3504 this->set_current_data_size(num_branches * 16);
3505 this->finalize_data_size();
3506 Output_section* os = this->output_section();
3507 os->set_section_offsets_need_adjustment();
3508 if (this->rel_ != NULL)
3509 {
3510 unsigned int reloc_size
3511 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3512 this->rel_->reset_address_and_file_offset();
3513 this->rel_->set_current_data_size(num_branches * reloc_size);
3514 this->rel_->finalize_data_size();
3515 Output_section* os = this->rel_->output_section();
3516 os->set_section_offsets_need_adjustment();
3517 }
3518 }
3519
3520 protected:
3521 void
3522 do_adjust_output_section(Output_section* os)
3523 {
3524 os->set_entsize(0);
3525 }
3526
3527 // Write to a map file.
3528 void
3529 do_print_to_mapfile(Mapfile* mapfile) const
3530 { mapfile->print_output_data(this, "** BRLT"); }
3531
3532 private:
3533 // Write out the BRLT data.
3534 void
3535 do_write(Output_file*);
3536
3537 // The reloc section.
3538 Reloc_section* rel_;
3539 Target_powerpc<size, big_endian>* targ_;
3540 };
3541
3542 // Make the branch lookup table section.
3543
3544 template<int size, bool big_endian>
3545 void
3546 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3547 {
3548 if (size == 64 && this->brlt_section_ == NULL)
3549 {
3550 Reloc_section* brlt_rel = NULL;
3551 bool is_pic = parameters->options().output_is_position_independent();
3552 if (is_pic)
3553 {
3554 // When PIC we can't fill in .branch_lt (like .plt it can be
3555 // a bss style section) but must initialise at runtime via
3556 // dynamic relocats.
3557 this->rela_dyn_section(layout);
3558 brlt_rel = new Reloc_section(false);
3559 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3560 }
3561 this->brlt_section_
3562 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3563 if (this->plt_ && is_pic)
3564 this->plt_->output_section()
3565 ->add_output_section_data(this->brlt_section_);
3566 else
3567 layout->add_output_section_data(".branch_lt",
3568 (is_pic ? elfcpp::SHT_NOBITS
3569 : elfcpp::SHT_PROGBITS),
3570 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3571 this->brlt_section_,
3572 (is_pic ? ORDER_SMALL_BSS
3573 : ORDER_SMALL_DATA),
3574 false);
3575 }
3576 }
3577
3578 // Write out .branch_lt when non-PIC.
3579
3580 template<int size, bool big_endian>
3581 void
3582 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3583 {
3584 if (size == 64 && !parameters->options().output_is_position_independent())
3585 {
3586 const section_size_type offset = this->offset();
3587 const section_size_type oview_size
3588 = convert_to_section_size_type(this->data_size());
3589 unsigned char* const oview = of->get_output_view(offset, oview_size);
3590
3591 this->targ_->write_branch_lookup_table(oview);
3592 of->write_output_view(offset, oview_size, oview);
3593 }
3594 }
3595
3596 static inline uint32_t
3597 l(uint32_t a)
3598 {
3599 return a & 0xffff;
3600 }
3601
3602 static inline uint32_t
3603 hi(uint32_t a)
3604 {
3605 return l(a >> 16);
3606 }
3607
3608 static inline uint32_t
3609 ha(uint32_t a)
3610 {
3611 return hi(a + 0x8000);
3612 }
3613
3614 template<int size>
3615 struct Eh_cie
3616 {
3617 static const unsigned char eh_frame_cie[12];
3618 };
3619
3620 template<int size>
3621 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3622 {
3623 1, // CIE version.
3624 'z', 'R', 0, // Augmentation string.
3625 4, // Code alignment.
3626 0x80 - size / 8 , // Data alignment.
3627 65, // RA reg.
3628 1, // Augmentation size.
3629 (elfcpp::DW_EH_PE_pcrel
3630 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3631 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3632 };
3633
3634 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3635 static const unsigned char glink_eh_frame_fde_64v1[] =
3636 {
3637 0, 0, 0, 0, // Replaced with offset to .glink.
3638 0, 0, 0, 0, // Replaced with size of .glink.
3639 0, // Augmentation size.
3640 elfcpp::DW_CFA_advance_loc + 1,
3641 elfcpp::DW_CFA_register, 65, 12,
3642 elfcpp::DW_CFA_advance_loc + 4,
3643 elfcpp::DW_CFA_restore_extended, 65
3644 };
3645
3646 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3647 static const unsigned char glink_eh_frame_fde_64v2[] =
3648 {
3649 0, 0, 0, 0, // Replaced with offset to .glink.
3650 0, 0, 0, 0, // Replaced with size of .glink.
3651 0, // Augmentation size.
3652 elfcpp::DW_CFA_advance_loc + 1,
3653 elfcpp::DW_CFA_register, 65, 0,
3654 elfcpp::DW_CFA_advance_loc + 4,
3655 elfcpp::DW_CFA_restore_extended, 65
3656 };
3657
3658 // Describe __glink_PLTresolve use of LR, 32-bit version.
3659 static const unsigned char glink_eh_frame_fde_32[] =
3660 {
3661 0, 0, 0, 0, // Replaced with offset to .glink.
3662 0, 0, 0, 0, // Replaced with size of .glink.
3663 0, // Augmentation size.
3664 elfcpp::DW_CFA_advance_loc + 2,
3665 elfcpp::DW_CFA_register, 65, 0,
3666 elfcpp::DW_CFA_advance_loc + 4,
3667 elfcpp::DW_CFA_restore_extended, 65
3668 };
3669
3670 static const unsigned char default_fde[] =
3671 {
3672 0, 0, 0, 0, // Replaced with offset to stubs.
3673 0, 0, 0, 0, // Replaced with size of stubs.
3674 0, // Augmentation size.
3675 elfcpp::DW_CFA_nop, // Pad.
3676 elfcpp::DW_CFA_nop,
3677 elfcpp::DW_CFA_nop
3678 };
3679
3680 template<bool big_endian>
3681 static inline void
3682 write_insn(unsigned char* p, uint32_t v)
3683 {
3684 elfcpp::Swap<32, big_endian>::writeval(p, v);
3685 }
3686
3687 // Stub_table holds information about plt and long branch stubs.
3688 // Stubs are built in an area following some input section determined
3689 // by group_sections(). This input section is converted to a relaxed
3690 // input section allowing it to be resized to accommodate the stubs
3691
3692 template<int size, bool big_endian>
3693 class Stub_table : public Output_relaxed_input_section
3694 {
3695 public:
3696 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3697 static const Address invalid_address = static_cast<Address>(0) - 1;
3698
3699 Stub_table(Target_powerpc<size, big_endian>* targ,
3700 Output_section* output_section,
3701 const Output_section::Input_section* owner)
3702 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
3703 owner->relobj()
3704 ->section_addralign(owner->shndx())),
3705 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3706 orig_data_size_(owner->current_data_size()),
3707 plt_size_(0), last_plt_size_(0),
3708 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
3709 eh_frame_added_(false), need_save_res_(false)
3710 {
3711 this->set_output_section(output_section);
3712
3713 std::vector<Output_relaxed_input_section*> new_relaxed;
3714 new_relaxed.push_back(this);
3715 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3716 }
3717
3718 // Add a plt call stub.
3719 bool
3720 add_plt_call_entry(Address,
3721 const Sized_relobj_file<size, big_endian>*,
3722 const Symbol*,
3723 unsigned int,
3724 Address);
3725
3726 bool
3727 add_plt_call_entry(Address,
3728 const Sized_relobj_file<size, big_endian>*,
3729 unsigned int,
3730 unsigned int,
3731 Address);
3732
3733 // Find a given plt call stub.
3734 Address
3735 find_plt_call_entry(const Symbol*) const;
3736
3737 Address
3738 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3739 unsigned int) const;
3740
3741 Address
3742 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3743 const Symbol*,
3744 unsigned int,
3745 Address) const;
3746
3747 Address
3748 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3749 unsigned int,
3750 unsigned int,
3751 Address) const;
3752
3753 // Add a long branch stub.
3754 bool
3755 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3756 unsigned int, Address, Address, bool);
3757
3758 Address
3759 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3760 Address) const;
3761
3762 bool
3763 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
3764 {
3765 Address max_branch_offset = max_branch_delta(r_type);
3766 if (max_branch_offset == 0)
3767 return true;
3768 gold_assert(from != invalid_address);
3769 Address loc = off + this->stub_address();
3770 return loc - from + max_branch_offset < 2 * max_branch_offset;
3771 }
3772
3773 void
3774 clear_stubs(bool all)
3775 {
3776 this->plt_call_stubs_.clear();
3777 this->plt_size_ = 0;
3778 this->long_branch_stubs_.clear();
3779 this->branch_size_ = 0;
3780 this->need_save_res_ = false;
3781 if (all)
3782 {
3783 this->last_plt_size_ = 0;
3784 this->last_branch_size_ = 0;
3785 }
3786 }
3787
3788 Address
3789 set_address_and_size(const Output_section* os, Address off)
3790 {
3791 Address start_off = off;
3792 off += this->orig_data_size_;
3793 Address my_size = this->plt_size_ + this->branch_size_;
3794 if (this->need_save_res_)
3795 my_size += this->targ_->savres_section()->data_size();
3796 if (my_size != 0)
3797 off = align_address(off, this->stub_align());
3798 // Include original section size and alignment padding in size
3799 my_size += off - start_off;
3800 // Ensure new size is always larger than min size
3801 // threshold. Alignment requirement is included in "my_size", so
3802 // increase "my_size" does not invalidate alignment.
3803 if (my_size < this->min_size_threshold_)
3804 my_size = this->min_size_threshold_;
3805 this->reset_address_and_file_offset();
3806 this->set_current_data_size(my_size);
3807 this->set_address_and_file_offset(os->address() + start_off,
3808 os->offset() + start_off);
3809 return my_size;
3810 }
3811
3812 Address
3813 stub_address() const
3814 {
3815 return align_address(this->address() + this->orig_data_size_,
3816 this->stub_align());
3817 }
3818
3819 Address
3820 stub_offset() const
3821 {
3822 return align_address(this->offset() + this->orig_data_size_,
3823 this->stub_align());
3824 }
3825
3826 section_size_type
3827 plt_size() const
3828 { return this->plt_size_; }
3829
3830 void set_min_size_threshold(Address min_size)
3831 { this->min_size_threshold_ = min_size; }
3832
3833 bool
3834 size_update()
3835 {
3836 Output_section* os = this->output_section();
3837 if (os->addralign() < this->stub_align())
3838 {
3839 os->set_addralign(this->stub_align());
3840 // FIXME: get rid of the insane checkpointing.
3841 // We can't increase alignment of the input section to which
3842 // stubs are attached; The input section may be .init which
3843 // is pasted together with other .init sections to form a
3844 // function. Aligning might insert zero padding resulting in
3845 // sigill. However we do need to increase alignment of the
3846 // output section so that the align_address() on offset in
3847 // set_address_and_size() adds the same padding as the
3848 // align_address() on address in stub_address().
3849 // What's more, we need this alignment for the layout done in
3850 // relaxation_loop_body() so that the output section starts at
3851 // a suitably aligned address.
3852 os->checkpoint_set_addralign(this->stub_align());
3853 }
3854 if (this->last_plt_size_ != this->plt_size_
3855 || this->last_branch_size_ != this->branch_size_)
3856 {
3857 this->last_plt_size_ = this->plt_size_;
3858 this->last_branch_size_ = this->branch_size_;
3859 return true;
3860 }
3861 return false;
3862 }
3863
3864 // Add .eh_frame info for this stub section. Unlike other linker
3865 // generated .eh_frame this is added late in the link, because we
3866 // only want the .eh_frame info if this particular stub section is
3867 // non-empty.
3868 void
3869 add_eh_frame(Layout* layout)
3870 {
3871 if (!this->eh_frame_added_)
3872 {
3873 if (!parameters->options().ld_generated_unwind_info())
3874 return;
3875
3876 // Since we add stub .eh_frame info late, it must be placed
3877 // after all other linker generated .eh_frame info so that
3878 // merge mapping need not be updated for input sections.
3879 // There is no provision to use a different CIE to that used
3880 // by .glink.
3881 if (!this->targ_->has_glink())
3882 return;
3883
3884 layout->add_eh_frame_for_plt(this,
3885 Eh_cie<size>::eh_frame_cie,
3886 sizeof (Eh_cie<size>::eh_frame_cie),
3887 default_fde,
3888 sizeof (default_fde));
3889 this->eh_frame_added_ = true;
3890 }
3891 }
3892
3893 Target_powerpc<size, big_endian>*
3894 targ() const
3895 { return targ_; }
3896
3897 private:
3898 class Plt_stub_ent;
3899 class Plt_stub_ent_hash;
3900 typedef Unordered_map<Plt_stub_ent, unsigned int,
3901 Plt_stub_ent_hash> Plt_stub_entries;
3902
3903 // Alignment of stub section.
3904 unsigned int
3905 stub_align() const
3906 {
3907 if (size == 32)
3908 return 16;
3909 unsigned int min_align = 32;
3910 unsigned int user_align = 1 << parameters->options().plt_align();
3911 return std::max(user_align, min_align);
3912 }
3913
3914 // Return the plt offset for the given call stub.
3915 Address
3916 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3917 {
3918 const Symbol* gsym = p->first.sym_;
3919 if (gsym != NULL)
3920 {
3921 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3922 && gsym->can_use_relative_reloc(false));
3923 return gsym->plt_offset();
3924 }
3925 else
3926 {
3927 *is_iplt = true;
3928 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3929 unsigned int local_sym_index = p->first.locsym_;
3930 return relobj->local_plt_offset(local_sym_index);
3931 }
3932 }
3933
3934 // Size of a given plt call stub.
3935 unsigned int
3936 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3937 {
3938 if (size == 32)
3939 return 16;
3940
3941 bool is_iplt;
3942 Address plt_addr = this->plt_off(p, &is_iplt);
3943 if (is_iplt)
3944 plt_addr += this->targ_->iplt_section()->address();
3945 else
3946 plt_addr += this->targ_->plt_section()->address();
3947 Address got_addr = this->targ_->got_section()->output_section()->address();
3948 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3949 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3950 got_addr += ppcobj->toc_base_offset();
3951 Address off = plt_addr - got_addr;
3952 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
3953 if (this->targ_->abiversion() < 2)
3954 {
3955 bool static_chain = parameters->options().plt_static_chain();
3956 bool thread_safe = this->targ_->plt_thread_safe();
3957 bytes += (4
3958 + 4 * static_chain
3959 + 8 * thread_safe
3960 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3961 }
3962 unsigned int align = 1 << parameters->options().plt_align();
3963 if (align > 1)
3964 bytes = (bytes + align - 1) & -align;
3965 return bytes;
3966 }
3967
3968 // Return long branch stub size.
3969 unsigned int
3970 branch_stub_size(Address to)
3971 {
3972 Address loc
3973 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3974 if (to - loc + (1 << 25) < 2 << 25)
3975 return 4;
3976 if (size == 64 || !parameters->options().output_is_position_independent())
3977 return 16;
3978 return 32;
3979 }
3980
3981 // Write out stubs.
3982 void
3983 do_write(Output_file*);
3984
3985 // Plt call stub keys.
3986 class Plt_stub_ent
3987 {
3988 public:
3989 Plt_stub_ent(const Symbol* sym)
3990 : sym_(sym), object_(0), addend_(0), locsym_(0)
3991 { }
3992
3993 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3994 unsigned int locsym_index)
3995 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3996 { }
3997
3998 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3999 const Symbol* sym,
4000 unsigned int r_type,
4001 Address addend)
4002 : sym_(sym), object_(0), addend_(0), locsym_(0)
4003 {
4004 if (size != 32)
4005 this->addend_ = addend;
4006 else if (parameters->options().output_is_position_independent()
4007 && r_type == elfcpp::R_PPC_PLTREL24)
4008 {
4009 this->addend_ = addend;
4010 if (this->addend_ >= 32768)
4011 this->object_ = object;
4012 }
4013 }
4014
4015 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
4016 unsigned int locsym_index,
4017 unsigned int r_type,
4018 Address addend)
4019 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4020 {
4021 if (size != 32)
4022 this->addend_ = addend;
4023 else if (parameters->options().output_is_position_independent()
4024 && r_type == elfcpp::R_PPC_PLTREL24)
4025 this->addend_ = addend;
4026 }
4027
4028 bool operator==(const Plt_stub_ent& that) const
4029 {
4030 return (this->sym_ == that.sym_
4031 && this->object_ == that.object_
4032 && this->addend_ == that.addend_
4033 && this->locsym_ == that.locsym_);
4034 }
4035
4036 const Symbol* sym_;
4037 const Sized_relobj_file<size, big_endian>* object_;
4038 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4039 unsigned int locsym_;
4040 };
4041
4042 class Plt_stub_ent_hash
4043 {
4044 public:
4045 size_t operator()(const Plt_stub_ent& ent) const
4046 {
4047 return (reinterpret_cast<uintptr_t>(ent.sym_)
4048 ^ reinterpret_cast<uintptr_t>(ent.object_)
4049 ^ ent.addend_
4050 ^ ent.locsym_);
4051 }
4052 };
4053
4054 // Long branch stub keys.
4055 class Branch_stub_ent
4056 {
4057 public:
4058 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
4059 Address to, bool save_res)
4060 : dest_(to), toc_base_off_(0), save_res_(save_res)
4061 {
4062 if (size == 64)
4063 toc_base_off_ = obj->toc_base_offset();
4064 }
4065
4066 bool operator==(const Branch_stub_ent& that) const
4067 {
4068 return (this->dest_ == that.dest_
4069 && (size == 32
4070 || this->toc_base_off_ == that.toc_base_off_));
4071 }
4072
4073 Address dest_;
4074 unsigned int toc_base_off_;
4075 bool save_res_;
4076 };
4077
4078 class Branch_stub_ent_hash
4079 {
4080 public:
4081 size_t operator()(const Branch_stub_ent& ent) const
4082 { return ent.dest_ ^ ent.toc_base_off_; }
4083 };
4084
4085 // In a sane world this would be a global.
4086 Target_powerpc<size, big_endian>* targ_;
4087 // Map sym/object/addend to stub offset.
4088 Plt_stub_entries plt_call_stubs_;
4089 // Map destination address to stub offset.
4090 typedef Unordered_map<Branch_stub_ent, unsigned int,
4091 Branch_stub_ent_hash> Branch_stub_entries;
4092 Branch_stub_entries long_branch_stubs_;
4093 // size of input section
4094 section_size_type orig_data_size_;
4095 // size of stubs
4096 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
4097 // Some rare cases cause (PR/20529) fluctuation in stub table
4098 // size, which leads to an endless relax loop. This is to be fixed
4099 // by, after the first few iterations, allowing only increase of
4100 // stub table size. This variable sets the minimal possible size of
4101 // a stub table, it is zero for the first few iterations, then
4102 // increases monotonically.
4103 Address min_size_threshold_;
4104 // Whether .eh_frame info has been created for this stub section.
4105 bool eh_frame_added_;
4106 // Set if this stub group needs a copy of out-of-line register
4107 // save/restore functions.
4108 bool need_save_res_;
4109 };
4110
4111 // Add a plt call stub, if we do not already have one for this
4112 // sym/object/addend combo.
4113
4114 template<int size, bool big_endian>
4115 bool
4116 Stub_table<size, big_endian>::add_plt_call_entry(
4117 Address from,
4118 const Sized_relobj_file<size, big_endian>* object,
4119 const Symbol* gsym,
4120 unsigned int r_type,
4121 Address addend)
4122 {
4123 Plt_stub_ent ent(object, gsym, r_type, addend);
4124 unsigned int off = this->plt_size_;
4125 std::pair<typename Plt_stub_entries::iterator, bool> p
4126 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
4127 if (p.second)
4128 this->plt_size_ = off + this->plt_call_size(p.first);
4129 return this->can_reach_stub(from, off, r_type);
4130 }
4131
4132 template<int size, bool big_endian>
4133 bool
4134 Stub_table<size, big_endian>::add_plt_call_entry(
4135 Address from,
4136 const Sized_relobj_file<size, big_endian>* object,
4137 unsigned int locsym_index,
4138 unsigned int r_type,
4139 Address addend)
4140 {
4141 Plt_stub_ent ent(object, locsym_index, r_type, addend);
4142 unsigned int off = this->plt_size_;
4143 std::pair<typename Plt_stub_entries::iterator, bool> p
4144 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
4145 if (p.second)
4146 this->plt_size_ = off + this->plt_call_size(p.first);
4147 return this->can_reach_stub(from, off, r_type);
4148 }
4149
4150 // Find a plt call stub.
4151
4152 template<int size, bool big_endian>
4153 typename Stub_table<size, big_endian>::Address
4154 Stub_table<size, big_endian>::find_plt_call_entry(
4155 const Sized_relobj_file<size, big_endian>* object,
4156 const Symbol* gsym,
4157 unsigned int r_type,
4158 Address addend) const
4159 {
4160 Plt_stub_ent ent(object, gsym, r_type, addend);
4161 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4162 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
4163 }
4164
4165 template<int size, bool big_endian>
4166 typename Stub_table<size, big_endian>::Address
4167 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
4168 {
4169 Plt_stub_ent ent(gsym);
4170 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4171 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
4172 }
4173
4174 template<int size, bool big_endian>
4175 typename Stub_table<size, big_endian>::Address
4176 Stub_table<size, big_endian>::find_plt_call_entry(
4177 const Sized_relobj_file<size, big_endian>* object,
4178 unsigned int locsym_index,
4179 unsigned int r_type,
4180 Address addend) const
4181 {
4182 Plt_stub_ent ent(object, locsym_index, r_type, addend);
4183 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4184 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
4185 }
4186
4187 template<int size, bool big_endian>
4188 typename Stub_table<size, big_endian>::Address
4189 Stub_table<size, big_endian>::find_plt_call_entry(
4190 const Sized_relobj_file<size, big_endian>* object,
4191 unsigned int locsym_index) const
4192 {
4193 Plt_stub_ent ent(object, locsym_index);
4194 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
4195 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
4196 }
4197
4198 // Add a long branch stub if we don't already have one to given
4199 // destination.
4200
4201 template<int size, bool big_endian>
4202 bool
4203 Stub_table<size, big_endian>::add_long_branch_entry(
4204 const Powerpc_relobj<size, big_endian>* object,
4205 unsigned int r_type,
4206 Address from,
4207 Address to,
4208 bool save_res)
4209 {
4210 Branch_stub_ent ent(object, to, save_res);
4211 Address off = this->branch_size_;
4212 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
4213 {
4214 if (save_res)
4215 this->need_save_res_ = true;
4216 else
4217 {
4218 unsigned int stub_size = this->branch_stub_size(to);
4219 this->branch_size_ = off + stub_size;
4220 if (size == 64 && stub_size != 4)
4221 this->targ_->add_branch_lookup_table(to);
4222 }
4223 }
4224 return this->can_reach_stub(from, off, r_type);
4225 }
4226
4227 // Find long branch stub offset.
4228
4229 template<int size, bool big_endian>
4230 typename Stub_table<size, big_endian>::Address
4231 Stub_table<size, big_endian>::find_long_branch_entry(
4232 const Powerpc_relobj<size, big_endian>* object,
4233 Address to) const
4234 {
4235 Branch_stub_ent ent(object, to, false);
4236 typename Branch_stub_entries::const_iterator p
4237 = this->long_branch_stubs_.find(ent);
4238 if (p == this->long_branch_stubs_.end())
4239 return invalid_address;
4240 if (p->first.save_res_)
4241 return to - this->targ_->savres_section()->address() + this->branch_size_;
4242 return p->second;
4243 }
4244
4245 // A class to handle .glink.
4246
4247 template<int size, bool big_endian>
4248 class Output_data_glink : public Output_section_data
4249 {
4250 public:
4251 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4252 static const Address invalid_address = static_cast<Address>(0) - 1;
4253 static const int pltresolve_size = 16*4;
4254
4255 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4256 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4257 end_branch_table_(), ge_size_(0)
4258 { }
4259
4260 void
4261 add_eh_frame(Layout* layout);
4262
4263 void
4264 add_global_entry(const Symbol*);
4265
4266 Address
4267 find_global_entry(const Symbol*) const;
4268
4269 Address
4270 global_entry_address() const
4271 {
4272 gold_assert(this->is_data_size_valid());
4273 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4274 return this->address() + global_entry_off;
4275 }
4276
4277 protected:
4278 // Write to a map file.
4279 void
4280 do_print_to_mapfile(Mapfile* mapfile) const
4281 { mapfile->print_output_data(this, _("** glink")); }
4282
4283 private:
4284 void
4285 set_final_data_size();
4286
4287 // Write out .glink
4288 void
4289 do_write(Output_file*);
4290
4291 // Allows access to .got and .plt for do_write.
4292 Target_powerpc<size, big_endian>* targ_;
4293
4294 // Map sym to stub offset.
4295 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4296 Global_entry_stub_entries global_entry_stubs_;
4297
4298 unsigned int end_branch_table_, ge_size_;
4299 };
4300
4301 template<int size, bool big_endian>
4302 void
4303 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4304 {
4305 if (!parameters->options().ld_generated_unwind_info())
4306 return;
4307
4308 if (size == 64)
4309 {
4310 if (this->targ_->abiversion() < 2)
4311 layout->add_eh_frame_for_plt(this,
4312 Eh_cie<64>::eh_frame_cie,
4313 sizeof (Eh_cie<64>::eh_frame_cie),
4314 glink_eh_frame_fde_64v1,
4315 sizeof (glink_eh_frame_fde_64v1));
4316 else
4317 layout->add_eh_frame_for_plt(this,
4318 Eh_cie<64>::eh_frame_cie,
4319 sizeof (Eh_cie<64>::eh_frame_cie),
4320 glink_eh_frame_fde_64v2,
4321 sizeof (glink_eh_frame_fde_64v2));
4322 }
4323 else
4324 {
4325 // 32-bit .glink can use the default since the CIE return
4326 // address reg, LR, is valid.
4327 layout->add_eh_frame_for_plt(this,
4328 Eh_cie<32>::eh_frame_cie,
4329 sizeof (Eh_cie<32>::eh_frame_cie),
4330 default_fde,
4331 sizeof (default_fde));
4332 // Except where LR is used in a PIC __glink_PLTresolve.
4333 if (parameters->options().output_is_position_independent())
4334 layout->add_eh_frame_for_plt(this,
4335 Eh_cie<32>::eh_frame_cie,
4336 sizeof (Eh_cie<32>::eh_frame_cie),
4337 glink_eh_frame_fde_32,
4338 sizeof (glink_eh_frame_fde_32));
4339 }
4340 }
4341
4342 template<int size, bool big_endian>
4343 void
4344 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4345 {
4346 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4347 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4348 if (p.second)
4349 this->ge_size_ += 16;
4350 }
4351
4352 template<int size, bool big_endian>
4353 typename Output_data_glink<size, big_endian>::Address
4354 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4355 {
4356 typename Global_entry_stub_entries::const_iterator p
4357 = this->global_entry_stubs_.find(gsym);
4358 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4359 }
4360
4361 template<int size, bool big_endian>
4362 void
4363 Output_data_glink<size, big_endian>::set_final_data_size()
4364 {
4365 unsigned int count = this->targ_->plt_entry_count();
4366 section_size_type total = 0;
4367
4368 if (count != 0)
4369 {
4370 if (size == 32)
4371 {
4372 // space for branch table
4373 total += 4 * (count - 1);
4374
4375 total += -total & 15;
4376 total += this->pltresolve_size;
4377 }
4378 else
4379 {
4380 total += this->pltresolve_size;
4381
4382 // space for branch table
4383 total += 4 * count;
4384 if (this->targ_->abiversion() < 2)
4385 {
4386 total += 4 * count;
4387 if (count > 0x8000)
4388 total += 4 * (count - 0x8000);
4389 }
4390 }
4391 }
4392 this->end_branch_table_ = total;
4393 total = (total + 15) & -16;
4394 total += this->ge_size_;
4395
4396 this->set_data_size(total);
4397 }
4398
4399 // Write out plt and long branch stub code.
4400
4401 template<int size, bool big_endian>
4402 void
4403 Stub_table<size, big_endian>::do_write(Output_file* of)
4404 {
4405 if (this->plt_call_stubs_.empty()
4406 && this->long_branch_stubs_.empty())
4407 return;
4408
4409 const section_size_type start_off = this->offset();
4410 const section_size_type off = this->stub_offset();
4411 const section_size_type oview_size =
4412 convert_to_section_size_type(this->data_size() - (off - start_off));
4413 unsigned char* const oview = of->get_output_view(off, oview_size);
4414 unsigned char* p;
4415
4416 if (size == 64)
4417 {
4418 const Output_data_got_powerpc<size, big_endian>* got
4419 = this->targ_->got_section();
4420 Address got_os_addr = got->output_section()->address();
4421
4422 if (!this->plt_call_stubs_.empty())
4423 {
4424 // The base address of the .plt section.
4425 Address plt_base = this->targ_->plt_section()->address();
4426 Address iplt_base = invalid_address;
4427
4428 // Write out plt call stubs.
4429 typename Plt_stub_entries::const_iterator cs;
4430 for (cs = this->plt_call_stubs_.begin();
4431 cs != this->plt_call_stubs_.end();
4432 ++cs)
4433 {
4434 bool is_iplt;
4435 Address pltoff = this->plt_off(cs, &is_iplt);
4436 Address plt_addr = pltoff;
4437 if (is_iplt)
4438 {
4439 if (iplt_base == invalid_address)
4440 iplt_base = this->targ_->iplt_section()->address();
4441 plt_addr += iplt_base;
4442 }
4443 else
4444 plt_addr += plt_base;
4445 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4446 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4447 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4448 Address off = plt_addr - got_addr;
4449
4450 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4451 gold_error(_("%s: linkage table error against `%s'"),
4452 cs->first.object_->name().c_str(),
4453 cs->first.sym_->demangled_name().c_str());
4454
4455 bool plt_load_toc = this->targ_->abiversion() < 2;
4456 bool static_chain
4457 = plt_load_toc && parameters->options().plt_static_chain();
4458 bool thread_safe
4459 = plt_load_toc && this->targ_->plt_thread_safe();
4460 bool use_fake_dep = false;
4461 Address cmp_branch_off = 0;
4462 if (thread_safe)
4463 {
4464 unsigned int pltindex
4465 = ((pltoff - this->targ_->first_plt_entry_offset())
4466 / this->targ_->plt_entry_size());
4467 Address glinkoff
4468 = (this->targ_->glink_section()->pltresolve_size
4469 + pltindex * 8);
4470 if (pltindex > 32768)
4471 glinkoff += (pltindex - 32768) * 4;
4472 Address to
4473 = this->targ_->glink_section()->address() + glinkoff;
4474 Address from
4475 = (this->stub_address() + cs->second + 24
4476 + 4 * (ha(off) != 0)
4477 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4478 + 4 * static_chain);
4479 cmp_branch_off = to - from;
4480 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4481 }
4482
4483 p = oview + cs->second;
4484 if (ha(off) != 0)
4485 {
4486 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4487 p += 4;
4488 if (plt_load_toc)
4489 {
4490 write_insn<big_endian>(p, addis_11_2 + ha(off));
4491 p += 4;
4492 write_insn<big_endian>(p, ld_12_11 + l(off));
4493 p += 4;
4494 }
4495 else
4496 {
4497 write_insn<big_endian>(p, addis_12_2 + ha(off));
4498 p += 4;
4499 write_insn<big_endian>(p, ld_12_12 + l(off));
4500 p += 4;
4501 }
4502 if (plt_load_toc
4503 && ha(off + 8 + 8 * static_chain) != ha(off))
4504 {
4505 write_insn<big_endian>(p, addi_11_11 + l(off));
4506 p += 4;
4507 off = 0;
4508 }
4509 write_insn<big_endian>(p, mtctr_12);
4510 p += 4;
4511 if (plt_load_toc)
4512 {
4513 if (use_fake_dep)
4514 {
4515 write_insn<big_endian>(p, xor_2_12_12);
4516 p += 4;
4517 write_insn<big_endian>(p, add_11_11_2);
4518 p += 4;
4519 }
4520 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4521 p += 4;
4522 if (static_chain)
4523 {
4524 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4525 p += 4;
4526 }
4527 }
4528 }
4529 else
4530 {
4531 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4532 p += 4;
4533 write_insn<big_endian>(p, ld_12_2 + l(off));
4534 p += 4;
4535 if (plt_load_toc
4536 && ha(off + 8 + 8 * static_chain) != ha(off))
4537 {
4538 write_insn<big_endian>(p, addi_2_2 + l(off));
4539 p += 4;
4540 off = 0;
4541 }
4542 write_insn<big_endian>(p, mtctr_12);
4543 p += 4;
4544 if (plt_load_toc)
4545 {
4546 if (use_fake_dep)
4547 {
4548 write_insn<big_endian>(p, xor_11_12_12);
4549 p += 4;
4550 write_insn<big_endian>(p, add_2_2_11);
4551 p += 4;
4552 }
4553 if (static_chain)
4554 {
4555 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4556 p += 4;
4557 }
4558 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4559 p += 4;
4560 }
4561 }
4562 if (thread_safe && !use_fake_dep)
4563 {
4564 write_insn<big_endian>(p, cmpldi_2_0);
4565 p += 4;
4566 write_insn<big_endian>(p, bnectr_p4);
4567 p += 4;
4568 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4569 }
4570 else
4571 write_insn<big_endian>(p, bctr);
4572 }
4573 }
4574
4575 // Write out long branch stubs.
4576 typename Branch_stub_entries::const_iterator bs;
4577 for (bs = this->long_branch_stubs_.begin();
4578 bs != this->long_branch_stubs_.end();
4579 ++bs)
4580 {
4581 if (bs->first.save_res_)
4582 continue;
4583 p = oview + this->plt_size_ + bs->second;
4584 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4585 Address delta = bs->first.dest_ - loc;
4586 if (delta + (1 << 25) < 2 << 25)
4587 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4588 else
4589 {
4590 Address brlt_addr
4591 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4592 gold_assert(brlt_addr != invalid_address);
4593 brlt_addr += this->targ_->brlt_section()->address();
4594 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4595 Address brltoff = brlt_addr - got_addr;
4596 if (ha(brltoff) == 0)
4597 {
4598 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
4599 }
4600 else
4601 {
4602 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
4603 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
4604 }
4605 write_insn<big_endian>(p, mtctr_12), p += 4;
4606 write_insn<big_endian>(p, bctr);
4607 }
4608 }
4609 }
4610 else
4611 {
4612 if (!this->plt_call_stubs_.empty())
4613 {
4614 // The base address of the .plt section.
4615 Address plt_base = this->targ_->plt_section()->address();
4616 Address iplt_base = invalid_address;
4617 // The address of _GLOBAL_OFFSET_TABLE_.
4618 Address g_o_t = invalid_address;
4619
4620 // Write out plt call stubs.
4621 typename Plt_stub_entries::const_iterator cs;
4622 for (cs = this->plt_call_stubs_.begin();
4623 cs != this->plt_call_stubs_.end();
4624 ++cs)
4625 {
4626 bool is_iplt;
4627 Address plt_addr = this->plt_off(cs, &is_iplt);
4628 if (is_iplt)
4629 {
4630 if (iplt_base == invalid_address)
4631 iplt_base = this->targ_->iplt_section()->address();
4632 plt_addr += iplt_base;
4633 }
4634 else
4635 plt_addr += plt_base;
4636
4637 p = oview + cs->second;
4638 if (parameters->options().output_is_position_independent())
4639 {
4640 Address got_addr;
4641 const Powerpc_relobj<size, big_endian>* ppcobj
4642 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4643 (cs->first.object_));
4644 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4645 {
4646 unsigned int got2 = ppcobj->got2_shndx();
4647 got_addr = ppcobj->get_output_section_offset(got2);
4648 gold_assert(got_addr != invalid_address);
4649 got_addr += (ppcobj->output_section(got2)->address()
4650 + cs->first.addend_);
4651 }
4652 else
4653 {
4654 if (g_o_t == invalid_address)
4655 {
4656 const Output_data_got_powerpc<size, big_endian>* got
4657 = this->targ_->got_section();
4658 g_o_t = got->address() + got->g_o_t();
4659 }
4660 got_addr = g_o_t;
4661 }
4662
4663 Address off = plt_addr - got_addr;
4664 if (ha(off) == 0)
4665 {
4666 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4667 write_insn<big_endian>(p + 4, mtctr_11);
4668 write_insn<big_endian>(p + 8, bctr);
4669 }
4670 else
4671 {
4672 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4673 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4674 write_insn<big_endian>(p + 8, mtctr_11);
4675 write_insn<big_endian>(p + 12, bctr);
4676 }
4677 }
4678 else
4679 {
4680 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4681 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4682 write_insn<big_endian>(p + 8, mtctr_11);
4683 write_insn<big_endian>(p + 12, bctr);
4684 }
4685 }
4686 }
4687
4688 // Write out long branch stubs.
4689 typename Branch_stub_entries::const_iterator bs;
4690 for (bs = this->long_branch_stubs_.begin();
4691 bs != this->long_branch_stubs_.end();
4692 ++bs)
4693 {
4694 if (bs->first.save_res_)
4695 continue;
4696 p = oview + this->plt_size_ + bs->second;
4697 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4698 Address delta = bs->first.dest_ - loc;
4699 if (delta + (1 << 25) < 2 << 25)
4700 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4701 else if (!parameters->options().output_is_position_independent())
4702 {
4703 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4704 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4705 write_insn<big_endian>(p + 8, mtctr_12);
4706 write_insn<big_endian>(p + 12, bctr);
4707 }
4708 else
4709 {
4710 delta -= 8;
4711 write_insn<big_endian>(p + 0, mflr_0);
4712 write_insn<big_endian>(p + 4, bcl_20_31);
4713 write_insn<big_endian>(p + 8, mflr_12);
4714 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4715 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4716 write_insn<big_endian>(p + 20, mtlr_0);
4717 write_insn<big_endian>(p + 24, mtctr_12);
4718 write_insn<big_endian>(p + 28, bctr);
4719 }
4720 }
4721 }
4722 if (this->need_save_res_)
4723 {
4724 p = oview + this->plt_size_ + this->branch_size_;
4725 memcpy (p, this->targ_->savres_section()->contents(),
4726 this->targ_->savres_section()->data_size());
4727 }
4728 }
4729
4730 // Write out .glink.
4731
4732 template<int size, bool big_endian>
4733 void
4734 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4735 {
4736 const section_size_type off = this->offset();
4737 const section_size_type oview_size =
4738 convert_to_section_size_type(this->data_size());
4739 unsigned char* const oview = of->get_output_view(off, oview_size);
4740 unsigned char* p;
4741
4742 // The base address of the .plt section.
4743 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4744 Address plt_base = this->targ_->plt_section()->address();
4745
4746 if (size == 64)
4747 {
4748 if (this->end_branch_table_ != 0)
4749 {
4750 // Write pltresolve stub.
4751 p = oview;
4752 Address after_bcl = this->address() + 16;
4753 Address pltoff = plt_base - after_bcl;
4754
4755 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4756
4757 if (this->targ_->abiversion() < 2)
4758 {
4759 write_insn<big_endian>(p, mflr_12), p += 4;
4760 write_insn<big_endian>(p, bcl_20_31), p += 4;
4761 write_insn<big_endian>(p, mflr_11), p += 4;
4762 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4763 write_insn<big_endian>(p, mtlr_12), p += 4;
4764 write_insn<big_endian>(p, add_11_2_11), p += 4;
4765 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4766 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
4767 write_insn<big_endian>(p, mtctr_12), p += 4;
4768 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
4769 }
4770 else
4771 {
4772 write_insn<big_endian>(p, mflr_0), p += 4;
4773 write_insn<big_endian>(p, bcl_20_31), p += 4;
4774 write_insn<big_endian>(p, mflr_11), p += 4;
4775 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4776 write_insn<big_endian>(p, mtlr_0), p += 4;
4777 write_insn<big_endian>(p, sub_12_12_11), p += 4;
4778 write_insn<big_endian>(p, add_11_2_11), p += 4;
4779 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
4780 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4781 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
4782 write_insn<big_endian>(p, mtctr_12), p += 4;
4783 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
4784 }
4785 write_insn<big_endian>(p, bctr), p += 4;
4786 while (p < oview + this->pltresolve_size)
4787 write_insn<big_endian>(p, nop), p += 4;
4788
4789 // Write lazy link call stubs.
4790 uint32_t indx = 0;
4791 while (p < oview + this->end_branch_table_)
4792 {
4793 if (this->targ_->abiversion() < 2)
4794 {
4795 if (indx < 0x8000)
4796 {
4797 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4798 }
4799 else
4800 {
4801 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
4802 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4803 }
4804 }
4805 uint32_t branch_off = 8 - (p - oview);
4806 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4807 indx++;
4808 }
4809 }
4810
4811 Address plt_base = this->targ_->plt_section()->address();
4812 Address iplt_base = invalid_address;
4813 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4814 Address global_entry_base = this->address() + global_entry_off;
4815 typename Global_entry_stub_entries::const_iterator ge;
4816 for (ge = this->global_entry_stubs_.begin();
4817 ge != this->global_entry_stubs_.end();
4818 ++ge)
4819 {
4820 p = oview + global_entry_off + ge->second;
4821 Address plt_addr = ge->first->plt_offset();
4822 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
4823 && ge->first->can_use_relative_reloc(false))
4824 {
4825 if (iplt_base == invalid_address)
4826 iplt_base = this->targ_->iplt_section()->address();
4827 plt_addr += iplt_base;
4828 }
4829 else
4830 plt_addr += plt_base;
4831 Address my_addr = global_entry_base + ge->second;
4832 Address off = plt_addr - my_addr;
4833
4834 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
4835 gold_error(_("%s: linkage table error against `%s'"),
4836 ge->first->object()->name().c_str(),
4837 ge->first->demangled_name().c_str());
4838
4839 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
4840 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
4841 write_insn<big_endian>(p, mtctr_12), p += 4;
4842 write_insn<big_endian>(p, bctr);
4843 }
4844 }
4845 else
4846 {
4847 const Output_data_got_powerpc<size, big_endian>* got
4848 = this->targ_->got_section();
4849 // The address of _GLOBAL_OFFSET_TABLE_.
4850 Address g_o_t = got->address() + got->g_o_t();
4851
4852 // Write out pltresolve branch table.
4853 p = oview;
4854 unsigned int the_end = oview_size - this->pltresolve_size;
4855 unsigned char* end_p = oview + the_end;
4856 while (p < end_p - 8 * 4)
4857 write_insn<big_endian>(p, b + end_p - p), p += 4;
4858 while (p < end_p)
4859 write_insn<big_endian>(p, nop), p += 4;
4860
4861 // Write out pltresolve call stub.
4862 if (parameters->options().output_is_position_independent())
4863 {
4864 Address res0_off = 0;
4865 Address after_bcl_off = the_end + 12;
4866 Address bcl_res0 = after_bcl_off - res0_off;
4867
4868 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4869 write_insn<big_endian>(p + 4, mflr_0);
4870 write_insn<big_endian>(p + 8, bcl_20_31);
4871 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4872 write_insn<big_endian>(p + 16, mflr_12);
4873 write_insn<big_endian>(p + 20, mtlr_0);
4874 write_insn<big_endian>(p + 24, sub_11_11_12);
4875
4876 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4877
4878 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4879 if (ha(got_bcl) == ha(got_bcl + 4))
4880 {
4881 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4882 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4883 }
4884 else
4885 {
4886 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4887 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4888 }
4889 write_insn<big_endian>(p + 40, mtctr_0);
4890 write_insn<big_endian>(p + 44, add_0_11_11);
4891 write_insn<big_endian>(p + 48, add_11_0_11);
4892 write_insn<big_endian>(p + 52, bctr);
4893 write_insn<big_endian>(p + 56, nop);
4894 write_insn<big_endian>(p + 60, nop);
4895 }
4896 else
4897 {
4898 Address res0 = this->address();
4899
4900 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4901 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4902 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4903 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4904 else
4905 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4906 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4907 write_insn<big_endian>(p + 16, mtctr_0);
4908 write_insn<big_endian>(p + 20, add_0_11_11);
4909 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4910 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4911 else
4912 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4913 write_insn<big_endian>(p + 28, add_11_0_11);
4914 write_insn<big_endian>(p + 32, bctr);
4915 write_insn<big_endian>(p + 36, nop);
4916 write_insn<big_endian>(p + 40, nop);
4917 write_insn<big_endian>(p + 44, nop);
4918 write_insn<big_endian>(p + 48, nop);
4919 write_insn<big_endian>(p + 52, nop);
4920 write_insn<big_endian>(p + 56, nop);
4921 write_insn<big_endian>(p + 60, nop);
4922 }
4923 p += 64;
4924 }
4925
4926 of->write_output_view(off, oview_size, oview);
4927 }
4928
4929
4930 // A class to handle linker generated save/restore functions.
4931
4932 template<int size, bool big_endian>
4933 class Output_data_save_res : public Output_section_data_build
4934 {
4935 public:
4936 Output_data_save_res(Symbol_table* symtab);
4937
4938 const unsigned char*
4939 contents() const
4940 {
4941 return contents_;
4942 }
4943
4944 protected:
4945 // Write to a map file.
4946 void
4947 do_print_to_mapfile(Mapfile* mapfile) const
4948 { mapfile->print_output_data(this, _("** save/restore")); }
4949
4950 void
4951 do_write(Output_file*);
4952
4953 private:
4954 // The maximum size of save/restore contents.
4955 static const unsigned int savres_max = 218*4;
4956
4957 void
4958 savres_define(Symbol_table* symtab,
4959 const char *name,
4960 unsigned int lo, unsigned int hi,
4961 unsigned char* write_ent(unsigned char*, int),
4962 unsigned char* write_tail(unsigned char*, int));
4963
4964 unsigned char *contents_;
4965 };
4966
4967 template<bool big_endian>
4968 static unsigned char*
4969 savegpr0(unsigned char* p, int r)
4970 {
4971 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4972 write_insn<big_endian>(p, insn);
4973 return p + 4;
4974 }
4975
4976 template<bool big_endian>
4977 static unsigned char*
4978 savegpr0_tail(unsigned char* p, int r)
4979 {
4980 p = savegpr0<big_endian>(p, r);
4981 uint32_t insn = std_0_1 + 16;
4982 write_insn<big_endian>(p, insn);
4983 p = p + 4;
4984 write_insn<big_endian>(p, blr);
4985 return p + 4;
4986 }
4987
4988 template<bool big_endian>
4989 static unsigned char*
4990 restgpr0(unsigned char* p, int r)
4991 {
4992 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4993 write_insn<big_endian>(p, insn);
4994 return p + 4;
4995 }
4996
4997 template<bool big_endian>
4998 static unsigned char*
4999 restgpr0_tail(unsigned char* p, int r)
5000 {
5001 uint32_t insn = ld_0_1 + 16;
5002 write_insn<big_endian>(p, insn);
5003 p = p + 4;
5004 p = restgpr0<big_endian>(p, r);
5005 write_insn<big_endian>(p, mtlr_0);
5006 p = p + 4;
5007 if (r == 29)
5008 {
5009 p = restgpr0<big_endian>(p, 30);
5010 p = restgpr0<big_endian>(p, 31);
5011 }
5012 write_insn<big_endian>(p, blr);
5013 return p + 4;
5014 }
5015
5016 template<bool big_endian>
5017 static unsigned char*
5018 savegpr1(unsigned char* p, int r)
5019 {
5020 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5021 write_insn<big_endian>(p, insn);
5022 return p + 4;
5023 }
5024
5025 template<bool big_endian>
5026 static unsigned char*
5027 savegpr1_tail(unsigned char* p, int r)
5028 {
5029 p = savegpr1<big_endian>(p, r);
5030 write_insn<big_endian>(p, blr);
5031 return p + 4;
5032 }
5033
5034 template<bool big_endian>
5035 static unsigned char*
5036 restgpr1(unsigned char* p, int r)
5037 {
5038 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
5039 write_insn<big_endian>(p, insn);
5040 return p + 4;
5041 }
5042
5043 template<bool big_endian>
5044 static unsigned char*
5045 restgpr1_tail(unsigned char* p, int r)
5046 {
5047 p = restgpr1<big_endian>(p, r);
5048 write_insn<big_endian>(p, blr);
5049 return p + 4;
5050 }
5051
5052 template<bool big_endian>
5053 static unsigned char*
5054 savefpr(unsigned char* p, int r)
5055 {
5056 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5057 write_insn<big_endian>(p, insn);
5058 return p + 4;
5059 }
5060
5061 template<bool big_endian>
5062 static unsigned char*
5063 savefpr0_tail(unsigned char* p, int r)
5064 {
5065 p = savefpr<big_endian>(p, r);
5066 write_insn<big_endian>(p, std_0_1 + 16);
5067 p = p + 4;
5068 write_insn<big_endian>(p, blr);
5069 return p + 4;
5070 }
5071
5072 template<bool big_endian>
5073 static unsigned char*
5074 restfpr(unsigned char* p, int r)
5075 {
5076 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
5077 write_insn<big_endian>(p, insn);
5078 return p + 4;
5079 }
5080
5081 template<bool big_endian>
5082 static unsigned char*
5083 restfpr0_tail(unsigned char* p, int r)
5084 {
5085 write_insn<big_endian>(p, ld_0_1 + 16);
5086 p = p + 4;
5087 p = restfpr<big_endian>(p, r);
5088 write_insn<big_endian>(p, mtlr_0);
5089 p = p + 4;
5090 if (r == 29)
5091 {
5092 p = restfpr<big_endian>(p, 30);
5093 p = restfpr<big_endian>(p, 31);
5094 }
5095 write_insn<big_endian>(p, blr);
5096 return p + 4;
5097 }
5098
5099 template<bool big_endian>
5100 static unsigned char*
5101 savefpr1_tail(unsigned char* p, int r)
5102 {
5103 p = savefpr<big_endian>(p, r);
5104 write_insn<big_endian>(p, blr);
5105 return p + 4;
5106 }
5107
5108 template<bool big_endian>
5109 static unsigned char*
5110 restfpr1_tail(unsigned char* p, int r)
5111 {
5112 p = restfpr<big_endian>(p, r);
5113 write_insn<big_endian>(p, blr);
5114 return p + 4;
5115 }
5116
5117 template<bool big_endian>
5118 static unsigned char*
5119 savevr(unsigned char* p, int r)
5120 {
5121 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5122 write_insn<big_endian>(p, insn);
5123 p = p + 4;
5124 insn = stvx_0_12_0 + (r << 21);
5125 write_insn<big_endian>(p, insn);
5126 return p + 4;
5127 }
5128
5129 template<bool big_endian>
5130 static unsigned char*
5131 savevr_tail(unsigned char* p, int r)
5132 {
5133 p = savevr<big_endian>(p, r);
5134 write_insn<big_endian>(p, blr);
5135 return p + 4;
5136 }
5137
5138 template<bool big_endian>
5139 static unsigned char*
5140 restvr(unsigned char* p, int r)
5141 {
5142 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
5143 write_insn<big_endian>(p, insn);
5144 p = p + 4;
5145 insn = lvx_0_12_0 + (r << 21);
5146 write_insn<big_endian>(p, insn);
5147 return p + 4;
5148 }
5149
5150 template<bool big_endian>
5151 static unsigned char*
5152 restvr_tail(unsigned char* p, int r)
5153 {
5154 p = restvr<big_endian>(p, r);
5155 write_insn<big_endian>(p, blr);
5156 return p + 4;
5157 }
5158
5159
5160 template<int size, bool big_endian>
5161 Output_data_save_res<size, big_endian>::Output_data_save_res(
5162 Symbol_table* symtab)
5163 : Output_section_data_build(4),
5164 contents_(NULL)
5165 {
5166 this->savres_define(symtab,
5167 "_savegpr0_", 14, 31,
5168 savegpr0<big_endian>, savegpr0_tail<big_endian>);
5169 this->savres_define(symtab,
5170 "_restgpr0_", 14, 29,
5171 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5172 this->savres_define(symtab,
5173 "_restgpr0_", 30, 31,
5174 restgpr0<big_endian>, restgpr0_tail<big_endian>);
5175 this->savres_define(symtab,
5176 "_savegpr1_", 14, 31,
5177 savegpr1<big_endian>, savegpr1_tail<big_endian>);
5178 this->savres_define(symtab,
5179 "_restgpr1_", 14, 31,
5180 restgpr1<big_endian>, restgpr1_tail<big_endian>);
5181 this->savres_define(symtab,
5182 "_savefpr_", 14, 31,
5183 savefpr<big_endian>, savefpr0_tail<big_endian>);
5184 this->savres_define(symtab,
5185 "_restfpr_", 14, 29,
5186 restfpr<big_endian>, restfpr0_tail<big_endian>);
5187 this->savres_define(symtab,
5188 "_restfpr_", 30, 31,
5189 restfpr<big_endian>, restfpr0_tail<big_endian>);
5190 this->savres_define(symtab,
5191 "._savef", 14, 31,
5192 savefpr<big_endian>, savefpr1_tail<big_endian>);
5193 this->savres_define(symtab,
5194 "._restf", 14, 31,
5195 restfpr<big_endian>, restfpr1_tail<big_endian>);
5196 this->savres_define(symtab,
5197 "_savevr_", 20, 31,
5198 savevr<big_endian>, savevr_tail<big_endian>);
5199 this->savres_define(symtab,
5200 "_restvr_", 20, 31,
5201 restvr<big_endian>, restvr_tail<big_endian>);
5202 }
5203
5204 template<int size, bool big_endian>
5205 void
5206 Output_data_save_res<size, big_endian>::savres_define(
5207 Symbol_table* symtab,
5208 const char *name,
5209 unsigned int lo, unsigned int hi,
5210 unsigned char* write_ent(unsigned char*, int),
5211 unsigned char* write_tail(unsigned char*, int))
5212 {
5213 size_t len = strlen(name);
5214 bool writing = false;
5215 char sym[16];
5216
5217 memcpy(sym, name, len);
5218 sym[len + 2] = 0;
5219
5220 for (unsigned int i = lo; i <= hi; i++)
5221 {
5222 sym[len + 0] = i / 10 + '0';
5223 sym[len + 1] = i % 10 + '0';
5224 Symbol* gsym = symtab->lookup(sym);
5225 bool refd = gsym != NULL && gsym->is_undefined();
5226 writing = writing || refd;
5227 if (writing)
5228 {
5229 if (this->contents_ == NULL)
5230 this->contents_ = new unsigned char[this->savres_max];
5231
5232 section_size_type value = this->current_data_size();
5233 unsigned char* p = this->contents_ + value;
5234 if (i != hi)
5235 p = write_ent(p, i);
5236 else
5237 p = write_tail(p, i);
5238 section_size_type cur_size = p - this->contents_;
5239 this->set_current_data_size(cur_size);
5240 if (refd)
5241 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
5242 this, value, cur_size - value,
5243 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
5244 elfcpp::STV_HIDDEN, 0, false, false);
5245 }
5246 }
5247 }
5248
5249 // Write out save/restore.
5250
5251 template<int size, bool big_endian>
5252 void
5253 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
5254 {
5255 const section_size_type off = this->offset();
5256 const section_size_type oview_size =
5257 convert_to_section_size_type(this->data_size());
5258 unsigned char* const oview = of->get_output_view(off, oview_size);
5259 memcpy(oview, this->contents_, oview_size);
5260 of->write_output_view(off, oview_size, oview);
5261 }
5262
5263
5264 // Create the glink section.
5265
5266 template<int size, bool big_endian>
5267 void
5268 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5269 {
5270 if (this->glink_ == NULL)
5271 {
5272 this->glink_ = new Output_data_glink<size, big_endian>(this);
5273 this->glink_->add_eh_frame(layout);
5274 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5275 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5276 this->glink_, ORDER_TEXT, false);
5277 }
5278 }
5279
5280 // Create a PLT entry for a global symbol.
5281
5282 template<int size, bool big_endian>
5283 void
5284 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5285 Layout* layout,
5286 Symbol* gsym)
5287 {
5288 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5289 && gsym->can_use_relative_reloc(false))
5290 {
5291 if (this->iplt_ == NULL)
5292 this->make_iplt_section(symtab, layout);
5293 this->iplt_->add_ifunc_entry(gsym);
5294 }
5295 else
5296 {
5297 if (this->plt_ == NULL)
5298 this->make_plt_section(symtab, layout);
5299 this->plt_->add_entry(gsym);
5300 }
5301 }
5302
5303 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5304
5305 template<int size, bool big_endian>
5306 void
5307 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5308 Symbol_table* symtab,
5309 Layout* layout,
5310 Sized_relobj_file<size, big_endian>* relobj,
5311 unsigned int r_sym)
5312 {
5313 if (this->iplt_ == NULL)
5314 this->make_iplt_section(symtab, layout);
5315 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5316 }
5317
5318 // Return the number of entries in the PLT.
5319
5320 template<int size, bool big_endian>
5321 unsigned int
5322 Target_powerpc<size, big_endian>::plt_entry_count() const
5323 {
5324 if (this->plt_ == NULL)
5325 return 0;
5326 return this->plt_->entry_count();
5327 }
5328
5329 // Create a GOT entry for local dynamic __tls_get_addr calls.
5330
5331 template<int size, bool big_endian>
5332 unsigned int
5333 Target_powerpc<size, big_endian>::tlsld_got_offset(
5334 Symbol_table* symtab,
5335 Layout* layout,
5336 Sized_relobj_file<size, big_endian>* object)
5337 {
5338 if (this->tlsld_got_offset_ == -1U)
5339 {
5340 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5341 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5342 Output_data_got_powerpc<size, big_endian>* got
5343 = this->got_section(symtab, layout);
5344 unsigned int got_offset = got->add_constant_pair(0, 0);
5345 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5346 got_offset, 0);
5347 this->tlsld_got_offset_ = got_offset;
5348 }
5349 return this->tlsld_got_offset_;
5350 }
5351
5352 // Get the Reference_flags for a particular relocation.
5353
5354 template<int size, bool big_endian>
5355 int
5356 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5357 unsigned int r_type,
5358 const Target_powerpc* target)
5359 {
5360 int ref = 0;
5361
5362 switch (r_type)
5363 {
5364 case elfcpp::R_POWERPC_NONE:
5365 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5366 case elfcpp::R_POWERPC_GNU_VTENTRY:
5367 case elfcpp::R_PPC64_TOC:
5368 // No symbol reference.
5369 break;
5370
5371 case elfcpp::R_PPC64_ADDR64:
5372 case elfcpp::R_PPC64_UADDR64:
5373 case elfcpp::R_POWERPC_ADDR32:
5374 case elfcpp::R_POWERPC_UADDR32:
5375 case elfcpp::R_POWERPC_ADDR16:
5376 case elfcpp::R_POWERPC_UADDR16:
5377 case elfcpp::R_POWERPC_ADDR16_LO:
5378 case elfcpp::R_POWERPC_ADDR16_HI:
5379 case elfcpp::R_POWERPC_ADDR16_HA:
5380 ref = Symbol::ABSOLUTE_REF;
5381 break;
5382
5383 case elfcpp::R_POWERPC_ADDR24:
5384 case elfcpp::R_POWERPC_ADDR14:
5385 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5386 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5387 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5388 break;
5389
5390 case elfcpp::R_PPC64_REL64:
5391 case elfcpp::R_POWERPC_REL32:
5392 case elfcpp::R_PPC_LOCAL24PC:
5393 case elfcpp::R_POWERPC_REL16:
5394 case elfcpp::R_POWERPC_REL16_LO:
5395 case elfcpp::R_POWERPC_REL16_HI:
5396 case elfcpp::R_POWERPC_REL16_HA:
5397 ref = Symbol::RELATIVE_REF;
5398 break;
5399
5400 case elfcpp::R_POWERPC_REL24:
5401 case elfcpp::R_PPC_PLTREL24:
5402 case elfcpp::R_POWERPC_REL14:
5403 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5404 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5405 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5406 break;
5407
5408 case elfcpp::R_POWERPC_GOT16:
5409 case elfcpp::R_POWERPC_GOT16_LO:
5410 case elfcpp::R_POWERPC_GOT16_HI:
5411 case elfcpp::R_POWERPC_GOT16_HA:
5412 case elfcpp::R_PPC64_GOT16_DS:
5413 case elfcpp::R_PPC64_GOT16_LO_DS:
5414 case elfcpp::R_PPC64_TOC16:
5415 case elfcpp::R_PPC64_TOC16_LO:
5416 case elfcpp::R_PPC64_TOC16_HI:
5417 case elfcpp::R_PPC64_TOC16_HA:
5418 case elfcpp::R_PPC64_TOC16_DS:
5419 case elfcpp::R_PPC64_TOC16_LO_DS:
5420 ref = Symbol::RELATIVE_REF;
5421 break;
5422
5423 case elfcpp::R_POWERPC_GOT_TPREL16:
5424 case elfcpp::R_POWERPC_TLS:
5425 ref = Symbol::TLS_REF;
5426 break;
5427
5428 case elfcpp::R_POWERPC_COPY:
5429 case elfcpp::R_POWERPC_GLOB_DAT:
5430 case elfcpp::R_POWERPC_JMP_SLOT:
5431 case elfcpp::R_POWERPC_RELATIVE:
5432 case elfcpp::R_POWERPC_DTPMOD:
5433 default:
5434 // Not expected. We will give an error later.
5435 break;
5436 }
5437
5438 if (size == 64 && target->abiversion() < 2)
5439 ref |= Symbol::FUNC_DESC_ABI;
5440 return ref;
5441 }
5442
5443 // Report an unsupported relocation against a local symbol.
5444
5445 template<int size, bool big_endian>
5446 void
5447 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5448 Sized_relobj_file<size, big_endian>* object,
5449 unsigned int r_type)
5450 {
5451 gold_error(_("%s: unsupported reloc %u against local symbol"),
5452 object->name().c_str(), r_type);
5453 }
5454
5455 // We are about to emit a dynamic relocation of type R_TYPE. If the
5456 // dynamic linker does not support it, issue an error.
5457
5458 template<int size, bool big_endian>
5459 void
5460 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5461 unsigned int r_type)
5462 {
5463 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5464
5465 // These are the relocation types supported by glibc for both 32-bit
5466 // and 64-bit powerpc.
5467 switch (r_type)
5468 {
5469 case elfcpp::R_POWERPC_NONE:
5470 case elfcpp::R_POWERPC_RELATIVE:
5471 case elfcpp::R_POWERPC_GLOB_DAT:
5472 case elfcpp::R_POWERPC_DTPMOD:
5473 case elfcpp::R_POWERPC_DTPREL:
5474 case elfcpp::R_POWERPC_TPREL:
5475 case elfcpp::R_POWERPC_JMP_SLOT:
5476 case elfcpp::R_POWERPC_COPY:
5477 case elfcpp::R_POWERPC_IRELATIVE:
5478 case elfcpp::R_POWERPC_ADDR32:
5479 case elfcpp::R_POWERPC_UADDR32:
5480 case elfcpp::R_POWERPC_ADDR24:
5481 case elfcpp::R_POWERPC_ADDR16:
5482 case elfcpp::R_POWERPC_UADDR16:
5483 case elfcpp::R_POWERPC_ADDR16_LO:
5484 case elfcpp::R_POWERPC_ADDR16_HI:
5485 case elfcpp::R_POWERPC_ADDR16_HA:
5486 case elfcpp::R_POWERPC_ADDR14:
5487 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5488 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5489 case elfcpp::R_POWERPC_REL32:
5490 case elfcpp::R_POWERPC_REL24:
5491 case elfcpp::R_POWERPC_TPREL16:
5492 case elfcpp::R_POWERPC_TPREL16_LO:
5493 case elfcpp::R_POWERPC_TPREL16_HI:
5494 case elfcpp::R_POWERPC_TPREL16_HA:
5495 return;
5496
5497 default:
5498 break;
5499 }
5500
5501 if (size == 64)
5502 {
5503 switch (r_type)
5504 {
5505 // These are the relocation types supported only on 64-bit.
5506 case elfcpp::R_PPC64_ADDR64:
5507 case elfcpp::R_PPC64_UADDR64:
5508 case elfcpp::R_PPC64_JMP_IREL:
5509 case elfcpp::R_PPC64_ADDR16_DS:
5510 case elfcpp::R_PPC64_ADDR16_LO_DS:
5511 case elfcpp::R_PPC64_ADDR16_HIGH:
5512 case elfcpp::R_PPC64_ADDR16_HIGHA:
5513 case elfcpp::R_PPC64_ADDR16_HIGHER:
5514 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5515 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5516 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5517 case elfcpp::R_PPC64_REL64:
5518 case elfcpp::R_POWERPC_ADDR30:
5519 case elfcpp::R_PPC64_TPREL16_DS:
5520 case elfcpp::R_PPC64_TPREL16_LO_DS:
5521 case elfcpp::R_PPC64_TPREL16_HIGH:
5522 case elfcpp::R_PPC64_TPREL16_HIGHA:
5523 case elfcpp::R_PPC64_TPREL16_HIGHER:
5524 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5525 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5526 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5527 return;
5528
5529 default:
5530 break;
5531 }
5532 }
5533 else
5534 {
5535 switch (r_type)
5536 {
5537 // These are the relocation types supported only on 32-bit.
5538 // ??? glibc ld.so doesn't need to support these.
5539 case elfcpp::R_POWERPC_DTPREL16:
5540 case elfcpp::R_POWERPC_DTPREL16_LO:
5541 case elfcpp::R_POWERPC_DTPREL16_HI:
5542 case elfcpp::R_POWERPC_DTPREL16_HA:
5543 return;
5544
5545 default:
5546 break;
5547 }
5548 }
5549
5550 // This prevents us from issuing more than one error per reloc
5551 // section. But we can still wind up issuing more than one
5552 // error per object file.
5553 if (this->issued_non_pic_error_)
5554 return;
5555 gold_assert(parameters->options().output_is_position_independent());
5556 object->error(_("requires unsupported dynamic reloc; "
5557 "recompile with -fPIC"));
5558 this->issued_non_pic_error_ = true;
5559 return;
5560 }
5561
5562 // Return whether we need to make a PLT entry for a relocation of the
5563 // given type against a STT_GNU_IFUNC symbol.
5564
5565 template<int size, bool big_endian>
5566 bool
5567 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5568 Target_powerpc<size, big_endian>* target,
5569 Sized_relobj_file<size, big_endian>* object,
5570 unsigned int r_type,
5571 bool report_err)
5572 {
5573 // In non-pic code any reference will resolve to the plt call stub
5574 // for the ifunc symbol.
5575 if ((size == 32 || target->abiversion() >= 2)
5576 && !parameters->options().output_is_position_independent())
5577 return true;
5578
5579 switch (r_type)
5580 {
5581 // Word size refs from data sections are OK, but don't need a PLT entry.
5582 case elfcpp::R_POWERPC_ADDR32:
5583 case elfcpp::R_POWERPC_UADDR32:
5584 if (size == 32)
5585 return false;
5586 break;
5587
5588 case elfcpp::R_PPC64_ADDR64:
5589 case elfcpp::R_PPC64_UADDR64:
5590 if (size == 64)
5591 return false;
5592 break;
5593
5594 // GOT refs are good, but also don't need a PLT entry.
5595 case elfcpp::R_POWERPC_GOT16:
5596 case elfcpp::R_POWERPC_GOT16_LO:
5597 case elfcpp::R_POWERPC_GOT16_HI:
5598 case elfcpp::R_POWERPC_GOT16_HA:
5599 case elfcpp::R_PPC64_GOT16_DS:
5600 case elfcpp::R_PPC64_GOT16_LO_DS:
5601 return false;
5602
5603 // Function calls are good, and these do need a PLT entry.
5604 case elfcpp::R_POWERPC_ADDR24:
5605 case elfcpp::R_POWERPC_ADDR14:
5606 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5607 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5608 case elfcpp::R_POWERPC_REL24:
5609 case elfcpp::R_PPC_PLTREL24:
5610 case elfcpp::R_POWERPC_REL14:
5611 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5612 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5613 return true;
5614
5615 default:
5616 break;
5617 }
5618
5619 // Anything else is a problem.
5620 // If we are building a static executable, the libc startup function
5621 // responsible for applying indirect function relocations is going
5622 // to complain about the reloc type.
5623 // If we are building a dynamic executable, we will have a text
5624 // relocation. The dynamic loader will set the text segment
5625 // writable and non-executable to apply text relocations. So we'll
5626 // segfault when trying to run the indirection function to resolve
5627 // the reloc.
5628 if (report_err)
5629 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5630 object->name().c_str(), r_type);
5631 return false;
5632 }
5633
5634 // Scan a relocation for a local symbol.
5635
5636 template<int size, bool big_endian>
5637 inline void
5638 Target_powerpc<size, big_endian>::Scan::local(
5639 Symbol_table* symtab,
5640 Layout* layout,
5641 Target_powerpc<size, big_endian>* target,
5642 Sized_relobj_file<size, big_endian>* object,
5643 unsigned int data_shndx,
5644 Output_section* output_section,
5645 const elfcpp::Rela<size, big_endian>& reloc,
5646 unsigned int r_type,
5647 const elfcpp::Sym<size, big_endian>& lsym,
5648 bool is_discarded)
5649 {
5650 this->maybe_skip_tls_get_addr_call(r_type, NULL);
5651
5652 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5653 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5654 {
5655 this->expect_tls_get_addr_call();
5656 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5657 if (tls_type != tls::TLSOPT_NONE)
5658 this->skip_next_tls_get_addr_call();
5659 }
5660 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5661 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5662 {
5663 this->expect_tls_get_addr_call();
5664 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5665 if (tls_type != tls::TLSOPT_NONE)
5666 this->skip_next_tls_get_addr_call();
5667 }
5668
5669 Powerpc_relobj<size, big_endian>* ppc_object
5670 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5671
5672 if (is_discarded)
5673 {
5674 if (size == 64
5675 && data_shndx == ppc_object->opd_shndx()
5676 && r_type == elfcpp::R_PPC64_ADDR64)
5677 ppc_object->set_opd_discard(reloc.get_r_offset());
5678 return;
5679 }
5680
5681 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5682 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5683 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5684 {
5685 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5686 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5687 r_type, r_sym, reloc.get_r_addend());
5688 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5689 }
5690
5691 switch (r_type)
5692 {
5693 case elfcpp::R_POWERPC_NONE:
5694 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5695 case elfcpp::R_POWERPC_GNU_VTENTRY:
5696 case elfcpp::R_PPC64_TOCSAVE:
5697 case elfcpp::R_POWERPC_TLS:
5698 case elfcpp::R_PPC64_ENTRY:
5699 break;
5700
5701 case elfcpp::R_PPC64_TOC:
5702 {
5703 Output_data_got_powerpc<size, big_endian>* got
5704 = target->got_section(symtab, layout);
5705 if (parameters->options().output_is_position_independent())
5706 {
5707 Address off = reloc.get_r_offset();
5708 if (size == 64
5709 && target->abiversion() < 2
5710 && data_shndx == ppc_object->opd_shndx()
5711 && ppc_object->get_opd_discard(off - 8))
5712 break;
5713
5714 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5715 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5716 rela_dyn->add_output_section_relative(got->output_section(),
5717 elfcpp::R_POWERPC_RELATIVE,
5718 output_section,
5719 object, data_shndx, off,
5720 symobj->toc_base_offset());
5721 }
5722 }
5723 break;
5724
5725 case elfcpp::R_PPC64_ADDR64:
5726 case elfcpp::R_PPC64_UADDR64:
5727 case elfcpp::R_POWERPC_ADDR32:
5728 case elfcpp::R_POWERPC_UADDR32:
5729 case elfcpp::R_POWERPC_ADDR24:
5730 case elfcpp::R_POWERPC_ADDR16:
5731 case elfcpp::R_POWERPC_ADDR16_LO:
5732 case elfcpp::R_POWERPC_ADDR16_HI:
5733 case elfcpp::R_POWERPC_ADDR16_HA:
5734 case elfcpp::R_POWERPC_UADDR16:
5735 case elfcpp::R_PPC64_ADDR16_HIGH:
5736 case elfcpp::R_PPC64_ADDR16_HIGHA:
5737 case elfcpp::R_PPC64_ADDR16_HIGHER:
5738 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5739 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5740 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5741 case elfcpp::R_PPC64_ADDR16_DS:
5742 case elfcpp::R_PPC64_ADDR16_LO_DS:
5743 case elfcpp::R_POWERPC_ADDR14:
5744 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5745 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5746 // If building a shared library (or a position-independent
5747 // executable), we need to create a dynamic relocation for
5748 // this location.
5749 if (parameters->options().output_is_position_independent()
5750 || (size == 64 && is_ifunc && target->abiversion() < 2))
5751 {
5752 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5753 is_ifunc);
5754 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5755 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5756 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5757 {
5758 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5759 : elfcpp::R_POWERPC_RELATIVE);
5760 rela_dyn->add_local_relative(object, r_sym, dynrel,
5761 output_section, data_shndx,
5762 reloc.get_r_offset(),
5763 reloc.get_r_addend(), false);
5764 }
5765 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
5766 {
5767 check_non_pic(object, r_type);
5768 rela_dyn->add_local(object, r_sym, r_type, output_section,
5769 data_shndx, reloc.get_r_offset(),
5770 reloc.get_r_addend());
5771 }
5772 else
5773 {
5774 gold_assert(lsym.get_st_value() == 0);
5775 unsigned int shndx = lsym.get_st_shndx();
5776 bool is_ordinary;
5777 shndx = object->adjust_sym_shndx(r_sym, shndx,
5778 &is_ordinary);
5779 if (!is_ordinary)
5780 object->error(_("section symbol %u has bad shndx %u"),
5781 r_sym, shndx);
5782 else
5783 rela_dyn->add_local_section(object, shndx, r_type,
5784 output_section, data_shndx,
5785 reloc.get_r_offset());
5786 }
5787 }
5788 break;
5789
5790 case elfcpp::R_POWERPC_REL24:
5791 case elfcpp::R_PPC_PLTREL24:
5792 case elfcpp::R_PPC_LOCAL24PC:
5793 case elfcpp::R_POWERPC_REL14:
5794 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5795 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5796 if (!is_ifunc)
5797 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5798 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5799 reloc.get_r_addend());
5800 break;
5801
5802 case elfcpp::R_PPC64_REL64:
5803 case elfcpp::R_POWERPC_REL32:
5804 case elfcpp::R_POWERPC_REL16:
5805 case elfcpp::R_POWERPC_REL16_LO:
5806 case elfcpp::R_POWERPC_REL16_HI:
5807 case elfcpp::R_POWERPC_REL16_HA:
5808 case elfcpp::R_POWERPC_REL16DX_HA:
5809 case elfcpp::R_POWERPC_SECTOFF:
5810 case elfcpp::R_POWERPC_SECTOFF_LO:
5811 case elfcpp::R_POWERPC_SECTOFF_HI:
5812 case elfcpp::R_POWERPC_SECTOFF_HA:
5813 case elfcpp::R_PPC64_SECTOFF_DS:
5814 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5815 case elfcpp::R_POWERPC_TPREL16:
5816 case elfcpp::R_POWERPC_TPREL16_LO:
5817 case elfcpp::R_POWERPC_TPREL16_HI:
5818 case elfcpp::R_POWERPC_TPREL16_HA:
5819 case elfcpp::R_PPC64_TPREL16_DS:
5820 case elfcpp::R_PPC64_TPREL16_LO_DS:
5821 case elfcpp::R_PPC64_TPREL16_HIGH:
5822 case elfcpp::R_PPC64_TPREL16_HIGHA:
5823 case elfcpp::R_PPC64_TPREL16_HIGHER:
5824 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5825 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5826 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5827 case elfcpp::R_POWERPC_DTPREL16:
5828 case elfcpp::R_POWERPC_DTPREL16_LO:
5829 case elfcpp::R_POWERPC_DTPREL16_HI:
5830 case elfcpp::R_POWERPC_DTPREL16_HA:
5831 case elfcpp::R_PPC64_DTPREL16_DS:
5832 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5833 case elfcpp::R_PPC64_DTPREL16_HIGH:
5834 case elfcpp::R_PPC64_DTPREL16_HIGHA:
5835 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5836 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5837 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5838 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5839 case elfcpp::R_PPC64_TLSGD:
5840 case elfcpp::R_PPC64_TLSLD:
5841 case elfcpp::R_PPC64_ADDR64_LOCAL:
5842 break;
5843
5844 case elfcpp::R_POWERPC_GOT16:
5845 case elfcpp::R_POWERPC_GOT16_LO:
5846 case elfcpp::R_POWERPC_GOT16_HI:
5847 case elfcpp::R_POWERPC_GOT16_HA:
5848 case elfcpp::R_PPC64_GOT16_DS:
5849 case elfcpp::R_PPC64_GOT16_LO_DS:
5850 {
5851 // The symbol requires a GOT entry.
5852 Output_data_got_powerpc<size, big_endian>* got
5853 = target->got_section(symtab, layout);
5854 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5855
5856 if (!parameters->options().output_is_position_independent())
5857 {
5858 if (is_ifunc
5859 && (size == 32 || target->abiversion() >= 2))
5860 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5861 else
5862 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5863 }
5864 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5865 {
5866 // If we are generating a shared object or a pie, this
5867 // symbol's GOT entry will be set by a dynamic relocation.
5868 unsigned int off;
5869 off = got->add_constant(0);
5870 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5871
5872 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5873 is_ifunc);
5874 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5875 : elfcpp::R_POWERPC_RELATIVE);
5876 rela_dyn->add_local_relative(object, r_sym, dynrel,
5877 got, off, 0, false);
5878 }
5879 }
5880 break;
5881
5882 case elfcpp::R_PPC64_TOC16:
5883 case elfcpp::R_PPC64_TOC16_LO:
5884 case elfcpp::R_PPC64_TOC16_HI:
5885 case elfcpp::R_PPC64_TOC16_HA:
5886 case elfcpp::R_PPC64_TOC16_DS:
5887 case elfcpp::R_PPC64_TOC16_LO_DS:
5888 // We need a GOT section.
5889 target->got_section(symtab, layout);
5890 break;
5891
5892 case elfcpp::R_POWERPC_GOT_TLSGD16:
5893 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5894 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5895 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5896 {
5897 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5898 if (tls_type == tls::TLSOPT_NONE)
5899 {
5900 Output_data_got_powerpc<size, big_endian>* got
5901 = target->got_section(symtab, layout);
5902 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5903 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5904 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5905 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5906 }
5907 else if (tls_type == tls::TLSOPT_TO_LE)
5908 {
5909 // no GOT relocs needed for Local Exec.
5910 }
5911 else
5912 gold_unreachable();
5913 }
5914 break;
5915
5916 case elfcpp::R_POWERPC_GOT_TLSLD16:
5917 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5918 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5919 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5920 {
5921 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5922 if (tls_type == tls::TLSOPT_NONE)
5923 target->tlsld_got_offset(symtab, layout, object);
5924 else if (tls_type == tls::TLSOPT_TO_LE)
5925 {
5926 // no GOT relocs needed for Local Exec.
5927 if (parameters->options().emit_relocs())
5928 {
5929 Output_section* os = layout->tls_segment()->first_section();
5930 gold_assert(os != NULL);
5931 os->set_needs_symtab_index();
5932 }
5933 }
5934 else
5935 gold_unreachable();
5936 }
5937 break;
5938
5939 case elfcpp::R_POWERPC_GOT_DTPREL16:
5940 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5941 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5942 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5943 {
5944 Output_data_got_powerpc<size, big_endian>* got
5945 = target->got_section(symtab, layout);
5946 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5947 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5948 }
5949 break;
5950
5951 case elfcpp::R_POWERPC_GOT_TPREL16:
5952 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5953 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5954 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5955 {
5956 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5957 if (tls_type == tls::TLSOPT_NONE)
5958 {
5959 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5960 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5961 {
5962 Output_data_got_powerpc<size, big_endian>* got
5963 = target->got_section(symtab, layout);
5964 unsigned int off = got->add_constant(0);
5965 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5966
5967 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5968 rela_dyn->add_symbolless_local_addend(object, r_sym,
5969 elfcpp::R_POWERPC_TPREL,
5970 got, off, 0);
5971 }
5972 }
5973 else if (tls_type == tls::TLSOPT_TO_LE)
5974 {
5975 // no GOT relocs needed for Local Exec.
5976 }
5977 else
5978 gold_unreachable();
5979 }
5980 break;
5981
5982 default:
5983 unsupported_reloc_local(object, r_type);
5984 break;
5985 }
5986
5987 switch (r_type)
5988 {
5989 case elfcpp::R_POWERPC_GOT_TLSLD16:
5990 case elfcpp::R_POWERPC_GOT_TLSGD16:
5991 case elfcpp::R_POWERPC_GOT_TPREL16:
5992 case elfcpp::R_POWERPC_GOT_DTPREL16:
5993 case elfcpp::R_POWERPC_GOT16:
5994 case elfcpp::R_PPC64_GOT16_DS:
5995 case elfcpp::R_PPC64_TOC16:
5996 case elfcpp::R_PPC64_TOC16_DS:
5997 ppc_object->set_has_small_toc_reloc();
5998 default:
5999 break;
6000 }
6001 }
6002
6003 // Report an unsupported relocation against a global symbol.
6004
6005 template<int size, bool big_endian>
6006 void
6007 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
6008 Sized_relobj_file<size, big_endian>* object,
6009 unsigned int r_type,
6010 Symbol* gsym)
6011 {
6012 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
6013 object->name().c_str(), r_type, gsym->demangled_name().c_str());
6014 }
6015
6016 // Scan a relocation for a global symbol.
6017
6018 template<int size, bool big_endian>
6019 inline void
6020 Target_powerpc<size, big_endian>::Scan::global(
6021 Symbol_table* symtab,
6022 Layout* layout,
6023 Target_powerpc<size, big_endian>* target,
6024 Sized_relobj_file<size, big_endian>* object,
6025 unsigned int data_shndx,
6026 Output_section* output_section,
6027 const elfcpp::Rela<size, big_endian>& reloc,
6028 unsigned int r_type,
6029 Symbol* gsym)
6030 {
6031 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
6032 return;
6033
6034 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6035 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6036 {
6037 this->expect_tls_get_addr_call();
6038 const bool final = gsym->final_value_is_known();
6039 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6040 if (tls_type != tls::TLSOPT_NONE)
6041 this->skip_next_tls_get_addr_call();
6042 }
6043 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6044 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6045 {
6046 this->expect_tls_get_addr_call();
6047 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6048 if (tls_type != tls::TLSOPT_NONE)
6049 this->skip_next_tls_get_addr_call();
6050 }
6051
6052 Powerpc_relobj<size, big_endian>* ppc_object
6053 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6054
6055 // A STT_GNU_IFUNC symbol may require a PLT entry.
6056 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
6057 bool pushed_ifunc = false;
6058 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
6059 {
6060 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6061 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
6062 reloc.get_r_addend());
6063 target->make_plt_entry(symtab, layout, gsym);
6064 pushed_ifunc = true;
6065 }
6066
6067 switch (r_type)
6068 {
6069 case elfcpp::R_POWERPC_NONE:
6070 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6071 case elfcpp::R_POWERPC_GNU_VTENTRY:
6072 case elfcpp::R_PPC_LOCAL24PC:
6073 case elfcpp::R_POWERPC_TLS:
6074 case elfcpp::R_PPC64_ENTRY:
6075 break;
6076
6077 case elfcpp::R_PPC64_TOC:
6078 {
6079 Output_data_got_powerpc<size, big_endian>* got
6080 = target->got_section(symtab, layout);
6081 if (parameters->options().output_is_position_independent())
6082 {
6083 Address off = reloc.get_r_offset();
6084 if (size == 64
6085 && data_shndx == ppc_object->opd_shndx()
6086 && ppc_object->get_opd_discard(off - 8))
6087 break;
6088
6089 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6090 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
6091 if (data_shndx != ppc_object->opd_shndx())
6092 symobj = static_cast
6093 <Powerpc_relobj<size, big_endian>*>(gsym->object());
6094 rela_dyn->add_output_section_relative(got->output_section(),
6095 elfcpp::R_POWERPC_RELATIVE,
6096 output_section,
6097 object, data_shndx, off,
6098 symobj->toc_base_offset());
6099 }
6100 }
6101 break;
6102
6103 case elfcpp::R_PPC64_ADDR64:
6104 if (size == 64
6105 && target->abiversion() < 2
6106 && data_shndx == ppc_object->opd_shndx()
6107 && (gsym->is_defined_in_discarded_section()
6108 || gsym->object() != object))
6109 {
6110 ppc_object->set_opd_discard(reloc.get_r_offset());
6111 break;
6112 }
6113 // Fall through.
6114 case elfcpp::R_PPC64_UADDR64:
6115 case elfcpp::R_POWERPC_ADDR32:
6116 case elfcpp::R_POWERPC_UADDR32:
6117 case elfcpp::R_POWERPC_ADDR24:
6118 case elfcpp::R_POWERPC_ADDR16:
6119 case elfcpp::R_POWERPC_ADDR16_LO:
6120 case elfcpp::R_POWERPC_ADDR16_HI:
6121 case elfcpp::R_POWERPC_ADDR16_HA:
6122 case elfcpp::R_POWERPC_UADDR16:
6123 case elfcpp::R_PPC64_ADDR16_HIGH:
6124 case elfcpp::R_PPC64_ADDR16_HIGHA:
6125 case elfcpp::R_PPC64_ADDR16_HIGHER:
6126 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6127 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6128 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6129 case elfcpp::R_PPC64_ADDR16_DS:
6130 case elfcpp::R_PPC64_ADDR16_LO_DS:
6131 case elfcpp::R_POWERPC_ADDR14:
6132 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6133 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6134 {
6135 // Make a PLT entry if necessary.
6136 if (gsym->needs_plt_entry())
6137 {
6138 // Since this is not a PC-relative relocation, we may be
6139 // taking the address of a function. In that case we need to
6140 // set the entry in the dynamic symbol table to the address of
6141 // the PLT call stub.
6142 bool need_ifunc_plt = false;
6143 if ((size == 32 || target->abiversion() >= 2)
6144 && gsym->is_from_dynobj()
6145 && !parameters->options().output_is_position_independent())
6146 {
6147 gsym->set_needs_dynsym_value();
6148 need_ifunc_plt = true;
6149 }
6150 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
6151 {
6152 target->push_branch(ppc_object, data_shndx,
6153 reloc.get_r_offset(), r_type,
6154 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
6155 reloc.get_r_addend());
6156 target->make_plt_entry(symtab, layout, gsym);
6157 }
6158 }
6159 // Make a dynamic relocation if necessary.
6160 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
6161 || (size == 64 && is_ifunc && target->abiversion() < 2))
6162 {
6163 if (!parameters->options().output_is_position_independent()
6164 && gsym->may_need_copy_reloc())
6165 {
6166 target->copy_reloc(symtab, layout, object,
6167 data_shndx, output_section, gsym, reloc);
6168 }
6169 else if ((((size == 32
6170 && r_type == elfcpp::R_POWERPC_ADDR32)
6171 || (size == 64
6172 && r_type == elfcpp::R_PPC64_ADDR64
6173 && target->abiversion() >= 2))
6174 && gsym->can_use_relative_reloc(false)
6175 && !(gsym->visibility() == elfcpp::STV_PROTECTED
6176 && parameters->options().shared()))
6177 || (size == 64
6178 && r_type == elfcpp::R_PPC64_ADDR64
6179 && target->abiversion() < 2
6180 && (gsym->can_use_relative_reloc(false)
6181 || data_shndx == ppc_object->opd_shndx())))
6182 {
6183 Reloc_section* rela_dyn
6184 = target->rela_dyn_section(symtab, layout, is_ifunc);
6185 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6186 : elfcpp::R_POWERPC_RELATIVE);
6187 rela_dyn->add_symbolless_global_addend(
6188 gsym, dynrel, output_section, object, data_shndx,
6189 reloc.get_r_offset(), reloc.get_r_addend());
6190 }
6191 else
6192 {
6193 Reloc_section* rela_dyn
6194 = target->rela_dyn_section(symtab, layout, is_ifunc);
6195 check_non_pic(object, r_type);
6196 rela_dyn->add_global(gsym, r_type, output_section,
6197 object, data_shndx,
6198 reloc.get_r_offset(),
6199 reloc.get_r_addend());
6200 }
6201 }
6202 }
6203 break;
6204
6205 case elfcpp::R_PPC_PLTREL24:
6206 case elfcpp::R_POWERPC_REL24:
6207 if (!is_ifunc)
6208 {
6209 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6210 r_type,
6211 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
6212 reloc.get_r_addend());
6213 if (gsym->needs_plt_entry()
6214 || (!gsym->final_value_is_known()
6215 && (gsym->is_undefined()
6216 || gsym->is_from_dynobj()
6217 || gsym->is_preemptible())))
6218 target->make_plt_entry(symtab, layout, gsym);
6219 }
6220 // Fall through.
6221
6222 case elfcpp::R_PPC64_REL64:
6223 case elfcpp::R_POWERPC_REL32:
6224 // Make a dynamic relocation if necessary.
6225 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
6226 {
6227 if (!parameters->options().output_is_position_independent()
6228 && gsym->may_need_copy_reloc())
6229 {
6230 target->copy_reloc(symtab, layout, object,
6231 data_shndx, output_section, gsym,
6232 reloc);
6233 }
6234 else
6235 {
6236 Reloc_section* rela_dyn
6237 = target->rela_dyn_section(symtab, layout, is_ifunc);
6238 check_non_pic(object, r_type);
6239 rela_dyn->add_global(gsym, r_type, output_section, object,
6240 data_shndx, reloc.get_r_offset(),
6241 reloc.get_r_addend());
6242 }
6243 }
6244 break;
6245
6246 case elfcpp::R_POWERPC_REL14:
6247 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6248 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6249 if (!is_ifunc)
6250 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
6251 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
6252 reloc.get_r_addend());
6253 break;
6254
6255 case elfcpp::R_POWERPC_REL16:
6256 case elfcpp::R_POWERPC_REL16_LO:
6257 case elfcpp::R_POWERPC_REL16_HI:
6258 case elfcpp::R_POWERPC_REL16_HA:
6259 case elfcpp::R_POWERPC_REL16DX_HA:
6260 case elfcpp::R_POWERPC_SECTOFF:
6261 case elfcpp::R_POWERPC_SECTOFF_LO:
6262 case elfcpp::R_POWERPC_SECTOFF_HI:
6263 case elfcpp::R_POWERPC_SECTOFF_HA:
6264 case elfcpp::R_PPC64_SECTOFF_DS:
6265 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6266 case elfcpp::R_POWERPC_TPREL16:
6267 case elfcpp::R_POWERPC_TPREL16_LO:
6268 case elfcpp::R_POWERPC_TPREL16_HI:
6269 case elfcpp::R_POWERPC_TPREL16_HA:
6270 case elfcpp::R_PPC64_TPREL16_DS:
6271 case elfcpp::R_PPC64_TPREL16_LO_DS:
6272 case elfcpp::R_PPC64_TPREL16_HIGH:
6273 case elfcpp::R_PPC64_TPREL16_HIGHA:
6274 case elfcpp::R_PPC64_TPREL16_HIGHER:
6275 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6276 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6277 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6278 case elfcpp::R_POWERPC_DTPREL16:
6279 case elfcpp::R_POWERPC_DTPREL16_LO:
6280 case elfcpp::R_POWERPC_DTPREL16_HI:
6281 case elfcpp::R_POWERPC_DTPREL16_HA:
6282 case elfcpp::R_PPC64_DTPREL16_DS:
6283 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6284 case elfcpp::R_PPC64_DTPREL16_HIGH:
6285 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6286 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6287 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6288 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6289 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6290 case elfcpp::R_PPC64_TLSGD:
6291 case elfcpp::R_PPC64_TLSLD:
6292 case elfcpp::R_PPC64_ADDR64_LOCAL:
6293 break;
6294
6295 case elfcpp::R_POWERPC_GOT16:
6296 case elfcpp::R_POWERPC_GOT16_LO:
6297 case elfcpp::R_POWERPC_GOT16_HI:
6298 case elfcpp::R_POWERPC_GOT16_HA:
6299 case elfcpp::R_PPC64_GOT16_DS:
6300 case elfcpp::R_PPC64_GOT16_LO_DS:
6301 {
6302 // The symbol requires a GOT entry.
6303 Output_data_got_powerpc<size, big_endian>* got;
6304
6305 got = target->got_section(symtab, layout);
6306 if (gsym->final_value_is_known())
6307 {
6308 if (is_ifunc
6309 && (size == 32 || target->abiversion() >= 2))
6310 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6311 else
6312 got->add_global(gsym, GOT_TYPE_STANDARD);
6313 }
6314 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6315 {
6316 // If we are generating a shared object or a pie, this
6317 // symbol's GOT entry will be set by a dynamic relocation.
6318 unsigned int off = got->add_constant(0);
6319 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6320
6321 Reloc_section* rela_dyn
6322 = target->rela_dyn_section(symtab, layout, is_ifunc);
6323
6324 if (gsym->can_use_relative_reloc(false)
6325 && !((size == 32
6326 || target->abiversion() >= 2)
6327 && gsym->visibility() == elfcpp::STV_PROTECTED
6328 && parameters->options().shared()))
6329 {
6330 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6331 : elfcpp::R_POWERPC_RELATIVE);
6332 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6333 }
6334 else
6335 {
6336 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6337 rela_dyn->add_global(gsym, dynrel, got, off, 0);
6338 }
6339 }
6340 }
6341 break;
6342
6343 case elfcpp::R_PPC64_TOC16:
6344 case elfcpp::R_PPC64_TOC16_LO:
6345 case elfcpp::R_PPC64_TOC16_HI:
6346 case elfcpp::R_PPC64_TOC16_HA:
6347 case elfcpp::R_PPC64_TOC16_DS:
6348 case elfcpp::R_PPC64_TOC16_LO_DS:
6349 // We need a GOT section.
6350 target->got_section(symtab, layout);
6351 break;
6352
6353 case elfcpp::R_POWERPC_GOT_TLSGD16:
6354 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6355 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6356 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6357 {
6358 const bool final = gsym->final_value_is_known();
6359 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6360 if (tls_type == tls::TLSOPT_NONE)
6361 {
6362 Output_data_got_powerpc<size, big_endian>* got
6363 = target->got_section(symtab, layout);
6364 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6365 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
6366 elfcpp::R_POWERPC_DTPMOD,
6367 elfcpp::R_POWERPC_DTPREL);
6368 }
6369 else if (tls_type == tls::TLSOPT_TO_IE)
6370 {
6371 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6372 {
6373 Output_data_got_powerpc<size, big_endian>* got
6374 = target->got_section(symtab, layout);
6375 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6376 if (gsym->is_undefined()
6377 || gsym->is_from_dynobj())
6378 {
6379 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6380 elfcpp::R_POWERPC_TPREL);
6381 }
6382 else
6383 {
6384 unsigned int off = got->add_constant(0);
6385 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6386 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6387 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6388 got, off, 0);
6389 }
6390 }
6391 }
6392 else if (tls_type == tls::TLSOPT_TO_LE)
6393 {
6394 // no GOT relocs needed for Local Exec.
6395 }
6396 else
6397 gold_unreachable();
6398 }
6399 break;
6400
6401 case elfcpp::R_POWERPC_GOT_TLSLD16:
6402 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6403 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6404 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6405 {
6406 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6407 if (tls_type == tls::TLSOPT_NONE)
6408 target->tlsld_got_offset(symtab, layout, object);
6409 else if (tls_type == tls::TLSOPT_TO_LE)
6410 {
6411 // no GOT relocs needed for Local Exec.
6412 if (parameters->options().emit_relocs())
6413 {
6414 Output_section* os = layout->tls_segment()->first_section();
6415 gold_assert(os != NULL);
6416 os->set_needs_symtab_index();
6417 }
6418 }
6419 else
6420 gold_unreachable();
6421 }
6422 break;
6423
6424 case elfcpp::R_POWERPC_GOT_DTPREL16:
6425 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6426 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6427 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6428 {
6429 Output_data_got_powerpc<size, big_endian>* got
6430 = target->got_section(symtab, layout);
6431 if (!gsym->final_value_is_known()
6432 && (gsym->is_from_dynobj()
6433 || gsym->is_undefined()
6434 || gsym->is_preemptible()))
6435 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6436 target->rela_dyn_section(layout),
6437 elfcpp::R_POWERPC_DTPREL);
6438 else
6439 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6440 }
6441 break;
6442
6443 case elfcpp::R_POWERPC_GOT_TPREL16:
6444 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6445 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6446 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6447 {
6448 const bool final = gsym->final_value_is_known();
6449 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6450 if (tls_type == tls::TLSOPT_NONE)
6451 {
6452 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6453 {
6454 Output_data_got_powerpc<size, big_endian>* got
6455 = target->got_section(symtab, layout);
6456 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6457 if (gsym->is_undefined()
6458 || gsym->is_from_dynobj())
6459 {
6460 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6461 elfcpp::R_POWERPC_TPREL);
6462 }
6463 else
6464 {
6465 unsigned int off = got->add_constant(0);
6466 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6467 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6468 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6469 got, off, 0);
6470 }
6471 }
6472 }
6473 else if (tls_type == tls::TLSOPT_TO_LE)
6474 {
6475 // no GOT relocs needed for Local Exec.
6476 }
6477 else
6478 gold_unreachable();
6479 }
6480 break;
6481
6482 default:
6483 unsupported_reloc_global(object, r_type, gsym);
6484 break;
6485 }
6486
6487 switch (r_type)
6488 {
6489 case elfcpp::R_POWERPC_GOT_TLSLD16:
6490 case elfcpp::R_POWERPC_GOT_TLSGD16:
6491 case elfcpp::R_POWERPC_GOT_TPREL16:
6492 case elfcpp::R_POWERPC_GOT_DTPREL16:
6493 case elfcpp::R_POWERPC_GOT16:
6494 case elfcpp::R_PPC64_GOT16_DS:
6495 case elfcpp::R_PPC64_TOC16:
6496 case elfcpp::R_PPC64_TOC16_DS:
6497 ppc_object->set_has_small_toc_reloc();
6498 default:
6499 break;
6500 }
6501 }
6502
6503 // Process relocations for gc.
6504
6505 template<int size, bool big_endian>
6506 void
6507 Target_powerpc<size, big_endian>::gc_process_relocs(
6508 Symbol_table* symtab,
6509 Layout* layout,
6510 Sized_relobj_file<size, big_endian>* object,
6511 unsigned int data_shndx,
6512 unsigned int,
6513 const unsigned char* prelocs,
6514 size_t reloc_count,
6515 Output_section* output_section,
6516 bool needs_special_offset_handling,
6517 size_t local_symbol_count,
6518 const unsigned char* plocal_symbols)
6519 {
6520 typedef Target_powerpc<size, big_endian> Powerpc;
6521 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6522 Classify_reloc;
6523
6524 Powerpc_relobj<size, big_endian>* ppc_object
6525 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6526 if (size == 64)
6527 ppc_object->set_opd_valid();
6528 if (size == 64 && data_shndx == ppc_object->opd_shndx())
6529 {
6530 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
6531 for (p = ppc_object->access_from_map()->begin();
6532 p != ppc_object->access_from_map()->end();
6533 ++p)
6534 {
6535 Address dst_off = p->first;
6536 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6537 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
6538 for (s = p->second.begin(); s != p->second.end(); ++s)
6539 {
6540 Relobj* src_obj = s->first;
6541 unsigned int src_indx = s->second;
6542 symtab->gc()->add_reference(src_obj, src_indx,
6543 ppc_object, dst_indx);
6544 }
6545 p->second.clear();
6546 }
6547 ppc_object->access_from_map()->clear();
6548 ppc_object->process_gc_mark(symtab);
6549 // Don't look at .opd relocs as .opd will reference everything.
6550 return;
6551 }
6552
6553 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6554 symtab,
6555 layout,
6556 this,
6557 object,
6558 data_shndx,
6559 prelocs,
6560 reloc_count,
6561 output_section,
6562 needs_special_offset_handling,
6563 local_symbol_count,
6564 plocal_symbols);
6565 }
6566
6567 // Handle target specific gc actions when adding a gc reference from
6568 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6569 // and DST_OFF. For powerpc64, this adds a referenc to the code
6570 // section of a function descriptor.
6571
6572 template<int size, bool big_endian>
6573 void
6574 Target_powerpc<size, big_endian>::do_gc_add_reference(
6575 Symbol_table* symtab,
6576 Relobj* src_obj,
6577 unsigned int src_shndx,
6578 Relobj* dst_obj,
6579 unsigned int dst_shndx,
6580 Address dst_off) const
6581 {
6582 if (size != 64 || dst_obj->is_dynamic())
6583 return;
6584
6585 Powerpc_relobj<size, big_endian>* ppc_object
6586 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
6587 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
6588 {
6589 if (ppc_object->opd_valid())
6590 {
6591 dst_shndx = ppc_object->get_opd_ent(dst_off);
6592 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
6593 }
6594 else
6595 {
6596 // If we haven't run scan_opd_relocs, we must delay
6597 // processing this function descriptor reference.
6598 ppc_object->add_reference(src_obj, src_shndx, dst_off);
6599 }
6600 }
6601 }
6602
6603 // Add any special sections for this symbol to the gc work list.
6604 // For powerpc64, this adds the code section of a function
6605 // descriptor.
6606
6607 template<int size, bool big_endian>
6608 void
6609 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
6610 Symbol_table* symtab,
6611 Symbol* sym) const
6612 {
6613 if (size == 64)
6614 {
6615 Powerpc_relobj<size, big_endian>* ppc_object
6616 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
6617 bool is_ordinary;
6618 unsigned int shndx = sym->shndx(&is_ordinary);
6619 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
6620 {
6621 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
6622 Address dst_off = gsym->value();
6623 if (ppc_object->opd_valid())
6624 {
6625 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6626 symtab->gc()->worklist().push_back(Section_id(ppc_object,
6627 dst_indx));
6628 }
6629 else
6630 ppc_object->add_gc_mark(dst_off);
6631 }
6632 }
6633 }
6634
6635 // For a symbol location in .opd, set LOC to the location of the
6636 // function entry.
6637
6638 template<int size, bool big_endian>
6639 void
6640 Target_powerpc<size, big_endian>::do_function_location(
6641 Symbol_location* loc) const
6642 {
6643 if (size == 64 && loc->shndx != 0)
6644 {
6645 if (loc->object->is_dynamic())
6646 {
6647 Powerpc_dynobj<size, big_endian>* ppc_object
6648 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
6649 if (loc->shndx == ppc_object->opd_shndx())
6650 {
6651 Address dest_off;
6652 Address off = loc->offset - ppc_object->opd_address();
6653 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
6654 loc->offset = dest_off;
6655 }
6656 }
6657 else
6658 {
6659 const Powerpc_relobj<size, big_endian>* ppc_object
6660 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
6661 if (loc->shndx == ppc_object->opd_shndx())
6662 {
6663 Address dest_off;
6664 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
6665 loc->offset = dest_off;
6666 }
6667 }
6668 }
6669 }
6670
6671 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6672 // compiled with -fsplit-stack. The function calls non-split-stack
6673 // code. Change the function to ensure it has enough stack space to
6674 // call some random function.
6675
6676 template<int size, bool big_endian>
6677 void
6678 Target_powerpc<size, big_endian>::do_calls_non_split(
6679 Relobj* object,
6680 unsigned int shndx,
6681 section_offset_type fnoffset,
6682 section_size_type fnsize,
6683 const unsigned char* prelocs,
6684 size_t reloc_count,
6685 unsigned char* view,
6686 section_size_type view_size,
6687 std::string* from,
6688 std::string* to) const
6689 {
6690 // 32-bit not supported.
6691 if (size == 32)
6692 {
6693 // warn
6694 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6695 prelocs, reloc_count, view, view_size,
6696 from, to);
6697 return;
6698 }
6699
6700 // The function always starts with
6701 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
6702 // addis %r12,%r1,-allocate@ha
6703 // addi %r12,%r12,-allocate@l
6704 // cmpld %r12,%r0
6705 // but note that the addis or addi may be replaced with a nop
6706
6707 unsigned char *entry = view + fnoffset;
6708 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
6709
6710 if ((insn & 0xffff0000) == addis_2_12)
6711 {
6712 /* Skip ELFv2 global entry code. */
6713 entry += 8;
6714 insn = elfcpp::Swap<32, big_endian>::readval(entry);
6715 }
6716
6717 unsigned char *pinsn = entry;
6718 bool ok = false;
6719 const uint32_t ld_private_ss = 0xe80d8fc0;
6720 if (insn == ld_private_ss)
6721 {
6722 int32_t allocate = 0;
6723 while (1)
6724 {
6725 pinsn += 4;
6726 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
6727 if ((insn & 0xffff0000) == addis_12_1)
6728 allocate += (insn & 0xffff) << 16;
6729 else if ((insn & 0xffff0000) == addi_12_1
6730 || (insn & 0xffff0000) == addi_12_12)
6731 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
6732 else if (insn != nop)
6733 break;
6734 }
6735 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
6736 {
6737 int extra = parameters->options().split_stack_adjust_size();
6738 allocate -= extra;
6739 if (allocate >= 0 || extra < 0)
6740 {
6741 object->error(_("split-stack stack size overflow at "
6742 "section %u offset %0zx"),
6743 shndx, static_cast<size_t>(fnoffset));
6744 return;
6745 }
6746 pinsn = entry + 4;
6747 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
6748 if (insn != addis_12_1)
6749 {
6750 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6751 pinsn += 4;
6752 insn = addi_12_12 | (allocate & 0xffff);
6753 if (insn != addi_12_12)
6754 {
6755 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6756 pinsn += 4;
6757 }
6758 }
6759 else
6760 {
6761 insn = addi_12_1 | (allocate & 0xffff);
6762 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6763 pinsn += 4;
6764 }
6765 if (pinsn != entry + 12)
6766 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
6767
6768 ok = true;
6769 }
6770 }
6771
6772 if (!ok)
6773 {
6774 if (!object->has_no_split_stack())
6775 object->error(_("failed to match split-stack sequence at "
6776 "section %u offset %0zx"),
6777 shndx, static_cast<size_t>(fnoffset));
6778 }
6779 }
6780
6781 // Scan relocations for a section.
6782
6783 template<int size, bool big_endian>
6784 void
6785 Target_powerpc<size, big_endian>::scan_relocs(
6786 Symbol_table* symtab,
6787 Layout* layout,
6788 Sized_relobj_file<size, big_endian>* object,
6789 unsigned int data_shndx,
6790 unsigned int sh_type,
6791 const unsigned char* prelocs,
6792 size_t reloc_count,
6793 Output_section* output_section,
6794 bool needs_special_offset_handling,
6795 size_t local_symbol_count,
6796 const unsigned char* plocal_symbols)
6797 {
6798 typedef Target_powerpc<size, big_endian> Powerpc;
6799 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
6800 Classify_reloc;
6801
6802 if (sh_type == elfcpp::SHT_REL)
6803 {
6804 gold_error(_("%s: unsupported REL reloc section"),
6805 object->name().c_str());
6806 return;
6807 }
6808
6809 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
6810 symtab,
6811 layout,
6812 this,
6813 object,
6814 data_shndx,
6815 prelocs,
6816 reloc_count,
6817 output_section,
6818 needs_special_offset_handling,
6819 local_symbol_count,
6820 plocal_symbols);
6821 }
6822
6823 // Functor class for processing the global symbol table.
6824 // Removes symbols defined on discarded opd entries.
6825
6826 template<bool big_endian>
6827 class Global_symbol_visitor_opd
6828 {
6829 public:
6830 Global_symbol_visitor_opd()
6831 { }
6832
6833 void
6834 operator()(Sized_symbol<64>* sym)
6835 {
6836 if (sym->has_symtab_index()
6837 || sym->source() != Symbol::FROM_OBJECT
6838 || !sym->in_real_elf())
6839 return;
6840
6841 if (sym->object()->is_dynamic())
6842 return;
6843
6844 Powerpc_relobj<64, big_endian>* symobj
6845 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6846 if (symobj->opd_shndx() == 0)
6847 return;
6848
6849 bool is_ordinary;
6850 unsigned int shndx = sym->shndx(&is_ordinary);
6851 if (shndx == symobj->opd_shndx()
6852 && symobj->get_opd_discard(sym->value()))
6853 {
6854 sym->set_undefined();
6855 sym->set_visibility(elfcpp::STV_DEFAULT);
6856 sym->set_is_defined_in_discarded_section();
6857 sym->set_symtab_index(-1U);
6858 }
6859 }
6860 };
6861
6862 template<int size, bool big_endian>
6863 void
6864 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6865 Layout* layout,
6866 Symbol_table* symtab)
6867 {
6868 if (size == 64)
6869 {
6870 Output_data_save_res<size, big_endian>* savres
6871 = new Output_data_save_res<size, big_endian>(symtab);
6872 this->savres_section_ = savres;
6873 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6874 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6875 savres, ORDER_TEXT, false);
6876 }
6877 }
6878
6879 // Sort linker created .got section first (for the header), then input
6880 // sections belonging to files using small model code.
6881
6882 template<bool big_endian>
6883 class Sort_toc_sections
6884 {
6885 public:
6886 bool
6887 operator()(const Output_section::Input_section& is1,
6888 const Output_section::Input_section& is2) const
6889 {
6890 if (!is1.is_input_section() && is2.is_input_section())
6891 return true;
6892 bool small1
6893 = (is1.is_input_section()
6894 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6895 ->has_small_toc_reloc()));
6896 bool small2
6897 = (is2.is_input_section()
6898 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6899 ->has_small_toc_reloc()));
6900 return small1 && !small2;
6901 }
6902 };
6903
6904 // Finalize the sections.
6905
6906 template<int size, bool big_endian>
6907 void
6908 Target_powerpc<size, big_endian>::do_finalize_sections(
6909 Layout* layout,
6910 const Input_objects*,
6911 Symbol_table* symtab)
6912 {
6913 if (parameters->doing_static_link())
6914 {
6915 // At least some versions of glibc elf-init.o have a strong
6916 // reference to __rela_iplt marker syms. A weak ref would be
6917 // better..
6918 if (this->iplt_ != NULL)
6919 {
6920 Reloc_section* rel = this->iplt_->rel_plt();
6921 symtab->define_in_output_data("__rela_iplt_start", NULL,
6922 Symbol_table::PREDEFINED, rel, 0, 0,
6923 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6924 elfcpp::STV_HIDDEN, 0, false, true);
6925 symtab->define_in_output_data("__rela_iplt_end", NULL,
6926 Symbol_table::PREDEFINED, rel, 0, 0,
6927 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6928 elfcpp::STV_HIDDEN, 0, true, true);
6929 }
6930 else
6931 {
6932 symtab->define_as_constant("__rela_iplt_start", NULL,
6933 Symbol_table::PREDEFINED, 0, 0,
6934 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6935 elfcpp::STV_HIDDEN, 0, true, false);
6936 symtab->define_as_constant("__rela_iplt_end", NULL,
6937 Symbol_table::PREDEFINED, 0, 0,
6938 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6939 elfcpp::STV_HIDDEN, 0, true, false);
6940 }
6941 }
6942
6943 if (size == 64)
6944 {
6945 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6946 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6947
6948 if (!parameters->options().relocatable())
6949 {
6950 this->define_save_restore_funcs(layout, symtab);
6951
6952 // Annoyingly, we need to make these sections now whether or
6953 // not we need them. If we delay until do_relax then we
6954 // need to mess with the relaxation machinery checkpointing.
6955 this->got_section(symtab, layout);
6956 this->make_brlt_section(layout);
6957
6958 if (parameters->options().toc_sort())
6959 {
6960 Output_section* os = this->got_->output_section();
6961 if (os != NULL && os->input_sections().size() > 1)
6962 std::stable_sort(os->input_sections().begin(),
6963 os->input_sections().end(),
6964 Sort_toc_sections<big_endian>());
6965 }
6966 }
6967 }
6968
6969 // Fill in some more dynamic tags.
6970 Output_data_dynamic* odyn = layout->dynamic_data();
6971 if (odyn != NULL)
6972 {
6973 const Reloc_section* rel_plt = (this->plt_ == NULL
6974 ? NULL
6975 : this->plt_->rel_plt());
6976 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6977 this->rela_dyn_, true, size == 32);
6978
6979 if (size == 32)
6980 {
6981 if (this->got_ != NULL)
6982 {
6983 this->got_->finalize_data_size();
6984 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6985 this->got_, this->got_->g_o_t());
6986 }
6987 }
6988 else
6989 {
6990 if (this->glink_ != NULL)
6991 {
6992 this->glink_->finalize_data_size();
6993 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6994 this->glink_,
6995 (this->glink_->pltresolve_size
6996 - 32));
6997 }
6998 }
6999 }
7000
7001 // Emit any relocs we saved in an attempt to avoid generating COPY
7002 // relocs.
7003 if (this->copy_relocs_.any_saved_relocs())
7004 this->copy_relocs_.emit(this->rela_dyn_section(layout));
7005 }
7006
7007 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7008 // reloc.
7009
7010 static bool
7011 ok_lo_toc_insn(uint32_t insn)
7012 {
7013 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
7014 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7015 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7016 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7017 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7018 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7019 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7020 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7021 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7022 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7023 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7024 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7025 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7026 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7027 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
7028 && (insn & 3) != 1)
7029 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
7030 && ((insn & 3) == 0 || (insn & 3) == 3))
7031 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
7032 }
7033
7034 // Return the value to use for a branch relocation.
7035
7036 template<int size, bool big_endian>
7037 bool
7038 Target_powerpc<size, big_endian>::symval_for_branch(
7039 const Symbol_table* symtab,
7040 const Sized_symbol<size>* gsym,
7041 Powerpc_relobj<size, big_endian>* object,
7042 Address *value,
7043 unsigned int *dest_shndx)
7044 {
7045 if (size == 32 || this->abiversion() >= 2)
7046 gold_unreachable();
7047 *dest_shndx = 0;
7048
7049 // If the symbol is defined in an opd section, ie. is a function
7050 // descriptor, use the function descriptor code entry address
7051 Powerpc_relobj<size, big_endian>* symobj = object;
7052 if (gsym != NULL
7053 && gsym->source() != Symbol::FROM_OBJECT)
7054 return true;
7055 if (gsym != NULL)
7056 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
7057 unsigned int shndx = symobj->opd_shndx();
7058 if (shndx == 0)
7059 return true;
7060 Address opd_addr = symobj->get_output_section_offset(shndx);
7061 if (opd_addr == invalid_address)
7062 return true;
7063 opd_addr += symobj->output_section_address(shndx);
7064 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
7065 {
7066 Address sec_off;
7067 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
7068 if (symtab->is_section_folded(symobj, *dest_shndx))
7069 {
7070 Section_id folded
7071 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
7072 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
7073 *dest_shndx = folded.second;
7074 }
7075 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
7076 if (sec_addr == invalid_address)
7077 return false;
7078
7079 sec_addr += symobj->output_section(*dest_shndx)->address();
7080 *value = sec_addr + sec_off;
7081 }
7082 return true;
7083 }
7084
7085 // Perform a relocation.
7086
7087 template<int size, bool big_endian>
7088 inline bool
7089 Target_powerpc<size, big_endian>::Relocate::relocate(
7090 const Relocate_info<size, big_endian>* relinfo,
7091 unsigned int,
7092 Target_powerpc* target,
7093 Output_section* os,
7094 size_t relnum,
7095 const unsigned char* preloc,
7096 const Sized_symbol<size>* gsym,
7097 const Symbol_value<size>* psymval,
7098 unsigned char* view,
7099 Address address,
7100 section_size_type view_size)
7101 {
7102 if (view == NULL)
7103 return true;
7104
7105 const elfcpp::Rela<size, big_endian> rela(preloc);
7106 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
7107 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
7108 {
7109 case Track_tls::NOT_EXPECTED:
7110 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7111 _("__tls_get_addr call lacks marker reloc"));
7112 break;
7113 case Track_tls::EXPECTED:
7114 // We have already complained.
7115 break;
7116 case Track_tls::SKIP:
7117 return true;
7118 case Track_tls::NORMAL:
7119 break;
7120 }
7121
7122 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
7123 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
7124 typedef typename Reloc_types<elfcpp::SHT_RELA,
7125 size, big_endian>::Reloc Reltype;
7126 // Offset from start of insn to d-field reloc.
7127 const int d_offset = big_endian ? 2 : 0;
7128
7129 Powerpc_relobj<size, big_endian>* const object
7130 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7131 Address value = 0;
7132 bool has_stub_value = false;
7133 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7134 if ((gsym != NULL
7135 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
7136 : object->local_has_plt_offset(r_sym))
7137 && (!psymval->is_ifunc_symbol()
7138 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
7139 {
7140 if (size == 64
7141 && gsym != NULL
7142 && target->abiversion() >= 2
7143 && !parameters->options().output_is_position_independent()
7144 && !is_branch_reloc(r_type))
7145 {
7146 Address off = target->glink_section()->find_global_entry(gsym);
7147 if (off != invalid_address)
7148 {
7149 value = target->glink_section()->global_entry_address() + off;
7150 has_stub_value = true;
7151 }
7152 }
7153 else
7154 {
7155 Stub_table<size, big_endian>* stub_table
7156 = object->stub_table(relinfo->data_shndx);
7157 if (stub_table == NULL)
7158 {
7159 // This is a ref from a data section to an ifunc symbol.
7160 if (target->stub_tables().size() != 0)
7161 stub_table = target->stub_tables()[0];
7162 }
7163 if (stub_table != NULL)
7164 {
7165 Address off;
7166 if (gsym != NULL)
7167 off = stub_table->find_plt_call_entry(object, gsym, r_type,
7168 rela.get_r_addend());
7169 else
7170 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
7171 rela.get_r_addend());
7172 if (off != invalid_address)
7173 {
7174 value = stub_table->stub_address() + off;
7175 has_stub_value = true;
7176 }
7177 }
7178 }
7179 // We don't care too much about bogus debug references to
7180 // non-local functions, but otherwise there had better be a plt
7181 // call stub or global entry stub as appropriate.
7182 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
7183 }
7184
7185 if (r_type == elfcpp::R_POWERPC_GOT16
7186 || r_type == elfcpp::R_POWERPC_GOT16_LO
7187 || r_type == elfcpp::R_POWERPC_GOT16_HI
7188 || r_type == elfcpp::R_POWERPC_GOT16_HA
7189 || r_type == elfcpp::R_PPC64_GOT16_DS
7190 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
7191 {
7192 if (gsym != NULL)
7193 {
7194 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
7195 value = gsym->got_offset(GOT_TYPE_STANDARD);
7196 }
7197 else
7198 {
7199 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7200 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
7201 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
7202 }
7203 value -= target->got_section()->got_base_offset(object);
7204 }
7205 else if (r_type == elfcpp::R_PPC64_TOC)
7206 {
7207 value = (target->got_section()->output_section()->address()
7208 + object->toc_base_offset());
7209 }
7210 else if (gsym != NULL
7211 && (r_type == elfcpp::R_POWERPC_REL24
7212 || r_type == elfcpp::R_PPC_PLTREL24)
7213 && has_stub_value)
7214 {
7215 if (size == 64)
7216 {
7217 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
7218 Valtype* wv = reinterpret_cast<Valtype*>(view);
7219 bool can_plt_call = false;
7220 if (rela.get_r_offset() + 8 <= view_size)
7221 {
7222 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
7223 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
7224 if ((insn & 1) != 0
7225 && (insn2 == nop
7226 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
7227 {
7228 elfcpp::Swap<32, big_endian>::
7229 writeval(wv + 1, ld_2_1 + target->stk_toc());
7230 can_plt_call = true;
7231 }
7232 }
7233 if (!can_plt_call)
7234 {
7235 // If we don't have a branch and link followed by a nop,
7236 // we can't go via the plt because there is no place to
7237 // put a toc restoring instruction.
7238 // Unless we know we won't be returning.
7239 if (strcmp(gsym->name(), "__libc_start_main") == 0)
7240 can_plt_call = true;
7241 }
7242 if (!can_plt_call)
7243 {
7244 // g++ as of 20130507 emits self-calls without a
7245 // following nop. This is arguably wrong since we have
7246 // conflicting information. On the one hand a global
7247 // symbol and on the other a local call sequence, but
7248 // don't error for this special case.
7249 // It isn't possible to cheaply verify we have exactly
7250 // such a call. Allow all calls to the same section.
7251 bool ok = false;
7252 Address code = value;
7253 if (gsym->source() == Symbol::FROM_OBJECT
7254 && gsym->object() == object)
7255 {
7256 unsigned int dest_shndx = 0;
7257 if (target->abiversion() < 2)
7258 {
7259 Address addend = rela.get_r_addend();
7260 code = psymval->value(object, addend);
7261 target->symval_for_branch(relinfo->symtab, gsym, object,
7262 &code, &dest_shndx);
7263 }
7264 bool is_ordinary;
7265 if (dest_shndx == 0)
7266 dest_shndx = gsym->shndx(&is_ordinary);
7267 ok = dest_shndx == relinfo->data_shndx;
7268 }
7269 if (!ok)
7270 {
7271 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7272 _("call lacks nop, can't restore toc; "
7273 "recompile with -fPIC"));
7274 value = code;
7275 }
7276 }
7277 }
7278 }
7279 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7280 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7281 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7282 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7283 {
7284 // First instruction of a global dynamic sequence, arg setup insn.
7285 const bool final = gsym == NULL || gsym->final_value_is_known();
7286 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7287 enum Got_type got_type = GOT_TYPE_STANDARD;
7288 if (tls_type == tls::TLSOPT_NONE)
7289 got_type = GOT_TYPE_TLSGD;
7290 else if (tls_type == tls::TLSOPT_TO_IE)
7291 got_type = GOT_TYPE_TPREL;
7292 if (got_type != GOT_TYPE_STANDARD)
7293 {
7294 if (gsym != NULL)
7295 {
7296 gold_assert(gsym->has_got_offset(got_type));
7297 value = gsym->got_offset(got_type);
7298 }
7299 else
7300 {
7301 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7302 gold_assert(object->local_has_got_offset(r_sym, got_type));
7303 value = object->local_got_offset(r_sym, got_type);
7304 }
7305 value -= target->got_section()->got_base_offset(object);
7306 }
7307 if (tls_type == tls::TLSOPT_TO_IE)
7308 {
7309 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7310 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7311 {
7312 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7313 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7314 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
7315 if (size == 32)
7316 insn |= 32 << 26; // lwz
7317 else
7318 insn |= 58 << 26; // ld
7319 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7320 }
7321 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7322 - elfcpp::R_POWERPC_GOT_TLSGD16);
7323 }
7324 else if (tls_type == tls::TLSOPT_TO_LE)
7325 {
7326 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7327 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7328 {
7329 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7330 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7331 insn &= (1 << 26) - (1 << 21); // extract rt
7332 if (size == 32)
7333 insn |= addis_0_2;
7334 else
7335 insn |= addis_0_13;
7336 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7337 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7338 value = psymval->value(object, rela.get_r_addend());
7339 }
7340 else
7341 {
7342 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7343 Insn insn = nop;
7344 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7345 r_type = elfcpp::R_POWERPC_NONE;
7346 }
7347 }
7348 }
7349 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7350 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7351 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7352 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7353 {
7354 // First instruction of a local dynamic sequence, arg setup insn.
7355 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7356 if (tls_type == tls::TLSOPT_NONE)
7357 {
7358 value = target->tlsld_got_offset();
7359 value -= target->got_section()->got_base_offset(object);
7360 }
7361 else
7362 {
7363 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7364 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7365 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7366 {
7367 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7368 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7369 insn &= (1 << 26) - (1 << 21); // extract rt
7370 if (size == 32)
7371 insn |= addis_0_2;
7372 else
7373 insn |= addis_0_13;
7374 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7375 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7376 value = dtp_offset;
7377 }
7378 else
7379 {
7380 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7381 Insn insn = nop;
7382 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7383 r_type = elfcpp::R_POWERPC_NONE;
7384 }
7385 }
7386 }
7387 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
7388 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
7389 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
7390 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
7391 {
7392 // Accesses relative to a local dynamic sequence address,
7393 // no optimisation here.
7394 if (gsym != NULL)
7395 {
7396 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
7397 value = gsym->got_offset(GOT_TYPE_DTPREL);
7398 }
7399 else
7400 {
7401 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7402 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
7403 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
7404 }
7405 value -= target->got_section()->got_base_offset(object);
7406 }
7407 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7408 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7409 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7410 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7411 {
7412 // First instruction of initial exec sequence.
7413 const bool final = gsym == NULL || gsym->final_value_is_known();
7414 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7415 if (tls_type == tls::TLSOPT_NONE)
7416 {
7417 if (gsym != NULL)
7418 {
7419 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
7420 value = gsym->got_offset(GOT_TYPE_TPREL);
7421 }
7422 else
7423 {
7424 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7425 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
7426 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
7427 }
7428 value -= target->got_section()->got_base_offset(object);
7429 }
7430 else
7431 {
7432 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7433 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7434 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7435 {
7436 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7437 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7438 insn &= (1 << 26) - (1 << 21); // extract rt from ld
7439 if (size == 32)
7440 insn |= addis_0_2;
7441 else
7442 insn |= addis_0_13;
7443 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7444 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7445 value = psymval->value(object, rela.get_r_addend());
7446 }
7447 else
7448 {
7449 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7450 Insn insn = nop;
7451 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7452 r_type = elfcpp::R_POWERPC_NONE;
7453 }
7454 }
7455 }
7456 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7457 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7458 {
7459 // Second instruction of a global dynamic sequence,
7460 // the __tls_get_addr call
7461 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7462 const bool final = gsym == NULL || gsym->final_value_is_known();
7463 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7464 if (tls_type != tls::TLSOPT_NONE)
7465 {
7466 if (tls_type == tls::TLSOPT_TO_IE)
7467 {
7468 Insn* iview = reinterpret_cast<Insn*>(view);
7469 Insn insn = add_3_3_13;
7470 if (size == 32)
7471 insn = add_3_3_2;
7472 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7473 r_type = elfcpp::R_POWERPC_NONE;
7474 }
7475 else
7476 {
7477 Insn* iview = reinterpret_cast<Insn*>(view);
7478 Insn insn = addi_3_3;
7479 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7480 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7481 view += d_offset;
7482 value = psymval->value(object, rela.get_r_addend());
7483 }
7484 this->skip_next_tls_get_addr_call();
7485 }
7486 }
7487 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7488 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7489 {
7490 // Second instruction of a local dynamic sequence,
7491 // the __tls_get_addr call
7492 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7493 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7494 if (tls_type == tls::TLSOPT_TO_LE)
7495 {
7496 Insn* iview = reinterpret_cast<Insn*>(view);
7497 Insn insn = addi_3_3;
7498 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7499 this->skip_next_tls_get_addr_call();
7500 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7501 view += d_offset;
7502 value = dtp_offset;
7503 }
7504 }
7505 else if (r_type == elfcpp::R_POWERPC_TLS)
7506 {
7507 // Second instruction of an initial exec sequence
7508 const bool final = gsym == NULL || gsym->final_value_is_known();
7509 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7510 if (tls_type == tls::TLSOPT_TO_LE)
7511 {
7512 Insn* iview = reinterpret_cast<Insn*>(view);
7513 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7514 unsigned int reg = size == 32 ? 2 : 13;
7515 insn = at_tls_transform(insn, reg);
7516 gold_assert(insn != 0);
7517 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7518 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7519 view += d_offset;
7520 value = psymval->value(object, rela.get_r_addend());
7521 }
7522 }
7523 else if (!has_stub_value)
7524 {
7525 Address addend = 0;
7526 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
7527 addend = rela.get_r_addend();
7528 value = psymval->value(object, addend);
7529 if (size == 64 && is_branch_reloc(r_type))
7530 {
7531 if (target->abiversion() >= 2)
7532 {
7533 if (gsym != NULL)
7534 value += object->ppc64_local_entry_offset(gsym);
7535 else
7536 value += object->ppc64_local_entry_offset(r_sym);
7537 }
7538 else
7539 {
7540 unsigned int dest_shndx;
7541 target->symval_for_branch(relinfo->symtab, gsym, object,
7542 &value, &dest_shndx);
7543 }
7544 }
7545 Address max_branch_offset = max_branch_delta(r_type);
7546 if (max_branch_offset != 0
7547 && value - address + max_branch_offset >= 2 * max_branch_offset)
7548 {
7549 Stub_table<size, big_endian>* stub_table
7550 = object->stub_table(relinfo->data_shndx);
7551 if (stub_table != NULL)
7552 {
7553 Address off = stub_table->find_long_branch_entry(object, value);
7554 if (off != invalid_address)
7555 {
7556 value = (stub_table->stub_address() + stub_table->plt_size()
7557 + off);
7558 has_stub_value = true;
7559 }
7560 }
7561 }
7562 }
7563
7564 switch (r_type)
7565 {
7566 case elfcpp::R_PPC64_REL64:
7567 case elfcpp::R_POWERPC_REL32:
7568 case elfcpp::R_POWERPC_REL24:
7569 case elfcpp::R_PPC_PLTREL24:
7570 case elfcpp::R_PPC_LOCAL24PC:
7571 case elfcpp::R_POWERPC_REL16:
7572 case elfcpp::R_POWERPC_REL16_LO:
7573 case elfcpp::R_POWERPC_REL16_HI:
7574 case elfcpp::R_POWERPC_REL16_HA:
7575 case elfcpp::R_POWERPC_REL16DX_HA:
7576 case elfcpp::R_POWERPC_REL14:
7577 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7578 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7579 value -= address;
7580 break;
7581
7582 case elfcpp::R_PPC64_TOC16:
7583 case elfcpp::R_PPC64_TOC16_LO:
7584 case elfcpp::R_PPC64_TOC16_HI:
7585 case elfcpp::R_PPC64_TOC16_HA:
7586 case elfcpp::R_PPC64_TOC16_DS:
7587 case elfcpp::R_PPC64_TOC16_LO_DS:
7588 // Subtract the TOC base address.
7589 value -= (target->got_section()->output_section()->address()
7590 + object->toc_base_offset());
7591 break;
7592
7593 case elfcpp::R_POWERPC_SECTOFF:
7594 case elfcpp::R_POWERPC_SECTOFF_LO:
7595 case elfcpp::R_POWERPC_SECTOFF_HI:
7596 case elfcpp::R_POWERPC_SECTOFF_HA:
7597 case elfcpp::R_PPC64_SECTOFF_DS:
7598 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7599 if (os != NULL)
7600 value -= os->address();
7601 break;
7602
7603 case elfcpp::R_PPC64_TPREL16_DS:
7604 case elfcpp::R_PPC64_TPREL16_LO_DS:
7605 case elfcpp::R_PPC64_TPREL16_HIGH:
7606 case elfcpp::R_PPC64_TPREL16_HIGHA:
7607 if (size != 64)
7608 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7609 break;
7610 // Fall through.
7611 case elfcpp::R_POWERPC_TPREL16:
7612 case elfcpp::R_POWERPC_TPREL16_LO:
7613 case elfcpp::R_POWERPC_TPREL16_HI:
7614 case elfcpp::R_POWERPC_TPREL16_HA:
7615 case elfcpp::R_POWERPC_TPREL:
7616 case elfcpp::R_PPC64_TPREL16_HIGHER:
7617 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7618 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7619 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7620 // tls symbol values are relative to tls_segment()->vaddr()
7621 value -= tp_offset;
7622 break;
7623
7624 case elfcpp::R_PPC64_DTPREL16_DS:
7625 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7626 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7627 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7628 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7629 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7630 if (size != 64)
7631 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7632 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7633 break;
7634 // Fall through.
7635 case elfcpp::R_POWERPC_DTPREL16:
7636 case elfcpp::R_POWERPC_DTPREL16_LO:
7637 case elfcpp::R_POWERPC_DTPREL16_HI:
7638 case elfcpp::R_POWERPC_DTPREL16_HA:
7639 case elfcpp::R_POWERPC_DTPREL:
7640 case elfcpp::R_PPC64_DTPREL16_HIGH:
7641 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7642 // tls symbol values are relative to tls_segment()->vaddr()
7643 value -= dtp_offset;
7644 break;
7645
7646 case elfcpp::R_PPC64_ADDR64_LOCAL:
7647 if (gsym != NULL)
7648 value += object->ppc64_local_entry_offset(gsym);
7649 else
7650 value += object->ppc64_local_entry_offset(r_sym);
7651 break;
7652
7653 default:
7654 break;
7655 }
7656
7657 Insn branch_bit = 0;
7658 switch (r_type)
7659 {
7660 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7661 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7662 branch_bit = 1 << 21;
7663 // Fall through.
7664 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7665 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7666 {
7667 Insn* iview = reinterpret_cast<Insn*>(view);
7668 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7669 insn &= ~(1 << 21);
7670 insn |= branch_bit;
7671 if (this->is_isa_v2)
7672 {
7673 // Set 'a' bit. This is 0b00010 in BO field for branch
7674 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7675 // for branch on CTR insns (BO == 1a00t or 1a01t).
7676 if ((insn & (0x14 << 21)) == (0x04 << 21))
7677 insn |= 0x02 << 21;
7678 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7679 insn |= 0x08 << 21;
7680 else
7681 break;
7682 }
7683 else
7684 {
7685 // Invert 'y' bit if not the default.
7686 if (static_cast<Signed_address>(value) < 0)
7687 insn ^= 1 << 21;
7688 }
7689 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7690 }
7691 break;
7692
7693 default:
7694 break;
7695 }
7696
7697 if (size == 64)
7698 {
7699 // Multi-instruction sequences that access the TOC can be
7700 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7701 // to nop; addi rb,r2,x;
7702 switch (r_type)
7703 {
7704 default:
7705 break;
7706
7707 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7708 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7709 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7710 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7711 case elfcpp::R_POWERPC_GOT16_HA:
7712 case elfcpp::R_PPC64_TOC16_HA:
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 ((insn & ((0x3f << 26) | 0x1f << 16))
7718 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7719 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7720 _("toc optimization is not supported "
7721 "for %#08x instruction"), insn);
7722 else if (value + 0x8000 < 0x10000)
7723 {
7724 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
7725 return true;
7726 }
7727 }
7728 break;
7729
7730 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7731 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7732 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7733 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7734 case elfcpp::R_POWERPC_GOT16_LO:
7735 case elfcpp::R_PPC64_GOT16_LO_DS:
7736 case elfcpp::R_PPC64_TOC16_LO:
7737 case elfcpp::R_PPC64_TOC16_LO_DS:
7738 if (parameters->options().toc_optimize())
7739 {
7740 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7741 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7742 if (!ok_lo_toc_insn(insn))
7743 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7744 _("toc optimization is not supported "
7745 "for %#08x instruction"), insn);
7746 else if (value + 0x8000 < 0x10000)
7747 {
7748 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
7749 {
7750 // Transform addic to addi when we change reg.
7751 insn &= ~((0x3f << 26) | (0x1f << 16));
7752 insn |= (14u << 26) | (2 << 16);
7753 }
7754 else
7755 {
7756 insn &= ~(0x1f << 16);
7757 insn |= 2 << 16;
7758 }
7759 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7760 }
7761 }
7762 break;
7763
7764 case elfcpp::R_PPC64_ENTRY:
7765 value = (target->got_section()->output_section()->address()
7766 + object->toc_base_offset());
7767 if (value + 0x80008000 <= 0xffffffff
7768 && !parameters->options().output_is_position_independent())
7769 {
7770 Insn* iview = reinterpret_cast<Insn*>(view);
7771 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
7772 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
7773
7774 if ((insn1 & ~0xfffc) == ld_2_12
7775 && insn2 == add_2_2_12)
7776 {
7777 insn1 = lis_2 + ha(value);
7778 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
7779 insn2 = addi_2_2 + l(value);
7780 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
7781 return true;
7782 }
7783 }
7784 else
7785 {
7786 value -= address;
7787 if (value + 0x80008000 <= 0xffffffff)
7788 {
7789 Insn* iview = reinterpret_cast<Insn*>(view);
7790 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
7791 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
7792
7793 if ((insn1 & ~0xfffc) == ld_2_12
7794 && insn2 == add_2_2_12)
7795 {
7796 insn1 = addis_2_12 + ha(value);
7797 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
7798 insn2 = addi_2_2 + l(value);
7799 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
7800 return true;
7801 }
7802 }
7803 }
7804 break;
7805
7806 case elfcpp::R_POWERPC_REL16_LO:
7807 // If we are generating a non-PIC executable, edit
7808 // 0: addis 2,12,.TOC.-0b@ha
7809 // addi 2,2,.TOC.-0b@l
7810 // used by ELFv2 global entry points to set up r2, to
7811 // lis 2,.TOC.@ha
7812 // addi 2,2,.TOC.@l
7813 // if .TOC. is in range. */
7814 if (value + address - 4 + 0x80008000 <= 0xffffffff
7815 && relnum != 0
7816 && preloc != NULL
7817 && target->abiversion() >= 2
7818 && !parameters->options().output_is_position_independent()
7819 && rela.get_r_addend() == d_offset + 4
7820 && gsym != NULL
7821 && strcmp(gsym->name(), ".TOC.") == 0)
7822 {
7823 const int reloc_size
7824 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7825 Reltype prev_rela(preloc - reloc_size);
7826 if ((prev_rela.get_r_info()
7827 == elfcpp::elf_r_info<size>(r_sym,
7828 elfcpp::R_POWERPC_REL16_HA))
7829 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
7830 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
7831 {
7832 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7833 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
7834 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
7835
7836 if ((insn1 & 0xffff0000) == addis_2_12
7837 && (insn2 & 0xffff0000) == addi_2_2)
7838 {
7839 insn1 = lis_2 + ha(value + address - 4);
7840 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
7841 insn2 = addi_2_2 + l(value + address - 4);
7842 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
7843 if (relinfo->rr)
7844 {
7845 relinfo->rr->set_strategy(relnum - 1,
7846 Relocatable_relocs::RELOC_SPECIAL);
7847 relinfo->rr->set_strategy(relnum,
7848 Relocatable_relocs::RELOC_SPECIAL);
7849 }
7850 return true;
7851 }
7852 }
7853 }
7854 break;
7855 }
7856 }
7857
7858 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
7859 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
7860 switch (r_type)
7861 {
7862 case elfcpp::R_POWERPC_ADDR32:
7863 case elfcpp::R_POWERPC_UADDR32:
7864 if (size == 64)
7865 overflow = Reloc::CHECK_BITFIELD;
7866 break;
7867
7868 case elfcpp::R_POWERPC_REL32:
7869 case elfcpp::R_POWERPC_REL16DX_HA:
7870 if (size == 64)
7871 overflow = Reloc::CHECK_SIGNED;
7872 break;
7873
7874 case elfcpp::R_POWERPC_UADDR16:
7875 overflow = Reloc::CHECK_BITFIELD;
7876 break;
7877
7878 case elfcpp::R_POWERPC_ADDR16:
7879 // We really should have three separate relocations,
7880 // one for 16-bit data, one for insns with 16-bit signed fields,
7881 // and one for insns with 16-bit unsigned fields.
7882 overflow = Reloc::CHECK_BITFIELD;
7883 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
7884 overflow = Reloc::CHECK_LOW_INSN;
7885 break;
7886
7887 case elfcpp::R_POWERPC_ADDR16_HI:
7888 case elfcpp::R_POWERPC_ADDR16_HA:
7889 case elfcpp::R_POWERPC_GOT16_HI:
7890 case elfcpp::R_POWERPC_GOT16_HA:
7891 case elfcpp::R_POWERPC_PLT16_HI:
7892 case elfcpp::R_POWERPC_PLT16_HA:
7893 case elfcpp::R_POWERPC_SECTOFF_HI:
7894 case elfcpp::R_POWERPC_SECTOFF_HA:
7895 case elfcpp::R_PPC64_TOC16_HI:
7896 case elfcpp::R_PPC64_TOC16_HA:
7897 case elfcpp::R_PPC64_PLTGOT16_HI:
7898 case elfcpp::R_PPC64_PLTGOT16_HA:
7899 case elfcpp::R_POWERPC_TPREL16_HI:
7900 case elfcpp::R_POWERPC_TPREL16_HA:
7901 case elfcpp::R_POWERPC_DTPREL16_HI:
7902 case elfcpp::R_POWERPC_DTPREL16_HA:
7903 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7904 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7905 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7906 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7907 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7908 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7909 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7910 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7911 case elfcpp::R_POWERPC_REL16_HI:
7912 case elfcpp::R_POWERPC_REL16_HA:
7913 if (size != 32)
7914 overflow = Reloc::CHECK_HIGH_INSN;
7915 break;
7916
7917 case elfcpp::R_POWERPC_REL16:
7918 case elfcpp::R_PPC64_TOC16:
7919 case elfcpp::R_POWERPC_GOT16:
7920 case elfcpp::R_POWERPC_SECTOFF:
7921 case elfcpp::R_POWERPC_TPREL16:
7922 case elfcpp::R_POWERPC_DTPREL16:
7923 case elfcpp::R_POWERPC_GOT_TLSGD16:
7924 case elfcpp::R_POWERPC_GOT_TLSLD16:
7925 case elfcpp::R_POWERPC_GOT_TPREL16:
7926 case elfcpp::R_POWERPC_GOT_DTPREL16:
7927 overflow = Reloc::CHECK_LOW_INSN;
7928 break;
7929
7930 case elfcpp::R_POWERPC_ADDR24:
7931 case elfcpp::R_POWERPC_ADDR14:
7932 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7933 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7934 case elfcpp::R_PPC64_ADDR16_DS:
7935 case elfcpp::R_POWERPC_REL24:
7936 case elfcpp::R_PPC_PLTREL24:
7937 case elfcpp::R_PPC_LOCAL24PC:
7938 case elfcpp::R_PPC64_TPREL16_DS:
7939 case elfcpp::R_PPC64_DTPREL16_DS:
7940 case elfcpp::R_PPC64_TOC16_DS:
7941 case elfcpp::R_PPC64_GOT16_DS:
7942 case elfcpp::R_PPC64_SECTOFF_DS:
7943 case elfcpp::R_POWERPC_REL14:
7944 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7945 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7946 overflow = Reloc::CHECK_SIGNED;
7947 break;
7948 }
7949
7950 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
7951 Insn insn = 0;
7952
7953 if (overflow == Reloc::CHECK_LOW_INSN
7954 || overflow == Reloc::CHECK_HIGH_INSN)
7955 {
7956 insn = elfcpp::Swap<32, big_endian>::readval(iview);
7957
7958 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
7959 overflow = Reloc::CHECK_BITFIELD;
7960 else if (overflow == Reloc::CHECK_LOW_INSN
7961 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
7962 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
7963 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
7964 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
7965 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
7966 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
7967 overflow = Reloc::CHECK_UNSIGNED;
7968 else
7969 overflow = Reloc::CHECK_SIGNED;
7970 }
7971
7972 bool maybe_dq_reloc = false;
7973 typename Powerpc_relocate_functions<size, big_endian>::Status status
7974 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
7975 switch (r_type)
7976 {
7977 case elfcpp::R_POWERPC_NONE:
7978 case elfcpp::R_POWERPC_TLS:
7979 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7980 case elfcpp::R_POWERPC_GNU_VTENTRY:
7981 break;
7982
7983 case elfcpp::R_PPC64_ADDR64:
7984 case elfcpp::R_PPC64_REL64:
7985 case elfcpp::R_PPC64_TOC:
7986 case elfcpp::R_PPC64_ADDR64_LOCAL:
7987 Reloc::addr64(view, value);
7988 break;
7989
7990 case elfcpp::R_POWERPC_TPREL:
7991 case elfcpp::R_POWERPC_DTPREL:
7992 if (size == 64)
7993 Reloc::addr64(view, value);
7994 else
7995 status = Reloc::addr32(view, value, overflow);
7996 break;
7997
7998 case elfcpp::R_PPC64_UADDR64:
7999 Reloc::addr64_u(view, value);
8000 break;
8001
8002 case elfcpp::R_POWERPC_ADDR32:
8003 status = Reloc::addr32(view, value, overflow);
8004 break;
8005
8006 case elfcpp::R_POWERPC_REL32:
8007 case elfcpp::R_POWERPC_UADDR32:
8008 status = Reloc::addr32_u(view, value, overflow);
8009 break;
8010
8011 case elfcpp::R_POWERPC_ADDR24:
8012 case elfcpp::R_POWERPC_REL24:
8013 case elfcpp::R_PPC_PLTREL24:
8014 case elfcpp::R_PPC_LOCAL24PC:
8015 status = Reloc::addr24(view, value, overflow);
8016 break;
8017
8018 case elfcpp::R_POWERPC_GOT_DTPREL16:
8019 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8020 case elfcpp::R_POWERPC_GOT_TPREL16:
8021 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8022 if (size == 64)
8023 {
8024 // On ppc64 these are all ds form
8025 maybe_dq_reloc = true;
8026 break;
8027 }
8028 // Fall through.
8029 case elfcpp::R_POWERPC_ADDR16:
8030 case elfcpp::R_POWERPC_REL16:
8031 case elfcpp::R_PPC64_TOC16:
8032 case elfcpp::R_POWERPC_GOT16:
8033 case elfcpp::R_POWERPC_SECTOFF:
8034 case elfcpp::R_POWERPC_TPREL16:
8035 case elfcpp::R_POWERPC_DTPREL16:
8036 case elfcpp::R_POWERPC_GOT_TLSGD16:
8037 case elfcpp::R_POWERPC_GOT_TLSLD16:
8038 case elfcpp::R_POWERPC_ADDR16_LO:
8039 case elfcpp::R_POWERPC_REL16_LO:
8040 case elfcpp::R_PPC64_TOC16_LO:
8041 case elfcpp::R_POWERPC_GOT16_LO:
8042 case elfcpp::R_POWERPC_SECTOFF_LO:
8043 case elfcpp::R_POWERPC_TPREL16_LO:
8044 case elfcpp::R_POWERPC_DTPREL16_LO:
8045 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8046 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8047 if (size == 64)
8048 status = Reloc::addr16(view, value, overflow);
8049 else
8050 maybe_dq_reloc = true;
8051 break;
8052
8053 case elfcpp::R_POWERPC_UADDR16:
8054 status = Reloc::addr16_u(view, value, overflow);
8055 break;
8056
8057 case elfcpp::R_PPC64_ADDR16_HIGH:
8058 case elfcpp::R_PPC64_TPREL16_HIGH:
8059 case elfcpp::R_PPC64_DTPREL16_HIGH:
8060 if (size == 32)
8061 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
8062 goto unsupp;
8063 // Fall through.
8064 case elfcpp::R_POWERPC_ADDR16_HI:
8065 case elfcpp::R_POWERPC_REL16_HI:
8066 case elfcpp::R_PPC64_TOC16_HI:
8067 case elfcpp::R_POWERPC_GOT16_HI:
8068 case elfcpp::R_POWERPC_SECTOFF_HI:
8069 case elfcpp::R_POWERPC_TPREL16_HI:
8070 case elfcpp::R_POWERPC_DTPREL16_HI:
8071 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8072 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8073 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8074 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8075 Reloc::addr16_hi(view, value);
8076 break;
8077
8078 case elfcpp::R_PPC64_ADDR16_HIGHA:
8079 case elfcpp::R_PPC64_TPREL16_HIGHA:
8080 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8081 if (size == 32)
8082 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
8083 goto unsupp;
8084 // Fall through.
8085 case elfcpp::R_POWERPC_ADDR16_HA:
8086 case elfcpp::R_POWERPC_REL16_HA:
8087 case elfcpp::R_PPC64_TOC16_HA:
8088 case elfcpp::R_POWERPC_GOT16_HA:
8089 case elfcpp::R_POWERPC_SECTOFF_HA:
8090 case elfcpp::R_POWERPC_TPREL16_HA:
8091 case elfcpp::R_POWERPC_DTPREL16_HA:
8092 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8093 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8094 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8095 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8096 Reloc::addr16_ha(view, value);
8097 break;
8098
8099 case elfcpp::R_POWERPC_REL16DX_HA:
8100 status = Reloc::addr16dx_ha(view, value, overflow);
8101 break;
8102
8103 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8104 if (size == 32)
8105 // R_PPC_EMB_NADDR16_LO
8106 goto unsupp;
8107 // Fall through.
8108 case elfcpp::R_PPC64_ADDR16_HIGHER:
8109 case elfcpp::R_PPC64_TPREL16_HIGHER:
8110 Reloc::addr16_hi2(view, value);
8111 break;
8112
8113 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8114 if (size == 32)
8115 // R_PPC_EMB_NADDR16_HI
8116 goto unsupp;
8117 // Fall through.
8118 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8119 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8120 Reloc::addr16_ha2(view, value);
8121 break;
8122
8123 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8124 if (size == 32)
8125 // R_PPC_EMB_NADDR16_HA
8126 goto unsupp;
8127 // Fall through.
8128 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8129 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8130 Reloc::addr16_hi3(view, value);
8131 break;
8132
8133 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8134 if (size == 32)
8135 // R_PPC_EMB_SDAI16
8136 goto unsupp;
8137 // Fall through.
8138 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8139 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8140 Reloc::addr16_ha3(view, value);
8141 break;
8142
8143 case elfcpp::R_PPC64_DTPREL16_DS:
8144 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8145 if (size == 32)
8146 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
8147 goto unsupp;
8148 // Fall through.
8149 case elfcpp::R_PPC64_TPREL16_DS:
8150 case elfcpp::R_PPC64_TPREL16_LO_DS:
8151 if (size == 32)
8152 // R_PPC_TLSGD, R_PPC_TLSLD
8153 break;
8154 // Fall through.
8155 case elfcpp::R_PPC64_ADDR16_DS:
8156 case elfcpp::R_PPC64_ADDR16_LO_DS:
8157 case elfcpp::R_PPC64_TOC16_DS:
8158 case elfcpp::R_PPC64_TOC16_LO_DS:
8159 case elfcpp::R_PPC64_GOT16_DS:
8160 case elfcpp::R_PPC64_GOT16_LO_DS:
8161 case elfcpp::R_PPC64_SECTOFF_DS:
8162 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8163 maybe_dq_reloc = true;
8164 break;
8165
8166 case elfcpp::R_POWERPC_ADDR14:
8167 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8168 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8169 case elfcpp::R_POWERPC_REL14:
8170 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8171 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8172 status = Reloc::addr14(view, value, overflow);
8173 break;
8174
8175 case elfcpp::R_POWERPC_COPY:
8176 case elfcpp::R_POWERPC_GLOB_DAT:
8177 case elfcpp::R_POWERPC_JMP_SLOT:
8178 case elfcpp::R_POWERPC_RELATIVE:
8179 case elfcpp::R_POWERPC_DTPMOD:
8180 case elfcpp::R_PPC64_JMP_IREL:
8181 case elfcpp::R_POWERPC_IRELATIVE:
8182 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8183 _("unexpected reloc %u in object file"),
8184 r_type);
8185 break;
8186
8187 case elfcpp::R_PPC_EMB_SDA21:
8188 if (size == 32)
8189 goto unsupp;
8190 else
8191 {
8192 // R_PPC64_TOCSAVE. For the time being this can be ignored.
8193 }
8194 break;
8195
8196 case elfcpp::R_PPC_EMB_SDA2I16:
8197 case elfcpp::R_PPC_EMB_SDA2REL:
8198 if (size == 32)
8199 goto unsupp;
8200 // R_PPC64_TLSGD, R_PPC64_TLSLD
8201 break;
8202
8203 case elfcpp::R_POWERPC_PLT32:
8204 case elfcpp::R_POWERPC_PLTREL32:
8205 case elfcpp::R_POWERPC_PLT16_LO:
8206 case elfcpp::R_POWERPC_PLT16_HI:
8207 case elfcpp::R_POWERPC_PLT16_HA:
8208 case elfcpp::R_PPC_SDAREL16:
8209 case elfcpp::R_POWERPC_ADDR30:
8210 case elfcpp::R_PPC64_PLT64:
8211 case elfcpp::R_PPC64_PLTREL64:
8212 case elfcpp::R_PPC64_PLTGOT16:
8213 case elfcpp::R_PPC64_PLTGOT16_LO:
8214 case elfcpp::R_PPC64_PLTGOT16_HI:
8215 case elfcpp::R_PPC64_PLTGOT16_HA:
8216 case elfcpp::R_PPC64_PLT16_LO_DS:
8217 case elfcpp::R_PPC64_PLTGOT16_DS:
8218 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
8219 case elfcpp::R_PPC_EMB_RELSDA:
8220 case elfcpp::R_PPC_TOC16:
8221 default:
8222 unsupp:
8223 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8224 _("unsupported reloc %u"),
8225 r_type);
8226 break;
8227 }
8228
8229 if (maybe_dq_reloc)
8230 {
8231 if (insn == 0)
8232 insn = elfcpp::Swap<32, big_endian>::readval(iview);
8233
8234 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
8235 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
8236 && (insn & 3) == 1))
8237 status = Reloc::addr16_dq(view, value, overflow);
8238 else if (size == 64
8239 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
8240 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
8241 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
8242 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
8243 status = Reloc::addr16_ds(view, value, overflow);
8244 else
8245 status = Reloc::addr16(view, value, overflow);
8246 }
8247
8248 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
8249 && (has_stub_value
8250 || !(gsym != NULL
8251 && gsym->is_undefined()
8252 && is_branch_reloc(r_type))))
8253 {
8254 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
8255 _("relocation overflow"));
8256 if (has_stub_value)
8257 gold_info(_("try relinking with a smaller --stub-group-size"));
8258 }
8259
8260 return true;
8261 }
8262
8263 // Relocate section data.
8264
8265 template<int size, bool big_endian>
8266 void
8267 Target_powerpc<size, big_endian>::relocate_section(
8268 const Relocate_info<size, big_endian>* relinfo,
8269 unsigned int sh_type,
8270 const unsigned char* prelocs,
8271 size_t reloc_count,
8272 Output_section* output_section,
8273 bool needs_special_offset_handling,
8274 unsigned char* view,
8275 Address address,
8276 section_size_type view_size,
8277 const Reloc_symbol_changes* reloc_symbol_changes)
8278 {
8279 typedef Target_powerpc<size, big_endian> Powerpc;
8280 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
8281 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
8282 Powerpc_comdat_behavior;
8283 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8284 Classify_reloc;
8285
8286 gold_assert(sh_type == elfcpp::SHT_RELA);
8287
8288 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
8289 Powerpc_comdat_behavior, Classify_reloc>(
8290 relinfo,
8291 this,
8292 prelocs,
8293 reloc_count,
8294 output_section,
8295 needs_special_offset_handling,
8296 view,
8297 address,
8298 view_size,
8299 reloc_symbol_changes);
8300 }
8301
8302 template<int size, bool big_endian>
8303 class Powerpc_scan_relocatable_reloc
8304 {
8305 public:
8306 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
8307 Reltype;
8308 static const int reloc_size =
8309 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
8310 static const int sh_type = elfcpp::SHT_RELA;
8311
8312 // Return the symbol referred to by the relocation.
8313 static inline unsigned int
8314 get_r_sym(const Reltype* reloc)
8315 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
8316
8317 // Return the type of the relocation.
8318 static inline unsigned int
8319 get_r_type(const Reltype* reloc)
8320 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
8321
8322 // Return the strategy to use for a local symbol which is not a
8323 // section symbol, given the relocation type.
8324 inline Relocatable_relocs::Reloc_strategy
8325 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
8326 {
8327 if (r_type == 0 && r_sym == 0)
8328 return Relocatable_relocs::RELOC_DISCARD;
8329 return Relocatable_relocs::RELOC_COPY;
8330 }
8331
8332 // Return the strategy to use for a local symbol which is a section
8333 // symbol, given the relocation type.
8334 inline Relocatable_relocs::Reloc_strategy
8335 local_section_strategy(unsigned int, Relobj*)
8336 {
8337 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
8338 }
8339
8340 // Return the strategy to use for a global symbol, given the
8341 // relocation type, the object, and the symbol index.
8342 inline Relocatable_relocs::Reloc_strategy
8343 global_strategy(unsigned int r_type, Relobj*, unsigned int)
8344 {
8345 if (r_type == elfcpp::R_PPC_PLTREL24)
8346 return Relocatable_relocs::RELOC_SPECIAL;
8347 return Relocatable_relocs::RELOC_COPY;
8348 }
8349 };
8350
8351 // Scan the relocs during a relocatable link.
8352
8353 template<int size, bool big_endian>
8354 void
8355 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
8356 Symbol_table* symtab,
8357 Layout* layout,
8358 Sized_relobj_file<size, big_endian>* object,
8359 unsigned int data_shndx,
8360 unsigned int sh_type,
8361 const unsigned char* prelocs,
8362 size_t reloc_count,
8363 Output_section* output_section,
8364 bool needs_special_offset_handling,
8365 size_t local_symbol_count,
8366 const unsigned char* plocal_symbols,
8367 Relocatable_relocs* rr)
8368 {
8369 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
8370
8371 gold_assert(sh_type == elfcpp::SHT_RELA);
8372
8373 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
8374 symtab,
8375 layout,
8376 object,
8377 data_shndx,
8378 prelocs,
8379 reloc_count,
8380 output_section,
8381 needs_special_offset_handling,
8382 local_symbol_count,
8383 plocal_symbols,
8384 rr);
8385 }
8386
8387 // Scan the relocs for --emit-relocs.
8388
8389 template<int size, bool big_endian>
8390 void
8391 Target_powerpc<size, big_endian>::emit_relocs_scan(
8392 Symbol_table* symtab,
8393 Layout* layout,
8394 Sized_relobj_file<size, big_endian>* object,
8395 unsigned int data_shndx,
8396 unsigned int sh_type,
8397 const unsigned char* prelocs,
8398 size_t reloc_count,
8399 Output_section* output_section,
8400 bool needs_special_offset_handling,
8401 size_t local_symbol_count,
8402 const unsigned char* plocal_syms,
8403 Relocatable_relocs* rr)
8404 {
8405 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8406 Classify_reloc;
8407 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
8408 Emit_relocs_strategy;
8409
8410 gold_assert(sh_type == elfcpp::SHT_RELA);
8411
8412 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
8413 symtab,
8414 layout,
8415 object,
8416 data_shndx,
8417 prelocs,
8418 reloc_count,
8419 output_section,
8420 needs_special_offset_handling,
8421 local_symbol_count,
8422 plocal_syms,
8423 rr);
8424 }
8425
8426 // Emit relocations for a section.
8427 // This is a modified version of the function by the same name in
8428 // target-reloc.h. Using relocate_special_relocatable for
8429 // R_PPC_PLTREL24 would require duplication of the entire body of the
8430 // loop, so we may as well duplicate the whole thing.
8431
8432 template<int size, bool big_endian>
8433 void
8434 Target_powerpc<size, big_endian>::relocate_relocs(
8435 const Relocate_info<size, big_endian>* relinfo,
8436 unsigned int sh_type,
8437 const unsigned char* prelocs,
8438 size_t reloc_count,
8439 Output_section* output_section,
8440 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
8441 unsigned char*,
8442 Address view_address,
8443 section_size_type,
8444 unsigned char* reloc_view,
8445 section_size_type reloc_view_size)
8446 {
8447 gold_assert(sh_type == elfcpp::SHT_RELA);
8448
8449 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
8450 Reltype;
8451 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
8452 Reltype_write;
8453 const int reloc_size
8454 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
8455 // Offset from start of insn to d-field reloc.
8456 const int d_offset = big_endian ? 2 : 0;
8457
8458 Powerpc_relobj<size, big_endian>* const object
8459 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
8460 const unsigned int local_count = object->local_symbol_count();
8461 unsigned int got2_shndx = object->got2_shndx();
8462 Address got2_addend = 0;
8463 if (got2_shndx != 0)
8464 {
8465 got2_addend = object->get_output_section_offset(got2_shndx);
8466 gold_assert(got2_addend != invalid_address);
8467 }
8468
8469 unsigned char* pwrite = reloc_view;
8470 bool zap_next = false;
8471 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8472 {
8473 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
8474 if (strategy == Relocatable_relocs::RELOC_DISCARD)
8475 continue;
8476
8477 Reltype reloc(prelocs);
8478 Reltype_write reloc_write(pwrite);
8479
8480 Address offset = reloc.get_r_offset();
8481 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
8482 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
8483 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
8484 const unsigned int orig_r_sym = r_sym;
8485 typename elfcpp::Elf_types<size>::Elf_Swxword addend
8486 = reloc.get_r_addend();
8487 const Symbol* gsym = NULL;
8488
8489 if (zap_next)
8490 {
8491 // We could arrange to discard these and other relocs for
8492 // tls optimised sequences in the strategy methods, but for
8493 // now do as BFD ld does.
8494 r_type = elfcpp::R_POWERPC_NONE;
8495 zap_next = false;
8496 }
8497
8498 // Get the new symbol index.
8499 Output_section* os = NULL;
8500 if (r_sym < local_count)
8501 {
8502 switch (strategy)
8503 {
8504 case Relocatable_relocs::RELOC_COPY:
8505 case Relocatable_relocs::RELOC_SPECIAL:
8506 if (r_sym != 0)
8507 {
8508 r_sym = object->symtab_index(r_sym);
8509 gold_assert(r_sym != -1U);
8510 }
8511 break;
8512
8513 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
8514 {
8515 // We are adjusting a section symbol. We need to find
8516 // the symbol table index of the section symbol for
8517 // the output section corresponding to input section
8518 // in which this symbol is defined.
8519 gold_assert(r_sym < local_count);
8520 bool is_ordinary;
8521 unsigned int shndx =
8522 object->local_symbol_input_shndx(r_sym, &is_ordinary);
8523 gold_assert(is_ordinary);
8524 os = object->output_section(shndx);
8525 gold_assert(os != NULL);
8526 gold_assert(os->needs_symtab_index());
8527 r_sym = os->symtab_index();
8528 }
8529 break;
8530
8531 default:
8532 gold_unreachable();
8533 }
8534 }
8535 else
8536 {
8537 gsym = object->global_symbol(r_sym);
8538 gold_assert(gsym != NULL);
8539 if (gsym->is_forwarder())
8540 gsym = relinfo->symtab->resolve_forwards(gsym);
8541
8542 gold_assert(gsym->has_symtab_index());
8543 r_sym = gsym->symtab_index();
8544 }
8545
8546 // Get the new offset--the location in the output section where
8547 // this relocation should be applied.
8548 if (static_cast<Address>(offset_in_output_section) != invalid_address)
8549 offset += offset_in_output_section;
8550 else
8551 {
8552 section_offset_type sot_offset =
8553 convert_types<section_offset_type, Address>(offset);
8554 section_offset_type new_sot_offset =
8555 output_section->output_offset(object, relinfo->data_shndx,
8556 sot_offset);
8557 gold_assert(new_sot_offset != -1);
8558 offset = new_sot_offset;
8559 }
8560
8561 // In an object file, r_offset is an offset within the section.
8562 // In an executable or dynamic object, generated by
8563 // --emit-relocs, r_offset is an absolute address.
8564 if (!parameters->options().relocatable())
8565 {
8566 offset += view_address;
8567 if (static_cast<Address>(offset_in_output_section) != invalid_address)
8568 offset -= offset_in_output_section;
8569 }
8570
8571 // Handle the reloc addend based on the strategy.
8572 if (strategy == Relocatable_relocs::RELOC_COPY)
8573 ;
8574 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
8575 {
8576 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
8577 gold_assert(os != NULL);
8578 addend = psymval->value(object, addend) - os->address();
8579 }
8580 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
8581 {
8582 if (size == 32)
8583 {
8584 if (addend >= 32768)
8585 addend += got2_addend;
8586 }
8587 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
8588 {
8589 r_type = elfcpp::R_POWERPC_ADDR16_HA;
8590 addend -= d_offset;
8591 }
8592 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
8593 {
8594 r_type = elfcpp::R_POWERPC_ADDR16_LO;
8595 addend -= d_offset + 4;
8596 }
8597 }
8598 else
8599 gold_unreachable();
8600
8601 if (!parameters->options().relocatable())
8602 {
8603 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8604 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8605 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8606 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8607 {
8608 // First instruction of a global dynamic sequence,
8609 // arg setup insn.
8610 const bool final = gsym == NULL || gsym->final_value_is_known();
8611 switch (this->optimize_tls_gd(final))
8612 {
8613 case tls::TLSOPT_TO_IE:
8614 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8615 - elfcpp::R_POWERPC_GOT_TLSGD16);
8616 break;
8617 case tls::TLSOPT_TO_LE:
8618 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8619 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8620 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8621 else
8622 {
8623 r_type = elfcpp::R_POWERPC_NONE;
8624 offset -= d_offset;
8625 }
8626 break;
8627 default:
8628 break;
8629 }
8630 }
8631 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8632 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8633 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8634 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8635 {
8636 // First instruction of a local dynamic sequence,
8637 // arg setup insn.
8638 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8639 {
8640 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8641 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8642 {
8643 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8644 const Output_section* os = relinfo->layout->tls_segment()
8645 ->first_section();
8646 gold_assert(os != NULL);
8647 gold_assert(os->needs_symtab_index());
8648 r_sym = os->symtab_index();
8649 addend = dtp_offset;
8650 }
8651 else
8652 {
8653 r_type = elfcpp::R_POWERPC_NONE;
8654 offset -= d_offset;
8655 }
8656 }
8657 }
8658 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8659 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8660 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8661 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8662 {
8663 // First instruction of initial exec sequence.
8664 const bool final = gsym == NULL || gsym->final_value_is_known();
8665 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8666 {
8667 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8668 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8669 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8670 else
8671 {
8672 r_type = elfcpp::R_POWERPC_NONE;
8673 offset -= d_offset;
8674 }
8675 }
8676 }
8677 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8678 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8679 {
8680 // Second instruction of a global dynamic sequence,
8681 // the __tls_get_addr call
8682 const bool final = gsym == NULL || gsym->final_value_is_known();
8683 switch (this->optimize_tls_gd(final))
8684 {
8685 case tls::TLSOPT_TO_IE:
8686 r_type = elfcpp::R_POWERPC_NONE;
8687 zap_next = true;
8688 break;
8689 case tls::TLSOPT_TO_LE:
8690 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8691 offset += d_offset;
8692 zap_next = true;
8693 break;
8694 default:
8695 break;
8696 }
8697 }
8698 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8699 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8700 {
8701 // Second instruction of a local dynamic sequence,
8702 // the __tls_get_addr call
8703 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8704 {
8705 const Output_section* os = relinfo->layout->tls_segment()
8706 ->first_section();
8707 gold_assert(os != NULL);
8708 gold_assert(os->needs_symtab_index());
8709 r_sym = os->symtab_index();
8710 addend = dtp_offset;
8711 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8712 offset += d_offset;
8713 zap_next = true;
8714 }
8715 }
8716 else if (r_type == elfcpp::R_POWERPC_TLS)
8717 {
8718 // Second instruction of an initial exec sequence
8719 const bool final = gsym == NULL || gsym->final_value_is_known();
8720 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8721 {
8722 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8723 offset += d_offset;
8724 }
8725 }
8726 }
8727
8728 reloc_write.put_r_offset(offset);
8729 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
8730 reloc_write.put_r_addend(addend);
8731
8732 pwrite += reloc_size;
8733 }
8734
8735 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
8736 == reloc_view_size);
8737 }
8738
8739 // Return the value to use for a dynamic symbol which requires special
8740 // treatment. This is how we support equality comparisons of function
8741 // pointers across shared library boundaries, as described in the
8742 // processor specific ABI supplement.
8743
8744 template<int size, bool big_endian>
8745 uint64_t
8746 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8747 {
8748 if (size == 32)
8749 {
8750 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8751 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8752 p != this->stub_tables_.end();
8753 ++p)
8754 {
8755 Address off = (*p)->find_plt_call_entry(gsym);
8756 if (off != invalid_address)
8757 return (*p)->stub_address() + off;
8758 }
8759 }
8760 else if (this->abiversion() >= 2)
8761 {
8762 Address off = this->glink_section()->find_global_entry(gsym);
8763 if (off != invalid_address)
8764 return this->glink_section()->global_entry_address() + off;
8765 }
8766 gold_unreachable();
8767 }
8768
8769 // Return the PLT address to use for a local symbol.
8770 template<int size, bool big_endian>
8771 uint64_t
8772 Target_powerpc<size, big_endian>::do_plt_address_for_local(
8773 const Relobj* object,
8774 unsigned int symndx) const
8775 {
8776 if (size == 32)
8777 {
8778 const Sized_relobj<size, big_endian>* relobj
8779 = static_cast<const Sized_relobj<size, big_endian>*>(object);
8780 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8781 p != this->stub_tables_.end();
8782 ++p)
8783 {
8784 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
8785 symndx);
8786 if (off != invalid_address)
8787 return (*p)->stub_address() + off;
8788 }
8789 }
8790 gold_unreachable();
8791 }
8792
8793 // Return the PLT address to use for a global symbol.
8794 template<int size, bool big_endian>
8795 uint64_t
8796 Target_powerpc<size, big_endian>::do_plt_address_for_global(
8797 const Symbol* gsym) const
8798 {
8799 if (size == 32)
8800 {
8801 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8802 p != this->stub_tables_.end();
8803 ++p)
8804 {
8805 Address off = (*p)->find_plt_call_entry(gsym);
8806 if (off != invalid_address)
8807 return (*p)->stub_address() + off;
8808 }
8809 }
8810 else if (this->abiversion() >= 2)
8811 {
8812 Address off = this->glink_section()->find_global_entry(gsym);
8813 if (off != invalid_address)
8814 return this->glink_section()->global_entry_address() + off;
8815 }
8816 gold_unreachable();
8817 }
8818
8819 // Return the offset to use for the GOT_INDX'th got entry which is
8820 // for a local tls symbol specified by OBJECT, SYMNDX.
8821 template<int size, bool big_endian>
8822 int64_t
8823 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
8824 const Relobj* object,
8825 unsigned int symndx,
8826 unsigned int got_indx) const
8827 {
8828 const Powerpc_relobj<size, big_endian>* ppc_object
8829 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
8830 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
8831 {
8832 for (Got_type got_type = GOT_TYPE_TLSGD;
8833 got_type <= GOT_TYPE_TPREL;
8834 got_type = Got_type(got_type + 1))
8835 if (ppc_object->local_has_got_offset(symndx, got_type))
8836 {
8837 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
8838 if (got_type == GOT_TYPE_TLSGD)
8839 off += size / 8;
8840 if (off == got_indx * (size / 8))
8841 {
8842 if (got_type == GOT_TYPE_TPREL)
8843 return -tp_offset;
8844 else
8845 return -dtp_offset;
8846 }
8847 }
8848 }
8849 gold_unreachable();
8850 }
8851
8852 // Return the offset to use for the GOT_INDX'th got entry which is
8853 // for global tls symbol GSYM.
8854 template<int size, bool big_endian>
8855 int64_t
8856 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
8857 Symbol* gsym,
8858 unsigned int got_indx) const
8859 {
8860 if (gsym->type() == elfcpp::STT_TLS)
8861 {
8862 for (Got_type got_type = GOT_TYPE_TLSGD;
8863 got_type <= GOT_TYPE_TPREL;
8864 got_type = Got_type(got_type + 1))
8865 if (gsym->has_got_offset(got_type))
8866 {
8867 unsigned int off = gsym->got_offset(got_type);
8868 if (got_type == GOT_TYPE_TLSGD)
8869 off += size / 8;
8870 if (off == got_indx * (size / 8))
8871 {
8872 if (got_type == GOT_TYPE_TPREL)
8873 return -tp_offset;
8874 else
8875 return -dtp_offset;
8876 }
8877 }
8878 }
8879 gold_unreachable();
8880 }
8881
8882 // The selector for powerpc object files.
8883
8884 template<int size, bool big_endian>
8885 class Target_selector_powerpc : public Target_selector
8886 {
8887 public:
8888 Target_selector_powerpc()
8889 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
8890 size, big_endian,
8891 (size == 64
8892 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
8893 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
8894 (size == 64
8895 ? (big_endian ? "elf64ppc" : "elf64lppc")
8896 : (big_endian ? "elf32ppc" : "elf32lppc")))
8897 { }
8898
8899 virtual Target*
8900 do_instantiate_target()
8901 { return new Target_powerpc<size, big_endian>(); }
8902 };
8903
8904 Target_selector_powerpc<32, true> target_selector_ppc32;
8905 Target_selector_powerpc<32, false> target_selector_ppc32le;
8906 Target_selector_powerpc<64, true> target_selector_ppc64;
8907 Target_selector_powerpc<64, false> target_selector_ppc64le;
8908
8909 // Instantiate these constants for -O0
8910 template<int size, bool big_endian>
8911 const int Output_data_glink<size, big_endian>::pltresolve_size;
8912 template<int size, bool big_endian>
8913 const typename Output_data_glink<size, big_endian>::Address
8914 Output_data_glink<size, big_endian>::invalid_address;
8915 template<int size, bool big_endian>
8916 const typename Stub_table<size, big_endian>::Address
8917 Stub_table<size, big_endian>::invalid_address;
8918 template<int size, bool big_endian>
8919 const typename Target_powerpc<size, big_endian>::Address
8920 Target_powerpc<size, big_endian>::invalid_address;
8921
8922 } // End anonymous namespace.
This page took 0.3139 seconds and 5 git commands to generate.