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