* powerpc.cc (Target_powerpc::Scan::local, global): Always emit
[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 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
3415 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
3416 {
3417 Output_data_got_powerpc<size, big_endian>* got
3418 = target->got_section(symtab, layout);
3419 unsigned int off = got->add_constant(0);
3420 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
3421
3422 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3423 rela_dyn->add_symbolless_local_addend(object, r_sym,
3424 elfcpp::R_POWERPC_TPREL,
3425 got, off, 0);
3426 }
3427 }
3428 else if (tls_type == tls::TLSOPT_TO_LE)
3429 {
3430 // no GOT relocs needed for Local Exec.
3431 }
3432 else
3433 gold_unreachable();
3434 }
3435 break;
3436
3437 default:
3438 unsupported_reloc_local(object, r_type);
3439 break;
3440 }
3441 }
3442
3443 // Report an unsupported relocation against a global symbol.
3444
3445 template<int size, bool big_endian>
3446 void
3447 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
3448 Sized_relobj_file<size, big_endian>* object,
3449 unsigned int r_type,
3450 Symbol* gsym)
3451 {
3452 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3453 object->name().c_str(), r_type, gsym->demangled_name().c_str());
3454 }
3455
3456 // Scan a relocation for a global symbol.
3457
3458 template<int size, bool big_endian>
3459 inline void
3460 Target_powerpc<size, big_endian>::Scan::global(
3461 Symbol_table* symtab,
3462 Layout* layout,
3463 Target_powerpc<size, big_endian>* target,
3464 Sized_relobj_file<size, big_endian>* object,
3465 unsigned int data_shndx,
3466 Output_section* output_section,
3467 const elfcpp::Rela<size, big_endian>& reloc,
3468 unsigned int r_type,
3469 Symbol* gsym)
3470 {
3471 Powerpc_relobj<size, big_endian>* ppc_object
3472 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3473
3474 // A STT_GNU_IFUNC symbol may require a PLT entry.
3475 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3476 && this->reloc_needs_plt_for_ifunc(object, r_type))
3477 target->make_plt_entry(layout, gsym, reloc, object);
3478
3479 switch (r_type)
3480 {
3481 case elfcpp::R_POWERPC_NONE:
3482 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3483 case elfcpp::R_POWERPC_GNU_VTENTRY:
3484 case elfcpp::R_PPC_LOCAL24PC:
3485 case elfcpp::R_PPC_EMB_MRKREF:
3486 case elfcpp::R_POWERPC_TLS:
3487 break;
3488
3489 case elfcpp::R_PPC64_TOC:
3490 {
3491 Output_data_got_powerpc<size, big_endian>* got
3492 = target->got_section(symtab, layout);
3493 if (parameters->options().output_is_position_independent())
3494 {
3495 Address off = reloc.get_r_offset();
3496 if (size == 64
3497 && data_shndx == ppc_object->opd_shndx()
3498 && ppc_object->get_opd_discard(off - 8))
3499 break;
3500
3501 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3502 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
3503 if (data_shndx != ppc_object->opd_shndx())
3504 symobj = static_cast
3505 <Powerpc_relobj<size, big_endian>*>(gsym->object());
3506 rela_dyn->add_output_section_relative(got->output_section(),
3507 elfcpp::R_POWERPC_RELATIVE,
3508 output_section,
3509 object, data_shndx, off,
3510 symobj->toc_base_offset());
3511 }
3512 }
3513 break;
3514
3515 case elfcpp::R_PPC64_ADDR64:
3516 if (size == 64
3517 && data_shndx == ppc_object->opd_shndx()
3518 && (gsym->is_defined_in_discarded_section()
3519 || gsym->object() != object))
3520 {
3521 ppc_object->set_opd_discard(reloc.get_r_offset());
3522 break;
3523 }
3524 // Fall thru
3525 case elfcpp::R_PPC64_UADDR64:
3526 case elfcpp::R_POWERPC_ADDR32:
3527 case elfcpp::R_POWERPC_UADDR32:
3528 case elfcpp::R_POWERPC_ADDR24:
3529 case elfcpp::R_POWERPC_ADDR16:
3530 case elfcpp::R_POWERPC_ADDR16_LO:
3531 case elfcpp::R_POWERPC_ADDR16_HI:
3532 case elfcpp::R_POWERPC_ADDR16_HA:
3533 case elfcpp::R_POWERPC_UADDR16:
3534 case elfcpp::R_PPC64_ADDR16_HIGHER:
3535 case elfcpp::R_PPC64_ADDR16_HIGHERA:
3536 case elfcpp::R_PPC64_ADDR16_HIGHEST:
3537 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3538 case elfcpp::R_PPC64_ADDR16_DS:
3539 case elfcpp::R_PPC64_ADDR16_LO_DS:
3540 case elfcpp::R_POWERPC_ADDR14:
3541 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3542 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3543 {
3544 // Make a PLT entry if necessary.
3545 if (gsym->needs_plt_entry())
3546 {
3547 target->make_plt_entry(layout, gsym, reloc, 0);
3548 // Since this is not a PC-relative relocation, we may be
3549 // taking the address of a function. In that case we need to
3550 // set the entry in the dynamic symbol table to the address of
3551 // the PLT call stub.
3552 if (size == 32
3553 && gsym->is_from_dynobj()
3554 && !parameters->options().output_is_position_independent())
3555 gsym->set_needs_dynsym_value();
3556 }
3557 // Make a dynamic relocation if necessary.
3558 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
3559 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
3560 {
3561 if (gsym->may_need_copy_reloc())
3562 {
3563 target->copy_reloc(symtab, layout, object,
3564 data_shndx, output_section, gsym, reloc);
3565 }
3566 else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
3567 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
3568 && (gsym->can_use_relative_reloc(false)
3569 || (size == 64
3570 && data_shndx == ppc_object->opd_shndx())))
3571 {
3572 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3573 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3574 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3575 {
3576 rela_dyn = target->iplt_section()->rel_plt();
3577 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3578 }
3579 rela_dyn->add_symbolless_global_addend(
3580 gsym, dynrel, output_section, object, data_shndx,
3581 reloc.get_r_offset(), reloc.get_r_addend());
3582 }
3583 else
3584 {
3585 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3586 check_non_pic(object, r_type);
3587 rela_dyn->add_global(gsym, r_type, output_section,
3588 object, data_shndx,
3589 reloc.get_r_offset(),
3590 reloc.get_r_addend());
3591 }
3592 }
3593 }
3594 break;
3595
3596 case elfcpp::R_PPC_PLTREL24:
3597 case elfcpp::R_POWERPC_REL24:
3598 if (gsym->needs_plt_entry()
3599 || (!gsym->final_value_is_known()
3600 && (gsym->is_undefined()
3601 || gsym->is_from_dynobj()
3602 || gsym->is_preemptible())))
3603 target->make_plt_entry(layout, gsym, reloc, object);
3604 // Fall thru
3605
3606 case elfcpp::R_PPC64_REL64:
3607 case elfcpp::R_POWERPC_REL32:
3608 // Make a dynamic relocation if necessary.
3609 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
3610 {
3611 if (gsym->may_need_copy_reloc())
3612 {
3613 target->copy_reloc(symtab, layout, object,
3614 data_shndx, output_section, gsym,
3615 reloc);
3616 }
3617 else
3618 {
3619 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3620 check_non_pic(object, r_type);
3621 rela_dyn->add_global(gsym, r_type, output_section, object,
3622 data_shndx, reloc.get_r_offset(),
3623 reloc.get_r_addend());
3624 }
3625 }
3626 break;
3627
3628 case elfcpp::R_POWERPC_REL16:
3629 case elfcpp::R_POWERPC_REL16_LO:
3630 case elfcpp::R_POWERPC_REL16_HI:
3631 case elfcpp::R_POWERPC_REL16_HA:
3632 case elfcpp::R_POWERPC_REL14:
3633 case elfcpp::R_POWERPC_REL14_BRTAKEN:
3634 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3635 case elfcpp::R_POWERPC_SECTOFF:
3636 case elfcpp::R_POWERPC_TPREL16:
3637 case elfcpp::R_POWERPC_DTPREL16:
3638 case elfcpp::R_POWERPC_SECTOFF_LO:
3639 case elfcpp::R_POWERPC_TPREL16_LO:
3640 case elfcpp::R_POWERPC_DTPREL16_LO:
3641 case elfcpp::R_POWERPC_SECTOFF_HI:
3642 case elfcpp::R_POWERPC_TPREL16_HI:
3643 case elfcpp::R_POWERPC_DTPREL16_HI:
3644 case elfcpp::R_POWERPC_SECTOFF_HA:
3645 case elfcpp::R_POWERPC_TPREL16_HA:
3646 case elfcpp::R_POWERPC_DTPREL16_HA:
3647 case elfcpp::R_PPC64_DTPREL16_HIGHER:
3648 case elfcpp::R_PPC64_TPREL16_HIGHER:
3649 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3650 case elfcpp::R_PPC64_TPREL16_HIGHERA:
3651 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3652 case elfcpp::R_PPC64_TPREL16_HIGHEST:
3653 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3654 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3655 case elfcpp::R_PPC64_TPREL16_DS:
3656 case elfcpp::R_PPC64_TPREL16_LO_DS:
3657 case elfcpp::R_PPC64_DTPREL16_DS:
3658 case elfcpp::R_PPC64_DTPREL16_LO_DS:
3659 case elfcpp::R_PPC64_SECTOFF_DS:
3660 case elfcpp::R_PPC64_SECTOFF_LO_DS:
3661 case elfcpp::R_PPC64_TLSGD:
3662 case elfcpp::R_PPC64_TLSLD:
3663 break;
3664
3665 case elfcpp::R_POWERPC_GOT16:
3666 case elfcpp::R_POWERPC_GOT16_LO:
3667 case elfcpp::R_POWERPC_GOT16_HI:
3668 case elfcpp::R_POWERPC_GOT16_HA:
3669 case elfcpp::R_PPC64_GOT16_DS:
3670 case elfcpp::R_PPC64_GOT16_LO_DS:
3671 {
3672 // The symbol requires a GOT entry.
3673 Output_data_got_powerpc<size, big_endian>* got;
3674
3675 got = target->got_section(symtab, layout);
3676 if (gsym->final_value_is_known())
3677 {
3678 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
3679 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3680 else
3681 got->add_global(gsym, GOT_TYPE_STANDARD);
3682 }
3683 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
3684 {
3685 // If we are generating a shared object or a pie, this
3686 // symbol's GOT entry will be set by a dynamic relocation.
3687 unsigned int off = got->add_constant(0);
3688 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
3689
3690 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3691 if (gsym->can_use_relative_reloc(false)
3692 && !(size == 32
3693 && gsym->visibility() == elfcpp::STV_PROTECTED
3694 && parameters->options().shared()))
3695 {
3696 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
3697 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3698 {
3699 rela_dyn = target->iplt_section()->rel_plt();
3700 dynrel = elfcpp::R_POWERPC_IRELATIVE;
3701 }
3702 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
3703 }
3704 else
3705 {
3706 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
3707 rela_dyn->add_global(gsym, dynrel, got, off, 0);
3708 }
3709 }
3710 }
3711 break;
3712
3713 case elfcpp::R_PPC64_TOC16:
3714 case elfcpp::R_PPC64_TOC16_LO:
3715 case elfcpp::R_PPC64_TOC16_HI:
3716 case elfcpp::R_PPC64_TOC16_HA:
3717 case elfcpp::R_PPC64_TOC16_DS:
3718 case elfcpp::R_PPC64_TOC16_LO_DS:
3719 // We need a GOT section.
3720 target->got_section(symtab, layout);
3721 break;
3722
3723 case elfcpp::R_POWERPC_GOT_TLSGD16:
3724 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3725 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3726 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3727 {
3728 const bool final = gsym->final_value_is_known();
3729 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3730 if (tls_type == tls::TLSOPT_NONE)
3731 {
3732 Output_data_got_powerpc<size, big_endian>* got
3733 = target->got_section(symtab, layout);
3734 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
3735 target->rela_dyn_section(layout),
3736 elfcpp::R_POWERPC_DTPMOD,
3737 elfcpp::R_POWERPC_DTPREL);
3738 }
3739 else if (tls_type == tls::TLSOPT_TO_IE)
3740 {
3741 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3742 {
3743 Output_data_got_powerpc<size, big_endian>* got
3744 = target->got_section(symtab, layout);
3745 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3746 if (gsym->is_undefined()
3747 || gsym->is_from_dynobj())
3748 {
3749 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3750 elfcpp::R_POWERPC_TPREL);
3751 }
3752 else
3753 {
3754 unsigned int off = got->add_constant(0);
3755 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3756 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3757 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3758 got, off, 0);
3759 }
3760 }
3761 }
3762 else if (tls_type == tls::TLSOPT_TO_LE)
3763 {
3764 // no GOT relocs needed for Local Exec.
3765 }
3766 else
3767 gold_unreachable();
3768 }
3769 break;
3770
3771 case elfcpp::R_POWERPC_GOT_TLSLD16:
3772 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3773 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3774 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3775 {
3776 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3777 if (tls_type == tls::TLSOPT_NONE)
3778 target->tlsld_got_offset(symtab, layout, object);
3779 else if (tls_type == tls::TLSOPT_TO_LE)
3780 {
3781 // no GOT relocs needed for Local Exec.
3782 if (parameters->options().emit_relocs())
3783 {
3784 Output_section* os = layout->tls_segment()->first_section();
3785 gold_assert(os != NULL);
3786 os->set_needs_symtab_index();
3787 }
3788 }
3789 else
3790 gold_unreachable();
3791 }
3792 break;
3793
3794 case elfcpp::R_POWERPC_GOT_DTPREL16:
3795 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3796 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3797 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3798 {
3799 Output_data_got_powerpc<size, big_endian>* got
3800 = target->got_section(symtab, layout);
3801 if (!gsym->final_value_is_known()
3802 && (gsym->is_from_dynobj()
3803 || gsym->is_undefined()
3804 || gsym->is_preemptible()))
3805 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
3806 target->rela_dyn_section(layout),
3807 elfcpp::R_POWERPC_DTPREL);
3808 else
3809 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
3810 }
3811 break;
3812
3813 case elfcpp::R_POWERPC_GOT_TPREL16:
3814 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3815 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3816 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3817 {
3818 const bool final = gsym->final_value_is_known();
3819 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3820 if (tls_type == tls::TLSOPT_NONE)
3821 {
3822 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
3823 {
3824 Output_data_got_powerpc<size, big_endian>* got
3825 = target->got_section(symtab, layout);
3826 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3827 if (gsym->is_undefined()
3828 || gsym->is_from_dynobj())
3829 {
3830 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
3831 elfcpp::R_POWERPC_TPREL);
3832 }
3833 else
3834 {
3835 unsigned int off = got->add_constant(0);
3836 gsym->set_got_offset(GOT_TYPE_TPREL, off);
3837 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
3838 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
3839 got, off, 0);
3840 }
3841 }
3842 }
3843 else if (tls_type == tls::TLSOPT_TO_LE)
3844 {
3845 // no GOT relocs needed for Local Exec.
3846 }
3847 else
3848 gold_unreachable();
3849 }
3850 break;
3851
3852 default:
3853 unsupported_reloc_global(object, r_type, gsym);
3854 break;
3855 }
3856 }
3857
3858 // Process relocations for gc.
3859
3860 template<int size, bool big_endian>
3861 void
3862 Target_powerpc<size, big_endian>::gc_process_relocs(
3863 Symbol_table* symtab,
3864 Layout* layout,
3865 Sized_relobj_file<size, big_endian>* object,
3866 unsigned int data_shndx,
3867 unsigned int,
3868 const unsigned char* prelocs,
3869 size_t reloc_count,
3870 Output_section* output_section,
3871 bool needs_special_offset_handling,
3872 size_t local_symbol_count,
3873 const unsigned char* plocal_symbols)
3874 {
3875 typedef Target_powerpc<size, big_endian> Powerpc;
3876 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3877 Powerpc_relobj<size, big_endian>* ppc_object
3878 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
3879 if (size == 64)
3880 ppc_object->set_opd_valid();
3881 if (size == 64 && data_shndx == ppc_object->opd_shndx())
3882 {
3883 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
3884 for (p = ppc_object->access_from_map()->begin();
3885 p != ppc_object->access_from_map()->end();
3886 ++p)
3887 {
3888 Address dst_off = p->first;
3889 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3890 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
3891 for (s = p->second.begin(); s != p->second.end(); ++s)
3892 {
3893 Object* src_obj = s->first;
3894 unsigned int src_indx = s->second;
3895 symtab->gc()->add_reference(src_obj, src_indx,
3896 ppc_object, dst_indx);
3897 }
3898 p->second.clear();
3899 }
3900 ppc_object->access_from_map()->clear();
3901 ppc_object->process_gc_mark(symtab);
3902 // Don't look at .opd relocs as .opd will reference everything.
3903 return;
3904 }
3905
3906 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3907 typename Target_powerpc::Relocatable_size_for_reloc>(
3908 symtab,
3909 layout,
3910 this,
3911 object,
3912 data_shndx,
3913 prelocs,
3914 reloc_count,
3915 output_section,
3916 needs_special_offset_handling,
3917 local_symbol_count,
3918 plocal_symbols);
3919 }
3920
3921 // Handle target specific gc actions when adding a gc reference from
3922 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3923 // and DST_OFF. For powerpc64, this adds a referenc to the code
3924 // section of a function descriptor.
3925
3926 template<int size, bool big_endian>
3927 void
3928 Target_powerpc<size, big_endian>::do_gc_add_reference(
3929 Symbol_table* symtab,
3930 Object* src_obj,
3931 unsigned int src_shndx,
3932 Object* dst_obj,
3933 unsigned int dst_shndx,
3934 Address dst_off) const
3935 {
3936 Powerpc_relobj<size, big_endian>* ppc_object
3937 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3938 if (size == 64
3939 && !ppc_object->is_dynamic()
3940 && dst_shndx == ppc_object->opd_shndx())
3941 {
3942 if (ppc_object->opd_valid())
3943 {
3944 dst_shndx = ppc_object->get_opd_ent(dst_off);
3945 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3946 }
3947 else
3948 {
3949 // If we haven't run scan_opd_relocs, we must delay
3950 // processing this function descriptor reference.
3951 ppc_object->add_reference(src_obj, src_shndx, dst_off);
3952 }
3953 }
3954 }
3955
3956 // Add any special sections for this symbol to the gc work list.
3957 // For powerpc64, this adds the code section of a function
3958 // descriptor.
3959
3960 template<int size, bool big_endian>
3961 void
3962 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3963 Symbol_table* symtab,
3964 Symbol* sym) const
3965 {
3966 if (size == 64)
3967 {
3968 Powerpc_relobj<size, big_endian>* ppc_object
3969 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3970 bool is_ordinary;
3971 unsigned int shndx = sym->shndx(&is_ordinary);
3972 if (is_ordinary && shndx == ppc_object->opd_shndx())
3973 {
3974 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
3975 Address dst_off = gsym->value();
3976 if (ppc_object->opd_valid())
3977 {
3978 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3979 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
3980 }
3981 else
3982 ppc_object->add_gc_mark(dst_off);
3983 }
3984 }
3985 }
3986
3987 // Scan relocations for a section.
3988
3989 template<int size, bool big_endian>
3990 void
3991 Target_powerpc<size, big_endian>::scan_relocs(
3992 Symbol_table* symtab,
3993 Layout* layout,
3994 Sized_relobj_file<size, big_endian>* object,
3995 unsigned int data_shndx,
3996 unsigned int sh_type,
3997 const unsigned char* prelocs,
3998 size_t reloc_count,
3999 Output_section* output_section,
4000 bool needs_special_offset_handling,
4001 size_t local_symbol_count,
4002 const unsigned char* plocal_symbols)
4003 {
4004 typedef Target_powerpc<size, big_endian> Powerpc;
4005 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
4006
4007 if (sh_type == elfcpp::SHT_REL)
4008 {
4009 gold_error(_("%s: unsupported REL reloc section"),
4010 object->name().c_str());
4011 return;
4012 }
4013
4014 if (size == 32)
4015 {
4016 // Define a weak hidden _GLOBAL_OFFSET_TABLE_ to ensure it isn't
4017 // seen as undefined when scanning relocs (and thus requires
4018 // non-relative dynamic relocs). The proper value will be
4019 // updated later.
4020 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
4021 if (gotsym != NULL && gotsym->is_undefined())
4022 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
4023 Symbol_table::PREDEFINED,
4024 this->got_section(symtab, layout), 0, 0,
4025 elfcpp::STT_OBJECT,
4026 elfcpp::STB_WEAK,
4027 elfcpp::STV_HIDDEN, 0,
4028 false, false);
4029
4030 static Output_data_space* sdata;
4031
4032 // Define _SDA_BASE_ at the start of the .sdata section.
4033 if (sdata == NULL)
4034 {
4035 // layout->find_output_section(".sdata") == NULL
4036 sdata = new Output_data_space(4, "** sdata");
4037 Output_section* os
4038 = layout->add_output_section_data(".sdata", 0,
4039 elfcpp::SHF_ALLOC
4040 | elfcpp::SHF_WRITE,
4041 sdata, ORDER_SMALL_DATA, false);
4042 symtab->define_in_output_data("_SDA_BASE_", NULL,
4043 Symbol_table::PREDEFINED,
4044 os, 32768, 0, elfcpp::STT_OBJECT,
4045 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
4046 0, false, false);
4047 }
4048 }
4049
4050 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
4051 symtab,
4052 layout,
4053 this,
4054 object,
4055 data_shndx,
4056 prelocs,
4057 reloc_count,
4058 output_section,
4059 needs_special_offset_handling,
4060 local_symbol_count,
4061 plocal_symbols);
4062 }
4063
4064 // Functor class for processing the global symbol table.
4065 // Removes symbols defined on discarded opd entries.
4066
4067 template<bool big_endian>
4068 class Global_symbol_visitor_opd
4069 {
4070 public:
4071 Global_symbol_visitor_opd()
4072 { }
4073
4074 void
4075 operator()(Sized_symbol<64>* sym)
4076 {
4077 if (sym->has_symtab_index()
4078 || sym->source() != Symbol::FROM_OBJECT
4079 || !sym->in_real_elf())
4080 return;
4081
4082 Powerpc_relobj<64, big_endian>* symobj
4083 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
4084 if (symobj->is_dynamic()
4085 || symobj->opd_shndx() == 0)
4086 return;
4087
4088 bool is_ordinary;
4089 unsigned int shndx = sym->shndx(&is_ordinary);
4090 if (shndx == symobj->opd_shndx()
4091 && symobj->get_opd_discard(sym->value()))
4092 sym->set_symtab_index(-1U);
4093 }
4094 };
4095
4096 template<int size, bool big_endian>
4097 void
4098 Target_powerpc<size, big_endian>::define_save_restore_funcs(
4099 Layout* layout,
4100 Symbol_table* symtab)
4101 {
4102 if (size == 64)
4103 {
4104 Output_data_save_res<64, big_endian>* savres
4105 = new Output_data_save_res<64, big_endian>(symtab);
4106 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4107 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4108 savres, ORDER_TEXT, false);
4109 }
4110 }
4111
4112 // Finalize the sections.
4113
4114 template<int size, bool big_endian>
4115 void
4116 Target_powerpc<size, big_endian>::do_finalize_sections(
4117 Layout* layout,
4118 const Input_objects*,
4119 Symbol_table* symtab)
4120 {
4121 if (parameters->doing_static_link())
4122 {
4123 // At least some versions of glibc elf-init.o have a strong
4124 // reference to __rela_iplt marker syms. A weak ref would be
4125 // better..
4126 if (this->iplt_ != NULL)
4127 {
4128 Reloc_section* rel = this->iplt_->rel_plt();
4129 symtab->define_in_output_data("__rela_iplt_start", NULL,
4130 Symbol_table::PREDEFINED, rel, 0, 0,
4131 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4132 elfcpp::STV_HIDDEN, 0, false, true);
4133 symtab->define_in_output_data("__rela_iplt_end", NULL,
4134 Symbol_table::PREDEFINED, rel, 0, 0,
4135 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4136 elfcpp::STV_HIDDEN, 0, true, true);
4137 }
4138 else
4139 {
4140 symtab->define_as_constant("__rela_iplt_start", NULL,
4141 Symbol_table::PREDEFINED, 0, 0,
4142 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4143 elfcpp::STV_HIDDEN, 0, true, false);
4144 symtab->define_as_constant("__rela_iplt_end", NULL,
4145 Symbol_table::PREDEFINED, 0, 0,
4146 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
4147 elfcpp::STV_HIDDEN, 0, true, false);
4148 }
4149 }
4150
4151 if (size == 64)
4152 {
4153 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
4154 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
4155 this->define_save_restore_funcs(layout, symtab);
4156 }
4157
4158 // Fill in some more dynamic tags.
4159 Output_data_dynamic* odyn = layout->dynamic_data();
4160 if (odyn != NULL)
4161 {
4162 const Reloc_section* rel_plt = (this->plt_ == NULL
4163 ? NULL
4164 : this->plt_->rel_plt());
4165 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
4166 this->rela_dyn_, true, size == 32);
4167
4168 if (size == 32)
4169 {
4170 if (this->got_ != NULL)
4171 {
4172 this->got_->finalize_data_size();
4173 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
4174 this->got_, this->got_->g_o_t());
4175 }
4176 }
4177 else
4178 {
4179 if (this->glink_ != NULL)
4180 {
4181 this->glink_->finalize_data_size();
4182 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
4183 this->glink_,
4184 (this->glink_->pltresolve()
4185 + this->glink_->pltresolve_size
4186 - 32));
4187 }
4188 }
4189 }
4190
4191 // Emit any relocs we saved in an attempt to avoid generating COPY
4192 // relocs.
4193 if (this->copy_relocs_.any_saved_relocs())
4194 this->copy_relocs_.emit(this->rela_dyn_section(layout));
4195 }
4196
4197 // Return the value to use for a branch relocation.
4198
4199 template<int size, bool big_endian>
4200 typename elfcpp::Elf_types<size>::Elf_Addr
4201 Target_powerpc<size, big_endian>::symval_for_branch(
4202 Address value,
4203 const Sized_symbol<size>* gsym,
4204 Powerpc_relobj<size, big_endian>* object,
4205 unsigned int *dest_shndx)
4206 {
4207 *dest_shndx = 0;
4208 if (size == 32)
4209 return value;
4210
4211 // If the symbol is defined in an opd section, ie. is a function
4212 // descriptor, use the function descriptor code entry address
4213 Powerpc_relobj<size, big_endian>* symobj = object;
4214 if (gsym != NULL
4215 && gsym->source() != Symbol::FROM_OBJECT)
4216 return value;
4217 if (gsym != NULL)
4218 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
4219 unsigned int shndx = symobj->opd_shndx();
4220 if (shndx == 0)
4221 return value;
4222 Address opd_addr = symobj->get_output_section_offset(shndx);
4223 gold_assert(opd_addr != invalid_address);
4224 opd_addr += symobj->output_section(shndx)->address();
4225 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
4226 {
4227 Address sec_off;
4228 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
4229 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
4230 gold_assert(sec_addr != invalid_address);
4231 sec_addr += symobj->output_section(*dest_shndx)->address();
4232 value = sec_addr + sec_off;
4233 }
4234 return value;
4235 }
4236
4237 // Perform a relocation.
4238
4239 template<int size, bool big_endian>
4240 inline bool
4241 Target_powerpc<size, big_endian>::Relocate::relocate(
4242 const Relocate_info<size, big_endian>* relinfo,
4243 Target_powerpc* target,
4244 Output_section* os,
4245 size_t relnum,
4246 const elfcpp::Rela<size, big_endian>& rela,
4247 unsigned int r_type,
4248 const Sized_symbol<size>* gsym,
4249 const Symbol_value<size>* psymval,
4250 unsigned char* view,
4251 Address address,
4252 section_size_type view_size)
4253 {
4254
4255 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
4256 || r_type == elfcpp::R_PPC_PLTREL24)
4257 && gsym != NULL
4258 && strcmp(gsym->name(), "__tls_get_addr") == 0);
4259 enum skip_tls last_tls = this->call_tls_get_addr_;
4260 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
4261 if (is_tls_call)
4262 {
4263 if (last_tls == CALL_NOT_EXPECTED)
4264 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4265 _("__tls_get_addr call lacks marker reloc"));
4266 else if (last_tls == CALL_SKIP)
4267 return false;
4268 }
4269 else if (last_tls != CALL_NOT_EXPECTED)
4270 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4271 _("missing expected __tls_get_addr call"));
4272
4273 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
4274 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
4275 Powerpc_relobj<size, big_endian>* const object
4276 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4277 Address value = 0;
4278 bool has_plt_value = false;
4279 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4280 if (gsym != NULL
4281 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
4282 : object->local_has_plt_offset(r_sym))
4283 {
4284 const Output_data_glink<size, big_endian>* glink
4285 = target->glink_section();
4286 unsigned int glink_index;
4287 if (gsym != NULL)
4288 glink_index = glink->find_entry(object, gsym, rela);
4289 else
4290 glink_index = glink->find_entry(object, r_sym, rela);
4291 value = glink->address() + glink_index * glink->glink_entry_size();
4292 has_plt_value = true;
4293 }
4294
4295 if (r_type == elfcpp::R_POWERPC_GOT16
4296 || r_type == elfcpp::R_POWERPC_GOT16_LO
4297 || r_type == elfcpp::R_POWERPC_GOT16_HI
4298 || r_type == elfcpp::R_POWERPC_GOT16_HA
4299 || r_type == elfcpp::R_PPC64_GOT16_DS
4300 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
4301 {
4302 if (gsym != NULL)
4303 {
4304 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4305 value = gsym->got_offset(GOT_TYPE_STANDARD);
4306 }
4307 else
4308 {
4309 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4310 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4311 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
4312 }
4313 value -= target->got_section()->got_base_offset(object);
4314 }
4315 else if (r_type == elfcpp::R_PPC64_TOC)
4316 {
4317 value = (target->got_section()->output_section()->address()
4318 + object->toc_base_offset());
4319 }
4320 else if (gsym != NULL
4321 && (r_type == elfcpp::R_POWERPC_REL24
4322 || r_type == elfcpp::R_PPC_PLTREL24)
4323 && has_plt_value)
4324 {
4325 if (size == 64)
4326 {
4327 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
4328 Valtype* wv = reinterpret_cast<Valtype*>(view);
4329 bool can_plt_call = false;
4330 if (rela.get_r_offset() + 8 <= view_size)
4331 {
4332 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
4333 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
4334 if ((insn & 1) != 0
4335 && (insn2 == nop
4336 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
4337 {
4338 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
4339 can_plt_call = true;
4340 }
4341 }
4342 if (!can_plt_call)
4343 {
4344 // If we don't have a branch and link followed by a nop,
4345 // we can't go via the plt because there is no place to
4346 // put a toc restoring instruction.
4347 // Unless we know we won't be returning.
4348 if (strcmp(gsym->name(), "__libc_start_main") == 0)
4349 can_plt_call = true;
4350 }
4351 if (!can_plt_call)
4352 {
4353 // This is not an error in one special case: A self
4354 // call. It isn't possible to cheaply verify we have
4355 // such a call so just check for a call to the same
4356 // section.
4357 bool ok = false;
4358 Address code = value;
4359 if (gsym->source() == Symbol::FROM_OBJECT
4360 && gsym->object() == object)
4361 {
4362 Address addend = rela.get_r_addend();
4363 unsigned int dest_shndx;
4364 Address opdent = psymval->value(object, addend);
4365 code = target->symval_for_branch(opdent, gsym, object,
4366 &dest_shndx);
4367 bool is_ordinary;
4368 if (dest_shndx == 0)
4369 dest_shndx = gsym->shndx(&is_ordinary);
4370 ok = dest_shndx == relinfo->data_shndx;
4371 }
4372 if (!ok)
4373 {
4374 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4375 _("call lacks nop, can't restore toc; "
4376 "recompile with -fPIC"));
4377 value = code;
4378 }
4379 }
4380 }
4381 }
4382 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4383 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4384 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4385 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4386 {
4387 // First instruction of a global dynamic sequence, arg setup insn.
4388 const bool final = gsym == NULL || gsym->final_value_is_known();
4389 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4390 enum Got_type got_type = GOT_TYPE_STANDARD;
4391 if (tls_type == tls::TLSOPT_NONE)
4392 got_type = GOT_TYPE_TLSGD;
4393 else if (tls_type == tls::TLSOPT_TO_IE)
4394 got_type = GOT_TYPE_TPREL;
4395 if (got_type != GOT_TYPE_STANDARD)
4396 {
4397 if (gsym != NULL)
4398 {
4399 gold_assert(gsym->has_got_offset(got_type));
4400 value = gsym->got_offset(got_type);
4401 }
4402 else
4403 {
4404 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4405 gold_assert(object->local_has_got_offset(r_sym, got_type));
4406 value = object->local_got_offset(r_sym, got_type);
4407 }
4408 value -= target->got_section()->got_base_offset(object);
4409 }
4410 if (tls_type == tls::TLSOPT_TO_IE)
4411 {
4412 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4413 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4414 {
4415 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4416 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4417 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
4418 if (size == 32)
4419 insn |= 32 << 26; // lwz
4420 else
4421 insn |= 58 << 26; // ld
4422 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4423 }
4424 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4425 - elfcpp::R_POWERPC_GOT_TLSGD16);
4426 }
4427 else if (tls_type == tls::TLSOPT_TO_LE)
4428 {
4429 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4430 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4431 {
4432 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4433 Insn insn = addis_3_13;
4434 if (size == 32)
4435 insn = addis_3_2;
4436 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4437 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4438 value = psymval->value(object, rela.get_r_addend());
4439 }
4440 else
4441 {
4442 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4443 Insn insn = nop;
4444 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4445 r_type = elfcpp::R_POWERPC_NONE;
4446 }
4447 }
4448 }
4449 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4450 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4451 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4452 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4453 {
4454 // First instruction of a local dynamic sequence, arg setup insn.
4455 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4456 if (tls_type == tls::TLSOPT_NONE)
4457 {
4458 value = target->tlsld_got_offset();
4459 value -= target->got_section()->got_base_offset(object);
4460 }
4461 else
4462 {
4463 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4464 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4465 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4466 {
4467 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4468 Insn insn = addis_3_13;
4469 if (size == 32)
4470 insn = addis_3_2;
4471 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4472 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4473 value = dtp_offset;
4474 }
4475 else
4476 {
4477 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4478 Insn insn = nop;
4479 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4480 r_type = elfcpp::R_POWERPC_NONE;
4481 }
4482 }
4483 }
4484 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
4485 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
4486 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
4487 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
4488 {
4489 // Accesses relative to a local dynamic sequence address,
4490 // no optimisation here.
4491 if (gsym != NULL)
4492 {
4493 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
4494 value = gsym->got_offset(GOT_TYPE_DTPREL);
4495 }
4496 else
4497 {
4498 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4499 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
4500 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
4501 }
4502 value -= target->got_section()->got_base_offset(object);
4503 }
4504 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4505 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4506 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4507 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4508 {
4509 // First instruction of initial exec sequence.
4510 const bool final = gsym == NULL || gsym->final_value_is_known();
4511 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4512 if (tls_type == tls::TLSOPT_NONE)
4513 {
4514 if (gsym != NULL)
4515 {
4516 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
4517 value = gsym->got_offset(GOT_TYPE_TPREL);
4518 }
4519 else
4520 {
4521 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4522 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
4523 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
4524 }
4525 value -= target->got_section()->got_base_offset(object);
4526 }
4527 else
4528 {
4529 gold_assert(tls_type == tls::TLSOPT_TO_LE);
4530 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4531 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4532 {
4533 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4534 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4535 insn &= (1 << 26) - (1 << 21); // extract rt from ld
4536 if (size == 32)
4537 insn |= addis_0_2;
4538 else
4539 insn |= addis_0_13;
4540 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4541 r_type = elfcpp::R_POWERPC_TPREL16_HA;
4542 value = psymval->value(object, rela.get_r_addend());
4543 }
4544 else
4545 {
4546 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
4547 Insn insn = nop;
4548 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4549 r_type = elfcpp::R_POWERPC_NONE;
4550 }
4551 }
4552 }
4553 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4554 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4555 {
4556 // Second instruction of a global dynamic sequence,
4557 // the __tls_get_addr call
4558 this->call_tls_get_addr_ = CALL_EXPECTED;
4559 const bool final = gsym == NULL || gsym->final_value_is_known();
4560 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4561 if (tls_type != tls::TLSOPT_NONE)
4562 {
4563 if (tls_type == tls::TLSOPT_TO_IE)
4564 {
4565 Insn* iview = reinterpret_cast<Insn*>(view);
4566 Insn insn = add_3_3_13;
4567 if (size == 32)
4568 insn = add_3_3_2;
4569 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4570 r_type = elfcpp::R_POWERPC_NONE;
4571 }
4572 else
4573 {
4574 Insn* iview = reinterpret_cast<Insn*>(view);
4575 Insn insn = addi_3_3;
4576 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4577 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4578 view += 2 * big_endian;
4579 value = psymval->value(object, rela.get_r_addend());
4580 }
4581 this->call_tls_get_addr_ = CALL_SKIP;
4582 }
4583 }
4584 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4585 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4586 {
4587 // Second instruction of a local dynamic sequence,
4588 // the __tls_get_addr call
4589 this->call_tls_get_addr_ = CALL_EXPECTED;
4590 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4591 if (tls_type == tls::TLSOPT_TO_LE)
4592 {
4593 Insn* iview = reinterpret_cast<Insn*>(view);
4594 Insn insn = addi_3_3;
4595 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4596 this->call_tls_get_addr_ = CALL_SKIP;
4597 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4598 view += 2 * big_endian;
4599 value = dtp_offset;
4600 }
4601 }
4602 else if (r_type == elfcpp::R_POWERPC_TLS)
4603 {
4604 // Second instruction of an initial exec sequence
4605 const bool final = gsym == NULL || gsym->final_value_is_known();
4606 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4607 if (tls_type == tls::TLSOPT_TO_LE)
4608 {
4609 Insn* iview = reinterpret_cast<Insn*>(view);
4610 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4611 unsigned int reg = size == 32 ? 2 : 13;
4612 insn = at_tls_transform(insn, reg);
4613 gold_assert(insn != 0);
4614 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4615 r_type = elfcpp::R_POWERPC_TPREL16_LO;
4616 view += 2 * big_endian;
4617 value = psymval->value(object, rela.get_r_addend());
4618 }
4619 }
4620 else if (!has_plt_value)
4621 {
4622 Address addend = 0;
4623 unsigned int dest_shndx;
4624 if (r_type != elfcpp::R_PPC_PLTREL24)
4625 addend = rela.get_r_addend();
4626 value = psymval->value(object, addend);
4627 if (size == 64 && is_branch_reloc(r_type))
4628 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
4629 }
4630
4631 switch (r_type)
4632 {
4633 case elfcpp::R_PPC64_REL64:
4634 case elfcpp::R_POWERPC_REL32:
4635 case elfcpp::R_POWERPC_REL24:
4636 case elfcpp::R_PPC_PLTREL24:
4637 case elfcpp::R_PPC_LOCAL24PC:
4638 case elfcpp::R_POWERPC_REL16:
4639 case elfcpp::R_POWERPC_REL16_LO:
4640 case elfcpp::R_POWERPC_REL16_HI:
4641 case elfcpp::R_POWERPC_REL16_HA:
4642 case elfcpp::R_POWERPC_REL14:
4643 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4644 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4645 value -= address;
4646 break;
4647
4648 case elfcpp::R_PPC64_TOC16:
4649 case elfcpp::R_PPC64_TOC16_LO:
4650 case elfcpp::R_PPC64_TOC16_HI:
4651 case elfcpp::R_PPC64_TOC16_HA:
4652 case elfcpp::R_PPC64_TOC16_DS:
4653 case elfcpp::R_PPC64_TOC16_LO_DS:
4654 // Subtract the TOC base address.
4655 value -= (target->got_section()->output_section()->address()
4656 + object->toc_base_offset());
4657 break;
4658
4659 case elfcpp::R_POWERPC_SECTOFF:
4660 case elfcpp::R_POWERPC_SECTOFF_LO:
4661 case elfcpp::R_POWERPC_SECTOFF_HI:
4662 case elfcpp::R_POWERPC_SECTOFF_HA:
4663 case elfcpp::R_PPC64_SECTOFF_DS:
4664 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4665 if (os != NULL)
4666 value -= os->address();
4667 break;
4668
4669 case elfcpp::R_PPC64_TPREL16_DS:
4670 case elfcpp::R_PPC64_TPREL16_LO_DS:
4671 if (size != 64)
4672 // R_PPC_TLSGD and R_PPC_TLSLD
4673 break;
4674 case elfcpp::R_POWERPC_TPREL16:
4675 case elfcpp::R_POWERPC_TPREL16_LO:
4676 case elfcpp::R_POWERPC_TPREL16_HI:
4677 case elfcpp::R_POWERPC_TPREL16_HA:
4678 case elfcpp::R_POWERPC_TPREL:
4679 case elfcpp::R_PPC64_TPREL16_HIGHER:
4680 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4681 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4682 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4683 // tls symbol values are relative to tls_segment()->vaddr()
4684 value -= tp_offset;
4685 break;
4686
4687 case elfcpp::R_PPC64_DTPREL16_DS:
4688 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4689 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4690 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4691 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4692 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4693 if (size != 64)
4694 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
4695 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
4696 break;
4697 case elfcpp::R_POWERPC_DTPREL16:
4698 case elfcpp::R_POWERPC_DTPREL16_LO:
4699 case elfcpp::R_POWERPC_DTPREL16_HI:
4700 case elfcpp::R_POWERPC_DTPREL16_HA:
4701 case elfcpp::R_POWERPC_DTPREL:
4702 // tls symbol values are relative to tls_segment()->vaddr()
4703 value -= dtp_offset;
4704 break;
4705
4706 default:
4707 break;
4708 }
4709
4710 Insn branch_bit = 0;
4711 switch (r_type)
4712 {
4713 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4714 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4715 branch_bit = 1 << 21;
4716 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4717 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4718 {
4719 Insn* iview = reinterpret_cast<Insn*>(view);
4720 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
4721 insn &= ~(1 << 21);
4722 insn |= branch_bit;
4723 if (this->is_isa_v2)
4724 {
4725 // Set 'a' bit. This is 0b00010 in BO field for branch
4726 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
4727 // for branch on CTR insns (BO == 1a00t or 1a01t).
4728 if ((insn & (0x14 << 21)) == (0x04 << 21))
4729 insn |= 0x02 << 21;
4730 else if ((insn & (0x14 << 21)) == (0x10 << 21))
4731 insn |= 0x08 << 21;
4732 else
4733 break;
4734 }
4735 else
4736 {
4737 // Invert 'y' bit if not the default.
4738 if (static_cast<Signed_address>(value) < 0)
4739 insn ^= 1 << 21;
4740 }
4741 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
4742 }
4743 break;
4744
4745 default:
4746 break;
4747 }
4748
4749 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
4750 switch (r_type)
4751 {
4752 case elfcpp::R_POWERPC_ADDR32:
4753 case elfcpp::R_POWERPC_UADDR32:
4754 if (size == 64)
4755 overflow = Reloc::CHECK_BITFIELD;
4756 break;
4757
4758 case elfcpp::R_POWERPC_REL32:
4759 if (size == 64)
4760 overflow = Reloc::CHECK_SIGNED;
4761 break;
4762
4763 case elfcpp::R_POWERPC_ADDR24:
4764 case elfcpp::R_POWERPC_ADDR16:
4765 case elfcpp::R_POWERPC_UADDR16:
4766 case elfcpp::R_PPC64_ADDR16_DS:
4767 case elfcpp::R_POWERPC_ADDR14:
4768 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4769 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4770 overflow = Reloc::CHECK_BITFIELD;
4771 break;
4772
4773 case elfcpp::R_POWERPC_REL24:
4774 case elfcpp::R_PPC_PLTREL24:
4775 case elfcpp::R_PPC_LOCAL24PC:
4776 case elfcpp::R_POWERPC_REL16:
4777 case elfcpp::R_PPC64_TOC16:
4778 case elfcpp::R_POWERPC_GOT16:
4779 case elfcpp::R_POWERPC_SECTOFF:
4780 case elfcpp::R_POWERPC_TPREL16:
4781 case elfcpp::R_POWERPC_DTPREL16:
4782 case elfcpp::R_PPC64_TPREL16_DS:
4783 case elfcpp::R_PPC64_DTPREL16_DS:
4784 case elfcpp::R_PPC64_TOC16_DS:
4785 case elfcpp::R_PPC64_GOT16_DS:
4786 case elfcpp::R_PPC64_SECTOFF_DS:
4787 case elfcpp::R_POWERPC_REL14:
4788 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4789 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4790 case elfcpp::R_POWERPC_GOT_TLSGD16:
4791 case elfcpp::R_POWERPC_GOT_TLSLD16:
4792 case elfcpp::R_POWERPC_GOT_TPREL16:
4793 case elfcpp::R_POWERPC_GOT_DTPREL16:
4794 overflow = Reloc::CHECK_SIGNED;
4795 break;
4796 }
4797
4798 typename Powerpc_relocate_functions<size, big_endian>::Status status
4799 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
4800 switch (r_type)
4801 {
4802 case elfcpp::R_POWERPC_NONE:
4803 case elfcpp::R_POWERPC_TLS:
4804 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4805 case elfcpp::R_POWERPC_GNU_VTENTRY:
4806 case elfcpp::R_PPC_EMB_MRKREF:
4807 break;
4808
4809 case elfcpp::R_PPC64_ADDR64:
4810 case elfcpp::R_PPC64_REL64:
4811 case elfcpp::R_PPC64_TOC:
4812 Reloc::addr64(view, value);
4813 break;
4814
4815 case elfcpp::R_POWERPC_TPREL:
4816 case elfcpp::R_POWERPC_DTPREL:
4817 if (size == 64)
4818 Reloc::addr64(view, value);
4819 else
4820 status = Reloc::addr32(view, value, overflow);
4821 break;
4822
4823 case elfcpp::R_PPC64_UADDR64:
4824 Reloc::addr64_u(view, value);
4825 break;
4826
4827 case elfcpp::R_POWERPC_ADDR32:
4828 status = Reloc::addr32(view, value, overflow);
4829 break;
4830
4831 case elfcpp::R_POWERPC_REL32:
4832 case elfcpp::R_POWERPC_UADDR32:
4833 status = Reloc::addr32_u(view, value, overflow);
4834 break;
4835
4836 case elfcpp::R_POWERPC_ADDR24:
4837 case elfcpp::R_POWERPC_REL24:
4838 case elfcpp::R_PPC_PLTREL24:
4839 case elfcpp::R_PPC_LOCAL24PC:
4840 status = Reloc::addr24(view, value, overflow);
4841 break;
4842
4843 case elfcpp::R_POWERPC_GOT_DTPREL16:
4844 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4845 if (size == 64)
4846 {
4847 status = Reloc::addr16_ds(view, value, overflow);
4848 break;
4849 }
4850 case elfcpp::R_POWERPC_ADDR16:
4851 case elfcpp::R_POWERPC_REL16:
4852 case elfcpp::R_PPC64_TOC16:
4853 case elfcpp::R_POWERPC_GOT16:
4854 case elfcpp::R_POWERPC_SECTOFF:
4855 case elfcpp::R_POWERPC_TPREL16:
4856 case elfcpp::R_POWERPC_DTPREL16:
4857 case elfcpp::R_POWERPC_GOT_TLSGD16:
4858 case elfcpp::R_POWERPC_GOT_TLSLD16:
4859 case elfcpp::R_POWERPC_GOT_TPREL16:
4860 case elfcpp::R_POWERPC_ADDR16_LO:
4861 case elfcpp::R_POWERPC_REL16_LO:
4862 case elfcpp::R_PPC64_TOC16_LO:
4863 case elfcpp::R_POWERPC_GOT16_LO:
4864 case elfcpp::R_POWERPC_SECTOFF_LO:
4865 case elfcpp::R_POWERPC_TPREL16_LO:
4866 case elfcpp::R_POWERPC_DTPREL16_LO:
4867 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4868 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4869 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4870 status = Reloc::addr16(view, value, overflow);
4871 break;
4872
4873 case elfcpp::R_POWERPC_UADDR16:
4874 status = Reloc::addr16_u(view, value, overflow);
4875 break;
4876
4877 case elfcpp::R_POWERPC_ADDR16_HI:
4878 case elfcpp::R_POWERPC_REL16_HI:
4879 case elfcpp::R_PPC64_TOC16_HI:
4880 case elfcpp::R_POWERPC_GOT16_HI:
4881 case elfcpp::R_POWERPC_SECTOFF_HI:
4882 case elfcpp::R_POWERPC_TPREL16_HI:
4883 case elfcpp::R_POWERPC_DTPREL16_HI:
4884 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4885 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4886 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4887 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4888 Reloc::addr16_hi(view, value);
4889 break;
4890
4891 case elfcpp::R_POWERPC_ADDR16_HA:
4892 case elfcpp::R_POWERPC_REL16_HA:
4893 case elfcpp::R_PPC64_TOC16_HA:
4894 case elfcpp::R_POWERPC_GOT16_HA:
4895 case elfcpp::R_POWERPC_SECTOFF_HA:
4896 case elfcpp::R_POWERPC_TPREL16_HA:
4897 case elfcpp::R_POWERPC_DTPREL16_HA:
4898 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4899 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4900 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4901 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4902 Reloc::addr16_ha(view, value);
4903 break;
4904
4905 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4906 if (size == 32)
4907 // R_PPC_EMB_NADDR16_LO
4908 goto unsupp;
4909 case elfcpp::R_PPC64_ADDR16_HIGHER:
4910 case elfcpp::R_PPC64_TPREL16_HIGHER:
4911 Reloc::addr16_hi2(view, value);
4912 break;
4913
4914 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4915 if (size == 32)
4916 // R_PPC_EMB_NADDR16_HI
4917 goto unsupp;
4918 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4919 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4920 Reloc::addr16_ha2(view, value);
4921 break;
4922
4923 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4924 if (size == 32)
4925 // R_PPC_EMB_NADDR16_HA
4926 goto unsupp;
4927 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4928 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4929 Reloc::addr16_hi3(view, value);
4930 break;
4931
4932 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4933 if (size == 32)
4934 // R_PPC_EMB_SDAI16
4935 goto unsupp;
4936 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4937 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4938 Reloc::addr16_ha3(view, value);
4939 break;
4940
4941 case elfcpp::R_PPC64_DTPREL16_DS:
4942 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4943 if (size == 32)
4944 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
4945 goto unsupp;
4946 case elfcpp::R_PPC64_TPREL16_DS:
4947 case elfcpp::R_PPC64_TPREL16_LO_DS:
4948 if (size == 32)
4949 // R_PPC_TLSGD, R_PPC_TLSLD
4950 break;
4951 case elfcpp::R_PPC64_ADDR16_DS:
4952 case elfcpp::R_PPC64_ADDR16_LO_DS:
4953 case elfcpp::R_PPC64_TOC16_DS:
4954 case elfcpp::R_PPC64_TOC16_LO_DS:
4955 case elfcpp::R_PPC64_GOT16_DS:
4956 case elfcpp::R_PPC64_GOT16_LO_DS:
4957 case elfcpp::R_PPC64_SECTOFF_DS:
4958 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4959 status = Reloc::addr16_ds(view, value, overflow);
4960 break;
4961
4962 case elfcpp::R_POWERPC_ADDR14:
4963 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4964 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4965 case elfcpp::R_POWERPC_REL14:
4966 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4967 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4968 status = Reloc::addr14(view, value, overflow);
4969 break;
4970
4971 case elfcpp::R_POWERPC_COPY:
4972 case elfcpp::R_POWERPC_GLOB_DAT:
4973 case elfcpp::R_POWERPC_JMP_SLOT:
4974 case elfcpp::R_POWERPC_RELATIVE:
4975 case elfcpp::R_POWERPC_DTPMOD:
4976 case elfcpp::R_PPC64_JMP_IREL:
4977 case elfcpp::R_POWERPC_IRELATIVE:
4978 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4979 _("unexpected reloc %u in object file"),
4980 r_type);
4981 break;
4982
4983 case elfcpp::R_PPC_EMB_SDA21:
4984 if (size == 32)
4985 goto unsupp;
4986 else
4987 {
4988 // R_PPC64_TOCSAVE. For the time being this can be ignored.
4989 }
4990 break;
4991
4992 case elfcpp::R_PPC_EMB_SDA2I16:
4993 case elfcpp::R_PPC_EMB_SDA2REL:
4994 if (size == 32)
4995 goto unsupp;
4996 // R_PPC64_TLSGD, R_PPC64_TLSLD
4997 break;
4998
4999 case elfcpp::R_POWERPC_PLT32:
5000 case elfcpp::R_POWERPC_PLTREL32:
5001 case elfcpp::R_POWERPC_PLT16_LO:
5002 case elfcpp::R_POWERPC_PLT16_HI:
5003 case elfcpp::R_POWERPC_PLT16_HA:
5004 case elfcpp::R_PPC_SDAREL16:
5005 case elfcpp::R_POWERPC_ADDR30:
5006 case elfcpp::R_PPC64_PLT64:
5007 case elfcpp::R_PPC64_PLTREL64:
5008 case elfcpp::R_PPC64_PLTGOT16:
5009 case elfcpp::R_PPC64_PLTGOT16_LO:
5010 case elfcpp::R_PPC64_PLTGOT16_HI:
5011 case elfcpp::R_PPC64_PLTGOT16_HA:
5012 case elfcpp::R_PPC64_PLT16_LO_DS:
5013 case elfcpp::R_PPC64_PLTGOT16_DS:
5014 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
5015 case elfcpp::R_PPC_EMB_RELSEC16:
5016 case elfcpp::R_PPC_EMB_RELST_LO:
5017 case elfcpp::R_PPC_EMB_RELST_HI:
5018 case elfcpp::R_PPC_EMB_RELST_HA:
5019 case elfcpp::R_PPC_EMB_BIT_FLD:
5020 case elfcpp::R_PPC_EMB_RELSDA:
5021 case elfcpp::R_PPC_TOC16:
5022 default:
5023 unsupp:
5024 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5025 _("unsupported reloc %u"),
5026 r_type);
5027 break;
5028 }
5029 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
5030 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5031 _("relocation overflow"));
5032
5033 return true;
5034 }
5035
5036 // Relocate section data.
5037
5038 template<int size, bool big_endian>
5039 void
5040 Target_powerpc<size, big_endian>::relocate_section(
5041 const Relocate_info<size, big_endian>* relinfo,
5042 unsigned int sh_type,
5043 const unsigned char* prelocs,
5044 size_t reloc_count,
5045 Output_section* output_section,
5046 bool needs_special_offset_handling,
5047 unsigned char* view,
5048 Address address,
5049 section_size_type view_size,
5050 const Reloc_symbol_changes* reloc_symbol_changes)
5051 {
5052 typedef Target_powerpc<size, big_endian> Powerpc;
5053 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
5054
5055 gold_assert(sh_type == elfcpp::SHT_RELA);
5056
5057 unsigned char *opd_rel = NULL;
5058 Powerpc_relobj<size, big_endian>* const object
5059 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5060 if (size == 64
5061 && relinfo->data_shndx == object->opd_shndx())
5062 {
5063 // Rewrite opd relocs, omitting those for discarded sections
5064 // to silence gold::relocate_section errors.
5065 const int reloc_size
5066 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5067 opd_rel = new unsigned char[reloc_count * reloc_size];
5068 const unsigned char* rrel = prelocs;
5069 unsigned char* wrel = opd_rel;
5070
5071 for (size_t i = 0;
5072 i < reloc_count;
5073 ++i, rrel += reloc_size, wrel += reloc_size)
5074 {
5075 typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5076 reloc(rrel);
5077 typename elfcpp::Elf_types<size>::Elf_WXword r_info
5078 = reloc.get_r_info();
5079 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5080 Address r_off = reloc.get_r_offset();
5081 if (r_type == elfcpp::R_PPC64_TOC)
5082 r_off -= 8;
5083 bool is_discarded = object->get_opd_discard(r_off);
5084
5085 // Reloc number is reported in some errors, so keep all relocs.
5086 if (is_discarded)
5087 memset(wrel, 0, reloc_size);
5088 else
5089 memcpy(wrel, rrel, reloc_size);
5090 }
5091 prelocs = opd_rel;
5092 }
5093
5094 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
5095 Powerpc_relocate>(
5096 relinfo,
5097 this,
5098 prelocs,
5099 reloc_count,
5100 output_section,
5101 needs_special_offset_handling,
5102 view,
5103 address,
5104 view_size,
5105 reloc_symbol_changes);
5106
5107 if (opd_rel != NULL)
5108 delete[] opd_rel;
5109 }
5110
5111 class Powerpc_scan_relocatable_reloc
5112 {
5113 public:
5114 // Return the strategy to use for a local symbol which is not a
5115 // section symbol, given the relocation type.
5116 inline Relocatable_relocs::Reloc_strategy
5117 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
5118 {
5119 if (r_type == 0 && r_sym == 0)
5120 return Relocatable_relocs::RELOC_DISCARD;
5121 return Relocatable_relocs::RELOC_COPY;
5122 }
5123
5124 // Return the strategy to use for a local symbol which is a section
5125 // symbol, given the relocation type.
5126 inline Relocatable_relocs::Reloc_strategy
5127 local_section_strategy(unsigned int, Relobj*)
5128 {
5129 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
5130 }
5131
5132 // Return the strategy to use for a global symbol, given the
5133 // relocation type, the object, and the symbol index.
5134 inline Relocatable_relocs::Reloc_strategy
5135 global_strategy(unsigned int r_type, Relobj*, unsigned int)
5136 {
5137 if (r_type == elfcpp::R_PPC_PLTREL24)
5138 return Relocatable_relocs::RELOC_SPECIAL;
5139 return Relocatable_relocs::RELOC_COPY;
5140 }
5141 };
5142
5143 // Scan the relocs during a relocatable link.
5144
5145 template<int size, bool big_endian>
5146 void
5147 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
5148 Symbol_table* symtab,
5149 Layout* layout,
5150 Sized_relobj_file<size, big_endian>* object,
5151 unsigned int data_shndx,
5152 unsigned int sh_type,
5153 const unsigned char* prelocs,
5154 size_t reloc_count,
5155 Output_section* output_section,
5156 bool needs_special_offset_handling,
5157 size_t local_symbol_count,
5158 const unsigned char* plocal_symbols,
5159 Relocatable_relocs* rr)
5160 {
5161 gold_assert(sh_type == elfcpp::SHT_RELA);
5162
5163 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
5164 Powerpc_scan_relocatable_reloc>(
5165 symtab,
5166 layout,
5167 object,
5168 data_shndx,
5169 prelocs,
5170 reloc_count,
5171 output_section,
5172 needs_special_offset_handling,
5173 local_symbol_count,
5174 plocal_symbols,
5175 rr);
5176 }
5177
5178 // Emit relocations for a section.
5179 // This is a modified version of the function by the same name in
5180 // target-reloc.h. Using relocate_special_relocatable for
5181 // R_PPC_PLTREL24 would require duplication of the entire body of the
5182 // loop, so we may as well duplicate the whole thing.
5183
5184 template<int size, bool big_endian>
5185 void
5186 Target_powerpc<size, big_endian>::relocate_relocs(
5187 const Relocate_info<size, big_endian>* relinfo,
5188 unsigned int sh_type,
5189 const unsigned char* prelocs,
5190 size_t reloc_count,
5191 Output_section* output_section,
5192 off_t offset_in_output_section,
5193 const Relocatable_relocs* rr,
5194 unsigned char*,
5195 Address view_address,
5196 section_size_type,
5197 unsigned char* reloc_view,
5198 section_size_type reloc_view_size)
5199 {
5200 gold_assert(sh_type == elfcpp::SHT_RELA);
5201
5202 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
5203 Reltype;
5204 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
5205 Reltype_write;
5206 const int reloc_size
5207 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
5208
5209 Powerpc_relobj<size, big_endian>* const object
5210 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5211 const unsigned int local_count = object->local_symbol_count();
5212 unsigned int got2_shndx = object->got2_shndx();
5213 Address got2_addend = 0;
5214 if (got2_shndx != 0)
5215 {
5216 got2_addend = object->get_output_section_offset(got2_shndx);
5217 gold_assert(got2_addend != invalid_address);
5218 }
5219
5220 unsigned char* pwrite = reloc_view;
5221 bool zap_next = false;
5222 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
5223 {
5224 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
5225 if (strategy == Relocatable_relocs::RELOC_DISCARD)
5226 continue;
5227
5228 Reltype reloc(prelocs);
5229 Reltype_write reloc_write(pwrite);
5230
5231 Address offset = reloc.get_r_offset();
5232 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
5233 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
5234 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
5235 const unsigned int orig_r_sym = r_sym;
5236 typename elfcpp::Elf_types<size>::Elf_Swxword addend
5237 = reloc.get_r_addend();
5238 const Symbol* gsym = NULL;
5239
5240 if (zap_next)
5241 {
5242 // We could arrange to discard these and other relocs for
5243 // tls optimised sequences in the strategy methods, but for
5244 // now do as BFD ld does.
5245 r_type = elfcpp::R_POWERPC_NONE;
5246 zap_next = false;
5247 }
5248
5249 // Get the new symbol index.
5250 if (r_sym < local_count)
5251 {
5252 switch (strategy)
5253 {
5254 case Relocatable_relocs::RELOC_COPY:
5255 case Relocatable_relocs::RELOC_SPECIAL:
5256 if (r_sym != 0)
5257 {
5258 r_sym = object->symtab_index(r_sym);
5259 gold_assert(r_sym != -1U);
5260 }
5261 break;
5262
5263 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
5264 {
5265 // We are adjusting a section symbol. We need to find
5266 // the symbol table index of the section symbol for
5267 // the output section corresponding to input section
5268 // in which this symbol is defined.
5269 gold_assert(r_sym < local_count);
5270 bool is_ordinary;
5271 unsigned int shndx =
5272 object->local_symbol_input_shndx(r_sym, &is_ordinary);
5273 gold_assert(is_ordinary);
5274 Output_section* os = object->output_section(shndx);
5275 gold_assert(os != NULL);
5276 gold_assert(os->needs_symtab_index());
5277 r_sym = os->symtab_index();
5278 }
5279 break;
5280
5281 default:
5282 gold_unreachable();
5283 }
5284 }
5285 else
5286 {
5287 gsym = object->global_symbol(r_sym);
5288 gold_assert(gsym != NULL);
5289 if (gsym->is_forwarder())
5290 gsym = relinfo->symtab->resolve_forwards(gsym);
5291
5292 gold_assert(gsym->has_symtab_index());
5293 r_sym = gsym->symtab_index();
5294 }
5295
5296 // Get the new offset--the location in the output section where
5297 // this relocation should be applied.
5298 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5299 offset += offset_in_output_section;
5300 else
5301 {
5302 section_offset_type sot_offset =
5303 convert_types<section_offset_type, Address>(offset);
5304 section_offset_type new_sot_offset =
5305 output_section->output_offset(object, relinfo->data_shndx,
5306 sot_offset);
5307 gold_assert(new_sot_offset != -1);
5308 offset = new_sot_offset;
5309 }
5310
5311 // In an object file, r_offset is an offset within the section.
5312 // In an executable or dynamic object, generated by
5313 // --emit-relocs, r_offset is an absolute address.
5314 if (!parameters->options().relocatable())
5315 {
5316 offset += view_address;
5317 if (static_cast<Address>(offset_in_output_section) != invalid_address)
5318 offset -= offset_in_output_section;
5319 }
5320
5321 // Handle the reloc addend based on the strategy.
5322 if (strategy == Relocatable_relocs::RELOC_COPY)
5323 ;
5324 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
5325 {
5326 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
5327 addend = psymval->value(object, addend);
5328 }
5329 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
5330 {
5331 if (addend >= 32768)
5332 addend += got2_addend;
5333 }
5334 else
5335 gold_unreachable();
5336
5337 if (!parameters->options().relocatable())
5338 {
5339 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5340 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5341 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5342 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5343 {
5344 // First instruction of a global dynamic sequence,
5345 // arg setup insn.
5346 const bool final = gsym == NULL || gsym->final_value_is_known();
5347 switch (this->optimize_tls_gd(final))
5348 {
5349 case tls::TLSOPT_TO_IE:
5350 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5351 - elfcpp::R_POWERPC_GOT_TLSGD16);
5352 break;
5353 case tls::TLSOPT_TO_LE:
5354 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5355 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_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 break;
5363 default:
5364 break;
5365 }
5366 }
5367 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5368 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5369 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5370 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5371 {
5372 // First instruction of a local dynamic sequence,
5373 // arg setup insn.
5374 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5375 {
5376 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5377 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5378 {
5379 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5380 const Output_section* os = relinfo->layout->tls_segment()
5381 ->first_section();
5382 gold_assert(os != NULL);
5383 gold_assert(os->needs_symtab_index());
5384 r_sym = os->symtab_index();
5385 addend = dtp_offset;
5386 }
5387 else
5388 {
5389 r_type = elfcpp::R_POWERPC_NONE;
5390 offset -= 2 * big_endian;
5391 }
5392 }
5393 }
5394 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5395 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5396 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5397 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5398 {
5399 // First instruction of initial exec sequence.
5400 const bool final = gsym == NULL || gsym->final_value_is_known();
5401 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5402 {
5403 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5404 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5405 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5406 else
5407 {
5408 r_type = elfcpp::R_POWERPC_NONE;
5409 offset -= 2 * big_endian;
5410 }
5411 }
5412 }
5413 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5414 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5415 {
5416 // Second instruction of a global dynamic sequence,
5417 // the __tls_get_addr call
5418 const bool final = gsym == NULL || gsym->final_value_is_known();
5419 switch (this->optimize_tls_gd(final))
5420 {
5421 case tls::TLSOPT_TO_IE:
5422 r_type = elfcpp::R_POWERPC_NONE;
5423 zap_next = true;
5424 break;
5425 case tls::TLSOPT_TO_LE:
5426 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5427 offset += 2 * big_endian;
5428 zap_next = true;
5429 break;
5430 default:
5431 break;
5432 }
5433 }
5434 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5435 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5436 {
5437 // Second instruction of a local dynamic sequence,
5438 // the __tls_get_addr call
5439 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
5440 {
5441 const Output_section* os = relinfo->layout->tls_segment()
5442 ->first_section();
5443 gold_assert(os != NULL);
5444 gold_assert(os->needs_symtab_index());
5445 r_sym = os->symtab_index();
5446 addend = dtp_offset;
5447 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5448 offset += 2 * big_endian;
5449 zap_next = true;
5450 }
5451 }
5452 else if (r_type == elfcpp::R_POWERPC_TLS)
5453 {
5454 // Second instruction of an initial exec sequence
5455 const bool final = gsym == NULL || gsym->final_value_is_known();
5456 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
5457 {
5458 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5459 offset += 2 * big_endian;
5460 }
5461 }
5462 }
5463
5464 reloc_write.put_r_offset(offset);
5465 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
5466 reloc_write.put_r_addend(addend);
5467
5468 pwrite += reloc_size;
5469 }
5470
5471 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
5472 == reloc_view_size);
5473 }
5474
5475 // Return the value to use for a dynamic which requires special
5476 // treatment. This is how we support equality comparisons of function
5477 // pointers across shared library boundaries, as described in the
5478 // processor specific ABI supplement.
5479
5480 template<int size, bool big_endian>
5481 uint64_t
5482 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
5483 {
5484 if (size == 32)
5485 {
5486 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5487 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5488 unsigned int glink_index = glink->find_entry(gsym);
5489 return glink->address() + glink_index * glink->glink_entry_size();
5490 }
5491 else
5492 gold_unreachable();
5493 }
5494
5495 // Return the PLT address to use for a local symbol.
5496 template<int size, bool big_endian>
5497 uint64_t
5498 Target_powerpc<size, big_endian>::do_plt_address_for_local(
5499 const Relobj* object,
5500 unsigned int symndx) const
5501 {
5502 if (size == 32)
5503 {
5504 const Sized_relobj<size, big_endian>* relobj
5505 = static_cast<const Sized_relobj<size, big_endian>*>(object);
5506 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5507 unsigned int glink_index = glink->find_entry(relobj->sized_relobj(),
5508 symndx);
5509 return glink->address() + glink_index * glink->glink_entry_size();
5510 }
5511 else
5512 gold_unreachable();
5513 }
5514
5515 // Return the PLT address to use for a global symbol.
5516 template<int size, bool big_endian>
5517 uint64_t
5518 Target_powerpc<size, big_endian>::do_plt_address_for_global(
5519 const Symbol* gsym) const
5520 {
5521 if (size == 32)
5522 {
5523 const Output_data_glink<size, big_endian>* glink = this->glink_section();
5524 unsigned int glink_index = glink->find_entry(gsym);
5525 return glink->address() + glink_index * glink->glink_entry_size();
5526 }
5527 else
5528 gold_unreachable();
5529 }
5530
5531 // Return the offset to use for the GOT_INDX'th got entry which is
5532 // for a local tls symbol specified by OBJECT, SYMNDX.
5533 template<int size, bool big_endian>
5534 int64_t
5535 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
5536 const Relobj* object,
5537 unsigned int symndx,
5538 unsigned int got_indx) const
5539 {
5540 const Powerpc_relobj<size, big_endian>* ppc_object
5541 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
5542 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
5543 {
5544 for (Got_type got_type = GOT_TYPE_TLSGD;
5545 got_type <= GOT_TYPE_TPREL;
5546 got_type = Got_type(got_type + 1))
5547 if (ppc_object->local_has_got_offset(symndx, got_type))
5548 {
5549 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
5550 if (got_type == GOT_TYPE_TLSGD)
5551 off += size / 8;
5552 if (off == got_indx * (size / 8))
5553 {
5554 if (got_type == GOT_TYPE_TPREL)
5555 return -tp_offset;
5556 else
5557 return -dtp_offset;
5558 }
5559 }
5560 }
5561 gold_unreachable();
5562 }
5563
5564 // Return the offset to use for the GOT_INDX'th got entry which is
5565 // for global tls symbol GSYM.
5566 template<int size, bool big_endian>
5567 int64_t
5568 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
5569 Symbol* gsym,
5570 unsigned int got_indx) const
5571 {
5572 if (gsym->type() == elfcpp::STT_TLS)
5573 {
5574 for (Got_type got_type = GOT_TYPE_TLSGD;
5575 got_type <= GOT_TYPE_TPREL;
5576 got_type = Got_type(got_type + 1))
5577 if (gsym->has_got_offset(got_type))
5578 {
5579 unsigned int off = gsym->got_offset(got_type);
5580 if (got_type == GOT_TYPE_TLSGD)
5581 off += size / 8;
5582 if (off == got_indx * (size / 8))
5583 {
5584 if (got_type == GOT_TYPE_TPREL)
5585 return -tp_offset;
5586 else
5587 return -dtp_offset;
5588 }
5589 }
5590 }
5591 gold_unreachable();
5592 }
5593
5594 // The selector for powerpc object files.
5595
5596 template<int size, bool big_endian>
5597 class Target_selector_powerpc : public Target_selector
5598 {
5599 public:
5600 Target_selector_powerpc()
5601 : Target_selector(elfcpp::EM_NONE, size, big_endian,
5602 (size == 64
5603 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
5604 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
5605 (size == 64
5606 ? (big_endian ? "elf64ppc" : "elf64lppc")
5607 : (big_endian ? "elf32ppc" : "elf32lppc")))
5608 { }
5609
5610 virtual Target*
5611 do_recognize(Input_file*, off_t, int machine, int, int)
5612 {
5613 switch (size)
5614 {
5615 case 64:
5616 if (machine != elfcpp::EM_PPC64)
5617 return NULL;
5618 break;
5619
5620 case 32:
5621 if (machine != elfcpp::EM_PPC)
5622 return NULL;
5623 break;
5624
5625 default:
5626 return NULL;
5627 }
5628
5629 return this->instantiate_target();
5630 }
5631
5632 virtual Target*
5633 do_instantiate_target()
5634 { return new Target_powerpc<size, big_endian>(); }
5635 };
5636
5637 Target_selector_powerpc<32, true> target_selector_ppc32;
5638 Target_selector_powerpc<32, false> target_selector_ppc32le;
5639 Target_selector_powerpc<64, true> target_selector_ppc64;
5640 Target_selector_powerpc<64, false> target_selector_ppc64le;
5641
5642 } // End anonymous namespace.