gold/
[binutils-gdb.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright 2008, 2009, 2010, 2011, 2012 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 "elfcpp.h"
27 #include "parameters.h"
28 #include "reloc.h"
29 #include "powerpc.h"
30 #include "object.h"
31 #include "symtab.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "copy-relocs.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39 #include "errors.h"
40 #include "gc.h"
41
42 namespace
43 {
44
45 using namespace gold;
46
47 template<int size, bool big_endian>
48 class Output_data_plt_powerpc;
49
50 template<int size, bool big_endian>
51 class Output_data_got_powerpc;
52
53 template<int size, bool big_endian>
54 class Output_data_glink;
55
56 template<int size, bool big_endian>
57 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
58 {
59 public:
60 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
61 typedef typename elfcpp::Elf_types<size>::Elf_Off Offset;
62 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
63 typedef Unordered_map<Address, Section_refs> Access_from;
64
65 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
66 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
67 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
68 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
69 { }
70
71 ~Powerpc_relobj()
72 { }
73
74 // The .got2 section shndx.
75 unsigned int
76 got2_shndx() const
77 {
78 if (size == 32)
79 return this->special_;
80 else
81 return 0;
82 }
83
84 // The .opd section shndx.
85 unsigned int
86 opd_shndx() const
87 {
88 if (size == 32)
89 return 0;
90 else
91 return this->special_;
92 }
93
94 // Init OPD entry arrays.
95 void
96 init_opd(size_t opd_size)
97 {
98 size_t count = this->opd_ent_ndx(opd_size);
99 this->opd_ent_.resize(count);
100 }
101
102 // Return section and offset of function entry for .opd + R_OFF.
103 unsigned int
104 get_opd_ent(Address r_off, Address* value = NULL) const
105 {
106 size_t ndx = this->opd_ent_ndx(r_off);
107 gold_assert(ndx < this->opd_ent_.size());
108 gold_assert(this->opd_ent_[ndx].shndx != 0);
109 if (value != NULL)
110 *value = this->opd_ent_[ndx].off;
111 return this->opd_ent_[ndx].shndx;
112 }
113
114 // Set section and offset of function entry for .opd + R_OFF.
115 void
116 set_opd_ent(Address r_off, unsigned int shndx, Address value)
117 {
118 size_t ndx = this->opd_ent_ndx(r_off);
119 gold_assert(ndx < this->opd_ent_.size());
120 this->opd_ent_[ndx].shndx = shndx;
121 this->opd_ent_[ndx].off = value;
122 }
123
124 // Return discard flag for .opd + R_OFF.
125 bool
126 get_opd_discard(Address r_off) const
127 {
128 size_t ndx = this->opd_ent_ndx(r_off);
129 gold_assert(ndx < this->opd_ent_.size());
130 return this->opd_ent_[ndx].discard;
131 }
132
133 // Set discard flag for .opd + R_OFF.
134 void
135 set_opd_discard(Address r_off)
136 {
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 this->opd_ent_[ndx].discard = true;
140 }
141
142 Access_from*
143 access_from_map()
144 { return &this->access_from_map_; }
145
146 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
147 // section at DST_OFF.
148 void
149 add_reference(Object* src_obj,
150 unsigned int src_indx,
151 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
152 {
153 Section_id src_id(src_obj, src_indx);
154 this->access_from_map_[dst_off].insert(src_id);
155 }
156
157 // Add a reference to the code section specified by the .opd entry
158 // at DST_OFF
159 void
160 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
161 {
162 size_t ndx = this->opd_ent_ndx(dst_off);
163 if (ndx >= this->opd_ent_.size())
164 this->opd_ent_.resize(ndx + 1);
165 this->opd_ent_[ndx].gc_mark = true;
166 }
167
168 void
169 process_gc_mark(Symbol_table* symtab)
170 {
171 for (size_t i = 0; i < this->opd_ent_.size(); i++)
172 if (this->opd_ent_[i].gc_mark)
173 {
174 unsigned int shndx = this->opd_ent_[i].shndx;
175 symtab->gc()->worklist().push(Section_id(this, shndx));
176 }
177 }
178
179 bool
180 opd_valid() const
181 { return this->opd_valid_; }
182
183 void
184 set_opd_valid()
185 { this->opd_valid_ = true; }
186
187 // Examine .rela.opd to build info about function entry points.
188 void
189 scan_opd_relocs(size_t reloc_count,
190 const unsigned char* prelocs,
191 const unsigned char* plocal_syms);
192
193 void
194 do_read_relocs(Read_relocs_data*);
195
196 bool
197 do_find_special_sections(Read_symbols_data* sd);
198
199 // Adjust this local symbol value. Return false if the symbol
200 // should be discarded from the output file.
201 bool
202 do_adjust_local_symbol(Symbol_value<size>* lv) const
203 {
204 if (size == 64 && this->opd_shndx() != 0)
205 {
206 bool is_ordinary;
207 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
208 return true;
209 if (this->get_opd_discard(lv->input_value()))
210 return false;
211 }
212 return true;
213 }
214
215 // Return offset in output GOT section that this object will use
216 // as a TOC pointer. Won't be just a constant with multi-toc support.
217 Address
218 toc_base_offset() const
219 { return 0x8000; }
220
221 private:
222 struct Opd_ent
223 {
224 unsigned int shndx;
225 bool discard : 1;
226 bool gc_mark : 1;
227 Offset off;
228 };
229
230 // Return index into opd_ent_ array for .opd entry at OFF.
231 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
232 // apart when the language doesn't use the last 8-byte word, the
233 // environment pointer. Thus dividing the entry section offset by
234 // 16 will give an index into opd_ent_ that works for either layout
235 // of .opd. (It leaves some elements of the vector unused when .opd
236 // entries are spaced 24 bytes apart, but we don't know the spacing
237 // until relocations are processed, and in any case it is possible
238 // for an object to have some entries spaced 16 bytes apart and
239 // others 24 bytes apart.)
240 size_t
241 opd_ent_ndx(size_t off) const
242 { return off >> 4;}
243
244 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
245 unsigned int special_;
246
247 // Set at the start of gc_process_relocs, when we know opd_ent_
248 // vector is valid. The flag could be made atomic and set in
249 // do_read_relocs with memory_order_release and then tested with
250 // memory_order_acquire, potentially resulting in fewer entries in
251 // access_from_map_.
252 bool opd_valid_;
253
254 // The first 8-byte word of an OPD entry gives the address of the
255 // entry point of the function. Relocatable object files have a
256 // relocation on this word. The following vector records the
257 // section and offset specified by these relocations.
258 std::vector<Opd_ent> opd_ent_;
259
260 // References made to this object's .opd section when running
261 // gc_process_relocs for another object, before the opd_ent_ vector
262 // is valid for this object.
263 Access_from access_from_map_;
264 };
265
266 template<int size, bool big_endian>
267 class Target_powerpc : public Sized_target<size, big_endian>
268 {
269 public:
270 typedef
271 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
272 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
273 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
274 static const Address invalid_address = static_cast<Address>(0) - 1;
275 // Offset of tp and dtp pointers from start of TLS block.
276 static const Address tp_offset = 0x7000;
277 static const Address dtp_offset = 0x8000;
278
279 Target_powerpc()
280 : Sized_target<size, big_endian>(&powerpc_info),
281 got_(NULL), plt_(NULL), iplt_(NULL), glink_(NULL), rela_dyn_(NULL),
282 copy_relocs_(elfcpp::R_POWERPC_COPY),
283 dynbss_(NULL), tlsld_got_offset_(-1U)
284 {
285 }
286
287 // Process the relocations to determine unreferenced sections for
288 // garbage collection.
289 void
290 gc_process_relocs(Symbol_table* symtab,
291 Layout* layout,
292 Sized_relobj_file<size, big_endian>* object,
293 unsigned int data_shndx,
294 unsigned int sh_type,
295 const unsigned char* prelocs,
296 size_t reloc_count,
297 Output_section* output_section,
298 bool needs_special_offset_handling,
299 size_t local_symbol_count,
300 const unsigned char* plocal_symbols);
301
302 // Scan the relocations to look for symbol adjustments.
303 void
304 scan_relocs(Symbol_table* symtab,
305 Layout* layout,
306 Sized_relobj_file<size, big_endian>* object,
307 unsigned int data_shndx,
308 unsigned int sh_type,
309 const unsigned char* prelocs,
310 size_t reloc_count,
311 Output_section* output_section,
312 bool needs_special_offset_handling,
313 size_t local_symbol_count,
314 const unsigned char* plocal_symbols);
315
316 // Map input .toc section to output .got section.
317 const char*
318 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
319 {
320 if (size == 64 && strcmp(name, ".toc") == 0)
321 {
322 *plen = 4;
323 return ".got";
324 }
325 return NULL;
326 }
327
328 // Provide linker defined save/restore functions.
329 void
330 define_save_restore_funcs(Layout*, Symbol_table*);
331
332 // Finalize the sections.
333 void
334 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
335
336 // Return the value to use for a dynamic which requires special
337 // treatment.
338 uint64_t
339 do_dynsym_value(const Symbol*) const;
340
341 // Return the PLT address to use for a local symbol.
342 uint64_t
343 do_plt_address_for_local(const Relobj*, unsigned int) const;
344
345 // Return the PLT address to use for a global symbol.
346 uint64_t
347 do_plt_address_for_global(const Symbol*) const;
348
349 // Return the offset to use for the GOT_INDX'th got entry which is
350 // for a local tls symbol specified by OBJECT, SYMNDX.
351 int64_t
352 do_tls_offset_for_local(const Relobj* object,
353 unsigned int symndx,
354 unsigned int got_indx) const;
355
356 // Return the offset to use for the GOT_INDX'th got entry which is
357 // for global tls symbol GSYM.
358 int64_t
359 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
360
361 // Relocate a section.
362 void
363 relocate_section(const Relocate_info<size, big_endian>*,
364 unsigned int sh_type,
365 const unsigned char* prelocs,
366 size_t reloc_count,
367 Output_section* output_section,
368 bool needs_special_offset_handling,
369 unsigned char* view,
370 Address view_address,
371 section_size_type view_size,
372 const Reloc_symbol_changes*);
373
374 // Scan the relocs during a relocatable link.
375 void
376 scan_relocatable_relocs(Symbol_table* symtab,
377 Layout* layout,
378 Sized_relobj_file<size, big_endian>* object,
379 unsigned int data_shndx,
380 unsigned int sh_type,
381 const unsigned char* prelocs,
382 size_t reloc_count,
383 Output_section* output_section,
384 bool needs_special_offset_handling,
385 size_t local_symbol_count,
386 const unsigned char* plocal_symbols,
387 Relocatable_relocs*);
388
389 // Emit relocations for a section.
390 void
391 relocate_relocs(const Relocate_info<size, big_endian>*,
392 unsigned int sh_type,
393 const unsigned char* prelocs,
394 size_t reloc_count,
395 Output_section* output_section,
396 typename elfcpp::Elf_types<size>::Elf_Off
397 offset_in_output_section,
398 const Relocatable_relocs*,
399 unsigned char*,
400 Address view_address,
401 section_size_type,
402 unsigned char* reloc_view,
403 section_size_type reloc_view_size);
404
405 // Return whether SYM is defined by the ABI.
406 bool
407 do_is_defined_by_abi(const Symbol* sym) const
408 {
409 return strcmp(sym->name(), "__tls_get_addr") == 0;
410 }
411
412 // Return the size of the GOT section.
413 section_size_type
414 got_size() const
415 {
416 gold_assert(this->got_ != NULL);
417 return this->got_->data_size();
418 }
419
420 // Get the PLT section.
421 const Output_data_plt_powerpc<size, big_endian>*
422 plt_section() const
423 {
424 gold_assert(this->plt_ != NULL);
425 return this->plt_;
426 }
427
428 // Get the IPLT section.
429 const Output_data_plt_powerpc<size, big_endian>*
430 iplt_section() const
431 {
432 gold_assert(this->iplt_ != NULL);
433 return this->iplt_;
434 }
435
436 // Get the .glink section.
437 const Output_data_glink<size, big_endian>*
438 glink_section() const
439 {
440 gold_assert(this->glink_ != NULL);
441 return this->glink_;
442 }
443
444 // Get the GOT section.
445 const Output_data_got_powerpc<size, big_endian>*
446 got_section() const
447 {
448 gold_assert(this->got_ != NULL);
449 return this->got_;
450 }
451
452 Object*
453 do_make_elf_object(const std::string&, Input_file*, off_t,
454 const elfcpp::Ehdr<size, big_endian>&);
455
456 // Return the number of entries in the GOT.
457 unsigned int
458 got_entry_count() const
459 {
460 if (this->got_ == NULL)
461 return 0;
462 return this->got_size() / (size / 8);
463 }
464
465 // Return the number of entries in the PLT.
466 unsigned int
467 plt_entry_count() const;
468
469 // Return the offset of the first non-reserved PLT entry.
470 unsigned int
471 first_plt_entry_offset() const;
472
473 // Return the size of each PLT entry.
474 unsigned int
475 plt_entry_size() const;
476
477 // Add any special sections for this symbol to the gc work list.
478 // For powerpc64, this adds the code section of a function
479 // descriptor.
480 void
481 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
482
483 // Handle target specific gc actions when adding a gc reference from
484 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
485 // and DST_OFF. For powerpc64, this adds a referenc to the code
486 // section of a function descriptor.
487 void
488 do_gc_add_reference(Symbol_table* symtab,
489 Object* src_obj,
490 unsigned int src_shndx,
491 Object* dst_obj,
492 unsigned int dst_shndx,
493 Address dst_off) const;
494
495 private:
496
497 // The class which scans relocations.
498 class Scan
499 {
500 public:
501 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
502
503 Scan()
504 : issued_non_pic_error_(false)
505 { }
506
507 static inline int
508 get_reference_flags(unsigned int r_type);
509
510 inline void
511 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
512 Sized_relobj_file<size, big_endian>* object,
513 unsigned int data_shndx,
514 Output_section* output_section,
515 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
516 const elfcpp::Sym<size, big_endian>& lsym,
517 bool is_discarded);
518
519 inline void
520 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
521 Sized_relobj_file<size, big_endian>* object,
522 unsigned int data_shndx,
523 Output_section* output_section,
524 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
525 Symbol* gsym);
526
527 inline bool
528 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
529 Target_powerpc* ,
530 Sized_relobj_file<size, big_endian>* ,
531 unsigned int ,
532 Output_section* ,
533 const elfcpp::Rela<size, big_endian>& ,
534 unsigned int ,
535 const elfcpp::Sym<size, big_endian>&)
536 { return false; }
537
538 inline bool
539 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
540 Target_powerpc* ,
541 Sized_relobj_file<size, big_endian>* ,
542 unsigned int ,
543 Output_section* ,
544 const elfcpp::Rela<size,
545 big_endian>& ,
546 unsigned int , Symbol*)
547 { return false; }
548
549 private:
550 static void
551 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
552 unsigned int r_type);
553
554 static void
555 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
556 unsigned int r_type, Symbol*);
557
558 static void
559 generate_tls_call(Symbol_table* symtab, Layout* layout,
560 Target_powerpc* target);
561
562 void
563 check_non_pic(Relobj*, unsigned int r_type);
564
565 bool
566 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
567 unsigned int r_type);
568
569 // Whether we have issued an error about a non-PIC compilation.
570 bool issued_non_pic_error_;
571 };
572
573 Address
574 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
575 Powerpc_relobj<size, big_endian>* object,
576 unsigned int *dest_shndx);
577
578 // The class which implements relocation.
579 class Relocate
580 {
581 public:
582 // Use 'at' branch hints when true, 'y' when false.
583 // FIXME maybe: set this with an option.
584 static const bool is_isa_v2 = true;
585
586 enum skip_tls
587 {
588 CALL_NOT_EXPECTED = 0,
589 CALL_EXPECTED = 1,
590 CALL_SKIP = 2
591 };
592
593 Relocate()
594 : call_tls_get_addr_(CALL_NOT_EXPECTED)
595 { }
596
597 ~Relocate()
598 {
599 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
600 {
601 // FIXME: This needs to specify the location somehow.
602 gold_error(_("missing expected __tls_get_addr call"));
603 }
604 }
605
606 // Do a relocation. Return false if the caller should not issue
607 // any warnings about this relocation.
608 inline bool
609 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
610 Output_section*, size_t relnum,
611 const elfcpp::Rela<size, big_endian>&,
612 unsigned int r_type, const Sized_symbol<size>*,
613 const Symbol_value<size>*,
614 unsigned char*,
615 typename elfcpp::Elf_types<size>::Elf_Addr,
616 section_size_type);
617
618 // This is set if we should skip the next reloc, which should be a
619 // call to __tls_get_addr.
620 enum skip_tls call_tls_get_addr_;
621 };
622
623 class Relocate_comdat_behavior
624 {
625 public:
626 // Decide what the linker should do for relocations that refer to
627 // discarded comdat sections.
628 inline Comdat_behavior
629 get(const char* name)
630 {
631 gold::Default_comdat_behavior default_behavior;
632 Comdat_behavior ret = default_behavior.get(name);
633 if (ret == CB_WARNING)
634 {
635 if (size == 32
636 && (strcmp(name, ".fixup") == 0
637 || strcmp(name, ".got2") == 0))
638 ret = CB_IGNORE;
639 if (size == 64
640 && (strcmp(name, ".opd") == 0
641 || strcmp(name, ".toc") == 0
642 || strcmp(name, ".toc1") == 0))
643 ret = CB_IGNORE;
644 }
645 return ret;
646 }
647 };
648
649 // A class which returns the size required for a relocation type,
650 // used while scanning relocs during a relocatable link.
651 class Relocatable_size_for_reloc
652 {
653 public:
654 unsigned int
655 get_size_for_reloc(unsigned int, Relobj*)
656 {
657 gold_unreachable();
658 return 0;
659 }
660 };
661
662 // Optimize the TLS relocation type based on what we know about the
663 // symbol. IS_FINAL is true if the final address of this symbol is
664 // known at link time.
665
666 tls::Tls_optimization
667 optimize_tls_gd(bool is_final)
668 {
669 // If we are generating a shared library, then we can't do anything
670 // in the linker.
671 if (parameters->options().shared())
672 return tls::TLSOPT_NONE;
673
674 if (!is_final)
675 return tls::TLSOPT_TO_IE;
676 return tls::TLSOPT_TO_LE;
677 }
678
679 tls::Tls_optimization
680 optimize_tls_ld()
681 {
682 if (parameters->options().shared())
683 return tls::TLSOPT_NONE;
684
685 return tls::TLSOPT_TO_LE;
686 }
687
688 tls::Tls_optimization
689 optimize_tls_ie(bool is_final)
690 {
691 if (!is_final || parameters->options().shared())
692 return tls::TLSOPT_NONE;
693
694 return tls::TLSOPT_TO_LE;
695 }
696
697 // Get the GOT section, creating it if necessary.
698 Output_data_got_powerpc<size, big_endian>*
699 got_section(Symbol_table*, Layout*);
700
701 // Create glink.
702 void
703 make_glink_section(Layout*);
704
705 // Create the PLT section.
706 void
707 make_plt_section(Layout*);
708
709 void
710 make_iplt_section(Layout*);
711
712 // Create a PLT entry for a global symbol.
713 void
714 make_plt_entry(Layout*, Symbol*,
715 const elfcpp::Rela<size, big_endian>&,
716 const Sized_relobj_file<size, big_endian>* object);
717
718 // Create a PLT entry for a local IFUNC symbol.
719 void
720 make_local_ifunc_plt_entry(Layout*,
721 const elfcpp::Rela<size, big_endian>&,
722 Sized_relobj_file<size, big_endian>*);
723
724 // Create a GOT entry for local dynamic __tls_get_addr.
725 unsigned int
726 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
727 Sized_relobj_file<size, big_endian>* object);
728
729 unsigned int
730 tlsld_got_offset() const
731 {
732 return this->tlsld_got_offset_;
733 }
734
735 // Get the dynamic reloc section, creating it if necessary.
736 Reloc_section*
737 rela_dyn_section(Layout*);
738
739 // Copy a relocation against a global symbol.
740 void
741 copy_reloc(Symbol_table* symtab, Layout* layout,
742 Sized_relobj_file<size, big_endian>* object,
743 unsigned int shndx, Output_section* output_section,
744 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
745 {
746 this->copy_relocs_.copy_reloc(symtab, layout,
747 symtab->get_sized_symbol<size>(sym),
748 object, shndx, output_section,
749 reloc, this->rela_dyn_section(layout));
750 }
751
752 // Information about this specific target which we pass to the
753 // general Target structure.
754 static Target::Target_info powerpc_info;
755
756 // The types of GOT entries needed for this platform.
757 // These values are exposed to the ABI in an incremental link.
758 // Do not renumber existing values without changing the version
759 // number of the .gnu_incremental_inputs section.
760 enum Got_type
761 {
762 GOT_TYPE_STANDARD,
763 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
764 GOT_TYPE_DTPREL, // entry for @got@dtprel
765 GOT_TYPE_TPREL // entry for @got@tprel
766 };
767
768 // The GOT output section.
769 Output_data_got_powerpc<size, big_endian>* got_;
770 // The PLT output section.
771 Output_data_plt_powerpc<size, big_endian>* plt_;
772 // The IPLT output section.
773 Output_data_plt_powerpc<size, big_endian>* iplt_;
774 // The .glink output section.
775 Output_data_glink<size, big_endian>* glink_;
776 // The dynamic reloc output section.
777 Reloc_section* rela_dyn_;
778 // Relocs saved to avoid a COPY reloc.
779 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
780 // Space for variables copied with a COPY reloc.
781 Output_data_space* dynbss_;
782 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
783 unsigned int tlsld_got_offset_;
784 };
785
786 template<>
787 Target::Target_info Target_powerpc<32, true>::powerpc_info =
788 {
789 32, // size
790 true, // is_big_endian
791 elfcpp::EM_PPC, // machine_code
792 false, // has_make_symbol
793 false, // has_resolve
794 false, // has_code_fill
795 true, // is_default_stack_executable
796 false, // can_icf_inline_merge_sections
797 '\0', // wrap_char
798 "/usr/lib/ld.so.1", // dynamic_linker
799 0x10000000, // default_text_segment_address
800 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
801 4 * 1024, // common_pagesize (overridable by -z common-page-size)
802 false, // isolate_execinstr
803 0, // rosegment_gap
804 elfcpp::SHN_UNDEF, // small_common_shndx
805 elfcpp::SHN_UNDEF, // large_common_shndx
806 0, // small_common_section_flags
807 0, // large_common_section_flags
808 NULL, // attributes_section
809 NULL // attributes_vendor
810 };
811
812 template<>
813 Target::Target_info Target_powerpc<32, false>::powerpc_info =
814 {
815 32, // size
816 false, // is_big_endian
817 elfcpp::EM_PPC, // machine_code
818 false, // has_make_symbol
819 false, // has_resolve
820 false, // has_code_fill
821 true, // is_default_stack_executable
822 false, // can_icf_inline_merge_sections
823 '\0', // wrap_char
824 "/usr/lib/ld.so.1", // dynamic_linker
825 0x10000000, // default_text_segment_address
826 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
827 4 * 1024, // common_pagesize (overridable by -z common-page-size)
828 false, // isolate_execinstr
829 0, // rosegment_gap
830 elfcpp::SHN_UNDEF, // small_common_shndx
831 elfcpp::SHN_UNDEF, // large_common_shndx
832 0, // small_common_section_flags
833 0, // large_common_section_flags
834 NULL, // attributes_section
835 NULL // attributes_vendor
836 };
837
838 template<>
839 Target::Target_info Target_powerpc<64, true>::powerpc_info =
840 {
841 64, // size
842 true, // is_big_endian
843 elfcpp::EM_PPC64, // machine_code
844 false, // has_make_symbol
845 false, // has_resolve
846 false, // has_code_fill
847 true, // is_default_stack_executable
848 false, // can_icf_inline_merge_sections
849 '\0', // wrap_char
850 "/usr/lib/ld.so.1", // dynamic_linker
851 0x10000000, // default_text_segment_address
852 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
853 4 * 1024, // common_pagesize (overridable by -z common-page-size)
854 false, // isolate_execinstr
855 0, // rosegment_gap
856 elfcpp::SHN_UNDEF, // small_common_shndx
857 elfcpp::SHN_UNDEF, // large_common_shndx
858 0, // small_common_section_flags
859 0, // large_common_section_flags
860 NULL, // attributes_section
861 NULL // attributes_vendor
862 };
863
864 template<>
865 Target::Target_info Target_powerpc<64, false>::powerpc_info =
866 {
867 64, // size
868 false, // is_big_endian
869 elfcpp::EM_PPC64, // machine_code
870 false, // has_make_symbol
871 false, // has_resolve
872 false, // has_code_fill
873 true, // is_default_stack_executable
874 false, // can_icf_inline_merge_sections
875 '\0', // wrap_char
876 "/usr/lib/ld.so.1", // dynamic_linker
877 0x10000000, // default_text_segment_address
878 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
879 4 * 1024, // common_pagesize (overridable by -z common-page-size)
880 false, // isolate_execinstr
881 0, // rosegment_gap
882 elfcpp::SHN_UNDEF, // small_common_shndx
883 elfcpp::SHN_UNDEF, // large_common_shndx
884 0, // small_common_section_flags
885 0, // large_common_section_flags
886 NULL, // attributes_section
887 NULL // attributes_vendor
888 };
889
890 inline bool
891 is_branch_reloc(unsigned int r_type)
892 {
893 return (r_type == elfcpp::R_POWERPC_REL24
894 || r_type == elfcpp::R_PPC_PLTREL24
895 || r_type == elfcpp::R_PPC_LOCAL24PC
896 || r_type == elfcpp::R_POWERPC_REL14
897 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
898 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
899 || r_type == elfcpp::R_POWERPC_ADDR24
900 || r_type == elfcpp::R_POWERPC_ADDR14
901 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
902 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
903 }
904
905 // If INSN is an opcode that may be used with an @tls operand, return
906 // the transformed insn for TLS optimisation, otherwise return 0. If
907 // REG is non-zero only match an insn with RB or RA equal to REG.
908 uint32_t
909 at_tls_transform(uint32_t insn, unsigned int reg)
910 {
911 if ((insn & (0x3f << 26)) != 31 << 26)
912 return 0;
913
914 unsigned int rtra;
915 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
916 rtra = insn & ((1 << 26) - (1 << 16));
917 else if (((insn >> 16) & 0x1f) == reg)
918 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
919 else
920 return 0;
921
922 if ((insn & (0x3ff << 1)) == 266 << 1)
923 // add -> addi
924 insn = 14 << 26;
925 else if ((insn & (0x1f << 1)) == 23 << 1
926 && ((insn & (0x1f << 6)) < 14 << 6
927 || ((insn & (0x1f << 6)) >= 16 << 6
928 && (insn & (0x1f << 6)) < 24 << 6)))
929 // load and store indexed -> dform
930 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
931 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
932 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
933 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
934 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
935 // lwax -> lwa
936 insn = (58 << 26) | 2;
937 else
938 return 0;
939 insn |= rtra;
940 return insn;
941 }
942
943 // Modified version of symtab.h class Symbol member
944 // Given a direct absolute or pc-relative static relocation against
945 // the global symbol, this function returns whether a dynamic relocation
946 // is needed.
947
948 template<int size>
949 bool
950 needs_dynamic_reloc(const Symbol* gsym, int flags)
951 {
952 // No dynamic relocations in a static link!
953 if (parameters->doing_static_link())
954 return false;
955
956 // A reference to an undefined symbol from an executable should be
957 // statically resolved to 0, and does not need a dynamic relocation.
958 // This matches gnu ld behavior.
959 if (gsym->is_undefined() && !parameters->options().shared())
960 return false;
961
962 // A reference to an absolute symbol does not need a dynamic relocation.
963 if (gsym->is_absolute())
964 return false;
965
966 // An absolute reference within a position-independent output file
967 // will need a dynamic relocation.
968 if ((flags & Symbol::ABSOLUTE_REF)
969 && parameters->options().output_is_position_independent())
970 return true;
971
972 // A function call that can branch to a local PLT entry does not need
973 // a dynamic relocation.
974 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
975 return false;
976
977 // A reference to any PLT entry in a non-position-independent executable
978 // does not need a dynamic relocation.
979 // Except due to having function descriptors on powerpc64 we don't define
980 // functions to their plt code in an executable, so this doesn't apply.
981 if (size == 32
982 && !parameters->options().output_is_position_independent()
983 && gsym->has_plt_offset())
984 return false;
985
986 // A reference to a symbol defined in a dynamic object or to a
987 // symbol that is preemptible will need a dynamic relocation.
988 if (gsym->is_from_dynobj()
989 || gsym->is_undefined()
990 || gsym->is_preemptible())
991 return true;
992
993 // For all other cases, return FALSE.
994 return false;
995 }
996
997 // Modified version of symtab.h class Symbol member
998 // Whether we should use the PLT offset associated with a symbol for
999 // a relocation. FLAGS is a set of Reference_flags.
1000
1001 template<int size>
1002 bool
1003 use_plt_offset(const Symbol* gsym, int flags)
1004 {
1005 // If the symbol doesn't have a PLT offset, then naturally we
1006 // don't want to use it.
1007 if (!gsym->has_plt_offset())
1008 return false;
1009
1010 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1011 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1012 return true;
1013
1014 // If we are going to generate a dynamic relocation, then we will
1015 // wind up using that, so no need to use the PLT entry.
1016 if (needs_dynamic_reloc<size>(gsym, flags))
1017 return false;
1018
1019 // If the symbol is from a dynamic object, we need to use the PLT
1020 // entry.
1021 if (gsym->is_from_dynobj())
1022 return true;
1023
1024 // If we are generating a shared object, and gsym symbol is
1025 // undefined or preemptible, we need to use the PLT entry.
1026 if (parameters->options().shared()
1027 && (gsym->is_undefined() || gsym->is_preemptible()))
1028 return true;
1029
1030 // If gsym is a call to a weak undefined symbol, we need to use
1031 // the PLT entry; the symbol may be defined by a library loaded
1032 // at runtime.
1033 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1034 return true;
1035
1036 // Otherwise we can use the regular definition.
1037 return false;
1038 }
1039
1040 template<int size, bool big_endian>
1041 class Powerpc_relocate_functions
1042 {
1043 public:
1044 enum Overflow_check
1045 {
1046 CHECK_NONE,
1047 CHECK_SIGNED,
1048 CHECK_BITFIELD
1049 };
1050
1051 enum Status
1052 {
1053 STATUS_OK,
1054 STATUS_OVERFLOW
1055 };
1056
1057 private:
1058 typedef Powerpc_relocate_functions<size, big_endian> This;
1059 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1060
1061 template<int valsize>
1062 static inline bool
1063 has_overflow_signed(Address value)
1064 {
1065 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1066 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1067 limit <<= ((valsize - 1) >> 1);
1068 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1069 return value + limit > (limit << 1) - 1;
1070 }
1071
1072 template<int valsize>
1073 static inline bool
1074 has_overflow_bitfield(Address value)
1075 {
1076 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1077 limit <<= ((valsize - 1) >> 1);
1078 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1079 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1080 }
1081
1082 template<int valsize>
1083 static inline Status
1084 overflowed(Address value, Overflow_check overflow)
1085 {
1086 if (overflow == CHECK_SIGNED)
1087 {
1088 if (has_overflow_signed<valsize>(value))
1089 return STATUS_OVERFLOW;
1090 }
1091 else if (overflow == CHECK_BITFIELD)
1092 {
1093 if (has_overflow_bitfield<valsize>(value))
1094 return STATUS_OVERFLOW;
1095 }
1096 return STATUS_OK;
1097 }
1098
1099 // Do a simple RELA relocation
1100 template<int valsize>
1101 static inline Status
1102 rela(unsigned char* view, Address value, Overflow_check overflow)
1103 {
1104 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1105 Valtype* wv = reinterpret_cast<Valtype*>(view);
1106 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1107 return overflowed<valsize>(value, overflow);
1108 }
1109
1110 template<int valsize>
1111 static inline Status
1112 rela(unsigned char* view,
1113 unsigned int right_shift,
1114 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1115 Address value,
1116 Overflow_check overflow)
1117 {
1118 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1119 Valtype* wv = reinterpret_cast<Valtype*>(view);
1120 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1121 Valtype reloc = value >> right_shift;
1122 val &= ~dst_mask;
1123 reloc &= dst_mask;
1124 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1125 return overflowed<valsize>(value >> right_shift, overflow);
1126 }
1127
1128 // Do a simple RELA relocation, unaligned.
1129 template<int valsize>
1130 static inline Status
1131 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1132 {
1133 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1134 return overflowed<valsize>(value, overflow);
1135 }
1136
1137 template<int valsize>
1138 static inline Status
1139 rela_ua(unsigned char* view,
1140 unsigned int right_shift,
1141 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1142 Address value,
1143 Overflow_check overflow)
1144 {
1145 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1146 Valtype;
1147 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1148 Valtype reloc = value >> right_shift;
1149 val &= ~dst_mask;
1150 reloc &= dst_mask;
1151 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1152 return overflowed<valsize>(value >> right_shift, overflow);
1153 }
1154
1155 public:
1156 // R_PPC64_ADDR64: (Symbol + Addend)
1157 static inline void
1158 addr64(unsigned char* view, Address value)
1159 { This::template rela<64>(view, value, CHECK_NONE); }
1160
1161 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1162 static inline void
1163 addr64_u(unsigned char* view, Address value)
1164 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1165
1166 // R_POWERPC_ADDR32: (Symbol + Addend)
1167 static inline Status
1168 addr32(unsigned char* view, Address value, Overflow_check overflow)
1169 { return This::template rela<32>(view, value, overflow); }
1170
1171 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1172 static inline Status
1173 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1174 { return This::template rela_ua<32>(view, value, overflow); }
1175
1176 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1177 static inline Status
1178 addr24(unsigned char* view, Address value, Overflow_check overflow)
1179 {
1180 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1181 if (overflow != CHECK_NONE && (value & 3) != 0)
1182 stat = STATUS_OVERFLOW;
1183 return stat;
1184 }
1185
1186 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1187 static inline Status
1188 addr16(unsigned char* view, Address value, Overflow_check overflow)
1189 { return This::template rela<16>(view, value, overflow); }
1190
1191 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1192 static inline Status
1193 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1194 { return This::template rela_ua<16>(view, value, overflow); }
1195
1196 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1197 static inline Status
1198 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1199 {
1200 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1201 if (overflow != CHECK_NONE && (value & 3) != 0)
1202 stat = STATUS_OVERFLOW;
1203 return stat;
1204 }
1205
1206 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1207 static inline void
1208 addr16_hi(unsigned char* view, Address value)
1209 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1210
1211 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1212 static inline void
1213 addr16_ha(unsigned char* view, Address value)
1214 { This::addr16_hi(view, value + 0x8000); }
1215
1216 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1217 static inline void
1218 addr16_hi2(unsigned char* view, Address value)
1219 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1220
1221 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1222 static inline void
1223 addr16_ha2(unsigned char* view, Address value)
1224 { This::addr16_hi2(view, value + 0x8000); }
1225
1226 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1227 static inline void
1228 addr16_hi3(unsigned char* view, Address value)
1229 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1230
1231 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1232 static inline void
1233 addr16_ha3(unsigned char* view, Address value)
1234 { This::addr16_hi3(view, value + 0x8000); }
1235
1236 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1237 static inline Status
1238 addr14(unsigned char* view, Address value, Overflow_check overflow)
1239 {
1240 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1241 if (overflow != CHECK_NONE && (value & 3) != 0)
1242 stat = STATUS_OVERFLOW;
1243 return stat;
1244 }
1245 };
1246
1247 // Stash away the index of .got2 or .opd in a relocatable object, if
1248 // such a section exists.
1249
1250 template<int size, bool big_endian>
1251 bool
1252 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1253 Read_symbols_data* sd)
1254 {
1255 const unsigned char* const pshdrs = sd->section_headers->data();
1256 const unsigned char* namesu = sd->section_names->data();
1257 const char* names = reinterpret_cast<const char*>(namesu);
1258 section_size_type names_size = sd->section_names_size;
1259 const unsigned char* s;
1260
1261 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1262 names, names_size, NULL);
1263 if (s != NULL)
1264 {
1265 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1266 this->special_ = ndx;
1267 }
1268 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1269 }
1270
1271 // Examine .rela.opd to build info about function entry points.
1272
1273 template<int size, bool big_endian>
1274 void
1275 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1276 size_t reloc_count,
1277 const unsigned char* prelocs,
1278 const unsigned char* plocal_syms)
1279 {
1280 if (size == 64)
1281 {
1282 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1283 Reltype;
1284 const int reloc_size
1285 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1286 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1287 Address expected_off = 0;
1288 bool regular = true;
1289 unsigned int opd_ent_size = 0;
1290
1291 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1292 {
1293 Reltype reloc(prelocs);
1294 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1295 = reloc.get_r_info();
1296 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1297 if (r_type == elfcpp::R_PPC64_ADDR64)
1298 {
1299 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1300 typename elfcpp::Elf_types<size>::Elf_Addr value;
1301 bool is_ordinary;
1302 unsigned int shndx;
1303 if (r_sym < this->local_symbol_count())
1304 {
1305 typename elfcpp::Sym<size, big_endian>
1306 lsym(plocal_syms + r_sym * sym_size);
1307 shndx = lsym.get_st_shndx();
1308 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1309 value = lsym.get_st_value();
1310 }
1311 else
1312 shndx = this->symbol_section_and_value(r_sym, &value,
1313 &is_ordinary);
1314 this->set_opd_ent(reloc.get_r_offset(), shndx,
1315 value + reloc.get_r_addend());
1316 if (i == 2)
1317 {
1318 expected_off = reloc.get_r_offset();
1319 opd_ent_size = expected_off;
1320 }
1321 else if (expected_off != reloc.get_r_offset())
1322 regular = false;
1323 expected_off += opd_ent_size;
1324 }
1325 else if (r_type == elfcpp::R_PPC64_TOC)
1326 {
1327 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1328 regular = false;
1329 }
1330 else
1331 {
1332 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1333 this->name().c_str(), r_type);
1334 regular = false;
1335 }
1336 }
1337 if (reloc_count <= 2)
1338 opd_ent_size = this->section_size(this->opd_shndx());
1339 if (opd_ent_size != 24 && opd_ent_size != 16)
1340 regular = false;
1341 if (!regular)
1342 {
1343 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1344 this->name().c_str());
1345 opd_ent_size = 0;
1346 }
1347 }
1348 }
1349
1350 template<int size, bool big_endian>
1351 void
1352 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1353 {
1354 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1355 if (size == 64)
1356 {
1357 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1358 p != rd->relocs.end();
1359 ++p)
1360 {
1361 if (p->data_shndx == this->opd_shndx())
1362 {
1363 uint64_t opd_size = this->section_size(this->opd_shndx());
1364 gold_assert(opd_size == static_cast<size_t>(opd_size));
1365 if (opd_size != 0)
1366 {
1367 this->init_opd(opd_size);
1368 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1369 rd->local_symbols->data());
1370 }
1371 break;
1372 }
1373 }
1374 }
1375 }
1376
1377 // Set up PowerPC target specific relobj.
1378
1379 template<int size, bool big_endian>
1380 Object*
1381 Target_powerpc<size, big_endian>::do_make_elf_object(
1382 const std::string& name,
1383 Input_file* input_file,
1384 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1385 {
1386 int et = ehdr.get_e_type();
1387 // ET_EXEC files are valid input for --just-symbols/-R,
1388 // and we treat them as relocatable objects.
1389 if (et == elfcpp::ET_REL
1390 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1391 {
1392 Powerpc_relobj<size, big_endian>* obj =
1393 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1394 obj->setup();
1395 return obj;
1396 }
1397 else if (et == elfcpp::ET_DYN)
1398 {
1399 Sized_dynobj<size, big_endian>* obj =
1400 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1401 obj->setup();
1402 return obj;
1403 }
1404 else
1405 {
1406 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1407 return NULL;
1408 }
1409 }
1410
1411 template<int size, bool big_endian>
1412 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1413 {
1414 public:
1415 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1416 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1417
1418 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1419 : Output_data_got<size, big_endian>(),
1420 symtab_(symtab), layout_(layout),
1421 header_ent_cnt_(size == 32 ? 3 : 1),
1422 header_index_(size == 32 ? 0x2000 : 0)
1423 {}
1424
1425 class Got_entry;
1426
1427 // Create a new GOT entry and return its offset.
1428 unsigned int
1429 add_got_entry(Got_entry got_entry)
1430 {
1431 this->reserve_ent();
1432 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1433 }
1434
1435 // Create a pair of new GOT entries and return the offset of the first.
1436 unsigned int
1437 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1438 {
1439 this->reserve_ent(2);
1440 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1441 got_entry_2);
1442 }
1443
1444 unsigned int
1445 add_constant_pair(Valtype c1, Valtype c2)
1446 {
1447 this->reserve_ent(2);
1448 unsigned int got_offset = this->add_constant(c1);
1449 this->add_constant(c2);
1450 return got_offset;
1451 }
1452
1453 // Offset of _GLOBAL_OFFSET_TABLE_.
1454 unsigned int
1455 g_o_t() const
1456 {
1457 return this->got_offset(this->header_index_);
1458 }
1459
1460 // Offset of base used to access the GOT/TOC.
1461 // The got/toc pointer reg will be set to this value.
1462 typename elfcpp::Elf_types<size>::Elf_Off
1463 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1464 {
1465 if (size == 32)
1466 return this->g_o_t();
1467 else
1468 return (this->output_section()->address()
1469 + object->toc_base_offset()
1470 - this->address());
1471 }
1472
1473 // Ensure our GOT has a header.
1474 void
1475 set_final_data_size()
1476 {
1477 if (this->header_ent_cnt_ != 0)
1478 this->make_header();
1479 Output_data_got<size, big_endian>::set_final_data_size();
1480 }
1481
1482 // First word of GOT header needs some values that are not
1483 // handled by Output_data_got so poke them in here.
1484 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1485 void
1486 do_write(Output_file* of)
1487 {
1488 Valtype val = 0;
1489 if (size == 32 && this->layout_->dynamic_data() != NULL)
1490 val = this->layout_->dynamic_section()->address();
1491 if (size == 64)
1492 val = this->output_section()->address() + 0x8000;
1493 this->replace_constant(this->header_index_, val);
1494 Output_data_got<size, big_endian>::do_write(of);
1495 }
1496
1497 private:
1498 void
1499 reserve_ent(unsigned int cnt = 1)
1500 {
1501 if (this->header_ent_cnt_ == 0)
1502 return;
1503 if (this->num_entries() + cnt > this->header_index_)
1504 this->make_header();
1505 }
1506
1507 void
1508 make_header()
1509 {
1510 this->header_ent_cnt_ = 0;
1511 this->header_index_ = this->num_entries();
1512 if (size == 32)
1513 {
1514 Output_data_got<size, big_endian>::add_constant(0);
1515 Output_data_got<size, big_endian>::add_constant(0);
1516 Output_data_got<size, big_endian>::add_constant(0);
1517
1518 // Define _GLOBAL_OFFSET_TABLE_ at the header
1519 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1520 Symbol_table::PREDEFINED,
1521 this, this->g_o_t(), 0,
1522 elfcpp::STT_OBJECT,
1523 elfcpp::STB_LOCAL,
1524 elfcpp::STV_HIDDEN,
1525 0, false, false);
1526 }
1527 else
1528 Output_data_got<size, big_endian>::add_constant(0);
1529 }
1530
1531 // Stashed pointers.
1532 Symbol_table* symtab_;
1533 Layout* layout_;
1534
1535 // GOT header size.
1536 unsigned int header_ent_cnt_;
1537 // GOT header index.
1538 unsigned int header_index_;
1539 };
1540
1541 // Get the GOT section, creating it if necessary.
1542
1543 template<int size, bool big_endian>
1544 Output_data_got_powerpc<size, big_endian>*
1545 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1546 Layout* layout)
1547 {
1548 if (this->got_ == NULL)
1549 {
1550 gold_assert(symtab != NULL && layout != NULL);
1551
1552 this->got_
1553 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1554
1555 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1556 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1557 this->got_, ORDER_DATA, false);
1558 }
1559
1560 return this->got_;
1561 }
1562
1563 // Get the dynamic reloc section, creating it if necessary.
1564
1565 template<int size, bool big_endian>
1566 typename Target_powerpc<size, big_endian>::Reloc_section*
1567 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1568 {
1569 if (this->rela_dyn_ == NULL)
1570 {
1571 gold_assert(layout != NULL);
1572 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1573 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1574 elfcpp::SHF_ALLOC, this->rela_dyn_,
1575 ORDER_DYNAMIC_RELOCS, false);
1576 }
1577 return this->rela_dyn_;
1578 }
1579
1580 // A class to handle the PLT data.
1581
1582 template<int size, bool big_endian>
1583 class Output_data_plt_powerpc : public Output_section_data_build
1584 {
1585 public:
1586 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1587 size, big_endian> Reloc_section;
1588
1589 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
1590 Reloc_section* plt_rel,
1591 unsigned int reserved_size,
1592 const char* name)
1593 : Output_section_data_build(size == 32 ? 4 : 8),
1594 rel_(plt_rel),
1595 targ_(targ),
1596 initial_plt_entry_size_(reserved_size),
1597 name_(name)
1598 { }
1599
1600 // Add an entry to the PLT.
1601 void
1602 add_entry(Symbol*);
1603
1604 void
1605 add_ifunc_entry(Symbol*);
1606
1607 void
1608 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
1609
1610 // Return the .rela.plt section data.
1611 Reloc_section*
1612 rel_plt() const
1613 {
1614 return this->rel_;
1615 }
1616
1617 // Return the number of PLT entries.
1618 unsigned int
1619 entry_count() const
1620 {
1621 return ((this->current_data_size() - this->initial_plt_entry_size_)
1622 / plt_entry_size);
1623 }
1624
1625 // Return the offset of the first non-reserved PLT entry.
1626 unsigned int
1627 first_plt_entry_offset()
1628 { return this->initial_plt_entry_size_; }
1629
1630 // Return the size of a PLT entry.
1631 static unsigned int
1632 get_plt_entry_size()
1633 { return plt_entry_size; }
1634
1635 protected:
1636 void
1637 do_adjust_output_section(Output_section* os)
1638 {
1639 os->set_entsize(0);
1640 }
1641
1642 // Write to a map file.
1643 void
1644 do_print_to_mapfile(Mapfile* mapfile) const
1645 { mapfile->print_output_data(this, this->name_); }
1646
1647 private:
1648 // The size of an entry in the PLT.
1649 static const int plt_entry_size = size == 32 ? 4 : 24;
1650
1651 // Write out the PLT data.
1652 void
1653 do_write(Output_file*);
1654
1655 // The reloc section.
1656 Reloc_section* rel_;
1657 // Allows access to .glink for do_write.
1658 Target_powerpc<size, big_endian>* targ_;
1659 // The size of the first reserved entry.
1660 int initial_plt_entry_size_;
1661 // What to report in map file.
1662 const char *name_;
1663 };
1664
1665 // Add an entry to the PLT.
1666
1667 template<int size, bool big_endian>
1668 void
1669 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1670 {
1671 if (!gsym->has_plt_offset())
1672 {
1673 off_t off = this->current_data_size();
1674 if (off == 0)
1675 off += this->first_plt_entry_offset();
1676 gsym->set_plt_offset(off);
1677 gsym->set_needs_dynsym_entry();
1678 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
1679 this->rel_->add_global(gsym, dynrel, this, off, 0);
1680 off += plt_entry_size;
1681 this->set_current_data_size(off);
1682 }
1683 }
1684
1685 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
1686
1687 template<int size, bool big_endian>
1688 void
1689 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
1690 {
1691 if (!gsym->has_plt_offset())
1692 {
1693 off_t off = this->current_data_size();
1694 gsym->set_plt_offset(off);
1695 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1696 if (size == 64)
1697 dynrel = elfcpp::R_PPC64_JMP_IREL;
1698 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
1699 off += plt_entry_size;
1700 this->set_current_data_size(off);
1701 }
1702 }
1703
1704 // Add an entry for a local ifunc symbol to the IPLT.
1705
1706 template<int size, bool big_endian>
1707 void
1708 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
1709 Sized_relobj_file<size, big_endian>* relobj,
1710 unsigned int local_sym_index)
1711 {
1712 if (!relobj->local_has_plt_offset(local_sym_index))
1713 {
1714 off_t off = this->current_data_size();
1715 relobj->set_local_plt_offset(local_sym_index, off);
1716 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
1717 if (size == 64)
1718 dynrel = elfcpp::R_PPC64_JMP_IREL;
1719 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
1720 this, off, 0);
1721 off += plt_entry_size;
1722 this->set_current_data_size(off);
1723 }
1724 }
1725
1726 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1727 static const uint32_t add_3_3_2 = 0x7c631214;
1728 static const uint32_t add_3_3_13 = 0x7c636a14;
1729 static const uint32_t add_11_0_11 = 0x7d605a14;
1730 static const uint32_t add_12_2_11 = 0x7d825a14;
1731 static const uint32_t addi_11_11 = 0x396b0000;
1732 static const uint32_t addi_12_12 = 0x398c0000;
1733 static const uint32_t addi_2_2 = 0x38420000;
1734 static const uint32_t addi_3_2 = 0x38620000;
1735 static const uint32_t addi_3_3 = 0x38630000;
1736 static const uint32_t addis_0_2 = 0x3c020000;
1737 static const uint32_t addis_0_13 = 0x3c0d0000;
1738 static const uint32_t addis_11_11 = 0x3d6b0000;
1739 static const uint32_t addis_11_30 = 0x3d7e0000;
1740 static const uint32_t addis_12_12 = 0x3d8c0000;
1741 static const uint32_t addis_12_2 = 0x3d820000;
1742 static const uint32_t addis_3_2 = 0x3c620000;
1743 static const uint32_t addis_3_13 = 0x3c6d0000;
1744 static const uint32_t b = 0x48000000;
1745 static const uint32_t bcl_20_31 = 0x429f0005;
1746 static const uint32_t bctr = 0x4e800420;
1747 static const uint32_t blr = 0x4e800020;
1748 static const uint32_t blrl = 0x4e800021;
1749 static const uint32_t cror_15_15_15 = 0x4def7b82;
1750 static const uint32_t cror_31_31_31 = 0x4ffffb82;
1751 static const uint32_t ld_0_1 = 0xe8010000;
1752 static const uint32_t ld_0_12 = 0xe80c0000;
1753 static const uint32_t ld_11_12 = 0xe96c0000;
1754 static const uint32_t ld_11_2 = 0xe9620000;
1755 static const uint32_t ld_2_1 = 0xe8410000;
1756 static const uint32_t ld_2_11 = 0xe84b0000;
1757 static const uint32_t ld_2_12 = 0xe84c0000;
1758 static const uint32_t ld_2_2 = 0xe8420000;
1759 static const uint32_t lfd_0_1 = 0xc8010000;
1760 static const uint32_t li_0_0 = 0x38000000;
1761 static const uint32_t li_12_0 = 0x39800000;
1762 static const uint32_t lis_0_0 = 0x3c000000;
1763 static const uint32_t lis_11 = 0x3d600000;
1764 static const uint32_t lis_12 = 0x3d800000;
1765 static const uint32_t lwz_0_12 = 0x800c0000;
1766 static const uint32_t lwz_11_11 = 0x816b0000;
1767 static const uint32_t lwz_11_30 = 0x817e0000;
1768 static const uint32_t lwz_12_12 = 0x818c0000;
1769 static const uint32_t lwzu_0_12 = 0x840c0000;
1770 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
1771 static const uint32_t mflr_0 = 0x7c0802a6;
1772 static const uint32_t mflr_11 = 0x7d6802a6;
1773 static const uint32_t mflr_12 = 0x7d8802a6;
1774 static const uint32_t mtctr_0 = 0x7c0903a6;
1775 static const uint32_t mtctr_11 = 0x7d6903a6;
1776 static const uint32_t mtlr_0 = 0x7c0803a6;
1777 static const uint32_t mtlr_12 = 0x7d8803a6;
1778 static const uint32_t nop = 0x60000000;
1779 static const uint32_t ori_0_0_0 = 0x60000000;
1780 static const uint32_t std_0_1 = 0xf8010000;
1781 static const uint32_t std_0_12 = 0xf80c0000;
1782 static const uint32_t std_2_1 = 0xf8410000;
1783 static const uint32_t stfd_0_1 = 0xd8010000;
1784 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
1785 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1786
1787 // Write out the PLT.
1788
1789 template<int size, bool big_endian>
1790 void
1791 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1792 {
1793 if (size == 32)
1794 {
1795 const off_t offset = this->offset();
1796 const section_size_type oview_size
1797 = convert_to_section_size_type(this->data_size());
1798 unsigned char* const oview = of->get_output_view(offset, oview_size);
1799 unsigned char* pov = oview;
1800 unsigned char* endpov = oview + oview_size;
1801
1802 // The address of the .glink branch table
1803 const Output_data_glink<size, big_endian>* glink
1804 = this->targ_->glink_section();
1805 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1806 = glink->address() + glink->pltresolve();
1807
1808 while (pov < endpov)
1809 {
1810 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1811 pov += 4;
1812 branch_tab += 4;
1813 }
1814
1815 of->write_output_view(offset, oview_size, oview);
1816 }
1817 }
1818
1819 // Create the PLT section.
1820
1821 template<int size, bool big_endian>
1822 void
1823 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1824 {
1825 if (this->plt_ == NULL)
1826 {
1827 if (this->glink_ == NULL)
1828 make_glink_section(layout);
1829
1830 // Ensure that .rela.dyn always appears before .rela.plt This is
1831 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1832 // needs to include .rela.plt in it's range.
1833 this->rela_dyn_section(layout);
1834
1835 Reloc_section* plt_rel = new Reloc_section(false);
1836 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1837 elfcpp::SHF_ALLOC, plt_rel,
1838 ORDER_DYNAMIC_PLT_RELOCS, false);
1839 this->plt_
1840 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
1841 size == 32 ? 0 : 24,
1842 "** PLT");
1843 layout->add_output_section_data(".plt",
1844 (size == 32
1845 ? elfcpp::SHT_PROGBITS
1846 : elfcpp::SHT_NOBITS),
1847 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1848 this->plt_,
1849 (size == 32
1850 ? ORDER_SMALL_DATA
1851 : ORDER_SMALL_BSS),
1852 false);
1853 }
1854 }
1855
1856 // Create the IPLT section.
1857
1858 template<int size, bool big_endian>
1859 void
1860 Target_powerpc<size, big_endian>::make_iplt_section(Layout* layout)
1861 {
1862 if (this->iplt_ == NULL)
1863 {
1864 this->make_plt_section(layout);
1865
1866 Reloc_section* iplt_rel = new Reloc_section(false);
1867 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
1868 this->iplt_
1869 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
1870 0, "** IPLT");
1871 this->plt_->output_section()->add_output_section_data(this->iplt_);
1872 }
1873 }
1874
1875 // A class to handle .glink.
1876
1877 template<int size, bool big_endian>
1878 class Output_data_glink : public Output_section_data
1879 {
1880 public:
1881 static const int pltresolve_size = 16*4;
1882
1883 Output_data_glink(Target_powerpc<size, big_endian>*);
1884
1885 // Add an entry
1886 void
1887 add_entry(const Sized_relobj_file<size, big_endian>*,
1888 const Symbol*,
1889 const elfcpp::Rela<size, big_endian>&);
1890
1891 void
1892 add_entry(const Sized_relobj_file<size, big_endian>*,
1893 unsigned int,
1894 const elfcpp::Rela<size, big_endian>&);
1895
1896 unsigned int
1897 find_entry(const Symbol*) const;
1898
1899 unsigned int
1900 find_entry(const Sized_relobj_file<size, big_endian>*, unsigned int) const;
1901
1902 unsigned int
1903 find_entry(const Sized_relobj_file<size, big_endian>*,
1904 const Symbol*,
1905 const elfcpp::Rela<size, big_endian>&) const;
1906
1907 unsigned int
1908 find_entry(const Sized_relobj_file<size, big_endian>*,
1909 unsigned int,
1910 const elfcpp::Rela<size, big_endian>&) const;
1911
1912 unsigned int
1913 glink_entry_size() const
1914 {
1915 if (size == 32)
1916 return 4 * 4;
1917 else
1918 // FIXME: We should be using multiple glink sections for
1919 // stubs to support > 33M applications.
1920 return 8 * 4;
1921 }
1922
1923 off_t
1924 pltresolve() const
1925 {
1926 return this->pltresolve_;
1927 }
1928
1929 protected:
1930 // Write to a map file.
1931 void
1932 do_print_to_mapfile(Mapfile* mapfile) const
1933 { mapfile->print_output_data(this, _("** glink")); }
1934
1935 private:
1936 void
1937 set_final_data_size();
1938
1939 // Write out .glink
1940 void
1941 do_write(Output_file*);
1942
1943 class Glink_sym_ent
1944 {
1945 public:
1946 Glink_sym_ent(const Symbol* sym)
1947 : sym_(sym), object_(0), addend_(0), locsym_(0)
1948 { }
1949
1950 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1951 unsigned int locsym_index)
1952 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1953 { }
1954
1955 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1956 const Symbol* sym,
1957 const elfcpp::Rela<size, big_endian>& reloc)
1958 : sym_(sym), object_(0), addend_(0), locsym_(0)
1959 {
1960 if (size != 32)
1961 this->addend_ = reloc.get_r_addend();
1962 else if (parameters->options().output_is_position_independent()
1963 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1964 == elfcpp::R_PPC_PLTREL24))
1965 {
1966 this->addend_ = reloc.get_r_addend();
1967 if (this->addend_ >= 32768)
1968 this->object_ = object;
1969 }
1970 }
1971
1972 Glink_sym_ent(const Sized_relobj_file<size, big_endian>* object,
1973 unsigned int locsym_index,
1974 const elfcpp::Rela<size, big_endian>& reloc)
1975 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
1976 {
1977 if (size != 32)
1978 this->addend_ = reloc.get_r_addend();
1979 else if (parameters->options().output_is_position_independent()
1980 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1981 == elfcpp::R_PPC_PLTREL24))
1982 this->addend_ = reloc.get_r_addend();
1983 }
1984
1985 bool operator==(const Glink_sym_ent& that) const
1986 {
1987 return (this->sym_ == that.sym_
1988 && this->object_ == that.object_
1989 && this->addend_ == that.addend_
1990 && this->locsym_ == that.locsym_);
1991 }
1992
1993 const Symbol* sym_;
1994 const Sized_relobj_file<size, big_endian>* object_;
1995 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
1996 unsigned int locsym_;
1997 };
1998
1999 class Glink_sym_ent_hash
2000 {
2001 public:
2002 size_t operator()(const Glink_sym_ent& ent) const
2003 {
2004 return (reinterpret_cast<uintptr_t>(ent.sym_)
2005 ^ reinterpret_cast<uintptr_t>(ent.object_)
2006 ^ ent.addend_
2007 ^ ent.locsym_);
2008 }
2009 };
2010
2011 // Map sym/object/addend to index.
2012 typedef Unordered_map<Glink_sym_ent, unsigned int,
2013 Glink_sym_ent_hash> Glink_entries;
2014 Glink_entries glink_entries_;
2015
2016 // Offset of pltresolve stub (actually, branch table for 32-bit)
2017 off_t pltresolve_;
2018
2019 // Allows access to .got and .plt for do_write.
2020 Target_powerpc<size, big_endian>* targ_;
2021 };
2022
2023 // Create the glink section.
2024
2025 template<int size, bool big_endian>
2026 Output_data_glink<size, big_endian>::Output_data_glink(
2027 Target_powerpc<size, big_endian>* targ)
2028 : Output_section_data(16),
2029 pltresolve_(0), targ_(targ)
2030 {
2031 }
2032
2033 // Add an entry to glink, if we do not already have one for this
2034 // sym/object/addend combo.
2035
2036 template<int size, bool big_endian>
2037 void
2038 Output_data_glink<size, big_endian>::add_entry(
2039 const Sized_relobj_file<size, big_endian>* object,
2040 const Symbol* gsym,
2041 const elfcpp::Rela<size, big_endian>& reloc)
2042 {
2043 Glink_sym_ent ent(object, gsym, reloc);
2044 unsigned int indx = this->glink_entries_.size();
2045 this->glink_entries_.insert(std::make_pair(ent, indx));
2046 }
2047
2048 template<int size, bool big_endian>
2049 void
2050 Output_data_glink<size, big_endian>::add_entry(
2051 const Sized_relobj_file<size, big_endian>* object,
2052 unsigned int locsym_index,
2053 const elfcpp::Rela<size, big_endian>& reloc)
2054 {
2055 Glink_sym_ent ent(object, locsym_index, reloc);
2056 unsigned int indx = this->glink_entries_.size();
2057 this->glink_entries_.insert(std::make_pair(ent, indx));
2058 }
2059
2060 template<int size, bool big_endian>
2061 unsigned int
2062 Output_data_glink<size, big_endian>::find_entry(
2063 const Sized_relobj_file<size, big_endian>* object,
2064 const Symbol* gsym,
2065 const elfcpp::Rela<size, big_endian>& reloc) const
2066 {
2067 Glink_sym_ent ent(object, gsym, reloc);
2068 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2069 gold_assert(p != this->glink_entries_.end());
2070 return p->second;
2071 }
2072
2073 template<int size, bool big_endian>
2074 unsigned int
2075 Output_data_glink<size, big_endian>::find_entry(const Symbol* gsym) const
2076 {
2077 Glink_sym_ent ent(gsym);
2078 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2079 gold_assert(p != this->glink_entries_.end());
2080 return p->second;
2081 }
2082
2083 template<int size, bool big_endian>
2084 unsigned int
2085 Output_data_glink<size, big_endian>::find_entry(
2086 const Sized_relobj_file<size, big_endian>* object,
2087 unsigned int locsym_index,
2088 const elfcpp::Rela<size, big_endian>& reloc) const
2089 {
2090 Glink_sym_ent ent(object, locsym_index, reloc);
2091 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2092 gold_assert(p != this->glink_entries_.end());
2093 return p->second;
2094 }
2095
2096 template<int size, bool big_endian>
2097 unsigned int
2098 Output_data_glink<size, big_endian>::find_entry(
2099 const Sized_relobj_file<size, big_endian>* object,
2100 unsigned int locsym_index) const
2101 {
2102 Glink_sym_ent ent(object, locsym_index);
2103 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
2104 gold_assert(p != this->glink_entries_.end());
2105 return p->second;
2106 }
2107
2108 template<int size, bool big_endian>
2109 void
2110 Output_data_glink<size, big_endian>::set_final_data_size()
2111 {
2112 unsigned int count = this->glink_entries_.size();
2113 off_t total = count;
2114
2115 if (count != 0)
2116 {
2117 if (size == 32)
2118 {
2119 total *= 16;
2120 this->pltresolve_ = total;
2121
2122 // space for branch table
2123 total += 4 * (count - 1);
2124
2125 total += -total & 15;
2126 total += this->pltresolve_size;
2127 }
2128 else
2129 {
2130 total *= 32;
2131 this->pltresolve_ = total;
2132 total += this->pltresolve_size;
2133
2134 // space for branch table
2135 total += 8 * count;
2136 if (count > 0x8000)
2137 total += 4 * (count - 0x8000);
2138 }
2139 }
2140
2141 this->set_data_size(total);
2142 }
2143
2144 static inline uint32_t
2145 l(uint32_t a)
2146 {
2147 return a & 0xffff;
2148 }
2149
2150 static inline uint32_t
2151 hi(uint32_t a)
2152 {
2153 return l(a >> 16);
2154 }
2155
2156 static inline uint32_t
2157 ha(uint32_t a)
2158 {
2159 return hi(a + 0x8000);
2160 }
2161
2162 template<bool big_endian>
2163 static inline void
2164 write_insn(unsigned char* p, uint32_t v)
2165 {
2166 elfcpp::Swap<32, big_endian>::writeval(p, v);
2167 }
2168
2169 // Write out .glink.
2170
2171 template<int size, bool big_endian>
2172 void
2173 Output_data_glink<size, big_endian>::do_write(Output_file* of)
2174 {
2175 const off_t off = this->offset();
2176 const section_size_type oview_size =
2177 convert_to_section_size_type(this->data_size());
2178 unsigned char* const oview = of->get_output_view(off, oview_size);
2179 unsigned char* p;
2180
2181 // The base address of the .plt section.
2182 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2183 static const Address invalid_address = static_cast<Address>(0) - 1;
2184 Address plt_base = this->targ_->plt_section()->address();
2185 Address iplt_base = invalid_address;
2186
2187 const Output_data_got_powerpc<size, big_endian>* got
2188 = this->targ_->got_section();
2189
2190 if (size == 64)
2191 {
2192 Address got_os_addr = got->output_section()->address();
2193
2194 // Write out call stubs.
2195 typename Glink_entries::const_iterator g;
2196 for (g = this->glink_entries_.begin();
2197 g != this->glink_entries_.end();
2198 ++g)
2199 {
2200 Address plt_addr;
2201 bool is_ifunc;
2202 const Symbol* gsym = g->first.sym_;
2203 if (gsym != NULL)
2204 {
2205 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2206 && gsym->can_use_relative_reloc(false));
2207 plt_addr = gsym->plt_offset();
2208 }
2209 else
2210 {
2211 is_ifunc = true;
2212 const Sized_relobj_file<size, big_endian>* relobj
2213 = g->first.object_;
2214 unsigned int local_sym_index = g->first.locsym_;
2215 plt_addr = relobj->local_plt_offset(local_sym_index);
2216 }
2217 if (is_ifunc)
2218 {
2219 if (iplt_base == invalid_address)
2220 iplt_base = this->targ_->iplt_section()->address();
2221 plt_addr += iplt_base;
2222 }
2223 else
2224 plt_addr += plt_base;
2225 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2226 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2227 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
2228 Address pltoff = plt_addr - got_addr;
2229
2230 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
2231 gold_error(_("%s: linkage table error against `%s'"),
2232 g->first.object_->name().c_str(),
2233 g->first.sym_->demangled_name().c_str());
2234
2235 p = oview + g->second * this->glink_entry_size();
2236 if (ha(pltoff) != 0)
2237 {
2238 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
2239 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2240 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
2241 if (ha(pltoff + 16) != ha(pltoff))
2242 {
2243 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
2244 pltoff = 0;
2245 }
2246 write_insn<big_endian>(p, mtctr_11), p += 4;
2247 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
2248 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
2249 write_insn<big_endian>(p, bctr), p += 4;
2250 }
2251 else
2252 {
2253 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
2254 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
2255 if (ha(pltoff + 16) != ha(pltoff))
2256 {
2257 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
2258 pltoff = 0;
2259 }
2260 write_insn<big_endian>(p, mtctr_11), p += 4;
2261 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
2262 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
2263 write_insn<big_endian>(p, bctr), p += 4;
2264 }
2265 }
2266
2267 // Write pltresolve stub.
2268 p = oview + this->pltresolve_;
2269 Address after_bcl = this->address() + this->pltresolve_ + 16;
2270 Address pltoff = plt_base - after_bcl;
2271
2272 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
2273
2274 write_insn<big_endian>(p, mflr_12), p += 4;
2275 write_insn<big_endian>(p, bcl_20_31), p += 4;
2276 write_insn<big_endian>(p, mflr_11), p += 4;
2277 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
2278 write_insn<big_endian>(p, mtlr_12), p += 4;
2279 write_insn<big_endian>(p, add_12_2_11), p += 4;
2280 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
2281 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
2282 write_insn<big_endian>(p, mtctr_11), p += 4;
2283 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
2284 write_insn<big_endian>(p, bctr), p += 4;
2285 while (p < oview + this->pltresolve_ + this->pltresolve_size)
2286 write_insn<big_endian>(p, nop), p += 4;
2287
2288 // Write lazy link call stubs.
2289 uint32_t indx = 0;
2290 while (p < oview + oview_size)
2291 {
2292 if (indx < 0x8000)
2293 {
2294 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
2295 }
2296 else
2297 {
2298 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
2299 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
2300 }
2301 uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
2302 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
2303 indx++;
2304 }
2305 }
2306 else
2307 {
2308 // The address of _GLOBAL_OFFSET_TABLE_.
2309 Address g_o_t = got->address() + got->g_o_t();
2310
2311 // Write out call stubs.
2312 typename Glink_entries::const_iterator g;
2313 for (g = this->glink_entries_.begin();
2314 g != this->glink_entries_.end();
2315 ++g)
2316 {
2317 Address plt_addr;
2318 bool is_ifunc;
2319 const Symbol* gsym = g->first.sym_;
2320 if (gsym != NULL)
2321 {
2322 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
2323 && gsym->can_use_relative_reloc(false));
2324 plt_addr = gsym->plt_offset();
2325 }
2326 else
2327 {
2328 is_ifunc = true;
2329 const Sized_relobj_file<size, big_endian>* relobj
2330 = g->first.object_;
2331 unsigned int local_sym_index = g->first.locsym_;
2332 plt_addr = relobj->local_plt_offset(local_sym_index);
2333 }
2334 if (is_ifunc)
2335 {
2336 if (iplt_base == invalid_address)
2337 iplt_base = this->targ_->iplt_section()->address();
2338 plt_addr += iplt_base;
2339 }
2340 else
2341 plt_addr += plt_base;
2342
2343 p = oview + g->second * this->glink_entry_size();
2344 if (parameters->options().output_is_position_independent())
2345 {
2346 Address got_addr;
2347 const Powerpc_relobj<size, big_endian>* object = static_cast
2348 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
2349 if (object != NULL && g->first.addend_ >= 32768)
2350 {
2351 unsigned int got2 = object->got2_shndx();
2352 got_addr = g->first.object_->get_output_section_offset(got2);
2353 gold_assert(got_addr != invalid_address);
2354 got_addr += (g->first.object_->output_section(got2)->address()
2355 + g->first.addend_);
2356 }
2357 else
2358 got_addr = g_o_t;
2359
2360 Address pltoff = plt_addr - got_addr;
2361 if (ha(pltoff) == 0)
2362 {
2363 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
2364 write_insn<big_endian>(p + 4, mtctr_11);
2365 write_insn<big_endian>(p + 8, bctr);
2366 }
2367 else
2368 {
2369 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
2370 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
2371 write_insn<big_endian>(p + 8, mtctr_11);
2372 write_insn<big_endian>(p + 12, bctr);
2373 }
2374 }
2375 else
2376 {
2377 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
2378 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
2379 write_insn<big_endian>(p + 8, mtctr_11);
2380 write_insn<big_endian>(p + 12, bctr);
2381 }
2382 }
2383
2384 // Write out pltresolve branch table.
2385 p = oview + this->pltresolve_;
2386 unsigned int the_end = oview_size - this->pltresolve_size;
2387 unsigned char* end_p = oview + the_end;
2388 while (p < end_p - 8 * 4)
2389 write_insn<big_endian>(p, b + end_p - p), p += 4;
2390 while (p < end_p)
2391 write_insn<big_endian>(p, nop), p += 4;
2392
2393 // Write out pltresolve call stub.
2394 if (parameters->options().output_is_position_independent())
2395 {
2396 Address res0_off = this->pltresolve_;
2397 Address after_bcl_off = the_end + 12;
2398 Address bcl_res0 = after_bcl_off - res0_off;
2399
2400 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
2401 write_insn<big_endian>(p + 4, mflr_0);
2402 write_insn<big_endian>(p + 8, bcl_20_31);
2403 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2404 write_insn<big_endian>(p + 16, mflr_12);
2405 write_insn<big_endian>(p + 20, mtlr_0);
2406 write_insn<big_endian>(p + 24, sub_11_11_12);
2407
2408 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2409
2410 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2411 if (ha(got_bcl) == ha(got_bcl + 4))
2412 {
2413 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2414 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2415 }
2416 else
2417 {
2418 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2419 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2420 }
2421 write_insn<big_endian>(p + 40, mtctr_0);
2422 write_insn<big_endian>(p + 44, add_0_11_11);
2423 write_insn<big_endian>(p + 48, add_11_0_11);
2424 write_insn<big_endian>(p + 52, bctr);
2425 write_insn<big_endian>(p + 56, nop);
2426 write_insn<big_endian>(p + 60, nop);
2427 }
2428 else
2429 {
2430 Address res0 = this->pltresolve_ + this->address();
2431
2432 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2433 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2434 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2435 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2436 else
2437 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2438 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2439 write_insn<big_endian>(p + 16, mtctr_0);
2440 write_insn<big_endian>(p + 20, add_0_11_11);
2441 if (ha(g_o_t + 4) == ha(g_o_t + 8))
2442 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2443 else
2444 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2445 write_insn<big_endian>(p + 28, add_11_0_11);
2446 write_insn<big_endian>(p + 32, bctr);
2447 write_insn<big_endian>(p + 36, nop);
2448 write_insn<big_endian>(p + 40, nop);
2449 write_insn<big_endian>(p + 44, nop);
2450 write_insn<big_endian>(p + 48, nop);
2451 write_insn<big_endian>(p + 52, nop);
2452 write_insn<big_endian>(p + 56, nop);
2453 write_insn<big_endian>(p + 60, nop);
2454 }
2455 p += 64;
2456 }
2457
2458 of->write_output_view(off, oview_size, oview);
2459 }
2460
2461
2462 // A class to handle linker generated save/restore functions.
2463
2464 template<int size, bool big_endian>
2465 class Output_data_save_res : public Output_section_data_build
2466 {
2467 public:
2468 Output_data_save_res(Symbol_table* symtab);
2469
2470 protected:
2471 // Write to a map file.
2472 void
2473 do_print_to_mapfile(Mapfile* mapfile) const
2474 { mapfile->print_output_data(this, _("** save/restore")); }
2475
2476 void
2477 do_write(Output_file*);
2478
2479 private:
2480 // The maximum size of save/restore contents.
2481 static const unsigned int savres_max = 218*4;
2482
2483 void
2484 savres_define(Symbol_table* symtab,
2485 const char *name,
2486 unsigned int lo, unsigned int hi,
2487 unsigned char* write_ent(unsigned char*, int),
2488 unsigned char* write_tail(unsigned char*, int));
2489
2490 unsigned char *contents_;
2491 };
2492
2493 template<bool big_endian>
2494 static unsigned char*
2495 savegpr0(unsigned char* p, int r)
2496 {
2497 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2498 write_insn<big_endian>(p, insn);
2499 return p + 4;
2500 }
2501
2502 template<bool big_endian>
2503 static unsigned char*
2504 savegpr0_tail(unsigned char* p, int r)
2505 {
2506 p = savegpr0<big_endian>(p, r);
2507 uint32_t insn = std_0_1 + 16;
2508 write_insn<big_endian>(p, insn);
2509 p = p + 4;
2510 write_insn<big_endian>(p, blr);
2511 return p + 4;
2512 }
2513
2514 template<bool big_endian>
2515 static unsigned char*
2516 restgpr0(unsigned char* p, int r)
2517 {
2518 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2519 write_insn<big_endian>(p, insn);
2520 return p + 4;
2521 }
2522
2523 template<bool big_endian>
2524 static unsigned char*
2525 restgpr0_tail(unsigned char* p, int r)
2526 {
2527 uint32_t insn = ld_0_1 + 16;
2528 write_insn<big_endian>(p, insn);
2529 p = p + 4;
2530 p = restgpr0<big_endian>(p, r);
2531 write_insn<big_endian>(p, mtlr_0);
2532 p = p + 4;
2533 if (r == 29)
2534 {
2535 p = restgpr0<big_endian>(p, 30);
2536 p = restgpr0<big_endian>(p, 31);
2537 }
2538 write_insn<big_endian>(p, blr);
2539 return p + 4;
2540 }
2541
2542 template<bool big_endian>
2543 static unsigned char*
2544 savegpr1(unsigned char* p, int r)
2545 {
2546 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2547 write_insn<big_endian>(p, insn);
2548 return p + 4;
2549 }
2550
2551 template<bool big_endian>
2552 static unsigned char*
2553 savegpr1_tail(unsigned char* p, int r)
2554 {
2555 p = savegpr1<big_endian>(p, r);
2556 write_insn<big_endian>(p, blr);
2557 return p + 4;
2558 }
2559
2560 template<bool big_endian>
2561 static unsigned char*
2562 restgpr1(unsigned char* p, int r)
2563 {
2564 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
2565 write_insn<big_endian>(p, insn);
2566 return p + 4;
2567 }
2568
2569 template<bool big_endian>
2570 static unsigned char*
2571 restgpr1_tail(unsigned char* p, int r)
2572 {
2573 p = restgpr1<big_endian>(p, r);
2574 write_insn<big_endian>(p, blr);
2575 return p + 4;
2576 }
2577
2578 template<bool big_endian>
2579 static unsigned char*
2580 savefpr(unsigned char* p, int r)
2581 {
2582 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2583 write_insn<big_endian>(p, insn);
2584 return p + 4;
2585 }
2586
2587 template<bool big_endian>
2588 static unsigned char*
2589 savefpr0_tail(unsigned char* p, int r)
2590 {
2591 p = savefpr<big_endian>(p, r);
2592 write_insn<big_endian>(p, std_0_1 + 16);
2593 p = p + 4;
2594 write_insn<big_endian>(p, blr);
2595 return p + 4;
2596 }
2597
2598 template<bool big_endian>
2599 static unsigned char*
2600 restfpr(unsigned char* p, int r)
2601 {
2602 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
2603 write_insn<big_endian>(p, insn);
2604 return p + 4;
2605 }
2606
2607 template<bool big_endian>
2608 static unsigned char*
2609 restfpr0_tail(unsigned char* p, int r)
2610 {
2611 write_insn<big_endian>(p, ld_0_1 + 16);
2612 p = p + 4;
2613 p = restfpr<big_endian>(p, r);
2614 write_insn<big_endian>(p, mtlr_0);
2615 p = p + 4;
2616 if (r == 29)
2617 {
2618 p = restfpr<big_endian>(p, 30);
2619 p = restfpr<big_endian>(p, 31);
2620 }
2621 write_insn<big_endian>(p, blr);
2622 return p + 4;
2623 }
2624
2625 template<bool big_endian>
2626 static unsigned char*
2627 savefpr1_tail(unsigned char* p, int r)
2628 {
2629 p = savefpr<big_endian>(p, r);
2630 write_insn<big_endian>(p, blr);
2631 return p + 4;
2632 }
2633
2634 template<bool big_endian>
2635 static unsigned char*
2636 restfpr1_tail(unsigned char* p, int r)
2637 {
2638 p = restfpr<big_endian>(p, r);
2639 write_insn<big_endian>(p, blr);
2640 return p + 4;
2641 }
2642
2643 template<bool big_endian>
2644 static unsigned char*
2645 savevr(unsigned char* p, int r)
2646 {
2647 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2648 write_insn<big_endian>(p, insn);
2649 p = p + 4;
2650 insn = stvx_0_12_0 + (r << 21);
2651 write_insn<big_endian>(p, insn);
2652 return p + 4;
2653 }
2654
2655 template<bool big_endian>
2656 static unsigned char*
2657 savevr_tail(unsigned char* p, int r)
2658 {
2659 p = savevr<big_endian>(p, r);
2660 write_insn<big_endian>(p, blr);
2661 return p + 4;
2662 }
2663
2664 template<bool big_endian>
2665 static unsigned char*
2666 restvr(unsigned char* p, int r)
2667 {
2668 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
2669 write_insn<big_endian>(p, insn);
2670 p = p + 4;
2671 insn = lvx_0_12_0 + (r << 21);
2672 write_insn<big_endian>(p, insn);
2673 return p + 4;
2674 }
2675
2676 template<bool big_endian>
2677 static unsigned char*
2678 restvr_tail(unsigned char* p, int r)
2679 {
2680 p = restvr<big_endian>(p, r);
2681 write_insn<big_endian>(p, blr);
2682 return p + 4;
2683 }
2684
2685
2686 template<int size, bool big_endian>
2687 Output_data_save_res<size, big_endian>::Output_data_save_res(
2688 Symbol_table* symtab)
2689 : Output_section_data_build(4),
2690 contents_(NULL)
2691 {
2692 this->savres_define(symtab,
2693 "_savegpr0_", 14, 31,
2694 savegpr0<big_endian>, savegpr0_tail<big_endian>);
2695 this->savres_define(symtab,
2696 "_restgpr0_", 14, 29,
2697 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2698 this->savres_define(symtab,
2699 "_restgpr0_", 30, 31,
2700 restgpr0<big_endian>, restgpr0_tail<big_endian>);
2701 this->savres_define(symtab,
2702 "_savegpr1_", 14, 31,
2703 savegpr1<big_endian>, savegpr1_tail<big_endian>);
2704 this->savres_define(symtab,
2705 "_restgpr1_", 14, 31,
2706 restgpr1<big_endian>, restgpr1_tail<big_endian>);
2707 this->savres_define(symtab,
2708 "_savefpr_", 14, 31,
2709 savefpr<big_endian>, savefpr0_tail<big_endian>);
2710 this->savres_define(symtab,
2711 "_restfpr_", 14, 29,
2712 restfpr<big_endian>, restfpr0_tail<big_endian>);
2713 this->savres_define(symtab,
2714 "_restfpr_", 30, 31,
2715 restfpr<big_endian>, restfpr0_tail<big_endian>);
2716 this->savres_define(symtab,
2717 "._savef", 14, 31,
2718 savefpr<big_endian>, savefpr1_tail<big_endian>);
2719 this->savres_define(symtab,
2720 "._restf", 14, 31,
2721 restfpr<big_endian>, restfpr1_tail<big_endian>);
2722 this->savres_define(symtab,
2723 "_savevr_", 20, 31,
2724 savevr<big_endian>, savevr_tail<big_endian>);
2725 this->savres_define(symtab,
2726 "_restvr_", 20, 31,
2727 restvr<big_endian>, restvr_tail<big_endian>);
2728 }
2729
2730 template<int size, bool big_endian>
2731 void
2732 Output_data_save_res<size, big_endian>::savres_define(
2733 Symbol_table* symtab,
2734 const char *name,
2735 unsigned int lo, unsigned int hi,
2736 unsigned char* write_ent(unsigned char*, int),
2737 unsigned char* write_tail(unsigned char*, int))
2738 {
2739 size_t len = strlen(name);
2740 bool writing = false;
2741 char sym[16];
2742
2743 memcpy(sym, name, len);
2744 sym[len + 2] = 0;
2745
2746 for (unsigned int i = lo; i <= hi; i++)
2747 {
2748 sym[len + 0] = i / 10 + '0';
2749 sym[len + 1] = i % 10 + '0';
2750 Symbol* gsym = symtab->lookup(sym);
2751 bool refd = gsym != NULL && gsym->is_undefined();
2752 writing = writing || refd;
2753 if (writing)
2754 {
2755 if (this->contents_ == NULL)
2756 this->contents_ = new unsigned char[this->savres_max];
2757
2758 off_t value = this->current_data_size();
2759 unsigned char* p = this->contents_ + value;
2760 if (i != hi)
2761 p = write_ent(p, i);
2762 else
2763 p = write_tail(p, i);
2764 off_t cur_size = p - this->contents_;
2765 this->set_current_data_size(cur_size);
2766 if (refd)
2767 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
2768 this, value, cur_size - value,
2769 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
2770 elfcpp::STV_HIDDEN, 0, false, false);
2771 }
2772 }
2773 }
2774
2775 // Write out save/restore.
2776
2777 template<int size, bool big_endian>
2778 void
2779 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
2780 {
2781 const off_t off = this->offset();
2782 const section_size_type oview_size =
2783 convert_to_section_size_type(this->data_size());
2784 unsigned char* const oview = of->get_output_view(off, oview_size);
2785 memcpy(oview, this->contents_, oview_size);
2786 of->write_output_view(off, oview_size, oview);
2787 }
2788
2789
2790 // Create the glink section.
2791
2792 template<int size, bool big_endian>
2793 void
2794 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2795 {
2796 if (this->glink_ == NULL)
2797 {
2798 this->glink_ = new Output_data_glink<size, big_endian>(this);
2799 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2800 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2801 this->glink_, ORDER_TEXT, false);
2802 }
2803 }
2804
2805 // Create a PLT entry for a global symbol.
2806
2807 template<int size, bool big_endian>
2808 void
2809 Target_powerpc<size, big_endian>::make_plt_entry(
2810 Layout* layout,
2811 Symbol* gsym,
2812 const elfcpp::Rela<size, big_endian>& reloc,
2813 const Sized_relobj_file<size, big_endian>* object)
2814 {
2815 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2816 && gsym->can_use_relative_reloc(false))
2817 {
2818 if (this->iplt_ == NULL)
2819 this->make_iplt_section(layout);
2820 this->iplt_->add_ifunc_entry(gsym);
2821 }
2822 else
2823 {
2824 if (this->plt_ == NULL)
2825 this->make_plt_section(layout);
2826 this->plt_->add_entry(gsym);
2827 }
2828 this->glink_->add_entry(object, gsym, reloc);
2829 }
2830
2831 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2832
2833 template<int size, bool big_endian>
2834 void
2835 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
2836 Layout* layout,
2837 const elfcpp::Rela<size, big_endian>& reloc,
2838 Sized_relobj_file<size, big_endian>* relobj)
2839 {
2840 if (this->iplt_ == NULL)
2841 this->make_iplt_section(layout);
2842 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2843 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
2844 this->glink_->add_entry(relobj, r_sym, reloc);
2845 }
2846
2847 // Return the number of entries in the PLT.
2848
2849 template<int size, bool big_endian>
2850 unsigned int
2851 Target_powerpc<size, big_endian>::plt_entry_count() const
2852 {
2853 if (this->plt_ == NULL)
2854 return 0;
2855 unsigned int count = this->plt_->entry_count();
2856 if (this->iplt_ != NULL)
2857 count += this->iplt_->entry_count();
2858 return count;
2859 }
2860
2861 // Return the offset of the first non-reserved PLT entry.
2862
2863 template<int size, bool big_endian>
2864 unsigned int
2865 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2866 {
2867 return this->plt_->first_plt_entry_offset();
2868 }
2869
2870 // Return the size of each PLT entry.
2871
2872 template<int size, bool big_endian>
2873 unsigned int
2874 Target_powerpc<size, big_endian>::plt_entry_size() const
2875 {
2876 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2877 }
2878
2879 // Create a GOT entry for local dynamic __tls_get_addr calls.
2880
2881 template<int size, bool big_endian>
2882 unsigned int
2883 Target_powerpc<size, big_endian>::tlsld_got_offset(
2884 Symbol_table* symtab,
2885 Layout* layout,
2886 Sized_relobj_file<size, big_endian>* object)
2887 {
2888 if (this->tlsld_got_offset_ == -1U)
2889 {
2890 gold_assert(symtab != NULL && layout != NULL && object != NULL);
2891 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2892 Output_data_got_powerpc<size, big_endian>* got
2893 = this->got_section(symtab, layout);
2894 unsigned int got_offset = got->add_constant_pair(0, 0);
2895 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2896 got_offset, 0);
2897 this->tlsld_got_offset_ = got_offset;
2898 }
2899 return this->tlsld_got_offset_;
2900 }
2901
2902 // Get the Reference_flags for a particular relocation.
2903
2904 template<int size, bool big_endian>
2905 int
2906 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2907 {
2908 switch (r_type)
2909 {
2910 case elfcpp::R_POWERPC_NONE:
2911 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2912 case elfcpp::R_POWERPC_GNU_VTENTRY:
2913 case elfcpp::R_PPC64_TOC:
2914 // No symbol reference.
2915 return 0;
2916
2917 case elfcpp::R_PPC64_ADDR64:
2918 case elfcpp::R_PPC64_UADDR64:
2919 case elfcpp::R_POWERPC_ADDR32:
2920 case elfcpp::R_POWERPC_UADDR32:
2921 case elfcpp::R_POWERPC_ADDR16:
2922 case elfcpp::R_POWERPC_UADDR16:
2923 case elfcpp::R_POWERPC_ADDR16_LO:
2924 case elfcpp::R_POWERPC_ADDR16_HI:
2925 case elfcpp::R_POWERPC_ADDR16_HA:
2926 return Symbol::ABSOLUTE_REF;
2927
2928 case elfcpp::R_POWERPC_ADDR24:
2929 case elfcpp::R_POWERPC_ADDR14:
2930 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2931 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2932 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2933
2934 case elfcpp::R_PPC64_REL64:
2935 case elfcpp::R_POWERPC_REL32:
2936 case elfcpp::R_PPC_LOCAL24PC:
2937 case elfcpp::R_POWERPC_REL16:
2938 case elfcpp::R_POWERPC_REL16_LO:
2939 case elfcpp::R_POWERPC_REL16_HI:
2940 case elfcpp::R_POWERPC_REL16_HA:
2941 return Symbol::RELATIVE_REF;
2942
2943 case elfcpp::R_POWERPC_REL24:
2944 case elfcpp::R_PPC_PLTREL24:
2945 case elfcpp::R_POWERPC_REL14:
2946 case elfcpp::R_POWERPC_REL14_BRTAKEN:
2947 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2948 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2949
2950 case elfcpp::R_POWERPC_GOT16:
2951 case elfcpp::R_POWERPC_GOT16_LO:
2952 case elfcpp::R_POWERPC_GOT16_HI:
2953 case elfcpp::R_POWERPC_GOT16_HA:
2954 case elfcpp::R_PPC64_GOT16_DS:
2955 case elfcpp::R_PPC64_GOT16_LO_DS:
2956 case elfcpp::R_PPC64_TOC16:
2957 case elfcpp::R_PPC64_TOC16_LO:
2958 case elfcpp::R_PPC64_TOC16_HI:
2959 case elfcpp::R_PPC64_TOC16_HA:
2960 case elfcpp::R_PPC64_TOC16_DS:
2961 case elfcpp::R_PPC64_TOC16_LO_DS:
2962 // Absolute in GOT.
2963 return Symbol::ABSOLUTE_REF;
2964
2965 case elfcpp::R_POWERPC_GOT_TPREL16:
2966 case elfcpp::R_POWERPC_TLS:
2967 return Symbol::TLS_REF;
2968
2969 case elfcpp::R_POWERPC_COPY:
2970 case elfcpp::R_POWERPC_GLOB_DAT:
2971 case elfcpp::R_POWERPC_JMP_SLOT:
2972 case elfcpp::R_POWERPC_RELATIVE:
2973 case elfcpp::R_POWERPC_DTPMOD:
2974 default:
2975 // Not expected. We will give an error later.
2976 return 0;
2977 }
2978 }
2979
2980 // Report an unsupported relocation against a local symbol.
2981
2982 template<int size, bool big_endian>
2983 void
2984 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2985 Sized_relobj_file<size, big_endian>* object,
2986 unsigned int r_type)
2987 {
2988 gold_error(_("%s: unsupported reloc %u against local symbol"),
2989 object->name().c_str(), r_type);
2990 }
2991
2992 // We are about to emit a dynamic relocation of type R_TYPE. If the
2993 // dynamic linker does not support it, issue an error.
2994
2995 template<int size, bool big_endian>
2996 void
2997 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2998 unsigned int r_type)
2999 {
3000 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
3001
3002 // These are the relocation types supported by glibc for both 32-bit
3003 // and 64-bit powerpc.
3004 switch (r_type)
3005 {
3006 case elfcpp::R_POWERPC_NONE:
3007 case elfcpp::R_POWERPC_RELATIVE:
3008 case elfcpp::R_POWERPC_GLOB_DAT:
3009 case elfcpp::R_POWERPC_DTPMOD:
3010 case elfcpp::R_POWERPC_DTPREL:
3011 case elfcpp::R_POWERPC_TPREL:
3012 case elfcpp::R_POWERPC_JMP_SLOT:
3013 case elfcpp::R_POWERPC_COPY:
3014 case elfcpp::R_POWERPC_IRELATIVE:
3015 case elfcpp::R_POWERPC_ADDR32:
3016 case elfcpp::R_POWERPC_UADDR32:
3017 case elfcpp::R_POWERPC_ADDR24:
3018 case elfcpp::R_POWERPC_ADDR16:
3019 case elfcpp::R_POWERPC_UADDR16:
3020 case elfcpp::R_POWERPC_ADDR16_LO:
3021 case elfcpp::R_POWERPC_ADDR16_HI:
3022 case elfcpp::R_POWERPC_ADDR16_HA:
3023 case elfcpp::R_POWERPC_ADDR14:
3024 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3025 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3026 case elfcpp::R_POWERPC_REL32:
3027 case elfcpp::R_POWERPC_REL24:
3028 case elfcpp::R_POWERPC_TPREL16:
3029 case elfcpp::R_POWERPC_TPREL16_LO:
3030 case elfcpp::R_POWERPC_TPREL16_HI:
3031 case elfcpp::R_POWERPC_TPREL16_HA:
3032 return;
3033
3034 default:
3035 break;
3036 }
3037
3038 if (size == 64)
3039 {
3040 switch (r_type)
3041 {
3042 // These are the relocation types supported only on 64-bit.
3043 case elfcpp::R_PPC64_ADDR64:
3044 case elfcpp::R_PPC64_UADDR64:
3045 case elfcpp::R_PPC64_JMP_IREL:
3046 case elfcpp::R_PPC64_ADDR16_DS:
3047 case elfcpp::R_PPC64_ADDR16_LO_DS:
3048 case elfcpp::R_PPC64_ADDR16_HIGHER:
3049 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3050 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3051 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3052 case elfcpp::R_PPC64_REL64:
3053 case elfcpp::R_POWERPC_ADDR30:
3054 case elfcpp::R_PPC64_TPREL16_DS:
3055 case elfcpp::R_PPC64_TPREL16_LO_DS:
3056 case elfcpp::R_PPC64_TPREL16_HIGHER:
3057 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3058 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3059 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3060 return;
3061
3062 default:
3063 break;
3064 }
3065 }
3066 else
3067 {
3068 switch (r_type)
3069 {
3070 // These are the relocation types supported only on 32-bit.
3071 // ??? glibc ld.so doesn't need to support these.
3072 case elfcpp::R_POWERPC_DTPREL16:
3073 case elfcpp::R_POWERPC_DTPREL16_LO:
3074 case elfcpp::R_POWERPC_DTPREL16_HI:
3075 case elfcpp::R_POWERPC_DTPREL16_HA:
3076 return;
3077
3078 default:
3079 break;
3080 }
3081 }
3082
3083 // This prevents us from issuing more than one error per reloc
3084 // section. But we can still wind up issuing more than one
3085 // error per object file.
3086 if (this->issued_non_pic_error_)
3087 return;
3088 gold_assert(parameters->options().output_is_position_independent());
3089 object->error(_("requires unsupported dynamic reloc; "
3090 "recompile with -fPIC"));
3091 this->issued_non_pic_error_ = true;
3092 return;
3093 }
3094
3095 // Return whether we need to make a PLT entry for a relocation of the
3096 // given type against a STT_GNU_IFUNC symbol.
3097
3098 template<int size, bool big_endian>
3099 bool
3100 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
3101 Sized_relobj_file<size, big_endian>* object,
3102 unsigned int r_type)
3103 {
3104 // In non-pic code any reference will resolve to the plt call stub
3105 // for the ifunc symbol.
3106 if (size == 32 && !parameters->options().output_is_position_independent())
3107 return true;
3108
3109 switch (r_type)
3110 {
3111 // Word size refs from data sections are OK.
3112 case elfcpp::R_POWERPC_ADDR32:
3113 case elfcpp::R_POWERPC_UADDR32:
3114 if (size == 32)
3115 return true;
3116 break;
3117
3118 case elfcpp::R_PPC64_ADDR64:
3119 case elfcpp::R_PPC64_UADDR64:
3120 if (size == 64)
3121 return true;
3122 break;
3123
3124 // GOT refs are good.
3125 case elfcpp::R_POWERPC_GOT16:
3126 case elfcpp::R_POWERPC_GOT16_LO:
3127 case elfcpp::R_POWERPC_GOT16_HI:
3128 case elfcpp::R_POWERPC_GOT16_HA:
3129 case elfcpp::R_PPC64_GOT16_DS:
3130 case elfcpp::R_PPC64_GOT16_LO_DS:
3131 return true;
3132
3133 // So are function calls.
3134 case elfcpp::R_POWERPC_ADDR24:
3135 case elfcpp::R_POWERPC_ADDR14:
3136 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3137 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3138 case elfcpp::R_POWERPC_REL24:
3139 case elfcpp::R_PPC_PLTREL24:
3140 case elfcpp::R_POWERPC_REL14:
3141 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3142 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3143 return true;
3144
3145 default:
3146 break;
3147 }
3148
3149 // Anything else is a problem.
3150 // If we are building a static executable, the libc startup function
3151 // responsible for applying indirect function relocations is going
3152 // to complain about the reloc type.
3153 // If we are building a dynamic executable, we will have a text
3154 // relocation. The dynamic loader will set the text segment
3155 // writable and non-executable to apply text relocations. So we'll
3156 // segfault when trying to run the indirection function to resolve
3157 // the reloc.
3158 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
3159 object->name().c_str(), r_type);
3160 return false;
3161 }
3162
3163 // Scan a relocation for a local symbol.
3164
3165 template<int size, bool big_endian>
3166 inline void
3167 Target_powerpc<size, big_endian>::Scan::local(
3168 Symbol_table* symtab,
3169 Layout* layout,
3170 Target_powerpc<size, big_endian>* target,
3171 Sized_relobj_file<size, big_endian>* object,
3172 unsigned int data_shndx,
3173 Output_section* output_section,
3174 const elfcpp::Rela<size, big_endian>& reloc,
3175 unsigned int r_type,
3176 const elfcpp::Sym<size, big_endian>& lsym,
3177 bool is_discarded)
3178 {
3179 Powerpc_relobj<size, big_endian>* ppc_object
3180 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3181
3182 if (is_discarded)
3183 {
3184 if (size == 64
3185 && data_shndx == ppc_object->opd_shndx()
3186 && r_type == elfcpp::R_PPC64_ADDR64)
3187 ppc_object->set_opd_discard(reloc.get_r_offset());
3188 return;
3189 }
3190
3191 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3192 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
3193 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
3194 target->make_local_ifunc_plt_entry(layout, reloc, object);
3195
3196 switch (r_type)
3197 {
3198 case elfcpp::R_POWERPC_NONE:
3199 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3200 case elfcpp::R_POWERPC_GNU_VTENTRY:
3201 case elfcpp::R_PPC64_TOCSAVE:
3202 case elfcpp::R_PPC_EMB_MRKREF:
3203 case elfcpp::R_POWERPC_TLS:
3204 break;
3205
3206 case elfcpp::R_PPC64_TOC:
3207 {
3208 Output_data_got_powerpc<size, big_endian>* got
3209 = target->got_section(symtab, layout);
3210 if (parameters->options().output_is_position_independent())
3211 {
3212 Address off = reloc.get_r_offset();
3213 if (size == 64
3214 && data_shndx == ppc_object->opd_shndx()
3215 && ppc_object->get_opd_discard(off - 8))
3216 break;
3217
3218 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3219 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3220 rela_dyn->add_output_section_relative(got->output_section(),
3221 elfcpp::R_POWERPC_RELATIVE,
3222 output_section,
3223 object, data_shndx, off,
3224 symobj->toc_base_offset());
3225 }
3226 }
3227 break;
3228
3229 case elfcpp::R_PPC64_ADDR64:
3230 case elfcpp::R_PPC64_UADDR64:
3231 case elfcpp::R_POWERPC_ADDR32:
3232 case elfcpp::R_POWERPC_UADDR32:
3233 case elfcpp::R_POWERPC_ADDR24:
3234 case elfcpp::R_POWERPC_ADDR16:
3235 case elfcpp::R_POWERPC_ADDR16_LO:
3236 case elfcpp::R_POWERPC_ADDR16_HI:
3237 case elfcpp::R_POWERPC_ADDR16_HA:
3238 case elfcpp::R_POWERPC_UADDR16:
3239 case elfcpp::R_PPC64_ADDR16_HIGHER:
3240 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3241 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3242 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3243 case elfcpp::R_PPC64_ADDR16_DS:
3244 case elfcpp::R_PPC64_ADDR16_LO_DS:
3245 case elfcpp::R_POWERPC_ADDR14:
3246 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3247 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3248 // If building a shared library (or a position-independent
3249 // executable), we need to create a dynamic relocation for
3250 // this location.
3251 if (parameters->options().output_is_position_independent()
3252 || (size == 64 && is_ifunc))
3253 {
3254 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3255
3256 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3257 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3258 {
3259 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3260 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3261 if (is_ifunc)
3262 {
3263 rela_dyn = target->iplt_section()->rel_plt();
3264 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3265 }
3266 rela_dyn->add_local_relative(object, r_sym, dynrel,
3267 output_section, data_shndx,
3268 reloc.get_r_offset(),
3269 reloc.get_r_addend(), false);
3270 }
3271 else
3272 {
3273 check_non_pic(object, r_type);
3274 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3275 rela_dyn->add_local(object, r_sym, r_type, output_section,
3276 data_shndx, reloc.get_r_offset(),
3277 reloc.get_r_addend());
3278 }
3279 }
3280 break;
3281
3282 case elfcpp::R_PPC64_REL64:
3283 case elfcpp::R_POWERPC_REL32:
3284 case elfcpp::R_POWERPC_REL24:
3285 case elfcpp::R_PPC_PLTREL24:
3286 case elfcpp::R_PPC_LOCAL24PC:
3287 case elfcpp::R_POWERPC_REL16:
3288 case elfcpp::R_POWERPC_REL16_LO:
3289 case elfcpp::R_POWERPC_REL16_HI:
3290 case elfcpp::R_POWERPC_REL16_HA:
3291 case elfcpp::R_POWERPC_REL14:
3292 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3293 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3294 case elfcpp::R_POWERPC_SECTOFF:
3295 case elfcpp::R_POWERPC_TPREL16:
3296 case elfcpp::R_POWERPC_DTPREL16:
3297 case elfcpp::R_POWERPC_SECTOFF_LO:
3298 case elfcpp::R_POWERPC_TPREL16_LO:
3299 case elfcpp::R_POWERPC_DTPREL16_LO:
3300 case elfcpp::R_POWERPC_SECTOFF_HI:
3301 case elfcpp::R_POWERPC_TPREL16_HI:
3302 case elfcpp::R_POWERPC_DTPREL16_HI:
3303 case elfcpp::R_POWERPC_SECTOFF_HA:
3304 case elfcpp::R_POWERPC_TPREL16_HA:
3305 case elfcpp::R_POWERPC_DTPREL16_HA:
3306 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3307 case elfcpp::R_PPC64_TPREL16_HIGHER:
3308 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3309 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3310 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3311 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3312 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3313 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3314 case elfcpp::R_PPC64_TPREL16_DS:
3315 case elfcpp::R_PPC64_TPREL16_LO_DS:
3316 case elfcpp::R_PPC64_DTPREL16_DS:
3317 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3318 case elfcpp::R_PPC64_SECTOFF_DS:
3319 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3320 case elfcpp::R_PPC64_TLSGD:
3321 case elfcpp::R_PPC64_TLSLD:
3322 break;
3323
3324 case elfcpp::R_POWERPC_GOT16:
3325 case elfcpp::R_POWERPC_GOT16_LO:
3326 case elfcpp::R_POWERPC_GOT16_HI:
3327 case elfcpp::R_POWERPC_GOT16_HA:
3328 case elfcpp::R_PPC64_GOT16_DS:
3329 case elfcpp::R_PPC64_GOT16_LO_DS:
3330 {
3331 // The symbol requires a GOT entry.
3332 Output_data_got_powerpc<size, big_endian>* got
3333 = target->got_section(symtab, layout);
3334 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3335
3336 if (!parameters->options().output_is_position_independent())
3337 {
3338 if (size == 32 && is_ifunc)
3339 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
3340 else
3341 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
3342 }
3343 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
3344 {
3345 // If we are generating a shared object or a pie, this
3346 // symbol's GOT entry will be set by a dynamic relocation.
3347 unsigned int off;
3348 off = got->add_constant(0);
3349 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
3350
3351 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3352 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3353 if (is_ifunc)
3354 {
3355 rela_dyn = target->iplt_section()->rel_plt();
3356 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3357 }
3358 rela_dyn->add_local_relative(object, r_sym, dynrel,
3359 got, off, 0, false);
3360 }
3361 }
3362 break;
3363
3364 case elfcpp::R_PPC64_TOC16:
3365 case elfcpp::R_PPC64_TOC16_LO:
3366 case elfcpp::R_PPC64_TOC16_HI:
3367 case elfcpp::R_PPC64_TOC16_HA:
3368 case elfcpp::R_PPC64_TOC16_DS:
3369 case elfcpp::R_PPC64_TOC16_LO_DS:
3370 // We need a GOT section.
3371 target->got_section(symtab, layout);
3372 break;
3373
3374 case elfcpp::R_POWERPC_GOT_TLSGD16:
3375 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3376 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3377 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3378 {
3379 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
3380 if (tls_type == tls::TLSOPT_NONE)
3381 {
3382 Output_data_got_powerpc<size, big_endian>* got
3383 = target->got_section(symtab, layout);
3384 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3385 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3386 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
3387 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
3388 }
3389 else if (tls_type == tls::TLSOPT_TO_LE)
3390 {
3391 // no GOT relocs needed for Local Exec.
3392 }
3393 else
3394 gold_unreachable();
3395 }
3396 break;
3397
3398 case elfcpp::R_POWERPC_GOT_TLSLD16:
3399 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3400 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3401 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3402 {
3403 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3404 if (tls_type == tls::TLSOPT_NONE)
3405 target->tlsld_got_offset(symtab, layout, object);
3406 else if (tls_type == tls::TLSOPT_TO_LE)
3407 {
3408 // no GOT relocs needed for Local Exec.
3409 if (parameters->options().emit_relocs())
3410 {
3411 Output_section* os = layout->tls_segment()->first_section();
3412 gold_assert(os != NULL);
3413 os->set_needs_symtab_index();
3414 }
3415 }
3416 else
3417 gold_unreachable();
3418 }
3419 break;
3420
3421 case elfcpp::R_POWERPC_GOT_DTPREL16:
3422 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3423 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3424 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3425 {
3426 Output_data_got_powerpc<size, big_endian>* got
3427 = target->got_section(symtab, layout);
3428 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3429 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
3430 }
3431 break;
3432
3433 case elfcpp::R_POWERPC_GOT_TPREL16:
3434 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3435 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3436 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3437 {
3438 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
3439 if (tls_type == tls::TLSOPT_NONE)
3440 {
3441 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3442 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
3443 {
3444 Output_data_got_powerpc<size, big_endian>* got
3445 = target->got_section(symtab, layout);
3446 unsigned int off = got->add_constant(0);
3447 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
3448
3449 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3450 rela_dyn->add_symbolless_local_addend(object, r_sym,
3451 elfcpp::R_POWERPC_TPREL,
3452 got, off, 0);
3453 }
3454 }
3455 else if (tls_type == tls::TLSOPT_TO_LE)
3456 {
3457 // no GOT relocs needed for Local Exec.
3458 }
3459 else
3460 gold_unreachable();
3461 }
3462 break;
3463
3464 default:
3465 unsupported_reloc_local(object, r_type);
3466 break;
3467 }
3468 }
3469
3470 // Report an unsupported relocation against a global symbol.
3471
3472 template<int size, bool big_endian>
3473 void
3474 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3475 Sized_relobj_file<size, big_endian>* object,
3476 unsigned int r_type,
3477 Symbol* gsym)
3478 {
3479 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3480 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3481 }
3482
3483 // Scan a relocation for a global symbol.
3484
3485 template<int size, bool big_endian>
3486 inline void
3487 Target_powerpc<size, big_endian>::Scan::global(
3488 Symbol_table* symtab,
3489 Layout* layout,
3490 Target_powerpc<size, big_endian>* target,
3491 Sized_relobj_file<size, big_endian>* object,
3492 unsigned int data_shndx,
3493 Output_section* output_section,
3494 const elfcpp::Rela<size, big_endian>& reloc,
3495 unsigned int r_type,
3496 Symbol* gsym)
3497 {
3498 Powerpc_relobj<size, big_endian>* ppc_object
3499 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3500
3501 // A STT_GNU_IFUNC symbol may require a PLT entry.
3502 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3503 && this->reloc_needs_plt_for_ifunc(object, r_type))
3504 target->make_plt_entry(layout, gsym, reloc, object);
3505
3506 switch (r_type)
3507 {
3508 case elfcpp::R_POWERPC_NONE:
3509 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3510 case elfcpp::R_POWERPC_GNU_VTENTRY:
3511 case elfcpp::R_PPC_LOCAL24PC:
3512 case elfcpp::R_PPC_EMB_MRKREF:
3513 case elfcpp::R_POWERPC_TLS:
3514 break;
3515
3516 case elfcpp::R_PPC64_TOC:
3517 {
3518 Output_data_got_powerpc<size, big_endian>* got
3519 = target->got_section(symtab, layout);
3520 if (parameters->options().output_is_position_independent())
3521 {
3522 Address off = reloc.get_r_offset();
3523 if (size == 64
3524 && data_shndx == ppc_object->opd_shndx()
3525 && ppc_object->get_opd_discard(off - 8))
3526 break;
3527
3528 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3529 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3530 if (data_shndx != ppc_object->opd_shndx())
3531 symobj = static_cast
3532 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3533 rela_dyn->add_output_section_relative(got->output_section(),
3534 elfcpp::R_POWERPC_RELATIVE,
3535 output_section,
3536 object, data_shndx, off,
3537 symobj->toc_base_offset());
3538 }
3539 }
3540 break;
3541
3542 case elfcpp::R_PPC64_ADDR64:
3543 if (size == 64
3544 && data_shndx == ppc_object->opd_shndx()
3545 && (gsym->is_defined_in_discarded_section()
3546 || gsym->object() != object))
3547 {
3548 ppc_object->set_opd_discard(reloc.get_r_offset());
3549 break;
3550 }
3551 // Fall thru
3552 case elfcpp::R_PPC64_UADDR64:
3553 case elfcpp::R_POWERPC_ADDR32:
3554 case elfcpp::R_POWERPC_UADDR32:
3555 case elfcpp::R_POWERPC_ADDR24:
3556 case elfcpp::R_POWERPC_ADDR16:
3557 case elfcpp::R_POWERPC_ADDR16_LO:
3558 case elfcpp::R_POWERPC_ADDR16_HI:
3559 case elfcpp::R_POWERPC_ADDR16_HA:
3560 case elfcpp::R_POWERPC_UADDR16:
3561 case elfcpp::R_PPC64_ADDR16_HIGHER:
3562 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3563 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3564 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3565 case elfcpp::R_PPC64_ADDR16_DS:
3566 case elfcpp::R_PPC64_ADDR16_LO_DS:
3567 case elfcpp::R_POWERPC_ADDR14:
3568 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3569 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3570 {
3571 // Make a PLT entry if necessary.
3572 if (gsym->needs_plt_entry())
3573 {
3574 target->make_plt_entry(layout, gsym, reloc, 0);
3575 // Since this is not a PC-relative relocation, we may be
3576 // taking the address of a function. In that case we need to
3577 // set the entry in the dynamic symbol table to the address of
3578 // the PLT call stub.
3579 if (size == 32
3580 && gsym->is_from_dynobj()
3581 && !parameters->options().output_is_position_independent())
3582 gsym->set_needs_dynsym_value();
3583 }
3584 // Make a dynamic relocation if necessary.
3585 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3586 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3587 {
3588 if (gsym->may_need_copy_reloc())
3589 {
3590 target->copy_reloc(symtab, layout, object,
3591 data_shndx, output_section, gsym, reloc);
3592 }
3593 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3594 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3595 && (gsym->can_use_relative_reloc(false)
3596 || (size == 64
3597 && data_shndx == ppc_object->opd_shndx())))
3598 {
3599 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3600 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3601 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3602 {
3603 rela_dyn = target->iplt_section()->rel_plt();
3604 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3605 }
3606 rela_dyn->add_symbolless_global_addend(
3607 gsym, dynrel, output_section, object, data_shndx,
3608 reloc.get_r_offset(), reloc.get_r_addend());
3609 }
3610 else
3611 {
3612 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3613 check_non_pic(object, r_type);
3614 rela_dyn->add_global(gsym, r_type, output_section,
3615 object, data_shndx,
3616 reloc.get_r_offset(),
3617 reloc.get_r_addend());
3618 }
3619 }
3620 }
3621 break;
3622
3623 case elfcpp::R_PPC_PLTREL24:
3624 case elfcpp::R_POWERPC_REL24:
3625 if (gsym->needs_plt_entry()
3626 || (!gsym->final_value_is_known()
3627 && (gsym->is_undefined()
3628 || gsym->is_from_dynobj()
3629 || gsym->is_preemptible())))
3630 target->make_plt_entry(layout, gsym, reloc, object);
3631 // Fall thru
3632
3633 case elfcpp::R_PPC64_REL64:
3634 case elfcpp::R_POWERPC_REL32:
3635 // Make a dynamic relocation if necessary.
3636 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3637 {
3638 if (gsym->may_need_copy_reloc())
3639 {
3640 target->copy_reloc(symtab, layout, object,
3641 data_shndx, output_section, gsym,
3642 reloc);
3643 }
3644 else
3645 {
3646 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3647 check_non_pic(object, r_type);
3648 rela_dyn->add_global(gsym, r_type, output_section, object,
3649 data_shndx, reloc.get_r_offset(),
3650 reloc.get_r_addend());
3651 }
3652 }
3653 break;
3654
3655 case elfcpp::R_POWERPC_REL16:
3656 case elfcpp::R_POWERPC_REL16_LO:
3657 case elfcpp::R_POWERPC_REL16_HI:
3658 case elfcpp::R_POWERPC_REL16_HA:
3659 case elfcpp::R_POWERPC_REL14:
3660 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3661 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3662 case elfcpp::R_POWERPC_SECTOFF:
3663 case elfcpp::R_POWERPC_TPREL16:
3664 case elfcpp::R_POWERPC_DTPREL16:
3665 case elfcpp::R_POWERPC_SECTOFF_LO:
3666 case elfcpp::R_POWERPC_TPREL16_LO:
3667 case elfcpp::R_POWERPC_DTPREL16_LO:
3668 case elfcpp::R_POWERPC_SECTOFF_HI:
3669 case elfcpp::R_POWERPC_TPREL16_HI:
3670 case elfcpp::R_POWERPC_DTPREL16_HI:
3671 case elfcpp::R_POWERPC_SECTOFF_HA:
3672 case elfcpp::R_POWERPC_TPREL16_HA:
3673 case elfcpp::R_POWERPC_DTPREL16_HA:
3674 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3675 case elfcpp::R_PPC64_TPREL16_HIGHER:
3676 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3677 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3678 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3679 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3680 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3681 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3682 case elfcpp::R_PPC64_TPREL16_DS:
3683 case elfcpp::R_PPC64_TPREL16_LO_DS:
3684 case elfcpp::R_PPC64_DTPREL16_DS:
3685 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3686 case elfcpp::R_PPC64_SECTOFF_DS:
3687 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3688 case elfcpp::R_PPC64_TLSGD:
3689 case elfcpp::R_PPC64_TLSLD:
3690 break;
3691
3692 case elfcpp::R_POWERPC_GOT16:
3693 case elfcpp::R_POWERPC_GOT16_LO:
3694 case elfcpp::R_POWERPC_GOT16_HI:
3695 case elfcpp::R_POWERPC_GOT16_HA:
3696 case elfcpp::R_PPC64_GOT16_DS:
3697 case elfcpp::R_PPC64_GOT16_LO_DS:
3698 {
3699 // The symbol requires a GOT entry.
3700 Output_data_got_powerpc<size, big_endian>* got;
3701
3702 got = target->got_section(symtab, layout);
3703 if (gsym->final_value_is_known())
3704 {
3705 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3706 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3707 else
3708 got->add_global(gsym, GOT_TYPE_STANDARD);
3709 }
3710 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3711 {
3712 // If we are generating a shared object or a pie, this
3713 // symbol's GOT entry will be set by a dynamic relocation.
3714 unsigned int off = got->add_constant(0);
3715 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3716
3717 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3718 if (gsym->can_use_relative_reloc(false)
3719 && !(size == 32
3720 && gsym->visibility() == elfcpp::STV_PROTECTED
3721 && parameters->options().shared()))
3722 {
3723 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3724 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3725 {
3726 rela_dyn = target->iplt_section()->rel_plt();
3727 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3728 }
3729 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3730 }
3731 else
3732 {
3733 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3734 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3735 }
3736 }
3737 }
3738 break;
3739
3740 case elfcpp::R_PPC64_TOC16:
3741 case elfcpp::R_PPC64_TOC16_LO:
3742 case elfcpp::R_PPC64_TOC16_HI:
3743 case elfcpp::R_PPC64_TOC16_HA:
3744 case elfcpp::R_PPC64_TOC16_DS:
3745 case elfcpp::R_PPC64_TOC16_LO_DS:
3746 // We need a GOT section.
3747 target->got_section(symtab, layout);
3748 break;
3749
3750 case elfcpp::R_POWERPC_GOT_TLSGD16:
3751 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3752 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3753 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3754 {
3755 const bool final = gsym->final_value_is_known();
3756 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3757 if (tls_type == tls::TLSOPT_NONE)
3758 {
3759 Output_data_got_powerpc<size, big_endian>* got
3760 = target->got_section(symtab, layout);
3761 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3762 target->rela_dyn_section(layout),
3763 elfcpp::R_POWERPC_DTPMOD,
3764 elfcpp::R_POWERPC_DTPREL);
3765 }
3766 else if (tls_type == tls::TLSOPT_TO_IE)
3767 {
3768 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3769 {
3770 Output_data_got_powerpc<size, big_endian>* got
3771 = target->got_section(symtab, layout);
3772 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3773 if (gsym->is_undefined()
3774 || gsym->is_from_dynobj())
3775 {
3776 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3777 elfcpp::R_POWERPC_TPREL);
3778 }
3779 else
3780 {
3781 unsigned int off = got->add_constant(0);
3782 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3783 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3784 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3785 got, off, 0);
3786 }
3787 }
3788 }
3789 else if (tls_type == tls::TLSOPT_TO_LE)
3790 {
3791 // no GOT relocs needed for Local Exec.
3792 }
3793 else
3794 gold_unreachable();
3795 }
3796 break;
3797
3798 case elfcpp::R_POWERPC_GOT_TLSLD16:
3799 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3800 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3801 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3802 {
3803 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3804 if (tls_type == tls::TLSOPT_NONE)
3805 target->tlsld_got_offset(symtab, layout, object);
3806 else if (tls_type == tls::TLSOPT_TO_LE)
3807 {
3808 // no GOT relocs needed for Local Exec.
3809 if (parameters->options().emit_relocs())
3810 {
3811 Output_section* os = layout->tls_segment()->first_section();
3812 gold_assert(os != NULL);
3813 os->set_needs_symtab_index();
3814 }
3815 }
3816 else
3817 gold_unreachable();
3818 }
3819 break;
3820
3821 case elfcpp::R_POWERPC_GOT_DTPREL16:
3822 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3823 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3824 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3825 {
3826 Output_data_got_powerpc<size, big_endian>* got
3827 = target->got_section(symtab, layout);
3828 if (!gsym->final_value_is_known()
3829 && (gsym->is_from_dynobj()
3830 || gsym->is_undefined()
3831 || gsym->is_preemptible()))
3832 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3833 target->rela_dyn_section(layout),
3834 elfcpp::R_POWERPC_DTPREL);
3835 else
3836 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3837 }
3838 break;
3839
3840 case elfcpp::R_POWERPC_GOT_TPREL16:
3841 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3842 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3843 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3844 {
3845 const bool final = gsym->final_value_is_known();
3846 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3847 if (tls_type == tls::TLSOPT_NONE)
3848 {
3849 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3850 {
3851 Output_data_got_powerpc<size, big_endian>* got
3852 = target->got_section(symtab, layout);
3853 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3854 if (gsym->is_undefined()
3855 || gsym->is_from_dynobj())
3856 {
3857 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3858 elfcpp::R_POWERPC_TPREL);
3859 }
3860 else
3861 {
3862 unsigned int off = got->add_constant(0);
3863 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3864 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3865 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3866 got, off, 0);
3867 }
3868 }
3869 }
3870 else if (tls_type == tls::TLSOPT_TO_LE)
3871 {
3872 // no GOT relocs needed for Local Exec.
3873 }
3874 else
3875 gold_unreachable();
3876 }
3877 break;
3878
3879 default:
3880 unsupported_reloc_global(object, r_type, gsym);
3881 break;
3882 }
3883 }
3884
3885 // Process relocations for gc.
3886
3887 template<int size, bool big_endian>
3888 void
3889 Target_powerpc<size, big_endian>::gc_process_relocs(
3890 Symbol_table* symtab,
3891 Layout* layout,
3892 Sized_relobj_file<size, big_endian>* object,
3893 unsigned int data_shndx,
3894 unsigned int,
3895 const unsigned char* prelocs,
3896 size_t reloc_count,
3897 Output_section* output_section,
3898 bool needs_special_offset_handling,
3899 size_t local_symbol_count,
3900 const unsigned char* plocal_symbols)
3901 {
3902 typedef Target_powerpc<size, big_endian> Powerpc;
3903 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3904 Powerpc_relobj<size, big_endian>* ppc_object
3905 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3906 if (size == 64)
3907 ppc_object->set_opd_valid();
3908 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3909 {
3910 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3911 for (p = ppc_object->access_from_map()->begin();
3912 p != ppc_object->access_from_map()->end();
3913 ++p)
3914 {
3915 Address dst_off = p->first;
3916 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3917 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3918 for (s = p->second.begin(); s != p->second.end(); ++s)
3919 {
3920 Object* src_obj = s->first;
3921 unsigned int src_indx = s->second;
3922 symtab->gc()->add_reference(src_obj, src_indx,
3923 ppc_object, dst_indx);
3924 }
3925 p->second.clear();
3926 }
3927 ppc_object->access_from_map()->clear();
3928 ppc_object->process_gc_mark(symtab);
3929 // Don't look at .opd relocs as .opd will reference everything.
3930 return;
3931 }
3932
3933 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3934 typename Target_powerpc::Relocatable_size_for_reloc>(
3935 symtab,
3936 layout,
3937 this,
3938 object,
3939 data_shndx,
3940 prelocs,
3941 reloc_count,
3942 output_section,
3943 needs_special_offset_handling,
3944 local_symbol_count,
3945 plocal_symbols);
3946 }
3947
3948 // Handle target specific gc actions when adding a gc reference from
3949 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3950 // and DST_OFF. For powerpc64, this adds a referenc to the code
3951 // section of a function descriptor.
3952
3953 template<int size, bool big_endian>
3954 void
3955 Target_powerpc<size, big_endian>::do_gc_add_reference(
3956 Symbol_table* symtab,
3957 Object* src_obj,
3958 unsigned int src_shndx,
3959 Object* dst_obj,
3960 unsigned int dst_shndx,
3961 Address dst_off) const
3962 {
3963 Powerpc_relobj<size, big_endian>* ppc_object
3964 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3965 if (size == 64
3966 && !ppc_object->is_dynamic()
3967 && dst_shndx == ppc_object->opd_shndx())
3968 {
3969 if (ppc_object->opd_valid())
3970 {
3971 dst_shndx = ppc_object->get_opd_ent(dst_off);
3972 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3973 }
3974 else
3975 {
3976 // If we haven't run scan_opd_relocs, we must delay
3977 // processing this function descriptor reference.
3978 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3979 }
3980 }
3981 }
3982
3983 // Add any special sections for this symbol to the gc work list.
3984 // For powerpc64, this adds the code section of a function
3985 // descriptor.
3986
3987 template<int size, bool big_endian>
3988 void
3989 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3990 Symbol_table* symtab,
3991 Symbol* sym) const
3992 {
3993 if (size == 64)
3994 {
3995 Powerpc_relobj<size, big_endian>* ppc_object
3996 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3997 bool is_ordinary;
3998 unsigned int shndx = sym->shndx(&is_ordinary);
3999 if (is_ordinary && shndx == ppc_object->opd_shndx())
4000 {
4001 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
4002 Address dst_off = gsym->value();
4003 if (ppc_object->opd_valid())
4004 {
4005 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
4006 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
4007 }
4008 else
4009 ppc_object->add_gc_mark(dst_off);
4010 }
4011 }
4012 }
4013
4014 // Scan relocations for a section.
4015
4016 template<int size, bool big_endian>
4017 void
4018 Target_powerpc<size, big_endian>::scan_relocs(
4019 Symbol_table* symtab,
4020 Layout* layout,
4021 Sized_relobj_file<size, big_endian>* object,
4022 unsigned int data_shndx,
4023 unsigned int sh_type,
4024 const unsigned char* prelocs,
4025 size_t reloc_count,
4026 Output_section* output_section,
4027 bool needs_special_offset_handling,
4028 size_t local_symbol_count,
4029 const unsigned char* plocal_symbols)
4030 {
4031 typedef Target_powerpc<size, big_endian> Powerpc;
4032 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
4033
4034 if (sh_type == elfcpp::SHT_REL)
4035 {
4036 gold_error(_("%s: unsupported REL reloc section"),
4037 object->name().c_str());
4038 return;
4039 }
4040
4041 if (size == 32)
4042 {
4043 // Define a weak hidden _GLOBAL_OFFSET_TABLE_ to ensure it isn't
4044 // seen as undefined when scanning relocs (and thus requires
4045 // non-relative dynamic relocs). The proper value will be
4046 // updated later.
4047 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
4048 if (gotsym != NULL && gotsym->is_undefined())
4049 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4050 Symbol_table::PREDEFINED,
4051 this->got_section(symtab, layout), 0, 0,
4052 elfcpp::STT_OBJECT,
4053 elfcpp::STB_WEAK,
4054 elfcpp::STV_HIDDEN, 0,
4055 false, false);
4056
4057 static Output_data_space* sdata;
4058
4059 // Define _SDA_BASE_ at the start of the .sdata section.
4060 if (sdata == NULL)
4061 {
4062 // layout->find_output_section(".sdata") == NULL
4063 sdata = new Output_data_space(4, "** sdata");
4064 Output_section* os
4065 = layout->add_output_section_data(".sdata", 0,
4066 elfcpp::SHF_ALLOC
4067 | elfcpp::SHF_WRITE,
4068 sdata, ORDER_SMALL_DATA, false);
4069 symtab->define_in_output_data("_SDA_BASE_", NULL,
4070 Symbol_table::PREDEFINED,
4071 os, 32768, 0, elfcpp::STT_OBJECT,
4072 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
4073 0, false, false);
4074 }
4075 }
4076
4077 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
4078 symtab,
4079 layout,
4080 this,
4081 object,
4082 data_shndx,
4083 prelocs,
4084 reloc_count,
4085 output_section,
4086 needs_special_offset_handling,
4087 local_symbol_count,
4088 plocal_symbols);
4089 }
4090
4091 // Functor class for processing the global symbol table.
4092 // Removes symbols defined on discarded opd entries.
4093
4094 template<bool big_endian>
4095 class Global_symbol_visitor_opd
4096 {
4097 public:
4098 Global_symbol_visitor_opd()
4099 { }
4100
4101 void
4102 operator()(Sized_symbol<64>* sym)
4103 {
4104 if (sym->has_symtab_index()
4105 || sym->source() != Symbol::FROM_OBJECT
4106 || !sym->in_real_elf())
4107 return;
4108
4109 Powerpc_relobj<64, big_endian>* symobj
4110 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
4111 if (symobj->is_dynamic()
4112 || symobj->opd_shndx() == 0)
4113 return;
4114
4115 bool is_ordinary;
4116 unsigned int shndx = sym->shndx(&is_ordinary);
4117 if (shndx == symobj->opd_shndx()
4118 && symobj->get_opd_discard(sym->value()))
4119 sym->set_symtab_index(-1U);
4120 }
4121 };
4122
4123 template<int size, bool big_endian>
4124 void
4125 Target_powerpc<size, big_endian>::define_save_restore_funcs(
4126 Layout* layout,
4127 Symbol_table* symtab)
4128 {
4129 if (size == 64)
4130 {
4131 Output_data_save_res<64, big_endian>* savres
4132 = new Output_data_save_res<64, big_endian>(symtab);
4133 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4134 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4135 savres, ORDER_TEXT, false);
4136 }
4137 }
4138
4139 // Finalize the sections.
4140
4141 template<int size, bool big_endian>
4142 void
4143 Target_powerpc<size, big_endian>::do_finalize_sections(
4144 Layout* layout,
4145 const Input_objects*,
4146 Symbol_table* symtab)
4147 {
4148 if (parameters->doing_static_link())
4149 {
4150 // At least some versions of glibc elf-init.o have a strong
4151 // reference to __rela_iplt marker syms. A weak ref would be
4152 // better..
4153 if (this->iplt_ != NULL)
4154 {
4155 Reloc_section* rel = this->iplt_->rel_plt();
4156 symtab->define_in_output_data("__rela_iplt_start", NULL,
4157 Symbol_table::PREDEFINED, rel, 0, 0,
4158 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4159 elfcpp::STV_HIDDEN, 0, false, true);
4160 symtab->define_in_output_data("__rela_iplt_end", NULL,
4161 Symbol_table::PREDEFINED, rel, 0, 0,
4162 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4163 elfcpp::STV_HIDDEN, 0, true, true);
4164 }
4165 else
4166 {
4167 symtab->define_as_constant("__rela_iplt_start", NULL,
4168 Symbol_table::PREDEFINED, 0, 0,
4169 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4170 elfcpp::STV_HIDDEN, 0, true, false);
4171 symtab->define_as_constant("__rela_iplt_end", NULL,
4172 Symbol_table::PREDEFINED, 0, 0,
4173 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4174 elfcpp::STV_HIDDEN, 0, true, false);
4175 }
4176 }
4177
4178 if (size == 64)
4179 {
4180 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
4181 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
4182 this->define_save_restore_funcs(layout, symtab);
4183 }
4184
4185 // Fill in some more dynamic tags.
4186 Output_data_dynamic* odyn = layout->dynamic_data();
4187 if (odyn != NULL)
4188 {
4189 const Reloc_section* rel_plt = (this->plt_ == NULL
4190 ? NULL
4191 : this->plt_->rel_plt());
4192 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
4193 this->rela_dyn_, true, size == 32);
4194
4195 if (size == 32)
4196 {
4197 if (this->got_ != NULL)
4198 {
4199 this->got_->finalize_data_size();
4200 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
4201 this->got_, this->got_->g_o_t());
4202 }
4203 }
4204 else
4205 {
4206 if (this->glink_ != NULL)
4207 {
4208 this->glink_->finalize_data_size();
4209 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
4210 this->glink_,
4211 (this->glink_->pltresolve()
4212 + this->glink_->pltresolve_size
4213 - 32));
4214 }
4215 }
4216 }
4217
4218 // Emit any relocs we saved in an attempt to avoid generating COPY
4219 // relocs.
4220 if (this->copy_relocs_.any_saved_relocs())
4221 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4222 }
4223
4224 // Return the value to use for a branch relocation.
4225
4226 template<int size, bool big_endian>
4227 typename elfcpp::Elf_types<size>::Elf_Addr
4228 Target_powerpc<size, big_endian>::symval_for_branch(
4229 Address value,
4230 const Sized_symbol<size>* gsym,
4231 Powerpc_relobj<size, big_endian>* object,
4232 unsigned int *dest_shndx)
4233 {
4234 *dest_shndx = 0;
4235 if (size == 32)
4236 return value;
4237
4238 // If the symbol is defined in an opd section, ie. is a function
4239 // descriptor, use the function descriptor code entry address
4240 Powerpc_relobj<size, big_endian>* symobj = object;
4241 if (gsym != NULL
4242 && gsym->source() != Symbol::FROM_OBJECT)
4243 return value;
4244 if (gsym != NULL)
4245 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
4246 unsigned int shndx = symobj->opd_shndx();
4247 if (shndx == 0)
4248 return value;
4249 Address opd_addr = symobj->get_output_section_offset(shndx);
4250 gold_assert(opd_addr != invalid_address);
4251 opd_addr += symobj->output_section(shndx)->address();
4252 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
4253 {
4254 Address sec_off;
4255 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
4256 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
4257 gold_assert(sec_addr != invalid_address);
4258 sec_addr += symobj->output_section(*dest_shndx)->address();
4259 value = sec_addr + sec_off;
4260 }
4261 return value;
4262 }
4263
4264 // Perform a relocation.
4265
4266 template<int size, bool big_endian>
4267 inline bool
4268 Target_powerpc<size, big_endian>::Relocate::relocate(
4269 const Relocate_info<size, big_endian>* relinfo,
4270 Target_powerpc* target,
4271 Output_section* os,
4272 size_t relnum,
4273 const elfcpp::Rela<size, big_endian>& rela,
4274 unsigned int r_type,
4275 const Sized_symbol<size>* gsym,
4276 const Symbol_value<size>* psymval,
4277 unsigned char* view,
4278 Address address,
4279 section_size_type view_size)
4280 {
4281
4282 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
4283 || r_type == elfcpp::R_PPC_PLTREL24)
4284 && gsym != NULL
4285 && strcmp(gsym->name(), "__tls_get_addr") == 0);
4286 enum skip_tls last_tls = this->call_tls_get_addr_;
4287 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
4288 if (is_tls_call)
4289 {
4290 if (last_tls == CALL_NOT_EXPECTED)
4291 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4292 _("__tls_get_addr call lacks marker reloc"));
4293 else if (last_tls == CALL_SKIP)
4294 return false;
4295 }
4296 else if (last_tls != CALL_NOT_EXPECTED)
4297 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4298 _("missing expected __tls_get_addr call"));
4299
4300 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
4301 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
4302 Powerpc_relobj<size, big_endian>* const object
4303 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4304 Address value = 0;
4305 bool has_plt_value = false;
4306 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4307 if (gsym != NULL
4308 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
4309 : object->local_has_plt_offset(r_sym))
4310 {
4311 const Output_data_glink<size, big_endian>* glink
4312 = target->glink_section();
4313 unsigned int glink_index;
4314 if (gsym != NULL)
4315 glink_index = glink->find_entry(object, gsym, rela);
4316 else
4317 glink_index = glink->find_entry(object, r_sym, rela);
4318 value = glink->address() + glink_index * glink->glink_entry_size();
4319 has_plt_value = true;
4320 }
4321
4322 if (r_type == elfcpp::R_POWERPC_GOT16
4323 || r_type == elfcpp::R_POWERPC_GOT16_LO
4324 || r_type == elfcpp::R_POWERPC_GOT16_HI
4325 || r_type == elfcpp::R_POWERPC_GOT16_HA
4326 || r_type == elfcpp::R_PPC64_GOT16_DS
4327 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
4328 {
4329 if (gsym != NULL)
4330 {
4331 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4332 value = gsym->got_offset(GOT_TYPE_STANDARD);
4333 }
4334 else
4335 {
4336 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4337 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4338 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
4339 }
4340 value -= target->got_section()->got_base_offset(object);
4341 }
4342 else if (r_type == elfcpp::R_PPC64_TOC)
4343 {
4344 value = (target->got_section()->output_section()->address()
4345 + object->toc_base_offset());
4346 }
4347 else if (gsym != NULL
4348 && (r_type == elfcpp::R_POWERPC_REL24
4349 || r_type == elfcpp::R_PPC_PLTREL24)
4350 && has_plt_value)
4351 {
4352 if (size == 64)
4353 {
4354 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4355 Valtype* wv = reinterpret_cast<Valtype*>(view);
4356 bool can_plt_call = false;
4357 if (rela.get_r_offset() + 8 <= view_size)
4358 {
4359 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
4360 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
4361 if ((insn & 1) != 0
4362 && (insn2 == nop
4363 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
4364 {
4365 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
4366 can_plt_call = true;
4367 }
4368 }
4369 if (!can_plt_call)
4370 {
4371 // If we don't have a branch and link followed by a nop,
4372 // we can't go via the plt because there is no place to
4373 // put a toc restoring instruction.
4374 // Unless we know we won't be returning.
4375 if (strcmp(gsym->name(), "__libc_start_main") == 0)
4376 can_plt_call = true;
4377 }
4378 if (!can_plt_call)
4379 {
4380 // This is not an error in one special case: A self
4381 // call. It isn't possible to cheaply verify we have
4382 // such a call so just check for a call to the same
4383 // section.
4384 bool ok = false;
4385 Address code = value;
4386 if (gsym->source() == Symbol::FROM_OBJECT
4387 && gsym->object() == object)
4388 {
4389 Address addend = rela.get_r_addend();
4390 unsigned int dest_shndx;
4391 Address opdent = psymval->value(object, addend);
4392 code = target->symval_for_branch(opdent, gsym, object,
4393 &dest_shndx);
4394 bool is_ordinary;
4395 if (dest_shndx == 0)
4396 dest_shndx = gsym->shndx(&is_ordinary);
4397 ok = dest_shndx == relinfo->data_shndx;
4398 }
4399 if (!ok)
4400 {
4401 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4402 _("call lacks nop, can't restore toc; "
4403 "recompile with -fPIC"));
4404 value = code;
4405 }
4406 }
4407 }
4408 }
4409 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4410 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4411 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4412 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4413 {
4414 // First instruction of a global dynamic sequence, arg setup insn.
4415 const bool final = gsym == NULL || gsym->final_value_is_known();
4416 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4417 enum Got_type got_type = GOT_TYPE_STANDARD;
4418 if (tls_type == tls::TLSOPT_NONE)
4419 got_type = GOT_TYPE_TLSGD;
4420 else if (tls_type == tls::TLSOPT_TO_IE)
4421 got_type = GOT_TYPE_TPREL;
4422 if (got_type != GOT_TYPE_STANDARD)
4423 {
4424 if (gsym != NULL)
4425 {
4426 gold_assert(gsym->has_got_offset(got_type));
4427 value = gsym->got_offset(got_type);
4428 }
4429 else
4430 {
4431 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4432 gold_assert(object->local_has_got_offset(r_sym, got_type));
4433 value = object->local_got_offset(r_sym, got_type);
4434 }
4435 value -= target->got_section()->got_base_offset(object);
4436 }
4437 if (tls_type == tls::TLSOPT_TO_IE)
4438 {
4439 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4440 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4441 {
4442 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4443 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4444 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
4445 if (size == 32)
4446 insn |= 32 << 26; // lwz
4447 else
4448 insn |= 58 << 26; // ld
4449 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4450 }
4451 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4452 - elfcpp::R_POWERPC_GOT_TLSGD16);
4453 }
4454 else if (tls_type == tls::TLSOPT_TO_LE)
4455 {
4456 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4457 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4458 {
4459 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4460 Insn insn = addis_3_13;
4461 if (size == 32)
4462 insn = addis_3_2;
4463 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4464 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4465 value = psymval->value(object, rela.get_r_addend());
4466 }
4467 else
4468 {
4469 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4470 Insn insn = nop;
4471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4472 r_type = elfcpp::R_POWERPC_NONE;
4473 }
4474 }
4475 }
4476 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4477 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4478 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4479 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4480 {
4481 // First instruction of a local dynamic sequence, arg setup insn.
4482 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4483 if (tls_type == tls::TLSOPT_NONE)
4484 {
4485 value = target->tlsld_got_offset();
4486 value -= target->got_section()->got_base_offset(object);
4487 }
4488 else
4489 {
4490 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4491 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4492 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4493 {
4494 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4495 Insn insn = addis_3_13;
4496 if (size == 32)
4497 insn = addis_3_2;
4498 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4499 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4500 value = dtp_offset;
4501 }
4502 else
4503 {
4504 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4505 Insn insn = nop;
4506 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4507 r_type = elfcpp::R_POWERPC_NONE;
4508 }
4509 }
4510 }
4511 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4512 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4513 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4514 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4515 {
4516 // Accesses relative to a local dynamic sequence address,
4517 // no optimisation here.
4518 if (gsym != NULL)
4519 {
4520 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4521 value = gsym->got_offset(GOT_TYPE_DTPREL);
4522 }
4523 else
4524 {
4525 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4526 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4527 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4528 }
4529 value -= target->got_section()->got_base_offset(object);
4530 }
4531 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4532 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4533 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4534 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4535 {
4536 // First instruction of initial exec sequence.
4537 const bool final = gsym == NULL || gsym->final_value_is_known();
4538 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4539 if (tls_type == tls::TLSOPT_NONE)
4540 {
4541 if (gsym != NULL)
4542 {
4543 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4544 value = gsym->got_offset(GOT_TYPE_TPREL);
4545 }
4546 else
4547 {
4548 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4549 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4550 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4551 }
4552 value -= target->got_section()->got_base_offset(object);
4553 }
4554 else
4555 {
4556 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4557 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4558 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4559 {
4560 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4561 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4562 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4563 if (size == 32)
4564 insn |= addis_0_2;
4565 else
4566 insn |= addis_0_13;
4567 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4568 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4569 value = psymval->value(object, rela.get_r_addend());
4570 }
4571 else
4572 {
4573 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4574 Insn insn = nop;
4575 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4576 r_type = elfcpp::R_POWERPC_NONE;
4577 }
4578 }
4579 }
4580 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4581 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4582 {
4583 // Second instruction of a global dynamic sequence,
4584 // the __tls_get_addr call
4585 this->call_tls_get_addr_ = CALL_EXPECTED;
4586 const bool final = gsym == NULL || gsym->final_value_is_known();
4587 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4588 if (tls_type != tls::TLSOPT_NONE)
4589 {
4590 if (tls_type == tls::TLSOPT_TO_IE)
4591 {
4592 Insn* iview = reinterpret_cast<Insn*>(view);
4593 Insn insn = add_3_3_13;
4594 if (size == 32)
4595 insn = add_3_3_2;
4596 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4597 r_type = elfcpp::R_POWERPC_NONE;
4598 }
4599 else
4600 {
4601 Insn* iview = reinterpret_cast<Insn*>(view);
4602 Insn insn = addi_3_3;
4603 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4604 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4605 view += 2 * big_endian;
4606 value = psymval->value(object, rela.get_r_addend());
4607 }
4608 this->call_tls_get_addr_ = CALL_SKIP;
4609 }
4610 }
4611 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4612 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4613 {
4614 // Second instruction of a local dynamic sequence,
4615 // the __tls_get_addr call
4616 this->call_tls_get_addr_ = CALL_EXPECTED;
4617 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4618 if (tls_type == tls::TLSOPT_TO_LE)
4619 {
4620 Insn* iview = reinterpret_cast<Insn*>(view);
4621 Insn insn = addi_3_3;
4622 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4623 this->call_tls_get_addr_ = CALL_SKIP;
4624 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4625 view += 2 * big_endian;
4626 value = dtp_offset;
4627 }
4628 }
4629 else if (r_type == elfcpp::R_POWERPC_TLS)
4630 {
4631 // Second instruction of an initial exec sequence
4632 const bool final = gsym == NULL || gsym->final_value_is_known();
4633 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4634 if (tls_type == tls::TLSOPT_TO_LE)
4635 {
4636 Insn* iview = reinterpret_cast<Insn*>(view);
4637 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4638 unsigned int reg = size == 32 ? 2 : 13;
4639 insn = at_tls_transform(insn, reg);
4640 gold_assert(insn != 0);
4641 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4642 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4643 view += 2 * big_endian;
4644 value = psymval->value(object, rela.get_r_addend());
4645 }
4646 }
4647 else if (!has_plt_value)
4648 {
4649 Address addend = 0;
4650 unsigned int dest_shndx;
4651 if (r_type != elfcpp::R_PPC_PLTREL24)
4652 addend = rela.get_r_addend();
4653 value = psymval->value(object, addend);
4654 if (size == 64 && is_branch_reloc(r_type))
4655 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4656 }
4657
4658 switch (r_type)
4659 {
4660 case elfcpp::R_PPC64_REL64:
4661 case elfcpp::R_POWERPC_REL32:
4662 case elfcpp::R_POWERPC_REL24:
4663 case elfcpp::R_PPC_PLTREL24:
4664 case elfcpp::R_PPC_LOCAL24PC:
4665 case elfcpp::R_POWERPC_REL16:
4666 case elfcpp::R_POWERPC_REL16_LO:
4667 case elfcpp::R_POWERPC_REL16_HI:
4668 case elfcpp::R_POWERPC_REL16_HA:
4669 case elfcpp::R_POWERPC_REL14:
4670 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4671 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4672 value -= address;
4673 break;
4674
4675 case elfcpp::R_PPC64_TOC16:
4676 case elfcpp::R_PPC64_TOC16_LO:
4677 case elfcpp::R_PPC64_TOC16_HI:
4678 case elfcpp::R_PPC64_TOC16_HA:
4679 case elfcpp::R_PPC64_TOC16_DS:
4680 case elfcpp::R_PPC64_TOC16_LO_DS:
4681 // Subtract the TOC base address.
4682 value -= (target->got_section()->output_section()->address()
4683 + object->toc_base_offset());
4684 break;
4685
4686 case elfcpp::R_POWERPC_SECTOFF:
4687 case elfcpp::R_POWERPC_SECTOFF_LO:
4688 case elfcpp::R_POWERPC_SECTOFF_HI:
4689 case elfcpp::R_POWERPC_SECTOFF_HA:
4690 case elfcpp::R_PPC64_SECTOFF_DS:
4691 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4692 if (os != NULL)
4693 value -= os->address();
4694 break;
4695
4696 case elfcpp::R_PPC64_TPREL16_DS:
4697 case elfcpp::R_PPC64_TPREL16_LO_DS:
4698 if (size != 64)
4699 // R_PPC_TLSGD and R_PPC_TLSLD
4700 break;
4701 case elfcpp::R_POWERPC_TPREL16:
4702 case elfcpp::R_POWERPC_TPREL16_LO:
4703 case elfcpp::R_POWERPC_TPREL16_HI:
4704 case elfcpp::R_POWERPC_TPREL16_HA:
4705 case elfcpp::R_POWERPC_TPREL:
4706 case elfcpp::R_PPC64_TPREL16_HIGHER:
4707 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4708 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4709 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4710 // tls symbol values are relative to tls_segment()->vaddr()
4711 value -= tp_offset;
4712 break;
4713
4714 case elfcpp::R_PPC64_DTPREL16_DS:
4715 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4716 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4717 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4718 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4719 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4720 if (size != 64)
4721 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4722 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4723 break;
4724 case elfcpp::R_POWERPC_DTPREL16:
4725 case elfcpp::R_POWERPC_DTPREL16_LO:
4726 case elfcpp::R_POWERPC_DTPREL16_HI:
4727 case elfcpp::R_POWERPC_DTPREL16_HA:
4728 case elfcpp::R_POWERPC_DTPREL:
4729 // tls symbol values are relative to tls_segment()->vaddr()
4730 value -= dtp_offset;
4731 break;
4732
4733 default:
4734 break;
4735 }
4736
4737 Insn branch_bit = 0;
4738 switch (r_type)
4739 {
4740 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4741 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4742 branch_bit = 1 << 21;
4743 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4744 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4745 {
4746 Insn* iview = reinterpret_cast<Insn*>(view);
4747 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4748 insn &= ~(1 << 21);
4749 insn |= branch_bit;
4750 if (this->is_isa_v2)
4751 {
4752 // Set 'a' bit. This is 0b00010 in BO field for branch
4753 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4754 // for branch on CTR insns (BO == 1a00t or 1a01t).
4755 if ((insn & (0x14 << 21)) == (0x04 << 21))
4756 insn |= 0x02 << 21;
4757 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4758 insn |= 0x08 << 21;
4759 else
4760 break;
4761 }
4762 else
4763 {
4764 // Invert 'y' bit if not the default.
4765 if (static_cast<Signed_address>(value) < 0)
4766 insn ^= 1 << 21;
4767 }
4768 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4769 }
4770 break;
4771
4772 default:
4773 break;
4774 }
4775
4776 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4777 switch (r_type)
4778 {
4779 case elfcpp::R_POWERPC_ADDR32:
4780 case elfcpp::R_POWERPC_UADDR32:
4781 if (size == 64)
4782 overflow = Reloc::CHECK_BITFIELD;
4783 break;
4784
4785 case elfcpp::R_POWERPC_REL32:
4786 if (size == 64)
4787 overflow = Reloc::CHECK_SIGNED;
4788 break;
4789
4790 case elfcpp::R_POWERPC_ADDR24:
4791 case elfcpp::R_POWERPC_ADDR16:
4792 case elfcpp::R_POWERPC_UADDR16:
4793 case elfcpp::R_PPC64_ADDR16_DS:
4794 case elfcpp::R_POWERPC_ADDR14:
4795 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4796 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4797 overflow = Reloc::CHECK_BITFIELD;
4798 break;
4799
4800 case elfcpp::R_POWERPC_REL24:
4801 case elfcpp::R_PPC_PLTREL24:
4802 case elfcpp::R_PPC_LOCAL24PC:
4803 case elfcpp::R_POWERPC_REL16:
4804 case elfcpp::R_PPC64_TOC16:
4805 case elfcpp::R_POWERPC_GOT16:
4806 case elfcpp::R_POWERPC_SECTOFF:
4807 case elfcpp::R_POWERPC_TPREL16:
4808 case elfcpp::R_POWERPC_DTPREL16:
4809 case elfcpp::R_PPC64_TPREL16_DS:
4810 case elfcpp::R_PPC64_DTPREL16_DS:
4811 case elfcpp::R_PPC64_TOC16_DS:
4812 case elfcpp::R_PPC64_GOT16_DS:
4813 case elfcpp::R_PPC64_SECTOFF_DS:
4814 case elfcpp::R_POWERPC_REL14:
4815 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4816 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4817 case elfcpp::R_POWERPC_GOT_TLSGD16:
4818 case elfcpp::R_POWERPC_GOT_TLSLD16:
4819 case elfcpp::R_POWERPC_GOT_TPREL16:
4820 case elfcpp::R_POWERPC_GOT_DTPREL16:
4821 overflow = Reloc::CHECK_SIGNED;
4822 break;
4823 }
4824
4825 typename Powerpc_relocate_functions<size, big_endian>::Status status
4826 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4827 switch (r_type)
4828 {
4829 case elfcpp::R_POWERPC_NONE:
4830 case elfcpp::R_POWERPC_TLS:
4831 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4832 case elfcpp::R_POWERPC_GNU_VTENTRY:
4833 case elfcpp::R_PPC_EMB_MRKREF:
4834 break;
4835
4836 case elfcpp::R_PPC64_ADDR64:
4837 case elfcpp::R_PPC64_REL64:
4838 case elfcpp::R_PPC64_TOC:
4839 Reloc::addr64(view, value);
4840 break;
4841
4842 case elfcpp::R_POWERPC_TPREL:
4843 case elfcpp::R_POWERPC_DTPREL:
4844 if (size == 64)
4845 Reloc::addr64(view, value);
4846 else
4847 status = Reloc::addr32(view, value, overflow);
4848 break;
4849
4850 case elfcpp::R_PPC64_UADDR64:
4851 Reloc::addr64_u(view, value);
4852 break;
4853
4854 case elfcpp::R_POWERPC_ADDR32:
4855 status = Reloc::addr32(view, value, overflow);
4856 break;
4857
4858 case elfcpp::R_POWERPC_REL32:
4859 case elfcpp::R_POWERPC_UADDR32:
4860 status = Reloc::addr32_u(view, value, overflow);
4861 break;
4862
4863 case elfcpp::R_POWERPC_ADDR24:
4864 case elfcpp::R_POWERPC_REL24:
4865 case elfcpp::R_PPC_PLTREL24:
4866 case elfcpp::R_PPC_LOCAL24PC:
4867 status = Reloc::addr24(view, value, overflow);
4868 break;
4869
4870 case elfcpp::R_POWERPC_GOT_DTPREL16:
4871 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4872 if (size == 64)
4873 {
4874 status = Reloc::addr16_ds(view, value, overflow);
4875 break;
4876 }
4877 case elfcpp::R_POWERPC_ADDR16:
4878 case elfcpp::R_POWERPC_REL16:
4879 case elfcpp::R_PPC64_TOC16:
4880 case elfcpp::R_POWERPC_GOT16:
4881 case elfcpp::R_POWERPC_SECTOFF:
4882 case elfcpp::R_POWERPC_TPREL16:
4883 case elfcpp::R_POWERPC_DTPREL16:
4884 case elfcpp::R_POWERPC_GOT_TLSGD16:
4885 case elfcpp::R_POWERPC_GOT_TLSLD16:
4886 case elfcpp::R_POWERPC_GOT_TPREL16:
4887 case elfcpp::R_POWERPC_ADDR16_LO:
4888 case elfcpp::R_POWERPC_REL16_LO:
4889 case elfcpp::R_PPC64_TOC16_LO:
4890 case elfcpp::R_POWERPC_GOT16_LO:
4891 case elfcpp::R_POWERPC_SECTOFF_LO:
4892 case elfcpp::R_POWERPC_TPREL16_LO:
4893 case elfcpp::R_POWERPC_DTPREL16_LO:
4894 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4895 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4896 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4897 status = Reloc::addr16(view, value, overflow);
4898 break;
4899
4900 case elfcpp::R_POWERPC_UADDR16:
4901 status = Reloc::addr16_u(view, value, overflow);
4902 break;
4903
4904 case elfcpp::R_POWERPC_ADDR16_HI:
4905 case elfcpp::R_POWERPC_REL16_HI:
4906 case elfcpp::R_PPC64_TOC16_HI:
4907 case elfcpp::R_POWERPC_GOT16_HI:
4908 case elfcpp::R_POWERPC_SECTOFF_HI:
4909 case elfcpp::R_POWERPC_TPREL16_HI:
4910 case elfcpp::R_POWERPC_DTPREL16_HI:
4911 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4912 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4913 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4914 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4915 Reloc::addr16_hi(view, value);
4916 break;
4917
4918 case elfcpp::R_POWERPC_ADDR16_HA:
4919 case elfcpp::R_POWERPC_REL16_HA:
4920 case elfcpp::R_PPC64_TOC16_HA:
4921 case elfcpp::R_POWERPC_GOT16_HA:
4922 case elfcpp::R_POWERPC_SECTOFF_HA:
4923 case elfcpp::R_POWERPC_TPREL16_HA:
4924 case elfcpp::R_POWERPC_DTPREL16_HA:
4925 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4926 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4927 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4928 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4929 Reloc::addr16_ha(view, value);
4930 break;
4931
4932 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4933 if (size == 32)
4934 // R_PPC_EMB_NADDR16_LO
4935 goto unsupp;
4936 case elfcpp::R_PPC64_ADDR16_HIGHER:
4937 case elfcpp::R_PPC64_TPREL16_HIGHER:
4938 Reloc::addr16_hi2(view, value);
4939 break;
4940
4941 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4942 if (size == 32)
4943 // R_PPC_EMB_NADDR16_HI
4944 goto unsupp;
4945 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4946 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4947 Reloc::addr16_ha2(view, value);
4948 break;
4949
4950 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4951 if (size == 32)
4952 // R_PPC_EMB_NADDR16_HA
4953 goto unsupp;
4954 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4955 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4956 Reloc::addr16_hi3(view, value);
4957 break;
4958
4959 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4960 if (size == 32)
4961 // R_PPC_EMB_SDAI16
4962 goto unsupp;
4963 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4964 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4965 Reloc::addr16_ha3(view, value);
4966 break;
4967
4968 case elfcpp::R_PPC64_DTPREL16_DS:
4969 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4970 if (size == 32)
4971 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4972 goto unsupp;
4973 case elfcpp::R_PPC64_TPREL16_DS:
4974 case elfcpp::R_PPC64_TPREL16_LO_DS:
4975 if (size == 32)
4976 // R_PPC_TLSGD, R_PPC_TLSLD
4977 break;
4978 case elfcpp::R_PPC64_ADDR16_DS:
4979 case elfcpp::R_PPC64_ADDR16_LO_DS:
4980 case elfcpp::R_PPC64_TOC16_DS:
4981 case elfcpp::R_PPC64_TOC16_LO_DS:
4982 case elfcpp::R_PPC64_GOT16_DS:
4983 case elfcpp::R_PPC64_GOT16_LO_DS:
4984 case elfcpp::R_PPC64_SECTOFF_DS:
4985 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4986 status = Reloc::addr16_ds(view, value, overflow);
4987 break;
4988
4989 case elfcpp::R_POWERPC_ADDR14:
4990 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4991 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4992 case elfcpp::R_POWERPC_REL14:
4993 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4994 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4995 status = Reloc::addr14(view, value, overflow);
4996 break;
4997
4998 case elfcpp::R_POWERPC_COPY:
4999 case elfcpp::R_POWERPC_GLOB_DAT:
5000 case elfcpp::R_POWERPC_JMP_SLOT:
5001 case elfcpp::R_POWERPC_RELATIVE:
5002 case elfcpp::R_POWERPC_DTPMOD:
5003 case elfcpp::R_PPC64_JMP_IREL:
5004 case elfcpp::R_POWERPC_IRELATIVE:
5005 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5006 _("unexpected reloc %u in object file"),
5007 r_type);
5008 break;
5009
5010 case elfcpp::R_PPC_EMB_SDA21:
5011 if (size == 32)
5012 goto unsupp;
5013 else
5014 {
5015 // R_PPC64_TOCSAVE. For the time being this can be ignored.
5016 }
5017 break;
5018
5019 case elfcpp::R_PPC_EMB_SDA2I16:
5020 case elfcpp::R_PPC_EMB_SDA2REL:
5021 if (size == 32)
5022 goto unsupp;
5023 // R_PPC64_TLSGD, R_PPC64_TLSLD
5024 break;
5025
5026 case elfcpp::R_POWERPC_PLT32:
5027 case elfcpp::R_POWERPC_PLTREL32:
5028 case elfcpp::R_POWERPC_PLT16_LO:
5029 case elfcpp::R_POWERPC_PLT16_HI:
5030 case elfcpp::R_POWERPC_PLT16_HA:
5031 case elfcpp::R_PPC_SDAREL16:
5032 case elfcpp::R_POWERPC_ADDR30:
5033 case elfcpp::R_PPC64_PLT64:
5034 case elfcpp::R_PPC64_PLTREL64:
5035 case elfcpp::R_PPC64_PLTGOT16:
5036 case elfcpp::R_PPC64_PLTGOT16_LO:
5037 case elfcpp::R_PPC64_PLTGOT16_HI:
5038 case elfcpp::R_PPC64_PLTGOT16_HA:
5039 case elfcpp::R_PPC64_PLT16_LO_DS:
5040 case elfcpp::R_PPC64_PLTGOT16_DS:
5041 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
5042 case elfcpp::R_PPC_EMB_RELSEC16:
5043 case elfcpp::R_PPC_EMB_RELST_LO:
5044 case elfcpp::R_PPC_EMB_RELST_HI:
5045 case elfcpp::R_PPC_EMB_RELST_HA:
5046 case elfcpp::R_PPC_EMB_BIT_FLD:
5047 case elfcpp::R_PPC_EMB_RELSDA:
5048 case elfcpp::R_PPC_TOC16:
5049 default:
5050 unsupp:
5051 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5052 _("unsupported reloc %u"),
5053 r_type);
5054 break;
5055 }
5056 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
5057 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5058 _("relocation overflow"));
5059
5060 return true;
5061 }
5062
5063 // Relocate section data.
5064
5065 template<int size, bool big_endian>
5066 void
5067 Target_powerpc<size, big_endian>::relocate_section(
5068 const Relocate_info<size, big_endian>* relinfo,
5069 unsigned int sh_type,
5070 const unsigned char* prelocs,
5071 size_t reloc_count,
5072 Output_section* output_section,
5073 bool needs_special_offset_handling,
5074 unsigned char* view,
5075 Address address,
5076 section_size_type view_size,
5077 const Reloc_symbol_changes* reloc_symbol_changes)
5078 {
5079 typedef Target_powerpc<size, big_endian> Powerpc;
5080 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
5081 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
5082 Powerpc_comdat_behavior;
5083
5084 gold_assert(sh_type == elfcpp::SHT_RELA);
5085
5086 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
5087 Powerpc_relocate, Powerpc_comdat_behavior>(
5088 relinfo,
5089 this,
5090 prelocs,
5091 reloc_count,
5092 output_section,
5093 needs_special_offset_handling,
5094 view,
5095 address,
5096 view_size,
5097 reloc_symbol_changes);
5098 }
5099
5100 class Powerpc_scan_relocatable_reloc
5101 {
5102 public:
5103 // Return the strategy to use for a local symbol which is not a
5104 // section symbol, given the relocation type.
5105 inline Relocatable_relocs::Reloc_strategy
5106 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
5107 {
5108 if (r_type == 0 && r_sym == 0)
5109 return Relocatable_relocs::RELOC_DISCARD;
5110 return Relocatable_relocs::RELOC_COPY;
5111 }
5112
5113 // Return the strategy to use for a local symbol which is a section
5114 // symbol, given the relocation type.
5115 inline Relocatable_relocs::Reloc_strategy
5116 local_section_strategy(unsigned int, Relobj*)
5117 {
5118 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
5119 }
5120
5121 // Return the strategy to use for a global symbol, given the
5122 // relocation type, the object, and the symbol index.
5123 inline Relocatable_relocs::Reloc_strategy
5124 global_strategy(unsigned int r_type, Relobj*, unsigned int)
5125 {
5126 if (r_type == elfcpp::R_PPC_PLTREL24)
5127 return Relocatable_relocs::RELOC_SPECIAL;
5128 return Relocatable_relocs::RELOC_COPY;
5129 }
5130 };
5131
5132 // Scan the relocs during a relocatable link.
5133
5134 template<int size, bool big_endian>
5135 void
5136 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
5137 Symbol_table* symtab,
5138 Layout* layout,
5139 Sized_relobj_file<size, big_endian>* object,
5140 unsigned int data_shndx,
5141 unsigned int sh_type,
5142 const unsigned char* prelocs,
5143 size_t reloc_count,
5144 Output_section* output_section,
5145 bool needs_special_offset_handling,
5146 size_t local_symbol_count,
5147 const unsigned char* plocal_symbols,
5148 Relocatable_relocs* rr)
5149 {
5150 gold_assert(sh_type == elfcpp::SHT_RELA);
5151
5152 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
5153 Powerpc_scan_relocatable_reloc>(
5154 symtab,
5155 layout,
5156 object,
5157 data_shndx,
5158 prelocs,
5159 reloc_count,
5160 output_section,
5161 needs_special_offset_handling,
5162 local_symbol_count,
5163 plocal_symbols,
5164 rr);
5165 }
5166
5167 // Emit relocations for a section.
5168 // This is a modified version of the function by the same name in
5169 // target-reloc.h. Using relocate_special_relocatable for
5170 // R_PPC_PLTREL24 would require duplication of the entire body of the
5171 // loop, so we may as well duplicate the whole thing.
5172
5173 template<int size, bool big_endian>
5174 void
5175 Target_powerpc<size, big_endian>::relocate_relocs(
5176 const Relocate_info<size, big_endian>* relinfo,
5177 unsigned int sh_type,
5178 const unsigned char* prelocs,
5179 size_t reloc_count,
5180 Output_section* output_section,
5181 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5182 const Relocatable_relocs* rr,
5183 unsigned char*,
5184 Address view_address,
5185 section_size_type,
5186 unsigned char* reloc_view,
5187 section_size_type reloc_view_size)
5188 {
5189 gold_assert(sh_type == elfcpp::SHT_RELA);
5190
5191 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5192 Reltype;
5193 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
5194 Reltype_write;
5195 const int reloc_size
5196 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5197
5198 Powerpc_relobj<size, big_endian>* const object
5199 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5200 const unsigned int local_count = object->local_symbol_count();
5201 unsigned int got2_shndx = object->got2_shndx();
5202 Address got2_addend = 0;
5203 if (got2_shndx != 0)
5204 {
5205 got2_addend = object->get_output_section_offset(got2_shndx);
5206 gold_assert(got2_addend != invalid_address);
5207 }
5208
5209 unsigned char* pwrite = reloc_view;
5210 bool zap_next = false;
5211 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5212 {
5213 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
5214 if (strategy == Relocatable_relocs::RELOC_DISCARD)
5215 continue;
5216
5217 Reltype reloc(prelocs);
5218 Reltype_write reloc_write(pwrite);
5219
5220 Address offset = reloc.get_r_offset();
5221 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
5222 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
5223 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5224 const unsigned int orig_r_sym = r_sym;
5225 typename elfcpp::Elf_types<size>::Elf_Swxword addend
5226 = reloc.get_r_addend();
5227 const Symbol* gsym = NULL;
5228
5229 if (zap_next)
5230 {
5231 // We could arrange to discard these and other relocs for
5232 // tls optimised sequences in the strategy methods, but for
5233 // now do as BFD ld does.
5234 r_type = elfcpp::R_POWERPC_NONE;
5235 zap_next = false;
5236 }
5237
5238 // Get the new symbol index.
5239 if (r_sym < local_count)
5240 {
5241 switch (strategy)
5242 {
5243 case Relocatable_relocs::RELOC_COPY:
5244 case Relocatable_relocs::RELOC_SPECIAL:
5245 if (r_sym != 0)
5246 {
5247 r_sym = object->symtab_index(r_sym);
5248 gold_assert(r_sym != -1U);
5249 }
5250 break;
5251
5252 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
5253 {
5254 // We are adjusting a section symbol. We need to find
5255 // the symbol table index of the section symbol for
5256 // the output section corresponding to input section
5257 // in which this symbol is defined.
5258 gold_assert(r_sym < local_count);
5259 bool is_ordinary;
5260 unsigned int shndx =
5261 object->local_symbol_input_shndx(r_sym, &is_ordinary);
5262 gold_assert(is_ordinary);
5263 Output_section* os = object->output_section(shndx);
5264 gold_assert(os != NULL);
5265 gold_assert(os->needs_symtab_index());
5266 r_sym = os->symtab_index();
5267 }
5268 break;
5269
5270 default:
5271 gold_unreachable();
5272 }
5273 }
5274 else
5275 {
5276 gsym = object->global_symbol(r_sym);
5277 gold_assert(gsym != NULL);
5278 if (gsym->is_forwarder())
5279 gsym = relinfo->symtab->resolve_forwards(gsym);
5280
5281 gold_assert(gsym->has_symtab_index());
5282 r_sym = gsym->symtab_index();
5283 }
5284
5285 // Get the new offset--the location in the output section where
5286 // this relocation should be applied.
5287 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5288 offset += offset_in_output_section;
5289 else
5290 {
5291 section_offset_type sot_offset =
5292 convert_types<section_offset_type, Address>(offset);
5293 section_offset_type new_sot_offset =
5294 output_section->output_offset(object, relinfo->data_shndx,
5295 sot_offset);
5296 gold_assert(new_sot_offset != -1);
5297 offset = new_sot_offset;
5298 }
5299
5300 // In an object file, r_offset is an offset within the section.
5301 // In an executable or dynamic object, generated by
5302 // --emit-relocs, r_offset is an absolute address.
5303 if (!parameters->options().relocatable())
5304 {
5305 offset += view_address;
5306 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5307 offset -= offset_in_output_section;
5308 }
5309
5310 // Handle the reloc addend based on the strategy.
5311 if (strategy == Relocatable_relocs::RELOC_COPY)
5312 ;
5313 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
5314 {
5315 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
5316 addend = psymval->value(object, addend);
5317 }
5318 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
5319 {
5320 if (addend >= 32768)
5321 addend += got2_addend;
5322 }
5323 else
5324 gold_unreachable();
5325
5326 if (!parameters->options().relocatable())
5327 {
5328 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5329 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5330 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5331 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5332 {
5333 // First instruction of a global dynamic sequence,
5334 // arg setup insn.
5335 const bool final = gsym == NULL || gsym->final_value_is_known();
5336 switch (this->optimize_tls_gd(final))
5337 {
5338 case tls::TLSOPT_TO_IE:
5339 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5340 - elfcpp::R_POWERPC_GOT_TLSGD16);
5341 break;
5342 case tls::TLSOPT_TO_LE:
5343 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5344 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5345 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5346 else
5347 {
5348 r_type = elfcpp::R_POWERPC_NONE;
5349 offset -= 2 * big_endian;
5350 }
5351 break;
5352 default:
5353 break;
5354 }
5355 }
5356 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5357 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5358 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5359 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5360 {
5361 // First instruction of a local dynamic sequence,
5362 // arg setup insn.
5363 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5364 {
5365 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5366 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5367 {
5368 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5369 const Output_section* os = relinfo->layout->tls_segment()
5370 ->first_section();
5371 gold_assert(os != NULL);
5372 gold_assert(os->needs_symtab_index());
5373 r_sym = os->symtab_index();
5374 addend = dtp_offset;
5375 }
5376 else
5377 {
5378 r_type = elfcpp::R_POWERPC_NONE;
5379 offset -= 2 * big_endian;
5380 }
5381 }
5382 }
5383 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5384 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5385 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5386 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5387 {
5388 // First instruction of initial exec sequence.
5389 const bool final = gsym == NULL || gsym->final_value_is_known();
5390 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5391 {
5392 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5393 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5394 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5395 else
5396 {
5397 r_type = elfcpp::R_POWERPC_NONE;
5398 offset -= 2 * big_endian;
5399 }
5400 }
5401 }
5402 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5403 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5404 {
5405 // Second instruction of a global dynamic sequence,
5406 // the __tls_get_addr call
5407 const bool final = gsym == NULL || gsym->final_value_is_known();
5408 switch (this->optimize_tls_gd(final))
5409 {
5410 case tls::TLSOPT_TO_IE:
5411 r_type = elfcpp::R_POWERPC_NONE;
5412 zap_next = true;
5413 break;
5414 case tls::TLSOPT_TO_LE:
5415 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5416 offset += 2 * big_endian;
5417 zap_next = true;
5418 break;
5419 default:
5420 break;
5421 }
5422 }
5423 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5424 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5425 {
5426 // Second instruction of a local dynamic sequence,
5427 // the __tls_get_addr call
5428 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5429 {
5430 const Output_section* os = relinfo->layout->tls_segment()
5431 ->first_section();
5432 gold_assert(os != NULL);
5433 gold_assert(os->needs_symtab_index());
5434 r_sym = os->symtab_index();
5435 addend = dtp_offset;
5436 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5437 offset += 2 * big_endian;
5438 zap_next = true;
5439 }
5440 }
5441 else if (r_type == elfcpp::R_POWERPC_TLS)
5442 {
5443 // Second instruction of an initial exec sequence
5444 const bool final = gsym == NULL || gsym->final_value_is_known();
5445 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5446 {
5447 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5448 offset += 2 * big_endian;
5449 }
5450 }
5451 }
5452
5453 reloc_write.put_r_offset(offset);
5454 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5455 reloc_write.put_r_addend(addend);
5456
5457 pwrite += reloc_size;
5458 }
5459
5460 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5461 == reloc_view_size);
5462 }
5463
5464 // Return the value to use for a dynamic which requires special
5465 // treatment. This is how we support equality comparisons of function
5466 // pointers across shared library boundaries, as described in the
5467 // processor specific ABI supplement.
5468
5469 template<int size, bool big_endian>
5470 uint64_t
5471 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5472 {
5473 if (size == 32)
5474 {
5475 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5476 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5477 unsigned int glink_index = glink->find_entry(gsym);
5478 return glink->address() + glink_index * glink->glink_entry_size();
5479 }
5480 else
5481 gold_unreachable();
5482 }
5483
5484 // Return the PLT address to use for a local symbol.
5485 template<int size, bool big_endian>
5486 uint64_t
5487 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5488 const Relobj* object,
5489 unsigned int symndx) const
5490 {
5491 if (size == 32)
5492 {
5493 const Sized_relobj<size, big_endian>* relobj
5494 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5495 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5496 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5497 symndx);
5498 return glink->address() + glink_index * glink->glink_entry_size();
5499 }
5500 else
5501 gold_unreachable();
5502 }
5503
5504 // Return the PLT address to use for a global symbol.
5505 template<int size, bool big_endian>
5506 uint64_t
5507 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5508 const Symbol* gsym) const
5509 {
5510 if (size == 32)
5511 {
5512 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5513 unsigned int glink_index = glink->find_entry(gsym);
5514 return glink->address() + glink_index * glink->glink_entry_size();
5515 }
5516 else
5517 gold_unreachable();
5518 }
5519
5520 // Return the offset to use for the GOT_INDX'th got entry which is
5521 // for a local tls symbol specified by OBJECT, SYMNDX.
5522 template<int size, bool big_endian>
5523 int64_t
5524 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5525 const Relobj* object,
5526 unsigned int symndx,
5527 unsigned int got_indx) const
5528 {
5529 const Powerpc_relobj<size, big_endian>* ppc_object
5530 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5531 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5532 {
5533 for (Got_type got_type = GOT_TYPE_TLSGD;
5534 got_type <= GOT_TYPE_TPREL;
5535 got_type = Got_type(got_type + 1))
5536 if (ppc_object->local_has_got_offset(symndx, got_type))
5537 {
5538 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5539 if (got_type == GOT_TYPE_TLSGD)
5540 off += size / 8;
5541 if (off == got_indx * (size / 8))
5542 {
5543 if (got_type == GOT_TYPE_TPREL)
5544 return -tp_offset;
5545 else
5546 return -dtp_offset;
5547 }
5548 }
5549 }
5550 gold_unreachable();
5551 }
5552
5553 // Return the offset to use for the GOT_INDX'th got entry which is
5554 // for global tls symbol GSYM.
5555 template<int size, bool big_endian>
5556 int64_t
5557 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5558 Symbol* gsym,
5559 unsigned int got_indx) const
5560 {
5561 if (gsym->type() == elfcpp::STT_TLS)
5562 {
5563 for (Got_type got_type = GOT_TYPE_TLSGD;
5564 got_type <= GOT_TYPE_TPREL;
5565 got_type = Got_type(got_type + 1))
5566 if (gsym->has_got_offset(got_type))
5567 {
5568 unsigned int off = gsym->got_offset(got_type);
5569 if (got_type == GOT_TYPE_TLSGD)
5570 off += size / 8;
5571 if (off == got_indx * (size / 8))
5572 {
5573 if (got_type == GOT_TYPE_TPREL)
5574 return -tp_offset;
5575 else
5576 return -dtp_offset;
5577 }
5578 }
5579 }
5580 gold_unreachable();
5581 }
5582
5583 // The selector for powerpc object files.
5584
5585 template<int size, bool big_endian>
5586 class Target_selector_powerpc : public Target_selector
5587 {
5588 public:
5589 Target_selector_powerpc()
5590 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5591 (size == 64
5592 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5593 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5594 (size == 64
5595 ? (big_endian ? "elf64ppc" : "elf64lppc")
5596 : (big_endian ? "elf32ppc" : "elf32lppc")))
5597 { }
5598
5599 virtual Target*
5600 do_recognize(Input_file*, off_t, int machine, int, int)
5601 {
5602 switch (size)
5603 {
5604 case 64:
5605 if (machine != elfcpp::EM_PPC64)
5606 return NULL;
5607 break;
5608
5609 case 32:
5610 if (machine != elfcpp::EM_PPC)
5611 return NULL;
5612 break;
5613
5614 default:
5615 return NULL;
5616 }
5617
5618 return this->instantiate_target();
5619 }
5620
5621 virtual Target*
5622 do_instantiate_target()
5623 { return new Target_powerpc<size, big_endian>(); }
5624 };
5625
5626 Target_selector_powerpc<32, true> target_selector_ppc32;
5627 Target_selector_powerpc<32, false> target_selector_ppc32le;
5628 Target_selector_powerpc<64, true> target_selector_ppc64;
5629 Target_selector_powerpc<64, false> target_selector_ppc64le;
5630
5631 } // End anonymous namespace.