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