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