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