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